Hi,
Quick review, giving me the opportunity for a quick v2 before disappearing into my summer vacation, fixing the nits pointed out by Kevin.
The series also got a third commit which moves the usb mass storage defines from usb-msc.h to usb.h, so the uas code can easily reuse them.
cheers, Gerd
Gerd Hoffmann (3): move usb mass storage defines to usb.h usb attached scsi boot support lsi53c895a boot support
Makefile | 3 +- src/Kconfig | 14 +++- src/block.c | 4 +- src/blockcmd.c | 8 ++- src/disk.h | 2 + src/lsi-scsi.c | 212 +++++++++++++++++++++++++++++++++++++++++++++ src/lsi-scsi.h | 8 ++ src/post.c | 2 + src/usb-msc.h | 11 --- src/usb-uas.c | 264 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/usb-uas.h | 9 ++ src/usb.c | 10 ++- src/usb.h | 11 +++ 13 files changed, 540 insertions(+), 18 deletions(-) create mode 100644 src/lsi-scsi.c create mode 100644 src/lsi-scsi.h create mode 100644 src/usb-uas.c create mode 100644 src/usb-uas.h
With the arrival of usb attached scsi support they will be needed outside usb-msd.c too.
Signed-off-by: Gerd Hoffmann kraxel@redhat.com --- src/usb-msc.h | 11 ----------- src/usb.h | 10 ++++++++++ 2 files changed, 10 insertions(+), 11 deletions(-)
diff --git a/src/usb-msc.h b/src/usb-msc.h index 31c81b9..3746b77 100644 --- a/src/usb-msc.h +++ b/src/usb-msc.h @@ -7,15 +7,4 @@ int usb_cmd_data(struct disk_op_s *op, void *cdbcmd, u16 blocksize); struct usbdevice_s; int usb_msc_init(struct usbdevice_s *usbdev);
- -/**************************************************************** - * MSC flags - ****************************************************************/ - -#define US_SC_ATAPI_8020 0x02 -#define US_SC_ATAPI_8070 0x05 -#define US_SC_SCSI 0x06 - -#define US_PR_BULK 0x50 - #endif // ush-msc.h diff --git a/src/usb.h b/src/usb.h index 2c961e6..4747224 100644 --- a/src/usb.h +++ b/src/usb.h @@ -206,6 +206,16 @@ struct usb_endpoint_descriptor {
/**************************************************************** + * usb mass storage flags + ****************************************************************/ + +#define US_SC_ATAPI_8020 0x02 +#define US_SC_ATAPI_8070 0x05 +#define US_SC_SCSI 0x06 + +#define US_PR_BULK 0x50 + +/**************************************************************** * function defs ****************************************************************/
This patch adds support for booting from UAS (usb attached scsi) devices.
For now only usb 2.0 support is there. On usb 3.0 the UAS protocol uses streams, so changes will be required to make usb 3.0 devices fly once we have a xhci host controller driver.
So far the driver has been tested on qemu-emulated virtual hardware only. In theory should just work on bare metal too.
Signed-off-by: Gerd Hoffmann kraxel@redhat.com --- Makefile | 3 +- src/Kconfig | 8 ++- src/block.c | 3 +- src/blockcmd.c | 5 +- src/disk.h | 1 + src/usb-uas.c | 264 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/usb-uas.h | 9 ++ src/usb.c | 10 ++- src/usb.h | 3 +- 9 files changed, 298 insertions(+), 8 deletions(-) create mode 100644 src/usb-uas.c create mode 100644 src/usb-uas.h
diff --git a/Makefile b/Makefile index fe974f7..8fa5c91 100644 --- a/Makefile +++ b/Makefile @@ -12,7 +12,8 @@ SRCBOTH=misc.c stacks.c pmm.c output.c util.c block.c floppy.c ata.c mouse.c \ kbd.c pci.c serial.c clock.c pic.c cdrom.c ps2port.c smp.c resume.c \ pnpbios.c pirtable.c vgahooks.c ramdisk.c pcibios.c blockcmd.c \ usb.c usb-uhci.c usb-ohci.c usb-ehci.c usb-hid.c usb-msc.c \ - virtio-ring.c virtio-pci.c virtio-blk.c virtio-scsi.c apm.c ahci.c + virtio-ring.c virtio-pci.c virtio-blk.c virtio-scsi.c apm.c ahci.c \ + usb-uas.c SRC16=$(SRCBOTH) system.c disk.c font.c SRC32FLAT=$(SRCBOTH) post.c shadow.c memmap.c coreboot.c boot.c \ acpi.c smm.c mptable.c smbios.c pciinit.c optionroms.c mtrr.c \ diff --git a/src/Kconfig b/src/Kconfig index 8932c9e..a798d9f 100644 --- a/src/Kconfig +++ b/src/Kconfig @@ -161,7 +161,13 @@ menu "Hardware support" bool "USB drives" default y help - Support USB disks. + Support USB BOT (bulk-only transport) disks. + config USB_UAS + depends on USB && DRIVES + bool "UAS drives" + default y + help + Support USB UAS (usb attached scsi) disks. config USB_HUB depends on USB bool "USB hubs" diff --git a/src/block.c b/src/block.c index 7a62787..50d7bb4 100644 --- a/src/block.c +++ b/src/block.c @@ -280,7 +280,7 @@ map_floppy_drive(struct drive_s *drive_g) static int process_scsi_op(struct disk_op_s *op) { - if (!CONFIG_VIRTIO_SCSI && !CONFIG_USB_MSC) + if (!CONFIG_VIRTIO_SCSI && !CONFIG_USB_MSC && !CONFIG_USB_UAS) return 0; switch (op->command) { case CMD_READ: @@ -321,6 +321,7 @@ process_op(struct disk_op_s *op) case DTYPE_AHCI: return process_ahci_op(op); case DTYPE_USB: + case DTYPE_UAS: case DTYPE_VIRTIO_SCSI: return process_scsi_op(op); default: diff --git a/src/blockcmd.c b/src/blockcmd.c index 2dda04d..2998a7c 100644 --- a/src/blockcmd.c +++ b/src/blockcmd.c @@ -12,6 +12,7 @@ #include "ata.h" // atapi_cmd_data #include "ahci.h" // atapi_cmd_data #include "usb-msc.h" // usb_cmd_data +#include "usb-uas.h" // usb_cmd_data #include "virtio-scsi.h" // virtio_scsi_cmd_data #include "boot.h" // boot_add_hd
@@ -25,6 +26,8 @@ cdb_cmd_data(struct disk_op_s *op, void *cdbcmd, u16 blocksize) return atapi_cmd_data(op, cdbcmd, blocksize); case DTYPE_USB: return usb_cmd_data(op, cdbcmd, blocksize); + case DTYPE_UAS: + return uas_cmd_data(op, cdbcmd, blocksize); case DTYPE_AHCI: return ahci_cmd_data(op, cdbcmd, blocksize); case DTYPE_VIRTIO_SCSI: @@ -90,7 +93,7 @@ scsi_is_ready(struct disk_op_s *op) int scsi_init_drive(struct drive_s *drive, const char *s, int prio) { - if (!CONFIG_USB_MSC && !CONFIG_VIRTIO_SCSI) + if (!CONFIG_USB_UAS && !CONFIG_USB_MSC && !CONFIG_VIRTIO_SCSI) return 0;
struct disk_op_s dop; diff --git a/src/disk.h b/src/disk.h index 6776ee6..787b5b4 100644 --- a/src/disk.h +++ b/src/disk.h @@ -231,6 +231,7 @@ struct drive_s { #define DTYPE_VIRTIO_SCSI 0x07 #define DTYPE_VIRTIO_BLK 0x08 #define DTYPE_USB 0x09 +#define DTYPE_UAS 0x0a
#define MAXDESCSIZE 80
diff --git a/src/usb-uas.c b/src/usb-uas.c new file mode 100644 index 0000000..be153ed --- /dev/null +++ b/src/usb-uas.c @@ -0,0 +1,264 @@ +// Code for handling usb attached scsi devices. +// +// only usb 2.0 for now. +// +// once we have xhci driver with usb 3.0 support this must +// be updated to use usb3 streams so booting from usb3 +// devices actually works. +// +// Authors: +// Gerd Hoffmann kraxel@redhat.com +// +// based on usb-msc.c which is written by: +// Kevin O'Connor kevin@koconnor.net +// +// This file may be distributed under the terms of the GNU LGPLv3 license. + +#include "util.h" // dprintf +#include "config.h" // CONFIG_USB_UAS +#include "usb.h" // struct usb_s +#include "biosvar.h" // GET_GLOBAL +#include "blockcmd.h" // cdb_read +#include "disk.h" // DTYPE_UAS +#include "boot.h" // bootprio_find_usb +#include "usb-uas.h" // usb_uas_init + +#define UAS_UI_COMMAND 0x01 +#define UAS_UI_SENSE 0x03 +#define UAS_UI_RESPONSE 0x04 +#define UAS_UI_TASK_MGMT 0x05 +#define UAS_UI_READ_READY 0x06 +#define UAS_UI_WRITE_READY 0x07 + +#define UAS_PIPE_ID_COMMAND 0x01 +#define UAS_PIPE_ID_STATUS 0x02 +#define UAS_PIPE_ID_DATA_IN 0x03 +#define UAS_PIPE_ID_DATA_OUT 0x04 + +typedef struct { + u8 id; + u8 reserved; + u16 tag; +} PACKED uas_ui_header; + +typedef struct { + u8 prio_taskattr; /* 6:3 priority, 2:0 task attribute */ + u8 reserved_1; + u8 add_cdb_length; /* 7:2 additional adb length (dwords) */ + u8 reserved_2; + u8 lun[8]; + u8 cdb[16]; + u8 add_cdb[]; +} PACKED uas_ui_command; + +typedef struct { + u16 status_qualifier; + u8 status; + u8 reserved[7]; + u16 sense_length; + u8 sense_data[18]; +} PACKED uas_ui_sense; + +typedef struct { + u16 add_response_info; + u8 response_code; +} PACKED uas_ui_response; + +typedef struct { + u8 function; + u8 reserved; + u16 task_tag; + u8 lun[8]; +} PACKED uas_ui_task_mgmt; + +typedef struct { + uas_ui_header hdr; + union { + uas_ui_command command; + uas_ui_sense sense; + uas_ui_task_mgmt task; + uas_ui_response response; + }; +} PACKED uas_ui; + +struct uasdrive_s { + struct drive_s drive; + struct usb_pipe *command, *status, *data_in, *data_out; + int lun; +}; + +int +uas_cmd_data(struct disk_op_s *op, void *cdbcmd, u16 blocksize) +{ + if (!CONFIG_USB_UAS) + return DISK_RET_EBADTRACK; + + struct uasdrive_s *drive = container_of( + op->drive_g, struct uasdrive_s, drive); + + uas_ui ui; + memset(&ui, 0, sizeof(ui)); + ui.hdr.id = UAS_UI_COMMAND; + ui.hdr.tag = 0xdead; + ui.command.lun[1] = drive->lun; + memcpy(ui.command.cdb, cdbcmd, sizeof(ui.command.cdb)); + int ret = usb_send_bulk(GET_GLOBAL(drive->command), + USB_DIR_OUT, MAKE_FLATPTR(GET_SEG(SS), &ui), + sizeof(ui.hdr) + sizeof(ui.command)); + if (ret) { + dprintf(1, "uas: command send fail"); + goto fail; + } + + memset(&ui, 0xff, sizeof(ui)); + ret = usb_send_bulk(GET_GLOBAL(drive->status), + USB_DIR_IN, MAKE_FLATPTR(GET_SEG(SS), &ui), sizeof(ui)); + if (ret) { + dprintf(1, "uas: status recv fail"); + goto fail; + } + + switch (ui.hdr.id) { + case UAS_UI_SENSE: + goto have_sense; + case UAS_UI_READ_READY: + ret = usb_send_bulk(GET_GLOBAL(drive->data_in), + USB_DIR_IN, op->buf_fl, op->count * blocksize); + if (ret) { + dprintf(1, "uas: data read fail"); + goto fail; + } + break; + case UAS_UI_WRITE_READY: + ret = usb_send_bulk(GET_GLOBAL(drive->data_out), + USB_DIR_OUT, op->buf_fl, op->count * blocksize); + if (ret) { + dprintf(1, "uas: data write fail"); + goto fail; + } + break; + default: + dprintf(1, "uas: unknown status ui id %d", ui.hdr.id); + goto fail; + } + + memset(&ui, 0xff, sizeof(ui)); + ret = usb_send_bulk(GET_GLOBAL(drive->status), + USB_DIR_IN, MAKE_FLATPTR(GET_SEG(SS), &ui), sizeof(ui)); + if (ret) { + dprintf(1, "uas: status recv fail"); + goto fail; + } + if (ui.hdr.id != UAS_UI_SENSE) { + dprintf(1, "uas: expected sense ui, got ui id %d", ui.hdr.id); + goto fail; + } + +have_sense: + if (ui.sense.status == 0) { + return DISK_RET_SUCCESS; + } + +fail: + return DISK_RET_EBADTRACK; +} + +static int +uas_init_lun(struct usbdevice_s *usbdev, + struct usb_pipe *command, struct usb_pipe *status, + struct usb_pipe *data_in, struct usb_pipe *data_out, + int lun) +{ + // Allocate drive structure. + struct uasdrive_s *drive = malloc_fseg(sizeof(*drive)); + if (!drive) { + warn_noalloc(); + return -1; + } + memset(drive, 0, sizeof(*drive)); + drive->drive.type = DTYPE_UAS; + drive->command = command; + drive->status = status; + drive->data_in = data_in; + drive->data_out = data_out; + drive->lun = lun; + + int prio = bootprio_find_usb(usbdev, lun); + int ret = scsi_init_drive(&drive->drive, "USB UAS", prio); + if (ret) { + free(drive); + return -1; + } + return 0; +} + +int +usb_uas_init(struct usbdevice_s *usbdev) +{ + if (!CONFIG_USB_UAS) + return -1; + + // Verify right kind of device + struct usb_interface_descriptor *iface = usbdev->iface; + if (iface->bInterfaceSubClass != US_SC_SCSI || + iface->bInterfaceProtocol != US_PR_UAS) { + dprintf(1, "Unsupported UAS device (subclass=%02x proto=%02x)\n" + , iface->bInterfaceSubClass, iface->bInterfaceProtocol); + return -1; + } + + /* find & allocate pipes */ + struct usb_endpoint_descriptor *ep = NULL; + struct usb_pipe *command = NULL; + struct usb_pipe *status = NULL; + struct usb_pipe *data_in = NULL; + struct usb_pipe *data_out = NULL; + u8 *desc = (u8*)iface; + while (desc) { + desc += desc[0]; + switch (desc[1]) { + case USB_DT_ENDPOINT: + ep = (void*)desc; + break; + case 0x24: + switch (desc[2]) { + case UAS_PIPE_ID_COMMAND: + command = usb_alloc_pipe(usbdev, ep); + break; + case UAS_PIPE_ID_STATUS: + status = usb_alloc_pipe(usbdev, ep); + break; + case UAS_PIPE_ID_DATA_IN: + data_in = usb_alloc_pipe(usbdev, ep); + break; + case UAS_PIPE_ID_DATA_OUT: + data_out = usb_alloc_pipe(usbdev, ep); + break; + default: + goto fail; + } + break; + default: + desc = NULL; + break; + } + } + if (!command || !status || !data_in || !data_out) + goto fail; + + /* TODO: send REPORT LUNS. For now, only LUN 0 is recognized. */ + int ret = uas_init_lun(usbdev, command, status, data_in, data_out, 0); + if (ret < 0) { + dprintf(1, "Unable to configure UAS drive.\n"); + goto fail; + } + + return 0; + +fail: + free_pipe(command); + free_pipe(status); + free_pipe(data_in); + free_pipe(data_out); + return -1; +} diff --git a/src/usb-uas.h b/src/usb-uas.h new file mode 100644 index 0000000..ed1d473 --- /dev/null +++ b/src/usb-uas.h @@ -0,0 +1,9 @@ +#ifndef __USB_UAS_H +#define __USB_UAS_H + +struct disk_op_s; +int uas_cmd_data(struct disk_op_s *op, void *cdbcmd, u16 blocksize); +struct usbdevice_s; +int usb_uas_init(struct usbdevice_s *usbdev); + +#endif /* __USB_UAS_H */ diff --git a/src/usb.c b/src/usb.c index bde7a58..421d0b8 100644 --- a/src/usb.c +++ b/src/usb.c @@ -15,6 +15,7 @@ #include "usb-hid.h" // usb_keyboard_setup #include "usb-hub.h" // usb_hub_init #include "usb-msc.h" // usb_msc_init +#include "usb-uas.h" // usb_uas_init #include "usb.h" // struct usb_s #include "biosvar.h" // GET_GLOBAL
@@ -323,9 +324,12 @@ configure_usb_device(struct usbdevice_s *usbdev) usbdev->imax = (void*)config + config->wTotalLength - (void*)iface; if (iface->bInterfaceClass == USB_CLASS_HUB) ret = usb_hub_init(usbdev); - else if (iface->bInterfaceClass == USB_CLASS_MASS_STORAGE) - ret = usb_msc_init(usbdev); - else + else if (iface->bInterfaceClass == USB_CLASS_MASS_STORAGE) { + if (iface->bInterfaceProtocol == US_PR_BULK) + ret = usb_msc_init(usbdev); + if (iface->bInterfaceProtocol == US_PR_UAS) + ret = usb_uas_init(usbdev); + } else ret = usb_hid_init(usbdev); if (ret) goto fail; diff --git a/src/usb.h b/src/usb.h index 4747224..a43e829 100644 --- a/src/usb.h +++ b/src/usb.h @@ -213,7 +213,8 @@ struct usb_endpoint_descriptor { #define US_SC_ATAPI_8070 0x05 #define US_SC_SCSI 0x06
-#define US_PR_BULK 0x50 +#define US_PR_BULK 0x50 /* bulk-only transport */ +#define US_PR_UAS 0x62 /* usb attached scsi */
/**************************************************************** * function defs
This patch adds support for the lsi53c895a scsi host adapter, allowing seabios to boot from scsi disks and cdroms connected to the lsi scsi hba emulated by qemu.
This driver was written by looking at the expectations of qemu's lsi emulation. I have no idea idea how close this is to work on real hardware, and I somehow doubt anyone cares given the age of physical lsi scsi cards. It depends on !COREBOOT for that reason.
Signed-off-by: Gerd Hoffmann kraxel@redhat.com --- Makefile | 2 +- src/Kconfig | 6 ++ src/block.c | 3 +- src/blockcmd.c | 5 +- src/disk.h | 1 + src/lsi-scsi.c | 212 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/lsi-scsi.h | 8 ++ src/post.c | 2 + 8 files changed, 236 insertions(+), 3 deletions(-) create mode 100644 src/lsi-scsi.c create mode 100644 src/lsi-scsi.h
diff --git a/Makefile b/Makefile index 8fa5c91..dfdec5c 100644 --- a/Makefile +++ b/Makefile @@ -13,7 +13,7 @@ SRCBOTH=misc.c stacks.c pmm.c output.c util.c block.c floppy.c ata.c mouse.c \ pnpbios.c pirtable.c vgahooks.c ramdisk.c pcibios.c blockcmd.c \ usb.c usb-uhci.c usb-ohci.c usb-ehci.c usb-hid.c usb-msc.c \ virtio-ring.c virtio-pci.c virtio-blk.c virtio-scsi.c apm.c ahci.c \ - usb-uas.c + usb-uas.c lsi-scsi.c SRC16=$(SRCBOTH) system.c disk.c font.c SRC32FLAT=$(SRCBOTH) post.c shadow.c memmap.c coreboot.c boot.c \ acpi.c smm.c mptable.c smbios.c pciinit.c optionroms.c mtrr.c \ diff --git a/src/Kconfig b/src/Kconfig index a798d9f..bc343ee 100644 --- a/src/Kconfig +++ b/src/Kconfig @@ -119,6 +119,12 @@ menu "Hardware support" default y help Support boot from virtio-scsi storage. + config LSI_SCSI + depends on DRIVES && !COREBOOT + bool "lsi53c895a scsi controllers" + default y + help + Support boot from qemu-emulated lsi53c895a scsi storage. config FLOPPY depends on DRIVES bool "Floppy controller" diff --git a/src/block.c b/src/block.c index 50d7bb4..2de9c5d 100644 --- a/src/block.c +++ b/src/block.c @@ -280,7 +280,7 @@ map_floppy_drive(struct drive_s *drive_g) static int process_scsi_op(struct disk_op_s *op) { - if (!CONFIG_VIRTIO_SCSI && !CONFIG_USB_MSC && !CONFIG_USB_UAS) + if (!CONFIG_VIRTIO_SCSI && !CONFIG_USB_MSC && !CONFIG_USB_UAS && !CONFIG_LSI_SCSI) return 0; switch (op->command) { case CMD_READ: @@ -323,6 +323,7 @@ process_op(struct disk_op_s *op) case DTYPE_USB: case DTYPE_UAS: case DTYPE_VIRTIO_SCSI: + case DTYPE_LSI_SCSI: return process_scsi_op(op); default: op->count = 0; diff --git a/src/blockcmd.c b/src/blockcmd.c index 2998a7c..0d0acfd 100644 --- a/src/blockcmd.c +++ b/src/blockcmd.c @@ -14,6 +14,7 @@ #include "usb-msc.h" // usb_cmd_data #include "usb-uas.h" // usb_cmd_data #include "virtio-scsi.h" // virtio_scsi_cmd_data +#include "lsi-scsi.h" // lsi_scsi_cmd_data #include "boot.h" // boot_add_hd
// Route command to low-level handler. @@ -32,6 +33,8 @@ cdb_cmd_data(struct disk_op_s *op, void *cdbcmd, u16 blocksize) return ahci_cmd_data(op, cdbcmd, blocksize); case DTYPE_VIRTIO_SCSI: return virtio_scsi_cmd_data(op, cdbcmd, blocksize); + case DTYPE_LSI_SCSI: + return lsi_scsi_cmd_data(op, cdbcmd, blocksize); default: op->count = 0; return DISK_RET_EPARAM; @@ -93,7 +96,7 @@ scsi_is_ready(struct disk_op_s *op) int scsi_init_drive(struct drive_s *drive, const char *s, int prio) { - if (!CONFIG_USB_UAS && !CONFIG_USB_MSC && !CONFIG_VIRTIO_SCSI) + if (!CONFIG_USB_UAS && !CONFIG_USB_MSC && !CONFIG_VIRTIO_SCSI && !CONFIG_LSI_SCSI) return 0;
struct disk_op_s dop; diff --git a/src/disk.h b/src/disk.h index 787b5b4..6810cd7 100644 --- a/src/disk.h +++ b/src/disk.h @@ -232,6 +232,7 @@ struct drive_s { #define DTYPE_VIRTIO_BLK 0x08 #define DTYPE_USB 0x09 #define DTYPE_UAS 0x0a +#define DTYPE_LSI_SCSI 0x0b
#define MAXDESCSIZE 80
diff --git a/src/lsi-scsi.c b/src/lsi-scsi.c new file mode 100644 index 0000000..949d3f4 --- /dev/null +++ b/src/lsi-scsi.c @@ -0,0 +1,212 @@ +// (qemu-emulated) lsi53c895a boot support. +// +// Copyright (C) 2012 Red Hat Inc. +// +// Authors: +// Gerd Hoffmann kraxel@redhat.com +// +// based on virtio-scsi.c which is written by: +// Paolo Bonzini pbonzini@redhat.com +// +// This file may be distributed under the terms of the GNU LGPLv3 license. + +#include "util.h" // dprintf +#include "pci.h" // foreachpci +#include "config.h" // CONFIG_* +#include "biosvar.h" // GET_GLOBAL +#include "pci_ids.h" // PCI_DEVICE_ID_VIRTIO_BLK +#include "pci_regs.h" // PCI_VENDOR_ID +#include "boot.h" // bootprio_find_scsi_device +#include "blockcmd.h" // scsi_init_drive +#include "disk.h" + +#define LSI_REG_DSTAT 0x0c +#define LSI_REG_ISTAT0 0x14 +#define LSI_REG_DSP0 0x2c +#define LSI_REG_DSP1 0x2d +#define LSI_REG_DSP2 0x2e +#define LSI_REG_DSP3 0x2f +#define LSI_REG_SIST0 0x42 +#define LSI_REG_SIST1 0x43 + +#define LSI_ISTAT0_DIP 0x01 +#define LSI_ISTAT0_SIP 0x02 +#define LSI_ISTAT0_INTF 0x04 +#define LSI_ISTAT0_CON 0x08 +#define LSI_ISTAT0_SEM 0x10 +#define LSI_ISTAT0_SIGP 0x20 +#define LSI_ISTAT0_SRST 0x40 +#define LSI_ISTAT0_ABRT 0x80 + +struct lsi_lun_s { + struct drive_s drive; + struct pci_device *pci; + u32 iobase; + u8 target; + u8 lun; +}; + +static int +lsi_scsi_cmd(struct lsi_lun_s *llun, struct disk_op_s *op, + void *cdbcmd, u16 target, u16 lun, u16 blocksize) +{ + u32 iobase = GET_GLOBAL(llun->iobase); + u32 dma = ((cdb_is_read(cdbcmd, blocksize) ? 0x01000000 : 0x00000000) | + (op->count * blocksize)); + u8 msgout[] = { + 0x80 | lun, // select lun + 0x08, + }; + u8 status = 0xff; + u8 msgin_tmp[2]; + u8 msgin = 0xff; + + u32 script[] = { + /* select target, send scsi command */ + 0x40000000 | target << 16, // select target + 0x00000000, + 0x06000001, // msgout + (u32)MAKE_FLATPTR(GET_SEG(SS), &msgout), + 0x02000010, // scsi command + (u32)MAKE_FLATPTR(GET_SEG(SS), cdbcmd), + + /* handle disconnect */ + 0x87820000, // phase == msgin ? + 0x00000018, + 0x07000002, // msgin + (u32)MAKE_FLATPTR(GET_SEG(SS), &msgin_tmp), + 0x50000000, // re-select + 0x00000000, + 0x07000002, // msgin + (u32)MAKE_FLATPTR(GET_SEG(SS), &msgin_tmp), + + /* dma data, get status, raise irq */ + dma, // dma data + (u32)op->buf_fl, + 0x03000001, // status + (u32)MAKE_FLATPTR(GET_SEG(SS), &status), + 0x07000001, // msgin + (u32)MAKE_FLATPTR(GET_SEG(SS), &msgin), + 0x98080000, // dma irq + 0x00000000, + }; + u32 dsp = (u32)MAKE_FLATPTR(GET_SEG(SS), &script); + + outb(dsp & 0xff, iobase + LSI_REG_DSP0); + outb((dsp >> 8) & 0xff, iobase + LSI_REG_DSP1); + outb((dsp >> 16) & 0xff, iobase + LSI_REG_DSP2); + outb((dsp >> 24) & 0xff, iobase + LSI_REG_DSP3); + + for (;;) { + u8 dstat = inb(iobase + LSI_REG_DSTAT); + u8 sist0 = inb(iobase + LSI_REG_SIST0); + u8 sist1 = inb(iobase + LSI_REG_SIST1); + if (sist0 || sist1) { + goto fail; + } + if (dstat & 0x04) { + break; + } + usleep(5); + } + + if (msgin == 0 && status == 0) { + return DISK_RET_SUCCESS; + } + +fail: + // sledge hammer to get back into a known-good state: hba reset + outb(LSI_ISTAT0_SRST, iobase + LSI_REG_ISTAT0); + return DISK_RET_EBADTRACK; +} + +int +lsi_scsi_cmd_data(struct disk_op_s *op, void *cdbcmd, u16 blocksize) +{ + if (!CONFIG_LSI_SCSI) + return DISK_RET_EBADTRACK; + + struct lsi_lun_s *llun = + container_of(op->drive_g, struct lsi_lun_s, drive); + + return lsi_scsi_cmd(llun, op, cdbcmd, + GET_GLOBAL(llun->target), GET_GLOBAL(llun->lun), + blocksize); +} + +static int +lsi_scsi_add_lun(struct pci_device *pci, u32 iobase, u8 target, u8 lun) +{ + struct lsi_lun_s *llun = malloc_fseg(sizeof(*llun)); + if (!llun) { + warn_noalloc(); + return -1; + } + memset(llun, 0, sizeof(*llun)); + llun->drive.type = DTYPE_LSI_SCSI; + llun->drive.cntl_id = pci->bdf; + llun->pci = pci; + llun->target = target; + llun->lun = lun; + llun->iobase = iobase; + + char *name = znprintf(16, "lsi %02x:%02x.%x %d:%d", + pci_bdf_to_bus(pci->bdf), pci_bdf_to_dev(pci->bdf), + pci_bdf_to_fn(pci->bdf), target, lun); + int prio = bootprio_find_scsi_device(pci, target, lun); + int ret = scsi_init_drive(&llun->drive, name, prio); + free(name); + if (ret) + goto fail; + return 0; + +fail: + free(llun); + return -1; +} + +static void +lsi_scsi_scan_target(struct pci_device *pci, u32 iobase, u8 target) +{ + /* TODO: send REPORT LUNS. For now, only LUN 0 is recognized. */ + lsi_scsi_add_lun(pci, iobase, target, 0); +} + +static void +init_lsi_scsi(struct pci_device *pci) +{ + u16 bdf = pci->bdf; + u32 iobase = pci_config_readl(pci->bdf, PCI_BASE_ADDRESS_0) + & PCI_BASE_ADDRESS_IO_MASK; + + dprintf(1, "found lsi53c895a at %02x:%02x.%x, io @ %x\n", + pci_bdf_to_bus(bdf), pci_bdf_to_dev(bdf), + pci_bdf_to_fn(bdf), iobase); + + // reset + outb(LSI_ISTAT0_SRST, iobase + LSI_REG_ISTAT0); + + int i; + for (i = 0; i < 7; i++) + lsi_scsi_scan_target(pci, iobase, i); + + return; +} + +void +lsi_scsi_setup(void) +{ + ASSERT32FLAT(); + if (!CONFIG_LSI_SCSI) + return; + + dprintf(3, "init lsi53c895a\n"); + + struct pci_device *pci; + foreachpci(pci) { + if (pci->vendor != PCI_VENDOR_ID_LSI_LOGIC + || pci->device != PCI_DEVICE_ID_LSI_53C895A) + continue; + init_lsi_scsi(pci); + } +} diff --git a/src/lsi-scsi.h b/src/lsi-scsi.h new file mode 100644 index 0000000..9c5a9b2 --- /dev/null +++ b/src/lsi-scsi.h @@ -0,0 +1,8 @@ +#ifndef __LSI_SCSI_H +#define __LSI_SCSI_H + +struct disk_op_s; +int lsi_scsi_cmd_data(struct disk_op_s *op, void *cdbcmd, u16 blocksize); +void lsi_scsi_setup(void); + +#endif /* __LSI_SCSI_H */ diff --git a/src/post.c b/src/post.c index 3101505..0f31b4c 100644 --- a/src/post.c +++ b/src/post.c @@ -27,6 +27,7 @@ #include "ps2port.h" // ps2port_setup #include "virtio-blk.h" // virtio_blk_setup #include "virtio-scsi.h" // virtio_scsi_setup +#include "lsi-scsi.h" // lsi_scsi_setup
/**************************************************************** @@ -194,6 +195,7 @@ init_hw(void) ramdisk_setup(); virtio_blk_setup(); virtio_scsi_setup(); + lsi_scsi_setup(); }
// Begin the boot process by invoking an int0x19 in 16bit mode.
On Fri, Jul 20, 2012 at 10:59:22AM +0200, Gerd Hoffmann wrote:
Hi,
Quick review, giving me the opportunity for a quick v2 before disappearing into my summer vacation, fixing the nits pointed out by Kevin.
The series also got a third commit which moves the usb mass storage defines from usb-msc.h to usb.h, so the uas code can easily reuse them.
Thanks. I committed this series.
-Kevin