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