blob: 4d85ce1001922819e40ea48e6ea46c86680dcb99 [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 Christieb6c395ed2006-07-24 15:47:15 -0500224 ctask->state = ISCSI_TASK_COMPLETED;
Mike Christie7996a772006-04-06 21:13:41 -0500225 ctask->sc = NULL;
Mike Christief47f2cf2006-08-31 18:09:33 -0400226 /* SCSI eh reuses commands to verify us */
227 sc->SCp.ptr = NULL;
Mike Christie7996a772006-04-06 21:13:41 -0500228 list_del_init(&ctask->running);
229 __kfifo_put(session->cmdpool.queue, (void*)&ctask, sizeof(void*));
230 sc->scsi_done(sc);
231}
232
Mike Christie60ecebf2006-08-31 18:09:25 -0400233static void __iscsi_get_ctask(struct iscsi_cmd_task *ctask)
234{
235 atomic_inc(&ctask->refcount);
236}
237
Mike Christie60ecebf2006-08-31 18:09:25 -0400238static void __iscsi_put_ctask(struct iscsi_cmd_task *ctask)
239{
Mike Christiee648f632006-08-31 18:09:34 -0400240 if (atomic_dec_and_test(&ctask->refcount))
Mike Christie60ecebf2006-08-31 18:09:25 -0400241 iscsi_complete_command(ctask);
Mike Christie60ecebf2006-08-31 18:09:25 -0400242}
243
Mike Christie7996a772006-04-06 21:13:41 -0500244/**
245 * iscsi_cmd_rsp - SCSI Command Response processing
246 * @conn: iscsi connection
247 * @hdr: iscsi header
248 * @ctask: scsi command task
249 * @data: cmd data buffer
250 * @datalen: len of buffer
251 *
252 * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
253 * then completes the command and task.
254 **/
Mike Christie77a23c22007-05-30 12:57:18 -0500255static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
256 struct iscsi_cmd_task *ctask, char *data,
257 int datalen)
Mike Christie7996a772006-04-06 21:13:41 -0500258{
Mike Christie7996a772006-04-06 21:13:41 -0500259 struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)hdr;
260 struct iscsi_session *session = conn->session;
261 struct scsi_cmnd *sc = ctask->sc;
262
Mike Christie77a23c22007-05-30 12:57:18 -0500263 iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
Mike Christie7996a772006-04-06 21:13:41 -0500264 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
265
266 sc->result = (DID_OK << 16) | rhdr->cmd_status;
267
268 if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
269 sc->result = DID_ERROR << 16;
270 goto out;
271 }
272
273 if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
Mike Christie9b80cb42006-12-17 12:10:28 -0600274 uint16_t senselen;
Mike Christie7996a772006-04-06 21:13:41 -0500275
276 if (datalen < 2) {
277invalid_datalen:
Or Gerlitzbe2df722006-05-02 19:46:43 -0500278 printk(KERN_ERR "iscsi: Got CHECK_CONDITION but "
279 "invalid data buffer size of %d\n", datalen);
Mike Christie7996a772006-04-06 21:13:41 -0500280 sc->result = DID_BAD_TARGET << 16;
281 goto out;
282 }
283
Mike Christie8eb00532007-02-28 17:32:19 -0600284 senselen = be16_to_cpu(get_unaligned((__be16 *) data));
Mike Christie7996a772006-04-06 21:13:41 -0500285 if (datalen < senselen)
286 goto invalid_datalen;
287
288 memcpy(sc->sense_buffer, data + 2,
Mike Christie9b80cb42006-12-17 12:10:28 -0600289 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
Mike Christie7996a772006-04-06 21:13:41 -0500290 debug_scsi("copied %d bytes of sense\n",
Mike Christie8eb00532007-02-28 17:32:19 -0600291 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
Mike Christie7996a772006-04-06 21:13:41 -0500292 }
293
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);
599 if (conn->c_stage == ISCSI_CONN_STARTED &&
600 !(hdr->opcode & ISCSI_OP_IMMEDIATE))
601 session->cmdsn++;
602 }
603
604 if (session->tt->init_mgmt_task)
605 session->tt->init_mgmt_task(conn, mtask);
606
607 debug_scsi("mgmtpdu [op 0x%x hdr->itt 0x%x datalen %d]\n",
608 hdr->opcode, hdr->itt, mtask->data_count);
609}
610
Mike Christie05db8882007-02-28 17:32:16 -0600611static int iscsi_xmit_mtask(struct iscsi_conn *conn)
Mike Christieb5072ea2006-10-16 18:09:42 -0400612{
613 struct iscsi_hdr *hdr = conn->mtask->hdr;
614 int rc, was_logout = 0;
615
Mike Christie77a23c22007-05-30 12:57:18 -0500616 spin_unlock_bh(&conn->session->lock);
Mike Christieb5072ea2006-10-16 18:09:42 -0400617 if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT) {
618 conn->session->state = ISCSI_STATE_IN_RECOVERY;
619 iscsi_block_session(session_to_cls(conn->session));
620 was_logout = 1;
621 }
622 rc = conn->session->tt->xmit_mgmt_task(conn, conn->mtask);
Mike Christie77a23c22007-05-30 12:57:18 -0500623 spin_lock_bh(&conn->session->lock);
Mike Christieb5072ea2006-10-16 18:09:42 -0400624 if (rc)
625 return rc;
626
Mike Christie05db8882007-02-28 17:32:16 -0600627 /* done with this in-progress mtask */
628 conn->mtask = NULL;
629
Mike Christieb5072ea2006-10-16 18:09:42 -0400630 if (was_logout) {
631 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
632 return -ENODATA;
633 }
634 return 0;
635}
636
Mike Christie77a23c22007-05-30 12:57:18 -0500637static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
638{
639 struct iscsi_session *session = conn->session;
640
641 /*
642 * Check for iSCSI window and take care of CmdSN wrap-around
643 */
644 if (!iscsi_sna_lte(session->cmdsn, session->max_cmdsn)) {
645 debug_scsi("iSCSI CmdSN closed. MaxCmdSN %u CmdSN %u\n",
646 session->max_cmdsn, session->cmdsn);
647 return -ENOSPC;
648 }
649 return 0;
650}
651
652static int iscsi_xmit_ctask(struct iscsi_conn *conn)
653{
654 struct iscsi_cmd_task *ctask = conn->ctask;
655 int rc = 0;
656
657 /*
658 * serialize with TMF AbortTask
659 */
660 if (ctask->state == ISCSI_TASK_ABORTING)
661 goto done;
662
663 __iscsi_get_ctask(ctask);
664 spin_unlock_bh(&conn->session->lock);
665 rc = conn->session->tt->xmit_cmd_task(conn, ctask);
666 spin_lock_bh(&conn->session->lock);
667 __iscsi_put_ctask(ctask);
668
669done:
670 if (!rc)
671 /* done with this ctask */
672 conn->ctask = NULL;
673 return rc;
674}
675
Mike Christie7996a772006-04-06 21:13:41 -0500676/**
677 * iscsi_data_xmit - xmit any command into the scheduled connection
678 * @conn: iscsi connection
679 *
680 * Notes:
681 * The function can return -EAGAIN in which case the caller must
682 * re-schedule it again later or recover. '0' return code means
683 * successful xmit.
684 **/
685static int iscsi_data_xmit(struct iscsi_conn *conn)
686{
Mike Christie3219e522006-05-30 00:37:28 -0500687 int rc = 0;
Mike Christie7996a772006-04-06 21:13:41 -0500688
Mike Christie77a23c22007-05-30 12:57:18 -0500689 spin_lock_bh(&conn->session->lock);
Mike Christie7996a772006-04-06 21:13:41 -0500690 if (unlikely(conn->suspend_tx)) {
691 debug_scsi("conn %d Tx suspended!\n", conn->id);
Mike Christie77a23c22007-05-30 12:57:18 -0500692 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -0500693 return -ENODATA;
Mike Christie7996a772006-04-06 21:13:41 -0500694 }
Mike Christie7996a772006-04-06 21:13:41 -0500695
696 if (conn->ctask) {
Mike Christie77a23c22007-05-30 12:57:18 -0500697 rc = iscsi_xmit_ctask(conn);
Mike Christie3219e522006-05-30 00:37:28 -0500698 if (rc)
Mike Christie7996a772006-04-06 21:13:41 -0500699 goto again;
Mike Christie7996a772006-04-06 21:13:41 -0500700 }
Mike Christie77a23c22007-05-30 12:57:18 -0500701
Mike Christie7996a772006-04-06 21:13:41 -0500702 if (conn->mtask) {
Mike Christie05db8882007-02-28 17:32:16 -0600703 rc = iscsi_xmit_mtask(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 }
707
Mike Christie77a23c22007-05-30 12:57:18 -0500708 /*
709 * process mgmt pdus like nops before commands since we should
710 * only have one nop-out as a ping from us and targets should not
711 * overflow us with nop-ins
712 */
713check_mgmt:
714 while (__kfifo_get(conn->mgmtqueue, (void*)&conn->mtask,
715 sizeof(void*))) {
716 iscsi_prep_mtask(conn, conn->mtask);
717 list_add_tail(&conn->mtask->running, &conn->mgmt_run_list);
718 rc = iscsi_xmit_mtask(conn);
719 if (rc)
720 goto again;
Mike Christie7996a772006-04-06 21:13:41 -0500721 }
722
723 /* process command queue */
Mike Christieb6c395ed2006-07-24 15:47:15 -0500724 while (!list_empty(&conn->xmitqueue)) {
Mike Christie77a23c22007-05-30 12:57:18 -0500725 rc = iscsi_check_cmdsn_window_closed(conn);
726 if (rc) {
727 spin_unlock_bh(&conn->session->lock);
728 return rc;
729 }
Mike Christie7996a772006-04-06 21:13:41 -0500730 /*
731 * iscsi tcp may readd the task to the xmitqueue to send
732 * write data
733 */
Mike Christieb6c395ed2006-07-24 15:47:15 -0500734 conn->ctask = list_entry(conn->xmitqueue.next,
735 struct iscsi_cmd_task, running);
Mike Christie77a23c22007-05-30 12:57:18 -0500736 if (conn->ctask->state == ISCSI_TASK_PENDING) {
737 iscsi_prep_scsi_cmd_pdu(conn->ctask);
738 conn->session->tt->init_cmd_task(conn->ctask);
739 }
Mike Christieb6c395ed2006-07-24 15:47:15 -0500740 conn->ctask->state = ISCSI_TASK_RUNNING;
741 list_move_tail(conn->xmitqueue.next, &conn->run_list);
Mike Christie77a23c22007-05-30 12:57:18 -0500742 rc = iscsi_xmit_ctask(conn);
743 if (rc)
Mike Christie60ecebf2006-08-31 18:09:25 -0400744 goto again;
Mike Christie77a23c22007-05-30 12:57:18 -0500745 /*
746 * we could continuously get new ctask requests so
747 * we need to check the mgmt queue for nops that need to
748 * be sent to aviod starvation
749 */
750 if (__kfifo_len(conn->mgmtqueue))
751 goto check_mgmt;
Mike Christie7996a772006-04-06 21:13:41 -0500752 }
Mike Christieb6c395ed2006-07-24 15:47:15 -0500753 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -0500754 return -ENODATA;
Mike Christie7996a772006-04-06 21:13:41 -0500755
756again:
757 if (unlikely(conn->suspend_tx))
Mike Christie77a23c22007-05-30 12:57:18 -0500758 rc = -ENODATA;
759 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -0500760 return rc;
Mike Christie7996a772006-04-06 21:13:41 -0500761}
762
David Howellsc4028952006-11-22 14:57:56 +0000763static void iscsi_xmitworker(struct work_struct *work)
Mike Christie7996a772006-04-06 21:13:41 -0500764{
David Howellsc4028952006-11-22 14:57:56 +0000765 struct iscsi_conn *conn =
766 container_of(work, struct iscsi_conn, xmitwork);
Mike Christie3219e522006-05-30 00:37:28 -0500767 int rc;
Mike Christie7996a772006-04-06 21:13:41 -0500768 /*
769 * serialize Xmit worker on a per-connection basis.
770 */
Mike Christie3219e522006-05-30 00:37:28 -0500771 do {
772 rc = iscsi_data_xmit(conn);
773 } while (rc >= 0 || rc == -EAGAIN);
Mike Christie7996a772006-04-06 21:13:41 -0500774}
775
776enum {
777 FAILURE_BAD_HOST = 1,
778 FAILURE_SESSION_FAILED,
779 FAILURE_SESSION_FREED,
780 FAILURE_WINDOW_CLOSED,
Mike Christie60ecebf2006-08-31 18:09:25 -0400781 FAILURE_OOM,
Mike Christie7996a772006-04-06 21:13:41 -0500782 FAILURE_SESSION_TERMINATE,
Mike Christie656cffc2006-05-18 20:31:42 -0500783 FAILURE_SESSION_IN_RECOVERY,
Mike Christie7996a772006-04-06 21:13:41 -0500784 FAILURE_SESSION_RECOVERY_TIMEOUT,
785};
786
787int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
788{
789 struct Scsi_Host *host;
790 int reason = 0;
791 struct iscsi_session *session;
792 struct iscsi_conn *conn;
793 struct iscsi_cmd_task *ctask = NULL;
794
795 sc->scsi_done = done;
796 sc->result = 0;
Mike Christief47f2cf2006-08-31 18:09:33 -0400797 sc->SCp.ptr = NULL;
Mike Christie7996a772006-04-06 21:13:41 -0500798
799 host = sc->device->host;
800 session = iscsi_hostdata(host->hostdata);
801
802 spin_lock(&session->lock);
803
Mike Christie656cffc2006-05-18 20:31:42 -0500804 /*
805 * ISCSI_STATE_FAILED is a temp. state. The recovery
806 * code will decide what is best to do with command queued
807 * during this time
808 */
809 if (session->state != ISCSI_STATE_LOGGED_IN &&
810 session->state != ISCSI_STATE_FAILED) {
811 /*
812 * to handle the race between when we set the recovery state
813 * and block the session we requeue here (commands could
814 * be entering our queuecommand while a block is starting
815 * up because the block code is not locked)
816 */
817 if (session->state == ISCSI_STATE_IN_RECOVERY) {
818 reason = FAILURE_SESSION_IN_RECOVERY;
Mike Christie67a61112006-05-30 00:37:20 -0500819 goto reject;
Mike Christie7996a772006-04-06 21:13:41 -0500820 }
Mike Christie656cffc2006-05-18 20:31:42 -0500821
822 if (session->state == ISCSI_STATE_RECOVERY_FAILED)
823 reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
824 else if (session->state == ISCSI_STATE_TERMINATE)
825 reason = FAILURE_SESSION_TERMINATE;
826 else
827 reason = FAILURE_SESSION_FREED;
Mike Christie7996a772006-04-06 21:13:41 -0500828 goto fault;
829 }
830
Mike Christie7996a772006-04-06 21:13:41 -0500831 conn = session->leadconn;
Mike Christie98644042006-10-16 18:09:39 -0400832 if (!conn) {
833 reason = FAILURE_SESSION_FREED;
834 goto fault;
835 }
Mike Christie7996a772006-04-06 21:13:41 -0500836
Mike Christie77a23c22007-05-30 12:57:18 -0500837 /*
838 * We check this here and in data xmit, because if we get to the point
839 * that this check is hitting the window then we have enough IO in
840 * flight and enough IO waiting to be transmitted it is better
841 * to let the scsi/block layer queue up.
842 */
843 if (iscsi_check_cmdsn_window_closed(conn)) {
844 reason = FAILURE_WINDOW_CLOSED;
845 goto reject;
846 }
847
Mike Christie60ecebf2006-08-31 18:09:25 -0400848 if (!__kfifo_get(session->cmdpool.queue, (void*)&ctask,
849 sizeof(void*))) {
850 reason = FAILURE_OOM;
851 goto reject;
852 }
Mike Christie7996a772006-04-06 21:13:41 -0500853 sc->SCp.phase = session->age;
854 sc->SCp.ptr = (char *)ctask;
855
Mike Christie60ecebf2006-08-31 18:09:25 -0400856 atomic_set(&ctask->refcount, 1);
Mike Christieb6c395ed2006-07-24 15:47:15 -0500857 ctask->state = ISCSI_TASK_PENDING;
Mike Christie7996a772006-04-06 21:13:41 -0500858 ctask->mtask = NULL;
859 ctask->conn = conn;
860 ctask->sc = sc;
861 INIT_LIST_HEAD(&ctask->running);
Mike Christie7996a772006-04-06 21:13:41 -0500862
Mike Christieb6c395ed2006-07-24 15:47:15 -0500863 list_add_tail(&ctask->running, &conn->xmitqueue);
Mike Christie7996a772006-04-06 21:13:41 -0500864 spin_unlock(&session->lock);
865
866 scsi_queue_work(host, &conn->xmitwork);
867 return 0;
868
869reject:
870 spin_unlock(&session->lock);
871 debug_scsi("cmd 0x%x rejected (%d)\n", sc->cmnd[0], reason);
872 return SCSI_MLQUEUE_HOST_BUSY;
873
874fault:
875 spin_unlock(&session->lock);
Or Gerlitzbe2df722006-05-02 19:46:43 -0500876 printk(KERN_ERR "iscsi: cmd 0x%x is not queued (%d)\n",
Mike Christie7996a772006-04-06 21:13:41 -0500877 sc->cmnd[0], reason);
878 sc->result = (DID_NO_CONNECT << 16);
FUJITA Tomonori1c138992007-06-14 22:13:17 +0900879 scsi_set_resid(sc, scsi_bufflen(sc));
Mike Christie7996a772006-04-06 21:13:41 -0500880 sc->scsi_done(sc);
881 return 0;
882}
883EXPORT_SYMBOL_GPL(iscsi_queuecommand);
884
885int iscsi_change_queue_depth(struct scsi_device *sdev, int depth)
886{
887 if (depth > ISCSI_MAX_CMD_PER_LUN)
888 depth = ISCSI_MAX_CMD_PER_LUN;
889 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
890 return sdev->queue_depth;
891}
892EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
893
Mike Christie77a23c22007-05-30 12:57:18 -0500894static struct iscsi_mgmt_task *
895__iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
896 char *data, uint32_t data_size)
Mike Christie7996a772006-04-06 21:13:41 -0500897{
898 struct iscsi_session *session = conn->session;
Mike Christie7996a772006-04-06 21:13:41 -0500899 struct iscsi_mgmt_task *mtask;
900
Mike Christie77a23c22007-05-30 12:57:18 -0500901 if (session->state == ISCSI_STATE_TERMINATE)
902 return NULL;
903
Mike Christie7996a772006-04-06 21:13:41 -0500904 if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) ||
905 hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
906 /*
907 * Login and Text are sent serially, in
908 * request-followed-by-response sequence.
909 * Same mtask can be used. Same ITT must be used.
910 * Note that login_mtask is preallocated at conn_create().
911 */
912 mtask = conn->login_mtask;
913 else {
Mike Christie656cffc2006-05-18 20:31:42 -0500914 BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
915 BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
Mike Christie7996a772006-04-06 21:13:41 -0500916
917 if (!__kfifo_get(session->mgmtpool.queue,
Mike Christie77a23c22007-05-30 12:57:18 -0500918 (void*)&mtask, sizeof(void*)))
919 return NULL;
Mike Christie7996a772006-04-06 21:13:41 -0500920 }
921
Mike Christie7996a772006-04-06 21:13:41 -0500922 if (data_size) {
923 memcpy(mtask->data, data, data_size);
924 mtask->data_count = data_size;
925 } else
926 mtask->data_count = 0;
927
928 INIT_LIST_HEAD(&mtask->running);
929 memcpy(mtask->hdr, hdr, sizeof(struct iscsi_hdr));
Mike Christie77a23c22007-05-30 12:57:18 -0500930 __kfifo_put(conn->mgmtqueue, (void*)&mtask, sizeof(void*));
931 return mtask;
Mike Christie7996a772006-04-06 21:13:41 -0500932}
933
934int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
935 char *data, uint32_t data_size)
936{
937 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie77a23c22007-05-30 12:57:18 -0500938 struct iscsi_session *session = conn->session;
939 int err = 0;
Mike Christie7996a772006-04-06 21:13:41 -0500940
Mike Christie77a23c22007-05-30 12:57:18 -0500941 spin_lock_bh(&session->lock);
942 if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size))
943 err = -EPERM;
944 spin_unlock_bh(&session->lock);
945 scsi_queue_work(session->host, &conn->xmitwork);
946 return err;
Mike Christie7996a772006-04-06 21:13:41 -0500947}
948EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
949
950void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
951{
952 struct iscsi_session *session = class_to_transport_session(cls_session);
953 struct iscsi_conn *conn = session->leadconn;
954
955 spin_lock_bh(&session->lock);
956 if (session->state != ISCSI_STATE_LOGGED_IN) {
Mike Christie656cffc2006-05-18 20:31:42 -0500957 session->state = ISCSI_STATE_RECOVERY_FAILED;
Mike Christie7996a772006-04-06 21:13:41 -0500958 if (conn)
959 wake_up(&conn->ehwait);
960 }
961 spin_unlock_bh(&session->lock);
962}
963EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
964
965int iscsi_eh_host_reset(struct scsi_cmnd *sc)
966{
967 struct Scsi_Host *host = sc->device->host;
968 struct iscsi_session *session = iscsi_hostdata(host->hostdata);
969 struct iscsi_conn *conn = session->leadconn;
970 int fail_session = 0;
971
972 spin_lock_bh(&session->lock);
973 if (session->state == ISCSI_STATE_TERMINATE) {
974failed:
975 debug_scsi("failing host reset: session terminated "
Pete Wyckoffd6e24d12006-11-08 15:58:32 -0600976 "[CID %d age %d]\n", conn->id, session->age);
Mike Christie7996a772006-04-06 21:13:41 -0500977 spin_unlock_bh(&session->lock);
978 return FAILED;
979 }
980
981 if (sc->SCp.phase == session->age) {
Pete Wyckoffd6e24d12006-11-08 15:58:32 -0600982 debug_scsi("failing connection CID %d due to SCSI host reset\n",
Mike Christie7996a772006-04-06 21:13:41 -0500983 conn->id);
984 fail_session = 1;
985 }
986 spin_unlock_bh(&session->lock);
987
988 /*
989 * we drop the lock here but the leadconn cannot be destoyed while
990 * we are in the scsi eh
991 */
Mike Christie656cffc2006-05-18 20:31:42 -0500992 if (fail_session)
Mike Christie7996a772006-04-06 21:13:41 -0500993 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie7996a772006-04-06 21:13:41 -0500994
995 debug_scsi("iscsi_eh_host_reset wait for relogin\n");
996 wait_event_interruptible(conn->ehwait,
997 session->state == ISCSI_STATE_TERMINATE ||
998 session->state == ISCSI_STATE_LOGGED_IN ||
Mike Christie656cffc2006-05-18 20:31:42 -0500999 session->state == ISCSI_STATE_RECOVERY_FAILED);
Mike Christie7996a772006-04-06 21:13:41 -05001000 if (signal_pending(current))
1001 flush_signals(current);
1002
1003 spin_lock_bh(&session->lock);
1004 if (session->state == ISCSI_STATE_LOGGED_IN)
Or Gerlitzbe2df722006-05-02 19:46:43 -05001005 printk(KERN_INFO "iscsi: host reset succeeded\n");
Mike Christie7996a772006-04-06 21:13:41 -05001006 else
1007 goto failed;
1008 spin_unlock_bh(&session->lock);
1009
1010 return SUCCESS;
1011}
1012EXPORT_SYMBOL_GPL(iscsi_eh_host_reset);
1013
1014static void iscsi_tmabort_timedout(unsigned long data)
1015{
1016 struct iscsi_cmd_task *ctask = (struct iscsi_cmd_task *)data;
1017 struct iscsi_conn *conn = ctask->conn;
1018 struct iscsi_session *session = conn->session;
1019
1020 spin_lock(&session->lock);
1021 if (conn->tmabort_state == TMABORT_INITIAL) {
1022 conn->tmabort_state = TMABORT_TIMEDOUT;
1023 debug_scsi("tmabort timedout [sc %p itt 0x%x]\n",
1024 ctask->sc, ctask->itt);
1025 /* unblock eh_abort() */
1026 wake_up(&conn->ehwait);
1027 }
1028 spin_unlock(&session->lock);
1029}
1030
Mike Christie7996a772006-04-06 21:13:41 -05001031static int iscsi_exec_abort_task(struct scsi_cmnd *sc,
1032 struct iscsi_cmd_task *ctask)
1033{
1034 struct iscsi_conn *conn = ctask->conn;
1035 struct iscsi_session *session = conn->session;
1036 struct iscsi_tm *hdr = &conn->tmhdr;
Mike Christie7996a772006-04-06 21:13:41 -05001037
1038 /*
1039 * ctask timed out but session is OK requests must be serialized.
1040 */
1041 memset(hdr, 0, sizeof(struct iscsi_tm));
1042 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
1043 hdr->flags = ISCSI_TM_FUNC_ABORT_TASK;
1044 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
1045 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
1046 hdr->rtt = ctask->hdr->itt;
1047 hdr->refcmdsn = ctask->hdr->cmdsn;
1048
Mike Christie77a23c22007-05-30 12:57:18 -05001049 ctask->mtask = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
1050 NULL, 0);
1051 if (!ctask->mtask) {
Mike Christie7996a772006-04-06 21:13:41 -05001052 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie77a23c22007-05-30 12:57:18 -05001053 debug_scsi("abort sent failure [itt 0x%x]\n", ctask->itt);
1054 return -EPERM;
Mike Christie7996a772006-04-06 21:13:41 -05001055 }
Mike Christie77a23c22007-05-30 12:57:18 -05001056 ctask->state = ISCSI_TASK_ABORTING;
Mike Christie7996a772006-04-06 21:13:41 -05001057
1058 debug_scsi("abort sent [itt 0x%x]\n", ctask->itt);
1059
Mike Christie7996a772006-04-06 21:13:41 -05001060 if (conn->tmabort_state == TMABORT_INITIAL) {
1061 conn->tmfcmd_pdus_cnt++;
Mike Christie77a23c22007-05-30 12:57:18 -05001062 conn->tmabort_timer.expires = 20*HZ + jiffies;
Mike Christie7996a772006-04-06 21:13:41 -05001063 conn->tmabort_timer.function = iscsi_tmabort_timedout;
1064 conn->tmabort_timer.data = (unsigned long)ctask;
1065 add_timer(&conn->tmabort_timer);
Pete Wyckoffd6e24d12006-11-08 15:58:32 -06001066 debug_scsi("abort set timeout [itt 0x%x]\n", ctask->itt);
Mike Christie7996a772006-04-06 21:13:41 -05001067 }
1068 spin_unlock_bh(&session->lock);
Mike Christie77a23c22007-05-30 12:57:18 -05001069 scsi_queue_work(session->host, &conn->xmitwork);
Mike Christie7996a772006-04-06 21:13:41 -05001070
1071 /*
1072 * block eh thread until:
1073 *
1074 * 1) abort response
1075 * 2) abort timeout
1076 * 3) session is terminated or restarted or userspace has
1077 * given up on recovery
1078 */
1079 wait_event_interruptible(conn->ehwait,
1080 sc->SCp.phase != session->age ||
1081 session->state != ISCSI_STATE_LOGGED_IN ||
Mike Christie656cffc2006-05-18 20:31:42 -05001082 conn->tmabort_state != TMABORT_INITIAL);
Mike Christie7996a772006-04-06 21:13:41 -05001083 if (signal_pending(current))
1084 flush_signals(current);
1085 del_timer_sync(&conn->tmabort_timer);
Mike Christie77a23c22007-05-30 12:57:18 -05001086 spin_lock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001087 return 0;
1088}
1089
1090/*
Mike Christie77a23c22007-05-30 12:57:18 -05001091 * session lock must be held
Mike Christie7996a772006-04-06 21:13:41 -05001092 */
Mike Christieb6c395ed2006-07-24 15:47:15 -05001093static struct iscsi_mgmt_task *
1094iscsi_remove_mgmt_task(struct kfifo *fifo, uint32_t itt)
1095{
1096 int i, nr_tasks = __kfifo_len(fifo) / sizeof(void*);
1097 struct iscsi_mgmt_task *task;
Mike Christie7996a772006-04-06 21:13:41 -05001098
Mike Christieb6c395ed2006-07-24 15:47:15 -05001099 debug_scsi("searching %d tasks\n", nr_tasks);
1100
1101 for (i = 0; i < nr_tasks; i++) {
1102 __kfifo_get(fifo, (void*)&task, sizeof(void*));
1103 debug_scsi("check task %u\n", task->itt);
1104
1105 if (task->itt == itt) {
1106 debug_scsi("matched task\n");
1107 return task;
1108 }
1109
1110 __kfifo_put(fifo, (void*)&task, sizeof(void*));
1111 }
1112 return NULL;
1113}
Mike Christie7996a772006-04-06 21:13:41 -05001114
1115static int iscsi_ctask_mtask_cleanup(struct iscsi_cmd_task *ctask)
1116{
1117 struct iscsi_conn *conn = ctask->conn;
1118 struct iscsi_session *session = conn->session;
1119
1120 if (!ctask->mtask)
1121 return -EINVAL;
1122
Mike Christie77a23c22007-05-30 12:57:18 -05001123 if (!iscsi_remove_mgmt_task(conn->mgmtqueue, ctask->mtask->itt))
Mike Christie7996a772006-04-06 21:13:41 -05001124 list_del(&ctask->mtask->running);
1125 __kfifo_put(session->mgmtpool.queue, (void*)&ctask->mtask,
1126 sizeof(void*));
1127 ctask->mtask = NULL;
1128 return 0;
1129}
1130
1131/*
Mike Christie77a23c22007-05-30 12:57:18 -05001132 * session lock must be held
Mike Christie7996a772006-04-06 21:13:41 -05001133 */
1134static void fail_command(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
1135 int err)
1136{
1137 struct scsi_cmnd *sc;
1138
Mike Christie7996a772006-04-06 21:13:41 -05001139 sc = ctask->sc;
1140 if (!sc)
1141 return;
Mike Christiee648f632006-08-31 18:09:34 -04001142
Mike Christie77a23c22007-05-30 12:57:18 -05001143 if (ctask->state != ISCSI_TASK_PENDING)
1144 conn->session->tt->cleanup_cmd_task(conn, ctask);
Mike Christie7ea8b822006-07-24 15:47:22 -05001145 iscsi_ctask_mtask_cleanup(ctask);
1146
Mike Christie7996a772006-04-06 21:13:41 -05001147 sc->result = err;
FUJITA Tomonori1c138992007-06-14 22:13:17 +09001148 scsi_set_resid(sc, scsi_bufflen(sc));
Mike Christie77a23c22007-05-30 12:57:18 -05001149 if (conn->ctask == ctask)
1150 conn->ctask = NULL;
Mike Christiee648f632006-08-31 18:09:34 -04001151 /* release ref from queuecommand */
Mike Christie60ecebf2006-08-31 18:09:25 -04001152 __iscsi_put_ctask(ctask);
Mike Christie7996a772006-04-06 21:13:41 -05001153}
1154
1155int iscsi_eh_abort(struct scsi_cmnd *sc)
1156{
Mike Christief47f2cf2006-08-31 18:09:33 -04001157 struct iscsi_cmd_task *ctask;
1158 struct iscsi_conn *conn;
1159 struct iscsi_session *session;
Mike Christie7996a772006-04-06 21:13:41 -05001160 int rc;
1161
Mike Christief47f2cf2006-08-31 18:09:33 -04001162 /*
1163 * if session was ISCSI_STATE_IN_RECOVERY then we may not have
1164 * got the command.
1165 */
1166 if (!sc->SCp.ptr) {
1167 debug_scsi("sc never reached iscsi layer or it completed.\n");
1168 return SUCCESS;
1169 }
1170
1171 ctask = (struct iscsi_cmd_task *)sc->SCp.ptr;
1172 conn = ctask->conn;
1173 session = conn->session;
1174
Mike Christie7996a772006-04-06 21:13:41 -05001175 conn->eh_abort_cnt++;
1176 debug_scsi("aborting [sc %p itt 0x%x]\n", sc, ctask->itt);
1177
Mike Christie7996a772006-04-06 21:13:41 -05001178 spin_lock_bh(&session->lock);
1179
1180 /*
1181 * If we are not logged in or we have started a new session
1182 * then let the host reset code handle this
1183 */
1184 if (session->state != ISCSI_STATE_LOGGED_IN ||
1185 sc->SCp.phase != session->age)
1186 goto failed;
1187
1188 /* ctask completed before time out */
Mike Christie7ea8b822006-07-24 15:47:22 -05001189 if (!ctask->sc) {
Mike Christie7ea8b822006-07-24 15:47:22 -05001190 debug_scsi("sc completed while abort in progress\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001191 goto success;
Mike Christie7ea8b822006-07-24 15:47:22 -05001192 }
Mike Christie7996a772006-04-06 21:13:41 -05001193
1194 /* what should we do here ? */
1195 if (conn->ctask == ctask) {
Or Gerlitzbe2df722006-05-02 19:46:43 -05001196 printk(KERN_INFO "iscsi: sc %p itt 0x%x partially sent. "
1197 "Failing abort\n", sc, ctask->itt);
Mike Christie7996a772006-04-06 21:13:41 -05001198 goto failed;
1199 }
1200
Mike Christie77a23c22007-05-30 12:57:18 -05001201 if (ctask->state == ISCSI_TASK_PENDING) {
1202 fail_command(conn, ctask, DID_ABORT << 16);
1203 goto success;
1204 }
Mike Christie7996a772006-04-06 21:13:41 -05001205
1206 conn->tmabort_state = TMABORT_INITIAL;
Mike Christie7996a772006-04-06 21:13:41 -05001207 rc = iscsi_exec_abort_task(sc, ctask);
Mike Christie7996a772006-04-06 21:13:41 -05001208 if (rc || sc->SCp.phase != session->age ||
1209 session->state != ISCSI_STATE_LOGGED_IN)
1210 goto failed;
Mike Christie7ea8b822006-07-24 15:47:22 -05001211 iscsi_ctask_mtask_cleanup(ctask);
Mike Christie7996a772006-04-06 21:13:41 -05001212
Mike Christie7ea8b822006-07-24 15:47:22 -05001213 switch (conn->tmabort_state) {
1214 case TMABORT_SUCCESS:
Mike Christie77a23c22007-05-30 12:57:18 -05001215 spin_unlock_bh(&session->lock);
1216 /*
1217 * clean up task if aborted. grab the recv lock as a writer
1218 */
1219 write_lock_bh(conn->recv_lock);
1220 spin_lock(&session->lock);
1221 fail_command(conn, ctask, DID_ABORT << 16);
1222 spin_unlock(&session->lock);
1223 write_unlock_bh(conn->recv_lock);
1224 /*
1225 * make sure xmit thread is not still touching the
1226 * ctask/scsi_cmnd
1227 */
1228 scsi_flush_work(session->host);
1229 goto success_unlocked;
Mike Christie7ea8b822006-07-24 15:47:22 -05001230 case TMABORT_NOT_FOUND:
1231 if (!ctask->sc) {
1232 /* ctask completed before tmf abort response */
Mike Christie7ea8b822006-07-24 15:47:22 -05001233 debug_scsi("sc completed while abort in progress\n");
Mike Christie77a23c22007-05-30 12:57:18 -05001234 goto success;
Mike Christie7ea8b822006-07-24 15:47:22 -05001235 }
1236 /* fall through */
1237 default:
1238 /* timedout or failed */
Mike Christie7996a772006-04-06 21:13:41 -05001239 spin_unlock_bh(&session->lock);
1240 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie77a23c22007-05-30 12:57:18 -05001241 goto failed_unlocked;
Mike Christie7996a772006-04-06 21:13:41 -05001242 }
1243
Mike Christie77a23c22007-05-30 12:57:18 -05001244success:
Mike Christie7996a772006-04-06 21:13:41 -05001245 spin_unlock_bh(&session->lock);
Mike Christie77a23c22007-05-30 12:57:18 -05001246success_unlocked:
1247 debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
Mike Christie7996a772006-04-06 21:13:41 -05001248 return SUCCESS;
1249
1250failed:
1251 spin_unlock_bh(&session->lock);
Mike Christie77a23c22007-05-30 12:57:18 -05001252failed_unlocked:
Mike Christie7996a772006-04-06 21:13:41 -05001253 debug_scsi("abort failed [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
1254 return FAILED;
1255}
1256EXPORT_SYMBOL_GPL(iscsi_eh_abort);
1257
1258int
1259iscsi_pool_init(struct iscsi_queue *q, int max, void ***items, int item_size)
1260{
1261 int i;
1262
1263 *items = kmalloc(max * sizeof(void*), GFP_KERNEL);
1264 if (*items == NULL)
1265 return -ENOMEM;
1266
1267 q->max = max;
1268 q->pool = kmalloc(max * sizeof(void*), GFP_KERNEL);
1269 if (q->pool == NULL) {
1270 kfree(*items);
1271 return -ENOMEM;
1272 }
1273
1274 q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
1275 GFP_KERNEL, NULL);
1276 if (q->queue == ERR_PTR(-ENOMEM)) {
1277 kfree(q->pool);
1278 kfree(*items);
1279 return -ENOMEM;
1280 }
1281
1282 for (i = 0; i < max; i++) {
1283 q->pool[i] = kmalloc(item_size, GFP_KERNEL);
1284 if (q->pool[i] == NULL) {
1285 int j;
1286
1287 for (j = 0; j < i; j++)
1288 kfree(q->pool[j]);
1289
1290 kfifo_free(q->queue);
1291 kfree(q->pool);
1292 kfree(*items);
1293 return -ENOMEM;
1294 }
1295 memset(q->pool[i], 0, item_size);
1296 (*items)[i] = q->pool[i];
1297 __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
1298 }
1299 return 0;
1300}
1301EXPORT_SYMBOL_GPL(iscsi_pool_init);
1302
1303void iscsi_pool_free(struct iscsi_queue *q, void **items)
1304{
1305 int i;
1306
1307 for (i = 0; i < q->max; i++)
1308 kfree(items[i]);
1309 kfree(q->pool);
1310 kfree(items);
1311}
1312EXPORT_SYMBOL_GPL(iscsi_pool_free);
1313
1314/*
1315 * iSCSI Session's hostdata organization:
1316 *
1317 * *------------------* <== hostdata_session(host->hostdata)
1318 * | ptr to class sess|
1319 * |------------------| <== iscsi_hostdata(host->hostdata)
1320 * | iscsi_session |
1321 * *------------------*
1322 */
1323
1324#define hostdata_privsize(_sz) (sizeof(unsigned long) + _sz + \
1325 _sz % sizeof(unsigned long))
1326
1327#define hostdata_session(_hostdata) (iscsi_ptr(*(unsigned long *)_hostdata))
1328
1329/**
1330 * iscsi_session_setup - create iscsi cls session and host and session
1331 * @scsit: scsi transport template
1332 * @iscsit: iscsi transport template
Mike Christie15482712007-05-30 12:57:19 -05001333 * @cmds_max: scsi host can queue
1334 * @qdepth: scsi host cmds per lun
1335 * @cmd_task_size: LLD ctask private data size
1336 * @mgmt_task_size: LLD mtask private data size
Mike Christie7996a772006-04-06 21:13:41 -05001337 * @initial_cmdsn: initial CmdSN
1338 * @hostno: host no allocated
1339 *
1340 * This can be used by software iscsi_transports that allocate
1341 * a session per scsi host.
1342 **/
1343struct iscsi_cls_session *
1344iscsi_session_setup(struct iscsi_transport *iscsit,
1345 struct scsi_transport_template *scsit,
Mike Christie15482712007-05-30 12:57:19 -05001346 uint16_t cmds_max, uint16_t qdepth,
Mike Christie7996a772006-04-06 21:13:41 -05001347 int cmd_task_size, int mgmt_task_size,
1348 uint32_t initial_cmdsn, uint32_t *hostno)
1349{
1350 struct Scsi_Host *shost;
1351 struct iscsi_session *session;
1352 struct iscsi_cls_session *cls_session;
1353 int cmd_i;
1354
Mike Christie15482712007-05-30 12:57:19 -05001355 if (qdepth > ISCSI_MAX_CMD_PER_LUN || qdepth < 1) {
1356 if (qdepth != 0)
1357 printk(KERN_ERR "iscsi: invalid queue depth of %d. "
1358 "Queue depth must be between 1 and %d.\n",
1359 qdepth, ISCSI_MAX_CMD_PER_LUN);
1360 qdepth = ISCSI_DEF_CMD_PER_LUN;
1361 }
1362
1363 if (cmds_max < 2 || (cmds_max & (cmds_max - 1)) ||
1364 cmds_max >= ISCSI_MGMT_ITT_OFFSET) {
1365 if (cmds_max != 0)
1366 printk(KERN_ERR "iscsi: invalid can_queue of %d. "
1367 "can_queue must be a power of 2 and between "
1368 "2 and %d - setting to %d.\n", cmds_max,
1369 ISCSI_MGMT_ITT_OFFSET, ISCSI_DEF_XMIT_CMDS_MAX);
1370 cmds_max = ISCSI_DEF_XMIT_CMDS_MAX;
1371 }
1372
Mike Christie7996a772006-04-06 21:13:41 -05001373 shost = scsi_host_alloc(iscsit->host_template,
1374 hostdata_privsize(sizeof(*session)));
1375 if (!shost)
1376 return NULL;
1377
Mike Christie15482712007-05-30 12:57:19 -05001378 /* the iscsi layer takes one task for reserve */
1379 shost->can_queue = cmds_max - 1;
1380 shost->cmd_per_lun = qdepth;
Mike Christie7996a772006-04-06 21:13:41 -05001381 shost->max_id = 1;
1382 shost->max_channel = 0;
1383 shost->max_lun = iscsit->max_lun;
1384 shost->max_cmd_len = iscsit->max_cmd_len;
1385 shost->transportt = scsit;
1386 shost->transportt->create_work_queue = 1;
1387 *hostno = shost->host_no;
1388
1389 session = iscsi_hostdata(shost->hostdata);
1390 memset(session, 0, sizeof(struct iscsi_session));
1391 session->host = shost;
1392 session->state = ISCSI_STATE_FREE;
1393 session->mgmtpool_max = ISCSI_MGMT_CMDS_MAX;
Mike Christie15482712007-05-30 12:57:19 -05001394 session->cmds_max = cmds_max;
Mike Christie7996a772006-04-06 21:13:41 -05001395 session->cmdsn = initial_cmdsn;
1396 session->exp_cmdsn = initial_cmdsn + 1;
1397 session->max_cmdsn = initial_cmdsn + 1;
1398 session->max_r2t = 1;
1399 session->tt = iscsit;
1400
1401 /* initialize SCSI PDU commands pool */
1402 if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
1403 (void***)&session->cmds,
1404 cmd_task_size + sizeof(struct iscsi_cmd_task)))
1405 goto cmdpool_alloc_fail;
1406
1407 /* pre-format cmds pool with ITT */
1408 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
1409 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
1410
1411 if (cmd_task_size)
1412 ctask->dd_data = &ctask[1];
1413 ctask->itt = cmd_i;
Mike Christieb6c395ed2006-07-24 15:47:15 -05001414 INIT_LIST_HEAD(&ctask->running);
Mike Christie7996a772006-04-06 21:13:41 -05001415 }
1416
1417 spin_lock_init(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001418
1419 /* initialize immediate command pool */
1420 if (iscsi_pool_init(&session->mgmtpool, session->mgmtpool_max,
1421 (void***)&session->mgmt_cmds,
1422 mgmt_task_size + sizeof(struct iscsi_mgmt_task)))
1423 goto mgmtpool_alloc_fail;
1424
1425
1426 /* pre-format immediate cmds pool with ITT */
1427 for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
1428 struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
1429
1430 if (mgmt_task_size)
1431 mtask->dd_data = &mtask[1];
1432 mtask->itt = ISCSI_MGMT_ITT_OFFSET + cmd_i;
Mike Christieb6c395ed2006-07-24 15:47:15 -05001433 INIT_LIST_HEAD(&mtask->running);
Mike Christie7996a772006-04-06 21:13:41 -05001434 }
1435
1436 if (scsi_add_host(shost, NULL))
1437 goto add_host_fail;
1438
Mike Christief53a88d2006-06-28 12:00:27 -05001439 if (!try_module_get(iscsit->owner))
1440 goto cls_session_fail;
1441
Mike Christie6a8a0d32006-06-28 12:00:31 -05001442 cls_session = iscsi_create_session(shost, iscsit, 0);
Mike Christie7996a772006-04-06 21:13:41 -05001443 if (!cls_session)
Mike Christief53a88d2006-06-28 12:00:27 -05001444 goto module_put;
Mike Christie7996a772006-04-06 21:13:41 -05001445 *(unsigned long*)shost->hostdata = (unsigned long)cls_session;
1446
1447 return cls_session;
1448
Mike Christief53a88d2006-06-28 12:00:27 -05001449module_put:
1450 module_put(iscsit->owner);
Mike Christie7996a772006-04-06 21:13:41 -05001451cls_session_fail:
1452 scsi_remove_host(shost);
1453add_host_fail:
Mike Christie7996a772006-04-06 21:13:41 -05001454 iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
1455mgmtpool_alloc_fail:
1456 iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
1457cmdpool_alloc_fail:
1458 scsi_host_put(shost);
1459 return NULL;
1460}
1461EXPORT_SYMBOL_GPL(iscsi_session_setup);
1462
1463/**
1464 * iscsi_session_teardown - destroy session, host, and cls_session
1465 * shost: scsi host
1466 *
1467 * This can be used by software iscsi_transports that allocate
1468 * a session per scsi host.
1469 **/
1470void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
1471{
1472 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
1473 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
Mike Christie63f75cc2006-07-24 15:47:29 -05001474 struct module *owner = cls_session->transport->owner;
Mike Christie7996a772006-04-06 21:13:41 -05001475
1476 scsi_remove_host(shost);
1477
Mike Christie7996a772006-04-06 21:13:41 -05001478 iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
1479 iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
1480
Mike Christieb2c64162007-05-30 12:57:16 -05001481 kfree(session->password);
1482 kfree(session->password_in);
1483 kfree(session->username);
1484 kfree(session->username_in);
Mike Christief3ff0c32006-07-24 15:47:50 -05001485 kfree(session->targetname);
Mike Christied8196ed2007-05-30 12:57:25 -05001486 kfree(session->netdev);
Mike Christie0801c242007-05-30 12:57:12 -05001487 kfree(session->hwaddress);
Mike Christie8ad57812007-05-30 12:57:13 -05001488 kfree(session->initiatorname);
Mike Christief3ff0c32006-07-24 15:47:50 -05001489
Mike Christie7996a772006-04-06 21:13:41 -05001490 iscsi_destroy_session(cls_session);
1491 scsi_host_put(shost);
Mike Christie63f75cc2006-07-24 15:47:29 -05001492 module_put(owner);
Mike Christie7996a772006-04-06 21:13:41 -05001493}
1494EXPORT_SYMBOL_GPL(iscsi_session_teardown);
1495
1496/**
1497 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
1498 * @cls_session: iscsi_cls_session
1499 * @conn_idx: cid
1500 **/
1501struct iscsi_cls_conn *
1502iscsi_conn_setup(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
1503{
1504 struct iscsi_session *session = class_to_transport_session(cls_session);
1505 struct iscsi_conn *conn;
1506 struct iscsi_cls_conn *cls_conn;
Mike Christied36ab6f2006-05-18 20:31:34 -05001507 char *data;
Mike Christie7996a772006-04-06 21:13:41 -05001508
1509 cls_conn = iscsi_create_conn(cls_session, conn_idx);
1510 if (!cls_conn)
1511 return NULL;
1512 conn = cls_conn->dd_data;
1513 memset(conn, 0, sizeof(*conn));
1514
1515 conn->session = session;
1516 conn->cls_conn = cls_conn;
1517 conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
1518 conn->id = conn_idx;
1519 conn->exp_statsn = 0;
1520 conn->tmabort_state = TMABORT_INITIAL;
1521 INIT_LIST_HEAD(&conn->run_list);
1522 INIT_LIST_HEAD(&conn->mgmt_run_list);
Mike Christieb6c395ed2006-07-24 15:47:15 -05001523 INIT_LIST_HEAD(&conn->xmitqueue);
Mike Christie7996a772006-04-06 21:13:41 -05001524
1525 /* initialize general immediate & non-immediate PDU commands queue */
Mike Christie7996a772006-04-06 21:13:41 -05001526 conn->mgmtqueue = kfifo_alloc(session->mgmtpool_max * sizeof(void*),
1527 GFP_KERNEL, NULL);
1528 if (conn->mgmtqueue == ERR_PTR(-ENOMEM))
1529 goto mgmtqueue_alloc_fail;
1530
David Howellsc4028952006-11-22 14:57:56 +00001531 INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
Mike Christie7996a772006-04-06 21:13:41 -05001532
1533 /* allocate login_mtask used for the login/text sequences */
1534 spin_lock_bh(&session->lock);
1535 if (!__kfifo_get(session->mgmtpool.queue,
1536 (void*)&conn->login_mtask,
1537 sizeof(void*))) {
1538 spin_unlock_bh(&session->lock);
1539 goto login_mtask_alloc_fail;
1540 }
1541 spin_unlock_bh(&session->lock);
1542
Mike Christiebf32ed32007-02-28 17:32:17 -06001543 data = kmalloc(ISCSI_DEF_MAX_RECV_SEG_LEN, GFP_KERNEL);
Mike Christied36ab6f2006-05-18 20:31:34 -05001544 if (!data)
1545 goto login_mtask_data_alloc_fail;
Mike Christiec8dc1e52006-07-24 15:47:39 -05001546 conn->login_mtask->data = conn->data = data;
Mike Christied36ab6f2006-05-18 20:31:34 -05001547
Mike Christie7996a772006-04-06 21:13:41 -05001548 init_timer(&conn->tmabort_timer);
Mike Christie7996a772006-04-06 21:13:41 -05001549 init_waitqueue_head(&conn->ehwait);
1550
1551 return cls_conn;
1552
Mike Christied36ab6f2006-05-18 20:31:34 -05001553login_mtask_data_alloc_fail:
1554 __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
1555 sizeof(void*));
Mike Christie7996a772006-04-06 21:13:41 -05001556login_mtask_alloc_fail:
1557 kfifo_free(conn->mgmtqueue);
1558mgmtqueue_alloc_fail:
Mike Christie7996a772006-04-06 21:13:41 -05001559 iscsi_destroy_conn(cls_conn);
1560 return NULL;
1561}
1562EXPORT_SYMBOL_GPL(iscsi_conn_setup);
1563
1564/**
1565 * iscsi_conn_teardown - teardown iscsi connection
1566 * cls_conn: iscsi class connection
1567 *
1568 * TODO: we may need to make this into a two step process
1569 * like scsi-mls remove + put host
1570 */
1571void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
1572{
1573 struct iscsi_conn *conn = cls_conn->dd_data;
1574 struct iscsi_session *session = conn->session;
1575 unsigned long flags;
1576
Mike Christie7996a772006-04-06 21:13:41 -05001577 spin_lock_bh(&session->lock);
Mike Christie77a23c22007-05-30 12:57:18 -05001578 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
Mike Christie7996a772006-04-06 21:13:41 -05001579 conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
1580 if (session->leadconn == conn) {
1581 /*
1582 * leading connection? then give up on recovery.
1583 */
1584 session->state = ISCSI_STATE_TERMINATE;
1585 wake_up(&conn->ehwait);
1586 }
1587 spin_unlock_bh(&session->lock);
1588
Mike Christie7996a772006-04-06 21:13:41 -05001589 /*
1590 * Block until all in-progress commands for this connection
1591 * time out or fail.
1592 */
1593 for (;;) {
1594 spin_lock_irqsave(session->host->host_lock, flags);
1595 if (!session->host->host_busy) { /* OK for ERL == 0 */
1596 spin_unlock_irqrestore(session->host->host_lock, flags);
1597 break;
1598 }
1599 spin_unlock_irqrestore(session->host->host_lock, flags);
1600 msleep_interruptible(500);
Or Gerlitzbe2df722006-05-02 19:46:43 -05001601 printk(KERN_INFO "iscsi: scsi conn_destroy(): host_busy %d "
1602 "host_failed %d\n", session->host->host_busy,
1603 session->host->host_failed);
Mike Christie7996a772006-04-06 21:13:41 -05001604 /*
1605 * force eh_abort() to unblock
1606 */
1607 wake_up(&conn->ehwait);
1608 }
1609
Mike Christie779ea122007-02-28 17:32:15 -06001610 /* flush queued up work because we free the connection below */
1611 scsi_flush_work(session->host);
1612
Mike Christie7996a772006-04-06 21:13:41 -05001613 spin_lock_bh(&session->lock);
Mike Christiec8dc1e52006-07-24 15:47:39 -05001614 kfree(conn->data);
Mike Christief3ff0c32006-07-24 15:47:50 -05001615 kfree(conn->persistent_address);
Mike Christie7996a772006-04-06 21:13:41 -05001616 __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
1617 sizeof(void*));
Mike Christie98644042006-10-16 18:09:39 -04001618 if (session->leadconn == conn) {
Mike Christie7996a772006-04-06 21:13:41 -05001619 session->leadconn = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05001620 /* no connections exits.. reset sequencing */
1621 session->cmdsn = session->max_cmdsn = session->exp_cmdsn = 1;
Mike Christie98644042006-10-16 18:09:39 -04001622 }
Mike Christie7996a772006-04-06 21:13:41 -05001623 spin_unlock_bh(&session->lock);
1624
Mike Christie7996a772006-04-06 21:13:41 -05001625 kfifo_free(conn->mgmtqueue);
1626
1627 iscsi_destroy_conn(cls_conn);
1628}
1629EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
1630
1631int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
1632{
1633 struct iscsi_conn *conn = cls_conn->dd_data;
1634 struct iscsi_session *session = conn->session;
1635
Mike Christieffd04362006-08-31 18:09:24 -04001636 if (!session) {
Mike Christie7996a772006-04-06 21:13:41 -05001637 printk(KERN_ERR "iscsi: can't start unbound connection\n");
1638 return -EPERM;
1639 }
1640
Mike Christiedb98ccd2006-08-31 18:09:31 -04001641 if ((session->imm_data_en || !session->initial_r2t_en) &&
1642 session->first_burst > session->max_burst) {
Mike Christieffd04362006-08-31 18:09:24 -04001643 printk("iscsi: invalid burst lengths: "
1644 "first_burst %d max_burst %d\n",
1645 session->first_burst, session->max_burst);
1646 return -EINVAL;
1647 }
1648
Mike Christie7996a772006-04-06 21:13:41 -05001649 spin_lock_bh(&session->lock);
1650 conn->c_stage = ISCSI_CONN_STARTED;
1651 session->state = ISCSI_STATE_LOGGED_IN;
1652
1653 switch(conn->stop_stage) {
1654 case STOP_CONN_RECOVER:
1655 /*
1656 * unblock eh_abort() if it is blocked. re-try all
1657 * commands after successful recovery
1658 */
Mike Christie7996a772006-04-06 21:13:41 -05001659 conn->stop_stage = 0;
1660 conn->tmabort_state = TMABORT_INITIAL;
1661 session->age++;
Mike Christie7996a772006-04-06 21:13:41 -05001662 spin_unlock_bh(&session->lock);
1663
1664 iscsi_unblock_session(session_to_cls(session));
1665 wake_up(&conn->ehwait);
1666 return 0;
1667 case STOP_CONN_TERM:
Mike Christie7996a772006-04-06 21:13:41 -05001668 conn->stop_stage = 0;
1669 break;
Mike Christie7996a772006-04-06 21:13:41 -05001670 default:
1671 break;
1672 }
1673 spin_unlock_bh(&session->lock);
1674
1675 return 0;
1676}
1677EXPORT_SYMBOL_GPL(iscsi_conn_start);
1678
1679static void
1680flush_control_queues(struct iscsi_session *session, struct iscsi_conn *conn)
1681{
1682 struct iscsi_mgmt_task *mtask, *tmp;
1683
1684 /* handle pending */
Mike Christie77a23c22007-05-30 12:57:18 -05001685 while (__kfifo_get(conn->mgmtqueue, (void*)&mtask, sizeof(void*))) {
Mike Christie7996a772006-04-06 21:13:41 -05001686 if (mtask == conn->login_mtask)
1687 continue;
1688 debug_scsi("flushing pending mgmt task itt 0x%x\n", mtask->itt);
1689 __kfifo_put(session->mgmtpool.queue, (void*)&mtask,
1690 sizeof(void*));
1691 }
1692
1693 /* handle running */
1694 list_for_each_entry_safe(mtask, tmp, &conn->mgmt_run_list, running) {
1695 debug_scsi("flushing running mgmt task itt 0x%x\n", mtask->itt);
Mike Christieed2abc72006-05-02 19:46:40 -05001696 list_del(&mtask->running);
1697
Mike Christie7996a772006-04-06 21:13:41 -05001698 if (mtask == conn->login_mtask)
1699 continue;
Mike Christieed2abc72006-05-02 19:46:40 -05001700 __kfifo_put(session->mgmtpool.queue, (void*)&mtask,
Mike Christie7996a772006-04-06 21:13:41 -05001701 sizeof(void*));
1702 }
1703
1704 conn->mtask = NULL;
1705}
1706
1707/* Fail commands. Mutex and session lock held and recv side suspended */
1708static void fail_all_commands(struct iscsi_conn *conn)
1709{
1710 struct iscsi_cmd_task *ctask, *tmp;
1711
1712 /* flush pending */
Mike Christieb6c395ed2006-07-24 15:47:15 -05001713 list_for_each_entry_safe(ctask, tmp, &conn->xmitqueue, running) {
Mike Christie7996a772006-04-06 21:13:41 -05001714 debug_scsi("failing pending sc %p itt 0x%x\n", ctask->sc,
1715 ctask->itt);
1716 fail_command(conn, ctask, DID_BUS_BUSY << 16);
1717 }
1718
1719 /* fail all other running */
1720 list_for_each_entry_safe(ctask, tmp, &conn->run_list, running) {
1721 debug_scsi("failing in progress sc %p itt 0x%x\n",
1722 ctask->sc, ctask->itt);
1723 fail_command(conn, ctask, DID_BUS_BUSY << 16);
1724 }
1725
1726 conn->ctask = NULL;
1727}
1728
Mike Christie656cffc2006-05-18 20:31:42 -05001729static void iscsi_start_session_recovery(struct iscsi_session *session,
1730 struct iscsi_conn *conn, int flag)
Mike Christie7996a772006-04-06 21:13:41 -05001731{
Mike Christieed2abc72006-05-02 19:46:40 -05001732 int old_stop_stage;
1733
Mike Christie7996a772006-04-06 21:13:41 -05001734 spin_lock_bh(&session->lock);
Mike Christieed2abc72006-05-02 19:46:40 -05001735 if (conn->stop_stage == STOP_CONN_TERM) {
Mike Christie7996a772006-04-06 21:13:41 -05001736 spin_unlock_bh(&session->lock);
1737 return;
1738 }
Mike Christieed2abc72006-05-02 19:46:40 -05001739
1740 /*
1741 * When this is called for the in_login state, we only want to clean
Mike Christie67a61112006-05-30 00:37:20 -05001742 * up the login task and connection. We do not need to block and set
1743 * the recovery state again
Mike Christieed2abc72006-05-02 19:46:40 -05001744 */
Mike Christie67a61112006-05-30 00:37:20 -05001745 if (flag == STOP_CONN_TERM)
1746 session->state = ISCSI_STATE_TERMINATE;
1747 else if (conn->stop_stage != STOP_CONN_RECOVER)
1748 session->state = ISCSI_STATE_IN_RECOVERY;
Mike Christieed2abc72006-05-02 19:46:40 -05001749
1750 old_stop_stage = conn->stop_stage;
Mike Christie7996a772006-04-06 21:13:41 -05001751 conn->stop_stage = flag;
Mike Christie67a61112006-05-30 00:37:20 -05001752 conn->c_stage = ISCSI_CONN_STOPPED;
1753 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
Mike Christie7996a772006-04-06 21:13:41 -05001754 spin_unlock_bh(&session->lock);
Mike Christie77a23c22007-05-30 12:57:18 -05001755 scsi_flush_work(session->host);
Mike Christie7996a772006-04-06 21:13:41 -05001756
Mike Christie1c834692006-07-24 15:47:26 -05001757 write_lock_bh(conn->recv_lock);
1758 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
1759 write_unlock_bh(conn->recv_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001760
Mike Christie7996a772006-04-06 21:13:41 -05001761 /*
1762 * for connection level recovery we should not calculate
1763 * header digest. conn->hdr_size used for optimization
1764 * in hdr_extract() and will be re-negotiated at
1765 * set_param() time.
1766 */
1767 if (flag == STOP_CONN_RECOVER) {
1768 conn->hdrdgst_en = 0;
1769 conn->datadgst_en = 0;
Mike Christie656cffc2006-05-18 20:31:42 -05001770 if (session->state == ISCSI_STATE_IN_RECOVERY &&
Mike Christie67a61112006-05-30 00:37:20 -05001771 old_stop_stage != STOP_CONN_RECOVER) {
1772 debug_scsi("blocking session\n");
Mike Christie7996a772006-04-06 21:13:41 -05001773 iscsi_block_session(session_to_cls(session));
Mike Christie67a61112006-05-30 00:37:20 -05001774 }
Mike Christie7996a772006-04-06 21:13:41 -05001775 }
Mike Christie656cffc2006-05-18 20:31:42 -05001776
Mike Christie656cffc2006-05-18 20:31:42 -05001777 /*
1778 * flush queues.
1779 */
1780 spin_lock_bh(&session->lock);
1781 fail_all_commands(conn);
1782 flush_control_queues(session, conn);
1783 spin_unlock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001784}
Mike Christie7996a772006-04-06 21:13:41 -05001785
1786void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
1787{
1788 struct iscsi_conn *conn = cls_conn->dd_data;
1789 struct iscsi_session *session = conn->session;
1790
1791 switch (flag) {
1792 case STOP_CONN_RECOVER:
1793 case STOP_CONN_TERM:
1794 iscsi_start_session_recovery(session, conn, flag);
Mike Christie8d2860b2006-05-02 19:46:47 -05001795 break;
Mike Christie7996a772006-04-06 21:13:41 -05001796 default:
Or Gerlitzbe2df722006-05-02 19:46:43 -05001797 printk(KERN_ERR "iscsi: invalid stop flag %d\n", flag);
Mike Christie7996a772006-04-06 21:13:41 -05001798 }
1799}
1800EXPORT_SYMBOL_GPL(iscsi_conn_stop);
1801
1802int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
1803 struct iscsi_cls_conn *cls_conn, int is_leading)
1804{
1805 struct iscsi_session *session = class_to_transport_session(cls_session);
Mike Christie98644042006-10-16 18:09:39 -04001806 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05001807
Mike Christie7996a772006-04-06 21:13:41 -05001808 spin_lock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001809 if (is_leading)
1810 session->leadconn = conn;
Mike Christie98644042006-10-16 18:09:39 -04001811 spin_unlock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001812
1813 /*
1814 * Unblock xmitworker(), Login Phase will pass through.
1815 */
1816 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
1817 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1818 return 0;
1819}
1820EXPORT_SYMBOL_GPL(iscsi_conn_bind);
1821
Mike Christiea54a52c2006-06-28 12:00:23 -05001822
1823int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
1824 enum iscsi_param param, char *buf, int buflen)
1825{
1826 struct iscsi_conn *conn = cls_conn->dd_data;
1827 struct iscsi_session *session = conn->session;
1828 uint32_t value;
1829
1830 switch(param) {
1831 case ISCSI_PARAM_MAX_RECV_DLENGTH:
1832 sscanf(buf, "%d", &conn->max_recv_dlength);
1833 break;
1834 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
1835 sscanf(buf, "%d", &conn->max_xmit_dlength);
1836 break;
1837 case ISCSI_PARAM_HDRDGST_EN:
1838 sscanf(buf, "%d", &conn->hdrdgst_en);
1839 break;
1840 case ISCSI_PARAM_DATADGST_EN:
1841 sscanf(buf, "%d", &conn->datadgst_en);
1842 break;
1843 case ISCSI_PARAM_INITIAL_R2T_EN:
1844 sscanf(buf, "%d", &session->initial_r2t_en);
1845 break;
1846 case ISCSI_PARAM_MAX_R2T:
1847 sscanf(buf, "%d", &session->max_r2t);
1848 break;
1849 case ISCSI_PARAM_IMM_DATA_EN:
1850 sscanf(buf, "%d", &session->imm_data_en);
1851 break;
1852 case ISCSI_PARAM_FIRST_BURST:
1853 sscanf(buf, "%d", &session->first_burst);
1854 break;
1855 case ISCSI_PARAM_MAX_BURST:
1856 sscanf(buf, "%d", &session->max_burst);
1857 break;
1858 case ISCSI_PARAM_PDU_INORDER_EN:
1859 sscanf(buf, "%d", &session->pdu_inorder_en);
1860 break;
1861 case ISCSI_PARAM_DATASEQ_INORDER_EN:
1862 sscanf(buf, "%d", &session->dataseq_inorder_en);
1863 break;
1864 case ISCSI_PARAM_ERL:
1865 sscanf(buf, "%d", &session->erl);
1866 break;
1867 case ISCSI_PARAM_IFMARKER_EN:
1868 sscanf(buf, "%d", &value);
1869 BUG_ON(value);
1870 break;
1871 case ISCSI_PARAM_OFMARKER_EN:
1872 sscanf(buf, "%d", &value);
1873 BUG_ON(value);
1874 break;
1875 case ISCSI_PARAM_EXP_STATSN:
1876 sscanf(buf, "%u", &conn->exp_statsn);
1877 break;
Mike Christieb2c64162007-05-30 12:57:16 -05001878 case ISCSI_PARAM_USERNAME:
1879 kfree(session->username);
1880 session->username = kstrdup(buf, GFP_KERNEL);
1881 if (!session->username)
1882 return -ENOMEM;
1883 break;
1884 case ISCSI_PARAM_USERNAME_IN:
1885 kfree(session->username_in);
1886 session->username_in = kstrdup(buf, GFP_KERNEL);
1887 if (!session->username_in)
1888 return -ENOMEM;
1889 break;
1890 case ISCSI_PARAM_PASSWORD:
1891 kfree(session->password);
1892 session->password = kstrdup(buf, GFP_KERNEL);
1893 if (!session->password)
1894 return -ENOMEM;
1895 break;
1896 case ISCSI_PARAM_PASSWORD_IN:
1897 kfree(session->password_in);
1898 session->password_in = kstrdup(buf, GFP_KERNEL);
1899 if (!session->password_in)
1900 return -ENOMEM;
1901 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05001902 case ISCSI_PARAM_TARGET_NAME:
1903 /* this should not change between logins */
1904 if (session->targetname)
1905 break;
1906
1907 session->targetname = kstrdup(buf, GFP_KERNEL);
1908 if (!session->targetname)
1909 return -ENOMEM;
1910 break;
1911 case ISCSI_PARAM_TPGT:
1912 sscanf(buf, "%d", &session->tpgt);
1913 break;
1914 case ISCSI_PARAM_PERSISTENT_PORT:
1915 sscanf(buf, "%d", &conn->persistent_port);
1916 break;
1917 case ISCSI_PARAM_PERSISTENT_ADDRESS:
1918 /*
1919 * this is the address returned in discovery so it should
1920 * not change between logins.
1921 */
1922 if (conn->persistent_address)
1923 break;
1924
1925 conn->persistent_address = kstrdup(buf, GFP_KERNEL);
1926 if (!conn->persistent_address)
1927 return -ENOMEM;
1928 break;
1929 default:
1930 return -ENOSYS;
1931 }
1932
1933 return 0;
1934}
1935EXPORT_SYMBOL_GPL(iscsi_set_param);
1936
1937int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
1938 enum iscsi_param param, char *buf)
1939{
1940 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
1941 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
1942 int len;
1943
1944 switch(param) {
1945 case ISCSI_PARAM_INITIAL_R2T_EN:
1946 len = sprintf(buf, "%d\n", session->initial_r2t_en);
1947 break;
1948 case ISCSI_PARAM_MAX_R2T:
1949 len = sprintf(buf, "%hu\n", session->max_r2t);
1950 break;
1951 case ISCSI_PARAM_IMM_DATA_EN:
1952 len = sprintf(buf, "%d\n", session->imm_data_en);
1953 break;
1954 case ISCSI_PARAM_FIRST_BURST:
1955 len = sprintf(buf, "%u\n", session->first_burst);
1956 break;
1957 case ISCSI_PARAM_MAX_BURST:
1958 len = sprintf(buf, "%u\n", session->max_burst);
1959 break;
1960 case ISCSI_PARAM_PDU_INORDER_EN:
1961 len = sprintf(buf, "%d\n", session->pdu_inorder_en);
1962 break;
1963 case ISCSI_PARAM_DATASEQ_INORDER_EN:
1964 len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
1965 break;
1966 case ISCSI_PARAM_ERL:
1967 len = sprintf(buf, "%d\n", session->erl);
1968 break;
1969 case ISCSI_PARAM_TARGET_NAME:
1970 len = sprintf(buf, "%s\n", session->targetname);
1971 break;
1972 case ISCSI_PARAM_TPGT:
1973 len = sprintf(buf, "%d\n", session->tpgt);
1974 break;
Mike Christieb2c64162007-05-30 12:57:16 -05001975 case ISCSI_PARAM_USERNAME:
1976 len = sprintf(buf, "%s\n", session->username);
1977 break;
1978 case ISCSI_PARAM_USERNAME_IN:
1979 len = sprintf(buf, "%s\n", session->username_in);
1980 break;
1981 case ISCSI_PARAM_PASSWORD:
1982 len = sprintf(buf, "%s\n", session->password);
1983 break;
1984 case ISCSI_PARAM_PASSWORD_IN:
1985 len = sprintf(buf, "%s\n", session->password_in);
1986 break;
Mike Christiea54a52c2006-06-28 12:00:23 -05001987 default:
1988 return -ENOSYS;
1989 }
1990
1991 return len;
1992}
1993EXPORT_SYMBOL_GPL(iscsi_session_get_param);
1994
1995int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
1996 enum iscsi_param param, char *buf)
1997{
1998 struct iscsi_conn *conn = cls_conn->dd_data;
1999 int len;
2000
2001 switch(param) {
2002 case ISCSI_PARAM_MAX_RECV_DLENGTH:
2003 len = sprintf(buf, "%u\n", conn->max_recv_dlength);
2004 break;
2005 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2006 len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
2007 break;
2008 case ISCSI_PARAM_HDRDGST_EN:
2009 len = sprintf(buf, "%d\n", conn->hdrdgst_en);
2010 break;
2011 case ISCSI_PARAM_DATADGST_EN:
2012 len = sprintf(buf, "%d\n", conn->datadgst_en);
2013 break;
2014 case ISCSI_PARAM_IFMARKER_EN:
2015 len = sprintf(buf, "%d\n", conn->ifmarker_en);
2016 break;
2017 case ISCSI_PARAM_OFMARKER_EN:
2018 len = sprintf(buf, "%d\n", conn->ofmarker_en);
2019 break;
2020 case ISCSI_PARAM_EXP_STATSN:
2021 len = sprintf(buf, "%u\n", conn->exp_statsn);
2022 break;
2023 case ISCSI_PARAM_PERSISTENT_PORT:
2024 len = sprintf(buf, "%d\n", conn->persistent_port);
2025 break;
2026 case ISCSI_PARAM_PERSISTENT_ADDRESS:
2027 len = sprintf(buf, "%s\n", conn->persistent_address);
2028 break;
2029 default:
2030 return -ENOSYS;
2031 }
2032
2033 return len;
2034}
2035EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
2036
Mike Christie0801c242007-05-30 12:57:12 -05002037int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2038 char *buf)
2039{
2040 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
2041 int len;
2042
2043 switch (param) {
Mike Christied8196ed2007-05-30 12:57:25 -05002044 case ISCSI_HOST_PARAM_NETDEV_NAME:
2045 if (!session->netdev)
2046 len = sprintf(buf, "%s\n", "default");
2047 else
2048 len = sprintf(buf, "%s\n", session->netdev);
2049 break;
Mike Christie0801c242007-05-30 12:57:12 -05002050 case ISCSI_HOST_PARAM_HWADDRESS:
2051 if (!session->hwaddress)
2052 len = sprintf(buf, "%s\n", "default");
2053 else
2054 len = sprintf(buf, "%s\n", session->hwaddress);
2055 break;
Mike Christie8ad57812007-05-30 12:57:13 -05002056 case ISCSI_HOST_PARAM_INITIATOR_NAME:
2057 if (!session->initiatorname)
2058 len = sprintf(buf, "%s\n", "unknown");
2059 else
2060 len = sprintf(buf, "%s\n", session->initiatorname);
2061 break;
2062
Mike Christie0801c242007-05-30 12:57:12 -05002063 default:
2064 return -ENOSYS;
2065 }
2066
2067 return len;
2068}
2069EXPORT_SYMBOL_GPL(iscsi_host_get_param);
2070
2071int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2072 char *buf, int buflen)
2073{
2074 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
2075
2076 switch (param) {
Mike Christied8196ed2007-05-30 12:57:25 -05002077 case ISCSI_HOST_PARAM_NETDEV_NAME:
2078 if (!session->netdev)
2079 session->netdev = kstrdup(buf, GFP_KERNEL);
2080 break;
Mike Christie0801c242007-05-30 12:57:12 -05002081 case ISCSI_HOST_PARAM_HWADDRESS:
2082 if (!session->hwaddress)
2083 session->hwaddress = kstrdup(buf, GFP_KERNEL);
2084 break;
Mike Christie8ad57812007-05-30 12:57:13 -05002085 case ISCSI_HOST_PARAM_INITIATOR_NAME:
2086 if (!session->initiatorname)
2087 session->initiatorname = kstrdup(buf, GFP_KERNEL);
2088 break;
Mike Christie0801c242007-05-30 12:57:12 -05002089 default:
2090 return -ENOSYS;
2091 }
2092
2093 return 0;
2094}
2095EXPORT_SYMBOL_GPL(iscsi_host_set_param);
2096
Mike Christie7996a772006-04-06 21:13:41 -05002097MODULE_AUTHOR("Mike Christie");
2098MODULE_DESCRIPTION("iSCSI library functions");
2099MODULE_LICENSE("GPL");