- Use vfs_readv/vfs_writev instead of direct calls to VFS functions
[mirror/scst/.git] / scst / src / scst_main.c
1 /*
2  *  scst_main.c
3  *
4  *  Copyright (C) 2004 - 2009 Vladislav Bolkhovitin <vst@vlnb.net>
5  *  Copyright (C) 2004 - 2005 Leonid Stoljar
6  *  Copyright (C) 2007 - 2009 ID7 Ltd.
7  *
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
11  *  of the License.
12  *
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.
17  */
18
19 #include <linux/module.h>
20
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>
31
32 #include "scst.h"
33 #include "scst_priv.h"
34 #include "scst_mem.h"
35
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\
40  details."
41 #endif
42
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) */
55
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."
60 #endif
61 #else
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."
65 #endif
66
67 /**
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.
71  **/
72
73 /*
74  * All targets, devices and dev_types management is done under this mutex.
75  *
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().
80  */
81 struct mutex scst_mutex;
82
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;
87
88 spinlock_t scst_main_lock;
89
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;
103
104 struct list_head scst_acg_list;
105 #ifdef CONFIG_SCST_PROC
106 struct scst_acg *scst_default_acg;
107 #endif
108
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;
113
114 struct kmem_cache *scst_cmd_cachep;
115
116 #if defined(CONFIG_SCST_DEBUG) || defined(CONFIG_SCST_TRACING)
117 unsigned long scst_trace_flag;
118 #endif
119
120 unsigned long scst_flags;
121 atomic_t scst_cmd_count;
122
123 struct scst_cmd_lists scst_main_cmd_lists;
124
125 struct scst_tasklet scst_tasklets[NR_CPUS];
126
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;
131
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;
136
137 wait_queue_head_t scst_dev_cmd_waitQ;
138
139 static struct mutex scst_suspend_mutex;
140 /* protected by scst_suspend_mutex */
141 static struct list_head scst_cmd_lists_list;
142
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;
150
151 static int suspend_count;
152
153 static int scst_virt_dev_last_id; /* protected by scst_mutex */
154
155 static unsigned int scst_max_cmd_mem;
156 unsigned int scst_max_dev_cmd_mem;
157
158 module_param_named(scst_threads, scst_threads, int, 0);
159 MODULE_PARM_DESC(scst_threads, "SCSI target threads count");
160
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");
164
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");
168
169 struct scst_dev_type scst_null_devtype = {
170         .name = "none",
171 };
172
173 static void __scst_resume_activity(void);
174
175 int __scst_register_target_template(struct scst_tgt_template *vtt,
176         const char *version)
177 {
178         int res = 0;
179         struct scst_tgt_template *t;
180         static DEFINE_MUTEX(m);
181
182         TRACE_ENTRY();
183
184         INIT_LIST_HEAD(&vtt->tgt_list);
185
186         if (strcmp(version, SCST_INTERFACE_VERSION) != 0) {
187                 PRINT_ERROR("Incorrect version of target %s", vtt->name);
188                 res = -EINVAL;
189                 goto out_err;
190         }
191
192         if (!vtt->detect) {
193                 PRINT_ERROR("Target driver %s must have "
194                         "detect() method.", vtt->name);
195                 res = -EINVAL;
196                 goto out_err;
197         }
198
199         if (!vtt->release) {
200                 PRINT_ERROR("Target driver %s must have "
201                         "release() method.", vtt->name);
202                 res = -EINVAL;
203                 goto out_err;
204         }
205
206         if (!vtt->xmit_response) {
207                 PRINT_ERROR("Target driver %s must have "
208                         "xmit_response() method.", vtt->name);
209                 res = -EINVAL;
210                 goto out_err;
211         }
212
213         if (vtt->threads_num < 0) {
214                 PRINT_ERROR("Wrong threads_num value %d for "
215                         "target \"%s\"", vtt->threads_num,
216                         vtt->name);
217                 res = -EINVAL;
218                 goto out_err;
219         }
220
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);
228         }
229 #else
230         if (!vtt->no_proc_entry) {
231                 res = scst_build_proc_target_dir_entries(vtt);
232                 if (res < 0)
233                         goto out_err;
234         }
235 #endif
236
237         res = scst_create_tgtt_sysfs(vtt);
238         if (res)
239                 goto out_sysfs_err;
240
241         if (vtt->rdy_to_xfer == NULL)
242                 vtt->rdy_to_xfer_atomic = 1;
243
244         if (mutex_lock_interruptible(&m) != 0)
245                 goto out_sysfs_err;
246
247         if (mutex_lock_interruptible(&scst_mutex) != 0)
248                 goto out_m_err;
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",
252                                 vtt->name);
253                         mutex_unlock(&scst_mutex);
254                         goto out_m_err;
255                 }
256         }
257         mutex_unlock(&scst_mutex);
258
259         TRACE_DBG("%s", "Calling target driver's detect()");
260         res = vtt->detect(vtt);
261         TRACE_DBG("Target driver's detect() returned %d", res);
262         if (res < 0) {
263                 PRINT_ERROR("%s", "The detect() routine failed");
264                 res = -EINVAL;
265                 goto out_m_err;
266         }
267
268         mutex_lock(&scst_mutex);
269         list_add_tail(&vtt->scst_template_list_entry, &scst_template_list);
270         mutex_unlock(&scst_mutex);
271
272         res = 0;
273
274         PRINT_INFO("Target template %s registered successfully", vtt->name);
275
276         mutex_unlock(&m);
277
278 out:
279         TRACE_EXIT_RES(res);
280         return res;
281
282 out_m_err:
283         mutex_unlock(&m);
284
285 out_sysfs_err:
286 #ifdef CONFIG_SCST_PROC
287         scst_cleanup_proc_target_dir_entries(vtt);
288 #endif
289         scst_tgtt_sysfs_put(vtt);
290
291 out_err:
292         PRINT_ERROR("Failed to register target template %s", vtt->name);
293         goto out;
294 }
295 EXPORT_SYMBOL(__scst_register_target_template);
296
297 void scst_unregister_target_template(struct scst_tgt_template *vtt)
298 {
299         struct scst_tgt *tgt;
300         struct scst_tgt_template *t;
301         int found = 0;
302
303         TRACE_ENTRY();
304
305         mutex_lock(&scst_mutex);
306
307         list_for_each_entry(t, &scst_template_list, scst_template_list_entry) {
308                 if (strcmp(t->name, vtt->name) == 0) {
309                         found = 1;
310                         break;
311                 }
312         }
313         if (!found) {
314                 PRINT_ERROR("Target driver %s isn't registered", vtt->name);
315                 goto out_err_up;
316         }
317
318 restart:
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);
323                 goto restart;
324         }
325         list_del(&vtt->scst_template_list_entry);
326
327         mutex_unlock(&scst_mutex);
328
329 #ifdef CONFIG_SCST_PROC
330         scst_cleanup_proc_target_dir_entries(vtt);
331 #endif
332
333         scst_tgtt_sysfs_put(vtt);
334
335         PRINT_INFO("Target template %s unregistered successfully", vtt->name);
336
337 out:
338         TRACE_EXIT();
339         return;
340
341 out_err_up:
342         mutex_unlock(&scst_mutex);
343         goto out;
344 }
345 EXPORT_SYMBOL(scst_unregister_target_template);
346
347 struct scst_tgt *scst_register(struct scst_tgt_template *vtt,
348         const char *target_name)
349 {
350         struct scst_tgt *tgt;
351         int rc = 0;
352
353         TRACE_ENTRY();
354
355         tgt = scst_alloc_tgt(vtt);
356         if (tgt == NULL) {
357                 rc = -ENOMEM;
358                 goto out_err;
359         }
360
361         rc = scst_suspend_activity(true);
362         if (rc != 0)
363                 goto out_free_tgt_err;
364
365         if (mutex_lock_interruptible(&scst_mutex) != 0) {
366                 rc = -EINTR;
367                 goto out_resume_free;
368         }
369
370         if (target_name != NULL) {
371 #ifdef CONFIG_SCST_PROC
372                 int len = strlen(target_name) +
373                         strlen(SCST_DEFAULT_ACG_NAME) + 1 + 1;
374
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);
379                         rc = -ENOMEM;
380                         goto out_unlock_resume;
381                 }
382                 sprintf(tgt->default_group_name, "%s_%s", SCST_DEFAULT_ACG_NAME,
383                         target_name);
384 #endif
385
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",
389                                 target_name);
390                         rc = -ENOMEM;
391 #ifdef CONFIG_SCST_PROC
392                         goto out_free_def_name;
393 #else
394                         goto out_unlock_resume;
395 #endif
396                 }
397                 strcpy(tgt->tgt_name, target_name);
398         } else {
399                 static int tgt_num; /* protected by scst_mutex */
400                 int len = strlen(vtt->name) +
401                         strlen(SCST_DEFAULT_TGT_NAME_SUFFIX) + 11 + 1;
402
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);
407                         rc = -ENOMEM;
408 #ifdef CONFIG_SCST_PROC
409                         goto out_free_def_name;
410 #else
411                         goto out_unlock_resume;
412 #endif
413                 }
414                 sprintf(tgt->tgt_name, "%s%s%d", vtt->name,
415                         SCST_DEFAULT_TGT_NAME_SUFFIX, tgt_num++);
416         }
417
418         tgt->default_acg = scst_alloc_add_acg(tgt->tgt_name);
419         if (tgt->default_acg == NULL)
420                 goto out_free_tgt_name;
421
422 #ifdef CONFIG_SCST_PROC
423         rc = scst_build_proc_target_entries(tgt);
424         if (rc < 0)
425                 goto out_free_acg;
426 #endif
427
428         rc = scst_create_tgt_sysfs(tgt);
429         if (rc < 0)
430 #ifdef CONFIG_SCST_PROC
431                 goto out_clean_proc;
432 #else
433                 goto out_free_acg;
434 #endif
435
436         list_add_tail(&tgt->tgt_list_entry, &vtt->tgt_list);
437
438         mutex_unlock(&scst_mutex);
439         scst_resume_activity();
440
441         PRINT_INFO("Target %s (%p) for template %s registered successfully",
442                 target_name, tgt, vtt->name);
443
444 out:
445         TRACE_EXIT();
446         return tgt;
447
448 #ifdef CONFIG_SCST_PROC
449 out_clean_proc:
450         scst_cleanup_proc_target_entries(tgt);
451 #endif
452
453 out_free_acg:
454         scst_destroy_acg(tgt->default_acg);
455
456 out_free_tgt_name:
457         kfree(tgt->tgt_name);
458
459 #ifdef CONFIG_SCST_PROC
460 out_free_def_name:
461         kfree(tgt->default_group_name);
462 #endif
463
464 out_unlock_resume:
465         mutex_unlock(&scst_mutex);
466
467 out_resume_free:
468         scst_resume_activity();
469
470 out_free_tgt_err:
471         scst_tgt_sysfs_put(tgt);
472         tgt = NULL;
473
474 out_err:
475         PRINT_ERROR("Failed to register target %s for template %s (error %d)",
476                 target_name, vtt->name, rc);
477         goto out;
478 }
479 EXPORT_SYMBOL(scst_register);
480
481 static inline int test_sess_list(struct scst_tgt *tgt)
482 {
483         int res;
484         mutex_lock(&scst_mutex);
485         res = list_empty(&tgt->sess_list);
486         mutex_unlock(&scst_mutex);
487         return res;
488 }
489
490 void scst_unregister(struct scst_tgt *tgt)
491 {
492         struct scst_session *sess;
493         struct scst_tgt_template *vtt = tgt->tgtt;
494
495         TRACE_ENTRY();
496
497         TRACE_DBG("%s", "Calling target driver's release()");
498         tgt->tgtt->release(tgt);
499         TRACE_DBG("%s", "Target driver's release() returned");
500
501         mutex_lock(&scst_mutex);
502 again:
503         list_for_each_entry(sess, &tgt->sess_list, sess_list_entry) {
504                 if (sess->shut_phase == SCST_SESS_SPH_READY) {
505                         /*
506                          * Sometimes it's hard for target driver to track all
507                          * its sessions (see scst_local, for example), so let's
508                          * help it.
509                          */
510                         mutex_unlock(&scst_mutex);
511                         scst_unregister_session(sess, 0, NULL);
512                         mutex_lock(&scst_mutex);
513                         goto again;
514                 }
515         }
516         mutex_unlock(&scst_mutex);
517
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");
521
522         scst_suspend_activity(false);
523         mutex_lock(&scst_mutex);
524
525         list_del(&tgt->tgt_list_entry);
526
527 #ifdef CONFIG_SCST_PROC
528         scst_cleanup_proc_target_entries(tgt);
529 #endif
530
531         mutex_unlock(&scst_mutex);
532         scst_resume_activity();
533
534         /*
535          * It should be before freeing of tgt_name, because acg_name
536          * points to it.
537          */
538         scst_destroy_acg(tgt->default_acg);
539
540         kfree(tgt->tgt_name);
541 #ifdef CONFIG_SCST_PROC
542         kfree(tgt->default_group_name);
543 #endif
544
545         del_timer_sync(&tgt->retry_timer);
546
547         scst_tgt_sysfs_put(tgt);
548
549         PRINT_INFO("Target %p for template %s unregistered successfully",
550                 tgt, vtt->name);
551
552         TRACE_EXIT();
553         return;
554 }
555 EXPORT_SYMBOL(scst_unregister);
556
557 static int scst_susp_wait(bool interruptible)
558 {
559         int res = 0;
560
561         TRACE_ENTRY();
562
563         if (interruptible) {
564                 res = wait_event_interruptible_timeout(scst_dev_cmd_waitQ,
565                         (atomic_read(&scst_cmd_count) == 0),
566                         SCST_SUSPENDING_TIMEOUT);
567                 if (res <= 0) {
568                         __scst_resume_activity();
569                         if (res == 0)
570                                 res = -EBUSY;
571                 } else
572                         res = 0;
573         } else
574                 wait_event(scst_dev_cmd_waitQ,
575                            atomic_read(&scst_cmd_count) == 0);
576
577         TRACE_MGMT_DBG("wait_event() returned %d", res);
578
579         TRACE_EXIT_RES(res);
580         return res;
581 }
582
583 int scst_suspend_activity(bool interruptible)
584 {
585         int res = 0;
586         bool rep = false;
587
588         TRACE_ENTRY();
589
590         if (interruptible) {
591                 if (mutex_lock_interruptible(&scst_suspend_mutex) != 0) {
592                         res = -EINTR;
593                         goto out;
594                 }
595         } else
596                 mutex_lock(&scst_suspend_mutex);
597
598         TRACE_MGMT_DBG("suspend_count %d", suspend_count);
599         suspend_count++;
600         if (suspend_count > 1)
601                 goto out_up;
602
603         set_bit(SCST_FLAG_SUSPENDING, &scst_flags);
604         set_bit(SCST_FLAG_SUSPENDED, &scst_flags);
605         /*
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.
609          */
610         smp_mb__after_set_bit();
611
612         /*
613          * See comment in scst_user.c::dev_user_task_mgmt_fn() for more
614          * information about scst_user behavior.
615          *
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().. )
619          */
620
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));
631                 rep = true;
632         }
633
634         res = scst_susp_wait(interruptible);
635         if (res != 0)
636                 goto out_clear;
637
638         clear_bit(SCST_FLAG_SUSPENDING, &scst_flags);
639         /* See comment about smp_mb() above */
640         smp_mb__after_clear_bit();
641
642         TRACE_MGMT_DBG("Waiting for %d active commands finally to complete",
643                 atomic_read(&scst_cmd_count));
644
645         res = scst_susp_wait(interruptible);
646         if (res != 0)
647                 goto out_clear;
648
649         if (rep)
650                 PRINT_INFO("%s", "All active commands completed");
651
652 out_up:
653         mutex_unlock(&scst_suspend_mutex);
654
655 out:
656         TRACE_EXIT_RES(res);
657         return res;
658
659 out_clear:
660         clear_bit(SCST_FLAG_SUSPENDING, &scst_flags);
661         /* See comment about smp_mb() above */
662         smp_mb__after_clear_bit();
663         goto out_up;
664 }
665 EXPORT_SYMBOL(scst_suspend_activity);
666
667 static void __scst_resume_activity(void)
668 {
669         struct scst_cmd_lists *l;
670
671         TRACE_ENTRY();
672
673         suspend_count--;
674         TRACE_MGMT_DBG("suspend_count %d left", suspend_count);
675         if (suspend_count > 0)
676                 goto out;
677
678         clear_bit(SCST_FLAG_SUSPENDED, &scst_flags);
679         /*
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.
682          */
683         smp_mb__after_clear_bit();
684
685         list_for_each_entry(l, &scst_cmd_lists_list, lists_list_entry) {
686                 wake_up_all(&l->cmd_list_waitQ);
687         }
688         wake_up_all(&scst_init_cmd_list_waitQ);
689
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 "
696                         "mgmt cmd list", m);
697                 list_move(&m->mgmt_cmd_list_entry, &scst_active_mgmt_cmd_list);
698         }
699         spin_unlock_irq(&scst_mcmd_lock);
700         wake_up_all(&scst_mgmt_cmd_list_waitQ);
701
702 out:
703         TRACE_EXIT();
704         return;
705 }
706
707 void scst_resume_activity(void)
708 {
709         TRACE_ENTRY();
710
711         mutex_lock(&scst_suspend_mutex);
712         __scst_resume_activity();
713         mutex_unlock(&scst_suspend_mutex);
714
715         TRACE_EXIT();
716         return;
717 }
718 EXPORT_SYMBOL(scst_resume_activity);
719
720 static int scst_register_device(struct scsi_device *scsidp)
721 {
722         int res = 0;
723         struct scst_device *dev;
724         struct scst_dev_type *dt;
725
726         TRACE_ENTRY();
727
728         res = scst_suspend_activity(true);
729         if (res != 0)
730                 goto out_err;
731
732         if (mutex_lock_interruptible(&scst_mutex) != 0) {
733                 res = -EINTR;
734                 goto out_resume;
735         }
736
737         res = scst_alloc_device(GFP_KERNEL, &dev);
738         if (res != 0)
739                 goto out_up;
740
741         dev->type = scsidp->type;
742
743         dev->virt_name = kmalloc(50, GFP_KERNEL);
744         if (dev->virt_name == NULL) {
745                 PRINT_ERROR("%s", "Unable to alloc device name");
746                 res = -ENOMEM;
747                 goto out_free_dev;
748         }
749         snprintf(dev->virt_name, 50, "%d:%d:%d:%d", scsidp->host->host_no,
750                 scsidp->channel, scsidp->id, scsidp->lun);
751
752         dev->rq_disk = alloc_disk(1);
753         if (dev->rq_disk == NULL) {
754                 PRINT_ERROR("Unable to alloc disk object for device %s",
755                         dev->virt_name);
756                 res = -ENOMEM;
757                 /* virt_name will be freed in scst_free_dev() */
758                 goto out_free_dev;
759         }
760         dev->rq_disk->major = SCST_MAJOR;
761
762         dev->scsi_dev = scsidp;
763
764         list_add_tail(&dev->dev_list_entry, &scst_dev_list);
765
766         res = scst_create_device_sysfs(dev);
767         if (res != 0)
768                 goto out_free;
769
770         list_for_each_entry(dt, &scst_dev_type_list, dev_type_list_entry) {
771                 if (dt->type == scsidp->type) {
772                         res = scst_assign_dev_handler(dev, dt);
773                         if (res != 0)
774                                 goto out_free;
775                         break;
776                 }
777         }
778
779 out_up:
780         mutex_unlock(&scst_mutex);
781
782 out_resume:
783         scst_resume_activity();
784
785 out_err:
786         if (res == 0) {
787                 PRINT_INFO("Attached to scsi%d, channel %d, id %d, lun %d, "
788                         "type %d", scsidp->host->host_no, scsidp->channel,
789                         scsidp->id, scsidp->lun, scsidp->type);
790         } else {
791                 PRINT_ERROR("Failed to attach to scsi%d, channel %d, id %d, "
792                         "lun %d, type %d", scsidp->host->host_no,
793                         scsidp->channel, scsidp->id, scsidp->lun, scsidp->type);
794         }
795
796         TRACE_EXIT_RES(res);
797         return res;
798
799 out_free:
800         list_del(&dev->dev_list_entry);
801         put_disk(dev->rq_disk);
802
803 out_free_dev:
804         scst_device_sysfs_put(dev);
805         goto out_up;
806 }
807
808 static void scst_unregister_device(struct scsi_device *scsidp)
809 {
810         struct scst_device *d, *dev = NULL;
811         struct scst_acg_dev *acg_dev, *aa;
812
813         TRACE_ENTRY();
814
815         scst_suspend_activity(false);
816         mutex_lock(&scst_mutex);
817
818         list_for_each_entry(d, &scst_dev_list, dev_list_entry) {
819                 if (d->scsi_dev == scsidp) {
820                         dev = d;
821                         TRACE_DBG("Target device %p found", dev);
822                         break;
823                 }
824         }
825         if (dev == NULL) {
826                 PRINT_ERROR("%s", "Target device not found");
827                 goto out_unblock;
828         }
829
830         list_del(&dev->dev_list_entry);
831
832         list_for_each_entry_safe(acg_dev, aa, &dev->dev_acg_dev_list,
833                                  dev_acg_dev_list_entry) {
834                 scst_acg_remove_dev(acg_dev->acg, dev, true);
835         }
836
837         scst_assign_dev_handler(dev, &scst_null_devtype);
838
839         put_disk(dev->rq_disk);
840         scst_device_sysfs_put(dev);
841
842         PRINT_INFO("Detached from scsi%d, channel %d, id %d, lun %d, type %d",
843                 scsidp->host->host_no, scsidp->channel, scsidp->id,
844                 scsidp->lun, scsidp->type);
845
846 out_unblock:
847         mutex_unlock(&scst_mutex);
848         scst_resume_activity();
849
850         TRACE_EXIT();
851         return;
852 }
853
854 static int scst_dev_handler_check(struct scst_dev_type *dev_handler)
855 {
856         int res = 0;
857
858         if (dev_handler->parse == NULL) {
859                 PRINT_ERROR("scst dev handler %s must have "
860                         "parse() method.", dev_handler->name);
861                 res = -EINVAL;
862                 goto out;
863         }
864
865         if (dev_handler->exec == NULL) {
866 #ifdef CONFIG_SCST_ALLOW_PASSTHROUGH_IO_SUBMIT_IN_SIRQ
867                 dev_handler->exec_atomic = 1;
868 #else
869                 dev_handler->exec_atomic = 0;
870 #endif
871         }
872
873         if (dev_handler->dev_done == NULL)
874                 dev_handler->dev_done_atomic = 1;
875
876 out:
877         TRACE_EXIT_RES(res);
878         return res;
879 }
880
881 int scst_register_virtual_device(struct scst_dev_type *dev_handler,
882         const char *dev_name)
883 {
884         int res, rc;
885         struct scst_device *dev = NULL;
886
887         TRACE_ENTRY();
888
889         if (dev_handler == NULL) {
890                 PRINT_ERROR("%s: valid device handler must be supplied",
891                             __func__);
892                 res = -EINVAL;
893                 goto out;
894         }
895
896         if (dev_name == NULL) {
897                 PRINT_ERROR("%s: device name must be non-NULL", __func__);
898                 res = -EINVAL;
899                 goto out;
900         }
901
902         res = scst_dev_handler_check(dev_handler);
903         if (res != 0)
904                 goto out;
905
906         res = scst_suspend_activity(true);
907         if (res != 0)
908                 goto out;
909
910         if (mutex_lock_interruptible(&scst_mutex) != 0) {
911                 res = -EINTR;
912                 goto out_resume;
913         }
914
915         res = scst_alloc_device(GFP_KERNEL, &dev);
916         if (res != 0)
917                 goto out_up;
918
919         dev->type = dev_handler->type;
920         dev->scsi_dev = NULL;
921         dev->virt_name = kstrdup(dev_name, GFP_KERNEL);
922         if (dev->virt_name == NULL) {
923                 PRINT_ERROR("Unable to allocate virt_name for dev %s",
924                         dev_name);
925                 res = -ENOMEM;
926                 goto out_release;
927         }
928         dev->virt_id = scst_virt_dev_last_id++;
929
930         list_add_tail(&dev->dev_list_entry, &scst_dev_list);
931
932         res = dev->virt_id;
933
934         rc = scst_create_device_sysfs(dev);
935         if (rc != 0) {
936                 res = rc;
937                 goto out_free_del;
938         }
939
940         rc = scst_assign_dev_handler(dev, dev_handler);
941         if (rc != 0) {
942                 res = rc;
943                 goto out_free_del;
944         }
945
946 out_up:
947         mutex_unlock(&scst_mutex);
948
949 out_resume:
950         scst_resume_activity();
951
952 out:
953         if (res > 0)
954                 PRINT_INFO("Attached to virtual device %s (id %d)",
955                         dev_name, dev->virt_id);
956         else
957                 PRINT_INFO("Failed to attach to virtual device %s", dev_name);
958
959         TRACE_EXIT_RES(res);
960         return res;
961
962 out_free_del:
963         list_del(&dev->dev_list_entry);
964
965 out_release:
966         scst_device_sysfs_put(dev);
967         goto out_up;
968 }
969 EXPORT_SYMBOL(scst_register_virtual_device);
970
971 void scst_unregister_virtual_device(int id)
972 {
973         struct scst_device *d, *dev = NULL;
974         struct scst_acg_dev *acg_dev, *aa;
975
976         TRACE_ENTRY();
977
978         scst_suspend_activity(false);
979         mutex_lock(&scst_mutex);
980
981         list_for_each_entry(d, &scst_dev_list, dev_list_entry) {
982                 if (d->virt_id == id) {
983                         dev = d;
984                         TRACE_DBG("Target device %p (id %d) found", dev, id);
985                         break;
986                 }
987         }
988         if (dev == NULL) {
989                 PRINT_ERROR("Target virtual device (id %d) not found", id);
990                 goto out_unblock;
991         }
992
993         list_del(&dev->dev_list_entry);
994
995         list_for_each_entry_safe(acg_dev, aa, &dev->dev_acg_dev_list,
996                                  dev_acg_dev_list_entry) {
997                 scst_acg_remove_dev(acg_dev->acg, dev, true);
998         }
999
1000         scst_assign_dev_handler(dev, &scst_null_devtype);
1001
1002         PRINT_INFO("Detached from virtual device %s (id %d)",
1003                 dev->virt_name, dev->virt_id);
1004
1005         scst_device_sysfs_put(dev);
1006
1007 out_unblock:
1008         mutex_unlock(&scst_mutex);
1009         scst_resume_activity();
1010
1011         TRACE_EXIT();
1012         return;
1013 }
1014 EXPORT_SYMBOL(scst_unregister_virtual_device);
1015
1016 int __scst_register_dev_driver(struct scst_dev_type *dev_type,
1017         const char *version)
1018 {
1019         struct scst_dev_type *dt;
1020         struct scst_device *dev;
1021         int res;
1022         int exist;
1023
1024         TRACE_ENTRY();
1025
1026         if (strcmp(version, SCST_INTERFACE_VERSION) != 0) {
1027                 PRINT_ERROR("Incorrect version of dev handler %s",
1028                         dev_type->name);
1029                 res = -EINVAL;
1030                 goto out_error;
1031         }
1032
1033         res = scst_dev_handler_check(dev_type);
1034         if (res != 0)
1035                 goto out_error;
1036
1037 #if !defined(SCSI_EXEC_REQ_FIFO_DEFINED)
1038         if (dev_type->exec == NULL) {
1039 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 30)
1040 #if !defined(CONFIG_SCST_STRICT_SERIALIZING)
1041                 PRINT_ERROR("Pass-through dev handlers (handler \"%s\") not "
1042                         "supported. Consider applying on your kernel patch "
1043                         "scst_exec_req_fifo-<kernel-version> or define "
1044                         "CONFIG_SCST_STRICT_SERIALIZING", dev_type->name);
1045 #endif /* !defined(CONFIG_SCST_STRICT_SERIALIZING) */
1046 #else  /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 30) */
1047                 PRINT_ERROR("Pass-through dev handlers (handler \"%s\") not "
1048                         "supported. Consider applying on your kernel patch "
1049                         "scst_exec_req_fifo-<kernel-version>", dev_type->name);
1050 #endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 30) */
1051                 res = -EINVAL;
1052                 goto out_error;
1053         }
1054 #endif /* !defined(SCSI_EXEC_REQ_FIFO_DEFINED) */
1055
1056         res = scst_suspend_activity(true);
1057         if (res != 0)
1058                 goto out_error;
1059
1060         if (mutex_lock_interruptible(&scst_mutex) != 0) {
1061                 res = -EINTR;
1062                 goto out_err_res;
1063         }
1064
1065         exist = 0;
1066         list_for_each_entry(dt, &scst_dev_type_list, dev_type_list_entry) {
1067                 if (strcmp(dt->name, dev_type->name) == 0) {
1068                         PRINT_ERROR("Device type handler \"%s\" already "
1069                                 "exist", dt->name);
1070                         exist = 1;
1071                         break;
1072                 }
1073         }
1074         if (exist)
1075                 goto out_up;
1076
1077 #ifdef CONFIG_SCST_PROC
1078         if (!dev_type->no_proc) {
1079                 res = scst_build_proc_dev_handler_dir_entries(dev_type);
1080                 if (res < 0)
1081                         goto out_up;
1082         }
1083 #endif
1084
1085         res = scst_create_devt_sysfs(dev_type);
1086         if (res < 0)
1087                 goto out_free;
1088
1089         list_add_tail(&dev_type->dev_type_list_entry, &scst_dev_type_list);
1090
1091         list_for_each_entry(dev, &scst_dev_list, dev_list_entry) {
1092                 if (dev->scsi_dev == NULL || dev->handler != &scst_null_devtype)
1093                         continue;
1094                 if (dev->scsi_dev->type == dev_type->type)
1095                         scst_assign_dev_handler(dev, dev_type);
1096         }
1097
1098         mutex_unlock(&scst_mutex);
1099         scst_resume_activity();
1100
1101         if (res == 0) {
1102                 PRINT_INFO("Device handler \"%s\" for type %d registered "
1103                         "successfully", dev_type->name, dev_type->type);
1104         }
1105
1106 out:
1107         TRACE_EXIT_RES(res);
1108         return res;
1109
1110 out_free:
1111 #ifdef CONFIG_SCST_PROC
1112         if (!dev_type->no_proc)
1113                 scst_cleanup_proc_dev_handler_dir_entries(dev_type);
1114 #endif
1115         scst_devt_sysfs_put(dev_type);
1116
1117 out_up:
1118         mutex_unlock(&scst_mutex);
1119
1120 out_err_res:
1121         scst_resume_activity();
1122
1123 out_error:
1124         PRINT_ERROR("Failed to register device handler \"%s\" for type %d",
1125                 dev_type->name, dev_type->type);
1126         goto out;
1127 }
1128 EXPORT_SYMBOL(__scst_register_dev_driver);
1129
1130 void scst_unregister_dev_driver(struct scst_dev_type *dev_type)
1131 {
1132         struct scst_device *dev;
1133         struct scst_dev_type *dt;
1134         int found = 0;
1135
1136         TRACE_ENTRY();
1137
1138         scst_suspend_activity(false);
1139         mutex_lock(&scst_mutex);
1140
1141         list_for_each_entry(dt, &scst_dev_type_list, dev_type_list_entry) {
1142                 if (strcmp(dt->name, dev_type->name) == 0) {
1143                         found = 1;
1144                         break;
1145                 }
1146         }
1147         if (!found) {
1148                 PRINT_ERROR("Dev handler \"%s\" isn't registered",
1149                         dev_type->name);
1150                 goto out_up;
1151         }
1152
1153         list_for_each_entry(dev, &scst_dev_list, dev_list_entry) {
1154                 if (dev->handler == dev_type) {
1155                         scst_assign_dev_handler(dev, &scst_null_devtype);
1156                         TRACE_DBG("Dev handler removed from device %p", dev);
1157                 }
1158         }
1159
1160         list_del(&dev_type->dev_type_list_entry);
1161
1162         mutex_unlock(&scst_mutex);
1163         scst_resume_activity();
1164
1165 #ifdef CONFIG_SCST_PROC
1166         scst_cleanup_proc_dev_handler_dir_entries(dev_type);
1167 #endif
1168
1169         scst_devt_sysfs_put(dev_type);
1170
1171         PRINT_INFO("Device handler \"%s\" for type %d unloaded",
1172                    dev_type->name, dev_type->type);
1173
1174 out:
1175         TRACE_EXIT();
1176         return;
1177
1178 out_up:
1179         mutex_unlock(&scst_mutex);
1180         scst_resume_activity();
1181         goto out;
1182 }
1183 EXPORT_SYMBOL(scst_unregister_dev_driver);
1184
1185 int __scst_register_virtual_dev_driver(struct scst_dev_type *dev_type,
1186         const char *version)
1187 {
1188         int res;
1189
1190         TRACE_ENTRY();
1191
1192         if (strcmp(version, SCST_INTERFACE_VERSION) != 0) {
1193                 PRINT_ERROR("Incorrect version of virtual dev handler %s",
1194                         dev_type->name);
1195                 res = -EINVAL;
1196                 goto out_err;
1197         }
1198
1199         res = scst_dev_handler_check(dev_type);
1200         if (res != 0)
1201                 goto out_err;
1202
1203 #ifdef CONFIG_SCST_PROC
1204         if (!dev_type->no_proc) {
1205                 res = scst_build_proc_dev_handler_dir_entries(dev_type);
1206                 if (res < 0)
1207                         goto out_err;
1208         }
1209 #endif
1210
1211         res = scst_create_devt_sysfs(dev_type);
1212         if (res < 0)
1213                 goto out_free;
1214
1215         if (dev_type->type != -1) {
1216                 PRINT_INFO("Virtual device handler %s for type %d "
1217                         "registered successfully", dev_type->name,
1218                         dev_type->type);
1219         } else {
1220                 PRINT_INFO("Virtual device handler \"%s\" registered "
1221                         "successfully", dev_type->name);
1222         }
1223
1224 out:
1225         TRACE_EXIT_RES(res);
1226         return res;
1227
1228 out_free:
1229 #ifdef CONFIG_SCST_PROC
1230         if (!dev_type->no_proc)
1231                 scst_cleanup_proc_dev_handler_dir_entries(dev_type);
1232 #endif
1233
1234         scst_devt_sysfs_put(dev_type);
1235
1236 out_err:
1237         PRINT_ERROR("Failed to register virtual device handler \"%s\"",
1238                 dev_type->name);
1239         goto out;
1240 }
1241 EXPORT_SYMBOL(__scst_register_virtual_dev_driver);
1242
1243 void scst_unregister_virtual_dev_driver(struct scst_dev_type *dev_type)
1244 {
1245         TRACE_ENTRY();
1246
1247 #ifdef CONFIG_SCST_PROC
1248         if (!dev_type->no_proc)
1249                 scst_cleanup_proc_dev_handler_dir_entries(dev_type);
1250 #endif
1251
1252         scst_devt_sysfs_put(dev_type);
1253
1254         PRINT_INFO("Device handler \"%s\" unloaded", dev_type->name);
1255
1256         TRACE_EXIT();
1257         return;
1258 }
1259 EXPORT_SYMBOL(scst_unregister_virtual_dev_driver);
1260
1261 /* Called under scst_mutex */
1262 int scst_add_dev_threads(struct scst_device *dev, int num)
1263 {
1264         int i, res = 0;
1265         int n = 0;
1266         struct scst_cmd_thread_t *thr;
1267         char nm[12];
1268
1269         TRACE_ENTRY();
1270
1271         list_for_each_entry(thr, &dev->threads_list, thread_list_entry) {
1272                 n++;
1273         }
1274
1275         for (i = 0; i < num; i++) {
1276                 thr = kmalloc(sizeof(*thr), GFP_KERNEL);
1277                 if (!thr) {
1278                         res = -ENOMEM;
1279                         PRINT_ERROR("Failed to allocate thr %d", res);
1280                         goto out_del;
1281                 }
1282                 strncpy(nm, dev->handler->name, ARRAY_SIZE(nm)-1);
1283                 nm[ARRAY_SIZE(nm)-1] = '\0';
1284                 thr->cmd_thread = kthread_create(scst_cmd_thread,
1285                         &dev->cmd_lists, "%sd%d_%d", nm, dev->dev_num, n++);
1286                 if (IS_ERR(thr->cmd_thread)) {
1287                         res = PTR_ERR(thr->cmd_thread);
1288                         PRINT_ERROR("kthread_create() failed: %d", res);
1289                         kfree(thr);
1290                         goto out_del;
1291                 }
1292
1293                 list_add(&thr->thread_list_entry, &dev->threads_list);
1294
1295                 /*
1296                  * ToDo: better to use tgt_dev_io_context instead, but we
1297                  * are not ready for that yet.
1298                  */
1299                 __exit_io_context(thr->cmd_thread->io_context);
1300                 thr->cmd_thread->io_context = ioc_task_link(dev->dev_io_ctx);
1301                 TRACE_DBG("Setting dev io ctx %p on thr %d", dev->dev_io_ctx,
1302                         thr->cmd_thread->pid);
1303
1304                 wake_up_process(thr->cmd_thread);
1305         }
1306
1307 out:
1308         TRACE_EXIT_RES(res);
1309         return res;
1310
1311 out_del:
1312         scst_del_dev_threads(dev, i);
1313         goto out;
1314 }
1315
1316 /* Called under scst_mutex and suspended activity */
1317 static int scst_create_dev_threads(struct scst_device *dev)
1318 {
1319         int res = 0;
1320         int threads_num;
1321
1322         TRACE_ENTRY();
1323
1324         if (dev->handler->threads_num <= 0)
1325                 goto out;
1326
1327         threads_num = dev->handler->threads_num;
1328
1329         spin_lock_init(&dev->cmd_lists.cmd_list_lock);
1330         INIT_LIST_HEAD(&dev->cmd_lists.active_cmd_list);
1331         init_waitqueue_head(&dev->cmd_lists.cmd_list_waitQ);
1332
1333         res = scst_add_dev_threads(dev, threads_num);
1334         if (res != 0)
1335                 goto out;
1336
1337         mutex_lock(&scst_suspend_mutex);
1338         list_add_tail(&dev->cmd_lists.lists_list_entry,
1339                 &scst_cmd_lists_list);
1340         mutex_unlock(&scst_suspend_mutex);
1341
1342         dev->p_cmd_lists = &dev->cmd_lists;
1343
1344 out:
1345         TRACE_EXIT_RES(res);
1346         return res;
1347 }
1348
1349 /* Called under scst_mutex */
1350 void scst_del_dev_threads(struct scst_device *dev, int num)
1351 {
1352         struct scst_cmd_thread_t *ct, *tmp;
1353         int i = 0;
1354
1355         TRACE_ENTRY();
1356
1357         if (num == 0)
1358                 goto out;
1359
1360         list_for_each_entry_safe_reverse(ct, tmp, &dev->threads_list,
1361                                 thread_list_entry) {
1362                 int rc;
1363                 struct scst_tgt_dev *tgt_dev;
1364
1365                 list_for_each_entry(tgt_dev, &dev->dev_tgt_dev_list,
1366                                 dev_tgt_dev_list_entry) {
1367                         struct scst_thr_data_hdr *td;
1368                         td = __scst_find_thr_data(tgt_dev, ct->cmd_thread);
1369                         if (td != NULL) {
1370                                 scst_thr_data_put(td);
1371                                 break;
1372                         }
1373                 }
1374
1375                 rc = kthread_stop(ct->cmd_thread);
1376                 if (rc < 0)
1377                         TRACE_MGMT_DBG("kthread_stop() failed: %d", rc);
1378
1379                 list_del(&ct->thread_list_entry);
1380                 kfree(ct);
1381
1382                 if ((num > 0) && (++i >= num))
1383                         break;
1384         }
1385
1386 out:
1387         TRACE_EXIT();
1388         return;
1389 }
1390
1391 /* Called under scst_mutex and suspended activity */
1392 static void scst_stop_dev_threads(struct scst_device *dev)
1393 {
1394         TRACE_ENTRY();
1395
1396         if (list_empty(&dev->threads_list))
1397                 goto out;
1398
1399         scst_del_dev_threads(dev, -1);
1400
1401         if (dev->p_cmd_lists == &dev->cmd_lists) {
1402                 mutex_lock(&scst_suspend_mutex);
1403                 list_del(&dev->cmd_lists.lists_list_entry);
1404                 mutex_unlock(&scst_suspend_mutex);
1405         }
1406
1407 out:
1408         TRACE_EXIT();
1409         return;
1410 }
1411
1412 /* The activity supposed to be suspended and scst_mutex held */
1413 int scst_assign_dev_handler(struct scst_device *dev,
1414         struct scst_dev_type *handler)
1415 {
1416         int res = 0;
1417         struct scst_tgt_dev *tgt_dev;
1418         LIST_HEAD(attached_tgt_devs);
1419
1420         TRACE_ENTRY();
1421
1422         sBUG_ON(handler == NULL);
1423
1424         if (dev->handler == handler)
1425                 goto out;
1426
1427         if (dev->handler == NULL)
1428                 goto assign;
1429
1430         if (dev->handler->detach_tgt) {
1431                 list_for_each_entry(tgt_dev, &dev->dev_tgt_dev_list,
1432                                 dev_tgt_dev_list_entry) {
1433                         TRACE_DBG("Calling dev handler's detach_tgt(%p)",
1434                                 tgt_dev);
1435                         dev->handler->detach_tgt(tgt_dev);
1436                         TRACE_DBG("%s", "Dev handler's detach_tgt() returned");
1437                 }
1438         }
1439
1440         if (dev->handler->detach) {
1441                 TRACE_DBG("%s", "Calling dev handler's detach()");
1442                 dev->handler->detach(dev);
1443                 TRACE_DBG("%s", "Old handler's detach() returned");
1444         }
1445
1446         scst_stop_dev_threads(dev);
1447
1448         scst_devt_dev_sysfs_put(dev);
1449
1450 assign:
1451         dev->handler = handler;
1452
1453         if (handler == NULL)
1454                 goto out;
1455
1456         res = scst_create_devt_dev_sysfs(dev);
1457         if (res != 0)
1458                 goto out_null;
1459
1460         res = scst_create_dev_threads(dev);
1461         if (res != 0)
1462                 goto out_remove_sysfs;
1463
1464         if (handler->attach) {
1465                 TRACE_DBG("Calling new dev handler's attach(%p)", dev);
1466                 res = handler->attach(dev);
1467                 TRACE_DBG("New dev handler's attach() returned %d", res);
1468                 if (res != 0) {
1469                         PRINT_ERROR("New device handler's %s attach() "
1470                                 "failed: %d", handler->name, res);
1471                         goto out_thr_null;
1472                 }
1473         }
1474
1475         if (handler->attach_tgt) {
1476                 list_for_each_entry(tgt_dev, &dev->dev_tgt_dev_list,
1477                                 dev_tgt_dev_list_entry) {
1478                         TRACE_DBG("Calling dev handler's attach_tgt(%p)",
1479                                 tgt_dev);
1480                         res = handler->attach_tgt(tgt_dev);
1481                         TRACE_DBG("%s", "Dev handler's attach_tgt() returned");
1482                         if (res != 0) {
1483                                 PRINT_ERROR("Device handler's %s attach_tgt() "
1484                                     "failed: %d", handler->name, res);
1485                                 goto out_err_detach_tgt;
1486                         }
1487                         list_add_tail(&tgt_dev->extra_tgt_dev_list_entry,
1488                                 &attached_tgt_devs);
1489                 }
1490         }
1491
1492 out:
1493         TRACE_EXIT_RES(res);
1494         return res;
1495
1496 out_err_detach_tgt:
1497         if (handler && handler->detach_tgt) {
1498                 list_for_each_entry(tgt_dev, &attached_tgt_devs,
1499                                  extra_tgt_dev_list_entry) {
1500                         TRACE_DBG("Calling handler's detach_tgt(%p)",
1501                                 tgt_dev);
1502                         handler->detach_tgt(tgt_dev);
1503                         TRACE_DBG("%s", "Handler's detach_tgt() returned");
1504                 }
1505         }
1506         if (handler && handler->detach) {
1507                 TRACE_DBG("%s", "Calling handler's detach()");
1508                 handler->detach(dev);
1509                 TRACE_DBG("%s", "Handler's detach() returned");
1510         }
1511
1512 out_thr_null:
1513         scst_stop_dev_threads(dev);
1514
1515 out_remove_sysfs:
1516         scst_devt_dev_sysfs_put(dev);
1517
1518 out_null:
1519         dev->handler = &scst_null_devtype;
1520         goto out;
1521 }
1522
1523 int scst_global_threads_count(void)
1524 {
1525         int i;
1526
1527         /*
1528          * Just to lower the race window, when user can get just changed value
1529          */
1530         mutex_lock(&scst_global_threads_mutex);
1531         i = scst_nr_global_threads;
1532         mutex_unlock(&scst_global_threads_mutex);
1533         return i;
1534 }
1535
1536 static void scst_threads_info_init(void)
1537 {
1538         mutex_init(&scst_global_threads_mutex);
1539         INIT_LIST_HEAD(&scst_global_threads_list);
1540 }
1541
1542 /* scst_global_threads_mutex supposed to be held */
1543 void __scst_del_global_threads(int num)
1544 {
1545         struct scst_cmd_thread_t *ct, *tmp;
1546
1547         TRACE_ENTRY();
1548
1549         if (num == 0)
1550                 goto out;
1551
1552         list_for_each_entry_safe(ct, tmp, &scst_global_threads_list,
1553                                 thread_list_entry) {
1554                 int res;
1555
1556                 res = kthread_stop(ct->cmd_thread);
1557                 if (res < 0)
1558                         TRACE_MGMT_DBG("kthread_stop() failed: %d", res);
1559                 list_del(&ct->thread_list_entry);
1560                 kfree(ct);
1561                 scst_nr_global_threads--;
1562                 --num;
1563                 if (num == 0)
1564                         break;
1565         }
1566
1567 out:
1568         TRACE_EXIT();
1569         return;
1570 }
1571
1572 /* scst_global_threads_mutex supposed to be held */
1573 int __scst_add_global_threads(int num)
1574 {
1575         int res = 0, i;
1576         static int scst_thread_num;
1577
1578         TRACE_ENTRY();
1579
1580         for (i = 0; i < num; i++) {
1581                 struct scst_cmd_thread_t *thr;
1582
1583                 thr = kmalloc(sizeof(*thr), GFP_KERNEL);
1584                 if (!thr) {
1585                         res = -ENOMEM;
1586                         PRINT_ERROR("fail to allocate thr %d", res);
1587                         goto out_error;
1588                 }
1589                 thr->cmd_thread = kthread_create(scst_cmd_thread,
1590                         &scst_main_cmd_lists, "scsi_tgt%d",
1591                         scst_thread_num++);
1592                 if (IS_ERR(thr->cmd_thread)) {
1593                         res = PTR_ERR(thr->cmd_thread);
1594                         PRINT_ERROR("kthread_create() failed: %d", res);
1595                         kfree(thr);
1596                         goto out_error;
1597                 }
1598
1599                 list_add(&thr->thread_list_entry, &scst_global_threads_list);
1600                 scst_nr_global_threads++;
1601
1602                 wake_up_process(thr->cmd_thread);
1603         }
1604         res = 0;
1605
1606 out:
1607         TRACE_EXIT_RES(res);
1608         return res;
1609
1610 out_error:
1611         __scst_del_global_threads(i);
1612         goto out;
1613 }
1614
1615 int scst_add_global_threads(int num)
1616 {
1617         int res;
1618
1619         TRACE_ENTRY();
1620
1621         mutex_lock(&scst_global_threads_mutex);
1622         res = __scst_add_global_threads(num);
1623         mutex_unlock(&scst_global_threads_mutex);
1624
1625         TRACE_EXIT_RES(res);
1626         return res;
1627 }
1628 EXPORT_SYMBOL(scst_add_global_threads);
1629
1630 void scst_del_global_threads(int num)
1631 {
1632         TRACE_ENTRY();
1633
1634         mutex_lock(&scst_global_threads_mutex);
1635         __scst_del_global_threads(num);
1636         mutex_unlock(&scst_global_threads_mutex);
1637
1638         TRACE_EXIT();
1639         return;
1640 }
1641 EXPORT_SYMBOL(scst_del_global_threads);
1642
1643 static void scst_stop_all_threads(void)
1644 {
1645         TRACE_ENTRY();
1646
1647         mutex_lock(&scst_global_threads_mutex);
1648         __scst_del_global_threads(-1);
1649         if (scst_mgmt_cmd_thread)
1650                 kthread_stop(scst_mgmt_cmd_thread);
1651         if (scst_mgmt_thread)
1652                 kthread_stop(scst_mgmt_thread);
1653         if (scst_init_cmd_thread)
1654                 kthread_stop(scst_init_cmd_thread);
1655         mutex_unlock(&scst_global_threads_mutex);
1656
1657         TRACE_EXIT();
1658         return;
1659 }
1660
1661 static int scst_start_all_threads(int num)
1662 {
1663         int res;
1664
1665         TRACE_ENTRY();
1666
1667         mutex_lock(&scst_global_threads_mutex);
1668         res = __scst_add_global_threads(num);
1669         if (res < 0)
1670                 goto out;
1671
1672         scst_init_cmd_thread = kthread_run(scst_init_thread,
1673                 NULL, "scsi_tgt_init");
1674         if (IS_ERR(scst_init_cmd_thread)) {
1675                 res = PTR_ERR(scst_init_cmd_thread);
1676                 PRINT_ERROR("kthread_create() for init cmd failed: %d", res);
1677                 scst_init_cmd_thread = NULL;
1678                 goto out;
1679         }
1680
1681         scst_mgmt_cmd_thread = kthread_run(scst_tm_thread,
1682                 NULL, "scsi_tm");
1683         if (IS_ERR(scst_mgmt_cmd_thread)) {
1684                 res = PTR_ERR(scst_mgmt_cmd_thread);
1685                 PRINT_ERROR("kthread_create() for TM failed: %d", res);
1686                 scst_mgmt_cmd_thread = NULL;
1687                 goto out;
1688         }
1689
1690         scst_mgmt_thread = kthread_run(scst_global_mgmt_thread,
1691                 NULL, "scsi_tgt_mgmt");
1692         if (IS_ERR(scst_mgmt_thread)) {
1693                 res = PTR_ERR(scst_mgmt_thread);
1694                 PRINT_ERROR("kthread_create() for mgmt failed: %d", res);
1695                 scst_mgmt_thread = NULL;
1696                 goto out;
1697         }
1698
1699 out:
1700         mutex_unlock(&scst_global_threads_mutex);
1701         TRACE_EXIT_RES(res);
1702         return res;
1703 }
1704
1705 void scst_get(void)
1706 {
1707         __scst_get(0);
1708 }
1709 EXPORT_SYMBOL(scst_get);
1710
1711 void scst_put(void)
1712 {
1713         __scst_put();
1714 }
1715 EXPORT_SYMBOL(scst_put);
1716
1717 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26)
1718 static int scst_add(struct class_device *cdev, struct class_interface *intf)
1719 #else
1720 static int scst_add(struct device *cdev, struct class_interface *intf)
1721 #endif
1722 {
1723         struct scsi_device *scsidp;
1724         int res = 0;
1725
1726         TRACE_ENTRY();
1727
1728 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26)
1729         scsidp = to_scsi_device(cdev->dev);
1730 #else
1731         scsidp = to_scsi_device(cdev->parent);
1732 #endif
1733
1734         if (strcmp(scsidp->host->hostt->name, SCST_LOCAL_NAME) != 0)
1735                 res = scst_register_device(scsidp);
1736
1737         TRACE_EXIT();
1738         return res;
1739 }
1740
1741 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26)
1742 static void scst_remove(struct class_device *cdev, struct class_interface *intf)
1743 #else
1744 static void scst_remove(struct device *cdev, struct class_interface *intf)
1745 #endif
1746 {
1747         struct scsi_device *scsidp;
1748
1749         TRACE_ENTRY();
1750
1751 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26)
1752         scsidp = to_scsi_device(cdev->dev);
1753 #else
1754         scsidp = to_scsi_device(cdev->parent);
1755 #endif
1756
1757         if (strcmp(scsidp->host->hostt->name, SCST_LOCAL_NAME) != 0)
1758                 scst_unregister_device(scsidp);
1759
1760         TRACE_EXIT();
1761         return;
1762 }
1763
1764 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 26)
1765 static struct class_interface scst_interface = {
1766         .add = scst_add,
1767         .remove = scst_remove,
1768 };
1769 #else
1770 static struct class_interface scst_interface = {
1771         .add_dev = scst_add,
1772         .remove_dev = scst_remove,
1773 };
1774 #endif
1775
1776 static void __init scst_print_config(void)
1777 {
1778         char buf[128];
1779         int i, j;
1780
1781         i = snprintf(buf, sizeof(buf), "Enabled features: ");
1782         j = i;
1783
1784 #ifdef CONFIG_SCST_STRICT_SERIALIZING
1785         i += snprintf(&buf[i], sizeof(buf) - i, "Strict serializing");
1786 #endif
1787
1788 #ifdef CONFIG_SCST_EXTRACHECKS
1789         i += snprintf(&buf[i], sizeof(buf) - i, "%sEXTRACHECKS",
1790                 (j == i) ? "" : ", ");
1791 #endif
1792
1793 #ifdef CONFIG_SCST_TRACING
1794         i += snprintf(&buf[i], sizeof(buf) - i, "%sTRACING",
1795                 (j == i) ? "" : ", ");
1796 #endif
1797
1798 #ifdef CONFIG_SCST_DEBUG
1799         i += snprintf(&buf[i], sizeof(buf) - i, "%sDEBUG",
1800                 (j == i) ? "" : ", ");
1801 #endif
1802
1803 #ifdef CONFIG_SCST_DEBUG_TM
1804         i += snprintf(&buf[i], sizeof(buf) - i, "%sDEBUG_TM",
1805                 (j == i) ? "" : ", ");
1806 #endif
1807
1808 #ifdef CONFIG_SCST_DEBUG_RETRY
1809         i += snprintf(&buf[i], sizeof(buf) - i, "%sDEBUG_RETRY",
1810                 (j == i) ? "" : ", ");
1811 #endif
1812
1813 #ifdef CONFIG_SCST_DEBUG_OOM
1814         i += snprintf(&buf[i], sizeof(buf) - i, "%sDEBUG_OOM",
1815                 (j == i) ? "" : ", ");
1816 #endif
1817
1818 #ifdef CONFIG_SCST_DEBUG_SN
1819         i += snprintf(&buf[i], sizeof(buf) - i, "%sDEBUG_SN",
1820                 (j == i) ? "" : ", ");
1821 #endif
1822
1823 #ifdef CONFIG_SCST_USE_EXPECTED_VALUES
1824         i += snprintf(&buf[i], sizeof(buf) - i, "%sUSE_EXPECTED_VALUES",
1825                 (j == i) ? "" : ", ");
1826 #endif
1827
1828 #ifdef CONFIG_SCST_ALLOW_PASSTHROUGH_IO_SUBMIT_IN_SIRQ
1829         i += snprintf(&buf[i], sizeof(buf) - i,
1830                 "%sALLOW_PASSTHROUGH_IO_SUBMIT_IN_SIRQ",
1831                 (j == i) ? "" : ", ");
1832 #endif
1833
1834 #ifdef CONFIG_SCST_STRICT_SECURITY
1835         i += snprintf(&buf[i], sizeof(buf) - i, "%sSCST_STRICT_SECURITY",
1836                 (j == i) ? "" : ", ");
1837 #endif
1838
1839         if (j != i)
1840                 PRINT_INFO("%s", buf);
1841 }
1842
1843 static int __init init_scst(void)
1844 {
1845         int res, i;
1846         int scst_num_cpus;
1847
1848         TRACE_ENTRY();
1849
1850 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18)
1851         {
1852                 struct scsi_request *req;
1853                 BUILD_BUG_ON(SCST_SENSE_BUFFERSIZE !=
1854                         sizeof(req->sr_sense_buffer));
1855         }
1856 #else
1857         {
1858                 struct scsi_sense_hdr *shdr;
1859                 BUILD_BUG_ON(SCST_SENSE_BUFFERSIZE < sizeof(*shdr));
1860         }
1861 #endif
1862         {
1863                 struct scst_tgt_dev *t;
1864                 struct scst_cmd *c;
1865                 BUILD_BUG_ON(sizeof(t->curr_sn) != sizeof(t->expected_sn));
1866                 BUILD_BUG_ON(sizeof(c->sn) != sizeof(t->expected_sn));
1867         }
1868
1869 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 27)
1870 #if !defined(SCST_IO_CONTEXT)
1871         PRINT_WARNING("%s", "Patch io_context was not applied on "
1872                 "your kernel. SCST will be working with not the best "
1873                 "performance.");
1874 #endif
1875 #else
1876         PRINT_WARNING("%s", "There is no patch io_context for your kernel "
1877                 "version. For performance reasons it is strongly recommended "
1878                 "to upgrade your kernel to version >= 2.6.27.x.");
1879 #endif
1880
1881         mutex_init(&scst_mutex);
1882         INIT_LIST_HEAD(&scst_template_list);
1883         INIT_LIST_HEAD(&scst_dev_list);
1884         INIT_LIST_HEAD(&scst_dev_type_list);
1885         spin_lock_init(&scst_main_lock);
1886         INIT_LIST_HEAD(&scst_acg_list);
1887         spin_lock_init(&scst_init_lock);
1888         init_waitqueue_head(&scst_init_cmd_list_waitQ);
1889         INIT_LIST_HEAD(&scst_init_cmd_list);
1890 #if defined(CONFIG_SCST_DEBUG) || defined(CONFIG_SCST_TRACING)
1891         scst_trace_flag = SCST_DEFAULT_LOG_FLAGS;
1892 #endif
1893         atomic_set(&scst_cmd_count, 0);
1894         spin_lock_init(&scst_mcmd_lock);
1895         INIT_LIST_HEAD(&scst_active_mgmt_cmd_list);
1896         INIT_LIST_HEAD(&scst_delayed_mgmt_cmd_list);
1897         init_waitqueue_head(&scst_mgmt_cmd_list_waitQ);
1898         init_waitqueue_head(&scst_mgmt_waitQ);
1899         spin_lock_init(&scst_mgmt_lock);
1900         INIT_LIST_HEAD(&scst_sess_init_list);
1901         INIT_LIST_HEAD(&scst_sess_shut_list);
1902         init_waitqueue_head(&scst_dev_cmd_waitQ);
1903         mutex_init(&scst_suspend_mutex);
1904         INIT_LIST_HEAD(&scst_cmd_lists_list);
1905         scst_virt_dev_last_id = 1;
1906         spin_lock_init(&scst_main_cmd_lists.cmd_list_lock);
1907         INIT_LIST_HEAD(&scst_main_cmd_lists.active_cmd_list);
1908         init_waitqueue_head(&scst_main_cmd_lists.cmd_list_waitQ);
1909         list_add_tail(&scst_main_cmd_lists.lists_list_entry,
1910                 &scst_cmd_lists_list);
1911
1912         res = scst_lib_init();
1913         if (res != 0)
1914                 goto out;
1915
1916         scst_num_cpus = num_online_cpus();
1917
1918         /* ToDo: register_cpu_notifier() */
1919
1920         if (scst_threads == 0)
1921                 scst_threads = scst_num_cpus;
1922
1923         if (scst_threads < 1) {
1924                 PRINT_ERROR("%s", "scst_threads can not be less than 1");
1925                 scst_threads = scst_num_cpus;
1926         }
1927
1928         scst_threads_info_init();
1929
1930 #define INIT_CACHEP(p, s, o) do {                                       \
1931                 p = KMEM_CACHE(s, SCST_SLAB_FLAGS);                     \
1932                 TRACE_MEM("Slab create: %s at %p size %zd", #s, p,      \
1933                           sizeof(struct s));                            \
1934                 if (p == NULL) {                                        \
1935                         res = -ENOMEM;                                  \
1936                         goto o;                                         \
1937                 }                                                       \
1938         } while (0)
1939
1940         INIT_CACHEP(scst_mgmt_cachep, scst_mgmt_cmd, out_lib_exit);
1941         INIT_CACHEP(scst_mgmt_stub_cachep, scst_mgmt_cmd_stub,
1942                         out_destroy_mgmt_cache);
1943         INIT_CACHEP(scst_ua_cachep, scst_tgt_dev_UA,
1944                         out_destroy_mgmt_stub_cache);
1945         {
1946                 struct scst_sense { uint8_t s[SCST_SENSE_BUFFERSIZE]; };
1947                 INIT_CACHEP(scst_sense_cachep, scst_sense,
1948                             out_destroy_ua_cache);
1949         }
1950         INIT_CACHEP(scst_aen_cachep, scst_aen, out_destroy_sense_cache);
1951         INIT_CACHEP(scst_cmd_cachep, scst_cmd, out_destroy_aen_cache);
1952         INIT_CACHEP(scst_sess_cachep, scst_session, out_destroy_cmd_cache);
1953         INIT_CACHEP(scst_tgtd_cachep, scst_tgt_dev, out_destroy_sess_cache);
1954         INIT_CACHEP(scst_acgd_cachep, scst_acg_dev, out_destroy_tgt_cache);
1955
1956         scst_mgmt_mempool = mempool_create(64, mempool_alloc_slab,
1957                 mempool_free_slab, scst_mgmt_cachep);
1958         if (scst_mgmt_mempool == NULL) {
1959                 res = -ENOMEM;
1960                 goto out_destroy_acg_cache;
1961         }
1962
1963         /*
1964          * All mgmt stubs, UAs and sense buffers are bursty and loosing them
1965          * may have fatal consequences, so let's have big pools for them.
1966          */
1967
1968         scst_mgmt_stub_mempool = mempool_create(1024, mempool_alloc_slab,
1969                 mempool_free_slab, scst_mgmt_stub_cachep);
1970         if (scst_mgmt_stub_mempool == NULL) {
1971                 res = -ENOMEM;
1972                 goto out_destroy_mgmt_mempool;
1973         }
1974
1975         scst_ua_mempool = mempool_create(512, mempool_alloc_slab,
1976                 mempool_free_slab, scst_ua_cachep);
1977         if (scst_ua_mempool == NULL) {
1978                 res = -ENOMEM;
1979                 goto out_destroy_mgmt_stub_mempool;
1980         }
1981
1982         scst_sense_mempool = mempool_create(1024, mempool_alloc_slab,
1983                 mempool_free_slab, scst_sense_cachep);
1984         if (scst_sense_mempool == NULL) {
1985                 res = -ENOMEM;
1986                 goto out_destroy_ua_mempool;
1987         }
1988
1989         scst_aen_mempool = mempool_create(100, mempool_alloc_slab,
1990                 mempool_free_slab, scst_aen_cachep);
1991         if (scst_aen_mempool == NULL) {
1992                 res = -ENOMEM;
1993                 goto out_destroy_sense_mempool;
1994         }
1995
1996         res = scst_sysfs_init();
1997         if (res != 0)
1998                 goto out_destroy_aen_mempool;
1999
2000         if (scst_max_cmd_mem == 0) {
2001                 struct sysinfo si;
2002                 si_meminfo(&si);
2003 #if BITS_PER_LONG == 32
2004                 scst_max_cmd_mem = min(
2005                         (((uint64_t)(si.totalram - si.totalhigh) << PAGE_SHIFT)
2006                                 >> 20) >> 2, (uint64_t)1 << 30);
2007 #else
2008                 scst_max_cmd_mem = (((si.totalram - si.totalhigh) << PAGE_SHIFT)
2009                                         >> 20) >> 2;
2010 #endif
2011         }
2012
2013         if (scst_max_dev_cmd_mem != 0) {
2014                 if (scst_max_dev_cmd_mem > scst_max_cmd_mem) {
2015                         PRINT_ERROR("scst_max_dev_cmd_mem (%d) > "
2016                                 "scst_max_cmd_mem (%d)",
2017                                 scst_max_dev_cmd_mem,
2018                                 scst_max_cmd_mem);
2019                         scst_max_dev_cmd_mem = scst_max_cmd_mem;
2020                 }
2021         } else
2022                 scst_max_dev_cmd_mem = scst_max_cmd_mem * 2 / 5;
2023
2024         res = scst_sgv_pools_init(
2025                 ((uint64_t)scst_max_cmd_mem << 10) >> (PAGE_SHIFT - 10), 0);
2026         if (res != 0)
2027                 goto out_sysfs_cleanup;
2028
2029 #ifdef CONFIG_SCST_PROC
2030         scst_default_acg = scst_alloc_add_acg(SCST_DEFAULT_ACG_NAME);
2031         if (scst_default_acg == NULL) {
2032                 res = -ENOMEM;
2033                 goto out_destroy_sgv_pool;
2034         }
2035 #endif
2036
2037         res = scsi_register_interface(&scst_interface);
2038         if (res != 0)
2039 #ifdef CONFIG_SCST_PROC
2040                 goto out_free_acg;
2041 #else
2042                 goto out_destroy_sgv_pool;
2043 #endif
2044
2045         for (i = 0; i < (int)ARRAY_SIZE(scst_tasklets); i++) {
2046                 spin_lock_init(&scst_tasklets[i].tasklet_lock);
2047                 INIT_LIST_HEAD(&scst_tasklets[i].tasklet_cmd_list);
2048                 tasklet_init(&scst_tasklets[i].tasklet,
2049                              (void *)scst_cmd_tasklet,
2050                              (unsigned long)&scst_tasklets[i]);
2051         }
2052
2053         TRACE_DBG("%d CPUs found, starting %d threads", scst_num_cpus,
2054                 scst_threads);
2055
2056         res = scst_start_all_threads(scst_threads);
2057         if (res < 0)
2058                 goto out_thread_free;
2059
2060 #ifdef CONFIG_SCST_PROC
2061         res = scst_proc_init_module();
2062         if (res != 0)
2063                 goto out_thread_free;
2064 #endif
2065
2066         PRINT_INFO("SCST version %s loaded successfully (max mem for "
2067                 "commands %dMB, per device %dMB)", SCST_VERSION_STRING,
2068                 scst_max_cmd_mem, scst_max_dev_cmd_mem);
2069
2070         scst_print_config();
2071
2072 out:
2073         TRACE_EXIT_RES(res);
2074         return res;
2075
2076 out_thread_free:
2077         scst_stop_all_threads();
2078
2079         scsi_unregister_interface(&scst_interface);
2080
2081 #ifdef CONFIG_SCST_PROC
2082 out_free_acg:
2083         scst_destroy_acg(scst_default_acg);
2084 #endif
2085
2086 out_destroy_sgv_pool:
2087         scst_sgv_pools_deinit();
2088
2089 out_sysfs_cleanup:
2090         scst_sysfs_cleanup();
2091
2092 out_destroy_aen_mempool:
2093         mempool_destroy(scst_aen_mempool);
2094
2095 out_destroy_sense_mempool:
2096         mempool_destroy(scst_sense_mempool);
2097
2098 out_destroy_ua_mempool:
2099         mempool_destroy(scst_ua_mempool);
2100
2101 out_destroy_mgmt_stub_mempool:
2102         mempool_destroy(scst_mgmt_stub_mempool);
2103
2104 out_destroy_mgmt_mempool:
2105         mempool_destroy(scst_mgmt_mempool);
2106
2107 out_destroy_acg_cache:
2108         kmem_cache_destroy(scst_acgd_cachep);
2109
2110 out_destroy_tgt_cache:
2111         kmem_cache_destroy(scst_tgtd_cachep);
2112
2113 out_destroy_sess_cache:
2114         kmem_cache_destroy(scst_sess_cachep);
2115
2116 out_destroy_cmd_cache:
2117         kmem_cache_destroy(scst_cmd_cachep);
2118
2119 out_destroy_aen_cache:
2120         kmem_cache_destroy(scst_aen_cachep);
2121
2122 out_destroy_sense_cache:
2123         kmem_cache_destroy(scst_sense_cachep);
2124
2125 out_destroy_ua_cache:
2126         kmem_cache_destroy(scst_ua_cachep);
2127
2128 out_destroy_mgmt_stub_cache:
2129         kmem_cache_destroy(scst_mgmt_stub_cachep);
2130
2131 out_destroy_mgmt_cache:
2132         kmem_cache_destroy(scst_mgmt_cachep);
2133
2134 out_lib_exit:
2135         scst_lib_exit();
2136         goto out;
2137 }
2138
2139 static void __exit exit_scst(void)
2140 {
2141         TRACE_ENTRY();
2142
2143         /* ToDo: unregister_cpu_notifier() */
2144
2145 #ifdef CONFIG_SCST_PROC
2146         scst_proc_cleanup_module();
2147 #endif
2148         scst_sysfs_cleanup();
2149
2150         scst_stop_all_threads();
2151
2152         scsi_unregister_interface(&scst_interface);
2153 #ifdef CONFIG_SCST_PROC
2154         scst_destroy_acg(scst_default_acg);
2155 #endif
2156
2157         scst_sgv_pools_deinit();
2158
2159 #define DEINIT_CACHEP(p) do {           \
2160                 kmem_cache_destroy(p);  \
2161                 p = NULL;               \
2162         } while (0)
2163
2164         mempool_destroy(scst_mgmt_mempool);
2165         mempool_destroy(scst_mgmt_stub_mempool);
2166         mempool_destroy(scst_ua_mempool);
2167         mempool_destroy(scst_sense_mempool);
2168         mempool_destroy(scst_aen_mempool);
2169
2170         DEINIT_CACHEP(scst_mgmt_cachep);
2171         DEINIT_CACHEP(scst_mgmt_stub_cachep);
2172         DEINIT_CACHEP(scst_ua_cachep);
2173         DEINIT_CACHEP(scst_sense_cachep);
2174         DEINIT_CACHEP(scst_aen_cachep);
2175         DEINIT_CACHEP(scst_cmd_cachep);
2176         DEINIT_CACHEP(scst_sess_cachep);
2177         DEINIT_CACHEP(scst_tgtd_cachep);
2178         DEINIT_CACHEP(scst_acgd_cachep);
2179
2180         scst_lib_exit();
2181
2182         PRINT_INFO("%s", "SCST unloaded");
2183
2184         TRACE_EXIT();
2185         return;
2186 }
2187
2188
2189 module_init(init_scst);
2190 module_exit(exit_scst);
2191
2192 MODULE_AUTHOR("Vladislav Bolkhovitin");
2193 MODULE_LICENSE("GPL");
2194 MODULE_DESCRIPTION("SCSI target core");
2195 MODULE_VERSION(SCST_VERSION_STRING);