]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/scsi/libiscsi.c
[SCSI] iscsi_tcp: drop session when itt does not match any command
[net-next-2.6.git] / drivers / scsi / libiscsi.c
CommitLineData
7996a778
MC
1/*
2 * iSCSI lib functions
3 *
4 * Copyright (C) 2006 Red Hat, Inc. All rights reserved.
5 * Copyright (C) 2004 - 2006 Mike Christie
6 * Copyright (C) 2004 - 2005 Dmitry Yusupov
7 * Copyright (C) 2004 - 2005 Alex Aizman
8 * maintained by open-iscsi@googlegroups.com
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 */
24#include <linux/types.h>
7996a778
MC
25#include <linux/kfifo.h>
26#include <linux/delay.h>
8eb00539 27#include <asm/unaligned.h>
7996a778
MC
28#include <net/tcp.h>
29#include <scsi/scsi_cmnd.h>
30#include <scsi/scsi_device.h>
31#include <scsi/scsi_eh.h>
32#include <scsi/scsi_tcq.h>
33#include <scsi/scsi_host.h>
34#include <scsi/scsi.h>
35#include <scsi/iscsi_proto.h>
36#include <scsi/scsi_transport.h>
37#include <scsi/scsi_transport_iscsi.h>
38#include <scsi/libiscsi.h>
39
40struct iscsi_session *
41class_to_transport_session(struct iscsi_cls_session *cls_session)
42{
43 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
44 return iscsi_hostdata(shost->hostdata);
45}
46EXPORT_SYMBOL_GPL(class_to_transport_session);
47
77a23c21
MC
48/* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
49#define SNA32_CHECK 2147483648UL
7996a778 50
77a23c21
MC
51static int iscsi_sna_lt(u32 n1, u32 n2)
52{
53 return n1 != n2 && ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
54 (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
55}
56
57/* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
58static int iscsi_sna_lte(u32 n1, u32 n2)
59{
60 return n1 == n2 || ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
61 (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
62}
63
64void
65iscsi_update_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr)
7996a778
MC
66{
67 uint32_t max_cmdsn = be32_to_cpu(hdr->max_cmdsn);
68 uint32_t exp_cmdsn = be32_to_cpu(hdr->exp_cmdsn);
69
77a23c21
MC
70 /*
71 * standard specifies this check for when to update expected and
72 * max sequence numbers
73 */
74 if (iscsi_sna_lt(max_cmdsn, exp_cmdsn - 1))
75 return;
76
77 if (exp_cmdsn != session->exp_cmdsn &&
78 !iscsi_sna_lt(exp_cmdsn, session->exp_cmdsn))
7996a778
MC
79 session->exp_cmdsn = exp_cmdsn;
80
77a23c21
MC
81 if (max_cmdsn != session->max_cmdsn &&
82 !iscsi_sna_lt(max_cmdsn, session->max_cmdsn)) {
83 session->max_cmdsn = max_cmdsn;
84 /*
85 * if the window closed with IO queued, then kick the
86 * xmit thread
87 */
88 if (!list_empty(&session->leadconn->xmitqueue) ||
843c0a8a 89 !list_empty(&session->leadconn->mgmtqueue))
77a23c21
MC
90 scsi_queue_work(session->host,
91 &session->leadconn->xmitwork);
92 }
7996a778 93}
77a23c21 94EXPORT_SYMBOL_GPL(iscsi_update_cmdsn);
7996a778
MC
95
96void iscsi_prep_unsolicit_data_pdu(struct iscsi_cmd_task *ctask,
ffd0436e 97 struct iscsi_data *hdr)
7996a778
MC
98{
99 struct iscsi_conn *conn = ctask->conn;
100
101 memset(hdr, 0, sizeof(struct iscsi_data));
102 hdr->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
103 hdr->datasn = cpu_to_be32(ctask->unsol_datasn);
104 ctask->unsol_datasn++;
105 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
106 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
107
108 hdr->itt = ctask->hdr->itt;
109 hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
ffd0436e 110 hdr->offset = cpu_to_be32(ctask->unsol_offset);
7996a778
MC
111
112 if (ctask->unsol_count > conn->max_xmit_dlength) {
113 hton24(hdr->dlength, conn->max_xmit_dlength);
114 ctask->data_count = conn->max_xmit_dlength;
ffd0436e 115 ctask->unsol_offset += ctask->data_count;
7996a778
MC
116 hdr->flags = 0;
117 } else {
118 hton24(hdr->dlength, ctask->unsol_count);
119 ctask->data_count = ctask->unsol_count;
120 hdr->flags = ISCSI_FLAG_CMD_FINAL;
121 }
122}
123EXPORT_SYMBOL_GPL(iscsi_prep_unsolicit_data_pdu);
124
004d6530
BH
125static int iscsi_add_hdr(struct iscsi_cmd_task *ctask, unsigned len)
126{
127 unsigned exp_len = ctask->hdr_len + len;
128
129 if (exp_len > ctask->hdr_max) {
130 WARN_ON(1);
131 return -EINVAL;
132 }
133
134 WARN_ON(len & (ISCSI_PAD_LEN - 1)); /* caller must pad the AHS */
135 ctask->hdr_len = exp_len;
136 return 0;
137}
138
7996a778
MC
139/**
140 * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
141 * @ctask: iscsi cmd task
142 *
143 * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
144 * fields like dlength or final based on how much data it sends
145 */
004d6530 146static int iscsi_prep_scsi_cmd_pdu(struct iscsi_cmd_task *ctask)
7996a778
MC
147{
148 struct iscsi_conn *conn = ctask->conn;
149 struct iscsi_session *session = conn->session;
150 struct iscsi_cmd *hdr = ctask->hdr;
151 struct scsi_cmnd *sc = ctask->sc;
004d6530
BH
152 unsigned hdrlength;
153 int rc;
7996a778 154
004d6530
BH
155 ctask->hdr_len = 0;
156 rc = iscsi_add_hdr(ctask, sizeof(*hdr));
157 if (rc)
158 return rc;
a8ac6311
OK
159 hdr->opcode = ISCSI_OP_SCSI_CMD;
160 hdr->flags = ISCSI_ATTR_SIMPLE;
161 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
162 hdr->itt = build_itt(ctask->itt, conn->id, session->age);
163 hdr->data_length = cpu_to_be32(scsi_bufflen(sc));
164 hdr->cmdsn = cpu_to_be32(session->cmdsn);
165 session->cmdsn++;
166 hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
167 memcpy(hdr->cdb, sc->cmnd, sc->cmd_len);
d473cc7f
MC
168 if (sc->cmd_len < MAX_COMMAND_SIZE)
169 memset(&hdr->cdb[sc->cmd_len], 0,
170 MAX_COMMAND_SIZE - sc->cmd_len);
7996a778 171
218432c6 172 ctask->imm_count = 0;
7996a778
MC
173 if (sc->sc_data_direction == DMA_TO_DEVICE) {
174 hdr->flags |= ISCSI_FLAG_CMD_WRITE;
175 /*
176 * Write counters:
177 *
178 * imm_count bytes to be sent right after
179 * SCSI PDU Header
180 *
181 * unsol_count bytes(as Data-Out) to be sent
182 * without R2T ack right after
183 * immediate data
184 *
185 * r2t_data_count bytes to be sent via R2T ack's
186 *
187 * pad_count bytes to be sent as zero-padding
188 */
7996a778 189 ctask->unsol_count = 0;
ffd0436e 190 ctask->unsol_offset = 0;
7996a778
MC
191 ctask->unsol_datasn = 0;
192
193 if (session->imm_data_en) {
1c138991 194 if (scsi_bufflen(sc) >= session->first_burst)
7996a778
MC
195 ctask->imm_count = min(session->first_burst,
196 conn->max_xmit_dlength);
197 else
1c138991 198 ctask->imm_count = min(scsi_bufflen(sc),
7996a778 199 conn->max_xmit_dlength);
a8ac6311 200 hton24(hdr->dlength, ctask->imm_count);
7996a778 201 } else
a8ac6311 202 zero_data(hdr->dlength);
7996a778 203
ffd0436e 204 if (!session->initial_r2t_en) {
857ae0bd 205 ctask->unsol_count = min((session->first_burst),
1c138991 206 (scsi_bufflen(sc))) - ctask->imm_count;
ffd0436e
MC
207 ctask->unsol_offset = ctask->imm_count;
208 }
209
7996a778
MC
210 if (!ctask->unsol_count)
211 /* No unsolicit Data-Out's */
a8ac6311 212 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
7996a778 213 } else {
7996a778
MC
214 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
215 zero_data(hdr->dlength);
216
217 if (sc->sc_data_direction == DMA_FROM_DEVICE)
218 hdr->flags |= ISCSI_FLAG_CMD_READ;
219 }
220
004d6530
BH
221 /* calculate size of additional header segments (AHSs) */
222 hdrlength = ctask->hdr_len - sizeof(*hdr);
223
224 WARN_ON(hdrlength & (ISCSI_PAD_LEN-1));
225 hdrlength /= ISCSI_PAD_LEN;
226
227 WARN_ON(hdrlength >= 256);
228 hdr->hlength = hdrlength & 0xFF;
229
a8ac6311
OK
230 if (conn->session->tt->init_cmd_task(conn->ctask))
231 return EIO;
77a23c21 232
a8ac6311
OK
233 conn->scsicmd_pdus_cnt++;
234 debug_scsi("iscsi prep [%s cid %d sc %p cdb 0x%x itt 0x%x len %d "
77a23c21 235 "cmdsn %d win %d]\n",
a8ac6311 236 sc->sc_data_direction == DMA_TO_DEVICE ? "write" : "read",
1c138991 237 conn->id, sc, sc->cmnd[0], ctask->itt, scsi_bufflen(sc),
a8ac6311 238 session->cmdsn, session->max_cmdsn - session->exp_cmdsn + 1);
004d6530 239 return 0;
7996a778 240}
7996a778
MC
241
242/**
243 * iscsi_complete_command - return command back to scsi-ml
7996a778
MC
244 * @ctask: iscsi cmd task
245 *
246 * Must be called with session lock.
247 * This function returns the scsi command to scsi-ml and returns
248 * the cmd task to the pool of available cmd tasks.
249 */
60ecebf5 250static void iscsi_complete_command(struct iscsi_cmd_task *ctask)
7996a778 251{
c1635cb7
MC
252 struct iscsi_conn *conn = ctask->conn;
253 struct iscsi_session *session = conn->session;
7996a778
MC
254 struct scsi_cmnd *sc = ctask->sc;
255
b6c395ed 256 ctask->state = ISCSI_TASK_COMPLETED;
7996a778 257 ctask->sc = NULL;
f47f2cf5
MC
258 /* SCSI eh reuses commands to verify us */
259 sc->SCp.ptr = NULL;
c1635cb7
MC
260 if (conn->ctask == ctask)
261 conn->ctask = NULL;
7996a778
MC
262 list_del_init(&ctask->running);
263 __kfifo_put(session->cmdpool.queue, (void*)&ctask, sizeof(void*));
264 sc->scsi_done(sc);
265}
266
60ecebf5
MC
267static void __iscsi_get_ctask(struct iscsi_cmd_task *ctask)
268{
269 atomic_inc(&ctask->refcount);
270}
271
60ecebf5
MC
272static void __iscsi_put_ctask(struct iscsi_cmd_task *ctask)
273{
e648f63c 274 if (atomic_dec_and_test(&ctask->refcount))
60ecebf5 275 iscsi_complete_command(ctask);
60ecebf5
MC
276}
277
b3a7ea8d
MC
278/*
279 * session lock must be held
280 */
281static void fail_command(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
282 int err)
283{
284 struct scsi_cmnd *sc;
285
286 sc = ctask->sc;
287 if (!sc)
288 return;
289
290 if (ctask->state == ISCSI_TASK_PENDING)
291 /*
292 * cmd never made it to the xmit thread, so we should not count
293 * the cmd in the sequencing
294 */
295 conn->session->queued_cmdsn--;
296 else
297 conn->session->tt->cleanup_cmd_task(conn, ctask);
298
299 sc->result = err;
300 scsi_set_resid(sc, scsi_bufflen(sc));
301 if (conn->ctask == ctask)
302 conn->ctask = NULL;
303 /* release ref from queuecommand */
304 __iscsi_put_ctask(ctask);
305}
306
307/**
308 * iscsi_free_mgmt_task - return mgmt task back to pool
309 * @conn: iscsi connection
310 * @mtask: mtask
311 *
312 * Must be called with session lock.
313 */
314void iscsi_free_mgmt_task(struct iscsi_conn *conn,
315 struct iscsi_mgmt_task *mtask)
316{
317 list_del_init(&mtask->running);
318 if (conn->login_mtask == mtask)
319 return;
f6d5180c
MC
320
321 if (conn->ping_mtask == mtask)
322 conn->ping_mtask = NULL;
b3a7ea8d
MC
323 __kfifo_put(conn->session->mgmtpool.queue,
324 (void*)&mtask, sizeof(void*));
325}
326EXPORT_SYMBOL_GPL(iscsi_free_mgmt_task);
327
f6d5180c
MC
328static struct iscsi_mgmt_task *
329__iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
330 char *data, uint32_t data_size)
331{
332 struct iscsi_session *session = conn->session;
333 struct iscsi_mgmt_task *mtask;
334
335 if (session->state == ISCSI_STATE_TERMINATE)
336 return NULL;
337
338 if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) ||
339 hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
340 /*
341 * Login and Text are sent serially, in
342 * request-followed-by-response sequence.
343 * Same mtask can be used. Same ITT must be used.
344 * Note that login_mtask is preallocated at conn_create().
345 */
346 mtask = conn->login_mtask;
347 else {
348 BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
349 BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
350
351 if (!__kfifo_get(session->mgmtpool.queue,
352 (void*)&mtask, sizeof(void*)))
353 return NULL;
354 }
355
356 if (data_size) {
357 memcpy(mtask->data, data, data_size);
358 mtask->data_count = data_size;
359 } else
360 mtask->data_count = 0;
361
362 memcpy(mtask->hdr, hdr, sizeof(struct iscsi_hdr));
363 INIT_LIST_HEAD(&mtask->running);
364 list_add_tail(&mtask->running, &conn->mgmtqueue);
365 return mtask;
366}
367
368int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
369 char *data, uint32_t data_size)
370{
371 struct iscsi_conn *conn = cls_conn->dd_data;
372 struct iscsi_session *session = conn->session;
373 int err = 0;
374
375 spin_lock_bh(&session->lock);
376 if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size))
377 err = -EPERM;
378 spin_unlock_bh(&session->lock);
379 scsi_queue_work(session->host, &conn->xmitwork);
380 return err;
381}
382EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
383
7996a778
MC
384/**
385 * iscsi_cmd_rsp - SCSI Command Response processing
386 * @conn: iscsi connection
387 * @hdr: iscsi header
388 * @ctask: scsi command task
389 * @data: cmd data buffer
390 * @datalen: len of buffer
391 *
392 * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
393 * then completes the command and task.
394 **/
77a23c21
MC
395static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
396 struct iscsi_cmd_task *ctask, char *data,
397 int datalen)
7996a778 398{
7996a778
MC
399 struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)hdr;
400 struct iscsi_session *session = conn->session;
401 struct scsi_cmnd *sc = ctask->sc;
402
77a23c21 403 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
7996a778
MC
404 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
405
406 sc->result = (DID_OK << 16) | rhdr->cmd_status;
407
408 if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
409 sc->result = DID_ERROR << 16;
410 goto out;
411 }
412
413 if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
9b80cb4b 414 uint16_t senselen;
7996a778
MC
415
416 if (datalen < 2) {
417invalid_datalen:
be2df72e
OG
418 printk(KERN_ERR "iscsi: Got CHECK_CONDITION but "
419 "invalid data buffer size of %d\n", datalen);
7996a778
MC
420 sc->result = DID_BAD_TARGET << 16;
421 goto out;
422 }
423
8eb00539 424 senselen = be16_to_cpu(get_unaligned((__be16 *) data));
7996a778
MC
425 if (datalen < senselen)
426 goto invalid_datalen;
427
428 memcpy(sc->sense_buffer, data + 2,
9b80cb4b 429 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
7996a778 430 debug_scsi("copied %d bytes of sense\n",
8eb00539 431 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
7996a778
MC
432 }
433
7207fea4
BH
434 if (rhdr->flags & (ISCSI_FLAG_CMD_UNDERFLOW |
435 ISCSI_FLAG_CMD_OVERFLOW)) {
7996a778
MC
436 int res_count = be32_to_cpu(rhdr->residual_count);
437
7207fea4
BH
438 if (res_count > 0 &&
439 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
440 res_count <= scsi_bufflen(sc)))
1c138991 441 scsi_set_resid(sc, res_count);
7996a778
MC
442 else
443 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
7207fea4
BH
444 } else if (rhdr->flags & (ISCSI_FLAG_CMD_BIDI_UNDERFLOW |
445 ISCSI_FLAG_CMD_BIDI_OVERFLOW))
7996a778 446 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
7996a778
MC
447
448out:
449 debug_scsi("done [sc %lx res %d itt 0x%x]\n",
450 (long)sc, sc->result, ctask->itt);
451 conn->scsirsp_pdus_cnt++;
452
60ecebf5 453 __iscsi_put_ctask(ctask);
7996a778
MC
454}
455
7ea8b828
MC
456static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
457{
458 struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
459
460 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
461 conn->tmfrsp_pdus_cnt++;
462
843c0a8a 463 if (conn->tmf_state != TMF_QUEUED)
7ea8b828
MC
464 return;
465
466 if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
843c0a8a 467 conn->tmf_state = TMF_SUCCESS;
7ea8b828 468 else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
843c0a8a 469 conn->tmf_state = TMF_NOT_FOUND;
7ea8b828 470 else
843c0a8a 471 conn->tmf_state = TMF_FAILED;
7ea8b828
MC
472 wake_up(&conn->ehwait);
473}
474
f6d5180c
MC
475static void iscsi_send_nopout(struct iscsi_conn *conn, struct iscsi_nopin *rhdr)
476{
477 struct iscsi_nopout hdr;
478 struct iscsi_mgmt_task *mtask;
479
480 if (!rhdr && conn->ping_mtask)
481 return;
482
483 memset(&hdr, 0, sizeof(struct iscsi_nopout));
484 hdr.opcode = ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE;
485 hdr.flags = ISCSI_FLAG_CMD_FINAL;
486
487 if (rhdr) {
488 memcpy(hdr.lun, rhdr->lun, 8);
489 hdr.ttt = rhdr->ttt;
490 hdr.itt = RESERVED_ITT;
491 } else
492 hdr.ttt = RESERVED_ITT;
493
494 mtask = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)&hdr, NULL, 0);
495 if (!mtask) {
496 printk(KERN_ERR "Could not send nopout\n");
497 return;
498 }
499
500 /* only track our nops */
501 if (!rhdr) {
502 conn->ping_mtask = mtask;
503 conn->last_ping = jiffies;
504 }
505 scsi_queue_work(conn->session->host, &conn->xmitwork);
506}
507
62f38300
MC
508static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
509 char *data, int datalen)
510{
511 struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
512 struct iscsi_hdr rejected_pdu;
513 uint32_t itt;
514
515 conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
516
517 if (reject->reason == ISCSI_REASON_DATA_DIGEST_ERROR) {
518 if (ntoh24(reject->dlength) > datalen)
519 return ISCSI_ERR_PROTO;
520
521 if (ntoh24(reject->dlength) >= sizeof(struct iscsi_hdr)) {
522 memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
b4377356 523 itt = get_itt(rejected_pdu.itt);
62f38300
MC
524 printk(KERN_ERR "itt 0x%x had pdu (op 0x%x) rejected "
525 "due to DataDigest error.\n", itt,
526 rejected_pdu.opcode);
527 }
528 }
529 return 0;
530}
531
7996a778
MC
532/**
533 * __iscsi_complete_pdu - complete pdu
534 * @conn: iscsi conn
535 * @hdr: iscsi header
536 * @data: data buffer
537 * @datalen: len of data buffer
538 *
539 * Completes pdu processing by freeing any resources allocated at
540 * queuecommand or send generic. session lock must be held and verify
541 * itt must have been called.
542 */
543int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
544 char *data, int datalen)
545{
546 struct iscsi_session *session = conn->session;
547 int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
548 struct iscsi_cmd_task *ctask;
549 struct iscsi_mgmt_task *mtask;
550 uint32_t itt;
551
f6d5180c 552 conn->last_recv = jiffies;
b4377356
AV
553 if (hdr->itt != RESERVED_ITT)
554 itt = get_itt(hdr->itt);
7996a778 555 else
b4377356 556 itt = ~0U;
7996a778
MC
557
558 if (itt < session->cmds_max) {
559 ctask = session->cmds[itt];
560
561 debug_scsi("cmdrsp [op 0x%x cid %d itt 0x%x len %d]\n",
562 opcode, conn->id, ctask->itt, datalen);
563
564 switch(opcode) {
565 case ISCSI_OP_SCSI_CMD_RSP:
566 BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
77a23c21
MC
567 iscsi_scsi_cmd_rsp(conn, hdr, ctask, data,
568 datalen);
7996a778
MC
569 break;
570 case ISCSI_OP_SCSI_DATA_IN:
571 BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
572 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
573 conn->scsirsp_pdus_cnt++;
60ecebf5 574 __iscsi_put_ctask(ctask);
7996a778
MC
575 }
576 break;
577 case ISCSI_OP_R2T:
578 /* LLD handles this for now */
579 break;
580 default:
581 rc = ISCSI_ERR_BAD_OPCODE;
582 break;
583 }
584 } else if (itt >= ISCSI_MGMT_ITT_OFFSET &&
585 itt < ISCSI_MGMT_ITT_OFFSET + session->mgmtpool_max) {
586 mtask = session->mgmt_cmds[itt - ISCSI_MGMT_ITT_OFFSET];
587
588 debug_scsi("immrsp [op 0x%x cid %d itt 0x%x len %d]\n",
589 opcode, conn->id, mtask->itt, datalen);
590
77a23c21 591 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
7996a778 592 switch(opcode) {
8d2860b3 593 case ISCSI_OP_LOGOUT_RSP:
c8dc1e52
MC
594 if (datalen) {
595 rc = ISCSI_ERR_PROTO;
596 break;
597 }
8d2860b3
MC
598 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
599 /* fall through */
7996a778
MC
600 case ISCSI_OP_LOGIN_RSP:
601 case ISCSI_OP_TEXT_RSP:
8d2860b3
MC
602 /*
603 * login related PDU's exp_statsn is handled in
604 * userspace
605 */
40527afe
MC
606 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
607 rc = ISCSI_ERR_CONN_FAILED;
b3a7ea8d 608 iscsi_free_mgmt_task(conn, mtask);
7996a778
MC
609 break;
610 case ISCSI_OP_SCSI_TMFUNC_RSP:
7996a778
MC
611 if (datalen) {
612 rc = ISCSI_ERR_PROTO;
613 break;
614 }
8d2860b3 615
7ea8b828 616 iscsi_tmf_rsp(conn, hdr);
b3a7ea8d 617 iscsi_free_mgmt_task(conn, mtask);
7996a778
MC
618 break;
619 case ISCSI_OP_NOOP_IN:
f6d5180c
MC
620 if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) ||
621 datalen) {
7996a778
MC
622 rc = ISCSI_ERR_PROTO;
623 break;
624 }
7996a778
MC
625 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
626
f6d5180c
MC
627 if (conn->ping_mtask != mtask) {
628 /*
629 * If this is not in response to one of our
630 * nops then it must be from userspace.
631 */
632 if (iscsi_recv_pdu(conn->cls_conn, hdr, data,
633 datalen))
634 rc = ISCSI_ERR_CONN_FAILED;
635 }
b3a7ea8d 636 iscsi_free_mgmt_task(conn, mtask);
7996a778
MC
637 break;
638 default:
639 rc = ISCSI_ERR_BAD_OPCODE;
640 break;
641 }
b4377356 642 } else if (itt == ~0U) {
77a23c21 643 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
62f38300 644
7996a778
MC
645 switch(opcode) {
646 case ISCSI_OP_NOOP_IN:
40527afe 647 if (datalen) {
7996a778 648 rc = ISCSI_ERR_PROTO;
40527afe
MC
649 break;
650 }
651
b4377356 652 if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
40527afe
MC
653 break;
654
f6d5180c 655 iscsi_send_nopout(conn, (struct iscsi_nopin*)hdr);
7996a778
MC
656 break;
657 case ISCSI_OP_REJECT:
62f38300
MC
658 rc = iscsi_handle_reject(conn, hdr, data, datalen);
659 break;
7996a778 660 case ISCSI_OP_ASYNC_EVENT:
8d2860b3 661 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
5831c737
MC
662 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
663 rc = ISCSI_ERR_CONN_FAILED;
7996a778
MC
664 break;
665 default:
666 rc = ISCSI_ERR_BAD_OPCODE;
667 break;
668 }
669 } else
670 rc = ISCSI_ERR_BAD_ITT;
671
672 return rc;
673}
674EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
675
676int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
677 char *data, int datalen)
678{
679 int rc;
680
681 spin_lock(&conn->session->lock);
682 rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
683 spin_unlock(&conn->session->lock);
684 return rc;
685}
686EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
687
688/* verify itt (itt encoding: age+cid+itt) */
689int iscsi_verify_itt(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
690 uint32_t *ret_itt)
691{
692 struct iscsi_session *session = conn->session;
693 struct iscsi_cmd_task *ctask;
694 uint32_t itt;
695
b4377356
AV
696 if (hdr->itt != RESERVED_ITT) {
697 if (((__force u32)hdr->itt & ISCSI_AGE_MASK) !=
7996a778 698 (session->age << ISCSI_AGE_SHIFT)) {
be2df72e 699 printk(KERN_ERR "iscsi: received itt %x expected "
b4377356 700 "session age (%x)\n", (__force u32)hdr->itt,
7996a778
MC
701 session->age & ISCSI_AGE_MASK);
702 return ISCSI_ERR_BAD_ITT;
703 }
704
b4377356 705 if (((__force u32)hdr->itt & ISCSI_CID_MASK) !=
7996a778 706 (conn->id << ISCSI_CID_SHIFT)) {
be2df72e 707 printk(KERN_ERR "iscsi: received itt %x, expected "
b4377356 708 "CID (%x)\n", (__force u32)hdr->itt, conn->id);
7996a778
MC
709 return ISCSI_ERR_BAD_ITT;
710 }
b4377356 711 itt = get_itt(hdr->itt);
7996a778 712 } else
b4377356 713 itt = ~0U;
7996a778
MC
714
715 if (itt < session->cmds_max) {
716 ctask = session->cmds[itt];
717
718 if (!ctask->sc) {
be2df72e 719 printk(KERN_INFO "iscsi: dropping ctask with "
7996a778
MC
720 "itt 0x%x\n", ctask->itt);
721 /* force drop */
722 return ISCSI_ERR_NO_SCSI_CMD;
723 }
724
725 if (ctask->sc->SCp.phase != session->age) {
be2df72e 726 printk(KERN_ERR "iscsi: ctask's session age %d, "
7996a778
MC
727 "expected %d\n", ctask->sc->SCp.phase,
728 session->age);
729 return ISCSI_ERR_SESSION_FAILED;
730 }
731 }
732
733 *ret_itt = itt;
734 return 0;
735}
736EXPORT_SYMBOL_GPL(iscsi_verify_itt);
737
738void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
739{
740 struct iscsi_session *session = conn->session;
741 unsigned long flags;
742
743 spin_lock_irqsave(&session->lock, flags);
656cffc9
MC
744 if (session->state == ISCSI_STATE_FAILED) {
745 spin_unlock_irqrestore(&session->lock, flags);
746 return;
747 }
748
67a61114 749 if (conn->stop_stage == 0)
7996a778
MC
750 session->state = ISCSI_STATE_FAILED;
751 spin_unlock_irqrestore(&session->lock, flags);
752 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
753 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
754 iscsi_conn_error(conn->cls_conn, err);
755}
756EXPORT_SYMBOL_GPL(iscsi_conn_failure);
757
77a23c21
MC
758static void iscsi_prep_mtask(struct iscsi_conn *conn,
759 struct iscsi_mgmt_task *mtask)
760{
761 struct iscsi_session *session = conn->session;
762 struct iscsi_hdr *hdr = mtask->hdr;
763 struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
764
765 if (hdr->opcode != (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) &&
766 hdr->opcode != (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
767 nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
768 /*
769 * pre-format CmdSN for outgoing PDU.
770 */
771 nop->cmdsn = cpu_to_be32(session->cmdsn);
772 if (hdr->itt != RESERVED_ITT) {
773 hdr->itt = build_itt(mtask->itt, conn->id, session->age);
e0726407
MC
774 /*
775 * TODO: We always use immediate, so we never hit this.
776 * If we start to send tmfs or nops as non-immediate then
777 * we should start checking the cmdsn numbers for mgmt tasks.
778 */
77a23c21 779 if (conn->c_stage == ISCSI_CONN_STARTED &&
e0726407
MC
780 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
781 session->queued_cmdsn++;
77a23c21 782 session->cmdsn++;
e0726407 783 }
77a23c21
MC
784 }
785
786 if (session->tt->init_mgmt_task)
787 session->tt->init_mgmt_task(conn, mtask);
788
789 debug_scsi("mgmtpdu [op 0x%x hdr->itt 0x%x datalen %d]\n",
843c0a8a
MC
790 hdr->opcode & ISCSI_OPCODE_MASK, hdr->itt,
791 mtask->data_count);
77a23c21
MC
792}
793
05db888a 794static int iscsi_xmit_mtask(struct iscsi_conn *conn)
b5072ea0
MC
795{
796 struct iscsi_hdr *hdr = conn->mtask->hdr;
b3a7ea8d 797 int rc;
b5072ea0 798
b3a7ea8d
MC
799 if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
800 conn->session->state = ISCSI_STATE_LOGGING_OUT;
77a23c21 801 spin_unlock_bh(&conn->session->lock);
b3a7ea8d 802
b5072ea0 803 rc = conn->session->tt->xmit_mgmt_task(conn, conn->mtask);
77a23c21 804 spin_lock_bh(&conn->session->lock);
b5072ea0
MC
805 if (rc)
806 return rc;
807
05db888a
MC
808 /* done with this in-progress mtask */
809 conn->mtask = NULL;
b5072ea0
MC
810 return 0;
811}
812
77a23c21
MC
813static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
814{
815 struct iscsi_session *session = conn->session;
816
817 /*
818 * Check for iSCSI window and take care of CmdSN wrap-around
819 */
e0726407
MC
820 if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) {
821 debug_scsi("iSCSI CmdSN closed. ExpCmdSn %u MaxCmdSN %u "
822 "CmdSN %u/%u\n", session->exp_cmdsn,
823 session->max_cmdsn, session->cmdsn,
824 session->queued_cmdsn);
77a23c21
MC
825 return -ENOSPC;
826 }
827 return 0;
828}
829
830static int iscsi_xmit_ctask(struct iscsi_conn *conn)
831{
832 struct iscsi_cmd_task *ctask = conn->ctask;
843c0a8a 833 int rc;
77a23c21
MC
834
835 __iscsi_get_ctask(ctask);
836 spin_unlock_bh(&conn->session->lock);
837 rc = conn->session->tt->xmit_cmd_task(conn, ctask);
838 spin_lock_bh(&conn->session->lock);
839 __iscsi_put_ctask(ctask);
77a23c21
MC
840 if (!rc)
841 /* done with this ctask */
842 conn->ctask = NULL;
843 return rc;
844}
845
843c0a8a
MC
846/**
847 * iscsi_requeue_ctask - requeue ctask to run from session workqueue
848 * @ctask: ctask to requeue
849 *
850 * LLDs that need to run a ctask from the session workqueue should call
851 * this. The session lock must be held.
852 */
853void iscsi_requeue_ctask(struct iscsi_cmd_task *ctask)
854{
855 struct iscsi_conn *conn = ctask->conn;
856
857 list_move_tail(&ctask->running, &conn->requeue);
858 scsi_queue_work(conn->session->host, &conn->xmitwork);
859}
860EXPORT_SYMBOL_GPL(iscsi_requeue_ctask);
861
7996a778
MC
862/**
863 * iscsi_data_xmit - xmit any command into the scheduled connection
864 * @conn: iscsi connection
865 *
866 * Notes:
867 * The function can return -EAGAIN in which case the caller must
868 * re-schedule it again later or recover. '0' return code means
869 * successful xmit.
870 **/
871static int iscsi_data_xmit(struct iscsi_conn *conn)
872{
3219e529 873 int rc = 0;
7996a778 874
77a23c21 875 spin_lock_bh(&conn->session->lock);
7996a778
MC
876 if (unlikely(conn->suspend_tx)) {
877 debug_scsi("conn %d Tx suspended!\n", conn->id);
77a23c21 878 spin_unlock_bh(&conn->session->lock);
3219e529 879 return -ENODATA;
7996a778 880 }
7996a778
MC
881
882 if (conn->ctask) {
77a23c21 883 rc = iscsi_xmit_ctask(conn);
3219e529 884 if (rc)
7996a778 885 goto again;
7996a778 886 }
77a23c21 887
7996a778 888 if (conn->mtask) {
05db888a 889 rc = iscsi_xmit_mtask(conn);
3219e529 890 if (rc)
7996a778 891 goto again;
7996a778
MC
892 }
893
77a23c21
MC
894 /*
895 * process mgmt pdus like nops before commands since we should
896 * only have one nop-out as a ping from us and targets should not
897 * overflow us with nop-ins
898 */
899check_mgmt:
843c0a8a
MC
900 while (!list_empty(&conn->mgmtqueue)) {
901 conn->mtask = list_entry(conn->mgmtqueue.next,
902 struct iscsi_mgmt_task, running);
b3a7ea8d
MC
903 if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
904 iscsi_free_mgmt_task(conn, conn->mtask);
905 conn->mtask = NULL;
906 continue;
907 }
908
77a23c21 909 iscsi_prep_mtask(conn, conn->mtask);
843c0a8a 910 list_move_tail(conn->mgmtqueue.next, &conn->mgmt_run_list);
77a23c21
MC
911 rc = iscsi_xmit_mtask(conn);
912 if (rc)
913 goto again;
7996a778
MC
914 }
915
843c0a8a 916 /* process pending command queue */
b6c395ed 917 while (!list_empty(&conn->xmitqueue)) {
843c0a8a
MC
918 if (conn->tmf_state == TMF_QUEUED)
919 break;
920
b6c395ed
MC
921 conn->ctask = list_entry(conn->xmitqueue.next,
922 struct iscsi_cmd_task, running);
b3a7ea8d 923 if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
9000bcd6 924 fail_command(conn, conn->ctask, DID_IMM_RETRY << 16);
b3a7ea8d
MC
925 continue;
926 }
004d6530
BH
927 if (iscsi_prep_scsi_cmd_pdu(conn->ctask)) {
928 fail_command(conn, conn->ctask, DID_ABORT << 16);
929 continue;
930 }
a8ac6311 931
843c0a8a 932 conn->ctask->state = ISCSI_TASK_RUNNING;
b6c395ed 933 list_move_tail(conn->xmitqueue.next, &conn->run_list);
77a23c21
MC
934 rc = iscsi_xmit_ctask(conn);
935 if (rc)
60ecebf5 936 goto again;
77a23c21
MC
937 /*
938 * we could continuously get new ctask requests so
939 * we need to check the mgmt queue for nops that need to
940 * be sent to aviod starvation
941 */
843c0a8a
MC
942 if (!list_empty(&conn->mgmtqueue))
943 goto check_mgmt;
944 }
945
946 while (!list_empty(&conn->requeue)) {
947 if (conn->session->fast_abort && conn->tmf_state != TMF_INITIAL)
948 break;
949
b3a7ea8d
MC
950 /*
951 * we always do fastlogout - conn stop code will clean up.
952 */
953 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
954 break;
955
843c0a8a
MC
956 conn->ctask = list_entry(conn->requeue.next,
957 struct iscsi_cmd_task, running);
958 conn->ctask->state = ISCSI_TASK_RUNNING;
959 list_move_tail(conn->requeue.next, &conn->run_list);
960 rc = iscsi_xmit_ctask(conn);
961 if (rc)
962 goto again;
963 if (!list_empty(&conn->mgmtqueue))
77a23c21 964 goto check_mgmt;
7996a778 965 }
b6c395ed 966 spin_unlock_bh(&conn->session->lock);
3219e529 967 return -ENODATA;
7996a778
MC
968
969again:
970 if (unlikely(conn->suspend_tx))
77a23c21
MC
971 rc = -ENODATA;
972 spin_unlock_bh(&conn->session->lock);
3219e529 973 return rc;
7996a778
MC
974}
975
c4028958 976static void iscsi_xmitworker(struct work_struct *work)
7996a778 977{
c4028958
DH
978 struct iscsi_conn *conn =
979 container_of(work, struct iscsi_conn, xmitwork);
3219e529 980 int rc;
7996a778
MC
981 /*
982 * serialize Xmit worker on a per-connection basis.
983 */
3219e529
MC
984 do {
985 rc = iscsi_data_xmit(conn);
986 } while (rc >= 0 || rc == -EAGAIN);
7996a778
MC
987}
988
989enum {
990 FAILURE_BAD_HOST = 1,
991 FAILURE_SESSION_FAILED,
992 FAILURE_SESSION_FREED,
993 FAILURE_WINDOW_CLOSED,
60ecebf5 994 FAILURE_OOM,
7996a778 995 FAILURE_SESSION_TERMINATE,
656cffc9 996 FAILURE_SESSION_IN_RECOVERY,
7996a778 997 FAILURE_SESSION_RECOVERY_TIMEOUT,
b3a7ea8d 998 FAILURE_SESSION_LOGGING_OUT,
7996a778
MC
999};
1000
1001int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
1002{
1003 struct Scsi_Host *host;
1004 int reason = 0;
1005 struct iscsi_session *session;
1006 struct iscsi_conn *conn;
1007 struct iscsi_cmd_task *ctask = NULL;
1008
1009 sc->scsi_done = done;
1010 sc->result = 0;
f47f2cf5 1011 sc->SCp.ptr = NULL;
7996a778
MC
1012
1013 host = sc->device->host;
1040c99d 1014 spin_unlock(host->host_lock);
7996a778 1015
1040c99d 1016 session = iscsi_hostdata(host->hostdata);
7996a778
MC
1017 spin_lock(&session->lock);
1018
656cffc9
MC
1019 /*
1020 * ISCSI_STATE_FAILED is a temp. state. The recovery
1021 * code will decide what is best to do with command queued
1022 * during this time
1023 */
1024 if (session->state != ISCSI_STATE_LOGGED_IN &&
1025 session->state != ISCSI_STATE_FAILED) {
1026 /*
1027 * to handle the race between when we set the recovery state
1028 * and block the session we requeue here (commands could
1029 * be entering our queuecommand while a block is starting
1030 * up because the block code is not locked)
1031 */
9000bcd6
MC
1032 switch (session->state) {
1033 case ISCSI_STATE_IN_RECOVERY:
656cffc9 1034 reason = FAILURE_SESSION_IN_RECOVERY;
67a61114 1035 goto reject;
9000bcd6
MC
1036 case ISCSI_STATE_LOGGING_OUT:
1037 reason = FAILURE_SESSION_LOGGING_OUT;
1038 goto reject;
b3a7ea8d 1039 case ISCSI_STATE_RECOVERY_FAILED:
656cffc9 1040 reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
b3a7ea8d
MC
1041 break;
1042 case ISCSI_STATE_TERMINATE:
656cffc9 1043 reason = FAILURE_SESSION_TERMINATE;
b3a7ea8d 1044 break;
b3a7ea8d 1045 default:
656cffc9 1046 reason = FAILURE_SESSION_FREED;
b3a7ea8d 1047 }
7996a778
MC
1048 goto fault;
1049 }
1050
7996a778 1051 conn = session->leadconn;
98644047
MC
1052 if (!conn) {
1053 reason = FAILURE_SESSION_FREED;
1054 goto fault;
1055 }
7996a778 1056
77a23c21
MC
1057 if (iscsi_check_cmdsn_window_closed(conn)) {
1058 reason = FAILURE_WINDOW_CLOSED;
1059 goto reject;
1060 }
1061
60ecebf5
MC
1062 if (!__kfifo_get(session->cmdpool.queue, (void*)&ctask,
1063 sizeof(void*))) {
1064 reason = FAILURE_OOM;
1065 goto reject;
1066 }
e0726407
MC
1067 session->queued_cmdsn++;
1068
7996a778
MC
1069 sc->SCp.phase = session->age;
1070 sc->SCp.ptr = (char *)ctask;
1071
60ecebf5 1072 atomic_set(&ctask->refcount, 1);
b6c395ed 1073 ctask->state = ISCSI_TASK_PENDING;
7996a778
MC
1074 ctask->conn = conn;
1075 ctask->sc = sc;
1076 INIT_LIST_HEAD(&ctask->running);
7996a778 1077
b6c395ed 1078 list_add_tail(&ctask->running, &conn->xmitqueue);
7996a778
MC
1079 spin_unlock(&session->lock);
1080
1081 scsi_queue_work(host, &conn->xmitwork);
1040c99d 1082 spin_lock(host->host_lock);
7996a778
MC
1083 return 0;
1084
1085reject:
1086 spin_unlock(&session->lock);
1087 debug_scsi("cmd 0x%x rejected (%d)\n", sc->cmnd[0], reason);
1040c99d 1088 spin_lock(host->host_lock);
7996a778
MC
1089 return SCSI_MLQUEUE_HOST_BUSY;
1090
1091fault:
1092 spin_unlock(&session->lock);
be2df72e 1093 printk(KERN_ERR "iscsi: cmd 0x%x is not queued (%d)\n",
7996a778
MC
1094 sc->cmnd[0], reason);
1095 sc->result = (DID_NO_CONNECT << 16);
1c138991 1096 scsi_set_resid(sc, scsi_bufflen(sc));
7996a778 1097 sc->scsi_done(sc);
1040c99d 1098 spin_lock(host->host_lock);
7996a778
MC
1099 return 0;
1100}
1101EXPORT_SYMBOL_GPL(iscsi_queuecommand);
1102
1103int iscsi_change_queue_depth(struct scsi_device *sdev, int depth)
1104{
1105 if (depth > ISCSI_MAX_CMD_PER_LUN)
1106 depth = ISCSI_MAX_CMD_PER_LUN;
1107 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
1108 return sdev->queue_depth;
1109}
1110EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
1111
7996a778
MC
1112void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
1113{
1114 struct iscsi_session *session = class_to_transport_session(cls_session);
7996a778
MC
1115
1116 spin_lock_bh(&session->lock);
1117 if (session->state != ISCSI_STATE_LOGGED_IN) {
656cffc9 1118 session->state = ISCSI_STATE_RECOVERY_FAILED;
843c0a8a
MC
1119 if (session->leadconn)
1120 wake_up(&session->leadconn->ehwait);
7996a778
MC
1121 }
1122 spin_unlock_bh(&session->lock);
1123}
1124EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
1125
1126int iscsi_eh_host_reset(struct scsi_cmnd *sc)
1127{
1128 struct Scsi_Host *host = sc->device->host;
1129 struct iscsi_session *session = iscsi_hostdata(host->hostdata);
1130 struct iscsi_conn *conn = session->leadconn;
7996a778 1131
bc436b27 1132 mutex_lock(&session->eh_mutex);
7996a778
MC
1133 spin_lock_bh(&session->lock);
1134 if (session->state == ISCSI_STATE_TERMINATE) {
1135failed:
1136 debug_scsi("failing host reset: session terminated "
d6e24d1c 1137 "[CID %d age %d]\n", conn->id, session->age);
7996a778 1138 spin_unlock_bh(&session->lock);
bc436b27 1139 mutex_unlock(&session->eh_mutex);
7996a778
MC
1140 return FAILED;
1141 }
1142
7996a778 1143 spin_unlock_bh(&session->lock);
bc436b27 1144 mutex_unlock(&session->eh_mutex);
7996a778
MC
1145 /*
1146 * we drop the lock here but the leadconn cannot be destoyed while
1147 * we are in the scsi eh
1148 */
843c0a8a 1149 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
7996a778
MC
1150
1151 debug_scsi("iscsi_eh_host_reset wait for relogin\n");
1152 wait_event_interruptible(conn->ehwait,
1153 session->state == ISCSI_STATE_TERMINATE ||
1154 session->state == ISCSI_STATE_LOGGED_IN ||
656cffc9 1155 session->state == ISCSI_STATE_RECOVERY_FAILED);
7996a778
MC
1156 if (signal_pending(current))
1157 flush_signals(current);
1158
bc436b27 1159 mutex_lock(&session->eh_mutex);
7996a778
MC
1160 spin_lock_bh(&session->lock);
1161 if (session->state == ISCSI_STATE_LOGGED_IN)
be2df72e 1162 printk(KERN_INFO "iscsi: host reset succeeded\n");
7996a778
MC
1163 else
1164 goto failed;
1165 spin_unlock_bh(&session->lock);
bc436b27 1166 mutex_unlock(&session->eh_mutex);
7996a778
MC
1167 return SUCCESS;
1168}
1169EXPORT_SYMBOL_GPL(iscsi_eh_host_reset);
1170
843c0a8a 1171static void iscsi_tmf_timedout(unsigned long data)
7996a778 1172{
843c0a8a 1173 struct iscsi_conn *conn = (struct iscsi_conn *)data;
7996a778
MC
1174 struct iscsi_session *session = conn->session;
1175
1176 spin_lock(&session->lock);
843c0a8a
MC
1177 if (conn->tmf_state == TMF_QUEUED) {
1178 conn->tmf_state = TMF_TIMEDOUT;
1179 debug_scsi("tmf timedout\n");
7996a778
MC
1180 /* unblock eh_abort() */
1181 wake_up(&conn->ehwait);
1182 }
1183 spin_unlock(&session->lock);
1184}
1185
843c0a8a 1186static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn,
f6d5180c
MC
1187 struct iscsi_tm *hdr, int age,
1188 int timeout)
7996a778 1189{
7996a778 1190 struct iscsi_session *session = conn->session;
843c0a8a 1191 struct iscsi_mgmt_task *mtask;
7996a778 1192
843c0a8a
MC
1193 mtask = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
1194 NULL, 0);
1195 if (!mtask) {
6724add1 1196 spin_unlock_bh(&session->lock);
7996a778 1197 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
843c0a8a
MC
1198 spin_lock_bh(&session->lock);
1199 debug_scsi("tmf exec failure\n");
77a23c21 1200 return -EPERM;
7996a778 1201 }
843c0a8a 1202 conn->tmfcmd_pdus_cnt++;
f6d5180c 1203 conn->tmf_timer.expires = timeout * HZ + jiffies;
843c0a8a
MC
1204 conn->tmf_timer.function = iscsi_tmf_timedout;
1205 conn->tmf_timer.data = (unsigned long)conn;
1206 add_timer(&conn->tmf_timer);
1207 debug_scsi("tmf set timeout\n");
7996a778 1208
7996a778 1209 spin_unlock_bh(&session->lock);
6724add1 1210 mutex_unlock(&session->eh_mutex);
77a23c21 1211 scsi_queue_work(session->host, &conn->xmitwork);
7996a778
MC
1212
1213 /*
1214 * block eh thread until:
1215 *
843c0a8a
MC
1216 * 1) tmf response
1217 * 2) tmf timeout
7996a778
MC
1218 * 3) session is terminated or restarted or userspace has
1219 * given up on recovery
1220 */
843c0a8a 1221 wait_event_interruptible(conn->ehwait, age != session->age ||
7996a778 1222 session->state != ISCSI_STATE_LOGGED_IN ||
843c0a8a 1223 conn->tmf_state != TMF_QUEUED);
7996a778
MC
1224 if (signal_pending(current))
1225 flush_signals(current);
843c0a8a
MC
1226 del_timer_sync(&conn->tmf_timer);
1227
6724add1 1228 mutex_lock(&session->eh_mutex);
77a23c21 1229 spin_lock_bh(&session->lock);
843c0a8a
MC
1230 /* if the session drops it will clean up the mtask */
1231 if (age != session->age ||
1232 session->state != ISCSI_STATE_LOGGED_IN)
1233 return -ENOTCONN;
7996a778
MC
1234 return 0;
1235}
1236
843c0a8a
MC
1237/*
1238 * Fail commands. session lock held and recv side suspended and xmit
1239 * thread flushed
1240 */
1241static void fail_all_commands(struct iscsi_conn *conn, unsigned lun)
1242{
1243 struct iscsi_cmd_task *ctask, *tmp;
1244
1245 if (conn->ctask && (conn->ctask->sc->device->lun == lun || lun == -1))
1246 conn->ctask = NULL;
1247
1248 /* flush pending */
1249 list_for_each_entry_safe(ctask, tmp, &conn->xmitqueue, running) {
1250 if (lun == ctask->sc->device->lun || lun == -1) {
1251 debug_scsi("failing pending sc %p itt 0x%x\n",
1252 ctask->sc, ctask->itt);
1253 fail_command(conn, ctask, DID_BUS_BUSY << 16);
1254 }
1255 }
1256
1257 list_for_each_entry_safe(ctask, tmp, &conn->requeue, running) {
1258 if (lun == ctask->sc->device->lun || lun == -1) {
1259 debug_scsi("failing requeued sc %p itt 0x%x\n",
1260 ctask->sc, ctask->itt);
1261 fail_command(conn, ctask, DID_BUS_BUSY << 16);
1262 }
1263 }
1264
1265 /* fail all other running */
1266 list_for_each_entry_safe(ctask, tmp, &conn->run_list, running) {
1267 if (lun == ctask->sc->device->lun || lun == -1) {
1268 debug_scsi("failing in progress sc %p itt 0x%x\n",
1269 ctask->sc, ctask->itt);
1270 fail_command(conn, ctask, DID_BUS_BUSY << 16);
1271 }
1272 }
1273}
1274
6724add1
MC
1275static void iscsi_suspend_tx(struct iscsi_conn *conn)
1276{
1277 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1278 scsi_flush_work(conn->session->host);
1279}
1280
1281static void iscsi_start_tx(struct iscsi_conn *conn)
1282{
1283 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1284 scsi_queue_work(conn->session->host, &conn->xmitwork);
1285}
1286
f6d5180c
MC
1287static enum scsi_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *scmd)
1288{
1289 struct iscsi_cls_session *cls_session;
1290 struct iscsi_session *session;
1291 struct iscsi_conn *conn;
1292 enum scsi_eh_timer_return rc = EH_NOT_HANDLED;
1293
1294 cls_session = starget_to_session(scsi_target(scmd->device));
1295 session = class_to_transport_session(cls_session);
1296
1297 debug_scsi("scsi cmd %p timedout\n", scmd);
1298
1299 spin_lock(&session->lock);
1300 if (session->state != ISCSI_STATE_LOGGED_IN) {
1301 /*
1302 * We are probably in the middle of iscsi recovery so let
1303 * that complete and handle the error.
1304 */
1305 rc = EH_RESET_TIMER;
1306 goto done;
1307 }
1308
1309 conn = session->leadconn;
1310 if (!conn) {
1311 /* In the middle of shuting down */
1312 rc = EH_RESET_TIMER;
1313 goto done;
1314 }
1315
1316 if (!conn->recv_timeout && !conn->ping_timeout)
1317 goto done;
1318 /*
1319 * if the ping timedout then we are in the middle of cleaning up
1320 * and can let the iscsi eh handle it
1321 */
1322 if (time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) +
1323 (conn->ping_timeout * HZ), jiffies))
1324 rc = EH_RESET_TIMER;
1325 /*
1326 * if we are about to check the transport then give the command
1327 * more time
1328 */
1329 if (time_before_eq(conn->last_recv + (conn->recv_timeout * HZ),
1330 jiffies))
1331 rc = EH_RESET_TIMER;
1332 /* if in the middle of checking the transport then give us more time */
1333 if (conn->ping_mtask)
1334 rc = EH_RESET_TIMER;
1335done:
1336 spin_unlock(&session->lock);
1337 debug_scsi("return %s\n", rc == EH_RESET_TIMER ? "timer reset" : "nh");
1338 return rc;
1339}
1340
1341static void iscsi_check_transport_timeouts(unsigned long data)
1342{
1343 struct iscsi_conn *conn = (struct iscsi_conn *)data;
1344 struct iscsi_session *session = conn->session;
1345 unsigned long timeout, next_timeout = 0, last_recv;
1346
1347 spin_lock(&session->lock);
1348 if (session->state != ISCSI_STATE_LOGGED_IN)
1349 goto done;
1350
1351 timeout = conn->recv_timeout;
1352 if (!timeout)
1353 goto done;
1354
1355 timeout *= HZ;
1356 last_recv = conn->last_recv;
1357 if (time_before_eq(last_recv + timeout + (conn->ping_timeout * HZ),
1358 jiffies)) {
1359 printk(KERN_ERR "ping timeout of %d secs expired, "
1360 "last rx %lu, last ping %lu, now %lu\n",
1361 conn->ping_timeout, last_recv,
1362 conn->last_ping, jiffies);
1363 spin_unlock(&session->lock);
1364 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1365 return;
1366 }
1367
1368 if (time_before_eq(last_recv + timeout, jiffies)) {
1369 if (time_before_eq(conn->last_ping, last_recv)) {
1370 /* send a ping to try to provoke some traffic */
1371 debug_scsi("Sending nopout as ping on conn %p\n", conn);
1372 iscsi_send_nopout(conn, NULL);
1373 }
1374 next_timeout = last_recv + timeout + (conn->ping_timeout * HZ);
1375 } else {
1376 next_timeout = last_recv + timeout;
1377 }
1378
1379 if (next_timeout) {
1380 debug_scsi("Setting next tmo %lu\n", next_timeout);
1381 mod_timer(&conn->transport_timer, next_timeout);
1382 }
1383done:
1384 spin_unlock(&session->lock);
1385}
1386
843c0a8a
MC
1387static void iscsi_prep_abort_task_pdu(struct iscsi_cmd_task *ctask,
1388 struct iscsi_tm *hdr)
1389{
1390 memset(hdr, 0, sizeof(*hdr));
1391 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
1392 hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK;
1393 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
1394 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
1395 hdr->rtt = ctask->hdr->itt;
1396 hdr->refcmdsn = ctask->hdr->cmdsn;
1397}
1398
7996a778
MC
1399int iscsi_eh_abort(struct scsi_cmnd *sc)
1400{
6724add1
MC
1401 struct Scsi_Host *host = sc->device->host;
1402 struct iscsi_session *session = iscsi_hostdata(host->hostdata);
f47f2cf5 1403 struct iscsi_conn *conn;
843c0a8a
MC
1404 struct iscsi_cmd_task *ctask;
1405 struct iscsi_tm *hdr;
1406 int rc, age;
7996a778 1407
6724add1
MC
1408 mutex_lock(&session->eh_mutex);
1409 spin_lock_bh(&session->lock);
f47f2cf5
MC
1410 /*
1411 * if session was ISCSI_STATE_IN_RECOVERY then we may not have
1412 * got the command.
1413 */
1414 if (!sc->SCp.ptr) {
1415 debug_scsi("sc never reached iscsi layer or it completed.\n");
6724add1
MC
1416 spin_unlock_bh(&session->lock);
1417 mutex_unlock(&session->eh_mutex);
f47f2cf5
MC
1418 return SUCCESS;
1419 }
1420
7996a778
MC
1421 /*
1422 * If we are not logged in or we have started a new session
1423 * then let the host reset code handle this
1424 */
843c0a8a
MC
1425 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN ||
1426 sc->SCp.phase != session->age) {
1427 spin_unlock_bh(&session->lock);
1428 mutex_unlock(&session->eh_mutex);
1429 return FAILED;
1430 }
1431
1432 conn = session->leadconn;
1433 conn->eh_abort_cnt++;
1434 age = session->age;
1435
1436 ctask = (struct iscsi_cmd_task *)sc->SCp.ptr;
1437 debug_scsi("aborting [sc %p itt 0x%x]\n", sc, ctask->itt);
7996a778
MC
1438
1439 /* ctask completed before time out */
7ea8b828 1440 if (!ctask->sc) {
7ea8b828 1441 debug_scsi("sc completed while abort in progress\n");
77a23c21 1442 goto success;
7ea8b828 1443 }
7996a778 1444
77a23c21
MC
1445 if (ctask->state == ISCSI_TASK_PENDING) {
1446 fail_command(conn, ctask, DID_ABORT << 16);
1447 goto success;
1448 }
7996a778 1449
843c0a8a
MC
1450 /* only have one tmf outstanding at a time */
1451 if (conn->tmf_state != TMF_INITIAL)
7996a778 1452 goto failed;
843c0a8a 1453 conn->tmf_state = TMF_QUEUED;
7996a778 1454
843c0a8a
MC
1455 hdr = &conn->tmhdr;
1456 iscsi_prep_abort_task_pdu(ctask, hdr);
1457
f6d5180c 1458 if (iscsi_exec_task_mgmt_fn(conn, hdr, age, session->abort_timeout)) {
843c0a8a
MC
1459 rc = FAILED;
1460 goto failed;
1461 }
1462
1463 switch (conn->tmf_state) {
1464 case TMF_SUCCESS:
77a23c21 1465 spin_unlock_bh(&session->lock);
6724add1 1466 iscsi_suspend_tx(conn);
77a23c21
MC
1467 /*
1468 * clean up task if aborted. grab the recv lock as a writer
1469 */
1470 write_lock_bh(conn->recv_lock);
1471 spin_lock(&session->lock);
1472 fail_command(conn, ctask, DID_ABORT << 16);
843c0a8a 1473 conn->tmf_state = TMF_INITIAL;
77a23c21
MC
1474 spin_unlock(&session->lock);
1475 write_unlock_bh(conn->recv_lock);
6724add1 1476 iscsi_start_tx(conn);
77a23c21 1477 goto success_unlocked;
843c0a8a
MC
1478 case TMF_TIMEDOUT:
1479 spin_unlock_bh(&session->lock);
1480 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1481 goto failed_unlocked;
1482 case TMF_NOT_FOUND:
1483 if (!sc->SCp.ptr) {
1484 conn->tmf_state = TMF_INITIAL;
7ea8b828 1485 /* ctask completed before tmf abort response */
7ea8b828 1486 debug_scsi("sc completed while abort in progress\n");
77a23c21 1487 goto success;
7ea8b828
MC
1488 }
1489 /* fall through */
1490 default:
843c0a8a
MC
1491 conn->tmf_state = TMF_INITIAL;
1492 goto failed;
7996a778
MC
1493 }
1494
77a23c21 1495success:
7996a778 1496 spin_unlock_bh(&session->lock);
77a23c21
MC
1497success_unlocked:
1498 debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
6724add1 1499 mutex_unlock(&session->eh_mutex);
7996a778
MC
1500 return SUCCESS;
1501
1502failed:
1503 spin_unlock_bh(&session->lock);
77a23c21 1504failed_unlocked:
843c0a8a
MC
1505 debug_scsi("abort failed [sc %p itt 0x%x]\n", sc,
1506 ctask ? ctask->itt : 0);
6724add1 1507 mutex_unlock(&session->eh_mutex);
7996a778
MC
1508 return FAILED;
1509}
1510EXPORT_SYMBOL_GPL(iscsi_eh_abort);
1511
843c0a8a
MC
1512static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
1513{
1514 memset(hdr, 0, sizeof(*hdr));
1515 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
1516 hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK;
1517 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
1518 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
f6d5180c 1519 hdr->rtt = RESERVED_ITT;
843c0a8a
MC
1520}
1521
1522int iscsi_eh_device_reset(struct scsi_cmnd *sc)
1523{
1524 struct Scsi_Host *host = sc->device->host;
1525 struct iscsi_session *session = iscsi_hostdata(host->hostdata);
1526 struct iscsi_conn *conn;
1527 struct iscsi_tm *hdr;
1528 int rc = FAILED;
1529
1530 debug_scsi("LU Reset [sc %p lun %u]\n", sc, sc->device->lun);
1531
1532 mutex_lock(&session->eh_mutex);
1533 spin_lock_bh(&session->lock);
1534 /*
1535 * Just check if we are not logged in. We cannot check for
1536 * the phase because the reset could come from a ioctl.
1537 */
1538 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
1539 goto unlock;
1540 conn = session->leadconn;
1541
1542 /* only have one tmf outstanding at a time */
1543 if (conn->tmf_state != TMF_INITIAL)
1544 goto unlock;
1545 conn->tmf_state = TMF_QUEUED;
1546
1547 hdr = &conn->tmhdr;
1548 iscsi_prep_lun_reset_pdu(sc, hdr);
1549
f6d5180c
MC
1550 if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
1551 session->lu_reset_timeout)) {
843c0a8a
MC
1552 rc = FAILED;
1553 goto unlock;
1554 }
1555
1556 switch (conn->tmf_state) {
1557 case TMF_SUCCESS:
1558 break;
1559 case TMF_TIMEDOUT:
1560 spin_unlock_bh(&session->lock);
1561 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1562 goto done;
1563 default:
1564 conn->tmf_state = TMF_INITIAL;
1565 goto unlock;
1566 }
1567
1568 rc = SUCCESS;
1569 spin_unlock_bh(&session->lock);
1570
1571 iscsi_suspend_tx(conn);
1572 /* need to grab the recv lock then session lock */
1573 write_lock_bh(conn->recv_lock);
1574 spin_lock(&session->lock);
1575 fail_all_commands(conn, sc->device->lun);
1576 conn->tmf_state = TMF_INITIAL;
1577 spin_unlock(&session->lock);
1578 write_unlock_bh(conn->recv_lock);
1579
1580 iscsi_start_tx(conn);
1581 goto done;
1582
1583unlock:
1584 spin_unlock_bh(&session->lock);
1585done:
1586 debug_scsi("iscsi_eh_device_reset %s\n",
1587 rc == SUCCESS ? "SUCCESS" : "FAILED");
1588 mutex_unlock(&session->eh_mutex);
1589 return rc;
1590}
1591EXPORT_SYMBOL_GPL(iscsi_eh_device_reset);
1592
6320377f
OK
1593/*
1594 * Pre-allocate a pool of @max items of @item_size. By default, the pool
1595 * should be accessed via kfifo_{get,put} on q->queue.
1596 * Optionally, the caller can obtain the array of object pointers
1597 * by passing in a non-NULL @items pointer
1598 */
7996a778 1599int
6320377f 1600iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size)
7996a778 1601{
6320377f 1602 int i, num_arrays = 1;
7996a778 1603
6320377f 1604 memset(q, 0, sizeof(*q));
7996a778
MC
1605
1606 q->max = max;
6320377f
OK
1607
1608 /* If the user passed an items pointer, he wants a copy of
1609 * the array. */
1610 if (items)
1611 num_arrays++;
1612 q->pool = kzalloc(num_arrays * max * sizeof(void*), GFP_KERNEL);
1613 if (q->pool == NULL)
1614 goto enomem;
7996a778
MC
1615
1616 q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
1617 GFP_KERNEL, NULL);
6320377f
OK
1618 if (q->queue == ERR_PTR(-ENOMEM))
1619 goto enomem;
7996a778
MC
1620
1621 for (i = 0; i < max; i++) {
6320377f 1622 q->pool[i] = kzalloc(item_size, GFP_KERNEL);
7996a778 1623 if (q->pool[i] == NULL) {
6320377f
OK
1624 q->max = i;
1625 goto enomem;
7996a778 1626 }
7996a778
MC
1627 __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
1628 }
6320377f
OK
1629
1630 if (items) {
1631 *items = q->pool + max;
1632 memcpy(*items, q->pool, max * sizeof(void *));
1633 }
1634
7996a778 1635 return 0;
6320377f
OK
1636
1637enomem:
1638 iscsi_pool_free(q);
1639 return -ENOMEM;
7996a778
MC
1640}
1641EXPORT_SYMBOL_GPL(iscsi_pool_init);
1642
6320377f 1643void iscsi_pool_free(struct iscsi_pool *q)
7996a778
MC
1644{
1645 int i;
1646
1647 for (i = 0; i < q->max; i++)
6320377f
OK
1648 kfree(q->pool[i]);
1649 if (q->pool)
1650 kfree(q->pool);
7996a778
MC
1651}
1652EXPORT_SYMBOL_GPL(iscsi_pool_free);
1653
1654/*
1655 * iSCSI Session's hostdata organization:
1656 *
1657 * *------------------* <== hostdata_session(host->hostdata)
1658 * | ptr to class sess|
1659 * |------------------| <== iscsi_hostdata(host->hostdata)
1660 * | iscsi_session |
1661 * *------------------*
1662 */
1663
1664#define hostdata_privsize(_sz) (sizeof(unsigned long) + _sz + \
1665 _sz % sizeof(unsigned long))
1666
1667#define hostdata_session(_hostdata) (iscsi_ptr(*(unsigned long *)_hostdata))
1668
1669/**
1670 * iscsi_session_setup - create iscsi cls session and host and session
1671 * @scsit: scsi transport template
1672 * @iscsit: iscsi transport template
1548271e
MC
1673 * @cmds_max: scsi host can queue
1674 * @qdepth: scsi host cmds per lun
1675 * @cmd_task_size: LLD ctask private data size
1676 * @mgmt_task_size: LLD mtask private data size
7996a778
MC
1677 * @initial_cmdsn: initial CmdSN
1678 * @hostno: host no allocated
1679 *
1680 * This can be used by software iscsi_transports that allocate
1681 * a session per scsi host.
1682 **/
1683struct iscsi_cls_session *
1684iscsi_session_setup(struct iscsi_transport *iscsit,
1685 struct scsi_transport_template *scsit,
1548271e 1686 uint16_t cmds_max, uint16_t qdepth,
7996a778
MC
1687 int cmd_task_size, int mgmt_task_size,
1688 uint32_t initial_cmdsn, uint32_t *hostno)
1689{
1690 struct Scsi_Host *shost;
1691 struct iscsi_session *session;
1692 struct iscsi_cls_session *cls_session;
1693 int cmd_i;
1694
1548271e
MC
1695 if (qdepth > ISCSI_MAX_CMD_PER_LUN || qdepth < 1) {
1696 if (qdepth != 0)
1697 printk(KERN_ERR "iscsi: invalid queue depth of %d. "
1698 "Queue depth must be between 1 and %d.\n",
1699 qdepth, ISCSI_MAX_CMD_PER_LUN);
1700 qdepth = ISCSI_DEF_CMD_PER_LUN;
1701 }
1702
1703 if (cmds_max < 2 || (cmds_max & (cmds_max - 1)) ||
1704 cmds_max >= ISCSI_MGMT_ITT_OFFSET) {
1705 if (cmds_max != 0)
1706 printk(KERN_ERR "iscsi: invalid can_queue of %d. "
1707 "can_queue must be a power of 2 and between "
1708 "2 and %d - setting to %d.\n", cmds_max,
1709 ISCSI_MGMT_ITT_OFFSET, ISCSI_DEF_XMIT_CMDS_MAX);
1710 cmds_max = ISCSI_DEF_XMIT_CMDS_MAX;
1711 }
1712
7996a778
MC
1713 shost = scsi_host_alloc(iscsit->host_template,
1714 hostdata_privsize(sizeof(*session)));
1715 if (!shost)
1716 return NULL;
1717
1548271e
MC
1718 /* the iscsi layer takes one task for reserve */
1719 shost->can_queue = cmds_max - 1;
1720 shost->cmd_per_lun = qdepth;
7996a778
MC
1721 shost->max_id = 1;
1722 shost->max_channel = 0;
1723 shost->max_lun = iscsit->max_lun;
1724 shost->max_cmd_len = iscsit->max_cmd_len;
1725 shost->transportt = scsit;
1726 shost->transportt->create_work_queue = 1;
f6d5180c 1727 shost->transportt->eh_timed_out = iscsi_eh_cmd_timed_out;
7996a778
MC
1728 *hostno = shost->host_no;
1729
1730 session = iscsi_hostdata(shost->hostdata);
1731 memset(session, 0, sizeof(struct iscsi_session));
1732 session->host = shost;
1733 session->state = ISCSI_STATE_FREE;
f6d5180c 1734 session->fast_abort = 1;
7996a778 1735 session->mgmtpool_max = ISCSI_MGMT_CMDS_MAX;
1548271e 1736 session->cmds_max = cmds_max;
e0726407 1737 session->queued_cmdsn = session->cmdsn = initial_cmdsn;
7996a778
MC
1738 session->exp_cmdsn = initial_cmdsn + 1;
1739 session->max_cmdsn = initial_cmdsn + 1;
1740 session->max_r2t = 1;
1741 session->tt = iscsit;
6724add1 1742 mutex_init(&session->eh_mutex);
7996a778
MC
1743
1744 /* initialize SCSI PDU commands pool */
1745 if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
1746 (void***)&session->cmds,
1747 cmd_task_size + sizeof(struct iscsi_cmd_task)))
1748 goto cmdpool_alloc_fail;
1749
1750 /* pre-format cmds pool with ITT */
1751 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
1752 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
1753
1754 if (cmd_task_size)
1755 ctask->dd_data = &ctask[1];
1756 ctask->itt = cmd_i;
b6c395ed 1757 INIT_LIST_HEAD(&ctask->running);
7996a778
MC
1758 }
1759
1760 spin_lock_init(&session->lock);
7996a778
MC
1761
1762 /* initialize immediate command pool */
1763 if (iscsi_pool_init(&session->mgmtpool, session->mgmtpool_max,
1764 (void***)&session->mgmt_cmds,
1765 mgmt_task_size + sizeof(struct iscsi_mgmt_task)))
1766 goto mgmtpool_alloc_fail;
1767
1768
1769 /* pre-format immediate cmds pool with ITT */
1770 for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
1771 struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
1772
1773 if (mgmt_task_size)
1774 mtask->dd_data = &mtask[1];
1775 mtask->itt = ISCSI_MGMT_ITT_OFFSET + cmd_i;
b6c395ed 1776 INIT_LIST_HEAD(&mtask->running);
7996a778
MC
1777 }
1778
1779 if (scsi_add_host(shost, NULL))
1780 goto add_host_fail;
1781
f53a88da
MC
1782 if (!try_module_get(iscsit->owner))
1783 goto cls_session_fail;
1784
6a8a0d36 1785 cls_session = iscsi_create_session(shost, iscsit, 0);
7996a778 1786 if (!cls_session)
f53a88da 1787 goto module_put;
7996a778
MC
1788 *(unsigned long*)shost->hostdata = (unsigned long)cls_session;
1789
1790 return cls_session;
1791
f53a88da
MC
1792module_put:
1793 module_put(iscsit->owner);
7996a778
MC
1794cls_session_fail:
1795 scsi_remove_host(shost);
1796add_host_fail:
6320377f 1797 iscsi_pool_free(&session->mgmtpool);
7996a778 1798mgmtpool_alloc_fail:
6320377f 1799 iscsi_pool_free(&session->cmdpool);
7996a778
MC
1800cmdpool_alloc_fail:
1801 scsi_host_put(shost);
1802 return NULL;
1803}
1804EXPORT_SYMBOL_GPL(iscsi_session_setup);
1805
1806/**
1807 * iscsi_session_teardown - destroy session, host, and cls_session
1808 * shost: scsi host
1809 *
1810 * This can be used by software iscsi_transports that allocate
1811 * a session per scsi host.
1812 **/
1813void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
1814{
1815 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
1816 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
63f75cc8 1817 struct module *owner = cls_session->transport->owner;
7996a778 1818
26974789 1819 iscsi_remove_session(cls_session);
7996a778
MC
1820 scsi_remove_host(shost);
1821
6320377f
OK
1822 iscsi_pool_free(&session->mgmtpool);
1823 iscsi_pool_free(&session->cmdpool);
7996a778 1824
b2c64167
MC
1825 kfree(session->password);
1826 kfree(session->password_in);
1827 kfree(session->username);
1828 kfree(session->username_in);
f3ff0c36 1829 kfree(session->targetname);
d8196ed2 1830 kfree(session->netdev);
0801c242 1831 kfree(session->hwaddress);
8ad5781a 1832 kfree(session->initiatorname);
f3ff0c36 1833
26974789 1834 iscsi_free_session(cls_session);
7996a778 1835 scsi_host_put(shost);
63f75cc8 1836 module_put(owner);
7996a778
MC
1837}
1838EXPORT_SYMBOL_GPL(iscsi_session_teardown);
1839
1840/**
1841 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
1842 * @cls_session: iscsi_cls_session
1843 * @conn_idx: cid
1844 **/
1845struct iscsi_cls_conn *
1846iscsi_conn_setup(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
1847{
1848 struct iscsi_session *session = class_to_transport_session(cls_session);
1849 struct iscsi_conn *conn;
1850 struct iscsi_cls_conn *cls_conn;
d36ab6f3 1851 char *data;
7996a778
MC
1852
1853 cls_conn = iscsi_create_conn(cls_session, conn_idx);
1854 if (!cls_conn)
1855 return NULL;
1856 conn = cls_conn->dd_data;
1857 memset(conn, 0, sizeof(*conn));
1858
1859 conn->session = session;
1860 conn->cls_conn = cls_conn;
1861 conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
1862 conn->id = conn_idx;
1863 conn->exp_statsn = 0;
843c0a8a 1864 conn->tmf_state = TMF_INITIAL;
f6d5180c
MC
1865
1866 init_timer(&conn->transport_timer);
1867 conn->transport_timer.data = (unsigned long)conn;
1868 conn->transport_timer.function = iscsi_check_transport_timeouts;
1869
7996a778
MC
1870 INIT_LIST_HEAD(&conn->run_list);
1871 INIT_LIST_HEAD(&conn->mgmt_run_list);
843c0a8a 1872 INIT_LIST_HEAD(&conn->mgmtqueue);
b6c395ed 1873 INIT_LIST_HEAD(&conn->xmitqueue);
843c0a8a 1874 INIT_LIST_HEAD(&conn->requeue);
c4028958 1875 INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
7996a778
MC
1876
1877 /* allocate login_mtask used for the login/text sequences */
1878 spin_lock_bh(&session->lock);
1879 if (!__kfifo_get(session->mgmtpool.queue,
1880 (void*)&conn->login_mtask,
1881 sizeof(void*))) {
1882 spin_unlock_bh(&session->lock);
1883 goto login_mtask_alloc_fail;
1884 }
1885 spin_unlock_bh(&session->lock);
1886
bf32ed33 1887 data = kmalloc(ISCSI_DEF_MAX_RECV_SEG_LEN, GFP_KERNEL);
d36ab6f3
MC
1888 if (!data)
1889 goto login_mtask_data_alloc_fail;
c8dc1e52 1890 conn->login_mtask->data = conn->data = data;
d36ab6f3 1891
843c0a8a 1892 init_timer(&conn->tmf_timer);
7996a778
MC
1893 init_waitqueue_head(&conn->ehwait);
1894
1895 return cls_conn;
1896
d36ab6f3
MC
1897login_mtask_data_alloc_fail:
1898 __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
1899 sizeof(void*));
7996a778 1900login_mtask_alloc_fail:
7996a778
MC
1901 iscsi_destroy_conn(cls_conn);
1902 return NULL;
1903}
1904EXPORT_SYMBOL_GPL(iscsi_conn_setup);
1905
1906/**
1907 * iscsi_conn_teardown - teardown iscsi connection
1908 * cls_conn: iscsi class connection
1909 *
1910 * TODO: we may need to make this into a two step process
1911 * like scsi-mls remove + put host
1912 */
1913void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
1914{
1915 struct iscsi_conn *conn = cls_conn->dd_data;
1916 struct iscsi_session *session = conn->session;
1917 unsigned long flags;
1918
f6d5180c
MC
1919 del_timer_sync(&conn->transport_timer);
1920
7996a778
MC
1921 spin_lock_bh(&session->lock);
1922 conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
1923 if (session->leadconn == conn) {
1924 /*
1925 * leading connection? then give up on recovery.
1926 */
1927 session->state = ISCSI_STATE_TERMINATE;
1928 wake_up(&conn->ehwait);
1929 }
1930 spin_unlock_bh(&session->lock);
1931
7996a778
MC
1932 /*
1933 * Block until all in-progress commands for this connection
1934 * time out or fail.
1935 */
1936 for (;;) {
1937 spin_lock_irqsave(session->host->host_lock, flags);
1938 if (!session->host->host_busy) { /* OK for ERL == 0 */
1939 spin_unlock_irqrestore(session->host->host_lock, flags);
1940 break;
1941 }
1942 spin_unlock_irqrestore(session->host->host_lock, flags);
1943 msleep_interruptible(500);
be2df72e
OG
1944 printk(KERN_INFO "iscsi: scsi conn_destroy(): host_busy %d "
1945 "host_failed %d\n", session->host->host_busy,
1946 session->host->host_failed);
7996a778
MC
1947 /*
1948 * force eh_abort() to unblock
1949 */
1950 wake_up(&conn->ehwait);
1951 }
1952
779ea120 1953 /* flush queued up work because we free the connection below */
843c0a8a 1954 iscsi_suspend_tx(conn);
779ea120 1955
7996a778 1956 spin_lock_bh(&session->lock);
c8dc1e52 1957 kfree(conn->data);
f3ff0c36 1958 kfree(conn->persistent_address);
7996a778
MC
1959 __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
1960 sizeof(void*));
e0726407 1961 if (session->leadconn == conn)
7996a778 1962 session->leadconn = NULL;
7996a778
MC
1963 spin_unlock_bh(&session->lock);
1964
7996a778
MC
1965 iscsi_destroy_conn(cls_conn);
1966}
1967EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
1968
1969int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
1970{
1971 struct iscsi_conn *conn = cls_conn->dd_data;
1972 struct iscsi_session *session = conn->session;
1973
ffd0436e 1974 if (!session) {
7996a778
MC
1975 printk(KERN_ERR "iscsi: can't start unbound connection\n");
1976 return -EPERM;
1977 }
1978
db98ccde
MC
1979 if ((session->imm_data_en || !session->initial_r2t_en) &&
1980 session->first_burst > session->max_burst) {
ffd0436e
MC
1981 printk("iscsi: invalid burst lengths: "
1982 "first_burst %d max_burst %d\n",
1983 session->first_burst, session->max_burst);
1984 return -EINVAL;
1985 }
1986
f6d5180c
MC
1987 if (conn->ping_timeout && !conn->recv_timeout) {
1988 printk(KERN_ERR "iscsi: invalid recv timeout of zero "
1989 "Using 5 seconds\n.");
1990 conn->recv_timeout = 5;
1991 }
1992
1993 if (conn->recv_timeout && !conn->ping_timeout) {
1994 printk(KERN_ERR "iscsi: invalid ping timeout of zero "
1995 "Using 5 seconds.\n");
1996 conn->ping_timeout = 5;
1997 }
1998
7996a778
MC
1999 spin_lock_bh(&session->lock);
2000 conn->c_stage = ISCSI_CONN_STARTED;
2001 session->state = ISCSI_STATE_LOGGED_IN;
e0726407 2002 session->queued_cmdsn = session->cmdsn;
7996a778 2003
f6d5180c
MC
2004 conn->last_recv = jiffies;
2005 conn->last_ping = jiffies;
2006 if (conn->recv_timeout && conn->ping_timeout)
2007 mod_timer(&conn->transport_timer,
2008 jiffies + (conn->recv_timeout * HZ));
2009
7996a778
MC
2010 switch(conn->stop_stage) {
2011 case STOP_CONN_RECOVER:
2012 /*
2013 * unblock eh_abort() if it is blocked. re-try all
2014 * commands after successful recovery
2015 */
7996a778 2016 conn->stop_stage = 0;
843c0a8a 2017 conn->tmf_state = TMF_INITIAL;
7996a778 2018 session->age++;
7996a778
MC
2019 spin_unlock_bh(&session->lock);
2020
2021 iscsi_unblock_session(session_to_cls(session));
2022 wake_up(&conn->ehwait);
2023 return 0;
2024 case STOP_CONN_TERM:
7996a778 2025 conn->stop_stage = 0;
7996a778
MC
2026 break;
2027 default:
2028 break;
2029 }
2030 spin_unlock_bh(&session->lock);
2031
2032 return 0;
2033}
2034EXPORT_SYMBOL_GPL(iscsi_conn_start);
2035
2036static void
2037flush_control_queues(struct iscsi_session *session, struct iscsi_conn *conn)
2038{
2039 struct iscsi_mgmt_task *mtask, *tmp;
2040
2041 /* handle pending */
843c0a8a
MC
2042 list_for_each_entry_safe(mtask, tmp, &conn->mgmtqueue, running) {
2043 debug_scsi("flushing pending mgmt task itt 0x%x\n", mtask->itt);
b3a7ea8d 2044 iscsi_free_mgmt_task(conn, mtask);
7996a778
MC
2045 }
2046
2047 /* handle running */
2048 list_for_each_entry_safe(mtask, tmp, &conn->mgmt_run_list, running) {
2049 debug_scsi("flushing running mgmt task itt 0x%x\n", mtask->itt);
b3a7ea8d 2050 iscsi_free_mgmt_task(conn, mtask);
7996a778
MC
2051 }
2052
2053 conn->mtask = NULL;
2054}
2055
656cffc9
MC
2056static void iscsi_start_session_recovery(struct iscsi_session *session,
2057 struct iscsi_conn *conn, int flag)
7996a778 2058{
ed2abc7f
MC
2059 int old_stop_stage;
2060
f6d5180c
MC
2061 del_timer_sync(&conn->transport_timer);
2062
6724add1 2063 mutex_lock(&session->eh_mutex);
7996a778 2064 spin_lock_bh(&session->lock);
ed2abc7f 2065 if (conn->stop_stage == STOP_CONN_TERM) {
7996a778 2066 spin_unlock_bh(&session->lock);
6724add1
MC
2067 mutex_unlock(&session->eh_mutex);
2068 return;
2069 }
2070
2071 /*
2072 * The LLD either freed/unset the lock on us, or userspace called
2073 * stop but did not create a proper connection (connection was never
2074 * bound or it was unbound then stop was called).
2075 */
2076 if (!conn->recv_lock) {
2077 spin_unlock_bh(&session->lock);
2078 mutex_unlock(&session->eh_mutex);
7996a778
MC
2079 return;
2080 }
ed2abc7f
MC
2081
2082 /*
2083 * When this is called for the in_login state, we only want to clean
67a61114
MC
2084 * up the login task and connection. We do not need to block and set
2085 * the recovery state again
ed2abc7f 2086 */
67a61114
MC
2087 if (flag == STOP_CONN_TERM)
2088 session->state = ISCSI_STATE_TERMINATE;
2089 else if (conn->stop_stage != STOP_CONN_RECOVER)
2090 session->state = ISCSI_STATE_IN_RECOVERY;
ed2abc7f
MC
2091
2092 old_stop_stage = conn->stop_stage;
7996a778 2093 conn->stop_stage = flag;
67a61114 2094 conn->c_stage = ISCSI_CONN_STOPPED;
7996a778 2095 spin_unlock_bh(&session->lock);
6724add1
MC
2096
2097 iscsi_suspend_tx(conn);
7996a778 2098
1c83469d
MC
2099 write_lock_bh(conn->recv_lock);
2100 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
2101 write_unlock_bh(conn->recv_lock);
7996a778 2102
7996a778
MC
2103 /*
2104 * for connection level recovery we should not calculate
2105 * header digest. conn->hdr_size used for optimization
2106 * in hdr_extract() and will be re-negotiated at
2107 * set_param() time.
2108 */
2109 if (flag == STOP_CONN_RECOVER) {
2110 conn->hdrdgst_en = 0;
2111 conn->datadgst_en = 0;
656cffc9 2112 if (session->state == ISCSI_STATE_IN_RECOVERY &&
67a61114
MC
2113 old_stop_stage != STOP_CONN_RECOVER) {
2114 debug_scsi("blocking session\n");
7996a778 2115 iscsi_block_session(session_to_cls(session));
67a61114 2116 }
7996a778 2117 }
656cffc9 2118
656cffc9
MC
2119 /*
2120 * flush queues.
2121 */
2122 spin_lock_bh(&session->lock);
843c0a8a 2123 fail_all_commands(conn, -1);
656cffc9
MC
2124 flush_control_queues(session, conn);
2125 spin_unlock_bh(&session->lock);
6724add1 2126 mutex_unlock(&session->eh_mutex);
7996a778 2127}
7996a778
MC
2128
2129void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
2130{
2131 struct iscsi_conn *conn = cls_conn->dd_data;
2132 struct iscsi_session *session = conn->session;
2133
2134 switch (flag) {
2135 case STOP_CONN_RECOVER:
2136 case STOP_CONN_TERM:
2137 iscsi_start_session_recovery(session, conn, flag);
8d2860b3 2138 break;
7996a778 2139 default:
be2df72e 2140 printk(KERN_ERR "iscsi: invalid stop flag %d\n", flag);
7996a778
MC
2141 }
2142}
2143EXPORT_SYMBOL_GPL(iscsi_conn_stop);
2144
2145int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
2146 struct iscsi_cls_conn *cls_conn, int is_leading)
2147{
2148 struct iscsi_session *session = class_to_transport_session(cls_session);
98644047 2149 struct iscsi_conn *conn = cls_conn->dd_data;
7996a778 2150
7996a778 2151 spin_lock_bh(&session->lock);
7996a778
MC
2152 if (is_leading)
2153 session->leadconn = conn;
98644047 2154 spin_unlock_bh(&session->lock);
7996a778
MC
2155
2156 /*
2157 * Unblock xmitworker(), Login Phase will pass through.
2158 */
2159 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
2160 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
2161 return 0;
2162}
2163EXPORT_SYMBOL_GPL(iscsi_conn_bind);
2164
a54a52ca
MC
2165
2166int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
2167 enum iscsi_param param, char *buf, int buflen)
2168{
2169 struct iscsi_conn *conn = cls_conn->dd_data;
2170 struct iscsi_session *session = conn->session;
2171 uint32_t value;
2172
2173 switch(param) {
843c0a8a
MC
2174 case ISCSI_PARAM_FAST_ABORT:
2175 sscanf(buf, "%d", &session->fast_abort);
2176 break;
f6d5180c
MC
2177 case ISCSI_PARAM_ABORT_TMO:
2178 sscanf(buf, "%d", &session->abort_timeout);
2179 break;
2180 case ISCSI_PARAM_LU_RESET_TMO:
2181 sscanf(buf, "%d", &session->lu_reset_timeout);
2182 break;
2183 case ISCSI_PARAM_PING_TMO:
2184 sscanf(buf, "%d", &conn->ping_timeout);
2185 break;
2186 case ISCSI_PARAM_RECV_TMO:
2187 sscanf(buf, "%d", &conn->recv_timeout);
2188 break;
a54a52ca
MC
2189 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2190 sscanf(buf, "%d", &conn->max_recv_dlength);
2191 break;
2192 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2193 sscanf(buf, "%d", &conn->max_xmit_dlength);
2194 break;
2195 case ISCSI_PARAM_HDRDGST_EN:
2196 sscanf(buf, "%d", &conn->hdrdgst_en);
2197 break;
2198 case ISCSI_PARAM_DATADGST_EN:
2199 sscanf(buf, "%d", &conn->datadgst_en);
2200 break;
2201 case ISCSI_PARAM_INITIAL_R2T_EN:
2202 sscanf(buf, "%d", &session->initial_r2t_en);
2203 break;
2204 case ISCSI_PARAM_MAX_R2T:
2205 sscanf(buf, "%d", &session->max_r2t);
2206 break;
2207 case ISCSI_PARAM_IMM_DATA_EN:
2208 sscanf(buf, "%d", &session->imm_data_en);
2209 break;
2210 case ISCSI_PARAM_FIRST_BURST:
2211 sscanf(buf, "%d", &session->first_burst);
2212 break;
2213 case ISCSI_PARAM_MAX_BURST:
2214 sscanf(buf, "%d", &session->max_burst);
2215 break;
2216 case ISCSI_PARAM_PDU_INORDER_EN:
2217 sscanf(buf, "%d", &session->pdu_inorder_en);
2218 break;
2219 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2220 sscanf(buf, "%d", &session->dataseq_inorder_en);
2221 break;
2222 case ISCSI_PARAM_ERL:
2223 sscanf(buf, "%d", &session->erl);
2224 break;
2225 case ISCSI_PARAM_IFMARKER_EN:
2226 sscanf(buf, "%d", &value);
2227 BUG_ON(value);
2228 break;
2229 case ISCSI_PARAM_OFMARKER_EN:
2230 sscanf(buf, "%d", &value);
2231 BUG_ON(value);
2232 break;
2233 case ISCSI_PARAM_EXP_STATSN:
2234 sscanf(buf, "%u", &conn->exp_statsn);
2235 break;
b2c64167
MC
2236 case ISCSI_PARAM_USERNAME:
2237 kfree(session->username);
2238 session->username = kstrdup(buf, GFP_KERNEL);
2239 if (!session->username)
2240 return -ENOMEM;
2241 break;
2242 case ISCSI_PARAM_USERNAME_IN:
2243 kfree(session->username_in);
2244 session->username_in = kstrdup(buf, GFP_KERNEL);
2245 if (!session->username_in)
2246 return -ENOMEM;
2247 break;
2248 case ISCSI_PARAM_PASSWORD:
2249 kfree(session->password);
2250 session->password = kstrdup(buf, GFP_KERNEL);
2251 if (!session->password)
2252 return -ENOMEM;
2253 break;
2254 case ISCSI_PARAM_PASSWORD_IN:
2255 kfree(session->password_in);
2256 session->password_in = kstrdup(buf, GFP_KERNEL);
2257 if (!session->password_in)
2258 return -ENOMEM;
2259 break;
a54a52ca
MC
2260 case ISCSI_PARAM_TARGET_NAME:
2261 /* this should not change between logins */
2262 if (session->targetname)
2263 break;
2264
2265 session->targetname = kstrdup(buf, GFP_KERNEL);
2266 if (!session->targetname)
2267 return -ENOMEM;
2268 break;
2269 case ISCSI_PARAM_TPGT:
2270 sscanf(buf, "%d", &session->tpgt);
2271 break;
2272 case ISCSI_PARAM_PERSISTENT_PORT:
2273 sscanf(buf, "%d", &conn->persistent_port);
2274 break;
2275 case ISCSI_PARAM_PERSISTENT_ADDRESS:
2276 /*
2277 * this is the address returned in discovery so it should
2278 * not change between logins.
2279 */
2280 if (conn->persistent_address)
2281 break;
2282
2283 conn->persistent_address = kstrdup(buf, GFP_KERNEL);
2284 if (!conn->persistent_address)
2285 return -ENOMEM;
2286 break;
2287 default:
2288 return -ENOSYS;
2289 }
2290
2291 return 0;
2292}
2293EXPORT_SYMBOL_GPL(iscsi_set_param);
2294
2295int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
2296 enum iscsi_param param, char *buf)
2297{
2298 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
2299 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
2300 int len;
2301
2302 switch(param) {
843c0a8a
MC
2303 case ISCSI_PARAM_FAST_ABORT:
2304 len = sprintf(buf, "%d\n", session->fast_abort);
2305 break;
f6d5180c
MC
2306 case ISCSI_PARAM_ABORT_TMO:
2307 len = sprintf(buf, "%d\n", session->abort_timeout);
2308 break;
2309 case ISCSI_PARAM_LU_RESET_TMO:
2310 len = sprintf(buf, "%d\n", session->lu_reset_timeout);
2311 break;
a54a52ca
MC
2312 case ISCSI_PARAM_INITIAL_R2T_EN:
2313 len = sprintf(buf, "%d\n", session->initial_r2t_en);
2314 break;
2315 case ISCSI_PARAM_MAX_R2T:
2316 len = sprintf(buf, "%hu\n", session->max_r2t);
2317 break;
2318 case ISCSI_PARAM_IMM_DATA_EN:
2319 len = sprintf(buf, "%d\n", session->imm_data_en);
2320 break;
2321 case ISCSI_PARAM_FIRST_BURST:
2322 len = sprintf(buf, "%u\n", session->first_burst);
2323 break;
2324 case ISCSI_PARAM_MAX_BURST:
2325 len = sprintf(buf, "%u\n", session->max_burst);
2326 break;
2327 case ISCSI_PARAM_PDU_INORDER_EN:
2328 len = sprintf(buf, "%d\n", session->pdu_inorder_en);
2329 break;
2330 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2331 len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
2332 break;
2333 case ISCSI_PARAM_ERL:
2334 len = sprintf(buf, "%d\n", session->erl);
2335 break;
2336 case ISCSI_PARAM_TARGET_NAME:
2337 len = sprintf(buf, "%s\n", session->targetname);
2338 break;
2339 case ISCSI_PARAM_TPGT:
2340 len = sprintf(buf, "%d\n", session->tpgt);
2341 break;
b2c64167
MC
2342 case ISCSI_PARAM_USERNAME:
2343 len = sprintf(buf, "%s\n", session->username);
2344 break;
2345 case ISCSI_PARAM_USERNAME_IN:
2346 len = sprintf(buf, "%s\n", session->username_in);
2347 break;
2348 case ISCSI_PARAM_PASSWORD:
2349 len = sprintf(buf, "%s\n", session->password);
2350 break;
2351 case ISCSI_PARAM_PASSWORD_IN:
2352 len = sprintf(buf, "%s\n", session->password_in);
2353 break;
a54a52ca
MC
2354 default:
2355 return -ENOSYS;
2356 }
2357
2358 return len;
2359}
2360EXPORT_SYMBOL_GPL(iscsi_session_get_param);
2361
2362int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
2363 enum iscsi_param param, char *buf)
2364{
2365 struct iscsi_conn *conn = cls_conn->dd_data;
2366 int len;
2367
2368 switch(param) {
f6d5180c
MC
2369 case ISCSI_PARAM_PING_TMO:
2370 len = sprintf(buf, "%u\n", conn->ping_timeout);
2371 break;
2372 case ISCSI_PARAM_RECV_TMO:
2373 len = sprintf(buf, "%u\n", conn->recv_timeout);
2374 break;
a54a52ca
MC
2375 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2376 len = sprintf(buf, "%u\n", conn->max_recv_dlength);
2377 break;
2378 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2379 len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
2380 break;
2381 case ISCSI_PARAM_HDRDGST_EN:
2382 len = sprintf(buf, "%d\n", conn->hdrdgst_en);
2383 break;
2384 case ISCSI_PARAM_DATADGST_EN:
2385 len = sprintf(buf, "%d\n", conn->datadgst_en);
2386 break;
2387 case ISCSI_PARAM_IFMARKER_EN:
2388 len = sprintf(buf, "%d\n", conn->ifmarker_en);
2389 break;
2390 case ISCSI_PARAM_OFMARKER_EN:
2391 len = sprintf(buf, "%d\n", conn->ofmarker_en);
2392 break;
2393 case ISCSI_PARAM_EXP_STATSN:
2394 len = sprintf(buf, "%u\n", conn->exp_statsn);
2395 break;
2396 case ISCSI_PARAM_PERSISTENT_PORT:
2397 len = sprintf(buf, "%d\n", conn->persistent_port);
2398 break;
2399 case ISCSI_PARAM_PERSISTENT_ADDRESS:
2400 len = sprintf(buf, "%s\n", conn->persistent_address);
2401 break;
2402 default:
2403 return -ENOSYS;
2404 }
2405
2406 return len;
2407}
2408EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
2409
0801c242
MC
2410int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2411 char *buf)
2412{
2413 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
2414 int len;
2415
2416 switch (param) {
d8196ed2
MC
2417 case ISCSI_HOST_PARAM_NETDEV_NAME:
2418 if (!session->netdev)
2419 len = sprintf(buf, "%s\n", "default");
2420 else
2421 len = sprintf(buf, "%s\n", session->netdev);
2422 break;
0801c242
MC
2423 case ISCSI_HOST_PARAM_HWADDRESS:
2424 if (!session->hwaddress)
2425 len = sprintf(buf, "%s\n", "default");
2426 else
2427 len = sprintf(buf, "%s\n", session->hwaddress);
2428 break;
8ad5781a
MC
2429 case ISCSI_HOST_PARAM_INITIATOR_NAME:
2430 if (!session->initiatorname)
2431 len = sprintf(buf, "%s\n", "unknown");
2432 else
2433 len = sprintf(buf, "%s\n", session->initiatorname);
2434 break;
2435
0801c242
MC
2436 default:
2437 return -ENOSYS;
2438 }
2439
2440 return len;
2441}
2442EXPORT_SYMBOL_GPL(iscsi_host_get_param);
2443
2444int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2445 char *buf, int buflen)
2446{
2447 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
2448
2449 switch (param) {
d8196ed2
MC
2450 case ISCSI_HOST_PARAM_NETDEV_NAME:
2451 if (!session->netdev)
2452 session->netdev = kstrdup(buf, GFP_KERNEL);
2453 break;
0801c242
MC
2454 case ISCSI_HOST_PARAM_HWADDRESS:
2455 if (!session->hwaddress)
2456 session->hwaddress = kstrdup(buf, GFP_KERNEL);
2457 break;
8ad5781a
MC
2458 case ISCSI_HOST_PARAM_INITIATOR_NAME:
2459 if (!session->initiatorname)
2460 session->initiatorname = kstrdup(buf, GFP_KERNEL);
2461 break;
0801c242
MC
2462 default:
2463 return -ENOSYS;
2464 }
2465
2466 return 0;
2467}
2468EXPORT_SYMBOL_GPL(iscsi_host_set_param);
2469
7996a778
MC
2470MODULE_AUTHOR("Mike Christie");
2471MODULE_DESCRIPTION("iSCSI library functions");
2472MODULE_LICENSE("GPL");