Fixes issues cased by recent change of cdb_len to unsigned. Reported by Krzysztof...
[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 static int get_trans_cdb_len_10(struct scst_cmd *cmd, uint8_t off)
2481 {
2482         cmd->cdb_len = 10;      
2483         cmd->bufflen = 0;
2484         return 0;
2485 }
2486
2487 static int get_trans_len_block_limit(struct scst_cmd *cmd, uint8_t off)
2488 {
2489         cmd->bufflen = 6;
2490         return 0;
2491 }
2492
2493 static int get_trans_len_read_capacity(struct scst_cmd *cmd, uint8_t off)
2494 {
2495         cmd->bufflen = READ_CAP_LEN;
2496         return 0;
2497 }
2498
2499 static int get_trans_len_serv_act_in(struct scst_cmd *cmd, uint8_t off)
2500 {
2501         int res = 0;
2502
2503         TRACE_ENTRY();
2504
2505         if ((cmd->cdb[1] & 0x1f) == SAI_READ_CAPACITY_16) {
2506                 cmd->op_name = "READ CAPACITY(16)";
2507                 cmd->bufflen = READ_CAP16_LEN;
2508                 cmd->op_flags |= SCST_IMPLICIT_HQ;
2509         } else
2510                 cmd->op_flags |= SCST_UNKNOWN_LENGTH;
2511
2512         TRACE_EXIT_RES(res);
2513         return res;
2514 }
2515
2516 static int get_trans_len_single(struct scst_cmd *cmd, uint8_t off)
2517 {
2518         cmd->bufflen = 1;
2519         return 0;
2520 }
2521
2522 static int get_trans_len_read_pos(struct scst_cmd *cmd, uint8_t off)
2523 {
2524         uint8_t *p = (uint8_t *)cmd->cdb + off;
2525         int res = 0;
2526
2527         cmd->bufflen = 0;
2528         cmd->bufflen |= ((u32)p[0]) << 8;
2529         cmd->bufflen |= ((u32)p[1]);
2530
2531         switch (cmd->cdb[1] & 0x1f) {
2532         case 0:
2533         case 1:
2534         case 6:
2535                 if (cmd->bufflen != 0) {
2536                         PRINT_ERROR("READ POSITION: Invalid non-zero (%d) "
2537                                 "allocation length for service action %x",
2538                                 cmd->bufflen, cmd->cdb[1] & 0x1f);
2539                         goto out_inval;
2540                 }
2541                 break;
2542         }
2543
2544         switch (cmd->cdb[1] & 0x1f) {
2545         case 0:
2546         case 1:
2547                 cmd->bufflen = 20;
2548                 break;
2549         case 6:
2550                 cmd->bufflen = 32;
2551                 break;
2552         case 8:
2553                 cmd->bufflen = max(28, cmd->bufflen);
2554                 break;
2555         default:
2556                 PRINT_ERROR("READ POSITION: Invalid service action %x",
2557                         cmd->cdb[1] & 0x1f);
2558                 goto out_inval;
2559         }
2560
2561 out:
2562         return res;
2563
2564 out_inval:
2565         scst_set_cmd_error(cmd,
2566                 SCST_LOAD_SENSE(scst_sense_invalid_field_in_cdb));
2567         res = 1;
2568         goto out;
2569 }
2570
2571 static int get_trans_len_1(struct scst_cmd *cmd, uint8_t off)
2572 {
2573         cmd->bufflen = (u32)cmd->cdb[off];
2574         return 0;
2575 }
2576
2577 static int get_trans_len_1_256(struct scst_cmd *cmd, uint8_t off)
2578 {
2579         cmd->bufflen = (u32)cmd->cdb[off];
2580         if (cmd->bufflen == 0)
2581                 cmd->bufflen = 256;
2582         return 0;
2583 }
2584
2585 static int get_trans_len_2(struct scst_cmd *cmd, uint8_t off)
2586 {
2587         const uint8_t *p = cmd->cdb + off;
2588
2589         cmd->bufflen = 0;
2590         cmd->bufflen |= ((u32)p[0]) << 8;
2591         cmd->bufflen |= ((u32)p[1]);
2592
2593         return 0;
2594 }
2595
2596 static int get_trans_len_3(struct scst_cmd *cmd, uint8_t off)
2597 {
2598         const uint8_t *p = cmd->cdb + off;
2599
2600         cmd->bufflen = 0;
2601         cmd->bufflen |= ((u32)p[0]) << 16;
2602         cmd->bufflen |= ((u32)p[1]) << 8;
2603         cmd->bufflen |= ((u32)p[2]);
2604
2605         return 0;
2606 }
2607
2608 static int get_trans_len_4(struct scst_cmd *cmd, uint8_t off)
2609 {
2610         const uint8_t *p = cmd->cdb + off;
2611
2612         cmd->bufflen = 0;
2613         cmd->bufflen |= ((u32)p[0]) << 24;
2614         cmd->bufflen |= ((u32)p[1]) << 16;
2615         cmd->bufflen |= ((u32)p[2]) << 8;
2616         cmd->bufflen |= ((u32)p[3]);
2617
2618         return 0;
2619 }
2620
2621 static int get_trans_len_none(struct scst_cmd *cmd, uint8_t off)
2622 {
2623         cmd->bufflen = 0;
2624         return 0;
2625 }
2626
2627 int scst_get_cdb_info(struct scst_cmd *cmd)
2628 {
2629         int dev_type = cmd->dev->type;
2630         int i, res = 0;
2631         uint8_t op;
2632         const struct scst_sdbops *ptr = NULL;
2633
2634         TRACE_ENTRY();
2635
2636         op = cmd->cdb[0];       /* get clear opcode */
2637
2638         TRACE_DBG("opcode=%02x, cdblen=%d bytes, tblsize=%d, "
2639                 "dev_type=%d", op, SCST_GET_CDB_LEN(op), SCST_CDB_TBL_SIZE,
2640                 dev_type);
2641
2642         i = scst_scsi_op_list[op];
2643         while (i < SCST_CDB_TBL_SIZE && scst_scsi_op_table[i].ops == op) {
2644                 if (scst_scsi_op_table[i].devkey[dev_type] != SCST_CDB_NOTSUPP) {
2645                         ptr = &scst_scsi_op_table[i];
2646                         TRACE_DBG("op = 0x%02x+'%c%c%c%c%c%c%c%c%c%c'+<%s>",
2647                               ptr->ops, ptr->devkey[0], /* disk     */
2648                               ptr->devkey[1],   /* tape     */
2649                               ptr->devkey[2],   /* printer */
2650                               ptr->devkey[3],   /* cpu      */
2651                               ptr->devkey[4],   /* cdr      */
2652                               ptr->devkey[5],   /* cdrom    */
2653                               ptr->devkey[6],   /* scanner */
2654                               ptr->devkey[7],   /* worm     */
2655                               ptr->devkey[8],   /* changer */
2656                               ptr->devkey[9],   /* commdev */
2657                               ptr->op_name);
2658                         TRACE_DBG("direction=%d flags=%d off=%d",
2659                               ptr->direction,
2660                               ptr->flags,
2661                               ptr->off);
2662                         break;
2663                 }
2664                 i++;
2665         }
2666
2667         if (unlikely(ptr == NULL)) {
2668                 /* opcode not found or now not used !!! */
2669                 TRACE(TRACE_SCSI, "Unknown opcode 0x%x for type %d", op,
2670                       dev_type);
2671                 res = -1;
2672                 cmd->op_flags = SCST_INFO_NOT_FOUND;
2673                 goto out;
2674         }
2675
2676         cmd->cdb_len = SCST_GET_CDB_LEN(op);
2677         cmd->op_name = ptr->op_name;
2678         cmd->data_direction = ptr->direction;
2679         cmd->op_flags = ptr->flags;
2680         res = (*ptr->get_trans_len)(cmd, ptr->off);
2681
2682 out:
2683         TRACE_EXIT_RES(res);
2684         return res;
2685 }
2686 EXPORT_SYMBOL(scst_get_cdb_info);
2687
2688 /* Packs SCST LUN back to SCSI form using peripheral device addressing method */
2689 uint64_t scst_pack_lun(const uint64_t lun)
2690 {
2691         uint64_t res;
2692         uint16_t *p = (uint16_t *)&res;
2693
2694         res = lun;
2695         *p = cpu_to_be16(*p);
2696
2697         TRACE_EXIT_HRES((unsigned long)res);
2698         return res;
2699 }
2700
2701 /*
2702  * Routine to extract a lun number from an 8-byte LUN structure
2703  * in network byte order (BE).
2704  * (see SAM-2, Section 4.12.3 page 40)
2705  * Supports 2 types of lun unpacking: peripheral and logical unit.
2706  */
2707 uint64_t scst_unpack_lun(const uint8_t *lun, int len)
2708 {
2709         uint64_t res = NO_SUCH_LUN;
2710         int address_method;
2711
2712         TRACE_ENTRY();
2713
2714         TRACE_BUFF_FLAG(TRACE_DEBUG, "Raw LUN", lun, len);
2715
2716         if (unlikely(len < 2)) {
2717                 PRINT_ERROR("Illegal lun length %d, expected 2 bytes or "
2718                         "more", len);
2719                 goto out;
2720         }
2721
2722         if (len > 2) {
2723                 switch (len) {
2724                 case 8:
2725                         if ((*((uint64_t *)lun) &
2726                           __constant_cpu_to_be64(0x0000FFFFFFFFFFFFLL)) != 0)
2727                                 goto out_err;
2728                         break;
2729                 case 4:
2730                         if (*((uint16_t *)&lun[2]) != 0)
2731                                 goto out_err;
2732                         break;
2733                 case 6:
2734                         if (*((uint32_t *)&lun[2]) != 0)
2735                                 goto out_err;
2736                         break;
2737                 default:
2738                         goto out_err;
2739                 }
2740         }
2741
2742         address_method = (*lun) >> 6;   /* high 2 bits of byte 0 */
2743         switch (address_method) {
2744         case 0: /* peripheral device addressing method */
2745 #if 0
2746                 if (*lun) {
2747                         PRINT_ERROR("Illegal BUS INDENTIFIER in LUN "
2748                              "peripheral device addressing method 0x%02x, "
2749                              "expected 0", *lun);
2750                         break;
2751                 }
2752                 res = *(lun + 1);
2753                 break;
2754 #else
2755                 /*
2756                  * Looks like it's legal to use it as flat space addressing
2757                  * method as well
2758                  */
2759
2760                 /* go through */
2761 #endif
2762
2763         case 1: /* flat space addressing method */
2764                 res = *(lun + 1) | (((*lun) & 0x3f) << 8);
2765                 break;
2766
2767         case 2: /* logical unit addressing method */
2768                 if (*lun & 0x3f) {
2769                         PRINT_ERROR("Illegal BUS NUMBER in LUN logical unit "
2770                                     "addressing method 0x%02x, expected 0",
2771                                     *lun & 0x3f);
2772                         break;
2773                 }
2774                 if (*(lun + 1) & 0xe0) {
2775                         PRINT_ERROR("Illegal TARGET in LUN logical unit "
2776                                     "addressing method 0x%02x, expected 0",
2777                                     (*(lun + 1) & 0xf8) >> 5);
2778                         break;
2779                 }
2780                 res = *(lun + 1) & 0x1f;
2781                 break;
2782
2783         case 3: /* extended logical unit addressing method */
2784         default:
2785                 PRINT_ERROR("Unimplemented LUN addressing method %u",
2786                             address_method);
2787                 break;
2788         }
2789
2790 out:
2791         TRACE_EXIT_RES((int)res);
2792         return res;
2793
2794 out_err:
2795         PRINT_ERROR("%s", "Multi-level LUN unimplemented");
2796         goto out;
2797 }
2798
2799 int scst_calc_block_shift(int sector_size)
2800 {
2801         int block_shift = 0;
2802         int t;
2803
2804         if (sector_size == 0)
2805                 sector_size = 512;
2806
2807         t = sector_size;
2808         while (1) {
2809                 if ((t & 1) != 0)
2810                         break;
2811                 t >>= 1;
2812                 block_shift++;
2813         }
2814         if (block_shift < 9) {
2815                 PRINT_ERROR("Wrong sector size %d", sector_size);
2816                 block_shift = -1;
2817         }
2818
2819         TRACE_EXIT_RES(block_shift);
2820         return block_shift;
2821 }
2822 EXPORT_SYMBOL(scst_calc_block_shift);
2823
2824 int scst_sbc_generic_parse(struct scst_cmd *cmd,
2825         int (*get_block_shift)(struct scst_cmd *cmd))
2826 {
2827         int res = 0;
2828
2829         TRACE_ENTRY();
2830
2831         /*
2832          * SCST sets good defaults for cmd->data_direction and cmd->bufflen,
2833          * therefore change them only if necessary
2834          */
2835
2836         TRACE_DBG("op_name <%s> direct %d flags %d transfer_len %d",
2837               cmd->op_name, cmd->data_direction, cmd->op_flags, cmd->bufflen);
2838
2839         switch (cmd->cdb[0]) {
2840         case VERIFY_6:
2841         case VERIFY:
2842         case VERIFY_12:
2843         case VERIFY_16:
2844                 if ((cmd->cdb[1] & BYTCHK) == 0) {
2845                         cmd->data_len = cmd->bufflen << get_block_shift(cmd);
2846                         cmd->bufflen = 0;
2847                         goto set_timeout;
2848                 } else
2849                         cmd->data_len = 0;
2850                 break;
2851         default:
2852                 /* It's all good */
2853                 break;
2854         }
2855
2856         if (cmd->op_flags & SCST_TRANSFER_LEN_TYPE_FIXED) {
2857                 /*
2858                  * No need for locks here, since *_detach() can not be
2859                  * called, when there are existing commands.
2860                  */
2861                 cmd->bufflen = cmd->bufflen << get_block_shift(cmd);
2862         }
2863
2864 set_timeout:
2865         if ((cmd->op_flags & (SCST_SMALL_TIMEOUT | SCST_LONG_TIMEOUT)) == 0)
2866                 cmd->timeout = SCST_GENERIC_DISK_REG_TIMEOUT;
2867         else if (cmd->op_flags & SCST_SMALL_TIMEOUT)
2868                 cmd->timeout = SCST_GENERIC_DISK_SMALL_TIMEOUT;
2869         else if (cmd->op_flags & SCST_LONG_TIMEOUT)
2870                 cmd->timeout = SCST_GENERIC_DISK_LONG_TIMEOUT;
2871
2872         TRACE_DBG("res %d, bufflen %d, data_len %d, direct %d",
2873               res, cmd->bufflen, cmd->data_len, cmd->data_direction);
2874
2875         TRACE_EXIT_RES(res);
2876         return res;
2877 }
2878 EXPORT_SYMBOL(scst_sbc_generic_parse);
2879
2880 int scst_cdrom_generic_parse(struct scst_cmd *cmd,
2881         int (*get_block_shift)(struct scst_cmd *cmd))
2882 {
2883         int res = 0;
2884
2885         TRACE_ENTRY();
2886
2887         /*
2888          * SCST sets good defaults for cmd->data_direction and cmd->bufflen,
2889          * therefore change them only if necessary
2890          */
2891
2892         TRACE_DBG("op_name <%s> direct %d flags %d transfer_len %d",
2893               cmd->op_name, cmd->data_direction, cmd->op_flags, cmd->bufflen);
2894
2895         cmd->cdb[1] &= 0x1f;
2896
2897         switch (cmd->cdb[0]) {
2898         case VERIFY_6:
2899         case VERIFY:
2900         case VERIFY_12:
2901         case VERIFY_16:
2902                 if ((cmd->cdb[1] & BYTCHK) == 0) {
2903                         cmd->data_len = cmd->bufflen << get_block_shift(cmd);
2904                         cmd->bufflen = 0;
2905                         goto set_timeout;
2906                 }
2907                 break;
2908         default:
2909                 /* It's all good */
2910                 break;
2911         }
2912
2913         if (cmd->op_flags & SCST_TRANSFER_LEN_TYPE_FIXED)
2914                 cmd->bufflen = cmd->bufflen << get_block_shift(cmd);
2915
2916 set_timeout:
2917         if ((cmd->op_flags & (SCST_SMALL_TIMEOUT | SCST_LONG_TIMEOUT)) == 0)
2918                 cmd->timeout = SCST_GENERIC_CDROM_REG_TIMEOUT;
2919         else if (cmd->op_flags & SCST_SMALL_TIMEOUT)
2920                 cmd->timeout = SCST_GENERIC_CDROM_SMALL_TIMEOUT;
2921         else if (cmd->op_flags & SCST_LONG_TIMEOUT)
2922                 cmd->timeout = SCST_GENERIC_CDROM_LONG_TIMEOUT;
2923
2924         TRACE_DBG("res=%d, bufflen=%d, direct=%d", res, cmd->bufflen,
2925                 cmd->data_direction);
2926
2927         TRACE_EXIT();
2928         return res;
2929 }
2930 EXPORT_SYMBOL(scst_cdrom_generic_parse);
2931
2932 int scst_modisk_generic_parse(struct scst_cmd *cmd,
2933         int (*get_block_shift)(struct scst_cmd *cmd))
2934 {
2935         int res = 0;
2936
2937         TRACE_ENTRY();
2938
2939         /*
2940          * SCST sets good defaults for cmd->data_direction and cmd->bufflen,
2941          * therefore change them only if necessary
2942          */
2943
2944         TRACE_DBG("op_name <%s> direct %d flags %d transfer_len %d",
2945               cmd->op_name, cmd->data_direction, cmd->op_flags, cmd->bufflen);
2946
2947         cmd->cdb[1] &= 0x1f;
2948
2949         switch (cmd->cdb[0]) {
2950         case VERIFY_6:
2951         case VERIFY:
2952         case VERIFY_12:
2953         case VERIFY_16:
2954                 if ((cmd->cdb[1] & BYTCHK) == 0) {
2955                         cmd->data_len = cmd->bufflen << get_block_shift(cmd);
2956                         cmd->bufflen = 0;
2957                         goto set_timeout;
2958                 }
2959                 break;
2960         default:
2961                 /* It's all good */
2962                 break;
2963         }
2964
2965         if (cmd->op_flags & SCST_TRANSFER_LEN_TYPE_FIXED)
2966                 cmd->bufflen = cmd->bufflen << get_block_shift(cmd);
2967
2968 set_timeout:
2969         if ((cmd->op_flags & (SCST_SMALL_TIMEOUT | SCST_LONG_TIMEOUT)) == 0)
2970                 cmd->timeout = SCST_GENERIC_MODISK_REG_TIMEOUT;
2971         else if (cmd->op_flags & SCST_SMALL_TIMEOUT)
2972                 cmd->timeout = SCST_GENERIC_MODISK_SMALL_TIMEOUT;
2973         else if (cmd->op_flags & SCST_LONG_TIMEOUT)
2974                 cmd->timeout = SCST_GENERIC_MODISK_LONG_TIMEOUT;
2975
2976         TRACE_DBG("res=%d, bufflen=%d, direct=%d", res, cmd->bufflen,
2977                 cmd->data_direction);
2978
2979         TRACE_EXIT_RES(res);
2980         return res;
2981 }
2982 EXPORT_SYMBOL(scst_modisk_generic_parse);
2983
2984 int scst_tape_generic_parse(struct scst_cmd *cmd,
2985         int (*get_block_size)(struct scst_cmd *cmd))
2986 {
2987         int res = 0;
2988
2989         TRACE_ENTRY();
2990
2991         /*
2992          * SCST sets good defaults for cmd->data_direction and cmd->bufflen,
2993          * therefore change them only if necessary
2994          */
2995
2996         TRACE_DBG("op_name <%s> direct %d flags %d transfer_len %d",
2997               cmd->op_name, cmd->data_direction, cmd->op_flags, cmd->bufflen);
2998
2999         if (cmd->cdb[0] == READ_POSITION) {
3000                 int tclp = cmd->cdb[1] & 4;
3001                 int long_bit = cmd->cdb[1] & 2;
3002                 int bt = cmd->cdb[1] & 1;
3003
3004                 if ((tclp == long_bit) && (!bt || !long_bit)) {
3005                         cmd->bufflen =
3006                             tclp ? POSITION_LEN_LONG : POSITION_LEN_SHORT;
3007                         cmd->data_direction = SCST_DATA_READ;
3008                 } else {
3009                         cmd->bufflen = 0;
3010                         cmd->data_direction = SCST_DATA_NONE;
3011                 }
3012         }
3013
3014         if (cmd->op_flags & SCST_TRANSFER_LEN_TYPE_FIXED & cmd->cdb[1])
3015                 cmd->bufflen = cmd->bufflen * get_block_size(cmd);
3016
3017         if ((cmd->op_flags & (SCST_SMALL_TIMEOUT | SCST_LONG_TIMEOUT)) == 0)
3018                 cmd->timeout = SCST_GENERIC_TAPE_REG_TIMEOUT;
3019         else if (cmd->op_flags & SCST_SMALL_TIMEOUT)
3020                 cmd->timeout = SCST_GENERIC_TAPE_SMALL_TIMEOUT;
3021         else if (cmd->op_flags & SCST_LONG_TIMEOUT)
3022                 cmd->timeout = SCST_GENERIC_TAPE_LONG_TIMEOUT;
3023
3024         TRACE_EXIT_RES(res);
3025         return res;
3026 }
3027 EXPORT_SYMBOL(scst_tape_generic_parse);
3028
3029 static int scst_null_parse(struct scst_cmd *cmd)
3030 {
3031         int res = 0;
3032
3033         TRACE_ENTRY();
3034
3035         /*
3036          * SCST sets good defaults for cmd->data_direction and cmd->bufflen,
3037          * therefore change them only if necessary
3038          */
3039
3040         TRACE_DBG("op_name <%s> direct %d flags %d transfer_len %d",
3041               cmd->op_name, cmd->data_direction, cmd->op_flags, cmd->bufflen);
3042 #if 0
3043         switch (cmd->cdb[0]) {
3044         default:
3045                 /* It's all good */
3046                 break;
3047         }
3048 #endif
3049         TRACE_DBG("res %d bufflen %d direct %d",
3050               res, cmd->bufflen, cmd->data_direction);
3051
3052         TRACE_EXIT();
3053         return res;
3054 }
3055
3056 int scst_changer_generic_parse(struct scst_cmd *cmd,
3057         int (*nothing)(struct scst_cmd *cmd))
3058 {
3059         int res = scst_null_parse(cmd);
3060
3061         if (cmd->op_flags & SCST_LONG_TIMEOUT)
3062                 cmd->timeout = SCST_GENERIC_CHANGER_LONG_TIMEOUT;
3063         else
3064                 cmd->timeout = SCST_GENERIC_CHANGER_TIMEOUT;
3065
3066         return res;
3067 }
3068 EXPORT_SYMBOL(scst_changer_generic_parse);
3069
3070 int scst_processor_generic_parse(struct scst_cmd *cmd,
3071         int (*nothing)(struct scst_cmd *cmd))
3072 {
3073         int res = scst_null_parse(cmd);
3074
3075         if (cmd->op_flags & SCST_LONG_TIMEOUT)
3076                 cmd->timeout = SCST_GENERIC_PROCESSOR_LONG_TIMEOUT;
3077         else
3078                 cmd->timeout = SCST_GENERIC_PROCESSOR_TIMEOUT;
3079
3080         return res;
3081 }
3082 EXPORT_SYMBOL(scst_processor_generic_parse);
3083
3084 int scst_raid_generic_parse(struct scst_cmd *cmd,
3085         int (*nothing)(struct scst_cmd *cmd))
3086 {
3087         int res = scst_null_parse(cmd);
3088
3089         if (cmd->op_flags & SCST_LONG_TIMEOUT)
3090                 cmd->timeout = SCST_GENERIC_RAID_LONG_TIMEOUT;
3091         else
3092                 cmd->timeout = SCST_GENERIC_RAID_TIMEOUT;
3093
3094         return res;
3095 }
3096 EXPORT_SYMBOL(scst_raid_generic_parse);
3097
3098 int scst_block_generic_dev_done(struct scst_cmd *cmd,
3099         void (*set_block_shift)(struct scst_cmd *cmd, int block_shift))
3100 {
3101         int opcode = cmd->cdb[0];
3102         int status = cmd->status;
3103         int res = SCST_CMD_STATE_DEFAULT;
3104
3105         TRACE_ENTRY();
3106
3107         /*
3108          * SCST sets good defaults for cmd->is_send_status and
3109          * cmd->resp_data_len based on cmd->status and cmd->data_direction,
3110          * therefore change them only if necessary
3111          */
3112
3113         if ((status == SAM_STAT_GOOD) || (status == SAM_STAT_CONDITION_MET)) {
3114                 switch (opcode) {
3115                 case READ_CAPACITY:
3116                 {
3117                         /* Always keep track of disk capacity */
3118                         int buffer_size, sector_size, sh;
3119                         uint8_t *buffer;
3120
3121                         buffer_size = scst_get_buf_first(cmd, &buffer);
3122                         if (unlikely(buffer_size <= 0)) {
3123                                 if (buffer_size < 0) {
3124                                         PRINT_ERROR("%s: Unable to get the"
3125                                         " buffer (%d)", __func__, buffer_size);
3126                                 }
3127                                 goto out;
3128                         }
3129
3130                         sector_size =
3131                             ((buffer[4] << 24) | (buffer[5] << 16) |
3132                              (buffer[6] << 8) | (buffer[7] << 0));
3133                         scst_put_buf(cmd, buffer);
3134                         if (sector_size != 0)
3135                                 sh = scst_calc_block_shift(sector_size);
3136                         else
3137                                 sh = 0;
3138                         set_block_shift(cmd, sh);
3139                         TRACE_DBG("block_shift %d", sh);
3140                         break;
3141                 }
3142                 default:
3143                         /* It's all good */
3144                         break;
3145                 }
3146         }
3147
3148         TRACE_DBG("cmd->is_send_status=%x, cmd->resp_data_len=%d, "
3149               "res=%d", cmd->is_send_status, cmd->resp_data_len, res);
3150
3151 out:
3152         TRACE_EXIT_RES(res);
3153         return res;
3154 }
3155 EXPORT_SYMBOL(scst_block_generic_dev_done);
3156
3157 int scst_tape_generic_dev_done(struct scst_cmd *cmd,
3158         void (*set_block_size)(struct scst_cmd *cmd, int block_shift))
3159 {
3160         int opcode = cmd->cdb[0];
3161         int res = SCST_CMD_STATE_DEFAULT;
3162         int buffer_size, bs;
3163         uint8_t *buffer = NULL;
3164
3165         TRACE_ENTRY();
3166
3167         /*
3168          * SCST sets good defaults for cmd->is_send_status and
3169          * cmd->resp_data_len based on cmd->status and cmd->data_direction,
3170          * therefore change them only if necessary
3171          */
3172
3173         switch (opcode) {
3174         case MODE_SENSE:
3175         case MODE_SELECT:
3176                 buffer_size = scst_get_buf_first(cmd, &buffer);
3177                 if (unlikely(buffer_size <= 0)) {
3178                         if (buffer_size < 0) {
3179                                 PRINT_ERROR("%s: Unable to get the buffer (%d)",
3180                                         __func__, buffer_size);
3181                         }
3182                         goto out;
3183                 }
3184                 break;
3185         }
3186
3187         switch (opcode) {
3188         case MODE_SENSE:
3189                 TRACE_DBG("%s", "MODE_SENSE");
3190                 if ((cmd->cdb[2] & 0xC0) == 0) {
3191                         if (buffer[3] == 8) {
3192                                 bs = (buffer[9] << 16) |
3193                                     (buffer[10] << 8) | buffer[11];
3194                                 set_block_size(cmd, bs);
3195                         }
3196                 }
3197                 break;
3198         case MODE_SELECT:
3199                 TRACE_DBG("%s", "MODE_SELECT");
3200                 if (buffer[3] == 8) {
3201                         bs = (buffer[9] << 16) | (buffer[10] << 8) |
3202                             (buffer[11]);
3203                         set_block_size(cmd, bs);
3204                 }
3205                 break;
3206         default:
3207                 /* It's all good */
3208                 break;
3209         }
3210
3211         switch (opcode) {
3212         case MODE_SENSE:
3213         case MODE_SELECT:
3214                 scst_put_buf(cmd, buffer);
3215                 break;
3216         }
3217
3218 out:
3219         TRACE_EXIT_RES(res);
3220         return res;
3221 }
3222 EXPORT_SYMBOL(scst_tape_generic_dev_done);
3223
3224 static void scst_check_internal_sense(struct scst_device *dev, int result,
3225         uint8_t *sense, int sense_len)
3226 {
3227         TRACE_ENTRY();
3228
3229         if (host_byte(result) == DID_RESET) {
3230                 TRACE(TRACE_MGMT_MINOR, "%s", "DID_RESET received, triggering "
3231                         "reset UA");
3232                 scst_set_sense(sense, sense_len, dev->d_sense,
3233                         SCST_LOAD_SENSE(scst_sense_reset_UA));
3234                 scst_dev_check_set_UA(dev, NULL, sense, sense_len);
3235         } else if ((status_byte(result) == CHECK_CONDITION) &&
3236                    SCST_SENSE_VALID(sense) && scst_is_ua_sense(sense))
3237                 scst_dev_check_set_UA(dev, NULL, sense, sense_len);
3238
3239         TRACE_EXIT();
3240         return;
3241 }
3242
3243 enum dma_data_direction scst_to_dma_dir(int scst_dir)
3244 {
3245         static const enum dma_data_direction tr_tbl[] = { DMA_NONE,
3246                 DMA_TO_DEVICE, DMA_FROM_DEVICE, DMA_BIDIRECTIONAL, DMA_NONE };
3247
3248         return tr_tbl[scst_dir];
3249 }
3250 EXPORT_SYMBOL(scst_to_dma_dir);
3251
3252 enum dma_data_direction scst_to_tgt_dma_dir(int scst_dir)
3253 {
3254         static const enum dma_data_direction tr_tbl[] = { DMA_NONE,
3255                 DMA_FROM_DEVICE, DMA_TO_DEVICE, DMA_BIDIRECTIONAL, DMA_NONE };
3256
3257         return tr_tbl[scst_dir];
3258 }
3259 EXPORT_SYMBOL(scst_to_tgt_dma_dir);
3260
3261 int scst_obtain_device_parameters(struct scst_device *dev)
3262 {
3263         int res = 0, i;
3264         uint8_t cmd[16];
3265         uint8_t buffer[4+0x0A];
3266         uint8_t sense_buffer[SCSI_SENSE_BUFFERSIZE];
3267
3268         TRACE_ENTRY();
3269
3270         EXTRACHECKS_BUG_ON(dev->scsi_dev == NULL);
3271
3272         for (i = 0; i < 5; i++) {
3273                 /* Get control mode page */
3274                 memset(cmd, 0, sizeof(cmd));
3275                 cmd[0] = MODE_SENSE;
3276                 cmd[1] = 8; /* DBD */
3277                 cmd[2] = 0x0A;
3278                 cmd[4] = sizeof(buffer);
3279
3280                 memset(buffer, 0, sizeof(buffer));
3281                 memset(sense_buffer, 0, sizeof(sense_buffer));
3282
3283                 TRACE(TRACE_SCSI, "%s", "Doing internal MODE_SENSE");
3284                 res = scsi_execute(dev->scsi_dev, cmd, SCST_DATA_READ, buffer,
3285                                 sizeof(buffer), sense_buffer, 15, 0, 0
3286 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,29)
3287                                 , NULL
3288 #endif
3289                                 );
3290
3291                 TRACE_DBG("MODE_SENSE done: %x", res);
3292
3293                 if (scsi_status_is_good(res)) {
3294                         int q;
3295
3296                         PRINT_BUFF_FLAG(TRACE_SCSI,
3297                                 "Returned control mode page data",
3298                                 buffer, sizeof(buffer));
3299
3300                         dev->tst = buffer[4+2] >> 5;
3301                         q = buffer[4+3] >> 4;
3302                         if (q > SCST_CONTR_MODE_QUEUE_ALG_UNRESTRICTED_REORDER) {
3303                                 PRINT_ERROR("Too big QUEUE ALG %x, dev "
3304                                         "%d:%d:%d:%d", dev->queue_alg,
3305                                         dev->scsi_dev->host->host_no,
3306                                         dev->scsi_dev->channel,
3307                                         dev->scsi_dev->id, dev->scsi_dev->lun);
3308                         }
3309                         dev->queue_alg = q;
3310                         dev->swp = (buffer[4+4] & 0x8) >> 3;
3311                         dev->tas = (buffer[4+5] & 0x40) >> 6;
3312                         dev->d_sense = (buffer[4+2] & 0x4) >> 2;
3313
3314                         /*
3315                          * Unfortunately, SCSI ML doesn't provide a way to
3316                          * specify commands task attribute, so we can rely on
3317                          * device's restricted reordering only.
3318                          */
3319                         dev->has_own_order_mgmt = !dev->queue_alg;
3320
3321                         TRACE(TRACE_SCSI|TRACE_MGMT_MINOR,
3322                                 "Device %d:%d:%d:%d: TST %x, "
3323                                 "QUEUE ALG %x, SWP %x, TAS %x, D_SENSE %d"
3324                                 "has_own_order_mgmt %d",
3325                                 dev->scsi_dev->host->host_no,
3326                                 dev->scsi_dev->channel, dev->scsi_dev->id,
3327                                 dev->scsi_dev->lun, dev->tst, dev->queue_alg,
3328                                 dev->swp, dev->tas, dev->d_sense,
3329                                 dev->has_own_order_mgmt);
3330
3331                         goto out;
3332                 } else {
3333 #if 0
3334                         if ((status_byte(res) == CHECK_CONDITION) &&
3335                             SCST_SENSE_VALID(sense_buffer)) {
3336 #else
3337                         /*
3338                          * 3ware controller is buggy and returns CONDITION_GOOD
3339                          * instead of CHECK_CONDITION
3340                          */
3341                         if (SCST_SENSE_VALID(sense_buffer)) {
3342 #endif
3343                                 if (scst_analyze_sense(sense_buffer,
3344                                                 sizeof(sense_buffer),
3345                                                 SCST_SENSE_KEY_VALID,
3346                                                 ILLEGAL_REQUEST, 0, 0)) {
3347                                         TRACE(TRACE_SCSI|TRACE_MGMT_MINOR,
3348                                                 "Device %d:%d:%d:%d doesn't "
3349                                                 "support control mode page, "
3350                                                 "using defaults: TST %x, "
3351                                                 "QUEUE ALG %x, SWP %x, "
3352                                                 "TAS %x, D_SENSE %d, "
3353                                                 "has_own_order_mgmt %d ",
3354                                                 dev->scsi_dev->host->host_no,
3355                                                 dev->scsi_dev->channel,
3356                                                 dev->scsi_dev->id,
3357                                                 dev->scsi_dev->lun,
3358                                                 dev->tst, dev->queue_alg,
3359                                                 dev->swp, dev->tas,
3360                                                 dev->d_sense,
3361                                                 dev->has_own_order_mgmt);
3362                                         res = 0;
3363                                         goto out;
3364                                 } else if (scst_analyze_sense(sense_buffer,
3365                                                 sizeof(sense_buffer),
3366                                                 SCST_SENSE_KEY_VALID,
3367                                                 NOT_READY, 0, 0)) {
3368                                         TRACE(TRACE_SCSI,
3369                                                 "Device %d:%d:%d:%d not ready",
3370                                                 dev->scsi_dev->host->host_no,
3371                                                 dev->scsi_dev->channel,
3372                                                 dev->scsi_dev->id,
3373                                                 dev->scsi_dev->lun);
3374                                         res = 0;
3375                                         goto out;
3376                                 }
3377                         } else {
3378                                 TRACE(TRACE_SCSI|TRACE_MGMT_MINOR,
3379                                         "Internal MODE SENSE to "
3380                                         "device %d:%d:%d:%d failed: %x",
3381                                         dev->scsi_dev->host->host_no,
3382                                         dev->scsi_dev->channel,
3383                                         dev->scsi_dev->id,
3384                                         dev->scsi_dev->lun, res);
3385                                 PRINT_BUFF_FLAG(TRACE_SCSI|TRACE_MGMT_MINOR,
3386                                         "MODE SENSE sense",
3387                                         sense_buffer, sizeof(sense_buffer));
3388                         }
3389                         scst_check_internal_sense(dev, res, sense_buffer,
3390                                         sizeof(sense_buffer));
3391                 }
3392         }
3393         res = -ENODEV;
3394
3395 out:
3396         TRACE_EXIT_RES(res);
3397         return res;
3398 }
3399 EXPORT_SYMBOL(scst_obtain_device_parameters);
3400
3401 /* Called under dev_lock and BH off */
3402 void scst_process_reset(struct scst_device *dev,
3403         struct scst_session *originator, struct scst_cmd *exclude_cmd,
3404         struct scst_mgmt_cmd *mcmd, bool setUA)
3405 {
3406         struct scst_tgt_dev *tgt_dev;
3407         struct scst_cmd *cmd, *tcmd;
3408
3409         TRACE_ENTRY();
3410
3411         /* Clear RESERVE'ation, if necessary */
3412         if (dev->dev_reserved) {
3413                 list_for_each_entry(tgt_dev, &dev->dev_tgt_dev_list,
3414                                     dev_tgt_dev_list_entry) {
3415                         TRACE(TRACE_MGMT_MINOR, "Clearing RESERVE'ation for "
3416                                 "tgt_dev lun %lld",
3417                                 (long long unsigned int)tgt_dev->lun);
3418                         clear_bit(SCST_TGT_DEV_RESERVED,
3419                                   &tgt_dev->tgt_dev_flags);
3420                 }
3421                 dev->dev_reserved = 0;
3422                 /*
3423                  * There is no need to send RELEASE, since the device is going
3424                  * to be resetted. Actually, since we can be in RESET TM
3425                  * function, it might be dangerous.
3426                  */
3427         }
3428
3429         dev->dev_double_ua_possible = 1;
3430
3431         list_for_each_entry(tgt_dev, &dev->dev_tgt_dev_list,
3432                 dev_tgt_dev_list_entry) {
3433                 struct scst_session *sess = tgt_dev->sess;
3434
3435                 spin_lock_bh(&tgt_dev->tgt_dev_lock);
3436
3437                 scst_free_all_UA(tgt_dev);
3438
3439                 memset(tgt_dev->tgt_dev_sense, 0,
3440                         sizeof(tgt_dev->tgt_dev_sense));
3441
3442                 spin_unlock_bh(&tgt_dev->tgt_dev_lock);
3443
3444                 spin_lock_irq(&sess->sess_list_lock);
3445
3446                 TRACE_DBG("Searching in search cmd list (sess=%p)", sess);
3447                 list_for_each_entry(cmd, &sess->search_cmd_list,
3448                                 search_cmd_list_entry) {
3449                         if (cmd == exclude_cmd)
3450                                 continue;
3451                         if ((cmd->tgt_dev == tgt_dev) ||
3452                             ((cmd->tgt_dev == NULL) &&
3453                              (cmd->lun == tgt_dev->lun))) {
3454                                 scst_abort_cmd(cmd, mcmd,
3455                                         (tgt_dev->sess != originator), 0);
3456                         }
3457                 }
3458                 spin_unlock_irq(&sess->sess_list_lock);
3459         }
3460
3461         list_for_each_entry_safe(cmd, tcmd, &dev->blocked_cmd_list,
3462                                 blocked_cmd_list_entry) {
3463                 if (test_bit(SCST_CMD_ABORTED, &cmd->cmd_flags)) {
3464                         list_del(&cmd->blocked_cmd_list_entry);
3465                         TRACE_MGMT_DBG("Adding aborted blocked cmd %p "
3466                                 "to active cmd list", cmd);
3467                         spin_lock_irq(&cmd->cmd_lists->cmd_list_lock);
3468                         list_add_tail(&cmd->cmd_list_entry,
3469                                 &cmd->cmd_lists->active_cmd_list);
3470                         wake_up(&cmd->cmd_lists->cmd_list_waitQ);
3471                         spin_unlock_irq(&cmd->cmd_lists->cmd_list_lock);
3472                 }
3473         }
3474
3475         if (setUA) {
3476                 uint8_t sense_buffer[SCST_STANDARD_SENSE_LEN];
3477                 scst_set_sense(sense_buffer, sizeof(sense_buffer),
3478                         dev->d_sense, SCST_LOAD_SENSE(scst_sense_reset_UA));
3479                 scst_dev_check_set_local_UA(dev, exclude_cmd, sense_buffer,
3480                         sizeof(sense_buffer));
3481         }
3482
3483         TRACE_EXIT();
3484         return;
3485 }
3486
3487 int scst_set_pending_UA(struct scst_cmd *cmd)
3488 {
3489         int res = 0, i;
3490         struct scst_tgt_dev_UA *UA_entry;
3491         bool first = true, global_unlock = false;
3492         struct scst_session *sess = cmd->sess;
3493
3494         TRACE_ENTRY();
3495
3496         TRACE(TRACE_MGMT_MINOR, "Setting pending UA cmd %p", cmd);
3497
3498         spin_lock_bh(&cmd->tgt_dev->tgt_dev_lock);
3499
3500 again:
3501         /* UA list could be cleared behind us, so retest */
3502         if (list_empty(&cmd->tgt_dev->UA_list)) {
3503                 TRACE_DBG("%s",
3504                       "SCST_TGT_DEV_UA_PENDING set, but UA_list empty");
3505                 res = -1;
3506                 goto out_unlock;
3507         }
3508
3509         UA_entry = list_entry(cmd->tgt_dev->UA_list.next, typeof(*UA_entry),
3510                               UA_list_entry);
3511
3512         TRACE_DBG("next %p UA_entry %p",
3513               cmd->tgt_dev->UA_list.next, UA_entry);
3514
3515         if (UA_entry->global_UA && first) {
3516                 TRACE_MGMT_DBG("Global UA %p detected", UA_entry);
3517
3518                 spin_unlock_bh(&cmd->tgt_dev->tgt_dev_lock);
3519
3520                 mutex_lock(&scst_mutex);
3521                 local_bh_disable();
3522
3523                 for (i = 0; i < TGT_DEV_HASH_SIZE; i++) {
3524                         struct list_head *sess_tgt_dev_list_head =
3525                                 &sess->sess_tgt_dev_list_hash[i];
3526                         struct scst_tgt_dev *tgt_dev;
3527                         list_for_each_entry(tgt_dev, sess_tgt_dev_list_head,
3528                                         sess_tgt_dev_list_entry) {
3529                                 /* Lockdep triggers here a false positive.. */
3530                                 spin_lock(&tgt_dev->tgt_dev_lock);
3531                         }
3532                 }
3533
3534                 first = false;
3535                 global_unlock = true;
3536                 goto again;
3537         }
3538
3539         scst_set_cmd_error_sense(cmd, UA_entry->UA_sense_buffer,
3540                 sizeof(UA_entry->UA_sense_buffer));
3541
3542         cmd->ua_ignore = 1;
3543
3544         list_del(&UA_entry->UA_list_entry);
3545
3546         if (UA_entry->global_UA) {
3547                 for (i = 0; i < TGT_DEV_HASH_SIZE; i++) {
3548                         struct list_head *sess_tgt_dev_list_head =
3549                                 &sess->sess_tgt_dev_list_hash[i];
3550                         struct scst_tgt_dev *tgt_dev;
3551
3552                         list_for_each_entry(tgt_dev, sess_tgt_dev_list_head,
3553                                         sess_tgt_dev_list_entry) {
3554                                 struct scst_tgt_dev_UA *ua;
3555                                 list_for_each_entry(ua, &tgt_dev->UA_list,
3556                                                         UA_list_entry) {
3557                                         if (ua->global_UA &&
3558                                             memcmp(ua->UA_sense_buffer,
3559                                                 UA_entry->UA_sense_buffer,
3560                                              sizeof(ua->UA_sense_buffer)) == 0) {
3561                                                 TRACE_MGMT_DBG("Freeing not "
3562                                                         "needed global UA %p",
3563                                                         ua);
3564                                                 list_del(&ua->UA_list_entry);
3565                                                 mempool_free(ua, scst_ua_mempool);
3566                                                 break;
3567                                         }
3568                                 }
3569                         }
3570                 }
3571         }
3572
3573         mempool_free(UA_entry, scst_ua_mempool);
3574
3575         if (list_empty(&cmd->tgt_dev->UA_list)) {
3576                 clear_bit(SCST_TGT_DEV_UA_PENDING,
3577                           &cmd->tgt_dev->tgt_dev_flags);
3578         }
3579
3580 out_unlock:
3581         if (global_unlock) {
3582                 for (i = TGT_DEV_HASH_SIZE-1; i >= 0; i--) {
3583                         struct list_head *sess_tgt_dev_list_head =
3584                                 &sess->sess_tgt_dev_list_hash[i];
3585                         struct scst_tgt_dev *tgt_dev;
3586                         list_for_each_entry_reverse(tgt_dev, sess_tgt_dev_list_head,
3587                                         sess_tgt_dev_list_entry) {
3588                                 spin_unlock(&tgt_dev->tgt_dev_lock);
3589                         }
3590                 }
3591
3592                 local_bh_enable();
3593                 mutex_unlock(&scst_mutex);
3594
3595                 spin_lock_bh(&cmd->tgt_dev->tgt_dev_lock);
3596         }
3597
3598         spin_unlock_bh(&cmd->tgt_dev->tgt_dev_lock);
3599
3600         TRACE_EXIT_RES(res);
3601         return res;
3602 }
3603
3604 /* Called under tgt_dev_lock and BH off */
3605 static void scst_alloc_set_UA(struct scst_tgt_dev *tgt_dev,
3606         const uint8_t *sense, int sense_len, int flags)
3607 {
3608         struct scst_tgt_dev_UA *UA_entry = NULL;
3609
3610         TRACE_ENTRY();
3611
3612         UA_entry = mempool_alloc(scst_ua_mempool, GFP_ATOMIC);
3613         if (UA_entry == NULL) {
3614                 PRINT_CRIT_ERROR("%s", "UNIT ATTENTION memory "
3615                      "allocation failed. The UNIT ATTENTION "
3616                      "on some sessions will be missed");
3617                 PRINT_BUFFER("Lost UA", sense, sense_len);
3618                 goto out;
3619         }
3620         memset(UA_entry, 0, sizeof(*UA_entry));
3621
3622         UA_entry->global_UA = (flags & SCST_SET_UA_FLAG_GLOBAL) != 0;
3623         if (UA_entry->global_UA)
3624                 TRACE_MGMT_DBG("Queuing global UA %p", UA_entry);
3625
3626         if (sense_len > (int)sizeof(UA_entry->UA_sense_buffer))
3627                 sense_len = sizeof(UA_entry->UA_sense_buffer);
3628         memcpy(UA_entry->UA_sense_buffer, sense, sense_len);
3629
3630         set_bit(SCST_TGT_DEV_UA_PENDING, &tgt_dev->tgt_dev_flags);
3631
3632         TRACE_MGMT_DBG("Adding new UA to tgt_dev %p", tgt_dev);
3633
3634         if (flags & SCST_SET_UA_FLAG_AT_HEAD)
3635                 list_add(&UA_entry->UA_list_entry, &tgt_dev->UA_list);
3636         else
3637                 list_add_tail(&UA_entry->UA_list_entry, &tgt_dev->UA_list);
3638
3639 out:
3640         TRACE_EXIT();
3641         return;
3642 }
3643
3644 /* tgt_dev_lock supposed to be held and BH off */
3645 static void __scst_check_set_UA(struct scst_tgt_dev *tgt_dev,
3646         const uint8_t *sense, int sense_len, int flags)
3647 {
3648         int skip_UA = 0;
3649         struct scst_tgt_dev_UA *UA_entry_tmp;
3650         int len = min((int)sizeof(UA_entry_tmp->UA_sense_buffer), sense_len);
3651
3652         TRACE_ENTRY();
3653
3654         list_for_each_entry(UA_entry_tmp, &tgt_dev->UA_list,
3655                             UA_list_entry) {
3656                 if (memcmp(sense, UA_entry_tmp->UA_sense_buffer, len) == 0) {
3657                         TRACE_MGMT_DBG("%s", "UA already exists");
3658                         skip_UA = 1;
3659                         break;
3660                 }
3661         }
3662
3663         if (skip_UA == 0)
3664                 scst_alloc_set_UA(tgt_dev, sense, len, flags);
3665
3666         TRACE_EXIT();
3667         return;
3668 }
3669
3670 void scst_check_set_UA(struct scst_tgt_dev *tgt_dev,
3671         const uint8_t *sense, int sense_len, int flags)
3672 {
3673         TRACE_ENTRY();
3674
3675         spin_lock_bh(&tgt_dev->tgt_dev_lock);
3676         __scst_check_set_UA(tgt_dev, sense, sense_len, flags);
3677         spin_unlock_bh(&tgt_dev->tgt_dev_lock);
3678
3679         TRACE_EXIT();
3680         return;
3681 }
3682
3683 /* Called under dev_lock and BH off */
3684 void scst_dev_check_set_local_UA(struct scst_device *dev,
3685         struct scst_cmd *exclude, const uint8_t *sense, int sense_len)
3686 {
3687         struct scst_tgt_dev *tgt_dev, *exclude_tgt_dev = NULL;
3688
3689         TRACE_ENTRY();
3690
3691         if (exclude != NULL)
3692                 exclude_tgt_dev = exclude->tgt_dev;
3693
3694         list_for_each_entry(tgt_dev, &dev->dev_tgt_dev_list,
3695                         dev_tgt_dev_list_entry) {
3696                 if (tgt_dev != exclude_tgt_dev)
3697                         scst_check_set_UA(tgt_dev, sense, sense_len, 0);
3698         }
3699
3700         TRACE_EXIT();
3701         return;
3702 }
3703
3704 /* Called under dev_lock and BH off */
3705 void __scst_dev_check_set_UA(struct scst_device *dev,
3706         struct scst_cmd *exclude, const uint8_t *sense, int sense_len)
3707 {
3708         TRACE_ENTRY();
3709
3710         TRACE(TRACE_MGMT_MINOR, "Processing UA dev %p", dev);
3711
3712         /* Check for reset UA */
3713         if (scst_analyze_sense(sense, sense_len, SCST_SENSE_ASC_VALID,
3714                                 0, SCST_SENSE_ASC_UA_RESET, 0))
3715                 scst_process_reset(dev,
3716                                    (exclude != NULL) ? exclude->sess : NULL,
3717                                    exclude, NULL, false);
3718
3719         scst_dev_check_set_local_UA(dev, exclude, sense, sense_len);
3720
3721         TRACE_EXIT();
3722         return;
3723 }
3724
3725 /* Called under tgt_dev_lock or when tgt_dev is unused */
3726 static void scst_free_all_UA(struct scst_tgt_dev *tgt_dev)
3727 {
3728         struct scst_tgt_dev_UA *UA_entry, *t;
3729
3730         TRACE_ENTRY();
3731
3732         list_for_each_entry_safe(UA_entry, t,
3733                                  &tgt_dev->UA_list, UA_list_entry) {
3734                 TRACE_MGMT_DBG("Clearing UA for tgt_dev lun %lld",
3735                                (long long unsigned int)tgt_dev->lun);
3736                 list_del(&UA_entry->UA_list_entry);
3737                 mempool_free(UA_entry, scst_ua_mempool);
3738         }
3739         INIT_LIST_HEAD(&tgt_dev->UA_list);
3740         clear_bit(SCST_TGT_DEV_UA_PENDING, &tgt_dev->tgt_dev_flags);
3741
3742         TRACE_EXIT();
3743         return;
3744 }
3745
3746 /* No locks */
3747 struct scst_cmd *__scst_check_deferred_commands(struct scst_tgt_dev *tgt_dev)
3748 {
3749         struct scst_cmd *res = NULL, *cmd, *t;
3750         typeof(tgt_dev->expected_sn) expected_sn = tgt_dev->expected_sn;