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