Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 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> |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 25 | #include <linux/kfifo.h> |
| 26 | #include <linux/delay.h> |
vignesh babu | 1183657 | 2007-12-13 12:43:41 -0600 | [diff] [blame] | 27 | #include <linux/log2.h> |
Mike Christie | 8eb0053 | 2007-02-28 17:32:19 -0600 | [diff] [blame] | 28 | #include <asm/unaligned.h> |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 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 | |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 41 | /* Serial Number Arithmetic, 32 bits, less than, RFC1982 */ |
| 42 | #define SNA32_CHECK 2147483648UL |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 43 | |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 44 | static int iscsi_sna_lt(u32 n1, u32 n2) |
| 45 | { |
| 46 | return n1 != n2 && ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) || |
| 47 | (n1 > n2 && (n2 - n1 < SNA32_CHECK))); |
| 48 | } |
| 49 | |
| 50 | /* Serial Number Arithmetic, 32 bits, less than, RFC1982 */ |
| 51 | static int iscsi_sna_lte(u32 n1, u32 n2) |
| 52 | { |
| 53 | return n1 == n2 || ((n1 < n2 && (n2 - n1 < SNA32_CHECK)) || |
| 54 | (n1 > n2 && (n2 - n1 < SNA32_CHECK))); |
| 55 | } |
| 56 | |
| 57 | void |
| 58 | iscsi_update_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr) |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 59 | { |
| 60 | uint32_t max_cmdsn = be32_to_cpu(hdr->max_cmdsn); |
| 61 | uint32_t exp_cmdsn = be32_to_cpu(hdr->exp_cmdsn); |
| 62 | |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 63 | /* |
| 64 | * standard specifies this check for when to update expected and |
| 65 | * max sequence numbers |
| 66 | */ |
| 67 | if (iscsi_sna_lt(max_cmdsn, exp_cmdsn - 1)) |
| 68 | return; |
| 69 | |
| 70 | if (exp_cmdsn != session->exp_cmdsn && |
| 71 | !iscsi_sna_lt(exp_cmdsn, session->exp_cmdsn)) |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 72 | session->exp_cmdsn = exp_cmdsn; |
| 73 | |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 74 | if (max_cmdsn != session->max_cmdsn && |
| 75 | !iscsi_sna_lt(max_cmdsn, session->max_cmdsn)) { |
| 76 | session->max_cmdsn = max_cmdsn; |
| 77 | /* |
| 78 | * if the window closed with IO queued, then kick the |
| 79 | * xmit thread |
| 80 | */ |
| 81 | if (!list_empty(&session->leadconn->xmitqueue) || |
Mike Christie | 052d014 | 2008-05-21 15:54:05 -0500 | [diff] [blame] | 82 | !list_empty(&session->leadconn->mgmtqueue)) { |
| 83 | if (!(session->tt->caps & CAP_DATA_PATH_OFFLOAD)) |
| 84 | scsi_queue_work(session->host, |
| 85 | &session->leadconn->xmitwork); |
| 86 | } |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 87 | } |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 88 | } |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 89 | EXPORT_SYMBOL_GPL(iscsi_update_cmdsn); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 90 | |
Mike Christie | 577577d | 2008-12-02 00:32:05 -0600 | [diff] [blame] | 91 | /** |
| 92 | * iscsi_prep_data_out_pdu - initialize Data-Out |
| 93 | * @task: scsi command task |
| 94 | * @r2t: R2T info |
| 95 | * @hdr: iscsi data in pdu |
| 96 | * |
| 97 | * Notes: |
| 98 | * Initialize Data-Out within this R2T sequence and finds |
| 99 | * proper data_offset within this SCSI command. |
| 100 | * |
| 101 | * This function is called with connection lock taken. |
| 102 | **/ |
| 103 | void iscsi_prep_data_out_pdu(struct iscsi_task *task, struct iscsi_r2t_info *r2t, |
| 104 | struct iscsi_data *hdr) |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 105 | { |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 106 | struct iscsi_conn *conn = task->conn; |
Mike Christie | 577577d | 2008-12-02 00:32:05 -0600 | [diff] [blame] | 107 | unsigned int left = r2t->data_length - r2t->sent; |
| 108 | |
| 109 | task->hdr_len = sizeof(struct iscsi_data); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 110 | |
| 111 | memset(hdr, 0, sizeof(struct iscsi_data)); |
Mike Christie | 577577d | 2008-12-02 00:32:05 -0600 | [diff] [blame] | 112 | hdr->ttt = r2t->ttt; |
| 113 | hdr->datasn = cpu_to_be32(r2t->datasn); |
| 114 | r2t->datasn++; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 115 | hdr->opcode = ISCSI_OP_SCSI_DATA_OUT; |
Mike Christie | 577577d | 2008-12-02 00:32:05 -0600 | [diff] [blame] | 116 | memcpy(hdr->lun, task->lun, sizeof(hdr->lun)); |
| 117 | hdr->itt = task->hdr_itt; |
| 118 | hdr->exp_statsn = r2t->exp_statsn; |
| 119 | hdr->offset = cpu_to_be32(r2t->data_offset + r2t->sent); |
| 120 | if (left > conn->max_xmit_dlength) { |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 121 | hton24(hdr->dlength, conn->max_xmit_dlength); |
Mike Christie | 577577d | 2008-12-02 00:32:05 -0600 | [diff] [blame] | 122 | r2t->data_count = conn->max_xmit_dlength; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 123 | hdr->flags = 0; |
| 124 | } else { |
Mike Christie | 577577d | 2008-12-02 00:32:05 -0600 | [diff] [blame] | 125 | hton24(hdr->dlength, left); |
| 126 | r2t->data_count = left; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 127 | hdr->flags = ISCSI_FLAG_CMD_FINAL; |
| 128 | } |
Mike Christie | 577577d | 2008-12-02 00:32:05 -0600 | [diff] [blame] | 129 | conn->dataout_pdus_cnt++; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 130 | } |
Mike Christie | 577577d | 2008-12-02 00:32:05 -0600 | [diff] [blame] | 131 | EXPORT_SYMBOL_GPL(iscsi_prep_data_out_pdu); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 132 | |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 133 | static int iscsi_add_hdr(struct iscsi_task *task, unsigned len) |
Boaz Harrosh | 004d653 | 2007-12-13 12:43:23 -0600 | [diff] [blame] | 134 | { |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 135 | unsigned exp_len = task->hdr_len + len; |
Boaz Harrosh | 004d653 | 2007-12-13 12:43:23 -0600 | [diff] [blame] | 136 | |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 137 | if (exp_len > task->hdr_max) { |
Boaz Harrosh | 004d653 | 2007-12-13 12:43:23 -0600 | [diff] [blame] | 138 | WARN_ON(1); |
| 139 | return -EINVAL; |
| 140 | } |
| 141 | |
| 142 | WARN_ON(len & (ISCSI_PAD_LEN - 1)); /* caller must pad the AHS */ |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 143 | task->hdr_len = exp_len; |
Boaz Harrosh | 004d653 | 2007-12-13 12:43:23 -0600 | [diff] [blame] | 144 | return 0; |
| 145 | } |
| 146 | |
Boaz Harrosh | 38d1c06 | 2008-04-18 10:11:51 -0500 | [diff] [blame] | 147 | /* |
| 148 | * make an extended cdb AHS |
| 149 | */ |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 150 | static int iscsi_prep_ecdb_ahs(struct iscsi_task *task) |
Boaz Harrosh | 38d1c06 | 2008-04-18 10:11:51 -0500 | [diff] [blame] | 151 | { |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 152 | struct scsi_cmnd *cmd = task->sc; |
Boaz Harrosh | 38d1c06 | 2008-04-18 10:11:51 -0500 | [diff] [blame] | 153 | unsigned rlen, pad_len; |
| 154 | unsigned short ahslength; |
| 155 | struct iscsi_ecdb_ahdr *ecdb_ahdr; |
| 156 | int rc; |
| 157 | |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 158 | ecdb_ahdr = iscsi_next_hdr(task); |
Boaz Harrosh | 38d1c06 | 2008-04-18 10:11:51 -0500 | [diff] [blame] | 159 | rlen = cmd->cmd_len - ISCSI_CDB_SIZE; |
| 160 | |
| 161 | BUG_ON(rlen > sizeof(ecdb_ahdr->ecdb)); |
| 162 | ahslength = rlen + sizeof(ecdb_ahdr->reserved); |
| 163 | |
| 164 | pad_len = iscsi_padding(rlen); |
| 165 | |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 166 | rc = iscsi_add_hdr(task, sizeof(ecdb_ahdr->ahslength) + |
Boaz Harrosh | 38d1c06 | 2008-04-18 10:11:51 -0500 | [diff] [blame] | 167 | sizeof(ecdb_ahdr->ahstype) + ahslength + pad_len); |
| 168 | if (rc) |
| 169 | return rc; |
| 170 | |
| 171 | if (pad_len) |
| 172 | memset(&ecdb_ahdr->ecdb[rlen], 0, pad_len); |
| 173 | |
| 174 | ecdb_ahdr->ahslength = cpu_to_be16(ahslength); |
| 175 | ecdb_ahdr->ahstype = ISCSI_AHSTYPE_CDB; |
| 176 | ecdb_ahdr->reserved = 0; |
| 177 | memcpy(ecdb_ahdr->ecdb, cmd->cmnd + ISCSI_CDB_SIZE, rlen); |
| 178 | |
| 179 | debug_scsi("iscsi_prep_ecdb_ahs: varlen_cdb_len %d " |
| 180 | "rlen %d pad_len %d ahs_length %d iscsi_headers_size %u\n", |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 181 | cmd->cmd_len, rlen, pad_len, ahslength, task->hdr_len); |
Boaz Harrosh | 38d1c06 | 2008-04-18 10:11:51 -0500 | [diff] [blame] | 182 | |
| 183 | return 0; |
| 184 | } |
| 185 | |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 186 | static int iscsi_prep_bidi_ahs(struct iscsi_task *task) |
Boaz Harrosh | c07d444 | 2008-04-18 10:11:52 -0500 | [diff] [blame] | 187 | { |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 188 | struct scsi_cmnd *sc = task->sc; |
Boaz Harrosh | c07d444 | 2008-04-18 10:11:52 -0500 | [diff] [blame] | 189 | struct iscsi_rlength_ahdr *rlen_ahdr; |
| 190 | int rc; |
| 191 | |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 192 | rlen_ahdr = iscsi_next_hdr(task); |
| 193 | rc = iscsi_add_hdr(task, sizeof(*rlen_ahdr)); |
Boaz Harrosh | c07d444 | 2008-04-18 10:11:52 -0500 | [diff] [blame] | 194 | if (rc) |
| 195 | return rc; |
| 196 | |
| 197 | rlen_ahdr->ahslength = |
| 198 | cpu_to_be16(sizeof(rlen_ahdr->read_length) + |
| 199 | sizeof(rlen_ahdr->reserved)); |
| 200 | rlen_ahdr->ahstype = ISCSI_AHSTYPE_RLENGTH; |
| 201 | rlen_ahdr->reserved = 0; |
| 202 | rlen_ahdr->read_length = cpu_to_be32(scsi_in(sc)->length); |
| 203 | |
| 204 | debug_scsi("bidi-in rlen_ahdr->read_length(%d) " |
| 205 | "rlen_ahdr->ahslength(%d)\n", |
| 206 | be32_to_cpu(rlen_ahdr->read_length), |
| 207 | be16_to_cpu(rlen_ahdr->ahslength)); |
| 208 | return 0; |
| 209 | } |
| 210 | |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 211 | /** |
| 212 | * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 213 | * @task: iscsi task |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 214 | * |
| 215 | * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set |
| 216 | * fields like dlength or final based on how much data it sends |
| 217 | */ |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 218 | static int iscsi_prep_scsi_cmd_pdu(struct iscsi_task *task) |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 219 | { |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 220 | struct iscsi_conn *conn = task->conn; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 221 | struct iscsi_session *session = conn->session; |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 222 | struct scsi_cmnd *sc = task->sc; |
Mike Christie | 577577d | 2008-12-02 00:32:05 -0600 | [diff] [blame] | 223 | struct iscsi_cmd *hdr; |
Boaz Harrosh | 38d1c06 | 2008-04-18 10:11:51 -0500 | [diff] [blame] | 224 | unsigned hdrlength, cmd_len; |
Mike Christie | 262ef63 | 2008-12-02 00:32:13 -0600 | [diff] [blame] | 225 | itt_t itt; |
Boaz Harrosh | 004d653 | 2007-12-13 12:43:23 -0600 | [diff] [blame] | 226 | int rc; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 227 | |
Mike Christie | 2ff79d5 | 2008-12-02 00:32:14 -0600 | [diff] [blame] | 228 | rc = conn->session->tt->alloc_pdu(task, ISCSI_OP_SCSI_CMD); |
Mike Christie | 577577d | 2008-12-02 00:32:05 -0600 | [diff] [blame] | 229 | if (rc) |
| 230 | return rc; |
| 231 | hdr = (struct iscsi_cmd *) task->hdr; |
Mike Christie | 262ef63 | 2008-12-02 00:32:13 -0600 | [diff] [blame] | 232 | itt = hdr->itt; |
Mike Christie | 577577d | 2008-12-02 00:32:05 -0600 | [diff] [blame] | 233 | memset(hdr, 0, sizeof(*hdr)); |
| 234 | |
Mike Christie | 2ff79d5 | 2008-12-02 00:32:14 -0600 | [diff] [blame] | 235 | if (session->tt->parse_pdu_itt) |
| 236 | hdr->itt = task->hdr_itt = itt; |
| 237 | else |
| 238 | hdr->itt = task->hdr_itt = build_itt(task->itt, |
| 239 | task->conn->session->age); |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 240 | task->hdr_len = 0; |
| 241 | rc = iscsi_add_hdr(task, sizeof(*hdr)); |
Boaz Harrosh | 004d653 | 2007-12-13 12:43:23 -0600 | [diff] [blame] | 242 | if (rc) |
| 243 | return rc; |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 244 | hdr->opcode = ISCSI_OP_SCSI_CMD; |
| 245 | hdr->flags = ISCSI_ATTR_SIMPLE; |
| 246 | int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun); |
Mike Christie | 577577d | 2008-12-02 00:32:05 -0600 | [diff] [blame] | 247 | memcpy(task->lun, hdr->lun, sizeof(task->lun)); |
Mike Christie | 577577d | 2008-12-02 00:32:05 -0600 | [diff] [blame] | 248 | hdr->cmdsn = task->cmdsn = cpu_to_be32(session->cmdsn); |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 249 | session->cmdsn++; |
| 250 | hdr->exp_statsn = cpu_to_be32(conn->exp_statsn); |
Boaz Harrosh | 38d1c06 | 2008-04-18 10:11:51 -0500 | [diff] [blame] | 251 | cmd_len = sc->cmd_len; |
| 252 | if (cmd_len < ISCSI_CDB_SIZE) |
| 253 | memset(&hdr->cdb[cmd_len], 0, ISCSI_CDB_SIZE - cmd_len); |
| 254 | else if (cmd_len > ISCSI_CDB_SIZE) { |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 255 | rc = iscsi_prep_ecdb_ahs(task); |
Boaz Harrosh | 38d1c06 | 2008-04-18 10:11:51 -0500 | [diff] [blame] | 256 | if (rc) |
| 257 | return rc; |
| 258 | cmd_len = ISCSI_CDB_SIZE; |
| 259 | } |
| 260 | memcpy(hdr->cdb, sc->cmnd, cmd_len); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 261 | |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 262 | task->imm_count = 0; |
Boaz Harrosh | c07d444 | 2008-04-18 10:11:52 -0500 | [diff] [blame] | 263 | if (scsi_bidi_cmnd(sc)) { |
| 264 | hdr->flags |= ISCSI_FLAG_CMD_READ; |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 265 | rc = iscsi_prep_bidi_ahs(task); |
Boaz Harrosh | c07d444 | 2008-04-18 10:11:52 -0500 | [diff] [blame] | 266 | if (rc) |
| 267 | return rc; |
| 268 | } |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 269 | if (sc->sc_data_direction == DMA_TO_DEVICE) { |
Boaz Harrosh | c07d444 | 2008-04-18 10:11:52 -0500 | [diff] [blame] | 270 | unsigned out_len = scsi_out(sc)->length; |
Mike Christie | 577577d | 2008-12-02 00:32:05 -0600 | [diff] [blame] | 271 | struct iscsi_r2t_info *r2t = &task->unsol_r2t; |
| 272 | |
Boaz Harrosh | c07d444 | 2008-04-18 10:11:52 -0500 | [diff] [blame] | 273 | hdr->data_length = cpu_to_be32(out_len); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 274 | hdr->flags |= ISCSI_FLAG_CMD_WRITE; |
| 275 | /* |
| 276 | * Write counters: |
| 277 | * |
| 278 | * imm_count bytes to be sent right after |
| 279 | * SCSI PDU Header |
| 280 | * |
| 281 | * unsol_count bytes(as Data-Out) to be sent |
| 282 | * without R2T ack right after |
| 283 | * immediate data |
| 284 | * |
Mike Christie | 577577d | 2008-12-02 00:32:05 -0600 | [diff] [blame] | 285 | * r2t data_length bytes to be sent via R2T ack's |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 286 | * |
| 287 | * pad_count bytes to be sent as zero-padding |
| 288 | */ |
Mike Christie | 577577d | 2008-12-02 00:32:05 -0600 | [diff] [blame] | 289 | memset(r2t, 0, sizeof(*r2t)); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 290 | |
| 291 | if (session->imm_data_en) { |
Boaz Harrosh | c07d444 | 2008-04-18 10:11:52 -0500 | [diff] [blame] | 292 | if (out_len >= session->first_burst) |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 293 | task->imm_count = min(session->first_burst, |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 294 | conn->max_xmit_dlength); |
| 295 | else |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 296 | task->imm_count = min(out_len, |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 297 | conn->max_xmit_dlength); |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 298 | hton24(hdr->dlength, task->imm_count); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 299 | } else |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 300 | zero_data(hdr->dlength); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 301 | |
Mike Christie | ffd0436 | 2006-08-31 18:09:24 -0400 | [diff] [blame] | 302 | if (!session->initial_r2t_en) { |
Mike Christie | 577577d | 2008-12-02 00:32:05 -0600 | [diff] [blame] | 303 | r2t->data_length = min(session->first_burst, out_len) - |
| 304 | task->imm_count; |
| 305 | r2t->data_offset = task->imm_count; |
| 306 | r2t->ttt = cpu_to_be32(ISCSI_RESERVED_TAG); |
| 307 | r2t->exp_statsn = cpu_to_be32(conn->exp_statsn); |
Mike Christie | ffd0436 | 2006-08-31 18:09:24 -0400 | [diff] [blame] | 308 | } |
| 309 | |
Mike Christie | 577577d | 2008-12-02 00:32:05 -0600 | [diff] [blame] | 310 | if (!task->unsol_r2t.data_length) |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 311 | /* No unsolicit Data-Out's */ |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 312 | hdr->flags |= ISCSI_FLAG_CMD_FINAL; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 313 | } else { |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 314 | hdr->flags |= ISCSI_FLAG_CMD_FINAL; |
| 315 | zero_data(hdr->dlength); |
Boaz Harrosh | c07d444 | 2008-04-18 10:11:52 -0500 | [diff] [blame] | 316 | hdr->data_length = cpu_to_be32(scsi_in(sc)->length); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 317 | |
| 318 | if (sc->sc_data_direction == DMA_FROM_DEVICE) |
| 319 | hdr->flags |= ISCSI_FLAG_CMD_READ; |
| 320 | } |
| 321 | |
Boaz Harrosh | 004d653 | 2007-12-13 12:43:23 -0600 | [diff] [blame] | 322 | /* calculate size of additional header segments (AHSs) */ |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 323 | hdrlength = task->hdr_len - sizeof(*hdr); |
Boaz Harrosh | 004d653 | 2007-12-13 12:43:23 -0600 | [diff] [blame] | 324 | |
| 325 | WARN_ON(hdrlength & (ISCSI_PAD_LEN-1)); |
| 326 | hdrlength /= ISCSI_PAD_LEN; |
| 327 | |
| 328 | WARN_ON(hdrlength >= 256); |
| 329 | hdr->hlength = hdrlength & 0xFF; |
| 330 | |
Mike Christie | 577577d | 2008-12-02 00:32:05 -0600 | [diff] [blame] | 331 | if (session->tt->init_task && session->tt->init_task(task)) |
Mike Christie | 052d014 | 2008-05-21 15:54:05 -0500 | [diff] [blame] | 332 | return -EIO; |
| 333 | |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 334 | task->state = ISCSI_TASK_RUNNING; |
| 335 | list_move_tail(&task->running, &conn->run_list); |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 336 | |
Olaf Kirch | a8ac631 | 2007-12-13 12:43:35 -0600 | [diff] [blame] | 337 | conn->scsicmd_pdus_cnt++; |
Mike Christie | 3e5c28a | 2008-05-21 15:54:06 -0500 | [diff] [blame] | 338 | debug_scsi("iscsi prep [%s cid %d sc %p cdb 0x%x itt 0x%x len %d " |
| 339 | "bidi_len %d cmdsn %d win %d]\n", scsi_bidi_cmnd(sc) ? |
| 340 | "bidirectional" : sc->sc_data_direction == DMA_TO_DEVICE ? |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 341 | "write" : "read", conn->id, sc, sc->cmnd[0], task->itt, |
Mike Christie | 3e5c28a | 2008-05-21 15:54:06 -0500 | [diff] [blame] | 342 | scsi_bufflen(sc), |
| 343 | scsi_bidi_cmnd(sc) ? scsi_in(sc)->length : 0, |
| 344 | session->cmdsn, session->max_cmdsn - session->exp_cmdsn + 1); |
Boaz Harrosh | 004d653 | 2007-12-13 12:43:23 -0600 | [diff] [blame] | 345 | return 0; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 346 | } |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 347 | |
| 348 | /** |
Mike Christie | 3e5c28a | 2008-05-21 15:54:06 -0500 | [diff] [blame] | 349 | * iscsi_complete_command - finish a task |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 350 | * @task: iscsi cmd task |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 351 | * |
| 352 | * Must be called with session lock. |
Mike Christie | 3e5c28a | 2008-05-21 15:54:06 -0500 | [diff] [blame] | 353 | * This function returns the scsi command to scsi-ml or cleans |
| 354 | * up mgmt tasks then returns the task to the pool. |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 355 | */ |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 356 | static void iscsi_complete_command(struct iscsi_task *task) |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 357 | { |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 358 | struct iscsi_conn *conn = task->conn; |
Mike Christie | c1635cb | 2007-12-13 12:43:33 -0600 | [diff] [blame] | 359 | struct iscsi_session *session = conn->session; |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 360 | struct scsi_cmnd *sc = task->sc; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 361 | |
Mike Christie | 577577d | 2008-12-02 00:32:05 -0600 | [diff] [blame] | 362 | session->tt->cleanup_task(task); |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 363 | list_del_init(&task->running); |
| 364 | task->state = ISCSI_TASK_COMPLETED; |
| 365 | task->sc = NULL; |
Mike Christie | 3e5c28a | 2008-05-21 15:54:06 -0500 | [diff] [blame] | 366 | |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 367 | if (conn->task == task) |
| 368 | conn->task = NULL; |
Mike Christie | 3e5c28a | 2008-05-21 15:54:06 -0500 | [diff] [blame] | 369 | /* |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 370 | * login task is preallocated so do not free |
Mike Christie | 3e5c28a | 2008-05-21 15:54:06 -0500 | [diff] [blame] | 371 | */ |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 372 | if (conn->login_task == task) |
Mike Christie | 3e5c28a | 2008-05-21 15:54:06 -0500 | [diff] [blame] | 373 | return; |
| 374 | |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 375 | __kfifo_put(session->cmdpool.queue, (void*)&task, sizeof(void*)); |
Mike Christie | 052d014 | 2008-05-21 15:54:05 -0500 | [diff] [blame] | 376 | |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 377 | if (conn->ping_task == task) |
| 378 | conn->ping_task = NULL; |
Mike Christie | 3e5c28a | 2008-05-21 15:54:06 -0500 | [diff] [blame] | 379 | |
| 380 | if (sc) { |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 381 | task->sc = NULL; |
Mike Christie | 3e5c28a | 2008-05-21 15:54:06 -0500 | [diff] [blame] | 382 | /* SCSI eh reuses commands to verify us */ |
| 383 | sc->SCp.ptr = NULL; |
| 384 | /* |
| 385 | * queue command may call this to free the task, but |
| 386 | * not have setup the sc callback |
| 387 | */ |
| 388 | if (sc->scsi_done) |
| 389 | sc->scsi_done(sc); |
| 390 | } |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 391 | } |
| 392 | |
Mike Christie | 913e5bf | 2008-05-21 15:54:18 -0500 | [diff] [blame] | 393 | void __iscsi_get_task(struct iscsi_task *task) |
Mike Christie | 60ecebf | 2006-08-31 18:09:25 -0400 | [diff] [blame] | 394 | { |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 395 | atomic_inc(&task->refcount); |
Mike Christie | 60ecebf | 2006-08-31 18:09:25 -0400 | [diff] [blame] | 396 | } |
Mike Christie | 913e5bf | 2008-05-21 15:54:18 -0500 | [diff] [blame] | 397 | EXPORT_SYMBOL_GPL(__iscsi_get_task); |
Mike Christie | 60ecebf | 2006-08-31 18:09:25 -0400 | [diff] [blame] | 398 | |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 399 | static void __iscsi_put_task(struct iscsi_task *task) |
Mike Christie | 60ecebf | 2006-08-31 18:09:25 -0400 | [diff] [blame] | 400 | { |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 401 | if (atomic_dec_and_test(&task->refcount)) |
| 402 | iscsi_complete_command(task); |
Mike Christie | 60ecebf | 2006-08-31 18:09:25 -0400 | [diff] [blame] | 403 | } |
| 404 | |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 405 | void iscsi_put_task(struct iscsi_task *task) |
Mike Christie | 3e5c28a | 2008-05-21 15:54:06 -0500 | [diff] [blame] | 406 | { |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 407 | struct iscsi_session *session = task->conn->session; |
Mike Christie | 3e5c28a | 2008-05-21 15:54:06 -0500 | [diff] [blame] | 408 | |
| 409 | spin_lock_bh(&session->lock); |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 410 | __iscsi_put_task(task); |
Mike Christie | 3e5c28a | 2008-05-21 15:54:06 -0500 | [diff] [blame] | 411 | spin_unlock_bh(&session->lock); |
| 412 | } |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 413 | EXPORT_SYMBOL_GPL(iscsi_put_task); |
Mike Christie | 3e5c28a | 2008-05-21 15:54:06 -0500 | [diff] [blame] | 414 | |
Mike Christie | b3a7ea8 | 2007-12-13 12:43:26 -0600 | [diff] [blame] | 415 | /* |
| 416 | * session lock must be held |
| 417 | */ |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 418 | static void fail_command(struct iscsi_conn *conn, struct iscsi_task *task, |
Mike Christie | b3a7ea8 | 2007-12-13 12:43:26 -0600 | [diff] [blame] | 419 | int err) |
| 420 | { |
| 421 | struct scsi_cmnd *sc; |
| 422 | |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 423 | sc = task->sc; |
Mike Christie | b3a7ea8 | 2007-12-13 12:43:26 -0600 | [diff] [blame] | 424 | if (!sc) |
| 425 | return; |
| 426 | |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 427 | if (task->state == ISCSI_TASK_PENDING) |
Mike Christie | b3a7ea8 | 2007-12-13 12:43:26 -0600 | [diff] [blame] | 428 | /* |
| 429 | * cmd never made it to the xmit thread, so we should not count |
| 430 | * the cmd in the sequencing |
| 431 | */ |
| 432 | conn->session->queued_cmdsn--; |
Mike Christie | b3a7ea8 | 2007-12-13 12:43:26 -0600 | [diff] [blame] | 433 | |
| 434 | sc->result = err; |
Boaz Harrosh | c07d444 | 2008-04-18 10:11:52 -0500 | [diff] [blame] | 435 | if (!scsi_bidi_cmnd(sc)) |
| 436 | scsi_set_resid(sc, scsi_bufflen(sc)); |
| 437 | else { |
| 438 | scsi_out(sc)->resid = scsi_out(sc)->length; |
| 439 | scsi_in(sc)->resid = scsi_in(sc)->length; |
| 440 | } |
Mike Christie | 3e5c28a | 2008-05-21 15:54:06 -0500 | [diff] [blame] | 441 | |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 442 | if (conn->task == task) |
| 443 | conn->task = NULL; |
Mike Christie | b3a7ea8 | 2007-12-13 12:43:26 -0600 | [diff] [blame] | 444 | /* release ref from queuecommand */ |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 445 | __iscsi_put_task(task); |
Mike Christie | b3a7ea8 | 2007-12-13 12:43:26 -0600 | [diff] [blame] | 446 | } |
| 447 | |
Mike Christie | 3e5c28a | 2008-05-21 15:54:06 -0500 | [diff] [blame] | 448 | static int iscsi_prep_mgmt_task(struct iscsi_conn *conn, |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 449 | struct iscsi_task *task) |
Mike Christie | 052d014 | 2008-05-21 15:54:05 -0500 | [diff] [blame] | 450 | { |
| 451 | struct iscsi_session *session = conn->session; |
Mike Christie | 577577d | 2008-12-02 00:32:05 -0600 | [diff] [blame] | 452 | struct iscsi_hdr *hdr = task->hdr; |
Mike Christie | 052d014 | 2008-05-21 15:54:05 -0500 | [diff] [blame] | 453 | struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr; |
| 454 | |
| 455 | if (conn->session->state == ISCSI_STATE_LOGGING_OUT) |
| 456 | return -ENOTCONN; |
| 457 | |
| 458 | if (hdr->opcode != (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) && |
| 459 | hdr->opcode != (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE)) |
| 460 | nop->exp_statsn = cpu_to_be32(conn->exp_statsn); |
| 461 | /* |
| 462 | * pre-format CmdSN for outgoing PDU. |
| 463 | */ |
| 464 | nop->cmdsn = cpu_to_be32(session->cmdsn); |
| 465 | if (hdr->itt != RESERVED_ITT) { |
Mike Christie | 052d014 | 2008-05-21 15:54:05 -0500 | [diff] [blame] | 466 | /* |
| 467 | * TODO: We always use immediate, so we never hit this. |
| 468 | * If we start to send tmfs or nops as non-immediate then |
| 469 | * we should start checking the cmdsn numbers for mgmt tasks. |
| 470 | */ |
| 471 | if (conn->c_stage == ISCSI_CONN_STARTED && |
| 472 | !(hdr->opcode & ISCSI_OP_IMMEDIATE)) { |
| 473 | session->queued_cmdsn++; |
| 474 | session->cmdsn++; |
| 475 | } |
| 476 | } |
| 477 | |
Mike Christie | ae15f80 | 2008-12-02 00:32:15 -0600 | [diff] [blame] | 478 | if (session->tt->init_task && session->tt->init_task(task)) |
| 479 | return -EIO; |
Mike Christie | 052d014 | 2008-05-21 15:54:05 -0500 | [diff] [blame] | 480 | |
| 481 | if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT) |
| 482 | session->state = ISCSI_STATE_LOGGING_OUT; |
| 483 | |
Mike Christie | 577577d | 2008-12-02 00:32:05 -0600 | [diff] [blame] | 484 | task->state = ISCSI_TASK_RUNNING; |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 485 | list_move_tail(&task->running, &conn->mgmt_run_list); |
Mike Christie | 052d014 | 2008-05-21 15:54:05 -0500 | [diff] [blame] | 486 | debug_scsi("mgmtpdu [op 0x%x hdr->itt 0x%x datalen %d]\n", |
| 487 | hdr->opcode & ISCSI_OPCODE_MASK, hdr->itt, |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 488 | task->data_count); |
Mike Christie | 052d014 | 2008-05-21 15:54:05 -0500 | [diff] [blame] | 489 | return 0; |
| 490 | } |
| 491 | |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 492 | static struct iscsi_task * |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 493 | __iscsi_conn_send_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr, |
| 494 | char *data, uint32_t data_size) |
| 495 | { |
| 496 | struct iscsi_session *session = conn->session; |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 497 | struct iscsi_task *task; |
Mike Christie | 262ef63 | 2008-12-02 00:32:13 -0600 | [diff] [blame] | 498 | itt_t itt; |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 499 | |
| 500 | if (session->state == ISCSI_STATE_TERMINATE) |
| 501 | return NULL; |
| 502 | |
| 503 | if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) || |
| 504 | hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE)) |
| 505 | /* |
| 506 | * Login and Text are sent serially, in |
| 507 | * request-followed-by-response sequence. |
Mike Christie | 3e5c28a | 2008-05-21 15:54:06 -0500 | [diff] [blame] | 508 | * Same task can be used. Same ITT must be used. |
| 509 | * Note that login_task is preallocated at conn_create(). |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 510 | */ |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 511 | task = conn->login_task; |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 512 | else { |
| 513 | BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE); |
| 514 | BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED); |
| 515 | |
Mike Christie | 3e5c28a | 2008-05-21 15:54:06 -0500 | [diff] [blame] | 516 | if (!__kfifo_get(session->cmdpool.queue, |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 517 | (void*)&task, sizeof(void*))) |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 518 | return NULL; |
| 519 | } |
Mike Christie | 3e5c28a | 2008-05-21 15:54:06 -0500 | [diff] [blame] | 520 | /* |
| 521 | * released in complete pdu for task we expect a response for, and |
| 522 | * released by the lld when it has transmitted the task for |
| 523 | * pdus we do not expect a response for. |
| 524 | */ |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 525 | atomic_set(&task->refcount, 1); |
| 526 | task->conn = conn; |
| 527 | task->sc = NULL; |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 528 | |
| 529 | if (data_size) { |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 530 | memcpy(task->data, data, data_size); |
| 531 | task->data_count = data_size; |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 532 | } else |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 533 | task->data_count = 0; |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 534 | |
Mike Christie | 2ff79d5 | 2008-12-02 00:32:14 -0600 | [diff] [blame] | 535 | if (conn->session->tt->alloc_pdu(task, hdr->opcode)) { |
Mike Christie | 577577d | 2008-12-02 00:32:05 -0600 | [diff] [blame] | 536 | iscsi_conn_printk(KERN_ERR, conn, "Could not allocate " |
| 537 | "pdu for mgmt task.\n"); |
| 538 | goto requeue_task; |
| 539 | } |
Mike Christie | 262ef63 | 2008-12-02 00:32:13 -0600 | [diff] [blame] | 540 | itt = task->hdr->itt; |
Mike Christie | 577577d | 2008-12-02 00:32:05 -0600 | [diff] [blame] | 541 | task->hdr_len = sizeof(struct iscsi_hdr); |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 542 | memcpy(task->hdr, hdr, sizeof(struct iscsi_hdr)); |
Mike Christie | 262ef63 | 2008-12-02 00:32:13 -0600 | [diff] [blame] | 543 | |
| 544 | if (hdr->itt != RESERVED_ITT) { |
| 545 | if (session->tt->parse_pdu_itt) |
| 546 | task->hdr->itt = itt; |
| 547 | else |
| 548 | task->hdr->itt = build_itt(task->itt, |
| 549 | task->conn->session->age); |
| 550 | } |
| 551 | |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 552 | INIT_LIST_HEAD(&task->running); |
| 553 | list_add_tail(&task->running, &conn->mgmtqueue); |
Mike Christie | 052d014 | 2008-05-21 15:54:05 -0500 | [diff] [blame] | 554 | |
| 555 | if (session->tt->caps & CAP_DATA_PATH_OFFLOAD) { |
Mike Christie | 577577d | 2008-12-02 00:32:05 -0600 | [diff] [blame] | 556 | if (iscsi_prep_mgmt_task(conn, task)) |
| 557 | goto free_task; |
Mike Christie | 052d014 | 2008-05-21 15:54:05 -0500 | [diff] [blame] | 558 | |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 559 | if (session->tt->xmit_task(task)) |
Mike Christie | 577577d | 2008-12-02 00:32:05 -0600 | [diff] [blame] | 560 | goto free_task; |
Mike Christie | 052d014 | 2008-05-21 15:54:05 -0500 | [diff] [blame] | 561 | |
| 562 | } else |
| 563 | scsi_queue_work(conn->session->host, &conn->xmitwork); |
| 564 | |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 565 | return task; |
Mike Christie | 577577d | 2008-12-02 00:32:05 -0600 | [diff] [blame] | 566 | |
| 567 | free_task: |
| 568 | __iscsi_put_task(task); |
| 569 | return NULL; |
| 570 | |
| 571 | requeue_task: |
| 572 | if (task != conn->login_task) |
| 573 | __kfifo_put(session->cmdpool.queue, (void*)&task, |
| 574 | sizeof(void*)); |
| 575 | return NULL; |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr, |
| 579 | char *data, uint32_t data_size) |
| 580 | { |
| 581 | struct iscsi_conn *conn = cls_conn->dd_data; |
| 582 | struct iscsi_session *session = conn->session; |
| 583 | int err = 0; |
| 584 | |
| 585 | spin_lock_bh(&session->lock); |
| 586 | if (!__iscsi_conn_send_pdu(conn, hdr, data, data_size)) |
| 587 | err = -EPERM; |
| 588 | spin_unlock_bh(&session->lock); |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 589 | return err; |
| 590 | } |
| 591 | EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu); |
| 592 | |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 593 | /** |
| 594 | * iscsi_cmd_rsp - SCSI Command Response processing |
| 595 | * @conn: iscsi connection |
| 596 | * @hdr: iscsi header |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 597 | * @task: scsi command task |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 598 | * @data: cmd data buffer |
| 599 | * @datalen: len of buffer |
| 600 | * |
| 601 | * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 602 | * then completes the command and task. |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 603 | **/ |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 604 | static void iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr, |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 605 | struct iscsi_task *task, char *data, |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 606 | int datalen) |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 607 | { |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 608 | struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)hdr; |
| 609 | struct iscsi_session *session = conn->session; |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 610 | struct scsi_cmnd *sc = task->sc; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 611 | |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 612 | iscsi_update_cmdsn(session, (struct iscsi_nopin*)rhdr); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 613 | conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1; |
| 614 | |
| 615 | sc->result = (DID_OK << 16) | rhdr->cmd_status; |
| 616 | |
| 617 | if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) { |
| 618 | sc->result = DID_ERROR << 16; |
| 619 | goto out; |
| 620 | } |
| 621 | |
| 622 | if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) { |
Mike Christie | 9b80cb4 | 2006-12-17 12:10:28 -0600 | [diff] [blame] | 623 | uint16_t senselen; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 624 | |
| 625 | if (datalen < 2) { |
| 626 | invalid_datalen: |
Mike Christie | 322d739 | 2008-01-31 13:36:52 -0600 | [diff] [blame] | 627 | iscsi_conn_printk(KERN_ERR, conn, |
| 628 | "Got CHECK_CONDITION but invalid data " |
| 629 | "buffer size of %d\n", datalen); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 630 | sc->result = DID_BAD_TARGET << 16; |
| 631 | goto out; |
| 632 | } |
| 633 | |
Harvey Harrison | 8f333991 | 2008-05-21 15:54:20 -0500 | [diff] [blame] | 634 | senselen = get_unaligned_be16(data); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 635 | if (datalen < senselen) |
| 636 | goto invalid_datalen; |
| 637 | |
| 638 | memcpy(sc->sense_buffer, data + 2, |
Mike Christie | 9b80cb4 | 2006-12-17 12:10:28 -0600 | [diff] [blame] | 639 | min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE)); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 640 | debug_scsi("copied %d bytes of sense\n", |
Mike Christie | 8eb0053 | 2007-02-28 17:32:19 -0600 | [diff] [blame] | 641 | min_t(uint16_t, senselen, SCSI_SENSE_BUFFERSIZE)); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 642 | } |
| 643 | |
Boaz Harrosh | c07d444 | 2008-04-18 10:11:52 -0500 | [diff] [blame] | 644 | if (rhdr->flags & (ISCSI_FLAG_CMD_BIDI_UNDERFLOW | |
| 645 | ISCSI_FLAG_CMD_BIDI_OVERFLOW)) { |
| 646 | int res_count = be32_to_cpu(rhdr->bi_residual_count); |
| 647 | |
| 648 | if (scsi_bidi_cmnd(sc) && res_count > 0 && |
| 649 | (rhdr->flags & ISCSI_FLAG_CMD_BIDI_OVERFLOW || |
| 650 | res_count <= scsi_in(sc)->length)) |
| 651 | scsi_in(sc)->resid = res_count; |
| 652 | else |
| 653 | sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status; |
| 654 | } |
| 655 | |
Boaz Harrosh | 7207fea | 2007-12-13 12:43:22 -0600 | [diff] [blame] | 656 | if (rhdr->flags & (ISCSI_FLAG_CMD_UNDERFLOW | |
| 657 | ISCSI_FLAG_CMD_OVERFLOW)) { |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 658 | int res_count = be32_to_cpu(rhdr->residual_count); |
| 659 | |
Boaz Harrosh | 7207fea | 2007-12-13 12:43:22 -0600 | [diff] [blame] | 660 | if (res_count > 0 && |
| 661 | (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW || |
| 662 | res_count <= scsi_bufflen(sc))) |
Boaz Harrosh | c07d444 | 2008-04-18 10:11:52 -0500 | [diff] [blame] | 663 | /* write side for bidi or uni-io set_resid */ |
FUJITA Tomonori | 1c13899 | 2007-06-14 22:13:17 +0900 | [diff] [blame] | 664 | scsi_set_resid(sc, res_count); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 665 | else |
| 666 | sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status; |
Boaz Harrosh | c07d444 | 2008-04-18 10:11:52 -0500 | [diff] [blame] | 667 | } |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 668 | out: |
| 669 | debug_scsi("done [sc %lx res %d itt 0x%x]\n", |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 670 | (long)sc, sc->result, task->itt); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 671 | conn->scsirsp_pdus_cnt++; |
| 672 | |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 673 | __iscsi_put_task(task); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 674 | } |
| 675 | |
Mike Christie | 1d9edf0 | 2008-09-24 11:46:09 -0500 | [diff] [blame] | 676 | /** |
| 677 | * iscsi_data_in_rsp - SCSI Data-In Response processing |
| 678 | * @conn: iscsi connection |
| 679 | * @hdr: iscsi pdu |
| 680 | * @task: scsi command task |
| 681 | **/ |
| 682 | static void |
| 683 | iscsi_data_in_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr, |
| 684 | struct iscsi_task *task) |
| 685 | { |
| 686 | struct iscsi_data_rsp *rhdr = (struct iscsi_data_rsp *)hdr; |
| 687 | struct scsi_cmnd *sc = task->sc; |
| 688 | |
| 689 | if (!(rhdr->flags & ISCSI_FLAG_DATA_STATUS)) |
| 690 | return; |
| 691 | |
| 692 | sc->result = (DID_OK << 16) | rhdr->cmd_status; |
| 693 | conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1; |
| 694 | if (rhdr->flags & (ISCSI_FLAG_DATA_UNDERFLOW | |
| 695 | ISCSI_FLAG_DATA_OVERFLOW)) { |
| 696 | int res_count = be32_to_cpu(rhdr->residual_count); |
| 697 | |
| 698 | if (res_count > 0 && |
| 699 | (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW || |
| 700 | res_count <= scsi_in(sc)->length)) |
| 701 | scsi_in(sc)->resid = res_count; |
| 702 | else |
| 703 | sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status; |
| 704 | } |
| 705 | |
| 706 | conn->scsirsp_pdus_cnt++; |
| 707 | __iscsi_put_task(task); |
| 708 | } |
| 709 | |
Mike Christie | 7ea8b82 | 2006-07-24 15:47:22 -0500 | [diff] [blame] | 710 | static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr) |
| 711 | { |
| 712 | struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr; |
| 713 | |
| 714 | conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1; |
| 715 | conn->tmfrsp_pdus_cnt++; |
| 716 | |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 717 | if (conn->tmf_state != TMF_QUEUED) |
Mike Christie | 7ea8b82 | 2006-07-24 15:47:22 -0500 | [diff] [blame] | 718 | return; |
| 719 | |
| 720 | if (tmf->response == ISCSI_TMF_RSP_COMPLETE) |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 721 | conn->tmf_state = TMF_SUCCESS; |
Mike Christie | 7ea8b82 | 2006-07-24 15:47:22 -0500 | [diff] [blame] | 722 | else if (tmf->response == ISCSI_TMF_RSP_NO_TASK) |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 723 | conn->tmf_state = TMF_NOT_FOUND; |
Mike Christie | 7ea8b82 | 2006-07-24 15:47:22 -0500 | [diff] [blame] | 724 | else |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 725 | conn->tmf_state = TMF_FAILED; |
Mike Christie | 7ea8b82 | 2006-07-24 15:47:22 -0500 | [diff] [blame] | 726 | wake_up(&conn->ehwait); |
| 727 | } |
| 728 | |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 729 | static void iscsi_send_nopout(struct iscsi_conn *conn, struct iscsi_nopin *rhdr) |
| 730 | { |
| 731 | struct iscsi_nopout hdr; |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 732 | struct iscsi_task *task; |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 733 | |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 734 | if (!rhdr && conn->ping_task) |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 735 | return; |
| 736 | |
| 737 | memset(&hdr, 0, sizeof(struct iscsi_nopout)); |
| 738 | hdr.opcode = ISCSI_OP_NOOP_OUT | ISCSI_OP_IMMEDIATE; |
| 739 | hdr.flags = ISCSI_FLAG_CMD_FINAL; |
| 740 | |
| 741 | if (rhdr) { |
| 742 | memcpy(hdr.lun, rhdr->lun, 8); |
| 743 | hdr.ttt = rhdr->ttt; |
| 744 | hdr.itt = RESERVED_ITT; |
| 745 | } else |
| 746 | hdr.ttt = RESERVED_ITT; |
| 747 | |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 748 | task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)&hdr, NULL, 0); |
| 749 | if (!task) |
Mike Christie | 322d739 | 2008-01-31 13:36:52 -0600 | [diff] [blame] | 750 | iscsi_conn_printk(KERN_ERR, conn, "Could not send nopout\n"); |
Mike Christie | d3acf02 | 2008-12-01 12:13:00 -0600 | [diff] [blame] | 751 | else if (!rhdr) { |
| 752 | /* only track our nops */ |
| 753 | conn->ping_task = task; |
| 754 | conn->last_ping = jiffies; |
| 755 | } |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 756 | } |
| 757 | |
Mike Christie | 62f3830 | 2006-08-31 18:09:27 -0400 | [diff] [blame] | 758 | static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr, |
| 759 | char *data, int datalen) |
| 760 | { |
| 761 | struct iscsi_reject *reject = (struct iscsi_reject *)hdr; |
| 762 | struct iscsi_hdr rejected_pdu; |
Mike Christie | 62f3830 | 2006-08-31 18:09:27 -0400 | [diff] [blame] | 763 | |
| 764 | conn->exp_statsn = be32_to_cpu(reject->statsn) + 1; |
| 765 | |
| 766 | if (reject->reason == ISCSI_REASON_DATA_DIGEST_ERROR) { |
| 767 | if (ntoh24(reject->dlength) > datalen) |
| 768 | return ISCSI_ERR_PROTO; |
| 769 | |
| 770 | if (ntoh24(reject->dlength) >= sizeof(struct iscsi_hdr)) { |
| 771 | memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr)); |
Mike Christie | 322d739 | 2008-01-31 13:36:52 -0600 | [diff] [blame] | 772 | iscsi_conn_printk(KERN_ERR, conn, |
Mike Christie | 262ef63 | 2008-12-02 00:32:13 -0600 | [diff] [blame] | 773 | "pdu (op 0x%x) rejected " |
| 774 | "due to DataDigest error.\n", |
Mike Christie | 322d739 | 2008-01-31 13:36:52 -0600 | [diff] [blame] | 775 | rejected_pdu.opcode); |
Mike Christie | 62f3830 | 2006-08-31 18:09:27 -0400 | [diff] [blame] | 776 | } |
| 777 | } |
| 778 | return 0; |
| 779 | } |
| 780 | |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 781 | /** |
Mike Christie | 913e5bf | 2008-05-21 15:54:18 -0500 | [diff] [blame] | 782 | * iscsi_itt_to_task - look up task by itt |
| 783 | * @conn: iscsi connection |
| 784 | * @itt: itt |
| 785 | * |
| 786 | * This should be used for mgmt tasks like login and nops, or if |
| 787 | * the LDD's itt space does not include the session age. |
| 788 | * |
| 789 | * The session lock must be held. |
| 790 | */ |
| 791 | static struct iscsi_task *iscsi_itt_to_task(struct iscsi_conn *conn, itt_t itt) |
| 792 | { |
| 793 | struct iscsi_session *session = conn->session; |
Mike Christie | 262ef63 | 2008-12-02 00:32:13 -0600 | [diff] [blame] | 794 | int i; |
Mike Christie | 913e5bf | 2008-05-21 15:54:18 -0500 | [diff] [blame] | 795 | |
| 796 | if (itt == RESERVED_ITT) |
| 797 | return NULL; |
| 798 | |
Mike Christie | 262ef63 | 2008-12-02 00:32:13 -0600 | [diff] [blame] | 799 | if (session->tt->parse_pdu_itt) |
| 800 | session->tt->parse_pdu_itt(conn, itt, &i, NULL); |
| 801 | else |
| 802 | i = get_itt(itt); |
Mike Christie | 913e5bf | 2008-05-21 15:54:18 -0500 | [diff] [blame] | 803 | if (i >= session->cmds_max) |
| 804 | return NULL; |
| 805 | |
| 806 | return session->cmds[i]; |
| 807 | } |
| 808 | |
| 809 | /** |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 810 | * __iscsi_complete_pdu - complete pdu |
| 811 | * @conn: iscsi conn |
| 812 | * @hdr: iscsi header |
| 813 | * @data: data buffer |
| 814 | * @datalen: len of data buffer |
| 815 | * |
| 816 | * Completes pdu processing by freeing any resources allocated at |
| 817 | * queuecommand or send generic. session lock must be held and verify |
| 818 | * itt must have been called. |
| 819 | */ |
Mike Christie | 913e5bf | 2008-05-21 15:54:18 -0500 | [diff] [blame] | 820 | int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr, |
| 821 | char *data, int datalen) |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 822 | { |
| 823 | struct iscsi_session *session = conn->session; |
| 824 | int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0; |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 825 | struct iscsi_task *task; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 826 | uint32_t itt; |
| 827 | |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 828 | conn->last_recv = jiffies; |
Mike Christie | 0af967f | 2008-05-21 15:54:04 -0500 | [diff] [blame] | 829 | rc = iscsi_verify_itt(conn, hdr->itt); |
| 830 | if (rc) |
| 831 | return rc; |
| 832 | |
Al Viro | b437735 | 2007-02-09 16:39:40 +0000 | [diff] [blame] | 833 | if (hdr->itt != RESERVED_ITT) |
| 834 | itt = get_itt(hdr->itt); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 835 | else |
Al Viro | b437735 | 2007-02-09 16:39:40 +0000 | [diff] [blame] | 836 | itt = ~0U; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 837 | |
Mike Christie | 3e5c28a | 2008-05-21 15:54:06 -0500 | [diff] [blame] | 838 | debug_scsi("[op 0x%x cid %d itt 0x%x len %d]\n", |
| 839 | opcode, conn->id, itt, datalen); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 840 | |
Mike Christie | 3e5c28a | 2008-05-21 15:54:06 -0500 | [diff] [blame] | 841 | if (itt == ~0U) { |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 842 | iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr); |
Mike Christie | 62f3830 | 2006-08-31 18:09:27 -0400 | [diff] [blame] | 843 | |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 844 | switch(opcode) { |
| 845 | case ISCSI_OP_NOOP_IN: |
Mike Christie | 40527af | 2006-07-24 15:47:45 -0500 | [diff] [blame] | 846 | if (datalen) { |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 847 | rc = ISCSI_ERR_PROTO; |
Mike Christie | 40527af | 2006-07-24 15:47:45 -0500 | [diff] [blame] | 848 | break; |
| 849 | } |
| 850 | |
Al Viro | b437735 | 2007-02-09 16:39:40 +0000 | [diff] [blame] | 851 | if (hdr->ttt == cpu_to_be32(ISCSI_RESERVED_TAG)) |
Mike Christie | 40527af | 2006-07-24 15:47:45 -0500 | [diff] [blame] | 852 | break; |
| 853 | |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 854 | iscsi_send_nopout(conn, (struct iscsi_nopin*)hdr); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 855 | break; |
| 856 | case ISCSI_OP_REJECT: |
Mike Christie | 62f3830 | 2006-08-31 18:09:27 -0400 | [diff] [blame] | 857 | rc = iscsi_handle_reject(conn, hdr, data, datalen); |
| 858 | break; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 859 | case ISCSI_OP_ASYNC_EVENT: |
Mike Christie | 8d2860b | 2006-05-02 19:46:47 -0500 | [diff] [blame] | 860 | conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1; |
Mike Christie | 5831c73 | 2006-10-16 18:09:41 -0400 | [diff] [blame] | 861 | if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen)) |
| 862 | rc = ISCSI_ERR_CONN_FAILED; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 863 | break; |
| 864 | default: |
| 865 | rc = ISCSI_ERR_BAD_OPCODE; |
| 866 | break; |
| 867 | } |
Mike Christie | 3e5c28a | 2008-05-21 15:54:06 -0500 | [diff] [blame] | 868 | goto out; |
| 869 | } |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 870 | |
Mike Christie | 3e5c28a | 2008-05-21 15:54:06 -0500 | [diff] [blame] | 871 | switch(opcode) { |
| 872 | case ISCSI_OP_SCSI_CMD_RSP: |
Mike Christie | 913e5bf | 2008-05-21 15:54:18 -0500 | [diff] [blame] | 873 | case ISCSI_OP_SCSI_DATA_IN: |
| 874 | task = iscsi_itt_to_ctask(conn, hdr->itt); |
| 875 | if (!task) |
| 876 | return ISCSI_ERR_BAD_ITT; |
| 877 | break; |
| 878 | case ISCSI_OP_R2T: |
| 879 | /* |
| 880 | * LLD handles R2Ts if they need to. |
| 881 | */ |
| 882 | return 0; |
| 883 | case ISCSI_OP_LOGOUT_RSP: |
| 884 | case ISCSI_OP_LOGIN_RSP: |
| 885 | case ISCSI_OP_TEXT_RSP: |
| 886 | case ISCSI_OP_SCSI_TMFUNC_RSP: |
| 887 | case ISCSI_OP_NOOP_IN: |
| 888 | task = iscsi_itt_to_task(conn, hdr->itt); |
| 889 | if (!task) |
| 890 | return ISCSI_ERR_BAD_ITT; |
| 891 | break; |
| 892 | default: |
| 893 | return ISCSI_ERR_BAD_OPCODE; |
| 894 | } |
| 895 | |
| 896 | switch(opcode) { |
| 897 | case ISCSI_OP_SCSI_CMD_RSP: |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 898 | iscsi_scsi_cmd_rsp(conn, hdr, task, data, datalen); |
Mike Christie | 3e5c28a | 2008-05-21 15:54:06 -0500 | [diff] [blame] | 899 | break; |
| 900 | case ISCSI_OP_SCSI_DATA_IN: |
Mike Christie | 1d9edf0 | 2008-09-24 11:46:09 -0500 | [diff] [blame] | 901 | iscsi_data_in_rsp(conn, hdr, task); |
Mike Christie | 3e5c28a | 2008-05-21 15:54:06 -0500 | [diff] [blame] | 902 | break; |
Mike Christie | 3e5c28a | 2008-05-21 15:54:06 -0500 | [diff] [blame] | 903 | case ISCSI_OP_LOGOUT_RSP: |
| 904 | iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr); |
| 905 | if (datalen) { |
| 906 | rc = ISCSI_ERR_PROTO; |
| 907 | break; |
| 908 | } |
| 909 | conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1; |
| 910 | goto recv_pdu; |
| 911 | case ISCSI_OP_LOGIN_RSP: |
| 912 | case ISCSI_OP_TEXT_RSP: |
| 913 | iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr); |
| 914 | /* |
| 915 | * login related PDU's exp_statsn is handled in |
| 916 | * userspace |
| 917 | */ |
| 918 | goto recv_pdu; |
| 919 | case ISCSI_OP_SCSI_TMFUNC_RSP: |
| 920 | iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr); |
| 921 | if (datalen) { |
| 922 | rc = ISCSI_ERR_PROTO; |
| 923 | break; |
| 924 | } |
| 925 | |
| 926 | iscsi_tmf_rsp(conn, hdr); |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 927 | __iscsi_put_task(task); |
Mike Christie | 3e5c28a | 2008-05-21 15:54:06 -0500 | [diff] [blame] | 928 | break; |
| 929 | case ISCSI_OP_NOOP_IN: |
| 930 | iscsi_update_cmdsn(session, (struct iscsi_nopin*)hdr); |
| 931 | if (hdr->ttt != cpu_to_be32(ISCSI_RESERVED_TAG) || datalen) { |
| 932 | rc = ISCSI_ERR_PROTO; |
| 933 | break; |
| 934 | } |
| 935 | conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1; |
| 936 | |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 937 | if (conn->ping_task != task) |
Mike Christie | 3e5c28a | 2008-05-21 15:54:06 -0500 | [diff] [blame] | 938 | /* |
| 939 | * If this is not in response to one of our |
| 940 | * nops then it must be from userspace. |
| 941 | */ |
| 942 | goto recv_pdu; |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 943 | |
| 944 | mod_timer(&conn->transport_timer, jiffies + conn->recv_timeout); |
| 945 | __iscsi_put_task(task); |
Mike Christie | 3e5c28a | 2008-05-21 15:54:06 -0500 | [diff] [blame] | 946 | break; |
| 947 | default: |
| 948 | rc = ISCSI_ERR_BAD_OPCODE; |
| 949 | break; |
| 950 | } |
| 951 | |
| 952 | out: |
| 953 | return rc; |
| 954 | recv_pdu: |
| 955 | if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen)) |
| 956 | rc = ISCSI_ERR_CONN_FAILED; |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 957 | __iscsi_put_task(task); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 958 | return rc; |
| 959 | } |
Mike Christie | 913e5bf | 2008-05-21 15:54:18 -0500 | [diff] [blame] | 960 | EXPORT_SYMBOL_GPL(__iscsi_complete_pdu); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 961 | |
| 962 | int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr, |
| 963 | char *data, int datalen) |
| 964 | { |
| 965 | int rc; |
| 966 | |
| 967 | spin_lock(&conn->session->lock); |
| 968 | rc = __iscsi_complete_pdu(conn, hdr, data, datalen); |
| 969 | spin_unlock(&conn->session->lock); |
| 970 | return rc; |
| 971 | } |
| 972 | EXPORT_SYMBOL_GPL(iscsi_complete_pdu); |
| 973 | |
Mike Christie | 0af967f | 2008-05-21 15:54:04 -0500 | [diff] [blame] | 974 | int iscsi_verify_itt(struct iscsi_conn *conn, itt_t itt) |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 975 | { |
| 976 | struct iscsi_session *session = conn->session; |
Mike Christie | 262ef63 | 2008-12-02 00:32:13 -0600 | [diff] [blame] | 977 | int age = 0, i = 0; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 978 | |
Mike Christie | 0af967f | 2008-05-21 15:54:04 -0500 | [diff] [blame] | 979 | if (itt == RESERVED_ITT) |
| 980 | return 0; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 981 | |
Mike Christie | 262ef63 | 2008-12-02 00:32:13 -0600 | [diff] [blame] | 982 | if (session->tt->parse_pdu_itt) |
| 983 | session->tt->parse_pdu_itt(conn, itt, &i, &age); |
| 984 | else { |
| 985 | i = get_itt(itt); |
| 986 | age = ((__force u32)itt >> ISCSI_AGE_SHIFT) & ISCSI_AGE_MASK; |
| 987 | } |
| 988 | |
| 989 | if (age != session->age) { |
Mike Christie | 0af967f | 2008-05-21 15:54:04 -0500 | [diff] [blame] | 990 | iscsi_conn_printk(KERN_ERR, conn, |
| 991 | "received itt %x expected session age (%x)\n", |
Mike Christie | 913e5bf | 2008-05-21 15:54:18 -0500 | [diff] [blame] | 992 | (__force u32)itt, session->age); |
Mike Christie | 0af967f | 2008-05-21 15:54:04 -0500 | [diff] [blame] | 993 | return ISCSI_ERR_BAD_ITT; |
| 994 | } |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 995 | |
Mike Christie | 3e5c28a | 2008-05-21 15:54:06 -0500 | [diff] [blame] | 996 | if (i >= session->cmds_max) { |
| 997 | iscsi_conn_printk(KERN_ERR, conn, |
| 998 | "received invalid itt index %u (max cmds " |
| 999 | "%u.\n", i, session->cmds_max); |
| 1000 | return ISCSI_ERR_BAD_ITT; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1001 | } |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1002 | return 0; |
| 1003 | } |
| 1004 | EXPORT_SYMBOL_GPL(iscsi_verify_itt); |
| 1005 | |
Mike Christie | 913e5bf | 2008-05-21 15:54:18 -0500 | [diff] [blame] | 1006 | /** |
| 1007 | * iscsi_itt_to_ctask - look up ctask by itt |
| 1008 | * @conn: iscsi connection |
| 1009 | * @itt: itt |
| 1010 | * |
| 1011 | * This should be used for cmd tasks. |
| 1012 | * |
| 1013 | * The session lock must be held. |
| 1014 | */ |
| 1015 | struct iscsi_task *iscsi_itt_to_ctask(struct iscsi_conn *conn, itt_t itt) |
Mike Christie | 0af967f | 2008-05-21 15:54:04 -0500 | [diff] [blame] | 1016 | { |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1017 | struct iscsi_task *task; |
Mike Christie | 0af967f | 2008-05-21 15:54:04 -0500 | [diff] [blame] | 1018 | |
| 1019 | if (iscsi_verify_itt(conn, itt)) |
| 1020 | return NULL; |
| 1021 | |
Mike Christie | 913e5bf | 2008-05-21 15:54:18 -0500 | [diff] [blame] | 1022 | task = iscsi_itt_to_task(conn, itt); |
| 1023 | if (!task || !task->sc) |
Mike Christie | 0af967f | 2008-05-21 15:54:04 -0500 | [diff] [blame] | 1024 | return NULL; |
| 1025 | |
Mike Christie | 913e5bf | 2008-05-21 15:54:18 -0500 | [diff] [blame] | 1026 | if (task->sc->SCp.phase != conn->session->age) { |
| 1027 | iscsi_session_printk(KERN_ERR, conn->session, |
| 1028 | "task's session age %d, expected %d\n", |
| 1029 | task->sc->SCp.phase, conn->session->age); |
Mike Christie | 0af967f | 2008-05-21 15:54:04 -0500 | [diff] [blame] | 1030 | return NULL; |
Mike Christie | 913e5bf | 2008-05-21 15:54:18 -0500 | [diff] [blame] | 1031 | } |
Mike Christie | 0af967f | 2008-05-21 15:54:04 -0500 | [diff] [blame] | 1032 | |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1033 | return task; |
Mike Christie | 0af967f | 2008-05-21 15:54:04 -0500 | [diff] [blame] | 1034 | } |
| 1035 | EXPORT_SYMBOL_GPL(iscsi_itt_to_ctask); |
| 1036 | |
Mike Christie | e5bd7b5 | 2008-09-24 11:46:10 -0500 | [diff] [blame] | 1037 | void iscsi_session_failure(struct iscsi_cls_session *cls_session, |
| 1038 | enum iscsi_err err) |
| 1039 | { |
| 1040 | struct iscsi_session *session = cls_session->dd_data; |
| 1041 | struct iscsi_conn *conn; |
| 1042 | struct device *dev; |
| 1043 | unsigned long flags; |
| 1044 | |
| 1045 | spin_lock_irqsave(&session->lock, flags); |
| 1046 | conn = session->leadconn; |
| 1047 | if (session->state == ISCSI_STATE_TERMINATE || !conn) { |
| 1048 | spin_unlock_irqrestore(&session->lock, flags); |
| 1049 | return; |
| 1050 | } |
| 1051 | |
| 1052 | dev = get_device(&conn->cls_conn->dev); |
| 1053 | spin_unlock_irqrestore(&session->lock, flags); |
| 1054 | if (!dev) |
| 1055 | return; |
| 1056 | /* |
| 1057 | * if the host is being removed bypass the connection |
| 1058 | * recovery initialization because we are going to kill |
| 1059 | * the session. |
| 1060 | */ |
| 1061 | if (err == ISCSI_ERR_INVALID_HOST) |
| 1062 | iscsi_conn_error_event(conn->cls_conn, err); |
| 1063 | else |
| 1064 | iscsi_conn_failure(conn, err); |
| 1065 | put_device(dev); |
| 1066 | } |
| 1067 | EXPORT_SYMBOL_GPL(iscsi_session_failure); |
| 1068 | |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1069 | void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err) |
| 1070 | { |
| 1071 | struct iscsi_session *session = conn->session; |
| 1072 | unsigned long flags; |
| 1073 | |
| 1074 | spin_lock_irqsave(&session->lock, flags); |
Mike Christie | 656cffc | 2006-05-18 20:31:42 -0500 | [diff] [blame] | 1075 | if (session->state == ISCSI_STATE_FAILED) { |
| 1076 | spin_unlock_irqrestore(&session->lock, flags); |
| 1077 | return; |
| 1078 | } |
| 1079 | |
Mike Christie | 67a6111 | 2006-05-30 00:37:20 -0500 | [diff] [blame] | 1080 | if (conn->stop_stage == 0) |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1081 | session->state = ISCSI_STATE_FAILED; |
| 1082 | spin_unlock_irqrestore(&session->lock, flags); |
Mike Christie | e5bd7b5 | 2008-09-24 11:46:10 -0500 | [diff] [blame] | 1083 | |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1084 | set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx); |
| 1085 | set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx); |
Mike Christie | e5bd7b5 | 2008-09-24 11:46:10 -0500 | [diff] [blame] | 1086 | iscsi_conn_error_event(conn->cls_conn, err); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1087 | } |
| 1088 | EXPORT_SYMBOL_GPL(iscsi_conn_failure); |
| 1089 | |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 1090 | static int iscsi_check_cmdsn_window_closed(struct iscsi_conn *conn) |
| 1091 | { |
| 1092 | struct iscsi_session *session = conn->session; |
| 1093 | |
| 1094 | /* |
| 1095 | * Check for iSCSI window and take care of CmdSN wrap-around |
| 1096 | */ |
Mike Christie | e072640 | 2007-07-26 12:46:48 -0500 | [diff] [blame] | 1097 | if (!iscsi_sna_lte(session->queued_cmdsn, session->max_cmdsn)) { |
| 1098 | debug_scsi("iSCSI CmdSN closed. ExpCmdSn %u MaxCmdSN %u " |
| 1099 | "CmdSN %u/%u\n", session->exp_cmdsn, |
| 1100 | session->max_cmdsn, session->cmdsn, |
| 1101 | session->queued_cmdsn); |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 1102 | return -ENOSPC; |
| 1103 | } |
| 1104 | return 0; |
| 1105 | } |
| 1106 | |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1107 | static int iscsi_xmit_task(struct iscsi_conn *conn) |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 1108 | { |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1109 | struct iscsi_task *task = conn->task; |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1110 | int rc; |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 1111 | |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1112 | __iscsi_get_task(task); |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 1113 | spin_unlock_bh(&conn->session->lock); |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1114 | rc = conn->session->tt->xmit_task(task); |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 1115 | spin_lock_bh(&conn->session->lock); |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1116 | __iscsi_put_task(task); |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 1117 | if (!rc) |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1118 | /* done with this task */ |
| 1119 | conn->task = NULL; |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 1120 | return rc; |
| 1121 | } |
| 1122 | |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1123 | /** |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1124 | * iscsi_requeue_task - requeue task to run from session workqueue |
| 1125 | * @task: task to requeue |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1126 | * |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1127 | * LLDs that need to run a task from the session workqueue should call |
Mike Christie | 052d014 | 2008-05-21 15:54:05 -0500 | [diff] [blame] | 1128 | * this. The session lock must be held. This should only be called |
| 1129 | * by software drivers. |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1130 | */ |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1131 | void iscsi_requeue_task(struct iscsi_task *task) |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1132 | { |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1133 | struct iscsi_conn *conn = task->conn; |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1134 | |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1135 | list_move_tail(&task->running, &conn->requeue); |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1136 | scsi_queue_work(conn->session->host, &conn->xmitwork); |
| 1137 | } |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1138 | EXPORT_SYMBOL_GPL(iscsi_requeue_task); |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1139 | |
| 1140 | /** |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1141 | * iscsi_data_xmit - xmit any command into the scheduled connection |
| 1142 | * @conn: iscsi connection |
| 1143 | * |
| 1144 | * Notes: |
| 1145 | * The function can return -EAGAIN in which case the caller must |
| 1146 | * re-schedule it again later or recover. '0' return code means |
| 1147 | * successful xmit. |
| 1148 | **/ |
| 1149 | static int iscsi_data_xmit(struct iscsi_conn *conn) |
| 1150 | { |
Mike Christie | 3219e52 | 2006-05-30 00:37:28 -0500 | [diff] [blame] | 1151 | int rc = 0; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1152 | |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 1153 | spin_lock_bh(&conn->session->lock); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1154 | if (unlikely(conn->suspend_tx)) { |
| 1155 | debug_scsi("conn %d Tx suspended!\n", conn->id); |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 1156 | spin_unlock_bh(&conn->session->lock); |
Mike Christie | 3219e52 | 2006-05-30 00:37:28 -0500 | [diff] [blame] | 1157 | return -ENODATA; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1158 | } |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1159 | |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1160 | if (conn->task) { |
| 1161 | rc = iscsi_xmit_task(conn); |
Mike Christie | 3219e52 | 2006-05-30 00:37:28 -0500 | [diff] [blame] | 1162 | if (rc) |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1163 | goto again; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1164 | } |
| 1165 | |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 1166 | /* |
| 1167 | * process mgmt pdus like nops before commands since we should |
| 1168 | * only have one nop-out as a ping from us and targets should not |
| 1169 | * overflow us with nop-ins |
| 1170 | */ |
| 1171 | check_mgmt: |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1172 | while (!list_empty(&conn->mgmtqueue)) { |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1173 | conn->task = list_entry(conn->mgmtqueue.next, |
| 1174 | struct iscsi_task, running); |
| 1175 | if (iscsi_prep_mgmt_task(conn, conn->task)) { |
| 1176 | __iscsi_put_task(conn->task); |
| 1177 | conn->task = NULL; |
Mike Christie | b3a7ea8 | 2007-12-13 12:43:26 -0600 | [diff] [blame] | 1178 | continue; |
| 1179 | } |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1180 | rc = iscsi_xmit_task(conn); |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 1181 | if (rc) |
| 1182 | goto again; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1183 | } |
| 1184 | |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1185 | /* process pending command queue */ |
Mike Christie | b6c395e | 2006-07-24 15:47:15 -0500 | [diff] [blame] | 1186 | while (!list_empty(&conn->xmitqueue)) { |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1187 | if (conn->tmf_state == TMF_QUEUED) |
| 1188 | break; |
| 1189 | |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1190 | conn->task = list_entry(conn->xmitqueue.next, |
| 1191 | struct iscsi_task, running); |
Mike Christie | b3a7ea8 | 2007-12-13 12:43:26 -0600 | [diff] [blame] | 1192 | if (conn->session->state == ISCSI_STATE_LOGGING_OUT) { |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1193 | fail_command(conn, conn->task, DID_IMM_RETRY << 16); |
Mike Christie | b3a7ea8 | 2007-12-13 12:43:26 -0600 | [diff] [blame] | 1194 | continue; |
| 1195 | } |
Mike Christie | 577577d | 2008-12-02 00:32:05 -0600 | [diff] [blame] | 1196 | rc = iscsi_prep_scsi_cmd_pdu(conn->task); |
| 1197 | if (rc) { |
| 1198 | if (rc == -ENOMEM) { |
| 1199 | conn->task = NULL; |
| 1200 | goto again; |
| 1201 | } else |
| 1202 | fail_command(conn, conn->task, DID_ABORT << 16); |
Boaz Harrosh | 004d653 | 2007-12-13 12:43:23 -0600 | [diff] [blame] | 1203 | continue; |
| 1204 | } |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1205 | rc = iscsi_xmit_task(conn); |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 1206 | if (rc) |
Mike Christie | 60ecebf | 2006-08-31 18:09:25 -0400 | [diff] [blame] | 1207 | goto again; |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 1208 | /* |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1209 | * we could continuously get new task requests so |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 1210 | * we need to check the mgmt queue for nops that need to |
| 1211 | * be sent to aviod starvation |
| 1212 | */ |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1213 | if (!list_empty(&conn->mgmtqueue)) |
| 1214 | goto check_mgmt; |
| 1215 | } |
| 1216 | |
| 1217 | while (!list_empty(&conn->requeue)) { |
| 1218 | if (conn->session->fast_abort && conn->tmf_state != TMF_INITIAL) |
| 1219 | break; |
| 1220 | |
Mike Christie | b3a7ea8 | 2007-12-13 12:43:26 -0600 | [diff] [blame] | 1221 | /* |
| 1222 | * we always do fastlogout - conn stop code will clean up. |
| 1223 | */ |
| 1224 | if (conn->session->state == ISCSI_STATE_LOGGING_OUT) |
| 1225 | break; |
| 1226 | |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1227 | conn->task = list_entry(conn->requeue.next, |
| 1228 | struct iscsi_task, running); |
| 1229 | conn->task->state = ISCSI_TASK_RUNNING; |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1230 | list_move_tail(conn->requeue.next, &conn->run_list); |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1231 | rc = iscsi_xmit_task(conn); |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1232 | if (rc) |
| 1233 | goto again; |
| 1234 | if (!list_empty(&conn->mgmtqueue)) |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 1235 | goto check_mgmt; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1236 | } |
Mike Christie | b6c395e | 2006-07-24 15:47:15 -0500 | [diff] [blame] | 1237 | spin_unlock_bh(&conn->session->lock); |
Mike Christie | 3219e52 | 2006-05-30 00:37:28 -0500 | [diff] [blame] | 1238 | return -ENODATA; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1239 | |
| 1240 | again: |
| 1241 | if (unlikely(conn->suspend_tx)) |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 1242 | rc = -ENODATA; |
| 1243 | spin_unlock_bh(&conn->session->lock); |
Mike Christie | 3219e52 | 2006-05-30 00:37:28 -0500 | [diff] [blame] | 1244 | return rc; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1245 | } |
| 1246 | |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 1247 | static void iscsi_xmitworker(struct work_struct *work) |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1248 | { |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 1249 | struct iscsi_conn *conn = |
| 1250 | container_of(work, struct iscsi_conn, xmitwork); |
Mike Christie | 3219e52 | 2006-05-30 00:37:28 -0500 | [diff] [blame] | 1251 | int rc; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1252 | /* |
| 1253 | * serialize Xmit worker on a per-connection basis. |
| 1254 | */ |
Mike Christie | 3219e52 | 2006-05-30 00:37:28 -0500 | [diff] [blame] | 1255 | do { |
| 1256 | rc = iscsi_data_xmit(conn); |
| 1257 | } while (rc >= 0 || rc == -EAGAIN); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1258 | } |
| 1259 | |
Mike Christie | 577577d | 2008-12-02 00:32:05 -0600 | [diff] [blame] | 1260 | static inline struct iscsi_task *iscsi_alloc_task(struct iscsi_conn *conn, |
| 1261 | struct scsi_cmnd *sc) |
| 1262 | { |
| 1263 | struct iscsi_task *task; |
| 1264 | |
| 1265 | if (!__kfifo_get(conn->session->cmdpool.queue, |
| 1266 | (void *) &task, sizeof(void *))) |
| 1267 | return NULL; |
| 1268 | |
| 1269 | sc->SCp.phase = conn->session->age; |
| 1270 | sc->SCp.ptr = (char *) task; |
| 1271 | |
| 1272 | atomic_set(&task->refcount, 1); |
| 1273 | task->state = ISCSI_TASK_PENDING; |
| 1274 | task->conn = conn; |
| 1275 | task->sc = sc; |
| 1276 | INIT_LIST_HEAD(&task->running); |
| 1277 | return task; |
| 1278 | } |
| 1279 | |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1280 | enum { |
| 1281 | FAILURE_BAD_HOST = 1, |
| 1282 | FAILURE_SESSION_FAILED, |
| 1283 | FAILURE_SESSION_FREED, |
| 1284 | FAILURE_WINDOW_CLOSED, |
Mike Christie | 60ecebf | 2006-08-31 18:09:25 -0400 | [diff] [blame] | 1285 | FAILURE_OOM, |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1286 | FAILURE_SESSION_TERMINATE, |
Mike Christie | 656cffc | 2006-05-18 20:31:42 -0500 | [diff] [blame] | 1287 | FAILURE_SESSION_IN_RECOVERY, |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1288 | FAILURE_SESSION_RECOVERY_TIMEOUT, |
Mike Christie | b3a7ea8 | 2007-12-13 12:43:26 -0600 | [diff] [blame] | 1289 | FAILURE_SESSION_LOGGING_OUT, |
Mike Christie | 6eabafb | 2008-01-31 13:36:43 -0600 | [diff] [blame] | 1290 | FAILURE_SESSION_NOT_READY, |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1291 | }; |
| 1292 | |
| 1293 | int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *)) |
| 1294 | { |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 1295 | struct iscsi_cls_session *cls_session; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1296 | struct Scsi_Host *host; |
| 1297 | int reason = 0; |
| 1298 | struct iscsi_session *session; |
| 1299 | struct iscsi_conn *conn; |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1300 | struct iscsi_task *task = NULL; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1301 | |
| 1302 | sc->scsi_done = done; |
| 1303 | sc->result = 0; |
Mike Christie | f47f2cf | 2006-08-31 18:09:33 -0400 | [diff] [blame] | 1304 | sc->SCp.ptr = NULL; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1305 | |
| 1306 | host = sc->device->host; |
Mike Christie | 1040c99 | 2007-12-13 12:43:34 -0600 | [diff] [blame] | 1307 | spin_unlock(host->host_lock); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1308 | |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 1309 | cls_session = starget_to_session(scsi_target(sc->device)); |
| 1310 | session = cls_session->dd_data; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1311 | spin_lock(&session->lock); |
| 1312 | |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 1313 | reason = iscsi_session_chkready(cls_session); |
Mike Christie | 6eabafb | 2008-01-31 13:36:43 -0600 | [diff] [blame] | 1314 | if (reason) { |
| 1315 | sc->result = reason; |
| 1316 | goto fault; |
| 1317 | } |
| 1318 | |
Mike Christie | 656cffc | 2006-05-18 20:31:42 -0500 | [diff] [blame] | 1319 | /* |
| 1320 | * ISCSI_STATE_FAILED is a temp. state. The recovery |
| 1321 | * code will decide what is best to do with command queued |
| 1322 | * during this time |
| 1323 | */ |
| 1324 | if (session->state != ISCSI_STATE_LOGGED_IN && |
| 1325 | session->state != ISCSI_STATE_FAILED) { |
| 1326 | /* |
| 1327 | * to handle the race between when we set the recovery state |
| 1328 | * and block the session we requeue here (commands could |
| 1329 | * be entering our queuecommand while a block is starting |
| 1330 | * up because the block code is not locked) |
| 1331 | */ |
Mike Christie | 9000bcd | 2007-12-13 12:43:32 -0600 | [diff] [blame] | 1332 | switch (session->state) { |
| 1333 | case ISCSI_STATE_IN_RECOVERY: |
Mike Christie | 656cffc | 2006-05-18 20:31:42 -0500 | [diff] [blame] | 1334 | reason = FAILURE_SESSION_IN_RECOVERY; |
Mike Christie | d6d13ee | 2008-08-17 15:24:43 -0500 | [diff] [blame] | 1335 | goto reject; |
Mike Christie | 9000bcd | 2007-12-13 12:43:32 -0600 | [diff] [blame] | 1336 | case ISCSI_STATE_LOGGING_OUT: |
| 1337 | reason = FAILURE_SESSION_LOGGING_OUT; |
Mike Christie | d6d13ee | 2008-08-17 15:24:43 -0500 | [diff] [blame] | 1338 | goto reject; |
Mike Christie | b3a7ea8 | 2007-12-13 12:43:26 -0600 | [diff] [blame] | 1339 | case ISCSI_STATE_RECOVERY_FAILED: |
Mike Christie | 656cffc | 2006-05-18 20:31:42 -0500 | [diff] [blame] | 1340 | reason = FAILURE_SESSION_RECOVERY_TIMEOUT; |
Mike Christie | 56d7fcf | 2008-08-19 18:45:26 -0500 | [diff] [blame] | 1341 | sc->result = DID_TRANSPORT_FAILFAST << 16; |
Mike Christie | b3a7ea8 | 2007-12-13 12:43:26 -0600 | [diff] [blame] | 1342 | break; |
| 1343 | case ISCSI_STATE_TERMINATE: |
Mike Christie | 656cffc | 2006-05-18 20:31:42 -0500 | [diff] [blame] | 1344 | reason = FAILURE_SESSION_TERMINATE; |
Mike Christie | 6eabafb | 2008-01-31 13:36:43 -0600 | [diff] [blame] | 1345 | sc->result = DID_NO_CONNECT << 16; |
Mike Christie | b3a7ea8 | 2007-12-13 12:43:26 -0600 | [diff] [blame] | 1346 | break; |
Mike Christie | b3a7ea8 | 2007-12-13 12:43:26 -0600 | [diff] [blame] | 1347 | default: |
Mike Christie | 656cffc | 2006-05-18 20:31:42 -0500 | [diff] [blame] | 1348 | reason = FAILURE_SESSION_FREED; |
Mike Christie | 6eabafb | 2008-01-31 13:36:43 -0600 | [diff] [blame] | 1349 | sc->result = DID_NO_CONNECT << 16; |
Mike Christie | b3a7ea8 | 2007-12-13 12:43:26 -0600 | [diff] [blame] | 1350 | } |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1351 | goto fault; |
| 1352 | } |
| 1353 | |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1354 | conn = session->leadconn; |
Mike Christie | 9864404 | 2006-10-16 18:09:39 -0400 | [diff] [blame] | 1355 | if (!conn) { |
| 1356 | reason = FAILURE_SESSION_FREED; |
Mike Christie | 6eabafb | 2008-01-31 13:36:43 -0600 | [diff] [blame] | 1357 | sc->result = DID_NO_CONNECT << 16; |
Mike Christie | 9864404 | 2006-10-16 18:09:39 -0400 | [diff] [blame] | 1358 | goto fault; |
| 1359 | } |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1360 | |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 1361 | if (iscsi_check_cmdsn_window_closed(conn)) { |
| 1362 | reason = FAILURE_WINDOW_CLOSED; |
| 1363 | goto reject; |
| 1364 | } |
| 1365 | |
Mike Christie | 577577d | 2008-12-02 00:32:05 -0600 | [diff] [blame] | 1366 | task = iscsi_alloc_task(conn, sc); |
| 1367 | if (!task) { |
Mike Christie | 60ecebf | 2006-08-31 18:09:25 -0400 | [diff] [blame] | 1368 | reason = FAILURE_OOM; |
| 1369 | goto reject; |
| 1370 | } |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1371 | list_add_tail(&task->running, &conn->xmitqueue); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1372 | |
Mike Christie | 052d014 | 2008-05-21 15:54:05 -0500 | [diff] [blame] | 1373 | if (session->tt->caps & CAP_DATA_PATH_OFFLOAD) { |
Mike Christie | 577577d | 2008-12-02 00:32:05 -0600 | [diff] [blame] | 1374 | reason = iscsi_prep_scsi_cmd_pdu(task); |
| 1375 | if (reason) { |
| 1376 | if (reason == -ENOMEM) { |
| 1377 | reason = FAILURE_OOM; |
| 1378 | goto prepd_reject; |
| 1379 | } else { |
| 1380 | sc->result = DID_ABORT << 16; |
| 1381 | goto prepd_fault; |
| 1382 | } |
Mike Christie | 052d014 | 2008-05-21 15:54:05 -0500 | [diff] [blame] | 1383 | } |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1384 | if (session->tt->xmit_task(task)) { |
Mike Christie | 052d014 | 2008-05-21 15:54:05 -0500 | [diff] [blame] | 1385 | reason = FAILURE_SESSION_NOT_READY; |
Mike Christie | 577577d | 2008-12-02 00:32:05 -0600 | [diff] [blame] | 1386 | goto prepd_reject; |
Mike Christie | 052d014 | 2008-05-21 15:54:05 -0500 | [diff] [blame] | 1387 | } |
| 1388 | } else |
| 1389 | scsi_queue_work(session->host, &conn->xmitwork); |
| 1390 | |
| 1391 | session->queued_cmdsn++; |
| 1392 | spin_unlock(&session->lock); |
Mike Christie | 1040c99 | 2007-12-13 12:43:34 -0600 | [diff] [blame] | 1393 | spin_lock(host->host_lock); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1394 | return 0; |
| 1395 | |
Mike Christie | 577577d | 2008-12-02 00:32:05 -0600 | [diff] [blame] | 1396 | prepd_reject: |
| 1397 | sc->scsi_done = NULL; |
| 1398 | iscsi_complete_command(task); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1399 | reject: |
| 1400 | spin_unlock(&session->lock); |
| 1401 | debug_scsi("cmd 0x%x rejected (%d)\n", sc->cmnd[0], reason); |
Mike Christie | 1040c99 | 2007-12-13 12:43:34 -0600 | [diff] [blame] | 1402 | spin_lock(host->host_lock); |
Mike Christie | d6d13ee | 2008-08-17 15:24:43 -0500 | [diff] [blame] | 1403 | return SCSI_MLQUEUE_TARGET_BUSY; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1404 | |
Mike Christie | 577577d | 2008-12-02 00:32:05 -0600 | [diff] [blame] | 1405 | prepd_fault: |
| 1406 | sc->scsi_done = NULL; |
| 1407 | iscsi_complete_command(task); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1408 | fault: |
| 1409 | spin_unlock(&session->lock); |
Mike Christie | 6eabafb | 2008-01-31 13:36:43 -0600 | [diff] [blame] | 1410 | debug_scsi("iscsi: cmd 0x%x is not queued (%d)\n", sc->cmnd[0], reason); |
Boaz Harrosh | c07d444 | 2008-04-18 10:11:52 -0500 | [diff] [blame] | 1411 | if (!scsi_bidi_cmnd(sc)) |
| 1412 | scsi_set_resid(sc, scsi_bufflen(sc)); |
| 1413 | else { |
| 1414 | scsi_out(sc)->resid = scsi_out(sc)->length; |
| 1415 | scsi_in(sc)->resid = scsi_in(sc)->length; |
| 1416 | } |
Mike Christie | 052d014 | 2008-05-21 15:54:05 -0500 | [diff] [blame] | 1417 | done(sc); |
Mike Christie | 1040c99 | 2007-12-13 12:43:34 -0600 | [diff] [blame] | 1418 | spin_lock(host->host_lock); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1419 | return 0; |
| 1420 | } |
| 1421 | EXPORT_SYMBOL_GPL(iscsi_queuecommand); |
| 1422 | |
| 1423 | int iscsi_change_queue_depth(struct scsi_device *sdev, int depth) |
| 1424 | { |
| 1425 | if (depth > ISCSI_MAX_CMD_PER_LUN) |
| 1426 | depth = ISCSI_MAX_CMD_PER_LUN; |
| 1427 | scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth); |
| 1428 | return sdev->queue_depth; |
| 1429 | } |
| 1430 | EXPORT_SYMBOL_GPL(iscsi_change_queue_depth); |
| 1431 | |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1432 | void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session) |
| 1433 | { |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 1434 | struct iscsi_session *session = cls_session->dd_data; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1435 | |
| 1436 | spin_lock_bh(&session->lock); |
| 1437 | if (session->state != ISCSI_STATE_LOGGED_IN) { |
Mike Christie | 656cffc | 2006-05-18 20:31:42 -0500 | [diff] [blame] | 1438 | session->state = ISCSI_STATE_RECOVERY_FAILED; |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1439 | if (session->leadconn) |
| 1440 | wake_up(&session->leadconn->ehwait); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1441 | } |
| 1442 | spin_unlock_bh(&session->lock); |
| 1443 | } |
| 1444 | EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout); |
| 1445 | |
Mike Christie | 8e12452 | 2008-09-24 11:46:12 -0500 | [diff] [blame] | 1446 | int iscsi_eh_target_reset(struct scsi_cmnd *sc) |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1447 | { |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 1448 | struct iscsi_cls_session *cls_session; |
| 1449 | struct iscsi_session *session; |
| 1450 | struct iscsi_conn *conn; |
| 1451 | |
| 1452 | cls_session = starget_to_session(scsi_target(sc->device)); |
| 1453 | session = cls_session->dd_data; |
| 1454 | conn = session->leadconn; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1455 | |
Mike Christie | bc436b2 | 2007-12-13 12:43:28 -0600 | [diff] [blame] | 1456 | mutex_lock(&session->eh_mutex); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1457 | spin_lock_bh(&session->lock); |
| 1458 | if (session->state == ISCSI_STATE_TERMINATE) { |
| 1459 | failed: |
Mike Christie | 8e12452 | 2008-09-24 11:46:12 -0500 | [diff] [blame] | 1460 | debug_scsi("failing target reset: session terminated " |
Pete Wyckoff | d6e24d1 | 2006-11-08 15:58:32 -0600 | [diff] [blame] | 1461 | "[CID %d age %d]\n", conn->id, session->age); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1462 | spin_unlock_bh(&session->lock); |
Mike Christie | bc436b2 | 2007-12-13 12:43:28 -0600 | [diff] [blame] | 1463 | mutex_unlock(&session->eh_mutex); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1464 | return FAILED; |
| 1465 | } |
| 1466 | |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1467 | spin_unlock_bh(&session->lock); |
Mike Christie | bc436b2 | 2007-12-13 12:43:28 -0600 | [diff] [blame] | 1468 | mutex_unlock(&session->eh_mutex); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1469 | /* |
| 1470 | * we drop the lock here but the leadconn cannot be destoyed while |
| 1471 | * we are in the scsi eh |
| 1472 | */ |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1473 | iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1474 | |
Mike Christie | 8e12452 | 2008-09-24 11:46:12 -0500 | [diff] [blame] | 1475 | debug_scsi("iscsi_eh_target_reset wait for relogin\n"); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1476 | wait_event_interruptible(conn->ehwait, |
| 1477 | session->state == ISCSI_STATE_TERMINATE || |
| 1478 | session->state == ISCSI_STATE_LOGGED_IN || |
Mike Christie | 656cffc | 2006-05-18 20:31:42 -0500 | [diff] [blame] | 1479 | session->state == ISCSI_STATE_RECOVERY_FAILED); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1480 | if (signal_pending(current)) |
| 1481 | flush_signals(current); |
| 1482 | |
Mike Christie | bc436b2 | 2007-12-13 12:43:28 -0600 | [diff] [blame] | 1483 | mutex_lock(&session->eh_mutex); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1484 | spin_lock_bh(&session->lock); |
| 1485 | if (session->state == ISCSI_STATE_LOGGED_IN) |
Mike Christie | 322d739 | 2008-01-31 13:36:52 -0600 | [diff] [blame] | 1486 | iscsi_session_printk(KERN_INFO, session, |
Mike Christie | 8e12452 | 2008-09-24 11:46:12 -0500 | [diff] [blame] | 1487 | "target reset succeeded\n"); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1488 | else |
| 1489 | goto failed; |
| 1490 | spin_unlock_bh(&session->lock); |
Mike Christie | bc436b2 | 2007-12-13 12:43:28 -0600 | [diff] [blame] | 1491 | mutex_unlock(&session->eh_mutex); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1492 | return SUCCESS; |
| 1493 | } |
Mike Christie | 8e12452 | 2008-09-24 11:46:12 -0500 | [diff] [blame] | 1494 | EXPORT_SYMBOL_GPL(iscsi_eh_target_reset); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1495 | |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1496 | static void iscsi_tmf_timedout(unsigned long data) |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1497 | { |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1498 | struct iscsi_conn *conn = (struct iscsi_conn *)data; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1499 | struct iscsi_session *session = conn->session; |
| 1500 | |
| 1501 | spin_lock(&session->lock); |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1502 | if (conn->tmf_state == TMF_QUEUED) { |
| 1503 | conn->tmf_state = TMF_TIMEDOUT; |
| 1504 | debug_scsi("tmf timedout\n"); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1505 | /* unblock eh_abort() */ |
| 1506 | wake_up(&conn->ehwait); |
| 1507 | } |
| 1508 | spin_unlock(&session->lock); |
| 1509 | } |
| 1510 | |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1511 | static int iscsi_exec_task_mgmt_fn(struct iscsi_conn *conn, |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 1512 | struct iscsi_tm *hdr, int age, |
| 1513 | int timeout) |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1514 | { |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1515 | struct iscsi_session *session = conn->session; |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1516 | struct iscsi_task *task; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1517 | |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1518 | task = __iscsi_conn_send_pdu(conn, (struct iscsi_hdr *)hdr, |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1519 | NULL, 0); |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1520 | if (!task) { |
Mike Christie | 6724add | 2007-08-15 01:38:30 -0500 | [diff] [blame] | 1521 | spin_unlock_bh(&session->lock); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1522 | iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED); |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1523 | spin_lock_bh(&session->lock); |
| 1524 | debug_scsi("tmf exec failure\n"); |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 1525 | return -EPERM; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1526 | } |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1527 | conn->tmfcmd_pdus_cnt++; |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 1528 | conn->tmf_timer.expires = timeout * HZ + jiffies; |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1529 | conn->tmf_timer.function = iscsi_tmf_timedout; |
| 1530 | conn->tmf_timer.data = (unsigned long)conn; |
| 1531 | add_timer(&conn->tmf_timer); |
| 1532 | debug_scsi("tmf set timeout\n"); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1533 | |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1534 | spin_unlock_bh(&session->lock); |
Mike Christie | 6724add | 2007-08-15 01:38:30 -0500 | [diff] [blame] | 1535 | mutex_unlock(&session->eh_mutex); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1536 | |
| 1537 | /* |
| 1538 | * block eh thread until: |
| 1539 | * |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1540 | * 1) tmf response |
| 1541 | * 2) tmf timeout |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1542 | * 3) session is terminated or restarted or userspace has |
| 1543 | * given up on recovery |
| 1544 | */ |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1545 | wait_event_interruptible(conn->ehwait, age != session->age || |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1546 | session->state != ISCSI_STATE_LOGGED_IN || |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1547 | conn->tmf_state != TMF_QUEUED); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1548 | if (signal_pending(current)) |
| 1549 | flush_signals(current); |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1550 | del_timer_sync(&conn->tmf_timer); |
| 1551 | |
Mike Christie | 6724add | 2007-08-15 01:38:30 -0500 | [diff] [blame] | 1552 | mutex_lock(&session->eh_mutex); |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 1553 | spin_lock_bh(&session->lock); |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1554 | /* if the session drops it will clean up the task */ |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1555 | if (age != session->age || |
| 1556 | session->state != ISCSI_STATE_LOGGED_IN) |
| 1557 | return -ENOTCONN; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1558 | return 0; |
| 1559 | } |
| 1560 | |
| 1561 | /* |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1562 | * Fail commands. session lock held and recv side suspended and xmit |
| 1563 | * thread flushed |
| 1564 | */ |
Mike Christie | 6eabafb | 2008-01-31 13:36:43 -0600 | [diff] [blame] | 1565 | static void fail_all_commands(struct iscsi_conn *conn, unsigned lun, |
| 1566 | int error) |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1567 | { |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1568 | struct iscsi_task *task, *tmp; |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1569 | |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1570 | if (conn->task && (conn->task->sc->device->lun == lun || lun == -1)) |
| 1571 | conn->task = NULL; |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1572 | |
| 1573 | /* flush pending */ |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1574 | list_for_each_entry_safe(task, tmp, &conn->xmitqueue, running) { |
| 1575 | if (lun == task->sc->device->lun || lun == -1) { |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1576 | debug_scsi("failing pending sc %p itt 0x%x\n", |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1577 | task->sc, task->itt); |
| 1578 | fail_command(conn, task, error << 16); |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1579 | } |
| 1580 | } |
| 1581 | |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1582 | list_for_each_entry_safe(task, tmp, &conn->requeue, running) { |
| 1583 | if (lun == task->sc->device->lun || lun == -1) { |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1584 | debug_scsi("failing requeued sc %p itt 0x%x\n", |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1585 | task->sc, task->itt); |
| 1586 | fail_command(conn, task, error << 16); |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1587 | } |
| 1588 | } |
| 1589 | |
| 1590 | /* fail all other running */ |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1591 | list_for_each_entry_safe(task, tmp, &conn->run_list, running) { |
| 1592 | if (lun == task->sc->device->lun || lun == -1) { |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1593 | debug_scsi("failing in progress sc %p itt 0x%x\n", |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1594 | task->sc, task->itt); |
Mike Christie | ac26d41 | 2008-09-06 08:39:15 -0500 | [diff] [blame] | 1595 | fail_command(conn, task, error << 16); |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1596 | } |
| 1597 | } |
| 1598 | } |
| 1599 | |
Mike Christie | b40977d | 2008-05-21 15:54:03 -0500 | [diff] [blame] | 1600 | void iscsi_suspend_tx(struct iscsi_conn *conn) |
Mike Christie | 6724add | 2007-08-15 01:38:30 -0500 | [diff] [blame] | 1601 | { |
| 1602 | set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx); |
Mike Christie | 052d014 | 2008-05-21 15:54:05 -0500 | [diff] [blame] | 1603 | if (!(conn->session->tt->caps & CAP_DATA_PATH_OFFLOAD)) |
| 1604 | scsi_flush_work(conn->session->host); |
Mike Christie | 6724add | 2007-08-15 01:38:30 -0500 | [diff] [blame] | 1605 | } |
Mike Christie | b40977d | 2008-05-21 15:54:03 -0500 | [diff] [blame] | 1606 | EXPORT_SYMBOL_GPL(iscsi_suspend_tx); |
Mike Christie | 6724add | 2007-08-15 01:38:30 -0500 | [diff] [blame] | 1607 | |
| 1608 | static void iscsi_start_tx(struct iscsi_conn *conn) |
| 1609 | { |
| 1610 | clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx); |
Mike Christie | 052d014 | 2008-05-21 15:54:05 -0500 | [diff] [blame] | 1611 | if (!(conn->session->tt->caps & CAP_DATA_PATH_OFFLOAD)) |
| 1612 | scsi_queue_work(conn->session->host, &conn->xmitwork); |
Mike Christie | 6724add | 2007-08-15 01:38:30 -0500 | [diff] [blame] | 1613 | } |
| 1614 | |
Jens Axboe | 242f9dc | 2008-09-14 05:55:09 -0700 | [diff] [blame] | 1615 | static enum blk_eh_timer_return iscsi_eh_cmd_timed_out(struct scsi_cmnd *scmd) |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 1616 | { |
| 1617 | struct iscsi_cls_session *cls_session; |
| 1618 | struct iscsi_session *session; |
| 1619 | struct iscsi_conn *conn; |
Jens Axboe | 242f9dc | 2008-09-14 05:55:09 -0700 | [diff] [blame] | 1620 | enum blk_eh_timer_return rc = BLK_EH_NOT_HANDLED; |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 1621 | |
| 1622 | cls_session = starget_to_session(scsi_target(scmd->device)); |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 1623 | session = cls_session->dd_data; |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 1624 | |
| 1625 | debug_scsi("scsi cmd %p timedout\n", scmd); |
| 1626 | |
| 1627 | spin_lock(&session->lock); |
| 1628 | if (session->state != ISCSI_STATE_LOGGED_IN) { |
| 1629 | /* |
| 1630 | * We are probably in the middle of iscsi recovery so let |
| 1631 | * that complete and handle the error. |
| 1632 | */ |
Jens Axboe | 242f9dc | 2008-09-14 05:55:09 -0700 | [diff] [blame] | 1633 | rc = BLK_EH_RESET_TIMER; |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 1634 | goto done; |
| 1635 | } |
| 1636 | |
| 1637 | conn = session->leadconn; |
| 1638 | if (!conn) { |
| 1639 | /* In the middle of shuting down */ |
Jens Axboe | 242f9dc | 2008-09-14 05:55:09 -0700 | [diff] [blame] | 1640 | rc = BLK_EH_RESET_TIMER; |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 1641 | goto done; |
| 1642 | } |
| 1643 | |
| 1644 | if (!conn->recv_timeout && !conn->ping_timeout) |
| 1645 | goto done; |
| 1646 | /* |
| 1647 | * if the ping timedout then we are in the middle of cleaning up |
| 1648 | * and can let the iscsi eh handle it |
| 1649 | */ |
| 1650 | if (time_before_eq(conn->last_recv + (conn->recv_timeout * HZ) + |
| 1651 | (conn->ping_timeout * HZ), jiffies)) |
Jens Axboe | 242f9dc | 2008-09-14 05:55:09 -0700 | [diff] [blame] | 1652 | rc = BLK_EH_RESET_TIMER; |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 1653 | /* |
| 1654 | * if we are about to check the transport then give the command |
| 1655 | * more time |
| 1656 | */ |
| 1657 | if (time_before_eq(conn->last_recv + (conn->recv_timeout * HZ), |
| 1658 | jiffies)) |
Jens Axboe | 242f9dc | 2008-09-14 05:55:09 -0700 | [diff] [blame] | 1659 | rc = BLK_EH_RESET_TIMER; |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 1660 | /* if in the middle of checking the transport then give us more time */ |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1661 | if (conn->ping_task) |
Jens Axboe | 242f9dc | 2008-09-14 05:55:09 -0700 | [diff] [blame] | 1662 | rc = BLK_EH_RESET_TIMER; |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 1663 | done: |
| 1664 | spin_unlock(&session->lock); |
Jens Axboe | 242f9dc | 2008-09-14 05:55:09 -0700 | [diff] [blame] | 1665 | debug_scsi("return %s\n", rc == BLK_EH_RESET_TIMER ? |
| 1666 | "timer reset" : "nh"); |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 1667 | return rc; |
| 1668 | } |
| 1669 | |
| 1670 | static void iscsi_check_transport_timeouts(unsigned long data) |
| 1671 | { |
| 1672 | struct iscsi_conn *conn = (struct iscsi_conn *)data; |
| 1673 | struct iscsi_session *session = conn->session; |
Mike Christie | 4cf1043 | 2008-05-07 20:43:52 -0500 | [diff] [blame] | 1674 | unsigned long recv_timeout, next_timeout = 0, last_recv; |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 1675 | |
| 1676 | spin_lock(&session->lock); |
| 1677 | if (session->state != ISCSI_STATE_LOGGED_IN) |
| 1678 | goto done; |
| 1679 | |
Mike Christie | 4cf1043 | 2008-05-07 20:43:52 -0500 | [diff] [blame] | 1680 | recv_timeout = conn->recv_timeout; |
| 1681 | if (!recv_timeout) |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 1682 | goto done; |
| 1683 | |
Mike Christie | 4cf1043 | 2008-05-07 20:43:52 -0500 | [diff] [blame] | 1684 | recv_timeout *= HZ; |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 1685 | last_recv = conn->last_recv; |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1686 | if (conn->ping_task && |
Mike Christie | 4cf1043 | 2008-05-07 20:43:52 -0500 | [diff] [blame] | 1687 | time_before_eq(conn->last_ping + (conn->ping_timeout * HZ), |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 1688 | jiffies)) { |
Mike Christie | 322d739 | 2008-01-31 13:36:52 -0600 | [diff] [blame] | 1689 | iscsi_conn_printk(KERN_ERR, conn, "ping timeout of %d secs " |
| 1690 | "expired, last rx %lu, last ping %lu, " |
| 1691 | "now %lu\n", conn->ping_timeout, last_recv, |
| 1692 | conn->last_ping, jiffies); |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 1693 | spin_unlock(&session->lock); |
| 1694 | iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED); |
| 1695 | return; |
| 1696 | } |
| 1697 | |
Mike Christie | 4cf1043 | 2008-05-07 20:43:52 -0500 | [diff] [blame] | 1698 | if (time_before_eq(last_recv + recv_timeout, jiffies)) { |
Mike Christie | c8611f9 | 2008-05-08 20:15:34 -0500 | [diff] [blame] | 1699 | /* send a ping to try to provoke some traffic */ |
| 1700 | debug_scsi("Sending nopout as ping on conn %p\n", conn); |
| 1701 | iscsi_send_nopout(conn, NULL); |
Mike Christie | 4cf1043 | 2008-05-07 20:43:52 -0500 | [diff] [blame] | 1702 | next_timeout = conn->last_ping + (conn->ping_timeout * HZ); |
Mike Christie | ad294e9 | 2008-01-31 13:36:50 -0600 | [diff] [blame] | 1703 | } else |
Mike Christie | 4cf1043 | 2008-05-07 20:43:52 -0500 | [diff] [blame] | 1704 | next_timeout = last_recv + recv_timeout; |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 1705 | |
Mike Christie | ad294e9 | 2008-01-31 13:36:50 -0600 | [diff] [blame] | 1706 | debug_scsi("Setting next tmo %lu\n", next_timeout); |
| 1707 | mod_timer(&conn->transport_timer, next_timeout); |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 1708 | done: |
| 1709 | spin_unlock(&session->lock); |
| 1710 | } |
| 1711 | |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1712 | static void iscsi_prep_abort_task_pdu(struct iscsi_task *task, |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1713 | struct iscsi_tm *hdr) |
| 1714 | { |
| 1715 | memset(hdr, 0, sizeof(*hdr)); |
| 1716 | hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE; |
| 1717 | hdr->flags = ISCSI_TM_FUNC_ABORT_TASK & ISCSI_FLAG_TM_FUNC_MASK; |
| 1718 | hdr->flags |= ISCSI_FLAG_CMD_FINAL; |
Mike Christie | 577577d | 2008-12-02 00:32:05 -0600 | [diff] [blame] | 1719 | memcpy(hdr->lun, task->lun, sizeof(hdr->lun)); |
| 1720 | hdr->rtt = task->hdr_itt; |
| 1721 | hdr->refcmdsn = task->cmdsn; |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1722 | } |
| 1723 | |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1724 | int iscsi_eh_abort(struct scsi_cmnd *sc) |
| 1725 | { |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 1726 | struct iscsi_cls_session *cls_session; |
| 1727 | struct iscsi_session *session; |
Mike Christie | f47f2cf | 2006-08-31 18:09:33 -0400 | [diff] [blame] | 1728 | struct iscsi_conn *conn; |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1729 | struct iscsi_task *task; |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1730 | struct iscsi_tm *hdr; |
| 1731 | int rc, age; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1732 | |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 1733 | cls_session = starget_to_session(scsi_target(sc->device)); |
| 1734 | session = cls_session->dd_data; |
| 1735 | |
Mike Christie | 6724add | 2007-08-15 01:38:30 -0500 | [diff] [blame] | 1736 | mutex_lock(&session->eh_mutex); |
| 1737 | spin_lock_bh(&session->lock); |
Mike Christie | f47f2cf | 2006-08-31 18:09:33 -0400 | [diff] [blame] | 1738 | /* |
| 1739 | * if session was ISCSI_STATE_IN_RECOVERY then we may not have |
| 1740 | * got the command. |
| 1741 | */ |
| 1742 | if (!sc->SCp.ptr) { |
| 1743 | debug_scsi("sc never reached iscsi layer or it completed.\n"); |
Mike Christie | 6724add | 2007-08-15 01:38:30 -0500 | [diff] [blame] | 1744 | spin_unlock_bh(&session->lock); |
| 1745 | mutex_unlock(&session->eh_mutex); |
Mike Christie | f47f2cf | 2006-08-31 18:09:33 -0400 | [diff] [blame] | 1746 | return SUCCESS; |
| 1747 | } |
| 1748 | |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1749 | /* |
| 1750 | * If we are not logged in or we have started a new session |
| 1751 | * then let the host reset code handle this |
| 1752 | */ |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1753 | if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN || |
| 1754 | sc->SCp.phase != session->age) { |
| 1755 | spin_unlock_bh(&session->lock); |
| 1756 | mutex_unlock(&session->eh_mutex); |
| 1757 | return FAILED; |
| 1758 | } |
| 1759 | |
| 1760 | conn = session->leadconn; |
| 1761 | conn->eh_abort_cnt++; |
| 1762 | age = session->age; |
| 1763 | |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1764 | task = (struct iscsi_task *)sc->SCp.ptr; |
| 1765 | debug_scsi("aborting [sc %p itt 0x%x]\n", sc, task->itt); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1766 | |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1767 | /* task completed before time out */ |
| 1768 | if (!task->sc) { |
Mike Christie | 7ea8b82 | 2006-07-24 15:47:22 -0500 | [diff] [blame] | 1769 | debug_scsi("sc completed while abort in progress\n"); |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 1770 | goto success; |
Mike Christie | 7ea8b82 | 2006-07-24 15:47:22 -0500 | [diff] [blame] | 1771 | } |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1772 | |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1773 | if (task->state == ISCSI_TASK_PENDING) { |
| 1774 | fail_command(conn, task, DID_ABORT << 16); |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 1775 | goto success; |
| 1776 | } |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1777 | |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1778 | /* only have one tmf outstanding at a time */ |
| 1779 | if (conn->tmf_state != TMF_INITIAL) |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1780 | goto failed; |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1781 | conn->tmf_state = TMF_QUEUED; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1782 | |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1783 | hdr = &conn->tmhdr; |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1784 | iscsi_prep_abort_task_pdu(task, hdr); |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1785 | |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1786 | if (iscsi_exec_task_mgmt_fn(conn, hdr, age, session->abort_timeout)) { |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1787 | rc = FAILED; |
| 1788 | goto failed; |
| 1789 | } |
| 1790 | |
| 1791 | switch (conn->tmf_state) { |
| 1792 | case TMF_SUCCESS: |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 1793 | spin_unlock_bh(&session->lock); |
Mike Christie | 913e5bf | 2008-05-21 15:54:18 -0500 | [diff] [blame] | 1794 | /* |
| 1795 | * stop tx side incase the target had sent a abort rsp but |
| 1796 | * the initiator was still writing out data. |
| 1797 | */ |
Mike Christie | 6724add | 2007-08-15 01:38:30 -0500 | [diff] [blame] | 1798 | iscsi_suspend_tx(conn); |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 1799 | /* |
Mike Christie | 913e5bf | 2008-05-21 15:54:18 -0500 | [diff] [blame] | 1800 | * we do not stop the recv side because targets have been |
| 1801 | * good and have never sent us a successful tmf response |
| 1802 | * then sent more data for the cmd. |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 1803 | */ |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 1804 | spin_lock(&session->lock); |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1805 | fail_command(conn, task, DID_ABORT << 16); |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1806 | conn->tmf_state = TMF_INITIAL; |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 1807 | spin_unlock(&session->lock); |
Mike Christie | 6724add | 2007-08-15 01:38:30 -0500 | [diff] [blame] | 1808 | iscsi_start_tx(conn); |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 1809 | goto success_unlocked; |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1810 | case TMF_TIMEDOUT: |
| 1811 | spin_unlock_bh(&session->lock); |
| 1812 | iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED); |
| 1813 | goto failed_unlocked; |
| 1814 | case TMF_NOT_FOUND: |
| 1815 | if (!sc->SCp.ptr) { |
| 1816 | conn->tmf_state = TMF_INITIAL; |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1817 | /* task completed before tmf abort response */ |
Mike Christie | 7ea8b82 | 2006-07-24 15:47:22 -0500 | [diff] [blame] | 1818 | debug_scsi("sc completed while abort in progress\n"); |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 1819 | goto success; |
Mike Christie | 7ea8b82 | 2006-07-24 15:47:22 -0500 | [diff] [blame] | 1820 | } |
| 1821 | /* fall through */ |
| 1822 | default: |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1823 | conn->tmf_state = TMF_INITIAL; |
| 1824 | goto failed; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1825 | } |
| 1826 | |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 1827 | success: |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1828 | spin_unlock_bh(&session->lock); |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 1829 | success_unlocked: |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1830 | debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc, task->itt); |
Mike Christie | 6724add | 2007-08-15 01:38:30 -0500 | [diff] [blame] | 1831 | mutex_unlock(&session->eh_mutex); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1832 | return SUCCESS; |
| 1833 | |
| 1834 | failed: |
| 1835 | spin_unlock_bh(&session->lock); |
Mike Christie | 77a23c2 | 2007-05-30 12:57:18 -0500 | [diff] [blame] | 1836 | failed_unlocked: |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1837 | debug_scsi("abort failed [sc %p itt 0x%x]\n", sc, |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1838 | task ? task->itt : 0); |
Mike Christie | 6724add | 2007-08-15 01:38:30 -0500 | [diff] [blame] | 1839 | mutex_unlock(&session->eh_mutex); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1840 | return FAILED; |
| 1841 | } |
| 1842 | EXPORT_SYMBOL_GPL(iscsi_eh_abort); |
| 1843 | |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1844 | static void iscsi_prep_lun_reset_pdu(struct scsi_cmnd *sc, struct iscsi_tm *hdr) |
| 1845 | { |
| 1846 | memset(hdr, 0, sizeof(*hdr)); |
| 1847 | hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE; |
| 1848 | hdr->flags = ISCSI_TM_FUNC_LOGICAL_UNIT_RESET & ISCSI_FLAG_TM_FUNC_MASK; |
| 1849 | hdr->flags |= ISCSI_FLAG_CMD_FINAL; |
| 1850 | int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun); |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 1851 | hdr->rtt = RESERVED_ITT; |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1852 | } |
| 1853 | |
| 1854 | int iscsi_eh_device_reset(struct scsi_cmnd *sc) |
| 1855 | { |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 1856 | struct iscsi_cls_session *cls_session; |
| 1857 | struct iscsi_session *session; |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1858 | struct iscsi_conn *conn; |
| 1859 | struct iscsi_tm *hdr; |
| 1860 | int rc = FAILED; |
| 1861 | |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 1862 | cls_session = starget_to_session(scsi_target(sc->device)); |
| 1863 | session = cls_session->dd_data; |
| 1864 | |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1865 | debug_scsi("LU Reset [sc %p lun %u]\n", sc, sc->device->lun); |
| 1866 | |
| 1867 | mutex_lock(&session->eh_mutex); |
| 1868 | spin_lock_bh(&session->lock); |
| 1869 | /* |
| 1870 | * Just check if we are not logged in. We cannot check for |
| 1871 | * the phase because the reset could come from a ioctl. |
| 1872 | */ |
| 1873 | if (!session->leadconn || session->state != ISCSI_STATE_LOGGED_IN) |
| 1874 | goto unlock; |
| 1875 | conn = session->leadconn; |
| 1876 | |
| 1877 | /* only have one tmf outstanding at a time */ |
| 1878 | if (conn->tmf_state != TMF_INITIAL) |
| 1879 | goto unlock; |
| 1880 | conn->tmf_state = TMF_QUEUED; |
| 1881 | |
| 1882 | hdr = &conn->tmhdr; |
| 1883 | iscsi_prep_lun_reset_pdu(sc, hdr); |
| 1884 | |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 1885 | if (iscsi_exec_task_mgmt_fn(conn, hdr, session->age, |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 1886 | session->lu_reset_timeout)) { |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1887 | rc = FAILED; |
| 1888 | goto unlock; |
| 1889 | } |
| 1890 | |
| 1891 | switch (conn->tmf_state) { |
| 1892 | case TMF_SUCCESS: |
| 1893 | break; |
| 1894 | case TMF_TIMEDOUT: |
| 1895 | spin_unlock_bh(&session->lock); |
| 1896 | iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED); |
| 1897 | goto done; |
| 1898 | default: |
| 1899 | conn->tmf_state = TMF_INITIAL; |
| 1900 | goto unlock; |
| 1901 | } |
| 1902 | |
| 1903 | rc = SUCCESS; |
| 1904 | spin_unlock_bh(&session->lock); |
| 1905 | |
| 1906 | iscsi_suspend_tx(conn); |
Mike Christie | 913e5bf | 2008-05-21 15:54:18 -0500 | [diff] [blame] | 1907 | |
Mike Christie | a343914 | 2008-09-24 11:46:15 -0500 | [diff] [blame] | 1908 | spin_lock_bh(&session->lock); |
Mike Christie | 6eabafb | 2008-01-31 13:36:43 -0600 | [diff] [blame] | 1909 | fail_all_commands(conn, sc->device->lun, DID_ERROR); |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1910 | conn->tmf_state = TMF_INITIAL; |
Mike Christie | a343914 | 2008-09-24 11:46:15 -0500 | [diff] [blame] | 1911 | spin_unlock_bh(&session->lock); |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 1912 | |
| 1913 | iscsi_start_tx(conn); |
| 1914 | goto done; |
| 1915 | |
| 1916 | unlock: |
| 1917 | spin_unlock_bh(&session->lock); |
| 1918 | done: |
| 1919 | debug_scsi("iscsi_eh_device_reset %s\n", |
| 1920 | rc == SUCCESS ? "SUCCESS" : "FAILED"); |
| 1921 | mutex_unlock(&session->eh_mutex); |
| 1922 | return rc; |
| 1923 | } |
| 1924 | EXPORT_SYMBOL_GPL(iscsi_eh_device_reset); |
| 1925 | |
Olaf Kirch | 6320377 | 2007-12-13 12:43:25 -0600 | [diff] [blame] | 1926 | /* |
| 1927 | * Pre-allocate a pool of @max items of @item_size. By default, the pool |
| 1928 | * should be accessed via kfifo_{get,put} on q->queue. |
| 1929 | * Optionally, the caller can obtain the array of object pointers |
| 1930 | * by passing in a non-NULL @items pointer |
| 1931 | */ |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1932 | int |
Olaf Kirch | 6320377 | 2007-12-13 12:43:25 -0600 | [diff] [blame] | 1933 | iscsi_pool_init(struct iscsi_pool *q, int max, void ***items, int item_size) |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1934 | { |
Olaf Kirch | 6320377 | 2007-12-13 12:43:25 -0600 | [diff] [blame] | 1935 | int i, num_arrays = 1; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1936 | |
Olaf Kirch | 6320377 | 2007-12-13 12:43:25 -0600 | [diff] [blame] | 1937 | memset(q, 0, sizeof(*q)); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1938 | |
| 1939 | q->max = max; |
Olaf Kirch | 6320377 | 2007-12-13 12:43:25 -0600 | [diff] [blame] | 1940 | |
| 1941 | /* If the user passed an items pointer, he wants a copy of |
| 1942 | * the array. */ |
| 1943 | if (items) |
| 1944 | num_arrays++; |
| 1945 | q->pool = kzalloc(num_arrays * max * sizeof(void*), GFP_KERNEL); |
| 1946 | if (q->pool == NULL) |
| 1947 | goto enomem; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1948 | |
| 1949 | q->queue = kfifo_init((void*)q->pool, max * sizeof(void*), |
| 1950 | GFP_KERNEL, NULL); |
Olaf Kirch | 6320377 | 2007-12-13 12:43:25 -0600 | [diff] [blame] | 1951 | if (q->queue == ERR_PTR(-ENOMEM)) |
| 1952 | goto enomem; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1953 | |
| 1954 | for (i = 0; i < max; i++) { |
Olaf Kirch | 6320377 | 2007-12-13 12:43:25 -0600 | [diff] [blame] | 1955 | q->pool[i] = kzalloc(item_size, GFP_KERNEL); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1956 | if (q->pool[i] == NULL) { |
Olaf Kirch | 6320377 | 2007-12-13 12:43:25 -0600 | [diff] [blame] | 1957 | q->max = i; |
| 1958 | goto enomem; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1959 | } |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1960 | __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*)); |
| 1961 | } |
Olaf Kirch | 6320377 | 2007-12-13 12:43:25 -0600 | [diff] [blame] | 1962 | |
| 1963 | if (items) { |
| 1964 | *items = q->pool + max; |
| 1965 | memcpy(*items, q->pool, max * sizeof(void *)); |
| 1966 | } |
| 1967 | |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1968 | return 0; |
Olaf Kirch | 6320377 | 2007-12-13 12:43:25 -0600 | [diff] [blame] | 1969 | |
| 1970 | enomem: |
| 1971 | iscsi_pool_free(q); |
| 1972 | return -ENOMEM; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1973 | } |
| 1974 | EXPORT_SYMBOL_GPL(iscsi_pool_init); |
| 1975 | |
Olaf Kirch | 6320377 | 2007-12-13 12:43:25 -0600 | [diff] [blame] | 1976 | void iscsi_pool_free(struct iscsi_pool *q) |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1977 | { |
| 1978 | int i; |
| 1979 | |
| 1980 | for (i = 0; i < q->max; i++) |
Olaf Kirch | 6320377 | 2007-12-13 12:43:25 -0600 | [diff] [blame] | 1981 | kfree(q->pool[i]); |
| 1982 | if (q->pool) |
| 1983 | kfree(q->pool); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1984 | } |
| 1985 | EXPORT_SYMBOL_GPL(iscsi_pool_free); |
| 1986 | |
Mike Christie | a4804cd | 2008-05-21 15:54:00 -0500 | [diff] [blame] | 1987 | /** |
| 1988 | * iscsi_host_add - add host to system |
| 1989 | * @shost: scsi host |
| 1990 | * @pdev: parent device |
| 1991 | * |
| 1992 | * This should be called by partial offload and software iscsi drivers |
| 1993 | * to add a host to the system. |
| 1994 | */ |
| 1995 | int iscsi_host_add(struct Scsi_Host *shost, struct device *pdev) |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 1996 | { |
Mike Christie | 8e9a20c | 2008-06-16 10:11:33 -0500 | [diff] [blame] | 1997 | if (!shost->can_queue) |
| 1998 | shost->can_queue = ISCSI_DEF_XMIT_CMDS_MAX; |
| 1999 | |
Mike Christie | a4804cd | 2008-05-21 15:54:00 -0500 | [diff] [blame] | 2000 | return scsi_add_host(shost, pdev); |
| 2001 | } |
| 2002 | EXPORT_SYMBOL_GPL(iscsi_host_add); |
| 2003 | |
| 2004 | /** |
| 2005 | * iscsi_host_alloc - allocate a host and driver data |
| 2006 | * @sht: scsi host template |
| 2007 | * @dd_data_size: driver host data size |
| 2008 | * @qdepth: default device queue depth |
| 2009 | * |
| 2010 | * This should be called by partial offload and software iscsi drivers. |
| 2011 | * To access the driver specific memory use the iscsi_host_priv() macro. |
| 2012 | */ |
| 2013 | struct Scsi_Host *iscsi_host_alloc(struct scsi_host_template *sht, |
| 2014 | int dd_data_size, uint16_t qdepth) |
| 2015 | { |
| 2016 | struct Scsi_Host *shost; |
Mike Christie | e5bd7b5 | 2008-09-24 11:46:10 -0500 | [diff] [blame] | 2017 | struct iscsi_host *ihost; |
Mike Christie | a4804cd | 2008-05-21 15:54:00 -0500 | [diff] [blame] | 2018 | |
| 2019 | shost = scsi_host_alloc(sht, sizeof(struct iscsi_host) + dd_data_size); |
| 2020 | if (!shost) |
| 2021 | return NULL; |
| 2022 | shost->transportt->eh_timed_out = iscsi_eh_cmd_timed_out; |
| 2023 | |
Mike Christie | 1548271 | 2007-05-30 12:57:19 -0500 | [diff] [blame] | 2024 | if (qdepth > ISCSI_MAX_CMD_PER_LUN || qdepth < 1) { |
| 2025 | if (qdepth != 0) |
| 2026 | printk(KERN_ERR "iscsi: invalid queue depth of %d. " |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 2027 | "Queue depth must be between 1 and %d.\n", |
| 2028 | qdepth, ISCSI_MAX_CMD_PER_LUN); |
Mike Christie | 1548271 | 2007-05-30 12:57:19 -0500 | [diff] [blame] | 2029 | qdepth = ISCSI_DEF_CMD_PER_LUN; |
| 2030 | } |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 2031 | shost->cmd_per_lun = qdepth; |
Mike Christie | e5bd7b5 | 2008-09-24 11:46:10 -0500 | [diff] [blame] | 2032 | |
| 2033 | ihost = shost_priv(shost); |
| 2034 | spin_lock_init(&ihost->lock); |
| 2035 | ihost->state = ISCSI_HOST_SETUP; |
| 2036 | ihost->num_sessions = 0; |
| 2037 | init_waitqueue_head(&ihost->session_removal_wq); |
Mike Christie | a4804cd | 2008-05-21 15:54:00 -0500 | [diff] [blame] | 2038 | return shost; |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 2039 | } |
Mike Christie | a4804cd | 2008-05-21 15:54:00 -0500 | [diff] [blame] | 2040 | EXPORT_SYMBOL_GPL(iscsi_host_alloc); |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 2041 | |
Mike Christie | e5bd7b5 | 2008-09-24 11:46:10 -0500 | [diff] [blame] | 2042 | static void iscsi_notify_host_removed(struct iscsi_cls_session *cls_session) |
| 2043 | { |
| 2044 | iscsi_session_failure(cls_session, ISCSI_ERR_INVALID_HOST); |
| 2045 | } |
| 2046 | |
Mike Christie | a4804cd | 2008-05-21 15:54:00 -0500 | [diff] [blame] | 2047 | /** |
| 2048 | * iscsi_host_remove - remove host and sessions |
| 2049 | * @shost: scsi host |
| 2050 | * |
Mike Christie | e5bd7b5 | 2008-09-24 11:46:10 -0500 | [diff] [blame] | 2051 | * If there are any sessions left, this will initiate the removal and wait |
| 2052 | * for the completion. |
Mike Christie | a4804cd | 2008-05-21 15:54:00 -0500 | [diff] [blame] | 2053 | */ |
| 2054 | void iscsi_host_remove(struct Scsi_Host *shost) |
| 2055 | { |
Mike Christie | e5bd7b5 | 2008-09-24 11:46:10 -0500 | [diff] [blame] | 2056 | struct iscsi_host *ihost = shost_priv(shost); |
| 2057 | unsigned long flags; |
| 2058 | |
| 2059 | spin_lock_irqsave(&ihost->lock, flags); |
| 2060 | ihost->state = ISCSI_HOST_REMOVED; |
| 2061 | spin_unlock_irqrestore(&ihost->lock, flags); |
| 2062 | |
| 2063 | iscsi_host_for_each_session(shost, iscsi_notify_host_removed); |
| 2064 | wait_event_interruptible(ihost->session_removal_wq, |
| 2065 | ihost->num_sessions == 0); |
| 2066 | if (signal_pending(current)) |
| 2067 | flush_signals(current); |
| 2068 | |
Mike Christie | a4804cd | 2008-05-21 15:54:00 -0500 | [diff] [blame] | 2069 | scsi_remove_host(shost); |
| 2070 | } |
| 2071 | EXPORT_SYMBOL_GPL(iscsi_host_remove); |
| 2072 | |
| 2073 | void iscsi_host_free(struct Scsi_Host *shost) |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 2074 | { |
| 2075 | struct iscsi_host *ihost = shost_priv(shost); |
| 2076 | |
| 2077 | kfree(ihost->netdev); |
| 2078 | kfree(ihost->hwaddress); |
| 2079 | kfree(ihost->initiatorname); |
Mike Christie | a4804cd | 2008-05-21 15:54:00 -0500 | [diff] [blame] | 2080 | scsi_host_put(shost); |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 2081 | } |
Mike Christie | a4804cd | 2008-05-21 15:54:00 -0500 | [diff] [blame] | 2082 | EXPORT_SYMBOL_GPL(iscsi_host_free); |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 2083 | |
Mike Christie | e5bd7b5 | 2008-09-24 11:46:10 -0500 | [diff] [blame] | 2084 | static void iscsi_host_dec_session_cnt(struct Scsi_Host *shost) |
| 2085 | { |
| 2086 | struct iscsi_host *ihost = shost_priv(shost); |
| 2087 | unsigned long flags; |
| 2088 | |
| 2089 | shost = scsi_host_get(shost); |
| 2090 | if (!shost) { |
| 2091 | printk(KERN_ERR "Invalid state. Cannot notify host removal " |
| 2092 | "of session teardown event because host already " |
| 2093 | "removed.\n"); |
| 2094 | return; |
| 2095 | } |
| 2096 | |
| 2097 | spin_lock_irqsave(&ihost->lock, flags); |
| 2098 | ihost->num_sessions--; |
| 2099 | if (ihost->num_sessions == 0) |
| 2100 | wake_up(&ihost->session_removal_wq); |
| 2101 | spin_unlock_irqrestore(&ihost->lock, flags); |
| 2102 | scsi_host_put(shost); |
| 2103 | } |
| 2104 | |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 2105 | /** |
| 2106 | * iscsi_session_setup - create iscsi cls session and host and session |
| 2107 | * @iscsit: iscsi transport template |
| 2108 | * @shost: scsi host |
| 2109 | * @cmds_max: session can queue |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 2110 | * @cmd_task_size: LLD task private data size |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 2111 | * @initial_cmdsn: initial CmdSN |
| 2112 | * |
| 2113 | * This can be used by software iscsi_transports that allocate |
| 2114 | * a session per scsi host. |
Mike Christie | 3cf7b23 | 2008-05-21 15:54:17 -0500 | [diff] [blame] | 2115 | * |
| 2116 | * Callers should set cmds_max to the largest total numer (mgmt + scsi) of |
| 2117 | * tasks they support. The iscsi layer reserves ISCSI_MGMT_CMDS_MAX tasks |
| 2118 | * for nop handling and login/logout requests. |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 2119 | */ |
| 2120 | struct iscsi_cls_session * |
| 2121 | iscsi_session_setup(struct iscsi_transport *iscsit, struct Scsi_Host *shost, |
Mike Christie | 3cf7b23 | 2008-05-21 15:54:17 -0500 | [diff] [blame] | 2122 | uint16_t cmds_max, int cmd_task_size, |
Mike Christie | 7970634 | 2008-05-21 15:54:12 -0500 | [diff] [blame] | 2123 | uint32_t initial_cmdsn, unsigned int id) |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 2124 | { |
Mike Christie | e5bd7b5 | 2008-09-24 11:46:10 -0500 | [diff] [blame] | 2125 | struct iscsi_host *ihost = shost_priv(shost); |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 2126 | struct iscsi_session *session; |
| 2127 | struct iscsi_cls_session *cls_session; |
Mike Christie | 3cf7b23 | 2008-05-21 15:54:17 -0500 | [diff] [blame] | 2128 | int cmd_i, scsi_cmds, total_cmds = cmds_max; |
Mike Christie | e5bd7b5 | 2008-09-24 11:46:10 -0500 | [diff] [blame] | 2129 | unsigned long flags; |
| 2130 | |
| 2131 | spin_lock_irqsave(&ihost->lock, flags); |
| 2132 | if (ihost->state == ISCSI_HOST_REMOVED) { |
| 2133 | spin_unlock_irqrestore(&ihost->lock, flags); |
| 2134 | return NULL; |
| 2135 | } |
| 2136 | ihost->num_sessions++; |
| 2137 | spin_unlock_irqrestore(&ihost->lock, flags); |
Mike Christie | 8e9a20c | 2008-06-16 10:11:33 -0500 | [diff] [blame] | 2138 | |
| 2139 | if (!total_cmds) |
| 2140 | total_cmds = ISCSI_DEF_XMIT_CMDS_MAX; |
Mike Christie | 3e5c28a | 2008-05-21 15:54:06 -0500 | [diff] [blame] | 2141 | /* |
Mike Christie | 3cf7b23 | 2008-05-21 15:54:17 -0500 | [diff] [blame] | 2142 | * The iscsi layer needs some tasks for nop handling and tmfs, |
| 2143 | * so the cmds_max must at least be greater than ISCSI_MGMT_CMDS_MAX |
| 2144 | * + 1 command for scsi IO. |
Mike Christie | 3e5c28a | 2008-05-21 15:54:06 -0500 | [diff] [blame] | 2145 | */ |
Mike Christie | 3cf7b23 | 2008-05-21 15:54:17 -0500 | [diff] [blame] | 2146 | if (total_cmds < ISCSI_TOTAL_CMDS_MIN) { |
| 2147 | printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue " |
| 2148 | "must be a power of two that is at least %d.\n", |
| 2149 | total_cmds, ISCSI_TOTAL_CMDS_MIN); |
Mike Christie | e5bd7b5 | 2008-09-24 11:46:10 -0500 | [diff] [blame] | 2150 | goto dec_session_count; |
Mike Christie | 1548271 | 2007-05-30 12:57:19 -0500 | [diff] [blame] | 2151 | } |
Mike Christie | 3cf7b23 | 2008-05-21 15:54:17 -0500 | [diff] [blame] | 2152 | |
| 2153 | if (total_cmds > ISCSI_TOTAL_CMDS_MAX) { |
| 2154 | printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue " |
| 2155 | "must be a power of 2 less than or equal to %d.\n", |
| 2156 | cmds_max, ISCSI_TOTAL_CMDS_MAX); |
| 2157 | total_cmds = ISCSI_TOTAL_CMDS_MAX; |
| 2158 | } |
| 2159 | |
| 2160 | if (!is_power_of_2(total_cmds)) { |
| 2161 | printk(KERN_ERR "iscsi: invalid can_queue of %d. can_queue " |
| 2162 | "must be a power of 2.\n", total_cmds); |
| 2163 | total_cmds = rounddown_pow_of_two(total_cmds); |
| 2164 | if (total_cmds < ISCSI_TOTAL_CMDS_MIN) |
| 2165 | return NULL; |
| 2166 | printk(KERN_INFO "iscsi: Rounding can_queue to %d.\n", |
| 2167 | total_cmds); |
| 2168 | } |
| 2169 | scsi_cmds = total_cmds - ISCSI_MGMT_CMDS_MAX; |
Mike Christie | 1548271 | 2007-05-30 12:57:19 -0500 | [diff] [blame] | 2170 | |
Mike Christie | 5d91e20 | 2008-05-21 15:54:01 -0500 | [diff] [blame] | 2171 | cls_session = iscsi_alloc_session(shost, iscsit, |
| 2172 | sizeof(struct iscsi_session)); |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 2173 | if (!cls_session) |
Mike Christie | e5bd7b5 | 2008-09-24 11:46:10 -0500 | [diff] [blame] | 2174 | goto dec_session_count; |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 2175 | session = cls_session->dd_data; |
| 2176 | session->cls_session = cls_session; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2177 | session->host = shost; |
| 2178 | session->state = ISCSI_STATE_FREE; |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 2179 | session->fast_abort = 1; |
Mike Christie | 4cd49ea | 2007-12-13 12:43:38 -0600 | [diff] [blame] | 2180 | session->lu_reset_timeout = 15; |
| 2181 | session->abort_timeout = 10; |
Mike Christie | 3cf7b23 | 2008-05-21 15:54:17 -0500 | [diff] [blame] | 2182 | session->scsi_cmds_max = scsi_cmds; |
| 2183 | session->cmds_max = total_cmds; |
Mike Christie | e072640 | 2007-07-26 12:46:48 -0500 | [diff] [blame] | 2184 | session->queued_cmdsn = session->cmdsn = initial_cmdsn; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2185 | session->exp_cmdsn = initial_cmdsn + 1; |
| 2186 | session->max_cmdsn = initial_cmdsn + 1; |
| 2187 | session->max_r2t = 1; |
| 2188 | session->tt = iscsit; |
Mike Christie | 6724add | 2007-08-15 01:38:30 -0500 | [diff] [blame] | 2189 | mutex_init(&session->eh_mutex); |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 2190 | spin_lock_init(&session->lock); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2191 | |
| 2192 | /* initialize SCSI PDU commands pool */ |
| 2193 | if (iscsi_pool_init(&session->cmdpool, session->cmds_max, |
| 2194 | (void***)&session->cmds, |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 2195 | cmd_task_size + sizeof(struct iscsi_task))) |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2196 | goto cmdpool_alloc_fail; |
| 2197 | |
| 2198 | /* pre-format cmds pool with ITT */ |
| 2199 | for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) { |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 2200 | struct iscsi_task *task = session->cmds[cmd_i]; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2201 | |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 2202 | if (cmd_task_size) |
| 2203 | task->dd_data = &task[1]; |
| 2204 | task->itt = cmd_i; |
| 2205 | INIT_LIST_HEAD(&task->running); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2206 | } |
| 2207 | |
Mike Christie | f53a88d | 2006-06-28 12:00:27 -0500 | [diff] [blame] | 2208 | if (!try_module_get(iscsit->owner)) |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 2209 | goto module_get_fail; |
| 2210 | |
Mike Christie | 7970634 | 2008-05-21 15:54:12 -0500 | [diff] [blame] | 2211 | if (iscsi_add_session(cls_session, id)) |
Mike Christie | f53a88d | 2006-06-28 12:00:27 -0500 | [diff] [blame] | 2212 | goto cls_session_fail; |
Mike Christie | e5bd7b5 | 2008-09-24 11:46:10 -0500 | [diff] [blame] | 2213 | |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2214 | return cls_session; |
| 2215 | |
| 2216 | cls_session_fail: |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 2217 | module_put(iscsit->owner); |
| 2218 | module_get_fail: |
Olaf Kirch | 6320377 | 2007-12-13 12:43:25 -0600 | [diff] [blame] | 2219 | iscsi_pool_free(&session->cmdpool); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2220 | cmdpool_alloc_fail: |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 2221 | iscsi_free_session(cls_session); |
Mike Christie | e5bd7b5 | 2008-09-24 11:46:10 -0500 | [diff] [blame] | 2222 | dec_session_count: |
| 2223 | iscsi_host_dec_session_cnt(shost); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2224 | return NULL; |
| 2225 | } |
| 2226 | EXPORT_SYMBOL_GPL(iscsi_session_setup); |
| 2227 | |
| 2228 | /** |
| 2229 | * iscsi_session_teardown - destroy session, host, and cls_session |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 2230 | * @cls_session: iscsi session |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2231 | * |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 2232 | * The driver must have called iscsi_remove_session before |
| 2233 | * calling this. |
| 2234 | */ |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2235 | void iscsi_session_teardown(struct iscsi_cls_session *cls_session) |
| 2236 | { |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 2237 | struct iscsi_session *session = cls_session->dd_data; |
Mike Christie | 63f75cc | 2006-07-24 15:47:29 -0500 | [diff] [blame] | 2238 | struct module *owner = cls_session->transport->owner; |
Mike Christie | e5bd7b5 | 2008-09-24 11:46:10 -0500 | [diff] [blame] | 2239 | struct Scsi_Host *shost = session->host; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2240 | |
Olaf Kirch | 6320377 | 2007-12-13 12:43:25 -0600 | [diff] [blame] | 2241 | iscsi_pool_free(&session->cmdpool); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2242 | |
Mike Christie | b2c6416 | 2007-05-30 12:57:16 -0500 | [diff] [blame] | 2243 | kfree(session->password); |
| 2244 | kfree(session->password_in); |
| 2245 | kfree(session->username); |
| 2246 | kfree(session->username_in); |
Mike Christie | f3ff0c3 | 2006-07-24 15:47:50 -0500 | [diff] [blame] | 2247 | kfree(session->targetname); |
Mike Christie | 88dfd34 | 2008-05-21 15:54:16 -0500 | [diff] [blame] | 2248 | kfree(session->initiatorname); |
| 2249 | kfree(session->ifacename); |
Mike Christie | f3ff0c3 | 2006-07-24 15:47:50 -0500 | [diff] [blame] | 2250 | |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 2251 | iscsi_destroy_session(cls_session); |
Mike Christie | e5bd7b5 | 2008-09-24 11:46:10 -0500 | [diff] [blame] | 2252 | iscsi_host_dec_session_cnt(shost); |
Mike Christie | 63f75cc | 2006-07-24 15:47:29 -0500 | [diff] [blame] | 2253 | module_put(owner); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2254 | } |
| 2255 | EXPORT_SYMBOL_GPL(iscsi_session_teardown); |
| 2256 | |
| 2257 | /** |
| 2258 | * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn |
| 2259 | * @cls_session: iscsi_cls_session |
Mike Christie | 5d91e20 | 2008-05-21 15:54:01 -0500 | [diff] [blame] | 2260 | * @dd_size: private driver data size |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2261 | * @conn_idx: cid |
Mike Christie | 5d91e20 | 2008-05-21 15:54:01 -0500 | [diff] [blame] | 2262 | */ |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2263 | struct iscsi_cls_conn * |
Mike Christie | 5d91e20 | 2008-05-21 15:54:01 -0500 | [diff] [blame] | 2264 | iscsi_conn_setup(struct iscsi_cls_session *cls_session, int dd_size, |
| 2265 | uint32_t conn_idx) |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2266 | { |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 2267 | struct iscsi_session *session = cls_session->dd_data; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2268 | struct iscsi_conn *conn; |
| 2269 | struct iscsi_cls_conn *cls_conn; |
Mike Christie | d36ab6f | 2006-05-18 20:31:34 -0500 | [diff] [blame] | 2270 | char *data; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2271 | |
Mike Christie | 5d91e20 | 2008-05-21 15:54:01 -0500 | [diff] [blame] | 2272 | cls_conn = iscsi_create_conn(cls_session, sizeof(*conn) + dd_size, |
| 2273 | conn_idx); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2274 | if (!cls_conn) |
| 2275 | return NULL; |
| 2276 | conn = cls_conn->dd_data; |
Mike Christie | 5d91e20 | 2008-05-21 15:54:01 -0500 | [diff] [blame] | 2277 | memset(conn, 0, sizeof(*conn) + dd_size); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2278 | |
Mike Christie | 5d91e20 | 2008-05-21 15:54:01 -0500 | [diff] [blame] | 2279 | conn->dd_data = cls_conn->dd_data + sizeof(*conn); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2280 | conn->session = session; |
| 2281 | conn->cls_conn = cls_conn; |
| 2282 | conn->c_stage = ISCSI_CONN_INITIAL_STAGE; |
| 2283 | conn->id = conn_idx; |
| 2284 | conn->exp_statsn = 0; |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 2285 | conn->tmf_state = TMF_INITIAL; |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 2286 | |
| 2287 | init_timer(&conn->transport_timer); |
| 2288 | conn->transport_timer.data = (unsigned long)conn; |
| 2289 | conn->transport_timer.function = iscsi_check_transport_timeouts; |
| 2290 | |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2291 | INIT_LIST_HEAD(&conn->run_list); |
| 2292 | INIT_LIST_HEAD(&conn->mgmt_run_list); |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 2293 | INIT_LIST_HEAD(&conn->mgmtqueue); |
Mike Christie | b6c395e | 2006-07-24 15:47:15 -0500 | [diff] [blame] | 2294 | INIT_LIST_HEAD(&conn->xmitqueue); |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 2295 | INIT_LIST_HEAD(&conn->requeue); |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 2296 | INIT_WORK(&conn->xmitwork, iscsi_xmitworker); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2297 | |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 2298 | /* allocate login_task used for the login/text sequences */ |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2299 | spin_lock_bh(&session->lock); |
Mike Christie | 3e5c28a | 2008-05-21 15:54:06 -0500 | [diff] [blame] | 2300 | if (!__kfifo_get(session->cmdpool.queue, |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 2301 | (void*)&conn->login_task, |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2302 | sizeof(void*))) { |
| 2303 | spin_unlock_bh(&session->lock); |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 2304 | goto login_task_alloc_fail; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2305 | } |
| 2306 | spin_unlock_bh(&session->lock); |
| 2307 | |
Mike Christie | cfeb2cf | 2008-12-02 00:32:09 -0600 | [diff] [blame] | 2308 | data = (char *) __get_free_pages(GFP_KERNEL, |
| 2309 | get_order(ISCSI_DEF_MAX_RECV_SEG_LEN)); |
Mike Christie | d36ab6f | 2006-05-18 20:31:34 -0500 | [diff] [blame] | 2310 | if (!data) |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 2311 | goto login_task_data_alloc_fail; |
| 2312 | conn->login_task->data = conn->data = data; |
Mike Christie | d36ab6f | 2006-05-18 20:31:34 -0500 | [diff] [blame] | 2313 | |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 2314 | init_timer(&conn->tmf_timer); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2315 | init_waitqueue_head(&conn->ehwait); |
| 2316 | |
| 2317 | return cls_conn; |
| 2318 | |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 2319 | login_task_data_alloc_fail: |
| 2320 | __kfifo_put(session->cmdpool.queue, (void*)&conn->login_task, |
Mike Christie | d36ab6f | 2006-05-18 20:31:34 -0500 | [diff] [blame] | 2321 | sizeof(void*)); |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 2322 | login_task_alloc_fail: |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2323 | iscsi_destroy_conn(cls_conn); |
| 2324 | return NULL; |
| 2325 | } |
| 2326 | EXPORT_SYMBOL_GPL(iscsi_conn_setup); |
| 2327 | |
| 2328 | /** |
| 2329 | * iscsi_conn_teardown - teardown iscsi connection |
| 2330 | * cls_conn: iscsi class connection |
| 2331 | * |
| 2332 | * TODO: we may need to make this into a two step process |
| 2333 | * like scsi-mls remove + put host |
| 2334 | */ |
| 2335 | void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn) |
| 2336 | { |
| 2337 | struct iscsi_conn *conn = cls_conn->dd_data; |
| 2338 | struct iscsi_session *session = conn->session; |
| 2339 | unsigned long flags; |
| 2340 | |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 2341 | del_timer_sync(&conn->transport_timer); |
| 2342 | |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2343 | spin_lock_bh(&session->lock); |
| 2344 | conn->c_stage = ISCSI_CONN_CLEANUP_WAIT; |
| 2345 | if (session->leadconn == conn) { |
| 2346 | /* |
| 2347 | * leading connection? then give up on recovery. |
| 2348 | */ |
| 2349 | session->state = ISCSI_STATE_TERMINATE; |
| 2350 | wake_up(&conn->ehwait); |
| 2351 | } |
| 2352 | spin_unlock_bh(&session->lock); |
| 2353 | |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2354 | /* |
| 2355 | * Block until all in-progress commands for this connection |
| 2356 | * time out or fail. |
| 2357 | */ |
| 2358 | for (;;) { |
| 2359 | spin_lock_irqsave(session->host->host_lock, flags); |
| 2360 | if (!session->host->host_busy) { /* OK for ERL == 0 */ |
| 2361 | spin_unlock_irqrestore(session->host->host_lock, flags); |
| 2362 | break; |
| 2363 | } |
| 2364 | spin_unlock_irqrestore(session->host->host_lock, flags); |
| 2365 | msleep_interruptible(500); |
Mike Christie | 322d739 | 2008-01-31 13:36:52 -0600 | [diff] [blame] | 2366 | iscsi_conn_printk(KERN_INFO, conn, "iscsi conn_destroy(): " |
| 2367 | "host_busy %d host_failed %d\n", |
| 2368 | session->host->host_busy, |
| 2369 | session->host->host_failed); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2370 | /* |
| 2371 | * force eh_abort() to unblock |
| 2372 | */ |
| 2373 | wake_up(&conn->ehwait); |
| 2374 | } |
| 2375 | |
Mike Christie | 779ea12 | 2007-02-28 17:32:15 -0600 | [diff] [blame] | 2376 | /* flush queued up work because we free the connection below */ |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 2377 | iscsi_suspend_tx(conn); |
Mike Christie | 779ea12 | 2007-02-28 17:32:15 -0600 | [diff] [blame] | 2378 | |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2379 | spin_lock_bh(&session->lock); |
Mike Christie | cfeb2cf | 2008-12-02 00:32:09 -0600 | [diff] [blame] | 2380 | free_pages((unsigned long) conn->data, |
| 2381 | get_order(ISCSI_DEF_MAX_RECV_SEG_LEN)); |
Mike Christie | f3ff0c3 | 2006-07-24 15:47:50 -0500 | [diff] [blame] | 2382 | kfree(conn->persistent_address); |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 2383 | __kfifo_put(session->cmdpool.queue, (void*)&conn->login_task, |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2384 | sizeof(void*)); |
Mike Christie | e072640 | 2007-07-26 12:46:48 -0500 | [diff] [blame] | 2385 | if (session->leadconn == conn) |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2386 | session->leadconn = NULL; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2387 | spin_unlock_bh(&session->lock); |
| 2388 | |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2389 | iscsi_destroy_conn(cls_conn); |
| 2390 | } |
| 2391 | EXPORT_SYMBOL_GPL(iscsi_conn_teardown); |
| 2392 | |
| 2393 | int iscsi_conn_start(struct iscsi_cls_conn *cls_conn) |
| 2394 | { |
| 2395 | struct iscsi_conn *conn = cls_conn->dd_data; |
| 2396 | struct iscsi_session *session = conn->session; |
| 2397 | |
Mike Christie | ffd0436 | 2006-08-31 18:09:24 -0400 | [diff] [blame] | 2398 | if (!session) { |
Mike Christie | 322d739 | 2008-01-31 13:36:52 -0600 | [diff] [blame] | 2399 | iscsi_conn_printk(KERN_ERR, conn, |
| 2400 | "can't start unbound connection\n"); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2401 | return -EPERM; |
| 2402 | } |
| 2403 | |
Mike Christie | db98ccd | 2006-08-31 18:09:31 -0400 | [diff] [blame] | 2404 | if ((session->imm_data_en || !session->initial_r2t_en) && |
| 2405 | session->first_burst > session->max_burst) { |
Mike Christie | 322d739 | 2008-01-31 13:36:52 -0600 | [diff] [blame] | 2406 | iscsi_conn_printk(KERN_INFO, conn, "invalid burst lengths: " |
| 2407 | "first_burst %d max_burst %d\n", |
| 2408 | session->first_burst, session->max_burst); |
Mike Christie | ffd0436 | 2006-08-31 18:09:24 -0400 | [diff] [blame] | 2409 | return -EINVAL; |
| 2410 | } |
| 2411 | |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 2412 | if (conn->ping_timeout && !conn->recv_timeout) { |
Mike Christie | 322d739 | 2008-01-31 13:36:52 -0600 | [diff] [blame] | 2413 | iscsi_conn_printk(KERN_ERR, conn, "invalid recv timeout of " |
| 2414 | "zero. Using 5 seconds\n."); |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 2415 | conn->recv_timeout = 5; |
| 2416 | } |
| 2417 | |
| 2418 | if (conn->recv_timeout && !conn->ping_timeout) { |
Mike Christie | 322d739 | 2008-01-31 13:36:52 -0600 | [diff] [blame] | 2419 | iscsi_conn_printk(KERN_ERR, conn, "invalid ping timeout of " |
| 2420 | "zero. Using 5 seconds.\n"); |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 2421 | conn->ping_timeout = 5; |
| 2422 | } |
| 2423 | |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2424 | spin_lock_bh(&session->lock); |
| 2425 | conn->c_stage = ISCSI_CONN_STARTED; |
| 2426 | session->state = ISCSI_STATE_LOGGED_IN; |
Mike Christie | e072640 | 2007-07-26 12:46:48 -0500 | [diff] [blame] | 2427 | session->queued_cmdsn = session->cmdsn; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2428 | |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 2429 | conn->last_recv = jiffies; |
| 2430 | conn->last_ping = jiffies; |
| 2431 | if (conn->recv_timeout && conn->ping_timeout) |
| 2432 | mod_timer(&conn->transport_timer, |
| 2433 | jiffies + (conn->recv_timeout * HZ)); |
| 2434 | |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2435 | switch(conn->stop_stage) { |
| 2436 | case STOP_CONN_RECOVER: |
| 2437 | /* |
| 2438 | * unblock eh_abort() if it is blocked. re-try all |
| 2439 | * commands after successful recovery |
| 2440 | */ |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2441 | conn->stop_stage = 0; |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 2442 | conn->tmf_state = TMF_INITIAL; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2443 | session->age++; |
Mike Christie | 8b1d034 | 2008-01-31 13:36:53 -0600 | [diff] [blame] | 2444 | if (session->age == 16) |
| 2445 | session->age = 0; |
Mike Christie | 6eabafb | 2008-01-31 13:36:43 -0600 | [diff] [blame] | 2446 | break; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2447 | case STOP_CONN_TERM: |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2448 | conn->stop_stage = 0; |
| 2449 | break; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2450 | default: |
| 2451 | break; |
| 2452 | } |
| 2453 | spin_unlock_bh(&session->lock); |
| 2454 | |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 2455 | iscsi_unblock_session(session->cls_session); |
Mike Christie | 6eabafb | 2008-01-31 13:36:43 -0600 | [diff] [blame] | 2456 | wake_up(&conn->ehwait); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2457 | return 0; |
| 2458 | } |
| 2459 | EXPORT_SYMBOL_GPL(iscsi_conn_start); |
| 2460 | |
| 2461 | static void |
| 2462 | flush_control_queues(struct iscsi_session *session, struct iscsi_conn *conn) |
| 2463 | { |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 2464 | struct iscsi_task *task, *tmp; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2465 | |
| 2466 | /* handle pending */ |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 2467 | list_for_each_entry_safe(task, tmp, &conn->mgmtqueue, running) { |
| 2468 | debug_scsi("flushing pending mgmt task itt 0x%x\n", task->itt); |
| 2469 | /* release ref from prep task */ |
| 2470 | __iscsi_put_task(task); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2471 | } |
| 2472 | |
| 2473 | /* handle running */ |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 2474 | list_for_each_entry_safe(task, tmp, &conn->mgmt_run_list, running) { |
| 2475 | debug_scsi("flushing running mgmt task itt 0x%x\n", task->itt); |
| 2476 | /* release ref from prep task */ |
| 2477 | __iscsi_put_task(task); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2478 | } |
| 2479 | |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 2480 | conn->task = NULL; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2481 | } |
| 2482 | |
Mike Christie | 656cffc | 2006-05-18 20:31:42 -0500 | [diff] [blame] | 2483 | static void iscsi_start_session_recovery(struct iscsi_session *session, |
| 2484 | struct iscsi_conn *conn, int flag) |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2485 | { |
Mike Christie | ed2abc7 | 2006-05-02 19:46:40 -0500 | [diff] [blame] | 2486 | int old_stop_stage; |
| 2487 | |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 2488 | del_timer_sync(&conn->transport_timer); |
| 2489 | |
Mike Christie | 6724add | 2007-08-15 01:38:30 -0500 | [diff] [blame] | 2490 | mutex_lock(&session->eh_mutex); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2491 | spin_lock_bh(&session->lock); |
Mike Christie | ed2abc7 | 2006-05-02 19:46:40 -0500 | [diff] [blame] | 2492 | if (conn->stop_stage == STOP_CONN_TERM) { |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2493 | spin_unlock_bh(&session->lock); |
Mike Christie | 6724add | 2007-08-15 01:38:30 -0500 | [diff] [blame] | 2494 | mutex_unlock(&session->eh_mutex); |
| 2495 | return; |
| 2496 | } |
| 2497 | |
| 2498 | /* |
Mike Christie | ed2abc7 | 2006-05-02 19:46:40 -0500 | [diff] [blame] | 2499 | * When this is called for the in_login state, we only want to clean |
Mike Christie | 9c19a7d | 2008-05-21 15:54:09 -0500 | [diff] [blame] | 2500 | * up the login task and connection. We do not need to block and set |
Mike Christie | 67a6111 | 2006-05-30 00:37:20 -0500 | [diff] [blame] | 2501 | * the recovery state again |
Mike Christie | ed2abc7 | 2006-05-02 19:46:40 -0500 | [diff] [blame] | 2502 | */ |
Mike Christie | 67a6111 | 2006-05-30 00:37:20 -0500 | [diff] [blame] | 2503 | if (flag == STOP_CONN_TERM) |
| 2504 | session->state = ISCSI_STATE_TERMINATE; |
| 2505 | else if (conn->stop_stage != STOP_CONN_RECOVER) |
| 2506 | session->state = ISCSI_STATE_IN_RECOVERY; |
Mike Christie | ed2abc7 | 2006-05-02 19:46:40 -0500 | [diff] [blame] | 2507 | |
| 2508 | old_stop_stage = conn->stop_stage; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2509 | conn->stop_stage = flag; |
Mike Christie | 67a6111 | 2006-05-30 00:37:20 -0500 | [diff] [blame] | 2510 | conn->c_stage = ISCSI_CONN_STOPPED; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2511 | spin_unlock_bh(&session->lock); |
Mike Christie | 6724add | 2007-08-15 01:38:30 -0500 | [diff] [blame] | 2512 | |
| 2513 | iscsi_suspend_tx(conn); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2514 | /* |
| 2515 | * for connection level recovery we should not calculate |
| 2516 | * header digest. conn->hdr_size used for optimization |
| 2517 | * in hdr_extract() and will be re-negotiated at |
| 2518 | * set_param() time. |
| 2519 | */ |
| 2520 | if (flag == STOP_CONN_RECOVER) { |
| 2521 | conn->hdrdgst_en = 0; |
| 2522 | conn->datadgst_en = 0; |
Mike Christie | 656cffc | 2006-05-18 20:31:42 -0500 | [diff] [blame] | 2523 | if (session->state == ISCSI_STATE_IN_RECOVERY && |
Mike Christie | 67a6111 | 2006-05-30 00:37:20 -0500 | [diff] [blame] | 2524 | old_stop_stage != STOP_CONN_RECOVER) { |
| 2525 | debug_scsi("blocking session\n"); |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 2526 | iscsi_block_session(session->cls_session); |
Mike Christie | 67a6111 | 2006-05-30 00:37:20 -0500 | [diff] [blame] | 2527 | } |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2528 | } |
Mike Christie | 656cffc | 2006-05-18 20:31:42 -0500 | [diff] [blame] | 2529 | |
Mike Christie | 656cffc | 2006-05-18 20:31:42 -0500 | [diff] [blame] | 2530 | /* |
| 2531 | * flush queues. |
| 2532 | */ |
| 2533 | spin_lock_bh(&session->lock); |
Mike Christie | 87cd9ea | 2008-09-24 11:46:14 -0500 | [diff] [blame] | 2534 | if (flag == STOP_CONN_RECOVER) |
Mike Christie | 56d7fcf | 2008-08-19 18:45:26 -0500 | [diff] [blame] | 2535 | fail_all_commands(conn, -1, DID_TRANSPORT_DISRUPTED); |
| 2536 | else |
| 2537 | fail_all_commands(conn, -1, DID_ERROR); |
Mike Christie | 656cffc | 2006-05-18 20:31:42 -0500 | [diff] [blame] | 2538 | flush_control_queues(session, conn); |
| 2539 | spin_unlock_bh(&session->lock); |
Mike Christie | 6724add | 2007-08-15 01:38:30 -0500 | [diff] [blame] | 2540 | mutex_unlock(&session->eh_mutex); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2541 | } |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2542 | |
| 2543 | void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag) |
| 2544 | { |
| 2545 | struct iscsi_conn *conn = cls_conn->dd_data; |
| 2546 | struct iscsi_session *session = conn->session; |
| 2547 | |
| 2548 | switch (flag) { |
| 2549 | case STOP_CONN_RECOVER: |
| 2550 | case STOP_CONN_TERM: |
| 2551 | iscsi_start_session_recovery(session, conn, flag); |
Mike Christie | 8d2860b | 2006-05-02 19:46:47 -0500 | [diff] [blame] | 2552 | break; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2553 | default: |
Mike Christie | 322d739 | 2008-01-31 13:36:52 -0600 | [diff] [blame] | 2554 | iscsi_conn_printk(KERN_ERR, conn, |
| 2555 | "invalid stop flag %d\n", flag); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2556 | } |
| 2557 | } |
| 2558 | EXPORT_SYMBOL_GPL(iscsi_conn_stop); |
| 2559 | |
| 2560 | int iscsi_conn_bind(struct iscsi_cls_session *cls_session, |
| 2561 | struct iscsi_cls_conn *cls_conn, int is_leading) |
| 2562 | { |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 2563 | struct iscsi_session *session = cls_session->dd_data; |
Mike Christie | 9864404 | 2006-10-16 18:09:39 -0400 | [diff] [blame] | 2564 | struct iscsi_conn *conn = cls_conn->dd_data; |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2565 | |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2566 | spin_lock_bh(&session->lock); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2567 | if (is_leading) |
| 2568 | session->leadconn = conn; |
Mike Christie | 9864404 | 2006-10-16 18:09:39 -0400 | [diff] [blame] | 2569 | spin_unlock_bh(&session->lock); |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2570 | |
| 2571 | /* |
| 2572 | * Unblock xmitworker(), Login Phase will pass through. |
| 2573 | */ |
| 2574 | clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx); |
| 2575 | clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx); |
| 2576 | return 0; |
| 2577 | } |
| 2578 | EXPORT_SYMBOL_GPL(iscsi_conn_bind); |
| 2579 | |
Mike Christie | a54a52c | 2006-06-28 12:00:23 -0500 | [diff] [blame] | 2580 | |
| 2581 | int iscsi_set_param(struct iscsi_cls_conn *cls_conn, |
| 2582 | enum iscsi_param param, char *buf, int buflen) |
| 2583 | { |
| 2584 | struct iscsi_conn *conn = cls_conn->dd_data; |
| 2585 | struct iscsi_session *session = conn->session; |
| 2586 | uint32_t value; |
| 2587 | |
| 2588 | switch(param) { |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 2589 | case ISCSI_PARAM_FAST_ABORT: |
| 2590 | sscanf(buf, "%d", &session->fast_abort); |
| 2591 | break; |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 2592 | case ISCSI_PARAM_ABORT_TMO: |
| 2593 | sscanf(buf, "%d", &session->abort_timeout); |
| 2594 | break; |
| 2595 | case ISCSI_PARAM_LU_RESET_TMO: |
| 2596 | sscanf(buf, "%d", &session->lu_reset_timeout); |
| 2597 | break; |
| 2598 | case ISCSI_PARAM_PING_TMO: |
| 2599 | sscanf(buf, "%d", &conn->ping_timeout); |
| 2600 | break; |
| 2601 | case ISCSI_PARAM_RECV_TMO: |
| 2602 | sscanf(buf, "%d", &conn->recv_timeout); |
| 2603 | break; |
Mike Christie | a54a52c | 2006-06-28 12:00:23 -0500 | [diff] [blame] | 2604 | case ISCSI_PARAM_MAX_RECV_DLENGTH: |
| 2605 | sscanf(buf, "%d", &conn->max_recv_dlength); |
| 2606 | break; |
| 2607 | case ISCSI_PARAM_MAX_XMIT_DLENGTH: |
| 2608 | sscanf(buf, "%d", &conn->max_xmit_dlength); |
| 2609 | break; |
| 2610 | case ISCSI_PARAM_HDRDGST_EN: |
| 2611 | sscanf(buf, "%d", &conn->hdrdgst_en); |
| 2612 | break; |
| 2613 | case ISCSI_PARAM_DATADGST_EN: |
| 2614 | sscanf(buf, "%d", &conn->datadgst_en); |
| 2615 | break; |
| 2616 | case ISCSI_PARAM_INITIAL_R2T_EN: |
| 2617 | sscanf(buf, "%d", &session->initial_r2t_en); |
| 2618 | break; |
| 2619 | case ISCSI_PARAM_MAX_R2T: |
| 2620 | sscanf(buf, "%d", &session->max_r2t); |
| 2621 | break; |
| 2622 | case ISCSI_PARAM_IMM_DATA_EN: |
| 2623 | sscanf(buf, "%d", &session->imm_data_en); |
| 2624 | break; |
| 2625 | case ISCSI_PARAM_FIRST_BURST: |
| 2626 | sscanf(buf, "%d", &session->first_burst); |
| 2627 | break; |
| 2628 | case ISCSI_PARAM_MAX_BURST: |
| 2629 | sscanf(buf, "%d", &session->max_burst); |
| 2630 | break; |
| 2631 | case ISCSI_PARAM_PDU_INORDER_EN: |
| 2632 | sscanf(buf, "%d", &session->pdu_inorder_en); |
| 2633 | break; |
| 2634 | case ISCSI_PARAM_DATASEQ_INORDER_EN: |
| 2635 | sscanf(buf, "%d", &session->dataseq_inorder_en); |
| 2636 | break; |
| 2637 | case ISCSI_PARAM_ERL: |
| 2638 | sscanf(buf, "%d", &session->erl); |
| 2639 | break; |
| 2640 | case ISCSI_PARAM_IFMARKER_EN: |
| 2641 | sscanf(buf, "%d", &value); |
| 2642 | BUG_ON(value); |
| 2643 | break; |
| 2644 | case ISCSI_PARAM_OFMARKER_EN: |
| 2645 | sscanf(buf, "%d", &value); |
| 2646 | BUG_ON(value); |
| 2647 | break; |
| 2648 | case ISCSI_PARAM_EXP_STATSN: |
| 2649 | sscanf(buf, "%u", &conn->exp_statsn); |
| 2650 | break; |
Mike Christie | b2c6416 | 2007-05-30 12:57:16 -0500 | [diff] [blame] | 2651 | case ISCSI_PARAM_USERNAME: |
| 2652 | kfree(session->username); |
| 2653 | session->username = kstrdup(buf, GFP_KERNEL); |
| 2654 | if (!session->username) |
| 2655 | return -ENOMEM; |
| 2656 | break; |
| 2657 | case ISCSI_PARAM_USERNAME_IN: |
| 2658 | kfree(session->username_in); |
| 2659 | session->username_in = kstrdup(buf, GFP_KERNEL); |
| 2660 | if (!session->username_in) |
| 2661 | return -ENOMEM; |
| 2662 | break; |
| 2663 | case ISCSI_PARAM_PASSWORD: |
| 2664 | kfree(session->password); |
| 2665 | session->password = kstrdup(buf, GFP_KERNEL); |
| 2666 | if (!session->password) |
| 2667 | return -ENOMEM; |
| 2668 | break; |
| 2669 | case ISCSI_PARAM_PASSWORD_IN: |
| 2670 | kfree(session->password_in); |
| 2671 | session->password_in = kstrdup(buf, GFP_KERNEL); |
| 2672 | if (!session->password_in) |
| 2673 | return -ENOMEM; |
| 2674 | break; |
Mike Christie | a54a52c | 2006-06-28 12:00:23 -0500 | [diff] [blame] | 2675 | case ISCSI_PARAM_TARGET_NAME: |
| 2676 | /* this should not change between logins */ |
| 2677 | if (session->targetname) |
| 2678 | break; |
| 2679 | |
| 2680 | session->targetname = kstrdup(buf, GFP_KERNEL); |
| 2681 | if (!session->targetname) |
| 2682 | return -ENOMEM; |
| 2683 | break; |
| 2684 | case ISCSI_PARAM_TPGT: |
| 2685 | sscanf(buf, "%d", &session->tpgt); |
| 2686 | break; |
| 2687 | case ISCSI_PARAM_PERSISTENT_PORT: |
| 2688 | sscanf(buf, "%d", &conn->persistent_port); |
| 2689 | break; |
| 2690 | case ISCSI_PARAM_PERSISTENT_ADDRESS: |
| 2691 | /* |
| 2692 | * this is the address returned in discovery so it should |
| 2693 | * not change between logins. |
| 2694 | */ |
| 2695 | if (conn->persistent_address) |
| 2696 | break; |
| 2697 | |
| 2698 | conn->persistent_address = kstrdup(buf, GFP_KERNEL); |
| 2699 | if (!conn->persistent_address) |
| 2700 | return -ENOMEM; |
| 2701 | break; |
Mike Christie | 88dfd34 | 2008-05-21 15:54:16 -0500 | [diff] [blame] | 2702 | case ISCSI_PARAM_IFACE_NAME: |
| 2703 | if (!session->ifacename) |
| 2704 | session->ifacename = kstrdup(buf, GFP_KERNEL); |
| 2705 | break; |
| 2706 | case ISCSI_PARAM_INITIATOR_NAME: |
| 2707 | if (!session->initiatorname) |
| 2708 | session->initiatorname = kstrdup(buf, GFP_KERNEL); |
| 2709 | break; |
Mike Christie | a54a52c | 2006-06-28 12:00:23 -0500 | [diff] [blame] | 2710 | default: |
| 2711 | return -ENOSYS; |
| 2712 | } |
| 2713 | |
| 2714 | return 0; |
| 2715 | } |
| 2716 | EXPORT_SYMBOL_GPL(iscsi_set_param); |
| 2717 | |
| 2718 | int iscsi_session_get_param(struct iscsi_cls_session *cls_session, |
| 2719 | enum iscsi_param param, char *buf) |
| 2720 | { |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 2721 | struct iscsi_session *session = cls_session->dd_data; |
Mike Christie | a54a52c | 2006-06-28 12:00:23 -0500 | [diff] [blame] | 2722 | int len; |
| 2723 | |
| 2724 | switch(param) { |
Mike Christie | 843c0a8 | 2007-12-13 12:43:20 -0600 | [diff] [blame] | 2725 | case ISCSI_PARAM_FAST_ABORT: |
| 2726 | len = sprintf(buf, "%d\n", session->fast_abort); |
| 2727 | break; |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 2728 | case ISCSI_PARAM_ABORT_TMO: |
| 2729 | len = sprintf(buf, "%d\n", session->abort_timeout); |
| 2730 | break; |
| 2731 | case ISCSI_PARAM_LU_RESET_TMO: |
| 2732 | len = sprintf(buf, "%d\n", session->lu_reset_timeout); |
| 2733 | break; |
Mike Christie | a54a52c | 2006-06-28 12:00:23 -0500 | [diff] [blame] | 2734 | case ISCSI_PARAM_INITIAL_R2T_EN: |
| 2735 | len = sprintf(buf, "%d\n", session->initial_r2t_en); |
| 2736 | break; |
| 2737 | case ISCSI_PARAM_MAX_R2T: |
| 2738 | len = sprintf(buf, "%hu\n", session->max_r2t); |
| 2739 | break; |
| 2740 | case ISCSI_PARAM_IMM_DATA_EN: |
| 2741 | len = sprintf(buf, "%d\n", session->imm_data_en); |
| 2742 | break; |
| 2743 | case ISCSI_PARAM_FIRST_BURST: |
| 2744 | len = sprintf(buf, "%u\n", session->first_burst); |
| 2745 | break; |
| 2746 | case ISCSI_PARAM_MAX_BURST: |
| 2747 | len = sprintf(buf, "%u\n", session->max_burst); |
| 2748 | break; |
| 2749 | case ISCSI_PARAM_PDU_INORDER_EN: |
| 2750 | len = sprintf(buf, "%d\n", session->pdu_inorder_en); |
| 2751 | break; |
| 2752 | case ISCSI_PARAM_DATASEQ_INORDER_EN: |
| 2753 | len = sprintf(buf, "%d\n", session->dataseq_inorder_en); |
| 2754 | break; |
| 2755 | case ISCSI_PARAM_ERL: |
| 2756 | len = sprintf(buf, "%d\n", session->erl); |
| 2757 | break; |
| 2758 | case ISCSI_PARAM_TARGET_NAME: |
| 2759 | len = sprintf(buf, "%s\n", session->targetname); |
| 2760 | break; |
| 2761 | case ISCSI_PARAM_TPGT: |
| 2762 | len = sprintf(buf, "%d\n", session->tpgt); |
| 2763 | break; |
Mike Christie | b2c6416 | 2007-05-30 12:57:16 -0500 | [diff] [blame] | 2764 | case ISCSI_PARAM_USERNAME: |
| 2765 | len = sprintf(buf, "%s\n", session->username); |
| 2766 | break; |
| 2767 | case ISCSI_PARAM_USERNAME_IN: |
| 2768 | len = sprintf(buf, "%s\n", session->username_in); |
| 2769 | break; |
| 2770 | case ISCSI_PARAM_PASSWORD: |
| 2771 | len = sprintf(buf, "%s\n", session->password); |
| 2772 | break; |
| 2773 | case ISCSI_PARAM_PASSWORD_IN: |
| 2774 | len = sprintf(buf, "%s\n", session->password_in); |
| 2775 | break; |
Mike Christie | 88dfd34 | 2008-05-21 15:54:16 -0500 | [diff] [blame] | 2776 | case ISCSI_PARAM_IFACE_NAME: |
| 2777 | len = sprintf(buf, "%s\n", session->ifacename); |
| 2778 | break; |
| 2779 | case ISCSI_PARAM_INITIATOR_NAME: |
| 2780 | if (!session->initiatorname) |
| 2781 | len = sprintf(buf, "%s\n", "unknown"); |
| 2782 | else |
| 2783 | len = sprintf(buf, "%s\n", session->initiatorname); |
| 2784 | break; |
Mike Christie | a54a52c | 2006-06-28 12:00:23 -0500 | [diff] [blame] | 2785 | default: |
| 2786 | return -ENOSYS; |
| 2787 | } |
| 2788 | |
| 2789 | return len; |
| 2790 | } |
| 2791 | EXPORT_SYMBOL_GPL(iscsi_session_get_param); |
| 2792 | |
| 2793 | int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn, |
| 2794 | enum iscsi_param param, char *buf) |
| 2795 | { |
| 2796 | struct iscsi_conn *conn = cls_conn->dd_data; |
| 2797 | int len; |
| 2798 | |
| 2799 | switch(param) { |
Mike Christie | f6d5180 | 2007-12-13 12:43:30 -0600 | [diff] [blame] | 2800 | case ISCSI_PARAM_PING_TMO: |
| 2801 | len = sprintf(buf, "%u\n", conn->ping_timeout); |
| 2802 | break; |
| 2803 | case ISCSI_PARAM_RECV_TMO: |
| 2804 | len = sprintf(buf, "%u\n", conn->recv_timeout); |
| 2805 | break; |
Mike Christie | a54a52c | 2006-06-28 12:00:23 -0500 | [diff] [blame] | 2806 | case ISCSI_PARAM_MAX_RECV_DLENGTH: |
| 2807 | len = sprintf(buf, "%u\n", conn->max_recv_dlength); |
| 2808 | break; |
| 2809 | case ISCSI_PARAM_MAX_XMIT_DLENGTH: |
| 2810 | len = sprintf(buf, "%u\n", conn->max_xmit_dlength); |
| 2811 | break; |
| 2812 | case ISCSI_PARAM_HDRDGST_EN: |
| 2813 | len = sprintf(buf, "%d\n", conn->hdrdgst_en); |
| 2814 | break; |
| 2815 | case ISCSI_PARAM_DATADGST_EN: |
| 2816 | len = sprintf(buf, "%d\n", conn->datadgst_en); |
| 2817 | break; |
| 2818 | case ISCSI_PARAM_IFMARKER_EN: |
| 2819 | len = sprintf(buf, "%d\n", conn->ifmarker_en); |
| 2820 | break; |
| 2821 | case ISCSI_PARAM_OFMARKER_EN: |
| 2822 | len = sprintf(buf, "%d\n", conn->ofmarker_en); |
| 2823 | break; |
| 2824 | case ISCSI_PARAM_EXP_STATSN: |
| 2825 | len = sprintf(buf, "%u\n", conn->exp_statsn); |
| 2826 | break; |
| 2827 | case ISCSI_PARAM_PERSISTENT_PORT: |
| 2828 | len = sprintf(buf, "%d\n", conn->persistent_port); |
| 2829 | break; |
| 2830 | case ISCSI_PARAM_PERSISTENT_ADDRESS: |
| 2831 | len = sprintf(buf, "%s\n", conn->persistent_address); |
| 2832 | break; |
| 2833 | default: |
| 2834 | return -ENOSYS; |
| 2835 | } |
| 2836 | |
| 2837 | return len; |
| 2838 | } |
| 2839 | EXPORT_SYMBOL_GPL(iscsi_conn_get_param); |
| 2840 | |
Mike Christie | 0801c24 | 2007-05-30 12:57:12 -0500 | [diff] [blame] | 2841 | int iscsi_host_get_param(struct Scsi_Host *shost, enum iscsi_host_param param, |
| 2842 | char *buf) |
| 2843 | { |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 2844 | struct iscsi_host *ihost = shost_priv(shost); |
Mike Christie | 0801c24 | 2007-05-30 12:57:12 -0500 | [diff] [blame] | 2845 | int len; |
| 2846 | |
| 2847 | switch (param) { |
Mike Christie | d8196ed | 2007-05-30 12:57:25 -0500 | [diff] [blame] | 2848 | case ISCSI_HOST_PARAM_NETDEV_NAME: |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 2849 | if (!ihost->netdev) |
Mike Christie | d8196ed | 2007-05-30 12:57:25 -0500 | [diff] [blame] | 2850 | len = sprintf(buf, "%s\n", "default"); |
| 2851 | else |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 2852 | len = sprintf(buf, "%s\n", ihost->netdev); |
Mike Christie | d8196ed | 2007-05-30 12:57:25 -0500 | [diff] [blame] | 2853 | break; |
Mike Christie | 0801c24 | 2007-05-30 12:57:12 -0500 | [diff] [blame] | 2854 | case ISCSI_HOST_PARAM_HWADDRESS: |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 2855 | if (!ihost->hwaddress) |
Mike Christie | 0801c24 | 2007-05-30 12:57:12 -0500 | [diff] [blame] | 2856 | len = sprintf(buf, "%s\n", "default"); |
| 2857 | else |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 2858 | len = sprintf(buf, "%s\n", ihost->hwaddress); |
Mike Christie | 0801c24 | 2007-05-30 12:57:12 -0500 | [diff] [blame] | 2859 | break; |
Mike Christie | 8ad5781 | 2007-05-30 12:57:13 -0500 | [diff] [blame] | 2860 | case ISCSI_HOST_PARAM_INITIATOR_NAME: |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 2861 | if (!ihost->initiatorname) |
Mike Christie | 8ad5781 | 2007-05-30 12:57:13 -0500 | [diff] [blame] | 2862 | len = sprintf(buf, "%s\n", "unknown"); |
| 2863 | else |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 2864 | len = sprintf(buf, "%s\n", ihost->initiatorname); |
Mike Christie | 8ad5781 | 2007-05-30 12:57:13 -0500 | [diff] [blame] | 2865 | break; |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 2866 | case ISCSI_HOST_PARAM_IPADDRESS: |
| 2867 | if (!strlen(ihost->local_address)) |
| 2868 | len = sprintf(buf, "%s\n", "unknown"); |
| 2869 | else |
| 2870 | len = sprintf(buf, "%s\n", |
| 2871 | ihost->local_address); |
Mike Christie | 88dfd34 | 2008-05-21 15:54:16 -0500 | [diff] [blame] | 2872 | break; |
Mike Christie | 0801c24 | 2007-05-30 12:57:12 -0500 | [diff] [blame] | 2873 | default: |
| 2874 | return -ENOSYS; |
| 2875 | } |
| 2876 | |
| 2877 | return len; |
| 2878 | } |
| 2879 | EXPORT_SYMBOL_GPL(iscsi_host_get_param); |
| 2880 | |
| 2881 | int iscsi_host_set_param(struct Scsi_Host *shost, enum iscsi_host_param param, |
| 2882 | char *buf, int buflen) |
| 2883 | { |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 2884 | struct iscsi_host *ihost = shost_priv(shost); |
Mike Christie | 0801c24 | 2007-05-30 12:57:12 -0500 | [diff] [blame] | 2885 | |
| 2886 | switch (param) { |
Mike Christie | d8196ed | 2007-05-30 12:57:25 -0500 | [diff] [blame] | 2887 | case ISCSI_HOST_PARAM_NETDEV_NAME: |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 2888 | if (!ihost->netdev) |
| 2889 | ihost->netdev = kstrdup(buf, GFP_KERNEL); |
Mike Christie | d8196ed | 2007-05-30 12:57:25 -0500 | [diff] [blame] | 2890 | break; |
Mike Christie | 0801c24 | 2007-05-30 12:57:12 -0500 | [diff] [blame] | 2891 | case ISCSI_HOST_PARAM_HWADDRESS: |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 2892 | if (!ihost->hwaddress) |
| 2893 | ihost->hwaddress = kstrdup(buf, GFP_KERNEL); |
Mike Christie | 0801c24 | 2007-05-30 12:57:12 -0500 | [diff] [blame] | 2894 | break; |
Mike Christie | 8ad5781 | 2007-05-30 12:57:13 -0500 | [diff] [blame] | 2895 | case ISCSI_HOST_PARAM_INITIATOR_NAME: |
Mike Christie | 7561352 | 2008-05-21 15:53:59 -0500 | [diff] [blame] | 2896 | if (!ihost->initiatorname) |
| 2897 | ihost->initiatorname = kstrdup(buf, GFP_KERNEL); |
Mike Christie | 8ad5781 | 2007-05-30 12:57:13 -0500 | [diff] [blame] | 2898 | break; |
Mike Christie | 0801c24 | 2007-05-30 12:57:12 -0500 | [diff] [blame] | 2899 | default: |
| 2900 | return -ENOSYS; |
| 2901 | } |
| 2902 | |
| 2903 | return 0; |
| 2904 | } |
| 2905 | EXPORT_SYMBOL_GPL(iscsi_host_set_param); |
| 2906 | |
Mike Christie | 7996a77 | 2006-04-06 21:13:41 -0500 | [diff] [blame] | 2907 | MODULE_AUTHOR("Mike Christie"); |
| 2908 | MODULE_DESCRIPTION("iSCSI library functions"); |
| 2909 | MODULE_LICENSE("GPL"); |