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