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