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