]>
Commit | Line | Data |
---|---|---|
7996a778 MC |
1 | /* |
2 | * iSCSI lib functions | |
3 | * | |
4 | * Copyright (C) 2006 Red Hat, Inc. All rights reserved. | |
5 | * Copyright (C) 2004 - 2006 Mike Christie | |
6 | * Copyright (C) 2004 - 2005 Dmitry Yusupov | |
7 | * Copyright (C) 2004 - 2005 Alex Aizman | |
8 | * maintained by open-iscsi@googlegroups.com | |
9 | * | |
10 | * This program is free software; you can redistribute it and/or modify | |
11 | * it under the terms of the GNU General Public License as published by | |
12 | * the Free Software Foundation; either version 2 of the License, or | |
13 | * (at your option) any later version. | |
14 | * | |
15 | * This program is distributed in the hope that it will be useful, | |
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | * GNU General Public License for more details. | |
19 | * | |
20 | * You should have received a copy of the GNU General Public License | |
21 | * along with this program; if not, write to the Free Software | |
22 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | |
23 | */ | |
24 | #include <linux/types.h> | |
7996a778 MC |
25 | #include <linux/kfifo.h> |
26 | #include <linux/delay.h> | |
11836572 | 27 | #include <linux/log2.h> |
8eb00539 | 28 | #include <asm/unaligned.h> |
7996a778 MC |
29 | #include <net/tcp.h> |
30 | #include <scsi/scsi_cmnd.h> | |
31 | #include <scsi/scsi_device.h> | |
32 | #include <scsi/scsi_eh.h> | |
33 | #include <scsi/scsi_tcq.h> | |
34 | #include <scsi/scsi_host.h> | |
35 | #include <scsi/scsi.h> | |
36 | #include <scsi/iscsi_proto.h> | |
37 | #include <scsi/scsi_transport.h> | |
38 | #include <scsi/scsi_transport_iscsi.h> | |
39 | #include <scsi/libiscsi.h> | |
40 | ||
bd2199d4 EZ |
41 | static int iscsi_dbg_lib_conn; |
42 | module_param_named(debug_libiscsi_conn, iscsi_dbg_lib_conn, int, | |
43 | S_IRUGO | S_IWUSR); | |
44 | MODULE_PARM_DESC(debug_libiscsi_conn, | |
45 | "Turn on debugging for connections in libiscsi module. " | |
46 | "Set to 1 to turn on, and zero to turn off. Default is off."); | |
47 | ||
48 | static int iscsi_dbg_lib_session; | |
49 | module_param_named(debug_libiscsi_session, iscsi_dbg_lib_session, int, | |
50 | S_IRUGO | S_IWUSR); | |
51 | MODULE_PARM_DESC(debug_libiscsi_session, | |
52 | "Turn on debugging for sessions in libiscsi module. " | |
53 | "Set to 1 to turn on, and zero to turn off. Default is off."); | |
54 | ||
55 | static int iscsi_dbg_lib_eh; | |
56 | module_param_named(debug_libiscsi_eh, iscsi_dbg_lib_eh, int, | |
57 | S_IRUGO | S_IWUSR); | |
58 | MODULE_PARM_DESC(debug_libiscsi_eh, | |
59 | "Turn on debugging for error handling in libiscsi module. " | |
60 | "Set to 1 to turn on, and zero to turn off. Default is off."); | |
1b2c7af8 MC |
61 | |
62 | #define ISCSI_DBG_CONN(_conn, dbg_fmt, arg...) \ | |
63 | do { \ | |
bd2199d4 | 64 | if (iscsi_dbg_lib_conn) \ |
1b2c7af8 MC |
65 | iscsi_conn_printk(KERN_INFO, _conn, \ |
66 | "%s " dbg_fmt, \ | |
67 | __func__, ##arg); \ | |
68 | } while (0); | |
69 | ||
70 | #define ISCSI_DBG_SESSION(_session, dbg_fmt, arg...) \ | |
71 | do { \ | |
bd2199d4 EZ |
72 | if (iscsi_dbg_lib_session) \ |
73 | iscsi_session_printk(KERN_INFO, _session, \ | |
74 | "%s " dbg_fmt, \ | |
75 | __func__, ##arg); \ | |
76 | } while (0); | |
77 | ||
78 | #define ISCSI_DBG_EH(_session, dbg_fmt, arg...) \ | |
79 | do { \ | |
80 | if (iscsi_dbg_lib_eh) \ | |
1b2c7af8 MC |
81 | iscsi_session_printk(KERN_INFO, _session, \ |
82 | "%s " dbg_fmt, \ | |
83 | __func__, ##arg); \ | |
84 | } while (0); | |
85 | ||
77a23c21 MC |
86 | /* Serial Number Arithmetic, 32 bits, less than, RFC1982 */ |
87 | #define SNA32_CHECK 2147483648UL | |
7996a778 | 88 | |
77a23c21 MC |
89 | static int iscsi_sna_lt(u32 n1, u32 n2) |
90 | { | |
91 | return n1 != n2 && ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) || | |
92 | (n1 > n2 && (n2 - n1 < SNA32_CHECK))); | |
93 | } | |
94 | ||
95 | /* Serial Number Arithmetic, 32 bits, less than, RFC1982 */ | |
96 | static int iscsi_sna_lte(u32 n1, u32 n2) | |
97 | { | |
98 | return n1 == n2 || ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) || | |
99 | (n1 > n2 && (n2 - n1 < SNA32_CHECK))); | |
100 | } | |
101 | ||
32ae763e MC |
102 | inline void iscsi_conn_queue_work(struct iscsi_conn *conn) |
103 | { | |
104 | struct Scsi_Host *shost = conn->session->host; | |
105 | struct iscsi_host *ihost = shost_priv(shost); | |
106 | ||
1336aed1 MC |
107 | if (ihost->workq) |
108 | queue_work(ihost->workq, &conn->xmitwork); | |
32ae763e MC |
109 | } |
110 | EXPORT_SYMBOL_GPL(iscsi_conn_queue_work); | |
111 | ||
77a23c21 MC |
112 | void |
113 | iscsi_update_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr) | |
7996a778 MC |
114 | { |
115 | uint32_t max_cmdsn = be32_to_cpu(hdr->max_cmdsn); | |
116 | uint32_t exp_cmdsn = be32_to_cpu(hdr->exp_cmdsn); | |
117 | ||
77a23c21 MC |
118 | /* |
119 | * standard specifies this check for when to update expected and | |
120 | * max sequence numbers | |
121 | */ | |
122 | if (iscsi_sna_lt(max_cmdsn, exp_cmdsn - 1)) | |
123 | return; | |
124 | ||
125 | if (exp_cmdsn != session->exp_cmdsn && | |
126 | !iscsi_sna_lt(exp_cmdsn, session->exp_cmdsn)) | |
7996a778 MC |
127 | session->exp_cmdsn = exp_cmdsn; |
128 | ||
77a23c21 MC |
129 | if (max_cmdsn != session->max_cmdsn && |
130 | !iscsi_sna_lt(max_cmdsn, session->max_cmdsn)) { | |
131 | session->max_cmdsn = max_cmdsn; | |
132 | /* | |
133 | * if the window closed with IO queued, then kick the | |
134 | * xmit thread | |
135 | */ | |
3bbaaad9 | 136 | if (!list_empty(&session->leadconn->cmdqueue) || |
1336aed1 MC |
137 | !list_empty(&session->leadconn->mgmtqueue)) |
138 | iscsi_conn_queue_work(session->leadconn); | |
77a23c21 | 139 | } |
7996a778 | 140 | } |
77a23c21 | 141 | EXPORT_SYMBOL_GPL(iscsi_update_cmdsn); |
7996a778 | 142 | |
577577da MC |
143 | /** |
144 | * iscsi_prep_data_out_pdu - initialize Data-Out | |
145 | * @task: scsi command task | |
146 | * @r2t: R2T info | |
147 | * @hdr: iscsi data in pdu | |
148 | * | |
149 | * Notes: | |
150 | * Initialize Data-Out within this R2T sequence and finds | |
151 | * proper data_offset within this SCSI command. | |
152 | * | |
153 | * This function is called with connection lock taken. | |
154 | **/ | |
155 | void iscsi_prep_data_out_pdu(struct iscsi_task *task, struct iscsi_r2t_info *r2t, | |
156 | struct iscsi_data *hdr) | |
7996a778 | 157 | { |
9c19a7d0 | 158 | struct iscsi_conn *conn = task->conn; |
577577da MC |
159 | unsigned int left = r2t->data_length - r2t->sent; |
160 | ||
161 | task->hdr_len = sizeof(struct iscsi_data); | |
7996a778 MC |
162 | |
163 | memset(hdr, 0, sizeof(struct iscsi_data)); | |
577577da MC |
164 | hdr->ttt = r2t->ttt; |
165 | hdr->datasn = cpu_to_be32(r2t->datasn); | |
166 | r2t->datasn++; | |
7996a778 | 167 | hdr->opcode = ISCSI_OP_SCSI_DATA_OUT; |
577577da MC |
168 | memcpy(hdr->lun, task->lun, sizeof(hdr->lun)); |
169 | hdr->itt = task->hdr_itt; | |
170 | hdr->exp_statsn = r2t->exp_statsn; | |
171 | hdr->offset = cpu_to_be32(r2t->data_offset + r2t->sent); | |
172 | if (left > conn->max_xmit_dlength) { | |
7996a778 | 173 | hton24(hdr->dlength, conn->max_xmit_dlength); |
577577da | 174 | r2t->data_count = conn->max_xmit_dlength; |
7996a778 MC |
175 | hdr->flags = 0; |
176 | } else { | |
577577da MC |
177 | hton24(hdr->dlength, left); |
178 | r2t->data_count = left; | |
7996a778 MC |
179 | hdr->flags = ISCSI_FLAG_CMD_FINAL; |
180 | } | |
577577da | 181 | conn->dataout_pdus_cnt++; |
7996a778 | 182 | } |
577577da | 183 | EXPORT_SYMBOL_GPL(iscsi_prep_data_out_pdu); |
7996a778 | 184 | |
9c19a7d0 | 185 | static int iscsi_add_hdr(struct iscsi_task *task, unsigned len) |
004d6530 | 186 | { |
9c19a7d0 | 187 | unsigned exp_len = task->hdr_len + len; |
004d6530 | 188 | |
9c19a7d0 | 189 | if (exp_len > task->hdr_max) { |
004d6530 BH |
190 | WARN_ON(1); |
191 | return -EINVAL; | |
192 | } | |
193 | ||
194 | WARN_ON(len & (ISCSI_PAD_LEN - 1)); /* caller must pad the AHS */ | |
9c19a7d0 | 195 | task->hdr_len = exp_len; |
004d6530 BH |
196 | return 0; |
197 | } | |
198 | ||
38d1c069 BH |
199 | /* |
200 | * make an extended cdb AHS | |
201 | */ | |
9c19a7d0 | 202 | static int iscsi_prep_ecdb_ahs(struct iscsi_task *task) |
38d1c069 | 203 | { |
9c19a7d0 | 204 | struct scsi_cmnd *cmd = task->sc; |
38d1c069 BH |
205 | unsigned rlen, pad_len; |
206 | unsigned short ahslength; | |
207 | struct iscsi_ecdb_ahdr *ecdb_ahdr; | |
208 | int rc; | |
209 | ||
9c19a7d0 | 210 | ecdb_ahdr = iscsi_next_hdr(task); |
38d1c069 BH |
211 | rlen = cmd->cmd_len - ISCSI_CDB_SIZE; |
212 | ||
213 | BUG_ON(rlen > sizeof(ecdb_ahdr->ecdb)); | |
214 | ahslength = rlen + sizeof(ecdb_ahdr->reserved); | |
215 | ||
216 | pad_len = iscsi_padding(rlen); | |
217 | ||
9c19a7d0 | 218 | rc = iscsi_add_hdr(task, sizeof(ecdb_ahdr->ahslength) + |
38d1c069 BH |
219 | sizeof(ecdb_ahdr->ahstype) + ahslength + pad_len); |
220 | if (rc) | |
221 | return rc; | |
222 | ||
223 | if (pad_len) | |
224 | memset(&ecdb_ahdr->ecdb[rlen], 0, pad_len); | |
225 | ||
226 | ecdb_ahdr->ahslength = cpu_to_be16(ahslength); | |
227 | ecdb_ahdr->ahstype = ISCSI_AHSTYPE_CDB; | |
228 | ecdb_ahdr->reserved = 0; | |
229 | memcpy(ecdb_ahdr->ecdb, cmd->cmnd + ISCSI_CDB_SIZE, rlen); | |
230 | ||
1b2c7af8 MC |
231 | ISCSI_DBG_SESSION(task->conn->session, |
232 | "iscsi_prep_ecdb_ahs: varlen_cdb_len %d " | |
233 | "rlen %d pad_len %d ahs_length %d iscsi_headers_size " | |
234 | "%u\n", cmd->cmd_len, rlen, pad_len, ahslength, | |
235 | task->hdr_len); | |
38d1c069 BH |
236 | return 0; |
237 | } | |
238 | ||
9c19a7d0 | 239 | static int iscsi_prep_bidi_ahs(struct iscsi_task *task) |
c07d4444 | 240 | { |
9c19a7d0 | 241 | struct scsi_cmnd *sc = task->sc; |
c07d4444 BH |
242 | struct iscsi_rlength_ahdr *rlen_ahdr; |
243 | int rc; | |
244 | ||
9c19a7d0 MC |
245 | rlen_ahdr = iscsi_next_hdr(task); |
246 | rc = iscsi_add_hdr(task, sizeof(*rlen_ahdr)); | |
c07d4444 BH |
247 | if (rc) |
248 | return rc; | |
249 | ||
250 | rlen_ahdr->ahslength = | |
251 | cpu_to_be16(sizeof(rlen_ahdr->read_length) + | |
252 | sizeof(rlen_ahdr->reserved)); | |
253 | rlen_ahdr->ahstype = ISCSI_AHSTYPE_RLENGTH; | |
254 | rlen_ahdr->reserved = 0; | |
255 | rlen_ahdr->read_length = cpu_to_be32(scsi_in(sc)->length); | |
256 | ||
1b2c7af8 MC |
257 | ISCSI_DBG_SESSION(task->conn->session, |
258 | "bidi-in rlen_ahdr->read_length(%d) " | |
259 | "rlen_ahdr->ahslength(%d)\n", | |
260 | be32_to_cpu(rlen_ahdr->read_length), | |
261 | be16_to_cpu(rlen_ahdr->ahslength)); | |
c07d4444 BH |
262 | return 0; |
263 | } | |
264 | ||
7996a778 MC |
265 | /** |
266 | * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu | |
9c19a7d0 | 267 | * @task: iscsi task |
7996a778 MC |
268 | * |
269 | * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set | |
270 | * fields like dlength or final based on how much data it sends | |
271 | */ | |
9c19a7d0 | 272 | static int iscsi_prep_scsi_cmd_pdu(struct iscsi_task *task) |
7996a778 | 273 | { |
9c19a7d0 | 274 | struct iscsi_conn *conn = task->conn; |
7996a778 | 275 | struct iscsi_session *session = conn->session; |
9c19a7d0 | 276 | struct scsi_cmnd *sc = task->sc; |
577577da | 277 | struct iscsi_cmd *hdr; |
38d1c069 | 278 | unsigned hdrlength, cmd_len; |
262ef636 | 279 | itt_t itt; |
004d6530 | 280 | int rc; |
7996a778 | 281 | |
184b57c6 MC |
282 | if (conn->session->tt->alloc_pdu) { |
283 | rc = conn->session->tt->alloc_pdu(task, ISCSI_OP_SCSI_CMD); | |
284 | if (rc) | |
285 | return rc; | |
286 | } | |
577577da | 287 | hdr = (struct iscsi_cmd *) task->hdr; |
262ef636 | 288 | itt = hdr->itt; |
577577da MC |
289 | memset(hdr, 0, sizeof(*hdr)); |
290 | ||
2ff79d52 MC |
291 | if (session->tt->parse_pdu_itt) |
292 | hdr->itt = task->hdr_itt = itt; | |
293 | else | |
294 | hdr->itt = task->hdr_itt = build_itt(task->itt, | |
295 | task->conn->session->age); | |
9c19a7d0 MC |
296 | task->hdr_len = 0; |
297 | rc = iscsi_add_hdr(task, sizeof(*hdr)); | |
004d6530 BH |
298 | if (rc) |
299 | return rc; | |
a8ac6311 OK |
300 | hdr->opcode = ISCSI_OP_SCSI_CMD; |
301 | hdr->flags = ISCSI_ATTR_SIMPLE; | |
302 | int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun); | |
577577da | 303 | memcpy(task->lun, hdr->lun, sizeof(task->lun)); |
a8ac6311 | 304 | hdr->exp_statsn = cpu_to_be32(conn->exp_statsn); |
38d1c069 BH |
305 | cmd_len = sc->cmd_len; |
306 | if (cmd_len < ISCSI_CDB_SIZE) | |
307 | memset(&hdr->cdb[cmd_len], 0, ISCSI_CDB_SIZE - cmd_len); | |
308 | else if (cmd_len > ISCSI_CDB_SIZE) { | |
9c19a7d0 | 309 | rc = iscsi_prep_ecdb_ahs(task); |
38d1c069 BH |
310 | if (rc) |
311 | return rc; | |
312 | cmd_len = ISCSI_CDB_SIZE; | |
313 | } | |
314 | memcpy(hdr->cdb, sc->cmnd, cmd_len); | |
7996a778 | 315 | |
9c19a7d0 | 316 | task->imm_count = 0; |
c07d4444 BH |
317 | if (scsi_bidi_cmnd(sc)) { |
318 | hdr->flags |= ISCSI_FLAG_CMD_READ; | |
9c19a7d0 | 319 | rc = iscsi_prep_bidi_ahs(task); |
c07d4444 BH |
320 | if (rc) |
321 | return rc; | |
322 | } | |
7996a778 | 323 | if (sc->sc_data_direction == DMA_TO_DEVICE) { |
c07d4444 | 324 | unsigned out_len = scsi_out(sc)->length; |
577577da MC |
325 | struct iscsi_r2t_info *r2t = &task->unsol_r2t; |
326 | ||
c07d4444 | 327 | hdr->data_length = cpu_to_be32(out_len); |
7996a778 MC |
328 | hdr->flags |= ISCSI_FLAG_CMD_WRITE; |
329 | /* | |
330 | * Write counters: | |
331 | * | |
332 | * imm_count bytes to be sent right after | |
333 | * SCSI PDU Header | |
334 | * | |
335 | * unsol_count bytes(as Data-Out) to be sent | |
336 | * without R2T ack right after | |
337 | * immediate data | |
338 | * | |
577577da | 339 | * r2t data_length bytes to be sent via R2T ack's |
7996a778 MC |
340 | * |
341 | * pad_count bytes to be sent as zero-padding | |
342 | */ | |
577577da | 343 | memset(r2t, 0, sizeof(*r2t)); |
7996a778 MC |
344 | |
345 | if (session->imm_data_en) { | |
c07d4444 | 346 | if (out_len >= session->first_burst) |
9c19a7d0 | 347 | task->imm_count = min(session->first_burst, |
7996a778 MC |
348 | conn->max_xmit_dlength); |
349 | else | |
9c19a7d0 | 350 | task->imm_count = min(out_len, |
7996a778 | 351 | conn->max_xmit_dlength); |
9c19a7d0 | 352 | hton24(hdr->dlength, task->imm_count); |
7996a778 | 353 | } else |
a8ac6311 | 354 | zero_data(hdr->dlength); |
7996a778 | 355 | |
ffd0436e | 356 | if (!session->initial_r2t_en) { |
577577da MC |
357 | r2t->data_length = min(session->first_burst, out_len) - |
358 | task->imm_count; | |
359 | r2t->data_offset = task->imm_count; | |
360 | r2t->ttt = cpu_to_be32(ISCSI_RESERVED_TAG); | |
361 | r2t->exp_statsn = cpu_to_be32(conn->exp_statsn); | |
ffd0436e MC |
362 | } |
363 | ||
577577da | 364 | if (!task->unsol_r2t.data_length) |
7996a778 | 365 | /* No unsolicit Data-Out's */ |
a8ac6311 | 366 | hdr->flags |= ISCSI_FLAG_CMD_FINAL; |
7996a778 | 367 | } else { |
7996a778 MC |
368 | hdr->flags |= ISCSI_FLAG_CMD_FINAL; |
369 | zero_data(hdr->dlength); | |
c07d4444 | 370 | hdr->data_length = cpu_to_be32(scsi_in(sc)->length); |
7996a778 MC |
371 | |
372 | if (sc->sc_data_direction == DMA_FROM_DEVICE) | |
373 | hdr->flags |= ISCSI_FLAG_CMD_READ; | |
374 | } | |
375 | ||
004d6530 | 376 | /* calculate size of additional header segments (AHSs) */ |
9c19a7d0 | 377 | hdrlength = task->hdr_len - sizeof(*hdr); |
004d6530 BH |
378 | |
379 | WARN_ON(hdrlength & (ISCSI_PAD_LEN-1)); | |
380 | hdrlength /= ISCSI_PAD_LEN; | |
381 | ||
382 | WARN_ON(hdrlength >= 256); | |
383 | hdr->hlength = hdrlength & 0xFF; | |
384 | ||
577577da | 385 | if (session->tt->init_task && session->tt->init_task(task)) |
052d0144 MC |
386 | return -EIO; |
387 | ||
9c19a7d0 | 388 | task->state = ISCSI_TASK_RUNNING; |
d3305f34 MC |
389 | hdr->cmdsn = task->cmdsn = cpu_to_be32(session->cmdsn); |
390 | session->cmdsn++; | |
77a23c21 | 391 | |
a8ac6311 | 392 | conn->scsicmd_pdus_cnt++; |
1b2c7af8 MC |
393 | ISCSI_DBG_SESSION(session, "iscsi prep [%s cid %d sc %p cdb 0x%x " |
394 | "itt 0x%x len %d bidi_len %d cmdsn %d win %d]\n", | |
395 | scsi_bidi_cmnd(sc) ? "bidirectional" : | |
396 | sc->sc_data_direction == DMA_TO_DEVICE ? | |
397 | "write" : "read", conn->id, sc, sc->cmnd[0], | |
398 | task->itt, scsi_bufflen(sc), | |
399 | scsi_bidi_cmnd(sc) ? scsi_in(sc)->length : 0, | |
400 | session->cmdsn, | |
401 | session->max_cmdsn - session->exp_cmdsn + 1); | |
004d6530 | 402 | return 0; |
7996a778 | 403 | } |
7996a778 MC |
404 | |
405 | /** | |
3bbaaad9 | 406 | * iscsi_free_task - free a task |
9c19a7d0 | 407 | * @task: iscsi cmd task |
7996a778 MC |
408 | * |
409 | * Must be called with session lock. | |
3e5c28ad MC |
410 | * This function returns the scsi command to scsi-ml or cleans |
411 | * up mgmt tasks then returns the task to the pool. | |
7996a778 | 412 | */ |
3bbaaad9 | 413 | static void iscsi_free_task(struct iscsi_task *task) |
7996a778 | 414 | { |
9c19a7d0 | 415 | struct iscsi_conn *conn = task->conn; |
c1635cb7 | 416 | struct iscsi_session *session = conn->session; |
9c19a7d0 | 417 | struct scsi_cmnd *sc = task->sc; |
7996a778 | 418 | |
4421c9eb MC |
419 | ISCSI_DBG_SESSION(session, "freeing task itt 0x%x state %d sc %p\n", |
420 | task->itt, task->state, task->sc); | |
421 | ||
577577da | 422 | session->tt->cleanup_task(task); |
3bbaaad9 | 423 | task->state = ISCSI_TASK_FREE; |
9c19a7d0 | 424 | task->sc = NULL; |
3e5c28ad | 425 | /* |
9c19a7d0 | 426 | * login task is preallocated so do not free |
3e5c28ad | 427 | */ |
9c19a7d0 | 428 | if (conn->login_task == task) |
3e5c28ad MC |
429 | return; |
430 | ||
9c19a7d0 | 431 | __kfifo_put(session->cmdpool.queue, (void*)&task, sizeof(void*)); |
052d0144 | 432 | |
3e5c28ad | 433 | if (sc) { |
9c19a7d0 | 434 | task->sc = NULL; |
3e5c28ad MC |
435 | /* SCSI eh reuses commands to verify us */ |
436 | sc->SCp.ptr = NULL; | |
437 | /* | |
438 | * queue command may call this to free the task, but | |
439 | * not have setup the sc callback | |
440 | */ | |
441 | if (sc->scsi_done) | |
442 | sc->scsi_done(sc); | |
443 | } | |
7996a778 MC |
444 | } |
445 | ||
913e5bf4 | 446 | void __iscsi_get_task(struct iscsi_task *task) |
60ecebf5 | 447 | { |
9c19a7d0 | 448 | atomic_inc(&task->refcount); |
60ecebf5 | 449 | } |
913e5bf4 | 450 | EXPORT_SYMBOL_GPL(__iscsi_get_task); |
60ecebf5 | 451 | |
9c19a7d0 | 452 | static void __iscsi_put_task(struct iscsi_task *task) |
60ecebf5 | 453 | { |
9c19a7d0 | 454 | if (atomic_dec_and_test(&task->refcount)) |
3bbaaad9 | 455 | iscsi_free_task(task); |
60ecebf5 MC |
456 | } |
457 | ||
9c19a7d0 | 458 | void iscsi_put_task(struct iscsi_task *task) |
3e5c28ad | 459 | { |
9c19a7d0 | 460 | struct iscsi_session *session = task->conn->session; |
3e5c28ad MC |
461 | |
462 | spin_lock_bh(&session->lock); | |
9c19a7d0 | 463 | __iscsi_put_task(task); |
3e5c28ad MC |
464 | spin_unlock_bh(&session->lock); |
465 | } | |
9c19a7d0 | 466 | EXPORT_SYMBOL_GPL(iscsi_put_task); |
3e5c28ad | 467 | |
3bbaaad9 MC |
468 | /** |
469 | * iscsi_complete_task - finish a task | |
470 | * @task: iscsi cmd task | |
b3cd5050 | 471 | * @state: state to complete task with |
3bbaaad9 MC |
472 | * |
473 | * Must be called with session lock. | |
474 | */ | |
b3cd5050 | 475 | static void iscsi_complete_task(struct iscsi_task *task, int state) |
3bbaaad9 MC |
476 | { |
477 | struct iscsi_conn *conn = task->conn; | |
478 | ||
4421c9eb MC |
479 | ISCSI_DBG_SESSION(conn->session, |
480 | "complete task itt 0x%x state %d sc %p\n", | |
481 | task->itt, task->state, task->sc); | |
b3cd5050 MC |
482 | if (task->state == ISCSI_TASK_COMPLETED || |
483 | task->state == ISCSI_TASK_ABRT_TMF || | |
484 | task->state == ISCSI_TASK_ABRT_SESS_RECOV) | |
3bbaaad9 MC |
485 | return; |
486 | WARN_ON_ONCE(task->state == ISCSI_TASK_FREE); | |
b3cd5050 | 487 | task->state = state; |
3bbaaad9 MC |
488 | |
489 | if (!list_empty(&task->running)) | |
490 | list_del_init(&task->running); | |
491 | ||
492 | if (conn->task == task) | |
493 | conn->task = NULL; | |
494 | ||
495 | if (conn->ping_task == task) | |
496 | conn->ping_task = NULL; | |
497 | ||
498 | /* release get from queueing */ | |
499 | __iscsi_put_task(task); | |
500 | } | |
501 | ||
b3a7ea8d | 502 | /* |
3bbaaad9 MC |
503 | * session lock must be held and if not called for a task that is |
504 | * still pending or from the xmit thread, then xmit thread must | |
505 | * be suspended. | |
b3a7ea8d | 506 | */ |
3bbaaad9 | 507 | static void fail_scsi_task(struct iscsi_task *task, int err) |
b3a7ea8d | 508 | { |
3bbaaad9 | 509 | struct iscsi_conn *conn = task->conn; |
b3a7ea8d | 510 | struct scsi_cmnd *sc; |
b3cd5050 | 511 | int state; |
b3a7ea8d | 512 | |
3bbaaad9 MC |
513 | /* |
514 | * if a command completes and we get a successful tmf response | |
515 | * we will hit this because the scsi eh abort code does not take | |
516 | * a ref to the task. | |
517 | */ | |
9c19a7d0 | 518 | sc = task->sc; |
b3a7ea8d MC |
519 | if (!sc) |
520 | return; | |
521 | ||
b3cd5050 | 522 | if (task->state == ISCSI_TASK_PENDING) { |
b3a7ea8d MC |
523 | /* |
524 | * cmd never made it to the xmit thread, so we should not count | |
525 | * the cmd in the sequencing | |
526 | */ | |
527 | conn->session->queued_cmdsn--; | |
b3cd5050 MC |
528 | /* it was never sent so just complete like normal */ |
529 | state = ISCSI_TASK_COMPLETED; | |
530 | } else if (err == DID_TRANSPORT_DISRUPTED) | |
531 | state = ISCSI_TASK_ABRT_SESS_RECOV; | |
532 | else | |
533 | state = ISCSI_TASK_ABRT_TMF; | |
b3a7ea8d | 534 | |
b3cd5050 | 535 | sc->result = err << 16; |
c07d4444 BH |
536 | if (!scsi_bidi_cmnd(sc)) |
537 | scsi_set_resid(sc, scsi_bufflen(sc)); | |
538 | else { | |
539 | scsi_out(sc)->resid = scsi_out(sc)->length; | |
540 | scsi_in(sc)->resid = scsi_in(sc)->length; | |
541 | } | |
3e5c28ad | 542 | |
b3cd5050 | 543 | iscsi_complete_task(task, state); |
b3a7ea8d MC |
544 | } |
545 | ||
3e5c28ad | 546 | static int iscsi_prep_mgmt_task(struct iscsi_conn *conn, |
9c19a7d0 | 547 | struct iscsi_task *task) |
052d0144 MC |
548 | { |
549 | struct iscsi_session *session = conn->session; | |
577577da | 550 | struct iscsi_hdr *hdr = task->hdr; |
052d0144 MC |
551 | struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr; |
552 | ||
553 | if (conn->session->state == ISCSI_STATE_LOGGING_OUT) | |
554 | return -ENOTCONN; | |
555 | ||
556 | if (hdr->opcode != (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) && | |
557 | hdr->opcode != (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE)) | |
558 | nop->exp_statsn = cpu_to_be32(conn->exp_statsn); | |
559 | /* | |
560 | * pre-format CmdSN for outgoing PDU. | |
561 | */ | |
562 | nop->cmdsn = cpu_to_be32(session->cmdsn); | |
563 | if (hdr->itt != RESERVED_ITT) { | |
052d0144 MC |
564 | /* |
565 | * TODO: We always use immediate, so we never hit this. | |
566 | * If we start to send tmfs or nops as non-immediate then | |
567 | * we should start checking the cmdsn numbers for mgmt tasks. | |
568 | */ | |
569 | if (conn->c_stage == ISCSI_CONN_STARTED && | |
570 | !(hdr->opcode & ISCSI_OP_IMMEDIATE)) { | |
571 | session->queued_cmdsn++; | |
572 | session->cmdsn++; | |
573 | } | |
574 | } | |
575 | ||
ae15f801 MC |
576 | if (session->tt->init_task && session->tt->init_task(task)) |
577 | return -EIO; | |
052d0144 MC |
578 | |
579 | if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT) | |
580 | session->state = ISCSI_STATE_LOGGING_OUT; | |
581 | ||
577577da | 582 | task->state = ISCSI_TASK_RUNNING; |
1b2c7af8 MC |
583 | ISCSI_DBG_SESSION(session, "mgmtpdu [op 0x%x hdr->itt 0x%x " |
584 | "datalen %d]\n", hdr->opcode & ISCSI_OPCODE_MASK, | |
585 | hdr->itt, task->data_count); | |
052d0144 MC |
586 | return 0; |
587 | } | |
588 | ||
9c19a7d0 | 589 | static struct iscsi_task * |
f6d5180c MC |
590 | __iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr, |
591 | char *data, uint32_t data_size) | |
592 | { | |
593 | struct iscsi_session *session = conn->session; | |
1336aed1 | 594 | struct iscsi_host *ihost = shost_priv(session->host); |
9c19a7d0 | 595 | struct iscsi_task *task; |
262ef636 | 596 | itt_t itt; |
f6d5180c MC |
597 | |
598 | if (session->state == ISCSI_STATE_TERMINATE) | |
599 | return NULL; | |
600 | ||
601 | if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) || | |
602 | hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE)) | |
603 | /* | |
604 | * Login and Text are sent serially, in | |
605 | * request-followed-by-response sequence. | |
3e5c28ad MC |
606 | * Same task can be used. Same ITT must be used. |
607 | * Note that login_task is preallocated at conn_create(). | |
f6d5180c | 608 | */ |
9c19a7d0 | 609 | task = conn->login_task; |
f6d5180c | 610 | else { |
26013ad4 MC |
611 | if (session->state != ISCSI_STATE_LOGGED_IN) |
612 | return NULL; | |
613 | ||
f6d5180c MC |
614 | BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE); |
615 | BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED); | |
616 | ||
3e5c28ad | 617 | if (!__kfifo_get(session->cmdpool.queue, |
9c19a7d0 | 618 | (void*)&task, sizeof(void*))) |
f6d5180c MC |
619 | return NULL; |
620 | } | |
3e5c28ad MC |
621 | /* |
622 | * released in complete pdu for task we expect a response for, and | |
623 | * released by the lld when it has transmitted the task for | |
624 | * pdus we do not expect a response for. | |
625 | */ | |
9c19a7d0 MC |
626 | atomic_set(&task->refcount, 1); |
627 | task->conn = conn; | |
628 | task->sc = NULL; | |
3bbaaad9 MC |
629 | INIT_LIST_HEAD(&task->running); |
630 | task->state = ISCSI_TASK_PENDING; | |
f6d5180c MC |
631 | |
632 | if (data_size) { | |
9c19a7d0 MC |
633 | memcpy(task->data, data, data_size); |
634 | task->data_count = data_size; | |
f6d5180c | 635 | } else |
9c19a7d0 | 636 | task->data_count = 0; |
f6d5180c | 637 | |
184b57c6 MC |
638 | if (conn->session->tt->alloc_pdu) { |
639 | if (conn->session->tt->alloc_pdu(task, hdr->opcode)) { | |
640 | iscsi_conn_printk(KERN_ERR, conn, "Could not allocate " | |
641 | "pdu for mgmt task.\n"); | |
3bbaaad9 | 642 | goto free_task; |
184b57c6 | 643 | } |
577577da | 644 | } |
184b57c6 | 645 | |
262ef636 | 646 | itt = task->hdr->itt; |
577577da | 647 | task->hdr_len = sizeof(struct iscsi_hdr); |
9c19a7d0 | 648 | memcpy(task->hdr, hdr, sizeof(struct iscsi_hdr)); |
262ef636 MC |
649 | |
650 | if (hdr->itt != RESERVED_ITT) { | |
651 | if (session->tt->parse_pdu_itt) | |
652 | task->hdr->itt = itt; | |
653 | else | |
654 | task->hdr->itt = build_itt(task->itt, | |
655 | task->conn->session->age); | |
656 | } | |
657 | ||
1336aed1 | 658 | if (!ihost->workq) { |
577577da MC |
659 | if (iscsi_prep_mgmt_task(conn, task)) |
660 | goto free_task; | |
052d0144 | 661 | |
9c19a7d0 | 662 | if (session->tt->xmit_task(task)) |
577577da | 663 | goto free_task; |
3bbaaad9 MC |
664 | } else { |
665 | list_add_tail(&task->running, &conn->mgmtqueue); | |
32ae763e | 666 | iscsi_conn_queue_work(conn); |
3bbaaad9 | 667 | } |
052d0144 | 668 | |
9c19a7d0 | 669 | return task; |
577577da MC |
670 | |
671 | free_task: | |
672 | __iscsi_put_task(task); | |
673 | return NULL; | |
f6d5180c MC |
674 | } |
675 | ||
676 | int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr, | |
677 | char *data, uint32_t data_size) | |
678 | { | |
679 | struct iscsi_conn *conn = cls_conn->dd_data; | |
680 | struct iscsi_session *session = conn->session; | |
681 | int err = 0; | |
682 | ||
683 | spin_lock_bh(&session->lock); | |
684 | if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size)) | |
685 | err = -EPERM; | |
686 | spin_unlock_bh(&session->lock); | |
f6d5180c MC |
687 | return err; |
688 | } | |
689 | EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu); | |
690 | ||
7996a778 MC |
691 | /** |
692 | * iscsi_cmd_rsp - SCSI Command Response processing | |
693 | * @conn: iscsi connection | |
694 | * @hdr: iscsi header | |
9c19a7d0 | 695 | * @task: scsi command task |
7996a778 MC |
696 | * @data: cmd data buffer |
697 | * @datalen: len of buffer | |
698 | * | |
699 | * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and | |
9c19a7d0 | 700 | * then completes the command and task. |
7996a778 | 701 | **/ |
77a23c21 | 702 | static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr, |
9c19a7d0 | 703 | struct iscsi_task *task, char *data, |
77a23c21 | 704 | int datalen) |
7996a778 | 705 | { |
7996a778 MC |
706 | struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)hdr; |
707 | struct iscsi_session *session = conn->session; | |
9c19a7d0 | 708 | struct scsi_cmnd *sc = task->sc; |
7996a778 | 709 | |
77a23c21 | 710 | iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr); |
7996a778 MC |
711 | conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1; |
712 | ||
713 | sc->result = (DID_OK << 16) | rhdr->cmd_status; | |
714 | ||
715 | if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) { | |
716 | sc->result = DID_ERROR << 16; | |
717 | goto out; | |
718 | } | |
719 | ||
720 | if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) { | |
9b80cb4b | 721 | uint16_t senselen; |
7996a778 MC |
722 | |
723 | if (datalen < 2) { | |
724 | invalid_datalen: | |
322d739d MC |
725 | iscsi_conn_printk(KERN_ERR, conn, |
726 | "Got CHECK_CONDITION but invalid data " | |
727 | "buffer size of %d\n", datalen); | |
7996a778 MC |
728 | sc->result = DID_BAD_TARGET << 16; |
729 | goto out; | |
730 | } | |
731 | ||
8f333991 | 732 | senselen = get_unaligned_be16(data); |
7996a778 MC |
733 | if (datalen < senselen) |
734 | goto invalid_datalen; | |
735 | ||
736 | memcpy(sc->sense_buffer, data + 2, | |
9b80cb4b | 737 | min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE)); |
1b2c7af8 MC |
738 | ISCSI_DBG_SESSION(session, "copied %d bytes of sense\n", |
739 | min_t(uint16_t, senselen, | |
740 | SCSI_SENSE_BUFFERSIZE)); | |
7996a778 MC |
741 | } |
742 | ||
c07d4444 BH |
743 | if (rhdr->flags & (ISCSI_FLAG_CMD_BIDI_UNDERFLOW | |
744 | ISCSI_FLAG_CMD_BIDI_OVERFLOW)) { | |
745 | int res_count = be32_to_cpu(rhdr->bi_residual_count); | |
746 | ||
747 | if (scsi_bidi_cmnd(sc) && res_count > 0 && | |
748 | (rhdr->flags & ISCSI_FLAG_CMD_BIDI_OVERFLOW || | |
749 | res_count <= scsi_in(sc)->length)) | |
750 | scsi_in(sc)->resid = res_count; | |
751 | else | |
752 | sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status; | |
753 | } | |
754 | ||
7207fea4 BH |
755 | if (rhdr->flags & (ISCSI_FLAG_CMD_UNDERFLOW | |
756 | ISCSI_FLAG_CMD_OVERFLOW)) { | |
7996a778 MC |
757 | int res_count = be32_to_cpu(rhdr->residual_count); |
758 | ||
7207fea4 BH |
759 | if (res_count > 0 && |
760 | (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW || | |
761 | res_count <= scsi_bufflen(sc))) | |
c07d4444 | 762 | /* write side for bidi or uni-io set_resid */ |
1c138991 | 763 | scsi_set_resid(sc, res_count); |
7996a778 MC |
764 | else |
765 | sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status; | |
c07d4444 | 766 | } |
7996a778 | 767 | out: |
3bbaaad9 | 768 | ISCSI_DBG_SESSION(session, "cmd rsp done [sc %p res %d itt 0x%x]\n", |
1b2c7af8 | 769 | sc, sc->result, task->itt); |
7996a778 | 770 | conn->scsirsp_pdus_cnt++; |
b3cd5050 | 771 | iscsi_complete_task(task, ISCSI_TASK_COMPLETED); |
7996a778 MC |
772 | } |
773 | ||
1d9edf02 MC |
774 | /** |
775 | * iscsi_data_in_rsp - SCSI Data-In Response processing | |
776 | * @conn: iscsi connection | |
777 | * @hdr: iscsi pdu | |
778 | * @task: scsi command task | |
779 | **/ | |
780 | static void | |
781 | iscsi_data_in_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr, | |
782 | struct iscsi_task *task) | |
783 | { | |
784 | struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)hdr; | |
785 | struct scsi_cmnd *sc = task->sc; | |
786 | ||
787 | if (!(rhdr->flags & ISCSI_FLAG_DATA_STATUS)) | |
788 | return; | |
789 | ||
edbc9aa0 | 790 | iscsi_update_cmdsn(conn->session, (struct iscsi_nopin *)hdr); |
1d9edf02 MC |
791 | sc->result = (DID_OK << 16) | rhdr->cmd_status; |
792 | conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1; | |
793 | if (rhdr->flags & (ISCSI_FLAG_DATA_UNDERFLOW | | |
794 | ISCSI_FLAG_DATA_OVERFLOW)) { | |
795 | int res_count = be32_to_cpu(rhdr->residual_count); | |
796 | ||
797 | if (res_count > 0 && | |
798 | (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW || | |
799 | res_count <= scsi_in(sc)->length)) | |
800 | scsi_in(sc)->resid = res_count; | |
801 | else | |
802 | sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status; | |
803 | } | |
804 | ||
3bbaaad9 MC |
805 | ISCSI_DBG_SESSION(conn->session, "data in with status done " |
806 | "[sc %p res %d itt 0x%x]\n", | |
807 | sc, sc->result, task->itt); | |
1d9edf02 | 808 | conn->scsirsp_pdus_cnt++; |
b3cd5050 | 809 | iscsi_complete_task(task, ISCSI_TASK_COMPLETED); |
1d9edf02 MC |
810 | } |
811 | ||
7ea8b828 MC |
812 | static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr) |
813 | { | |
814 | struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr; | |
815 | ||
816 | conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1; | |
817 | conn->tmfrsp_pdus_cnt++; | |
818 | ||
843c0a8a | 819 | if (conn->tmf_state != TMF_QUEUED) |
7ea8b828 MC |
820 | return; |
821 | ||
822 | if (tmf->response == ISCSI_TMF_RSP_COMPLETE) | |
843c0a8a | 823 | conn->tmf_state = TMF_SUCCESS; |
7ea8b828 | 824 | else if (tmf->response == ISCSI_TMF_RSP_NO_TASK) |
843c0a8a | 825 | conn->tmf_state = TMF_NOT_FOUND; |
7ea8b828 | 826 | else |
843c0a8a | 827 | conn->tmf_state = TMF_FAILED; |
7ea8b828 MC |
828 | wake_up(&conn->ehwait); |
829 | } | |
830 | ||
f6d5180c MC |
831 | static void iscsi_send_nopout(struct iscsi_conn *conn, struct iscsi_nopin *rhdr) |
832 | { | |
833 | struct iscsi_nopout hdr; | |
9c19a7d0 | 834 | struct iscsi_task *task; |
f6d5180c | 835 | |
9c19a7d0 | 836 | if (!rhdr && conn->ping_task) |
f6d5180c MC |
837 | return; |
838 | ||
839 | memset(&hdr, 0, sizeof(struct iscsi_nopout)); | |
840 | hdr.opcode = ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE; | |
841 | hdr.flags = ISCSI_FLAG_CMD_FINAL; | |
842 | ||
843 | if (rhdr) { | |
844 | memcpy(hdr.lun, rhdr->lun, 8); | |
845 | hdr.ttt = rhdr->ttt; | |
846 | hdr.itt = RESERVED_ITT; | |
847 | } else | |
848 | hdr.ttt = RESERVED_ITT; | |
849 | ||
9c19a7d0 MC |
850 | task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)&hdr, NULL, 0); |
851 | if (!task) | |
322d739d | 852 | iscsi_conn_printk(KERN_ERR, conn, "Could not send nopout\n"); |
d3acf022 MC |
853 | else if (!rhdr) { |
854 | /* only track our nops */ | |
855 | conn->ping_task = task; | |
856 | conn->last_ping = jiffies; | |
857 | } | |
f6d5180c MC |
858 | } |
859 | ||
62f38300 MC |
860 | static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr, |
861 | char *data, int datalen) | |
862 | { | |
863 | struct iscsi_reject *reject = (struct iscsi_reject *)hdr; | |
864 | struct iscsi_hdr rejected_pdu; | |
62f38300 MC |
865 | |
866 | conn->exp_statsn = be32_to_cpu(reject->statsn) + 1; | |
867 | ||
868 | if (reject->reason == ISCSI_REASON_DATA_DIGEST_ERROR) { | |
869 | if (ntoh24(reject->dlength) > datalen) | |
870 | return ISCSI_ERR_PROTO; | |
871 | ||
872 | if (ntoh24(reject->dlength) >= sizeof(struct iscsi_hdr)) { | |
873 | memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr)); | |
322d739d | 874 | iscsi_conn_printk(KERN_ERR, conn, |
262ef636 MC |
875 | "pdu (op 0x%x) rejected " |
876 | "due to DataDigest error.\n", | |
322d739d | 877 | rejected_pdu.opcode); |
62f38300 MC |
878 | } |
879 | } | |
880 | return 0; | |
881 | } | |
882 | ||
913e5bf4 MC |
883 | /** |
884 | * iscsi_itt_to_task - look up task by itt | |
885 | * @conn: iscsi connection | |
886 | * @itt: itt | |
887 | * | |
888 | * This should be used for mgmt tasks like login and nops, or if | |
889 | * the LDD's itt space does not include the session age. | |
890 | * | |
891 | * The session lock must be held. | |
892 | */ | |
8f9256ce | 893 | struct iscsi_task *iscsi_itt_to_task(struct iscsi_conn *conn, itt_t itt) |
913e5bf4 MC |
894 | { |
895 | struct iscsi_session *session = conn->session; | |
262ef636 | 896 | int i; |
913e5bf4 MC |
897 | |
898 | if (itt == RESERVED_ITT) | |
899 | return NULL; | |
900 | ||
262ef636 MC |
901 | if (session->tt->parse_pdu_itt) |
902 | session->tt->parse_pdu_itt(conn, itt, &i, NULL); | |
903 | else | |
904 | i = get_itt(itt); | |
913e5bf4 MC |
905 | if (i >= session->cmds_max) |
906 | return NULL; | |
907 | ||
908 | return session->cmds[i]; | |
909 | } | |
8f9256ce | 910 | EXPORT_SYMBOL_GPL(iscsi_itt_to_task); |
913e5bf4 | 911 | |
7996a778 MC |
912 | /** |
913 | * __iscsi_complete_pdu - complete pdu | |
914 | * @conn: iscsi conn | |
915 | * @hdr: iscsi header | |
916 | * @data: data buffer | |
917 | * @datalen: len of data buffer | |
918 | * | |
919 | * Completes pdu processing by freeing any resources allocated at | |
920 | * queuecommand or send generic. session lock must be held and verify | |
921 | * itt must have been called. | |
922 | */ | |
913e5bf4 MC |
923 | int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr, |
924 | char *data, int datalen) | |
7996a778 MC |
925 | { |
926 | struct iscsi_session *session = conn->session; | |
927 | int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0; | |
9c19a7d0 | 928 | struct iscsi_task *task; |
7996a778 MC |
929 | uint32_t itt; |
930 | ||
f6d5180c | 931 | conn->last_recv = jiffies; |
0af967f5 MC |
932 | rc = iscsi_verify_itt(conn, hdr->itt); |
933 | if (rc) | |
934 | return rc; | |
935 | ||
b4377356 AV |
936 | if (hdr->itt != RESERVED_ITT) |
937 | itt = get_itt(hdr->itt); | |
7996a778 | 938 | else |
b4377356 | 939 | itt = ~0U; |
7996a778 | 940 | |
1b2c7af8 MC |
941 | ISCSI_DBG_SESSION(session, "[op 0x%x cid %d itt 0x%x len %d]\n", |
942 | opcode, conn->id, itt, datalen); | |
7996a778 | 943 | |
3e5c28ad | 944 | if (itt == ~0U) { |
77a23c21 | 945 | iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr); |
62f38300 | 946 | |
7996a778 MC |
947 | switch(opcode) { |
948 | case ISCSI_OP_NOOP_IN: | |
40527afe | 949 | if (datalen) { |
7996a778 | 950 | rc = ISCSI_ERR_PROTO; |
40527afe MC |
951 | break; |
952 | } | |
953 | ||
b4377356 | 954 | if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG)) |
40527afe MC |
955 | break; |
956 | ||
f6d5180c | 957 | iscsi_send_nopout(conn, (struct iscsi_nopin*)hdr); |
7996a778 MC |
958 | break; |
959 | case ISCSI_OP_REJECT: | |
62f38300 MC |
960 | rc = iscsi_handle_reject(conn, hdr, data, datalen); |
961 | break; | |
7996a778 | 962 | case ISCSI_OP_ASYNC_EVENT: |
8d2860b3 | 963 | conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1; |
5831c737 MC |
964 | if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen)) |
965 | rc = ISCSI_ERR_CONN_FAILED; | |
7996a778 MC |
966 | break; |
967 | default: | |
968 | rc = ISCSI_ERR_BAD_OPCODE; | |
969 | break; | |
970 | } | |
3e5c28ad MC |
971 | goto out; |
972 | } | |
973 | ||
3e5c28ad MC |
974 | switch(opcode) { |
975 | case ISCSI_OP_SCSI_CMD_RSP: | |
913e5bf4 MC |
976 | case ISCSI_OP_SCSI_DATA_IN: |
977 | task = iscsi_itt_to_ctask(conn, hdr->itt); | |
978 | if (!task) | |
979 | return ISCSI_ERR_BAD_ITT; | |
d355e57d | 980 | task->last_xfer = jiffies; |
913e5bf4 MC |
981 | break; |
982 | case ISCSI_OP_R2T: | |
983 | /* | |
984 | * LLD handles R2Ts if they need to. | |
985 | */ | |
986 | return 0; | |
987 | case ISCSI_OP_LOGOUT_RSP: | |
988 | case ISCSI_OP_LOGIN_RSP: | |
989 | case ISCSI_OP_TEXT_RSP: | |
990 | case ISCSI_OP_SCSI_TMFUNC_RSP: | |
991 | case ISCSI_OP_NOOP_IN: | |
992 | task = iscsi_itt_to_task(conn, hdr->itt); | |
993 | if (!task) | |
994 | return ISCSI_ERR_BAD_ITT; | |
995 | break; | |
996 | default: | |
997 | return ISCSI_ERR_BAD_OPCODE; | |
998 | } | |
999 | ||
1000 | switch(opcode) { | |
1001 | case ISCSI_OP_SCSI_CMD_RSP: | |
9c19a7d0 | 1002 | iscsi_scsi_cmd_rsp(conn, hdr, task, data, datalen); |
3e5c28ad MC |
1003 | break; |
1004 | case ISCSI_OP_SCSI_DATA_IN: | |
1d9edf02 | 1005 | iscsi_data_in_rsp(conn, hdr, task); |
3e5c28ad | 1006 | break; |
3e5c28ad MC |
1007 | case ISCSI_OP_LOGOUT_RSP: |
1008 | iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr); | |
1009 | if (datalen) { | |
1010 | rc = ISCSI_ERR_PROTO; | |
1011 | break; | |
1012 | } | |
1013 | conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1; | |
1014 | goto recv_pdu; | |
1015 | case ISCSI_OP_LOGIN_RSP: | |
1016 | case ISCSI_OP_TEXT_RSP: | |
1017 | iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr); | |
1018 | /* | |
1019 | * login related PDU's exp_statsn is handled in | |
1020 | * userspace | |
1021 | */ | |
1022 | goto recv_pdu; | |
1023 | case ISCSI_OP_SCSI_TMFUNC_RSP: | |
1024 | iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr); | |
1025 | if (datalen) { | |
1026 | rc = ISCSI_ERR_PROTO; | |
1027 | break; | |
1028 | } | |
1029 | ||
1030 | iscsi_tmf_rsp(conn, hdr); | |
b3cd5050 | 1031 | iscsi_complete_task(task, ISCSI_TASK_COMPLETED); |
3e5c28ad MC |
1032 | break; |
1033 | case ISCSI_OP_NOOP_IN: | |
1034 | iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr); | |
1035 | if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) || datalen) { | |
1036 | rc = ISCSI_ERR_PROTO; | |
1037 | break; | |
1038 | } | |
1039 | conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1; | |
1040 | ||
9c19a7d0 | 1041 | if (conn->ping_task != task) |
3e5c28ad MC |
1042 | /* |
1043 | * If this is not in response to one of our | |
1044 | * nops then it must be from userspace. | |
1045 | */ | |
1046 | goto recv_pdu; | |
9c19a7d0 MC |
1047 | |
1048 | mod_timer(&conn->transport_timer, jiffies + conn->recv_timeout); | |
b3cd5050 | 1049 | iscsi_complete_task(task, ISCSI_TASK_COMPLETED); |
3e5c28ad MC |
1050 | break; |
1051 | default: | |
1052 | rc = ISCSI_ERR_BAD_OPCODE; | |
1053 | break; | |
1054 | } | |
7996a778 | 1055 | |
3e5c28ad MC |
1056 | out: |
1057 | return rc; | |
1058 | recv_pdu: | |
1059 | if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen)) | |
1060 | rc = ISCSI_ERR_CONN_FAILED; | |
b3cd5050 | 1061 | iscsi_complete_task(task, ISCSI_TASK_COMPLETED); |
7996a778 MC |
1062 | return rc; |
1063 | } | |
913e5bf4 | 1064 | EXPORT_SYMBOL_GPL(__iscsi_complete_pdu); |
7996a778 MC |
1065 | |
1066 | int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr, | |
1067 | char *data, int datalen) | |
1068 | { | |
1069 | int rc; | |
1070 | ||
1071 | spin_lock(&conn->session->lock); | |
1072 | rc = __iscsi_complete_pdu(conn, hdr, data, datalen); | |
1073 | spin_unlock(&conn->session->lock); | |
1074 | return rc; | |
1075 | } | |
1076 | EXPORT_SYMBOL_GPL(iscsi_complete_pdu); | |
1077 | ||
0af967f5 | 1078 | int iscsi_verify_itt(struct iscsi_conn *conn, itt_t itt) |
7996a778 MC |
1079 | { |
1080 | struct iscsi_session *session = conn->session; | |
262ef636 | 1081 | int age = 0, i = 0; |
7996a778 | 1082 | |
0af967f5 MC |
1083 | if (itt == RESERVED_ITT) |
1084 | return 0; | |
7996a778 | 1085 | |
262ef636 MC |
1086 | if (session->tt->parse_pdu_itt) |
1087 | session->tt->parse_pdu_itt(conn, itt, &i, &age); | |
1088 | else { | |
1089 | i = get_itt(itt); | |
1090 | age = ((__force u32)itt >> ISCSI_AGE_SHIFT) & ISCSI_AGE_MASK; | |
1091 | } | |
1092 | ||
1093 | if (age != session->age) { | |
0af967f5 MC |
1094 | iscsi_conn_printk(KERN_ERR, conn, |
1095 | "received itt %x expected session age (%x)\n", | |
913e5bf4 | 1096 | (__force u32)itt, session->age); |
0af967f5 MC |
1097 | return ISCSI_ERR_BAD_ITT; |
1098 | } | |
7996a778 | 1099 | |
3e5c28ad MC |
1100 | if (i >= session->cmds_max) { |
1101 | iscsi_conn_printk(KERN_ERR, conn, | |
1102 | "received invalid itt index %u (max cmds " | |
1103 | "%u.\n", i, session->cmds_max); | |
1104 | return ISCSI_ERR_BAD_ITT; | |
7996a778 | 1105 | } |
7996a778 MC |
1106 | return 0; |
1107 | } | |
1108 | EXPORT_SYMBOL_GPL(iscsi_verify_itt); | |
1109 | ||
913e5bf4 MC |
1110 | /** |
1111 | * iscsi_itt_to_ctask - look up ctask by itt | |
1112 | * @conn: iscsi connection | |
1113 | * @itt: itt | |
1114 | * | |
1115 | * This should be used for cmd tasks. | |
1116 | * | |
1117 | * The session lock must be held. | |
1118 | */ | |
1119 | struct iscsi_task *iscsi_itt_to_ctask(struct iscsi_conn *conn, itt_t itt) | |
0af967f5 | 1120 | { |
9c19a7d0 | 1121 | struct iscsi_task *task; |
0af967f5 MC |
1122 | |
1123 | if (iscsi_verify_itt(conn, itt)) | |
1124 | return NULL; | |
1125 | ||
913e5bf4 MC |
1126 | task = iscsi_itt_to_task(conn, itt); |
1127 | if (!task || !task->sc) | |
0af967f5 MC |
1128 | return NULL; |
1129 | ||
913e5bf4 MC |
1130 | if (task->sc->SCp.phase != conn->session->age) { |
1131 | iscsi_session_printk(KERN_ERR, conn->session, | |
1132 | "task's session age %d, expected %d\n", | |
1133 | task->sc->SCp.phase, conn->session->age); | |
0af967f5 | 1134 | return NULL; |
913e5bf4 | 1135 | } |
0af967f5 | 1136 | |
9c19a7d0 | 1137 | return task; |
0af967f5 MC |
1138 | } |
1139 | EXPORT_SYMBOL_GPL(iscsi_itt_to_ctask); | |
1140 | ||
40a06e75 | 1141 | void iscsi_session_failure(struct iscsi_session *session, |
e5bd7b54 MC |
1142 | enum iscsi_err err) |
1143 | { | |
e5bd7b54 MC |
1144 | struct iscsi_conn *conn; |
1145 | struct device *dev; | |
1146 | unsigned long flags; | |
1147 | ||
1148 | spin_lock_irqsave(&session->lock, flags); | |
1149 | conn = session->leadconn; | |
1150 | if (session->state == ISCSI_STATE_TERMINATE || !conn) { | |
1151 | spin_unlock_irqrestore(&session->lock, flags); | |
1152 | return; | |
1153 | } | |
1154 | ||
1155 | dev = get_device(&conn->cls_conn->dev); | |
1156 | spin_unlock_irqrestore(&session->lock, flags); | |
1157 | if (!dev) | |
1158 | return; | |
1159 | /* | |
1160 | * if the host is being removed bypass the connection | |
1161 | * recovery initialization because we are going to kill | |
1162 | * the session. | |
1163 | */ | |
1164 | if (err == ISCSI_ERR_INVALID_HOST) | |
1165 | iscsi_conn_error_event(conn->cls_conn, err); | |
1166 | else | |
1167 | iscsi_conn_failure(conn, err); | |
1168 | put_device(dev); | |
1169 | } | |
1170 | EXPORT_SYMBOL_GPL(iscsi_session_failure); | |
1171 | ||
7996a778 MC |
1172 | void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err) |
1173 | { | |
1174 | struct iscsi_session *session = conn->session; | |
1175 | unsigned long flags; | |
1176 | ||
1177 | spin_lock_irqsave(&session->lock, flags); | |
656cffc9 MC |
1178 | if (session->state == ISCSI_STATE_FAILED) { |
1179 | spin_unlock_irqrestore(&session->lock, flags); | |
1180 | return; | |
1181 | } | |
1182 | ||
67a61114 | 1183 | if (conn->stop_stage == 0) |
7996a778 MC |
1184 | session->state = ISCSI_STATE_FAILED; |
1185 | spin_unlock_irqrestore(&session->lock, flags); | |
e5bd7b54 | 1186 | |
7996a778 MC |
1187 | set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx); |
1188 | set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx); | |
e5bd7b54 | 1189 | iscsi_conn_error_event(conn->cls_conn, err); |
7996a778 MC |
1190 | } |
1191 | EXPORT_SYMBOL_GPL(iscsi_conn_failure); | |
1192 | ||
77a23c21 MC |
1193 | static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn) |
1194 | { | |
1195 | struct iscsi_session *session = conn->session; | |
1196 | ||
1197 | /* | |
1198 | * Check for iSCSI window and take care of CmdSN wrap-around | |
1199 | */ | |
e0726407 | 1200 | if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) { |
1b2c7af8 MC |
1201 | ISCSI_DBG_SESSION(session, "iSCSI CmdSN closed. ExpCmdSn " |
1202 | "%u MaxCmdSN %u CmdSN %u/%u\n", | |
1203 | session->exp_cmdsn, session->max_cmdsn, | |
1204 | session->cmdsn, session->queued_cmdsn); | |
77a23c21 MC |
1205 | return -ENOSPC; |
1206 | } | |
1207 | return 0; | |
1208 | } | |
1209 | ||
9c19a7d0 | 1210 | static int iscsi_xmit_task(struct iscsi_conn *conn) |
77a23c21 | 1211 | { |
9c19a7d0 | 1212 | struct iscsi_task *task = conn->task; |
843c0a8a | 1213 | int rc; |
77a23c21 | 1214 | |
9c19a7d0 | 1215 | __iscsi_get_task(task); |
77a23c21 | 1216 | spin_unlock_bh(&conn->session->lock); |
9c19a7d0 | 1217 | rc = conn->session->tt->xmit_task(task); |
77a23c21 | 1218 | spin_lock_bh(&conn->session->lock); |
d355e57d | 1219 | if (!rc) { |
9c19a7d0 | 1220 | /* done with this task */ |
d355e57d | 1221 | task->last_xfer = jiffies; |
9c19a7d0 | 1222 | conn->task = NULL; |
d355e57d MC |
1223 | } |
1224 | __iscsi_put_task(task); | |
77a23c21 MC |
1225 | return rc; |
1226 | } | |
1227 | ||
843c0a8a | 1228 | /** |
9c19a7d0 MC |
1229 | * iscsi_requeue_task - requeue task to run from session workqueue |
1230 | * @task: task to requeue | |
843c0a8a | 1231 | * |
9c19a7d0 | 1232 | * LLDs that need to run a task from the session workqueue should call |
052d0144 MC |
1233 | * this. The session lock must be held. This should only be called |
1234 | * by software drivers. | |
843c0a8a | 1235 | */ |
9c19a7d0 | 1236 | void iscsi_requeue_task(struct iscsi_task *task) |
843c0a8a | 1237 | { |
9c19a7d0 | 1238 | struct iscsi_conn *conn = task->conn; |
843c0a8a | 1239 | |
3bbaaad9 MC |
1240 | /* |
1241 | * this may be on the requeue list already if the xmit_task callout | |
1242 | * is handling the r2ts while we are adding new ones | |
1243 | */ | |
1244 | if (list_empty(&task->running)) | |
1245 | list_add_tail(&task->running, &conn->requeue); | |
32ae763e | 1246 | iscsi_conn_queue_work(conn); |
843c0a8a | 1247 | } |
9c19a7d0 | 1248 | EXPORT_SYMBOL_GPL(iscsi_requeue_task); |
843c0a8a | 1249 | |
7996a778 MC |
1250 | /** |
1251 | * iscsi_data_xmit - xmit any command into the scheduled connection | |
1252 | * @conn: iscsi connection | |
1253 | * | |
1254 | * Notes: | |
1255 | * The function can return -EAGAIN in which case the caller must | |
1256 | * re-schedule it again later or recover. '0' return code means | |
1257 | * successful xmit. | |
1258 | **/ | |
1259 | static int iscsi_data_xmit(struct iscsi_conn *conn) | |
1260 | { | |
3219e529 | 1261 | int rc = 0; |
7996a778 | 1262 | |
77a23c21 | 1263 | spin_lock_bh(&conn->session->lock); |
7996a778 | 1264 | if (unlikely(conn->suspend_tx)) { |
1b2c7af8 | 1265 | ISCSI_DBG_SESSION(conn->session, "Tx suspended!\n"); |
77a23c21 | 1266 | spin_unlock_bh(&conn->session->lock); |
3219e529 | 1267 | return -ENODATA; |
7996a778 | 1268 | } |
7996a778 | 1269 | |
9c19a7d0 MC |
1270 | if (conn->task) { |
1271 | rc = iscsi_xmit_task(conn); | |
3219e529 | 1272 | if (rc) |
7996a778 | 1273 | goto again; |
7996a778 MC |
1274 | } |
1275 | ||
77a23c21 MC |
1276 | /* |
1277 | * process mgmt pdus like nops before commands since we should | |
1278 | * only have one nop-out as a ping from us and targets should not | |
1279 | * overflow us with nop-ins | |
1280 | */ | |
1281 | check_mgmt: | |
843c0a8a | 1282 | while (!list_empty(&conn->mgmtqueue)) { |
9c19a7d0 MC |
1283 | conn->task = list_entry(conn->mgmtqueue.next, |
1284 | struct iscsi_task, running); | |
3bbaaad9 | 1285 | list_del_init(&conn->task->running); |
9c19a7d0 MC |
1286 | if (iscsi_prep_mgmt_task(conn, conn->task)) { |
1287 | __iscsi_put_task(conn->task); | |
1288 | conn->task = NULL; | |
b3a7ea8d MC |
1289 | continue; |
1290 | } | |
9c19a7d0 | 1291 | rc = iscsi_xmit_task(conn); |
77a23c21 MC |
1292 | if (rc) |
1293 | goto again; | |
7996a778 MC |
1294 | } |
1295 | ||
843c0a8a | 1296 | /* process pending command queue */ |
3bbaaad9 | 1297 | while (!list_empty(&conn->cmdqueue)) { |
843c0a8a MC |
1298 | if (conn->tmf_state == TMF_QUEUED) |
1299 | break; | |
1300 | ||
3bbaaad9 | 1301 | conn->task = list_entry(conn->cmdqueue.next, |
9c19a7d0 | 1302 | struct iscsi_task, running); |
3bbaaad9 | 1303 | list_del_init(&conn->task->running); |
b3a7ea8d | 1304 | if (conn->session->state == ISCSI_STATE_LOGGING_OUT) { |
b3cd5050 | 1305 | fail_scsi_task(conn->task, DID_IMM_RETRY); |
b3a7ea8d MC |
1306 | continue; |
1307 | } | |
577577da MC |
1308 | rc = iscsi_prep_scsi_cmd_pdu(conn->task); |
1309 | if (rc) { | |
1310 | if (rc == -ENOMEM) { | |
3bbaaad9 MC |
1311 | list_add_tail(&conn->task->running, |
1312 | &conn->cmdqueue); | |
577577da MC |
1313 | conn->task = NULL; |
1314 | goto again; | |
1315 | } else | |
b3cd5050 | 1316 | fail_scsi_task(conn->task, DID_ABORT); |
004d6530 BH |
1317 | continue; |
1318 | } | |
9c19a7d0 | 1319 | rc = iscsi_xmit_task(conn); |
77a23c21 | 1320 | if (rc) |
60ecebf5 | 1321 | goto again; |
77a23c21 | 1322 | /* |
9c19a7d0 | 1323 | * we could continuously get new task requests so |
77a23c21 MC |
1324 | * we need to check the mgmt queue for nops that need to |
1325 | * be sent to aviod starvation | |
1326 | */ | |
843c0a8a MC |
1327 | if (!list_empty(&conn->mgmtqueue)) |
1328 | goto check_mgmt; | |
1329 | } | |
1330 | ||
1331 | while (!list_empty(&conn->requeue)) { | |
1332 | if (conn->session->fast_abort && conn->tmf_state != TMF_INITIAL) | |
1333 | break; | |
1334 | ||
b3a7ea8d MC |
1335 | /* |
1336 | * we always do fastlogout - conn stop code will clean up. | |
1337 | */ | |
1338 | if (conn->session->state == ISCSI_STATE_LOGGING_OUT) | |
1339 | break; | |
1340 | ||
9c19a7d0 MC |
1341 | conn->task = list_entry(conn->requeue.next, |
1342 | struct iscsi_task, running); | |
3bbaaad9 | 1343 | list_del_init(&conn->task->running); |
9c19a7d0 | 1344 | conn->task->state = ISCSI_TASK_RUNNING; |
9c19a7d0 | 1345 | rc = iscsi_xmit_task(conn); |
843c0a8a MC |
1346 | if (rc) |
1347 | goto again; | |
1348 | if (!list_empty(&conn->mgmtqueue)) | |
77a23c21 | 1349 | goto check_mgmt; |
7996a778 | 1350 | } |
b6c395ed | 1351 | spin_unlock_bh(&conn->session->lock); |
3219e529 | 1352 | return -ENODATA; |
7996a778 MC |
1353 | |
1354 | again: | |
1355 | if (unlikely(conn->suspend_tx)) | |
77a23c21 MC |
1356 | rc = -ENODATA; |
1357 | spin_unlock_bh(&conn->session->lock); | |
3219e529 | 1358 | return rc; |
7996a778 MC |
1359 | } |
1360 | ||
c4028958 | 1361 | static void iscsi_xmitworker(struct work_struct *work) |
7996a778 | 1362 | { |
c4028958 DH |
1363 | struct iscsi_conn *conn = |
1364 | container_of(work, struct iscsi_conn, xmitwork); | |
3219e529 | 1365 | int rc; |
7996a778 MC |
1366 | /* |
1367 | * serialize Xmit worker on a per-connection basis. | |
1368 | */ | |
3219e529 MC |
1369 | do { |
1370 | rc = iscsi_data_xmit(conn); | |
1371 | } while (rc >= 0 || rc == -EAGAIN); | |
7996a778 MC |
1372 | } |
1373 | ||
577577da MC |
1374 | static inline struct iscsi_task *iscsi_alloc_task(struct iscsi_conn *conn, |
1375 | struct scsi_cmnd *sc) | |
1376 | { | |
1377 | struct iscsi_task *task; | |
1378 | ||
1379 | if (!__kfifo_get(conn->session->cmdpool.queue, | |
1380 | (void *) &task, sizeof(void *))) | |
1381 | return NULL; | |
1382 | ||
1383 | sc->SCp.phase = conn->session->age; | |
1384 | sc->SCp.ptr = (char *) task; | |
1385 | ||
1386 | atomic_set(&task->refcount, 1); | |
1387 | task->state = ISCSI_TASK_PENDING; | |
1388 | task->conn = conn; | |
1389 | task->sc = sc; | |
d355e57d MC |
1390 | task->have_checked_conn = false; |
1391 | task->last_timeout = jiffies; | |
1392 | task->last_xfer = jiffies; | |
577577da MC |
1393 | INIT_LIST_HEAD(&task->running); |
1394 | return task; | |
1395 | } | |
1396 | ||
7996a778 MC |
1397 | enum { |
1398 | FAILURE_BAD_HOST = 1, | |
1399 | FAILURE_SESSION_FAILED, | |
1400 | FAILURE_SESSION_FREED, | |
1401 | FAILURE_WINDOW_CLOSED, | |
60ecebf5 | 1402 | FAILURE_OOM, |
7996a778 | 1403 | FAILURE_SESSION_TERMINATE, |
656cffc9 | 1404 | FAILURE_SESSION_IN_RECOVERY, |
7996a778 | 1405 | FAILURE_SESSION_RECOVERY_TIMEOUT, |
b3a7ea8d | 1406 | FAILURE_SESSION_LOGGING_OUT, |
6eabafbe | 1407 | FAILURE_SESSION_NOT_READY, |
7996a778 MC |
1408 | }; |
1409 | ||
1410 | int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *)) | |
1411 | { | |
75613521 | 1412 | struct iscsi_cls_session *cls_session; |
7996a778 | 1413 | struct Scsi_Host *host; |
1336aed1 | 1414 | struct iscsi_host *ihost; |
7996a778 MC |
1415 | int reason = 0; |
1416 | struct iscsi_session *session; | |
1417 | struct iscsi_conn *conn; | |
9c19a7d0 | 1418 | struct iscsi_task *task = NULL; |
7996a778 MC |
1419 | |
1420 | sc->scsi_done = done; | |
1421 | sc->result = 0; | |
f47f2cf5 | 1422 | sc->SCp.ptr = NULL; |
7996a778 MC |
1423 | |
1424 | host = sc->device->host; | |
1336aed1 | 1425 | ihost = shost_priv(host); |
1040c99d | 1426 | spin_unlock(host->host_lock); |
7996a778 | 1427 | |
75613521 MC |
1428 | cls_session = starget_to_session(scsi_target(sc->device)); |
1429 | session = cls_session->dd_data; | |
7996a778 MC |
1430 | spin_lock(&session->lock); |
1431 | ||
75613521 | 1432 | reason = iscsi_session_chkready(cls_session); |
6eabafbe MC |
1433 | if (reason) { |
1434 | sc->result = reason; | |
1435 | goto fault; | |
1436 | } | |
1437 | ||
301e0f7e | 1438 | if (session->state != ISCSI_STATE_LOGGED_IN) { |
656cffc9 MC |
1439 | /* |
1440 | * to handle the race between when we set the recovery state | |
1441 | * and block the session we requeue here (commands could | |
1442 | * be entering our queuecommand while a block is starting | |
1443 | * up because the block code is not locked) | |
1444 | */ | |
9000bcd6 | 1445 | switch (session->state) { |
301e0f7e | 1446 | case ISCSI_STATE_FAILED: |
9000bcd6 | 1447 | case ISCSI_STATE_IN_RECOVERY: |
656cffc9 | 1448 | reason = FAILURE_SESSION_IN_RECOVERY; |
301e0f7e MC |
1449 | sc->result = DID_IMM_RETRY << 16; |
1450 | break; | |
9000bcd6 MC |
1451 | case ISCSI_STATE_LOGGING_OUT: |
1452 | reason = FAILURE_SESSION_LOGGING_OUT; | |
301e0f7e MC |
1453 | sc->result = DID_IMM_RETRY << 16; |
1454 | break; | |
b3a7ea8d | 1455 | case ISCSI_STATE_RECOVERY_FAILED: |
656cffc9 | 1456 | reason = FAILURE_SESSION_RECOVERY_TIMEOUT; |
56d7fcfa | 1457 | sc->result = DID_TRANSPORT_FAILFAST << 16; |
b3a7ea8d MC |
1458 | break; |
1459 | case ISCSI_STATE_TERMINATE: | |
656cffc9 | 1460 | reason = FAILURE_SESSION_TERMINATE; |
6eabafbe | 1461 | sc->result = DID_NO_CONNECT << 16; |
b3a7ea8d | 1462 | break; |
b3a7ea8d | 1463 | default: |
656cffc9 | 1464 | reason = FAILURE_SESSION_FREED; |
6eabafbe | 1465 | sc->result = DID_NO_CONNECT << 16; |
b3a7ea8d | 1466 | } |
7996a778 MC |
1467 | goto fault; |
1468 | } | |
1469 | ||
7996a778 | 1470 | conn = session->leadconn; |
98644047 MC |
1471 | if (!conn) { |
1472 | reason = FAILURE_SESSION_FREED; | |
6eabafbe | 1473 | sc->result = DID_NO_CONNECT << 16; |
98644047 MC |
1474 | goto fault; |
1475 | } | |
7996a778 | 1476 | |
77a23c21 MC |
1477 | if (iscsi_check_cmdsn_window_closed(conn)) { |
1478 | reason = FAILURE_WINDOW_CLOSED; | |
1479 | goto reject; | |
1480 | } | |
1481 | ||
577577da MC |
1482 | task = iscsi_alloc_task(conn, sc); |
1483 | if (!task) { | |
60ecebf5 MC |
1484 | reason = FAILURE_OOM; |
1485 | goto reject; | |
1486 | } | |
7996a778 | 1487 | |
1336aed1 | 1488 | if (!ihost->workq) { |
577577da MC |
1489 | reason = iscsi_prep_scsi_cmd_pdu(task); |
1490 | if (reason) { | |
1491 | if (reason == -ENOMEM) { | |
1492 | reason = FAILURE_OOM; | |
1493 | goto prepd_reject; | |
1494 | } else { | |
1495 | sc->result = DID_ABORT << 16; | |
1496 | goto prepd_fault; | |
1497 | } | |
052d0144 | 1498 | } |
9c19a7d0 | 1499 | if (session->tt->xmit_task(task)) { |
d3305f34 | 1500 | session->cmdsn--; |
052d0144 | 1501 | reason = FAILURE_SESSION_NOT_READY; |
577577da | 1502 | goto prepd_reject; |
052d0144 | 1503 | } |
3bbaaad9 MC |
1504 | } else { |
1505 | list_add_tail(&task->running, &conn->cmdqueue); | |
32ae763e | 1506 | iscsi_conn_queue_work(conn); |
3bbaaad9 | 1507 | } |
052d0144 MC |
1508 | |
1509 | session->queued_cmdsn++; | |
1510 | spin_unlock(&session->lock); | |
1040c99d | 1511 | spin_lock(host->host_lock); |
7996a778 MC |
1512 | return 0; |
1513 | ||
577577da MC |
1514 | prepd_reject: |
1515 | sc->scsi_done = NULL; | |
b3cd5050 | 1516 | iscsi_complete_task(task, ISCSI_TASK_COMPLETED); |
7996a778 MC |
1517 | reject: |
1518 | spin_unlock(&session->lock); | |
1b2c7af8 MC |
1519 | ISCSI_DBG_SESSION(session, "cmd 0x%x rejected (%d)\n", |
1520 | sc->cmnd[0], reason); | |
1040c99d | 1521 | spin_lock(host->host_lock); |
d6d13ee1 | 1522 | return SCSI_MLQUEUE_TARGET_BUSY; |
7996a778 | 1523 | |
577577da MC |
1524 | prepd_fault: |
1525 | sc->scsi_done = NULL; | |
b3cd5050 | 1526 | iscsi_complete_task(task, ISCSI_TASK_COMPLETED); |
7996a778 MC |
1527 | fault: |
1528 | spin_unlock(&session->lock); | |
1b2c7af8 MC |
1529 | ISCSI_DBG_SESSION(session, "iscsi: cmd 0x%x is not queued (%d)\n", |
1530 | sc->cmnd[0], reason); | |
c07d4444 BH |
1531 | if (!scsi_bidi_cmnd(sc)) |
1532 | scsi_set_resid(sc, scsi_bufflen(sc)); | |
1533 | else { | |
1534 | scsi_out(sc)->resid = scsi_out(sc)->length; | |
1535 | scsi_in(sc)->resid = scsi_in(sc)->length; | |
1536 | } | |
052d0144 | 1537 | done(sc); |
1040c99d | 1538 | spin_lock(host->host_lock); |
7996a778 MC |
1539 | return 0; |
1540 | } | |
1541 | EXPORT_SYMBOL_GPL(iscsi_queuecommand); | |
1542 | ||
1543 | int iscsi_change_queue_depth(struct scsi_device *sdev, int depth) | |
1544 | { | |
7996a778 MC |
1545 | scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth); |
1546 | return sdev->queue_depth; | |
1547 | } | |
1548 | EXPORT_SYMBOL_GPL(iscsi_change_queue_depth); | |
1549 | ||
6b5d6c44 MC |
1550 | int iscsi_target_alloc(struct scsi_target *starget) |
1551 | { | |
1552 | struct iscsi_cls_session *cls_session = starget_to_session(starget); | |
1553 | struct iscsi_session *session = cls_session->dd_data; | |
1554 | ||
1555 | starget->can_queue = session->scsi_cmds_max; | |
1556 | return 0; | |
1557 | } | |
1558 | EXPORT_SYMBOL_GPL(iscsi_target_alloc); | |
1559 | ||
7996a778 MC |
1560 | void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session) |
1561 | { | |
75613521 | 1562 | struct iscsi_session *session = cls_session->dd_data; |
7996a778 MC |
1563 | |
1564 | spin_lock_bh(&session->lock); | |
1565 | if (session->state != ISCSI_STATE_LOGGED_IN) { | |
656cffc9 | 1566 | session->state = ISCSI_STATE_RECOVERY_FAILED; |
843c0a8a MC |
1567 | if (session->leadconn) |
1568 | wake_up(&session->leadconn->ehwait); | |
7996a778 MC |
1569 | } |
1570 | spin_unlock_bh(&session->lock); | |
1571 | } | |
1572 | EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout); | |
1573 | ||
8e124525 | 1574 | int iscsi_eh_target_reset(struct scsi_cmnd *sc) |
7996a778 | 1575 | { |
75613521 MC |
1576 | struct iscsi_cls_session *cls_session; |
1577 | struct iscsi_session *session; | |
1578 | struct iscsi_conn *conn; | |
1579 | ||
1580 | cls_session = starget_to_session(scsi_target(sc->device)); | |
1581 | session = cls_session->dd_data; | |
1582 | conn = session->leadconn; | |
7996a778 | 1583 | |
bc436b27 | 1584 | mutex_lock(&session->eh_mutex); |
7996a778 MC |
1585 | spin_lock_bh(&session->lock); |
1586 | if (session->state == ISCSI_STATE_TERMINATE) { | |
1587 | failed: | |
bd2199d4 EZ |
1588 | ISCSI_DBG_EH(session, |
1589 | "failing target reset: Could not log back into " | |
1590 | "target [age %d]\n", | |
1591 | session->age); | |
7996a778 | 1592 | spin_unlock_bh(&session->lock); |
bc436b27 | 1593 | mutex_unlock(&session->eh_mutex); |
7996a778 MC |
1594 | return FAILED; |
1595 | } | |
1596 | ||
7996a778 | 1597 | spin_unlock_bh(&session->lock); |
bc436b27 | 1598 | mutex_unlock(&session->eh_mutex); |
7996a778 MC |
1599 | /* |
1600 | * we drop the lock here but the leadconn cannot be destoyed while | |
1601 | * we are in the scsi eh | |
1602 | */ | |
843c0a8a | 1603 | iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED); |
7996a778 | 1604 | |
bd2199d4 | 1605 | ISCSI_DBG_EH(session, "wait for relogin\n"); |
7996a778 MC |
1606 | wait_event_interruptible(conn->ehwait, |
1607 | session->state == ISCSI_STATE_TERMINATE || | |
1608 | session->state == ISCSI_STATE_LOGGED_IN || | |
656cffc9 | 1609 | session->state == ISCSI_STATE_RECOVERY_FAILED); |
7996a778 MC |
1610 | if (signal_pending(current)) |
1611 | flush_signals(current); | |
1612 | ||
bc436b27 | 1613 | mutex_lock(&session->eh_mutex); |
7996a778 | 1614 | spin_lock_bh(&session->lock); |
bd2199d4 EZ |
1615 | if (session->state == ISCSI_STATE_LOGGED_IN) { |
1616 | ISCSI_DBG_EH(session, | |
1617 | "target reset succeeded\n"); | |
1618 | } else | |
7996a778 MC |
1619 | goto failed; |
1620 | spin_unlock_bh(&session->lock); | |
bc436b27 | 1621 | mutex_unlock(&session->eh_mutex); |
7996a778 MC |
1622 | return SUCCESS; |
1623 | } | |
8e124525 | 1624 | EXPORT_SYMBOL_GPL(iscsi_eh_target_reset); |
7996a778 | 1625 | |
843c0a8a | 1626 | static void iscsi_tmf_timedout(unsigned long data) |
7996a778 | 1627 | { |
843c0a8a | 1628 | struct iscsi_conn *conn = (struct iscsi_conn *)data; |
7996a778 MC |
1629 | struct iscsi_session *session = conn->session; |
1630 | ||
1631 | spin_lock(&session->lock); | |
843c0a8a MC |
1632 | if (conn->tmf_state == TMF_QUEUED) { |
1633 | conn->tmf_state = TMF_TIMEDOUT; | |
bd2199d4 | 1634 | ISCSI_DBG_EH(session, "tmf timedout\n"); |
7996a778 MC |
1635 | /* unblock eh_abort() */ |
1636 | wake_up(&conn->ehwait); | |
1637 | } | |
1638 | spin_unlock(&session->lock); | |
1639 | } | |
1640 | ||
9c19a7d0 | 1641 | static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn, |
f6d5180c MC |
1642 | struct iscsi_tm *hdr, int age, |
1643 | int timeout) | |
7996a778 | 1644 | { |
7996a778 | 1645 | struct iscsi_session *session = conn->session; |
9c19a7d0 | 1646 | struct iscsi_task *task; |
7996a778 | 1647 | |
9c19a7d0 | 1648 | task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr, |
843c0a8a | 1649 | NULL, 0); |
9c19a7d0 | 1650 | if (!task) { |
6724add1 | 1651 | spin_unlock_bh(&session->lock); |
7996a778 | 1652 | iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED); |
843c0a8a | 1653 | spin_lock_bh(&session->lock); |
bd2199d4 | 1654 | ISCSI_DBG_EH(session, "tmf exec failure\n"); |
77a23c21 | 1655 | return -EPERM; |
7996a778 | 1656 | } |
843c0a8a | 1657 | conn->tmfcmd_pdus_cnt++; |
f6d5180c | 1658 | conn->tmf_timer.expires = timeout * HZ + jiffies; |
843c0a8a MC |
1659 | conn->tmf_timer.function = iscsi_tmf_timedout; |
1660 | conn->tmf_timer.data = (unsigned long)conn; | |
1661 | add_timer(&conn->tmf_timer); | |
bd2199d4 | 1662 | ISCSI_DBG_EH(session, "tmf set timeout\n"); |
7996a778 | 1663 | |
7996a778 | 1664 | spin_unlock_bh(&session->lock); |
6724add1 | 1665 | mutex_unlock(&session->eh_mutex); |
7996a778 MC |
1666 | |
1667 | /* | |
1668 | * block eh thread until: | |
1669 | * | |
843c0a8a MC |
1670 | * 1) tmf response |
1671 | * 2) tmf timeout | |
7996a778 MC |
1672 | * 3) session is terminated or restarted or userspace has |
1673 | * given up on recovery | |
1674 | */ | |
843c0a8a | 1675 | wait_event_interruptible(conn->ehwait, age != session->age || |
7996a778 | 1676 | session->state != ISCSI_STATE_LOGGED_IN || |
843c0a8a | 1677 | conn->tmf_state != TMF_QUEUED); |
7996a778 MC |
1678 | if (signal_pending(current)) |
1679 | flush_signals(current); | |
843c0a8a MC |
1680 | del_timer_sync(&conn->tmf_timer); |
1681 | ||
6724add1 | 1682 | mutex_lock(&session->eh_mutex); |
77a23c21 | 1683 | spin_lock_bh(&session->lock); |
9c19a7d0 | 1684 | /* if the session drops it will clean up the task */ |
843c0a8a MC |
1685 | if (age != session->age || |
1686 | session->state != ISCSI_STATE_LOGGED_IN) | |
1687 | return -ENOTCONN; | |
7996a778 MC |
1688 | return 0; |
1689 | } | |
1690 | ||
843c0a8a MC |
1691 | /* |
1692 | * Fail commands. session lock held and recv side suspended and xmit | |
1693 | * thread flushed | |
1694 | */ | |
3bbaaad9 MC |
1695 | static void fail_scsi_tasks(struct iscsi_conn *conn, unsigned lun, |
1696 | int error) | |
843c0a8a | 1697 | { |
3bbaaad9 MC |
1698 | struct iscsi_task *task; |
1699 | int i; | |
843c0a8a | 1700 | |
3bbaaad9 MC |
1701 | for (i = 0; i < conn->session->cmds_max; i++) { |
1702 | task = conn->session->cmds[i]; | |
1703 | if (!task->sc || task->state == ISCSI_TASK_FREE) | |
1704 | continue; | |
843c0a8a | 1705 | |
3bbaaad9 MC |
1706 | if (lun != -1 && lun != task->sc->device->lun) |
1707 | continue; | |
843c0a8a | 1708 | |
3bbaaad9 MC |
1709 | ISCSI_DBG_SESSION(conn->session, |
1710 | "failing sc %p itt 0x%x state %d\n", | |
1711 | task->sc, task->itt, task->state); | |
b3cd5050 | 1712 | fail_scsi_task(task, error); |
843c0a8a MC |
1713 | } |
1714 | } | |
1715 | ||
b40977d9 | 1716 | void iscsi_suspend_tx(struct iscsi_conn *conn) |
6724add1 | 1717 | { |
32ae763e MC |
1718 | struct Scsi_Host *shost = conn->session->host; |
1719 | struct iscsi_host *ihost = shost_priv(shost); | |
1720 | ||
6724add1 | 1721 | set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx); |
1336aed1 | 1722 | if (ihost->workq) |
32ae763e | 1723 | flush_workqueue(ihost->workq); |
6724add1 | 1724 | } |
b40977d9 | 1725 | EXPORT_SYMBOL_GPL(iscsi_suspend_tx); |
6724add1 MC |
1726 | |
1727 | static void iscsi_start_tx(struct iscsi_conn *conn) | |
1728 | { | |
1729 | clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx); | |
1336aed1 | 1730 | iscsi_conn_queue_work(conn); |
6724add1 MC |
1731 | } |
1732 | ||
4c48a829 MC |
1733 | /* |
1734 | * We want to make sure a ping is in flight. It has timed out. | |
1735 | * And we are not busy processing a pdu that is making | |
1736 | * progress but got started before the ping and is taking a while | |
1737 | * to complete so the ping is just stuck behind it in a queue. | |
1738 | */ | |
1739 | static int iscsi_has_ping_timed_out(struct iscsi_conn *conn) | |
1740 | { | |
1741 | if (conn->ping_task && | |
1742 | time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) + | |
1743 | (conn->ping_timeout * HZ), jiffies)) | |
1744 | return 1; | |
1745 | else | |
1746 | return 0; | |
1747 | } | |
1748 | ||
d355e57d | 1749 | static enum blk_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *sc) |
f6d5180c | 1750 | { |
d355e57d MC |
1751 | enum blk_eh_timer_return rc = BLK_EH_NOT_HANDLED; |
1752 | struct iscsi_task *task = NULL; | |
f6d5180c MC |
1753 | struct iscsi_cls_session *cls_session; |
1754 | struct iscsi_session *session; | |
1755 | struct iscsi_conn *conn; | |
f6d5180c | 1756 | |
d355e57d | 1757 | cls_session = starget_to_session(scsi_target(sc->device)); |
75613521 | 1758 | session = cls_session->dd_data; |
f6d5180c | 1759 | |
bd2199d4 | 1760 | ISCSI_DBG_EH(session, "scsi cmd %p timedout\n", sc); |
f6d5180c MC |
1761 | |
1762 | spin_lock(&session->lock); | |
1763 | if (session->state != ISCSI_STATE_LOGGED_IN) { | |
1764 | /* | |
1765 | * We are probably in the middle of iscsi recovery so let | |
1766 | * that complete and handle the error. | |
1767 | */ | |
242f9dcb | 1768 | rc = BLK_EH_RESET_TIMER; |
f6d5180c MC |
1769 | goto done; |
1770 | } | |
1771 | ||
1772 | conn = session->leadconn; | |
1773 | if (!conn) { | |
1774 | /* In the middle of shuting down */ | |
242f9dcb | 1775 | rc = BLK_EH_RESET_TIMER; |
f6d5180c MC |
1776 | goto done; |
1777 | } | |
1778 | ||
d355e57d MC |
1779 | task = (struct iscsi_task *)sc->SCp.ptr; |
1780 | if (!task) | |
1781 | goto done; | |
1782 | /* | |
1783 | * If we have sent (at least queued to the network layer) a pdu or | |
1784 | * recvd one for the task since the last timeout ask for | |
1785 | * more time. If on the next timeout we have not made progress | |
1786 | * we can check if it is the task or connection when we send the | |
1787 | * nop as a ping. | |
1788 | */ | |
1789 | if (time_after_eq(task->last_xfer, task->last_timeout)) { | |
bd2199d4 EZ |
1790 | ISCSI_DBG_EH(session, "Command making progress. Asking " |
1791 | "scsi-ml for more time to complete. " | |
1792 | "Last data recv at %lu. Last timeout was at " | |
1793 | "%lu\n.", task->last_xfer, task->last_timeout); | |
d355e57d MC |
1794 | task->have_checked_conn = false; |
1795 | rc = BLK_EH_RESET_TIMER; | |
1796 | goto done; | |
1797 | } | |
1798 | ||
f6d5180c MC |
1799 | if (!conn->recv_timeout && !conn->ping_timeout) |
1800 | goto done; | |
1801 | /* | |
1802 | * if the ping timedout then we are in the middle of cleaning up | |
1803 | * and can let the iscsi eh handle it | |
1804 | */ | |
4c48a829 | 1805 | if (iscsi_has_ping_timed_out(conn)) { |
242f9dcb | 1806 | rc = BLK_EH_RESET_TIMER; |
4c48a829 MC |
1807 | goto done; |
1808 | } | |
d355e57d MC |
1809 | |
1810 | /* Assumes nop timeout is shorter than scsi cmd timeout */ | |
1811 | if (task->have_checked_conn) | |
1812 | goto done; | |
1813 | ||
f6d5180c | 1814 | /* |
d355e57d MC |
1815 | * Checking the transport already or nop from a cmd timeout still |
1816 | * running | |
f6d5180c | 1817 | */ |
d355e57d MC |
1818 | if (conn->ping_task) { |
1819 | task->have_checked_conn = true; | |
242f9dcb | 1820 | rc = BLK_EH_RESET_TIMER; |
4c48a829 MC |
1821 | goto done; |
1822 | } | |
1823 | ||
d355e57d MC |
1824 | /* Make sure there is a transport check done */ |
1825 | iscsi_send_nopout(conn, NULL); | |
1826 | task->have_checked_conn = true; | |
1827 | rc = BLK_EH_RESET_TIMER; | |
1828 | ||
f6d5180c | 1829 | done: |
d355e57d MC |
1830 | if (task) |
1831 | task->last_timeout = jiffies; | |
f6d5180c | 1832 | spin_unlock(&session->lock); |
bd2199d4 EZ |
1833 | ISCSI_DBG_EH(session, "return %s\n", rc == BLK_EH_RESET_TIMER ? |
1834 | "timer reset" : "nh"); | |
f6d5180c MC |
1835 | return rc; |
1836 | } | |
1837 | ||
1838 | static void iscsi_check_transport_timeouts(unsigned long data) | |
1839 | { | |
1840 | struct iscsi_conn *conn = (struct iscsi_conn *)data; | |
1841 | struct iscsi_session *session = conn->session; | |
4cf10435 | 1842 | unsigned long recv_timeout, next_timeout = 0, last_recv; |
f6d5180c MC |
1843 | |
1844 | spin_lock(&session->lock); | |
1845 | if (session->state != ISCSI_STATE_LOGGED_IN) | |
1846 | goto done; | |
1847 | ||
4cf10435 MC |
1848 | recv_timeout = conn->recv_timeout; |
1849 | if (!recv_timeout) | |
f6d5180c MC |
1850 | goto done; |
1851 | ||
4cf10435 | 1852 | recv_timeout *= HZ; |
f6d5180c | 1853 | last_recv = conn->last_recv; |
4c48a829 MC |
1854 | |
1855 | if (iscsi_has_ping_timed_out(conn)) { | |
322d739d | 1856 | iscsi_conn_printk(KERN_ERR, conn, "ping timeout of %d secs " |
4c48a829 MC |
1857 | "expired, recv timeout %d, last rx %lu, " |
1858 | "last ping %lu, now %lu\n", | |
1859 | conn->ping_timeout, conn->recv_timeout, | |
1860 | last_recv, conn->last_ping, jiffies); | |
f6d5180c MC |
1861 | spin_unlock(&session->lock); |
1862 | iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED); | |
1863 | return; | |
1864 | } | |
1865 | ||
4cf10435 | 1866 | if (time_before_eq(last_recv + recv_timeout, jiffies)) { |
c8611f97 | 1867 | /* send a ping to try to provoke some traffic */ |
1b2c7af8 | 1868 | ISCSI_DBG_CONN(conn, "Sending nopout as ping\n"); |
c8611f97 | 1869 | iscsi_send_nopout(conn, NULL); |
4cf10435 | 1870 | next_timeout = conn->last_ping + (conn->ping_timeout * HZ); |
ad294e9c | 1871 | } else |
4cf10435 | 1872 | next_timeout = last_recv + recv_timeout; |
f6d5180c | 1873 | |
1b2c7af8 | 1874 | ISCSI_DBG_CONN(conn, "Setting next tmo %lu\n", next_timeout); |
ad294e9c | 1875 | mod_timer(&conn->transport_timer, next_timeout); |
f6d5180c MC |
1876 | done: |
1877 | spin_unlock(&session->lock); | |
1878 | } | |
1879 | ||
9c19a7d0 | 1880 | static void iscsi_prep_abort_task_pdu(struct iscsi_task *task, |
843c0a8a MC |
1881 | struct iscsi_tm *hdr) |
1882 | { | |
1883 | memset(hdr, 0, sizeof(*hdr)); | |
1884 | hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE; | |
1885 | hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK; | |
1886 | hdr->flags |= ISCSI_FLAG_CMD_FINAL; | |
577577da MC |
1887 | memcpy(hdr->lun, task->lun, sizeof(hdr->lun)); |
1888 | hdr->rtt = task->hdr_itt; | |
1889 | hdr->refcmdsn = task->cmdsn; | |
843c0a8a MC |
1890 | } |
1891 | ||
7996a778 MC |
1892 | int iscsi_eh_abort(struct scsi_cmnd *sc) |
1893 | { | |
75613521 MC |
1894 | struct iscsi_cls_session *cls_session; |
1895 | struct iscsi_session *session; | |
f47f2cf5 | 1896 | struct iscsi_conn *conn; |
9c19a7d0 | 1897 | struct iscsi_task *task; |
843c0a8a MC |
1898 | struct iscsi_tm *hdr; |
1899 | int rc, age; | |
7996a778 | 1900 | |
75613521 MC |
1901 | cls_session = starget_to_session(scsi_target(sc->device)); |
1902 | session = cls_session->dd_data; | |
1903 | ||
bd2199d4 | 1904 | ISCSI_DBG_EH(session, "aborting sc %p\n", sc); |
4421c9eb | 1905 | |
6724add1 MC |
1906 | mutex_lock(&session->eh_mutex); |
1907 | spin_lock_bh(&session->lock); | |
f47f2cf5 MC |
1908 | /* |
1909 | * if session was ISCSI_STATE_IN_RECOVERY then we may not have | |
1910 | * got the command. | |
1911 | */ | |
1912 | if (!sc->SCp.ptr) { | |
bd2199d4 EZ |
1913 | ISCSI_DBG_EH(session, "sc never reached iscsi layer or " |
1914 | "it completed.\n"); | |
6724add1 MC |
1915 | spin_unlock_bh(&session->lock); |
1916 | mutex_unlock(&session->eh_mutex); | |
f47f2cf5 MC |
1917 | return SUCCESS; |
1918 | } | |
1919 | ||
7996a778 MC |
1920 | /* |
1921 | * If we are not logged in or we have started a new session | |
1922 | * then let the host reset code handle this | |
1923 | */ | |
843c0a8a MC |
1924 | if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN || |
1925 | sc->SCp.phase != session->age) { | |
1926 | spin_unlock_bh(&session->lock); | |
1927 | mutex_unlock(&session->eh_mutex); | |
bd2199d4 | 1928 | ISCSI_DBG_EH(session, "failing abort due to dropped " |
4421c9eb | 1929 | "session.\n"); |
843c0a8a MC |
1930 | return FAILED; |
1931 | } | |
1932 | ||
1933 | conn = session->leadconn; | |
1934 | conn->eh_abort_cnt++; | |
1935 | age = session->age; | |
1936 | ||
9c19a7d0 | 1937 | task = (struct iscsi_task *)sc->SCp.ptr; |
bd2199d4 EZ |
1938 | ISCSI_DBG_EH(session, "aborting [sc %p itt 0x%x]\n", |
1939 | sc, task->itt); | |
7996a778 | 1940 | |
9c19a7d0 MC |
1941 | /* task completed before time out */ |
1942 | if (!task->sc) { | |
bd2199d4 | 1943 | ISCSI_DBG_EH(session, "sc completed while abort in progress\n"); |
77a23c21 | 1944 | goto success; |
7ea8b828 | 1945 | } |
7996a778 | 1946 | |
9c19a7d0 | 1947 | if (task->state == ISCSI_TASK_PENDING) { |
b3cd5050 | 1948 | fail_scsi_task(task, DID_ABORT); |
77a23c21 MC |
1949 | goto success; |
1950 | } | |
7996a778 | 1951 | |
843c0a8a MC |
1952 | /* only have one tmf outstanding at a time */ |
1953 | if (conn->tmf_state != TMF_INITIAL) | |
7996a778 | 1954 | goto failed; |
843c0a8a | 1955 | conn->tmf_state = TMF_QUEUED; |
7996a778 | 1956 | |
843c0a8a | 1957 | hdr = &conn->tmhdr; |
9c19a7d0 | 1958 | iscsi_prep_abort_task_pdu(task, hdr); |
843c0a8a | 1959 | |
9c19a7d0 | 1960 | if (iscsi_exec_task_mgmt_fn(conn, hdr, age, session->abort_timeout)) { |
843c0a8a MC |
1961 | rc = FAILED; |
1962 | goto failed; | |
1963 | } | |
1964 | ||
1965 | switch (conn->tmf_state) { | |
1966 | case TMF_SUCCESS: | |
77a23c21 | 1967 | spin_unlock_bh(&session->lock); |
913e5bf4 MC |
1968 | /* |
1969 | * stop tx side incase the target had sent a abort rsp but | |
1970 | * the initiator was still writing out data. | |
1971 | */ | |
6724add1 | 1972 | iscsi_suspend_tx(conn); |
77a23c21 | 1973 | /* |
913e5bf4 MC |
1974 | * we do not stop the recv side because targets have been |
1975 | * good and have never sent us a successful tmf response | |
1976 | * then sent more data for the cmd. | |
77a23c21 | 1977 | */ |
6187c242 | 1978 | spin_lock_bh(&session->lock); |
b3cd5050 | 1979 | fail_scsi_task(task, DID_ABORT); |
843c0a8a | 1980 | conn->tmf_state = TMF_INITIAL; |
6187c242 | 1981 | spin_unlock_bh(&session->lock); |
6724add1 | 1982 | iscsi_start_tx(conn); |
77a23c21 | 1983 | goto success_unlocked; |
843c0a8a MC |
1984 | case TMF_TIMEDOUT: |
1985 | spin_unlock_bh(&session->lock); | |
1986 | iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED); | |
1987 | goto failed_unlocked; | |
1988 | case TMF_NOT_FOUND: | |
1989 | if (!sc->SCp.ptr) { | |
1990 | conn->tmf_state = TMF_INITIAL; | |
9c19a7d0 | 1991 | /* task completed before tmf abort response */ |
bd2199d4 EZ |
1992 | ISCSI_DBG_EH(session, "sc completed while abort in " |
1993 | "progress\n"); | |
77a23c21 | 1994 | goto success; |
7ea8b828 MC |
1995 | } |
1996 | /* fall through */ | |
1997 | default: | |
843c0a8a MC |
1998 | conn->tmf_state = TMF_INITIAL; |
1999 | goto failed; | |
7996a778 MC |
2000 | } |
2001 | ||
77a23c21 | 2002 | success: |
7996a778 | 2003 | spin_unlock_bh(&session->lock); |
77a23c21 | 2004 | success_unlocked: |
bd2199d4 EZ |
2005 | ISCSI_DBG_EH(session, "abort success [sc %p itt 0x%x]\n", |
2006 | sc, task->itt); | |
6724add1 | 2007 | mutex_unlock(&session->eh_mutex); |
7996a778 MC |
2008 | return SUCCESS; |
2009 | ||
2010 | failed: | |
2011 | spin_unlock_bh(&session->lock); | |
77a23c21 | 2012 | failed_unlocked: |
bd2199d4 EZ |
2013 | ISCSI_DBG_EH(session, "abort failed [sc %p itt 0x%x]\n", sc, |
2014 | task ? task->itt : 0); | |
6724add1 | 2015 | mutex_unlock(&session->eh_mutex); |
7996a778 MC |
2016 | return FAILED; |
2017 | } | |
2018 | EXPORT_SYMBOL_GPL(iscsi_eh_abort); | |
2019 | ||
843c0a8a MC |
2020 | static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr) |
2021 | { | |
2022 | memset(hdr, 0, sizeof(*hdr)); | |
2023 | hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE; | |
2024 | hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK; | |
2025 | hdr->flags |= ISCSI_FLAG_CMD_FINAL; | |
2026 | int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun); | |
f6d5180c | 2027 | hdr->rtt = RESERVED_ITT; |
843c0a8a MC |
2028 | } |
2029 | ||
2030 | int iscsi_eh_device_reset(struct scsi_cmnd *sc) | |
2031 | { | |
75613521 MC |
2032 | struct iscsi_cls_session *cls_session; |
2033 | struct iscsi_session *session; | |
843c0a8a MC |
2034 | struct iscsi_conn *conn; |
2035 | struct iscsi_tm *hdr; | |
2036 | int rc = FAILED; | |
2037 | ||
75613521 MC |
2038 | cls_session = starget_to_session(scsi_target(sc->device)); |
2039 | session = cls_session->dd_data; | |
2040 | ||
bd2199d4 | 2041 | ISCSI_DBG_EH(session, "LU Reset [sc %p lun %u]\n", sc, sc->device->lun); |
843c0a8a MC |
2042 | |
2043 | mutex_lock(&session->eh_mutex); | |
2044 | spin_lock_bh(&session->lock); | |
2045 | /* | |
2046 | * Just check if we are not logged in. We cannot check for | |
2047 | * the phase because the reset could come from a ioctl. | |
2048 | */ | |
2049 | if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN) | |
2050 | goto unlock; | |
2051 | conn = session->leadconn; | |
2052 | ||
2053 | /* only have one tmf outstanding at a time */ | |
2054 | if (conn->tmf_state != TMF_INITIAL) | |
2055 | goto unlock; | |
2056 | conn->tmf_state = TMF_QUEUED; | |
2057 | ||
2058 | hdr = &conn->tmhdr; | |
2059 | iscsi_prep_lun_reset_pdu(sc, hdr); | |
2060 | ||
9c19a7d0 | 2061 | if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age, |
f6d5180c | 2062 | session->lu_reset_timeout)) { |
843c0a8a MC |
2063 | rc = FAILED; |
2064 | goto unlock; | |
2065 | } | |
2066 | ||
2067 | switch (conn->tmf_state) { | |
2068 | case TMF_SUCCESS: | |
2069 | break; | |
2070 | case TMF_TIMEDOUT: | |
2071 | spin_unlock_bh(&session->lock); | |
2072 | iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED); | |
2073 | goto done; | |
2074 | default: | |
2075 | conn->tmf_state = TMF_INITIAL; | |
2076 | goto unlock; | |
2077 | } | |
2078 | ||
2079 | rc = SUCCESS; | |
2080 | spin_unlock_bh(&session->lock); | |
2081 | ||
2082 | iscsi_suspend_tx(conn); | |
913e5bf4 | 2083 | |
a3439148 | 2084 | spin_lock_bh(&session->lock); |
3bbaaad9 | 2085 | fail_scsi_tasks(conn, sc->device->lun, DID_ERROR); |
843c0a8a | 2086 | conn->tmf_state = TMF_INITIAL; |
a3439148 | 2087 | spin_unlock_bh(&session->lock); |
843c0a8a MC |
2088 | |
2089 | iscsi_start_tx(conn); | |
2090 | goto done; | |
2091 | ||
2092 | unlock: | |
2093 | spin_unlock_bh(&session->lock); | |
2094 | done: | |
bd2199d4 EZ |
2095 | ISCSI_DBG_EH(session, "dev reset result = %s\n", |
2096 | rc == SUCCESS ? "SUCCESS" : "FAILED"); | |
843c0a8a MC |
2097 | mutex_unlock(&session->eh_mutex); |
2098 | return rc; | |
2099 | } | |
2100 | EXPORT_SYMBOL_GPL(iscsi_eh_device_reset); | |
2101 | ||
6320377f OK |
2102 | /* |
2103 | * Pre-allocate a pool of @max items of @item_size. By default, the pool | |
2104 | * should be accessed via kfifo_{get,put} on q->queue. | |
2105 | * Optionally, the caller can obtain the array of object pointers | |
2106 | * by passing in a non-NULL @items pointer | |
2107 | */ | |
7996a778 | 2108 | int |
6320377f | 2109 | iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size) |
7996a778 | 2110 | { |
6320377f | 2111 | int i, num_arrays = 1; |
7996a778 | 2112 | |
6320377f | 2113 | memset(q, 0, sizeof(*q)); |
7996a778 MC |
2114 | |
2115 | q->max = max; | |
6320377f OK |
2116 | |
2117 | /* If the user passed an items pointer, he wants a copy of | |
2118 | * the array. */ | |
2119 | if (items) | |
2120 | num_arrays++; | |
2121 | q->pool = kzalloc(num_arrays * max * sizeof(void*), GFP_KERNEL); | |
2122 | if (q->pool == NULL) | |
f474a37b | 2123 | return -ENOMEM; |
7996a778 MC |
2124 | |
2125 | q->queue = kfifo_init((void*)q->pool, max * sizeof(void*), | |
2126 | GFP_KERNEL, NULL); | |
fd6e1c14 JD |
2127 | if (IS_ERR(q->queue)) { |
2128 | q->queue = NULL; | |
6320377f | 2129 | goto enomem; |
fd6e1c14 | 2130 | } |
7996a778 MC |
2131 | |
2132 | for (i = 0; i < max; i++) { | |
6320377f | 2133 | q->pool[i] = kzalloc(item_size, GFP_KERNEL); |
7996a778 | 2134 | if (q->pool[i] == NULL) { |
6320377f OK |
2135 | q->max = i; |
2136 | goto enomem; | |
7996a778 | 2137 | } |
7996a778 MC |
2138 | __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*)); |
2139 | } | |
6320377f OK |
2140 | |
2141 | if (items) { | |
2142 | *items = q->pool + max; | |
2143 | memcpy(*items, q->pool, max * sizeof(void *)); | |
2144 | } | |
2145 | ||
7996a778 | 2146 | return 0; |
6320377f OK |
2147 | |
2148 | enomem: | |
2149 | iscsi_pool_free(q); | |
2150 | return -ENOMEM; | |
7996a778 MC |
2151 | } |
2152 | EXPORT_SYMBOL_GPL(iscsi_pool_init); | |
2153 | ||
6320377f | 2154 | void iscsi_pool_free(struct iscsi_pool *q) |
7996a778 MC |
2155 | { |
2156 | int i; | |
2157 | ||
2158 | for (i = 0; i < q->max; i++) | |
6320377f | 2159 | kfree(q->pool[i]); |
f474a37b | 2160 | kfree(q->pool); |
2f5899a3 | 2161 | kfree(q->queue); |
7996a778 MC |
2162 | } |
2163 | EXPORT_SYMBOL_GPL(iscsi_pool_free); | |
2164 | ||
a4804cd6 MC |
2165 | /** |
2166 | * iscsi_host_add - add host to system | |
2167 | * @shost: scsi host | |
2168 | * @pdev: parent device | |
2169 | * | |
2170 | * This should be called by partial offload and software iscsi drivers | |
2171 | * to add a host to the system. | |
2172 | */ | |
2173 | int iscsi_host_add(struct Scsi_Host *shost, struct device *pdev) | |
2174 | { | |
8e9a20ce MC |
2175 | if (!shost->can_queue) |
2176 | shost->can_queue = ISCSI_DEF_XMIT_CMDS_MAX; | |
2177 | ||
4d108350 MC |
2178 | if (!shost->cmd_per_lun) |
2179 | shost->cmd_per_lun = ISCSI_DEF_CMD_PER_LUN; | |
2180 | ||
308cec14 MC |
2181 | if (!shost->transportt->eh_timed_out) |
2182 | shost->transportt->eh_timed_out = iscsi_eh_cmd_timed_out; | |
a4804cd6 MC |
2183 | return scsi_add_host(shost, pdev); |
2184 | } | |
2185 | EXPORT_SYMBOL_GPL(iscsi_host_add); | |
2186 | ||
2187 | /** | |
2188 | * iscsi_host_alloc - allocate a host and driver data | |
2189 | * @sht: scsi host template | |
2190 | * @dd_data_size: driver host data size | |
32ae763e | 2191 | * @xmit_can_sleep: bool indicating if LLD will queue IO from a work queue |
a4804cd6 MC |
2192 | * |
2193 | * This should be called by partial offload and software iscsi drivers. | |
2194 | * To access the driver specific memory use the iscsi_host_priv() macro. | |
2195 | */ | |
2196 | struct Scsi_Host *iscsi_host_alloc(struct scsi_host_template *sht, | |
4d108350 | 2197 | int dd_data_size, bool xmit_can_sleep) |
75613521 | 2198 | { |
a4804cd6 | 2199 | struct Scsi_Host *shost; |
e5bd7b54 | 2200 | struct iscsi_host *ihost; |
a4804cd6 MC |
2201 | |
2202 | shost = scsi_host_alloc(sht, sizeof(struct iscsi_host) + dd_data_size); | |
2203 | if (!shost) | |
2204 | return NULL; | |
e5bd7b54 | 2205 | ihost = shost_priv(shost); |
32ae763e MC |
2206 | |
2207 | if (xmit_can_sleep) { | |
2208 | snprintf(ihost->workq_name, sizeof(ihost->workq_name), | |
2209 | "iscsi_q_%d", shost->host_no); | |
2210 | ihost->workq = create_singlethread_workqueue(ihost->workq_name); | |
2211 | if (!ihost->workq) | |
2212 | goto free_host; | |
2213 | } | |
2214 | ||
e5bd7b54 MC |
2215 | spin_lock_init(&ihost->lock); |
2216 | ihost->state = ISCSI_HOST_SETUP; | |
2217 | ihost->num_sessions = 0; | |
2218 | init_waitqueue_head(&ihost->session_removal_wq); | |
a4804cd6 | 2219 | return shost; |
32ae763e MC |
2220 | |
2221 | free_host: | |
2222 | scsi_host_put(shost); | |
2223 | return NULL; | |
a4804cd6 MC |
2224 | } |
2225 | EXPORT_SYMBOL_GPL(iscsi_host_alloc); | |
2226 | ||
e5bd7b54 MC |
2227 | static void iscsi_notify_host_removed(struct iscsi_cls_session *cls_session) |
2228 | { | |
40a06e75 | 2229 | iscsi_session_failure(cls_session->dd_data, ISCSI_ERR_INVALID_HOST); |
e5bd7b54 MC |
2230 | } |
2231 | ||
a4804cd6 MC |
2232 | /** |
2233 | * iscsi_host_remove - remove host and sessions | |
2234 | * @shost: scsi host | |
2235 | * | |
e5bd7b54 MC |
2236 | * If there are any sessions left, this will initiate the removal and wait |
2237 | * for the completion. | |
a4804cd6 MC |
2238 | */ |
2239 | void iscsi_host_remove(struct Scsi_Host *shost) | |
2240 | { | |
e5bd7b54 MC |
2241 | struct iscsi_host *ihost = shost_priv(shost); |
2242 | unsigned long flags; | |
2243 | ||
2244 | spin_lock_irqsave(&ihost->lock, flags); | |
2245 | ihost->state = ISCSI_HOST_REMOVED; | |
2246 | spin_unlock_irqrestore(&ihost->lock, flags); | |
2247 | ||
2248 | iscsi_host_for_each_session(shost, iscsi_notify_host_removed); | |
2249 | wait_event_interruptible(ihost->session_removal_wq, | |
2250 | ihost->num_sessions == 0); | |
2251 | if (signal_pending(current)) | |
2252 | flush_signals(current); | |
2253 | ||
a4804cd6 | 2254 | scsi_remove_host(shost); |
32ae763e MC |
2255 | if (ihost->workq) |
2256 | destroy_workqueue(ihost->workq); | |
75613521 | 2257 | } |
a4804cd6 | 2258 | EXPORT_SYMBOL_GPL(iscsi_host_remove); |
7996a778 | 2259 | |
a4804cd6 | 2260 | void iscsi_host_free(struct Scsi_Host *shost) |
75613521 MC |
2261 | { |
2262 | struct iscsi_host *ihost = shost_priv(shost); | |
7996a778 | 2263 | |
75613521 MC |
2264 | kfree(ihost->netdev); |
2265 | kfree(ihost->hwaddress); | |
2266 | kfree(ihost->initiatorname); | |
a4804cd6 | 2267 | scsi_host_put(shost); |
75613521 | 2268 | } |
a4804cd6 | 2269 | EXPORT_SYMBOL_GPL(iscsi_host_free); |
7996a778 | 2270 | |
e5bd7b54 MC |
2271 | static void iscsi_host_dec_session_cnt(struct Scsi_Host *shost) |
2272 | { | |
2273 | struct iscsi_host *ihost = shost_priv(shost); | |
2274 | unsigned long flags; | |
2275 | ||
2276 | shost = scsi_host_get(shost); | |
2277 | if (!shost) { | |
2278 | printk(KERN_ERR "Invalid state. Cannot notify host removal " | |
2279 | "of session teardown event because host already " | |
2280 | "removed.\n"); | |
2281 | return; | |
2282 | } | |
2283 | ||
2284 | spin_lock_irqsave(&ihost->lock, flags); | |
2285 | ihost->num_sessions--; | |
2286 | if (ihost->num_sessions == 0) | |
2287 | wake_up(&ihost->session_removal_wq); | |
2288 | spin_unlock_irqrestore(&ihost->lock, flags); | |
2289 | scsi_host_put(shost); | |
2290 | } | |
2291 | ||
7996a778 MC |
2292 | /** |
2293 | * iscsi_session_setup - create iscsi cls session and host and session | |
7996a778 | 2294 | * @iscsit: iscsi transport template |
75613521 MC |
2295 | * @shost: scsi host |
2296 | * @cmds_max: session can queue | |
9c19a7d0 | 2297 | * @cmd_task_size: LLD task private data size |
7996a778 | 2298 | * @initial_cmdsn: initial CmdSN |
7996a778 MC |
2299 | * |
2300 | * This can be used by software iscsi_transports that allocate | |
2301 | * a session per scsi host. | |
3cf7b233 MC |
2302 | * |
2303 | * Callers should set cmds_max to the largest total numer (mgmt + scsi) of | |
2304 | * tasks they support. The iscsi layer reserves ISCSI_MGMT_CMDS_MAX tasks | |
2305 | * for nop handling and login/logout requests. | |
75613521 | 2306 | */ |
7996a778 | 2307 | struct iscsi_cls_session * |
75613521 | 2308 | iscsi_session_setup(struct iscsi_transport *iscsit, struct Scsi_Host *shost, |
3cf7b233 | 2309 | uint16_t cmds_max, int cmd_task_size, |
7970634b | 2310 | uint32_t initial_cmdsn, unsigned int id) |
7996a778 | 2311 | { |
e5bd7b54 | 2312 | struct iscsi_host *ihost = shost_priv(shost); |
7996a778 MC |
2313 | struct iscsi_session *session; |
2314 | struct iscsi_cls_session *cls_session; | |
3cf7b233 | 2315 | int cmd_i, scsi_cmds, total_cmds = cmds_max; |
e5bd7b54 MC |
2316 | unsigned long flags; |
2317 | ||
2318 | spin_lock_irqsave(&ihost->lock, flags); | |
2319 | if (ihost->state == ISCSI_HOST_REMOVED) { | |
2320 | spin_unlock_irqrestore(&ihost->lock, flags); | |
2321 | return NULL; | |
2322 | } | |
2323 | ihost->num_sessions++; | |
2324 | spin_unlock_irqrestore(&ihost->lock, flags); | |
8e9a20ce MC |
2325 | |
2326 | if (!total_cmds) | |
2327 | total_cmds = ISCSI_DEF_XMIT_CMDS_MAX; | |
3e5c28ad | 2328 | /* |
3cf7b233 MC |
2329 | * The iscsi layer needs some tasks for nop handling and tmfs, |
2330 | * so the cmds_max must at least be greater than ISCSI_MGMT_CMDS_MAX | |
2331 | * + 1 command for scsi IO. | |
3e5c28ad | 2332 | */ |
3cf7b233 MC |
2333 | if (total_cmds < ISCSI_TOTAL_CMDS_MIN) { |
2334 | printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue " | |
2335 | "must be a power of two that is at least %d.\n", | |
2336 | total_cmds, ISCSI_TOTAL_CMDS_MIN); | |
e5bd7b54 | 2337 | goto dec_session_count; |
3cf7b233 MC |
2338 | } |
2339 | ||
2340 | if (total_cmds > ISCSI_TOTAL_CMDS_MAX) { | |
2341 | printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue " | |
2342 | "must be a power of 2 less than or equal to %d.\n", | |
2343 | cmds_max, ISCSI_TOTAL_CMDS_MAX); | |
2344 | total_cmds = ISCSI_TOTAL_CMDS_MAX; | |
2345 | } | |
2346 | ||
2347 | if (!is_power_of_2(total_cmds)) { | |
2348 | printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue " | |
2349 | "must be a power of 2.\n", total_cmds); | |
2350 | total_cmds = rounddown_pow_of_two(total_cmds); | |
2351 | if (total_cmds < ISCSI_TOTAL_CMDS_MIN) | |
2352 | return NULL; | |
2353 | printk(KERN_INFO "iscsi: Rounding can_queue to %d.\n", | |
2354 | total_cmds); | |
1548271e | 2355 | } |
3cf7b233 | 2356 | scsi_cmds = total_cmds - ISCSI_MGMT_CMDS_MAX; |
1548271e | 2357 | |
5d91e209 MC |
2358 | cls_session = iscsi_alloc_session(shost, iscsit, |
2359 | sizeof(struct iscsi_session)); | |
75613521 | 2360 | if (!cls_session) |
e5bd7b54 | 2361 | goto dec_session_count; |
75613521 MC |
2362 | session = cls_session->dd_data; |
2363 | session->cls_session = cls_session; | |
7996a778 MC |
2364 | session->host = shost; |
2365 | session->state = ISCSI_STATE_FREE; | |
f6d5180c | 2366 | session->fast_abort = 1; |
4cd49ea1 MC |
2367 | session->lu_reset_timeout = 15; |
2368 | session->abort_timeout = 10; | |
3cf7b233 MC |
2369 | session->scsi_cmds_max = scsi_cmds; |
2370 | session->cmds_max = total_cmds; | |
e0726407 | 2371 | session->queued_cmdsn = session->cmdsn = initial_cmdsn; |
7996a778 MC |
2372 | session->exp_cmdsn = initial_cmdsn + 1; |
2373 | session->max_cmdsn = initial_cmdsn + 1; | |
2374 | session->max_r2t = 1; | |
2375 | session->tt = iscsit; | |
6724add1 | 2376 | mutex_init(&session->eh_mutex); |
75613521 | 2377 | spin_lock_init(&session->lock); |
7996a778 MC |
2378 | |
2379 | /* initialize SCSI PDU commands pool */ | |
2380 | if (iscsi_pool_init(&session->cmdpool, session->cmds_max, | |
2381 | (void***)&session->cmds, | |
9c19a7d0 | 2382 | cmd_task_size + sizeof(struct iscsi_task))) |
7996a778 MC |
2383 | goto cmdpool_alloc_fail; |
2384 | ||
2385 | /* pre-format cmds pool with ITT */ | |
2386 | for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) { | |
9c19a7d0 | 2387 | struct iscsi_task *task = session->cmds[cmd_i]; |
7996a778 | 2388 | |
9c19a7d0 MC |
2389 | if (cmd_task_size) |
2390 | task->dd_data = &task[1]; | |
2391 | task->itt = cmd_i; | |
3bbaaad9 | 2392 | task->state = ISCSI_TASK_FREE; |
9c19a7d0 | 2393 | INIT_LIST_HEAD(&task->running); |
7996a778 MC |
2394 | } |
2395 | ||
f53a88da | 2396 | if (!try_module_get(iscsit->owner)) |
75613521 | 2397 | goto module_get_fail; |
7996a778 | 2398 | |
7970634b | 2399 | if (iscsi_add_session(cls_session, id)) |
75613521 | 2400 | goto cls_session_fail; |
e5bd7b54 | 2401 | |
7996a778 MC |
2402 | return cls_session; |
2403 | ||
2404 | cls_session_fail: | |
75613521 MC |
2405 | module_put(iscsit->owner); |
2406 | module_get_fail: | |
6320377f | 2407 | iscsi_pool_free(&session->cmdpool); |
7996a778 | 2408 | cmdpool_alloc_fail: |
75613521 | 2409 | iscsi_free_session(cls_session); |
e5bd7b54 MC |
2410 | dec_session_count: |
2411 | iscsi_host_dec_session_cnt(shost); | |
7996a778 MC |
2412 | return NULL; |
2413 | } | |
2414 | EXPORT_SYMBOL_GPL(iscsi_session_setup); | |
2415 | ||
2416 | /** | |
2417 | * iscsi_session_teardown - destroy session, host, and cls_session | |
75613521 | 2418 | * @cls_session: iscsi session |
7996a778 | 2419 | * |
75613521 MC |
2420 | * The driver must have called iscsi_remove_session before |
2421 | * calling this. | |
2422 | */ | |
7996a778 MC |
2423 | void iscsi_session_teardown(struct iscsi_cls_session *cls_session) |
2424 | { | |
75613521 | 2425 | struct iscsi_session *session = cls_session->dd_data; |
63f75cc8 | 2426 | struct module *owner = cls_session->transport->owner; |
e5bd7b54 | 2427 | struct Scsi_Host *shost = session->host; |
7996a778 | 2428 | |
6320377f | 2429 | iscsi_pool_free(&session->cmdpool); |
7996a778 | 2430 | |
b2c64167 MC |
2431 | kfree(session->password); |
2432 | kfree(session->password_in); | |
2433 | kfree(session->username); | |
2434 | kfree(session->username_in); | |
f3ff0c36 | 2435 | kfree(session->targetname); |
88dfd340 MC |
2436 | kfree(session->initiatorname); |
2437 | kfree(session->ifacename); | |
f3ff0c36 | 2438 | |
75613521 | 2439 | iscsi_destroy_session(cls_session); |
e5bd7b54 | 2440 | iscsi_host_dec_session_cnt(shost); |
63f75cc8 | 2441 | module_put(owner); |
7996a778 MC |
2442 | } |
2443 | EXPORT_SYMBOL_GPL(iscsi_session_teardown); | |
2444 | ||
2445 | /** | |
2446 | * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn | |
2447 | * @cls_session: iscsi_cls_session | |
5d91e209 | 2448 | * @dd_size: private driver data size |
7996a778 | 2449 | * @conn_idx: cid |
5d91e209 | 2450 | */ |
7996a778 | 2451 | struct iscsi_cls_conn * |
5d91e209 MC |
2452 | iscsi_conn_setup(struct iscsi_cls_session *cls_session, int dd_size, |
2453 | uint32_t conn_idx) | |
7996a778 | 2454 | { |
75613521 | 2455 | struct iscsi_session *session = cls_session->dd_data; |
7996a778 MC |
2456 | struct iscsi_conn *conn; |
2457 | struct iscsi_cls_conn *cls_conn; | |
d36ab6f3 | 2458 | char *data; |
7996a778 | 2459 | |
5d91e209 MC |
2460 | cls_conn = iscsi_create_conn(cls_session, sizeof(*conn) + dd_size, |
2461 | conn_idx); | |
7996a778 MC |
2462 | if (!cls_conn) |
2463 | return NULL; | |
2464 | conn = cls_conn->dd_data; | |
5d91e209 | 2465 | memset(conn, 0, sizeof(*conn) + dd_size); |
7996a778 | 2466 | |
5d91e209 | 2467 | conn->dd_data = cls_conn->dd_data + sizeof(*conn); |
7996a778 MC |
2468 | conn->session = session; |
2469 | conn->cls_conn = cls_conn; | |
2470 | conn->c_stage = ISCSI_CONN_INITIAL_STAGE; | |
2471 | conn->id = conn_idx; | |
2472 | conn->exp_statsn = 0; | |
843c0a8a | 2473 | conn->tmf_state = TMF_INITIAL; |
f6d5180c MC |
2474 | |
2475 | init_timer(&conn->transport_timer); | |
2476 | conn->transport_timer.data = (unsigned long)conn; | |
2477 | conn->transport_timer.function = iscsi_check_transport_timeouts; | |
2478 | ||
843c0a8a | 2479 | INIT_LIST_HEAD(&conn->mgmtqueue); |
3bbaaad9 | 2480 | INIT_LIST_HEAD(&conn->cmdqueue); |
843c0a8a | 2481 | INIT_LIST_HEAD(&conn->requeue); |
c4028958 | 2482 | INIT_WORK(&conn->xmitwork, iscsi_xmitworker); |
7996a778 | 2483 | |
9c19a7d0 | 2484 | /* allocate login_task used for the login/text sequences */ |
7996a778 | 2485 | spin_lock_bh(&session->lock); |
3e5c28ad | 2486 | if (!__kfifo_get(session->cmdpool.queue, |
9c19a7d0 | 2487 | (void*)&conn->login_task, |
7996a778 MC |
2488 | sizeof(void*))) { |
2489 | spin_unlock_bh(&session->lock); | |
9c19a7d0 | 2490 | goto login_task_alloc_fail; |
7996a778 MC |
2491 | } |
2492 | spin_unlock_bh(&session->lock); | |
2493 | ||
cfeb2cf9 MC |
2494 | data = (char *) __get_free_pages(GFP_KERNEL, |
2495 | get_order(ISCSI_DEF_MAX_RECV_SEG_LEN)); | |
d36ab6f3 | 2496 | if (!data) |
9c19a7d0 MC |
2497 | goto login_task_data_alloc_fail; |
2498 | conn->login_task->data = conn->data = data; | |
d36ab6f3 | 2499 | |
843c0a8a | 2500 | init_timer(&conn->tmf_timer); |
7996a778 MC |
2501 | init_waitqueue_head(&conn->ehwait); |
2502 | ||
2503 | return cls_conn; | |
2504 | ||
9c19a7d0 MC |
2505 | login_task_data_alloc_fail: |
2506 | __kfifo_put(session->cmdpool.queue, (void*)&conn->login_task, | |
d36ab6f3 | 2507 | sizeof(void*)); |
9c19a7d0 | 2508 | login_task_alloc_fail: |
7996a778 MC |
2509 | iscsi_destroy_conn(cls_conn); |
2510 | return NULL; | |
2511 | } | |
2512 | EXPORT_SYMBOL_GPL(iscsi_conn_setup); | |
2513 | ||
2514 | /** | |
2515 | * iscsi_conn_teardown - teardown iscsi connection | |
2516 | * cls_conn: iscsi class connection | |
2517 | * | |
2518 | * TODO: we may need to make this into a two step process | |
2519 | * like scsi-mls remove + put host | |
2520 | */ | |
2521 | void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn) | |
2522 | { | |
2523 | struct iscsi_conn *conn = cls_conn->dd_data; | |
2524 | struct iscsi_session *session = conn->session; | |
2525 | unsigned long flags; | |
2526 | ||
f6d5180c MC |
2527 | del_timer_sync(&conn->transport_timer); |
2528 | ||
7996a778 MC |
2529 | spin_lock_bh(&session->lock); |
2530 | conn->c_stage = ISCSI_CONN_CLEANUP_WAIT; | |
2531 | if (session->leadconn == conn) { | |
2532 | /* | |
2533 | * leading connection? then give up on recovery. | |
2534 | */ | |
2535 | session->state = ISCSI_STATE_TERMINATE; | |
2536 | wake_up(&conn->ehwait); | |
2537 | } | |
2538 | spin_unlock_bh(&session->lock); | |
2539 | ||
7996a778 MC |
2540 | /* |
2541 | * Block until all in-progress commands for this connection | |
2542 | * time out or fail. | |
2543 | */ | |
2544 | for (;;) { | |
2545 | spin_lock_irqsave(session->host->host_lock, flags); | |
2546 | if (!session->host->host_busy) { /* OK for ERL == 0 */ | |
2547 | spin_unlock_irqrestore(session->host->host_lock, flags); | |
2548 | break; | |
2549 | } | |
2550 | spin_unlock_irqrestore(session->host->host_lock, flags); | |
2551 | msleep_interruptible(500); | |
322d739d MC |
2552 | iscsi_conn_printk(KERN_INFO, conn, "iscsi conn_destroy(): " |
2553 | "host_busy %d host_failed %d\n", | |
2554 | session->host->host_busy, | |
2555 | session->host->host_failed); | |
7996a778 MC |
2556 | /* |
2557 | * force eh_abort() to unblock | |
2558 | */ | |
2559 | wake_up(&conn->ehwait); | |
2560 | } | |
2561 | ||
779ea120 | 2562 | /* flush queued up work because we free the connection below */ |
843c0a8a | 2563 | iscsi_suspend_tx(conn); |
779ea120 | 2564 | |
7996a778 | 2565 | spin_lock_bh(&session->lock); |
cfeb2cf9 MC |
2566 | free_pages((unsigned long) conn->data, |
2567 | get_order(ISCSI_DEF_MAX_RECV_SEG_LEN)); | |
f3ff0c36 | 2568 | kfree(conn->persistent_address); |
9c19a7d0 | 2569 | __kfifo_put(session->cmdpool.queue, (void*)&conn->login_task, |
7996a778 | 2570 | sizeof(void*)); |
e0726407 | 2571 | if (session->leadconn == conn) |
7996a778 | 2572 | session->leadconn = NULL; |
7996a778 MC |
2573 | spin_unlock_bh(&session->lock); |
2574 | ||
7996a778 MC |
2575 | iscsi_destroy_conn(cls_conn); |
2576 | } | |
2577 | EXPORT_SYMBOL_GPL(iscsi_conn_teardown); | |
2578 | ||
2579 | int iscsi_conn_start(struct iscsi_cls_conn *cls_conn) | |
2580 | { | |
2581 | struct iscsi_conn *conn = cls_conn->dd_data; | |
2582 | struct iscsi_session *session = conn->session; | |
2583 | ||
ffd0436e | 2584 | if (!session) { |
322d739d MC |
2585 | iscsi_conn_printk(KERN_ERR, conn, |
2586 | "can't start unbound connection\n"); | |
7996a778 MC |
2587 | return -EPERM; |
2588 | } | |
2589 | ||
db98ccde MC |
2590 | if ((session->imm_data_en || !session->initial_r2t_en) && |
2591 | session->first_burst > session->max_burst) { | |
322d739d MC |
2592 | iscsi_conn_printk(KERN_INFO, conn, "invalid burst lengths: " |
2593 | "first_burst %d max_burst %d\n", | |
2594 | session->first_burst, session->max_burst); | |
ffd0436e MC |
2595 | return -EINVAL; |
2596 | } | |
2597 | ||
f6d5180c | 2598 | if (conn->ping_timeout && !conn->recv_timeout) { |
322d739d MC |
2599 | iscsi_conn_printk(KERN_ERR, conn, "invalid recv timeout of " |
2600 | "zero. Using 5 seconds\n."); | |
f6d5180c MC |
2601 | conn->recv_timeout = 5; |
2602 | } | |
2603 | ||
2604 | if (conn->recv_timeout && !conn->ping_timeout) { | |
322d739d MC |
2605 | iscsi_conn_printk(KERN_ERR, conn, "invalid ping timeout of " |
2606 | "zero. Using 5 seconds.\n"); | |
f6d5180c MC |
2607 | conn->ping_timeout = 5; |
2608 | } | |
2609 | ||
7996a778 MC |
2610 | spin_lock_bh(&session->lock); |
2611 | conn->c_stage = ISCSI_CONN_STARTED; | |
2612 | session->state = ISCSI_STATE_LOGGED_IN; | |
e0726407 | 2613 | session->queued_cmdsn = session->cmdsn; |
7996a778 | 2614 | |
f6d5180c MC |
2615 | conn->last_recv = jiffies; |
2616 | conn->last_ping = jiffies; | |
2617 | if (conn->recv_timeout && conn->ping_timeout) | |
2618 | mod_timer(&conn->transport_timer, | |
2619 | jiffies + (conn->recv_timeout * HZ)); | |
2620 | ||
7996a778 MC |
2621 | switch(conn->stop_stage) { |
2622 | case STOP_CONN_RECOVER: | |
2623 | /* | |
2624 | * unblock eh_abort() if it is blocked. re-try all | |
2625 | * commands after successful recovery | |
2626 | */ | |
7996a778 | 2627 | conn->stop_stage = 0; |
843c0a8a | 2628 | conn->tmf_state = TMF_INITIAL; |
7996a778 | 2629 | session->age++; |
8b1d0343 MC |
2630 | if (session->age == 16) |
2631 | session->age = 0; | |
6eabafbe | 2632 | break; |
7996a778 | 2633 | case STOP_CONN_TERM: |
7996a778 | 2634 | conn->stop_stage = 0; |
7996a778 MC |
2635 | break; |
2636 | default: | |
2637 | break; | |
2638 | } | |
2639 | spin_unlock_bh(&session->lock); | |
2640 | ||
75613521 | 2641 | iscsi_unblock_session(session->cls_session); |
6eabafbe | 2642 | wake_up(&conn->ehwait); |
7996a778 MC |
2643 | return 0; |
2644 | } | |
2645 | EXPORT_SYMBOL_GPL(iscsi_conn_start); | |
2646 | ||
2647 | static void | |
3bbaaad9 | 2648 | fail_mgmt_tasks(struct iscsi_session *session, struct iscsi_conn *conn) |
7996a778 | 2649 | { |
3bbaaad9 | 2650 | struct iscsi_task *task; |
b3cd5050 | 2651 | int i, state; |
7996a778 | 2652 | |
3bbaaad9 MC |
2653 | for (i = 0; i < conn->session->cmds_max; i++) { |
2654 | task = conn->session->cmds[i]; | |
2655 | if (task->sc) | |
2656 | continue; | |
7996a778 | 2657 | |
3bbaaad9 MC |
2658 | if (task->state == ISCSI_TASK_FREE) |
2659 | continue; | |
7996a778 | 2660 | |
3bbaaad9 MC |
2661 | ISCSI_DBG_SESSION(conn->session, |
2662 | "failing mgmt itt 0x%x state %d\n", | |
2663 | task->itt, task->state); | |
b3cd5050 MC |
2664 | state = ISCSI_TASK_ABRT_SESS_RECOV; |
2665 | if (task->state == ISCSI_TASK_PENDING) | |
2666 | state = ISCSI_TASK_COMPLETED; | |
2667 | iscsi_complete_task(task, state); | |
2668 | ||
3bbaaad9 | 2669 | } |
7996a778 MC |
2670 | } |
2671 | ||
656cffc9 MC |
2672 | static void iscsi_start_session_recovery(struct iscsi_session *session, |
2673 | struct iscsi_conn *conn, int flag) | |
7996a778 | 2674 | { |
ed2abc7f MC |
2675 | int old_stop_stage; |
2676 | ||
6724add1 | 2677 | mutex_lock(&session->eh_mutex); |
7996a778 | 2678 | spin_lock_bh(&session->lock); |
ed2abc7f | 2679 | if (conn->stop_stage == STOP_CONN_TERM) { |
7996a778 | 2680 | spin_unlock_bh(&session->lock); |
6724add1 MC |
2681 | mutex_unlock(&session->eh_mutex); |
2682 | return; | |
2683 | } | |
2684 | ||
ed2abc7f MC |
2685 | /* |
2686 | * When this is called for the in_login state, we only want to clean | |
9c19a7d0 | 2687 | * up the login task and connection. We do not need to block and set |
67a61114 | 2688 | * the recovery state again |
ed2abc7f | 2689 | */ |
67a61114 MC |
2690 | if (flag == STOP_CONN_TERM) |
2691 | session->state = ISCSI_STATE_TERMINATE; | |
2692 | else if (conn->stop_stage != STOP_CONN_RECOVER) | |
2693 | session->state = ISCSI_STATE_IN_RECOVERY; | |
26013ad4 | 2694 | spin_unlock_bh(&session->lock); |
ed2abc7f | 2695 | |
26013ad4 MC |
2696 | del_timer_sync(&conn->transport_timer); |
2697 | iscsi_suspend_tx(conn); | |
2698 | ||
2699 | spin_lock_bh(&session->lock); | |
ed2abc7f | 2700 | old_stop_stage = conn->stop_stage; |
7996a778 | 2701 | conn->stop_stage = flag; |
67a61114 | 2702 | conn->c_stage = ISCSI_CONN_STOPPED; |
7996a778 | 2703 | spin_unlock_bh(&session->lock); |
6724add1 | 2704 | |
7996a778 MC |
2705 | /* |
2706 | * for connection level recovery we should not calculate | |
2707 | * header digest. conn->hdr_size used for optimization | |
2708 | * in hdr_extract() and will be re-negotiated at | |
2709 | * set_param() time. | |
2710 | */ | |
2711 | if (flag == STOP_CONN_RECOVER) { | |
2712 | conn->hdrdgst_en = 0; | |
2713 | conn->datadgst_en = 0; | |
656cffc9 | 2714 | if (session->state == ISCSI_STATE_IN_RECOVERY && |
67a61114 | 2715 | old_stop_stage != STOP_CONN_RECOVER) { |
1b2c7af8 | 2716 | ISCSI_DBG_SESSION(session, "blocking session\n"); |
75613521 | 2717 | iscsi_block_session(session->cls_session); |
67a61114 | 2718 | } |
7996a778 | 2719 | } |
656cffc9 | 2720 | |
656cffc9 MC |
2721 | /* |
2722 | * flush queues. | |
2723 | */ | |
2724 | spin_lock_bh(&session->lock); | |
b3cd5050 | 2725 | fail_scsi_tasks(conn, -1, DID_TRANSPORT_DISRUPTED); |
3bbaaad9 | 2726 | fail_mgmt_tasks(session, conn); |
656cffc9 | 2727 | spin_unlock_bh(&session->lock); |
6724add1 | 2728 | mutex_unlock(&session->eh_mutex); |
7996a778 | 2729 | } |
7996a778 MC |
2730 | |
2731 | void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag) | |
2732 | { | |
2733 | struct iscsi_conn *conn = cls_conn->dd_data; | |
2734 | struct iscsi_session *session = conn->session; | |
2735 | ||
2736 | switch (flag) { | |
2737 | case STOP_CONN_RECOVER: | |
2738 | case STOP_CONN_TERM: | |
2739 | iscsi_start_session_recovery(session, conn, flag); | |
8d2860b3 | 2740 | break; |
7996a778 | 2741 | default: |
322d739d MC |
2742 | iscsi_conn_printk(KERN_ERR, conn, |
2743 | "invalid stop flag %d\n", flag); | |
7996a778 MC |
2744 | } |
2745 | } | |
2746 | EXPORT_SYMBOL_GPL(iscsi_conn_stop); | |
2747 | ||
2748 | int iscsi_conn_bind(struct iscsi_cls_session *cls_session, | |
2749 | struct iscsi_cls_conn *cls_conn, int is_leading) | |
2750 | { | |
75613521 | 2751 | struct iscsi_session *session = cls_session->dd_data; |
98644047 | 2752 | struct iscsi_conn *conn = cls_conn->dd_data; |
7996a778 | 2753 | |
7996a778 | 2754 | spin_lock_bh(&session->lock); |
7996a778 MC |
2755 | if (is_leading) |
2756 | session->leadconn = conn; | |
98644047 | 2757 | spin_unlock_bh(&session->lock); |
7996a778 MC |
2758 | |
2759 | /* | |
2760 | * Unblock xmitworker(), Login Phase will pass through. | |
2761 | */ | |
2762 | clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx); | |
2763 | clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx); | |
2764 | return 0; | |
2765 | } | |
2766 | EXPORT_SYMBOL_GPL(iscsi_conn_bind); | |
2767 | ||
5700b1af MC |
2768 | static int iscsi_switch_str_param(char **param, char *new_val_buf) |
2769 | { | |
2770 | char *new_val; | |
2771 | ||
2772 | if (*param) { | |
2773 | if (!strcmp(*param, new_val_buf)) | |
2774 | return 0; | |
2775 | } | |
2776 | ||
2777 | new_val = kstrdup(new_val_buf, GFP_NOIO); | |
2778 | if (!new_val) | |
2779 | return -ENOMEM; | |
2780 | ||
2781 | kfree(*param); | |
2782 | *param = new_val; | |
2783 | return 0; | |
2784 | } | |
a54a52ca MC |
2785 | |
2786 | int iscsi_set_param(struct iscsi_cls_conn *cls_conn, | |
2787 | enum iscsi_param param, char *buf, int buflen) | |
2788 | { | |
2789 | struct iscsi_conn *conn = cls_conn->dd_data; | |
2790 | struct iscsi_session *session = conn->session; | |
2791 | uint32_t value; | |
2792 | ||
2793 | switch(param) { | |
843c0a8a MC |
2794 | case ISCSI_PARAM_FAST_ABORT: |
2795 | sscanf(buf, "%d", &session->fast_abort); | |
2796 | break; | |
f6d5180c MC |
2797 | case ISCSI_PARAM_ABORT_TMO: |
2798 | sscanf(buf, "%d", &session->abort_timeout); | |
2799 | break; | |
2800 | case ISCSI_PARAM_LU_RESET_TMO: | |
2801 | sscanf(buf, "%d", &session->lu_reset_timeout); | |
2802 | break; | |
2803 | case ISCSI_PARAM_PING_TMO: | |
2804 | sscanf(buf, "%d", &conn->ping_timeout); | |
2805 | break; | |
2806 | case ISCSI_PARAM_RECV_TMO: | |
2807 | sscanf(buf, "%d", &conn->recv_timeout); | |
2808 | break; | |
a54a52ca MC |
2809 | case ISCSI_PARAM_MAX_RECV_DLENGTH: |
2810 | sscanf(buf, "%d", &conn->max_recv_dlength); | |
2811 | break; | |
2812 | case ISCSI_PARAM_MAX_XMIT_DLENGTH: | |
2813 | sscanf(buf, "%d", &conn->max_xmit_dlength); | |
2814 | break; | |
2815 | case ISCSI_PARAM_HDRDGST_EN: | |
2816 | sscanf(buf, "%d", &conn->hdrdgst_en); | |
2817 | break; | |
2818 | case ISCSI_PARAM_DATADGST_EN: | |
2819 | sscanf(buf, "%d", &conn->datadgst_en); | |
2820 | break; | |
2821 | case ISCSI_PARAM_INITIAL_R2T_EN: | |
2822 | sscanf(buf, "%d", &session->initial_r2t_en); | |
2823 | break; | |
2824 | case ISCSI_PARAM_MAX_R2T: | |
2825 | sscanf(buf, "%d", &session->max_r2t); | |
2826 | break; | |
2827 | case ISCSI_PARAM_IMM_DATA_EN: | |
2828 | sscanf(buf, "%d", &session->imm_data_en); | |
2829 | break; | |
2830 | case ISCSI_PARAM_FIRST_BURST: | |
2831 | sscanf(buf, "%d", &session->first_burst); | |
2832 | break; | |
2833 | case ISCSI_PARAM_MAX_BURST: | |
2834 | sscanf(buf, "%d", &session->max_burst); | |
2835 | break; | |
2836 | case ISCSI_PARAM_PDU_INORDER_EN: | |
2837 | sscanf(buf, "%d", &session->pdu_inorder_en); | |
2838 | break; | |
2839 | case ISCSI_PARAM_DATASEQ_INORDER_EN: | |
2840 | sscanf(buf, "%d", &session->dataseq_inorder_en); | |
2841 | break; | |
2842 | case ISCSI_PARAM_ERL: | |
2843 | sscanf(buf, "%d", &session->erl); | |
2844 | break; | |
2845 | case ISCSI_PARAM_IFMARKER_EN: | |
2846 | sscanf(buf, "%d", &value); | |
2847 | BUG_ON(value); | |
2848 | break; | |
2849 | case ISCSI_PARAM_OFMARKER_EN: | |
2850 | sscanf(buf, "%d", &value); | |
2851 | BUG_ON(value); | |
2852 | break; | |
2853 | case ISCSI_PARAM_EXP_STATSN: | |
2854 | sscanf(buf, "%u", &conn->exp_statsn); | |
2855 | break; | |
b2c64167 | 2856 | case ISCSI_PARAM_USERNAME: |
5700b1af | 2857 | return iscsi_switch_str_param(&session->username, buf); |
b2c64167 | 2858 | case ISCSI_PARAM_USERNAME_IN: |
5700b1af | 2859 | return iscsi_switch_str_param(&session->username_in, buf); |
b2c64167 | 2860 | case ISCSI_PARAM_PASSWORD: |
5700b1af | 2861 | return iscsi_switch_str_param(&session->password, buf); |
b2c64167 | 2862 | case ISCSI_PARAM_PASSWORD_IN: |
5700b1af | 2863 | return iscsi_switch_str_param(&session->password_in, buf); |
a54a52ca | 2864 | case ISCSI_PARAM_TARGET_NAME: |
5700b1af | 2865 | return iscsi_switch_str_param(&session->targetname, buf); |
a54a52ca MC |
2866 | case ISCSI_PARAM_TPGT: |
2867 | sscanf(buf, "%d", &session->tpgt); | |
2868 | break; | |
2869 | case ISCSI_PARAM_PERSISTENT_PORT: | |
2870 | sscanf(buf, "%d", &conn->persistent_port); | |
2871 | break; | |
2872 | case ISCSI_PARAM_PERSISTENT_ADDRESS: | |
5700b1af | 2873 | return iscsi_switch_str_param(&conn->persistent_address, buf); |
88dfd340 | 2874 | case ISCSI_PARAM_IFACE_NAME: |
5700b1af | 2875 | return iscsi_switch_str_param(&session->ifacename, buf); |
88dfd340 | 2876 | case ISCSI_PARAM_INITIATOR_NAME: |
5700b1af | 2877 | return iscsi_switch_str_param(&session->initiatorname, buf); |
a54a52ca MC |
2878 | default: |
2879 | return -ENOSYS; | |
2880 | } | |
2881 | ||
2882 | return 0; | |
2883 | } | |
2884 | EXPORT_SYMBOL_GPL(iscsi_set_param); | |
2885 | ||
2886 | int iscsi_session_get_param(struct iscsi_cls_session *cls_session, | |
2887 | enum iscsi_param param, char *buf) | |
2888 | { | |
75613521 | 2889 | struct iscsi_session *session = cls_session->dd_data; |
a54a52ca MC |
2890 | int len; |
2891 | ||
2892 | switch(param) { | |
843c0a8a MC |
2893 | case ISCSI_PARAM_FAST_ABORT: |
2894 | len = sprintf(buf, "%d\n", session->fast_abort); | |
2895 | break; | |
f6d5180c MC |
2896 | case ISCSI_PARAM_ABORT_TMO: |
2897 | len = sprintf(buf, "%d\n", session->abort_timeout); | |
2898 | break; | |
2899 | case ISCSI_PARAM_LU_RESET_TMO: | |
2900 | len = sprintf(buf, "%d\n", session->lu_reset_timeout); | |
2901 | break; | |
a54a52ca MC |
2902 | case ISCSI_PARAM_INITIAL_R2T_EN: |
2903 | len = sprintf(buf, "%d\n", session->initial_r2t_en); | |
2904 | break; | |
2905 | case ISCSI_PARAM_MAX_R2T: | |
2906 | len = sprintf(buf, "%hu\n", session->max_r2t); | |
2907 | break; | |
2908 | case ISCSI_PARAM_IMM_DATA_EN: | |
2909 | len = sprintf(buf, "%d\n", session->imm_data_en); | |
2910 | break; | |
2911 | case ISCSI_PARAM_FIRST_BURST: | |
2912 | len = sprintf(buf, "%u\n", session->first_burst); | |
2913 | break; | |
2914 | case ISCSI_PARAM_MAX_BURST: | |
2915 | len = sprintf(buf, "%u\n", session->max_burst); | |
2916 | break; | |
2917 | case ISCSI_PARAM_PDU_INORDER_EN: | |
2918 | len = sprintf(buf, "%d\n", session->pdu_inorder_en); | |
2919 | break; | |
2920 | case ISCSI_PARAM_DATASEQ_INORDER_EN: | |
2921 | len = sprintf(buf, "%d\n", session->dataseq_inorder_en); | |
2922 | break; | |
2923 | case ISCSI_PARAM_ERL: | |
2924 | len = sprintf(buf, "%d\n", session->erl); | |
2925 | break; | |
2926 | case ISCSI_PARAM_TARGET_NAME: | |
2927 | len = sprintf(buf, "%s\n", session->targetname); | |
2928 | break; | |
2929 | case ISCSI_PARAM_TPGT: | |
2930 | len = sprintf(buf, "%d\n", session->tpgt); | |
2931 | break; | |
b2c64167 MC |
2932 | case ISCSI_PARAM_USERNAME: |
2933 | len = sprintf(buf, "%s\n", session->username); | |
2934 | break; | |
2935 | case ISCSI_PARAM_USERNAME_IN: | |
2936 | len = sprintf(buf, "%s\n", session->username_in); | |
2937 | break; | |
2938 | case ISCSI_PARAM_PASSWORD: | |
2939 | len = sprintf(buf, "%s\n", session->password); | |
2940 | break; | |
2941 | case ISCSI_PARAM_PASSWORD_IN: | |
2942 | len = sprintf(buf, "%s\n", session->password_in); | |
2943 | break; | |
88dfd340 MC |
2944 | case ISCSI_PARAM_IFACE_NAME: |
2945 | len = sprintf(buf, "%s\n", session->ifacename); | |
2946 | break; | |
2947 | case ISCSI_PARAM_INITIATOR_NAME: | |
5700b1af | 2948 | len = sprintf(buf, "%s\n", session->initiatorname); |
88dfd340 | 2949 | break; |
a54a52ca MC |
2950 | default: |
2951 | return -ENOSYS; | |
2952 | } | |
2953 | ||
2954 | return len; | |
2955 | } | |
2956 | EXPORT_SYMBOL_GPL(iscsi_session_get_param); | |
2957 | ||
2958 | int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn, | |
2959 | enum iscsi_param param, char *buf) | |
2960 | { | |
2961 | struct iscsi_conn *conn = cls_conn->dd_data; | |
2962 | int len; | |
2963 | ||
2964 | switch(param) { | |
f6d5180c MC |
2965 | case ISCSI_PARAM_PING_TMO: |
2966 | len = sprintf(buf, "%u\n", conn->ping_timeout); | |
2967 | break; | |
2968 | case ISCSI_PARAM_RECV_TMO: | |
2969 | len = sprintf(buf, "%u\n", conn->recv_timeout); | |
2970 | break; | |
a54a52ca MC |
2971 | case ISCSI_PARAM_MAX_RECV_DLENGTH: |
2972 | len = sprintf(buf, "%u\n", conn->max_recv_dlength); | |
2973 | break; | |
2974 | case ISCSI_PARAM_MAX_XMIT_DLENGTH: | |
2975 | len = sprintf(buf, "%u\n", conn->max_xmit_dlength); | |
2976 | break; | |
2977 | case ISCSI_PARAM_HDRDGST_EN: | |
2978 | len = sprintf(buf, "%d\n", conn->hdrdgst_en); | |
2979 | break; | |
2980 | case ISCSI_PARAM_DATADGST_EN: | |
2981 | len = sprintf(buf, "%d\n", conn->datadgst_en); | |
2982 | break; | |
2983 | case ISCSI_PARAM_IFMARKER_EN: | |
2984 | len = sprintf(buf, "%d\n", conn->ifmarker_en); | |
2985 | break; | |
2986 | case ISCSI_PARAM_OFMARKER_EN: | |
2987 | len = sprintf(buf, "%d\n", conn->ofmarker_en); | |
2988 | break; | |
2989 | case ISCSI_PARAM_EXP_STATSN: | |
2990 | len = sprintf(buf, "%u\n", conn->exp_statsn); | |
2991 | break; | |
2992 | case ISCSI_PARAM_PERSISTENT_PORT: | |
2993 | len = sprintf(buf, "%d\n", conn->persistent_port); | |
2994 | break; | |
2995 | case ISCSI_PARAM_PERSISTENT_ADDRESS: | |
2996 | len = sprintf(buf, "%s\n", conn->persistent_address); | |
2997 | break; | |
2998 | default: | |
2999 | return -ENOSYS; | |
3000 | } | |
3001 | ||
3002 | return len; | |
3003 | } | |
3004 | EXPORT_SYMBOL_GPL(iscsi_conn_get_param); | |
3005 | ||
0801c242 MC |
3006 | int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param, |
3007 | char *buf) | |
3008 | { | |
75613521 | 3009 | struct iscsi_host *ihost = shost_priv(shost); |
0801c242 MC |
3010 | int len; |
3011 | ||
3012 | switch (param) { | |
d8196ed2 | 3013 | case ISCSI_HOST_PARAM_NETDEV_NAME: |
5700b1af | 3014 | len = sprintf(buf, "%s\n", ihost->netdev); |
d8196ed2 | 3015 | break; |
0801c242 | 3016 | case ISCSI_HOST_PARAM_HWADDRESS: |
5700b1af | 3017 | len = sprintf(buf, "%s\n", ihost->hwaddress); |
0801c242 | 3018 | break; |
8ad5781a | 3019 | case ISCSI_HOST_PARAM_INITIATOR_NAME: |
5700b1af | 3020 | len = sprintf(buf, "%s\n", ihost->initiatorname); |
8ad5781a | 3021 | break; |
75613521 | 3022 | case ISCSI_HOST_PARAM_IPADDRESS: |
5700b1af | 3023 | len = sprintf(buf, "%s\n", ihost->local_address); |
88dfd340 | 3024 | break; |
0801c242 MC |
3025 | default: |
3026 | return -ENOSYS; | |
3027 | } | |
3028 | ||
3029 | return len; | |
3030 | } | |
3031 | EXPORT_SYMBOL_GPL(iscsi_host_get_param); | |
3032 | ||
3033 | int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param, | |
3034 | char *buf, int buflen) | |
3035 | { | |
75613521 | 3036 | struct iscsi_host *ihost = shost_priv(shost); |
0801c242 MC |
3037 | |
3038 | switch (param) { | |
d8196ed2 | 3039 | case ISCSI_HOST_PARAM_NETDEV_NAME: |
5700b1af | 3040 | return iscsi_switch_str_param(&ihost->netdev, buf); |
0801c242 | 3041 | case ISCSI_HOST_PARAM_HWADDRESS: |
5700b1af | 3042 | return iscsi_switch_str_param(&ihost->hwaddress, buf); |
8ad5781a | 3043 | case ISCSI_HOST_PARAM_INITIATOR_NAME: |
5700b1af | 3044 | return iscsi_switch_str_param(&ihost->initiatorname, buf); |
0801c242 MC |
3045 | default: |
3046 | return -ENOSYS; | |
3047 | } | |
3048 | ||
3049 | return 0; | |
3050 | } | |
3051 | EXPORT_SYMBOL_GPL(iscsi_host_set_param); | |
3052 | ||
7996a778 MC |
3053 | MODULE_AUTHOR("Mike Christie"); |
3054 | MODULE_DESCRIPTION("iSCSI library functions"); | |
3055 | MODULE_LICENSE("GPL"); |