blob: 8b57af5baaec7d41c215c645b437af4a725b2acd [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
Mike Christie7996a772006-04-06 21:13:41 -0500294 if (rhdr->flags & ISCSI_FLAG_CMD_UNDERFLOW) {
295 int res_count = be32_to_cpu(rhdr->residual_count);
296
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900297 if (res_count > 0 && res_count <= scsi_bufflen(sc))
298 scsi_set_resid(sc, res_count);
Mike Christie7996a772006-04-06 21:13:41 -0500299 else
300 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
301 } else if (rhdr->flags & ISCSI_FLAG_CMD_BIDI_UNDERFLOW)
302 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
303 else if (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW)
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900304 scsi_set_resid(sc, be32_to_cpu(rhdr->residual_count));
Mike Christie7996a772006-04-06 21:13:41 -0500305
306out:
307 debug_scsi("done [sc %lx res %d itt 0x%x]\n",
308 (long)sc, sc->result, ctask->itt);
309 conn->scsirsp_pdus_cnt++;
310
Mike Christie60ecebf2006-08-31 18:09:25 -0400311 __iscsi_put_ctask(ctask);
Mike Christie7996a772006-04-06 21:13:41 -0500312}
313
Mike Christie7ea8b822006-07-24 15:47:22 -0500314static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
315{
316 struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
317
318 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
319 conn->tmfrsp_pdus_cnt++;
320
321 if (conn->tmabort_state != TMABORT_INITIAL)
322 return;
323
324 if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
325 conn->tmabort_state = TMABORT_SUCCESS;
326 else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
327 conn->tmabort_state = TMABORT_NOT_FOUND;
328 else
329 conn->tmabort_state = TMABORT_FAILED;
330 wake_up(&conn->ehwait);
331}
332
Mike Christie62f38302006-08-31 18:09:27 -0400333static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
334 char *data, int datalen)
335{
336 struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
337 struct iscsi_hdr rejected_pdu;
338 uint32_t itt;
339
340 conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
341
342 if (reject->reason == ISCSI_REASON_DATA_DIGEST_ERROR) {
343 if (ntoh24(reject->dlength) > datalen)
344 return ISCSI_ERR_PROTO;
345
346 if (ntoh24(reject->dlength) >= sizeof(struct iscsi_hdr)) {
347 memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
Al Virob4377352007-02-09 16:39:40 +0000348 itt = get_itt(rejected_pdu.itt);
Mike Christie62f38302006-08-31 18:09:27 -0400349 printk(KERN_ERR "itt 0x%x had pdu (op 0x%x) rejected "
350 "due to DataDigest error.\n", itt,
351 rejected_pdu.opcode);
352 }
353 }
354 return 0;
355}
356
Mike Christie7996a772006-04-06 21:13:41 -0500357/**
358 * __iscsi_complete_pdu - complete pdu
359 * @conn: iscsi conn
360 * @hdr: iscsi header
361 * @data: data buffer
362 * @datalen: len of data buffer
363 *
364 * Completes pdu processing by freeing any resources allocated at
365 * queuecommand or send generic. session lock must be held and verify
366 * itt must have been called.
367 */
368int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
369 char *data, int datalen)
370{
371 struct iscsi_session *session = conn->session;
372 int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
373 struct iscsi_cmd_task *ctask;
374 struct iscsi_mgmt_task *mtask;
375 uint32_t itt;
376
Al Virob4377352007-02-09 16:39:40 +0000377 if (hdr->itt != RESERVED_ITT)
378 itt = get_itt(hdr->itt);
Mike Christie7996a772006-04-06 21:13:41 -0500379 else
Al Virob4377352007-02-09 16:39:40 +0000380 itt = ~0U;
Mike Christie7996a772006-04-06 21:13:41 -0500381
382 if (itt < session->cmds_max) {
383 ctask = session->cmds[itt];
384
385 debug_scsi("cmdrsp [op 0x%x cid %d itt 0x%x len %d]\n",
386 opcode, conn->id, ctask->itt, datalen);
387
388 switch(opcode) {
389 case ISCSI_OP_SCSI_CMD_RSP:
390 BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
Mike Christie77a23c22007-05-30 12:57:18 -0500391 iscsi_scsi_cmd_rsp(conn, hdr, ctask, data,
392 datalen);
Mike Christie7996a772006-04-06 21:13:41 -0500393 break;
394 case ISCSI_OP_SCSI_DATA_IN:
395 BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
396 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
397 conn->scsirsp_pdus_cnt++;
Mike Christie60ecebf2006-08-31 18:09:25 -0400398 __iscsi_put_ctask(ctask);
Mike Christie7996a772006-04-06 21:13:41 -0500399 }
400 break;
401 case ISCSI_OP_R2T:
402 /* LLD handles this for now */
403 break;
404 default:
405 rc = ISCSI_ERR_BAD_OPCODE;
406 break;
407 }
408 } else if (itt >= ISCSI_MGMT_ITT_OFFSET &&
409 itt < ISCSI_MGMT_ITT_OFFSET + session->mgmtpool_max) {
410 mtask = session->mgmt_cmds[itt - ISCSI_MGMT_ITT_OFFSET];
411
412 debug_scsi("immrsp [op 0x%x cid %d itt 0x%x len %d]\n",
413 opcode, conn->id, mtask->itt, datalen);
414
Mike Christie77a23c22007-05-30 12:57:18 -0500415 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
Mike Christie7996a772006-04-06 21:13:41 -0500416 switch(opcode) {
Mike Christie8d2860b2006-05-02 19:46:47 -0500417 case ISCSI_OP_LOGOUT_RSP:
Mike Christiec8dc1e52006-07-24 15:47:39 -0500418 if (datalen) {
419 rc = ISCSI_ERR_PROTO;
420 break;
421 }
Mike Christie8d2860b2006-05-02 19:46:47 -0500422 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
423 /* fall through */
Mike Christie7996a772006-04-06 21:13:41 -0500424 case ISCSI_OP_LOGIN_RSP:
425 case ISCSI_OP_TEXT_RSP:
Mike Christie8d2860b2006-05-02 19:46:47 -0500426 /*
427 * login related PDU's exp_statsn is handled in
428 * userspace
429 */
Mike Christie40527af2006-07-24 15:47:45 -0500430 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
431 rc = ISCSI_ERR_CONN_FAILED;
Mike Christie7996a772006-04-06 21:13:41 -0500432 list_del(&mtask->running);
433 if (conn->login_mtask != mtask)
434 __kfifo_put(session->mgmtpool.queue,
435 (void*)&mtask, sizeof(void*));
436 break;
437 case ISCSI_OP_SCSI_TMFUNC_RSP:
Mike Christie7996a772006-04-06 21:13:41 -0500438 if (datalen) {
439 rc = ISCSI_ERR_PROTO;
440 break;
441 }
Mike Christie8d2860b2006-05-02 19:46:47 -0500442
Mike Christie7ea8b822006-07-24 15:47:22 -0500443 iscsi_tmf_rsp(conn, hdr);
Mike Christie7996a772006-04-06 21:13:41 -0500444 break;
445 case ISCSI_OP_NOOP_IN:
Al Virob4377352007-02-09 16:39:40 +0000446 if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) || datalen) {
Mike Christie7996a772006-04-06 21:13:41 -0500447 rc = ISCSI_ERR_PROTO;
448 break;
449 }
Mike Christie7996a772006-04-06 21:13:41 -0500450 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
451
Mike Christie40527af2006-07-24 15:47:45 -0500452 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
453 rc = ISCSI_ERR_CONN_FAILED;
Mike Christie7996a772006-04-06 21:13:41 -0500454 list_del(&mtask->running);
455 if (conn->login_mtask != mtask)
456 __kfifo_put(session->mgmtpool.queue,
457 (void*)&mtask, sizeof(void*));
458 break;
459 default:
460 rc = ISCSI_ERR_BAD_OPCODE;
461 break;
462 }
Al Virob4377352007-02-09 16:39:40 +0000463 } else if (itt == ~0U) {
Mike Christie77a23c22007-05-30 12:57:18 -0500464 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
Mike Christie62f38302006-08-31 18:09:27 -0400465
Mike Christie7996a772006-04-06 21:13:41 -0500466 switch(opcode) {
467 case ISCSI_OP_NOOP_IN:
Mike Christie40527af2006-07-24 15:47:45 -0500468 if (datalen) {
Mike Christie7996a772006-04-06 21:13:41 -0500469 rc = ISCSI_ERR_PROTO;
Mike Christie40527af2006-07-24 15:47:45 -0500470 break;
471 }
472
Al Virob4377352007-02-09 16:39:40 +0000473 if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
Mike Christie40527af2006-07-24 15:47:45 -0500474 break;
475
476 if (iscsi_recv_pdu(conn->cls_conn, hdr, NULL, 0))
477 rc = ISCSI_ERR_CONN_FAILED;
Mike Christie7996a772006-04-06 21:13:41 -0500478 break;
479 case ISCSI_OP_REJECT:
Mike Christie62f38302006-08-31 18:09:27 -0400480 rc = iscsi_handle_reject(conn, hdr, data, datalen);
481 break;
Mike Christie7996a772006-04-06 21:13:41 -0500482 case ISCSI_OP_ASYNC_EVENT:
Mike Christie8d2860b2006-05-02 19:46:47 -0500483 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
Mike Christie5831c732006-10-16 18:09:41 -0400484 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
485 rc = ISCSI_ERR_CONN_FAILED;
Mike Christie7996a772006-04-06 21:13:41 -0500486 break;
487 default:
488 rc = ISCSI_ERR_BAD_OPCODE;
489 break;
490 }
491 } else
492 rc = ISCSI_ERR_BAD_ITT;
493
494 return rc;
495}
496EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
497
498int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
499 char *data, int datalen)
500{
501 int rc;
502
503 spin_lock(&conn->session->lock);
504 rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
505 spin_unlock(&conn->session->lock);
506 return rc;
507}
508EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
509
510/* verify itt (itt encoding: age+cid+itt) */
511int iscsi_verify_itt(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
512 uint32_t *ret_itt)
513{
514 struct iscsi_session *session = conn->session;
515 struct iscsi_cmd_task *ctask;
516 uint32_t itt;
517
Al Virob4377352007-02-09 16:39:40 +0000518 if (hdr->itt != RESERVED_ITT) {
519 if (((__force u32)hdr->itt & ISCSI_AGE_MASK) !=
Mike Christie7996a772006-04-06 21:13:41 -0500520 (session->age << ISCSI_AGE_SHIFT)) {
Or Gerlitzbe2df722006-05-02 19:46:43 -0500521 printk(KERN_ERR "iscsi: received itt %x expected "
Al Virob4377352007-02-09 16:39:40 +0000522 "session age (%x)\n", (__force u32)hdr->itt,
Mike Christie7996a772006-04-06 21:13:41 -0500523 session->age & ISCSI_AGE_MASK);
524 return ISCSI_ERR_BAD_ITT;
525 }
526
Al Virob4377352007-02-09 16:39:40 +0000527 if (((__force u32)hdr->itt & ISCSI_CID_MASK) !=
Mike Christie7996a772006-04-06 21:13:41 -0500528 (conn->id << ISCSI_CID_SHIFT)) {
Or Gerlitzbe2df722006-05-02 19:46:43 -0500529 printk(KERN_ERR "iscsi: received itt %x, expected "
Al Virob4377352007-02-09 16:39:40 +0000530 "CID (%x)\n", (__force u32)hdr->itt, conn->id);
Mike Christie7996a772006-04-06 21:13:41 -0500531 return ISCSI_ERR_BAD_ITT;
532 }
Al Virob4377352007-02-09 16:39:40 +0000533 itt = get_itt(hdr->itt);
Mike Christie7996a772006-04-06 21:13:41 -0500534 } else
Al Virob4377352007-02-09 16:39:40 +0000535 itt = ~0U;
Mike Christie7996a772006-04-06 21:13:41 -0500536
537 if (itt < session->cmds_max) {
538 ctask = session->cmds[itt];
539
540 if (!ctask->sc) {
Or Gerlitzbe2df722006-05-02 19:46:43 -0500541 printk(KERN_INFO "iscsi: dropping ctask with "
Mike Christie7996a772006-04-06 21:13:41 -0500542 "itt 0x%x\n", ctask->itt);
543 /* force drop */
544 return ISCSI_ERR_NO_SCSI_CMD;
545 }
546
547 if (ctask->sc->SCp.phase != session->age) {
Or Gerlitzbe2df722006-05-02 19:46:43 -0500548 printk(KERN_ERR "iscsi: ctask's session age %d, "
Mike Christie7996a772006-04-06 21:13:41 -0500549 "expected %d\n", ctask->sc->SCp.phase,
550 session->age);
551 return ISCSI_ERR_SESSION_FAILED;
552 }
553 }
554
555 *ret_itt = itt;
556 return 0;
557}
558EXPORT_SYMBOL_GPL(iscsi_verify_itt);
559
560void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
561{
562 struct iscsi_session *session = conn->session;
563 unsigned long flags;
564
565 spin_lock_irqsave(&session->lock, flags);
Mike Christie656cffc2006-05-18 20:31:42 -0500566 if (session->state == ISCSI_STATE_FAILED) {
567 spin_unlock_irqrestore(&session->lock, flags);
568 return;
569 }
570
Mike Christie67a61112006-05-30 00:37:20 -0500571 if (conn->stop_stage == 0)
Mike Christie7996a772006-04-06 21:13:41 -0500572 session->state = ISCSI_STATE_FAILED;
573 spin_unlock_irqrestore(&session->lock, flags);
574 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
575 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
576 iscsi_conn_error(conn->cls_conn, err);
577}
578EXPORT_SYMBOL_GPL(iscsi_conn_failure);
579
Mike Christie77a23c22007-05-30 12:57:18 -0500580static void iscsi_prep_mtask(struct iscsi_conn *conn,
581 struct iscsi_mgmt_task *mtask)
582{
583 struct iscsi_session *session = conn->session;
584 struct iscsi_hdr *hdr = mtask->hdr;
585 struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
586
587 if (hdr->opcode != (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) &&
588 hdr->opcode != (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
589 nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
590 /*
591 * pre-format CmdSN for outgoing PDU.
592 */
593 nop->cmdsn = cpu_to_be32(session->cmdsn);
594 if (hdr->itt != RESERVED_ITT) {
595 hdr->itt = build_itt(mtask->itt, conn->id, session->age);
Mike Christiee0726402007-07-26 12:46:48 -0500596 /*
597 * TODO: We always use immediate, so we never hit this.
598 * If we start to send tmfs or nops as non-immediate then
599 * we should start checking the cmdsn numbers for mgmt tasks.
600 */
Mike Christie77a23c22007-05-30 12:57:18 -0500601 if (conn->c_stage == ISCSI_CONN_STARTED &&
Mike Christiee0726402007-07-26 12:46:48 -0500602 !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
603 session->queued_cmdsn++;
Mike Christie77a23c22007-05-30 12:57:18 -0500604 session->cmdsn++;
Mike Christiee0726402007-07-26 12:46:48 -0500605 }
Mike Christie77a23c22007-05-30 12:57:18 -0500606 }
607
608 if (session->tt->init_mgmt_task)
609 session->tt->init_mgmt_task(conn, mtask);
610
611 debug_scsi("mgmtpdu [op 0x%x hdr->itt 0x%x datalen %d]\n",
612 hdr->opcode, hdr->itt, mtask->data_count);
613}
614
Mike Christie05db8882007-02-28 17:32:16 -0600615static int iscsi_xmit_mtask(struct iscsi_conn *conn)
Mike Christieb5072ea2006-10-16 18:09:42 -0400616{
617 struct iscsi_hdr *hdr = conn->mtask->hdr;
618 int rc, was_logout = 0;
619
Mike Christie77a23c22007-05-30 12:57:18 -0500620 spin_unlock_bh(&conn->session->lock);
Mike Christieb5072ea2006-10-16 18:09:42 -0400621 if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT) {
622 conn->session->state = ISCSI_STATE_IN_RECOVERY;
623 iscsi_block_session(session_to_cls(conn->session));
624 was_logout = 1;
625 }
626 rc = conn->session->tt->xmit_mgmt_task(conn, conn->mtask);
Mike Christie77a23c22007-05-30 12:57:18 -0500627 spin_lock_bh(&conn->session->lock);
Mike Christieb5072ea2006-10-16 18:09:42 -0400628 if (rc)
629 return rc;
630
Mike Christie05db8882007-02-28 17:32:16 -0600631 /* done with this in-progress mtask */
632 conn->mtask = NULL;
633
Mike Christieb5072ea2006-10-16 18:09:42 -0400634 if (was_logout) {
635 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
636 return -ENODATA;
637 }
638 return 0;
639}
640
Mike Christie77a23c22007-05-30 12:57:18 -0500641static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
642{
643 struct iscsi_session *session = conn->session;
644
645 /*
646 * Check for iSCSI window and take care of CmdSN wrap-around
647 */
Mike Christiee0726402007-07-26 12:46:48 -0500648 if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) {
649 debug_scsi("iSCSI CmdSN closed. ExpCmdSn %u MaxCmdSN %u "
650 "CmdSN %u/%u\n", session->exp_cmdsn,
651 session->max_cmdsn, session->cmdsn,
652 session->queued_cmdsn);
Mike Christie77a23c22007-05-30 12:57:18 -0500653 return -ENOSPC;
654 }
655 return 0;
656}
657
658static int iscsi_xmit_ctask(struct iscsi_conn *conn)
659{
660 struct iscsi_cmd_task *ctask = conn->ctask;
661 int rc = 0;
662
663 /*
664 * serialize with TMF AbortTask
665 */
666 if (ctask->state == ISCSI_TASK_ABORTING)
667 goto done;
668
669 __iscsi_get_ctask(ctask);
670 spin_unlock_bh(&conn->session->lock);
671 rc = conn->session->tt->xmit_cmd_task(conn, ctask);
672 spin_lock_bh(&conn->session->lock);
673 __iscsi_put_ctask(ctask);
674
675done:
676 if (!rc)
677 /* done with this ctask */
678 conn->ctask = NULL;
679 return rc;
680}
681
Mike Christie7996a772006-04-06 21:13:41 -0500682/**
683 * iscsi_data_xmit - xmit any command into the scheduled connection
684 * @conn: iscsi connection
685 *
686 * Notes:
687 * The function can return -EAGAIN in which case the caller must
688 * re-schedule it again later or recover. '0' return code means
689 * successful xmit.
690 **/
691static int iscsi_data_xmit(struct iscsi_conn *conn)
692{
Mike Christie3219e522006-05-30 00:37:28 -0500693 int rc = 0;
Mike Christie7996a772006-04-06 21:13:41 -0500694
Mike Christie77a23c22007-05-30 12:57:18 -0500695 spin_lock_bh(&conn->session->lock);
Mike Christie7996a772006-04-06 21:13:41 -0500696 if (unlikely(conn->suspend_tx)) {
697 debug_scsi("conn %d Tx suspended!\n", conn->id);
Mike Christie77a23c22007-05-30 12:57:18 -0500698 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -0500699 return -ENODATA;
Mike Christie7996a772006-04-06 21:13:41 -0500700 }
Mike Christie7996a772006-04-06 21:13:41 -0500701
702 if (conn->ctask) {
Mike Christie77a23c22007-05-30 12:57:18 -0500703 rc = iscsi_xmit_ctask(conn);
Mike Christie3219e522006-05-30 00:37:28 -0500704 if (rc)
Mike Christie7996a772006-04-06 21:13:41 -0500705 goto again;
Mike Christie7996a772006-04-06 21:13:41 -0500706 }
Mike Christie77a23c22007-05-30 12:57:18 -0500707
Mike Christie7996a772006-04-06 21:13:41 -0500708 if (conn->mtask) {
Mike Christie05db8882007-02-28 17:32:16 -0600709 rc = iscsi_xmit_mtask(conn);
Mike Christie3219e522006-05-30 00:37:28 -0500710 if (rc)
Mike Christie7996a772006-04-06 21:13:41 -0500711 goto again;
Mike Christie7996a772006-04-06 21:13:41 -0500712 }
713
Mike Christie77a23c22007-05-30 12:57:18 -0500714 /*
715 * process mgmt pdus like nops before commands since we should
716 * only have one nop-out as a ping from us and targets should not
717 * overflow us with nop-ins
718 */
719check_mgmt:
720 while (__kfifo_get(conn->mgmtqueue, (void*)&conn->mtask,
721 sizeof(void*))) {
722 iscsi_prep_mtask(conn, conn->mtask);
723 list_add_tail(&conn->mtask->running, &conn->mgmt_run_list);
724 rc = iscsi_xmit_mtask(conn);
725 if (rc)
726 goto again;
Mike Christie7996a772006-04-06 21:13:41 -0500727 }
728
729 /* process command queue */
Mike Christieb6c395e2006-07-24 15:47:15 -0500730 while (!list_empty(&conn->xmitqueue)) {
Mike Christie7996a772006-04-06 21:13:41 -0500731 /*
732 * iscsi tcp may readd the task to the xmitqueue to send
733 * write data
734 */
Mike Christieb6c395e2006-07-24 15:47:15 -0500735 conn->ctask = list_entry(conn->xmitqueue.next,
736 struct iscsi_cmd_task, running);
Mike Christie96809f12007-08-15 01:38:29 -0500737 switch (conn->ctask->state) {
738 case ISCSI_TASK_ABORTING:
739 break;
740 case ISCSI_TASK_PENDING:
Mike Christie77a23c22007-05-30 12:57:18 -0500741 iscsi_prep_scsi_cmd_pdu(conn->ctask);
742 conn->session->tt->init_cmd_task(conn->ctask);
Mike Christie96809f12007-08-15 01:38:29 -0500743 /* fall through */
744 default:
745 conn->ctask->state = ISCSI_TASK_RUNNING;
746 break;
Mike Christie77a23c22007-05-30 12:57:18 -0500747 }
Mike Christieb6c395e2006-07-24 15:47:15 -0500748 list_move_tail(conn->xmitqueue.next, &conn->run_list);
Mike Christie96809f12007-08-15 01:38:29 -0500749
Mike Christie77a23c22007-05-30 12:57:18 -0500750 rc = iscsi_xmit_ctask(conn);
751 if (rc)
Mike Christie60ecebf2006-08-31 18:09:25 -0400752 goto again;
Mike Christie77a23c22007-05-30 12:57:18 -0500753 /*
754 * we could continuously get new ctask requests so
755 * we need to check the mgmt queue for nops that need to
756 * be sent to aviod starvation
757 */
758 if (__kfifo_len(conn->mgmtqueue))
759 goto check_mgmt;
Mike Christie7996a772006-04-06 21:13:41 -0500760 }
Mike Christieb6c395e2006-07-24 15:47:15 -0500761 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -0500762 return -ENODATA;
Mike Christie7996a772006-04-06 21:13:41 -0500763
764again:
765 if (unlikely(conn->suspend_tx))
Mike Christie77a23c22007-05-30 12:57:18 -0500766 rc = -ENODATA;
767 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -0500768 return rc;
Mike Christie7996a772006-04-06 21:13:41 -0500769}
770
David Howellsc4028952006-11-22 14:57:56 +0000771static void iscsi_xmitworker(struct work_struct *work)
Mike Christie7996a772006-04-06 21:13:41 -0500772{
David Howellsc4028952006-11-22 14:57:56 +0000773 struct iscsi_conn *conn =
774 container_of(work, struct iscsi_conn, xmitwork);
Mike Christie3219e522006-05-30 00:37:28 -0500775 int rc;
Mike Christie7996a772006-04-06 21:13:41 -0500776 /*
777 * serialize Xmit worker on a per-connection basis.
778 */
Mike Christie3219e522006-05-30 00:37:28 -0500779 do {
780 rc = iscsi_data_xmit(conn);
781 } while (rc >= 0 || rc == -EAGAIN);
Mike Christie7996a772006-04-06 21:13:41 -0500782}
783
784enum {
785 FAILURE_BAD_HOST = 1,
786 FAILURE_SESSION_FAILED,
787 FAILURE_SESSION_FREED,
788 FAILURE_WINDOW_CLOSED,
Mike Christie60ecebf2006-08-31 18:09:25 -0400789 FAILURE_OOM,
Mike Christie7996a772006-04-06 21:13:41 -0500790 FAILURE_SESSION_TERMINATE,
Mike Christie656cffc2006-05-18 20:31:42 -0500791 FAILURE_SESSION_IN_RECOVERY,
Mike Christie7996a772006-04-06 21:13:41 -0500792 FAILURE_SESSION_RECOVERY_TIMEOUT,
793};
794
795int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
796{
797 struct Scsi_Host *host;
798 int reason = 0;
799 struct iscsi_session *session;
800 struct iscsi_conn *conn;
801 struct iscsi_cmd_task *ctask = NULL;
802
803 sc->scsi_done = done;
804 sc->result = 0;
Mike Christief47f2cf2006-08-31 18:09:33 -0400805 sc->SCp.ptr = NULL;
Mike Christie7996a772006-04-06 21:13:41 -0500806
807 host = sc->device->host;
808 session = iscsi_hostdata(host->hostdata);
809
810 spin_lock(&session->lock);
811
Mike Christie656cffc2006-05-18 20:31:42 -0500812 /*
813 * ISCSI_STATE_FAILED is a temp. state. The recovery
814 * code will decide what is best to do with command queued
815 * during this time
816 */
817 if (session->state != ISCSI_STATE_LOGGED_IN &&
818 session->state != ISCSI_STATE_FAILED) {
819 /*
820 * to handle the race between when we set the recovery state
821 * and block the session we requeue here (commands could
822 * be entering our queuecommand while a block is starting
823 * up because the block code is not locked)
824 */
825 if (session->state == ISCSI_STATE_IN_RECOVERY) {
826 reason = FAILURE_SESSION_IN_RECOVERY;
Mike Christie67a61112006-05-30 00:37:20 -0500827 goto reject;
Mike Christie7996a772006-04-06 21:13:41 -0500828 }
Mike Christie656cffc2006-05-18 20:31:42 -0500829
830 if (session->state == ISCSI_STATE_RECOVERY_FAILED)
831 reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
832 else if (session->state == ISCSI_STATE_TERMINATE)
833 reason = FAILURE_SESSION_TERMINATE;
834 else
835 reason = FAILURE_SESSION_FREED;
Mike Christie7996a772006-04-06 21:13:41 -0500836 goto fault;
837 }
838
Mike Christie7996a772006-04-06 21:13:41 -0500839 conn = session->leadconn;
Mike Christie98644042006-10-16 18:09:39 -0400840 if (!conn) {
841 reason = FAILURE_SESSION_FREED;
842 goto fault;
843 }
Mike Christie7996a772006-04-06 21:13:41 -0500844
Mike Christie77a23c22007-05-30 12:57:18 -0500845 if (iscsi_check_cmdsn_window_closed(conn)) {
846 reason = FAILURE_WINDOW_CLOSED;
847 goto reject;
848 }
849
Mike Christie60ecebf2006-08-31 18:09:25 -0400850 if (!__kfifo_get(session->cmdpool.queue, (void*)&ctask,
851 sizeof(void*))) {
852 reason = FAILURE_OOM;
853 goto reject;
854 }
Mike Christiee0726402007-07-26 12:46:48 -0500855 session->queued_cmdsn++;
856
Mike Christie7996a772006-04-06 21:13:41 -0500857 sc->SCp.phase = session->age;
858 sc->SCp.ptr = (char *)ctask;
859
Mike Christie60ecebf2006-08-31 18:09:25 -0400860 atomic_set(&ctask->refcount, 1);
Mike Christieb6c395e2006-07-24 15:47:15 -0500861 ctask->state = ISCSI_TASK_PENDING;
Mike Christie7996a772006-04-06 21:13:41 -0500862 ctask->mtask = NULL;
863 ctask->conn = conn;
864 ctask->sc = sc;
865 INIT_LIST_HEAD(&ctask->running);
Mike Christie7996a772006-04-06 21:13:41 -0500866
Mike Christieb6c395e2006-07-24 15:47:15 -0500867 list_add_tail(&ctask->running, &conn->xmitqueue);
Mike Christie7996a772006-04-06 21:13:41 -0500868 spin_unlock(&session->lock);
869
870 scsi_queue_work(host, &conn->xmitwork);
871 return 0;
872
873reject:
874 spin_unlock(&session->lock);
875 debug_scsi("cmd 0x%x rejected (%d)\n", sc->cmnd[0], reason);
876 return SCSI_MLQUEUE_HOST_BUSY;
877
878fault:
879 spin_unlock(&session->lock);
Or Gerlitzbe2df722006-05-02 19:46:43 -0500880 printk(KERN_ERR "iscsi: cmd 0x%x is not queued (%d)\n",
Mike Christie7996a772006-04-06 21:13:41 -0500881 sc->cmnd[0], reason);
882 sc->result = (DID_NO_CONNECT << 16);
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900883 scsi_set_resid(sc, scsi_bufflen(sc));
Mike Christie7996a772006-04-06 21:13:41 -0500884 sc->scsi_done(sc);
885 return 0;
886}
887EXPORT_SYMBOL_GPL(iscsi_queuecommand);
888
889int iscsi_change_queue_depth(struct scsi_device *sdev, int depth)
890{
891 if (depth > ISCSI_MAX_CMD_PER_LUN)
892 depth = ISCSI_MAX_CMD_PER_LUN;
893 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
894 return sdev->queue_depth;
895}
896EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
897
Mike Christie77a23c22007-05-30 12:57:18 -0500898static struct iscsi_mgmt_task *
899__iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
900 char *data, uint32_t data_size)
Mike Christie7996a772006-04-06 21:13:41 -0500901{
902 struct iscsi_session *session = conn->session;
Mike Christie7996a772006-04-06 21:13:41 -0500903 struct iscsi_mgmt_task *mtask;
904
Mike Christie77a23c22007-05-30 12:57:18 -0500905 if (session->state == ISCSI_STATE_TERMINATE)
906 return NULL;
907
Mike Christie7996a772006-04-06 21:13:41 -0500908 if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) ||
909 hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
910 /*
911 * Login and Text are sent serially, in
912 * request-followed-by-response sequence.
913 * Same mtask can be used. Same ITT must be used.
914 * Note that login_mtask is preallocated at conn_create().
915 */
916 mtask = conn->login_mtask;
917 else {
Mike Christie656cffc2006-05-18 20:31:42 -0500918 BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
919 BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
Mike Christie7996a772006-04-06 21:13:41 -0500920
921 if (!__kfifo_get(session->mgmtpool.queue,
Mike Christie77a23c22007-05-30 12:57:18 -0500922 (void*)&mtask, sizeof(void*)))
923 return NULL;
Mike Christie7996a772006-04-06 21:13:41 -0500924 }
925
Mike Christie7996a772006-04-06 21:13:41 -0500926 if (data_size) {
927 memcpy(mtask->data, data, data_size);
928 mtask->data_count = data_size;
929 } else
930 mtask->data_count = 0;
931
932 INIT_LIST_HEAD(&mtask->running);
933 memcpy(mtask->hdr, hdr, sizeof(struct iscsi_hdr));
Mike Christie77a23c22007-05-30 12:57:18 -0500934 __kfifo_put(conn->mgmtqueue, (void*)&mtask, sizeof(void*));
935 return mtask;
Mike Christie7996a772006-04-06 21:13:41 -0500936}
937
938int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
939 char *data, uint32_t data_size)
940{
941 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie77a23c22007-05-30 12:57:18 -0500942 struct iscsi_session *session = conn->session;
943 int err = 0;
Mike Christie7996a772006-04-06 21:13:41 -0500944
Mike Christie77a23c22007-05-30 12:57:18 -0500945 spin_lock_bh(&session->lock);
946 if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size))
947 err = -EPERM;
948 spin_unlock_bh(&session->lock);
949 scsi_queue_work(session->host, &conn->xmitwork);
950 return err;
Mike Christie7996a772006-04-06 21:13:41 -0500951}
952EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
953
954void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
955{
956 struct iscsi_session *session = class_to_transport_session(cls_session);
957 struct iscsi_conn *conn = session->leadconn;
958
959 spin_lock_bh(&session->lock);
960 if (session->state != ISCSI_STATE_LOGGED_IN) {
Mike Christie656cffc2006-05-18 20:31:42 -0500961 session->state = ISCSI_STATE_RECOVERY_FAILED;
Mike Christie7996a772006-04-06 21:13:41 -0500962 if (conn)
963 wake_up(&conn->ehwait);
964 }
965 spin_unlock_bh(&session->lock);
966}
967EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
968
969int iscsi_eh_host_reset(struct scsi_cmnd *sc)
970{
971 struct Scsi_Host *host = sc->device->host;
972 struct iscsi_session *session = iscsi_hostdata(host->hostdata);
973 struct iscsi_conn *conn = session->leadconn;
974 int fail_session = 0;
975
976 spin_lock_bh(&session->lock);
977 if (session->state == ISCSI_STATE_TERMINATE) {
978failed:
979 debug_scsi("failing host reset: session terminated "
Pete Wyckoffd6e24d12006-11-08 15:58:32 -0600980 "[CID %d age %d]\n", conn->id, session->age);
Mike Christie7996a772006-04-06 21:13:41 -0500981 spin_unlock_bh(&session->lock);
982 return FAILED;
983 }
984
985 if (sc->SCp.phase == session->age) {
Pete Wyckoffd6e24d12006-11-08 15:58:32 -0600986 debug_scsi("failing connection CID %d due to SCSI host reset\n",
Mike Christie7996a772006-04-06 21:13:41 -0500987 conn->id);
988 fail_session = 1;
989 }
990 spin_unlock_bh(&session->lock);
991
992 /*
993 * we drop the lock here but the leadconn cannot be destoyed while
994 * we are in the scsi eh
995 */
Mike Christie656cffc2006-05-18 20:31:42 -0500996 if (fail_session)
Mike Christie7996a772006-04-06 21:13:41 -0500997 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie7996a772006-04-06 21:13:41 -0500998
999 debug_scsi("iscsi_eh_host_reset wait for relogin\n");
1000 wait_event_interruptible(conn->ehwait,
1001 session->state == ISCSI_STATE_TERMINATE ||
1002 session->state == ISCSI_STATE_LOGGED_IN ||
Mike Christie656cffc2006-05-18 20:31:42 -05001003 session->state == ISCSI_STATE_RECOVERY_FAILED);
Mike Christie7996a772006-04-06 21:13:41 -05001004 if (signal_pending(current))
1005 flush_signals(current);
1006
1007 spin_lock_bh(&session->lock);
1008 if (session->state == ISCSI_STATE_LOGGED_IN)
Or Gerlitzbe2df722006-05-02 19:46:43 -05001009 printk(KERN_INFO "iscsi: host reset succeeded\n");
Mike Christie7996a772006-04-06 21:13:41 -05001010 else
1011 goto failed;
1012 spin_unlock_bh(&session->lock);
1013
1014 return SUCCESS;
1015}
1016EXPORT_SYMBOL_GPL(iscsi_eh_host_reset);
1017
1018static void iscsi_tmabort_timedout(unsigned long data)
1019{
1020 struct iscsi_cmd_task *ctask = (struct iscsi_cmd_task *)data;
1021 struct iscsi_conn *conn = ctask->conn;
1022 struct iscsi_session *session = conn->session;
1023
1024 spin_lock(&session->lock);
1025 if (conn->tmabort_state == TMABORT_INITIAL) {
1026 conn->tmabort_state = TMABORT_TIMEDOUT;
1027 debug_scsi("tmabort timedout [sc %p itt 0x%x]\n",
1028 ctask->sc, ctask->itt);
1029 /* unblock eh_abort() */
1030 wake_up(&conn->ehwait);
1031 }
1032 spin_unlock(&session->lock);
1033}
1034
Mike Christie7996a772006-04-06 21:13:41 -05001035static int iscsi_exec_abort_task(struct scsi_cmnd *sc,
1036 struct iscsi_cmd_task *ctask)
1037{
1038 struct iscsi_conn *conn = ctask->conn;
1039 struct iscsi_session *session = conn->session;
1040 struct iscsi_tm *hdr = &conn->tmhdr;
Mike Christie7996a772006-04-06 21:13:41 -05001041
1042 /*
1043 * ctask timed out but session is OK requests must be serialized.
1044 */
1045 memset(hdr, 0, sizeof(struct iscsi_tm));
1046 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
1047 hdr->flags = ISCSI_TM_FUNC_ABORT_TASK;
1048 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
1049 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
1050 hdr->rtt = ctask->hdr->itt;
1051 hdr->refcmdsn = ctask->hdr->cmdsn;
1052
Mike Christie77a23c22007-05-30 12:57:18 -05001053 ctask->mtask = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
1054 NULL, 0);
1055 if (!ctask->mtask) {
Mike Christie6724add2007-08-15 01:38:30 -05001056 spin_unlock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001057 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie6724add2007-08-15 01:38:30 -05001058 spin_lock_bh(&session->lock)
Mike Christie77a23c22007-05-30 12:57:18 -05001059 debug_scsi("abort sent failure [itt 0x%x]\n", ctask->itt);
1060 return -EPERM;
Mike Christie7996a772006-04-06 21:13:41 -05001061 }
Mike Christie77a23c22007-05-30 12:57:18 -05001062 ctask->state = ISCSI_TASK_ABORTING;
Mike Christie7996a772006-04-06 21:13:41 -05001063
1064 debug_scsi("abort sent [itt 0x%x]\n", ctask->itt);
1065
Mike Christie7996a772006-04-06 21:13:41 -05001066 if (conn->tmabort_state == TMABORT_INITIAL) {
1067 conn->tmfcmd_pdus_cnt++;
Mike Christie77a23c22007-05-30 12:57:18 -05001068 conn->tmabort_timer.expires = 20*HZ + jiffies;
Mike Christie7996a772006-04-06 21:13:41 -05001069 conn->tmabort_timer.function = iscsi_tmabort_timedout;
1070 conn->tmabort_timer.data = (unsigned long)ctask;
1071 add_timer(&conn->tmabort_timer);
Pete Wyckoffd6e24d12006-11-08 15:58:32 -06001072 debug_scsi("abort set timeout [itt 0x%x]\n", ctask->itt);
Mike Christie7996a772006-04-06 21:13:41 -05001073 }
1074 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05001075 mutex_unlock(&session->eh_mutex);
Mike Christie77a23c22007-05-30 12:57:18 -05001076 scsi_queue_work(session->host, &conn->xmitwork);
Mike Christie7996a772006-04-06 21:13:41 -05001077
1078 /*
1079 * block eh thread until:
1080 *
1081 * 1) abort response
1082 * 2) abort timeout
1083 * 3) session is terminated or restarted or userspace has
1084 * given up on recovery
1085 */
1086 wait_event_interruptible(conn->ehwait,
1087 sc->SCp.phase != session->age ||
1088 session->state != ISCSI_STATE_LOGGED_IN ||
Mike Christie656cffc2006-05-18 20:31:42 -05001089 conn->tmabort_state != TMABORT_INITIAL);
Mike Christie7996a772006-04-06 21:13:41 -05001090 if (signal_pending(current))
1091 flush_signals(current);
1092 del_timer_sync(&conn->tmabort_timer);
Mike Christie6724add2007-08-15 01:38:30 -05001093 mutex_lock(&session->eh_mutex);
Mike Christie77a23c22007-05-30 12:57:18 -05001094 spin_lock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001095 return 0;
1096}
1097
1098/*
Mike Christie77a23c22007-05-30 12:57:18 -05001099 * session lock must be held
Mike Christie7996a772006-04-06 21:13:41 -05001100 */
Mike Christieb6c395e2006-07-24 15:47:15 -05001101static struct iscsi_mgmt_task *
1102iscsi_remove_mgmt_task(struct kfifo *fifo, uint32_t itt)
1103{
1104 int i, nr_tasks = __kfifo_len(fifo) / sizeof(void*);
1105 struct iscsi_mgmt_task *task;
Mike Christie7996a772006-04-06 21:13:41 -05001106
Mike Christieb6c395e2006-07-24 15:47:15 -05001107 debug_scsi("searching %d tasks\n", nr_tasks);
1108
1109 for (i = 0; i < nr_tasks; i++) {
1110 __kfifo_get(fifo, (void*)&task, sizeof(void*));
1111 debug_scsi("check task %u\n", task->itt);
1112
1113 if (task->itt == itt) {
1114 debug_scsi("matched task\n");
1115 return task;
1116 }
1117
1118 __kfifo_put(fifo, (void*)&task, sizeof(void*));
1119 }
1120 return NULL;
1121}
Mike Christie7996a772006-04-06 21:13:41 -05001122
1123static int iscsi_ctask_mtask_cleanup(struct iscsi_cmd_task *ctask)
1124{
1125 struct iscsi_conn *conn = ctask->conn;
1126 struct iscsi_session *session = conn->session;
1127
1128 if (!ctask->mtask)
1129 return -EINVAL;
1130
Mike Christie77a23c22007-05-30 12:57:18 -05001131 if (!iscsi_remove_mgmt_task(conn->mgmtqueue, ctask->mtask->itt))
Mike Christie7996a772006-04-06 21:13:41 -05001132 list_del(&ctask->mtask->running);
1133 __kfifo_put(session->mgmtpool.queue, (void*)&ctask->mtask,
1134 sizeof(void*));
1135 ctask->mtask = NULL;
1136 return 0;
1137}
1138
1139/*
Mike Christie77a23c22007-05-30 12:57:18 -05001140 * session lock must be held
Mike Christie7996a772006-04-06 21:13:41 -05001141 */
1142static void fail_command(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
1143 int err)
1144{
1145 struct scsi_cmnd *sc;
1146
Mike Christie7996a772006-04-06 21:13:41 -05001147 sc = ctask->sc;
1148 if (!sc)
1149 return;
Mike Christiee648f632006-08-31 18:09:34 -04001150
Mike Christiee0726402007-07-26 12:46:48 -05001151 if (ctask->state == ISCSI_TASK_PENDING)
1152 /*
1153 * cmd never made it to the xmit thread, so we should not count
1154 * the cmd in the sequencing
1155 */
1156 conn->session->queued_cmdsn--;
1157 else
Mike Christie77a23c22007-05-30 12:57:18 -05001158 conn->session->tt->cleanup_cmd_task(conn, ctask);
Mike Christie7ea8b822006-07-24 15:47:22 -05001159 iscsi_ctask_mtask_cleanup(ctask);
1160
Mike Christie7996a772006-04-06 21:13:41 -05001161 sc->result = err;
FUJITA Tomonori1c138992007-06-14 22:13:17 +09001162 scsi_set_resid(sc, scsi_bufflen(sc));
Mike Christie77a23c22007-05-30 12:57:18 -05001163 if (conn->ctask == ctask)
1164 conn->ctask = NULL;
Mike Christiee648f632006-08-31 18:09:34 -04001165 /* release ref from queuecommand */
Mike Christie60ecebf2006-08-31 18:09:25 -04001166 __iscsi_put_ctask(ctask);
Mike Christie7996a772006-04-06 21:13:41 -05001167}
1168
Mike Christie6724add2007-08-15 01:38:30 -05001169static void iscsi_suspend_tx(struct iscsi_conn *conn)
1170{
1171 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1172 scsi_flush_work(conn->session->host);
1173}
1174
1175static void iscsi_start_tx(struct iscsi_conn *conn)
1176{
1177 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1178 scsi_queue_work(conn->session->host, &conn->xmitwork);
1179}
1180
Mike Christie7996a772006-04-06 21:13:41 -05001181int iscsi_eh_abort(struct scsi_cmnd *sc)
1182{
Mike Christie6724add2007-08-15 01:38:30 -05001183 struct Scsi_Host *host = sc->device->host;
1184 struct iscsi_session *session = iscsi_hostdata(host->hostdata);
Mike Christief47f2cf2006-08-31 18:09:33 -04001185 struct iscsi_cmd_task *ctask;
1186 struct iscsi_conn *conn;
Mike Christie7996a772006-04-06 21:13:41 -05001187 int rc;
1188
Mike Christie6724add2007-08-15 01:38:30 -05001189 mutex_lock(&session->eh_mutex);
1190 spin_lock_bh(&session->lock);
Mike Christief47f2cf2006-08-31 18:09:33 -04001191 /*
1192 * if session was ISCSI_STATE_IN_RECOVERY then we may not have
1193 * got the command.
1194 */
1195 if (!sc->SCp.ptr) {
1196 debug_scsi("sc never reached iscsi layer or it completed.\n");
Mike Christie6724add2007-08-15 01:38:30 -05001197 spin_unlock_bh(&session->lock);
1198 mutex_unlock(&session->eh_mutex);
Mike Christief47f2cf2006-08-31 18:09:33 -04001199 return SUCCESS;
1200 }
1201
1202 ctask = (struct iscsi_cmd_task *)sc->SCp.ptr;
1203 conn = ctask->conn;
Mike Christief47f2cf2006-08-31 18:09:33 -04001204
Mike Christie7996a772006-04-06 21:13:41 -05001205 conn->eh_abort_cnt++;
1206 debug_scsi("aborting [sc %p itt 0x%x]\n", sc, ctask->itt);
1207
Mike Christie7996a772006-04-06 21:13:41 -05001208 /*
1209 * If we are not logged in or we have started a new session
1210 * then let the host reset code handle this
1211 */
1212 if (session->state != ISCSI_STATE_LOGGED_IN ||
1213 sc->SCp.phase != session->age)
1214 goto failed;
1215
1216 /* ctask completed before time out */
Mike Christie7ea8b822006-07-24 15:47:22 -05001217 if (!ctask->sc) {
Mike Christie7ea8b822006-07-24 15:47:22 -05001218 debug_scsi("sc completed while abort in progress\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001219 goto success;
Mike Christie7ea8b822006-07-24 15:47:22 -05001220 }
Mike Christie7996a772006-04-06 21:13:41 -05001221
1222 /* what should we do here ? */
1223 if (conn->ctask == ctask) {
Or Gerlitzbe2df722006-05-02 19:46:43 -05001224 printk(KERN_INFO "iscsi: sc %p itt 0x%x partially sent. "
1225 "Failing abort\n", sc, ctask->itt);
Mike Christie7996a772006-04-06 21:13:41 -05001226 goto failed;
1227 }
1228
Mike Christie77a23c22007-05-30 12:57:18 -05001229 if (ctask->state == ISCSI_TASK_PENDING) {
1230 fail_command(conn, ctask, DID_ABORT << 16);
1231 goto success;
1232 }
Mike Christie7996a772006-04-06 21:13:41 -05001233
1234 conn->tmabort_state = TMABORT_INITIAL;
Mike Christie7996a772006-04-06 21:13:41 -05001235 rc = iscsi_exec_abort_task(sc, ctask);
Mike Christie7996a772006-04-06 21:13:41 -05001236 if (rc || sc->SCp.phase != session->age ||
1237 session->state != ISCSI_STATE_LOGGED_IN)
1238 goto failed;
Mike Christie7ea8b822006-07-24 15:47:22 -05001239 iscsi_ctask_mtask_cleanup(ctask);
Mike Christie7996a772006-04-06 21:13:41 -05001240
Mike Christie7ea8b822006-07-24 15:47:22 -05001241 switch (conn->tmabort_state) {
1242 case TMABORT_SUCCESS:
Mike Christie77a23c22007-05-30 12:57:18 -05001243 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05001244 iscsi_suspend_tx(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001245 /*
1246 * clean up task if aborted. grab the recv lock as a writer
1247 */
1248 write_lock_bh(conn->recv_lock);
1249 spin_lock(&session->lock);
1250 fail_command(conn, ctask, DID_ABORT << 16);
1251 spin_unlock(&session->lock);
1252 write_unlock_bh(conn->recv_lock);
Mike Christie6724add2007-08-15 01:38:30 -05001253 iscsi_start_tx(conn);
Mike Christie77a23c22007-05-30 12:57:18 -05001254 goto success_unlocked;
Mike Christie7ea8b822006-07-24 15:47:22 -05001255 case TMABORT_NOT_FOUND:
1256 if (!ctask->sc) {
1257 /* ctask completed before tmf abort response */
Mike Christie7ea8b822006-07-24 15:47:22 -05001258 debug_scsi("sc completed while abort in progress\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001259 goto success;
Mike Christie7ea8b822006-07-24 15:47:22 -05001260 }
1261 /* fall through */
1262 default:
1263 /* timedout or failed */
Mike Christie7996a772006-04-06 21:13:41 -05001264 spin_unlock_bh(&session->lock);
1265 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie77a23c22007-05-30 12:57:18 -05001266 goto failed_unlocked;
Mike Christie7996a772006-04-06 21:13:41 -05001267 }
1268
Mike Christie77a23c22007-05-30 12:57:18 -05001269success:
Mike Christie7996a772006-04-06 21:13:41 -05001270 spin_unlock_bh(&session->lock);
Mike Christie77a23c22007-05-30 12:57:18 -05001271success_unlocked:
1272 debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
Mike Christie6724add2007-08-15 01:38:30 -05001273 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001274 return SUCCESS;
1275
1276failed:
1277 spin_unlock_bh(&session->lock);
Mike Christie77a23c22007-05-30 12:57:18 -05001278failed_unlocked:
Mike Christie7996a772006-04-06 21:13:41 -05001279 debug_scsi("abort failed [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
Mike Christie6724add2007-08-15 01:38:30 -05001280 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001281 return FAILED;
1282}
1283EXPORT_SYMBOL_GPL(iscsi_eh_abort);
1284
1285int
1286iscsi_pool_init(struct iscsi_queue *q, int max, void ***items, int item_size)
1287{
1288 int i;
1289
1290 *items = kmalloc(max * sizeof(void*), GFP_KERNEL);
1291 if (*items == NULL)
1292 return -ENOMEM;
1293
1294 q->max = max;
1295 q->pool = kmalloc(max * sizeof(void*), GFP_KERNEL);
1296 if (q->pool == NULL) {
1297 kfree(*items);
1298 return -ENOMEM;
1299 }
1300
1301 q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
1302 GFP_KERNEL, NULL);
1303 if (q->queue == ERR_PTR(-ENOMEM)) {
1304 kfree(q->pool);
1305 kfree(*items);
1306 return -ENOMEM;
1307 }
1308
1309 for (i = 0; i < max; i++) {
1310 q->pool[i] = kmalloc(item_size, GFP_KERNEL);
1311 if (q->pool[i] == NULL) {
1312 int j;
1313
1314 for (j = 0; j < i; j++)
1315 kfree(q->pool[j]);
1316
1317 kfifo_free(q->queue);
1318 kfree(q->pool);
1319 kfree(*items);
1320 return -ENOMEM;
1321 }
1322 memset(q->pool[i], 0, item_size);
1323 (*items)[i] = q->pool[i];
1324 __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
1325 }
1326 return 0;
1327}
1328EXPORT_SYMBOL_GPL(iscsi_pool_init);
1329
1330void iscsi_pool_free(struct iscsi_queue *q, void **items)
1331{
1332 int i;
1333
1334 for (i = 0; i < q->max; i++)
1335 kfree(items[i]);
1336 kfree(q->pool);
1337 kfree(items);
1338}
1339EXPORT_SYMBOL_GPL(iscsi_pool_free);
1340
1341/*
1342 * iSCSI Session's hostdata organization:
1343 *
1344 * *------------------* <== hostdata_session(host->hostdata)
1345 * | ptr to class sess|
1346 * |------------------| <== iscsi_hostdata(host->hostdata)
1347 * | iscsi_session |
1348 * *------------------*
1349 */
1350
1351#define hostdata_privsize(_sz) (sizeof(unsigned long) + _sz + \
1352 _sz % sizeof(unsigned long))
1353
1354#define hostdata_session(_hostdata) (iscsi_ptr(*(unsigned long *)_hostdata))
1355
1356/**
1357 * iscsi_session_setup - create iscsi cls session and host and session
1358 * @scsit: scsi transport template
1359 * @iscsit: iscsi transport template
Mike Christie15482712007-05-30 12:57:19 -05001360 * @cmds_max: scsi host can queue
1361 * @qdepth: scsi host cmds per lun
1362 * @cmd_task_size: LLD ctask private data size
1363 * @mgmt_task_size: LLD mtask private data size
Mike Christie7996a772006-04-06 21:13:41 -05001364 * @initial_cmdsn: initial CmdSN
1365 * @hostno: host no allocated
1366 *
1367 * This can be used by software iscsi_transports that allocate
1368 * a session per scsi host.
1369 **/
1370struct iscsi_cls_session *
1371iscsi_session_setup(struct iscsi_transport *iscsit,
1372 struct scsi_transport_template *scsit,
Mike Christie15482712007-05-30 12:57:19 -05001373 uint16_t cmds_max, uint16_t qdepth,
Mike Christie7996a772006-04-06 21:13:41 -05001374 int cmd_task_size, int mgmt_task_size,
1375 uint32_t initial_cmdsn, uint32_t *hostno)
1376{
1377 struct Scsi_Host *shost;
1378 struct iscsi_session *session;
1379 struct iscsi_cls_session *cls_session;
1380 int cmd_i;
1381
Mike Christie15482712007-05-30 12:57:19 -05001382 if (qdepth > ISCSI_MAX_CMD_PER_LUN || qdepth < 1) {
1383 if (qdepth != 0)
1384 printk(KERN_ERR "iscsi: invalid queue depth of %d. "
1385 "Queue depth must be between 1 and %d.\n",
1386 qdepth, ISCSI_MAX_CMD_PER_LUN);
1387 qdepth = ISCSI_DEF_CMD_PER_LUN;
1388 }
1389
1390 if (cmds_max < 2 || (cmds_max & (cmds_max - 1)) ||
1391 cmds_max >= ISCSI_MGMT_ITT_OFFSET) {
1392 if (cmds_max != 0)
1393 printk(KERN_ERR "iscsi: invalid can_queue of %d. "
1394 "can_queue must be a power of 2 and between "
1395 "2 and %d - setting to %d.\n", cmds_max,
1396 ISCSI_MGMT_ITT_OFFSET, ISCSI_DEF_XMIT_CMDS_MAX);
1397 cmds_max = ISCSI_DEF_XMIT_CMDS_MAX;
1398 }
1399
Mike Christie7996a772006-04-06 21:13:41 -05001400 shost = scsi_host_alloc(iscsit->host_template,
1401 hostdata_privsize(sizeof(*session)));
1402 if (!shost)
1403 return NULL;
1404
Mike Christie15482712007-05-30 12:57:19 -05001405 /* the iscsi layer takes one task for reserve */
1406 shost->can_queue = cmds_max - 1;
1407 shost->cmd_per_lun = qdepth;
Mike Christie7996a772006-04-06 21:13:41 -05001408 shost->max_id = 1;
1409 shost->max_channel = 0;
1410 shost->max_lun = iscsit->max_lun;
1411 shost->max_cmd_len = iscsit->max_cmd_len;
1412 shost->transportt = scsit;
1413 shost->transportt->create_work_queue = 1;
1414 *hostno = shost->host_no;
1415
1416 session = iscsi_hostdata(shost->hostdata);
1417 memset(session, 0, sizeof(struct iscsi_session));
1418 session->host = shost;
1419 session->state = ISCSI_STATE_FREE;
1420 session->mgmtpool_max = ISCSI_MGMT_CMDS_MAX;
Mike Christie15482712007-05-30 12:57:19 -05001421 session->cmds_max = cmds_max;
Mike Christiee0726402007-07-26 12:46:48 -05001422 session->queued_cmdsn = session->cmdsn = initial_cmdsn;
Mike Christie7996a772006-04-06 21:13:41 -05001423 session->exp_cmdsn = initial_cmdsn + 1;
1424 session->max_cmdsn = initial_cmdsn + 1;
1425 session->max_r2t = 1;
1426 session->tt = iscsit;
Mike Christie6724add2007-08-15 01:38:30 -05001427 mutex_init(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001428
1429 /* initialize SCSI PDU commands pool */
1430 if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
1431 (void***)&session->cmds,
1432 cmd_task_size + sizeof(struct iscsi_cmd_task)))
1433 goto cmdpool_alloc_fail;
1434
1435 /* pre-format cmds pool with ITT */
1436 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
1437 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
1438
1439 if (cmd_task_size)
1440 ctask->dd_data = &ctask[1];
1441 ctask->itt = cmd_i;
Mike Christieb6c395e2006-07-24 15:47:15 -05001442 INIT_LIST_HEAD(&ctask->running);
Mike Christie7996a772006-04-06 21:13:41 -05001443 }
1444
1445 spin_lock_init(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001446
1447 /* initialize immediate command pool */
1448 if (iscsi_pool_init(&session->mgmtpool, session->mgmtpool_max,
1449 (void***)&session->mgmt_cmds,
1450 mgmt_task_size + sizeof(struct iscsi_mgmt_task)))
1451 goto mgmtpool_alloc_fail;
1452
1453
1454 /* pre-format immediate cmds pool with ITT */
1455 for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
1456 struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
1457
1458 if (mgmt_task_size)
1459 mtask->dd_data = &mtask[1];
1460 mtask->itt = ISCSI_MGMT_ITT_OFFSET + cmd_i;
Mike Christieb6c395e2006-07-24 15:47:15 -05001461 INIT_LIST_HEAD(&mtask->running);
Mike Christie7996a772006-04-06 21:13:41 -05001462 }
1463
1464 if (scsi_add_host(shost, NULL))
1465 goto add_host_fail;
1466
Mike Christief53a88d2006-06-28 12:00:27 -05001467 if (!try_module_get(iscsit->owner))
1468 goto cls_session_fail;
1469
Mike Christie6a8a0d32006-06-28 12:00:31 -05001470 cls_session = iscsi_create_session(shost, iscsit, 0);
Mike Christie7996a772006-04-06 21:13:41 -05001471 if (!cls_session)
Mike Christief53a88d2006-06-28 12:00:27 -05001472 goto module_put;
Mike Christie7996a772006-04-06 21:13:41 -05001473 *(unsigned long*)shost->hostdata = (unsigned long)cls_session;
1474
1475 return cls_session;
1476
Mike Christief53a88d2006-06-28 12:00:27 -05001477module_put:
1478 module_put(iscsit->owner);
Mike Christie7996a772006-04-06 21:13:41 -05001479cls_session_fail:
1480 scsi_remove_host(shost);
1481add_host_fail:
Mike Christie7996a772006-04-06 21:13:41 -05001482 iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
1483mgmtpool_alloc_fail:
1484 iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
1485cmdpool_alloc_fail:
1486 scsi_host_put(shost);
1487 return NULL;
1488}
1489EXPORT_SYMBOL_GPL(iscsi_session_setup);
1490
1491/**
1492 * iscsi_session_teardown - destroy session, host, and cls_session
1493 * shost: scsi host
1494 *
1495 * This can be used by software iscsi_transports that allocate
1496 * a session per scsi host.
1497 **/
1498void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
1499{
1500 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
1501 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
Mike Christie63f75cc2006-07-24 15:47:29 -05001502 struct module *owner = cls_session->transport->owner;
Mike Christie7996a772006-04-06 21:13:41 -05001503
Mike Christie464bb992007-07-26 12:46:45 -05001504 iscsi_unblock_session(cls_session);
Mike Christie7996a772006-04-06 21:13:41 -05001505 scsi_remove_host(shost);
1506
Mike Christie7996a772006-04-06 21:13:41 -05001507 iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
1508 iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
1509
Mike Christieb2c64162007-05-30 12:57:16 -05001510 kfree(session->password);
1511 kfree(session->password_in);
1512 kfree(session->username);
1513 kfree(session->username_in);
Mike Christief3ff0c32006-07-24 15:47:50 -05001514 kfree(session->targetname);
Mike Christied8196ed2007-05-30 12:57:25 -05001515 kfree(session->netdev);
Mike Christie0801c242007-05-30 12:57:12 -05001516 kfree(session->hwaddress);
Mike Christie8ad57812007-05-30 12:57:13 -05001517 kfree(session->initiatorname);
Mike Christief3ff0c32006-07-24 15:47:50 -05001518
Mike Christie7996a772006-04-06 21:13:41 -05001519 iscsi_destroy_session(cls_session);
1520 scsi_host_put(shost);
Mike Christie63f75cc2006-07-24 15:47:29 -05001521 module_put(owner);
Mike Christie7996a772006-04-06 21:13:41 -05001522}
1523EXPORT_SYMBOL_GPL(iscsi_session_teardown);
1524
1525/**
1526 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
1527 * @cls_session: iscsi_cls_session
1528 * @conn_idx: cid
1529 **/
1530struct iscsi_cls_conn *
1531iscsi_conn_setup(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
1532{
1533 struct iscsi_session *session = class_to_transport_session(cls_session);
1534 struct iscsi_conn *conn;
1535 struct iscsi_cls_conn *cls_conn;
Mike Christied36ab6f2006-05-18 20:31:34 -05001536 char *data;
Mike Christie7996a772006-04-06 21:13:41 -05001537
1538 cls_conn = iscsi_create_conn(cls_session, conn_idx);
1539 if (!cls_conn)
1540 return NULL;
1541 conn = cls_conn->dd_data;
1542 memset(conn, 0, sizeof(*conn));
1543
1544 conn->session = session;
1545 conn->cls_conn = cls_conn;
1546 conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
1547 conn->id = conn_idx;
1548 conn->exp_statsn = 0;
1549 conn->tmabort_state = TMABORT_INITIAL;
1550 INIT_LIST_HEAD(&conn->run_list);
1551 INIT_LIST_HEAD(&conn->mgmt_run_list);
Mike Christieb6c395e2006-07-24 15:47:15 -05001552 INIT_LIST_HEAD(&conn->xmitqueue);
Mike Christie7996a772006-04-06 21:13:41 -05001553
1554 /* initialize general immediate & non-immediate PDU commands queue */
Mike Christie7996a772006-04-06 21:13:41 -05001555 conn->mgmtqueue = kfifo_alloc(session->mgmtpool_max * sizeof(void*),
1556 GFP_KERNEL, NULL);
1557 if (conn->mgmtqueue == ERR_PTR(-ENOMEM))
1558 goto mgmtqueue_alloc_fail;
1559
David Howellsc4028952006-11-22 14:57:56 +00001560 INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
Mike Christie7996a772006-04-06 21:13:41 -05001561
1562 /* allocate login_mtask used for the login/text sequences */
1563 spin_lock_bh(&session->lock);
1564 if (!__kfifo_get(session->mgmtpool.queue,
1565 (void*)&conn->login_mtask,
1566 sizeof(void*))) {
1567 spin_unlock_bh(&session->lock);
1568 goto login_mtask_alloc_fail;
1569 }
1570 spin_unlock_bh(&session->lock);
1571
Mike Christiebf32ed32007-02-28 17:32:17 -06001572 data = kmalloc(ISCSI_DEF_MAX_RECV_SEG_LEN, GFP_KERNEL);
Mike Christied36ab6f2006-05-18 20:31:34 -05001573 if (!data)
1574 goto login_mtask_data_alloc_fail;
Mike Christiec8dc1e52006-07-24 15:47:39 -05001575 conn->login_mtask->data = conn->data = data;
Mike Christied36ab6f2006-05-18 20:31:34 -05001576
Mike Christie7996a772006-04-06 21:13:41 -05001577 init_timer(&conn->tmabort_timer);
Mike Christie7996a772006-04-06 21:13:41 -05001578 init_waitqueue_head(&conn->ehwait);
1579
1580 return cls_conn;
1581
Mike Christied36ab6f2006-05-18 20:31:34 -05001582login_mtask_data_alloc_fail:
1583 __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
1584 sizeof(void*));
Mike Christie7996a772006-04-06 21:13:41 -05001585login_mtask_alloc_fail:
1586 kfifo_free(conn->mgmtqueue);
1587mgmtqueue_alloc_fail:
Mike Christie7996a772006-04-06 21:13:41 -05001588 iscsi_destroy_conn(cls_conn);
1589 return NULL;
1590}
1591EXPORT_SYMBOL_GPL(iscsi_conn_setup);
1592
1593/**
1594 * iscsi_conn_teardown - teardown iscsi connection
1595 * cls_conn: iscsi class connection
1596 *
1597 * TODO: we may need to make this into a two step process
1598 * like scsi-mls remove + put host
1599 */
1600void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
1601{
1602 struct iscsi_conn *conn = cls_conn->dd_data;
1603 struct iscsi_session *session = conn->session;
1604 unsigned long flags;
1605
Mike Christie7996a772006-04-06 21:13:41 -05001606 spin_lock_bh(&session->lock);
Mike Christie77a23c22007-05-30 12:57:18 -05001607 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
Mike Christie7996a772006-04-06 21:13:41 -05001608 conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
1609 if (session->leadconn == conn) {
1610 /*
1611 * leading connection? then give up on recovery.
1612 */
1613 session->state = ISCSI_STATE_TERMINATE;
1614 wake_up(&conn->ehwait);
1615 }
1616 spin_unlock_bh(&session->lock);
1617
Mike Christie7996a772006-04-06 21:13:41 -05001618 /*
1619 * Block until all in-progress commands for this connection
1620 * time out or fail.
1621 */
1622 for (;;) {
1623 spin_lock_irqsave(session->host->host_lock, flags);
1624 if (!session->host->host_busy) { /* OK for ERL == 0 */
1625 spin_unlock_irqrestore(session->host->host_lock, flags);
1626 break;
1627 }
1628 spin_unlock_irqrestore(session->host->host_lock, flags);
1629 msleep_interruptible(500);
Or Gerlitzbe2df722006-05-02 19:46:43 -05001630 printk(KERN_INFO "iscsi: scsi conn_destroy(): host_busy %d "
1631 "host_failed %d\n", session->host->host_busy,
1632 session->host->host_failed);
Mike Christie7996a772006-04-06 21:13:41 -05001633 /*
1634 * force eh_abort() to unblock
1635 */
1636 wake_up(&conn->ehwait);
1637 }
1638
Mike Christie779ea122007-02-28 17:32:15 -06001639 /* flush queued up work because we free the connection below */
1640 scsi_flush_work(session->host);
1641
Mike Christie7996a772006-04-06 21:13:41 -05001642 spin_lock_bh(&session->lock);
Mike Christiec8dc1e52006-07-24 15:47:39 -05001643 kfree(conn->data);
Mike Christief3ff0c32006-07-24 15:47:50 -05001644 kfree(conn->persistent_address);
Mike Christie7996a772006-04-06 21:13:41 -05001645 __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
1646 sizeof(void*));
Mike Christiee0726402007-07-26 12:46:48 -05001647 if (session->leadconn == conn)
Mike Christie7996a772006-04-06 21:13:41 -05001648 session->leadconn = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05001649 spin_unlock_bh(&session->lock);
1650
Mike Christie7996a772006-04-06 21:13:41 -05001651 kfifo_free(conn->mgmtqueue);
1652
1653 iscsi_destroy_conn(cls_conn);
1654}
1655EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
1656
1657int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
1658{
1659 struct iscsi_conn *conn = cls_conn->dd_data;
1660 struct iscsi_session *session = conn->session;
1661
Mike Christieffd04362006-08-31 18:09:24 -04001662 if (!session) {
Mike Christie7996a772006-04-06 21:13:41 -05001663 printk(KERN_ERR "iscsi: can't start unbound connection\n");
1664 return -EPERM;
1665 }
1666
Mike Christiedb98ccd2006-08-31 18:09:31 -04001667 if ((session->imm_data_en || !session->initial_r2t_en) &&
1668 session->first_burst > session->max_burst) {
Mike Christieffd04362006-08-31 18:09:24 -04001669 printk("iscsi: invalid burst lengths: "
1670 "first_burst %d max_burst %d\n",
1671 session->first_burst, session->max_burst);
1672 return -EINVAL;
1673 }
1674
Mike Christie7996a772006-04-06 21:13:41 -05001675 spin_lock_bh(&session->lock);
1676 conn->c_stage = ISCSI_CONN_STARTED;
1677 session->state = ISCSI_STATE_LOGGED_IN;
Mike Christiee0726402007-07-26 12:46:48 -05001678 session->queued_cmdsn = session->cmdsn;
Mike Christie7996a772006-04-06 21:13:41 -05001679
1680 switch(conn->stop_stage) {
1681 case STOP_CONN_RECOVER:
1682 /*
1683 * unblock eh_abort() if it is blocked. re-try all
1684 * commands after successful recovery
1685 */
Mike Christie7996a772006-04-06 21:13:41 -05001686 conn->stop_stage = 0;
1687 conn->tmabort_state = TMABORT_INITIAL;
1688 session->age++;
Mike Christie7996a772006-04-06 21:13:41 -05001689 spin_unlock_bh(&session->lock);
1690
1691 iscsi_unblock_session(session_to_cls(session));
1692 wake_up(&conn->ehwait);
1693 return 0;
1694 case STOP_CONN_TERM:
Mike Christie7996a772006-04-06 21:13:41 -05001695 conn->stop_stage = 0;
1696 break;
Mike Christie7996a772006-04-06 21:13:41 -05001697 default:
1698 break;
1699 }
1700 spin_unlock_bh(&session->lock);
1701
1702 return 0;
1703}
1704EXPORT_SYMBOL_GPL(iscsi_conn_start);
1705
1706static void
1707flush_control_queues(struct iscsi_session *session, struct iscsi_conn *conn)
1708{
1709 struct iscsi_mgmt_task *mtask, *tmp;
1710
1711 /* handle pending */
Mike Christie77a23c22007-05-30 12:57:18 -05001712 while (__kfifo_get(conn->mgmtqueue, (void*)&mtask, sizeof(void*))) {
Mike Christie7996a772006-04-06 21:13:41 -05001713 if (mtask == conn->login_mtask)
1714 continue;
1715 debug_scsi("flushing pending mgmt task itt 0x%x\n", mtask->itt);
1716 __kfifo_put(session->mgmtpool.queue, (void*)&mtask,
1717 sizeof(void*));
1718 }
1719
1720 /* handle running */
1721 list_for_each_entry_safe(mtask, tmp, &conn->mgmt_run_list, running) {
1722 debug_scsi("flushing running mgmt task itt 0x%x\n", mtask->itt);
Mike Christieed2abc72006-05-02 19:46:40 -05001723 list_del(&mtask->running);
1724
Mike Christie7996a772006-04-06 21:13:41 -05001725 if (mtask == conn->login_mtask)
1726 continue;
Mike Christieed2abc72006-05-02 19:46:40 -05001727 __kfifo_put(session->mgmtpool.queue, (void*)&mtask,
Mike Christie7996a772006-04-06 21:13:41 -05001728 sizeof(void*));
1729 }
1730
1731 conn->mtask = NULL;
1732}
1733
1734/* Fail commands. Mutex and session lock held and recv side suspended */
1735static void fail_all_commands(struct iscsi_conn *conn)
1736{
1737 struct iscsi_cmd_task *ctask, *tmp;
1738
1739 /* flush pending */
Mike Christieb6c395e2006-07-24 15:47:15 -05001740 list_for_each_entry_safe(ctask, tmp, &conn->xmitqueue, running) {
Mike Christie7996a772006-04-06 21:13:41 -05001741 debug_scsi("failing pending sc %p itt 0x%x\n", ctask->sc,
1742 ctask->itt);
1743 fail_command(conn, ctask, DID_BUS_BUSY << 16);
1744 }
1745
1746 /* fail all other running */
1747 list_for_each_entry_safe(ctask, tmp, &conn->run_list, running) {
1748 debug_scsi("failing in progress sc %p itt 0x%x\n",
1749 ctask->sc, ctask->itt);
1750 fail_command(conn, ctask, DID_BUS_BUSY << 16);
1751 }
1752
1753 conn->ctask = NULL;
1754}
1755
Mike Christie656cffc2006-05-18 20:31:42 -05001756static void iscsi_start_session_recovery(struct iscsi_session *session,
1757 struct iscsi_conn *conn, int flag)
Mike Christie7996a772006-04-06 21:13:41 -05001758{
Mike Christieed2abc72006-05-02 19:46:40 -05001759 int old_stop_stage;
1760
Mike Christie6724add2007-08-15 01:38:30 -05001761 mutex_lock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001762 spin_lock_bh(&session->lock);
Mike Christieed2abc72006-05-02 19:46:40 -05001763 if (conn->stop_stage == STOP_CONN_TERM) {
Mike Christie7996a772006-04-06 21:13:41 -05001764 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05001765 mutex_unlock(&session->eh_mutex);
1766 return;
1767 }
1768
1769 /*
1770 * The LLD either freed/unset the lock on us, or userspace called
1771 * stop but did not create a proper connection (connection was never
1772 * bound or it was unbound then stop was called).
1773 */
1774 if (!conn->recv_lock) {
1775 spin_unlock_bh(&session->lock);
1776 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001777 return;
1778 }
Mike Christieed2abc72006-05-02 19:46:40 -05001779
1780 /*
1781 * When this is called for the in_login state, we only want to clean
Mike Christie67a61112006-05-30 00:37:20 -05001782 * up the login task and connection. We do not need to block and set
1783 * the recovery state again
Mike Christieed2abc72006-05-02 19:46:40 -05001784 */
Mike Christie67a61112006-05-30 00:37:20 -05001785 if (flag == STOP_CONN_TERM)
1786 session->state = ISCSI_STATE_TERMINATE;
1787 else if (conn->stop_stage != STOP_CONN_RECOVER)
1788 session->state = ISCSI_STATE_IN_RECOVERY;
Mike Christieed2abc72006-05-02 19:46:40 -05001789
1790 old_stop_stage = conn->stop_stage;
Mike Christie7996a772006-04-06 21:13:41 -05001791 conn->stop_stage = flag;
Mike Christie67a61112006-05-30 00:37:20 -05001792 conn->c_stage = ISCSI_CONN_STOPPED;
Mike Christie7996a772006-04-06 21:13:41 -05001793 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05001794
1795 iscsi_suspend_tx(conn);
Mike Christie7996a772006-04-06 21:13:41 -05001796
Mike Christie1c834692006-07-24 15:47:26 -05001797 write_lock_bh(conn->recv_lock);
1798 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
1799 write_unlock_bh(conn->recv_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001800
Mike Christie7996a772006-04-06 21:13:41 -05001801 /*
1802 * for connection level recovery we should not calculate
1803 * header digest. conn->hdr_size used for optimization
1804 * in hdr_extract() and will be re-negotiated at
1805 * set_param() time.
1806 */
1807 if (flag == STOP_CONN_RECOVER) {
1808 conn->hdrdgst_en = 0;
1809 conn->datadgst_en = 0;
Mike Christie656cffc2006-05-18 20:31:42 -05001810 if (session->state == ISCSI_STATE_IN_RECOVERY &&
Mike Christie67a61112006-05-30 00:37:20 -05001811 old_stop_stage != STOP_CONN_RECOVER) {
1812 debug_scsi("blocking session\n");
Mike Christie7996a772006-04-06 21:13:41 -05001813 iscsi_block_session(session_to_cls(session));
Mike Christie67a61112006-05-30 00:37:20 -05001814 }
Mike Christie7996a772006-04-06 21:13:41 -05001815 }
Mike Christie656cffc2006-05-18 20:31:42 -05001816
Mike Christie656cffc2006-05-18 20:31:42 -05001817 /*
1818 * flush queues.
1819 */
1820 spin_lock_bh(&session->lock);
1821 fail_all_commands(conn);
1822 flush_control_queues(session, conn);
1823 spin_unlock_bh(&session->lock);
Mike Christie6724add2007-08-15 01:38:30 -05001824 mutex_unlock(&session->eh_mutex);
Mike Christie7996a772006-04-06 21:13:41 -05001825}
Mike Christie7996a772006-04-06 21:13:41 -05001826
1827void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
1828{
1829 struct iscsi_conn *conn = cls_conn->dd_data;
1830 struct iscsi_session *session = conn->session;
1831
1832 switch (flag) {
1833 case STOP_CONN_RECOVER:
1834 case STOP_CONN_TERM:
1835 iscsi_start_session_recovery(session, conn, flag);
Mike Christie8d2860b2006-05-02 19:46:47 -05001836 break;
Mike Christie7996a772006-04-06 21:13:41 -05001837 default:
Or Gerlitzbe2df722006-05-02 19:46:43 -05001838 printk(KERN_ERR "iscsi: invalid stop flag %d\n", flag);
Mike Christie7996a772006-04-06 21:13:41 -05001839 }
1840}
1841EXPORT_SYMBOL_GPL(iscsi_conn_stop);
1842
1843int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
1844 struct iscsi_cls_conn *cls_conn, int is_leading)
1845{
1846 struct iscsi_session *session = class_to_transport_session(cls_session);
Mike Christie98644042006-10-16 18:09:39 -04001847 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05001848
Mike Christie7996a772006-04-06 21:13:41 -05001849 spin_lock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001850 if (is_leading)
1851 session->leadconn = conn;
Mike Christie98644042006-10-16 18:09:39 -04001852 spin_unlock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001853
1854 /*
1855 * Unblock xmitworker(), Login Phase will pass through.
1856 */
1857 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
1858 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1859 return 0;
1860}
1861EXPORT_SYMBOL_GPL(iscsi_conn_bind);
1862
Mike Christiea54a52c2006-06-28 12:00:23 -05001863
1864int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
1865 enum iscsi_param param, char *buf, int buflen)
1866{
1867 struct iscsi_conn *conn = cls_conn->dd_data;
1868 struct iscsi_session *session = conn->session;
1869 uint32_t value;
1870
1871 switch(param) {
1872 case ISCSI_PARAM_MAX_RECV_DLENGTH:
1873 sscanf(buf, "%d", &conn->max_recv_dlength);
1874 break;
1875 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
1876 sscanf(buf, "%d", &conn->max_xmit_dlength);
1877 break;
1878 case ISCSI_PARAM_HDRDGST_EN:
1879 sscanf(buf, "%d", &conn->hdrdgst_en);
1880 break;
1881 case ISCSI_PARAM_DATADGST_EN:
1882 sscanf(buf, "%d", &conn->datadgst_en);
1883 break;
1884 case ISCSI_PARAM_INITIAL_R2T_EN:
1885 sscanf(buf, "%d", &session->initial_r2t_en);
1886 break;
1887 case ISCSI_PARAM_MAX_R2T:
1888 sscanf(buf, "%d", &session->max_r2t);
1889 break;
1890 case ISCSI_PARAM_IMM_DATA_EN:
1891 sscanf(buf, "%d", &session->imm_data_en);
1892 break;
1893 case ISCSI_PARAM_FIRST_BURST:
1894 sscanf(buf, "%d", &session->first_burst);
1895 break;
1896 case ISCSI_PARAM_MAX_BURST:
1897 sscanf(buf, "%d", &session->max_burst);
1898 break;
1899 case ISCSI_PARAM_PDU_INORDER_EN:
1900 sscanf(buf, "%d", &session->pdu_inorder_en);
1901 break;
1902 case ISCSI_PARAM_DATASEQ_INORDER_EN:
1903 sscanf(buf, "%d", &session->dataseq_inorder_en);
1904 break;
1905 case ISCSI_PARAM_ERL:
1906 sscanf(buf, "%d", &session->erl);
1907 break;
1908 case ISCSI_PARAM_IFMARKER_EN:
1909 sscanf(buf, "%d", &value);
1910 BUG_ON(value);
1911 break;
1912 case ISCSI_PARAM_OFMARKER_EN:
1913 sscanf(buf, "%d", &value);
1914 BUG_ON(value);
1915 break;
1916 case ISCSI_PARAM_EXP_STATSN:
1917 sscanf(buf, "%u", &conn->exp_statsn);
1918 break;
Mike Christieb2c64162007-05-30 12:57:16 -05001919 case ISCSI_PARAM_USERNAME:
1920 kfree(session->username);
1921 session->username = kstrdup(buf, GFP_KERNEL);
1922 if (!session->username)
1923 return -ENOMEM;
1924 break;
1925 case ISCSI_PARAM_USERNAME_IN:
1926 kfree(session->username_in);
1927 session->username_in = kstrdup(buf, GFP_KERNEL);
1928 if (!session->username_in)
1929 return -ENOMEM;
1930 break;
1931 case ISCSI_PARAM_PASSWORD:
1932 kfree(session->password);
1933 session->password = kstrdup(buf, GFP_KERNEL);
1934 if (!session->password)
1935 return -ENOMEM;
1936 break;
1937 case ISCSI_PARAM_PASSWORD_IN:
1938 kfree(session->password_in);
1939 session->password_in = kstrdup(buf, GFP_KERNEL);
1940 if (!session->password_in)
1941 return -ENOMEM;
1942 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05001943 case ISCSI_PARAM_TARGET_NAME:
1944 /* this should not change between logins */
1945 if (session->targetname)
1946 break;
1947
1948 session->targetname = kstrdup(buf, GFP_KERNEL);
1949 if (!session->targetname)
1950 return -ENOMEM;
1951 break;
1952 case ISCSI_PARAM_TPGT:
1953 sscanf(buf, "%d", &session->tpgt);
1954 break;
1955 case ISCSI_PARAM_PERSISTENT_PORT:
1956 sscanf(buf, "%d", &conn->persistent_port);
1957 break;
1958 case ISCSI_PARAM_PERSISTENT_ADDRESS:
1959 /*
1960 * this is the address returned in discovery so it should
1961 * not change between logins.
1962 */
1963 if (conn->persistent_address)
1964 break;
1965
1966 conn->persistent_address = kstrdup(buf, GFP_KERNEL);
1967 if (!conn->persistent_address)
1968 return -ENOMEM;
1969 break;
1970 default:
1971 return -ENOSYS;
1972 }
1973
1974 return 0;
1975}
1976EXPORT_SYMBOL_GPL(iscsi_set_param);
1977
1978int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
1979 enum iscsi_param param, char *buf)
1980{
1981 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
1982 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
1983 int len;
1984
1985 switch(param) {
1986 case ISCSI_PARAM_INITIAL_R2T_EN:
1987 len = sprintf(buf, "%d\n", session->initial_r2t_en);
1988 break;
1989 case ISCSI_PARAM_MAX_R2T:
1990 len = sprintf(buf, "%hu\n", session->max_r2t);
1991 break;
1992 case ISCSI_PARAM_IMM_DATA_EN:
1993 len = sprintf(buf, "%d\n", session->imm_data_en);
1994 break;
1995 case ISCSI_PARAM_FIRST_BURST:
1996 len = sprintf(buf, "%u\n", session->first_burst);
1997 break;
1998 case ISCSI_PARAM_MAX_BURST:
1999 len = sprintf(buf, "%u\n", session->max_burst);
2000 break;
2001 case ISCSI_PARAM_PDU_INORDER_EN:
2002 len = sprintf(buf, "%d\n", session->pdu_inorder_en);
2003 break;
2004 case ISCSI_PARAM_DATASEQ_INORDER_EN:
2005 len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
2006 break;
2007 case ISCSI_PARAM_ERL:
2008 len = sprintf(buf, "%d\n", session->erl);
2009 break;
2010 case ISCSI_PARAM_TARGET_NAME:
2011 len = sprintf(buf, "%s\n", session->targetname);
2012 break;
2013 case ISCSI_PARAM_TPGT:
2014 len = sprintf(buf, "%d\n", session->tpgt);
2015 break;
Mike Christieb2c64162007-05-30 12:57:16 -05002016 case ISCSI_PARAM_USERNAME:
2017 len = sprintf(buf, "%s\n", session->username);
2018 break;
2019 case ISCSI_PARAM_USERNAME_IN:
2020 len = sprintf(buf, "%s\n", session->username_in);
2021 break;
2022 case ISCSI_PARAM_PASSWORD:
2023 len = sprintf(buf, "%s\n", session->password);
2024 break;
2025 case ISCSI_PARAM_PASSWORD_IN:
2026 len = sprintf(buf, "%s\n", session->password_in);
2027 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05002028 default:
2029 return -ENOSYS;
2030 }
2031
2032 return len;
2033}
2034EXPORT_SYMBOL_GPL(iscsi_session_get_param);
2035
2036int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
2037 enum iscsi_param param, char *buf)
2038{
2039 struct iscsi_conn *conn = cls_conn->dd_data;
2040 int len;
2041
2042 switch(param) {
2043 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2044 len = sprintf(buf, "%u\n", conn->max_recv_dlength);
2045 break;
2046 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2047 len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
2048 break;
2049 case ISCSI_PARAM_HDRDGST_EN:
2050 len = sprintf(buf, "%d\n", conn->hdrdgst_en);
2051 break;
2052 case ISCSI_PARAM_DATADGST_EN:
2053 len = sprintf(buf, "%d\n", conn->datadgst_en);
2054 break;
2055 case ISCSI_PARAM_IFMARKER_EN:
2056 len = sprintf(buf, "%d\n", conn->ifmarker_en);
2057 break;
2058 case ISCSI_PARAM_OFMARKER_EN:
2059 len = sprintf(buf, "%d\n", conn->ofmarker_en);
2060 break;
2061 case ISCSI_PARAM_EXP_STATSN:
2062 len = sprintf(buf, "%u\n", conn->exp_statsn);
2063 break;
2064 case ISCSI_PARAM_PERSISTENT_PORT:
2065 len = sprintf(buf, "%d\n", conn->persistent_port);
2066 break;
2067 case ISCSI_PARAM_PERSISTENT_ADDRESS:
2068 len = sprintf(buf, "%s\n", conn->persistent_address);
2069 break;
2070 default:
2071 return -ENOSYS;
2072 }
2073
2074 return len;
2075}
2076EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
2077
Mike Christie0801c242007-05-30 12:57:12 -05002078int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2079 char *buf)
2080{
2081 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
2082 int len;
2083
2084 switch (param) {
Mike Christied8196ed2007-05-30 12:57:25 -05002085 case ISCSI_HOST_PARAM_NETDEV_NAME:
2086 if (!session->netdev)
2087 len = sprintf(buf, "%s\n", "default");
2088 else
2089 len = sprintf(buf, "%s\n", session->netdev);
2090 break;
Mike Christie0801c242007-05-30 12:57:12 -05002091 case ISCSI_HOST_PARAM_HWADDRESS:
2092 if (!session->hwaddress)
2093 len = sprintf(buf, "%s\n", "default");
2094 else
2095 len = sprintf(buf, "%s\n", session->hwaddress);
2096 break;
Mike Christie8ad57812007-05-30 12:57:13 -05002097 case ISCSI_HOST_PARAM_INITIATOR_NAME:
2098 if (!session->initiatorname)
2099 len = sprintf(buf, "%s\n", "unknown");
2100 else
2101 len = sprintf(buf, "%s\n", session->initiatorname);
2102 break;
2103
Mike Christie0801c242007-05-30 12:57:12 -05002104 default:
2105 return -ENOSYS;
2106 }
2107
2108 return len;
2109}
2110EXPORT_SYMBOL_GPL(iscsi_host_get_param);
2111
2112int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2113 char *buf, int buflen)
2114{
2115 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
2116
2117 switch (param) {
Mike Christied8196ed2007-05-30 12:57:25 -05002118 case ISCSI_HOST_PARAM_NETDEV_NAME:
2119 if (!session->netdev)
2120 session->netdev = kstrdup(buf, GFP_KERNEL);
2121 break;
Mike Christie0801c242007-05-30 12:57:12 -05002122 case ISCSI_HOST_PARAM_HWADDRESS:
2123 if (!session->hwaddress)
2124 session->hwaddress = kstrdup(buf, GFP_KERNEL);
2125 break;
Mike Christie8ad57812007-05-30 12:57:13 -05002126 case ISCSI_HOST_PARAM_INITIATOR_NAME:
2127 if (!session->initiatorname)
2128 session->initiatorname = kstrdup(buf, GFP_KERNEL);
2129 break;
Mike Christie0801c242007-05-30 12:57:12 -05002130 default:
2131 return -ENOSYS;
2132 }
2133
2134 return 0;
2135}
2136EXPORT_SYMBOL_GPL(iscsi_host_set_param);
2137
Mike Christie7996a772006-04-06 21:13:41 -05002138MODULE_AUTHOR("Mike Christie");
2139MODULE_DESCRIPTION("iSCSI library functions");
2140MODULE_LICENSE("GPL");