10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*6898Sfb209375 * Common Development and Distribution License (the "License").
6*6898Sfb209375 * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate *
21*6898Sfb209375 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
220Sstevel@tonic-gate * Use is subject to license terms.
230Sstevel@tonic-gate */
240Sstevel@tonic-gate
250Sstevel@tonic-gate
260Sstevel@tonic-gate /*
270Sstevel@tonic-gate * scsa2usb_ms_cbi.c:
280Sstevel@tonic-gate *
290Sstevel@tonic-gate * This file implements USB Mass Storage Class
300Sstevel@tonic-gate * Control Bulk Interrupt (CB/CBI) transport v1.0
310Sstevel@tonic-gate * http://www.usb.org/developers/data/devclass/usbmass-cbi10.pdf
320Sstevel@tonic-gate */
330Sstevel@tonic-gate #include <sys/usb/usba/usbai_version.h>
340Sstevel@tonic-gate #include <sys/scsi/scsi.h>
350Sstevel@tonic-gate #include <sys/callb.h> /* needed by scsa2usb.h */
360Sstevel@tonic-gate #include <sys/strsubr.h>
370Sstevel@tonic-gate
380Sstevel@tonic-gate #include <sys/usb/usba.h>
390Sstevel@tonic-gate #include <sys/usb/usba/usba_private.h>
400Sstevel@tonic-gate #include <sys/usb/usba/usba_ugen.h>
410Sstevel@tonic-gate
420Sstevel@tonic-gate #include <sys/usb/clients/mass_storage/usb_cbi.h>
430Sstevel@tonic-gate #include <sys/usb/scsa2usb/scsa2usb.h>
440Sstevel@tonic-gate
450Sstevel@tonic-gate /*
460Sstevel@tonic-gate * Function Prototypes
470Sstevel@tonic-gate */
480Sstevel@tonic-gate int scsa2usb_cbi_transport(scsa2usb_state_t *, scsa2usb_cmd_t *);
490Sstevel@tonic-gate static int scsa2usb_handle_cbi_status(usb_intr_req_t *);
500Sstevel@tonic-gate static void scsa2usb_cbi_reset_recovery(scsa2usb_state_t *);
510Sstevel@tonic-gate static void scsa2usb_cbi_handle_error(scsa2usb_state_t *, int, usb_cr_t);
520Sstevel@tonic-gate static usb_intr_req_t *scsa2usb_cbi_start_intr_polling(scsa2usb_state_t *);
530Sstevel@tonic-gate void scsa2usb_cbi_stop_intr_polling(scsa2usb_state_t *);
540Sstevel@tonic-gate
550Sstevel@tonic-gate /* extern functions */
560Sstevel@tonic-gate extern void scsa2usb_setup_next_xfer(scsa2usb_state_t *, scsa2usb_cmd_t *);
570Sstevel@tonic-gate extern int scsa2usb_handle_data_start(scsa2usb_state_t *,
580Sstevel@tonic-gate scsa2usb_cmd_t *, usb_bulk_req_t *);
590Sstevel@tonic-gate extern void scsa2usb_handle_data_done(scsa2usb_state_t *, scsa2usb_cmd_t *,
600Sstevel@tonic-gate usb_bulk_req_t *);
610Sstevel@tonic-gate extern usb_bulk_req_t *scsa2usb_init_bulk_req(scsa2usb_state_t *,
620Sstevel@tonic-gate size_t, uint_t, usb_req_attrs_t, usb_flags_t);
630Sstevel@tonic-gate extern int scsa2usb_clear_ept_stall(scsa2usb_state_t *, uint_t,
640Sstevel@tonic-gate usb_pipe_handle_t, char *);
650Sstevel@tonic-gate extern void scsa2usb_close_usb_pipes(scsa2usb_state_t *);
660Sstevel@tonic-gate
670Sstevel@tonic-gate #ifdef DEBUG /* debugging information */
680Sstevel@tonic-gate extern void scsa2usb_print_cdb(scsa2usb_state_t *, scsa2usb_cmd_t *);
690Sstevel@tonic-gate #endif /* DEBUG */
700Sstevel@tonic-gate
710Sstevel@tonic-gate
720Sstevel@tonic-gate /*
730Sstevel@tonic-gate * scsa2usb_cbi_transport:
740Sstevel@tonic-gate * Implements the CB/CBI state machine by these steps:
750Sstevel@tonic-gate * a) Issues command to the device over control pipe.
760Sstevel@tonic-gate * b) Start Data Phase if applicable
770Sstevel@tonic-gate * c) Start Status Phase
780Sstevel@tonic-gate *
790Sstevel@tonic-gate * returns TRAN_* values and not USB_SUCCESS/FAILURE
800Sstevel@tonic-gate *
810Sstevel@tonic-gate * scsa2usb_cbi_transport() handles the normal transitions or
820Sstevel@tonic-gate * continuation after clearing stalls or error recovery.
830Sstevel@tonic-gate *
840Sstevel@tonic-gate * Command Phase:
850Sstevel@tonic-gate * prepare a valid command and transport it on default pipe
860Sstevel@tonic-gate * if error on default-pipe:
870Sstevel@tonic-gate * set pkt_reason to CMD_TRAN_ERR
880Sstevel@tonic-gate * new pkt state is SCSA2USB_PKT_DO_COMP
890Sstevel@tonic-gate * do reset recovery synchronously
900Sstevel@tonic-gate * else
910Sstevel@tonic-gate * proceed to data phase
920Sstevel@tonic-gate *
930Sstevel@tonic-gate * Data Phase:
940Sstevel@tonic-gate * if data in:
950Sstevel@tonic-gate * setup data in on bulkin
960Sstevel@tonic-gate * else if data out:
970Sstevel@tonic-gate * setup data out on bulkout
980Sstevel@tonic-gate *
990Sstevel@tonic-gate * data: (in)
1000Sstevel@tonic-gate * copy data transferred so far, no more data to transfer
1010Sstevel@tonic-gate *
1020Sstevel@tonic-gate * if stall on bulkin pipe
1030Sstevel@tonic-gate * terminate data transfers, set cmd_done
1040Sstevel@tonic-gate * clear stall on bulkin syncrhonously
1050Sstevel@tonic-gate * else if other exception
1060Sstevel@tonic-gate * set pkt_reason to CMD_TRAN_ERR
1070Sstevel@tonic-gate * new pkt state is SCSA2USB_PKT_DO_COMP
1080Sstevel@tonic-gate * do reset recovery synchronously
1090Sstevel@tonic-gate * else (no error)
1100Sstevel@tonic-gate * receive status
1110Sstevel@tonic-gate *
1120Sstevel@tonic-gate * data: (out)
1130Sstevel@tonic-gate * if stall on bulkout pipe
1140Sstevel@tonic-gate * terminate data transfers
1150Sstevel@tonic-gate * clear stall on bulkout synchronously USBA
1160Sstevel@tonic-gate * else if other exception
1170Sstevel@tonic-gate * set pkt_reason to CMD_TRAN_ERR
1180Sstevel@tonic-gate * new pkt state is SCSA2USB_PKT_DO_COMP
1190Sstevel@tonic-gate * do reset recovery synchronously
1200Sstevel@tonic-gate * else (no error)
1210Sstevel@tonic-gate * receive status
1220Sstevel@tonic-gate *
1230Sstevel@tonic-gate * Status Phase: (on Interrupt pipe for CBI devices only)
1240Sstevel@tonic-gate * if error
1250Sstevel@tonic-gate * if stall
1260Sstevel@tonic-gate * new pkt state is SCSA2USB_PKT_DO_COMP
1270Sstevel@tonic-gate * clear stall on interrupt pipe
1280Sstevel@tonic-gate * else
1290Sstevel@tonic-gate * set pkt_reason to CMD_TRAN_ERR
1300Sstevel@tonic-gate * new pkt state is SCSA2USB_PKT_DO_COMP
1310Sstevel@tonic-gate * do reset recovery synchronously
1320Sstevel@tonic-gate * else (no error)
1330Sstevel@tonic-gate * goto read status
1340Sstevel@tonic-gate *
1350Sstevel@tonic-gate * read status:
1360Sstevel@tonic-gate * if not OK or phase error
1370Sstevel@tonic-gate * new pkt state is SCSA2USB_PKT_DO_COMP
1380Sstevel@tonic-gate * set pkt reason CMD_TRAN_ERR
1390Sstevel@tonic-gate * reset recovery synchronously
1400Sstevel@tonic-gate * else (status ok)
1410Sstevel@tonic-gate * goto SCSA2USB_PKT_DO_COMP
1420Sstevel@tonic-gate *
1430Sstevel@tonic-gate * The reset recovery walks sequentially thru device reset, clearing
1440Sstevel@tonic-gate * stalls and pipe resets. When the reset recovery completes we return
1450Sstevel@tonic-gate * to the taskq thread.
1460Sstevel@tonic-gate *
1470Sstevel@tonic-gate * Clearing stalls clears the stall condition, resets the pipe, and
1480Sstevel@tonic-gate * then returns to the transport.
1490Sstevel@tonic-gate */
1500Sstevel@tonic-gate int
scsa2usb_cbi_transport(scsa2usb_state_t * scsa2usbp,scsa2usb_cmd_t * cmd)1510Sstevel@tonic-gate scsa2usb_cbi_transport(scsa2usb_state_t *scsa2usbp, scsa2usb_cmd_t *cmd)
1520Sstevel@tonic-gate {
1530Sstevel@tonic-gate int i, rval = TRAN_ACCEPT;
1540Sstevel@tonic-gate mblk_t *data;
1550Sstevel@tonic-gate usb_cr_t completion_reason;
1560Sstevel@tonic-gate usb_cb_flags_t cb_flags;
1570Sstevel@tonic-gate usb_bulk_req_t *data_req;
1580Sstevel@tonic-gate usb_intr_req_t *status_req;
1590Sstevel@tonic-gate
1600Sstevel@tonic-gate USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
161*6898Sfb209375 "scsa2usb_cbi_transport: cmd = 0x%p", (void *)cmd);
1620Sstevel@tonic-gate ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
1630Sstevel@tonic-gate
1640Sstevel@tonic-gate Cmd_Phase:
1650Sstevel@tonic-gate if (!(SCSA2USB_DEVICE_ACCESS_OK(scsa2usbp))) {
1660Sstevel@tonic-gate
1670Sstevel@tonic-gate return (TRAN_FATAL_ERROR);
1680Sstevel@tonic-gate }
1690Sstevel@tonic-gate
1700Sstevel@tonic-gate /*
1710Sstevel@tonic-gate * Start command phase (C - in CBI)
1720Sstevel@tonic-gate */
1730Sstevel@tonic-gate data = allocb_wait(CBI_CLASS_CMD_LEN, BPRI_LO, STR_NOSIG, NULL);
1740Sstevel@tonic-gate
1750Sstevel@tonic-gate /* Initialize the data */
1760Sstevel@tonic-gate for (i = 0; i < CBI_CLASS_CMD_LEN; i++) {
1770Sstevel@tonic-gate *data->b_wptr++ = cmd->cmd_cdb[i];
1780Sstevel@tonic-gate }
1790Sstevel@tonic-gate
1800Sstevel@tonic-gate SCSA2USB_PRINT_CDB(scsa2usbp, cmd); /* print the CDB */
1810Sstevel@tonic-gate
1820Sstevel@tonic-gate /* Send the Command to the device */
1830Sstevel@tonic-gate mutex_exit(&scsa2usbp->scsa2usb_mutex);
1840Sstevel@tonic-gate rval = usb_pipe_sync_ctrl_xfer(scsa2usbp->scsa2usb_dip,
1850Sstevel@tonic-gate scsa2usbp->scsa2usb_default_pipe,
1860Sstevel@tonic-gate CBI_REQUEST_TYPE, /* bmRequestType */
1870Sstevel@tonic-gate 0, /* bRequest */
1880Sstevel@tonic-gate CBI_WVALUE, /* wValue */
1890Sstevel@tonic-gate scsa2usbp->scsa2usb_intfc_num, /* wIndex */
1900Sstevel@tonic-gate CBI_CLASS_CMD_LEN, /* wLength */
1910Sstevel@tonic-gate &data, /* data */
1920Sstevel@tonic-gate USB_ATTRS_PIPE_RESET, /* attributes */
1930Sstevel@tonic-gate &completion_reason, &cb_flags, USB_FLAGS_SLEEP);
1940Sstevel@tonic-gate mutex_enter(&scsa2usbp->scsa2usb_mutex);
1950Sstevel@tonic-gate
1960Sstevel@tonic-gate USB_DPRINTF_L2(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
1970Sstevel@tonic-gate "scsa2usb_cbi_transport: sent cmd = 0x%x rval = %d",
1980Sstevel@tonic-gate cmd->cmd_cdb[SCSA2USB_OPCODE], rval);
1990Sstevel@tonic-gate
2000Sstevel@tonic-gate SCSA2USB_FREE_MSG(data); /* get rid of the data */
2010Sstevel@tonic-gate if (rval != USB_SUCCESS) {
2020Sstevel@tonic-gate scsa2usb_cbi_handle_error(scsa2usbp, rval, completion_reason);
2030Sstevel@tonic-gate
2040Sstevel@tonic-gate return (TRAN_FATAL_ERROR);
2050Sstevel@tonic-gate }
2060Sstevel@tonic-gate
2070Sstevel@tonic-gate /*
2080Sstevel@tonic-gate * Xferred command to the device.
2090Sstevel@tonic-gate * Start data phase (B - in CBI)
2100Sstevel@tonic-gate */
2110Sstevel@tonic-gate
2120Sstevel@tonic-gate /*
2130Sstevel@tonic-gate * we've not transferred any data yet; updated in
2140Sstevel@tonic-gate * scsa2usb_handle_data_done
2150Sstevel@tonic-gate */
2160Sstevel@tonic-gate cmd->cmd_resid_xfercount = 0;
2170Sstevel@tonic-gate
2180Sstevel@tonic-gate /* if data to be xferred ? */
2190Sstevel@tonic-gate if (cmd->cmd_xfercount) {
2200Sstevel@tonic-gate
2210Sstevel@tonic-gate /* Initialize a bulk_req_t */
2220Sstevel@tonic-gate data_req = scsa2usb_init_bulk_req(scsa2usbp, 0,
2230Sstevel@tonic-gate cmd->cmd_timeout, USB_ATTRS_PIPE_RESET, USB_FLAGS_SLEEP);
2240Sstevel@tonic-gate
2250Sstevel@tonic-gate /* start I/O to/from the device */
2260Sstevel@tonic-gate rval = scsa2usb_handle_data_start(scsa2usbp, cmd,
227*6898Sfb209375 data_req);
2280Sstevel@tonic-gate /* handle data returned */
2290Sstevel@tonic-gate scsa2usb_handle_data_done(scsa2usbp, cmd,
230*6898Sfb209375 data_req);
2310Sstevel@tonic-gate if (rval != USB_SUCCESS) {
2320Sstevel@tonic-gate /*
2330Sstevel@tonic-gate * we ran into an error and it wasn't a STALL
2340Sstevel@tonic-gate */
2350Sstevel@tonic-gate if (data_req->bulk_completion_reason == USB_CR_STALL) {
2360Sstevel@tonic-gate if (scsa2usbp->scsa2usb_cur_pkt) {
2370Sstevel@tonic-gate scsa2usbp->scsa2usb_cur_pkt->
238*6898Sfb209375 pkt_reason = CMD_TRAN_ERR;
2390Sstevel@tonic-gate }
2400Sstevel@tonic-gate } else {
2410Sstevel@tonic-gate scsa2usb_cbi_handle_error(scsa2usbp,
2420Sstevel@tonic-gate rval, data_req->bulk_completion_reason);
2430Sstevel@tonic-gate
2440Sstevel@tonic-gate /* get rid of req */
2450Sstevel@tonic-gate SCSA2USB_FREE_BULK_REQ(data_req);
2460Sstevel@tonic-gate
2470Sstevel@tonic-gate return (TRAN_FATAL_ERROR);
2480Sstevel@tonic-gate }
2490Sstevel@tonic-gate }
2500Sstevel@tonic-gate
2510Sstevel@tonic-gate SCSA2USB_FREE_BULK_REQ(data_req); /* get rid of bulk_req */
2520Sstevel@tonic-gate }
2530Sstevel@tonic-gate
2540Sstevel@tonic-gate /* CB devices don't do status over interrupt pipe */
2550Sstevel@tonic-gate if (SCSA2USB_IS_CB(scsa2usbp)) {
2560Sstevel@tonic-gate USB_DPRINTF_L2(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
2570Sstevel@tonic-gate "scsa2usb_cbi_transport: CB done rval = %d", rval);
2580Sstevel@tonic-gate goto end_it;
2590Sstevel@tonic-gate }
2600Sstevel@tonic-gate
2610Sstevel@tonic-gate /*
2620Sstevel@tonic-gate * Start status phase (I - in CBI)
2630Sstevel@tonic-gate */
2640Sstevel@tonic-gate
2650Sstevel@tonic-gate /* Get Status over interrupt pipe */
2660Sstevel@tonic-gate if ((status_req = scsa2usb_cbi_start_intr_polling(scsa2usbp)) == NULL) {
2670Sstevel@tonic-gate
2680Sstevel@tonic-gate return (TRAN_FATAL_ERROR); /* lack of better return code */
2690Sstevel@tonic-gate }
2700Sstevel@tonic-gate
2710Sstevel@tonic-gate rval = scsa2usb_handle_cbi_status(status_req);
2720Sstevel@tonic-gate
2730Sstevel@tonic-gate usb_free_intr_req(status_req);
2740Sstevel@tonic-gate
2750Sstevel@tonic-gate /* stop interrupt pipe polling (CBI only) */
2760Sstevel@tonic-gate if (SCSA2USB_IS_CBI(scsa2usbp)) {
2770Sstevel@tonic-gate scsa2usb_cbi_stop_intr_polling(scsa2usbp);
2780Sstevel@tonic-gate }
2790Sstevel@tonic-gate
2800Sstevel@tonic-gate end_it:
2810Sstevel@tonic-gate if ((rval == USB_SUCCESS) && /* CSW was ok */
2820Sstevel@tonic-gate (scsa2usbp->scsa2usb_cur_pkt->pkt_reason == CMD_CMPLT) &&
2830Sstevel@tonic-gate (cmd->cmd_xfercount != 0) && /* more data to xfer */
2840Sstevel@tonic-gate !cmd->cmd_done) { /* we aren't done yet */
2850Sstevel@tonic-gate scsa2usb_setup_next_xfer(scsa2usbp, cmd);
2860Sstevel@tonic-gate goto Cmd_Phase;
2870Sstevel@tonic-gate } else {
2880Sstevel@tonic-gate if (SCSA2USB_IS_CB(scsa2usbp)) {
2890Sstevel@tonic-gate cmd->cmd_done = 1;
2900Sstevel@tonic-gate SCSA2USB_SET_PKT_DO_COMP_STATE(scsa2usbp);
2910Sstevel@tonic-gate }
2920Sstevel@tonic-gate }
2930Sstevel@tonic-gate
2940Sstevel@tonic-gate return (rval == USB_SUCCESS ? TRAN_ACCEPT : TRAN_FATAL_ERROR);
2950Sstevel@tonic-gate }
2960Sstevel@tonic-gate
2970Sstevel@tonic-gate
2980Sstevel@tonic-gate /*
2990Sstevel@tonic-gate * scsa2usb_cbi_handle_error:
3000Sstevel@tonic-gate * handle errors from transport that are not STALL conditions
3010Sstevel@tonic-gate */
3020Sstevel@tonic-gate static void
scsa2usb_cbi_handle_error(scsa2usb_state_t * scsa2usbp,int rval,usb_cr_t cr)3030Sstevel@tonic-gate scsa2usb_cbi_handle_error(scsa2usb_state_t *scsa2usbp, int rval, usb_cr_t cr)
3040Sstevel@tonic-gate {
3050Sstevel@tonic-gate struct scsi_pkt *pkt = scsa2usbp->scsa2usb_cur_pkt;
3060Sstevel@tonic-gate
3070Sstevel@tonic-gate USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
3080Sstevel@tonic-gate "scsa2usb_cbi_handle_error: data error %d cr = %d", rval, cr);
3090Sstevel@tonic-gate
3100Sstevel@tonic-gate SCSA2USB_SET_PKT_DO_COMP_STATE(scsa2usbp);
3110Sstevel@tonic-gate
3120Sstevel@tonic-gate /* do reset error recovery */
3130Sstevel@tonic-gate switch (cr) {
3140Sstevel@tonic-gate case USB_CR_STALL:
3150Sstevel@tonic-gate if (pkt) {
3160Sstevel@tonic-gate pkt->pkt_reason = CMD_TRAN_ERR;
3170Sstevel@tonic-gate *(pkt->pkt_scbp) = STATUS_CHECK;
3180Sstevel@tonic-gate }
3190Sstevel@tonic-gate break;
3200Sstevel@tonic-gate case USB_CR_TIMEOUT:
3210Sstevel@tonic-gate if (pkt) {
3220Sstevel@tonic-gate pkt->pkt_reason = CMD_TIMEOUT;
3230Sstevel@tonic-gate pkt->pkt_statistics |= STAT_TIMEOUT;
3240Sstevel@tonic-gate }
3250Sstevel@tonic-gate break;
3260Sstevel@tonic-gate case USB_CR_DEV_NOT_RESP:
3270Sstevel@tonic-gate scsa2usb_cbi_stop_intr_polling(scsa2usbp);
3280Sstevel@tonic-gate if (pkt) {
3290Sstevel@tonic-gate pkt->pkt_reason = CMD_DEV_GONE;
3300Sstevel@tonic-gate /* scsi_poll relies on this */
3310Sstevel@tonic-gate pkt->pkt_state = STATE_GOT_BUS;
3320Sstevel@tonic-gate }
3330Sstevel@tonic-gate break;
3340Sstevel@tonic-gate default:
3350Sstevel@tonic-gate if (pkt) {
3360Sstevel@tonic-gate pkt->pkt_reason = CMD_TRAN_ERR;
3370Sstevel@tonic-gate }
3380Sstevel@tonic-gate scsa2usb_cbi_reset_recovery(scsa2usbp);
3390Sstevel@tonic-gate }
3400Sstevel@tonic-gate }
3410Sstevel@tonic-gate
3420Sstevel@tonic-gate
3430Sstevel@tonic-gate /*
3440Sstevel@tonic-gate * scsa2usb_cbi_start_intr_polling:
3450Sstevel@tonic-gate * start polling asynchronously without notification
3460Sstevel@tonic-gate */
3470Sstevel@tonic-gate static usb_intr_req_t *
scsa2usb_cbi_start_intr_polling(scsa2usb_state_t * scsa2usbp)3480Sstevel@tonic-gate scsa2usb_cbi_start_intr_polling(scsa2usb_state_t *scsa2usbp)
3490Sstevel@tonic-gate {
3500Sstevel@tonic-gate int rval;
3510Sstevel@tonic-gate usb_pipe_state_t pipe_state;
3520Sstevel@tonic-gate usb_intr_req_t *req = NULL;
3530Sstevel@tonic-gate
3540Sstevel@tonic-gate USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
3550Sstevel@tonic-gate "scsa2usb_cbi_start_intr_polling:");
3560Sstevel@tonic-gate
3570Sstevel@tonic-gate if (!SCSA2USB_IS_CBI(scsa2usbp)) {
3580Sstevel@tonic-gate
3590Sstevel@tonic-gate return (NULL);
3600Sstevel@tonic-gate }
3610Sstevel@tonic-gate
3620Sstevel@tonic-gate req = usb_alloc_intr_req(scsa2usbp->scsa2usb_dip, 0, USB_FLAGS_SLEEP);
3630Sstevel@tonic-gate req->intr_client_private = (usb_opaque_t)scsa2usbp;
3640Sstevel@tonic-gate req->intr_attributes = USB_ATTRS_ONE_XFER | USB_ATTRS_PIPE_RESET |
365*6898Sfb209375 USB_ATTRS_SHORT_XFER_OK;
3660Sstevel@tonic-gate req->intr_len = scsa2usbp->scsa2usb_intr_ept.wMaxPacketSize;
3670Sstevel@tonic-gate req->intr_timeout = 20; /* arbitrary large for now */
3680Sstevel@tonic-gate mutex_exit(&scsa2usbp->scsa2usb_mutex);
3690Sstevel@tonic-gate
3700Sstevel@tonic-gate if ((rval = usb_pipe_intr_xfer(scsa2usbp->scsa2usb_intr_pipe, req,
3710Sstevel@tonic-gate USB_FLAGS_SLEEP)) != USB_SUCCESS) {
3720Sstevel@tonic-gate mutex_enter(&scsa2usbp->scsa2usb_mutex);
3730Sstevel@tonic-gate USB_DPRINTF_L2(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
3740Sstevel@tonic-gate "polling failed rval: %d", rval);
3750Sstevel@tonic-gate
3760Sstevel@tonic-gate /* clear stall */
3770Sstevel@tonic-gate if (req->intr_completion_reason == USB_CR_STALL) {
3780Sstevel@tonic-gate (void) scsa2usb_clear_ept_stall(scsa2usbp,
3790Sstevel@tonic-gate scsa2usbp->scsa2usb_intr_ept.bEndpointAddress,
3800Sstevel@tonic-gate scsa2usbp->scsa2usb_intr_pipe, "intr");
3810Sstevel@tonic-gate }
3820Sstevel@tonic-gate
3830Sstevel@tonic-gate /* handle other errors here */
3840Sstevel@tonic-gate scsa2usb_cbi_handle_error(scsa2usbp, rval,
3850Sstevel@tonic-gate req->intr_completion_reason);
3860Sstevel@tonic-gate mutex_exit(&scsa2usbp->scsa2usb_mutex);
3870Sstevel@tonic-gate
3880Sstevel@tonic-gate usb_free_intr_req(req);
3890Sstevel@tonic-gate req = NULL;
3900Sstevel@tonic-gate }
3910Sstevel@tonic-gate
3920Sstevel@tonic-gate rval = usb_pipe_get_state(scsa2usbp->scsa2usb_intr_pipe,
3930Sstevel@tonic-gate &pipe_state, USB_FLAGS_SLEEP);
3940Sstevel@tonic-gate if (pipe_state != USB_PIPE_STATE_ACTIVE) {
3950Sstevel@tonic-gate USB_DPRINTF_L2(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
3960Sstevel@tonic-gate "intr pipes state: %d, rval: %d", pipe_state, rval);
3970Sstevel@tonic-gate }
3980Sstevel@tonic-gate mutex_enter(&scsa2usbp->scsa2usb_mutex);
3990Sstevel@tonic-gate
4000Sstevel@tonic-gate return (req);
4010Sstevel@tonic-gate }
4020Sstevel@tonic-gate
4030Sstevel@tonic-gate
4040Sstevel@tonic-gate /*
4050Sstevel@tonic-gate * scsa2usb_cbi_stop_intr_polling:
4060Sstevel@tonic-gate * Stop polling on interrupt pipe (for status)
4070Sstevel@tonic-gate */
4080Sstevel@tonic-gate void
scsa2usb_cbi_stop_intr_polling(scsa2usb_state_t * scsa2usbp)4090Sstevel@tonic-gate scsa2usb_cbi_stop_intr_polling(scsa2usb_state_t *scsa2usbp)
4100Sstevel@tonic-gate {
4110Sstevel@tonic-gate USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
4120Sstevel@tonic-gate "scsa2usb_cbi_stop_intr_polling:");
4130Sstevel@tonic-gate
4140Sstevel@tonic-gate ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
4150Sstevel@tonic-gate
4160Sstevel@tonic-gate if (!SCSA2USB_IS_CBI(scsa2usbp)) {
4170Sstevel@tonic-gate
4180Sstevel@tonic-gate return;
4190Sstevel@tonic-gate }
4200Sstevel@tonic-gate
4210Sstevel@tonic-gate if (scsa2usbp->scsa2usb_intr_pipe) {
4220Sstevel@tonic-gate mutex_exit(&scsa2usbp->scsa2usb_mutex);
4230Sstevel@tonic-gate usb_pipe_stop_intr_polling(scsa2usbp->scsa2usb_intr_pipe,
4240Sstevel@tonic-gate USB_FLAGS_SLEEP);
4250Sstevel@tonic-gate mutex_enter(&scsa2usbp->scsa2usb_mutex);
4260Sstevel@tonic-gate }
4270Sstevel@tonic-gate }
4280Sstevel@tonic-gate
4290Sstevel@tonic-gate
4300Sstevel@tonic-gate /*
4310Sstevel@tonic-gate * scsa2usb_handle_cbi_status:
4320Sstevel@tonic-gate * Handle CBI status results
4330Sstevel@tonic-gate */
4340Sstevel@tonic-gate static int
scsa2usb_handle_cbi_status(usb_intr_req_t * req)4350Sstevel@tonic-gate scsa2usb_handle_cbi_status(usb_intr_req_t *req)
4360Sstevel@tonic-gate {
4370Sstevel@tonic-gate int rval = USB_SUCCESS;
4380Sstevel@tonic-gate int status;
4390Sstevel@tonic-gate char *msg;
4400Sstevel@tonic-gate scsa2usb_cmd_t *cmd;
4410Sstevel@tonic-gate scsa2usb_state_t *scsa2usbp = (scsa2usb_state_t *)
442*6898Sfb209375 req->intr_client_private;
4430Sstevel@tonic-gate
4440Sstevel@tonic-gate USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
445*6898Sfb209375 "scsa2usb_handle_cbi_status: req: 0x%p", (void *)req);
4460Sstevel@tonic-gate
4470Sstevel@tonic-gate ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
4480Sstevel@tonic-gate ASSERT(req->intr_data != NULL);
4490Sstevel@tonic-gate
4500Sstevel@tonic-gate cmd = PKT2CMD(scsa2usbp->scsa2usb_cur_pkt);
4510Sstevel@tonic-gate status = *(req->intr_data->b_rptr + 1) & CBI_STATUS_MASK;
4520Sstevel@tonic-gate
4530Sstevel@tonic-gate /*
4540Sstevel@tonic-gate * CBI status contains ASC and ASCQ.
4550Sstevel@tonic-gate * SCMD_REQUEST_SENSE and SCMD_INQUIRY don't affect the sense data
4560Sstevel@tonic-gate * on CBI devices. So, we can ignore that info for these 2 commands.
4570Sstevel@tonic-gate *
4580Sstevel@tonic-gate * (See details in UFI spec section 3.5 - that says that INQUIRY,
4590Sstevel@tonic-gate * SEND_DIAG, and REQUEST_SENSE ought to be supported by any deivce
4600Sstevel@tonic-gate * irrespective).
4610Sstevel@tonic-gate */
4620Sstevel@tonic-gate if ((cmd->cmd_cdb[SCSA2USB_OPCODE] == SCMD_REQUEST_SENSE) ||
4630Sstevel@tonic-gate (cmd->cmd_cdb[SCSA2USB_OPCODE] == SCMD_INQUIRY)) {
4640Sstevel@tonic-gate USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
4650Sstevel@tonic-gate "scsa2usb_handle_cbi_status: CBI STATUS = (0x%x, 0x%x)",
4660Sstevel@tonic-gate *(req->intr_data->b_rptr), *(req->intr_data->b_rptr+1));
4670Sstevel@tonic-gate
4680Sstevel@tonic-gate SCSA2USB_SET_PKT_DO_COMP_STATE(scsa2usbp);
4690Sstevel@tonic-gate
4700Sstevel@tonic-gate return (USB_SUCCESS);
4710Sstevel@tonic-gate }
4720Sstevel@tonic-gate
4730Sstevel@tonic-gate switch (status) {
4740Sstevel@tonic-gate case CBI_STATUS_PASS:
4750Sstevel@tonic-gate msg = "PASSED";
4760Sstevel@tonic-gate /* non-zero command completion interrupt */
4770Sstevel@tonic-gate if (*(req->intr_data->b_rptr)) {
4780Sstevel@tonic-gate *(scsa2usbp->scsa2usb_cur_pkt->pkt_scbp) = STATUS_CHECK;
4790Sstevel@tonic-gate cmd->cmd_done = 1;
4800Sstevel@tonic-gate }
4810Sstevel@tonic-gate break;
4820Sstevel@tonic-gate case CBI_STATUS_FAILED:
4830Sstevel@tonic-gate case CBI_STATUS_PERSISTENT_FAIL:
4840Sstevel@tonic-gate msg = (status == CBI_STATUS_PERSISTENT_FAIL) ?
485*6898Sfb209375 "PERSISTENT_FAILURE" : "FAILED";
4860Sstevel@tonic-gate *(scsa2usbp->scsa2usb_cur_pkt->pkt_scbp) = STATUS_CHECK;
4870Sstevel@tonic-gate cmd->cmd_done = 1;
4880Sstevel@tonic-gate break;
4890Sstevel@tonic-gate case CBI_STATUS_PHASE_ERR:
4900Sstevel@tonic-gate msg = "PHASE_ERR";
4910Sstevel@tonic-gate scsa2usb_cbi_reset_recovery(scsa2usbp);
4920Sstevel@tonic-gate rval = USB_FAILURE;
4930Sstevel@tonic-gate break;
4940Sstevel@tonic-gate }
4950Sstevel@tonic-gate
4960Sstevel@tonic-gate USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
4970Sstevel@tonic-gate "CBI STATUS = 0x%x %s (0x%x, 0x%x)", status, msg,
4980Sstevel@tonic-gate *(req->intr_data->b_rptr), *(req->intr_data->b_rptr+1));
4990Sstevel@tonic-gate
5000Sstevel@tonic-gate /* we are done and ready to callback */
5010Sstevel@tonic-gate SCSA2USB_SET_PKT_DO_COMP_STATE(scsa2usbp);
5020Sstevel@tonic-gate
5030Sstevel@tonic-gate return (rval);
5040Sstevel@tonic-gate }
5050Sstevel@tonic-gate
5060Sstevel@tonic-gate
5070Sstevel@tonic-gate /*
5080Sstevel@tonic-gate * scsa2usb_cbi_reset_recovery:
5090Sstevel@tonic-gate * Reset the USB device in case of errors.
5100Sstevel@tonic-gate */
5110Sstevel@tonic-gate static void
scsa2usb_cbi_reset_recovery(scsa2usb_state_t * scsa2usbp)5120Sstevel@tonic-gate scsa2usb_cbi_reset_recovery(scsa2usb_state_t *scsa2usbp)
5130Sstevel@tonic-gate {
5140Sstevel@tonic-gate int i, rval;
5150Sstevel@tonic-gate mblk_t *cdb;
5160Sstevel@tonic-gate usb_cr_t completion_reason;
5170Sstevel@tonic-gate usb_cb_flags_t cb_flags;
5180Sstevel@tonic-gate
5190Sstevel@tonic-gate USB_DPRINTF_L4(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
520*6898Sfb209375 "scsa2usb_cbi_reset_recovery: (0x%p)", (void *)scsa2usbp);
5210Sstevel@tonic-gate
5220Sstevel@tonic-gate ASSERT(mutex_owned(&scsa2usbp->scsa2usb_mutex));
5230Sstevel@tonic-gate
5240Sstevel@tonic-gate if (!(SCSA2USB_DEVICE_ACCESS_OK(scsa2usbp))) {
5250Sstevel@tonic-gate
5260Sstevel@tonic-gate return;
5270Sstevel@tonic-gate }
5280Sstevel@tonic-gate
5290Sstevel@tonic-gate if (scsa2usbp->scsa2usb_cur_pkt) {
5300Sstevel@tonic-gate scsa2usbp->scsa2usb_cur_pkt->pkt_statistics |= STAT_DEV_RESET;
5310Sstevel@tonic-gate }
5320Sstevel@tonic-gate
5330Sstevel@tonic-gate /* Allocate an mblk for CBR */
5340Sstevel@tonic-gate cdb = allocb_wait(CBI_CLASS_CMD_LEN, BPRI_LO, STR_NOSIG, NULL);
5350Sstevel@tonic-gate
5360Sstevel@tonic-gate *cdb->b_wptr++ = SCMD_SDIAG; /* Set it to DIAG */
5370Sstevel@tonic-gate *cdb->b_wptr++ = CBI_SELF_TEST; /* Set it to reset */
5380Sstevel@tonic-gate for (i = 2; i < CBI_CLASS_CMD_LEN; i++) {
5390Sstevel@tonic-gate *cdb->b_wptr++ = CBI_CBR_VALUE; /* Set it to 0xff */
5400Sstevel@tonic-gate }
5410Sstevel@tonic-gate
5420Sstevel@tonic-gate scsa2usbp->scsa2usb_pipe_state = SCSA2USB_PIPE_DEV_RESET;
5430Sstevel@tonic-gate
5440Sstevel@tonic-gate /*
5450Sstevel@tonic-gate * Send a Reset request to the device
5460Sstevel@tonic-gate */
5470Sstevel@tonic-gate mutex_exit(&scsa2usbp->scsa2usb_mutex);
5480Sstevel@tonic-gate rval = usb_pipe_sync_ctrl_xfer(scsa2usbp->scsa2usb_dip,
5490Sstevel@tonic-gate scsa2usbp->scsa2usb_default_pipe,
5500Sstevel@tonic-gate CBI_REQUEST_TYPE, /* bmRequestType */
5510Sstevel@tonic-gate 0, /* bRequest */
5520Sstevel@tonic-gate CBI_WVALUE, /* wValue */
5530Sstevel@tonic-gate scsa2usbp->scsa2usb_intfc_num, /* wIndex address */
5540Sstevel@tonic-gate CBI_CLASS_CMD_LEN, /* wLength */
5550Sstevel@tonic-gate &cdb, /* data to be sent */
5560Sstevel@tonic-gate 0, &completion_reason, &cb_flags, 0);
5570Sstevel@tonic-gate mutex_enter(&scsa2usbp->scsa2usb_mutex);
5580Sstevel@tonic-gate
5590Sstevel@tonic-gate USB_DPRINTF_L3(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
5600Sstevel@tonic-gate "\tCBI RESET: rval = %x cr = %x", rval, completion_reason);
5610Sstevel@tonic-gate if (rval != USB_SUCCESS) {
5620Sstevel@tonic-gate goto exc_exit;
5630Sstevel@tonic-gate }
5640Sstevel@tonic-gate
5650Sstevel@tonic-gate /* reset and clear STALL on bulk-in pipe */
5660Sstevel@tonic-gate rval = scsa2usb_clear_ept_stall(scsa2usbp,
5670Sstevel@tonic-gate scsa2usbp->scsa2usb_bulkin_ept.bEndpointAddress,
5680Sstevel@tonic-gate scsa2usbp->scsa2usb_bulkin_pipe, "bulk-in");
5690Sstevel@tonic-gate USB_DPRINTF_L3(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
5700Sstevel@tonic-gate "\tclear stall on bulk-in pipe: %d", rval);
5710Sstevel@tonic-gate if (rval != USB_SUCCESS) {
5720Sstevel@tonic-gate goto exc_exit;
5730Sstevel@tonic-gate }
5740Sstevel@tonic-gate
5750Sstevel@tonic-gate /* reset and clear STALL on bulk-out pipe */
5760Sstevel@tonic-gate rval = scsa2usb_clear_ept_stall(scsa2usbp,
5770Sstevel@tonic-gate scsa2usbp->scsa2usb_bulkout_ept.bEndpointAddress,
5780Sstevel@tonic-gate scsa2usbp->scsa2usb_bulkout_pipe, "bulk-out");
5790Sstevel@tonic-gate USB_DPRINTF_L3(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
5800Sstevel@tonic-gate "\tclear stall on bulk-out pipe: %d", rval);
5810Sstevel@tonic-gate if (rval != USB_SUCCESS) {
5820Sstevel@tonic-gate goto exc_exit;
5830Sstevel@tonic-gate }
5840Sstevel@tonic-gate
5850Sstevel@tonic-gate /* reset and clear STALL on interrupt pipe */
5860Sstevel@tonic-gate if (SCSA2USB_IS_CBI(scsa2usbp)) {
5870Sstevel@tonic-gate rval = scsa2usb_clear_ept_stall(scsa2usbp,
5880Sstevel@tonic-gate scsa2usbp->scsa2usb_intr_ept.bEndpointAddress,
5890Sstevel@tonic-gate scsa2usbp->scsa2usb_intr_pipe, "intr");
5900Sstevel@tonic-gate
5910Sstevel@tonic-gate USB_DPRINTF_L3(DPRINT_MASK_SCSA, scsa2usbp->scsa2usb_log_handle,
5920Sstevel@tonic-gate "\tclear stall on intr pipe: %d", rval);
5930Sstevel@tonic-gate }
5940Sstevel@tonic-gate
5950Sstevel@tonic-gate exc_exit:
5960Sstevel@tonic-gate SCSA2USB_FREE_MSG(cdb); /* Free the data */
5970Sstevel@tonic-gate scsa2usbp->scsa2usb_pipe_state &= ~SCSA2USB_PIPE_DEV_RESET;
5980Sstevel@tonic-gate }
599