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