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