blob: 10ba76285852953ee1970caaad9573ed9d1c7ecd [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>
vignesh babu11836572007-12-13 12:43:41 -060027#include <linux/log2.h>
Mike Christie8eb00532007-02-28 17:32:19 -060028#include <asm/unaligned.h>
Mike Christie7996a772006-04-06 21:13:41 -050029#include <net/tcp.h>
30#include <scsi/scsi_cmnd.h>
31#include <scsi/scsi_device.h>
32#include <scsi/scsi_eh.h>
33#include <scsi/scsi_tcq.h>
34#include <scsi/scsi_host.h>
35#include <scsi/scsi.h>
36#include <scsi/iscsi_proto.h>
37#include <scsi/scsi_transport.h>
38#include <scsi/scsi_transport_iscsi.h>
39#include <scsi/libiscsi.h>
40
41struct iscsi_session *
42class_to_transport_session(struct iscsi_cls_session *cls_session)
43{
44 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
45 return iscsi_hostdata(shost->hostdata);
46}
47EXPORT_SYMBOL_GPL(class_to_transport_session);
48
Mike Christie77a23c22007-05-30 12:57:18 -050049/* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
50#define SNA32_CHECK 2147483648UL
Mike Christie7996a772006-04-06 21:13:41 -050051
Mike Christie77a23c22007-05-30 12:57:18 -050052static int iscsi_sna_lt(u32 n1, u32 n2)
53{
54 return n1 != n2 && ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
55 (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
56}
57
58/* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
59static int iscsi_sna_lte(u32 n1, u32 n2)
60{
61 return n1 == n2 || ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
62 (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
63}
64
65void
66iscsi_update_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr)
Mike Christie7996a772006-04-06 21:13:41 -050067{
68 uint32_t max_cmdsn = be32_to_cpu(hdr->max_cmdsn);
69 uint32_t exp_cmdsn = be32_to_cpu(hdr->exp_cmdsn);
70
Mike Christie77a23c22007-05-30 12:57:18 -050071 /*
72 * standard specifies this check for when to update expected and
73 * max sequence numbers
74 */
75 if (iscsi_sna_lt(max_cmdsn, exp_cmdsn - 1))
76 return;
77
78 if (exp_cmdsn != session->exp_cmdsn &&
79 !iscsi_sna_lt(exp_cmdsn, session->exp_cmdsn))
Mike Christie7996a772006-04-06 21:13:41 -050080 session->exp_cmdsn = exp_cmdsn;
81
Mike Christie77a23c22007-05-30 12:57:18 -050082 if (max_cmdsn != session->max_cmdsn &&
83 !iscsi_sna_lt(max_cmdsn, session->max_cmdsn)) {
84 session->max_cmdsn = max_cmdsn;
85 /*
86 * if the window closed with IO queued, then kick the
87 * xmit thread
88 */
89 if (!list_empty(&session->leadconn->xmitqueue) ||
Mike Christie843c0a82007-12-13 12:43:20 -060090 !list_empty(&session->leadconn->mgmtqueue))
Mike Christie77a23c22007-05-30 12:57:18 -050091 scsi_queue_work(session->host,
92 &session->leadconn->xmitwork);
93 }
Mike Christie7996a772006-04-06 21:13:41 -050094}
Mike Christie77a23c22007-05-30 12:57:18 -050095EXPORT_SYMBOL_GPL(iscsi_update_cmdsn);
Mike Christie7996a772006-04-06 21:13:41 -050096
97void iscsi_prep_unsolicit_data_pdu(struct iscsi_cmd_task *ctask,
Mike Christieffd04362006-08-31 18:09:24 -040098 struct iscsi_data *hdr)
Mike Christie7996a772006-04-06 21:13:41 -050099{
100 struct iscsi_conn *conn = ctask->conn;
101
102 memset(hdr, 0, sizeof(struct iscsi_data));
103 hdr->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
104 hdr->datasn = cpu_to_be32(ctask->unsol_datasn);
105 ctask->unsol_datasn++;
106 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
107 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
108
109 hdr->itt = ctask->hdr->itt;
110 hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
Mike Christieffd04362006-08-31 18:09:24 -0400111 hdr->offset = cpu_to_be32(ctask->unsol_offset);
Mike Christie7996a772006-04-06 21:13:41 -0500112
113 if (ctask->unsol_count > conn->max_xmit_dlength) {
114 hton24(hdr->dlength, conn->max_xmit_dlength);
115 ctask->data_count = conn->max_xmit_dlength;
Mike Christieffd04362006-08-31 18:09:24 -0400116 ctask->unsol_offset += ctask->data_count;
Mike Christie7996a772006-04-06 21:13:41 -0500117 hdr->flags = 0;
118 } else {
119 hton24(hdr->dlength, ctask->unsol_count);
120 ctask->data_count = ctask->unsol_count;
121 hdr->flags = ISCSI_FLAG_CMD_FINAL;
122 }
123}
124EXPORT_SYMBOL_GPL(iscsi_prep_unsolicit_data_pdu);
125
Boaz Harrosh004d6532007-12-13 12:43:23 -0600126static int iscsi_add_hdr(struct iscsi_cmd_task *ctask, unsigned len)
127{
128 unsigned exp_len = ctask->hdr_len + len;
129
130 if (exp_len > ctask->hdr_max) {
131 WARN_ON(1);
132 return -EINVAL;
133 }
134
135 WARN_ON(len & (ISCSI_PAD_LEN - 1)); /* caller must pad the AHS */
136 ctask->hdr_len = exp_len;
137 return 0;
138}
139
Mike Christie7996a772006-04-06 21:13:41 -0500140/**
141 * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
142 * @ctask: iscsi cmd task
143 *
144 * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
145 * fields like dlength or final based on how much data it sends
146 */
Boaz Harrosh004d6532007-12-13 12:43:23 -0600147static int iscsi_prep_scsi_cmd_pdu(struct iscsi_cmd_task *ctask)
Mike Christie7996a772006-04-06 21:13:41 -0500148{
149 struct iscsi_conn *conn = ctask->conn;
150 struct iscsi_session *session = conn->session;
151 struct iscsi_cmd *hdr = ctask->hdr;
152 struct scsi_cmnd *sc = ctask->sc;
Boaz Harrosh004d6532007-12-13 12:43:23 -0600153 unsigned hdrlength;
154 int rc;
Mike Christie7996a772006-04-06 21:13:41 -0500155
Boaz Harrosh004d6532007-12-13 12:43:23 -0600156 ctask->hdr_len = 0;
157 rc = iscsi_add_hdr(ctask, sizeof(*hdr));
158 if (rc)
159 return rc;
Olaf Kircha8ac6312007-12-13 12:43:35 -0600160 hdr->opcode = ISCSI_OP_SCSI_CMD;
161 hdr->flags = ISCSI_ATTR_SIMPLE;
162 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
163 hdr->itt = build_itt(ctask->itt, conn->id, session->age);
164 hdr->data_length = cpu_to_be32(scsi_bufflen(sc));
165 hdr->cmdsn = cpu_to_be32(session->cmdsn);
166 session->cmdsn++;
167 hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
168 memcpy(hdr->cdb, sc->cmnd, sc->cmd_len);
Mike Christied473cc72007-05-30 12:57:14 -0500169 if (sc->cmd_len < MAX_COMMAND_SIZE)
170 memset(&hdr->cdb[sc->cmd_len], 0,
171 MAX_COMMAND_SIZE - sc->cmd_len);
Mike Christie7996a772006-04-06 21:13:41 -0500172
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);
Olaf Kircha8ac6312007-12-13 12:43:35 -0600201 hton24(hdr->dlength, ctask->imm_count);
Mike Christie7996a772006-04-06 21:13:41 -0500202 } else
Olaf Kircha8ac6312007-12-13 12:43:35 -0600203 zero_data(hdr->dlength);
Mike Christie7996a772006-04-06 21:13:41 -0500204
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 */
Olaf Kircha8ac6312007-12-13 12:43:35 -0600213 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
Mike Christie7996a772006-04-06 21:13:41 -0500214 } 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
Olaf Kircha8ac6312007-12-13 12:43:35 -0600231 if (conn->session->tt->init_cmd_task(conn->ctask))
232 return EIO;
Mike Christie77a23c22007-05-30 12:57:18 -0500233
Olaf Kircha8ac6312007-12-13 12:43:35 -0600234 conn->scsicmd_pdus_cnt++;
235 debug_scsi("iscsi prep [%s cid %d sc %p cdb 0x%x itt 0x%x len %d "
Mike Christie77a23c22007-05-30 12:57:18 -0500236 "cmdsn %d win %d]\n",
Olaf Kircha8ac6312007-12-13 12:43:35 -0600237 sc->sc_data_direction == DMA_TO_DEVICE ? "write" : "read",
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900238 conn->id, sc, sc->cmnd[0], ctask->itt, scsi_bufflen(sc),
Olaf Kircha8ac6312007-12-13 12:43:35 -0600239 session->cmdsn, session->max_cmdsn - session->exp_cmdsn + 1);
Boaz Harrosh004d6532007-12-13 12:43:23 -0600240 return 0;
Mike Christie7996a772006-04-06 21:13:41 -0500241}
Mike Christie7996a772006-04-06 21:13:41 -0500242
243/**
244 * iscsi_complete_command - return command back to scsi-ml
Mike Christie7996a772006-04-06 21:13:41 -0500245 * @ctask: iscsi cmd task
246 *
247 * Must be called with session lock.
248 * This function returns the scsi command to scsi-ml and returns
249 * the cmd task to the pool of available cmd tasks.
250 */
Mike Christie60ecebf2006-08-31 18:09:25 -0400251static void iscsi_complete_command(struct iscsi_cmd_task *ctask)
Mike Christie7996a772006-04-06 21:13:41 -0500252{
Mike Christiec1635cb2007-12-13 12:43:33 -0600253 struct iscsi_conn *conn = ctask->conn;
254 struct iscsi_session *session = conn->session;
Mike Christie7996a772006-04-06 21:13:41 -0500255 struct scsi_cmnd *sc = ctask->sc;
256
Mike Christieb6c395e2006-07-24 15:47:15 -0500257 ctask->state = ISCSI_TASK_COMPLETED;
Mike Christie7996a772006-04-06 21:13:41 -0500258 ctask->sc = NULL;
Mike Christief47f2cf2006-08-31 18:09:33 -0400259 /* SCSI eh reuses commands to verify us */
260 sc->SCp.ptr = NULL;
Mike Christiec1635cb2007-12-13 12:43:33 -0600261 if (conn->ctask == ctask)
262 conn->ctask = NULL;
Mike Christie7996a772006-04-06 21:13:41 -0500263 list_del_init(&ctask->running);
264 __kfifo_put(session->cmdpool.queue, (void*)&ctask, sizeof(void*));
265 sc->scsi_done(sc);
266}
267
Mike Christie60ecebf2006-08-31 18:09:25 -0400268static void __iscsi_get_ctask(struct iscsi_cmd_task *ctask)
269{
270 atomic_inc(&ctask->refcount);
271}
272
Mike Christie60ecebf2006-08-31 18:09:25 -0400273static void __iscsi_put_ctask(struct iscsi_cmd_task *ctask)
274{
Mike Christiee648f632006-08-31 18:09:34 -0400275 if (atomic_dec_and_test(&ctask->refcount))
Mike Christie60ecebf2006-08-31 18:09:25 -0400276 iscsi_complete_command(ctask);
Mike Christie60ecebf2006-08-31 18:09:25 -0400277}
278
Mike Christieb3a7ea82007-12-13 12:43:26 -0600279/*
280 * session lock must be held
281 */
282static void fail_command(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
283 int err)
284{
285 struct scsi_cmnd *sc;
286
287 sc = ctask->sc;
288 if (!sc)
289 return;
290
291 if (ctask->state == ISCSI_TASK_PENDING)
292 /*
293 * cmd never made it to the xmit thread, so we should not count
294 * the cmd in the sequencing
295 */
296 conn->session->queued_cmdsn--;
297 else
298 conn->session->tt->cleanup_cmd_task(conn, ctask);
299
300 sc->result = err;
301 scsi_set_resid(sc, scsi_bufflen(sc));
302 if (conn->ctask == ctask)
303 conn->ctask = NULL;
304 /* release ref from queuecommand */
305 __iscsi_put_ctask(ctask);
306}
307
308/**
309 * iscsi_free_mgmt_task - return mgmt task back to pool
310 * @conn: iscsi connection
311 * @mtask: mtask
312 *
313 * Must be called with session lock.
314 */
315void iscsi_free_mgmt_task(struct iscsi_conn *conn,
316 struct iscsi_mgmt_task *mtask)
317{
318 list_del_init(&mtask->running);
319 if (conn->login_mtask == mtask)
320 return;
Mike Christief6d51802007-12-13 12:43:30 -0600321
322 if (conn->ping_mtask == mtask)
323 conn->ping_mtask = NULL;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600324 __kfifo_put(conn->session->mgmtpool.queue,
325 (void*)&mtask, sizeof(void*));
326}
327EXPORT_SYMBOL_GPL(iscsi_free_mgmt_task);
328
Mike Christief6d51802007-12-13 12:43:30 -0600329static struct iscsi_mgmt_task *
330__iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
331 char *data, uint32_t data_size)
332{
333 struct iscsi_session *session = conn->session;
334 struct iscsi_mgmt_task *mtask;
335
336 if (session->state == ISCSI_STATE_TERMINATE)
337 return NULL;
338
339 if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) ||
340 hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
341 /*
342 * Login and Text are sent serially, in
343 * request-followed-by-response sequence.
344 * Same mtask can be used. Same ITT must be used.
345 * Note that login_mtask is preallocated at conn_create().
346 */
347 mtask = conn->login_mtask;
348 else {
349 BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
350 BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
351
352 if (!__kfifo_get(session->mgmtpool.queue,
353 (void*)&mtask, sizeof(void*)))
354 return NULL;
355 }
356
357 if (data_size) {
358 memcpy(mtask->data, data, data_size);
359 mtask->data_count = data_size;
360 } else
361 mtask->data_count = 0;
362
363 memcpy(mtask->hdr, hdr, sizeof(struct iscsi_hdr));
364 INIT_LIST_HEAD(&mtask->running);
365 list_add_tail(&mtask->running, &conn->mgmtqueue);
366 return mtask;
367}
368
369int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
370 char *data, uint32_t data_size)
371{
372 struct iscsi_conn *conn = cls_conn->dd_data;
373 struct iscsi_session *session = conn->session;
374 int err = 0;
375
376 spin_lock_bh(&session->lock);
377 if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size))
378 err = -EPERM;
379 spin_unlock_bh(&session->lock);
380 scsi_queue_work(session->host, &conn->xmitwork);
381 return err;
382}
383EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
384
Mike Christie7996a772006-04-06 21:13:41 -0500385/**
386 * iscsi_cmd_rsp - SCSI Command Response processing
387 * @conn: iscsi connection
388 * @hdr: iscsi header
389 * @ctask: scsi command task
390 * @data: cmd data buffer
391 * @datalen: len of buffer
392 *
393 * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
394 * then completes the command and task.
395 **/
Mike Christie77a23c22007-05-30 12:57:18 -0500396static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
397 struct iscsi_cmd_task *ctask, char *data,
398 int datalen)
Mike Christie7996a772006-04-06 21:13:41 -0500399{
Mike Christie7996a772006-04-06 21:13:41 -0500400 struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)hdr;
401 struct iscsi_session *session = conn->session;
402 struct scsi_cmnd *sc = ctask->sc;
403
Mike Christie77a23c22007-05-30 12:57:18 -0500404 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
Mike Christie7996a772006-04-06 21:13:41 -0500405 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
406
407 sc->result = (DID_OK << 16) | rhdr->cmd_status;
408
409 if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
410 sc->result = DID_ERROR << 16;
411 goto out;
412 }
413
414 if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
Mike Christie9b80cb42006-12-17 12:10:28 -0600415 uint16_t senselen;
Mike Christie7996a772006-04-06 21:13:41 -0500416
417 if (datalen < 2) {
418invalid_datalen:
Mike Christie322d7392008-01-31 13:36:52 -0600419 iscsi_conn_printk(KERN_ERR, conn,
420 "Got CHECK_CONDITION but invalid data "
421 "buffer size of %d\n", datalen);
Mike Christie7996a772006-04-06 21:13:41 -0500422 sc->result = DID_BAD_TARGET << 16;
423 goto out;
424 }
425
Mike Christie8eb00532007-02-28 17:32:19 -0600426 senselen = be16_to_cpu(get_unaligned((__be16 *) data));
Mike Christie7996a772006-04-06 21:13:41 -0500427 if (datalen < senselen)
428 goto invalid_datalen;
429
430 memcpy(sc->sense_buffer, data + 2,
Mike Christie9b80cb42006-12-17 12:10:28 -0600431 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
Mike Christie7996a772006-04-06 21:13:41 -0500432 debug_scsi("copied %d bytes of sense\n",
Mike Christie8eb00532007-02-28 17:32:19 -0600433 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
Mike Christie7996a772006-04-06 21:13:41 -0500434 }
435
Boaz Harrosh7207fea2007-12-13 12:43:22 -0600436 if (rhdr->flags & (ISCSI_FLAG_CMD_UNDERFLOW |
437 ISCSI_FLAG_CMD_OVERFLOW)) {
Mike Christie7996a772006-04-06 21:13:41 -0500438 int res_count = be32_to_cpu(rhdr->residual_count);
439
Boaz Harrosh7207fea2007-12-13 12:43:22 -0600440 if (res_count > 0 &&
441 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
442 res_count <= scsi_bufflen(sc)))
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900443 scsi_set_resid(sc, res_count);
Mike Christie7996a772006-04-06 21:13:41 -0500444 else
445 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
Boaz Harrosh7207fea2007-12-13 12:43:22 -0600446 } else if (rhdr->flags & (ISCSI_FLAG_CMD_BIDI_UNDERFLOW |
447 ISCSI_FLAG_CMD_BIDI_OVERFLOW))
Mike Christie7996a772006-04-06 21:13:41 -0500448 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
Mike Christie7996a772006-04-06 21:13:41 -0500449
450out:
451 debug_scsi("done [sc %lx res %d itt 0x%x]\n",
452 (long)sc, sc->result, ctask->itt);
453 conn->scsirsp_pdus_cnt++;
454
Mike Christie60ecebf2006-08-31 18:09:25 -0400455 __iscsi_put_ctask(ctask);
Mike Christie7996a772006-04-06 21:13:41 -0500456}
457
Mike Christie7ea8b822006-07-24 15:47:22 -0500458static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
459{
460 struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
461
462 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
463 conn->tmfrsp_pdus_cnt++;
464
Mike Christie843c0a82007-12-13 12:43:20 -0600465 if (conn->tmf_state != TMF_QUEUED)
Mike Christie7ea8b822006-07-24 15:47:22 -0500466 return;
467
468 if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
Mike Christie843c0a82007-12-13 12:43:20 -0600469 conn->tmf_state = TMF_SUCCESS;
Mike Christie7ea8b822006-07-24 15:47:22 -0500470 else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
Mike Christie843c0a82007-12-13 12:43:20 -0600471 conn->tmf_state = TMF_NOT_FOUND;
Mike Christie7ea8b822006-07-24 15:47:22 -0500472 else
Mike Christie843c0a82007-12-13 12:43:20 -0600473 conn->tmf_state = TMF_FAILED;
Mike Christie7ea8b822006-07-24 15:47:22 -0500474 wake_up(&conn->ehwait);
475}
476
Mike Christief6d51802007-12-13 12:43:30 -0600477static void iscsi_send_nopout(struct iscsi_conn *conn, struct iscsi_nopin *rhdr)
478{
479 struct iscsi_nopout hdr;
480 struct iscsi_mgmt_task *mtask;
481
482 if (!rhdr && conn->ping_mtask)
483 return;
484
485 memset(&hdr, 0, sizeof(struct iscsi_nopout));
486 hdr.opcode = ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE;
487 hdr.flags = ISCSI_FLAG_CMD_FINAL;
488
489 if (rhdr) {
490 memcpy(hdr.lun, rhdr->lun, 8);
491 hdr.ttt = rhdr->ttt;
492 hdr.itt = RESERVED_ITT;
493 } else
494 hdr.ttt = RESERVED_ITT;
495
496 mtask = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)&hdr, NULL, 0);
497 if (!mtask) {
Mike Christie322d7392008-01-31 13:36:52 -0600498 iscsi_conn_printk(KERN_ERR, conn, "Could not send nopout\n");
Mike Christief6d51802007-12-13 12:43:30 -0600499 return;
500 }
501
502 /* only track our nops */
503 if (!rhdr) {
504 conn->ping_mtask = mtask;
505 conn->last_ping = jiffies;
506 }
507 scsi_queue_work(conn->session->host, &conn->xmitwork);
508}
509
Mike Christie62f38302006-08-31 18:09:27 -0400510static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
511 char *data, int datalen)
512{
513 struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
514 struct iscsi_hdr rejected_pdu;
515 uint32_t itt;
516
517 conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
518
519 if (reject->reason == ISCSI_REASON_DATA_DIGEST_ERROR) {
520 if (ntoh24(reject->dlength) > datalen)
521 return ISCSI_ERR_PROTO;
522
523 if (ntoh24(reject->dlength) >= sizeof(struct iscsi_hdr)) {
524 memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
Al Virob4377352007-02-09 16:39:40 +0000525 itt = get_itt(rejected_pdu.itt);
Mike Christie322d7392008-01-31 13:36:52 -0600526 iscsi_conn_printk(KERN_ERR, conn,
527 "itt 0x%x had pdu (op 0x%x) rejected "
528 "due to DataDigest error.\n", itt,
529 rejected_pdu.opcode);
Mike Christie62f38302006-08-31 18:09:27 -0400530 }
531 }
532 return 0;
533}
534
Mike Christie7996a772006-04-06 21:13:41 -0500535/**
536 * __iscsi_complete_pdu - complete pdu
537 * @conn: iscsi conn
538 * @hdr: iscsi header
539 * @data: data buffer
540 * @datalen: len of data buffer
541 *
542 * Completes pdu processing by freeing any resources allocated at
543 * queuecommand or send generic. session lock must be held and verify
544 * itt must have been called.
545 */
Adrian Bunkf8d9d652008-01-29 00:11:27 +0200546static int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
547 char *data, int datalen)
Mike Christie7996a772006-04-06 21:13:41 -0500548{
549 struct iscsi_session *session = conn->session;
550 int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
551 struct iscsi_cmd_task *ctask;
552 struct iscsi_mgmt_task *mtask;
553 uint32_t itt;
554
Mike Christief6d51802007-12-13 12:43:30 -0600555 conn->last_recv = jiffies;
Al Virob4377352007-02-09 16:39:40 +0000556 if (hdr->itt != RESERVED_ITT)
557 itt = get_itt(hdr->itt);
Mike Christie7996a772006-04-06 21:13:41 -0500558 else
Al Virob4377352007-02-09 16:39:40 +0000559 itt = ~0U;
Mike Christie7996a772006-04-06 21:13:41 -0500560
561 if (itt < session->cmds_max) {
562 ctask = session->cmds[itt];
563
564 debug_scsi("cmdrsp [op 0x%x cid %d itt 0x%x len %d]\n",
565 opcode, conn->id, ctask->itt, datalen);
566
567 switch(opcode) {
568 case ISCSI_OP_SCSI_CMD_RSP:
569 BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
Mike Christie77a23c22007-05-30 12:57:18 -0500570 iscsi_scsi_cmd_rsp(conn, hdr, ctask, data,
571 datalen);
Mike Christie7996a772006-04-06 21:13:41 -0500572 break;
573 case ISCSI_OP_SCSI_DATA_IN:
574 BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
575 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
576 conn->scsirsp_pdus_cnt++;
Mike Christie60ecebf2006-08-31 18:09:25 -0400577 __iscsi_put_ctask(ctask);
Mike Christie7996a772006-04-06 21:13:41 -0500578 }
579 break;
580 case ISCSI_OP_R2T:
581 /* LLD handles this for now */
582 break;
583 default:
584 rc = ISCSI_ERR_BAD_OPCODE;
585 break;
586 }
587 } else if (itt >= ISCSI_MGMT_ITT_OFFSET &&
588 itt < ISCSI_MGMT_ITT_OFFSET + session->mgmtpool_max) {
589 mtask = session->mgmt_cmds[itt - ISCSI_MGMT_ITT_OFFSET];
590
591 debug_scsi("immrsp [op 0x%x cid %d itt 0x%x len %d]\n",
592 opcode, conn->id, mtask->itt, datalen);
593
Mike Christie77a23c22007-05-30 12:57:18 -0500594 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
Mike Christie7996a772006-04-06 21:13:41 -0500595 switch(opcode) {
Mike Christie8d2860b2006-05-02 19:46:47 -0500596 case ISCSI_OP_LOGOUT_RSP:
Mike Christiec8dc1e52006-07-24 15:47:39 -0500597 if (datalen) {
598 rc = ISCSI_ERR_PROTO;
599 break;
600 }
Mike Christie8d2860b2006-05-02 19:46:47 -0500601 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
602 /* fall through */
Mike Christie7996a772006-04-06 21:13:41 -0500603 case ISCSI_OP_LOGIN_RSP:
604 case ISCSI_OP_TEXT_RSP:
Mike Christie8d2860b2006-05-02 19:46:47 -0500605 /*
606 * login related PDU's exp_statsn is handled in
607 * userspace
608 */
Mike Christie40527af2006-07-24 15:47:45 -0500609 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
610 rc = ISCSI_ERR_CONN_FAILED;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600611 iscsi_free_mgmt_task(conn, mtask);
Mike Christie7996a772006-04-06 21:13:41 -0500612 break;
613 case ISCSI_OP_SCSI_TMFUNC_RSP:
Mike Christie7996a772006-04-06 21:13:41 -0500614 if (datalen) {
615 rc = ISCSI_ERR_PROTO;
616 break;
617 }
Mike Christie8d2860b2006-05-02 19:46:47 -0500618
Mike Christie7ea8b822006-07-24 15:47:22 -0500619 iscsi_tmf_rsp(conn, hdr);
Mike Christieb3a7ea82007-12-13 12:43:26 -0600620 iscsi_free_mgmt_task(conn, mtask);
Mike Christie7996a772006-04-06 21:13:41 -0500621 break;
622 case ISCSI_OP_NOOP_IN:
Mike Christief6d51802007-12-13 12:43:30 -0600623 if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) ||
624 datalen) {
Mike Christie7996a772006-04-06 21:13:41 -0500625 rc = ISCSI_ERR_PROTO;
626 break;
627 }
Mike Christie7996a772006-04-06 21:13:41 -0500628 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
629
Mike Christief6d51802007-12-13 12:43:30 -0600630 if (conn->ping_mtask != mtask) {
631 /*
632 * If this is not in response to one of our
633 * nops then it must be from userspace.
634 */
635 if (iscsi_recv_pdu(conn->cls_conn, hdr, data,
636 datalen))
637 rc = ISCSI_ERR_CONN_FAILED;
638 }
Mike Christieb3a7ea82007-12-13 12:43:26 -0600639 iscsi_free_mgmt_task(conn, mtask);
Mike Christie7996a772006-04-06 21:13:41 -0500640 break;
641 default:
642 rc = ISCSI_ERR_BAD_OPCODE;
643 break;
644 }
Al Virob4377352007-02-09 16:39:40 +0000645 } else if (itt == ~0U) {
Mike Christie77a23c22007-05-30 12:57:18 -0500646 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
Mike Christie62f38302006-08-31 18:09:27 -0400647
Mike Christie7996a772006-04-06 21:13:41 -0500648 switch(opcode) {
649 case ISCSI_OP_NOOP_IN:
Mike Christie40527af2006-07-24 15:47:45 -0500650 if (datalen) {
Mike Christie7996a772006-04-06 21:13:41 -0500651 rc = ISCSI_ERR_PROTO;
Mike Christie40527af2006-07-24 15:47:45 -0500652 break;
653 }
654
Al Virob4377352007-02-09 16:39:40 +0000655 if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
Mike Christie40527af2006-07-24 15:47:45 -0500656 break;
657
Mike Christief6d51802007-12-13 12:43:30 -0600658 iscsi_send_nopout(conn, (struct iscsi_nopin*)hdr);
Mike Christie7996a772006-04-06 21:13:41 -0500659 break;
660 case ISCSI_OP_REJECT:
Mike Christie62f38302006-08-31 18:09:27 -0400661 rc = iscsi_handle_reject(conn, hdr, data, datalen);
662 break;
Mike Christie7996a772006-04-06 21:13:41 -0500663 case ISCSI_OP_ASYNC_EVENT:
Mike Christie8d2860b2006-05-02 19:46:47 -0500664 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
Mike Christie5831c732006-10-16 18:09:41 -0400665 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
666 rc = ISCSI_ERR_CONN_FAILED;
Mike Christie7996a772006-04-06 21:13:41 -0500667 break;
668 default:
669 rc = ISCSI_ERR_BAD_OPCODE;
670 break;
671 }
672 } else
673 rc = ISCSI_ERR_BAD_ITT;
674
675 return rc;
676}
Mike Christie7996a772006-04-06 21:13:41 -0500677
678int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
679 char *data, int datalen)
680{
681 int rc;
682
683 spin_lock(&conn->session->lock);
684 rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
685 spin_unlock(&conn->session->lock);
686 return rc;
687}
688EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
689
690/* verify itt (itt encoding: age+cid+itt) */
691int iscsi_verify_itt(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
692 uint32_t *ret_itt)
693{
694 struct iscsi_session *session = conn->session;
695 struct iscsi_cmd_task *ctask;
696 uint32_t itt;
697
Al Virob4377352007-02-09 16:39:40 +0000698 if (hdr->itt != RESERVED_ITT) {
699 if (((__force u32)hdr->itt & ISCSI_AGE_MASK) !=
Mike Christie7996a772006-04-06 21:13:41 -0500700 (session->age << ISCSI_AGE_SHIFT)) {
Mike Christie322d7392008-01-31 13:36:52 -0600701 iscsi_conn_printk(KERN_ERR, conn,
702 "received itt %x expected session "
703 "age (%x)\n", (__force u32)hdr->itt,
704 session->age & ISCSI_AGE_MASK);
Mike Christie7996a772006-04-06 21:13:41 -0500705 return ISCSI_ERR_BAD_ITT;
706 }
707
Al Virob4377352007-02-09 16:39:40 +0000708 if (((__force u32)hdr->itt & ISCSI_CID_MASK) !=
Mike Christie7996a772006-04-06 21:13:41 -0500709 (conn->id << ISCSI_CID_SHIFT)) {
Mike Christie322d7392008-01-31 13:36:52 -0600710 iscsi_conn_printk(KERN_ERR, conn,
711 "iscsi: received itt %x, expected "
712 "CID (%x)\n",
713 (__force u32)hdr->itt, conn->id);
Mike Christie7996a772006-04-06 21:13:41 -0500714 return ISCSI_ERR_BAD_ITT;
715 }
Al Virob4377352007-02-09 16:39:40 +0000716 itt = get_itt(hdr->itt);
Mike Christie7996a772006-04-06 21:13:41 -0500717 } else
Al Virob4377352007-02-09 16:39:40 +0000718 itt = ~0U;
Mike Christie7996a772006-04-06 21:13:41 -0500719
720 if (itt < session->cmds_max) {
721 ctask = session->cmds[itt];
722
723 if (!ctask->sc) {
Mike Christie322d7392008-01-31 13:36:52 -0600724 iscsi_conn_printk(KERN_INFO, conn, "dropping ctask "
725 "with itt 0x%x\n", ctask->itt);
Mike Christie7996a772006-04-06 21:13:41 -0500726 /* force drop */
727 return ISCSI_ERR_NO_SCSI_CMD;
728 }
729
730 if (ctask->sc->SCp.phase != session->age) {
Mike Christie322d7392008-01-31 13:36:52 -0600731 iscsi_conn_printk(KERN_ERR, conn,
732 "iscsi: ctask's session age %d, "
733 "expected %d\n", ctask->sc->SCp.phase,
734 session->age);
Mike Christie7996a772006-04-06 21:13:41 -0500735 return ISCSI_ERR_SESSION_FAILED;
736 }
737 }
738
739 *ret_itt = itt;
740 return 0;
741}
742EXPORT_SYMBOL_GPL(iscsi_verify_itt);
743
744void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
745{
746 struct iscsi_session *session = conn->session;
747 unsigned long flags;
748
749 spin_lock_irqsave(&session->lock, flags);
Mike Christie656cffc2006-05-18 20:31:42 -0500750 if (session->state == ISCSI_STATE_FAILED) {
751 spin_unlock_irqrestore(&session->lock, flags);
752 return;
753 }
754
Mike Christie67a61112006-05-30 00:37:20 -0500755 if (conn->stop_stage == 0)
Mike Christie7996a772006-04-06 21:13:41 -0500756 session->state = ISCSI_STATE_FAILED;
757 spin_unlock_irqrestore(&session->lock, flags);
758 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
759 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
760 iscsi_conn_error(conn->cls_conn, err);
761}
762EXPORT_SYMBOL_GPL(iscsi_conn_failure);
763
Mike Christie77a23c22007-05-30 12:57:18 -0500764static void iscsi_prep_mtask(struct iscsi_conn *conn,
765 struct iscsi_mgmt_task *mtask)
766{
767 struct iscsi_session *session = conn->session;
768 struct iscsi_hdr *hdr = mtask->hdr;
769 struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
770
771 if (hdr->opcode != (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) &&
772 hdr->opcode != (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
773 nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
774 /*
775 * pre-format CmdSN for outgoing PDU.
776 */
777 nop->cmdsn = cpu_to_be32(session->cmdsn);
778 if (hdr->itt != RESERVED_ITT) {
779 hdr->itt = build_itt(mtask->itt, conn->id, session->age);
Mike Christiee0726402007-07-26 12:46:48 -0500780 /*
781 * TODO: We always use immediate, so we never hit this.
782 * If we start to send tmfs or nops as non-immediate then
783 * we should start checking the cmdsn numbers for mgmt tasks.
784 */
Mike Christie77a23c22007-05-30 12:57:18 -0500785 if (conn->c_stage == ISCSI_CONN_STARTED &&
Mike Christiee0726402007-07-26 12:46:48 -0500786 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
787 session->queued_cmdsn++;
Mike Christie77a23c22007-05-30 12:57:18 -0500788 session->cmdsn++;
Mike Christiee0726402007-07-26 12:46:48 -0500789 }
Mike Christie77a23c22007-05-30 12:57:18 -0500790 }
791
792 if (session->tt->init_mgmt_task)
793 session->tt->init_mgmt_task(conn, mtask);
794
795 debug_scsi("mgmtpdu [op 0x%x hdr->itt 0x%x datalen %d]\n",
Mike Christie843c0a82007-12-13 12:43:20 -0600796 hdr->opcode & ISCSI_OPCODE_MASK, hdr->itt,
797 mtask->data_count);
Mike Christie77a23c22007-05-30 12:57:18 -0500798}
799
Mike Christie05db8882007-02-28 17:32:16 -0600800static int iscsi_xmit_mtask(struct iscsi_conn *conn)
Mike Christieb5072ea2006-10-16 18:09:42 -0400801{
802 struct iscsi_hdr *hdr = conn->mtask->hdr;
Mike Christieb3a7ea82007-12-13 12:43:26 -0600803 int rc;
Mike Christieb5072ea2006-10-16 18:09:42 -0400804
Mike Christieb3a7ea82007-12-13 12:43:26 -0600805 if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT)
806 conn->session->state = ISCSI_STATE_LOGGING_OUT;
Mike Christie77a23c22007-05-30 12:57:18 -0500807 spin_unlock_bh(&conn->session->lock);
Mike Christieb3a7ea82007-12-13 12:43:26 -0600808
Mike Christieb5072ea2006-10-16 18:09:42 -0400809 rc = conn->session->tt->xmit_mgmt_task(conn, conn->mtask);
Mike Christie77a23c22007-05-30 12:57:18 -0500810 spin_lock_bh(&conn->session->lock);
Mike Christieb5072ea2006-10-16 18:09:42 -0400811 if (rc)
812 return rc;
813
Mike Christie05db8882007-02-28 17:32:16 -0600814 /* done with this in-progress mtask */
815 conn->mtask = NULL;
Mike Christieb5072ea2006-10-16 18:09:42 -0400816 return 0;
817}
818
Mike Christie77a23c22007-05-30 12:57:18 -0500819static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
820{
821 struct iscsi_session *session = conn->session;
822
823 /*
824 * Check for iSCSI window and take care of CmdSN wrap-around
825 */
Mike Christiee0726402007-07-26 12:46:48 -0500826 if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) {
827 debug_scsi("iSCSI CmdSN closed. ExpCmdSn %u MaxCmdSN %u "
828 "CmdSN %u/%u\n", session->exp_cmdsn,
829 session->max_cmdsn, session->cmdsn,
830 session->queued_cmdsn);
Mike Christie77a23c22007-05-30 12:57:18 -0500831 return -ENOSPC;
832 }
833 return 0;
834}
835
836static int iscsi_xmit_ctask(struct iscsi_conn *conn)
837{
838 struct iscsi_cmd_task *ctask = conn->ctask;
Mike Christie843c0a82007-12-13 12:43:20 -0600839 int rc;
Mike Christie77a23c22007-05-30 12:57:18 -0500840
841 __iscsi_get_ctask(ctask);
842 spin_unlock_bh(&conn->session->lock);
843 rc = conn->session->tt->xmit_cmd_task(conn, ctask);
844 spin_lock_bh(&conn->session->lock);
845 __iscsi_put_ctask(ctask);
Mike Christie77a23c22007-05-30 12:57:18 -0500846 if (!rc)
847 /* done with this ctask */
848 conn->ctask = NULL;
849 return rc;
850}
851
Mike Christie7996a772006-04-06 21:13:41 -0500852/**
Mike Christie843c0a82007-12-13 12:43:20 -0600853 * iscsi_requeue_ctask - requeue ctask to run from session workqueue
854 * @ctask: ctask to requeue
855 *
856 * LLDs that need to run a ctask from the session workqueue should call
857 * this. The session lock must be held.
858 */
859void iscsi_requeue_ctask(struct iscsi_cmd_task *ctask)
860{
861 struct iscsi_conn *conn = ctask->conn;
862
863 list_move_tail(&ctask->running, &conn->requeue);
864 scsi_queue_work(conn->session->host, &conn->xmitwork);
865}
866EXPORT_SYMBOL_GPL(iscsi_requeue_ctask);
867
868/**
Mike Christie7996a772006-04-06 21:13:41 -0500869 * iscsi_data_xmit - xmit any command into the scheduled connection
870 * @conn: iscsi connection
871 *
872 * Notes:
873 * The function can return -EAGAIN in which case the caller must
874 * re-schedule it again later or recover. '0' return code means
875 * successful xmit.
876 **/
877static int iscsi_data_xmit(struct iscsi_conn *conn)
878{
Mike Christie3219e522006-05-30 00:37:28 -0500879 int rc = 0;
Mike Christie7996a772006-04-06 21:13:41 -0500880
Mike Christie77a23c22007-05-30 12:57:18 -0500881 spin_lock_bh(&conn->session->lock);
Mike Christie7996a772006-04-06 21:13:41 -0500882 if (unlikely(conn->suspend_tx)) {
883 debug_scsi("conn %d Tx suspended!\n", conn->id);
Mike Christie77a23c22007-05-30 12:57:18 -0500884 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -0500885 return -ENODATA;
Mike Christie7996a772006-04-06 21:13:41 -0500886 }
Mike Christie7996a772006-04-06 21:13:41 -0500887
888 if (conn->ctask) {
Mike Christie77a23c22007-05-30 12:57:18 -0500889 rc = iscsi_xmit_ctask(conn);
Mike Christie3219e522006-05-30 00:37:28 -0500890 if (rc)
Mike Christie7996a772006-04-06 21:13:41 -0500891 goto again;
Mike Christie7996a772006-04-06 21:13:41 -0500892 }
Mike Christie77a23c22007-05-30 12:57:18 -0500893
Mike Christie7996a772006-04-06 21:13:41 -0500894 if (conn->mtask) {
Mike Christie05db8882007-02-28 17:32:16 -0600895 rc = iscsi_xmit_mtask(conn);
Mike Christie3219e522006-05-30 00:37:28 -0500896 if (rc)
Mike Christie7996a772006-04-06 21:13:41 -0500897 goto again;
Mike Christie7996a772006-04-06 21:13:41 -0500898 }
899
Mike Christie77a23c22007-05-30 12:57:18 -0500900 /*
901 * process mgmt pdus like nops before commands since we should
902 * only have one nop-out as a ping from us and targets should not
903 * overflow us with nop-ins
904 */
905check_mgmt:
Mike Christie843c0a82007-12-13 12:43:20 -0600906 while (!list_empty(&conn->mgmtqueue)) {
907 conn->mtask = list_entry(conn->mgmtqueue.next,
908 struct iscsi_mgmt_task, running);
Mike Christieb3a7ea82007-12-13 12:43:26 -0600909 if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
910 iscsi_free_mgmt_task(conn, conn->mtask);
911 conn->mtask = NULL;
912 continue;
913 }
914
Mike Christie77a23c22007-05-30 12:57:18 -0500915 iscsi_prep_mtask(conn, conn->mtask);
Mike Christie843c0a82007-12-13 12:43:20 -0600916 list_move_tail(conn->mgmtqueue.next, &conn->mgmt_run_list);
Mike Christie77a23c22007-05-30 12:57:18 -0500917 rc = iscsi_xmit_mtask(conn);
918 if (rc)
919 goto again;
Mike Christie7996a772006-04-06 21:13:41 -0500920 }
921
Mike Christie843c0a82007-12-13 12:43:20 -0600922 /* process pending command queue */
Mike Christieb6c395e2006-07-24 15:47:15 -0500923 while (!list_empty(&conn->xmitqueue)) {
Mike Christie843c0a82007-12-13 12:43:20 -0600924 if (conn->tmf_state == TMF_QUEUED)
925 break;
926
Mike Christieb6c395e2006-07-24 15:47:15 -0500927 conn->ctask = list_entry(conn->xmitqueue.next,
928 struct iscsi_cmd_task, running);
Mike Christieb3a7ea82007-12-13 12:43:26 -0600929 if (conn->session->state == ISCSI_STATE_LOGGING_OUT) {
Mike Christie9000bcd2007-12-13 12:43:32 -0600930 fail_command(conn, conn->ctask, DID_IMM_RETRY << 16);
Mike Christieb3a7ea82007-12-13 12:43:26 -0600931 continue;
932 }
Boaz Harrosh004d6532007-12-13 12:43:23 -0600933 if (iscsi_prep_scsi_cmd_pdu(conn->ctask)) {
934 fail_command(conn, conn->ctask, DID_ABORT << 16);
935 continue;
936 }
Olaf Kircha8ac6312007-12-13 12:43:35 -0600937
Mike Christie843c0a82007-12-13 12:43:20 -0600938 conn->ctask->state = ISCSI_TASK_RUNNING;
Mike Christieb6c395e2006-07-24 15:47:15 -0500939 list_move_tail(conn->xmitqueue.next, &conn->run_list);
Mike Christie77a23c22007-05-30 12:57:18 -0500940 rc = iscsi_xmit_ctask(conn);
941 if (rc)
Mike Christie60ecebf2006-08-31 18:09:25 -0400942 goto again;
Mike Christie77a23c22007-05-30 12:57:18 -0500943 /*
944 * we could continuously get new ctask requests so
945 * we need to check the mgmt queue for nops that need to
946 * be sent to aviod starvation
947 */
Mike Christie843c0a82007-12-13 12:43:20 -0600948 if (!list_empty(&conn->mgmtqueue))
949 goto check_mgmt;
950 }
951
952 while (!list_empty(&conn->requeue)) {
953 if (conn->session->fast_abort && conn->tmf_state != TMF_INITIAL)
954 break;
955
Mike Christieb3a7ea82007-12-13 12:43:26 -0600956 /*
957 * we always do fastlogout - conn stop code will clean up.
958 */
959 if (conn->session->state == ISCSI_STATE_LOGGING_OUT)
960 break;
961
Mike Christie843c0a82007-12-13 12:43:20 -0600962 conn->ctask = list_entry(conn->requeue.next,
963 struct iscsi_cmd_task, running);
964 conn->ctask->state = ISCSI_TASK_RUNNING;
965 list_move_tail(conn->requeue.next, &conn->run_list);
966 rc = iscsi_xmit_ctask(conn);
967 if (rc)
968 goto again;
969 if (!list_empty(&conn->mgmtqueue))
Mike Christie77a23c22007-05-30 12:57:18 -0500970 goto check_mgmt;
Mike Christie7996a772006-04-06 21:13:41 -0500971 }
Mike Christieb6c395e2006-07-24 15:47:15 -0500972 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -0500973 return -ENODATA;
Mike Christie7996a772006-04-06 21:13:41 -0500974
975again:
976 if (unlikely(conn->suspend_tx))
Mike Christie77a23c22007-05-30 12:57:18 -0500977 rc = -ENODATA;
978 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -0500979 return rc;
Mike Christie7996a772006-04-06 21:13:41 -0500980}
981
David Howellsc4028952006-11-22 14:57:56 +0000982static void iscsi_xmitworker(struct work_struct *work)
Mike Christie7996a772006-04-06 21:13:41 -0500983{
David Howellsc4028952006-11-22 14:57:56 +0000984 struct iscsi_conn *conn =
985 container_of(work, struct iscsi_conn, xmitwork);
Mike Christie3219e522006-05-30 00:37:28 -0500986 int rc;
Mike Christie7996a772006-04-06 21:13:41 -0500987 /*
988 * serialize Xmit worker on a per-connection basis.
989 */
Mike Christie3219e522006-05-30 00:37:28 -0500990 do {
991 rc = iscsi_data_xmit(conn);
992 } while (rc >= 0 || rc == -EAGAIN);
Mike Christie7996a772006-04-06 21:13:41 -0500993}
994
995enum {
996 FAILURE_BAD_HOST = 1,
997 FAILURE_SESSION_FAILED,
998 FAILURE_SESSION_FREED,
999 FAILURE_WINDOW_CLOSED,
Mike Christie60ecebf2006-08-31 18:09:25 -04001000 FAILURE_OOM,
Mike Christie7996a772006-04-06 21:13:41 -05001001 FAILURE_SESSION_TERMINATE,
Mike Christie656cffc2006-05-18 20:31:42 -05001002 FAILURE_SESSION_IN_RECOVERY,
Mike Christie7996a772006-04-06 21:13:41 -05001003 FAILURE_SESSION_RECOVERY_TIMEOUT,
Mike Christieb3a7ea82007-12-13 12:43:26 -06001004 FAILURE_SESSION_LOGGING_OUT,
Mike Christie6eabafb2008-01-31 13:36:43 -06001005 FAILURE_SESSION_NOT_READY,
Mike Christie7996a772006-04-06 21:13:41 -05001006};
1007
1008int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
1009{
1010 struct Scsi_Host *host;
1011 int reason = 0;
1012 struct iscsi_session *session;
1013 struct iscsi_conn *conn;
1014 struct iscsi_cmd_task *ctask = NULL;
1015
1016 sc->scsi_done = done;
1017 sc->result = 0;
Mike Christief47f2cf2006-08-31 18:09:33 -04001018 sc->SCp.ptr = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05001019
1020 host = sc->device->host;
Mike Christie1040c992007-12-13 12:43:34 -06001021 spin_unlock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001022
Mike Christie1040c992007-12-13 12:43:34 -06001023 session = iscsi_hostdata(host->hostdata);
Mike Christie7996a772006-04-06 21:13:41 -05001024 spin_lock(&session->lock);
1025
Mike Christie6eabafb2008-01-31 13:36:43 -06001026 reason = iscsi_session_chkready(session_to_cls(session));
1027 if (reason) {
1028 sc->result = reason;
1029 goto fault;
1030 }
1031
Mike Christie656cffc2006-05-18 20:31:42 -05001032 /*
1033 * ISCSI_STATE_FAILED is a temp. state. The recovery
1034 * code will decide what is best to do with command queued
1035 * during this time
1036 */
1037 if (session->state != ISCSI_STATE_LOGGED_IN &&
1038 session->state != ISCSI_STATE_FAILED) {
1039 /*
1040 * to handle the race between when we set the recovery state
1041 * and block the session we requeue here (commands could
1042 * be entering our queuecommand while a block is starting
1043 * up because the block code is not locked)
1044 */
Mike Christie9000bcd2007-12-13 12:43:32 -06001045 switch (session->state) {
1046 case ISCSI_STATE_IN_RECOVERY:
Mike Christie656cffc2006-05-18 20:31:42 -05001047 reason = FAILURE_SESSION_IN_RECOVERY;
Mike Christie6eabafb2008-01-31 13:36:43 -06001048 sc->result = DID_IMM_RETRY << 16;
1049 break;
Mike Christie9000bcd2007-12-13 12:43:32 -06001050 case ISCSI_STATE_LOGGING_OUT:
1051 reason = FAILURE_SESSION_LOGGING_OUT;
Mike Christie6eabafb2008-01-31 13:36:43 -06001052 sc->result = DID_IMM_RETRY << 16;
1053 break;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001054 case ISCSI_STATE_RECOVERY_FAILED:
Mike Christie656cffc2006-05-18 20:31:42 -05001055 reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
Mike Christie6eabafb2008-01-31 13:36:43 -06001056 sc->result = DID_NO_CONNECT << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001057 break;
1058 case ISCSI_STATE_TERMINATE:
Mike Christie656cffc2006-05-18 20:31:42 -05001059 reason = FAILURE_SESSION_TERMINATE;
Mike Christie6eabafb2008-01-31 13:36:43 -06001060 sc->result = DID_NO_CONNECT << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001061 break;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001062 default:
Mike Christie656cffc2006-05-18 20:31:42 -05001063 reason = FAILURE_SESSION_FREED;
Mike Christie6eabafb2008-01-31 13:36:43 -06001064 sc->result = DID_NO_CONNECT << 16;
Mike Christieb3a7ea82007-12-13 12:43:26 -06001065 }
Mike Christie7996a772006-04-06 21:13:41 -05001066 goto fault;
1067 }
1068
Mike Christie7996a772006-04-06 21:13:41 -05001069 conn = session->leadconn;
Mike Christie98644042006-10-16 18:09:39 -04001070 if (!conn) {
1071 reason = FAILURE_SESSION_FREED;
Mike Christie6eabafb2008-01-31 13:36:43 -06001072 sc->result = DID_NO_CONNECT << 16;
Mike Christie98644042006-10-16 18:09:39 -04001073 goto fault;
1074 }
Mike Christie7996a772006-04-06 21:13:41 -05001075
Mike Christie77a23c22007-05-30 12:57:18 -05001076 if (iscsi_check_cmdsn_window_closed(conn)) {
1077 reason = FAILURE_WINDOW_CLOSED;
1078 goto reject;
1079 }
1080
Mike Christie60ecebf2006-08-31 18:09:25 -04001081 if (!__kfifo_get(session->cmdpool.queue, (void*)&ctask,
1082 sizeof(void*))) {
1083 reason = FAILURE_OOM;
1084 goto reject;
1085 }
Mike Christiee0726402007-07-26 12:46:48 -05001086 session->queued_cmdsn++;
1087
Mike Christie7996a772006-04-06 21:13:41 -05001088 sc->SCp.phase = session->age;
1089 sc->SCp.ptr = (char *)ctask;
1090
Mike Christie60ecebf2006-08-31 18:09:25 -04001091 atomic_set(&ctask->refcount, 1);
Mike Christieb6c395e2006-07-24 15:47:15 -05001092 ctask->state = ISCSI_TASK_PENDING;
Mike Christie7996a772006-04-06 21:13:41 -05001093 ctask->conn = conn;
1094 ctask->sc = sc;
1095 INIT_LIST_HEAD(&ctask->running);
Mike Christie7996a772006-04-06 21:13:41 -05001096
Mike Christieb6c395e2006-07-24 15:47:15 -05001097 list_add_tail(&ctask->running, &conn->xmitqueue);
Mike Christie7996a772006-04-06 21:13:41 -05001098 spin_unlock(&session->lock);
1099
1100 scsi_queue_work(host, &conn->xmitwork);
Mike Christie1040c992007-12-13 12:43:34 -06001101 spin_lock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001102 return 0;
1103
1104reject:
1105 spin_unlock(&session->lock);
1106 debug_scsi("cmd 0x%x rejected (%d)\n", sc->cmnd[0], reason);
Mike Christie1040c992007-12-13 12:43:34 -06001107 spin_lock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001108 return SCSI_MLQUEUE_HOST_BUSY;
1109
1110fault:
1111 spin_unlock(&session->lock);
Mike Christie6eabafb2008-01-31 13:36:43 -06001112 debug_scsi("iscsi: cmd 0x%x is not queued (%d)\n", sc->cmnd[0], reason);
FUJITA Tomonori1c138992007-06-14 22:13:17 +09001113 scsi_set_resid(sc, scsi_bufflen(sc));
Mike Christie7996a772006-04-06 21:13:41 -05001114 sc->scsi_done(sc);
Mike Christie1040c992007-12-13 12:43:34 -06001115 spin_lock(host->host_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001116 return 0;
1117}
1118EXPORT_SYMBOL_GPL(iscsi_queuecommand);
1119
1120int iscsi_change_queue_depth(struct scsi_device *sdev, int depth)
1121{
1122 if (depth > ISCSI_MAX_CMD_PER_LUN)
1123 depth = ISCSI_MAX_CMD_PER_LUN;
1124 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
1125 return sdev->queue_depth;
1126}
1127EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
1128
Mike Christie7996a772006-04-06 21:13:41 -05001129void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
1130{
1131 struct iscsi_session *session = class_to_transport_session(cls_session);
Mike Christie7996a772006-04-06 21:13:41 -05001132
1133 spin_lock_bh(&session->lock);
1134 if (session->state != ISCSI_STATE_LOGGED_IN) {
Mike Christie656cffc2006-05-18 20:31:42 -05001135 session->state = ISCSI_STATE_RECOVERY_FAILED;
Mike Christie843c0a82007-12-13 12:43:20 -06001136 if (session->leadconn)
1137 wake_up(&session->leadconn->ehwait);
Mike Christie7996a772006-04-06 21:13:41 -05001138 }
1139 spin_unlock_bh(&session->lock);
1140}
1141EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
1142
1143int iscsi_eh_host_reset(struct scsi_cmnd *sc)
1144{
1145 struct Scsi_Host *host = sc->device->host;
1146 struct iscsi_session *session = iscsi_hostdata(host->hostdata);
1147 struct iscsi_conn *conn = session->leadconn;
Mike Christie7996a772006-04-06 21:13:41 -05001148
Mike Christiebc436b22007-12-13 12:43:28 -06001149 mutex_lock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001150 spin_lock_bh(&session->lock);
1151 if (session->state == ISCSI_STATE_TERMINATE) {
1152failed:
1153 debug_scsi("failing host reset: session terminated "
Pete Wyckoffd6e24d12006-11-08 15:58:32 -06001154 "[CID %d age %d]\n", conn->id, session->age);
Mike Christie7996a772006-04-06 21:13:41 -05001155 spin_unlock_bh(&session->lock);
Mike Christiebc436b22007-12-13 12:43:28 -06001156 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001157 return FAILED;
1158 }
1159
Mike Christie7996a772006-04-06 21:13:41 -05001160 spin_unlock_bh(&session->lock);
Mike Christiebc436b22007-12-13 12:43:28 -06001161 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001162 /*
1163 * we drop the lock here but the leadconn cannot be destoyed while
1164 * we are in the scsi eh
1165 */
Mike Christie843c0a82007-12-13 12:43:20 -06001166 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie7996a772006-04-06 21:13:41 -05001167
1168 debug_scsi("iscsi_eh_host_reset wait for relogin\n");
1169 wait_event_interruptible(conn->ehwait,
1170 session->state == ISCSI_STATE_TERMINATE ||
1171 session->state == ISCSI_STATE_LOGGED_IN ||
Mike Christie656cffc2006-05-18 20:31:42 -05001172 session->state == ISCSI_STATE_RECOVERY_FAILED);
Mike Christie7996a772006-04-06 21:13:41 -05001173 if (signal_pending(current))
1174 flush_signals(current);
1175
Mike Christiebc436b22007-12-13 12:43:28 -06001176 mutex_lock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001177 spin_lock_bh(&session->lock);
1178 if (session->state == ISCSI_STATE_LOGGED_IN)
Mike Christie322d7392008-01-31 13:36:52 -06001179 iscsi_session_printk(KERN_INFO, session,
1180 "host reset succeeded\n");
Mike Christie7996a772006-04-06 21:13:41 -05001181 else
1182 goto failed;
1183 spin_unlock_bh(&session->lock);
Mike Christiebc436b22007-12-13 12:43:28 -06001184 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001185 return SUCCESS;
1186}
1187EXPORT_SYMBOL_GPL(iscsi_eh_host_reset);
1188
Mike Christie843c0a82007-12-13 12:43:20 -06001189static void iscsi_tmf_timedout(unsigned long data)
Mike Christie7996a772006-04-06 21:13:41 -05001190{
Mike Christie843c0a82007-12-13 12:43:20 -06001191 struct iscsi_conn *conn = (struct iscsi_conn *)data;
Mike Christie7996a772006-04-06 21:13:41 -05001192 struct iscsi_session *session = conn->session;
1193
1194 spin_lock(&session->lock);
Mike Christie843c0a82007-12-13 12:43:20 -06001195 if (conn->tmf_state == TMF_QUEUED) {
1196 conn->tmf_state = TMF_TIMEDOUT;
1197 debug_scsi("tmf timedout\n");
Mike Christie7996a772006-04-06 21:13:41 -05001198 /* unblock eh_abort() */
1199 wake_up(&conn->ehwait);
1200 }
1201 spin_unlock(&session->lock);
1202}
1203
Mike Christie843c0a82007-12-13 12:43:20 -06001204static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn,
Mike Christief6d51802007-12-13 12:43:30 -06001205 struct iscsi_tm *hdr, int age,
1206 int timeout)
Mike Christie7996a772006-04-06 21:13:41 -05001207{
Mike Christie7996a772006-04-06 21:13:41 -05001208 struct iscsi_session *session = conn->session;
Mike Christie843c0a82007-12-13 12:43:20 -06001209 struct iscsi_mgmt_task *mtask;
Mike Christie7996a772006-04-06 21:13:41 -05001210
Mike Christie843c0a82007-12-13 12:43:20 -06001211 mtask = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
1212 NULL, 0);
1213 if (!mtask) {
Mike Christie6724add2007-08-15 01:38:30 -05001214 spin_unlock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001215 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie843c0a82007-12-13 12:43:20 -06001216 spin_lock_bh(&session->lock);
1217 debug_scsi("tmf exec failure\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001218 return -EPERM;
Mike Christie7996a772006-04-06 21:13:41 -05001219 }
Mike Christie843c0a82007-12-13 12:43:20 -06001220 conn->tmfcmd_pdus_cnt++;
Mike Christief6d51802007-12-13 12:43:30 -06001221 conn->tmf_timer.expires = timeout * HZ + jiffies;
Mike Christie843c0a82007-12-13 12:43:20 -06001222 conn->tmf_timer.function = iscsi_tmf_timedout;
1223 conn->tmf_timer.data = (unsigned long)conn;
1224 add_timer(&conn->tmf_timer);
1225 debug_scsi("tmf set timeout\n");
Mike Christie7996a772006-04-06 21:13:41 -05001226
Mike Christie7996a772006-04-06 21:13:41 -05001227 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05001228 mutex_unlock(&session->eh_mutex);
Mike Christie77a23c22007-05-30 12:57:18 -05001229 scsi_queue_work(session->host, &conn->xmitwork);
Mike Christie7996a772006-04-06 21:13:41 -05001230
1231 /*
1232 * block eh thread until:
1233 *
Mike Christie843c0a82007-12-13 12:43:20 -06001234 * 1) tmf response
1235 * 2) tmf timeout
Mike Christie7996a772006-04-06 21:13:41 -05001236 * 3) session is terminated or restarted or userspace has
1237 * given up on recovery
1238 */
Mike Christie843c0a82007-12-13 12:43:20 -06001239 wait_event_interruptible(conn->ehwait, age != session->age ||
Mike Christie7996a772006-04-06 21:13:41 -05001240 session->state != ISCSI_STATE_LOGGED_IN ||
Mike Christie843c0a82007-12-13 12:43:20 -06001241 conn->tmf_state != TMF_QUEUED);
Mike Christie7996a772006-04-06 21:13:41 -05001242 if (signal_pending(current))
1243 flush_signals(current);
Mike Christie843c0a82007-12-13 12:43:20 -06001244 del_timer_sync(&conn->tmf_timer);
1245
Mike Christie6724add2007-08-15 01:38:30 -05001246 mutex_lock(&session->eh_mutex);
Mike Christie77a23c22007-05-30 12:57:18 -05001247 spin_lock_bh(&session->lock);
Mike Christie843c0a82007-12-13 12:43:20 -06001248 /* if the session drops it will clean up the mtask */
1249 if (age != session->age ||
1250 session->state != ISCSI_STATE_LOGGED_IN)
1251 return -ENOTCONN;
Mike Christie7996a772006-04-06 21:13:41 -05001252 return 0;
1253}
1254
1255/*
Mike Christie843c0a82007-12-13 12:43:20 -06001256 * Fail commands. session lock held and recv side suspended and xmit
1257 * thread flushed
1258 */
Mike Christie6eabafb2008-01-31 13:36:43 -06001259static void fail_all_commands(struct iscsi_conn *conn, unsigned lun,
1260 int error)
Mike Christie843c0a82007-12-13 12:43:20 -06001261{
1262 struct iscsi_cmd_task *ctask, *tmp;
1263
1264 if (conn->ctask && (conn->ctask->sc->device->lun == lun || lun == -1))
1265 conn->ctask = NULL;
1266
1267 /* flush pending */
1268 list_for_each_entry_safe(ctask, tmp, &conn->xmitqueue, running) {
1269 if (lun == ctask->sc->device->lun || lun == -1) {
1270 debug_scsi("failing pending sc %p itt 0x%x\n",
1271 ctask->sc, ctask->itt);
Mike Christie6eabafb2008-01-31 13:36:43 -06001272 fail_command(conn, ctask, error << 16);
Mike Christie843c0a82007-12-13 12:43:20 -06001273 }
1274 }
1275
1276 list_for_each_entry_safe(ctask, tmp, &conn->requeue, running) {
1277 if (lun == ctask->sc->device->lun || lun == -1) {
1278 debug_scsi("failing requeued sc %p itt 0x%x\n",
1279 ctask->sc, ctask->itt);
Mike Christie6eabafb2008-01-31 13:36:43 -06001280 fail_command(conn, ctask, error << 16);
Mike Christie843c0a82007-12-13 12:43:20 -06001281 }
1282 }
1283
1284 /* fail all other running */
1285 list_for_each_entry_safe(ctask, tmp, &conn->run_list, running) {
1286 if (lun == ctask->sc->device->lun || lun == -1) {
1287 debug_scsi("failing in progress sc %p itt 0x%x\n",
1288 ctask->sc, ctask->itt);
1289 fail_command(conn, ctask, DID_BUS_BUSY << 16);
1290 }
1291 }
1292}
1293
Mike Christie6724add2007-08-15 01:38:30 -05001294static void iscsi_suspend_tx(struct iscsi_conn *conn)
1295{
1296 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1297 scsi_flush_work(conn->session->host);
1298}
1299
1300static void iscsi_start_tx(struct iscsi_conn *conn)
1301{
1302 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1303 scsi_queue_work(conn->session->host, &conn->xmitwork);
1304}
1305
Mike Christief6d51802007-12-13 12:43:30 -06001306static enum scsi_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *scmd)
1307{
1308 struct iscsi_cls_session *cls_session;
1309 struct iscsi_session *session;
1310 struct iscsi_conn *conn;
1311 enum scsi_eh_timer_return rc = EH_NOT_HANDLED;
1312
1313 cls_session = starget_to_session(scsi_target(scmd->device));
1314 session = class_to_transport_session(cls_session);
1315
1316 debug_scsi("scsi cmd %p timedout\n", scmd);
1317
1318 spin_lock(&session->lock);
1319 if (session->state != ISCSI_STATE_LOGGED_IN) {
1320 /*
1321 * We are probably in the middle of iscsi recovery so let
1322 * that complete and handle the error.
1323 */
1324 rc = EH_RESET_TIMER;
1325 goto done;
1326 }
1327
1328 conn = session->leadconn;
1329 if (!conn) {
1330 /* In the middle of shuting down */
1331 rc = EH_RESET_TIMER;
1332 goto done;
1333 }
1334
1335 if (!conn->recv_timeout && !conn->ping_timeout)
1336 goto done;
1337 /*
1338 * if the ping timedout then we are in the middle of cleaning up
1339 * and can let the iscsi eh handle it
1340 */
1341 if (time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) +
1342 (conn->ping_timeout * HZ), jiffies))
1343 rc = EH_RESET_TIMER;
1344 /*
1345 * if we are about to check the transport then give the command
1346 * more time
1347 */
1348 if (time_before_eq(conn->last_recv + (conn->recv_timeout * HZ),
1349 jiffies))
1350 rc = EH_RESET_TIMER;
1351 /* if in the middle of checking the transport then give us more time */
1352 if (conn->ping_mtask)
1353 rc = EH_RESET_TIMER;
1354done:
1355 spin_unlock(&session->lock);
1356 debug_scsi("return %s\n", rc == EH_RESET_TIMER ? "timer reset" : "nh");
1357 return rc;
1358}
1359
1360static void iscsi_check_transport_timeouts(unsigned long data)
1361{
1362 struct iscsi_conn *conn = (struct iscsi_conn *)data;
1363 struct iscsi_session *session = conn->session;
1364 unsigned long timeout, next_timeout = 0, last_recv;
1365
1366 spin_lock(&session->lock);
1367 if (session->state != ISCSI_STATE_LOGGED_IN)
1368 goto done;
1369
1370 timeout = conn->recv_timeout;
1371 if (!timeout)
1372 goto done;
1373
1374 timeout *= HZ;
1375 last_recv = conn->last_recv;
1376 if (time_before_eq(last_recv + timeout + (conn->ping_timeout * HZ),
1377 jiffies)) {
Mike Christie322d7392008-01-31 13:36:52 -06001378 iscsi_conn_printk(KERN_ERR, conn, "ping timeout of %d secs "
1379 "expired, last rx %lu, last ping %lu, "
1380 "now %lu\n", conn->ping_timeout, last_recv,
1381 conn->last_ping, jiffies);
Mike Christief6d51802007-12-13 12:43:30 -06001382 spin_unlock(&session->lock);
1383 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1384 return;
1385 }
1386
1387 if (time_before_eq(last_recv + timeout, jiffies)) {
1388 if (time_before_eq(conn->last_ping, last_recv)) {
1389 /* send a ping to try to provoke some traffic */
1390 debug_scsi("Sending nopout as ping on conn %p\n", conn);
1391 iscsi_send_nopout(conn, NULL);
1392 }
1393 next_timeout = last_recv + timeout + (conn->ping_timeout * HZ);
Mike Christiead294e92008-01-31 13:36:50 -06001394 } else
Mike Christief6d51802007-12-13 12:43:30 -06001395 next_timeout = last_recv + timeout;
Mike Christief6d51802007-12-13 12:43:30 -06001396
Mike Christiead294e92008-01-31 13:36:50 -06001397 debug_scsi("Setting next tmo %lu\n", next_timeout);
1398 mod_timer(&conn->transport_timer, next_timeout);
Mike Christief6d51802007-12-13 12:43:30 -06001399done:
1400 spin_unlock(&session->lock);
1401}
1402
Mike Christie843c0a82007-12-13 12:43:20 -06001403static void iscsi_prep_abort_task_pdu(struct iscsi_cmd_task *ctask,
1404 struct iscsi_tm *hdr)
1405{
1406 memset(hdr, 0, sizeof(*hdr));
1407 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
1408 hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK;
1409 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
1410 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
1411 hdr->rtt = ctask->hdr->itt;
1412 hdr->refcmdsn = ctask->hdr->cmdsn;
1413}
1414
Mike Christie7996a772006-04-06 21:13:41 -05001415int iscsi_eh_abort(struct scsi_cmnd *sc)
1416{
Mike Christie6724add2007-08-15 01:38:30 -05001417 struct Scsi_Host *host = sc->device->host;
1418 struct iscsi_session *session = iscsi_hostdata(host->hostdata);
Mike Christief47f2cf2006-08-31 18:09:33 -04001419 struct iscsi_conn *conn;
Mike Christie843c0a82007-12-13 12:43:20 -06001420 struct iscsi_cmd_task *ctask;
1421 struct iscsi_tm *hdr;
1422 int rc, age;
Mike Christie7996a772006-04-06 21:13:41 -05001423
Mike Christie6724add2007-08-15 01:38:30 -05001424 mutex_lock(&session->eh_mutex);
1425 spin_lock_bh(&session->lock);
Mike Christief47f2cf2006-08-31 18:09:33 -04001426 /*
1427 * if session was ISCSI_STATE_IN_RECOVERY then we may not have
1428 * got the command.
1429 */
1430 if (!sc->SCp.ptr) {
1431 debug_scsi("sc never reached iscsi layer or it completed.\n");
Mike Christie6724add2007-08-15 01:38:30 -05001432 spin_unlock_bh(&session->lock);
1433 mutex_unlock(&session->eh_mutex);
Mike Christief47f2cf2006-08-31 18:09:33 -04001434 return SUCCESS;
1435 }
1436
Mike Christie7996a772006-04-06 21:13:41 -05001437 /*
1438 * If we are not logged in or we have started a new session
1439 * then let the host reset code handle this
1440 */
Mike Christie843c0a82007-12-13 12:43:20 -06001441 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN ||
1442 sc->SCp.phase != session->age) {
1443 spin_unlock_bh(&session->lock);
1444 mutex_unlock(&session->eh_mutex);
1445 return FAILED;
1446 }
1447
1448 conn = session->leadconn;
1449 conn->eh_abort_cnt++;
1450 age = session->age;
1451
1452 ctask = (struct iscsi_cmd_task *)sc->SCp.ptr;
1453 debug_scsi("aborting [sc %p itt 0x%x]\n", sc, ctask->itt);
Mike Christie7996a772006-04-06 21:13:41 -05001454
1455 /* ctask completed before time out */
Mike Christie7ea8b822006-07-24 15:47:22 -05001456 if (!ctask->sc) {
Mike Christie7ea8b822006-07-24 15:47:22 -05001457 debug_scsi("sc completed while abort in progress\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001458 goto success;
Mike Christie7ea8b822006-07-24 15:47:22 -05001459 }
Mike Christie7996a772006-04-06 21:13:41 -05001460
Mike Christie77a23c22007-05-30 12:57:18 -05001461 if (ctask->state == ISCSI_TASK_PENDING) {
1462 fail_command(conn, ctask, DID_ABORT << 16);
1463 goto success;
1464 }
Mike Christie7996a772006-04-06 21:13:41 -05001465
Mike Christie843c0a82007-12-13 12:43:20 -06001466 /* only have one tmf outstanding at a time */
1467 if (conn->tmf_state != TMF_INITIAL)
Mike Christie7996a772006-04-06 21:13:41 -05001468 goto failed;
Mike Christie843c0a82007-12-13 12:43:20 -06001469 conn->tmf_state = TMF_QUEUED;
Mike Christie7996a772006-04-06 21:13:41 -05001470
Mike Christie843c0a82007-12-13 12:43:20 -06001471 hdr = &conn->tmhdr;
1472 iscsi_prep_abort_task_pdu(ctask, hdr);
1473
Mike Christief6d51802007-12-13 12:43:30 -06001474 if (iscsi_exec_task_mgmt_fn(conn, hdr, age, session->abort_timeout)) {
Mike Christie843c0a82007-12-13 12:43:20 -06001475 rc = FAILED;
1476 goto failed;
1477 }
1478
1479 switch (conn->tmf_state) {
1480 case TMF_SUCCESS:
Mike Christie77a23c22007-05-30 12:57:18 -05001481 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05001482 iscsi_suspend_tx(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001483 /*
1484 * clean up task if aborted. grab the recv lock as a writer
1485 */
1486 write_lock_bh(conn->recv_lock);
1487 spin_lock(&session->lock);
1488 fail_command(conn, ctask, DID_ABORT << 16);
Mike Christie843c0a82007-12-13 12:43:20 -06001489 conn->tmf_state = TMF_INITIAL;
Mike Christie77a23c22007-05-30 12:57:18 -05001490 spin_unlock(&session->lock);
1491 write_unlock_bh(conn->recv_lock);
Mike Christie6724add2007-08-15 01:38:30 -05001492 iscsi_start_tx(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001493 goto success_unlocked;
Mike Christie843c0a82007-12-13 12:43:20 -06001494 case TMF_TIMEDOUT:
1495 spin_unlock_bh(&session->lock);
1496 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1497 goto failed_unlocked;
1498 case TMF_NOT_FOUND:
1499 if (!sc->SCp.ptr) {
1500 conn->tmf_state = TMF_INITIAL;
Mike Christie7ea8b822006-07-24 15:47:22 -05001501 /* ctask completed before tmf abort response */
Mike Christie7ea8b822006-07-24 15:47:22 -05001502 debug_scsi("sc completed while abort in progress\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001503 goto success;
Mike Christie7ea8b822006-07-24 15:47:22 -05001504 }
1505 /* fall through */
1506 default:
Mike Christie843c0a82007-12-13 12:43:20 -06001507 conn->tmf_state = TMF_INITIAL;
1508 goto failed;
Mike Christie7996a772006-04-06 21:13:41 -05001509 }
1510
Mike Christie77a23c22007-05-30 12:57:18 -05001511success:
Mike Christie7996a772006-04-06 21:13:41 -05001512 spin_unlock_bh(&session->lock);
Mike Christie77a23c22007-05-30 12:57:18 -05001513success_unlocked:
1514 debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
Mike Christie6724add2007-08-15 01:38:30 -05001515 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001516 return SUCCESS;
1517
1518failed:
1519 spin_unlock_bh(&session->lock);
Mike Christie77a23c22007-05-30 12:57:18 -05001520failed_unlocked:
Mike Christie843c0a82007-12-13 12:43:20 -06001521 debug_scsi("abort failed [sc %p itt 0x%x]\n", sc,
1522 ctask ? ctask->itt : 0);
Mike Christie6724add2007-08-15 01:38:30 -05001523 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001524 return FAILED;
1525}
1526EXPORT_SYMBOL_GPL(iscsi_eh_abort);
1527
Mike Christie843c0a82007-12-13 12:43:20 -06001528static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
1529{
1530 memset(hdr, 0, sizeof(*hdr));
1531 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
1532 hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK;
1533 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
1534 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
Mike Christief6d51802007-12-13 12:43:30 -06001535 hdr->rtt = RESERVED_ITT;
Mike Christie843c0a82007-12-13 12:43:20 -06001536}
1537
1538int iscsi_eh_device_reset(struct scsi_cmnd *sc)
1539{
1540 struct Scsi_Host *host = sc->device->host;
1541 struct iscsi_session *session = iscsi_hostdata(host->hostdata);
1542 struct iscsi_conn *conn;
1543 struct iscsi_tm *hdr;
1544 int rc = FAILED;
1545
1546 debug_scsi("LU Reset [sc %p lun %u]\n", sc, sc->device->lun);
1547
1548 mutex_lock(&session->eh_mutex);
1549 spin_lock_bh(&session->lock);
1550 /*
1551 * Just check if we are not logged in. We cannot check for
1552 * the phase because the reset could come from a ioctl.
1553 */
1554 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
1555 goto unlock;
1556 conn = session->leadconn;
1557
1558 /* only have one tmf outstanding at a time */
1559 if (conn->tmf_state != TMF_INITIAL)
1560 goto unlock;
1561 conn->tmf_state = TMF_QUEUED;
1562
1563 hdr = &conn->tmhdr;
1564 iscsi_prep_lun_reset_pdu(sc, hdr);
1565
Mike Christief6d51802007-12-13 12:43:30 -06001566 if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age,
1567 session->lu_reset_timeout)) {
Mike Christie843c0a82007-12-13 12:43:20 -06001568 rc = FAILED;
1569 goto unlock;
1570 }
1571
1572 switch (conn->tmf_state) {
1573 case TMF_SUCCESS:
1574 break;
1575 case TMF_TIMEDOUT:
1576 spin_unlock_bh(&session->lock);
1577 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1578 goto done;
1579 default:
1580 conn->tmf_state = TMF_INITIAL;
1581 goto unlock;
1582 }
1583
1584 rc = SUCCESS;
1585 spin_unlock_bh(&session->lock);
1586
1587 iscsi_suspend_tx(conn);
1588 /* need to grab the recv lock then session lock */
1589 write_lock_bh(conn->recv_lock);
1590 spin_lock(&session->lock);
Mike Christie6eabafb2008-01-31 13:36:43 -06001591 fail_all_commands(conn, sc->device->lun, DID_ERROR);
Mike Christie843c0a82007-12-13 12:43:20 -06001592 conn->tmf_state = TMF_INITIAL;
1593 spin_unlock(&session->lock);
1594 write_unlock_bh(conn->recv_lock);
1595
1596 iscsi_start_tx(conn);
1597 goto done;
1598
1599unlock:
1600 spin_unlock_bh(&session->lock);
1601done:
1602 debug_scsi("iscsi_eh_device_reset %s\n",
1603 rc == SUCCESS ? "SUCCESS" : "FAILED");
1604 mutex_unlock(&session->eh_mutex);
1605 return rc;
1606}
1607EXPORT_SYMBOL_GPL(iscsi_eh_device_reset);
1608
Olaf Kirch63203772007-12-13 12:43:25 -06001609/*
1610 * Pre-allocate a pool of @max items of @item_size. By default, the pool
1611 * should be accessed via kfifo_{get,put} on q->queue.
1612 * Optionally, the caller can obtain the array of object pointers
1613 * by passing in a non-NULL @items pointer
1614 */
Mike Christie7996a772006-04-06 21:13:41 -05001615int
Olaf Kirch63203772007-12-13 12:43:25 -06001616iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size)
Mike Christie7996a772006-04-06 21:13:41 -05001617{
Olaf Kirch63203772007-12-13 12:43:25 -06001618 int i, num_arrays = 1;
Mike Christie7996a772006-04-06 21:13:41 -05001619
Olaf Kirch63203772007-12-13 12:43:25 -06001620 memset(q, 0, sizeof(*q));
Mike Christie7996a772006-04-06 21:13:41 -05001621
1622 q->max = max;
Olaf Kirch63203772007-12-13 12:43:25 -06001623
1624 /* If the user passed an items pointer, he wants a copy of
1625 * the array. */
1626 if (items)
1627 num_arrays++;
1628 q->pool = kzalloc(num_arrays * max * sizeof(void*), GFP_KERNEL);
1629 if (q->pool == NULL)
1630 goto enomem;
Mike Christie7996a772006-04-06 21:13:41 -05001631
1632 q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
1633 GFP_KERNEL, NULL);
Olaf Kirch63203772007-12-13 12:43:25 -06001634 if (q->queue == ERR_PTR(-ENOMEM))
1635 goto enomem;
Mike Christie7996a772006-04-06 21:13:41 -05001636
1637 for (i = 0; i < max; i++) {
Olaf Kirch63203772007-12-13 12:43:25 -06001638 q->pool[i] = kzalloc(item_size, GFP_KERNEL);
Mike Christie7996a772006-04-06 21:13:41 -05001639 if (q->pool[i] == NULL) {
Olaf Kirch63203772007-12-13 12:43:25 -06001640 q->max = i;
1641 goto enomem;
Mike Christie7996a772006-04-06 21:13:41 -05001642 }
Mike Christie7996a772006-04-06 21:13:41 -05001643 __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
1644 }
Olaf Kirch63203772007-12-13 12:43:25 -06001645
1646 if (items) {
1647 *items = q->pool + max;
1648 memcpy(*items, q->pool, max * sizeof(void *));
1649 }
1650
Mike Christie7996a772006-04-06 21:13:41 -05001651 return 0;
Olaf Kirch63203772007-12-13 12:43:25 -06001652
1653enomem:
1654 iscsi_pool_free(q);
1655 return -ENOMEM;
Mike Christie7996a772006-04-06 21:13:41 -05001656}
1657EXPORT_SYMBOL_GPL(iscsi_pool_init);
1658
Olaf Kirch63203772007-12-13 12:43:25 -06001659void iscsi_pool_free(struct iscsi_pool *q)
Mike Christie7996a772006-04-06 21:13:41 -05001660{
1661 int i;
1662
1663 for (i = 0; i < q->max; i++)
Olaf Kirch63203772007-12-13 12:43:25 -06001664 kfree(q->pool[i]);
1665 if (q->pool)
1666 kfree(q->pool);
Mike Christie7996a772006-04-06 21:13:41 -05001667}
1668EXPORT_SYMBOL_GPL(iscsi_pool_free);
1669
1670/*
1671 * iSCSI Session's hostdata organization:
1672 *
1673 * *------------------* <== hostdata_session(host->hostdata)
1674 * | ptr to class sess|
1675 * |------------------| <== iscsi_hostdata(host->hostdata)
1676 * | iscsi_session |
1677 * *------------------*
1678 */
1679
1680#define hostdata_privsize(_sz) (sizeof(unsigned long) + _sz + \
1681 _sz % sizeof(unsigned long))
1682
1683#define hostdata_session(_hostdata) (iscsi_ptr(*(unsigned long *)_hostdata))
1684
1685/**
1686 * iscsi_session_setup - create iscsi cls session and host and session
1687 * @scsit: scsi transport template
1688 * @iscsit: iscsi transport template
Mike Christie15482712007-05-30 12:57:19 -05001689 * @cmds_max: scsi host can queue
1690 * @qdepth: scsi host cmds per lun
1691 * @cmd_task_size: LLD ctask private data size
1692 * @mgmt_task_size: LLD mtask private data size
Mike Christie7996a772006-04-06 21:13:41 -05001693 * @initial_cmdsn: initial CmdSN
1694 * @hostno: host no allocated
1695 *
1696 * This can be used by software iscsi_transports that allocate
1697 * a session per scsi host.
1698 **/
1699struct iscsi_cls_session *
1700iscsi_session_setup(struct iscsi_transport *iscsit,
1701 struct scsi_transport_template *scsit,
Mike Christie15482712007-05-30 12:57:19 -05001702 uint16_t cmds_max, uint16_t qdepth,
Mike Christie7996a772006-04-06 21:13:41 -05001703 int cmd_task_size, int mgmt_task_size,
1704 uint32_t initial_cmdsn, uint32_t *hostno)
1705{
1706 struct Scsi_Host *shost;
1707 struct iscsi_session *session;
1708 struct iscsi_cls_session *cls_session;
1709 int cmd_i;
1710
Mike Christie15482712007-05-30 12:57:19 -05001711 if (qdepth > ISCSI_MAX_CMD_PER_LUN || qdepth < 1) {
1712 if (qdepth != 0)
1713 printk(KERN_ERR "iscsi: invalid queue depth of %d. "
1714 "Queue depth must be between 1 and %d.\n",
1715 qdepth, ISCSI_MAX_CMD_PER_LUN);
1716 qdepth = ISCSI_DEF_CMD_PER_LUN;
1717 }
1718
vignesh babu11836572007-12-13 12:43:41 -06001719 if (!is_power_of_2(cmds_max) ||
Mike Christie15482712007-05-30 12:57:19 -05001720 cmds_max >= ISCSI_MGMT_ITT_OFFSET) {
1721 if (cmds_max != 0)
1722 printk(KERN_ERR "iscsi: invalid can_queue of %d. "
1723 "can_queue must be a power of 2 and between "
1724 "2 and %d - setting to %d.\n", cmds_max,
1725 ISCSI_MGMT_ITT_OFFSET, ISCSI_DEF_XMIT_CMDS_MAX);
1726 cmds_max = ISCSI_DEF_XMIT_CMDS_MAX;
1727 }
1728
Mike Christie7996a772006-04-06 21:13:41 -05001729 shost = scsi_host_alloc(iscsit->host_template,
1730 hostdata_privsize(sizeof(*session)));
1731 if (!shost)
1732 return NULL;
1733
Mike Christie15482712007-05-30 12:57:19 -05001734 /* the iscsi layer takes one task for reserve */
1735 shost->can_queue = cmds_max - 1;
1736 shost->cmd_per_lun = qdepth;
Mike Christie7996a772006-04-06 21:13:41 -05001737 shost->max_id = 1;
1738 shost->max_channel = 0;
1739 shost->max_lun = iscsit->max_lun;
1740 shost->max_cmd_len = iscsit->max_cmd_len;
1741 shost->transportt = scsit;
1742 shost->transportt->create_work_queue = 1;
Mike Christief6d51802007-12-13 12:43:30 -06001743 shost->transportt->eh_timed_out = iscsi_eh_cmd_timed_out;
Mike Christie7996a772006-04-06 21:13:41 -05001744 *hostno = shost->host_no;
1745
1746 session = iscsi_hostdata(shost->hostdata);
1747 memset(session, 0, sizeof(struct iscsi_session));
1748 session->host = shost;
1749 session->state = ISCSI_STATE_FREE;
Mike Christief6d51802007-12-13 12:43:30 -06001750 session->fast_abort = 1;
Mike Christie4cd49ea2007-12-13 12:43:38 -06001751 session->lu_reset_timeout = 15;
1752 session->abort_timeout = 10;
Mike Christie7996a772006-04-06 21:13:41 -05001753 session->mgmtpool_max = ISCSI_MGMT_CMDS_MAX;
Mike Christie15482712007-05-30 12:57:19 -05001754 session->cmds_max = cmds_max;
Mike Christiee0726402007-07-26 12:46:48 -05001755 session->queued_cmdsn = session->cmdsn = initial_cmdsn;
Mike Christie7996a772006-04-06 21:13:41 -05001756 session->exp_cmdsn = initial_cmdsn + 1;
1757 session->max_cmdsn = initial_cmdsn + 1;
1758 session->max_r2t = 1;
1759 session->tt = iscsit;
Mike Christie6724add2007-08-15 01:38:30 -05001760 mutex_init(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001761
1762 /* initialize SCSI PDU commands pool */
1763 if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
1764 (void***)&session->cmds,
1765 cmd_task_size + sizeof(struct iscsi_cmd_task)))
1766 goto cmdpool_alloc_fail;
1767
1768 /* pre-format cmds pool with ITT */
1769 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
1770 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
1771
1772 if (cmd_task_size)
1773 ctask->dd_data = &ctask[1];
1774 ctask->itt = cmd_i;
Mike Christieb6c395e2006-07-24 15:47:15 -05001775 INIT_LIST_HEAD(&ctask->running);
Mike Christie7996a772006-04-06 21:13:41 -05001776 }
1777
1778 spin_lock_init(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001779
1780 /* initialize immediate command pool */
1781 if (iscsi_pool_init(&session->mgmtpool, session->mgmtpool_max,
1782 (void***)&session->mgmt_cmds,
1783 mgmt_task_size + sizeof(struct iscsi_mgmt_task)))
1784 goto mgmtpool_alloc_fail;
1785
1786
1787 /* pre-format immediate cmds pool with ITT */
1788 for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
1789 struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
1790
1791 if (mgmt_task_size)
1792 mtask->dd_data = &mtask[1];
1793 mtask->itt = ISCSI_MGMT_ITT_OFFSET + cmd_i;
Mike Christieb6c395e2006-07-24 15:47:15 -05001794 INIT_LIST_HEAD(&mtask->running);
Mike Christie7996a772006-04-06 21:13:41 -05001795 }
1796
1797 if (scsi_add_host(shost, NULL))
1798 goto add_host_fail;
1799
Mike Christief53a88d2006-06-28 12:00:27 -05001800 if (!try_module_get(iscsit->owner))
1801 goto cls_session_fail;
1802
Mike Christie6a8a0d32006-06-28 12:00:31 -05001803 cls_session = iscsi_create_session(shost, iscsit, 0);
Mike Christie7996a772006-04-06 21:13:41 -05001804 if (!cls_session)
Mike Christief53a88d2006-06-28 12:00:27 -05001805 goto module_put;
Mike Christie7996a772006-04-06 21:13:41 -05001806 *(unsigned long*)shost->hostdata = (unsigned long)cls_session;
1807
1808 return cls_session;
1809
Mike Christief53a88d2006-06-28 12:00:27 -05001810module_put:
1811 module_put(iscsit->owner);
Mike Christie7996a772006-04-06 21:13:41 -05001812cls_session_fail:
1813 scsi_remove_host(shost);
1814add_host_fail:
Olaf Kirch63203772007-12-13 12:43:25 -06001815 iscsi_pool_free(&session->mgmtpool);
Mike Christie7996a772006-04-06 21:13:41 -05001816mgmtpool_alloc_fail:
Olaf Kirch63203772007-12-13 12:43:25 -06001817 iscsi_pool_free(&session->cmdpool);
Mike Christie7996a772006-04-06 21:13:41 -05001818cmdpool_alloc_fail:
1819 scsi_host_put(shost);
1820 return NULL;
1821}
1822EXPORT_SYMBOL_GPL(iscsi_session_setup);
1823
1824/**
1825 * iscsi_session_teardown - destroy session, host, and cls_session
1826 * shost: scsi host
1827 *
1828 * This can be used by software iscsi_transports that allocate
1829 * a session per scsi host.
1830 **/
1831void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
1832{
1833 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
1834 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
Mike Christie63f75cc2006-07-24 15:47:29 -05001835 struct module *owner = cls_session->transport->owner;
Mike Christie7996a772006-04-06 21:13:41 -05001836
Mike Christie26974782007-12-13 12:43:29 -06001837 iscsi_remove_session(cls_session);
Mike Christie7996a772006-04-06 21:13:41 -05001838 scsi_remove_host(shost);
1839
Olaf Kirch63203772007-12-13 12:43:25 -06001840 iscsi_pool_free(&session->mgmtpool);
1841 iscsi_pool_free(&session->cmdpool);
Mike Christie7996a772006-04-06 21:13:41 -05001842
Mike Christieb2c64162007-05-30 12:57:16 -05001843 kfree(session->password);
1844 kfree(session->password_in);
1845 kfree(session->username);
1846 kfree(session->username_in);
Mike Christief3ff0c32006-07-24 15:47:50 -05001847 kfree(session->targetname);
Mike Christied8196ed2007-05-30 12:57:25 -05001848 kfree(session->netdev);
Mike Christie0801c242007-05-30 12:57:12 -05001849 kfree(session->hwaddress);
Mike Christie8ad57812007-05-30 12:57:13 -05001850 kfree(session->initiatorname);
Mike Christief3ff0c32006-07-24 15:47:50 -05001851
Mike Christie26974782007-12-13 12:43:29 -06001852 iscsi_free_session(cls_session);
Mike Christie7996a772006-04-06 21:13:41 -05001853 scsi_host_put(shost);
Mike Christie63f75cc2006-07-24 15:47:29 -05001854 module_put(owner);
Mike Christie7996a772006-04-06 21:13:41 -05001855}
1856EXPORT_SYMBOL_GPL(iscsi_session_teardown);
1857
1858/**
1859 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
1860 * @cls_session: iscsi_cls_session
1861 * @conn_idx: cid
1862 **/
1863struct iscsi_cls_conn *
1864iscsi_conn_setup(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
1865{
1866 struct iscsi_session *session = class_to_transport_session(cls_session);
1867 struct iscsi_conn *conn;
1868 struct iscsi_cls_conn *cls_conn;
Mike Christied36ab6f2006-05-18 20:31:34 -05001869 char *data;
Mike Christie7996a772006-04-06 21:13:41 -05001870
1871 cls_conn = iscsi_create_conn(cls_session, conn_idx);
1872 if (!cls_conn)
1873 return NULL;
1874 conn = cls_conn->dd_data;
1875 memset(conn, 0, sizeof(*conn));
1876
1877 conn->session = session;
1878 conn->cls_conn = cls_conn;
1879 conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
1880 conn->id = conn_idx;
1881 conn->exp_statsn = 0;
Mike Christie843c0a82007-12-13 12:43:20 -06001882 conn->tmf_state = TMF_INITIAL;
Mike Christief6d51802007-12-13 12:43:30 -06001883
1884 init_timer(&conn->transport_timer);
1885 conn->transport_timer.data = (unsigned long)conn;
1886 conn->transport_timer.function = iscsi_check_transport_timeouts;
1887
Mike Christie7996a772006-04-06 21:13:41 -05001888 INIT_LIST_HEAD(&conn->run_list);
1889 INIT_LIST_HEAD(&conn->mgmt_run_list);
Mike Christie843c0a82007-12-13 12:43:20 -06001890 INIT_LIST_HEAD(&conn->mgmtqueue);
Mike Christieb6c395e2006-07-24 15:47:15 -05001891 INIT_LIST_HEAD(&conn->xmitqueue);
Mike Christie843c0a82007-12-13 12:43:20 -06001892 INIT_LIST_HEAD(&conn->requeue);
David Howellsc4028952006-11-22 14:57:56 +00001893 INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
Mike Christie7996a772006-04-06 21:13:41 -05001894
1895 /* allocate login_mtask used for the login/text sequences */
1896 spin_lock_bh(&session->lock);
1897 if (!__kfifo_get(session->mgmtpool.queue,
1898 (void*)&conn->login_mtask,
1899 sizeof(void*))) {
1900 spin_unlock_bh(&session->lock);
1901 goto login_mtask_alloc_fail;
1902 }
1903 spin_unlock_bh(&session->lock);
1904
Mike Christiebf32ed32007-02-28 17:32:17 -06001905 data = kmalloc(ISCSI_DEF_MAX_RECV_SEG_LEN, GFP_KERNEL);
Mike Christied36ab6f2006-05-18 20:31:34 -05001906 if (!data)
1907 goto login_mtask_data_alloc_fail;
Mike Christiec8dc1e52006-07-24 15:47:39 -05001908 conn->login_mtask->data = conn->data = data;
Mike Christied36ab6f2006-05-18 20:31:34 -05001909
Mike Christie843c0a82007-12-13 12:43:20 -06001910 init_timer(&conn->tmf_timer);
Mike Christie7996a772006-04-06 21:13:41 -05001911 init_waitqueue_head(&conn->ehwait);
1912
1913 return cls_conn;
1914
Mike Christied36ab6f2006-05-18 20:31:34 -05001915login_mtask_data_alloc_fail:
1916 __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
1917 sizeof(void*));
Mike Christie7996a772006-04-06 21:13:41 -05001918login_mtask_alloc_fail:
Mike Christie7996a772006-04-06 21:13:41 -05001919 iscsi_destroy_conn(cls_conn);
1920 return NULL;
1921}
1922EXPORT_SYMBOL_GPL(iscsi_conn_setup);
1923
1924/**
1925 * iscsi_conn_teardown - teardown iscsi connection
1926 * cls_conn: iscsi class connection
1927 *
1928 * TODO: we may need to make this into a two step process
1929 * like scsi-mls remove + put host
1930 */
1931void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
1932{
1933 struct iscsi_conn *conn = cls_conn->dd_data;
1934 struct iscsi_session *session = conn->session;
1935 unsigned long flags;
1936
Mike Christief6d51802007-12-13 12:43:30 -06001937 del_timer_sync(&conn->transport_timer);
1938
Mike Christie7996a772006-04-06 21:13:41 -05001939 spin_lock_bh(&session->lock);
1940 conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
1941 if (session->leadconn == conn) {
1942 /*
1943 * leading connection? then give up on recovery.
1944 */
1945 session->state = ISCSI_STATE_TERMINATE;
1946 wake_up(&conn->ehwait);
1947 }
1948 spin_unlock_bh(&session->lock);
1949
Mike Christie7996a772006-04-06 21:13:41 -05001950 /*
1951 * Block until all in-progress commands for this connection
1952 * time out or fail.
1953 */
1954 for (;;) {
1955 spin_lock_irqsave(session->host->host_lock, flags);
1956 if (!session->host->host_busy) { /* OK for ERL == 0 */
1957 spin_unlock_irqrestore(session->host->host_lock, flags);
1958 break;
1959 }
1960 spin_unlock_irqrestore(session->host->host_lock, flags);
1961 msleep_interruptible(500);
Mike Christie322d7392008-01-31 13:36:52 -06001962 iscsi_conn_printk(KERN_INFO, conn, "iscsi conn_destroy(): "
1963 "host_busy %d host_failed %d\n",
1964 session->host->host_busy,
1965 session->host->host_failed);
Mike Christie7996a772006-04-06 21:13:41 -05001966 /*
1967 * force eh_abort() to unblock
1968 */
1969 wake_up(&conn->ehwait);
1970 }
1971
Mike Christie779ea122007-02-28 17:32:15 -06001972 /* flush queued up work because we free the connection below */
Mike Christie843c0a82007-12-13 12:43:20 -06001973 iscsi_suspend_tx(conn);
Mike Christie779ea122007-02-28 17:32:15 -06001974
Mike Christie7996a772006-04-06 21:13:41 -05001975 spin_lock_bh(&session->lock);
Mike Christiec8dc1e52006-07-24 15:47:39 -05001976 kfree(conn->data);
Mike Christief3ff0c32006-07-24 15:47:50 -05001977 kfree(conn->persistent_address);
Mike Christie7996a772006-04-06 21:13:41 -05001978 __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
1979 sizeof(void*));
Mike Christiee0726402007-07-26 12:46:48 -05001980 if (session->leadconn == conn)
Mike Christie7996a772006-04-06 21:13:41 -05001981 session->leadconn = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05001982 spin_unlock_bh(&session->lock);
1983
Mike Christie7996a772006-04-06 21:13:41 -05001984 iscsi_destroy_conn(cls_conn);
1985}
1986EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
1987
1988int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
1989{
1990 struct iscsi_conn *conn = cls_conn->dd_data;
1991 struct iscsi_session *session = conn->session;
1992
Mike Christieffd04362006-08-31 18:09:24 -04001993 if (!session) {
Mike Christie322d7392008-01-31 13:36:52 -06001994 iscsi_conn_printk(KERN_ERR, conn,
1995 "can't start unbound connection\n");
Mike Christie7996a772006-04-06 21:13:41 -05001996 return -EPERM;
1997 }
1998
Mike Christiedb98ccd2006-08-31 18:09:31 -04001999 if ((session->imm_data_en || !session->initial_r2t_en) &&
2000 session->first_burst > session->max_burst) {
Mike Christie322d7392008-01-31 13:36:52 -06002001 iscsi_conn_printk(KERN_INFO, conn, "invalid burst lengths: "
2002 "first_burst %d max_burst %d\n",
2003 session->first_burst, session->max_burst);
Mike Christieffd04362006-08-31 18:09:24 -04002004 return -EINVAL;
2005 }
2006
Mike Christief6d51802007-12-13 12:43:30 -06002007 if (conn->ping_timeout && !conn->recv_timeout) {
Mike Christie322d7392008-01-31 13:36:52 -06002008 iscsi_conn_printk(KERN_ERR, conn, "invalid recv timeout of "
2009 "zero. Using 5 seconds\n.");
Mike Christief6d51802007-12-13 12:43:30 -06002010 conn->recv_timeout = 5;
2011 }
2012
2013 if (conn->recv_timeout && !conn->ping_timeout) {
Mike Christie322d7392008-01-31 13:36:52 -06002014 iscsi_conn_printk(KERN_ERR, conn, "invalid ping timeout of "
2015 "zero. Using 5 seconds.\n");
Mike Christief6d51802007-12-13 12:43:30 -06002016 conn->ping_timeout = 5;
2017 }
2018
Mike Christie7996a772006-04-06 21:13:41 -05002019 spin_lock_bh(&session->lock);
2020 conn->c_stage = ISCSI_CONN_STARTED;
2021 session->state = ISCSI_STATE_LOGGED_IN;
Mike Christiee0726402007-07-26 12:46:48 -05002022 session->queued_cmdsn = session->cmdsn;
Mike Christie7996a772006-04-06 21:13:41 -05002023
Mike Christief6d51802007-12-13 12:43:30 -06002024 conn->last_recv = jiffies;
2025 conn->last_ping = jiffies;
2026 if (conn->recv_timeout && conn->ping_timeout)
2027 mod_timer(&conn->transport_timer,
2028 jiffies + (conn->recv_timeout * HZ));
2029
Mike Christie7996a772006-04-06 21:13:41 -05002030 switch(conn->stop_stage) {
2031 case STOP_CONN_RECOVER:
2032 /*
2033 * unblock eh_abort() if it is blocked. re-try all
2034 * commands after successful recovery
2035 */
Mike Christie7996a772006-04-06 21:13:41 -05002036 conn->stop_stage = 0;
Mike Christie843c0a82007-12-13 12:43:20 -06002037 conn->tmf_state = TMF_INITIAL;
Mike Christie7996a772006-04-06 21:13:41 -05002038 session->age++;
Mike Christie6eabafb2008-01-31 13:36:43 -06002039 break;
Mike Christie7996a772006-04-06 21:13:41 -05002040 case STOP_CONN_TERM:
Mike Christie7996a772006-04-06 21:13:41 -05002041 conn->stop_stage = 0;
2042 break;
Mike Christie7996a772006-04-06 21:13:41 -05002043 default:
2044 break;
2045 }
2046 spin_unlock_bh(&session->lock);
2047
Mike Christie6eabafb2008-01-31 13:36:43 -06002048 iscsi_unblock_session(session_to_cls(session));
2049 wake_up(&conn->ehwait);
Mike Christie7996a772006-04-06 21:13:41 -05002050 return 0;
2051}
2052EXPORT_SYMBOL_GPL(iscsi_conn_start);
2053
2054static void
2055flush_control_queues(struct iscsi_session *session, struct iscsi_conn *conn)
2056{
2057 struct iscsi_mgmt_task *mtask, *tmp;
2058
2059 /* handle pending */
Mike Christie843c0a82007-12-13 12:43:20 -06002060 list_for_each_entry_safe(mtask, tmp, &conn->mgmtqueue, running) {
2061 debug_scsi("flushing pending mgmt task itt 0x%x\n", mtask->itt);
Mike Christieb3a7ea82007-12-13 12:43:26 -06002062 iscsi_free_mgmt_task(conn, mtask);
Mike Christie7996a772006-04-06 21:13:41 -05002063 }
2064
2065 /* handle running */
2066 list_for_each_entry_safe(mtask, tmp, &conn->mgmt_run_list, running) {
2067 debug_scsi("flushing running mgmt task itt 0x%x\n", mtask->itt);
Mike Christieb3a7ea82007-12-13 12:43:26 -06002068 iscsi_free_mgmt_task(conn, mtask);
Mike Christie7996a772006-04-06 21:13:41 -05002069 }
2070
2071 conn->mtask = NULL;
2072}
2073
Mike Christie656cffc2006-05-18 20:31:42 -05002074static void iscsi_start_session_recovery(struct iscsi_session *session,
2075 struct iscsi_conn *conn, int flag)
Mike Christie7996a772006-04-06 21:13:41 -05002076{
Mike Christieed2abc72006-05-02 19:46:40 -05002077 int old_stop_stage;
2078
Mike Christief6d51802007-12-13 12:43:30 -06002079 del_timer_sync(&conn->transport_timer);
2080
Mike Christie6724add2007-08-15 01:38:30 -05002081 mutex_lock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05002082 spin_lock_bh(&session->lock);
Mike Christieed2abc72006-05-02 19:46:40 -05002083 if (conn->stop_stage == STOP_CONN_TERM) {
Mike Christie7996a772006-04-06 21:13:41 -05002084 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05002085 mutex_unlock(&session->eh_mutex);
2086 return;
2087 }
2088
2089 /*
2090 * The LLD either freed/unset the lock on us, or userspace called
2091 * stop but did not create a proper connection (connection was never
2092 * bound or it was unbound then stop was called).
2093 */
2094 if (!conn->recv_lock) {
2095 spin_unlock_bh(&session->lock);
2096 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05002097 return;
2098 }
Mike Christieed2abc72006-05-02 19:46:40 -05002099
2100 /*
2101 * When this is called for the in_login state, we only want to clean
Mike Christie67a61112006-05-30 00:37:20 -05002102 * up the login task and connection. We do not need to block and set
2103 * the recovery state again
Mike Christieed2abc72006-05-02 19:46:40 -05002104 */
Mike Christie67a61112006-05-30 00:37:20 -05002105 if (flag == STOP_CONN_TERM)
2106 session->state = ISCSI_STATE_TERMINATE;
2107 else if (conn->stop_stage != STOP_CONN_RECOVER)
2108 session->state = ISCSI_STATE_IN_RECOVERY;
Mike Christieed2abc72006-05-02 19:46:40 -05002109
2110 old_stop_stage = conn->stop_stage;
Mike Christie7996a772006-04-06 21:13:41 -05002111 conn->stop_stage = flag;
Mike Christie67a61112006-05-30 00:37:20 -05002112 conn->c_stage = ISCSI_CONN_STOPPED;
Mike Christie7996a772006-04-06 21:13:41 -05002113 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05002114
2115 iscsi_suspend_tx(conn);
Mike Christie7996a772006-04-06 21:13:41 -05002116
Mike Christie1c834692006-07-24 15:47:26 -05002117 write_lock_bh(conn->recv_lock);
2118 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
2119 write_unlock_bh(conn->recv_lock);
Mike Christie7996a772006-04-06 21:13:41 -05002120
Mike Christie7996a772006-04-06 21:13:41 -05002121 /*
2122 * for connection level recovery we should not calculate
2123 * header digest. conn->hdr_size used for optimization
2124 * in hdr_extract() and will be re-negotiated at
2125 * set_param() time.
2126 */
2127 if (flag == STOP_CONN_RECOVER) {
2128 conn->hdrdgst_en = 0;
2129 conn->datadgst_en = 0;
Mike Christie656cffc2006-05-18 20:31:42 -05002130 if (session->state == ISCSI_STATE_IN_RECOVERY &&
Mike Christie67a61112006-05-30 00:37:20 -05002131 old_stop_stage != STOP_CONN_RECOVER) {
2132 debug_scsi("blocking session\n");
Mike Christie7996a772006-04-06 21:13:41 -05002133 iscsi_block_session(session_to_cls(session));
Mike Christie67a61112006-05-30 00:37:20 -05002134 }
Mike Christie7996a772006-04-06 21:13:41 -05002135 }
Mike Christie656cffc2006-05-18 20:31:42 -05002136
Mike Christie656cffc2006-05-18 20:31:42 -05002137 /*
2138 * flush queues.
2139 */
2140 spin_lock_bh(&session->lock);
Mike Christie6eabafb2008-01-31 13:36:43 -06002141 fail_all_commands(conn, -1,
2142 STOP_CONN_RECOVER ? DID_BUS_BUSY : DID_ERROR);
Mike Christie656cffc2006-05-18 20:31:42 -05002143 flush_control_queues(session, conn);
2144 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05002145 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05002146}
Mike Christie7996a772006-04-06 21:13:41 -05002147
2148void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
2149{
2150 struct iscsi_conn *conn = cls_conn->dd_data;
2151 struct iscsi_session *session = conn->session;
2152
2153 switch (flag) {
2154 case STOP_CONN_RECOVER:
2155 case STOP_CONN_TERM:
2156 iscsi_start_session_recovery(session, conn, flag);
Mike Christie8d2860b2006-05-02 19:46:47 -05002157 break;
Mike Christie7996a772006-04-06 21:13:41 -05002158 default:
Mike Christie322d7392008-01-31 13:36:52 -06002159 iscsi_conn_printk(KERN_ERR, conn,
2160 "invalid stop flag %d\n", flag);
Mike Christie7996a772006-04-06 21:13:41 -05002161 }
2162}
2163EXPORT_SYMBOL_GPL(iscsi_conn_stop);
2164
2165int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
2166 struct iscsi_cls_conn *cls_conn, int is_leading)
2167{
2168 struct iscsi_session *session = class_to_transport_session(cls_session);
Mike Christie98644042006-10-16 18:09:39 -04002169 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05002170
Mike Christie7996a772006-04-06 21:13:41 -05002171 spin_lock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05002172 if (is_leading)
2173 session->leadconn = conn;
Mike Christie98644042006-10-16 18:09:39 -04002174 spin_unlock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05002175
2176 /*
2177 * Unblock xmitworker(), Login Phase will pass through.
2178 */
2179 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
2180 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
2181 return 0;
2182}
2183EXPORT_SYMBOL_GPL(iscsi_conn_bind);
2184
Mike Christiea54a52c2006-06-28 12:00:23 -05002185
2186int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
2187 enum iscsi_param param, char *buf, int buflen)
2188{
2189 struct iscsi_conn *conn = cls_conn->dd_data;
2190 struct iscsi_session *session = conn->session;
2191 uint32_t value;
2192
2193 switch(param) {
Mike Christie843c0a82007-12-13 12:43:20 -06002194 case ISCSI_PARAM_FAST_ABORT:
2195 sscanf(buf, "%d", &session->fast_abort);
2196 break;
Mike Christief6d51802007-12-13 12:43:30 -06002197 case ISCSI_PARAM_ABORT_TMO:
2198 sscanf(buf, "%d", &session->abort_timeout);
2199 break;
2200 case ISCSI_PARAM_LU_RESET_TMO:
2201 sscanf(buf, "%d", &session->lu_reset_timeout);
2202 break;
2203 case ISCSI_PARAM_PING_TMO:
2204 sscanf(buf, "%d", &conn->ping_timeout);
2205 break;
2206 case ISCSI_PARAM_RECV_TMO:
2207 sscanf(buf, "%d", &conn->recv_timeout);
2208 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002209 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2210 sscanf(buf, "%d", &conn->max_recv_dlength);
2211 break;
2212 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2213 sscanf(buf, "%d", &conn->max_xmit_dlength);
2214 break;
2215 case ISCSI_PARAM_HDRDGST_EN:
2216 sscanf(buf, "%d", &conn->hdrdgst_en);
2217 break;
2218 case ISCSI_PARAM_DATADGST_EN:
2219 sscanf(buf, "%d", &conn->datadgst_en);
2220 break;
2221 case ISCSI_PARAM_INITIAL_R2T_EN:
2222 sscanf(buf, "%d", &session->initial_r2t_en);
2223 break;
2224 case ISCSI_PARAM_MAX_R2T:
2225 sscanf(buf, "%d", &session->max_r2t);
2226 break;
2227 case ISCSI_PARAM_IMM_DATA_EN:
2228 sscanf(buf, "%d", &session->imm_data_en);
2229 break;
2230 case ISCSI_PARAM_FIRST_BURST:
2231 sscanf(buf, "%d", &session->first_burst);
2232 break;
2233 case ISCSI_PARAM_MAX_BURST:
2234 sscanf(buf, "%d", &session->max_burst);
2235 break;
2236 case ISCSI_PARAM_PDU_INORDER_EN:
2237 sscanf(buf, "%d", &session->pdu_inorder_en);
2238 break;
2239 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2240 sscanf(buf, "%d", &session->dataseq_inorder_en);
2241 break;
2242 case ISCSI_PARAM_ERL:
2243 sscanf(buf, "%d", &session->erl);
2244 break;
2245 case ISCSI_PARAM_IFMARKER_EN:
2246 sscanf(buf, "%d", &value);
2247 BUG_ON(value);
2248 break;
2249 case ISCSI_PARAM_OFMARKER_EN:
2250 sscanf(buf, "%d", &value);
2251 BUG_ON(value);
2252 break;
2253 case ISCSI_PARAM_EXP_STATSN:
2254 sscanf(buf, "%u", &conn->exp_statsn);
2255 break;
Mike Christieb2c64162007-05-30 12:57:16 -05002256 case ISCSI_PARAM_USERNAME:
2257 kfree(session->username);
2258 session->username = kstrdup(buf, GFP_KERNEL);
2259 if (!session->username)
2260 return -ENOMEM;
2261 break;
2262 case ISCSI_PARAM_USERNAME_IN:
2263 kfree(session->username_in);
2264 session->username_in = kstrdup(buf, GFP_KERNEL);
2265 if (!session->username_in)
2266 return -ENOMEM;
2267 break;
2268 case ISCSI_PARAM_PASSWORD:
2269 kfree(session->password);
2270 session->password = kstrdup(buf, GFP_KERNEL);
2271 if (!session->password)
2272 return -ENOMEM;
2273 break;
2274 case ISCSI_PARAM_PASSWORD_IN:
2275 kfree(session->password_in);
2276 session->password_in = kstrdup(buf, GFP_KERNEL);
2277 if (!session->password_in)
2278 return -ENOMEM;
2279 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002280 case ISCSI_PARAM_TARGET_NAME:
2281 /* this should not change between logins */
2282 if (session->targetname)
2283 break;
2284
2285 session->targetname = kstrdup(buf, GFP_KERNEL);
2286 if (!session->targetname)
2287 return -ENOMEM;
2288 break;
2289 case ISCSI_PARAM_TPGT:
2290 sscanf(buf, "%d", &session->tpgt);
2291 break;
2292 case ISCSI_PARAM_PERSISTENT_PORT:
2293 sscanf(buf, "%d", &conn->persistent_port);
2294 break;
2295 case ISCSI_PARAM_PERSISTENT_ADDRESS:
2296 /*
2297 * this is the address returned in discovery so it should
2298 * not change between logins.
2299 */
2300 if (conn->persistent_address)
2301 break;
2302
2303 conn->persistent_address = kstrdup(buf, GFP_KERNEL);
2304 if (!conn->persistent_address)
2305 return -ENOMEM;
2306 break;
2307 default:
2308 return -ENOSYS;
2309 }
2310
2311 return 0;
2312}
2313EXPORT_SYMBOL_GPL(iscsi_set_param);
2314
2315int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
2316 enum iscsi_param param, char *buf)
2317{
2318 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
2319 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
2320 int len;
2321
2322 switch(param) {
Mike Christie843c0a82007-12-13 12:43:20 -06002323 case ISCSI_PARAM_FAST_ABORT:
2324 len = sprintf(buf, "%d\n", session->fast_abort);
2325 break;
Mike Christief6d51802007-12-13 12:43:30 -06002326 case ISCSI_PARAM_ABORT_TMO:
2327 len = sprintf(buf, "%d\n", session->abort_timeout);
2328 break;
2329 case ISCSI_PARAM_LU_RESET_TMO:
2330 len = sprintf(buf, "%d\n", session->lu_reset_timeout);
2331 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002332 case ISCSI_PARAM_INITIAL_R2T_EN:
2333 len = sprintf(buf, "%d\n", session->initial_r2t_en);
2334 break;
2335 case ISCSI_PARAM_MAX_R2T:
2336 len = sprintf(buf, "%hu\n", session->max_r2t);
2337 break;
2338 case ISCSI_PARAM_IMM_DATA_EN:
2339 len = sprintf(buf, "%d\n", session->imm_data_en);
2340 break;
2341 case ISCSI_PARAM_FIRST_BURST:
2342 len = sprintf(buf, "%u\n", session->first_burst);
2343 break;
2344 case ISCSI_PARAM_MAX_BURST:
2345 len = sprintf(buf, "%u\n", session->max_burst);
2346 break;
2347 case ISCSI_PARAM_PDU_INORDER_EN:
2348 len = sprintf(buf, "%d\n", session->pdu_inorder_en);
2349 break;
2350 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2351 len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
2352 break;
2353 case ISCSI_PARAM_ERL:
2354 len = sprintf(buf, "%d\n", session->erl);
2355 break;
2356 case ISCSI_PARAM_TARGET_NAME:
2357 len = sprintf(buf, "%s\n", session->targetname);
2358 break;
2359 case ISCSI_PARAM_TPGT:
2360 len = sprintf(buf, "%d\n", session->tpgt);
2361 break;
Mike Christieb2c64162007-05-30 12:57:16 -05002362 case ISCSI_PARAM_USERNAME:
2363 len = sprintf(buf, "%s\n", session->username);
2364 break;
2365 case ISCSI_PARAM_USERNAME_IN:
2366 len = sprintf(buf, "%s\n", session->username_in);
2367 break;
2368 case ISCSI_PARAM_PASSWORD:
2369 len = sprintf(buf, "%s\n", session->password);
2370 break;
2371 case ISCSI_PARAM_PASSWORD_IN:
2372 len = sprintf(buf, "%s\n", session->password_in);
2373 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002374 default:
2375 return -ENOSYS;
2376 }
2377
2378 return len;
2379}
2380EXPORT_SYMBOL_GPL(iscsi_session_get_param);
2381
2382int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
2383 enum iscsi_param param, char *buf)
2384{
2385 struct iscsi_conn *conn = cls_conn->dd_data;
2386 int len;
2387
2388 switch(param) {
Mike Christief6d51802007-12-13 12:43:30 -06002389 case ISCSI_PARAM_PING_TMO:
2390 len = sprintf(buf, "%u\n", conn->ping_timeout);
2391 break;
2392 case ISCSI_PARAM_RECV_TMO:
2393 len = sprintf(buf, "%u\n", conn->recv_timeout);
2394 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002395 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2396 len = sprintf(buf, "%u\n", conn->max_recv_dlength);
2397 break;
2398 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2399 len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
2400 break;
2401 case ISCSI_PARAM_HDRDGST_EN:
2402 len = sprintf(buf, "%d\n", conn->hdrdgst_en);
2403 break;
2404 case ISCSI_PARAM_DATADGST_EN:
2405 len = sprintf(buf, "%d\n", conn->datadgst_en);
2406 break;
2407 case ISCSI_PARAM_IFMARKER_EN:
2408 len = sprintf(buf, "%d\n", conn->ifmarker_en);
2409 break;
2410 case ISCSI_PARAM_OFMARKER_EN:
2411 len = sprintf(buf, "%d\n", conn->ofmarker_en);
2412 break;
2413 case ISCSI_PARAM_EXP_STATSN:
2414 len = sprintf(buf, "%u\n", conn->exp_statsn);
2415 break;
2416 case ISCSI_PARAM_PERSISTENT_PORT:
2417 len = sprintf(buf, "%d\n", conn->persistent_port);
2418 break;
2419 case ISCSI_PARAM_PERSISTENT_ADDRESS:
2420 len = sprintf(buf, "%s\n", conn->persistent_address);
2421 break;
2422 default:
2423 return -ENOSYS;
2424 }
2425
2426 return len;
2427}
2428EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
2429
Mike Christie0801c242007-05-30 12:57:12 -05002430int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2431 char *buf)
2432{
2433 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
2434 int len;
2435
2436 switch (param) {
Mike Christied8196ed2007-05-30 12:57:25 -05002437 case ISCSI_HOST_PARAM_NETDEV_NAME:
2438 if (!session->netdev)
2439 len = sprintf(buf, "%s\n", "default");
2440 else
2441 len = sprintf(buf, "%s\n", session->netdev);
2442 break;
Mike Christie0801c242007-05-30 12:57:12 -05002443 case ISCSI_HOST_PARAM_HWADDRESS:
2444 if (!session->hwaddress)
2445 len = sprintf(buf, "%s\n", "default");
2446 else
2447 len = sprintf(buf, "%s\n", session->hwaddress);
2448 break;
Mike Christie8ad57812007-05-30 12:57:13 -05002449 case ISCSI_HOST_PARAM_INITIATOR_NAME:
2450 if (!session->initiatorname)
2451 len = sprintf(buf, "%s\n", "unknown");
2452 else
2453 len = sprintf(buf, "%s\n", session->initiatorname);
2454 break;
2455
Mike Christie0801c242007-05-30 12:57:12 -05002456 default:
2457 return -ENOSYS;
2458 }
2459
2460 return len;
2461}
2462EXPORT_SYMBOL_GPL(iscsi_host_get_param);
2463
2464int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2465 char *buf, int buflen)
2466{
2467 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
2468
2469 switch (param) {
Mike Christied8196ed2007-05-30 12:57:25 -05002470 case ISCSI_HOST_PARAM_NETDEV_NAME:
2471 if (!session->netdev)
2472 session->netdev = kstrdup(buf, GFP_KERNEL);
2473 break;
Mike Christie0801c242007-05-30 12:57:12 -05002474 case ISCSI_HOST_PARAM_HWADDRESS:
2475 if (!session->hwaddress)
2476 session->hwaddress = kstrdup(buf, GFP_KERNEL);
2477 break;
Mike Christie8ad57812007-05-30 12:57:13 -05002478 case ISCSI_HOST_PARAM_INITIATOR_NAME:
2479 if (!session->initiatorname)
2480 session->initiatorname = kstrdup(buf, GFP_KERNEL);
2481 break;
Mike Christie0801c242007-05-30 12:57:12 -05002482 default:
2483 return -ENOSYS;
2484 }
2485
2486 return 0;
2487}
2488EXPORT_SYMBOL_GPL(iscsi_host_set_param);
2489
Mike Christie7996a772006-04-06 21:13:41 -05002490MODULE_AUTHOR("Mike Christie");
2491MODULE_DESCRIPTION("iSCSI library functions");
2492MODULE_LICENSE("GPL");