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