blob: 5e6a42429c39ee9b1bde05717f46bb02cfd5b16b [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>
25#include <linux/mutex.h>
26#include <linux/kfifo.h>
27#include <linux/delay.h>
Mike Christie8eb00532007-02-28 17:32:19 -060028#include <asm/unaligned.h>
Mike Christie7996a772006-04-06 21:13:41 -050029#include <net/tcp.h>
30#include <scsi/scsi_cmnd.h>
31#include <scsi/scsi_device.h>
32#include <scsi/scsi_eh.h>
33#include <scsi/scsi_tcq.h>
34#include <scsi/scsi_host.h>
35#include <scsi/scsi.h>
36#include <scsi/iscsi_proto.h>
37#include <scsi/scsi_transport.h>
38#include <scsi/scsi_transport_iscsi.h>
39#include <scsi/libiscsi.h>
40
41struct iscsi_session *
42class_to_transport_session(struct iscsi_cls_session *cls_session)
43{
44 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
45 return iscsi_hostdata(shost->hostdata);
46}
47EXPORT_SYMBOL_GPL(class_to_transport_session);
48
49#define INVALID_SN_DELTA 0xffff
50
51int
52iscsi_check_assign_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr)
53{
54 uint32_t max_cmdsn = be32_to_cpu(hdr->max_cmdsn);
55 uint32_t exp_cmdsn = be32_to_cpu(hdr->exp_cmdsn);
56
57 if (max_cmdsn < exp_cmdsn -1 &&
58 max_cmdsn > exp_cmdsn - INVALID_SN_DELTA)
59 return ISCSI_ERR_MAX_CMDSN;
60 if (max_cmdsn > session->max_cmdsn ||
61 max_cmdsn < session->max_cmdsn - INVALID_SN_DELTA)
62 session->max_cmdsn = max_cmdsn;
63 if (exp_cmdsn > session->exp_cmdsn ||
64 exp_cmdsn < session->exp_cmdsn - INVALID_SN_DELTA)
65 session->exp_cmdsn = exp_cmdsn;
66
67 return 0;
68}
69EXPORT_SYMBOL_GPL(iscsi_check_assign_cmdsn);
70
71void iscsi_prep_unsolicit_data_pdu(struct iscsi_cmd_task *ctask,
Mike Christieffd04362006-08-31 18:09:24 -040072 struct iscsi_data *hdr)
Mike Christie7996a772006-04-06 21:13:41 -050073{
74 struct iscsi_conn *conn = ctask->conn;
75
76 memset(hdr, 0, sizeof(struct iscsi_data));
77 hdr->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
78 hdr->datasn = cpu_to_be32(ctask->unsol_datasn);
79 ctask->unsol_datasn++;
80 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
81 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
82
83 hdr->itt = ctask->hdr->itt;
84 hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
Mike Christieffd04362006-08-31 18:09:24 -040085 hdr->offset = cpu_to_be32(ctask->unsol_offset);
Mike Christie7996a772006-04-06 21:13:41 -050086
87 if (ctask->unsol_count > conn->max_xmit_dlength) {
88 hton24(hdr->dlength, conn->max_xmit_dlength);
89 ctask->data_count = conn->max_xmit_dlength;
Mike Christieffd04362006-08-31 18:09:24 -040090 ctask->unsol_offset += ctask->data_count;
Mike Christie7996a772006-04-06 21:13:41 -050091 hdr->flags = 0;
92 } else {
93 hton24(hdr->dlength, ctask->unsol_count);
94 ctask->data_count = ctask->unsol_count;
95 hdr->flags = ISCSI_FLAG_CMD_FINAL;
96 }
97}
98EXPORT_SYMBOL_GPL(iscsi_prep_unsolicit_data_pdu);
99
100/**
101 * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
102 * @ctask: iscsi cmd task
103 *
104 * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
105 * fields like dlength or final based on how much data it sends
106 */
107static void iscsi_prep_scsi_cmd_pdu(struct iscsi_cmd_task *ctask)
108{
109 struct iscsi_conn *conn = ctask->conn;
110 struct iscsi_session *session = conn->session;
111 struct iscsi_cmd *hdr = ctask->hdr;
112 struct scsi_cmnd *sc = ctask->sc;
113
114 hdr->opcode = ISCSI_OP_SCSI_CMD;
115 hdr->flags = ISCSI_ATTR_SIMPLE;
116 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
Al Virob4377352007-02-09 16:39:40 +0000117 hdr->itt = build_itt(ctask->itt, conn->id, session->age);
Mike Christie7996a772006-04-06 21:13:41 -0500118 hdr->data_length = cpu_to_be32(sc->request_bufflen);
119 hdr->cmdsn = cpu_to_be32(session->cmdsn);
120 session->cmdsn++;
121 hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
122 memcpy(hdr->cdb, sc->cmnd, sc->cmd_len);
123 memset(&hdr->cdb[sc->cmd_len], 0, MAX_COMMAND_SIZE - sc->cmd_len);
124
Mike Christieffd04362006-08-31 18:09:24 -0400125 ctask->data_count = 0;
Mike Christie7996a772006-04-06 21:13:41 -0500126 if (sc->sc_data_direction == DMA_TO_DEVICE) {
127 hdr->flags |= ISCSI_FLAG_CMD_WRITE;
128 /*
129 * Write counters:
130 *
131 * imm_count bytes to be sent right after
132 * SCSI PDU Header
133 *
134 * unsol_count bytes(as Data-Out) to be sent
135 * without R2T ack right after
136 * immediate data
137 *
138 * r2t_data_count bytes to be sent via R2T ack's
139 *
140 * pad_count bytes to be sent as zero-padding
141 */
142 ctask->imm_count = 0;
143 ctask->unsol_count = 0;
Mike Christieffd04362006-08-31 18:09:24 -0400144 ctask->unsol_offset = 0;
Mike Christie7996a772006-04-06 21:13:41 -0500145 ctask->unsol_datasn = 0;
146
147 if (session->imm_data_en) {
148 if (ctask->total_length >= session->first_burst)
149 ctask->imm_count = min(session->first_burst,
150 conn->max_xmit_dlength);
151 else
152 ctask->imm_count = min(ctask->total_length,
153 conn->max_xmit_dlength);
154 hton24(ctask->hdr->dlength, ctask->imm_count);
155 } else
156 zero_data(ctask->hdr->dlength);
157
Mike Christieffd04362006-08-31 18:09:24 -0400158 if (!session->initial_r2t_en) {
Mike Christie7996a772006-04-06 21:13:41 -0500159 ctask->unsol_count = min(session->first_burst,
160 ctask->total_length) - ctask->imm_count;
Mike Christieffd04362006-08-31 18:09:24 -0400161 ctask->unsol_offset = ctask->imm_count;
162 }
163
Mike Christie7996a772006-04-06 21:13:41 -0500164 if (!ctask->unsol_count)
165 /* No unsolicit Data-Out's */
166 ctask->hdr->flags |= ISCSI_FLAG_CMD_FINAL;
167 } else {
168 ctask->datasn = 0;
169 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
170 zero_data(hdr->dlength);
171
172 if (sc->sc_data_direction == DMA_FROM_DEVICE)
173 hdr->flags |= ISCSI_FLAG_CMD_READ;
174 }
175
176 conn->scsicmd_pdus_cnt++;
177}
178EXPORT_SYMBOL_GPL(iscsi_prep_scsi_cmd_pdu);
179
180/**
181 * iscsi_complete_command - return command back to scsi-ml
Mike Christie7996a772006-04-06 21:13:41 -0500182 * @ctask: iscsi cmd task
183 *
184 * Must be called with session lock.
185 * This function returns the scsi command to scsi-ml and returns
186 * the cmd task to the pool of available cmd tasks.
187 */
Mike Christie60ecebf2006-08-31 18:09:25 -0400188static void iscsi_complete_command(struct iscsi_cmd_task *ctask)
Mike Christie7996a772006-04-06 21:13:41 -0500189{
Mike Christie60ecebf2006-08-31 18:09:25 -0400190 struct iscsi_session *session = ctask->conn->session;
Mike Christie7996a772006-04-06 21:13:41 -0500191 struct scsi_cmnd *sc = ctask->sc;
192
Mike Christieb6c395e2006-07-24 15:47:15 -0500193 ctask->state = ISCSI_TASK_COMPLETED;
Mike Christie7996a772006-04-06 21:13:41 -0500194 ctask->sc = NULL;
Mike Christief47f2cf2006-08-31 18:09:33 -0400195 /* SCSI eh reuses commands to verify us */
196 sc->SCp.ptr = NULL;
Mike Christie7996a772006-04-06 21:13:41 -0500197 list_del_init(&ctask->running);
198 __kfifo_put(session->cmdpool.queue, (void*)&ctask, sizeof(void*));
199 sc->scsi_done(sc);
200}
201
Mike Christie60ecebf2006-08-31 18:09:25 -0400202static void __iscsi_get_ctask(struct iscsi_cmd_task *ctask)
203{
204 atomic_inc(&ctask->refcount);
205}
206
207static void iscsi_get_ctask(struct iscsi_cmd_task *ctask)
208{
209 spin_lock_bh(&ctask->conn->session->lock);
210 __iscsi_get_ctask(ctask);
211 spin_unlock_bh(&ctask->conn->session->lock);
212}
213
214static void __iscsi_put_ctask(struct iscsi_cmd_task *ctask)
215{
Mike Christiee648f632006-08-31 18:09:34 -0400216 if (atomic_dec_and_test(&ctask->refcount))
Mike Christie60ecebf2006-08-31 18:09:25 -0400217 iscsi_complete_command(ctask);
Mike Christie60ecebf2006-08-31 18:09:25 -0400218}
219
220static void iscsi_put_ctask(struct iscsi_cmd_task *ctask)
221{
222 spin_lock_bh(&ctask->conn->session->lock);
223 __iscsi_put_ctask(ctask);
224 spin_unlock_bh(&ctask->conn->session->lock);
225}
226
Mike Christie7996a772006-04-06 21:13:41 -0500227/**
228 * iscsi_cmd_rsp - SCSI Command Response processing
229 * @conn: iscsi connection
230 * @hdr: iscsi header
231 * @ctask: scsi command task
232 * @data: cmd data buffer
233 * @datalen: len of buffer
234 *
235 * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
236 * then completes the command and task.
237 **/
238static int iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
239 struct iscsi_cmd_task *ctask, char *data,
240 int datalen)
241{
242 int rc;
243 struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)hdr;
244 struct iscsi_session *session = conn->session;
245 struct scsi_cmnd *sc = ctask->sc;
246
247 rc = iscsi_check_assign_cmdsn(session, (struct iscsi_nopin*)rhdr);
248 if (rc) {
249 sc->result = DID_ERROR << 16;
250 goto out;
251 }
252
253 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
254
255 sc->result = (DID_OK << 16) | rhdr->cmd_status;
256
257 if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
258 sc->result = DID_ERROR << 16;
259 goto out;
260 }
261
262 if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
Mike Christie9b80cb42006-12-17 12:10:28 -0600263 uint16_t senselen;
Mike Christie7996a772006-04-06 21:13:41 -0500264
265 if (datalen < 2) {
266invalid_datalen:
Or Gerlitzbe2df722006-05-02 19:46:43 -0500267 printk(KERN_ERR "iscsi: Got CHECK_CONDITION but "
268 "invalid data buffer size of %d\n", datalen);
Mike Christie7996a772006-04-06 21:13:41 -0500269 sc->result = DID_BAD_TARGET << 16;
270 goto out;
271 }
272
Mike Christie8eb00532007-02-28 17:32:19 -0600273 senselen = be16_to_cpu(get_unaligned((__be16 *) data));
Mike Christie7996a772006-04-06 21:13:41 -0500274 if (datalen < senselen)
275 goto invalid_datalen;
276
277 memcpy(sc->sense_buffer, data + 2,
Mike Christie9b80cb42006-12-17 12:10:28 -0600278 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
Mike Christie7996a772006-04-06 21:13:41 -0500279 debug_scsi("copied %d bytes of sense\n",
Mike Christie8eb00532007-02-28 17:32:19 -0600280 min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
Mike Christie7996a772006-04-06 21:13:41 -0500281 }
282
283 if (sc->sc_data_direction == DMA_TO_DEVICE)
284 goto out;
285
286 if (rhdr->flags & ISCSI_FLAG_CMD_UNDERFLOW) {
287 int res_count = be32_to_cpu(rhdr->residual_count);
288
289 if (res_count > 0 && res_count <= sc->request_bufflen)
290 sc->resid = res_count;
291 else
292 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
293 } else if (rhdr->flags & ISCSI_FLAG_CMD_BIDI_UNDERFLOW)
294 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
295 else if (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW)
296 sc->resid = be32_to_cpu(rhdr->residual_count);
297
298out:
299 debug_scsi("done [sc %lx res %d itt 0x%x]\n",
300 (long)sc, sc->result, ctask->itt);
301 conn->scsirsp_pdus_cnt++;
302
Mike Christie60ecebf2006-08-31 18:09:25 -0400303 __iscsi_put_ctask(ctask);
Mike Christie7996a772006-04-06 21:13:41 -0500304 return rc;
305}
306
Mike Christie7ea8b822006-07-24 15:47:22 -0500307static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
308{
309 struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
310
311 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
312 conn->tmfrsp_pdus_cnt++;
313
314 if (conn->tmabort_state != TMABORT_INITIAL)
315 return;
316
317 if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
318 conn->tmabort_state = TMABORT_SUCCESS;
319 else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
320 conn->tmabort_state = TMABORT_NOT_FOUND;
321 else
322 conn->tmabort_state = TMABORT_FAILED;
323 wake_up(&conn->ehwait);
324}
325
Mike Christie62f38302006-08-31 18:09:27 -0400326static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
327 char *data, int datalen)
328{
329 struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
330 struct iscsi_hdr rejected_pdu;
331 uint32_t itt;
332
333 conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
334
335 if (reject->reason == ISCSI_REASON_DATA_DIGEST_ERROR) {
336 if (ntoh24(reject->dlength) > datalen)
337 return ISCSI_ERR_PROTO;
338
339 if (ntoh24(reject->dlength) >= sizeof(struct iscsi_hdr)) {
340 memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
Al Virob4377352007-02-09 16:39:40 +0000341 itt = get_itt(rejected_pdu.itt);
Mike Christie62f38302006-08-31 18:09:27 -0400342 printk(KERN_ERR "itt 0x%x had pdu (op 0x%x) rejected "
343 "due to DataDigest error.\n", itt,
344 rejected_pdu.opcode);
345 }
346 }
347 return 0;
348}
349
Mike Christie7996a772006-04-06 21:13:41 -0500350/**
351 * __iscsi_complete_pdu - complete pdu
352 * @conn: iscsi conn
353 * @hdr: iscsi header
354 * @data: data buffer
355 * @datalen: len of data buffer
356 *
357 * Completes pdu processing by freeing any resources allocated at
358 * queuecommand or send generic. session lock must be held and verify
359 * itt must have been called.
360 */
361int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
362 char *data, int datalen)
363{
364 struct iscsi_session *session = conn->session;
365 int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
366 struct iscsi_cmd_task *ctask;
367 struct iscsi_mgmt_task *mtask;
368 uint32_t itt;
369
Al Virob4377352007-02-09 16:39:40 +0000370 if (hdr->itt != RESERVED_ITT)
371 itt = get_itt(hdr->itt);
Mike Christie7996a772006-04-06 21:13:41 -0500372 else
Al Virob4377352007-02-09 16:39:40 +0000373 itt = ~0U;
Mike Christie7996a772006-04-06 21:13:41 -0500374
375 if (itt < session->cmds_max) {
376 ctask = session->cmds[itt];
377
378 debug_scsi("cmdrsp [op 0x%x cid %d itt 0x%x len %d]\n",
379 opcode, conn->id, ctask->itt, datalen);
380
381 switch(opcode) {
382 case ISCSI_OP_SCSI_CMD_RSP:
383 BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
384 rc = iscsi_scsi_cmd_rsp(conn, hdr, ctask, data,
385 datalen);
386 break;
387 case ISCSI_OP_SCSI_DATA_IN:
388 BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
389 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
390 conn->scsirsp_pdus_cnt++;
Mike Christie60ecebf2006-08-31 18:09:25 -0400391 __iscsi_put_ctask(ctask);
Mike Christie7996a772006-04-06 21:13:41 -0500392 }
393 break;
394 case ISCSI_OP_R2T:
395 /* LLD handles this for now */
396 break;
397 default:
398 rc = ISCSI_ERR_BAD_OPCODE;
399 break;
400 }
401 } else if (itt >= ISCSI_MGMT_ITT_OFFSET &&
402 itt < ISCSI_MGMT_ITT_OFFSET + session->mgmtpool_max) {
403 mtask = session->mgmt_cmds[itt - ISCSI_MGMT_ITT_OFFSET];
404
405 debug_scsi("immrsp [op 0x%x cid %d itt 0x%x len %d]\n",
406 opcode, conn->id, mtask->itt, datalen);
407
Mike Christie8d2860b2006-05-02 19:46:47 -0500408 rc = iscsi_check_assign_cmdsn(session,
409 (struct iscsi_nopin*)hdr);
410 if (rc)
411 goto done;
412
Mike Christie7996a772006-04-06 21:13:41 -0500413 switch(opcode) {
Mike Christie8d2860b2006-05-02 19:46:47 -0500414 case ISCSI_OP_LOGOUT_RSP:
Mike Christiec8dc1e52006-07-24 15:47:39 -0500415 if (datalen) {
416 rc = ISCSI_ERR_PROTO;
417 break;
418 }
Mike Christie8d2860b2006-05-02 19:46:47 -0500419 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
420 /* fall through */
Mike Christie7996a772006-04-06 21:13:41 -0500421 case ISCSI_OP_LOGIN_RSP:
422 case ISCSI_OP_TEXT_RSP:
Mike Christie8d2860b2006-05-02 19:46:47 -0500423 /*
424 * login related PDU's exp_statsn is handled in
425 * userspace
426 */
Mike Christie40527af2006-07-24 15:47:45 -0500427 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
428 rc = ISCSI_ERR_CONN_FAILED;
Mike Christie7996a772006-04-06 21:13:41 -0500429 list_del(&mtask->running);
430 if (conn->login_mtask != mtask)
431 __kfifo_put(session->mgmtpool.queue,
432 (void*)&mtask, sizeof(void*));
433 break;
434 case ISCSI_OP_SCSI_TMFUNC_RSP:
Mike Christie7996a772006-04-06 21:13:41 -0500435 if (datalen) {
436 rc = ISCSI_ERR_PROTO;
437 break;
438 }
Mike Christie8d2860b2006-05-02 19:46:47 -0500439
Mike Christie7ea8b822006-07-24 15:47:22 -0500440 iscsi_tmf_rsp(conn, hdr);
Mike Christie7996a772006-04-06 21:13:41 -0500441 break;
442 case ISCSI_OP_NOOP_IN:
Al Virob4377352007-02-09 16:39:40 +0000443 if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) || datalen) {
Mike Christie7996a772006-04-06 21:13:41 -0500444 rc = ISCSI_ERR_PROTO;
445 break;
446 }
Mike Christie7996a772006-04-06 21:13:41 -0500447 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
448
Mike Christie40527af2006-07-24 15:47:45 -0500449 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
450 rc = ISCSI_ERR_CONN_FAILED;
Mike Christie7996a772006-04-06 21:13:41 -0500451 list_del(&mtask->running);
452 if (conn->login_mtask != mtask)
453 __kfifo_put(session->mgmtpool.queue,
454 (void*)&mtask, sizeof(void*));
455 break;
456 default:
457 rc = ISCSI_ERR_BAD_OPCODE;
458 break;
459 }
Al Virob4377352007-02-09 16:39:40 +0000460 } else if (itt == ~0U) {
Mike Christie62f38302006-08-31 18:09:27 -0400461 rc = iscsi_check_assign_cmdsn(session,
462 (struct iscsi_nopin*)hdr);
463 if (rc)
464 goto done;
465
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
Mike Christie8d2860b2006-05-02 19:46:47 -0500494done:
Mike Christie7996a772006-04-06 21:13:41 -0500495 return rc;
496}
497EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
498
499int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
500 char *data, int datalen)
501{
502 int rc;
503
504 spin_lock(&conn->session->lock);
505 rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
506 spin_unlock(&conn->session->lock);
507 return rc;
508}
509EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
510
511/* verify itt (itt encoding: age+cid+itt) */
512int iscsi_verify_itt(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
513 uint32_t *ret_itt)
514{
515 struct iscsi_session *session = conn->session;
516 struct iscsi_cmd_task *ctask;
517 uint32_t itt;
518
Al Virob4377352007-02-09 16:39:40 +0000519 if (hdr->itt != RESERVED_ITT) {
520 if (((__force u32)hdr->itt & ISCSI_AGE_MASK) !=
Mike Christie7996a772006-04-06 21:13:41 -0500521 (session->age << ISCSI_AGE_SHIFT)) {
Or Gerlitzbe2df722006-05-02 19:46:43 -0500522 printk(KERN_ERR "iscsi: received itt %x expected "
Al Virob4377352007-02-09 16:39:40 +0000523 "session age (%x)\n", (__force u32)hdr->itt,
Mike Christie7996a772006-04-06 21:13:41 -0500524 session->age & ISCSI_AGE_MASK);
525 return ISCSI_ERR_BAD_ITT;
526 }
527
Al Virob4377352007-02-09 16:39:40 +0000528 if (((__force u32)hdr->itt & ISCSI_CID_MASK) !=
Mike Christie7996a772006-04-06 21:13:41 -0500529 (conn->id << ISCSI_CID_SHIFT)) {
Or Gerlitzbe2df722006-05-02 19:46:43 -0500530 printk(KERN_ERR "iscsi: received itt %x, expected "
Al Virob4377352007-02-09 16:39:40 +0000531 "CID (%x)\n", (__force u32)hdr->itt, conn->id);
Mike Christie7996a772006-04-06 21:13:41 -0500532 return ISCSI_ERR_BAD_ITT;
533 }
Al Virob4377352007-02-09 16:39:40 +0000534 itt = get_itt(hdr->itt);
Mike Christie7996a772006-04-06 21:13:41 -0500535 } else
Al Virob4377352007-02-09 16:39:40 +0000536 itt = ~0U;
Mike Christie7996a772006-04-06 21:13:41 -0500537
538 if (itt < session->cmds_max) {
539 ctask = session->cmds[itt];
540
541 if (!ctask->sc) {
Or Gerlitzbe2df722006-05-02 19:46:43 -0500542 printk(KERN_INFO "iscsi: dropping ctask with "
Mike Christie7996a772006-04-06 21:13:41 -0500543 "itt 0x%x\n", ctask->itt);
544 /* force drop */
545 return ISCSI_ERR_NO_SCSI_CMD;
546 }
547
548 if (ctask->sc->SCp.phase != session->age) {
Or Gerlitzbe2df722006-05-02 19:46:43 -0500549 printk(KERN_ERR "iscsi: ctask's session age %d, "
Mike Christie7996a772006-04-06 21:13:41 -0500550 "expected %d\n", ctask->sc->SCp.phase,
551 session->age);
552 return ISCSI_ERR_SESSION_FAILED;
553 }
554 }
555
556 *ret_itt = itt;
557 return 0;
558}
559EXPORT_SYMBOL_GPL(iscsi_verify_itt);
560
561void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
562{
563 struct iscsi_session *session = conn->session;
564 unsigned long flags;
565
566 spin_lock_irqsave(&session->lock, flags);
Mike Christie656cffc2006-05-18 20:31:42 -0500567 if (session->state == ISCSI_STATE_FAILED) {
568 spin_unlock_irqrestore(&session->lock, flags);
569 return;
570 }
571
Mike Christie67a61112006-05-30 00:37:20 -0500572 if (conn->stop_stage == 0)
Mike Christie7996a772006-04-06 21:13:41 -0500573 session->state = ISCSI_STATE_FAILED;
574 spin_unlock_irqrestore(&session->lock, flags);
575 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
576 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
577 iscsi_conn_error(conn->cls_conn, err);
578}
579EXPORT_SYMBOL_GPL(iscsi_conn_failure);
580
Mike Christie05db8882007-02-28 17:32:16 -0600581static int iscsi_xmit_mtask(struct iscsi_conn *conn)
Mike Christieb5072ea2006-10-16 18:09:42 -0400582{
583 struct iscsi_hdr *hdr = conn->mtask->hdr;
584 int rc, was_logout = 0;
585
586 if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT) {
587 conn->session->state = ISCSI_STATE_IN_RECOVERY;
588 iscsi_block_session(session_to_cls(conn->session));
589 was_logout = 1;
590 }
591 rc = conn->session->tt->xmit_mgmt_task(conn, conn->mtask);
592 if (rc)
593 return rc;
594
Mike Christie05db8882007-02-28 17:32:16 -0600595 /* done with this in-progress mtask */
596 conn->mtask = NULL;
597
Mike Christieb5072ea2006-10-16 18:09:42 -0400598 if (was_logout) {
599 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
600 return -ENODATA;
601 }
602 return 0;
603}
604
Mike Christie7996a772006-04-06 21:13:41 -0500605/**
606 * iscsi_data_xmit - xmit any command into the scheduled connection
607 * @conn: iscsi connection
608 *
609 * Notes:
610 * The function can return -EAGAIN in which case the caller must
611 * re-schedule it again later or recover. '0' return code means
612 * successful xmit.
613 **/
614static int iscsi_data_xmit(struct iscsi_conn *conn)
615{
616 struct iscsi_transport *tt;
Mike Christie3219e522006-05-30 00:37:28 -0500617 int rc = 0;
Mike Christie7996a772006-04-06 21:13:41 -0500618
619 if (unlikely(conn->suspend_tx)) {
620 debug_scsi("conn %d Tx suspended!\n", conn->id);
Mike Christie3219e522006-05-30 00:37:28 -0500621 return -ENODATA;
Mike Christie7996a772006-04-06 21:13:41 -0500622 }
623 tt = conn->session->tt;
624
625 /*
626 * Transmit in the following order:
627 *
628 * 1) un-finished xmit (ctask or mtask)
629 * 2) immediate control PDUs
630 * 3) write data
631 * 4) SCSI commands
632 * 5) non-immediate control PDUs
633 *
634 * No need to lock around __kfifo_get as long as
635 * there's one producer and one consumer.
636 */
637
638 BUG_ON(conn->ctask && conn->mtask);
639
640 if (conn->ctask) {
Mike Christie60ecebf2006-08-31 18:09:25 -0400641 iscsi_get_ctask(conn->ctask);
Mike Christie3219e522006-05-30 00:37:28 -0500642 rc = tt->xmit_cmd_task(conn, conn->ctask);
Mike Christie60ecebf2006-08-31 18:09:25 -0400643 iscsi_put_ctask(conn->ctask);
Mike Christie3219e522006-05-30 00:37:28 -0500644 if (rc)
Mike Christie7996a772006-04-06 21:13:41 -0500645 goto again;
646 /* done with this in-progress ctask */
647 conn->ctask = NULL;
648 }
649 if (conn->mtask) {
Mike Christie05db8882007-02-28 17:32:16 -0600650 rc = iscsi_xmit_mtask(conn);
Mike Christie3219e522006-05-30 00:37:28 -0500651 if (rc)
Mike Christie7996a772006-04-06 21:13:41 -0500652 goto again;
Mike Christie7996a772006-04-06 21:13:41 -0500653 }
654
655 /* process immediate first */
656 if (unlikely(__kfifo_len(conn->immqueue))) {
657 while (__kfifo_get(conn->immqueue, (void*)&conn->mtask,
658 sizeof(void*))) {
Mike Christie994442e2006-05-30 00:37:22 -0500659 spin_lock_bh(&conn->session->lock);
Mike Christie7996a772006-04-06 21:13:41 -0500660 list_add_tail(&conn->mtask->running,
661 &conn->mgmt_run_list);
Mike Christie994442e2006-05-30 00:37:22 -0500662 spin_unlock_bh(&conn->session->lock);
Mike Christie05db8882007-02-28 17:32:16 -0600663 rc = iscsi_xmit_mtask(conn);
Mike Christie3219e522006-05-30 00:37:28 -0500664 if (rc)
Mike Christie7996a772006-04-06 21:13:41 -0500665 goto again;
666 }
Mike Christie7996a772006-04-06 21:13:41 -0500667 }
668
669 /* process command queue */
Mike Christieb6c395e2006-07-24 15:47:15 -0500670 spin_lock_bh(&conn->session->lock);
671 while (!list_empty(&conn->xmitqueue)) {
Mike Christie7996a772006-04-06 21:13:41 -0500672 /*
673 * iscsi tcp may readd the task to the xmitqueue to send
674 * write data
675 */
Mike Christieb6c395e2006-07-24 15:47:15 -0500676 conn->ctask = list_entry(conn->xmitqueue.next,
677 struct iscsi_cmd_task, running);
678 conn->ctask->state = ISCSI_TASK_RUNNING;
679 list_move_tail(conn->xmitqueue.next, &conn->run_list);
Mike Christie60ecebf2006-08-31 18:09:25 -0400680 __iscsi_get_ctask(conn->ctask);
Mike Christie994442e2006-05-30 00:37:22 -0500681 spin_unlock_bh(&conn->session->lock);
Mike Christieb6c395e2006-07-24 15:47:15 -0500682
Mike Christie3219e522006-05-30 00:37:28 -0500683 rc = tt->xmit_cmd_task(conn, conn->ctask);
Mike Christie60ecebf2006-08-31 18:09:25 -0400684
Mike Christieb6c395e2006-07-24 15:47:15 -0500685 spin_lock_bh(&conn->session->lock);
Mike Christie60ecebf2006-08-31 18:09:25 -0400686 __iscsi_put_ctask(conn->ctask);
687 if (rc) {
688 spin_unlock_bh(&conn->session->lock);
689 goto again;
690 }
Mike Christie7996a772006-04-06 21:13:41 -0500691 }
Mike Christieb6c395e2006-07-24 15:47:15 -0500692 spin_unlock_bh(&conn->session->lock);
Mike Christie7996a772006-04-06 21:13:41 -0500693 /* done with this ctask */
694 conn->ctask = NULL;
695
696 /* process the rest control plane PDUs, if any */
697 if (unlikely(__kfifo_len(conn->mgmtqueue))) {
698 while (__kfifo_get(conn->mgmtqueue, (void*)&conn->mtask,
699 sizeof(void*))) {
Mike Christie994442e2006-05-30 00:37:22 -0500700 spin_lock_bh(&conn->session->lock);
Mike Christie7996a772006-04-06 21:13:41 -0500701 list_add_tail(&conn->mtask->running,
702 &conn->mgmt_run_list);
Mike Christie994442e2006-05-30 00:37:22 -0500703 spin_unlock_bh(&conn->session->lock);
Mike Christie05db8882007-02-28 17:32:16 -0600704 rc = iscsi_xmit_mtask(conn);
705 if (rc)
Mike Christie7996a772006-04-06 21:13:41 -0500706 goto again;
707 }
Mike Christie7996a772006-04-06 21:13:41 -0500708 }
709
Mike Christie3219e522006-05-30 00:37:28 -0500710 return -ENODATA;
Mike Christie7996a772006-04-06 21:13:41 -0500711
712again:
713 if (unlikely(conn->suspend_tx))
Mike Christie3219e522006-05-30 00:37:28 -0500714 return -ENODATA;
Mike Christie7996a772006-04-06 21:13:41 -0500715
Mike Christie3219e522006-05-30 00:37:28 -0500716 return rc;
Mike Christie7996a772006-04-06 21:13:41 -0500717}
718
David Howellsc4028952006-11-22 14:57:56 +0000719static void iscsi_xmitworker(struct work_struct *work)
Mike Christie7996a772006-04-06 21:13:41 -0500720{
David Howellsc4028952006-11-22 14:57:56 +0000721 struct iscsi_conn *conn =
722 container_of(work, struct iscsi_conn, xmitwork);
Mike Christie3219e522006-05-30 00:37:28 -0500723 int rc;
Mike Christie7996a772006-04-06 21:13:41 -0500724 /*
725 * serialize Xmit worker on a per-connection basis.
726 */
727 mutex_lock(&conn->xmitmutex);
Mike Christie3219e522006-05-30 00:37:28 -0500728 do {
729 rc = iscsi_data_xmit(conn);
730 } while (rc >= 0 || rc == -EAGAIN);
Mike Christie7996a772006-04-06 21:13:41 -0500731 mutex_unlock(&conn->xmitmutex);
732}
733
734enum {
735 FAILURE_BAD_HOST = 1,
736 FAILURE_SESSION_FAILED,
737 FAILURE_SESSION_FREED,
738 FAILURE_WINDOW_CLOSED,
Mike Christie60ecebf2006-08-31 18:09:25 -0400739 FAILURE_OOM,
Mike Christie7996a772006-04-06 21:13:41 -0500740 FAILURE_SESSION_TERMINATE,
Mike Christie656cffc2006-05-18 20:31:42 -0500741 FAILURE_SESSION_IN_RECOVERY,
Mike Christie7996a772006-04-06 21:13:41 -0500742 FAILURE_SESSION_RECOVERY_TIMEOUT,
743};
744
745int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
746{
747 struct Scsi_Host *host;
748 int reason = 0;
749 struct iscsi_session *session;
750 struct iscsi_conn *conn;
751 struct iscsi_cmd_task *ctask = NULL;
752
753 sc->scsi_done = done;
754 sc->result = 0;
Mike Christief47f2cf2006-08-31 18:09:33 -0400755 sc->SCp.ptr = NULL;
Mike Christie7996a772006-04-06 21:13:41 -0500756
757 host = sc->device->host;
758 session = iscsi_hostdata(host->hostdata);
759
760 spin_lock(&session->lock);
761
Mike Christie656cffc2006-05-18 20:31:42 -0500762 /*
763 * ISCSI_STATE_FAILED is a temp. state. The recovery
764 * code will decide what is best to do with command queued
765 * during this time
766 */
767 if (session->state != ISCSI_STATE_LOGGED_IN &&
768 session->state != ISCSI_STATE_FAILED) {
769 /*
770 * to handle the race between when we set the recovery state
771 * and block the session we requeue here (commands could
772 * be entering our queuecommand while a block is starting
773 * up because the block code is not locked)
774 */
775 if (session->state == ISCSI_STATE_IN_RECOVERY) {
776 reason = FAILURE_SESSION_IN_RECOVERY;
Mike Christie67a61112006-05-30 00:37:20 -0500777 goto reject;
Mike Christie7996a772006-04-06 21:13:41 -0500778 }
Mike Christie656cffc2006-05-18 20:31:42 -0500779
780 if (session->state == ISCSI_STATE_RECOVERY_FAILED)
781 reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
782 else if (session->state == ISCSI_STATE_TERMINATE)
783 reason = FAILURE_SESSION_TERMINATE;
784 else
785 reason = FAILURE_SESSION_FREED;
Mike Christie7996a772006-04-06 21:13:41 -0500786 goto fault;
787 }
788
789 /*
790 * Check for iSCSI window and take care of CmdSN wrap-around
791 */
792 if ((int)(session->max_cmdsn - session->cmdsn) < 0) {
793 reason = FAILURE_WINDOW_CLOSED;
794 goto reject;
795 }
796
797 conn = session->leadconn;
Mike Christie98644042006-10-16 18:09:39 -0400798 if (!conn) {
799 reason = FAILURE_SESSION_FREED;
800 goto fault;
801 }
Mike Christie7996a772006-04-06 21:13:41 -0500802
Mike Christie60ecebf2006-08-31 18:09:25 -0400803 if (!__kfifo_get(session->cmdpool.queue, (void*)&ctask,
804 sizeof(void*))) {
805 reason = FAILURE_OOM;
806 goto reject;
807 }
Mike Christie7996a772006-04-06 21:13:41 -0500808 sc->SCp.phase = session->age;
809 sc->SCp.ptr = (char *)ctask;
810
Mike Christie60ecebf2006-08-31 18:09:25 -0400811 atomic_set(&ctask->refcount, 1);
Mike Christieb6c395e2006-07-24 15:47:15 -0500812 ctask->state = ISCSI_TASK_PENDING;
Mike Christie7996a772006-04-06 21:13:41 -0500813 ctask->mtask = NULL;
814 ctask->conn = conn;
815 ctask->sc = sc;
816 INIT_LIST_HEAD(&ctask->running);
817 ctask->total_length = sc->request_bufflen;
818 iscsi_prep_scsi_cmd_pdu(ctask);
819
820 session->tt->init_cmd_task(ctask);
821
Mike Christieb6c395e2006-07-24 15:47:15 -0500822 list_add_tail(&ctask->running, &conn->xmitqueue);
Mike Christie7996a772006-04-06 21:13:41 -0500823 debug_scsi(
Mike Christief47f2cf2006-08-31 18:09:33 -0400824 "ctask enq [%s cid %d sc %p cdb 0x%x itt 0x%x len %d cmdsn %d "
825 "win %d]\n",
Mike Christie7996a772006-04-06 21:13:41 -0500826 sc->sc_data_direction == DMA_TO_DEVICE ? "write" : "read",
Mike Christief47f2cf2006-08-31 18:09:33 -0400827 conn->id, sc, sc->cmnd[0], ctask->itt, sc->request_bufflen,
Mike Christie7996a772006-04-06 21:13:41 -0500828 session->cmdsn, session->max_cmdsn - session->exp_cmdsn + 1);
829 spin_unlock(&session->lock);
830
831 scsi_queue_work(host, &conn->xmitwork);
832 return 0;
833
834reject:
835 spin_unlock(&session->lock);
836 debug_scsi("cmd 0x%x rejected (%d)\n", sc->cmnd[0], reason);
837 return SCSI_MLQUEUE_HOST_BUSY;
838
839fault:
840 spin_unlock(&session->lock);
Or Gerlitzbe2df722006-05-02 19:46:43 -0500841 printk(KERN_ERR "iscsi: cmd 0x%x is not queued (%d)\n",
Mike Christie7996a772006-04-06 21:13:41 -0500842 sc->cmnd[0], reason);
843 sc->result = (DID_NO_CONNECT << 16);
844 sc->resid = sc->request_bufflen;
845 sc->scsi_done(sc);
846 return 0;
847}
848EXPORT_SYMBOL_GPL(iscsi_queuecommand);
849
850int iscsi_change_queue_depth(struct scsi_device *sdev, int depth)
851{
852 if (depth > ISCSI_MAX_CMD_PER_LUN)
853 depth = ISCSI_MAX_CMD_PER_LUN;
854 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
855 return sdev->queue_depth;
856}
857EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
858
859static int
860iscsi_conn_send_generic(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
861 char *data, uint32_t data_size)
862{
863 struct iscsi_session *session = conn->session;
864 struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
865 struct iscsi_mgmt_task *mtask;
866
867 spin_lock_bh(&session->lock);
868 if (session->state == ISCSI_STATE_TERMINATE) {
869 spin_unlock_bh(&session->lock);
870 return -EPERM;
871 }
872 if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) ||
873 hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
874 /*
875 * Login and Text are sent serially, in
876 * request-followed-by-response sequence.
877 * Same mtask can be used. Same ITT must be used.
878 * Note that login_mtask is preallocated at conn_create().
879 */
880 mtask = conn->login_mtask;
881 else {
Mike Christie656cffc2006-05-18 20:31:42 -0500882 BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
883 BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
Mike Christie7996a772006-04-06 21:13:41 -0500884
Mike Christie8d2860b2006-05-02 19:46:47 -0500885 nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
Mike Christie7996a772006-04-06 21:13:41 -0500886 if (!__kfifo_get(session->mgmtpool.queue,
887 (void*)&mtask, sizeof(void*))) {
888 spin_unlock_bh(&session->lock);
889 return -ENOSPC;
890 }
891 }
892
893 /*
Mike Christie8d2860b2006-05-02 19:46:47 -0500894 * pre-format CmdSN for outgoing PDU.
Mike Christie7996a772006-04-06 21:13:41 -0500895 */
Al Virob4377352007-02-09 16:39:40 +0000896 if (hdr->itt != RESERVED_ITT) {
897 hdr->itt = build_itt(mtask->itt, conn->id, session->age);
Mike Christie7996a772006-04-06 21:13:41 -0500898 nop->cmdsn = cpu_to_be32(session->cmdsn);
899 if (conn->c_stage == ISCSI_CONN_STARTED &&
900 !(hdr->opcode & ISCSI_OP_IMMEDIATE))
901 session->cmdsn++;
902 } else
903 /* do not advance CmdSN */
904 nop->cmdsn = cpu_to_be32(session->cmdsn);
905
Mike Christie7996a772006-04-06 21:13:41 -0500906 if (data_size) {
907 memcpy(mtask->data, data, data_size);
908 mtask->data_count = data_size;
909 } else
910 mtask->data_count = 0;
911
912 INIT_LIST_HEAD(&mtask->running);
913 memcpy(mtask->hdr, hdr, sizeof(struct iscsi_hdr));
914 if (session->tt->init_mgmt_task)
915 session->tt->init_mgmt_task(conn, mtask, data, data_size);
916 spin_unlock_bh(&session->lock);
917
918 debug_scsi("mgmtpdu [op 0x%x hdr->itt 0x%x datalen %d]\n",
919 hdr->opcode, hdr->itt, data_size);
920
921 /*
922 * since send_pdu() could be called at least from two contexts,
923 * we need to serialize __kfifo_put, so we don't have to take
924 * additional lock on fast data-path
925 */
926 if (hdr->opcode & ISCSI_OP_IMMEDIATE)
927 __kfifo_put(conn->immqueue, (void*)&mtask, sizeof(void*));
928 else
929 __kfifo_put(conn->mgmtqueue, (void*)&mtask, sizeof(void*));
930
931 scsi_queue_work(session->host, &conn->xmitwork);
932 return 0;
933}
934
935int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
936 char *data, uint32_t data_size)
937{
938 struct iscsi_conn *conn = cls_conn->dd_data;
939 int rc;
940
941 mutex_lock(&conn->xmitmutex);
942 rc = iscsi_conn_send_generic(conn, hdr, data, data_size);
943 mutex_unlock(&conn->xmitmutex);
944
945 return rc;
946}
947EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
948
949void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
950{
951 struct iscsi_session *session = class_to_transport_session(cls_session);
952 struct iscsi_conn *conn = session->leadconn;
953
954 spin_lock_bh(&session->lock);
955 if (session->state != ISCSI_STATE_LOGGED_IN) {
Mike Christie656cffc2006-05-18 20:31:42 -0500956 session->state = ISCSI_STATE_RECOVERY_FAILED;
Mike Christie7996a772006-04-06 21:13:41 -0500957 if (conn)
958 wake_up(&conn->ehwait);
959 }
960 spin_unlock_bh(&session->lock);
961}
962EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
963
964int iscsi_eh_host_reset(struct scsi_cmnd *sc)
965{
966 struct Scsi_Host *host = sc->device->host;
967 struct iscsi_session *session = iscsi_hostdata(host->hostdata);
968 struct iscsi_conn *conn = session->leadconn;
969 int fail_session = 0;
970
971 spin_lock_bh(&session->lock);
972 if (session->state == ISCSI_STATE_TERMINATE) {
973failed:
974 debug_scsi("failing host reset: session terminated "
Pete Wyckoffd6e24d12006-11-08 15:58:32 -0600975 "[CID %d age %d]\n", conn->id, session->age);
Mike Christie7996a772006-04-06 21:13:41 -0500976 spin_unlock_bh(&session->lock);
977 return FAILED;
978 }
979
980 if (sc->SCp.phase == session->age) {
Pete Wyckoffd6e24d12006-11-08 15:58:32 -0600981 debug_scsi("failing connection CID %d due to SCSI host reset\n",
Mike Christie7996a772006-04-06 21:13:41 -0500982 conn->id);
983 fail_session = 1;
984 }
985 spin_unlock_bh(&session->lock);
986
987 /*
988 * we drop the lock here but the leadconn cannot be destoyed while
989 * we are in the scsi eh
990 */
Mike Christie656cffc2006-05-18 20:31:42 -0500991 if (fail_session)
Mike Christie7996a772006-04-06 21:13:41 -0500992 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie7996a772006-04-06 21:13:41 -0500993
994 debug_scsi("iscsi_eh_host_reset wait for relogin\n");
995 wait_event_interruptible(conn->ehwait,
996 session->state == ISCSI_STATE_TERMINATE ||
997 session->state == ISCSI_STATE_LOGGED_IN ||
Mike Christie656cffc2006-05-18 20:31:42 -0500998 session->state == ISCSI_STATE_RECOVERY_FAILED);
Mike Christie7996a772006-04-06 21:13:41 -0500999 if (signal_pending(current))
1000 flush_signals(current);
1001
1002 spin_lock_bh(&session->lock);
1003 if (session->state == ISCSI_STATE_LOGGED_IN)
Or Gerlitzbe2df722006-05-02 19:46:43 -05001004 printk(KERN_INFO "iscsi: host reset succeeded\n");
Mike Christie7996a772006-04-06 21:13:41 -05001005 else
1006 goto failed;
1007 spin_unlock_bh(&session->lock);
1008
1009 return SUCCESS;
1010}
1011EXPORT_SYMBOL_GPL(iscsi_eh_host_reset);
1012
1013static void iscsi_tmabort_timedout(unsigned long data)
1014{
1015 struct iscsi_cmd_task *ctask = (struct iscsi_cmd_task *)data;
1016 struct iscsi_conn *conn = ctask->conn;
1017 struct iscsi_session *session = conn->session;
1018
1019 spin_lock(&session->lock);
1020 if (conn->tmabort_state == TMABORT_INITIAL) {
1021 conn->tmabort_state = TMABORT_TIMEDOUT;
1022 debug_scsi("tmabort timedout [sc %p itt 0x%x]\n",
1023 ctask->sc, ctask->itt);
1024 /* unblock eh_abort() */
1025 wake_up(&conn->ehwait);
1026 }
1027 spin_unlock(&session->lock);
1028}
1029
1030/* must be called with the mutex lock */
1031static 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;
1037 int rc;
1038
1039 /*
1040 * ctask timed out but session is OK requests must be serialized.
1041 */
1042 memset(hdr, 0, sizeof(struct iscsi_tm));
1043 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
1044 hdr->flags = ISCSI_TM_FUNC_ABORT_TASK;
1045 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
1046 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
1047 hdr->rtt = ctask->hdr->itt;
1048 hdr->refcmdsn = ctask->hdr->cmdsn;
1049
1050 rc = iscsi_conn_send_generic(conn, (struct iscsi_hdr *)hdr,
1051 NULL, 0);
1052 if (rc) {
1053 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Pete Wyckoffd6e24d12006-11-08 15:58:32 -06001054 debug_scsi("abort sent failure [itt 0x%x] %d\n", ctask->itt,
1055 rc);
Mike Christie7996a772006-04-06 21:13:41 -05001056 return rc;
1057 }
1058
1059 debug_scsi("abort sent [itt 0x%x]\n", ctask->itt);
1060
1061 spin_lock_bh(&session->lock);
1062 ctask->mtask = (struct iscsi_mgmt_task *)
Al Virob4377352007-02-09 16:39:40 +00001063 session->mgmt_cmds[get_itt(hdr->itt) -
Mike Christie7996a772006-04-06 21:13:41 -05001064 ISCSI_MGMT_ITT_OFFSET];
1065
1066 if (conn->tmabort_state == TMABORT_INITIAL) {
1067 conn->tmfcmd_pdus_cnt++;
1068 conn->tmabort_timer.expires = 10*HZ + jiffies;
1069 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);
1075 mutex_unlock(&conn->xmitmutex);
1076
1077 /*
1078 * block eh thread until:
1079 *
1080 * 1) abort response
1081 * 2) abort timeout
1082 * 3) session is terminated or restarted or userspace has
1083 * given up on recovery
1084 */
1085 wait_event_interruptible(conn->ehwait,
1086 sc->SCp.phase != session->age ||
1087 session->state != ISCSI_STATE_LOGGED_IN ||
Mike Christie656cffc2006-05-18 20:31:42 -05001088 conn->tmabort_state != TMABORT_INITIAL);
Mike Christie7996a772006-04-06 21:13:41 -05001089 if (signal_pending(current))
1090 flush_signals(current);
1091 del_timer_sync(&conn->tmabort_timer);
1092
1093 mutex_lock(&conn->xmitmutex);
1094 return 0;
1095}
1096
1097/*
1098 * xmit mutex and session lock must be held
1099 */
Mike Christieb6c395e2006-07-24 15:47:15 -05001100static struct iscsi_mgmt_task *
1101iscsi_remove_mgmt_task(struct kfifo *fifo, uint32_t itt)
1102{
1103 int i, nr_tasks = __kfifo_len(fifo) / sizeof(void*);
1104 struct iscsi_mgmt_task *task;
Mike Christie7996a772006-04-06 21:13:41 -05001105
Mike Christieb6c395e2006-07-24 15:47:15 -05001106 debug_scsi("searching %d tasks\n", nr_tasks);
1107
1108 for (i = 0; i < nr_tasks; i++) {
1109 __kfifo_get(fifo, (void*)&task, sizeof(void*));
1110 debug_scsi("check task %u\n", task->itt);
1111
1112 if (task->itt == itt) {
1113 debug_scsi("matched task\n");
1114 return task;
1115 }
1116
1117 __kfifo_put(fifo, (void*)&task, sizeof(void*));
1118 }
1119 return NULL;
1120}
Mike Christie7996a772006-04-06 21:13:41 -05001121
1122static int iscsi_ctask_mtask_cleanup(struct iscsi_cmd_task *ctask)
1123{
1124 struct iscsi_conn *conn = ctask->conn;
1125 struct iscsi_session *session = conn->session;
1126
1127 if (!ctask->mtask)
1128 return -EINVAL;
1129
1130 if (!iscsi_remove_mgmt_task(conn->immqueue, ctask->mtask->itt))
1131 list_del(&ctask->mtask->running);
1132 __kfifo_put(session->mgmtpool.queue, (void*)&ctask->mtask,
1133 sizeof(void*));
1134 ctask->mtask = NULL;
1135 return 0;
1136}
1137
1138/*
1139 * session lock and xmitmutex must be held
1140 */
1141static void fail_command(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
1142 int err)
1143{
1144 struct scsi_cmnd *sc;
1145
Mike Christie7996a772006-04-06 21:13:41 -05001146 sc = ctask->sc;
1147 if (!sc)
1148 return;
Mike Christiee648f632006-08-31 18:09:34 -04001149
1150 conn->session->tt->cleanup_cmd_task(conn, ctask);
Mike Christie7ea8b822006-07-24 15:47:22 -05001151 iscsi_ctask_mtask_cleanup(ctask);
1152
Mike Christie7996a772006-04-06 21:13:41 -05001153 sc->result = err;
1154 sc->resid = sc->request_bufflen;
Mike Christiee648f632006-08-31 18:09:34 -04001155 /* release ref from queuecommand */
Mike Christie60ecebf2006-08-31 18:09:25 -04001156 __iscsi_put_ctask(ctask);
Mike Christie7996a772006-04-06 21:13:41 -05001157}
1158
1159int iscsi_eh_abort(struct scsi_cmnd *sc)
1160{
Mike Christief47f2cf2006-08-31 18:09:33 -04001161 struct iscsi_cmd_task *ctask;
1162 struct iscsi_conn *conn;
1163 struct iscsi_session *session;
Mike Christie7996a772006-04-06 21:13:41 -05001164 int rc;
1165
Mike Christief47f2cf2006-08-31 18:09:33 -04001166 /*
1167 * if session was ISCSI_STATE_IN_RECOVERY then we may not have
1168 * got the command.
1169 */
1170 if (!sc->SCp.ptr) {
1171 debug_scsi("sc never reached iscsi layer or it completed.\n");
1172 return SUCCESS;
1173 }
1174
1175 ctask = (struct iscsi_cmd_task *)sc->SCp.ptr;
1176 conn = ctask->conn;
1177 session = conn->session;
1178
Mike Christie7996a772006-04-06 21:13:41 -05001179 conn->eh_abort_cnt++;
1180 debug_scsi("aborting [sc %p itt 0x%x]\n", sc, ctask->itt);
1181
1182 mutex_lock(&conn->xmitmutex);
1183 spin_lock_bh(&session->lock);
1184
1185 /*
1186 * If we are not logged in or we have started a new session
1187 * then let the host reset code handle this
1188 */
1189 if (session->state != ISCSI_STATE_LOGGED_IN ||
1190 sc->SCp.phase != session->age)
1191 goto failed;
1192
1193 /* ctask completed before time out */
Mike Christie7ea8b822006-07-24 15:47:22 -05001194 if (!ctask->sc) {
1195 spin_unlock_bh(&session->lock);
1196 debug_scsi("sc completed while abort in progress\n");
1197 goto success_rel_mutex;
1198 }
Mike Christie7996a772006-04-06 21:13:41 -05001199
1200 /* what should we do here ? */
1201 if (conn->ctask == ctask) {
Or Gerlitzbe2df722006-05-02 19:46:43 -05001202 printk(KERN_INFO "iscsi: sc %p itt 0x%x partially sent. "
1203 "Failing abort\n", sc, ctask->itt);
Mike Christie7996a772006-04-06 21:13:41 -05001204 goto failed;
1205 }
1206
Mike Christieb6c395e2006-07-24 15:47:15 -05001207 if (ctask->state == ISCSI_TASK_PENDING)
Mike Christie7ea8b822006-07-24 15:47:22 -05001208 goto success_cleanup;
Mike Christie7996a772006-04-06 21:13:41 -05001209
1210 conn->tmabort_state = TMABORT_INITIAL;
1211
1212 spin_unlock_bh(&session->lock);
1213 rc = iscsi_exec_abort_task(sc, ctask);
1214 spin_lock_bh(&session->lock);
1215
Mike Christie7996a772006-04-06 21:13:41 -05001216 if (rc || sc->SCp.phase != session->age ||
1217 session->state != ISCSI_STATE_LOGGED_IN)
1218 goto failed;
Mike Christie7ea8b822006-07-24 15:47:22 -05001219 iscsi_ctask_mtask_cleanup(ctask);
Mike Christie7996a772006-04-06 21:13:41 -05001220
Mike Christie7ea8b822006-07-24 15:47:22 -05001221 switch (conn->tmabort_state) {
1222 case TMABORT_SUCCESS:
1223 goto success_cleanup;
1224 case TMABORT_NOT_FOUND:
1225 if (!ctask->sc) {
1226 /* ctask completed before tmf abort response */
1227 spin_unlock_bh(&session->lock);
1228 debug_scsi("sc completed while abort in progress\n");
1229 goto success_rel_mutex;
1230 }
1231 /* fall through */
1232 default:
1233 /* timedout or failed */
Mike Christie7996a772006-04-06 21:13:41 -05001234 spin_unlock_bh(&session->lock);
1235 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1236 spin_lock_bh(&session->lock);
1237 goto failed;
1238 }
1239
Mike Christie7ea8b822006-07-24 15:47:22 -05001240success_cleanup:
Mike Christie7996a772006-04-06 21:13:41 -05001241 debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
1242 spin_unlock_bh(&session->lock);
1243
1244 /*
1245 * clean up task if aborted. we have the xmitmutex so grab
1246 * 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);
1253
Mike Christie7ea8b822006-07-24 15:47:22 -05001254success_rel_mutex:
Mike Christie7996a772006-04-06 21:13:41 -05001255 mutex_unlock(&conn->xmitmutex);
1256 return SUCCESS;
1257
1258failed:
1259 spin_unlock_bh(&session->lock);
1260 mutex_unlock(&conn->xmitmutex);
1261
1262 debug_scsi("abort failed [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
1263 return FAILED;
1264}
1265EXPORT_SYMBOL_GPL(iscsi_eh_abort);
1266
1267int
1268iscsi_pool_init(struct iscsi_queue *q, int max, void ***items, int item_size)
1269{
1270 int i;
1271
1272 *items = kmalloc(max * sizeof(void*), GFP_KERNEL);
1273 if (*items == NULL)
1274 return -ENOMEM;
1275
1276 q->max = max;
1277 q->pool = kmalloc(max * sizeof(void*), GFP_KERNEL);
1278 if (q->pool == NULL) {
1279 kfree(*items);
1280 return -ENOMEM;
1281 }
1282
1283 q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
1284 GFP_KERNEL, NULL);
1285 if (q->queue == ERR_PTR(-ENOMEM)) {
1286 kfree(q->pool);
1287 kfree(*items);
1288 return -ENOMEM;
1289 }
1290
1291 for (i = 0; i < max; i++) {
1292 q->pool[i] = kmalloc(item_size, GFP_KERNEL);
1293 if (q->pool[i] == NULL) {
1294 int j;
1295
1296 for (j = 0; j < i; j++)
1297 kfree(q->pool[j]);
1298
1299 kfifo_free(q->queue);
1300 kfree(q->pool);
1301 kfree(*items);
1302 return -ENOMEM;
1303 }
1304 memset(q->pool[i], 0, item_size);
1305 (*items)[i] = q->pool[i];
1306 __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
1307 }
1308 return 0;
1309}
1310EXPORT_SYMBOL_GPL(iscsi_pool_init);
1311
1312void iscsi_pool_free(struct iscsi_queue *q, void **items)
1313{
1314 int i;
1315
1316 for (i = 0; i < q->max; i++)
1317 kfree(items[i]);
1318 kfree(q->pool);
1319 kfree(items);
1320}
1321EXPORT_SYMBOL_GPL(iscsi_pool_free);
1322
1323/*
1324 * iSCSI Session's hostdata organization:
1325 *
1326 * *------------------* <== hostdata_session(host->hostdata)
1327 * | ptr to class sess|
1328 * |------------------| <== iscsi_hostdata(host->hostdata)
1329 * | iscsi_session |
1330 * *------------------*
1331 */
1332
1333#define hostdata_privsize(_sz) (sizeof(unsigned long) + _sz + \
1334 _sz % sizeof(unsigned long))
1335
1336#define hostdata_session(_hostdata) (iscsi_ptr(*(unsigned long *)_hostdata))
1337
1338/**
1339 * iscsi_session_setup - create iscsi cls session and host and session
1340 * @scsit: scsi transport template
1341 * @iscsit: iscsi transport template
1342 * @initial_cmdsn: initial CmdSN
1343 * @hostno: host no allocated
1344 *
1345 * This can be used by software iscsi_transports that allocate
1346 * a session per scsi host.
1347 **/
1348struct iscsi_cls_session *
1349iscsi_session_setup(struct iscsi_transport *iscsit,
1350 struct scsi_transport_template *scsit,
1351 int cmd_task_size, int mgmt_task_size,
1352 uint32_t initial_cmdsn, uint32_t *hostno)
1353{
1354 struct Scsi_Host *shost;
1355 struct iscsi_session *session;
1356 struct iscsi_cls_session *cls_session;
1357 int cmd_i;
1358
1359 shost = scsi_host_alloc(iscsit->host_template,
1360 hostdata_privsize(sizeof(*session)));
1361 if (!shost)
1362 return NULL;
1363
1364 shost->max_id = 1;
1365 shost->max_channel = 0;
1366 shost->max_lun = iscsit->max_lun;
1367 shost->max_cmd_len = iscsit->max_cmd_len;
1368 shost->transportt = scsit;
1369 shost->transportt->create_work_queue = 1;
1370 *hostno = shost->host_no;
1371
1372 session = iscsi_hostdata(shost->hostdata);
1373 memset(session, 0, sizeof(struct iscsi_session));
1374 session->host = shost;
1375 session->state = ISCSI_STATE_FREE;
1376 session->mgmtpool_max = ISCSI_MGMT_CMDS_MAX;
1377 session->cmds_max = ISCSI_XMIT_CMDS_MAX;
1378 session->cmdsn = initial_cmdsn;
1379 session->exp_cmdsn = initial_cmdsn + 1;
1380 session->max_cmdsn = initial_cmdsn + 1;
1381 session->max_r2t = 1;
1382 session->tt = iscsit;
1383
1384 /* initialize SCSI PDU commands pool */
1385 if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
1386 (void***)&session->cmds,
1387 cmd_task_size + sizeof(struct iscsi_cmd_task)))
1388 goto cmdpool_alloc_fail;
1389
1390 /* pre-format cmds pool with ITT */
1391 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
1392 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
1393
1394 if (cmd_task_size)
1395 ctask->dd_data = &ctask[1];
1396 ctask->itt = cmd_i;
Mike Christieb6c395e2006-07-24 15:47:15 -05001397 INIT_LIST_HEAD(&ctask->running);
Mike Christie7996a772006-04-06 21:13:41 -05001398 }
1399
1400 spin_lock_init(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001401
1402 /* initialize immediate command pool */
1403 if (iscsi_pool_init(&session->mgmtpool, session->mgmtpool_max,
1404 (void***)&session->mgmt_cmds,
1405 mgmt_task_size + sizeof(struct iscsi_mgmt_task)))
1406 goto mgmtpool_alloc_fail;
1407
1408
1409 /* pre-format immediate cmds pool with ITT */
1410 for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
1411 struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
1412
1413 if (mgmt_task_size)
1414 mtask->dd_data = &mtask[1];
1415 mtask->itt = ISCSI_MGMT_ITT_OFFSET + cmd_i;
Mike Christieb6c395e2006-07-24 15:47:15 -05001416 INIT_LIST_HEAD(&mtask->running);
Mike Christie7996a772006-04-06 21:13:41 -05001417 }
1418
1419 if (scsi_add_host(shost, NULL))
1420 goto add_host_fail;
1421
Mike Christief53a88d2006-06-28 12:00:27 -05001422 if (!try_module_get(iscsit->owner))
1423 goto cls_session_fail;
1424
Mike Christie6a8a0d32006-06-28 12:00:31 -05001425 cls_session = iscsi_create_session(shost, iscsit, 0);
Mike Christie7996a772006-04-06 21:13:41 -05001426 if (!cls_session)
Mike Christief53a88d2006-06-28 12:00:27 -05001427 goto module_put;
Mike Christie7996a772006-04-06 21:13:41 -05001428 *(unsigned long*)shost->hostdata = (unsigned long)cls_session;
1429
1430 return cls_session;
1431
Mike Christief53a88d2006-06-28 12:00:27 -05001432module_put:
1433 module_put(iscsit->owner);
Mike Christie7996a772006-04-06 21:13:41 -05001434cls_session_fail:
1435 scsi_remove_host(shost);
1436add_host_fail:
Mike Christie7996a772006-04-06 21:13:41 -05001437 iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
1438mgmtpool_alloc_fail:
1439 iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
1440cmdpool_alloc_fail:
1441 scsi_host_put(shost);
1442 return NULL;
1443}
1444EXPORT_SYMBOL_GPL(iscsi_session_setup);
1445
1446/**
1447 * iscsi_session_teardown - destroy session, host, and cls_session
1448 * shost: scsi host
1449 *
1450 * This can be used by software iscsi_transports that allocate
1451 * a session per scsi host.
1452 **/
1453void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
1454{
1455 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
1456 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
Mike Christie63f75cc2006-07-24 15:47:29 -05001457 struct module *owner = cls_session->transport->owner;
Mike Christie7996a772006-04-06 21:13:41 -05001458
1459 scsi_remove_host(shost);
1460
Mike Christie7996a772006-04-06 21:13:41 -05001461 iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
1462 iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
1463
Mike Christief3ff0c32006-07-24 15:47:50 -05001464 kfree(session->targetname);
Mike Christie0801c242007-05-30 12:57:12 -05001465 kfree(session->hwaddress);
Mike Christie8ad57812007-05-30 12:57:13 -05001466 kfree(session->initiatorname);
Mike Christief3ff0c32006-07-24 15:47:50 -05001467
Mike Christie7996a772006-04-06 21:13:41 -05001468 iscsi_destroy_session(cls_session);
1469 scsi_host_put(shost);
Mike Christie63f75cc2006-07-24 15:47:29 -05001470 module_put(owner);
Mike Christie7996a772006-04-06 21:13:41 -05001471}
1472EXPORT_SYMBOL_GPL(iscsi_session_teardown);
1473
1474/**
1475 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
1476 * @cls_session: iscsi_cls_session
1477 * @conn_idx: cid
1478 **/
1479struct iscsi_cls_conn *
1480iscsi_conn_setup(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
1481{
1482 struct iscsi_session *session = class_to_transport_session(cls_session);
1483 struct iscsi_conn *conn;
1484 struct iscsi_cls_conn *cls_conn;
Mike Christied36ab6f2006-05-18 20:31:34 -05001485 char *data;
Mike Christie7996a772006-04-06 21:13:41 -05001486
1487 cls_conn = iscsi_create_conn(cls_session, conn_idx);
1488 if (!cls_conn)
1489 return NULL;
1490 conn = cls_conn->dd_data;
1491 memset(conn, 0, sizeof(*conn));
1492
1493 conn->session = session;
1494 conn->cls_conn = cls_conn;
1495 conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
1496 conn->id = conn_idx;
1497 conn->exp_statsn = 0;
1498 conn->tmabort_state = TMABORT_INITIAL;
1499 INIT_LIST_HEAD(&conn->run_list);
1500 INIT_LIST_HEAD(&conn->mgmt_run_list);
Mike Christieb6c395e2006-07-24 15:47:15 -05001501 INIT_LIST_HEAD(&conn->xmitqueue);
Mike Christie7996a772006-04-06 21:13:41 -05001502
1503 /* initialize general immediate & non-immediate PDU commands queue */
1504 conn->immqueue = kfifo_alloc(session->mgmtpool_max * sizeof(void*),
1505 GFP_KERNEL, NULL);
1506 if (conn->immqueue == ERR_PTR(-ENOMEM))
1507 goto immqueue_alloc_fail;
1508
1509 conn->mgmtqueue = kfifo_alloc(session->mgmtpool_max * sizeof(void*),
1510 GFP_KERNEL, NULL);
1511 if (conn->mgmtqueue == ERR_PTR(-ENOMEM))
1512 goto mgmtqueue_alloc_fail;
1513
David Howellsc4028952006-11-22 14:57:56 +00001514 INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
Mike Christie7996a772006-04-06 21:13:41 -05001515
1516 /* allocate login_mtask used for the login/text sequences */
1517 spin_lock_bh(&session->lock);
1518 if (!__kfifo_get(session->mgmtpool.queue,
1519 (void*)&conn->login_mtask,
1520 sizeof(void*))) {
1521 spin_unlock_bh(&session->lock);
1522 goto login_mtask_alloc_fail;
1523 }
1524 spin_unlock_bh(&session->lock);
1525
Mike Christiebf32ed32007-02-28 17:32:17 -06001526 data = kmalloc(ISCSI_DEF_MAX_RECV_SEG_LEN, GFP_KERNEL);
Mike Christied36ab6f2006-05-18 20:31:34 -05001527 if (!data)
1528 goto login_mtask_data_alloc_fail;
Mike Christiec8dc1e52006-07-24 15:47:39 -05001529 conn->login_mtask->data = conn->data = data;
Mike Christied36ab6f2006-05-18 20:31:34 -05001530
Mike Christie7996a772006-04-06 21:13:41 -05001531 init_timer(&conn->tmabort_timer);
1532 mutex_init(&conn->xmitmutex);
1533 init_waitqueue_head(&conn->ehwait);
1534
1535 return cls_conn;
1536
Mike Christied36ab6f2006-05-18 20:31:34 -05001537login_mtask_data_alloc_fail:
1538 __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
1539 sizeof(void*));
Mike Christie7996a772006-04-06 21:13:41 -05001540login_mtask_alloc_fail:
1541 kfifo_free(conn->mgmtqueue);
1542mgmtqueue_alloc_fail:
1543 kfifo_free(conn->immqueue);
1544immqueue_alloc_fail:
Mike Christie7996a772006-04-06 21:13:41 -05001545 iscsi_destroy_conn(cls_conn);
1546 return NULL;
1547}
1548EXPORT_SYMBOL_GPL(iscsi_conn_setup);
1549
1550/**
1551 * iscsi_conn_teardown - teardown iscsi connection
1552 * cls_conn: iscsi class connection
1553 *
1554 * TODO: we may need to make this into a two step process
1555 * like scsi-mls remove + put host
1556 */
1557void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
1558{
1559 struct iscsi_conn *conn = cls_conn->dd_data;
1560 struct iscsi_session *session = conn->session;
1561 unsigned long flags;
1562
Mike Christie7996a772006-04-06 21:13:41 -05001563 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
Mike Christie67a61112006-05-30 00:37:20 -05001564 mutex_lock(&conn->xmitmutex);
Mike Christie7996a772006-04-06 21:13:41 -05001565
1566 spin_lock_bh(&session->lock);
1567 conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
1568 if (session->leadconn == conn) {
1569 /*
1570 * leading connection? then give up on recovery.
1571 */
1572 session->state = ISCSI_STATE_TERMINATE;
1573 wake_up(&conn->ehwait);
1574 }
1575 spin_unlock_bh(&session->lock);
1576
1577 mutex_unlock(&conn->xmitmutex);
1578
1579 /*
1580 * Block until all in-progress commands for this connection
1581 * time out or fail.
1582 */
1583 for (;;) {
1584 spin_lock_irqsave(session->host->host_lock, flags);
1585 if (!session->host->host_busy) { /* OK for ERL == 0 */
1586 spin_unlock_irqrestore(session->host->host_lock, flags);
1587 break;
1588 }
1589 spin_unlock_irqrestore(session->host->host_lock, flags);
1590 msleep_interruptible(500);
Or Gerlitzbe2df722006-05-02 19:46:43 -05001591 printk(KERN_INFO "iscsi: scsi conn_destroy(): host_busy %d "
1592 "host_failed %d\n", session->host->host_busy,
1593 session->host->host_failed);
Mike Christie7996a772006-04-06 21:13:41 -05001594 /*
1595 * force eh_abort() to unblock
1596 */
1597 wake_up(&conn->ehwait);
1598 }
1599
Mike Christie779ea122007-02-28 17:32:15 -06001600 /* flush queued up work because we free the connection below */
1601 scsi_flush_work(session->host);
1602
Mike Christie7996a772006-04-06 21:13:41 -05001603 spin_lock_bh(&session->lock);
Mike Christiec8dc1e52006-07-24 15:47:39 -05001604 kfree(conn->data);
Mike Christief3ff0c32006-07-24 15:47:50 -05001605 kfree(conn->persistent_address);
Mike Christie7996a772006-04-06 21:13:41 -05001606 __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
1607 sizeof(void*));
Mike Christie98644042006-10-16 18:09:39 -04001608 if (session->leadconn == conn) {
Mike Christie7996a772006-04-06 21:13:41 -05001609 session->leadconn = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05001610 /* no connections exits.. reset sequencing */
1611 session->cmdsn = session->max_cmdsn = session->exp_cmdsn = 1;
Mike Christie98644042006-10-16 18:09:39 -04001612 }
Mike Christie7996a772006-04-06 21:13:41 -05001613 spin_unlock_bh(&session->lock);
1614
Mike Christie7996a772006-04-06 21:13:41 -05001615 kfifo_free(conn->immqueue);
1616 kfifo_free(conn->mgmtqueue);
1617
1618 iscsi_destroy_conn(cls_conn);
1619}
1620EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
1621
1622int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
1623{
1624 struct iscsi_conn *conn = cls_conn->dd_data;
1625 struct iscsi_session *session = conn->session;
1626
Mike Christieffd04362006-08-31 18:09:24 -04001627 if (!session) {
Mike Christie7996a772006-04-06 21:13:41 -05001628 printk(KERN_ERR "iscsi: can't start unbound connection\n");
1629 return -EPERM;
1630 }
1631
Mike Christiedb98ccd2006-08-31 18:09:31 -04001632 if ((session->imm_data_en || !session->initial_r2t_en) &&
1633 session->first_burst > session->max_burst) {
Mike Christieffd04362006-08-31 18:09:24 -04001634 printk("iscsi: invalid burst lengths: "
1635 "first_burst %d max_burst %d\n",
1636 session->first_burst, session->max_burst);
1637 return -EINVAL;
1638 }
1639
Mike Christie7996a772006-04-06 21:13:41 -05001640 spin_lock_bh(&session->lock);
1641 conn->c_stage = ISCSI_CONN_STARTED;
1642 session->state = ISCSI_STATE_LOGGED_IN;
1643
1644 switch(conn->stop_stage) {
1645 case STOP_CONN_RECOVER:
1646 /*
1647 * unblock eh_abort() if it is blocked. re-try all
1648 * commands after successful recovery
1649 */
Mike Christie7996a772006-04-06 21:13:41 -05001650 conn->stop_stage = 0;
1651 conn->tmabort_state = TMABORT_INITIAL;
1652 session->age++;
Mike Christie7996a772006-04-06 21:13:41 -05001653 spin_unlock_bh(&session->lock);
1654
1655 iscsi_unblock_session(session_to_cls(session));
1656 wake_up(&conn->ehwait);
1657 return 0;
1658 case STOP_CONN_TERM:
Mike Christie7996a772006-04-06 21:13:41 -05001659 conn->stop_stage = 0;
1660 break;
Mike Christie7996a772006-04-06 21:13:41 -05001661 default:
1662 break;
1663 }
1664 spin_unlock_bh(&session->lock);
1665
1666 return 0;
1667}
1668EXPORT_SYMBOL_GPL(iscsi_conn_start);
1669
1670static void
1671flush_control_queues(struct iscsi_session *session, struct iscsi_conn *conn)
1672{
1673 struct iscsi_mgmt_task *mtask, *tmp;
1674
1675 /* handle pending */
1676 while (__kfifo_get(conn->immqueue, (void*)&mtask, sizeof(void*)) ||
1677 __kfifo_get(conn->mgmtqueue, (void*)&mtask, sizeof(void*))) {
1678 if (mtask == conn->login_mtask)
1679 continue;
1680 debug_scsi("flushing pending mgmt task itt 0x%x\n", mtask->itt);
1681 __kfifo_put(session->mgmtpool.queue, (void*)&mtask,
1682 sizeof(void*));
1683 }
1684
1685 /* handle running */
1686 list_for_each_entry_safe(mtask, tmp, &conn->mgmt_run_list, running) {
1687 debug_scsi("flushing running mgmt task itt 0x%x\n", mtask->itt);
Mike Christieed2abc72006-05-02 19:46:40 -05001688 list_del(&mtask->running);
1689
Mike Christie7996a772006-04-06 21:13:41 -05001690 if (mtask == conn->login_mtask)
1691 continue;
Mike Christieed2abc72006-05-02 19:46:40 -05001692 __kfifo_put(session->mgmtpool.queue, (void*)&mtask,
Mike Christie7996a772006-04-06 21:13:41 -05001693 sizeof(void*));
1694 }
1695
1696 conn->mtask = NULL;
1697}
1698
1699/* Fail commands. Mutex and session lock held and recv side suspended */
1700static void fail_all_commands(struct iscsi_conn *conn)
1701{
1702 struct iscsi_cmd_task *ctask, *tmp;
1703
1704 /* flush pending */
Mike Christieb6c395e2006-07-24 15:47:15 -05001705 list_for_each_entry_safe(ctask, tmp, &conn->xmitqueue, running) {
Mike Christie7996a772006-04-06 21:13:41 -05001706 debug_scsi("failing pending sc %p itt 0x%x\n", ctask->sc,
1707 ctask->itt);
1708 fail_command(conn, ctask, DID_BUS_BUSY << 16);
1709 }
1710
1711 /* fail all other running */
1712 list_for_each_entry_safe(ctask, tmp, &conn->run_list, running) {
1713 debug_scsi("failing in progress sc %p itt 0x%x\n",
1714 ctask->sc, ctask->itt);
1715 fail_command(conn, ctask, DID_BUS_BUSY << 16);
1716 }
1717
1718 conn->ctask = NULL;
1719}
1720
Mike Christie656cffc2006-05-18 20:31:42 -05001721static void iscsi_start_session_recovery(struct iscsi_session *session,
1722 struct iscsi_conn *conn, int flag)
Mike Christie7996a772006-04-06 21:13:41 -05001723{
Mike Christieed2abc72006-05-02 19:46:40 -05001724 int old_stop_stage;
1725
Mike Christie7996a772006-04-06 21:13:41 -05001726 spin_lock_bh(&session->lock);
Mike Christieed2abc72006-05-02 19:46:40 -05001727 if (conn->stop_stage == STOP_CONN_TERM) {
Mike Christie7996a772006-04-06 21:13:41 -05001728 spin_unlock_bh(&session->lock);
1729 return;
1730 }
Mike Christieed2abc72006-05-02 19:46:40 -05001731
1732 /*
1733 * When this is called for the in_login state, we only want to clean
Mike Christie67a61112006-05-30 00:37:20 -05001734 * up the login task and connection. We do not need to block and set
1735 * the recovery state again
Mike Christieed2abc72006-05-02 19:46:40 -05001736 */
Mike Christie67a61112006-05-30 00:37:20 -05001737 if (flag == STOP_CONN_TERM)
1738 session->state = ISCSI_STATE_TERMINATE;
1739 else if (conn->stop_stage != STOP_CONN_RECOVER)
1740 session->state = ISCSI_STATE_IN_RECOVERY;
Mike Christieed2abc72006-05-02 19:46:40 -05001741
1742 old_stop_stage = conn->stop_stage;
Mike Christie7996a772006-04-06 21:13:41 -05001743 conn->stop_stage = flag;
Mike Christie67a61112006-05-30 00:37:20 -05001744 conn->c_stage = ISCSI_CONN_STOPPED;
1745 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
Mike Christie7996a772006-04-06 21:13:41 -05001746 spin_unlock_bh(&session->lock);
1747
Mike Christie1c834692006-07-24 15:47:26 -05001748 write_lock_bh(conn->recv_lock);
1749 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
1750 write_unlock_bh(conn->recv_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001751
1752 mutex_lock(&conn->xmitmutex);
Mike Christie7996a772006-04-06 21:13:41 -05001753 /*
1754 * for connection level recovery we should not calculate
1755 * header digest. conn->hdr_size used for optimization
1756 * in hdr_extract() and will be re-negotiated at
1757 * set_param() time.
1758 */
1759 if (flag == STOP_CONN_RECOVER) {
1760 conn->hdrdgst_en = 0;
1761 conn->datadgst_en = 0;
Mike Christie656cffc2006-05-18 20:31:42 -05001762 if (session->state == ISCSI_STATE_IN_RECOVERY &&
Mike Christie67a61112006-05-30 00:37:20 -05001763 old_stop_stage != STOP_CONN_RECOVER) {
1764 debug_scsi("blocking session\n");
Mike Christie7996a772006-04-06 21:13:41 -05001765 iscsi_block_session(session_to_cls(session));
Mike Christie67a61112006-05-30 00:37:20 -05001766 }
Mike Christie7996a772006-04-06 21:13:41 -05001767 }
Mike Christie656cffc2006-05-18 20:31:42 -05001768
Mike Christie656cffc2006-05-18 20:31:42 -05001769 /*
1770 * flush queues.
1771 */
1772 spin_lock_bh(&session->lock);
1773 fail_all_commands(conn);
1774 flush_control_queues(session, conn);
1775 spin_unlock_bh(&session->lock);
1776
Mike Christie7996a772006-04-06 21:13:41 -05001777 mutex_unlock(&conn->xmitmutex);
1778}
Mike Christie7996a772006-04-06 21:13:41 -05001779
1780void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
1781{
1782 struct iscsi_conn *conn = cls_conn->dd_data;
1783 struct iscsi_session *session = conn->session;
1784
1785 switch (flag) {
1786 case STOP_CONN_RECOVER:
1787 case STOP_CONN_TERM:
1788 iscsi_start_session_recovery(session, conn, flag);
Mike Christie8d2860b2006-05-02 19:46:47 -05001789 break;
Mike Christie7996a772006-04-06 21:13:41 -05001790 default:
Or Gerlitzbe2df722006-05-02 19:46:43 -05001791 printk(KERN_ERR "iscsi: invalid stop flag %d\n", flag);
Mike Christie7996a772006-04-06 21:13:41 -05001792 }
1793}
1794EXPORT_SYMBOL_GPL(iscsi_conn_stop);
1795
1796int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
1797 struct iscsi_cls_conn *cls_conn, int is_leading)
1798{
1799 struct iscsi_session *session = class_to_transport_session(cls_session);
Mike Christie98644042006-10-16 18:09:39 -04001800 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05001801
Mike Christie7996a772006-04-06 21:13:41 -05001802 spin_lock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001803 if (is_leading)
1804 session->leadconn = conn;
Mike Christie98644042006-10-16 18:09:39 -04001805 spin_unlock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001806
1807 /*
1808 * Unblock xmitworker(), Login Phase will pass through.
1809 */
1810 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
1811 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1812 return 0;
1813}
1814EXPORT_SYMBOL_GPL(iscsi_conn_bind);
1815
Mike Christiea54a52c2006-06-28 12:00:23 -05001816
1817int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
1818 enum iscsi_param param, char *buf, int buflen)
1819{
1820 struct iscsi_conn *conn = cls_conn->dd_data;
1821 struct iscsi_session *session = conn->session;
1822 uint32_t value;
1823
1824 switch(param) {
1825 case ISCSI_PARAM_MAX_RECV_DLENGTH:
1826 sscanf(buf, "%d", &conn->max_recv_dlength);
1827 break;
1828 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
1829 sscanf(buf, "%d", &conn->max_xmit_dlength);
1830 break;
1831 case ISCSI_PARAM_HDRDGST_EN:
1832 sscanf(buf, "%d", &conn->hdrdgst_en);
1833 break;
1834 case ISCSI_PARAM_DATADGST_EN:
1835 sscanf(buf, "%d", &conn->datadgst_en);
1836 break;
1837 case ISCSI_PARAM_INITIAL_R2T_EN:
1838 sscanf(buf, "%d", &session->initial_r2t_en);
1839 break;
1840 case ISCSI_PARAM_MAX_R2T:
1841 sscanf(buf, "%d", &session->max_r2t);
1842 break;
1843 case ISCSI_PARAM_IMM_DATA_EN:
1844 sscanf(buf, "%d", &session->imm_data_en);
1845 break;
1846 case ISCSI_PARAM_FIRST_BURST:
1847 sscanf(buf, "%d", &session->first_burst);
1848 break;
1849 case ISCSI_PARAM_MAX_BURST:
1850 sscanf(buf, "%d", &session->max_burst);
1851 break;
1852 case ISCSI_PARAM_PDU_INORDER_EN:
1853 sscanf(buf, "%d", &session->pdu_inorder_en);
1854 break;
1855 case ISCSI_PARAM_DATASEQ_INORDER_EN:
1856 sscanf(buf, "%d", &session->dataseq_inorder_en);
1857 break;
1858 case ISCSI_PARAM_ERL:
1859 sscanf(buf, "%d", &session->erl);
1860 break;
1861 case ISCSI_PARAM_IFMARKER_EN:
1862 sscanf(buf, "%d", &value);
1863 BUG_ON(value);
1864 break;
1865 case ISCSI_PARAM_OFMARKER_EN:
1866 sscanf(buf, "%d", &value);
1867 BUG_ON(value);
1868 break;
1869 case ISCSI_PARAM_EXP_STATSN:
1870 sscanf(buf, "%u", &conn->exp_statsn);
1871 break;
1872 case ISCSI_PARAM_TARGET_NAME:
1873 /* this should not change between logins */
1874 if (session->targetname)
1875 break;
1876
1877 session->targetname = kstrdup(buf, GFP_KERNEL);
1878 if (!session->targetname)
1879 return -ENOMEM;
1880 break;
1881 case ISCSI_PARAM_TPGT:
1882 sscanf(buf, "%d", &session->tpgt);
1883 break;
1884 case ISCSI_PARAM_PERSISTENT_PORT:
1885 sscanf(buf, "%d", &conn->persistent_port);
1886 break;
1887 case ISCSI_PARAM_PERSISTENT_ADDRESS:
1888 /*
1889 * this is the address returned in discovery so it should
1890 * not change between logins.
1891 */
1892 if (conn->persistent_address)
1893 break;
1894
1895 conn->persistent_address = kstrdup(buf, GFP_KERNEL);
1896 if (!conn->persistent_address)
1897 return -ENOMEM;
1898 break;
1899 default:
1900 return -ENOSYS;
1901 }
1902
1903 return 0;
1904}
1905EXPORT_SYMBOL_GPL(iscsi_set_param);
1906
1907int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
1908 enum iscsi_param param, char *buf)
1909{
1910 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
1911 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
1912 int len;
1913
1914 switch(param) {
1915 case ISCSI_PARAM_INITIAL_R2T_EN:
1916 len = sprintf(buf, "%d\n", session->initial_r2t_en);
1917 break;
1918 case ISCSI_PARAM_MAX_R2T:
1919 len = sprintf(buf, "%hu\n", session->max_r2t);
1920 break;
1921 case ISCSI_PARAM_IMM_DATA_EN:
1922 len = sprintf(buf, "%d\n", session->imm_data_en);
1923 break;
1924 case ISCSI_PARAM_FIRST_BURST:
1925 len = sprintf(buf, "%u\n", session->first_burst);
1926 break;
1927 case ISCSI_PARAM_MAX_BURST:
1928 len = sprintf(buf, "%u\n", session->max_burst);
1929 break;
1930 case ISCSI_PARAM_PDU_INORDER_EN:
1931 len = sprintf(buf, "%d\n", session->pdu_inorder_en);
1932 break;
1933 case ISCSI_PARAM_DATASEQ_INORDER_EN:
1934 len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
1935 break;
1936 case ISCSI_PARAM_ERL:
1937 len = sprintf(buf, "%d\n", session->erl);
1938 break;
1939 case ISCSI_PARAM_TARGET_NAME:
1940 len = sprintf(buf, "%s\n", session->targetname);
1941 break;
1942 case ISCSI_PARAM_TPGT:
1943 len = sprintf(buf, "%d\n", session->tpgt);
1944 break;
1945 default:
1946 return -ENOSYS;
1947 }
1948
1949 return len;
1950}
1951EXPORT_SYMBOL_GPL(iscsi_session_get_param);
1952
1953int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
1954 enum iscsi_param param, char *buf)
1955{
1956 struct iscsi_conn *conn = cls_conn->dd_data;
1957 int len;
1958
1959 switch(param) {
1960 case ISCSI_PARAM_MAX_RECV_DLENGTH:
1961 len = sprintf(buf, "%u\n", conn->max_recv_dlength);
1962 break;
1963 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
1964 len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
1965 break;
1966 case ISCSI_PARAM_HDRDGST_EN:
1967 len = sprintf(buf, "%d\n", conn->hdrdgst_en);
1968 break;
1969 case ISCSI_PARAM_DATADGST_EN:
1970 len = sprintf(buf, "%d\n", conn->datadgst_en);
1971 break;
1972 case ISCSI_PARAM_IFMARKER_EN:
1973 len = sprintf(buf, "%d\n", conn->ifmarker_en);
1974 break;
1975 case ISCSI_PARAM_OFMARKER_EN:
1976 len = sprintf(buf, "%d\n", conn->ofmarker_en);
1977 break;
1978 case ISCSI_PARAM_EXP_STATSN:
1979 len = sprintf(buf, "%u\n", conn->exp_statsn);
1980 break;
1981 case ISCSI_PARAM_PERSISTENT_PORT:
1982 len = sprintf(buf, "%d\n", conn->persistent_port);
1983 break;
1984 case ISCSI_PARAM_PERSISTENT_ADDRESS:
1985 len = sprintf(buf, "%s\n", conn->persistent_address);
1986 break;
1987 default:
1988 return -ENOSYS;
1989 }
1990
1991 return len;
1992}
1993EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
1994
Mike Christie0801c242007-05-30 12:57:12 -05001995int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
1996 char *buf)
1997{
1998 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
1999 int len;
2000
2001 switch (param) {
2002 case ISCSI_HOST_PARAM_HWADDRESS:
2003 if (!session->hwaddress)
2004 len = sprintf(buf, "%s\n", "default");
2005 else
2006 len = sprintf(buf, "%s\n", session->hwaddress);
2007 break;
Mike Christie8ad57812007-05-30 12:57:13 -05002008 case ISCSI_HOST_PARAM_INITIATOR_NAME:
2009 if (!session->initiatorname)
2010 len = sprintf(buf, "%s\n", "unknown");
2011 else
2012 len = sprintf(buf, "%s\n", session->initiatorname);
2013 break;
2014
Mike Christie0801c242007-05-30 12:57:12 -05002015 default:
2016 return -ENOSYS;
2017 }
2018
2019 return len;
2020}
2021EXPORT_SYMBOL_GPL(iscsi_host_get_param);
2022
2023int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2024 char *buf, int buflen)
2025{
2026 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
2027
2028 switch (param) {
2029 case ISCSI_HOST_PARAM_HWADDRESS:
2030 if (!session->hwaddress)
2031 session->hwaddress = kstrdup(buf, GFP_KERNEL);
2032 break;
Mike Christie8ad57812007-05-30 12:57:13 -05002033 case ISCSI_HOST_PARAM_INITIATOR_NAME:
2034 if (!session->initiatorname)
2035 session->initiatorname = kstrdup(buf, GFP_KERNEL);
2036 break;
Mike Christie0801c242007-05-30 12:57:12 -05002037 default:
2038 return -ENOSYS;
2039 }
2040
2041 return 0;
2042}
2043EXPORT_SYMBOL_GPL(iscsi_host_set_param);
2044
Mike Christie7996a772006-04-06 21:13:41 -05002045MODULE_AUTHOR("Mike Christie");
2046MODULE_DESCRIPTION("iSCSI library functions");
2047MODULE_LICENSE("GPL");