scsi: revert "[SCSI] Get rid of scsi_cmnd->done"
[linux-2.6] / drivers / scsi / libiscsi.c
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/kfifo.h>
26 #include <linux/delay.h>
27 #include <asm/unaligned.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
40 struct iscsi_session *
41 class_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 }
46 EXPORT_SYMBOL_GPL(class_to_transport_session);
47
48 /* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
49 #define SNA32_CHECK 2147483648UL
50
51 static int iscsi_sna_lt(u32 n1, u32 n2)
52 {
53         return n1 != n2 && ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
54                             (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
55 }
56
57 /* Serial Number Arithmetic, 32 bits, less than, RFC1982 */
58 static int iscsi_sna_lte(u32 n1, u32 n2)
59 {
60         return n1 == n2 || ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) ||
61                             (n1 > n2 && (n2 - n1 < SNA32_CHECK)));
62 }
63
64 void
65 iscsi_update_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr)
66 {
67         uint32_t max_cmdsn = be32_to_cpu(hdr->max_cmdsn);
68         uint32_t exp_cmdsn = be32_to_cpu(hdr->exp_cmdsn);
69
70         /*
71          * standard specifies this check for when to update expected and
72          * max sequence numbers
73          */
74         if (iscsi_sna_lt(max_cmdsn, exp_cmdsn - 1))
75                 return;
76
77         if (exp_cmdsn != session->exp_cmdsn &&
78             !iscsi_sna_lt(exp_cmdsn, session->exp_cmdsn))
79                 session->exp_cmdsn = exp_cmdsn;
80
81         if (max_cmdsn != session->max_cmdsn &&
82             !iscsi_sna_lt(max_cmdsn, session->max_cmdsn)) {
83                 session->max_cmdsn = max_cmdsn;
84                 /*
85                  * if the window closed with IO queued, then kick the
86                  * xmit thread
87                  */
88                 if (!list_empty(&session->leadconn->xmitqueue) ||
89                     __kfifo_len(session->leadconn->mgmtqueue))
90                         scsi_queue_work(session->host,
91                                         &session->leadconn->xmitwork);
92         }
93 }
94 EXPORT_SYMBOL_GPL(iscsi_update_cmdsn);
95
96 void iscsi_prep_unsolicit_data_pdu(struct iscsi_cmd_task *ctask,
97                                    struct iscsi_data *hdr)
98 {
99         struct iscsi_conn *conn = ctask->conn;
100
101         memset(hdr, 0, sizeof(struct iscsi_data));
102         hdr->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
103         hdr->datasn = cpu_to_be32(ctask->unsol_datasn);
104         ctask->unsol_datasn++;
105         hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
106         memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
107
108         hdr->itt = ctask->hdr->itt;
109         hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
110         hdr->offset = cpu_to_be32(ctask->unsol_offset);
111
112         if (ctask->unsol_count > conn->max_xmit_dlength) {
113                 hton24(hdr->dlength, conn->max_xmit_dlength);
114                 ctask->data_count = conn->max_xmit_dlength;
115                 ctask->unsol_offset += ctask->data_count;
116                 hdr->flags = 0;
117         } else {
118                 hton24(hdr->dlength, ctask->unsol_count);
119                 ctask->data_count = ctask->unsol_count;
120                 hdr->flags = ISCSI_FLAG_CMD_FINAL;
121         }
122 }
123 EXPORT_SYMBOL_GPL(iscsi_prep_unsolicit_data_pdu);
124
125 /**
126  * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
127  * @ctask: iscsi cmd task
128  *
129  * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
130  * fields like dlength or final based on how much data it sends
131  */
132 static void iscsi_prep_scsi_cmd_pdu(struct iscsi_cmd_task *ctask)
133 {
134         struct iscsi_conn *conn = ctask->conn;
135         struct iscsi_session *session = conn->session;
136         struct iscsi_cmd *hdr = ctask->hdr;
137         struct scsi_cmnd *sc = ctask->sc;
138
139         hdr->opcode = ISCSI_OP_SCSI_CMD;
140         hdr->flags = ISCSI_ATTR_SIMPLE;
141         int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
142         hdr->itt = build_itt(ctask->itt, conn->id, session->age);
143         hdr->data_length = cpu_to_be32(scsi_bufflen(sc));
144         hdr->cmdsn = cpu_to_be32(session->cmdsn);
145         session->cmdsn++;
146         hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
147         memcpy(hdr->cdb, sc->cmnd, sc->cmd_len);
148         if (sc->cmd_len < MAX_COMMAND_SIZE)
149                 memset(&hdr->cdb[sc->cmd_len], 0,
150                         MAX_COMMAND_SIZE - sc->cmd_len);
151
152         ctask->data_count = 0;
153         ctask->imm_count = 0;
154         if (sc->sc_data_direction == DMA_TO_DEVICE) {
155                 hdr->flags |= ISCSI_FLAG_CMD_WRITE;
156                 /*
157                  * Write counters:
158                  *
159                  *      imm_count       bytes to be sent right after
160                  *                      SCSI PDU Header
161                  *
162                  *      unsol_count     bytes(as Data-Out) to be sent
163                  *                      without R2T ack right after
164                  *                      immediate data
165                  *
166                  *      r2t_data_count  bytes to be sent via R2T ack's
167                  *
168                  *      pad_count       bytes to be sent as zero-padding
169                  */
170                 ctask->unsol_count = 0;
171                 ctask->unsol_offset = 0;
172                 ctask->unsol_datasn = 0;
173
174                 if (session->imm_data_en) {
175                         if (scsi_bufflen(sc) >= session->first_burst)
176                                 ctask->imm_count = min(session->first_burst,
177                                                         conn->max_xmit_dlength);
178                         else
179                                 ctask->imm_count = min(scsi_bufflen(sc),
180                                                         conn->max_xmit_dlength);
181                         hton24(ctask->hdr->dlength, ctask->imm_count);
182                 } else
183                         zero_data(ctask->hdr->dlength);
184
185                 if (!session->initial_r2t_en) {
186                         ctask->unsol_count = min((session->first_burst),
187                                 (scsi_bufflen(sc))) - ctask->imm_count;
188                         ctask->unsol_offset = ctask->imm_count;
189                 }
190
191                 if (!ctask->unsol_count)
192                         /* No unsolicit Data-Out's */
193                         ctask->hdr->flags |= ISCSI_FLAG_CMD_FINAL;
194         } else {
195                 hdr->flags |= ISCSI_FLAG_CMD_FINAL;
196                 zero_data(hdr->dlength);
197
198                 if (sc->sc_data_direction == DMA_FROM_DEVICE)
199                         hdr->flags |= ISCSI_FLAG_CMD_READ;
200         }
201
202         conn->scsicmd_pdus_cnt++;
203
204         debug_scsi("iscsi prep [%s cid %d sc %p cdb 0x%x itt 0x%x len %d "
205                 "cmdsn %d win %d]\n",
206                 sc->sc_data_direction == DMA_TO_DEVICE ? "write" : "read",
207                 conn->id, sc, sc->cmnd[0], ctask->itt, scsi_bufflen(sc),
208                 session->cmdsn, session->max_cmdsn - session->exp_cmdsn + 1);
209 }
210
211 /**
212  * iscsi_complete_command - return command back to scsi-ml
213  * @ctask: iscsi cmd task
214  *
215  * Must be called with session lock.
216  * This function returns the scsi command to scsi-ml and returns
217  * the cmd task to the pool of available cmd tasks.
218  */
219 static void iscsi_complete_command(struct iscsi_cmd_task *ctask)
220 {
221         struct iscsi_session *session = ctask->conn->session;
222         struct scsi_cmnd *sc = ctask->sc;
223
224         ctask->state = ISCSI_TASK_COMPLETED;
225         ctask->sc = NULL;
226         /* SCSI eh reuses commands to verify us */
227         sc->SCp.ptr = NULL;
228         list_del_init(&ctask->running);
229         __kfifo_put(session->cmdpool.queue, (void*)&ctask, sizeof(void*));
230         sc->scsi_done(sc);
231 }
232
233 static void __iscsi_get_ctask(struct iscsi_cmd_task *ctask)
234 {
235         atomic_inc(&ctask->refcount);
236 }
237
238 static void __iscsi_put_ctask(struct iscsi_cmd_task *ctask)
239 {
240         if (atomic_dec_and_test(&ctask->refcount))
241                 iscsi_complete_command(ctask);
242 }
243
244 /**
245  * iscsi_cmd_rsp - SCSI Command Response processing
246  * @conn: iscsi connection
247  * @hdr: iscsi header
248  * @ctask: scsi command task
249  * @data: cmd data buffer
250  * @datalen: len of buffer
251  *
252  * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
253  * then completes the command and task.
254  **/
255 static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
256                                struct iscsi_cmd_task *ctask, char *data,
257                                int datalen)
258 {
259         struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)hdr;
260         struct iscsi_session *session = conn->session;
261         struct scsi_cmnd *sc = ctask->sc;
262
263         iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr);
264         conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
265
266         sc->result = (DID_OK << 16) | rhdr->cmd_status;
267
268         if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
269                 sc->result = DID_ERROR << 16;
270                 goto out;
271         }
272
273         if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
274                 uint16_t senselen;
275
276                 if (datalen < 2) {
277 invalid_datalen:
278                         printk(KERN_ERR "iscsi: Got CHECK_CONDITION but "
279                                "invalid data buffer size of %d\n", datalen);
280                         sc->result = DID_BAD_TARGET << 16;
281                         goto out;
282                 }
283
284                 senselen = be16_to_cpu(get_unaligned((__be16 *) data));
285                 if (datalen < senselen)
286                         goto invalid_datalen;
287
288                 memcpy(sc->sense_buffer, data + 2,
289                        min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
290                 debug_scsi("copied %d bytes of sense\n",
291                            min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE));
292         }
293
294         if (rhdr->flags & ISCSI_FLAG_CMD_UNDERFLOW) {
295                 int res_count = be32_to_cpu(rhdr->residual_count);
296
297                 if (res_count > 0 && res_count <= scsi_bufflen(sc))
298                         scsi_set_resid(sc, res_count);
299                 else
300                         sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
301         } else if (rhdr->flags & ISCSI_FLAG_CMD_BIDI_UNDERFLOW)
302                 sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
303         else if (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW)
304                 scsi_set_resid(sc, be32_to_cpu(rhdr->residual_count));
305
306 out:
307         debug_scsi("done [sc %lx res %d itt 0x%x]\n",
308                    (long)sc, sc->result, ctask->itt);
309         conn->scsirsp_pdus_cnt++;
310
311         __iscsi_put_ctask(ctask);
312 }
313
314 static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
315 {
316         struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
317
318         conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
319         conn->tmfrsp_pdus_cnt++;
320
321         if (conn->tmabort_state != TMABORT_INITIAL)
322                 return;
323
324         if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
325                 conn->tmabort_state = TMABORT_SUCCESS;
326         else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
327                 conn->tmabort_state = TMABORT_NOT_FOUND;
328         else
329                 conn->tmabort_state = TMABORT_FAILED;
330         wake_up(&conn->ehwait);
331 }
332
333 static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
334                                char *data, int datalen)
335 {
336         struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
337         struct iscsi_hdr rejected_pdu;
338         uint32_t itt;
339
340         conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
341
342         if (reject->reason == ISCSI_REASON_DATA_DIGEST_ERROR) {
343                 if (ntoh24(reject->dlength) > datalen)
344                         return ISCSI_ERR_PROTO;
345
346                 if (ntoh24(reject->dlength) >= sizeof(struct iscsi_hdr)) {
347                         memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
348                         itt = get_itt(rejected_pdu.itt);
349                         printk(KERN_ERR "itt 0x%x had pdu (op 0x%x) rejected "
350                                 "due to DataDigest error.\n", itt,
351                                 rejected_pdu.opcode);
352                 }
353         }
354         return 0;
355 }
356
357 /**
358  * __iscsi_complete_pdu - complete pdu
359  * @conn: iscsi conn
360  * @hdr: iscsi header
361  * @data: data buffer
362  * @datalen: len of data buffer
363  *
364  * Completes pdu processing by freeing any resources allocated at
365  * queuecommand or send generic. session lock must be held and verify
366  * itt must have been called.
367  */
368 int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
369                          char *data, int datalen)
370 {
371         struct iscsi_session *session = conn->session;
372         int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
373         struct iscsi_cmd_task *ctask;
374         struct iscsi_mgmt_task *mtask;
375         uint32_t itt;
376
377         if (hdr->itt != RESERVED_ITT)
378                 itt = get_itt(hdr->itt);
379         else
380                 itt = ~0U;
381
382         if (itt < session->cmds_max) {
383                 ctask = session->cmds[itt];
384
385                 debug_scsi("cmdrsp [op 0x%x cid %d itt 0x%x len %d]\n",
386                            opcode, conn->id, ctask->itt, datalen);
387
388                 switch(opcode) {
389                 case ISCSI_OP_SCSI_CMD_RSP:
390                         BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
391                         iscsi_scsi_cmd_rsp(conn, hdr, ctask, data,
392                                            datalen);
393                         break;
394                 case ISCSI_OP_SCSI_DATA_IN:
395                         BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
396                         if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
397                                 conn->scsirsp_pdus_cnt++;
398                                 __iscsi_put_ctask(ctask);
399                         }
400                         break;
401                 case ISCSI_OP_R2T:
402                         /* LLD handles this for now */
403                         break;
404                 default:
405                         rc = ISCSI_ERR_BAD_OPCODE;
406                         break;
407                 }
408         } else if (itt >= ISCSI_MGMT_ITT_OFFSET &&
409                    itt < ISCSI_MGMT_ITT_OFFSET + session->mgmtpool_max) {
410                 mtask = session->mgmt_cmds[itt - ISCSI_MGMT_ITT_OFFSET];
411
412                 debug_scsi("immrsp [op 0x%x cid %d itt 0x%x len %d]\n",
413                            opcode, conn->id, mtask->itt, datalen);
414
415                 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
416                 switch(opcode) {
417                 case ISCSI_OP_LOGOUT_RSP:
418                         if (datalen) {
419                                 rc = ISCSI_ERR_PROTO;
420                                 break;
421                         }
422                         conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
423                         /* fall through */
424                 case ISCSI_OP_LOGIN_RSP:
425                 case ISCSI_OP_TEXT_RSP:
426                         /*
427                          * login related PDU's exp_statsn is handled in
428                          * userspace
429                          */
430                         if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
431                                 rc = ISCSI_ERR_CONN_FAILED;
432                         list_del(&mtask->running);
433                         if (conn->login_mtask != mtask)
434                                 __kfifo_put(session->mgmtpool.queue,
435                                             (void*)&mtask, sizeof(void*));
436                         break;
437                 case ISCSI_OP_SCSI_TMFUNC_RSP:
438                         if (datalen) {
439                                 rc = ISCSI_ERR_PROTO;
440                                 break;
441                         }
442
443                         iscsi_tmf_rsp(conn, hdr);
444                         break;
445                 case ISCSI_OP_NOOP_IN:
446                         if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) || datalen) {
447                                 rc = ISCSI_ERR_PROTO;
448                                 break;
449                         }
450                         conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
451
452                         if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
453                                 rc = ISCSI_ERR_CONN_FAILED;
454                         list_del(&mtask->running);
455                         if (conn->login_mtask != mtask)
456                                 __kfifo_put(session->mgmtpool.queue,
457                                             (void*)&mtask, sizeof(void*));
458                         break;
459                 default:
460                         rc = ISCSI_ERR_BAD_OPCODE;
461                         break;
462                 }
463         } else if (itt == ~0U) {
464                 iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr);
465
466                 switch(opcode) {
467                 case ISCSI_OP_NOOP_IN:
468                         if (datalen) {
469                                 rc = ISCSI_ERR_PROTO;
470                                 break;
471                         }
472
473                         if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG))
474                                 break;
475
476                         if (iscsi_recv_pdu(conn->cls_conn, hdr, NULL, 0))
477                                 rc = ISCSI_ERR_CONN_FAILED;
478                         break;
479                 case ISCSI_OP_REJECT:
480                         rc = iscsi_handle_reject(conn, hdr, data, datalen);
481                         break;
482                 case ISCSI_OP_ASYNC_EVENT:
483                         conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
484                         if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
485                                 rc = ISCSI_ERR_CONN_FAILED;
486                         break;
487                 default:
488                         rc = ISCSI_ERR_BAD_OPCODE;
489                         break;
490                 }
491         } else
492                 rc = ISCSI_ERR_BAD_ITT;
493
494         return rc;
495 }
496 EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
497
498 int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
499                        char *data, int datalen)
500 {
501         int rc;
502
503         spin_lock(&conn->session->lock);
504         rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
505         spin_unlock(&conn->session->lock);
506         return rc;
507 }
508 EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
509
510 /* verify itt (itt encoding: age+cid+itt) */
511 int iscsi_verify_itt(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
512                      uint32_t *ret_itt)
513 {
514         struct iscsi_session *session = conn->session;
515         struct iscsi_cmd_task *ctask;
516         uint32_t itt;
517
518         if (hdr->itt != RESERVED_ITT) {
519                 if (((__force u32)hdr->itt & ISCSI_AGE_MASK) !=
520                     (session->age << ISCSI_AGE_SHIFT)) {
521                         printk(KERN_ERR "iscsi: received itt %x expected "
522                                 "session age (%x)\n", (__force u32)hdr->itt,
523                                 session->age & ISCSI_AGE_MASK);
524                         return ISCSI_ERR_BAD_ITT;
525                 }
526
527                 if (((__force u32)hdr->itt & ISCSI_CID_MASK) !=
528                     (conn->id << ISCSI_CID_SHIFT)) {
529                         printk(KERN_ERR "iscsi: received itt %x, expected "
530                                 "CID (%x)\n", (__force u32)hdr->itt, conn->id);
531                         return ISCSI_ERR_BAD_ITT;
532                 }
533                 itt = get_itt(hdr->itt);
534         } else
535                 itt = ~0U;
536
537         if (itt < session->cmds_max) {
538                 ctask = session->cmds[itt];
539
540                 if (!ctask->sc) {
541                         printk(KERN_INFO "iscsi: dropping ctask with "
542                                "itt 0x%x\n", ctask->itt);
543                         /* force drop */
544                         return ISCSI_ERR_NO_SCSI_CMD;
545                 }
546
547                 if (ctask->sc->SCp.phase != session->age) {
548                         printk(KERN_ERR "iscsi: ctask's session age %d, "
549                                 "expected %d\n", ctask->sc->SCp.phase,
550                                 session->age);
551                         return ISCSI_ERR_SESSION_FAILED;
552                 }
553         }
554
555         *ret_itt = itt;
556         return 0;
557 }
558 EXPORT_SYMBOL_GPL(iscsi_verify_itt);
559
560 void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
561 {
562         struct iscsi_session *session = conn->session;
563         unsigned long flags;
564
565         spin_lock_irqsave(&session->lock, flags);
566         if (session->state == ISCSI_STATE_FAILED) {
567                 spin_unlock_irqrestore(&session->lock, flags);
568                 return;
569         }
570
571         if (conn->stop_stage == 0)
572                 session->state = ISCSI_STATE_FAILED;
573         spin_unlock_irqrestore(&session->lock, flags);
574         set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
575         set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
576         iscsi_conn_error(conn->cls_conn, err);
577 }
578 EXPORT_SYMBOL_GPL(iscsi_conn_failure);
579
580 static void iscsi_prep_mtask(struct iscsi_conn *conn,
581                              struct iscsi_mgmt_task *mtask)
582 {
583         struct iscsi_session *session = conn->session;
584         struct iscsi_hdr *hdr = mtask->hdr;
585         struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
586
587         if (hdr->opcode != (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) &&
588             hdr->opcode != (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
589                 nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
590         /*
591          * pre-format CmdSN for outgoing PDU.
592          */
593         nop->cmdsn = cpu_to_be32(session->cmdsn);
594         if (hdr->itt != RESERVED_ITT) {
595                 hdr->itt = build_itt(mtask->itt, conn->id, session->age);
596                 /*
597                  * TODO: We always use immediate, so we never hit this.
598                  * If we start to send tmfs or nops as non-immediate then
599                  * we should start checking the cmdsn numbers for mgmt tasks.
600                  */
601                 if (conn->c_stage == ISCSI_CONN_STARTED &&
602                     !(hdr->opcode & ISCSI_OP_IMMEDIATE)) {
603                         session->queued_cmdsn++;
604                         session->cmdsn++;
605                 }
606         }
607
608         if (session->tt->init_mgmt_task)
609                 session->tt->init_mgmt_task(conn, mtask);
610
611         debug_scsi("mgmtpdu [op 0x%x hdr->itt 0x%x datalen %d]\n",
612                    hdr->opcode, hdr->itt, mtask->data_count);
613 }
614
615 static int iscsi_xmit_mtask(struct iscsi_conn *conn)
616 {
617         struct iscsi_hdr *hdr = conn->mtask->hdr;
618         int rc, was_logout = 0;
619
620         spin_unlock_bh(&conn->session->lock);
621         if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT) {
622                 conn->session->state = ISCSI_STATE_IN_RECOVERY;
623                 iscsi_block_session(session_to_cls(conn->session));
624                 was_logout = 1;
625         }
626         rc = conn->session->tt->xmit_mgmt_task(conn, conn->mtask);
627         spin_lock_bh(&conn->session->lock);
628         if (rc)
629                 return rc;
630
631         /* done with this in-progress mtask */
632         conn->mtask = NULL;
633
634         if (was_logout) {
635                 set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
636                 return -ENODATA;
637         }
638         return 0;
639 }
640
641 static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn)
642 {
643         struct iscsi_session *session = conn->session;
644
645         /*
646          * Check for iSCSI window and take care of CmdSN wrap-around
647          */
648         if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) {
649                 debug_scsi("iSCSI CmdSN closed. ExpCmdSn %u MaxCmdSN %u "
650                            "CmdSN %u/%u\n", session->exp_cmdsn,
651                            session->max_cmdsn, session->cmdsn,
652                            session->queued_cmdsn);
653                 return -ENOSPC;
654         }
655         return 0;
656 }
657
658 static int iscsi_xmit_ctask(struct iscsi_conn *conn)
659 {
660         struct iscsi_cmd_task *ctask = conn->ctask;
661         int rc = 0;
662
663         /*
664          * serialize with TMF AbortTask
665          */
666         if (ctask->state == ISCSI_TASK_ABORTING)
667                 goto done;
668
669         __iscsi_get_ctask(ctask);
670         spin_unlock_bh(&conn->session->lock);
671         rc = conn->session->tt->xmit_cmd_task(conn, ctask);
672         spin_lock_bh(&conn->session->lock);
673         __iscsi_put_ctask(ctask);
674
675 done:
676         if (!rc)
677                 /* done with this ctask */
678                 conn->ctask = NULL;
679         return rc;
680 }
681
682 /**
683  * iscsi_data_xmit - xmit any command into the scheduled connection
684  * @conn: iscsi connection
685  *
686  * Notes:
687  *      The function can return -EAGAIN in which case the caller must
688  *      re-schedule it again later or recover. '0' return code means
689  *      successful xmit.
690  **/
691 static int iscsi_data_xmit(struct iscsi_conn *conn)
692 {
693         int rc = 0;
694
695         spin_lock_bh(&conn->session->lock);
696         if (unlikely(conn->suspend_tx)) {
697                 debug_scsi("conn %d Tx suspended!\n", conn->id);
698                 spin_unlock_bh(&conn->session->lock);
699                 return -ENODATA;
700         }
701
702         if (conn->ctask) {
703                 rc = iscsi_xmit_ctask(conn);
704                 if (rc)
705                         goto again;
706         }
707
708         if (conn->mtask) {
709                 rc = iscsi_xmit_mtask(conn);
710                 if (rc)
711                         goto again;
712         }
713
714         /*
715          * process mgmt pdus like nops before commands since we should
716          * only have one nop-out as a ping from us and targets should not
717          * overflow us with nop-ins
718          */
719 check_mgmt:
720         while (__kfifo_get(conn->mgmtqueue, (void*)&conn->mtask,
721                            sizeof(void*))) {
722                 iscsi_prep_mtask(conn, conn->mtask);
723                 list_add_tail(&conn->mtask->running, &conn->mgmt_run_list);
724                 rc = iscsi_xmit_mtask(conn);
725                 if (rc)
726                         goto again;
727         }
728
729         /* process command queue */
730         while (!list_empty(&conn->xmitqueue)) {
731                 /*
732                  * iscsi tcp may readd the task to the xmitqueue to send
733                  * write data
734                  */
735                 conn->ctask = list_entry(conn->xmitqueue.next,
736                                          struct iscsi_cmd_task, running);
737                 switch (conn->ctask->state) {
738                 case ISCSI_TASK_ABORTING:
739                         break;
740                 case ISCSI_TASK_PENDING:
741                         iscsi_prep_scsi_cmd_pdu(conn->ctask);
742                         conn->session->tt->init_cmd_task(conn->ctask);
743                         /* fall through */
744                 default:
745                         conn->ctask->state = ISCSI_TASK_RUNNING;
746                         break;
747                 }
748                 list_move_tail(conn->xmitqueue.next, &conn->run_list);
749
750                 rc = iscsi_xmit_ctask(conn);
751                 if (rc)
752                         goto again;
753                 /*
754                  * we could continuously get new ctask requests so
755                  * we need to check the mgmt queue for nops that need to
756                  * be sent to aviod starvation
757                  */
758                 if (__kfifo_len(conn->mgmtqueue))
759                         goto check_mgmt;
760         }
761         spin_unlock_bh(&conn->session->lock);
762         return -ENODATA;
763
764 again:
765         if (unlikely(conn->suspend_tx))
766                 rc = -ENODATA;
767         spin_unlock_bh(&conn->session->lock);
768         return rc;
769 }
770
771 static void iscsi_xmitworker(struct work_struct *work)
772 {
773         struct iscsi_conn *conn =
774                 container_of(work, struct iscsi_conn, xmitwork);
775         int rc;
776         /*
777          * serialize Xmit worker on a per-connection basis.
778          */
779         do {
780                 rc = iscsi_data_xmit(conn);
781         } while (rc >= 0 || rc == -EAGAIN);
782 }
783
784 enum {
785         FAILURE_BAD_HOST = 1,
786         FAILURE_SESSION_FAILED,
787         FAILURE_SESSION_FREED,
788         FAILURE_WINDOW_CLOSED,
789         FAILURE_OOM,
790         FAILURE_SESSION_TERMINATE,
791         FAILURE_SESSION_IN_RECOVERY,
792         FAILURE_SESSION_RECOVERY_TIMEOUT,
793 };
794
795 int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
796 {
797         struct Scsi_Host *host;
798         int reason = 0;
799         struct iscsi_session *session;
800         struct iscsi_conn *conn;
801         struct iscsi_cmd_task *ctask = NULL;
802
803         sc->scsi_done = done;
804         sc->result = 0;
805         sc->SCp.ptr = NULL;
806
807         host = sc->device->host;
808         session = iscsi_hostdata(host->hostdata);
809
810         spin_lock(&session->lock);
811
812         /*
813          * ISCSI_STATE_FAILED is a temp. state. The recovery
814          * code will decide what is best to do with command queued
815          * during this time
816          */
817         if (session->state != ISCSI_STATE_LOGGED_IN &&
818             session->state != ISCSI_STATE_FAILED) {
819                 /*
820                  * to handle the race between when we set the recovery state
821                  * and block the session we requeue here (commands could
822                  * be entering our queuecommand while a block is starting
823                  * up because the block code is not locked)
824                  */
825                 if (session->state == ISCSI_STATE_IN_RECOVERY) {
826                         reason = FAILURE_SESSION_IN_RECOVERY;
827                         goto reject;
828                 }
829
830                 if (session->state == ISCSI_STATE_RECOVERY_FAILED)
831                         reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
832                 else if (session->state == ISCSI_STATE_TERMINATE)
833                         reason = FAILURE_SESSION_TERMINATE;
834                 else
835                         reason = FAILURE_SESSION_FREED;
836                 goto fault;
837         }
838
839         conn = session->leadconn;
840         if (!conn) {
841                 reason = FAILURE_SESSION_FREED;
842                 goto fault;
843         }
844
845         if (iscsi_check_cmdsn_window_closed(conn)) {
846                 reason = FAILURE_WINDOW_CLOSED;
847                 goto reject;
848         }
849
850         if (!__kfifo_get(session->cmdpool.queue, (void*)&ctask,
851                          sizeof(void*))) {
852                 reason = FAILURE_OOM;
853                 goto reject;
854         }
855         session->queued_cmdsn++;
856
857         sc->SCp.phase = session->age;
858         sc->SCp.ptr = (char *)ctask;
859
860         atomic_set(&ctask->refcount, 1);
861         ctask->state = ISCSI_TASK_PENDING;
862         ctask->mtask = NULL;
863         ctask->conn = conn;
864         ctask->sc = sc;
865         INIT_LIST_HEAD(&ctask->running);
866
867         list_add_tail(&ctask->running, &conn->xmitqueue);
868         spin_unlock(&session->lock);
869
870         scsi_queue_work(host, &conn->xmitwork);
871         return 0;
872
873 reject:
874         spin_unlock(&session->lock);
875         debug_scsi("cmd 0x%x rejected (%d)\n", sc->cmnd[0], reason);
876         return SCSI_MLQUEUE_HOST_BUSY;
877
878 fault:
879         spin_unlock(&session->lock);
880         printk(KERN_ERR "iscsi: cmd 0x%x is not queued (%d)\n",
881                sc->cmnd[0], reason);
882         sc->result = (DID_NO_CONNECT << 16);
883         scsi_set_resid(sc, scsi_bufflen(sc));
884         sc->scsi_done(sc);
885         return 0;
886 }
887 EXPORT_SYMBOL_GPL(iscsi_queuecommand);
888
889 int iscsi_change_queue_depth(struct scsi_device *sdev, int depth)
890 {
891         if (depth > ISCSI_MAX_CMD_PER_LUN)
892                 depth = ISCSI_MAX_CMD_PER_LUN;
893         scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
894         return sdev->queue_depth;
895 }
896 EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
897
898 static struct iscsi_mgmt_task *
899 __iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
900                       char *data, uint32_t data_size)
901 {
902         struct iscsi_session *session = conn->session;
903         struct iscsi_mgmt_task *mtask;
904
905         if (session->state == ISCSI_STATE_TERMINATE)
906                 return NULL;
907
908         if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) ||
909             hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
910                 /*
911                  * Login and Text are sent serially, in
912                  * request-followed-by-response sequence.
913                  * Same mtask can be used. Same ITT must be used.
914                  * Note that login_mtask is preallocated at conn_create().
915                  */
916                 mtask = conn->login_mtask;
917         else {
918                 BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
919                 BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
920
921                 if (!__kfifo_get(session->mgmtpool.queue,
922                                  (void*)&mtask, sizeof(void*)))
923                         return NULL;
924         }
925
926         if (data_size) {
927                 memcpy(mtask->data, data, data_size);
928                 mtask->data_count = data_size;
929         } else
930                 mtask->data_count = 0;
931
932         INIT_LIST_HEAD(&mtask->running);
933         memcpy(mtask->hdr, hdr, sizeof(struct iscsi_hdr));
934         __kfifo_put(conn->mgmtqueue, (void*)&mtask, sizeof(void*));
935         return mtask;
936 }
937
938 int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
939                         char *data, uint32_t data_size)
940 {
941         struct iscsi_conn *conn = cls_conn->dd_data;
942         struct iscsi_session *session = conn->session;
943         int err = 0;
944
945         spin_lock_bh(&session->lock);
946         if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size))
947                 err = -EPERM;
948         spin_unlock_bh(&session->lock);
949         scsi_queue_work(session->host, &conn->xmitwork);
950         return err;
951 }
952 EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
953
954 void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
955 {
956         struct iscsi_session *session = class_to_transport_session(cls_session);
957         struct iscsi_conn *conn = session->leadconn;
958
959         spin_lock_bh(&session->lock);
960         if (session->state != ISCSI_STATE_LOGGED_IN) {
961                 session->state = ISCSI_STATE_RECOVERY_FAILED;
962                 if (conn)
963                         wake_up(&conn->ehwait);
964         }
965         spin_unlock_bh(&session->lock);
966 }
967 EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
968
969 int iscsi_eh_host_reset(struct scsi_cmnd *sc)
970 {
971         struct Scsi_Host *host = sc->device->host;
972         struct iscsi_session *session = iscsi_hostdata(host->hostdata);
973         struct iscsi_conn *conn = session->leadconn;
974         int fail_session = 0;
975
976         spin_lock_bh(&session->lock);
977         if (session->state == ISCSI_STATE_TERMINATE) {
978 failed:
979                 debug_scsi("failing host reset: session terminated "
980                            "[CID %d age %d]\n", conn->id, session->age);
981                 spin_unlock_bh(&session->lock);
982                 return FAILED;
983         }
984
985         if (sc->SCp.phase == session->age) {
986                 debug_scsi("failing connection CID %d due to SCSI host reset\n",
987                            conn->id);
988                 fail_session = 1;
989         }
990         spin_unlock_bh(&session->lock);
991
992         /*
993          * we drop the lock here but the leadconn cannot be destoyed while
994          * we are in the scsi eh
995          */
996         if (fail_session)
997                 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
998
999         debug_scsi("iscsi_eh_host_reset wait for relogin\n");
1000         wait_event_interruptible(conn->ehwait,
1001                                  session->state == ISCSI_STATE_TERMINATE ||
1002                                  session->state == ISCSI_STATE_LOGGED_IN ||
1003                                  session->state == ISCSI_STATE_RECOVERY_FAILED);
1004         if (signal_pending(current))
1005                 flush_signals(current);
1006
1007         spin_lock_bh(&session->lock);
1008         if (session->state == ISCSI_STATE_LOGGED_IN)
1009                 printk(KERN_INFO "iscsi: host reset succeeded\n");
1010         else
1011                 goto failed;
1012         spin_unlock_bh(&session->lock);
1013
1014         return SUCCESS;
1015 }
1016 EXPORT_SYMBOL_GPL(iscsi_eh_host_reset);
1017
1018 static void iscsi_tmabort_timedout(unsigned long data)
1019 {
1020         struct iscsi_cmd_task *ctask = (struct iscsi_cmd_task *)data;
1021         struct iscsi_conn *conn = ctask->conn;
1022         struct iscsi_session *session = conn->session;
1023
1024         spin_lock(&session->lock);
1025         if (conn->tmabort_state == TMABORT_INITIAL) {
1026                 conn->tmabort_state = TMABORT_TIMEDOUT;
1027                 debug_scsi("tmabort timedout [sc %p itt 0x%x]\n",
1028                         ctask->sc, ctask->itt);
1029                 /* unblock eh_abort() */
1030                 wake_up(&conn->ehwait);
1031         }
1032         spin_unlock(&session->lock);
1033 }
1034
1035 static int iscsi_exec_abort_task(struct scsi_cmnd *sc,
1036                                  struct iscsi_cmd_task *ctask)
1037 {
1038         struct iscsi_conn *conn = ctask->conn;
1039         struct iscsi_session *session = conn->session;
1040         struct iscsi_tm *hdr = &conn->tmhdr;
1041
1042         /*
1043          * ctask timed out but session is OK requests must be serialized.
1044          */
1045         memset(hdr, 0, sizeof(struct iscsi_tm));
1046         hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
1047         hdr->flags = ISCSI_TM_FUNC_ABORT_TASK;
1048         hdr->flags |= ISCSI_FLAG_CMD_FINAL;
1049         memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
1050         hdr->rtt = ctask->hdr->itt;
1051         hdr->refcmdsn = ctask->hdr->cmdsn;
1052
1053         ctask->mtask = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr,
1054                                             NULL, 0);
1055         if (!ctask->mtask) {
1056                 spin_unlock_bh(&session->lock);
1057                 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1058                 spin_lock_bh(&session->lock)
1059                 debug_scsi("abort sent failure [itt 0x%x]\n", ctask->itt);
1060                 return -EPERM;
1061         }
1062         ctask->state = ISCSI_TASK_ABORTING;
1063
1064         debug_scsi("abort sent [itt 0x%x]\n", ctask->itt);
1065
1066         if (conn->tmabort_state == TMABORT_INITIAL) {
1067                 conn->tmfcmd_pdus_cnt++;
1068                 conn->tmabort_timer.expires = 20*HZ + jiffies;
1069                 conn->tmabort_timer.function = iscsi_tmabort_timedout;
1070                 conn->tmabort_timer.data = (unsigned long)ctask;
1071                 add_timer(&conn->tmabort_timer);
1072                 debug_scsi("abort set timeout [itt 0x%x]\n", ctask->itt);
1073         }
1074         spin_unlock_bh(&session->lock);
1075         mutex_unlock(&session->eh_mutex);
1076         scsi_queue_work(session->host, &conn->xmitwork);
1077
1078         /*
1079          * block eh thread until:
1080          *
1081          * 1) abort response
1082          * 2) abort timeout
1083          * 3) session is terminated or restarted or userspace has
1084          * given up on recovery
1085          */
1086         wait_event_interruptible(conn->ehwait,
1087                                  sc->SCp.phase != session->age ||
1088                                  session->state != ISCSI_STATE_LOGGED_IN ||
1089                                  conn->tmabort_state != TMABORT_INITIAL);
1090         if (signal_pending(current))
1091                 flush_signals(current);
1092         del_timer_sync(&conn->tmabort_timer);
1093         mutex_lock(&session->eh_mutex);
1094         spin_lock_bh(&session->lock);
1095         return 0;
1096 }
1097
1098 /*
1099  * session lock must be held
1100  */
1101 static struct iscsi_mgmt_task *
1102 iscsi_remove_mgmt_task(struct kfifo *fifo, uint32_t itt)
1103 {
1104         int i, nr_tasks = __kfifo_len(fifo) / sizeof(void*);
1105         struct iscsi_mgmt_task *task;
1106
1107         debug_scsi("searching %d tasks\n", nr_tasks);
1108
1109         for (i = 0; i < nr_tasks; i++) {
1110                 __kfifo_get(fifo, (void*)&task, sizeof(void*));
1111                 debug_scsi("check task %u\n", task->itt);
1112
1113                 if (task->itt == itt) {
1114                         debug_scsi("matched task\n");
1115                         return task;
1116                 }
1117
1118                 __kfifo_put(fifo, (void*)&task, sizeof(void*));
1119         }
1120         return NULL;
1121 }
1122
1123 static int iscsi_ctask_mtask_cleanup(struct iscsi_cmd_task *ctask)
1124 {
1125         struct iscsi_conn *conn = ctask->conn;
1126         struct iscsi_session *session = conn->session;
1127
1128         if (!ctask->mtask)
1129                 return -EINVAL;
1130
1131         if (!iscsi_remove_mgmt_task(conn->mgmtqueue, ctask->mtask->itt))
1132                 list_del(&ctask->mtask->running);
1133         __kfifo_put(session->mgmtpool.queue, (void*)&ctask->mtask,
1134                     sizeof(void*));
1135         ctask->mtask = NULL;
1136         return 0;
1137 }
1138
1139 /*
1140  * session lock must be held
1141  */
1142 static void fail_command(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
1143                          int err)
1144 {
1145         struct scsi_cmnd *sc;
1146
1147         sc = ctask->sc;
1148         if (!sc)
1149                 return;
1150
1151         if (ctask->state == ISCSI_TASK_PENDING)
1152                 /*
1153                  * cmd never made it to the xmit thread, so we should not count
1154                  * the cmd in the sequencing
1155                  */
1156                 conn->session->queued_cmdsn--;
1157         else
1158                 conn->session->tt->cleanup_cmd_task(conn, ctask);
1159         iscsi_ctask_mtask_cleanup(ctask);
1160
1161         sc->result = err;
1162         scsi_set_resid(sc, scsi_bufflen(sc));
1163         if (conn->ctask == ctask)
1164                 conn->ctask = NULL;
1165         /* release ref from queuecommand */
1166         __iscsi_put_ctask(ctask);
1167 }
1168
1169 static void iscsi_suspend_tx(struct iscsi_conn *conn)
1170 {
1171         set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1172         scsi_flush_work(conn->session->host);
1173 }
1174
1175 static void iscsi_start_tx(struct iscsi_conn *conn)
1176 {
1177         clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1178         scsi_queue_work(conn->session->host, &conn->xmitwork);
1179 }
1180
1181 int iscsi_eh_abort(struct scsi_cmnd *sc)
1182 {
1183         struct Scsi_Host *host = sc->device->host;
1184         struct iscsi_session *session = iscsi_hostdata(host->hostdata);
1185         struct iscsi_cmd_task *ctask;
1186         struct iscsi_conn *conn;
1187         int rc;
1188
1189         mutex_lock(&session->eh_mutex);
1190         spin_lock_bh(&session->lock);
1191         /*
1192          * if session was ISCSI_STATE_IN_RECOVERY then we may not have
1193          * got the command.
1194          */
1195         if (!sc->SCp.ptr) {
1196                 debug_scsi("sc never reached iscsi layer or it completed.\n");
1197                 spin_unlock_bh(&session->lock);
1198                 mutex_unlock(&session->eh_mutex);
1199                 return SUCCESS;
1200         }
1201
1202         ctask = (struct iscsi_cmd_task *)sc->SCp.ptr;
1203         conn = ctask->conn;
1204
1205         conn->eh_abort_cnt++;
1206         debug_scsi("aborting [sc %p itt 0x%x]\n", sc, ctask->itt);
1207
1208         /*
1209          * If we are not logged in or we have started a new session
1210          * then let the host reset code handle this
1211          */
1212         if (session->state != ISCSI_STATE_LOGGED_IN ||
1213             sc->SCp.phase != session->age)
1214                 goto failed;
1215
1216         /* ctask completed before time out */
1217         if (!ctask->sc) {
1218                 debug_scsi("sc completed while abort in progress\n");
1219                 goto success;
1220         }
1221
1222         /* what should we do here ? */
1223         if (conn->ctask == ctask) {
1224                 printk(KERN_INFO "iscsi: sc %p itt 0x%x partially sent. "
1225                        "Failing abort\n", sc, ctask->itt);
1226                 goto failed;
1227         }
1228
1229         if (ctask->state == ISCSI_TASK_PENDING) {
1230                 fail_command(conn, ctask, DID_ABORT << 16);
1231                 goto success;
1232         }
1233
1234         conn->tmabort_state = TMABORT_INITIAL;
1235         rc = iscsi_exec_abort_task(sc, ctask);
1236         if (rc || sc->SCp.phase != session->age ||
1237             session->state != ISCSI_STATE_LOGGED_IN)
1238                 goto failed;
1239         iscsi_ctask_mtask_cleanup(ctask);
1240
1241         switch (conn->tmabort_state) {
1242         case TMABORT_SUCCESS:
1243                 spin_unlock_bh(&session->lock);
1244                 iscsi_suspend_tx(conn);
1245                 /*
1246                  * clean up task if aborted. grab the recv lock as a writer
1247                  */
1248                 write_lock_bh(conn->recv_lock);
1249                 spin_lock(&session->lock);
1250                 fail_command(conn, ctask, DID_ABORT << 16);
1251                 spin_unlock(&session->lock);
1252                 write_unlock_bh(conn->recv_lock);
1253                 iscsi_start_tx(conn);
1254                 goto success_unlocked;
1255         case TMABORT_NOT_FOUND:
1256                 if (!ctask->sc) {
1257                         /* ctask completed before tmf abort response */
1258                         debug_scsi("sc completed while abort in progress\n");
1259                         goto success;
1260                 }
1261                 /* fall through */
1262         default:
1263                 /* timedout or failed */
1264                 spin_unlock_bh(&session->lock);
1265                 iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
1266                 goto failed_unlocked;
1267         }
1268
1269 success:
1270         spin_unlock_bh(&session->lock);
1271 success_unlocked:
1272         debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
1273         mutex_unlock(&session->eh_mutex);
1274         return SUCCESS;
1275
1276 failed:
1277         spin_unlock_bh(&session->lock);
1278 failed_unlocked:
1279         debug_scsi("abort failed [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
1280         mutex_unlock(&session->eh_mutex);
1281         return FAILED;
1282 }
1283 EXPORT_SYMBOL_GPL(iscsi_eh_abort);
1284
1285 int
1286 iscsi_pool_init(struct iscsi_queue *q, int max, void ***items, int item_size)
1287 {
1288         int i;
1289
1290         *items = kmalloc(max * sizeof(void*), GFP_KERNEL);
1291         if (*items == NULL)
1292                 return -ENOMEM;
1293
1294         q->max = max;
1295         q->pool = kmalloc(max * sizeof(void*), GFP_KERNEL);
1296         if (q->pool == NULL) {
1297                 kfree(*items);
1298                 return -ENOMEM;
1299         }
1300
1301         q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
1302                               GFP_KERNEL, NULL);
1303         if (q->queue == ERR_PTR(-ENOMEM)) {
1304                 kfree(q->pool);
1305                 kfree(*items);
1306                 return -ENOMEM;
1307         }
1308
1309         for (i = 0; i < max; i++) {
1310                 q->pool[i] = kmalloc(item_size, GFP_KERNEL);
1311                 if (q->pool[i] == NULL) {
1312                         int j;
1313
1314                         for (j = 0; j < i; j++)
1315                                 kfree(q->pool[j]);
1316
1317                         kfifo_free(q->queue);
1318                         kfree(q->pool);
1319                         kfree(*items);
1320                         return -ENOMEM;
1321                 }
1322                 memset(q->pool[i], 0, item_size);
1323                 (*items)[i] = q->pool[i];
1324                 __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
1325         }
1326         return 0;
1327 }
1328 EXPORT_SYMBOL_GPL(iscsi_pool_init);
1329
1330 void iscsi_pool_free(struct iscsi_queue *q, void **items)
1331 {
1332         int i;
1333
1334         for (i = 0; i < q->max; i++)
1335                 kfree(items[i]);
1336         kfree(q->pool);
1337         kfree(items);
1338 }
1339 EXPORT_SYMBOL_GPL(iscsi_pool_free);
1340
1341 /*
1342  * iSCSI Session's hostdata organization:
1343  *
1344  *    *------------------* <== hostdata_session(host->hostdata)
1345  *    | ptr to class sess|
1346  *    |------------------| <== iscsi_hostdata(host->hostdata)
1347  *    | iscsi_session    |
1348  *    *------------------*
1349  */
1350
1351 #define hostdata_privsize(_sz)  (sizeof(unsigned long) + _sz + \
1352                                  _sz % sizeof(unsigned long))
1353
1354 #define hostdata_session(_hostdata) (iscsi_ptr(*(unsigned long *)_hostdata))
1355
1356 /**
1357  * iscsi_session_setup - create iscsi cls session and host and session
1358  * @scsit: scsi transport template
1359  * @iscsit: iscsi transport template
1360  * @cmds_max: scsi host can queue
1361  * @qdepth: scsi host cmds per lun
1362  * @cmd_task_size: LLD ctask private data size
1363  * @mgmt_task_size: LLD mtask private data size
1364  * @initial_cmdsn: initial CmdSN
1365  * @hostno: host no allocated
1366  *
1367  * This can be used by software iscsi_transports that allocate
1368  * a session per scsi host.
1369  **/
1370 struct iscsi_cls_session *
1371 iscsi_session_setup(struct iscsi_transport *iscsit,
1372                     struct scsi_transport_template *scsit,
1373                     uint16_t cmds_max, uint16_t qdepth,
1374                     int cmd_task_size, int mgmt_task_size,
1375                     uint32_t initial_cmdsn, uint32_t *hostno)
1376 {
1377         struct Scsi_Host *shost;
1378         struct iscsi_session *session;
1379         struct iscsi_cls_session *cls_session;
1380         int cmd_i;
1381
1382         if (qdepth > ISCSI_MAX_CMD_PER_LUN || qdepth < 1) {
1383                 if (qdepth != 0)
1384                         printk(KERN_ERR "iscsi: invalid queue depth of %d. "
1385                               "Queue depth must be between 1 and %d.\n",
1386                               qdepth, ISCSI_MAX_CMD_PER_LUN);
1387                 qdepth = ISCSI_DEF_CMD_PER_LUN;
1388         }
1389
1390         if (cmds_max < 2 || (cmds_max & (cmds_max - 1)) ||
1391             cmds_max >= ISCSI_MGMT_ITT_OFFSET) {
1392                 if (cmds_max != 0)
1393                         printk(KERN_ERR "iscsi: invalid can_queue of %d. "
1394                                "can_queue must be a power of 2 and between "
1395                                "2 and %d - setting to %d.\n", cmds_max,
1396                                ISCSI_MGMT_ITT_OFFSET, ISCSI_DEF_XMIT_CMDS_MAX);
1397                 cmds_max = ISCSI_DEF_XMIT_CMDS_MAX;
1398         }
1399
1400         shost = scsi_host_alloc(iscsit->host_template,
1401                                 hostdata_privsize(sizeof(*session)));
1402         if (!shost)
1403                 return NULL;
1404
1405         /* the iscsi layer takes one task for reserve */
1406         shost->can_queue = cmds_max - 1;
1407         shost->cmd_per_lun = qdepth;
1408         shost->max_id = 1;
1409         shost->max_channel = 0;
1410         shost->max_lun = iscsit->max_lun;
1411         shost->max_cmd_len = iscsit->max_cmd_len;
1412         shost->transportt = scsit;
1413         shost->transportt->create_work_queue = 1;
1414         *hostno = shost->host_no;
1415
1416         session = iscsi_hostdata(shost->hostdata);
1417         memset(session, 0, sizeof(struct iscsi_session));
1418         session->host = shost;
1419         session->state = ISCSI_STATE_FREE;
1420         session->mgmtpool_max = ISCSI_MGMT_CMDS_MAX;
1421         session->cmds_max = cmds_max;
1422         session->queued_cmdsn = session->cmdsn = initial_cmdsn;
1423         session->exp_cmdsn = initial_cmdsn + 1;
1424         session->max_cmdsn = initial_cmdsn + 1;
1425         session->max_r2t = 1;
1426         session->tt = iscsit;
1427         mutex_init(&session->eh_mutex);
1428
1429         /* initialize SCSI PDU commands pool */
1430         if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
1431                             (void***)&session->cmds,
1432                             cmd_task_size + sizeof(struct iscsi_cmd_task)))
1433                 goto cmdpool_alloc_fail;
1434
1435         /* pre-format cmds pool with ITT */
1436         for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
1437                 struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
1438
1439                 if (cmd_task_size)
1440                         ctask->dd_data = &ctask[1];
1441                 ctask->itt = cmd_i;
1442                 INIT_LIST_HEAD(&ctask->running);
1443         }
1444
1445         spin_lock_init(&session->lock);
1446
1447         /* initialize immediate command pool */
1448         if (iscsi_pool_init(&session->mgmtpool, session->mgmtpool_max,
1449                            (void***)&session->mgmt_cmds,
1450                            mgmt_task_size + sizeof(struct iscsi_mgmt_task)))
1451                 goto mgmtpool_alloc_fail;
1452
1453
1454         /* pre-format immediate cmds pool with ITT */
1455         for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
1456                 struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
1457
1458                 if (mgmt_task_size)
1459                         mtask->dd_data = &mtask[1];
1460                 mtask->itt = ISCSI_MGMT_ITT_OFFSET + cmd_i;
1461                 INIT_LIST_HEAD(&mtask->running);
1462         }
1463
1464         if (scsi_add_host(shost, NULL))
1465                 goto add_host_fail;
1466
1467         if (!try_module_get(iscsit->owner))
1468                 goto cls_session_fail;
1469
1470         cls_session = iscsi_create_session(shost, iscsit, 0);
1471         if (!cls_session)
1472                 goto module_put;
1473         *(unsigned long*)shost->hostdata = (unsigned long)cls_session;
1474
1475         return cls_session;
1476
1477 module_put:
1478         module_put(iscsit->owner);
1479 cls_session_fail:
1480         scsi_remove_host(shost);
1481 add_host_fail:
1482         iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
1483 mgmtpool_alloc_fail:
1484         iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
1485 cmdpool_alloc_fail:
1486         scsi_host_put(shost);
1487         return NULL;
1488 }
1489 EXPORT_SYMBOL_GPL(iscsi_session_setup);
1490
1491 /**
1492  * iscsi_session_teardown - destroy session, host, and cls_session
1493  * shost: scsi host
1494  *
1495  * This can be used by software iscsi_transports that allocate
1496  * a session per scsi host.
1497  **/
1498 void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
1499 {
1500         struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
1501         struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
1502         struct module *owner = cls_session->transport->owner;
1503
1504         iscsi_unblock_session(cls_session);
1505         scsi_remove_host(shost);
1506
1507         iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
1508         iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
1509
1510         kfree(session->password);
1511         kfree(session->password_in);
1512         kfree(session->username);
1513         kfree(session->username_in);
1514         kfree(session->targetname);
1515         kfree(session->netdev);
1516         kfree(session->hwaddress);
1517         kfree(session->initiatorname);
1518
1519         iscsi_destroy_session(cls_session);
1520         scsi_host_put(shost);
1521         module_put(owner);
1522 }
1523 EXPORT_SYMBOL_GPL(iscsi_session_teardown);
1524
1525 /**
1526  * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
1527  * @cls_session: iscsi_cls_session
1528  * @conn_idx: cid
1529  **/
1530 struct iscsi_cls_conn *
1531 iscsi_conn_setup(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
1532 {
1533         struct iscsi_session *session = class_to_transport_session(cls_session);
1534         struct iscsi_conn *conn;
1535         struct iscsi_cls_conn *cls_conn;
1536         char *data;
1537
1538         cls_conn = iscsi_create_conn(cls_session, conn_idx);
1539         if (!cls_conn)
1540                 return NULL;
1541         conn = cls_conn->dd_data;
1542         memset(conn, 0, sizeof(*conn));
1543
1544         conn->session = session;
1545         conn->cls_conn = cls_conn;
1546         conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
1547         conn->id = conn_idx;
1548         conn->exp_statsn = 0;
1549         conn->tmabort_state = TMABORT_INITIAL;
1550         INIT_LIST_HEAD(&conn->run_list);
1551         INIT_LIST_HEAD(&conn->mgmt_run_list);
1552         INIT_LIST_HEAD(&conn->xmitqueue);
1553
1554         /* initialize general immediate & non-immediate PDU commands queue */
1555         conn->mgmtqueue = kfifo_alloc(session->mgmtpool_max * sizeof(void*),
1556                                         GFP_KERNEL, NULL);
1557         if (conn->mgmtqueue == ERR_PTR(-ENOMEM))
1558                 goto mgmtqueue_alloc_fail;
1559
1560         INIT_WORK(&conn->xmitwork, iscsi_xmitworker);
1561
1562         /* allocate login_mtask used for the login/text sequences */
1563         spin_lock_bh(&session->lock);
1564         if (!__kfifo_get(session->mgmtpool.queue,
1565                          (void*)&conn->login_mtask,
1566                          sizeof(void*))) {
1567                 spin_unlock_bh(&session->lock);
1568                 goto login_mtask_alloc_fail;
1569         }
1570         spin_unlock_bh(&session->lock);
1571
1572         data = kmalloc(ISCSI_DEF_MAX_RECV_SEG_LEN, GFP_KERNEL);
1573         if (!data)
1574                 goto login_mtask_data_alloc_fail;
1575         conn->login_mtask->data = conn->data = data;
1576
1577         init_timer(&conn->tmabort_timer);
1578         init_waitqueue_head(&conn->ehwait);
1579
1580         return cls_conn;
1581
1582 login_mtask_data_alloc_fail:
1583         __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
1584                     sizeof(void*));
1585 login_mtask_alloc_fail:
1586         kfifo_free(conn->mgmtqueue);
1587 mgmtqueue_alloc_fail:
1588         iscsi_destroy_conn(cls_conn);
1589         return NULL;
1590 }
1591 EXPORT_SYMBOL_GPL(iscsi_conn_setup);
1592
1593 /**
1594  * iscsi_conn_teardown - teardown iscsi connection
1595  * cls_conn: iscsi class connection
1596  *
1597  * TODO: we may need to make this into a two step process
1598  * like scsi-mls remove + put host
1599  */
1600 void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
1601 {
1602         struct iscsi_conn *conn = cls_conn->dd_data;
1603         struct iscsi_session *session = conn->session;
1604         unsigned long flags;
1605
1606         spin_lock_bh(&session->lock);
1607         set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1608         conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
1609         if (session->leadconn == conn) {
1610                 /*
1611                  * leading connection? then give up on recovery.
1612                  */
1613                 session->state = ISCSI_STATE_TERMINATE;
1614                 wake_up(&conn->ehwait);
1615         }
1616         spin_unlock_bh(&session->lock);
1617
1618         /*
1619          * Block until all in-progress commands for this connection
1620          * time out or fail.
1621          */
1622         for (;;) {
1623                 spin_lock_irqsave(session->host->host_lock, flags);
1624                 if (!session->host->host_busy) { /* OK for ERL == 0 */
1625                         spin_unlock_irqrestore(session->host->host_lock, flags);
1626                         break;
1627                 }
1628                 spin_unlock_irqrestore(session->host->host_lock, flags);
1629                 msleep_interruptible(500);
1630                 printk(KERN_INFO "iscsi: scsi conn_destroy(): host_busy %d "
1631                        "host_failed %d\n", session->host->host_busy,
1632                        session->host->host_failed);
1633                 /*
1634                  * force eh_abort() to unblock
1635                  */
1636                 wake_up(&conn->ehwait);
1637         }
1638
1639         /* flush queued up work because we free the connection below */
1640         scsi_flush_work(session->host);
1641
1642         spin_lock_bh(&session->lock);
1643         kfree(conn->data);
1644         kfree(conn->persistent_address);
1645         __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
1646                     sizeof(void*));
1647         if (session->leadconn == conn)
1648                 session->leadconn = NULL;
1649         spin_unlock_bh(&session->lock);
1650
1651         kfifo_free(conn->mgmtqueue);
1652
1653         iscsi_destroy_conn(cls_conn);
1654 }
1655 EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
1656
1657 int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
1658 {
1659         struct iscsi_conn *conn = cls_conn->dd_data;
1660         struct iscsi_session *session = conn->session;
1661
1662         if (!session) {
1663                 printk(KERN_ERR "iscsi: can't start unbound connection\n");
1664                 return -EPERM;
1665         }
1666
1667         if ((session->imm_data_en || !session->initial_r2t_en) &&
1668              session->first_burst > session->max_burst) {
1669                 printk("iscsi: invalid burst lengths: "
1670                        "first_burst %d max_burst %d\n",
1671                        session->first_burst, session->max_burst);
1672                 return -EINVAL;
1673         }
1674
1675         spin_lock_bh(&session->lock);
1676         conn->c_stage = ISCSI_CONN_STARTED;
1677         session->state = ISCSI_STATE_LOGGED_IN;
1678         session->queued_cmdsn = session->cmdsn;
1679
1680         switch(conn->stop_stage) {
1681         case STOP_CONN_RECOVER:
1682                 /*
1683                  * unblock eh_abort() if it is blocked. re-try all
1684                  * commands after successful recovery
1685                  */
1686                 conn->stop_stage = 0;
1687                 conn->tmabort_state = TMABORT_INITIAL;
1688                 session->age++;
1689                 spin_unlock_bh(&session->lock);
1690
1691                 iscsi_unblock_session(session_to_cls(session));
1692                 wake_up(&conn->ehwait);
1693                 return 0;
1694         case STOP_CONN_TERM:
1695                 conn->stop_stage = 0;
1696                 break;
1697         default:
1698                 break;
1699         }
1700         spin_unlock_bh(&session->lock);
1701
1702         return 0;
1703 }
1704 EXPORT_SYMBOL_GPL(iscsi_conn_start);
1705
1706 static void
1707 flush_control_queues(struct iscsi_session *session, struct iscsi_conn *conn)
1708 {
1709         struct iscsi_mgmt_task *mtask, *tmp;
1710
1711         /* handle pending */
1712         while (__kfifo_get(conn->mgmtqueue, (void*)&mtask, sizeof(void*))) {
1713                 if (mtask == conn->login_mtask)
1714                         continue;
1715                 debug_scsi("flushing pending mgmt task itt 0x%x\n", mtask->itt);
1716                 __kfifo_put(session->mgmtpool.queue, (void*)&mtask,
1717                             sizeof(void*));
1718         }
1719
1720         /* handle running */
1721         list_for_each_entry_safe(mtask, tmp, &conn->mgmt_run_list, running) {
1722                 debug_scsi("flushing running mgmt task itt 0x%x\n", mtask->itt);
1723                 list_del(&mtask->running);
1724
1725                 if (mtask == conn->login_mtask)
1726                         continue;
1727                 __kfifo_put(session->mgmtpool.queue, (void*)&mtask,
1728                            sizeof(void*));
1729         }
1730
1731         conn->mtask = NULL;
1732 }
1733
1734 /* Fail commands. Mutex and session lock held and recv side suspended */
1735 static void fail_all_commands(struct iscsi_conn *conn)
1736 {
1737         struct iscsi_cmd_task *ctask, *tmp;
1738
1739         /* flush pending */
1740         list_for_each_entry_safe(ctask, tmp, &conn->xmitqueue, running) {
1741                 debug_scsi("failing pending sc %p itt 0x%x\n", ctask->sc,
1742                            ctask->itt);
1743                 fail_command(conn, ctask, DID_BUS_BUSY << 16);
1744         }
1745
1746         /* fail all other running */
1747         list_for_each_entry_safe(ctask, tmp, &conn->run_list, running) {
1748                 debug_scsi("failing in progress sc %p itt 0x%x\n",
1749                            ctask->sc, ctask->itt);
1750                 fail_command(conn, ctask, DID_BUS_BUSY << 16);
1751         }
1752
1753         conn->ctask = NULL;
1754 }
1755
1756 static void iscsi_start_session_recovery(struct iscsi_session *session,
1757                                          struct iscsi_conn *conn, int flag)
1758 {
1759         int old_stop_stage;
1760
1761         mutex_lock(&session->eh_mutex);
1762         spin_lock_bh(&session->lock);
1763         if (conn->stop_stage == STOP_CONN_TERM) {
1764                 spin_unlock_bh(&session->lock);
1765                 mutex_unlock(&session->eh_mutex);
1766                 return;
1767         }
1768
1769         /*
1770          * The LLD either freed/unset the lock on us, or userspace called
1771          * stop but did not create a proper connection (connection was never
1772          * bound or it was unbound then stop was called).
1773          */
1774         if (!conn->recv_lock) {
1775                 spin_unlock_bh(&session->lock);
1776                 mutex_unlock(&session->eh_mutex);
1777                 return;
1778         }
1779
1780         /*
1781          * When this is called for the in_login state, we only want to clean
1782          * up the login task and connection. We do not need to block and set
1783          * the recovery state again
1784          */
1785         if (flag == STOP_CONN_TERM)
1786                 session->state = ISCSI_STATE_TERMINATE;
1787         else if (conn->stop_stage != STOP_CONN_RECOVER)
1788                 session->state = ISCSI_STATE_IN_RECOVERY;
1789
1790         old_stop_stage = conn->stop_stage;
1791         conn->stop_stage = flag;
1792         conn->c_stage = ISCSI_CONN_STOPPED;
1793         spin_unlock_bh(&session->lock);
1794
1795         iscsi_suspend_tx(conn);
1796
1797         write_lock_bh(conn->recv_lock);
1798         set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
1799         write_unlock_bh(conn->recv_lock);
1800
1801         /*
1802          * for connection level recovery we should not calculate
1803          * header digest. conn->hdr_size used for optimization
1804          * in hdr_extract() and will be re-negotiated at
1805          * set_param() time.
1806          */
1807         if (flag == STOP_CONN_RECOVER) {
1808                 conn->hdrdgst_en = 0;
1809                 conn->datadgst_en = 0;
1810                 if (session->state == ISCSI_STATE_IN_RECOVERY &&
1811                     old_stop_stage != STOP_CONN_RECOVER) {
1812                         debug_scsi("blocking session\n");
1813                         iscsi_block_session(session_to_cls(session));
1814                 }
1815         }
1816
1817         /*
1818          * flush queues.
1819          */
1820         spin_lock_bh(&session->lock);
1821         fail_all_commands(conn);
1822         flush_control_queues(session, conn);
1823         spin_unlock_bh(&session->lock);
1824         mutex_unlock(&session->eh_mutex);
1825 }
1826
1827 void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
1828 {
1829         struct iscsi_conn *conn = cls_conn->dd_data;
1830         struct iscsi_session *session = conn->session;
1831
1832         switch (flag) {
1833         case STOP_CONN_RECOVER:
1834         case STOP_CONN_TERM:
1835                 iscsi_start_session_recovery(session, conn, flag);
1836                 break;
1837         default:
1838                 printk(KERN_ERR "iscsi: invalid stop flag %d\n", flag);
1839         }
1840 }
1841 EXPORT_SYMBOL_GPL(iscsi_conn_stop);
1842
1843 int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
1844                     struct iscsi_cls_conn *cls_conn, int is_leading)
1845 {
1846         struct iscsi_session *session = class_to_transport_session(cls_session);
1847         struct iscsi_conn *conn = cls_conn->dd_data;
1848
1849         spin_lock_bh(&session->lock);
1850         if (is_leading)
1851                 session->leadconn = conn;
1852         spin_unlock_bh(&session->lock);
1853
1854         /*
1855          * Unblock xmitworker(), Login Phase will pass through.
1856          */
1857         clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
1858         clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
1859         return 0;
1860 }
1861 EXPORT_SYMBOL_GPL(iscsi_conn_bind);
1862
1863
1864 int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
1865                     enum iscsi_param param, char *buf, int buflen)
1866 {
1867         struct iscsi_conn *conn = cls_conn->dd_data;
1868         struct iscsi_session *session = conn->session;
1869         uint32_t value;
1870
1871         switch(param) {
1872         case ISCSI_PARAM_MAX_RECV_DLENGTH:
1873                 sscanf(buf, "%d", &conn->max_recv_dlength);
1874                 break;
1875         case ISCSI_PARAM_MAX_XMIT_DLENGTH:
1876                 sscanf(buf, "%d", &conn->max_xmit_dlength);
1877                 break;
1878         case ISCSI_PARAM_HDRDGST_EN:
1879                 sscanf(buf, "%d", &conn->hdrdgst_en);
1880                 break;
1881         case ISCSI_PARAM_DATADGST_EN:
1882                 sscanf(buf, "%d", &conn->datadgst_en);
1883                 break;
1884         case ISCSI_PARAM_INITIAL_R2T_EN:
1885                 sscanf(buf, "%d", &session->initial_r2t_en);
1886                 break;
1887         case ISCSI_PARAM_MAX_R2T:
1888                 sscanf(buf, "%d", &session->max_r2t);
1889                 break;
1890         case ISCSI_PARAM_IMM_DATA_EN:
1891                 sscanf(buf, "%d", &session->imm_data_en);
1892                 break;
1893         case ISCSI_PARAM_FIRST_BURST:
1894                 sscanf(buf, "%d", &session->first_burst);
1895                 break;
1896         case ISCSI_PARAM_MAX_BURST:
1897                 sscanf(buf, "%d", &session->max_burst);
1898                 break;
1899         case ISCSI_PARAM_PDU_INORDER_EN:
1900                 sscanf(buf, "%d", &session->pdu_inorder_en);
1901                 break;
1902         case ISCSI_PARAM_DATASEQ_INORDER_EN:
1903                 sscanf(buf, "%d", &session->dataseq_inorder_en);
1904                 break;
1905         case ISCSI_PARAM_ERL:
1906                 sscanf(buf, "%d", &session->erl);
1907                 break;
1908         case ISCSI_PARAM_IFMARKER_EN:
1909                 sscanf(buf, "%d", &value);
1910                 BUG_ON(value);
1911                 break;
1912         case ISCSI_PARAM_OFMARKER_EN:
1913                 sscanf(buf, "%d", &value);
1914                 BUG_ON(value);
1915                 break;
1916         case ISCSI_PARAM_EXP_STATSN:
1917                 sscanf(buf, "%u", &conn->exp_statsn);
1918                 break;
1919         case ISCSI_PARAM_USERNAME:
1920                 kfree(session->username);
1921                 session->username = kstrdup(buf, GFP_KERNEL);
1922                 if (!session->username)
1923                         return -ENOMEM;
1924                 break;
1925         case ISCSI_PARAM_USERNAME_IN:
1926                 kfree(session->username_in);
1927                 session->username_in = kstrdup(buf, GFP_KERNEL);
1928                 if (!session->username_in)
1929                         return -ENOMEM;
1930                 break;
1931         case ISCSI_PARAM_PASSWORD:
1932                 kfree(session->password);
1933                 session->password = kstrdup(buf, GFP_KERNEL);
1934                 if (!session->password)
1935                         return -ENOMEM;
1936                 break;
1937         case ISCSI_PARAM_PASSWORD_IN:
1938                 kfree(session->password_in);
1939                 session->password_in = kstrdup(buf, GFP_KERNEL);
1940                 if (!session->password_in)
1941                         return -ENOMEM;
1942                 break;
1943         case ISCSI_PARAM_TARGET_NAME:
1944                 /* this should not change between logins */
1945                 if (session->targetname)
1946                         break;
1947
1948                 session->targetname = kstrdup(buf, GFP_KERNEL);
1949                 if (!session->targetname)
1950                         return -ENOMEM;
1951                 break;
1952         case ISCSI_PARAM_TPGT:
1953                 sscanf(buf, "%d", &session->tpgt);
1954                 break;
1955         case ISCSI_PARAM_PERSISTENT_PORT:
1956                 sscanf(buf, "%d", &conn->persistent_port);
1957                 break;
1958         case ISCSI_PARAM_PERSISTENT_ADDRESS:
1959                 /*
1960                  * this is the address returned in discovery so it should
1961                  * not change between logins.
1962                  */
1963                 if (conn->persistent_address)
1964                         break;
1965
1966                 conn->persistent_address = kstrdup(buf, GFP_KERNEL);
1967                 if (!conn->persistent_address)
1968                         return -ENOMEM;
1969                 break;
1970         default:
1971                 return -ENOSYS;
1972         }
1973
1974         return 0;
1975 }
1976 EXPORT_SYMBOL_GPL(iscsi_set_param);
1977
1978 int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
1979                             enum iscsi_param param, char *buf)
1980 {
1981         struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
1982         struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
1983         int len;
1984
1985         switch(param) {
1986         case ISCSI_PARAM_INITIAL_R2T_EN:
1987                 len = sprintf(buf, "%d\n", session->initial_r2t_en);
1988                 break;
1989         case ISCSI_PARAM_MAX_R2T:
1990                 len = sprintf(buf, "%hu\n", session->max_r2t);
1991                 break;
1992         case ISCSI_PARAM_IMM_DATA_EN:
1993                 len = sprintf(buf, "%d\n", session->imm_data_en);
1994                 break;
1995         case ISCSI_PARAM_FIRST_BURST:
1996                 len = sprintf(buf, "%u\n", session->first_burst);
1997                 break;
1998         case ISCSI_PARAM_MAX_BURST:
1999                 len = sprintf(buf, "%u\n", session->max_burst);
2000                 break;
2001         case ISCSI_PARAM_PDU_INORDER_EN:
2002                 len = sprintf(buf, "%d\n", session->pdu_inorder_en);
2003                 break;
2004         case ISCSI_PARAM_DATASEQ_INORDER_EN:
2005                 len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
2006                 break;
2007         case ISCSI_PARAM_ERL:
2008                 len = sprintf(buf, "%d\n", session->erl);
2009                 break;
2010         case ISCSI_PARAM_TARGET_NAME:
2011                 len = sprintf(buf, "%s\n", session->targetname);
2012                 break;
2013         case ISCSI_PARAM_TPGT:
2014                 len = sprintf(buf, "%d\n", session->tpgt);
2015                 break;
2016         case ISCSI_PARAM_USERNAME:
2017                 len = sprintf(buf, "%s\n", session->username);
2018                 break;
2019         case ISCSI_PARAM_USERNAME_IN:
2020                 len = sprintf(buf, "%s\n", session->username_in);
2021                 break;
2022         case ISCSI_PARAM_PASSWORD:
2023                 len = sprintf(buf, "%s\n", session->password);
2024                 break;
2025         case ISCSI_PARAM_PASSWORD_IN:
2026                 len = sprintf(buf, "%s\n", session->password_in);
2027                 break;
2028         default:
2029                 return -ENOSYS;
2030         }
2031
2032         return len;
2033 }
2034 EXPORT_SYMBOL_GPL(iscsi_session_get_param);
2035
2036 int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
2037                          enum iscsi_param param, char *buf)
2038 {
2039         struct iscsi_conn *conn = cls_conn->dd_data;
2040         int len;
2041
2042         switch(param) {
2043         case ISCSI_PARAM_MAX_RECV_DLENGTH:
2044                 len = sprintf(buf, "%u\n", conn->max_recv_dlength);
2045                 break;
2046         case ISCSI_PARAM_MAX_XMIT_DLENGTH:
2047                 len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
2048                 break;
2049         case ISCSI_PARAM_HDRDGST_EN:
2050                 len = sprintf(buf, "%d\n", conn->hdrdgst_en);
2051                 break;
2052         case ISCSI_PARAM_DATADGST_EN:
2053                 len = sprintf(buf, "%d\n", conn->datadgst_en);
2054                 break;
2055         case ISCSI_PARAM_IFMARKER_EN:
2056                 len = sprintf(buf, "%d\n", conn->ifmarker_en);
2057                 break;
2058         case ISCSI_PARAM_OFMARKER_EN:
2059                 len = sprintf(buf, "%d\n", conn->ofmarker_en);
2060                 break;
2061         case ISCSI_PARAM_EXP_STATSN:
2062                 len = sprintf(buf, "%u\n", conn->exp_statsn);
2063                 break;
2064         case ISCSI_PARAM_PERSISTENT_PORT:
2065                 len = sprintf(buf, "%d\n", conn->persistent_port);
2066                 break;
2067         case ISCSI_PARAM_PERSISTENT_ADDRESS:
2068                 len = sprintf(buf, "%s\n", conn->persistent_address);
2069                 break;
2070         default:
2071                 return -ENOSYS;
2072         }
2073
2074         return len;
2075 }
2076 EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
2077
2078 int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2079                          char *buf)
2080 {
2081         struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
2082         int len;
2083
2084         switch (param) {
2085         case ISCSI_HOST_PARAM_NETDEV_NAME:
2086                 if (!session->netdev)
2087                         len = sprintf(buf, "%s\n", "default");
2088                 else
2089                         len = sprintf(buf, "%s\n", session->netdev);
2090                 break;
2091         case ISCSI_HOST_PARAM_HWADDRESS:
2092                 if (!session->hwaddress)
2093                         len = sprintf(buf, "%s\n", "default");
2094                 else
2095                         len = sprintf(buf, "%s\n", session->hwaddress);
2096                 break;
2097         case ISCSI_HOST_PARAM_INITIATOR_NAME:
2098                 if (!session->initiatorname)
2099                         len = sprintf(buf, "%s\n", "unknown");
2100                 else
2101                         len = sprintf(buf, "%s\n", session->initiatorname);
2102                 break;
2103
2104         default:
2105                 return -ENOSYS;
2106         }
2107
2108         return len;
2109 }
2110 EXPORT_SYMBOL_GPL(iscsi_host_get_param);
2111
2112 int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param,
2113                          char *buf, int buflen)
2114 {
2115         struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
2116
2117         switch (param) {
2118         case ISCSI_HOST_PARAM_NETDEV_NAME:
2119                 if (!session->netdev)
2120                         session->netdev = kstrdup(buf, GFP_KERNEL);
2121                 break;
2122         case ISCSI_HOST_PARAM_HWADDRESS:
2123                 if (!session->hwaddress)
2124                         session->hwaddress = kstrdup(buf, GFP_KERNEL);
2125                 break;
2126         case ISCSI_HOST_PARAM_INITIATOR_NAME:
2127                 if (!session->initiatorname)
2128                         session->initiatorname = kstrdup(buf, GFP_KERNEL);
2129                 break;
2130         default:
2131                 return -ENOSYS;
2132         }
2133
2134         return 0;
2135 }
2136 EXPORT_SYMBOL_GPL(iscsi_host_set_param);
2137
2138 MODULE_AUTHOR("Mike Christie");
2139 MODULE_DESCRIPTION("iSCSI library functions");
2140 MODULE_LICENSE("GPL");