blob: d37048c96eab5163231f04917408a6c418c25b79 [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>
28#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
48#define INVALID_SN_DELTA 0xffff
49
50int
51iscsi_check_assign_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr)
52{
53 uint32_t max_cmdsn = be32_to_cpu(hdr->max_cmdsn);
54 uint32_t exp_cmdsn = be32_to_cpu(hdr->exp_cmdsn);
55
56 if (max_cmdsn < exp_cmdsn -1 &&
57 max_cmdsn > exp_cmdsn - INVALID_SN_DELTA)
58 return ISCSI_ERR_MAX_CMDSN;
59 if (max_cmdsn > session->max_cmdsn ||
60 max_cmdsn < session->max_cmdsn - INVALID_SN_DELTA)
61 session->max_cmdsn = max_cmdsn;
62 if (exp_cmdsn > session->exp_cmdsn ||
63 exp_cmdsn < session->exp_cmdsn - INVALID_SN_DELTA)
64 session->exp_cmdsn = exp_cmdsn;
65
66 return 0;
67}
68EXPORT_SYMBOL_GPL(iscsi_check_assign_cmdsn);
69
70void iscsi_prep_unsolicit_data_pdu(struct iscsi_cmd_task *ctask,
Mike Christieffd04362006-08-31 18:09:24 -040071 struct iscsi_data *hdr)
Mike Christie7996a772006-04-06 21:13:41 -050072{
73 struct iscsi_conn *conn = ctask->conn;
74
75 memset(hdr, 0, sizeof(struct iscsi_data));
76 hdr->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
77 hdr->datasn = cpu_to_be32(ctask->unsol_datasn);
78 ctask->unsol_datasn++;
79 hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
80 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
81
82 hdr->itt = ctask->hdr->itt;
83 hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
Mike Christieffd04362006-08-31 18:09:24 -040084 hdr->offset = cpu_to_be32(ctask->unsol_offset);
Mike Christie7996a772006-04-06 21:13:41 -050085
86 if (ctask->unsol_count > conn->max_xmit_dlength) {
87 hton24(hdr->dlength, conn->max_xmit_dlength);
88 ctask->data_count = conn->max_xmit_dlength;
Mike Christieffd04362006-08-31 18:09:24 -040089 ctask->unsol_offset += ctask->data_count;
Mike Christie7996a772006-04-06 21:13:41 -050090 hdr->flags = 0;
91 } else {
92 hton24(hdr->dlength, ctask->unsol_count);
93 ctask->data_count = ctask->unsol_count;
94 hdr->flags = ISCSI_FLAG_CMD_FINAL;
95 }
96}
97EXPORT_SYMBOL_GPL(iscsi_prep_unsolicit_data_pdu);
98
99/**
100 * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
101 * @ctask: iscsi cmd task
102 *
103 * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
104 * fields like dlength or final based on how much data it sends
105 */
106static void iscsi_prep_scsi_cmd_pdu(struct iscsi_cmd_task *ctask)
107{
108 struct iscsi_conn *conn = ctask->conn;
109 struct iscsi_session *session = conn->session;
110 struct iscsi_cmd *hdr = ctask->hdr;
111 struct scsi_cmnd *sc = ctask->sc;
112
113 hdr->opcode = ISCSI_OP_SCSI_CMD;
114 hdr->flags = ISCSI_ATTR_SIMPLE;
115 int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
116 hdr->itt = ctask->itt | (conn->id << ISCSI_CID_SHIFT) |
117 (session->age << ISCSI_AGE_SHIFT);
118 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 Christie9b80cb42006-12-17 12:10:28 -0600273 senselen = be16_to_cpu(*(uint16_t *)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",
280 min(senselen, SCSI_SENSE_BUFFERSIZE));
281 }
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));
341 itt = rejected_pdu.itt & ISCSI_ITT_MASK;
342 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
370 if (hdr->itt != cpu_to_be32(ISCSI_RESERVED_TAG))
371 itt = hdr->itt & ISCSI_ITT_MASK;
372 else
373 itt = hdr->itt;
374
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:
Mike Christiec8dc1e52006-07-24 15:47:39 -0500443 if (hdr->ttt != 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 }
460 } else if (itt == ISCSI_RESERVED_TAG) {
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
Mike Christie40527af2006-07-24 15:47:45 -0500473 if (hdr->ttt == ISCSI_RESERVED_TAG)
474 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
519 if (hdr->itt != cpu_to_be32(ISCSI_RESERVED_TAG)) {
520 if ((hdr->itt & ISCSI_AGE_MASK) !=
521 (session->age << ISCSI_AGE_SHIFT)) {
Or Gerlitzbe2df722006-05-02 19:46:43 -0500522 printk(KERN_ERR "iscsi: received itt %x expected "
Mike Christie7996a772006-04-06 21:13:41 -0500523 "session age (%x)\n", hdr->itt,
524 session->age & ISCSI_AGE_MASK);
525 return ISCSI_ERR_BAD_ITT;
526 }
527
528 if ((hdr->itt & ISCSI_CID_MASK) !=
529 (conn->id << ISCSI_CID_SHIFT)) {
Or Gerlitzbe2df722006-05-02 19:46:43 -0500530 printk(KERN_ERR "iscsi: received itt %x, expected "
Mike Christie7996a772006-04-06 21:13:41 -0500531 "CID (%x)\n", hdr->itt, conn->id);
532 return ISCSI_ERR_BAD_ITT;
533 }
534 itt = hdr->itt & ISCSI_ITT_MASK;
535 } else
536 itt = hdr->itt;
537
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 Christieb5072ea2006-10-16 18:09:42 -0400581static int iscsi_xmit_imm_task(struct iscsi_conn *conn)
582{
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
595 if (was_logout) {
596 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
597 return -ENODATA;
598 }
599 return 0;
600}
601
Mike Christie7996a772006-04-06 21:13:41 -0500602/**
603 * iscsi_data_xmit - xmit any command into the scheduled connection
604 * @conn: iscsi connection
605 *
606 * Notes:
607 * The function can return -EAGAIN in which case the caller must
608 * re-schedule it again later or recover. '0' return code means
609 * successful xmit.
610 **/
611static int iscsi_data_xmit(struct iscsi_conn *conn)
612{
613 struct iscsi_transport *tt;
Mike Christie3219e522006-05-30 00:37:28 -0500614 int rc = 0;
Mike Christie7996a772006-04-06 21:13:41 -0500615
616 if (unlikely(conn->suspend_tx)) {
617 debug_scsi("conn %d Tx suspended!\n", conn->id);
Mike Christie3219e522006-05-30 00:37:28 -0500618 return -ENODATA;
Mike Christie7996a772006-04-06 21:13:41 -0500619 }
620 tt = conn->session->tt;
621
622 /*
623 * Transmit in the following order:
624 *
625 * 1) un-finished xmit (ctask or mtask)
626 * 2) immediate control PDUs
627 * 3) write data
628 * 4) SCSI commands
629 * 5) non-immediate control PDUs
630 *
631 * No need to lock around __kfifo_get as long as
632 * there's one producer and one consumer.
633 */
634
635 BUG_ON(conn->ctask && conn->mtask);
636
637 if (conn->ctask) {
Mike Christie60ecebf2006-08-31 18:09:25 -0400638 iscsi_get_ctask(conn->ctask);
Mike Christie3219e522006-05-30 00:37:28 -0500639 rc = tt->xmit_cmd_task(conn, conn->ctask);
Mike Christie60ecebf2006-08-31 18:09:25 -0400640 iscsi_put_ctask(conn->ctask);
Mike Christie3219e522006-05-30 00:37:28 -0500641 if (rc)
Mike Christie7996a772006-04-06 21:13:41 -0500642 goto again;
643 /* done with this in-progress ctask */
644 conn->ctask = NULL;
645 }
646 if (conn->mtask) {
Mike Christieb5072ea2006-10-16 18:09:42 -0400647 rc = iscsi_xmit_imm_task(conn);
Mike Christie3219e522006-05-30 00:37:28 -0500648 if (rc)
Mike Christie7996a772006-04-06 21:13:41 -0500649 goto again;
650 /* done with this in-progress mtask */
651 conn->mtask = NULL;
652 }
653
654 /* process immediate first */
655 if (unlikely(__kfifo_len(conn->immqueue))) {
656 while (__kfifo_get(conn->immqueue, (void*)&conn->mtask,
657 sizeof(void*))) {
Mike Christie994442e2006-05-30 00:37:22 -0500658 spin_lock_bh(&conn->session->lock);
Mike Christie7996a772006-04-06 21:13:41 -0500659 list_add_tail(&conn->mtask->running,
660 &conn->mgmt_run_list);
Mike Christie994442e2006-05-30 00:37:22 -0500661 spin_unlock_bh(&conn->session->lock);
Mike Christieb5072ea2006-10-16 18:09:42 -0400662 rc = iscsi_xmit_imm_task(conn);
Mike Christie3219e522006-05-30 00:37:28 -0500663 if (rc)
Mike Christie7996a772006-04-06 21:13:41 -0500664 goto again;
665 }
666 /* done with this mtask */
667 conn->mtask = NULL;
668 }
669
670 /* process command queue */
Mike Christieb6c395e2006-07-24 15:47:15 -0500671 spin_lock_bh(&conn->session->lock);
672 while (!list_empty(&conn->xmitqueue)) {
Mike Christie7996a772006-04-06 21:13:41 -0500673 /*
674 * iscsi tcp may readd the task to the xmitqueue to send
675 * write data
676 */
Mike Christieb6c395e2006-07-24 15:47:15 -0500677 conn->ctask = list_entry(conn->xmitqueue.next,
678 struct iscsi_cmd_task, running);
679 conn->ctask->state = ISCSI_TASK_RUNNING;
680 list_move_tail(conn->xmitqueue.next, &conn->run_list);
Mike Christie60ecebf2006-08-31 18:09:25 -0400681 __iscsi_get_ctask(conn->ctask);
Mike Christie994442e2006-05-30 00:37:22 -0500682 spin_unlock_bh(&conn->session->lock);
Mike Christieb6c395e2006-07-24 15:47:15 -0500683
Mike Christie3219e522006-05-30 00:37:28 -0500684 rc = tt->xmit_cmd_task(conn, conn->ctask);
Mike Christie60ecebf2006-08-31 18:09:25 -0400685
Mike Christieb6c395e2006-07-24 15:47:15 -0500686 spin_lock_bh(&conn->session->lock);
Mike Christie60ecebf2006-08-31 18:09:25 -0400687 __iscsi_put_ctask(conn->ctask);
688 if (rc) {
689 spin_unlock_bh(&conn->session->lock);
690 goto again;
691 }
Mike Christie7996a772006-04-06 21:13:41 -0500692 }
Mike Christieb6c395e2006-07-24 15:47:15 -0500693 spin_unlock_bh(&conn->session->lock);
Mike Christie7996a772006-04-06 21:13:41 -0500694 /* done with this ctask */
695 conn->ctask = NULL;
696
697 /* process the rest control plane PDUs, if any */
698 if (unlikely(__kfifo_len(conn->mgmtqueue))) {
699 while (__kfifo_get(conn->mgmtqueue, (void*)&conn->mtask,
700 sizeof(void*))) {
Mike Christie994442e2006-05-30 00:37:22 -0500701 spin_lock_bh(&conn->session->lock);
Mike Christie7996a772006-04-06 21:13:41 -0500702 list_add_tail(&conn->mtask->running,
703 &conn->mgmt_run_list);
Mike Christie994442e2006-05-30 00:37:22 -0500704 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -0500705 rc = tt->xmit_mgmt_task(conn, conn->mtask);
706 if (rc)
Mike Christie7996a772006-04-06 21:13:41 -0500707 goto again;
708 }
709 /* done with this mtask */
710 conn->mtask = NULL;
711 }
712
Mike Christie3219e522006-05-30 00:37:28 -0500713 return -ENODATA;
Mike Christie7996a772006-04-06 21:13:41 -0500714
715again:
716 if (unlikely(conn->suspend_tx))
Mike Christie3219e522006-05-30 00:37:28 -0500717 return -ENODATA;
Mike Christie7996a772006-04-06 21:13:41 -0500718
Mike Christie3219e522006-05-30 00:37:28 -0500719 return rc;
Mike Christie7996a772006-04-06 21:13:41 -0500720}
721
David Howellsc4028952006-11-22 14:57:56 +0000722static void iscsi_xmitworker(struct work_struct *work)
Mike Christie7996a772006-04-06 21:13:41 -0500723{
David Howellsc4028952006-11-22 14:57:56 +0000724 struct iscsi_conn *conn =
725 container_of(work, struct iscsi_conn, xmitwork);
Mike Christie3219e522006-05-30 00:37:28 -0500726 int rc;
Mike Christie7996a772006-04-06 21:13:41 -0500727 /*
728 * serialize Xmit worker on a per-connection basis.
729 */
730 mutex_lock(&conn->xmitmutex);
Mike Christie3219e522006-05-30 00:37:28 -0500731 do {
732 rc = iscsi_data_xmit(conn);
733 } while (rc >= 0 || rc == -EAGAIN);
Mike Christie7996a772006-04-06 21:13:41 -0500734 mutex_unlock(&conn->xmitmutex);
735}
736
737enum {
738 FAILURE_BAD_HOST = 1,
739 FAILURE_SESSION_FAILED,
740 FAILURE_SESSION_FREED,
741 FAILURE_WINDOW_CLOSED,
Mike Christie60ecebf2006-08-31 18:09:25 -0400742 FAILURE_OOM,
Mike Christie7996a772006-04-06 21:13:41 -0500743 FAILURE_SESSION_TERMINATE,
Mike Christie656cffc2006-05-18 20:31:42 -0500744 FAILURE_SESSION_IN_RECOVERY,
Mike Christie7996a772006-04-06 21:13:41 -0500745 FAILURE_SESSION_RECOVERY_TIMEOUT,
746};
747
748int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
749{
750 struct Scsi_Host *host;
751 int reason = 0;
752 struct iscsi_session *session;
753 struct iscsi_conn *conn;
754 struct iscsi_cmd_task *ctask = NULL;
755
756 sc->scsi_done = done;
757 sc->result = 0;
Mike Christief47f2cf2006-08-31 18:09:33 -0400758 sc->SCp.ptr = NULL;
Mike Christie7996a772006-04-06 21:13:41 -0500759
760 host = sc->device->host;
761 session = iscsi_hostdata(host->hostdata);
762
763 spin_lock(&session->lock);
764
Mike Christie656cffc2006-05-18 20:31:42 -0500765 /*
766 * ISCSI_STATE_FAILED is a temp. state. The recovery
767 * code will decide what is best to do with command queued
768 * during this time
769 */
770 if (session->state != ISCSI_STATE_LOGGED_IN &&
771 session->state != ISCSI_STATE_FAILED) {
772 /*
773 * to handle the race between when we set the recovery state
774 * and block the session we requeue here (commands could
775 * be entering our queuecommand while a block is starting
776 * up because the block code is not locked)
777 */
778 if (session->state == ISCSI_STATE_IN_RECOVERY) {
779 reason = FAILURE_SESSION_IN_RECOVERY;
Mike Christie67a61112006-05-30 00:37:20 -0500780 goto reject;
Mike Christie7996a772006-04-06 21:13:41 -0500781 }
Mike Christie656cffc2006-05-18 20:31:42 -0500782
783 if (session->state == ISCSI_STATE_RECOVERY_FAILED)
784 reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
785 else if (session->state == ISCSI_STATE_TERMINATE)
786 reason = FAILURE_SESSION_TERMINATE;
787 else
788 reason = FAILURE_SESSION_FREED;
Mike Christie7996a772006-04-06 21:13:41 -0500789 goto fault;
790 }
791
792 /*
793 * Check for iSCSI window and take care of CmdSN wrap-around
794 */
795 if ((int)(session->max_cmdsn - session->cmdsn) < 0) {
796 reason = FAILURE_WINDOW_CLOSED;
797 goto reject;
798 }
799
800 conn = session->leadconn;
Mike Christie98644042006-10-16 18:09:39 -0400801 if (!conn) {
802 reason = FAILURE_SESSION_FREED;
803 goto fault;
804 }
Mike Christie7996a772006-04-06 21:13:41 -0500805
Mike Christie60ecebf2006-08-31 18:09:25 -0400806 if (!__kfifo_get(session->cmdpool.queue, (void*)&ctask,
807 sizeof(void*))) {
808 reason = FAILURE_OOM;
809 goto reject;
810 }
Mike Christie7996a772006-04-06 21:13:41 -0500811 sc->SCp.phase = session->age;
812 sc->SCp.ptr = (char *)ctask;
813
Mike Christie60ecebf2006-08-31 18:09:25 -0400814 atomic_set(&ctask->refcount, 1);
Mike Christieb6c395e2006-07-24 15:47:15 -0500815 ctask->state = ISCSI_TASK_PENDING;
Mike Christie7996a772006-04-06 21:13:41 -0500816 ctask->mtask = NULL;
817 ctask->conn = conn;
818 ctask->sc = sc;
819 INIT_LIST_HEAD(&ctask->running);
820 ctask->total_length = sc->request_bufflen;
821 iscsi_prep_scsi_cmd_pdu(ctask);
822
823 session->tt->init_cmd_task(ctask);
824
Mike Christieb6c395e2006-07-24 15:47:15 -0500825 list_add_tail(&ctask->running, &conn->xmitqueue);
Mike Christie7996a772006-04-06 21:13:41 -0500826 debug_scsi(
Mike Christief47f2cf2006-08-31 18:09:33 -0400827 "ctask enq [%s cid %d sc %p cdb 0x%x itt 0x%x len %d cmdsn %d "
828 "win %d]\n",
Mike Christie7996a772006-04-06 21:13:41 -0500829 sc->sc_data_direction == DMA_TO_DEVICE ? "write" : "read",
Mike Christief47f2cf2006-08-31 18:09:33 -0400830 conn->id, sc, sc->cmnd[0], ctask->itt, sc->request_bufflen,
Mike Christie7996a772006-04-06 21:13:41 -0500831 session->cmdsn, session->max_cmdsn - session->exp_cmdsn + 1);
832 spin_unlock(&session->lock);
833
834 scsi_queue_work(host, &conn->xmitwork);
835 return 0;
836
837reject:
838 spin_unlock(&session->lock);
839 debug_scsi("cmd 0x%x rejected (%d)\n", sc->cmnd[0], reason);
840 return SCSI_MLQUEUE_HOST_BUSY;
841
842fault:
843 spin_unlock(&session->lock);
Or Gerlitzbe2df722006-05-02 19:46:43 -0500844 printk(KERN_ERR "iscsi: cmd 0x%x is not queued (%d)\n",
Mike Christie7996a772006-04-06 21:13:41 -0500845 sc->cmnd[0], reason);
846 sc->result = (DID_NO_CONNECT << 16);
847 sc->resid = sc->request_bufflen;
848 sc->scsi_done(sc);
849 return 0;
850}
851EXPORT_SYMBOL_GPL(iscsi_queuecommand);
852
853int iscsi_change_queue_depth(struct scsi_device *sdev, int depth)
854{
855 if (depth > ISCSI_MAX_CMD_PER_LUN)
856 depth = ISCSI_MAX_CMD_PER_LUN;
857 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
858 return sdev->queue_depth;
859}
860EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
861
862static int
863iscsi_conn_send_generic(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
864 char *data, uint32_t data_size)
865{
866 struct iscsi_session *session = conn->session;
867 struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
868 struct iscsi_mgmt_task *mtask;
869
870 spin_lock_bh(&session->lock);
871 if (session->state == ISCSI_STATE_TERMINATE) {
872 spin_unlock_bh(&session->lock);
873 return -EPERM;
874 }
875 if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) ||
876 hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
877 /*
878 * Login and Text are sent serially, in
879 * request-followed-by-response sequence.
880 * Same mtask can be used. Same ITT must be used.
881 * Note that login_mtask is preallocated at conn_create().
882 */
883 mtask = conn->login_mtask;
884 else {
Mike Christie656cffc2006-05-18 20:31:42 -0500885 BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
886 BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
Mike Christie7996a772006-04-06 21:13:41 -0500887
Mike Christie8d2860b2006-05-02 19:46:47 -0500888 nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
Mike Christie7996a772006-04-06 21:13:41 -0500889 if (!__kfifo_get(session->mgmtpool.queue,
890 (void*)&mtask, sizeof(void*))) {
891 spin_unlock_bh(&session->lock);
892 return -ENOSPC;
893 }
894 }
895
896 /*
Mike Christie8d2860b2006-05-02 19:46:47 -0500897 * pre-format CmdSN for outgoing PDU.
Mike Christie7996a772006-04-06 21:13:41 -0500898 */
899 if (hdr->itt != cpu_to_be32(ISCSI_RESERVED_TAG)) {
900 hdr->itt = mtask->itt | (conn->id << ISCSI_CID_SHIFT) |
901 (session->age << ISCSI_AGE_SHIFT);
902 nop->cmdsn = cpu_to_be32(session->cmdsn);
903 if (conn->c_stage == ISCSI_CONN_STARTED &&
904 !(hdr->opcode & ISCSI_OP_IMMEDIATE))
905 session->cmdsn++;
906 } else
907 /* do not advance CmdSN */
908 nop->cmdsn = cpu_to_be32(session->cmdsn);
909
Mike Christie7996a772006-04-06 21:13:41 -0500910 if (data_size) {
911 memcpy(mtask->data, data, data_size);
912 mtask->data_count = data_size;
913 } else
914 mtask->data_count = 0;
915
916 INIT_LIST_HEAD(&mtask->running);
917 memcpy(mtask->hdr, hdr, sizeof(struct iscsi_hdr));
918 if (session->tt->init_mgmt_task)
919 session->tt->init_mgmt_task(conn, mtask, data, data_size);
920 spin_unlock_bh(&session->lock);
921
922 debug_scsi("mgmtpdu [op 0x%x hdr->itt 0x%x datalen %d]\n",
923 hdr->opcode, hdr->itt, data_size);
924
925 /*
926 * since send_pdu() could be called at least from two contexts,
927 * we need to serialize __kfifo_put, so we don't have to take
928 * additional lock on fast data-path
929 */
930 if (hdr->opcode & ISCSI_OP_IMMEDIATE)
931 __kfifo_put(conn->immqueue, (void*)&mtask, sizeof(void*));
932 else
933 __kfifo_put(conn->mgmtqueue, (void*)&mtask, sizeof(void*));
934
935 scsi_queue_work(session->host, &conn->xmitwork);
936 return 0;
937}
938
939int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
940 char *data, uint32_t data_size)
941{
942 struct iscsi_conn *conn = cls_conn->dd_data;
943 int rc;
944
945 mutex_lock(&conn->xmitmutex);
946 rc = iscsi_conn_send_generic(conn, hdr, data, data_size);
947 mutex_unlock(&conn->xmitmutex);
948
949 return rc;
950}
951EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
952
953void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
954{
955 struct iscsi_session *session = class_to_transport_session(cls_session);
956 struct iscsi_conn *conn = session->leadconn;
957
958 spin_lock_bh(&session->lock);
959 if (session->state != ISCSI_STATE_LOGGED_IN) {
Mike Christie656cffc2006-05-18 20:31:42 -0500960 session->state = ISCSI_STATE_RECOVERY_FAILED;
Mike Christie7996a772006-04-06 21:13:41 -0500961 if (conn)
962 wake_up(&conn->ehwait);
963 }
964 spin_unlock_bh(&session->lock);
965}
966EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
967
968int iscsi_eh_host_reset(struct scsi_cmnd *sc)
969{
970 struct Scsi_Host *host = sc->device->host;
971 struct iscsi_session *session = iscsi_hostdata(host->hostdata);
972 struct iscsi_conn *conn = session->leadconn;
973 int fail_session = 0;
974
975 spin_lock_bh(&session->lock);
976 if (session->state == ISCSI_STATE_TERMINATE) {
977failed:
978 debug_scsi("failing host reset: session terminated "
Pete Wyckoffd6e24d12006-11-08 15:58:32 -0600979 "[CID %d age %d]\n", conn->id, session->age);
Mike Christie7996a772006-04-06 21:13:41 -0500980 spin_unlock_bh(&session->lock);
981 return FAILED;
982 }
983
984 if (sc->SCp.phase == session->age) {
Pete Wyckoffd6e24d12006-11-08 15:58:32 -0600985 debug_scsi("failing connection CID %d due to SCSI host reset\n",
Mike Christie7996a772006-04-06 21:13:41 -0500986 conn->id);
987 fail_session = 1;
988 }
989 spin_unlock_bh(&session->lock);
990
991 /*
992 * we drop the lock here but the leadconn cannot be destoyed while
993 * we are in the scsi eh
994 */
Mike Christie656cffc2006-05-18 20:31:42 -0500995 if (fail_session)
Mike Christie7996a772006-04-06 21:13:41 -0500996 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie7996a772006-04-06 21:13:41 -0500997
998 debug_scsi("iscsi_eh_host_reset wait for relogin\n");
999 wait_event_interruptible(conn->ehwait,
1000 session->state == ISCSI_STATE_TERMINATE ||
1001 session->state == ISCSI_STATE_LOGGED_IN ||
Mike Christie656cffc2006-05-18 20:31:42 -05001002 session->state == ISCSI_STATE_RECOVERY_FAILED);
Mike Christie7996a772006-04-06 21:13:41 -05001003 if (signal_pending(current))
1004 flush_signals(current);
1005
1006 spin_lock_bh(&session->lock);
1007 if (session->state == ISCSI_STATE_LOGGED_IN)
Or Gerlitzbe2df722006-05-02 19:46:43 -05001008 printk(KERN_INFO "iscsi: host reset succeeded\n");
Mike Christie7996a772006-04-06 21:13:41 -05001009 else
1010 goto failed;
1011 spin_unlock_bh(&session->lock);
1012
1013 return SUCCESS;
1014}
1015EXPORT_SYMBOL_GPL(iscsi_eh_host_reset);
1016
1017static void iscsi_tmabort_timedout(unsigned long data)
1018{
1019 struct iscsi_cmd_task *ctask = (struct iscsi_cmd_task *)data;
1020 struct iscsi_conn *conn = ctask->conn;
1021 struct iscsi_session *session = conn->session;
1022
1023 spin_lock(&session->lock);
1024 if (conn->tmabort_state == TMABORT_INITIAL) {
1025 conn->tmabort_state = TMABORT_TIMEDOUT;
1026 debug_scsi("tmabort timedout [sc %p itt 0x%x]\n",
1027 ctask->sc, ctask->itt);
1028 /* unblock eh_abort() */
1029 wake_up(&conn->ehwait);
1030 }
1031 spin_unlock(&session->lock);
1032}
1033
1034/* must be called with the mutex lock */
1035static int iscsi_exec_abort_task(struct scsi_cmnd *sc,
1036 struct iscsi_cmd_task *ctask)
1037{
1038 struct iscsi_conn *conn = ctask->conn;
1039 struct iscsi_session *session = conn->session;
1040 struct iscsi_tm *hdr = &conn->tmhdr;
1041 int rc;
1042
1043 /*
1044 * ctask timed out but session is OK requests must be serialized.
1045 */
1046 memset(hdr, 0, sizeof(struct iscsi_tm));
1047 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
1048 hdr->flags = ISCSI_TM_FUNC_ABORT_TASK;
1049 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
1050 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
1051 hdr->rtt = ctask->hdr->itt;
1052 hdr->refcmdsn = ctask->hdr->cmdsn;
1053
1054 rc = iscsi_conn_send_generic(conn, (struct iscsi_hdr *)hdr,
1055 NULL, 0);
1056 if (rc) {
1057 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Pete Wyckoffd6e24d12006-11-08 15:58:32 -06001058 debug_scsi("abort sent failure [itt 0x%x] %d\n", ctask->itt,
1059 rc);
Mike Christie7996a772006-04-06 21:13:41 -05001060 return rc;
1061 }
1062
1063 debug_scsi("abort sent [itt 0x%x]\n", ctask->itt);
1064
1065 spin_lock_bh(&session->lock);
1066 ctask->mtask = (struct iscsi_mgmt_task *)
1067 session->mgmt_cmds[(hdr->itt & ISCSI_ITT_MASK) -
1068 ISCSI_MGMT_ITT_OFFSET];
1069
1070 if (conn->tmabort_state == TMABORT_INITIAL) {
1071 conn->tmfcmd_pdus_cnt++;
1072 conn->tmabort_timer.expires = 10*HZ + jiffies;
1073 conn->tmabort_timer.function = iscsi_tmabort_timedout;
1074 conn->tmabort_timer.data = (unsigned long)ctask;
1075 add_timer(&conn->tmabort_timer);
Pete Wyckoffd6e24d12006-11-08 15:58:32 -06001076 debug_scsi("abort set timeout [itt 0x%x]\n", ctask->itt);
Mike Christie7996a772006-04-06 21:13:41 -05001077 }
1078 spin_unlock_bh(&session->lock);
1079 mutex_unlock(&conn->xmitmutex);
1080
1081 /*
1082 * block eh thread until:
1083 *
1084 * 1) abort response
1085 * 2) abort timeout
1086 * 3) session is terminated or restarted or userspace has
1087 * given up on recovery
1088 */
1089 wait_event_interruptible(conn->ehwait,
1090 sc->SCp.phase != session->age ||
1091 session->state != ISCSI_STATE_LOGGED_IN ||
Mike Christie656cffc2006-05-18 20:31:42 -05001092 conn->tmabort_state != TMABORT_INITIAL);
Mike Christie7996a772006-04-06 21:13:41 -05001093 if (signal_pending(current))
1094 flush_signals(current);
1095 del_timer_sync(&conn->tmabort_timer);
1096
1097 mutex_lock(&conn->xmitmutex);
1098 return 0;
1099}
1100
1101/*
1102 * xmit mutex and session lock must be held
1103 */
Mike Christieb6c395e2006-07-24 15:47:15 -05001104static struct iscsi_mgmt_task *
1105iscsi_remove_mgmt_task(struct kfifo *fifo, uint32_t itt)
1106{
1107 int i, nr_tasks = __kfifo_len(fifo) / sizeof(void*);
1108 struct iscsi_mgmt_task *task;
Mike Christie7996a772006-04-06 21:13:41 -05001109
Mike Christieb6c395e2006-07-24 15:47:15 -05001110 debug_scsi("searching %d tasks\n", nr_tasks);
1111
1112 for (i = 0; i < nr_tasks; i++) {
1113 __kfifo_get(fifo, (void*)&task, sizeof(void*));
1114 debug_scsi("check task %u\n", task->itt);
1115
1116 if (task->itt == itt) {
1117 debug_scsi("matched task\n");
1118 return task;
1119 }
1120
1121 __kfifo_put(fifo, (void*)&task, sizeof(void*));
1122 }
1123 return NULL;
1124}
Mike Christie7996a772006-04-06 21:13:41 -05001125
1126static int iscsi_ctask_mtask_cleanup(struct iscsi_cmd_task *ctask)
1127{
1128 struct iscsi_conn *conn = ctask->conn;
1129 struct iscsi_session *session = conn->session;
1130
1131 if (!ctask->mtask)
1132 return -EINVAL;
1133
1134 if (!iscsi_remove_mgmt_task(conn->immqueue, ctask->mtask->itt))
1135 list_del(&ctask->mtask->running);
1136 __kfifo_put(session->mgmtpool.queue, (void*)&ctask->mtask,
1137 sizeof(void*));
1138 ctask->mtask = NULL;
1139 return 0;
1140}
1141
1142/*
1143 * session lock and xmitmutex must be held
1144 */
1145static void fail_command(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
1146 int err)
1147{
1148 struct scsi_cmnd *sc;
1149
Mike Christie7996a772006-04-06 21:13:41 -05001150 sc = ctask->sc;
1151 if (!sc)
1152 return;
Mike Christiee648f632006-08-31 18:09:34 -04001153
1154 conn->session->tt->cleanup_cmd_task(conn, ctask);
Mike Christie7ea8b822006-07-24 15:47:22 -05001155 iscsi_ctask_mtask_cleanup(ctask);
1156
Mike Christie7996a772006-04-06 21:13:41 -05001157 sc->result = err;
1158 sc->resid = sc->request_bufflen;
Mike Christiee648f632006-08-31 18:09:34 -04001159 /* release ref from queuecommand */
Mike Christie60ecebf2006-08-31 18:09:25 -04001160 __iscsi_put_ctask(ctask);
Mike Christie7996a772006-04-06 21:13:41 -05001161}
1162
1163int iscsi_eh_abort(struct scsi_cmnd *sc)
1164{
Mike Christief47f2cf2006-08-31 18:09:33 -04001165 struct iscsi_cmd_task *ctask;
1166 struct iscsi_conn *conn;
1167 struct iscsi_session *session;
Mike Christie7996a772006-04-06 21:13:41 -05001168 int rc;
1169
Mike Christief47f2cf2006-08-31 18:09:33 -04001170 /*
1171 * if session was ISCSI_STATE_IN_RECOVERY then we may not have
1172 * got the command.
1173 */
1174 if (!sc->SCp.ptr) {
1175 debug_scsi("sc never reached iscsi layer or it completed.\n");
1176 return SUCCESS;
1177 }
1178
1179 ctask = (struct iscsi_cmd_task *)sc->SCp.ptr;
1180 conn = ctask->conn;
1181 session = conn->session;
1182
Mike Christie7996a772006-04-06 21:13:41 -05001183 conn->eh_abort_cnt++;
1184 debug_scsi("aborting [sc %p itt 0x%x]\n", sc, ctask->itt);
1185
1186 mutex_lock(&conn->xmitmutex);
1187 spin_lock_bh(&session->lock);
1188
1189 /*
1190 * If we are not logged in or we have started a new session
1191 * then let the host reset code handle this
1192 */
1193 if (session->state != ISCSI_STATE_LOGGED_IN ||
1194 sc->SCp.phase != session->age)
1195 goto failed;
1196
1197 /* ctask completed before time out */
Mike Christie7ea8b822006-07-24 15:47:22 -05001198 if (!ctask->sc) {
1199 spin_unlock_bh(&session->lock);
1200 debug_scsi("sc completed while abort in progress\n");
1201 goto success_rel_mutex;
1202 }
Mike Christie7996a772006-04-06 21:13:41 -05001203
1204 /* what should we do here ? */
1205 if (conn->ctask == ctask) {
Or Gerlitzbe2df722006-05-02 19:46:43 -05001206 printk(KERN_INFO "iscsi: sc %p itt 0x%x partially sent. "
1207 "Failing abort\n", sc, ctask->itt);
Mike Christie7996a772006-04-06 21:13:41 -05001208 goto failed;
1209 }
1210
Mike Christieb6c395e2006-07-24 15:47:15 -05001211 if (ctask->state == ISCSI_TASK_PENDING)
Mike Christie7ea8b822006-07-24 15:47:22 -05001212 goto success_cleanup;
Mike Christie7996a772006-04-06 21:13:41 -05001213
1214 conn->tmabort_state = TMABORT_INITIAL;
1215
1216 spin_unlock_bh(&session->lock);
1217 rc = iscsi_exec_abort_task(sc, ctask);
1218 spin_lock_bh(&session->lock);
1219
Mike Christie7996a772006-04-06 21:13:41 -05001220 if (rc || sc->SCp.phase != session->age ||
1221 session->state != ISCSI_STATE_LOGGED_IN)
1222 goto failed;
Mike Christie7ea8b822006-07-24 15:47:22 -05001223 iscsi_ctask_mtask_cleanup(ctask);
Mike Christie7996a772006-04-06 21:13:41 -05001224
Mike Christie7ea8b822006-07-24 15:47:22 -05001225 switch (conn->tmabort_state) {
1226 case TMABORT_SUCCESS:
1227 goto success_cleanup;
1228 case TMABORT_NOT_FOUND:
1229 if (!ctask->sc) {
1230 /* ctask completed before tmf abort response */
1231 spin_unlock_bh(&session->lock);
1232 debug_scsi("sc completed while abort in progress\n");
1233 goto success_rel_mutex;
1234 }
1235 /* fall through */
1236 default:
1237 /* timedout or failed */
Mike Christie7996a772006-04-06 21:13:41 -05001238 spin_unlock_bh(&session->lock);
1239 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1240 spin_lock_bh(&session->lock);
1241 goto failed;
1242 }
1243
Mike Christie7ea8b822006-07-24 15:47:22 -05001244success_cleanup:
Mike Christie7996a772006-04-06 21:13:41 -05001245 debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
1246 spin_unlock_bh(&session->lock);
1247
1248 /*
1249 * clean up task if aborted. we have the xmitmutex so grab
1250 * the recv lock as a writer
1251 */
1252 write_lock_bh(conn->recv_lock);
1253 spin_lock(&session->lock);
1254 fail_command(conn, ctask, DID_ABORT << 16);
1255 spin_unlock(&session->lock);
1256 write_unlock_bh(conn->recv_lock);
1257
Mike Christie7ea8b822006-07-24 15:47:22 -05001258success_rel_mutex:
Mike Christie7996a772006-04-06 21:13:41 -05001259 mutex_unlock(&conn->xmitmutex);
1260 return SUCCESS;
1261
1262failed:
1263 spin_unlock_bh(&session->lock);
1264 mutex_unlock(&conn->xmitmutex);
1265
1266 debug_scsi("abort failed [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
1267 return FAILED;
1268}
1269EXPORT_SYMBOL_GPL(iscsi_eh_abort);
1270
1271int
1272iscsi_pool_init(struct iscsi_queue *q, int max, void ***items, int item_size)
1273{
1274 int i;
1275
1276 *items = kmalloc(max * sizeof(void*), GFP_KERNEL);
1277 if (*items == NULL)
1278 return -ENOMEM;
1279
1280 q->max = max;
1281 q->pool = kmalloc(max * sizeof(void*), GFP_KERNEL);
1282 if (q->pool == NULL) {
1283 kfree(*items);
1284 return -ENOMEM;
1285 }
1286
1287 q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
1288 GFP_KERNEL, NULL);
1289 if (q->queue == ERR_PTR(-ENOMEM)) {
1290 kfree(q->pool);
1291 kfree(*items);
1292 return -ENOMEM;
1293 }
1294
1295 for (i = 0; i < max; i++) {
1296 q->pool[i] = kmalloc(item_size, GFP_KERNEL);
1297 if (q->pool[i] == NULL) {
1298 int j;
1299
1300 for (j = 0; j < i; j++)
1301 kfree(q->pool[j]);
1302
1303 kfifo_free(q->queue);
1304 kfree(q->pool);
1305 kfree(*items);
1306 return -ENOMEM;
1307 }
1308 memset(q->pool[i], 0, item_size);
1309 (*items)[i] = q->pool[i];
1310 __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
1311 }
1312 return 0;
1313}
1314EXPORT_SYMBOL_GPL(iscsi_pool_init);
1315
1316void iscsi_pool_free(struct iscsi_queue *q, void **items)
1317{
1318 int i;
1319
1320 for (i = 0; i < q->max; i++)
1321 kfree(items[i]);
1322 kfree(q->pool);
1323 kfree(items);
1324}
1325EXPORT_SYMBOL_GPL(iscsi_pool_free);
1326
1327/*
1328 * iSCSI Session's hostdata organization:
1329 *
1330 * *------------------* <== hostdata_session(host->hostdata)
1331 * | ptr to class sess|
1332 * |------------------| <== iscsi_hostdata(host->hostdata)
1333 * | iscsi_session |
1334 * *------------------*
1335 */
1336
1337#define hostdata_privsize(_sz) (sizeof(unsigned long) + _sz + \
1338 _sz % sizeof(unsigned long))
1339
1340#define hostdata_session(_hostdata) (iscsi_ptr(*(unsigned long *)_hostdata))
1341
1342/**
1343 * iscsi_session_setup - create iscsi cls session and host and session
1344 * @scsit: scsi transport template
1345 * @iscsit: iscsi transport template
1346 * @initial_cmdsn: initial CmdSN
1347 * @hostno: host no allocated
1348 *
1349 * This can be used by software iscsi_transports that allocate
1350 * a session per scsi host.
1351 **/
1352struct iscsi_cls_session *
1353iscsi_session_setup(struct iscsi_transport *iscsit,
1354 struct scsi_transport_template *scsit,
1355 int cmd_task_size, int mgmt_task_size,
1356 uint32_t initial_cmdsn, uint32_t *hostno)
1357{
1358 struct Scsi_Host *shost;
1359 struct iscsi_session *session;
1360 struct iscsi_cls_session *cls_session;
1361 int cmd_i;
1362
1363 shost = scsi_host_alloc(iscsit->host_template,
1364 hostdata_privsize(sizeof(*session)));
1365 if (!shost)
1366 return NULL;
1367
1368 shost->max_id = 1;
1369 shost->max_channel = 0;
1370 shost->max_lun = iscsit->max_lun;
1371 shost->max_cmd_len = iscsit->max_cmd_len;
1372 shost->transportt = scsit;
1373 shost->transportt->create_work_queue = 1;
1374 *hostno = shost->host_no;
1375
1376 session = iscsi_hostdata(shost->hostdata);
1377 memset(session, 0, sizeof(struct iscsi_session));
1378 session->host = shost;
1379 session->state = ISCSI_STATE_FREE;
1380 session->mgmtpool_max = ISCSI_MGMT_CMDS_MAX;
1381 session->cmds_max = ISCSI_XMIT_CMDS_MAX;
1382 session->cmdsn = initial_cmdsn;
1383 session->exp_cmdsn = initial_cmdsn + 1;
1384 session->max_cmdsn = initial_cmdsn + 1;
1385 session->max_r2t = 1;
1386 session->tt = iscsit;
1387
1388 /* initialize SCSI PDU commands pool */
1389 if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
1390 (void***)&session->cmds,
1391 cmd_task_size + sizeof(struct iscsi_cmd_task)))
1392 goto cmdpool_alloc_fail;
1393
1394 /* pre-format cmds pool with ITT */
1395 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
1396 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
1397
1398 if (cmd_task_size)
1399 ctask->dd_data = &ctask[1];
1400 ctask->itt = cmd_i;
Mike Christieb6c395e2006-07-24 15:47:15 -05001401 INIT_LIST_HEAD(&ctask->running);
Mike Christie7996a772006-04-06 21:13:41 -05001402 }
1403
1404 spin_lock_init(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001405
1406 /* initialize immediate command pool */
1407 if (iscsi_pool_init(&session->mgmtpool, session->mgmtpool_max,
1408 (void***)&session->mgmt_cmds,
1409 mgmt_task_size + sizeof(struct iscsi_mgmt_task)))
1410 goto mgmtpool_alloc_fail;
1411
1412
1413 /* pre-format immediate cmds pool with ITT */
1414 for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
1415 struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
1416
1417 if (mgmt_task_size)
1418 mtask->dd_data = &mtask[1];
1419 mtask->itt = ISCSI_MGMT_ITT_OFFSET + cmd_i;
Mike Christieb6c395e2006-07-24 15:47:15 -05001420 INIT_LIST_HEAD(&mtask->running);
Mike Christie7996a772006-04-06 21:13:41 -05001421 }
1422
1423 if (scsi_add_host(shost, NULL))
1424 goto add_host_fail;
1425
Mike Christief53a88d2006-06-28 12:00:27 -05001426 if (!try_module_get(iscsit->owner))
1427 goto cls_session_fail;
1428
Mike Christie6a8a0d32006-06-28 12:00:31 -05001429 cls_session = iscsi_create_session(shost, iscsit, 0);
Mike Christie7996a772006-04-06 21:13:41 -05001430 if (!cls_session)
Mike Christief53a88d2006-06-28 12:00:27 -05001431 goto module_put;
Mike Christie7996a772006-04-06 21:13:41 -05001432 *(unsigned long*)shost->hostdata = (unsigned long)cls_session;
1433
1434 return cls_session;
1435
Mike Christief53a88d2006-06-28 12:00:27 -05001436module_put:
1437 module_put(iscsit->owner);
Mike Christie7996a772006-04-06 21:13:41 -05001438cls_session_fail:
1439 scsi_remove_host(shost);
1440add_host_fail:
Mike Christie7996a772006-04-06 21:13:41 -05001441 iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
1442mgmtpool_alloc_fail:
1443 iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
1444cmdpool_alloc_fail:
1445 scsi_host_put(shost);
1446 return NULL;
1447}
1448EXPORT_SYMBOL_GPL(iscsi_session_setup);
1449
1450/**
1451 * iscsi_session_teardown - destroy session, host, and cls_session
1452 * shost: scsi host
1453 *
1454 * This can be used by software iscsi_transports that allocate
1455 * a session per scsi host.
1456 **/
1457void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
1458{
1459 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
1460 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
Mike Christie63f75cc2006-07-24 15:47:29 -05001461 struct module *owner = cls_session->transport->owner;
Mike Christie7996a772006-04-06 21:13:41 -05001462
1463 scsi_remove_host(shost);
1464
Mike Christie7996a772006-04-06 21:13:41 -05001465 iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
1466 iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
1467
Mike Christief3ff0c32006-07-24 15:47:50 -05001468 kfree(session->targetname);
1469
Mike Christie7996a772006-04-06 21:13:41 -05001470 iscsi_destroy_session(cls_session);
1471 scsi_host_put(shost);
Mike Christie63f75cc2006-07-24 15:47:29 -05001472 module_put(owner);
Mike Christie7996a772006-04-06 21:13:41 -05001473}
1474EXPORT_SYMBOL_GPL(iscsi_session_teardown);
1475
1476/**
1477 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
1478 * @cls_session: iscsi_cls_session
1479 * @conn_idx: cid
1480 **/
1481struct iscsi_cls_conn *
1482iscsi_conn_setup(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
1483{
1484 struct iscsi_session *session = class_to_transport_session(cls_session);
1485 struct iscsi_conn *conn;
1486 struct iscsi_cls_conn *cls_conn;
Mike Christied36ab6f2006-05-18 20:31:34 -05001487 char *data;
Mike Christie7996a772006-04-06 21:13:41 -05001488
1489 cls_conn = iscsi_create_conn(cls_session, conn_idx);
1490 if (!cls_conn)
1491 return NULL;
1492 conn = cls_conn->dd_data;
1493 memset(conn, 0, sizeof(*conn));
1494
1495 conn->session = session;
1496 conn->cls_conn = cls_conn;
1497 conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
1498 conn->id = conn_idx;
1499 conn->exp_statsn = 0;
1500 conn->tmabort_state = TMABORT_INITIAL;
1501 INIT_LIST_HEAD(&conn->run_list);
1502 INIT_LIST_HEAD(&conn->mgmt_run_list);
Mike Christieb6c395e2006-07-24 15:47:15 -05001503 INIT_LIST_HEAD(&conn->xmitqueue);
Mike Christie7996a772006-04-06 21:13:41 -05001504
1505 /* initialize general immediate & non-immediate PDU commands queue */
1506 conn->immqueue = kfifo_alloc(session->mgmtpool_max * sizeof(void*),
1507 GFP_KERNEL, NULL);
1508 if (conn->immqueue == ERR_PTR(-ENOMEM))
1509 goto immqueue_alloc_fail;
1510
1511 conn->mgmtqueue = kfifo_alloc(session->mgmtpool_max * sizeof(void*),
1512 GFP_KERNEL, NULL);
1513 if (conn->mgmtqueue == ERR_PTR(-ENOMEM))
1514 goto mgmtqueue_alloc_fail;
1515
David Howellsc4028952006-11-22 14:57:56 +00001516 INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
Mike Christie7996a772006-04-06 21:13:41 -05001517
1518 /* allocate login_mtask used for the login/text sequences */
1519 spin_lock_bh(&session->lock);
1520 if (!__kfifo_get(session->mgmtpool.queue,
1521 (void*)&conn->login_mtask,
1522 sizeof(void*))) {
1523 spin_unlock_bh(&session->lock);
1524 goto login_mtask_alloc_fail;
1525 }
1526 spin_unlock_bh(&session->lock);
1527
Mike Christied36ab6f2006-05-18 20:31:34 -05001528 data = kmalloc(DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH, GFP_KERNEL);
1529 if (!data)
1530 goto login_mtask_data_alloc_fail;
Mike Christiec8dc1e52006-07-24 15:47:39 -05001531 conn->login_mtask->data = conn->data = data;
Mike Christied36ab6f2006-05-18 20:31:34 -05001532
Mike Christie7996a772006-04-06 21:13:41 -05001533 init_timer(&conn->tmabort_timer);
1534 mutex_init(&conn->xmitmutex);
1535 init_waitqueue_head(&conn->ehwait);
1536
1537 return cls_conn;
1538
Mike Christied36ab6f2006-05-18 20:31:34 -05001539login_mtask_data_alloc_fail:
1540 __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
1541 sizeof(void*));
Mike Christie7996a772006-04-06 21:13:41 -05001542login_mtask_alloc_fail:
1543 kfifo_free(conn->mgmtqueue);
1544mgmtqueue_alloc_fail:
1545 kfifo_free(conn->immqueue);
1546immqueue_alloc_fail:
Mike Christie7996a772006-04-06 21:13:41 -05001547 iscsi_destroy_conn(cls_conn);
1548 return NULL;
1549}
1550EXPORT_SYMBOL_GPL(iscsi_conn_setup);
1551
1552/**
1553 * iscsi_conn_teardown - teardown iscsi connection
1554 * cls_conn: iscsi class connection
1555 *
1556 * TODO: we may need to make this into a two step process
1557 * like scsi-mls remove + put host
1558 */
1559void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
1560{
1561 struct iscsi_conn *conn = cls_conn->dd_data;
1562 struct iscsi_session *session = conn->session;
1563 unsigned long flags;
1564
Mike Christie7996a772006-04-06 21:13:41 -05001565 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
Mike Christie67a61112006-05-30 00:37:20 -05001566 mutex_lock(&conn->xmitmutex);
Mike Christie7996a772006-04-06 21:13:41 -05001567
1568 spin_lock_bh(&session->lock);
1569 conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
1570 if (session->leadconn == conn) {
1571 /*
1572 * leading connection? then give up on recovery.
1573 */
1574 session->state = ISCSI_STATE_TERMINATE;
1575 wake_up(&conn->ehwait);
1576 }
1577 spin_unlock_bh(&session->lock);
1578
1579 mutex_unlock(&conn->xmitmutex);
1580
1581 /*
1582 * Block until all in-progress commands for this connection
1583 * time out or fail.
1584 */
1585 for (;;) {
1586 spin_lock_irqsave(session->host->host_lock, flags);
1587 if (!session->host->host_busy) { /* OK for ERL == 0 */
1588 spin_unlock_irqrestore(session->host->host_lock, flags);
1589 break;
1590 }
1591 spin_unlock_irqrestore(session->host->host_lock, flags);
1592 msleep_interruptible(500);
Or Gerlitzbe2df722006-05-02 19:46:43 -05001593 printk(KERN_INFO "iscsi: scsi conn_destroy(): host_busy %d "
1594 "host_failed %d\n", session->host->host_busy,
1595 session->host->host_failed);
Mike Christie7996a772006-04-06 21:13:41 -05001596 /*
1597 * force eh_abort() to unblock
1598 */
1599 wake_up(&conn->ehwait);
1600 }
1601
1602 spin_lock_bh(&session->lock);
Mike Christiec8dc1e52006-07-24 15:47:39 -05001603 kfree(conn->data);
Mike Christief3ff0c32006-07-24 15:47:50 -05001604 kfree(conn->persistent_address);
Mike Christie7996a772006-04-06 21:13:41 -05001605 __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
1606 sizeof(void*));
Mike Christie98644042006-10-16 18:09:39 -04001607 if (session->leadconn == conn) {
Mike Christie7996a772006-04-06 21:13:41 -05001608 session->leadconn = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05001609 /* no connections exits.. reset sequencing */
1610 session->cmdsn = session->max_cmdsn = session->exp_cmdsn = 1;
Mike Christie98644042006-10-16 18:09:39 -04001611 }
Mike Christie7996a772006-04-06 21:13:41 -05001612 spin_unlock_bh(&session->lock);
1613
Mike Christie7996a772006-04-06 21:13:41 -05001614 kfifo_free(conn->immqueue);
1615 kfifo_free(conn->mgmtqueue);
1616
1617 iscsi_destroy_conn(cls_conn);
1618}
1619EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
1620
1621int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
1622{
1623 struct iscsi_conn *conn = cls_conn->dd_data;
1624 struct iscsi_session *session = conn->session;
1625
Mike Christieffd04362006-08-31 18:09:24 -04001626 if (!session) {
Mike Christie7996a772006-04-06 21:13:41 -05001627 printk(KERN_ERR "iscsi: can't start unbound connection\n");
1628 return -EPERM;
1629 }
1630
Mike Christiedb98ccd2006-08-31 18:09:31 -04001631 if ((session->imm_data_en || !session->initial_r2t_en) &&
1632 session->first_burst > session->max_burst) {
Mike Christieffd04362006-08-31 18:09:24 -04001633 printk("iscsi: invalid burst lengths: "
1634 "first_burst %d max_burst %d\n",
1635 session->first_burst, session->max_burst);
1636 return -EINVAL;
1637 }
1638
Mike Christie7996a772006-04-06 21:13:41 -05001639 spin_lock_bh(&session->lock);
1640 conn->c_stage = ISCSI_CONN_STARTED;
1641 session->state = ISCSI_STATE_LOGGED_IN;
1642
1643 switch(conn->stop_stage) {
1644 case STOP_CONN_RECOVER:
1645 /*
1646 * unblock eh_abort() if it is blocked. re-try all
1647 * commands after successful recovery
1648 */
Mike Christie7996a772006-04-06 21:13:41 -05001649 conn->stop_stage = 0;
1650 conn->tmabort_state = TMABORT_INITIAL;
1651 session->age++;
Mike Christie7996a772006-04-06 21:13:41 -05001652 spin_unlock_bh(&session->lock);
1653
1654 iscsi_unblock_session(session_to_cls(session));
1655 wake_up(&conn->ehwait);
1656 return 0;
1657 case STOP_CONN_TERM:
Mike Christie7996a772006-04-06 21:13:41 -05001658 conn->stop_stage = 0;
1659 break;
Mike Christie7996a772006-04-06 21:13:41 -05001660 default:
1661 break;
1662 }
1663 spin_unlock_bh(&session->lock);
1664
1665 return 0;
1666}
1667EXPORT_SYMBOL_GPL(iscsi_conn_start);
1668
1669static void
1670flush_control_queues(struct iscsi_session *session, struct iscsi_conn *conn)
1671{
1672 struct iscsi_mgmt_task *mtask, *tmp;
1673
1674 /* handle pending */
1675 while (__kfifo_get(conn->immqueue, (void*)&mtask, sizeof(void*)) ||
1676 __kfifo_get(conn->mgmtqueue, (void*)&mtask, sizeof(void*))) {
1677 if (mtask == conn->login_mtask)
1678 continue;
1679 debug_scsi("flushing pending mgmt task itt 0x%x\n", mtask->itt);
1680 __kfifo_put(session->mgmtpool.queue, (void*)&mtask,
1681 sizeof(void*));
1682 }
1683
1684 /* handle running */
1685 list_for_each_entry_safe(mtask, tmp, &conn->mgmt_run_list, running) {
1686 debug_scsi("flushing running mgmt task itt 0x%x\n", mtask->itt);
Mike Christieed2abc72006-05-02 19:46:40 -05001687 list_del(&mtask->running);
1688
Mike Christie7996a772006-04-06 21:13:41 -05001689 if (mtask == conn->login_mtask)
1690 continue;
Mike Christieed2abc72006-05-02 19:46:40 -05001691 __kfifo_put(session->mgmtpool.queue, (void*)&mtask,
Mike Christie7996a772006-04-06 21:13:41 -05001692 sizeof(void*));
1693 }
1694
1695 conn->mtask = NULL;
1696}
1697
1698/* Fail commands. Mutex and session lock held and recv side suspended */
1699static void fail_all_commands(struct iscsi_conn *conn)
1700{
1701 struct iscsi_cmd_task *ctask, *tmp;
1702
1703 /* flush pending */
Mike Christieb6c395e2006-07-24 15:47:15 -05001704 list_for_each_entry_safe(ctask, tmp, &conn->xmitqueue, running) {
Mike Christie7996a772006-04-06 21:13:41 -05001705 debug_scsi("failing pending sc %p itt 0x%x\n", ctask->sc,
1706 ctask->itt);
1707 fail_command(conn, ctask, DID_BUS_BUSY << 16);
1708 }
1709
1710 /* fail all other running */
1711 list_for_each_entry_safe(ctask, tmp, &conn->run_list, running) {
1712 debug_scsi("failing in progress sc %p itt 0x%x\n",
1713 ctask->sc, ctask->itt);
1714 fail_command(conn, ctask, DID_BUS_BUSY << 16);
1715 }
1716
1717 conn->ctask = NULL;
1718}
1719
Mike Christie656cffc2006-05-18 20:31:42 -05001720static void iscsi_start_session_recovery(struct iscsi_session *session,
1721 struct iscsi_conn *conn, int flag)
Mike Christie7996a772006-04-06 21:13:41 -05001722{
Mike Christieed2abc72006-05-02 19:46:40 -05001723 int old_stop_stage;
1724
Mike Christie7996a772006-04-06 21:13:41 -05001725 spin_lock_bh(&session->lock);
Mike Christieed2abc72006-05-02 19:46:40 -05001726 if (conn->stop_stage == STOP_CONN_TERM) {
Mike Christie7996a772006-04-06 21:13:41 -05001727 spin_unlock_bh(&session->lock);
1728 return;
1729 }
Mike Christieed2abc72006-05-02 19:46:40 -05001730
1731 /*
1732 * When this is called for the in_login state, we only want to clean
Mike Christie67a61112006-05-30 00:37:20 -05001733 * up the login task and connection. We do not need to block and set
1734 * the recovery state again
Mike Christieed2abc72006-05-02 19:46:40 -05001735 */
Mike Christie67a61112006-05-30 00:37:20 -05001736 if (flag == STOP_CONN_TERM)
1737 session->state = ISCSI_STATE_TERMINATE;
1738 else if (conn->stop_stage != STOP_CONN_RECOVER)
1739 session->state = ISCSI_STATE_IN_RECOVERY;
Mike Christieed2abc72006-05-02 19:46:40 -05001740
1741 old_stop_stage = conn->stop_stage;
Mike Christie7996a772006-04-06 21:13:41 -05001742 conn->stop_stage = flag;
Mike Christie67a61112006-05-30 00:37:20 -05001743 conn->c_stage = ISCSI_CONN_STOPPED;
1744 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
Mike Christie7996a772006-04-06 21:13:41 -05001745 spin_unlock_bh(&session->lock);
1746
Mike Christie1c834692006-07-24 15:47:26 -05001747 write_lock_bh(conn->recv_lock);
1748 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
1749 write_unlock_bh(conn->recv_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001750
1751 mutex_lock(&conn->xmitmutex);
Mike Christie7996a772006-04-06 21:13:41 -05001752 /*
1753 * for connection level recovery we should not calculate
1754 * header digest. conn->hdr_size used for optimization
1755 * in hdr_extract() and will be re-negotiated at
1756 * set_param() time.
1757 */
1758 if (flag == STOP_CONN_RECOVER) {
1759 conn->hdrdgst_en = 0;
1760 conn->datadgst_en = 0;
Mike Christie656cffc2006-05-18 20:31:42 -05001761 if (session->state == ISCSI_STATE_IN_RECOVERY &&
Mike Christie67a61112006-05-30 00:37:20 -05001762 old_stop_stage != STOP_CONN_RECOVER) {
1763 debug_scsi("blocking session\n");
Mike Christie7996a772006-04-06 21:13:41 -05001764 iscsi_block_session(session_to_cls(session));
Mike Christie67a61112006-05-30 00:37:20 -05001765 }
Mike Christie7996a772006-04-06 21:13:41 -05001766 }
Mike Christie656cffc2006-05-18 20:31:42 -05001767
Mike Christie656cffc2006-05-18 20:31:42 -05001768 /*
1769 * flush queues.
1770 */
1771 spin_lock_bh(&session->lock);
1772 fail_all_commands(conn);
1773 flush_control_queues(session, conn);
1774 spin_unlock_bh(&session->lock);
1775
Mike Christie7996a772006-04-06 21:13:41 -05001776 mutex_unlock(&conn->xmitmutex);
1777}
Mike Christie7996a772006-04-06 21:13:41 -05001778
1779void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
1780{
1781 struct iscsi_conn *conn = cls_conn->dd_data;
1782 struct iscsi_session *session = conn->session;
1783
1784 switch (flag) {
1785 case STOP_CONN_RECOVER:
1786 case STOP_CONN_TERM:
1787 iscsi_start_session_recovery(session, conn, flag);
Mike Christie8d2860b2006-05-02 19:46:47 -05001788 break;
Mike Christie7996a772006-04-06 21:13:41 -05001789 default:
Or Gerlitzbe2df722006-05-02 19:46:43 -05001790 printk(KERN_ERR "iscsi: invalid stop flag %d\n", flag);
Mike Christie7996a772006-04-06 21:13:41 -05001791 }
1792}
1793EXPORT_SYMBOL_GPL(iscsi_conn_stop);
1794
1795int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
1796 struct iscsi_cls_conn *cls_conn, int is_leading)
1797{
1798 struct iscsi_session *session = class_to_transport_session(cls_session);
Mike Christie98644042006-10-16 18:09:39 -04001799 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05001800
Mike Christie7996a772006-04-06 21:13:41 -05001801 spin_lock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001802 if (is_leading)
1803 session->leadconn = conn;
Mike Christie98644042006-10-16 18:09:39 -04001804 spin_unlock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001805
1806 /*
1807 * Unblock xmitworker(), Login Phase will pass through.
1808 */
1809 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
1810 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1811 return 0;
1812}
1813EXPORT_SYMBOL_GPL(iscsi_conn_bind);
1814
Mike Christiea54a52c2006-06-28 12:00:23 -05001815
1816int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
1817 enum iscsi_param param, char *buf, int buflen)
1818{
1819 struct iscsi_conn *conn = cls_conn->dd_data;
1820 struct iscsi_session *session = conn->session;
1821 uint32_t value;
1822
1823 switch(param) {
1824 case ISCSI_PARAM_MAX_RECV_DLENGTH:
1825 sscanf(buf, "%d", &conn->max_recv_dlength);
1826 break;
1827 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
1828 sscanf(buf, "%d", &conn->max_xmit_dlength);
1829 break;
1830 case ISCSI_PARAM_HDRDGST_EN:
1831 sscanf(buf, "%d", &conn->hdrdgst_en);
1832 break;
1833 case ISCSI_PARAM_DATADGST_EN:
1834 sscanf(buf, "%d", &conn->datadgst_en);
1835 break;
1836 case ISCSI_PARAM_INITIAL_R2T_EN:
1837 sscanf(buf, "%d", &session->initial_r2t_en);
1838 break;
1839 case ISCSI_PARAM_MAX_R2T:
1840 sscanf(buf, "%d", &session->max_r2t);
1841 break;
1842 case ISCSI_PARAM_IMM_DATA_EN:
1843 sscanf(buf, "%d", &session->imm_data_en);
1844 break;
1845 case ISCSI_PARAM_FIRST_BURST:
1846 sscanf(buf, "%d", &session->first_burst);
1847 break;
1848 case ISCSI_PARAM_MAX_BURST:
1849 sscanf(buf, "%d", &session->max_burst);
1850 break;
1851 case ISCSI_PARAM_PDU_INORDER_EN:
1852 sscanf(buf, "%d", &session->pdu_inorder_en);
1853 break;
1854 case ISCSI_PARAM_DATASEQ_INORDER_EN:
1855 sscanf(buf, "%d", &session->dataseq_inorder_en);
1856 break;
1857 case ISCSI_PARAM_ERL:
1858 sscanf(buf, "%d", &session->erl);
1859 break;
1860 case ISCSI_PARAM_IFMARKER_EN:
1861 sscanf(buf, "%d", &value);
1862 BUG_ON(value);
1863 break;
1864 case ISCSI_PARAM_OFMARKER_EN:
1865 sscanf(buf, "%d", &value);
1866 BUG_ON(value);
1867 break;
1868 case ISCSI_PARAM_EXP_STATSN:
1869 sscanf(buf, "%u", &conn->exp_statsn);
1870 break;
1871 case ISCSI_PARAM_TARGET_NAME:
1872 /* this should not change between logins */
1873 if (session->targetname)
1874 break;
1875
1876 session->targetname = kstrdup(buf, GFP_KERNEL);
1877 if (!session->targetname)
1878 return -ENOMEM;
1879 break;
1880 case ISCSI_PARAM_TPGT:
1881 sscanf(buf, "%d", &session->tpgt);
1882 break;
1883 case ISCSI_PARAM_PERSISTENT_PORT:
1884 sscanf(buf, "%d", &conn->persistent_port);
1885 break;
1886 case ISCSI_PARAM_PERSISTENT_ADDRESS:
1887 /*
1888 * this is the address returned in discovery so it should
1889 * not change between logins.
1890 */
1891 if (conn->persistent_address)
1892 break;
1893
1894 conn->persistent_address = kstrdup(buf, GFP_KERNEL);
1895 if (!conn->persistent_address)
1896 return -ENOMEM;
1897 break;
1898 default:
1899 return -ENOSYS;
1900 }
1901
1902 return 0;
1903}
1904EXPORT_SYMBOL_GPL(iscsi_set_param);
1905
1906int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
1907 enum iscsi_param param, char *buf)
1908{
1909 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
1910 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
1911 int len;
1912
1913 switch(param) {
1914 case ISCSI_PARAM_INITIAL_R2T_EN:
1915 len = sprintf(buf, "%d\n", session->initial_r2t_en);
1916 break;
1917 case ISCSI_PARAM_MAX_R2T:
1918 len = sprintf(buf, "%hu\n", session->max_r2t);
1919 break;
1920 case ISCSI_PARAM_IMM_DATA_EN:
1921 len = sprintf(buf, "%d\n", session->imm_data_en);
1922 break;
1923 case ISCSI_PARAM_FIRST_BURST:
1924 len = sprintf(buf, "%u\n", session->first_burst);
1925 break;
1926 case ISCSI_PARAM_MAX_BURST:
1927 len = sprintf(buf, "%u\n", session->max_burst);
1928 break;
1929 case ISCSI_PARAM_PDU_INORDER_EN:
1930 len = sprintf(buf, "%d\n", session->pdu_inorder_en);
1931 break;
1932 case ISCSI_PARAM_DATASEQ_INORDER_EN:
1933 len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
1934 break;
1935 case ISCSI_PARAM_ERL:
1936 len = sprintf(buf, "%d\n", session->erl);
1937 break;
1938 case ISCSI_PARAM_TARGET_NAME:
1939 len = sprintf(buf, "%s\n", session->targetname);
1940 break;
1941 case ISCSI_PARAM_TPGT:
1942 len = sprintf(buf, "%d\n", session->tpgt);
1943 break;
1944 default:
1945 return -ENOSYS;
1946 }
1947
1948 return len;
1949}
1950EXPORT_SYMBOL_GPL(iscsi_session_get_param);
1951
1952int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
1953 enum iscsi_param param, char *buf)
1954{
1955 struct iscsi_conn *conn = cls_conn->dd_data;
1956 int len;
1957
1958 switch(param) {
1959 case ISCSI_PARAM_MAX_RECV_DLENGTH:
1960 len = sprintf(buf, "%u\n", conn->max_recv_dlength);
1961 break;
1962 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
1963 len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
1964 break;
1965 case ISCSI_PARAM_HDRDGST_EN:
1966 len = sprintf(buf, "%d\n", conn->hdrdgst_en);
1967 break;
1968 case ISCSI_PARAM_DATADGST_EN:
1969 len = sprintf(buf, "%d\n", conn->datadgst_en);
1970 break;
1971 case ISCSI_PARAM_IFMARKER_EN:
1972 len = sprintf(buf, "%d\n", conn->ifmarker_en);
1973 break;
1974 case ISCSI_PARAM_OFMARKER_EN:
1975 len = sprintf(buf, "%d\n", conn->ofmarker_en);
1976 break;
1977 case ISCSI_PARAM_EXP_STATSN:
1978 len = sprintf(buf, "%u\n", conn->exp_statsn);
1979 break;
1980 case ISCSI_PARAM_PERSISTENT_PORT:
1981 len = sprintf(buf, "%d\n", conn->persistent_port);
1982 break;
1983 case ISCSI_PARAM_PERSISTENT_ADDRESS:
1984 len = sprintf(buf, "%s\n", conn->persistent_address);
1985 break;
1986 default:
1987 return -ENOSYS;
1988 }
1989
1990 return len;
1991}
1992EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
1993
Mike Christie7996a772006-04-06 21:13:41 -05001994MODULE_AUTHOR("Mike Christie");
1995MODULE_DESCRIPTION("iSCSI library functions");
1996MODULE_LICENSE("GPL");