]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/scsi/libiscsi.c
[SCSI] libiscsi: check that command ptr is set before accessing it
[mirror_ubuntu-artful-kernel.git] / drivers / scsi / libiscsi.c
CommitLineData
7996a778
MC
1/*
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,
ffd0436e 71 struct iscsi_data *hdr)
7996a778
MC
72{
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);
ffd0436e 84 hdr->offset = cpu_to_be32(ctask->unsol_offset);
7996a778
MC
85
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;
ffd0436e 89 ctask->unsol_offset += ctask->data_count;
7996a778
MC
90 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
ffd0436e 125 ctask->data_count = 0;
7996a778
MC
126 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;
ffd0436e 144 ctask->unsol_offset = 0;
7996a778
MC
145 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
ffd0436e 158 if (!session->initial_r2t_en) {
7996a778
MC
159 ctask->unsol_count = min(session->first_burst,
160 ctask->total_length) - ctask->imm_count;
ffd0436e
MC
161 ctask->unsol_offset = ctask->imm_count;
162 }
163
7996a778
MC
164 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
7996a778
MC
182 * @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 */
60ecebf5 188static void iscsi_complete_command(struct iscsi_cmd_task *ctask)
7996a778 189{
60ecebf5 190 struct iscsi_session *session = ctask->conn->session;
7996a778
MC
191 struct scsi_cmnd *sc = ctask->sc;
192
b6c395ed 193 ctask->state = ISCSI_TASK_COMPLETED;
7996a778 194 ctask->sc = NULL;
f47f2cf5
MC
195 /* SCSI eh reuses commands to verify us */
196 sc->SCp.ptr = NULL;
7996a778
MC
197 list_del_init(&ctask->running);
198 __kfifo_put(session->cmdpool.queue, (void*)&ctask, sizeof(void*));
199 sc->scsi_done(sc);
200}
201
60ecebf5
MC
202static 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{
216 struct iscsi_conn *conn = ctask->conn;
217
218 if (atomic_dec_and_test(&ctask->refcount)) {
219 conn->session->tt->cleanup_cmd_task(conn, ctask);
220 iscsi_complete_command(ctask);
221 }
222}
223
224static void iscsi_put_ctask(struct iscsi_cmd_task *ctask)
225{
226 spin_lock_bh(&ctask->conn->session->lock);
227 __iscsi_put_ctask(ctask);
228 spin_unlock_bh(&ctask->conn->session->lock);
229}
230
7996a778
MC
231/**
232 * iscsi_cmd_rsp - SCSI Command Response processing
233 * @conn: iscsi connection
234 * @hdr: iscsi header
235 * @ctask: scsi command task
236 * @data: cmd data buffer
237 * @datalen: len of buffer
238 *
239 * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
240 * then completes the command and task.
241 **/
242static int iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
243 struct iscsi_cmd_task *ctask, char *data,
244 int datalen)
245{
246 int rc;
247 struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)hdr;
248 struct iscsi_session *session = conn->session;
249 struct scsi_cmnd *sc = ctask->sc;
250
251 rc = iscsi_check_assign_cmdsn(session, (struct iscsi_nopin*)rhdr);
252 if (rc) {
253 sc->result = DID_ERROR << 16;
254 goto out;
255 }
256
257 conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
258
259 sc->result = (DID_OK << 16) | rhdr->cmd_status;
260
261 if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
262 sc->result = DID_ERROR << 16;
263 goto out;
264 }
265
266 if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
267 int senselen;
268
269 if (datalen < 2) {
270invalid_datalen:
be2df72e
OG
271 printk(KERN_ERR "iscsi: Got CHECK_CONDITION but "
272 "invalid data buffer size of %d\n", datalen);
7996a778
MC
273 sc->result = DID_BAD_TARGET << 16;
274 goto out;
275 }
276
277 senselen = (data[0] << 8) | data[1];
278 if (datalen < senselen)
279 goto invalid_datalen;
280
281 memcpy(sc->sense_buffer, data + 2,
282 min(senselen, SCSI_SENSE_BUFFERSIZE));
283 debug_scsi("copied %d bytes of sense\n",
284 min(senselen, SCSI_SENSE_BUFFERSIZE));
285 }
286
287 if (sc->sc_data_direction == DMA_TO_DEVICE)
288 goto out;
289
290 if (rhdr->flags & ISCSI_FLAG_CMD_UNDERFLOW) {
291 int res_count = be32_to_cpu(rhdr->residual_count);
292
293 if (res_count > 0 && res_count <= sc->request_bufflen)
294 sc->resid = res_count;
295 else
296 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
297 } else if (rhdr->flags & ISCSI_FLAG_CMD_BIDI_UNDERFLOW)
298 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
299 else if (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW)
300 sc->resid = be32_to_cpu(rhdr->residual_count);
301
302out:
303 debug_scsi("done [sc %lx res %d itt 0x%x]\n",
304 (long)sc, sc->result, ctask->itt);
305 conn->scsirsp_pdus_cnt++;
306
60ecebf5 307 __iscsi_put_ctask(ctask);
7996a778
MC
308 return rc;
309}
310
7ea8b828
MC
311static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
312{
313 struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
314
315 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
316 conn->tmfrsp_pdus_cnt++;
317
318 if (conn->tmabort_state != TMABORT_INITIAL)
319 return;
320
321 if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
322 conn->tmabort_state = TMABORT_SUCCESS;
323 else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
324 conn->tmabort_state = TMABORT_NOT_FOUND;
325 else
326 conn->tmabort_state = TMABORT_FAILED;
327 wake_up(&conn->ehwait);
328}
329
62f38300
MC
330static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
331 char *data, int datalen)
332{
333 struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
334 struct iscsi_hdr rejected_pdu;
335 uint32_t itt;
336
337 conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
338
339 if (reject->reason == ISCSI_REASON_DATA_DIGEST_ERROR) {
340 if (ntoh24(reject->dlength) > datalen)
341 return ISCSI_ERR_PROTO;
342
343 if (ntoh24(reject->dlength) >= sizeof(struct iscsi_hdr)) {
344 memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
345 itt = rejected_pdu.itt & ISCSI_ITT_MASK;
346 printk(KERN_ERR "itt 0x%x had pdu (op 0x%x) rejected "
347 "due to DataDigest error.\n", itt,
348 rejected_pdu.opcode);
349 }
350 }
351 return 0;
352}
353
7996a778
MC
354/**
355 * __iscsi_complete_pdu - complete pdu
356 * @conn: iscsi conn
357 * @hdr: iscsi header
358 * @data: data buffer
359 * @datalen: len of data buffer
360 *
361 * Completes pdu processing by freeing any resources allocated at
362 * queuecommand or send generic. session lock must be held and verify
363 * itt must have been called.
364 */
365int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
366 char *data, int datalen)
367{
368 struct iscsi_session *session = conn->session;
369 int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
370 struct iscsi_cmd_task *ctask;
371 struct iscsi_mgmt_task *mtask;
372 uint32_t itt;
373
374 if (hdr->itt != cpu_to_be32(ISCSI_RESERVED_TAG))
375 itt = hdr->itt & ISCSI_ITT_MASK;
376 else
377 itt = hdr->itt;
378
379 if (itt < session->cmds_max) {
380 ctask = session->cmds[itt];
381
382 debug_scsi("cmdrsp [op 0x%x cid %d itt 0x%x len %d]\n",
383 opcode, conn->id, ctask->itt, datalen);
384
385 switch(opcode) {
386 case ISCSI_OP_SCSI_CMD_RSP:
387 BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
388 rc = iscsi_scsi_cmd_rsp(conn, hdr, ctask, data,
389 datalen);
390 break;
391 case ISCSI_OP_SCSI_DATA_IN:
392 BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
393 if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
394 conn->scsirsp_pdus_cnt++;
60ecebf5 395 __iscsi_put_ctask(ctask);
7996a778
MC
396 }
397 break;
398 case ISCSI_OP_R2T:
399 /* LLD handles this for now */
400 break;
401 default:
402 rc = ISCSI_ERR_BAD_OPCODE;
403 break;
404 }
405 } else if (itt >= ISCSI_MGMT_ITT_OFFSET &&
406 itt < ISCSI_MGMT_ITT_OFFSET + session->mgmtpool_max) {
407 mtask = session->mgmt_cmds[itt - ISCSI_MGMT_ITT_OFFSET];
408
409 debug_scsi("immrsp [op 0x%x cid %d itt 0x%x len %d]\n",
410 opcode, conn->id, mtask->itt, datalen);
411
8d2860b3
MC
412 rc = iscsi_check_assign_cmdsn(session,
413 (struct iscsi_nopin*)hdr);
414 if (rc)
415 goto done;
416
7996a778 417 switch(opcode) {
8d2860b3 418 case ISCSI_OP_LOGOUT_RSP:
c8dc1e52
MC
419 if (datalen) {
420 rc = ISCSI_ERR_PROTO;
421 break;
422 }
8d2860b3
MC
423 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
424 /* fall through */
7996a778
MC
425 case ISCSI_OP_LOGIN_RSP:
426 case ISCSI_OP_TEXT_RSP:
8d2860b3
MC
427 /*
428 * login related PDU's exp_statsn is handled in
429 * userspace
430 */
40527afe
MC
431 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
432 rc = ISCSI_ERR_CONN_FAILED;
7996a778
MC
433 list_del(&mtask->running);
434 if (conn->login_mtask != mtask)
435 __kfifo_put(session->mgmtpool.queue,
436 (void*)&mtask, sizeof(void*));
437 break;
438 case ISCSI_OP_SCSI_TMFUNC_RSP:
7996a778
MC
439 if (datalen) {
440 rc = ISCSI_ERR_PROTO;
441 break;
442 }
8d2860b3 443
7ea8b828 444 iscsi_tmf_rsp(conn, hdr);
7996a778
MC
445 break;
446 case ISCSI_OP_NOOP_IN:
c8dc1e52 447 if (hdr->ttt != ISCSI_RESERVED_TAG || datalen) {
7996a778
MC
448 rc = ISCSI_ERR_PROTO;
449 break;
450 }
7996a778
MC
451 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
452
40527afe
MC
453 if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
454 rc = ISCSI_ERR_CONN_FAILED;
7996a778
MC
455 list_del(&mtask->running);
456 if (conn->login_mtask != mtask)
457 __kfifo_put(session->mgmtpool.queue,
458 (void*)&mtask, sizeof(void*));
459 break;
460 default:
461 rc = ISCSI_ERR_BAD_OPCODE;
462 break;
463 }
464 } else if (itt == ISCSI_RESERVED_TAG) {
62f38300
MC
465 rc = iscsi_check_assign_cmdsn(session,
466 (struct iscsi_nopin*)hdr);
467 if (rc)
468 goto done;
469
7996a778
MC
470 switch(opcode) {
471 case ISCSI_OP_NOOP_IN:
40527afe 472 if (datalen) {
7996a778 473 rc = ISCSI_ERR_PROTO;
40527afe
MC
474 break;
475 }
476
40527afe
MC
477 if (hdr->ttt == ISCSI_RESERVED_TAG)
478 break;
479
480 if (iscsi_recv_pdu(conn->cls_conn, hdr, NULL, 0))
481 rc = ISCSI_ERR_CONN_FAILED;
7996a778
MC
482 break;
483 case ISCSI_OP_REJECT:
62f38300
MC
484 rc = iscsi_handle_reject(conn, hdr, data, datalen);
485 break;
7996a778 486 case ISCSI_OP_ASYNC_EVENT:
8d2860b3 487 conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
7996a778
MC
488 /* we need sth like iscsi_async_event_rsp() */
489 rc = ISCSI_ERR_BAD_OPCODE;
490 break;
491 default:
492 rc = ISCSI_ERR_BAD_OPCODE;
493 break;
494 }
495 } else
496 rc = ISCSI_ERR_BAD_ITT;
497
8d2860b3 498done:
7996a778
MC
499 return rc;
500}
501EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
502
503int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
504 char *data, int datalen)
505{
506 int rc;
507
508 spin_lock(&conn->session->lock);
509 rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
510 spin_unlock(&conn->session->lock);
511 return rc;
512}
513EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
514
515/* verify itt (itt encoding: age+cid+itt) */
516int iscsi_verify_itt(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
517 uint32_t *ret_itt)
518{
519 struct iscsi_session *session = conn->session;
520 struct iscsi_cmd_task *ctask;
521 uint32_t itt;
522
523 if (hdr->itt != cpu_to_be32(ISCSI_RESERVED_TAG)) {
524 if ((hdr->itt & ISCSI_AGE_MASK) !=
525 (session->age << ISCSI_AGE_SHIFT)) {
be2df72e 526 printk(KERN_ERR "iscsi: received itt %x expected "
7996a778
MC
527 "session age (%x)\n", hdr->itt,
528 session->age & ISCSI_AGE_MASK);
529 return ISCSI_ERR_BAD_ITT;
530 }
531
532 if ((hdr->itt & ISCSI_CID_MASK) !=
533 (conn->id << ISCSI_CID_SHIFT)) {
be2df72e 534 printk(KERN_ERR "iscsi: received itt %x, expected "
7996a778
MC
535 "CID (%x)\n", hdr->itt, conn->id);
536 return ISCSI_ERR_BAD_ITT;
537 }
538 itt = hdr->itt & ISCSI_ITT_MASK;
539 } else
540 itt = hdr->itt;
541
542 if (itt < session->cmds_max) {
543 ctask = session->cmds[itt];
544
545 if (!ctask->sc) {
be2df72e 546 printk(KERN_INFO "iscsi: dropping ctask with "
7996a778
MC
547 "itt 0x%x\n", ctask->itt);
548 /* force drop */
549 return ISCSI_ERR_NO_SCSI_CMD;
550 }
551
552 if (ctask->sc->SCp.phase != session->age) {
be2df72e 553 printk(KERN_ERR "iscsi: ctask's session age %d, "
7996a778
MC
554 "expected %d\n", ctask->sc->SCp.phase,
555 session->age);
556 return ISCSI_ERR_SESSION_FAILED;
557 }
558 }
559
560 *ret_itt = itt;
561 return 0;
562}
563EXPORT_SYMBOL_GPL(iscsi_verify_itt);
564
565void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
566{
567 struct iscsi_session *session = conn->session;
568 unsigned long flags;
569
570 spin_lock_irqsave(&session->lock, flags);
656cffc9
MC
571 if (session->state == ISCSI_STATE_FAILED) {
572 spin_unlock_irqrestore(&session->lock, flags);
573 return;
574 }
575
67a61114 576 if (conn->stop_stage == 0)
7996a778
MC
577 session->state = ISCSI_STATE_FAILED;
578 spin_unlock_irqrestore(&session->lock, flags);
579 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
580 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
581 iscsi_conn_error(conn->cls_conn, err);
582}
583EXPORT_SYMBOL_GPL(iscsi_conn_failure);
584
585/**
586 * iscsi_data_xmit - xmit any command into the scheduled connection
587 * @conn: iscsi connection
588 *
589 * Notes:
590 * The function can return -EAGAIN in which case the caller must
591 * re-schedule it again later or recover. '0' return code means
592 * successful xmit.
593 **/
594static int iscsi_data_xmit(struct iscsi_conn *conn)
595{
596 struct iscsi_transport *tt;
3219e529 597 int rc = 0;
7996a778
MC
598
599 if (unlikely(conn->suspend_tx)) {
600 debug_scsi("conn %d Tx suspended!\n", conn->id);
3219e529 601 return -ENODATA;
7996a778
MC
602 }
603 tt = conn->session->tt;
604
605 /*
606 * Transmit in the following order:
607 *
608 * 1) un-finished xmit (ctask or mtask)
609 * 2) immediate control PDUs
610 * 3) write data
611 * 4) SCSI commands
612 * 5) non-immediate control PDUs
613 *
614 * No need to lock around __kfifo_get as long as
615 * there's one producer and one consumer.
616 */
617
618 BUG_ON(conn->ctask && conn->mtask);
619
620 if (conn->ctask) {
60ecebf5 621 iscsi_get_ctask(conn->ctask);
3219e529 622 rc = tt->xmit_cmd_task(conn, conn->ctask);
60ecebf5 623 iscsi_put_ctask(conn->ctask);
3219e529 624 if (rc)
7996a778
MC
625 goto again;
626 /* done with this in-progress ctask */
627 conn->ctask = NULL;
628 }
629 if (conn->mtask) {
3219e529
MC
630 rc = tt->xmit_mgmt_task(conn, conn->mtask);
631 if (rc)
7996a778
MC
632 goto again;
633 /* done with this in-progress mtask */
634 conn->mtask = NULL;
635 }
636
637 /* process immediate first */
638 if (unlikely(__kfifo_len(conn->immqueue))) {
639 while (__kfifo_get(conn->immqueue, (void*)&conn->mtask,
640 sizeof(void*))) {
994442e8 641 spin_lock_bh(&conn->session->lock);
7996a778
MC
642 list_add_tail(&conn->mtask->running,
643 &conn->mgmt_run_list);
994442e8 644 spin_unlock_bh(&conn->session->lock);
3219e529
MC
645 rc = tt->xmit_mgmt_task(conn, conn->mtask);
646 if (rc)
7996a778
MC
647 goto again;
648 }
649 /* done with this mtask */
650 conn->mtask = NULL;
651 }
652
653 /* process command queue */
b6c395ed
MC
654 spin_lock_bh(&conn->session->lock);
655 while (!list_empty(&conn->xmitqueue)) {
7996a778
MC
656 /*
657 * iscsi tcp may readd the task to the xmitqueue to send
658 * write data
659 */
b6c395ed
MC
660 conn->ctask = list_entry(conn->xmitqueue.next,
661 struct iscsi_cmd_task, running);
662 conn->ctask->state = ISCSI_TASK_RUNNING;
663 list_move_tail(conn->xmitqueue.next, &conn->run_list);
60ecebf5 664 __iscsi_get_ctask(conn->ctask);
994442e8 665 spin_unlock_bh(&conn->session->lock);
b6c395ed 666
3219e529
MC
667 rc = tt->xmit_cmd_task(conn, conn->ctask);
668 if (rc)
7996a778 669 goto again;
60ecebf5 670
b6c395ed 671 spin_lock_bh(&conn->session->lock);
60ecebf5
MC
672 __iscsi_put_ctask(conn->ctask);
673 if (rc) {
674 spin_unlock_bh(&conn->session->lock);
675 goto again;
676 }
7996a778 677 }
b6c395ed 678 spin_unlock_bh(&conn->session->lock);
7996a778
MC
679 /* done with this ctask */
680 conn->ctask = NULL;
681
682 /* process the rest control plane PDUs, if any */
683 if (unlikely(__kfifo_len(conn->mgmtqueue))) {
684 while (__kfifo_get(conn->mgmtqueue, (void*)&conn->mtask,
685 sizeof(void*))) {
994442e8 686 spin_lock_bh(&conn->session->lock);
7996a778
MC
687 list_add_tail(&conn->mtask->running,
688 &conn->mgmt_run_list);
994442e8 689 spin_unlock_bh(&conn->session->lock);
3219e529
MC
690 rc = tt->xmit_mgmt_task(conn, conn->mtask);
691 if (rc)
7996a778
MC
692 goto again;
693 }
694 /* done with this mtask */
695 conn->mtask = NULL;
696 }
697
3219e529 698 return -ENODATA;
7996a778
MC
699
700again:
701 if (unlikely(conn->suspend_tx))
3219e529 702 return -ENODATA;
7996a778 703
3219e529 704 return rc;
7996a778
MC
705}
706
707static void iscsi_xmitworker(void *data)
708{
709 struct iscsi_conn *conn = data;
3219e529 710 int rc;
7996a778
MC
711 /*
712 * serialize Xmit worker on a per-connection basis.
713 */
714 mutex_lock(&conn->xmitmutex);
3219e529
MC
715 do {
716 rc = iscsi_data_xmit(conn);
717 } while (rc >= 0 || rc == -EAGAIN);
7996a778
MC
718 mutex_unlock(&conn->xmitmutex);
719}
720
721enum {
722 FAILURE_BAD_HOST = 1,
723 FAILURE_SESSION_FAILED,
724 FAILURE_SESSION_FREED,
725 FAILURE_WINDOW_CLOSED,
60ecebf5 726 FAILURE_OOM,
7996a778 727 FAILURE_SESSION_TERMINATE,
656cffc9 728 FAILURE_SESSION_IN_RECOVERY,
7996a778
MC
729 FAILURE_SESSION_RECOVERY_TIMEOUT,
730};
731
732int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
733{
734 struct Scsi_Host *host;
735 int reason = 0;
736 struct iscsi_session *session;
737 struct iscsi_conn *conn;
738 struct iscsi_cmd_task *ctask = NULL;
739
740 sc->scsi_done = done;
741 sc->result = 0;
f47f2cf5 742 sc->SCp.ptr = NULL;
7996a778
MC
743
744 host = sc->device->host;
745 session = iscsi_hostdata(host->hostdata);
746
747 spin_lock(&session->lock);
748
656cffc9
MC
749 /*
750 * ISCSI_STATE_FAILED is a temp. state. The recovery
751 * code will decide what is best to do with command queued
752 * during this time
753 */
754 if (session->state != ISCSI_STATE_LOGGED_IN &&
755 session->state != ISCSI_STATE_FAILED) {
756 /*
757 * to handle the race between when we set the recovery state
758 * and block the session we requeue here (commands could
759 * be entering our queuecommand while a block is starting
760 * up because the block code is not locked)
761 */
762 if (session->state == ISCSI_STATE_IN_RECOVERY) {
763 reason = FAILURE_SESSION_IN_RECOVERY;
67a61114 764 goto reject;
7996a778 765 }
656cffc9
MC
766
767 if (session->state == ISCSI_STATE_RECOVERY_FAILED)
768 reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
769 else if (session->state == ISCSI_STATE_TERMINATE)
770 reason = FAILURE_SESSION_TERMINATE;
771 else
772 reason = FAILURE_SESSION_FREED;
7996a778
MC
773 goto fault;
774 }
775
776 /*
777 * Check for iSCSI window and take care of CmdSN wrap-around
778 */
779 if ((int)(session->max_cmdsn - session->cmdsn) < 0) {
780 reason = FAILURE_WINDOW_CLOSED;
781 goto reject;
782 }
783
784 conn = session->leadconn;
785
60ecebf5
MC
786 if (!__kfifo_get(session->cmdpool.queue, (void*)&ctask,
787 sizeof(void*))) {
788 reason = FAILURE_OOM;
789 goto reject;
790 }
7996a778
MC
791 sc->SCp.phase = session->age;
792 sc->SCp.ptr = (char *)ctask;
793
60ecebf5 794 atomic_set(&ctask->refcount, 1);
b6c395ed 795 ctask->state = ISCSI_TASK_PENDING;
7996a778
MC
796 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
b6c395ed 805 list_add_tail(&ctask->running, &conn->xmitqueue);
7996a778 806 debug_scsi(
f47f2cf5
MC
807 "ctask enq [%s cid %d sc %p cdb 0x%x itt 0x%x len %d cmdsn %d "
808 "win %d]\n",
7996a778 809 sc->sc_data_direction == DMA_TO_DEVICE ? "write" : "read",
f47f2cf5 810 conn->id, sc, sc->cmnd[0], ctask->itt, sc->request_bufflen,
7996a778
MC
811 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);
be2df72e 824 printk(KERN_ERR "iscsi: cmd 0x%x is not queued (%d)\n",
7996a778
MC
825 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 {
656cffc9
MC
865 BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
866 BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
7996a778 867
8d2860b3 868 nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
7996a778
MC
869 if (!__kfifo_get(session->mgmtpool.queue,
870 (void*)&mtask, sizeof(void*))) {
871 spin_unlock_bh(&session->lock);
872 return -ENOSPC;
873 }
874 }
875
876 /*
8d2860b3 877 * pre-format CmdSN for outgoing PDU.
7996a778
MC
878 */
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
7996a778
MC
890 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) {
656cffc9 940 session->state = ISCSI_STATE_RECOVERY_FAILED;
7996a778
MC
941 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 */
656cffc9 975 if (fail_session)
7996a778 976 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
7996a778
MC
977
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 ||
656cffc9 982 session->state == ISCSI_STATE_RECOVERY_FAILED);
7996a778
MC
983 if (signal_pending(current))
984 flush_signals(current);
985
986 spin_lock_bh(&session->lock);
987 if (session->state == ISCSI_STATE_LOGGED_IN)
be2df72e 988 printk(KERN_INFO "iscsi: host reset succeeded\n");
7996a778
MC
989 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 ||
656cffc9 1071 conn->tmabort_state != TMABORT_INITIAL);
7996a778
MC
1072 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 */
b6c395ed
MC
1083static 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;
1088
1089 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 }
7996a778 1099
b6c395ed
MC
1100 __kfifo_put(fifo, (void*)&task, sizeof(void*));
1101 }
1102 return NULL;
1103}
7996a778
MC
1104
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
7996a778
MC
1129 sc = ctask->sc;
1130 if (!sc)
1131 return;
7ea8b828
MC
1132 iscsi_ctask_mtask_cleanup(ctask);
1133
7996a778
MC
1134 sc->result = err;
1135 sc->resid = sc->request_bufflen;
60ecebf5 1136 __iscsi_put_ctask(ctask);
7996a778
MC
1137}
1138
1139int iscsi_eh_abort(struct scsi_cmnd *sc)
1140{
f47f2cf5
MC
1141 struct iscsi_cmd_task *ctask;
1142 struct iscsi_conn *conn;
1143 struct iscsi_session *session;
7996a778
MC
1144 int rc;
1145
f47f2cf5
MC
1146 /*
1147 * if session was ISCSI_STATE_IN_RECOVERY then we may not have
1148 * got the command.
1149 */
1150 if (!sc->SCp.ptr) {
1151 debug_scsi("sc never reached iscsi layer or it completed.\n");
1152 return SUCCESS;
1153 }
1154
1155 ctask = (struct iscsi_cmd_task *)sc->SCp.ptr;
1156 conn = ctask->conn;
1157 session = conn->session;
1158
7996a778
MC
1159 conn->eh_abort_cnt++;
1160 debug_scsi("aborting [sc %p itt 0x%x]\n", sc, ctask->itt);
1161
1162 mutex_lock(&conn->xmitmutex);
1163 spin_lock_bh(&session->lock);
1164
1165 /*
1166 * If we are not logged in or we have started a new session
1167 * then let the host reset code handle this
1168 */
1169 if (session->state != ISCSI_STATE_LOGGED_IN ||
1170 sc->SCp.phase != session->age)
1171 goto failed;
1172
1173 /* ctask completed before time out */
7ea8b828
MC
1174 if (!ctask->sc) {
1175 spin_unlock_bh(&session->lock);
1176 debug_scsi("sc completed while abort in progress\n");
1177 goto success_rel_mutex;
1178 }
7996a778
MC
1179
1180 /* what should we do here ? */
1181 if (conn->ctask == ctask) {
be2df72e
OG
1182 printk(KERN_INFO "iscsi: sc %p itt 0x%x partially sent. "
1183 "Failing abort\n", sc, ctask->itt);
7996a778
MC
1184 goto failed;
1185 }
1186
b6c395ed 1187 if (ctask->state == ISCSI_TASK_PENDING)
7ea8b828 1188 goto success_cleanup;
7996a778
MC
1189
1190 conn->tmabort_state = TMABORT_INITIAL;
1191
1192 spin_unlock_bh(&session->lock);
1193 rc = iscsi_exec_abort_task(sc, ctask);
1194 spin_lock_bh(&session->lock);
1195
7996a778
MC
1196 if (rc || sc->SCp.phase != session->age ||
1197 session->state != ISCSI_STATE_LOGGED_IN)
1198 goto failed;
7ea8b828 1199 iscsi_ctask_mtask_cleanup(ctask);
7996a778 1200
7ea8b828
MC
1201 switch (conn->tmabort_state) {
1202 case TMABORT_SUCCESS:
1203 goto success_cleanup;
1204 case TMABORT_NOT_FOUND:
1205 if (!ctask->sc) {
1206 /* ctask completed before tmf abort response */
1207 spin_unlock_bh(&session->lock);
1208 debug_scsi("sc completed while abort in progress\n");
1209 goto success_rel_mutex;
1210 }
1211 /* fall through */
1212 default:
1213 /* timedout or failed */
7996a778
MC
1214 spin_unlock_bh(&session->lock);
1215 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1216 spin_lock_bh(&session->lock);
1217 goto failed;
1218 }
1219
7ea8b828 1220success_cleanup:
7996a778
MC
1221 debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
1222 spin_unlock_bh(&session->lock);
1223
1224 /*
1225 * clean up task if aborted. we have the xmitmutex so grab
1226 * the recv lock as a writer
1227 */
1228 write_lock_bh(conn->recv_lock);
1229 spin_lock(&session->lock);
1230 fail_command(conn, ctask, DID_ABORT << 16);
1231 spin_unlock(&session->lock);
1232 write_unlock_bh(conn->recv_lock);
1233
7ea8b828 1234success_rel_mutex:
7996a778
MC
1235 mutex_unlock(&conn->xmitmutex);
1236 return SUCCESS;
1237
1238failed:
1239 spin_unlock_bh(&session->lock);
1240 mutex_unlock(&conn->xmitmutex);
1241
1242 debug_scsi("abort failed [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
1243 return FAILED;
1244}
1245EXPORT_SYMBOL_GPL(iscsi_eh_abort);
1246
1247int
1248iscsi_pool_init(struct iscsi_queue *q, int max, void ***items, int item_size)
1249{
1250 int i;
1251
1252 *items = kmalloc(max * sizeof(void*), GFP_KERNEL);
1253 if (*items == NULL)
1254 return -ENOMEM;
1255
1256 q->max = max;
1257 q->pool = kmalloc(max * sizeof(void*), GFP_KERNEL);
1258 if (q->pool == NULL) {
1259 kfree(*items);
1260 return -ENOMEM;
1261 }
1262
1263 q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
1264 GFP_KERNEL, NULL);
1265 if (q->queue == ERR_PTR(-ENOMEM)) {
1266 kfree(q->pool);
1267 kfree(*items);
1268 return -ENOMEM;
1269 }
1270
1271 for (i = 0; i < max; i++) {
1272 q->pool[i] = kmalloc(item_size, GFP_KERNEL);
1273 if (q->pool[i] == NULL) {
1274 int j;
1275
1276 for (j = 0; j < i; j++)
1277 kfree(q->pool[j]);
1278
1279 kfifo_free(q->queue);
1280 kfree(q->pool);
1281 kfree(*items);
1282 return -ENOMEM;
1283 }
1284 memset(q->pool[i], 0, item_size);
1285 (*items)[i] = q->pool[i];
1286 __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
1287 }
1288 return 0;
1289}
1290EXPORT_SYMBOL_GPL(iscsi_pool_init);
1291
1292void iscsi_pool_free(struct iscsi_queue *q, void **items)
1293{
1294 int i;
1295
1296 for (i = 0; i < q->max; i++)
1297 kfree(items[i]);
1298 kfree(q->pool);
1299 kfree(items);
1300}
1301EXPORT_SYMBOL_GPL(iscsi_pool_free);
1302
1303/*
1304 * iSCSI Session's hostdata organization:
1305 *
1306 * *------------------* <== hostdata_session(host->hostdata)
1307 * | ptr to class sess|
1308 * |------------------| <== iscsi_hostdata(host->hostdata)
1309 * | iscsi_session |
1310 * *------------------*
1311 */
1312
1313#define hostdata_privsize(_sz) (sizeof(unsigned long) + _sz + \
1314 _sz % sizeof(unsigned long))
1315
1316#define hostdata_session(_hostdata) (iscsi_ptr(*(unsigned long *)_hostdata))
1317
1318/**
1319 * iscsi_session_setup - create iscsi cls session and host and session
1320 * @scsit: scsi transport template
1321 * @iscsit: iscsi transport template
1322 * @initial_cmdsn: initial CmdSN
1323 * @hostno: host no allocated
1324 *
1325 * This can be used by software iscsi_transports that allocate
1326 * a session per scsi host.
1327 **/
1328struct iscsi_cls_session *
1329iscsi_session_setup(struct iscsi_transport *iscsit,
1330 struct scsi_transport_template *scsit,
1331 int cmd_task_size, int mgmt_task_size,
1332 uint32_t initial_cmdsn, uint32_t *hostno)
1333{
1334 struct Scsi_Host *shost;
1335 struct iscsi_session *session;
1336 struct iscsi_cls_session *cls_session;
1337 int cmd_i;
1338
1339 shost = scsi_host_alloc(iscsit->host_template,
1340 hostdata_privsize(sizeof(*session)));
1341 if (!shost)
1342 return NULL;
1343
1344 shost->max_id = 1;
1345 shost->max_channel = 0;
1346 shost->max_lun = iscsit->max_lun;
1347 shost->max_cmd_len = iscsit->max_cmd_len;
1348 shost->transportt = scsit;
1349 shost->transportt->create_work_queue = 1;
1350 *hostno = shost->host_no;
1351
1352 session = iscsi_hostdata(shost->hostdata);
1353 memset(session, 0, sizeof(struct iscsi_session));
1354 session->host = shost;
1355 session->state = ISCSI_STATE_FREE;
1356 session->mgmtpool_max = ISCSI_MGMT_CMDS_MAX;
1357 session->cmds_max = ISCSI_XMIT_CMDS_MAX;
1358 session->cmdsn = initial_cmdsn;
1359 session->exp_cmdsn = initial_cmdsn + 1;
1360 session->max_cmdsn = initial_cmdsn + 1;
1361 session->max_r2t = 1;
1362 session->tt = iscsit;
1363
1364 /* initialize SCSI PDU commands pool */
1365 if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
1366 (void***)&session->cmds,
1367 cmd_task_size + sizeof(struct iscsi_cmd_task)))
1368 goto cmdpool_alloc_fail;
1369
1370 /* pre-format cmds pool with ITT */
1371 for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
1372 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
1373
1374 if (cmd_task_size)
1375 ctask->dd_data = &ctask[1];
1376 ctask->itt = cmd_i;
b6c395ed 1377 INIT_LIST_HEAD(&ctask->running);
7996a778
MC
1378 }
1379
1380 spin_lock_init(&session->lock);
1381 INIT_LIST_HEAD(&session->connections);
1382
1383 /* initialize immediate command pool */
1384 if (iscsi_pool_init(&session->mgmtpool, session->mgmtpool_max,
1385 (void***)&session->mgmt_cmds,
1386 mgmt_task_size + sizeof(struct iscsi_mgmt_task)))
1387 goto mgmtpool_alloc_fail;
1388
1389
1390 /* pre-format immediate cmds pool with ITT */
1391 for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
1392 struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
1393
1394 if (mgmt_task_size)
1395 mtask->dd_data = &mtask[1];
1396 mtask->itt = ISCSI_MGMT_ITT_OFFSET + cmd_i;
b6c395ed 1397 INIT_LIST_HEAD(&mtask->running);
7996a778
MC
1398 }
1399
1400 if (scsi_add_host(shost, NULL))
1401 goto add_host_fail;
1402
f53a88da
MC
1403 if (!try_module_get(iscsit->owner))
1404 goto cls_session_fail;
1405
6a8a0d36 1406 cls_session = iscsi_create_session(shost, iscsit, 0);
7996a778 1407 if (!cls_session)
f53a88da 1408 goto module_put;
7996a778
MC
1409 *(unsigned long*)shost->hostdata = (unsigned long)cls_session;
1410
1411 return cls_session;
1412
f53a88da
MC
1413module_put:
1414 module_put(iscsit->owner);
7996a778
MC
1415cls_session_fail:
1416 scsi_remove_host(shost);
1417add_host_fail:
7996a778
MC
1418 iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
1419mgmtpool_alloc_fail:
1420 iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
1421cmdpool_alloc_fail:
1422 scsi_host_put(shost);
1423 return NULL;
1424}
1425EXPORT_SYMBOL_GPL(iscsi_session_setup);
1426
1427/**
1428 * iscsi_session_teardown - destroy session, host, and cls_session
1429 * shost: scsi host
1430 *
1431 * This can be used by software iscsi_transports that allocate
1432 * a session per scsi host.
1433 **/
1434void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
1435{
1436 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
1437 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
63f75cc8 1438 struct module *owner = cls_session->transport->owner;
7996a778
MC
1439
1440 scsi_remove_host(shost);
1441
7996a778
MC
1442 iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
1443 iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
1444
f3ff0c36
MC
1445 kfree(session->targetname);
1446
7996a778
MC
1447 iscsi_destroy_session(cls_session);
1448 scsi_host_put(shost);
63f75cc8 1449 module_put(owner);
7996a778
MC
1450}
1451EXPORT_SYMBOL_GPL(iscsi_session_teardown);
1452
1453/**
1454 * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
1455 * @cls_session: iscsi_cls_session
1456 * @conn_idx: cid
1457 **/
1458struct iscsi_cls_conn *
1459iscsi_conn_setup(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
1460{
1461 struct iscsi_session *session = class_to_transport_session(cls_session);
1462 struct iscsi_conn *conn;
1463 struct iscsi_cls_conn *cls_conn;
d36ab6f3 1464 char *data;
7996a778
MC
1465
1466 cls_conn = iscsi_create_conn(cls_session, conn_idx);
1467 if (!cls_conn)
1468 return NULL;
1469 conn = cls_conn->dd_data;
1470 memset(conn, 0, sizeof(*conn));
1471
1472 conn->session = session;
1473 conn->cls_conn = cls_conn;
1474 conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
1475 conn->id = conn_idx;
1476 conn->exp_statsn = 0;
1477 conn->tmabort_state = TMABORT_INITIAL;
1478 INIT_LIST_HEAD(&conn->run_list);
1479 INIT_LIST_HEAD(&conn->mgmt_run_list);
b6c395ed 1480 INIT_LIST_HEAD(&conn->xmitqueue);
7996a778
MC
1481
1482 /* initialize general immediate & non-immediate PDU commands queue */
1483 conn->immqueue = kfifo_alloc(session->mgmtpool_max * sizeof(void*),
1484 GFP_KERNEL, NULL);
1485 if (conn->immqueue == ERR_PTR(-ENOMEM))
1486 goto immqueue_alloc_fail;
1487
1488 conn->mgmtqueue = kfifo_alloc(session->mgmtpool_max * sizeof(void*),
1489 GFP_KERNEL, NULL);
1490 if (conn->mgmtqueue == ERR_PTR(-ENOMEM))
1491 goto mgmtqueue_alloc_fail;
1492
1493 INIT_WORK(&conn->xmitwork, iscsi_xmitworker, conn);
1494
1495 /* allocate login_mtask used for the login/text sequences */
1496 spin_lock_bh(&session->lock);
1497 if (!__kfifo_get(session->mgmtpool.queue,
1498 (void*)&conn->login_mtask,
1499 sizeof(void*))) {
1500 spin_unlock_bh(&session->lock);
1501 goto login_mtask_alloc_fail;
1502 }
1503 spin_unlock_bh(&session->lock);
1504
d36ab6f3
MC
1505 data = kmalloc(DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH, GFP_KERNEL);
1506 if (!data)
1507 goto login_mtask_data_alloc_fail;
c8dc1e52 1508 conn->login_mtask->data = conn->data = data;
d36ab6f3 1509
7996a778
MC
1510 init_timer(&conn->tmabort_timer);
1511 mutex_init(&conn->xmitmutex);
1512 init_waitqueue_head(&conn->ehwait);
1513
1514 return cls_conn;
1515
d36ab6f3
MC
1516login_mtask_data_alloc_fail:
1517 __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
1518 sizeof(void*));
7996a778
MC
1519login_mtask_alloc_fail:
1520 kfifo_free(conn->mgmtqueue);
1521mgmtqueue_alloc_fail:
1522 kfifo_free(conn->immqueue);
1523immqueue_alloc_fail:
7996a778
MC
1524 iscsi_destroy_conn(cls_conn);
1525 return NULL;
1526}
1527EXPORT_SYMBOL_GPL(iscsi_conn_setup);
1528
1529/**
1530 * iscsi_conn_teardown - teardown iscsi connection
1531 * cls_conn: iscsi class connection
1532 *
1533 * TODO: we may need to make this into a two step process
1534 * like scsi-mls remove + put host
1535 */
1536void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
1537{
1538 struct iscsi_conn *conn = cls_conn->dd_data;
1539 struct iscsi_session *session = conn->session;
1540 unsigned long flags;
1541
7996a778 1542 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
67a61114 1543 mutex_lock(&conn->xmitmutex);
7996a778
MC
1544
1545 spin_lock_bh(&session->lock);
1546 conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
1547 if (session->leadconn == conn) {
1548 /*
1549 * leading connection? then give up on recovery.
1550 */
1551 session->state = ISCSI_STATE_TERMINATE;
1552 wake_up(&conn->ehwait);
1553 }
1554 spin_unlock_bh(&session->lock);
1555
1556 mutex_unlock(&conn->xmitmutex);
1557
1558 /*
1559 * Block until all in-progress commands for this connection
1560 * time out or fail.
1561 */
1562 for (;;) {
1563 spin_lock_irqsave(session->host->host_lock, flags);
1564 if (!session->host->host_busy) { /* OK for ERL == 0 */
1565 spin_unlock_irqrestore(session->host->host_lock, flags);
1566 break;
1567 }
1568 spin_unlock_irqrestore(session->host->host_lock, flags);
1569 msleep_interruptible(500);
be2df72e
OG
1570 printk(KERN_INFO "iscsi: scsi conn_destroy(): host_busy %d "
1571 "host_failed %d\n", session->host->host_busy,
1572 session->host->host_failed);
7996a778
MC
1573 /*
1574 * force eh_abort() to unblock
1575 */
1576 wake_up(&conn->ehwait);
1577 }
1578
1579 spin_lock_bh(&session->lock);
c8dc1e52 1580 kfree(conn->data);
f3ff0c36 1581 kfree(conn->persistent_address);
7996a778
MC
1582 __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
1583 sizeof(void*));
1584 list_del(&conn->item);
1585 if (list_empty(&session->connections))
1586 session->leadconn = NULL;
1587 if (session->leadconn && session->leadconn == conn)
1588 session->leadconn = container_of(session->connections.next,
1589 struct iscsi_conn, item);
1590
1591 if (session->leadconn == NULL)
1592 /* no connections exits.. reset sequencing */
1593 session->cmdsn = session->max_cmdsn = session->exp_cmdsn = 1;
1594 spin_unlock_bh(&session->lock);
1595
7996a778
MC
1596 kfifo_free(conn->immqueue);
1597 kfifo_free(conn->mgmtqueue);
1598
1599 iscsi_destroy_conn(cls_conn);
1600}
1601EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
1602
1603int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
1604{
1605 struct iscsi_conn *conn = cls_conn->dd_data;
1606 struct iscsi_session *session = conn->session;
1607
ffd0436e 1608 if (!session) {
7996a778
MC
1609 printk(KERN_ERR "iscsi: can't start unbound connection\n");
1610 return -EPERM;
1611 }
1612
db98ccde
MC
1613 if ((session->imm_data_en || !session->initial_r2t_en) &&
1614 session->first_burst > session->max_burst) {
ffd0436e
MC
1615 printk("iscsi: invalid burst lengths: "
1616 "first_burst %d max_burst %d\n",
1617 session->first_burst, session->max_burst);
1618 return -EINVAL;
1619 }
1620
7996a778
MC
1621 spin_lock_bh(&session->lock);
1622 conn->c_stage = ISCSI_CONN_STARTED;
1623 session->state = ISCSI_STATE_LOGGED_IN;
1624
1625 switch(conn->stop_stage) {
1626 case STOP_CONN_RECOVER:
1627 /*
1628 * unblock eh_abort() if it is blocked. re-try all
1629 * commands after successful recovery
1630 */
7996a778
MC
1631 conn->stop_stage = 0;
1632 conn->tmabort_state = TMABORT_INITIAL;
1633 session->age++;
7996a778
MC
1634 spin_unlock_bh(&session->lock);
1635
1636 iscsi_unblock_session(session_to_cls(session));
1637 wake_up(&conn->ehwait);
1638 return 0;
1639 case STOP_CONN_TERM:
7996a778 1640 conn->stop_stage = 0;
7996a778
MC
1641 break;
1642 default:
1643 break;
1644 }
1645 spin_unlock_bh(&session->lock);
1646
1647 return 0;
1648}
1649EXPORT_SYMBOL_GPL(iscsi_conn_start);
1650
1651static void
1652flush_control_queues(struct iscsi_session *session, struct iscsi_conn *conn)
1653{
1654 struct iscsi_mgmt_task *mtask, *tmp;
1655
1656 /* handle pending */
1657 while (__kfifo_get(conn->immqueue, (void*)&mtask, sizeof(void*)) ||
1658 __kfifo_get(conn->mgmtqueue, (void*)&mtask, sizeof(void*))) {
1659 if (mtask == conn->login_mtask)
1660 continue;
1661 debug_scsi("flushing pending mgmt task itt 0x%x\n", mtask->itt);
1662 __kfifo_put(session->mgmtpool.queue, (void*)&mtask,
1663 sizeof(void*));
1664 }
1665
1666 /* handle running */
1667 list_for_each_entry_safe(mtask, tmp, &conn->mgmt_run_list, running) {
1668 debug_scsi("flushing running mgmt task itt 0x%x\n", mtask->itt);
ed2abc7f
MC
1669 list_del(&mtask->running);
1670
7996a778
MC
1671 if (mtask == conn->login_mtask)
1672 continue;
ed2abc7f 1673 __kfifo_put(session->mgmtpool.queue, (void*)&mtask,
7996a778
MC
1674 sizeof(void*));
1675 }
1676
1677 conn->mtask = NULL;
1678}
1679
1680/* Fail commands. Mutex and session lock held and recv side suspended */
1681static void fail_all_commands(struct iscsi_conn *conn)
1682{
1683 struct iscsi_cmd_task *ctask, *tmp;
1684
1685 /* flush pending */
b6c395ed 1686 list_for_each_entry_safe(ctask, tmp, &conn->xmitqueue, running) {
7996a778
MC
1687 debug_scsi("failing pending sc %p itt 0x%x\n", ctask->sc,
1688 ctask->itt);
1689 fail_command(conn, ctask, DID_BUS_BUSY << 16);
1690 }
1691
1692 /* fail all other running */
1693 list_for_each_entry_safe(ctask, tmp, &conn->run_list, running) {
1694 debug_scsi("failing in progress sc %p itt 0x%x\n",
1695 ctask->sc, ctask->itt);
1696 fail_command(conn, ctask, DID_BUS_BUSY << 16);
1697 }
1698
1699 conn->ctask = NULL;
1700}
1701
656cffc9
MC
1702static void iscsi_start_session_recovery(struct iscsi_session *session,
1703 struct iscsi_conn *conn, int flag)
7996a778 1704{
ed2abc7f
MC
1705 int old_stop_stage;
1706
7996a778 1707 spin_lock_bh(&session->lock);
ed2abc7f 1708 if (conn->stop_stage == STOP_CONN_TERM) {
7996a778
MC
1709 spin_unlock_bh(&session->lock);
1710 return;
1711 }
ed2abc7f
MC
1712
1713 /*
1714 * When this is called for the in_login state, we only want to clean
67a61114
MC
1715 * up the login task and connection. We do not need to block and set
1716 * the recovery state again
ed2abc7f 1717 */
67a61114
MC
1718 if (flag == STOP_CONN_TERM)
1719 session->state = ISCSI_STATE_TERMINATE;
1720 else if (conn->stop_stage != STOP_CONN_RECOVER)
1721 session->state = ISCSI_STATE_IN_RECOVERY;
ed2abc7f
MC
1722
1723 old_stop_stage = conn->stop_stage;
7996a778 1724 conn->stop_stage = flag;
67a61114
MC
1725 conn->c_stage = ISCSI_CONN_STOPPED;
1726 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
7996a778
MC
1727 spin_unlock_bh(&session->lock);
1728
1c83469d
MC
1729 write_lock_bh(conn->recv_lock);
1730 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
1731 write_unlock_bh(conn->recv_lock);
7996a778
MC
1732
1733 mutex_lock(&conn->xmitmutex);
7996a778
MC
1734 /*
1735 * for connection level recovery we should not calculate
1736 * header digest. conn->hdr_size used for optimization
1737 * in hdr_extract() and will be re-negotiated at
1738 * set_param() time.
1739 */
1740 if (flag == STOP_CONN_RECOVER) {
1741 conn->hdrdgst_en = 0;
1742 conn->datadgst_en = 0;
656cffc9 1743 if (session->state == ISCSI_STATE_IN_RECOVERY &&
67a61114
MC
1744 old_stop_stage != STOP_CONN_RECOVER) {
1745 debug_scsi("blocking session\n");
7996a778 1746 iscsi_block_session(session_to_cls(session));
67a61114 1747 }
7996a778 1748 }
656cffc9 1749
656cffc9
MC
1750 /*
1751 * flush queues.
1752 */
1753 spin_lock_bh(&session->lock);
1754 fail_all_commands(conn);
1755 flush_control_queues(session, conn);
1756 spin_unlock_bh(&session->lock);
1757
7996a778
MC
1758 mutex_unlock(&conn->xmitmutex);
1759}
7996a778
MC
1760
1761void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
1762{
1763 struct iscsi_conn *conn = cls_conn->dd_data;
1764 struct iscsi_session *session = conn->session;
1765
1766 switch (flag) {
1767 case STOP_CONN_RECOVER:
1768 case STOP_CONN_TERM:
1769 iscsi_start_session_recovery(session, conn, flag);
8d2860b3 1770 break;
7996a778 1771 default:
be2df72e 1772 printk(KERN_ERR "iscsi: invalid stop flag %d\n", flag);
7996a778
MC
1773 }
1774}
1775EXPORT_SYMBOL_GPL(iscsi_conn_stop);
1776
1777int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
1778 struct iscsi_cls_conn *cls_conn, int is_leading)
1779{
1780 struct iscsi_session *session = class_to_transport_session(cls_session);
1781 struct iscsi_conn *tmp = ERR_PTR(-EEXIST), *conn = cls_conn->dd_data;
1782
1783 /* lookup for existing connection */
1784 spin_lock_bh(&session->lock);
1785 list_for_each_entry(tmp, &session->connections, item) {
1786 if (tmp == conn) {
1787 if (conn->c_stage != ISCSI_CONN_STOPPED ||
1788 conn->stop_stage == STOP_CONN_TERM) {
be2df72e 1789 printk(KERN_ERR "iscsi: can't bind "
7996a778
MC
1790 "non-stopped connection (%d:%d)\n",
1791 conn->c_stage, conn->stop_stage);
1792 spin_unlock_bh(&session->lock);
1793 return -EIO;
1794 }
1795 break;
1796 }
1797 }
1798 if (tmp != conn) {
1799 /* bind new iSCSI connection to session */
1800 conn->session = session;
1801 list_add(&conn->item, &session->connections);
1802 }
1803 spin_unlock_bh(&session->lock);
1804
1805 if (is_leading)
1806 session->leadconn = conn;
1807
1808 /*
1809 * Unblock xmitworker(), Login Phase will pass through.
1810 */
1811 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
1812 clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1813 return 0;
1814}
1815EXPORT_SYMBOL_GPL(iscsi_conn_bind);
1816
a54a52ca
MC
1817
1818int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
1819 enum iscsi_param param, char *buf, int buflen)
1820{
1821 struct iscsi_conn *conn = cls_conn->dd_data;
1822 struct iscsi_session *session = conn->session;
1823 uint32_t value;
1824
1825 switch(param) {
1826 case ISCSI_PARAM_MAX_RECV_DLENGTH:
1827 sscanf(buf, "%d", &conn->max_recv_dlength);
1828 break;
1829 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
1830 sscanf(buf, "%d", &conn->max_xmit_dlength);
1831 break;
1832 case ISCSI_PARAM_HDRDGST_EN:
1833 sscanf(buf, "%d", &conn->hdrdgst_en);
1834 break;
1835 case ISCSI_PARAM_DATADGST_EN:
1836 sscanf(buf, "%d", &conn->datadgst_en);
1837 break;
1838 case ISCSI_PARAM_INITIAL_R2T_EN:
1839 sscanf(buf, "%d", &session->initial_r2t_en);
1840 break;
1841 case ISCSI_PARAM_MAX_R2T:
1842 sscanf(buf, "%d", &session->max_r2t);
1843 break;
1844 case ISCSI_PARAM_IMM_DATA_EN:
1845 sscanf(buf, "%d", &session->imm_data_en);
1846 break;
1847 case ISCSI_PARAM_FIRST_BURST:
1848 sscanf(buf, "%d", &session->first_burst);
1849 break;
1850 case ISCSI_PARAM_MAX_BURST:
1851 sscanf(buf, "%d", &session->max_burst);
1852 break;
1853 case ISCSI_PARAM_PDU_INORDER_EN:
1854 sscanf(buf, "%d", &session->pdu_inorder_en);
1855 break;
1856 case ISCSI_PARAM_DATASEQ_INORDER_EN:
1857 sscanf(buf, "%d", &session->dataseq_inorder_en);
1858 break;
1859 case ISCSI_PARAM_ERL:
1860 sscanf(buf, "%d", &session->erl);
1861 break;
1862 case ISCSI_PARAM_IFMARKER_EN:
1863 sscanf(buf, "%d", &value);
1864 BUG_ON(value);
1865 break;
1866 case ISCSI_PARAM_OFMARKER_EN:
1867 sscanf(buf, "%d", &value);
1868 BUG_ON(value);
1869 break;
1870 case ISCSI_PARAM_EXP_STATSN:
1871 sscanf(buf, "%u", &conn->exp_statsn);
1872 break;
1873 case ISCSI_PARAM_TARGET_NAME:
1874 /* this should not change between logins */
1875 if (session->targetname)
1876 break;
1877
1878 session->targetname = kstrdup(buf, GFP_KERNEL);
1879 if (!session->targetname)
1880 return -ENOMEM;
1881 break;
1882 case ISCSI_PARAM_TPGT:
1883 sscanf(buf, "%d", &session->tpgt);
1884 break;
1885 case ISCSI_PARAM_PERSISTENT_PORT:
1886 sscanf(buf, "%d", &conn->persistent_port);
1887 break;
1888 case ISCSI_PARAM_PERSISTENT_ADDRESS:
1889 /*
1890 * this is the address returned in discovery so it should
1891 * not change between logins.
1892 */
1893 if (conn->persistent_address)
1894 break;
1895
1896 conn->persistent_address = kstrdup(buf, GFP_KERNEL);
1897 if (!conn->persistent_address)
1898 return -ENOMEM;
1899 break;
1900 default:
1901 return -ENOSYS;
1902 }
1903
1904 return 0;
1905}
1906EXPORT_SYMBOL_GPL(iscsi_set_param);
1907
1908int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
1909 enum iscsi_param param, char *buf)
1910{
1911 struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
1912 struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
1913 int len;
1914
1915 switch(param) {
1916 case ISCSI_PARAM_INITIAL_R2T_EN:
1917 len = sprintf(buf, "%d\n", session->initial_r2t_en);
1918 break;
1919 case ISCSI_PARAM_MAX_R2T:
1920 len = sprintf(buf, "%hu\n", session->max_r2t);
1921 break;
1922 case ISCSI_PARAM_IMM_DATA_EN:
1923 len = sprintf(buf, "%d\n", session->imm_data_en);
1924 break;
1925 case ISCSI_PARAM_FIRST_BURST:
1926 len = sprintf(buf, "%u\n", session->first_burst);
1927 break;
1928 case ISCSI_PARAM_MAX_BURST:
1929 len = sprintf(buf, "%u\n", session->max_burst);
1930 break;
1931 case ISCSI_PARAM_PDU_INORDER_EN:
1932 len = sprintf(buf, "%d\n", session->pdu_inorder_en);
1933 break;
1934 case ISCSI_PARAM_DATASEQ_INORDER_EN:
1935 len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
1936 break;
1937 case ISCSI_PARAM_ERL:
1938 len = sprintf(buf, "%d\n", session->erl);
1939 break;
1940 case ISCSI_PARAM_TARGET_NAME:
1941 len = sprintf(buf, "%s\n", session->targetname);
1942 break;
1943 case ISCSI_PARAM_TPGT:
1944 len = sprintf(buf, "%d\n", session->tpgt);
1945 break;
1946 default:
1947 return -ENOSYS;
1948 }
1949
1950 return len;
1951}
1952EXPORT_SYMBOL_GPL(iscsi_session_get_param);
1953
1954int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
1955 enum iscsi_param param, char *buf)
1956{
1957 struct iscsi_conn *conn = cls_conn->dd_data;
1958 int len;
1959
1960 switch(param) {
1961 case ISCSI_PARAM_MAX_RECV_DLENGTH:
1962 len = sprintf(buf, "%u\n", conn->max_recv_dlength);
1963 break;
1964 case ISCSI_PARAM_MAX_XMIT_DLENGTH:
1965 len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
1966 break;
1967 case ISCSI_PARAM_HDRDGST_EN:
1968 len = sprintf(buf, "%d\n", conn->hdrdgst_en);
1969 break;
1970 case ISCSI_PARAM_DATADGST_EN:
1971 len = sprintf(buf, "%d\n", conn->datadgst_en);
1972 break;
1973 case ISCSI_PARAM_IFMARKER_EN:
1974 len = sprintf(buf, "%d\n", conn->ifmarker_en);
1975 break;
1976 case ISCSI_PARAM_OFMARKER_EN:
1977 len = sprintf(buf, "%d\n", conn->ofmarker_en);
1978 break;
1979 case ISCSI_PARAM_EXP_STATSN:
1980 len = sprintf(buf, "%u\n", conn->exp_statsn);
1981 break;
1982 case ISCSI_PARAM_PERSISTENT_PORT:
1983 len = sprintf(buf, "%d\n", conn->persistent_port);
1984 break;
1985 case ISCSI_PARAM_PERSISTENT_ADDRESS:
1986 len = sprintf(buf, "%s\n", conn->persistent_address);
1987 break;
1988 default:
1989 return -ENOSYS;
1990 }
1991
1992 return len;
1993}
1994EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
1995
7996a778
MC
1996MODULE_AUTHOR("Mike Christie");
1997MODULE_DESCRIPTION("iSCSI library functions");
1998MODULE_LICENSE("GPL");