5925c30c213d3325c6b0e2e05cf8adf72f6c00da
[mirror/scst/.git] / scst / src / scst_lib.c
1 /*
2  *  scst_lib.c
3  *
4  *  Copyright (C) 2004-2007 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 <linux/kthread.h>
26 #include <linux/cdrom.h>
27 #include <asm/unistd.h>
28 #include <asm/string.h>
29
30 #ifdef SCST_HIGHMEM
31 #include <linux/highmem.h>
32 #endif
33
34 #include "scst.h"
35 #include "scst_priv.h"
36 #include "scst_mem.h"
37
38 #include "scst_cdbprobe.h"
39
40 static void scst_free_tgt_dev(struct scst_tgt_dev *tgt_dev);
41 static void scst_check_internal_sense(struct scst_device *dev, int result,
42         uint8_t *sense, int sense_len);
43
44 int scst_alloc_sense(struct scst_cmd *cmd, int atomic)
45 {
46         int res = 0;
47         unsigned long gfp_mask = atomic ? GFP_ATOMIC : (GFP_KERNEL|__GFP_NOFAIL);
48
49         TRACE_ENTRY();
50
51         sBUG_ON(cmd->sense != NULL);
52
53         cmd->sense = mempool_alloc(scst_sense_mempool, gfp_mask);
54         if (cmd->sense == NULL) {
55                 PRINT_CRIT_ERROR("Sense memory allocation failed (op %x). "
56                         "The sense data will be lost!!", cmd->cdb[0]);
57                 res = -ENOMEM;
58                 goto out;
59         }
60
61         memset(cmd->sense, 0, SCST_SENSE_BUFFERSIZE);
62
63 out:
64         TRACE_EXIT_RES(res);
65         return res;
66 }
67
68 int scst_alloc_set_sense(struct scst_cmd *cmd, int atomic,
69         const uint8_t *sense, unsigned int len)
70 {
71         int res;
72
73         TRACE_ENTRY();
74
75         res = scst_alloc_sense(cmd, atomic);
76         if (res != 0) {
77                 PRINT_BUFFER("Lost sense", sense, len);
78                 goto out;
79         }
80
81         memcpy(cmd->sense, sense, min((int)len, (int)SCST_SENSE_BUFFERSIZE));
82         TRACE_BUFFER("Sense set", cmd->sense, SCST_SENSE_BUFFERSIZE);
83
84 out:
85         TRACE_EXIT_RES(res);
86         return res;
87 }
88
89 void scst_set_cmd_error_status(struct scst_cmd *cmd, int status)
90 {
91         TRACE_ENTRY();
92
93         cmd->status = status;
94         cmd->host_status = DID_OK;
95
96         cmd->data_direction = SCST_DATA_NONE;
97         cmd->is_send_status = 1;
98         cmd->resp_data_len = 0;
99
100         cmd->completed = 1;
101
102         TRACE_EXIT();
103         return;
104 }
105
106 void scst_set_cmd_error(struct scst_cmd *cmd, int key, int asc, int ascq)
107 {
108         int rc;
109
110         TRACE_ENTRY();
111
112         scst_set_cmd_error_status(cmd, SAM_STAT_CHECK_CONDITION);
113
114         rc = scst_alloc_sense(cmd, 1);
115         if (rc != 0) {
116                 PRINT_ERROR("Lost sense data (key %x, asc %x, ascq %x)",
117                         key, asc, ascq);
118                 goto out;
119         }
120
121         scst_set_sense(cmd->sense, SCST_SENSE_BUFFERSIZE, key, asc, ascq);
122         TRACE_BUFFER("Sense set", cmd->sense, SCST_SENSE_BUFFERSIZE);
123
124 out:
125         TRACE_EXIT();
126         return;
127 }
128
129 void scst_set_sense(uint8_t *buffer, int len, int key,
130         int asc, int ascq)
131 {
132         memset(buffer, 0, len);
133         buffer[0] = 0x70;       /* Error Code                   */
134         buffer[2] = key;        /* Sense Key                    */
135         buffer[7] = 0x0a;       /* Additional Sense Length      */
136         buffer[12] = asc;       /* ASC                          */
137         buffer[13] = ascq;      /* ASCQ                         */
138         TRACE_BUFFER("Sense set", buffer, len);
139         return;
140 }
141
142 void scst_set_cmd_error_sense(struct scst_cmd *cmd, uint8_t *sense,
143         unsigned int len)
144 {
145         TRACE_ENTRY();
146
147         scst_set_cmd_error_status(cmd, SAM_STAT_CHECK_CONDITION);
148         scst_alloc_set_sense(cmd, 1, sense, len);
149
150         TRACE_EXIT();
151         return;
152 }
153
154 void scst_set_busy(struct scst_cmd *cmd)
155 {
156         int c = atomic_read(&cmd->sess->sess_cmd_count);
157
158         TRACE_ENTRY();
159
160         if ((c <= 1) || (cmd->sess->init_phase != SCST_SESS_IPH_READY)) {
161                 scst_set_cmd_error_status(cmd, SAM_STAT_BUSY);
162                 TRACE(TRACE_MGMT_MINOR, "Sending BUSY status to initiator %s "
163                         "(cmds count %d, queue_type %x, sess->init_phase %d)",
164                         cmd->sess->initiator_name, c,
165                         cmd->queue_type, cmd->sess->init_phase);
166         } else {
167                 scst_set_cmd_error_status(cmd, SAM_STAT_TASK_SET_FULL);
168                 TRACE(TRACE_MGMT_MINOR, "Sending QUEUE_FULL status to "
169                         "initiator %s (cmds count %d, queue_type %x, "
170                         "sess->init_phase %d)", cmd->sess->initiator_name, c,
171                         cmd->queue_type, cmd->sess->init_phase);
172         }
173
174         TRACE_EXIT();
175         return;
176 }
177
178 void scst_set_resp_data_len(struct scst_cmd *cmd, int resp_data_len)
179 {
180         int i, l;
181
182         TRACE_ENTRY();
183
184         scst_check_restore_sg_buff(cmd);
185         cmd->resp_data_len = resp_data_len;
186
187         if (resp_data_len == cmd->bufflen)
188                 goto out;
189
190         l = 0;
191         for (i = 0; i < cmd->sg_cnt; i++) {
192                 l += cmd->sg[i].length;
193                 if (l >= resp_data_len) {
194                         int left = resp_data_len - (l - cmd->sg[i].length);
195 #ifdef DEBUG
196                         TRACE(TRACE_SG_OP|TRACE_MEMORY, "cmd %p (tag %llu), "
197                                 "resp_data_len %d, i %d, cmd->sg[i].length %d, "
198                                 "left %d", cmd, cmd->tag, resp_data_len, i,
199                                 cmd->sg[i].length, left);
200 #endif
201                         cmd->orig_sg_cnt = cmd->sg_cnt;
202                         cmd->orig_sg_entry = i;
203                         cmd->orig_entry_len = cmd->sg[i].length;
204                         cmd->sg_cnt = (left > 0) ? i+1 : i;
205                         cmd->sg[i].length = left;
206                         cmd->sg_buff_modified = 1;
207                         break;
208                 }
209         }
210
211 out:
212         TRACE_EXIT();
213         return;
214 }
215
216 /* Called under scst_mutex and suspended activity */
217 int scst_alloc_device(int gfp_mask, struct scst_device **out_dev)
218 {
219         struct scst_device *dev;
220         int res = 0;
221         static int dev_num; /* protected by scst_mutex */
222
223         TRACE_ENTRY();
224
225         dev = kzalloc(sizeof(*dev), gfp_mask);
226         if (dev == NULL) {
227                 TRACE(TRACE_OUT_OF_MEM, "%s",
228                         "Allocation of scst_device failed");
229                 res = -ENOMEM;
230                 goto out;
231         }
232
233         dev->handler = &scst_null_devtype;
234         dev->p_cmd_lists = &scst_main_cmd_lists;
235         atomic_set(&dev->dev_cmd_count, 0);
236         atomic_set(&dev->write_cmd_count, 0);
237         spin_lock_init(&dev->dev_lock);
238         atomic_set(&dev->on_dev_count, 0);
239         INIT_LIST_HEAD(&dev->blocked_cmd_list);
240         INIT_LIST_HEAD(&dev->dev_tgt_dev_list);
241         INIT_LIST_HEAD(&dev->dev_acg_dev_list);
242         INIT_LIST_HEAD(&dev->threads_list);
243         init_waitqueue_head(&dev->on_dev_waitQ);
244         dev->dev_double_ua_possible = 1;
245         dev->dev_serialized = 1;
246         dev->queue_alg = SCST_CONTR_MODE_QUEUE_ALG_UNRESTRICTED_REORDER;
247         dev->dev_num = dev_num++;
248
249         *out_dev = dev;
250
251 out:
252         TRACE_EXIT_RES(res);
253         return res;
254 }
255
256 /* Called under scst_mutex and suspended activity */
257 void scst_free_device(struct scst_device *dev)
258 {
259         TRACE_ENTRY();
260
261 #ifdef EXTRACHECKS
262         if (!list_empty(&dev->dev_tgt_dev_list) ||
263             !list_empty(&dev->dev_acg_dev_list)) {
264                 PRINT_CRIT_ERROR("%s: dev_tgt_dev_list or dev_acg_dev_list "
265                         "is not empty!", __FUNCTION__);
266                 sBUG();
267         }
268 #endif
269
270         kfree(dev);
271
272         TRACE_EXIT();
273         return;
274 }
275
276 struct scst_acg_dev *scst_alloc_acg_dev(struct scst_acg *acg,
277         struct scst_device *dev, lun_t lun)
278 {
279         struct scst_acg_dev *res;
280
281         TRACE_ENTRY();
282
283 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 17)
284         res = kmem_cache_alloc(scst_acgd_cachep, GFP_KERNEL);
285 #else
286         res = kmem_cache_zalloc(scst_acgd_cachep, GFP_KERNEL);
287 #endif
288         if (res == NULL) {
289                 TRACE(TRACE_OUT_OF_MEM, "%s", "Allocation of scst_acg_dev failed");
290                 goto out;
291         }
292 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 17)
293         memset(res, 0, sizeof(*res));
294 #endif
295
296         res->dev = dev;
297         res->acg = acg;
298         res->lun = lun;
299
300 out:
301         TRACE_EXIT_HRES(res);
302         return res;
303 }
304
305 /* The activity supposed to be suspended and scst_mutex held */
306 void scst_free_acg_dev(struct scst_acg_dev *acg_dev)
307 {
308         TRACE_ENTRY();
309
310         TRACE_DBG("Removing acg_dev %p from acg_dev_list and dev_acg_dev_list",
311                 acg_dev);
312         list_del(&acg_dev->acg_dev_list_entry);
313         list_del(&acg_dev->dev_acg_dev_list_entry);
314
315         kmem_cache_free(scst_acgd_cachep, acg_dev);
316
317         TRACE_EXIT();
318         return;
319 }
320
321 /* The activity supposed to be suspended and scst_mutex held */
322 struct scst_acg *scst_alloc_add_acg(const char *acg_name)
323 {
324         struct scst_acg *acg;
325
326         TRACE_ENTRY();
327
328         acg = kzalloc(sizeof(*acg), GFP_KERNEL);
329         if (acg == NULL) {
330                 TRACE(TRACE_OUT_OF_MEM, "%s", "Allocation of acg failed");
331                 goto out;
332         }
333
334         INIT_LIST_HEAD(&acg->acg_dev_list);
335         INIT_LIST_HEAD(&acg->acg_sess_list);
336         INIT_LIST_HEAD(&acg->acn_list);
337         acg->acg_name = acg_name;
338
339         TRACE_DBG("Adding acg %s to scst_acg_list", acg_name);
340         list_add_tail(&acg->scst_acg_list_entry, &scst_acg_list);
341
342 out:
343         TRACE_EXIT_HRES(acg);
344         return acg;
345 }
346
347 /* The activity supposed to be suspended and scst_mutex held */
348 int scst_destroy_acg(struct scst_acg *acg)
349 {
350         struct scst_acn *n, *nn;
351         struct scst_acg_dev *acg_dev, *acg_dev_tmp;
352         int res = 0;
353
354         TRACE_ENTRY();
355
356         if (!list_empty(&acg->acg_sess_list)) {
357                 PRINT_ERROR("%s: acg_sess_list is not empty!", __FUNCTION__);
358                 res = -EBUSY;
359                 goto out;
360         }
361
362         TRACE_DBG("Removing acg %s from scst_acg_list", acg->acg_name);
363         list_del(&acg->scst_acg_list_entry);
364
365         /* Freeing acg_devs */
366         list_for_each_entry_safe(acg_dev, acg_dev_tmp, &acg->acg_dev_list,
367                         acg_dev_list_entry) {
368                 struct scst_tgt_dev *tgt_dev, *tt;
369                 list_for_each_entry_safe(tgt_dev, tt,
370                                  &acg_dev->dev->dev_tgt_dev_list,
371                                  dev_tgt_dev_list_entry) {
372                         if (tgt_dev->acg_dev == acg_dev)
373                                 scst_free_tgt_dev(tgt_dev);
374                 }
375                 scst_free_acg_dev(acg_dev);
376         }
377
378         /* Freeing names */
379         list_for_each_entry_safe(n, nn, &acg->acn_list,
380                         acn_list_entry) {
381                 list_del(&n->acn_list_entry);
382                 kfree(n->name);
383                 kfree(n);
384         }
385         INIT_LIST_HEAD(&acg->acn_list);
386
387         kfree(acg);
388 out:
389         TRACE_EXIT_RES(res);
390         return res;
391 }
392
393 /* scst_mutex supposed to be held, there must not be parallel activity in this sess */
394 static struct scst_tgt_dev *scst_alloc_add_tgt_dev(struct scst_session *sess,
395         struct scst_acg_dev *acg_dev)
396 {
397         int ini_sg, ini_unchecked_isa_dma, ini_use_clustering;
398         struct scst_tgt_dev *tgt_dev;
399         struct scst_device *dev = acg_dev->dev;
400         struct list_head *sess_tgt_dev_list_head;
401         struct scst_tgt_template *vtt = sess->tgt->tgtt;
402         int rc, i;
403
404         TRACE_ENTRY();
405
406 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 17)
407         tgt_dev = kmem_cache_alloc(scst_tgtd_cachep, GFP_KERNEL);
408 #else
409         tgt_dev = kmem_cache_zalloc(scst_tgtd_cachep, GFP_KERNEL);
410 #endif
411         if (tgt_dev == NULL) {
412                 TRACE(TRACE_OUT_OF_MEM, "%s",
413                       "Allocation of scst_tgt_dev failed");
414                 goto out;
415         }
416 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 17)
417         memset(tgt_dev, 0, sizeof(*tgt_dev));
418 #endif
419
420         tgt_dev->dev = dev;
421         tgt_dev->lun = acg_dev->lun;
422         tgt_dev->acg_dev = acg_dev;
423         tgt_dev->sess = sess;
424         atomic_set(&tgt_dev->tgt_dev_cmd_count, 0);
425
426         scst_sgv_pool_use_norm(tgt_dev);
427
428         if (dev->scsi_dev != NULL) {
429                 ini_sg = dev->scsi_dev->host->sg_tablesize;
430                 ini_unchecked_isa_dma = dev->scsi_dev->host->unchecked_isa_dma;
431                 ini_use_clustering = (dev->scsi_dev->host->use_clustering ==
432                                 ENABLE_CLUSTERING);
433         } else {
434                 ini_sg = (1 << 15) /* infinite */;
435                 ini_unchecked_isa_dma = 0;
436                 ini_use_clustering = 0;
437         }
438         tgt_dev->max_sg_cnt = min(ini_sg, sess->tgt->sg_tablesize);
439
440         if ((sess->tgt->tgtt->use_clustering || ini_use_clustering) &&
441             !sess->tgt->tgtt->no_clustering)
442                 scst_sgv_pool_use_norm_clust(tgt_dev);
443
444         if (sess->tgt->tgtt->unchecked_isa_dma || ini_unchecked_isa_dma) {
445                 scst_sgv_pool_use_dma(tgt_dev);
446         } else {
447 #ifdef SCST_HIGHMEM
448                 scst_sgv_pool_use_highmem(tgt_dev);
449 #endif
450         }
451
452         if (dev->scsi_dev != NULL) {
453                 TRACE_MGMT_DBG("host=%d, channel=%d, id=%d, lun=%d, "
454                       "SCST lun=%Ld", dev->scsi_dev->host->host_no,
455                       dev->scsi_dev->channel, dev->scsi_dev->id,
456                       dev->scsi_dev->lun, (uint64_t)tgt_dev->lun);
457         } else {
458                 TRACE_MGMT_DBG("Virtual device %s on SCST lun=%Ld",
459                         dev->virt_name, (uint64_t)tgt_dev->lun);
460         }
461
462         spin_lock_init(&tgt_dev->tgt_dev_lock);
463         INIT_LIST_HEAD(&tgt_dev->UA_list);
464         spin_lock_init(&tgt_dev->thr_data_lock);
465         INIT_LIST_HEAD(&tgt_dev->thr_data_list);
466         spin_lock_init(&tgt_dev->sn_lock);
467         INIT_LIST_HEAD(&tgt_dev->deferred_cmd_list);
468         INIT_LIST_HEAD(&tgt_dev->skipped_sn_list);
469         tgt_dev->expected_sn = 1;
470         tgt_dev->num_free_sn_slots = ARRAY_SIZE(tgt_dev->sn_slots)-1;
471         tgt_dev->cur_sn_slot = &tgt_dev->sn_slots[0];
472         for (i = 0; i < (int)ARRAY_SIZE(tgt_dev->sn_slots); i++)
473                 atomic_set(&tgt_dev->sn_slots[i], 0);
474
475         if (dev->handler->parse_atomic &&
476             (sess->tgt->tgtt->preprocessing_done == NULL)) {
477                 if (sess->tgt->tgtt->rdy_to_xfer_atomic ||
478                     (sess->tgt->tgtt->rdy_to_xfer == NULL))
479                         __set_bit(SCST_TGT_DEV_AFTER_INIT_WR_ATOMIC,
480                                 &tgt_dev->tgt_dev_flags);
481                 if (dev->handler->exec_atomic || (dev->handler->exec == NULL))
482                         __set_bit(SCST_TGT_DEV_AFTER_INIT_OTH_ATOMIC,
483                                 &tgt_dev->tgt_dev_flags);
484         }
485         if (dev->handler->exec_atomic || (dev->handler->exec == NULL)) {
486                 if (sess->tgt->tgtt->rdy_to_xfer_atomic ||
487                     (sess->tgt->tgtt->rdy_to_xfer == NULL))
488                         __set_bit(SCST_TGT_DEV_AFTER_RESTART_WR_ATOMIC,
489                                 &tgt_dev->tgt_dev_flags);
490                 __set_bit(SCST_TGT_DEV_AFTER_RESTART_OTH_ATOMIC,
491                                 &tgt_dev->tgt_dev_flags);
492                 __set_bit(SCST_TGT_DEV_AFTER_RX_DATA_ATOMIC,
493                         &tgt_dev->tgt_dev_flags);
494         }
495         if ((dev->handler->dev_done_atomic ||
496              (dev->handler->dev_done == NULL)) &&
497             sess->tgt->tgtt->xmit_response_atomic) {
498                 __set_bit(SCST_TGT_DEV_AFTER_EXEC_ATOMIC,
499                         &tgt_dev->tgt_dev_flags);
500         }
501
502         spin_lock_bh(&scst_temp_UA_lock);
503         scst_set_sense(scst_temp_UA, sizeof(scst_temp_UA),
504                 SCST_LOAD_SENSE(scst_sense_reset_UA));
505         scst_alloc_set_UA(tgt_dev, scst_temp_UA, sizeof(scst_temp_UA), 0);
506         spin_unlock_bh(&scst_temp_UA_lock);
507
508         tm_dbg_init_tgt_dev(tgt_dev, acg_dev);
509
510         if (vtt->threads_num > 0) {
511                 rc = 0;
512                 if (dev->handler->threads_num > 0)
513                         rc = scst_add_dev_threads(dev, vtt->threads_num);
514                 else if (dev->handler->threads_num == 0)
515                         rc = scst_add_cmd_threads(vtt->threads_num);
516                 if (rc != 0)
517                         goto out_free;
518         }
519
520         if (dev->handler && dev->handler->attach_tgt) {
521                 TRACE_DBG("Calling dev handler's attach_tgt(%p)",
522                       tgt_dev);
523                 rc = dev->handler->attach_tgt(tgt_dev);
524                 TRACE_DBG("%s", "Dev handler's attach_tgt() returned");
525                 if (rc != 0) {
526                         PRINT_ERROR("Device handler's %s attach_tgt() "
527                             "failed: %d", dev->handler->name, rc);
528                         goto out_thr_free;
529                 }
530         }
531
532         spin_lock_bh(&dev->dev_lock);
533         list_add_tail(&tgt_dev->dev_tgt_dev_list_entry, &dev->dev_tgt_dev_list);
534         if (dev->dev_reserved)
535                 __set_bit(SCST_TGT_DEV_RESERVED, &tgt_dev->tgt_dev_flags);
536         spin_unlock_bh(&dev->dev_lock);
537
538         sess_tgt_dev_list_head =
539                 &sess->sess_tgt_dev_list_hash[HASH_VAL(tgt_dev->lun)];
540         list_add_tail(&tgt_dev->sess_tgt_dev_list_entry, sess_tgt_dev_list_head);
541
542 out:
543         TRACE_EXIT();
544         return tgt_dev;
545
546 out_thr_free:
547         if (vtt->threads_num > 0) {
548                 if (dev->handler->threads_num > 0)
549                         scst_del_dev_threads(dev, vtt->threads_num);
550                 else if (dev->handler->threads_num == 0)
551                         scst_del_cmd_threads(vtt->threads_num);
552         }
553
554 out_free:
555         kmem_cache_free(scst_tgtd_cachep, tgt_dev);
556         tgt_dev = NULL;
557         goto out;
558 }
559
560 static void scst_clear_reservation(struct scst_tgt_dev *tgt_dev);
561
562 /* No locks supposed to be held, scst_mutex - held */
563 void scst_nexus_loss(struct scst_tgt_dev *tgt_dev)
564 {
565         TRACE_ENTRY();
566
567         scst_clear_reservation(tgt_dev);
568
569         /* With activity suspended the lock isn't needed, but let's be safe */
570         spin_lock_bh(&tgt_dev->tgt_dev_lock);
571         scst_free_all_UA(tgt_dev);
572         spin_unlock_bh(&tgt_dev->tgt_dev_lock);
573
574         spin_lock_bh(&scst_temp_UA_lock);
575         scst_set_sense(scst_temp_UA, sizeof(scst_temp_UA),
576                 SCST_LOAD_SENSE(scst_sense_nexus_loss_UA));
577         scst_check_set_UA(tgt_dev, scst_temp_UA, sizeof(scst_temp_UA), 0);
578         spin_unlock_bh(&scst_temp_UA_lock);
579
580         TRACE_EXIT();
581         return;
582 }
583
584 /* scst_mutex supposed to be held, there must not be parallel activity in this sess */
585 static void scst_free_tgt_dev(struct scst_tgt_dev *tgt_dev)
586 {
587         struct scst_device *dev = tgt_dev->dev;
588         struct scst_tgt_template *vtt = tgt_dev->sess->tgt->tgtt;
589
590         TRACE_ENTRY();
591
592         tm_dbg_deinit_tgt_dev(tgt_dev);
593
594         spin_lock_bh(&dev->dev_lock);
595         list_del(&tgt_dev->dev_tgt_dev_list_entry);
596         spin_unlock_bh(&dev->dev_lock);
597
598         list_del(&tgt_dev->sess_tgt_dev_list_entry);
599
600         scst_clear_reservation(tgt_dev);
601         scst_free_all_UA(tgt_dev);
602
603         if (dev->handler && dev->handler->detach_tgt) {
604                 TRACE_DBG("Calling dev handler's detach_tgt(%p)",
605                       tgt_dev);
606                 dev->handler->detach_tgt(tgt_dev);
607                 TRACE_DBG("%s", "Dev handler's detach_tgt() returned");
608         }
609
610         if (vtt->threads_num > 0) {
611                 if (dev->handler->threads_num > 0)
612                         scst_del_dev_threads(dev, vtt->threads_num);
613                 else if (dev->handler->threads_num == 0)
614                         scst_del_cmd_threads(vtt->threads_num);
615         }
616
617         kmem_cache_free(scst_tgtd_cachep, tgt_dev);
618
619         TRACE_EXIT();
620         return;
621 }
622
623 /* scst_mutex supposed to be held */
624 int scst_sess_alloc_tgt_devs(struct scst_session *sess)
625 {
626         int res = 0;
627         struct scst_acg_dev *acg_dev;
628         struct scst_tgt_dev *tgt_dev;
629
630         TRACE_ENTRY();
631
632         list_for_each_entry(acg_dev, &sess->acg->acg_dev_list,
633                         acg_dev_list_entry) {
634                 tgt_dev = scst_alloc_add_tgt_dev(sess, acg_dev);
635                 if (tgt_dev == NULL) {
636                         res = -ENOMEM;
637                         goto out_free;
638                 }
639         }
640
641 out:
642         TRACE_EXIT();
643         return res;
644
645 out_free:
646         scst_sess_free_tgt_devs(sess);
647         goto out;
648 }
649
650 /* scst_mutex supposed to be held, there must not be parallel activity in this sess */
651 void scst_sess_free_tgt_devs(struct scst_session *sess)
652 {
653         int i;
654         struct scst_tgt_dev *tgt_dev, *t;
655
656         TRACE_ENTRY();
657
658         /* The session is going down, no users, so no locks */
659         for (i = 0; i < TGT_DEV_HASH_SIZE; i++) {
660                 struct list_head *sess_tgt_dev_list_head =
661                         &sess->sess_tgt_dev_list_hash[i];
662                 list_for_each_entry_safe(tgt_dev, t, sess_tgt_dev_list_head,
663                                 sess_tgt_dev_list_entry) {
664                         scst_free_tgt_dev(tgt_dev);
665                 }
666                 INIT_LIST_HEAD(sess_tgt_dev_list_head);
667         }
668
669         TRACE_EXIT();
670         return;
671 }
672
673 /* The activity supposed to be suspended and scst_mutex held */
674 int scst_acg_add_dev(struct scst_acg *acg, struct scst_device *dev, lun_t lun,
675         int read_only)
676 {
677         int res = 0;
678         struct scst_acg_dev *acg_dev;
679         struct scst_tgt_dev *tgt_dev;
680         struct scst_session *sess;
681         LIST_HEAD(tmp_tgt_dev_list);
682
683         TRACE_ENTRY();
684
685         INIT_LIST_HEAD(&tmp_tgt_dev_list);
686
687 #ifdef EXTRACHECKS
688         list_for_each_entry(acg_dev, &acg->acg_dev_list, acg_dev_list_entry) {
689                 if (acg_dev->dev == dev) {
690                         PRINT_ERROR("Device is already in group %s",
691                                 acg->acg_name);
692                         res = -EINVAL;
693                         goto out;
694                 }
695         }
696 #endif
697
698         acg_dev = scst_alloc_acg_dev(acg, dev, lun);
699         if (acg_dev == NULL) {
700                 res = -ENOMEM;
701                 goto out;
702         }
703         acg_dev->rd_only_flag = read_only;
704
705         TRACE_DBG("Adding acg_dev %p to acg_dev_list and dev_acg_dev_list",
706                 acg_dev);
707         list_add_tail(&acg_dev->acg_dev_list_entry, &acg->acg_dev_list);
708         list_add_tail(&acg_dev->dev_acg_dev_list_entry, &dev->dev_acg_dev_list);
709
710         list_for_each_entry(sess, &acg->acg_sess_list, acg_sess_list_entry)
711         {
712                 tgt_dev = scst_alloc_add_tgt_dev(sess, acg_dev);
713                 if (tgt_dev == NULL) {
714                         res = -ENOMEM;
715                         goto out_free;
716                 }
717                 list_add_tail(&tgt_dev->extra_tgt_dev_list_entry,
718                               &tmp_tgt_dev_list);
719         }
720
721 out:
722         if (res == 0) {
723                 if (dev->virt_name != NULL) {
724                         PRINT_INFO("Added device %s to group %s (LUN %Ld, "
725                                 "rd_only %d)", dev->virt_name, acg->acg_name,
726                                 lun, read_only);
727                 } else {
728                         PRINT_INFO("Added device %d:%d:%d:%d to group %s (LUN "
729                                 "%Ld, rd_only %d)", dev->scsi_dev->host->host_no,
730                                 dev->scsi_dev->channel, dev->scsi_dev->id,
731                                 dev->scsi_dev->lun, acg->acg_name, lun,
732                                 read_only);
733                 }
734         }
735
736         TRACE_EXIT_RES(res);
737         return res;
738
739 out_free:
740         list_for_each_entry(tgt_dev, &tmp_tgt_dev_list,
741                          extra_tgt_dev_list_entry) {
742                 scst_free_tgt_dev(tgt_dev);
743         }
744         scst_free_acg_dev(acg_dev);
745         goto out;
746 }
747
748 /* The activity supposed to be suspended and scst_mutex held */
749 int scst_acg_remove_dev(struct scst_acg *acg, struct scst_device *dev)
750 {
751         int res = 0;
752         struct scst_acg_dev *acg_dev = NULL, *a;
753         struct scst_tgt_dev *tgt_dev, *tt;
754
755         TRACE_ENTRY();
756
757         list_for_each_entry(a, &acg->acg_dev_list, acg_dev_list_entry) {
758                 if (a->dev == dev) {
759                         acg_dev = a;
760                         break;
761                 }
762         }
763
764         if (acg_dev == NULL) {
765                 PRINT_ERROR("Device is not found in group %s", acg->acg_name);
766                 res = -EINVAL;
767                 goto out;
768         }
769
770         list_for_each_entry_safe(tgt_dev, tt, &dev->dev_tgt_dev_list,
771                          dev_tgt_dev_list_entry) {
772                 if (tgt_dev->acg_dev == acg_dev)
773                         scst_free_tgt_dev(tgt_dev);
774         }
775         scst_free_acg_dev(acg_dev);
776
777 out:
778         if (res == 0) {
779                 if (dev->virt_name != NULL) {
780                         PRINT_INFO("Removed device %s from group %s",
781                                 dev->virt_name, acg->acg_name);
782                 } else {
783                         PRINT_INFO("Removed device %d:%d:%d:%d from group %s",
784                                 dev->scsi_dev->host->host_no,
785                                 dev->scsi_dev->channel, dev->scsi_dev->id,
786                                 dev->scsi_dev->lun, acg->acg_name);
787                 }
788         }
789
790         TRACE_EXIT_RES(res);
791         return res;
792 }
793
794 /* scst_mutex supposed to be held */
795 int scst_acg_add_name(struct scst_acg *acg, const char *name)
796 {
797         int res = 0;
798         struct scst_acn *n;
799         int len;
800         char *nm;
801
802         TRACE_ENTRY();
803
804         list_for_each_entry(n, &acg->acn_list, acn_list_entry)
805         {
806                 if (strcmp(n->name, name) == 0) {
807                         PRINT_ERROR("Name %s already exists in group %s",
808                                 name, acg->acg_name);
809                         res = -EINVAL;
810                         goto out;
811                 }
812         }
813
814         n = kmalloc(sizeof(*n), GFP_KERNEL);
815         if (n == NULL) {
816                 PRINT_ERROR("%s", "Unable to allocate scst_acn");
817                 res = -ENOMEM;
818                 goto out;
819         }
820
821         len = strlen(name);
822         nm = kmalloc(len + 1, GFP_KERNEL);
823         if (nm == NULL) {
824                 PRINT_ERROR("%s", "Unable to allocate scst_acn->name");
825                 res = -ENOMEM;
826                 goto out_free;
827         }
828
829         strcpy(nm, name);
830         n->name = nm;
831
832         list_add_tail(&n->acn_list_entry, &acg->acn_list);
833
834 out:
835         if (res == 0)
836                 PRINT_INFO("Added name %s to group %s", name, acg->acg_name);
837
838         TRACE_EXIT_RES(res);
839         return res;
840
841 out_free:
842         kfree(n);
843         goto out;
844 }
845
846 /* scst_mutex supposed to be held */
847 int scst_acg_remove_name(struct scst_acg *acg, const char *name)
848 {
849         int res = -EINVAL;
850         struct scst_acn *n;
851
852         TRACE_ENTRY();
853
854         list_for_each_entry(n, &acg->acn_list, acn_list_entry)
855         {
856                 if (strcmp(n->name, name) == 0) {
857                         list_del(&n->acn_list_entry);
858                         kfree(n->name);
859                         kfree(n);
860                         res = 0;
861                         break;
862                 }
863         }
864
865         if (res == 0) {
866                 PRINT_INFO("Removed name %s from group %s", name,
867                         acg->acg_name);
868         } else {
869                 PRINT_ERROR("Unable to find name %s in group %s", name,
870                         acg->acg_name);
871         }
872
873         TRACE_EXIT_RES(res);
874         return res;
875 }
876
877 struct scst_cmd *scst_create_prepare_internal_cmd(
878         struct scst_cmd *orig_cmd, int bufsize)
879 {
880         struct scst_cmd *res;
881         int gfp_mask = scst_cmd_atomic(orig_cmd) ? GFP_ATOMIC : GFP_KERNEL;
882
883         TRACE_ENTRY();
884
885         res = scst_alloc_cmd(gfp_mask);
886         if (res == NULL)
887                 goto out;
888
889         res->cmd_lists = orig_cmd->cmd_lists;
890         res->sess = orig_cmd->sess;
891         res->state = SCST_CMD_STATE_DEV_PARSE;
892         res->atomic = scst_cmd_atomic(orig_cmd);
893         res->internal = 1;
894         res->tgtt = orig_cmd->tgtt;
895         res->tgt = orig_cmd->tgt;
896         res->dev = orig_cmd->dev;
897         res->tgt_dev = orig_cmd->tgt_dev;
898         res->lun = orig_cmd->lun;
899         res->queue_type = SCST_CMD_QUEUE_HEAD_OF_QUEUE;
900         res->data_direction = SCST_DATA_UNKNOWN;
901         res->orig_cmd = orig_cmd;
902
903         res->bufflen = bufsize;
904
905 out:
906         TRACE_EXIT_HRES((unsigned long)res);
907         return res;
908 }
909
910 void scst_free_internal_cmd(struct scst_cmd *cmd)
911 {
912         TRACE_ENTRY();
913
914         __scst_cmd_put(cmd);
915
916         TRACE_EXIT();
917         return;
918 }
919
920 int scst_prepare_request_sense(struct scst_cmd *orig_cmd)
921 {
922         int res = SCST_CMD_STATE_RES_CONT_NEXT;
923 #define sbuf_size 252
924         static const uint8_t request_sense[6] =
925             { REQUEST_SENSE, 0, 0, 0, sbuf_size, 0 };
926         struct scst_cmd *rs_cmd;
927
928         TRACE_ENTRY();
929
930         rs_cmd = scst_create_prepare_internal_cmd(orig_cmd, sbuf_size);
931         if (rs_cmd == NULL)
932                 goto out_error;
933
934         memcpy(rs_cmd->cdb, request_sense, sizeof(request_sense));
935         rs_cmd->cdb_len = sizeof(request_sense);
936         rs_cmd->data_direction = SCST_DATA_READ;
937
938         TRACE(TRACE_MGMT_MINOR, "Adding REQUEST SENSE cmd %p to head of active "
939                 "cmd list ", rs_cmd);
940         spin_lock_irq(&rs_cmd->cmd_lists->cmd_list_lock);
941         list_add(&rs_cmd->cmd_list_entry, &rs_cmd->cmd_lists->active_cmd_list);
942         spin_unlock_irq(&rs_cmd->cmd_lists->cmd_list_lock);
943
944 out:
945         TRACE_EXIT_RES(res);
946         return res;
947
948 out_error:
949         res = -1;
950         goto out;
951 #undef sbuf_size
952 }
953
954 struct scst_cmd *scst_complete_request_sense(struct scst_cmd *req_cmd)
955 {
956         struct scst_cmd *orig_cmd = req_cmd->orig_cmd;
957         uint8_t *buf;
958         int len;
959
960         TRACE_ENTRY();
961
962         if (req_cmd->dev->handler->dev_done != NULL) {
963                 int rc;
964                 TRACE_DBG("Calling dev handler %s dev_done(%p)",
965                       req_cmd->dev->handler->name, req_cmd);
966                 rc = req_cmd->dev->handler->dev_done(req_cmd);
967                 TRACE_DBG("Dev handler %s dev_done() returned %d",
968                       req_cmd->dev->handler->name, rc);
969         }
970
971         sBUG_ON(orig_cmd);
972
973         len = scst_get_buf_first(req_cmd, &buf);
974
975         if (scsi_status_is_good(req_cmd->status) && (len > 0) &&
976             SCST_SENSE_VALID(buf) && (!SCST_NO_SENSE(buf))) {
977                 PRINT_BUFF_FLAG(TRACE_SCSI, "REQUEST SENSE returned",
978                         buf, len);
979                 scst_alloc_set_sense(orig_cmd, scst_cmd_atomic(req_cmd), buf,
980                         len);
981         } else {
982                 PRINT_ERROR("%s", "Unable to get the sense via "
983                         "REQUEST SENSE, returning HARDWARE ERROR");
984                 scst_set_cmd_error(orig_cmd,
985                         SCST_LOAD_SENSE(scst_sense_hardw_error));
986         }
987
988         if (len > 0)
989                 scst_put_buf(req_cmd, buf);
990
991         scst_free_internal_cmd(req_cmd);
992
993         TRACE_EXIT_HRES((unsigned long)orig_cmd);
994         return orig_cmd;
995 }
996
997 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18)
998 static void scst_req_done(struct scsi_cmnd *scsi_cmd)
999 {
1000         struct scsi_request *req;
1001
1002         TRACE_ENTRY();
1003
1004         if (scsi_cmd && (req = scsi_cmd->sc_request)) {
1005                 if (req) {
1006                         if (req->sr_bufflen)
1007                                 kfree(req->sr_buffer);
1008                         scsi_release_request(req);
1009                 }
1010         }
1011
1012         TRACE_EXIT();
1013         return;
1014 }
1015
1016 static void scst_send_release(struct scst_device *dev)
1017 {
1018         struct scsi_request *req;
1019         struct scsi_device *scsi_dev;
1020         uint8_t cdb[6];
1021
1022         TRACE_ENTRY();
1023
1024         if (dev->scsi_dev == NULL)
1025                 goto out;
1026
1027         scsi_dev = dev->scsi_dev;
1028
1029         req = scsi_allocate_request(scsi_dev, GFP_KERNEL);
1030         if (req == NULL) {
1031                 PRINT_ERROR("Allocation of scsi_request failed: unable "
1032                             "to RELEASE device %d:%d:%d:%d",
1033                             scsi_dev->host->host_no, scsi_dev->channel,
1034                             scsi_dev->id, scsi_dev->lun);
1035                 goto out;
1036         }
1037
1038         memset(cdb, 0, sizeof(cdb));
1039         cdb[0] = RELEASE;
1040         cdb[1] = (scsi_dev->scsi_level <= SCSI_2) ?
1041             ((scsi_dev->lun << 5) & 0xe0) : 0;
1042         memcpy(req->sr_cmnd, cdb, sizeof(cdb));
1043         req->sr_cmd_len = sizeof(cdb);
1044         req->sr_data_direction = SCST_DATA_NONE;
1045         req->sr_use_sg = 0;
1046         req->sr_bufflen = 0;
1047         req->sr_buffer = NULL;
1048         req->sr_request->rq_disk = dev->rq_disk;
1049         req->sr_sense_buffer[0] = 0;
1050
1051         TRACE(TRACE_DEBUG | TRACE_SCSI, "Sending RELEASE req %p to SCSI "
1052                 "mid-level", req);
1053         scst_do_req(req, req->sr_cmnd, (void *)req->sr_buffer, req->sr_bufflen,
1054                     scst_req_done, SCST_DEFAULT_TIMEOUT, 3);
1055
1056 out:
1057         TRACE_EXIT();
1058         return;
1059 }
1060 #else /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18) */
1061 static void scst_send_release(struct scst_device *dev)
1062 {
1063         struct scsi_device *scsi_dev;
1064         unsigned char cdb[6];
1065         unsigned char *sense;
1066         int rc, i;
1067
1068         TRACE_ENTRY();
1069
1070         if (dev->scsi_dev == NULL)
1071                 goto out;
1072
1073         /* We can't afford missing RELEASE due to memory shortage */
1074         sense = kmalloc(SCST_SENSE_BUFFERSIZE, GFP_KERNEL|__GFP_NOFAIL);
1075
1076         scsi_dev = dev->scsi_dev;
1077
1078         for (i = 0; i < 5; i++) {
1079                 memset(cdb, 0, sizeof(cdb));
1080                 cdb[0] = RELEASE;
1081                 cdb[1] = (scsi_dev->scsi_level <= SCSI_2) ?
1082                     ((scsi_dev->lun << 5) & 0xe0) : 0;
1083
1084                 memset(sense, 0, SCST_SENSE_BUFFERSIZE);
1085
1086                 TRACE(TRACE_DEBUG | TRACE_SCSI, "%s", "Sending RELEASE req to "
1087                         "SCSI mid-level");
1088                 rc = scsi_execute(scsi_dev, cdb, SCST_DATA_NONE, NULL, 0,
1089                                 sense, SCST_DEFAULT_TIMEOUT, 0, 0);
1090                 TRACE_DBG("MODE_SENSE done: %x", rc);
1091
1092                 if (scsi_status_is_good(rc)) {
1093                         break;
1094                 } else {
1095                         PRINT_ERROR("RELEASE failed: %d", rc);
1096                         PRINT_BUFFER("RELEASE sense", sense,
1097                                 SCST_SENSE_BUFFERSIZE);
1098                         scst_check_internal_sense(dev, rc,
1099                                         sense, SCST_SENSE_BUFFERSIZE);
1100                 }
1101         }
1102
1103         kfree(sense);
1104
1105 out:
1106         TRACE_EXIT();
1107         return;
1108 }
1109 #endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18) */
1110
1111 /* scst_mutex supposed to be held */
1112 static void scst_clear_reservation(struct scst_tgt_dev *tgt_dev)
1113 {
1114         struct scst_device *dev = tgt_dev->dev;
1115         int release = 0;
1116
1117         TRACE_ENTRY();
1118
1119         spin_lock_bh(&dev->dev_lock);
1120         if (dev->dev_reserved &&
1121             !test_bit(SCST_TGT_DEV_RESERVED, &tgt_dev->tgt_dev_flags)) {
1122                 /* This is one who holds the reservation */
1123                 struct scst_tgt_dev *tgt_dev_tmp;
1124                 list_for_each_entry(tgt_dev_tmp, &dev->dev_tgt_dev_list,
1125                                     dev_tgt_dev_list_entry) {
1126                         clear_bit(SCST_TGT_DEV_RESERVED,
1127                                     &tgt_dev_tmp->tgt_dev_flags);
1128                 }
1129                 dev->dev_reserved = 0;
1130                 release = 1;
1131         }
1132         spin_unlock_bh(&dev->dev_lock);
1133
1134         if (release)
1135                 scst_send_release(dev);
1136
1137         TRACE_EXIT();
1138         return;
1139 }
1140
1141 struct scst_session *scst_alloc_session(struct scst_tgt *tgt, int gfp_mask,
1142         const char *initiator_name)
1143 {
1144         struct scst_session *sess;
1145         int i;
1146         int len;
1147         char *nm;
1148
1149         TRACE_ENTRY();
1150
1151 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 17)
1152         sess = kmem_cache_alloc(scst_sess_cachep, gfp_mask);
1153 #else
1154         sess = kmem_cache_zalloc(scst_sess_cachep, gfp_mask);
1155 #endif
1156         if (sess == NULL) {
1157                 TRACE(TRACE_OUT_OF_MEM, "%s",
1158                       "Allocation of scst_session failed");
1159                 goto out;
1160         }
1161 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 17)
1162         memset(sess, 0, sizeof(*sess));
1163 #endif
1164
1165         sess->init_phase = SCST_SESS_IPH_INITING;
1166         sess->shut_phase = SCST_SESS_SPH_READY;
1167         atomic_set(&sess->refcnt, 0);
1168         for (i = 0; i < TGT_DEV_HASH_SIZE; i++) {
1169                 struct list_head *sess_tgt_dev_list_head =
1170                          &sess->sess_tgt_dev_list_hash[i];
1171                 INIT_LIST_HEAD(sess_tgt_dev_list_head);
1172         }
1173         spin_lock_init(&sess->sess_list_lock);
1174         INIT_LIST_HEAD(&sess->search_cmd_list);
1175         sess->tgt = tgt;
1176         INIT_LIST_HEAD(&sess->init_deferred_cmd_list);
1177         INIT_LIST_HEAD(&sess->init_deferred_mcmd_list);
1178
1179 #ifdef MEASURE_LATENCY
1180         spin_lock_init(&sess->meas_lock);
1181 #endif
1182
1183         len = strlen(initiator_name);
1184         nm = kmalloc(len + 1, gfp_mask);
1185         if (nm == NULL) {
1186                 PRINT_ERROR("%s", "Unable to allocate sess->initiator_name");
1187                 goto out_free;
1188         }
1189
1190         strcpy(nm, initiator_name);
1191         sess->initiator_name = nm;
1192
1193 out:
1194         TRACE_EXIT();
1195         return sess;
1196
1197 out_free:
1198         kmem_cache_free(scst_sess_cachep, sess);
1199         sess = NULL;
1200         goto out;
1201 }
1202
1203 void scst_free_session(struct scst_session *sess)
1204 {
1205         TRACE_ENTRY();
1206
1207         mutex_lock(&scst_mutex);
1208
1209         TRACE_DBG("Removing sess %p from the list", sess);
1210         list_del(&sess->sess_list_entry);
1211         TRACE_DBG("Removing session %p from acg %s", sess, sess->acg->acg_name);
1212         list_del(&sess->acg_sess_list_entry);
1213
1214         scst_sess_free_tgt_devs(sess);
1215
1216         wake_up_all(&sess->tgt->unreg_waitQ);
1217
1218         mutex_unlock(&scst_mutex);
1219
1220         kfree(sess->initiator_name);
1221         kmem_cache_free(scst_sess_cachep, sess);
1222
1223         TRACE_EXIT();
1224         return;
1225 }
1226
1227 void scst_free_session_callback(struct scst_session *sess)
1228 {
1229         struct completion *c;
1230
1231         TRACE_ENTRY();
1232
1233         TRACE_DBG("Freeing session %p", sess);
1234
1235         c = sess->shutdown_compl;
1236
1237         if (sess->unreg_done_fn) {
1238                 TRACE_DBG("Calling unreg_done_fn(%p)", sess);
1239                 sess->unreg_done_fn(sess);
1240                 TRACE_DBG("%s", "unreg_done_fn() returned");
1241         }
1242         scst_free_session(sess);
1243
1244         if (c)
1245                 complete_all(c);
1246
1247         TRACE_EXIT();
1248         return;
1249 }
1250
1251 void scst_sched_session_free(struct scst_session *sess)
1252 {
1253         unsigned long flags;
1254
1255         TRACE_ENTRY();
1256
1257         spin_lock_irqsave(&scst_mgmt_lock, flags);
1258         TRACE_DBG("Adding sess %p to scst_sess_shut_list", sess);
1259         list_add_tail(&sess->sess_shut_list_entry, &scst_sess_shut_list);
1260         spin_unlock_irqrestore(&scst_mgmt_lock, flags);
1261
1262         wake_up(&scst_mgmt_waitQ);
1263
1264         TRACE_EXIT();
1265         return;
1266 }
1267
1268 void scst_cmd_get(struct scst_cmd *cmd)
1269 {
1270         __scst_cmd_get(cmd);
1271 }
1272
1273 void scst_cmd_put(struct scst_cmd *cmd)
1274 {
1275         __scst_cmd_put(cmd);
1276 }
1277
1278 struct scst_cmd *scst_alloc_cmd(int gfp_mask)
1279 {
1280         struct scst_cmd *cmd;
1281
1282         TRACE_ENTRY();
1283
1284 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 17)
1285         cmd = kmem_cache_alloc(scst_cmd_cachep, gfp_mask);
1286 #else
1287         cmd = kmem_cache_zalloc(scst_cmd_cachep, gfp_mask);
1288 #endif
1289         if (cmd == NULL) {
1290                 TRACE(TRACE_OUT_OF_MEM, "%s", "Allocation of scst_cmd failed");
1291                 goto out;
1292         }
1293 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 17)
1294         memset(cmd, 0, sizeof(*cmd));
1295 #endif
1296
1297         cmd->state = SCST_CMD_STATE_INIT_WAIT;
1298         atomic_set(&cmd->cmd_ref, 1);
1299         cmd->cmd_lists = &scst_main_cmd_lists;
1300         INIT_LIST_HEAD(&cmd->mgmt_cmd_list);
1301         cmd->queue_type = SCST_CMD_QUEUE_SIMPLE;
1302         cmd->timeout = SCST_DEFAULT_TIMEOUT;
1303         cmd->retries = 0;
1304         cmd->data_len = -1;
1305         cmd->is_send_status = 1;
1306         cmd->resp_data_len = -1;
1307
1308 out:
1309         TRACE_EXIT();
1310         return cmd;
1311 }
1312
1313 void scst_destroy_put_cmd(struct scst_cmd *cmd)
1314 {
1315         scst_sess_put(cmd->sess);
1316
1317         /* At this point tgt_dev can be dead, but the pointer remains not-NULL */
1318         if (likely(cmd->tgt_dev != NULL))
1319                 __scst_put();
1320
1321         scst_destroy_cmd(cmd);
1322         return;
1323 }
1324
1325 /* No locks supposed to be held */
1326 void scst_free_cmd(struct scst_cmd *cmd)
1327 {
1328         int destroy = 1;
1329
1330         TRACE_ENTRY();
1331
1332         TRACE_DBG("Freeing cmd %p (tag %Lu)", cmd, cmd->tag);
1333
1334         if (unlikely(test_bit(SCST_CMD_ABORTED, &cmd->cmd_flags))) {
1335                 TRACE_MGMT_DBG("Freeing aborted cmd %p (scst_cmd_count %d)",
1336                         cmd, atomic_read(&scst_cmd_count));
1337         }
1338
1339         sBUG_ON(cmd->inc_blocking || cmd->needs_unblocking ||
1340                 cmd->dec_on_dev_needed);
1341
1342 #if defined(EXTRACHECKS) && (LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18))
1343         if (cmd->scsi_req) {
1344                 PRINT_ERROR("%s: %s", __FUNCTION__, "Cmd with unfreed "
1345                         "scsi_req!");
1346                 scst_release_request(cmd);
1347         }
1348 #endif
1349
1350         scst_check_restore_sg_buff(cmd);
1351
1352         if (unlikely(cmd->internal)) {
1353                 if (cmd->bufflen > 0)
1354                         scst_release_space(cmd);
1355                 scst_destroy_cmd(cmd);
1356                 goto out;
1357         }
1358
1359         if (cmd->tgtt->on_free_cmd != NULL) {
1360                 TRACE_DBG("Calling target's on_free_cmd(%p)", cmd);
1361                 cmd->tgtt->on_free_cmd(cmd);
1362                 TRACE_DBG("%s", "Target's on_free_cmd() returned");
1363         }
1364
1365         if (likely(cmd->dev != NULL)) {
1366                 struct scst_dev_type *handler = cmd->dev->handler;
1367                 if (handler->on_free_cmd != NULL) {
1368                         TRACE_DBG("Calling dev handler %s on_free_cmd(%p)",
1369                               handler->name, cmd);
1370                         handler->on_free_cmd(cmd);
1371                         TRACE_DBG("Dev handler %s on_free_cmd() returned",
1372                                 handler->name);
1373                 }
1374         }
1375
1376         scst_release_space(cmd);
1377
1378         if (unlikely(cmd->sense != NULL)) {
1379                 TRACE_MEM("Releasing sense %p (cmd %p)", cmd->sense, cmd);
1380                 mempool_free(cmd->sense, scst_sense_mempool);
1381                 cmd->sense = NULL;
1382         }
1383
1384         if (likely(cmd->tgt_dev != NULL)) {
1385 #ifdef EXTRACHECKS
1386                 if (unlikely(!cmd->sent_to_midlev)) {
1387                         PRINT_ERROR("Finishing not executed cmd %p (opcode "
1388                              "%d, target %s, lun %Ld, sn %ld, expected_sn %ld)",
1389                              cmd, cmd->cdb[0], cmd->tgtt->name, (uint64_t)cmd->lun,
1390                              cmd->sn, cmd->tgt_dev->expected_sn);
1391                         scst_unblock_deferred(cmd->tgt_dev, cmd);
1392                 }
1393 #endif
1394
1395                 if (unlikely(cmd->out_of_sn)) {
1396                         TRACE_SN("Out of SN cmd %p (tag %llu, sn %ld), "
1397                                 "destroy=%d", cmd, cmd->tag, cmd->sn, destroy);
1398                         destroy = test_and_set_bit(SCST_CMD_CAN_BE_DESTROYED,
1399                                         &cmd->cmd_flags);
1400                 }
1401         }
1402
1403         if (likely(destroy))
1404                 scst_destroy_put_cmd(cmd);
1405
1406 out:
1407         TRACE_EXIT();
1408         return;
1409 }
1410
1411 /* No locks supposed to be held. */
1412 void scst_check_retries(struct scst_tgt *tgt)
1413 {
1414         int need_wake_up = 0;
1415
1416         TRACE_ENTRY();
1417
1418         /*
1419          * We don't worry about overflow of finished_cmds, because we check
1420          * only for its change
1421          */
1422         atomic_inc(&tgt->finished_cmds);
1423         smp_mb__after_atomic_inc();
1424         if (unlikely(tgt->retry_cmds > 0)) {
1425                 struct scst_cmd *c, *tc;
1426                 unsigned long flags;
1427
1428                 TRACE_RETRY("Checking retry cmd list (retry_cmds %d)",
1429                       tgt->retry_cmds);
1430
1431                 spin_lock_irqsave(&tgt->tgt_lock, flags);
1432                 list_for_each_entry_safe(c, tc, &tgt->retry_cmd_list,
1433                                 cmd_list_entry)
1434                 {
1435                         tgt->retry_cmds--;
1436
1437                         TRACE_RETRY("Moving retry cmd %p to head of active "
1438                                 "cmd list (retry_cmds left %d)", c, tgt->retry_cmds);
1439                         spin_lock(&c->cmd_lists->cmd_list_lock);
1440                         list_move(&c->cmd_list_entry, &c->cmd_lists->active_cmd_list);
1441                         wake_up(&c->cmd_lists->cmd_list_waitQ);
1442                         spin_unlock(&c->cmd_lists->cmd_list_lock);
1443
1444                         need_wake_up++;
1445                         if (need_wake_up >= 2) /* "slow start" */
1446                                 break;
1447                 }
1448                 spin_unlock_irqrestore(&tgt->tgt_lock, flags);
1449         }
1450
1451         TRACE_EXIT();
1452         return;
1453 }
1454
1455 void scst_tgt_retry_timer_fn(unsigned long arg)
1456 {
1457         struct scst_tgt *tgt = (struct scst_tgt *)arg;
1458         unsigned long flags;
1459
1460         TRACE_RETRY("Retry timer expired (retry_cmds %d)", tgt->retry_cmds);
1461
1462         spin_lock_irqsave(&tgt->tgt_lock, flags);
1463         tgt->retry_timer_active = 0;
1464         spin_unlock_irqrestore(&tgt->tgt_lock, flags);
1465
1466         scst_check_retries(tgt);
1467
1468         TRACE_EXIT();
1469         return;
1470 }
1471
1472 struct scst_mgmt_cmd *scst_alloc_mgmt_cmd(int gfp_mask)
1473 {
1474         struct scst_mgmt_cmd *mcmd;
1475
1476         TRACE_ENTRY();
1477
1478         mcmd = mempool_alloc(scst_mgmt_mempool, gfp_mask);
1479         if (mcmd == NULL) {
1480                 PRINT_CRIT_ERROR("%s", "Allocation of management command "
1481                         "failed, some commands and their data could leak");
1482                 goto out;
1483         }
1484         memset(mcmd, 0, sizeof(*mcmd));
1485
1486 out:
1487         TRACE_EXIT();
1488         return mcmd;
1489 }
1490
1491 void scst_free_mgmt_cmd(struct scst_mgmt_cmd *mcmd)
1492 {
1493         unsigned long flags;
1494
1495         TRACE_ENTRY();
1496
1497         spin_lock_irqsave(&mcmd->sess->sess_list_lock, flags);
1498         atomic_dec(&mcmd->sess->sess_cmd_count);
1499         spin_unlock_irqrestore(&mcmd->sess->sess_list_lock, flags);
1500
1501         scst_sess_put(mcmd->sess);
1502
1503         if (mcmd->mcmd_tgt_dev != NULL)
1504                 __scst_put();
1505
1506         mempool_free(mcmd, scst_mgmt_mempool);
1507
1508         TRACE_EXIT();
1509         return;
1510 }
1511
1512 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18)
1513 int scst_alloc_request(struct scst_cmd *cmd)
1514 {
1515         int res = 0;
1516         struct scsi_request *req;
1517         int gm = scst_cmd_atomic(cmd) ? GFP_ATOMIC : GFP_KERNEL;
1518
1519         TRACE_ENTRY();
1520
1521         /* cmd->dev->scsi_dev must be non-NULL here */
1522         req = scsi_allocate_request(cmd->dev->scsi_dev, gm);
1523         if (req == NULL) {
1524                 TRACE(TRACE_OUT_OF_MEM, "%s",
1525                       "Allocation of scsi_request failed");
1526                 res = -ENOMEM;
1527                 goto out;
1528         }
1529
1530         cmd->scsi_req = req;
1531
1532         memcpy(req->sr_cmnd, cmd->cdb, cmd->cdb_len);
1533         req->sr_cmd_len = cmd->cdb_len;
1534         req->sr_data_direction = cmd->data_direction;
1535         req->sr_use_sg = cmd->sg_cnt;
1536         req->sr_bufflen = cmd->bufflen;
1537         req->sr_buffer = cmd->sg;
1538         req->sr_request->rq_disk = cmd->dev->rq_disk;
1539         req->sr_sense_buffer[0] = 0;
1540
1541         cmd->scsi_req->upper_private_data = cmd;
1542
1543 out:
1544         TRACE_EXIT();
1545         return res;
1546 }
1547
1548 void scst_release_request(struct scst_cmd *cmd)
1549 {
1550         scsi_release_request(cmd->scsi_req);
1551         cmd->scsi_req = NULL;
1552 }
1553 #endif
1554
1555 int scst_alloc_space(struct scst_cmd *cmd)
1556 {
1557         int gfp_mask;
1558         int res = -ENOMEM;
1559         int atomic = scst_cmd_atomic(cmd);
1560         int flags;
1561         struct scst_tgt_dev *tgt_dev = cmd->tgt_dev;
1562         int bufflen = cmd->bufflen;
1563
1564         TRACE_ENTRY();
1565
1566         gfp_mask = tgt_dev->gfp_mask | (atomic ? GFP_ATOMIC : GFP_KERNEL);
1567
1568         flags = atomic ? SCST_POOL_NO_ALLOC_ON_CACHE_MISS : 0;
1569         if (cmd->no_sgv)
1570                 flags |= SCST_POOL_ALLOC_NO_CACHED;
1571
1572         if (unlikely(cmd->bufflen == 0)) {
1573                 TRACE(TRACE_MGMT_MINOR, "Warning: data direction %d or/and "
1574                         "zero buffer length. Opcode 0x%x, handler %s, target "
1575                         "%s", cmd->data_direction, cmd->cdb[0],
1576                         cmd->dev->handler->name, cmd->tgtt->name);
1577                 /*
1578                  * Be on the safe side and alloc stub buffer. Neither target
1579                  * drivers, nor user space will touch it, since bufflen
1580                  * remains 0.
1581                  */
1582                 bufflen = PAGE_SIZE;
1583         }
1584
1585         cmd->sg = sgv_pool_alloc(tgt_dev->pool, bufflen, gfp_mask, flags,
1586                         &cmd->sg_cnt, &cmd->sgv, NULL);
1587         if (cmd->sg == NULL)
1588                 goto out;
1589
1590         if (unlikely(cmd->sg_cnt > tgt_dev->max_sg_cnt)) {
1591                 static int ll;
1592                 if (ll < 10) {
1593                         PRINT_INFO("Unable to complete command due to "
1594                                 "SG IO count limitation (requested %d, "
1595                                 "available %d, tgt lim %d)", cmd->sg_cnt,
1596                                 tgt_dev->max_sg_cnt, cmd->tgt->sg_tablesize);
1597                         ll++;
1598                 }
1599                 goto out_sg_free;
1600         }
1601
1602         res = 0;
1603
1604 out:
1605         TRACE_EXIT();
1606         return res;
1607
1608 out_sg_free:
1609         sgv_pool_free(cmd->sgv);
1610         cmd->sgv = NULL;
1611         cmd->sg = NULL;
1612         cmd->sg_cnt = 0;
1613         goto out;
1614 }
1615
1616 void scst_release_space(struct scst_cmd *cmd)
1617 {
1618         TRACE_ENTRY();
1619
1620         if (cmd->sgv == NULL)
1621                 goto out;
1622
1623         if (cmd->data_buf_alloced) {
1624                 TRACE_MEM("%s", "data_buf_alloced set, returning");
1625                 goto out;
1626         }
1627
1628         sgv_pool_free(cmd->sgv);
1629
1630         cmd->sgv = NULL;
1631         cmd->sg_cnt = 0;
1632         cmd->sg = NULL;
1633         cmd->bufflen = 0;
1634         cmd->data_len = 0;
1635
1636 out:
1637         TRACE_EXIT();
1638         return;
1639 }
1640
1641 static const int SCST_CDB_LENGTH[8] = { 6, 10, 10, -1, 16, 12, -1, -1 };
1642
1643 #define SCST_CDB_GROUP(opcode)   ((opcode >> 5) & 0x7)
1644 #define SCST_GET_CDB_LEN(opcode) SCST_CDB_LENGTH[SCST_CDB_GROUP(opcode)]
1645
1646 int scst_get_cdb_len(const uint8_t *cdb)
1647 {
1648         return SCST_GET_CDB_LEN(cdb[0]);
1649 }
1650
1651 /* get_trans_len_x extract x bytes from cdb as length starting from off */
1652
1653 /* for special commands */
1654 static int get_trans_len_block_limit(struct scst_cmd *cmd, uint8_t off)
1655 {
1656         cmd->bufflen = 6;
1657         return 0;
1658 }
1659
1660 static int get_trans_len_read_capacity(struct scst_cmd *cmd, uint8_t off)
1661 {
1662         cmd->bufflen = READ_CAP_LEN;
1663         return 0;
1664 }
1665
1666 static int get_trans_len_single(struct scst_cmd *cmd, uint8_t off)
1667 {
1668         cmd->bufflen = 1;
1669         return 0;
1670 }
1671
1672 static int get_trans_len_read_pos(struct scst_cmd *cmd, uint8_t off)
1673 {
1674         uint8_t *p = (uint8_t *)cmd->cdb + off;
1675         int res = 0;
1676
1677         cmd->bufflen = 0;
1678         cmd->bufflen |= ((u32)p[0]) << 8;
1679         cmd->bufflen |= ((u32)p[1]);
1680
1681         switch (cmd->cdb[1] & 0x1f) {
1682         case 0:
1683         case 1:
1684         case 6:
1685                 if (cmd->bufflen != 0) {
1686                         PRINT_ERROR("READ POSITION: Invalid non-zero (%d) "
1687                                 "allocation length for service action %x",
1688                                 cmd->bufflen, cmd->cdb[1] & 0x1f);
1689                         goto out_inval;
1690                 }
1691                 break;
1692         }
1693
1694         switch (cmd->cdb[1] & 0x1f) {
1695         case 0:
1696         case 1:
1697                 cmd->bufflen = 20;
1698                 break;
1699         case 6:
1700                 cmd->bufflen = 32;
1701                 break;
1702         case 8:
1703                 cmd->bufflen = max(28, cmd->bufflen);
1704                 break;
1705         default:
1706                 PRINT_ERROR("READ POSITION: Invalid service action %x",
1707                         cmd->cdb[1] & 0x1f);
1708                 goto out_inval;
1709         }
1710
1711 out:
1712         return res;
1713
1714 out_inval:
1715         scst_set_cmd_error(cmd,
1716                 SCST_LOAD_SENSE(scst_sense_invalid_field_in_cdb));
1717         res = 1;
1718         goto out;
1719 }
1720
1721 static int get_trans_len_1(struct scst_cmd *cmd, uint8_t off)
1722 {
1723         cmd->bufflen = (u32)cmd->cdb[off];
1724         return 0;
1725 }
1726
1727 static int get_trans_len_2(struct scst_cmd *cmd, uint8_t off)
1728 {
1729         const uint8_t *p = cmd->cdb + off;
1730
1731         cmd->bufflen = 0;
1732         cmd->bufflen |= ((u32)p[0]) << 8;
1733         cmd->bufflen |= ((u32)p[1]);
1734
1735         return 0;
1736 }
1737
1738 static int get_trans_len_3(struct scst_cmd *cmd, uint8_t off)
1739 {
1740         const uint8_t *p = cmd->cdb + off;
1741
1742         cmd->bufflen = 0;
1743         cmd->bufflen |= ((u32)p[0]) << 16;
1744         cmd->bufflen |= ((u32)p[1]) << 8;
1745         cmd->bufflen |= ((u32)p[2]);
1746
1747         return 0;
1748 }
1749
1750 static int get_trans_len_4(struct scst_cmd *cmd, uint8_t off)
1751 {
1752         const uint8_t *p = cmd->cdb + off;
1753
1754         cmd->bufflen = 0;
1755         cmd->bufflen |= ((u32)p[0]) << 24;
1756         cmd->bufflen |= ((u32)p[1]) << 16;
1757         cmd->bufflen |= ((u32)p[2]) << 8;
1758         cmd->bufflen |= ((u32)p[3]);
1759
1760         return 0;
1761 }
1762
1763 static int get_trans_len_none(struct scst_cmd *cmd, uint8_t off)
1764 {
1765         cmd->bufflen = 0;
1766         return 0;
1767 }
1768
1769 int scst_get_cdb_info(struct scst_cmd *cmd)
1770 {
1771         int dev_type = cmd->dev->handler->type;
1772         int i, res = 0;
1773         uint8_t op;
1774         const struct scst_sdbops *ptr = NULL;
1775
1776         TRACE_ENTRY();
1777
1778         op = cmd->cdb[0];       /* get clear opcode */
1779
1780         TRACE_DBG("opcode=%02x, cdblen=%d bytes, tblsize=%d, "
1781                 "dev_type=%d", op, SCST_GET_CDB_LEN(op), SCST_CDB_TBL_SIZE,
1782                 dev_type);
1783
1784         i = scst_scsi_op_list[op];
1785         while (i < SCST_CDB_TBL_SIZE && scst_scsi_op_table[i].ops == op) {
1786                 if (scst_scsi_op_table[i].devkey[dev_type] != SCST_CDB_NOTSUPP) {
1787                         ptr = &scst_scsi_op_table[i];
1788                         TRACE_DBG("op = 0x%02x+'%c%c%c%c%c%c%c%c%c%c'+<%s>",
1789                               ptr->ops, ptr->devkey[0], /* disk     */
1790                               ptr->devkey[1],   /* tape     */
1791                               ptr->devkey[2],   /* printer */
1792                               ptr->devkey[3],   /* cpu      */
1793                               ptr->devkey[4],   /* cdr      */
1794                               ptr->devkey[5],   /* cdrom    */
1795                               ptr->devkey[6],   /* scanner */
1796                               ptr->devkey[7],   /* worm     */
1797                               ptr->devkey[8],   /* changer */
1798                               ptr->devkey[9],   /* commdev */
1799                               ptr->op_name);
1800                         TRACE_DBG("direction=%d flags=%d off=%d",
1801                               ptr->direction,
1802                               ptr->flags,
1803                               ptr->off);
1804                         break;
1805                 }
1806                 i++;
1807         }
1808
1809         if (ptr == NULL) {
1810                 /* opcode not found or now not used !!! */
1811                 TRACE(TRACE_SCSI, "Unknown opcode 0x%x for type %d", op,
1812                       dev_type);
1813                 res = -1;
1814                 cmd->op_flags = SCST_INFO_INVALID;
1815                 goto out;
1816         }
1817
1818         cmd->cdb_len = SCST_GET_CDB_LEN(op);
1819         cmd->op_name = ptr->op_name;
1820         cmd->data_direction = ptr->direction;
1821         cmd->op_flags = ptr->flags;
1822         res = (*ptr->get_trans_len)(cmd, ptr->off);
1823
1824 out:
1825         TRACE_EXIT();
1826         return res;
1827 }
1828
1829 /*
1830  * Routine to extract a lun number from an 8-byte LUN structure
1831  * in network byte order (BE).
1832  * (see SAM-2, Section 4.12.3 page 40)
1833  * Supports 2 types of lun unpacking: peripheral and logical unit.
1834  */
1835 lun_t scst_unpack_lun(const uint8_t *lun, int len)
1836 {
1837         lun_t res = (lun_t)-1;
1838         int address_method;
1839
1840         TRACE_ENTRY();
1841
1842         TRACE_BUFF_FLAG(TRACE_DEBUG, "Raw LUN", lun, len);
1843
1844         if (unlikely(len < 2)) {
1845                 PRINT_ERROR("Illegal lun length %d, expected 2 bytes or "
1846                         "more", len);
1847                 goto out;
1848         }
1849
1850         if (len > 2) {
1851                 switch (len) {
1852                 case 8:
1853                         if ((*((uint64_t *)lun) &
1854                           __constant_cpu_to_be64(0x0000FFFFFFFFFFFFLL)) != 0)
1855                                 goto out_err;
1856                         break;
1857                 case 4:
1858                         if (*((uint16_t *)&lun[2]) != 0)
1859                                 goto out_err;
1860                         break;
1861                 case 6:
1862                         if (*((uint32_t *)&lun[2]) != 0)
1863                                 goto out_err;
1864                         break;
1865                 default:
1866                         goto out_err;
1867                 }
1868         }
1869
1870         address_method = (*lun) >> 6;   /* high 2 bits of byte 0 */
1871         switch (address_method) {
1872         case 0: /* peripheral device addressing method */
1873 #if 0 /* Looks like it's legal to use it as flat space addressing method as well */
1874                 if (*lun) {
1875                         PRINT_ERROR("Illegal BUS INDENTIFIER in LUN "
1876                              "peripheral device addressing method 0x%02x, "
1877                              "expected 0", *lun);
1878                         break;
1879                 }
1880                 res = *(lun + 1);
1881                 break;
1882 #else
1883                 /* go through */
1884 #endif
1885
1886         case 1: /* flat space addressing method */
1887                 res = *(lun + 1) | (((*lun) & 0x3f) << 8);
1888                 break;
1889
1890         case 2: /* logical unit addressing method */
1891                 if (*lun & 0x3f) {
1892                         PRINT_ERROR("Illegal BUS NUMBER in LUN logical unit "
1893                                     "addressing method 0x%02x, expected 0",
1894                                     *lun & 0x3f);
1895                         break;
1896                 }
1897                 if (*(lun + 1) & 0xe0) {
1898                         PRINT_ERROR("Illegal TARGET in LUN logical unit "
1899                                     "addressing method 0x%02x, expected 0",
1900                                     (*(lun + 1) & 0xf8) >> 5);
1901                         break;
1902                 }
1903                 res = *(lun + 1) & 0x1f;
1904                 break;
1905
1906         case 3: /* extended logical unit addressing method */
1907         default:
1908                 PRINT_ERROR("Unimplemented LUN addressing method %u",
1909                             address_method);
1910                 break;
1911         }
1912
1913 out:
1914         TRACE_EXIT_RES((int)res);
1915         return res;
1916
1917 out_err:
1918         PRINT_ERROR("%s", "Multi-level LUN unimplemented");
1919         goto out;
1920 }
1921
1922 int scst_calc_block_shift(int sector_size)
1923 {
1924         int block_shift = 0;
1925         int t;
1926
1927         if (sector_size == 0)
1928                 sector_size = 512;
1929
1930         t = sector_size;
1931         while (1) {
1932                 if ((t & 1) != 0)
1933                         break;
1934                 t >>= 1;
1935                 block_shift++;
1936         }
1937         if (block_shift < 9) {
1938                 PRINT_ERROR("Wrong sector size %d", sector_size);
1939                 block_shift = -1;
1940         }
1941
1942         TRACE_EXIT_RES(block_shift);
1943         return block_shift;
1944 }
1945
1946 int scst_sbc_generic_parse(struct scst_cmd *cmd,
1947         int (*get_block_shift)(struct scst_cmd *cmd))
1948 {
1949         int res = 0;
1950
1951         TRACE_ENTRY();
1952
1953         /*
1954          * SCST sets good defaults for cmd->data_direction and cmd->bufflen,
1955          * therefore change them only if necessary
1956          */
1957
1958         TRACE_DBG("op_name <%s> direct %d flags %d transfer_len %d",
1959               cmd->op_name, cmd->data_direction, cmd->op_flags, cmd->bufflen);
1960
1961         switch (cmd->cdb[0]) {
1962         case SERVICE_ACTION_IN:
1963                 if ((cmd->cdb[1] & 0x1f) == SAI_READ_CAPACITY_16) {
1964                         cmd->bufflen = READ_CAP16_LEN;
1965                         cmd->data_direction = SCST_DATA_READ;
1966                 }
1967                 break;
1968         case VERIFY_6:
1969         case VERIFY:
1970         case VERIFY_12:
1971         case VERIFY_16:
1972                 if ((cmd->cdb[1] & BYTCHK) == 0) {
1973                         cmd->data_len = cmd->bufflen << get_block_shift(cmd);
1974                         cmd->bufflen = 0;
1975                         goto out;
1976                 } else
1977                         cmd->data_len = 0;
1978                 break;
1979         default:
1980                 /* It's all good */
1981                 break;
1982         }
1983
1984         if (cmd->op_flags & SCST_TRANSFER_LEN_TYPE_FIXED) {
1985                 /*
1986                  * No need for locks here, since *_detach() can not be
1987                  * called, when there are existing commands.
1988                  */
1989                 cmd->bufflen = cmd->bufflen << get_block_shift(cmd);
1990         }
1991
1992 out:
1993         TRACE_DBG("res %d, bufflen %d, data_len %d, direct %d",
1994               res, cmd->bufflen, cmd->data_len, cmd->data_direction);
1995
1996         TRACE_EXIT_RES(res);
1997         return res;
1998 }
1999
2000 int scst_cdrom_generic_parse(struct scst_cmd *cmd,
2001         int (*get_block_shift)(struct scst_cmd *cmd))
2002 {
2003         int res = 0;
2004
2005         TRACE_ENTRY();
2006
2007         /*
2008          * SCST sets good defaults for cmd->data_direction and cmd->bufflen,
2009          * therefore change them only if necessary
2010          */
2011
2012         TRACE_DBG("op_name <%s> direct %d flags %d transfer_len %d",
2013               cmd->op_name, cmd->data_direction, cmd->op_flags, cmd->bufflen);
2014
2015         cmd->cdb[1] &= 0x1f;
2016
2017         switch (cmd->cdb[0]) {
2018         case VERIFY_6:
2019         case VERIFY:
2020         case VERIFY_12:
2021         case VERIFY_16:
2022                 if ((cmd->cdb[1] & BYTCHK) == 0) {
2023                         cmd->data_len = cmd->bufflen << get_block_shift(cmd);
2024                         cmd->bufflen = 0;
2025                         goto out;
2026                 }
2027                 break;
2028         default:
2029                 /* It's all good */
2030                 break;
2031         }
2032
2033         if (cmd->op_flags & SCST_TRANSFER_LEN_TYPE_FIXED)
2034                 cmd->bufflen = cmd->bufflen << get_block_shift(cmd);
2035
2036 out:
2037         TRACE_DBG("res=%d, bufflen=%d, direct=%d", res, cmd->bufflen,
2038                 cmd->data_direction);
2039
2040         TRACE_EXIT();
2041         return res;
2042 }
2043
2044 int scst_modisk_generic_parse(struct scst_cmd *cmd,
2045         int (*get_block_shift)(struct scst_cmd *cmd))
2046 {
2047         int res = 0;
2048
2049         TRACE_ENTRY();
2050
2051         /*
2052          * SCST sets good defaults for cmd->data_direction and cmd->bufflen,
2053          * therefore change them only if necessary
2054          */
2055
2056         TRACE_DBG("op_name <%s> direct %d flags %d transfer_len %d",
2057               cmd->op_name, cmd->data_direction, cmd->op_flags, cmd->bufflen);
2058
2059         cmd->cdb[1] &= 0x1f;
2060
2061         switch (cmd->cdb[0]) {
2062         case VERIFY_6:
2063         case VERIFY:
2064         case VERIFY_12:
2065         case VERIFY_16:
2066                 if ((cmd->cdb[1] & BYTCHK) == 0) {
2067                         cmd->data_len = cmd->bufflen << get_block_shift(cmd);
2068                         cmd->bufflen = 0;
2069                         goto out;
2070                 }
2071                 break;
2072         default:
2073                 /* It's all good */
2074                 break;
2075         }
2076
2077         if (cmd->op_flags & SCST_TRANSFER_LEN_TYPE_FIXED)
2078                 cmd->bufflen = cmd->bufflen << get_block_shift(cmd);
2079
2080 out:
2081         TRACE_DBG("res=%d, bufflen=%d, direct=%d", res, cmd->bufflen,
2082                 cmd->data_direction);
2083
2084         TRACE_EXIT_RES(res);
2085         return res;
2086 }
2087
2088 int scst_tape_generic_parse(struct scst_cmd *cmd,
2089         int (*get_block_size)(struct scst_cmd *cmd))
2090 {
2091         int res = 0;
2092
2093         TRACE_ENTRY();
2094
2095         /*
2096          * SCST sets good defaults for cmd->data_direction and cmd->bufflen,
2097          * therefore change them only if necessary
2098          */
2099
2100         TRACE_DBG("op_name <%s> direct %d flags %d transfer_len %d",
2101               cmd->op_name, cmd->data_direction, cmd->op_flags, cmd->bufflen);
2102
2103         if (cmd->cdb[0] == READ_POSITION) {
2104                 int tclp = cmd->cdb[1] & TCLP_BIT;
2105                 int long_bit = cmd->cdb[1] & LONG_BIT;
2106                 int bt = cmd->cdb[1] & BT_BIT;
2107
2108                 if ((tclp == long_bit) && (!bt || !long_bit)) {
2109                         cmd->bufflen =
2110                             tclp ? POSITION_LEN_LONG : POSITION_LEN_SHORT;
2111                         cmd->data_direction = SCST_DATA_READ;
2112                 } else {
2113                         cmd->bufflen = 0;
2114                         cmd->data_direction = SCST_DATA_NONE;
2115                 }
2116         }
2117
2118         if (cmd->op_flags & SCST_TRANSFER_LEN_TYPE_FIXED & cmd->cdb[1])
2119                 cmd->bufflen = cmd->bufflen * get_block_size(cmd);
2120
2121         TRACE_EXIT_RES(res);
2122         return res;
2123 }
2124
2125 static int scst_null_parse(struct scst_cmd *cmd)
2126 {
2127         int res = 0;
2128
2129         TRACE_ENTRY();
2130
2131         /*
2132          * SCST sets good defaults for cmd->data_direction and cmd->bufflen,
2133          * therefore change them only if necessary
2134          */
2135
2136         TRACE_DBG("op_name <%s> direct %d flags %d transfer_len %d",
2137               cmd->op_name, cmd->data_direction, cmd->op_flags, cmd->bufflen);
2138 #if 0
2139         switch (cmd->cdb[0]) {
2140         default:
2141                 /* It's all good */
2142                 break;
2143         }
2144 #endif
2145         TRACE_DBG("res %d bufflen %d direct %d",
2146               res, cmd->bufflen, cmd->data_direction);
2147
2148         TRACE_EXIT();
2149         return res;
2150 }
2151
2152 int scst_changer_generic_parse(struct scst_cmd *cmd,
2153         int (*nothing)(struct scst_cmd *cmd))
2154 {
2155         return scst_null_parse(cmd);
2156 }
2157
2158 int scst_processor_generic_parse(struct scst_cmd *cmd,
2159         int (*nothing)(struct scst_cmd *cmd))
2160 {
2161         return scst_null_parse(cmd);
2162 }
2163
2164 int scst_raid_generic_parse(struct scst_cmd *cmd,
2165         int (*nothing)(struct scst_cmd *cmd))
2166 {
2167         return scst_null_parse(cmd);
2168 }
2169
2170 int scst_block_generic_dev_done(struct scst_cmd *cmd,
2171         void (*set_block_shift)(struct scst_cmd *cmd, int block_shift))
2172 {
2173         int opcode = cmd->cdb[0];
2174         int status = cmd->status;
2175         int res = SCST_CMD_STATE_DEFAULT;
2176
2177         TRACE_ENTRY();
2178
2179         /*
2180          * SCST sets good defaults for cmd->is_send_status and cmd->resp_data_len
2181          * based on cmd->status and cmd->data_direction, therefore change
2182          * them only if necessary
2183          */
2184
2185         if ((status == SAM_STAT_GOOD) || (status == SAM_STAT_CONDITION_MET)) {
2186                 switch (opcode) {
2187                 case READ_CAPACITY:
2188                 {
2189                         /* Always keep track of disk capacity */
2190                         int buffer_size, sector_size, sh;
2191                         uint8_t *buffer;
2192
2193                         buffer_size = scst_get_buf_first(cmd, &buffer);
2194                         if (unlikely(buffer_size <= 0)) {
2195                                 PRINT_ERROR("%s: Unable to get the buffer "
2196                                         "(%d)", __FUNCTION__, buffer_size);
2197                                 goto out;
2198                         }
2199
2200                         sector_size =
2201                             ((buffer[4] << 24) | (buffer[5] << 16) |
2202                              (buffer[6] << 8) | (buffer[7] << 0));
2203                         scst_put_buf(cmd, buffer);
2204                         if (sector_size != 0)
2205                                 sh = scst_calc_block_shift(sector_size);
2206                         else
2207                                 sh = 0;
2208                         set_block_shift(cmd, sh);
2209                         TRACE_DBG("block_shift %d", sh);
2210                         break;
2211                 }
2212                 default:
2213                         /* It's all good */
2214                         break;
2215                 }
2216         }
2217
2218         TRACE_DBG("cmd->is_send_status=%x, cmd->resp_data_len=%d, "
2219               "res=%d", cmd->is_send_status, cmd->resp_data_len, res);
2220
2221 out:
2222         TRACE_EXIT_RES(res);
2223         return res;
2224 }
2225
2226 int scst_tape_generic_dev_done(struct scst_cmd *cmd,
2227         void (*set_block_size)(struct scst_cmd *cmd, int block_shift))
2228 {
2229         int opcode = cmd->cdb[0];
2230         int res = SCST_CMD_STATE_DEFAULT;
2231         int buffer_size, bs;
2232         uint8_t *buffer = NULL;
2233
2234         TRACE_ENTRY();
2235
2236         /*
2237          * SCST sets good defaults for cmd->is_send_status and cmd->resp_data_len
2238          * based on cmd->status and cmd->data_direction, therefore change
2239          * them only if necessary
2240          */
2241
2242         switch (opcode) {
2243         case MODE_SENSE:
2244         case MODE_SELECT:
2245                 buffer_size = scst_get_buf_first(cmd, &buffer);
2246                 if (unlikely(buffer_size <= 0)) {
2247                         PRINT_ERROR("%s: Unable to get the buffer (%d)",
2248                                 __FUNCTION__, buffer_size);
2249                         goto out;
2250                 }
2251                 break;
2252         }
2253
2254         switch (opcode) {
2255         case MODE_SENSE:
2256                 TRACE_DBG("%s", "MODE_SENSE");
2257                 if ((cmd->cdb[2] & 0xC0) == 0) {
2258                         if (buffer[3] == 8) {
2259                                 bs = (buffer[9] << 16) |
2260                                     (buffer[10] << 8) | buffer[11];
2261                                 set_block_size(cmd, bs);
2262                         }
2263                 }
2264                 break;
2265         case MODE_SELECT:
2266                 TRACE_DBG("%s", "MODE_SELECT");
2267                 if (buffer[3] == 8) {
2268                         bs = (buffer[9] << 16) | (buffer[10] << 8) |
2269                             (buffer[11]);
2270                         set_block_size(cmd, bs);
2271                 }
2272                 break;
2273         default:
2274                 /* It's all good */
2275                 break;
2276         }
2277
2278         switch (opcode) {
2279         case MODE_SENSE:
2280         case MODE_SELECT:
2281                 scst_put_buf(cmd, buffer);
2282                 break;
2283         }
2284
2285 out:
2286         TRACE_EXIT_RES(res);
2287         return res;
2288 }
2289
2290 static void scst_check_internal_sense(struct scst_device *dev, int result,
2291         uint8_t *sense, int sense_len)
2292 {
2293         TRACE_ENTRY();
2294
2295         if (host_byte(result) == DID_RESET) {
2296                 TRACE(TRACE_MGMT_MINOR, "%s", "DID_RESET received, triggering "
2297                         "reset UA");
2298                 scst_set_sense(sense, sense_len,
2299                         SCST_LOAD_SENSE(scst_sense_reset_UA));
2300                 scst_dev_check_set_UA(dev, NULL, sense, sense_len);
2301         } else if ((status_byte(result) == CHECK_CONDITION) &&
2302                    SCST_SENSE_VALID(sense) && scst_is_ua_sense(sense))
2303                 scst_dev_check_set_UA(dev, NULL, sense, sense_len);
2304
2305         TRACE_EXIT();
2306         return;
2307 }
2308
2309 int scst_obtain_device_parameters(struct scst_device *dev)
2310 {
2311         int res = 0, i;
2312         uint8_t cmd[16];
2313         uint8_t buffer[4+0x0A];
2314         uint8_t sense_buffer[SCST_SENSE_BUFFERSIZE];
2315
2316         TRACE_ENTRY();
2317
2318         sBUG_ON(in_interrupt() || in_atomic());
2319         EXTRACHECKS_BUG_ON(dev->scsi_dev == NULL);
2320
2321         for (i = 0; i < 5; i++) {
2322                 /* Get control mode page */
2323                 memset(cmd, 0, sizeof(cmd));
2324                 cmd[0] = MODE_SENSE;
2325                 cmd[1] = 8; /* DBD */
2326                 cmd[2] = 0x0A;
2327                 cmd[4] = sizeof(buffer);
2328
2329                 memset(buffer, 0, sizeof(buffer));
2330                 memset(sense_buffer, 0, sizeof(sense_buffer));
2331
2332                 TRACE(TRACE_SCSI, "%s", "Doing internal MODE_SENSE");
2333                 res = scsi_execute(dev->scsi_dev, cmd, SCST_DATA_READ, buffer,
2334                            sizeof(buffer), sense_buffer, SCST_DEFAULT_TIMEOUT,
2335                             0, 0);
2336
2337                 TRACE_DBG("MODE_SENSE done: %x", res);
2338
2339                 if (scsi_status_is_good(res)) {
2340                         int q;
2341
2342                         PRINT_BUFF_FLAG(TRACE_SCSI, "Returned control mode page data",
2343                                 buffer, sizeof(buffer));
2344
2345                         dev->tst = buffer[4+2] >> 5;
2346                         q = buffer[4+3] >> 4;
2347                         if (q > SCST_CONTR_MODE_QUEUE_ALG_UNRESTRICTED_REORDER) {
2348                                 PRINT_ERROR("Too big QUEUE ALG %x, dev "
2349                                         "%d:%d:%d:%d", dev->queue_alg,
2350                                         dev->scsi_dev->host->host_no, dev->scsi_dev->channel,
2351                                         dev->scsi_dev->id, dev->scsi_dev->lun);
2352                         }
2353                         dev->queue_alg = q;
2354                         dev->swp = (buffer[4+4] & 0x8) >> 3;
2355                         dev->tas = (buffer[4+5] & 0x40) >> 6;
2356
2357                         /*
2358                          * Unfortunately, SCSI ML doesn't provide a way to
2359                          * specify commands task attribute, so we can rely on
2360                          * device's restricted reordering only.
2361                          */
2362                         dev->has_own_order_mgmt = !dev->queue_alg;
2363
2364                         TRACE(TRACE_SCSI|TRACE_MGMT_MINOR, "Device %d:%d:%d:%d: TST %x, "
2365                                 "QUEUE ALG %x, SWP %x, TAS %x, has_own_order_mgmt "
2366                                 "%d", dev->scsi_dev->host->host_no,
2367                                 dev->scsi_dev->channel, dev->scsi_dev->id,
2368                                 dev->scsi_dev->lun, dev->tst, dev->queue_alg,
2369                                 dev->swp, dev->tas, dev->has_own_order_mgmt);
2370
2371                         goto out;
2372                 } else {
2373 #if 0 /* 3ware controller is buggy and returns CONDITION_GOOD instead of CHECK_CONDITION */
2374                         if ((status_byte(res) == CHECK_CONDITION) &&
2375 #else
2376                         if (
2377 #endif
2378                             SCST_SENSE_VALID(sense_buffer)) {
2379                                 if (sense_buffer[2] == ILLEGAL_REQUEST) {
2380                                         TRACE(TRACE_SCSI|TRACE_MGMT_MINOR, "Device "
2381                                                 "%d:%d:%d:%d doesn't support control "
2382                                                 "mode page, using defaults: TST "
2383                                                 "%x, QUEUE ALG %x, SWP %x, TAS %x, "
2384                                                 "has_own_order_mgmt %d",
2385                                                 dev->scsi_dev->host->host_no,
2386                                                 dev->scsi_dev->channel, dev->scsi_dev->id,
2387                                                 dev->scsi_dev->lun, dev->tst, dev->queue_alg,
2388                                                 dev->swp, dev->tas, dev->has_own_order_mgmt);
2389                                         res = 0;
2390                                         goto out;
2391                                 } else if (sense_buffer[2] == NOT_READY) {
2392                                         TRACE(TRACE_SCSI, "Device %d:%d:%d:%d not ready",
2393                                                 dev->scsi_dev->host->host_no,
2394                                                 dev->scsi_dev->channel, dev->scsi_dev->id,
2395                                                 dev->scsi_dev->lun);
2396                                         res = 0;
2397                                         goto out;
2398                                 }
2399                         } else {
2400                                 TRACE(TRACE_SCSI|TRACE_MGMT_MINOR, "Internal MODE SENSE to "
2401                                         "device %d:%d:%d:%d failed: %x",
2402                                         dev->scsi_dev->host->host_no,
2403                                         dev->scsi_dev->channel, dev->scsi_dev->id,
2404                                         dev->scsi_dev->lun, res);
2405                                 PRINT_BUFF_FLAG(TRACE_SCSI|TRACE_MGMT_MINOR, "MODE SENSE "
2406                                         "sense", sense_buffer, sizeof(sense_buffer));
2407                         }
2408                         scst_check_internal_sense(dev, res, sense_buffer,
2409                                         sizeof(sense_buffer));
2410                 }
2411         }
2412         res = -ENODEV;
2413
2414 out:
2415         TRACE_EXIT_RES(res);
2416         return res;
2417 }
2418
2419 /* Called under dev_lock and BH off */
2420 void scst_process_reset(struct scst_device *dev,
2421         struct scst_session *originator, struct scst_cmd *exclude_cmd,
2422         struct scst_mgmt_cmd *mcmd)
2423 {
2424         struct scst_tgt_dev *tgt_dev;
2425         struct scst_cmd *cmd, *tcmd;
2426
2427         TRACE_ENTRY();
2428
2429         /* Clear RESERVE'ation, if necessary */
2430         if (dev->dev_reserved) {
2431                 list_for_each_entry(tgt_dev, &dev->dev_tgt_dev_list,
2432                                     dev_tgt_dev_list_entry) {
2433                         TRACE(TRACE_MGMT, "Clearing RESERVE'ation for tgt_dev "
2434                                 "lun %Ld", tgt_dev->lun);
2435                         clear_bit(SCST_TGT_DEV_RESERVED,
2436                                   &tgt_dev->tgt_dev_flags);
2437                 }
2438                 dev->dev_reserved = 0;
2439                 /*
2440                  * There is no need to send RELEASE, since the device is going
2441                  * to be resetted. Actually, since we can be in RESET TM
2442                  * function, it might be dangerous.
2443                  */
2444         }
2445
2446         dev->dev_double_ua_possible = 1;
2447         dev->dev_serialized = 1;
2448
2449         list_for_each_entry(tgt_dev, &dev->dev_tgt_dev_list,
2450                 dev_tgt_dev_list_entry) {
2451                 struct scst_session *sess = tgt_dev->sess;
2452
2453                 spin_lock_bh(&tgt_dev->tgt_dev_lock);
2454                 scst_free_all_UA(tgt_dev);
2455                 spin_unlock_bh(&tgt_dev->tgt_dev_lock);
2456
2457                 spin_lock_irq(&sess->sess_list_lock);
2458
2459                 TRACE_DBG("Searching in search cmd list (sess=%p)", sess);
2460                 list_for_each_entry(cmd, &sess->search_cmd_list,
2461                                 search_cmd_list_entry) {
2462                         if (cmd == exclude_cmd)
2463                                 continue;
2464                         if ((cmd->tgt_dev == tgt_dev) ||
2465                             ((cmd->tgt_dev == NULL) &&
2466                              (cmd->lun == tgt_dev->lun))) {
2467                                 scst_abort_cmd(cmd, mcmd,
2468                                         (tgt_dev->sess != originator), 0);
2469                         }
2470                 }
2471                 spin_unlock_irq(&sess->sess_list_lock);
2472         }
2473
2474         list_for_each_entry_safe(cmd, tcmd, &dev->blocked_cmd_list,
2475                                 blocked_cmd_list_entry) {
2476                 if (test_bit(SCST_CMD_ABORTED, &cmd->cmd_flags)) {
2477                         list_del(&cmd->blocked_cmd_list_entry);
2478                         TRACE_MGMT_DBG("Adding aborted blocked cmd %p "
2479                                 "to active cmd list", cmd);
2480                         spin_lock_irq(&cmd->cmd_lists->cmd_list_lock);
2481                         list_add_tail(&cmd->cmd_list_entry,
2482                                 &cmd->cmd_lists->active_cmd_list);
2483                         wake_up(&cmd->cmd_lists->cmd_list_waitQ);
2484                         spin_unlock_irq(&cmd->cmd_lists->cmd_list_lock);
2485                 }
2486         }
2487
2488         /* BH already off */
2489         spin_lock(&scst_temp_UA_lock);
2490         scst_set_sense(scst_temp_UA, sizeof(scst_temp_UA),
2491                 SCST_LOAD_SENSE(scst_sense_reset_UA));
2492         scst_dev_check_set_local_UA(dev, exclude_cmd, scst_temp_UA,
2493                 sizeof(scst_temp_UA));
2494         spin_unlock(&scst_temp_UA_lock);
2495
2496         TRACE_EXIT();
2497         return;
2498 }
2499
2500 int scst_set_pending_UA(struct scst_cmd *cmd)
2501 {
2502         int res = 0;
2503         struct scst_tgt_dev_UA *UA_entry;
2504
2505         TRACE_ENTRY();
2506
2507         TRACE(TRACE_MGMT, "Setting pending UA cmd %p", cmd);
2508
2509         spin_lock_bh(&cmd->tgt_dev->tgt_dev_lock);
2510
2511         /* UA list could be cleared behind us, so retest */
2512         if (list_empty(&cmd->tgt_dev->UA_list)) {
2513                 TRACE_DBG("%s",
2514                       "SCST_TGT_DEV_UA_PENDING set, but UA_list empty");
2515                 res = -1;
2516                 goto out_unlock;
2517         }
2518
2519         UA_entry = list_entry(cmd->tgt_dev->UA_list.next, typeof(*UA_entry),
2520                               UA_list_entry);
2521
2522         TRACE_DBG("next %p UA_entry %p",
2523               cmd->tgt_dev->UA_list.next, UA_entry);
2524
2525         scst_set_cmd_error_sense(cmd, UA_entry->UA_sense_buffer,
2526                 sizeof(UA_entry->UA_sense_buffer));
2527
2528         cmd->ua_ignore = 1;
2529
2530         list_del(&UA_entry->UA_list_entry);
2531
2532         mempool_free(UA_entry, scst_ua_mempool);
2533
2534         if (list_empty(&cmd->tgt_dev->UA_list)) {
2535                 clear_bit(SCST_TGT_DEV_UA_PENDING,
2536                           &cmd->tgt_dev->tgt_dev_flags);
2537         }
2538
2539         spin_unlock_bh(&cmd->tgt_dev->tgt_dev_lock);
2540
2541 out:
2542         TRACE_EXIT_RES(res);
2543         return res;
2544
2545 out_unlock:
2546         spin_unlock_bh(&cmd->tgt_dev->tgt_dev_lock);
2547         goto out;
2548 }
2549
2550 /* Called under tgt_dev_lock and BH off */
2551 void scst_alloc_set_UA(struct scst_tgt_dev *tgt_dev,
2552         const uint8_t *sense, int sense_len, int head)
2553 {
2554         struct scst_tgt_dev_UA *UA_entry = NULL;
2555
2556         TRACE_ENTRY();
2557
2558         UA_entry = mempool_alloc(scst_ua_mempool, GFP_ATOMIC);
2559         if (UA_entry == NULL) {
2560                 PRINT_CRIT_ERROR("%s", "UNIT ATTENTION memory "
2561                      "allocation failed. The UNIT ATTENTION "
2562                      "on some sessions will be missed");
2563                 PRINT_BUFFER("Lost UA", sense, sense_len);
2564                 goto out;
2565         }
2566         memset(UA_entry, 0, sizeof(*UA_entry));
2567
2568         if (sense_len > (int)sizeof(UA_entry->UA_sense_buffer))
2569                 sense_len = sizeof(UA_entry->UA_sense_buffer);
2570         memcpy(UA_entry->UA_sense_buffer, sense, sense_len);
2571
2572         set_bit(SCST_TGT_DEV_UA_PENDING, &tgt_dev->tgt_dev_flags);
2573
2574         TRACE_MGMT_DBG("Adding new UA to tgt_dev %p", tgt_dev);
2575
2576         if (head)
2577                 list_add(&UA_entry->UA_list_entry, &tgt_dev->UA_list);
2578         else
2579                 list_add_tail(&UA_entry->UA_list_entry, &tgt_dev->UA_list);
2580
2581 out:
2582         TRACE_EXIT();
2583         return;
2584 }
2585
2586 void scst_check_set_UA(struct scst_tgt_dev *tgt_dev,
2587         const uint8_t *sense, int sense_len, int head)
2588 {
2589         int skip_UA = 0;
2590         struct scst_tgt_dev_UA *UA_entry_tmp;
2591
2592         TRACE_ENTRY();
2593
2594         spin_lock_bh(&tgt_dev->tgt_dev_lock);
2595
2596         list_for_each_entry(UA_entry_tmp, &tgt_dev->UA_list,
2597                             UA_list_entry) {
2598                 if (memcmp(sense, UA_entry_tmp->UA_sense_buffer, sense_len) == 0) {
2599                         TRACE_MGMT_DBG("%s", "UA already exists");
2600                         skip_UA = 1;
2601                         break;
2602                 }
2603         }
2604
2605         if (skip_UA == 0)
2606                 scst_alloc_set_UA(tgt_dev, sense, sense_len, head);
2607
2608         spin_unlock_bh(&tgt_dev->tgt_dev_lock);
2609
2610         TRACE_EXIT();
2611         return;
2612 }
2613
2614 /* Called under dev_lock and BH off */
2615 void scst_dev_check_set_local_UA(struct scst_device *dev,
2616         struct scst_cmd *exclude, const uint8_t *sense, int sense_len)
2617 {
2618         struct scst_tgt_dev *tgt_dev, *exclude_tgt_dev = NULL;
2619
2620         TRACE_ENTRY();
2621
2622         if (exclude != NULL)
2623                 exclude_tgt_dev = exclude->tgt_dev;
2624
2625         list_for_each_entry(tgt_dev, &dev->dev_tgt_dev_list,
2626                         dev_tgt_dev_list_entry) {
2627                 if (tgt_dev != exclude_tgt_dev)
2628                         scst_check_set_UA(tgt_dev, sense, sense_len, 0);
2629         }
2630
2631         TRACE_EXIT();
2632         return;
2633 }
2634
2635 /* Called under dev_lock and BH off */
2636 void __scst_dev_check_set_UA(struct scst_device *dev,
2637         struct scst_cmd *exclude, const uint8_t *sense, int sense_len)
2638 {
2639         TRACE_ENTRY();
2640
2641         TRACE(TRACE_MGMT, "Processing UA dev %p", dev);
2642
2643         /* Check for reset UA */
2644         if (sense[12] == SCST_SENSE_ASC_UA_RESET)
2645                 scst_process_reset(dev, (exclude != NULL) ? exclude->sess : NULL,
2646                         exclude, NULL);
2647
2648         scst_dev_check_set_local_UA(dev, exclude, sense, sense_len);
2649
2650         TRACE_EXIT();
2651         return;
2652 }
2653
2654 /* Called under tgt_dev_lock or when tgt_dev is unused */
2655 void scst_free_all_UA(struct scst_tgt_dev *tgt_dev)
2656 {
2657         struct scst_tgt_dev_UA *UA_entry, *t;
2658
2659         TRACE_ENTRY();
2660
2661         list_for_each_entry_safe(UA_entry, t, &tgt_dev->UA_list, UA_list_entry) {
2662                 TRACE_MGMT_DBG("Clearing UA for tgt_dev lun %Ld",
2663                         tgt_dev->lun);
2664                 list_del(&UA_entry->UA_list_entry);
2665                 kfree(UA_entry);
2666         }
2667         INIT_LIST_HEAD(&tgt_dev->UA_list);
2668         clear_bit(SCST_TGT_DEV_UA_PENDING, &tgt_dev->tgt_dev_flags);
2669
2670         TRACE_EXIT();
2671         return;
2672 }
2673
2674 /* No locks */
2675 struct scst_cmd *__scst_check_deferred_commands(struct scst_tgt_dev *tgt_dev)
2676 {
2677         struct scst_cmd *res = NULL, *cmd, *t;
2678         typeof(tgt_dev->expected_sn) expected_sn = tgt_dev->expected_sn;
2679
2680         spin_lock_irq(&tgt_dev->sn_lock);
2681
2682         if (unlikely(tgt_dev->hq_cmd_count != 0))
2683                 goto out_unlock;
2684
2685 restart:
2686         list_for_each_entry_safe(cmd, t, &tgt_dev->deferred_cmd_list,
2687                                 sn_cmd_list_entry) {
2688                 EXTRACHECKS_BUG_ON(cmd->queue_type ==
2689                         SCST_CMD_QUEUE_HEAD_OF_QUEUE);
2690                 if (cmd->sn == expected_sn) {
2691                         TRACE_SN("Deferred command %p (sn %ld, set %d) found",
2692                                 cmd, cmd->sn, cmd->sn_set);
2693                         tgt_dev->def_cmd_count--;
2694                         list_del(&cmd->sn_cmd_list_entry);
2695                         if (res == NULL)
2696                                 res = cmd;
2697                         else {
2698                                 spin_lock(&cmd->cmd_lists->cmd_list_lock);
2699                                 TRACE_SN("Adding cmd %p to active cmd list",
2700                                         cmd);
2701                                 list_add_tail(&cmd->cmd_list_entry,
2702                                         &cmd->cmd_lists->active_cmd_list);
2703                                 wake_up(&cmd->cmd_lists->cmd_list_waitQ);
2704                                 spin_unlock(&cmd->cmd_lists->cmd_list_lock);
2705                         }
2706                 }
2707         }
2708         if (res != NULL)
2709                 goto out_unlock;
2710
2711         list_for_each_entry(cmd, &tgt_dev->skipped_sn_list,
2712                                 sn_cmd_list_entry) {
2713                 EXTRACHECKS_BUG_ON(cmd->queue_type ==
2714                         SCST_CMD_QUEUE_HEAD_OF_QUEUE);
2715                 if (cmd->sn == expected_sn) {
2716                         atomic_t *slot = cmd->sn_slot;
2717                         /*
2718                          * !! At this point any pointer in cmd, except !!
2719                          * !! sn_slot and sn_cmd_list_entry, could be   !!
2720                          * !! already destroyed                         !!
2721                          */
2722                         TRACE_SN("cmd %p (tag %llu) with skipped sn %ld found",
2723                                 cmd, cmd->tag, cmd->sn);
2724                         tgt_dev->def_cmd_count--;
2725                         list_del(&cmd->sn_cmd_list_entry);
2726                         spin_unlock_irq(&tgt_dev->sn_lock);
2727                         if (test_and_set_bit(SCST_CMD_CAN_BE_DESTROYED,
2728                                              &cmd->cmd_flags))
2729                                 scst_destroy_put_cmd(cmd);
2730                         scst_inc_expected_sn(tgt_dev, slot);
2731                         expected_sn = tgt_dev->expected_sn;
2732                         spin_lock_irq(&tgt_dev->sn_lock);
2733                         goto restart;
2734                 }
2735         }
2736
2737 out_unlock:
2738         spin_unlock_irq(&tgt_dev->sn_lock);
2739         return res;
2740 }
2741
2742 void scst_add_thr_data(struct scst_tgt_dev *tgt_dev,
2743         struct scst_thr_data_hdr *data,
2744         void (*free_fn) (struct scst_thr_data_hdr *data))
2745 {
2746         data->pid = current->pid;
2747         atomic_set(&data->ref, 1);
2748         EXTRACHECKS_BUG_ON(free_fn == NULL);
2749         data->free_fn = free_fn;
2750         spin_lock(&tgt_dev->thr_data_lock);
2751         list_add_tail(&data->thr_data_list_entry, &tgt_dev->thr_data_list);
2752         spin_unlock(&tgt_dev->thr_data_lock);
2753 }
2754
2755 void scst_del_all_thr_data(struct scst_tgt_dev *tgt_dev)
2756 {
2757         spin_lock(&tgt_dev->thr_data_lock);
2758         while (!list_empty(&tgt_dev->thr_data_list)) {
2759                 struct scst_thr_data_hdr *d = list_entry(
2760                                 tgt_dev->thr_data_list.next, typeof(*d),
2761                                 thr_data_list_entry);
2762                 list_del(&d->thr_data_list_entry);
2763                 spin_unlock(&tgt_dev->thr_data_lock);
2764                 scst_thr_data_put(d);
2765                 spin_lock(&tgt_dev->thr_data_lock);
2766         }
2767         spin_unlock(&tgt_dev->thr_data_lock);
2768         return;
2769 }
2770
2771 void scst_dev_del_all_thr_data(struct scst_device *dev)
2772 {
2773         struct scst_tgt_dev *tgt_dev;
2774
2775         TRACE_ENTRY();
2776
2777         mutex_lock(&scst_mutex);
2778
2779         list_for_each_entry(tgt_dev, &dev->dev_tgt_dev_list,
2780                                 dev_tgt_dev_list_entry) {
2781                 scst_del_all_thr_data(tgt_dev);
2782         }
2783
2784         mutex_unlock(&scst_mutex);
2785
2786         TRACE_EXIT();
2787         return;
2788 }
2789
2790 struct scst_thr_data_hdr *scst_find_thr_data(struct scst_tgt_dev *tgt_dev)
2791 {
2792         struct scst_thr_data_hdr *res = NULL, *d;
2793
2794         spin_lock(&tgt_dev->thr_data_lock);
2795         list_for_each_entry(d, &tgt_dev->thr_data_list, thr_data_list_entry) {
2796                 if (d->pid == current->pid) {
2797                         res = d;
2798                         scst_thr_data_get(res);
2799                         break;
2800                 }
2801         }
2802         spin_unlock(&tgt_dev->thr_data_lock);
2803         return res;
2804 }
2805
2806 /* dev_lock supposed to be held and BH disabled */
2807 void __scst_block_dev(struct scst_device *dev)
2808 {
2809         dev->block_count++;
2810         TRACE_MGMT_DBG("Device BLOCK(new %d), dev %p", dev->block_count, dev);
2811 }
2812
2813 /* No locks */
2814 void scst_block_dev(struct scst_device *dev, int outstanding)
2815 {
2816         spin_lock_bh(&dev->dev_lock);
2817         __scst_block_dev(dev);
2818         spin_unlock_bh(&dev->dev_lock);
2819
2820         /* spin_unlock_bh() doesn't provide the necessary memory barrier */
2821         smp_mb();
2822
2823         TRACE_MGMT_DBG("Waiting during blocking outstanding %d (on_dev_count "
2824                 "%d)", outstanding, atomic_read(&dev->on_dev_count));
2825         wait_event(dev->on_dev_waitQ,
2826                 atomic_read(&dev->on_dev_count) <= outstanding);
2827         TRACE_MGMT_DBG("%s", "wait_event() returned");
2828 }
2829
2830 /* No locks */
2831 void scst_block_dev_cmd(struct scst_cmd *cmd, int outstanding)
2832 {
2833         sBUG_ON(cmd->needs_unblocking);
2834
2835         cmd->needs_unblocking = 1;
2836         TRACE_MGMT_DBG("Needs unblocking cmd %p (tag %llu)", cmd, cmd->tag);
2837
2838         scst_block_dev(cmd->dev, outstanding);
2839 }
2840
2841 /* No locks */
2842 void scst_unblock_dev(struct scst_device *dev)
2843 {
2844         spin_lock_bh(&dev->dev_lock);
2845         TRACE_MGMT_DBG("Device UNBLOCK(new %d), dev %p",
2846                 dev->block_count-1, dev);
2847         if (--dev->block_count == 0)
2848                 scst_unblock_cmds(dev);
2849         spin_unlock_bh(&dev->dev_lock);
2850         sBUG_ON(dev->block_count < 0);
2851 }
2852
2853 /* No locks */
2854 void scst_unblock_dev_cmd(struct scst_cmd *cmd)
2855 {
2856         scst_unblock_dev(cmd->dev);
2857         cmd->needs_unblocking = 0;
2858 }
2859
2860 /* No locks */
2861 int scst_inc_on_dev_cmd(struct scst_cmd *cmd)
2862 {
2863         int res = 0;
2864         struct scst_device *dev = cmd->dev;
2865
2866         TRACE_ENTRY();
2867
2868         sBUG_ON(cmd->inc_blocking || cmd->dec_on_dev_needed);
2869
2870         atomic_inc(&dev->on_dev_count);
2871         cmd->dec_on_dev_needed = 1;
2872         TRACE_DBG("New on_dev_count %d", atomic_read(&dev->on_dev_count));
2873
2874 #ifdef STRICT_SERIALIZING
2875         spin_lock_bh(&dev->dev_lock);
2876         if (unlikely(test_bit(SCST_CMD_ABORTED, &cmd->cmd_flags)))
2877                 goto out_unlock;
2878         if (dev->block_count > 0) {
2879                 scst_dec_on_dev_cmd(cmd);
2880                 TRACE_MGMT_DBG("Delaying cmd %p due to blocking or strict "
2881                         "serializing (tag %llu, dev %p)", cmd, cmd->tag, dev);
2882                 list_add_tail(&cmd->blocked_cmd_list_entry,
2883                               &dev->blocked_cmd_list);
2884                 res = 1;
2885         } else {
2886                 __scst_block_dev(dev);
2887                 cmd->inc_blocking = 1;
2888         }
2889         spin_unlock_bh(&dev->dev_lock);
2890         goto out;
2891 #else
2892 repeat:
2893         if (unlikely(dev->block_count > 0)) {
2894                 spin_lock_bh(&dev->dev_lock);
2895                 if (unlikely(test_bit(SCST_CMD_ABORTED, &cmd->cmd_flags)))
2896                         goto out_unlock;
2897                 barrier(); /* to reread block_count */
2898                 if (dev->block_count > 0) {
2899                         scst_dec_on_dev_cmd(cmd);
2900                         TRACE_MGMT_DBG("Delaying cmd %p due to blocking or "
2901                                 "serializing (tag %llu, dev %p)", cmd,
2902                                 cmd->tag, dev);
2903                         list_add_tail(&cmd->blocked_cmd_list_entry,
2904                                       &dev->blocked_cmd_list);
2905                         res = 1;
2906                         spin_unlock_bh(&dev->dev_lock);
2907                         goto out;
2908                 } else {
2909                         TRACE_MGMT_DBG("%s", "Somebody unblocked the device, "
2910                                 "continuing");
2911                 }
2912                 spin_unlock_bh(&dev->dev_lock);
2913         }
2914         if (unlikely(dev->dev_serialized)) {
2915                 spin_lock_bh(&dev->dev_lock);
2916                 barrier(); /* to reread block_count */
2917                 if (dev->block_count == 0) {
2918                         TRACE_MGMT_DBG("cmd %p (tag %llu), blocking further "
2919                                 "cmds due to serializing (dev %p)", cmd,
2920                                 cmd->tag, dev);
2921                         __scst_block_dev(dev);
2922                         cmd->inc_blocking = 1;
2923                 } else {
2924                         spin_unlock_bh(&dev->dev_lock);
2925                         TRACE_MGMT_DBG("Somebody blocked the device, "
2926                                 "repeating (count %d)", dev->block_count);
2927                         goto repeat;
2928                 }
2929                 spin_unlock_bh(&dev->dev_lock);
2930         }
2931 #endif
2932
2933 out:
2934         TRACE_EXIT_RES(res);
2935         return res;
2936
2937 out_unlock:
2938         spin_unlock_bh(&dev->dev_lock);
2939         goto out;
2940 }
2941
2942 /* Called under dev_lock */
2943 void scst_unblock_cmds(struct scst_device *dev)
2944 {
2945 #ifdef STRICT_SERIALIZING
2946         struct scst_cmd *cmd, *t;
2947         unsigned long flags;
2948
2949         TRACE_ENTRY();
2950
2951         local_irq_save(flags);
2952         list_for_each_entry_safe(cmd, t, &dev->blocked_cmd_list,
2953                                  blocked_cmd_list_entry) {
2954                 int brk = 0;
2955                 /*
2956                  * Since only one cmd per time is being executed, expected_sn
2957                  * can't change behind us, if the corresponding cmd is in
2958                  * blocked_cmd_list, but we could be called before
2959                  * scst_inc_expected_sn().
2960                  */
2961                 if (likely(!cmd->internal && !cmd->retry)) {
2962                         typeof(cmd->tgt_dev->expected_sn) expected_sn;
2963                         if (cmd->tgt_dev == NULL)
2964                                 sBUG();
2965                         expected_sn = cmd->tgt_dev->expected_sn;
2966                         if (cmd->sn == expected_sn)
2967                                 brk = 1;
2968                         else if (cmd->sn != (expected_sn+1))
2969                                 continue;
2970                 }
2971
2972                 list_del(&cmd->blocked_cmd_list_entry);
2973                 TRACE_MGMT_DBG("Adding cmd %p to head of active cmd list", cmd);
2974                 spin_lock(&cmd->cmd_lists->cmd_list_lock);
2975                 list_add(&cmd->cmd_list_entry, &cmd->cmd_lists->active_cmd_list);
2976                 wake_up(&cmd->cmd_lists->cmd_list_waitQ);
2977                 spin_unlock(&cmd->cmd_lists->cmd_list_lock);
2978                 if (brk)
2979                         break;
2980         }
2981         local_irq_restore(flags);
2982 #else /* STRICT_SERIALIZING */
2983         struct scst_cmd *cmd, *tcmd;
2984         unsigned long flags;
2985
2986         TRACE_ENTRY();
2987
2988         local_irq_save(flags);
2989         list_for_each_entry_safe(cmd, tcmd, &dev->blocked_cmd_list,
2990                                  blocked_cmd_list_entry) {
2991                 list_del(&cmd->blocked_cmd_list_entry);
2992                 TRACE_MGMT_DBG("Adding blocked cmd %p to active cmd list", cmd);
2993                 spin_lock(&cmd->cmd_lists->cmd_list_lock);
2994                 if (unlikely(cmd->queue_type == SCST_CMD_QUEUE_HEAD_OF_QUEUE))
2995                         list_add(&cmd->cmd_list_entry,
2996                                 &cmd->cmd_lists->active_cmd_list);
2997                 else
2998                         list_add_tail(&cmd->cmd_list_entry,
2999                                 &cmd->cmd_lists->active_cmd_list);
3000                 wake_up(&cmd->cmd_lists->cmd_list_waitQ);
3001                 spin_unlock(&cmd->cmd_lists->cmd_list_lock);
3002         }
3003         local_irq_restore(flags);
3004 #endif /* STRICT_SERIALIZING */
3005
3006         TRACE_EXIT();
3007         return;
3008 }
3009
3010 static void __scst_unblock_deferred(struct scst_tgt_dev *tgt_dev,
3011         struct scst_cmd *out_of_sn_cmd)
3012 {
3013         EXTRACHECKS_BUG_ON(!out_of_sn_cmd->sn_set);
3014
3015         if (out_of_sn_cmd->sn == tgt_dev->expected_sn) {
3016                 scst_inc_expected_sn(tgt_dev, out_of_sn_cmd->sn_slot);
3017                 scst_make_deferred_commands_active(tgt_dev, out_of_sn_cmd);
3018         } else {
3019                 out_of_sn_cmd->out_of_sn = 1;
3020                 spin_lock_irq(&tgt_dev->sn_lock);
3021                 tgt_dev->def_cmd_count++;
3022                 list_add_tail(&out_of_sn_cmd->sn_cmd_list_entry,
3023                               &tgt_dev->skipped_sn_list);
3024                 TRACE_SN("out_of_sn_cmd %p with sn %ld added to skipped_sn_list "
3025                         "(expected_sn %ld)", out_of_sn_cmd, out_of_sn_cmd->sn,
3026                         tgt_dev->expected_sn);
3027                 spin_unlock_irq(&tgt_dev->sn_lock);
3028         }
3029
3030         return;
3031 }
3032
3033 void scst_unblock_deferred(struct scst_tgt_dev *tgt_dev,
3034         struct scst_cmd *out_of_sn_cmd)
3035 {
3036         TRACE_ENTRY();
3037
3038         if (!out_of_sn_cmd->sn_set) {
3039                 TRACE_SN("cmd %p without sn", out_of_sn_cmd);
3040                 goto out;
3041         }
3042
3043         __scst_unblock_deferred(tgt_dev, out_of_sn_cmd);
3044
3045 out:
3046         TRACE_EXIT();
3047         return;
3048 }
3049
3050 void scst_on_hq_cmd_response(struct scst_cmd *cmd)
3051 {
3052         struct scst_tgt_dev *tgt_dev = cmd->tgt_dev;
3053
3054         TRACE_ENTRY();
3055
3056         if (!cmd->hq_cmd_inced)
3057                 goto out;
3058
3059         spin_lock_irq(&tgt_dev->sn_lock);
3060         tgt_dev->hq_cmd_count--;
3061         spin_unlock_irq(&tgt_dev->sn_lock);
3062
3063         EXTRACHECKS_BUG_ON(tgt_dev->hq_cmd_count < 0);
3064
3065         /*
3066          * There is no problem in checking hq_cmd_count in the
3067          * non-locked state. In the worst case we will only have
3068          * unneeded run of the deferred commands.
3069          */
3070         if (tgt_dev->hq_cmd_count == 0)
3071                 scst_make_deferred_commands_active(tgt_dev, cmd);
3072
3073 out:
3074         TRACE_EXIT();
3075         return;
3076 }
3077
3078 void scst_xmit_process_aborted_cmd(struct scst_cmd *cmd)
3079 {
3080         TRACE_ENTRY();
3081
3082         TRACE_MGMT_DBG("Aborted cmd %p done (cmd_ref %d, "
3083                 "scst_cmd_count %d)", cmd, atomic_read(&cmd->cmd_ref),
3084                 atomic_read(&scst_cmd_count));
3085
3086         scst_done_cmd_mgmt(cmd);
3087
3088         smp_rmb();
3089         if (test_bit(SCST_CMD_ABORTED_OTHER, &cmd->cmd_flags)) {
3090                 if (cmd->completed) {
3091                         /* It's completed and it's OK to return its result */
3092                         goto out;
3093                 }
3094
3095                 if (cmd->dev->tas) {
3096                         TRACE_MGMT_DBG("Flag ABORTED OTHER set for cmd %p "
3097                                 "(tag %llu), returning TASK ABORTED ", cmd,
3098                                 cmd->tag);
3099                         scst_set_cmd_error_status(cmd, SAM_STAT_TASK_ABORTED);
3100                 } else {
3101                         TRACE_MGMT_DBG("Flag ABORTED OTHER set for cmd %p "
3102                                 "(tag %llu), aborting without delivery or "
3103                                 "notification", cmd, cmd->tag);
3104                         /*
3105                          * There is no need to check/requeue possible UA,
3106                          * because, if it exists, it will be delivered
3107                          * by the "completed" branch above.
3108                          */
3109                         clear_bit(SCST_CMD_ABORTED_OTHER, &cmd->cmd_flags);
3110                 }
3111         }
3112
3113 out:
3114         TRACE_EXIT();
3115         return;
3116 }
3117
3118 void __init scst_scsi_op_list_init(void)
3119 {
3120         int i;
3121         uint8_t op = 0xff;
3122
3123         TRACE_ENTRY();
3124
3125         for (i = 0; i < 256; i++)
3126                 scst_scsi_op_list[i] = SCST_CDB_TBL_SIZE;
3127
3128         for (i = 0; i < SCST_CDB_TBL_SIZE; i++) {
3129                 if (scst_scsi_op_table[i].ops != op) {
3130                         op = scst_scsi_op_table[i].ops;
3131                         scst_scsi_op_list[op] = i;
3132                 }
3133         }
3134
3135         TRACE_EXIT();
3136         return;
3137 }
3138
3139 #ifdef DEBUG
3140 /* Original taken from the XFS code */
3141 unsigned long scst_random(void)
3142 {
3143         static int Inited;
3144         static unsigned long RandomValue;
3145         static spinlock_t lock = SPIN_LOCK_UNLOCKED;
3146         /* cycles pseudo-randomly through all values between 1 and 2^31 - 2 */
3147         register long rv;
3148         register long lo;
3149         register long hi;
3150         unsigned long flags;
3151
3152         spin_lock_irqsave(&lock, flags);
3153         if (!Inited) {
3154                 RandomValue = jiffies;
3155                 Inited = 1;
3156         }
3157         rv = RandomValue;
3158         hi = rv / 127773;
3159         lo = rv % 127773;
3160         rv = 16807 * lo - 2836 * hi;
3161         if (rv <= 0)
3162                 rv += 2147483647;
3163         RandomValue = rv;
3164         spin_unlock_irqrestore(&lock, flags);
3165         return rv;
3166 }
3167 #endif
3168
3169 #ifdef DEBUG_TM
3170
3171 #define TM_DBG_STATE_ABORT              0
3172 #define TM_DBG_STATE_RESET              1
3173 #define TM_DBG_STATE_OFFLINE            2
3174
3175 #define INIT_TM_DBG_STATE               TM_DBG_STATE_ABORT
3176
3177 static void tm_dbg_timer_fn(unsigned long arg);
3178
3179 static spinlock_t scst_tm_dbg_lock = SPIN_LOCK_UNLOCKED;
3180 /* All serialized by scst_tm_dbg_lock */
3181 struct {
3182         unsigned int tm_dbg_release:1;
3183         unsigned int tm_dbg_blocked:1;
3184 } tm_dbg_flags;
3185 static LIST_HEAD(tm_dbg_delayed_cmd_list);
3186 static int tm_dbg_delayed_cmds_count;
3187 static int tm_dbg_passed_cmds_count;
3188 static int tm_dbg_state;
3189 static int tm_dbg_on_state_passes;
3190 static DEFINE_TIMER(tm_dbg_timer, tm_dbg_timer_fn, 0, 0);
3191 static wait_queue_head_t *tm_dbg_p_cmd_list_waitQ;
3192
3193 static const int tm_dbg_on_state_num_passes[] = { 5, 1, 0x7ffffff };
3194
3195 void tm_dbg_init_tgt_dev(struct scst_tgt_dev *tgt_dev,
3196         struct scst_acg_dev *acg_dev)
3197 {
3198         if ((acg_dev->acg == scst_default_acg) && (acg_dev->lun == 0)) {
3199                 unsigned long flags;
3200                 /* Do TM debugging only for LUN 0 */
3201                 spin_lock_irqsave(&scst_tm_dbg_lock, flags);
3202                 tm_dbg_p_cmd_list_waitQ =
3203                         &tgt_dev->dev->p_cmd_lists->cmd_list_waitQ;
3204                 tm_dbg_state = INIT_TM_DBG_STATE;
3205                 tm_dbg_on_state_passes =
3206                         tm_dbg_on_state_num_passes[tm_dbg_state];
3207                 __set_bit(SCST_TGT_DEV_UNDER_TM_DBG, &tgt_dev->tgt_dev_flags);
3208                 PRINT_INFO("LUN 0 connected from initiator %s is under "
3209                         "TM debugging", tgt_dev->sess->tgt->tgtt->name);
3210                 spin_unlock_irqrestore(&scst_tm_dbg_lock, flags);
3211         }
3212 }
3213
3214 void tm_dbg_deinit_tgt_dev(struct scst_tgt_dev *tgt_dev)
3215 {
3216         if (test_bit(SCST_TGT_DEV_UNDER_TM_DBG, &tgt_dev->tgt_dev_flags)) {
3217                 unsigned long flags;
3218                 del_timer_sync(&tm_dbg_timer);
3219                 spin_lock_irqsave(&scst_tm_dbg_lock, flags);
3220                 tm_dbg_p_cmd_list_waitQ = NULL;
3221                 spin_unlock_irqrestore(&scst_tm_dbg_lock, flags);
3222         }
3223 }
3224
3225 static void tm_dbg_timer_fn(unsigned long arg)
3226 {
3227         TRACE_MGMT_DBG("%s", "delayed cmd timer expired");
3228         tm_dbg_flags.tm_dbg_release = 1;
3229         smp_wmb();
3230         wake_up_all(tm_dbg_p_cmd_list_waitQ);
3231 }
3232
3233 /* Called under scst_tm_dbg_lock and IRQs off */
3234 static void tm_dbg_delay_cmd(struct scst_cmd *cmd)
3235 {
3236         switch (tm_dbg_state) {
3237         case TM_DBG_STATE_ABORT:
3238                 if (tm_dbg_delayed_cmds_count == 0) {
3239                         unsigned long d = 58*HZ + (scst_random() % (4*HZ));
3240                         TRACE_MGMT_DBG("STATE ABORT: delaying cmd %p (tag %llu) "
3241                                 "for %ld.%ld seconds (%ld HZ), "
3242                                 "tm_dbg_on_state_passes=%d", cmd, cmd->tag,
3243                                 d/HZ, (d%HZ)*100/HZ, d, tm_dbg_on_state_passes);
3244                         mod_timer(&tm_dbg_timer, jiffies + d);
3245 #if 0
3246                         tm_dbg_flags.tm_dbg_blocked = 1;
3247 #endif
3248                 } else {
3249                         TRACE_MGMT_DBG("Delaying another timed cmd %p "
3250                                 "(tag %llu), delayed_cmds_count=%d, "
3251                                 "tm_dbg_on_state_passes=%d", cmd, cmd->tag,
3252                                 tm_dbg_delayed_cmds_count,
3253                                 tm_dbg_on_state_passes);
3254                         if (tm_dbg_delayed_cmds_count == 2)
3255                                 tm_dbg_flags.tm_dbg_blocked = 0;
3256                 }
3257                 break;
3258
3259         case TM_DBG_STATE_RESET:
3260         case TM_DBG_STATE_OFFLINE:
3261                 TRACE_MGMT_DBG("STATE RESET/OFFLINE: delaying cmd %p "
3262                         "(tag %llu), delayed_cmds_count=%d, "
3263                         "tm_dbg_on_state_passes=%d", cmd, cmd->tag,
3264                         tm_dbg_delayed_cmds_count, tm_dbg_on_state_passes);
3265                 tm_dbg_flags.tm_dbg_blocked = 1;
3266                 break;
3267
3268         default:
3269                 sBUG();
3270         }
3271         /* IRQs already off */
3272         spin_lock(&cmd->cmd_lists->cmd_list_lock);
3273         list_add_tail(&cmd->cmd_list_entry, &tm_dbg_delayed_cmd_list);
3274         spin_unlock(&cmd->cmd_lists->cmd_list_lock);
3275         cmd->tm_dbg_delayed = 1;
3276         tm_dbg_delayed_cmds_count++;
3277         return;
3278 }
3279
3280 /* No locks */
3281 void tm_dbg_check_released_cmds(void)
3282 {
3283         if (tm_dbg_flags.tm_dbg_release) {
3284                 struct scst_cmd *cmd, *tc;
3285                 spin_lock_irq(&scst_tm_dbg_lock);
3286                 list_for_each_entry_safe_reverse(cmd, tc,
3287                                 &tm_dbg_delayed_cmd_list, cmd_list_entry) {
3288                         TRACE_MGMT_DBG("Releasing timed cmd %p (tag %llu), "
3289                                 "delayed_cmds_count=%d", cmd, cmd->tag,
3290                                 tm_dbg_delayed_cmds_count);
3291                         spin_lock(&cmd->cmd_lists->cmd_list_lock);
3292                         list_move(&cmd->cmd_list_entry,
3293                                 &cmd->cmd_lists->active_cmd_list);
3294                         spin_unlock(&cmd->cmd_lists->cmd_list_lock);
3295                 }
3296                 tm_dbg_flags.tm_dbg_release = 0;
3297                 spin_unlock_irq(&scst_tm_dbg_lock);
3298         }
3299 }
3300
3301 /* Called under scst_tm_dbg_lock */
3302 static void tm_dbg_change_state(void)
3303 {
3304         tm_dbg_flags.tm_dbg_blocked = 0;
3305         if (--tm_dbg_on_state_passes == 0) {
3306                 switch (tm_dbg_state) {
3307                 case TM_DBG_STATE_ABORT:
3308                         TRACE_MGMT_DBG("%s", "Changing "
3309                             "tm_dbg_state to RESET");
3310                         tm_dbg_state =
3311                                 TM_DBG_STATE_RESET;
3312                         tm_dbg_flags.tm_dbg_blocked = 0;
3313                         break;
3314                 case TM_DBG_STATE_RESET:
3315                 case TM_DBG_STATE_OFFLINE:
3316                         if (TM_DBG_GO_OFFLINE) {
3317                             TRACE_MGMT_DBG("%s", "Changing "
3318                                     "tm_dbg_state to OFFLINE");
3319                             tm_dbg_state =
3320                                 TM_DBG_STATE_OFFLINE;
3321                         } else {
3322                             TRACE_MGMT_DBG("%s", "Changing "
3323                                     "tm_dbg_state to ABORT");
3324                             tm_dbg_state =
3325                                 TM_DBG_STATE_ABORT;
3326                         }
3327                         break;
3328                 default:
3329                         sBUG();
3330                 }
3331                 tm_dbg_on_state_passes =
3332                     tm_dbg_on_state_num_passes[tm_dbg_state];
3333         }
3334
3335         TRACE_MGMT_DBG("%s", "Deleting timer");
3336         del_timer(&tm_dbg_timer);
3337 }
3338
3339 /* No locks */
3340 int tm_dbg_check_cmd(struct scst_cmd *cmd)
3341 {
3342         int res = 0;
3343         unsigned long flags;
3344
3345         if (cmd->tm_dbg_immut)
3346                 goto out;
3347
3348         if (cmd->tm_dbg_delayed) {
3349                 spin_lock_irqsave(&scst_tm_dbg_lock, flags);
3350                 TRACE_MGMT_DBG("Processing delayed cmd %p (tag %llu), "
3351                         "delayed_cmds_count=%d", cmd, cmd->tag,
3352                         tm_dbg_delayed_cmds_count);
3353
3354                 cmd->tm_dbg_immut = 1;
3355                 tm_dbg_delayed_cmds_count--;
3356                 if ((tm_dbg_delayed_cmds_count == 0) &&
3357                     (tm_dbg_state == TM_DBG_STATE_ABORT))
3358                         tm_dbg_change_state();
3359                 spin_unlock_irqrestore(&scst_tm_dbg_lock, flags);
3360         } else if (cmd->tgt_dev && test_bit(SCST_TGT_DEV_UNDER_TM_DBG,
3361                                         &cmd->tgt_dev->tgt_dev_flags)) {
3362                 /* Delay 50th command */
3363                 spin_lock_irqsave(&scst_tm_dbg_lock, flags);
3364                 if (tm_dbg_flags.tm_dbg_blocked ||
3365                     (++tm_dbg_passed_cmds_count % 50) == 0) {
3366                         tm_dbg_delay_cmd(cmd);
3367                         res = 1;
3368                 } else
3369                         cmd->tm_dbg_immut = 1;
3370                 spin_unlock_irqrestore(&scst_tm_dbg_lock, flags);
3371         }
3372
3373 out:
3374         return res;
3375 }
3376
3377 /* No locks */
3378 void tm_dbg_release_cmd(struct scst_cmd *cmd)
3379 {
3380         struct scst_cmd *c;
3381         unsigned long flags;
3382
3383         spin_lock_irqsave(&scst_tm_dbg_lock, flags);
3384         list_for_each_entry(c, &tm_dbg_delayed_cmd_list,
3385                                 cmd_list_entry) {
3386                 if (c == cmd) {
3387                         TRACE_MGMT_DBG("Abort request for "
3388                                 "delayed cmd %p (tag=%llu), moving it to "
3389                                 "active cmd list (delayed_cmds_count=%d)",
3390                                 c, c->tag, tm_dbg_delayed_cmds_count);
3391
3392                         if (!test_bit(SCST_CMD_ABORTED_OTHER, &cmd->cmd_flags)) {
3393                                 /* Test how completed commands handled */
3394                                 if (((scst_random() % 10) == 5)) {
3395                                         scst_set_cmd_error(cmd,
3396                                            SCST_LOAD_SENSE(scst_sense_hardw_error));
3397                                         /* It's completed now */
3398                                 }
3399                         }
3400
3401                         spin_lock(&cmd->cmd_lists->cmd_list_lock);
3402                         list_move(&c->cmd_list_entry,
3403                                 &c->cmd_lists->active_cmd_list);
3404                         wake_up(&c->cmd_lists->cmd_list_waitQ);
3405                         spin_unlock(&cmd->cmd_lists->cmd_list_lock);
3406                         break;
3407                 }
3408         }
3409         spin_unlock_irqrestore(&scst_tm_dbg_lock, flags);
3410 }
3411
3412 /* Might be called under scst_mutex */
3413 void tm_dbg_task_mgmt(struct scst_device *dev, const char *fn, int force)
3414 {
3415         unsigned long flags;
3416
3417         if (dev != NULL) {
3418                 struct scst_tgt_dev *tgt_dev;
3419                 bool found = 0;
3420
3421                 spin_lock_bh(&dev->dev_lock);
3422                 list_for_each_entry(tgt_dev, &dev->dev_tgt_dev_list,
3423                                             dev_tgt_dev_list_entry) {
3424                         if (test_bit(SCST_TGT_DEV_UNDER_TM_DBG,
3425                                         &tgt_dev->tgt_dev_flags)) {
3426                                 found = 1;
3427                                 break;
3428                         }
3429                 }
3430                 spin_unlock_bh(&dev->dev_lock);
3431
3432                 if (!found)
3433                         goto out;
3434         }
3435
3436         spin_lock_irqsave(&scst_tm_dbg_lock, flags);
3437         if ((tm_dbg_state != TM_DBG_STATE_OFFLINE) || force) {
3438                 TRACE_MGMT_DBG("%s: freeing %d delayed cmds", fn,
3439                         tm_dbg_delayed_cmds_count);
3440                 tm_dbg_change_state();
3441                 tm_dbg_flags.tm_dbg_release = 1;
3442                 smp_wmb();
3443                 if (tm_dbg_p_cmd_list_waitQ != NULL)
3444                         wake_up_all(tm_dbg_p_cmd_list_waitQ);
3445         } else {
3446                 TRACE_MGMT_DBG("%s: while OFFLINE state, doing nothing", fn);
3447         }
3448         spin_unlock_irqrestore(&scst_tm_dbg_lock, flags);
3449
3450 out:
3451         return;
3452 }
3453
3454 int tm_dbg_is_release(void)
3455 {
3456         return tm_dbg_flags.tm_dbg_release;
3457 }
3458 #endif /* DEBUG_TM */
3459
3460 #ifdef DEBUG_SN
3461 void scst_check_debug_sn(struct scst_cmd *cmd)
3462 {
3463         static spinlock_t lock = SPIN_LOCK_UNLOCKED;
3464         static int type;
3465         static int cnt;
3466         unsigned long flags;
3467         int old = cmd->queue_type;
3468
3469         spin_lock_irqsave(&lock, flags);
3470
3471         if (cnt == 0) {
3472                 if ((scst_random() % 1000) == 500) {
3473                         if ((scst_random() % 3) == 1)
3474                                 type = SCST_CMD_QUEUE_HEAD_OF_QUEUE;
3475                         else
3476                                 type = SCST_CMD_QUEUE_ORDERED;
3477                         do {
3478                                 cnt = scst_random() % 10;
3479                         } while (cnt == 0);
3480                 } else
3481                         goto out_unlock;
3482         }
3483
3484         cmd->queue_type = type;
3485         cnt--;
3486
3487         if (((scst_random() % 1000) == 750))
3488                 cmd->queue_type = SCST_CMD_QUEUE_ORDERED;
3489         else if (((scst_random() % 1000) == 751))
3490                 cmd->queue_type = SCST_CMD_QUEUE_HEAD_OF_QUEUE;
3491         else if (((scst_random() % 1000) == 752))
3492                 cmd->queue_type = SCST_CMD_QUEUE_SIMPLE;
3493
3494         TRACE_SN("DbgSN changed cmd %p: %d/%d (cnt %d)", cmd, old,
3495                 cmd->queue_type, cnt);
3496
3497 out_unlock:
3498         spin_unlock_irqrestore(&lock, flags);
3499         return;
3500 }
3501 #endif /* DEBUG_SN */