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