4 * Copyright (C) 2004-2006 Vladislav Bolkhovitin <vst@vlnb.net>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation, version 2
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
18 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/kernel.h>
22 #include <linux/errno.h>
23 #include <linux/list.h>
24 #include <linux/spinlock.h>
25 #include <linux/slab.h>
26 #include <linux/sched.h>
27 #include <asm/unistd.h>
28 #include <asm/string.h>
30 #include "scst_debug.h"
32 #include "scst_priv.h"
35 #include "scst_debug.c"
37 #if defined(DEBUG) || defined(TRACING)
38 unsigned long trace_flag = SCST_DEFAULT_LOG_FLAGS;
42 * All targets, devices and dev_types management is done under
45 DECLARE_MUTEX(scst_mutex);
48 DECLARE_WAIT_QUEUE_HEAD(scst_dev_cmd_waitQ);
49 LIST_HEAD(scst_dev_wait_sess_list);
51 LIST_HEAD(scst_template_list);
52 LIST_HEAD(scst_dev_list);
53 LIST_HEAD(scst_dev_type_list);
55 kmem_cache_t *scst_mgmt_cachep;
56 mempool_t *scst_mgmt_mempool;
57 kmem_cache_t *scst_ua_cachep;
58 mempool_t *scst_ua_mempool;
59 kmem_cache_t *scst_tgtd_cachep;
60 kmem_cache_t *scst_sess_cachep;
61 kmem_cache_t *scst_acgd_cachep;
63 LIST_HEAD(scst_acg_list);
64 struct scst_acg *scst_default_acg;
66 kmem_cache_t *scst_cmd_cachep;
68 unsigned long scst_flags;
69 atomic_t scst_cmd_count = ATOMIC_INIT(0);
70 spinlock_t scst_list_lock = SPIN_LOCK_UNLOCKED;
71 LIST_HEAD(scst_active_cmd_list);
72 LIST_HEAD(scst_init_cmd_list);
73 LIST_HEAD(scst_cmd_list);
74 DECLARE_WAIT_QUEUE_HEAD(scst_list_waitQ);
76 spinlock_t scst_cmd_mem_lock = SPIN_LOCK_UNLOCKED;
77 unsigned long scst_cur_cmd_mem, scst_cur_max_cmd_mem;
79 struct tasklet_struct scst_tasklets[NR_CPUS];
81 struct scst_sgv_pools scst_sgv;
83 DECLARE_WORK(scst_cmd_mem_work, scst_cmd_mem_work_fn, 0);
85 unsigned long scst_max_cmd_mem;
87 LIST_HEAD(scst_mgmt_cmd_list);
88 LIST_HEAD(scst_active_mgmt_cmd_list);
89 LIST_HEAD(scst_delayed_mgmt_cmd_list);
90 DECLARE_WAIT_QUEUE_HEAD(scst_mgmt_cmd_list_waitQ);
92 DECLARE_WAIT_QUEUE_HEAD(scst_mgmt_waitQ);
93 spinlock_t scst_mgmt_lock = SPIN_LOCK_UNLOCKED;
94 LIST_HEAD(scst_sess_mgmt_list);
96 struct semaphore *scst_shutdown_mutex = NULL;
99 atomic_t scst_threads_count = ATOMIC_INIT(0);
100 int scst_shut_threads_count;
102 static int suspend_count;
104 int scst_virt_dev_last_id = 1; /* protected by scst_mutex */
107 * This buffer and lock are intended to avoid memory allocation, which
108 * could fail in improper places.
110 spinlock_t scst_temp_UA_lock = SPIN_LOCK_UNLOCKED;
111 uint8_t scst_temp_UA[SCSI_SENSE_BUFFERSIZE];
113 module_param_named(scst_threads, scst_threads, int, 0);
114 MODULE_PARM_DESC(scst_threads, "SCSI target threads count");
116 module_param_named(scst_max_cmd_mem, scst_max_cmd_mem, long, 0);
117 MODULE_PARM_DESC(scst_max_cmd_mem, "Maximum memory allowed to be consumed by "
118 "the SCST commands at any given time in Mb");
120 int scst_register_target_template(struct scst_tgt_template *vtt)
123 struct scst_tgt_template *t;
127 INIT_LIST_HEAD(&vtt->tgt_list);
130 PRINT_ERROR_PR("Target driver %s doesn't have a "
131 "detect() method.", vtt->name);
137 PRINT_ERROR_PR("Target driver %s doesn't have a "
138 "release() method.", vtt->name);
143 if (!vtt->xmit_response) {
144 PRINT_ERROR_PR("Target driver %s doesn't have a "
145 "xmit_response() method.", vtt->name);
150 if (!vtt->rdy_to_xfer) {
151 PRINT_ERROR_PR("Target driver %s doesn't have a "
152 "rdy_to_xfer() method.", vtt->name);
157 if (!vtt->on_free_cmd) {
158 PRINT_ERROR_PR("Target driver %s doesn't have a "
159 "on_free_cmd() method.", vtt->name);
164 if (!vtt->no_proc_entry) {
165 res = scst_build_proc_target_dir_entries(vtt);
171 if (down_interruptible(&scst_mutex) != 0)
173 list_for_each_entry(t, &scst_template_list, scst_template_list_entry) {
174 if (strcmp(t->name, vtt->name) == 0) {
175 PRINT_ERROR_PR("Target driver %s already registered",
181 /* That's OK to drop it. The race doesn't matter */
184 TRACE_DBG("%s", "Calling target driver's detect()");
185 res = vtt->detect(vtt);
186 TRACE_DBG("Target driver's detect() returned %d", res);
188 PRINT_ERROR_PR("%s", "The detect() routine failed");
194 list_add_tail(&vtt->scst_template_list_entry, &scst_template_list);
204 scst_cleanup_proc_target_dir_entries(vtt);
208 void scst_unregister_target_template(struct scst_tgt_template *vtt)
210 struct scst_tgt *tgt;
214 if (down_interruptible(&scst_mutex) != 0)
218 list_for_each_entry(tgt, &vtt->tgt_list, tgt_list_entry) {
220 scst_unregister(tgt);
224 list_del(&vtt->scst_template_list_entry);
227 scst_cleanup_proc_target_dir_entries(vtt);
234 struct scst_tgt *scst_register(struct scst_tgt_template *vtt)
236 struct scst_tgt *tgt;
240 tgt = kzalloc(sizeof(*tgt), GFP_KERNEL);
241 TRACE_MEM("kzalloc(GFP_KERNEL) for tgt (%zd): %p",
244 TRACE(TRACE_OUT_OF_MEM, "%s", "kzalloc() failed");
248 INIT_LIST_HEAD(&tgt->sess_list);
249 init_waitqueue_head(&tgt->unreg_waitQ);
251 tgt->sg_tablesize = vtt->sg_tablesize;
252 spin_lock_init(&tgt->tgt_lock);
253 INIT_LIST_HEAD(&tgt->retry_cmd_list);
254 atomic_set(&tgt->finished_cmds, 0);
255 init_timer(&tgt->retry_timer);
256 tgt->retry_timer.data = (unsigned long)tgt;
257 tgt->retry_timer.function = scst_tgt_retry_timer_fn;
261 if (scst_build_proc_target_entries(tgt) < 0) {
266 list_add_tail(&tgt->tgt_list_entry, &vtt->tgt_list);
275 TRACE_MEM("kfree() for tgt %p", tgt);
280 static inline int test_sess_list(struct scst_tgt *tgt)
284 res = list_empty(&tgt->sess_list);
289 void scst_unregister(struct scst_tgt *tgt)
291 struct scst_session *sess;
295 TRACE_DBG("%s", "Calling target driver's release()");
296 tgt->tgtt->release(tgt);
297 TRACE_DBG("%s", "Target driver's release() returned");
300 list_for_each_entry(sess, &tgt->sess_list, sess_list_entry) {
301 BUG_ON(!sess->shutting_down);
305 TRACE_DBG("%s", "Waiting for sessions shutdown");
306 wait_event(tgt->unreg_waitQ, test_sess_list(tgt));
307 TRACE_DBG("%s", "wait_event() returned");
310 list_del(&tgt->tgt_list_entry);
313 scst_cleanup_proc_target_entries(tgt);
315 del_timer_sync(&tgt->retry_timer);
317 TRACE_MEM("kfree for tgt: %p", tgt);
324 /* scst_mutex supposed to be held */
325 void __scst_suspend_activity(void)
330 if (suspend_count > 1)
333 set_bit(SCST_FLAG_SUSPENDED, &scst_flags);
334 smp_mb__after_set_bit();
336 TRACE_DBG("Waiting for all %d active commands to complete",
337 atomic_read(&scst_cmd_count));
338 wait_event(scst_dev_cmd_waitQ, atomic_read(&scst_cmd_count) == 0);
339 TRACE_DBG("%s", "wait_event() returned");
346 void scst_suspend_activity(void)
349 __scst_suspend_activity();
352 /* scst_mutex supposed to be held */
353 void __scst_resume_activity(void)
355 struct scst_session *sess, *tsess;
360 if (suspend_count > 0)
363 clear_bit(SCST_FLAG_SUSPENDED, &scst_flags);
364 smp_mb__after_clear_bit();
366 spin_lock_irq(&scst_list_lock);
367 list_for_each_entry_safe(sess, tsess, &scst_dev_wait_sess_list,
368 dev_wait_sess_list_entry)
371 list_del(&sess->dev_wait_sess_list_entry);
373 spin_unlock_irq(&scst_list_lock);
375 wake_up_all(&scst_list_waitQ);
376 wake_up_all(&scst_mgmt_cmd_list_waitQ);
383 void scst_resume_activity(void)
385 __scst_resume_activity();
389 /* Called under scst_mutex */
390 static int scst_register_device(struct scsi_device *scsidp)
393 struct scst_device *dev;
394 struct scst_dev_type *dt;
398 dev = scst_alloc_device(GFP_KERNEL);
404 dev->rq_disk = alloc_disk(1);
405 if (dev->rq_disk == NULL) {
407 scst_free_device(dev);
410 dev->rq_disk->major = SCST_MAJOR;
412 dev->scsi_dev = scsidp;
414 list_add_tail(&dev->dev_list_entry, &scst_dev_list);
416 list_for_each_entry(dt, &scst_dev_type_list, dev_type_list_entry) {
417 if (dt->type == scsidp->type) {
418 res = scst_assign_dev_handler(dev, dt);
427 PRINT_INFO_PR("Attached SCSI target mid-level at "
428 "scsi%d, channel %d, id %d, lun %d, type %d",
429 scsidp->host->host_no, scsidp->channel, scsidp->id,
430 scsidp->lun, scsidp->type);
433 PRINT_ERROR_PR("Failed to attach SCSI target mid-level "
434 "at scsi%d, channel %d, id %d, lun %d, type %d",
435 scsidp->host->host_no, scsidp->channel, scsidp->id,
436 scsidp->lun, scsidp->type);
443 put_disk(dev->rq_disk);
445 list_del(&dev->dev_list_entry);
446 scst_assign_dev_handler(dev, NULL);
450 /* Called under scst_mutex */
451 static void scst_unregister_device(struct scsi_device *scsidp)
453 struct scst_device *d, *dev = NULL;
454 struct scst_acg_dev *acg_dev, *aa;
458 __scst_suspend_activity();
460 list_for_each_entry(d, &scst_dev_list, dev_list_entry) {
461 if (d->scsi_dev == scsidp) {
463 TRACE_DBG("Target device %p found", dev);
469 PRINT_ERROR_PR("%s", "Target device not found");
473 list_del(&dev->dev_list_entry);
475 list_for_each_entry_safe(acg_dev, aa, &dev->dev_acg_dev_list,
476 dev_acg_dev_list_entry)
478 scst_acg_remove_dev(acg_dev->acg, dev);
481 scst_assign_dev_handler(dev, NULL);
483 put_disk(dev->rq_disk);
484 scst_free_device(dev);
487 __scst_resume_activity();
489 PRINT_INFO_PR("Detached SCSI target mid-level from scsi%d, channel %d, "
490 "id %d, lun %d, type %d", scsidp->host->host_no, scsidp->channel,
491 scsidp->id, scsidp->lun, scsidp->type);
497 int scst_register_virtual_device(struct scst_dev_type *dev_handler,
498 const char *dev_name)
500 int res = -EINVAL, rc;
501 struct scst_device *dev = NULL;
505 if (dev_handler == NULL) {
506 PRINT_ERROR_PR("%s: valid device handler must be supplied",
512 if (dev_name == NULL) {
513 PRINT_ERROR_PR("%s: device name must be non-NULL", __FUNCTION__);
518 dev = scst_alloc_device(GFP_KERNEL);
524 dev->scsi_dev = NULL;
525 dev->virt_name = dev_name;
527 if (down_interruptible(&scst_mutex) != 0) {
532 dev->virt_id = scst_virt_dev_last_id++;
534 list_add_tail(&dev->dev_list_entry, &scst_dev_list);
538 rc = scst_assign_dev_handler(dev, dev_handler);
548 PRINT_INFO_PR("Attached SCSI target mid-level to virtual "
549 "device %s (id %d)", dev_name, dev->virt_id);
552 PRINT_INFO_PR("Failed to attach SCSI target mid-level to "
553 "virtual device %s", dev_name);
560 list_del(&dev->dev_list_entry);
564 scst_free_device(dev);
568 void scst_unregister_virtual_device(int id)
570 struct scst_device *d, *dev = NULL;
571 struct scst_acg_dev *acg_dev, *aa;
575 if (down_interruptible(&scst_mutex) != 0)
578 __scst_suspend_activity();
580 list_for_each_entry(d, &scst_dev_list, dev_list_entry) {
581 if (d->virt_id == id) {
583 TRACE_DBG("Target device %p found", dev);
589 PRINT_ERROR_PR("%s", "Target device not found");
593 list_del(&dev->dev_list_entry);
595 list_for_each_entry_safe(acg_dev, aa, &dev->dev_acg_dev_list,
596 dev_acg_dev_list_entry)
598 scst_acg_remove_dev(acg_dev->acg, dev);
601 scst_assign_dev_handler(dev, NULL);
603 PRINT_INFO_PR("Detached SCSI target mid-level from virtual device %s "
604 "(id %d)", dev->virt_name, dev->virt_id);
606 scst_free_device(dev);
609 __scst_resume_activity();
618 int scst_register_dev_driver(struct scst_dev_type *dev_type)
620 struct scst_dev_type *dt;
621 struct scst_device *dev;
627 if (dev_type->parse == NULL) {
628 PRINT_ERROR_PR("scst dev_type driver %s doesn't have a "
629 "parse() method.", dev_type->name);
635 if (dev_type->exec == NULL) {
636 PRINT_ERROR_PR("Pass-through dev handlers (handler %s) not "
637 "supported. Recompile SCST with undefined FILEIO_ONLY",
644 if (down_interruptible(&scst_mutex) != 0) {
650 list_for_each_entry(dt, &scst_dev_type_list, dev_type_list_entry) {
651 if (dt->type == dev_type->type) {
652 TRACE_DBG("Device type handler for type %d "
653 "already exist", dt->type);
659 res = scst_build_proc_dev_handler_dir_entries(dev_type);
664 list_add_tail(&dev_type->dev_type_list_entry, &scst_dev_type_list);
669 __scst_suspend_activity();
670 list_for_each_entry(dev, &scst_dev_list, dev_list_entry) {
671 if (dev->scsi_dev == NULL)
673 if (dev->scsi_dev->type == dev_type->type)
674 scst_assign_dev_handler(dev, dev_type);
676 __scst_resume_activity();
682 PRINT_INFO_PR("Device handler %s for type %d loaded "
683 "successfully", dev_type->name, dev_type->type);
691 void scst_unregister_dev_driver(struct scst_dev_type *dev_type)
693 struct scst_device *dev;
697 if (down_interruptible(&scst_mutex) != 0)
700 __scst_suspend_activity();
701 list_for_each_entry(dev, &scst_dev_list, dev_list_entry) {
702 if (dev->handler == dev_type) {
703 scst_assign_dev_handler(dev, NULL);
704 TRACE_DBG("Dev handler removed from device %p", dev);
707 __scst_resume_activity();
709 list_del(&dev_type->dev_type_list_entry);
713 scst_cleanup_proc_dev_handler_dir_entries(dev_type);
715 PRINT_INFO_PR("Device handler %s for type %d unloaded",
716 dev_type->name, dev_type->type);
723 int scst_register_virtual_dev_driver(struct scst_dev_type *dev_type)
729 if (dev_type->parse == NULL) {
730 PRINT_ERROR_PR("scst dev_type driver %s doesn't have a "
731 "parse() method.", dev_type->name);
736 res = scst_build_proc_dev_handler_dir_entries(dev_type);
741 PRINT_INFO_PR("Device handler %s for type %d loaded "
742 "successfully", dev_type->name, dev_type->type);
749 void scst_unregister_virtual_dev_driver(struct scst_dev_type *dev_type)
753 scst_cleanup_proc_dev_handler_dir_entries(dev_type);
755 PRINT_INFO_PR("Device handler %s for type %d unloaded",
756 dev_type->name, dev_type->type);
762 /* scst_mutex supposed to be held */
763 int scst_assign_dev_handler(struct scst_device *dev,
764 struct scst_dev_type *handler)
767 struct scst_tgt_dev *tgt_dev;
768 LIST_HEAD(attached_tgt_devs);
772 if (dev->handler == handler)
775 __scst_suspend_activity();
777 if (dev->handler && dev->handler->detach_tgt) {
778 list_for_each_entry(tgt_dev, &dev->dev_tgt_dev_list,
779 dev_tgt_dev_list_entry)
781 TRACE_DBG("Calling dev handler's detach_tgt(%p)",
783 dev->handler->detach_tgt(tgt_dev);
784 TRACE_DBG("%s", "Dev handler's detach_tgt() returned");
788 if (dev->handler && dev->handler->detach) {
789 TRACE_DBG("%s", "Calling dev handler's detach()");
790 dev->handler->detach(dev);
791 TRACE_DBG("%s", "Old handler's detach() returned");
794 dev->handler = handler;
796 if (handler && handler->attach) {
797 TRACE_DBG("Calling new dev handler's attach(%p)", dev);
798 res = handler->attach(dev);
799 TRACE_DBG("New dev handler's attach() returned %d", res);
801 PRINT_ERROR_PR("New device handler's %s attach() "
802 "failed: %d", handler->name, res);
807 if (handler && handler->attach_tgt) {
808 list_for_each_entry(tgt_dev, &dev->dev_tgt_dev_list,
809 dev_tgt_dev_list_entry)
811 TRACE_DBG("Calling dev handler's attach_tgt(%p)",
813 res = handler->attach_tgt(tgt_dev);
814 TRACE_DBG("%s", "Dev handler's attach_tgt() returned");
816 PRINT_ERROR_PR("Device handler's %s attach_tgt() "
817 "failed: %d", handler->name, res);
818 goto out_err_detach_tgt;
820 list_add_tail(&tgt_dev->extra_tgt_dev_list_entry,
828 __scst_resume_activity();
835 if (handler && handler->detach_tgt) {
836 list_for_each_entry(tgt_dev, &attached_tgt_devs,
837 extra_tgt_dev_list_entry)
839 TRACE_DBG("Calling handler's detach_tgt(%p)",
841 handler->detach_tgt(tgt_dev);
842 TRACE_DBG("%s", "Handler's detach_tgt() returned");
845 if (handler && handler->detach) {
846 TRACE_DBG("%s", "Calling handler's detach()");
847 handler->detach(dev);
848 TRACE_DBG("%s", "Hhandler's detach() returned");
853 int scst_add_threads(int num)
859 for (i = 0; i < num; i++) {
860 res = kernel_thread(scst_cmd_thread, 0, SCST_THREAD_FLAGS);
862 PRINT_ERROR_PR("kernel_thread() failed: %d", res);
865 atomic_inc(&scst_threads_count);
876 scst_del_threads(i-1);
880 void scst_del_threads(int num)
884 spin_lock_irq(&scst_list_lock);
885 scst_shut_threads_count += num;
886 spin_unlock_irq(&scst_list_lock);
888 wake_up_nr(&scst_list_waitQ, num);
896 scst_inc_cmd_count();
901 scst_dec_cmd_count();
904 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,15)
905 static int scst_add(struct class_device *cdev)
907 static int scst_add(struct class_device *cdev, struct class_interface *intf)
910 struct scsi_device *scsidp;
915 scsidp = to_scsi_device(cdev->dev);
918 res = scst_register_device(scsidp);
925 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,15)
926 static void scst_remove(struct class_device *cdev)
928 static void scst_remove(struct class_device *cdev, struct class_interface *intf)
931 struct scsi_device *scsidp;
935 scsidp = to_scsi_device(cdev->dev);
938 scst_unregister_device(scsidp);
945 static struct class_interface scst_interface = {
947 .remove = scst_remove,
950 static inline int get_cpus_count(void)
953 return cpus_weight(cpu_online_map);
959 static int __init init_scst(void)
962 struct scst_cmd *cmd;
966 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18)
968 struct scsi_request *req;
969 BUILD_BUG_ON(sizeof(cmd->sense_buffer) !=
970 sizeof(req->sr_sense_buffer));
974 struct scsi_sense_hdr *shdr;
975 BUILD_BUG_ON((sizeof(cmd->sense_buffer) < sizeof(*shdr)) &&
976 (sizeof(cmd->sense_buffer) >= SCSI_SENSE_BUFFERSIZE));
980 scst_num_cpus = get_cpus_count();
982 /* ToDo: register_cpu_notifier() */
984 if (scst_threads == 0)
985 scst_threads = scst_num_cpus;
987 if (scst_threads < scst_num_cpus) {
988 PRINT_ERROR_PR("%s", "scst_threads can not be less than "
994 #define INIT_CACHEP(p, s, t, o) do { \
995 p = kmem_cache_create(s, sizeof(struct t), 0, \
996 SCST_SLAB_FLAGS, NULL, NULL); \
997 TRACE_MEM("Slab create: %s at %p size %zd", s, p, \
999 if (p == NULL) { res = -ENOMEM; goto o; } \
1002 INIT_CACHEP(scst_mgmt_cachep, SCST_MGMT_CMD_CACHE_STRING,
1003 scst_mgmt_cmd, out);
1004 INIT_CACHEP(scst_ua_cachep, SCST_UA_CACHE_STRING,
1005 scst_tgt_dev_UA, out_destroy_mgmt_cache);
1006 INIT_CACHEP(scst_cmd_cachep, SCST_CMD_CACHE_STRING,
1007 scst_cmd, out_destroy_ua_cache);
1008 INIT_CACHEP(scst_sess_cachep, SCST_SESSION_CACHE_STRING,
1009 scst_session, out_destroy_cmd_cache);
1010 INIT_CACHEP(scst_tgtd_cachep, SCST_TGT_DEV_CACHE_STRING,
1011 scst_tgt_dev, out_destroy_sess_cache);
1012 INIT_CACHEP(scst_acgd_cachep, SCST_ACG_DEV_CACHE_STRING,
1013 scst_acg_dev, out_destroy_tgt_cache);
1015 scst_mgmt_mempool = mempool_create(10, mempool_alloc_slab,
1016 mempool_free_slab, scst_mgmt_cachep);
1017 if (scst_mgmt_mempool == NULL) {
1019 goto out_destroy_acg_cache;
1022 scst_ua_mempool = mempool_create(25, mempool_alloc_slab,
1023 mempool_free_slab, scst_ua_cachep);
1024 if (scst_ua_mempool == NULL) {
1026 goto out_destroy_mgmt_mempool;
1029 res = scst_sgv_pools_init(&scst_sgv);
1031 goto out_destroy_ua_mempool;
1033 scst_default_acg = scst_alloc_add_acg(SCST_DEFAULT_ACG_NAME);
1034 if (scst_default_acg == NULL) {
1036 goto out_destroy_sgv_pool;
1039 res = scsi_register_interface(&scst_interface);
1043 scst_scsi_op_list_init();
1045 res = scst_proc_init_module();
1047 goto out_unreg_interface;
1049 TRACE_DBG("%d CPUs found, starting %d threads", scst_num_cpus,
1052 for (i = 0; i < scst_threads; i++) {
1053 res = kernel_thread(scst_cmd_thread, NULL, SCST_THREAD_FLAGS);
1055 PRINT_ERROR_PR("kernel_thread() failed: %d", res);
1056 goto out_thread_free;
1058 atomic_inc(&scst_threads_count);
1061 for (i = 0; i < sizeof(scst_tasklets) / sizeof(scst_tasklets[0]); i++)
1062 tasklet_init(&scst_tasklets[i], (void *)scst_cmd_tasklet, 0);
1064 res = kernel_thread(scst_mgmt_cmd_thread, NULL, SCST_THREAD_FLAGS);
1066 PRINT_ERROR_PR("kernel_thread() for mcmd failed: %d", res);
1067 goto out_thread_free;
1069 atomic_inc(&scst_threads_count);
1071 res = kernel_thread(scst_mgmt_thread, NULL, SCST_THREAD_FLAGS);
1073 PRINT_ERROR_PR("kernel_thread() for mgmt failed: %d", res);
1074 goto out_thread_free;
1076 atomic_inc(&scst_threads_count);
1078 if (scst_max_cmd_mem == 0) {
1081 #if BITS_PER_LONG == 32
1082 scst_max_cmd_mem = min(((uint64_t)si.totalram << PAGE_SHIFT) >> 2,
1085 scst_max_cmd_mem = (si.totalram << PAGE_SHIFT) >> 2;
1088 scst_max_cmd_mem <<= 20;
1090 scst_cur_max_cmd_mem = scst_max_cmd_mem;
1092 PRINT_INFO_PR("SCST version %s loaded successfully (max mem for "
1093 "commands %ld Mb)", SCST_VERSION_STRING, scst_max_cmd_mem >> 20);
1096 TRACE_EXIT_RES(res);
1100 if (atomic_read(&scst_threads_count)) {
1101 DECLARE_MUTEX_LOCKED(shm);
1102 scst_shutdown_mutex = &shm;
1104 set_bit(SCST_FLAG_SHUTDOWN, &scst_flags);
1106 wake_up_all(&scst_list_waitQ);
1107 wake_up_all(&scst_mgmt_cmd_list_waitQ);
1108 wake_up_all(&scst_mgmt_waitQ);
1110 TRACE_DBG("Waiting for %d threads to complete",
1111 atomic_read(&scst_threads_count));
1115 scst_proc_cleanup_module();
1117 out_unreg_interface:
1118 scsi_unregister_interface(&scst_interface);
1121 scst_destroy_acg(scst_default_acg);
1123 out_destroy_sgv_pool:
1124 scst_sgv_pools_deinit(&scst_sgv);
1126 out_destroy_ua_mempool:
1127 mempool_destroy(scst_ua_mempool);
1129 out_destroy_mgmt_mempool:
1130 mempool_destroy(scst_mgmt_mempool);
1132 out_destroy_acg_cache:
1133 kmem_cache_destroy(scst_acgd_cachep);
1135 out_destroy_tgt_cache:
1136 kmem_cache_destroy(scst_tgtd_cachep);
1138 out_destroy_sess_cache:
1139 kmem_cache_destroy(scst_sess_cachep);
1141 out_destroy_cmd_cache:
1142 kmem_cache_destroy(scst_cmd_cachep);
1144 out_destroy_ua_cache:
1145 kmem_cache_destroy(scst_ua_cachep);
1147 out_destroy_mgmt_cache:
1148 kmem_cache_destroy(scst_mgmt_cachep);
1152 static void __exit exit_scst(void)
1154 #ifdef CONFIG_LOCKDEP
1155 static /* To hide the lockdep's warning about non-static key */
1157 DECLARE_MUTEX_LOCKED(shm);
1161 /* ToDo: unregister_cpu_notifier() */
1163 scst_shutdown_mutex = &shm;
1165 set_bit(SCST_FLAG_SHUTDOWN, &scst_flags);
1167 wake_up_all(&scst_list_waitQ);
1168 wake_up_all(&scst_mgmt_cmd_list_waitQ);
1169 wake_up_all(&scst_mgmt_waitQ);
1171 if (atomic_read(&scst_threads_count)) {
1172 TRACE_DBG("Waiting for %d threads to complete",
1173 atomic_read(&scst_threads_count));
1177 * Wait for one sec. to allow the thread(s) actually exit,
1178 * otherwise we can get Oops. Any better way?
1181 unsigned long t = jiffies;
1182 TRACE_DBG("%s", "Waiting 1 sec...");
1183 while((jiffies - t) < HZ)
1188 if (test_bit(SCST_FLAG_CMD_MEM_WORK_SCHEDULED, &scst_flags)) {
1189 cancel_delayed_work(&scst_cmd_mem_work);
1190 flush_scheduled_work();
1193 scst_proc_cleanup_module();
1194 scsi_unregister_interface(&scst_interface);
1195 scst_destroy_acg(scst_default_acg);
1197 scst_sgv_pools_deinit(&scst_sgv);
1199 #define DEINIT_CACHEP(p, s) do { \
1200 if (kmem_cache_destroy(p)) { \
1201 PRINT_INFO_PR("kmem_cache_destroy of %s returned an "\
1207 mempool_destroy(scst_mgmt_mempool);
1208 mempool_destroy(scst_ua_mempool);
1210 DEINIT_CACHEP(scst_mgmt_cachep, SCST_MGMT_CMD_CACHE_STRING);
1211 DEINIT_CACHEP(scst_ua_cachep, SCST_UA_CACHE_STRING);
1212 DEINIT_CACHEP(scst_cmd_cachep, SCST_CMD_CACHE_STRING);
1213 DEINIT_CACHEP(scst_sess_cachep, SCST_SESSION_CACHE_STRING);
1214 DEINIT_CACHEP(scst_tgtd_cachep, SCST_TGT_DEV_CACHE_STRING);
1215 DEINIT_CACHEP(scst_acgd_cachep, SCST_ACG_DEV_CACHE_STRING);
1217 PRINT_INFO_PR("%s", "SCST unloaded");
1224 * Device Handler Side (i.e. scst_fileio)
1226 EXPORT_SYMBOL(scst_register_dev_driver);
1227 EXPORT_SYMBOL(scst_unregister_dev_driver);
1228 EXPORT_SYMBOL(scst_register);
1229 EXPORT_SYMBOL(scst_unregister);
1231 EXPORT_SYMBOL(scst_register_virtual_device);
1232 EXPORT_SYMBOL(scst_unregister_virtual_device);
1233 EXPORT_SYMBOL(scst_register_virtual_dev_driver);
1234 EXPORT_SYMBOL(scst_unregister_virtual_dev_driver);
1236 EXPORT_SYMBOL(scst_set_busy);
1237 EXPORT_SYMBOL(scst_set_cmd_error_status);
1238 EXPORT_SYMBOL(scst_set_cmd_error);
1239 EXPORT_SYMBOL(scst_set_resp_data_len);
1242 * Target Driver Side (i.e. HBA)
1244 EXPORT_SYMBOL(scst_register_session);
1245 EXPORT_SYMBOL(scst_unregister_session);
1247 EXPORT_SYMBOL(scst_register_target_template);
1248 EXPORT_SYMBOL(scst_unregister_target_template);
1250 EXPORT_SYMBOL(scst_cmd_init_done);
1251 EXPORT_SYMBOL(scst_tgt_cmd_done);
1252 EXPORT_SYMBOL(scst_rx_cmd);
1253 EXPORT_SYMBOL(scst_rx_data);
1254 EXPORT_SYMBOL(scst_rx_mgmt_fn_tag);
1255 EXPORT_SYMBOL(scst_rx_mgmt_fn_lun);
1257 EXPORT_SYMBOL(scst_find_cmd);
1258 EXPORT_SYMBOL(scst_find_cmd_by_tag);
1263 EXPORT_SYMBOL(scst_suspend_activity);
1264 EXPORT_SYMBOL(scst_resume_activity);
1266 EXPORT_SYMBOL(scst_add_threads);
1267 EXPORT_SYMBOL(scst_del_threads);
1269 #if defined(DEBUG) || defined(TRACING)
1270 EXPORT_SYMBOL(scst_proc_log_entry_read);
1271 EXPORT_SYMBOL(scst_proc_log_entry_write);
1274 EXPORT_SYMBOL(__scst_get_buf);
1275 EXPORT_SYMBOL(scst_check_mem);
1276 EXPORT_SYMBOL(scst_get);
1277 EXPORT_SYMBOL(scst_put);
1282 EXPORT_SYMBOL(scst_get_cdb_info);
1283 EXPORT_SYMBOL(scst_cmd_get_tgt_specific_lock);
1284 EXPORT_SYMBOL(scst_cmd_set_tgt_specific_lock);
1287 EXPORT_SYMBOL(scst_random);
1290 module_init(init_scst);
1291 module_exit(exit_scst);
1293 MODULE_AUTHOR("Vladislav Bolkhovitin & Leonid Stoljar");
1294 MODULE_LICENSE("GPL");
1295 MODULE_DESCRIPTION("SCSI target core");
1296 MODULE_VERSION(SCST_VERSION_STRING);