148a31030SOndrej Zary /* 248a31030SOndrej Zary * Driver for Western Digital WD7193, WD7197 and WD7296 SCSI cards 348a31030SOndrej Zary * Copyright 2013 Ondrej Zary 448a31030SOndrej Zary * 548a31030SOndrej Zary * Original driver by 648a31030SOndrej Zary * Aaron Dewell <dewell@woods.net> 748a31030SOndrej Zary * Gaerti <Juergen.Gaertner@mbox.si.uni-hannover.de> 848a31030SOndrej Zary * 948a31030SOndrej Zary * HW documentation available in book: 1048a31030SOndrej Zary * 1148a31030SOndrej Zary * SPIDER Command Protocol 1248a31030SOndrej Zary * by Chandru M. Sippy 1348a31030SOndrej Zary * SCSI Storage Products (MCP) 1448a31030SOndrej Zary * Western Digital Corporation 1548a31030SOndrej Zary * 09-15-95 1648a31030SOndrej Zary * 1748a31030SOndrej Zary * http://web.archive.org/web/20070717175254/http://sun1.rrzn.uni-hannover.de/gaertner.juergen/wd719x/Linux/Docu/Spider/ 1848a31030SOndrej Zary */ 1948a31030SOndrej Zary 2048a31030SOndrej Zary /* 2148a31030SOndrej Zary * Driver workflow: 2248a31030SOndrej Zary * 1. SCSI command is transformed to SCB (Spider Control Block) by the 2348a31030SOndrej Zary * queuecommand function. 2448a31030SOndrej Zary * 2. The address of the SCB is stored in a list to be able to access it, if 2548a31030SOndrej Zary * something goes wrong. 2648a31030SOndrej Zary * 3. The address of the SCB is written to the Controller, which loads the SCB 2748a31030SOndrej Zary * via BM-DMA and processes it. 2848a31030SOndrej Zary * 4. After it has finished, it generates an interrupt, and sets registers. 2948a31030SOndrej Zary * 3048a31030SOndrej Zary * flaws: 3148a31030SOndrej Zary * - abort/reset functions 3248a31030SOndrej Zary * 3348a31030SOndrej Zary * ToDo: 3448a31030SOndrej Zary * - tagged queueing 3548a31030SOndrej Zary */ 3648a31030SOndrej Zary 3748a31030SOndrej Zary #include <linux/interrupt.h> 3848a31030SOndrej Zary #include <linux/module.h> 3948a31030SOndrej Zary #include <linux/delay.h> 4048a31030SOndrej Zary #include <linux/pci.h> 4148a31030SOndrej Zary #include <linux/firmware.h> 4248a31030SOndrej Zary #include <linux/eeprom_93cx6.h> 4348a31030SOndrej Zary #include <scsi/scsi_cmnd.h> 4448a31030SOndrej Zary #include <scsi/scsi_device.h> 4548a31030SOndrej Zary #include <scsi/scsi_host.h> 4648a31030SOndrej Zary #include "wd719x.h" 4748a31030SOndrej Zary 4848a31030SOndrej Zary /* low-level register access */ 4948a31030SOndrej Zary static inline u8 wd719x_readb(struct wd719x *wd, u8 reg) 5048a31030SOndrej Zary { 5148a31030SOndrej Zary return ioread8(wd->base + reg); 5248a31030SOndrej Zary } 5348a31030SOndrej Zary 5448a31030SOndrej Zary static inline u32 wd719x_readl(struct wd719x *wd, u8 reg) 5548a31030SOndrej Zary { 5648a31030SOndrej Zary return ioread32(wd->base + reg); 5748a31030SOndrej Zary } 5848a31030SOndrej Zary 5948a31030SOndrej Zary static inline void wd719x_writeb(struct wd719x *wd, u8 reg, u8 val) 6048a31030SOndrej Zary { 6148a31030SOndrej Zary iowrite8(val, wd->base + reg); 6248a31030SOndrej Zary } 6348a31030SOndrej Zary 6448a31030SOndrej Zary static inline void wd719x_writew(struct wd719x *wd, u8 reg, u16 val) 6548a31030SOndrej Zary { 6648a31030SOndrej Zary iowrite16(val, wd->base + reg); 6748a31030SOndrej Zary } 6848a31030SOndrej Zary 6948a31030SOndrej Zary static inline void wd719x_writel(struct wd719x *wd, u8 reg, u32 val) 7048a31030SOndrej Zary { 7148a31030SOndrej Zary iowrite32(val, wd->base + reg); 7248a31030SOndrej Zary } 7348a31030SOndrej Zary 7448a31030SOndrej Zary /* wait until the command register is ready */ 7548a31030SOndrej Zary static inline int wd719x_wait_ready(struct wd719x *wd) 7648a31030SOndrej Zary { 7748a31030SOndrej Zary int i = 0; 7848a31030SOndrej Zary 7948a31030SOndrej Zary do { 8048a31030SOndrej Zary if (wd719x_readb(wd, WD719X_AMR_COMMAND) == WD719X_CMD_READY) 8148a31030SOndrej Zary return 0; 8248a31030SOndrej Zary udelay(1); 8348a31030SOndrej Zary } while (i++ < WD719X_WAIT_FOR_CMD_READY); 8448a31030SOndrej Zary 8548a31030SOndrej Zary dev_err(&wd->pdev->dev, "command register is not ready: 0x%02x\n", 8648a31030SOndrej Zary wd719x_readb(wd, WD719X_AMR_COMMAND)); 8748a31030SOndrej Zary 8848a31030SOndrej Zary return -ETIMEDOUT; 8948a31030SOndrej Zary } 9048a31030SOndrej Zary 9148a31030SOndrej Zary /* poll interrupt status register until command finishes */ 9248a31030SOndrej Zary static inline int wd719x_wait_done(struct wd719x *wd, int timeout) 9348a31030SOndrej Zary { 9448a31030SOndrej Zary u8 status; 9548a31030SOndrej Zary 9648a31030SOndrej Zary while (timeout > 0) { 9748a31030SOndrej Zary status = wd719x_readb(wd, WD719X_AMR_INT_STATUS); 9848a31030SOndrej Zary if (status) 9948a31030SOndrej Zary break; 10048a31030SOndrej Zary timeout--; 10148a31030SOndrej Zary udelay(1); 10248a31030SOndrej Zary } 10348a31030SOndrej Zary 10448a31030SOndrej Zary if (timeout <= 0) { 10548a31030SOndrej Zary dev_err(&wd->pdev->dev, "direct command timed out\n"); 10648a31030SOndrej Zary return -ETIMEDOUT; 10748a31030SOndrej Zary } 10848a31030SOndrej Zary 10948a31030SOndrej Zary if (status != WD719X_INT_NOERRORS) { 11048a31030SOndrej Zary dev_err(&wd->pdev->dev, "direct command failed, status 0x%02x, SUE 0x%02x\n", 11148a31030SOndrej Zary status, wd719x_readb(wd, WD719X_AMR_SCB_ERROR)); 11248a31030SOndrej Zary return -EIO; 11348a31030SOndrej Zary } 11448a31030SOndrej Zary 11548a31030SOndrej Zary return 0; 11648a31030SOndrej Zary } 11748a31030SOndrej Zary 11848a31030SOndrej Zary static int wd719x_direct_cmd(struct wd719x *wd, u8 opcode, u8 dev, u8 lun, 11948a31030SOndrej Zary u8 tag, dma_addr_t data, int timeout) 12048a31030SOndrej Zary { 12148a31030SOndrej Zary int ret = 0; 12248a31030SOndrej Zary 12348a31030SOndrej Zary /* clear interrupt status register (allow command register to clear) */ 12448a31030SOndrej Zary wd719x_writeb(wd, WD719X_AMR_INT_STATUS, WD719X_INT_NONE); 12548a31030SOndrej Zary 12648a31030SOndrej Zary /* Wait for the Command register to become free */ 12748a31030SOndrej Zary if (wd719x_wait_ready(wd)) 12848a31030SOndrej Zary return -ETIMEDOUT; 12948a31030SOndrej Zary 13048a31030SOndrej Zary /* make sure we get NO interrupts */ 13148a31030SOndrej Zary dev |= WD719X_DISABLE_INT; 13248a31030SOndrej Zary wd719x_writeb(wd, WD719X_AMR_CMD_PARAM, dev); 13348a31030SOndrej Zary wd719x_writeb(wd, WD719X_AMR_CMD_PARAM_2, lun); 13448a31030SOndrej Zary wd719x_writeb(wd, WD719X_AMR_CMD_PARAM_3, tag); 13548a31030SOndrej Zary if (data) 13648a31030SOndrej Zary wd719x_writel(wd, WD719X_AMR_SCB_IN, data); 13748a31030SOndrej Zary 13848a31030SOndrej Zary /* clear interrupt status register again */ 13948a31030SOndrej Zary wd719x_writeb(wd, WD719X_AMR_INT_STATUS, WD719X_INT_NONE); 14048a31030SOndrej Zary 14148a31030SOndrej Zary /* Now, write the command */ 14248a31030SOndrej Zary wd719x_writeb(wd, WD719X_AMR_COMMAND, opcode); 14348a31030SOndrej Zary 14448a31030SOndrej Zary if (timeout) /* wait for the command to complete */ 14548a31030SOndrej Zary ret = wd719x_wait_done(wd, timeout); 14648a31030SOndrej Zary 14748a31030SOndrej Zary /* clear interrupt status register (clean up) */ 14848a31030SOndrej Zary if (opcode != WD719X_CMD_READ_FIRMVER) 14948a31030SOndrej Zary wd719x_writeb(wd, WD719X_AMR_INT_STATUS, WD719X_INT_NONE); 15048a31030SOndrej Zary 15148a31030SOndrej Zary return ret; 15248a31030SOndrej Zary } 15348a31030SOndrej Zary 15448a31030SOndrej Zary static void wd719x_destroy(struct wd719x *wd) 15548a31030SOndrej Zary { 15648a31030SOndrej Zary struct wd719x_scb *scb; 15748a31030SOndrej Zary 15848a31030SOndrej Zary /* stop the RISC */ 15948a31030SOndrej Zary if (wd719x_direct_cmd(wd, WD719X_CMD_SLEEP, 0, 0, 0, 0, 16048a31030SOndrej Zary WD719X_WAIT_FOR_RISC)) 16148a31030SOndrej Zary dev_warn(&wd->pdev->dev, "RISC sleep command failed\n"); 16248a31030SOndrej Zary /* disable RISC */ 16348a31030SOndrej Zary wd719x_writeb(wd, WD719X_PCI_MODE_SELECT, 0); 16448a31030SOndrej Zary 16548a31030SOndrej Zary /* free all SCBs */ 16648a31030SOndrej Zary list_for_each_entry(scb, &wd->active_scbs, list) 16748a31030SOndrej Zary pci_free_consistent(wd->pdev, sizeof(struct wd719x_scb), scb, 16848a31030SOndrej Zary scb->phys); 16948a31030SOndrej Zary list_for_each_entry(scb, &wd->free_scbs, list) 17048a31030SOndrej Zary pci_free_consistent(wd->pdev, sizeof(struct wd719x_scb), scb, 17148a31030SOndrej Zary scb->phys); 17248a31030SOndrej Zary /* free internal buffers */ 17348a31030SOndrej Zary pci_free_consistent(wd->pdev, wd->fw_size, wd->fw_virt, wd->fw_phys); 17448a31030SOndrej Zary wd->fw_virt = NULL; 17548a31030SOndrej Zary pci_free_consistent(wd->pdev, WD719X_HASH_TABLE_SIZE, wd->hash_virt, 17648a31030SOndrej Zary wd->hash_phys); 17748a31030SOndrej Zary wd->hash_virt = NULL; 17848a31030SOndrej Zary pci_free_consistent(wd->pdev, sizeof(struct wd719x_host_param), 17948a31030SOndrej Zary wd->params, wd->params_phys); 18048a31030SOndrej Zary wd->params = NULL; 18148a31030SOndrej Zary free_irq(wd->pdev->irq, wd); 18248a31030SOndrej Zary } 18348a31030SOndrej Zary 18448a31030SOndrej Zary /* finish a SCSI command, mark SCB (if any) as free, unmap buffers */ 18548a31030SOndrej Zary static void wd719x_finish_cmd(struct scsi_cmnd *cmd, int result) 18648a31030SOndrej Zary { 18748a31030SOndrej Zary struct wd719x *wd = shost_priv(cmd->device->host); 18848a31030SOndrej Zary struct wd719x_scb *scb = (struct wd719x_scb *) cmd->host_scribble; 18948a31030SOndrej Zary 19048a31030SOndrej Zary if (scb) { 19148a31030SOndrej Zary list_move(&scb->list, &wd->free_scbs); 19248a31030SOndrej Zary dma_unmap_single(&wd->pdev->dev, cmd->SCp.dma_handle, 19348a31030SOndrej Zary SCSI_SENSE_BUFFERSIZE, DMA_FROM_DEVICE); 19448a31030SOndrej Zary scsi_dma_unmap(cmd); 19548a31030SOndrej Zary } 19648a31030SOndrej Zary cmd->result = result << 16; 19748a31030SOndrej Zary cmd->scsi_done(cmd); 19848a31030SOndrej Zary } 19948a31030SOndrej Zary 20048a31030SOndrej Zary /* Build a SCB and send it to the card */ 20148a31030SOndrej Zary static int wd719x_queuecommand(struct Scsi_Host *sh, struct scsi_cmnd *cmd) 20248a31030SOndrej Zary { 20348a31030SOndrej Zary int i, count_sg; 20448a31030SOndrej Zary unsigned long flags; 20548a31030SOndrej Zary struct wd719x_scb *scb; 20648a31030SOndrej Zary struct wd719x *wd = shost_priv(sh); 20748a31030SOndrej Zary dma_addr_t phys; 20848a31030SOndrej Zary 20948a31030SOndrej Zary cmd->host_scribble = NULL; 21048a31030SOndrej Zary 21148a31030SOndrej Zary /* get a free SCB - either from existing ones or allocate a new one */ 21248a31030SOndrej Zary spin_lock_irqsave(wd->sh->host_lock, flags); 21348a31030SOndrej Zary scb = list_first_entry_or_null(&wd->free_scbs, struct wd719x_scb, list); 21448a31030SOndrej Zary if (scb) { 21548a31030SOndrej Zary list_del(&scb->list); 21648a31030SOndrej Zary phys = scb->phys; 21748a31030SOndrej Zary } else { 21848a31030SOndrej Zary spin_unlock_irqrestore(wd->sh->host_lock, flags); 21948a31030SOndrej Zary scb = pci_alloc_consistent(wd->pdev, sizeof(struct wd719x_scb), 22048a31030SOndrej Zary &phys); 22148a31030SOndrej Zary spin_lock_irqsave(wd->sh->host_lock, flags); 22248a31030SOndrej Zary if (!scb) { 22348a31030SOndrej Zary dev_err(&wd->pdev->dev, "unable to allocate SCB\n"); 22448a31030SOndrej Zary wd719x_finish_cmd(cmd, DID_ERROR); 22548a31030SOndrej Zary spin_unlock_irqrestore(wd->sh->host_lock, flags); 22648a31030SOndrej Zary return 0; 22748a31030SOndrej Zary } 22848a31030SOndrej Zary } 22948a31030SOndrej Zary memset(scb, 0, sizeof(struct wd719x_scb)); 23048a31030SOndrej Zary list_add(&scb->list, &wd->active_scbs); 23148a31030SOndrej Zary 23248a31030SOndrej Zary scb->phys = phys; 23348a31030SOndrej Zary scb->cmd = cmd; 23448a31030SOndrej Zary cmd->host_scribble = (char *) scb; 23548a31030SOndrej Zary 23648a31030SOndrej Zary scb->CDB_tag = 0; /* Tagged queueing not supported yet */ 23748a31030SOndrej Zary scb->devid = cmd->device->id; 23848a31030SOndrej Zary scb->lun = cmd->device->lun; 23948a31030SOndrej Zary 24048a31030SOndrej Zary /* copy the command */ 24148a31030SOndrej Zary memcpy(scb->CDB, cmd->cmnd, cmd->cmd_len); 24248a31030SOndrej Zary 24348a31030SOndrej Zary /* map sense buffer */ 24448a31030SOndrej Zary scb->sense_buf_length = SCSI_SENSE_BUFFERSIZE; 24548a31030SOndrej Zary cmd->SCp.dma_handle = dma_map_single(&wd->pdev->dev, cmd->sense_buffer, 24648a31030SOndrej Zary SCSI_SENSE_BUFFERSIZE, DMA_FROM_DEVICE); 24748a31030SOndrej Zary scb->sense_buf = cpu_to_le32(cmd->SCp.dma_handle); 24848a31030SOndrej Zary 24948a31030SOndrej Zary /* request autosense */ 25048a31030SOndrej Zary scb->SCB_options |= WD719X_SCB_FLAGS_AUTO_REQUEST_SENSE; 25148a31030SOndrej Zary 25248a31030SOndrej Zary /* check direction */ 25348a31030SOndrej Zary if (cmd->sc_data_direction == DMA_TO_DEVICE) 25448a31030SOndrej Zary scb->SCB_options |= WD719X_SCB_FLAGS_CHECK_DIRECTION 25548a31030SOndrej Zary | WD719X_SCB_FLAGS_PCI_TO_SCSI; 25648a31030SOndrej Zary else if (cmd->sc_data_direction == DMA_FROM_DEVICE) 25748a31030SOndrej Zary scb->SCB_options |= WD719X_SCB_FLAGS_CHECK_DIRECTION; 25848a31030SOndrej Zary 25948a31030SOndrej Zary /* Scather/gather */ 26048a31030SOndrej Zary count_sg = scsi_dma_map(cmd); 26148a31030SOndrej Zary if (count_sg < 0) { 26248a31030SOndrej Zary wd719x_finish_cmd(cmd, DID_ERROR); 26348a31030SOndrej Zary spin_unlock_irqrestore(wd->sh->host_lock, flags); 26448a31030SOndrej Zary return 0; 26548a31030SOndrej Zary } 26648a31030SOndrej Zary BUG_ON(count_sg > WD719X_SG); 26748a31030SOndrej Zary 26848a31030SOndrej Zary if (count_sg) { 26948a31030SOndrej Zary struct scatterlist *sg; 27048a31030SOndrej Zary 27148a31030SOndrej Zary scb->data_length = cpu_to_le32(count_sg * 27248a31030SOndrej Zary sizeof(struct wd719x_sglist)); 27348a31030SOndrej Zary scb->data_p = cpu_to_le32(scb->phys + 27448a31030SOndrej Zary offsetof(struct wd719x_scb, sg_list)); 27548a31030SOndrej Zary 27648a31030SOndrej Zary scsi_for_each_sg(cmd, sg, count_sg, i) { 27748a31030SOndrej Zary scb->sg_list[i].ptr = cpu_to_le32(sg_dma_address(sg)); 27848a31030SOndrej Zary scb->sg_list[i].length = cpu_to_le32(sg_dma_len(sg)); 27948a31030SOndrej Zary } 28048a31030SOndrej Zary scb->SCB_options |= WD719X_SCB_FLAGS_DO_SCATTER_GATHER; 28148a31030SOndrej Zary } else { /* zero length */ 28248a31030SOndrej Zary scb->data_length = 0; 28348a31030SOndrej Zary scb->data_p = 0; 28448a31030SOndrej Zary } 28548a31030SOndrej Zary 28648a31030SOndrej Zary /* check if the Command register is free */ 28748a31030SOndrej Zary if (wd719x_readb(wd, WD719X_AMR_COMMAND) != WD719X_CMD_READY) { 28848a31030SOndrej Zary spin_unlock_irqrestore(wd->sh->host_lock, flags); 28948a31030SOndrej Zary return SCSI_MLQUEUE_HOST_BUSY; 29048a31030SOndrej Zary } 29148a31030SOndrej Zary 29248a31030SOndrej Zary /* write pointer to the AMR */ 29348a31030SOndrej Zary wd719x_writel(wd, WD719X_AMR_SCB_IN, scb->phys); 29448a31030SOndrej Zary /* send SCB opcode */ 29548a31030SOndrej Zary wd719x_writeb(wd, WD719X_AMR_COMMAND, WD719X_CMD_PROCESS_SCB); 29648a31030SOndrej Zary 29748a31030SOndrej Zary spin_unlock_irqrestore(wd->sh->host_lock, flags); 29848a31030SOndrej Zary 29948a31030SOndrej Zary return 0; 30048a31030SOndrej Zary } 30148a31030SOndrej Zary 30248a31030SOndrej Zary static int wd719x_chip_init(struct wd719x *wd) 30348a31030SOndrej Zary { 30448a31030SOndrej Zary int i, ret; 30548a31030SOndrej Zary u32 risc_init[3]; 30648a31030SOndrej Zary const struct firmware *fw_wcs, *fw_risc; 30748a31030SOndrej Zary const char fwname_wcs[] = "wd719x-wcs.bin"; 30848a31030SOndrej Zary const char fwname_risc[] = "wd719x-risc.bin"; 30948a31030SOndrej Zary 31048a31030SOndrej Zary memset(wd->hash_virt, 0, WD719X_HASH_TABLE_SIZE); 31148a31030SOndrej Zary 31248a31030SOndrej Zary /* WCS (sequencer) firmware */ 31348a31030SOndrej Zary ret = request_firmware(&fw_wcs, fwname_wcs, &wd->pdev->dev); 31448a31030SOndrej Zary if (ret) { 31548a31030SOndrej Zary dev_err(&wd->pdev->dev, "Unable to load firmware %s: %d\n", 31648a31030SOndrej Zary fwname_wcs, ret); 31748a31030SOndrej Zary return ret; 31848a31030SOndrej Zary } 31948a31030SOndrej Zary /* RISC firmware */ 32048a31030SOndrej Zary ret = request_firmware(&fw_risc, fwname_risc, &wd->pdev->dev); 32148a31030SOndrej Zary if (ret) { 32248a31030SOndrej Zary dev_err(&wd->pdev->dev, "Unable to load firmware %s: %d\n", 32348a31030SOndrej Zary fwname_risc, ret); 32448a31030SOndrej Zary release_firmware(fw_wcs); 32548a31030SOndrej Zary return ret; 32648a31030SOndrej Zary } 32748a31030SOndrej Zary wd->fw_size = ALIGN(fw_wcs->size, 4) + fw_risc->size; 32848a31030SOndrej Zary 32948a31030SOndrej Zary if (!wd->fw_virt) 33048a31030SOndrej Zary wd->fw_virt = pci_alloc_consistent(wd->pdev, wd->fw_size, 33148a31030SOndrej Zary &wd->fw_phys); 33248a31030SOndrej Zary if (!wd->fw_virt) { 33348a31030SOndrej Zary ret = -ENOMEM; 33448a31030SOndrej Zary goto wd719x_init_end; 33548a31030SOndrej Zary } 33648a31030SOndrej Zary 33748a31030SOndrej Zary /* make a fresh copy of WCS and RISC code */ 33848a31030SOndrej Zary memcpy(wd->fw_virt, fw_wcs->data, fw_wcs->size); 33948a31030SOndrej Zary memcpy(wd->fw_virt + ALIGN(fw_wcs->size, 4), fw_risc->data, 34048a31030SOndrej Zary fw_risc->size); 34148a31030SOndrej Zary 34248a31030SOndrej Zary /* Reset the Spider Chip and adapter itself */ 34348a31030SOndrej Zary wd719x_writeb(wd, WD719X_PCI_PORT_RESET, WD719X_PCI_RESET); 34448a31030SOndrej Zary udelay(WD719X_WAIT_FOR_RISC); 34548a31030SOndrej Zary /* Clear PIO mode bits set by BIOS */ 34648a31030SOndrej Zary wd719x_writeb(wd, WD719X_AMR_CMD_PARAM, 0); 34748a31030SOndrej Zary /* ensure RISC is not running */ 34848a31030SOndrej Zary wd719x_writeb(wd, WD719X_PCI_MODE_SELECT, 0); 34948a31030SOndrej Zary /* ensure command port is ready */ 35048a31030SOndrej Zary wd719x_writeb(wd, WD719X_AMR_COMMAND, 0); 35148a31030SOndrej Zary if (wd719x_wait_ready(wd)) { 35248a31030SOndrej Zary ret = -ETIMEDOUT; 35348a31030SOndrej Zary goto wd719x_init_end; 35448a31030SOndrej Zary } 35548a31030SOndrej Zary 35648a31030SOndrej Zary /* Transfer the first 2K words of RISC code to kick start the uP */ 35748a31030SOndrej Zary risc_init[0] = wd->fw_phys; /* WCS FW */ 35848a31030SOndrej Zary risc_init[1] = wd->fw_phys + ALIGN(fw_wcs->size, 4); /* RISC FW */ 35948a31030SOndrej Zary risc_init[2] = wd->hash_phys; /* hash table */ 36048a31030SOndrej Zary 36148a31030SOndrej Zary /* clear DMA status */ 36248a31030SOndrej Zary wd719x_writeb(wd, WD719X_PCI_CHANNEL2_3STATUS, 0); 36348a31030SOndrej Zary 36448a31030SOndrej Zary /* address to read firmware from */ 36548a31030SOndrej Zary wd719x_writel(wd, WD719X_PCI_EXTERNAL_ADDR, risc_init[1]); 36648a31030SOndrej Zary /* base address to write firmware to (on card) */ 36748a31030SOndrej Zary wd719x_writew(wd, WD719X_PCI_INTERNAL_ADDR, WD719X_PRAM_BASE_ADDR); 36848a31030SOndrej Zary /* size: first 2K words */ 36948a31030SOndrej Zary wd719x_writew(wd, WD719X_PCI_DMA_TRANSFER_SIZE, 2048 * 2); 37048a31030SOndrej Zary /* start DMA */ 37148a31030SOndrej Zary wd719x_writeb(wd, WD719X_PCI_CHANNEL2_3CMD, WD719X_START_CHANNEL2_3DMA); 37248a31030SOndrej Zary 37348a31030SOndrej Zary /* wait for DMA to complete */ 37448a31030SOndrej Zary i = WD719X_WAIT_FOR_RISC; 37548a31030SOndrej Zary while (i-- > 0) { 37648a31030SOndrej Zary u8 status = wd719x_readb(wd, WD719X_PCI_CHANNEL2_3STATUS); 37748a31030SOndrej Zary if (status == WD719X_START_CHANNEL2_3DONE) 37848a31030SOndrej Zary break; 37948a31030SOndrej Zary if (status == WD719X_START_CHANNEL2_3ABORT) { 38048a31030SOndrej Zary dev_warn(&wd->pdev->dev, "RISC bootstrap failed: DMA aborted\n"); 38148a31030SOndrej Zary ret = -EIO; 38248a31030SOndrej Zary goto wd719x_init_end; 38348a31030SOndrej Zary } 38448a31030SOndrej Zary udelay(1); 38548a31030SOndrej Zary } 38648a31030SOndrej Zary if (i < 1) { 38748a31030SOndrej Zary dev_warn(&wd->pdev->dev, "RISC bootstrap failed: DMA timeout\n"); 38848a31030SOndrej Zary ret = -ETIMEDOUT; 38948a31030SOndrej Zary goto wd719x_init_end; 39048a31030SOndrej Zary } 39148a31030SOndrej Zary 39248a31030SOndrej Zary /* firmware is loaded, now initialize and wake up the RISC */ 39348a31030SOndrej Zary /* write RISC initialization long words to Spider */ 39448a31030SOndrej Zary wd719x_writel(wd, WD719X_AMR_SCB_IN, risc_init[0]); 39548a31030SOndrej Zary wd719x_writel(wd, WD719X_AMR_SCB_IN + 4, risc_init[1]); 39648a31030SOndrej Zary wd719x_writel(wd, WD719X_AMR_SCB_IN + 8, risc_init[2]); 39748a31030SOndrej Zary 39848a31030SOndrej Zary /* disable interrupts during initialization of RISC */ 39948a31030SOndrej Zary wd719x_writeb(wd, WD719X_AMR_CMD_PARAM, WD719X_DISABLE_INT); 40048a31030SOndrej Zary 40148a31030SOndrej Zary /* issue INITIALIZE RISC comand */ 40248a31030SOndrej Zary wd719x_writeb(wd, WD719X_AMR_COMMAND, WD719X_CMD_INIT_RISC); 40348a31030SOndrej Zary /* enable advanced mode (wake up RISC) */ 40448a31030SOndrej Zary wd719x_writeb(wd, WD719X_PCI_MODE_SELECT, WD719X_ENABLE_ADVANCE_MODE); 40548a31030SOndrej Zary udelay(WD719X_WAIT_FOR_RISC); 40648a31030SOndrej Zary 40748a31030SOndrej Zary ret = wd719x_wait_done(wd, WD719X_WAIT_FOR_RISC); 40848a31030SOndrej Zary /* clear interrupt status register */ 40948a31030SOndrej Zary wd719x_writeb(wd, WD719X_AMR_INT_STATUS, WD719X_INT_NONE); 41048a31030SOndrej Zary if (ret) { 41148a31030SOndrej Zary dev_warn(&wd->pdev->dev, "Unable to initialize RISC\n"); 41248a31030SOndrej Zary goto wd719x_init_end; 41348a31030SOndrej Zary } 41448a31030SOndrej Zary /* RISC is up and running */ 41548a31030SOndrej Zary 41648a31030SOndrej Zary /* Read FW version from RISC */ 41748a31030SOndrej Zary ret = wd719x_direct_cmd(wd, WD719X_CMD_READ_FIRMVER, 0, 0, 0, 0, 41848a31030SOndrej Zary WD719X_WAIT_FOR_RISC); 41948a31030SOndrej Zary if (ret) { 42048a31030SOndrej Zary dev_warn(&wd->pdev->dev, "Unable to read firmware version\n"); 42148a31030SOndrej Zary goto wd719x_init_end; 42248a31030SOndrej Zary } 42348a31030SOndrej Zary dev_info(&wd->pdev->dev, "RISC initialized with firmware version %.2x.%.2x\n", 42448a31030SOndrej Zary wd719x_readb(wd, WD719X_AMR_SCB_OUT + 1), 42548a31030SOndrej Zary wd719x_readb(wd, WD719X_AMR_SCB_OUT)); 42648a31030SOndrej Zary 42748a31030SOndrej Zary /* RESET SCSI bus */ 42848a31030SOndrej Zary ret = wd719x_direct_cmd(wd, WD719X_CMD_BUSRESET, 0, 0, 0, 0, 42948a31030SOndrej Zary WD719X_WAIT_FOR_SCSI_RESET); 43048a31030SOndrej Zary if (ret) { 43148a31030SOndrej Zary dev_warn(&wd->pdev->dev, "SCSI bus reset failed\n"); 43248a31030SOndrej Zary goto wd719x_init_end; 43348a31030SOndrej Zary } 43448a31030SOndrej Zary 43548a31030SOndrej Zary /* use HostParameter structure to set Spider's Host Parameter Block */ 43648a31030SOndrej Zary ret = wd719x_direct_cmd(wd, WD719X_CMD_SET_PARAM, 0, 43748a31030SOndrej Zary sizeof(struct wd719x_host_param), 0, 43848a31030SOndrej Zary wd->params_phys, WD719X_WAIT_FOR_RISC); 43948a31030SOndrej Zary if (ret) { 44048a31030SOndrej Zary dev_warn(&wd->pdev->dev, "Failed to set HOST PARAMETERS\n"); 44148a31030SOndrej Zary goto wd719x_init_end; 44248a31030SOndrej Zary } 44348a31030SOndrej Zary 44448a31030SOndrej Zary /* initiate SCAM (does nothing if disabled in BIOS) */ 44548a31030SOndrej Zary /* bug?: we should pass a mask of static IDs which we don't have */ 44648a31030SOndrej Zary ret = wd719x_direct_cmd(wd, WD719X_CMD_INIT_SCAM, 0, 0, 0, 0, 44748a31030SOndrej Zary WD719X_WAIT_FOR_SCSI_RESET); 44848a31030SOndrej Zary if (ret) { 44948a31030SOndrej Zary dev_warn(&wd->pdev->dev, "SCAM initialization failed\n"); 45048a31030SOndrej Zary goto wd719x_init_end; 45148a31030SOndrej Zary } 45248a31030SOndrej Zary 45348a31030SOndrej Zary /* clear AMR_BIOS_SHARE_INT register */ 45448a31030SOndrej Zary wd719x_writeb(wd, WD719X_AMR_BIOS_SHARE_INT, 0); 45548a31030SOndrej Zary 45648a31030SOndrej Zary wd719x_init_end: 45748a31030SOndrej Zary release_firmware(fw_wcs); 45848a31030SOndrej Zary release_firmware(fw_risc); 45948a31030SOndrej Zary 46048a31030SOndrej Zary return ret; 46148a31030SOndrej Zary } 46248a31030SOndrej Zary 46348a31030SOndrej Zary static int wd719x_abort(struct scsi_cmnd *cmd) 46448a31030SOndrej Zary { 46548a31030SOndrej Zary int action, result; 46648a31030SOndrej Zary unsigned long flags; 46748a31030SOndrej Zary struct wd719x_scb *scb = (struct wd719x_scb *)cmd->host_scribble; 46848a31030SOndrej Zary struct wd719x *wd = shost_priv(cmd->device->host); 46948a31030SOndrej Zary 47048a31030SOndrej Zary dev_info(&wd->pdev->dev, "abort command, tag: %x\n", cmd->tag); 47148a31030SOndrej Zary 47248a31030SOndrej Zary action = /*cmd->tag ? WD719X_CMD_ABORT_TAG : */WD719X_CMD_ABORT; 47348a31030SOndrej Zary 47448a31030SOndrej Zary spin_lock_irqsave(wd->sh->host_lock, flags); 47548a31030SOndrej Zary result = wd719x_direct_cmd(wd, action, cmd->device->id, 47648a31030SOndrej Zary cmd->device->lun, cmd->tag, scb->phys, 0); 47748a31030SOndrej Zary spin_unlock_irqrestore(wd->sh->host_lock, flags); 47848a31030SOndrej Zary if (result) 47948a31030SOndrej Zary return FAILED; 48048a31030SOndrej Zary 48148a31030SOndrej Zary return SUCCESS; 48248a31030SOndrej Zary } 48348a31030SOndrej Zary 48448a31030SOndrej Zary static int wd719x_reset(struct scsi_cmnd *cmd, u8 opcode, u8 device) 48548a31030SOndrej Zary { 48648a31030SOndrej Zary int result; 48748a31030SOndrej Zary unsigned long flags; 48848a31030SOndrej Zary struct wd719x *wd = shost_priv(cmd->device->host); 48948a31030SOndrej Zary 49048a31030SOndrej Zary dev_info(&wd->pdev->dev, "%s reset requested\n", 49148a31030SOndrej Zary (opcode == WD719X_CMD_BUSRESET) ? "bus" : "device"); 49248a31030SOndrej Zary 49348a31030SOndrej Zary spin_lock_irqsave(wd->sh->host_lock, flags); 49448a31030SOndrej Zary result = wd719x_direct_cmd(wd, opcode, device, 0, 0, 0, 49548a31030SOndrej Zary WD719X_WAIT_FOR_SCSI_RESET); 49648a31030SOndrej Zary spin_unlock_irqrestore(wd->sh->host_lock, flags); 49748a31030SOndrej Zary if (result) 49848a31030SOndrej Zary return FAILED; 49948a31030SOndrej Zary 50048a31030SOndrej Zary return SUCCESS; 50148a31030SOndrej Zary } 50248a31030SOndrej Zary 50348a31030SOndrej Zary static int wd719x_dev_reset(struct scsi_cmnd *cmd) 50448a31030SOndrej Zary { 50548a31030SOndrej Zary return wd719x_reset(cmd, WD719X_CMD_RESET, cmd->device->id); 50648a31030SOndrej Zary } 50748a31030SOndrej Zary 50848a31030SOndrej Zary static int wd719x_bus_reset(struct scsi_cmnd *cmd) 50948a31030SOndrej Zary { 51048a31030SOndrej Zary return wd719x_reset(cmd, WD719X_CMD_BUSRESET, 0); 51148a31030SOndrej Zary } 51248a31030SOndrej Zary 51348a31030SOndrej Zary static int wd719x_host_reset(struct scsi_cmnd *cmd) 51448a31030SOndrej Zary { 51548a31030SOndrej Zary struct wd719x *wd = shost_priv(cmd->device->host); 51648a31030SOndrej Zary struct wd719x_scb *scb, *tmp; 51748a31030SOndrej Zary unsigned long flags; 51848a31030SOndrej Zary int result; 51948a31030SOndrej Zary 52048a31030SOndrej Zary dev_info(&wd->pdev->dev, "host reset requested\n"); 52148a31030SOndrej Zary spin_lock_irqsave(wd->sh->host_lock, flags); 52248a31030SOndrej Zary /* Try to reinit the RISC */ 52348a31030SOndrej Zary if (wd719x_chip_init(wd) == 0) 52448a31030SOndrej Zary result = SUCCESS; 52548a31030SOndrej Zary else 52648a31030SOndrej Zary result = FAILED; 52748a31030SOndrej Zary 52848a31030SOndrej Zary /* flush all SCBs */ 52948a31030SOndrej Zary list_for_each_entry_safe(scb, tmp, &wd->active_scbs, list) { 53048a31030SOndrej Zary struct scsi_cmnd *tmp_cmd = scb->cmd; 53148a31030SOndrej Zary wd719x_finish_cmd(tmp_cmd, result); 53248a31030SOndrej Zary } 53348a31030SOndrej Zary spin_unlock_irqrestore(wd->sh->host_lock, flags); 53448a31030SOndrej Zary 53548a31030SOndrej Zary return result; 53648a31030SOndrej Zary } 53748a31030SOndrej Zary 53848a31030SOndrej Zary static int wd719x_biosparam(struct scsi_device *sdev, struct block_device *bdev, 53948a31030SOndrej Zary sector_t capacity, int geom[]) 54048a31030SOndrej Zary { 54148a31030SOndrej Zary if (capacity >= 0x200000) { 54248a31030SOndrej Zary geom[0] = 255; /* heads */ 54348a31030SOndrej Zary geom[1] = 63; /* sectors */ 54448a31030SOndrej Zary } else { 54548a31030SOndrej Zary geom[0] = 64; /* heads */ 54648a31030SOndrej Zary geom[1] = 32; /* sectors */ 54748a31030SOndrej Zary } 54848a31030SOndrej Zary geom[2] = sector_div(capacity, geom[0] * geom[1]); /* cylinders */ 54948a31030SOndrej Zary 55048a31030SOndrej Zary return 0; 55148a31030SOndrej Zary } 55248a31030SOndrej Zary 55348a31030SOndrej Zary /* process a SCB-completion interrupt */ 55448a31030SOndrej Zary static inline void wd719x_interrupt_SCB(struct wd719x *wd, 55548a31030SOndrej Zary union wd719x_regs regs, 55648a31030SOndrej Zary struct wd719x_scb *scb) 55748a31030SOndrej Zary { 55848a31030SOndrej Zary struct scsi_cmnd *cmd; 55948a31030SOndrej Zary int result; 56048a31030SOndrej Zary 56148a31030SOndrej Zary /* now have to find result from card */ 56248a31030SOndrej Zary switch (regs.bytes.SUE) { 56348a31030SOndrej Zary case WD719X_SUE_NOERRORS: 56448a31030SOndrej Zary result = DID_OK; 56548a31030SOndrej Zary break; 56648a31030SOndrej Zary case WD719X_SUE_REJECTED: 56748a31030SOndrej Zary dev_err(&wd->pdev->dev, "command rejected\n"); 56848a31030SOndrej Zary result = DID_ERROR; 56948a31030SOndrej Zary break; 57048a31030SOndrej Zary case WD719X_SUE_SCBQFULL: 57148a31030SOndrej Zary dev_err(&wd->pdev->dev, "SCB queue is full\n"); 57248a31030SOndrej Zary result = DID_ERROR; 57348a31030SOndrej Zary break; 57448a31030SOndrej Zary case WD719X_SUE_TERM: 57548a31030SOndrej Zary dev_dbg(&wd->pdev->dev, "SCB terminated by direct command\n"); 57648a31030SOndrej Zary result = DID_ABORT; /* or DID_RESET? */ 57748a31030SOndrej Zary break; 57848a31030SOndrej Zary case WD719X_SUE_CHAN1ABORT: 57948a31030SOndrej Zary case WD719X_SUE_CHAN23ABORT: 58048a31030SOndrej Zary result = DID_ABORT; 58148a31030SOndrej Zary dev_err(&wd->pdev->dev, "DMA abort\n"); 58248a31030SOndrej Zary break; 58348a31030SOndrej Zary case WD719X_SUE_CHAN1PAR: 58448a31030SOndrej Zary case WD719X_SUE_CHAN23PAR: 58548a31030SOndrej Zary result = DID_PARITY; 58648a31030SOndrej Zary dev_err(&wd->pdev->dev, "DMA parity error\n"); 58748a31030SOndrej Zary break; 58848a31030SOndrej Zary case WD719X_SUE_TIMEOUT: 58948a31030SOndrej Zary result = DID_TIME_OUT; 59048a31030SOndrej Zary dev_dbg(&wd->pdev->dev, "selection timeout\n"); 59148a31030SOndrej Zary break; 59248a31030SOndrej Zary case WD719X_SUE_RESET: 593804ff603SMasanari Iida dev_dbg(&wd->pdev->dev, "bus reset occurred\n"); 59448a31030SOndrej Zary result = DID_RESET; 59548a31030SOndrej Zary break; 59648a31030SOndrej Zary case WD719X_SUE_BUSERROR: 59748a31030SOndrej Zary dev_dbg(&wd->pdev->dev, "SCSI bus error\n"); 59848a31030SOndrej Zary result = DID_ERROR; 59948a31030SOndrej Zary break; 60048a31030SOndrej Zary case WD719X_SUE_WRONGWAY: 60148a31030SOndrej Zary dev_err(&wd->pdev->dev, "wrong data transfer direction\n"); 60248a31030SOndrej Zary result = DID_ERROR; 60348a31030SOndrej Zary break; 60448a31030SOndrej Zary case WD719X_SUE_BADPHASE: 60548a31030SOndrej Zary dev_err(&wd->pdev->dev, "invalid SCSI phase\n"); 60648a31030SOndrej Zary result = DID_ERROR; 60748a31030SOndrej Zary break; 60848a31030SOndrej Zary case WD719X_SUE_TOOLONG: 60948a31030SOndrej Zary dev_err(&wd->pdev->dev, "record too long\n"); 61048a31030SOndrej Zary result = DID_ERROR; 61148a31030SOndrej Zary break; 61248a31030SOndrej Zary case WD719X_SUE_BUSFREE: 61348a31030SOndrej Zary dev_err(&wd->pdev->dev, "unexpected bus free\n"); 61448a31030SOndrej Zary result = DID_NO_CONNECT; /* or DID_ERROR ???*/ 61548a31030SOndrej Zary break; 61648a31030SOndrej Zary case WD719X_SUE_ARSDONE: 61748a31030SOndrej Zary dev_dbg(&wd->pdev->dev, "auto request sense\n"); 61848a31030SOndrej Zary if (regs.bytes.SCSI == 0) 61948a31030SOndrej Zary result = DID_OK; 62048a31030SOndrej Zary else 62148a31030SOndrej Zary result = DID_PARITY; 62248a31030SOndrej Zary break; 62348a31030SOndrej Zary case WD719X_SUE_IGNORED: 62448a31030SOndrej Zary dev_err(&wd->pdev->dev, "target id %d ignored command\n", 62548a31030SOndrej Zary scb->cmd->device->id); 62648a31030SOndrej Zary result = DID_NO_CONNECT; 62748a31030SOndrej Zary break; 62848a31030SOndrej Zary case WD719X_SUE_WRONGTAGS: 62948a31030SOndrej Zary dev_err(&wd->pdev->dev, "reversed tags\n"); 63048a31030SOndrej Zary result = DID_ERROR; 63148a31030SOndrej Zary break; 63248a31030SOndrej Zary case WD719X_SUE_BADTAGS: 63348a31030SOndrej Zary dev_err(&wd->pdev->dev, "tag type not supported by target\n"); 63448a31030SOndrej Zary result = DID_ERROR; 63548a31030SOndrej Zary break; 63648a31030SOndrej Zary case WD719X_SUE_NOSCAMID: 63748a31030SOndrej Zary dev_err(&wd->pdev->dev, "no SCAM soft ID available\n"); 63848a31030SOndrej Zary result = DID_ERROR; 63948a31030SOndrej Zary break; 64048a31030SOndrej Zary default: 64148a31030SOndrej Zary dev_warn(&wd->pdev->dev, "unknown SUE error code: 0x%x\n", 64248a31030SOndrej Zary regs.bytes.SUE); 64348a31030SOndrej Zary result = DID_ERROR; 64448a31030SOndrej Zary break; 64548a31030SOndrej Zary } 64648a31030SOndrej Zary cmd = scb->cmd; 64748a31030SOndrej Zary 64848a31030SOndrej Zary wd719x_finish_cmd(cmd, result); 64948a31030SOndrej Zary } 65048a31030SOndrej Zary 65148a31030SOndrej Zary static irqreturn_t wd719x_interrupt(int irq, void *dev_id) 65248a31030SOndrej Zary { 65348a31030SOndrej Zary struct wd719x *wd = dev_id; 65448a31030SOndrej Zary union wd719x_regs regs; 65548a31030SOndrej Zary unsigned long flags; 65648a31030SOndrej Zary u32 SCB_out; 65748a31030SOndrej Zary 65848a31030SOndrej Zary spin_lock_irqsave(wd->sh->host_lock, flags); 65948a31030SOndrej Zary /* read SCB pointer back from card */ 66048a31030SOndrej Zary SCB_out = wd719x_readl(wd, WD719X_AMR_SCB_OUT); 66148a31030SOndrej Zary /* read all status info at once */ 66248a31030SOndrej Zary regs.all = cpu_to_le32(wd719x_readl(wd, WD719X_AMR_OP_CODE)); 66348a31030SOndrej Zary 66448a31030SOndrej Zary switch (regs.bytes.INT) { 66548a31030SOndrej Zary case WD719X_INT_NONE: 66648a31030SOndrej Zary spin_unlock_irqrestore(wd->sh->host_lock, flags); 66748a31030SOndrej Zary return IRQ_NONE; 66848a31030SOndrej Zary case WD719X_INT_LINKNOSTATUS: 66948a31030SOndrej Zary dev_err(&wd->pdev->dev, "linked command completed with no status\n"); 67048a31030SOndrej Zary break; 67148a31030SOndrej Zary case WD719X_INT_BADINT: 67248a31030SOndrej Zary dev_err(&wd->pdev->dev, "unsolicited interrupt\n"); 67348a31030SOndrej Zary break; 67448a31030SOndrej Zary case WD719X_INT_NOERRORS: 67548a31030SOndrej Zary case WD719X_INT_LINKNOERRORS: 67648a31030SOndrej Zary case WD719X_INT_ERRORSLOGGED: 67748a31030SOndrej Zary case WD719X_INT_SPIDERFAILED: 67848a31030SOndrej Zary /* was the cmd completed a direct or SCB command? */ 67948a31030SOndrej Zary if (regs.bytes.OPC == WD719X_CMD_PROCESS_SCB) { 68048a31030SOndrej Zary struct wd719x_scb *scb; 68148a31030SOndrej Zary list_for_each_entry(scb, &wd->active_scbs, list) 68248a31030SOndrej Zary if (SCB_out == scb->phys) 68348a31030SOndrej Zary break; 68448a31030SOndrej Zary if (SCB_out == scb->phys) 68548a31030SOndrej Zary wd719x_interrupt_SCB(wd, regs, scb); 68648a31030SOndrej Zary else 68748a31030SOndrej Zary dev_err(&wd->pdev->dev, "card returned invalid SCB pointer\n"); 68848a31030SOndrej Zary } else 68948a31030SOndrej Zary dev_warn(&wd->pdev->dev, "direct command 0x%x completed\n", 69048a31030SOndrej Zary regs.bytes.OPC); 69148a31030SOndrej Zary break; 69248a31030SOndrej Zary case WD719X_INT_PIOREADY: 69348a31030SOndrej Zary dev_err(&wd->pdev->dev, "card indicates PIO data ready but we never use PIO\n"); 69448a31030SOndrej Zary /* interrupt will not be cleared until all data is read */ 69548a31030SOndrej Zary break; 69648a31030SOndrej Zary default: 69748a31030SOndrej Zary dev_err(&wd->pdev->dev, "unknown interrupt reason: %d\n", 69848a31030SOndrej Zary regs.bytes.INT); 69948a31030SOndrej Zary 70048a31030SOndrej Zary } 70148a31030SOndrej Zary /* clear interrupt so another can happen */ 70248a31030SOndrej Zary wd719x_writeb(wd, WD719X_AMR_INT_STATUS, WD719X_INT_NONE); 70348a31030SOndrej Zary spin_unlock_irqrestore(wd->sh->host_lock, flags); 70448a31030SOndrej Zary 70548a31030SOndrej Zary return IRQ_HANDLED; 70648a31030SOndrej Zary } 70748a31030SOndrej Zary 70848a31030SOndrej Zary static void wd719x_eeprom_reg_read(struct eeprom_93cx6 *eeprom) 70948a31030SOndrej Zary { 71048a31030SOndrej Zary struct wd719x *wd = eeprom->data; 71148a31030SOndrej Zary u8 reg = wd719x_readb(wd, WD719X_PCI_GPIO_DATA); 71248a31030SOndrej Zary 71348a31030SOndrej Zary eeprom->reg_data_out = reg & WD719X_EE_DO; 71448a31030SOndrej Zary } 71548a31030SOndrej Zary 71648a31030SOndrej Zary static void wd719x_eeprom_reg_write(struct eeprom_93cx6 *eeprom) 71748a31030SOndrej Zary { 71848a31030SOndrej Zary struct wd719x *wd = eeprom->data; 71948a31030SOndrej Zary u8 reg = 0; 72048a31030SOndrej Zary 72148a31030SOndrej Zary if (eeprom->reg_data_in) 72248a31030SOndrej Zary reg |= WD719X_EE_DI; 72348a31030SOndrej Zary if (eeprom->reg_data_clock) 72448a31030SOndrej Zary reg |= WD719X_EE_CLK; 72548a31030SOndrej Zary if (eeprom->reg_chip_select) 72648a31030SOndrej Zary reg |= WD719X_EE_CS; 72748a31030SOndrej Zary 72848a31030SOndrej Zary wd719x_writeb(wd, WD719X_PCI_GPIO_DATA, reg); 72948a31030SOndrej Zary } 73048a31030SOndrej Zary 73148a31030SOndrej Zary /* read config from EEPROM so it can be downloaded by the RISC on (re-)init */ 73248a31030SOndrej Zary static void wd719x_read_eeprom(struct wd719x *wd) 73348a31030SOndrej Zary { 73448a31030SOndrej Zary struct eeprom_93cx6 eeprom; 73548a31030SOndrej Zary u8 gpio; 73648a31030SOndrej Zary struct wd719x_eeprom_header header; 73748a31030SOndrej Zary 73848a31030SOndrej Zary eeprom.data = wd; 73948a31030SOndrej Zary eeprom.register_read = wd719x_eeprom_reg_read; 74048a31030SOndrej Zary eeprom.register_write = wd719x_eeprom_reg_write; 74148a31030SOndrej Zary eeprom.width = PCI_EEPROM_WIDTH_93C46; 74248a31030SOndrej Zary 74348a31030SOndrej Zary /* set all outputs to low */ 74448a31030SOndrej Zary wd719x_writeb(wd, WD719X_PCI_GPIO_DATA, 0); 74548a31030SOndrej Zary /* configure GPIO pins */ 74648a31030SOndrej Zary gpio = wd719x_readb(wd, WD719X_PCI_GPIO_CONTROL); 74748a31030SOndrej Zary /* GPIO outputs */ 74848a31030SOndrej Zary gpio &= (~(WD719X_EE_CLK | WD719X_EE_DI | WD719X_EE_CS)); 74948a31030SOndrej Zary /* GPIO input */ 75048a31030SOndrej Zary gpio |= WD719X_EE_DO; 75148a31030SOndrej Zary wd719x_writeb(wd, WD719X_PCI_GPIO_CONTROL, gpio); 75248a31030SOndrej Zary 75348a31030SOndrej Zary /* read EEPROM header */ 75448a31030SOndrej Zary eeprom_93cx6_multireadb(&eeprom, 0, (u8 *)&header, sizeof(header)); 75548a31030SOndrej Zary 75648a31030SOndrej Zary if (header.sig1 == 'W' && header.sig2 == 'D') 75748a31030SOndrej Zary eeprom_93cx6_multireadb(&eeprom, header.cfg_offset, 75848a31030SOndrej Zary (u8 *)wd->params, 75948a31030SOndrej Zary sizeof(struct wd719x_host_param)); 76048a31030SOndrej Zary else { /* default EEPROM values */ 76148a31030SOndrej Zary dev_warn(&wd->pdev->dev, "EEPROM signature is invalid (0x%02x 0x%02x), using default values\n", 76248a31030SOndrej Zary header.sig1, header.sig2); 76348a31030SOndrej Zary wd->params->ch_1_th = 0x10; /* 16 DWs = 64 B */ 76448a31030SOndrej Zary wd->params->scsi_conf = 0x4c; /* 48ma, spue, parity check */ 76548a31030SOndrej Zary wd->params->own_scsi_id = 0x07; /* ID 7, SCAM disabled */ 76648a31030SOndrej Zary wd->params->sel_timeout = 0x4d; /* 250 ms */ 76748a31030SOndrej Zary wd->params->sleep_timer = 0x01; 76848a31030SOndrej Zary wd->params->cdb_size = cpu_to_le16(0x5555); /* all 6 B */ 76948a31030SOndrej Zary wd->params->scsi_pad = 0x1b; 77048a31030SOndrej Zary if (wd->type == WD719X_TYPE_7193) /* narrow card - disable */ 77148a31030SOndrej Zary wd->params->wide = cpu_to_le32(0x00000000); 77248a31030SOndrej Zary else /* initiate & respond to WIDE messages */ 77348a31030SOndrej Zary wd->params->wide = cpu_to_le32(0xffffffff); 77448a31030SOndrej Zary wd->params->sync = cpu_to_le32(0xffffffff); 77548a31030SOndrej Zary wd->params->soft_mask = 0x00; /* all disabled */ 77648a31030SOndrej Zary wd->params->unsol_mask = 0x00; /* all disabled */ 77748a31030SOndrej Zary } 77848a31030SOndrej Zary /* disable TAGGED messages */ 77948a31030SOndrej Zary wd->params->tag_en = cpu_to_le16(0x0000); 78048a31030SOndrej Zary } 78148a31030SOndrej Zary 78248a31030SOndrej Zary /* Read card type from GPIO bits 1 and 3 */ 78348a31030SOndrej Zary static enum wd719x_card_type wd719x_detect_type(struct wd719x *wd) 78448a31030SOndrej Zary { 78548a31030SOndrej Zary u8 card = wd719x_readb(wd, WD719X_PCI_GPIO_CONTROL); 78648a31030SOndrej Zary 78748a31030SOndrej Zary card |= WD719X_GPIO_ID_BITS; 78848a31030SOndrej Zary wd719x_writeb(wd, WD719X_PCI_GPIO_CONTROL, card); 78948a31030SOndrej Zary card = wd719x_readb(wd, WD719X_PCI_GPIO_DATA) & WD719X_GPIO_ID_BITS; 79048a31030SOndrej Zary switch (card) { 79148a31030SOndrej Zary case 0x08: 79248a31030SOndrej Zary return WD719X_TYPE_7193; 79348a31030SOndrej Zary case 0x02: 79448a31030SOndrej Zary return WD719X_TYPE_7197; 79548a31030SOndrej Zary case 0x00: 79648a31030SOndrej Zary return WD719X_TYPE_7296; 79748a31030SOndrej Zary default: 79848a31030SOndrej Zary dev_warn(&wd->pdev->dev, "unknown card type 0x%x\n", card); 79948a31030SOndrej Zary return WD719X_TYPE_UNKNOWN; 80048a31030SOndrej Zary } 80148a31030SOndrej Zary } 80248a31030SOndrej Zary 80348a31030SOndrej Zary static int wd719x_board_found(struct Scsi_Host *sh) 80448a31030SOndrej Zary { 80548a31030SOndrej Zary struct wd719x *wd = shost_priv(sh); 80648a31030SOndrej Zary char *card_types[] = { "Unknown card", "WD7193", "WD7197", "WD7296" }; 80748a31030SOndrej Zary int ret; 80848a31030SOndrej Zary 80948a31030SOndrej Zary INIT_LIST_HEAD(&wd->active_scbs); 81048a31030SOndrej Zary INIT_LIST_HEAD(&wd->free_scbs); 81148a31030SOndrej Zary 81248a31030SOndrej Zary sh->base = pci_resource_start(wd->pdev, 0); 81348a31030SOndrej Zary 81448a31030SOndrej Zary wd->type = wd719x_detect_type(wd); 81548a31030SOndrej Zary 81648a31030SOndrej Zary wd->sh = sh; 81748a31030SOndrej Zary sh->irq = wd->pdev->irq; 81848a31030SOndrej Zary wd->fw_virt = NULL; 81948a31030SOndrej Zary 82048a31030SOndrej Zary /* memory area for host (EEPROM) parameters */ 82148a31030SOndrej Zary wd->params = pci_alloc_consistent(wd->pdev, 82248a31030SOndrej Zary sizeof(struct wd719x_host_param), 82348a31030SOndrej Zary &wd->params_phys); 82448a31030SOndrej Zary if (!wd->params) { 82548a31030SOndrej Zary dev_warn(&wd->pdev->dev, "unable to allocate parameter buffer\n"); 82648a31030SOndrej Zary return -ENOMEM; 82748a31030SOndrej Zary } 82848a31030SOndrej Zary 82948a31030SOndrej Zary /* memory area for the RISC for hash table of outstanding requests */ 83048a31030SOndrej Zary wd->hash_virt = pci_alloc_consistent(wd->pdev, WD719X_HASH_TABLE_SIZE, 83148a31030SOndrej Zary &wd->hash_phys); 83248a31030SOndrej Zary if (!wd->hash_virt) { 83348a31030SOndrej Zary dev_warn(&wd->pdev->dev, "unable to allocate hash buffer\n"); 83448a31030SOndrej Zary ret = -ENOMEM; 83548a31030SOndrej Zary goto fail_free_params; 83648a31030SOndrej Zary } 83748a31030SOndrej Zary 83848a31030SOndrej Zary ret = request_irq(wd->pdev->irq, wd719x_interrupt, IRQF_SHARED, 83948a31030SOndrej Zary "wd719x", wd); 84048a31030SOndrej Zary if (ret) { 84148a31030SOndrej Zary dev_warn(&wd->pdev->dev, "unable to assign IRQ %d\n", 84248a31030SOndrej Zary wd->pdev->irq); 84348a31030SOndrej Zary goto fail_free_hash; 84448a31030SOndrej Zary } 84548a31030SOndrej Zary 84648a31030SOndrej Zary /* read parameters from EEPROM */ 84748a31030SOndrej Zary wd719x_read_eeprom(wd); 84848a31030SOndrej Zary 84948a31030SOndrej Zary ret = wd719x_chip_init(wd); 85048a31030SOndrej Zary if (ret) 85148a31030SOndrej Zary goto fail_free_irq; 85248a31030SOndrej Zary 85348a31030SOndrej Zary sh->this_id = wd->params->own_scsi_id & WD719X_EE_SCSI_ID_MASK; 85448a31030SOndrej Zary 85548a31030SOndrej Zary dev_info(&wd->pdev->dev, "%s at I/O 0x%lx, IRQ %u, SCSI ID %d\n", 85648a31030SOndrej Zary card_types[wd->type], sh->base, sh->irq, sh->this_id); 85748a31030SOndrej Zary 85848a31030SOndrej Zary return 0; 85948a31030SOndrej Zary 86048a31030SOndrej Zary fail_free_irq: 86148a31030SOndrej Zary free_irq(wd->pdev->irq, wd); 86248a31030SOndrej Zary fail_free_hash: 86348a31030SOndrej Zary pci_free_consistent(wd->pdev, WD719X_HASH_TABLE_SIZE, wd->hash_virt, 86448a31030SOndrej Zary wd->hash_phys); 86548a31030SOndrej Zary fail_free_params: 86648a31030SOndrej Zary pci_free_consistent(wd->pdev, sizeof(struct wd719x_host_param), 86748a31030SOndrej Zary wd->params, wd->params_phys); 86848a31030SOndrej Zary 86948a31030SOndrej Zary return ret; 87048a31030SOndrej Zary } 87148a31030SOndrej Zary 87248a31030SOndrej Zary static struct scsi_host_template wd719x_template = { 8732ecf8e0aSOndrej Zary .module = THIS_MODULE, 87448a31030SOndrej Zary .name = "Western Digital 719x", 87548a31030SOndrej Zary .queuecommand = wd719x_queuecommand, 87648a31030SOndrej Zary .eh_abort_handler = wd719x_abort, 87748a31030SOndrej Zary .eh_device_reset_handler = wd719x_dev_reset, 87848a31030SOndrej Zary .eh_bus_reset_handler = wd719x_bus_reset, 87948a31030SOndrej Zary .eh_host_reset_handler = wd719x_host_reset, 88048a31030SOndrej Zary .bios_param = wd719x_biosparam, 88148a31030SOndrej Zary .proc_name = "wd719x", 88248a31030SOndrej Zary .can_queue = 255, 88348a31030SOndrej Zary .this_id = 7, 88448a31030SOndrej Zary .sg_tablesize = WD719X_SG, 88548a31030SOndrej Zary .use_clustering = ENABLE_CLUSTERING, 88648a31030SOndrej Zary }; 88748a31030SOndrej Zary 88848a31030SOndrej Zary static int wd719x_pci_probe(struct pci_dev *pdev, const struct pci_device_id *d) 88948a31030SOndrej Zary { 89048a31030SOndrej Zary int err; 89148a31030SOndrej Zary struct Scsi_Host *sh; 89248a31030SOndrej Zary struct wd719x *wd; 89348a31030SOndrej Zary 89448a31030SOndrej Zary err = pci_enable_device(pdev); 89548a31030SOndrej Zary if (err) 89648a31030SOndrej Zary goto fail; 89748a31030SOndrej Zary 89848a31030SOndrej Zary if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32))) { 89948a31030SOndrej Zary dev_warn(&pdev->dev, "Unable to set 32-bit DMA mask\n"); 90048a31030SOndrej Zary goto disable_device; 90148a31030SOndrej Zary } 90248a31030SOndrej Zary 90348a31030SOndrej Zary err = pci_request_regions(pdev, "wd719x"); 90448a31030SOndrej Zary if (err) 90548a31030SOndrej Zary goto disable_device; 90648a31030SOndrej Zary pci_set_master(pdev); 90748a31030SOndrej Zary 90848a31030SOndrej Zary err = -ENODEV; 90948a31030SOndrej Zary if (pci_resource_len(pdev, 0) == 0) 91048a31030SOndrej Zary goto release_region; 91148a31030SOndrej Zary 91248a31030SOndrej Zary err = -ENOMEM; 91348a31030SOndrej Zary sh = scsi_host_alloc(&wd719x_template, sizeof(struct wd719x)); 91448a31030SOndrej Zary if (!sh) 91548a31030SOndrej Zary goto release_region; 91648a31030SOndrej Zary 91748a31030SOndrej Zary wd = shost_priv(sh); 91848a31030SOndrej Zary wd->base = pci_iomap(pdev, 0, 0); 91948a31030SOndrej Zary if (!wd->base) 92048a31030SOndrej Zary goto free_host; 92148a31030SOndrej Zary wd->pdev = pdev; 92248a31030SOndrej Zary 92348a31030SOndrej Zary err = wd719x_board_found(sh); 92448a31030SOndrej Zary if (err) 92548a31030SOndrej Zary goto unmap; 92648a31030SOndrej Zary 92748a31030SOndrej Zary err = scsi_add_host(sh, &wd->pdev->dev); 92848a31030SOndrej Zary if (err) 92948a31030SOndrej Zary goto destroy; 93048a31030SOndrej Zary 93148a31030SOndrej Zary scsi_scan_host(sh); 93248a31030SOndrej Zary 93348a31030SOndrej Zary pci_set_drvdata(pdev, sh); 93448a31030SOndrej Zary return 0; 93548a31030SOndrej Zary 93648a31030SOndrej Zary destroy: 93748a31030SOndrej Zary wd719x_destroy(wd); 93848a31030SOndrej Zary unmap: 93948a31030SOndrej Zary pci_iounmap(pdev, wd->base); 94048a31030SOndrej Zary free_host: 94148a31030SOndrej Zary scsi_host_put(sh); 94248a31030SOndrej Zary release_region: 94348a31030SOndrej Zary pci_release_regions(pdev); 94448a31030SOndrej Zary disable_device: 94548a31030SOndrej Zary pci_disable_device(pdev); 94648a31030SOndrej Zary fail: 94748a31030SOndrej Zary return err; 94848a31030SOndrej Zary } 94948a31030SOndrej Zary 95048a31030SOndrej Zary 95148a31030SOndrej Zary static void wd719x_pci_remove(struct pci_dev *pdev) 95248a31030SOndrej Zary { 95348a31030SOndrej Zary struct Scsi_Host *sh = pci_get_drvdata(pdev); 95448a31030SOndrej Zary struct wd719x *wd = shost_priv(sh); 95548a31030SOndrej Zary 95648a31030SOndrej Zary scsi_remove_host(sh); 95748a31030SOndrej Zary wd719x_destroy(wd); 95848a31030SOndrej Zary pci_iounmap(pdev, wd->base); 95948a31030SOndrej Zary pci_release_regions(pdev); 96048a31030SOndrej Zary pci_disable_device(pdev); 96148a31030SOndrej Zary 96248a31030SOndrej Zary scsi_host_put(sh); 96348a31030SOndrej Zary } 96448a31030SOndrej Zary 965*8a793beaSJoe Perches static const struct pci_device_id wd719x_pci_table[] = { 96648a31030SOndrej Zary { PCI_DEVICE(PCI_VENDOR_ID_WD, 0x3296) }, 96748a31030SOndrej Zary {} 96848a31030SOndrej Zary }; 96948a31030SOndrej Zary 97048a31030SOndrej Zary MODULE_DEVICE_TABLE(pci, wd719x_pci_table); 97148a31030SOndrej Zary 97248a31030SOndrej Zary static struct pci_driver wd719x_pci_driver = { 97348a31030SOndrej Zary .name = "wd719x", 97448a31030SOndrej Zary .id_table = wd719x_pci_table, 97548a31030SOndrej Zary .probe = wd719x_pci_probe, 97648a31030SOndrej Zary .remove = wd719x_pci_remove, 97748a31030SOndrej Zary }; 97848a31030SOndrej Zary 97948a31030SOndrej Zary static int __init wd719x_init(void) 98048a31030SOndrej Zary { 98148a31030SOndrej Zary return pci_register_driver(&wd719x_pci_driver); 98248a31030SOndrej Zary } 98348a31030SOndrej Zary 98448a31030SOndrej Zary static void __exit wd719x_exit(void) 98548a31030SOndrej Zary { 98648a31030SOndrej Zary pci_unregister_driver(&wd719x_pci_driver); 98748a31030SOndrej Zary } 98848a31030SOndrej Zary 98948a31030SOndrej Zary module_init(wd719x_init); 99048a31030SOndrej Zary module_exit(wd719x_exit); 99148a31030SOndrej Zary 99248a31030SOndrej Zary MODULE_DESCRIPTION("Western Digital WD7193/7197/7296 SCSI driver"); 99348a31030SOndrej Zary MODULE_AUTHOR("Ondrej Zary, Aaron Dewell, Juergen Gaertner"); 99448a31030SOndrej Zary MODULE_LICENSE("GPL"); 99548a31030SOndrej Zary MODULE_FIRMWARE("wd719x-wcs.bin"); 99648a31030SOndrej Zary MODULE_FIRMWARE("wd719x-risc.bin"); 997