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