blob: efceed451b466245d847f59e4da02b7c141be70a [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) ||
89 __kfifo_len(session->leadconn->mgmtqueue))
90 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 Christieb6c395e2006-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
294 if (sc->sc_data_direction == DMA_TO_DEVICE)
295 goto out;
296
297 if (rhdr->flags & ISCSI_FLAG_CMD_UNDERFLOW) {
298 int res_count = be32_to_cpu(rhdr->residual_count);
299
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900300 if (res_count > 0 && res_count <= scsi_bufflen(sc))
301 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;
304 } else if (rhdr->flags & ISCSI_FLAG_CMD_BIDI_UNDERFLOW)
305 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
306 else if (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW)
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900307 scsi_set_resid(sc, be32_to_cpu(rhdr->residual_count));
Mike Christie7996a772006-04-06 21:13:41 -0500308
309out:
310 debug_scsi("done [sc %lx res %d itt 0x%x]\n",
311 (long)sc, sc->result, ctask->itt);
312 conn->scsirsp_pdus_cnt++;
313
Mike Christie60ecebf2006-08-31 18:09:25 -0400314 __iscsi_put_ctask(ctask);
Mike Christie7996a772006-04-06 21:13:41 -0500315}
316
Mike Christie7ea8b822006-07-24 15:47:22 -0500317static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
318{
319 struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
320
321 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
322 conn->tmfrsp_pdus_cnt++;
323
324 if (conn->tmabort_state != TMABORT_INITIAL)
325 return;
326
327 if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
328 conn->tmabort_state = TMABORT_SUCCESS;
329 else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
330 conn->tmabort_state = TMABORT_NOT_FOUND;
331 else
332 conn->tmabort_state = TMABORT_FAILED;
333 wake_up(&conn->ehwait);
334}
335
Mike Christie62f38302006-08-31 18:09:27 -0400336static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
337 char *data, int datalen)
338{
339 struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
340 struct iscsi_hdr rejected_pdu;
341 uint32_t itt;
342
343 conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
344
345 if (reject->reason == ISCSI_REASON_DATA_DIGEST_ERROR) {
346 if (ntoh24(reject->dlength) > datalen)
347 return ISCSI_ERR_PROTO;
348
349 if (ntoh24(reject->dlength) >= sizeof(struct iscsi_hdr)) {
350 memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
Al Virob4377352007-02-09 16:39:40 +0000351 itt = get_itt(rejected_pdu.itt);
Mike Christie62f38302006-08-31 18:09:27 -0400352 printk(KERN_ERR "itt 0x%x had pdu (op 0x%x) rejected "
353 "due to DataDigest error.\n", itt,
354 rejected_pdu.opcode);
355 }
356 }
357 return 0;
358}
359
Mike Christie7996a772006-04-06 21:13:41 -0500360/**
361 * __iscsi_complete_pdu - complete pdu
362 * @conn: iscsi conn
363 * @hdr: iscsi header
364 * @data: data buffer
365 * @datalen: len of data buffer
366 *
367 * Completes pdu processing by freeing any resources allocated at
368 * queuecommand or send generic. session lock must be held and verify
369 * itt must have been called.
370 */
371int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
372 char *data, int datalen)
373{
374 struct iscsi_session *session = conn->session;
375 int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
376 struct iscsi_cmd_task *ctask;
377 struct iscsi_mgmt_task *mtask;
378 uint32_t itt;
379
Al Virob4377352007-02-09 16:39:40 +0000380 if (hdr->itt != RESERVED_ITT)
381 itt = get_itt(hdr->itt);
Mike Christie7996a772006-04-06 21:13:41 -0500382 else
Al Virob4377352007-02-09 16:39:40 +0000383 itt = ~0U;
Mike Christie7996a772006-04-06 21:13:41 -0500384
385 if (itt < session->cmds_max) {
386 ctask = session->cmds[itt];
387
388 debug_scsi("cmdrsp [op 0x%x cid %d itt 0x%x len %d]\n",
389 opcode, conn->id, ctask->itt, datalen);
390
391 switch(opcode) {
392 case ISCSI_OP_SCSI_CMD_RSP:
393 BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
Mike Christie77a23c22007-05-30 12:57:18 -0500394 iscsi_scsi_cmd_rsp(conn, hdr, ctask, data,
395 datalen);
Mike Christie7996a772006-04-06 21:13:41 -0500396 break;
397 case ISCSI_OP_SCSI_DATA_IN:
398 BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
399 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
400 conn->scsirsp_pdus_cnt++;
Mike Christie60ecebf2006-08-31 18:09:25 -0400401 __iscsi_put_ctask(ctask);
Mike Christie7996a772006-04-06 21:13:41 -0500402 }
403 break;
404 case ISCSI_OP_R2T:
405 /* LLD handles this for now */
406 break;
407 default:
408 rc = ISCSI_ERR_BAD_OPCODE;
409 break;
410 }
411 } else if (itt >= ISCSI_MGMT_ITT_OFFSET &&
412 itt < ISCSI_MGMT_ITT_OFFSET + session->mgmtpool_max) {
413 mtask = session->mgmt_cmds[itt - ISCSI_MGMT_ITT_OFFSET];
414
415 debug_scsi("immrsp [op 0x%x cid %d itt 0x%x len %d]\n",
416 opcode, conn->id, mtask->itt, datalen);
417
Mike Christie77a23c22007-05-30 12:57:18 -0500418 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
Mike Christie7996a772006-04-06 21:13:41 -0500419 switch(opcode) {
Mike Christie8d2860b2006-05-02 19:46:47 -0500420 case ISCSI_OP_LOGOUT_RSP:
Mike Christiec8dc1e52006-07-24 15:47:39 -0500421 if (datalen) {
422 rc = ISCSI_ERR_PROTO;
423 break;
424 }
Mike Christie8d2860b2006-05-02 19:46:47 -0500425 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
426 /* fall through */
Mike Christie7996a772006-04-06 21:13:41 -0500427 case ISCSI_OP_LOGIN_RSP:
428 case ISCSI_OP_TEXT_RSP:
Mike Christie8d2860b2006-05-02 19:46:47 -0500429 /*
430 * login related PDU's exp_statsn is handled in
431 * userspace
432 */
Mike Christie40527af2006-07-24 15:47:45 -0500433 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
434 rc = ISCSI_ERR_CONN_FAILED;
Mike Christie7996a772006-04-06 21:13:41 -0500435 list_del(&mtask->running);
436 if (conn->login_mtask != mtask)
437 __kfifo_put(session->mgmtpool.queue,
438 (void*)&mtask, sizeof(void*));
439 break;
440 case ISCSI_OP_SCSI_TMFUNC_RSP:
Mike Christie7996a772006-04-06 21:13:41 -0500441 if (datalen) {
442 rc = ISCSI_ERR_PROTO;
443 break;
444 }
Mike Christie8d2860b2006-05-02 19:46:47 -0500445
Mike Christie7ea8b822006-07-24 15:47:22 -0500446 iscsi_tmf_rsp(conn, hdr);
Mike Christie7996a772006-04-06 21:13:41 -0500447 break;
448 case ISCSI_OP_NOOP_IN:
Al Virob4377352007-02-09 16:39:40 +0000449 if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) || datalen) {
Mike Christie7996a772006-04-06 21:13:41 -0500450 rc = ISCSI_ERR_PROTO;
451 break;
452 }
Mike Christie7996a772006-04-06 21:13:41 -0500453 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
454
Mike Christie40527af2006-07-24 15:47:45 -0500455 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
456 rc = ISCSI_ERR_CONN_FAILED;
Mike Christie7996a772006-04-06 21:13:41 -0500457 list_del(&mtask->running);
458 if (conn->login_mtask != mtask)
459 __kfifo_put(session->mgmtpool.queue,
460 (void*)&mtask, sizeof(void*));
461 break;
462 default:
463 rc = ISCSI_ERR_BAD_OPCODE;
464 break;
465 }
Al Virob4377352007-02-09 16:39:40 +0000466 } else if (itt == ~0U) {
Mike Christie77a23c22007-05-30 12:57:18 -0500467 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
Mike Christie62f38302006-08-31 18:09:27 -0400468
Mike Christie7996a772006-04-06 21:13:41 -0500469 switch(opcode) {
470 case ISCSI_OP_NOOP_IN:
Mike Christie40527af2006-07-24 15:47:45 -0500471 if (datalen) {
Mike Christie7996a772006-04-06 21:13:41 -0500472 rc = ISCSI_ERR_PROTO;
Mike Christie40527af2006-07-24 15:47:45 -0500473 break;
474 }
475
Al Virob4377352007-02-09 16:39:40 +0000476 if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
Mike Christie40527af2006-07-24 15:47:45 -0500477 break;
478
479 if (iscsi_recv_pdu(conn->cls_conn, hdr, NULL, 0))
480 rc = ISCSI_ERR_CONN_FAILED;
Mike Christie7996a772006-04-06 21:13:41 -0500481 break;
482 case ISCSI_OP_REJECT:
Mike Christie62f38302006-08-31 18:09:27 -0400483 rc = iscsi_handle_reject(conn, hdr, data, datalen);
484 break;
Mike Christie7996a772006-04-06 21:13:41 -0500485 case ISCSI_OP_ASYNC_EVENT:
Mike Christie8d2860b2006-05-02 19:46:47 -0500486 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
Mike Christie5831c732006-10-16 18:09:41 -0400487 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
488 rc = ISCSI_ERR_CONN_FAILED;
Mike Christie7996a772006-04-06 21:13:41 -0500489 break;
490 default:
491 rc = ISCSI_ERR_BAD_OPCODE;
492 break;
493 }
494 } else
495 rc = ISCSI_ERR_BAD_ITT;
496
497 return rc;
498}
499EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
500
501int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
502 char *data, int datalen)
503{
504 int rc;
505
506 spin_lock(&conn->session->lock);
507 rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
508 spin_unlock(&conn->session->lock);
509 return rc;
510}
511EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
512
513/* verify itt (itt encoding: age+cid+itt) */
514int iscsi_verify_itt(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
515 uint32_t *ret_itt)
516{
517 struct iscsi_session *session = conn->session;
518 struct iscsi_cmd_task *ctask;
519 uint32_t itt;
520
Al Virob4377352007-02-09 16:39:40 +0000521 if (hdr->itt != RESERVED_ITT) {
522 if (((__force u32)hdr->itt & ISCSI_AGE_MASK) !=
Mike Christie7996a772006-04-06 21:13:41 -0500523 (session->age << ISCSI_AGE_SHIFT)) {
Or Gerlitzbe2df722006-05-02 19:46:43 -0500524 printk(KERN_ERR "iscsi: received itt %x expected "
Al Virob4377352007-02-09 16:39:40 +0000525 "session age (%x)\n", (__force u32)hdr->itt,
Mike Christie7996a772006-04-06 21:13:41 -0500526 session->age & ISCSI_AGE_MASK);
527 return ISCSI_ERR_BAD_ITT;
528 }
529
Al Virob4377352007-02-09 16:39:40 +0000530 if (((__force u32)hdr->itt & ISCSI_CID_MASK) !=
Mike Christie7996a772006-04-06 21:13:41 -0500531 (conn->id << ISCSI_CID_SHIFT)) {
Or Gerlitzbe2df722006-05-02 19:46:43 -0500532 printk(KERN_ERR "iscsi: received itt %x, expected "
Al Virob4377352007-02-09 16:39:40 +0000533 "CID (%x)\n", (__force u32)hdr->itt, conn->id);
Mike Christie7996a772006-04-06 21:13:41 -0500534 return ISCSI_ERR_BAD_ITT;
535 }
Al Virob4377352007-02-09 16:39:40 +0000536 itt = get_itt(hdr->itt);
Mike Christie7996a772006-04-06 21:13:41 -0500537 } else
Al Virob4377352007-02-09 16:39:40 +0000538 itt = ~0U;
Mike Christie7996a772006-04-06 21:13:41 -0500539
540 if (itt < session->cmds_max) {
541 ctask = session->cmds[itt];
542
543 if (!ctask->sc) {
Or Gerlitzbe2df722006-05-02 19:46:43 -0500544 printk(KERN_INFO "iscsi: dropping ctask with "
Mike Christie7996a772006-04-06 21:13:41 -0500545 "itt 0x%x\n", ctask->itt);
546 /* force drop */
547 return ISCSI_ERR_NO_SCSI_CMD;
548 }
549
550 if (ctask->sc->SCp.phase != session->age) {
Or Gerlitzbe2df722006-05-02 19:46:43 -0500551 printk(KERN_ERR "iscsi: ctask's session age %d, "
Mike Christie7996a772006-04-06 21:13:41 -0500552 "expected %d\n", ctask->sc->SCp.phase,
553 session->age);
554 return ISCSI_ERR_SESSION_FAILED;
555 }
556 }
557
558 *ret_itt = itt;
559 return 0;
560}
561EXPORT_SYMBOL_GPL(iscsi_verify_itt);
562
563void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
564{
565 struct iscsi_session *session = conn->session;
566 unsigned long flags;
567
568 spin_lock_irqsave(&session->lock, flags);
Mike Christie656cffc2006-05-18 20:31:42 -0500569 if (session->state == ISCSI_STATE_FAILED) {
570 spin_unlock_irqrestore(&session->lock, flags);
571 return;
572 }
573
Mike Christie67a61112006-05-30 00:37:20 -0500574 if (conn->stop_stage == 0)
Mike Christie7996a772006-04-06 21:13:41 -0500575 session->state = ISCSI_STATE_FAILED;
576 spin_unlock_irqrestore(&session->lock, flags);
577 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
578 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
579 iscsi_conn_error(conn->cls_conn, err);
580}
581EXPORT_SYMBOL_GPL(iscsi_conn_failure);
582
Mike Christie77a23c22007-05-30 12:57:18 -0500583static void iscsi_prep_mtask(struct iscsi_conn *conn,
584 struct iscsi_mgmt_task *mtask)
585{
586 struct iscsi_session *session = conn->session;
587 struct iscsi_hdr *hdr = mtask->hdr;
588 struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
589
590 if (hdr->opcode != (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) &&
591 hdr->opcode != (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
592 nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
593 /*
594 * pre-format CmdSN for outgoing PDU.
595 */
596 nop->cmdsn = cpu_to_be32(session->cmdsn);
597 if (hdr->itt != RESERVED_ITT) {
598 hdr->itt = build_itt(mtask->itt, conn->id, session->age);
Mike Christiee0726402007-07-26 12:46:48 -0500599 /*
600 * TODO: We always use immediate, so we never hit this.
601 * If we start to send tmfs or nops as non-immediate then
602 * we should start checking the cmdsn numbers for mgmt tasks.
603 */
Mike Christie77a23c22007-05-30 12:57:18 -0500604 if (conn->c_stage == ISCSI_CONN_STARTED &&
Mike Christiee0726402007-07-26 12:46:48 -0500605 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
606 session->queued_cmdsn++;
Mike Christie77a23c22007-05-30 12:57:18 -0500607 session->cmdsn++;
Mike Christiee0726402007-07-26 12:46:48 -0500608 }
Mike Christie77a23c22007-05-30 12:57:18 -0500609 }
610
611 if (session->tt->init_mgmt_task)
612 session->tt->init_mgmt_task(conn, mtask);
613
614 debug_scsi("mgmtpdu [op 0x%x hdr->itt 0x%x datalen %d]\n",
615 hdr->opcode, hdr->itt, mtask->data_count);
616}
617
Mike Christie05db8882007-02-28 17:32:16 -0600618static int iscsi_xmit_mtask(struct iscsi_conn *conn)
Mike Christieb5072ea2006-10-16 18:09:42 -0400619{
620 struct iscsi_hdr *hdr = conn->mtask->hdr;
621 int rc, was_logout = 0;
622
Mike Christie77a23c22007-05-30 12:57:18 -0500623 spin_unlock_bh(&conn->session->lock);
Mike Christieb5072ea2006-10-16 18:09:42 -0400624 if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT) {
625 conn->session->state = ISCSI_STATE_IN_RECOVERY;
626 iscsi_block_session(session_to_cls(conn->session));
627 was_logout = 1;
628 }
629 rc = conn->session->tt->xmit_mgmt_task(conn, conn->mtask);
Mike Christie77a23c22007-05-30 12:57:18 -0500630 spin_lock_bh(&conn->session->lock);
Mike Christieb5072ea2006-10-16 18:09:42 -0400631 if (rc)
632 return rc;
633
Mike Christie05db8882007-02-28 17:32:16 -0600634 /* done with this in-progress mtask */
635 conn->mtask = NULL;
636
Mike Christieb5072ea2006-10-16 18:09:42 -0400637 if (was_logout) {
638 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
639 return -ENODATA;
640 }
641 return 0;
642}
643
Mike Christie77a23c22007-05-30 12:57:18 -0500644static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
645{
646 struct iscsi_session *session = conn->session;
647
648 /*
649 * Check for iSCSI window and take care of CmdSN wrap-around
650 */
Mike Christiee0726402007-07-26 12:46:48 -0500651 if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) {
652 debug_scsi("iSCSI CmdSN closed. ExpCmdSn %u MaxCmdSN %u "
653 "CmdSN %u/%u\n", session->exp_cmdsn,
654 session->max_cmdsn, session->cmdsn,
655 session->queued_cmdsn);
Mike Christie77a23c22007-05-30 12:57:18 -0500656 return -ENOSPC;
657 }
658 return 0;
659}
660
661static int iscsi_xmit_ctask(struct iscsi_conn *conn)
662{
663 struct iscsi_cmd_task *ctask = conn->ctask;
664 int rc = 0;
665
666 /*
667 * serialize with TMF AbortTask
668 */
669 if (ctask->state == ISCSI_TASK_ABORTING)
670 goto done;
671
672 __iscsi_get_ctask(ctask);
673 spin_unlock_bh(&conn->session->lock);
674 rc = conn->session->tt->xmit_cmd_task(conn, ctask);
675 spin_lock_bh(&conn->session->lock);
676 __iscsi_put_ctask(ctask);
677
678done:
679 if (!rc)
680 /* done with this ctask */
681 conn->ctask = NULL;
682 return rc;
683}
684
Mike Christie7996a772006-04-06 21:13:41 -0500685/**
686 * iscsi_data_xmit - xmit any command into the scheduled connection
687 * @conn: iscsi connection
688 *
689 * Notes:
690 * The function can return -EAGAIN in which case the caller must
691 * re-schedule it again later or recover. '0' return code means
692 * successful xmit.
693 **/
694static int iscsi_data_xmit(struct iscsi_conn *conn)
695{
Mike Christie3219e522006-05-30 00:37:28 -0500696 int rc = 0;
Mike Christie7996a772006-04-06 21:13:41 -0500697
Mike Christie77a23c22007-05-30 12:57:18 -0500698 spin_lock_bh(&conn->session->lock);
Mike Christie7996a772006-04-06 21:13:41 -0500699 if (unlikely(conn->suspend_tx)) {
700 debug_scsi("conn %d Tx suspended!\n", conn->id);
Mike Christie77a23c22007-05-30 12:57:18 -0500701 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -0500702 return -ENODATA;
Mike Christie7996a772006-04-06 21:13:41 -0500703 }
Mike Christie7996a772006-04-06 21:13:41 -0500704
705 if (conn->ctask) {
Mike Christie77a23c22007-05-30 12:57:18 -0500706 rc = iscsi_xmit_ctask(conn);
Mike Christie3219e522006-05-30 00:37:28 -0500707 if (rc)
Mike Christie7996a772006-04-06 21:13:41 -0500708 goto again;
Mike Christie7996a772006-04-06 21:13:41 -0500709 }
Mike Christie77a23c22007-05-30 12:57:18 -0500710
Mike Christie7996a772006-04-06 21:13:41 -0500711 if (conn->mtask) {
Mike Christie05db8882007-02-28 17:32:16 -0600712 rc = iscsi_xmit_mtask(conn);
Mike Christie3219e522006-05-30 00:37:28 -0500713 if (rc)
Mike Christie7996a772006-04-06 21:13:41 -0500714 goto again;
Mike Christie7996a772006-04-06 21:13:41 -0500715 }
716
Mike Christie77a23c22007-05-30 12:57:18 -0500717 /*
718 * process mgmt pdus like nops before commands since we should
719 * only have one nop-out as a ping from us and targets should not
720 * overflow us with nop-ins
721 */
722check_mgmt:
723 while (__kfifo_get(conn->mgmtqueue, (void*)&conn->mtask,
724 sizeof(void*))) {
725 iscsi_prep_mtask(conn, conn->mtask);
726 list_add_tail(&conn->mtask->running, &conn->mgmt_run_list);
727 rc = iscsi_xmit_mtask(conn);
728 if (rc)
729 goto again;
Mike Christie7996a772006-04-06 21:13:41 -0500730 }
731
732 /* process command queue */
Mike Christieb6c395e2006-07-24 15:47:15 -0500733 while (!list_empty(&conn->xmitqueue)) {
Mike Christie7996a772006-04-06 21:13:41 -0500734 /*
735 * iscsi tcp may readd the task to the xmitqueue to send
736 * write data
737 */
Mike Christieb6c395e2006-07-24 15:47:15 -0500738 conn->ctask = list_entry(conn->xmitqueue.next,
739 struct iscsi_cmd_task, running);
Mike Christie96809f12007-08-15 01:38:29 -0500740 switch (conn->ctask->state) {
741 case ISCSI_TASK_ABORTING:
742 break;
743 case ISCSI_TASK_PENDING:
Mike Christie77a23c22007-05-30 12:57:18 -0500744 iscsi_prep_scsi_cmd_pdu(conn->ctask);
745 conn->session->tt->init_cmd_task(conn->ctask);
Mike Christie96809f12007-08-15 01:38:29 -0500746 /* fall through */
747 default:
748 conn->ctask->state = ISCSI_TASK_RUNNING;
749 break;
Mike Christie77a23c22007-05-30 12:57:18 -0500750 }
Mike Christieb6c395e2006-07-24 15:47:15 -0500751 list_move_tail(conn->xmitqueue.next, &conn->run_list);
Mike Christie96809f12007-08-15 01:38:29 -0500752
Mike Christie77a23c22007-05-30 12:57:18 -0500753 rc = iscsi_xmit_ctask(conn);
754 if (rc)
Mike Christie60ecebf2006-08-31 18:09:25 -0400755 goto again;
Mike Christie77a23c22007-05-30 12:57:18 -0500756 /*
757 * we could continuously get new ctask requests so
758 * we need to check the mgmt queue for nops that need to
759 * be sent to aviod starvation
760 */
761 if (__kfifo_len(conn->mgmtqueue))
762 goto check_mgmt;
Mike Christie7996a772006-04-06 21:13:41 -0500763 }
Mike Christieb6c395e2006-07-24 15:47:15 -0500764 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -0500765 return -ENODATA;
Mike Christie7996a772006-04-06 21:13:41 -0500766
767again:
768 if (unlikely(conn->suspend_tx))
Mike Christie77a23c22007-05-30 12:57:18 -0500769 rc = -ENODATA;
770 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -0500771 return rc;
Mike Christie7996a772006-04-06 21:13:41 -0500772}
773
David Howellsc4028952006-11-22 14:57:56 +0000774static void iscsi_xmitworker(struct work_struct *work)
Mike Christie7996a772006-04-06 21:13:41 -0500775{
David Howellsc4028952006-11-22 14:57:56 +0000776 struct iscsi_conn *conn =
777 container_of(work, struct iscsi_conn, xmitwork);
Mike Christie3219e522006-05-30 00:37:28 -0500778 int rc;
Mike Christie7996a772006-04-06 21:13:41 -0500779 /*
780 * serialize Xmit worker on a per-connection basis.
781 */
Mike Christie3219e522006-05-30 00:37:28 -0500782 do {
783 rc = iscsi_data_xmit(conn);
784 } while (rc >= 0 || rc == -EAGAIN);
Mike Christie7996a772006-04-06 21:13:41 -0500785}
786
787enum {
788 FAILURE_BAD_HOST = 1,
789 FAILURE_SESSION_FAILED,
790 FAILURE_SESSION_FREED,
791 FAILURE_WINDOW_CLOSED,
Mike Christie60ecebf2006-08-31 18:09:25 -0400792 FAILURE_OOM,
Mike Christie7996a772006-04-06 21:13:41 -0500793 FAILURE_SESSION_TERMINATE,
Mike Christie656cffc2006-05-18 20:31:42 -0500794 FAILURE_SESSION_IN_RECOVERY,
Mike Christie7996a772006-04-06 21:13:41 -0500795 FAILURE_SESSION_RECOVERY_TIMEOUT,
796};
797
798int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
799{
800 struct Scsi_Host *host;
801 int reason = 0;
802 struct iscsi_session *session;
803 struct iscsi_conn *conn;
804 struct iscsi_cmd_task *ctask = NULL;
805
806 sc->scsi_done = done;
807 sc->result = 0;
Mike Christief47f2cf2006-08-31 18:09:33 -0400808 sc->SCp.ptr = NULL;
Mike Christie7996a772006-04-06 21:13:41 -0500809
810 host = sc->device->host;
811 session = iscsi_hostdata(host->hostdata);
812
813 spin_lock(&session->lock);
814
Mike Christie656cffc2006-05-18 20:31:42 -0500815 /*
816 * ISCSI_STATE_FAILED is a temp. state. The recovery
817 * code will decide what is best to do with command queued
818 * during this time
819 */
820 if (session->state != ISCSI_STATE_LOGGED_IN &&
821 session->state != ISCSI_STATE_FAILED) {
822 /*
823 * to handle the race between when we set the recovery state
824 * and block the session we requeue here (commands could
825 * be entering our queuecommand while a block is starting
826 * up because the block code is not locked)
827 */
828 if (session->state == ISCSI_STATE_IN_RECOVERY) {
829 reason = FAILURE_SESSION_IN_RECOVERY;
Mike Christie67a61112006-05-30 00:37:20 -0500830 goto reject;
Mike Christie7996a772006-04-06 21:13:41 -0500831 }
Mike Christie656cffc2006-05-18 20:31:42 -0500832
833 if (session->state == ISCSI_STATE_RECOVERY_FAILED)
834 reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
835 else if (session->state == ISCSI_STATE_TERMINATE)
836 reason = FAILURE_SESSION_TERMINATE;
837 else
838 reason = FAILURE_SESSION_FREED;
Mike Christie7996a772006-04-06 21:13:41 -0500839 goto fault;
840 }
841
Mike Christie7996a772006-04-06 21:13:41 -0500842 conn = session->leadconn;
Mike Christie98644042006-10-16 18:09:39 -0400843 if (!conn) {
844 reason = FAILURE_SESSION_FREED;
845 goto fault;
846 }
Mike Christie7996a772006-04-06 21:13:41 -0500847
Mike Christie77a23c22007-05-30 12:57:18 -0500848 if (iscsi_check_cmdsn_window_closed(conn)) {
849 reason = FAILURE_WINDOW_CLOSED;
850 goto reject;
851 }
852
Mike Christie60ecebf2006-08-31 18:09:25 -0400853 if (!__kfifo_get(session->cmdpool.queue, (void*)&ctask,
854 sizeof(void*))) {
855 reason = FAILURE_OOM;
856 goto reject;
857 }
Mike Christiee0726402007-07-26 12:46:48 -0500858 session->queued_cmdsn++;
859
Mike Christie7996a772006-04-06 21:13:41 -0500860 sc->SCp.phase = session->age;
861 sc->SCp.ptr = (char *)ctask;
862
Mike Christie60ecebf2006-08-31 18:09:25 -0400863 atomic_set(&ctask->refcount, 1);
Mike Christieb6c395e2006-07-24 15:47:15 -0500864 ctask->state = ISCSI_TASK_PENDING;
Mike Christie7996a772006-04-06 21:13:41 -0500865 ctask->mtask = NULL;
866 ctask->conn = conn;
867 ctask->sc = sc;
868 INIT_LIST_HEAD(&ctask->running);
Mike Christie7996a772006-04-06 21:13:41 -0500869
Mike Christieb6c395e2006-07-24 15:47:15 -0500870 list_add_tail(&ctask->running, &conn->xmitqueue);
Mike Christie7996a772006-04-06 21:13:41 -0500871 spin_unlock(&session->lock);
872
873 scsi_queue_work(host, &conn->xmitwork);
874 return 0;
875
876reject:
877 spin_unlock(&session->lock);
878 debug_scsi("cmd 0x%x rejected (%d)\n", sc->cmnd[0], reason);
879 return SCSI_MLQUEUE_HOST_BUSY;
880
881fault:
882 spin_unlock(&session->lock);
Or Gerlitzbe2df722006-05-02 19:46:43 -0500883 printk(KERN_ERR "iscsi: cmd 0x%x is not queued (%d)\n",
Mike Christie7996a772006-04-06 21:13:41 -0500884 sc->cmnd[0], reason);
885 sc->result = (DID_NO_CONNECT << 16);
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900886 scsi_set_resid(sc, scsi_bufflen(sc));
Mike Christie7996a772006-04-06 21:13:41 -0500887 sc->scsi_done(sc);
888 return 0;
889}
890EXPORT_SYMBOL_GPL(iscsi_queuecommand);
891
892int iscsi_change_queue_depth(struct scsi_device *sdev, int depth)
893{
894 if (depth > ISCSI_MAX_CMD_PER_LUN)
895 depth = ISCSI_MAX_CMD_PER_LUN;
896 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
897 return sdev->queue_depth;
898}
899EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
900
Mike Christie77a23c22007-05-30 12:57:18 -0500901static struct iscsi_mgmt_task *
902__iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
903 char *data, uint32_t data_size)
Mike Christie7996a772006-04-06 21:13:41 -0500904{
905 struct iscsi_session *session = conn->session;
Mike Christie7996a772006-04-06 21:13:41 -0500906 struct iscsi_mgmt_task *mtask;
907
Mike Christie77a23c22007-05-30 12:57:18 -0500908 if (session->state == ISCSI_STATE_TERMINATE)
909 return NULL;
910
Mike Christie7996a772006-04-06 21:13:41 -0500911 if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) ||
912 hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
913 /*
914 * Login and Text are sent serially, in
915 * request-followed-by-response sequence.
916 * Same mtask can be used. Same ITT must be used.
917 * Note that login_mtask is preallocated at conn_create().
918 */
919 mtask = conn->login_mtask;
920 else {
Mike Christie656cffc2006-05-18 20:31:42 -0500921 BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
922 BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
Mike Christie7996a772006-04-06 21:13:41 -0500923
924 if (!__kfifo_get(session->mgmtpool.queue,
Mike Christie77a23c22007-05-30 12:57:18 -0500925 (void*)&mtask, sizeof(void*)))
926 return NULL;
Mike Christie7996a772006-04-06 21:13:41 -0500927 }
928
Mike Christie7996a772006-04-06 21:13:41 -0500929 if (data_size) {
930 memcpy(mtask->data, data, data_size);
931 mtask->data_count = data_size;
932 } else
933 mtask->data_count = 0;
934
935 INIT_LIST_HEAD(&mtask->running);
936 memcpy(mtask->hdr, hdr, sizeof(struct iscsi_hdr));
Mike Christie77a23c22007-05-30 12:57:18 -0500937 __kfifo_put(conn->mgmtqueue, (void*)&mtask, sizeof(void*));
938 return mtask;
Mike Christie7996a772006-04-06 21:13:41 -0500939}
940
941int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
942 char *data, uint32_t data_size)
943{
944 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie77a23c22007-05-30 12:57:18 -0500945 struct iscsi_session *session = conn->session;
946 int err = 0;
Mike Christie7996a772006-04-06 21:13:41 -0500947
Mike Christie77a23c22007-05-30 12:57:18 -0500948 spin_lock_bh(&session->lock);
949 if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size))
950 err = -EPERM;
951 spin_unlock_bh(&session->lock);
952 scsi_queue_work(session->host, &conn->xmitwork);
953 return err;
Mike Christie7996a772006-04-06 21:13:41 -0500954}
955EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
956
957void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
958{
959 struct iscsi_session *session = class_to_transport_session(cls_session);
960 struct iscsi_conn *conn = session->leadconn;
961
962 spin_lock_bh(&session->lock);
963 if (session->state != ISCSI_STATE_LOGGED_IN) {
Mike Christie656cffc2006-05-18 20:31:42 -0500964 session->state = ISCSI_STATE_RECOVERY_FAILED;
Mike Christie7996a772006-04-06 21:13:41 -0500965 if (conn)
966 wake_up(&conn->ehwait);
967 }
968 spin_unlock_bh(&session->lock);
969}
970EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
971
972int iscsi_eh_host_reset(struct scsi_cmnd *sc)
973{
974 struct Scsi_Host *host = sc->device->host;
975 struct iscsi_session *session = iscsi_hostdata(host->hostdata);
976 struct iscsi_conn *conn = session->leadconn;
977 int fail_session = 0;
978
979 spin_lock_bh(&session->lock);
980 if (session->state == ISCSI_STATE_TERMINATE) {
981failed:
982 debug_scsi("failing host reset: session terminated "
Pete Wyckoffd6e24d12006-11-08 15:58:32 -0600983 "[CID %d age %d]\n", conn->id, session->age);
Mike Christie7996a772006-04-06 21:13:41 -0500984 spin_unlock_bh(&session->lock);
985 return FAILED;
986 }
987
988 if (sc->SCp.phase == session->age) {
Pete Wyckoffd6e24d12006-11-08 15:58:32 -0600989 debug_scsi("failing connection CID %d due to SCSI host reset\n",
Mike Christie7996a772006-04-06 21:13:41 -0500990 conn->id);
991 fail_session = 1;
992 }
993 spin_unlock_bh(&session->lock);
994
995 /*
996 * we drop the lock here but the leadconn cannot be destoyed while
997 * we are in the scsi eh
998 */
Mike Christie656cffc2006-05-18 20:31:42 -0500999 if (fail_session)
Mike Christie7996a772006-04-06 21:13:41 -05001000 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie7996a772006-04-06 21:13:41 -05001001
1002 debug_scsi("iscsi_eh_host_reset wait for relogin\n");
1003 wait_event_interruptible(conn->ehwait,
1004 session->state == ISCSI_STATE_TERMINATE ||
1005 session->state == ISCSI_STATE_LOGGED_IN ||
Mike Christie656cffc2006-05-18 20:31:42 -05001006 session->state == ISCSI_STATE_RECOVERY_FAILED);
Mike Christie7996a772006-04-06 21:13:41 -05001007 if (signal_pending(current))
1008 flush_signals(current);
1009
1010 spin_lock_bh(&session->lock);
1011 if (session->state == ISCSI_STATE_LOGGED_IN)
Or Gerlitzbe2df722006-05-02 19:46:43 -05001012 printk(KERN_INFO "iscsi: host reset succeeded\n");
Mike Christie7996a772006-04-06 21:13:41 -05001013 else
1014 goto failed;
1015 spin_unlock_bh(&session->lock);
1016
1017 return SUCCESS;
1018}
1019EXPORT_SYMBOL_GPL(iscsi_eh_host_reset);
1020
1021static void iscsi_tmabort_timedout(unsigned long data)
1022{
1023 struct iscsi_cmd_task *ctask = (struct iscsi_cmd_task *)data;
1024 struct iscsi_conn *conn = ctask->conn;
1025 struct iscsi_session *session = conn->session;
1026
1027 spin_lock(&session->lock);
1028 if (conn->tmabort_state == TMABORT_INITIAL) {
1029 conn->tmabort_state = TMABORT_TIMEDOUT;
1030 debug_scsi("tmabort timedout [sc %p itt 0x%x]\n",
1031 ctask->sc, ctask->itt);
1032 /* unblock eh_abort() */
1033 wake_up(&conn->ehwait);
1034 }
1035 spin_unlock(&session->lock);
1036}
1037
Mike Christie7996a772006-04-06 21:13:41 -05001038static int iscsi_exec_abort_task(struct scsi_cmnd *sc,
1039 struct iscsi_cmd_task *ctask)
1040{
1041 struct iscsi_conn *conn = ctask->conn;
1042 struct iscsi_session *session = conn->session;
1043 struct iscsi_tm *hdr = &conn->tmhdr;
Mike Christie7996a772006-04-06 21:13:41 -05001044
1045 /*
1046 * ctask timed out but session is OK requests must be serialized.
1047 */
1048 memset(hdr, 0, sizeof(struct iscsi_tm));
1049 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
1050 hdr->flags = ISCSI_TM_FUNC_ABORT_TASK;
1051 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
1052 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
1053 hdr->rtt = ctask->hdr->itt;
1054 hdr->refcmdsn = ctask->hdr->cmdsn;
1055
Mike Christie77a23c22007-05-30 12:57:18 -05001056 ctask->mtask = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
1057 NULL, 0);
1058 if (!ctask->mtask) {
Mike Christie6724add2007-08-15 01:38:30 -05001059 spin_unlock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001060 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie6724add2007-08-15 01:38:30 -05001061 spin_lock_bh(&session->lock)
Mike Christie77a23c22007-05-30 12:57:18 -05001062 debug_scsi("abort sent failure [itt 0x%x]\n", ctask->itt);
1063 return -EPERM;
Mike Christie7996a772006-04-06 21:13:41 -05001064 }
Mike Christie77a23c22007-05-30 12:57:18 -05001065 ctask->state = ISCSI_TASK_ABORTING;
Mike Christie7996a772006-04-06 21:13:41 -05001066
1067 debug_scsi("abort sent [itt 0x%x]\n", ctask->itt);
1068
Mike Christie7996a772006-04-06 21:13:41 -05001069 if (conn->tmabort_state == TMABORT_INITIAL) {
1070 conn->tmfcmd_pdus_cnt++;
Mike Christie77a23c22007-05-30 12:57:18 -05001071 conn->tmabort_timer.expires = 20*HZ + jiffies;
Mike Christie7996a772006-04-06 21:13:41 -05001072 conn->tmabort_timer.function = iscsi_tmabort_timedout;
1073 conn->tmabort_timer.data = (unsigned long)ctask;
1074 add_timer(&conn->tmabort_timer);
Pete Wyckoffd6e24d12006-11-08 15:58:32 -06001075 debug_scsi("abort set timeout [itt 0x%x]\n", ctask->itt);
Mike Christie7996a772006-04-06 21:13:41 -05001076 }
1077 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05001078 mutex_unlock(&session->eh_mutex);
Mike Christie77a23c22007-05-30 12:57:18 -05001079 scsi_queue_work(session->host, &conn->xmitwork);
Mike Christie7996a772006-04-06 21:13:41 -05001080
1081 /*
1082 * block eh thread until:
1083 *
1084 * 1) abort response
1085 * 2) abort timeout
1086 * 3) session is terminated or restarted or userspace has
1087 * given up on recovery
1088 */
1089 wait_event_interruptible(conn->ehwait,
1090 sc->SCp.phase != session->age ||
1091 session->state != ISCSI_STATE_LOGGED_IN ||
Mike Christie656cffc2006-05-18 20:31:42 -05001092 conn->tmabort_state != TMABORT_INITIAL);
Mike Christie7996a772006-04-06 21:13:41 -05001093 if (signal_pending(current))
1094 flush_signals(current);
1095 del_timer_sync(&conn->tmabort_timer);
Mike Christie6724add2007-08-15 01:38:30 -05001096 mutex_lock(&session->eh_mutex);
Mike Christie77a23c22007-05-30 12:57:18 -05001097 spin_lock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001098 return 0;
1099}
1100
1101/*
Mike Christie77a23c22007-05-30 12:57:18 -05001102 * session lock must be held
Mike Christie7996a772006-04-06 21:13:41 -05001103 */
Mike Christieb6c395e2006-07-24 15:47:15 -05001104static struct iscsi_mgmt_task *
1105iscsi_remove_mgmt_task(struct kfifo *fifo, uint32_t itt)
1106{
1107 int i, nr_tasks = __kfifo_len(fifo) / sizeof(void*);
1108 struct iscsi_mgmt_task *task;
Mike Christie7996a772006-04-06 21:13:41 -05001109
Mike Christieb6c395e2006-07-24 15:47:15 -05001110 debug_scsi("searching %d tasks\n", nr_tasks);
1111
1112 for (i = 0; i < nr_tasks; i++) {
1113 __kfifo_get(fifo, (void*)&task, sizeof(void*));
1114 debug_scsi("check task %u\n", task->itt);
1115
1116 if (task->itt == itt) {
1117 debug_scsi("matched task\n");
1118 return task;
1119 }
1120
1121 __kfifo_put(fifo, (void*)&task, sizeof(void*));
1122 }
1123 return NULL;
1124}
Mike Christie7996a772006-04-06 21:13:41 -05001125
1126static int iscsi_ctask_mtask_cleanup(struct iscsi_cmd_task *ctask)
1127{
1128 struct iscsi_conn *conn = ctask->conn;
1129 struct iscsi_session *session = conn->session;
1130
1131 if (!ctask->mtask)
1132 return -EINVAL;
1133
Mike Christie77a23c22007-05-30 12:57:18 -05001134 if (!iscsi_remove_mgmt_task(conn->mgmtqueue, ctask->mtask->itt))
Mike Christie7996a772006-04-06 21:13:41 -05001135 list_del(&ctask->mtask->running);
1136 __kfifo_put(session->mgmtpool.queue, (void*)&ctask->mtask,
1137 sizeof(void*));
1138 ctask->mtask = NULL;
1139 return 0;
1140}
1141
1142/*
Mike Christie77a23c22007-05-30 12:57:18 -05001143 * session lock must be held
Mike Christie7996a772006-04-06 21:13:41 -05001144 */
1145static void fail_command(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
1146 int err)
1147{
1148 struct scsi_cmnd *sc;
1149
Mike Christie7996a772006-04-06 21:13:41 -05001150 sc = ctask->sc;
1151 if (!sc)
1152 return;
Mike Christiee648f632006-08-31 18:09:34 -04001153
Mike Christiee0726402007-07-26 12:46:48 -05001154 if (ctask->state == ISCSI_TASK_PENDING)
1155 /*
1156 * cmd never made it to the xmit thread, so we should not count
1157 * the cmd in the sequencing
1158 */
1159 conn->session->queued_cmdsn--;
1160 else
Mike Christie77a23c22007-05-30 12:57:18 -05001161 conn->session->tt->cleanup_cmd_task(conn, ctask);
Mike Christie7ea8b822006-07-24 15:47:22 -05001162 iscsi_ctask_mtask_cleanup(ctask);
1163
Mike Christie7996a772006-04-06 21:13:41 -05001164 sc->result = err;
FUJITA Tomonori1c138992007-06-14 22:13:17 +09001165 scsi_set_resid(sc, scsi_bufflen(sc));
Mike Christie77a23c22007-05-30 12:57:18 -05001166 if (conn->ctask == ctask)
1167 conn->ctask = NULL;
Mike Christiee648f632006-08-31 18:09:34 -04001168 /* release ref from queuecommand */
Mike Christie60ecebf2006-08-31 18:09:25 -04001169 __iscsi_put_ctask(ctask);
Mike Christie7996a772006-04-06 21:13:41 -05001170}
1171
Mike Christie6724add2007-08-15 01:38:30 -05001172static void iscsi_suspend_tx(struct iscsi_conn *conn)
1173{
1174 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1175 scsi_flush_work(conn->session->host);
1176}
1177
1178static void iscsi_start_tx(struct iscsi_conn *conn)
1179{
1180 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1181 scsi_queue_work(conn->session->host, &conn->xmitwork);
1182}
1183
Mike Christie7996a772006-04-06 21:13:41 -05001184int iscsi_eh_abort(struct scsi_cmnd *sc)
1185{
Mike Christie6724add2007-08-15 01:38:30 -05001186 struct Scsi_Host *host = sc->device->host;
1187 struct iscsi_session *session = iscsi_hostdata(host->hostdata);
Mike Christief47f2cf2006-08-31 18:09:33 -04001188 struct iscsi_cmd_task *ctask;
1189 struct iscsi_conn *conn;
Mike Christie7996a772006-04-06 21:13:41 -05001190 int rc;
1191
Mike Christie6724add2007-08-15 01:38:30 -05001192 mutex_lock(&session->eh_mutex);
1193 spin_lock_bh(&session->lock);
Mike Christief47f2cf2006-08-31 18:09:33 -04001194 /*
1195 * if session was ISCSI_STATE_IN_RECOVERY then we may not have
1196 * got the command.
1197 */
1198 if (!sc->SCp.ptr) {
1199 debug_scsi("sc never reached iscsi layer or it completed.\n");
Mike Christie6724add2007-08-15 01:38:30 -05001200 spin_unlock_bh(&session->lock);
1201 mutex_unlock(&session->eh_mutex);
Mike Christief47f2cf2006-08-31 18:09:33 -04001202 return SUCCESS;
1203 }
1204
1205 ctask = (struct iscsi_cmd_task *)sc->SCp.ptr;
1206 conn = ctask->conn;
Mike Christief47f2cf2006-08-31 18:09:33 -04001207
Mike Christie7996a772006-04-06 21:13:41 -05001208 conn->eh_abort_cnt++;
1209 debug_scsi("aborting [sc %p itt 0x%x]\n", sc, ctask->itt);
1210
Mike Christie7996a772006-04-06 21:13:41 -05001211 /*
1212 * If we are not logged in or we have started a new session
1213 * then let the host reset code handle this
1214 */
1215 if (session->state != ISCSI_STATE_LOGGED_IN ||
1216 sc->SCp.phase != session->age)
1217 goto failed;
1218
1219 /* ctask completed before time out */
Mike Christie7ea8b822006-07-24 15:47:22 -05001220 if (!ctask->sc) {
Mike Christie7ea8b822006-07-24 15:47:22 -05001221 debug_scsi("sc completed while abort in progress\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001222 goto success;
Mike Christie7ea8b822006-07-24 15:47:22 -05001223 }
Mike Christie7996a772006-04-06 21:13:41 -05001224
1225 /* what should we do here ? */
1226 if (conn->ctask == ctask) {
Or Gerlitzbe2df722006-05-02 19:46:43 -05001227 printk(KERN_INFO "iscsi: sc %p itt 0x%x partially sent. "
1228 "Failing abort\n", sc, ctask->itt);
Mike Christie7996a772006-04-06 21:13:41 -05001229 goto failed;
1230 }
1231
Mike Christie77a23c22007-05-30 12:57:18 -05001232 if (ctask->state == ISCSI_TASK_PENDING) {
1233 fail_command(conn, ctask, DID_ABORT << 16);
1234 goto success;
1235 }
Mike Christie7996a772006-04-06 21:13:41 -05001236
1237 conn->tmabort_state = TMABORT_INITIAL;
Mike Christie7996a772006-04-06 21:13:41 -05001238 rc = iscsi_exec_abort_task(sc, ctask);
Mike Christie7996a772006-04-06 21:13:41 -05001239 if (rc || sc->SCp.phase != session->age ||
1240 session->state != ISCSI_STATE_LOGGED_IN)
1241 goto failed;
Mike Christie7ea8b822006-07-24 15:47:22 -05001242 iscsi_ctask_mtask_cleanup(ctask);
Mike Christie7996a772006-04-06 21:13:41 -05001243
Mike Christie7ea8b822006-07-24 15:47:22 -05001244 switch (conn->tmabort_state) {
1245 case TMABORT_SUCCESS:
Mike Christie77a23c22007-05-30 12:57:18 -05001246 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05001247 iscsi_suspend_tx(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001248 /*
1249 * clean up task if aborted. grab the recv lock as a writer
1250 */
1251 write_lock_bh(conn->recv_lock);
1252 spin_lock(&session->lock);
1253 fail_command(conn, ctask, DID_ABORT << 16);
1254 spin_unlock(&session->lock);
1255 write_unlock_bh(conn->recv_lock);
Mike Christie6724add2007-08-15 01:38:30 -05001256 iscsi_start_tx(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001257 goto success_unlocked;
Mike Christie7ea8b822006-07-24 15:47:22 -05001258 case TMABORT_NOT_FOUND:
1259 if (!ctask->sc) {
1260 /* ctask completed before tmf abort response */
Mike Christie7ea8b822006-07-24 15:47:22 -05001261 debug_scsi("sc completed while abort in progress\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001262 goto success;
Mike Christie7ea8b822006-07-24 15:47:22 -05001263 }
1264 /* fall through */
1265 default:
1266 /* timedout or failed */
Mike Christie7996a772006-04-06 21:13:41 -05001267 spin_unlock_bh(&session->lock);
1268 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie77a23c22007-05-30 12:57:18 -05001269 goto failed_unlocked;
Mike Christie7996a772006-04-06 21:13:41 -05001270 }
1271
Mike Christie77a23c22007-05-30 12:57:18 -05001272success:
Mike Christie7996a772006-04-06 21:13:41 -05001273 spin_unlock_bh(&session->lock);
Mike Christie77a23c22007-05-30 12:57:18 -05001274success_unlocked:
1275 debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
Mike Christie6724add2007-08-15 01:38:30 -05001276 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001277 return SUCCESS;
1278
1279failed:
1280 spin_unlock_bh(&session->lock);
Mike Christie77a23c22007-05-30 12:57:18 -05001281failed_unlocked:
Mike Christie7996a772006-04-06 21:13:41 -05001282 debug_scsi("abort failed [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
Mike Christie6724add2007-08-15 01:38:30 -05001283 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001284 return FAILED;
1285}
1286EXPORT_SYMBOL_GPL(iscsi_eh_abort);
1287
1288int
1289iscsi_pool_init(struct iscsi_queue *q, int max, void ***items, int item_size)
1290{
1291 int i;
1292
1293 *items = kmalloc(max * sizeof(void*), GFP_KERNEL);
1294 if (*items == NULL)
1295 return -ENOMEM;
1296
1297 q->max = max;
1298 q->pool = kmalloc(max * sizeof(void*), GFP_KERNEL);
1299 if (q->pool == NULL) {
1300 kfree(*items);
1301 return -ENOMEM;
1302 }
1303
1304 q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
1305 GFP_KERNEL, NULL);
1306 if (q->queue == ERR_PTR(-ENOMEM)) {
1307 kfree(q->pool);
1308 kfree(*items);
1309 return -ENOMEM;
1310 }
1311
1312 for (i = 0; i < max; i++) {
1313 q->pool[i] = kmalloc(item_size, GFP_KERNEL);
1314 if (q->pool[i] == NULL) {
1315 int j;
1316
1317 for (j = 0; j < i; j++)
1318 kfree(q->pool[j]);
1319
1320 kfifo_free(q->queue);
1321 kfree(q->pool);
1322 kfree(*items);
1323 return -ENOMEM;
1324 }
1325 memset(q->pool[i], 0, item_size);
1326 (*items)[i] = q->pool[i];
1327 __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
1328 }
1329 return 0;
1330}
1331EXPORT_SYMBOL_GPL(iscsi_pool_init);
1332
1333void iscsi_pool_free(struct iscsi_queue *q, void **items)
1334{
1335 int i;
1336
1337 for (i = 0; i < q->max; i++)
1338 kfree(items[i]);
1339 kfree(q->pool);
1340 kfree(items);
1341}
1342EXPORT_SYMBOL_GPL(iscsi_pool_free);
1343
1344/*
1345 * iSCSI Session's hostdata organization:
1346 *
1347 * *------------------* <== hostdata_session(host->hostdata)
1348 * | ptr to class sess|
1349 * |------------------| <== iscsi_hostdata(host->hostdata)
1350 * | iscsi_session |
1351 * *------------------*
1352 */
1353
1354#define hostdata_privsize(_sz) (sizeof(unsigned long) + _sz + \
1355 _sz % sizeof(unsigned long))
1356
1357#define hostdata_session(_hostdata) (iscsi_ptr(*(unsigned long *)_hostdata))
1358
1359/**
1360 * iscsi_session_setup - create iscsi cls session and host and session
1361 * @scsit: scsi transport template
1362 * @iscsit: iscsi transport template
Mike Christie15482712007-05-30 12:57:19 -05001363 * @cmds_max: scsi host can queue
1364 * @qdepth: scsi host cmds per lun
1365 * @cmd_task_size: LLD ctask private data size
1366 * @mgmt_task_size: LLD mtask private data size
Mike Christie7996a772006-04-06 21:13:41 -05001367 * @initial_cmdsn: initial CmdSN
1368 * @hostno: host no allocated
1369 *
1370 * This can be used by software iscsi_transports that allocate
1371 * a session per scsi host.
1372 **/
1373struct iscsi_cls_session *
1374iscsi_session_setup(struct iscsi_transport *iscsit,
1375 struct scsi_transport_template *scsit,
Mike Christie15482712007-05-30 12:57:19 -05001376 uint16_t cmds_max, uint16_t qdepth,
Mike Christie7996a772006-04-06 21:13:41 -05001377 int cmd_task_size, int mgmt_task_size,
1378 uint32_t initial_cmdsn, uint32_t *hostno)
1379{
1380 struct Scsi_Host *shost;
1381 struct iscsi_session *session;
1382 struct iscsi_cls_session *cls_session;
1383 int cmd_i;
1384
Mike Christie15482712007-05-30 12:57:19 -05001385 if (qdepth > ISCSI_MAX_CMD_PER_LUN || qdepth < 1) {
1386 if (qdepth != 0)
1387 printk(KERN_ERR "iscsi: invalid queue depth of %d. "
1388 "Queue depth must be between 1 and %d.\n",
1389 qdepth, ISCSI_MAX_CMD_PER_LUN);
1390 qdepth = ISCSI_DEF_CMD_PER_LUN;
1391 }
1392
1393 if (cmds_max < 2 || (cmds_max & (cmds_max - 1)) ||
1394 cmds_max >= ISCSI_MGMT_ITT_OFFSET) {
1395 if (cmds_max != 0)
1396 printk(KERN_ERR "iscsi: invalid can_queue of %d. "
1397 "can_queue must be a power of 2 and between "
1398 "2 and %d - setting to %d.\n", cmds_max,
1399 ISCSI_MGMT_ITT_OFFSET, ISCSI_DEF_XMIT_CMDS_MAX);
1400 cmds_max = ISCSI_DEF_XMIT_CMDS_MAX;
1401 }
1402
Mike Christie7996a772006-04-06 21:13:41 -05001403 shost = scsi_host_alloc(iscsit->host_template,
1404 hostdata_privsize(sizeof(*session)));
1405 if (!shost)
1406 return NULL;
1407
Mike Christie15482712007-05-30 12:57:19 -05001408 /* the iscsi layer takes one task for reserve */
1409 shost->can_queue = cmds_max - 1;
1410 shost->cmd_per_lun = qdepth;
Mike Christie7996a772006-04-06 21:13:41 -05001411 shost->max_id = 1;
1412 shost->max_channel = 0;
1413 shost->max_lun = iscsit->max_lun;
1414 shost->max_cmd_len = iscsit->max_cmd_len;
1415 shost->transportt = scsit;
1416 shost->transportt->create_work_queue = 1;
1417 *hostno = shost->host_no;
1418
1419 session = iscsi_hostdata(shost->hostdata);
1420 memset(session, 0, sizeof(struct iscsi_session));
1421 session->host = shost;
1422 session->state = ISCSI_STATE_FREE;
1423 session->mgmtpool_max = ISCSI_MGMT_CMDS_MAX;
Mike Christie15482712007-05-30 12:57:19 -05001424 session->cmds_max = cmds_max;
Mike Christiee0726402007-07-26 12:46:48 -05001425 session->queued_cmdsn = session->cmdsn = initial_cmdsn;
Mike Christie7996a772006-04-06 21:13:41 -05001426 session->exp_cmdsn = initial_cmdsn + 1;
1427 session->max_cmdsn = initial_cmdsn + 1;
1428 session->max_r2t = 1;
1429 session->tt = iscsit;
Mike Christie6724add2007-08-15 01:38:30 -05001430 mutex_init(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001431
1432 /* initialize SCSI PDU commands pool */
1433 if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
1434 (void***)&session->cmds,
1435 cmd_task_size + sizeof(struct iscsi_cmd_task)))
1436 goto cmdpool_alloc_fail;
1437
1438 /* pre-format cmds pool with ITT */
1439 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
1440 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
1441
1442 if (cmd_task_size)
1443 ctask->dd_data = &ctask[1];
1444 ctask->itt = cmd_i;
Mike Christieb6c395e2006-07-24 15:47:15 -05001445 INIT_LIST_HEAD(&ctask->running);
Mike Christie7996a772006-04-06 21:13:41 -05001446 }
1447
1448 spin_lock_init(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001449
1450 /* initialize immediate command pool */
1451 if (iscsi_pool_init(&session->mgmtpool, session->mgmtpool_max,
1452 (void***)&session->mgmt_cmds,
1453 mgmt_task_size + sizeof(struct iscsi_mgmt_task)))
1454 goto mgmtpool_alloc_fail;
1455
1456
1457 /* pre-format immediate cmds pool with ITT */
1458 for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
1459 struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
1460
1461 if (mgmt_task_size)
1462 mtask->dd_data = &mtask[1];
1463 mtask->itt = ISCSI_MGMT_ITT_OFFSET + cmd_i;
Mike Christieb6c395e2006-07-24 15:47:15 -05001464 INIT_LIST_HEAD(&mtask->running);
Mike Christie7996a772006-04-06 21:13:41 -05001465 }
1466
1467 if (scsi_add_host(shost, NULL))
1468 goto add_host_fail;
1469
Mike Christief53a88d2006-06-28 12:00:27 -05001470 if (!try_module_get(iscsit->owner))
1471 goto cls_session_fail;
1472
Mike Christie6a8a0d32006-06-28 12:00:31 -05001473 cls_session = iscsi_create_session(shost, iscsit, 0);
Mike Christie7996a772006-04-06 21:13:41 -05001474 if (!cls_session)
Mike Christief53a88d2006-06-28 12:00:27 -05001475 goto module_put;
Mike Christie7996a772006-04-06 21:13:41 -05001476 *(unsigned long*)shost->hostdata = (unsigned long)cls_session;
1477
1478 return cls_session;
1479
Mike Christief53a88d2006-06-28 12:00:27 -05001480module_put:
1481 module_put(iscsit->owner);
Mike Christie7996a772006-04-06 21:13:41 -05001482cls_session_fail:
1483 scsi_remove_host(shost);
1484add_host_fail:
Mike Christie7996a772006-04-06 21:13:41 -05001485 iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
1486mgmtpool_alloc_fail:
1487 iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
1488cmdpool_alloc_fail:
1489 scsi_host_put(shost);
1490 return NULL;
1491}
1492EXPORT_SYMBOL_GPL(iscsi_session_setup);
1493
1494/**
1495 * iscsi_session_teardown - destroy session, host, and cls_session
1496 * shost: scsi host
1497 *
1498 * This can be used by software iscsi_transports that allocate
1499 * a session per scsi host.
1500 **/
1501void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
1502{
1503 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
1504 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
Mike Christie63f75cc2006-07-24 15:47:29 -05001505 struct module *owner = cls_session->transport->owner;
Mike Christie7996a772006-04-06 21:13:41 -05001506
Mike Christie464bb992007-07-26 12:46:45 -05001507 iscsi_unblock_session(cls_session);
Mike Christie7996a772006-04-06 21:13:41 -05001508 scsi_remove_host(shost);
1509
Mike Christie7996a772006-04-06 21:13:41 -05001510 iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
1511 iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
1512
Mike Christieb2c64162007-05-30 12:57:16 -05001513 kfree(session->password);
1514 kfree(session->password_in);
1515 kfree(session->username);
1516 kfree(session->username_in);
Mike Christief3ff0c32006-07-24 15:47:50 -05001517 kfree(session->targetname);
Mike Christied8196ed2007-05-30 12:57:25 -05001518 kfree(session->netdev);
Mike Christie0801c242007-05-30 12:57:12 -05001519 kfree(session->hwaddress);
Mike Christie8ad57812007-05-30 12:57:13 -05001520 kfree(session->initiatorname);
Mike Christief3ff0c32006-07-24 15:47:50 -05001521
Mike Christie7996a772006-04-06 21:13:41 -05001522 iscsi_destroy_session(cls_session);
1523 scsi_host_put(shost);
Mike Christie63f75cc2006-07-24 15:47:29 -05001524 module_put(owner);
Mike Christie7996a772006-04-06 21:13:41 -05001525}
1526EXPORT_SYMBOL_GPL(iscsi_session_teardown);
1527
1528/**
1529 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
1530 * @cls_session: iscsi_cls_session
1531 * @conn_idx: cid
1532 **/
1533struct iscsi_cls_conn *
1534iscsi_conn_setup(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
1535{
1536 struct iscsi_session *session = class_to_transport_session(cls_session);
1537 struct iscsi_conn *conn;
1538 struct iscsi_cls_conn *cls_conn;
Mike Christied36ab6f2006-05-18 20:31:34 -05001539 char *data;
Mike Christie7996a772006-04-06 21:13:41 -05001540
1541 cls_conn = iscsi_create_conn(cls_session, conn_idx);
1542 if (!cls_conn)
1543 return NULL;
1544 conn = cls_conn->dd_data;
1545 memset(conn, 0, sizeof(*conn));
1546
1547 conn->session = session;
1548 conn->cls_conn = cls_conn;
1549 conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
1550 conn->id = conn_idx;
1551 conn->exp_statsn = 0;
1552 conn->tmabort_state = TMABORT_INITIAL;
1553 INIT_LIST_HEAD(&conn->run_list);
1554 INIT_LIST_HEAD(&conn->mgmt_run_list);
Mike Christieb6c395e2006-07-24 15:47:15 -05001555 INIT_LIST_HEAD(&conn->xmitqueue);
Mike Christie7996a772006-04-06 21:13:41 -05001556
1557 /* initialize general immediate & non-immediate PDU commands queue */
Mike Christie7996a772006-04-06 21:13:41 -05001558 conn->mgmtqueue = kfifo_alloc(session->mgmtpool_max * sizeof(void*),
1559 GFP_KERNEL, NULL);
1560 if (conn->mgmtqueue == ERR_PTR(-ENOMEM))
1561 goto mgmtqueue_alloc_fail;
1562
David Howellsc4028952006-11-22 14:57:56 +00001563 INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
Mike Christie7996a772006-04-06 21:13:41 -05001564
1565 /* allocate login_mtask used for the login/text sequences */
1566 spin_lock_bh(&session->lock);
1567 if (!__kfifo_get(session->mgmtpool.queue,
1568 (void*)&conn->login_mtask,
1569 sizeof(void*))) {
1570 spin_unlock_bh(&session->lock);
1571 goto login_mtask_alloc_fail;
1572 }
1573 spin_unlock_bh(&session->lock);
1574
Mike Christiebf32ed32007-02-28 17:32:17 -06001575 data = kmalloc(ISCSI_DEF_MAX_RECV_SEG_LEN, GFP_KERNEL);
Mike Christied36ab6f2006-05-18 20:31:34 -05001576 if (!data)
1577 goto login_mtask_data_alloc_fail;
Mike Christiec8dc1e52006-07-24 15:47:39 -05001578 conn->login_mtask->data = conn->data = data;
Mike Christied36ab6f2006-05-18 20:31:34 -05001579
Mike Christie7996a772006-04-06 21:13:41 -05001580 init_timer(&conn->tmabort_timer);
Mike Christie7996a772006-04-06 21:13:41 -05001581 init_waitqueue_head(&conn->ehwait);
1582
1583 return cls_conn;
1584
Mike Christied36ab6f2006-05-18 20:31:34 -05001585login_mtask_data_alloc_fail:
1586 __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
1587 sizeof(void*));
Mike Christie7996a772006-04-06 21:13:41 -05001588login_mtask_alloc_fail:
1589 kfifo_free(conn->mgmtqueue);
1590mgmtqueue_alloc_fail:
Mike Christie7996a772006-04-06 21:13:41 -05001591 iscsi_destroy_conn(cls_conn);
1592 return NULL;
1593}
1594EXPORT_SYMBOL_GPL(iscsi_conn_setup);
1595
1596/**
1597 * iscsi_conn_teardown - teardown iscsi connection
1598 * cls_conn: iscsi class connection
1599 *
1600 * TODO: we may need to make this into a two step process
1601 * like scsi-mls remove + put host
1602 */
1603void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
1604{
1605 struct iscsi_conn *conn = cls_conn->dd_data;
1606 struct iscsi_session *session = conn->session;
1607 unsigned long flags;
1608
Mike Christie7996a772006-04-06 21:13:41 -05001609 spin_lock_bh(&session->lock);
Mike Christie77a23c22007-05-30 12:57:18 -05001610 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
Mike Christie7996a772006-04-06 21:13:41 -05001611 conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
1612 if (session->leadconn == conn) {
1613 /*
1614 * leading connection? then give up on recovery.
1615 */
1616 session->state = ISCSI_STATE_TERMINATE;
1617 wake_up(&conn->ehwait);
1618 }
1619 spin_unlock_bh(&session->lock);
1620
Mike Christie7996a772006-04-06 21:13:41 -05001621 /*
1622 * Block until all in-progress commands for this connection
1623 * time out or fail.
1624 */
1625 for (;;) {
1626 spin_lock_irqsave(session->host->host_lock, flags);
1627 if (!session->host->host_busy) { /* OK for ERL == 0 */
1628 spin_unlock_irqrestore(session->host->host_lock, flags);
1629 break;
1630 }
1631 spin_unlock_irqrestore(session->host->host_lock, flags);
1632 msleep_interruptible(500);
Or Gerlitzbe2df722006-05-02 19:46:43 -05001633 printk(KERN_INFO "iscsi: scsi conn_destroy(): host_busy %d "
1634 "host_failed %d\n", session->host->host_busy,
1635 session->host->host_failed);
Mike Christie7996a772006-04-06 21:13:41 -05001636 /*
1637 * force eh_abort() to unblock
1638 */
1639 wake_up(&conn->ehwait);
1640 }
1641
Mike Christie779ea122007-02-28 17:32:15 -06001642 /* flush queued up work because we free the connection below */
1643 scsi_flush_work(session->host);
1644
Mike Christie7996a772006-04-06 21:13:41 -05001645 spin_lock_bh(&session->lock);
Mike Christiec8dc1e52006-07-24 15:47:39 -05001646 kfree(conn->data);
Mike Christief3ff0c32006-07-24 15:47:50 -05001647 kfree(conn->persistent_address);
Mike Christie7996a772006-04-06 21:13:41 -05001648 __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
1649 sizeof(void*));
Mike Christiee0726402007-07-26 12:46:48 -05001650 if (session->leadconn == conn)
Mike Christie7996a772006-04-06 21:13:41 -05001651 session->leadconn = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05001652 spin_unlock_bh(&session->lock);
1653
Mike Christie7996a772006-04-06 21:13:41 -05001654 kfifo_free(conn->mgmtqueue);
1655
1656 iscsi_destroy_conn(cls_conn);
1657}
1658EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
1659
1660int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
1661{
1662 struct iscsi_conn *conn = cls_conn->dd_data;
1663 struct iscsi_session *session = conn->session;
1664
Mike Christieffd04362006-08-31 18:09:24 -04001665 if (!session) {
Mike Christie7996a772006-04-06 21:13:41 -05001666 printk(KERN_ERR "iscsi: can't start unbound connection\n");
1667 return -EPERM;
1668 }
1669
Mike Christiedb98ccd2006-08-31 18:09:31 -04001670 if ((session->imm_data_en || !session->initial_r2t_en) &&
1671 session->first_burst > session->max_burst) {
Mike Christieffd04362006-08-31 18:09:24 -04001672 printk("iscsi: invalid burst lengths: "
1673 "first_burst %d max_burst %d\n",
1674 session->first_burst, session->max_burst);
1675 return -EINVAL;
1676 }
1677
Mike Christie7996a772006-04-06 21:13:41 -05001678 spin_lock_bh(&session->lock);
1679 conn->c_stage = ISCSI_CONN_STARTED;
1680 session->state = ISCSI_STATE_LOGGED_IN;
Mike Christiee0726402007-07-26 12:46:48 -05001681 session->queued_cmdsn = session->cmdsn;
Mike Christie7996a772006-04-06 21:13:41 -05001682
1683 switch(conn->stop_stage) {
1684 case STOP_CONN_RECOVER:
1685 /*
1686 * unblock eh_abort() if it is blocked. re-try all
1687 * commands after successful recovery
1688 */
Mike Christie7996a772006-04-06 21:13:41 -05001689 conn->stop_stage = 0;
1690 conn->tmabort_state = TMABORT_INITIAL;
1691 session->age++;
Mike Christie7996a772006-04-06 21:13:41 -05001692 spin_unlock_bh(&session->lock);
1693
1694 iscsi_unblock_session(session_to_cls(session));
1695 wake_up(&conn->ehwait);
1696 return 0;
1697 case STOP_CONN_TERM:
Mike Christie7996a772006-04-06 21:13:41 -05001698 conn->stop_stage = 0;
1699 break;
Mike Christie7996a772006-04-06 21:13:41 -05001700 default:
1701 break;
1702 }
1703 spin_unlock_bh(&session->lock);
1704
1705 return 0;
1706}
1707EXPORT_SYMBOL_GPL(iscsi_conn_start);
1708
1709static void
1710flush_control_queues(struct iscsi_session *session, struct iscsi_conn *conn)
1711{
1712 struct iscsi_mgmt_task *mtask, *tmp;
1713
1714 /* handle pending */
Mike Christie77a23c22007-05-30 12:57:18 -05001715 while (__kfifo_get(conn->mgmtqueue, (void*)&mtask, sizeof(void*))) {
Mike Christie7996a772006-04-06 21:13:41 -05001716 if (mtask == conn->login_mtask)
1717 continue;
1718 debug_scsi("flushing pending mgmt task itt 0x%x\n", mtask->itt);
1719 __kfifo_put(session->mgmtpool.queue, (void*)&mtask,
1720 sizeof(void*));
1721 }
1722
1723 /* handle running */
1724 list_for_each_entry_safe(mtask, tmp, &conn->mgmt_run_list, running) {
1725 debug_scsi("flushing running mgmt task itt 0x%x\n", mtask->itt);
Mike Christieed2abc72006-05-02 19:46:40 -05001726 list_del(&mtask->running);
1727
Mike Christie7996a772006-04-06 21:13:41 -05001728 if (mtask == conn->login_mtask)
1729 continue;
Mike Christieed2abc72006-05-02 19:46:40 -05001730 __kfifo_put(session->mgmtpool.queue, (void*)&mtask,
Mike Christie7996a772006-04-06 21:13:41 -05001731 sizeof(void*));
1732 }
1733
1734 conn->mtask = NULL;
1735}
1736
1737/* Fail commands. Mutex and session lock held and recv side suspended */
1738static void fail_all_commands(struct iscsi_conn *conn)
1739{
1740 struct iscsi_cmd_task *ctask, *tmp;
1741
1742 /* flush pending */
Mike Christieb6c395e2006-07-24 15:47:15 -05001743 list_for_each_entry_safe(ctask, tmp, &conn->xmitqueue, running) {
Mike Christie7996a772006-04-06 21:13:41 -05001744 debug_scsi("failing pending sc %p itt 0x%x\n", ctask->sc,
1745 ctask->itt);
1746 fail_command(conn, ctask, DID_BUS_BUSY << 16);
1747 }
1748
1749 /* fail all other running */
1750 list_for_each_entry_safe(ctask, tmp, &conn->run_list, running) {
1751 debug_scsi("failing in progress sc %p itt 0x%x\n",
1752 ctask->sc, ctask->itt);
1753 fail_command(conn, ctask, DID_BUS_BUSY << 16);
1754 }
1755
1756 conn->ctask = NULL;
1757}
1758
Mike Christie656cffc2006-05-18 20:31:42 -05001759static void iscsi_start_session_recovery(struct iscsi_session *session,
1760 struct iscsi_conn *conn, int flag)
Mike Christie7996a772006-04-06 21:13:41 -05001761{
Mike Christieed2abc72006-05-02 19:46:40 -05001762 int old_stop_stage;
1763
Mike Christie6724add2007-08-15 01:38:30 -05001764 mutex_lock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001765 spin_lock_bh(&session->lock);
Mike Christieed2abc72006-05-02 19:46:40 -05001766 if (conn->stop_stage == STOP_CONN_TERM) {
Mike Christie7996a772006-04-06 21:13:41 -05001767 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05001768 mutex_unlock(&session->eh_mutex);
1769 return;
1770 }
1771
1772 /*
1773 * The LLD either freed/unset the lock on us, or userspace called
1774 * stop but did not create a proper connection (connection was never
1775 * bound or it was unbound then stop was called).
1776 */
1777 if (!conn->recv_lock) {
1778 spin_unlock_bh(&session->lock);
1779 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001780 return;
1781 }
Mike Christieed2abc72006-05-02 19:46:40 -05001782
1783 /*
1784 * When this is called for the in_login state, we only want to clean
Mike Christie67a61112006-05-30 00:37:20 -05001785 * up the login task and connection. We do not need to block and set
1786 * the recovery state again
Mike Christieed2abc72006-05-02 19:46:40 -05001787 */
Mike Christie67a61112006-05-30 00:37:20 -05001788 if (flag == STOP_CONN_TERM)
1789 session->state = ISCSI_STATE_TERMINATE;
1790 else if (conn->stop_stage != STOP_CONN_RECOVER)
1791 session->state = ISCSI_STATE_IN_RECOVERY;
Mike Christieed2abc72006-05-02 19:46:40 -05001792
1793 old_stop_stage = conn->stop_stage;
Mike Christie7996a772006-04-06 21:13:41 -05001794 conn->stop_stage = flag;
Mike Christie67a61112006-05-30 00:37:20 -05001795 conn->c_stage = ISCSI_CONN_STOPPED;
Mike Christie7996a772006-04-06 21:13:41 -05001796 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05001797
1798 iscsi_suspend_tx(conn);
Mike Christie7996a772006-04-06 21:13:41 -05001799
Mike Christie1c834692006-07-24 15:47:26 -05001800 write_lock_bh(conn->recv_lock);
1801 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
1802 write_unlock_bh(conn->recv_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001803
Mike Christie7996a772006-04-06 21:13:41 -05001804 /*
1805 * for connection level recovery we should not calculate
1806 * header digest. conn->hdr_size used for optimization
1807 * in hdr_extract() and will be re-negotiated at
1808 * set_param() time.
1809 */
1810 if (flag == STOP_CONN_RECOVER) {
1811 conn->hdrdgst_en = 0;
1812 conn->datadgst_en = 0;
Mike Christie656cffc2006-05-18 20:31:42 -05001813 if (session->state == ISCSI_STATE_IN_RECOVERY &&
Mike Christie67a61112006-05-30 00:37:20 -05001814 old_stop_stage != STOP_CONN_RECOVER) {
1815 debug_scsi("blocking session\n");
Mike Christie7996a772006-04-06 21:13:41 -05001816 iscsi_block_session(session_to_cls(session));
Mike Christie67a61112006-05-30 00:37:20 -05001817 }
Mike Christie7996a772006-04-06 21:13:41 -05001818 }
Mike Christie656cffc2006-05-18 20:31:42 -05001819
Mike Christie656cffc2006-05-18 20:31:42 -05001820 /*
1821 * flush queues.
1822 */
1823 spin_lock_bh(&session->lock);
1824 fail_all_commands(conn);
1825 flush_control_queues(session, conn);
1826 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05001827 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001828}
Mike Christie7996a772006-04-06 21:13:41 -05001829
1830void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
1831{
1832 struct iscsi_conn *conn = cls_conn->dd_data;
1833 struct iscsi_session *session = conn->session;
1834
1835 switch (flag) {
1836 case STOP_CONN_RECOVER:
1837 case STOP_CONN_TERM:
1838 iscsi_start_session_recovery(session, conn, flag);
Mike Christie8d2860b2006-05-02 19:46:47 -05001839 break;
Mike Christie7996a772006-04-06 21:13:41 -05001840 default:
Or Gerlitzbe2df722006-05-02 19:46:43 -05001841 printk(KERN_ERR "iscsi: invalid stop flag %d\n", flag);
Mike Christie7996a772006-04-06 21:13:41 -05001842 }
1843}
1844EXPORT_SYMBOL_GPL(iscsi_conn_stop);
1845
1846int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
1847 struct iscsi_cls_conn *cls_conn, int is_leading)
1848{
1849 struct iscsi_session *session = class_to_transport_session(cls_session);
Mike Christie98644042006-10-16 18:09:39 -04001850 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05001851
Mike Christie7996a772006-04-06 21:13:41 -05001852 spin_lock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001853 if (is_leading)
1854 session->leadconn = conn;
Mike Christie98644042006-10-16 18:09:39 -04001855 spin_unlock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001856
1857 /*
1858 * Unblock xmitworker(), Login Phase will pass through.
1859 */
1860 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
1861 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1862 return 0;
1863}
1864EXPORT_SYMBOL_GPL(iscsi_conn_bind);
1865
Mike Christiea54a52c2006-06-28 12:00:23 -05001866
1867int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
1868 enum iscsi_param param, char *buf, int buflen)
1869{
1870 struct iscsi_conn *conn = cls_conn->dd_data;
1871 struct iscsi_session *session = conn->session;
1872 uint32_t value;
1873
1874 switch(param) {
1875 case ISCSI_PARAM_MAX_RECV_DLENGTH:
1876 sscanf(buf, "%d", &conn->max_recv_dlength);
1877 break;
1878 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
1879 sscanf(buf, "%d", &conn->max_xmit_dlength);
1880 break;
1881 case ISCSI_PARAM_HDRDGST_EN:
1882 sscanf(buf, "%d", &conn->hdrdgst_en);
1883 break;
1884 case ISCSI_PARAM_DATADGST_EN:
1885 sscanf(buf, "%d", &conn->datadgst_en);
1886 break;
1887 case ISCSI_PARAM_INITIAL_R2T_EN:
1888 sscanf(buf, "%d", &session->initial_r2t_en);
1889 break;
1890 case ISCSI_PARAM_MAX_R2T:
1891 sscanf(buf, "%d", &session->max_r2t);
1892 break;
1893 case ISCSI_PARAM_IMM_DATA_EN:
1894 sscanf(buf, "%d", &session->imm_data_en);
1895 break;
1896 case ISCSI_PARAM_FIRST_BURST:
1897 sscanf(buf, "%d", &session->first_burst);
1898 break;
1899 case ISCSI_PARAM_MAX_BURST:
1900 sscanf(buf, "%d", &session->max_burst);
1901 break;
1902 case ISCSI_PARAM_PDU_INORDER_EN:
1903 sscanf(buf, "%d", &session->pdu_inorder_en);
1904 break;
1905 case ISCSI_PARAM_DATASEQ_INORDER_EN:
1906 sscanf(buf, "%d", &session->dataseq_inorder_en);
1907 break;
1908 case ISCSI_PARAM_ERL:
1909 sscanf(buf, "%d", &session->erl);
1910 break;
1911 case ISCSI_PARAM_IFMARKER_EN:
1912 sscanf(buf, "%d", &value);
1913 BUG_ON(value);
1914 break;
1915 case ISCSI_PARAM_OFMARKER_EN:
1916 sscanf(buf, "%d", &value);
1917 BUG_ON(value);
1918 break;
1919 case ISCSI_PARAM_EXP_STATSN:
1920 sscanf(buf, "%u", &conn->exp_statsn);
1921 break;
Mike Christieb2c64162007-05-30 12:57:16 -05001922 case ISCSI_PARAM_USERNAME:
1923 kfree(session->username);
1924 session->username = kstrdup(buf, GFP_KERNEL);
1925 if (!session->username)
1926 return -ENOMEM;
1927 break;
1928 case ISCSI_PARAM_USERNAME_IN:
1929 kfree(session->username_in);
1930 session->username_in = kstrdup(buf, GFP_KERNEL);
1931 if (!session->username_in)
1932 return -ENOMEM;
1933 break;
1934 case ISCSI_PARAM_PASSWORD:
1935 kfree(session->password);
1936 session->password = kstrdup(buf, GFP_KERNEL);
1937 if (!session->password)
1938 return -ENOMEM;
1939 break;
1940 case ISCSI_PARAM_PASSWORD_IN:
1941 kfree(session->password_in);
1942 session->password_in = kstrdup(buf, GFP_KERNEL);
1943 if (!session->password_in)
1944 return -ENOMEM;
1945 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05001946 case ISCSI_PARAM_TARGET_NAME:
1947 /* this should not change between logins */
1948 if (session->targetname)
1949 break;
1950
1951 session->targetname = kstrdup(buf, GFP_KERNEL);
1952 if (!session->targetname)
1953 return -ENOMEM;
1954 break;
1955 case ISCSI_PARAM_TPGT:
1956 sscanf(buf, "%d", &session->tpgt);
1957 break;
1958 case ISCSI_PARAM_PERSISTENT_PORT:
1959 sscanf(buf, "%d", &conn->persistent_port);
1960 break;
1961 case ISCSI_PARAM_PERSISTENT_ADDRESS:
1962 /*
1963 * this is the address returned in discovery so it should
1964 * not change between logins.
1965 */
1966 if (conn->persistent_address)
1967 break;
1968
1969 conn->persistent_address = kstrdup(buf, GFP_KERNEL);
1970 if (!conn->persistent_address)
1971 return -ENOMEM;
1972 break;
1973 default:
1974 return -ENOSYS;
1975 }
1976
1977 return 0;
1978}
1979EXPORT_SYMBOL_GPL(iscsi_set_param);
1980
1981int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
1982 enum iscsi_param param, char *buf)
1983{
1984 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
1985 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
1986 int len;
1987
1988 switch(param) {
1989 case ISCSI_PARAM_INITIAL_R2T_EN:
1990 len = sprintf(buf, "%d\n", session->initial_r2t_en);
1991 break;
1992 case ISCSI_PARAM_MAX_R2T:
1993 len = sprintf(buf, "%hu\n", session->max_r2t);
1994 break;
1995 case ISCSI_PARAM_IMM_DATA_EN:
1996 len = sprintf(buf, "%d\n", session->imm_data_en);
1997 break;
1998 case ISCSI_PARAM_FIRST_BURST:
1999 len = sprintf(buf, "%u\n", session->first_burst);
2000 break;
2001 case ISCSI_PARAM_MAX_BURST:
2002 len = sprintf(buf, "%u\n", session->max_burst);
2003 break;
2004 case ISCSI_PARAM_PDU_INORDER_EN:
2005 len = sprintf(buf, "%d\n", session->pdu_inorder_en);
2006 break;
2007 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2008 len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
2009 break;
2010 case ISCSI_PARAM_ERL:
2011 len = sprintf(buf, "%d\n", session->erl);
2012 break;
2013 case ISCSI_PARAM_TARGET_NAME:
2014 len = sprintf(buf, "%s\n", session->targetname);
2015 break;
2016 case ISCSI_PARAM_TPGT:
2017 len = sprintf(buf, "%d\n", session->tpgt);
2018 break;
Mike Christieb2c64162007-05-30 12:57:16 -05002019 case ISCSI_PARAM_USERNAME:
2020 len = sprintf(buf, "%s\n", session->username);
2021 break;
2022 case ISCSI_PARAM_USERNAME_IN:
2023 len = sprintf(buf, "%s\n", session->username_in);
2024 break;
2025 case ISCSI_PARAM_PASSWORD:
2026 len = sprintf(buf, "%s\n", session->password);
2027 break;
2028 case ISCSI_PARAM_PASSWORD_IN:
2029 len = sprintf(buf, "%s\n", session->password_in);
2030 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002031 default:
2032 return -ENOSYS;
2033 }
2034
2035 return len;
2036}
2037EXPORT_SYMBOL_GPL(iscsi_session_get_param);
2038
2039int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
2040 enum iscsi_param param, char *buf)
2041{
2042 struct iscsi_conn *conn = cls_conn->dd_data;
2043 int len;
2044
2045 switch(param) {
2046 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2047 len = sprintf(buf, "%u\n", conn->max_recv_dlength);
2048 break;
2049 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2050 len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
2051 break;
2052 case ISCSI_PARAM_HDRDGST_EN:
2053 len = sprintf(buf, "%d\n", conn->hdrdgst_en);
2054 break;
2055 case ISCSI_PARAM_DATADGST_EN:
2056 len = sprintf(buf, "%d\n", conn->datadgst_en);
2057 break;
2058 case ISCSI_PARAM_IFMARKER_EN:
2059 len = sprintf(buf, "%d\n", conn->ifmarker_en);
2060 break;
2061 case ISCSI_PARAM_OFMARKER_EN:
2062 len = sprintf(buf, "%d\n", conn->ofmarker_en);
2063 break;
2064 case ISCSI_PARAM_EXP_STATSN:
2065 len = sprintf(buf, "%u\n", conn->exp_statsn);
2066 break;
2067 case ISCSI_PARAM_PERSISTENT_PORT:
2068 len = sprintf(buf, "%d\n", conn->persistent_port);
2069 break;
2070 case ISCSI_PARAM_PERSISTENT_ADDRESS:
2071 len = sprintf(buf, "%s\n", conn->persistent_address);
2072 break;
2073 default:
2074 return -ENOSYS;
2075 }
2076
2077 return len;
2078}
2079EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
2080
Mike Christie0801c242007-05-30 12:57:12 -05002081int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2082 char *buf)
2083{
2084 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
2085 int len;
2086
2087 switch (param) {
Mike Christied8196ed2007-05-30 12:57:25 -05002088 case ISCSI_HOST_PARAM_NETDEV_NAME:
2089 if (!session->netdev)
2090 len = sprintf(buf, "%s\n", "default");
2091 else
2092 len = sprintf(buf, "%s\n", session->netdev);
2093 break;
Mike Christie0801c242007-05-30 12:57:12 -05002094 case ISCSI_HOST_PARAM_HWADDRESS:
2095 if (!session->hwaddress)
2096 len = sprintf(buf, "%s\n", "default");
2097 else
2098 len = sprintf(buf, "%s\n", session->hwaddress);
2099 break;
Mike Christie8ad57812007-05-30 12:57:13 -05002100 case ISCSI_HOST_PARAM_INITIATOR_NAME:
2101 if (!session->initiatorname)
2102 len = sprintf(buf, "%s\n", "unknown");
2103 else
2104 len = sprintf(buf, "%s\n", session->initiatorname);
2105 break;
2106
Mike Christie0801c242007-05-30 12:57:12 -05002107 default:
2108 return -ENOSYS;
2109 }
2110
2111 return len;
2112}
2113EXPORT_SYMBOL_GPL(iscsi_host_get_param);
2114
2115int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2116 char *buf, int buflen)
2117{
2118 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
2119
2120 switch (param) {
Mike Christied8196ed2007-05-30 12:57:25 -05002121 case ISCSI_HOST_PARAM_NETDEV_NAME:
2122 if (!session->netdev)
2123 session->netdev = kstrdup(buf, GFP_KERNEL);
2124 break;
Mike Christie0801c242007-05-30 12:57:12 -05002125 case ISCSI_HOST_PARAM_HWADDRESS:
2126 if (!session->hwaddress)
2127 session->hwaddress = kstrdup(buf, GFP_KERNEL);
2128 break;
Mike Christie8ad57812007-05-30 12:57:13 -05002129 case ISCSI_HOST_PARAM_INITIATOR_NAME:
2130 if (!session->initiatorname)
2131 session->initiatorname = kstrdup(buf, GFP_KERNEL);
2132 break;
Mike Christie0801c242007-05-30 12:57:12 -05002133 default:
2134 return -ENOSYS;
2135 }
2136
2137 return 0;
2138}
2139EXPORT_SYMBOL_GPL(iscsi_host_set_param);
2140
Mike Christie7996a772006-04-06 21:13:41 -05002141MODULE_AUTHOR("Mike Christie");
2142MODULE_DESCRIPTION("iSCSI library functions");
2143MODULE_LICENSE("GPL");