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