blob: 1000fe936791af76ee88a14b15dd425d0f551e06 [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 Christieb6c395ed2006-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) {
263 int senselen;
264
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
273 senselen = (data[0] << 8) | data[1];
274 if (datalen < senselen)
275 goto invalid_datalen;
276
277 memcpy(sc->sense_buffer, data + 2,
278 min(senselen, SCSI_SENSE_BUFFERSIZE));
279 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 Christie7996a772006-04-06 21:13:41 -0500484 /* we need sth like iscsi_async_event_rsp() */
485 rc = ISCSI_ERR_BAD_OPCODE;
486 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
581/**
582 * iscsi_data_xmit - xmit any command into the scheduled connection
583 * @conn: iscsi connection
584 *
585 * Notes:
586 * The function can return -EAGAIN in which case the caller must
587 * re-schedule it again later or recover. '0' return code means
588 * successful xmit.
589 **/
590static int iscsi_data_xmit(struct iscsi_conn *conn)
591{
592 struct iscsi_transport *tt;
Mike Christie3219e522006-05-30 00:37:28 -0500593 int rc = 0;
Mike Christie7996a772006-04-06 21:13:41 -0500594
595 if (unlikely(conn->suspend_tx)) {
596 debug_scsi("conn %d Tx suspended!\n", conn->id);
Mike Christie3219e522006-05-30 00:37:28 -0500597 return -ENODATA;
Mike Christie7996a772006-04-06 21:13:41 -0500598 }
599 tt = conn->session->tt;
600
601 /*
602 * Transmit in the following order:
603 *
604 * 1) un-finished xmit (ctask or mtask)
605 * 2) immediate control PDUs
606 * 3) write data
607 * 4) SCSI commands
608 * 5) non-immediate control PDUs
609 *
610 * No need to lock around __kfifo_get as long as
611 * there's one producer and one consumer.
612 */
613
614 BUG_ON(conn->ctask && conn->mtask);
615
616 if (conn->ctask) {
Mike Christie60ecebf2006-08-31 18:09:25 -0400617 iscsi_get_ctask(conn->ctask);
Mike Christie3219e522006-05-30 00:37:28 -0500618 rc = tt->xmit_cmd_task(conn, conn->ctask);
Mike Christie60ecebf2006-08-31 18:09:25 -0400619 iscsi_put_ctask(conn->ctask);
Mike Christie3219e522006-05-30 00:37:28 -0500620 if (rc)
Mike Christie7996a772006-04-06 21:13:41 -0500621 goto again;
622 /* done with this in-progress ctask */
623 conn->ctask = NULL;
624 }
625 if (conn->mtask) {
Mike Christie3219e522006-05-30 00:37:28 -0500626 rc = tt->xmit_mgmt_task(conn, conn->mtask);
627 if (rc)
Mike Christie7996a772006-04-06 21:13:41 -0500628 goto again;
629 /* done with this in-progress mtask */
630 conn->mtask = NULL;
631 }
632
633 /* process immediate first */
634 if (unlikely(__kfifo_len(conn->immqueue))) {
635 while (__kfifo_get(conn->immqueue, (void*)&conn->mtask,
636 sizeof(void*))) {
Mike Christie994442e2006-05-30 00:37:22 -0500637 spin_lock_bh(&conn->session->lock);
Mike Christie7996a772006-04-06 21:13:41 -0500638 list_add_tail(&conn->mtask->running,
639 &conn->mgmt_run_list);
Mike Christie994442e2006-05-30 00:37:22 -0500640 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -0500641 rc = tt->xmit_mgmt_task(conn, conn->mtask);
642 if (rc)
Mike Christie7996a772006-04-06 21:13:41 -0500643 goto again;
644 }
645 /* done with this mtask */
646 conn->mtask = NULL;
647 }
648
649 /* process command queue */
Mike Christieb6c395ed2006-07-24 15:47:15 -0500650 spin_lock_bh(&conn->session->lock);
651 while (!list_empty(&conn->xmitqueue)) {
Mike Christie7996a772006-04-06 21:13:41 -0500652 /*
653 * iscsi tcp may readd the task to the xmitqueue to send
654 * write data
655 */
Mike Christieb6c395ed2006-07-24 15:47:15 -0500656 conn->ctask = list_entry(conn->xmitqueue.next,
657 struct iscsi_cmd_task, running);
658 conn->ctask->state = ISCSI_TASK_RUNNING;
659 list_move_tail(conn->xmitqueue.next, &conn->run_list);
Mike Christie60ecebf2006-08-31 18:09:25 -0400660 __iscsi_get_ctask(conn->ctask);
Mike Christie994442e2006-05-30 00:37:22 -0500661 spin_unlock_bh(&conn->session->lock);
Mike Christieb6c395ed2006-07-24 15:47:15 -0500662
Mike Christie3219e522006-05-30 00:37:28 -0500663 rc = tt->xmit_cmd_task(conn, conn->ctask);
664 if (rc)
Mike Christie7996a772006-04-06 21:13:41 -0500665 goto again;
Mike Christie60ecebf2006-08-31 18:09:25 -0400666
Mike Christieb6c395ed2006-07-24 15:47:15 -0500667 spin_lock_bh(&conn->session->lock);
Mike Christie60ecebf2006-08-31 18:09:25 -0400668 __iscsi_put_ctask(conn->ctask);
669 if (rc) {
670 spin_unlock_bh(&conn->session->lock);
671 goto again;
672 }
Mike Christie7996a772006-04-06 21:13:41 -0500673 }
Mike Christieb6c395ed2006-07-24 15:47:15 -0500674 spin_unlock_bh(&conn->session->lock);
Mike Christie7996a772006-04-06 21:13:41 -0500675 /* done with this ctask */
676 conn->ctask = NULL;
677
678 /* process the rest control plane PDUs, if any */
679 if (unlikely(__kfifo_len(conn->mgmtqueue))) {
680 while (__kfifo_get(conn->mgmtqueue, (void*)&conn->mtask,
681 sizeof(void*))) {
Mike Christie994442e2006-05-30 00:37:22 -0500682 spin_lock_bh(&conn->session->lock);
Mike Christie7996a772006-04-06 21:13:41 -0500683 list_add_tail(&conn->mtask->running,
684 &conn->mgmt_run_list);
Mike Christie994442e2006-05-30 00:37:22 -0500685 spin_unlock_bh(&conn->session->lock);
Mike Christie3219e522006-05-30 00:37:28 -0500686 rc = tt->xmit_mgmt_task(conn, conn->mtask);
687 if (rc)
Mike Christie7996a772006-04-06 21:13:41 -0500688 goto again;
689 }
690 /* done with this mtask */
691 conn->mtask = NULL;
692 }
693
Mike Christie3219e522006-05-30 00:37:28 -0500694 return -ENODATA;
Mike Christie7996a772006-04-06 21:13:41 -0500695
696again:
697 if (unlikely(conn->suspend_tx))
Mike Christie3219e522006-05-30 00:37:28 -0500698 return -ENODATA;
Mike Christie7996a772006-04-06 21:13:41 -0500699
Mike Christie3219e522006-05-30 00:37:28 -0500700 return rc;
Mike Christie7996a772006-04-06 21:13:41 -0500701}
702
703static void iscsi_xmitworker(void *data)
704{
705 struct iscsi_conn *conn = data;
Mike Christie3219e522006-05-30 00:37:28 -0500706 int rc;
Mike Christie7996a772006-04-06 21:13:41 -0500707 /*
708 * serialize Xmit worker on a per-connection basis.
709 */
710 mutex_lock(&conn->xmitmutex);
Mike Christie3219e522006-05-30 00:37:28 -0500711 do {
712 rc = iscsi_data_xmit(conn);
713 } while (rc >= 0 || rc == -EAGAIN);
Mike Christie7996a772006-04-06 21:13:41 -0500714 mutex_unlock(&conn->xmitmutex);
715}
716
717enum {
718 FAILURE_BAD_HOST = 1,
719 FAILURE_SESSION_FAILED,
720 FAILURE_SESSION_FREED,
721 FAILURE_WINDOW_CLOSED,
Mike Christie60ecebf2006-08-31 18:09:25 -0400722 FAILURE_OOM,
Mike Christie7996a772006-04-06 21:13:41 -0500723 FAILURE_SESSION_TERMINATE,
Mike Christie656cffc2006-05-18 20:31:42 -0500724 FAILURE_SESSION_IN_RECOVERY,
Mike Christie7996a772006-04-06 21:13:41 -0500725 FAILURE_SESSION_RECOVERY_TIMEOUT,
726};
727
728int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
729{
730 struct Scsi_Host *host;
731 int reason = 0;
732 struct iscsi_session *session;
733 struct iscsi_conn *conn;
734 struct iscsi_cmd_task *ctask = NULL;
735
736 sc->scsi_done = done;
737 sc->result = 0;
Mike Christief47f2cf2006-08-31 18:09:33 -0400738 sc->SCp.ptr = NULL;
Mike Christie7996a772006-04-06 21:13:41 -0500739
740 host = sc->device->host;
741 session = iscsi_hostdata(host->hostdata);
742
743 spin_lock(&session->lock);
744
Mike Christie656cffc2006-05-18 20:31:42 -0500745 /*
746 * ISCSI_STATE_FAILED is a temp. state. The recovery
747 * code will decide what is best to do with command queued
748 * during this time
749 */
750 if (session->state != ISCSI_STATE_LOGGED_IN &&
751 session->state != ISCSI_STATE_FAILED) {
752 /*
753 * to handle the race between when we set the recovery state
754 * and block the session we requeue here (commands could
755 * be entering our queuecommand while a block is starting
756 * up because the block code is not locked)
757 */
758 if (session->state == ISCSI_STATE_IN_RECOVERY) {
759 reason = FAILURE_SESSION_IN_RECOVERY;
Mike Christie67a61112006-05-30 00:37:20 -0500760 goto reject;
Mike Christie7996a772006-04-06 21:13:41 -0500761 }
Mike Christie656cffc2006-05-18 20:31:42 -0500762
763 if (session->state == ISCSI_STATE_RECOVERY_FAILED)
764 reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
765 else if (session->state == ISCSI_STATE_TERMINATE)
766 reason = FAILURE_SESSION_TERMINATE;
767 else
768 reason = FAILURE_SESSION_FREED;
Mike Christie7996a772006-04-06 21:13:41 -0500769 goto fault;
770 }
771
772 /*
773 * Check for iSCSI window and take care of CmdSN wrap-around
774 */
775 if ((int)(session->max_cmdsn - session->cmdsn) < 0) {
776 reason = FAILURE_WINDOW_CLOSED;
777 goto reject;
778 }
779
780 conn = session->leadconn;
Mike Christie98644042006-10-16 18:09:39 -0400781 if (!conn) {
782 reason = FAILURE_SESSION_FREED;
783 goto fault;
784 }
Mike Christie7996a772006-04-06 21:13:41 -0500785
Mike Christie60ecebf2006-08-31 18:09:25 -0400786 if (!__kfifo_get(session->cmdpool.queue, (void*)&ctask,
787 sizeof(void*))) {
788 reason = FAILURE_OOM;
789 goto reject;
790 }
Mike Christie7996a772006-04-06 21:13:41 -0500791 sc->SCp.phase = session->age;
792 sc->SCp.ptr = (char *)ctask;
793
Mike Christie60ecebf2006-08-31 18:09:25 -0400794 atomic_set(&ctask->refcount, 1);
Mike Christieb6c395ed2006-07-24 15:47:15 -0500795 ctask->state = ISCSI_TASK_PENDING;
Mike Christie7996a772006-04-06 21:13:41 -0500796 ctask->mtask = NULL;
797 ctask->conn = conn;
798 ctask->sc = sc;
799 INIT_LIST_HEAD(&ctask->running);
800 ctask->total_length = sc->request_bufflen;
801 iscsi_prep_scsi_cmd_pdu(ctask);
802
803 session->tt->init_cmd_task(ctask);
804
Mike Christieb6c395ed2006-07-24 15:47:15 -0500805 list_add_tail(&ctask->running, &conn->xmitqueue);
Mike Christie7996a772006-04-06 21:13:41 -0500806 debug_scsi(
Mike Christief47f2cf2006-08-31 18:09:33 -0400807 "ctask enq [%s cid %d sc %p cdb 0x%x itt 0x%x len %d cmdsn %d "
808 "win %d]\n",
Mike Christie7996a772006-04-06 21:13:41 -0500809 sc->sc_data_direction == DMA_TO_DEVICE ? "write" : "read",
Mike Christief47f2cf2006-08-31 18:09:33 -0400810 conn->id, sc, sc->cmnd[0], ctask->itt, sc->request_bufflen,
Mike Christie7996a772006-04-06 21:13:41 -0500811 session->cmdsn, session->max_cmdsn - session->exp_cmdsn + 1);
812 spin_unlock(&session->lock);
813
814 scsi_queue_work(host, &conn->xmitwork);
815 return 0;
816
817reject:
818 spin_unlock(&session->lock);
819 debug_scsi("cmd 0x%x rejected (%d)\n", sc->cmnd[0], reason);
820 return SCSI_MLQUEUE_HOST_BUSY;
821
822fault:
823 spin_unlock(&session->lock);
Or Gerlitzbe2df722006-05-02 19:46:43 -0500824 printk(KERN_ERR "iscsi: cmd 0x%x is not queued (%d)\n",
Mike Christie7996a772006-04-06 21:13:41 -0500825 sc->cmnd[0], reason);
826 sc->result = (DID_NO_CONNECT << 16);
827 sc->resid = sc->request_bufflen;
828 sc->scsi_done(sc);
829 return 0;
830}
831EXPORT_SYMBOL_GPL(iscsi_queuecommand);
832
833int iscsi_change_queue_depth(struct scsi_device *sdev, int depth)
834{
835 if (depth > ISCSI_MAX_CMD_PER_LUN)
836 depth = ISCSI_MAX_CMD_PER_LUN;
837 scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
838 return sdev->queue_depth;
839}
840EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
841
842static int
843iscsi_conn_send_generic(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
844 char *data, uint32_t data_size)
845{
846 struct iscsi_session *session = conn->session;
847 struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
848 struct iscsi_mgmt_task *mtask;
849
850 spin_lock_bh(&session->lock);
851 if (session->state == ISCSI_STATE_TERMINATE) {
852 spin_unlock_bh(&session->lock);
853 return -EPERM;
854 }
855 if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) ||
856 hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
857 /*
858 * Login and Text are sent serially, in
859 * request-followed-by-response sequence.
860 * Same mtask can be used. Same ITT must be used.
861 * Note that login_mtask is preallocated at conn_create().
862 */
863 mtask = conn->login_mtask;
864 else {
Mike Christie656cffc2006-05-18 20:31:42 -0500865 BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
866 BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
Mike Christie7996a772006-04-06 21:13:41 -0500867
Mike Christie8d2860b2006-05-02 19:46:47 -0500868 nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
Mike Christie7996a772006-04-06 21:13:41 -0500869 if (!__kfifo_get(session->mgmtpool.queue,
870 (void*)&mtask, sizeof(void*))) {
871 spin_unlock_bh(&session->lock);
872 return -ENOSPC;
873 }
874 }
875
876 /*
Mike Christie8d2860b2006-05-02 19:46:47 -0500877 * pre-format CmdSN for outgoing PDU.
Mike Christie7996a772006-04-06 21:13:41 -0500878 */
879 if (hdr->itt != cpu_to_be32(ISCSI_RESERVED_TAG)) {
880 hdr->itt = mtask->itt | (conn->id << ISCSI_CID_SHIFT) |
881 (session->age << ISCSI_AGE_SHIFT);
882 nop->cmdsn = cpu_to_be32(session->cmdsn);
883 if (conn->c_stage == ISCSI_CONN_STARTED &&
884 !(hdr->opcode & ISCSI_OP_IMMEDIATE))
885 session->cmdsn++;
886 } else
887 /* do not advance CmdSN */
888 nop->cmdsn = cpu_to_be32(session->cmdsn);
889
Mike Christie7996a772006-04-06 21:13:41 -0500890 if (data_size) {
891 memcpy(mtask->data, data, data_size);
892 mtask->data_count = data_size;
893 } else
894 mtask->data_count = 0;
895
896 INIT_LIST_HEAD(&mtask->running);
897 memcpy(mtask->hdr, hdr, sizeof(struct iscsi_hdr));
898 if (session->tt->init_mgmt_task)
899 session->tt->init_mgmt_task(conn, mtask, data, data_size);
900 spin_unlock_bh(&session->lock);
901
902 debug_scsi("mgmtpdu [op 0x%x hdr->itt 0x%x datalen %d]\n",
903 hdr->opcode, hdr->itt, data_size);
904
905 /*
906 * since send_pdu() could be called at least from two contexts,
907 * we need to serialize __kfifo_put, so we don't have to take
908 * additional lock on fast data-path
909 */
910 if (hdr->opcode & ISCSI_OP_IMMEDIATE)
911 __kfifo_put(conn->immqueue, (void*)&mtask, sizeof(void*));
912 else
913 __kfifo_put(conn->mgmtqueue, (void*)&mtask, sizeof(void*));
914
915 scsi_queue_work(session->host, &conn->xmitwork);
916 return 0;
917}
918
919int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
920 char *data, uint32_t data_size)
921{
922 struct iscsi_conn *conn = cls_conn->dd_data;
923 int rc;
924
925 mutex_lock(&conn->xmitmutex);
926 rc = iscsi_conn_send_generic(conn, hdr, data, data_size);
927 mutex_unlock(&conn->xmitmutex);
928
929 return rc;
930}
931EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
932
933void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
934{
935 struct iscsi_session *session = class_to_transport_session(cls_session);
936 struct iscsi_conn *conn = session->leadconn;
937
938 spin_lock_bh(&session->lock);
939 if (session->state != ISCSI_STATE_LOGGED_IN) {
Mike Christie656cffc2006-05-18 20:31:42 -0500940 session->state = ISCSI_STATE_RECOVERY_FAILED;
Mike Christie7996a772006-04-06 21:13:41 -0500941 if (conn)
942 wake_up(&conn->ehwait);
943 }
944 spin_unlock_bh(&session->lock);
945}
946EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
947
948int iscsi_eh_host_reset(struct scsi_cmnd *sc)
949{
950 struct Scsi_Host *host = sc->device->host;
951 struct iscsi_session *session = iscsi_hostdata(host->hostdata);
952 struct iscsi_conn *conn = session->leadconn;
953 int fail_session = 0;
954
955 spin_lock_bh(&session->lock);
956 if (session->state == ISCSI_STATE_TERMINATE) {
957failed:
958 debug_scsi("failing host reset: session terminated "
959 "[CID %d age %d]", conn->id, session->age);
960 spin_unlock_bh(&session->lock);
961 return FAILED;
962 }
963
964 if (sc->SCp.phase == session->age) {
965 debug_scsi("failing connection CID %d due to SCSI host reset",
966 conn->id);
967 fail_session = 1;
968 }
969 spin_unlock_bh(&session->lock);
970
971 /*
972 * we drop the lock here but the leadconn cannot be destoyed while
973 * we are in the scsi eh
974 */
Mike Christie656cffc2006-05-18 20:31:42 -0500975 if (fail_session)
Mike Christie7996a772006-04-06 21:13:41 -0500976 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
Mike Christie7996a772006-04-06 21:13:41 -0500977
978 debug_scsi("iscsi_eh_host_reset wait for relogin\n");
979 wait_event_interruptible(conn->ehwait,
980 session->state == ISCSI_STATE_TERMINATE ||
981 session->state == ISCSI_STATE_LOGGED_IN ||
Mike Christie656cffc2006-05-18 20:31:42 -0500982 session->state == ISCSI_STATE_RECOVERY_FAILED);
Mike Christie7996a772006-04-06 21:13:41 -0500983 if (signal_pending(current))
984 flush_signals(current);
985
986 spin_lock_bh(&session->lock);
987 if (session->state == ISCSI_STATE_LOGGED_IN)
Or Gerlitzbe2df722006-05-02 19:46:43 -0500988 printk(KERN_INFO "iscsi: host reset succeeded\n");
Mike Christie7996a772006-04-06 21:13:41 -0500989 else
990 goto failed;
991 spin_unlock_bh(&session->lock);
992
993 return SUCCESS;
994}
995EXPORT_SYMBOL_GPL(iscsi_eh_host_reset);
996
997static void iscsi_tmabort_timedout(unsigned long data)
998{
999 struct iscsi_cmd_task *ctask = (struct iscsi_cmd_task *)data;
1000 struct iscsi_conn *conn = ctask->conn;
1001 struct iscsi_session *session = conn->session;
1002
1003 spin_lock(&session->lock);
1004 if (conn->tmabort_state == TMABORT_INITIAL) {
1005 conn->tmabort_state = TMABORT_TIMEDOUT;
1006 debug_scsi("tmabort timedout [sc %p itt 0x%x]\n",
1007 ctask->sc, ctask->itt);
1008 /* unblock eh_abort() */
1009 wake_up(&conn->ehwait);
1010 }
1011 spin_unlock(&session->lock);
1012}
1013
1014/* must be called with the mutex lock */
1015static int iscsi_exec_abort_task(struct scsi_cmnd *sc,
1016 struct iscsi_cmd_task *ctask)
1017{
1018 struct iscsi_conn *conn = ctask->conn;
1019 struct iscsi_session *session = conn->session;
1020 struct iscsi_tm *hdr = &conn->tmhdr;
1021 int rc;
1022
1023 /*
1024 * ctask timed out but session is OK requests must be serialized.
1025 */
1026 memset(hdr, 0, sizeof(struct iscsi_tm));
1027 hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
1028 hdr->flags = ISCSI_TM_FUNC_ABORT_TASK;
1029 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
1030 memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
1031 hdr->rtt = ctask->hdr->itt;
1032 hdr->refcmdsn = ctask->hdr->cmdsn;
1033
1034 rc = iscsi_conn_send_generic(conn, (struct iscsi_hdr *)hdr,
1035 NULL, 0);
1036 if (rc) {
1037 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1038 debug_scsi("abort sent failure [itt 0x%x] %d", ctask->itt, rc);
1039 return rc;
1040 }
1041
1042 debug_scsi("abort sent [itt 0x%x]\n", ctask->itt);
1043
1044 spin_lock_bh(&session->lock);
1045 ctask->mtask = (struct iscsi_mgmt_task *)
1046 session->mgmt_cmds[(hdr->itt & ISCSI_ITT_MASK) -
1047 ISCSI_MGMT_ITT_OFFSET];
1048
1049 if (conn->tmabort_state == TMABORT_INITIAL) {
1050 conn->tmfcmd_pdus_cnt++;
1051 conn->tmabort_timer.expires = 10*HZ + jiffies;
1052 conn->tmabort_timer.function = iscsi_tmabort_timedout;
1053 conn->tmabort_timer.data = (unsigned long)ctask;
1054 add_timer(&conn->tmabort_timer);
1055 debug_scsi("abort set timeout [itt 0x%x]", ctask->itt);
1056 }
1057 spin_unlock_bh(&session->lock);
1058 mutex_unlock(&conn->xmitmutex);
1059
1060 /*
1061 * block eh thread until:
1062 *
1063 * 1) abort response
1064 * 2) abort timeout
1065 * 3) session is terminated or restarted or userspace has
1066 * given up on recovery
1067 */
1068 wait_event_interruptible(conn->ehwait,
1069 sc->SCp.phase != session->age ||
1070 session->state != ISCSI_STATE_LOGGED_IN ||
Mike Christie656cffc2006-05-18 20:31:42 -05001071 conn->tmabort_state != TMABORT_INITIAL);
Mike Christie7996a772006-04-06 21:13:41 -05001072 if (signal_pending(current))
1073 flush_signals(current);
1074 del_timer_sync(&conn->tmabort_timer);
1075
1076 mutex_lock(&conn->xmitmutex);
1077 return 0;
1078}
1079
1080/*
1081 * xmit mutex and session lock must be held
1082 */
Mike Christieb6c395ed2006-07-24 15:47:15 -05001083static struct iscsi_mgmt_task *
1084iscsi_remove_mgmt_task(struct kfifo *fifo, uint32_t itt)
1085{
1086 int i, nr_tasks = __kfifo_len(fifo) / sizeof(void*);
1087 struct iscsi_mgmt_task *task;
Mike Christie7996a772006-04-06 21:13:41 -05001088
Mike Christieb6c395ed2006-07-24 15:47:15 -05001089 debug_scsi("searching %d tasks\n", nr_tasks);
1090
1091 for (i = 0; i < nr_tasks; i++) {
1092 __kfifo_get(fifo, (void*)&task, sizeof(void*));
1093 debug_scsi("check task %u\n", task->itt);
1094
1095 if (task->itt == itt) {
1096 debug_scsi("matched task\n");
1097 return task;
1098 }
1099
1100 __kfifo_put(fifo, (void*)&task, sizeof(void*));
1101 }
1102 return NULL;
1103}
Mike Christie7996a772006-04-06 21:13:41 -05001104
1105static int iscsi_ctask_mtask_cleanup(struct iscsi_cmd_task *ctask)
1106{
1107 struct iscsi_conn *conn = ctask->conn;
1108 struct iscsi_session *session = conn->session;
1109
1110 if (!ctask->mtask)
1111 return -EINVAL;
1112
1113 if (!iscsi_remove_mgmt_task(conn->immqueue, ctask->mtask->itt))
1114 list_del(&ctask->mtask->running);
1115 __kfifo_put(session->mgmtpool.queue, (void*)&ctask->mtask,
1116 sizeof(void*));
1117 ctask->mtask = NULL;
1118 return 0;
1119}
1120
1121/*
1122 * session lock and xmitmutex must be held
1123 */
1124static void fail_command(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
1125 int err)
1126{
1127 struct scsi_cmnd *sc;
1128
Mike Christie7996a772006-04-06 21:13:41 -05001129 sc = ctask->sc;
1130 if (!sc)
1131 return;
Mike Christiee648f632006-08-31 18:09:34 -04001132
1133 conn->session->tt->cleanup_cmd_task(conn, ctask);
Mike Christie7ea8b822006-07-24 15:47:22 -05001134 iscsi_ctask_mtask_cleanup(ctask);
1135
Mike Christie7996a772006-04-06 21:13:41 -05001136 sc->result = err;
1137 sc->resid = sc->request_bufflen;
Mike Christiee648f632006-08-31 18:09:34 -04001138 /* release ref from queuecommand */
Mike Christie60ecebf2006-08-31 18:09:25 -04001139 __iscsi_put_ctask(ctask);
Mike Christie7996a772006-04-06 21:13:41 -05001140}
1141
1142int iscsi_eh_abort(struct scsi_cmnd *sc)
1143{
Mike Christief47f2cf2006-08-31 18:09:33 -04001144 struct iscsi_cmd_task *ctask;
1145 struct iscsi_conn *conn;
1146 struct iscsi_session *session;
Mike Christie7996a772006-04-06 21:13:41 -05001147 int rc;
1148
Mike Christief47f2cf2006-08-31 18:09:33 -04001149 /*
1150 * if session was ISCSI_STATE_IN_RECOVERY then we may not have
1151 * got the command.
1152 */
1153 if (!sc->SCp.ptr) {
1154 debug_scsi("sc never reached iscsi layer or it completed.\n");
1155 return SUCCESS;
1156 }
1157
1158 ctask = (struct iscsi_cmd_task *)sc->SCp.ptr;
1159 conn = ctask->conn;
1160 session = conn->session;
1161
Mike Christie7996a772006-04-06 21:13:41 -05001162 conn->eh_abort_cnt++;
1163 debug_scsi("aborting [sc %p itt 0x%x]\n", sc, ctask->itt);
1164
1165 mutex_lock(&conn->xmitmutex);
1166 spin_lock_bh(&session->lock);
1167
1168 /*
1169 * If we are not logged in or we have started a new session
1170 * then let the host reset code handle this
1171 */
1172 if (session->state != ISCSI_STATE_LOGGED_IN ||
1173 sc->SCp.phase != session->age)
1174 goto failed;
1175
1176 /* ctask completed before time out */
Mike Christie7ea8b822006-07-24 15:47:22 -05001177 if (!ctask->sc) {
1178 spin_unlock_bh(&session->lock);
1179 debug_scsi("sc completed while abort in progress\n");
1180 goto success_rel_mutex;
1181 }
Mike Christie7996a772006-04-06 21:13:41 -05001182
1183 /* what should we do here ? */
1184 if (conn->ctask == ctask) {
Or Gerlitzbe2df722006-05-02 19:46:43 -05001185 printk(KERN_INFO "iscsi: sc %p itt 0x%x partially sent. "
1186 "Failing abort\n", sc, ctask->itt);
Mike Christie7996a772006-04-06 21:13:41 -05001187 goto failed;
1188 }
1189
Mike Christieb6c395ed2006-07-24 15:47:15 -05001190 if (ctask->state == ISCSI_TASK_PENDING)
Mike Christie7ea8b822006-07-24 15:47:22 -05001191 goto success_cleanup;
Mike Christie7996a772006-04-06 21:13:41 -05001192
1193 conn->tmabort_state = TMABORT_INITIAL;
1194
1195 spin_unlock_bh(&session->lock);
1196 rc = iscsi_exec_abort_task(sc, ctask);
1197 spin_lock_bh(&session->lock);
1198
Mike Christie7996a772006-04-06 21:13:41 -05001199 if (rc || sc->SCp.phase != session->age ||
1200 session->state != ISCSI_STATE_LOGGED_IN)
1201 goto failed;
Mike Christie7ea8b822006-07-24 15:47:22 -05001202 iscsi_ctask_mtask_cleanup(ctask);
Mike Christie7996a772006-04-06 21:13:41 -05001203
Mike Christie7ea8b822006-07-24 15:47:22 -05001204 switch (conn->tmabort_state) {
1205 case TMABORT_SUCCESS:
1206 goto success_cleanup;
1207 case TMABORT_NOT_FOUND:
1208 if (!ctask->sc) {
1209 /* ctask completed before tmf abort response */
1210 spin_unlock_bh(&session->lock);
1211 debug_scsi("sc completed while abort in progress\n");
1212 goto success_rel_mutex;
1213 }
1214 /* fall through */
1215 default:
1216 /* timedout or failed */
Mike Christie7996a772006-04-06 21:13:41 -05001217 spin_unlock_bh(&session->lock);
1218 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1219 spin_lock_bh(&session->lock);
1220 goto failed;
1221 }
1222
Mike Christie7ea8b822006-07-24 15:47:22 -05001223success_cleanup:
Mike Christie7996a772006-04-06 21:13:41 -05001224 debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
1225 spin_unlock_bh(&session->lock);
1226
1227 /*
1228 * clean up task if aborted. we have the xmitmutex so grab
1229 * the recv lock as a writer
1230 */
1231 write_lock_bh(conn->recv_lock);
1232 spin_lock(&session->lock);
1233 fail_command(conn, ctask, DID_ABORT << 16);
1234 spin_unlock(&session->lock);
1235 write_unlock_bh(conn->recv_lock);
1236
Mike Christie7ea8b822006-07-24 15:47:22 -05001237success_rel_mutex:
Mike Christie7996a772006-04-06 21:13:41 -05001238 mutex_unlock(&conn->xmitmutex);
1239 return SUCCESS;
1240
1241failed:
1242 spin_unlock_bh(&session->lock);
1243 mutex_unlock(&conn->xmitmutex);
1244
1245 debug_scsi("abort failed [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
1246 return FAILED;
1247}
1248EXPORT_SYMBOL_GPL(iscsi_eh_abort);
1249
1250int
1251iscsi_pool_init(struct iscsi_queue *q, int max, void ***items, int item_size)
1252{
1253 int i;
1254
1255 *items = kmalloc(max * sizeof(void*), GFP_KERNEL);
1256 if (*items == NULL)
1257 return -ENOMEM;
1258
1259 q->max = max;
1260 q->pool = kmalloc(max * sizeof(void*), GFP_KERNEL);
1261 if (q->pool == NULL) {
1262 kfree(*items);
1263 return -ENOMEM;
1264 }
1265
1266 q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
1267 GFP_KERNEL, NULL);
1268 if (q->queue == ERR_PTR(-ENOMEM)) {
1269 kfree(q->pool);
1270 kfree(*items);
1271 return -ENOMEM;
1272 }
1273
1274 for (i = 0; i < max; i++) {
1275 q->pool[i] = kmalloc(item_size, GFP_KERNEL);
1276 if (q->pool[i] == NULL) {
1277 int j;
1278
1279 for (j = 0; j < i; j++)
1280 kfree(q->pool[j]);
1281
1282 kfifo_free(q->queue);
1283 kfree(q->pool);
1284 kfree(*items);
1285 return -ENOMEM;
1286 }
1287 memset(q->pool[i], 0, item_size);
1288 (*items)[i] = q->pool[i];
1289 __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
1290 }
1291 return 0;
1292}
1293EXPORT_SYMBOL_GPL(iscsi_pool_init);
1294
1295void iscsi_pool_free(struct iscsi_queue *q, void **items)
1296{
1297 int i;
1298
1299 for (i = 0; i < q->max; i++)
1300 kfree(items[i]);
1301 kfree(q->pool);
1302 kfree(items);
1303}
1304EXPORT_SYMBOL_GPL(iscsi_pool_free);
1305
1306/*
1307 * iSCSI Session's hostdata organization:
1308 *
1309 * *------------------* <== hostdata_session(host->hostdata)
1310 * | ptr to class sess|
1311 * |------------------| <== iscsi_hostdata(host->hostdata)
1312 * | iscsi_session |
1313 * *------------------*
1314 */
1315
1316#define hostdata_privsize(_sz) (sizeof(unsigned long) + _sz + \
1317 _sz % sizeof(unsigned long))
1318
1319#define hostdata_session(_hostdata) (iscsi_ptr(*(unsigned long *)_hostdata))
1320
1321/**
1322 * iscsi_session_setup - create iscsi cls session and host and session
1323 * @scsit: scsi transport template
1324 * @iscsit: iscsi transport template
1325 * @initial_cmdsn: initial CmdSN
1326 * @hostno: host no allocated
1327 *
1328 * This can be used by software iscsi_transports that allocate
1329 * a session per scsi host.
1330 **/
1331struct iscsi_cls_session *
1332iscsi_session_setup(struct iscsi_transport *iscsit,
1333 struct scsi_transport_template *scsit,
1334 int cmd_task_size, int mgmt_task_size,
1335 uint32_t initial_cmdsn, uint32_t *hostno)
1336{
1337 struct Scsi_Host *shost;
1338 struct iscsi_session *session;
1339 struct iscsi_cls_session *cls_session;
1340 int cmd_i;
1341
1342 shost = scsi_host_alloc(iscsit->host_template,
1343 hostdata_privsize(sizeof(*session)));
1344 if (!shost)
1345 return NULL;
1346
1347 shost->max_id = 1;
1348 shost->max_channel = 0;
1349 shost->max_lun = iscsit->max_lun;
1350 shost->max_cmd_len = iscsit->max_cmd_len;
1351 shost->transportt = scsit;
1352 shost->transportt->create_work_queue = 1;
1353 *hostno = shost->host_no;
1354
1355 session = iscsi_hostdata(shost->hostdata);
1356 memset(session, 0, sizeof(struct iscsi_session));
1357 session->host = shost;
1358 session->state = ISCSI_STATE_FREE;
1359 session->mgmtpool_max = ISCSI_MGMT_CMDS_MAX;
1360 session->cmds_max = ISCSI_XMIT_CMDS_MAX;
1361 session->cmdsn = initial_cmdsn;
1362 session->exp_cmdsn = initial_cmdsn + 1;
1363 session->max_cmdsn = initial_cmdsn + 1;
1364 session->max_r2t = 1;
1365 session->tt = iscsit;
1366
1367 /* initialize SCSI PDU commands pool */
1368 if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
1369 (void***)&session->cmds,
1370 cmd_task_size + sizeof(struct iscsi_cmd_task)))
1371 goto cmdpool_alloc_fail;
1372
1373 /* pre-format cmds pool with ITT */
1374 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
1375 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
1376
1377 if (cmd_task_size)
1378 ctask->dd_data = &ctask[1];
1379 ctask->itt = cmd_i;
Mike Christieb6c395ed2006-07-24 15:47:15 -05001380 INIT_LIST_HEAD(&ctask->running);
Mike Christie7996a772006-04-06 21:13:41 -05001381 }
1382
1383 spin_lock_init(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001384
1385 /* initialize immediate command pool */
1386 if (iscsi_pool_init(&session->mgmtpool, session->mgmtpool_max,
1387 (void***)&session->mgmt_cmds,
1388 mgmt_task_size + sizeof(struct iscsi_mgmt_task)))
1389 goto mgmtpool_alloc_fail;
1390
1391
1392 /* pre-format immediate cmds pool with ITT */
1393 for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
1394 struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
1395
1396 if (mgmt_task_size)
1397 mtask->dd_data = &mtask[1];
1398 mtask->itt = ISCSI_MGMT_ITT_OFFSET + cmd_i;
Mike Christieb6c395ed2006-07-24 15:47:15 -05001399 INIT_LIST_HEAD(&mtask->running);
Mike Christie7996a772006-04-06 21:13:41 -05001400 }
1401
1402 if (scsi_add_host(shost, NULL))
1403 goto add_host_fail;
1404
Mike Christief53a88d2006-06-28 12:00:27 -05001405 if (!try_module_get(iscsit->owner))
1406 goto cls_session_fail;
1407
Mike Christie6a8a0d32006-06-28 12:00:31 -05001408 cls_session = iscsi_create_session(shost, iscsit, 0);
Mike Christie7996a772006-04-06 21:13:41 -05001409 if (!cls_session)
Mike Christief53a88d2006-06-28 12:00:27 -05001410 goto module_put;
Mike Christie7996a772006-04-06 21:13:41 -05001411 *(unsigned long*)shost->hostdata = (unsigned long)cls_session;
1412
1413 return cls_session;
1414
Mike Christief53a88d2006-06-28 12:00:27 -05001415module_put:
1416 module_put(iscsit->owner);
Mike Christie7996a772006-04-06 21:13:41 -05001417cls_session_fail:
1418 scsi_remove_host(shost);
1419add_host_fail:
Mike Christie7996a772006-04-06 21:13:41 -05001420 iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
1421mgmtpool_alloc_fail:
1422 iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
1423cmdpool_alloc_fail:
1424 scsi_host_put(shost);
1425 return NULL;
1426}
1427EXPORT_SYMBOL_GPL(iscsi_session_setup);
1428
1429/**
1430 * iscsi_session_teardown - destroy session, host, and cls_session
1431 * shost: scsi host
1432 *
1433 * This can be used by software iscsi_transports that allocate
1434 * a session per scsi host.
1435 **/
1436void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
1437{
1438 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
1439 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
Mike Christie63f75cc2006-07-24 15:47:29 -05001440 struct module *owner = cls_session->transport->owner;
Mike Christie7996a772006-04-06 21:13:41 -05001441
1442 scsi_remove_host(shost);
1443
Mike Christie7996a772006-04-06 21:13:41 -05001444 iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
1445 iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
1446
Mike Christief3ff0c32006-07-24 15:47:50 -05001447 kfree(session->targetname);
1448
Mike Christie7996a772006-04-06 21:13:41 -05001449 iscsi_destroy_session(cls_session);
1450 scsi_host_put(shost);
Mike Christie63f75cc2006-07-24 15:47:29 -05001451 module_put(owner);
Mike Christie7996a772006-04-06 21:13:41 -05001452}
1453EXPORT_SYMBOL_GPL(iscsi_session_teardown);
1454
1455/**
1456 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
1457 * @cls_session: iscsi_cls_session
1458 * @conn_idx: cid
1459 **/
1460struct iscsi_cls_conn *
1461iscsi_conn_setup(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
1462{
1463 struct iscsi_session *session = class_to_transport_session(cls_session);
1464 struct iscsi_conn *conn;
1465 struct iscsi_cls_conn *cls_conn;
Mike Christied36ab6f2006-05-18 20:31:34 -05001466 char *data;
Mike Christie7996a772006-04-06 21:13:41 -05001467
1468 cls_conn = iscsi_create_conn(cls_session, conn_idx);
1469 if (!cls_conn)
1470 return NULL;
1471 conn = cls_conn->dd_data;
1472 memset(conn, 0, sizeof(*conn));
1473
1474 conn->session = session;
1475 conn->cls_conn = cls_conn;
1476 conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
1477 conn->id = conn_idx;
1478 conn->exp_statsn = 0;
1479 conn->tmabort_state = TMABORT_INITIAL;
1480 INIT_LIST_HEAD(&conn->run_list);
1481 INIT_LIST_HEAD(&conn->mgmt_run_list);
Mike Christieb6c395ed2006-07-24 15:47:15 -05001482 INIT_LIST_HEAD(&conn->xmitqueue);
Mike Christie7996a772006-04-06 21:13:41 -05001483
1484 /* initialize general immediate & non-immediate PDU commands queue */
1485 conn->immqueue = kfifo_alloc(session->mgmtpool_max * sizeof(void*),
1486 GFP_KERNEL, NULL);
1487 if (conn->immqueue == ERR_PTR(-ENOMEM))
1488 goto immqueue_alloc_fail;
1489
1490 conn->mgmtqueue = kfifo_alloc(session->mgmtpool_max * sizeof(void*),
1491 GFP_KERNEL, NULL);
1492 if (conn->mgmtqueue == ERR_PTR(-ENOMEM))
1493 goto mgmtqueue_alloc_fail;
1494
1495 INIT_WORK(&conn->xmitwork, iscsi_xmitworker, conn);
1496
1497 /* allocate login_mtask used for the login/text sequences */
1498 spin_lock_bh(&session->lock);
1499 if (!__kfifo_get(session->mgmtpool.queue,
1500 (void*)&conn->login_mtask,
1501 sizeof(void*))) {
1502 spin_unlock_bh(&session->lock);
1503 goto login_mtask_alloc_fail;
1504 }
1505 spin_unlock_bh(&session->lock);
1506
Mike Christied36ab6f2006-05-18 20:31:34 -05001507 data = kmalloc(DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH, GFP_KERNEL);
1508 if (!data)
1509 goto login_mtask_data_alloc_fail;
Mike Christiec8dc1e52006-07-24 15:47:39 -05001510 conn->login_mtask->data = conn->data = data;
Mike Christied36ab6f2006-05-18 20:31:34 -05001511
Mike Christie7996a772006-04-06 21:13:41 -05001512 init_timer(&conn->tmabort_timer);
1513 mutex_init(&conn->xmitmutex);
1514 init_waitqueue_head(&conn->ehwait);
1515
1516 return cls_conn;
1517
Mike Christied36ab6f2006-05-18 20:31:34 -05001518login_mtask_data_alloc_fail:
1519 __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
1520 sizeof(void*));
Mike Christie7996a772006-04-06 21:13:41 -05001521login_mtask_alloc_fail:
1522 kfifo_free(conn->mgmtqueue);
1523mgmtqueue_alloc_fail:
1524 kfifo_free(conn->immqueue);
1525immqueue_alloc_fail:
Mike Christie7996a772006-04-06 21:13:41 -05001526 iscsi_destroy_conn(cls_conn);
1527 return NULL;
1528}
1529EXPORT_SYMBOL_GPL(iscsi_conn_setup);
1530
1531/**
1532 * iscsi_conn_teardown - teardown iscsi connection
1533 * cls_conn: iscsi class connection
1534 *
1535 * TODO: we may need to make this into a two step process
1536 * like scsi-mls remove + put host
1537 */
1538void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
1539{
1540 struct iscsi_conn *conn = cls_conn->dd_data;
1541 struct iscsi_session *session = conn->session;
1542 unsigned long flags;
1543
Mike Christie7996a772006-04-06 21:13:41 -05001544 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
Mike Christie67a61112006-05-30 00:37:20 -05001545 mutex_lock(&conn->xmitmutex);
Mike Christie7996a772006-04-06 21:13:41 -05001546
1547 spin_lock_bh(&session->lock);
1548 conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
1549 if (session->leadconn == conn) {
1550 /*
1551 * leading connection? then give up on recovery.
1552 */
1553 session->state = ISCSI_STATE_TERMINATE;
1554 wake_up(&conn->ehwait);
1555 }
1556 spin_unlock_bh(&session->lock);
1557
1558 mutex_unlock(&conn->xmitmutex);
1559
1560 /*
1561 * Block until all in-progress commands for this connection
1562 * time out or fail.
1563 */
1564 for (;;) {
1565 spin_lock_irqsave(session->host->host_lock, flags);
1566 if (!session->host->host_busy) { /* OK for ERL == 0 */
1567 spin_unlock_irqrestore(session->host->host_lock, flags);
1568 break;
1569 }
1570 spin_unlock_irqrestore(session->host->host_lock, flags);
1571 msleep_interruptible(500);
Or Gerlitzbe2df722006-05-02 19:46:43 -05001572 printk(KERN_INFO "iscsi: scsi conn_destroy(): host_busy %d "
1573 "host_failed %d\n", session->host->host_busy,
1574 session->host->host_failed);
Mike Christie7996a772006-04-06 21:13:41 -05001575 /*
1576 * force eh_abort() to unblock
1577 */
1578 wake_up(&conn->ehwait);
1579 }
1580
1581 spin_lock_bh(&session->lock);
Mike Christiec8dc1e52006-07-24 15:47:39 -05001582 kfree(conn->data);
Mike Christief3ff0c32006-07-24 15:47:50 -05001583 kfree(conn->persistent_address);
Mike Christie7996a772006-04-06 21:13:41 -05001584 __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
1585 sizeof(void*));
Mike Christie98644042006-10-16 18:09:39 -04001586 if (session->leadconn == conn) {
Mike Christie7996a772006-04-06 21:13:41 -05001587 session->leadconn = NULL;
Mike Christie7996a772006-04-06 21:13:41 -05001588 /* no connections exits.. reset sequencing */
1589 session->cmdsn = session->max_cmdsn = session->exp_cmdsn = 1;
Mike Christie98644042006-10-16 18:09:39 -04001590 }
Mike Christie7996a772006-04-06 21:13:41 -05001591 spin_unlock_bh(&session->lock);
1592
Mike Christie7996a772006-04-06 21:13:41 -05001593 kfifo_free(conn->immqueue);
1594 kfifo_free(conn->mgmtqueue);
1595
1596 iscsi_destroy_conn(cls_conn);
1597}
1598EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
1599
1600int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
1601{
1602 struct iscsi_conn *conn = cls_conn->dd_data;
1603 struct iscsi_session *session = conn->session;
1604
Mike Christieffd04362006-08-31 18:09:24 -04001605 if (!session) {
Mike Christie7996a772006-04-06 21:13:41 -05001606 printk(KERN_ERR "iscsi: can't start unbound connection\n");
1607 return -EPERM;
1608 }
1609
Mike Christiedb98ccd2006-08-31 18:09:31 -04001610 if ((session->imm_data_en || !session->initial_r2t_en) &&
1611 session->first_burst > session->max_burst) {
Mike Christieffd04362006-08-31 18:09:24 -04001612 printk("iscsi: invalid burst lengths: "
1613 "first_burst %d max_burst %d\n",
1614 session->first_burst, session->max_burst);
1615 return -EINVAL;
1616 }
1617
Mike Christie7996a772006-04-06 21:13:41 -05001618 spin_lock_bh(&session->lock);
1619 conn->c_stage = ISCSI_CONN_STARTED;
1620 session->state = ISCSI_STATE_LOGGED_IN;
1621
1622 switch(conn->stop_stage) {
1623 case STOP_CONN_RECOVER:
1624 /*
1625 * unblock eh_abort() if it is blocked. re-try all
1626 * commands after successful recovery
1627 */
Mike Christie7996a772006-04-06 21:13:41 -05001628 conn->stop_stage = 0;
1629 conn->tmabort_state = TMABORT_INITIAL;
1630 session->age++;
Mike Christie7996a772006-04-06 21:13:41 -05001631 spin_unlock_bh(&session->lock);
1632
1633 iscsi_unblock_session(session_to_cls(session));
1634 wake_up(&conn->ehwait);
1635 return 0;
1636 case STOP_CONN_TERM:
Mike Christie7996a772006-04-06 21:13:41 -05001637 conn->stop_stage = 0;
1638 break;
Mike Christie7996a772006-04-06 21:13:41 -05001639 default:
1640 break;
1641 }
1642 spin_unlock_bh(&session->lock);
1643
1644 return 0;
1645}
1646EXPORT_SYMBOL_GPL(iscsi_conn_start);
1647
1648static void
1649flush_control_queues(struct iscsi_session *session, struct iscsi_conn *conn)
1650{
1651 struct iscsi_mgmt_task *mtask, *tmp;
1652
1653 /* handle pending */
1654 while (__kfifo_get(conn->immqueue, (void*)&mtask, sizeof(void*)) ||
1655 __kfifo_get(conn->mgmtqueue, (void*)&mtask, sizeof(void*))) {
1656 if (mtask == conn->login_mtask)
1657 continue;
1658 debug_scsi("flushing pending mgmt task itt 0x%x\n", mtask->itt);
1659 __kfifo_put(session->mgmtpool.queue, (void*)&mtask,
1660 sizeof(void*));
1661 }
1662
1663 /* handle running */
1664 list_for_each_entry_safe(mtask, tmp, &conn->mgmt_run_list, running) {
1665 debug_scsi("flushing running mgmt task itt 0x%x\n", mtask->itt);
Mike Christieed2abc72006-05-02 19:46:40 -05001666 list_del(&mtask->running);
1667
Mike Christie7996a772006-04-06 21:13:41 -05001668 if (mtask == conn->login_mtask)
1669 continue;
Mike Christieed2abc72006-05-02 19:46:40 -05001670 __kfifo_put(session->mgmtpool.queue, (void*)&mtask,
Mike Christie7996a772006-04-06 21:13:41 -05001671 sizeof(void*));
1672 }
1673
1674 conn->mtask = NULL;
1675}
1676
1677/* Fail commands. Mutex and session lock held and recv side suspended */
1678static void fail_all_commands(struct iscsi_conn *conn)
1679{
1680 struct iscsi_cmd_task *ctask, *tmp;
1681
1682 /* flush pending */
Mike Christieb6c395ed2006-07-24 15:47:15 -05001683 list_for_each_entry_safe(ctask, tmp, &conn->xmitqueue, running) {
Mike Christie7996a772006-04-06 21:13:41 -05001684 debug_scsi("failing pending sc %p itt 0x%x\n", ctask->sc,
1685 ctask->itt);
1686 fail_command(conn, ctask, DID_BUS_BUSY << 16);
1687 }
1688
1689 /* fail all other running */
1690 list_for_each_entry_safe(ctask, tmp, &conn->run_list, running) {
1691 debug_scsi("failing in progress sc %p itt 0x%x\n",
1692 ctask->sc, ctask->itt);
1693 fail_command(conn, ctask, DID_BUS_BUSY << 16);
1694 }
1695
1696 conn->ctask = NULL;
1697}
1698
Mike Christie656cffc2006-05-18 20:31:42 -05001699static void iscsi_start_session_recovery(struct iscsi_session *session,
1700 struct iscsi_conn *conn, int flag)
Mike Christie7996a772006-04-06 21:13:41 -05001701{
Mike Christieed2abc72006-05-02 19:46:40 -05001702 int old_stop_stage;
1703
Mike Christie7996a772006-04-06 21:13:41 -05001704 spin_lock_bh(&session->lock);
Mike Christieed2abc72006-05-02 19:46:40 -05001705 if (conn->stop_stage == STOP_CONN_TERM) {
Mike Christie7996a772006-04-06 21:13:41 -05001706 spin_unlock_bh(&session->lock);
1707 return;
1708 }
Mike Christieed2abc72006-05-02 19:46:40 -05001709
1710 /*
1711 * When this is called for the in_login state, we only want to clean
Mike Christie67a61112006-05-30 00:37:20 -05001712 * up the login task and connection. We do not need to block and set
1713 * the recovery state again
Mike Christieed2abc72006-05-02 19:46:40 -05001714 */
Mike Christie67a61112006-05-30 00:37:20 -05001715 if (flag == STOP_CONN_TERM)
1716 session->state = ISCSI_STATE_TERMINATE;
1717 else if (conn->stop_stage != STOP_CONN_RECOVER)
1718 session->state = ISCSI_STATE_IN_RECOVERY;
Mike Christieed2abc72006-05-02 19:46:40 -05001719
1720 old_stop_stage = conn->stop_stage;
Mike Christie7996a772006-04-06 21:13:41 -05001721 conn->stop_stage = flag;
Mike Christie67a61112006-05-30 00:37:20 -05001722 conn->c_stage = ISCSI_CONN_STOPPED;
1723 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
Mike Christie7996a772006-04-06 21:13:41 -05001724 spin_unlock_bh(&session->lock);
1725
Mike Christie1c834692006-07-24 15:47:26 -05001726 write_lock_bh(conn->recv_lock);
1727 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
1728 write_unlock_bh(conn->recv_lock);
Mike Christie7996a772006-04-06 21:13:41 -05001729
1730 mutex_lock(&conn->xmitmutex);
Mike Christie7996a772006-04-06 21:13:41 -05001731 /*
1732 * for connection level recovery we should not calculate
1733 * header digest. conn->hdr_size used for optimization
1734 * in hdr_extract() and will be re-negotiated at
1735 * set_param() time.
1736 */
1737 if (flag == STOP_CONN_RECOVER) {
1738 conn->hdrdgst_en = 0;
1739 conn->datadgst_en = 0;
Mike Christie656cffc2006-05-18 20:31:42 -05001740 if (session->state == ISCSI_STATE_IN_RECOVERY &&
Mike Christie67a61112006-05-30 00:37:20 -05001741 old_stop_stage != STOP_CONN_RECOVER) {
1742 debug_scsi("blocking session\n");
Mike Christie7996a772006-04-06 21:13:41 -05001743 iscsi_block_session(session_to_cls(session));
Mike Christie67a61112006-05-30 00:37:20 -05001744 }
Mike Christie7996a772006-04-06 21:13:41 -05001745 }
Mike Christie656cffc2006-05-18 20:31:42 -05001746
Mike Christie656cffc2006-05-18 20:31:42 -05001747 /*
1748 * flush queues.
1749 */
1750 spin_lock_bh(&session->lock);
1751 fail_all_commands(conn);
1752 flush_control_queues(session, conn);
1753 spin_unlock_bh(&session->lock);
1754
Mike Christie7996a772006-04-06 21:13:41 -05001755 mutex_unlock(&conn->xmitmutex);
1756}
Mike Christie7996a772006-04-06 21:13:41 -05001757
1758void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
1759{
1760 struct iscsi_conn *conn = cls_conn->dd_data;
1761 struct iscsi_session *session = conn->session;
1762
1763 switch (flag) {
1764 case STOP_CONN_RECOVER:
1765 case STOP_CONN_TERM:
1766 iscsi_start_session_recovery(session, conn, flag);
Mike Christie8d2860b2006-05-02 19:46:47 -05001767 break;
Mike Christie7996a772006-04-06 21:13:41 -05001768 default:
Or Gerlitzbe2df722006-05-02 19:46:43 -05001769 printk(KERN_ERR "iscsi: invalid stop flag %d\n", flag);
Mike Christie7996a772006-04-06 21:13:41 -05001770 }
1771}
1772EXPORT_SYMBOL_GPL(iscsi_conn_stop);
1773
1774int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
1775 struct iscsi_cls_conn *cls_conn, int is_leading)
1776{
1777 struct iscsi_session *session = class_to_transport_session(cls_session);
Mike Christie98644042006-10-16 18:09:39 -04001778 struct iscsi_conn *conn = cls_conn->dd_data;
Mike Christie7996a772006-04-06 21:13:41 -05001779
Mike Christie7996a772006-04-06 21:13:41 -05001780 spin_lock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001781 if (is_leading)
1782 session->leadconn = conn;
Mike Christie98644042006-10-16 18:09:39 -04001783 spin_unlock_bh(&session->lock);
Mike Christie7996a772006-04-06 21:13:41 -05001784
1785 /*
1786 * Unblock xmitworker(), Login Phase will pass through.
1787 */
1788 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
1789 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1790 return 0;
1791}
1792EXPORT_SYMBOL_GPL(iscsi_conn_bind);
1793
Mike Christiea54a52c2006-06-28 12:00:23 -05001794
1795int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
1796 enum iscsi_param param, char *buf, int buflen)
1797{
1798 struct iscsi_conn *conn = cls_conn->dd_data;
1799 struct iscsi_session *session = conn->session;
1800 uint32_t value;
1801
1802 switch(param) {
1803 case ISCSI_PARAM_MAX_RECV_DLENGTH:
1804 sscanf(buf, "%d", &conn->max_recv_dlength);
1805 break;
1806 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
1807 sscanf(buf, "%d", &conn->max_xmit_dlength);
1808 break;
1809 case ISCSI_PARAM_HDRDGST_EN:
1810 sscanf(buf, "%d", &conn->hdrdgst_en);
1811 break;
1812 case ISCSI_PARAM_DATADGST_EN:
1813 sscanf(buf, "%d", &conn->datadgst_en);
1814 break;
1815 case ISCSI_PARAM_INITIAL_R2T_EN:
1816 sscanf(buf, "%d", &session->initial_r2t_en);
1817 break;
1818 case ISCSI_PARAM_MAX_R2T:
1819 sscanf(buf, "%d", &session->max_r2t);
1820 break;
1821 case ISCSI_PARAM_IMM_DATA_EN:
1822 sscanf(buf, "%d", &session->imm_data_en);
1823 break;
1824 case ISCSI_PARAM_FIRST_BURST:
1825 sscanf(buf, "%d", &session->first_burst);
1826 break;
1827 case ISCSI_PARAM_MAX_BURST:
1828 sscanf(buf, "%d", &session->max_burst);
1829 break;
1830 case ISCSI_PARAM_PDU_INORDER_EN:
1831 sscanf(buf, "%d", &session->pdu_inorder_en);
1832 break;
1833 case ISCSI_PARAM_DATASEQ_INORDER_EN:
1834 sscanf(buf, "%d", &session->dataseq_inorder_en);
1835 break;
1836 case ISCSI_PARAM_ERL:
1837 sscanf(buf, "%d", &session->erl);
1838 break;
1839 case ISCSI_PARAM_IFMARKER_EN:
1840 sscanf(buf, "%d", &value);
1841 BUG_ON(value);
1842 break;
1843 case ISCSI_PARAM_OFMARKER_EN:
1844 sscanf(buf, "%d", &value);
1845 BUG_ON(value);
1846 break;
1847 case ISCSI_PARAM_EXP_STATSN:
1848 sscanf(buf, "%u", &conn->exp_statsn);
1849 break;
1850 case ISCSI_PARAM_TARGET_NAME:
1851 /* this should not change between logins */
1852 if (session->targetname)
1853 break;
1854
1855 session->targetname = kstrdup(buf, GFP_KERNEL);
1856 if (!session->targetname)
1857 return -ENOMEM;
1858 break;
1859 case ISCSI_PARAM_TPGT:
1860 sscanf(buf, "%d", &session->tpgt);
1861 break;
1862 case ISCSI_PARAM_PERSISTENT_PORT:
1863 sscanf(buf, "%d", &conn->persistent_port);
1864 break;
1865 case ISCSI_PARAM_PERSISTENT_ADDRESS:
1866 /*
1867 * this is the address returned in discovery so it should
1868 * not change between logins.
1869 */
1870 if (conn->persistent_address)
1871 break;
1872
1873 conn->persistent_address = kstrdup(buf, GFP_KERNEL);
1874 if (!conn->persistent_address)
1875 return -ENOMEM;
1876 break;
1877 default:
1878 return -ENOSYS;
1879 }
1880
1881 return 0;
1882}
1883EXPORT_SYMBOL_GPL(iscsi_set_param);
1884
1885int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
1886 enum iscsi_param param, char *buf)
1887{
1888 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
1889 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
1890 int len;
1891
1892 switch(param) {
1893 case ISCSI_PARAM_INITIAL_R2T_EN:
1894 len = sprintf(buf, "%d\n", session->initial_r2t_en);
1895 break;
1896 case ISCSI_PARAM_MAX_R2T:
1897 len = sprintf(buf, "%hu\n", session->max_r2t);
1898 break;
1899 case ISCSI_PARAM_IMM_DATA_EN:
1900 len = sprintf(buf, "%d\n", session->imm_data_en);
1901 break;
1902 case ISCSI_PARAM_FIRST_BURST:
1903 len = sprintf(buf, "%u\n", session->first_burst);
1904 break;
1905 case ISCSI_PARAM_MAX_BURST:
1906 len = sprintf(buf, "%u\n", session->max_burst);
1907 break;
1908 case ISCSI_PARAM_PDU_INORDER_EN:
1909 len = sprintf(buf, "%d\n", session->pdu_inorder_en);
1910 break;
1911 case ISCSI_PARAM_DATASEQ_INORDER_EN:
1912 len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
1913 break;
1914 case ISCSI_PARAM_ERL:
1915 len = sprintf(buf, "%d\n", session->erl);
1916 break;
1917 case ISCSI_PARAM_TARGET_NAME:
1918 len = sprintf(buf, "%s\n", session->targetname);
1919 break;
1920 case ISCSI_PARAM_TPGT:
1921 len = sprintf(buf, "%d\n", session->tpgt);
1922 break;
1923 default:
1924 return -ENOSYS;
1925 }
1926
1927 return len;
1928}
1929EXPORT_SYMBOL_GPL(iscsi_session_get_param);
1930
1931int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
1932 enum iscsi_param param, char *buf)
1933{
1934 struct iscsi_conn *conn = cls_conn->dd_data;
1935 int len;
1936
1937 switch(param) {
1938 case ISCSI_PARAM_MAX_RECV_DLENGTH:
1939 len = sprintf(buf, "%u\n", conn->max_recv_dlength);
1940 break;
1941 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
1942 len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
1943 break;
1944 case ISCSI_PARAM_HDRDGST_EN:
1945 len = sprintf(buf, "%d\n", conn->hdrdgst_en);
1946 break;
1947 case ISCSI_PARAM_DATADGST_EN:
1948 len = sprintf(buf, "%d\n", conn->datadgst_en);
1949 break;
1950 case ISCSI_PARAM_IFMARKER_EN:
1951 len = sprintf(buf, "%d\n", conn->ifmarker_en);
1952 break;
1953 case ISCSI_PARAM_OFMARKER_EN:
1954 len = sprintf(buf, "%d\n", conn->ofmarker_en);
1955 break;
1956 case ISCSI_PARAM_EXP_STATSN:
1957 len = sprintf(buf, "%u\n", conn->exp_statsn);
1958 break;
1959 case ISCSI_PARAM_PERSISTENT_PORT:
1960 len = sprintf(buf, "%d\n", conn->persistent_port);
1961 break;
1962 case ISCSI_PARAM_PERSISTENT_ADDRESS:
1963 len = sprintf(buf, "%s\n", conn->persistent_address);
1964 break;
1965 default:
1966 return -ENOSYS;
1967 }
1968
1969 return len;
1970}
1971EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
1972
Mike Christie7996a772006-04-06 21:13:41 -05001973MODULE_AUTHOR("Mike Christie");
1974MODULE_DESCRIPTION("iSCSI library functions");
1975MODULE_LICENSE("GPL");