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