blob: b17081bed128a379984da9d10e4d7670ded5c3cc [file] [log] [blame]
Mike Christie7996a772006-04-06 21:13:41 -05001/*
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>
Mike Christie7996a772006-04-06 21:13:41 -050025#include <linux/kfifo.h>
26#include <linux/delay.h>
Mike Christie8eb00532007-02-28 17:32:19 -060027#include <asm/unaligned.h>
Mike Christie7996a772006-04-06 21:13:41 -050028#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
Mike Christie77a23c22007-05-30 12:57:18 -050048/* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
49#define SNA32_CHECK 2147483648UL
Mike Christie7996a772006-04-06 21:13:41 -050050
Mike Christie77a23c22007-05-30 12:57:18 -050051static 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)
Mike Christie7996a772006-04-06 21:13:41 -050066{
67 uint32_t max_cmdsn = be32_to_cpu(hdr->max_cmdsn);
68 uint32_t exp_cmdsn = be32_to_cpu(hdr->exp_cmdsn);
69
Mike Christie77a23c22007-05-30 12:57:18 -050070 /*
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))
Mike Christie7996a772006-04-06 21:13:41 -050079 session->exp_cmdsn = exp_cmdsn;
80
Mike Christie77a23c22007-05-30 12:57:18 -050081 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) ||
Mike Christie843c0a82007-12-13 12:43:20 -060089 !list_empty(&session->leadconn->mgmtqueue))
Mike Christie77a23c22007-05-30 12:57:18 -050090 scsi_queue_work(session->host,
91 &session->leadconn->xmitwork);
92 }
Mike Christie7996a772006-04-06 21:13:41 -050093}
Mike Christie77a23c22007-05-30 12:57:18 -050094EXPORT_SYMBOL_GPL(iscsi_update_cmdsn);
Mike Christie7996a772006-04-06 21:13:41 -050095
96void iscsi_prep_unsolicit_data_pdu(struct iscsi_cmd_task *ctask,
Mike Christieffd04362006-08-31 18:09:24 -040097 struct iscsi_data *hdr)
Mike Christie7996a772006-04-06 21:13:41 -050098{
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);
Mike Christieffd04362006-08-31 18:09:24 -0400110 hdr->offset = cpu_to_be32(ctask->unsol_offset);
Mike Christie7996a772006-04-06 21:13:41 -0500111
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;
Mike Christieffd04362006-08-31 18:09:24 -0400115 ctask->unsol_offset += ctask->data_count;
Mike Christie7996a772006-04-06 21:13:41 -0500116 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
Boaz Harrosh004d6532007-12-13 12:43:23 -0600125static 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
Mike Christie7996a772006-04-06 21:13:41 -0500139/**
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 */
Boaz Harrosh004d6532007-12-13 12:43:23 -0600146static int iscsi_prep_scsi_cmd_pdu(struct iscsi_cmd_task *ctask)
Mike Christie7996a772006-04-06 21:13:41 -0500147{
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;
Boaz Harrosh004d6532007-12-13 12:43:23 -0600152 unsigned hdrlength;
153 int rc;
Mike Christie7996a772006-04-06 21:13:41 -0500154
Boaz Harrosh004d6532007-12-13 12:43:23 -0600155 ctask->hdr_len = 0;
156 rc = iscsi_add_hdr(ctask, sizeof(*hdr));
157 if (rc)
158 return rc;
Mike Christie7996a772006-04-06 21:13:41 -0500159 hdr->opcode = ISCSI_OP_SCSI_CMD;
160 hdr->flags = ISCSI_ATTR_SIMPLE;
161 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
Al Virob4377352007-02-09 16:39:40 +0000162 hdr->itt = build_itt(ctask->itt, conn->id, session->age);
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900163 hdr->data_length = cpu_to_be32(scsi_bufflen(sc));
Mike Christie7996a772006-04-06 21:13:41 -0500164 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);
Mike Christied473cc72007-05-30 12:57:14 -0500168 if (sc->cmd_len < MAX_COMMAND_SIZE)
169 memset(&hdr->cdb[sc->cmd_len], 0,
170 MAX_COMMAND_SIZE - sc->cmd_len);
Mike Christie7996a772006-04-06 21:13:41 -0500171
Mike Christieffd04362006-08-31 18:09:24 -0400172 ctask->data_count = 0;
Mike Christie218432c2007-05-30 12:57:17 -0500173 ctask->imm_count = 0;
Mike Christie7996a772006-04-06 21:13:41 -0500174 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 */
Mike Christie7996a772006-04-06 21:13:41 -0500190 ctask->unsol_count = 0;
Mike Christieffd04362006-08-31 18:09:24 -0400191 ctask->unsol_offset = 0;
Mike Christie7996a772006-04-06 21:13:41 -0500192 ctask->unsol_datasn = 0;
193
194 if (session->imm_data_en) {
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900195 if (scsi_bufflen(sc) >= session->first_burst)
Mike Christie7996a772006-04-06 21:13:41 -0500196 ctask->imm_count = min(session->first_burst,
197 conn->max_xmit_dlength);
198 else
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900199 ctask->imm_count = min(scsi_bufflen(sc),
Mike Christie7996a772006-04-06 21:13:41 -0500200 conn->max_xmit_dlength);
201 hton24(ctask->hdr->dlength, ctask->imm_count);
202 } else
203 zero_data(ctask->hdr->dlength);
204
Mike Christieffd04362006-08-31 18:09:24 -0400205 if (!session->initial_r2t_en) {
Mike Christie857ae0b2007-05-30 12:57:15 -0500206 ctask->unsol_count = min((session->first_burst),
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900207 (scsi_bufflen(sc))) - ctask->imm_count;
Mike Christieffd04362006-08-31 18:09:24 -0400208 ctask->unsol_offset = ctask->imm_count;
209 }
210
Mike Christie7996a772006-04-06 21:13:41 -0500211 if (!ctask->unsol_count)
212 /* No unsolicit Data-Out's */
213 ctask->hdr->flags |= ISCSI_FLAG_CMD_FINAL;
214 } else {
Mike Christie7996a772006-04-06 21:13:41 -0500215 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
Boaz Harrosh004d6532007-12-13 12:43:23 -0600222 /* 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
Mike Christie7996a772006-04-06 21:13:41 -0500231 conn->scsicmd_pdus_cnt++;
Mike Christie77a23c22007-05-30 12:57:18 -0500232
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",
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900236 conn->id, sc, sc->cmnd[0], ctask->itt, scsi_bufflen(sc),
Mike Christie77a23c22007-05-30 12:57:18 -0500237 session->cmdsn, session->max_cmdsn - session->exp_cmdsn + 1);
Boaz Harrosh004d6532007-12-13 12:43:23 -0600238 return 0;
Mike Christie7996a772006-04-06 21:13:41 -0500239}
Mike Christie7996a772006-04-06 21:13:41 -0500240
241/**
242 * iscsi_complete_command - return command back to scsi-ml
Mike Christie7996a772006-04-06 21:13:41 -0500243 * @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 */
Mike Christie60ecebf2006-08-31 18:09:25 -0400249static void iscsi_complete_command(struct iscsi_cmd_task *ctask)
Mike Christie7996a772006-04-06 21:13:41 -0500250{
Mike Christie60ecebf2006-08-31 18:09:25 -0400251 struct iscsi_session *session = ctask->conn->session;
Mike Christie7996a772006-04-06 21:13:41 -0500252 struct scsi_cmnd *sc = ctask->sc;
253
Mike Christieb6c395e2006-07-24 15:47:15 -0500254 ctask->state = ISCSI_TASK_COMPLETED;
Mike Christie7996a772006-04-06 21:13:41 -0500255 ctask->sc = NULL;
Mike Christief47f2cf2006-08-31 18:09:33 -0400256 /* SCSI eh reuses commands to verify us */
257 sc->SCp.ptr = NULL;
Mike Christie7996a772006-04-06 21:13:41 -0500258 list_del_init(&ctask->running);
259 __kfifo_put(session->cmdpool.queue, (void*)&ctask, sizeof(void*));
260 sc->scsi_done(sc);
261}
262
Mike Christie60ecebf2006-08-31 18:09:25 -0400263static void __iscsi_get_ctask(struct iscsi_cmd_task *ctask)
264{
265 atomic_inc(&ctask->refcount);
266}
267
Mike Christie60ecebf2006-08-31 18:09:25 -0400268static void __iscsi_put_ctask(struct iscsi_cmd_task *ctask)
269{
Mike Christiee648f632006-08-31 18:09:34 -0400270 if (atomic_dec_and_test(&ctask->refcount))
Mike Christie60ecebf2006-08-31 18:09:25 -0400271 iscsi_complete_command(ctask);
Mike Christie60ecebf2006-08-31 18:09:25 -0400272}
273
Mike Christieb3a7ea82007-12-13 12:43:26 -0600274/*
275 * session lock must be held
276 */
277static void fail_command(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
278 int err)
279{
280 struct scsi_cmnd *sc;
281
282 sc = ctask->sc;
283 if (!sc)
284 return;
285
286 if (ctask->state == ISCSI_TASK_PENDING)
287 /*
288 * cmd never made it to the xmit thread, so we should not count
289 * the cmd in the sequencing
290 */
291 conn->session->queued_cmdsn--;
292 else
293 conn->session->tt->cleanup_cmd_task(conn, ctask);
294
295 sc->result = err;
296 scsi_set_resid(sc, scsi_bufflen(sc));
297 if (conn->ctask == ctask)
298 conn->ctask = NULL;
299 /* release ref from queuecommand */
300 __iscsi_put_ctask(ctask);
301}
302
303/**
304 * iscsi_free_mgmt_task - return mgmt task back to pool
305 * @conn: iscsi connection
306 * @mtask: mtask
307 *
308 * Must be called with session lock.
309 */
310void iscsi_free_mgmt_task(struct iscsi_conn *conn,
311 struct iscsi_mgmt_task *mtask)
312{
313 list_del_init(&mtask->running);
314 if (conn->login_mtask == mtask)
315 return;
Mike Christief6d51802007-12-13 12:43:30 -0600316
317 if (conn->ping_mtask == mtask)
318 conn->ping_mtask = NULL;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600319 __kfifo_put(conn->session->mgmtpool.queue,
320 (void*)&mtask, sizeof(void*));
321}
322EXPORT_SYMBOL_GPL(iscsi_free_mgmt_task);
323
Mike Christief6d51802007-12-13 12:43:30 -0600324static struct iscsi_mgmt_task *
325__iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
326 char *data, uint32_t data_size)
327{
328 struct iscsi_session *session = conn->session;
329 struct iscsi_mgmt_task *mtask;
330
331 if (session->state == ISCSI_STATE_TERMINATE)
332 return NULL;
333
334 if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) ||
335 hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
336 /*
337 * Login and Text are sent serially, in
338 * request-followed-by-response sequence.
339 * Same mtask can be used. Same ITT must be used.
340 * Note that login_mtask is preallocated at conn_create().
341 */
342 mtask = conn->login_mtask;
343 else {
344 BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
345 BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
346
347 if (!__kfifo_get(session->mgmtpool.queue,
348 (void*)&mtask, sizeof(void*)))
349 return NULL;
350 }
351
352 if (data_size) {
353 memcpy(mtask->data, data, data_size);
354 mtask->data_count = data_size;
355 } else
356 mtask->data_count = 0;
357
358 memcpy(mtask->hdr, hdr, sizeof(struct iscsi_hdr));
359 INIT_LIST_HEAD(&mtask->running);
360 list_add_tail(&mtask->running, &conn->mgmtqueue);
361 return mtask;
362}
363
364int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
365 char *data, uint32_t data_size)
366{
367 struct iscsi_conn *conn = cls_conn->dd_data;
368 struct iscsi_session *session = conn->session;
369 int err = 0;
370
371 spin_lock_bh(&session->lock);
372 if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size))
373 err = -EPERM;
374 spin_unlock_bh(&session->lock);
375 scsi_queue_work(session->host, &conn->xmitwork);
376 return err;
377}
378EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
379
Mike Christie7996a772006-04-06 21:13:41 -0500380/**
381 * iscsi_cmd_rsp - SCSI Command Response processing
382 * @conn: iscsi connection
383 * @hdr: iscsi header
384 * @ctask: scsi command task
385 * @data: cmd data buffer
386 * @datalen: len of buffer
387 *
388 * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
389 * then completes the command and task.
390 **/
Mike Christie77a23c22007-05-30 12:57:18 -0500391static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
392 struct iscsi_cmd_task *ctask, char *data,
393 int datalen)
Mike Christie7996a772006-04-06 21:13:41 -0500394{
Mike Christie7996a772006-04-06 21:13:41 -0500395 struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)hdr;
396 struct iscsi_session *session = conn->session;
397 struct scsi_cmnd *sc = ctask->sc;
398
Mike Christie77a23c22007-05-30 12:57:18 -0500399 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
Mike Christie7996a772006-04-06 21:13:41 -0500400 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
401
402 sc->result = (DID_OK << 16) | rhdr->cmd_status;
403
404 if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
405 sc->result = DID_ERROR << 16;
406 goto out;
407 }
408
409 if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
Mike Christie9b80cb42006-12-17 12:10:28 -0600410 uint16_t senselen;
Mike Christie7996a772006-04-06 21:13:41 -0500411
412 if (datalen < 2) {
413invalid_datalen:
Or Gerlitzbe2df722006-05-02 19:46:43 -0500414 printk(KERN_ERR "iscsi: Got CHECK_CONDITION but "
415 "invalid data buffer size of %d\n", datalen);
Mike Christie7996a772006-04-06 21:13:41 -0500416 sc->result = DID_BAD_TARGET << 16;
417 goto out;
418 }
419
Mike Christie8eb00532007-02-28 17:32:19 -0600420 senselen = be16_to_cpu(get_unaligned((__be16 *) data));
Mike Christie7996a772006-04-06 21:13:41 -0500421 if (datalen < senselen)
422 goto invalid_datalen;
423
424 memcpy(sc->sense_buffer, data + 2,
Mike Christie9b80cb42006-12-17 12:10:28 -0600425 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
Mike Christie7996a772006-04-06 21:13:41 -0500426 debug_scsi("copied %d bytes of sense\n",
Mike Christie8eb00532007-02-28 17:32:19 -0600427 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
Mike Christie7996a772006-04-06 21:13:41 -0500428 }
429
Boaz Harrosh7207fea2007-12-13 12:43:22 -0600430 if (rhdr->flags & (ISCSI_FLAG_CMD_UNDERFLOW |
431 ISCSI_FLAG_CMD_OVERFLOW)) {
Mike Christie7996a772006-04-06 21:13:41 -0500432 int res_count = be32_to_cpu(rhdr->residual_count);
433
Boaz Harrosh7207fea2007-12-13 12:43:22 -0600434 if (res_count > 0 &&
435 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
436 res_count <= scsi_bufflen(sc)))
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900437 scsi_set_resid(sc, res_count);
Mike Christie7996a772006-04-06 21:13:41 -0500438 else
439 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
Boaz Harrosh7207fea2007-12-13 12:43:22 -0600440 } else if (rhdr->flags & (ISCSI_FLAG_CMD_BIDI_UNDERFLOW |
441 ISCSI_FLAG_CMD_BIDI_OVERFLOW))
Mike Christie7996a772006-04-06 21:13:41 -0500442 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
Mike Christie7996a772006-04-06 21:13:41 -0500443
444out:
445 debug_scsi("done [sc %lx res %d itt 0x%x]\n",
446 (long)sc, sc->result, ctask->itt);
447 conn->scsirsp_pdus_cnt++;
448
Mike Christie60ecebf2006-08-31 18:09:25 -0400449 __iscsi_put_ctask(ctask);
Mike Christie7996a772006-04-06 21:13:41 -0500450}
451
Mike Christie7ea8b822006-07-24 15:47:22 -0500452static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
453{
454 struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
455
456 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
457 conn->tmfrsp_pdus_cnt++;
458
Mike Christie843c0a82007-12-13 12:43:20 -0600459 if (conn->tmf_state != TMF_QUEUED)
Mike Christie7ea8b822006-07-24 15:47:22 -0500460 return;
461
462 if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
Mike Christie843c0a82007-12-13 12:43:20 -0600463 conn->tmf_state = TMF_SUCCESS;
Mike Christie7ea8b822006-07-24 15:47:22 -0500464 else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
Mike Christie843c0a82007-12-13 12:43:20 -0600465 conn->tmf_state = TMF_NOT_FOUND;
Mike Christie7ea8b822006-07-24 15:47:22 -0500466 else
Mike Christie843c0a82007-12-13 12:43:20 -0600467 conn->tmf_state = TMF_FAILED;
Mike Christie7ea8b822006-07-24 15:47:22 -0500468 wake_up(&conn->ehwait);
469}
470
Mike Christief6d51802007-12-13 12:43:30 -0600471static void iscsi_send_nopout(struct iscsi_conn *conn, struct iscsi_nopin *rhdr)
472{
473 struct iscsi_nopout hdr;
474 struct iscsi_mgmt_task *mtask;
475
476 if (!rhdr && conn->ping_mtask)
477 return;
478
479 memset(&hdr, 0, sizeof(struct iscsi_nopout));
480 hdr.opcode = ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE;
481 hdr.flags = ISCSI_FLAG_CMD_FINAL;
482
483 if (rhdr) {
484 memcpy(hdr.lun, rhdr->lun, 8);
485 hdr.ttt = rhdr->ttt;
486 hdr.itt = RESERVED_ITT;
487 } else
488 hdr.ttt = RESERVED_ITT;
489
490 mtask = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)&hdr, NULL, 0);
491 if (!mtask) {
492 printk(KERN_ERR "Could not send nopout\n");
493 return;
494 }
495
496 /* only track our nops */
497 if (!rhdr) {
498 conn->ping_mtask = mtask;
499 conn->last_ping = jiffies;
500 }
501 scsi_queue_work(conn->session->host, &conn->xmitwork);
502}
503
Mike Christie62f38302006-08-31 18:09:27 -0400504static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
505 char *data, int datalen)
506{
507 struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
508 struct iscsi_hdr rejected_pdu;
509 uint32_t itt;
510
511 conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
512
513 if (reject->reason == ISCSI_REASON_DATA_DIGEST_ERROR) {
514 if (ntoh24(reject->dlength) > datalen)
515 return ISCSI_ERR_PROTO;
516
517 if (ntoh24(reject->dlength) >= sizeof(struct iscsi_hdr)) {
518 memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
Al Virob4377352007-02-09 16:39:40 +0000519 itt = get_itt(rejected_pdu.itt);
Mike Christie62f38302006-08-31 18:09:27 -0400520 printk(KERN_ERR "itt 0x%x had pdu (op 0x%x) rejected "
521 "due to DataDigest error.\n", itt,
522 rejected_pdu.opcode);
523 }
524 }
525 return 0;
526}
527
Mike Christie7996a772006-04-06 21:13:41 -0500528/**
529 * __iscsi_complete_pdu - complete pdu
530 * @conn: iscsi conn
531 * @hdr: iscsi header
532 * @data: data buffer
533 * @datalen: len of data buffer
534 *
535 * Completes pdu processing by freeing any resources allocated at
536 * queuecommand or send generic. session lock must be held and verify
537 * itt must have been called.
538 */
539int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
540 char *data, int datalen)
541{
542 struct iscsi_session *session = conn->session;
543 int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
544 struct iscsi_cmd_task *ctask;
545 struct iscsi_mgmt_task *mtask;
546 uint32_t itt;
547
Mike Christief6d51802007-12-13 12:43:30 -0600548 conn->last_recv = jiffies;
Al Virob4377352007-02-09 16:39:40 +0000549 if (hdr->itt != RESERVED_ITT)
550 itt = get_itt(hdr->itt);
Mike Christie7996a772006-04-06 21:13:41 -0500551 else
Al Virob4377352007-02-09 16:39:40 +0000552 itt = ~0U;
Mike Christie7996a772006-04-06 21:13:41 -0500553
554 if (itt < session->cmds_max) {
555 ctask = session->cmds[itt];
556
557 debug_scsi("cmdrsp [op 0x%x cid %d itt 0x%x len %d]\n",
558 opcode, conn->id, ctask->itt, datalen);
559
560 switch(opcode) {
561 case ISCSI_OP_SCSI_CMD_RSP:
562 BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
Mike Christie77a23c22007-05-30 12:57:18 -0500563 iscsi_scsi_cmd_rsp(conn, hdr, ctask, data,
564 datalen);
Mike Christie7996a772006-04-06 21:13:41 -0500565 break;
566 case ISCSI_OP_SCSI_DATA_IN:
567 BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
568 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
569 conn->scsirsp_pdus_cnt++;
Mike Christie60ecebf2006-08-31 18:09:25 -0400570 __iscsi_put_ctask(ctask);
Mike Christie7996a772006-04-06 21:13:41 -0500571 }
572 break;
573 case ISCSI_OP_R2T:
574 /* LLD handles this for now */
575 break;
576 default:
577 rc = ISCSI_ERR_BAD_OPCODE;
578 break;
579 }
580 } else if (itt >= ISCSI_MGMT_ITT_OFFSET &&
581 itt < ISCSI_MGMT_ITT_OFFSET + session->mgmtpool_max) {
582 mtask = session->mgmt_cmds[itt - ISCSI_MGMT_ITT_OFFSET];
583
584 debug_scsi("immrsp [op 0x%x cid %d itt 0x%x len %d]\n",
585 opcode, conn->id, mtask->itt, datalen);
586
Mike Christie77a23c22007-05-30 12:57:18 -0500587 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
Mike Christie7996a772006-04-06 21:13:41 -0500588 switch(opcode) {
Mike Christie8d2860b2006-05-02 19:46:47 -0500589 case ISCSI_OP_LOGOUT_RSP:
Mike Christiec8dc1e52006-07-24 15:47:39 -0500590 if (datalen) {
591 rc = ISCSI_ERR_PROTO;
592 break;
593 }
Mike Christie8d2860b2006-05-02 19:46:47 -0500594 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
595 /* fall through */
Mike Christie7996a772006-04-06 21:13:41 -0500596 case ISCSI_OP_LOGIN_RSP:
597 case ISCSI_OP_TEXT_RSP:
Mike Christie8d2860b2006-05-02 19:46:47 -0500598 /*
599 * login related PDU's exp_statsn is handled in
600 * userspace
601 */
Mike Christie40527af2006-07-24 15:47:45 -0500602 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
603 rc = ISCSI_ERR_CONN_FAILED;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600604 iscsi_free_mgmt_task(conn, mtask);
Mike Christie7996a772006-04-06 21:13:41 -0500605 break;
606 case ISCSI_OP_SCSI_TMFUNC_RSP:
Mike Christie7996a772006-04-06 21:13:41 -0500607 if (datalen) {
608 rc = ISCSI_ERR_PROTO;
609 break;
610 }
Mike Christie8d2860b2006-05-02 19:46:47 -0500611
Mike Christie7ea8b822006-07-24 15:47:22 -0500612 iscsi_tmf_rsp(conn, hdr);
Mike Christieb3a7ea82007-12-13 12:43:26 -0600613 iscsi_free_mgmt_task(conn, mtask);
Mike Christie7996a772006-04-06 21:13:41 -0500614 break;
615 case ISCSI_OP_NOOP_IN:
Mike Christief6d51802007-12-13 12:43:30 -0600616 if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) ||
617 datalen) {
Mike Christie7996a772006-04-06 21:13:41 -0500618 rc = ISCSI_ERR_PROTO;
619 break;
620 }
Mike Christie7996a772006-04-06 21:13:41 -0500621 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
622
Mike Christief6d51802007-12-13 12:43:30 -0600623 if (conn->ping_mtask != mtask) {
624 /*
625 * If this is not in response to one of our
626 * nops then it must be from userspace.
627 */
628 if (iscsi_recv_pdu(conn->cls_conn, hdr, data,
629 datalen))
630 rc = ISCSI_ERR_CONN_FAILED;
631 }
Mike Christieb3a7ea82007-12-13 12:43:26 -0600632 iscsi_free_mgmt_task(conn, mtask);
Mike Christie7996a772006-04-06 21:13:41 -0500633 break;
634 default:
635 rc = ISCSI_ERR_BAD_OPCODE;
636 break;
637 }
Al Virob4377352007-02-09 16:39:40 +0000638 } else if (itt == ~0U) {
Mike Christie77a23c22007-05-30 12:57:18 -0500639 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
Mike Christie62f38302006-08-31 18:09:27 -0400640
Mike Christie7996a772006-04-06 21:13:41 -0500641 switch(opcode) {
642 case ISCSI_OP_NOOP_IN:
Mike Christie40527af2006-07-24 15:47:45 -0500643 if (datalen) {
Mike Christie7996a772006-04-06 21:13:41 -0500644 rc = ISCSI_ERR_PROTO;
Mike Christie40527af2006-07-24 15:47:45 -0500645 break;
646 }
647
Al Virob4377352007-02-09 16:39:40 +0000648 if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
Mike Christie40527af2006-07-24 15:47:45 -0500649 break;
650
Mike Christief6d51802007-12-13 12:43:30 -0600651 iscsi_send_nopout(conn, (struct iscsi_nopin*)hdr);
Mike Christie7996a772006-04-06 21:13:41 -0500652 break;
653 case ISCSI_OP_REJECT:
Mike Christie62f38302006-08-31 18:09:27 -0400654 rc = iscsi_handle_reject(conn, hdr, data, datalen);
655 break;
Mike Christie7996a772006-04-06 21:13:41 -0500656 case ISCSI_OP_ASYNC_EVENT:
Mike Christie8d2860b2006-05-02 19:46:47 -0500657 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
Mike Christie5831c732006-10-16 18:09:41 -0400658 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
659 rc = ISCSI_ERR_CONN_FAILED;
Mike Christie7996a772006-04-06 21:13:41 -0500660 break;
661 default:
662 rc = ISCSI_ERR_BAD_OPCODE;
663 break;
664 }
665 } else
666 rc = ISCSI_ERR_BAD_ITT;
667
668 return rc;
669}
670EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
671
672int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
673 char *data, int datalen)
674{
675 int rc;
676
677 spin_lock(&conn->session->lock);
678 rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
679 spin_unlock(&conn->session->lock);
680 return rc;
681}
682EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
683
684/* verify itt (itt encoding: age+cid+itt) */
685int iscsi_verify_itt(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
686 uint32_t *ret_itt)
687{
688 struct iscsi_session *session = conn->session;
689 struct iscsi_cmd_task *ctask;
690 uint32_t itt;
691
Al Virob4377352007-02-09 16:39:40 +0000692 if (hdr->itt != RESERVED_ITT) {
693 if (((__force u32)hdr->itt & ISCSI_AGE_MASK) !=
Mike Christie7996a772006-04-06 21:13:41 -0500694 (session->age << ISCSI_AGE_SHIFT)) {
Or Gerlitzbe2df722006-05-02 19:46:43 -0500695 printk(KERN_ERR "iscsi: received itt %x expected "
Al Virob4377352007-02-09 16:39:40 +0000696 "session age (%x)\n", (__force u32)hdr->itt,
Mike Christie7996a772006-04-06 21:13:41 -0500697 session->age & ISCSI_AGE_MASK);
698 return ISCSI_ERR_BAD_ITT;
699 }
700
Al Virob4377352007-02-09 16:39:40 +0000701 if (((__force u32)hdr->itt & ISCSI_CID_MASK) !=
Mike Christie7996a772006-04-06 21:13:41 -0500702 (conn->id << ISCSI_CID_SHIFT)) {
Or Gerlitzbe2df722006-05-02 19:46:43 -0500703 printk(KERN_ERR "iscsi: received itt %x, expected "
Al Virob4377352007-02-09 16:39:40 +0000704 "CID (%x)\n", (__force u32)hdr->itt, conn->id);
Mike Christie7996a772006-04-06 21:13:41 -0500705 return ISCSI_ERR_BAD_ITT;
706 }
Al Virob4377352007-02-09 16:39:40 +0000707 itt = get_itt(hdr->itt);
Mike Christie7996a772006-04-06 21:13:41 -0500708 } else
Al Virob4377352007-02-09 16:39:40 +0000709 itt = ~0U;
Mike Christie7996a772006-04-06 21:13:41 -0500710
711 if (itt < session->cmds_max) {
712 ctask = session->cmds[itt];
713
714 if (!ctask->sc) {
Or Gerlitzbe2df722006-05-02 19:46:43 -0500715 printk(KERN_INFO "iscsi: dropping ctask with "
Mike Christie7996a772006-04-06 21:13:41 -0500716 "itt 0x%x\n", ctask->itt);
717 /* force drop */
718 return ISCSI_ERR_NO_SCSI_CMD;
719 }
720
721 if (ctask->sc->SCp.phase != session->age) {
Or Gerlitzbe2df722006-05-02 19:46:43 -0500722 printk(KERN_ERR "iscsi: ctask's session age %d, "
Mike Christie7996a772006-04-06 21:13:41 -0500723 "expected %d\n", ctask->sc->SCp.phase,
724 session->age);
725 return ISCSI_ERR_SESSION_FAILED;
726 }
727 }
728
729 *ret_itt = itt;
730 return 0;
731}
732EXPORT_SYMBOL_GPL(iscsi_verify_itt);
733
734void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
735{
736 struct iscsi_session *session = conn->session;
737 unsigned long flags;
738
739 spin_lock_irqsave(&session->lock, flags);
Mike Christie656cffc2006-05-18 20:31:42 -0500740 if (session->state == ISCSI_STATE_FAILED) {
741 spin_unlock_irqrestore(&session->lock, flags);
742 return;
743 }
744
Mike Christie67a61112006-05-30 00:37:20 -0500745 if (conn->stop_stage == 0)
Mike Christie7996a772006-04-06 21:13:41 -0500746 session->state = ISCSI_STATE_FAILED;
747 spin_unlock_irqrestore(&session->lock, flags);
748 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
749 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
750 iscsi_conn_error(conn->cls_conn, err);
751}
752EXPORT_SYMBOL_GPL(iscsi_conn_failure);
753
Mike Christie77a23c22007-05-30 12:57:18 -0500754static void iscsi_prep_mtask(struct iscsi_conn *conn,
755 struct iscsi_mgmt_task *mtask)
756{
757 struct iscsi_session *session = conn->session;
758 struct iscsi_hdr *hdr = mtask->hdr;
759 struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
760
761 if (hdr->opcode != (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) &&
762 hdr->opcode != (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
763 nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
764 /*
765 * pre-format CmdSN for outgoing PDU.
766 */
767 nop->cmdsn = cpu_to_be32(session->cmdsn);
768 if (hdr->itt != RESERVED_ITT) {
769 hdr->itt = build_itt(mtask->itt, conn->id, session->age);
Mike Christiee0726402007-07-26 12:46:48 -0500770 /*
771 * TODO: We always use immediate, so we never hit this.
772 * If we start to send tmfs or nops as non-immediate then
773 * we should start checking the cmdsn numbers for mgmt tasks.
774 */
Mike Christie77a23c22007-05-30 12:57:18 -0500775 if (conn->c_stage == ISCSI_CONN_STARTED &&
Mike Christiee0726402007-07-26 12:46:48 -0500776 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
777 session->queued_cmdsn++;
Mike Christie77a23c22007-05-30 12:57:18 -0500778 session->cmdsn++;
Mike Christiee0726402007-07-26 12:46:48 -0500779 }
Mike Christie77a23c22007-05-30 12:57:18 -0500780 }
781
782 if (session->tt->init_mgmt_task)
783 session->tt->init_mgmt_task(conn, mtask);
784
785 debug_scsi("mgmtpdu [op 0x%x hdr->itt 0x%x datalen %d]\n",
Mike Christie843c0a82007-12-13 12:43:20 -0600786 hdr->opcode & ISCSI_OPCODE_MASK, hdr->itt,
787 mtask->data_count);
Mike Christie77a23c22007-05-30 12:57:18 -0500788}
789
Mike Christie05db8882007-02-28 17:32:16 -0600790static int iscsi_xmit_mtask(struct iscsi_conn *conn)
Mike Christieb5072ea2006-10-16 18:09:42 -0400791{
792 struct iscsi_hdr *hdr = conn->mtask->hdr;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600793 int rc;
Mike Christieb5072ea2006-10-16 18:09:42 -0400794
Mike Christieb3a7ea82007-12-13 12:43:26 -0600795 if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
796 conn->session->state = ISCSI_STATE_LOGGING_OUT;
Mike Christie77a23c22007-05-30 12:57:18 -0500797 spin_unlock_bh(&conn->session->lock);
Mike Christieb3a7ea82007-12-13 12:43:26 -0600798
Mike Christieb5072ea2006-10-16 18:09:42 -0400799 rc = conn->session->tt->xmit_mgmt_task(conn, conn->mtask);
Mike Christie77a23c22007-05-30 12:57:18 -0500800 spin_lock_bh(&conn->session->lock);
Mike Christieb5072ea2006-10-16 18:09:42 -0400801 if (rc)
802 return rc;
803
Mike Christie05db8882007-02-28 17:32:16 -0600804 /* done with this in-progress mtask */
805 conn->mtask = NULL;
Mike Christieb5072ea2006-10-16 18:09:42 -0400806 return 0;
807}
808
Mike Christie77a23c22007-05-30 12:57:18 -0500809static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
810{
811 struct iscsi_session *session = conn->session;
812
813 /*
814 * Check for iSCSI window and take care of CmdSN wrap-around
815 */
Mike Christiee0726402007-07-26 12:46:48 -0500816 if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) {
817 debug_scsi("iSCSI CmdSN closed. ExpCmdSn %u MaxCmdSN %u "
818 "CmdSN %u/%u\n", session->exp_cmdsn,
819 session->max_cmdsn, session->cmdsn,
820 session->queued_cmdsn);
Mike Christie77a23c22007-05-30 12:57:18 -0500821 return -ENOSPC;
822 }
823 return 0;
824}
825
826static int iscsi_xmit_ctask(struct iscsi_conn *conn)
827{
828 struct iscsi_cmd_task *ctask = conn->ctask;
Mike Christie843c0a82007-12-13 12:43:20 -0600829 int rc;
Mike Christie77a23c22007-05-30 12:57:18 -0500830
831 __iscsi_get_ctask(ctask);
832 spin_unlock_bh(&conn->session->lock);
833 rc = conn->session->tt->xmit_cmd_task(conn, ctask);
834 spin_lock_bh(&conn->session->lock);
835 __iscsi_put_ctask(ctask);
Mike Christie77a23c22007-05-30 12:57:18 -0500836 if (!rc)
837 /* done with this ctask */
838 conn->ctask = NULL;
839 return rc;
840}
841
Mike Christie7996a772006-04-06 21:13:41 -0500842/**
Mike Christie843c0a82007-12-13 12:43:20 -0600843 * iscsi_requeue_ctask - requeue ctask to run from session workqueue
844 * @ctask: ctask to requeue
845 *
846 * LLDs that need to run a ctask from the session workqueue should call
847 * this. The session lock must be held.
848 */
849void iscsi_requeue_ctask(struct iscsi_cmd_task *ctask)
850{
851 struct iscsi_conn *conn = ctask->conn;
852
853 list_move_tail(&ctask->running, &conn->requeue);
854 scsi_queue_work(conn->session->host, &conn->xmitwork);
855}
856EXPORT_SYMBOL_GPL(iscsi_requeue_ctask);
857
858/**
Mike Christie7996a772006-04-06 21:13:41 -0500859 * iscsi_data_xmit - xmit any command into the scheduled connection
860 * @conn: iscsi connection
861 *
862 * Notes:
863 * The function can return -EAGAIN in which case the caller must
864 * re-schedule it again later or recover. '0' return code means
865 * successful xmit.
866 **/
867static int iscsi_data_xmit(struct iscsi_conn *conn)
868{
Mike Christie3219e522006-05-30 00:37:28 -0500869 int rc = 0;
Mike Christie7996a772006-04-06 21:13:41 -0500870
Mike Christie77a23c22007-05-30 12:57:18 -0500871 spin_lock_bh(&conn->session->lock);
Mike Christie7996a772006-04-06 21:13:41 -0500872 if (unlikely(conn->suspend_tx)) {
873 debug_scsi("conn %d Tx suspended!\n", conn->id);
Mike Christie77a23c22007-05-30 12:57:18 -0500874 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -0500875 return -ENODATA;
Mike Christie7996a772006-04-06 21:13:41 -0500876 }
Mike Christie7996a772006-04-06 21:13:41 -0500877
878 if (conn->ctask) {
Mike Christie77a23c22007-05-30 12:57:18 -0500879 rc = iscsi_xmit_ctask(conn);
Mike Christie3219e522006-05-30 00:37:28 -0500880 if (rc)
Mike Christie7996a772006-04-06 21:13:41 -0500881 goto again;
Mike Christie7996a772006-04-06 21:13:41 -0500882 }
Mike Christie77a23c22007-05-30 12:57:18 -0500883
Mike Christie7996a772006-04-06 21:13:41 -0500884 if (conn->mtask) {
Mike Christie05db8882007-02-28 17:32:16 -0600885 rc = iscsi_xmit_mtask(conn);
Mike Christie3219e522006-05-30 00:37:28 -0500886 if (rc)
Mike Christie7996a772006-04-06 21:13:41 -0500887 goto again;
Mike Christie7996a772006-04-06 21:13:41 -0500888 }
889
Mike Christie77a23c22007-05-30 12:57:18 -0500890 /*
891 * process mgmt pdus like nops before commands since we should
892 * only have one nop-out as a ping from us and targets should not
893 * overflow us with nop-ins
894 */
895check_mgmt:
Mike Christie843c0a82007-12-13 12:43:20 -0600896 while (!list_empty(&conn->mgmtqueue)) {
897 conn->mtask = list_entry(conn->mgmtqueue.next,
898 struct iscsi_mgmt_task, running);
Mike Christieb3a7ea82007-12-13 12:43:26 -0600899 if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
900 iscsi_free_mgmt_task(conn, conn->mtask);
901 conn->mtask = NULL;
902 continue;
903 }
904
Mike Christie77a23c22007-05-30 12:57:18 -0500905 iscsi_prep_mtask(conn, conn->mtask);
Mike Christie843c0a82007-12-13 12:43:20 -0600906 list_move_tail(conn->mgmtqueue.next, &conn->mgmt_run_list);
Mike Christie77a23c22007-05-30 12:57:18 -0500907 rc = iscsi_xmit_mtask(conn);
908 if (rc)
909 goto again;
Mike Christie7996a772006-04-06 21:13:41 -0500910 }
911
Mike Christie843c0a82007-12-13 12:43:20 -0600912 /* process pending command queue */
Mike Christieb6c395e2006-07-24 15:47:15 -0500913 while (!list_empty(&conn->xmitqueue)) {
Mike Christie843c0a82007-12-13 12:43:20 -0600914 if (conn->tmf_state == TMF_QUEUED)
915 break;
916
Mike Christieb6c395e2006-07-24 15:47:15 -0500917 conn->ctask = list_entry(conn->xmitqueue.next,
918 struct iscsi_cmd_task, running);
Mike Christieb3a7ea82007-12-13 12:43:26 -0600919 if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
Mike Christie9000bcd2007-12-13 12:43:32 -0600920 fail_command(conn, conn->ctask, DID_IMM_RETRY << 16);
Mike Christieb3a7ea82007-12-13 12:43:26 -0600921 continue;
922 }
Boaz Harrosh004d6532007-12-13 12:43:23 -0600923 if (iscsi_prep_scsi_cmd_pdu(conn->ctask)) {
924 fail_command(conn, conn->ctask, DID_ABORT << 16);
925 continue;
926 }
Mike Christie843c0a82007-12-13 12:43:20 -0600927 conn->session->tt->init_cmd_task(conn->ctask);
928 conn->ctask->state = ISCSI_TASK_RUNNING;
Mike Christieb6c395e2006-07-24 15:47:15 -0500929 list_move_tail(conn->xmitqueue.next, &conn->run_list);
Mike Christie77a23c22007-05-30 12:57:18 -0500930 rc = iscsi_xmit_ctask(conn);
931 if (rc)
Mike Christie60ecebf2006-08-31 18:09:25 -0400932 goto again;
Mike Christie77a23c22007-05-30 12:57:18 -0500933 /*
934 * we could continuously get new ctask requests so
935 * we need to check the mgmt queue for nops that need to
936 * be sent to aviod starvation
937 */
Mike Christie843c0a82007-12-13 12:43:20 -0600938 if (!list_empty(&conn->mgmtqueue))
939 goto check_mgmt;
940 }
941
942 while (!list_empty(&conn->requeue)) {
943 if (conn->session->fast_abort && conn->tmf_state != TMF_INITIAL)
944 break;
945
Mike Christieb3a7ea82007-12-13 12:43:26 -0600946 /*
947 * we always do fastlogout - conn stop code will clean up.
948 */
949 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
950 break;
951
Mike Christie843c0a82007-12-13 12:43:20 -0600952 conn->ctask = list_entry(conn->requeue.next,
953 struct iscsi_cmd_task, running);
954 conn->ctask->state = ISCSI_TASK_RUNNING;
955 list_move_tail(conn->requeue.next, &conn->run_list);
956 rc = iscsi_xmit_ctask(conn);
957 if (rc)
958 goto again;
959 if (!list_empty(&conn->mgmtqueue))
Mike Christie77a23c22007-05-30 12:57:18 -0500960 goto check_mgmt;
Mike Christie7996a772006-04-06 21:13:41 -0500961 }
Mike Christieb6c395e2006-07-24 15:47:15 -0500962 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -0500963 return -ENODATA;
Mike Christie7996a772006-04-06 21:13:41 -0500964
965again:
966 if (unlikely(conn->suspend_tx))
Mike Christie77a23c22007-05-30 12:57:18 -0500967 rc = -ENODATA;
968 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -0500969 return rc;
Mike Christie7996a772006-04-06 21:13:41 -0500970}
971
David Howellsc4028952006-11-22 14:57:56 +0000972static void iscsi_xmitworker(struct work_struct *work)
Mike Christie7996a772006-04-06 21:13:41 -0500973{
David Howellsc4028952006-11-22 14:57:56 +0000974 struct iscsi_conn *conn =
975 container_of(work, struct iscsi_conn, xmitwork);
Mike Christie3219e522006-05-30 00:37:28 -0500976 int rc;
Mike Christie7996a772006-04-06 21:13:41 -0500977 /*
978 * serialize Xmit worker on a per-connection basis.
979 */
Mike Christie3219e522006-05-30 00:37:28 -0500980 do {
981 rc = iscsi_data_xmit(conn);
982 } while (rc >= 0 || rc == -EAGAIN);
Mike Christie7996a772006-04-06 21:13:41 -0500983}
984
985enum {
986 FAILURE_BAD_HOST = 1,
987 FAILURE_SESSION_FAILED,
988 FAILURE_SESSION_FREED,
989 FAILURE_WINDOW_CLOSED,
Mike Christie60ecebf2006-08-31 18:09:25 -0400990 FAILURE_OOM,
Mike Christie7996a772006-04-06 21:13:41 -0500991 FAILURE_SESSION_TERMINATE,
Mike Christie656cffc2006-05-18 20:31:42 -0500992 FAILURE_SESSION_IN_RECOVERY,
Mike Christie7996a772006-04-06 21:13:41 -0500993 FAILURE_SESSION_RECOVERY_TIMEOUT,
Mike Christieb3a7ea82007-12-13 12:43:26 -0600994 FAILURE_SESSION_LOGGING_OUT,
Mike Christie7996a772006-04-06 21:13:41 -0500995};
996
997int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
998{
999 struct Scsi_Host *host;
1000 int reason = 0;
1001 struct iscsi_session *session;
1002 struct iscsi_conn *conn;
1003 struct iscsi_cmd_task *ctask = NULL;
1004
1005 sc->scsi_done = done;
1006 sc->result = 0;
Mike Christief47f2cf2006-08-31 18:09:33 -04001007 sc->SCp.ptr = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05001008
1009 host = sc->device->host;
1010 session = iscsi_hostdata(host->hostdata);
1011
1012 spin_lock(&session->lock);
1013
Mike Christie656cffc2006-05-18 20:31:42 -05001014 /*
1015 * ISCSI_STATE_FAILED is a temp. state. The recovery
1016 * code will decide what is best to do with command queued
1017 * during this time
1018 */
1019 if (session->state != ISCSI_STATE_LOGGED_IN &&
1020 session->state != ISCSI_STATE_FAILED) {
1021 /*
1022 * to handle the race between when we set the recovery state
1023 * and block the session we requeue here (commands could
1024 * be entering our queuecommand while a block is starting
1025 * up because the block code is not locked)
1026 */
Mike Christie9000bcd2007-12-13 12:43:32 -06001027 switch (session->state) {
1028 case ISCSI_STATE_IN_RECOVERY:
Mike Christie656cffc2006-05-18 20:31:42 -05001029 reason = FAILURE_SESSION_IN_RECOVERY;
Mike Christie67a61112006-05-30 00:37:20 -05001030 goto reject;
Mike Christie9000bcd2007-12-13 12:43:32 -06001031 case ISCSI_STATE_LOGGING_OUT:
1032 reason = FAILURE_SESSION_LOGGING_OUT;
1033 goto reject;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001034 case ISCSI_STATE_RECOVERY_FAILED:
Mike Christie656cffc2006-05-18 20:31:42 -05001035 reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001036 break;
1037 case ISCSI_STATE_TERMINATE:
Mike Christie656cffc2006-05-18 20:31:42 -05001038 reason = FAILURE_SESSION_TERMINATE;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001039 break;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001040 default:
Mike Christie656cffc2006-05-18 20:31:42 -05001041 reason = FAILURE_SESSION_FREED;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001042 }
Mike Christie7996a772006-04-06 21:13:41 -05001043 goto fault;
1044 }
1045
Mike Christie7996a772006-04-06 21:13:41 -05001046 conn = session->leadconn;
Mike Christie98644042006-10-16 18:09:39 -04001047 if (!conn) {
1048 reason = FAILURE_SESSION_FREED;
1049 goto fault;
1050 }
Mike Christie7996a772006-04-06 21:13:41 -05001051
Mike Christie77a23c22007-05-30 12:57:18 -05001052 if (iscsi_check_cmdsn_window_closed(conn)) {
1053 reason = FAILURE_WINDOW_CLOSED;
1054 goto reject;
1055 }
1056
Mike Christie60ecebf2006-08-31 18:09:25 -04001057 if (!__kfifo_get(session->cmdpool.queue, (void*)&ctask,
1058 sizeof(void*))) {
1059 reason = FAILURE_OOM;
1060 goto reject;
1061 }
Mike Christiee0726402007-07-26 12:46:48 -05001062 session->queued_cmdsn++;
1063
Mike Christie7996a772006-04-06 21:13:41 -05001064 sc->SCp.phase = session->age;
1065 sc->SCp.ptr = (char *)ctask;
1066
Mike Christie60ecebf2006-08-31 18:09:25 -04001067 atomic_set(&ctask->refcount, 1);
Mike Christieb6c395e2006-07-24 15:47:15 -05001068 ctask->state = ISCSI_TASK_PENDING;
Mike Christie7996a772006-04-06 21:13:41 -05001069 ctask->conn = conn;
1070 ctask->sc = sc;
1071 INIT_LIST_HEAD(&ctask->running);
Mike Christie7996a772006-04-06 21:13:41 -05001072
Mike Christieb6c395e2006-07-24 15:47:15 -05001073 list_add_tail(&ctask->running, &conn->xmitqueue);
Mike Christie7996a772006-04-06 21:13:41 -05001074 spin_unlock(&session->lock);
1075
1076 scsi_queue_work(host, &conn->xmitwork);
1077 return 0;
1078
1079reject:
1080 spin_unlock(&session->lock);
1081 debug_scsi("cmd 0x%x rejected (%d)\n", sc->cmnd[0], reason);
1082 return SCSI_MLQUEUE_HOST_BUSY;
1083
1084fault:
1085 spin_unlock(&session->lock);
Or Gerlitzbe2df722006-05-02 19:46:43 -05001086 printk(KERN_ERR "iscsi: cmd 0x%x is not queued (%d)\n",
Mike Christie7996a772006-04-06 21:13:41 -05001087 sc->cmnd[0], reason);
1088 sc->result = (DID_NO_CONNECT << 16);
FUJITA Tomonori1c138992007-06-14 22:13:17 +09001089 scsi_set_resid(sc, scsi_bufflen(sc));
Mike Christie7996a772006-04-06 21:13:41 -05001090 sc->scsi_done(sc);
1091 return 0;
1092}
1093EXPORT_SYMBOL_GPL(iscsi_queuecommand);
1094
1095int iscsi_change_queue_depth(struct scsi_device *sdev, int depth)
1096{
1097 if (depth > ISCSI_MAX_CMD_PER_LUN)
1098 depth = ISCSI_MAX_CMD_PER_LUN;
1099 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
1100 return sdev->queue_depth;
1101}
1102EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
1103
Mike Christie7996a772006-04-06 21:13:41 -05001104void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
1105{
1106 struct iscsi_session *session = class_to_transport_session(cls_session);
Mike Christie7996a772006-04-06 21:13:41 -05001107
1108 spin_lock_bh(&session->lock);
1109 if (session->state != ISCSI_STATE_LOGGED_IN) {
Mike Christie656cffc2006-05-18 20:31:42 -05001110 session->state = ISCSI_STATE_RECOVERY_FAILED;
Mike Christie843c0a82007-12-13 12:43:20 -06001111 if (session->leadconn)
1112 wake_up(&session->leadconn->ehwait);
Mike Christie7996a772006-04-06 21:13:41 -05001113 }
1114 spin_unlock_bh(&session->lock);
1115}
1116EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
1117
1118int iscsi_eh_host_reset(struct scsi_cmnd *sc)
1119{
1120 struct Scsi_Host *host = sc->device->host;
1121 struct iscsi_session *session = iscsi_hostdata(host->hostdata);
1122 struct iscsi_conn *conn = session->leadconn;
Mike Christie7996a772006-04-06 21:13:41 -05001123
Mike Christiebc436b22007-12-13 12:43:28 -06001124 mutex_lock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001125 spin_lock_bh(&session->lock);
1126 if (session->state == ISCSI_STATE_TERMINATE) {
1127failed:
1128 debug_scsi("failing host reset: session terminated "
Pete Wyckoffd6e24d12006-11-08 15:58:32 -06001129 "[CID %d age %d]\n", conn->id, session->age);
Mike Christie7996a772006-04-06 21:13:41 -05001130 spin_unlock_bh(&session->lock);
Mike Christiebc436b22007-12-13 12:43:28 -06001131 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001132 return FAILED;
1133 }
1134
Mike Christie7996a772006-04-06 21:13:41 -05001135 spin_unlock_bh(&session->lock);
Mike Christiebc436b22007-12-13 12:43:28 -06001136 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001137 /*
1138 * we drop the lock here but the leadconn cannot be destoyed while
1139 * we are in the scsi eh
1140 */
Mike Christie843c0a82007-12-13 12:43:20 -06001141 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie7996a772006-04-06 21:13:41 -05001142
1143 debug_scsi("iscsi_eh_host_reset wait for relogin\n");
1144 wait_event_interruptible(conn->ehwait,
1145 session->state == ISCSI_STATE_TERMINATE ||
1146 session->state == ISCSI_STATE_LOGGED_IN ||
Mike Christie656cffc2006-05-18 20:31:42 -05001147 session->state == ISCSI_STATE_RECOVERY_FAILED);
Mike Christie7996a772006-04-06 21:13:41 -05001148 if (signal_pending(current))
1149 flush_signals(current);
1150
Mike Christiebc436b22007-12-13 12:43:28 -06001151 mutex_lock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001152 spin_lock_bh(&session->lock);
1153 if (session->state == ISCSI_STATE_LOGGED_IN)
Or Gerlitzbe2df722006-05-02 19:46:43 -05001154 printk(KERN_INFO "iscsi: host reset succeeded\n");
Mike Christie7996a772006-04-06 21:13:41 -05001155 else
1156 goto failed;
1157 spin_unlock_bh(&session->lock);
Mike Christiebc436b22007-12-13 12:43:28 -06001158 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001159 return SUCCESS;
1160}
1161EXPORT_SYMBOL_GPL(iscsi_eh_host_reset);
1162
Mike Christie843c0a82007-12-13 12:43:20 -06001163static void iscsi_tmf_timedout(unsigned long data)
Mike Christie7996a772006-04-06 21:13:41 -05001164{
Mike Christie843c0a82007-12-13 12:43:20 -06001165 struct iscsi_conn *conn = (struct iscsi_conn *)data;
Mike Christie7996a772006-04-06 21:13:41 -05001166 struct iscsi_session *session = conn->session;
1167
1168 spin_lock(&session->lock);
Mike Christie843c0a82007-12-13 12:43:20 -06001169 if (conn->tmf_state == TMF_QUEUED) {
1170 conn->tmf_state = TMF_TIMEDOUT;
1171 debug_scsi("tmf timedout\n");
Mike Christie7996a772006-04-06 21:13:41 -05001172 /* unblock eh_abort() */
1173 wake_up(&conn->ehwait);
1174 }
1175 spin_unlock(&session->lock);
1176}
1177
Mike Christie843c0a82007-12-13 12:43:20 -06001178static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn,
Mike Christief6d51802007-12-13 12:43:30 -06001179 struct iscsi_tm *hdr, int age,
1180 int timeout)
Mike Christie7996a772006-04-06 21:13:41 -05001181{
Mike Christie7996a772006-04-06 21:13:41 -05001182 struct iscsi_session *session = conn->session;
Mike Christie843c0a82007-12-13 12:43:20 -06001183 struct iscsi_mgmt_task *mtask;
Mike Christie7996a772006-04-06 21:13:41 -05001184
Mike Christie843c0a82007-12-13 12:43:20 -06001185 mtask = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
1186 NULL, 0);
1187 if (!mtask) {
Mike Christie6724add2007-08-15 01:38:30 -05001188 spin_unlock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001189 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie843c0a82007-12-13 12:43:20 -06001190 spin_lock_bh(&session->lock);
1191 debug_scsi("tmf exec failure\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001192 return -EPERM;
Mike Christie7996a772006-04-06 21:13:41 -05001193 }
Mike Christie843c0a82007-12-13 12:43:20 -06001194 conn->tmfcmd_pdus_cnt++;
Mike Christief6d51802007-12-13 12:43:30 -06001195 conn->tmf_timer.expires = timeout * HZ + jiffies;
Mike Christie843c0a82007-12-13 12:43:20 -06001196 conn->tmf_timer.function = iscsi_tmf_timedout;
1197 conn->tmf_timer.data = (unsigned long)conn;
1198 add_timer(&conn->tmf_timer);
1199 debug_scsi("tmf set timeout\n");
Mike Christie7996a772006-04-06 21:13:41 -05001200
Mike Christie7996a772006-04-06 21:13:41 -05001201 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05001202 mutex_unlock(&session->eh_mutex);
Mike Christie77a23c22007-05-30 12:57:18 -05001203 scsi_queue_work(session->host, &conn->xmitwork);
Mike Christie7996a772006-04-06 21:13:41 -05001204
1205 /*
1206 * block eh thread until:
1207 *
Mike Christie843c0a82007-12-13 12:43:20 -06001208 * 1) tmf response
1209 * 2) tmf timeout
Mike Christie7996a772006-04-06 21:13:41 -05001210 * 3) session is terminated or restarted or userspace has
1211 * given up on recovery
1212 */
Mike Christie843c0a82007-12-13 12:43:20 -06001213 wait_event_interruptible(conn->ehwait, age != session->age ||
Mike Christie7996a772006-04-06 21:13:41 -05001214 session->state != ISCSI_STATE_LOGGED_IN ||
Mike Christie843c0a82007-12-13 12:43:20 -06001215 conn->tmf_state != TMF_QUEUED);
Mike Christie7996a772006-04-06 21:13:41 -05001216 if (signal_pending(current))
1217 flush_signals(current);
Mike Christie843c0a82007-12-13 12:43:20 -06001218 del_timer_sync(&conn->tmf_timer);
1219
Mike Christie6724add2007-08-15 01:38:30 -05001220 mutex_lock(&session->eh_mutex);
Mike Christie77a23c22007-05-30 12:57:18 -05001221 spin_lock_bh(&session->lock);
Mike Christie843c0a82007-12-13 12:43:20 -06001222 /* if the session drops it will clean up the mtask */
1223 if (age != session->age ||
1224 session->state != ISCSI_STATE_LOGGED_IN)
1225 return -ENOTCONN;
Mike Christie7996a772006-04-06 21:13:41 -05001226 return 0;
1227}
1228
1229/*
Mike Christie843c0a82007-12-13 12:43:20 -06001230 * Fail commands. session lock held and recv side suspended and xmit
1231 * thread flushed
1232 */
1233static void fail_all_commands(struct iscsi_conn *conn, unsigned lun)
1234{
1235 struct iscsi_cmd_task *ctask, *tmp;
1236
1237 if (conn->ctask && (conn->ctask->sc->device->lun == lun || lun == -1))
1238 conn->ctask = NULL;
1239
1240 /* flush pending */
1241 list_for_each_entry_safe(ctask, tmp, &conn->xmitqueue, running) {
1242 if (lun == ctask->sc->device->lun || lun == -1) {
1243 debug_scsi("failing pending sc %p itt 0x%x\n",
1244 ctask->sc, ctask->itt);
1245 fail_command(conn, ctask, DID_BUS_BUSY << 16);
1246 }
1247 }
1248
1249 list_for_each_entry_safe(ctask, tmp, &conn->requeue, running) {
1250 if (lun == ctask->sc->device->lun || lun == -1) {
1251 debug_scsi("failing requeued sc %p itt 0x%x\n",
1252 ctask->sc, ctask->itt);
1253 fail_command(conn, ctask, DID_BUS_BUSY << 16);
1254 }
1255 }
1256
1257 /* fail all other running */
1258 list_for_each_entry_safe(ctask, tmp, &conn->run_list, running) {
1259 if (lun == ctask->sc->device->lun || lun == -1) {
1260 debug_scsi("failing in progress sc %p itt 0x%x\n",
1261 ctask->sc, ctask->itt);
1262 fail_command(conn, ctask, DID_BUS_BUSY << 16);
1263 }
1264 }
1265}
1266
Mike Christie6724add2007-08-15 01:38:30 -05001267static void iscsi_suspend_tx(struct iscsi_conn *conn)
1268{
1269 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1270 scsi_flush_work(conn->session->host);
1271}
1272
1273static void iscsi_start_tx(struct iscsi_conn *conn)
1274{
1275 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1276 scsi_queue_work(conn->session->host, &conn->xmitwork);
1277}
1278
Mike Christief6d51802007-12-13 12:43:30 -06001279static enum scsi_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *scmd)
1280{
1281 struct iscsi_cls_session *cls_session;
1282 struct iscsi_session *session;
1283 struct iscsi_conn *conn;
1284 enum scsi_eh_timer_return rc = EH_NOT_HANDLED;
1285
1286 cls_session = starget_to_session(scsi_target(scmd->device));
1287 session = class_to_transport_session(cls_session);
1288
1289 debug_scsi("scsi cmd %p timedout\n", scmd);
1290
1291 spin_lock(&session->lock);
1292 if (session->state != ISCSI_STATE_LOGGED_IN) {
1293 /*
1294 * We are probably in the middle of iscsi recovery so let
1295 * that complete and handle the error.
1296 */
1297 rc = EH_RESET_TIMER;
1298 goto done;
1299 }
1300
1301 conn = session->leadconn;
1302 if (!conn) {
1303 /* In the middle of shuting down */
1304 rc = EH_RESET_TIMER;
1305 goto done;
1306 }
1307
1308 if (!conn->recv_timeout && !conn->ping_timeout)
1309 goto done;
1310 /*
1311 * if the ping timedout then we are in the middle of cleaning up
1312 * and can let the iscsi eh handle it
1313 */
1314 if (time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) +
1315 (conn->ping_timeout * HZ), jiffies))
1316 rc = EH_RESET_TIMER;
1317 /*
1318 * if we are about to check the transport then give the command
1319 * more time
1320 */
1321 if (time_before_eq(conn->last_recv + (conn->recv_timeout * HZ),
1322 jiffies))
1323 rc = EH_RESET_TIMER;
1324 /* if in the middle of checking the transport then give us more time */
1325 if (conn->ping_mtask)
1326 rc = EH_RESET_TIMER;
1327done:
1328 spin_unlock(&session->lock);
1329 debug_scsi("return %s\n", rc == EH_RESET_TIMER ? "timer reset" : "nh");
1330 return rc;
1331}
1332
1333static void iscsi_check_transport_timeouts(unsigned long data)
1334{
1335 struct iscsi_conn *conn = (struct iscsi_conn *)data;
1336 struct iscsi_session *session = conn->session;
1337 unsigned long timeout, next_timeout = 0, last_recv;
1338
1339 spin_lock(&session->lock);
1340 if (session->state != ISCSI_STATE_LOGGED_IN)
1341 goto done;
1342
1343 timeout = conn->recv_timeout;
1344 if (!timeout)
1345 goto done;
1346
1347 timeout *= HZ;
1348 last_recv = conn->last_recv;
1349 if (time_before_eq(last_recv + timeout + (conn->ping_timeout * HZ),
1350 jiffies)) {
1351 printk(KERN_ERR "ping timeout of %d secs expired, "
1352 "last rx %lu, last ping %lu, now %lu\n",
1353 conn->ping_timeout, last_recv,
1354 conn->last_ping, jiffies);
1355 spin_unlock(&session->lock);
1356 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1357 return;
1358 }
1359
1360 if (time_before_eq(last_recv + timeout, jiffies)) {
1361 if (time_before_eq(conn->last_ping, last_recv)) {
1362 /* send a ping to try to provoke some traffic */
1363 debug_scsi("Sending nopout as ping on conn %p\n", conn);
1364 iscsi_send_nopout(conn, NULL);
1365 }
1366 next_timeout = last_recv + timeout + (conn->ping_timeout * HZ);
1367 } else {
1368 next_timeout = last_recv + timeout;
1369 }
1370
1371 if (next_timeout) {
1372 debug_scsi("Setting next tmo %lu\n", next_timeout);
1373 mod_timer(&conn->transport_timer, next_timeout);
1374 }
1375done:
1376 spin_unlock(&session->lock);
1377}
1378
Mike Christie843c0a82007-12-13 12:43:20 -06001379static void iscsi_prep_abort_task_pdu(struct iscsi_cmd_task *ctask,
1380 struct iscsi_tm *hdr)
1381{
1382 memset(hdr, 0, sizeof(*hdr));
1383 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
1384 hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK;
1385 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
1386 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
1387 hdr->rtt = ctask->hdr->itt;
1388 hdr->refcmdsn = ctask->hdr->cmdsn;
1389}
1390
Mike Christie7996a772006-04-06 21:13:41 -05001391int iscsi_eh_abort(struct scsi_cmnd *sc)
1392{
Mike Christie6724add2007-08-15 01:38:30 -05001393 struct Scsi_Host *host = sc->device->host;
1394 struct iscsi_session *session = iscsi_hostdata(host->hostdata);
Mike Christief47f2cf2006-08-31 18:09:33 -04001395 struct iscsi_conn *conn;
Mike Christie843c0a82007-12-13 12:43:20 -06001396 struct iscsi_cmd_task *ctask;
1397 struct iscsi_tm *hdr;
1398 int rc, age;
Mike Christie7996a772006-04-06 21:13:41 -05001399
Mike Christie6724add2007-08-15 01:38:30 -05001400 mutex_lock(&session->eh_mutex);
1401 spin_lock_bh(&session->lock);
Mike Christief47f2cf2006-08-31 18:09:33 -04001402 /*
1403 * if session was ISCSI_STATE_IN_RECOVERY then we may not have
1404 * got the command.
1405 */
1406 if (!sc->SCp.ptr) {
1407 debug_scsi("sc never reached iscsi layer or it completed.\n");
Mike Christie6724add2007-08-15 01:38:30 -05001408 spin_unlock_bh(&session->lock);
1409 mutex_unlock(&session->eh_mutex);
Mike Christief47f2cf2006-08-31 18:09:33 -04001410 return SUCCESS;
1411 }
1412
Mike Christie7996a772006-04-06 21:13:41 -05001413 /*
1414 * If we are not logged in or we have started a new session
1415 * then let the host reset code handle this
1416 */
Mike Christie843c0a82007-12-13 12:43:20 -06001417 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN ||
1418 sc->SCp.phase != session->age) {
1419 spin_unlock_bh(&session->lock);
1420 mutex_unlock(&session->eh_mutex);
1421 return FAILED;
1422 }
1423
1424 conn = session->leadconn;
1425 conn->eh_abort_cnt++;
1426 age = session->age;
1427
1428 ctask = (struct iscsi_cmd_task *)sc->SCp.ptr;
1429 debug_scsi("aborting [sc %p itt 0x%x]\n", sc, ctask->itt);
Mike Christie7996a772006-04-06 21:13:41 -05001430
1431 /* ctask completed before time out */
Mike Christie7ea8b822006-07-24 15:47:22 -05001432 if (!ctask->sc) {
Mike Christie7ea8b822006-07-24 15:47:22 -05001433 debug_scsi("sc completed while abort in progress\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001434 goto success;
Mike Christie7ea8b822006-07-24 15:47:22 -05001435 }
Mike Christie7996a772006-04-06 21:13:41 -05001436
Mike Christie77a23c22007-05-30 12:57:18 -05001437 if (ctask->state == ISCSI_TASK_PENDING) {
1438 fail_command(conn, ctask, DID_ABORT << 16);
1439 goto success;
1440 }
Mike Christie7996a772006-04-06 21:13:41 -05001441
Mike Christie843c0a82007-12-13 12:43:20 -06001442 /* only have one tmf outstanding at a time */
1443 if (conn->tmf_state != TMF_INITIAL)
Mike Christie7996a772006-04-06 21:13:41 -05001444 goto failed;
Mike Christie843c0a82007-12-13 12:43:20 -06001445 conn->tmf_state = TMF_QUEUED;
Mike Christie7996a772006-04-06 21:13:41 -05001446
Mike Christie843c0a82007-12-13 12:43:20 -06001447 hdr = &conn->tmhdr;
1448 iscsi_prep_abort_task_pdu(ctask, hdr);
1449
Mike Christief6d51802007-12-13 12:43:30 -06001450 if (iscsi_exec_task_mgmt_fn(conn, hdr, age, session->abort_timeout)) {
Mike Christie843c0a82007-12-13 12:43:20 -06001451 rc = FAILED;
1452 goto failed;
1453 }
1454
1455 switch (conn->tmf_state) {
1456 case TMF_SUCCESS:
Mike Christie77a23c22007-05-30 12:57:18 -05001457 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05001458 iscsi_suspend_tx(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001459 /*
1460 * clean up task if aborted. grab the recv lock as a writer
1461 */
1462 write_lock_bh(conn->recv_lock);
1463 spin_lock(&session->lock);
1464 fail_command(conn, ctask, DID_ABORT << 16);
Mike Christie843c0a82007-12-13 12:43:20 -06001465 conn->tmf_state = TMF_INITIAL;
Mike Christie77a23c22007-05-30 12:57:18 -05001466 spin_unlock(&session->lock);
1467 write_unlock_bh(conn->recv_lock);
Mike Christie6724add2007-08-15 01:38:30 -05001468 iscsi_start_tx(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001469 goto success_unlocked;
Mike Christie843c0a82007-12-13 12:43:20 -06001470 case TMF_TIMEDOUT:
1471 spin_unlock_bh(&session->lock);
1472 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1473 goto failed_unlocked;
1474 case TMF_NOT_FOUND:
1475 if (!sc->SCp.ptr) {
1476 conn->tmf_state = TMF_INITIAL;
Mike Christie7ea8b822006-07-24 15:47:22 -05001477 /* ctask completed before tmf abort response */
Mike Christie7ea8b822006-07-24 15:47:22 -05001478 debug_scsi("sc completed while abort in progress\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001479 goto success;
Mike Christie7ea8b822006-07-24 15:47:22 -05001480 }
1481 /* fall through */
1482 default:
Mike Christie843c0a82007-12-13 12:43:20 -06001483 conn->tmf_state = TMF_INITIAL;
1484 goto failed;
Mike Christie7996a772006-04-06 21:13:41 -05001485 }
1486
Mike Christie77a23c22007-05-30 12:57:18 -05001487success:
Mike Christie7996a772006-04-06 21:13:41 -05001488 spin_unlock_bh(&session->lock);
Mike Christie77a23c22007-05-30 12:57:18 -05001489success_unlocked:
1490 debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
Mike Christie6724add2007-08-15 01:38:30 -05001491 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001492 return SUCCESS;
1493
1494failed:
1495 spin_unlock_bh(&session->lock);
Mike Christie77a23c22007-05-30 12:57:18 -05001496failed_unlocked:
Mike Christie843c0a82007-12-13 12:43:20 -06001497 debug_scsi("abort failed [sc %p itt 0x%x]\n", sc,
1498 ctask ? ctask->itt : 0);
Mike Christie6724add2007-08-15 01:38:30 -05001499 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001500 return FAILED;
1501}
1502EXPORT_SYMBOL_GPL(iscsi_eh_abort);
1503
Mike Christie843c0a82007-12-13 12:43:20 -06001504static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
1505{
1506 memset(hdr, 0, sizeof(*hdr));
1507 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
1508 hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK;
1509 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
1510 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
Mike Christief6d51802007-12-13 12:43:30 -06001511 hdr->rtt = RESERVED_ITT;
Mike Christie843c0a82007-12-13 12:43:20 -06001512}
1513
1514int iscsi_eh_device_reset(struct scsi_cmnd *sc)
1515{
1516 struct Scsi_Host *host = sc->device->host;
1517 struct iscsi_session *session = iscsi_hostdata(host->hostdata);
1518 struct iscsi_conn *conn;
1519 struct iscsi_tm *hdr;
1520 int rc = FAILED;
1521
1522 debug_scsi("LU Reset [sc %p lun %u]\n", sc, sc->device->lun);
1523
1524 mutex_lock(&session->eh_mutex);
1525 spin_lock_bh(&session->lock);
1526 /*
1527 * Just check if we are not logged in. We cannot check for
1528 * the phase because the reset could come from a ioctl.
1529 */
1530 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
1531 goto unlock;
1532 conn = session->leadconn;
1533
1534 /* only have one tmf outstanding at a time */
1535 if (conn->tmf_state != TMF_INITIAL)
1536 goto unlock;
1537 conn->tmf_state = TMF_QUEUED;
1538
1539 hdr = &conn->tmhdr;
1540 iscsi_prep_lun_reset_pdu(sc, hdr);
1541
Mike Christief6d51802007-12-13 12:43:30 -06001542 if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
1543 session->lu_reset_timeout)) {
Mike Christie843c0a82007-12-13 12:43:20 -06001544 rc = FAILED;
1545 goto unlock;
1546 }
1547
1548 switch (conn->tmf_state) {
1549 case TMF_SUCCESS:
1550 break;
1551 case TMF_TIMEDOUT:
1552 spin_unlock_bh(&session->lock);
1553 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1554 goto done;
1555 default:
1556 conn->tmf_state = TMF_INITIAL;
1557 goto unlock;
1558 }
1559
1560 rc = SUCCESS;
1561 spin_unlock_bh(&session->lock);
1562
1563 iscsi_suspend_tx(conn);
1564 /* need to grab the recv lock then session lock */
1565 write_lock_bh(conn->recv_lock);
1566 spin_lock(&session->lock);
1567 fail_all_commands(conn, sc->device->lun);
1568 conn->tmf_state = TMF_INITIAL;
1569 spin_unlock(&session->lock);
1570 write_unlock_bh(conn->recv_lock);
1571
1572 iscsi_start_tx(conn);
1573 goto done;
1574
1575unlock:
1576 spin_unlock_bh(&session->lock);
1577done:
1578 debug_scsi("iscsi_eh_device_reset %s\n",
1579 rc == SUCCESS ? "SUCCESS" : "FAILED");
1580 mutex_unlock(&session->eh_mutex);
1581 return rc;
1582}
1583EXPORT_SYMBOL_GPL(iscsi_eh_device_reset);
1584
Olaf Kirch63203772007-12-13 12:43:25 -06001585/*
1586 * Pre-allocate a pool of @max items of @item_size. By default, the pool
1587 * should be accessed via kfifo_{get,put} on q->queue.
1588 * Optionally, the caller can obtain the array of object pointers
1589 * by passing in a non-NULL @items pointer
1590 */
Mike Christie7996a772006-04-06 21:13:41 -05001591int
Olaf Kirch63203772007-12-13 12:43:25 -06001592iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size)
Mike Christie7996a772006-04-06 21:13:41 -05001593{
Olaf Kirch63203772007-12-13 12:43:25 -06001594 int i, num_arrays = 1;
Mike Christie7996a772006-04-06 21:13:41 -05001595
Olaf Kirch63203772007-12-13 12:43:25 -06001596 memset(q, 0, sizeof(*q));
Mike Christie7996a772006-04-06 21:13:41 -05001597
1598 q->max = max;
Olaf Kirch63203772007-12-13 12:43:25 -06001599
1600 /* If the user passed an items pointer, he wants a copy of
1601 * the array. */
1602 if (items)
1603 num_arrays++;
1604 q->pool = kzalloc(num_arrays * max * sizeof(void*), GFP_KERNEL);
1605 if (q->pool == NULL)
1606 goto enomem;
Mike Christie7996a772006-04-06 21:13:41 -05001607
1608 q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
1609 GFP_KERNEL, NULL);
Olaf Kirch63203772007-12-13 12:43:25 -06001610 if (q->queue == ERR_PTR(-ENOMEM))
1611 goto enomem;
Mike Christie7996a772006-04-06 21:13:41 -05001612
1613 for (i = 0; i < max; i++) {
Olaf Kirch63203772007-12-13 12:43:25 -06001614 q->pool[i] = kzalloc(item_size, GFP_KERNEL);
Mike Christie7996a772006-04-06 21:13:41 -05001615 if (q->pool[i] == NULL) {
Olaf Kirch63203772007-12-13 12:43:25 -06001616 q->max = i;
1617 goto enomem;
Mike Christie7996a772006-04-06 21:13:41 -05001618 }
Mike Christie7996a772006-04-06 21:13:41 -05001619 __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
1620 }
Olaf Kirch63203772007-12-13 12:43:25 -06001621
1622 if (items) {
1623 *items = q->pool + max;
1624 memcpy(*items, q->pool, max * sizeof(void *));
1625 }
1626
Mike Christie7996a772006-04-06 21:13:41 -05001627 return 0;
Olaf Kirch63203772007-12-13 12:43:25 -06001628
1629enomem:
1630 iscsi_pool_free(q);
1631 return -ENOMEM;
Mike Christie7996a772006-04-06 21:13:41 -05001632}
1633EXPORT_SYMBOL_GPL(iscsi_pool_init);
1634
Olaf Kirch63203772007-12-13 12:43:25 -06001635void iscsi_pool_free(struct iscsi_pool *q)
Mike Christie7996a772006-04-06 21:13:41 -05001636{
1637 int i;
1638
1639 for (i = 0; i < q->max; i++)
Olaf Kirch63203772007-12-13 12:43:25 -06001640 kfree(q->pool[i]);
1641 if (q->pool)
1642 kfree(q->pool);
Mike Christie7996a772006-04-06 21:13:41 -05001643}
1644EXPORT_SYMBOL_GPL(iscsi_pool_free);
1645
1646/*
1647 * iSCSI Session's hostdata organization:
1648 *
1649 * *------------------* <== hostdata_session(host->hostdata)
1650 * | ptr to class sess|
1651 * |------------------| <== iscsi_hostdata(host->hostdata)
1652 * | iscsi_session |
1653 * *------------------*
1654 */
1655
1656#define hostdata_privsize(_sz) (sizeof(unsigned long) + _sz + \
1657 _sz % sizeof(unsigned long))
1658
1659#define hostdata_session(_hostdata) (iscsi_ptr(*(unsigned long *)_hostdata))
1660
1661/**
1662 * iscsi_session_setup - create iscsi cls session and host and session
1663 * @scsit: scsi transport template
1664 * @iscsit: iscsi transport template
Mike Christie15482712007-05-30 12:57:19 -05001665 * @cmds_max: scsi host can queue
1666 * @qdepth: scsi host cmds per lun
1667 * @cmd_task_size: LLD ctask private data size
1668 * @mgmt_task_size: LLD mtask private data size
Mike Christie7996a772006-04-06 21:13:41 -05001669 * @initial_cmdsn: initial CmdSN
1670 * @hostno: host no allocated
1671 *
1672 * This can be used by software iscsi_transports that allocate
1673 * a session per scsi host.
1674 **/
1675struct iscsi_cls_session *
1676iscsi_session_setup(struct iscsi_transport *iscsit,
1677 struct scsi_transport_template *scsit,
Mike Christie15482712007-05-30 12:57:19 -05001678 uint16_t cmds_max, uint16_t qdepth,
Mike Christie7996a772006-04-06 21:13:41 -05001679 int cmd_task_size, int mgmt_task_size,
1680 uint32_t initial_cmdsn, uint32_t *hostno)
1681{
1682 struct Scsi_Host *shost;
1683 struct iscsi_session *session;
1684 struct iscsi_cls_session *cls_session;
1685 int cmd_i;
1686
Mike Christie15482712007-05-30 12:57:19 -05001687 if (qdepth > ISCSI_MAX_CMD_PER_LUN || qdepth < 1) {
1688 if (qdepth != 0)
1689 printk(KERN_ERR "iscsi: invalid queue depth of %d. "
1690 "Queue depth must be between 1 and %d.\n",
1691 qdepth, ISCSI_MAX_CMD_PER_LUN);
1692 qdepth = ISCSI_DEF_CMD_PER_LUN;
1693 }
1694
1695 if (cmds_max < 2 || (cmds_max & (cmds_max - 1)) ||
1696 cmds_max >= ISCSI_MGMT_ITT_OFFSET) {
1697 if (cmds_max != 0)
1698 printk(KERN_ERR "iscsi: invalid can_queue of %d. "
1699 "can_queue must be a power of 2 and between "
1700 "2 and %d - setting to %d.\n", cmds_max,
1701 ISCSI_MGMT_ITT_OFFSET, ISCSI_DEF_XMIT_CMDS_MAX);
1702 cmds_max = ISCSI_DEF_XMIT_CMDS_MAX;
1703 }
1704
Mike Christie7996a772006-04-06 21:13:41 -05001705 shost = scsi_host_alloc(iscsit->host_template,
1706 hostdata_privsize(sizeof(*session)));
1707 if (!shost)
1708 return NULL;
1709
Mike Christie15482712007-05-30 12:57:19 -05001710 /* the iscsi layer takes one task for reserve */
1711 shost->can_queue = cmds_max - 1;
1712 shost->cmd_per_lun = qdepth;
Mike Christie7996a772006-04-06 21:13:41 -05001713 shost->max_id = 1;
1714 shost->max_channel = 0;
1715 shost->max_lun = iscsit->max_lun;
1716 shost->max_cmd_len = iscsit->max_cmd_len;
1717 shost->transportt = scsit;
1718 shost->transportt->create_work_queue = 1;
Mike Christief6d51802007-12-13 12:43:30 -06001719 shost->transportt->eh_timed_out = iscsi_eh_cmd_timed_out;
Mike Christie7996a772006-04-06 21:13:41 -05001720 *hostno = shost->host_no;
1721
1722 session = iscsi_hostdata(shost->hostdata);
1723 memset(session, 0, sizeof(struct iscsi_session));
1724 session->host = shost;
1725 session->state = ISCSI_STATE_FREE;
Mike Christief6d51802007-12-13 12:43:30 -06001726 session->fast_abort = 1;
Mike Christie7996a772006-04-06 21:13:41 -05001727 session->mgmtpool_max = ISCSI_MGMT_CMDS_MAX;
Mike Christie15482712007-05-30 12:57:19 -05001728 session->cmds_max = cmds_max;
Mike Christiee0726402007-07-26 12:46:48 -05001729 session->queued_cmdsn = session->cmdsn = initial_cmdsn;
Mike Christie7996a772006-04-06 21:13:41 -05001730 session->exp_cmdsn = initial_cmdsn + 1;
1731 session->max_cmdsn = initial_cmdsn + 1;
1732 session->max_r2t = 1;
1733 session->tt = iscsit;
Mike Christie6724add2007-08-15 01:38:30 -05001734 mutex_init(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001735
1736 /* initialize SCSI PDU commands pool */
1737 if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
1738 (void***)&session->cmds,
1739 cmd_task_size + sizeof(struct iscsi_cmd_task)))
1740 goto cmdpool_alloc_fail;
1741
1742 /* pre-format cmds pool with ITT */
1743 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
1744 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
1745
1746 if (cmd_task_size)
1747 ctask->dd_data = &ctask[1];
1748 ctask->itt = cmd_i;
Mike Christieb6c395e2006-07-24 15:47:15 -05001749 INIT_LIST_HEAD(&ctask->running);
Mike Christie7996a772006-04-06 21:13:41 -05001750 }
1751
1752 spin_lock_init(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001753
1754 /* initialize immediate command pool */
1755 if (iscsi_pool_init(&session->mgmtpool, session->mgmtpool_max,
1756 (void***)&session->mgmt_cmds,
1757 mgmt_task_size + sizeof(struct iscsi_mgmt_task)))
1758 goto mgmtpool_alloc_fail;
1759
1760
1761 /* pre-format immediate cmds pool with ITT */
1762 for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
1763 struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
1764
1765 if (mgmt_task_size)
1766 mtask->dd_data = &mtask[1];
1767 mtask->itt = ISCSI_MGMT_ITT_OFFSET + cmd_i;
Mike Christieb6c395e2006-07-24 15:47:15 -05001768 INIT_LIST_HEAD(&mtask->running);
Mike Christie7996a772006-04-06 21:13:41 -05001769 }
1770
1771 if (scsi_add_host(shost, NULL))
1772 goto add_host_fail;
1773
Mike Christief53a88d2006-06-28 12:00:27 -05001774 if (!try_module_get(iscsit->owner))
1775 goto cls_session_fail;
1776
Mike Christie6a8a0d32006-06-28 12:00:31 -05001777 cls_session = iscsi_create_session(shost, iscsit, 0);
Mike Christie7996a772006-04-06 21:13:41 -05001778 if (!cls_session)
Mike Christief53a88d2006-06-28 12:00:27 -05001779 goto module_put;
Mike Christie7996a772006-04-06 21:13:41 -05001780 *(unsigned long*)shost->hostdata = (unsigned long)cls_session;
1781
1782 return cls_session;
1783
Mike Christief53a88d2006-06-28 12:00:27 -05001784module_put:
1785 module_put(iscsit->owner);
Mike Christie7996a772006-04-06 21:13:41 -05001786cls_session_fail:
1787 scsi_remove_host(shost);
1788add_host_fail:
Olaf Kirch63203772007-12-13 12:43:25 -06001789 iscsi_pool_free(&session->mgmtpool);
Mike Christie7996a772006-04-06 21:13:41 -05001790mgmtpool_alloc_fail:
Olaf Kirch63203772007-12-13 12:43:25 -06001791 iscsi_pool_free(&session->cmdpool);
Mike Christie7996a772006-04-06 21:13:41 -05001792cmdpool_alloc_fail:
1793 scsi_host_put(shost);
1794 return NULL;
1795}
1796EXPORT_SYMBOL_GPL(iscsi_session_setup);
1797
1798/**
1799 * iscsi_session_teardown - destroy session, host, and cls_session
1800 * shost: scsi host
1801 *
1802 * This can be used by software iscsi_transports that allocate
1803 * a session per scsi host.
1804 **/
1805void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
1806{
1807 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
1808 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
Mike Christie63f75cc2006-07-24 15:47:29 -05001809 struct module *owner = cls_session->transport->owner;
Mike Christie7996a772006-04-06 21:13:41 -05001810
Mike Christie26974782007-12-13 12:43:29 -06001811 iscsi_remove_session(cls_session);
Mike Christie7996a772006-04-06 21:13:41 -05001812 scsi_remove_host(shost);
1813
Olaf Kirch63203772007-12-13 12:43:25 -06001814 iscsi_pool_free(&session->mgmtpool);
1815 iscsi_pool_free(&session->cmdpool);
Mike Christie7996a772006-04-06 21:13:41 -05001816
Mike Christieb2c64162007-05-30 12:57:16 -05001817 kfree(session->password);
1818 kfree(session->password_in);
1819 kfree(session->username);
1820 kfree(session->username_in);
Mike Christief3ff0c32006-07-24 15:47:50 -05001821 kfree(session->targetname);
Mike Christied8196ed2007-05-30 12:57:25 -05001822 kfree(session->netdev);
Mike Christie0801c242007-05-30 12:57:12 -05001823 kfree(session->hwaddress);
Mike Christie8ad57812007-05-30 12:57:13 -05001824 kfree(session->initiatorname);
Mike Christief3ff0c32006-07-24 15:47:50 -05001825
Mike Christie26974782007-12-13 12:43:29 -06001826 iscsi_free_session(cls_session);
Mike Christie7996a772006-04-06 21:13:41 -05001827 scsi_host_put(shost);
Mike Christie63f75cc2006-07-24 15:47:29 -05001828 module_put(owner);
Mike Christie7996a772006-04-06 21:13:41 -05001829}
1830EXPORT_SYMBOL_GPL(iscsi_session_teardown);
1831
1832/**
1833 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
1834 * @cls_session: iscsi_cls_session
1835 * @conn_idx: cid
1836 **/
1837struct iscsi_cls_conn *
1838iscsi_conn_setup(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
1839{
1840 struct iscsi_session *session = class_to_transport_session(cls_session);
1841 struct iscsi_conn *conn;
1842 struct iscsi_cls_conn *cls_conn;
Mike Christied36ab6f2006-05-18 20:31:34 -05001843 char *data;
Mike Christie7996a772006-04-06 21:13:41 -05001844
1845 cls_conn = iscsi_create_conn(cls_session, conn_idx);
1846 if (!cls_conn)
1847 return NULL;
1848 conn = cls_conn->dd_data;
1849 memset(conn, 0, sizeof(*conn));
1850
1851 conn->session = session;
1852 conn->cls_conn = cls_conn;
1853 conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
1854 conn->id = conn_idx;
1855 conn->exp_statsn = 0;
Mike Christie843c0a82007-12-13 12:43:20 -06001856 conn->tmf_state = TMF_INITIAL;
Mike Christief6d51802007-12-13 12:43:30 -06001857
1858 init_timer(&conn->transport_timer);
1859 conn->transport_timer.data = (unsigned long)conn;
1860 conn->transport_timer.function = iscsi_check_transport_timeouts;
1861
Mike Christie7996a772006-04-06 21:13:41 -05001862 INIT_LIST_HEAD(&conn->run_list);
1863 INIT_LIST_HEAD(&conn->mgmt_run_list);
Mike Christie843c0a82007-12-13 12:43:20 -06001864 INIT_LIST_HEAD(&conn->mgmtqueue);
Mike Christieb6c395e2006-07-24 15:47:15 -05001865 INIT_LIST_HEAD(&conn->xmitqueue);
Mike Christie843c0a82007-12-13 12:43:20 -06001866 INIT_LIST_HEAD(&conn->requeue);
David Howellsc4028952006-11-22 14:57:56 +00001867 INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
Mike Christie7996a772006-04-06 21:13:41 -05001868
1869 /* allocate login_mtask used for the login/text sequences */
1870 spin_lock_bh(&session->lock);
1871 if (!__kfifo_get(session->mgmtpool.queue,
1872 (void*)&conn->login_mtask,
1873 sizeof(void*))) {
1874 spin_unlock_bh(&session->lock);
1875 goto login_mtask_alloc_fail;
1876 }
1877 spin_unlock_bh(&session->lock);
1878
Mike Christiebf32ed32007-02-28 17:32:17 -06001879 data = kmalloc(ISCSI_DEF_MAX_RECV_SEG_LEN, GFP_KERNEL);
Mike Christied36ab6f2006-05-18 20:31:34 -05001880 if (!data)
1881 goto login_mtask_data_alloc_fail;
Mike Christiec8dc1e52006-07-24 15:47:39 -05001882 conn->login_mtask->data = conn->data = data;
Mike Christied36ab6f2006-05-18 20:31:34 -05001883
Mike Christie843c0a82007-12-13 12:43:20 -06001884 init_timer(&conn->tmf_timer);
Mike Christie7996a772006-04-06 21:13:41 -05001885 init_waitqueue_head(&conn->ehwait);
1886
1887 return cls_conn;
1888
Mike Christied36ab6f2006-05-18 20:31:34 -05001889login_mtask_data_alloc_fail:
1890 __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
1891 sizeof(void*));
Mike Christie7996a772006-04-06 21:13:41 -05001892login_mtask_alloc_fail:
Mike Christie7996a772006-04-06 21:13:41 -05001893 iscsi_destroy_conn(cls_conn);
1894 return NULL;
1895}
1896EXPORT_SYMBOL_GPL(iscsi_conn_setup);
1897
1898/**
1899 * iscsi_conn_teardown - teardown iscsi connection
1900 * cls_conn: iscsi class connection
1901 *
1902 * TODO: we may need to make this into a two step process
1903 * like scsi-mls remove + put host
1904 */
1905void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
1906{
1907 struct iscsi_conn *conn = cls_conn->dd_data;
1908 struct iscsi_session *session = conn->session;
1909 unsigned long flags;
1910
Mike Christief6d51802007-12-13 12:43:30 -06001911 del_timer_sync(&conn->transport_timer);
1912
Mike Christie7996a772006-04-06 21:13:41 -05001913 spin_lock_bh(&session->lock);
1914 conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
1915 if (session->leadconn == conn) {
1916 /*
1917 * leading connection? then give up on recovery.
1918 */
1919 session->state = ISCSI_STATE_TERMINATE;
1920 wake_up(&conn->ehwait);
1921 }
1922 spin_unlock_bh(&session->lock);
1923
Mike Christie7996a772006-04-06 21:13:41 -05001924 /*
1925 * Block until all in-progress commands for this connection
1926 * time out or fail.
1927 */
1928 for (;;) {
1929 spin_lock_irqsave(session->host->host_lock, flags);
1930 if (!session->host->host_busy) { /* OK for ERL == 0 */
1931 spin_unlock_irqrestore(session->host->host_lock, flags);
1932 break;
1933 }
1934 spin_unlock_irqrestore(session->host->host_lock, flags);
1935 msleep_interruptible(500);
Or Gerlitzbe2df722006-05-02 19:46:43 -05001936 printk(KERN_INFO "iscsi: scsi conn_destroy(): host_busy %d "
1937 "host_failed %d\n", session->host->host_busy,
1938 session->host->host_failed);
Mike Christie7996a772006-04-06 21:13:41 -05001939 /*
1940 * force eh_abort() to unblock
1941 */
1942 wake_up(&conn->ehwait);
1943 }
1944
Mike Christie779ea122007-02-28 17:32:15 -06001945 /* flush queued up work because we free the connection below */
Mike Christie843c0a82007-12-13 12:43:20 -06001946 iscsi_suspend_tx(conn);
Mike Christie779ea122007-02-28 17:32:15 -06001947
Mike Christie7996a772006-04-06 21:13:41 -05001948 spin_lock_bh(&session->lock);
Mike Christiec8dc1e52006-07-24 15:47:39 -05001949 kfree(conn->data);
Mike Christief3ff0c32006-07-24 15:47:50 -05001950 kfree(conn->persistent_address);
Mike Christie7996a772006-04-06 21:13:41 -05001951 __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
1952 sizeof(void*));
Mike Christiee0726402007-07-26 12:46:48 -05001953 if (session->leadconn == conn)
Mike Christie7996a772006-04-06 21:13:41 -05001954 session->leadconn = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05001955 spin_unlock_bh(&session->lock);
1956
Mike Christie7996a772006-04-06 21:13:41 -05001957 iscsi_destroy_conn(cls_conn);
1958}
1959EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
1960
1961int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
1962{
1963 struct iscsi_conn *conn = cls_conn->dd_data;
1964 struct iscsi_session *session = conn->session;
1965
Mike Christieffd04362006-08-31 18:09:24 -04001966 if (!session) {
Mike Christie7996a772006-04-06 21:13:41 -05001967 printk(KERN_ERR "iscsi: can't start unbound connection\n");
1968 return -EPERM;
1969 }
1970
Mike Christiedb98ccd2006-08-31 18:09:31 -04001971 if ((session->imm_data_en || !session->initial_r2t_en) &&
1972 session->first_burst > session->max_burst) {
Mike Christieffd04362006-08-31 18:09:24 -04001973 printk("iscsi: invalid burst lengths: "
1974 "first_burst %d max_burst %d\n",
1975 session->first_burst, session->max_burst);
1976 return -EINVAL;
1977 }
1978
Mike Christief6d51802007-12-13 12:43:30 -06001979 if (conn->ping_timeout && !conn->recv_timeout) {
1980 printk(KERN_ERR "iscsi: invalid recv timeout of zero "
1981 "Using 5 seconds\n.");
1982 conn->recv_timeout = 5;
1983 }
1984
1985 if (conn->recv_timeout && !conn->ping_timeout) {
1986 printk(KERN_ERR "iscsi: invalid ping timeout of zero "
1987 "Using 5 seconds.\n");
1988 conn->ping_timeout = 5;
1989 }
1990
Mike Christie7996a772006-04-06 21:13:41 -05001991 spin_lock_bh(&session->lock);
1992 conn->c_stage = ISCSI_CONN_STARTED;
1993 session->state = ISCSI_STATE_LOGGED_IN;
Mike Christiee0726402007-07-26 12:46:48 -05001994 session->queued_cmdsn = session->cmdsn;
Mike Christie7996a772006-04-06 21:13:41 -05001995
Mike Christief6d51802007-12-13 12:43:30 -06001996 conn->last_recv = jiffies;
1997 conn->last_ping = jiffies;
1998 if (conn->recv_timeout && conn->ping_timeout)
1999 mod_timer(&conn->transport_timer,
2000 jiffies + (conn->recv_timeout * HZ));
2001
Mike Christie7996a772006-04-06 21:13:41 -05002002 switch(conn->stop_stage) {
2003 case STOP_CONN_RECOVER:
2004 /*
2005 * unblock eh_abort() if it is blocked. re-try all
2006 * commands after successful recovery
2007 */
Mike Christie7996a772006-04-06 21:13:41 -05002008 conn->stop_stage = 0;
Mike Christie843c0a82007-12-13 12:43:20 -06002009 conn->tmf_state = TMF_INITIAL;
Mike Christie7996a772006-04-06 21:13:41 -05002010 session->age++;
Mike Christie7996a772006-04-06 21:13:41 -05002011 spin_unlock_bh(&session->lock);
2012
2013 iscsi_unblock_session(session_to_cls(session));
2014 wake_up(&conn->ehwait);
2015 return 0;
2016 case STOP_CONN_TERM:
Mike Christie7996a772006-04-06 21:13:41 -05002017 conn->stop_stage = 0;
2018 break;
Mike Christie7996a772006-04-06 21:13:41 -05002019 default:
2020 break;
2021 }
2022 spin_unlock_bh(&session->lock);
2023
2024 return 0;
2025}
2026EXPORT_SYMBOL_GPL(iscsi_conn_start);
2027
2028static void
2029flush_control_queues(struct iscsi_session *session, struct iscsi_conn *conn)
2030{
2031 struct iscsi_mgmt_task *mtask, *tmp;
2032
2033 /* handle pending */
Mike Christie843c0a82007-12-13 12:43:20 -06002034 list_for_each_entry_safe(mtask, tmp, &conn->mgmtqueue, running) {
2035 debug_scsi("flushing pending mgmt task itt 0x%x\n", mtask->itt);
Mike Christieb3a7ea82007-12-13 12:43:26 -06002036 iscsi_free_mgmt_task(conn, mtask);
Mike Christie7996a772006-04-06 21:13:41 -05002037 }
2038
2039 /* handle running */
2040 list_for_each_entry_safe(mtask, tmp, &conn->mgmt_run_list, running) {
2041 debug_scsi("flushing running mgmt task itt 0x%x\n", mtask->itt);
Mike Christieb3a7ea82007-12-13 12:43:26 -06002042 iscsi_free_mgmt_task(conn, mtask);
Mike Christie7996a772006-04-06 21:13:41 -05002043 }
2044
2045 conn->mtask = NULL;
2046}
2047
Mike Christie656cffc2006-05-18 20:31:42 -05002048static void iscsi_start_session_recovery(struct iscsi_session *session,
2049 struct iscsi_conn *conn, int flag)
Mike Christie7996a772006-04-06 21:13:41 -05002050{
Mike Christieed2abc72006-05-02 19:46:40 -05002051 int old_stop_stage;
2052
Mike Christief6d51802007-12-13 12:43:30 -06002053 del_timer_sync(&conn->transport_timer);
2054
Mike Christie6724add2007-08-15 01:38:30 -05002055 mutex_lock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05002056 spin_lock_bh(&session->lock);
Mike Christieed2abc72006-05-02 19:46:40 -05002057 if (conn->stop_stage == STOP_CONN_TERM) {
Mike Christie7996a772006-04-06 21:13:41 -05002058 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05002059 mutex_unlock(&session->eh_mutex);
2060 return;
2061 }
2062
2063 /*
2064 * The LLD either freed/unset the lock on us, or userspace called
2065 * stop but did not create a proper connection (connection was never
2066 * bound or it was unbound then stop was called).
2067 */
2068 if (!conn->recv_lock) {
2069 spin_unlock_bh(&session->lock);
2070 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05002071 return;
2072 }
Mike Christieed2abc72006-05-02 19:46:40 -05002073
2074 /*
2075 * When this is called for the in_login state, we only want to clean
Mike Christie67a61112006-05-30 00:37:20 -05002076 * up the login task and connection. We do not need to block and set
2077 * the recovery state again
Mike Christieed2abc72006-05-02 19:46:40 -05002078 */
Mike Christie67a61112006-05-30 00:37:20 -05002079 if (flag == STOP_CONN_TERM)
2080 session->state = ISCSI_STATE_TERMINATE;
2081 else if (conn->stop_stage != STOP_CONN_RECOVER)
2082 session->state = ISCSI_STATE_IN_RECOVERY;
Mike Christieed2abc72006-05-02 19:46:40 -05002083
2084 old_stop_stage = conn->stop_stage;
Mike Christie7996a772006-04-06 21:13:41 -05002085 conn->stop_stage = flag;
Mike Christie67a61112006-05-30 00:37:20 -05002086 conn->c_stage = ISCSI_CONN_STOPPED;
Mike Christie7996a772006-04-06 21:13:41 -05002087 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05002088
2089 iscsi_suspend_tx(conn);
Mike Christie7996a772006-04-06 21:13:41 -05002090
Mike Christie1c834692006-07-24 15:47:26 -05002091 write_lock_bh(conn->recv_lock);
2092 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
2093 write_unlock_bh(conn->recv_lock);
Mike Christie7996a772006-04-06 21:13:41 -05002094
Mike Christie7996a772006-04-06 21:13:41 -05002095 /*
2096 * for connection level recovery we should not calculate
2097 * header digest. conn->hdr_size used for optimization
2098 * in hdr_extract() and will be re-negotiated at
2099 * set_param() time.
2100 */
2101 if (flag == STOP_CONN_RECOVER) {
2102 conn->hdrdgst_en = 0;
2103 conn->datadgst_en = 0;
Mike Christie656cffc2006-05-18 20:31:42 -05002104 if (session->state == ISCSI_STATE_IN_RECOVERY &&
Mike Christie67a61112006-05-30 00:37:20 -05002105 old_stop_stage != STOP_CONN_RECOVER) {
2106 debug_scsi("blocking session\n");
Mike Christie7996a772006-04-06 21:13:41 -05002107 iscsi_block_session(session_to_cls(session));
Mike Christie67a61112006-05-30 00:37:20 -05002108 }
Mike Christie7996a772006-04-06 21:13:41 -05002109 }
Mike Christie656cffc2006-05-18 20:31:42 -05002110
Mike Christie656cffc2006-05-18 20:31:42 -05002111 /*
2112 * flush queues.
2113 */
2114 spin_lock_bh(&session->lock);
Mike Christie843c0a82007-12-13 12:43:20 -06002115 fail_all_commands(conn, -1);
Mike Christie656cffc2006-05-18 20:31:42 -05002116 flush_control_queues(session, conn);
2117 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05002118 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05002119}
Mike Christie7996a772006-04-06 21:13:41 -05002120
2121void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
2122{
2123 struct iscsi_conn *conn = cls_conn->dd_data;
2124 struct iscsi_session *session = conn->session;
2125
2126 switch (flag) {
2127 case STOP_CONN_RECOVER:
2128 case STOP_CONN_TERM:
2129 iscsi_start_session_recovery(session, conn, flag);
Mike Christie8d2860b2006-05-02 19:46:47 -05002130 break;
Mike Christie7996a772006-04-06 21:13:41 -05002131 default:
Or Gerlitzbe2df722006-05-02 19:46:43 -05002132 printk(KERN_ERR "iscsi: invalid stop flag %d\n", flag);
Mike Christie7996a772006-04-06 21:13:41 -05002133 }
2134}
2135EXPORT_SYMBOL_GPL(iscsi_conn_stop);
2136
2137int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
2138 struct iscsi_cls_conn *cls_conn, int is_leading)
2139{
2140 struct iscsi_session *session = class_to_transport_session(cls_session);
Mike Christie98644042006-10-16 18:09:39 -04002141 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05002142
Mike Christie7996a772006-04-06 21:13:41 -05002143 spin_lock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05002144 if (is_leading)
2145 session->leadconn = conn;
Mike Christie98644042006-10-16 18:09:39 -04002146 spin_unlock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05002147
2148 /*
2149 * Unblock xmitworker(), Login Phase will pass through.
2150 */
2151 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
2152 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
2153 return 0;
2154}
2155EXPORT_SYMBOL_GPL(iscsi_conn_bind);
2156
Mike Christiea54a52c2006-06-28 12:00:23 -05002157
2158int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
2159 enum iscsi_param param, char *buf, int buflen)
2160{
2161 struct iscsi_conn *conn = cls_conn->dd_data;
2162 struct iscsi_session *session = conn->session;
2163 uint32_t value;
2164
2165 switch(param) {
Mike Christie843c0a82007-12-13 12:43:20 -06002166 case ISCSI_PARAM_FAST_ABORT:
2167 sscanf(buf, "%d", &session->fast_abort);
2168 break;
Mike Christief6d51802007-12-13 12:43:30 -06002169 case ISCSI_PARAM_ABORT_TMO:
2170 sscanf(buf, "%d", &session->abort_timeout);
2171 break;
2172 case ISCSI_PARAM_LU_RESET_TMO:
2173 sscanf(buf, "%d", &session->lu_reset_timeout);
2174 break;
2175 case ISCSI_PARAM_PING_TMO:
2176 sscanf(buf, "%d", &conn->ping_timeout);
2177 break;
2178 case ISCSI_PARAM_RECV_TMO:
2179 sscanf(buf, "%d", &conn->recv_timeout);
2180 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002181 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2182 sscanf(buf, "%d", &conn->max_recv_dlength);
2183 break;
2184 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2185 sscanf(buf, "%d", &conn->max_xmit_dlength);
2186 break;
2187 case ISCSI_PARAM_HDRDGST_EN:
2188 sscanf(buf, "%d", &conn->hdrdgst_en);
2189 break;
2190 case ISCSI_PARAM_DATADGST_EN:
2191 sscanf(buf, "%d", &conn->datadgst_en);
2192 break;
2193 case ISCSI_PARAM_INITIAL_R2T_EN:
2194 sscanf(buf, "%d", &session->initial_r2t_en);
2195 break;
2196 case ISCSI_PARAM_MAX_R2T:
2197 sscanf(buf, "%d", &session->max_r2t);
2198 break;
2199 case ISCSI_PARAM_IMM_DATA_EN:
2200 sscanf(buf, "%d", &session->imm_data_en);
2201 break;
2202 case ISCSI_PARAM_FIRST_BURST:
2203 sscanf(buf, "%d", &session->first_burst);
2204 break;
2205 case ISCSI_PARAM_MAX_BURST:
2206 sscanf(buf, "%d", &session->max_burst);
2207 break;
2208 case ISCSI_PARAM_PDU_INORDER_EN:
2209 sscanf(buf, "%d", &session->pdu_inorder_en);
2210 break;
2211 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2212 sscanf(buf, "%d", &session->dataseq_inorder_en);
2213 break;
2214 case ISCSI_PARAM_ERL:
2215 sscanf(buf, "%d", &session->erl);
2216 break;
2217 case ISCSI_PARAM_IFMARKER_EN:
2218 sscanf(buf, "%d", &value);
2219 BUG_ON(value);
2220 break;
2221 case ISCSI_PARAM_OFMARKER_EN:
2222 sscanf(buf, "%d", &value);
2223 BUG_ON(value);
2224 break;
2225 case ISCSI_PARAM_EXP_STATSN:
2226 sscanf(buf, "%u", &conn->exp_statsn);
2227 break;
Mike Christieb2c64162007-05-30 12:57:16 -05002228 case ISCSI_PARAM_USERNAME:
2229 kfree(session->username);
2230 session->username = kstrdup(buf, GFP_KERNEL);
2231 if (!session->username)
2232 return -ENOMEM;
2233 break;
2234 case ISCSI_PARAM_USERNAME_IN:
2235 kfree(session->username_in);
2236 session->username_in = kstrdup(buf, GFP_KERNEL);
2237 if (!session->username_in)
2238 return -ENOMEM;
2239 break;
2240 case ISCSI_PARAM_PASSWORD:
2241 kfree(session->password);
2242 session->password = kstrdup(buf, GFP_KERNEL);
2243 if (!session->password)
2244 return -ENOMEM;
2245 break;
2246 case ISCSI_PARAM_PASSWORD_IN:
2247 kfree(session->password_in);
2248 session->password_in = kstrdup(buf, GFP_KERNEL);
2249 if (!session->password_in)
2250 return -ENOMEM;
2251 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002252 case ISCSI_PARAM_TARGET_NAME:
2253 /* this should not change between logins */
2254 if (session->targetname)
2255 break;
2256
2257 session->targetname = kstrdup(buf, GFP_KERNEL);
2258 if (!session->targetname)
2259 return -ENOMEM;
2260 break;
2261 case ISCSI_PARAM_TPGT:
2262 sscanf(buf, "%d", &session->tpgt);
2263 break;
2264 case ISCSI_PARAM_PERSISTENT_PORT:
2265 sscanf(buf, "%d", &conn->persistent_port);
2266 break;
2267 case ISCSI_PARAM_PERSISTENT_ADDRESS:
2268 /*
2269 * this is the address returned in discovery so it should
2270 * not change between logins.
2271 */
2272 if (conn->persistent_address)
2273 break;
2274
2275 conn->persistent_address = kstrdup(buf, GFP_KERNEL);
2276 if (!conn->persistent_address)
2277 return -ENOMEM;
2278 break;
2279 default:
2280 return -ENOSYS;
2281 }
2282
2283 return 0;
2284}
2285EXPORT_SYMBOL_GPL(iscsi_set_param);
2286
2287int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
2288 enum iscsi_param param, char *buf)
2289{
2290 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
2291 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
2292 int len;
2293
2294 switch(param) {
Mike Christie843c0a82007-12-13 12:43:20 -06002295 case ISCSI_PARAM_FAST_ABORT:
2296 len = sprintf(buf, "%d\n", session->fast_abort);
2297 break;
Mike Christief6d51802007-12-13 12:43:30 -06002298 case ISCSI_PARAM_ABORT_TMO:
2299 len = sprintf(buf, "%d\n", session->abort_timeout);
2300 break;
2301 case ISCSI_PARAM_LU_RESET_TMO:
2302 len = sprintf(buf, "%d\n", session->lu_reset_timeout);
2303 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002304 case ISCSI_PARAM_INITIAL_R2T_EN:
2305 len = sprintf(buf, "%d\n", session->initial_r2t_en);
2306 break;
2307 case ISCSI_PARAM_MAX_R2T:
2308 len = sprintf(buf, "%hu\n", session->max_r2t);
2309 break;
2310 case ISCSI_PARAM_IMM_DATA_EN:
2311 len = sprintf(buf, "%d\n", session->imm_data_en);
2312 break;
2313 case ISCSI_PARAM_FIRST_BURST:
2314 len = sprintf(buf, "%u\n", session->first_burst);
2315 break;
2316 case ISCSI_PARAM_MAX_BURST:
2317 len = sprintf(buf, "%u\n", session->max_burst);
2318 break;
2319 case ISCSI_PARAM_PDU_INORDER_EN:
2320 len = sprintf(buf, "%d\n", session->pdu_inorder_en);
2321 break;
2322 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2323 len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
2324 break;
2325 case ISCSI_PARAM_ERL:
2326 len = sprintf(buf, "%d\n", session->erl);
2327 break;
2328 case ISCSI_PARAM_TARGET_NAME:
2329 len = sprintf(buf, "%s\n", session->targetname);
2330 break;
2331 case ISCSI_PARAM_TPGT:
2332 len = sprintf(buf, "%d\n", session->tpgt);
2333 break;
Mike Christieb2c64162007-05-30 12:57:16 -05002334 case ISCSI_PARAM_USERNAME:
2335 len = sprintf(buf, "%s\n", session->username);
2336 break;
2337 case ISCSI_PARAM_USERNAME_IN:
2338 len = sprintf(buf, "%s\n", session->username_in);
2339 break;
2340 case ISCSI_PARAM_PASSWORD:
2341 len = sprintf(buf, "%s\n", session->password);
2342 break;
2343 case ISCSI_PARAM_PASSWORD_IN:
2344 len = sprintf(buf, "%s\n", session->password_in);
2345 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002346 default:
2347 return -ENOSYS;
2348 }
2349
2350 return len;
2351}
2352EXPORT_SYMBOL_GPL(iscsi_session_get_param);
2353
2354int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
2355 enum iscsi_param param, char *buf)
2356{
2357 struct iscsi_conn *conn = cls_conn->dd_data;
2358 int len;
2359
2360 switch(param) {
Mike Christief6d51802007-12-13 12:43:30 -06002361 case ISCSI_PARAM_PING_TMO:
2362 len = sprintf(buf, "%u\n", conn->ping_timeout);
2363 break;
2364 case ISCSI_PARAM_RECV_TMO:
2365 len = sprintf(buf, "%u\n", conn->recv_timeout);
2366 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002367 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2368 len = sprintf(buf, "%u\n", conn->max_recv_dlength);
2369 break;
2370 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2371 len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
2372 break;
2373 case ISCSI_PARAM_HDRDGST_EN:
2374 len = sprintf(buf, "%d\n", conn->hdrdgst_en);
2375 break;
2376 case ISCSI_PARAM_DATADGST_EN:
2377 len = sprintf(buf, "%d\n", conn->datadgst_en);
2378 break;
2379 case ISCSI_PARAM_IFMARKER_EN:
2380 len = sprintf(buf, "%d\n", conn->ifmarker_en);
2381 break;
2382 case ISCSI_PARAM_OFMARKER_EN:
2383 len = sprintf(buf, "%d\n", conn->ofmarker_en);
2384 break;
2385 case ISCSI_PARAM_EXP_STATSN:
2386 len = sprintf(buf, "%u\n", conn->exp_statsn);
2387 break;
2388 case ISCSI_PARAM_PERSISTENT_PORT:
2389 len = sprintf(buf, "%d\n", conn->persistent_port);
2390 break;
2391 case ISCSI_PARAM_PERSISTENT_ADDRESS:
2392 len = sprintf(buf, "%s\n", conn->persistent_address);
2393 break;
2394 default:
2395 return -ENOSYS;
2396 }
2397
2398 return len;
2399}
2400EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
2401
Mike Christie0801c242007-05-30 12:57:12 -05002402int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2403 char *buf)
2404{
2405 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
2406 int len;
2407
2408 switch (param) {
Mike Christied8196ed2007-05-30 12:57:25 -05002409 case ISCSI_HOST_PARAM_NETDEV_NAME:
2410 if (!session->netdev)
2411 len = sprintf(buf, "%s\n", "default");
2412 else
2413 len = sprintf(buf, "%s\n", session->netdev);
2414 break;
Mike Christie0801c242007-05-30 12:57:12 -05002415 case ISCSI_HOST_PARAM_HWADDRESS:
2416 if (!session->hwaddress)
2417 len = sprintf(buf, "%s\n", "default");
2418 else
2419 len = sprintf(buf, "%s\n", session->hwaddress);
2420 break;
Mike Christie8ad57812007-05-30 12:57:13 -05002421 case ISCSI_HOST_PARAM_INITIATOR_NAME:
2422 if (!session->initiatorname)
2423 len = sprintf(buf, "%s\n", "unknown");
2424 else
2425 len = sprintf(buf, "%s\n", session->initiatorname);
2426 break;
2427
Mike Christie0801c242007-05-30 12:57:12 -05002428 default:
2429 return -ENOSYS;
2430 }
2431
2432 return len;
2433}
2434EXPORT_SYMBOL_GPL(iscsi_host_get_param);
2435
2436int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2437 char *buf, int buflen)
2438{
2439 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
2440
2441 switch (param) {
Mike Christied8196ed2007-05-30 12:57:25 -05002442 case ISCSI_HOST_PARAM_NETDEV_NAME:
2443 if (!session->netdev)
2444 session->netdev = kstrdup(buf, GFP_KERNEL);
2445 break;
Mike Christie0801c242007-05-30 12:57:12 -05002446 case ISCSI_HOST_PARAM_HWADDRESS:
2447 if (!session->hwaddress)
2448 session->hwaddress = kstrdup(buf, GFP_KERNEL);
2449 break;
Mike Christie8ad57812007-05-30 12:57:13 -05002450 case ISCSI_HOST_PARAM_INITIATOR_NAME:
2451 if (!session->initiatorname)
2452 session->initiatorname = kstrdup(buf, GFP_KERNEL);
2453 break;
Mike Christie0801c242007-05-30 12:57:12 -05002454 default:
2455 return -ENOSYS;
2456 }
2457
2458 return 0;
2459}
2460EXPORT_SYMBOL_GPL(iscsi_host_set_param);
2461
Mike Christie7996a772006-04-06 21:13:41 -05002462MODULE_AUTHOR("Mike Christie");
2463MODULE_DESCRIPTION("iSCSI library functions");
2464MODULE_LICENSE("GPL");