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