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