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