iscsi_rcv.c revision 1.1 1 1.1 agc /* $NetBSD: iscsi_rcv.c,v 1.1 2011/10/23 21:15:02 agc Exp $ */
2 1.1 agc
3 1.1 agc /*-
4 1.1 agc * Copyright (c) 2004,2005,2006,2011 The NetBSD Foundation, Inc.
5 1.1 agc * All rights reserved.
6 1.1 agc *
7 1.1 agc * This code is derived from software contributed to The NetBSD Foundation
8 1.1 agc * by Wasabi Systems, Inc.
9 1.1 agc *
10 1.1 agc * Redistribution and use in source and binary forms, with or without
11 1.1 agc * modification, are permitted provided that the following conditions
12 1.1 agc * are met:
13 1.1 agc * 1. Redistributions of source code must retain the above copyright
14 1.1 agc * notice, this list of conditions and the following disclaimer.
15 1.1 agc * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 agc * notice, this list of conditions and the following disclaimer in the
17 1.1 agc * documentation and/or other materials provided with the distribution.
18 1.1 agc *
19 1.1 agc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 agc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 agc * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 agc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 agc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 agc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 agc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 agc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 agc * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 agc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 agc * POSSIBILITY OF SUCH DAMAGE.
30 1.1 agc */
31 1.1 agc #include "iscsi_globals.h"
32 1.1 agc
33 1.1 agc #include <sys/file.h>
34 1.1 agc #include <sys/socket.h>
35 1.1 agc #include <sys/socketvar.h>
36 1.1 agc
37 1.1 agc /*****************************************************************************/
38 1.1 agc
39 1.1 agc /*
40 1.1 agc * my_soo_read:
41 1.1 agc * Replacement for soo_read with flag handling.
42 1.1 agc *
43 1.1 agc * Parameter:
44 1.1 agc * conn The connection
45 1.1 agc * u The uio descriptor
46 1.1 agc * flags Read flags
47 1.1 agc *
48 1.1 agc * Returns: 0 on success, else 1
49 1.1 agc */
50 1.1 agc
51 1.1 agc STATIC int
52 1.1 agc my_soo_read(connection_t *conn, struct uio *u, int flags)
53 1.1 agc {
54 1.1 agc struct socket *so = (struct socket *) conn->sock->f_data;
55 1.1 agc int ret;
56 1.1 agc #ifdef ISCSI_DEBUG
57 1.1 agc size_t resid = u->uio_resid;
58 1.1 agc #endif
59 1.1 agc
60 1.1 agc DEBC(conn, 99, ("soo_read req: %zu\n", resid));
61 1.1 agc
62 1.1 agc ret = soreceive(so, NULL, u, NULL, NULL, &flags);
63 1.1 agc
64 1.1 agc if (ret || (flags != MSG_DONTWAIT && u->uio_resid)) {
65 1.1 agc DEBC(conn, 1, ("Read failed (ret: %d, req: %zu, out: %zu)\n", ret, resid,
66 1.1 agc u->uio_resid));
67 1.1 agc handle_connection_error(conn, ISCSI_STATUS_SOCKET_ERROR,
68 1.1 agc RECOVER_CONNECTION);
69 1.1 agc return 1;
70 1.1 agc }
71 1.1 agc return 0;
72 1.1 agc }
73 1.1 agc
74 1.1 agc
75 1.1 agc /*
76 1.1 agc * try_resynch_receive:
77 1.1 agc * Skip over everything in the socket's receive buffer, in the hope of
78 1.1 agc * ending up at the start of a new PDU.
79 1.1 agc *
80 1.1 agc * Parameter:
81 1.1 agc * conn The connection
82 1.1 agc */
83 1.1 agc
84 1.1 agc STATIC void
85 1.1 agc try_resynch_receive(connection_t *conn)
86 1.1 agc {
87 1.1 agc uint8_t buffer[64];
88 1.1 agc struct uio uio;
89 1.1 agc struct iovec io_vec;
90 1.1 agc int rc;
91 1.1 agc
92 1.1 agc uio.uio_rw = UIO_READ;
93 1.1 agc #if (__NetBSD_Version__ >= 399000900)
94 1.1 agc UIO_SETUP_SYSSPACE(&uio);
95 1.1 agc #else
96 1.1 agc uio.uio_segflg = UIO_SYSSPACE;
97 1.1 agc #endif
98 1.1 agc
99 1.1 agc do {
100 1.1 agc io_vec.iov_base = buffer;
101 1.1 agc uio.uio_iov = &io_vec;
102 1.1 agc uio.uio_iovcnt = 1;
103 1.1 agc uio.uio_resid = io_vec.iov_len = sizeof(buffer);
104 1.1 agc
105 1.1 agc rc = my_soo_read(conn, &uio, MSG_DONTWAIT);
106 1.1 agc DEBC(conn, 9, ("try_resynch_receive: rc = %d, resid = %zu\n",
107 1.1 agc rc, uio.uio_resid));
108 1.1 agc } while (!rc && !uio.uio_resid);
109 1.1 agc }
110 1.1 agc
111 1.1 agc
112 1.1 agc /*
113 1.1 agc * ccb_from_itt
114 1.1 agc * Translate ITT into CCB pointer.
115 1.1 agc *
116 1.1 agc * Parameter:
117 1.1 agc * conn The connection
118 1.1 agc * itt The Initiator Task Tag
119 1.1 agc *
120 1.1 agc * Returns:
121 1.1 agc * Pointer to CCB, or NULL if ITT is not a valid CCB index.
122 1.1 agc */
123 1.1 agc
124 1.1 agc STATIC ccb_t *
125 1.1 agc ccb_from_itt(connection_t *conn, uint32_t itt)
126 1.1 agc {
127 1.1 agc ccb_t *ccb;
128 1.1 agc int cidx;
129 1.1 agc
130 1.1 agc cidx = itt & 0xff;
131 1.1 agc if (cidx >= CCBS_PER_SESSION) {
132 1.1 agc return NULL;
133 1.1 agc }
134 1.1 agc ccb = &conn->session->ccb[cidx];
135 1.1 agc if (ccb->ITT != itt || ccb->disp <= CCBDISP_BUSY) {
136 1.1 agc return NULL;
137 1.1 agc }
138 1.1 agc return ccb;
139 1.1 agc }
140 1.1 agc
141 1.1 agc
142 1.1 agc /*
143 1.1 agc * read_pdu_data:
144 1.1 agc * Initialize the uio structure for receiving everything after the
145 1.1 agc * header, including data (if present), and padding. Read the data.
146 1.1 agc *
147 1.1 agc * Parameter:
148 1.1 agc * pdu The PDU
149 1.1 agc * data Pointer to data (may be NULL for auto-allocation)
150 1.1 agc * offset The offset into the data pointer
151 1.1 agc *
152 1.1 agc * Returns: 0 on success
153 1.1 agc * 1 if an error occurs during read
154 1.1 agc * -1 if the data digest was incorrect (PDU must be ignored)
155 1.1 agc */
156 1.1 agc
157 1.1 agc STATIC int
158 1.1 agc read_pdu_data(pdu_t *pdu, uint8_t *data, uint32_t offset)
159 1.1 agc {
160 1.1 agc static uint8_t pad_bytes[4];
161 1.1 agc uint32_t len, digest;
162 1.1 agc struct uio *uio;
163 1.1 agc int i, pad;
164 1.1 agc connection_t *conn = pdu->connection;
165 1.1 agc
166 1.1 agc if (!(len = ntoh3(pdu->pdu.DataSegmentLength))) {
167 1.1 agc return 0;
168 1.1 agc }
169 1.1 agc pad = len & 0x03;
170 1.1 agc if (pad) {
171 1.1 agc pad = 4 - pad;
172 1.1 agc }
173 1.1 agc assert((data != NULL) || (offset == 0));
174 1.1 agc
175 1.1 agc if (data == NULL) {
176 1.1 agc /*
177 1.1 agc * NOTE: Always allocate 2 extra bytes when reading temp data,
178 1.1 agc * since temp data is mostly used for received text, and we can
179 1.1 agc * make sure there's a double zero at the end of the data to mark EOF.
180 1.1 agc */
181 1.1 agc if ((data = (uint8_t *) malloc(len + 2, M_TEMP, M_WAITOK)) == NULL) {
182 1.1 agc DEBOUT(("ran out of mem on receive\n"));
183 1.1 agc handle_connection_error(pdu->connection,
184 1.1 agc ISCSI_STATUS_NO_RESOURCES, LOGOUT_SESSION);
185 1.1 agc return 1;
186 1.1 agc }
187 1.1 agc pdu->temp_data = data;
188 1.1 agc pdu->temp_data_len = len;
189 1.1 agc }
190 1.1 agc
191 1.1 agc pdu->io_vec[0].iov_base = data + offset;
192 1.1 agc pdu->io_vec[0].iov_len = len;
193 1.1 agc
194 1.1 agc uio = &pdu->uio;
195 1.1 agc
196 1.1 agc uio->uio_iov = pdu->io_vec;
197 1.1 agc uio->uio_iovcnt = 1;
198 1.1 agc uio->uio_rw = UIO_READ;
199 1.1 agc uio->uio_resid = len;
200 1.1 agc #if (__NetBSD_Version__ >= 399000900)
201 1.1 agc UIO_SETUP_SYSSPACE(uio);
202 1.1 agc #else
203 1.1 agc uio->uio_segflg = UIO_SYSSPACE;
204 1.1 agc #endif
205 1.1 agc
206 1.1 agc if (pad) {
207 1.1 agc uio->uio_iovcnt++;
208 1.1 agc uio->uio_iov[1].iov_base = pad_bytes;
209 1.1 agc uio->uio_iov[1].iov_len = pad;
210 1.1 agc uio->uio_resid += pad;
211 1.1 agc }
212 1.1 agc
213 1.1 agc if (conn->DataDigest) {
214 1.1 agc i = uio->uio_iovcnt++;
215 1.1 agc pdu->io_vec[i].iov_base = &pdu->data_digest;
216 1.1 agc pdu->io_vec[i].iov_len = 4;
217 1.1 agc uio->uio_resid += 4;
218 1.1 agc }
219 1.1 agc #ifdef ISCSI_TEST_MODE
220 1.1 agc /* save data pointer and size */
221 1.1 agc pdu->save_uio.uio_iov = (struct iovec *) data;
222 1.1 agc pdu->save_uio.uio_resid = len;
223 1.1 agc #endif
224 1.1 agc
225 1.1 agc /* get the data */
226 1.1 agc if (my_soo_read(conn, &pdu->uio, MSG_WAITALL) != 0) {
227 1.1 agc return 1;
228 1.1 agc }
229 1.1 agc if (conn->DataDigest) {
230 1.1 agc digest = gen_digest_2(data, len, pad_bytes, pad);
231 1.1 agc
232 1.1 agc if (digest != pdu->data_digest) {
233 1.1 agc DEBOUT(("Data Digest Error: comp = %08x, rx = %08x\n",
234 1.1 agc digest, pdu->data_digest));
235 1.1 agc switch (pdu->pdu.Opcode & OPCODE_MASK) {
236 1.1 agc case TOP_SCSI_Response:
237 1.1 agc case TOP_Text_Response:
238 1.1 agc send_snack(pdu->connection, pdu, NULL, SNACK_STATUS_NAK);
239 1.1 agc break;
240 1.1 agc
241 1.1 agc case TOP_SCSI_Data_in:
242 1.1 agc send_snack(pdu->connection, pdu, NULL, SNACK_DATA_NAK);
243 1.1 agc break;
244 1.1 agc
245 1.1 agc default:
246 1.1 agc /* ignore all others */
247 1.1 agc break;
248 1.1 agc }
249 1.1 agc return -1;
250 1.1 agc }
251 1.1 agc }
252 1.1 agc return 0;
253 1.1 agc }
254 1.1 agc
255 1.1 agc
256 1.1 agc /*
257 1.1 agc * collect_text_data
258 1.1 agc * Handle text continuation in login and text response PDUs
259 1.1 agc *
260 1.1 agc * Parameter:
261 1.1 agc * pdu The received PDU
262 1.1 agc * req_CCB The CCB associated with the original request
263 1.1 agc *
264 1.1 agc * Returns: -1 if continue flag is set
265 1.1 agc * 0 if text is complete
266 1.1 agc * +1 if an error occurred (out of resources)
267 1.1 agc */
268 1.1 agc STATIC int
269 1.1 agc collect_text_data(pdu_t *pdu, ccb_t *req_ccb)
270 1.1 agc {
271 1.1 agc
272 1.1 agc if (req_ccb->text_data) {
273 1.1 agc int nlen;
274 1.1 agc uint8_t *newp;
275 1.1 agc
276 1.1 agc nlen = req_ccb->text_len + pdu->temp_data_len;
277 1.1 agc /* Note: allocate extra 2 bytes for text terminator */
278 1.1 agc if ((newp = malloc(nlen + 2, M_TEMP, M_WAITOK)) == NULL) {
279 1.1 agc req_ccb->status = ISCSI_STATUS_NO_RESOURCES;
280 1.1 agc return 1;
281 1.1 agc }
282 1.1 agc memcpy(newp, req_ccb->text_data, req_ccb->text_len);
283 1.1 agc memcpy(&newp[req_ccb->text_len], pdu->temp_data, pdu->temp_data_len);
284 1.1 agc
285 1.1 agc free(req_ccb->text_data, M_TEMP);
286 1.1 agc free(pdu->temp_data, M_TEMP);
287 1.1 agc
288 1.1 agc req_ccb->text_data = NULL;
289 1.1 agc pdu->temp_data = newp;
290 1.1 agc pdu->temp_data_len = nlen;
291 1.1 agc }
292 1.1 agc
293 1.1 agc if (pdu->pdu.Flags & FLAG_CONTINUE) {
294 1.1 agc req_ccb->text_data = pdu->temp_data;
295 1.1 agc req_ccb->text_len = pdu->temp_data_len;
296 1.1 agc pdu->temp_data = NULL;
297 1.1 agc
298 1.1 agc acknowledge_text(req_ccb->connection, pdu, req_ccb);
299 1.1 agc return -1;
300 1.1 agc }
301 1.1 agc return 0;
302 1.1 agc }
303 1.1 agc
304 1.1 agc
305 1.1 agc /*
306 1.1 agc * check_StatSN
307 1.1 agc * Check received vs. expected StatSN
308 1.1 agc *
309 1.1 agc * Parameter:
310 1.1 agc * conn The connection
311 1.1 agc * nw_sn The received StatSN in network byte order
312 1.1 agc * ack Acknowledge this SN if TRUE
313 1.1 agc */
314 1.1 agc
315 1.1 agc STATIC int
316 1.1 agc check_StatSN(connection_t *conn, uint32_t nw_sn, bool ack)
317 1.1 agc {
318 1.1 agc int rc;
319 1.1 agc uint32_t sn = ntohl(nw_sn);
320 1.1 agc
321 1.1 agc rc = add_sernum(&conn->StatSN_buf, sn);
322 1.1 agc
323 1.1 agc if (ack)
324 1.1 agc ack_sernum(&conn->StatSN_buf, sn);
325 1.1 agc
326 1.1 agc if (rc != 1) {
327 1.1 agc if (!rc)
328 1.1 agc return -1;
329 1.1 agc
330 1.1 agc if (rc < 0) {
331 1.1 agc DEBOUT(("Excessive outstanding Status PDUs, ExpSN %d, Recvd: %d\n",
332 1.1 agc conn->StatSN_buf.ExpSN, sn));
333 1.1 agc handle_connection_error(conn, ISCSI_STATUS_PDUS_LOST,
334 1.1 agc RECOVER_CONNECTION);
335 1.1 agc return rc;
336 1.1 agc }
337 1.1 agc
338 1.1 agc DEBOUT(("Missing Status PDUs: First %d, num: %d\n",
339 1.1 agc conn->StatSN_buf.ExpSN, rc - 1));
340 1.1 agc if (conn->state == ST_FULL_FEATURE &&
341 1.1 agc conn->session->ErrorRecoveryLevel) {
342 1.1 agc snack_missing(conn, NULL, SNACK_STATUS_NAK,
343 1.1 agc conn->StatSN_buf.ExpSN, rc - 1);
344 1.1 agc } else {
345 1.1 agc DEBOUT(("StatSN killing connection (State = %d, "
346 1.1 agc "ErrorRecoveryLevel = %d)\n",
347 1.1 agc conn->state, conn->session->ErrorRecoveryLevel));
348 1.1 agc handle_connection_error(conn, ISCSI_STATUS_PDUS_LOST,
349 1.1 agc RECOVER_CONNECTION);
350 1.1 agc return -1;
351 1.1 agc }
352 1.1 agc }
353 1.1 agc return 0;
354 1.1 agc }
355 1.1 agc
356 1.1 agc
357 1.1 agc /*
358 1.1 agc * check_CmdSN
359 1.1 agc * Check received vs. expected CmdSN
360 1.1 agc *
361 1.1 agc * Parameter:
362 1.1 agc * conn The connection
363 1.1 agc * nw_sn The received ExpCmdSN in network byte order
364 1.1 agc */
365 1.1 agc
366 1.1 agc STATIC void
367 1.1 agc check_CmdSN(connection_t *conn, uint32_t nw_sn)
368 1.1 agc {
369 1.1 agc uint32_t sn = ntohl(nw_sn);
370 1.1 agc ccb_t *ccb, *nxt;
371 1.1 agc
372 1.1 agc for (ccb = TAILQ_FIRST(&conn->ccbs_waiting); ccb != NULL; ccb = nxt) {
373 1.1 agc nxt = TAILQ_NEXT(ccb, chain);
374 1.1 agc DEBC(conn, 10,
375 1.1 agc ("CheckCmdSN - CmdSN=%d, ExpCmdSn=%d, waiting=%p, flags=%x\n",
376 1.1 agc ccb->CmdSN, sn, ccb->pdu_waiting, ccb->flags));
377 1.1 agc if (ccb->pdu_waiting != NULL && ccb->CmdSN > sn &&
378 1.1 agc !(ccb->flags & CCBF_GOT_RSP)) {
379 1.1 agc DEBC(conn, 1, ("CheckCmdSN resending - CmdSN=%d, ExpCmdSn=%d\n",
380 1.1 agc ccb->CmdSN, sn));
381 1.1 agc
382 1.1 agc ccb->total_tries++;
383 1.1 agc
384 1.1 agc if (++ccb->num_timeouts > MAX_CCB_TIMEOUTS ||
385 1.1 agc ccb->total_tries > MAX_CCB_TRIES) {
386 1.1 agc handle_connection_error(conn, ISCSI_STATUS_TIMEOUT,
387 1.1 agc (ccb->total_tries <= MAX_CCB_TRIES) ? RECOVER_CONNECTION
388 1.1 agc : LOGOUT_CONNECTION);
389 1.1 agc break;
390 1.1 agc } else {
391 1.1 agc resend_pdu(ccb);
392 1.1 agc }
393 1.1 agc }
394 1.1 agc }
395 1.1 agc }
396 1.1 agc
397 1.1 agc
398 1.1 agc /*
399 1.1 agc * receive_login_pdu
400 1.1 agc * Handle receipt of a login response PDU.
401 1.1 agc *
402 1.1 agc * Parameter:
403 1.1 agc * conn The connection
404 1.1 agc * pdu The PDU
405 1.1 agc * req_CCB The CCB associated with the original request (if any)
406 1.1 agc */
407 1.1 agc
408 1.1 agc STATIC int
409 1.1 agc receive_login_pdu(connection_t *conn, pdu_t *pdu, ccb_t *req_ccb)
410 1.1 agc {
411 1.1 agc int rc;
412 1.1 agc
413 1.1 agc DEBC(conn, 9, ("Received Login Response PDU, op=%x, flags=%x\n",
414 1.1 agc pdu->pdu.Opcode, pdu->pdu.Flags));
415 1.1 agc
416 1.1 agc if (req_ccb == NULL) {
417 1.1 agc /* Duplicate?? */
418 1.1 agc DEBOUT(("Received duplicate login response (no associated CCB)\n"));
419 1.1 agc return -1;
420 1.1 agc }
421 1.1 agc
422 1.1 agc if (!conn->StatSN_buf.next_sn)
423 1.1 agc conn->StatSN_buf.next_sn = conn->StatSN_buf.ExpSN =
424 1.1 agc ntohl(pdu->pdu.p.login_rsp.StatSN) + 1;
425 1.1 agc else if (check_StatSN(conn, pdu->pdu.p.login_rsp.StatSN, TRUE))
426 1.1 agc return -1;
427 1.1 agc
428 1.1 agc if (pdu->temp_data_len) {
429 1.1 agc if ((rc = collect_text_data(pdu, req_ccb)) != 0) {
430 1.1 agc return max(rc, 0);
431 1.1 agc }
432 1.1 agc }
433 1.1 agc
434 1.1 agc if (pdu->pdu.p.login_rsp.StatusClass) {
435 1.1 agc DEBC(conn, 1, ("Login problem - Class = %x, Detail = %x\n",
436 1.1 agc pdu->pdu.p.login_rsp.StatusClass,
437 1.1 agc pdu->pdu.p.login_rsp.StatusDetail));
438 1.1 agc
439 1.1 agc req_ccb->status = ISCSI_STATUS_LOGIN_FAILED;
440 1.1 agc } else {
441 1.1 agc negotiate_login(conn, pdu, req_ccb);
442 1.1 agc /* negotiate_login will decide whether login is complete or not */
443 1.1 agc return 0;
444 1.1 agc }
445 1.1 agc
446 1.1 agc wake_ccb(req_ccb);
447 1.1 agc return 0;
448 1.1 agc }
449 1.1 agc
450 1.1 agc
451 1.1 agc /*
452 1.1 agc * receive_text_response_pdu
453 1.1 agc * Handle receipt of a text response PDU.
454 1.1 agc *
455 1.1 agc * Parameter:
456 1.1 agc * conn The connection
457 1.1 agc * pdu The PDU
458 1.1 agc * req_CCB The CCB associated with the original request (if any)
459 1.1 agc */
460 1.1 agc
461 1.1 agc STATIC int
462 1.1 agc receive_text_response_pdu(connection_t *conn, pdu_t *pdu, ccb_t *req_ccb)
463 1.1 agc {
464 1.1 agc int rc;
465 1.1 agc
466 1.1 agc DEBC(conn, 9, ("Received Text Response PDU, op=%x, flags=%x\n",
467 1.1 agc pdu->pdu.Opcode, pdu->pdu.Flags));
468 1.1 agc
469 1.1 agc if (check_StatSN(conn, pdu->pdu.p.text_rsp.StatSN, TRUE)) {
470 1.1 agc return -1;
471 1.1 agc }
472 1.1 agc if (req_ccb == NULL) {
473 1.1 agc DEBOUT(("Received unsolicited text response\n"));
474 1.1 agc handle_connection_error(conn, ISCSI_STATUS_TARGET_ERROR,
475 1.1 agc LOGOUT_CONNECTION);
476 1.1 agc return -1;
477 1.1 agc }
478 1.1 agc
479 1.1 agc if (req_ccb->pdu_waiting != NULL) {
480 1.1 agc SET_CCB_TIMEOUT(conn, req_ccb, COMMAND_TIMEOUT);
481 1.1 agc req_ccb->num_timeouts = 0;
482 1.1 agc }
483 1.1 agc
484 1.1 agc if ((rc = collect_text_data(pdu, req_ccb)) != 0) {
485 1.1 agc return max(0, rc);
486 1.1 agc }
487 1.1 agc negotiate_text(conn, pdu, req_ccb);
488 1.1 agc
489 1.1 agc return 0;
490 1.1 agc }
491 1.1 agc
492 1.1 agc
493 1.1 agc /*
494 1.1 agc * receive_logout_pdu
495 1.1 agc * Handle receipt of a logout response PDU.
496 1.1 agc *
497 1.1 agc * Parameter:
498 1.1 agc * conn The connection
499 1.1 agc * pdu The PDU
500 1.1 agc * req_CCB The CCB associated with the original request (if any)
501 1.1 agc */
502 1.1 agc
503 1.1 agc STATIC int
504 1.1 agc receive_logout_pdu(connection_t *conn, pdu_t *pdu, ccb_t *req_ccb)
505 1.1 agc {
506 1.1 agc bool otherconn;
507 1.1 agc uint8_t response;
508 1.1 agc
509 1.1 agc otherconn = (req_ccb != NULL) ? (req_ccb->flags & CCBF_OTHERCONN) != 0 : 1;
510 1.1 agc response = pdu->pdu.OpcodeSpecific [0];
511 1.1 agc DEBC(conn, 1,
512 1.1 agc ("Received Logout PDU - CCB = %p, otherconn=%d, response=%d\n",
513 1.1 agc req_ccb, otherconn, response));
514 1.1 agc
515 1.1 agc if (req_ccb == NULL)
516 1.1 agc return 0;
517 1.1 agc
518 1.1 agc if (otherconn && check_StatSN(conn, pdu->pdu.p.logout_rsp.StatSN, TRUE))
519 1.1 agc return -1;
520 1.1 agc
521 1.1 agc switch (response) {
522 1.1 agc case 0:
523 1.1 agc req_ccb->status = ISCSI_STATUS_SUCCESS;
524 1.1 agc break;
525 1.1 agc case 1:
526 1.1 agc req_ccb->status = ISCSI_STATUS_LOGOUT_CID_NOT_FOUND;
527 1.1 agc break;
528 1.1 agc case 2:
529 1.1 agc req_ccb->status = ISCSI_STATUS_LOGOUT_RECOVERY_NS;
530 1.1 agc break;
531 1.1 agc default:
532 1.1 agc req_ccb->status = ISCSI_STATUS_LOGOUT_ERROR;
533 1.1 agc break;
534 1.1 agc }
535 1.1 agc
536 1.1 agc if (conn->session->ErrorRecoveryLevel >= 2 && response != 1) {
537 1.1 agc connection_t *refconn = (otherconn) ? req_ccb->par : conn;
538 1.1 agc
539 1.1 agc refconn->Time2Wait = ntohs(pdu->pdu.p.logout_rsp.Time2Wait);
540 1.1 agc refconn->Time2Retain = ntohs(pdu->pdu.p.logout_rsp.Time2Retain);
541 1.1 agc }
542 1.1 agc
543 1.1 agc wake_ccb(req_ccb);
544 1.1 agc
545 1.1 agc if (!otherconn && conn->state == ST_LOGOUT_SENT) {
546 1.1 agc conn->terminating = ISCSI_STATUS_LOGOUT;
547 1.1 agc conn->state = ST_SETTLING;
548 1.1 agc conn->loggedout = (response) ? LOGOUT_FAILED : LOGOUT_SUCCESS;
549 1.1 agc
550 1.1 agc callout_stop(&conn->timeout);
551 1.1 agc
552 1.1 agc #ifdef ISCSI_TEST_MODE
553 1.1 agc test_remove_connection(conn);
554 1.1 agc #endif
555 1.1 agc /* let send thread take over next step of cleanup */
556 1.1 agc wakeup(&conn->pdus_to_send);
557 1.1 agc }
558 1.1 agc
559 1.1 agc return !otherconn;
560 1.1 agc }
561 1.1 agc
562 1.1 agc
563 1.1 agc /*
564 1.1 agc * receive_data_in_pdu
565 1.1 agc * Handle receipt of a data in PDU.
566 1.1 agc *
567 1.1 agc * Parameter:
568 1.1 agc * conn The connection
569 1.1 agc * pdu The PDU
570 1.1 agc * req_CCB The CCB associated with the original request (if any)
571 1.1 agc */
572 1.1 agc
573 1.1 agc STATIC int
574 1.1 agc receive_data_in_pdu(connection_t *conn, pdu_t *pdu, ccb_t *req_ccb)
575 1.1 agc {
576 1.1 agc uint32_t dsl, sn;
577 1.1 agc bool done;
578 1.1 agc int rc;
579 1.1 agc
580 1.1 agc dsl = ntoh3(pdu->pdu.DataSegmentLength);
581 1.1 agc
582 1.1 agc if (req_ccb == NULL || !req_ccb->data_in || !req_ccb->data_len) {
583 1.1 agc DEBOUT(("Received Data In, but req_ccb not waiting for it, ignored\n"));
584 1.1 agc return 0;
585 1.1 agc }
586 1.1 agc req_ccb->flags |= CCBF_GOT_RSP;
587 1.1 agc
588 1.1 agc if (req_ccb->pdu_waiting != NULL) {
589 1.1 agc SET_CCB_TIMEOUT(conn, req_ccb, COMMAND_TIMEOUT);
590 1.1 agc req_ccb->num_timeouts = 0;
591 1.1 agc }
592 1.1 agc
593 1.1 agc sn = ntohl(pdu->pdu.p.data_in.DataSN);
594 1.1 agc
595 1.1 agc if ((rc = add_sernum(&req_ccb->DataSN_buf, sn)) != 1) {
596 1.1 agc if (!rc) {
597 1.1 agc return -1;
598 1.1 agc }
599 1.1 agc if (rc < 0) {
600 1.1 agc DEBOUT(("Excessive outstanding Data PDUs\n"));
601 1.1 agc handle_connection_error(req_ccb->connection,
602 1.1 agc ISCSI_STATUS_PDUS_LOST, LOGOUT_CONNECTION);
603 1.1 agc return -1;
604 1.1 agc }
605 1.1 agc DEBOUT(("Missing Data PDUs: First %d, num: %d\n",
606 1.1 agc req_ccb->DataSN_buf.ExpSN, rc - 1));
607 1.1 agc
608 1.1 agc if (conn->state == ST_FULL_FEATURE &&
609 1.1 agc conn->session->ErrorRecoveryLevel) {
610 1.1 agc snack_missing(req_ccb->connection, req_ccb,
611 1.1 agc SNACK_DATA_NAK, req_ccb->DataSN_buf.ExpSN,
612 1.1 agc rc - 1);
613 1.1 agc } else {
614 1.1 agc DEBOUT(("Killing connection (State=%d, ErrorRecoveryLevel=%d)\n",
615 1.1 agc conn->state, conn->session->ErrorRecoveryLevel));
616 1.1 agc handle_connection_error(conn, ISCSI_STATUS_PDUS_LOST,
617 1.1 agc LOGOUT_CONNECTION);
618 1.1 agc return -1;
619 1.1 agc }
620 1.1 agc }
621 1.1 agc
622 1.1 agc ack_sernum(&req_ccb->DataSN_buf, sn);
623 1.1 agc
624 1.1 agc req_ccb->xfer_len += dsl;
625 1.1 agc
626 1.1 agc if ((pdu->pdu.Flags & FLAG_ACK) && conn->session->ErrorRecoveryLevel)
627 1.1 agc send_snack(conn, pdu, req_ccb, SNACK_DATA_ACK);
628 1.1 agc
629 1.1 agc done = sn_empty(&req_ccb->DataSN_buf);
630 1.1 agc
631 1.1 agc if (pdu->pdu.Flags & FLAG_STATUS) {
632 1.1 agc DEBC(conn, 10, ("Rx Data In Complete, done = %d\n", done));
633 1.1 agc
634 1.1 agc req_ccb->flags |= CCBF_COMPLETE;
635 1.1 agc /* successful transfer, reset recover count */
636 1.1 agc conn->recover = 0;
637 1.1 agc
638 1.1 agc if (done) {
639 1.1 agc wake_ccb(req_ccb);
640 1.1 agc }
641 1.1 agc if (check_StatSN(conn, pdu->pdu.p.data_in.StatSN, done)) {
642 1.1 agc return -1;
643 1.1 agc }
644 1.1 agc } else if (done && (req_ccb->flags & CCBF_COMPLETE)) {
645 1.1 agc wake_ccb(req_ccb);
646 1.1 agc }
647 1.1 agc /* else wait for command response */
648 1.1 agc
649 1.1 agc return 0;
650 1.1 agc }
651 1.1 agc
652 1.1 agc
653 1.1 agc /*
654 1.1 agc * receive_r2t_pdu
655 1.1 agc * Handle receipt of a R2T PDU.
656 1.1 agc *
657 1.1 agc * Parameter:
658 1.1 agc * conn The connection
659 1.1 agc * pdu The PDU
660 1.1 agc * req_CCB The CCB associated with the original request (if any)
661 1.1 agc */
662 1.1 agc
663 1.1 agc STATIC int
664 1.1 agc receive_r2t_pdu(connection_t *conn, pdu_t *pdu, ccb_t *req_ccb)
665 1.1 agc {
666 1.1 agc
667 1.1 agc DEBC(conn, 10, ("Received R2T PDU - CCB = %p\n", req_ccb));
668 1.1 agc
669 1.1 agc if (req_ccb != NULL) {
670 1.1 agc if (req_ccb->pdu_waiting != NULL) {
671 1.1 agc SET_CCB_TIMEOUT(conn, req_ccb, COMMAND_TIMEOUT);
672 1.1 agc req_ccb->num_timeouts = 0;
673 1.1 agc }
674 1.1 agc send_data_out(conn, pdu, req_ccb, CCBDISP_NOWAIT, TRUE);
675 1.1 agc }
676 1.1 agc
677 1.1 agc return 0;
678 1.1 agc }
679 1.1 agc
680 1.1 agc
681 1.1 agc /*
682 1.1 agc * receive_command_response_pdu
683 1.1 agc * Handle receipt of a command response PDU.
684 1.1 agc *
685 1.1 agc * Parameter:
686 1.1 agc * conn The connection
687 1.1 agc * pdu The PDU
688 1.1 agc * req_CCB The CCB associated with the original request (if any)
689 1.1 agc */
690 1.1 agc
691 1.1 agc STATIC int
692 1.1 agc receive_command_response_pdu(connection_t *conn, pdu_t *pdu, ccb_t *req_ccb)
693 1.1 agc {
694 1.1 agc int len, rc;
695 1.1 agc bool done;
696 1.1 agc
697 1.1 agc /* Read any provided data */
698 1.1 agc if (pdu->temp_data_len && req_ccb != NULL && req_ccb->sense_len_req) {
699 1.1 agc len = min(req_ccb->sense_len_req,
700 1.1 agc ntohs(*((uint16_t *) pdu->temp_data)));
701 1.1 agc memcpy(req_ccb->sense_ptr, ((uint16_t *) pdu->temp_data) + 1,
702 1.1 agc len);
703 1.1 agc req_ccb->sense_len_got = len;
704 1.1 agc }
705 1.1 agc
706 1.1 agc if (req_ccb == NULL) {
707 1.1 agc /* Assume duplicate... */
708 1.1 agc DEBOUT(("Possibly duplicate command response (no associated CCB)\n"));
709 1.1 agc return -1;
710 1.1 agc }
711 1.1 agc
712 1.1 agc PERF_SNAP(req_ccb, PERF_PDURCVSTS);
713 1.1 agc
714 1.1 agc if (req_ccb->pdu_waiting != NULL) {
715 1.1 agc SET_CCB_TIMEOUT(conn, req_ccb, COMMAND_TIMEOUT);
716 1.1 agc req_ccb->num_timeouts = 0;
717 1.1 agc }
718 1.1 agc
719 1.1 agc req_ccb->flags |= CCBF_COMPLETE;
720 1.1 agc conn->recover = 0; /* successful transfer, reset recover count */
721 1.1 agc
722 1.1 agc if (pdu->pdu.OpcodeSpecific[0]) { /* Response */
723 1.1 agc req_ccb->status = ISCSI_STATUS_TARGET_FAILURE;
724 1.1 agc } else {
725 1.1 agc switch (pdu->pdu.OpcodeSpecific[1]) { /* Status */
726 1.1 agc case 0x00:
727 1.1 agc /* success */
728 1.1 agc break;
729 1.1 agc
730 1.1 agc case 0x02:
731 1.1 agc req_ccb->status = ISCSI_STATUS_CHECK_CONDITION;
732 1.1 agc break;
733 1.1 agc
734 1.1 agc case 0x08:
735 1.1 agc req_ccb->status = ISCSI_STATUS_TARGET_BUSY;
736 1.1 agc break;
737 1.1 agc
738 1.1 agc default:
739 1.1 agc req_ccb->status = ISCSI_STATUS_TARGET_ERROR;
740 1.1 agc break;
741 1.1 agc }
742 1.1 agc }
743 1.1 agc
744 1.1 agc if (pdu->pdu.Flags & (FLAG_OVERFLOW | FLAG_UNDERFLOW))
745 1.1 agc req_ccb->residual = ntohl(pdu->pdu.p.response.ResidualCount);
746 1.1 agc
747 1.1 agc done = req_ccb->status || sn_empty(&req_ccb->DataSN_buf);
748 1.1 agc
749 1.1 agc DEBC(conn, 10, ("Rx Command Response rsp = %x, status = %x\n",
750 1.1 agc pdu->pdu.OpcodeSpecific[0], pdu->pdu.OpcodeSpecific[1]));
751 1.1 agc
752 1.1 agc rc = check_StatSN(conn, pdu->pdu.p.response.StatSN, done);
753 1.1 agc
754 1.1 agc if (done)
755 1.1 agc wake_ccb(req_ccb);
756 1.1 agc
757 1.1 agc return rc;
758 1.1 agc }
759 1.1 agc
760 1.1 agc
761 1.1 agc /*
762 1.1 agc * receive_asynch_pdu
763 1.1 agc * Handle receipt of an asynchronous message PDU.
764 1.1 agc *
765 1.1 agc * Parameter:
766 1.1 agc * conn The connection
767 1.1 agc * pdu The PDU
768 1.1 agc */
769 1.1 agc
770 1.1 agc STATIC int
771 1.1 agc receive_asynch_pdu(connection_t *conn, pdu_t *pdu)
772 1.1 agc {
773 1.1 agc
774 1.1 agc DEBOUT(("Received Asynch PDU, Event %d\n", pdu->pdu.p.asynch.AsyncEvent));
775 1.1 agc
776 1.1 agc switch (pdu->pdu.p.asynch.AsyncEvent) {
777 1.1 agc case 0: /* SCSI Asynch event. Don't know what to do with it... */
778 1.1 agc break;
779 1.1 agc
780 1.1 agc case 1: /* Target requests logout. */
781 1.1 agc if (conn->session->active_connections > 1) {
782 1.1 agc kill_connection(conn, ISCSI_STATUS_TARGET_LOGOUT,
783 1.1 agc LOGOUT_CONNECTION, FALSE);
784 1.1 agc } else {
785 1.1 agc kill_session(conn->session, ISCSI_STATUS_TARGET_LOGOUT,
786 1.1 agc LOGOUT_SESSION, FALSE);
787 1.1 agc }
788 1.1 agc break;
789 1.1 agc
790 1.1 agc case 2: /* Target is dropping connection */
791 1.1 agc conn = find_connection(conn->session,
792 1.1 agc ntohs(pdu->pdu.p.asynch.Parameter1));
793 1.1 agc if (conn != NULL) {
794 1.1 agc conn->Time2Wait = ntohs(pdu->pdu.p.asynch.Parameter2);
795 1.1 agc conn->Time2Retain = ntohs(pdu->pdu.p.asynch.Parameter3);
796 1.1 agc kill_connection(conn, ISCSI_STATUS_TARGET_DROP,
797 1.1 agc NO_LOGOUT, TRUE);
798 1.1 agc }
799 1.1 agc break;
800 1.1 agc
801 1.1 agc case 3: /* Target is dropping all connections of session */
802 1.1 agc conn->session->DefaultTime2Wait = ntohs(pdu->pdu.p.asynch.Parameter2);
803 1.1 agc conn->session->DefaultTime2Retain = ntohs(pdu->pdu.p.asynch.Parameter3);
804 1.1 agc kill_session(conn->session, ISCSI_STATUS_TARGET_DROP, NO_LOGOUT, TRUE);
805 1.1 agc break;
806 1.1 agc
807 1.1 agc case 4: /* Target requests parameter negotiation */
808 1.1 agc start_text_negotiation(conn);
809 1.1 agc break;
810 1.1 agc
811 1.1 agc default:
812 1.1 agc /* ignore */
813 1.1 agc break;
814 1.1 agc }
815 1.1 agc return 0;
816 1.1 agc }
817 1.1 agc
818 1.1 agc
819 1.1 agc /*
820 1.1 agc * receive_reject_pdu
821 1.1 agc * Handle receipt of a reject PDU.
822 1.1 agc *
823 1.1 agc * Parameter:
824 1.1 agc * conn The connection
825 1.1 agc * pdu The PDU
826 1.1 agc */
827 1.1 agc
828 1.1 agc STATIC int
829 1.1 agc receive_reject_pdu(connection_t *conn, pdu_t *pdu)
830 1.1 agc {
831 1.1 agc pdu_header_t *hpdu;
832 1.1 agc ccb_t *req_ccb;
833 1.1 agc
834 1.1 agc DEBOUT(("Received Reject PDU, reason = %x, data_len = %d\n",
835 1.1 agc pdu->pdu.OpcodeSpecific[0], pdu->temp_data_len));
836 1.1 agc
837 1.1 agc if (pdu->temp_data_len >= BHS_SIZE) {
838 1.1 agc hpdu = (pdu_header_t *) pdu->temp_data;
839 1.1 agc req_ccb = ccb_from_itt(conn, hpdu->InitiatorTaskTag);
840 1.1 agc
841 1.1 agc DEBC(conn, 9, ("Reject PDU ITT (ccb)= %x (%p)\n",
842 1.1 agc hpdu->InitiatorTaskTag, req_ccb));
843 1.1 agc if (!req_ccb) {
844 1.1 agc return 0;
845 1.1 agc }
846 1.1 agc switch (pdu->pdu.OpcodeSpecific[0]) {
847 1.1 agc case REJECT_DIGEST_ERROR:
848 1.1 agc /* don't retransmit data out */
849 1.1 agc if ((hpdu->Opcode & OPCODE_MASK) == IOP_SCSI_Data_out)
850 1.1 agc return 0;
851 1.1 agc resend_pdu(req_ccb);
852 1.1 agc return 0;
853 1.1 agc
854 1.1 agc case REJECT_IMMED_COMMAND:
855 1.1 agc case REJECT_LONG_OPERATION:
856 1.1 agc resend_pdu(req_ccb);
857 1.1 agc return 0;
858 1.1 agc
859 1.1 agc case REJECT_SNACK:
860 1.1 agc case REJECT_PROTOCOL_ERROR:
861 1.1 agc req_ccb->status = ISCSI_STATUS_PROTOCOL_ERROR;
862 1.1 agc break;
863 1.1 agc
864 1.1 agc case REJECT_CMD_NOT_SUPPORTED:
865 1.1 agc req_ccb->status = ISCSI_STATUS_CMD_NOT_SUPPORTED;
866 1.1 agc break;
867 1.1 agc
868 1.1 agc case REJECT_INVALID_PDU_FIELD:
869 1.1 agc req_ccb->status = ISCSI_STATUS_PDU_ERROR;
870 1.1 agc break;
871 1.1 agc
872 1.1 agc default:
873 1.1 agc req_ccb->status = ISCSI_STATUS_GENERAL_ERROR;
874 1.1 agc break;
875 1.1 agc }
876 1.1 agc
877 1.1 agc wake_ccb(req_ccb);
878 1.1 agc handle_connection_error(conn, ISCSI_STATUS_PROTOCOL_ERROR,
879 1.1 agc LOGOUT_CONNECTION);
880 1.1 agc }
881 1.1 agc return 0;
882 1.1 agc }
883 1.1 agc
884 1.1 agc
885 1.1 agc /*
886 1.1 agc * receive_task_management_pdu
887 1.1 agc * Handle receipt of a task management PDU.
888 1.1 agc *
889 1.1 agc * Parameter:
890 1.1 agc * conn The connection
891 1.1 agc * pdu The PDU
892 1.1 agc * req_CCB The CCB associated with the original request (if any)
893 1.1 agc */
894 1.1 agc
895 1.1 agc STATIC int
896 1.1 agc receive_task_management_pdu(connection_t *conn, pdu_t *pdu, ccb_t *req_ccb)
897 1.1 agc {
898 1.1 agc
899 1.1 agc DEBC(conn, 2, ("Received Task Management PDU, response %d, req_ccb %p\n",
900 1.1 agc pdu->pdu.OpcodeSpecific[0], req_ccb));
901 1.1 agc
902 1.1 agc if (req_ccb != NULL) {
903 1.1 agc switch (pdu->pdu.OpcodeSpecific[0]) { /* Response */
904 1.1 agc case 0:
905 1.1 agc req_ccb->status = ISCSI_STATUS_SUCCESS;
906 1.1 agc break;
907 1.1 agc case 1:
908 1.1 agc req_ccb->status = ISCSI_STATUS_TASK_NOT_FOUND;
909 1.1 agc break;
910 1.1 agc case 2:
911 1.1 agc req_ccb->status = ISCSI_STATUS_LUN_NOT_FOUND;
912 1.1 agc break;
913 1.1 agc case 3:
914 1.1 agc req_ccb->status = ISCSI_STATUS_TASK_ALLEGIANT;
915 1.1 agc break;
916 1.1 agc case 4:
917 1.1 agc req_ccb->status = ISCSI_STATUS_CANT_REASSIGN;
918 1.1 agc break;
919 1.1 agc case 5:
920 1.1 agc req_ccb->status = ISCSI_STATUS_FUNCTION_UNSUPPORTED;
921 1.1 agc break;
922 1.1 agc case 6:
923 1.1 agc req_ccb->status = ISCSI_STATUS_FUNCTION_NOT_AUTHORIZED;
924 1.1 agc break;
925 1.1 agc case 255:
926 1.1 agc req_ccb->status = ISCSI_STATUS_FUNCTION_REJECTED;
927 1.1 agc break;
928 1.1 agc default:
929 1.1 agc req_ccb->status = ISCSI_STATUS_UNKNOWN_REASON;
930 1.1 agc break;
931 1.1 agc }
932 1.1 agc wake_ccb(req_ccb);
933 1.1 agc }
934 1.1 agc
935 1.1 agc check_StatSN(conn, pdu->pdu.p.task_rsp.StatSN, TRUE);
936 1.1 agc
937 1.1 agc return 0;
938 1.1 agc }
939 1.1 agc
940 1.1 agc
941 1.1 agc /*
942 1.1 agc * receive_nop_in_pdu
943 1.1 agc * Handle receipt of a Nop-In PDU.
944 1.1 agc *
945 1.1 agc * Parameter:
946 1.1 agc * conn The connection
947 1.1 agc * pdu The PDU
948 1.1 agc * req_CCB The CCB associated with the original request (if any)
949 1.1 agc */
950 1.1 agc
951 1.1 agc STATIC int
952 1.1 agc receive_nop_in_pdu(connection_t *conn, pdu_t *pdu, ccb_t *req_ccb)
953 1.1 agc {
954 1.1 agc DEBC(conn, 10,
955 1.1 agc ("Received NOP-In PDU, req_ccb=%p, ITT=%x, TTT=%x, StatSN=%x\n",
956 1.1 agc req_ccb, pdu->pdu.InitiatorTaskTag,
957 1.1 agc pdu->pdu.p.nop_in.TargetTransferTag,
958 1.1 agc ntohl(pdu->pdu.p.nop_in.StatSN)));
959 1.1 agc
960 1.1 agc if (pdu->pdu.InitiatorTaskTag == 0xffffffff) {
961 1.1 agc /* this is a target ping - respond with a pong */
962 1.1 agc if (pdu->pdu.p.nop_in.TargetTransferTag != 0xffffffff)
963 1.1 agc send_nop_out(conn, pdu);
964 1.1 agc
965 1.1 agc /*
966 1.1 agc Any receive resets the connection timeout, but we got a ping, which
967 1.1 agc means that it's likely the other side was waiting for something to
968 1.1 agc happen on the connection. If we aren't idle, send a ping right
969 1.1 agc away to synch counters (don't synch on this ping because other
970 1.1 agc PDUs may be on the way).
971 1.1 agc */
972 1.1 agc if (TAILQ_FIRST(&conn->ccbs_waiting) != NULL)
973 1.1 agc send_nop_out(conn, NULL);
974 1.1 agc } else if (req_ccb != NULL) {
975 1.1 agc /* this is a solicited ping, check CmdSN for lost commands */
976 1.1 agc /* and advance StatSN */
977 1.1 agc check_CmdSN(conn, pdu->pdu.p.nop_in.ExpCmdSN);
978 1.1 agc
979 1.1 agc wake_ccb(req_ccb);
980 1.1 agc
981 1.1 agc check_StatSN(conn, pdu->pdu.p.nop_in.StatSN, TRUE);
982 1.1 agc }
983 1.1 agc
984 1.1 agc return 0;
985 1.1 agc }
986 1.1 agc
987 1.1 agc
988 1.1 agc /*
989 1.1 agc * receive_pdu
990 1.1 agc * Get parameters, call the appropriate handler for a received PDU.
991 1.1 agc *
992 1.1 agc * Parameter:
993 1.1 agc * conn The connection
994 1.1 agc * pdu The PDU
995 1.1 agc *
996 1.1 agc * Returns: 0 on success, nonzero if the connection is broken.
997 1.1 agc */
998 1.1 agc
999 1.1 agc STATIC int
1000 1.1 agc receive_pdu(connection_t *conn, pdu_t *pdu)
1001 1.1 agc {
1002 1.1 agc ccb_t *req_ccb;
1003 1.1 agc ccb_list_t waiting;
1004 1.1 agc int rc;
1005 1.1 agc uint32_t MaxCmdSN, digest;
1006 1.1 agc session_t *sess = conn->session;
1007 1.1 agc
1008 1.1 agc if (conn->HeaderDigest) {
1009 1.1 agc digest = gen_digest(&pdu->pdu, BHS_SIZE);
1010 1.1 agc if (digest != pdu->pdu.HeaderDigest) {
1011 1.1 agc DEBOUT(("Header Digest Error: comp = %08x, rx = %08x\n",
1012 1.1 agc digest, pdu->pdu.HeaderDigest));
1013 1.1 agc #ifdef ISCSI_TEST_MODE
1014 1.1 agc test_mode_rx(conn, pdu, TEST_INVALID_HEADER_CRC);
1015 1.1 agc #endif
1016 1.1 agc /* try to skip to next PDU */
1017 1.1 agc try_resynch_receive(conn);
1018 1.1 agc free_pdu(pdu);
1019 1.1 agc return 0;
1020 1.1 agc }
1021 1.1 agc }
1022 1.1 agc
1023 1.1 agc req_ccb = ccb_from_itt(conn, pdu->pdu.InitiatorTaskTag);
1024 1.1 agc
1025 1.1 agc if (req_ccb != NULL && req_ccb->data_in && req_ccb->data_len &&
1026 1.1 agc (pdu->pdu.Opcode & OPCODE_MASK) == TOP_SCSI_Data_in) {
1027 1.1 agc uint32_t dsl, offset;
1028 1.1 agc
1029 1.1 agc dsl = ntoh3(pdu->pdu.DataSegmentLength);
1030 1.1 agc offset = ntohl(pdu->pdu.p.data_in.BufferOffset);
1031 1.1 agc
1032 1.1 agc if ((offset + dsl) > req_ccb->data_len) {
1033 1.1 agc DEBOUT(("Received more data than requested (len %d, offset %d)\n",
1034 1.1 agc dsl, offset));
1035 1.1 agc handle_connection_error(conn, ISCSI_STATUS_TARGET_ERROR, NO_LOGOUT);
1036 1.1 agc return 1;
1037 1.1 agc }
1038 1.1 agc DEBC(conn, 10,
1039 1.1 agc ("Received Data in PDU - CCB = %p, Datalen = %d, Offset = %d\n",
1040 1.1 agc req_ccb, dsl, offset));
1041 1.1 agc
1042 1.1 agc PERF_SNAPC(req_ccb, PERF_BEGIN_PDURCVDATA);
1043 1.1 agc rc = read_pdu_data(pdu, req_ccb->data_ptr, offset);
1044 1.1 agc PERF_SNAP(req_ccb, PERF_END_PDURCVDATA);
1045 1.1 agc } else {
1046 1.1 agc rc = read_pdu_data(pdu, NULL, 0);
1047 1.1 agc }
1048 1.1 agc #ifdef ISCSI_TEST_MODE
1049 1.1 agc if (test_mode_rx(conn, pdu, rc)) {
1050 1.1 agc free_pdu(pdu);
1051 1.1 agc return rc;
1052 1.1 agc }
1053 1.1 agc #endif
1054 1.1 agc if (!rc && (conn->state <= ST_WINDING_DOWN ||
1055 1.1 agc (pdu->pdu.Opcode & OPCODE_MASK) == TOP_Logout_Response)) {
1056 1.1 agc
1057 1.1 agc switch (pdu->pdu.Opcode & OPCODE_MASK) {
1058 1.1 agc case TOP_NOP_In:
1059 1.1 agc rc = receive_nop_in_pdu(conn, pdu, req_ccb);
1060 1.1 agc break;
1061 1.1 agc
1062 1.1 agc case TOP_SCSI_Response:
1063 1.1 agc rc = receive_command_response_pdu(conn, pdu, req_ccb);
1064 1.1 agc break;
1065 1.1 agc
1066 1.1 agc case TOP_SCSI_Task_Management:
1067 1.1 agc rc = receive_task_management_pdu(conn, pdu, req_ccb);
1068 1.1 agc break;
1069 1.1 agc
1070 1.1 agc case TOP_Login_Response:
1071 1.1 agc rc = receive_login_pdu(conn, pdu, req_ccb);
1072 1.1 agc break;
1073 1.1 agc
1074 1.1 agc case TOP_Text_Response:
1075 1.1 agc rc = receive_text_response_pdu(conn, pdu, req_ccb);
1076 1.1 agc break;
1077 1.1 agc
1078 1.1 agc case TOP_SCSI_Data_in:
1079 1.1 agc rc = receive_data_in_pdu(conn, pdu, req_ccb);
1080 1.1 agc break;
1081 1.1 agc
1082 1.1 agc case TOP_Logout_Response:
1083 1.1 agc rc = receive_logout_pdu(conn, pdu, req_ccb);
1084 1.1 agc break;
1085 1.1 agc
1086 1.1 agc case TOP_R2T:
1087 1.1 agc rc = receive_r2t_pdu(conn, pdu, req_ccb);
1088 1.1 agc break;
1089 1.1 agc
1090 1.1 agc case TOP_Asynchronous_Message:
1091 1.1 agc rc = receive_asynch_pdu(conn, pdu);
1092 1.1 agc break;
1093 1.1 agc
1094 1.1 agc case TOP_Reject:
1095 1.1 agc rc = receive_reject_pdu(conn, pdu);
1096 1.1 agc break;
1097 1.1 agc
1098 1.1 agc default:
1099 1.1 agc DEBOUT(("Received Invalid Opcode %x\n", pdu->pdu.Opcode));
1100 1.1 agc try_resynch_receive(conn);
1101 1.1 agc rc = -1;
1102 1.1 agc break;
1103 1.1 agc }
1104 1.1 agc }
1105 1.1 agc
1106 1.1 agc free_pdu(pdu);
1107 1.1 agc if (rc)
1108 1.1 agc return rc;
1109 1.1 agc
1110 1.1 agc /* MaxCmdSN and ExpCmdSN are in the same place in all received PDUs */
1111 1.1 agc sess->ExpCmdSN = max(sess->ExpCmdSN, ntohl(pdu->pdu.p.nop_in.ExpCmdSN));
1112 1.1 agc MaxCmdSN = ntohl(pdu->pdu.p.nop_in.MaxCmdSN);
1113 1.1 agc
1114 1.1 agc /* received a valid frame, reset timeout */
1115 1.1 agc
1116 1.1 agc SET_CONN_TIMEOUT(conn,
1117 1.1 agc (((pdu->pdu.Opcode & OPCODE_MASK) == TOP_NOP_In) &&
1118 1.1 agc (TAILQ_FIRST(&conn->ccbs_waiting) == NULL)) ?
1119 1.1 agc conn->idle_timeout_val : CONNECTION_TIMEOUT);
1120 1.1 agc conn->num_timeouts = 0;
1121 1.1 agc
1122 1.1 agc /*
1123 1.1 agc * Un-throttle - wakeup all CCBs waiting for MaxCmdSN to increase.
1124 1.1 agc * We have to handle wait/nowait CCBs a bit differently.
1125 1.1 agc */
1126 1.1 agc if (MaxCmdSN != sess->MaxCmdSN) {
1127 1.1 agc sess->MaxCmdSN = MaxCmdSN;
1128 1.1 agc #if 0
1129 1.1 agc /* XXX - agc */
1130 1.1 agc if (TAILQ_FIRST(&sess->ccbs_throttled) == NULL ||
1131 1.1 agc !sn_a_lt_b(sess->CmdSN, MaxCmdSN))
1132 1.1 agc return 0;
1133 1.1 agc #else
1134 1.1 agc if (TAILQ_FIRST(&sess->ccbs_throttled) == NULL)
1135 1.1 agc return 0;
1136 1.1 agc #endif
1137 1.1 agc
1138 1.1 agc DEBC(conn, 1, ("Unthrottling - MaxCmdSN = %d\n", MaxCmdSN));
1139 1.1 agc
1140 1.1 agc CS_BEGIN;
1141 1.1 agc waiting = sess->ccbs_throttled;
1142 1.1 agc TAILQ_INIT(&sess->ccbs_throttled);
1143 1.1 agc CS_END;
1144 1.1 agc while ((req_ccb = TAILQ_FIRST(&waiting)) != NULL) {
1145 1.1 agc TAILQ_REMOVE(&waiting, req_ccb, chain);
1146 1.1 agc DEBC(conn, 1, ("Unthrottling - ccb = %p, disp = %d\n",
1147 1.1 agc req_ccb, req_ccb->disp));
1148 1.1 agc
1149 1.1 agc if (req_ccb->flags & CCBF_WAITING)
1150 1.1 agc wakeup(req_ccb);
1151 1.1 agc else
1152 1.1 agc send_command(req_ccb, req_ccb->disp, FALSE, FALSE);
1153 1.1 agc }
1154 1.1 agc }
1155 1.1 agc
1156 1.1 agc return 0;
1157 1.1 agc }
1158 1.1 agc
1159 1.1 agc /*****************************************************************************/
1160 1.1 agc
1161 1.1 agc /*
1162 1.1 agc * iscsi_receive_thread
1163 1.1 agc * Per connection thread handling receive data.
1164 1.1 agc *
1165 1.1 agc * Parameter:
1166 1.1 agc * conn The connection
1167 1.1 agc */
1168 1.1 agc
1169 1.1 agc void
1170 1.1 agc iscsi_rcv_thread(void *par)
1171 1.1 agc {
1172 1.1 agc connection_t *conn = (connection_t *) par;
1173 1.1 agc pdu_t *pdu;
1174 1.1 agc size_t hlen;
1175 1.1 agc
1176 1.1 agc do {
1177 1.1 agc while (!conn->terminating) {
1178 1.1 agc pdu = get_pdu(conn);
1179 1.1 agc pdu->uio.uio_iov = pdu->io_vec;
1180 1.1 agc UIO_SETUP_SYSSPACE(&pdu->uio);
1181 1.1 agc pdu->uio.uio_iovcnt = 1;
1182 1.1 agc pdu->uio.uio_rw = UIO_READ;
1183 1.1 agc
1184 1.1 agc pdu->io_vec[0].iov_base = &pdu->pdu;
1185 1.1 agc hlen = (conn->HeaderDigest) ? BHS_SIZE + 4 : BHS_SIZE;
1186 1.1 agc pdu->io_vec[0].iov_len = hlen;
1187 1.1 agc pdu->uio.uio_resid = hlen;
1188 1.1 agc
1189 1.1 agc DEBC(conn, 99, ("Receive thread waiting for data\n"));
1190 1.1 agc if (my_soo_read(conn, &pdu->uio, MSG_WAITALL)) {
1191 1.1 agc free_pdu(pdu);
1192 1.1 agc break;
1193 1.1 agc }
1194 1.1 agc /* Check again for header digest */
1195 1.1 agc /* (it may have changed during the wait) */
1196 1.1 agc if (hlen == BHS_SIZE && conn->HeaderDigest) {
1197 1.1 agc pdu->uio.uio_iov = pdu->io_vec;
1198 1.1 agc pdu->uio.uio_iovcnt = 1;
1199 1.1 agc pdu->io_vec[0].iov_base = &pdu->pdu.HeaderDigest;
1200 1.1 agc pdu->io_vec[0].iov_len = 4;
1201 1.1 agc pdu->uio.uio_resid = 4;
1202 1.1 agc if (my_soo_read(conn, &pdu->uio, MSG_WAITALL)) {
1203 1.1 agc free_pdu(pdu);
1204 1.1 agc break;
1205 1.1 agc }
1206 1.1 agc }
1207 1.1 agc
1208 1.1 agc if (receive_pdu(conn, pdu) > 0) {
1209 1.1 agc break;
1210 1.1 agc }
1211 1.1 agc }
1212 1.1 agc if (!conn->destroy) {
1213 1.1 agc tsleep(conn, PRIBIO, "conn_idle", 0);
1214 1.1 agc }
1215 1.1 agc } while (!conn->destroy);
1216 1.1 agc
1217 1.1 agc conn->rcvproc = NULL;
1218 1.1 agc DEBC(conn, 5, ("Receive thread exits\n"));
1219 1.1 agc kthread_exit(0);
1220 1.1 agc }
1221