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