df978af98eb5e9d66daf83fcd2d7404014224aa1
[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       ((int)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         /*
624          * We don't check here if the existing sense is valid or not, because
625          * we suppose the caller did it based on cmd->status.
626          */
627
628         res = scst_alloc_sense(cmd, atomic);
629         if (res != 0) {
630                 PRINT_BUFFER("Lost sense", sense, len);
631                 goto out;
632         }
633
634         cmd->sense_valid_len = len;
635         if (cmd->sense_buflen < len) {
636                 PRINT_WARNING("Sense truncated (needed %d), shall you increase "
637                         "SCST_SENSE_BUFFERSIZE? Op: %x", len, cmd->cdb[0]);
638                 cmd->sense_valid_len = cmd->sense_buflen;
639         }
640
641         memcpy(cmd->sense, sense, cmd->sense_valid_len);
642         TRACE_BUFFER("Sense set", cmd->sense, cmd->sense_valid_len);
643
644 out:
645         TRACE_EXIT_RES(res);
646         return res;
647 }
648 EXPORT_SYMBOL(scst_alloc_set_sense);
649
650 int scst_set_cmd_error_status(struct scst_cmd *cmd, int status)
651 {
652         int res = 0;
653
654         TRACE_ENTRY();
655
656         if (cmd->status != 0) {
657                 TRACE_MGMT_DBG("cmd %p already has status %x set", cmd,
658                         cmd->status);
659                 res = -EEXIST;
660                 goto out;
661         }
662
663         cmd->status = status;
664         cmd->host_status = DID_OK;
665
666         cmd->dbl_ua_orig_resp_data_len = cmd->resp_data_len;
667         cmd->dbl_ua_orig_data_direction = cmd->data_direction;
668
669         cmd->data_direction = SCST_DATA_NONE;
670         cmd->resp_data_len = 0;
671         cmd->is_send_status = 1;
672
673         cmd->completed = 1;
674
675 out:
676         TRACE_EXIT_RES(res);
677         return res;
678 }
679 EXPORT_SYMBOL(scst_set_cmd_error_status);
680
681 int scst_set_cmd_error(struct scst_cmd *cmd, int key, int asc, int ascq)
682 {
683         int res;
684
685         TRACE_ENTRY();
686
687         res = scst_set_cmd_error_status(cmd, SAM_STAT_CHECK_CONDITION);
688         if (res != 0)
689                 goto out;
690
691         res = scst_alloc_sense(cmd, 1);
692         if (res != 0) {
693                 PRINT_ERROR("Lost sense data (key %x, asc %x, ascq %x)",
694                         key, asc, ascq);
695                 goto out;
696         }
697
698         cmd->sense_valid_len = scst_set_sense(cmd->sense, cmd->sense_buflen,
699                 scst_get_cmd_dev_d_sense(cmd), key, asc, ascq);
700         TRACE_BUFFER("Sense set", cmd->sense, cmd->sense_valid_len);
701
702 out:
703         TRACE_EXIT_RES(res);
704         return res;
705 }
706 EXPORT_SYMBOL(scst_set_cmd_error);
707
708 int scst_set_sense(uint8_t *buffer, int len, bool d_sense,
709         int key, int asc, int ascq)
710 {
711         int res;
712
713         sBUG_ON(len == 0);
714
715         memset(buffer, 0, len);
716
717         if (d_sense) {
718                 /* Descriptor format */
719                 if (len < 8) {
720                         PRINT_ERROR("Length %d of sense buffer too small to "
721                                 "fit sense %x:%x:%x", len, key, asc, ascq);
722                 }
723
724                 buffer[0] = 0x72;               /* Response Code        */
725                 if (len > 1)
726                         buffer[1] = key;        /* Sense Key            */
727                 if (len > 2)
728                         buffer[2] = asc;        /* ASC                  */
729                 if (len > 3)
730                         buffer[3] = ascq;       /* ASCQ                 */
731                 res = 8;
732         } else {
733                 /* Fixed format */
734                 if (len < 18) {
735                         PRINT_ERROR("Length %d of sense buffer too small to "
736                                 "fit sense %x:%x:%x", len, key, asc, ascq);
737                 }
738
739                 buffer[0] = 0x70;               /* Response Code        */
740                 if (len > 2)
741                         buffer[2] = key;        /* Sense Key            */
742                 if (len > 7)
743                         buffer[7] = 0x0a;       /* Additional Sense Length */
744                 if (len > 12)
745                         buffer[12] = asc;       /* ASC                  */
746                 if (len > 13)
747                         buffer[13] = ascq;      /* ASCQ                 */
748                 res = 18;
749         }
750
751         TRACE_BUFFER("Sense set", buffer, res);
752         return res;
753 }
754 EXPORT_SYMBOL(scst_set_sense);
755
756 bool scst_analyze_sense(const uint8_t *sense, int len, unsigned int valid_mask,
757         int key, int asc, int ascq)
758 {
759         bool res = false;
760
761         /* Response Code */
762         if ((sense[0] == 0x70) || (sense[0] == 0x71)) {
763                 /* Fixed format */
764
765                 /* Sense Key */
766                 if (valid_mask & SCST_SENSE_KEY_VALID) {
767                         if (len < 3)
768                                 goto out;
769                         if (sense[2] != key)
770                                 goto out;
771                 }
772
773                 /* ASC */
774                 if (valid_mask & SCST_SENSE_ASC_VALID) {
775                         if (len < 13)
776                                 goto out;
777                         if (sense[12] != asc)
778                                 goto out;
779                 }
780
781                 /* ASCQ */
782                 if (valid_mask & SCST_SENSE_ASCQ_VALID) {
783                         if (len < 14)
784                                 goto out;
785                         if (sense[13] != ascq)
786                                 goto out;
787                 }
788         } else if ((sense[0] == 0x72) || (sense[0] == 0x73)) {
789                 /* Descriptor format */
790
791                 /* Sense Key */
792                 if (valid_mask & SCST_SENSE_KEY_VALID) {
793                         if (len < 2)
794                                 goto out;
795                         if (sense[1] != key)
796                                 goto out;
797                 }
798
799                 /* ASC */
800                 if (valid_mask & SCST_SENSE_ASC_VALID) {
801                         if (len < 3)
802                                 goto out;
803                         if (sense[2] != asc)
804                                 goto out;
805                 }
806
807                 /* ASCQ */
808                 if (valid_mask & SCST_SENSE_ASCQ_VALID) {
809                         if (len < 4)
810                                 goto out;
811                         if (sense[3] != ascq)
812                                 goto out;
813                 }
814         } else
815                 goto out;
816
817         res = true;
818
819 out:
820         TRACE_EXIT_RES((int)res);
821         return res;
822 }
823 EXPORT_SYMBOL(scst_analyze_sense);
824
825 bool scst_is_ua_sense(const uint8_t *sense, int len)
826 {
827         if (SCST_SENSE_VALID(sense))
828                 return scst_analyze_sense(sense, len,
829                         SCST_SENSE_KEY_VALID, UNIT_ATTENTION, 0, 0);
830         else
831                 return false;
832 }
833 EXPORT_SYMBOL(scst_is_ua_sense);
834
835 bool scst_is_ua_global(const uint8_t *sense, int len)
836 {
837         bool res;
838
839         /* Changing it don't forget to change scst_requeue_ua() as well!! */
840
841         if (scst_analyze_sense(sense, len, SCST_SENSE_ALL_VALID,
842                         SCST_LOAD_SENSE(scst_sense_reported_luns_data_changed)))
843                 res = true;
844         else
845                 res = false;
846
847         return res;
848 }
849
850 void scst_check_convert_sense(struct scst_cmd *cmd)
851 {
852         bool d_sense;
853
854         TRACE_ENTRY();
855
856         if ((cmd->sense == NULL) || (cmd->status != SAM_STAT_CHECK_CONDITION))
857                 goto out;
858
859         d_sense = scst_get_cmd_dev_d_sense(cmd);
860         if (d_sense && ((cmd->sense[0] == 0x70) || (cmd->sense[0] == 0x71))) {
861                 TRACE_MGMT_DBG("Converting fixed sense to descriptor (cmd %p)",
862                         cmd);
863                 if ((cmd->sense_valid_len < 18)) {
864                         PRINT_ERROR("Sense too small to convert (%d, "
865                                 "type: fixed)", cmd->sense_buflen);
866                         goto out;
867                 }
868                 cmd->sense_valid_len = scst_set_sense(cmd->sense, cmd->sense_buflen,
869                         d_sense, cmd->sense[2], cmd->sense[12], cmd->sense[13]);
870         } else if (!d_sense && ((cmd->sense[0] == 0x72) ||
871                                 (cmd->sense[0] == 0x73))) {
872                 TRACE_MGMT_DBG("Converting descriptor sense to fixed (cmd %p)",
873                         cmd);
874                 if ((cmd->sense_buflen < 18) || (cmd->sense_valid_len < 8)) {
875                         PRINT_ERROR("Sense too small to convert (%d, "
876                                 "type: descryptor, valid %d)",
877                                 cmd->sense_buflen, cmd->sense_valid_len);
878                         goto out;
879                 }
880                 cmd->sense_valid_len = scst_set_sense(cmd->sense,
881                         cmd->sense_buflen, d_sense,
882                         cmd->sense[1], cmd->sense[2], cmd->sense[3]);
883         }
884
885 out:
886         TRACE_EXIT();
887         return;
888 }
889 EXPORT_SYMBOL(scst_check_convert_sense);
890
891 static int scst_set_cmd_error_sense(struct scst_cmd *cmd, uint8_t *sense,
892         unsigned int len)
893 {
894         int res;
895
896         TRACE_ENTRY();
897
898         res = scst_set_cmd_error_status(cmd, SAM_STAT_CHECK_CONDITION);
899         if (res != 0)
900                 goto out;
901
902         res = scst_alloc_set_sense(cmd, 1, sense, len);
903
904 out:
905         TRACE_EXIT_RES(res);
906         return res;
907 }
908
909 void scst_set_busy(struct scst_cmd *cmd)
910 {
911         int c = atomic_read(&cmd->sess->sess_cmd_count);
912
913         TRACE_ENTRY();
914
915         if ((c <= 1) || (cmd->sess->init_phase != SCST_SESS_IPH_READY)) {
916                 scst_set_cmd_error_status(cmd, SAM_STAT_BUSY);
917                 TRACE(TRACE_FLOW_CONTROL, "Sending BUSY status to initiator %s "
918                         "(cmds count %d, queue_type %x, sess->init_phase %d)",
919                         cmd->sess->initiator_name, c,
920                         cmd->queue_type, cmd->sess->init_phase);
921         } else {
922                 scst_set_cmd_error_status(cmd, SAM_STAT_TASK_SET_FULL);
923                 TRACE(TRACE_FLOW_CONTROL, "Sending QUEUE_FULL status to "
924                         "initiator %s (cmds count %d, queue_type %x, "
925                         "sess->init_phase %d)", cmd->sess->initiator_name, c,
926                         cmd->queue_type, cmd->sess->init_phase);
927         }
928
929         TRACE_EXIT();
930         return;
931 }
932 EXPORT_SYMBOL(scst_set_busy);
933
934 void scst_set_initial_UA(struct scst_session *sess, int key, int asc, int ascq)
935 {
936         int i;
937
938         TRACE_ENTRY();
939
940         TRACE_MGMT_DBG("Setting for sess %p initial UA %x/%x/%x", sess, key,
941                 asc, ascq);
942
943         /* Protect sess_tgt_dev_list_hash */
944         mutex_lock(&scst_mutex);
945
946         for (i = 0; i < TGT_DEV_HASH_SIZE; i++) {
947                 struct list_head *sess_tgt_dev_list_head =
948                         &sess->sess_tgt_dev_list_hash[i];
949                 struct scst_tgt_dev *tgt_dev;
950
951                 list_for_each_entry(tgt_dev, sess_tgt_dev_list_head,
952                                 sess_tgt_dev_list_entry) {
953                         spin_lock_bh(&tgt_dev->tgt_dev_lock);
954                         if (!list_empty(&tgt_dev->UA_list)) {
955                                 struct scst_tgt_dev_UA *ua;
956
957                                 ua = list_entry(tgt_dev->UA_list.next,
958                                         typeof(*ua), UA_list_entry);
959                                 if (scst_analyze_sense(ua->UA_sense_buffer,
960                                                 ua->UA_valid_sense_len,
961                                                 SCST_SENSE_ALL_VALID,
962                                                 SCST_LOAD_SENSE(scst_sense_reset_UA))) {
963                                         ua->UA_valid_sense_len = scst_set_sense(
964                                                 ua->UA_sense_buffer,
965                                                 sizeof(ua->UA_sense_buffer),
966                                                 tgt_dev->dev->d_sense,
967                                                 key, asc, ascq);
968                                 } else
969                                         PRINT_ERROR("%s",
970                                                 "The first UA isn't RESET UA");
971                         } else
972                                 PRINT_ERROR("%s", "There's no RESET UA to "
973                                         "replace");
974                         spin_unlock_bh(&tgt_dev->tgt_dev_lock);
975                 }
976         }
977
978         mutex_unlock(&scst_mutex);
979
980         TRACE_EXIT();
981         return;
982 }
983 EXPORT_SYMBOL(scst_set_initial_UA);
984
985 static struct scst_aen *scst_alloc_aen(struct scst_session *sess,
986         uint64_t unpacked_lun)
987 {
988         struct scst_aen *aen;
989
990         TRACE_ENTRY();
991
992         aen = mempool_alloc(scst_aen_mempool, GFP_KERNEL);
993         if (aen == NULL) {
994                 PRINT_ERROR("AEN memory allocation failed. Corresponding "
995                         "event notification will not be performed (initiator "
996                         "%s)", sess->initiator_name);
997                 goto out;
998         }
999         memset(aen, 0, sizeof(*aen));
1000
1001         aen->sess = sess;
1002         scst_sess_get(sess);
1003
1004         aen->lun = scst_pack_lun(unpacked_lun);
1005
1006 out:
1007         TRACE_EXIT_HRES((unsigned long)aen);
1008         return aen;
1009 };
1010
1011 static void scst_free_aen(struct scst_aen *aen)
1012 {
1013         TRACE_ENTRY();
1014
1015         scst_sess_put(aen->sess);
1016         mempool_free(aen, scst_aen_mempool);
1017
1018         TRACE_EXIT();
1019         return;
1020 };
1021
1022 /* Must be called under scst_mutex */
1023 void scst_gen_aen_or_ua(struct scst_tgt_dev *tgt_dev,
1024         int key, int asc, int ascq)
1025 {
1026         struct scst_tgt_template *tgtt = tgt_dev->sess->tgt->tgtt;
1027         uint8_t sense_buffer[SCST_STANDARD_SENSE_LEN];
1028         int sl;
1029
1030         TRACE_ENTRY();
1031
1032         if (tgtt->report_aen != NULL) {
1033                 struct scst_aen *aen;
1034                 int rc;
1035
1036                 aen = scst_alloc_aen(tgt_dev->sess, tgt_dev->lun);
1037                 if (aen == NULL)
1038                         goto queue_ua;
1039
1040                 aen->event_fn = SCST_AEN_SCSI;
1041                 aen->aen_sense_len = scst_set_sense(aen->aen_sense,
1042                         sizeof(aen->aen_sense), tgt_dev->dev->d_sense,
1043                         key, asc, ascq);
1044
1045                 TRACE_DBG("Calling target's %s report_aen(%p)",
1046                         tgtt->name, aen);
1047                 rc = tgtt->report_aen(aen);
1048                 TRACE_DBG("Target's %s report_aen(%p) returned %d",
1049                         tgtt->name, aen, rc);
1050                 if (rc == SCST_AEN_RES_SUCCESS)
1051                         goto out;
1052
1053                 scst_free_aen(aen);
1054         }
1055
1056 queue_ua:
1057         TRACE_MGMT_DBG("AEN not supported, queuing plain UA (tgt_dev %p)",
1058                 tgt_dev);
1059         sl = scst_set_sense(sense_buffer, sizeof(sense_buffer),
1060                 tgt_dev->dev->d_sense, key, asc, ascq);
1061         scst_check_set_UA(tgt_dev, sense_buffer, sl, 0);
1062
1063 out:
1064         TRACE_EXIT();
1065         return;
1066 }
1067
1068 /* No locks */
1069 void scst_capacity_data_changed(struct scst_device *dev)
1070 {
1071         struct scst_tgt_dev *tgt_dev;
1072
1073         TRACE_ENTRY();
1074
1075         if (dev->type != TYPE_DISK) {
1076                 TRACE_MGMT_DBG("Device type %d isn't for CAPACITY DATA "
1077                         "CHANGED UA", dev->type);
1078                 goto out;
1079         }
1080
1081         TRACE_MGMT_DBG("CAPACITY DATA CHANGED (dev %p)", dev);
1082
1083         mutex_lock(&scst_mutex);
1084
1085         list_for_each_entry(tgt_dev, &dev->dev_tgt_dev_list,
1086                             dev_tgt_dev_list_entry) {
1087                 scst_gen_aen_or_ua(tgt_dev,
1088                         SCST_LOAD_SENSE(scst_sense_capacity_data_changed));
1089         }
1090
1091         mutex_unlock(&scst_mutex);
1092
1093 out:
1094         TRACE_EXIT();
1095         return;
1096 }
1097 EXPORT_SYMBOL(scst_capacity_data_changed);
1098
1099 static inline bool scst_is_report_luns_changed_type(int type)
1100 {
1101         switch (type) {
1102         case TYPE_DISK:
1103         case TYPE_TAPE:
1104         case TYPE_PRINTER:
1105         case TYPE_PROCESSOR:
1106         case TYPE_WORM:
1107         case TYPE_ROM:
1108         case TYPE_SCANNER:
1109         case TYPE_MOD:
1110         case TYPE_MEDIUM_CHANGER:
1111         case TYPE_RAID:
1112         case TYPE_ENCLOSURE:
1113                 return true;
1114         default:
1115                 return false;
1116         }
1117 }
1118
1119 /* scst_mutex supposed to be held */
1120 static void scst_queue_report_luns_changed_UA(struct scst_session *sess,
1121                                               int flags)
1122 {
1123         uint8_t sense_buffer[SCST_STANDARD_SENSE_LEN];
1124         struct list_head *shead;
1125         struct scst_tgt_dev *tgt_dev;
1126         int i;
1127
1128         TRACE_ENTRY();
1129
1130         TRACE_MGMT_DBG("Queuing REPORTED LUNS DATA CHANGED UA "
1131                 "(sess %p)", sess);
1132
1133         local_bh_disable();
1134
1135         for (i = 0; i < TGT_DEV_HASH_SIZE; i++) {
1136                 shead = &sess->sess_tgt_dev_list_hash[i];
1137
1138                 list_for_each_entry(tgt_dev, shead,
1139                                 sess_tgt_dev_list_entry) {
1140                         /* Lockdep triggers here a false positive.. */
1141                         spin_lock(&tgt_dev->tgt_dev_lock);
1142                 }
1143         }
1144
1145         for (i = 0; i < TGT_DEV_HASH_SIZE; i++) {
1146                 shead = &sess->sess_tgt_dev_list_hash[i];
1147
1148                 list_for_each_entry(tgt_dev, shead,
1149                                 sess_tgt_dev_list_entry) {
1150                         int sl;
1151
1152                         if (!scst_is_report_luns_changed_type(
1153                                         tgt_dev->dev->type))
1154                                 continue;
1155
1156                         sl = scst_set_sense(sense_buffer, sizeof(sense_buffer),
1157                                 tgt_dev->dev->d_sense,
1158                                 SCST_LOAD_SENSE(scst_sense_reported_luns_data_changed));
1159
1160                         __scst_check_set_UA(tgt_dev, sense_buffer,
1161                                 sl, flags | SCST_SET_UA_FLAG_GLOBAL);
1162                 }
1163         }
1164
1165         for (i = TGT_DEV_HASH_SIZE-1; i >= 0; i--) {
1166                 shead = &sess->sess_tgt_dev_list_hash[i];
1167
1168                 list_for_each_entry_reverse(tgt_dev,
1169                                 shead, sess_tgt_dev_list_entry) {
1170                         spin_unlock(&tgt_dev->tgt_dev_lock);
1171                 }
1172         }
1173
1174         local_bh_enable();
1175
1176         TRACE_EXIT();
1177         return;
1178 }
1179
1180 /* The activity supposed to be suspended and scst_mutex held */
1181 static void scst_report_luns_changed_sess(struct scst_session *sess)
1182 {
1183         int i;
1184         struct scst_tgt_template *tgtt = sess->tgt->tgtt;
1185         int d_sense = 0;
1186         uint64_t lun = 0;
1187
1188         TRACE_ENTRY();
1189
1190         TRACE_DBG("REPORTED LUNS DATA CHANGED (sess %p)", sess);
1191
1192         for (i = 0; i < TGT_DEV_HASH_SIZE; i++) {
1193                 struct list_head *shead;
1194                 struct scst_tgt_dev *tgt_dev;
1195
1196                 shead = &sess->sess_tgt_dev_list_hash[i];
1197
1198                 list_for_each_entry(tgt_dev, shead,
1199                                 sess_tgt_dev_list_entry) {
1200                         if (scst_is_report_luns_changed_type(
1201                                         tgt_dev->dev->type)) {
1202                                 lun = tgt_dev->lun;
1203                                 d_sense = tgt_dev->dev->d_sense;
1204                                 goto found;
1205                         }
1206                 }
1207         }
1208
1209 found:
1210         if (tgtt->report_aen != NULL) {
1211                 struct scst_aen *aen;
1212                 int rc;
1213
1214                 aen = scst_alloc_aen(sess, lun);
1215                 if (aen == NULL)
1216                         goto queue_ua;
1217
1218                 aen->event_fn = SCST_AEN_SCSI;
1219                 aen->aen_sense_len = scst_set_sense(aen->aen_sense,
1220                         sizeof(aen->aen_sense), d_sense,
1221                         SCST_LOAD_SENSE(scst_sense_reported_luns_data_changed));
1222
1223                 TRACE_DBG("Calling target's %s report_aen(%p)",
1224                         tgtt->name, aen);
1225                 rc = tgtt->report_aen(aen);
1226                 TRACE_DBG("Target's %s report_aen(%p) returned %d",
1227                         tgtt->name, aen, rc);
1228                 if (rc == SCST_AEN_RES_SUCCESS)
1229                         goto out;
1230
1231                 scst_free_aen(aen);
1232         }
1233
1234 queue_ua:
1235         scst_queue_report_luns_changed_UA(sess, 0);
1236
1237 out:
1238         TRACE_EXIT();
1239         return;
1240 }
1241
1242 /* The activity supposed to be suspended and scst_mutex held */
1243 void scst_report_luns_changed(struct scst_acg *acg)
1244 {
1245         struct scst_session *sess;
1246
1247         TRACE_ENTRY();
1248
1249         TRACE_MGMT_DBG("REPORTED LUNS DATA CHANGED (acg %s)", acg->acg_name);
1250
1251         list_for_each_entry(sess, &acg->acg_sess_list, acg_sess_list_entry) {
1252                 scst_report_luns_changed_sess(sess);
1253         }
1254
1255         TRACE_EXIT();
1256         return;
1257 }
1258
1259 void scst_aen_done(struct scst_aen *aen)
1260 {
1261         TRACE_ENTRY();
1262
1263         TRACE_MGMT_DBG("AEN %p (fn %d) done (initiator %s)", aen,
1264                 aen->event_fn, aen->sess->initiator_name);
1265
1266         if (aen->delivery_status == SCST_AEN_RES_SUCCESS)
1267                 goto out_free;
1268
1269         if (aen->event_fn != SCST_AEN_SCSI)
1270                 goto out_free;
1271
1272         TRACE_MGMT_DBG("Delivery of SCSI AEN failed (initiator %s)",
1273                 aen->sess->initiator_name);
1274
1275         if (scst_analyze_sense(aen->aen_sense, aen->aen_sense_len,
1276                         SCST_SENSE_ALL_VALID, SCST_LOAD_SENSE(
1277                                 scst_sense_reported_luns_data_changed))) {
1278                 mutex_lock(&scst_mutex);
1279                 scst_queue_report_luns_changed_UA(aen->sess,
1280                         SCST_SET_UA_FLAG_AT_HEAD);
1281                 mutex_unlock(&scst_mutex);
1282         } else {
1283                 struct list_head *shead;
1284                 struct scst_tgt_dev *tgt_dev;
1285                 uint64_t lun;
1286
1287                 lun = scst_unpack_lun((uint8_t *)&aen->lun, sizeof(aen->lun));
1288
1289                 mutex_lock(&scst_mutex);
1290
1291                 /* tgt_dev might get dead, so we need to reseek it */
1292                 shead = &aen->sess->sess_tgt_dev_list_hash[HASH_VAL(lun)];
1293                 list_for_each_entry(tgt_dev, shead,
1294                                 sess_tgt_dev_list_entry) {
1295                         if (tgt_dev->lun == lun) {
1296                                 TRACE_MGMT_DBG("Requeuing failed AEN UA for "
1297                                         "tgt_dev %p", tgt_dev);
1298                                 scst_check_set_UA(tgt_dev, aen->aen_sense,
1299                                         aen->aen_sense_len,
1300                                         SCST_SET_UA_FLAG_AT_HEAD);
1301                                 break;
1302                         }
1303                 }
1304
1305                 mutex_unlock(&scst_mutex);
1306         }
1307
1308 out_free:
1309         scst_free_aen(aen);
1310
1311         TRACE_EXIT();
1312         return;
1313 }
1314 EXPORT_SYMBOL(scst_aen_done);
1315
1316 void scst_requeue_ua(struct scst_cmd *cmd)
1317 {
1318         TRACE_ENTRY();
1319
1320         if (scst_analyze_sense(cmd->sense, cmd->sense_valid_len,
1321                         SCST_SENSE_ALL_VALID,
1322                         SCST_LOAD_SENSE(scst_sense_reported_luns_data_changed))) {
1323                 TRACE_MGMT_DBG("Requeuing REPORTED LUNS DATA CHANGED UA "
1324                         "for delivery failed cmd %p", cmd);
1325                 mutex_lock(&scst_mutex);
1326                 scst_queue_report_luns_changed_UA(cmd->sess,
1327                         SCST_SET_UA_FLAG_AT_HEAD);
1328                 mutex_unlock(&scst_mutex);
1329         } else {
1330                 TRACE_MGMT_DBG("Requeuing UA for delivery failed cmd %p", cmd);
1331                 scst_check_set_UA(cmd->tgt_dev, cmd->sense,
1332                         cmd->sense_valid_len, SCST_SET_UA_FLAG_AT_HEAD);
1333         }
1334
1335         TRACE_EXIT();
1336         return;
1337 }
1338
1339 /* The activity supposed to be suspended and scst_mutex held */
1340 static void scst_check_reassign_sess(struct scst_session *sess)
1341 {
1342         struct scst_acg *acg, *old_acg;
1343         struct scst_acg_dev *acg_dev;
1344         int i;
1345         struct list_head *shead;
1346         struct scst_tgt_dev *tgt_dev;
1347         bool luns_changed = false;
1348         bool add_failed, something_freed, not_needed_freed = false;
1349
1350         TRACE_ENTRY();
1351
1352         TRACE_MGMT_DBG("Checking reassignment for sess %p (initiator %s)",
1353                 sess, sess->initiator_name);
1354
1355         acg = scst_find_acg(sess);
1356         if (acg == sess->acg) {
1357                 TRACE_MGMT_DBG("No reassignment for sess %p", sess);
1358                 goto out;
1359         }
1360
1361         TRACE_MGMT_DBG("sess %p will be reassigned from acg %s to acg %s",
1362                 sess, sess->acg->acg_name, acg->acg_name);
1363
1364         old_acg = sess->acg;
1365         sess->acg = NULL; /* to catch implicit dependencies earlier */
1366
1367 retry_add:
1368         add_failed = false;
1369         list_for_each_entry(acg_dev, &acg->acg_dev_list, acg_dev_list_entry) {
1370                 unsigned int inq_changed_ua_needed = 0;
1371
1372                 for (i = 0; i < TGT_DEV_HASH_SIZE; i++) {
1373                         shead = &sess->sess_tgt_dev_list_hash[i];
1374
1375                         list_for_each_entry(tgt_dev, shead,
1376                                         sess_tgt_dev_list_entry) {
1377                                 if ((tgt_dev->dev == acg_dev->dev) &&
1378                                     (tgt_dev->lun == acg_dev->lun) &&
1379                                     (tgt_dev->acg_dev->rd_only == acg_dev->rd_only)) {
1380                                         TRACE_MGMT_DBG("sess %p: tgt_dev %p for "
1381                                                 "LUN %lld stays the same",
1382                                                 sess, tgt_dev,
1383                                                 (unsigned long long)tgt_dev->lun);
1384                                         tgt_dev->acg_dev = acg_dev;
1385                                         goto next;
1386                                 } else if (tgt_dev->lun == acg_dev->lun)
1387                                         inq_changed_ua_needed = 1;
1388                         }
1389                 }
1390
1391                 luns_changed = true;
1392
1393                 TRACE_MGMT_DBG("sess %p: Allocing new tgt_dev for LUN %lld",
1394                         sess, (unsigned long long)acg_dev->lun);
1395
1396                 tgt_dev = scst_alloc_add_tgt_dev(sess, acg_dev);
1397                 if (tgt_dev == NULL) {
1398                         add_failed = true;
1399                         break;
1400                 }
1401
1402                 tgt_dev->inq_changed_ua_needed = inq_changed_ua_needed ||
1403                                                  not_needed_freed;
1404 next:
1405                 continue;
1406         }
1407
1408         something_freed = false;
1409         not_needed_freed = true;
1410         for (i = 0; i < TGT_DEV_HASH_SIZE; i++) {
1411                 struct scst_tgt_dev *t;
1412                 shead = &sess->sess_tgt_dev_list_hash[i];
1413
1414                 list_for_each_entry_safe(tgt_dev, t, shead,
1415                                         sess_tgt_dev_list_entry) {
1416                         if (tgt_dev->acg_dev->acg != acg) {
1417                                 TRACE_MGMT_DBG("sess %p: Deleting not used "
1418                                         "tgt_dev %p for LUN %lld",
1419                                         sess, tgt_dev,
1420                                         (unsigned long long)tgt_dev->lun);
1421                                 luns_changed = true;
1422                                 something_freed = true;
1423                                 scst_free_tgt_dev(tgt_dev);
1424                         }
1425                 }
1426         }
1427
1428         if (add_failed && something_freed) {
1429                 TRACE_MGMT_DBG("sess %p: Retrying adding new tgt_devs", sess);
1430                 goto retry_add;
1431         }
1432
1433         sess->acg = acg;
1434
1435         TRACE_DBG("Moving sess %p from acg %s to acg %s", sess,
1436                 old_acg->acg_name, acg->acg_name);
1437         list_move_tail(&sess->acg_sess_list_entry, &acg->acg_sess_list);
1438
1439         if (luns_changed) {
1440                 scst_report_luns_changed_sess(sess);
1441
1442                 for (i = 0; i < TGT_DEV_HASH_SIZE; i++) {
1443                         shead = &sess->sess_tgt_dev_list_hash[i];
1444
1445                         list_for_each_entry(tgt_dev, shead,
1446                                         sess_tgt_dev_list_entry) {
1447                                 if (tgt_dev->inq_changed_ua_needed) {
1448                                         TRACE_MGMT_DBG("sess %p: Setting "
1449                                                 "INQUIRY DATA HAS CHANGED UA "
1450                                                 "(tgt_dev %p)", sess, tgt_dev);
1451
1452                                         tgt_dev->inq_changed_ua_needed = 0;
1453
1454                                         scst_gen_aen_or_ua(tgt_dev,
1455                                                 SCST_LOAD_SENSE(scst_sense_inquery_data_changed));
1456                                 }
1457                         }
1458                 }
1459         }
1460
1461 out:
1462         TRACE_EXIT();
1463         return;
1464 }
1465
1466 /* The activity supposed to be suspended and scst_mutex held */
1467 void scst_check_reassign_sessions(void)
1468 {
1469         struct scst_tgt_template *tgtt;
1470
1471         TRACE_ENTRY();
1472
1473         list_for_each_entry(tgtt, &scst_template_list, scst_template_list_entry) {
1474                 struct scst_tgt *tgt;
1475                 list_for_each_entry(tgt, &tgtt->tgt_list, tgt_list_entry) {
1476                         struct scst_session *sess;
1477                         list_for_each_entry(sess, &tgt->sess_list,
1478                                                 sess_list_entry) {
1479                                 scst_check_reassign_sess(sess);
1480                         }
1481                 }
1482         }
1483
1484         TRACE_EXIT();
1485         return;
1486 }
1487
1488 int scst_get_cmd_abnormal_done_state(const struct scst_cmd *cmd)
1489 {
1490         int res;
1491
1492         TRACE_ENTRY();
1493
1494         switch (cmd->state) {
1495         case SCST_CMD_STATE_INIT_WAIT:
1496         case SCST_CMD_STATE_INIT:
1497         case SCST_CMD_STATE_PRE_PARSE:
1498         case SCST_CMD_STATE_DEV_PARSE:
1499                 if (cmd->preprocessing_only) {
1500                         res = SCST_CMD_STATE_PREPROCESSING_DONE;
1501                         break;
1502                 } /* else go through */
1503         case SCST_CMD_STATE_DEV_DONE:
1504                 if (cmd->internal)
1505                         res = SCST_CMD_STATE_FINISHED_INTERNAL;
1506                 else
1507                         res = SCST_CMD_STATE_PRE_XMIT_RESP;
1508                 break;
1509
1510         case SCST_CMD_STATE_PRE_DEV_DONE:
1511         case SCST_CMD_STATE_MODE_SELECT_CHECKS:
1512                 res = SCST_CMD_STATE_DEV_DONE;
1513                 break;
1514
1515         case SCST_CMD_STATE_PRE_XMIT_RESP:
1516                 res = SCST_CMD_STATE_XMIT_RESP;
1517                 break;
1518
1519         case SCST_CMD_STATE_PREPROCESSING_DONE:
1520         case SCST_CMD_STATE_PREPROCESSING_DONE_CALLED:
1521                 if (cmd->tgt_dev == NULL)
1522                         res = SCST_CMD_STATE_PRE_XMIT_RESP;
1523                 else
1524                         res = SCST_CMD_STATE_PRE_DEV_DONE;
1525                 break;
1526
1527         case SCST_CMD_STATE_PREPARE_SPACE:
1528                 if (cmd->preprocessing_only) {
1529                         res = SCST_CMD_STATE_PREPROCESSING_DONE;
1530                         break;
1531                 } /* else go through */
1532         case SCST_CMD_STATE_RDY_TO_XFER:
1533         case SCST_CMD_STATE_DATA_WAIT:
1534         case SCST_CMD_STATE_TGT_PRE_EXEC:
1535         case SCST_CMD_STATE_SEND_FOR_EXEC:
1536         case SCST_CMD_STATE_LOCAL_EXEC:
1537         case SCST_CMD_STATE_REAL_EXEC:
1538         case SCST_CMD_STATE_REAL_EXECUTING:
1539                 res = SCST_CMD_STATE_PRE_DEV_DONE;
1540                 break;
1541
1542         default:
1543                 PRINT_CRIT_ERROR("Wrong cmd state %d (cmd %p, op %x)",
1544                         cmd->state, cmd, cmd->cdb[0]);
1545                 sBUG();
1546                 /* Invalid state to supress compiler's warning */
1547                 res = SCST_CMD_STATE_LAST_ACTIVE;
1548         }
1549
1550         TRACE_EXIT_RES(res);
1551         return res;
1552 }
1553 EXPORT_SYMBOL(scst_get_cmd_abnormal_done_state);
1554
1555 void scst_set_cmd_abnormal_done_state(struct scst_cmd *cmd)
1556 {
1557         TRACE_ENTRY();
1558
1559 #ifdef CONFIG_SCST_EXTRACHECKS
1560         switch (cmd->state) {
1561         case SCST_CMD_STATE_XMIT_RESP:
1562         case SCST_CMD_STATE_FINISHED:
1563         case SCST_CMD_STATE_FINISHED_INTERNAL:
1564         case SCST_CMD_STATE_XMIT_WAIT:
1565                 PRINT_CRIT_ERROR("Wrong cmd state %d (cmd %p, op %x)",
1566                         cmd->state, cmd, cmd->cdb[0]);
1567                 sBUG();
1568         }
1569 #endif
1570
1571         cmd->state = scst_get_cmd_abnormal_done_state(cmd);
1572
1573 #ifdef CONFIG_SCST_EXTRACHECKS
1574         if (((cmd->state != SCST_CMD_STATE_PRE_XMIT_RESP) &&
1575              (cmd->state != SCST_CMD_STATE_PREPROCESSING_DONE)) &&
1576                    (cmd->tgt_dev == NULL) && !cmd->internal) {
1577                 PRINT_CRIT_ERROR("Wrong not inited cmd state %d (cmd %p, "
1578                         "op %x)", cmd->state, cmd, cmd->cdb[0]);
1579                 sBUG();
1580         }
1581 #endif
1582
1583         TRACE_EXIT();
1584         return;
1585 }
1586 EXPORT_SYMBOL(scst_set_cmd_abnormal_done_state);
1587
1588 void scst_set_resp_data_len(struct scst_cmd *cmd, int resp_data_len)
1589 {
1590         int i, l;
1591
1592         TRACE_ENTRY();
1593
1594         scst_check_restore_sg_buff(cmd);
1595         cmd->resp_data_len = resp_data_len;
1596
1597         if (resp_data_len == cmd->bufflen)
1598                 goto out;
1599
1600         l = 0;
1601         for (i = 0; i < cmd->sg_cnt; i++) {
1602                 l += cmd->sg[i].length;
1603                 if (l >= resp_data_len) {
1604                         int left = resp_data_len - (l - cmd->sg[i].length);
1605 #ifdef CONFIG_SCST_DEBUG
1606                         TRACE(TRACE_SG_OP|TRACE_MEMORY, "cmd %p (tag %llu), "
1607                                 "resp_data_len %d, i %d, cmd->sg[i].length %d, "
1608                                 "left %d",
1609                                 cmd, (long long unsigned int)cmd->tag,
1610                                 resp_data_len, i,
1611                                 cmd->sg[i].length, left);
1612 #endif
1613                         cmd->orig_sg_cnt = cmd->sg_cnt;
1614                         cmd->orig_sg_entry = i;
1615                         cmd->orig_entry_len = cmd->sg[i].length;
1616                         cmd->sg_cnt = (left > 0) ? i+1 : i;
1617                         cmd->sg[i].length = left;
1618                         cmd->sg_buff_modified = 1;
1619                         break;
1620                 }
1621         }
1622
1623 out:
1624         TRACE_EXIT();
1625         return;
1626 }
1627 EXPORT_SYMBOL(scst_set_resp_data_len);
1628
1629 /* No locks */
1630 int scst_queue_retry_cmd(struct scst_cmd *cmd, int finished_cmds)
1631 {
1632         struct scst_tgt *tgt = cmd->tgt;
1633         int res = 0;
1634         unsigned long flags;
1635
1636         TRACE_ENTRY();
1637
1638         spin_lock_irqsave(&tgt->tgt_lock, flags);
1639         tgt->retry_cmds++;
1640         /*
1641          * Memory barrier is needed here, because we need the exact order
1642          * between the read and write between retry_cmds and finished_cmds to
1643          * not miss the case when a command finished while we queuing it for
1644          * retry after the finished_cmds check.
1645          */
1646         smp_mb();
1647         TRACE_RETRY("TGT QUEUE FULL: incrementing retry_cmds %d",
1648               tgt->retry_cmds);
1649         if (finished_cmds != atomic_read(&tgt->finished_cmds)) {
1650                 /* At least one cmd finished, so try again */
1651                 tgt->retry_cmds--;
1652                 TRACE_RETRY("Some command(s) finished, direct retry "
1653                       "(finished_cmds=%d, tgt->finished_cmds=%d, "
1654                       "retry_cmds=%d)", finished_cmds,
1655                       atomic_read(&tgt->finished_cmds), tgt->retry_cmds);
1656                 res = -1;
1657                 goto out_unlock_tgt;
1658         }
1659
1660         TRACE_RETRY("Adding cmd %p to retry cmd list", cmd);
1661         list_add_tail(&cmd->cmd_list_entry, &tgt->retry_cmd_list);
1662
1663         if (!tgt->retry_timer_active) {
1664                 tgt->retry_timer.expires = jiffies + SCST_TGT_RETRY_TIMEOUT;
1665                 add_timer(&tgt->retry_timer);
1666                 tgt->retry_timer_active = 1;
1667         }
1668
1669 out_unlock_tgt:
1670         spin_unlock_irqrestore(&tgt->tgt_lock, flags);
1671
1672         TRACE_EXIT_RES(res);
1673         return res;
1674 }
1675
1676 /* Returns 0 to continue, >0 to restart, <0 to break */
1677 static int scst_check_hw_pending_cmd(struct scst_cmd *cmd,
1678         unsigned long cur_time, unsigned long max_time,
1679         struct scst_session *sess, unsigned long *flags,
1680         struct scst_tgt_template *tgtt)
1681 {
1682         int res = -1; /* break */
1683
1684         TRACE_DBG("cmd %p, hw_pending %d, proc time %ld, "
1685                 "pending time %ld", cmd, cmd->cmd_hw_pending,
1686                 (long)(cur_time - cmd->start_time) / HZ,
1687                 (long)(cur_time - cmd->hw_pending_start) / HZ);
1688
1689         if (time_before_eq(cur_time, cmd->start_time + max_time)) {
1690                 /* Cmds are ordered, so no need to check more */
1691                 goto out;
1692         }
1693
1694         if (!cmd->cmd_hw_pending) {
1695                 res = 0; /* continue */
1696                 goto out;
1697         }
1698
1699         if (time_before(cur_time, cmd->hw_pending_start + max_time)) {
1700                 /* Cmds are ordered, so no need to check more */
1701                 goto out;
1702         }
1703
1704         TRACE_MGMT_DBG("Cmd %p HW pending for too long %ld (state %x)",
1705                 cmd, (cur_time - cmd->hw_pending_start) / HZ,
1706                 cmd->state);
1707
1708         cmd->cmd_hw_pending = 0;
1709
1710         spin_unlock_irqrestore(&sess->sess_list_lock, *flags);
1711         tgtt->on_hw_pending_cmd_timeout(cmd);
1712         spin_lock_irqsave(&sess->sess_list_lock, *flags);
1713
1714         res = 1; /* restart */
1715
1716 out:
1717         TRACE_EXIT_RES(res);
1718         return res;
1719 }
1720
1721 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)
1722 static void scst_hw_pending_work_fn(void *p)
1723 #else
1724 static void scst_hw_pending_work_fn(struct delayed_work *work)
1725 #endif
1726 {
1727 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 20)
1728         struct scst_session *sess = (struct scst_session *)p;
1729 #else
1730         struct scst_session *sess = container_of(work, struct scst_session,
1731                                         hw_pending_work);
1732 #endif
1733         struct scst_tgt_template *tgtt = sess->tgt->tgtt;
1734         struct scst_cmd *cmd;
1735         unsigned long cur_time = jiffies;
1736         unsigned long flags;
1737         unsigned long max_time = tgtt->max_hw_pending_time * HZ;
1738
1739         TRACE_ENTRY();
1740
1741         TRACE_DBG("HW pending work (sess %p, max time %ld)", sess, max_time/HZ);
1742
1743         clear_bit(SCST_SESS_HW_PENDING_WORK_SCHEDULED, &sess->sess_aflags);
1744
1745         spin_lock_irqsave(&sess->sess_list_lock, flags);
1746
1747 restart:
1748         list_for_each_entry(cmd, &sess->sess_cmd_list, sess_cmd_list_entry) {
1749                 int rc;
1750
1751                 rc = scst_check_hw_pending_cmd(cmd, cur_time, max_time, sess,
1752                                         &flags, tgtt);
1753                 if (rc < 0)
1754                         break;
1755                 else if (rc == 0)
1756                         continue;
1757                 else
1758                         goto restart;
1759         }
1760
1761         if (list_empty(&sess->sess_cmd_list)) {
1762                 /*
1763                  * For stuck cmds if there is no activity we might need to have
1764                  * one more run to release them, so reschedule once again.
1765                  */
1766                 TRACE_DBG("Sched HW pending work for sess %p (max time %d)",
1767                         sess, tgtt->max_hw_pending_time);
1768                 set_bit(SCST_SESS_HW_PENDING_WORK_SCHEDULED, &sess->sess_aflags);
1769                 schedule_delayed_work(&sess->hw_pending_work,
1770                                 tgtt->max_hw_pending_time * HZ);
1771         }
1772
1773         spin_unlock_irqrestore(&sess->sess_list_lock, flags);
1774
1775         TRACE_EXIT();
1776         return;
1777 }
1778
1779 struct scst_tgt *scst_alloc_tgt(struct scst_tgt_template *tgtt)
1780 {
1781         struct scst_tgt *tgt;
1782
1783         TRACE_ENTRY();
1784
1785         tgt = kzalloc(sizeof(*tgt), GFP_KERNEL);
1786         if (tgt == NULL) {
1787                 TRACE(TRACE_OUT_OF_MEM, "%s", "Allocation of tgt failed");
1788                 goto out;
1789         }
1790
1791         INIT_LIST_HEAD(&tgt->sess_list);
1792         init_waitqueue_head(&tgt->unreg_waitQ);
1793         tgt->tgtt = tgtt;
1794         tgt->sg_tablesize = tgtt->sg_tablesize;
1795         spin_lock_init(&tgt->tgt_lock);
1796         INIT_LIST_HEAD(&tgt->retry_cmd_list);
1797         atomic_set(&tgt->finished_cmds, 0);
1798         init_timer(&tgt->retry_timer);
1799         tgt->retry_timer.data = (unsigned long)tgt;
1800         tgt->retry_timer.function = scst_tgt_retry_timer_fn;
1801
1802 out:
1803         TRACE_EXIT_HRES((unsigned long)tgt);
1804         return tgt;
1805 }
1806
1807 void scst_free_tgt(struct scst_tgt *tgt)
1808 {
1809         TRACE_ENTRY();
1810
1811         kfree(tgt);
1812
1813         TRACE_EXIT();
1814         return;
1815 }
1816
1817 /* Called under scst_mutex and suspended activity */
1818 int scst_alloc_device(gfp_t gfp_mask, struct scst_device **out_dev)
1819 {
1820         struct scst_device *dev;
1821         int res = 0;
1822         static int dev_num; /* protected by scst_mutex */
1823
1824         TRACE_ENTRY();
1825
1826         dev = kzalloc(sizeof(*dev), gfp_mask);
1827         if (dev == NULL) {
1828                 TRACE(TRACE_OUT_OF_MEM, "%s",
1829                         "Allocation of scst_device failed");
1830                 res = -ENOMEM;
1831                 goto out;
1832         }
1833
1834         dev->handler = &scst_null_devtype;
1835         dev->p_cmd_lists = &scst_main_cmd_lists;
1836         atomic_set(&dev->dev_cmd_count, 0);
1837         atomic_set(&dev->write_cmd_count, 0);
1838         scst_init_mem_lim(&dev->dev_mem_lim);
1839         spin_lock_init(&dev->dev_lock);
1840         atomic_set(&dev->on_dev_count, 0);
1841         INIT_LIST_HEAD(&dev->blocked_cmd_list);
1842         INIT_LIST_HEAD(&dev->dev_tgt_dev_list);
1843         INIT_LIST_HEAD(&dev->dev_acg_dev_list);
1844         INIT_LIST_HEAD(&dev->threads_list);
1845         init_waitqueue_head(&dev->on_dev_waitQ);
1846         dev->dev_double_ua_possible = 1;
1847         dev->queue_alg = SCST_CONTR_MODE_QUEUE_ALG_UNRESTRICTED_REORDER;
1848         dev->dev_num = dev_num++;
1849
1850 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25) && defined(SCST_IO_CONTEXT)
1851 #if defined(CONFIG_BLOCK)
1852         dev->dev_io_ctx = alloc_io_context(GFP_KERNEL, -1);
1853         if (dev->dev_io_ctx == NULL) {
1854                 TRACE(TRACE_OUT_OF_MEM, "%s", "Failed to alloc dev IO context");
1855                 res = -ENOMEM;
1856                 kfree(dev);
1857                 goto out;
1858         }
1859 #endif
1860 #endif
1861
1862         *out_dev = dev;
1863
1864 out:
1865         TRACE_EXIT_RES(res);
1866         return res;
1867 }
1868
1869 void scst_free_device(struct scst_device *dev)
1870 {
1871         TRACE_ENTRY();
1872
1873 #ifdef CONFIG_SCST_EXTRACHECKS
1874         if (!list_empty(&dev->dev_tgt_dev_list) ||
1875             !list_empty(&dev->dev_acg_dev_list)) {
1876                 PRINT_CRIT_ERROR("%s: dev_tgt_dev_list or dev_acg_dev_list "
1877                         "is not empty!", __func__);
1878                 sBUG();
1879         }
1880 #endif
1881
1882         kfree(dev->virt_name);
1883         __exit_io_context(dev->dev_io_ctx);
1884
1885         kfree(dev);
1886
1887         TRACE_EXIT();
1888         return;
1889 }
1890
1891 void scst_init_mem_lim(struct scst_mem_lim *mem_lim)
1892 {
1893         atomic_set(&mem_lim->alloced_pages, 0);
1894         mem_lim->max_allowed_pages =
1895                 ((uint64_t)scst_max_dev_cmd_mem << 10) >> (PAGE_SHIFT - 10);
1896 }
1897 EXPORT_SYMBOL(scst_init_mem_lim);
1898
1899 static struct scst_acg_dev *scst_alloc_acg_dev(struct scst_acg *acg,
1900                                         struct scst_device *dev, uint64_t lun)
1901 {
1902         struct scst_acg_dev *res;
1903
1904         TRACE_ENTRY();
1905
1906 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 17)
1907         res = kmem_cache_alloc(scst_acgd_cachep, GFP_KERNEL);
1908 #else
1909         res = kmem_cache_zalloc(scst_acgd_cachep, GFP_KERNEL);
1910 #endif
1911         if (res == NULL) {
1912                 TRACE(TRACE_OUT_OF_MEM,
1913                       "%s", "Allocation of scst_acg_dev failed");
1914                 goto out;
1915         }
1916 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 17)
1917         memset(res, 0, sizeof(*res));
1918 #endif
1919
1920         res->dev = dev;
1921         res->acg = acg;
1922         res->lun = lun;
1923
1924 out:
1925         TRACE_EXIT_HRES(res);
1926         return res;
1927 }
1928
1929 void scst_acg_dev_destroy(struct scst_acg_dev *acg_dev)
1930 {
1931         TRACE_ENTRY();
1932
1933         kmem_cache_free(scst_acgd_cachep, acg_dev);
1934
1935         TRACE_EXIT();
1936         return;
1937 }
1938
1939 /* The activity supposed to be suspended and scst_mutex held */
1940 static void scst_free_acg_dev(struct scst_acg_dev *acg_dev)
1941 {
1942         TRACE_ENTRY();
1943
1944         TRACE_DBG("Removing acg_dev %p from acg_dev_list and dev_acg_dev_list",
1945                 acg_dev);
1946         list_del(&acg_dev->acg_dev_list_entry);
1947         list_del(&acg_dev->dev_acg_dev_list_entry);
1948
1949         if (acg_dev->acg_dev_kobj_initialized) {
1950                 kobject_del(&acg_dev->acg_dev_kobj);
1951                 kobject_put(&acg_dev->acg_dev_kobj);
1952         } else
1953                 scst_acg_dev_destroy(acg_dev);
1954
1955         TRACE_EXIT();
1956         return;
1957 }
1958
1959 /* The activity supposed to be suspended and scst_mutex held */
1960 struct scst_acg *scst_alloc_add_acg(const char *acg_name)
1961 {
1962         struct scst_acg *acg;
1963
1964         TRACE_ENTRY();
1965
1966         acg = kzalloc(sizeof(*acg), GFP_KERNEL);
1967         if (acg == NULL) {
1968                 TRACE(TRACE_OUT_OF_MEM, "%s", "Allocation of acg failed");
1969                 goto out;
1970         }
1971
1972         INIT_LIST_HEAD(&acg->acg_dev_list);
1973         INIT_LIST_HEAD(&acg->acg_sess_list);
1974         INIT_LIST_HEAD(&acg->acn_list);
1975         acg->acg_name = acg_name;
1976
1977         TRACE_DBG("Adding acg %s to scst_acg_list", acg_name);
1978         list_add_tail(&acg->scst_acg_list_entry, &scst_acg_list);
1979
1980         scst_check_reassign_sessions();
1981
1982 out:
1983         TRACE_EXIT_HRES(acg);
1984         return acg;
1985 }
1986
1987 /* The activity supposed to be suspended and scst_mutex held */
1988 int scst_destroy_acg(struct scst_acg *acg)
1989 {
1990         struct scst_acn *n, *nn;
1991         struct scst_acg_dev *acg_dev, *acg_dev_tmp;
1992         int res = 0;
1993
1994         TRACE_ENTRY();
1995
1996         if (!list_empty(&acg->acg_sess_list)) {
1997                 PRINT_ERROR("%s: acg_sess_list is not empty!", __func__);
1998                 res = -EBUSY;
1999                 goto out;
2000         }
2001
2002         TRACE_DBG("Removing acg %s from scst_acg_list", acg->acg_name);
2003         list_del(&acg->scst_acg_list_entry);
2004
2005         /* Freeing acg_devs */
2006         list_for_each_entry_safe(acg_dev, acg_dev_tmp, &acg->acg_dev_list,
2007                         acg_dev_list_entry) {
2008                 struct scst_tgt_dev *tgt_dev, *tt;
2009                 list_for_each_entry_safe(tgt_dev, tt,
2010                                  &acg_dev->dev->dev_tgt_dev_list,
2011                                  dev_tgt_dev_list_entry) {
2012                         if (tgt_dev->acg_dev == acg_dev)
2013                                 scst_free_tgt_dev(tgt_dev);
2014                 }
2015                 scst_free_acg_dev(acg_dev);
2016         }
2017
2018         /* Freeing names */
2019         list_for_each_entry_safe(n, nn, &acg->acn_list,
2020                         acn_list_entry) {
2021                 list_del(&n->acn_list_entry);
2022                 kfree(n->name);
2023                 kfree(n);
2024         }
2025         INIT_LIST_HEAD(&acg->acn_list);
2026
2027         kfree(acg);
2028 out:
2029         TRACE_EXIT_RES(res);
2030         return res;
2031 }
2032
2033 /*
2034  * scst_mutex supposed to be held, there must not be parallel activity in this
2035  * session.
2036  */
2037 static struct scst_tgt_dev *scst_alloc_add_tgt_dev(struct scst_session *sess,
2038         struct scst_acg_dev *acg_dev)
2039 {
2040         int ini_sg, ini_unchecked_isa_dma, ini_use_clustering;
2041         struct scst_tgt_dev *tgt_dev, *t = NULL;
2042         struct scst_device *dev = acg_dev->dev;
2043         struct list_head *sess_tgt_dev_list_head;
2044         struct scst_tgt_template *vtt = sess->tgt->tgtt;
2045         int rc, i, sl;
2046         bool share_io_ctx = false;
2047         uint8_t sense_buffer[SCST_STANDARD_SENSE_LEN];
2048
2049         TRACE_ENTRY();
2050
2051 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 17)
2052         tgt_dev = kmem_cache_alloc(scst_tgtd_cachep, GFP_KERNEL);
2053 #else
2054         tgt_dev = kmem_cache_zalloc(scst_tgtd_cachep, GFP_KERNEL);
2055 #endif
2056         if (tgt_dev == NULL) {
2057                 TRACE(TRACE_OUT_OF_MEM, "%s",
2058                       "Allocation of scst_tgt_dev failed");
2059                 goto out;
2060         }
2061 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 17)
2062         memset(tgt_dev, 0, sizeof(*tgt_dev));
2063 #endif
2064
2065         tgt_dev->dev = dev;
2066         tgt_dev->lun = acg_dev->lun;
2067         tgt_dev->acg_dev = acg_dev;
2068         tgt_dev->sess = sess;
2069         atomic_set(&tgt_dev->tgt_dev_cmd_count, 0);
2070
2071         scst_sgv_pool_use_norm(tgt_dev);
2072
2073         if (dev->scsi_dev != NULL) {
2074                 ini_sg = dev->scsi_dev->host->sg_tablesize;
2075                 ini_unchecked_isa_dma = dev->scsi_dev->host->unchecked_isa_dma;
2076                 ini_use_clustering = (dev->scsi_dev->host->use_clustering ==
2077                                 ENABLE_CLUSTERING);
2078         } else {
2079                 ini_sg = (1 << 15) /* infinite */;
2080                 ini_unchecked_isa_dma = 0;
2081                 ini_use_clustering = 0;
2082         }
2083         tgt_dev->max_sg_cnt = min(ini_sg, sess->tgt->sg_tablesize);
2084
2085         if ((sess->tgt->tgtt->use_clustering || ini_use_clustering) &&
2086             !sess->tgt->tgtt->no_clustering)
2087                 scst_sgv_pool_use_norm_clust(tgt_dev);
2088
2089         if (sess->tgt->tgtt->unchecked_isa_dma || ini_unchecked_isa_dma)
2090                 scst_sgv_pool_use_dma(tgt_dev);
2091
2092         TRACE_MGMT_DBG("Device %s on SCST lun=%lld",
2093                dev->virt_name, (long long unsigned int)tgt_dev->lun);
2094
2095         spin_lock_init(&tgt_dev->tgt_dev_lock);
2096         INIT_LIST_HEAD(&tgt_dev->UA_list);
2097         spin_lock_init(&tgt_dev->thr_data_lock);
2098         INIT_LIST_HEAD(&tgt_dev->thr_data_list);
2099         spin_lock_init(&tgt_dev->sn_lock);
2100         INIT_LIST_HEAD(&tgt_dev->deferred_cmd_list);
2101         INIT_LIST_HEAD(&tgt_dev->skipped_sn_list);
2102         tgt_dev->curr_sn = (typeof(tgt_dev->curr_sn))(-300);
2103         tgt_dev->expected_sn = tgt_dev->curr_sn + 1;
2104         tgt_dev->num_free_sn_slots = ARRAY_SIZE(tgt_dev->sn_slots)-1;
2105         tgt_dev->cur_sn_slot = &tgt_dev->sn_slots[0];
2106         for (i = 0; i < (int)ARRAY_SIZE(tgt_dev->sn_slots); i++)
2107                 atomic_set(&tgt_dev->sn_slots[i], 0);
2108
2109         if (dev->handler->parse_atomic &&
2110             (sess->tgt->tgtt->preprocessing_done == NULL)) {
2111                 if (sess->tgt->tgtt->rdy_to_xfer_atomic)
2112                         __set_bit(SCST_TGT_DEV_AFTER_INIT_WR_ATOMIC,
2113                                 &tgt_dev->tgt_dev_flags);
2114                 if (dev->handler->exec_atomic)
2115                         __set_bit(SCST_TGT_DEV_AFTER_INIT_OTH_ATOMIC,
2116                                 &tgt_dev->tgt_dev_flags);
2117         }
2118         if (dev->handler->exec_atomic) {
2119                 if (sess->tgt->tgtt->rdy_to_xfer_atomic)
2120                         __set_bit(SCST_TGT_DEV_AFTER_RESTART_WR_ATOMIC,
2121                                 &tgt_dev->tgt_dev_flags);
2122                 __set_bit(SCST_TGT_DEV_AFTER_RESTART_OTH_ATOMIC,
2123                                 &tgt_dev->tgt_dev_flags);
2124                 __set_bit(SCST_TGT_DEV_AFTER_RX_DATA_ATOMIC,
2125                         &tgt_dev->tgt_dev_flags);
2126         }
2127         if (dev->handler->dev_done_atomic &&
2128             sess->tgt->tgtt->xmit_response_atomic) {
2129                 __set_bit(SCST_TGT_DEV_AFTER_EXEC_ATOMIC,
2130                         &tgt_dev->tgt_dev_flags);
2131         }
2132
2133         sl = scst_set_sense(sense_buffer, sizeof(sense_buffer),
2134                 dev->d_sense, SCST_LOAD_SENSE(scst_sense_reset_UA));
2135         scst_alloc_set_UA(tgt_dev, sense_buffer, sl, 0);
2136
2137         tm_dbg_init_tgt_dev(tgt_dev, acg_dev);
2138
2139         if (tgt_dev->sess->initiator_name != NULL) {
2140                 spin_lock_bh(&dev->dev_lock);
2141                 list_for_each_entry(t, &dev->dev_tgt_dev_list,
2142                                 dev_tgt_dev_list_entry) {
2143                         TRACE_DBG("t name %s (tgt_dev name %s)",
2144                                 t->sess->initiator_name,
2145                                 tgt_dev->sess->initiator_name);
2146                         if (t->sess->initiator_name == NULL)
2147                                 continue;
2148                         if (strcmp(t->sess->initiator_name,
2149                                         tgt_dev->sess->initiator_name) == 0) {
2150                                 share_io_ctx = true;
2151                                 break;
2152                         }
2153                 }
2154                 spin_unlock_bh(&dev->dev_lock);
2155         }
2156
2157         if (share_io_ctx) {
2158                 TRACE_MGMT_DBG("Sharing IO context %p (tgt_dev %p, ini %s)",
2159                         t->tgt_dev_io_ctx, tgt_dev,
2160                         tgt_dev->sess->initiator_name);
2161                 tgt_dev->tgt_dev_io_ctx = ioc_task_link(t->tgt_dev_io_ctx);
2162         } else {
2163 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 25) && defined(SCST_IO_CONTEXT)
2164 #if defined(CONFIG_BLOCK)
2165                 tgt_dev->tgt_dev_io_ctx = alloc_io_context(GFP_KERNEL, -1);
2166                 if (tgt_dev->tgt_dev_io_ctx == NULL) {
2167                         TRACE(TRACE_OUT_OF_MEM, "Failed to alloc tgt_dev IO "
2168                                 "context for dev %s (initiator %s)",
2169                                 dev->virt_name, sess->initiator_name);
2170                         goto out_free;
2171                 }
2172 #endif
2173 #endif
2174         }
2175
2176         if (vtt->threads_num > 0) {
2177                 rc = 0;
2178                 if (dev->handler->threads_num > 0)
2179                         rc = scst_add_dev_threads(dev, vtt->threads_num);
2180                 else if (dev->handler->threads_num == 0)
2181                         rc = scst_add_global_threads(vtt->threads_num);
2182                 if (rc != 0)
2183                         goto out_free;
2184         }
2185
2186         if (dev->handler && dev->handler->attach_tgt) {
2187                 TRACE_DBG("Calling dev handler's attach_tgt(%p)",
2188                       tgt_dev);
2189                 rc = dev->handler->attach_tgt(tgt_dev);
2190                 TRACE_DBG("%s", "Dev handler's attach_tgt() returned");
2191                 if (rc != 0) {
2192                         PRINT_ERROR("Device handler's %s attach_tgt() "
2193                             "failed: %d", dev->handler->name, rc);
2194                         goto out_thr_free;
2195                 }
2196         }
2197
2198         spin_lock_bh(&dev->dev_lock);
2199         list_add_tail(&tgt_dev->dev_tgt_dev_list_entry, &dev->dev_tgt_dev_list);
2200         if (dev->dev_reserved)
2201                 __set_bit(SCST_TGT_DEV_RESERVED, &tgt_dev->tgt_dev_flags);
2202         spin_unlock_bh(&dev->dev_lock);
2203
2204         sess_tgt_dev_list_head =
2205                 &sess->sess_tgt_dev_list_hash[HASH_VAL(tgt_dev->lun)];
2206         list_add_tail(&tgt_dev->sess_tgt_dev_list_entry,
2207                       sess_tgt_dev_list_head);
2208
2209 out:
2210         TRACE_EXIT();
2211         return tgt_dev;
2212
2213 out_thr_free:
2214         if (vtt->threads_num > 0) {
2215                 if (dev->handler->threads_num > 0)
2216                         scst_del_dev_threads(dev, vtt->threads_num);
2217                 else if (dev->handler->threads_num == 0)
2218                         scst_del_global_threads(vtt->threads_num);
2219         }
2220
2221 out_free:
2222         scst_free_all_UA(tgt_dev);
2223         __exit_io_context(tgt_dev->tgt_dev_io_ctx);
2224
2225         kmem_cache_free(scst_tgtd_cachep, tgt_dev);
2226         tgt_dev = NULL;
2227         goto out;
2228 }
2229
2230 /* No locks supposed to be held, scst_mutex - held */
2231 void scst_nexus_loss(struct scst_tgt_dev *tgt_dev, bool queue_UA)
2232 {
2233         TRACE_ENTRY();
2234
2235         scst_clear_reservation(tgt_dev);
2236
2237         /* With activity suspended the lock isn't needed, but let's be safe */
2238         spin_lock_bh(&tgt_dev->tgt_dev_lock);
2239         scst_free_all_UA(tgt_dev);
2240         memset(tgt_dev->tgt_dev_sense, 0, sizeof(tgt_dev->tgt_dev_sense));
2241         spin_unlock_bh(&tgt_dev->tgt_dev_lock);
2242
2243         if (queue_UA) {
2244                 uint8_t sense_buffer[SCST_STANDARD_SENSE_LEN];
2245                 int sl = scst_set_sense(sense_buffer, sizeof(sense_buffer),
2246                                 tgt_dev->dev->d_sense,
2247                                 SCST_LOAD_SENSE(scst_sense_nexus_loss_UA));
2248                 scst_check_set_UA(tgt_dev, sense_buffer, sl, 0);
2249         }
2250
2251         TRACE_EXIT();
2252         return;
2253 }
2254
2255 /*
2256  * scst_mutex supposed to be held, there must not be parallel activity in this
2257  * session.
2258  */
2259 static void scst_free_tgt_dev(struct scst_tgt_dev *tgt_dev)
2260 {
2261         struct scst_device *dev = tgt_dev->dev;
2262         struct scst_tgt_template *vtt = tgt_dev->sess->tgt->tgtt;
2263
2264         TRACE_ENTRY();
2265
2266         tm_dbg_deinit_tgt_dev(tgt_dev);
2267
2268         spin_lock_bh(&dev->dev_lock);
2269         list_del(&tgt_dev->dev_tgt_dev_list_entry);
2270         spin_unlock_bh(&dev->dev_lock);
2271
2272         list_del(&tgt_dev->sess_tgt_dev_list_entry);
2273
2274         scst_clear_reservation(tgt_dev);
2275         scst_free_all_UA(tgt_dev);
2276
2277         if (dev->handler && dev->handler->detach_tgt) {
2278                 TRACE_DBG("Calling dev handler's detach_tgt(%p)",
2279                       tgt_dev);
2280                 dev->handler->detach_tgt(tgt_dev);
2281                 TRACE_DBG("%s", "Dev handler's detach_tgt() returned");
2282         }
2283
2284         if (vtt->threads_num > 0) {
2285                 if (dev->handler->threads_num > 0)
2286                         scst_del_dev_threads(dev, vtt->threads_num);
2287                 else if (dev->handler->threads_num == 0)
2288                         scst_del_global_threads(vtt->threads_num);
2289         }
2290
2291         __exit_io_context(tgt_dev->tgt_dev_io_ctx);
2292
2293         kmem_cache_free(scst_tgtd_cachep, tgt_dev);
2294
2295         TRACE_EXIT();
2296         return;
2297 }
2298
2299 /* scst_mutex supposed to be held */
2300 int scst_sess_alloc_tgt_devs(struct scst_session *sess)
2301 {
2302         int res = 0;
2303         struct scst_acg_dev *acg_dev;
2304         struct scst_tgt_dev *tgt_dev;
2305
2306         TRACE_ENTRY();
2307
2308         list_for_each_entry(acg_dev, &sess->acg->acg_dev_list,
2309                         acg_dev_list_entry) {
2310                 tgt_dev = scst_alloc_add_tgt_dev(sess, acg_dev);
2311                 if (tgt_dev == NULL) {
2312                         res = -ENOMEM;
2313                         goto out_free;
2314                 }
2315         }
2316
2317 out:
2318         TRACE_EXIT();
2319         return res;
2320
2321 out_free:
2322         scst_sess_free_tgt_devs(sess);
2323         goto out;
2324 }
2325
2326 /*
2327  * scst_mutex supposed to be held, there must not be parallel activity in this
2328  * session.
2329  */
2330 void scst_sess_free_tgt_devs(struct scst_session *sess)
2331 {
2332         int i;
2333         struct scst_tgt_dev *tgt_dev, *t;
2334
2335         TRACE_ENTRY();
2336
2337         /* The session is going down, no users, so no locks */
2338         for (i = 0; i < TGT_DEV_HASH_SIZE; i++) {
2339                 struct list_head *sess_tgt_dev_list_head =
2340                         &sess->sess_tgt_dev_list_hash[i];
2341                 list_for_each_entry_safe(tgt_dev, t, sess_tgt_dev_list_head,
2342                                 sess_tgt_dev_list_entry) {
2343                         scst_free_tgt_dev(tgt_dev);
2344                 }
2345                 INIT_LIST_HEAD(sess_tgt_dev_list_head);
2346         }
2347
2348         TRACE_EXIT();
2349         return;
2350 }
2351
2352 /* The activity supposed to be suspended and scst_mutex held */
2353 int scst_acg_add_dev(struct scst_acg *acg, struct scst_device *dev,
2354         uint64_t lun, int read_only, bool gen_scst_report_luns_changed)
2355 {
2356         int res = 0;
2357         struct scst_acg_dev *acg_dev;
2358         struct scst_tgt_dev *tgt_dev;
2359         struct scst_session *sess;
2360         LIST_HEAD(tmp_tgt_dev_list);
2361
2362         TRACE_ENTRY();
2363
2364         INIT_LIST_HEAD(&tmp_tgt_dev_list);
2365
2366         acg_dev = scst_alloc_acg_dev(acg, dev, lun);
2367         if (acg_dev == NULL) {
2368                 res = -ENOMEM;
2369                 goto out;
2370         }
2371         acg_dev->rd_only = read_only;
2372
2373         TRACE_DBG("Adding acg_dev %p to acg_dev_list and dev_acg_dev_list",
2374                 acg_dev);
2375         list_add_tail(&acg_dev->acg_dev_list_entry, &acg->acg_dev_list);
2376         list_add_tail(&acg_dev->dev_acg_dev_list_entry, &dev->dev_acg_dev_list);
2377
2378         list_for_each_entry(sess, &acg->acg_sess_list, acg_sess_list_entry) {
2379                 tgt_dev = scst_alloc_add_tgt_dev(sess, acg_dev);
2380                 if (tgt_dev == NULL) {
2381                         res = -ENOMEM;
2382                         goto out_free;
2383                 }
2384                 list_add_tail(&tgt_dev->extra_tgt_dev_list_entry,
2385                               &tmp_tgt_dev_list);
2386         }
2387
2388         if (gen_scst_report_luns_changed)
2389                 scst_report_luns_changed(acg);
2390
2391         PRINT_INFO("Added device %s to group %s (LUN %lld, "
2392                 "rd_only %d)", dev->virt_name, acg->acg_name,
2393                 (long long unsigned int)lun, read_only);
2394
2395 out:
2396         TRACE_EXIT_RES(res);
2397         return res;
2398
2399 out_free:
2400         list_for_each_entry(tgt_dev, &tmp_tgt_dev_list,
2401                          extra_tgt_dev_list_entry) {
2402                 scst_free_tgt_dev(tgt_dev);
2403         }
2404         scst_free_acg_dev(acg_dev);
2405         goto out;
2406 }
2407
2408 /* The activity supposed to be suspended and scst_mutex held */
2409 int scst_acg_remove_dev(struct scst_acg *acg, struct scst_device *dev,
2410         bool gen_scst_report_luns_changed)
2411 {
2412         int res = 0;
2413         struct scst_acg_dev *acg_dev = NULL, *a;
2414         struct scst_tgt_dev *tgt_dev, *tt;
2415
2416         TRACE_ENTRY();
2417
2418         list_for_each_entry(a, &acg->acg_dev_list, acg_dev_list_entry) {
2419                 if (a->dev == dev) {
2420                         acg_dev = a;
2421                         break;
2422                 }
2423         }
2424
2425         if (acg_dev == NULL) {
2426                 PRINT_ERROR("Device is not found in group %s", acg->acg_name);
2427                 res = -EINVAL;
2428                 goto out;
2429         }
2430
2431         list_for_each_entry_safe(tgt_dev, tt, &dev->dev_tgt_dev_list,
2432                          dev_tgt_dev_list_entry) {
2433                 if (tgt_dev->acg_dev == acg_dev)
2434                         scst_free_tgt_dev(tgt_dev);
2435         }
2436         scst_free_acg_dev(acg_dev);
2437
2438         if (gen_scst_report_luns_changed)
2439                 scst_report_luns_changed(acg);
2440
2441         PRINT_INFO("Removed device %s from group %s", dev->virt_name,
2442                 acg->acg_name);
2443
2444 out:
2445         TRACE_EXIT_RES(res);
2446         return res;
2447 }
2448
2449 /* The activity supposed to be suspended and scst_mutex held */
2450 int scst_acg_add_name(struct scst_acg *acg, const char *name)
2451 {
2452         int res = 0;
2453         struct scst_acn *n;
2454         int len;
2455         char *nm;
2456
2457         TRACE_ENTRY();
2458
2459         list_for_each_entry(n, &acg->acn_list, acn_list_entry) {
2460                 if (strcmp(n->name, name) == 0) {
2461                         PRINT_ERROR("Name %s already exists in group %s",
2462                                 name, acg->acg_name);
2463                         res = -EEXIST;
2464                         goto out;
2465                 }
2466         }
2467
2468         n = kmalloc(sizeof(*n), GFP_KERNEL);
2469         if (n == NULL) {
2470                 PRINT_ERROR("%s", "Unable to allocate scst_acn");
2471                 res = -ENOMEM;
2472                 goto out;
2473         }
2474
2475         len = strlen(name);
2476         nm = kmalloc(len + 1, GFP_KERNEL);
2477         if (nm == NULL) {
2478                 PRINT_ERROR("%s", "Unable to allocate scst_acn->name");
2479                 res = -ENOMEM;
2480                 goto out_free;
2481         }
2482
2483         strcpy(nm, name);
2484         n->name = nm;
2485
2486         list_add_tail(&n->acn_list_entry, &acg->acn_list);
2487
2488 out:
2489         if (res == 0) {
2490                 PRINT_INFO("Added name %s to group %s", name, acg->acg_name);
2491                 scst_check_reassign_sessions();
2492         }
2493
2494         TRACE_EXIT_RES(res);
2495         return res;
2496
2497 out_free:
2498         kfree(n);
2499         goto out;
2500 }
2501
2502 /* scst_mutex supposed to be held */
2503 void __scst_acg_remove_acn(struct scst_acn *n)
2504 {
2505         TRACE_ENTRY();
2506
2507         list_del(&n->acn_list_entry);
2508         kfree(n->name);
2509         kfree(n);
2510
2511         TRACE_EXIT();
2512         return;
2513 }
2514
2515 /* The activity supposed to be suspended and scst_mutex held */
2516 int scst_acg_remove_name(struct scst_acg *acg, const char *name, bool reassign)
2517 {
2518         int res = -EINVAL;
2519         struct scst_acn *n;
2520
2521         TRACE_ENTRY();
2522
2523         list_for_each_entry(n, &acg->acn_list, acn_list_entry) {
2524                 if (strcmp(n->name, name) == 0) {
2525                         __scst_acg_remove_acn(n);
2526                         res = 0;
2527                         break;
2528                 }
2529         }
2530
2531         if (res == 0) {
2532                 PRINT_INFO("Removed name %s from group %s", name,
2533                         acg->acg_name);
2534                 if (reassign)
2535                         scst_check_reassign_sessions();
2536         } else
2537                 PRINT_ERROR("Unable to find name %s in group %s", name,
2538                         acg->acg_name);
2539
2540         TRACE_EXIT_RES(res);
2541         return res;
2542 }
2543
2544 static struct scst_cmd *scst_create_prepare_internal_cmd(
2545         struct scst_cmd *orig_cmd, int bufsize)
2546 {
2547         struct scst_cmd *res;
2548         gfp_t gfp_mask = scst_cmd_atomic(orig_cmd) ? GFP_ATOMIC : GFP_KERNEL;
2549
2550         TRACE_ENTRY();
2551
2552         res = scst_alloc_cmd(gfp_mask);
2553         if (res == NULL)
2554                 goto out;
2555
2556         res->cmd_lists = orig_cmd->cmd_lists;
2557         res->sess = orig_cmd->sess;
2558         res->atomic = scst_cmd_atomic(orig_cmd);
2559         res->internal = 1;
2560         res->tgtt = orig_cmd->tgtt;
2561         res->tgt = orig_cmd->tgt;
2562         res->dev = orig_cmd->dev;
2563         res->tgt_dev = orig_cmd->tgt_dev;
2564         res->lun = orig_cmd->lun;
2565         res->queue_type = SCST_CMD_QUEUE_HEAD_OF_QUEUE;
2566         res->data_direction = SCST_DATA_UNKNOWN;
2567         res->orig_cmd = orig_cmd;
2568         res->bufflen = bufsize;
2569
2570         scst_sess_get(res->sess);
2571         if (res->tgt_dev != NULL)
2572                 __scst_get(0);
2573
2574         res->state = SCST_CMD_STATE_PRE_PARSE;
2575
2576 out:
2577         TRACE_EXIT_HRES((unsigned long)res);
2578         return res;
2579 }
2580
2581 int scst_prepare_request_sense(struct scst_cmd *orig_cmd)
2582 {
2583         int res = 0;
2584         static const uint8_t request_sense[6] = {
2585                 REQUEST_SENSE, 0, 0, 0, SCST_SENSE_BUFFERSIZE, 0
2586         };
2587         struct scst_cmd *rs_cmd;
2588
2589         TRACE_ENTRY();
2590
2591         if (orig_cmd->sense != NULL) {
2592                 TRACE_MEM("Releasing sense %p (orig_cmd %p)",
2593                         orig_cmd->sense, orig_cmd);
2594                 mempool_free(orig_cmd->sense, scst_sense_mempool);
2595                 orig_cmd->sense = NULL;
2596         }
2597
2598         rs_cmd = scst_create_prepare_internal_cmd(orig_cmd,
2599                         SCST_SENSE_BUFFERSIZE);
2600         if (rs_cmd == NULL)
2601                 goto out_error;
2602
2603         memcpy(rs_cmd->cdb, request_sense, sizeof(request_sense));
2604         rs_cmd->cdb[1] |= scst_get_cmd_dev_d_sense(orig_cmd);
2605         rs_cmd->cdb_len = sizeof(request_sense);
2606         rs_cmd->data_direction = SCST_DATA_READ;
2607         rs_cmd->expected_data_direction = rs_cmd->data_direction;
2608         rs_cmd->expected_transfer_len = SCST_SENSE_BUFFERSIZE;
2609         rs_cmd->expected_values_set = 1;
2610
2611         TRACE(TRACE_MGMT_MINOR, "Adding REQUEST SENSE cmd %p to head of active "
2612                 "cmd list", rs_cmd);
2613         spin_lock_irq(&rs_cmd->cmd_lists->cmd_list_lock);
2614         list_add(&rs_cmd->cmd_list_entry, &rs_cmd->cmd_lists->active_cmd_list);
2615         wake_up(&rs_cmd->cmd_lists->cmd_list_waitQ);
2616         spin_unlock_irq(&rs_cmd->cmd_lists->cmd_list_lock);
2617
2618 out:
2619         TRACE_EXIT_RES(res);
2620         return res;
2621
2622 out_error:
2623         res = -1;
2624         goto out;
2625 }
2626
2627 static void scst_complete_request_sense(struct scst_cmd *req_cmd)
2628 {
2629         struct scst_cmd *orig_cmd = req_cmd->orig_cmd;
2630         uint8_t *buf;
2631         int len;
2632
2633         TRACE_ENTRY();
2634
2635         sBUG_ON(orig_cmd == NULL);
2636
2637         len = scst_get_buf_first(req_cmd, &buf);
2638
2639         if (scsi_status_is_good(req_cmd->status) && (len > 0) &&
2640             SCST_SENSE_VALID(buf) && (!SCST_NO_SENSE(buf))) {
2641                 PRINT_BUFF_FLAG(TRACE_SCSI, "REQUEST SENSE returned",
2642                         buf, len);
2643                 scst_alloc_set_sense(orig_cmd, scst_cmd_atomic(req_cmd), buf,
2644                         len);
2645         } else {
2646                 PRINT_ERROR("%s", "Unable to get the sense via "
2647                         "REQUEST SENSE, returning HARDWARE ERROR");
2648                 scst_set_cmd_error(orig_cmd,
2649                         SCST_LOAD_SENSE(scst_sense_hardw_error));
2650         }
2651
2652         if (len > 0)
2653                 scst_put_buf(req_cmd, buf);
2654
2655         TRACE(TRACE_MGMT_MINOR, "Adding orig cmd %p to head of active "
2656                 "cmd list", orig_cmd);
2657         spin_lock_irq(&orig_cmd->cmd_lists->cmd_list_lock);
2658         list_add(&orig_cmd->cmd_list_entry, &orig_cmd->cmd_lists->active_cmd_list);
2659         wake_up(&orig_cmd->cmd_lists->cmd_list_waitQ);
2660         spin_unlock_irq(&orig_cmd->cmd_lists->cmd_list_lock);
2661
2662         TRACE_EXIT();
2663         return;
2664 }
2665
2666 int scst_finish_internal_cmd(struct scst_cmd *cmd)
2667 {
2668         int res;
2669
2670         TRACE_ENTRY();
2671
2672         sBUG_ON(!cmd->internal);
2673
2674         if (cmd->cdb[0] == REQUEST_SENSE)
2675                 scst_complete_request_sense(cmd);
2676
2677         __scst_cmd_put(cmd);
2678
2679         res = SCST_CMD_STATE_RES_CONT_NEXT;
2680
2681         TRACE_EXIT_HRES(res);
2682         return res;
2683 }
2684
2685 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18)
2686 static void scst_req_done(struct scsi_cmnd *scsi_cmd)
2687 {
2688         struct scsi_request *req;
2689
2690         TRACE_ENTRY();
2691
2692         if (scsi_cmd) {
2693                 req = scsi_cmd->sc_request;
2694                 if (req) {
2695                         if (req->sr_bufflen)
2696                                 kfree(req->sr_buffer);
2697                         scsi_release_request(req);
2698                 }
2699         }
2700
2701         TRACE_EXIT();
2702         return;
2703 }
2704
2705 static void scst_send_release(struct scst_device *dev)
2706 {
2707         struct scsi_request *req;
2708         struct scsi_device *scsi_dev;
2709         uint8_t cdb[6];
2710
2711         TRACE_ENTRY();
2712
2713         if (dev->scsi_dev == NULL)
2714                 goto out;
2715
2716         scsi_dev = dev->scsi_dev;
2717
2718         req = scsi_allocate_request(scsi_dev, GFP_KERNEL);
2719         if (req == NULL) {
2720                 PRINT_ERROR("Allocation of scsi_request failed: unable "
2721                             "to RELEASE device %s", dev->virt_name);
2722                 goto out;
2723         }
2724
2725         memset(cdb, 0, sizeof(cdb));
2726         cdb[0] = RELEASE;
2727         cdb[1] = (scsi_dev->scsi_level <= SCSI_2) ?
2728             ((scsi_dev->lun << 5) & 0xe0) : 0;
2729         memcpy(req->sr_cmnd, cdb, sizeof(cdb));
2730         req->sr_cmd_len = sizeof(cdb);
2731         req->sr_data_direction = SCST_DATA_NONE;
2732         req->sr_use_sg = 0;
2733         req->sr_bufflen = 0;
2734         req->sr_buffer = NULL;
2735         req->sr_request->rq_disk = dev->rq_disk;
2736         req->sr_sense_buffer[0] = 0;
2737
2738         TRACE(TRACE_DEBUG | TRACE_SCSI, "Sending RELEASE req %p to SCSI "
2739                 "mid-level", req);
2740         scst_do_req(req, req->sr_cmnd, (void *)req->sr_buffer, req->sr_bufflen,
2741                     scst_req_done, 15, 3);
2742
2743 out:
2744         TRACE_EXIT();
2745         return;
2746 }
2747 #else /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18) */
2748 static void scst_send_release(struct scst_device *dev)
2749 {
2750         struct scsi_device *scsi_dev;
2751         unsigned char cdb[6];
2752         uint8_t sense[SCSI_SENSE_BUFFERSIZE];
2753         int rc, i;
2754
2755         TRACE_ENTRY();
2756
2757         if (dev->scsi_dev == NULL)
2758                 goto out;
2759
2760         scsi_dev = dev->scsi_dev;
2761
2762         for (i = 0; i < 5; i++) {
2763                 memset(cdb, 0, sizeof(cdb));
2764                 cdb[0] = RELEASE;
2765                 cdb[1] = (scsi_dev->scsi_level <= SCSI_2) ?
2766                     ((scsi_dev->lun << 5) & 0xe0) : 0;
2767
2768                 memset(sense, 0, sizeof(sense));
2769
2770                 TRACE(TRACE_DEBUG | TRACE_SCSI, "%s", "Sending RELEASE req to "
2771                         "SCSI mid-level");
2772                 rc = scsi_execute(scsi_dev, cdb, SCST_DATA_NONE, NULL, 0,
2773                                 sense, 15, 0, 0
2774 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,29)
2775                                 , NULL
2776 #endif
2777                                 );
2778                 TRACE_DBG("MODE_SENSE done: %x", rc);
2779
2780                 if (scsi_status_is_good(rc)) {
2781                         break;
2782                 } else {
2783                         PRINT_ERROR("RELEASE failed: %d", rc);
2784                         PRINT_BUFFER("RELEASE sense", sense, sizeof(sense));
2785                         scst_check_internal_sense(dev, rc, sense,
2786                                 sizeof(sense));
2787                 }
2788         }
2789
2790 out:
2791         TRACE_EXIT();
2792         return;
2793 }
2794 #endif /* LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18) */
2795
2796 /* scst_mutex supposed to be held */
2797 static void scst_clear_reservation(struct scst_tgt_dev *tgt_dev)
2798 {
2799         struct scst_device *dev = tgt_dev->dev;
2800         int release = 0;
2801
2802         TRACE_ENTRY();
2803
2804         spin_lock_bh(&dev->dev_lock);
2805         if (dev->dev_reserved &&
2806             !test_bit(SCST_TGT_DEV_RESERVED, &tgt_dev->tgt_dev_flags)) {
2807                 /* This is one who holds the reservation */
2808                 struct scst_tgt_dev *tgt_dev_tmp;
2809                 list_for_each_entry(tgt_dev_tmp, &dev->dev_tgt_dev_list,
2810                                     dev_tgt_dev_list_entry) {
2811                         clear_bit(SCST_TGT_DEV_RESERVED,
2812                                     &tgt_dev_tmp->tgt_dev_flags);
2813                 }
2814                 dev->dev_reserved = 0;
2815                 release = 1;
2816         }
2817         spin_unlock_bh(&dev->dev_lock);
2818
2819         if (release)
2820                 scst_send_release(dev);
2821
2822         TRACE_EXIT();
2823         return;
2824 }
2825
2826 struct scst_session *scst_alloc_session(struct scst_tgt *tgt, gfp_t gfp_mask,
2827         const char *initiator_name)
2828 {
2829         struct scst_session *sess;
2830         int i;
2831         int len;
2832         char *nm;
2833
2834         TRACE_ENTRY();
2835
2836 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 17)
2837         sess = kmem_cache_alloc(scst_sess_cachep, gfp_mask);
2838 #else
2839         sess = kmem_cache_zalloc(scst_sess_cachep, gfp_mask);
2840 #endif
2841         if (sess == NULL) {
2842                 TRACE(TRACE_OUT_OF_MEM, "%s",
2843                       "Allocation of scst_session failed");
2844                 goto out;
2845         }
2846 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 17)
2847         memset(sess, 0, sizeof(*sess));
2848 #endif
2849
2850         sess->init_phase = SCST_SESS_IPH_INITING;
2851         sess->shut_phase = SCST_SESS_SPH_READY;
2852         atomic_set(&sess->refcnt, 0);
2853         for (i = 0; i < TGT_DEV_HASH_SIZE; i++) {
2854                 struct list_head *sess_tgt_dev_list_head =
2855                          &sess->sess_tgt_dev_list_hash[i];
2856                 INIT_LIST_HEAD(sess_tgt_dev_list_head);
2857         }
2858         spin_lock_init(&sess->sess_list_lock);
2859         INIT_LIST_HEAD(&sess->sess_cmd_list);
2860         sess->tgt = tgt;
2861         INIT_LIST_HEAD(&sess->init_deferred_cmd_list);
2862         INIT_LIST_HEAD(&sess->init_deferred_mcmd_list);
2863 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 20))
2864         INIT_DELAYED_WORK(&sess->hw_pending_work,
2865                 (void (*)(struct work_struct *))scst_hw_pending_work_fn);
2866 #else
2867         INIT_WORK(&sess->hw_pending_work, scst_hw_pending_work_fn, sess);
2868 #endif
2869
2870 #ifdef CONFIG_SCST_MEASURE_LATENCY
2871         spin_lock_init(&sess->lat_lock);
2872 #endif
2873
2874         len = strlen(initiator_name);
2875         nm = kmalloc(len + 1, gfp_mask);
2876         if (nm == NULL) {
2877                 PRINT_ERROR("%s", "Unable to allocate sess->initiator_name");
2878                 goto out_free;
2879         }
2880
2881         strcpy(nm, initiator_name);
2882         sess->initiator_name = nm;
2883
2884 out:
2885         TRACE_EXIT();
2886         return sess;
2887
2888 out_free:
2889         kmem_cache_free(scst_sess_cachep, sess);
2890         sess = NULL;
2891         goto out;
2892 }
2893
2894 void scst_free_session(struct scst_session *sess)
2895 {
2896         TRACE_ENTRY();
2897
2898         mutex_lock(&scst_mutex);
2899
2900         TRACE_DBG("Removing sess %p from the list", sess);
2901         list_del(&sess->sess_list_entry);
2902         TRACE_DBG("Removing session %p from acg %s", sess, sess->acg->acg_name);
2903         list_del(&sess->acg_sess_list_entry);
2904
2905         scst_sess_free_tgt_devs(sess);
2906
2907         /* Called under lock to protect from too early tgt release */
2908         wake_up_all(&sess->tgt->unreg_waitQ);
2909
2910         mutex_unlock(&scst_mutex);
2911
2912         scst_sess_sysfs_put(sess); /* must not be called under scst_mutex */
2913
2914         TRACE_EXIT();
2915         return;
2916 }
2917
2918 void scst_release_session(struct scst_session *sess)
2919 {
2920         TRACE_ENTRY();
2921
2922         kfree(sess->initiator_name);
2923         kmem_cache_free(scst_sess_cachep, sess);
2924
2925         TRACE_EXIT();
2926         return;
2927 }
2928
2929 void scst_free_session_callback(struct scst_session *sess)
2930 {
2931         struct completion *c;
2932
2933         TRACE_ENTRY();
2934
2935         TRACE_DBG("Freeing session %p", sess);
2936
2937         cancel_delayed_work_sync(&sess->hw_pending_work);
2938
2939         c = sess->shutdown_compl;
2940
2941         if (sess->unreg_done_fn) {
2942                 TRACE_DBG("Calling unreg_done_fn(%p)", sess);
2943                 sess->unreg_done_fn(sess);
2944                 TRACE_DBG("%s", "unreg_done_fn() returned");
2945         }
2946         scst_free_session(sess);
2947
2948         if (c)
2949                 complete_all(c);
2950
2951         TRACE_EXIT();
2952         return;
2953 }
2954
2955 void scst_sched_session_free(struct scst_session *sess)
2956 {
2957         unsigned long flags;
2958
2959         TRACE_ENTRY();
2960
2961         if (sess->shut_phase != SCST_SESS_SPH_SHUTDOWN) {
2962                 PRINT_CRIT_ERROR("session %p is going to shutdown with unknown "
2963                         "shut phase %lx", sess, sess->shut_phase);
2964                 sBUG();
2965         }
2966
2967         spin_lock_irqsave(&scst_mgmt_lock, flags);
2968         TRACE_DBG("Adding sess %p to scst_sess_shut_list", sess);
2969         list_add_tail(&sess->sess_shut_list_entry, &scst_sess_shut_list);
2970         spin_unlock_irqrestore(&scst_mgmt_lock, flags);
2971
2972         wake_up(&scst_mgmt_waitQ);
2973
2974         TRACE_EXIT();
2975         return;
2976 }
2977
2978 void scst_cmd_get(struct scst_cmd *cmd)
2979 {
2980         __scst_cmd_get(cmd);
2981 }
2982 EXPORT_SYMBOL(scst_cmd_get);
2983
2984 void scst_cmd_put(struct scst_cmd *cmd)
2985 {
2986         __scst_cmd_put(cmd);
2987 }
2988 EXPORT_SYMBOL(scst_cmd_put);
2989
2990 struct scst_cmd *scst_alloc_cmd(gfp_t gfp_mask)
2991 {
2992         struct scst_cmd *cmd;
2993
2994         TRACE_ENTRY();
2995
2996 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 17)
2997         cmd = kmem_cache_alloc(scst_cmd_cachep, gfp_mask);
2998 #else
2999         cmd = kmem_cache_zalloc(scst_cmd_cachep, gfp_mask);
3000 #endif
3001         if (cmd == NULL) {
3002                 TRACE(TRACE_OUT_OF_MEM, "%s", "Allocation of scst_cmd failed");
3003                 goto out;
3004         }
3005 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 17)
3006         memset(cmd, 0, sizeof(*cmd));
3007 #endif
3008
3009         cmd->state = SCST_CMD_STATE_INIT_WAIT;
3010         cmd->start_time = jiffies;
3011         atomic_set(&cmd->cmd_ref, 1);
3012         cmd->cmd_lists = &scst_main_cmd_lists;
3013         INIT_LIST_HEAD(&cmd->mgmt_cmd_list);
3014         cmd->queue_type = SCST_CMD_QUEUE_SIMPLE;
3015         cmd->timeout = SCST_DEFAULT_TIMEOUT;
3016         cmd->retries = 0;
3017         cmd->data_len = -1;
3018         cmd->is_send_status = 1;
3019         cmd->resp_data_len = -1;
3020
3021         cmd->dbl_ua_orig_data_direction = SCST_DATA_UNKNOWN;
3022         cmd->dbl_ua_orig_resp_data_len = -1;
3023
3024 out:
3025         TRACE_EXIT();
3026         return cmd;
3027 }
3028
3029 static void scst_destroy_put_cmd(struct scst_cmd *cmd)
3030 {
3031         scst_sess_put(cmd->sess);
3032
3033         /*
3034          * At this point tgt_dev can be dead, but the pointer remains non-NULL
3035          */
3036         if (likely(cmd->tgt_dev != NULL))
3037                 __scst_put();
3038
3039         scst_destroy_cmd(cmd);
3040         return;
3041 }
3042
3043 /* No locks supposed to be held */
3044 void scst_free_cmd(struct scst_cmd *cmd)
3045 {
3046         int destroy = 1;
3047
3048         TRACE_ENTRY();
3049
3050         TRACE_DBG("Freeing cmd %p (tag %llu)",
3051                   cmd, (long long unsigned int)cmd->tag);
3052
3053         if (unlikely(test_bit(SCST_CMD_ABORTED, &cmd->cmd_flags))) {
3054                 TRACE_MGMT_DBG("Freeing aborted cmd %p (scst_cmd_count %d)",
3055                         cmd, atomic_read(&scst_cmd_count));
3056         }
3057
3058         sBUG_ON(cmd->inc_blocking || cmd->needs_unblocking ||
3059                 cmd->dec_on_dev_needed);
3060
3061 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18)
3062 #if defined(CONFIG_SCST_EXTRACHECKS)
3063         if (cmd->scsi_req) {
3064                 PRINT_ERROR("%s: %s", __func__, "Cmd with unfreed "
3065                         "scsi_req!");
3066                 scst_release_request(cmd);
3067         }
3068 #endif
3069 #endif
3070
3071         /*
3072          * Target driver can already free sg buffer before calling
3073          * scst_tgt_cmd_done(). E.g., scst_local has to do that.
3074          */
3075         if (!cmd->tgt_data_buf_alloced)
3076                 scst_check_restore_sg_buff(cmd);
3077
3078         if ((cmd->tgtt->on_free_cmd != NULL) && likely(!cmd->internal)) {
3079                 TRACE_DBG("Calling target's on_free_cmd(%p)", cmd);
3080                 scst_set_cur_start(cmd);
3081                 cmd->tgtt->on_free_cmd(cmd);
3082                 scst_set_tgt_on_free_time(cmd);
3083                 TRACE_DBG("%s", "Target's on_free_cmd() returned");
3084         }
3085
3086         if (likely(cmd->dev != NULL)) {
3087                 struct scst_dev_type *handler = cmd->dev->handler;
3088                 if (handler->on_free_cmd != NULL) {
3089                         TRACE_DBG("Calling dev handler %s on_free_cmd(%p)",
3090                                 handler->name, cmd);
3091                         scst_set_cur_start(cmd);
3092                         handler->on_free_cmd(cmd);
3093                         scst_set_dev_on_free_time(cmd);
3094                         TRACE_DBG("Dev handler %s on_free_cmd() returned",
3095                                 handler->name);
3096                 }
3097         }
3098
3099         scst_release_space(cmd);
3100
3101         if (unlikely(cmd->sense != NULL)) {
3102                 TRACE_MEM("Releasing sense %p (cmd %p)", cmd->sense, cmd);
3103                 mempool_free(cmd->sense, scst_sense_mempool);
3104                 cmd->sense = NULL;
3105         }
3106
3107         if (likely(cmd->tgt_dev != NULL)) {
3108 #ifdef CONFIG_SCST_EXTRACHECKS
3109                 if (unlikely(!cmd->sent_for_exec) && !cmd->internal) {
3110                         PRINT_ERROR("Finishing not executed cmd %p (opcode "
3111                             "%d, target %s, LUN %lld, sn %d, expected_sn %d)",
3112                             cmd, cmd->cdb[0], cmd->tgtt->name,
3113                             (long long unsigned int)cmd->lun,
3114                             cmd->sn, cmd->tgt_dev->expected_sn);
3115                         scst_unblock_deferred(cmd->tgt_dev, cmd);
3116                 }
3117 #endif
3118
3119                 if (unlikely(cmd->out_of_sn)) {
3120                         TRACE_SN("Out of SN cmd %p (tag %llu, sn %d), "
3121                                 "destroy=%d", cmd,
3122                                 (long long unsigned int)cmd->tag,
3123                                 cmd->sn, destroy);
3124                         destroy = test_and_set_bit(SCST_CMD_CAN_BE_DESTROYED,
3125                                         &cmd->cmd_flags);
3126                 }
3127         }
3128
3129         if (likely(destroy))
3130                 scst_destroy_put_cmd(cmd);
3131
3132         TRACE_EXIT();
3133         return;
3134 }
3135
3136 /* No locks supposed to be held. */
3137 void scst_check_retries(struct scst_tgt *tgt)
3138 {
3139         int need_wake_up = 0;
3140
3141         TRACE_ENTRY();
3142
3143         /*
3144          * We don't worry about overflow of finished_cmds, because we check
3145          * only for its change.
3146          */
3147         atomic_inc(&tgt->finished_cmds);
3148         /* See comment in scst_queue_retry_cmd() */
3149         smp_mb__after_atomic_inc();
3150         if (unlikely(tgt->retry_cmds > 0)) {
3151                 struct scst_cmd *c, *tc;
3152                 unsigned long flags;
3153
3154                 TRACE_RETRY("Checking retry cmd list (retry_cmds %d)",
3155                       tgt->retry_cmds);
3156
3157                 spin_lock_irqsave(&tgt->tgt_lock, flags);
3158                 list_for_each_entry_safe(c, tc, &tgt->retry_cmd_list,
3159                                 cmd_list_entry) {
3160                         tgt->retry_cmds--;
3161
3162                         TRACE_RETRY("Moving retry cmd %p to head of active "
3163                                 "cmd list (retry_cmds left %d)",
3164                                 c, tgt->retry_cmds);
3165                         spin_lock(&c->cmd_lists->cmd_list_lock);
3166                         list_move(&c->cmd_list_entry,
3167                                   &c->cmd_lists->active_cmd_list);
3168                         wake_up(&c->cmd_lists->cmd_list_waitQ);
3169                         spin_unlock(&c->cmd_lists->cmd_list_lock);
3170
3171                         need_wake_up++;
3172                         if (need_wake_up >= 2) /* "slow start" */
3173                                 break;
3174                 }
3175                 spin_unlock_irqrestore(&tgt->tgt_lock, flags);
3176         }
3177
3178         TRACE_EXIT();
3179         return;
3180 }
3181
3182 static void scst_tgt_retry_timer_fn(unsigned long arg)
3183 {
3184         struct scst_tgt *tgt = (struct scst_tgt *)arg;
3185         unsigned long flags;
3186
3187         TRACE_RETRY("Retry timer expired (retry_cmds %d)", tgt->retry_cmds);
3188
3189         spin_lock_irqsave(&tgt->tgt_lock, flags);
3190         tgt->retry_timer_active = 0;
3191         spin_unlock_irqrestore(&tgt->tgt_lock, flags);
3192
3193         scst_check_retries(tgt);
3194
3195         TRACE_EXIT();
3196         return;
3197 }
3198
3199 struct scst_mgmt_cmd *scst_alloc_mgmt_cmd(gfp_t gfp_mask)
3200 {
3201         struct scst_mgmt_cmd *mcmd;
3202
3203         TRACE_ENTRY();
3204
3205         mcmd = mempool_alloc(scst_mgmt_mempool, gfp_mask);
3206         if (mcmd == NULL) {
3207                 PRINT_CRIT_ERROR("%s", "Allocation of management command "
3208                         "failed, some commands and their data could leak");
3209                 goto out;
3210         }
3211         memset(mcmd, 0, sizeof(*mcmd));
3212
3213 out:
3214         TRACE_EXIT();
3215         return mcmd;
3216 }
3217
3218 void scst_free_mgmt_cmd(struct scst_mgmt_cmd *mcmd)
3219 {
3220         unsigned long flags;
3221
3222         TRACE_ENTRY();
3223
3224         spin_lock_irqsave(&mcmd->sess->sess_list_lock, flags);
3225         atomic_dec(&mcmd->sess->sess_cmd_count);
3226         spin_unlock_irqrestore(&mcmd->sess->sess_list_lock, flags);
3227
3228         scst_sess_put(mcmd->sess);
3229
3230         if (mcmd->mcmd_tgt_dev != NULL)
3231                 __scst_put();
3232
3233         mempool_free(mcmd, scst_mgmt_mempool);
3234
3235         TRACE_EXIT();
3236         return;
3237 }
3238
3239 #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 18)
3240 int scst_alloc_request(struct scst_cmd *cmd)
3241 {
3242         int res = 0;
3243         struct scsi_request *req;
3244         int gm = scst_cmd_atomic(cmd) ? GFP_ATOMIC : GFP_KERNEL;
3245
3246         TRACE_ENTRY();
3247
3248         /* cmd->dev->scsi_dev must be non-NULL here */
3249         req = scsi_allocate_request(cmd->dev->scsi_dev, gm);
3250         if (req == NULL) {
3251                 TRACE(TRACE_OUT_OF_MEM, "%s",
3252                       "Allocation of scsi_request failed");
3253                 res = -ENOMEM;
3254                 goto out;
3255         }
3256
3257         cmd->scsi_req = req;
3258
3259         memcpy(req->sr_cmnd, cmd->cdb, cmd->cdb_len);
3260         req->sr_cmd_len = cmd->cdb_len;
3261         req->sr_data_direction = cmd->data_direction;
3262         req->sr_use_sg = cmd->sg_cnt;
3263         req->sr_bufflen = cmd->bufflen;
3264         req->sr_buffer = cmd->sg;
3265         req->sr_request->rq_disk = cmd->dev->rq_disk;
3266         req->sr_sense_buffer[0] = 0;
3267
3268         cmd->scsi_req->upper_private_data = cmd;
3269
3270 out:
3271         TRACE_EXIT();
3272         return res;
3273 }
3274
3275 void scst_release_request(struct scst_cmd *cmd)
3276 {
3277         scsi_release_request(cmd->scsi_req);
3278         cmd->scsi_req = NULL;
3279 }
3280 #endif
3281
3282 static bool is_report_sg_limitation(void)
3283 {
3284 #if defined(CONFIG_SCST_DEBUG) || defined(CONFIG_SCST_TRACING)
3285         return (trace_flag & TRACE_OUT_OF_MEM) != 0;
3286 #else
3287         return false;
3288 #endif
3289 }
3290
3291 int scst_alloc_space(struct scst_cmd *cmd)
3292 {
3293         gfp_t gfp_mask;
3294         int res = -ENOMEM;
3295         int atomic = scst_cmd_atomic(cmd);
3296         int flags;
3297         struct scst_tgt_dev *tgt_dev = cmd->tgt_dev;
3298         static int ll;
3299
3300         TRACE_ENTRY();
3301
3302         gfp_mask = tgt_dev->gfp_mask | (atomic ? GFP_ATOMIC : GFP_KERNEL);
3303
3304         flags = atomic ? SGV_POOL_NO_ALLOC_ON_CACHE_MISS : 0;
3305         if (cmd->no_sgv)
3306                 flags |= SGV_POOL_ALLOC_NO_CACHED;
3307
3308         cmd->sg = sgv_pool_alloc(tgt_dev->pool, cmd->bufflen, gfp_mask, flags,
3309                         &cmd->sg_cnt, &cmd->sgv, &cmd->dev->dev_mem_lim, NULL);
3310         if (cmd->sg == NULL)
3311                 goto out;
3312
3313         if (unlikely(cmd->sg_cnt > tgt_dev->max_sg_cnt)) {
3314                 if ((ll < 10) || is_report_sg_limitation()) {
3315                         PRINT_INFO("Unable to complete command due to "
3316                                 "SG IO count limitation (requested %d, "
3317                                 "available %d, tgt lim %d)", cmd->sg_cnt,
3318                                 tgt_dev->max_sg_cnt, cmd->tgt->sg_tablesize);
3319                         ll++;
3320                 }
3321                 goto out_sg_free;
3322         }
3323
3324         if (cmd->data_direction != SCST_DATA_BIDI)
3325                 goto success;
3326
3327         cmd->in_sg = sgv_pool_alloc(tgt_dev->pool, cmd->in_bufflen, gfp_mask,
3328                          flags, &cmd->in_sg_cnt, &cmd->in_sgv,
3329                          &cmd->dev->dev_mem_lim, NULL);
3330         if (cmd->in_sg == NULL)
3331                 goto out_sg_free;
3332
3333         if (unlikely(cmd->in_sg_cnt > tgt_dev->max_sg_cnt)) {
3334                 if ((ll < 10)  || is_report_sg_limitation()) {
3335                         PRINT_INFO("Unable to complete command due to "
3336                                 "SG IO count limitation (IN buffer, requested "
3337                                 "%d, available %d, tgt lim %d)", cmd->in_sg_cnt,
3338                                 tgt_dev->max_sg_cnt, cmd->tgt->sg_tablesize);
3339                         ll++;
3340                 }
3341                 goto out_in_sg_free;
3342         }
3343
3344 success:
3345         res = 0;
3346
3347 out:
3348         TRACE_EXIT();
3349         return res;
3350
3351 out_in_sg_free:
3352         sgv_pool_free(cmd->in_sgv, &cmd->dev->dev_mem_lim);
3353         cmd->in_sgv = NULL;
3354         cmd->in_sg = NULL;
3355         cmd->in_sg_cnt = 0;
3356
3357 out_sg_free:
3358         sgv_pool_free(cmd->sgv, &cmd->dev->dev_mem_lim);
3359         cmd->sgv = NULL;
3360         cmd->sg = NULL;
3361         cmd->sg_cnt = 0;
3362         goto out;
3363 }
3364
3365 static void scst_release_space(struct scst_cmd *cmd)
3366 {
3367         TRACE_ENTRY();
3368
3369         if (cmd->sgv == NULL)
3370                 goto out;
3371
3372         if (cmd->tgt_data_buf_alloced || cmd->dh_data_buf_alloced) {
3373                 TRACE_MEM("%s", "*data_buf_alloced set, returning");
3374                 goto out;
3375         }
3376
3377         sgv_pool_free(cmd->sgv, &cmd->dev->dev_mem_lim);
3378         cmd->sgv = NULL;
3379         cmd->sg_cnt = 0;
3380         cmd->sg = NULL;
3381         cmd->bufflen = 0;
3382         cmd->data_len = 0;
3383
3384         if (cmd->in_sgv != NULL) {
3385                 sgv_pool_free(cmd->in_sgv, &cmd->dev->dev_mem_lim);
3386                 cmd->in_sgv = NULL;
3387                 cmd->in_sg_cnt = 0;
3388                 cmd->in_sg = NULL;
3389                 cmd->in_bufflen = 0;
3390         }
3391
3392 out:
3393         TRACE_EXIT();
3394         return;
3395 }
3396
3397 #if !((LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 30)) && defined(SCSI_EXEC_REQ_FIFO_DEFINED))
3398
3399 /*
3400  * Can switch to the next dst_sg element, so, to copy to strictly only
3401  * one dst_sg element, it must be either last in the chain, or
3402  * copy_len == dst_sg->length.
3403  */
3404 static int sg_copy_elem(struct scatterlist **pdst_sg, size_t *pdst_len,
3405                         size_t *pdst_offs, struct scatterlist *src_sg,
3406                         size_t copy_len,
3407                         enum km_type d_km_type, enum km_type s_km_type)
3408 {
3409         int res = 0;
3410         struct scatterlist *dst_sg;
3411         size_t src_len, dst_len, src_offs, dst_offs;
3412         struct page *src_page, *dst_page;
3413
3414         dst_sg = *pdst_sg;
3415         dst_len = *pdst_len;
3416         dst_offs = *pdst_offs;
3417         dst_page = sg_page(dst_sg);
3418
3419         src_page = sg_page(src_sg);
3420         src_len = src_sg->length;
3421         src_offs = src_sg->offset;
3422
3423         do {
3424                 void *saddr, *daddr;
3425                 size_t n;
3426
3427                 saddr = kmap_atomic(src_page +
3428                                          (src_offs >> PAGE_SHIFT), s_km_type) +
3429                                     (src_offs & ~PAGE_MASK);
3430                 daddr = kmap_atomic(dst_page +
3431                                         (dst_offs >> PAGE_SHIFT), d_km_type) +
3432                                     (dst_offs & ~PAGE_MASK);
3433
3434                 if (((src_offs & ~PAGE_MASK) == 0) &&
3435                     ((dst_offs & ~PAGE_MASK) == 0) &&
3436                     (src_len >= PAGE_SIZE) && (dst_len >= PAGE_SIZE) &&
3437                     (copy_len >= PAGE_SIZE)) {
3438                         copy_page(daddr, saddr);
3439                         n = PAGE_SIZE;
3440                 } else {
3441                         n = min_t(size_t, PAGE_SIZE - (dst_offs & ~PAGE_MASK),
3442                                           PAGE_SIZE - (src_offs & ~PAGE_MASK));
3443                         n = min(n, src_len);
3444                         n = min(n, dst_len);
3445                         n = min_t(size_t, n, copy_len);
3446                         memcpy(daddr, saddr, n);
3447                 }
3448                 dst_offs += n;
3449                 src_offs += n;
3450
3451                 kunmap_atomic(saddr, s_km_type);
3452                 kunmap_atomic(daddr, d_km_type);
3453
3454                 res += n;
3455                 copy_len -= n;
3456                 if (copy_len == 0)
3457                         goto out;
3458
3459                 src_len -= n;
3460                 dst_len -= n;
3461                 if (dst_len == 0) {
3462                         dst_sg = sg_next(dst_sg);
3463                         if (dst_sg == NULL)
3464                                 goto out;
3465                         dst_page = sg_page(dst_sg);
3466                         dst_len = dst_sg->length;
3467                         dst_offs = dst_sg->offset;
3468                 }
3469         } while (src_len > 0);
3470
3471 out:
3472         *pdst_sg = dst_sg;
3473         *pdst_len = dst_len;
3474         *pdst_offs = dst_offs;
3475         return res;
3476 }
3477
3478 /**
3479  * sg_copy - copy one SG vector to another
3480  * @dst_sg:     destination SG
3481  * @src_sg:     source SG
3482  * @nents_to_copy: maximum number of entries to copy
3483  * @copy_len:   maximum amount of data to copy. If 0, then copy all.
3484  * @d_km_type:  kmap_atomic type for the destination SG
3485  * @s_km_type:  kmap_atomic type for the source SG
3486  *
3487  * Description:
3488  *    Data from the source SG vector will be copied to the destination SG
3489  *    vector. End of the vectors will be determined by sg_next() returning
3490  *    NULL. Returns number of bytes copied.
3491  */
3492 static int sg_copy(struct scatterlist *dst_sg, struct scatterlist *src_sg,
3493             int nents_to_copy, size_t copy_len,
3494             enum km_type d_km_type, enum km_type s_km_type)
3495 {
3496         int res = 0;
3497         size_t dst_len, dst_offs;
3498
3499         if (copy_len == 0)
3500                 copy_len = 0x7FFFFFFF; /* copy all */
3501
3502         if (nents_to_copy == 0)
3503                 nents_to_copy = 0x7FFFFFFF; /* copy all */
3504
3505         dst_len = dst_sg->length;
3506         dst_offs = dst_sg->offset;
3507
3508         do {
3509                 int copied = sg_copy_elem(&dst_sg, &dst_len, &dst_offs,
3510                                 src_sg, copy_len, d_km_type, s_km_type);
3511                 copy_len -= copied;
3512                 res += copied;
3513                 if ((copy_len == 0) || (dst_sg == NULL))
3514                         goto out;
3515
3516                 nents_to_copy--;
3517                 if (nents_to_copy == 0)
3518                         goto out;
3519
3520                 src_sg = sg_next(src_sg);
3521         } while (src_sg != NULL);
3522
3523 out:
3524         return res;
3525 }
3526
3527 #endif /* !((LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 30)) && defined(SCSI_EXEC_REQ_FIFO_DEFINED)) */
3528
3529 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 30)) && defined(SCSI_EXEC_REQ_FIFO_DEFINED)
3530 static void scsi_end_async(struct request *req, int error)
3531 {
3532         struct scsi_io_context *sioc = req->end_io_data;
3533
3534         TRACE_DBG("sioc %p, cmd %p", sioc, sioc->data);
3535
3536         if (sioc->done)
3537 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2, 6, 30)
3538                 sioc->done(sioc->data, sioc->sense, req->errors, req->data_len);
3539 #else
3540                 sioc->done(sioc->data, sioc->sense, req->errors, req->resid_len);
3541 #endif
3542
3543         if (!sioc->full_cdb_used)
3544                 kmem_cache_free(scsi_io_context_cache, sioc);
3545         else
3546                 kfree(sioc);
3547
3548         __blk_put_request(req->q, req);
3549         return;
3550 }
3551
3552 /**
3553  * scst_scsi_exec_async - executes a SCSI command in pass-through mode
3554  * @cmd:        scst command
3555  * @done:       callback function when done
3556  */
3557 int scst_scsi_exec_async(struct scst_cmd *cmd,
3558                        void (*done)(void *, char *, int, int))
3559 {
3560         int res = 0;
3561         struct request_queue *q = cmd->dev->scsi_dev->request_queue;
3562         struct request *rq;
3563         struct scsi_io_context *sioc;
3564         int write = (cmd->data_direction & SCST_DATA_WRITE) ? WRITE : READ;
3565         gfp_t gfp = scst_cmd_atomic(cmd) ? GFP_ATOMIC : GFP_KERNEL;
3566         int cmd_len = cmd->cdb_len;
3567
3568         if (cmd->ext_cdb_len == 0) {
3569                 TRACE_DBG("Simple CDB (cmd_len %d)", cmd_len);
3570                 sioc = kmem_cache_zalloc(scsi_io_context_cache, gfp);
3571                 if (sioc == NULL) {
3572                         res = -ENOMEM;
3573                         goto out;
3574                 }
3575         } else {
3576                 cmd_len += cmd->ext_cdb_len;
3577
3578                 TRACE_DBG("Extended CDB (cmd_len %d)", cmd_len);
3579
3580                 sioc = kzalloc(sizeof(*sioc) + cmd_len, gfp);
3581                 if (sioc == NULL) {
3582                         res = -ENOMEM;
3583                         goto out;
3584                 }
3585
3586                 sioc->full_cdb_used = 1;
3587
3588                 memcpy(sioc->full_cdb, cmd->cdb, cmd->cdb_len);
3589                 memcpy(&sioc->full_cdb[cmd->cdb_len], cmd->ext_cdb,
3590                         cmd->ext_cdb_len);
3591         }
3592
3593         rq = blk_get_request(q, write, gfp);
3594         if (rq == NULL) {
3595                 res = -ENOMEM;
3596                 goto out_free_sioc;
3597         }
3598
3599         rq->cmd_type = REQ_TYPE_BLOCK_PC;
3600         rq->cmd_flags |= REQ_QUIET;
3601
3602         if (cmd->sg != NULL) {
3603                 res = blk_rq_map_kern_sg(rq, cmd->sg, cmd->sg_cnt, gfp);
3604                 if (res) {
3605                         TRACE_DBG("blk_rq_map_kern_sg() failed: %d", res);
3606                         goto out_free_rq;
3607                 }
3608         }
3609
3610         if (cmd->data_direction == SCST_DATA_BIDI) {
3611                 struct request *next_rq;
3612
3613                 if (!test_bit(QUEUE_FLAG_BIDI, &q->queue_flags)) {
3614                         res = -EOPNOTSUPP;
3615                         goto out_free_unmap;
3616                 }
3617
3618                 next_rq = blk_get_request(q, READ, gfp);
3619                 if (next_rq == NULL) {
3620                         res = -ENOMEM;
3621                         goto out_free_unmap;
3622                 }
3623                 rq->next_rq = next_rq;
3624                 next_rq->cmd_type = rq->cmd_type;
3625
3626                 res = blk_rq_map_kern_sg(next_rq, cmd->in_sg,
3627                         cmd->in_sg_cnt, gfp);
3628                 if (res != 0)
3629                         goto out_free_unmap;
3630         }
3631
3632         TRACE_DBG("sioc %p, cmd %p", sioc, cmd);
3633
3634         sioc->data = cmd;
3635         sioc->done = done;
3636
3637         rq->cmd_len = cmd_len;
3638         if (cmd->ext_cdb_len == 0) {
3639                 memset(rq->cmd, 0, BLK_MAX_CDB); /* ATAPI hates garbage after CDB */
3640                 memcpy(rq->cmd, cmd->cdb, cmd->cdb_len);
3641         } else
3642                 rq->cmd = sioc->full_cdb;
3643
3644         rq->sense = sioc->sense;
3645         rq->sense_len = sizeof(sioc->sense);
3646         rq->timeout = cmd->timeout;
3647         rq->retries = cmd->retries;
3648         rq->end_io_data = sioc;
3649
3650         blk_execute_rq_nowait(rq->q, NULL, rq,
3651                 (cmd->queue_type == SCST_CMD_QUEUE_HEAD_OF_QUEUE), scsi_end_async);
3652 out:
3653         return res;
3654
3655 out_free_unmap:
3656         if (rq->next_rq != NULL) {
3657                 blk_put_request(rq->next_rq);
3658                 rq->next_rq = NULL;
3659         }
3660         blk_rq_unmap_kern_sg(rq, res);
3661
3662 out_free_rq:
3663         blk_put_request(rq);
3664
3665 out_free_sioc:
3666         if (!sioc->full_cdb_used)
3667                 kmem_cache_free(scsi_io_context_cache, sioc);
3668         else
3669                 kfree(sioc);
3670         goto out;
3671 }
3672
3673 #endif /* LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 30) && defined(SCSI_EXEC_REQ_FIFO_DEFINED) */
3674
3675 void scst_copy_sg(struct scst_cmd *cmd, enum scst_sg_copy_dir copy_dir)
3676 {
3677         struct scatterlist *src_sg, *dst_sg;
3678         unsigned int to_copy;
3679         int atomic = scst_cmd_atomic(cmd);
3680
3681         TRACE_ENTRY();
3682
3683         if (copy_dir == SCST_SG_COPY_FROM_TARGET) {
3684                 if (cmd->data_direction != SCST_DATA_BIDI) {
3685                         src_sg = cmd->tgt_sg;
3686                         dst_sg = cmd->sg;
3687                         to_copy = cmd->bufflen;
3688                 } else {
3689                         TRACE_MEM("BIDI cmd %p", cmd);
3690                         src_sg = cmd->tgt_in_sg;
3691                         dst_sg = cmd->in_sg;
3692                         to_copy = cmd->in_bufflen;
3693                 }
3694         } else {
3695                 src_sg = cmd->sg;
3696                 dst_sg = cmd->tgt_sg;
3697                 to_copy = cmd->resp_data_len;
3698         }
3699
3700         TRACE_MEM("cmd %p, copy_dir %d, src_sg %p, dst_sg %p, to_copy %lld",
3701                 cmd, copy_dir, src_sg, dst_sg, (long long)to_copy);
3702
3703         if (unlikely(src_sg == NULL) || unlikely(dst_sg == NULL)) {
3704                 /*
3705                  * It can happened, e.g., with scst_user for cmd with delay
3706                  * alloc, which failed with Check Condition.
3707                  */
3708                 goto out;
3709         }
3710
3711         sg_copy(dst_sg, src_sg, 0, to_copy,
3712                 atomic ? KM_SOFTIRQ0 : KM_USER0,
3713                 atomic ? KM_SOFTIRQ1 : KM_USER1);
3714
3715 out:
3716         TRACE_EXIT();
3717         return;
3718 }
3719 EXPORT_SYMBOL(scst_copy_sg);
3720
3721 static const int SCST_CDB_LENGTH[8] = { 6, 10, 10, -1, 16, 12, -1, -1 };
3722
3723 #define SCST_CDB_GROUP(opcode)   ((opcode >> 5) & 0x7)
3724 #define SCST_GET_CDB_LEN(opcode) SCST_CDB_LENGTH[SCST_CDB_GROUP(opcode)]
3725
3726 int scst_get_cdb_len(const uint8_t *cdb)
3727 {
3728         return SCST_GET_CDB_LEN(cdb[0]);
3729 }
3730