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