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