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