More intelligent IO flow control implemented
[mirror/scst/.git] / scst / src / scst_lib.c
1 /*
2  *  scst_lib.c
3  *  
4  *  Copyright (C) 2004-2006 Vladislav Bolkhovitin <vst@vlnb.net>
5  *                 and Leonid Stoljar
6  *  
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
10  *  of the License.
11  * 
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.
16  */
17
18 #include <linux/init.h>
19 #include <linux/kernel.h>
20 #include <linux/errno.h>
21 #include <linux/list.h>
22 #include <linux/spinlock.h>
23 #include <linux/slab.h>
24 #include <linux/sched.h>
25 #include <asm/unistd.h>
26 #include <asm/string.h>
27
28 #ifdef SCST_HIGHMEM
29 #include <linux/highmem.h>
30 #endif
31
32 #include "scst_debug.h"
33 #include "scsi_tgt.h"
34 #include "scst_priv.h"
35 #include "scst_mem.h"
36
37 #include "scst_cdbprobe.h"
38
39 static void scst_free_tgt_dev(struct scst_tgt_dev *tgt_dev);
40
41 void scst_set_cmd_error_status(struct scst_cmd *cmd, int status)
42 {
43         TRACE_ENTRY();
44
45         cmd->status = status;
46         cmd->masked_status = status >> 1;
47         cmd->host_status = DID_OK;
48
49         cmd->data_direction = SCST_DATA_NONE;
50         cmd->tgt_resp_flags = SCST_TSC_FLAG_STATUS;
51         cmd->resp_data_len = 0;
52
53         TRACE_EXIT();
54         return;
55 }
56
57 void scst_set_cmd_error(struct scst_cmd *cmd, int key, int asc, int ascq)
58 {
59         TRACE_ENTRY();
60
61         scst_set_cmd_error_status(cmd, SAM_STAT_CHECK_CONDITION);
62         scst_set_sense(cmd->sense_buffer, sizeof(cmd->sense_buffer),
63                 key, asc, ascq);
64         TRACE_BUFFER("Sense set", cmd->sense_buffer, sizeof(cmd->sense_buffer));
65
66         TRACE_EXIT();
67         return;
68 }
69
70 void scst_set_cmd_error_sense(struct scst_cmd *cmd, uint8_t *sense, 
71         unsigned int len)
72 {
73         TRACE_ENTRY();
74
75         scst_set_cmd_error_status(cmd, SAM_STAT_CHECK_CONDITION);
76
77         memset(cmd->sense_buffer, 0, sizeof(cmd->sense_buffer));
78         memcpy(cmd->sense_buffer, sense, min((unsigned long)len, 
79                 (unsigned long)sizeof(cmd->sense_buffer)));
80         TRACE_BUFFER("Sense set", cmd->sense_buffer, sizeof(cmd->sense_buffer));
81
82         TRACE_EXIT();
83         return;
84 }
85
86 void scst_set_busy(struct scst_cmd *cmd)
87 {
88         TRACE_ENTRY();
89
90         if ((cmd->sess->sess_cmd_count <= 1) || 
91             (cmd->sess->init_phase != SCST_SESS_IPH_READY))
92         {
93                 scst_set_cmd_error_status(cmd, SAM_STAT_BUSY);
94                 TRACE_MGMT_DBG("Sending BUSY status to initiator %s "
95                         "(cmds count %d, queue_type %x, sess->init_phase %d)",
96                         cmd->sess->initiator_name, cmd->sess->sess_cmd_count,
97                         cmd->queue_type, cmd->sess->init_phase);
98         } else {
99                 scst_set_cmd_error_status(cmd, SAM_STAT_TASK_SET_FULL);
100                 TRACE_MGMT_DBG("Sending QUEUE_FULL status to initiator %s "
101                         "(cmds count %d, queue_type %x, sess->init_phase %d)",
102                         cmd->sess->initiator_name, cmd->sess->sess_cmd_count,
103                         cmd->queue_type, cmd->sess->init_phase);
104         }
105
106         TRACE_EXIT();
107         return;
108 }
109
110 void scst_set_resp_data_len(struct scst_cmd *cmd, int resp_data_len)
111 {
112         int i, l;
113
114         TRACE_ENTRY();
115
116         scst_check_restore_sg_buff(cmd);
117         cmd->resp_data_len = resp_data_len;
118
119         if (resp_data_len == cmd->bufflen)
120                 goto out;
121
122         l = 0;
123         for(i = 0; i < cmd->sg_cnt; i++) {
124                 l += cmd->sg[i].length;
125                 if (l >= resp_data_len) {
126                         int left = resp_data_len - (l - cmd->sg[i].length);
127                         TRACE(TRACE_SG, "cmd %p (tag %d), "
128                                 "resp_data_len %d, i %d, cmd->sg[i].length %d, "
129                                 "left %d", cmd, cmd->tag, resp_data_len, i,
130                                 cmd->sg[i].length, left);
131                         cmd->orig_sg_cnt = cmd->sg_cnt;
132                         cmd->orig_sg_entry = i;
133                         cmd->orig_entry_len = cmd->sg[i].length;
134                         cmd->sg_cnt = i+1;
135                         cmd->sg[i].length = left;
136                         cmd->sg_buff_modified = 1;
137                         break;
138                 }
139         }
140
141 out:
142         TRACE_EXIT();
143         return;
144 }
145
146 struct scst_device *scst_alloc_device(int gfp_mask)
147 {
148         struct scst_device *dev;
149
150         TRACE_ENTRY();
151
152         dev = kzalloc(sizeof(*dev), gfp_mask);
153         TRACE_MEM("kzalloc() for dev (%zd): %p", sizeof(*dev), dev);
154         if (dev == NULL) {
155                 TRACE(TRACE_OUT_OF_MEM, "%s",
156                       "Allocation of scst_device failed");
157                 goto out;
158         }
159
160         spin_lock_init(&dev->dev_lock);
161         atomic_set(&dev->on_dev_count, 0);
162         INIT_LIST_HEAD(&dev->blocked_cmd_list);
163         INIT_LIST_HEAD(&dev->dev_tgt_dev_list);
164         INIT_LIST_HEAD(&dev->dev_acg_dev_list);
165         init_waitqueue_head(&dev->on_dev_waitQ);
166         dev->dev_double_ua_possible = 1;
167         dev->dev_serialized = 1;
168
169 out:
170         TRACE_EXIT_HRES(dev);
171         return dev;
172 }
173
174 void scst_free_device(struct scst_device *dev)
175 {
176         TRACE_ENTRY();
177
178 #ifdef EXTRACHECKS
179         if (!list_empty(&dev->dev_tgt_dev_list) || 
180             !list_empty(&dev->dev_acg_dev_list))
181         {
182                 PRINT_ERROR_PR("%s: dev_tgt_dev_list or dev_acg_dev_list "
183                         "is not empty!", __FUNCTION__);
184                 BUG();
185         }
186 #endif
187
188         TRACE_MEM("kfree for dev: %p", dev);
189         kfree(dev);
190
191         TRACE_EXIT();
192         return;
193 }
194
195 struct scst_acg_dev *scst_alloc_acg_dev(struct scst_acg *acg,
196         struct scst_device *dev, lun_t lun)
197 {
198         struct scst_acg_dev *res;
199
200         TRACE_ENTRY();
201         
202         res = kmem_cache_alloc(scst_acgd_cachep, GFP_KERNEL);
203         TRACE_MEM("kmem_cache_alloc() for acg_dev (%zd): %p", sizeof(*res), res);
204         if (res == NULL) {
205                 TRACE(TRACE_OUT_OF_MEM, "%s", "Allocation of scst_acg_dev failed");
206                 goto out;
207         }
208         memset(res, 0, sizeof(*res));
209         
210         res->dev = dev;
211         res->acg = acg;
212         res->lun = lun;
213         
214 out:
215         TRACE_EXIT_HRES(res);
216         return res;
217 }
218
219 /* scst_mutex supposed to be held */
220 void scst_free_acg_dev(struct scst_acg_dev *acg_dev)
221 {
222         TRACE_ENTRY();
223         
224         TRACE_DBG("Removing acg_dev %p from acg_dev_list and dev_acg_dev_list", 
225                 acg_dev);
226         list_del(&acg_dev->acg_dev_list_entry);
227         list_del(&acg_dev->dev_acg_dev_list_entry);
228         
229         TRACE_MEM("kfree for acg_dev: %p", acg_dev);
230         kmem_cache_free(scst_acgd_cachep, acg_dev);
231         
232         TRACE_EXIT();
233         return;
234 }
235
236 /* scst_mutex supposed to be held */
237 struct scst_acg *scst_alloc_add_acg(const char *acg_name)
238 {
239         struct scst_acg *acg;
240
241         TRACE_ENTRY();
242
243         acg = kzalloc(sizeof(*acg), GFP_KERNEL);
244         TRACE_MEM("kzalloc() for acg (%zd): %p", sizeof(*acg), acg);
245         if (acg == NULL) {
246                 TRACE(TRACE_OUT_OF_MEM, "%s", "Allocation of acg failed");
247                 goto out;
248         }
249
250         INIT_LIST_HEAD(&acg->acg_dev_list);
251         INIT_LIST_HEAD(&acg->acg_sess_list);
252         INIT_LIST_HEAD(&acg->acn_list);
253         acg->acg_name = acg_name;
254         
255         TRACE_DBG("Adding acg %s to scst_acg_list", acg_name);
256         list_add_tail(&acg->scst_acg_list_entry, &scst_acg_list);
257         
258 out:
259         TRACE_EXIT_HRES(acg);
260         return acg;
261 }
262
263 /* scst_mutex supposed to be held */
264 int scst_destroy_acg(struct scst_acg *acg)
265 {
266         struct scst_acn *n, *nn;
267         struct scst_acg_dev *acg_dev, *acg_dev_tmp;
268         int res = 0;
269
270         TRACE_ENTRY();
271
272         if (!list_empty(&acg->acg_sess_list)) {
273                 PRINT_ERROR_PR("%s: acg_sess_list is not empty!", __FUNCTION__);
274                 res = -EBUSY;
275                 goto out;
276         }
277
278         __scst_suspend_activity();
279
280         TRACE_DBG("Removing acg %s from scst_acg_list", acg->acg_name);
281         list_del(&acg->scst_acg_list_entry);
282         
283         /* Freeing acg_devs */
284         list_for_each_entry_safe(acg_dev, acg_dev_tmp, &acg->acg_dev_list, 
285                 acg_dev_list_entry)
286         {
287                 struct scst_tgt_dev *tgt_dev, *tt;
288                 list_for_each_entry_safe(tgt_dev, tt,
289                          &acg_dev->dev->dev_tgt_dev_list,
290                          dev_tgt_dev_list_entry)
291                 {
292                         if (tgt_dev->acg_dev == acg_dev)
293                                 scst_free_tgt_dev(tgt_dev);
294                 }
295                 scst_free_acg_dev(acg_dev);
296         }
297
298         __scst_resume_activity();
299
300         /* Freeing names */
301         list_for_each_entry_safe(n, nn, &acg->acn_list, 
302                 acn_list_entry)
303         {
304                 list_del(&n->acn_list_entry);
305                 TRACE_MEM("kfree() for scst_acn->name: %p", n->name);
306                 kfree(n->name);
307                 TRACE_MEM("kfree() for scst_acn: %p", n);
308                 kfree(n);
309         }
310         INIT_LIST_HEAD(&acg->acn_list);
311
312         TRACE_MEM("kfree for acg: %p", acg);
313         kfree(acg);
314
315 out:
316         TRACE_EXIT_RES(res);
317         return res;
318 }
319
320 /*
321  * No spin locks supposed to be held, scst_mutex - held.
322  * The activity is suspended.
323  */
324 static struct scst_tgt_dev *scst_alloc_add_tgt_dev(struct scst_session *sess,
325         struct scst_acg_dev *acg_dev)
326 {
327         struct scst_tgt_dev *tgt_dev;
328         struct scst_device *dev = acg_dev->dev;
329         int res;
330
331         TRACE_ENTRY();
332
333         tgt_dev = kmem_cache_alloc(scst_tgtd_cachep, GFP_KERNEL);
334         TRACE_MEM("kmem_cache_alloc(GFP_KERNEL) for tgt_dev (%zd): %p",
335               sizeof(*tgt_dev), tgt_dev);
336         if (tgt_dev != NULL)
337                 memset(tgt_dev, 0, sizeof(*tgt_dev));
338         else {
339                 TRACE(TRACE_OUT_OF_MEM, "%s",
340                       "Allocation of scst_tgt_dev failed");
341                 goto out;
342         }
343
344         tgt_dev->acg_dev = acg_dev;
345         tgt_dev->sess = sess;
346         tgt_dev->cmd_count = 0;
347
348         if (dev->scsi_dev != NULL) {
349                 TRACE(TRACE_DEBUG, "host=%d, channel=%d, id=%d, lun=%d, "
350                       "SCST lun=%Ld", dev->scsi_dev->host->host_no, 
351                       dev->scsi_dev->channel, dev->scsi_dev->id, 
352                       dev->scsi_dev->lun, (uint64_t)tgt_dev->acg_dev->lun);
353         }
354         else {
355                 TRACE(TRACE_MINOR, "Virtual device SCST lun=%Ld", 
356                       (uint64_t)tgt_dev->acg_dev->lun);
357         }
358
359         spin_lock_init(&tgt_dev->tgt_dev_lock);
360         INIT_LIST_HEAD(&tgt_dev->UA_list);
361         spin_lock_init(&tgt_dev->sn_lock);
362         INIT_LIST_HEAD(&tgt_dev->deferred_cmd_list);
363         INIT_LIST_HEAD(&tgt_dev->skipped_sn_list);
364
365         spin_lock_bh(&scst_temp_UA_lock);
366         scst_set_sense(scst_temp_UA, sizeof(scst_temp_UA),
367                 SCST_LOAD_SENSE(scst_sense_reset_UA));
368         scst_alloc_set_UA(tgt_dev, scst_temp_UA, sizeof(scst_temp_UA));
369         spin_unlock_bh(&scst_temp_UA_lock);
370
371         tm_dbg_init_tgt_dev(tgt_dev, acg_dev);
372
373         if (dev->handler && dev->handler->attach_tgt) {
374                 TRACE_DBG("Calling dev handler's attach_tgt(%p)",
375                       tgt_dev);
376                 res = dev->handler->attach_tgt(tgt_dev);
377                 TRACE_DBG("%s", "Dev handler's attach_tgt() returned");
378                 if (res != 0) {
379                         PRINT_ERROR_PR("Device handler's %s attach_tgt() "
380                             "failed: %d", dev->handler->name, res);
381                         goto out_free;
382                 }
383         }
384         
385         list_add_tail(&tgt_dev->dev_tgt_dev_list_entry, &dev->dev_tgt_dev_list);
386         if (dev->dev_reserved)
387                 __set_bit(SCST_TGT_DEV_RESERVED, &tgt_dev->tgt_dev_flags);
388
389         list_add_tail(&tgt_dev->sess_tgt_dev_list_entry,
390                 &sess->sess_tgt_dev_list);
391
392 out:
393         TRACE_EXIT();
394         return tgt_dev;
395
396 out_free:
397         TRACE_MEM("kfree for tgt_dev: %p", tgt_dev);
398         kmem_cache_free(scst_tgtd_cachep, tgt_dev);
399         tgt_dev = NULL;
400         goto out;
401 }
402
403 static void scst_send_release(struct scst_tgt_dev *tgt_dev);
404
405 /* 
406  * No locks supposed to be held, scst_mutex - held.
407  * The activity is suspended.
408  */
409 void scst_reset_tgt_dev(struct scst_tgt_dev *tgt_dev, int nexus_loss)
410 {
411         struct scst_device *dev = tgt_dev->acg_dev->dev;
412
413         if (dev->dev_reserved &&
414             !test_bit(SCST_TGT_DEV_RESERVED, &tgt_dev->tgt_dev_flags)) 
415         {
416                 /* This is one who holds the reservation */
417                 struct scst_tgt_dev *tgt_dev_tmp;
418                 list_for_each_entry(tgt_dev_tmp, &dev->dev_tgt_dev_list,
419                                     dev_tgt_dev_list_entry) 
420                 {
421                         clear_bit(SCST_TGT_DEV_RESERVED,
422                                     &tgt_dev_tmp->tgt_dev_flags);
423                 }
424                 dev->dev_reserved = 0;
425
426                 scst_send_release(tgt_dev);
427         }
428
429         spin_lock_bh(&scst_temp_UA_lock);
430         if (nexus_loss) {
431                 scst_set_sense(scst_temp_UA, sizeof(scst_temp_UA),
432                         SCST_LOAD_SENSE(scst_sense_nexus_loss_UA));
433         } else {
434                 scst_set_sense(scst_temp_UA, sizeof(scst_temp_UA),
435                         SCST_LOAD_SENSE(scst_sense_reset_UA));
436         }
437         scst_check_set_UA(tgt_dev, scst_temp_UA, sizeof(scst_temp_UA));
438         spin_unlock_bh(&scst_temp_UA_lock);
439 }
440
441 /* 
442  * No locks supposed to be held, scst_mutex - held.
443  * The activity is suspended.
444  */
445 static void scst_free_tgt_dev(struct scst_tgt_dev *tgt_dev)
446 {
447         struct scst_device *dev = tgt_dev->acg_dev->dev;
448
449         TRACE_ENTRY();
450
451         tm_dbg_deinit_tgt_dev(tgt_dev);
452
453         list_del(&tgt_dev->dev_tgt_dev_list_entry);
454         list_del(&tgt_dev->sess_tgt_dev_list_entry);
455
456         scst_reset_tgt_dev(tgt_dev, 0);
457         scst_free_all_UA(tgt_dev);
458
459         if (dev->handler && dev->handler->detach_tgt) {
460                 TRACE_DBG("Calling dev handler's detach_tgt(%p)",
461                       tgt_dev);
462                 dev->handler->detach_tgt(tgt_dev);
463                 TRACE_DBG("%s", "Dev handler's detach_tgt() returned");
464         }
465
466         TRACE_MEM("kfree for tgt_dev: %p", tgt_dev);
467         kmem_cache_free(scst_tgtd_cachep, tgt_dev);
468
469         TRACE_EXIT();
470         return;
471 }
472
473 /* scst_mutex supposed to be held */
474 int scst_sess_alloc_tgt_devs(struct scst_session *sess)
475 {
476         int res = 0;
477         struct scst_acg_dev *acg_dev;
478         struct scst_tgt_dev *tgt_dev;
479
480         TRACE_ENTRY();
481
482         __scst_suspend_activity();
483
484         INIT_LIST_HEAD(&sess->sess_tgt_dev_list);
485         list_for_each_entry(acg_dev, &sess->acg->acg_dev_list, 
486                 acg_dev_list_entry)
487         {
488                 tgt_dev = scst_alloc_add_tgt_dev(sess, acg_dev);
489                 if (tgt_dev == NULL) {
490                         res = -ENOMEM;
491                         goto out_free;
492                 }
493         }
494
495 out_resume:
496         __scst_resume_activity();
497
498         TRACE_EXIT();
499         return res;
500
501 out_free:
502         scst_sess_free_tgt_devs(sess);
503         goto out_resume;
504 }
505
506 /* scst_mutex supposed to be held and activity suspended */
507 void scst_sess_free_tgt_devs(struct scst_session *sess)
508 {
509         struct scst_tgt_dev *tgt_dev, *t;
510
511         TRACE_ENTRY();
512         
513         /* The session is going down, no users, so no locks */
514         list_for_each_entry_safe(tgt_dev, t, &sess->sess_tgt_dev_list,
515                                  sess_tgt_dev_list_entry) 
516         {
517                 scst_free_tgt_dev(tgt_dev);
518         }
519         INIT_LIST_HEAD(&sess->sess_tgt_dev_list);
520
521         TRACE_EXIT();
522         return;
523 }
524
525 /* scst_mutex supposed to be held */
526 int scst_acg_add_dev(struct scst_acg *acg, struct scst_device *dev, lun_t lun,
527         int read_only)
528 {
529         int res = 0;
530         struct scst_acg_dev *acg_dev;
531         struct scst_tgt_dev *tgt_dev;
532         struct scst_session *sess;
533         LIST_HEAD(tmp_tgt_dev_list);
534         
535         TRACE_ENTRY();
536         
537         INIT_LIST_HEAD(&tmp_tgt_dev_list);
538         
539 #ifdef EXTRACHECKS
540         list_for_each_entry(acg_dev, &acg->acg_dev_list, acg_dev_list_entry) {
541                 if (acg_dev->dev == dev) {
542                         PRINT_ERROR_PR("Device is already in group %s", 
543                                 acg->acg_name);
544                         res = -EINVAL;
545                         goto out;
546                 }
547         }
548 #endif
549         
550         acg_dev = scst_alloc_acg_dev(acg, dev, lun);
551         if (acg_dev == NULL) {
552                 res = -ENOMEM;
553                 goto out;
554         }
555         acg_dev->rd_only_flag = read_only;
556
557         __scst_suspend_activity();
558
559         TRACE_DBG("Adding acg_dev %p to acg_dev_list and dev_acg_dev_list", 
560                 acg_dev);
561         list_add_tail(&acg_dev->acg_dev_list_entry, &acg->acg_dev_list);
562         list_add_tail(&acg_dev->dev_acg_dev_list_entry, &dev->dev_acg_dev_list);
563         
564         list_for_each_entry(sess, &acg->acg_sess_list, acg_sess_list_entry) 
565         {
566                 tgt_dev = scst_alloc_add_tgt_dev(sess, acg_dev);
567                 if (tgt_dev == NULL) {
568                         res = -ENOMEM;
569                         goto out_free;
570                 }
571                 list_add_tail(&tgt_dev->extra_tgt_dev_list_entry,
572                               &tmp_tgt_dev_list);
573         }
574
575 out_resume:
576         __scst_resume_activity();
577
578 out:
579         TRACE_EXIT_RES(res);
580         return res;
581
582 out_free:
583         list_for_each_entry(tgt_dev, &tmp_tgt_dev_list,
584                          extra_tgt_dev_list_entry) 
585         {
586                 scst_free_tgt_dev(tgt_dev);
587         }
588         scst_free_acg_dev(acg_dev);
589         goto out_resume;
590 }
591
592 /* scst_mutex supposed to be held */
593 int scst_acg_remove_dev(struct scst_acg *acg, struct scst_device *dev)
594 {
595         int res = 0;
596         struct scst_acg_dev *acg_dev = NULL, *a;
597         struct scst_tgt_dev *tgt_dev, *tt;
598         
599         TRACE_ENTRY();
600         
601         list_for_each_entry(a, &acg->acg_dev_list, acg_dev_list_entry) {
602                 if (a->dev == dev) {
603                         acg_dev = a;
604                         break;
605                 }
606         }
607         
608         if (acg_dev == NULL) {
609                 PRINT_ERROR_PR("Device is not found in group %s", acg->acg_name);
610                 res = -EINVAL;
611                 goto out;
612         }
613
614         __scst_suspend_activity();
615
616         list_for_each_entry_safe(tgt_dev, tt, &dev->dev_tgt_dev_list,
617                  dev_tgt_dev_list_entry) 
618         {
619                 if (tgt_dev->acg_dev == acg_dev)
620                         scst_free_tgt_dev(tgt_dev);
621         }
622         scst_free_acg_dev(acg_dev);
623
624         __scst_resume_activity();
625
626 out:    
627         TRACE_EXIT_RES(res);
628         return res;
629 }
630
631 /* scst_mutex supposed to be held */
632 int scst_acg_add_name(struct scst_acg *acg, const char *name)
633 {
634         int res = 0;
635         struct scst_acn *n;
636         int len;
637         char *nm;
638         
639         TRACE_ENTRY();
640
641         list_for_each_entry(n, &acg->acn_list, acn_list_entry) 
642         {
643                 if (strcmp(n->name, name) == 0) {
644                         PRINT_ERROR_PR("Name %s already exists in access "
645                                 "control group %s", name, acg->acg_name);
646                         res = -EINVAL;
647                         goto out;
648                 }
649         }
650         
651         n = kmalloc(sizeof(*n), GFP_KERNEL);
652         TRACE_MEM("kmalloc(GFP_KERNEL) for scst_acn (%zd): %p", sizeof(*n), n);
653         if (n == NULL) {
654                 PRINT_ERROR_PR("%s", "Unable to allocate scst_acn");
655                 res = -ENOMEM;
656                 goto out;
657         }
658         
659         len = strlen(name);
660         nm = kmalloc(len + 1, GFP_KERNEL);
661         TRACE_MEM("kmalloc(GFP_KERNEL) for scst_acn->name (%d): %p",
662                   len + 1, nm);
663         if (nm == NULL) {
664                 PRINT_ERROR_PR("%s", "Unable to allocate scst_acn->name");
665                 res = -ENOMEM;
666                 goto out_free;
667         }
668         
669         strcpy(nm, name);
670         n->name = nm;
671         
672         list_add_tail(&n->acn_list_entry, &acg->acn_list);
673
674 out:
675         TRACE_EXIT_RES(res);
676         return res;
677
678 out_free:
679         TRACE_MEM("kfree() for scst_acn: %p", n);
680         kfree(n);
681         goto out;
682 }
683
684 /* scst_mutex supposed to be held */
685 int scst_acg_remove_name(struct scst_acg *acg, const char *name)
686 {
687         int res = -EINVAL;
688         struct scst_acn *n;
689         
690         TRACE_ENTRY();
691         
692         list_for_each_entry(n, &acg->acn_list, acn_list_entry)
693         {
694                 if (strcmp(n->name, name) == 0) {
695                         list_del(&n->acn_list_entry);
696                         TRACE_MEM("kfree() for scst_acn->name: %p", n->name);
697                         kfree(n->name);
698                         TRACE_MEM("kfree() for scst_acn: %p", n);
699                         kfree(n);
700                         res = 0;
701                         break;
702                 }
703         }
704         
705         if (res != 0) {
706                 PRINT_ERROR_PR("Unable to find name %s in access control "
707                         "group %s", name, acg->acg_name);
708         }
709
710         TRACE_EXIT_RES(res);
711         return res;
712 }
713
714 static void scst_req_done(struct scsi_cmnd *scsi_cmd)
715 {
716         struct scsi_request *req;
717
718         TRACE_ENTRY();
719
720         if (scsi_cmd && (req = scsi_cmd->sc_request)) {
721                 if (req) {
722                         if (req->sr_bufflen) {
723                                 TRACE_MEM("kfree for req->sr_buffer: %p",
724                                       req->sr_buffer);
725                                 kfree(req->sr_buffer);
726                         }
727                         scsi_release_request(req);
728                 }
729         }
730
731         TRACE_EXIT();
732         return;
733 }
734
735 static void scst_send_release(struct scst_tgt_dev *tgt_dev)
736 {
737         struct scsi_request *req;
738         struct scsi_device *scsi_dev;
739         uint8_t cdb[6];
740
741         TRACE_ENTRY();
742         
743         if (tgt_dev->acg_dev->dev->scsi_dev == NULL)
744                 goto out;
745
746         scsi_dev = tgt_dev->acg_dev->dev->scsi_dev;
747
748         req = scsi_allocate_request(scsi_dev, GFP_KERNEL);
749         if (req == NULL) {
750                 PRINT_ERROR_PR("Allocation of scsi_request failed: unable "
751                             "to RELEASE device %d:%d:%d:%d",
752                             scsi_dev->host->host_no, scsi_dev->channel,
753                             scsi_dev->id, scsi_dev->lun);
754                 goto out;
755         }
756
757         memset(cdb, 0, sizeof(cdb));
758         cdb[0] = RELEASE;
759         cdb[1] = (scsi_dev->scsi_level <= SCSI_2) ?
760             ((scsi_dev->lun << 5) & 0xe0) : 0;
761         memcpy(req->sr_cmnd, cdb, sizeof(cdb));
762         req->sr_cmd_len = sizeof(cdb);
763         req->sr_data_direction = SCST_DATA_NONE;
764         req->sr_use_sg = 0;
765         req->sr_bufflen = 0;
766         req->sr_buffer = NULL;
767         req->sr_request->rq_disk = tgt_dev->acg_dev->dev->rq_disk;
768         req->sr_sense_buffer[0] = 0;
769
770         TRACE(TRACE_DEBUG | TRACE_SCSI, "Sending RELEASE req %p to SCSI "
771                 "mid-level", req);
772         scst_do_req(req, req->sr_cmnd, (void *)req->sr_buffer, req->sr_bufflen,
773                     scst_req_done, SCST_DEFAULT_TIMEOUT, 3);
774
775 out:
776         TRACE_EXIT();
777         return;
778 }
779
780 struct scst_session *scst_alloc_session(struct scst_tgt *tgt, int gfp_mask,
781         const char *initiator_name)
782 {
783         struct scst_session *sess;
784         int len;
785         char *nm;
786
787         TRACE_ENTRY();
788
789         sess = kmem_cache_alloc(scst_sess_cachep, gfp_mask);
790         TRACE_MEM("kmem_cache_alloc() for sess (%zd): %p", sizeof(*sess), sess);
791         if (sess != NULL)
792                 memset(sess, 0, sizeof(*sess));
793         else {
794                 TRACE(TRACE_OUT_OF_MEM, "%s",
795                       "Allocation of scst_session failed");
796                 goto out;
797         }
798
799         sess->init_phase = SCST_SESS_IPH_INITING;
800         atomic_set(&sess->refcnt, 0);
801         INIT_LIST_HEAD(&sess->sess_tgt_dev_list);
802         INIT_LIST_HEAD(&sess->search_cmd_list);
803         sess->tgt = tgt;
804         INIT_LIST_HEAD(&sess->init_deferred_cmd_list);
805         INIT_LIST_HEAD(&sess->init_deferred_mcmd_list);
806         
807         len = strlen(initiator_name);
808         nm = kmalloc(len + 1, gfp_mask);
809         TRACE_MEM("kmalloc(GFP_KERNEL) for sess->initiator_name (%d): %p",
810                   len + 1, nm);
811         if (nm == NULL) {
812                 PRINT_ERROR_PR("%s", "Unable to allocate sess->initiator_name");
813                 goto out_free;
814         }
815         
816         strcpy(nm, initiator_name);
817         sess->initiator_name = nm;
818         
819 out:
820         TRACE_EXIT();
821         return sess;
822
823 out_free:
824         TRACE_MEM("kfree() for sess: %p", sess);
825         kmem_cache_free(scst_sess_cachep, sess);
826         sess = NULL;
827         goto out;
828 }
829
830 void scst_free_session(struct scst_session *sess)
831 {
832         TRACE_ENTRY();
833
834         down(&scst_mutex);
835         TRACE_DBG("Removing sess %p from the list", sess);
836         list_del(&sess->sess_list_entry);
837         TRACE_DBG("Removing session %p from acg %s", sess, sess->acg->acg_name);
838         list_del(&sess->acg_sess_list_entry);
839         
840         __scst_suspend_activity();
841         scst_sess_free_tgt_devs(sess);
842         __scst_resume_activity();
843
844         wake_up_all(&sess->tgt->unreg_waitQ);
845
846         up(&scst_mutex);
847
848         TRACE_MEM("kfree for sess->initiator_name: %p", sess->initiator_name);
849         kfree(sess->initiator_name);
850
851         TRACE_MEM("kfree for sess: %p", sess);
852         kmem_cache_free(scst_sess_cachep, sess);
853
854         TRACE_EXIT();
855         return;
856 }
857
858 void scst_free_session_callback(struct scst_session *sess)
859 {
860         struct semaphore *shm;
861
862         TRACE_ENTRY();
863
864         TRACE_DBG("Freeing session %p", sess);
865
866         shm = sess->shutdown_mutex;
867
868         if (sess->unreg_done_fn) {
869                 TRACE_DBG("Calling unreg_done_fn(%p)", sess);
870                 sess->unreg_done_fn(sess);
871                 TRACE_DBG("%s", "unreg_done_fn() returned");
872         }
873         scst_free_session(sess);
874
875         if (shm)
876                 up(shm);
877
878         TRACE_EXIT();
879         return;
880 }
881
882 void scst_sched_session_free(struct scst_session *sess)
883 {
884         unsigned long flags;
885
886         TRACE_ENTRY();
887
888         spin_lock_irqsave(&scst_mgmt_lock, flags);
889         TRACE_DBG("Adding sess %p to scst_sess_mgmt_list", sess);
890         list_add_tail(&sess->sess_mgmt_list_entry, &scst_sess_mgmt_list);
891         spin_unlock_irqrestore(&scst_mgmt_lock, flags);
892         
893         wake_up(&scst_mgmt_waitQ);
894
895         TRACE_EXIT();
896         return;
897 }
898
899 struct scst_cmd *scst_alloc_cmd(int gfp_mask)
900 {
901         struct scst_cmd *cmd;
902
903         TRACE_ENTRY();
904
905         cmd = kmem_cache_alloc(scst_cmd_cachep, gfp_mask);
906         TRACE_MEM("kmem_cache_alloc() for cmd (%zd): %p", sizeof(*cmd), cmd);
907         if (cmd != NULL)
908                 memset(cmd, 0, sizeof(*cmd));
909         else {
910                 TRACE(TRACE_OUT_OF_MEM, "%s", "Allocation of scst_cmd failed");
911                 goto out;
912         }
913
914         cmd->queue_type = SCST_CMD_QUEUE_UNTAGGED;
915         cmd->timeout = SCST_DEFAULT_TIMEOUT;
916         cmd->retries = SCST_DEFAULT_RETRIES;
917         cmd->data_len = -1;
918         cmd->tgt_resp_flags = SCST_TSC_FLAG_STATUS;
919         cmd->resp_data_len = -1;
920
921 out:
922         TRACE_EXIT();
923         return cmd;
924 }
925
926 static void scst_destroy_put_cmd(struct scst_cmd *cmd)
927 {
928         scst_sess_put(cmd->sess);
929
930         /* At this point tgt_dev can be dead, but the pointer remains not-NULL */
931         if (likely(cmd->tgt_dev != NULL))
932                 scst_dec_cmd_count();
933
934         scst_destroy_cmd(cmd);
935         return;
936 }
937
938 /* No locks supposed to be held. Must be called only from scst_finish_cmd()! */
939 void scst_free_cmd(struct scst_cmd *cmd)
940 {
941         int destroy = 1;
942
943         TRACE_ENTRY();
944
945         BUG_ON(cmd->blocking);
946
947 #ifdef EXTRACHECKS
948         if (cmd->scsi_req) {
949                 PRINT_ERROR_PR("%s: %s", __FUNCTION__, "Cmd with unfreed "
950                         "scsi_req!");
951                 scst_release_request(cmd);
952         }
953 #endif
954
955         TRACE_DBG("Calling target's on_free_cmd(%p)", cmd);
956         cmd->tgtt->on_free_cmd(cmd);
957         TRACE_DBG("%s", "Target's on_free_cmd() returned");
958
959         if (likely(cmd->dev != NULL)) {
960                 struct scst_dev_type *handler = cmd->dev->handler;
961                 if (handler->on_free_cmd != NULL) {
962                         TRACE_DBG("Calling dev handler %s on_free_cmd(%p)",
963                               handler->name, cmd);
964                         handler->on_free_cmd(cmd);
965                         TRACE_DBG("Dev handler %s on_free_cmd() returned",
966                                 handler->name);
967                 }
968         }
969
970         scst_release_space(cmd);
971
972         if (likely(cmd->tgt_dev != NULL)) {
973 #ifdef EXTRACHECKS
974                 if (cmd->sent_to_midlev == 0) {
975                         PRINT_ERROR_PR("Finishing not executed cmd (opcode %d, "
976                              "target %s, lun %Ld, sn %d, expected_sn %d)", 
977                              cmd->cdb[0], cmd->tgtt->name, (uint64_t)cmd->lun, 
978                              cmd->sn, cmd->tgt_dev->expected_sn);
979                         scst_inc_expected_sn_unblock(cmd->tgt_dev, cmd, 0);
980                 }
981 #endif
982                 if (unlikely(test_bit(SCST_CMD_OUT_OF_SN, 
983                                 &cmd->cmd_flags)))
984                 {
985                         spin_lock_bh(&cmd->tgt_dev->sn_lock);
986                         set_bit(SCST_CMD_CAN_BE_DESTROYED, 
987                                 &cmd->cmd_flags);
988                         barrier(); /* to reread SCST_CMD_OUT_OF_SN */
989                         destroy = !test_bit(SCST_CMD_OUT_OF_SN, 
990                                         &cmd->cmd_flags);
991                         TRACE(TRACE_SCSI_SERIALIZING, "Out of SN "
992                                 "cmd %p (tag %d, sn %d), destroy=%d", cmd,
993                                 cmd->tag, cmd->sn, destroy);
994                         spin_unlock_bh(&cmd->tgt_dev->sn_lock);
995                 }
996         }
997
998         if (likely(destroy))
999                 scst_destroy_put_cmd(cmd);
1000
1001         TRACE_EXIT();
1002         return;
1003 }
1004
1005 /* No locks supposed to be held. */
1006 void scst_check_retries(struct scst_tgt *tgt, int processible_env)
1007 {
1008         int need_wake_up = 0;
1009
1010         TRACE_ENTRY();
1011
1012         /* 
1013          * We don't worry about overflow of finished_cmds, because we check 
1014          * only for its change 
1015          */
1016         atomic_inc(&tgt->finished_cmds);
1017         smp_mb__after_atomic_inc();
1018         if (unlikely(tgt->retry_cmds > 0)) 
1019         {
1020                 struct scst_cmd *c, *tc;
1021                 unsigned long flags;
1022
1023                 TRACE(TRACE_RETRY, "Checking retry cmd list (retry_cmds %d)",
1024                       tgt->retry_cmds);
1025
1026                 spin_lock_irqsave(&tgt->tgt_lock, flags);
1027                 spin_lock(&scst_list_lock);
1028
1029                 list_for_each_entry_safe(c, tc, &tgt->retry_cmd_list,
1030                                 cmd_list_entry)
1031                 {
1032                         tgt->retry_cmds--;
1033
1034                         TRACE(TRACE_RETRY, "Moving retry cmd %p to active cmd "
1035                             "list (retry_cmds left %d)", c, tgt->retry_cmds);
1036                         list_move(&c->cmd_list_entry, &scst_active_cmd_list);
1037
1038                         need_wake_up++;
1039                         if (need_wake_up >= 2) /* "slow start" */
1040                                 break; 
1041                 }
1042
1043                 spin_unlock(&scst_list_lock);
1044                 spin_unlock_irqrestore(&tgt->tgt_lock, flags);
1045         }
1046
1047         if (need_wake_up && !processible_env)
1048                 wake_up(&scst_list_waitQ);
1049
1050         TRACE_EXIT();
1051         return;
1052 }
1053
1054 void scst_tgt_retry_timer_fn(unsigned long arg)
1055 {
1056         struct scst_tgt *tgt = (struct scst_tgt*)arg;
1057         unsigned long flags;
1058
1059         TRACE(TRACE_RETRY, "Retry timer expired (retry_cmds %d)",
1060                 tgt->retry_cmds);
1061
1062         spin_lock_irqsave(&tgt->tgt_lock, flags);
1063         tgt->retry_timer_active = 0;
1064         spin_unlock_irqrestore(&tgt->tgt_lock, flags);
1065
1066         scst_check_retries(tgt, 0);
1067
1068         TRACE_EXIT();
1069         return;
1070 }
1071
1072 struct scst_mgmt_cmd *scst_alloc_mgmt_cmd(int gfp_mask)
1073 {
1074         struct scst_mgmt_cmd *mcmd;
1075
1076         TRACE_ENTRY();
1077
1078         mcmd = mempool_alloc(scst_mgmt_mempool, gfp_mask);
1079         TRACE_MEM("mempool_alloc() for mgmt cmd (%zd): %p", sizeof(*mcmd),
1080                 mcmd);
1081         if (mcmd == NULL) {
1082                 PRINT_ERROR("%s", "Allocation of management command "
1083                         "failed, some commands and their data could leak");
1084                 goto out;
1085         }
1086         memset(mcmd, 0, sizeof(*mcmd));
1087
1088 out:
1089         TRACE_EXIT();
1090         return mcmd;
1091 }
1092
1093 void scst_free_mgmt_cmd(struct scst_mgmt_cmd *mcmd, int del)
1094 {
1095         unsigned long flags;
1096
1097         TRACE_ENTRY();
1098
1099         spin_lock_irqsave(&scst_list_lock, flags);
1100         if (del)
1101                 list_del(&mcmd->mgmt_cmd_list_entry);
1102         mcmd->sess->sess_cmd_count--;
1103         spin_unlock_irqrestore(&scst_list_lock, flags);
1104
1105         scst_sess_put(mcmd->sess);
1106
1107         if (mcmd->mcmd_tgt_dev != NULL)
1108                 scst_dec_cmd_count();
1109
1110         TRACE_MEM("mempool_free for mgmt cmd: %p", mcmd);
1111         mempool_free(mcmd, scst_mgmt_mempool);
1112
1113         TRACE_EXIT();
1114         return;
1115 }
1116
1117 int scst_alloc_request(struct scst_cmd *cmd)
1118 {
1119         int res = 0;
1120         struct scsi_request *req;
1121         int gm = scst_cmd_atomic(cmd) ? GFP_ATOMIC : GFP_KERNEL;
1122
1123         TRACE_ENTRY();
1124
1125         /* cmd->dev->scsi_dev must be non-NULL here */
1126         req = scsi_allocate_request(cmd->dev->scsi_dev, gm);
1127         if (req == NULL) {
1128                 TRACE(TRACE_OUT_OF_MEM, "%s",
1129                       "Allocation of scsi_request failed");
1130                 res = -ENOMEM;
1131                 goto out;
1132         }
1133
1134         cmd->scsi_req = req;
1135
1136         memcpy(req->sr_cmnd, cmd->cdb, cmd->cdb_len);
1137         req->sr_cmd_len = cmd->cdb_len;
1138         req->sr_data_direction = cmd->data_direction;
1139         req->sr_use_sg = cmd->sg_cnt;
1140         req->sr_bufflen = cmd->bufflen;
1141         req->sr_buffer = cmd->sg;
1142         req->sr_request->rq_disk = cmd->dev->rq_disk;
1143         req->sr_sense_buffer[0] = 0;
1144
1145         cmd->scsi_req->upper_private_data = cmd;
1146
1147 out:
1148         TRACE_EXIT();
1149         return res;
1150 }
1151
1152 void scst_release_request(struct scst_cmd *cmd)
1153 {
1154         scsi_release_request(cmd->scsi_req);
1155         cmd->scsi_req = NULL;
1156 }
1157
1158 int scst_alloc_space(struct scst_cmd *cmd)
1159 {
1160         int tgt_sg = cmd->tgt->sg_tablesize;
1161         int ini_sg;
1162         int gfp_mask;
1163         int res = -ENOMEM;
1164         int ini_unchecked_isa_dma, ini_use_clustering;
1165         struct sgv_pool *pool;
1166         struct sgv_pool_obj *sgv;
1167
1168         TRACE_ENTRY();
1169
1170         if (cmd->data_buf_alloced) {
1171                 TRACE_MEM("%s", "data_buf_alloced set, returning");
1172                 BUG_ON(cmd->sg == NULL);
1173                 res = 0;
1174                 goto out;
1175         }
1176
1177         gfp_mask = __GFP_NOWARN;
1178         gfp_mask |= (scst_cmd_atomic(cmd) ? GFP_ATOMIC : GFP_KERNEL);
1179         pool = &scst_sgv.norm;
1180
1181         if (cmd->dev->scsi_dev != NULL) {
1182                 ini_sg = cmd->dev->scsi_dev->host->sg_tablesize;
1183                 ini_unchecked_isa_dma = 
1184                         cmd->dev->scsi_dev->host->unchecked_isa_dma;
1185                 ini_use_clustering = 
1186                         (cmd->dev->scsi_dev->host->use_clustering == 
1187                                 ENABLE_CLUSTERING);
1188         }
1189         else {
1190                 ini_sg = (1 << 15) /* infinite */;
1191                 ini_unchecked_isa_dma = 0;
1192                 ini_use_clustering = 0;
1193         }
1194
1195         if (cmd->tgtt->use_clustering || ini_use_clustering)
1196         {
1197                 TRACE_MEM("%s", "Use clustering");
1198                 pool = &scst_sgv.norm_clust;
1199         }
1200
1201         if (cmd->tgtt->unchecked_isa_dma || ini_unchecked_isa_dma) {
1202                 TRACE_MEM("%s", "Use ISA DMA memory");
1203                 gfp_mask |= GFP_DMA;
1204                 pool = &scst_sgv.dma;
1205         } else {
1206 #ifdef SCST_HIGHMEM
1207                 gfp_mask |= __GFP_HIGHMEM;
1208                 pool = &scst_sgv.highmem;
1209 #endif
1210         }
1211
1212         sgv = sgv_pool_alloc(pool, cmd->bufflen, gfp_mask, &cmd->sg_cnt);
1213         if (sgv == NULL)
1214                 goto out;
1215
1216         if (unlikely(cmd->sg_cnt > ini_sg)) {
1217                 static int ll;
1218                 if (ll < 10) {
1219                         PRINT_INFO("Unable to complete command due to "
1220                                 "underlying device SG IO count limitation "
1221                                 "(requested %d, available %d)", cmd->sg_cnt,
1222                                 ini_sg);
1223                         ll++;
1224                 }
1225                 goto out_sgv_free;
1226         }
1227
1228         if (unlikely(cmd->sg_cnt > tgt_sg)) {
1229                 static int ll;
1230                 if (ll < 10) {
1231                         PRINT_INFO("Unable to complete command due to "
1232                                 "target device %s SG IO count limitation "
1233                                 "(requested %d, available %d)", cmd->tgtt->name,
1234                                 cmd->sg_cnt, tgt_sg);
1235                         ll++;
1236                 }
1237                 goto out_sgv_free;
1238         }
1239
1240         cmd->sgv = sgv;
1241         cmd->sg = sgv_pool_sg(sgv);
1242         
1243         res = 0;
1244
1245 out:
1246         TRACE_EXIT();
1247         return res;
1248
1249 out_sgv_free:
1250         sgv_pool_free(sgv);
1251         cmd->sg_cnt = 0;
1252         goto out;
1253 }
1254
1255 void scst_release_space(struct scst_cmd *cmd)
1256 {
1257         TRACE_ENTRY();
1258
1259         if (cmd->data_buf_alloced) {
1260                 TRACE_MEM("%s", "data_buf_alloced set, returning");
1261                 goto out;
1262         }
1263
1264         if (cmd->sgv) {
1265                 scst_check_restore_sg_buff(cmd);
1266                 sgv_pool_free(cmd->sgv);
1267         }
1268
1269         cmd->sgv = NULL;
1270         cmd->sg_cnt = 0;
1271         cmd->sg = NULL;
1272         cmd->bufflen = 0;
1273         cmd->data_len = 0;
1274
1275 out:
1276         TRACE_EXIT();
1277         return;
1278 }
1279
1280 int __scst_get_buf(struct scst_cmd *cmd, uint8_t **buf)
1281 {
1282         int res = 0;
1283         struct scatterlist *sg = cmd->sg;
1284         int i = cmd->get_sg_buf_entry_num;
1285         
1286         TRACE_ENTRY();
1287         
1288         *buf = NULL;
1289         
1290         if (i >= cmd->sg_cnt)
1291                 goto out;
1292 #ifdef SCST_HIGHMEM
1293         /* 
1294          * HIGHMEM pages not merged (clustered), so if it's 
1295          * not HIGHMEM page, kmap() is the same as page_address()
1296          */
1297         if (scst_cmd_atomic(cmd)) {
1298                 enum km_type km;
1299                 if (in_softirq())
1300                         km = KM_SOFTIRQ0;
1301                 else
1302                         km = KM_USER0;
1303                 *buf = kmap_atomic(sg[i].page, km);
1304         } else
1305                 *buf = kmap(sg[i].page);
1306 #else
1307         *buf = page_address(sg[i].page);
1308 #endif
1309         res = sg[i].length;
1310         cmd->get_sg_buf_entry_num++;
1311         
1312 out:
1313         TRACE_EXIT_RES(res);
1314         return res;
1315 }
1316
1317 static const int SCST_CDB_LENGTH[8] = { 6, 10, 10, -1, 16, 12, -1, -1 };
1318
1319 #define SCST_CDB_GROUP(opcode)   ((opcode >> 5) & 0x7)
1320 #define SCST_GET_CDB_LEN(opcode) SCST_CDB_LENGTH[SCST_CDB_GROUP(opcode)]
1321
1322 int scst_get_cdb_len(const uint8_t *cdb)
1323 {
1324         return SCST_GET_CDB_LEN(cdb[0]);
1325 }
1326
1327 int scst_get_cdb_info(const uint8_t *cdb_p, int dev_type,
1328                       struct scst_info_cdb *info_p)
1329 {
1330         int i, res = 0;
1331         uint8_t op;
1332         const struct scst_sdbops *ptr = NULL;
1333
1334         TRACE_ENTRY();
1335
1336         memset(info_p, 0, sizeof(*info_p));
1337         info_p->direction = SCST_DATA_NONE;
1338         info_p->op_name = "NOOP";
1339         op = *cdb_p;    /* get clear opcode */
1340
1341         TRACE(TRACE_SCSI, "opcode=%02x, cdblen=%d bytes, tblsize=%zd, "
1342                 "dev_type=%d", op, SCST_GET_CDB_LEN(op), SCST_CDB_TBL_SIZE,
1343                 dev_type);
1344
1345         i = scst_scsi_op_list[op];
1346         while (i < SCST_CDB_TBL_SIZE && scst_scsi_op_table[i].ops == op) {
1347                 if (scst_scsi_op_table[i].devkey[dev_type] != SCST_CDB_NOTSUPP) {
1348                         ptr = &scst_scsi_op_table[i];
1349 #if 0
1350                         TRACE(TRACE_SCSI, "op = 0x%02x+'%c%c%c%c%c%c%c%c%c%c'+<%s>", 
1351                               ptr->ops, ptr->devkey[0], /* disk     */
1352                               ptr->devkey[1],   /* tape     */
1353                               ptr->devkey[2],   /* printer */
1354                               ptr->devkey[3],   /* cpu      */
1355                               ptr->devkey[4],   /* cdr      */
1356                               ptr->devkey[5],   /* cdrom    */
1357                               ptr->devkey[6],   /* scanner */
1358                               ptr->devkey[7],   /* worm     */
1359                               ptr->devkey[8],   /* changer */
1360                               ptr->devkey[9],   /* commdev */
1361                               ptr->op_name);
1362
1363                         TRACE(TRACE_SCSI,
1364                               "direction=%d size_field_len=%d fixed=%d flag1=%d flag2=%d",
1365                               ptr->direction,
1366                               ptr->size_field_len,
1367                               ptr->fixed, ptr->flag1, ptr->flag2);
1368 #endif
1369                         break;
1370                 }
1371                 i++;
1372         }
1373
1374         if (ptr == NULL) {
1375                 /* opcode not found or now not used !!! */
1376                 TRACE(TRACE_SCSI, "Unknown opcode 0x%x for type %d", op,
1377                       dev_type);
1378                 res = -1;
1379                 goto out;
1380         }
1381
1382         info_p->cdb_len = SCST_GET_CDB_LEN(op);
1383         info_p->op_name = ptr->op_name;
1384         /* 1. direction */
1385         info_p->direction = ptr->direction;
1386         if (info_p->direction == SCST_DATA_NONE)
1387                 goto out;
1388
1389         /* 2. flags */
1390         info_p->flags = ptr->fixed;
1391
1392         /*
1393          * CDB length needed, because we must know offsets:
1394          * 1) for  6-bytes CDB len = 1 byte or 3 bytes(if real transfer exist)
1395          * 2) for 10-bytes CDB len = 1 byte or 2 bytes(0x24,0x25 = 3)
1396          * 3) for 12-bytes CDB len = 1 byte or 4 bytes
1397          */
1398
1399         /* 3. transfer_len */
1400         if (SCST_GET_CDB_LEN(op) == 6) {
1401                 if (ptr->size_field_len == 3) {
1402                         /* length = 3 bytes */
1403                         info_p->transfer_len = (((*(cdb_p + 2)) & 0xff) << 16) +
1404                             (((*(cdb_p + 3)) & 0xff) << 8) +
1405                             ((*(cdb_p + 4)) & 0xff);
1406                         info_p->transfer_len &= 0xffffff;
1407                 } else if (ptr->size_field_len == 1) {
1408                         /* 
1409                          * Warning!!! CDB 'READ BLOCK LIMITS'
1410                          * always returns 6-byte block with limits
1411                          * info_p->transfer_len = (int)(*(cdb_p + 4));
1412                          */
1413                         info_p->transfer_len = ((op == READ_BLOCK_LIMITS) ?
1414                                                 SCST_BLOCK_LIMIT_LEN : 
1415                                                 *(cdb_p + 4)) & 0xff;
1416                 }
1417         } else if (SCST_GET_CDB_LEN(op) == 10) {
1418                 if (ptr->size_field_len == 3)
1419                         /* 
1420                          * SET window usees 3 bytes length SET/GET WINDOW
1421                          * if ((uint8_t)ptr->ops == 0x24 || 0x25)
1422                          */
1423                 {
1424                         info_p->transfer_len = (((*(cdb_p + 6)) & 0xff) << 16) +
1425                             (((*(cdb_p + 7)) & 0xff) << 8) +
1426                             ((*(cdb_p + 8)) & 0xff);
1427                         info_p->transfer_len &= 0xffffff;
1428                 } else if (ptr->size_field_len == 2) {
1429                         info_p->transfer_len = (((*(cdb_p + 7)) & 0xff) << 8) +
1430                             ((*(cdb_p + 8)) & 0xff);
1431                         info_p->transfer_len &= 0xffff;
1432                 } else if (ptr->size_field_len == 1) {
1433                         info_p->transfer_len = (*(cdb_p + 8));
1434
1435                         /* opcode = READ-WRITE UPDATED BLOCK */
1436                         if ((ptr->ops == 0x5d) ||
1437                             (ptr->ops == UPDATE_BLOCK) ||
1438                             (ptr->ops == WRITE_SAME)) {
1439                                 /* the opcode always returns 1 block */
1440                                 info_p->flags |= SCST_TRANSFER_LEN_TYPE_FIXED;
1441                                 info_p->transfer_len = 1;
1442                         }
1443
1444                         if ((ptr->ops == COMPARE) || (ptr->ops == COPY_VERIFY)) {
1445                                 /* ese other place in CDB [3,4],5 */
1446                                 info_p->transfer_len = (*(cdb_p + 5));
1447                         }
1448
1449                         info_p->transfer_len &= 0xff;
1450                 }
1451         } else if (SCST_GET_CDB_LEN(op) == 12) {
1452                 if (ptr->size_field_len == 4) {
1453                         info_p->transfer_len = (((*(cdb_p + 6)) & 0xff) << 24) +
1454                             (((*(cdb_p + 7)) & 0xff) << 16) +
1455                             (((*(cdb_p + 8)) & 0xff) << 8) +
1456                             ((*(cdb_p + 9)) & 0xff);
1457                         info_p->transfer_len &= 0xffffffff;
1458                 } else if (ptr->size_field_len == 3) {
1459                         info_p->transfer_len = (((*(cdb_p + 7)) & 0xff) << 16) +
1460                             (((*(cdb_p + 8)) & 0xff) << 8) +
1461                             ((*(cdb_p + 9)) & 0xff);
1462                         info_p->transfer_len &= 0xffffff;
1463                 } else if (ptr->size_field_len == 2) {
1464                         info_p->transfer_len = (((*(cdb_p + 8)) & 0xff) << 8) +
1465                             ((*(cdb_p + 9)) & 0xff);
1466                         info_p->transfer_len &= 0xffff;
1467                 } else {
1468                         if (ptr->size_field_len == 1) {
1469                                 info_p->transfer_len = (*(cdb_p + 9));
1470                                 info_p->transfer_len &= 0xff;
1471                         }
1472                 }
1473         } else if (SCST_GET_CDB_LEN(op) == 16) {
1474                 if (ptr->size_field_len == 4) {
1475                         info_p->transfer_len =
1476                             (((*(cdb_p + 10)) & 0xff) << 24) +
1477                             (((*(cdb_p + 11)) & 0xff) << 16) +
1478                             (((*(cdb_p + 12)) & 0xff) << 8) +
1479                             ((*(cdb_p + 13)) & 0xff);
1480                 }
1481         }
1482         if (!info_p->transfer_len) {
1483                 TRACE(TRACE_SCSI,
1484                       "Warning! transfer_len 0, direction %d change on " "%d",
1485                       info_p->direction, SCST_DATA_NONE);
1486                 info_p->direction = SCST_DATA_NONE;
1487         }
1488
1489 out:
1490         TRACE_EXIT();
1491         return res;
1492 }
1493
1494 void scst_scsi_op_list_init(void)
1495 {
1496         int i;
1497         uint8_t op = 0xff;
1498
1499         TRACE_ENTRY();
1500
1501         for (i = 0; i < 256; i++)
1502                 scst_scsi_op_list[i] = SCST_CDB_TBL_SIZE;
1503
1504         for (i = 0; i < SCST_CDB_TBL_SIZE; i++) {
1505                 if (scst_scsi_op_table[i].ops != op) {
1506                         op = scst_scsi_op_table[i].ops;
1507                         scst_scsi_op_list[op] = i;
1508                 }
1509         }
1510
1511         TRACE_EXIT();
1512         return;
1513 }
1514
1515 /*
1516  * Routine to extract a lun number from an 8-byte LUN structure
1517  * in network byte order (BE).
1518  * (see SAM-2, Section 4.12.3 page 40)
1519  * Supports 2 types of lun unpacking: peripheral and logical unit.
1520  */
1521 lun_t scst_unpack_lun(const uint8_t *lun, int len)
1522 {
1523         lun_t res = (lun_t)-1;
1524         int address_method;
1525
1526         TRACE_ENTRY();
1527
1528         TRACE_BUFF_FLAG(TRACE_DEBUG, "Raw LUN", lun, len);
1529
1530         if (len < 2) {
1531                 PRINT_ERROR_PR("Illegal lun length %d, expected 2 bytes or "
1532                         "more", len);
1533                 goto out;
1534         }
1535
1536         if (len > 2) {
1537                 switch(len) {
1538                 case 8:
1539                 {
1540                         if ((*((uint64_t*)lun) & 
1541                           __constant_cpu_to_be64(0x0000FFFFFFFFFFFFLL)) != 0)
1542                                 goto out_err;
1543                         break;
1544                 }
1545                 case 4:
1546                         if (*((uint16_t*)&lun[2]) != 0)
1547                                 goto out_err;
1548                         break;
1549                 case 6:
1550                         if (*((uint32_t*)&lun[2]) != 0)
1551                                 goto out_err;
1552                         break;
1553                 default:
1554                         goto out_err;
1555                 }
1556         }
1557
1558         address_method = (*lun) >> 6;   /* high 2 bits of byte 0 */
1559         switch (address_method) {
1560         case 0: /* peripheral device addressing method */
1561                 if (*lun) {
1562                         PRINT_ERROR_PR("Illegal BUS INDENTIFIER in LUN "
1563                              "peripheral device addressing method 0x%02x, "
1564                              "expected 0", *lun);
1565                         break;
1566                 }
1567                 res = *(lun + 1);
1568                 break;
1569
1570         case 1: /* flat space addressing method */
1571                 res = *(lun + 1) | (((*lun) & 0x3f) << 8);
1572                 break;
1573
1574         case 2: /* logical unit addressing method */
1575                 if (*lun & 0x3f) {
1576                         PRINT_ERROR_PR("Illegal BUS NUMBER in LUN logical unit "
1577                                     "addressing method 0x%02x, expected 0",
1578                                     *lun & 0x3f);
1579                         break;
1580                 }
1581                 if (*(lun + 1) & 0xe0) {
1582                         PRINT_ERROR_PR("Illegal TARGET in LUN logical unit "
1583                                     "addressing method 0x%02x, expected 0",
1584                                     (*(lun + 1) & 0xf8) >> 5);
1585                         break;
1586                 }
1587                 res = *(lun + 1) & 0x1f;
1588                 break;
1589
1590         case 3: /* extended logical unit addressing method */
1591         default:
1592                 PRINT_ERROR_PR("Unimplemented LUN addressing method %u",
1593                             address_method);
1594                 break;
1595         }
1596
1597 out:
1598         TRACE_EXIT_RES((int)res);
1599         return res;
1600
1601 out_err:
1602         PRINT_ERROR_PR("%s", "Multi-level LUN unimplemented");
1603         goto out;
1604 }
1605
1606 /* Called under dev_lock and BH off */
1607 void scst_process_reset(struct scst_device *dev,
1608         struct scst_session *originator, struct scst_cmd *exclude_cmd,
1609         struct scst_mgmt_cmd *mcmd)
1610 {
1611         struct scst_tgt_dev *tgt_dev;
1612         struct scst_cmd *cmd, *tcmd;
1613         int wake = 0;
1614
1615         TRACE_ENTRY();
1616
1617         /* Clear RESERVE'ation, if necessary */
1618         if (dev->dev_reserved) {
1619                 list_for_each_entry(tgt_dev, &dev->dev_tgt_dev_list,
1620                                     dev_tgt_dev_list_entry) 
1621                 {
1622                         TRACE(TRACE_MGMT, "Clearing RESERVE'ation for tgt_dev "
1623                                 "lun %d", tgt_dev->acg_dev->lun);
1624                         clear_bit(SCST_TGT_DEV_RESERVED,
1625                                   &tgt_dev->tgt_dev_flags);
1626                 }
1627                 dev->dev_reserved = 0;
1628                 /*
1629                  * There is no need to send RELEASE, since the device is going
1630                  * to be resetted
1631                  */
1632         }
1633
1634         dev->dev_double_ua_possible = 1;
1635         dev->dev_serialized = 1;
1636
1637         /* BH already off */
1638         spin_lock(&scst_temp_UA_lock);
1639         scst_set_sense(scst_temp_UA, sizeof(scst_temp_UA),
1640                 SCST_LOAD_SENSE(scst_sense_reset_UA));
1641         __scst_process_UA(dev, exclude_cmd, scst_temp_UA, sizeof(scst_temp_UA),
1642                 1);
1643         spin_unlock(&scst_temp_UA_lock);
1644
1645         list_for_each_entry(tgt_dev, &dev->dev_tgt_dev_list, 
1646                 dev_tgt_dev_list_entry) 
1647         {
1648                 struct scst_session *sess = tgt_dev->sess;
1649
1650                 spin_lock_irq(&scst_list_lock);
1651
1652                 TRACE_DBG("Searching in search cmd list (sess=%p)", sess);
1653                 list_for_each_entry(cmd, &sess->search_cmd_list, 
1654                                 search_cmd_list_entry) {
1655                         if (cmd == exclude_cmd)
1656                                 continue;
1657                         if ((cmd->tgt_dev == tgt_dev) ||
1658                             ((cmd->tgt_dev == NULL) && 
1659                              (cmd->lun == tgt_dev->acg_dev->lun))) {
1660                                 scst_abort_cmd(cmd, mcmd, 
1661                                         (tgt_dev->sess != originator), 0);
1662                         }
1663                 }
1664                 spin_unlock_irq(&scst_list_lock);
1665         }
1666
1667         list_for_each_entry_safe(cmd, tcmd, &dev->blocked_cmd_list,
1668                                 blocked_cmd_list_entry) {
1669                 if (test_bit(SCST_CMD_ABORTED, &cmd->cmd_flags)) {
1670                         list_del(&cmd->blocked_cmd_list_entry);
1671                         TRACE_MGMT_DBG("Moving aborted blocked cmd %p "
1672                                 "to active cmd list", cmd);
1673                         spin_lock_irq(&scst_list_lock);
1674                         list_move_tail(&cmd->cmd_list_entry,
1675                                 &scst_active_cmd_list);
1676                         spin_unlock_irq(&scst_list_lock);
1677                         wake = 1;
1678                 }
1679         }
1680
1681         if (wake)
1682                 wake_up(&scst_list_waitQ);
1683
1684         TRACE_EXIT();
1685         return;
1686 }
1687
1688 int scst_set_pending_UA(struct scst_cmd *cmd)
1689 {
1690         int res = 0;
1691         struct scst_tgt_dev_UA *UA_entry;
1692
1693         TRACE_ENTRY();
1694
1695         TRACE(TRACE_MGMT, "Setting pending UA cmd %p", cmd);
1696
1697         spin_lock_bh(&cmd->tgt_dev->tgt_dev_lock);
1698
1699         /* UA list could be cleared behind us, so retest */
1700         if (list_empty(&cmd->tgt_dev->UA_list)) {
1701                 TRACE_DBG("%s",
1702                       "SCST_TGT_DEV_UA_PENDING set, but UA_list empty");
1703                 res = -1;
1704                 goto out_unlock;
1705         }
1706
1707         UA_entry = list_entry(cmd->tgt_dev->UA_list.next, typeof(*UA_entry),
1708                               UA_list_entry);
1709
1710         TRACE_DBG("next %p UA_entry %p",
1711               cmd->tgt_dev->UA_list.next, UA_entry);
1712
1713         scst_set_cmd_error_sense(cmd, UA_entry->UA_sense_buffer,
1714                 sizeof(UA_entry->UA_sense_buffer));
1715
1716         cmd->ua_ignore = 1;
1717
1718         list_del(&UA_entry->UA_list_entry);
1719
1720
1721         TRACE_MEM("mempool_free for UA_entry: %p", UA_entry);
1722         mempool_free(UA_entry, scst_ua_mempool);
1723
1724         if (list_empty(&cmd->tgt_dev->UA_list)) {
1725                 clear_bit(SCST_TGT_DEV_UA_PENDING,
1726                           &cmd->tgt_dev->tgt_dev_flags);
1727         }
1728
1729         spin_unlock_bh(&cmd->tgt_dev->tgt_dev_lock);
1730
1731 out:
1732         TRACE_EXIT_RES(res);
1733         return res;
1734
1735 out_unlock:
1736         spin_unlock_bh(&cmd->tgt_dev->tgt_dev_lock);
1737         goto out;
1738 }
1739
1740 /* Called under dev_lock, tgt_dev_lock and BH off */
1741 void scst_alloc_set_UA(struct scst_tgt_dev *tgt_dev,
1742         const uint8_t *sense, int sense_len)
1743 {
1744         struct scst_tgt_dev_UA *UA_entry = NULL;
1745
1746         TRACE_ENTRY();
1747
1748         UA_entry = mempool_alloc(scst_ua_mempool, GFP_ATOMIC);
1749         TRACE_MEM("mempool_alloc(GFP_ATOMIC) for UA_entry (%zd): %p",
1750                 sizeof(*UA_entry), UA_entry);
1751         if (UA_entry == NULL) {
1752                 PRINT_ERROR_PR("%s", "UNIT ATTENTION memory "
1753                      "allocation failed. The UNIT ATTENTION "
1754                      "on some sessions will be missed");
1755                 goto out;
1756         }
1757         memset(UA_entry, 0, sizeof(*UA_entry));
1758
1759         if (sense_len > sizeof(UA_entry->UA_sense_buffer))
1760                 sense_len = sizeof(UA_entry->UA_sense_buffer);
1761         memcpy(UA_entry->UA_sense_buffer, sense, sense_len);
1762         set_bit(SCST_TGT_DEV_UA_PENDING, &tgt_dev->tgt_dev_flags);
1763         smp_mb__after_set_bit();
1764         list_add_tail(&UA_entry->UA_list_entry, &tgt_dev->UA_list);
1765
1766 out:
1767         TRACE_EXIT();
1768         return;
1769 }
1770
1771 /* Called under dev_lock and BH off */
1772 void scst_check_set_UA(struct scst_tgt_dev *tgt_dev,
1773         const uint8_t *sense, int sense_len)
1774 {
1775         int skip_UA = 0;
1776         struct scst_tgt_dev_UA *UA_entry_tmp;
1777
1778         TRACE_ENTRY();
1779
1780         spin_lock(&tgt_dev->tgt_dev_lock);
1781
1782         list_for_each_entry(UA_entry_tmp, &tgt_dev->UA_list,
1783                             UA_list_entry) 
1784         {
1785                 if (sense[12] == UA_entry_tmp->UA_sense_buffer[12]) {
1786                         skip_UA = 1;
1787                         break;
1788                 }
1789         }
1790
1791         if (skip_UA == 0)
1792                 scst_alloc_set_UA(tgt_dev, sense, sense_len);
1793
1794         spin_unlock(&tgt_dev->tgt_dev_lock);
1795
1796         TRACE_EXIT();
1797         return;
1798 }
1799
1800 /* Called under dev_lock and BH off */
1801 void __scst_process_UA(struct scst_device *dev,
1802         struct scst_cmd *exclude, const uint8_t *sense, int sense_len,
1803         int internal)
1804 {
1805         struct scst_tgt_dev *tgt_dev, *exclude_tgt_dev = NULL;
1806
1807         TRACE_ENTRY();
1808
1809         TRACE(TRACE_MGMT, "Processing UA dev %p", dev);
1810
1811         if (exclude != NULL)
1812                 exclude_tgt_dev = exclude->tgt_dev;
1813
1814         /* Check for reset UA */
1815         if (!internal && (sense[12] == SCST_SENSE_ASC_UA_RESET)) {
1816                 scst_process_reset(dev, (exclude != NULL) ? exclude->sess : NULL,
1817                         exclude, NULL);
1818         }
1819
1820         list_for_each_entry(tgt_dev, &dev->dev_tgt_dev_list, 
1821                                 dev_tgt_dev_list_entry) {
1822                 if (tgt_dev != exclude_tgt_dev)
1823                         scst_check_set_UA(tgt_dev, sense, sense_len);
1824         }
1825
1826         TRACE_EXIT();
1827         return;
1828 }
1829
1830 /* Called under tgt_dev_lock or when tgt_dev is unused */
1831 void scst_free_all_UA(struct scst_tgt_dev *tgt_dev)
1832 {
1833         struct scst_tgt_dev_UA *UA_entry, *t;
1834
1835         TRACE_ENTRY();
1836
1837         list_for_each_entry_safe(UA_entry, t, &tgt_dev->UA_list, UA_list_entry) {
1838                 TRACE_MGMT_DBG("Clearing UA for tgt_dev lun %d", 
1839                         tgt_dev->acg_dev->lun);
1840                 list_del(&UA_entry->UA_list_entry);
1841                 TRACE_MEM("kfree for UA_entry: %p", UA_entry);
1842                 kfree(UA_entry);
1843         }
1844         INIT_LIST_HEAD(&tgt_dev->UA_list);
1845         clear_bit(SCST_TGT_DEV_UA_PENDING, &tgt_dev->tgt_dev_flags);
1846
1847         TRACE_EXIT();
1848         return;
1849 }
1850
1851 struct scst_cmd *__scst_check_deferred_commands(struct scst_tgt_dev *tgt_dev,
1852         int expected_sn)
1853 {
1854         struct scst_cmd *cmd = NULL, *tcmd;
1855
1856         if (tgt_dev->def_cmd_count == 0)
1857                 goto out;
1858
1859         spin_lock_bh(&tgt_dev->sn_lock);
1860
1861 restart:
1862         list_for_each_entry(tcmd, &tgt_dev->deferred_cmd_list,
1863                                 sn_cmd_list_entry) {
1864                 if (tcmd->sn == expected_sn) {
1865                         TRACE(TRACE_SCSI_SERIALIZING,
1866                               "Deferred command sn %d found", tcmd->sn);
1867                         tgt_dev->def_cmd_count--;
1868                         list_del(&tcmd->sn_cmd_list_entry);
1869                         cmd = tcmd;
1870                         goto out_unlock;
1871                 }
1872         }
1873
1874         list_for_each_entry(tcmd, &tgt_dev->skipped_sn_list,
1875                                 sn_cmd_list_entry) {
1876                 if (tcmd->sn == expected_sn) {
1877                         /* 
1878                          * !! At this point any pointer in tcmd, except      !!
1879                          * !! sn_cmd_list_entry, could be already destroyed  !!
1880                          */
1881                         TRACE(TRACE_SCSI_SERIALIZING,
1882                               "cmd %p (tag %d) with skipped sn %d found", tcmd,
1883                               tcmd->tag, tcmd->sn);
1884                         tgt_dev->def_cmd_count--;
1885                         list_del(&tcmd->sn_cmd_list_entry);
1886                         if (test_bit(SCST_CMD_CAN_BE_DESTROYED, 
1887                                         &tcmd->cmd_flags)) {
1888                                 scst_destroy_put_cmd(tcmd);
1889                         } else {
1890                                 smp_mb__before_clear_bit();
1891                                 clear_bit(SCST_CMD_OUT_OF_SN, &tcmd->cmd_flags);
1892                         }
1893                         expected_sn = __scst_inc_expected_sn(tgt_dev);
1894                         goto restart;
1895                 }
1896         }
1897
1898 out_unlock:
1899         spin_unlock_bh(&tgt_dev->sn_lock);
1900
1901 out:
1902         return cmd;
1903 }
1904
1905 /* No locks */
1906 int scst_inc_on_dev_cmd(struct scst_cmd *cmd)
1907 {
1908         int res = 0;
1909         struct scst_device *dev = cmd->dev;
1910
1911         BUG_ON(cmd->blocking);
1912
1913         atomic_inc(&dev->on_dev_count);
1914
1915 #ifdef STRICT_SERIALIZING
1916         spin_lock_bh(&dev->dev_lock);
1917         if (test_bit(SCST_CMD_ABORTED, &cmd->cmd_flags))
1918                 goto out_unlock;
1919         if (dev->block_count > 0) {
1920                 scst_dec_on_dev_cmd(cmd);
1921                 TRACE_MGMT_DBG("Delaying cmd %p due to blocking or serializing"
1922                       "(tag %d, dev %p)", cmd, cmd->tag, dev);
1923                 list_add_tail(&cmd->blocked_cmd_list_entry,
1924                               &dev->blocked_cmd_list);
1925                 res = 1;
1926         } else {
1927                 __scst_block_dev(cmd->dev);
1928                 cmd->blocking = 1;
1929         }
1930         spin_unlock_bh(&dev->dev_lock);
1931         goto out;
1932 #else
1933 repeat:
1934         if (unlikely(dev->block_count > 0)) {
1935                 spin_lock_bh(&dev->dev_lock);
1936                 if (test_bit(SCST_CMD_ABORTED, &cmd->cmd_flags))
1937                         goto out_unlock;
1938                 barrier(); /* to reread block_count */
1939                 if (dev->block_count > 0) {
1940                         scst_dec_on_dev_cmd(cmd);
1941                         TRACE_MGMT_DBG("Delaying cmd %p due to blocking or "
1942                                 "serializing (tag %d, dev %p)", cmd,
1943                                 cmd->tag, dev);
1944                         list_add_tail(&cmd->blocked_cmd_list_entry,
1945                                       &dev->blocked_cmd_list);
1946                         res = 1;
1947                         spin_unlock_bh(&dev->dev_lock);
1948                         goto out;
1949                 } else {
1950                         TRACE_MGMT_DBG("%s", "Somebody unblocked the device, "
1951                                 "continuing");
1952                 }
1953                 spin_unlock_bh(&dev->dev_lock);
1954         }
1955         if (unlikely(cmd->dev->dev_serialized)) {
1956                 spin_lock_bh(&dev->dev_lock);
1957                 barrier(); /* to reread block_count */
1958                 if (cmd->dev->block_count == 0) {
1959                         TRACE_MGMT_DBG("cmd %p (tag %d), blocking further "
1960                                 "cmds due to serializing (dev %p)", cmd,
1961                                 cmd->tag, dev);
1962                         __scst_block_dev(cmd->dev);
1963                         cmd->blocking = 1;
1964                 } else {
1965                         spin_unlock_bh(&dev->dev_lock);
1966                         goto repeat;
1967                 }
1968                 spin_unlock_bh(&dev->dev_lock);
1969         }
1970 #endif
1971
1972 out:
1973         return res;
1974
1975 out_unlock:
1976         spin_unlock_bh(&dev->dev_lock);
1977         goto out;
1978 }
1979
1980 /* Called under dev_lock */
1981 void scst_unblock_cmds(struct scst_device *dev)
1982 {
1983 #ifdef STRICT_SERIALIZING
1984         struct scst_cmd *cmd;
1985         int found = 0;
1986
1987         TRACE_ENTRY();
1988
1989         list_for_each_entry(cmd, &dev->blocked_cmd_list,
1990                                  blocked_cmd_list_entry) {
1991                 /* 
1992                  * Since only one cmd per time is being executed, expected_sn
1993                  * can't change behind us, if the corresponding cmd is in
1994                  * blocked_cmd_list
1995                  */
1996                 if ((cmd->tgt_dev && (cmd->sn == cmd->tgt_dev->expected_sn)) ||
1997                     (unlikely(cmd->internal) || unlikely(cmd->retry))) {
1998                         unsigned long flags;
1999                         list_del(&cmd->blocked_cmd_list_entry);
2000                         TRACE_MGMT_DBG("Moving cmd %p to active cmd list", cmd);
2001                         spin_lock_irqsave(&scst_list_lock, flags);
2002                         list_move(&cmd->cmd_list_entry, &scst_active_cmd_list);
2003                         spin_unlock_irqrestore(&scst_list_lock, flags);
2004                         wake_up(&scst_list_waitQ);
2005                         found = 1;
2006                         break;
2007                 }
2008         }
2009 #ifdef EXTRACHECKS
2010         if (!found && !list_empty(&dev->blocked_cmd_list)) {
2011                 TRACE(TRACE_MINOR, "%s", "No commands unblocked when "
2012                         "blocked cmd list is not empty");
2013         }
2014 #endif
2015 #else /* STRICT_SERIALIZING */
2016         struct scst_cmd *cmd, *tcmd;
2017         unsigned long flags;
2018
2019         TRACE_ENTRY();
2020
2021         spin_lock_irqsave(&scst_list_lock, flags);
2022         list_for_each_entry_safe(cmd, tcmd, &dev->blocked_cmd_list,
2023                                  blocked_cmd_list_entry) {
2024                 list_del(&cmd->blocked_cmd_list_entry);
2025                 TRACE_MGMT_DBG("Moving blocked cmd %p to active cmd list", cmd);
2026                 list_move_tail(&cmd->cmd_list_entry, &scst_active_cmd_list);
2027                 wake_up(&scst_list_waitQ);
2028         }
2029         spin_unlock_irqrestore(&scst_list_lock, flags);
2030 #endif /* STRICT_SERIALIZING */
2031
2032         TRACE_EXIT();
2033         return;
2034 }
2035
2036 static struct scst_cmd *scst_inc_expected_sn(
2037         struct scst_tgt_dev *tgt_dev, struct scst_cmd *out_of_sn_cmd)
2038 {
2039         struct scst_cmd *res = NULL;
2040
2041         if (out_of_sn_cmd->sn == tgt_dev->expected_sn) {
2042                 __scst_inc_expected_sn(tgt_dev);
2043         } else {
2044                 spin_lock_bh(&tgt_dev->sn_lock);
2045                 tgt_dev->def_cmd_count++;
2046                 set_bit(SCST_CMD_OUT_OF_SN, &out_of_sn_cmd->cmd_flags);
2047                 list_add_tail(&out_of_sn_cmd->sn_cmd_list_entry,
2048                               &tgt_dev->skipped_sn_list);
2049                 TRACE(TRACE_SCSI_SERIALIZING, "out_of_sn_cmd %p with sn %d "
2050                         "added to skipped_sn_list (expected_sn %d)",
2051                         out_of_sn_cmd, out_of_sn_cmd->sn, tgt_dev->expected_sn);
2052                 spin_unlock_bh(&tgt_dev->sn_lock);
2053                 smp_mb(); /* just in case, we need new value of tgt_dev->expected_sn */
2054         }
2055         res = scst_check_deferred_commands(tgt_dev, tgt_dev->expected_sn);
2056         return res;
2057 }
2058
2059 void scst_inc_expected_sn_unblock(struct scst_tgt_dev *tgt_dev,
2060         struct scst_cmd *cmd_sn, int locked)
2061 {
2062         struct scst_cmd *cmd;
2063
2064         TRACE_ENTRY();
2065
2066         cmd = scst_inc_expected_sn(tgt_dev, cmd_sn);
2067         if (cmd != NULL) {
2068                 unsigned long flags = 0;
2069                 if (!locked)
2070                         spin_lock_irqsave(&scst_list_lock, flags);
2071                 TRACE(TRACE_SCSI_SERIALIZING, "cmd %p with sn %d "
2072                         "moved to active cmd list", cmd, cmd->sn);
2073                 list_move(&cmd->cmd_list_entry, &scst_active_cmd_list);
2074                 if (!locked)
2075                         spin_unlock_irqrestore(&scst_list_lock, flags);
2076                 if (!cmd_sn->processible_env)
2077                         wake_up(&scst_list_waitQ);
2078         }
2079
2080         TRACE_EXIT();
2081         return;
2082 }
2083
2084 #ifdef DEBUG
2085 /* Original taken from the XFS code */
2086 unsigned long scst_random(void)
2087 {
2088         static int Inited;
2089         static unsigned long RandomValue;
2090         static spinlock_t lock = SPIN_LOCK_UNLOCKED;
2091         /* cycles pseudo-randomly through all values between 1 and 2^31 - 2 */
2092         register long rv;
2093         register long lo;
2094         register long hi;
2095         unsigned long flags;
2096
2097         spin_lock_irqsave(&lock, flags);
2098         if (!Inited) {
2099                 RandomValue = jiffies;
2100                 Inited = 1;
2101         }
2102         rv = RandomValue;
2103         hi = rv / 127773;
2104         lo = rv % 127773;
2105         rv = 16807 * lo - 2836 * hi;
2106         if (rv <= 0) rv += 2147483647;
2107         RandomValue = rv;
2108         spin_unlock_irqrestore(&lock, flags);
2109         return rv;
2110 }
2111 #endif
2112
2113 #ifdef DEBUG_TM
2114
2115 #define TM_DBG_STATE_ABORT              0
2116 #define TM_DBG_STATE_RESET              1
2117 #define TM_DBG_STATE_OFFLINE            2
2118
2119 #define INIT_TM_DBG_STATE               TM_DBG_STATE_ABORT
2120
2121 static void tm_dbg_timer_fn(unsigned long arg);
2122
2123 /* All serialized by scst_list_lock */
2124 static int tm_dbg_release;
2125 static int tm_dbg_blocked;
2126 static LIST_HEAD(tm_dbg_delayed_cmd_list);
2127 static int tm_dbg_delayed_cmds_count;
2128 static int tm_dbg_passed_cmds_count;
2129 static int tm_dbg_state;
2130 static int tm_dbg_on_state_passes;
2131 static DEFINE_TIMER(tm_dbg_timer, tm_dbg_timer_fn, 0, 0);
2132
2133 static const int tm_dbg_on_state_num_passes[] = { 10, 1, 0x7ffffff };
2134
2135 void tm_dbg_init_tgt_dev(struct scst_tgt_dev *tgt_dev,
2136         struct scst_acg_dev *acg_dev)
2137 {
2138         if ((acg_dev->acg == scst_default_acg) && (acg_dev->lun == 0)) {
2139                 /* Do TM debugging only for LUN 0 */
2140                 tm_dbg_state = INIT_TM_DBG_STATE;
2141                 tm_dbg_on_state_passes =
2142                         tm_dbg_on_state_num_passes[tm_dbg_state];
2143                 __set_bit(SCST_TGT_DEV_UNDER_TM_DBG, &tgt_dev->tgt_dev_flags);
2144                 PRINT_INFO("LUN 0 connected from initiator %s is under "
2145                         "TM debugging", tgt_dev->sess->tgt->tgtt->name);
2146         }
2147 }
2148
2149 void tm_dbg_deinit_tgt_dev(struct scst_tgt_dev *tgt_dev)
2150 {
2151         if (test_bit(SCST_TGT_DEV_UNDER_TM_DBG, &tgt_dev->tgt_dev_flags))
2152                 del_timer_sync(&tm_dbg_timer);
2153 }
2154
2155 static void tm_dbg_timer_fn(unsigned long arg)
2156 {
2157         TRACE_MGMT_DBG("%s: delayed cmd timer expired", __func__);
2158         tm_dbg_release = 1;
2159         smp_mb();
2160         wake_up_all(&scst_list_waitQ);
2161 }
2162
2163 /* Called under scst_list_lock */
2164 static void tm_dbg_delay_cmd(struct scst_cmd *cmd)
2165 {
2166         switch(tm_dbg_state) {
2167         case TM_DBG_STATE_ABORT:
2168                 if (tm_dbg_delayed_cmds_count == 0) {
2169                         unsigned long d = 58*HZ + (scst_random() % (4*HZ));
2170                         TRACE_MGMT_DBG("%s: delaying timed cmd %p (tag %d) "
2171                                 "for %ld.%ld seconds (%ld HZ)", __func__, cmd, cmd->tag,
2172                                 d/HZ, (d%HZ)*100/HZ, d);
2173                         mod_timer(&tm_dbg_timer, jiffies + d);
2174 #if 0
2175                         tm_dbg_blocked = 1;
2176 #endif
2177                 } else {
2178                         TRACE_MGMT_DBG("%s: delaying another timed cmd %p "
2179                                 "(tag %d), delayed_cmds_count=%d", __func__, cmd,
2180                                 cmd->tag, tm_dbg_delayed_cmds_count);
2181                         if (tm_dbg_delayed_cmds_count == 2)
2182                                 tm_dbg_blocked = 0;
2183                 }
2184                 break;
2185
2186         case TM_DBG_STATE_RESET:
2187         case TM_DBG_STATE_OFFLINE:
2188                 TRACE_MGMT_DBG("%s: delaying cmd %p "
2189                         "(tag %d), delayed_cmds_count=%d", __func__, cmd,
2190                         cmd->tag, tm_dbg_delayed_cmds_count);
2191                 tm_dbg_blocked = 1;
2192                 break;
2193
2194         default:
2195                 BUG();
2196         }
2197         list_move_tail(&cmd->cmd_list_entry, &tm_dbg_delayed_cmd_list);
2198         cmd->tm_dbg_delayed = 1;
2199         tm_dbg_delayed_cmds_count++;
2200         return;
2201 }
2202
2203 /* Called under scst_list_lock */
2204 void tm_dbg_check_released_cmds(void)
2205 {
2206         if (tm_dbg_release) {
2207                 struct scst_cmd *cmd, *tc;
2208                 list_for_each_entry_safe_reverse(cmd, tc, 
2209                                 &tm_dbg_delayed_cmd_list, cmd_list_entry) {
2210                         TRACE_MGMT_DBG("%s: Releasing timed cmd %p "
2211                                 "(tag %d), delayed_cmds_count=%d", __func__,
2212                                 cmd, cmd->tag, tm_dbg_delayed_cmds_count);
2213                         list_move(&cmd->cmd_list_entry, &scst_active_cmd_list);
2214                 }
2215                 tm_dbg_release = 0;
2216         }
2217 }
2218
2219 static void tm_dbg_change_state(void)
2220 {
2221         tm_dbg_blocked = 0;
2222         if (--tm_dbg_on_state_passes == 0) {
2223                 switch(tm_dbg_state) {
2224                 case TM_DBG_STATE_ABORT:
2225                         TRACE_MGMT_DBG("%s", "Changing "
2226                             "tm_dbg_state to RESET");
2227                         tm_dbg_state =
2228                                 TM_DBG_STATE_RESET;
2229                         tm_dbg_blocked = 0;
2230                         break;
2231                 case TM_DBG_STATE_RESET:
2232                 case TM_DBG_STATE_OFFLINE:
2233                         if (TM_DBG_GO_OFFLINE) {
2234                             TRACE_MGMT_DBG("%s", "Changing "
2235                                     "tm_dbg_state to OFFLINE");
2236                             tm_dbg_state =
2237                                 TM_DBG_STATE_OFFLINE;
2238                         } else {
2239                             TRACE_MGMT_DBG("%s", "Changing "
2240                                     "tm_dbg_state to ABORT");
2241                             tm_dbg_state =
2242                                 TM_DBG_STATE_ABORT;
2243                         }
2244                         break;
2245                 default:
2246                         BUG();
2247                 }
2248                 tm_dbg_on_state_passes =
2249                     tm_dbg_on_state_num_passes[tm_dbg_state];
2250         }
2251                 
2252         TRACE_MGMT_DBG("%s", "Deleting timer");
2253         del_timer(&tm_dbg_timer);
2254 }
2255
2256 /* Called under scst_list_lock */
2257 int tm_dbg_check_cmd(struct scst_cmd *cmd)
2258 {
2259         int res = 0;
2260
2261         if (cmd->tm_dbg_immut)
2262                 goto out;
2263
2264         if (cmd->tm_dbg_delayed) {
2265                 TRACE_MGMT_DBG("Processing delayed cmd %p (tag %d), "
2266                         "delayed_cmds_count=%d", cmd, cmd->tag,
2267                         tm_dbg_delayed_cmds_count);
2268
2269                 cmd->tm_dbg_immut = 1;
2270                 tm_dbg_delayed_cmds_count--;
2271                 if ((tm_dbg_delayed_cmds_count == 0) &&
2272                     (tm_dbg_state == TM_DBG_STATE_ABORT))
2273                         tm_dbg_change_state();
2274
2275         } else if (cmd->tgt_dev && test_bit(SCST_TGT_DEV_UNDER_TM_DBG,
2276                                         &cmd->tgt_dev->tgt_dev_flags)) {
2277                 /* Delay 50th command */
2278                 if (tm_dbg_blocked || (++tm_dbg_passed_cmds_count % 50) == 0) {
2279                         tm_dbg_delay_cmd(cmd);
2280                         res = 1;
2281                 } else
2282                         cmd->tm_dbg_immut = 1;
2283         }
2284
2285 out:
2286         return res;
2287 }
2288
2289 /* Called under scst_list_lock */
2290 void tm_dbg_release_cmd(struct scst_cmd *cmd)
2291 {
2292         struct scst_cmd *c;
2293         list_for_each_entry(c, &tm_dbg_delayed_cmd_list,
2294                                 cmd_list_entry) {
2295                 if (c == cmd) {
2296                         TRACE_MGMT_DBG("Abort request for "
2297                                 "delayed cmd %p (tag=%d), moving it to "
2298                                 "active cmd list (delayed_cmds_count=%d)",
2299                                 c, c->tag, tm_dbg_delayed_cmds_count);
2300                         list_move(&c->cmd_list_entry, &scst_active_cmd_list);
2301                         wake_up_all(&scst_list_waitQ);
2302                         break;
2303                 }
2304         }
2305 }
2306
2307 /* Called under scst_list_lock */
2308 void tm_dbg_task_mgmt(const char *fn)
2309 {
2310         if (tm_dbg_state != TM_DBG_STATE_OFFLINE) {
2311                 TRACE_MGMT_DBG("%s: freeing %d delayed cmds", fn,
2312                         tm_dbg_delayed_cmds_count);
2313                 tm_dbg_change_state();
2314                 tm_dbg_release = 1;
2315                 smp_mb();
2316                 wake_up_all(&scst_list_waitQ);
2317         } else {
2318                 TRACE_MGMT_DBG("%s: while OFFLINE state, doing nothing", fn);
2319         }
2320 }
2321
2322 int tm_dbg_is_release(void)
2323 {
2324         return tm_dbg_release;
2325 }
2326 #endif /* DEBUG_TM */