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