1 // Code for handling usb attached scsi devices.
3 // only usb 2.0 for now.
5 // once we have xhci driver with usb 3.0 support this must
6 // be updated to use usb3 streams so booting from usb3
7 // devices actually works.
10 // Gerd Hoffmann <kraxel@redhat.com>
12 // based on usb-msc.c which is written by:
13 // Kevin O'Connor <kevin@koconnor.net>
15 // This file may be distributed under the terms of the GNU LGPLv3 license.
17 #include "biosvar.h" // GET_GLOBALFLAT
18 #include "block.h" // DTYPE_USB
19 #include "blockcmd.h" // cdb_read
20 #include "config.h" // CONFIG_USB_UAS
21 #include "malloc.h" // free
22 #include "output.h" // dprintf
23 #include "std/disk.h" // DISK_RET_SUCCESS
24 #include "string.h" // memset
25 #include "usb.h" // struct usb_s
26 #include "usb-uas.h" // usb_uas_init
27 #include "util.h" // bootprio_find_usb
29 #define UAS_UI_COMMAND 0x01
30 #define UAS_UI_SENSE 0x03
31 #define UAS_UI_RESPONSE 0x04
32 #define UAS_UI_TASK_MGMT 0x05
33 #define UAS_UI_READ_READY 0x06
34 #define UAS_UI_WRITE_READY 0x07
36 #define UAS_PIPE_ID_COMMAND 0x01
37 #define UAS_PIPE_ID_STATUS 0x02
38 #define UAS_PIPE_ID_DATA_IN 0x03
39 #define UAS_PIPE_ID_DATA_OUT 0x04
45 } PACKED uas_ui_header;
48 u8 prio_taskattr; /* 6:3 priority, 2:0 task attribute */
50 u8 add_cdb_length; /* 7:2 additional adb length (dwords) */
55 } PACKED uas_ui_command;
63 } PACKED uas_ui_sense;
66 u16 add_response_info;
68 } PACKED uas_ui_response;
75 } PACKED uas_ui_task_mgmt;
80 uas_ui_command command;
82 uas_ui_task_mgmt task;
83 uas_ui_response response;
89 struct usb_pipe *command, *status, *data_in, *data_out;
94 uas_cmd_data(struct disk_op_s *op, void *cdbcmd, u16 blocksize)
97 return DISK_RET_EBADTRACK;
99 struct uasdrive_s *drive_gf = container_of(
100 op->drive_gf, struct uasdrive_s, drive);
103 memset(&ui, 0, sizeof(ui));
104 ui.hdr.id = UAS_UI_COMMAND;
106 ui.command.lun[1] = GET_GLOBALFLAT(drive_gf->lun);
107 memcpy(ui.command.cdb, cdbcmd, sizeof(ui.command.cdb));
108 int ret = usb_send_bulk(GET_GLOBALFLAT(drive_gf->command),
109 USB_DIR_OUT, MAKE_FLATPTR(GET_SEG(SS), &ui),
110 sizeof(ui.hdr) + sizeof(ui.command));
112 dprintf(1, "uas: command send fail");
116 memset(&ui, 0xff, sizeof(ui));
117 ret = usb_send_bulk(GET_GLOBALFLAT(drive_gf->status),
118 USB_DIR_IN, MAKE_FLATPTR(GET_SEG(SS), &ui), sizeof(ui));
120 dprintf(1, "uas: status recv fail");
127 case UAS_UI_READ_READY:
128 ret = usb_send_bulk(GET_GLOBALFLAT(drive_gf->data_in),
129 USB_DIR_IN, op->buf_fl, op->count * blocksize);
131 dprintf(1, "uas: data read fail");
135 case UAS_UI_WRITE_READY:
136 ret = usb_send_bulk(GET_GLOBALFLAT(drive_gf->data_out),
137 USB_DIR_OUT, op->buf_fl, op->count * blocksize);
139 dprintf(1, "uas: data write fail");
144 dprintf(1, "uas: unknown status ui id %d", ui.hdr.id);
148 memset(&ui, 0xff, sizeof(ui));
149 ret = usb_send_bulk(GET_GLOBALFLAT(drive_gf->status),
150 USB_DIR_IN, MAKE_FLATPTR(GET_SEG(SS), &ui), sizeof(ui));
152 dprintf(1, "uas: status recv fail");
155 if (ui.hdr.id != UAS_UI_SENSE) {
156 dprintf(1, "uas: expected sense ui, got ui id %d", ui.hdr.id);
161 if (ui.sense.status == 0) {
162 return DISK_RET_SUCCESS;
166 return DISK_RET_EBADTRACK;
170 uas_lun_setup(struct usbdevice_s *usbdev,
171 struct usb_pipe *command, struct usb_pipe *status,
172 struct usb_pipe *data_in, struct usb_pipe *data_out,
175 // Allocate drive structure.
176 struct uasdrive_s *drive = malloc_fseg(sizeof(*drive));
181 memset(drive, 0, sizeof(*drive));
182 if (usb_32bit_pipe(data_in))
183 drive->drive.type = DTYPE_UAS_32;
185 drive->drive.type = DTYPE_UAS;
186 drive->command = command;
187 drive->status = status;
188 drive->data_in = data_in;
189 drive->data_out = data_out;
192 int prio = bootprio_find_usb(usbdev, lun);
193 int ret = scsi_drive_setup(&drive->drive, "USB UAS", prio);
202 usb_uas_setup(struct usbdevice_s *usbdev)
207 // Verify right kind of device
208 struct usb_interface_descriptor *iface = usbdev->iface;
209 if (iface->bInterfaceSubClass != US_SC_SCSI ||
210 iface->bInterfaceProtocol != US_PR_UAS) {
211 dprintf(1, "Unsupported UAS device (subclass=%02x proto=%02x)\n"
212 , iface->bInterfaceSubClass, iface->bInterfaceProtocol);
216 /* find & allocate pipes */
217 struct usb_endpoint_descriptor *ep = NULL;
218 struct usb_pipe *command = NULL;
219 struct usb_pipe *status = NULL;
220 struct usb_pipe *data_in = NULL;
221 struct usb_pipe *data_out = NULL;
222 u8 *desc = (u8*)iface;
226 case USB_DT_ENDPOINT:
229 case USB_DT_ENDPOINT_COMPANION:
230 /* No support (yet) for usb3 streams */
231 dprintf(1, "Superspeed UAS devices not supported (yet)\n");
235 case UAS_PIPE_ID_COMMAND:
236 command = usb_alloc_pipe(usbdev, ep);
238 case UAS_PIPE_ID_STATUS:
239 status = usb_alloc_pipe(usbdev, ep);
241 case UAS_PIPE_ID_DATA_IN:
242 data_in = usb_alloc_pipe(usbdev, ep);
244 case UAS_PIPE_ID_DATA_OUT:
245 data_out = usb_alloc_pipe(usbdev, ep);
256 if (!command || !status || !data_in || !data_out)
259 /* TODO: send REPORT LUNS. For now, only LUN 0 is recognized. */
260 int ret = uas_lun_setup(usbdev, command, status, data_in, data_out, 0);
262 dprintf(1, "Unable to configure UAS drive.\n");
269 usb_free_pipe(usbdev, command);
270 usb_free_pipe(usbdev, status);
271 usb_free_pipe(usbdev, data_in);
272 usb_free_pipe(usbdev, data_out);