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