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