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