e6ccc27ef829acf0cf01c65b43341d5f8986cfb3
[mirror/scst/.git] / scst / src / scst_targ.c
1 /*
2  *  scst_targ.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/smp_lock.h>
27 #include <linux/unistd.h>
28 #include <linux/string.h>
29 #include <linux/kthread.h>
30 #include <linux/delay.h>
31
32 #include "scst.h"
33 #include "scst_priv.h"
34
35 static void scst_cmd_set_sn(struct scst_cmd *cmd);
36 static int __scst_init_cmd(struct scst_cmd *cmd);
37 static void scst_finish_cmd_mgmt(struct scst_cmd *cmd);
38 static struct scst_cmd *__scst_find_cmd_by_tag(struct scst_session *sess,
39         uint64_t tag);
40 static void scst_proccess_redirect_cmd(struct scst_cmd *cmd,
41         enum scst_exec_context context, int check_retries);
42
43 static inline void scst_schedule_tasklet(struct scst_cmd *cmd)
44 {
45         struct scst_tasklet *t = &scst_tasklets[smp_processor_id()];
46         unsigned long flags;
47
48         spin_lock_irqsave(&t->tasklet_lock, flags);
49         TRACE_DBG("Adding cmd %p to tasklet %d cmd list", cmd,
50                 smp_processor_id());
51         list_add_tail(&cmd->cmd_list_entry, &t->tasklet_cmd_list);
52         spin_unlock_irqrestore(&t->tasklet_lock, flags);
53
54         tasklet_schedule(&t->tasklet);
55 }
56
57 /*
58  * Must not be called in parallel with scst_unregister_session() for the
59  * same sess
60  */
61 struct scst_cmd *scst_rx_cmd(struct scst_session *sess,
62                              const uint8_t *lun, int lun_len,
63                              const uint8_t *cdb, int cdb_len, int atomic)
64 {
65         struct scst_cmd *cmd;
66
67         TRACE_ENTRY();
68
69 #ifdef CONFIG_SCST_EXTRACHECKS
70         if (unlikely(sess->shut_phase != SCST_SESS_SPH_READY)) {
71                 PRINT_CRIT_ERROR("%s",
72                         "New cmd while shutting down the session");
73                 sBUG();
74         }
75 #endif
76
77         cmd = scst_alloc_cmd(atomic ? GFP_ATOMIC : GFP_KERNEL);
78         if (cmd == NULL)
79                 goto out;
80
81         cmd->sess = sess;
82         cmd->tgt = sess->tgt;
83         cmd->tgtt = sess->tgt->tgtt;
84
85         /*
86          * For both wrong lun and CDB defer the error reporting for
87          * scst_cmd_init_done()
88          */
89
90         cmd->lun = scst_unpack_lun(lun, lun_len);
91
92         if (cdb_len <= SCST_MAX_CDB_SIZE) {
93                 memcpy(cmd->cdb, cdb, cdb_len);
94                 cmd->cdb_len = cdb_len;
95         }
96
97         TRACE_DBG("cmd %p, sess %p", cmd, sess);
98         scst_sess_get(sess);
99
100 out:
101         TRACE_EXIT();
102         return cmd;
103 }
104 EXPORT_SYMBOL(scst_rx_cmd);
105
106 /*
107  * No locks, but might be on IRQ. Returns 0 on success, <0 if processing of
108  * this command should be stopped.
109  */
110 static int scst_init_cmd(struct scst_cmd *cmd, enum scst_exec_context *context)
111 {
112         int rc, res = 0;
113
114         TRACE_ENTRY();
115
116         /* See the comment in scst_do_job_init() */
117         if (unlikely(!list_empty(&scst_init_cmd_list))) {
118                 TRACE_MGMT_DBG("%s", "init cmd list busy");
119                 goto out_redirect;
120         }
121         /*
122          * Memory barrier isn't necessary here, because CPU appears to
123          * be self-consistent and we don't care about the race, described
124          * in comment in scst_do_job_init().
125          */
126
127         rc = __scst_init_cmd(cmd);
128         if (unlikely(rc > 0))
129                 goto out_redirect;
130         else if (unlikely(rc != 0)) {
131                 res = 1;
132                 goto out;
133         }
134
135         /* Small context optimization */
136         if (((*context == SCST_CONTEXT_TASKLET) ||
137              (*context == SCST_CONTEXT_DIRECT_ATOMIC) ||
138              ((*context == SCST_CONTEXT_SAME) && scst_cmd_atomic(cmd))) &&
139               scst_cmd_is_expected_set(cmd)) {
140                 if (cmd->expected_data_direction & SCST_DATA_WRITE) {
141                         if (!test_bit(SCST_TGT_DEV_AFTER_INIT_WR_ATOMIC,
142                                         &cmd->tgt_dev->tgt_dev_flags))
143                                 *context = SCST_CONTEXT_THREAD;
144                 } else {
145                         if (!test_bit(SCST_TGT_DEV_AFTER_INIT_OTH_ATOMIC,
146                                         &cmd->tgt_dev->tgt_dev_flags))
147                                 *context = SCST_CONTEXT_THREAD;
148                 }
149         }
150
151 out:
152         TRACE_EXIT_RES(res);
153         return res;
154
155 out_redirect:
156         if (cmd->preprocessing_only) {
157                 /*
158                  * Poor man solution for single threaded targets, where
159                  * blocking receiver at least sometimes means blocking all.
160                  */
161                 sBUG_ON(*context != SCST_CONTEXT_DIRECT);
162                 scst_set_busy(cmd);
163                 scst_set_cmd_abnormal_done_state(cmd);
164                 res = 1;
165                 /* Keep initiator away from too many BUSY commands */
166                 msleep(50);
167         } else {
168                 unsigned long flags;
169                 spin_lock_irqsave(&scst_init_lock, flags);
170                 TRACE_MGMT_DBG("Adding cmd %p to init cmd list (scst_cmd_count "
171                         "%d)", cmd, atomic_read(&scst_cmd_count));
172                 list_add_tail(&cmd->cmd_list_entry, &scst_init_cmd_list);
173                 if (test_bit(SCST_CMD_ABORTED, &cmd->cmd_flags))
174                         scst_init_poll_cnt++;
175                 spin_unlock_irqrestore(&scst_init_lock, flags);
176                 wake_up(&scst_init_cmd_list_waitQ);
177                 res = -1;
178         }
179         goto out;
180 }
181
182 #ifdef CONFIG_SCST_MEASURE_LATENCY
183 static inline uint64_t scst_sec_to_nsec(time_t sec)
184 {
185         return (uint64_t)sec * 1000000000;
186 }
187 #endif
188
189 void scst_cmd_init_done(struct scst_cmd *cmd,
190         enum scst_exec_context pref_context)
191 {
192         unsigned long flags;
193         struct scst_session *sess = cmd->sess;
194         int rc;
195
196         TRACE_ENTRY();
197
198 #ifdef CONFIG_SCST_MEASURE_LATENCY
199         {
200                 struct timespec ts;
201                 getnstimeofday(&ts);
202                 cmd->start = scst_sec_to_nsec(ts.tv_sec) + ts.tv_nsec;
203                 TRACE_DBG("cmd %p (sess %p): start %lld (tv_sec %ld, "
204                         "tv_nsec %ld)", cmd, sess, cmd->start, ts.tv_sec,
205                         ts.tv_nsec);
206         }
207 #endif
208
209         TRACE_DBG("Preferred context: %d (cmd %p)", pref_context, cmd);
210         TRACE(TRACE_SCSI, "tag=%llu, lun=%lld, CDB len=%d, queue_type=%x "
211                 "(cmd %p)", (long long unsigned int)cmd->tag,
212                 (long long unsigned int)cmd->lun, cmd->cdb_len,
213                 cmd->queue_type, cmd);
214         PRINT_BUFF_FLAG(TRACE_SCSI|TRACE_RCV_BOT, "Recieving CDB",
215                 cmd->cdb, cmd->cdb_len);
216
217 #ifdef CONFIG_SCST_EXTRACHECKS
218         if (unlikely((in_irq() || irqs_disabled())) &&
219             ((pref_context == SCST_CONTEXT_DIRECT) ||
220              (pref_context == SCST_CONTEXT_DIRECT_ATOMIC))) {
221                 PRINT_ERROR("Wrong context %d in IRQ from target %s, use "
222                         "SCST_CONTEXT_THREAD instead\n", pref_context,
223                         cmd->tgtt->name);
224                 pref_context = SCST_CONTEXT_THREAD;
225         }
226 #endif
227
228         atomic_inc(&sess->sess_cmd_count);
229
230         spin_lock_irqsave(&sess->sess_list_lock, flags);
231
232         if (unlikely(sess->init_phase != SCST_SESS_IPH_READY)) {
233                 /*
234                  * We have to always keep command in the search list from the
235                  * very beginning, because otherwise it can be missed during
236                  * TM processing. This check is needed because there might be
237                  * old, i.e. deferred, commands and new, i.e. just coming, ones.
238                  */
239                 if (cmd->search_cmd_list_entry.next == NULL)
240                         list_add_tail(&cmd->search_cmd_list_entry,
241                                 &sess->search_cmd_list);
242                 switch (sess->init_phase) {
243                 case SCST_SESS_IPH_SUCCESS:
244                         break;
245                 case SCST_SESS_IPH_INITING:
246                         TRACE_DBG("Adding cmd %p to init deferred cmd list",
247                                   cmd);
248                         list_add_tail(&cmd->cmd_list_entry,
249                                 &sess->init_deferred_cmd_list);
250                         spin_unlock_irqrestore(&sess->sess_list_lock, flags);
251                         goto out;
252                 case SCST_SESS_IPH_FAILED:
253                         spin_unlock_irqrestore(&sess->sess_list_lock, flags);
254                         scst_set_busy(cmd);
255                         scst_set_cmd_abnormal_done_state(cmd);
256                         goto active;
257                 default:
258                         sBUG();
259                 }
260         } else
261                 list_add_tail(&cmd->search_cmd_list_entry,
262                               &sess->search_cmd_list);
263
264         spin_unlock_irqrestore(&sess->sess_list_lock, flags);
265
266         if (unlikely(cmd->lun == NO_SUCH_LUN)) {
267                 PRINT_ERROR("Wrong LUN %d, finishing cmd", -1);
268                 scst_set_cmd_error(cmd,
269                            SCST_LOAD_SENSE(scst_sense_lun_not_supported));
270                 scst_set_cmd_abnormal_done_state(cmd);
271                 goto active;
272         }
273
274         if (unlikely(cmd->cdb_len == 0)) {
275                 PRINT_ERROR("%s", "Wrong CDB len, finishing cmd");
276                 scst_set_cmd_error(cmd,
277                            SCST_LOAD_SENSE(scst_sense_invalid_opcode));
278                 scst_set_cmd_abnormal_done_state(cmd);
279                 goto active;
280         }
281
282         if (unlikely(cmd->queue_type >= SCST_CMD_QUEUE_ACA)) {
283                 PRINT_ERROR("Unsupported queue type %d", cmd->queue_type);
284                 scst_set_cmd_error(cmd,
285                         SCST_LOAD_SENSE(scst_sense_invalid_message));
286                 scst_set_cmd_abnormal_done_state(cmd);
287                 goto active;
288         }
289
290         /*
291          * Cmd must be inited here to preserve the order. In case if cmd
292          * already preliminary completed by target driver we need to init
293          * cmd anyway to find out in which format we should return sense.
294          */
295         cmd->state = SCST_CMD_STATE_INIT;
296         rc = scst_init_cmd(cmd, &pref_context);
297         if (unlikely(rc < 0))
298                 goto out;
299         else if (unlikely(cmd->status == SAM_STAT_CHECK_CONDITION)) {
300                 if (rc == 0) {
301                         /* Target driver preliminary completed cmd */
302                         scst_set_cmd_abnormal_done_state(cmd);
303                 }
304         }
305
306 active:
307         /* Here cmd must not be in any cmd list, no locks */
308         switch (pref_context) {
309         case SCST_CONTEXT_TASKLET:
310                 scst_schedule_tasklet(cmd);
311                 break;
312
313         case SCST_CONTEXT_DIRECT:
314                 scst_process_active_cmd(cmd, false);
315                 /* For *NEED_THREAD wake_up() is already done */
316                 break;
317
318         case SCST_CONTEXT_DIRECT_ATOMIC:
319                 scst_process_active_cmd(cmd, true);
320                 /* For *NEED_THREAD wake_up() is already done */
321                 break;
322
323         default:
324                 PRINT_ERROR("Context %x is undefined, using the thread one",
325                         pref_context);
326                 /* go through */
327         case SCST_CONTEXT_THREAD:
328                 spin_lock_irqsave(&cmd->cmd_lists->cmd_list_lock, flags);
329                 TRACE_DBG("Adding cmd %p to active cmd list", cmd);
330                 if (unlikely(cmd->queue_type == SCST_CMD_QUEUE_HEAD_OF_QUEUE))
331                         list_add(&cmd->cmd_list_entry,
332                                 &cmd->cmd_lists->active_cmd_list);
333                 else
334                         list_add_tail(&cmd->cmd_list_entry,
335                                 &cmd->cmd_lists->active_cmd_list);
336                 wake_up(&cmd->cmd_lists->cmd_list_waitQ);
337                 spin_unlock_irqrestore(&cmd->cmd_lists->cmd_list_lock, flags);
338                 break;
339         }
340
341 out:
342         TRACE_EXIT();
343         return;
344 }
345 EXPORT_SYMBOL(scst_cmd_init_done);
346
347 static int scst_pre_parse(struct scst_cmd *cmd)
348 {
349         int res = SCST_CMD_STATE_RES_CONT_SAME;
350         struct scst_device *dev = cmd->dev;
351         int rc;
352
353         TRACE_ENTRY();
354
355         cmd->inc_expected_sn_on_done = dev->handler->exec_sync ||
356              (!dev->has_own_order_mgmt &&
357               (dev->queue_alg == SCST_CONTR_MODE_QUEUE_ALG_RESTRICTED_REORDER ||
358                cmd->queue_type == SCST_CMD_QUEUE_ORDERED));
359
360         /*
361          * Expected transfer data supplied by the SCSI transport via the
362          * target driver are untrusted, so we prefer to fetch them from CDB.
363          * Additionally, not all transports support supplying the expected
364          * transfer data.
365          */
366
367         rc = scst_get_cdb_info(cmd);
368         if (unlikely(rc != 0)) {
369                 if (rc > 0) {
370                         PRINT_BUFFER("Failed CDB", cmd->cdb, cmd->cdb_len);
371                         goto out_xmit;
372                 }
373                 PRINT_ERROR("Unknown opcode 0x%02x for %s. "
374                         "Should you update scst_scsi_op_table?",
375                         cmd->cdb[0], dev->handler->name);
376                 PRINT_BUFFER("Failed CDB", cmd->cdb, cmd->cdb_len);
377 #ifdef CONFIG_SCST_USE_EXPECTED_VALUES
378                 if (scst_cmd_is_expected_set(cmd)) {
379                         TRACE(TRACE_SCSI, "Using initiator supplied values: "
380                                 "direction %d, transfer_len %d",
381                                 cmd->expected_data_direction,
382                                 cmd->expected_transfer_len);
383                         cmd->data_direction = cmd->expected_data_direction;
384
385                         cmd->bufflen = cmd->expected_transfer_len;
386                         /* Restore (possibly) lost CDB length */
387                         cmd->cdb_len = scst_get_cdb_len(cmd->cdb);
388                         if (cmd->cdb_len == -1) {
389                                 PRINT_ERROR("Unable to get CDB length for "
390                                         "opcode 0x%02x. Returning INVALID "
391                                         "OPCODE", cmd->cdb[0]);
392                                 scst_set_cmd_error(cmd,
393                                    SCST_LOAD_SENSE(scst_sense_invalid_opcode));
394                                 goto out_xmit;
395                         }
396                 } else {
397                         PRINT_ERROR("Unknown opcode 0x%02x for %s and "
398                              "target %s not supplied expected values",
399                              cmd->cdb[0], dev->handler->name, cmd->tgtt->name);
400                         scst_set_cmd_error(cmd,
401                                    SCST_LOAD_SENSE(scst_sense_invalid_opcode));
402                         goto out_xmit;
403                 }
404 #else
405                 scst_set_cmd_error(cmd,
406                            SCST_LOAD_SENSE(scst_sense_invalid_opcode));
407                 goto out_xmit;
408 #endif
409         } else {
410                 TRACE(TRACE_SCSI, "op_name <%s> (cmd %p), direction=%d "
411                         "(expected %d, set %s), transfer_len=%d (expected "
412                         "len %d), flags=%d", cmd->op_name, cmd,
413                         cmd->data_direction, cmd->expected_data_direction,
414                         scst_cmd_is_expected_set(cmd) ? "yes" : "no",
415                         cmd->bufflen, cmd->expected_transfer_len,
416                         cmd->op_flags);
417
418                 if (unlikely((cmd->op_flags & SCST_UNKNOWN_LENGTH) != 0)) {
419                         if (scst_cmd_is_expected_set(cmd)) {
420                                 /*
421                                  * Command data length can't be easily
422                                  * determined from the CDB. ToDo, all such
423                                  * commands processing should be fixed. Until
424                                  * it's done, get the length from the supplied
425                                  * expected value, but limit it to some
426                                  * reasonable value (15MB).
427                                  */
428                                 cmd->bufflen = min(cmd->expected_transfer_len,
429                                                         15*1024*1024);
430                                 cmd->op_flags &= ~SCST_UNKNOWN_LENGTH;
431                         } else
432                                 cmd->bufflen = 0;
433                 }
434         }
435
436         if (unlikely(cmd->cdb[cmd->cdb_len - 1] & CONTROL_BYTE_NACA_BIT)) {
437                 PRINT_ERROR("NACA bit in control byte CDB is not supported "
438                             "(opcode 0x%02x)", cmd->cdb[0]);
439                 scst_set_cmd_error(cmd,
440                         SCST_LOAD_SENSE(scst_sense_invalid_field_in_cdb));
441                 goto out_xmit;
442         }
443
444         if (unlikely(cmd->cdb[cmd->cdb_len - 1] & CONTROL_BYTE_LINK_BIT)) {
445                 PRINT_ERROR("Linked commands are not supported "
446                             "(opcode 0x%02x)", cmd->cdb[0]);
447                 scst_set_cmd_error(cmd,
448                         SCST_LOAD_SENSE(scst_sense_invalid_field_in_cdb));
449                 goto out_xmit;
450         }
451
452         cmd->state = SCST_CMD_STATE_DEV_PARSE;
453
454 out:
455         TRACE_EXIT_RES(res);
456         return res;
457
458 out_xmit:
459         scst_set_cmd_abnormal_done_state(cmd);
460         res = SCST_CMD_STATE_RES_CONT_SAME;
461         goto out;
462 }
463
464 #ifndef CONFIG_SCST_USE_EXPECTED_VALUES
465 static bool scst_is_allowed_to_mismatch_cmd(struct scst_cmd *cmd)
466 {
467         bool res = false;
468
469         switch (cmd->cdb[0]) {
470         case TEST_UNIT_READY:
471                 /* Crazy VMware people sometimes do TUR with READ direction */
472                 res = true;
473                 break;
474         case VERIFY:
475         case VERIFY_6:
476         case VERIFY_12:
477         case VERIFY_16:
478                 /* VERIFY commands with BYTCHK unset shouldn't fail here */
479                 if ((cmd->op_flags & SCST_VERIFY_BYTCHK_MISMATCH_ALLOWED) &&
480                     (cmd->cdb[1] & BYTCHK) == 0)
481                         res = true;
482                 break;
483         }
484
485         return res;
486 }
487 #endif
488
489 static int scst_parse_cmd(struct scst_cmd *cmd)
490 {
491         int res = SCST_CMD_STATE_RES_CONT_SAME;
492         int state;
493         struct scst_device *dev = cmd->dev;
494         int orig_bufflen = cmd->bufflen;
495
496         TRACE_ENTRY();
497
498         if (likely(!scst_is_cmd_local(cmd))) {
499                 if (unlikely(!dev->handler->parse_atomic &&
500                              scst_cmd_atomic(cmd))) {
501                         /*
502                          * It shouldn't be because of SCST_TGT_DEV_AFTER_*
503                          * optimization.
504                          */
505                         TRACE_DBG("Dev handler %s parse() needs thread "
506                                 "context, rescheduling", dev->handler->name);
507                         res = SCST_CMD_STATE_RES_NEED_THREAD;
508                         goto out;
509                 }
510
511                 TRACE_DBG("Calling dev handler %s parse(%p)",
512                       dev->handler->name, cmd);
513                 TRACE_BUFF_FLAG(TRACE_SND_BOT, "Parsing: ",
514                                 cmd->cdb, cmd->cdb_len);
515                 state = dev->handler->parse(cmd);
516                 /* Caution: cmd can be already dead here */
517                 TRACE_DBG("Dev handler %s parse() returned %d",
518                         dev->handler->name, state);
519
520                 switch (state) {
521                 case SCST_CMD_STATE_NEED_THREAD_CTX:
522                         TRACE_DBG("Dev handler %s parse() requested thread "
523                               "context, rescheduling", dev->handler->name);
524                         res = SCST_CMD_STATE_RES_NEED_THREAD;
525                         goto out;
526
527                 case SCST_CMD_STATE_STOP:
528                         TRACE_DBG("Dev handler %s parse() requested stop "
529                                 "processing", dev->handler->name);
530                         res = SCST_CMD_STATE_RES_CONT_NEXT;
531                         goto out;
532                 }
533
534                 if (state == SCST_CMD_STATE_DEFAULT)
535                         state = SCST_CMD_STATE_PREPARE_SPACE;
536         } else
537                 state = SCST_CMD_STATE_PREPARE_SPACE;
538
539         if (cmd->data_len == -1)
540                 cmd->data_len = cmd->bufflen;
541
542         if (cmd->bufflen == 0) {
543                 /*
544                  * According to SPC bufflen 0 for data transfer commands isn't
545                  * an error, so we need to fix the transfer direction.
546                  */
547                 cmd->data_direction = SCST_DATA_NONE;
548         }
549
550         if (cmd->dh_data_buf_alloced &&
551             unlikely((orig_bufflen > cmd->bufflen))) {
552                 PRINT_ERROR("Dev handler supplied data buffer (size %d), "
553                         "is less, than required (size %d)", cmd->bufflen,
554                         orig_bufflen);
555                 PRINT_BUFFER("Failed CDB", cmd->cdb, cmd->cdb_len);
556                 goto out_error;
557         }
558
559         if (unlikely(state == SCST_CMD_STATE_PRE_XMIT_RESP))
560                 goto set_res;
561
562         if (unlikely((cmd->bufflen == 0) &&
563                      (cmd->op_flags & SCST_UNKNOWN_LENGTH))) {
564                 PRINT_ERROR("Unknown data transfer length for opcode 0x%x "
565                         "(handler %s, target %s)", cmd->cdb[0],
566                         dev->handler->name, cmd->tgtt->name);
567                 PRINT_BUFFER("Failed CDB", cmd->cdb, cmd->cdb_len);
568                 goto out_error;
569         }
570
571 #ifdef CONFIG_SCST_EXTRACHECKS
572         if ((cmd->bufflen != 0) &&
573             ((cmd->data_direction == SCST_DATA_NONE) ||
574              ((cmd->sg == NULL) && (state > SCST_CMD_STATE_PREPARE_SPACE)))) {
575                 PRINT_ERROR("Dev handler %s parse() returned "
576                         "invalid cmd data_direction %d, bufflen %d, state %d "
577                         "or sg %p (opcode 0x%x)", dev->handler->name,
578                         cmd->data_direction, cmd->bufflen, state, cmd->sg,
579                         cmd->cdb[0]);
580                 PRINT_BUFFER("Failed CDB", cmd->cdb, cmd->cdb_len);
581                 goto out_error;
582         }
583 #endif
584
585         if (scst_cmd_is_expected_set(cmd)) {
586 #ifdef CONFIG_SCST_USE_EXPECTED_VALUES
587 #       ifdef CONFIG_SCST_EXTRACHECKS
588                 if ((cmd->data_direction != cmd->expected_data_direction) ||
589                     (cmd->bufflen != cmd->expected_transfer_len)) {
590                         PRINT_WARNING("Expected values don't match decoded "
591                                 "ones: data_direction %d, "
592                                 "expected_data_direction %d, "
593                                 "bufflen %d, expected_transfer_len %d",
594                                 cmd->data_direction,
595                                 cmd->expected_data_direction,
596                                 cmd->bufflen, cmd->expected_transfer_len);
597                         PRINT_BUFFER("Suspicious CDB", cmd->cdb, cmd->cdb_len);
598                 }
599 #       endif
600                 cmd->data_direction = cmd->expected_data_direction;
601                 cmd->bufflen = cmd->expected_transfer_len;
602 #else
603                 if (unlikely(cmd->data_direction !=
604                                 cmd->expected_data_direction)) {
605                         if (((cmd->expected_data_direction != SCST_DATA_NONE) ||
606                              (cmd->bufflen != 0)) &&
607                             !scst_is_allowed_to_mismatch_cmd(cmd)) {
608                                 PRINT_ERROR("Expected data direction %d for "
609                                         "opcode 0x%02x (handler %s, target %s) "
610                                         "doesn't match "
611                                         "decoded value %d",
612                                         cmd->expected_data_direction,
613                                         cmd->cdb[0], dev->handler->name,
614                                         cmd->tgtt->name, cmd->data_direction);
615                                 PRINT_BUFFER("Failed CDB",
616                                         cmd->cdb, cmd->cdb_len);
617                                 scst_set_cmd_error(cmd,
618                                    SCST_LOAD_SENSE(scst_sense_invalid_message));
619                                 goto out_dev_done;
620                         }
621                 }
622                 if (unlikely(cmd->bufflen != cmd->expected_transfer_len)) {
623                         TRACE(TRACE_MGMT_MINOR, "Warning: expected "
624                                 "transfer length %d for opcode 0x%02x "
625                                 "(handler %s, target %s) doesn't match "
626                                 "decoded value %d. Faulty initiator "
627                                 "(e.g. VMware is known to be such) or "
628                                 "scst_scsi_op_table should be updated?",
629                                 cmd->expected_transfer_len, cmd->cdb[0],
630                                 dev->handler->name, cmd->tgtt->name,
631                                 cmd->bufflen);
632                         PRINT_BUFF_FLAG(TRACE_MGMT_MINOR, "Suspicious CDB",
633                                 cmd->cdb, cmd->cdb_len);
634                 }
635 #endif
636         }
637
638         if (unlikely(cmd->data_direction == SCST_DATA_UNKNOWN)) {
639                 PRINT_ERROR("Unknown data direction. Opcode 0x%x, handler %s, "
640                         "target %s", cmd->cdb[0], dev->handler->name,
641                         cmd->tgtt->name);
642                 PRINT_BUFFER("Failed CDB", cmd->cdb, cmd->cdb_len);
643                 goto out_error;
644         }
645
646 set_res:
647         switch (state) {
648         case SCST_CMD_STATE_PREPARE_SPACE:
649         case SCST_CMD_STATE_PRE_PARSE:
650         case SCST_CMD_STATE_DEV_PARSE:
651         case SCST_CMD_STATE_RDY_TO_XFER:
652         case SCST_CMD_STATE_TGT_PRE_EXEC:
653         case SCST_CMD_STATE_SEND_FOR_EXEC:
654         case SCST_CMD_STATE_LOCAL_EXEC:
655         case SCST_CMD_STATE_REAL_EXEC:
656         case SCST_CMD_STATE_PRE_DEV_DONE:
657         case SCST_CMD_STATE_DEV_DONE:
658         case SCST_CMD_STATE_PRE_XMIT_RESP:
659         case SCST_CMD_STATE_XMIT_RESP:
660         case SCST_CMD_STATE_FINISHED:
661         case SCST_CMD_STATE_FINISHED_INTERNAL:
662                 cmd->state = state;
663                 res = SCST_CMD_STATE_RES_CONT_SAME;
664                 break;
665
666         default:
667                 if (state >= 0) {
668                         PRINT_ERROR("Dev handler %s parse() returned "
669                              "invalid cmd state %d (opcode %d)",
670                              dev->handler->name, state, cmd->cdb[0]);
671                 } else {
672                         PRINT_ERROR("Dev handler %s parse() returned "
673                                 "error %d (opcode %d)", dev->handler->name,
674                                 state, cmd->cdb[0]);
675                 }
676                 goto out_error;
677         }
678
679         if (cmd->resp_data_len == -1) {
680                 if (cmd->data_direction & SCST_DATA_READ)
681                         cmd->resp_data_len = cmd->bufflen;
682                 else
683                          cmd->resp_data_len = 0;
684         }
685
686 out:
687         TRACE_EXIT_HRES(res);
688         return res;
689
690 out_error:
691         /* dev_done() will be called as part of the regular cmd's finish */
692         scst_set_cmd_error(cmd, SCST_LOAD_SENSE(scst_sense_hardw_error));
693
694 #ifndef CONFIG_SCST_USE_EXPECTED_VALUES
695 out_dev_done:
696 #endif
697         cmd->state = SCST_CMD_STATE_PRE_DEV_DONE;
698         res = SCST_CMD_STATE_RES_CONT_SAME;
699         goto out;
700 }
701
702 static int scst_prepare_space(struct scst_cmd *cmd)
703 {
704         int r = 0, res = SCST_CMD_STATE_RES_CONT_SAME;
705
706         TRACE_ENTRY();
707
708         if (cmd->data_direction == SCST_DATA_NONE)
709                 goto prep_done;
710
711         if (cmd->tgt_need_alloc_data_buf) {
712                 int orig_bufflen = cmd->bufflen;
713
714                 TRACE_MEM("Custom tgt data buf allocation requested (cmd %p)",
715                         cmd);
716
717                 r = cmd->tgtt->alloc_data_buf(cmd);
718                 if (r > 0)
719                         goto alloc;
720                 else if (r == 0) {
721                         if (unlikely(cmd->bufflen == 0)) {
722                                 /* See comment in scst_alloc_space() */
723                                 if (cmd->sg == NULL)
724                                         goto alloc;
725                         }
726
727                         cmd->tgt_data_buf_alloced = 1;
728
729                         if (unlikely(orig_bufflen < cmd->bufflen)) {
730                                 PRINT_ERROR("Target driver allocated data "
731                                         "buffer (size %d), is less, than "
732                                         "required (size %d)", orig_bufflen,
733                                         cmd->bufflen);
734                                 goto out_error;
735                         }
736                         TRACE_MEM("tgt_data_buf_alloced (cmd %p)", cmd);
737                 } else
738                         goto check;
739         }
740
741 alloc:
742         if (!cmd->tgt_data_buf_alloced && !cmd->dh_data_buf_alloced) {
743                 r = scst_alloc_space(cmd);
744         } else if (cmd->dh_data_buf_alloced && !cmd->tgt_data_buf_alloced) {
745                 TRACE_MEM("dh_data_buf_alloced set (cmd %p)", cmd);
746                 r = 0;
747         } else if (cmd->tgt_data_buf_alloced && !cmd->dh_data_buf_alloced) {
748                 TRACE_MEM("tgt_data_buf_alloced set (cmd %p)", cmd);
749                 cmd->sg = cmd->tgt_sg;
750                 cmd->sg_cnt = cmd->tgt_sg_cnt;
751                 cmd->in_sg = cmd->tgt_in_sg;
752                 cmd->in_sg_cnt = cmd->tgt_in_sg_cnt;
753                 r = 0;
754         } else {
755                 TRACE_MEM("Both *_data_buf_alloced set (cmd %p, sg %p, "
756                         "sg_cnt %d, tgt_sg %p, tgt_sg_cnt %d)", cmd, cmd->sg,
757                         cmd->sg_cnt, cmd->tgt_sg, cmd->tgt_sg_cnt);
758                 r = 0;
759         }
760
761 check:
762         if (r != 0) {
763                 if (scst_cmd_atomic(cmd)) {
764                         TRACE_MEM("%s", "Atomic memory allocation failed, "
765                               "rescheduling to the thread");
766                         res = SCST_CMD_STATE_RES_NEED_THREAD;
767                         goto out;
768                 } else
769                         goto out_no_space;
770         }
771
772 prep_done:
773         if (cmd->preprocessing_only) {
774                 cmd->preprocessing_only = 0;
775
776                 if (unlikely(test_bit(SCST_CMD_ABORTED, &cmd->cmd_flags))) {
777                         TRACE_MGMT_DBG("ABORTED set, returning ABORTED for "
778                                 "cmd %p", cmd);
779                         scst_set_cmd_abnormal_done_state(cmd);
780                         res = SCST_CMD_STATE_RES_CONT_SAME;
781                         goto out;
782                 }
783
784                 res = SCST_CMD_STATE_RES_CONT_NEXT;
785                 cmd->state = SCST_CMD_STATE_PREPROCESS_DONE;
786
787                 TRACE_DBG("Calling preprocessing_done(cmd %p)", cmd);
788                 cmd->tgtt->preprocessing_done(cmd);
789                 TRACE_DBG("%s", "preprocessing_done() returned");
790                 goto out;
791
792         }
793
794         if (cmd->data_direction & SCST_DATA_WRITE)
795                 cmd->state = SCST_CMD_STATE_RDY_TO_XFER;
796         else
797                 cmd->state = SCST_CMD_STATE_TGT_PRE_EXEC;
798
799 out:
800         TRACE_EXIT_HRES(res);
801         return res;
802
803 out_no_space:
804         TRACE(TRACE_OUT_OF_MEM, "Unable to allocate or build requested buffer "
805                 "(size %d), sending BUSY or QUEUE FULL status", cmd->bufflen);
806         scst_set_busy(cmd);
807         scst_set_cmd_abnormal_done_state(cmd);
808         res = SCST_CMD_STATE_RES_CONT_SAME;
809         goto out;
810
811 out_error:
812         scst_set_cmd_error(cmd, SCST_LOAD_SENSE(scst_sense_hardw_error));
813         scst_set_cmd_abnormal_done_state(cmd);
814         res = SCST_CMD_STATE_RES_CONT_SAME;
815         goto out;
816 }
817
818 void scst_restart_cmd(struct scst_cmd *cmd, int status,
819         enum scst_exec_context pref_context)
820 {
821         TRACE_ENTRY();
822
823         TRACE_DBG("Preferred context: %d", pref_context);
824         TRACE_DBG("tag=%llu, status=%#x",
825                   (long long unsigned int)scst_cmd_get_tag(cmd),
826                   status);
827
828 #ifdef CONFIG_SCST_EXTRACHECKS
829         if ((in_irq() || irqs_disabled()) &&
830             ((pref_context == SCST_CONTEXT_DIRECT) ||
831              (pref_context == SCST_CONTEXT_DIRECT_ATOMIC))) {
832                 PRINT_ERROR("Wrong context %d in IRQ from target %s, use "
833                         "SCST_CONTEXT_THREAD instead\n", pref_context,
834                         cmd->tgtt->name);
835                 pref_context = SCST_CONTEXT_THREAD;
836         }
837 #endif
838
839         switch (status) {
840         case SCST_PREPROCESS_STATUS_SUCCESS:
841                 if (cmd->data_direction & SCST_DATA_WRITE)
842                         cmd->state = SCST_CMD_STATE_RDY_TO_XFER;
843                 else
844                         cmd->state = SCST_CMD_STATE_TGT_PRE_EXEC;
845                 if (cmd->set_sn_on_restart_cmd)
846                         scst_cmd_set_sn(cmd);
847                 /* Small context optimization */
848                 if ((pref_context == SCST_CONTEXT_TASKLET) ||
849                     (pref_context == SCST_CONTEXT_DIRECT_ATOMIC) ||
850                     ((pref_context == SCST_CONTEXT_SAME) &&
851                      scst_cmd_atomic(cmd))) {
852                         if (cmd->data_direction & SCST_DATA_WRITE) {
853                                 if (!test_bit(SCST_TGT_DEV_AFTER_RESTART_WR_ATOMIC,
854                                                 &cmd->tgt_dev->tgt_dev_flags))
855                                         pref_context = SCST_CONTEXT_THREAD;
856                         } else {
857                                 if (!test_bit(SCST_TGT_DEV_AFTER_RESTART_OTH_ATOMIC,
858                                                 &cmd->tgt_dev->tgt_dev_flags))
859                                         pref_context = SCST_CONTEXT_THREAD;
860                         }
861                 }
862                 break;
863
864         case SCST_PREPROCESS_STATUS_ERROR_SENSE_SET:
865                 scst_set_cmd_abnormal_done_state(cmd);
866                 break;
867
868         case SCST_PREPROCESS_STATUS_ERROR_FATAL:
869                 set_bit(SCST_CMD_NO_RESP, &cmd->cmd_flags);
870                 /* go through */
871         case SCST_PREPROCESS_STATUS_ERROR:
872                 if (cmd->sense != NULL)
873                         scst_set_cmd_error(cmd,
874                                 SCST_LOAD_SENSE(scst_sense_hardw_error));
875                 scst_set_cmd_abnormal_done_state(cmd);
876                 break;
877
878         default:
879                 PRINT_ERROR("%s() received unknown status %x", __func__,
880                         status);
881                 scst_set_cmd_abnormal_done_state(cmd);
882                 break;
883         }
884
885         scst_proccess_redirect_cmd(cmd, pref_context, 1);
886
887         TRACE_EXIT();
888         return;
889 }
890 EXPORT_SYMBOL(scst_restart_cmd);
891
892 /* No locks */
893 static int scst_queue_retry_cmd(struct scst_cmd *cmd, int finished_cmds)
894 {
895         struct scst_tgt *tgt = cmd->sess->tgt;
896         int res = 0;
897         unsigned long flags;
898
899         TRACE_ENTRY();
900
901         spin_lock_irqsave(&tgt->tgt_lock, flags);
902         tgt->retry_cmds++;
903         /*
904          * Memory barrier is needed here, because we need the exact order
905          * between the read and write between retry_cmds and finished_cmds to
906          * not miss the case when a command finished while we queuing it for
907          * retry after the finished_cmds check.
908          */
909         smp_mb();
910         TRACE_RETRY("TGT QUEUE FULL: incrementing retry_cmds %d",
911               tgt->retry_cmds);
912         if (finished_cmds != atomic_read(&tgt->finished_cmds)) {
913                 /* At least one cmd finished, so try again */
914                 tgt->retry_cmds--;
915                 TRACE_RETRY("Some command(s) finished, direct retry "
916                       "(finished_cmds=%d, tgt->finished_cmds=%d, "
917                       "retry_cmds=%d)", finished_cmds,
918                       atomic_read(&tgt->finished_cmds), tgt->retry_cmds);
919                 res = -1;
920                 goto out_unlock_tgt;
921         }
922
923         TRACE_RETRY("Adding cmd %p to retry cmd list", cmd);
924         list_add_tail(&cmd->cmd_list_entry, &tgt->retry_cmd_list);
925
926         if (!tgt->retry_timer_active) {
927                 tgt->retry_timer.expires = jiffies + SCST_TGT_RETRY_TIMEOUT;
928                 add_timer(&tgt->retry_timer);
929                 tgt->retry_timer_active = 1;
930         }
931
932 out_unlock_tgt:
933         spin_unlock_irqrestore(&tgt->tgt_lock, flags);
934
935         TRACE_EXIT_RES(res);
936         return res;
937 }
938
939 static int scst_rdy_to_xfer(struct scst_cmd *cmd)
940 {
941         int res, rc;
942
943         TRACE_ENTRY();
944
945         if (unlikely(test_bit(SCST_CMD_ABORTED, &cmd->cmd_flags))) {
946                 TRACE_MGMT_DBG("ABORTED set, aborting cmd %p", cmd);
947                 goto out_dev_done;
948         }
949
950         if ((cmd->tgtt->rdy_to_xfer == NULL) || unlikely(cmd->internal)) {
951                 cmd->state = SCST_CMD_STATE_TGT_PRE_EXEC;
952                 res = SCST_CMD_STATE_RES_CONT_SAME;
953                 goto out;
954         }
955
956         if (unlikely(!cmd->tgtt->rdy_to_xfer_atomic && scst_cmd_atomic(cmd))) {
957                 /*
958                  * It shouldn't be because of SCST_TGT_DEV_AFTER_*
959                  * optimization.
960                  */
961                 TRACE_DBG("Target driver %s rdy_to_xfer() needs thread "
962                               "context, rescheduling", cmd->tgtt->name);
963                 res = SCST_CMD_STATE_RES_NEED_THREAD;
964                 goto out;
965         }
966
967         while (1) {
968                 int finished_cmds = atomic_read(&cmd->sess->tgt->finished_cmds);
969
970                 res = SCST_CMD_STATE_RES_CONT_NEXT;
971                 cmd->state = SCST_CMD_STATE_DATA_WAIT;
972
973                 TRACE_DBG("Calling rdy_to_xfer(%p)", cmd);
974 #ifdef CONFIG_SCST_DEBUG_RETRY
975                 if (((scst_random() % 100) == 75))
976                         rc = SCST_TGT_RES_QUEUE_FULL;
977                 else
978 #endif
979                         rc = cmd->tgtt->rdy_to_xfer(cmd);
980                 TRACE_DBG("rdy_to_xfer() returned %d", rc);
981
982                 if (likely(rc == SCST_TGT_RES_SUCCESS))
983                         goto out;
984
985                 /* Restore the previous state */
986                 cmd->state = SCST_CMD_STATE_RDY_TO_XFER;
987
988                 switch (rc) {
989                 case SCST_TGT_RES_QUEUE_FULL:
990                         if (scst_queue_retry_cmd(cmd, finished_cmds) == 0)
991                                 break;
992                         else
993                                 continue;
994
995                 case SCST_TGT_RES_NEED_THREAD_CTX:
996                         TRACE_DBG("Target driver %s "
997                               "rdy_to_xfer() requested thread "
998                               "context, rescheduling", cmd->tgtt->name);
999                         res = SCST_CMD_STATE_RES_NEED_THREAD;
1000                         break;
1001
1002                 default:
1003                         goto out_error_rc;
1004                 }
1005                 break;
1006         }
1007
1008 out:
1009         TRACE_EXIT_HRES(res);
1010         return res;
1011
1012 out_error_rc:
1013         if (rc == SCST_TGT_RES_FATAL_ERROR) {
1014                 PRINT_ERROR("Target driver %s rdy_to_xfer() returned "
1015                      "fatal error", cmd->tgtt->name);
1016         } else {
1017                 PRINT_ERROR("Target driver %s rdy_to_xfer() returned invalid "
1018                             "value %d", cmd->tgtt->name, rc);
1019         }
1020         scst_set_cmd_error(cmd, SCST_LOAD_SENSE(scst_sense_hardw_error));
1021
1022 out_dev_done:
1023         scst_set_cmd_abnormal_done_state(cmd);
1024         res = SCST_CMD_STATE_RES_CONT_SAME;
1025         goto out;
1026 }
1027
1028 /* No locks, but might be in IRQ */
1029 static void scst_proccess_redirect_cmd(struct scst_cmd *cmd,
1030         enum scst_exec_context context, int check_retries)
1031 {
1032         unsigned long flags;
1033
1034         TRACE_ENTRY();
1035
1036         TRACE_DBG("Context: %x", context);
1037
1038         if (context == SCST_CONTEXT_SAME)
1039                 context = scst_cmd_atomic(cmd) ? SCST_CONTEXT_DIRECT_ATOMIC :
1040                                                  SCST_CONTEXT_DIRECT;
1041
1042         switch (context) {
1043         case SCST_CONTEXT_DIRECT_ATOMIC:
1044                 scst_process_active_cmd(cmd, true);
1045                 break;
1046
1047         case SCST_CONTEXT_DIRECT:
1048                 if (check_retries)
1049                         scst_check_retries(cmd->tgt);
1050                 scst_process_active_cmd(cmd, false);
1051                 break;
1052
1053         default:
1054                 PRINT_ERROR("Context %x is unknown, using the thread one",
1055                             context);
1056                 /* go through */
1057         case SCST_CONTEXT_THREAD:
1058                 if (check_retries)
1059                         scst_check_retries(cmd->tgt);
1060                 spin_lock_irqsave(&cmd->cmd_lists->cmd_list_lock, flags);
1061                 TRACE_DBG("Adding cmd %p to active cmd list", cmd);
1062                 if (unlikely(cmd->queue_type == SCST_CMD_QUEUE_HEAD_OF_QUEUE))
1063                         list_add(&cmd->cmd_list_entry,
1064                                 &cmd->cmd_lists->active_cmd_list);
1065                 else
1066                         list_add_tail(&cmd->cmd_list_entry,
1067                                 &cmd->cmd_lists->active_cmd_list);
1068                 wake_up(&cmd->cmd_lists->cmd_list_waitQ);
1069                 spin_unlock_irqrestore(&cmd->cmd_lists->cmd_list_lock, flags);
1070                 break;
1071
1072         case SCST_CONTEXT_TASKLET:
1073                 if (check_retries)
1074                         scst_check_retries(cmd->tgt);
1075                 scst_schedule_tasklet(cmd);
1076                 break;
1077         }
1078
1079         TRACE_EXIT();
1080         return;
1081 }
1082
1083 void scst_rx_data(struct scst_cmd *cmd, int status,
1084         enum scst_exec_context pref_context)
1085 {
1086         TRACE_ENTRY();
1087
1088         TRACE_DBG("Preferred context: %d", pref_context);
1089         TRACE(TRACE_SCSI, "cmd %p, status %#x", cmd, status);
1090
1091 #ifdef CONFIG_SCST_EXTRACHECKS
1092         if ((in_irq() || irqs_disabled()) &&
1093             ((pref_context == SCST_CONTEXT_DIRECT) ||
1094              (pref_context == SCST_CONTEXT_DIRECT_ATOMIC))) {
1095                 PRINT_ERROR("Wrong context %d in IRQ from target %s, use "
1096                         "SCST_CONTEXT_THREAD instead\n", pref_context,
1097                         cmd->tgtt->name);
1098                 pref_context = SCST_CONTEXT_THREAD;
1099         }
1100 #endif
1101
1102         switch (status) {
1103         case SCST_RX_STATUS_SUCCESS:
1104 #if defined(CONFIG_SCST_DEBUG) || defined(CONFIG_SCST_TRACING)
1105                 if (trace_flag & TRACE_RCV_BOT) {
1106                         int i;
1107                         struct scatterlist *sg;
1108                         if (cmd->in_sg != NULL)
1109                                 sg = cmd->in_sg;
1110                         else if (cmd->tgt_in_sg != NULL)
1111                                 sg = cmd->tgt_in_sg;
1112                         else if (cmd->tgt_sg != NULL)
1113                                 sg = cmd->tgt_sg;
1114                         else
1115                                 sg = cmd->sg;
1116                         if (sg != NULL) {
1117                                 TRACE_RECV_BOT("RX data for cmd %p "
1118                                         "(sg_cnt %d, sg %p, sg[0].page %p)",
1119                                         cmd, cmd->tgt_sg_cnt, sg,
1120                                         (void *)sg_page(&sg[0]));
1121                                 for (i = 0; i < cmd->tgt_sg_cnt; ++i) {
1122                                         PRINT_BUFF_FLAG(TRACE_RCV_BOT, "RX sg",
1123                                                 sg_virt(&sg[i]), sg[i].length);
1124                                 }
1125                         }
1126                 }
1127 #endif
1128                 cmd->state = SCST_CMD_STATE_TGT_PRE_EXEC;
1129                 /* Small context optimization */
1130                 if ((pref_context == SCST_CONTEXT_TASKLET) ||
1131                     (pref_context == SCST_CONTEXT_DIRECT_ATOMIC) ||
1132                     ((pref_context == SCST_CONTEXT_SAME) &&
1133                      scst_cmd_atomic(cmd))) {
1134                         if (!test_bit(SCST_TGT_DEV_AFTER_RX_DATA_ATOMIC,
1135                                         &cmd->tgt_dev->tgt_dev_flags))
1136                                 pref_context = SCST_CONTEXT_THREAD;
1137                 }
1138                 break;
1139
1140         case SCST_RX_STATUS_ERROR_SENSE_SET:
1141                 scst_set_cmd_abnormal_done_state(cmd);
1142                 break;
1143
1144         case SCST_RX_STATUS_ERROR_FATAL:
1145                 set_bit(SCST_CMD_NO_RESP, &cmd->cmd_flags);
1146                 /* go through */
1147         case SCST_RX_STATUS_ERROR:
1148                 scst_set_cmd_error(cmd,
1149                            SCST_LOAD_SENSE(scst_sense_hardw_error));
1150                 scst_set_cmd_abnormal_done_state(cmd);
1151                 break;
1152
1153         default:
1154                 PRINT_ERROR("scst_rx_data() received unknown status %x",
1155                         status);
1156                 scst_set_cmd_abnormal_done_state(cmd);
1157                 break;
1158         }
1159
1160         scst_proccess_redirect_cmd(cmd, pref_context, 1);
1161
1162         TRACE_EXIT();
1163         return;
1164 }
1165 EXPORT_SYMBOL(scst_rx_data);
1166
1167 static int scst_tgt_pre_exec(struct scst_cmd *cmd)
1168 {
1169         int res = SCST_CMD_STATE_RES_CONT_SAME, rc;
1170
1171         TRACE_ENTRY();
1172
1173         cmd->state = SCST_CMD_STATE_SEND_FOR_EXEC;
1174
1175         if ((cmd->tgtt->pre_exec == NULL) || unlikely(cmd->internal))
1176                 goto out;
1177
1178         TRACE_DBG("Calling pre_exec(%p)", cmd);
1179         rc = cmd->tgtt->pre_exec(cmd);
1180         TRACE_DBG("pre_exec() returned %d", rc);
1181
1182         if (unlikely(rc != SCST_PREPROCESS_STATUS_SUCCESS)) {
1183                 switch (rc) {
1184                 case SCST_PREPROCESS_STATUS_ERROR_SENSE_SET:
1185                         scst_set_cmd_abnormal_done_state(cmd);
1186                         break;
1187                 case SCST_PREPROCESS_STATUS_ERROR_FATAL:
1188                         set_bit(SCST_CMD_NO_RESP, &cmd->cmd_flags);
1189                         /* go through */
1190                 case SCST_PREPROCESS_STATUS_ERROR:
1191                         scst_set_cmd_error(cmd,
1192                                    SCST_LOAD_SENSE(scst_sense_hardw_error));
1193                         scst_set_cmd_abnormal_done_state(cmd);
1194                         break;
1195                 case SCST_PREPROCESS_STATUS_NEED_THREAD:
1196                         TRACE_DBG("Target driver's %s pre_exec() requested "
1197                                 "thread context, rescheduling",
1198                                 cmd->tgtt->name);
1199                         res = SCST_CMD_STATE_RES_NEED_THREAD;
1200                         cmd->state = SCST_CMD_STATE_TGT_PRE_EXEC;
1201                         break;
1202                 default:
1203                         sBUG();
1204                         break;
1205                 }
1206         }
1207
1208 out:
1209         TRACE_EXIT_RES(res);
1210         return res;
1211 }
1212
1213 static void scst_do_cmd_done(struct scst_cmd *cmd, int result,
1214         const uint8_t *rq_sense, int rq_sense_len, int resid)
1215 {
1216         TRACE_ENTRY();
1217
1218 #ifdef CONFIG_SCST_MEASURE_LATENCY
1219         {
1220                 struct timespec ts;
1221                 getnstimeofday(&ts);
1222                 cmd->post_exec_start = scst_sec_to_nsec(ts.tv_sec) + ts.tv_nsec;
1223                 TRACE_DBG("cmd %p (sess %p): post_exec_start %lld (tv_sec %ld, "
1224                         "tv_nsec %ld)", cmd, cmd->sess, cmd->post_exec_start,
1225                         ts.tv_sec, ts.tv_nsec);
1226         }
1227 #endif
1228
1229         cmd->status = result & 0xff;
1230         cmd->msg_status = msg_byte(result);
1231         cmd->host_status = host_byte(result);
1232         cmd->driver_status = driver_byte(result);
1233         if (unlikely(resid != 0)) {
1234 #ifdef CONFIG_SCST_EXTRACHECKS
1235                 if ((resid < 0) || (resid > cmd->resp_data_len)) {
1236                         PRINT_ERROR("Wrong resid %d (cmd->resp_data_len=%d, "
1237                                 "op %x)", resid, cmd->resp_data_len,
1238                                 cmd->cdb[0]);
1239                 } else
1240 #endif
1241                         scst_set_resp_data_len(cmd, cmd->resp_data_len - resid);
1242         }
1243
1244         if (unlikely(cmd->status == SAM_STAT_CHECK_CONDITION)) {
1245                 /* We might have double reset UA here */
1246                 cmd->dbl_ua_orig_resp_data_len = cmd->resp_data_len;
1247                 cmd->dbl_ua_orig_data_direction = cmd->data_direction;
1248
1249                 scst_alloc_set_sense(cmd, 1, rq_sense, rq_sense_len);
1250         }
1251
1252         TRACE(TRACE_SCSI, "cmd %p, result=%x, cmd->status=%x, resid=%d, "
1253               "cmd->msg_status=%x, cmd->host_status=%x, "
1254               "cmd->driver_status=%x (cmd %p)", cmd, result, cmd->status, resid,
1255               cmd->msg_status, cmd->host_status, cmd->driver_status, cmd);
1256
1257         cmd->completed = 1;
1258
1259         TRACE_EXIT();
1260         return;
1261 }
1262
1263 /* For small context optimization */
1264 static inline enum scst_exec_context scst_optimize_post_exec_context(
1265         struct scst_cmd *cmd, enum scst_exec_context context)
1266 {
1267         if (((context == SCST_CONTEXT_SAME) && scst_cmd_atomic(cmd)) ||
1268             (context == SCST_CONTEXT_TASKLET) ||
1269             (context == SCST_CONTEXT_DIRECT_ATOMIC)) {
1270                 if (!test_bit(SCST_TGT_DEV_AFTER_EXEC_ATOMIC,
1271                                 &cmd->tgt_dev->tgt_dev_flags))
1272                         context = SCST_CONTEXT_THREAD;
1273         }
1274         return context;
1275 }
1276
1277 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18)
1278 static inline struct scst_cmd *scst_get_cmd(struct scsi_cmnd *scsi_cmd,
1279                                             struct scsi_request **req)
1280 {
1281         struct scst_cmd *cmd = NULL;
1282
1283         if (scsi_cmd && (*req = scsi_cmd->sc_request))
1284                 cmd = (struct scst_cmd *)(*req)->upper_private_data;
1285
1286         if (cmd == NULL) {
1287                 PRINT_ERROR("%s", "Request with NULL cmd");
1288                 if (*req)
1289                         scsi_release_request(*req);
1290         }
1291
1292         return cmd;
1293 }
1294
1295 static void scst_cmd_done(struct scsi_cmnd *scsi_cmd)
1296 {
1297         struct scsi_request *req = NULL;
1298         struct scst_cmd *cmd;
1299
1300         TRACE_ENTRY();
1301
1302         cmd = scst_get_cmd(scsi_cmd, &req);
1303         if (cmd == NULL)
1304                 goto out;
1305
1306         scst_do_cmd_done(cmd, req->sr_result, req->sr_sense_buffer,
1307                 sizeof(req->sr_sense_buffer), scsi_cmd->resid);
1308
1309         /* Clear out request structure */
1310         req->sr_use_sg = 0;
1311         req->sr_sglist_len = 0;
1312         req->sr_bufflen = 0;
1313         req->sr_buffer = NULL;
1314         req->sr_underflow = 0;
1315         req->sr_request->rq_disk = NULL; /* disown request blk */
1316
1317         scst_release_request(cmd);
1318
1319         cmd->state = SCST_CMD_STATE_PRE_DEV_DONE;
1320
1321         scst_proccess_redirect_cmd(cmd,
1322                 scst_optimize_post_exec_context(cmd, scst_estimate_context()),
1323                                                 0);
1324
1325 out:
1326         TRACE_EXIT();
1327         return;
1328 }
1329 #else /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18) */
1330 static void scst_cmd_done(void *data, char *sense, int result, int resid)
1331 {
1332         struct scst_cmd *cmd;
1333
1334         TRACE_ENTRY();
1335
1336         cmd = (struct scst_cmd *)data;
1337         if (cmd == NULL)
1338                 goto out;
1339
1340         scst_do_cmd_done(cmd, result, sense, SCSI_SENSE_BUFFERSIZE, resid);
1341
1342         cmd->state = SCST_CMD_STATE_PRE_DEV_DONE;
1343
1344         scst_proccess_redirect_cmd(cmd,
1345             scst_optimize_post_exec_context(cmd, scst_estimate_context()), 0);
1346
1347 out:
1348         TRACE_EXIT();
1349         return;
1350 }
1351 #endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18) */
1352
1353 static void scst_cmd_done_local(struct scst_cmd *cmd, int next_state,
1354         enum scst_exec_context pref_context)
1355 {
1356         TRACE_ENTRY();
1357
1358 #ifdef CONFIG_SCST_MEASURE_LATENCY
1359         {
1360                 struct timespec ts;
1361                 getnstimeofday(&ts);
1362                 cmd->post_exec_start = scst_sec_to_nsec(ts.tv_sec) + ts.tv_nsec;
1363                 TRACE_DBG("cmd %p (sess %p): post_exec_start %lld (tv_sec %ld, "
1364                         "tv_nsec %ld)", cmd, cmd->sess, cmd->post_exec_start,
1365                         ts.tv_sec, ts.tv_nsec);
1366         }
1367 #endif
1368
1369         if (next_state == SCST_CMD_STATE_DEFAULT)
1370                 next_state = SCST_CMD_STATE_PRE_DEV_DONE;
1371
1372 #if defined(CONFIG_SCST_DEBUG)
1373         if (next_state == SCST_CMD_STATE_PRE_DEV_DONE) {
1374                 if ((trace_flag & TRACE_RCV_TOP) && (cmd->sg != NULL)) {
1375                         int i;
1376                         struct scatterlist *sg = cmd->sg;
1377                         TRACE_RECV_TOP("Exec'd %d S/G(s) at %p sg[0].page at "
1378                                 "%p", cmd->sg_cnt, sg, (void *)sg_page(&sg[0]));
1379                         for (i = 0; i < cmd->sg_cnt; ++i) {
1380                                 TRACE_BUFF_FLAG(TRACE_RCV_TOP,
1381                                         "Exec'd sg", sg_virt(&sg[i]),
1382                                         sg[i].length);
1383                         }
1384                 }
1385         }
1386 #endif
1387
1388         cmd->state = next_state;
1389
1390 #ifdef CONFIG_SCST_EXTRACHECKS
1391         if ((next_state != SCST_CMD_STATE_PRE_DEV_DONE) &&
1392             (next_state != SCST_CMD_STATE_PRE_XMIT_RESP) &&
1393             (next_state != SCST_CMD_STATE_FINISHED) &&
1394             (next_state != SCST_CMD_STATE_FINISHED_INTERNAL)) {
1395                 PRINT_ERROR("%s() received invalid cmd state %d (opcode %d)",
1396                         __func__, next_state, cmd->cdb[0]);
1397                 scst_set_cmd_error(cmd,
1398                                    SCST_LOAD_SENSE(scst_sense_hardw_error));
1399                 scst_set_cmd_abnormal_done_state(cmd);
1400         }
1401 #endif
1402         pref_context = scst_optimize_post_exec_context(cmd, pref_context);
1403         scst_proccess_redirect_cmd(cmd, pref_context, 0);
1404
1405         TRACE_EXIT();
1406         return;
1407 }
1408
1409 static int scst_report_luns_local(struct scst_cmd *cmd)
1410 {
1411         int res = SCST_EXEC_COMPLETED, rc;
1412         int dev_cnt = 0;
1413         int buffer_size;
1414         int i;
1415         struct scst_tgt_dev *tgt_dev = NULL;
1416         uint8_t *buffer;
1417         int offs, overflow = 0;
1418
1419         TRACE_ENTRY();
1420
1421         if (scst_cmd_atomic(cmd)) {
1422                 res = SCST_EXEC_NEED_THREAD;
1423                 goto out;
1424         }
1425
1426         rc = scst_check_local_events(cmd);
1427         if (unlikely(rc != 0))
1428                 goto out_done;
1429
1430         cmd->status = 0;
1431         cmd->msg_status = 0;
1432         cmd->host_status = DID_OK;
1433         cmd->driver_status = 0;
1434
1435         if ((cmd->cdb[2] != 0) && (cmd->cdb[2] != 2)) {
1436                 PRINT_ERROR("Unsupported SELECT REPORT value %x in REPORT "
1437                         "LUNS command", cmd->cdb[2]);
1438                 goto out_err;
1439         }
1440
1441         buffer_size = scst_get_buf_first(cmd, &buffer);
1442         if (unlikely(buffer_size == 0))
1443                 goto out_compl;
1444         else if (unlikely(buffer_size < 0))
1445                 goto out_hw_err;
1446
1447         if (buffer_size < 16)
1448                 goto out_put_err;
1449
1450         memset(buffer, 0, buffer_size);
1451         offs = 8;
1452
1453         /* sess->sess_tgt_dev_list_hash is protected by suspended activity */
1454         for (i = 0; i < TGT_DEV_HASH_SIZE; i++) {
1455                 struct list_head *sess_tgt_dev_list_head =
1456                         &cmd->sess->sess_tgt_dev_list_hash[i];
1457                 list_for_each_entry(tgt_dev, sess_tgt_dev_list_head,
1458                                 sess_tgt_dev_list_entry) {
1459                         if (!overflow) {
1460                                 if (offs >= buffer_size) {
1461                                         scst_put_buf(cmd, buffer);
1462                                         buffer_size = scst_get_buf_next(cmd,
1463                                                                        &buffer);
1464                                         if (buffer_size > 0) {
1465                                                 memset(buffer, 0, buffer_size);
1466                                                 offs = 0;
1467                                         } else {
1468                                                 overflow = 1;
1469                                                 goto inc_dev_cnt;
1470                                         }
1471                                 }
1472                                 if ((buffer_size - offs) < 8) {
1473                                         PRINT_ERROR("Buffer allocated for "
1474                                                 "REPORT LUNS command doesn't "
1475                                                 "allow to fit 8 byte entry "
1476                                                 "(buffer_size=%d)",
1477                                                 buffer_size);
1478                                         goto out_put_hw_err;
1479                                 }
1480                                 buffer[offs] = (tgt_dev->lun >> 8) & 0xff;
1481                                 buffer[offs+1] = tgt_dev->lun & 0xff;
1482                                 offs += 8;
1483                         }
1484 inc_dev_cnt:
1485                         dev_cnt++;
1486                 }
1487         }
1488         if (!overflow)
1489                 scst_put_buf(cmd, buffer);
1490
1491         /* Set the response header */
1492         buffer_size = scst_get_buf_first(cmd, &buffer);
1493         if (unlikely(buffer_size == 0))
1494                 goto out_compl;
1495         else if (unlikely(buffer_size < 0))
1496                 goto out_hw_err;
1497
1498         dev_cnt *= 8;
1499         buffer[0] = (dev_cnt >> 24) & 0xff;
1500         buffer[1] = (dev_cnt >> 16) & 0xff;
1501         buffer[2] = (dev_cnt >> 8) & 0xff;
1502         buffer[3] = dev_cnt & 0xff;
1503
1504         scst_put_buf(cmd, buffer);
1505
1506         dev_cnt += 8;
1507         if (dev_cnt < cmd->resp_data_len)
1508                 scst_set_resp_data_len(cmd, dev_cnt);
1509
1510 out_compl:
1511         cmd->completed = 1;
1512
1513         /* Clear left sense_reported_luns_data_changed UA, if any. */
1514
1515         mutex_lock(&scst_mutex); /* protect sess_tgt_dev_list_hash */
1516         for (i = 0; i < TGT_DEV_HASH_SIZE; i++) {
1517                 struct list_head *sess_tgt_dev_list_head =
1518                         &cmd->sess->sess_tgt_dev_list_hash[i];
1519
1520                 list_for_each_entry(tgt_dev, sess_tgt_dev_list_head,
1521                                 sess_tgt_dev_list_entry) {
1522                         struct scst_tgt_dev_UA *ua;
1523
1524                         spin_lock_bh(&tgt_dev->tgt_dev_lock);
1525                         list_for_each_entry(ua, &tgt_dev->UA_list,
1526                                                 UA_list_entry) {
1527                                 if (scst_analyze_sense(ua->UA_sense_buffer,
1528                                                 sizeof(ua->UA_sense_buffer),
1529                                                 SCST_SENSE_ALL_VALID,
1530                                                 SCST_LOAD_SENSE(scst_sense_reported_luns_data_changed))) {
1531                                         TRACE_MGMT_DBG("Freeing not needed "
1532                                                 "REPORTED LUNS DATA CHANGED UA "
1533                                                 "%p", ua);
1534                                         list_del(&ua->UA_list_entry);
1535                                         mempool_free(ua, scst_ua_mempool);
1536                                         break;
1537                                 }
1538                         }
1539                         spin_unlock_bh(&tgt_dev->tgt_dev_lock);
1540                 }
1541         }
1542         mutex_unlock(&scst_mutex);
1543
1544 out_done:
1545         /* Report the result */
1546         cmd->scst_cmd_done(cmd, SCST_CMD_STATE_DEFAULT, SCST_CONTEXT_SAME);
1547
1548 out:
1549         TRACE_EXIT_RES(res);
1550         return res;
1551
1552 out_put_err:
1553         scst_put_buf(cmd, buffer);
1554
1555 out_err:
1556         scst_set_cmd_error(cmd,
1557                    SCST_LOAD_SENSE(scst_sense_invalid_field_in_cdb));
1558         goto out_compl;
1559
1560 out_put_hw_err:
1561         scst_put_buf(cmd, buffer);
1562
1563 out_hw_err:
1564         scst_set_cmd_error(cmd, SCST_LOAD_SENSE(scst_sense_hardw_error));
1565         goto out_compl;
1566 }
1567
1568 static int scst_request_sense_local(struct scst_cmd *cmd)
1569 {
1570         int res = SCST_EXEC_COMPLETED, rc;
1571         struct scst_tgt_dev *tgt_dev = cmd->tgt_dev;
1572         uint8_t *buffer;
1573         int buffer_size = 0;
1574
1575         TRACE_ENTRY();
1576
1577         rc = scst_check_local_events(cmd);
1578         if (unlikely(rc != 0))
1579                 goto out_done;
1580
1581         cmd->status = 0;
1582         cmd->msg_status = 0;
1583         cmd->host_status = DID_OK;
1584         cmd->driver_status = 0;
1585
1586         spin_lock_bh(&tgt_dev->tgt_dev_lock);
1587
1588         if (tgt_dev->tgt_dev_valid_sense_len == 0)
1589                 goto out_not_completed;
1590
1591         TRACE(TRACE_SCSI, "%s: Returning stored sense", cmd->op_name);
1592
1593         buffer_size = scst_get_buf_first(cmd, &buffer);
1594         if (unlikely(buffer_size == 0))
1595                 goto out_compl;
1596         else if (unlikely(buffer_size < 0))
1597                 goto out_hw_err;
1598
1599         memset(buffer, 0, buffer_size);
1600
1601         if (((tgt_dev->tgt_dev_sense[0] == 0x70) ||
1602              (tgt_dev->tgt_dev_sense[0] == 0x71)) && (cmd->cdb[1] & 1)) {
1603                 PRINT_WARNING("%s: Fixed format of the saved sense, but "
1604                         "descriptor format requested. Convertion will "
1605                         "truncated data", cmd->op_name);
1606                 PRINT_BUFFER("Original sense", tgt_dev->tgt_dev_sense,
1607                         tgt_dev->tgt_dev_valid_sense_len);
1608
1609                 buffer_size = min(SCST_STANDARD_SENSE_LEN, buffer_size);
1610                 scst_set_sense(buffer, buffer_size, true,
1611                         tgt_dev->tgt_dev_sense[2], tgt_dev->tgt_dev_sense[12],
1612                         tgt_dev->tgt_dev_sense[13]);
1613         } else if (((tgt_dev->tgt_dev_sense[0] == 0x72) ||
1614                     (tgt_dev->tgt_dev_sense[0] == 0x73)) && !(cmd->cdb[1] & 1)) {
1615                 PRINT_WARNING("%s: Descriptor format of the "
1616                         "saved sense, but fixed format requested. Convertion "
1617                         "will truncated data", cmd->op_name);
1618                 PRINT_BUFFER("Original sense", tgt_dev->tgt_dev_sense,
1619                         tgt_dev->tgt_dev_valid_sense_len);
1620
1621                 buffer_size = min(SCST_STANDARD_SENSE_LEN, buffer_size);
1622                 scst_set_sense(buffer, buffer_size, false,
1623                         tgt_dev->tgt_dev_sense[1], tgt_dev->tgt_dev_sense[2],
1624                         tgt_dev->tgt_dev_sense[3]);
1625         } else {
1626                 if (buffer_size >= tgt_dev->tgt_dev_valid_sense_len)
1627                         buffer_size = tgt_dev->tgt_dev_valid_sense_len;
1628                 else {
1629                         PRINT_WARNING("%s: Being returned sense truncated to "
1630                                 "size %d (needed %d)", cmd->op_name,
1631                                 buffer_size, tgt_dev->tgt_dev_valid_sense_len);
1632                 }
1633                 memcpy(buffer, tgt_dev->tgt_dev_sense, buffer_size);
1634         }
1635
1636         scst_put_buf(cmd, buffer);
1637
1638 out_compl:
1639         tgt_dev->tgt_dev_valid_sense_len = 0;
1640         scst_set_resp_data_len(cmd, buffer_size);
1641
1642         spin_unlock_bh(&tgt_dev->tgt_dev_lock);
1643
1644         cmd->completed = 1;
1645
1646 out_done:
1647         /* Report the result */
1648         cmd->scst_cmd_done(cmd, SCST_CMD_STATE_DEFAULT, SCST_CONTEXT_SAME);
1649
1650 out:
1651         TRACE_EXIT_RES(res);
1652         return res;
1653
1654 out_hw_err:
1655         scst_set_cmd_error(cmd, SCST_LOAD_SENSE(scst_sense_hardw_error));
1656         goto out_compl;
1657
1658 out_not_completed:
1659         spin_unlock_bh(&tgt_dev->tgt_dev_lock);
1660         res = SCST_EXEC_NOT_COMPLETED;
1661         goto out;
1662 }
1663
1664 static int scst_pre_select(struct scst_cmd *cmd)
1665 {
1666         int res = SCST_EXEC_NOT_COMPLETED;
1667
1668         TRACE_ENTRY();
1669
1670         if (scst_cmd_atomic(cmd)) {
1671                 res = SCST_EXEC_NEED_THREAD;
1672                 goto out;
1673         }
1674
1675         scst_block_dev_cmd(cmd, 1);
1676
1677         /* Check for local events will be done when cmd will be executed */
1678
1679 out:
1680         TRACE_EXIT_RES(res);
1681         return res;
1682 }
1683
1684 static int scst_reserve_local(struct scst_cmd *cmd)
1685 {
1686         int res = SCST_EXEC_NOT_COMPLETED, rc;
1687         struct scst_device *dev;
1688         struct scst_tgt_dev *tgt_dev_tmp;
1689
1690         TRACE_ENTRY();
1691
1692         if (scst_cmd_atomic(cmd)) {
1693                 res = SCST_EXEC_NEED_THREAD;
1694                 goto out;
1695         }
1696
1697         if ((cmd->cdb[0] == RESERVE_10) && (cmd->cdb[2] & SCST_RES_3RDPTY)) {
1698                 PRINT_ERROR("RESERVE_10: 3rdPty RESERVE not implemented "
1699                      "(lun=%lld)", (long long unsigned int)cmd->lun);
1700                 scst_set_cmd_error(cmd,
1701                         SCST_LOAD_SENSE(scst_sense_invalid_field_in_cdb));
1702                 goto out_done;
1703         }
1704
1705         dev = cmd->dev;
1706
1707         if (dev->tst == SCST_CONTR_MODE_ONE_TASK_SET)
1708                 scst_block_dev_cmd(cmd, 1);
1709
1710         rc = scst_check_local_events(cmd);
1711         if (unlikely(rc != 0))
1712                 goto out_done;
1713
1714         spin_lock_bh(&dev->dev_lock);
1715
1716         if (test_bit(SCST_TGT_DEV_RESERVED, &cmd->tgt_dev->tgt_dev_flags)) {
1717                 spin_unlock_bh(&dev->dev_lock);
1718                 scst_set_cmd_error_status(cmd, SAM_STAT_RESERVATION_CONFLICT);
1719                 goto out_done;
1720         }
1721
1722         list_for_each_entry(tgt_dev_tmp, &dev->dev_tgt_dev_list,
1723                             dev_tgt_dev_list_entry) {
1724                 if (cmd->tgt_dev != tgt_dev_tmp)
1725                         set_bit(SCST_TGT_DEV_RESERVED,
1726                                 &tgt_dev_tmp->tgt_dev_flags);
1727         }
1728         dev->dev_reserved = 1;
1729
1730         spin_unlock_bh(&dev->dev_lock);
1731
1732 out:
1733         TRACE_EXIT_RES(res);
1734         return res;
1735
1736 out_done:
1737         /* Report the result */
1738         cmd->scst_cmd_done(cmd, SCST_CMD_STATE_DEFAULT, SCST_CONTEXT_SAME);
1739         res = SCST_EXEC_COMPLETED;
1740         goto out;
1741 }
1742
1743 static int scst_release_local(struct scst_cmd *cmd)
1744 {
1745         int res = SCST_EXEC_NOT_COMPLETED, rc;
1746         struct scst_tgt_dev *tgt_dev_tmp;
1747         struct scst_device *dev;
1748
1749         TRACE_ENTRY();
1750
1751         if (scst_cmd_atomic(cmd)) {
1752                 res = SCST_EXEC_NEED_THREAD;
1753                 goto out;
1754         }
1755
1756         dev = cmd->dev;
1757
1758         if (dev->tst == SCST_CONTR_MODE_ONE_TASK_SET)
1759                 scst_block_dev_cmd(cmd, 1);
1760
1761         rc = scst_check_local_events(cmd);
1762         if (unlikely(rc != 0))
1763                 goto out_done;
1764
1765         spin_lock_bh(&dev->dev_lock);
1766
1767         /*
1768          * The device could be RELEASED behind us, if RESERVING session
1769          * is closed (see scst_free_tgt_dev()), but this actually doesn't
1770          * matter, so use lock and no retest for DEV_RESERVED bits again
1771          */
1772         if (test_bit(SCST_TGT_DEV_RESERVED, &cmd->tgt_dev->tgt_dev_flags)) {
1773                 res = SCST_EXEC_COMPLETED;
1774                 cmd->status = 0;
1775                 cmd->msg_status = 0;
1776                 cmd->host_status = DID_OK;
1777                 cmd->driver_status = 0;
1778                 cmd->completed = 1;
1779         } else {
1780                 list_for_each_entry(tgt_dev_tmp,
1781                                     &dev->dev_tgt_dev_list,
1782                                     dev_tgt_dev_list_entry) {
1783                         clear_bit(SCST_TGT_DEV_RESERVED,
1784                                 &tgt_dev_tmp->tgt_dev_flags);
1785                 }
1786                 dev->dev_reserved = 0;
1787         }
1788
1789         spin_unlock_bh(&dev->dev_lock);
1790
1791         if (res == SCST_EXEC_COMPLETED)
1792                 goto out_done;
1793
1794 out:
1795         TRACE_EXIT_RES(res);
1796         return res;
1797
1798 out_done:
1799         res = SCST_EXEC_COMPLETED;
1800         /* Report the result */
1801         cmd->scst_cmd_done(cmd, SCST_CMD_STATE_DEFAULT, SCST_CONTEXT_SAME);
1802         goto out;
1803 }
1804
1805 /* No locks, no IRQ or IRQ-safe context allowed */
1806 int scst_check_local_events(struct scst_cmd *cmd)
1807 {
1808         int res, rc;
1809         struct scst_tgt_dev *tgt_dev = cmd->tgt_dev;
1810         struct scst_device *dev = cmd->dev;
1811
1812         TRACE_ENTRY();
1813
1814         /*
1815          * There's no race here, because we need to trace commands sent
1816          * *after* dev_double_ua_possible flag was set.
1817          */
1818         if (unlikely(dev->dev_double_ua_possible))
1819                 cmd->double_ua_possible = 1;
1820
1821         if (unlikely(test_bit(SCST_CMD_ABORTED, &cmd->cmd_flags))) {
1822                 TRACE_MGMT_DBG("ABORTED set, aborting cmd %p", cmd);
1823                 goto out_uncomplete;
1824         }
1825
1826         /* Reserve check before Unit Attention */
1827         if (unlikely(test_bit(SCST_TGT_DEV_RESERVED,
1828                               &tgt_dev->tgt_dev_flags))) {
1829                 if (cmd->cdb[0] != INQUIRY &&
1830                     cmd->cdb[0] != REPORT_LUNS &&
1831                     cmd->cdb[0] != RELEASE &&
1832                     cmd->cdb[0] != RELEASE_10 &&
1833                     cmd->cdb[0] != REPORT_DEVICE_IDENTIFIER &&
1834                     (cmd->cdb[0] != ALLOW_MEDIUM_REMOVAL ||
1835                      (cmd->cdb[4] & 3)) &&
1836                     cmd->cdb[0] != LOG_SENSE &&
1837                     cmd->cdb[0] != REQUEST_SENSE) {
1838                         scst_set_cmd_error_status(cmd,
1839                                 SAM_STAT_RESERVATION_CONFLICT);
1840                         goto out_complete;
1841                 }
1842         }
1843
1844         /* If we had internal bus reset, set the command error unit attention */
1845         if ((dev->scsi_dev != NULL) &&
1846             unlikely(dev->scsi_dev->was_reset)) {
1847                 if (scst_is_ua_command(cmd)) {
1848                         int done = 0;
1849                         /*
1850                          * Prevent more than 1 cmd to be triggered by
1851                          * was_reset.
1852                          */
1853                         spin_lock_bh(&dev->dev_lock);
1854                         if (dev->scsi_dev->was_reset) {
1855                                 TRACE(TRACE_MGMT, "was_reset is %d", 1);
1856                                 scst_set_cmd_error(cmd,
1857                                           SCST_LOAD_SENSE(scst_sense_reset_UA));
1858                                 /*
1859                                  * It looks like it is safe to clear was_reset
1860                                  * here.
1861                                  */
1862                                 dev->scsi_dev->was_reset = 0;
1863                                 done = 1;
1864                         }
1865                         spin_unlock_bh(&dev->dev_lock);
1866
1867                         if (done)
1868                                 goto out_complete;
1869                 }
1870         }
1871
1872         if (unlikely(test_bit(SCST_TGT_DEV_UA_PENDING,
1873                         &cmd->tgt_dev->tgt_dev_flags))) {
1874                 if (scst_is_ua_command(cmd)) {
1875                         rc = scst_set_pending_UA(cmd);
1876                         if (rc == 0)
1877                                 goto out_complete;
1878                 }
1879         }
1880
1881         res = 0;
1882
1883 out:
1884         TRACE_EXIT_RES(res);
1885         return res;
1886
1887 out_complete:
1888         res = 1;
1889         sBUG_ON(!cmd->completed);
1890         goto out;
1891
1892 out_uncomplete:
1893         res = -1;
1894         goto out;
1895 }
1896 EXPORT_SYMBOL(scst_check_local_events);
1897
1898 /* No locks */
1899 void scst_inc_expected_sn(struct scst_tgt_dev *tgt_dev, atomic_t *slot)
1900 {
1901         if (slot == NULL)
1902                 goto inc;
1903
1904         /* Optimized for lockless fast path */
1905
1906         TRACE_SN("Slot %zd, *cur_sn_slot %d", slot - tgt_dev->sn_slots,
1907                 atomic_read(slot));
1908
1909         if (!atomic_dec_and_test(slot))
1910                 goto out;
1911
1912         TRACE_SN("Slot is 0 (num_free_sn_slots=%d)",
1913                 tgt_dev->num_free_sn_slots);
1914         if (tgt_dev->num_free_sn_slots < (int)ARRAY_SIZE(tgt_dev->sn_slots)-1) {
1915                 spin_lock_irq(&tgt_dev->sn_lock);
1916                 if (likely(tgt_dev->num_free_sn_slots < (int)ARRAY_SIZE(tgt_dev->sn_slots)-1)) {
1917                         if (tgt_dev->num_free_sn_slots < 0)
1918                                 tgt_dev->cur_sn_slot = slot;
1919                         /*
1920                          * To be in-sync with SIMPLE case in scst_cmd_set_sn()
1921                          */
1922                         smp_mb();
1923                         tgt_dev->num_free_sn_slots++;
1924                         TRACE_SN("Incremented num_free_sn_slots (%d)",
1925                                 tgt_dev->num_free_sn_slots);
1926
1927                 }
1928                 spin_unlock_irq(&tgt_dev->sn_lock);
1929         }
1930
1931 inc:
1932         /*
1933          * No protection of expected_sn is needed, because only one thread
1934          * at time can be here (serialized by sn). Also it is supposed that
1935          * there could not be half-incremented halves.
1936          */
1937         tgt_dev->expected_sn++;
1938         /*
1939          * Write must be before def_cmd_count read to be in sync. with
1940          * scst_post_exec_sn(). See comment in scst_send_for_exec().
1941          */
1942         smp_mb();
1943         TRACE_SN("Next expected_sn: %ld", tgt_dev->expected_sn);
1944
1945 out:
1946         return;
1947 }
1948
1949 /* No locks */
1950 static struct scst_cmd *scst_post_exec_sn(struct scst_cmd *cmd,
1951         bool make_active)
1952 {
1953         /* For HQ commands SN is not set */
1954         bool inc_expected_sn = !cmd->inc_expected_sn_on_done &&
1955                                cmd->sn_set && !cmd->retry;
1956         struct scst_tgt_dev *tgt_dev = cmd->tgt_dev;
1957         struct scst_cmd *res;
1958
1959         TRACE_ENTRY();
1960
1961         if (inc_expected_sn)
1962                 scst_inc_expected_sn(tgt_dev, cmd->sn_slot);
1963
1964         if (make_active) {
1965                 scst_make_deferred_commands_active(tgt_dev);
1966                 res = NULL;
1967         } else
1968                 res = scst_check_deferred_commands(tgt_dev);
1969
1970         TRACE_EXIT_HRES(res);
1971         return res;
1972 }
1973
1974 /* cmd must be additionally referenced to not die inside */
1975 static int scst_do_real_exec(struct scst_cmd *cmd)
1976 {
1977         int res = SCST_EXEC_NOT_COMPLETED;
1978 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 18)
1979         int rc;
1980 #endif
1981         bool atomic = scst_cmd_atomic(cmd);
1982         struct scst_device *dev = cmd->dev;
1983         struct scst_dev_type *handler = dev->handler;
1984         struct io_context *old_ctx = NULL;
1985         bool ctx_changed = false;
1986
1987         TRACE_ENTRY();
1988
1989         if (!atomic)
1990                 ctx_changed = scst_set_io_context(cmd, &old_ctx);
1991
1992         cmd->state = SCST_CMD_STATE_REAL_EXECUTING;
1993
1994         if (handler->exec) {
1995                 if (unlikely(!dev->handler->exec_atomic && atomic)) {
1996                         /*
1997                          * It shouldn't be because of SCST_TGT_DEV_AFTER_*
1998                          * optimization.
1999                          */
2000                         TRACE_DBG("Dev handler %s exec() needs thread "
2001                                 "context, rescheduling", dev->handler->name);
2002                         res = SCST_EXEC_NEED_THREAD;
2003                         goto out_restore;
2004                 }
2005
2006                 TRACE_DBG("Calling dev handler %s exec(%p)",
2007                       handler->name, cmd);
2008                 TRACE_BUFF_FLAG(TRACE_SND_TOP, "Execing: ", cmd->cdb,
2009                         cmd->cdb_len);
2010                 res = handler->exec(cmd);
2011                 TRACE_DBG("Dev handler %s exec() returned %d",
2012                       handler->name, res);
2013
2014                 if (res == SCST_EXEC_COMPLETED)
2015                         goto out_complete;
2016                 else if (res == SCST_EXEC_NEED_THREAD)
2017                         goto out_restore;
2018
2019                 sBUG_ON(res != SCST_EXEC_NOT_COMPLETED);
2020         }
2021
2022         TRACE_DBG("Sending cmd %p to SCSI mid-level", cmd);
2023
2024         if (unlikely(dev->scsi_dev == NULL)) {
2025                 PRINT_ERROR("Command for virtual device must be "
2026                         "processed by device handler (LUN %lld)!",
2027                         (long long unsigned int)cmd->lun);
2028                 goto out_error;
2029         }
2030
2031         res = scst_check_local_events(cmd);
2032         if (unlikely(res != 0))
2033                 goto out_done;
2034
2035 #ifndef CONFIG_SCST_ALLOW_PASSTHROUGH_IO_SUBMIT_IN_SIRQ
2036         if (unlikely(atomic)) {
2037                 TRACE_DBG("Pass-through exec() can not be called in atomic "
2038                         "context, rescheduling to the thread (handler %s)",
2039                         handler->name);
2040                 res = SCST_EXEC_NEED_THREAD;
2041                 goto out_restore;
2042         }
2043 #endif
2044
2045 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18)
2046         if (unlikely(scst_alloc_request(cmd) != 0)) {
2047                 if (atomic) {
2048                         res = SCST_EXEC_NEED_THREAD;
2049                         goto out_restore;
2050                 } else {
2051                         PRINT_INFO("%s", "Unable to allocate request, "
2052                                 "sending BUSY status");
2053                         goto out_busy;
2054                 }
2055         }
2056
2057         scst_do_req(cmd->scsi_req, (void *)cmd->cdb,
2058                     (void *)cmd->scsi_req->sr_buffer,
2059                     cmd->scsi_req->sr_bufflen, scst_cmd_done, cmd->timeout,
2060                     cmd->retries);
2061 #else
2062         rc = scst_exec_req(dev->scsi_dev, cmd->cdb, cmd->cdb_len,
2063                         cmd->data_direction, cmd->sg, cmd->bufflen, cmd->sg_cnt,
2064                         cmd->timeout, cmd->retries, cmd, scst_cmd_done,
2065                         atomic ? GFP_ATOMIC : GFP_KERNEL);
2066         if (unlikely(rc != 0)) {
2067                 if (atomic) {
2068                         res = SCST_EXEC_NEED_THREAD;
2069                         goto out_restore;
2070                 } else {
2071                         PRINT_ERROR("scst_exec_req() failed: %d", res);
2072                         goto out_error;
2073                 }
2074         }
2075 #endif
2076
2077 out_complete:
2078         res = SCST_EXEC_COMPLETED;
2079
2080 out_reset_ctx:
2081         if (ctx_changed)
2082                 scst_reset_io_context(cmd->tgt_dev, old_ctx);
2083
2084         TRACE_EXIT();
2085         return res;
2086
2087 out_restore:
2088         /* Restore the state */
2089         cmd->state = SCST_CMD_STATE_REAL_EXEC;
2090         goto out_reset_ctx;
2091
2092 out_error:
2093         scst_set_cmd_error(cmd, SCST_LOAD_SENSE(scst_sense_hardw_error));
2094         goto out_done;
2095
2096 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18)
2097 out_busy:
2098         scst_set_busy(cmd);
2099         /* go through */
2100 #endif
2101
2102 out_done:
2103         res = SCST_EXEC_COMPLETED;
2104         /* Report the result */
2105         cmd->scst_cmd_done(cmd, SCST_CMD_STATE_DEFAULT, SCST_CONTEXT_SAME);
2106         goto out_complete;
2107 }
2108
2109 static inline int scst_real_exec(struct scst_cmd *cmd)
2110 {
2111         int res;
2112
2113         TRACE_ENTRY();
2114
2115         BUILD_BUG_ON(SCST_CMD_STATE_RES_CONT_SAME != SCST_EXEC_NOT_COMPLETED);
2116         BUILD_BUG_ON(SCST_CMD_STATE_RES_CONT_NEXT != SCST_EXEC_COMPLETED);
2117         BUILD_BUG_ON(SCST_CMD_STATE_RES_NEED_THREAD != SCST_EXEC_NEED_THREAD);
2118
2119         __scst_cmd_get(cmd);
2120
2121         res = scst_do_real_exec(cmd);
2122
2123         if (likely(res == SCST_EXEC_COMPLETED)) {
2124                 scst_post_exec_sn(cmd, true);
2125                 if (cmd->dev->scsi_dev != NULL)
2126                         generic_unplug_device(
2127                                 cmd->dev->scsi_dev->request_queue);
2128         } else
2129                 sBUG_ON(res != SCST_EXEC_NEED_THREAD);
2130
2131         __scst_cmd_put(cmd);
2132
2133         /* SCST_EXEC_* match SCST_CMD_STATE_RES_* */
2134
2135         TRACE_EXIT_RES(res);
2136         return res;
2137 }
2138
2139 static int scst_do_local_exec(struct scst_cmd *cmd)
2140 {
2141         int res;
2142         struct scst_tgt_dev *tgt_dev = cmd->tgt_dev;
2143
2144         TRACE_ENTRY();
2145
2146         /* Check READ_ONLY device status */
2147         if ((cmd->op_flags & SCST_WRITE_MEDIUM) &&
2148             ((tgt_dev->acg_dev->rd_only_flag) || cmd->dev->swp)) {
2149                 PRINT_WARNING("Attempt of write access to read-only device: "
2150                         "initiator %s, LUN %lld, op %x",
2151                         cmd->sess->initiator_name, cmd->lun, cmd->cdb[0]);
2152                 scst_set_cmd_error(cmd,
2153                            SCST_LOAD_SENSE(scst_sense_data_protect));
2154                 goto out_done;
2155         }
2156
2157         /*
2158          * Adding new commands here don't forget to update
2159          * scst_is_cmd_local() in scst.h, if necessary
2160          */
2161
2162         if (!(cmd->op_flags & SCST_LOCAL_EXEC_NEEDED)) {
2163                 res = SCST_EXEC_NOT_COMPLETED;
2164                 goto out;
2165         }
2166
2167         switch (cmd->cdb[0]) {
2168         case MODE_SELECT:
2169         case MODE_SELECT_10:
2170         case LOG_SELECT:
2171                 res = scst_pre_select(cmd);
2172                 break;
2173         case RESERVE:
2174         case RESERVE_10:
2175                 res = scst_reserve_local(cmd);
2176                 break;
2177         case RELEASE:
2178         case RELEASE_10:
2179                 res = scst_release_local(cmd);
2180                 break;
2181         case REPORT_LUNS:
2182                 res = scst_report_luns_local(cmd);
2183                 break;
2184         case REQUEST_SENSE:
2185                 res = scst_request_sense_local(cmd);
2186                 break;
2187         default:
2188                 res = SCST_EXEC_NOT_COMPLETED;
2189                 break;
2190         }
2191
2192 out:
2193         TRACE_EXIT_RES(res);
2194         return res;
2195
2196 out_done:
2197         /* Report the result */
2198         cmd->scst_cmd_done(cmd, SCST_CMD_STATE_DEFAULT, SCST_CONTEXT_SAME);
2199         res = SCST_EXEC_COMPLETED;
2200         goto out;
2201 }
2202
2203 static int scst_local_exec(struct scst_cmd *cmd)
2204 {
2205         int res;
2206
2207         TRACE_ENTRY();
2208
2209         BUILD_BUG_ON(SCST_CMD_STATE_RES_CONT_SAME != SCST_EXEC_NOT_COMPLETED);
2210         BUILD_BUG_ON(SCST_CMD_STATE_RES_CONT_NEXT != SCST_EXEC_COMPLETED);
2211         BUILD_BUG_ON(SCST_CMD_STATE_RES_NEED_THREAD != SCST_EXEC_NEED_THREAD);
2212
2213         __scst_cmd_get(cmd);
2214
2215         res = scst_do_local_exec(cmd);
2216         if (likely(res == SCST_EXEC_NOT_COMPLETED))
2217                 cmd->state = SCST_CMD_STATE_REAL_EXEC;
2218         else if (res == SCST_EXEC_COMPLETED)
2219                 scst_post_exec_sn(cmd, true);
2220         else
2221                 sBUG_ON(res != SCST_EXEC_NEED_THREAD);
2222
2223         __scst_cmd_put(cmd);
2224
2225         /* SCST_EXEC_* match SCST_CMD_STATE_RES_* */
2226         TRACE_EXIT_RES(res);
2227         return res;
2228 }
2229
2230 static int scst_exec(struct scst_cmd **active_cmd)
2231 {
2232         struct scst_cmd *cmd = *active_cmd;
2233         struct scst_cmd *ref_cmd;
2234         struct scst_device *dev = cmd->dev;
2235         int res = SCST_CMD_STATE_RES_CONT_NEXT, count;
2236
2237         TRACE_ENTRY();
2238
2239         if (unlikely(scst_inc_on_dev_cmd(cmd) != 0))
2240                 goto out;
2241
2242         /* To protect tgt_dev */
2243         ref_cmd = cmd;
2244         __scst_cmd_get(ref_cmd);
2245
2246         count = 0;
2247         while (1) {
2248                 int rc;
2249
2250                 cmd->sent_for_exec = 1;
2251                 /*
2252                  * To sync with scst_abort_cmd(). The above assignment must
2253                  * be before SCST_CMD_ABORTED test, done later in
2254                  * scst_check_local_events(). It's far from here, so the order
2255                  * is virtually guaranteed, but let's have it just in case.
2256                  */
2257                 smp_mb();
2258
2259                 cmd->scst_cmd_done = scst_cmd_done_local;
2260                 cmd->state = SCST_CMD_STATE_LOCAL_EXEC;
2261
2262                 if (cmd->tgt_data_buf_alloced && cmd->dh_data_buf_alloced &&
2263                     (cmd->data_direction & SCST_DATA_WRITE))
2264                         scst_copy_sg(cmd, SCST_SG_COPY_FROM_TARGET);
2265
2266                 rc = scst_do_local_exec(cmd);
2267                 if (likely(rc == SCST_EXEC_NOT_COMPLETED))
2268                         /* Nothing to do */;
2269                 else if (rc == SCST_EXEC_NEED_THREAD) {
2270                         TRACE_DBG("%s", "scst_do_local_exec() requested "
2271                                 "thread context, rescheduling");
2272                         scst_dec_on_dev_cmd(cmd);
2273                         res = SCST_CMD_STATE_RES_NEED_THREAD;
2274                         break;
2275                 } else {
2276                         sBUG_ON(rc != SCST_EXEC_COMPLETED);
2277                         goto done;
2278                 }
2279
2280                 cmd->state = SCST_CMD_STATE_REAL_EXEC;
2281
2282                 rc = scst_do_real_exec(cmd);
2283                 if (likely(rc == SCST_EXEC_COMPLETED))
2284                         /* Nothing to do */;
2285                 else if (rc == SCST_EXEC_NEED_THREAD) {
2286                         TRACE_DBG("scst_real_exec() requested thread "
2287                                 "context, rescheduling (cmd %p)", cmd);
2288                         scst_dec_on_dev_cmd(cmd);
2289                         res = SCST_CMD_STATE_RES_NEED_THREAD;
2290                         break;
2291                 } else
2292                         sBUG();
2293
2294 done:
2295                 count++;
2296
2297                 cmd = scst_post_exec_sn(cmd, false);
2298                 if (cmd == NULL)
2299                         break;
2300
2301                 if (unlikely(scst_inc_on_dev_cmd(cmd) != 0))
2302                         break;
2303
2304                 __scst_cmd_put(ref_cmd);
2305                 ref_cmd = cmd;
2306                 __scst_cmd_get(ref_cmd);
2307         }
2308
2309         *active_cmd = cmd;
2310
2311         if (count == 0)
2312                 goto out_put;
2313
2314         if (dev->scsi_dev != NULL)
2315                 generic_unplug_device(dev->scsi_dev->request_queue);
2316
2317 out_put:
2318         __scst_cmd_put(ref_cmd);
2319         /* !! At this point sess, dev and tgt_dev can be already freed !! */
2320
2321 out:
2322         TRACE_EXIT_RES(res);
2323         return res;
2324 }
2325
2326 static int scst_send_for_exec(struct scst_cmd **active_cmd)
2327 {
2328         int res;
2329         struct scst_cmd *cmd = *active_cmd;
2330         struct scst_tgt_dev *tgt_dev = cmd->tgt_dev;
2331         typeof(tgt_dev->expected_sn) expected_sn;
2332
2333         TRACE_ENTRY();
2334
2335 #ifdef CONFIG_SCST_MEASURE_LATENCY
2336         if (cmd->pre_exec_finish == 0) {
2337                 struct timespec ts;
2338                 getnstimeofday(&ts);
2339                 cmd->pre_exec_finish = scst_sec_to_nsec(ts.tv_sec) + ts.tv_nsec;
2340                 TRACE_DBG("cmd %p (sess %p): pre_exec_finish %lld (tv_sec %ld, "
2341                         "tv_nsec %ld)", cmd, cmd->sess, cmd->pre_exec_finish,
2342                         ts.tv_sec, ts.tv_nsec);
2343         }
2344 #endif
2345
2346         if (unlikely(cmd->internal))
2347                 goto exec;
2348
2349         if (unlikely(cmd->queue_type == SCST_CMD_QUEUE_HEAD_OF_QUEUE))
2350                 goto exec;
2351
2352         sBUG_ON(!cmd->sn_set);
2353
2354         expected_sn = tgt_dev->expected_sn;
2355         /* Optimized for lockless fast path */
2356         if ((cmd->sn != expected_sn) || (tgt_dev->hq_cmd_count > 0)) {
2357                 spin_lock_irq(&tgt_dev->sn_lock);
2358
2359                 tgt_dev->def_cmd_count++;
2360                 /*
2361                  * Memory barrier is needed here to implement lockless fast
2362                  * path. We need the exact order of read and write between
2363                  * def_cmd_count and expected_sn. Otherwise, we can miss case,
2364                  * when expected_sn was changed to be equal to cmd->sn while
2365                  * we are queuing cmd the deferred list after the expected_sn
2366                  * below. It will lead to a forever stuck command. But with
2367                  * the barrier in such case __scst_check_deferred_commands()
2368                  * will be called and it will take sn_lock, so we will be
2369                  * synchronized.
2370                  */
2371                 smp_mb();
2372
2373                 expected_sn = tgt_dev->expected_sn;
2374                 if ((cmd->sn != expected_sn) || (tgt_dev->hq_cmd_count > 0)) {
2375                         if (unlikely(test_bit(SCST_CMD_ABORTED,
2376                                               &cmd->cmd_flags))) {
2377                                 /* Necessary to allow aborting out of sn cmds */
2378                                 TRACE_MGMT_DBG("Aborting out of sn cmd %p "
2379                                         "(tag %llu, sn %lu)", cmd,
2380                                         (long long unsigned)cmd->tag, cmd->sn);
2381                                 tgt_dev->def_cmd_count--;
2382                                 scst_set_cmd_abnormal_done_state(cmd);
2383                                 res = SCST_CMD_STATE_RES_CONT_SAME;
2384                         } else {
2385                                 TRACE_SN("Deferring cmd %p (sn=%ld, set %d, "
2386                                         "expected_sn=%ld)", cmd, cmd->sn,
2387                                         cmd->sn_set, expected_sn);
2388                                 list_add_tail(&cmd->sn_cmd_list_entry,
2389                                               &tgt_dev->deferred_cmd_list);
2390                                 res = SCST_CMD_STATE_RES_CONT_NEXT;
2391                         }
2392                         spin_unlock_irq(&tgt_dev->sn_lock);
2393                         goto out;
2394                 } else {
2395                         TRACE_SN("Somebody incremented expected_sn %ld, "
2396                                 "continuing", expected_sn);
2397                         tgt_dev->def_cmd_count--;
2398                         spin_unlock_irq(&tgt_dev->sn_lock);
2399                 }
2400         }
2401
2402 exec:
2403         res = scst_exec(active_cmd);
2404
2405 out:
2406         TRACE_EXIT_HRES(res);
2407         return res;
2408 }
2409
2410 /* No locks supposed to be held */
2411 static int scst_check_sense(struct scst_cmd *cmd)
2412 {
2413         int res = 0;
2414         struct scst_device *dev = cmd->dev;
2415
2416         TRACE_ENTRY();
2417
2418         if (unlikely(cmd->ua_ignore))
2419                 goto out;
2420
2421         /* If we had internal bus reset behind us, set the command error UA */
2422         if ((dev->scsi_dev != NULL) &&
2423             unlikely(cmd->host_status == DID_RESET) &&
2424             scst_is_ua_command(cmd)) {
2425                 TRACE(TRACE_MGMT, "DID_RESET: was_reset=%d host_status=%x",
2426                       dev->scsi_dev->was_reset, cmd->host_status);
2427                 scst_set_cmd_error(cmd, SCST_LOAD_SENSE(scst_sense_reset_UA));
2428                 /* It looks like it is safe to clear was_reset here */
2429                 dev->scsi_dev->was_reset = 0;
2430         }
2431
2432         if (unlikely(cmd->status == SAM_STAT_CHECK_CONDITION) &&
2433             SCST_SENSE_VALID(cmd->sense)) {
2434                 PRINT_BUFF_FLAG(TRACE_SCSI, "Sense", cmd->sense,
2435                         cmd->sense_bufflen);
2436
2437                 /* Check Unit Attention Sense Key */
2438                 if (scst_is_ua_sense(cmd->sense)) {
2439                         if (scst_analyze_sense(cmd->sense, cmd->sense_bufflen,
2440                                         SCST_SENSE_ASC_VALID,
2441                                         0, SCST_SENSE_ASC_UA_RESET, 0)) {
2442                                 if (cmd->double_ua_possible) {
2443                                         TRACE(TRACE_MGMT_MINOR, "Double UA "
2444                                                 "detected for device %p", dev);
2445                                         TRACE(TRACE_MGMT_MINOR, "Retrying cmd"
2446                                                 " %p (tag %llu)", cmd,
2447                                                 (long long unsigned)cmd->tag);
2448
2449                                         cmd->status = 0;
2450                                         cmd->msg_status = 0;
2451                                         cmd->host_status = DID_OK;
2452                                         cmd->driver_status = 0;
2453
2454                                         mempool_free(cmd->sense,
2455                                                      scst_sense_mempool);
2456                                         cmd->sense = NULL;
2457
2458                                         scst_check_restore_sg_buff(cmd);
2459
2460                                         sBUG_ON(cmd->dbl_ua_orig_resp_data_len < 0);
2461                                         cmd->data_direction =
2462                                                 cmd->dbl_ua_orig_data_direction;
2463                                         cmd->resp_data_len =
2464                                                 cmd->dbl_ua_orig_resp_data_len;
2465
2466                                         cmd->state = SCST_CMD_STATE_REAL_EXEC;
2467                                         cmd->retry = 1;
2468                                         res = 1;
2469                                         goto out;
2470                                 }
2471                         }
2472                         scst_dev_check_set_UA(dev, cmd, cmd->sense,
2473                                 cmd->sense_bufflen);
2474                 }
2475         }
2476
2477         if (unlikely(cmd->double_ua_possible)) {
2478                 if (scst_is_ua_command(cmd)) {
2479                         TRACE_MGMT_DBG("Clearing dbl_ua_possible flag (dev %p, "
2480                                 "cmd %p)", dev, cmd);
2481                         /*
2482                          * Lock used to protect other flags in the bitfield
2483                          * (just in case, actually). Those flags can't be
2484                          * changed in parallel, because the device is
2485                          * serialized.
2486                          */
2487                         spin_lock_bh(&dev->dev_lock);
2488                         dev->dev_double_ua_possible = 0;
2489                         spin_unlock_bh(&dev->dev_lock);
2490                 }
2491         }
2492
2493 out:
2494         TRACE_EXIT_RES(res);
2495         return res;
2496 }
2497
2498 static int scst_check_auto_sense(struct scst_cmd *cmd)
2499 {
2500         int res = 0;
2501
2502         TRACE_ENTRY();
2503
2504         if (unlikely(cmd->status == SAM_STAT_CHECK_CONDITION) &&
2505             (!SCST_SENSE_VALID(cmd->sense) ||
2506              SCST_NO_SENSE(cmd->sense))) {
2507                 TRACE(TRACE_SCSI|TRACE_MINOR, "CHECK_CONDITION, but no sense: "
2508                       "cmd->status=%x, cmd->msg_status=%x, "
2509                       "cmd->host_status=%x, cmd->driver_status=%x (cmd %p)",
2510                       cmd->status, cmd->msg_status, cmd->host_status,
2511                       cmd->driver_status, cmd);
2512                 res = 1;
2513         } else if (unlikely(cmd->host_status)) {
2514                 if ((cmd->host_status == DID_REQUEUE) ||
2515                     (cmd->host_status == DID_IMM_RETRY) ||
2516                     (cmd->host_status == DID_SOFT_ERROR) ||
2517                     (cmd->host_status == DID_ABORT)) {
2518                         scst_set_busy(cmd);
2519                 } else {
2520                         TRACE(TRACE_SCSI|TRACE_MINOR, "Host status %x "
2521                                 "received, returning HARDWARE ERROR instead "
2522                                 "(cmd %p)", cmd->host_status, cmd);
2523                         scst_set_cmd_error(cmd,
2524                                 SCST_LOAD_SENSE(scst_sense_hardw_error));
2525                 }
2526         }
2527
2528         TRACE_EXIT_RES(res);
2529         return res;
2530 }
2531
2532 static int scst_pre_dev_done(struct scst_cmd *cmd)
2533 {
2534         int res = SCST_CMD_STATE_RES_CONT_SAME, rc;
2535
2536         TRACE_ENTRY();
2537
2538         if (unlikely(scst_check_auto_sense(cmd))) {
2539                 PRINT_INFO("Command finished with CHECK CONDITION, but "
2540                             "without sense data (opcode 0x%x), issuing "
2541                             "REQUEST SENSE", cmd->cdb[0]);
2542                 rc = scst_prepare_request_sense(cmd);
2543                 if (rc == 0)
2544                         res = SCST_CMD_STATE_RES_CONT_NEXT;
2545                 else {
2546                         PRINT_ERROR("%s", "Unable to issue REQUEST SENSE, "
2547                                     "returning HARDWARE ERROR");
2548                         scst_set_cmd_error(cmd,
2549                                 SCST_LOAD_SENSE(scst_sense_hardw_error));
2550                 }
2551                 goto out;
2552         } else if (unlikely(scst_check_sense(cmd)))
2553                 goto out;
2554
2555         if (likely(scsi_status_is_good(cmd->status))) {
2556                 unsigned char type = cmd->dev->type;
2557                 if (unlikely((cmd->cdb[0] == MODE_SENSE ||
2558                               cmd->cdb[0] == MODE_SENSE_10)) &&
2559                     cmd->tgt_dev->acg_dev->rd_only_flag &&
2560                     (type == TYPE_DISK ||
2561                      type == TYPE_WORM ||
2562                      type == TYPE_MOD ||
2563                      type == TYPE_TAPE)) {
2564                         int32_t length;
2565                         uint8_t *address;
2566                         bool err = false;
2567
2568                         length = scst_get_buf_first(cmd, &address);
2569                         if (length < 0) {
2570                                 PRINT_ERROR("%s", "Unable to get "
2571                                         "MODE_SENSE buffer");
2572                                 scst_set_cmd_error(cmd,
2573                                         SCST_LOAD_SENSE(
2574                                                 scst_sense_hardw_error));
2575                                 err = true;
2576                         } else if (length > 2 && cmd->cdb[0] == MODE_SENSE)
2577                                 address[2] |= 0x80;   /* Write Protect*/
2578                         else if (length > 3 && cmd->cdb[0] == MODE_SENSE_10)
2579                                 address[3] |= 0x80;   /* Write Protect*/
2580                         scst_put_buf(cmd, address);
2581
2582                         if (err)
2583                                 goto out;
2584                 }
2585
2586                 /*
2587                  * Check and clear NormACA option for the device, if necessary,
2588                  * since we don't support ACA
2589                  */
2590                 if (unlikely((cmd->cdb[0] == INQUIRY)) &&
2591                     /* Std INQUIRY data (no EVPD) */
2592                     !(cmd->cdb[1] & SCST_INQ_EVPD) &&
2593                     (cmd->resp_data_len > SCST_INQ_BYTE3)) {
2594                         uint8_t *buffer;
2595                         int buflen;
2596                         bool err = false;
2597
2598                         /* ToDo: all pages ?? */
2599                         buflen = scst_get_buf_first(cmd, &buffer);
2600                         if (buflen > SCST_INQ_BYTE3) {
2601 #ifdef CONFIG_SCST_EXTRACHECKS
2602                                 if (buffer[SCST_INQ_BYTE3] & SCST_INQ_NORMACA_BIT) {
2603                                         PRINT_INFO("NormACA set for device: "
2604                                             "lun=%lld, type 0x%02x. Clear it, "
2605                                             "since it's unsupported.",
2606                                             (long long unsigned int)cmd->lun,
2607                                             buffer[0]);
2608                                 }
2609 #endif
2610                                 buffer[SCST_INQ_BYTE3] &= ~SCST_INQ_NORMACA_BIT;
2611                         } else if (buflen != 0) {
2612                                 PRINT_ERROR("%s", "Unable to get INQUIRY "
2613                                     "buffer");
2614                                 scst_set_cmd_error(cmd,
2615                                        SCST_LOAD_SENSE(scst_sense_hardw_error));
2616                                 err = true;
2617                         }
2618                         if (buflen > 0)
2619                                 scst_put_buf(cmd, buffer);
2620
2621                         if (err)
2622                                 goto out;
2623                 }
2624
2625                 if (unlikely((cmd->cdb[0] == MODE_SELECT) ||
2626                     (cmd->cdb[0] == MODE_SELECT_10) ||
2627                     (cmd->cdb[0] == LOG_SELECT))) {
2628                         TRACE(TRACE_SCSI,
2629                                 "MODE/LOG SELECT succeeded (LUN %lld)",
2630                                 (long long unsigned int)cmd->lun);
2631                         cmd->state = SCST_CMD_STATE_MODE_SELECT_CHECKS;
2632                         goto out;
2633                 }
2634         } else {
2635                 if ((cmd->cdb[0] == RESERVE) || (cmd->cdb[0] == RESERVE_10)) {
2636                         if (!test_bit(SCST_TGT_DEV_RESERVED,
2637                                         &cmd->tgt_dev->tgt_dev_flags)) {
2638                                 struct scst_tgt_dev *tgt_dev_tmp;
2639                                 struct scst_device *dev = cmd->dev;
2640
2641                                 TRACE(TRACE_SCSI,
2642                                         "Real RESERVE failed lun=%lld, "
2643                                         "status=%x",
2644                                         (long long unsigned int)cmd->lun,
2645                                         cmd->status);
2646                                 PRINT_BUFF_FLAG(TRACE_SCSI, "Sense", cmd->sense,
2647                                         cmd->sense_bufflen);
2648
2649                                 /* Clearing the reservation */
2650                                 spin_lock_bh(&dev->dev_lock);
2651                                 list_for_each_entry(tgt_dev_tmp,
2652                                                     &dev->dev_tgt_dev_list,
2653                                                     dev_tgt_dev_list_entry) {
2654                                         clear_bit(SCST_TGT_DEV_RESERVED,
2655                                                 &tgt_dev_tmp->tgt_dev_flags);
2656                                 }
2657                                 dev->dev_reserved = 0;
2658                                 spin_unlock_bh(&dev->dev_lock);
2659                         }
2660                 }
2661
2662                 /* Check for MODE PARAMETERS CHANGED UA */
2663                 if ((cmd->dev->scsi_dev != NULL) &&
2664                     (cmd->status == SAM_STAT_CHECK_CONDITION) &&
2665                     SCST_SENSE_VALID(cmd->sense) &&
2666                     scst_is_ua_sense(cmd->sense) &&
2667                     scst_analyze_sense(cmd->sense, cmd->sense_bufflen,
2668                                         SCST_SENSE_ASCx_VALID,
2669                                         0, 0x2a, 0x01)) {
2670                         TRACE(TRACE_SCSI, "MODE PARAMETERS CHANGED UA (lun "
2671                                 "%lld)", (long long unsigned int)cmd->lun);
2672                         cmd->state = SCST_CMD_STATE_MODE_SELECT_CHECKS;
2673                         goto out;
2674                 }
2675         }
2676
2677         cmd->state = SCST_CMD_STATE_DEV_DONE;
2678
2679 out:
2680         TRACE_EXIT_RES(res);
2681         return res;
2682 }
2683
2684 static int scst_mode_select_checks(struct scst_cmd *cmd)
2685 {
2686         int res = SCST_CMD_STATE_RES_CONT_SAME;
2687         int atomic = scst_cmd_atomic(cmd);
2688
2689         TRACE_ENTRY();
2690
2691         if (likely(scsi_status_is_good(cmd->status))) {
2692                 if (unlikely((cmd->cdb[0] == MODE_SELECT) ||
2693                     (cmd->cdb[0] == MODE_SELECT_10) ||
2694                     (cmd->cdb[0] == LOG_SELECT))) {
2695                         struct scst_device *dev = cmd->dev;
2696                         uint8_t sense_buffer[SCST_STANDARD_SENSE_LEN];
2697
2698                         if (atomic && (dev->scsi_dev != NULL)) {
2699                                 TRACE_DBG("%s", "MODE/LOG SELECT: thread "
2700                                         "context required");
2701                                 res = SCST_CMD_STATE_RES_NEED_THREAD;
2702                                 goto out;
2703                         }
2704
2705                         TRACE(TRACE_SCSI, "MODE/LOG SELECT succeeded, "
2706                                 "setting the SELECT UA (lun=%lld)",
2707                                 (long long unsigned int)cmd->lun);
2708
2709                         spin_lock_bh(&dev->dev_lock);
2710                         if (cmd->cdb[0] == LOG_SELECT) {
2711                                 scst_set_sense(sense_buffer,
2712                                         sizeof(sense_buffer),
2713                                         dev->d_sense,
2714                                         UNIT_ATTENTION, 0x2a, 0x02);
2715                         } else {
2716                                 scst_set_sense(sense_buffer,
2717                                         sizeof(sense_buffer),
2718                                         dev->d_sense,
2719                                         UNIT_ATTENTION, 0x2a, 0x01);
2720                         }
2721                         scst_dev_check_set_local_UA(dev, cmd, sense_buffer,
2722                                 sizeof(sense_buffer));
2723                         spin_unlock_bh(&dev->dev_lock);
2724
2725                         if (dev->scsi_dev != NULL)
2726                                 scst_obtain_device_parameters(dev);
2727                 }
2728         } else if ((cmd->status == SAM_STAT_CHECK_CONDITION) &&
2729                     SCST_SENSE_VALID(cmd->sense) &&
2730                     scst_is_ua_sense(cmd->sense) &&
2731                      /* mode parameters changed */
2732                     (scst_analyze_sense(cmd->sense, cmd->sense_bufflen,
2733                                         SCST_SENSE_ASCx_VALID,
2734                                         0, 0x2a, 0x01) ||
2735                      scst_analyze_sense(cmd->sense, cmd->sense_bufflen,
2736                                         SCST_SENSE_ASC_VALID,
2737                                         0, 0x29, 0) /* reset */ ||
2738                      scst_analyze_sense(cmd->sense, cmd->sense_bufflen,
2739                                         SCST_SENSE_ASC_VALID,
2740                                         0, 0x28, 0) /* medium changed */ ||
2741                      /* cleared by another ini (just in case) */
2742                      scst_analyze_sense(cmd->sense, cmd->sense_bufflen,
2743                                         SCST_SENSE_ASC_VALID,
2744                                         0, 0x2F, 0))) {
2745                 if (atomic) {
2746                         TRACE_DBG("Possible parameters changed UA %x: "
2747                                 "thread context required", cmd->sense[12]);
2748                         res = SCST_CMD_STATE_RES_NEED_THREAD;
2749                         goto out;
2750                 }
2751
2752                 TRACE(TRACE_SCSI, "Possible parameters changed UA %x "
2753                         "(LUN %lld): getting new parameters", cmd->sense[12],
2754                         (long long unsigned int)cmd->lun);
2755
2756                 scst_obtain_device_parameters(cmd->dev);
2757         } else
2758                 sBUG();
2759
2760         cmd->state = SCST_CMD_STATE_DEV_DONE;
2761
2762 out:
2763         TRACE_EXIT_HRES(res);
2764         return res;
2765 }
2766
2767 static void scst_inc_check_expected_sn(struct scst_cmd *cmd)
2768 {
2769         if (likely(cmd->sn_set))
2770                 scst_inc_expected_sn(cmd->tgt_dev, cmd->sn_slot);
2771
2772         scst_make_deferred_commands_active(cmd->tgt_dev);
2773 }
2774
2775 static int scst_dev_done(struct scst_cmd *cmd)
2776 {
2777         int res = SCST_CMD_STATE_RES_CONT_SAME;
2778         int state;
2779         struct scst_device *dev = cmd->dev;
2780
2781         TRACE_ENTRY();
2782
2783         state = SCST_CMD_STATE_PRE_XMIT_RESP;
2784
2785         if (likely(!scst_is_cmd_local(cmd)) &&
2786             likely(dev->handler->dev_done != NULL)) {
2787                 int rc;
2788
2789                 if (unlikely(!dev->handler->dev_done_atomic &&
2790                              scst_cmd_atomic(cmd))) {
2791                         /*
2792                          * It shouldn't be because of SCST_TGT_DEV_AFTER_*
2793                          * optimization.
2794                          */
2795                         TRACE_DBG("Dev handler %s dev_done() needs thread "
2796                               "context, rescheduling", dev->handler->name);
2797                         res = SCST_CMD_STATE_RES_NEED_THREAD;
2798                         goto out;
2799                 }
2800
2801                 TRACE_DBG("Calling dev handler %s dev_done(%p)",
2802                       dev->handler->name, cmd);
2803                 rc = dev->handler->dev_done(cmd);
2804                 TRACE_DBG("Dev handler %s dev_done() returned %d",
2805                       dev->handler->name, rc);
2806                 if (rc != SCST_CMD_STATE_DEFAULT)
2807                         state = rc;
2808         }
2809
2810         switch (state) {
2811         case SCST_CMD_STATE_PRE_XMIT_RESP:
2812         case SCST_CMD_STATE_DEV_PARSE:
2813         case SCST_CMD_STATE_PRE_PARSE:
2814         case SCST_CMD_STATE_PREPARE_SPACE:
2815         case SCST_CMD_STATE_RDY_TO_XFER:
2816         case SCST_CMD_STATE_TGT_PRE_EXEC:
2817         case SCST_CMD_STATE_SEND_FOR_EXEC:
2818         case SCST_CMD_STATE_LOCAL_EXEC:
2819         case SCST_CMD_STATE_REAL_EXEC:
2820         case SCST_CMD_STATE_PRE_DEV_DONE:
2821         case SCST_CMD_STATE_MODE_SELECT_CHECKS:
2822         case SCST_CMD_STATE_DEV_DONE:
2823         case SCST_CMD_STATE_XMIT_RESP:
2824         case SCST_CMD_STATE_FINISHED:
2825         case SCST_CMD_STATE_FINISHED_INTERNAL:
2826                 cmd->state = state;
2827                 break;
2828
2829         case SCST_CMD_STATE_NEED_THREAD_CTX:
2830                 TRACE_DBG("Dev handler %s dev_done() requested "
2831                       "thread context, rescheduling",
2832                       dev->handler->name);
2833                 res = SCST_CMD_STATE_RES_NEED_THREAD;
2834                 break;
2835
2836         default:
2837                 if (state >= 0) {
2838                         PRINT_ERROR("Dev handler %s dev_done() returned "
2839                                 "invalid cmd state %d",
2840                                 dev->handler->name, state);
2841                 } else {
2842                         PRINT_ERROR("Dev handler %s dev_done() returned "
2843                                 "error %d", dev->handler->name,
2844                                 state);
2845                 }
2846                 scst_set_cmd_error(cmd,
2847                            SCST_LOAD_SENSE(scst_sense_hardw_error));
2848                 scst_set_cmd_abnormal_done_state(cmd);
2849                 break;
2850         }
2851
2852         if (cmd->needs_unblocking)
2853                 scst_unblock_dev_cmd(cmd);
2854
2855         if (likely(cmd->dec_on_dev_needed))
2856                 scst_dec_on_dev_cmd(cmd);
2857
2858         if (cmd->inc_expected_sn_on_done && cmd->sent_for_exec)
2859                 scst_inc_check_expected_sn(cmd);
2860
2861         if (unlikely(cmd->internal))
2862                 cmd->state = SCST_CMD_STATE_FINISHED_INTERNAL;
2863
2864 out:
2865         TRACE_EXIT_HRES(res);
2866         return res;
2867 }
2868
2869 static int scst_pre_xmit_response(struct scst_cmd *cmd)
2870 {
2871         int res;
2872
2873         TRACE_ENTRY();
2874
2875         EXTRACHECKS_BUG_ON(cmd->internal);
2876
2877 #ifdef CONFIG_SCST_DEBUG_TM
2878         if (cmd->tm_dbg_delayed &&
2879                         !test_bit(SCST_CMD_ABORTED, &cmd->cmd_flags)) {
2880                 if (scst_cmd_atomic(cmd)) {
2881                         TRACE_MGMT_DBG("%s",
2882                                 "DEBUG_TM delayed cmd needs a thread");
2883                         res = SCST_CMD_STATE_RES_NEED_THREAD;
2884                         return res;
2885                 }
2886                 TRACE_MGMT_DBG("Delaying cmd %p (tag %llu) for 1 second",
2887                         cmd, cmd->tag);
2888                 schedule_timeout_uninterruptible(HZ);
2889         }
2890 #endif
2891
2892         if (likely(cmd->tgt_dev != NULL)) {
2893                 atomic_dec(&cmd->tgt_dev->tgt_dev_cmd_count);
2894                 atomic_dec(&cmd->dev->dev_cmd_count);
2895                 /* If expected values not set, expected direction is UNKNOWN */
2896                 if (cmd->expected_data_direction & SCST_DATA_WRITE)
2897                         atomic_dec(&cmd->dev->write_cmd_count);
2898
2899                 if (unlikely(cmd->queue_type == SCST_CMD_QUEUE_HEAD_OF_QUEUE))
2900                         scst_on_hq_cmd_response(cmd);
2901
2902                 if (unlikely(!cmd->sent_for_exec)) {
2903                         TRACE_SN("cmd %p was not sent to mid-lev"
2904                                 " (sn %ld, set %d)",
2905                                 cmd, cmd->sn, cmd->sn_set);
2906                         scst_unblock_deferred(cmd->tgt_dev, cmd);
2907                         cmd->sent_for_exec = 1;
2908                 }
2909         }
2910
2911         /*
2912          * If we don't remove cmd from the search list here, before
2913          * submitting it for transmittion, we will have a race, when for
2914          * some reason cmd's release is delayed after transmittion and
2915          * initiator sends cmd with the same tag => it is possible that
2916          * a wrong cmd will be found by find() functions.
2917          */
2918         spin_lock_irq(&cmd->sess->sess_list_lock);
2919         list_del(&cmd->search_cmd_list_entry);
2920         spin_unlock_irq(&cmd->sess->sess_list_lock);
2921
2922         cmd->done = 1;
2923         smp_mb(); /* to sync with scst_abort_cmd() */
2924
2925         if (unlikely(test_bit(SCST_CMD_ABORTED, &cmd->cmd_flags)))
2926                 scst_xmit_process_aborted_cmd(cmd);
2927         else if (unlikely(cmd->status == SAM_STAT_CHECK_CONDITION))
2928                 scst_store_sense(cmd);
2929
2930         if (unlikely(test_bit(SCST_CMD_NO_RESP, &cmd->cmd_flags))) {
2931                 TRACE_MGMT_DBG("Flag NO_RESP set for cmd %p (tag %llu),"
2932                                 " skipping",
2933                                 cmd, (long long unsigned int)cmd->tag);
2934                 cmd->state = SCST_CMD_STATE_FINISHED;
2935                 res = SCST_CMD_STATE_RES_CONT_SAME;
2936                 goto out;
2937         }
2938
2939         if (cmd->tgt_data_buf_alloced && cmd->dh_data_buf_alloced &&
2940             (cmd->data_direction & SCST_DATA_READ))
2941                 scst_copy_sg(cmd, SCST_SG_COPY_TO_TARGET);
2942
2943         cmd->state = SCST_CMD_STATE_XMIT_RESP;
2944         res = SCST_CMD_STATE_RES_CONT_SAME;
2945
2946 out:
2947 #ifdef CONFIG_SCST_MEASURE_LATENCY
2948         {
2949                 struct timespec ts;
2950                 uint64_t finish, scst_time, proc_time;
2951                 struct scst_session *sess = cmd->sess;
2952
2953                 getnstimeofday(&ts);
2954                 finish = scst_sec_to_nsec(ts.tv_sec) + ts.tv_nsec;
2955
2956                 spin_lock_bh(&sess->meas_lock);
2957
2958                 scst_time = cmd->pre_exec_finish - cmd->start;
2959                 scst_time += finish - cmd->post_exec_start;
2960                 proc_time = finish - cmd->start;
2961
2962                 sess->scst_time += scst_time;
2963                 sess->processing_time += proc_time;
2964                 sess->processed_cmds++;
2965
2966                 spin_unlock_bh(&sess->meas_lock);
2967
2968                 TRACE_DBG("cmd %p (sess %p): finish %lld (tv_sec %ld, "
2969                         "tv_nsec %ld), scst_time %lld, proc_time %lld",
2970                         cmd, sess, finish, ts.tv_sec, ts.tv_nsec, scst_time,
2971                         proc_time);
2972         }
2973 #endif
2974         TRACE_EXIT_HRES(res);
2975         return res;
2976 }
2977
2978 static int scst_xmit_response(struct scst_cmd *cmd)
2979 {
2980         int res, rc;
2981
2982         TRACE_ENTRY();
2983
2984         EXTRACHECKS_BUG_ON(cmd->internal);
2985
2986         if (unlikely(!cmd->tgtt->xmit_response_atomic &&
2987                      scst_cmd_atomic(cmd))) {
2988                 /*
2989                  * It shouldn't be because of SCST_TGT_DEV_AFTER_*
2990                  * optimization.
2991                  */
2992                 TRACE_DBG("Target driver %s xmit_response() needs thread "
2993                               "context, rescheduling", cmd->tgtt->name);
2994                 res = SCST_CMD_STATE_RES_NEED_THREAD;
2995                 goto out;
2996         }
2997
2998         while (1) {
2999                 int finished_cmds = atomic_read(&cmd->sess->tgt->finished_cmds);
3000
3001                 res = SCST_CMD_STATE_RES_CONT_NEXT;
3002                 cmd->state = SCST_CMD_STATE_XMIT_WAIT;
3003
3004                 TRACE_DBG("Calling xmit_response(%p)", cmd);
3005
3006 #if defined(CONFIG_SCST_DEBUG) || defined(CONFIG_SCST_TRACING)
3007                 if (trace_flag & TRACE_SND_BOT) {
3008                         int i;
3009                         struct scatterlist *sg;
3010                         if (cmd->tgt_sg != NULL)
3011                                 sg = cmd->tgt_sg;
3012                         else
3013                                 sg = cmd->sg;
3014                         if (sg != NULL) {
3015                                 TRACE(TRACE_SND_BOT, "Xmitting data for cmd %p "
3016                                         "(sg_cnt %d, sg %p, sg[0].page %p)",
3017                                         cmd, cmd->tgt_sg_cnt, sg,
3018                                         (void *)sg_page(&sg[0]));
3019                                 for (i = 0; i < cmd->tgt_sg_cnt; ++i) {
3020                                         PRINT_BUFF_FLAG(TRACE_SND_BOT,
3021                                                 "Xmitting sg", sg_virt(&sg[i]),
3022                                                 sg[i].length);
3023                                 }
3024                         }
3025                 }
3026 #endif
3027
3028 #ifdef CONFIG_SCST_DEBUG_RETRY
3029                 if (((scst_random() % 100) == 77))
3030                         rc = SCST_TGT_RES_QUEUE_FULL;
3031                 else
3032 #endif
3033                         rc = cmd->tgtt->xmit_response(cmd);
3034                 TRACE_DBG("xmit_response() returned %d", rc);
3035
3036                 if (likely(rc == SCST_TGT_RES_SUCCESS))
3037                         goto out;
3038
3039                 /* Restore the previous state */
3040                 cmd->state = SCST_CMD_STATE_XMIT_RESP;
3041
3042                 switch (rc) {
3043                 case SCST_TGT_RES_QUEUE_FULL:
3044                         if (scst_queue_retry_cmd(cmd, finished_cmds) == 0)
3045                                 break;
3046                         else
3047                                 continue;
3048
3049                 case SCST_TGT_RES_NEED_THREAD_CTX:
3050                         TRACE_DBG("Target driver %s xmit_response() "
3051                               "requested thread context, rescheduling",
3052                               cmd->tgtt->name);
3053                         res = SCST_CMD_STATE_RES_NEED_THREAD;
3054                         break;
3055
3056                 default:
3057                         goto out_error;
3058                 }
3059                 break;
3060         }
3061
3062 out:
3063         /* Caution: cmd can be already dead here */
3064         TRACE_EXIT_HRES(res);
3065         return res;
3066
3067 out_error:
3068         if (rc == SCST_TGT_RES_FATAL_ERROR) {
3069                 PRINT_ERROR("Target driver %s xmit_response() returned "
3070                         "fatal error", cmd->tgtt->name);
3071         } else {
3072                 PRINT_ERROR("Target driver %s xmit_response() returned "
3073                         "invalid value %d", cmd->tgtt->name, rc);
3074         }
3075         scst_set_cmd_error(cmd, SCST_LOAD_SENSE(scst_sense_hardw_error));
3076         cmd->state = SCST_CMD_STATE_FINISHED;
3077         res = SCST_CMD_STATE_RES_CONT_SAME;
3078         goto out;
3079 }
3080
3081 void scst_tgt_cmd_done(struct scst_cmd *cmd,
3082         enum scst_exec_context pref_context)
3083 {
3084         TRACE_ENTRY();
3085
3086         sBUG_ON(cmd->state != SCST_CMD_STATE_XMIT_WAIT);
3087
3088         cmd->state = SCST_CMD_STATE_FINISHED;
3089         scst_proccess_redirect_cmd(cmd, pref_context, 1);
3090
3091         TRACE_EXIT();
3092         return;
3093 }
3094 EXPORT_SYMBOL(scst_tgt_cmd_done);
3095
3096 static int scst_finish_cmd(struct scst_cmd *cmd)
3097 {
3098         int res;
3099
3100         TRACE_ENTRY();
3101
3102         atomic_dec(&cmd->sess->sess_cmd_count);
3103
3104         cmd->finished = 1;
3105         smp_mb(); /* to sync with scst_abort_cmd() */
3106
3107         if (unlikely(test_bit(SCST_CMD_ABORTED, &cmd->cmd_flags))) {
3108                 TRACE_MGMT_DBG("Aborted cmd %p finished (cmd_ref %d, "
3109                         "scst_cmd_count %d)", cmd, atomic_read(&cmd->cmd_ref),
3110                         atomic_read(&scst_cmd_count));
3111
3112                 scst_finish_cmd_mgmt(cmd);
3113         }
3114
3115         if (unlikely(cmd->delivery_status != SCST_CMD_DELIVERY_SUCCESS)) {
3116                 if ((cmd->tgt_dev != NULL) &&
3117                     scst_is_ua_sense(cmd->sense)) {
3118                         /* This UA delivery failed, so requeue it */
3119                         TRACE_MGMT_DBG("Requeuing UA for delivery failed cmd "
3120                                 "%p", cmd);
3121                         scst_check_set_UA(cmd->tgt_dev, cmd->sense,
3122                                 cmd->sense_bufflen, SCST_SET_UA_FLAG_AT_HEAD);
3123                 }
3124         }
3125
3126         __scst_cmd_put(cmd);
3127
3128         res = SCST_CMD_STATE_RES_CONT_NEXT;
3129
3130         TRACE_EXIT_HRES(res);
3131         return res;
3132 }
3133
3134 /*
3135  * No locks, but it must be externally serialized (see comment for
3136  * scst_cmd_init_done() in scst.h)
3137  */
3138 static void scst_cmd_set_sn(struct scst_cmd *cmd)
3139 {
3140         struct scst_tgt_dev *tgt_dev = cmd->tgt_dev;
3141         unsigned long flags;
3142
3143         TRACE_ENTRY();
3144
3145         if (scst_is_implicit_hq(cmd)) {
3146                 TRACE_SN("Implicit HQ cmd %p", cmd);
3147                 cmd->queue_type = SCST_CMD_QUEUE_HEAD_OF_QUEUE;
3148         }
3149
3150         EXTRACHECKS_BUG_ON(cmd->sn_set || cmd->hq_cmd_inced);
3151
3152         /* Optimized for lockless fast path */
3153
3154         scst_check_debug_sn(cmd);
3155
3156         if (cmd->dev->queue_alg ==
3157                         SCST_CONTR_MODE_QUEUE_ALG_RESTRICTED_REORDER) {
3158                 /*
3159                  * Not the best way, but well enough until there will be a
3160                  * possibility to specify queue type during pass-through
3161                  * commands submission.
3162                  */
3163                 cmd->queue_type = SCST_CMD_QUEUE_ORDERED;
3164         }
3165
3166         switch (cmd->queue_type) {
3167         case SCST_CMD_QUEUE_SIMPLE:
3168         case SCST_CMD_QUEUE_UNTAGGED:
3169 #if 0 /* left for future performance investigations */
3170                 if (scst_cmd_is_expected_set(cmd)) {
3171                         if ((cmd->expected_data_direction == SCST_DATA_READ) &&
3172                             (atomic_read(&cmd->dev->write_cmd_count) == 0))
3173                                 goto ordered;
3174                 } else
3175                         goto ordered;
3176 #endif
3177                 if (likely(tgt_dev->num_free_sn_slots >= 0)) {
3178                         /*
3179                          * atomic_inc_return() implies memory barrier to sync
3180                          * with scst_inc_expected_sn()
3181                          */
3182                         if (atomic_inc_return(tgt_dev->cur_sn_slot) == 1) {
3183                                 tgt_dev->curr_sn++;
3184                                 TRACE_SN("Incremented curr_sn %ld",
3185                                         tgt_dev->curr_sn);
3186                         }
3187                         cmd->sn_slot = tgt_dev->cur_sn_slot;
3188                         cmd->sn = tgt_dev->curr_sn;
3189
3190                         tgt_dev->prev_cmd_ordered = 0;
3191                 } else {
3192                         TRACE(TRACE_MINOR, "***WARNING*** Not enough SN slots "
3193                                 "%zd", ARRAY_SIZE(tgt_dev->sn_slots));
3194                         goto ordered;
3195                 }
3196                 break;
3197
3198         case SCST_CMD_QUEUE_ORDERED:
3199                 TRACE_SN("ORDERED cmd %p (op %x)", cmd, cmd->cdb[0]);
3200 ordered:
3201                 if (!tgt_dev->prev_cmd_ordered) {
3202                         spin_lock_irqsave(&tgt_dev->sn_lock, flags);
3203                         if (tgt_dev->num_free_sn_slots >= 0) {
3204                                 tgt_dev->num_free_sn_slots--;
3205                                 if (tgt_dev->num_free_sn_slots >= 0) {
3206                                         int i = 0;
3207                                         /* Commands can finish in any order, so
3208                                          * we don't know which slot is empty.
3209                                          */
3210                                         while (1) {
3211                                                 tgt_dev->cur_sn_slot++;
3212                                                 if (tgt_dev->cur_sn_slot ==
3213                                                       tgt_dev->sn_slots + ARRAY_SIZE(tgt_dev->sn_slots))
3214                                                         tgt_dev->cur_sn_slot = tgt_dev->sn_slots;
3215
3216                                                 if (atomic_read(tgt_dev->cur_sn_slot) == 0)
3217                                                         break;
3218
3219                                                 i++;
3220                                                 sBUG_ON(i == ARRAY_SIZE(tgt_dev->sn_slots));
3221                                         }
3222                                         TRACE_SN("New cur SN slot %zd",
3223                                                 tgt_dev->cur_sn_slot -
3224                                                 tgt_dev->sn_slots);
3225                                 }
3226                         }
3227                         spin_unlock_irqrestore(&tgt_dev->sn_lock, flags);
3228                 }
3229                 tgt_dev->prev_cmd_ordered = 1;
3230                 tgt_dev->curr_sn++;
3231                 cmd->sn = tgt_dev->curr_sn;
3232                 break;
3233
3234         case SCST_CMD_QUEUE_HEAD_OF_QUEUE:
3235                 TRACE_SN("HQ cmd %p (op %x)", cmd, cmd->cdb[0]);
3236                 spin_lock_irqsave(&tgt_dev->sn_lock, flags);
3237                 tgt_dev->hq_cmd_count++;
3238                 spin_unlock_irqrestore(&tgt_dev->sn_lock, flags);
3239                 cmd->hq_cmd_inced = 1;
3240                 goto out;
3241
3242         default:
3243                 sBUG();
3244         }
3245
3246         TRACE_SN("cmd(%p)->sn: %ld (tgt_dev %p, *cur_sn_slot %d, "
3247                 "num_free_sn_slots %d, prev_cmd_ordered %ld, "
3248                 "cur_sn_slot %zd)", cmd, cmd->sn, tgt_dev,
3249                 atomic_read(tgt_dev->cur_sn_slot),
3250                 tgt_dev->num_free_sn_slots, tgt_dev->prev_cmd_ordered,
3251                 tgt_dev->cur_sn_slot-tgt_dev->sn_slots);
3252
3253         cmd->sn_set = 1;
3254
3255 out:
3256         TRACE_EXIT();
3257         return;
3258 }
3259
3260 /*
3261  * Returns 0 on success, > 0 when we need to wait for unblock,
3262  * < 0 if there is no device (lun) or device type handler.
3263  *
3264  * No locks, but might be on IRQ, protection is done by the
3265  * suspended activity.
3266  */
3267 static int scst_translate_lun(struct scst_cmd *cmd)
3268 {
3269         struct scst_tgt_dev *tgt_dev = NULL;
3270         int res;
3271
3272         TRACE_ENTRY();
3273
3274         /* See comment about smp_mb() in scst_suspend_activity() */
3275         __scst_get(1);
3276
3277         if (likely(!test_bit(SCST_FLAG_SUSPENDED, &scst_flags))) {
3278                 struct list_head *sess_tgt_dev_list_head =
3279                         &cmd->sess->sess_tgt_dev_list_hash[HASH_VAL(cmd->lun)];
3280                 TRACE_DBG("Finding tgt_dev for cmd %p (lun %lld)", cmd,
3281                         (long long unsigned int)cmd->lun);
3282                 res = -1;
3283                 list_for_each_entry(tgt_dev, sess_tgt_dev_list_head,
3284                                 sess_tgt_dev_list_entry) {
3285                         if (tgt_dev->lun == cmd->lun) {
3286                                 TRACE_DBG("tgt_dev %p found", tgt_dev);
3287
3288                                 if (unlikely(tgt_dev->dev->handler ==
3289                                                 &scst_null_devtype)) {
3290                                         PRINT_INFO("Dev handler for device "
3291                                           "%lld is NULL, the device will not "
3292                                           "be visible remotely",
3293                                            (long long unsigned int)cmd->lun);
3294                                         break;
3295                                 }
3296
3297                                 cmd->cmd_lists = tgt_dev->dev->p_cmd_lists;
3298                                 cmd->tgt_dev = tgt_dev;
3299                                 cmd->dev = tgt_dev->dev;
3300
3301                                 res = 0;
3302                                 break;
3303                         }
3304                 }
3305                 if (res != 0) {
3306                         TRACE(TRACE_MINOR,
3307                                 "tgt_dev for LUN %lld not found, command to "
3308                                 "unexisting LU?",
3309                                 (long long unsigned int)cmd->lun);
3310                         __scst_put();
3311                 }
3312         } else {
3313                 TRACE_MGMT_DBG("%s", "FLAG SUSPENDED set, skipping");
3314                 __scst_put();
3315                 res = 1;
3316         }
3317
3318         TRACE_EXIT_RES(res);
3319         return res;
3320 }
3321
3322 /*
3323  * No locks, but might be on IRQ
3324  *
3325  * Returns 0 on success, > 0 when we need to wait for unblock,
3326  * < 0 if there is no device (lun) or device type handler.
3327  */
3328 static int __scst_init_cmd(struct scst_cmd *cmd)
3329 {
3330         int res = 0;
3331
3332         TRACE_ENTRY();
3333
3334         res = scst_translate_lun(cmd);
3335         if (likely(res == 0)) {
3336                 int cnt;
3337                 bool failure = false;
3338
3339                 cmd->state = SCST_CMD_STATE_PRE_PARSE;
3340
3341                 cnt = atomic_inc_return(&cmd->tgt_dev->tgt_dev_cmd_count);
3342                 if (unlikely(cnt > SCST_MAX_TGT_DEV_COMMANDS)) {
3343                         TRACE(TRACE_MGMT_MINOR,
3344                                 "Too many pending commands (%d) in "
3345                                 "session, returning BUSY to initiator \"%s\"",
3346                                 cnt, (cmd->sess->initiator_name[0] == '\0') ?
3347                                   "Anonymous" : cmd->sess->initiator_name);
3348                         failure = true;
3349                 }
3350
3351                 cnt = atomic_inc_return(&cmd->dev->dev_cmd_count);
3352                 if (unlikely(cnt > SCST_MAX_DEV_COMMANDS)) {
3353                         if (!failure) {
3354                                 TRACE(TRACE_MGMT_MINOR,
3355                                         "Too many pending device "
3356                                         "commands (%d), returning BUSY to "
3357                                         "initiator \"%s\"", cnt,
3358                                         (cmd->sess->initiator_name[0] == '\0') ?
3359                                                 "Anonymous" :
3360                                                 cmd->sess->initiator_name);
3361                                 failure = true;
3362                         }
3363                 }
3364
3365                 /* If expected values not set, expected direction is UNKNOWN */
3366                 if (cmd->expected_data_direction & SCST_DATA_WRITE)
3367                         atomic_inc(&cmd->dev->write_cmd_count);
3368
3369                 if (unlikely(failure))
3370                         goto out_busy;
3371
3372                 if (!cmd->set_sn_on_restart_cmd)
3373                         scst_cmd_set_sn(cmd);
3374         } else if (res < 0) {
3375                 TRACE_DBG("Finishing cmd %p", cmd);
3376                 scst_set_cmd_error(cmd,
3377                            SCST_LOAD_SENSE(scst_sense_lun_not_supported));
3378                 scst_set_cmd_abnormal_done_state(cmd);
3379         } else
3380                 goto out;
3381
3382 out:
3383         TRACE_EXIT_RES(res);
3384         return res;
3385
3386 out_busy:
3387         scst_set_busy(cmd);
3388         scst_set_cmd_abnormal_done_state(cmd);
3389         goto out;
3390 }
3391
3392 /* Called under scst_init_lock and IRQs disabled */
3393 static void scst_do_job_init(void)
3394         __releases(&scst_init_lock)
3395         __acquires(&scst_init_lock)
3396 {
3397         struct scst_cmd *cmd;
3398         int susp;
3399
3400         TRACE_ENTRY();
3401
3402 restart:
3403         /*
3404          * There is no need for read barrier here, because we don't care where
3405          * this check will be done.
3406          */
3407         susp = test_bit(SCST_FLAG_SUSPENDED, &scst_flags);
3408         if (scst_init_poll_cnt > 0)
3409                 scst_init_poll_cnt--;
3410
3411         list_for_each_entry(cmd, &scst_init_cmd_list, cmd_list_entry) {
3412                 int rc;
3413                 if (susp && !test_bit(SCST_CMD_ABORTED, &cmd->cmd_flags))
3414                         continue;
3415                 if (!test_bit(SCST_CMD_ABORTED, &cmd->cmd_flags)) {
3416                         spin_unlock_irq(&scst_init_lock);
3417                         rc = __scst_init_cmd(cmd);
3418                         spin_lock_irq(&scst_init_lock);
3419                         if (rc > 0) {
3420                                 TRACE_MGMT_DBG("%s",
3421                                         "FLAG SUSPENDED set, restarting");
3422                                 goto restart;
3423                         }
3424                 } else {
3425                         TRACE_MGMT_DBG("Aborting not inited cmd %p (tag %llu)",
3426                                        cmd, (long long unsigned int)cmd->tag);
3427                         scst_set_cmd_abnormal_done_state(cmd);
3428                 }
3429
3430                 /*
3431                  * Deleting cmd from init cmd list after __scst_init_cmd()
3432                  * is necessary to keep the check in scst_init_cmd() correct
3433                  * to preserve the commands order.
3434                  *
3435                  * We don't care about the race, when init cmd list is empty
3436                  * and one command detected that it just was not empty, so
3437                  * it's inserting to it, but another command at the same time
3438                  * seeing init cmd list empty and goes directly, because it
3439                  * could affect only commands from the same initiator to the
3440                  * same tgt_dev, but scst_cmd_init_done*() doesn't guarantee
3441                  * the order in case of simultaneous such calls anyway.
3442                  */
3443                 TRACE_MGMT_DBG("Deleting cmd %p from init cmd list", cmd);
3444                 smp_wmb(); /* enforce the required order */
3445                 list_del(&cmd->cmd_list_entry);
3446                 spin_unlock(&scst_init_lock);
3447
3448                 spin_lock(&cmd->cmd_lists->cmd_list_lock);
3449                 TRACE_MGMT_DBG("Adding cmd %p to active cmd list", cmd);
3450                 if (unlikely(cmd->queue_type == SCST_CMD_QUEUE_HEAD_OF_QUEUE))
3451                         list_add(&cmd->cmd_list_entry,
3452                                 &cmd->cmd_lists->active_cmd_list);
3453                 else
3454                         list_add_tail(&cmd->cmd_list_entry,
3455                                 &cmd->cmd_lists->active_cmd_list);
3456                 wake_up(&cmd->cmd_lists->cmd_list_waitQ);
3457                 spin_unlock(&cmd->cmd_lists->cmd_list_lock);
3458
3459                 spin_lock(&scst_init_lock);
3460                 goto restart;
3461         }
3462
3463         /* It isn't really needed, but let's keep it */
3464         if (susp != test_bit(SCST_FLAG_SUSPENDED, &scst_flags))
3465                 goto restart;
3466
3467         TRACE_EXIT();
3468         return;
3469 }
3470
3471 static inline int test_init_cmd_list(void)
3472 {
3473         int res = (!list_empty(&scst_init_cmd_list) &&
3474                    !test_bit(SCST_FLAG_SUSPENDED, &scst_flags)) ||
3475                   unlikely(kthread_should_stop()) ||
3476                   (scst_init_poll_cnt > 0);
3477         return res;
3478 }
3479
3480 int scst_init_thread(void *arg)
3481 {
3482         TRACE_ENTRY();
3483
3484         PRINT_INFO("Init thread started, PID %d", current->pid);
3485
3486         current->flags |= PF_NOFREEZE;
3487