blob: 0beb4c6209621be5ee87237ed52e99675e0a036b [file] [log] [blame]
Mike Christie7996a772006-04-06 21:13:41 -05001/*
2 * iSCSI lib functions
3 *
4 * Copyright (C) 2006 Red Hat, Inc. All rights reserved.
5 * Copyright (C) 2004 - 2006 Mike Christie
6 * Copyright (C) 2004 - 2005 Dmitry Yusupov
7 * Copyright (C) 2004 - 2005 Alex Aizman
8 * maintained by open-iscsi@googlegroups.com
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 */
24#include <linux/types.h>
Mike Christie7996a772006-04-06 21:13:41 -050025#include <linux/kfifo.h>
26#include <linux/delay.h>
Mike Christie8eb00532007-02-28 17:32:19 -060027#include <asm/unaligned.h>
Mike Christie7996a772006-04-06 21:13:41 -050028#include <net/tcp.h>
29#include <scsi/scsi_cmnd.h>
30#include <scsi/scsi_device.h>
31#include <scsi/scsi_eh.h>
32#include <scsi/scsi_tcq.h>
33#include <scsi/scsi_host.h>
34#include <scsi/scsi.h>
35#include <scsi/iscsi_proto.h>
36#include <scsi/scsi_transport.h>
37#include <scsi/scsi_transport_iscsi.h>
38#include <scsi/libiscsi.h>
39
40struct iscsi_session *
41class_to_transport_session(struct iscsi_cls_session *cls_session)
42{
43 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
44 return iscsi_hostdata(shost->hostdata);
45}
46EXPORT_SYMBOL_GPL(class_to_transport_session);
47
Mike Christie77a23c22007-05-30 12:57:18 -050048/* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
49#define SNA32_CHECK 2147483648UL
Mike Christie7996a772006-04-06 21:13:41 -050050
Mike Christie77a23c22007-05-30 12:57:18 -050051static int iscsi_sna_lt(u32 n1, u32 n2)
52{
53 return n1 != n2 && ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
54 (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
55}
56
57/* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
58static int iscsi_sna_lte(u32 n1, u32 n2)
59{
60 return n1 == n2 || ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
61 (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
62}
63
64void
65iscsi_update_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr)
Mike Christie7996a772006-04-06 21:13:41 -050066{
67 uint32_t max_cmdsn = be32_to_cpu(hdr->max_cmdsn);
68 uint32_t exp_cmdsn = be32_to_cpu(hdr->exp_cmdsn);
69
Mike Christie77a23c22007-05-30 12:57:18 -050070 /*
71 * standard specifies this check for when to update expected and
72 * max sequence numbers
73 */
74 if (iscsi_sna_lt(max_cmdsn, exp_cmdsn - 1))
75 return;
76
77 if (exp_cmdsn != session->exp_cmdsn &&
78 !iscsi_sna_lt(exp_cmdsn, session->exp_cmdsn))
Mike Christie7996a772006-04-06 21:13:41 -050079 session->exp_cmdsn = exp_cmdsn;
80
Mike Christie77a23c22007-05-30 12:57:18 -050081 if (max_cmdsn != session->max_cmdsn &&
82 !iscsi_sna_lt(max_cmdsn, session->max_cmdsn)) {
83 session->max_cmdsn = max_cmdsn;
84 /*
85 * if the window closed with IO queued, then kick the
86 * xmit thread
87 */
88 if (!list_empty(&session->leadconn->xmitqueue) ||
Mike Christie843c0a82007-12-13 12:43:20 -060089 !list_empty(&session->leadconn->mgmtqueue))
Mike Christie77a23c22007-05-30 12:57:18 -050090 scsi_queue_work(session->host,
91 &session->leadconn->xmitwork);
92 }
Mike Christie7996a772006-04-06 21:13:41 -050093}
Mike Christie77a23c22007-05-30 12:57:18 -050094EXPORT_SYMBOL_GPL(iscsi_update_cmdsn);
Mike Christie7996a772006-04-06 21:13:41 -050095
96void iscsi_prep_unsolicit_data_pdu(struct iscsi_cmd_task *ctask,
Mike Christieffd04362006-08-31 18:09:24 -040097 struct iscsi_data *hdr)
Mike Christie7996a772006-04-06 21:13:41 -050098{
99 struct iscsi_conn *conn = ctask->conn;
100
101 memset(hdr, 0, sizeof(struct iscsi_data));
102 hdr->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
103 hdr->datasn = cpu_to_be32(ctask->unsol_datasn);
104 ctask->unsol_datasn++;
105 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
106 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
107
108 hdr->itt = ctask->hdr->itt;
109 hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
Mike Christieffd04362006-08-31 18:09:24 -0400110 hdr->offset = cpu_to_be32(ctask->unsol_offset);
Mike Christie7996a772006-04-06 21:13:41 -0500111
112 if (ctask->unsol_count > conn->max_xmit_dlength) {
113 hton24(hdr->dlength, conn->max_xmit_dlength);
114 ctask->data_count = conn->max_xmit_dlength;
Mike Christieffd04362006-08-31 18:09:24 -0400115 ctask->unsol_offset += ctask->data_count;
Mike Christie7996a772006-04-06 21:13:41 -0500116 hdr->flags = 0;
117 } else {
118 hton24(hdr->dlength, ctask->unsol_count);
119 ctask->data_count = ctask->unsol_count;
120 hdr->flags = ISCSI_FLAG_CMD_FINAL;
121 }
122}
123EXPORT_SYMBOL_GPL(iscsi_prep_unsolicit_data_pdu);
124
125/**
126 * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
127 * @ctask: iscsi cmd task
128 *
129 * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
130 * fields like dlength or final based on how much data it sends
131 */
132static void iscsi_prep_scsi_cmd_pdu(struct iscsi_cmd_task *ctask)
133{
134 struct iscsi_conn *conn = ctask->conn;
135 struct iscsi_session *session = conn->session;
136 struct iscsi_cmd *hdr = ctask->hdr;
137 struct scsi_cmnd *sc = ctask->sc;
138
139 hdr->opcode = ISCSI_OP_SCSI_CMD;
140 hdr->flags = ISCSI_ATTR_SIMPLE;
141 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
Al Virob4377352007-02-09 16:39:40 +0000142 hdr->itt = build_itt(ctask->itt, conn->id, session->age);
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900143 hdr->data_length = cpu_to_be32(scsi_bufflen(sc));
Mike Christie7996a772006-04-06 21:13:41 -0500144 hdr->cmdsn = cpu_to_be32(session->cmdsn);
145 session->cmdsn++;
146 hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
147 memcpy(hdr->cdb, sc->cmnd, sc->cmd_len);
Mike Christied473cc72007-05-30 12:57:14 -0500148 if (sc->cmd_len < MAX_COMMAND_SIZE)
149 memset(&hdr->cdb[sc->cmd_len], 0,
150 MAX_COMMAND_SIZE - sc->cmd_len);
Mike Christie7996a772006-04-06 21:13:41 -0500151
Mike Christieffd04362006-08-31 18:09:24 -0400152 ctask->data_count = 0;
Mike Christie218432c2007-05-30 12:57:17 -0500153 ctask->imm_count = 0;
Mike Christie7996a772006-04-06 21:13:41 -0500154 if (sc->sc_data_direction == DMA_TO_DEVICE) {
155 hdr->flags |= ISCSI_FLAG_CMD_WRITE;
156 /*
157 * Write counters:
158 *
159 * imm_count bytes to be sent right after
160 * SCSI PDU Header
161 *
162 * unsol_count bytes(as Data-Out) to be sent
163 * without R2T ack right after
164 * immediate data
165 *
166 * r2t_data_count bytes to be sent via R2T ack's
167 *
168 * pad_count bytes to be sent as zero-padding
169 */
Mike Christie7996a772006-04-06 21:13:41 -0500170 ctask->unsol_count = 0;
Mike Christieffd04362006-08-31 18:09:24 -0400171 ctask->unsol_offset = 0;
Mike Christie7996a772006-04-06 21:13:41 -0500172 ctask->unsol_datasn = 0;
173
174 if (session->imm_data_en) {
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900175 if (scsi_bufflen(sc) >= session->first_burst)
Mike Christie7996a772006-04-06 21:13:41 -0500176 ctask->imm_count = min(session->first_burst,
177 conn->max_xmit_dlength);
178 else
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900179 ctask->imm_count = min(scsi_bufflen(sc),
Mike Christie7996a772006-04-06 21:13:41 -0500180 conn->max_xmit_dlength);
181 hton24(ctask->hdr->dlength, ctask->imm_count);
182 } else
183 zero_data(ctask->hdr->dlength);
184
Mike Christieffd04362006-08-31 18:09:24 -0400185 if (!session->initial_r2t_en) {
Mike Christie857ae0b2007-05-30 12:57:15 -0500186 ctask->unsol_count = min((session->first_burst),
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900187 (scsi_bufflen(sc))) - ctask->imm_count;
Mike Christieffd04362006-08-31 18:09:24 -0400188 ctask->unsol_offset = ctask->imm_count;
189 }
190
Mike Christie7996a772006-04-06 21:13:41 -0500191 if (!ctask->unsol_count)
192 /* No unsolicit Data-Out's */
193 ctask->hdr->flags |= ISCSI_FLAG_CMD_FINAL;
194 } else {
Mike Christie7996a772006-04-06 21:13:41 -0500195 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
196 zero_data(hdr->dlength);
197
198 if (sc->sc_data_direction == DMA_FROM_DEVICE)
199 hdr->flags |= ISCSI_FLAG_CMD_READ;
200 }
201
202 conn->scsicmd_pdus_cnt++;
Mike Christie77a23c22007-05-30 12:57:18 -0500203
204 debug_scsi("iscsi prep [%s cid %d sc %p cdb 0x%x itt 0x%x len %d "
205 "cmdsn %d win %d]\n",
206 sc->sc_data_direction == DMA_TO_DEVICE ? "write" : "read",
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900207 conn->id, sc, sc->cmnd[0], ctask->itt, scsi_bufflen(sc),
Mike Christie77a23c22007-05-30 12:57:18 -0500208 session->cmdsn, session->max_cmdsn - session->exp_cmdsn + 1);
Mike Christie7996a772006-04-06 21:13:41 -0500209}
Mike Christie7996a772006-04-06 21:13:41 -0500210
211/**
212 * iscsi_complete_command - return command back to scsi-ml
Mike Christie7996a772006-04-06 21:13:41 -0500213 * @ctask: iscsi cmd task
214 *
215 * Must be called with session lock.
216 * This function returns the scsi command to scsi-ml and returns
217 * the cmd task to the pool of available cmd tasks.
218 */
Mike Christie60ecebf2006-08-31 18:09:25 -0400219static void iscsi_complete_command(struct iscsi_cmd_task *ctask)
Mike Christie7996a772006-04-06 21:13:41 -0500220{
Mike Christie60ecebf2006-08-31 18:09:25 -0400221 struct iscsi_session *session = ctask->conn->session;
Mike Christie7996a772006-04-06 21:13:41 -0500222 struct scsi_cmnd *sc = ctask->sc;
223
Mike Christieb6c395ed2006-07-24 15:47:15 -0500224 ctask->state = ISCSI_TASK_COMPLETED;
Mike Christie7996a772006-04-06 21:13:41 -0500225 ctask->sc = NULL;
Mike Christief47f2cf2006-08-31 18:09:33 -0400226 /* SCSI eh reuses commands to verify us */
227 sc->SCp.ptr = NULL;
Mike Christie7996a772006-04-06 21:13:41 -0500228 list_del_init(&ctask->running);
229 __kfifo_put(session->cmdpool.queue, (void*)&ctask, sizeof(void*));
230 sc->scsi_done(sc);
231}
232
Mike Christie60ecebf2006-08-31 18:09:25 -0400233static void __iscsi_get_ctask(struct iscsi_cmd_task *ctask)
234{
235 atomic_inc(&ctask->refcount);
236}
237
Mike Christie60ecebf2006-08-31 18:09:25 -0400238static void __iscsi_put_ctask(struct iscsi_cmd_task *ctask)
239{
Mike Christiee648f632006-08-31 18:09:34 -0400240 if (atomic_dec_and_test(&ctask->refcount))
Mike Christie60ecebf2006-08-31 18:09:25 -0400241 iscsi_complete_command(ctask);
Mike Christie60ecebf2006-08-31 18:09:25 -0400242}
243
Mike Christie7996a772006-04-06 21:13:41 -0500244/**
245 * iscsi_cmd_rsp - SCSI Command Response processing
246 * @conn: iscsi connection
247 * @hdr: iscsi header
248 * @ctask: scsi command task
249 * @data: cmd data buffer
250 * @datalen: len of buffer
251 *
252 * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
253 * then completes the command and task.
254 **/
Mike Christie77a23c22007-05-30 12:57:18 -0500255static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
256 struct iscsi_cmd_task *ctask, char *data,
257 int datalen)
Mike Christie7996a772006-04-06 21:13:41 -0500258{
Mike Christie7996a772006-04-06 21:13:41 -0500259 struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)hdr;
260 struct iscsi_session *session = conn->session;
261 struct scsi_cmnd *sc = ctask->sc;
262
Mike Christie77a23c22007-05-30 12:57:18 -0500263 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
Mike Christie7996a772006-04-06 21:13:41 -0500264 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
265
266 sc->result = (DID_OK << 16) | rhdr->cmd_status;
267
268 if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
269 sc->result = DID_ERROR << 16;
270 goto out;
271 }
272
273 if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
Mike Christie9b80cb42006-12-17 12:10:28 -0600274 uint16_t senselen;
Mike Christie7996a772006-04-06 21:13:41 -0500275
276 if (datalen < 2) {
277invalid_datalen:
Or Gerlitzbe2df722006-05-02 19:46:43 -0500278 printk(KERN_ERR "iscsi: Got CHECK_CONDITION but "
279 "invalid data buffer size of %d\n", datalen);
Mike Christie7996a772006-04-06 21:13:41 -0500280 sc->result = DID_BAD_TARGET << 16;
281 goto out;
282 }
283
Mike Christie8eb00532007-02-28 17:32:19 -0600284 senselen = be16_to_cpu(get_unaligned((__be16 *) data));
Mike Christie7996a772006-04-06 21:13:41 -0500285 if (datalen < senselen)
286 goto invalid_datalen;
287
288 memcpy(sc->sense_buffer, data + 2,
Mike Christie9b80cb42006-12-17 12:10:28 -0600289 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
Mike Christie7996a772006-04-06 21:13:41 -0500290 debug_scsi("copied %d bytes of sense\n",
Mike Christie8eb00532007-02-28 17:32:19 -0600291 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
Mike Christie7996a772006-04-06 21:13:41 -0500292 }
293
Boaz Harrosh7207fea2007-12-13 12:43:22 -0600294 if (rhdr->flags & (ISCSI_FLAG_CMD_UNDERFLOW |
295 ISCSI_FLAG_CMD_OVERFLOW)) {
Mike Christie7996a772006-04-06 21:13:41 -0500296 int res_count = be32_to_cpu(rhdr->residual_count);
297
Boaz Harrosh7207fea2007-12-13 12:43:22 -0600298 if (res_count > 0 &&
299 (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW ||
300 res_count <= scsi_bufflen(sc)))
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900301 scsi_set_resid(sc, res_count);
Mike Christie7996a772006-04-06 21:13:41 -0500302 else
303 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
Boaz Harrosh7207fea2007-12-13 12:43:22 -0600304 } else if (rhdr->flags & (ISCSI_FLAG_CMD_BIDI_UNDERFLOW |
305 ISCSI_FLAG_CMD_BIDI_OVERFLOW))
Mike Christie7996a772006-04-06 21:13:41 -0500306 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
Mike Christie7996a772006-04-06 21:13:41 -0500307
308out:
309 debug_scsi("done [sc %lx res %d itt 0x%x]\n",
310 (long)sc, sc->result, ctask->itt);
311 conn->scsirsp_pdus_cnt++;
312
Mike Christie60ecebf2006-08-31 18:09:25 -0400313 __iscsi_put_ctask(ctask);
Mike Christie7996a772006-04-06 21:13:41 -0500314}
315
Mike Christie7ea8b822006-07-24 15:47:22 -0500316static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
317{
318 struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
319
320 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
321 conn->tmfrsp_pdus_cnt++;
322
Mike Christie843c0a82007-12-13 12:43:20 -0600323 if (conn->tmf_state != TMF_QUEUED)
Mike Christie7ea8b822006-07-24 15:47:22 -0500324 return;
325
326 if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
Mike Christie843c0a82007-12-13 12:43:20 -0600327 conn->tmf_state = TMF_SUCCESS;
Mike Christie7ea8b822006-07-24 15:47:22 -0500328 else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
Mike Christie843c0a82007-12-13 12:43:20 -0600329 conn->tmf_state = TMF_NOT_FOUND;
Mike Christie7ea8b822006-07-24 15:47:22 -0500330 else
Mike Christie843c0a82007-12-13 12:43:20 -0600331 conn->tmf_state = TMF_FAILED;
Mike Christie7ea8b822006-07-24 15:47:22 -0500332 wake_up(&conn->ehwait);
333}
334
Mike Christie62f38302006-08-31 18:09:27 -0400335static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
336 char *data, int datalen)
337{
338 struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
339 struct iscsi_hdr rejected_pdu;
340 uint32_t itt;
341
342 conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
343
344 if (reject->reason == ISCSI_REASON_DATA_DIGEST_ERROR) {
345 if (ntoh24(reject->dlength) > datalen)
346 return ISCSI_ERR_PROTO;
347
348 if (ntoh24(reject->dlength) >= sizeof(struct iscsi_hdr)) {
349 memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
Al Virob4377352007-02-09 16:39:40 +0000350 itt = get_itt(rejected_pdu.itt);
Mike Christie62f38302006-08-31 18:09:27 -0400351 printk(KERN_ERR "itt 0x%x had pdu (op 0x%x) rejected "
352 "due to DataDigest error.\n", itt,
353 rejected_pdu.opcode);
354 }
355 }
356 return 0;
357}
358
Mike Christie7996a772006-04-06 21:13:41 -0500359/**
360 * __iscsi_complete_pdu - complete pdu
361 * @conn: iscsi conn
362 * @hdr: iscsi header
363 * @data: data buffer
364 * @datalen: len of data buffer
365 *
366 * Completes pdu processing by freeing any resources allocated at
367 * queuecommand or send generic. session lock must be held and verify
368 * itt must have been called.
369 */
370int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
371 char *data, int datalen)
372{
373 struct iscsi_session *session = conn->session;
374 int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
375 struct iscsi_cmd_task *ctask;
376 struct iscsi_mgmt_task *mtask;
377 uint32_t itt;
378
Al Virob4377352007-02-09 16:39:40 +0000379 if (hdr->itt != RESERVED_ITT)
380 itt = get_itt(hdr->itt);
Mike Christie7996a772006-04-06 21:13:41 -0500381 else
Al Virob4377352007-02-09 16:39:40 +0000382 itt = ~0U;
Mike Christie7996a772006-04-06 21:13:41 -0500383
384 if (itt < session->cmds_max) {
385 ctask = session->cmds[itt];
386
387 debug_scsi("cmdrsp [op 0x%x cid %d itt 0x%x len %d]\n",
388 opcode, conn->id, ctask->itt, datalen);
389
390 switch(opcode) {
391 case ISCSI_OP_SCSI_CMD_RSP:
392 BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
Mike Christie77a23c22007-05-30 12:57:18 -0500393 iscsi_scsi_cmd_rsp(conn, hdr, ctask, data,
394 datalen);
Mike Christie7996a772006-04-06 21:13:41 -0500395 break;
396 case ISCSI_OP_SCSI_DATA_IN:
397 BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
398 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
399 conn->scsirsp_pdus_cnt++;
Mike Christie60ecebf2006-08-31 18:09:25 -0400400 __iscsi_put_ctask(ctask);
Mike Christie7996a772006-04-06 21:13:41 -0500401 }
402 break;
403 case ISCSI_OP_R2T:
404 /* LLD handles this for now */
405 break;
406 default:
407 rc = ISCSI_ERR_BAD_OPCODE;
408 break;
409 }
410 } else if (itt >= ISCSI_MGMT_ITT_OFFSET &&
411 itt < ISCSI_MGMT_ITT_OFFSET + session->mgmtpool_max) {
412 mtask = session->mgmt_cmds[itt - ISCSI_MGMT_ITT_OFFSET];
413
414 debug_scsi("immrsp [op 0x%x cid %d itt 0x%x len %d]\n",
415 opcode, conn->id, mtask->itt, datalen);
416
Mike Christie77a23c22007-05-30 12:57:18 -0500417 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
Mike Christie7996a772006-04-06 21:13:41 -0500418 switch(opcode) {
Mike Christie8d2860b2006-05-02 19:46:47 -0500419 case ISCSI_OP_LOGOUT_RSP:
Mike Christiec8dc1e52006-07-24 15:47:39 -0500420 if (datalen) {
421 rc = ISCSI_ERR_PROTO;
422 break;
423 }
Mike Christie8d2860b2006-05-02 19:46:47 -0500424 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
425 /* fall through */
Mike Christie7996a772006-04-06 21:13:41 -0500426 case ISCSI_OP_LOGIN_RSP:
427 case ISCSI_OP_TEXT_RSP:
Mike Christie8d2860b2006-05-02 19:46:47 -0500428 /*
429 * login related PDU's exp_statsn is handled in
430 * userspace
431 */
Mike Christie40527af2006-07-24 15:47:45 -0500432 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
433 rc = ISCSI_ERR_CONN_FAILED;
Mike Christie843c0a82007-12-13 12:43:20 -0600434 list_del_init(&mtask->running);
Mike Christie7996a772006-04-06 21:13:41 -0500435 if (conn->login_mtask != mtask)
436 __kfifo_put(session->mgmtpool.queue,
437 (void*)&mtask, sizeof(void*));
438 break;
439 case ISCSI_OP_SCSI_TMFUNC_RSP:
Mike Christie7996a772006-04-06 21:13:41 -0500440 if (datalen) {
441 rc = ISCSI_ERR_PROTO;
442 break;
443 }
Mike Christie8d2860b2006-05-02 19:46:47 -0500444
Mike Christie7ea8b822006-07-24 15:47:22 -0500445 iscsi_tmf_rsp(conn, hdr);
Mike Christie7996a772006-04-06 21:13:41 -0500446 break;
447 case ISCSI_OP_NOOP_IN:
Al Virob4377352007-02-09 16:39:40 +0000448 if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) || datalen) {
Mike Christie7996a772006-04-06 21:13:41 -0500449 rc = ISCSI_ERR_PROTO;
450 break;
451 }
Mike Christie7996a772006-04-06 21:13:41 -0500452 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
453
Mike Christie40527af2006-07-24 15:47:45 -0500454 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
455 rc = ISCSI_ERR_CONN_FAILED;
Mike Christie843c0a82007-12-13 12:43:20 -0600456 list_del_init(&mtask->running);
457 __kfifo_put(session->mgmtpool.queue,
458 (void*)&mtask, sizeof(void*));
Mike Christie7996a772006-04-06 21:13:41 -0500459 break;
460 default:
461 rc = ISCSI_ERR_BAD_OPCODE;
462 break;
463 }
Al Virob4377352007-02-09 16:39:40 +0000464 } else if (itt == ~0U) {
Mike Christie77a23c22007-05-30 12:57:18 -0500465 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
Mike Christie62f38302006-08-31 18:09:27 -0400466
Mike Christie7996a772006-04-06 21:13:41 -0500467 switch(opcode) {
468 case ISCSI_OP_NOOP_IN:
Mike Christie40527af2006-07-24 15:47:45 -0500469 if (datalen) {
Mike Christie7996a772006-04-06 21:13:41 -0500470 rc = ISCSI_ERR_PROTO;
Mike Christie40527af2006-07-24 15:47:45 -0500471 break;
472 }
473
Al Virob4377352007-02-09 16:39:40 +0000474 if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
Mike Christie40527af2006-07-24 15:47:45 -0500475 break;
476
477 if (iscsi_recv_pdu(conn->cls_conn, hdr, NULL, 0))
478 rc = ISCSI_ERR_CONN_FAILED;
Mike Christie7996a772006-04-06 21:13:41 -0500479 break;
480 case ISCSI_OP_REJECT:
Mike Christie62f38302006-08-31 18:09:27 -0400481 rc = iscsi_handle_reject(conn, hdr, data, datalen);
482 break;
Mike Christie7996a772006-04-06 21:13:41 -0500483 case ISCSI_OP_ASYNC_EVENT:
Mike Christie8d2860b2006-05-02 19:46:47 -0500484 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
Mike Christie5831c732006-10-16 18:09:41 -0400485 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
486 rc = ISCSI_ERR_CONN_FAILED;
Mike Christie7996a772006-04-06 21:13:41 -0500487 break;
488 default:
489 rc = ISCSI_ERR_BAD_OPCODE;
490 break;
491 }
492 } else
493 rc = ISCSI_ERR_BAD_ITT;
494
495 return rc;
496}
497EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
498
499int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
500 char *data, int datalen)
501{
502 int rc;
503
504 spin_lock(&conn->session->lock);
505 rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
506 spin_unlock(&conn->session->lock);
507 return rc;
508}
509EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
510
511/* verify itt (itt encoding: age+cid+itt) */
512int iscsi_verify_itt(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
513 uint32_t *ret_itt)
514{
515 struct iscsi_session *session = conn->session;
516 struct iscsi_cmd_task *ctask;
517 uint32_t itt;
518
Al Virob4377352007-02-09 16:39:40 +0000519 if (hdr->itt != RESERVED_ITT) {
520 if (((__force u32)hdr->itt & ISCSI_AGE_MASK) !=
Mike Christie7996a772006-04-06 21:13:41 -0500521 (session->age << ISCSI_AGE_SHIFT)) {
Or Gerlitzbe2df722006-05-02 19:46:43 -0500522 printk(KERN_ERR "iscsi: received itt %x expected "
Al Virob4377352007-02-09 16:39:40 +0000523 "session age (%x)\n", (__force u32)hdr->itt,
Mike Christie7996a772006-04-06 21:13:41 -0500524 session->age & ISCSI_AGE_MASK);
525 return ISCSI_ERR_BAD_ITT;
526 }
527
Al Virob4377352007-02-09 16:39:40 +0000528 if (((__force u32)hdr->itt & ISCSI_CID_MASK) !=
Mike Christie7996a772006-04-06 21:13:41 -0500529 (conn->id << ISCSI_CID_SHIFT)) {
Or Gerlitzbe2df722006-05-02 19:46:43 -0500530 printk(KERN_ERR "iscsi: received itt %x, expected "
Al Virob4377352007-02-09 16:39:40 +0000531 "CID (%x)\n", (__force u32)hdr->itt, conn->id);
Mike Christie7996a772006-04-06 21:13:41 -0500532 return ISCSI_ERR_BAD_ITT;
533 }
Al Virob4377352007-02-09 16:39:40 +0000534 itt = get_itt(hdr->itt);
Mike Christie7996a772006-04-06 21:13:41 -0500535 } else
Al Virob4377352007-02-09 16:39:40 +0000536 itt = ~0U;
Mike Christie7996a772006-04-06 21:13:41 -0500537
538 if (itt < session->cmds_max) {
539 ctask = session->cmds[itt];
540
541 if (!ctask->sc) {
Or Gerlitzbe2df722006-05-02 19:46:43 -0500542 printk(KERN_INFO "iscsi: dropping ctask with "
Mike Christie7996a772006-04-06 21:13:41 -0500543 "itt 0x%x\n", ctask->itt);
544 /* force drop */
545 return ISCSI_ERR_NO_SCSI_CMD;
546 }
547
548 if (ctask->sc->SCp.phase != session->age) {
Or Gerlitzbe2df722006-05-02 19:46:43 -0500549 printk(KERN_ERR "iscsi: ctask's session age %d, "
Mike Christie7996a772006-04-06 21:13:41 -0500550 "expected %d\n", ctask->sc->SCp.phase,
551 session->age);
552 return ISCSI_ERR_SESSION_FAILED;
553 }
554 }
555
556 *ret_itt = itt;
557 return 0;
558}
559EXPORT_SYMBOL_GPL(iscsi_verify_itt);
560
561void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
562{
563 struct iscsi_session *session = conn->session;
564 unsigned long flags;
565
566 spin_lock_irqsave(&session->lock, flags);
Mike Christie656cffc2006-05-18 20:31:42 -0500567 if (session->state == ISCSI_STATE_FAILED) {
568 spin_unlock_irqrestore(&session->lock, flags);
569 return;
570 }
571
Mike Christie67a61112006-05-30 00:37:20 -0500572 if (conn->stop_stage == 0)
Mike Christie7996a772006-04-06 21:13:41 -0500573 session->state = ISCSI_STATE_FAILED;
574 spin_unlock_irqrestore(&session->lock, flags);
575 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
576 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
577 iscsi_conn_error(conn->cls_conn, err);
578}
579EXPORT_SYMBOL_GPL(iscsi_conn_failure);
580
Mike Christie77a23c22007-05-30 12:57:18 -0500581static void iscsi_prep_mtask(struct iscsi_conn *conn,
582 struct iscsi_mgmt_task *mtask)
583{
584 struct iscsi_session *session = conn->session;
585 struct iscsi_hdr *hdr = mtask->hdr;
586 struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
587
588 if (hdr->opcode != (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) &&
589 hdr->opcode != (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
590 nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
591 /*
592 * pre-format CmdSN for outgoing PDU.
593 */
594 nop->cmdsn = cpu_to_be32(session->cmdsn);
595 if (hdr->itt != RESERVED_ITT) {
596 hdr->itt = build_itt(mtask->itt, conn->id, session->age);
Mike Christiee0726402007-07-26 12:46:48 -0500597 /*
598 * TODO: We always use immediate, so we never hit this.
599 * If we start to send tmfs or nops as non-immediate then
600 * we should start checking the cmdsn numbers for mgmt tasks.
601 */
Mike Christie77a23c22007-05-30 12:57:18 -0500602 if (conn->c_stage == ISCSI_CONN_STARTED &&
Mike Christiee0726402007-07-26 12:46:48 -0500603 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
604 session->queued_cmdsn++;
Mike Christie77a23c22007-05-30 12:57:18 -0500605 session->cmdsn++;
Mike Christiee0726402007-07-26 12:46:48 -0500606 }
Mike Christie77a23c22007-05-30 12:57:18 -0500607 }
608
609 if (session->tt->init_mgmt_task)
610 session->tt->init_mgmt_task(conn, mtask);
611
612 debug_scsi("mgmtpdu [op 0x%x hdr->itt 0x%x datalen %d]\n",
Mike Christie843c0a82007-12-13 12:43:20 -0600613 hdr->opcode & ISCSI_OPCODE_MASK, hdr->itt,
614 mtask->data_count);
Mike Christie77a23c22007-05-30 12:57:18 -0500615}
616
Mike Christie05db8882007-02-28 17:32:16 -0600617static int iscsi_xmit_mtask(struct iscsi_conn *conn)
Mike Christieb5072ea2006-10-16 18:09:42 -0400618{
619 struct iscsi_hdr *hdr = conn->mtask->hdr;
620 int rc, was_logout = 0;
621
Mike Christie77a23c22007-05-30 12:57:18 -0500622 spin_unlock_bh(&conn->session->lock);
Mike Christieb5072ea2006-10-16 18:09:42 -0400623 if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT) {
624 conn->session->state = ISCSI_STATE_IN_RECOVERY;
625 iscsi_block_session(session_to_cls(conn->session));
626 was_logout = 1;
627 }
628 rc = conn->session->tt->xmit_mgmt_task(conn, conn->mtask);
Mike Christie77a23c22007-05-30 12:57:18 -0500629 spin_lock_bh(&conn->session->lock);
Mike Christieb5072ea2006-10-16 18:09:42 -0400630 if (rc)
631 return rc;
632
Mike Christie05db8882007-02-28 17:32:16 -0600633 /* done with this in-progress mtask */
634 conn->mtask = NULL;
635
Mike Christieb5072ea2006-10-16 18:09:42 -0400636 if (was_logout) {
637 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
638 return -ENODATA;
639 }
640 return 0;
641}
642
Mike Christie77a23c22007-05-30 12:57:18 -0500643static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
644{
645 struct iscsi_session *session = conn->session;
646
647 /*
648 * Check for iSCSI window and take care of CmdSN wrap-around
649 */
Mike Christiee0726402007-07-26 12:46:48 -0500650 if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) {
651 debug_scsi("iSCSI CmdSN closed. ExpCmdSn %u MaxCmdSN %u "
652 "CmdSN %u/%u\n", session->exp_cmdsn,
653 session->max_cmdsn, session->cmdsn,
654 session->queued_cmdsn);
Mike Christie77a23c22007-05-30 12:57:18 -0500655 return -ENOSPC;
656 }
657 return 0;
658}
659
660static int iscsi_xmit_ctask(struct iscsi_conn *conn)
661{
662 struct iscsi_cmd_task *ctask = conn->ctask;
Mike Christie843c0a82007-12-13 12:43:20 -0600663 int rc;
Mike Christie77a23c22007-05-30 12:57:18 -0500664
665 __iscsi_get_ctask(ctask);
666 spin_unlock_bh(&conn->session->lock);
667 rc = conn->session->tt->xmit_cmd_task(conn, ctask);
668 spin_lock_bh(&conn->session->lock);
669 __iscsi_put_ctask(ctask);
Mike Christie77a23c22007-05-30 12:57:18 -0500670 if (!rc)
671 /* done with this ctask */
672 conn->ctask = NULL;
673 return rc;
674}
675
Mike Christie7996a772006-04-06 21:13:41 -0500676/**
Mike Christie843c0a82007-12-13 12:43:20 -0600677 * iscsi_requeue_ctask - requeue ctask to run from session workqueue
678 * @ctask: ctask to requeue
679 *
680 * LLDs that need to run a ctask from the session workqueue should call
681 * this. The session lock must be held.
682 */
683void iscsi_requeue_ctask(struct iscsi_cmd_task *ctask)
684{
685 struct iscsi_conn *conn = ctask->conn;
686
687 list_move_tail(&ctask->running, &conn->requeue);
688 scsi_queue_work(conn->session->host, &conn->xmitwork);
689}
690EXPORT_SYMBOL_GPL(iscsi_requeue_ctask);
691
692/**
Mike Christie7996a772006-04-06 21:13:41 -0500693 * iscsi_data_xmit - xmit any command into the scheduled connection
694 * @conn: iscsi connection
695 *
696 * Notes:
697 * The function can return -EAGAIN in which case the caller must
698 * re-schedule it again later or recover. '0' return code means
699 * successful xmit.
700 **/
701static int iscsi_data_xmit(struct iscsi_conn *conn)
702{
Mike Christie3219e522006-05-30 00:37:28 -0500703 int rc = 0;
Mike Christie7996a772006-04-06 21:13:41 -0500704
Mike Christie77a23c22007-05-30 12:57:18 -0500705 spin_lock_bh(&conn->session->lock);
Mike Christie7996a772006-04-06 21:13:41 -0500706 if (unlikely(conn->suspend_tx)) {
707 debug_scsi("conn %d Tx suspended!\n", conn->id);
Mike Christie77a23c22007-05-30 12:57:18 -0500708 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -0500709 return -ENODATA;
Mike Christie7996a772006-04-06 21:13:41 -0500710 }
Mike Christie7996a772006-04-06 21:13:41 -0500711
712 if (conn->ctask) {
Mike Christie77a23c22007-05-30 12:57:18 -0500713 rc = iscsi_xmit_ctask(conn);
Mike Christie3219e522006-05-30 00:37:28 -0500714 if (rc)
Mike Christie7996a772006-04-06 21:13:41 -0500715 goto again;
Mike Christie7996a772006-04-06 21:13:41 -0500716 }
Mike Christie77a23c22007-05-30 12:57:18 -0500717
Mike Christie7996a772006-04-06 21:13:41 -0500718 if (conn->mtask) {
Mike Christie05db8882007-02-28 17:32:16 -0600719 rc = iscsi_xmit_mtask(conn);
Mike Christie3219e522006-05-30 00:37:28 -0500720 if (rc)
Mike Christie7996a772006-04-06 21:13:41 -0500721 goto again;
Mike Christie7996a772006-04-06 21:13:41 -0500722 }
723
Mike Christie77a23c22007-05-30 12:57:18 -0500724 /*
725 * process mgmt pdus like nops before commands since we should
726 * only have one nop-out as a ping from us and targets should not
727 * overflow us with nop-ins
728 */
729check_mgmt:
Mike Christie843c0a82007-12-13 12:43:20 -0600730 while (!list_empty(&conn->mgmtqueue)) {
731 conn->mtask = list_entry(conn->mgmtqueue.next,
732 struct iscsi_mgmt_task, running);
Mike Christie77a23c22007-05-30 12:57:18 -0500733 iscsi_prep_mtask(conn, conn->mtask);
Mike Christie843c0a82007-12-13 12:43:20 -0600734 list_move_tail(conn->mgmtqueue.next, &conn->mgmt_run_list);
Mike Christie77a23c22007-05-30 12:57:18 -0500735 rc = iscsi_xmit_mtask(conn);
736 if (rc)
737 goto again;
Mike Christie7996a772006-04-06 21:13:41 -0500738 }
739
Mike Christie843c0a82007-12-13 12:43:20 -0600740 /* process pending command queue */
Mike Christieb6c395ed2006-07-24 15:47:15 -0500741 while (!list_empty(&conn->xmitqueue)) {
Mike Christie843c0a82007-12-13 12:43:20 -0600742 if (conn->tmf_state == TMF_QUEUED)
743 break;
744
Mike Christieb6c395ed2006-07-24 15:47:15 -0500745 conn->ctask = list_entry(conn->xmitqueue.next,
746 struct iscsi_cmd_task, running);
Mike Christie843c0a82007-12-13 12:43:20 -0600747 iscsi_prep_scsi_cmd_pdu(conn->ctask);
748 conn->session->tt->init_cmd_task(conn->ctask);
749 conn->ctask->state = ISCSI_TASK_RUNNING;
Mike Christieb6c395ed2006-07-24 15:47:15 -0500750 list_move_tail(conn->xmitqueue.next, &conn->run_list);
Mike Christie77a23c22007-05-30 12:57:18 -0500751 rc = iscsi_xmit_ctask(conn);
752 if (rc)
Mike Christie60ecebf2006-08-31 18:09:25 -0400753 goto again;
Mike Christie77a23c22007-05-30 12:57:18 -0500754 /*
755 * we could continuously get new ctask requests so
756 * we need to check the mgmt queue for nops that need to
757 * be sent to aviod starvation
758 */
Mike Christie843c0a82007-12-13 12:43:20 -0600759 if (!list_empty(&conn->mgmtqueue))
760 goto check_mgmt;
761 }
762
763 while (!list_empty(&conn->requeue)) {
764 if (conn->session->fast_abort && conn->tmf_state != TMF_INITIAL)
765 break;
766
767 conn->ctask = list_entry(conn->requeue.next,
768 struct iscsi_cmd_task, running);
769 conn->ctask->state = ISCSI_TASK_RUNNING;
770 list_move_tail(conn->requeue.next, &conn->run_list);
771 rc = iscsi_xmit_ctask(conn);
772 if (rc)
773 goto again;
774 if (!list_empty(&conn->mgmtqueue))
Mike Christie77a23c22007-05-30 12:57:18 -0500775 goto check_mgmt;
Mike Christie7996a772006-04-06 21:13:41 -0500776 }
Mike Christieb6c395ed2006-07-24 15:47:15 -0500777 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -0500778 return -ENODATA;
Mike Christie7996a772006-04-06 21:13:41 -0500779
780again:
781 if (unlikely(conn->suspend_tx))
Mike Christie77a23c22007-05-30 12:57:18 -0500782 rc = -ENODATA;
783 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -0500784 return rc;
Mike Christie7996a772006-04-06 21:13:41 -0500785}
786
David Howellsc4028952006-11-22 14:57:56 +0000787static void iscsi_xmitworker(struct work_struct *work)
Mike Christie7996a772006-04-06 21:13:41 -0500788{
David Howellsc4028952006-11-22 14:57:56 +0000789 struct iscsi_conn *conn =
790 container_of(work, struct iscsi_conn, xmitwork);
Mike Christie3219e522006-05-30 00:37:28 -0500791 int rc;
Mike Christie7996a772006-04-06 21:13:41 -0500792 /*
793 * serialize Xmit worker on a per-connection basis.
794 */
Mike Christie3219e522006-05-30 00:37:28 -0500795 do {
796 rc = iscsi_data_xmit(conn);
797 } while (rc >= 0 || rc == -EAGAIN);
Mike Christie7996a772006-04-06 21:13:41 -0500798}
799
800enum {
801 FAILURE_BAD_HOST = 1,
802 FAILURE_SESSION_FAILED,
803 FAILURE_SESSION_FREED,
804 FAILURE_WINDOW_CLOSED,
Mike Christie60ecebf2006-08-31 18:09:25 -0400805 FAILURE_OOM,
Mike Christie7996a772006-04-06 21:13:41 -0500806 FAILURE_SESSION_TERMINATE,
Mike Christie656cffc2006-05-18 20:31:42 -0500807 FAILURE_SESSION_IN_RECOVERY,
Mike Christie7996a772006-04-06 21:13:41 -0500808 FAILURE_SESSION_RECOVERY_TIMEOUT,
809};
810
811int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
812{
813 struct Scsi_Host *host;
814 int reason = 0;
815 struct iscsi_session *session;
816 struct iscsi_conn *conn;
817 struct iscsi_cmd_task *ctask = NULL;
818
819 sc->scsi_done = done;
820 sc->result = 0;
Mike Christief47f2cf2006-08-31 18:09:33 -0400821 sc->SCp.ptr = NULL;
Mike Christie7996a772006-04-06 21:13:41 -0500822
823 host = sc->device->host;
824 session = iscsi_hostdata(host->hostdata);
825
826 spin_lock(&session->lock);
827
Mike Christie656cffc2006-05-18 20:31:42 -0500828 /*
829 * ISCSI_STATE_FAILED is a temp. state. The recovery
830 * code will decide what is best to do with command queued
831 * during this time
832 */
833 if (session->state != ISCSI_STATE_LOGGED_IN &&
834 session->state != ISCSI_STATE_FAILED) {
835 /*
836 * to handle the race between when we set the recovery state
837 * and block the session we requeue here (commands could
838 * be entering our queuecommand while a block is starting
839 * up because the block code is not locked)
840 */
841 if (session->state == ISCSI_STATE_IN_RECOVERY) {
842 reason = FAILURE_SESSION_IN_RECOVERY;
Mike Christie67a61112006-05-30 00:37:20 -0500843 goto reject;
Mike Christie7996a772006-04-06 21:13:41 -0500844 }
Mike Christie656cffc2006-05-18 20:31:42 -0500845
846 if (session->state == ISCSI_STATE_RECOVERY_FAILED)
847 reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
848 else if (session->state == ISCSI_STATE_TERMINATE)
849 reason = FAILURE_SESSION_TERMINATE;
850 else
851 reason = FAILURE_SESSION_FREED;
Mike Christie7996a772006-04-06 21:13:41 -0500852 goto fault;
853 }
854
Mike Christie7996a772006-04-06 21:13:41 -0500855 conn = session->leadconn;
Mike Christie98644042006-10-16 18:09:39 -0400856 if (!conn) {
857 reason = FAILURE_SESSION_FREED;
858 goto fault;
859 }
Mike Christie7996a772006-04-06 21:13:41 -0500860
Mike Christie77a23c22007-05-30 12:57:18 -0500861 if (iscsi_check_cmdsn_window_closed(conn)) {
862 reason = FAILURE_WINDOW_CLOSED;
863 goto reject;
864 }
865
Mike Christie60ecebf2006-08-31 18:09:25 -0400866 if (!__kfifo_get(session->cmdpool.queue, (void*)&ctask,
867 sizeof(void*))) {
868 reason = FAILURE_OOM;
869 goto reject;
870 }
Mike Christiee0726402007-07-26 12:46:48 -0500871 session->queued_cmdsn++;
872
Mike Christie7996a772006-04-06 21:13:41 -0500873 sc->SCp.phase = session->age;
874 sc->SCp.ptr = (char *)ctask;
875
Mike Christie60ecebf2006-08-31 18:09:25 -0400876 atomic_set(&ctask->refcount, 1);
Mike Christieb6c395ed2006-07-24 15:47:15 -0500877 ctask->state = ISCSI_TASK_PENDING;
Mike Christie7996a772006-04-06 21:13:41 -0500878 ctask->conn = conn;
879 ctask->sc = sc;
880 INIT_LIST_HEAD(&ctask->running);
Mike Christie7996a772006-04-06 21:13:41 -0500881
Mike Christieb6c395ed2006-07-24 15:47:15 -0500882 list_add_tail(&ctask->running, &conn->xmitqueue);
Mike Christie7996a772006-04-06 21:13:41 -0500883 spin_unlock(&session->lock);
884
885 scsi_queue_work(host, &conn->xmitwork);
886 return 0;
887
888reject:
889 spin_unlock(&session->lock);
890 debug_scsi("cmd 0x%x rejected (%d)\n", sc->cmnd[0], reason);
891 return SCSI_MLQUEUE_HOST_BUSY;
892
893fault:
894 spin_unlock(&session->lock);
Or Gerlitzbe2df722006-05-02 19:46:43 -0500895 printk(KERN_ERR "iscsi: cmd 0x%x is not queued (%d)\n",
Mike Christie7996a772006-04-06 21:13:41 -0500896 sc->cmnd[0], reason);
897 sc->result = (DID_NO_CONNECT << 16);
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900898 scsi_set_resid(sc, scsi_bufflen(sc));
Mike Christie7996a772006-04-06 21:13:41 -0500899 sc->scsi_done(sc);
900 return 0;
901}
902EXPORT_SYMBOL_GPL(iscsi_queuecommand);
903
904int iscsi_change_queue_depth(struct scsi_device *sdev, int depth)
905{
906 if (depth > ISCSI_MAX_CMD_PER_LUN)
907 depth = ISCSI_MAX_CMD_PER_LUN;
908 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
909 return sdev->queue_depth;
910}
911EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
912
Mike Christie77a23c22007-05-30 12:57:18 -0500913static struct iscsi_mgmt_task *
914__iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
915 char *data, uint32_t data_size)
Mike Christie7996a772006-04-06 21:13:41 -0500916{
917 struct iscsi_session *session = conn->session;
Mike Christie7996a772006-04-06 21:13:41 -0500918 struct iscsi_mgmt_task *mtask;
919
Mike Christie77a23c22007-05-30 12:57:18 -0500920 if (session->state == ISCSI_STATE_TERMINATE)
921 return NULL;
922
Mike Christie7996a772006-04-06 21:13:41 -0500923 if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) ||
924 hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
925 /*
926 * Login and Text are sent serially, in
927 * request-followed-by-response sequence.
928 * Same mtask can be used. Same ITT must be used.
929 * Note that login_mtask is preallocated at conn_create().
930 */
931 mtask = conn->login_mtask;
932 else {
Mike Christie656cffc2006-05-18 20:31:42 -0500933 BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
934 BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
Mike Christie7996a772006-04-06 21:13:41 -0500935
936 if (!__kfifo_get(session->mgmtpool.queue,
Mike Christie77a23c22007-05-30 12:57:18 -0500937 (void*)&mtask, sizeof(void*)))
938 return NULL;
Mike Christie7996a772006-04-06 21:13:41 -0500939 }
940
Mike Christie7996a772006-04-06 21:13:41 -0500941 if (data_size) {
942 memcpy(mtask->data, data, data_size);
943 mtask->data_count = data_size;
944 } else
945 mtask->data_count = 0;
946
Mike Christie7996a772006-04-06 21:13:41 -0500947 memcpy(mtask->hdr, hdr, sizeof(struct iscsi_hdr));
Mike Christie843c0a82007-12-13 12:43:20 -0600948 INIT_LIST_HEAD(&mtask->running);
949 list_add_tail(&mtask->running, &conn->mgmtqueue);
Mike Christie77a23c22007-05-30 12:57:18 -0500950 return mtask;
Mike Christie7996a772006-04-06 21:13:41 -0500951}
952
953int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
954 char *data, uint32_t data_size)
955{
956 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie77a23c22007-05-30 12:57:18 -0500957 struct iscsi_session *session = conn->session;
958 int err = 0;
Mike Christie7996a772006-04-06 21:13:41 -0500959
Mike Christie77a23c22007-05-30 12:57:18 -0500960 spin_lock_bh(&session->lock);
961 if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size))
962 err = -EPERM;
963 spin_unlock_bh(&session->lock);
964 scsi_queue_work(session->host, &conn->xmitwork);
965 return err;
Mike Christie7996a772006-04-06 21:13:41 -0500966}
967EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
968
969void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
970{
971 struct iscsi_session *session = class_to_transport_session(cls_session);
Mike Christie7996a772006-04-06 21:13:41 -0500972
973 spin_lock_bh(&session->lock);
974 if (session->state != ISCSI_STATE_LOGGED_IN) {
Mike Christie656cffc2006-05-18 20:31:42 -0500975 session->state = ISCSI_STATE_RECOVERY_FAILED;
Mike Christie843c0a82007-12-13 12:43:20 -0600976 if (session->leadconn)
977 wake_up(&session->leadconn->ehwait);
Mike Christie7996a772006-04-06 21:13:41 -0500978 }
979 spin_unlock_bh(&session->lock);
980}
981EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
982
983int iscsi_eh_host_reset(struct scsi_cmnd *sc)
984{
985 struct Scsi_Host *host = sc->device->host;
986 struct iscsi_session *session = iscsi_hostdata(host->hostdata);
987 struct iscsi_conn *conn = session->leadconn;
Mike Christie7996a772006-04-06 21:13:41 -0500988
989 spin_lock_bh(&session->lock);
990 if (session->state == ISCSI_STATE_TERMINATE) {
991failed:
992 debug_scsi("failing host reset: session terminated "
Pete Wyckoffd6e24d12006-11-08 15:58:32 -0600993 "[CID %d age %d]\n", conn->id, session->age);
Mike Christie7996a772006-04-06 21:13:41 -0500994 spin_unlock_bh(&session->lock);
995 return FAILED;
996 }
997
Mike Christie7996a772006-04-06 21:13:41 -0500998 spin_unlock_bh(&session->lock);
999
1000 /*
1001 * we drop the lock here but the leadconn cannot be destoyed while
1002 * we are in the scsi eh
1003 */
Mike Christie843c0a82007-12-13 12:43:20 -06001004 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie7996a772006-04-06 21:13:41 -05001005
1006 debug_scsi("iscsi_eh_host_reset wait for relogin\n");
1007 wait_event_interruptible(conn->ehwait,
1008 session->state == ISCSI_STATE_TERMINATE ||
1009 session->state == ISCSI_STATE_LOGGED_IN ||
Mike Christie656cffc2006-05-18 20:31:42 -05001010 session->state == ISCSI_STATE_RECOVERY_FAILED);
Mike Christie7996a772006-04-06 21:13:41 -05001011 if (signal_pending(current))
1012 flush_signals(current);
1013
1014 spin_lock_bh(&session->lock);
1015 if (session->state == ISCSI_STATE_LOGGED_IN)
Or Gerlitzbe2df722006-05-02 19:46:43 -05001016 printk(KERN_INFO "iscsi: host reset succeeded\n");
Mike Christie7996a772006-04-06 21:13:41 -05001017 else
1018 goto failed;
1019 spin_unlock_bh(&session->lock);
1020
1021 return SUCCESS;
1022}
1023EXPORT_SYMBOL_GPL(iscsi_eh_host_reset);
1024
Mike Christie843c0a82007-12-13 12:43:20 -06001025static void iscsi_tmf_timedout(unsigned long data)
Mike Christie7996a772006-04-06 21:13:41 -05001026{
Mike Christie843c0a82007-12-13 12:43:20 -06001027 struct iscsi_conn *conn = (struct iscsi_conn *)data;
Mike Christie7996a772006-04-06 21:13:41 -05001028 struct iscsi_session *session = conn->session;
1029
1030 spin_lock(&session->lock);
Mike Christie843c0a82007-12-13 12:43:20 -06001031 if (conn->tmf_state == TMF_QUEUED) {
1032 conn->tmf_state = TMF_TIMEDOUT;
1033 debug_scsi("tmf timedout\n");
Mike Christie7996a772006-04-06 21:13:41 -05001034 /* unblock eh_abort() */
1035 wake_up(&conn->ehwait);
1036 }
1037 spin_unlock(&session->lock);
1038}
1039
Mike Christie843c0a82007-12-13 12:43:20 -06001040static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn,
1041 struct iscsi_tm *hdr, int age)
Mike Christie7996a772006-04-06 21:13:41 -05001042{
Mike Christie7996a772006-04-06 21:13:41 -05001043 struct iscsi_session *session = conn->session;
Mike Christie843c0a82007-12-13 12:43:20 -06001044 struct iscsi_mgmt_task *mtask;
Mike Christie7996a772006-04-06 21:13:41 -05001045
Mike Christie843c0a82007-12-13 12:43:20 -06001046 mtask = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
1047 NULL, 0);
1048 if (!mtask) {
Mike Christie6724add2007-08-15 01:38:30 -05001049 spin_unlock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001050 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie843c0a82007-12-13 12:43:20 -06001051 spin_lock_bh(&session->lock);
1052 debug_scsi("tmf exec failure\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001053 return -EPERM;
Mike Christie7996a772006-04-06 21:13:41 -05001054 }
Mike Christie843c0a82007-12-13 12:43:20 -06001055 conn->tmfcmd_pdus_cnt++;
1056 conn->tmf_timer.expires = 30 * HZ + jiffies;
1057 conn->tmf_timer.function = iscsi_tmf_timedout;
1058 conn->tmf_timer.data = (unsigned long)conn;
1059 add_timer(&conn->tmf_timer);
1060 debug_scsi("tmf set timeout\n");
Mike Christie7996a772006-04-06 21:13:41 -05001061
Mike Christie7996a772006-04-06 21:13:41 -05001062 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05001063 mutex_unlock(&session->eh_mutex);
Mike Christie77a23c22007-05-30 12:57:18 -05001064 scsi_queue_work(session->host, &conn->xmitwork);
Mike Christie7996a772006-04-06 21:13:41 -05001065
1066 /*
1067 * block eh thread until:
1068 *
Mike Christie843c0a82007-12-13 12:43:20 -06001069 * 1) tmf response
1070 * 2) tmf timeout
Mike Christie7996a772006-04-06 21:13:41 -05001071 * 3) session is terminated or restarted or userspace has
1072 * given up on recovery
1073 */
Mike Christie843c0a82007-12-13 12:43:20 -06001074 wait_event_interruptible(conn->ehwait, age != session->age ||
Mike Christie7996a772006-04-06 21:13:41 -05001075 session->state != ISCSI_STATE_LOGGED_IN ||
Mike Christie843c0a82007-12-13 12:43:20 -06001076 conn->tmf_state != TMF_QUEUED);
Mike Christie7996a772006-04-06 21:13:41 -05001077 if (signal_pending(current))
1078 flush_signals(current);
Mike Christie843c0a82007-12-13 12:43:20 -06001079 del_timer_sync(&conn->tmf_timer);
1080
Mike Christie6724add2007-08-15 01:38:30 -05001081 mutex_lock(&session->eh_mutex);
Mike Christie77a23c22007-05-30 12:57:18 -05001082 spin_lock_bh(&session->lock);
Mike Christie843c0a82007-12-13 12:43:20 -06001083 /* if the session drops it will clean up the mtask */
1084 if (age != session->age ||
1085 session->state != ISCSI_STATE_LOGGED_IN)
1086 return -ENOTCONN;
Mike Christie7996a772006-04-06 21:13:41 -05001087
Mike Christie843c0a82007-12-13 12:43:20 -06001088 if (!list_empty(&mtask->running)) {
1089 list_del_init(&mtask->running);
1090 __kfifo_put(session->mgmtpool.queue, (void*)&mtask,
1091 sizeof(void*));
Mike Christieb6c395ed2006-07-24 15:47:15 -05001092 }
Mike Christie7996a772006-04-06 21:13:41 -05001093 return 0;
1094}
1095
1096/*
Mike Christie77a23c22007-05-30 12:57:18 -05001097 * session lock must be held
Mike Christie7996a772006-04-06 21:13:41 -05001098 */
1099static void fail_command(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
1100 int err)
1101{
1102 struct scsi_cmnd *sc;
1103
Mike Christie7996a772006-04-06 21:13:41 -05001104 sc = ctask->sc;
1105 if (!sc)
1106 return;
Mike Christiee648f632006-08-31 18:09:34 -04001107
Mike Christiee0726402007-07-26 12:46:48 -05001108 if (ctask->state == ISCSI_TASK_PENDING)
1109 /*
1110 * cmd never made it to the xmit thread, so we should not count
1111 * the cmd in the sequencing
1112 */
1113 conn->session->queued_cmdsn--;
1114 else
Mike Christie77a23c22007-05-30 12:57:18 -05001115 conn->session->tt->cleanup_cmd_task(conn, ctask);
Mike Christie7ea8b822006-07-24 15:47:22 -05001116
Mike Christie7996a772006-04-06 21:13:41 -05001117 sc->result = err;
FUJITA Tomonori1c138992007-06-14 22:13:17 +09001118 scsi_set_resid(sc, scsi_bufflen(sc));
Mike Christie77a23c22007-05-30 12:57:18 -05001119 if (conn->ctask == ctask)
1120 conn->ctask = NULL;
Mike Christiee648f632006-08-31 18:09:34 -04001121 /* release ref from queuecommand */
Mike Christie60ecebf2006-08-31 18:09:25 -04001122 __iscsi_put_ctask(ctask);
Mike Christie7996a772006-04-06 21:13:41 -05001123}
1124
Mike Christie843c0a82007-12-13 12:43:20 -06001125/*
1126 * Fail commands. session lock held and recv side suspended and xmit
1127 * thread flushed
1128 */
1129static void fail_all_commands(struct iscsi_conn *conn, unsigned lun)
1130{
1131 struct iscsi_cmd_task *ctask, *tmp;
1132
1133 if (conn->ctask && (conn->ctask->sc->device->lun == lun || lun == -1))
1134 conn->ctask = NULL;
1135
1136 /* flush pending */
1137 list_for_each_entry_safe(ctask, tmp, &conn->xmitqueue, running) {
1138 if (lun == ctask->sc->device->lun || lun == -1) {
1139 debug_scsi("failing pending sc %p itt 0x%x\n",
1140 ctask->sc, ctask->itt);
1141 fail_command(conn, ctask, DID_BUS_BUSY << 16);
1142 }
1143 }
1144
1145 list_for_each_entry_safe(ctask, tmp, &conn->requeue, running) {
1146 if (lun == ctask->sc->device->lun || lun == -1) {
1147 debug_scsi("failing requeued sc %p itt 0x%x\n",
1148 ctask->sc, ctask->itt);
1149 fail_command(conn, ctask, DID_BUS_BUSY << 16);
1150 }
1151 }
1152
1153 /* fail all other running */
1154 list_for_each_entry_safe(ctask, tmp, &conn->run_list, running) {
1155 if (lun == ctask->sc->device->lun || lun == -1) {
1156 debug_scsi("failing in progress sc %p itt 0x%x\n",
1157 ctask->sc, ctask->itt);
1158 fail_command(conn, ctask, DID_BUS_BUSY << 16);
1159 }
1160 }
1161}
1162
Mike Christie6724add2007-08-15 01:38:30 -05001163static void iscsi_suspend_tx(struct iscsi_conn *conn)
1164{
1165 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1166 scsi_flush_work(conn->session->host);
1167}
1168
1169static void iscsi_start_tx(struct iscsi_conn *conn)
1170{
1171 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1172 scsi_queue_work(conn->session->host, &conn->xmitwork);
1173}
1174
Mike Christie843c0a82007-12-13 12:43:20 -06001175static void iscsi_prep_abort_task_pdu(struct iscsi_cmd_task *ctask,
1176 struct iscsi_tm *hdr)
1177{
1178 memset(hdr, 0, sizeof(*hdr));
1179 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
1180 hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK;
1181 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
1182 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
1183 hdr->rtt = ctask->hdr->itt;
1184 hdr->refcmdsn = ctask->hdr->cmdsn;
1185}
1186
Mike Christie7996a772006-04-06 21:13:41 -05001187int iscsi_eh_abort(struct scsi_cmnd *sc)
1188{
Mike Christie6724add2007-08-15 01:38:30 -05001189 struct Scsi_Host *host = sc->device->host;
1190 struct iscsi_session *session = iscsi_hostdata(host->hostdata);
Mike Christief47f2cf2006-08-31 18:09:33 -04001191 struct iscsi_conn *conn;
Mike Christie843c0a82007-12-13 12:43:20 -06001192 struct iscsi_cmd_task *ctask;
1193 struct iscsi_tm *hdr;
1194 int rc, age;
Mike Christie7996a772006-04-06 21:13:41 -05001195
Mike Christie6724add2007-08-15 01:38:30 -05001196 mutex_lock(&session->eh_mutex);
1197 spin_lock_bh(&session->lock);
Mike Christief47f2cf2006-08-31 18:09:33 -04001198 /*
1199 * if session was ISCSI_STATE_IN_RECOVERY then we may not have
1200 * got the command.
1201 */
1202 if (!sc->SCp.ptr) {
1203 debug_scsi("sc never reached iscsi layer or it completed.\n");
Mike Christie6724add2007-08-15 01:38:30 -05001204 spin_unlock_bh(&session->lock);
1205 mutex_unlock(&session->eh_mutex);
Mike Christief47f2cf2006-08-31 18:09:33 -04001206 return SUCCESS;
1207 }
1208
Mike Christie7996a772006-04-06 21:13:41 -05001209 /*
1210 * If we are not logged in or we have started a new session
1211 * then let the host reset code handle this
1212 */
Mike Christie843c0a82007-12-13 12:43:20 -06001213 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN ||
1214 sc->SCp.phase != session->age) {
1215 spin_unlock_bh(&session->lock);
1216 mutex_unlock(&session->eh_mutex);
1217 return FAILED;
1218 }
1219
1220 conn = session->leadconn;
1221 conn->eh_abort_cnt++;
1222 age = session->age;
1223
1224 ctask = (struct iscsi_cmd_task *)sc->SCp.ptr;
1225 debug_scsi("aborting [sc %p itt 0x%x]\n", sc, ctask->itt);
Mike Christie7996a772006-04-06 21:13:41 -05001226
1227 /* ctask completed before time out */
Mike Christie7ea8b822006-07-24 15:47:22 -05001228 if (!ctask->sc) {
Mike Christie7ea8b822006-07-24 15:47:22 -05001229 debug_scsi("sc completed while abort in progress\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001230 goto success;
Mike Christie7ea8b822006-07-24 15:47:22 -05001231 }
Mike Christie7996a772006-04-06 21:13:41 -05001232
Mike Christie77a23c22007-05-30 12:57:18 -05001233 if (ctask->state == ISCSI_TASK_PENDING) {
1234 fail_command(conn, ctask, DID_ABORT << 16);
1235 goto success;
1236 }
Mike Christie7996a772006-04-06 21:13:41 -05001237
Mike Christie843c0a82007-12-13 12:43:20 -06001238 /* only have one tmf outstanding at a time */
1239 if (conn->tmf_state != TMF_INITIAL)
Mike Christie7996a772006-04-06 21:13:41 -05001240 goto failed;
Mike Christie843c0a82007-12-13 12:43:20 -06001241 conn->tmf_state = TMF_QUEUED;
Mike Christie7996a772006-04-06 21:13:41 -05001242
Mike Christie843c0a82007-12-13 12:43:20 -06001243 hdr = &conn->tmhdr;
1244 iscsi_prep_abort_task_pdu(ctask, hdr);
1245
1246 if (iscsi_exec_task_mgmt_fn(conn, hdr, age)) {
1247 rc = FAILED;
1248 goto failed;
1249 }
1250
1251 switch (conn->tmf_state) {
1252 case TMF_SUCCESS:
Mike Christie77a23c22007-05-30 12:57:18 -05001253 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05001254 iscsi_suspend_tx(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001255 /*
1256 * clean up task if aborted. grab the recv lock as a writer
1257 */
1258 write_lock_bh(conn->recv_lock);
1259 spin_lock(&session->lock);
1260 fail_command(conn, ctask, DID_ABORT << 16);
Mike Christie843c0a82007-12-13 12:43:20 -06001261 conn->tmf_state = TMF_INITIAL;
Mike Christie77a23c22007-05-30 12:57:18 -05001262 spin_unlock(&session->lock);
1263 write_unlock_bh(conn->recv_lock);
Mike Christie6724add2007-08-15 01:38:30 -05001264 iscsi_start_tx(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001265 goto success_unlocked;
Mike Christie843c0a82007-12-13 12:43:20 -06001266 case TMF_TIMEDOUT:
1267 spin_unlock_bh(&session->lock);
1268 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1269 goto failed_unlocked;
1270 case TMF_NOT_FOUND:
1271 if (!sc->SCp.ptr) {
1272 conn->tmf_state = TMF_INITIAL;
Mike Christie7ea8b822006-07-24 15:47:22 -05001273 /* ctask completed before tmf abort response */
Mike Christie7ea8b822006-07-24 15:47:22 -05001274 debug_scsi("sc completed while abort in progress\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001275 goto success;
Mike Christie7ea8b822006-07-24 15:47:22 -05001276 }
1277 /* fall through */
1278 default:
Mike Christie843c0a82007-12-13 12:43:20 -06001279 conn->tmf_state = TMF_INITIAL;
1280 goto failed;
Mike Christie7996a772006-04-06 21:13:41 -05001281 }
1282
Mike Christie77a23c22007-05-30 12:57:18 -05001283success:
Mike Christie7996a772006-04-06 21:13:41 -05001284 spin_unlock_bh(&session->lock);
Mike Christie77a23c22007-05-30 12:57:18 -05001285success_unlocked:
1286 debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
Mike Christie6724add2007-08-15 01:38:30 -05001287 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001288 return SUCCESS;
1289
1290failed:
1291 spin_unlock_bh(&session->lock);
Mike Christie77a23c22007-05-30 12:57:18 -05001292failed_unlocked:
Mike Christie843c0a82007-12-13 12:43:20 -06001293 debug_scsi("abort failed [sc %p itt 0x%x]\n", sc,
1294 ctask ? ctask->itt : 0);
Mike Christie6724add2007-08-15 01:38:30 -05001295 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001296 return FAILED;
1297}
1298EXPORT_SYMBOL_GPL(iscsi_eh_abort);
1299
Mike Christie843c0a82007-12-13 12:43:20 -06001300static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr)
1301{
1302 memset(hdr, 0, sizeof(*hdr));
1303 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
1304 hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK;
1305 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
1306 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
1307 hdr->rtt = ISCSI_RESERVED_TAG;
1308}
1309
1310int iscsi_eh_device_reset(struct scsi_cmnd *sc)
1311{
1312 struct Scsi_Host *host = sc->device->host;
1313 struct iscsi_session *session = iscsi_hostdata(host->hostdata);
1314 struct iscsi_conn *conn;
1315 struct iscsi_tm *hdr;
1316 int rc = FAILED;
1317
1318 debug_scsi("LU Reset [sc %p lun %u]\n", sc, sc->device->lun);
1319
1320 mutex_lock(&session->eh_mutex);
1321 spin_lock_bh(&session->lock);
1322 /*
1323 * Just check if we are not logged in. We cannot check for
1324 * the phase because the reset could come from a ioctl.
1325 */
1326 if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN)
1327 goto unlock;
1328 conn = session->leadconn;
1329
1330 /* only have one tmf outstanding at a time */
1331 if (conn->tmf_state != TMF_INITIAL)
1332 goto unlock;
1333 conn->tmf_state = TMF_QUEUED;
1334
1335 hdr = &conn->tmhdr;
1336 iscsi_prep_lun_reset_pdu(sc, hdr);
1337
1338 if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age)) {
1339 rc = FAILED;
1340 goto unlock;
1341 }
1342
1343 switch (conn->tmf_state) {
1344 case TMF_SUCCESS:
1345 break;
1346 case TMF_TIMEDOUT:
1347 spin_unlock_bh(&session->lock);
1348 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1349 goto done;
1350 default:
1351 conn->tmf_state = TMF_INITIAL;
1352 goto unlock;
1353 }
1354
1355 rc = SUCCESS;
1356 spin_unlock_bh(&session->lock);
1357
1358 iscsi_suspend_tx(conn);
1359 /* need to grab the recv lock then session lock */
1360 write_lock_bh(conn->recv_lock);
1361 spin_lock(&session->lock);
1362 fail_all_commands(conn, sc->device->lun);
1363 conn->tmf_state = TMF_INITIAL;
1364 spin_unlock(&session->lock);
1365 write_unlock_bh(conn->recv_lock);
1366
1367 iscsi_start_tx(conn);
1368 goto done;
1369
1370unlock:
1371 spin_unlock_bh(&session->lock);
1372done:
1373 debug_scsi("iscsi_eh_device_reset %s\n",
1374 rc == SUCCESS ? "SUCCESS" : "FAILED");
1375 mutex_unlock(&session->eh_mutex);
1376 return rc;
1377}
1378EXPORT_SYMBOL_GPL(iscsi_eh_device_reset);
1379
Mike Christie7996a772006-04-06 21:13:41 -05001380int
1381iscsi_pool_init(struct iscsi_queue *q, int max, void ***items, int item_size)
1382{
1383 int i;
1384
1385 *items = kmalloc(max * sizeof(void*), GFP_KERNEL);
1386 if (*items == NULL)
1387 return -ENOMEM;
1388
1389 q->max = max;
1390 q->pool = kmalloc(max * sizeof(void*), GFP_KERNEL);
1391 if (q->pool == NULL) {
1392 kfree(*items);
1393 return -ENOMEM;
1394 }
1395
1396 q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
1397 GFP_KERNEL, NULL);
1398 if (q->queue == ERR_PTR(-ENOMEM)) {
1399 kfree(q->pool);
1400 kfree(*items);
1401 return -ENOMEM;
1402 }
1403
1404 for (i = 0; i < max; i++) {
1405 q->pool[i] = kmalloc(item_size, GFP_KERNEL);
1406 if (q->pool[i] == NULL) {
1407 int j;
1408
1409 for (j = 0; j < i; j++)
1410 kfree(q->pool[j]);
1411
1412 kfifo_free(q->queue);
1413 kfree(q->pool);
1414 kfree(*items);
1415 return -ENOMEM;
1416 }
1417 memset(q->pool[i], 0, item_size);
1418 (*items)[i] = q->pool[i];
1419 __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
1420 }
1421 return 0;
1422}
1423EXPORT_SYMBOL_GPL(iscsi_pool_init);
1424
1425void iscsi_pool_free(struct iscsi_queue *q, void **items)
1426{
1427 int i;
1428
1429 for (i = 0; i < q->max; i++)
1430 kfree(items[i]);
1431 kfree(q->pool);
1432 kfree(items);
1433}
1434EXPORT_SYMBOL_GPL(iscsi_pool_free);
1435
1436/*
1437 * iSCSI Session's hostdata organization:
1438 *
1439 * *------------------* <== hostdata_session(host->hostdata)
1440 * | ptr to class sess|
1441 * |------------------| <== iscsi_hostdata(host->hostdata)
1442 * | iscsi_session |
1443 * *------------------*
1444 */
1445
1446#define hostdata_privsize(_sz) (sizeof(unsigned long) + _sz + \
1447 _sz % sizeof(unsigned long))
1448
1449#define hostdata_session(_hostdata) (iscsi_ptr(*(unsigned long *)_hostdata))
1450
1451/**
1452 * iscsi_session_setup - create iscsi cls session and host and session
1453 * @scsit: scsi transport template
1454 * @iscsit: iscsi transport template
Mike Christie15482712007-05-30 12:57:19 -05001455 * @cmds_max: scsi host can queue
1456 * @qdepth: scsi host cmds per lun
1457 * @cmd_task_size: LLD ctask private data size
1458 * @mgmt_task_size: LLD mtask private data size
Mike Christie7996a772006-04-06 21:13:41 -05001459 * @initial_cmdsn: initial CmdSN
1460 * @hostno: host no allocated
1461 *
1462 * This can be used by software iscsi_transports that allocate
1463 * a session per scsi host.
1464 **/
1465struct iscsi_cls_session *
1466iscsi_session_setup(struct iscsi_transport *iscsit,
1467 struct scsi_transport_template *scsit,
Mike Christie15482712007-05-30 12:57:19 -05001468 uint16_t cmds_max, uint16_t qdepth,
Mike Christie7996a772006-04-06 21:13:41 -05001469 int cmd_task_size, int mgmt_task_size,
1470 uint32_t initial_cmdsn, uint32_t *hostno)
1471{
1472 struct Scsi_Host *shost;
1473 struct iscsi_session *session;
1474 struct iscsi_cls_session *cls_session;
1475 int cmd_i;
1476
Mike Christie15482712007-05-30 12:57:19 -05001477 if (qdepth > ISCSI_MAX_CMD_PER_LUN || qdepth < 1) {
1478 if (qdepth != 0)
1479 printk(KERN_ERR "iscsi: invalid queue depth of %d. "
1480 "Queue depth must be between 1 and %d.\n",
1481 qdepth, ISCSI_MAX_CMD_PER_LUN);
1482 qdepth = ISCSI_DEF_CMD_PER_LUN;
1483 }
1484
1485 if (cmds_max < 2 || (cmds_max & (cmds_max - 1)) ||
1486 cmds_max >= ISCSI_MGMT_ITT_OFFSET) {
1487 if (cmds_max != 0)
1488 printk(KERN_ERR "iscsi: invalid can_queue of %d. "
1489 "can_queue must be a power of 2 and between "
1490 "2 and %d - setting to %d.\n", cmds_max,
1491 ISCSI_MGMT_ITT_OFFSET, ISCSI_DEF_XMIT_CMDS_MAX);
1492 cmds_max = ISCSI_DEF_XMIT_CMDS_MAX;
1493 }
1494
Mike Christie7996a772006-04-06 21:13:41 -05001495 shost = scsi_host_alloc(iscsit->host_template,
1496 hostdata_privsize(sizeof(*session)));
1497 if (!shost)
1498 return NULL;
1499
Mike Christie15482712007-05-30 12:57:19 -05001500 /* the iscsi layer takes one task for reserve */
1501 shost->can_queue = cmds_max - 1;
1502 shost->cmd_per_lun = qdepth;
Mike Christie7996a772006-04-06 21:13:41 -05001503 shost->max_id = 1;
1504 shost->max_channel = 0;
1505 shost->max_lun = iscsit->max_lun;
1506 shost->max_cmd_len = iscsit->max_cmd_len;
1507 shost->transportt = scsit;
1508 shost->transportt->create_work_queue = 1;
1509 *hostno = shost->host_no;
1510
1511 session = iscsi_hostdata(shost->hostdata);
1512 memset(session, 0, sizeof(struct iscsi_session));
1513 session->host = shost;
1514 session->state = ISCSI_STATE_FREE;
1515 session->mgmtpool_max = ISCSI_MGMT_CMDS_MAX;
Mike Christie15482712007-05-30 12:57:19 -05001516 session->cmds_max = cmds_max;
Mike Christiee0726402007-07-26 12:46:48 -05001517 session->queued_cmdsn = session->cmdsn = initial_cmdsn;
Mike Christie7996a772006-04-06 21:13:41 -05001518 session->exp_cmdsn = initial_cmdsn + 1;
1519 session->max_cmdsn = initial_cmdsn + 1;
1520 session->max_r2t = 1;
1521 session->tt = iscsit;
Mike Christie6724add2007-08-15 01:38:30 -05001522 mutex_init(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001523
1524 /* initialize SCSI PDU commands pool */
1525 if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
1526 (void***)&session->cmds,
1527 cmd_task_size + sizeof(struct iscsi_cmd_task)))
1528 goto cmdpool_alloc_fail;
1529
1530 /* pre-format cmds pool with ITT */
1531 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
1532 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
1533
1534 if (cmd_task_size)
1535 ctask->dd_data = &ctask[1];
1536 ctask->itt = cmd_i;
Mike Christieb6c395ed2006-07-24 15:47:15 -05001537 INIT_LIST_HEAD(&ctask->running);
Mike Christie7996a772006-04-06 21:13:41 -05001538 }
1539
1540 spin_lock_init(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001541
1542 /* initialize immediate command pool */
1543 if (iscsi_pool_init(&session->mgmtpool, session->mgmtpool_max,
1544 (void***)&session->mgmt_cmds,
1545 mgmt_task_size + sizeof(struct iscsi_mgmt_task)))
1546 goto mgmtpool_alloc_fail;
1547
1548
1549 /* pre-format immediate cmds pool with ITT */
1550 for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
1551 struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
1552
1553 if (mgmt_task_size)
1554 mtask->dd_data = &mtask[1];
1555 mtask->itt = ISCSI_MGMT_ITT_OFFSET + cmd_i;
Mike Christieb6c395ed2006-07-24 15:47:15 -05001556 INIT_LIST_HEAD(&mtask->running);
Mike Christie7996a772006-04-06 21:13:41 -05001557 }
1558
1559 if (scsi_add_host(shost, NULL))
1560 goto add_host_fail;
1561
Mike Christief53a88d2006-06-28 12:00:27 -05001562 if (!try_module_get(iscsit->owner))
1563 goto cls_session_fail;
1564
Mike Christie6a8a0d32006-06-28 12:00:31 -05001565 cls_session = iscsi_create_session(shost, iscsit, 0);
Mike Christie7996a772006-04-06 21:13:41 -05001566 if (!cls_session)
Mike Christief53a88d2006-06-28 12:00:27 -05001567 goto module_put;
Mike Christie7996a772006-04-06 21:13:41 -05001568 *(unsigned long*)shost->hostdata = (unsigned long)cls_session;
1569
1570 return cls_session;
1571
Mike Christief53a88d2006-06-28 12:00:27 -05001572module_put:
1573 module_put(iscsit->owner);
Mike Christie7996a772006-04-06 21:13:41 -05001574cls_session_fail:
1575 scsi_remove_host(shost);
1576add_host_fail:
Mike Christie7996a772006-04-06 21:13:41 -05001577 iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
1578mgmtpool_alloc_fail:
1579 iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
1580cmdpool_alloc_fail:
1581 scsi_host_put(shost);
1582 return NULL;
1583}
1584EXPORT_SYMBOL_GPL(iscsi_session_setup);
1585
1586/**
1587 * iscsi_session_teardown - destroy session, host, and cls_session
1588 * shost: scsi host
1589 *
1590 * This can be used by software iscsi_transports that allocate
1591 * a session per scsi host.
1592 **/
1593void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
1594{
1595 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
1596 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
Mike Christie63f75cc2006-07-24 15:47:29 -05001597 struct module *owner = cls_session->transport->owner;
Mike Christie7996a772006-04-06 21:13:41 -05001598
Mike Christie464bb992007-07-26 12:46:45 -05001599 iscsi_unblock_session(cls_session);
Mike Christie7996a772006-04-06 21:13:41 -05001600 scsi_remove_host(shost);
1601
Mike Christie7996a772006-04-06 21:13:41 -05001602 iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
1603 iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
1604
Mike Christieb2c64162007-05-30 12:57:16 -05001605 kfree(session->password);
1606 kfree(session->password_in);
1607 kfree(session->username);
1608 kfree(session->username_in);
Mike Christief3ff0c32006-07-24 15:47:50 -05001609 kfree(session->targetname);
Mike Christied8196ed2007-05-30 12:57:25 -05001610 kfree(session->netdev);
Mike Christie0801c242007-05-30 12:57:12 -05001611 kfree(session->hwaddress);
Mike Christie8ad57812007-05-30 12:57:13 -05001612 kfree(session->initiatorname);
Mike Christief3ff0c32006-07-24 15:47:50 -05001613
Mike Christie7996a772006-04-06 21:13:41 -05001614 iscsi_destroy_session(cls_session);
1615 scsi_host_put(shost);
Mike Christie63f75cc2006-07-24 15:47:29 -05001616 module_put(owner);
Mike Christie7996a772006-04-06 21:13:41 -05001617}
1618EXPORT_SYMBOL_GPL(iscsi_session_teardown);
1619
1620/**
1621 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
1622 * @cls_session: iscsi_cls_session
1623 * @conn_idx: cid
1624 **/
1625struct iscsi_cls_conn *
1626iscsi_conn_setup(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
1627{
1628 struct iscsi_session *session = class_to_transport_session(cls_session);
1629 struct iscsi_conn *conn;
1630 struct iscsi_cls_conn *cls_conn;
Mike Christied36ab6f2006-05-18 20:31:34 -05001631 char *data;
Mike Christie7996a772006-04-06 21:13:41 -05001632
1633 cls_conn = iscsi_create_conn(cls_session, conn_idx);
1634 if (!cls_conn)
1635 return NULL;
1636 conn = cls_conn->dd_data;
1637 memset(conn, 0, sizeof(*conn));
1638
1639 conn->session = session;
1640 conn->cls_conn = cls_conn;
1641 conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
1642 conn->id = conn_idx;
1643 conn->exp_statsn = 0;
Mike Christie843c0a82007-12-13 12:43:20 -06001644 conn->tmf_state = TMF_INITIAL;
Mike Christie7996a772006-04-06 21:13:41 -05001645 INIT_LIST_HEAD(&conn->run_list);
1646 INIT_LIST_HEAD(&conn->mgmt_run_list);
Mike Christie843c0a82007-12-13 12:43:20 -06001647 INIT_LIST_HEAD(&conn->mgmtqueue);
Mike Christieb6c395ed2006-07-24 15:47:15 -05001648 INIT_LIST_HEAD(&conn->xmitqueue);
Mike Christie843c0a82007-12-13 12:43:20 -06001649 INIT_LIST_HEAD(&conn->requeue);
David Howellsc4028952006-11-22 14:57:56 +00001650 INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
Mike Christie7996a772006-04-06 21:13:41 -05001651
1652 /* allocate login_mtask used for the login/text sequences */
1653 spin_lock_bh(&session->lock);
1654 if (!__kfifo_get(session->mgmtpool.queue,
1655 (void*)&conn->login_mtask,
1656 sizeof(void*))) {
1657 spin_unlock_bh(&session->lock);
1658 goto login_mtask_alloc_fail;
1659 }
1660 spin_unlock_bh(&session->lock);
1661
Mike Christiebf32ed32007-02-28 17:32:17 -06001662 data = kmalloc(ISCSI_DEF_MAX_RECV_SEG_LEN, GFP_KERNEL);
Mike Christied36ab6f2006-05-18 20:31:34 -05001663 if (!data)
1664 goto login_mtask_data_alloc_fail;
Mike Christiec8dc1e52006-07-24 15:47:39 -05001665 conn->login_mtask->data = conn->data = data;
Mike Christied36ab6f2006-05-18 20:31:34 -05001666
Mike Christie843c0a82007-12-13 12:43:20 -06001667 init_timer(&conn->tmf_timer);
Mike Christie7996a772006-04-06 21:13:41 -05001668 init_waitqueue_head(&conn->ehwait);
1669
1670 return cls_conn;
1671
Mike Christied36ab6f2006-05-18 20:31:34 -05001672login_mtask_data_alloc_fail:
1673 __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
1674 sizeof(void*));
Mike Christie7996a772006-04-06 21:13:41 -05001675login_mtask_alloc_fail:
Mike Christie7996a772006-04-06 21:13:41 -05001676 iscsi_destroy_conn(cls_conn);
1677 return NULL;
1678}
1679EXPORT_SYMBOL_GPL(iscsi_conn_setup);
1680
1681/**
1682 * iscsi_conn_teardown - teardown iscsi connection
1683 * cls_conn: iscsi class connection
1684 *
1685 * TODO: we may need to make this into a two step process
1686 * like scsi-mls remove + put host
1687 */
1688void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
1689{
1690 struct iscsi_conn *conn = cls_conn->dd_data;
1691 struct iscsi_session *session = conn->session;
1692 unsigned long flags;
1693
Mike Christie7996a772006-04-06 21:13:41 -05001694 spin_lock_bh(&session->lock);
1695 conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
1696 if (session->leadconn == conn) {
1697 /*
1698 * leading connection? then give up on recovery.
1699 */
1700 session->state = ISCSI_STATE_TERMINATE;
1701 wake_up(&conn->ehwait);
1702 }
1703 spin_unlock_bh(&session->lock);
1704
Mike Christie7996a772006-04-06 21:13:41 -05001705 /*
1706 * Block until all in-progress commands for this connection
1707 * time out or fail.
1708 */
1709 for (;;) {
1710 spin_lock_irqsave(session->host->host_lock, flags);
1711 if (!session->host->host_busy) { /* OK for ERL == 0 */
1712 spin_unlock_irqrestore(session->host->host_lock, flags);
1713 break;
1714 }
1715 spin_unlock_irqrestore(session->host->host_lock, flags);
1716 msleep_interruptible(500);
Or Gerlitzbe2df722006-05-02 19:46:43 -05001717 printk(KERN_INFO "iscsi: scsi conn_destroy(): host_busy %d "
1718 "host_failed %d\n", session->host->host_busy,
1719 session->host->host_failed);
Mike Christie7996a772006-04-06 21:13:41 -05001720 /*
1721 * force eh_abort() to unblock
1722 */
1723 wake_up(&conn->ehwait);
1724 }
1725
Mike Christie779ea122007-02-28 17:32:15 -06001726 /* flush queued up work because we free the connection below */
Mike Christie843c0a82007-12-13 12:43:20 -06001727 iscsi_suspend_tx(conn);
Mike Christie779ea122007-02-28 17:32:15 -06001728
Mike Christie7996a772006-04-06 21:13:41 -05001729 spin_lock_bh(&session->lock);
Mike Christiec8dc1e52006-07-24 15:47:39 -05001730 kfree(conn->data);
Mike Christief3ff0c32006-07-24 15:47:50 -05001731 kfree(conn->persistent_address);
Mike Christie7996a772006-04-06 21:13:41 -05001732 __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
1733 sizeof(void*));
Mike Christiee0726402007-07-26 12:46:48 -05001734 if (session->leadconn == conn)
Mike Christie7996a772006-04-06 21:13:41 -05001735 session->leadconn = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05001736 spin_unlock_bh(&session->lock);
1737
Mike Christie7996a772006-04-06 21:13:41 -05001738 iscsi_destroy_conn(cls_conn);
1739}
1740EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
1741
1742int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
1743{
1744 struct iscsi_conn *conn = cls_conn->dd_data;
1745 struct iscsi_session *session = conn->session;
1746
Mike Christieffd04362006-08-31 18:09:24 -04001747 if (!session) {
Mike Christie7996a772006-04-06 21:13:41 -05001748 printk(KERN_ERR "iscsi: can't start unbound connection\n");
1749 return -EPERM;
1750 }
1751
Mike Christiedb98ccd2006-08-31 18:09:31 -04001752 if ((session->imm_data_en || !session->initial_r2t_en) &&
1753 session->first_burst > session->max_burst) {
Mike Christieffd04362006-08-31 18:09:24 -04001754 printk("iscsi: invalid burst lengths: "
1755 "first_burst %d max_burst %d\n",
1756 session->first_burst, session->max_burst);
1757 return -EINVAL;
1758 }
1759
Mike Christie7996a772006-04-06 21:13:41 -05001760 spin_lock_bh(&session->lock);
1761 conn->c_stage = ISCSI_CONN_STARTED;
1762 session->state = ISCSI_STATE_LOGGED_IN;
Mike Christiee0726402007-07-26 12:46:48 -05001763 session->queued_cmdsn = session->cmdsn;
Mike Christie7996a772006-04-06 21:13:41 -05001764
1765 switch(conn->stop_stage) {
1766 case STOP_CONN_RECOVER:
1767 /*
1768 * unblock eh_abort() if it is blocked. re-try all
1769 * commands after successful recovery
1770 */
Mike Christie7996a772006-04-06 21:13:41 -05001771 conn->stop_stage = 0;
Mike Christie843c0a82007-12-13 12:43:20 -06001772 conn->tmf_state = TMF_INITIAL;
Mike Christie7996a772006-04-06 21:13:41 -05001773 session->age++;
Mike Christie7996a772006-04-06 21:13:41 -05001774 spin_unlock_bh(&session->lock);
1775
1776 iscsi_unblock_session(session_to_cls(session));
1777 wake_up(&conn->ehwait);
1778 return 0;
1779 case STOP_CONN_TERM:
Mike Christie7996a772006-04-06 21:13:41 -05001780 conn->stop_stage = 0;
1781 break;
Mike Christie7996a772006-04-06 21:13:41 -05001782 default:
1783 break;
1784 }
1785 spin_unlock_bh(&session->lock);
1786
1787 return 0;
1788}
1789EXPORT_SYMBOL_GPL(iscsi_conn_start);
1790
1791static void
1792flush_control_queues(struct iscsi_session *session, struct iscsi_conn *conn)
1793{
1794 struct iscsi_mgmt_task *mtask, *tmp;
1795
1796 /* handle pending */
Mike Christie843c0a82007-12-13 12:43:20 -06001797 list_for_each_entry_safe(mtask, tmp, &conn->mgmtqueue, running) {
1798 debug_scsi("flushing pending mgmt task itt 0x%x\n", mtask->itt);
1799 list_del_init(&mtask->running);
Mike Christie7996a772006-04-06 21:13:41 -05001800 if (mtask == conn->login_mtask)
1801 continue;
Mike Christie7996a772006-04-06 21:13:41 -05001802 __kfifo_put(session->mgmtpool.queue, (void*)&mtask,
1803 sizeof(void*));
1804 }
1805
1806 /* handle running */
1807 list_for_each_entry_safe(mtask, tmp, &conn->mgmt_run_list, running) {
1808 debug_scsi("flushing running mgmt task itt 0x%x\n", mtask->itt);
Mike Christie843c0a82007-12-13 12:43:20 -06001809 list_del_init(&mtask->running);
Mike Christieed2abc72006-05-02 19:46:40 -05001810
Mike Christie7996a772006-04-06 21:13:41 -05001811 if (mtask == conn->login_mtask)
1812 continue;
Mike Christieed2abc72006-05-02 19:46:40 -05001813 __kfifo_put(session->mgmtpool.queue, (void*)&mtask,
Mike Christie7996a772006-04-06 21:13:41 -05001814 sizeof(void*));
1815 }
1816
1817 conn->mtask = NULL;
1818}
1819
Mike Christie656cffc2006-05-18 20:31:42 -05001820static void iscsi_start_session_recovery(struct iscsi_session *session,
1821 struct iscsi_conn *conn, int flag)
Mike Christie7996a772006-04-06 21:13:41 -05001822{
Mike Christieed2abc72006-05-02 19:46:40 -05001823 int old_stop_stage;
1824
Mike Christie6724add2007-08-15 01:38:30 -05001825 mutex_lock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001826 spin_lock_bh(&session->lock);
Mike Christieed2abc72006-05-02 19:46:40 -05001827 if (conn->stop_stage == STOP_CONN_TERM) {
Mike Christie7996a772006-04-06 21:13:41 -05001828 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05001829 mutex_unlock(&session->eh_mutex);
1830 return;
1831 }
1832
1833 /*
1834 * The LLD either freed/unset the lock on us, or userspace called
1835 * stop but did not create a proper connection (connection was never
1836 * bound or it was unbound then stop was called).
1837 */
1838 if (!conn->recv_lock) {
1839 spin_unlock_bh(&session->lock);
1840 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001841 return;
1842 }
Mike Christieed2abc72006-05-02 19:46:40 -05001843
1844 /*
1845 * When this is called for the in_login state, we only want to clean
Mike Christie67a61112006-05-30 00:37:20 -05001846 * up the login task and connection. We do not need to block and set
1847 * the recovery state again
Mike Christieed2abc72006-05-02 19:46:40 -05001848 */
Mike Christie67a61112006-05-30 00:37:20 -05001849 if (flag == STOP_CONN_TERM)
1850 session->state = ISCSI_STATE_TERMINATE;
1851 else if (conn->stop_stage != STOP_CONN_RECOVER)
1852 session->state = ISCSI_STATE_IN_RECOVERY;
Mike Christieed2abc72006-05-02 19:46:40 -05001853
1854 old_stop_stage = conn->stop_stage;
Mike Christie7996a772006-04-06 21:13:41 -05001855 conn->stop_stage = flag;
Mike Christie67a61112006-05-30 00:37:20 -05001856 conn->c_stage = ISCSI_CONN_STOPPED;
Mike Christie7996a772006-04-06 21:13:41 -05001857 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05001858
1859 iscsi_suspend_tx(conn);
Mike Christie7996a772006-04-06 21:13:41 -05001860
Mike Christie1c834692006-07-24 15:47:26 -05001861 write_lock_bh(conn->recv_lock);
1862 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
1863 write_unlock_bh(conn->recv_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001864
Mike Christie7996a772006-04-06 21:13:41 -05001865 /*
1866 * for connection level recovery we should not calculate
1867 * header digest. conn->hdr_size used for optimization
1868 * in hdr_extract() and will be re-negotiated at
1869 * set_param() time.
1870 */
1871 if (flag == STOP_CONN_RECOVER) {
1872 conn->hdrdgst_en = 0;
1873 conn->datadgst_en = 0;
Mike Christie656cffc2006-05-18 20:31:42 -05001874 if (session->state == ISCSI_STATE_IN_RECOVERY &&
Mike Christie67a61112006-05-30 00:37:20 -05001875 old_stop_stage != STOP_CONN_RECOVER) {
1876 debug_scsi("blocking session\n");
Mike Christie7996a772006-04-06 21:13:41 -05001877 iscsi_block_session(session_to_cls(session));
Mike Christie67a61112006-05-30 00:37:20 -05001878 }
Mike Christie7996a772006-04-06 21:13:41 -05001879 }
Mike Christie656cffc2006-05-18 20:31:42 -05001880
Mike Christie656cffc2006-05-18 20:31:42 -05001881 /*
1882 * flush queues.
1883 */
1884 spin_lock_bh(&session->lock);
Mike Christie843c0a82007-12-13 12:43:20 -06001885 fail_all_commands(conn, -1);
Mike Christie656cffc2006-05-18 20:31:42 -05001886 flush_control_queues(session, conn);
1887 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05001888 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001889}
Mike Christie7996a772006-04-06 21:13:41 -05001890
1891void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
1892{
1893 struct iscsi_conn *conn = cls_conn->dd_data;
1894 struct iscsi_session *session = conn->session;
1895
1896 switch (flag) {
1897 case STOP_CONN_RECOVER:
1898 case STOP_CONN_TERM:
1899 iscsi_start_session_recovery(session, conn, flag);
Mike Christie8d2860b2006-05-02 19:46:47 -05001900 break;
Mike Christie7996a772006-04-06 21:13:41 -05001901 default:
Or Gerlitzbe2df722006-05-02 19:46:43 -05001902 printk(KERN_ERR "iscsi: invalid stop flag %d\n", flag);
Mike Christie7996a772006-04-06 21:13:41 -05001903 }
1904}
1905EXPORT_SYMBOL_GPL(iscsi_conn_stop);
1906
1907int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
1908 struct iscsi_cls_conn *cls_conn, int is_leading)
1909{
1910 struct iscsi_session *session = class_to_transport_session(cls_session);
Mike Christie98644042006-10-16 18:09:39 -04001911 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05001912
Mike Christie7996a772006-04-06 21:13:41 -05001913 spin_lock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001914 if (is_leading)
1915 session->leadconn = conn;
Mike Christie98644042006-10-16 18:09:39 -04001916 spin_unlock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001917
1918 /*
1919 * Unblock xmitworker(), Login Phase will pass through.
1920 */
1921 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
1922 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1923 return 0;
1924}
1925EXPORT_SYMBOL_GPL(iscsi_conn_bind);
1926
Mike Christiea54a52c2006-06-28 12:00:23 -05001927
1928int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
1929 enum iscsi_param param, char *buf, int buflen)
1930{
1931 struct iscsi_conn *conn = cls_conn->dd_data;
1932 struct iscsi_session *session = conn->session;
1933 uint32_t value;
1934
1935 switch(param) {
Mike Christie843c0a82007-12-13 12:43:20 -06001936 case ISCSI_PARAM_FAST_ABORT:
1937 sscanf(buf, "%d", &session->fast_abort);
1938 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05001939 case ISCSI_PARAM_MAX_RECV_DLENGTH:
1940 sscanf(buf, "%d", &conn->max_recv_dlength);
1941 break;
1942 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
1943 sscanf(buf, "%d", &conn->max_xmit_dlength);
1944 break;
1945 case ISCSI_PARAM_HDRDGST_EN:
1946 sscanf(buf, "%d", &conn->hdrdgst_en);
1947 break;
1948 case ISCSI_PARAM_DATADGST_EN:
1949 sscanf(buf, "%d", &conn->datadgst_en);
1950 break;
1951 case ISCSI_PARAM_INITIAL_R2T_EN:
1952 sscanf(buf, "%d", &session->initial_r2t_en);
1953 break;
1954 case ISCSI_PARAM_MAX_R2T:
1955 sscanf(buf, "%d", &session->max_r2t);
1956 break;
1957 case ISCSI_PARAM_IMM_DATA_EN:
1958 sscanf(buf, "%d", &session->imm_data_en);
1959 break;
1960 case ISCSI_PARAM_FIRST_BURST:
1961 sscanf(buf, "%d", &session->first_burst);
1962 break;
1963 case ISCSI_PARAM_MAX_BURST:
1964 sscanf(buf, "%d", &session->max_burst);
1965 break;
1966 case ISCSI_PARAM_PDU_INORDER_EN:
1967 sscanf(buf, "%d", &session->pdu_inorder_en);
1968 break;
1969 case ISCSI_PARAM_DATASEQ_INORDER_EN:
1970 sscanf(buf, "%d", &session->dataseq_inorder_en);
1971 break;
1972 case ISCSI_PARAM_ERL:
1973 sscanf(buf, "%d", &session->erl);
1974 break;
1975 case ISCSI_PARAM_IFMARKER_EN:
1976 sscanf(buf, "%d", &value);
1977 BUG_ON(value);
1978 break;
1979 case ISCSI_PARAM_OFMARKER_EN:
1980 sscanf(buf, "%d", &value);
1981 BUG_ON(value);
1982 break;
1983 case ISCSI_PARAM_EXP_STATSN:
1984 sscanf(buf, "%u", &conn->exp_statsn);
1985 break;
Mike Christieb2c64162007-05-30 12:57:16 -05001986 case ISCSI_PARAM_USERNAME:
1987 kfree(session->username);
1988 session->username = kstrdup(buf, GFP_KERNEL);
1989 if (!session->username)
1990 return -ENOMEM;
1991 break;
1992 case ISCSI_PARAM_USERNAME_IN:
1993 kfree(session->username_in);
1994 session->username_in = kstrdup(buf, GFP_KERNEL);
1995 if (!session->username_in)
1996 return -ENOMEM;
1997 break;
1998 case ISCSI_PARAM_PASSWORD:
1999 kfree(session->password);
2000 session->password = kstrdup(buf, GFP_KERNEL);
2001 if (!session->password)
2002 return -ENOMEM;
2003 break;
2004 case ISCSI_PARAM_PASSWORD_IN:
2005 kfree(session->password_in);
2006 session->password_in = kstrdup(buf, GFP_KERNEL);
2007 if (!session->password_in)
2008 return -ENOMEM;
2009 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002010 case ISCSI_PARAM_TARGET_NAME:
2011 /* this should not change between logins */
2012 if (session->targetname)
2013 break;
2014
2015 session->targetname = kstrdup(buf, GFP_KERNEL);
2016 if (!session->targetname)
2017 return -ENOMEM;
2018 break;
2019 case ISCSI_PARAM_TPGT:
2020 sscanf(buf, "%d", &session->tpgt);
2021 break;
2022 case ISCSI_PARAM_PERSISTENT_PORT:
2023 sscanf(buf, "%d", &conn->persistent_port);
2024 break;
2025 case ISCSI_PARAM_PERSISTENT_ADDRESS:
2026 /*
2027 * this is the address returned in discovery so it should
2028 * not change between logins.
2029 */
2030 if (conn->persistent_address)
2031 break;
2032
2033 conn->persistent_address = kstrdup(buf, GFP_KERNEL);
2034 if (!conn->persistent_address)
2035 return -ENOMEM;
2036 break;
2037 default:
2038 return -ENOSYS;
2039 }
2040
2041 return 0;
2042}
2043EXPORT_SYMBOL_GPL(iscsi_set_param);
2044
2045int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
2046 enum iscsi_param param, char *buf)
2047{
2048 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
2049 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
2050 int len;
2051
2052 switch(param) {
Mike Christie843c0a82007-12-13 12:43:20 -06002053 case ISCSI_PARAM_FAST_ABORT:
2054 len = sprintf(buf, "%d\n", session->fast_abort);
2055 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002056 case ISCSI_PARAM_INITIAL_R2T_EN:
2057 len = sprintf(buf, "%d\n", session->initial_r2t_en);
2058 break;
2059 case ISCSI_PARAM_MAX_R2T:
2060 len = sprintf(buf, "%hu\n", session->max_r2t);
2061 break;
2062 case ISCSI_PARAM_IMM_DATA_EN:
2063 len = sprintf(buf, "%d\n", session->imm_data_en);
2064 break;
2065 case ISCSI_PARAM_FIRST_BURST:
2066 len = sprintf(buf, "%u\n", session->first_burst);
2067 break;
2068 case ISCSI_PARAM_MAX_BURST:
2069 len = sprintf(buf, "%u\n", session->max_burst);
2070 break;
2071 case ISCSI_PARAM_PDU_INORDER_EN:
2072 len = sprintf(buf, "%d\n", session->pdu_inorder_en);
2073 break;
2074 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2075 len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
2076 break;
2077 case ISCSI_PARAM_ERL:
2078 len = sprintf(buf, "%d\n", session->erl);
2079 break;
2080 case ISCSI_PARAM_TARGET_NAME:
2081 len = sprintf(buf, "%s\n", session->targetname);
2082 break;
2083 case ISCSI_PARAM_TPGT:
2084 len = sprintf(buf, "%d\n", session->tpgt);
2085 break;
Mike Christieb2c64162007-05-30 12:57:16 -05002086 case ISCSI_PARAM_USERNAME:
2087 len = sprintf(buf, "%s\n", session->username);
2088 break;
2089 case ISCSI_PARAM_USERNAME_IN:
2090 len = sprintf(buf, "%s\n", session->username_in);
2091 break;
2092 case ISCSI_PARAM_PASSWORD:
2093 len = sprintf(buf, "%s\n", session->password);
2094 break;
2095 case ISCSI_PARAM_PASSWORD_IN:
2096 len = sprintf(buf, "%s\n", session->password_in);
2097 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002098 default:
2099 return -ENOSYS;
2100 }
2101
2102 return len;
2103}
2104EXPORT_SYMBOL_GPL(iscsi_session_get_param);
2105
2106int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
2107 enum iscsi_param param, char *buf)
2108{
2109 struct iscsi_conn *conn = cls_conn->dd_data;
2110 int len;
2111
2112 switch(param) {
2113 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2114 len = sprintf(buf, "%u\n", conn->max_recv_dlength);
2115 break;
2116 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2117 len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
2118 break;
2119 case ISCSI_PARAM_HDRDGST_EN:
2120 len = sprintf(buf, "%d\n", conn->hdrdgst_en);
2121 break;
2122 case ISCSI_PARAM_DATADGST_EN:
2123 len = sprintf(buf, "%d\n", conn->datadgst_en);
2124 break;
2125 case ISCSI_PARAM_IFMARKER_EN:
2126 len = sprintf(buf, "%d\n", conn->ifmarker_en);
2127 break;
2128 case ISCSI_PARAM_OFMARKER_EN:
2129 len = sprintf(buf, "%d\n", conn->ofmarker_en);
2130 break;
2131 case ISCSI_PARAM_EXP_STATSN:
2132 len = sprintf(buf, "%u\n", conn->exp_statsn);
2133 break;
2134 case ISCSI_PARAM_PERSISTENT_PORT:
2135 len = sprintf(buf, "%d\n", conn->persistent_port);
2136 break;
2137 case ISCSI_PARAM_PERSISTENT_ADDRESS:
2138 len = sprintf(buf, "%s\n", conn->persistent_address);
2139 break;
2140 default:
2141 return -ENOSYS;
2142 }
2143
2144 return len;
2145}
2146EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
2147
Mike Christie0801c242007-05-30 12:57:12 -05002148int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2149 char *buf)
2150{
2151 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
2152 int len;
2153
2154 switch (param) {
Mike Christied8196ed2007-05-30 12:57:25 -05002155 case ISCSI_HOST_PARAM_NETDEV_NAME:
2156 if (!session->netdev)
2157 len = sprintf(buf, "%s\n", "default");
2158 else
2159 len = sprintf(buf, "%s\n", session->netdev);
2160 break;
Mike Christie0801c242007-05-30 12:57:12 -05002161 case ISCSI_HOST_PARAM_HWADDRESS:
2162 if (!session->hwaddress)
2163 len = sprintf(buf, "%s\n", "default");
2164 else
2165 len = sprintf(buf, "%s\n", session->hwaddress);
2166 break;
Mike Christie8ad57812007-05-30 12:57:13 -05002167 case ISCSI_HOST_PARAM_INITIATOR_NAME:
2168 if (!session->initiatorname)
2169 len = sprintf(buf, "%s\n", "unknown");
2170 else
2171 len = sprintf(buf, "%s\n", session->initiatorname);
2172 break;
2173
Mike Christie0801c242007-05-30 12:57:12 -05002174 default:
2175 return -ENOSYS;
2176 }
2177
2178 return len;
2179}
2180EXPORT_SYMBOL_GPL(iscsi_host_get_param);
2181
2182int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2183 char *buf, int buflen)
2184{
2185 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
2186
2187 switch (param) {
Mike Christied8196ed2007-05-30 12:57:25 -05002188 case ISCSI_HOST_PARAM_NETDEV_NAME:
2189 if (!session->netdev)
2190 session->netdev = kstrdup(buf, GFP_KERNEL);
2191 break;
Mike Christie0801c242007-05-30 12:57:12 -05002192 case ISCSI_HOST_PARAM_HWADDRESS:
2193 if (!session->hwaddress)
2194 session->hwaddress = kstrdup(buf, GFP_KERNEL);
2195 break;
Mike Christie8ad57812007-05-30 12:57:13 -05002196 case ISCSI_HOST_PARAM_INITIATOR_NAME:
2197 if (!session->initiatorname)
2198 session->initiatorname = kstrdup(buf, GFP_KERNEL);
2199 break;
Mike Christie0801c242007-05-30 12:57:12 -05002200 default:
2201 return -ENOSYS;
2202 }
2203
2204 return 0;
2205}
2206EXPORT_SYMBOL_GPL(iscsi_host_set_param);
2207
Mike Christie7996a772006-04-06 21:13:41 -05002208MODULE_AUTHOR("Mike Christie");
2209MODULE_DESCRIPTION("iSCSI library functions");
2210MODULE_LICENSE("GPL");