The following patch enhances libata to allow SAS device drivers to utilize libata to talk to SATA devices. It introduces some new APIs which allow libata to be used without allocating a virtual scsi host. Since the last time I posted this code, I have removed all dependence on the ata_host_set struct. New APIs: ata_sas_port_alloc - Allocate an ata_port ata_sas_port_init - Initialize an ata_port (probe device, etc) ata_sas_port_destroy - Free an ata_port allocated by ata_sas_port_alloc ata_sas_slave_configure - configure scsi device ata_sas_queuecmd - queue a scsi command, similar to ata_scsi_queuecomand These new APIs can be used either directly by a SAS LLDD or could be used by the SAS transport class. Possible usage for a SAS LLDD would be: scsi_scan_host slave_alloc ata_sas_port_alloc ata_sas_port_init slave_configure ata_sas_slave_configure Commands received by the LLDD for SATA devices would call ata_sas_queuecmd. Device teardown would occur with: slave_destroy ata_sas_port_destroy Signed-off-by: Brian King <brking@xxxxxxxxxx> --- linux-2.6-bjking1/drivers/scsi/libata-core.c | 32 ++-- linux-2.6-bjking1/drivers/scsi/libata-scsi.c | 191 +++++++++++++++++++++++---- linux-2.6-bjking1/drivers/scsi/libata.h | 7 linux-2.6-bjking1/include/linux/libata.h | 7 4 files changed, 199 insertions(+), 38 deletions(-) diff -puN drivers/scsi/libata-scsi.c~libata_sas drivers/scsi/libata-scsi.c --- linux-2.6/drivers/scsi/libata-scsi.c~libata_sas 2005-10-24 15:08:27.000000000 -0500 +++ linux-2.6-bjking1/drivers/scsi/libata-scsi.c 2005-10-24 16:53:48.000000000 -0500 @@ -325,6 +325,30 @@ void ata_to_sense_error(struct ata_queue } } +static void ata_scsi_sdev_config(struct scsi_device *sdev) +{ + sdev->use_10_for_rw = 1; + sdev->use_10_for_ms = 1; +} + +static void ata_scsi_dev_config(struct scsi_device *sdev, + struct ata_device *dev) +{ + /* TODO: 1024 is an arbitrary number, not the + * hardware maximum. This should be increased to + * 65534 when Jens Axboe's patch for dynamically + * determining max_sectors is merged. + */ + if ((dev->flags & ATA_DFLAG_LBA48) && + ((dev->flags & ATA_DFLAG_LOCK_SECTORS) == 0)) { + /* + * do not overwrite sdev->host->max_sectors, since + * other drives on this host may not support LBA48 + */ + blk_queue_max_sectors(sdev->request_queue, 2048); + } +} + /** * ata_scsi_slave_config - Set SCSI device attributes * @sdev: SCSI device to examine @@ -339,9 +363,7 @@ void ata_to_sense_error(struct ata_queue int ata_scsi_slave_config(struct scsi_device *sdev) { - sdev->use_10_for_rw = 1; - sdev->use_10_for_ms = 1; - + ata_scsi_sdev_config(sdev); blk_queue_max_phys_segments(sdev->request_queue, LIBATA_MAX_PRD); if (sdev->id < ATA_MAX_DEVICES) { @@ -351,19 +373,7 @@ int ata_scsi_slave_config(struct scsi_de ap = (struct ata_port *) &sdev->host->hostdata[0]; dev = &ap->device[sdev->id]; - /* TODO: 1024 is an arbitrary number, not the - * hardware maximum. This should be increased to - * 65534 when Jens Axboe's patch for dynamically - * determining max_sectors is merged. - */ - if ((dev->flags & ATA_DFLAG_LBA48) && - ((dev->flags & ATA_DFLAG_LOCK_SECTORS) == 0)) { - /* - * do not overwrite sdev->host->max_sectors, since - * other drives on this host may not support LBA48 - */ - blk_queue_max_sectors(sdev->request_queue, 2048); - } + ata_scsi_dev_config(sdev, dev); } return 0; /* scsi layer doesn't check return value, sigh */ @@ -1541,6 +1551,21 @@ static inline void ata_scsi_dump_cdb(str #endif } +static void __ata_scsi_queuecmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *), + struct ata_port *ap, struct ata_device *dev) +{ + if (dev->class == ATA_DEV_ATA) { + ata_xlat_func_t xlat_func = ata_get_xlat_func(dev, + cmd->cmnd[0]); + + if (xlat_func) + ata_scsi_translate(ap, dev, cmd, done, xlat_func); + else + ata_scsi_simulate(dev->id, cmd, done); + } else + ata_scsi_translate(ap, dev, cmd, done, atapi_xlat); +} + /** * ata_scsi_queuecmd - Issue SCSI cdb to libata-managed device * @cmd: SCSI command to be sent @@ -1577,16 +1602,7 @@ int ata_scsi_queuecmd(struct scsi_cmnd * goto out_unlock; } - if (dev->class == ATA_DEV_ATA) { - ata_xlat_func_t xlat_func = ata_get_xlat_func(dev, - cmd->cmnd[0]); - - if (xlat_func) - ata_scsi_translate(ap, dev, cmd, done, xlat_func); - else - ata_scsi_simulate(dev->id, cmd, done); - } else - ata_scsi_translate(ap, dev, cmd, done, atapi_xlat); + __ata_scsi_queuecmd(cmd, done, ap, dev); out_unlock: return 0; @@ -1678,3 +1694,126 @@ void ata_scsi_simulate(u16 *id, } } +/** + * ata_sas_port_alloc - Allocate port for a SAS attached SATA device + * @pdev: PCI device that the scsi device is attached to + * @port_info: Information from low-level host driver + * + * LOCKING: + * PCI/etc. bus probe sem. + * + * RETURNS: + * ata_port pointer on success / NULL on failure. + */ + +struct ata_port *ata_sas_port_alloc(struct pci_dev *pdev, + struct ata_port_info *port_info) +{ + struct ata_port *ap = kzalloc(sizeof(*ap), GFP_KERNEL); + + if (!ap) + return NULL; + + ap->dev = &pdev->dev; + ap->ops = port_info->port_ops; + ap->flags = port_info->host_flags; + ap->pio_mask = port_info->pio_mask; + ap->mwdma_mask = port_info->mwdma_mask; + ap->udma_mask = port_info->udma_mask; + ap->cbl = ATA_CBL_SATA; + ap->active_tag = ATA_TAG_POISON; + ap->last_ctl = 0xFF; + return ap; +} +EXPORT_SYMBOL_GPL(ata_sas_port_alloc); + +static void ata_sas_port_free(struct ata_port *ap) +{ + kfree(ap); +} + +/** + * ata_sas_port_init - Initialize a SATA device + * @ap: SATA port to initialize + * + * LOCKING: + * PCI/etc. bus probe sem. + * + * RETURNS: + * Zero on success, non-zero on error. + */ + +int ata_sas_port_init(struct ata_port *ap) +{ + int rc = ap->ops->port_start(ap); + + if (!rc) + rc = ata_bus_probe(ap); + + return rc; +} +EXPORT_SYMBOL_GPL(ata_sas_port_init); + +/** + * ata_sas_port_destroy - Destroy a SATA port allocated by ata_sas_port_alloc + * @ap: SATA port to destroy + * + */ + +void ata_sas_port_destroy(struct ata_port *ap) +{ + if (ap) { + ap->ops->port_stop(ap); + ata_sas_port_free(ap); + } +} +EXPORT_SYMBOL_GPL(ata_sas_port_destroy); + +/** + * ata_sas_slave_configure - Default slave_config routine for libata devices + * @sdev: SCSI device to configure + * @ap: ATA port to which SCSI device is attached + * + * RETURNS: + * Zero. + */ + +int ata_sas_slave_configure(struct scsi_device *sdev, struct ata_port *ap) +{ + ata_scsi_sdev_config(sdev); + ata_scsi_dev_config(sdev, ap->device); + if (ata_dev_knobble(ap)) + blk_queue_max_sectors(sdev->request_queue, ATA_MAX_SECTORS); + return 0; +} +EXPORT_SYMBOL_GPL(ata_sas_slave_configure); + +/** + * ata_sas_queuecmd - Issue SCSI cdb to libata-managed device + * @cmd: SCSI command to be sent + * @done: Completion function, called when command is complete + * @ap: ATA port to which the command is being sent + * + * RETURNS: + * Zero. + */ + +int ata_sas_queuecmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *), + struct ata_port *ap) +{ + if (unlikely(!ata_dev_present(ap->device))) { + cmd->result = (DID_BAD_TARGET << 16); + done(cmd); + return 0; + } + + if (cmd->cmd_len > ap->cdb_len) { + cmd->result = (DID_ABORT << 16); + done(cmd); + return 0; + } + + __ata_scsi_queuecmd(cmd, done, ap, ap->device); + return 0; +} +EXPORT_SYMBOL_GPL(ata_sas_queuecmd); diff -puN drivers/scsi/libata-core.c~libata_sas drivers/scsi/libata-core.c --- linux-2.6/drivers/scsi/libata-core.c~libata_sas 2005-10-24 15:08:27.000000000 -0500 +++ linux-2.6-bjking1/drivers/scsi/libata-core.c 2005-10-24 15:08:27.000000000 -0500 @@ -1106,6 +1106,21 @@ static inline void ata_dump_id(struct at dev->id[93]); } +static void ata_set_cdb_len(struct ata_port *ap, unsigned int cdb_len) +{ + ap->cdb_len = cdb_len; + if (ap->host) + ap->host->max_cmd_len = cdb_len; +} + +static void ata_set_max_sectors(struct ata_port *ap, unsigned int max_sectors) +{ + if (ap->host) { + ap->host->max_sectors = ATA_MAX_SECTORS; + ap->host->hostt->max_sectors = ATA_MAX_SECTORS; + } +} + /** * ata_dev_identify - obtain IDENTIFY x DEVICE page * @ap: port on which device we wish to probe resides @@ -1266,7 +1281,7 @@ retry: dev->n_sectors = ata_id_u32(dev->id, 60); } - ap->host->max_cmd_len = 16; + ata_set_cdb_len(ap, 16); /* print device info to dmesg */ printk(KERN_INFO "ata%u: dev %u ATA, max %s, %Lu sectors:%s\n", @@ -1286,8 +1301,8 @@ retry: printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id); goto err_out_nosup; } - ap->cdb_len = (unsigned int) rc; - ap->host->max_cmd_len = (unsigned char) ap->cdb_len; + + ata_set_cdb_len(ap, rc); /* print device info to dmesg */ printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n", @@ -1306,12 +1321,6 @@ err_out: DPRINTK("EXIT, err\n"); } - -static inline u8 ata_dev_knobble(struct ata_port *ap) -{ - return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(ap->device->id))); -} - /** * ata_dev_config - Run device specific handlers and check for * SATA->PATA bridges @@ -1328,8 +1337,7 @@ void ata_dev_config(struct ata_port *ap, printk(KERN_INFO "ata%u(%u): applying bridge limits\n", ap->id, ap->device->devno); ap->udma_mask &= ATA_UDMA5; - ap->host->max_sectors = ATA_MAX_SECTORS; - ap->host->hostt->max_sectors = ATA_MAX_SECTORS; + ata_set_max_sectors(ap, ATA_MAX_SECTORS); ap->device->flags |= ATA_DFLAG_LOCK_SECTORS; } @@ -1352,7 +1360,7 @@ void ata_dev_config(struct ata_port *ap, * Zero on success, non-zero on error. */ -static int ata_bus_probe(struct ata_port *ap) +int ata_bus_probe(struct ata_port *ap) { unsigned int i, found = 0; diff -puN drivers/scsi/libata.h~libata_sas drivers/scsi/libata.h --- linux-2.6/drivers/scsi/libata.h~libata_sas 2005-10-24 15:08:27.000000000 -0500 +++ linux-2.6-bjking1/drivers/scsi/libata.h 2005-10-24 15:08:27.000000000 -0500 @@ -79,6 +79,8 @@ extern void ata_scsi_badcmd(struct scsi_ extern void ata_scsi_rbuf_fill(struct ata_scsi_args *args, unsigned int (*actor) (struct ata_scsi_args *args, u8 *rbuf, unsigned int buflen)); +extern int ata_bus_probe(struct ata_port *ap); +extern int ata_sas_port_init(struct ata_port *ap); static inline void ata_bad_scsiop(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *)) { @@ -90,4 +92,9 @@ static inline void ata_bad_cdb(struct sc ata_scsi_badcmd(cmd, done, 0x24, 0x00); } +static inline u8 ata_dev_knobble(struct ata_port *ap) +{ + return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(ap->device->id))); +} + #endif /* __LIBATA_H__ */ diff -puN include/linux/libata.h~libata_sas include/linux/libata.h --- linux-2.6/include/linux/libata.h~libata_sas 2005-10-24 15:08:27.000000000 -0500 +++ linux-2.6-bjking1/include/linux/libata.h 2005-10-24 15:08:27.000000000 -0500 @@ -401,6 +401,13 @@ extern int ata_scsi_ioctl(struct scsi_de extern int ata_scsi_queuecmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *)); extern int ata_scsi_error(struct Scsi_Host *host); extern int ata_scsi_release(struct Scsi_Host *host); +extern void ata_sas_port_destroy(struct ata_port *ap); +extern struct ata_port *ata_sas_port_alloc(struct pci_dev *pdev, + struct ata_port_info *port_info); +extern int ata_sas_port_init(struct ata_port *ap); +extern int ata_sas_slave_configure(struct scsi_device *sdev, struct ata_port *ap); +extern int ata_sas_queuecmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *), + struct ata_port *ap); extern unsigned int ata_host_intr(struct ata_port *ap, struct ata_queued_cmd *qc); /* * Default driver ops implementations _