]>
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 | ||
41 | struct iscsi_session * | |
42 | class_to_transport_session(struct iscsi_cls_session *cls_session) | |
43 | { | |
44 | struct Scsi_Host *shost = iscsi_session_to_shost(cls_session); | |
45 | return iscsi_hostdata(shost->hostdata); | |
46 | } | |
47 | EXPORT_SYMBOL_GPL(class_to_transport_session); | |
48 | ||
77a23c21 MC |
49 | /* Serial Number Arithmetic, 32 bits, less than, RFC1982 */ |
50 | #define SNA32_CHECK 2147483648UL | |
7996a778 | 51 | |
77a23c21 MC |
52 | static int iscsi_sna_lt(u32 n1, u32 n2) |
53 | { | |
54 | return n1 != n2 && ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) || | |
55 | (n1 > n2 && (n2 - n1 < SNA32_CHECK))); | |
56 | } | |
57 | ||
58 | /* Serial Number Arithmetic, 32 bits, less than, RFC1982 */ | |
59 | static int iscsi_sna_lte(u32 n1, u32 n2) | |
60 | { | |
61 | return n1 == n2 || ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) || | |
62 | (n1 > n2 && (n2 - n1 < SNA32_CHECK))); | |
63 | } | |
64 | ||
65 | void | |
66 | iscsi_update_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr) | |
7996a778 MC |
67 | { |
68 | uint32_t max_cmdsn = be32_to_cpu(hdr->max_cmdsn); | |
69 | uint32_t exp_cmdsn = be32_to_cpu(hdr->exp_cmdsn); | |
70 | ||
77a23c21 MC |
71 | /* |
72 | * standard specifies this check for when to update expected and | |
73 | * max sequence numbers | |
74 | */ | |
75 | if (iscsi_sna_lt(max_cmdsn, exp_cmdsn - 1)) | |
76 | return; | |
77 | ||
78 | if (exp_cmdsn != session->exp_cmdsn && | |
79 | !iscsi_sna_lt(exp_cmdsn, session->exp_cmdsn)) | |
7996a778 MC |
80 | session->exp_cmdsn = exp_cmdsn; |
81 | ||
77a23c21 MC |
82 | if (max_cmdsn != session->max_cmdsn && |
83 | !iscsi_sna_lt(max_cmdsn, session->max_cmdsn)) { | |
84 | session->max_cmdsn = max_cmdsn; | |
85 | /* | |
86 | * if the window closed with IO queued, then kick the | |
87 | * xmit thread | |
88 | */ | |
89 | if (!list_empty(&session->leadconn->xmitqueue) || | |
843c0a8a | 90 | !list_empty(&session->leadconn->mgmtqueue)) |
77a23c21 MC |
91 | scsi_queue_work(session->host, |
92 | &session->leadconn->xmitwork); | |
93 | } | |
7996a778 | 94 | } |
77a23c21 | 95 | EXPORT_SYMBOL_GPL(iscsi_update_cmdsn); |
7996a778 MC |
96 | |
97 | void iscsi_prep_unsolicit_data_pdu(struct iscsi_cmd_task *ctask, | |
ffd0436e | 98 | struct iscsi_data *hdr) |
7996a778 MC |
99 | { |
100 | struct iscsi_conn *conn = ctask->conn; | |
101 | ||
102 | memset(hdr, 0, sizeof(struct iscsi_data)); | |
103 | hdr->ttt = cpu_to_be32(ISCSI_RESERVED_TAG); | |
104 | hdr->datasn = cpu_to_be32(ctask->unsol_datasn); | |
105 | ctask->unsol_datasn++; | |
106 | hdr->opcode = ISCSI_OP_SCSI_DATA_OUT; | |
107 | memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun)); | |
108 | ||
109 | hdr->itt = ctask->hdr->itt; | |
110 | hdr->exp_statsn = cpu_to_be32(conn->exp_statsn); | |
ffd0436e | 111 | hdr->offset = cpu_to_be32(ctask->unsol_offset); |
7996a778 MC |
112 | |
113 | if (ctask->unsol_count > conn->max_xmit_dlength) { | |
114 | hton24(hdr->dlength, conn->max_xmit_dlength); | |
115 | ctask->data_count = conn->max_xmit_dlength; | |
ffd0436e | 116 | ctask->unsol_offset += ctask->data_count; |
7996a778 MC |
117 | hdr->flags = 0; |
118 | } else { | |
119 | hton24(hdr->dlength, ctask->unsol_count); | |
120 | ctask->data_count = ctask->unsol_count; | |
121 | hdr->flags = ISCSI_FLAG_CMD_FINAL; | |
122 | } | |
123 | } | |
124 | EXPORT_SYMBOL_GPL(iscsi_prep_unsolicit_data_pdu); | |
125 | ||
004d6530 BH |
126 | static int iscsi_add_hdr(struct iscsi_cmd_task *ctask, unsigned len) |
127 | { | |
128 | unsigned exp_len = ctask->hdr_len + len; | |
129 | ||
130 | if (exp_len > ctask->hdr_max) { | |
131 | WARN_ON(1); | |
132 | return -EINVAL; | |
133 | } | |
134 | ||
135 | WARN_ON(len & (ISCSI_PAD_LEN - 1)); /* caller must pad the AHS */ | |
136 | ctask->hdr_len = exp_len; | |
137 | return 0; | |
138 | } | |
139 | ||
38d1c069 BH |
140 | /* |
141 | * make an extended cdb AHS | |
142 | */ | |
143 | static int iscsi_prep_ecdb_ahs(struct iscsi_cmd_task *ctask) | |
144 | { | |
145 | struct scsi_cmnd *cmd = ctask->sc; | |
146 | unsigned rlen, pad_len; | |
147 | unsigned short ahslength; | |
148 | struct iscsi_ecdb_ahdr *ecdb_ahdr; | |
149 | int rc; | |
150 | ||
151 | ecdb_ahdr = iscsi_next_hdr(ctask); | |
152 | rlen = cmd->cmd_len - ISCSI_CDB_SIZE; | |
153 | ||
154 | BUG_ON(rlen > sizeof(ecdb_ahdr->ecdb)); | |
155 | ahslength = rlen + sizeof(ecdb_ahdr->reserved); | |
156 | ||
157 | pad_len = iscsi_padding(rlen); | |
158 | ||
159 | rc = iscsi_add_hdr(ctask, sizeof(ecdb_ahdr->ahslength) + | |
160 | sizeof(ecdb_ahdr->ahstype) + ahslength + pad_len); | |
161 | if (rc) | |
162 | return rc; | |
163 | ||
164 | if (pad_len) | |
165 | memset(&ecdb_ahdr->ecdb[rlen], 0, pad_len); | |
166 | ||
167 | ecdb_ahdr->ahslength = cpu_to_be16(ahslength); | |
168 | ecdb_ahdr->ahstype = ISCSI_AHSTYPE_CDB; | |
169 | ecdb_ahdr->reserved = 0; | |
170 | memcpy(ecdb_ahdr->ecdb, cmd->cmnd + ISCSI_CDB_SIZE, rlen); | |
171 | ||
172 | debug_scsi("iscsi_prep_ecdb_ahs: varlen_cdb_len %d " | |
173 | "rlen %d pad_len %d ahs_length %d iscsi_headers_size %u\n", | |
174 | cmd->cmd_len, rlen, pad_len, ahslength, ctask->hdr_len); | |
175 | ||
176 | return 0; | |
177 | } | |
178 | ||
7996a778 MC |
179 | /** |
180 | * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu | |
181 | * @ctask: iscsi cmd task | |
182 | * | |
183 | * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set | |
184 | * fields like dlength or final based on how much data it sends | |
185 | */ | |
004d6530 | 186 | static int iscsi_prep_scsi_cmd_pdu(struct iscsi_cmd_task *ctask) |
7996a778 MC |
187 | { |
188 | struct iscsi_conn *conn = ctask->conn; | |
189 | struct iscsi_session *session = conn->session; | |
190 | struct iscsi_cmd *hdr = ctask->hdr; | |
191 | struct scsi_cmnd *sc = ctask->sc; | |
38d1c069 | 192 | unsigned hdrlength, cmd_len; |
004d6530 | 193 | int rc; |
7996a778 | 194 | |
004d6530 BH |
195 | ctask->hdr_len = 0; |
196 | rc = iscsi_add_hdr(ctask, sizeof(*hdr)); | |
197 | if (rc) | |
198 | return rc; | |
a8ac6311 OK |
199 | hdr->opcode = ISCSI_OP_SCSI_CMD; |
200 | hdr->flags = ISCSI_ATTR_SIMPLE; | |
201 | int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun); | |
8b1d0343 | 202 | hdr->itt = build_itt(ctask->itt, session->age); |
a8ac6311 OK |
203 | hdr->data_length = cpu_to_be32(scsi_bufflen(sc)); |
204 | hdr->cmdsn = cpu_to_be32(session->cmdsn); | |
205 | session->cmdsn++; | |
206 | hdr->exp_statsn = cpu_to_be32(conn->exp_statsn); | |
38d1c069 BH |
207 | cmd_len = sc->cmd_len; |
208 | if (cmd_len < ISCSI_CDB_SIZE) | |
209 | memset(&hdr->cdb[cmd_len], 0, ISCSI_CDB_SIZE - cmd_len); | |
210 | else if (cmd_len > ISCSI_CDB_SIZE) { | |
211 | rc = iscsi_prep_ecdb_ahs(ctask); | |
212 | if (rc) | |
213 | return rc; | |
214 | cmd_len = ISCSI_CDB_SIZE; | |
215 | } | |
216 | memcpy(hdr->cdb, sc->cmnd, cmd_len); | |
7996a778 | 217 | |
218432c6 | 218 | ctask->imm_count = 0; |
7996a778 MC |
219 | if (sc->sc_data_direction == DMA_TO_DEVICE) { |
220 | hdr->flags |= ISCSI_FLAG_CMD_WRITE; | |
221 | /* | |
222 | * Write counters: | |
223 | * | |
224 | * imm_count bytes to be sent right after | |
225 | * SCSI PDU Header | |
226 | * | |
227 | * unsol_count bytes(as Data-Out) to be sent | |
228 | * without R2T ack right after | |
229 | * immediate data | |
230 | * | |
231 | * r2t_data_count bytes to be sent via R2T ack's | |
232 | * | |
233 | * pad_count bytes to be sent as zero-padding | |
234 | */ | |
7996a778 | 235 | ctask->unsol_count = 0; |
ffd0436e | 236 | ctask->unsol_offset = 0; |
7996a778 MC |
237 | ctask->unsol_datasn = 0; |
238 | ||
239 | if (session->imm_data_en) { | |
1c138991 | 240 | if (scsi_bufflen(sc) >= session->first_burst) |
7996a778 MC |
241 | ctask->imm_count = min(session->first_burst, |
242 | conn->max_xmit_dlength); | |
243 | else | |
1c138991 | 244 | ctask->imm_count = min(scsi_bufflen(sc), |
7996a778 | 245 | conn->max_xmit_dlength); |
a8ac6311 | 246 | hton24(hdr->dlength, ctask->imm_count); |
7996a778 | 247 | } else |
a8ac6311 | 248 | zero_data(hdr->dlength); |
7996a778 | 249 | |
ffd0436e | 250 | if (!session->initial_r2t_en) { |
857ae0bd | 251 | ctask->unsol_count = min((session->first_burst), |
1c138991 | 252 | (scsi_bufflen(sc))) - ctask->imm_count; |
ffd0436e MC |
253 | ctask->unsol_offset = ctask->imm_count; |
254 | } | |
255 | ||
7996a778 MC |
256 | if (!ctask->unsol_count) |
257 | /* No unsolicit Data-Out's */ | |
a8ac6311 | 258 | hdr->flags |= ISCSI_FLAG_CMD_FINAL; |
7996a778 | 259 | } else { |
7996a778 MC |
260 | hdr->flags |= ISCSI_FLAG_CMD_FINAL; |
261 | zero_data(hdr->dlength); | |
262 | ||
263 | if (sc->sc_data_direction == DMA_FROM_DEVICE) | |
264 | hdr->flags |= ISCSI_FLAG_CMD_READ; | |
265 | } | |
266 | ||
004d6530 BH |
267 | /* calculate size of additional header segments (AHSs) */ |
268 | hdrlength = ctask->hdr_len - sizeof(*hdr); | |
269 | ||
270 | WARN_ON(hdrlength & (ISCSI_PAD_LEN-1)); | |
271 | hdrlength /= ISCSI_PAD_LEN; | |
272 | ||
273 | WARN_ON(hdrlength >= 256); | |
274 | hdr->hlength = hdrlength & 0xFF; | |
275 | ||
a8ac6311 OK |
276 | if (conn->session->tt->init_cmd_task(conn->ctask)) |
277 | return EIO; | |
77a23c21 | 278 | |
a8ac6311 OK |
279 | conn->scsicmd_pdus_cnt++; |
280 | debug_scsi("iscsi prep [%s cid %d sc %p cdb 0x%x itt 0x%x len %d " | |
77a23c21 | 281 | "cmdsn %d win %d]\n", |
a8ac6311 | 282 | sc->sc_data_direction == DMA_TO_DEVICE ? "write" : "read", |
1c138991 | 283 | conn->id, sc, sc->cmnd[0], ctask->itt, scsi_bufflen(sc), |
a8ac6311 | 284 | session->cmdsn, session->max_cmdsn - session->exp_cmdsn + 1); |
004d6530 | 285 | return 0; |
7996a778 | 286 | } |
7996a778 MC |
287 | |
288 | /** | |
289 | * iscsi_complete_command - return command back to scsi-ml | |
7996a778 MC |
290 | * @ctask: iscsi cmd task |
291 | * | |
292 | * Must be called with session lock. | |
293 | * This function returns the scsi command to scsi-ml and returns | |
294 | * the cmd task to the pool of available cmd tasks. | |
295 | */ | |
60ecebf5 | 296 | static void iscsi_complete_command(struct iscsi_cmd_task *ctask) |
7996a778 | 297 | { |
c1635cb7 MC |
298 | struct iscsi_conn *conn = ctask->conn; |
299 | struct iscsi_session *session = conn->session; | |
7996a778 MC |
300 | struct scsi_cmnd *sc = ctask->sc; |
301 | ||
b6c395ed | 302 | ctask->state = ISCSI_TASK_COMPLETED; |
7996a778 | 303 | ctask->sc = NULL; |
f47f2cf5 MC |
304 | /* SCSI eh reuses commands to verify us */ |
305 | sc->SCp.ptr = NULL; | |
c1635cb7 MC |
306 | if (conn->ctask == ctask) |
307 | conn->ctask = NULL; | |
7996a778 MC |
308 | list_del_init(&ctask->running); |
309 | __kfifo_put(session->cmdpool.queue, (void*)&ctask, sizeof(void*)); | |
310 | sc->scsi_done(sc); | |
311 | } | |
312 | ||
60ecebf5 MC |
313 | static void __iscsi_get_ctask(struct iscsi_cmd_task *ctask) |
314 | { | |
315 | atomic_inc(&ctask->refcount); | |
316 | } | |
317 | ||
60ecebf5 MC |
318 | static void __iscsi_put_ctask(struct iscsi_cmd_task *ctask) |
319 | { | |
e648f63c | 320 | if (atomic_dec_and_test(&ctask->refcount)) |
60ecebf5 | 321 | iscsi_complete_command(ctask); |
60ecebf5 MC |
322 | } |
323 | ||
b3a7ea8d MC |
324 | /* |
325 | * session lock must be held | |
326 | */ | |
327 | static void fail_command(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask, | |
328 | int err) | |
329 | { | |
330 | struct scsi_cmnd *sc; | |
331 | ||
332 | sc = ctask->sc; | |
333 | if (!sc) | |
334 | return; | |
335 | ||
336 | if (ctask->state == ISCSI_TASK_PENDING) | |
337 | /* | |
338 | * cmd never made it to the xmit thread, so we should not count | |
339 | * the cmd in the sequencing | |
340 | */ | |
341 | conn->session->queued_cmdsn--; | |
342 | else | |
343 | conn->session->tt->cleanup_cmd_task(conn, ctask); | |
344 | ||
345 | sc->result = err; | |
346 | scsi_set_resid(sc, scsi_bufflen(sc)); | |
347 | if (conn->ctask == ctask) | |
348 | conn->ctask = NULL; | |
349 | /* release ref from queuecommand */ | |
350 | __iscsi_put_ctask(ctask); | |
351 | } | |
352 | ||
353 | /** | |
354 | * iscsi_free_mgmt_task - return mgmt task back to pool | |
355 | * @conn: iscsi connection | |
356 | * @mtask: mtask | |
357 | * | |
358 | * Must be called with session lock. | |
359 | */ | |
360 | void iscsi_free_mgmt_task(struct iscsi_conn *conn, | |
361 | struct iscsi_mgmt_task *mtask) | |
362 | { | |
363 | list_del_init(&mtask->running); | |
364 | if (conn->login_mtask == mtask) | |
365 | return; | |
f6d5180c MC |
366 | |
367 | if (conn->ping_mtask == mtask) | |
368 | conn->ping_mtask = NULL; | |
b3a7ea8d MC |
369 | __kfifo_put(conn->session->mgmtpool.queue, |
370 | (void*)&mtask, sizeof(void*)); | |
371 | } | |
372 | EXPORT_SYMBOL_GPL(iscsi_free_mgmt_task); | |
373 | ||
f6d5180c MC |
374 | static struct iscsi_mgmt_task * |
375 | __iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr, | |
376 | char *data, uint32_t data_size) | |
377 | { | |
378 | struct iscsi_session *session = conn->session; | |
379 | struct iscsi_mgmt_task *mtask; | |
380 | ||
381 | if (session->state == ISCSI_STATE_TERMINATE) | |
382 | return NULL; | |
383 | ||
384 | if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) || | |
385 | hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE)) | |
386 | /* | |
387 | * Login and Text are sent serially, in | |
388 | * request-followed-by-response sequence. | |
389 | * Same mtask can be used. Same ITT must be used. | |
390 | * Note that login_mtask is preallocated at conn_create(). | |
391 | */ | |
392 | mtask = conn->login_mtask; | |
393 | else { | |
394 | BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE); | |
395 | BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED); | |
396 | ||
397 | if (!__kfifo_get(session->mgmtpool.queue, | |
398 | (void*)&mtask, sizeof(void*))) | |
399 | return NULL; | |
400 | } | |
401 | ||
402 | if (data_size) { | |
403 | memcpy(mtask->data, data, data_size); | |
404 | mtask->data_count = data_size; | |
405 | } else | |
406 | mtask->data_count = 0; | |
407 | ||
408 | memcpy(mtask->hdr, hdr, sizeof(struct iscsi_hdr)); | |
409 | INIT_LIST_HEAD(&mtask->running); | |
410 | list_add_tail(&mtask->running, &conn->mgmtqueue); | |
411 | return mtask; | |
412 | } | |
413 | ||
414 | int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr, | |
415 | char *data, uint32_t data_size) | |
416 | { | |
417 | struct iscsi_conn *conn = cls_conn->dd_data; | |
418 | struct iscsi_session *session = conn->session; | |
419 | int err = 0; | |
420 | ||
421 | spin_lock_bh(&session->lock); | |
422 | if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size)) | |
423 | err = -EPERM; | |
424 | spin_unlock_bh(&session->lock); | |
425 | scsi_queue_work(session->host, &conn->xmitwork); | |
426 | return err; | |
427 | } | |
428 | EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu); | |
429 | ||
7996a778 MC |
430 | /** |
431 | * iscsi_cmd_rsp - SCSI Command Response processing | |
432 | * @conn: iscsi connection | |
433 | * @hdr: iscsi header | |
434 | * @ctask: scsi command task | |
435 | * @data: cmd data buffer | |
436 | * @datalen: len of buffer | |
437 | * | |
438 | * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and | |
439 | * then completes the command and task. | |
440 | **/ | |
77a23c21 MC |
441 | static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr, |
442 | struct iscsi_cmd_task *ctask, char *data, | |
443 | int datalen) | |
7996a778 | 444 | { |
7996a778 MC |
445 | struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)hdr; |
446 | struct iscsi_session *session = conn->session; | |
447 | struct scsi_cmnd *sc = ctask->sc; | |
448 | ||
77a23c21 | 449 | iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr); |
7996a778 MC |
450 | conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1; |
451 | ||
452 | sc->result = (DID_OK << 16) | rhdr->cmd_status; | |
453 | ||
454 | if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) { | |
455 | sc->result = DID_ERROR << 16; | |
456 | goto out; | |
457 | } | |
458 | ||
459 | if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) { | |
9b80cb4b | 460 | uint16_t senselen; |
7996a778 MC |
461 | |
462 | if (datalen < 2) { | |
463 | invalid_datalen: | |
322d739d MC |
464 | iscsi_conn_printk(KERN_ERR, conn, |
465 | "Got CHECK_CONDITION but invalid data " | |
466 | "buffer size of %d\n", datalen); | |
7996a778 MC |
467 | sc->result = DID_BAD_TARGET << 16; |
468 | goto out; | |
469 | } | |
470 | ||
8eb00539 | 471 | senselen = be16_to_cpu(get_unaligned((__be16 *) data)); |
7996a778 MC |
472 | if (datalen < senselen) |
473 | goto invalid_datalen; | |
474 | ||
475 | memcpy(sc->sense_buffer, data + 2, | |
9b80cb4b | 476 | min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE)); |
7996a778 | 477 | debug_scsi("copied %d bytes of sense\n", |
8eb00539 | 478 | min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE)); |
7996a778 MC |
479 | } |
480 | ||
7207fea4 BH |
481 | if (rhdr->flags & (ISCSI_FLAG_CMD_UNDERFLOW | |
482 | ISCSI_FLAG_CMD_OVERFLOW)) { | |
7996a778 MC |
483 | int res_count = be32_to_cpu(rhdr->residual_count); |
484 | ||
7207fea4 BH |
485 | if (res_count > 0 && |
486 | (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW || | |
487 | res_count <= scsi_bufflen(sc))) | |
1c138991 | 488 | scsi_set_resid(sc, res_count); |
7996a778 MC |
489 | else |
490 | sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status; | |
7207fea4 BH |
491 | } else if (rhdr->flags & (ISCSI_FLAG_CMD_BIDI_UNDERFLOW | |
492 | ISCSI_FLAG_CMD_BIDI_OVERFLOW)) | |
7996a778 | 493 | sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status; |
7996a778 MC |
494 | |
495 | out: | |
496 | debug_scsi("done [sc %lx res %d itt 0x%x]\n", | |
497 | (long)sc, sc->result, ctask->itt); | |
498 | conn->scsirsp_pdus_cnt++; | |
499 | ||
60ecebf5 | 500 | __iscsi_put_ctask(ctask); |
7996a778 MC |
501 | } |
502 | ||
7ea8b828 MC |
503 | static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr) |
504 | { | |
505 | struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr; | |
506 | ||
507 | conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1; | |
508 | conn->tmfrsp_pdus_cnt++; | |
509 | ||
843c0a8a | 510 | if (conn->tmf_state != TMF_QUEUED) |
7ea8b828 MC |
511 | return; |
512 | ||
513 | if (tmf->response == ISCSI_TMF_RSP_COMPLETE) | |
843c0a8a | 514 | conn->tmf_state = TMF_SUCCESS; |
7ea8b828 | 515 | else if (tmf->response == ISCSI_TMF_RSP_NO_TASK) |
843c0a8a | 516 | conn->tmf_state = TMF_NOT_FOUND; |
7ea8b828 | 517 | else |
843c0a8a | 518 | conn->tmf_state = TMF_FAILED; |
7ea8b828 MC |
519 | wake_up(&conn->ehwait); |
520 | } | |
521 | ||
f6d5180c MC |
522 | static void iscsi_send_nopout(struct iscsi_conn *conn, struct iscsi_nopin *rhdr) |
523 | { | |
524 | struct iscsi_nopout hdr; | |
525 | struct iscsi_mgmt_task *mtask; | |
526 | ||
527 | if (!rhdr && conn->ping_mtask) | |
528 | return; | |
529 | ||
530 | memset(&hdr, 0, sizeof(struct iscsi_nopout)); | |
531 | hdr.opcode = ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE; | |
532 | hdr.flags = ISCSI_FLAG_CMD_FINAL; | |
533 | ||
534 | if (rhdr) { | |
535 | memcpy(hdr.lun, rhdr->lun, 8); | |
536 | hdr.ttt = rhdr->ttt; | |
537 | hdr.itt = RESERVED_ITT; | |
538 | } else | |
539 | hdr.ttt = RESERVED_ITT; | |
540 | ||
541 | mtask = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)&hdr, NULL, 0); | |
542 | if (!mtask) { | |
322d739d | 543 | iscsi_conn_printk(KERN_ERR, conn, "Could not send nopout\n"); |
f6d5180c MC |
544 | return; |
545 | } | |
546 | ||
547 | /* only track our nops */ | |
548 | if (!rhdr) { | |
549 | conn->ping_mtask = mtask; | |
550 | conn->last_ping = jiffies; | |
551 | } | |
552 | scsi_queue_work(conn->session->host, &conn->xmitwork); | |
553 | } | |
554 | ||
62f38300 MC |
555 | static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr, |
556 | char *data, int datalen) | |
557 | { | |
558 | struct iscsi_reject *reject = (struct iscsi_reject *)hdr; | |
559 | struct iscsi_hdr rejected_pdu; | |
560 | uint32_t itt; | |
561 | ||
562 | conn->exp_statsn = be32_to_cpu(reject->statsn) + 1; | |
563 | ||
564 | if (reject->reason == ISCSI_REASON_DATA_DIGEST_ERROR) { | |
565 | if (ntoh24(reject->dlength) > datalen) | |
566 | return ISCSI_ERR_PROTO; | |
567 | ||
568 | if (ntoh24(reject->dlength) >= sizeof(struct iscsi_hdr)) { | |
569 | memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr)); | |
b4377356 | 570 | itt = get_itt(rejected_pdu.itt); |
322d739d MC |
571 | iscsi_conn_printk(KERN_ERR, conn, |
572 | "itt 0x%x had pdu (op 0x%x) rejected " | |
573 | "due to DataDigest error.\n", itt, | |
574 | rejected_pdu.opcode); | |
62f38300 MC |
575 | } |
576 | } | |
577 | return 0; | |
578 | } | |
579 | ||
7996a778 MC |
580 | /** |
581 | * __iscsi_complete_pdu - complete pdu | |
582 | * @conn: iscsi conn | |
583 | * @hdr: iscsi header | |
584 | * @data: data buffer | |
585 | * @datalen: len of data buffer | |
586 | * | |
587 | * Completes pdu processing by freeing any resources allocated at | |
588 | * queuecommand or send generic. session lock must be held and verify | |
589 | * itt must have been called. | |
590 | */ | |
f8d9d654 AB |
591 | static int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr, |
592 | char *data, int datalen) | |
7996a778 MC |
593 | { |
594 | struct iscsi_session *session = conn->session; | |
595 | int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0; | |
596 | struct iscsi_cmd_task *ctask; | |
597 | struct iscsi_mgmt_task *mtask; | |
598 | uint32_t itt; | |
599 | ||
f6d5180c | 600 | conn->last_recv = jiffies; |
b4377356 AV |
601 | if (hdr->itt != RESERVED_ITT) |
602 | itt = get_itt(hdr->itt); | |
7996a778 | 603 | else |
b4377356 | 604 | itt = ~0U; |
7996a778 MC |
605 | |
606 | if (itt < session->cmds_max) { | |
607 | ctask = session->cmds[itt]; | |
608 | ||
609 | debug_scsi("cmdrsp [op 0x%x cid %d itt 0x%x len %d]\n", | |
610 | opcode, conn->id, ctask->itt, datalen); | |
611 | ||
612 | switch(opcode) { | |
613 | case ISCSI_OP_SCSI_CMD_RSP: | |
614 | BUG_ON((void*)ctask != ctask->sc->SCp.ptr); | |
77a23c21 MC |
615 | iscsi_scsi_cmd_rsp(conn, hdr, ctask, data, |
616 | datalen); | |
7996a778 MC |
617 | break; |
618 | case ISCSI_OP_SCSI_DATA_IN: | |
619 | BUG_ON((void*)ctask != ctask->sc->SCp.ptr); | |
620 | if (hdr->flags & ISCSI_FLAG_DATA_STATUS) { | |
621 | conn->scsirsp_pdus_cnt++; | |
60ecebf5 | 622 | __iscsi_put_ctask(ctask); |
7996a778 MC |
623 | } |
624 | break; | |
625 | case ISCSI_OP_R2T: | |
626 | /* LLD handles this for now */ | |
627 | break; | |
628 | default: | |
629 | rc = ISCSI_ERR_BAD_OPCODE; | |
630 | break; | |
631 | } | |
632 | } else if (itt >= ISCSI_MGMT_ITT_OFFSET && | |
633 | itt < ISCSI_MGMT_ITT_OFFSET + session->mgmtpool_max) { | |
634 | mtask = session->mgmt_cmds[itt - ISCSI_MGMT_ITT_OFFSET]; | |
635 | ||
636 | debug_scsi("immrsp [op 0x%x cid %d itt 0x%x len %d]\n", | |
637 | opcode, conn->id, mtask->itt, datalen); | |
638 | ||
77a23c21 | 639 | iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr); |
7996a778 | 640 | switch(opcode) { |
8d2860b3 | 641 | case ISCSI_OP_LOGOUT_RSP: |
c8dc1e52 MC |
642 | if (datalen) { |
643 | rc = ISCSI_ERR_PROTO; | |
644 | break; | |
645 | } | |
8d2860b3 MC |
646 | conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1; |
647 | /* fall through */ | |
7996a778 MC |
648 | case ISCSI_OP_LOGIN_RSP: |
649 | case ISCSI_OP_TEXT_RSP: | |
8d2860b3 MC |
650 | /* |
651 | * login related PDU's exp_statsn is handled in | |
652 | * userspace | |
653 | */ | |
40527afe MC |
654 | if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen)) |
655 | rc = ISCSI_ERR_CONN_FAILED; | |
b3a7ea8d | 656 | iscsi_free_mgmt_task(conn, mtask); |
7996a778 MC |
657 | break; |
658 | case ISCSI_OP_SCSI_TMFUNC_RSP: | |
7996a778 MC |
659 | if (datalen) { |
660 | rc = ISCSI_ERR_PROTO; | |
661 | break; | |
662 | } | |
8d2860b3 | 663 | |
7ea8b828 | 664 | iscsi_tmf_rsp(conn, hdr); |
b3a7ea8d | 665 | iscsi_free_mgmt_task(conn, mtask); |
7996a778 MC |
666 | break; |
667 | case ISCSI_OP_NOOP_IN: | |
f6d5180c MC |
668 | if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) || |
669 | datalen) { | |
7996a778 MC |
670 | rc = ISCSI_ERR_PROTO; |
671 | break; | |
672 | } | |
7996a778 MC |
673 | conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1; |
674 | ||
f6d5180c MC |
675 | if (conn->ping_mtask != mtask) { |
676 | /* | |
677 | * If this is not in response to one of our | |
678 | * nops then it must be from userspace. | |
679 | */ | |
680 | if (iscsi_recv_pdu(conn->cls_conn, hdr, data, | |
681 | datalen)) | |
682 | rc = ISCSI_ERR_CONN_FAILED; | |
683 | } | |
b3a7ea8d | 684 | iscsi_free_mgmt_task(conn, mtask); |
7996a778 MC |
685 | break; |
686 | default: | |
687 | rc = ISCSI_ERR_BAD_OPCODE; | |
688 | break; | |
689 | } | |
b4377356 | 690 | } else if (itt == ~0U) { |
77a23c21 | 691 | iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr); |
62f38300 | 692 | |
7996a778 MC |
693 | switch(opcode) { |
694 | case ISCSI_OP_NOOP_IN: | |
40527afe | 695 | if (datalen) { |
7996a778 | 696 | rc = ISCSI_ERR_PROTO; |
40527afe MC |
697 | break; |
698 | } | |
699 | ||
b4377356 | 700 | if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG)) |
40527afe MC |
701 | break; |
702 | ||
f6d5180c | 703 | iscsi_send_nopout(conn, (struct iscsi_nopin*)hdr); |
7996a778 MC |
704 | break; |
705 | case ISCSI_OP_REJECT: | |
62f38300 MC |
706 | rc = iscsi_handle_reject(conn, hdr, data, datalen); |
707 | break; | |
7996a778 | 708 | case ISCSI_OP_ASYNC_EVENT: |
8d2860b3 | 709 | conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1; |
5831c737 MC |
710 | if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen)) |
711 | rc = ISCSI_ERR_CONN_FAILED; | |
7996a778 MC |
712 | break; |
713 | default: | |
714 | rc = ISCSI_ERR_BAD_OPCODE; | |
715 | break; | |
716 | } | |
717 | } else | |
718 | rc = ISCSI_ERR_BAD_ITT; | |
719 | ||
720 | return rc; | |
721 | } | |
7996a778 MC |
722 | |
723 | int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr, | |
724 | char *data, int datalen) | |
725 | { | |
726 | int rc; | |
727 | ||
728 | spin_lock(&conn->session->lock); | |
729 | rc = __iscsi_complete_pdu(conn, hdr, data, datalen); | |
730 | spin_unlock(&conn->session->lock); | |
731 | return rc; | |
732 | } | |
733 | EXPORT_SYMBOL_GPL(iscsi_complete_pdu); | |
734 | ||
735 | /* verify itt (itt encoding: age+cid+itt) */ | |
736 | int iscsi_verify_itt(struct iscsi_conn *conn, struct iscsi_hdr *hdr, | |
737 | uint32_t *ret_itt) | |
738 | { | |
739 | struct iscsi_session *session = conn->session; | |
740 | struct iscsi_cmd_task *ctask; | |
741 | uint32_t itt; | |
742 | ||
b4377356 AV |
743 | if (hdr->itt != RESERVED_ITT) { |
744 | if (((__force u32)hdr->itt & ISCSI_AGE_MASK) != | |
7996a778 | 745 | (session->age << ISCSI_AGE_SHIFT)) { |
322d739d MC |
746 | iscsi_conn_printk(KERN_ERR, conn, |
747 | "received itt %x expected session " | |
748 | "age (%x)\n", (__force u32)hdr->itt, | |
749 | session->age & ISCSI_AGE_MASK); | |
7996a778 MC |
750 | return ISCSI_ERR_BAD_ITT; |
751 | } | |
752 | ||
b4377356 | 753 | itt = get_itt(hdr->itt); |
7996a778 | 754 | } else |
b4377356 | 755 | itt = ~0U; |
7996a778 MC |
756 | |
757 | if (itt < session->cmds_max) { | |
758 | ctask = session->cmds[itt]; | |
759 | ||
760 | if (!ctask->sc) { | |
322d739d MC |
761 | iscsi_conn_printk(KERN_INFO, conn, "dropping ctask " |
762 | "with itt 0x%x\n", ctask->itt); | |
7996a778 MC |
763 | /* force drop */ |
764 | return ISCSI_ERR_NO_SCSI_CMD; | |
765 | } | |
766 | ||
767 | if (ctask->sc->SCp.phase != session->age) { | |
322d739d MC |
768 | iscsi_conn_printk(KERN_ERR, conn, |
769 | "iscsi: ctask's session age %d, " | |
770 | "expected %d\n", ctask->sc->SCp.phase, | |
771 | session->age); | |
7996a778 MC |
772 | return ISCSI_ERR_SESSION_FAILED; |
773 | } | |
774 | } | |
775 | ||
776 | *ret_itt = itt; | |
777 | return 0; | |
778 | } | |
779 | EXPORT_SYMBOL_GPL(iscsi_verify_itt); | |
780 | ||
781 | void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err) | |
782 | { | |
783 | struct iscsi_session *session = conn->session; | |
784 | unsigned long flags; | |
785 | ||
786 | spin_lock_irqsave(&session->lock, flags); | |
656cffc9 MC |
787 | if (session->state == ISCSI_STATE_FAILED) { |
788 | spin_unlock_irqrestore(&session->lock, flags); | |
789 | return; | |
790 | } | |
791 | ||
67a61114 | 792 | if (conn->stop_stage == 0) |
7996a778 MC |
793 | session->state = ISCSI_STATE_FAILED; |
794 | spin_unlock_irqrestore(&session->lock, flags); | |
795 | set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx); | |
796 | set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx); | |
797 | iscsi_conn_error(conn->cls_conn, err); | |
798 | } | |
799 | EXPORT_SYMBOL_GPL(iscsi_conn_failure); | |
800 | ||
77a23c21 MC |
801 | static void iscsi_prep_mtask(struct iscsi_conn *conn, |
802 | struct iscsi_mgmt_task *mtask) | |
803 | { | |
804 | struct iscsi_session *session = conn->session; | |
805 | struct iscsi_hdr *hdr = mtask->hdr; | |
806 | struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr; | |
807 | ||
808 | if (hdr->opcode != (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) && | |
809 | hdr->opcode != (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE)) | |
810 | nop->exp_statsn = cpu_to_be32(conn->exp_statsn); | |
811 | /* | |
812 | * pre-format CmdSN for outgoing PDU. | |
813 | */ | |
814 | nop->cmdsn = cpu_to_be32(session->cmdsn); | |
815 | if (hdr->itt != RESERVED_ITT) { | |
8b1d0343 | 816 | hdr->itt = build_itt(mtask->itt, session->age); |
e0726407 MC |
817 | /* |
818 | * TODO: We always use immediate, so we never hit this. | |
819 | * If we start to send tmfs or nops as non-immediate then | |
820 | * we should start checking the cmdsn numbers for mgmt tasks. | |
821 | */ | |
77a23c21 | 822 | if (conn->c_stage == ISCSI_CONN_STARTED && |
e0726407 MC |
823 | !(hdr->opcode & ISCSI_OP_IMMEDIATE)) { |
824 | session->queued_cmdsn++; | |
77a23c21 | 825 | session->cmdsn++; |
e0726407 | 826 | } |
77a23c21 MC |
827 | } |
828 | ||
829 | if (session->tt->init_mgmt_task) | |
830 | session->tt->init_mgmt_task(conn, mtask); | |
831 | ||
832 | debug_scsi("mgmtpdu [op 0x%x hdr->itt 0x%x datalen %d]\n", | |
843c0a8a MC |
833 | hdr->opcode & ISCSI_OPCODE_MASK, hdr->itt, |
834 | mtask->data_count); | |
77a23c21 MC |
835 | } |
836 | ||
05db888a | 837 | static int iscsi_xmit_mtask(struct iscsi_conn *conn) |
b5072ea0 MC |
838 | { |
839 | struct iscsi_hdr *hdr = conn->mtask->hdr; | |
b3a7ea8d | 840 | int rc; |
b5072ea0 | 841 | |
b3a7ea8d MC |
842 | if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT) |
843 | conn->session->state = ISCSI_STATE_LOGGING_OUT; | |
77a23c21 | 844 | spin_unlock_bh(&conn->session->lock); |
b3a7ea8d | 845 | |
b5072ea0 | 846 | rc = conn->session->tt->xmit_mgmt_task(conn, conn->mtask); |
77a23c21 | 847 | spin_lock_bh(&conn->session->lock); |
b5072ea0 MC |
848 | if (rc) |
849 | return rc; | |
850 | ||
05db888a MC |
851 | /* done with this in-progress mtask */ |
852 | conn->mtask = NULL; | |
b5072ea0 MC |
853 | return 0; |
854 | } | |
855 | ||
77a23c21 MC |
856 | static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn) |
857 | { | |
858 | struct iscsi_session *session = conn->session; | |
859 | ||
860 | /* | |
861 | * Check for iSCSI window and take care of CmdSN wrap-around | |
862 | */ | |
e0726407 MC |
863 | if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) { |
864 | debug_scsi("iSCSI CmdSN closed. ExpCmdSn %u MaxCmdSN %u " | |
865 | "CmdSN %u/%u\n", session->exp_cmdsn, | |
866 | session->max_cmdsn, session->cmdsn, | |
867 | session->queued_cmdsn); | |
77a23c21 MC |
868 | return -ENOSPC; |
869 | } | |
870 | return 0; | |
871 | } | |
872 | ||
873 | static int iscsi_xmit_ctask(struct iscsi_conn *conn) | |
874 | { | |
875 | struct iscsi_cmd_task *ctask = conn->ctask; | |
843c0a8a | 876 | int rc; |
77a23c21 MC |
877 | |
878 | __iscsi_get_ctask(ctask); | |
879 | spin_unlock_bh(&conn->session->lock); | |
880 | rc = conn->session->tt->xmit_cmd_task(conn, ctask); | |
881 | spin_lock_bh(&conn->session->lock); | |
882 | __iscsi_put_ctask(ctask); | |
77a23c21 MC |
883 | if (!rc) |
884 | /* done with this ctask */ | |
885 | conn->ctask = NULL; | |
886 | return rc; | |
887 | } | |
888 | ||
843c0a8a MC |
889 | /** |
890 | * iscsi_requeue_ctask - requeue ctask to run from session workqueue | |
891 | * @ctask: ctask to requeue | |
892 | * | |
893 | * LLDs that need to run a ctask from the session workqueue should call | |
894 | * this. The session lock must be held. | |
895 | */ | |
896 | void iscsi_requeue_ctask(struct iscsi_cmd_task *ctask) | |
897 | { | |
898 | struct iscsi_conn *conn = ctask->conn; | |
899 | ||
900 | list_move_tail(&ctask->running, &conn->requeue); | |
901 | scsi_queue_work(conn->session->host, &conn->xmitwork); | |
902 | } | |
903 | EXPORT_SYMBOL_GPL(iscsi_requeue_ctask); | |
904 | ||
7996a778 MC |
905 | /** |
906 | * iscsi_data_xmit - xmit any command into the scheduled connection | |
907 | * @conn: iscsi connection | |
908 | * | |
909 | * Notes: | |
910 | * The function can return -EAGAIN in which case the caller must | |
911 | * re-schedule it again later or recover. '0' return code means | |
912 | * successful xmit. | |
913 | **/ | |
914 | static int iscsi_data_xmit(struct iscsi_conn *conn) | |
915 | { | |
3219e529 | 916 | int rc = 0; |
7996a778 | 917 | |
77a23c21 | 918 | spin_lock_bh(&conn->session->lock); |
7996a778 MC |
919 | if (unlikely(conn->suspend_tx)) { |
920 | debug_scsi("conn %d Tx suspended!\n", conn->id); | |
77a23c21 | 921 | spin_unlock_bh(&conn->session->lock); |
3219e529 | 922 | return -ENODATA; |
7996a778 | 923 | } |
7996a778 MC |
924 | |
925 | if (conn->ctask) { | |
77a23c21 | 926 | rc = iscsi_xmit_ctask(conn); |
3219e529 | 927 | if (rc) |
7996a778 | 928 | goto again; |
7996a778 | 929 | } |
77a23c21 | 930 | |
7996a778 | 931 | if (conn->mtask) { |
05db888a | 932 | rc = iscsi_xmit_mtask(conn); |
3219e529 | 933 | if (rc) |
7996a778 | 934 | goto again; |
7996a778 MC |
935 | } |
936 | ||
77a23c21 MC |
937 | /* |
938 | * process mgmt pdus like nops before commands since we should | |
939 | * only have one nop-out as a ping from us and targets should not | |
940 | * overflow us with nop-ins | |
941 | */ | |
942 | check_mgmt: | |
843c0a8a MC |
943 | while (!list_empty(&conn->mgmtqueue)) { |
944 | conn->mtask = list_entry(conn->mgmtqueue.next, | |
945 | struct iscsi_mgmt_task, running); | |
b3a7ea8d MC |
946 | if (conn->session->state == ISCSI_STATE_LOGGING_OUT) { |
947 | iscsi_free_mgmt_task(conn, conn->mtask); | |
948 | conn->mtask = NULL; | |
949 | continue; | |
950 | } | |
951 | ||
77a23c21 | 952 | iscsi_prep_mtask(conn, conn->mtask); |
843c0a8a | 953 | list_move_tail(conn->mgmtqueue.next, &conn->mgmt_run_list); |
77a23c21 MC |
954 | rc = iscsi_xmit_mtask(conn); |
955 | if (rc) | |
956 | goto again; | |
7996a778 MC |
957 | } |
958 | ||
843c0a8a | 959 | /* process pending command queue */ |
b6c395ed | 960 | while (!list_empty(&conn->xmitqueue)) { |
843c0a8a MC |
961 | if (conn->tmf_state == TMF_QUEUED) |
962 | break; | |
963 | ||
b6c395ed MC |
964 | conn->ctask = list_entry(conn->xmitqueue.next, |
965 | struct iscsi_cmd_task, running); | |
b3a7ea8d | 966 | if (conn->session->state == ISCSI_STATE_LOGGING_OUT) { |
9000bcd6 | 967 | fail_command(conn, conn->ctask, DID_IMM_RETRY << 16); |
b3a7ea8d MC |
968 | continue; |
969 | } | |
004d6530 BH |
970 | if (iscsi_prep_scsi_cmd_pdu(conn->ctask)) { |
971 | fail_command(conn, conn->ctask, DID_ABORT << 16); | |
972 | continue; | |
973 | } | |
a8ac6311 | 974 | |
843c0a8a | 975 | conn->ctask->state = ISCSI_TASK_RUNNING; |
b6c395ed | 976 | list_move_tail(conn->xmitqueue.next, &conn->run_list); |
77a23c21 MC |
977 | rc = iscsi_xmit_ctask(conn); |
978 | if (rc) | |
60ecebf5 | 979 | goto again; |
77a23c21 MC |
980 | /* |
981 | * we could continuously get new ctask requests so | |
982 | * we need to check the mgmt queue for nops that need to | |
983 | * be sent to aviod starvation | |
984 | */ | |
843c0a8a MC |
985 | if (!list_empty(&conn->mgmtqueue)) |
986 | goto check_mgmt; | |
987 | } | |
988 | ||
989 | while (!list_empty(&conn->requeue)) { | |
990 | if (conn->session->fast_abort && conn->tmf_state != TMF_INITIAL) | |
991 | break; | |
992 | ||
b3a7ea8d MC |
993 | /* |
994 | * we always do fastlogout - conn stop code will clean up. | |
995 | */ | |
996 | if (conn->session->state == ISCSI_STATE_LOGGING_OUT) | |
997 | break; | |
998 | ||
843c0a8a MC |
999 | conn->ctask = list_entry(conn->requeue.next, |
1000 | struct iscsi_cmd_task, running); | |
1001 | conn->ctask->state = ISCSI_TASK_RUNNING; | |
1002 | list_move_tail(conn->requeue.next, &conn->run_list); | |
1003 | rc = iscsi_xmit_ctask(conn); | |
1004 | if (rc) | |
1005 | goto again; | |
1006 | if (!list_empty(&conn->mgmtqueue)) | |
77a23c21 | 1007 | goto check_mgmt; |
7996a778 | 1008 | } |
b6c395ed | 1009 | spin_unlock_bh(&conn->session->lock); |
3219e529 | 1010 | return -ENODATA; |
7996a778 MC |
1011 | |
1012 | again: | |
1013 | if (unlikely(conn->suspend_tx)) | |
77a23c21 MC |
1014 | rc = -ENODATA; |
1015 | spin_unlock_bh(&conn->session->lock); | |
3219e529 | 1016 | return rc; |
7996a778 MC |
1017 | } |
1018 | ||
c4028958 | 1019 | static void iscsi_xmitworker(struct work_struct *work) |
7996a778 | 1020 | { |
c4028958 DH |
1021 | struct iscsi_conn *conn = |
1022 | container_of(work, struct iscsi_conn, xmitwork); | |
3219e529 | 1023 | int rc; |
7996a778 MC |
1024 | /* |
1025 | * serialize Xmit worker on a per-connection basis. | |
1026 | */ | |
3219e529 MC |
1027 | do { |
1028 | rc = iscsi_data_xmit(conn); | |
1029 | } while (rc >= 0 || rc == -EAGAIN); | |
7996a778 MC |
1030 | } |
1031 | ||
1032 | enum { | |
1033 | FAILURE_BAD_HOST = 1, | |
1034 | FAILURE_SESSION_FAILED, | |
1035 | FAILURE_SESSION_FREED, | |
1036 | FAILURE_WINDOW_CLOSED, | |
60ecebf5 | 1037 | FAILURE_OOM, |
7996a778 | 1038 | FAILURE_SESSION_TERMINATE, |
656cffc9 | 1039 | FAILURE_SESSION_IN_RECOVERY, |
7996a778 | 1040 | FAILURE_SESSION_RECOVERY_TIMEOUT, |
b3a7ea8d | 1041 | FAILURE_SESSION_LOGGING_OUT, |
6eabafbe | 1042 | FAILURE_SESSION_NOT_READY, |
7996a778 MC |
1043 | }; |
1044 | ||
1045 | int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *)) | |
1046 | { | |
1047 | struct Scsi_Host *host; | |
1048 | int reason = 0; | |
1049 | struct iscsi_session *session; | |
1050 | struct iscsi_conn *conn; | |
1051 | struct iscsi_cmd_task *ctask = NULL; | |
1052 | ||
1053 | sc->scsi_done = done; | |
1054 | sc->result = 0; | |
f47f2cf5 | 1055 | sc->SCp.ptr = NULL; |
7996a778 MC |
1056 | |
1057 | host = sc->device->host; | |
1040c99d | 1058 | spin_unlock(host->host_lock); |
7996a778 | 1059 | |
1040c99d | 1060 | session = iscsi_hostdata(host->hostdata); |
7996a778 MC |
1061 | spin_lock(&session->lock); |
1062 | ||
6eabafbe MC |
1063 | reason = iscsi_session_chkready(session_to_cls(session)); |
1064 | if (reason) { | |
1065 | sc->result = reason; | |
1066 | goto fault; | |
1067 | } | |
1068 | ||
656cffc9 MC |
1069 | /* |
1070 | * ISCSI_STATE_FAILED is a temp. state. The recovery | |
1071 | * code will decide what is best to do with command queued | |
1072 | * during this time | |
1073 | */ | |
1074 | if (session->state != ISCSI_STATE_LOGGED_IN && | |
1075 | session->state != ISCSI_STATE_FAILED) { | |
1076 | /* | |
1077 | * to handle the race between when we set the recovery state | |
1078 | * and block the session we requeue here (commands could | |
1079 | * be entering our queuecommand while a block is starting | |
1080 | * up because the block code is not locked) | |
1081 | */ | |
9000bcd6 MC |
1082 | switch (session->state) { |
1083 | case ISCSI_STATE_IN_RECOVERY: | |
656cffc9 | 1084 | reason = FAILURE_SESSION_IN_RECOVERY; |
6eabafbe MC |
1085 | sc->result = DID_IMM_RETRY << 16; |
1086 | break; | |
9000bcd6 MC |
1087 | case ISCSI_STATE_LOGGING_OUT: |
1088 | reason = FAILURE_SESSION_LOGGING_OUT; | |
6eabafbe MC |
1089 | sc->result = DID_IMM_RETRY << 16; |
1090 | break; | |
b3a7ea8d | 1091 | case ISCSI_STATE_RECOVERY_FAILED: |
656cffc9 | 1092 | reason = FAILURE_SESSION_RECOVERY_TIMEOUT; |
6eabafbe | 1093 | sc->result = DID_NO_CONNECT << 16; |
b3a7ea8d MC |
1094 | break; |
1095 | case ISCSI_STATE_TERMINATE: | |
656cffc9 | 1096 | reason = FAILURE_SESSION_TERMINATE; |
6eabafbe | 1097 | sc->result = DID_NO_CONNECT << 16; |
b3a7ea8d | 1098 | break; |
b3a7ea8d | 1099 | default: |
656cffc9 | 1100 | reason = FAILURE_SESSION_FREED; |
6eabafbe | 1101 | sc->result = DID_NO_CONNECT << 16; |
b3a7ea8d | 1102 | } |
7996a778 MC |
1103 | goto fault; |
1104 | } | |
1105 | ||
7996a778 | 1106 | conn = session->leadconn; |
98644047 MC |
1107 | if (!conn) { |
1108 | reason = FAILURE_SESSION_FREED; | |
6eabafbe | 1109 | sc->result = DID_NO_CONNECT << 16; |
98644047 MC |
1110 | goto fault; |
1111 | } | |
7996a778 | 1112 | |
77a23c21 MC |
1113 | if (iscsi_check_cmdsn_window_closed(conn)) { |
1114 | reason = FAILURE_WINDOW_CLOSED; | |
1115 | goto reject; | |
1116 | } | |
1117 | ||
60ecebf5 MC |
1118 | if (!__kfifo_get(session->cmdpool.queue, (void*)&ctask, |
1119 | sizeof(void*))) { | |
1120 | reason = FAILURE_OOM; | |
1121 | goto reject; | |
1122 | } | |
e0726407 MC |
1123 | session->queued_cmdsn++; |
1124 | ||
7996a778 MC |
1125 | sc->SCp.phase = session->age; |
1126 | sc->SCp.ptr = (char *)ctask; | |
1127 | ||
60ecebf5 | 1128 | atomic_set(&ctask->refcount, 1); |
b6c395ed | 1129 | ctask->state = ISCSI_TASK_PENDING; |
7996a778 MC |
1130 | ctask->conn = conn; |
1131 | ctask->sc = sc; | |
1132 | INIT_LIST_HEAD(&ctask->running); | |
7996a778 | 1133 | |
b6c395ed | 1134 | list_add_tail(&ctask->running, &conn->xmitqueue); |
7996a778 MC |
1135 | spin_unlock(&session->lock); |
1136 | ||
1137 | scsi_queue_work(host, &conn->xmitwork); | |
1040c99d | 1138 | spin_lock(host->host_lock); |
7996a778 MC |
1139 | return 0; |
1140 | ||
1141 | reject: | |
1142 | spin_unlock(&session->lock); | |
1143 | debug_scsi("cmd 0x%x rejected (%d)\n", sc->cmnd[0], reason); | |
1040c99d | 1144 | spin_lock(host->host_lock); |
7996a778 MC |
1145 | return SCSI_MLQUEUE_HOST_BUSY; |
1146 | ||
1147 | fault: | |
1148 | spin_unlock(&session->lock); | |
6eabafbe | 1149 | debug_scsi("iscsi: cmd 0x%x is not queued (%d)\n", sc->cmnd[0], reason); |
1c138991 | 1150 | scsi_set_resid(sc, scsi_bufflen(sc)); |
7996a778 | 1151 | sc->scsi_done(sc); |
1040c99d | 1152 | spin_lock(host->host_lock); |
7996a778 MC |
1153 | return 0; |
1154 | } | |
1155 | EXPORT_SYMBOL_GPL(iscsi_queuecommand); | |
1156 | ||
1157 | int iscsi_change_queue_depth(struct scsi_device *sdev, int depth) | |
1158 | { | |
1159 | if (depth > ISCSI_MAX_CMD_PER_LUN) | |
1160 | depth = ISCSI_MAX_CMD_PER_LUN; | |
1161 | scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth); | |
1162 | return sdev->queue_depth; | |
1163 | } | |
1164 | EXPORT_SYMBOL_GPL(iscsi_change_queue_depth); | |
1165 | ||
7996a778 MC |
1166 | void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session) |
1167 | { | |
1168 | struct iscsi_session *session = class_to_transport_session(cls_session); | |
7996a778 MC |
1169 | |
1170 | spin_lock_bh(&session->lock); | |
1171 | if (session->state != ISCSI_STATE_LOGGED_IN) { | |
656cffc9 | 1172 | session->state = ISCSI_STATE_RECOVERY_FAILED; |
843c0a8a MC |
1173 | if (session->leadconn) |
1174 | wake_up(&session->leadconn->ehwait); | |
7996a778 MC |
1175 | } |
1176 | spin_unlock_bh(&session->lock); | |
1177 | } | |
1178 | EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout); | |
1179 | ||
1180 | int iscsi_eh_host_reset(struct scsi_cmnd *sc) | |
1181 | { | |
1182 | struct Scsi_Host *host = sc->device->host; | |
1183 | struct iscsi_session *session = iscsi_hostdata(host->hostdata); | |
1184 | struct iscsi_conn *conn = session->leadconn; | |
7996a778 | 1185 | |
bc436b27 | 1186 | mutex_lock(&session->eh_mutex); |
7996a778 MC |
1187 | spin_lock_bh(&session->lock); |
1188 | if (session->state == ISCSI_STATE_TERMINATE) { | |
1189 | failed: | |
1190 | debug_scsi("failing host reset: session terminated " | |
d6e24d1c | 1191 | "[CID %d age %d]\n", conn->id, session->age); |
7996a778 | 1192 | spin_unlock_bh(&session->lock); |
bc436b27 | 1193 | mutex_unlock(&session->eh_mutex); |
7996a778 MC |
1194 | return FAILED; |
1195 | } | |
1196 | ||
7996a778 | 1197 | spin_unlock_bh(&session->lock); |
bc436b27 | 1198 | mutex_unlock(&session->eh_mutex); |
7996a778 MC |
1199 | /* |
1200 | * we drop the lock here but the leadconn cannot be destoyed while | |
1201 | * we are in the scsi eh | |
1202 | */ | |
843c0a8a | 1203 | iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED); |
7996a778 MC |
1204 | |
1205 | debug_scsi("iscsi_eh_host_reset wait for relogin\n"); | |
1206 | wait_event_interruptible(conn->ehwait, | |
1207 | session->state == ISCSI_STATE_TERMINATE || | |
1208 | session->state == ISCSI_STATE_LOGGED_IN || | |
656cffc9 | 1209 | session->state == ISCSI_STATE_RECOVERY_FAILED); |
7996a778 MC |
1210 | if (signal_pending(current)) |
1211 | flush_signals(current); | |
1212 | ||
bc436b27 | 1213 | mutex_lock(&session->eh_mutex); |
7996a778 MC |
1214 | spin_lock_bh(&session->lock); |
1215 | if (session->state == ISCSI_STATE_LOGGED_IN) | |
322d739d MC |
1216 | iscsi_session_printk(KERN_INFO, session, |
1217 | "host reset succeeded\n"); | |
7996a778 MC |
1218 | else |
1219 | goto failed; | |
1220 | spin_unlock_bh(&session->lock); | |
bc436b27 | 1221 | mutex_unlock(&session->eh_mutex); |
7996a778 MC |
1222 | return SUCCESS; |
1223 | } | |
1224 | EXPORT_SYMBOL_GPL(iscsi_eh_host_reset); | |
1225 | ||
843c0a8a | 1226 | static void iscsi_tmf_timedout(unsigned long data) |
7996a778 | 1227 | { |
843c0a8a | 1228 | struct iscsi_conn *conn = (struct iscsi_conn *)data; |
7996a778 MC |
1229 | struct iscsi_session *session = conn->session; |
1230 | ||
1231 | spin_lock(&session->lock); | |
843c0a8a MC |
1232 | if (conn->tmf_state == TMF_QUEUED) { |
1233 | conn->tmf_state = TMF_TIMEDOUT; | |
1234 | debug_scsi("tmf timedout\n"); | |
7996a778 MC |
1235 | /* unblock eh_abort() */ |
1236 | wake_up(&conn->ehwait); | |
1237 | } | |
1238 | spin_unlock(&session->lock); | |
1239 | } | |
1240 | ||
843c0a8a | 1241 | static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn, |
f6d5180c MC |
1242 | struct iscsi_tm *hdr, int age, |
1243 | int timeout) | |
7996a778 | 1244 | { |
7996a778 | 1245 | struct iscsi_session *session = conn->session; |
843c0a8a | 1246 | struct iscsi_mgmt_task *mtask; |
7996a778 | 1247 | |
843c0a8a MC |
1248 | mtask = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr, |
1249 | NULL, 0); | |
1250 | if (!mtask) { | |
6724add1 | 1251 | spin_unlock_bh(&session->lock); |
7996a778 | 1252 | iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED); |
843c0a8a MC |
1253 | spin_lock_bh(&session->lock); |
1254 | debug_scsi("tmf exec failure\n"); | |
77a23c21 | 1255 | return -EPERM; |
7996a778 | 1256 | } |
843c0a8a | 1257 | conn->tmfcmd_pdus_cnt++; |
f6d5180c | 1258 | conn->tmf_timer.expires = timeout * HZ + jiffies; |
843c0a8a MC |
1259 | conn->tmf_timer.function = iscsi_tmf_timedout; |
1260 | conn->tmf_timer.data = (unsigned long)conn; | |
1261 | add_timer(&conn->tmf_timer); | |
1262 | debug_scsi("tmf set timeout\n"); | |
7996a778 | 1263 | |
7996a778 | 1264 | spin_unlock_bh(&session->lock); |
6724add1 | 1265 | mutex_unlock(&session->eh_mutex); |
77a23c21 | 1266 | scsi_queue_work(session->host, &conn->xmitwork); |
7996a778 MC |
1267 | |
1268 | /* | |
1269 | * block eh thread until: | |
1270 | * | |
843c0a8a MC |
1271 | * 1) tmf response |
1272 | * 2) tmf timeout | |
7996a778 MC |
1273 | * 3) session is terminated or restarted or userspace has |
1274 | * given up on recovery | |
1275 | */ | |
843c0a8a | 1276 | wait_event_interruptible(conn->ehwait, age != session->age || |
7996a778 | 1277 | session->state != ISCSI_STATE_LOGGED_IN || |
843c0a8a | 1278 | conn->tmf_state != TMF_QUEUED); |
7996a778 MC |
1279 | if (signal_pending(current)) |
1280 | flush_signals(current); | |
843c0a8a MC |
1281 | del_timer_sync(&conn->tmf_timer); |
1282 | ||
6724add1 | 1283 | mutex_lock(&session->eh_mutex); |
77a23c21 | 1284 | spin_lock_bh(&session->lock); |
843c0a8a MC |
1285 | /* if the session drops it will clean up the mtask */ |
1286 | if (age != session->age || | |
1287 | session->state != ISCSI_STATE_LOGGED_IN) | |
1288 | return -ENOTCONN; | |
7996a778 MC |
1289 | return 0; |
1290 | } | |
1291 | ||
843c0a8a MC |
1292 | /* |
1293 | * Fail commands. session lock held and recv side suspended and xmit | |
1294 | * thread flushed | |
1295 | */ | |
6eabafbe MC |
1296 | static void fail_all_commands(struct iscsi_conn *conn, unsigned lun, |
1297 | int error) | |
843c0a8a MC |
1298 | { |
1299 | struct iscsi_cmd_task *ctask, *tmp; | |
1300 | ||
1301 | if (conn->ctask && (conn->ctask->sc->device->lun == lun || lun == -1)) | |
1302 | conn->ctask = NULL; | |
1303 | ||
1304 | /* flush pending */ | |
1305 | list_for_each_entry_safe(ctask, tmp, &conn->xmitqueue, running) { | |
1306 | if (lun == ctask->sc->device->lun || lun == -1) { | |
1307 | debug_scsi("failing pending sc %p itt 0x%x\n", | |
1308 | ctask->sc, ctask->itt); | |
6eabafbe | 1309 | fail_command(conn, ctask, error << 16); |
843c0a8a MC |
1310 | } |
1311 | } | |
1312 | ||
1313 | list_for_each_entry_safe(ctask, tmp, &conn->requeue, running) { | |
1314 | if (lun == ctask->sc->device->lun || lun == -1) { | |
1315 | debug_scsi("failing requeued sc %p itt 0x%x\n", | |
1316 | ctask->sc, ctask->itt); | |
6eabafbe | 1317 | fail_command(conn, ctask, error << 16); |
843c0a8a MC |
1318 | } |
1319 | } | |
1320 | ||
1321 | /* fail all other running */ | |
1322 | list_for_each_entry_safe(ctask, tmp, &conn->run_list, running) { | |
1323 | if (lun == ctask->sc->device->lun || lun == -1) { | |
1324 | debug_scsi("failing in progress sc %p itt 0x%x\n", | |
1325 | ctask->sc, ctask->itt); | |
1326 | fail_command(conn, ctask, DID_BUS_BUSY << 16); | |
1327 | } | |
1328 | } | |
1329 | } | |
1330 | ||
6724add1 MC |
1331 | static void iscsi_suspend_tx(struct iscsi_conn *conn) |
1332 | { | |
1333 | set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx); | |
1334 | scsi_flush_work(conn->session->host); | |
1335 | } | |
1336 | ||
1337 | static void iscsi_start_tx(struct iscsi_conn *conn) | |
1338 | { | |
1339 | clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx); | |
1340 | scsi_queue_work(conn->session->host, &conn->xmitwork); | |
1341 | } | |
1342 | ||
f6d5180c MC |
1343 | static enum scsi_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *scmd) |
1344 | { | |
1345 | struct iscsi_cls_session *cls_session; | |
1346 | struct iscsi_session *session; | |
1347 | struct iscsi_conn *conn; | |
1348 | enum scsi_eh_timer_return rc = EH_NOT_HANDLED; | |
1349 | ||
1350 | cls_session = starget_to_session(scsi_target(scmd->device)); | |
1351 | session = class_to_transport_session(cls_session); | |
1352 | ||
1353 | debug_scsi("scsi cmd %p timedout\n", scmd); | |
1354 | ||
1355 | spin_lock(&session->lock); | |
1356 | if (session->state != ISCSI_STATE_LOGGED_IN) { | |
1357 | /* | |
1358 | * We are probably in the middle of iscsi recovery so let | |
1359 | * that complete and handle the error. | |
1360 | */ | |
1361 | rc = EH_RESET_TIMER; | |
1362 | goto done; | |
1363 | } | |
1364 | ||
1365 | conn = session->leadconn; | |
1366 | if (!conn) { | |
1367 | /* In the middle of shuting down */ | |
1368 | rc = EH_RESET_TIMER; | |
1369 | goto done; | |
1370 | } | |
1371 | ||
1372 | if (!conn->recv_timeout && !conn->ping_timeout) | |
1373 | goto done; | |
1374 | /* | |
1375 | * if the ping timedout then we are in the middle of cleaning up | |
1376 | * and can let the iscsi eh handle it | |
1377 | */ | |
1378 | if (time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) + | |
1379 | (conn->ping_timeout * HZ), jiffies)) | |
1380 | rc = EH_RESET_TIMER; | |
1381 | /* | |
1382 | * if we are about to check the transport then give the command | |
1383 | * more time | |
1384 | */ | |
1385 | if (time_before_eq(conn->last_recv + (conn->recv_timeout * HZ), | |
1386 | jiffies)) | |
1387 | rc = EH_RESET_TIMER; | |
1388 | /* if in the middle of checking the transport then give us more time */ | |
1389 | if (conn->ping_mtask) | |
1390 | rc = EH_RESET_TIMER; | |
1391 | done: | |
1392 | spin_unlock(&session->lock); | |
1393 | debug_scsi("return %s\n", rc == EH_RESET_TIMER ? "timer reset" : "nh"); | |
1394 | return rc; | |
1395 | } | |
1396 | ||
1397 | static void iscsi_check_transport_timeouts(unsigned long data) | |
1398 | { | |
1399 | struct iscsi_conn *conn = (struct iscsi_conn *)data; | |
1400 | struct iscsi_session *session = conn->session; | |
1401 | unsigned long timeout, next_timeout = 0, last_recv; | |
1402 | ||
1403 | spin_lock(&session->lock); | |
1404 | if (session->state != ISCSI_STATE_LOGGED_IN) | |
1405 | goto done; | |
1406 | ||
1407 | timeout = conn->recv_timeout; | |
1408 | if (!timeout) | |
1409 | goto done; | |
1410 | ||
1411 | timeout *= HZ; | |
1412 | last_recv = conn->last_recv; | |
1413 | if (time_before_eq(last_recv + timeout + (conn->ping_timeout * HZ), | |
1414 | jiffies)) { | |
322d739d MC |
1415 | iscsi_conn_printk(KERN_ERR, conn, "ping timeout of %d secs " |
1416 | "expired, last rx %lu, last ping %lu, " | |
1417 | "now %lu\n", conn->ping_timeout, last_recv, | |
1418 | conn->last_ping, jiffies); | |
f6d5180c MC |
1419 | spin_unlock(&session->lock); |
1420 | iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED); | |
1421 | return; | |
1422 | } | |
1423 | ||
1424 | if (time_before_eq(last_recv + timeout, jiffies)) { | |
1425 | if (time_before_eq(conn->last_ping, last_recv)) { | |
1426 | /* send a ping to try to provoke some traffic */ | |
1427 | debug_scsi("Sending nopout as ping on conn %p\n", conn); | |
1428 | iscsi_send_nopout(conn, NULL); | |
1429 | } | |
1430 | next_timeout = last_recv + timeout + (conn->ping_timeout * HZ); | |
ad294e9c | 1431 | } else |
f6d5180c | 1432 | next_timeout = last_recv + timeout; |
f6d5180c | 1433 | |
ad294e9c MC |
1434 | debug_scsi("Setting next tmo %lu\n", next_timeout); |
1435 | mod_timer(&conn->transport_timer, next_timeout); | |
f6d5180c MC |
1436 | done: |
1437 | spin_unlock(&session->lock); | |
1438 | } | |
1439 | ||
843c0a8a MC |
1440 | static void iscsi_prep_abort_task_pdu(struct iscsi_cmd_task *ctask, |
1441 | struct iscsi_tm *hdr) | |
1442 | { | |
1443 | memset(hdr, 0, sizeof(*hdr)); | |
1444 | hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE; | |
1445 | hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK; | |
1446 | hdr->flags |= ISCSI_FLAG_CMD_FINAL; | |
1447 | memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun)); | |
1448 | hdr->rtt = ctask->hdr->itt; | |
1449 | hdr->refcmdsn = ctask->hdr->cmdsn; | |
1450 | } | |
1451 | ||
7996a778 MC |
1452 | int iscsi_eh_abort(struct scsi_cmnd *sc) |
1453 | { | |
6724add1 MC |
1454 | struct Scsi_Host *host = sc->device->host; |
1455 | struct iscsi_session *session = iscsi_hostdata(host->hostdata); | |
f47f2cf5 | 1456 | struct iscsi_conn *conn; |
843c0a8a MC |
1457 | struct iscsi_cmd_task *ctask; |
1458 | struct iscsi_tm *hdr; | |
1459 | int rc, age; | |
7996a778 | 1460 | |
6724add1 MC |
1461 | mutex_lock(&session->eh_mutex); |
1462 | spin_lock_bh(&session->lock); | |
f47f2cf5 MC |
1463 | /* |
1464 | * if session was ISCSI_STATE_IN_RECOVERY then we may not have | |
1465 | * got the command. | |
1466 | */ | |
1467 | if (!sc->SCp.ptr) { | |
1468 | debug_scsi("sc never reached iscsi layer or it completed.\n"); | |
6724add1 MC |
1469 | spin_unlock_bh(&session->lock); |
1470 | mutex_unlock(&session->eh_mutex); | |
f47f2cf5 MC |
1471 | return SUCCESS; |
1472 | } | |
1473 | ||
7996a778 MC |
1474 | /* |
1475 | * If we are not logged in or we have started a new session | |
1476 | * then let the host reset code handle this | |
1477 | */ | |
843c0a8a MC |
1478 | if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN || |
1479 | sc->SCp.phase != session->age) { | |
1480 | spin_unlock_bh(&session->lock); | |
1481 | mutex_unlock(&session->eh_mutex); | |
1482 | return FAILED; | |
1483 | } | |
1484 | ||
1485 | conn = session->leadconn; | |
1486 | conn->eh_abort_cnt++; | |
1487 | age = session->age; | |
1488 | ||
1489 | ctask = (struct iscsi_cmd_task *)sc->SCp.ptr; | |
1490 | debug_scsi("aborting [sc %p itt 0x%x]\n", sc, ctask->itt); | |
7996a778 MC |
1491 | |
1492 | /* ctask completed before time out */ | |
7ea8b828 | 1493 | if (!ctask->sc) { |
7ea8b828 | 1494 | debug_scsi("sc completed while abort in progress\n"); |
77a23c21 | 1495 | goto success; |
7ea8b828 | 1496 | } |
7996a778 | 1497 | |
77a23c21 MC |
1498 | if (ctask->state == ISCSI_TASK_PENDING) { |
1499 | fail_command(conn, ctask, DID_ABORT << 16); | |
1500 | goto success; | |
1501 | } | |
7996a778 | 1502 | |
843c0a8a MC |
1503 | /* only have one tmf outstanding at a time */ |
1504 | if (conn->tmf_state != TMF_INITIAL) | |
7996a778 | 1505 | goto failed; |
843c0a8a | 1506 | conn->tmf_state = TMF_QUEUED; |
7996a778 | 1507 | |
843c0a8a MC |
1508 | hdr = &conn->tmhdr; |
1509 | iscsi_prep_abort_task_pdu(ctask, hdr); | |
1510 | ||
f6d5180c | 1511 | if (iscsi_exec_task_mgmt_fn(conn, hdr, age, session->abort_timeout)) { |
843c0a8a MC |
1512 | rc = FAILED; |
1513 | goto failed; | |
1514 | } | |
1515 | ||
1516 | switch (conn->tmf_state) { | |
1517 | case TMF_SUCCESS: | |
77a23c21 | 1518 | spin_unlock_bh(&session->lock); |
6724add1 | 1519 | iscsi_suspend_tx(conn); |
77a23c21 MC |
1520 | /* |
1521 | * clean up task if aborted. grab the recv lock as a writer | |
1522 | */ | |
1523 | write_lock_bh(conn->recv_lock); | |
1524 | spin_lock(&session->lock); | |
1525 | fail_command(conn, ctask, DID_ABORT << 16); | |
843c0a8a | 1526 | conn->tmf_state = TMF_INITIAL; |
77a23c21 MC |
1527 | spin_unlock(&session->lock); |
1528 | write_unlock_bh(conn->recv_lock); | |
6724add1 | 1529 | iscsi_start_tx(conn); |
77a23c21 | 1530 | goto success_unlocked; |
843c0a8a MC |
1531 | case TMF_TIMEDOUT: |
1532 | spin_unlock_bh(&session->lock); | |
1533 | iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED); | |
1534 | goto failed_unlocked; | |
1535 | case TMF_NOT_FOUND: | |
1536 | if (!sc->SCp.ptr) { | |
1537 | conn->tmf_state = TMF_INITIAL; | |
7ea8b828 | 1538 | /* ctask completed before tmf abort response */ |
7ea8b828 | 1539 | debug_scsi("sc completed while abort in progress\n"); |
77a23c21 | 1540 | goto success; |
7ea8b828 MC |
1541 | } |
1542 | /* fall through */ | |
1543 | default: | |
843c0a8a MC |
1544 | conn->tmf_state = TMF_INITIAL; |
1545 | goto failed; | |
7996a778 MC |
1546 | } |
1547 | ||
77a23c21 | 1548 | success: |
7996a778 | 1549 | spin_unlock_bh(&session->lock); |
77a23c21 MC |
1550 | success_unlocked: |
1551 | debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc, ctask->itt); | |
6724add1 | 1552 | mutex_unlock(&session->eh_mutex); |
7996a778 MC |
1553 | return SUCCESS; |
1554 | ||
1555 | failed: | |
1556 | spin_unlock_bh(&session->lock); | |
77a23c21 | 1557 | failed_unlocked: |
843c0a8a MC |
1558 | debug_scsi("abort failed [sc %p itt 0x%x]\n", sc, |
1559 | ctask ? ctask->itt : 0); | |
6724add1 | 1560 | mutex_unlock(&session->eh_mutex); |
7996a778 MC |
1561 | return FAILED; |
1562 | } | |
1563 | EXPORT_SYMBOL_GPL(iscsi_eh_abort); | |
1564 | ||
843c0a8a MC |
1565 | static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr) |
1566 | { | |
1567 | memset(hdr, 0, sizeof(*hdr)); | |
1568 | hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE; | |
1569 | hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK; | |
1570 | hdr->flags |= ISCSI_FLAG_CMD_FINAL; | |
1571 | int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun); | |
f6d5180c | 1572 | hdr->rtt = RESERVED_ITT; |
843c0a8a MC |
1573 | } |
1574 | ||
1575 | int iscsi_eh_device_reset(struct scsi_cmnd *sc) | |
1576 | { | |
1577 | struct Scsi_Host *host = sc->device->host; | |
1578 | struct iscsi_session *session = iscsi_hostdata(host->hostdata); | |
1579 | struct iscsi_conn *conn; | |
1580 | struct iscsi_tm *hdr; | |
1581 | int rc = FAILED; | |
1582 | ||
1583 | debug_scsi("LU Reset [sc %p lun %u]\n", sc, sc->device->lun); | |
1584 | ||
1585 | mutex_lock(&session->eh_mutex); | |
1586 | spin_lock_bh(&session->lock); | |
1587 | /* | |
1588 | * Just check if we are not logged in. We cannot check for | |
1589 | * the phase because the reset could come from a ioctl. | |
1590 | */ | |
1591 | if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN) | |
1592 | goto unlock; | |
1593 | conn = session->leadconn; | |
1594 | ||
1595 | /* only have one tmf outstanding at a time */ | |
1596 | if (conn->tmf_state != TMF_INITIAL) | |
1597 | goto unlock; | |
1598 | conn->tmf_state = TMF_QUEUED; | |
1599 | ||
1600 | hdr = &conn->tmhdr; | |
1601 | iscsi_prep_lun_reset_pdu(sc, hdr); | |
1602 | ||
f6d5180c MC |
1603 | if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age, |
1604 | session->lu_reset_timeout)) { | |
843c0a8a MC |
1605 | rc = FAILED; |
1606 | goto unlock; | |
1607 | } | |
1608 | ||
1609 | switch (conn->tmf_state) { | |
1610 | case TMF_SUCCESS: | |
1611 | break; | |
1612 | case TMF_TIMEDOUT: | |
1613 | spin_unlock_bh(&session->lock); | |
1614 | iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED); | |
1615 | goto done; | |
1616 | default: | |
1617 | conn->tmf_state = TMF_INITIAL; | |
1618 | goto unlock; | |
1619 | } | |
1620 | ||
1621 | rc = SUCCESS; | |
1622 | spin_unlock_bh(&session->lock); | |
1623 | ||
1624 | iscsi_suspend_tx(conn); | |
1625 | /* need to grab the recv lock then session lock */ | |
1626 | write_lock_bh(conn->recv_lock); | |
1627 | spin_lock(&session->lock); | |
6eabafbe | 1628 | fail_all_commands(conn, sc->device->lun, DID_ERROR); |
843c0a8a MC |
1629 | conn->tmf_state = TMF_INITIAL; |
1630 | spin_unlock(&session->lock); | |
1631 | write_unlock_bh(conn->recv_lock); | |
1632 | ||
1633 | iscsi_start_tx(conn); | |
1634 | goto done; | |
1635 | ||
1636 | unlock: | |
1637 | spin_unlock_bh(&session->lock); | |
1638 | done: | |
1639 | debug_scsi("iscsi_eh_device_reset %s\n", | |
1640 | rc == SUCCESS ? "SUCCESS" : "FAILED"); | |
1641 | mutex_unlock(&session->eh_mutex); | |
1642 | return rc; | |
1643 | } | |
1644 | EXPORT_SYMBOL_GPL(iscsi_eh_device_reset); | |
1645 | ||
6320377f OK |
1646 | /* |
1647 | * Pre-allocate a pool of @max items of @item_size. By default, the pool | |
1648 | * should be accessed via kfifo_{get,put} on q->queue. | |
1649 | * Optionally, the caller can obtain the array of object pointers | |
1650 | * by passing in a non-NULL @items pointer | |
1651 | */ | |
7996a778 | 1652 | int |
6320377f | 1653 | iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size) |
7996a778 | 1654 | { |
6320377f | 1655 | int i, num_arrays = 1; |
7996a778 | 1656 | |
6320377f | 1657 | memset(q, 0, sizeof(*q)); |
7996a778 MC |
1658 | |
1659 | q->max = max; | |
6320377f OK |
1660 | |
1661 | /* If the user passed an items pointer, he wants a copy of | |
1662 | * the array. */ | |
1663 | if (items) | |
1664 | num_arrays++; | |
1665 | q->pool = kzalloc(num_arrays * max * sizeof(void*), GFP_KERNEL); | |
1666 | if (q->pool == NULL) | |
1667 | goto enomem; | |
7996a778 MC |
1668 | |
1669 | q->queue = kfifo_init((void*)q->pool, max * sizeof(void*), | |
1670 | GFP_KERNEL, NULL); | |
6320377f OK |
1671 | if (q->queue == ERR_PTR(-ENOMEM)) |
1672 | goto enomem; | |
7996a778 MC |
1673 | |
1674 | for (i = 0; i < max; i++) { | |
6320377f | 1675 | q->pool[i] = kzalloc(item_size, GFP_KERNEL); |
7996a778 | 1676 | if (q->pool[i] == NULL) { |
6320377f OK |
1677 | q->max = i; |
1678 | goto enomem; | |
7996a778 | 1679 | } |
7996a778 MC |
1680 | __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*)); |
1681 | } | |
6320377f OK |
1682 | |
1683 | if (items) { | |
1684 | *items = q->pool + max; | |
1685 | memcpy(*items, q->pool, max * sizeof(void *)); | |
1686 | } | |
1687 | ||
7996a778 | 1688 | return 0; |
6320377f OK |
1689 | |
1690 | enomem: | |
1691 | iscsi_pool_free(q); | |
1692 | return -ENOMEM; | |
7996a778 MC |
1693 | } |
1694 | EXPORT_SYMBOL_GPL(iscsi_pool_init); | |
1695 | ||
6320377f | 1696 | void iscsi_pool_free(struct iscsi_pool *q) |
7996a778 MC |
1697 | { |
1698 | int i; | |
1699 | ||
1700 | for (i = 0; i < q->max; i++) | |
6320377f OK |
1701 | kfree(q->pool[i]); |
1702 | if (q->pool) | |
1703 | kfree(q->pool); | |
7996a778 MC |
1704 | } |
1705 | EXPORT_SYMBOL_GPL(iscsi_pool_free); | |
1706 | ||
1707 | /* | |
1708 | * iSCSI Session's hostdata organization: | |
1709 | * | |
1710 | * *------------------* <== hostdata_session(host->hostdata) | |
1711 | * | ptr to class sess| | |
1712 | * |------------------| <== iscsi_hostdata(host->hostdata) | |
1713 | * | iscsi_session | | |
1714 | * *------------------* | |
1715 | */ | |
1716 | ||
1717 | #define hostdata_privsize(_sz) (sizeof(unsigned long) + _sz + \ | |
1718 | _sz % sizeof(unsigned long)) | |
1719 | ||
1720 | #define hostdata_session(_hostdata) (iscsi_ptr(*(unsigned long *)_hostdata)) | |
1721 | ||
1722 | /** | |
1723 | * iscsi_session_setup - create iscsi cls session and host and session | |
1724 | * @scsit: scsi transport template | |
1725 | * @iscsit: iscsi transport template | |
1548271e MC |
1726 | * @cmds_max: scsi host can queue |
1727 | * @qdepth: scsi host cmds per lun | |
1728 | * @cmd_task_size: LLD ctask private data size | |
1729 | * @mgmt_task_size: LLD mtask private data size | |
7996a778 MC |
1730 | * @initial_cmdsn: initial CmdSN |
1731 | * @hostno: host no allocated | |
1732 | * | |
1733 | * This can be used by software iscsi_transports that allocate | |
1734 | * a session per scsi host. | |
1735 | **/ | |
1736 | struct iscsi_cls_session * | |
1737 | iscsi_session_setup(struct iscsi_transport *iscsit, | |
1738 | struct scsi_transport_template *scsit, | |
1548271e | 1739 | uint16_t cmds_max, uint16_t qdepth, |
7996a778 MC |
1740 | int cmd_task_size, int mgmt_task_size, |
1741 | uint32_t initial_cmdsn, uint32_t *hostno) | |
1742 | { | |
1743 | struct Scsi_Host *shost; | |
1744 | struct iscsi_session *session; | |
1745 | struct iscsi_cls_session *cls_session; | |
1746 | int cmd_i; | |
1747 | ||
1548271e MC |
1748 | if (qdepth > ISCSI_MAX_CMD_PER_LUN || qdepth < 1) { |
1749 | if (qdepth != 0) | |
1750 | printk(KERN_ERR "iscsi: invalid queue depth of %d. " | |
1751 | "Queue depth must be between 1 and %d.\n", | |
1752 | qdepth, ISCSI_MAX_CMD_PER_LUN); | |
1753 | qdepth = ISCSI_DEF_CMD_PER_LUN; | |
1754 | } | |
1755 | ||
31ed0bf4 MC |
1756 | if (!is_power_of_2(cmds_max) || cmds_max >= ISCSI_MGMT_ITT_OFFSET || |
1757 | cmds_max < 2) { | |
1548271e MC |
1758 | if (cmds_max != 0) |
1759 | printk(KERN_ERR "iscsi: invalid can_queue of %d. " | |
1760 | "can_queue must be a power of 2 and between " | |
1761 | "2 and %d - setting to %d.\n", cmds_max, | |
1762 | ISCSI_MGMT_ITT_OFFSET, ISCSI_DEF_XMIT_CMDS_MAX); | |
1763 | cmds_max = ISCSI_DEF_XMIT_CMDS_MAX; | |
1764 | } | |
1765 | ||
7996a778 MC |
1766 | shost = scsi_host_alloc(iscsit->host_template, |
1767 | hostdata_privsize(sizeof(*session))); | |
1768 | if (!shost) | |
1769 | return NULL; | |
1770 | ||
1548271e MC |
1771 | /* the iscsi layer takes one task for reserve */ |
1772 | shost->can_queue = cmds_max - 1; | |
1773 | shost->cmd_per_lun = qdepth; | |
7996a778 MC |
1774 | shost->max_id = 1; |
1775 | shost->max_channel = 0; | |
1776 | shost->max_lun = iscsit->max_lun; | |
1777 | shost->max_cmd_len = iscsit->max_cmd_len; | |
1778 | shost->transportt = scsit; | |
1779 | shost->transportt->create_work_queue = 1; | |
f6d5180c | 1780 | shost->transportt->eh_timed_out = iscsi_eh_cmd_timed_out; |
7996a778 MC |
1781 | *hostno = shost->host_no; |
1782 | ||
1783 | session = iscsi_hostdata(shost->hostdata); | |
1784 | memset(session, 0, sizeof(struct iscsi_session)); | |
1785 | session->host = shost; | |
1786 | session->state = ISCSI_STATE_FREE; | |
f6d5180c | 1787 | session->fast_abort = 1; |
4cd49ea1 MC |
1788 | session->lu_reset_timeout = 15; |
1789 | session->abort_timeout = 10; | |
7996a778 | 1790 | session->mgmtpool_max = ISCSI_MGMT_CMDS_MAX; |
1548271e | 1791 | session->cmds_max = cmds_max; |
e0726407 | 1792 | session->queued_cmdsn = session->cmdsn = initial_cmdsn; |
7996a778 MC |
1793 | session->exp_cmdsn = initial_cmdsn + 1; |
1794 | session->max_cmdsn = initial_cmdsn + 1; | |
1795 | session->max_r2t = 1; | |
1796 | session->tt = iscsit; | |
6724add1 | 1797 | mutex_init(&session->eh_mutex); |
7996a778 MC |
1798 | |
1799 | /* initialize SCSI PDU commands pool */ | |
1800 | if (iscsi_pool_init(&session->cmdpool, session->cmds_max, | |
1801 | (void***)&session->cmds, | |
1802 | cmd_task_size + sizeof(struct iscsi_cmd_task))) | |
1803 | goto cmdpool_alloc_fail; | |
1804 | ||
1805 | /* pre-format cmds pool with ITT */ | |
1806 | for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) { | |
1807 | struct iscsi_cmd_task *ctask = session->cmds[cmd_i]; | |
1808 | ||
1809 | if (cmd_task_size) | |
1810 | ctask->dd_data = &ctask[1]; | |
1811 | ctask->itt = cmd_i; | |
b6c395ed | 1812 | INIT_LIST_HEAD(&ctask->running); |
7996a778 MC |
1813 | } |
1814 | ||
1815 | spin_lock_init(&session->lock); | |
7996a778 MC |
1816 | |
1817 | /* initialize immediate command pool */ | |
1818 | if (iscsi_pool_init(&session->mgmtpool, session->mgmtpool_max, | |
1819 | (void***)&session->mgmt_cmds, | |
1820 | mgmt_task_size + sizeof(struct iscsi_mgmt_task))) | |
1821 | goto mgmtpool_alloc_fail; | |
1822 | ||
1823 | ||
1824 | /* pre-format immediate cmds pool with ITT */ | |
1825 | for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) { | |
1826 | struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i]; | |
1827 | ||
1828 | if (mgmt_task_size) | |
1829 | mtask->dd_data = &mtask[1]; | |
1830 | mtask->itt = ISCSI_MGMT_ITT_OFFSET + cmd_i; | |
b6c395ed | 1831 | INIT_LIST_HEAD(&mtask->running); |
7996a778 MC |
1832 | } |
1833 | ||
1834 | if (scsi_add_host(shost, NULL)) | |
1835 | goto add_host_fail; | |
1836 | ||
f53a88da MC |
1837 | if (!try_module_get(iscsit->owner)) |
1838 | goto cls_session_fail; | |
1839 | ||
6a8a0d36 | 1840 | cls_session = iscsi_create_session(shost, iscsit, 0); |
7996a778 | 1841 | if (!cls_session) |
f53a88da | 1842 | goto module_put; |
7996a778 MC |
1843 | *(unsigned long*)shost->hostdata = (unsigned long)cls_session; |
1844 | ||
1845 | return cls_session; | |
1846 | ||
f53a88da MC |
1847 | module_put: |
1848 | module_put(iscsit->owner); | |
7996a778 MC |
1849 | cls_session_fail: |
1850 | scsi_remove_host(shost); | |
1851 | add_host_fail: | |
6320377f | 1852 | iscsi_pool_free(&session->mgmtpool); |
7996a778 | 1853 | mgmtpool_alloc_fail: |
6320377f | 1854 | iscsi_pool_free(&session->cmdpool); |
7996a778 MC |
1855 | cmdpool_alloc_fail: |
1856 | scsi_host_put(shost); | |
1857 | return NULL; | |
1858 | } | |
1859 | EXPORT_SYMBOL_GPL(iscsi_session_setup); | |
1860 | ||
1861 | /** | |
1862 | * iscsi_session_teardown - destroy session, host, and cls_session | |
1863 | * shost: scsi host | |
1864 | * | |
1865 | * This can be used by software iscsi_transports that allocate | |
1866 | * a session per scsi host. | |
1867 | **/ | |
1868 | void iscsi_session_teardown(struct iscsi_cls_session *cls_session) | |
1869 | { | |
1870 | struct Scsi_Host *shost = iscsi_session_to_shost(cls_session); | |
1871 | struct iscsi_session *session = iscsi_hostdata(shost->hostdata); | |
63f75cc8 | 1872 | struct module *owner = cls_session->transport->owner; |
7996a778 | 1873 | |
26974789 | 1874 | iscsi_remove_session(cls_session); |
7996a778 MC |
1875 | scsi_remove_host(shost); |
1876 | ||
6320377f OK |
1877 | iscsi_pool_free(&session->mgmtpool); |
1878 | iscsi_pool_free(&session->cmdpool); | |
7996a778 | 1879 | |
b2c64167 MC |
1880 | kfree(session->password); |
1881 | kfree(session->password_in); | |
1882 | kfree(session->username); | |
1883 | kfree(session->username_in); | |
f3ff0c36 | 1884 | kfree(session->targetname); |
d8196ed2 | 1885 | kfree(session->netdev); |
0801c242 | 1886 | kfree(session->hwaddress); |
8ad5781a | 1887 | kfree(session->initiatorname); |
f3ff0c36 | 1888 | |
26974789 | 1889 | iscsi_free_session(cls_session); |
7996a778 | 1890 | scsi_host_put(shost); |
63f75cc8 | 1891 | module_put(owner); |
7996a778 MC |
1892 | } |
1893 | EXPORT_SYMBOL_GPL(iscsi_session_teardown); | |
1894 | ||
1895 | /** | |
1896 | * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn | |
1897 | * @cls_session: iscsi_cls_session | |
1898 | * @conn_idx: cid | |
1899 | **/ | |
1900 | struct iscsi_cls_conn * | |
1901 | iscsi_conn_setup(struct iscsi_cls_session *cls_session, uint32_t conn_idx) | |
1902 | { | |
1903 | struct iscsi_session *session = class_to_transport_session(cls_session); | |
1904 | struct iscsi_conn *conn; | |
1905 | struct iscsi_cls_conn *cls_conn; | |
d36ab6f3 | 1906 | char *data; |
7996a778 MC |
1907 | |
1908 | cls_conn = iscsi_create_conn(cls_session, conn_idx); | |
1909 | if (!cls_conn) | |
1910 | return NULL; | |
1911 | conn = cls_conn->dd_data; | |
1912 | memset(conn, 0, sizeof(*conn)); | |
1913 | ||
1914 | conn->session = session; | |
1915 | conn->cls_conn = cls_conn; | |
1916 | conn->c_stage = ISCSI_CONN_INITIAL_STAGE; | |
1917 | conn->id = conn_idx; | |
1918 | conn->exp_statsn = 0; | |
843c0a8a | 1919 | conn->tmf_state = TMF_INITIAL; |
f6d5180c MC |
1920 | |
1921 | init_timer(&conn->transport_timer); | |
1922 | conn->transport_timer.data = (unsigned long)conn; | |
1923 | conn->transport_timer.function = iscsi_check_transport_timeouts; | |
1924 | ||
7996a778 MC |
1925 | INIT_LIST_HEAD(&conn->run_list); |
1926 | INIT_LIST_HEAD(&conn->mgmt_run_list); | |
843c0a8a | 1927 | INIT_LIST_HEAD(&conn->mgmtqueue); |
b6c395ed | 1928 | INIT_LIST_HEAD(&conn->xmitqueue); |
843c0a8a | 1929 | INIT_LIST_HEAD(&conn->requeue); |
c4028958 | 1930 | INIT_WORK(&conn->xmitwork, iscsi_xmitworker); |
7996a778 MC |
1931 | |
1932 | /* allocate login_mtask used for the login/text sequences */ | |
1933 | spin_lock_bh(&session->lock); | |
1934 | if (!__kfifo_get(session->mgmtpool.queue, | |
1935 | (void*)&conn->login_mtask, | |
1936 | sizeof(void*))) { | |
1937 | spin_unlock_bh(&session->lock); | |
1938 | goto login_mtask_alloc_fail; | |
1939 | } | |
1940 | spin_unlock_bh(&session->lock); | |
1941 | ||
bf32ed33 | 1942 | data = kmalloc(ISCSI_DEF_MAX_RECV_SEG_LEN, GFP_KERNEL); |
d36ab6f3 MC |
1943 | if (!data) |
1944 | goto login_mtask_data_alloc_fail; | |
c8dc1e52 | 1945 | conn->login_mtask->data = conn->data = data; |
d36ab6f3 | 1946 | |
843c0a8a | 1947 | init_timer(&conn->tmf_timer); |
7996a778 MC |
1948 | init_waitqueue_head(&conn->ehwait); |
1949 | ||
1950 | return cls_conn; | |
1951 | ||
d36ab6f3 MC |
1952 | login_mtask_data_alloc_fail: |
1953 | __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask, | |
1954 | sizeof(void*)); | |
7996a778 | 1955 | login_mtask_alloc_fail: |
7996a778 MC |
1956 | iscsi_destroy_conn(cls_conn); |
1957 | return NULL; | |
1958 | } | |
1959 | EXPORT_SYMBOL_GPL(iscsi_conn_setup); | |
1960 | ||
1961 | /** | |
1962 | * iscsi_conn_teardown - teardown iscsi connection | |
1963 | * cls_conn: iscsi class connection | |
1964 | * | |
1965 | * TODO: we may need to make this into a two step process | |
1966 | * like scsi-mls remove + put host | |
1967 | */ | |
1968 | void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn) | |
1969 | { | |
1970 | struct iscsi_conn *conn = cls_conn->dd_data; | |
1971 | struct iscsi_session *session = conn->session; | |
1972 | unsigned long flags; | |
1973 | ||
f6d5180c MC |
1974 | del_timer_sync(&conn->transport_timer); |
1975 | ||
7996a778 MC |
1976 | spin_lock_bh(&session->lock); |
1977 | conn->c_stage = ISCSI_CONN_CLEANUP_WAIT; | |
1978 | if (session->leadconn == conn) { | |
1979 | /* | |
1980 | * leading connection? then give up on recovery. | |
1981 | */ | |
1982 | session->state = ISCSI_STATE_TERMINATE; | |
1983 | wake_up(&conn->ehwait); | |
1984 | } | |
1985 | spin_unlock_bh(&session->lock); | |
1986 | ||
7996a778 MC |
1987 | /* |
1988 | * Block until all in-progress commands for this connection | |
1989 | * time out or fail. | |
1990 | */ | |
1991 | for (;;) { | |
1992 | spin_lock_irqsave(session->host->host_lock, flags); | |
1993 | if (!session->host->host_busy) { /* OK for ERL == 0 */ | |
1994 | spin_unlock_irqrestore(session->host->host_lock, flags); | |
1995 | break; | |
1996 | } | |
1997 | spin_unlock_irqrestore(session->host->host_lock, flags); | |
1998 | msleep_interruptible(500); | |
322d739d MC |
1999 | iscsi_conn_printk(KERN_INFO, conn, "iscsi conn_destroy(): " |
2000 | "host_busy %d host_failed %d\n", | |
2001 | session->host->host_busy, | |
2002 | session->host->host_failed); | |
7996a778 MC |
2003 | /* |
2004 | * force eh_abort() to unblock | |
2005 | */ | |
2006 | wake_up(&conn->ehwait); | |
2007 | } | |
2008 | ||
779ea120 | 2009 | /* flush queued up work because we free the connection below */ |
843c0a8a | 2010 | iscsi_suspend_tx(conn); |
779ea120 | 2011 | |
7996a778 | 2012 | spin_lock_bh(&session->lock); |
c8dc1e52 | 2013 | kfree(conn->data); |
f3ff0c36 | 2014 | kfree(conn->persistent_address); |
7996a778 MC |
2015 | __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask, |
2016 | sizeof(void*)); | |
e0726407 | 2017 | if (session->leadconn == conn) |
7996a778 | 2018 | session->leadconn = NULL; |
7996a778 MC |
2019 | spin_unlock_bh(&session->lock); |
2020 | ||
7996a778 MC |
2021 | iscsi_destroy_conn(cls_conn); |
2022 | } | |
2023 | EXPORT_SYMBOL_GPL(iscsi_conn_teardown); | |
2024 | ||
2025 | int iscsi_conn_start(struct iscsi_cls_conn *cls_conn) | |
2026 | { | |
2027 | struct iscsi_conn *conn = cls_conn->dd_data; | |
2028 | struct iscsi_session *session = conn->session; | |
2029 | ||
ffd0436e | 2030 | if (!session) { |
322d739d MC |
2031 | iscsi_conn_printk(KERN_ERR, conn, |
2032 | "can't start unbound connection\n"); | |
7996a778 MC |
2033 | return -EPERM; |
2034 | } | |
2035 | ||
db98ccde MC |
2036 | if ((session->imm_data_en || !session->initial_r2t_en) && |
2037 | session->first_burst > session->max_burst) { | |
322d739d MC |
2038 | iscsi_conn_printk(KERN_INFO, conn, "invalid burst lengths: " |
2039 | "first_burst %d max_burst %d\n", | |
2040 | session->first_burst, session->max_burst); | |
ffd0436e MC |
2041 | return -EINVAL; |
2042 | } | |
2043 | ||
f6d5180c | 2044 | if (conn->ping_timeout && !conn->recv_timeout) { |
322d739d MC |
2045 | iscsi_conn_printk(KERN_ERR, conn, "invalid recv timeout of " |
2046 | "zero. Using 5 seconds\n."); | |
f6d5180c MC |
2047 | conn->recv_timeout = 5; |
2048 | } | |
2049 | ||
2050 | if (conn->recv_timeout && !conn->ping_timeout) { | |
322d739d MC |
2051 | iscsi_conn_printk(KERN_ERR, conn, "invalid ping timeout of " |
2052 | "zero. Using 5 seconds.\n"); | |
f6d5180c MC |
2053 | conn->ping_timeout = 5; |
2054 | } | |
2055 | ||
7996a778 MC |
2056 | spin_lock_bh(&session->lock); |
2057 | conn->c_stage = ISCSI_CONN_STARTED; | |
2058 | session->state = ISCSI_STATE_LOGGED_IN; | |
e0726407 | 2059 | session->queued_cmdsn = session->cmdsn; |
7996a778 | 2060 | |
f6d5180c MC |
2061 | conn->last_recv = jiffies; |
2062 | conn->last_ping = jiffies; | |
2063 | if (conn->recv_timeout && conn->ping_timeout) | |
2064 | mod_timer(&conn->transport_timer, | |
2065 | jiffies + (conn->recv_timeout * HZ)); | |
2066 | ||
7996a778 MC |
2067 | switch(conn->stop_stage) { |
2068 | case STOP_CONN_RECOVER: | |
2069 | /* | |
2070 | * unblock eh_abort() if it is blocked. re-try all | |
2071 | * commands after successful recovery | |
2072 | */ | |
7996a778 | 2073 | conn->stop_stage = 0; |
843c0a8a | 2074 | conn->tmf_state = TMF_INITIAL; |
7996a778 | 2075 | session->age++; |
8b1d0343 MC |
2076 | if (session->age == 16) |
2077 | session->age = 0; | |
6eabafbe | 2078 | break; |
7996a778 | 2079 | case STOP_CONN_TERM: |
7996a778 | 2080 | conn->stop_stage = 0; |
7996a778 MC |
2081 | break; |
2082 | default: | |
2083 | break; | |
2084 | } | |
2085 | spin_unlock_bh(&session->lock); | |
2086 | ||
6eabafbe MC |
2087 | iscsi_unblock_session(session_to_cls(session)); |
2088 | wake_up(&conn->ehwait); | |
7996a778 MC |
2089 | return 0; |
2090 | } | |
2091 | EXPORT_SYMBOL_GPL(iscsi_conn_start); | |
2092 | ||
2093 | static void | |
2094 | flush_control_queues(struct iscsi_session *session, struct iscsi_conn *conn) | |
2095 | { | |
2096 | struct iscsi_mgmt_task *mtask, *tmp; | |
2097 | ||
2098 | /* handle pending */ | |
843c0a8a MC |
2099 | list_for_each_entry_safe(mtask, tmp, &conn->mgmtqueue, running) { |
2100 | debug_scsi("flushing pending mgmt task itt 0x%x\n", mtask->itt); | |
b3a7ea8d | 2101 | iscsi_free_mgmt_task(conn, mtask); |
7996a778 MC |
2102 | } |
2103 | ||
2104 | /* handle running */ | |
2105 | list_for_each_entry_safe(mtask, tmp, &conn->mgmt_run_list, running) { | |
2106 | debug_scsi("flushing running mgmt task itt 0x%x\n", mtask->itt); | |
b3a7ea8d | 2107 | iscsi_free_mgmt_task(conn, mtask); |
7996a778 MC |
2108 | } |
2109 | ||
2110 | conn->mtask = NULL; | |
2111 | } | |
2112 | ||
656cffc9 MC |
2113 | static void iscsi_start_session_recovery(struct iscsi_session *session, |
2114 | struct iscsi_conn *conn, int flag) | |
7996a778 | 2115 | { |
ed2abc7f MC |
2116 | int old_stop_stage; |
2117 | ||
f6d5180c MC |
2118 | del_timer_sync(&conn->transport_timer); |
2119 | ||
6724add1 | 2120 | mutex_lock(&session->eh_mutex); |
7996a778 | 2121 | spin_lock_bh(&session->lock); |
ed2abc7f | 2122 | if (conn->stop_stage == STOP_CONN_TERM) { |
7996a778 | 2123 | spin_unlock_bh(&session->lock); |
6724add1 MC |
2124 | mutex_unlock(&session->eh_mutex); |
2125 | return; | |
2126 | } | |
2127 | ||
2128 | /* | |
2129 | * The LLD either freed/unset the lock on us, or userspace called | |
2130 | * stop but did not create a proper connection (connection was never | |
2131 | * bound or it was unbound then stop was called). | |
2132 | */ | |
2133 | if (!conn->recv_lock) { | |
2134 | spin_unlock_bh(&session->lock); | |
2135 | mutex_unlock(&session->eh_mutex); | |
7996a778 MC |
2136 | return; |
2137 | } | |
ed2abc7f MC |
2138 | |
2139 | /* | |
2140 | * When this is called for the in_login state, we only want to clean | |
67a61114 MC |
2141 | * up the login task and connection. We do not need to block and set |
2142 | * the recovery state again | |
ed2abc7f | 2143 | */ |
67a61114 MC |
2144 | if (flag == STOP_CONN_TERM) |
2145 | session->state = ISCSI_STATE_TERMINATE; | |
2146 | else if (conn->stop_stage != STOP_CONN_RECOVER) | |
2147 | session->state = ISCSI_STATE_IN_RECOVERY; | |
ed2abc7f MC |
2148 | |
2149 | old_stop_stage = conn->stop_stage; | |
7996a778 | 2150 | conn->stop_stage = flag; |
67a61114 | 2151 | conn->c_stage = ISCSI_CONN_STOPPED; |
7996a778 | 2152 | spin_unlock_bh(&session->lock); |
6724add1 MC |
2153 | |
2154 | iscsi_suspend_tx(conn); | |
7996a778 | 2155 | |
1c83469d MC |
2156 | write_lock_bh(conn->recv_lock); |
2157 | set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx); | |
2158 | write_unlock_bh(conn->recv_lock); | |
7996a778 | 2159 | |
7996a778 MC |
2160 | /* |
2161 | * for connection level recovery we should not calculate | |
2162 | * header digest. conn->hdr_size used for optimization | |
2163 | * in hdr_extract() and will be re-negotiated at | |
2164 | * set_param() time. | |
2165 | */ | |
2166 | if (flag == STOP_CONN_RECOVER) { | |
2167 | conn->hdrdgst_en = 0; | |
2168 | conn->datadgst_en = 0; | |
656cffc9 | 2169 | if (session->state == ISCSI_STATE_IN_RECOVERY && |
67a61114 MC |
2170 | old_stop_stage != STOP_CONN_RECOVER) { |
2171 | debug_scsi("blocking session\n"); | |
7996a778 | 2172 | iscsi_block_session(session_to_cls(session)); |
67a61114 | 2173 | } |
7996a778 | 2174 | } |
656cffc9 | 2175 | |
656cffc9 MC |
2176 | /* |
2177 | * flush queues. | |
2178 | */ | |
2179 | spin_lock_bh(&session->lock); | |
6eabafbe MC |
2180 | fail_all_commands(conn, -1, |
2181 | STOP_CONN_RECOVER ? DID_BUS_BUSY : DID_ERROR); | |
656cffc9 MC |
2182 | flush_control_queues(session, conn); |
2183 | spin_unlock_bh(&session->lock); | |
6724add1 | 2184 | mutex_unlock(&session->eh_mutex); |
7996a778 | 2185 | } |
7996a778 MC |
2186 | |
2187 | void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag) | |
2188 | { | |
2189 | struct iscsi_conn *conn = cls_conn->dd_data; | |
2190 | struct iscsi_session *session = conn->session; | |
2191 | ||
2192 | switch (flag) { | |
2193 | case STOP_CONN_RECOVER: | |
2194 | case STOP_CONN_TERM: | |
2195 | iscsi_start_session_recovery(session, conn, flag); | |
8d2860b3 | 2196 | break; |
7996a778 | 2197 | default: |
322d739d MC |
2198 | iscsi_conn_printk(KERN_ERR, conn, |
2199 | "invalid stop flag %d\n", flag); | |
7996a778 MC |
2200 | } |
2201 | } | |
2202 | EXPORT_SYMBOL_GPL(iscsi_conn_stop); | |
2203 | ||
2204 | int iscsi_conn_bind(struct iscsi_cls_session *cls_session, | |
2205 | struct iscsi_cls_conn *cls_conn, int is_leading) | |
2206 | { | |
2207 | struct iscsi_session *session = class_to_transport_session(cls_session); | |
98644047 | 2208 | struct iscsi_conn *conn = cls_conn->dd_data; |
7996a778 | 2209 | |
7996a778 | 2210 | spin_lock_bh(&session->lock); |
7996a778 MC |
2211 | if (is_leading) |
2212 | session->leadconn = conn; | |
98644047 | 2213 | spin_unlock_bh(&session->lock); |
7996a778 MC |
2214 | |
2215 | /* | |
2216 | * Unblock xmitworker(), Login Phase will pass through. | |
2217 | */ | |
2218 | clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx); | |
2219 | clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx); | |
2220 | return 0; | |
2221 | } | |
2222 | EXPORT_SYMBOL_GPL(iscsi_conn_bind); | |
2223 | ||
a54a52ca MC |
2224 | |
2225 | int iscsi_set_param(struct iscsi_cls_conn *cls_conn, | |
2226 | enum iscsi_param param, char *buf, int buflen) | |
2227 | { | |
2228 | struct iscsi_conn *conn = cls_conn->dd_data; | |
2229 | struct iscsi_session *session = conn->session; | |
2230 | uint32_t value; | |
2231 | ||
2232 | switch(param) { | |
843c0a8a MC |
2233 | case ISCSI_PARAM_FAST_ABORT: |
2234 | sscanf(buf, "%d", &session->fast_abort); | |
2235 | break; | |
f6d5180c MC |
2236 | case ISCSI_PARAM_ABORT_TMO: |
2237 | sscanf(buf, "%d", &session->abort_timeout); | |
2238 | break; | |
2239 | case ISCSI_PARAM_LU_RESET_TMO: | |
2240 | sscanf(buf, "%d", &session->lu_reset_timeout); | |
2241 | break; | |
2242 | case ISCSI_PARAM_PING_TMO: | |
2243 | sscanf(buf, "%d", &conn->ping_timeout); | |
2244 | break; | |
2245 | case ISCSI_PARAM_RECV_TMO: | |
2246 | sscanf(buf, "%d", &conn->recv_timeout); | |
2247 | break; | |
a54a52ca MC |
2248 | case ISCSI_PARAM_MAX_RECV_DLENGTH: |
2249 | sscanf(buf, "%d", &conn->max_recv_dlength); | |
2250 | break; | |
2251 | case ISCSI_PARAM_MAX_XMIT_DLENGTH: | |
2252 | sscanf(buf, "%d", &conn->max_xmit_dlength); | |
2253 | break; | |
2254 | case ISCSI_PARAM_HDRDGST_EN: | |
2255 | sscanf(buf, "%d", &conn->hdrdgst_en); | |
2256 | break; | |
2257 | case ISCSI_PARAM_DATADGST_EN: | |
2258 | sscanf(buf, "%d", &conn->datadgst_en); | |
2259 | break; | |
2260 | case ISCSI_PARAM_INITIAL_R2T_EN: | |
2261 | sscanf(buf, "%d", &session->initial_r2t_en); | |
2262 | break; | |
2263 | case ISCSI_PARAM_MAX_R2T: | |
2264 | sscanf(buf, "%d", &session->max_r2t); | |
2265 | break; | |
2266 | case ISCSI_PARAM_IMM_DATA_EN: | |
2267 | sscanf(buf, "%d", &session->imm_data_en); | |
2268 | break; | |
2269 | case ISCSI_PARAM_FIRST_BURST: | |
2270 | sscanf(buf, "%d", &session->first_burst); | |
2271 | break; | |
2272 | case ISCSI_PARAM_MAX_BURST: | |
2273 | sscanf(buf, "%d", &session->max_burst); | |
2274 | break; | |
2275 | case ISCSI_PARAM_PDU_INORDER_EN: | |
2276 | sscanf(buf, "%d", &session->pdu_inorder_en); | |
2277 | break; | |
2278 | case ISCSI_PARAM_DATASEQ_INORDER_EN: | |
2279 | sscanf(buf, "%d", &session->dataseq_inorder_en); | |
2280 | break; | |
2281 | case ISCSI_PARAM_ERL: | |
2282 | sscanf(buf, "%d", &session->erl); | |
2283 | break; | |
2284 | case ISCSI_PARAM_IFMARKER_EN: | |
2285 | sscanf(buf, "%d", &value); | |
2286 | BUG_ON(value); | |
2287 | break; | |
2288 | case ISCSI_PARAM_OFMARKER_EN: | |
2289 | sscanf(buf, "%d", &value); | |
2290 | BUG_ON(value); | |
2291 | break; | |
2292 | case ISCSI_PARAM_EXP_STATSN: | |
2293 | sscanf(buf, "%u", &conn->exp_statsn); | |
2294 | break; | |
b2c64167 MC |
2295 | case ISCSI_PARAM_USERNAME: |
2296 | kfree(session->username); | |
2297 | session->username = kstrdup(buf, GFP_KERNEL); | |
2298 | if (!session->username) | |
2299 | return -ENOMEM; | |
2300 | break; | |
2301 | case ISCSI_PARAM_USERNAME_IN: | |
2302 | kfree(session->username_in); | |
2303 | session->username_in = kstrdup(buf, GFP_KERNEL); | |
2304 | if (!session->username_in) | |
2305 | return -ENOMEM; | |
2306 | break; | |
2307 | case ISCSI_PARAM_PASSWORD: | |
2308 | kfree(session->password); | |
2309 | session->password = kstrdup(buf, GFP_KERNEL); | |
2310 | if (!session->password) | |
2311 | return -ENOMEM; | |
2312 | break; | |
2313 | case ISCSI_PARAM_PASSWORD_IN: | |
2314 | kfree(session->password_in); | |
2315 | session->password_in = kstrdup(buf, GFP_KERNEL); | |
2316 | if (!session->password_in) | |
2317 | return -ENOMEM; | |
2318 | break; | |
a54a52ca MC |
2319 | case ISCSI_PARAM_TARGET_NAME: |
2320 | /* this should not change between logins */ | |
2321 | if (session->targetname) | |
2322 | break; | |
2323 | ||
2324 | session->targetname = kstrdup(buf, GFP_KERNEL); | |
2325 | if (!session->targetname) | |
2326 | return -ENOMEM; | |
2327 | break; | |
2328 | case ISCSI_PARAM_TPGT: | |
2329 | sscanf(buf, "%d", &session->tpgt); | |
2330 | break; | |
2331 | case ISCSI_PARAM_PERSISTENT_PORT: | |
2332 | sscanf(buf, "%d", &conn->persistent_port); | |
2333 | break; | |
2334 | case ISCSI_PARAM_PERSISTENT_ADDRESS: | |
2335 | /* | |
2336 | * this is the address returned in discovery so it should | |
2337 | * not change between logins. | |
2338 | */ | |
2339 | if (conn->persistent_address) | |
2340 | break; | |
2341 | ||
2342 | conn->persistent_address = kstrdup(buf, GFP_KERNEL); | |
2343 | if (!conn->persistent_address) | |
2344 | return -ENOMEM; | |
2345 | break; | |
2346 | default: | |
2347 | return -ENOSYS; | |
2348 | } | |
2349 | ||
2350 | return 0; | |
2351 | } | |
2352 | EXPORT_SYMBOL_GPL(iscsi_set_param); | |
2353 | ||
2354 | int iscsi_session_get_param(struct iscsi_cls_session *cls_session, | |
2355 | enum iscsi_param param, char *buf) | |
2356 | { | |
2357 | struct Scsi_Host *shost = iscsi_session_to_shost(cls_session); | |
2358 | struct iscsi_session *session = iscsi_hostdata(shost->hostdata); | |
2359 | int len; | |
2360 | ||
2361 | switch(param) { | |
843c0a8a MC |
2362 | case ISCSI_PARAM_FAST_ABORT: |
2363 | len = sprintf(buf, "%d\n", session->fast_abort); | |
2364 | break; | |
f6d5180c MC |
2365 | case ISCSI_PARAM_ABORT_TMO: |
2366 | len = sprintf(buf, "%d\n", session->abort_timeout); | |
2367 | break; | |
2368 | case ISCSI_PARAM_LU_RESET_TMO: | |
2369 | len = sprintf(buf, "%d\n", session->lu_reset_timeout); | |
2370 | break; | |
a54a52ca MC |
2371 | case ISCSI_PARAM_INITIAL_R2T_EN: |
2372 | len = sprintf(buf, "%d\n", session->initial_r2t_en); | |
2373 | break; | |
2374 | case ISCSI_PARAM_MAX_R2T: | |
2375 | len = sprintf(buf, "%hu\n", session->max_r2t); | |
2376 | break; | |
2377 | case ISCSI_PARAM_IMM_DATA_EN: | |
2378 | len = sprintf(buf, "%d\n", session->imm_data_en); | |
2379 | break; | |
2380 | case ISCSI_PARAM_FIRST_BURST: | |
2381 | len = sprintf(buf, "%u\n", session->first_burst); | |
2382 | break; | |
2383 | case ISCSI_PARAM_MAX_BURST: | |
2384 | len = sprintf(buf, "%u\n", session->max_burst); | |
2385 | break; | |
2386 | case ISCSI_PARAM_PDU_INORDER_EN: | |
2387 | len = sprintf(buf, "%d\n", session->pdu_inorder_en); | |
2388 | break; | |
2389 | case ISCSI_PARAM_DATASEQ_INORDER_EN: | |
2390 | len = sprintf(buf, "%d\n", session->dataseq_inorder_en); | |
2391 | break; | |
2392 | case ISCSI_PARAM_ERL: | |
2393 | len = sprintf(buf, "%d\n", session->erl); | |
2394 | break; | |
2395 | case ISCSI_PARAM_TARGET_NAME: | |
2396 | len = sprintf(buf, "%s\n", session->targetname); | |
2397 | break; | |
2398 | case ISCSI_PARAM_TPGT: | |
2399 | len = sprintf(buf, "%d\n", session->tpgt); | |
2400 | break; | |
b2c64167 MC |
2401 | case ISCSI_PARAM_USERNAME: |
2402 | len = sprintf(buf, "%s\n", session->username); | |
2403 | break; | |
2404 | case ISCSI_PARAM_USERNAME_IN: | |
2405 | len = sprintf(buf, "%s\n", session->username_in); | |
2406 | break; | |
2407 | case ISCSI_PARAM_PASSWORD: | |
2408 | len = sprintf(buf, "%s\n", session->password); | |
2409 | break; | |
2410 | case ISCSI_PARAM_PASSWORD_IN: | |
2411 | len = sprintf(buf, "%s\n", session->password_in); | |
2412 | break; | |
a54a52ca MC |
2413 | default: |
2414 | return -ENOSYS; | |
2415 | } | |
2416 | ||
2417 | return len; | |
2418 | } | |
2419 | EXPORT_SYMBOL_GPL(iscsi_session_get_param); | |
2420 | ||
2421 | int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn, | |
2422 | enum iscsi_param param, char *buf) | |
2423 | { | |
2424 | struct iscsi_conn *conn = cls_conn->dd_data; | |
2425 | int len; | |
2426 | ||
2427 | switch(param) { | |
f6d5180c MC |
2428 | case ISCSI_PARAM_PING_TMO: |
2429 | len = sprintf(buf, "%u\n", conn->ping_timeout); | |
2430 | break; | |
2431 | case ISCSI_PARAM_RECV_TMO: | |
2432 | len = sprintf(buf, "%u\n", conn->recv_timeout); | |
2433 | break; | |
a54a52ca MC |
2434 | case ISCSI_PARAM_MAX_RECV_DLENGTH: |
2435 | len = sprintf(buf, "%u\n", conn->max_recv_dlength); | |
2436 | break; | |
2437 | case ISCSI_PARAM_MAX_XMIT_DLENGTH: | |
2438 | len = sprintf(buf, "%u\n", conn->max_xmit_dlength); | |
2439 | break; | |
2440 | case ISCSI_PARAM_HDRDGST_EN: | |
2441 | len = sprintf(buf, "%d\n", conn->hdrdgst_en); | |
2442 | break; | |
2443 | case ISCSI_PARAM_DATADGST_EN: | |
2444 | len = sprintf(buf, "%d\n", conn->datadgst_en); | |
2445 | break; | |
2446 | case ISCSI_PARAM_IFMARKER_EN: | |
2447 | len = sprintf(buf, "%d\n", conn->ifmarker_en); | |
2448 | break; | |
2449 | case ISCSI_PARAM_OFMARKER_EN: | |
2450 | len = sprintf(buf, "%d\n", conn->ofmarker_en); | |
2451 | break; | |
2452 | case ISCSI_PARAM_EXP_STATSN: | |
2453 | len = sprintf(buf, "%u\n", conn->exp_statsn); | |
2454 | break; | |
2455 | case ISCSI_PARAM_PERSISTENT_PORT: | |
2456 | len = sprintf(buf, "%d\n", conn->persistent_port); | |
2457 | break; | |
2458 | case ISCSI_PARAM_PERSISTENT_ADDRESS: | |
2459 | len = sprintf(buf, "%s\n", conn->persistent_address); | |
2460 | break; | |
2461 | default: | |
2462 | return -ENOSYS; | |
2463 | } | |
2464 | ||
2465 | return len; | |
2466 | } | |
2467 | EXPORT_SYMBOL_GPL(iscsi_conn_get_param); | |
2468 | ||
0801c242 MC |
2469 | int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param, |
2470 | char *buf) | |
2471 | { | |
2472 | struct iscsi_session *session = iscsi_hostdata(shost->hostdata); | |
2473 | int len; | |
2474 | ||
2475 | switch (param) { | |
d8196ed2 MC |
2476 | case ISCSI_HOST_PARAM_NETDEV_NAME: |
2477 | if (!session->netdev) | |
2478 | len = sprintf(buf, "%s\n", "default"); | |
2479 | else | |
2480 | len = sprintf(buf, "%s\n", session->netdev); | |
2481 | break; | |
0801c242 MC |
2482 | case ISCSI_HOST_PARAM_HWADDRESS: |
2483 | if (!session->hwaddress) | |
2484 | len = sprintf(buf, "%s\n", "default"); | |
2485 | else | |
2486 | len = sprintf(buf, "%s\n", session->hwaddress); | |
2487 | break; | |
8ad5781a MC |
2488 | case ISCSI_HOST_PARAM_INITIATOR_NAME: |
2489 | if (!session->initiatorname) | |
2490 | len = sprintf(buf, "%s\n", "unknown"); | |
2491 | else | |
2492 | len = sprintf(buf, "%s\n", session->initiatorname); | |
2493 | break; | |
2494 | ||
0801c242 MC |
2495 | default: |
2496 | return -ENOSYS; | |
2497 | } | |
2498 | ||
2499 | return len; | |
2500 | } | |
2501 | EXPORT_SYMBOL_GPL(iscsi_host_get_param); | |
2502 | ||
2503 | int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param, | |
2504 | char *buf, int buflen) | |
2505 | { | |
2506 | struct iscsi_session *session = iscsi_hostdata(shost->hostdata); | |
2507 | ||
2508 | switch (param) { | |
d8196ed2 MC |
2509 | case ISCSI_HOST_PARAM_NETDEV_NAME: |
2510 | if (!session->netdev) | |
2511 | session->netdev = kstrdup(buf, GFP_KERNEL); | |
2512 | break; | |
0801c242 MC |
2513 | case ISCSI_HOST_PARAM_HWADDRESS: |
2514 | if (!session->hwaddress) | |
2515 | session->hwaddress = kstrdup(buf, GFP_KERNEL); | |
2516 | break; | |
8ad5781a MC |
2517 | case ISCSI_HOST_PARAM_INITIATOR_NAME: |
2518 | if (!session->initiatorname) | |
2519 | session->initiatorname = kstrdup(buf, GFP_KERNEL); | |
2520 | break; | |
0801c242 MC |
2521 | default: |
2522 | return -ENOSYS; | |
2523 | } | |
2524 | ||
2525 | return 0; | |
2526 | } | |
2527 | EXPORT_SYMBOL_GPL(iscsi_host_set_param); | |
2528 | ||
7996a778 MC |
2529 | MODULE_AUTHOR("Mike Christie"); |
2530 | MODULE_DESCRIPTION("iSCSI library functions"); | |
2531 | MODULE_LICENSE("GPL"); |