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