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