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