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