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