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