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