xref: /illumos-gate/usr/src/uts/common/io/usb/scsa2usb/usb_ms_bulkonly.c (revision 33efde4275d24731ef87927237b0ffb0630b6b2d)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  *
21  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
22  * Use is subject to license terms.
23  */
24 
25 
26 /*
27  * scsa2usb_ms_bulkonly.c:
28  *
29  * This file implements USB Mass Storage Class
30  * Bulk Only (BO) transport v1.0
31  * http://www.usb.org/developers/data/devclass/usbmassbulk_10.pdf
32  */
33 #include <sys/usb/usba/usbai_version.h>
34 #include <sys/scsi/scsi.h>
35 #include <sys/callb.h>		/* needed by scsa2usb.h */
36 #include <sys/strsubr.h>
37 #include <sys/strsun.h>
38 
39 #include <sys/usb/usba.h>
40 #include <sys/usb/usba/usba_private.h>
41 #include <sys/usb/usba/usba_ugen.h>
42 
43 #include <sys/usb/clients/mass_storage/usb_bulkonly.h>
44 #include <sys/usb/scsa2usb/scsa2usb.h>
45 
46 /*
47  * Function Prototypes
48  */
49 int		scsa2usb_bulk_only_transport(scsa2usb_state_t *,
50 		    scsa2usb_cmd_t *);
51 static void	scsa2usb_fill_in_cbw(scsa2usb_state_t *, scsa2usb_cmd_t *,
52 		    mblk_t *);
53 static void	scsa2usb_bulk_only_reset_recovery(scsa2usb_state_t *);
54 static void	scsa2usb_bulk_only_handle_error(scsa2usb_state_t *,
55 		    usb_bulk_req_t *);
56 int		scsa2usb_bulk_only_get_max_lun(scsa2usb_state_t *);
57 static int	scsa2usb_handle_status_start(scsa2usb_state_t *,
58 		    usb_bulk_req_t *);
59 static int	scsa2usb_handle_csw_result(scsa2usb_state_t *, mblk_t *);
60 
61 
62 /* extern functions */
63 extern void	scsa2usb_setup_next_xfer(scsa2usb_state_t *, scsa2usb_cmd_t *);
64 extern int	scsa2usb_handle_data_start(scsa2usb_state_t *,
65 		    scsa2usb_cmd_t *, usb_bulk_req_t *);
66 extern void	scsa2usb_handle_data_done(scsa2usb_state_t *, scsa2usb_cmd_t *,
67 		    usb_bulk_req_t *);
68 extern usb_bulk_req_t *scsa2usb_init_bulk_req(scsa2usb_state_t *,
69 			    size_t, uint_t, usb_req_attrs_t, usb_flags_t);
70 extern int	scsa2usb_bulk_timeout(int);
71 extern int	scsa2usb_clear_ept_stall(scsa2usb_state_t *, uint_t,
72 		    usb_pipe_handle_t, char *);
73 extern void	scsa2usb_close_usb_pipes(scsa2usb_state_t *);
74 
75 #ifdef DEBUG	/* debugging information */
76 extern void	scsa2usb_print_cdb(scsa2usb_state_t *, scsa2usb_cmd_t *);
77 #endif	/* DEBUG */
78 
79 
80 #ifdef	SCSA2USB_BULK_ONLY_TEST
81 /*
82  * Test 13 cases. (See USB Mass Storage Class - Bulk Only Transport).
83  * We are not covering test cases 1, 6, and 12 as these are the "good"
84  * test cases and are tested as part of the normal drive access operations.
85  *
86  * NOTE: This is for testing only. It will be replaced by a uscsi test.
87  */
88 int scsa2usb_test_case_2 = 0;
89 int scsa2usb_test_case_3 = 0;
90 int scsa2usb_test_case_4 = 0;
91 int scsa2usb_test_case_7 = 0;
92 extern int scsa2usb_test_case_8;
93 int scsa2usb_test_case_9 = 0;
94 extern int scsa2usb_test_case_10;
95 int scsa2usb_test_case_13 = 0;
96 #endif	/* SCSA2USB_BULK_ONLY_TEST */
97 
98 
99 /*
100  * scsa2usb_bulk_only_transport:
101  *	Implements the BO state machine by these steps:
102  *	a) Issues CBW to a Bulk Only device.
103  *	b) Start Data Phase if applicable
104  *	c) Start Status Phase
105  *
106  *	returns TRAN_* values
107  *
108  * scsa2usb_bulk_only_state_machine:
109  *
110  * scsa2usb_bulk_only_transport() handles the normal transitions or
111  * continuation after clearing stalls or error recovery.
112  *
113  * Command Phase:
114  *	prepare a valid CBW and transport it on bulk-out pipe
115  *	if error on bulkout:
116  *		set pkt_reason to CMD_TRAN_ERR
117  *		new pkt state is SCSA2USB_PKT_DO_COMP
118  *		reset recovery synchronously
119  *	else
120  *		proceed to data phase
121  *
122  * Data Phase:
123  *	if data in:
124  *		setup data in on bulkin
125  *	else if data out:
126  *		setup data out on bulkout
127  *
128  *	data: (in)
129  *		copy data transferred so far, no more data to transfer
130  *
131  *		if stall on bulkin pipe
132  *			terminate data transfers, set cmd_done
133  *			clear stall on bulkin syncrhonously
134  *		else if other exception
135  *			set pkt_reason to CMD_TRAN_ERR
136  *			new pkt state is SCSA2USB_PKT_DO_COMP
137  *			reset recovery syncrhonously
138  *		else (no error)
139  *			receive status
140  *
141  *	 data: (out)
142  *		if stall on bulkout pipe
143  *			terminate data transfers, set cmd_done
144  *			clear stall on bulkout synchronously USBA
145  *		else if other exception
146  *			set pkt_reason to CMD_TRAN_ERR
147  *			new pkt state is SCSA2USB_PKT_DO_COMP
148  *			reset recovery synchronously
149  *		else (no error)
150  *			receive status
151  *
152  * Status Phase:
153  *
154  *	if stall (first attempt)
155  *		new pkt state is SCSA2USB_PKT_PROCESS_CSW
156  *		setup receiving status on bulkin
157  *		if stall (second attempt)
158  *			new pkt state is SCSA2USB_PKT_DO_COMP
159  *			reset recovery synchronously, we are hosed.
160  *		else
161  *			goto check CSW
162  *	else
163  *		goto check CSW
164  *
165  * check CSW:
166  *	- check length equals 13, signature, and matching tag
167  *	- check status is less than or equal to 2
168  *	- check residue is less than or equal to data length
169  *		adjust residue based on if we got valid data
170  *
171  *	if not OK
172  *		new pkt state is SCSA2USB_PKT_DO_COMP
173  *		set pkt reason CMD_TRAN_ERR
174  *		reset recovery synchronously, we are hosed
175  *	else if phase error
176  *		new pkt state is SCSA2USB_PKT_DO_COMP
177  *		set pkt reason CMD_TRAN_ERR
178  *		reset recovery synchronously
179  *	else if (status < 2)
180  *		if status is equal to 1
181  *			set check condition
182  *		if residue
183  *			calculate residue from data xferred and DataResidue
184  *
185  *			set pkt_residue
186  *		goto  SCSA2USB_PKT_DO_COMP
187  *
188  * The reset recovery walks sequentially thru device reset, clearing
189  * stalls and pipe resets. When the reset recovery completes we return
190  * to the taskq thread.
191  *
192  * Clearing stalls clears the stall condition, resets the pipe, and
193  * then returns to the transport.
194  */
195 int
scsa2usb_bulk_only_transport(scsa2usb_state_t * scsa2usbp,scsa2usb_cmd_t * cmd)196 scsa2usb_bulk_only_transport(scsa2usb_state_t *scsa2usbp, scsa2usb_cmd_t *cmd)
197 {
198 	int	rval;
199 	int	nretry;
200 	usb_bulk_req_t *req;
201 
202 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
203 
204 Cmd_Phase:
205 	/*
206 	 * Start Command Phase
207 	 * Initialize a bulk_req_t
208 	 */
209 	req = scsa2usb_init_bulk_req(scsa2usbp, USB_BULK_CBWCMD_LEN,
210 	    SCSA2USB_BULK_PIPE_TIMEOUT, USB_ATTRS_PIPE_RESET, USB_FLAGS_SLEEP);
211 
212 	scsa2usb_fill_in_cbw(scsa2usbp, cmd, req->bulk_data);	/* Fill CBW */
213 	SCSA2USB_PRINT_CDB(scsa2usbp, cmd);			/* Print CDB */
214 
215 	/* Send a Bulk Command Block Wrapper (CBW) to the device */
216 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
217 
218 	ASSERT(req->bulk_timeout);
219 	rval = usb_pipe_bulk_xfer(scsa2usbp->scsa2usb_bulkout_pipe, req,
220 	    USB_FLAGS_SLEEP);
221 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
222 
223 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
224 	    "scsa2usb_bulk_only_transport: "
225 	    "sent cmd = 0x%x Tag = 0x%x DataXferLen = 0x%lx rval = %d",
226 	    cmd->cmd_cdb[SCSA2USB_OPCODE], cmd->cmd_tag, cmd->cmd_xfercount,
227 	    rval);
228 
229 	if (rval != USB_SUCCESS) {
230 		scsa2usb_bulk_only_handle_error(scsa2usbp, req);
231 
232 		return (TRAN_FATAL_ERROR);
233 	}
234 
235 	/* free the data */
236 	SCSA2USB_FREE_MSG(req->bulk_data);
237 	req->bulk_data = NULL;
238 
239 	/*
240 	 * Start Data Phase
241 	 * re-set timeout
242 	 */
243 	req->bulk_timeout = scsa2usb_bulk_timeout(cmd->cmd_timeout);
244 
245 	/*
246 	 * we've not transferred any data yet; updated in
247 	 * scsa2usb_handle_data_done
248 	 */
249 	cmd->cmd_resid_xfercount = cmd->cmd_xfercount;
250 
251 	if (cmd->cmd_xfercount) {
252 		/* start I/O to/from the device */
253 		rval = scsa2usb_handle_data_start(scsa2usbp, cmd, req);
254 
255 		/* handle data returned, if any */
256 		scsa2usb_handle_data_done(scsa2usbp, cmd, req);
257 
258 		if (rval != USB_SUCCESS) {
259 			USB_DPRINTF_L2(DPRINT_MASK_SCSA,
260 			    scsa2usbp->scsa2usb_log_handle,
261 			    "data xfer phase, error =  %d, cr = %d",
262 			    rval, req->bulk_completion_reason);
263 			/*
264 			 * we ran into an error
265 			 */
266 			if (req->bulk_completion_reason == USB_CR_STALL) {
267 				if (scsa2usbp->scsa2usb_cur_pkt) {
268 					scsa2usbp->scsa2usb_cur_pkt->
269 					    pkt_reason = CMD_TRAN_ERR;
270 				}
271 			} else {
272 				scsa2usb_bulk_only_handle_error(scsa2usbp, req);
273 
274 				return (TRAN_FATAL_ERROR);
275 			}
276 		} /* end of else */
277 
278 		/* free the data */
279 		SCSA2USB_FREE_MSG(req->bulk_data);
280 		req->bulk_data = NULL;
281 	}
282 
283 	/*
284 	 * Start status phase
285 	 * read in CSW
286 	 */
287 	for (nretry = 0; nretry < SCSA2USB_STATUS_RETRIES; nretry++) {
288 		rval = scsa2usb_handle_status_start(scsa2usbp, req);
289 
290 		if ((rval != USB_SUCCESS) &&
291 		    (req->bulk_completion_reason == USB_CR_STALL)) {
292 			/*
293 			 * We ran into STALL condition here.
294 			 * If the condition cannot be cleared
295 			 * successfully, retry for limited times.
296 			 */
297 			scsa2usbp->scsa2usb_pkt_state =
298 			    SCSA2USB_PKT_PROCESS_CSW;
299 		} else {
300 
301 			break;
302 		}
303 	}
304 
305 	if (rval == USB_SUCCESS) {
306 		/* process CSW */
307 		rval = scsa2usb_handle_csw_result(scsa2usbp, req->bulk_data);
308 	} else {
309 		scsa2usb_bulk_only_handle_error(scsa2usbp, req);
310 
311 		return (TRAN_FATAL_ERROR);
312 	}
313 
314 	SCSA2USB_FREE_BULK_REQ(req);	/* free request */
315 
316 	if ((rval == USB_SUCCESS) &&		/* CSW was ok */
317 	    (scsa2usbp->scsa2usb_cur_pkt->pkt_reason == CMD_CMPLT) &&
318 	    (cmd->cmd_xfercount != 0) &&	/* more data to xfer */
319 	    !cmd->cmd_done) {			/* we aren't done yet */
320 		scsa2usb_setup_next_xfer(scsa2usbp, cmd);
321 		goto Cmd_Phase;
322 	}
323 
324 	return (rval == USB_SUCCESS ? TRAN_ACCEPT : TRAN_FATAL_ERROR);
325 }
326 
327 
328 /*
329  * scsa2usb_fill_in_cbw:
330  *	Fill in a CBW request packet. This
331  *	packet is transported to the device
332  */
333 static void
scsa2usb_fill_in_cbw(scsa2usb_state_t * scsa2usbp,scsa2usb_cmd_t * cmd,mblk_t * mp)334 scsa2usb_fill_in_cbw(scsa2usb_state_t *scsa2usbp,
335     scsa2usb_cmd_t *cmd, mblk_t *mp)
336 {
337 	int	i;
338 	int	len;
339 	uchar_t dir, *cdb = (uchar_t *)(&cmd->cmd_cdb);
340 
341 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
342 
343 	*mp->b_wptr++ = CBW_MSB(CBW_SIGNATURE);	/* CBW Signature */;
344 	*mp->b_wptr++ = CBW_MID1(CBW_SIGNATURE);
345 	*mp->b_wptr++ = CBW_MID2(CBW_SIGNATURE);
346 	*mp->b_wptr++ = CBW_LSB(CBW_SIGNATURE);
347 	*mp->b_wptr++ = CBW_LSB(cmd->cmd_tag);	/* CBW Tag */
348 	*mp->b_wptr++ = CBW_MID2(cmd->cmd_tag);
349 	*mp->b_wptr++ = CBW_MID1(cmd->cmd_tag);
350 	*mp->b_wptr++ = CBW_MSB(cmd->cmd_tag);
351 
352 	dir = cmd->cmd_dir;
353 	len = cmd->cmd_xfercount;
354 #ifdef	SCSA2USB_BULK_ONLY_TEST
355 	if (scsa2usb_test_case_2 && (cdb[0] == SCMD_READ_CAPACITY)) {
356 		/* Host expects no data. The device wants data. Hn < Di */
357 		scsa2usb_test_case_2 = len = 0;
358 		USB_DPRINTF_L1(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
359 		    "TEST 2: Hn < Di cdb: 0x%x len: 0x%x", cdb[0], len);
360 	}
361 
362 	if (scsa2usb_test_case_3 && (cmd->cmd_dir == CBW_DIR_OUT)) {
363 		/* Host expects no data. The device wants data. Hn < Do */
364 		if (cdb[0] == SCMD_WRITE_G1) {
365 			scsa2usb_test_case_3 = len = 0;
366 			USB_DPRINTF_L1(DPRINT_MASK_SCSA,
367 			    scsa2usbp->scsa2usb_log_handle,
368 			    "TEST 3: Hn < Do cdb: 0x%x len:%x", cdb[0], len);
369 		}
370 	}
371 
372 	if (scsa2usb_test_case_4 && (cdb[0] == SCMD_READ_G1)) {
373 		cdb[0] = 0x5e;
374 		USB_DPRINTF_L1(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
375 		    "TEST 4: Hi > Dn: changed cdb to 0x%x", cdb[0]);
376 		scsa2usb_test_case_4 = 0;
377 	}
378 
379 	if (scsa2usb_test_case_7 && (cmd->cmd_cdb[0] == SCMD_READ_G1)) {
380 		len -= 0x10;
381 		USB_DPRINTF_L1(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
382 		    "TEST 7: Hi < Di cdb: 0x%x len: 0x%x", cdb[0], len);
383 		scsa2usb_test_case_7 = 0;
384 	}
385 
386 	if (scsa2usb_test_case_8 && (cdb[0] == SCMD_READ_G1)) {
387 		dir = (dir == CBW_DIR_IN) ? CBW_DIR_OUT : dir;
388 		USB_DPRINTF_L1(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
389 		    "TEST 8: Hi <> Do cdb: 0x%x dir: 0x%x", cdb[0], dir);
390 	}
391 
392 	if (scsa2usb_test_case_9 && (cdb[0] == SCMD_WRITE_G1)) {
393 		USB_DPRINTF_L1(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
394 		    "TEST 9: Ho <> Di (%x)", cdb[0]);
395 		cdb[SCSA2USB_LEN_0] = cdb[SCSA2USB_LEN_1] = 0;
396 		scsa2usb_test_case_9 = 0;
397 	}
398 
399 	if (scsa2usb_test_case_10 && (cdb[0] == SCMD_WRITE_G1)) {
400 		dir = (dir == CBW_DIR_OUT) ? CBW_DIR_IN : dir;
401 		USB_DPRINTF_L1(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
402 		    "TEST 10: Ho <> Di cdb: 0x%x dir: 0x%x", cdb[0], dir);
403 	}
404 
405 	/*
406 	 * This case occurs when the device intends to receive
407 	 * more data from the host than the host sends.
408 	 */
409 	if (scsa2usb_test_case_13) {
410 		if ((cdb[0] == SCMD_WRITE_G1) || (cdb[0] == SCMD_READ_G1)) {
411 			USB_DPRINTF_L1(DPRINT_MASK_SCSA,
412 			    scsa2usbp->scsa2usb_log_handle, "TEST 13: Ho < Do");
413 
414 			len -= 30;
415 			scsa2usb_test_case_13 = 0;
416 		}
417 	}
418 #endif	/* SCSA2USB_BULK_ONLY_TEST */
419 
420 	*mp->b_wptr++ = CBW_MSB(len);		/* Transfer Length */
421 	*mp->b_wptr++ = CBW_MID1(len);
422 	*mp->b_wptr++ = CBW_MID2(len);
423 	*mp->b_wptr++ = CBW_LSB(len);
424 
425 	*mp->b_wptr++ = dir;			/* Transfer Direction */
426 	*mp->b_wptr++ = cmd->cmd_pkt->pkt_address.a_lun;	/* Lun # */
427 	*mp->b_wptr++ = cmd->cmd_actual_len;			/* CDB Len */
428 
429 	/* Copy the CDB out */
430 	for (i = 0; i < CBW_CDB_LEN; i++) {
431 		*mp->b_wptr++ = *cdb++;
432 	}
433 #ifdef DUMP_CWB
434 {
435 	int len = mp->b_wptr - mp->b_rptr;
436 	char *buf;
437 
438 	int i;
439 
440 	cmn_err(CE_CONT, "CWB: len=%d\n", len);
441 	buf = kmem_zalloc(512, KM_SLEEP);
442 	for (i = 0; i < len; i++) {
443 		sprintf(&buf[strlen(buf)], "%02x ", mp->b_rptr[i]);
444 	}
445 	cmn_err(CE_CONT, "%s\n", buf);
446 	kmem_free(buf, 512);
447 }
448 #endif
449 
450 }
451 
452 
453 /*
454  * scsa2usb_bulk_only_handle_error:
455  *	handle transport errors and start recovery
456  */
457 static void
scsa2usb_bulk_only_handle_error(scsa2usb_state_t * scsa2usbp,usb_bulk_req_t * req)458 scsa2usb_bulk_only_handle_error(scsa2usb_state_t *scsa2usbp,
459     usb_bulk_req_t *req)
460 {
461 	struct scsi_pkt *pkt = scsa2usbp->scsa2usb_cur_pkt;
462 
463 	USB_DPRINTF_L2(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
464 	    "scsa2usb_bulk_only_handle_error: req = 0x%p, cr = 0x%x",
465 	    (void *)req, (req ? req->bulk_completion_reason : 0));
466 
467 	if (req) {
468 		SCSA2USB_SET_PKT_DO_COMP_STATE(scsa2usbp);
469 
470 		/* invoke reset recovery */
471 		switch (req->bulk_completion_reason) {
472 		case USB_CR_STALL:
473 			if (pkt) {
474 				pkt->pkt_reason = CMD_TRAN_ERR;
475 			}
476 			break;
477 		case USB_CR_TIMEOUT:
478 			if (pkt) {
479 				pkt->pkt_reason = CMD_TIMEOUT;
480 				pkt->pkt_statistics |= STAT_TIMEOUT;
481 			}
482 			break;
483 		case USB_CR_DEV_NOT_RESP:
484 			if (pkt) {
485 				pkt->pkt_reason = CMD_DEV_GONE;
486 				/* scsi_poll relies on this */
487 				pkt->pkt_state = STATE_GOT_BUS;
488 			}
489 			break;
490 		default:
491 			if (pkt) {
492 				pkt->pkt_reason = CMD_TRAN_ERR;
493 			}
494 		}
495 		scsa2usb_bulk_only_reset_recovery(scsa2usbp);
496 	}
497 
498 	SCSA2USB_FREE_BULK_REQ(req);
499 }
500 
501 
502 /*
503  * scsa2usb_handle_status_start:
504  *	Receive status data
505  */
506 static int
scsa2usb_handle_status_start(scsa2usb_state_t * scsa2usbp,usb_bulk_req_t * req)507 scsa2usb_handle_status_start(scsa2usb_state_t *scsa2usbp,
508     usb_bulk_req_t *req)
509 {
510 	int rval;
511 
512 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
513 	    "scsa2usb_handle_status_start: req = 0x%p", (void *)req);
514 
515 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
516 
517 	/* setup up for receiving CSW */
518 #ifdef	SCSA2USB_BULK_ONLY_TEST
519 	req->bulk_attributes = 0;
520 #else
521 	req->bulk_attributes = USB_ATTRS_SHORT_XFER_OK;
522 #endif	/* SCSA2USB_BULK_ONLY_TEST */
523 	req->bulk_len = CSW_LEN;
524 
525 	SCSA2USB_FREE_MSG(req->bulk_data);
526 	req->bulk_data = allocb_wait(req->bulk_len,
527 	    BPRI_LO, STR_NOSIG, NULL);
528 
529 	/* Issue the request */
530 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
531 
532 	ASSERT(req->bulk_timeout);
533 	rval = usb_pipe_bulk_xfer(scsa2usbp->scsa2usb_bulkin_pipe, req,
534 	    USB_FLAGS_SLEEP);
535 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
536 
537 	USB_DPRINTF_L3(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
538 	    "scsa2usb_handle_status_start: END rval = 0x%x", rval);
539 
540 	if (rval != USB_SUCCESS) {
541 		if (scsa2usbp->scsa2usb_pkt_state == SCSA2USB_PKT_PROCESS_CSW) {
542 			scsa2usb_bulk_only_reset_recovery(scsa2usbp);
543 
544 			return (rval);
545 		}
546 
547 		if (req->bulk_completion_reason == USB_CR_STALL) {
548 			(void) scsa2usb_clear_ept_stall(scsa2usbp,
549 			    scsa2usbp->scsa2usb_bulkin_ept.bEndpointAddress,
550 			    scsa2usbp->scsa2usb_bulkin_pipe, "bulk-in");
551 		}
552 	}
553 
554 	return (rval);
555 }
556 
557 
558 /*
559  * scsa2usb_handle_csw_result:
560  *	Handle status results
561  */
562 static int
scsa2usb_handle_csw_result(scsa2usb_state_t * scsa2usbp,mblk_t * data)563 scsa2usb_handle_csw_result(scsa2usb_state_t *scsa2usbp, mblk_t *data)
564 {
565 	int		rval = USB_SUCCESS;
566 	int		residue;
567 	char		*msg = "CSW FAILED";
568 	uint_t		signature, tag, status;
569 	usb_bulk_csw_t	csw;
570 	struct scsi_pkt *pkt = scsa2usbp->scsa2usb_cur_pkt;
571 	scsa2usb_cmd_t	*cmd = PKT2CMD(pkt);
572 
573 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
574 
575 	/*
576 	 * This shouldn't happen. It implies the device's
577 	 * firmware is bad and has returned NULL CSW.
578 	 * return failure back.
579 	 */
580 	if (data == NULL) {
581 		USB_DPRINTF_L2(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
582 		    "scsa2usb_handle_csw_result: data == NULL");
583 
584 		return (USB_FAILURE);
585 	}
586 
587 	/* check if we got back CSW_LEN or not */
588 	if (MBLKL(data) != CSW_LEN) {
589 		USB_DPRINTF_L2(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
590 		    "scsa2usb_handle_csw_result: no enough data (%ld)",
591 		    (long)(MBLKL(data)));
592 
593 		return (USB_FAILURE);
594 	}
595 
596 	/* Read into csw */
597 	bcopy(data->b_rptr, &csw, CSW_LEN);
598 
599 	status = csw.csw_bCSWStatus;
600 	signature = SCSA2USB_MK_32BIT(csw.csw_dCSWSignature3,
601 	    csw.csw_dCSWSignature2, csw.csw_dCSWSignature1,
602 	    csw.csw_dCSWSignature0);
603 	residue = SCSA2USB_MK_32BIT(csw.csw_dCSWDataResidue3,
604 	    csw.csw_dCSWDataResidue2, csw.csw_dCSWDataResidue1,
605 	    csw.csw_dCSWDataResidue0);
606 	tag = SCSA2USB_MK_32BIT(csw.csw_dCSWTag3, csw.csw_dCSWTag2,
607 	    csw.csw_dCSWTag1, csw.csw_dCSWTag0);
608 
609 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
610 	    "CSW: Signature = 0x%x Status = 0%x Tag = 0x%x Residue = 0x%x",
611 	    signature, status, tag,  residue);
612 
613 	/* Check for abnormal errors */
614 	if ((signature != CSW_SIGNATURE) || (tag != cmd->cmd_tag) ||
615 	    (status > CSW_STATUS_PHASE_ERROR)) {
616 
617 		USB_DPRINTF_L2(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
618 		    "CSW_ERR: Status = 0x%x, Tag = 0x%x xfercount = 0x%lx",
619 		    status, cmd->cmd_tag, cmd->cmd_total_xfercount);
620 
621 		return (USB_FAILURE);
622 	}
623 
624 	switch (status) {
625 	case CSW_STATUS_GOOD:
626 		/*
627 		 * Fail the command if the device misbehaves and
628 		 * gives a good status but doesn't transfer any data.
629 		 * Otherwise we'll get into an infinite retry loop.
630 		 *
631 		 * We test only against cmd_total_xfercount here and
632 		 * assume that this will not happen on a command that
633 		 * transfers a large amount of data and therefore may
634 		 * be split into separate transfers. For a large data
635 		 * transfer it is assumed that the device will return
636 		 * an error status if the transfer does not occur.
637 		 * this isn't quite correct because a subsequent request
638 		 * sense may not give a valid sense key.
639 		 */
640 		if (!cmd->cmd_done && residue &&
641 		    (residue == cmd->cmd_total_xfercount)) {
642 			*(pkt->pkt_scbp) = STATUS_CHECK;
643 			cmd->cmd_xfercount = 0;
644 			cmd->cmd_done = 1;
645 		} else {
646 			msg = "CSW GOOD";
647 		}
648 		break;
649 	case CSW_STATUS_FAILED:
650 		*(pkt->pkt_scbp) = STATUS_CHECK; /* Set check condition */
651 		cmd->cmd_done = 1;
652 		break;
653 	case CSW_STATUS_PHASE_ERROR:
654 		USB_DPRINTF_L2(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
655 		    "scsa2usb_handle_csw_result: Phase Error");
656 
657 		/* invoke reset recovery */
658 		scsa2usb_bulk_only_handle_error(scsa2usbp, NULL);
659 
660 		return (USB_FAILURE);
661 	default:	/* shouldn't happen anymore */
662 		USB_DPRINTF_L2(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
663 		    "scsa2usb_handle_csw_result: Invalid CSW");
664 
665 		/* invoke reset recovery */
666 		scsa2usb_bulk_only_handle_error(scsa2usbp, NULL);
667 
668 		return (USB_SUCCESS);
669 	} /* end of switch */
670 
671 	/* Set resid */
672 	if (residue || cmd->cmd_resid_xfercount) {
673 		USB_DPRINTF_L2(DPRINT_MASK_SCSA,
674 		    scsa2usbp->scsa2usb_log_handle,
675 		    "total=0x%lx cmd_xfercount=0x%lx residue=0x%x "
676 		    "cmd_offset=0x%lx",
677 		    cmd->cmd_total_xfercount, cmd->cmd_xfercount,
678 		    residue, cmd->cmd_offset);
679 
680 		/*
681 		 * we need to adjust using the residue and
682 		 * assume worst case. Some devices lie about
683 		 * residue. some report a residue greater than
684 		 * the residue we have calculated.
685 		 * first adjust back the total_xfercount
686 		 */
687 		cmd->cmd_total_xfercount += cmd->cmd_xfercount -
688 		    cmd->cmd_resid_xfercount;
689 		/*
690 		 * we need to adjust cmd_offset as well, or the data
691 		 * buffer for subsequent transfer may exceed the buffer
692 		 * boundary
693 		 */
694 		cmd->cmd_offset -= cmd->cmd_xfercount -
695 		    cmd->cmd_resid_xfercount;
696 
697 		/*
698 		 * now take the min of the reported residue by
699 		 * the device and the requested xfer count
700 		 * (just in case the device reported a residue greater
701 		 * than our request count).
702 		 * then take the max of this residue and the residue
703 		 * that the HCD reported and subtract this from
704 		 * the request count. This is the actual number
705 		 * of valid bytes transferred during the last transfer
706 		 * which we now subtract from the total_xfercount
707 		 */
708 		if ((!(scsa2usbp->scsa2usb_attrs &
709 		    SCSA2USB_ATTRS_USE_CSW_RESIDUE)) ||
710 		    (residue < 0) ||
711 		    (residue > cmd->cmd_total_xfercount)) {
712 			/* some devices lie about the resid, ignore */
713 			cmd->cmd_total_xfercount -=
714 			    cmd->cmd_xfercount - cmd->cmd_resid_xfercount;
715 			cmd->cmd_offset +=
716 			    cmd->cmd_xfercount - cmd->cmd_resid_xfercount;
717 		} else {
718 			cmd->cmd_total_xfercount -=
719 			    cmd->cmd_xfercount -
720 			    max(min(residue, cmd->cmd_xfercount),
721 			    cmd->cmd_resid_xfercount);
722 			cmd->cmd_offset +=
723 			    cmd->cmd_xfercount -
724 			    max(min(residue, cmd->cmd_xfercount),
725 			    cmd->cmd_resid_xfercount);
726 			/*
727 			 * if HCD does not report residue while the device
728 			 * reports a residue equivalent to the xfercount,
729 			 * it is very likely the device lies about the
730 			 * residue. we need to stop the command, or we'll
731 			 * get into an infinite retry loop.
732 			 */
733 			if ((cmd->cmd_resid_xfercount == 0) &&
734 			    (residue == cmd->cmd_xfercount)) {
735 				cmd->cmd_xfercount = 0;
736 				cmd->cmd_done = 1;
737 			}
738 		}
739 
740 		pkt->pkt_resid = cmd->cmd_total_xfercount;
741 	}
742 
743 	USB_DPRINTF_L3(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
744 	    "scsa2usb_handle_csw_result: %s, resid: 0x%lx",
745 	    msg, pkt->pkt_resid);
746 
747 	/* we are done and ready to callback */
748 	SCSA2USB_SET_PKT_DO_COMP_STATE(scsa2usbp);
749 
750 	return (rval);
751 }
752 
753 
754 /*
755  * scsa2usb_bulk_only_reset_recovery:
756  *	Reset the USB device step-wise in case of errors.
757  *	NOTE that the order of reset is very important.
758  */
759 static void
scsa2usb_bulk_only_reset_recovery(scsa2usb_state_t * scsa2usbp)760 scsa2usb_bulk_only_reset_recovery(scsa2usb_state_t *scsa2usbp)
761 {
762 	int		rval;
763 	usb_cr_t	completion_reason;
764 	usb_cb_flags_t	cb_flags;
765 
766 	USB_DPRINTF_L2(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
767 	    "scsa2usb_bulk_only_reset_recovery: scsa2usbp = 0x%p",
768 	    (void *)scsa2usbp);
769 
770 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
771 
772 	if (!(SCSA2USB_DEVICE_ACCESS_OK(scsa2usbp))) {
773 
774 		return;
775 	}
776 
777 	/*
778 	 * assume that the reset will be successful. if it isn't, retrying
779 	 * from target driver won't help much
780 	 */
781 	if (scsa2usbp->scsa2usb_cur_pkt) {
782 		scsa2usbp->scsa2usb_cur_pkt->pkt_statistics |= STAT_DEV_RESET;
783 	}
784 
785 	/* set the reset condition */
786 	scsa2usbp->scsa2usb_pipe_state = SCSA2USB_PIPE_DEV_RESET;
787 
788 	/* Send a sync DEVICE-RESET request to the device */
789 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
790 	rval = usb_pipe_sync_ctrl_xfer(scsa2usbp->scsa2usb_dip,
791 	    scsa2usbp->scsa2usb_default_pipe,
792 	    USB_DEV_REQ_TYPE_CLASS | USB_DEV_REQ_RCPT_IF,
793 	    (uint8_t)BULK_ONLY_RESET,		/* bRequest */
794 	    0,					/* wValue */
795 	    scsa2usbp->scsa2usb_intfc_num,	/* wIndex */
796 	    0,					/* wLength */
797 	    NULL, 0, &completion_reason, &cb_flags, 0);
798 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
799 
800 	USB_DPRINTF_L2(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
801 	    "\tbulk-only device-reset rval: %d", rval);
802 	if (rval != USB_SUCCESS) {
803 		goto exc_exit;
804 	}
805 
806 	/* reset and clear STALL on bulk-in pipe */
807 	rval = scsa2usb_clear_ept_stall(scsa2usbp,
808 	    scsa2usbp->scsa2usb_bulkin_ept.bEndpointAddress,
809 	    scsa2usbp->scsa2usb_bulkin_pipe, "bulk-in");
810 
811 	USB_DPRINTF_L2(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
812 	    "\tbulk-in pipe clear stall: %d", rval);
813 	if (rval != USB_SUCCESS) {
814 		goto exc_exit;
815 	}
816 
817 	/* reset and clear STALL on bulk-out pipe */
818 	rval = scsa2usb_clear_ept_stall(scsa2usbp,
819 	    scsa2usbp->scsa2usb_bulkout_ept.bEndpointAddress,
820 	    scsa2usbp->scsa2usb_bulkout_pipe, "bulk-out");
821 
822 	USB_DPRINTF_L2(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
823 	    "\tbulk-out pipe clear stall: %d", rval);
824 
825 exc_exit:
826 	/* clear the reset condition */
827 	scsa2usbp->scsa2usb_pipe_state &= ~SCSA2USB_PIPE_DEV_RESET;
828 }
829 
830 
831 /*
832  * scsa2usb_bulk_only_get_max_lun:
833  *	this function returns the number of LUNs supported by the device
834  */
835 int
scsa2usb_bulk_only_get_max_lun(scsa2usb_state_t * scsa2usbp)836 scsa2usb_bulk_only_get_max_lun(scsa2usb_state_t *scsa2usbp)
837 {
838 	int		luns = 1, rval;
839 	mblk_t		*data = NULL;
840 	usb_cr_t	completion_reason;
841 	usb_cb_flags_t	cb_flags;
842 
843 	USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
844 	    "scsa2usb_bulk_only_get_max_lun:");
845 
846 	ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
847 
848 	mutex_exit(&scsa2usbp->scsa2usb_mutex);
849 	rval = usb_pipe_sync_ctrl_xfer(scsa2usbp->scsa2usb_dip,
850 	    scsa2usbp->scsa2usb_default_pipe,
851 	    BULK_ONLY_GET_MAXLUN_BMREQ,		/* bmRequestType */
852 	    BULK_ONLY_GET_MAXLUN_REQ,		/* bRequest */
853 	    0,					/* wValue */
854 	    scsa2usbp->scsa2usb_intfc_num,	/* wIndex */
855 	    1,					/* wLength */
856 	    &data, 0,
857 	    &completion_reason, &cb_flags, 0);
858 	mutex_enter(&scsa2usbp->scsa2usb_mutex);
859 
860 	if (rval != USB_SUCCESS) {
861 		USB_DPRINTF_L2(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
862 		    "get max lun failed, rval=%d cr=%d cb=0x%x data=0x%p",
863 		    rval, completion_reason, cb_flags, (void *)data);
864 	} else {
865 		/*
866 		 * This check ensures that we have valid data returned back.
867 		 * Otherwise we assume that device supports only one LUN.
868 		 */
869 		if (MBLKL(data) != 1) {
870 			USB_DPRINTF_L2(DPRINT_MASK_SCSA,
871 			    scsa2usbp->scsa2usb_log_handle,
872 			    "device reported incorrect luns (adjusting to 1)");
873 		} else {
874 			/*
875 			 * Set scsa2usb_n_luns to value returned by the device
876 			 * plus 1. (See Section 3.2)
877 			 */
878 			luns = *data->b_rptr + 1;
879 
880 			/*
881 			 * In case a device returns incorrect LUNs
882 			 * which are more than 15 or negative or 0;
883 			 * we assume 1.
884 			 */
885 			if ((luns >= SCSA2USB_MAX_LUNS) || (luns <= 0)) {
886 				USB_DPRINTF_L2(DPRINT_MASK_SCSA,
887 				    scsa2usbp->scsa2usb_log_handle,
888 				    "device reported %d luns "
889 				    "(adjusting to 1)", luns);
890 				luns = 1;
891 			}
892 		}
893 	}
894 
895 	SCSA2USB_FREE_MSG(data);	/* Free data */
896 
897 	return (luns);
898 }
899