4 * Copyright (C) 2004 - 2009 Vladislav Bolkhovitin <vst@vlnb.net>
5 * Copyright (C) 2004 - 2005 Leonid Stoljar
6 * Copyright (C) 2007 - 2009 ID7 Ltd.
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation, version 2
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
19 #include <linux/module.h>
21 #include <linux/init.h>
22 #include <linux/kernel.h>
23 #include <linux/errno.h>
24 #include <linux/list.h>
25 #include <linux/spinlock.h>
26 #include <linux/slab.h>
27 #include <linux/sched.h>
28 #include <linux/unistd.h>
29 #include <linux/string.h>
30 #include <linux/kthread.h>
33 #include "scst_priv.h"
36 #if defined(CONFIG_HIGHMEM4G) || defined(CONFIG_HIGHMEM64G)
37 #warning "HIGHMEM kernel configurations are fully supported, but not\
38 recommended for performance reasons. Consider changing VMSPLIT\
39 option or use a 64-bit configuration instead. See README file for\
43 #if !defined(SCSI_EXEC_REQ_FIFO_DEFINED)
44 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 30)
45 #if !defined(CONFIG_SCST_STRICT_SERIALIZING)
46 #warning "Patch scst_exec_req_fifo-<kernel-version> was not applied on\
47 your kernel and CONFIG_SCST_STRICT_SERIALIZING isn't defined.\
48 Pass-through dev handlers will not work."
49 #endif /* !defined(CONFIG_SCST_STRICT_SERIALIZING) */
50 #else /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 30) */
51 #warning "Patch scst_exec_req_fifo-<kernel-version> was not applied on\
52 your kernel. Pass-through dev handlers will not work."
53 #endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 30) */
54 #endif /* !defined(SCSI_EXEC_REQ_FIFO_DEFINED) */
56 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 26)
57 #if !defined(SCST_IO_CONTEXT)
58 #warning "Patch io_context-<kernel-version> was not applied\
59 on your kernel. SCST will be working with not the best performance."
62 #warning "There is no patch io_context-<kernel-version>\
63 for your kernel version. For performance reasons it is strongly recommended\
64 to upgrade your kernel to version >= 2.6.26.x."
68 ** SCST global variables. They are all uninitialized to have their layout in
69 ** memory be exactly as specified. Otherwise compiler puts zero-initialized
70 ** variable separately from nonzero-initialized ones.
74 * All targets, devices and dev_types management is done under this mutex.
76 * It must NOT be used in any works (schedule_work(), etc.), because
77 * otherwise a deadlock (double lock, actually) is possible, e.g., with
78 * scst_user detach_tgt(), which is called under scst_mutex and calls
79 * flush_scheduled_work().
81 struct mutex scst_mutex;
83 /* All 3 protected by scst_mutex */
84 struct list_head scst_template_list;
85 struct list_head scst_dev_list;
86 struct list_head scst_dev_type_list;
88 spinlock_t scst_main_lock;
90 static struct kmem_cache *scst_mgmt_cachep;
91 mempool_t *scst_mgmt_mempool;
92 static struct kmem_cache *scst_mgmt_stub_cachep;
93 mempool_t *scst_mgmt_stub_mempool;
94 static struct kmem_cache *scst_ua_cachep;
95 mempool_t *scst_ua_mempool;
96 static struct kmem_cache *scst_sense_cachep;
97 mempool_t *scst_sense_mempool;
98 static struct kmem_cache *scst_aen_cachep;
99 mempool_t *scst_aen_mempool;
100 struct kmem_cache *scst_tgtd_cachep;
101 struct kmem_cache *scst_sess_cachep;
102 struct kmem_cache *scst_acgd_cachep;
104 struct list_head scst_acg_list;
105 #ifdef CONFIG_SCST_PROC
106 struct scst_acg *scst_default_acg;
109 spinlock_t scst_init_lock;
110 wait_queue_head_t scst_init_cmd_list_waitQ;
111 struct list_head scst_init_cmd_list;
112 unsigned int scst_init_poll_cnt;
114 struct kmem_cache *scst_cmd_cachep;
116 #if defined(CONFIG_SCST_DEBUG) || defined(CONFIG_SCST_TRACING)
117 unsigned long scst_trace_flag;
120 unsigned long scst_flags;
121 atomic_t scst_cmd_count;
123 struct scst_cmd_lists scst_main_cmd_lists;
125 struct scst_tasklet scst_tasklets[NR_CPUS];
127 spinlock_t scst_mcmd_lock;
128 struct list_head scst_active_mgmt_cmd_list;
129 struct list_head scst_delayed_mgmt_cmd_list;
130 wait_queue_head_t scst_mgmt_cmd_list_waitQ;
132 wait_queue_head_t scst_mgmt_waitQ;
133 spinlock_t scst_mgmt_lock;
134 struct list_head scst_sess_init_list;
135 struct list_head scst_sess_shut_list;
137 wait_queue_head_t scst_dev_cmd_waitQ;
139 static struct mutex scst_suspend_mutex;
140 /* protected by scst_suspend_mutex */
141 static struct list_head scst_cmd_lists_list;
143 static int scst_threads;
144 struct mutex scst_global_threads_mutex;
145 u32 scst_nr_global_threads;
146 static struct list_head scst_global_threads_list;
147 static struct task_struct *scst_init_cmd_thread;
148 static struct task_struct *scst_mgmt_thread;
149 static struct task_struct *scst_mgmt_cmd_thread;
151 static int suspend_count;
153 static int scst_virt_dev_last_id; /* protected by scst_mutex */
155 static unsigned int scst_max_cmd_mem;
156 unsigned int scst_max_dev_cmd_mem;
158 module_param_named(scst_threads, scst_threads, int, 0);
159 MODULE_PARM_DESC(scst_threads, "SCSI target threads count");
161 module_param_named(scst_max_cmd_mem, scst_max_cmd_mem, int, S_IRUGO);
162 MODULE_PARM_DESC(scst_max_cmd_mem, "Maximum memory allowed to be consumed by "
163 "all SCSI commands of all devices at any given time in MB");
165 module_param_named(scst_max_dev_cmd_mem, scst_max_dev_cmd_mem, int, S_IRUGO);
166 MODULE_PARM_DESC(scst_max_dev_cmd_mem, "Maximum memory allowed to be consumed "
167 "by all SCSI commands of a device at any given time in MB");
169 struct scst_dev_type scst_null_devtype = {
173 static void __scst_resume_activity(void);
175 int __scst_register_target_template(struct scst_tgt_template *vtt,
179 struct scst_tgt_template *t;
180 static DEFINE_MUTEX(m);
184 INIT_LIST_HEAD(&vtt->tgt_list);
186 if (strcmp(version, SCST_INTERFACE_VERSION) != 0) {
187 PRINT_ERROR("Incorrect version of target %s", vtt->name);
193 PRINT_ERROR("Target driver %s must have "
194 "detect() method.", vtt->name);
200 PRINT_ERROR("Target driver %s must have "
201 "release() method.", vtt->name);
206 if (!vtt->xmit_response) {
207 PRINT_ERROR("Target driver %s must have "
208 "xmit_response() method.", vtt->name);
213 if (vtt->threads_num < 0) {
214 PRINT_ERROR("Wrong threads_num value %d for "
215 "target \"%s\"", vtt->threads_num,
221 #ifndef CONFIG_SCST_PROC
222 if (!vtt->enable_tgt || !vtt->is_tgt_enabled) {
223 PRINT_WARNING("Target driver %s doesn't have enable_tgt() "
224 "and/or is_tgt_enabled() method(s). This is unsafe "
225 "and can lead that initiators connected on the "
226 "initialization time can see an unexpected set of "
227 "devices or no devices at all!", vtt->name);
230 if (!vtt->no_proc_entry) {
231 res = scst_build_proc_target_dir_entries(vtt);
237 res = scst_create_tgtt_sysfs(vtt);
241 if (vtt->rdy_to_xfer == NULL)
242 vtt->rdy_to_xfer_atomic = 1;
244 if (mutex_lock_interruptible(&m) != 0)
247 if (mutex_lock_interruptible(&scst_mutex) != 0)
249 list_for_each_entry(t, &scst_template_list, scst_template_list_entry) {
250 if (strcmp(t->name, vtt->name) == 0) {
251 PRINT_ERROR("Target driver %s already registered",
253 mutex_unlock(&scst_mutex);
257 mutex_unlock(&scst_mutex);
259 TRACE_DBG("%s", "Calling target driver's detect()");
260 res = vtt->detect(vtt);
261 TRACE_DBG("Target driver's detect() returned %d", res);
263 PRINT_ERROR("%s", "The detect() routine failed");
268 mutex_lock(&scst_mutex);
269 list_add_tail(&vtt->scst_template_list_entry, &scst_template_list);
270 mutex_unlock(&scst_mutex);
274 PRINT_INFO("Target template %s registered successfully", vtt->name);
286 #ifdef CONFIG_SCST_PROC
287 scst_cleanup_proc_target_dir_entries(vtt);
289 scst_tgtt_sysfs_put(vtt);
292 PRINT_ERROR("Failed to register target template %s", vtt->name);
295 EXPORT_SYMBOL(__scst_register_target_template);
297 void scst_unregister_target_template(struct scst_tgt_template *vtt)
299 struct scst_tgt *tgt;
300 struct scst_tgt_template *t;
305 mutex_lock(&scst_mutex);
307 list_for_each_entry(t, &scst_template_list, scst_template_list_entry) {
308 if (strcmp(t->name, vtt->name) == 0) {
314 PRINT_ERROR("Target driver %s isn't registered", vtt->name);
319 list_for_each_entry(tgt, &vtt->tgt_list, tgt_list_entry) {
320 mutex_unlock(&scst_mutex);
321 scst_unregister(tgt);
322 mutex_lock(&scst_mutex);
325 list_del(&vtt->scst_template_list_entry);
327 mutex_unlock(&scst_mutex);
329 #ifdef CONFIG_SCST_PROC
330 scst_cleanup_proc_target_dir_entries(vtt);
333 scst_tgtt_sysfs_put(vtt);
335 PRINT_INFO("Target template %s unregistered successfully", vtt->name);
342 mutex_unlock(&scst_mutex);
345 EXPORT_SYMBOL(scst_unregister_target_template);
347 struct scst_tgt *scst_register(struct scst_tgt_template *vtt,
348 const char *target_name)
350 struct scst_tgt *tgt;
355 tgt = scst_alloc_tgt(vtt);
361 rc = scst_suspend_activity(true);
363 goto out_free_tgt_err;
365 if (mutex_lock_interruptible(&scst_mutex) != 0) {
367 goto out_resume_free;
370 if (target_name != NULL) {
371 #ifdef CONFIG_SCST_PROC
372 int len = strlen(target_name) +
373 strlen(SCST_DEFAULT_ACG_NAME) + 1 + 1;
375 tgt->default_group_name = kmalloc(len, GFP_KERNEL);
376 if (tgt->default_group_name == NULL) {
377 TRACE(TRACE_OUT_OF_MEM, "Allocation of default "
378 "group name failed (tgt %s)", target_name);
380 goto out_unlock_resume;
382 sprintf(tgt->default_group_name, "%s_%s", SCST_DEFAULT_ACG_NAME,
386 tgt->tgt_name = kmalloc(strlen(target_name) + 1, GFP_KERNEL);
387 if (tgt->tgt_name == NULL) {
388 TRACE(TRACE_OUT_OF_MEM, "Allocation of tgt name %s failed",
391 #ifdef CONFIG_SCST_PROC
392 goto out_free_def_name;
394 goto out_unlock_resume;
397 strcpy(tgt->tgt_name, target_name);
399 static int tgt_num; /* protected by scst_mutex */
400 int len = strlen(vtt->name) +
401 strlen(SCST_DEFAULT_TGT_NAME_SUFFIX) + 11 + 1;
403 tgt->tgt_name = kmalloc(len, GFP_KERNEL);
404 if (tgt->tgt_name == NULL) {
405 TRACE(TRACE_OUT_OF_MEM, "Allocation of tgt name failed "
406 "(template name %s)", vtt->name);
408 #ifdef CONFIG_SCST_PROC
409 goto out_free_def_name;
411 goto out_unlock_resume;
414 sprintf(tgt->tgt_name, "%s%s%d", vtt->name,
415 SCST_DEFAULT_TGT_NAME_SUFFIX, tgt_num++);
418 tgt->default_acg = scst_alloc_add_acg(tgt->tgt_name);
419 if (tgt->default_acg == NULL)
420 goto out_free_tgt_name;
422 #ifdef CONFIG_SCST_PROC
423 rc = scst_build_proc_target_entries(tgt);
428 rc = scst_create_tgt_sysfs(tgt);
430 #ifdef CONFIG_SCST_PROC
436 list_add_tail(&tgt->tgt_list_entry, &vtt->tgt_list);
438 mutex_unlock(&scst_mutex);
439 scst_resume_activity();
441 PRINT_INFO("Target %s (%p) for template %s registered successfully",
442 target_name, tgt, vtt->name);
448 #ifdef CONFIG_SCST_PROC
450 scst_cleanup_proc_target_entries(tgt);
454 scst_destroy_acg(tgt->default_acg);
457 kfree(tgt->tgt_name);
459 #ifdef CONFIG_SCST_PROC
461 kfree(tgt->default_group_name);
465 mutex_unlock(&scst_mutex);
468 scst_resume_activity();
471 scst_tgt_sysfs_put(tgt);
475 PRINT_ERROR("Failed to register target %s for template %s (error %d)",
476 target_name, vtt->name, rc);
479 EXPORT_SYMBOL(scst_register);
481 static inline int test_sess_list(struct scst_tgt *tgt)
484 mutex_lock(&scst_mutex);
485 res = list_empty(&tgt->sess_list);
486 mutex_unlock(&scst_mutex);
490 void scst_unregister(struct scst_tgt *tgt)
492 struct scst_session *sess;
493 struct scst_tgt_template *vtt = tgt->tgtt;
497 TRACE_DBG("%s", "Calling target driver's release()");
498 tgt->tgtt->release(tgt);
499 TRACE_DBG("%s", "Target driver's release() returned");
501 mutex_lock(&scst_mutex);
503 list_for_each_entry(sess, &tgt->sess_list, sess_list_entry) {
504 if (sess->shut_phase == SCST_SESS_SPH_READY) {
506 * Sometimes it's hard for target driver to track all
507 * its sessions (see scst_local, for example), so let's
510 mutex_unlock(&scst_mutex);
511 scst_unregister_session(sess, 0, NULL);
512 mutex_lock(&scst_mutex);
516 mutex_unlock(&scst_mutex);
518 TRACE_DBG("%s", "Waiting for sessions shutdown");
519 wait_event(tgt->unreg_waitQ, test_sess_list(tgt));
520 TRACE_DBG("%s", "wait_event() returned");
522 scst_suspend_activity(false);
523 mutex_lock(&scst_mutex);
525 list_del(&tgt->tgt_list_entry);
527 #ifdef CONFIG_SCST_PROC
528 scst_cleanup_proc_target_entries(tgt);
531 mutex_unlock(&scst_mutex);
532 scst_resume_activity();
535 * It should be before freeing of tgt_name, because acg_name
538 scst_destroy_acg(tgt->default_acg);
540 kfree(tgt->tgt_name);
541 #ifdef CONFIG_SCST_PROC
542 kfree(tgt->default_group_name);
545 del_timer_sync(&tgt->retry_timer);
547 scst_tgt_sysfs_put(tgt);
549 PRINT_INFO("Target %p for template %s unregistered successfully",
555 EXPORT_SYMBOL(scst_unregister);
557 static int scst_susp_wait(bool interruptible)
564 res = wait_event_interruptible_timeout(scst_dev_cmd_waitQ,
565 (atomic_read(&scst_cmd_count) == 0),
566 SCST_SUSPENDING_TIMEOUT);
568 __scst_resume_activity();
574 wait_event(scst_dev_cmd_waitQ,
575 atomic_read(&scst_cmd_count) == 0);
577 TRACE_MGMT_DBG("wait_event() returned %d", res);
583 int scst_suspend_activity(bool interruptible)
591 if (mutex_lock_interruptible(&scst_suspend_mutex) != 0) {
596 mutex_lock(&scst_suspend_mutex);
598 TRACE_MGMT_DBG("suspend_count %d", suspend_count);
600 if (suspend_count > 1)
603 set_bit(SCST_FLAG_SUSPENDING, &scst_flags);
604 set_bit(SCST_FLAG_SUSPENDED, &scst_flags);
606 * Assignment of SCST_FLAG_SUSPENDING and SCST_FLAG_SUSPENDED must be
607 * ordered with scst_cmd_count. Otherwise lockless logic in
608 * scst_translate_lun() and scst_mgmt_translate_lun() won't work.
610 smp_mb__after_set_bit();
613 * See comment in scst_user.c::dev_user_task_mgmt_fn() for more
614 * information about scst_user behavior.
616 * ToDo: make the global suspending unneeded (switch to per-device
617 * reference counting? That would mean to switch off from lockless
618 * implementation of scst_translate_lun().. )
621 if (atomic_read(&scst_cmd_count) != 0) {
622 PRINT_INFO("Waiting for %d active commands to complete... This "
623 "might take few minutes for disks or few hours for "
624 "tapes, if you use long executed commands, like "
625 "REWIND or FORMAT. In case, if you have a hung user "
626 "space device (i.e. made using scst_user module) not "
627 "responding to any commands, if might take virtually "
628 "forever until the corresponding user space "
629 "program recovers and starts responding or gets "
630 "killed.", atomic_read(&scst_cmd_count));
634 res = scst_susp_wait(interruptible);
638 clear_bit(SCST_FLAG_SUSPENDING, &scst_flags);
639 /* See comment about smp_mb() above */
640 smp_mb__after_clear_bit();
642 TRACE_MGMT_DBG("Waiting for %d active commands finally to complete",
643 atomic_read(&scst_cmd_count));
645 res = scst_susp_wait(interruptible);
650 PRINT_INFO("%s", "All active commands completed");
653 mutex_unlock(&scst_suspend_mutex);
660 clear_bit(SCST_FLAG_SUSPENDING, &scst_flags);
661 /* See comment about smp_mb() above */
662 smp_mb__after_clear_bit();
665 EXPORT_SYMBOL(scst_suspend_activity);
667 static void __scst_resume_activity(void)
669 struct scst_cmd_lists *l;
674 TRACE_MGMT_DBG("suspend_count %d left", suspend_count);
675 if (suspend_count > 0)
678 clear_bit(SCST_FLAG_SUSPENDED, &scst_flags);
680 * The barrier is needed to make sure all woken up threads see the
681 * cleared flag. Not sure if it's really needed, but let's be safe.
683 smp_mb__after_clear_bit();
685 list_for_each_entry(l, &scst_cmd_lists_list, lists_list_entry) {
686 wake_up_all(&l->cmd_list_waitQ);
688 wake_up_all(&scst_init_cmd_list_waitQ);
690 spin_lock_irq(&scst_mcmd_lock);
691 if (!list_empty(&scst_delayed_mgmt_cmd_list)) {
692 struct scst_mgmt_cmd *m;
693 m = list_entry(scst_delayed_mgmt_cmd_list.next, typeof(*m),
694 mgmt_cmd_list_entry);
695 TRACE_MGMT_DBG("Moving delayed mgmt cmd %p to head of active "
697 list_move(&m->mgmt_cmd_list_entry, &scst_active_mgmt_cmd_list);
699 spin_unlock_irq(&scst_mcmd_lock);
700 wake_up_all(&scst_mgmt_cmd_list_waitQ);
707 void scst_resume_activity(void)
711 mutex_lock(&scst_suspend_mutex);
712 __scst_resume_activity();
713 mutex_unlock(&scst_suspend_mutex);
718 EXPORT_SYMBOL(scst_resume_activity);
720 static int scst_register_device(struct scsi_device *scsidp)
723 struct scst_device *dev;
724 struct scst_dev_type *dt;
728 res = scst_suspend_activity(true);
732 if (mutex_lock_interruptible(&scst_mutex) != 0) {
737 res = scst_alloc_device(GFP_KERNEL, &dev);
741 dev->type = scsidp->type;
743 dev->virt_name = kmalloc(50, GFP_KERNEL);
744 if (dev->virt_name == NULL) {
745 PRINT_ERROR("%s", "Unable to alloc device name");
749 snprintf(dev->virt_name, 50, "%d:%d:%d:%d", scsidp->host->host_no,
750 scsidp->channel, scsidp->id, scsidp->lun);
752 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18)
753 dev->rq_disk = alloc_disk(1);
754 if (dev->rq_disk == NULL) {
755 PRINT_ERROR("Unable to alloc disk object for device %s",
758 /* virt_name will be freed in scst_free_dev() */
761 dev->rq_disk->major = SCST_MAJOR;
764 dev->scsi_dev = scsidp;
766 list_add_tail(&dev->dev_list_entry, &scst_dev_list);
768 res = scst_create_device_sysfs(dev);
772 list_for_each_entry(dt, &scst_dev_type_list, dev_type_list_entry) {
773 if (dt->type == scsidp->type) {
774 res = scst_assign_dev_handler(dev, dt);
782 mutex_unlock(&scst_mutex);
785 scst_resume_activity();
789 PRINT_INFO("Attached to scsi%d, channel %d, id %d, lun %d, "
790 "type %d", scsidp->host->host_no, scsidp->channel,
791 scsidp->id, scsidp->lun, scsidp->type);
793 PRINT_ERROR("Failed to attach to scsi%d, channel %d, id %d, "
794 "lun %d, type %d", scsidp->host->host_no,
795 scsidp->channel, scsidp->id, scsidp->lun, scsidp->type);
802 list_del(&dev->dev_list_entry);
803 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18)
804 put_disk(dev->rq_disk);
808 scst_device_sysfs_put(dev);
812 static void scst_unregister_device(struct scsi_device *scsidp)
814 struct scst_device *d, *dev = NULL;
815 struct scst_acg_dev *acg_dev, *aa;
819 scst_suspend_activity(false);
820 mutex_lock(&scst_mutex);
822 list_for_each_entry(d, &scst_dev_list, dev_list_entry) {
823 if (d->scsi_dev == scsidp) {
825 TRACE_DBG("Target device %p found", dev);
830 PRINT_ERROR("%s", "Target device not found");
834 list_del(&dev->dev_list_entry);
836 list_for_each_entry_safe(acg_dev, aa, &dev->dev_acg_dev_list,
837 dev_acg_dev_list_entry) {
838 scst_acg_remove_dev(acg_dev->acg, dev, true);
841 scst_assign_dev_handler(dev, &scst_null_devtype);
843 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18)
844 put_disk(dev->rq_disk);
847 scst_device_sysfs_put(dev);
849 PRINT_INFO("Detached from scsi%d, channel %d, id %d, lun %d, type %d",
850 scsidp->host->host_no, scsidp->channel, scsidp->id,
851 scsidp->lun, scsidp->type);
854 mutex_unlock(&scst_mutex);
855 scst_resume_activity();
861 static int scst_dev_handler_check(struct scst_dev_type *dev_handler)
865 if (dev_handler->parse == NULL) {
866 PRINT_ERROR("scst dev handler %s must have "
867 "parse() method.", dev_handler->name);
872 if (dev_handler->exec == NULL) {
873 #ifdef CONFIG_SCST_ALLOW_PASSTHROUGH_IO_SUBMIT_IN_SIRQ
874 dev_handler->exec_atomic = 1;
876 dev_handler->exec_atomic = 0;
880 if (dev_handler->dev_done == NULL)
881 dev_handler->dev_done_atomic = 1;
888 int scst_register_virtual_device(struct scst_dev_type *dev_handler,
889 const char *dev_name)
892 struct scst_device *dev = NULL;
896 if (dev_handler == NULL) {
897 PRINT_ERROR("%s: valid device handler must be supplied",
903 if (dev_name == NULL) {
904 PRINT_ERROR("%s: device name must be non-NULL", __func__);
909 res = scst_dev_handler_check(dev_handler);
913 res = scst_suspend_activity(true);
917 if (mutex_lock_interruptible(&scst_mutex) != 0) {
922 res = scst_alloc_device(GFP_KERNEL, &dev);
926 dev->type = dev_handler->type;
927 dev->scsi_dev = NULL;
928 dev->virt_name = kstrdup(dev_name, GFP_KERNEL);
929 if (dev->virt_name == NULL) {
930 PRINT_ERROR("Unable to allocate virt_name for dev %s",
935 dev->virt_id = scst_virt_dev_last_id++;
937 list_add_tail(&dev->dev_list_entry, &scst_dev_list);
941 rc = scst_create_device_sysfs(dev);
947 rc = scst_assign_dev_handler(dev, dev_handler);
954 mutex_unlock(&scst_mutex);
957 scst_resume_activity();
961 PRINT_INFO("Attached to virtual device %s (id %d)",
962 dev_name, dev->virt_id);
964 PRINT_INFO("Failed to attach to virtual device %s", dev_name);
970 list_del(&dev->dev_list_entry);
973 scst_device_sysfs_put(dev);
976 EXPORT_SYMBOL(scst_register_virtual_device);
978 void scst_unregister_virtual_device(int id)
980 struct scst_device *d, *dev = NULL;
981 struct scst_acg_dev *acg_dev, *aa;
985 scst_suspend_activity(false);
986 mutex_lock(&scst_mutex);
988 list_for_each_entry(d, &scst_dev_list, dev_list_entry) {
989 if (d->virt_id == id) {
991 TRACE_DBG("Target device %p (id %d) found", dev, id);
996 PRINT_ERROR("Target virtual device (id %d) not found", id);
1000 list_del(&dev->dev_list_entry);
1002 list_for_each_entry_safe(acg_dev, aa, &dev->dev_acg_dev_list,
1003 dev_acg_dev_list_entry) {
1004 scst_acg_remove_dev(acg_dev->acg, dev, true);
1007 scst_assign_dev_handler(dev, &scst_null_devtype);
1009 PRINT_INFO("Detached from virtual device %s (id %d)",
1010 dev->virt_name, dev->virt_id);
1012 scst_device_sysfs_put(dev);
1015 mutex_unlock(&scst_mutex);
1016 scst_resume_activity();
1021 EXPORT_SYMBOL(scst_unregister_virtual_device);
1023 int __scst_register_dev_driver(struct scst_dev_type *dev_type,
1024 const char *version)
1026 struct scst_dev_type *dt;
1027 struct scst_device *dev;
1033 if (strcmp(version, SCST_INTERFACE_VERSION) != 0) {
1034 PRINT_ERROR("Incorrect version of dev handler %s",
1040 res = scst_dev_handler_check(dev_type);
1044 #if !defined(SCSI_EXEC_REQ_FIFO_DEFINED)
1045 if (dev_type->exec == NULL) {
1046 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 30)
1047 #if !defined(CONFIG_SCST_STRICT_SERIALIZING)
1048 PRINT_ERROR("Pass-through dev handlers (handler \"%s\") not "
1049 "supported. Consider applying on your kernel patch "
1050 "scst_exec_req_fifo-<kernel-version> or define "
1051 "CONFIG_SCST_STRICT_SERIALIZING", dev_type->name);
1052 #endif /* !defined(CONFIG_SCST_STRICT_SERIALIZING) */
1053 #else /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 30) */
1054 PRINT_ERROR("Pass-through dev handlers (handler \"%s\") not "
1055 "supported. Consider applying on your kernel patch "
1056 "scst_exec_req_fifo-<kernel-version>", dev_type->name);
1057 #endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 30) */
1061 #endif /* !defined(SCSI_EXEC_REQ_FIFO_DEFINED) */
1063 res = scst_suspend_activity(true);
1067 if (mutex_lock_interruptible(&scst_mutex) != 0) {
1073 list_for_each_entry(dt, &scst_dev_type_list, dev_type_list_entry) {
1074 if (strcmp(dt->name, dev_type->name) == 0) {
1075 PRINT_ERROR("Device type handler \"%s\" already "
1084 #ifdef CONFIG_SCST_PROC
1085 if (!dev_type->no_proc) {
1086 res = scst_build_proc_dev_handler_dir_entries(dev_type);
1092 res = scst_create_devt_sysfs(dev_type);
1096 list_add_tail(&dev_type->dev_type_list_entry, &scst_dev_type_list);
1098 list_for_each_entry(dev, &scst_dev_list, dev_list_entry) {
1099 if (dev->scsi_dev == NULL || dev->handler != &scst_null_devtype)
1101 if (dev->scsi_dev->type == dev_type->type)
1102 scst_assign_dev_handler(dev, dev_type);
1105 mutex_unlock(&scst_mutex);
1106 scst_resume_activity();
1109 PRINT_INFO("Device handler \"%s\" for type %d registered "
1110 "successfully", dev_type->name, dev_type->type);
1114 TRACE_EXIT_RES(res);
1118 #ifdef CONFIG_SCST_PROC
1119 if (!dev_type->no_proc)
1120 scst_cleanup_proc_dev_handler_dir_entries(dev_type);
1122 scst_devt_sysfs_put(dev_type);
1125 mutex_unlock(&scst_mutex);
1128 scst_resume_activity();
1131 PRINT_ERROR("Failed to register device handler \"%s\" for type %d",
1132 dev_type->name, dev_type->type);
1135 EXPORT_SYMBOL(__scst_register_dev_driver);
1137 void scst_unregister_dev_driver(struct scst_dev_type *dev_type)
1139 struct scst_device *dev;
1140 struct scst_dev_type *dt;
1145 scst_suspend_activity(false);
1146 mutex_lock(&scst_mutex);
1148 list_for_each_entry(dt, &scst_dev_type_list, dev_type_list_entry) {
1149 if (strcmp(dt->name, dev_type->name) == 0) {
1155 PRINT_ERROR("Dev handler \"%s\" isn't registered",
1160 list_for_each_entry(dev, &scst_dev_list, dev_list_entry) {
1161 if (dev->handler == dev_type) {
1162 scst_assign_dev_handler(dev, &scst_null_devtype);
1163 TRACE_DBG("Dev handler removed from device %p", dev);
1167 list_del(&dev_type->dev_type_list_entry);
1169 mutex_unlock(&scst_mutex);
1170 scst_resume_activity();
1172 #ifdef CONFIG_SCST_PROC
1173 scst_cleanup_proc_dev_handler_dir_entries(dev_type);
1176 scst_devt_sysfs_put(dev_type);
1178 PRINT_INFO("Device handler \"%s\" for type %d unloaded",
1179 dev_type->name, dev_type->type);
1186 mutex_unlock(&scst_mutex);
1187 scst_resume_activity();
1190 EXPORT_SYMBOL(scst_unregister_dev_driver);
1192 int __scst_register_virtual_dev_driver(struct scst_dev_type *dev_type,
1193 const char *version)
1199 if (strcmp(version, SCST_INTERFACE_VERSION) != 0) {
1200 PRINT_ERROR("Incorrect version of virtual dev handler %s",
1206 res = scst_dev_handler_check(dev_type);
1210 #ifdef CONFIG_SCST_PROC
1211 if (!dev_type->no_proc) {
1212 res = scst_build_proc_dev_handler_dir_entries(dev_type);
1218 res = scst_create_devt_sysfs(dev_type);
1222 if (dev_type->type != -1) {
1223 PRINT_INFO("Virtual device handler %s for type %d "
1224 "registered successfully", dev_type->name,
1227 PRINT_INFO("Virtual device handler \"%s\" registered "
1228 "successfully", dev_type->name);
1232 TRACE_EXIT_RES(res);
1236 #ifdef CONFIG_SCST_PROC
1237 if (!dev_type->no_proc)
1238 scst_cleanup_proc_dev_handler_dir_entries(dev_type);
1241 scst_devt_sysfs_put(dev_type);
1244 PRINT_ERROR("Failed to register virtual device handler \"%s\"",
1248 EXPORT_SYMBOL(__scst_register_virtual_dev_driver);
1250 void scst_unregister_virtual_dev_driver(struct scst_dev_type *dev_type)
1254 #ifdef CONFIG_SCST_PROC
1255 if (!dev_type->no_proc)
1256 scst_cleanup_proc_dev_handler_dir_entries(dev_type);
1259 scst_devt_sysfs_put(dev_type);
1261 PRINT_INFO("Device handler \"%s\" unloaded", dev_type->name);
1266 EXPORT_SYMBOL(scst_unregister_virtual_dev_driver);
1268 /* Called under scst_mutex */
1269 int scst_add_dev_threads(struct scst_device *dev, int num)
1273 struct scst_cmd_thread_t *thr;
1278 list_for_each_entry(thr, &dev->threads_list, thread_list_entry) {
1282 for (i = 0; i < num; i++) {
1283 thr = kmalloc(sizeof(*thr), GFP_KERNEL);
1286 PRINT_ERROR("Failed to allocate thr %d", res);
1289 strncpy(nm, dev->handler->name, ARRAY_SIZE(nm)-1);
1290 nm[ARRAY_SIZE(nm)-1] = '\0';
1291 thr->cmd_thread = kthread_create(scst_cmd_thread,
1292 &dev->cmd_lists, "%sd%d_%d", nm, dev->dev_num, n++);
1293 if (IS_ERR(thr->cmd_thread)) {
1294 res = PTR_ERR(thr->cmd_thread);
1295 PRINT_ERROR("kthread_create() failed: %d", res);
1300 list_add(&thr->thread_list_entry, &dev->threads_list);
1303 * ToDo: better to use tgt_dev_io_context instead, but we
1304 * are not ready for that yet.
1306 __exit_io_context(thr->cmd_thread->io_context);
1307 thr->cmd_thread->io_context = ioc_task_link(dev->dev_io_ctx);
1308 TRACE_DBG("Setting dev io ctx %p on thr %d", dev->dev_io_ctx,
1309 thr->cmd_thread->pid);
1311 wake_up_process(thr->cmd_thread);
1315 TRACE_EXIT_RES(res);
1319 scst_del_dev_threads(dev, i);
1323 /* Called under scst_mutex and suspended activity */
1324 static int scst_create_dev_threads(struct scst_device *dev)
1331 if (dev->handler->threads_num <= 0)
1334 threads_num = dev->handler->threads_num;
1336 spin_lock_init(&dev->cmd_lists.cmd_list_lock);
1337 INIT_LIST_HEAD(&dev->cmd_lists.active_cmd_list);
1338 init_waitqueue_head(&dev->cmd_lists.cmd_list_waitQ);
1340 res = scst_add_dev_threads(dev, threads_num);
1344 mutex_lock(&scst_suspend_mutex);
1345 list_add_tail(&dev->cmd_lists.lists_list_entry,
1346 &scst_cmd_lists_list);
1347 mutex_unlock(&scst_suspend_mutex);
1349 dev->p_cmd_lists = &dev->cmd_lists;
1352 TRACE_EXIT_RES(res);
1356 /* Called under scst_mutex */
1357 void scst_del_dev_threads(struct scst_device *dev, int num)
1359 struct scst_cmd_thread_t *ct, *tmp;
1367 list_for_each_entry_safe_reverse(ct, tmp, &dev->threads_list,
1368 thread_list_entry) {
1370 struct scst_tgt_dev *tgt_dev;
1372 list_for_each_entry(tgt_dev, &dev->dev_tgt_dev_list,
1373 dev_tgt_dev_list_entry) {
1374 struct scst_thr_data_hdr *td;
1375 td = __scst_find_thr_data(tgt_dev, ct->cmd_thread);
1377 scst_thr_data_put(td);
1382 rc = kthread_stop(ct->cmd_thread);
1384 TRACE_MGMT_DBG("kthread_stop() failed: %d", rc);
1386 list_del(&ct->thread_list_entry);
1389 if ((num > 0) && (++i >= num))
1398 /* Called under scst_mutex and suspended activity */
1399 static void scst_stop_dev_threads(struct scst_device *dev)
1403 if (list_empty(&dev->threads_list))
1406 scst_del_dev_threads(dev, -1);
1408 if (dev->p_cmd_lists == &dev->cmd_lists) {
1409 mutex_lock(&scst_suspend_mutex);
1410 list_del(&dev->cmd_lists.lists_list_entry);
1411 mutex_unlock(&scst_suspend_mutex);
1419 /* The activity supposed to be suspended and scst_mutex held */
1420 int scst_assign_dev_handler(struct scst_device *dev,
1421 struct scst_dev_type *handler)
1424 struct scst_tgt_dev *tgt_dev;
1425 LIST_HEAD(attached_tgt_devs);
1429 sBUG_ON(handler == NULL);
1431 if (dev->handler == handler)
1434 if (dev->handler == NULL)
1437 if (dev->handler->detach_tgt) {
1438 list_for_each_entry(tgt_dev, &dev->dev_tgt_dev_list,
1439 dev_tgt_dev_list_entry) {
1440 TRACE_DBG("Calling dev handler's detach_tgt(%p)",
1442 dev->handler->detach_tgt(tgt_dev);
1443 TRACE_DBG("%s", "Dev handler's detach_tgt() returned");
1447 if (dev->handler->detach) {
1448 TRACE_DBG("%s", "Calling dev handler's detach()");
1449 dev->handler->detach(dev);
1450 TRACE_DBG("%s", "Old handler's detach() returned");
1453 scst_stop_dev_threads(dev);
1455 scst_devt_dev_sysfs_put(dev);
1458 dev->handler = handler;
1460 if (handler == NULL)
1463 res = scst_create_devt_dev_sysfs(dev);
1467 res = scst_create_dev_threads(dev);
1469 goto out_remove_sysfs;
1471 if (handler->attach) {
1472 TRACE_DBG("Calling new dev handler's attach(%p)", dev);
1473 res = handler->attach(dev);
1474 TRACE_DBG("New dev handler's attach() returned %d", res);
1476 PRINT_ERROR("New device handler's %s attach() "
1477 "failed: %d", handler->name, res);
1482 if (handler->attach_tgt) {
1483 list_for_each_entry(tgt_dev, &dev->dev_tgt_dev_list,
1484 dev_tgt_dev_list_entry) {
1485 TRACE_DBG("Calling dev handler's attach_tgt(%p)",
1487 res = handler->attach_tgt(tgt_dev);
1488 TRACE_DBG("%s", "Dev handler's attach_tgt() returned");
1490 PRINT_ERROR("Device handler's %s attach_tgt() "
1491 "failed: %d", handler->name, res);
1492 goto out_err_detach_tgt;
1494 list_add_tail(&tgt_dev->extra_tgt_dev_list_entry,
1495 &attached_tgt_devs);
1500 TRACE_EXIT_RES(res);
1504 if (handler && handler->detach_tgt) {
1505 list_for_each_entry(tgt_dev, &attached_tgt_devs,
1506 extra_tgt_dev_list_entry) {
1507 TRACE_DBG("Calling handler's detach_tgt(%p)",
1509 handler->detach_tgt(tgt_dev);
1510 TRACE_DBG("%s", "Handler's detach_tgt() returned");
1513 if (handler && handler->detach) {
1514 TRACE_DBG("%s", "Calling handler's detach()");
1515 handler->detach(dev);
1516 TRACE_DBG("%s", "Handler's detach() returned");
1520 scst_stop_dev_threads(dev);
1523 scst_devt_dev_sysfs_put(dev);
1526 dev->handler = &scst_null_devtype;
1530 int scst_global_threads_count(void)
1535 * Just to lower the race window, when user can get just changed value
1537 mutex_lock(&scst_global_threads_mutex);
1538 i = scst_nr_global_threads;
1539 mutex_unlock(&scst_global_threads_mutex);
1543 static void scst_threads_info_init(void)
1545 mutex_init(&scst_global_threads_mutex);
1546 INIT_LIST_HEAD(&scst_global_threads_list);
1549 /* scst_global_threads_mutex supposed to be held */
1550 void __scst_del_global_threads(int num)
1552 struct scst_cmd_thread_t *ct, *tmp;
1559 list_for_each_entry_safe(ct, tmp, &scst_global_threads_list,
1560 thread_list_entry) {
1563 res = kthread_stop(ct->cmd_thread);
1565 TRACE_MGMT_DBG("kthread_stop() failed: %d", res);
1566 list_del(&ct->thread_list_entry);
1568 scst_nr_global_threads--;
1579 /* scst_global_threads_mutex supposed to be held */
1580 int __scst_add_global_threads(int num)
1583 static int scst_thread_num;
1587 for (i = 0; i < num; i++) {
1588 struct scst_cmd_thread_t *thr;
1590 thr = kmalloc(sizeof(*thr), GFP_KERNEL);
1593 PRINT_ERROR("fail to allocate thr %d", res);
1596 thr->cmd_thread = kthread_create(scst_cmd_thread,
1597 &scst_main_cmd_lists, "scsi_tgt%d",
1599 if (IS_ERR(thr->cmd_thread)) {
1600 res = PTR_ERR(thr->cmd_thread);
1601 PRINT_ERROR("kthread_create() failed: %d", res);
1606 list_add(&thr->thread_list_entry, &scst_global_threads_list);
1607 scst_nr_global_threads++;
1609 wake_up_process(thr->cmd_thread);
1614 TRACE_EXIT_RES(res);
1618 __scst_del_global_threads(i);
1622 int scst_add_global_threads(int num)
1628 mutex_lock(&scst_global_threads_mutex);
1629 res = __scst_add_global_threads(num);
1630 mutex_unlock(&scst_global_threads_mutex);
1632 TRACE_EXIT_RES(res);
1635 EXPORT_SYMBOL(scst_add_global_threads);
1637 void scst_del_global_threads(int num)
1641 mutex_lock(&scst_global_threads_mutex);
1642 __scst_del_global_threads(num);
1643 mutex_unlock(&scst_global_threads_mutex);
1648 EXPORT_SYMBOL(scst_del_global_threads);
1650 static void scst_stop_all_threads(void)
1654 mutex_lock(&scst_global_threads_mutex);
1655 __scst_del_global_threads(-1);
1656 if (scst_mgmt_cmd_thread)
1657 kthread_stop(scst_mgmt_cmd_thread);
1658 if (scst_mgmt_thread)
1659 kthread_stop(scst_mgmt_thread);
1660 if (scst_init_cmd_thread)
1661 kthread_stop(scst_init_cmd_thread);
1662 mutex_unlock(&scst_global_threads_mutex);
1668 static int scst_start_all_threads(int num)
1674 mutex_lock(&scst_global_threads_mutex);
1675 res = __scst_add_global_threads(num);
1679 scst_init_cmd_thread = kthread_run(scst_init_thread,
1680 NULL, "scsi_tgt_init");
1681 if (IS_ERR(scst_init_cmd_thread)) {
1682 res = PTR_ERR(scst_init_cmd_thread);
1683 PRINT_ERROR("kthread_create() for init cmd failed: %d", res);
1684 scst_init_cmd_thread = NULL;
1688 scst_mgmt_cmd_thread = kthread_run(scst_tm_thread,
1690 if (IS_ERR(scst_mgmt_cmd_thread)) {
1691 res = PTR_ERR(scst_mgmt_cmd_thread);
1692 PRINT_ERROR("kthread_create() for TM failed: %d", res);
1693 scst_mgmt_cmd_thread = NULL;
1697 scst_mgmt_thread = kthread_run(scst_global_mgmt_thread,
1698 NULL, "scsi_tgt_mgmt");
1699 if (IS_ERR(scst_mgmt_thread)) {
1700 res = PTR_ERR(scst_mgmt_thread);
1701 PRINT_ERROR("kthread_create() for mgmt failed: %d", res);
1702 scst_mgmt_thread = NULL;
1707 mutex_unlock(&scst_global_threads_mutex);
1708 TRACE_EXIT_RES(res);
1716 EXPORT_SYMBOL(scst_get);
1722 EXPORT_SYMBOL(scst_put);
1724 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26)
1725 static int scst_add(struct class_device *cdev, struct class_interface *intf)
1727 static int scst_add(struct device *cdev, struct class_interface *intf)
1730 struct scsi_device *scsidp;
1735 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26)
1736 scsidp = to_scsi_device(cdev->dev);
1738 scsidp = to_scsi_device(cdev->parent);
1741 if (strcmp(scsidp->host->hostt->name, SCST_LOCAL_NAME) != 0)
1742 res = scst_register_device(scsidp);
1748 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26)
1749 static void scst_remove(struct class_device *cdev, struct class_interface *intf)
1751 static void scst_remove(struct device *cdev, struct class_interface *intf)
1754 struct scsi_device *scsidp;
1758 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26)
1759 scsidp = to_scsi_device(cdev->dev);
1761 scsidp = to_scsi_device(cdev->parent);
1764 if (strcmp(scsidp->host->hostt->name, SCST_LOCAL_NAME) != 0)
1765 scst_unregister_device(scsidp);
1771 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26)
1772 static struct class_interface scst_interface = {
1774 .remove = scst_remove,
1777 static struct class_interface scst_interface = {
1778 .add_dev = scst_add,
1779 .remove_dev = scst_remove,
1783 static void __init scst_print_config(void)
1788 i = snprintf(buf, sizeof(buf), "Enabled features: ");
1791 #ifdef CONFIG_SCST_STRICT_SERIALIZING
1792 i += snprintf(&buf[i], sizeof(buf) - i, "Strict serializing");
1795 #ifdef CONFIG_SCST_EXTRACHECKS
1796 i += snprintf(&buf[i], sizeof(buf) - i, "%sEXTRACHECKS",
1797 (j == i) ? "" : ", ");
1800 #ifdef CONFIG_SCST_TRACING
1801 i += snprintf(&buf[i], sizeof(buf) - i, "%sTRACING",
1802 (j == i) ? "" : ", ");
1805 #ifdef CONFIG_SCST_DEBUG
1806 i += snprintf(&buf[i], sizeof(buf) - i, "%sDEBUG",
1807 (j == i) ? "" : ", ");
1810 #ifdef CONFIG_SCST_DEBUG_TM
1811 i += snprintf(&buf[i], sizeof(buf) - i, "%sDEBUG_TM",
1812 (j == i) ? "" : ", ");
1815 #ifdef CONFIG_SCST_DEBUG_RETRY
1816 i += snprintf(&buf[i], sizeof(buf) - i, "%sDEBUG_RETRY",
1817 (j == i) ? "" : ", ");
1820 #ifdef CONFIG_SCST_DEBUG_OOM
1821 i += snprintf(&buf[i], sizeof(buf) - i, "%sDEBUG_OOM",
1822 (j == i) ? "" : ", ");
1825 #ifdef CONFIG_SCST_DEBUG_SN
1826 i += snprintf(&buf[i], sizeof(buf) - i, "%sDEBUG_SN",
1827 (j == i) ? "" : ", ");
1830 #ifdef CONFIG_SCST_USE_EXPECTED_VALUES
1831 i += snprintf(&buf[i], sizeof(buf) - i, "%sUSE_EXPECTED_VALUES",
1832 (j == i) ? "" : ", ");
1835 #ifdef CONFIG_SCST_ALLOW_PASSTHROUGH_IO_SUBMIT_IN_SIRQ
1836 i += snprintf(&buf[i], sizeof(buf) - i,
1837 "%sALLOW_PASSTHROUGH_IO_SUBMIT_IN_SIRQ",
1838 (j == i) ? "" : ", ");
1841 #ifdef CONFIG_SCST_STRICT_SECURITY
1842 i += snprintf(&buf[i], sizeof(buf) - i, "%sSCST_STRICT_SECURITY",
1843 (j == i) ? "" : ", ");
1847 PRINT_INFO("%s", buf);
1850 static int __init init_scst(void)
1857 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18)
1859 struct scsi_request *req;
1860 BUILD_BUG_ON(SCST_SENSE_BUFFERSIZE !=
1861 sizeof(req->sr_sense_buffer));
1865 struct scsi_sense_hdr *shdr;
1866 BUILD_BUG_ON(SCST_SENSE_BUFFERSIZE < sizeof(*shdr));
1870 struct scst_tgt_dev *t;
1872 BUILD_BUG_ON(sizeof(t->curr_sn) != sizeof(t->expected_sn));
1873 BUILD_BUG_ON(sizeof(c->sn) != sizeof(t->expected_sn));
1876 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27)
1877 #if !defined(SCST_IO_CONTEXT)
1878 PRINT_WARNING("%s", "Patch io_context was not applied on "
1879 "your kernel. SCST will be working with not the best "
1883 PRINT_WARNING("%s", "There is no patch io_context for your kernel "
1884 "version. For performance reasons it is strongly recommended "
1885 "to upgrade your kernel to version >= 2.6.27.x.");
1888 mutex_init(&scst_mutex);
1889 INIT_LIST_HEAD(&scst_template_list);
1890 INIT_LIST_HEAD(&scst_dev_list);
1891 INIT_LIST_HEAD(&scst_dev_type_list);
1892 spin_lock_init(&scst_main_lock);
1893 INIT_LIST_HEAD(&scst_acg_list);
1894 spin_lock_init(&scst_init_lock);
1895 init_waitqueue_head(&scst_init_cmd_list_waitQ);
1896 INIT_LIST_HEAD(&scst_init_cmd_list);
1897 #if defined(CONFIG_SCST_DEBUG) || defined(CONFIG_SCST_TRACING)
1898 scst_trace_flag = SCST_DEFAULT_LOG_FLAGS;
1900 atomic_set(&scst_cmd_count, 0);
1901 spin_lock_init(&scst_mcmd_lock);
1902 INIT_LIST_HEAD(&scst_active_mgmt_cmd_list);
1903 INIT_LIST_HEAD(&scst_delayed_mgmt_cmd_list);
1904 init_waitqueue_head(&scst_mgmt_cmd_list_waitQ);
1905 init_waitqueue_head(&scst_mgmt_waitQ);
1906 spin_lock_init(&scst_mgmt_lock);
1907 INIT_LIST_HEAD(&scst_sess_init_list);
1908 INIT_LIST_HEAD(&scst_sess_shut_list);
1909 init_waitqueue_head(&scst_dev_cmd_waitQ);
1910 mutex_init(&scst_suspend_mutex);
1911 INIT_LIST_HEAD(&scst_cmd_lists_list);
1912 scst_virt_dev_last_id = 1;
1913 spin_lock_init(&scst_main_cmd_lists.cmd_list_lock);
1914 INIT_LIST_HEAD(&scst_main_cmd_lists.active_cmd_list);
1915 init_waitqueue_head(&scst_main_cmd_lists.cmd_list_waitQ);
1916 list_add_tail(&scst_main_cmd_lists.lists_list_entry,
1917 &scst_cmd_lists_list);
1919 res = scst_lib_init();
1923 scst_num_cpus = num_online_cpus();
1925 /* ToDo: register_cpu_notifier() */
1927 if (scst_threads == 0)
1928 scst_threads = scst_num_cpus;
1930 if (scst_threads < 1) {
1931 PRINT_ERROR("%s", "scst_threads can not be less than 1");
1932 scst_threads = scst_num_cpus;
1935 scst_threads_info_init();
1937 #define INIT_CACHEP(p, s, o) do { \
1938 p = KMEM_CACHE(s, SCST_SLAB_FLAGS); \
1939 TRACE_MEM("Slab create: %s at %p size %zd", #s, p, \
1940 sizeof(struct s)); \
1947 INIT_CACHEP(scst_mgmt_cachep, scst_mgmt_cmd, out_lib_exit);
1948 INIT_CACHEP(scst_mgmt_stub_cachep, scst_mgmt_cmd_stub,
1949 out_destroy_mgmt_cache);
1950 INIT_CACHEP(scst_ua_cachep, scst_tgt_dev_UA,
1951 out_destroy_mgmt_stub_cache);
1953 struct scst_sense { uint8_t s[SCST_SENSE_BUFFERSIZE]; };
1954 INIT_CACHEP(scst_sense_cachep, scst_sense,
1955 out_destroy_ua_cache);
1957 INIT_CACHEP(scst_aen_cachep, scst_aen, out_destroy_sense_cache);
1958 INIT_CACHEP(scst_cmd_cachep, scst_cmd, out_destroy_aen_cache);
1959 INIT_CACHEP(scst_sess_cachep, scst_session, out_destroy_cmd_cache);
1960 INIT_CACHEP(scst_tgtd_cachep, scst_tgt_dev, out_destroy_sess_cache);
1961 INIT_CACHEP(scst_acgd_cachep, scst_acg_dev, out_destroy_tgt_cache);
1963 scst_mgmt_mempool = mempool_create(64, mempool_alloc_slab,
1964 mempool_free_slab, scst_mgmt_cachep);
1965 if (scst_mgmt_mempool == NULL) {
1967 goto out_destroy_acg_cache;
1971 * All mgmt stubs, UAs and sense buffers are bursty and loosing them
1972 * may have fatal consequences, so let's have big pools for them.
1975 scst_mgmt_stub_mempool = mempool_create(1024, mempool_alloc_slab,
1976 mempool_free_slab, scst_mgmt_stub_cachep);
1977 if (scst_mgmt_stub_mempool == NULL) {
1979 goto out_destroy_mgmt_mempool;
1982 scst_ua_mempool = mempool_create(512, mempool_alloc_slab,
1983 mempool_free_slab, scst_ua_cachep);
1984 if (scst_ua_mempool == NULL) {
1986 goto out_destroy_mgmt_stub_mempool;
1989 scst_sense_mempool = mempool_create(1024, mempool_alloc_slab,
1990 mempool_free_slab, scst_sense_cachep);
1991 if (scst_sense_mempool == NULL) {
1993 goto out_destroy_ua_mempool;
1996 scst_aen_mempool = mempool_create(100, mempool_alloc_slab,
1997 mempool_free_slab, scst_aen_cachep);
1998 if (scst_aen_mempool == NULL) {
2000 goto out_destroy_sense_mempool;
2003 res = scst_sysfs_init();
2005 goto out_destroy_aen_mempool;
2007 if (scst_max_cmd_mem == 0) {
2010 #if BITS_PER_LONG == 32
2011 scst_max_cmd_mem = min(
2012 (((uint64_t)(si.totalram - si.totalhigh) << PAGE_SHIFT)
2013 >> 20) >> 2, (uint64_t)1 << 30);
2015 scst_max_cmd_mem = (((si.totalram - si.totalhigh) << PAGE_SHIFT)
2020 if (scst_max_dev_cmd_mem != 0) {
2021 if (scst_max_dev_cmd_mem > scst_max_cmd_mem) {
2022 PRINT_ERROR("scst_max_dev_cmd_mem (%d) > "
2023 "scst_max_cmd_mem (%d)",
2024 scst_max_dev_cmd_mem,
2026 scst_max_dev_cmd_mem = scst_max_cmd_mem;
2029 scst_max_dev_cmd_mem = scst_max_cmd_mem * 2 / 5;
2031 res = scst_sgv_pools_init(
2032 ((uint64_t)scst_max_cmd_mem << 10) >> (PAGE_SHIFT - 10), 0);
2034 goto out_sysfs_cleanup;
2036 #ifdef CONFIG_SCST_PROC
2037 scst_default_acg = scst_alloc_add_acg(SCST_DEFAULT_ACG_NAME);
2038 if (scst_default_acg == NULL) {
2040 goto out_destroy_sgv_pool;
2044 res = scsi_register_interface(&scst_interface);
2046 #ifdef CONFIG_SCST_PROC
2049 goto out_destroy_sgv_pool;
2052 for (i = 0; i < (int)ARRAY_SIZE(scst_tasklets); i++) {
2053 spin_lock_init(&scst_tasklets[i].tasklet_lock);
2054 INIT_LIST_HEAD(&scst_tasklets[i].tasklet_cmd_list);
2055 tasklet_init(&scst_tasklets[i].tasklet,
2056 (void *)scst_cmd_tasklet,
2057 (unsigned long)&scst_tasklets[i]);
2060 TRACE_DBG("%d CPUs found, starting %d threads", scst_num_cpus,
2063 res = scst_start_all_threads(scst_threads);
2065 goto out_thread_free;
2067 #ifdef CONFIG_SCST_PROC
2068 res = scst_proc_init_module();
2070 goto out_thread_free;
2073 PRINT_INFO("SCST version %s loaded successfully (max mem for "
2074 "commands %dMB, per device %dMB)", SCST_VERSION_STRING,
2075 scst_max_cmd_mem, scst_max_dev_cmd_mem);
2077 scst_print_config();
2080 TRACE_EXIT_RES(res);
2084 scst_stop_all_threads();
2086 scsi_unregister_interface(&scst_interface);
2088 #ifdef CONFIG_SCST_PROC
2090 scst_destroy_acg(scst_default_acg);
2093 out_destroy_sgv_pool:
2094 scst_sgv_pools_deinit();
2097 scst_sysfs_cleanup();
2099 out_destroy_aen_mempool:
2100 mempool_destroy(scst_aen_mempool);
2102 out_destroy_sense_mempool:
2103 mempool_destroy(scst_sense_mempool);
2105 out_destroy_ua_mempool:
2106 mempool_destroy(scst_ua_mempool);
2108 out_destroy_mgmt_stub_mempool:
2109 mempool_destroy(scst_mgmt_stub_mempool);
2111 out_destroy_mgmt_mempool:
2112 mempool_destroy(scst_mgmt_mempool);
2114 out_destroy_acg_cache:
2115 kmem_cache_destroy(scst_acgd_cachep);
2117 out_destroy_tgt_cache:
2118 kmem_cache_destroy(scst_tgtd_cachep);
2120 out_destroy_sess_cache:
2121 kmem_cache_destroy(scst_sess_cachep);
2123 out_destroy_cmd_cache:
2124 kmem_cache_destroy(scst_cmd_cachep);
2126 out_destroy_aen_cache:
2127 kmem_cache_destroy(scst_aen_cachep);
2129 out_destroy_sense_cache:
2130 kmem_cache_destroy(scst_sense_cachep);
2132 out_destroy_ua_cache:
2133 kmem_cache_destroy(scst_ua_cachep);
2135 out_destroy_mgmt_stub_cache:
2136 kmem_cache_destroy(scst_mgmt_stub_cachep);
2138 out_destroy_mgmt_cache:
2139 kmem_cache_destroy(scst_mgmt_cachep);
2146 static void __exit exit_scst(void)
2150 /* ToDo: unregister_cpu_notifier() */
2152 #ifdef CONFIG_SCST_PROC
2153 scst_proc_cleanup_module();
2155 scst_sysfs_cleanup();
2157 scst_stop_all_threads();
2159 scsi_unregister_interface(&scst_interface);
2160 #ifdef CONFIG_SCST_PROC
2161 scst_destroy_acg(scst_default_acg);
2164 scst_sgv_pools_deinit();
2166 #define DEINIT_CACHEP(p) do { \
2167 kmem_cache_destroy(p); \
2171 mempool_destroy(scst_mgmt_mempool);
2172 mempool_destroy(scst_mgmt_stub_mempool);
2173 mempool_destroy(scst_ua_mempool);
2174 mempool_destroy(scst_sense_mempool);
2175 mempool_destroy(scst_aen_mempool);
2177 DEINIT_CACHEP(scst_mgmt_cachep);
2178 DEINIT_CACHEP(scst_mgmt_stub_cachep);
2179 DEINIT_CACHEP(scst_ua_cachep);
2180 DEINIT_CACHEP(scst_sense_cachep);
2181 DEINIT_CACHEP(scst_aen_cachep);
2182 DEINIT_CACHEP(scst_cmd_cachep);
2183 DEINIT_CACHEP(scst_sess_cachep);
2184 DEINIT_CACHEP(scst_tgtd_cachep);
2185 DEINIT_CACHEP(scst_acgd_cachep);
2189 PRINT_INFO("%s", "SCST unloaded");
2195 module_init(init_scst);
2196 module_exit(exit_scst);
2198 MODULE_AUTHOR("Vladislav Bolkhovitin");
2199 MODULE_LICENSE("GPL");
2200 MODULE_DESCRIPTION("SCSI target core");
2201 MODULE_VERSION(SCST_VERSION_STRING);