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
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.
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.
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.
24 #include <linux/types.h>
25 #include <linux/kfifo.h>
26 #include <linux/delay.h>
27 #include <asm/unaligned.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>
40 static void fail_command(struct iscsi_conn
*conn
, struct iscsi_cmd_task
*ctask
,
43 struct iscsi_session
*
44 class_to_transport_session(struct iscsi_cls_session
*cls_session
)
46 struct Scsi_Host
*shost
= iscsi_session_to_shost(cls_session
);
47 return iscsi_hostdata(shost
->hostdata
);
49 EXPORT_SYMBOL_GPL(class_to_transport_session
);
51 /* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
52 #define SNA32_CHECK 2147483648UL
54 static int iscsi_sna_lt(u32 n1
, u32 n2
)
56 return n1
!= n2
&& ((n1
< n2
&& (n2
- n1
< SNA32_CHECK
)) ||
57 (n1
> n2
&& (n2
- n1
< SNA32_CHECK
)));
60 /* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
61 static int iscsi_sna_lte(u32 n1
, u32 n2
)
63 return n1
== n2
|| ((n1
< n2
&& (n2
- n1
< SNA32_CHECK
)) ||
64 (n1
> n2
&& (n2
- n1
< SNA32_CHECK
)));
68 iscsi_update_cmdsn(struct iscsi_session
*session
, struct iscsi_nopin
*hdr
)
70 uint32_t max_cmdsn
= be32_to_cpu(hdr
->max_cmdsn
);
71 uint32_t exp_cmdsn
= be32_to_cpu(hdr
->exp_cmdsn
);
74 * standard specifies this check for when to update expected and
75 * max sequence numbers
77 if (iscsi_sna_lt(max_cmdsn
, exp_cmdsn
- 1))
80 if (exp_cmdsn
!= session
->exp_cmdsn
&&
81 !iscsi_sna_lt(exp_cmdsn
, session
->exp_cmdsn
))
82 session
->exp_cmdsn
= exp_cmdsn
;
84 if (max_cmdsn
!= session
->max_cmdsn
&&
85 !iscsi_sna_lt(max_cmdsn
, session
->max_cmdsn
)) {
86 session
->max_cmdsn
= max_cmdsn
;
88 * if the window closed with IO queued, then kick the
91 if (!list_empty(&session
->leadconn
->xmitqueue
) ||
92 !list_empty(&session
->leadconn
->mgmtqueue
))
93 scsi_queue_work(session
->host
,
94 &session
->leadconn
->xmitwork
);
97 EXPORT_SYMBOL_GPL(iscsi_update_cmdsn
);
99 void iscsi_prep_unsolicit_data_pdu(struct iscsi_cmd_task
*ctask
,
100 struct iscsi_data
*hdr
)
102 struct iscsi_conn
*conn
= ctask
->conn
;
104 memset(hdr
, 0, sizeof(struct iscsi_data
));
105 hdr
->ttt
= cpu_to_be32(ISCSI_RESERVED_TAG
);
106 hdr
->datasn
= cpu_to_be32(ctask
->unsol_datasn
);
107 ctask
->unsol_datasn
++;
108 hdr
->opcode
= ISCSI_OP_SCSI_DATA_OUT
;
109 memcpy(hdr
->lun
, ctask
->hdr
->lun
, sizeof(hdr
->lun
));
111 hdr
->itt
= ctask
->hdr
->itt
;
112 hdr
->exp_statsn
= cpu_to_be32(conn
->exp_statsn
);
113 hdr
->offset
= cpu_to_be32(ctask
->unsol_offset
);
115 if (ctask
->unsol_count
> conn
->max_xmit_dlength
) {
116 hton24(hdr
->dlength
, conn
->max_xmit_dlength
);
117 ctask
->data_count
= conn
->max_xmit_dlength
;
118 ctask
->unsol_offset
+= ctask
->data_count
;
121 hton24(hdr
->dlength
, ctask
->unsol_count
);
122 ctask
->data_count
= ctask
->unsol_count
;
123 hdr
->flags
= ISCSI_FLAG_CMD_FINAL
;
126 EXPORT_SYMBOL_GPL(iscsi_prep_unsolicit_data_pdu
);
128 static int iscsi_add_hdr(struct iscsi_cmd_task
*ctask
, unsigned len
)
130 unsigned exp_len
= ctask
->hdr_len
+ len
;
132 if (exp_len
> ctask
->hdr_max
) {
137 WARN_ON(len
& (ISCSI_PAD_LEN
- 1)); /* caller must pad the AHS */
138 ctask
->hdr_len
= exp_len
;
143 * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
144 * @ctask: iscsi cmd task
146 * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
147 * fields like dlength or final based on how much data it sends
149 static int iscsi_prep_scsi_cmd_pdu(struct iscsi_cmd_task
*ctask
)
151 struct iscsi_conn
*conn
= ctask
->conn
;
152 struct iscsi_session
*session
= conn
->session
;
153 struct iscsi_cmd
*hdr
= ctask
->hdr
;
154 struct scsi_cmnd
*sc
= ctask
->sc
;
159 rc
= iscsi_add_hdr(ctask
, sizeof(*hdr
));
162 hdr
->opcode
= ISCSI_OP_SCSI_CMD
;
163 hdr
->flags
= ISCSI_ATTR_SIMPLE
;
164 int_to_scsilun(sc
->device
->lun
, (struct scsi_lun
*)hdr
->lun
);
165 hdr
->itt
= build_itt(ctask
->itt
, conn
->id
, session
->age
);
166 hdr
->data_length
= cpu_to_be32(scsi_bufflen(sc
));
167 hdr
->cmdsn
= cpu_to_be32(session
->cmdsn
);
169 hdr
->exp_statsn
= cpu_to_be32(conn
->exp_statsn
);
170 memcpy(hdr
->cdb
, sc
->cmnd
, sc
->cmd_len
);
171 if (sc
->cmd_len
< MAX_COMMAND_SIZE
)
172 memset(&hdr
->cdb
[sc
->cmd_len
], 0,
173 MAX_COMMAND_SIZE
- sc
->cmd_len
);
175 ctask
->data_count
= 0;
176 ctask
->imm_count
= 0;
177 if (sc
->sc_data_direction
== DMA_TO_DEVICE
) {
178 hdr
->flags
|= ISCSI_FLAG_CMD_WRITE
;
182 * imm_count bytes to be sent right after
185 * unsol_count bytes(as Data-Out) to be sent
186 * without R2T ack right after
189 * r2t_data_count bytes to be sent via R2T ack's
191 * pad_count bytes to be sent as zero-padding
193 ctask
->unsol_count
= 0;
194 ctask
->unsol_offset
= 0;
195 ctask
->unsol_datasn
= 0;
197 if (session
->imm_data_en
) {
198 if (scsi_bufflen(sc
) >= session
->first_burst
)
199 ctask
->imm_count
= min(session
->first_burst
,
200 conn
->max_xmit_dlength
);
202 ctask
->imm_count
= min(scsi_bufflen(sc
),
203 conn
->max_xmit_dlength
);
204 hton24(ctask
->hdr
->dlength
, ctask
->imm_count
);
206 zero_data(ctask
->hdr
->dlength
);
208 if (!session
->initial_r2t_en
) {
209 ctask
->unsol_count
= min((session
->first_burst
),
210 (scsi_bufflen(sc
))) - ctask
->imm_count
;
211 ctask
->unsol_offset
= ctask
->imm_count
;
214 if (!ctask
->unsol_count
)
215 /* No unsolicit Data-Out's */
216 ctask
->hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
218 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
219 zero_data(hdr
->dlength
);
221 if (sc
->sc_data_direction
== DMA_FROM_DEVICE
)
222 hdr
->flags
|= ISCSI_FLAG_CMD_READ
;
225 /* calculate size of additional header segments (AHSs) */
226 hdrlength
= ctask
->hdr_len
- sizeof(*hdr
);
228 WARN_ON(hdrlength
& (ISCSI_PAD_LEN
-1));
229 hdrlength
/= ISCSI_PAD_LEN
;
231 WARN_ON(hdrlength
>= 256);
232 hdr
->hlength
= hdrlength
& 0xFF;
234 conn
->scsicmd_pdus_cnt
++;
236 debug_scsi("iscsi prep [%s cid %d sc %p cdb 0x%x itt 0x%x len %d "
237 "cmdsn %d win %d]\n",
238 sc
->sc_data_direction
== DMA_TO_DEVICE
? "write" : "read",
239 conn
->id
, sc
, sc
->cmnd
[0], ctask
->itt
, scsi_bufflen(sc
),
240 session
->cmdsn
, session
->max_cmdsn
- session
->exp_cmdsn
+ 1);
245 * iscsi_complete_command - return command back to scsi-ml
246 * @ctask: iscsi cmd task
248 * Must be called with session lock.
249 * This function returns the scsi command to scsi-ml and returns
250 * the cmd task to the pool of available cmd tasks.
252 static void iscsi_complete_command(struct iscsi_cmd_task
*ctask
)
254 struct iscsi_session
*session
= ctask
->conn
->session
;
255 struct scsi_cmnd
*sc
= ctask
->sc
;
257 ctask
->state
= ISCSI_TASK_COMPLETED
;
259 /* SCSI eh reuses commands to verify us */
261 list_del_init(&ctask
->running
);
262 __kfifo_put(session
->cmdpool
.queue
, (void*)&ctask
, sizeof(void*));
266 static void __iscsi_get_ctask(struct iscsi_cmd_task
*ctask
)
268 atomic_inc(&ctask
->refcount
);
271 static void __iscsi_put_ctask(struct iscsi_cmd_task
*ctask
)
273 if (atomic_dec_and_test(&ctask
->refcount
))
274 iscsi_complete_command(ctask
);
278 * iscsi_cmd_rsp - SCSI Command Response processing
279 * @conn: iscsi connection
281 * @ctask: scsi command task
282 * @data: cmd data buffer
283 * @datalen: len of buffer
285 * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
286 * then completes the command and task.
288 static void iscsi_scsi_cmd_rsp(struct iscsi_conn
*conn
, struct iscsi_hdr
*hdr
,
289 struct iscsi_cmd_task
*ctask
, char *data
,
292 struct iscsi_cmd_rsp
*rhdr
= (struct iscsi_cmd_rsp
*)hdr
;
293 struct iscsi_session
*session
= conn
->session
;
294 struct scsi_cmnd
*sc
= ctask
->sc
;
296 iscsi_update_cmdsn(session
, (struct iscsi_nopin
*)rhdr
);
297 conn
->exp_statsn
= be32_to_cpu(rhdr
->statsn
) + 1;
299 sc
->result
= (DID_OK
<< 16) | rhdr
->cmd_status
;
301 if (rhdr
->response
!= ISCSI_STATUS_CMD_COMPLETED
) {
302 sc
->result
= DID_ERROR
<< 16;
306 if (rhdr
->cmd_status
== SAM_STAT_CHECK_CONDITION
) {
311 printk(KERN_ERR
"iscsi: Got CHECK_CONDITION but "
312 "invalid data buffer size of %d\n", datalen
);
313 sc
->result
= DID_BAD_TARGET
<< 16;
317 senselen
= be16_to_cpu(get_unaligned((__be16
*) data
));
318 if (datalen
< senselen
)
319 goto invalid_datalen
;
321 memcpy(sc
->sense_buffer
, data
+ 2,
322 min_t(uint16_t, senselen
, SCSI_SENSE_BUFFERSIZE
));
323 debug_scsi("copied %d bytes of sense\n",
324 min_t(uint16_t, senselen
, SCSI_SENSE_BUFFERSIZE
));
327 if (rhdr
->flags
& (ISCSI_FLAG_CMD_UNDERFLOW
|
328 ISCSI_FLAG_CMD_OVERFLOW
)) {
329 int res_count
= be32_to_cpu(rhdr
->residual_count
);
332 (rhdr
->flags
& ISCSI_FLAG_CMD_OVERFLOW
||
333 res_count
<= scsi_bufflen(sc
)))
334 scsi_set_resid(sc
, res_count
);
336 sc
->result
= (DID_BAD_TARGET
<< 16) | rhdr
->cmd_status
;
337 } else if (rhdr
->flags
& (ISCSI_FLAG_CMD_BIDI_UNDERFLOW
|
338 ISCSI_FLAG_CMD_BIDI_OVERFLOW
))
339 sc
->result
= (DID_BAD_TARGET
<< 16) | rhdr
->cmd_status
;
342 debug_scsi("done [sc %lx res %d itt 0x%x]\n",
343 (long)sc
, sc
->result
, ctask
->itt
);
344 conn
->scsirsp_pdus_cnt
++;
346 __iscsi_put_ctask(ctask
);
349 static void iscsi_tmf_rsp(struct iscsi_conn
*conn
, struct iscsi_hdr
*hdr
)
351 struct iscsi_tm_rsp
*tmf
= (struct iscsi_tm_rsp
*)hdr
;
353 conn
->exp_statsn
= be32_to_cpu(hdr
->statsn
) + 1;
354 conn
->tmfrsp_pdus_cnt
++;
356 if (conn
->tmf_state
!= TMF_QUEUED
)
359 if (tmf
->response
== ISCSI_TMF_RSP_COMPLETE
)
360 conn
->tmf_state
= TMF_SUCCESS
;
361 else if (tmf
->response
== ISCSI_TMF_RSP_NO_TASK
)
362 conn
->tmf_state
= TMF_NOT_FOUND
;
364 conn
->tmf_state
= TMF_FAILED
;
365 wake_up(&conn
->ehwait
);
368 static int iscsi_handle_reject(struct iscsi_conn
*conn
, struct iscsi_hdr
*hdr
,
369 char *data
, int datalen
)
371 struct iscsi_reject
*reject
= (struct iscsi_reject
*)hdr
;
372 struct iscsi_hdr rejected_pdu
;
375 conn
->exp_statsn
= be32_to_cpu(reject
->statsn
) + 1;
377 if (reject
->reason
== ISCSI_REASON_DATA_DIGEST_ERROR
) {
378 if (ntoh24(reject
->dlength
) > datalen
)
379 return ISCSI_ERR_PROTO
;
381 if (ntoh24(reject
->dlength
) >= sizeof(struct iscsi_hdr
)) {
382 memcpy(&rejected_pdu
, data
, sizeof(struct iscsi_hdr
));
383 itt
= get_itt(rejected_pdu
.itt
);
384 printk(KERN_ERR
"itt 0x%x had pdu (op 0x%x) rejected "
385 "due to DataDigest error.\n", itt
,
386 rejected_pdu
.opcode
);
393 * __iscsi_complete_pdu - complete pdu
397 * @datalen: len of data buffer
399 * Completes pdu processing by freeing any resources allocated at
400 * queuecommand or send generic. session lock must be held and verify
401 * itt must have been called.
403 int __iscsi_complete_pdu(struct iscsi_conn
*conn
, struct iscsi_hdr
*hdr
,
404 char *data
, int datalen
)
406 struct iscsi_session
*session
= conn
->session
;
407 int opcode
= hdr
->opcode
& ISCSI_OPCODE_MASK
, rc
= 0;
408 struct iscsi_cmd_task
*ctask
;
409 struct iscsi_mgmt_task
*mtask
;
412 if (hdr
->itt
!= RESERVED_ITT
)
413 itt
= get_itt(hdr
->itt
);
417 if (itt
< session
->cmds_max
) {
418 ctask
= session
->cmds
[itt
];
420 debug_scsi("cmdrsp [op 0x%x cid %d itt 0x%x len %d]\n",
421 opcode
, conn
->id
, ctask
->itt
, datalen
);
424 case ISCSI_OP_SCSI_CMD_RSP
:
425 BUG_ON((void*)ctask
!= ctask
->sc
->SCp
.ptr
);
426 iscsi_scsi_cmd_rsp(conn
, hdr
, ctask
, data
,
429 case ISCSI_OP_SCSI_DATA_IN
:
430 BUG_ON((void*)ctask
!= ctask
->sc
->SCp
.ptr
);
431 if (hdr
->flags
& ISCSI_FLAG_DATA_STATUS
) {
432 conn
->scsirsp_pdus_cnt
++;
433 __iscsi_put_ctask(ctask
);
437 /* LLD handles this for now */
440 rc
= ISCSI_ERR_BAD_OPCODE
;
443 } else if (itt
>= ISCSI_MGMT_ITT_OFFSET
&&
444 itt
< ISCSI_MGMT_ITT_OFFSET
+ session
->mgmtpool_max
) {
445 mtask
= session
->mgmt_cmds
[itt
- ISCSI_MGMT_ITT_OFFSET
];
447 debug_scsi("immrsp [op 0x%x cid %d itt 0x%x len %d]\n",
448 opcode
, conn
->id
, mtask
->itt
, datalen
);
450 iscsi_update_cmdsn(session
, (struct iscsi_nopin
*)hdr
);
452 case ISCSI_OP_LOGOUT_RSP
:
454 rc
= ISCSI_ERR_PROTO
;
457 conn
->exp_statsn
= be32_to_cpu(hdr
->statsn
) + 1;
459 case ISCSI_OP_LOGIN_RSP
:
460 case ISCSI_OP_TEXT_RSP
:
462 * login related PDU's exp_statsn is handled in
465 if (iscsi_recv_pdu(conn
->cls_conn
, hdr
, data
, datalen
))
466 rc
= ISCSI_ERR_CONN_FAILED
;
467 list_del_init(&mtask
->running
);
468 if (conn
->login_mtask
!= mtask
)
469 __kfifo_put(session
->mgmtpool
.queue
,
470 (void*)&mtask
, sizeof(void*));
472 case ISCSI_OP_SCSI_TMFUNC_RSP
:
474 rc
= ISCSI_ERR_PROTO
;
478 iscsi_tmf_rsp(conn
, hdr
);
480 case ISCSI_OP_NOOP_IN
:
481 if (hdr
->ttt
!= cpu_to_be32(ISCSI_RESERVED_TAG
) || datalen
) {
482 rc
= ISCSI_ERR_PROTO
;
485 conn
->exp_statsn
= be32_to_cpu(hdr
->statsn
) + 1;
487 if (iscsi_recv_pdu(conn
->cls_conn
, hdr
, data
, datalen
))
488 rc
= ISCSI_ERR_CONN_FAILED
;
489 list_del_init(&mtask
->running
);
490 __kfifo_put(session
->mgmtpool
.queue
,
491 (void*)&mtask
, sizeof(void*));
494 rc
= ISCSI_ERR_BAD_OPCODE
;
497 } else if (itt
== ~0U) {
498 iscsi_update_cmdsn(session
, (struct iscsi_nopin
*)hdr
);
501 case ISCSI_OP_NOOP_IN
:
503 rc
= ISCSI_ERR_PROTO
;
507 if (hdr
->ttt
== cpu_to_be32(ISCSI_RESERVED_TAG
))
510 if (iscsi_recv_pdu(conn
->cls_conn
, hdr
, NULL
, 0))
511 rc
= ISCSI_ERR_CONN_FAILED
;
513 case ISCSI_OP_REJECT
:
514 rc
= iscsi_handle_reject(conn
, hdr
, data
, datalen
);
516 case ISCSI_OP_ASYNC_EVENT
:
517 conn
->exp_statsn
= be32_to_cpu(hdr
->statsn
) + 1;
518 if (iscsi_recv_pdu(conn
->cls_conn
, hdr
, data
, datalen
))
519 rc
= ISCSI_ERR_CONN_FAILED
;
522 rc
= ISCSI_ERR_BAD_OPCODE
;
526 rc
= ISCSI_ERR_BAD_ITT
;
530 EXPORT_SYMBOL_GPL(__iscsi_complete_pdu
);
532 int iscsi_complete_pdu(struct iscsi_conn
*conn
, struct iscsi_hdr
*hdr
,
533 char *data
, int datalen
)
537 spin_lock(&conn
->session
->lock
);
538 rc
= __iscsi_complete_pdu(conn
, hdr
, data
, datalen
);
539 spin_unlock(&conn
->session
->lock
);
542 EXPORT_SYMBOL_GPL(iscsi_complete_pdu
);
544 /* verify itt (itt encoding: age+cid+itt) */
545 int iscsi_verify_itt(struct iscsi_conn
*conn
, struct iscsi_hdr
*hdr
,
548 struct iscsi_session
*session
= conn
->session
;
549 struct iscsi_cmd_task
*ctask
;
552 if (hdr
->itt
!= RESERVED_ITT
) {
553 if (((__force u32
)hdr
->itt
& ISCSI_AGE_MASK
) !=
554 (session
->age
<< ISCSI_AGE_SHIFT
)) {
555 printk(KERN_ERR
"iscsi: received itt %x expected "
556 "session age (%x)\n", (__force u32
)hdr
->itt
,
557 session
->age
& ISCSI_AGE_MASK
);
558 return ISCSI_ERR_BAD_ITT
;
561 if (((__force u32
)hdr
->itt
& ISCSI_CID_MASK
) !=
562 (conn
->id
<< ISCSI_CID_SHIFT
)) {
563 printk(KERN_ERR
"iscsi: received itt %x, expected "
564 "CID (%x)\n", (__force u32
)hdr
->itt
, conn
->id
);
565 return ISCSI_ERR_BAD_ITT
;
567 itt
= get_itt(hdr
->itt
);
571 if (itt
< session
->cmds_max
) {
572 ctask
= session
->cmds
[itt
];
575 printk(KERN_INFO
"iscsi: dropping ctask with "
576 "itt 0x%x\n", ctask
->itt
);
578 return ISCSI_ERR_NO_SCSI_CMD
;
581 if (ctask
->sc
->SCp
.phase
!= session
->age
) {
582 printk(KERN_ERR
"iscsi: ctask's session age %d, "
583 "expected %d\n", ctask
->sc
->SCp
.phase
,
585 return ISCSI_ERR_SESSION_FAILED
;
592 EXPORT_SYMBOL_GPL(iscsi_verify_itt
);
594 void iscsi_conn_failure(struct iscsi_conn
*conn
, enum iscsi_err err
)
596 struct iscsi_session
*session
= conn
->session
;
599 spin_lock_irqsave(&session
->lock
, flags
);
600 if (session
->state
== ISCSI_STATE_FAILED
) {
601 spin_unlock_irqrestore(&session
->lock
, flags
);
605 if (conn
->stop_stage
== 0)
606 session
->state
= ISCSI_STATE_FAILED
;
607 spin_unlock_irqrestore(&session
->lock
, flags
);
608 set_bit(ISCSI_SUSPEND_BIT
, &conn
->suspend_tx
);
609 set_bit(ISCSI_SUSPEND_BIT
, &conn
->suspend_rx
);
610 iscsi_conn_error(conn
->cls_conn
, err
);
612 EXPORT_SYMBOL_GPL(iscsi_conn_failure
);
614 static void iscsi_prep_mtask(struct iscsi_conn
*conn
,
615 struct iscsi_mgmt_task
*mtask
)
617 struct iscsi_session
*session
= conn
->session
;
618 struct iscsi_hdr
*hdr
= mtask
->hdr
;
619 struct iscsi_nopout
*nop
= (struct iscsi_nopout
*)hdr
;
621 if (hdr
->opcode
!= (ISCSI_OP_LOGIN
| ISCSI_OP_IMMEDIATE
) &&
622 hdr
->opcode
!= (ISCSI_OP_TEXT
| ISCSI_OP_IMMEDIATE
))
623 nop
->exp_statsn
= cpu_to_be32(conn
->exp_statsn
);
625 * pre-format CmdSN for outgoing PDU.
627 nop
->cmdsn
= cpu_to_be32(session
->cmdsn
);
628 if (hdr
->itt
!= RESERVED_ITT
) {
629 hdr
->itt
= build_itt(mtask
->itt
, conn
->id
, session
->age
);
631 * TODO: We always use immediate, so we never hit this.
632 * If we start to send tmfs or nops as non-immediate then
633 * we should start checking the cmdsn numbers for mgmt tasks.
635 if (conn
->c_stage
== ISCSI_CONN_STARTED
&&
636 !(hdr
->opcode
& ISCSI_OP_IMMEDIATE
)) {
637 session
->queued_cmdsn
++;
642 if (session
->tt
->init_mgmt_task
)
643 session
->tt
->init_mgmt_task(conn
, mtask
);
645 debug_scsi("mgmtpdu [op 0x%x hdr->itt 0x%x datalen %d]\n",
646 hdr
->opcode
& ISCSI_OPCODE_MASK
, hdr
->itt
,
650 static int iscsi_xmit_mtask(struct iscsi_conn
*conn
)
652 struct iscsi_hdr
*hdr
= conn
->mtask
->hdr
;
653 int rc
, was_logout
= 0;
655 spin_unlock_bh(&conn
->session
->lock
);
656 if ((hdr
->opcode
& ISCSI_OPCODE_MASK
) == ISCSI_OP_LOGOUT
) {
657 conn
->session
->state
= ISCSI_STATE_IN_RECOVERY
;
658 iscsi_block_session(session_to_cls(conn
->session
));
661 rc
= conn
->session
->tt
->xmit_mgmt_task(conn
, conn
->mtask
);
662 spin_lock_bh(&conn
->session
->lock
);
666 /* done with this in-progress mtask */
670 set_bit(ISCSI_SUSPEND_BIT
, &conn
->suspend_tx
);
676 static int iscsi_check_cmdsn_window_closed(struct iscsi_conn
*conn
)
678 struct iscsi_session
*session
= conn
->session
;
681 * Check for iSCSI window and take care of CmdSN wrap-around
683 if (!iscsi_sna_lte(session
->queued_cmdsn
, session
->max_cmdsn
)) {
684 debug_scsi("iSCSI CmdSN closed. ExpCmdSn %u MaxCmdSN %u "
685 "CmdSN %u/%u\n", session
->exp_cmdsn
,
686 session
->max_cmdsn
, session
->cmdsn
,
687 session
->queued_cmdsn
);
693 static int iscsi_xmit_ctask(struct iscsi_conn
*conn
)
695 struct iscsi_cmd_task
*ctask
= conn
->ctask
;
698 __iscsi_get_ctask(ctask
);
699 spin_unlock_bh(&conn
->session
->lock
);
700 rc
= conn
->session
->tt
->xmit_cmd_task(conn
, ctask
);
701 spin_lock_bh(&conn
->session
->lock
);
702 __iscsi_put_ctask(ctask
);
704 /* done with this ctask */
710 * iscsi_requeue_ctask - requeue ctask to run from session workqueue
711 * @ctask: ctask to requeue
713 * LLDs that need to run a ctask from the session workqueue should call
714 * this. The session lock must be held.
716 void iscsi_requeue_ctask(struct iscsi_cmd_task
*ctask
)
718 struct iscsi_conn
*conn
= ctask
->conn
;
720 list_move_tail(&ctask
->running
, &conn
->requeue
);
721 scsi_queue_work(conn
->session
->host
, &conn
->xmitwork
);
723 EXPORT_SYMBOL_GPL(iscsi_requeue_ctask
);
726 * iscsi_data_xmit - xmit any command into the scheduled connection
727 * @conn: iscsi connection
730 * The function can return -EAGAIN in which case the caller must
731 * re-schedule it again later or recover. '0' return code means
734 static int iscsi_data_xmit(struct iscsi_conn
*conn
)
738 spin_lock_bh(&conn
->session
->lock
);
739 if (unlikely(conn
->suspend_tx
)) {
740 debug_scsi("conn %d Tx suspended!\n", conn
->id
);
741 spin_unlock_bh(&conn
->session
->lock
);
746 rc
= iscsi_xmit_ctask(conn
);
752 rc
= iscsi_xmit_mtask(conn
);
758 * process mgmt pdus like nops before commands since we should
759 * only have one nop-out as a ping from us and targets should not
760 * overflow us with nop-ins
763 while (!list_empty(&conn
->mgmtqueue
)) {
764 conn
->mtask
= list_entry(conn
->mgmtqueue
.next
,
765 struct iscsi_mgmt_task
, running
);
766 iscsi_prep_mtask(conn
, conn
->mtask
);
767 list_move_tail(conn
->mgmtqueue
.next
, &conn
->mgmt_run_list
);
768 rc
= iscsi_xmit_mtask(conn
);
773 /* process pending command queue */
774 while (!list_empty(&conn
->xmitqueue
)) {
775 if (conn
->tmf_state
== TMF_QUEUED
)
778 conn
->ctask
= list_entry(conn
->xmitqueue
.next
,
779 struct iscsi_cmd_task
, running
);
780 if (iscsi_prep_scsi_cmd_pdu(conn
->ctask
)) {
781 fail_command(conn
, conn
->ctask
, DID_ABORT
<< 16);
784 conn
->session
->tt
->init_cmd_task(conn
->ctask
);
785 conn
->ctask
->state
= ISCSI_TASK_RUNNING
;
786 list_move_tail(conn
->xmitqueue
.next
, &conn
->run_list
);
787 rc
= iscsi_xmit_ctask(conn
);
791 * we could continuously get new ctask requests so
792 * we need to check the mgmt queue for nops that need to
793 * be sent to aviod starvation
795 if (!list_empty(&conn
->mgmtqueue
))
799 while (!list_empty(&conn
->requeue
)) {
800 if (conn
->session
->fast_abort
&& conn
->tmf_state
!= TMF_INITIAL
)
803 conn
->ctask
= list_entry(conn
->requeue
.next
,
804 struct iscsi_cmd_task
, running
);
805 conn
->ctask
->state
= ISCSI_TASK_RUNNING
;
806 list_move_tail(conn
->requeue
.next
, &conn
->run_list
);
807 rc
= iscsi_xmit_ctask(conn
);
810 if (!list_empty(&conn
->mgmtqueue
))
813 spin_unlock_bh(&conn
->session
->lock
);
817 if (unlikely(conn
->suspend_tx
))
819 spin_unlock_bh(&conn
->session
->lock
);
823 static void iscsi_xmitworker(struct work_struct
*work
)
825 struct iscsi_conn
*conn
=
826 container_of(work
, struct iscsi_conn
, xmitwork
);
829 * serialize Xmit worker on a per-connection basis.
832 rc
= iscsi_data_xmit(conn
);
833 } while (rc
>= 0 || rc
== -EAGAIN
);
837 FAILURE_BAD_HOST
= 1,
838 FAILURE_SESSION_FAILED
,
839 FAILURE_SESSION_FREED
,
840 FAILURE_WINDOW_CLOSED
,
842 FAILURE_SESSION_TERMINATE
,
843 FAILURE_SESSION_IN_RECOVERY
,
844 FAILURE_SESSION_RECOVERY_TIMEOUT
,
847 int iscsi_queuecommand(struct scsi_cmnd
*sc
, void (*done
)(struct scsi_cmnd
*))
849 struct Scsi_Host
*host
;
851 struct iscsi_session
*session
;
852 struct iscsi_conn
*conn
;
853 struct iscsi_cmd_task
*ctask
= NULL
;
855 sc
->scsi_done
= done
;
859 host
= sc
->device
->host
;
860 session
= iscsi_hostdata(host
->hostdata
);
862 spin_lock(&session
->lock
);
865 * ISCSI_STATE_FAILED is a temp. state. The recovery
866 * code will decide what is best to do with command queued
869 if (session
->state
!= ISCSI_STATE_LOGGED_IN
&&
870 session
->state
!= ISCSI_STATE_FAILED
) {
872 * to handle the race between when we set the recovery state
873 * and block the session we requeue here (commands could
874 * be entering our queuecommand while a block is starting
875 * up because the block code is not locked)
877 if (session
->state
== ISCSI_STATE_IN_RECOVERY
) {
878 reason
= FAILURE_SESSION_IN_RECOVERY
;
882 if (session
->state
== ISCSI_STATE_RECOVERY_FAILED
)
883 reason
= FAILURE_SESSION_RECOVERY_TIMEOUT
;
884 else if (session
->state
== ISCSI_STATE_TERMINATE
)
885 reason
= FAILURE_SESSION_TERMINATE
;
887 reason
= FAILURE_SESSION_FREED
;
891 conn
= session
->leadconn
;
893 reason
= FAILURE_SESSION_FREED
;
897 if (iscsi_check_cmdsn_window_closed(conn
)) {
898 reason
= FAILURE_WINDOW_CLOSED
;
902 if (!__kfifo_get(session
->cmdpool
.queue
, (void*)&ctask
,
904 reason
= FAILURE_OOM
;
907 session
->queued_cmdsn
++;
909 sc
->SCp
.phase
= session
->age
;
910 sc
->SCp
.ptr
= (char *)ctask
;
912 atomic_set(&ctask
->refcount
, 1);
913 ctask
->state
= ISCSI_TASK_PENDING
;
916 INIT_LIST_HEAD(&ctask
->running
);
918 list_add_tail(&ctask
->running
, &conn
->xmitqueue
);
919 spin_unlock(&session
->lock
);
921 scsi_queue_work(host
, &conn
->xmitwork
);
925 spin_unlock(&session
->lock
);
926 debug_scsi("cmd 0x%x rejected (%d)\n", sc
->cmnd
[0], reason
);
927 return SCSI_MLQUEUE_HOST_BUSY
;
930 spin_unlock(&session
->lock
);
931 printk(KERN_ERR
"iscsi: cmd 0x%x is not queued (%d)\n",
932 sc
->cmnd
[0], reason
);
933 sc
->result
= (DID_NO_CONNECT
<< 16);
934 scsi_set_resid(sc
, scsi_bufflen(sc
));
938 EXPORT_SYMBOL_GPL(iscsi_queuecommand
);
940 int iscsi_change_queue_depth(struct scsi_device
*sdev
, int depth
)
942 if (depth
> ISCSI_MAX_CMD_PER_LUN
)
943 depth
= ISCSI_MAX_CMD_PER_LUN
;
944 scsi_adjust_queue_depth(sdev
, scsi_get_tag_type(sdev
), depth
);
945 return sdev
->queue_depth
;
947 EXPORT_SYMBOL_GPL(iscsi_change_queue_depth
);
949 static struct iscsi_mgmt_task
*
950 __iscsi_conn_send_pdu(struct iscsi_conn
*conn
, struct iscsi_hdr
*hdr
,
951 char *data
, uint32_t data_size
)
953 struct iscsi_session
*session
= conn
->session
;
954 struct iscsi_mgmt_task
*mtask
;
956 if (session
->state
== ISCSI_STATE_TERMINATE
)
959 if (hdr
->opcode
== (ISCSI_OP_LOGIN
| ISCSI_OP_IMMEDIATE
) ||
960 hdr
->opcode
== (ISCSI_OP_TEXT
| ISCSI_OP_IMMEDIATE
))
962 * Login and Text are sent serially, in
963 * request-followed-by-response sequence.
964 * Same mtask can be used. Same ITT must be used.
965 * Note that login_mtask is preallocated at conn_create().
967 mtask
= conn
->login_mtask
;
969 BUG_ON(conn
->c_stage
== ISCSI_CONN_INITIAL_STAGE
);
970 BUG_ON(conn
->c_stage
== ISCSI_CONN_STOPPED
);
972 if (!__kfifo_get(session
->mgmtpool
.queue
,
973 (void*)&mtask
, sizeof(void*)))
978 memcpy(mtask
->data
, data
, data_size
);
979 mtask
->data_count
= data_size
;
981 mtask
->data_count
= 0;
983 memcpy(mtask
->hdr
, hdr
, sizeof(struct iscsi_hdr
));
984 INIT_LIST_HEAD(&mtask
->running
);
985 list_add_tail(&mtask
->running
, &conn
->mgmtqueue
);
989 int iscsi_conn_send_pdu(struct iscsi_cls_conn
*cls_conn
, struct iscsi_hdr
*hdr
,
990 char *data
, uint32_t data_size
)
992 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
993 struct iscsi_session
*session
= conn
->session
;
996 spin_lock_bh(&session
->lock
);
997 if (!__iscsi_conn_send_pdu(conn
, hdr
, data
, data_size
))
999 spin_unlock_bh(&session
->lock
);
1000 scsi_queue_work(session
->host
, &conn
->xmitwork
);
1003 EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu
);
1005 void iscsi_session_recovery_timedout(struct iscsi_cls_session
*cls_session
)
1007 struct iscsi_session
*session
= class_to_transport_session(cls_session
);
1009 spin_lock_bh(&session
->lock
);
1010 if (session
->state
!= ISCSI_STATE_LOGGED_IN
) {
1011 session
->state
= ISCSI_STATE_RECOVERY_FAILED
;
1012 if (session
->leadconn
)
1013 wake_up(&session
->leadconn
->ehwait
);
1015 spin_unlock_bh(&session
->lock
);
1017 EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout
);
1019 int iscsi_eh_host_reset(struct scsi_cmnd
*sc
)
1021 struct Scsi_Host
*host
= sc
->device
->host
;
1022 struct iscsi_session
*session
= iscsi_hostdata(host
->hostdata
);
1023 struct iscsi_conn
*conn
= session
->leadconn
;
1025 spin_lock_bh(&session
->lock
);
1026 if (session
->state
== ISCSI_STATE_TERMINATE
) {
1028 debug_scsi("failing host reset: session terminated "
1029 "[CID %d age %d]\n", conn
->id
, session
->age
);
1030 spin_unlock_bh(&session
->lock
);
1034 spin_unlock_bh(&session
->lock
);
1037 * we drop the lock here but the leadconn cannot be destoyed while
1038 * we are in the scsi eh
1040 iscsi_conn_failure(conn
, ISCSI_ERR_CONN_FAILED
);
1042 debug_scsi("iscsi_eh_host_reset wait for relogin\n");
1043 wait_event_interruptible(conn
->ehwait
,
1044 session
->state
== ISCSI_STATE_TERMINATE
||
1045 session
->state
== ISCSI_STATE_LOGGED_IN
||
1046 session
->state
== ISCSI_STATE_RECOVERY_FAILED
);
1047 if (signal_pending(current
))
1048 flush_signals(current
);
1050 spin_lock_bh(&session
->lock
);
1051 if (session
->state
== ISCSI_STATE_LOGGED_IN
)
1052 printk(KERN_INFO
"iscsi: host reset succeeded\n");
1055 spin_unlock_bh(&session
->lock
);
1059 EXPORT_SYMBOL_GPL(iscsi_eh_host_reset
);
1061 static void iscsi_tmf_timedout(unsigned long data
)
1063 struct iscsi_conn
*conn
= (struct iscsi_conn
*)data
;
1064 struct iscsi_session
*session
= conn
->session
;
1066 spin_lock(&session
->lock
);
1067 if (conn
->tmf_state
== TMF_QUEUED
) {
1068 conn
->tmf_state
= TMF_TIMEDOUT
;
1069 debug_scsi("tmf timedout\n");
1070 /* unblock eh_abort() */
1071 wake_up(&conn
->ehwait
);
1073 spin_unlock(&session
->lock
);
1076 static int iscsi_exec_task_mgmt_fn(struct iscsi_conn
*conn
,
1077 struct iscsi_tm
*hdr
, int age
)
1079 struct iscsi_session
*session
= conn
->session
;
1080 struct iscsi_mgmt_task
*mtask
;
1082 mtask
= __iscsi_conn_send_pdu(conn
, (struct iscsi_hdr
*)hdr
,
1085 spin_unlock_bh(&session
->lock
);
1086 iscsi_conn_failure(conn
, ISCSI_ERR_CONN_FAILED
);
1087 spin_lock_bh(&session
->lock
);
1088 debug_scsi("tmf exec failure\n");
1091 conn
->tmfcmd_pdus_cnt
++;
1092 conn
->tmf_timer
.expires
= 30 * HZ
+ jiffies
;
1093 conn
->tmf_timer
.function
= iscsi_tmf_timedout
;
1094 conn
->tmf_timer
.data
= (unsigned long)conn
;
1095 add_timer(&conn
->tmf_timer
);
1096 debug_scsi("tmf set timeout\n");
1098 spin_unlock_bh(&session
->lock
);
1099 mutex_unlock(&session
->eh_mutex
);
1100 scsi_queue_work(session
->host
, &conn
->xmitwork
);
1103 * block eh thread until:
1107 * 3) session is terminated or restarted or userspace has
1108 * given up on recovery
1110 wait_event_interruptible(conn
->ehwait
, age
!= session
->age
||
1111 session
->state
!= ISCSI_STATE_LOGGED_IN
||
1112 conn
->tmf_state
!= TMF_QUEUED
);
1113 if (signal_pending(current
))
1114 flush_signals(current
);
1115 del_timer_sync(&conn
->tmf_timer
);
1117 mutex_lock(&session
->eh_mutex
);
1118 spin_lock_bh(&session
->lock
);
1119 /* if the session drops it will clean up the mtask */
1120 if (age
!= session
->age
||
1121 session
->state
!= ISCSI_STATE_LOGGED_IN
)
1124 if (!list_empty(&mtask
->running
)) {
1125 list_del_init(&mtask
->running
);
1126 __kfifo_put(session
->mgmtpool
.queue
, (void*)&mtask
,
1133 * session lock must be held
1135 static void fail_command(struct iscsi_conn
*conn
, struct iscsi_cmd_task
*ctask
,
1138 struct scsi_cmnd
*sc
;
1144 if (ctask
->state
== ISCSI_TASK_PENDING
)
1146 * cmd never made it to the xmit thread, so we should not count
1147 * the cmd in the sequencing
1149 conn
->session
->queued_cmdsn
--;
1151 conn
->session
->tt
->cleanup_cmd_task(conn
, ctask
);
1154 scsi_set_resid(sc
, scsi_bufflen(sc
));
1155 if (conn
->ctask
== ctask
)
1157 /* release ref from queuecommand */
1158 __iscsi_put_ctask(ctask
);
1162 * Fail commands. session lock held and recv side suspended and xmit
1165 static void fail_all_commands(struct iscsi_conn
*conn
, unsigned lun
)
1167 struct iscsi_cmd_task
*ctask
, *tmp
;
1169 if (conn
->ctask
&& (conn
->ctask
->sc
->device
->lun
== lun
|| lun
== -1))
1173 list_for_each_entry_safe(ctask
, tmp
, &conn
->xmitqueue
, running
) {
1174 if (lun
== ctask
->sc
->device
->lun
|| lun
== -1) {
1175 debug_scsi("failing pending sc %p itt 0x%x\n",
1176 ctask
->sc
, ctask
->itt
);
1177 fail_command(conn
, ctask
, DID_BUS_BUSY
<< 16);
1181 list_for_each_entry_safe(ctask
, tmp
, &conn
->requeue
, running
) {
1182 if (lun
== ctask
->sc
->device
->lun
|| lun
== -1) {
1183 debug_scsi("failing requeued sc %p itt 0x%x\n",
1184 ctask
->sc
, ctask
->itt
);
1185 fail_command(conn
, ctask
, DID_BUS_BUSY
<< 16);
1189 /* fail all other running */
1190 list_for_each_entry_safe(ctask
, tmp
, &conn
->run_list
, running
) {
1191 if (lun
== ctask
->sc
->device
->lun
|| lun
== -1) {
1192 debug_scsi("failing in progress sc %p itt 0x%x\n",
1193 ctask
->sc
, ctask
->itt
);
1194 fail_command(conn
, ctask
, DID_BUS_BUSY
<< 16);
1199 static void iscsi_suspend_tx(struct iscsi_conn
*conn
)
1201 set_bit(ISCSI_SUSPEND_BIT
, &conn
->suspend_tx
);
1202 scsi_flush_work(conn
->session
->host
);
1205 static void iscsi_start_tx(struct iscsi_conn
*conn
)
1207 clear_bit(ISCSI_SUSPEND_BIT
, &conn
->suspend_tx
);
1208 scsi_queue_work(conn
->session
->host
, &conn
->xmitwork
);
1211 static void iscsi_prep_abort_task_pdu(struct iscsi_cmd_task
*ctask
,
1212 struct iscsi_tm
*hdr
)
1214 memset(hdr
, 0, sizeof(*hdr
));
1215 hdr
->opcode
= ISCSI_OP_SCSI_TMFUNC
| ISCSI_OP_IMMEDIATE
;
1216 hdr
->flags
= ISCSI_TM_FUNC_ABORT_TASK
& ISCSI_FLAG_TM_FUNC_MASK
;
1217 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
1218 memcpy(hdr
->lun
, ctask
->hdr
->lun
, sizeof(hdr
->lun
));
1219 hdr
->rtt
= ctask
->hdr
->itt
;
1220 hdr
->refcmdsn
= ctask
->hdr
->cmdsn
;
1223 int iscsi_eh_abort(struct scsi_cmnd
*sc
)
1225 struct Scsi_Host
*host
= sc
->device
->host
;
1226 struct iscsi_session
*session
= iscsi_hostdata(host
->hostdata
);
1227 struct iscsi_conn
*conn
;
1228 struct iscsi_cmd_task
*ctask
;
1229 struct iscsi_tm
*hdr
;
1232 mutex_lock(&session
->eh_mutex
);
1233 spin_lock_bh(&session
->lock
);
1235 * if session was ISCSI_STATE_IN_RECOVERY then we may not have
1239 debug_scsi("sc never reached iscsi layer or it completed.\n");
1240 spin_unlock_bh(&session
->lock
);
1241 mutex_unlock(&session
->eh_mutex
);
1246 * If we are not logged in or we have started a new session
1247 * then let the host reset code handle this
1249 if (!session
->leadconn
|| session
->state
!= ISCSI_STATE_LOGGED_IN
||
1250 sc
->SCp
.phase
!= session
->age
) {
1251 spin_unlock_bh(&session
->lock
);
1252 mutex_unlock(&session
->eh_mutex
);
1256 conn
= session
->leadconn
;
1257 conn
->eh_abort_cnt
++;
1260 ctask
= (struct iscsi_cmd_task
*)sc
->SCp
.ptr
;
1261 debug_scsi("aborting [sc %p itt 0x%x]\n", sc
, ctask
->itt
);
1263 /* ctask completed before time out */
1265 debug_scsi("sc completed while abort in progress\n");
1269 if (ctask
->state
== ISCSI_TASK_PENDING
) {
1270 fail_command(conn
, ctask
, DID_ABORT
<< 16);
1274 /* only have one tmf outstanding at a time */
1275 if (conn
->tmf_state
!= TMF_INITIAL
)
1277 conn
->tmf_state
= TMF_QUEUED
;
1280 iscsi_prep_abort_task_pdu(ctask
, hdr
);
1282 if (iscsi_exec_task_mgmt_fn(conn
, hdr
, age
)) {
1287 switch (conn
->tmf_state
) {
1289 spin_unlock_bh(&session
->lock
);
1290 iscsi_suspend_tx(conn
);
1292 * clean up task if aborted. grab the recv lock as a writer
1294 write_lock_bh(conn
->recv_lock
);
1295 spin_lock(&session
->lock
);
1296 fail_command(conn
, ctask
, DID_ABORT
<< 16);
1297 conn
->tmf_state
= TMF_INITIAL
;
1298 spin_unlock(&session
->lock
);
1299 write_unlock_bh(conn
->recv_lock
);
1300 iscsi_start_tx(conn
);
1301 goto success_unlocked
;
1303 spin_unlock_bh(&session
->lock
);
1304 iscsi_conn_failure(conn
, ISCSI_ERR_CONN_FAILED
);
1305 goto failed_unlocked
;
1308 conn
->tmf_state
= TMF_INITIAL
;
1309 /* ctask completed before tmf abort response */
1310 debug_scsi("sc completed while abort in progress\n");
1315 conn
->tmf_state
= TMF_INITIAL
;
1320 spin_unlock_bh(&session
->lock
);
1322 debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc
, ctask
->itt
);
1323 mutex_unlock(&session
->eh_mutex
);
1327 spin_unlock_bh(&session
->lock
);
1329 debug_scsi("abort failed [sc %p itt 0x%x]\n", sc
,
1330 ctask
? ctask
->itt
: 0);
1331 mutex_unlock(&session
->eh_mutex
);
1334 EXPORT_SYMBOL_GPL(iscsi_eh_abort
);
1336 static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd
*sc
, struct iscsi_tm
*hdr
)
1338 memset(hdr
, 0, sizeof(*hdr
));
1339 hdr
->opcode
= ISCSI_OP_SCSI_TMFUNC
| ISCSI_OP_IMMEDIATE
;
1340 hdr
->flags
= ISCSI_TM_FUNC_LOGICAL_UNIT_RESET
& ISCSI_FLAG_TM_FUNC_MASK
;
1341 hdr
->flags
|= ISCSI_FLAG_CMD_FINAL
;
1342 int_to_scsilun(sc
->device
->lun
, (struct scsi_lun
*)hdr
->lun
);
1343 hdr
->rtt
= ISCSI_RESERVED_TAG
;
1346 int iscsi_eh_device_reset(struct scsi_cmnd
*sc
)
1348 struct Scsi_Host
*host
= sc
->device
->host
;
1349 struct iscsi_session
*session
= iscsi_hostdata(host
->hostdata
);
1350 struct iscsi_conn
*conn
;
1351 struct iscsi_tm
*hdr
;
1354 debug_scsi("LU Reset [sc %p lun %u]\n", sc
, sc
->device
->lun
);
1356 mutex_lock(&session
->eh_mutex
);
1357 spin_lock_bh(&session
->lock
);
1359 * Just check if we are not logged in. We cannot check for
1360 * the phase because the reset could come from a ioctl.
1362 if (!session
->leadconn
|| session
->state
!= ISCSI_STATE_LOGGED_IN
)
1364 conn
= session
->leadconn
;
1366 /* only have one tmf outstanding at a time */
1367 if (conn
->tmf_state
!= TMF_INITIAL
)
1369 conn
->tmf_state
= TMF_QUEUED
;
1372 iscsi_prep_lun_reset_pdu(sc
, hdr
);
1374 if (iscsi_exec_task_mgmt_fn(conn
, hdr
, session
->age
)) {
1379 switch (conn
->tmf_state
) {
1383 spin_unlock_bh(&session
->lock
);
1384 iscsi_conn_failure(conn
, ISCSI_ERR_CONN_FAILED
);
1387 conn
->tmf_state
= TMF_INITIAL
;
1392 spin_unlock_bh(&session
->lock
);
1394 iscsi_suspend_tx(conn
);
1395 /* need to grab the recv lock then session lock */
1396 write_lock_bh(conn
->recv_lock
);
1397 spin_lock(&session
->lock
);
1398 fail_all_commands(conn
, sc
->device
->lun
);
1399 conn
->tmf_state
= TMF_INITIAL
;
1400 spin_unlock(&session
->lock
);
1401 write_unlock_bh(conn
->recv_lock
);
1403 iscsi_start_tx(conn
);
1407 spin_unlock_bh(&session
->lock
);
1409 debug_scsi("iscsi_eh_device_reset %s\n",
1410 rc
== SUCCESS
? "SUCCESS" : "FAILED");
1411 mutex_unlock(&session
->eh_mutex
);
1414 EXPORT_SYMBOL_GPL(iscsi_eh_device_reset
);
1417 iscsi_pool_init(struct iscsi_queue
*q
, int max
, void ***items
, int item_size
)
1421 *items
= kmalloc(max
* sizeof(void*), GFP_KERNEL
);
1426 q
->pool
= kmalloc(max
* sizeof(void*), GFP_KERNEL
);
1427 if (q
->pool
== NULL
) {
1432 q
->queue
= kfifo_init((void*)q
->pool
, max
* sizeof(void*),
1434 if (q
->queue
== ERR_PTR(-ENOMEM
)) {
1440 for (i
= 0; i
< max
; i
++) {
1441 q
->pool
[i
] = kmalloc(item_size
, GFP_KERNEL
);
1442 if (q
->pool
[i
] == NULL
) {
1445 for (j
= 0; j
< i
; j
++)
1448 kfifo_free(q
->queue
);
1453 memset(q
->pool
[i
], 0, item_size
);
1454 (*items
)[i
] = q
->pool
[i
];
1455 __kfifo_put(q
->queue
, (void*)&q
->pool
[i
], sizeof(void*));
1459 EXPORT_SYMBOL_GPL(iscsi_pool_init
);
1461 void iscsi_pool_free(struct iscsi_queue
*q
, void **items
)
1465 for (i
= 0; i
< q
->max
; i
++)
1470 EXPORT_SYMBOL_GPL(iscsi_pool_free
);
1473 * iSCSI Session's hostdata organization:
1475 * *------------------* <== hostdata_session(host->hostdata)
1476 * | ptr to class sess|
1477 * |------------------| <== iscsi_hostdata(host->hostdata)
1479 * *------------------*
1482 #define hostdata_privsize(_sz) (sizeof(unsigned long) + _sz + \
1483 _sz % sizeof(unsigned long))
1485 #define hostdata_session(_hostdata) (iscsi_ptr(*(unsigned long *)_hostdata))
1488 * iscsi_session_setup - create iscsi cls session and host and session
1489 * @scsit: scsi transport template
1490 * @iscsit: iscsi transport template
1491 * @cmds_max: scsi host can queue
1492 * @qdepth: scsi host cmds per lun
1493 * @cmd_task_size: LLD ctask private data size
1494 * @mgmt_task_size: LLD mtask private data size
1495 * @initial_cmdsn: initial CmdSN
1496 * @hostno: host no allocated
1498 * This can be used by software iscsi_transports that allocate
1499 * a session per scsi host.
1501 struct iscsi_cls_session
*
1502 iscsi_session_setup(struct iscsi_transport
*iscsit
,
1503 struct scsi_transport_template
*scsit
,
1504 uint16_t cmds_max
, uint16_t qdepth
,
1505 int cmd_task_size
, int mgmt_task_size
,
1506 uint32_t initial_cmdsn
, uint32_t *hostno
)
1508 struct Scsi_Host
*shost
;
1509 struct iscsi_session
*session
;
1510 struct iscsi_cls_session
*cls_session
;
1513 if (qdepth
> ISCSI_MAX_CMD_PER_LUN
|| qdepth
< 1) {
1515 printk(KERN_ERR
"iscsi: invalid queue depth of %d. "
1516 "Queue depth must be between 1 and %d.\n",
1517 qdepth
, ISCSI_MAX_CMD_PER_LUN
);
1518 qdepth
= ISCSI_DEF_CMD_PER_LUN
;
1521 if (cmds_max
< 2 || (cmds_max
& (cmds_max
- 1)) ||
1522 cmds_max
>= ISCSI_MGMT_ITT_OFFSET
) {
1524 printk(KERN_ERR
"iscsi: invalid can_queue of %d. "
1525 "can_queue must be a power of 2 and between "
1526 "2 and %d - setting to %d.\n", cmds_max
,
1527 ISCSI_MGMT_ITT_OFFSET
, ISCSI_DEF_XMIT_CMDS_MAX
);
1528 cmds_max
= ISCSI_DEF_XMIT_CMDS_MAX
;
1531 shost
= scsi_host_alloc(iscsit
->host_template
,
1532 hostdata_privsize(sizeof(*session
)));
1536 /* the iscsi layer takes one task for reserve */
1537 shost
->can_queue
= cmds_max
- 1;
1538 shost
->cmd_per_lun
= qdepth
;
1540 shost
->max_channel
= 0;
1541 shost
->max_lun
= iscsit
->max_lun
;
1542 shost
->max_cmd_len
= iscsit
->max_cmd_len
;
1543 shost
->transportt
= scsit
;
1544 shost
->transportt
->create_work_queue
= 1;
1545 *hostno
= shost
->host_no
;
1547 session
= iscsi_hostdata(shost
->hostdata
);
1548 memset(session
, 0, sizeof(struct iscsi_session
));
1549 session
->host
= shost
;
1550 session
->state
= ISCSI_STATE_FREE
;
1551 session
->mgmtpool_max
= ISCSI_MGMT_CMDS_MAX
;
1552 session
->cmds_max
= cmds_max
;
1553 session
->queued_cmdsn
= session
->cmdsn
= initial_cmdsn
;
1554 session
->exp_cmdsn
= initial_cmdsn
+ 1;
1555 session
->max_cmdsn
= initial_cmdsn
+ 1;
1556 session
->max_r2t
= 1;
1557 session
->tt
= iscsit
;
1558 mutex_init(&session
->eh_mutex
);
1560 /* initialize SCSI PDU commands pool */
1561 if (iscsi_pool_init(&session
->cmdpool
, session
->cmds_max
,
1562 (void***)&session
->cmds
,
1563 cmd_task_size
+ sizeof(struct iscsi_cmd_task
)))
1564 goto cmdpool_alloc_fail
;
1566 /* pre-format cmds pool with ITT */
1567 for (cmd_i
= 0; cmd_i
< session
->cmds_max
; cmd_i
++) {
1568 struct iscsi_cmd_task
*ctask
= session
->cmds
[cmd_i
];
1571 ctask
->dd_data
= &ctask
[1];
1573 ctask
->hdr_max
= sizeof(struct iscsi_cmd
);
1574 INIT_LIST_HEAD(&ctask
->running
);
1577 spin_lock_init(&session
->lock
);
1579 /* initialize immediate command pool */
1580 if (iscsi_pool_init(&session
->mgmtpool
, session
->mgmtpool_max
,
1581 (void***)&session
->mgmt_cmds
,
1582 mgmt_task_size
+ sizeof(struct iscsi_mgmt_task
)))
1583 goto mgmtpool_alloc_fail
;
1586 /* pre-format immediate cmds pool with ITT */
1587 for (cmd_i
= 0; cmd_i
< session
->mgmtpool_max
; cmd_i
++) {
1588 struct iscsi_mgmt_task
*mtask
= session
->mgmt_cmds
[cmd_i
];
1591 mtask
->dd_data
= &mtask
[1];
1592 mtask
->itt
= ISCSI_MGMT_ITT_OFFSET
+ cmd_i
;
1593 INIT_LIST_HEAD(&mtask
->running
);
1596 if (scsi_add_host(shost
, NULL
))
1599 if (!try_module_get(iscsit
->owner
))
1600 goto cls_session_fail
;
1602 cls_session
= iscsi_create_session(shost
, iscsit
, 0);
1605 *(unsigned long*)shost
->hostdata
= (unsigned long)cls_session
;
1610 module_put(iscsit
->owner
);
1612 scsi_remove_host(shost
);
1614 iscsi_pool_free(&session
->mgmtpool
, (void**)session
->mgmt_cmds
);
1615 mgmtpool_alloc_fail
:
1616 iscsi_pool_free(&session
->cmdpool
, (void**)session
->cmds
);
1618 scsi_host_put(shost
);
1621 EXPORT_SYMBOL_GPL(iscsi_session_setup
);
1624 * iscsi_session_teardown - destroy session, host, and cls_session
1627 * This can be used by software iscsi_transports that allocate
1628 * a session per scsi host.
1630 void iscsi_session_teardown(struct iscsi_cls_session
*cls_session
)
1632 struct Scsi_Host
*shost
= iscsi_session_to_shost(cls_session
);
1633 struct iscsi_session
*session
= iscsi_hostdata(shost
->hostdata
);
1634 struct module
*owner
= cls_session
->transport
->owner
;
1636 iscsi_unblock_session(cls_session
);
1637 scsi_remove_host(shost
);
1639 iscsi_pool_free(&session
->mgmtpool
, (void**)session
->mgmt_cmds
);
1640 iscsi_pool_free(&session
->cmdpool
, (void**)session
->cmds
);
1642 kfree(session
->password
);
1643 kfree(session
->password_in
);
1644 kfree(session
->username
);
1645 kfree(session
->username_in
);
1646 kfree(session
->targetname
);
1647 kfree(session
->netdev
);
1648 kfree(session
->hwaddress
);
1649 kfree(session
->initiatorname
);
1651 iscsi_destroy_session(cls_session
);
1652 scsi_host_put(shost
);
1655 EXPORT_SYMBOL_GPL(iscsi_session_teardown
);
1658 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
1659 * @cls_session: iscsi_cls_session
1662 struct iscsi_cls_conn
*
1663 iscsi_conn_setup(struct iscsi_cls_session
*cls_session
, uint32_t conn_idx
)
1665 struct iscsi_session
*session
= class_to_transport_session(cls_session
);
1666 struct iscsi_conn
*conn
;
1667 struct iscsi_cls_conn
*cls_conn
;
1670 cls_conn
= iscsi_create_conn(cls_session
, conn_idx
);
1673 conn
= cls_conn
->dd_data
;
1674 memset(conn
, 0, sizeof(*conn
));
1676 conn
->session
= session
;
1677 conn
->cls_conn
= cls_conn
;
1678 conn
->c_stage
= ISCSI_CONN_INITIAL_STAGE
;
1679 conn
->id
= conn_idx
;
1680 conn
->exp_statsn
= 0;
1681 conn
->tmf_state
= TMF_INITIAL
;
1682 INIT_LIST_HEAD(&conn
->run_list
);
1683 INIT_LIST_HEAD(&conn
->mgmt_run_list
);
1684 INIT_LIST_HEAD(&conn
->mgmtqueue
);
1685 INIT_LIST_HEAD(&conn
->xmitqueue
);
1686 INIT_LIST_HEAD(&conn
->requeue
);
1687 INIT_WORK(&conn
->xmitwork
, iscsi_xmitworker
);
1689 /* allocate login_mtask used for the login/text sequences */
1690 spin_lock_bh(&session
->lock
);
1691 if (!__kfifo_get(session
->mgmtpool
.queue
,
1692 (void*)&conn
->login_mtask
,
1694 spin_unlock_bh(&session
->lock
);
1695 goto login_mtask_alloc_fail
;
1697 spin_unlock_bh(&session
->lock
);
1699 data
= kmalloc(ISCSI_DEF_MAX_RECV_SEG_LEN
, GFP_KERNEL
);
1701 goto login_mtask_data_alloc_fail
;
1702 conn
->login_mtask
->data
= conn
->data
= data
;
1704 init_timer(&conn
->tmf_timer
);
1705 init_waitqueue_head(&conn
->ehwait
);
1709 login_mtask_data_alloc_fail
:
1710 __kfifo_put(session
->mgmtpool
.queue
, (void*)&conn
->login_mtask
,
1712 login_mtask_alloc_fail
:
1713 iscsi_destroy_conn(cls_conn
);
1716 EXPORT_SYMBOL_GPL(iscsi_conn_setup
);
1719 * iscsi_conn_teardown - teardown iscsi connection
1720 * cls_conn: iscsi class connection
1722 * TODO: we may need to make this into a two step process
1723 * like scsi-mls remove + put host
1725 void iscsi_conn_teardown(struct iscsi_cls_conn
*cls_conn
)
1727 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
1728 struct iscsi_session
*session
= conn
->session
;
1729 unsigned long flags
;
1731 spin_lock_bh(&session
->lock
);
1732 conn
->c_stage
= ISCSI_CONN_CLEANUP_WAIT
;
1733 if (session
->leadconn
== conn
) {
1735 * leading connection? then give up on recovery.
1737 session
->state
= ISCSI_STATE_TERMINATE
;
1738 wake_up(&conn
->ehwait
);
1740 spin_unlock_bh(&session
->lock
);
1743 * Block until all in-progress commands for this connection
1747 spin_lock_irqsave(session
->host
->host_lock
, flags
);
1748 if (!session
->host
->host_busy
) { /* OK for ERL == 0 */
1749 spin_unlock_irqrestore(session
->host
->host_lock
, flags
);
1752 spin_unlock_irqrestore(session
->host
->host_lock
, flags
);
1753 msleep_interruptible(500);
1754 printk(KERN_INFO
"iscsi: scsi conn_destroy(): host_busy %d "
1755 "host_failed %d\n", session
->host
->host_busy
,
1756 session
->host
->host_failed
);
1758 * force eh_abort() to unblock
1760 wake_up(&conn
->ehwait
);
1763 /* flush queued up work because we free the connection below */
1764 iscsi_suspend_tx(conn
);
1766 spin_lock_bh(&session
->lock
);
1768 kfree(conn
->persistent_address
);
1769 __kfifo_put(session
->mgmtpool
.queue
, (void*)&conn
->login_mtask
,
1771 if (session
->leadconn
== conn
)
1772 session
->leadconn
= NULL
;
1773 spin_unlock_bh(&session
->lock
);
1775 iscsi_destroy_conn(cls_conn
);
1777 EXPORT_SYMBOL_GPL(iscsi_conn_teardown
);
1779 int iscsi_conn_start(struct iscsi_cls_conn
*cls_conn
)
1781 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
1782 struct iscsi_session
*session
= conn
->session
;
1785 printk(KERN_ERR
"iscsi: can't start unbound connection\n");
1789 if ((session
->imm_data_en
|| !session
->initial_r2t_en
) &&
1790 session
->first_burst
> session
->max_burst
) {
1791 printk("iscsi: invalid burst lengths: "
1792 "first_burst %d max_burst %d\n",
1793 session
->first_burst
, session
->max_burst
);
1797 spin_lock_bh(&session
->lock
);
1798 conn
->c_stage
= ISCSI_CONN_STARTED
;
1799 session
->state
= ISCSI_STATE_LOGGED_IN
;
1800 session
->queued_cmdsn
= session
->cmdsn
;
1802 switch(conn
->stop_stage
) {
1803 case STOP_CONN_RECOVER
:
1805 * unblock eh_abort() if it is blocked. re-try all
1806 * commands after successful recovery
1808 conn
->stop_stage
= 0;
1809 conn
->tmf_state
= TMF_INITIAL
;
1811 spin_unlock_bh(&session
->lock
);
1813 iscsi_unblock_session(session_to_cls(session
));
1814 wake_up(&conn
->ehwait
);
1816 case STOP_CONN_TERM
:
1817 conn
->stop_stage
= 0;
1822 spin_unlock_bh(&session
->lock
);
1826 EXPORT_SYMBOL_GPL(iscsi_conn_start
);
1829 flush_control_queues(struct iscsi_session
*session
, struct iscsi_conn
*conn
)
1831 struct iscsi_mgmt_task
*mtask
, *tmp
;
1833 /* handle pending */
1834 list_for_each_entry_safe(mtask
, tmp
, &conn
->mgmtqueue
, running
) {
1835 debug_scsi("flushing pending mgmt task itt 0x%x\n", mtask
->itt
);
1836 list_del_init(&mtask
->running
);
1837 if (mtask
== conn
->login_mtask
)
1839 __kfifo_put(session
->mgmtpool
.queue
, (void*)&mtask
,
1843 /* handle running */
1844 list_for_each_entry_safe(mtask
, tmp
, &conn
->mgmt_run_list
, running
) {
1845 debug_scsi("flushing running mgmt task itt 0x%x\n", mtask
->itt
);
1846 list_del_init(&mtask
->running
);
1848 if (mtask
== conn
->login_mtask
)
1850 __kfifo_put(session
->mgmtpool
.queue
, (void*)&mtask
,
1857 static void iscsi_start_session_recovery(struct iscsi_session
*session
,
1858 struct iscsi_conn
*conn
, int flag
)
1862 mutex_lock(&session
->eh_mutex
);
1863 spin_lock_bh(&session
->lock
);
1864 if (conn
->stop_stage
== STOP_CONN_TERM
) {
1865 spin_unlock_bh(&session
->lock
);
1866 mutex_unlock(&session
->eh_mutex
);
1871 * The LLD either freed/unset the lock on us, or userspace called
1872 * stop but did not create a proper connection (connection was never
1873 * bound or it was unbound then stop was called).
1875 if (!conn
->recv_lock
) {
1876 spin_unlock_bh(&session
->lock
);
1877 mutex_unlock(&session
->eh_mutex
);
1882 * When this is called for the in_login state, we only want to clean
1883 * up the login task and connection. We do not need to block and set
1884 * the recovery state again
1886 if (flag
== STOP_CONN_TERM
)
1887 session
->state
= ISCSI_STATE_TERMINATE
;
1888 else if (conn
->stop_stage
!= STOP_CONN_RECOVER
)
1889 session
->state
= ISCSI_STATE_IN_RECOVERY
;
1891 old_stop_stage
= conn
->stop_stage
;
1892 conn
->stop_stage
= flag
;
1893 conn
->c_stage
= ISCSI_CONN_STOPPED
;
1894 spin_unlock_bh(&session
->lock
);
1896 iscsi_suspend_tx(conn
);
1898 write_lock_bh(conn
->recv_lock
);
1899 set_bit(ISCSI_SUSPEND_BIT
, &conn
->suspend_rx
);
1900 write_unlock_bh(conn
->recv_lock
);
1903 * for connection level recovery we should not calculate
1904 * header digest. conn->hdr_size used for optimization
1905 * in hdr_extract() and will be re-negotiated at
1908 if (flag
== STOP_CONN_RECOVER
) {
1909 conn
->hdrdgst_en
= 0;
1910 conn
->datadgst_en
= 0;
1911 if (session
->state
== ISCSI_STATE_IN_RECOVERY
&&
1912 old_stop_stage
!= STOP_CONN_RECOVER
) {
1913 debug_scsi("blocking session\n");
1914 iscsi_block_session(session_to_cls(session
));
1921 spin_lock_bh(&session
->lock
);
1922 fail_all_commands(conn
, -1);
1923 flush_control_queues(session
, conn
);
1924 spin_unlock_bh(&session
->lock
);
1925 mutex_unlock(&session
->eh_mutex
);
1928 void iscsi_conn_stop(struct iscsi_cls_conn
*cls_conn
, int flag
)
1930 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
1931 struct iscsi_session
*session
= conn
->session
;
1934 case STOP_CONN_RECOVER
:
1935 case STOP_CONN_TERM
:
1936 iscsi_start_session_recovery(session
, conn
, flag
);
1939 printk(KERN_ERR
"iscsi: invalid stop flag %d\n", flag
);
1942 EXPORT_SYMBOL_GPL(iscsi_conn_stop
);
1944 int iscsi_conn_bind(struct iscsi_cls_session
*cls_session
,
1945 struct iscsi_cls_conn
*cls_conn
, int is_leading
)
1947 struct iscsi_session
*session
= class_to_transport_session(cls_session
);
1948 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
1950 spin_lock_bh(&session
->lock
);
1952 session
->leadconn
= conn
;
1953 spin_unlock_bh(&session
->lock
);
1956 * Unblock xmitworker(), Login Phase will pass through.
1958 clear_bit(ISCSI_SUSPEND_BIT
, &conn
->suspend_rx
);
1959 clear_bit(ISCSI_SUSPEND_BIT
, &conn
->suspend_tx
);
1962 EXPORT_SYMBOL_GPL(iscsi_conn_bind
);
1965 int iscsi_set_param(struct iscsi_cls_conn
*cls_conn
,
1966 enum iscsi_param param
, char *buf
, int buflen
)
1968 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
1969 struct iscsi_session
*session
= conn
->session
;
1973 case ISCSI_PARAM_FAST_ABORT
:
1974 sscanf(buf
, "%d", &session
->fast_abort
);
1976 case ISCSI_PARAM_MAX_RECV_DLENGTH
:
1977 sscanf(buf
, "%d", &conn
->max_recv_dlength
);
1979 case ISCSI_PARAM_MAX_XMIT_DLENGTH
:
1980 sscanf(buf
, "%d", &conn
->max_xmit_dlength
);
1982 case ISCSI_PARAM_HDRDGST_EN
:
1983 sscanf(buf
, "%d", &conn
->hdrdgst_en
);
1985 case ISCSI_PARAM_DATADGST_EN
:
1986 sscanf(buf
, "%d", &conn
->datadgst_en
);
1988 case ISCSI_PARAM_INITIAL_R2T_EN
:
1989 sscanf(buf
, "%d", &session
->initial_r2t_en
);
1991 case ISCSI_PARAM_MAX_R2T
:
1992 sscanf(buf
, "%d", &session
->max_r2t
);
1994 case ISCSI_PARAM_IMM_DATA_EN
:
1995 sscanf(buf
, "%d", &session
->imm_data_en
);
1997 case ISCSI_PARAM_FIRST_BURST
:
1998 sscanf(buf
, "%d", &session
->first_burst
);
2000 case ISCSI_PARAM_MAX_BURST
:
2001 sscanf(buf
, "%d", &session
->max_burst
);
2003 case ISCSI_PARAM_PDU_INORDER_EN
:
2004 sscanf(buf
, "%d", &session
->pdu_inorder_en
);
2006 case ISCSI_PARAM_DATASEQ_INORDER_EN
:
2007 sscanf(buf
, "%d", &session
->dataseq_inorder_en
);
2009 case ISCSI_PARAM_ERL
:
2010 sscanf(buf
, "%d", &session
->erl
);
2012 case ISCSI_PARAM_IFMARKER_EN
:
2013 sscanf(buf
, "%d", &value
);
2016 case ISCSI_PARAM_OFMARKER_EN
:
2017 sscanf(buf
, "%d", &value
);
2020 case ISCSI_PARAM_EXP_STATSN
:
2021 sscanf(buf
, "%u", &conn
->exp_statsn
);
2023 case ISCSI_PARAM_USERNAME
:
2024 kfree(session
->username
);
2025 session
->username
= kstrdup(buf
, GFP_KERNEL
);
2026 if (!session
->username
)
2029 case ISCSI_PARAM_USERNAME_IN
:
2030 kfree(session
->username_in
);
2031 session
->username_in
= kstrdup(buf
, GFP_KERNEL
);
2032 if (!session
->username_in
)
2035 case ISCSI_PARAM_PASSWORD
:
2036 kfree(session
->password
);
2037 session
->password
= kstrdup(buf
, GFP_KERNEL
);
2038 if (!session
->password
)
2041 case ISCSI_PARAM_PASSWORD_IN
:
2042 kfree(session
->password_in
);
2043 session
->password_in
= kstrdup(buf
, GFP_KERNEL
);
2044 if (!session
->password_in
)
2047 case ISCSI_PARAM_TARGET_NAME
:
2048 /* this should not change between logins */
2049 if (session
->targetname
)
2052 session
->targetname
= kstrdup(buf
, GFP_KERNEL
);
2053 if (!session
->targetname
)
2056 case ISCSI_PARAM_TPGT
:
2057 sscanf(buf
, "%d", &session
->tpgt
);
2059 case ISCSI_PARAM_PERSISTENT_PORT
:
2060 sscanf(buf
, "%d", &conn
->persistent_port
);
2062 case ISCSI_PARAM_PERSISTENT_ADDRESS
:
2064 * this is the address returned in discovery so it should
2065 * not change between logins.
2067 if (conn
->persistent_address
)
2070 conn
->persistent_address
= kstrdup(buf
, GFP_KERNEL
);
2071 if (!conn
->persistent_address
)
2080 EXPORT_SYMBOL_GPL(iscsi_set_param
);
2082 int iscsi_session_get_param(struct iscsi_cls_session
*cls_session
,
2083 enum iscsi_param param
, char *buf
)
2085 struct Scsi_Host
*shost
= iscsi_session_to_shost(cls_session
);
2086 struct iscsi_session
*session
= iscsi_hostdata(shost
->hostdata
);
2090 case ISCSI_PARAM_FAST_ABORT
:
2091 len
= sprintf(buf
, "%d\n", session
->fast_abort
);
2093 case ISCSI_PARAM_INITIAL_R2T_EN
:
2094 len
= sprintf(buf
, "%d\n", session
->initial_r2t_en
);
2096 case ISCSI_PARAM_MAX_R2T
:
2097 len
= sprintf(buf
, "%hu\n", session
->max_r2t
);
2099 case ISCSI_PARAM_IMM_DATA_EN
:
2100 len
= sprintf(buf
, "%d\n", session
->imm_data_en
);
2102 case ISCSI_PARAM_FIRST_BURST
:
2103 len
= sprintf(buf
, "%u\n", session
->first_burst
);
2105 case ISCSI_PARAM_MAX_BURST
:
2106 len
= sprintf(buf
, "%u\n", session
->max_burst
);
2108 case ISCSI_PARAM_PDU_INORDER_EN
:
2109 len
= sprintf(buf
, "%d\n", session
->pdu_inorder_en
);
2111 case ISCSI_PARAM_DATASEQ_INORDER_EN
:
2112 len
= sprintf(buf
, "%d\n", session
->dataseq_inorder_en
);
2114 case ISCSI_PARAM_ERL
:
2115 len
= sprintf(buf
, "%d\n", session
->erl
);
2117 case ISCSI_PARAM_TARGET_NAME
:
2118 len
= sprintf(buf
, "%s\n", session
->targetname
);
2120 case ISCSI_PARAM_TPGT
:
2121 len
= sprintf(buf
, "%d\n", session
->tpgt
);
2123 case ISCSI_PARAM_USERNAME
:
2124 len
= sprintf(buf
, "%s\n", session
->username
);
2126 case ISCSI_PARAM_USERNAME_IN
:
2127 len
= sprintf(buf
, "%s\n", session
->username_in
);
2129 case ISCSI_PARAM_PASSWORD
:
2130 len
= sprintf(buf
, "%s\n", session
->password
);
2132 case ISCSI_PARAM_PASSWORD_IN
:
2133 len
= sprintf(buf
, "%s\n", session
->password_in
);
2141 EXPORT_SYMBOL_GPL(iscsi_session_get_param
);
2143 int iscsi_conn_get_param(struct iscsi_cls_conn
*cls_conn
,
2144 enum iscsi_param param
, char *buf
)
2146 struct iscsi_conn
*conn
= cls_conn
->dd_data
;
2150 case ISCSI_PARAM_MAX_RECV_DLENGTH
:
2151 len
= sprintf(buf
, "%u\n", conn
->max_recv_dlength
);
2153 case ISCSI_PARAM_MAX_XMIT_DLENGTH
:
2154 len
= sprintf(buf
, "%u\n", conn
->max_xmit_dlength
);
2156 case ISCSI_PARAM_HDRDGST_EN
:
2157 len
= sprintf(buf
, "%d\n", conn
->hdrdgst_en
);
2159 case ISCSI_PARAM_DATADGST_EN
:
2160 len
= sprintf(buf
, "%d\n", conn
->datadgst_en
);
2162 case ISCSI_PARAM_IFMARKER_EN
:
2163 len
= sprintf(buf
, "%d\n", conn
->ifmarker_en
);
2165 case ISCSI_PARAM_OFMARKER_EN
:
2166 len
= sprintf(buf
, "%d\n", conn
->ofmarker_en
);
2168 case ISCSI_PARAM_EXP_STATSN
:
2169 len
= sprintf(buf
, "%u\n", conn
->exp_statsn
);
2171 case ISCSI_PARAM_PERSISTENT_PORT
:
2172 len
= sprintf(buf
, "%d\n", conn
->persistent_port
);
2174 case ISCSI_PARAM_PERSISTENT_ADDRESS
:
2175 len
= sprintf(buf
, "%s\n", conn
->persistent_address
);
2183 EXPORT_SYMBOL_GPL(iscsi_conn_get_param
);
2185 int iscsi_host_get_param(struct Scsi_Host
*shost
, enum iscsi_host_param param
,
2188 struct iscsi_session
*session
= iscsi_hostdata(shost
->hostdata
);
2192 case ISCSI_HOST_PARAM_NETDEV_NAME
:
2193 if (!session
->netdev
)
2194 len
= sprintf(buf
, "%s\n", "default");
2196 len
= sprintf(buf
, "%s\n", session
->netdev
);
2198 case ISCSI_HOST_PARAM_HWADDRESS
:
2199 if (!session
->hwaddress
)
2200 len
= sprintf(buf
, "%s\n", "default");
2202 len
= sprintf(buf
, "%s\n", session
->hwaddress
);
2204 case ISCSI_HOST_PARAM_INITIATOR_NAME
:
2205 if (!session
->initiatorname
)
2206 len
= sprintf(buf
, "%s\n", "unknown");
2208 len
= sprintf(buf
, "%s\n", session
->initiatorname
);
2217 EXPORT_SYMBOL_GPL(iscsi_host_get_param
);
2219 int iscsi_host_set_param(struct Scsi_Host
*shost
, enum iscsi_host_param param
,
2220 char *buf
, int buflen
)
2222 struct iscsi_session
*session
= iscsi_hostdata(shost
->hostdata
);
2225 case ISCSI_HOST_PARAM_NETDEV_NAME
:
2226 if (!session
->netdev
)
2227 session
->netdev
= kstrdup(buf
, GFP_KERNEL
);
2229 case ISCSI_HOST_PARAM_HWADDRESS
:
2230 if (!session
->hwaddress
)
2231 session
->hwaddress
= kstrdup(buf
, GFP_KERNEL
);
2233 case ISCSI_HOST_PARAM_INITIATOR_NAME
:
2234 if (!session
->initiatorname
)
2235 session
->initiatorname
= kstrdup(buf
, GFP_KERNEL
);
2243 EXPORT_SYMBOL_GPL(iscsi_host_set_param
);
2245 MODULE_AUTHOR("Mike Christie");
2246 MODULE_DESCRIPTION("iSCSI library functions");
2247 MODULE_LICENSE("GPL");