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