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