1*48a31030SOndrej Zary /* 2*48a31030SOndrej Zary * Driver for Western Digital WD7193, WD7197 and WD7296 SCSI cards 3*48a31030SOndrej Zary * Copyright 2013 Ondrej Zary 4*48a31030SOndrej Zary * 5*48a31030SOndrej Zary * Original driver by 6*48a31030SOndrej Zary * Aaron Dewell <dewell@woods.net> 7*48a31030SOndrej Zary * Gaerti <Juergen.Gaertner@mbox.si.uni-hannover.de> 8*48a31030SOndrej Zary * 9*48a31030SOndrej Zary * HW documentation available in book: 10*48a31030SOndrej Zary * 11*48a31030SOndrej Zary * SPIDER Command Protocol 12*48a31030SOndrej Zary * by Chandru M. Sippy 13*48a31030SOndrej Zary * SCSI Storage Products (MCP) 14*48a31030SOndrej Zary * Western Digital Corporation 15*48a31030SOndrej Zary * 09-15-95 16*48a31030SOndrej Zary * 17*48a31030SOndrej Zary * http://web.archive.org/web/20070717175254/http://sun1.rrzn.uni-hannover.de/gaertner.juergen/wd719x/Linux/Docu/Spider/ 18*48a31030SOndrej Zary */ 19*48a31030SOndrej Zary 20*48a31030SOndrej Zary /* 21*48a31030SOndrej Zary * Driver workflow: 22*48a31030SOndrej Zary * 1. SCSI command is transformed to SCB (Spider Control Block) by the 23*48a31030SOndrej Zary * queuecommand function. 24*48a31030SOndrej Zary * 2. The address of the SCB is stored in a list to be able to access it, if 25*48a31030SOndrej Zary * something goes wrong. 26*48a31030SOndrej Zary * 3. The address of the SCB is written to the Controller, which loads the SCB 27*48a31030SOndrej Zary * via BM-DMA and processes it. 28*48a31030SOndrej Zary * 4. After it has finished, it generates an interrupt, and sets registers. 29*48a31030SOndrej Zary * 30*48a31030SOndrej Zary * flaws: 31*48a31030SOndrej Zary * - abort/reset functions 32*48a31030SOndrej Zary * 33*48a31030SOndrej Zary * ToDo: 34*48a31030SOndrej Zary * - tagged queueing 35*48a31030SOndrej Zary */ 36*48a31030SOndrej Zary 37*48a31030SOndrej Zary #include <linux/interrupt.h> 38*48a31030SOndrej Zary #include <linux/module.h> 39*48a31030SOndrej Zary #include <linux/delay.h> 40*48a31030SOndrej Zary #include <linux/pci.h> 41*48a31030SOndrej Zary #include <linux/firmware.h> 42*48a31030SOndrej Zary #include <linux/eeprom_93cx6.h> 43*48a31030SOndrej Zary #include <scsi/scsi_cmnd.h> 44*48a31030SOndrej Zary #include <scsi/scsi_device.h> 45*48a31030SOndrej Zary #include <scsi/scsi_host.h> 46*48a31030SOndrej Zary #include "wd719x.h" 47*48a31030SOndrej Zary 48*48a31030SOndrej Zary /* low-level register access */ 49*48a31030SOndrej Zary static inline u8 wd719x_readb(struct wd719x *wd, u8 reg) 50*48a31030SOndrej Zary { 51*48a31030SOndrej Zary return ioread8(wd->base + reg); 52*48a31030SOndrej Zary } 53*48a31030SOndrej Zary 54*48a31030SOndrej Zary static inline u32 wd719x_readl(struct wd719x *wd, u8 reg) 55*48a31030SOndrej Zary { 56*48a31030SOndrej Zary return ioread32(wd->base + reg); 57*48a31030SOndrej Zary } 58*48a31030SOndrej Zary 59*48a31030SOndrej Zary static inline void wd719x_writeb(struct wd719x *wd, u8 reg, u8 val) 60*48a31030SOndrej Zary { 61*48a31030SOndrej Zary iowrite8(val, wd->base + reg); 62*48a31030SOndrej Zary } 63*48a31030SOndrej Zary 64*48a31030SOndrej Zary static inline void wd719x_writew(struct wd719x *wd, u8 reg, u16 val) 65*48a31030SOndrej Zary { 66*48a31030SOndrej Zary iowrite16(val, wd->base + reg); 67*48a31030SOndrej Zary } 68*48a31030SOndrej Zary 69*48a31030SOndrej Zary static inline void wd719x_writel(struct wd719x *wd, u8 reg, u32 val) 70*48a31030SOndrej Zary { 71*48a31030SOndrej Zary iowrite32(val, wd->base + reg); 72*48a31030SOndrej Zary } 73*48a31030SOndrej Zary 74*48a31030SOndrej Zary /* wait until the command register is ready */ 75*48a31030SOndrej Zary static inline int wd719x_wait_ready(struct wd719x *wd) 76*48a31030SOndrej Zary { 77*48a31030SOndrej Zary int i = 0; 78*48a31030SOndrej Zary 79*48a31030SOndrej Zary do { 80*48a31030SOndrej Zary if (wd719x_readb(wd, WD719X_AMR_COMMAND) == WD719X_CMD_READY) 81*48a31030SOndrej Zary return 0; 82*48a31030SOndrej Zary udelay(1); 83*48a31030SOndrej Zary } while (i++ < WD719X_WAIT_FOR_CMD_READY); 84*48a31030SOndrej Zary 85*48a31030SOndrej Zary dev_err(&wd->pdev->dev, "command register is not ready: 0x%02x\n", 86*48a31030SOndrej Zary wd719x_readb(wd, WD719X_AMR_COMMAND)); 87*48a31030SOndrej Zary 88*48a31030SOndrej Zary return -ETIMEDOUT; 89*48a31030SOndrej Zary } 90*48a31030SOndrej Zary 91*48a31030SOndrej Zary /* poll interrupt status register until command finishes */ 92*48a31030SOndrej Zary static inline int wd719x_wait_done(struct wd719x *wd, int timeout) 93*48a31030SOndrej Zary { 94*48a31030SOndrej Zary u8 status; 95*48a31030SOndrej Zary 96*48a31030SOndrej Zary while (timeout > 0) { 97*48a31030SOndrej Zary status = wd719x_readb(wd, WD719X_AMR_INT_STATUS); 98*48a31030SOndrej Zary if (status) 99*48a31030SOndrej Zary break; 100*48a31030SOndrej Zary timeout--; 101*48a31030SOndrej Zary udelay(1); 102*48a31030SOndrej Zary } 103*48a31030SOndrej Zary 104*48a31030SOndrej Zary if (timeout <= 0) { 105*48a31030SOndrej Zary dev_err(&wd->pdev->dev, "direct command timed out\n"); 106*48a31030SOndrej Zary return -ETIMEDOUT; 107*48a31030SOndrej Zary } 108*48a31030SOndrej Zary 109*48a31030SOndrej Zary if (status != WD719X_INT_NOERRORS) { 110*48a31030SOndrej Zary dev_err(&wd->pdev->dev, "direct command failed, status 0x%02x, SUE 0x%02x\n", 111*48a31030SOndrej Zary status, wd719x_readb(wd, WD719X_AMR_SCB_ERROR)); 112*48a31030SOndrej Zary return -EIO; 113*48a31030SOndrej Zary } 114*48a31030SOndrej Zary 115*48a31030SOndrej Zary return 0; 116*48a31030SOndrej Zary } 117*48a31030SOndrej Zary 118*48a31030SOndrej Zary static int wd719x_direct_cmd(struct wd719x *wd, u8 opcode, u8 dev, u8 lun, 119*48a31030SOndrej Zary u8 tag, dma_addr_t data, int timeout) 120*48a31030SOndrej Zary { 121*48a31030SOndrej Zary int ret = 0; 122*48a31030SOndrej Zary 123*48a31030SOndrej Zary /* clear interrupt status register (allow command register to clear) */ 124*48a31030SOndrej Zary wd719x_writeb(wd, WD719X_AMR_INT_STATUS, WD719X_INT_NONE); 125*48a31030SOndrej Zary 126*48a31030SOndrej Zary /* Wait for the Command register to become free */ 127*48a31030SOndrej Zary if (wd719x_wait_ready(wd)) 128*48a31030SOndrej Zary return -ETIMEDOUT; 129*48a31030SOndrej Zary 130*48a31030SOndrej Zary /* make sure we get NO interrupts */ 131*48a31030SOndrej Zary dev |= WD719X_DISABLE_INT; 132*48a31030SOndrej Zary wd719x_writeb(wd, WD719X_AMR_CMD_PARAM, dev); 133*48a31030SOndrej Zary wd719x_writeb(wd, WD719X_AMR_CMD_PARAM_2, lun); 134*48a31030SOndrej Zary wd719x_writeb(wd, WD719X_AMR_CMD_PARAM_3, tag); 135*48a31030SOndrej Zary if (data) 136*48a31030SOndrej Zary wd719x_writel(wd, WD719X_AMR_SCB_IN, data); 137*48a31030SOndrej Zary 138*48a31030SOndrej Zary /* clear interrupt status register again */ 139*48a31030SOndrej Zary wd719x_writeb(wd, WD719X_AMR_INT_STATUS, WD719X_INT_NONE); 140*48a31030SOndrej Zary 141*48a31030SOndrej Zary /* Now, write the command */ 142*48a31030SOndrej Zary wd719x_writeb(wd, WD719X_AMR_COMMAND, opcode); 143*48a31030SOndrej Zary 144*48a31030SOndrej Zary if (timeout) /* wait for the command to complete */ 145*48a31030SOndrej Zary ret = wd719x_wait_done(wd, timeout); 146*48a31030SOndrej Zary 147*48a31030SOndrej Zary /* clear interrupt status register (clean up) */ 148*48a31030SOndrej Zary if (opcode != WD719X_CMD_READ_FIRMVER) 149*48a31030SOndrej Zary wd719x_writeb(wd, WD719X_AMR_INT_STATUS, WD719X_INT_NONE); 150*48a31030SOndrej Zary 151*48a31030SOndrej Zary return ret; 152*48a31030SOndrej Zary } 153*48a31030SOndrej Zary 154*48a31030SOndrej Zary static void wd719x_destroy(struct wd719x *wd) 155*48a31030SOndrej Zary { 156*48a31030SOndrej Zary struct wd719x_scb *scb; 157*48a31030SOndrej Zary 158*48a31030SOndrej Zary /* stop the RISC */ 159*48a31030SOndrej Zary if (wd719x_direct_cmd(wd, WD719X_CMD_SLEEP, 0, 0, 0, 0, 160*48a31030SOndrej Zary WD719X_WAIT_FOR_RISC)) 161*48a31030SOndrej Zary dev_warn(&wd->pdev->dev, "RISC sleep command failed\n"); 162*48a31030SOndrej Zary /* disable RISC */ 163*48a31030SOndrej Zary wd719x_writeb(wd, WD719X_PCI_MODE_SELECT, 0); 164*48a31030SOndrej Zary 165*48a31030SOndrej Zary /* free all SCBs */ 166*48a31030SOndrej Zary list_for_each_entry(scb, &wd->active_scbs, list) 167*48a31030SOndrej Zary pci_free_consistent(wd->pdev, sizeof(struct wd719x_scb), scb, 168*48a31030SOndrej Zary scb->phys); 169*48a31030SOndrej Zary list_for_each_entry(scb, &wd->free_scbs, list) 170*48a31030SOndrej Zary pci_free_consistent(wd->pdev, sizeof(struct wd719x_scb), scb, 171*48a31030SOndrej Zary scb->phys); 172*48a31030SOndrej Zary /* free internal buffers */ 173*48a31030SOndrej Zary pci_free_consistent(wd->pdev, wd->fw_size, wd->fw_virt, wd->fw_phys); 174*48a31030SOndrej Zary wd->fw_virt = NULL; 175*48a31030SOndrej Zary pci_free_consistent(wd->pdev, WD719X_HASH_TABLE_SIZE, wd->hash_virt, 176*48a31030SOndrej Zary wd->hash_phys); 177*48a31030SOndrej Zary wd->hash_virt = NULL; 178*48a31030SOndrej Zary pci_free_consistent(wd->pdev, sizeof(struct wd719x_host_param), 179*48a31030SOndrej Zary wd->params, wd->params_phys); 180*48a31030SOndrej Zary wd->params = NULL; 181*48a31030SOndrej Zary free_irq(wd->pdev->irq, wd); 182*48a31030SOndrej Zary } 183*48a31030SOndrej Zary 184*48a31030SOndrej Zary /* finish a SCSI command, mark SCB (if any) as free, unmap buffers */ 185*48a31030SOndrej Zary static void wd719x_finish_cmd(struct scsi_cmnd *cmd, int result) 186*48a31030SOndrej Zary { 187*48a31030SOndrej Zary struct wd719x *wd = shost_priv(cmd->device->host); 188*48a31030SOndrej Zary struct wd719x_scb *scb = (struct wd719x_scb *) cmd->host_scribble; 189*48a31030SOndrej Zary 190*48a31030SOndrej Zary if (scb) { 191*48a31030SOndrej Zary list_move(&scb->list, &wd->free_scbs); 192*48a31030SOndrej Zary dma_unmap_single(&wd->pdev->dev, cmd->SCp.dma_handle, 193*48a31030SOndrej Zary SCSI_SENSE_BUFFERSIZE, DMA_FROM_DEVICE); 194*48a31030SOndrej Zary scsi_dma_unmap(cmd); 195*48a31030SOndrej Zary } 196*48a31030SOndrej Zary cmd->result = result << 16; 197*48a31030SOndrej Zary cmd->scsi_done(cmd); 198*48a31030SOndrej Zary } 199*48a31030SOndrej Zary 200*48a31030SOndrej Zary /* Build a SCB and send it to the card */ 201*48a31030SOndrej Zary static int wd719x_queuecommand(struct Scsi_Host *sh, struct scsi_cmnd *cmd) 202*48a31030SOndrej Zary { 203*48a31030SOndrej Zary int i, count_sg; 204*48a31030SOndrej Zary unsigned long flags; 205*48a31030SOndrej Zary struct wd719x_scb *scb; 206*48a31030SOndrej Zary struct wd719x *wd = shost_priv(sh); 207*48a31030SOndrej Zary dma_addr_t phys; 208*48a31030SOndrej Zary 209*48a31030SOndrej Zary cmd->host_scribble = NULL; 210*48a31030SOndrej Zary 211*48a31030SOndrej Zary /* get a free SCB - either from existing ones or allocate a new one */ 212*48a31030SOndrej Zary spin_lock_irqsave(wd->sh->host_lock, flags); 213*48a31030SOndrej Zary scb = list_first_entry_or_null(&wd->free_scbs, struct wd719x_scb, list); 214*48a31030SOndrej Zary if (scb) { 215*48a31030SOndrej Zary list_del(&scb->list); 216*48a31030SOndrej Zary phys = scb->phys; 217*48a31030SOndrej Zary } else { 218*48a31030SOndrej Zary spin_unlock_irqrestore(wd->sh->host_lock, flags); 219*48a31030SOndrej Zary scb = pci_alloc_consistent(wd->pdev, sizeof(struct wd719x_scb), 220*48a31030SOndrej Zary &phys); 221*48a31030SOndrej Zary spin_lock_irqsave(wd->sh->host_lock, flags); 222*48a31030SOndrej Zary if (!scb) { 223*48a31030SOndrej Zary dev_err(&wd->pdev->dev, "unable to allocate SCB\n"); 224*48a31030SOndrej Zary wd719x_finish_cmd(cmd, DID_ERROR); 225*48a31030SOndrej Zary spin_unlock_irqrestore(wd->sh->host_lock, flags); 226*48a31030SOndrej Zary return 0; 227*48a31030SOndrej Zary } 228*48a31030SOndrej Zary } 229*48a31030SOndrej Zary memset(scb, 0, sizeof(struct wd719x_scb)); 230*48a31030SOndrej Zary list_add(&scb->list, &wd->active_scbs); 231*48a31030SOndrej Zary 232*48a31030SOndrej Zary scb->phys = phys; 233*48a31030SOndrej Zary scb->cmd = cmd; 234*48a31030SOndrej Zary cmd->host_scribble = (char *) scb; 235*48a31030SOndrej Zary 236*48a31030SOndrej Zary scb->CDB_tag = 0; /* Tagged queueing not supported yet */ 237*48a31030SOndrej Zary scb->devid = cmd->device->id; 238*48a31030SOndrej Zary scb->lun = cmd->device->lun; 239*48a31030SOndrej Zary 240*48a31030SOndrej Zary /* copy the command */ 241*48a31030SOndrej Zary memcpy(scb->CDB, cmd->cmnd, cmd->cmd_len); 242*48a31030SOndrej Zary 243*48a31030SOndrej Zary /* map sense buffer */ 244*48a31030SOndrej Zary scb->sense_buf_length = SCSI_SENSE_BUFFERSIZE; 245*48a31030SOndrej Zary cmd->SCp.dma_handle = dma_map_single(&wd->pdev->dev, cmd->sense_buffer, 246*48a31030SOndrej Zary SCSI_SENSE_BUFFERSIZE, DMA_FROM_DEVICE); 247*48a31030SOndrej Zary dma_cache_sync(&wd->pdev->dev, cmd->sense_buffer, 248*48a31030SOndrej Zary SCSI_SENSE_BUFFERSIZE, DMA_FROM_DEVICE); 249*48a31030SOndrej Zary scb->sense_buf = cpu_to_le32(cmd->SCp.dma_handle); 250*48a31030SOndrej Zary 251*48a31030SOndrej Zary /* request autosense */ 252*48a31030SOndrej Zary scb->SCB_options |= WD719X_SCB_FLAGS_AUTO_REQUEST_SENSE; 253*48a31030SOndrej Zary 254*48a31030SOndrej Zary /* check direction */ 255*48a31030SOndrej Zary if (cmd->sc_data_direction == DMA_TO_DEVICE) 256*48a31030SOndrej Zary scb->SCB_options |= WD719X_SCB_FLAGS_CHECK_DIRECTION 257*48a31030SOndrej Zary | WD719X_SCB_FLAGS_PCI_TO_SCSI; 258*48a31030SOndrej Zary else if (cmd->sc_data_direction == DMA_FROM_DEVICE) 259*48a31030SOndrej Zary scb->SCB_options |= WD719X_SCB_FLAGS_CHECK_DIRECTION; 260*48a31030SOndrej Zary 261*48a31030SOndrej Zary /* Scather/gather */ 262*48a31030SOndrej Zary count_sg = scsi_dma_map(cmd); 263*48a31030SOndrej Zary if (count_sg < 0) { 264*48a31030SOndrej Zary wd719x_finish_cmd(cmd, DID_ERROR); 265*48a31030SOndrej Zary spin_unlock_irqrestore(wd->sh->host_lock, flags); 266*48a31030SOndrej Zary return 0; 267*48a31030SOndrej Zary } 268*48a31030SOndrej Zary BUG_ON(count_sg > WD719X_SG); 269*48a31030SOndrej Zary 270*48a31030SOndrej Zary if (count_sg) { 271*48a31030SOndrej Zary struct scatterlist *sg; 272*48a31030SOndrej Zary 273*48a31030SOndrej Zary scb->data_length = cpu_to_le32(count_sg * 274*48a31030SOndrej Zary sizeof(struct wd719x_sglist)); 275*48a31030SOndrej Zary scb->data_p = cpu_to_le32(scb->phys + 276*48a31030SOndrej Zary offsetof(struct wd719x_scb, sg_list)); 277*48a31030SOndrej Zary 278*48a31030SOndrej Zary scsi_for_each_sg(cmd, sg, count_sg, i) { 279*48a31030SOndrej Zary scb->sg_list[i].ptr = cpu_to_le32(sg_dma_address(sg)); 280*48a31030SOndrej Zary scb->sg_list[i].length = cpu_to_le32(sg_dma_len(sg)); 281*48a31030SOndrej Zary } 282*48a31030SOndrej Zary scb->SCB_options |= WD719X_SCB_FLAGS_DO_SCATTER_GATHER; 283*48a31030SOndrej Zary } else { /* zero length */ 284*48a31030SOndrej Zary scb->data_length = 0; 285*48a31030SOndrej Zary scb->data_p = 0; 286*48a31030SOndrej Zary } 287*48a31030SOndrej Zary 288*48a31030SOndrej Zary /* check if the Command register is free */ 289*48a31030SOndrej Zary if (wd719x_readb(wd, WD719X_AMR_COMMAND) != WD719X_CMD_READY) { 290*48a31030SOndrej Zary spin_unlock_irqrestore(wd->sh->host_lock, flags); 291*48a31030SOndrej Zary return SCSI_MLQUEUE_HOST_BUSY; 292*48a31030SOndrej Zary } 293*48a31030SOndrej Zary 294*48a31030SOndrej Zary /* write pointer to the AMR */ 295*48a31030SOndrej Zary wd719x_writel(wd, WD719X_AMR_SCB_IN, scb->phys); 296*48a31030SOndrej Zary /* send SCB opcode */ 297*48a31030SOndrej Zary wd719x_writeb(wd, WD719X_AMR_COMMAND, WD719X_CMD_PROCESS_SCB); 298*48a31030SOndrej Zary 299*48a31030SOndrej Zary spin_unlock_irqrestore(wd->sh->host_lock, flags); 300*48a31030SOndrej Zary 301*48a31030SOndrej Zary return 0; 302*48a31030SOndrej Zary } 303*48a31030SOndrej Zary 304*48a31030SOndrej Zary static int wd719x_chip_init(struct wd719x *wd) 305*48a31030SOndrej Zary { 306*48a31030SOndrej Zary int i, ret; 307*48a31030SOndrej Zary u32 risc_init[3]; 308*48a31030SOndrej Zary const struct firmware *fw_wcs, *fw_risc; 309*48a31030SOndrej Zary const char fwname_wcs[] = "wd719x-wcs.bin"; 310*48a31030SOndrej Zary const char fwname_risc[] = "wd719x-risc.bin"; 311*48a31030SOndrej Zary 312*48a31030SOndrej Zary memset(wd->hash_virt, 0, WD719X_HASH_TABLE_SIZE); 313*48a31030SOndrej Zary 314*48a31030SOndrej Zary /* WCS (sequencer) firmware */ 315*48a31030SOndrej Zary ret = request_firmware(&fw_wcs, fwname_wcs, &wd->pdev->dev); 316*48a31030SOndrej Zary if (ret) { 317*48a31030SOndrej Zary dev_err(&wd->pdev->dev, "Unable to load firmware %s: %d\n", 318*48a31030SOndrej Zary fwname_wcs, ret); 319*48a31030SOndrej Zary return ret; 320*48a31030SOndrej Zary } 321*48a31030SOndrej Zary /* RISC firmware */ 322*48a31030SOndrej Zary ret = request_firmware(&fw_risc, fwname_risc, &wd->pdev->dev); 323*48a31030SOndrej Zary if (ret) { 324*48a31030SOndrej Zary dev_err(&wd->pdev->dev, "Unable to load firmware %s: %d\n", 325*48a31030SOndrej Zary fwname_risc, ret); 326*48a31030SOndrej Zary release_firmware(fw_wcs); 327*48a31030SOndrej Zary return ret; 328*48a31030SOndrej Zary } 329*48a31030SOndrej Zary wd->fw_size = ALIGN(fw_wcs->size, 4) + fw_risc->size; 330*48a31030SOndrej Zary 331*48a31030SOndrej Zary if (!wd->fw_virt) 332*48a31030SOndrej Zary wd->fw_virt = pci_alloc_consistent(wd->pdev, wd->fw_size, 333*48a31030SOndrej Zary &wd->fw_phys); 334*48a31030SOndrej Zary if (!wd->fw_virt) { 335*48a31030SOndrej Zary ret = -ENOMEM; 336*48a31030SOndrej Zary goto wd719x_init_end; 337*48a31030SOndrej Zary } 338*48a31030SOndrej Zary 339*48a31030SOndrej Zary /* make a fresh copy of WCS and RISC code */ 340*48a31030SOndrej Zary memcpy(wd->fw_virt, fw_wcs->data, fw_wcs->size); 341*48a31030SOndrej Zary memcpy(wd->fw_virt + ALIGN(fw_wcs->size, 4), fw_risc->data, 342*48a31030SOndrej Zary fw_risc->size); 343*48a31030SOndrej Zary 344*48a31030SOndrej Zary /* Reset the Spider Chip and adapter itself */ 345*48a31030SOndrej Zary wd719x_writeb(wd, WD719X_PCI_PORT_RESET, WD719X_PCI_RESET); 346*48a31030SOndrej Zary udelay(WD719X_WAIT_FOR_RISC); 347*48a31030SOndrej Zary /* Clear PIO mode bits set by BIOS */ 348*48a31030SOndrej Zary wd719x_writeb(wd, WD719X_AMR_CMD_PARAM, 0); 349*48a31030SOndrej Zary /* ensure RISC is not running */ 350*48a31030SOndrej Zary wd719x_writeb(wd, WD719X_PCI_MODE_SELECT, 0); 351*48a31030SOndrej Zary /* ensure command port is ready */ 352*48a31030SOndrej Zary wd719x_writeb(wd, WD719X_AMR_COMMAND, 0); 353*48a31030SOndrej Zary if (wd719x_wait_ready(wd)) { 354*48a31030SOndrej Zary ret = -ETIMEDOUT; 355*48a31030SOndrej Zary goto wd719x_init_end; 356*48a31030SOndrej Zary } 357*48a31030SOndrej Zary 358*48a31030SOndrej Zary /* Transfer the first 2K words of RISC code to kick start the uP */ 359*48a31030SOndrej Zary risc_init[0] = wd->fw_phys; /* WCS FW */ 360*48a31030SOndrej Zary risc_init[1] = wd->fw_phys + ALIGN(fw_wcs->size, 4); /* RISC FW */ 361*48a31030SOndrej Zary risc_init[2] = wd->hash_phys; /* hash table */ 362*48a31030SOndrej Zary 363*48a31030SOndrej Zary /* clear DMA status */ 364*48a31030SOndrej Zary wd719x_writeb(wd, WD719X_PCI_CHANNEL2_3STATUS, 0); 365*48a31030SOndrej Zary 366*48a31030SOndrej Zary /* address to read firmware from */ 367*48a31030SOndrej Zary wd719x_writel(wd, WD719X_PCI_EXTERNAL_ADDR, risc_init[1]); 368*48a31030SOndrej Zary /* base address to write firmware to (on card) */ 369*48a31030SOndrej Zary wd719x_writew(wd, WD719X_PCI_INTERNAL_ADDR, WD719X_PRAM_BASE_ADDR); 370*48a31030SOndrej Zary /* size: first 2K words */ 371*48a31030SOndrej Zary wd719x_writew(wd, WD719X_PCI_DMA_TRANSFER_SIZE, 2048 * 2); 372*48a31030SOndrej Zary /* start DMA */ 373*48a31030SOndrej Zary wd719x_writeb(wd, WD719X_PCI_CHANNEL2_3CMD, WD719X_START_CHANNEL2_3DMA); 374*48a31030SOndrej Zary 375*48a31030SOndrej Zary /* wait for DMA to complete */ 376*48a31030SOndrej Zary i = WD719X_WAIT_FOR_RISC; 377*48a31030SOndrej Zary while (i-- > 0) { 378*48a31030SOndrej Zary u8 status = wd719x_readb(wd, WD719X_PCI_CHANNEL2_3STATUS); 379*48a31030SOndrej Zary if (status == WD719X_START_CHANNEL2_3DONE) 380*48a31030SOndrej Zary break; 381*48a31030SOndrej Zary if (status == WD719X_START_CHANNEL2_3ABORT) { 382*48a31030SOndrej Zary dev_warn(&wd->pdev->dev, "RISC bootstrap failed: DMA aborted\n"); 383*48a31030SOndrej Zary ret = -EIO; 384*48a31030SOndrej Zary goto wd719x_init_end; 385*48a31030SOndrej Zary } 386*48a31030SOndrej Zary udelay(1); 387*48a31030SOndrej Zary } 388*48a31030SOndrej Zary if (i < 1) { 389*48a31030SOndrej Zary dev_warn(&wd->pdev->dev, "RISC bootstrap failed: DMA timeout\n"); 390*48a31030SOndrej Zary ret = -ETIMEDOUT; 391*48a31030SOndrej Zary goto wd719x_init_end; 392*48a31030SOndrej Zary } 393*48a31030SOndrej Zary 394*48a31030SOndrej Zary /* firmware is loaded, now initialize and wake up the RISC */ 395*48a31030SOndrej Zary /* write RISC initialization long words to Spider */ 396*48a31030SOndrej Zary wd719x_writel(wd, WD719X_AMR_SCB_IN, risc_init[0]); 397*48a31030SOndrej Zary wd719x_writel(wd, WD719X_AMR_SCB_IN + 4, risc_init[1]); 398*48a31030SOndrej Zary wd719x_writel(wd, WD719X_AMR_SCB_IN + 8, risc_init[2]); 399*48a31030SOndrej Zary 400*48a31030SOndrej Zary /* disable interrupts during initialization of RISC */ 401*48a31030SOndrej Zary wd719x_writeb(wd, WD719X_AMR_CMD_PARAM, WD719X_DISABLE_INT); 402*48a31030SOndrej Zary 403*48a31030SOndrej Zary /* issue INITIALIZE RISC comand */ 404*48a31030SOndrej Zary wd719x_writeb(wd, WD719X_AMR_COMMAND, WD719X_CMD_INIT_RISC); 405*48a31030SOndrej Zary /* enable advanced mode (wake up RISC) */ 406*48a31030SOndrej Zary wd719x_writeb(wd, WD719X_PCI_MODE_SELECT, WD719X_ENABLE_ADVANCE_MODE); 407*48a31030SOndrej Zary udelay(WD719X_WAIT_FOR_RISC); 408*48a31030SOndrej Zary 409*48a31030SOndrej Zary ret = wd719x_wait_done(wd, WD719X_WAIT_FOR_RISC); 410*48a31030SOndrej Zary /* clear interrupt status register */ 411*48a31030SOndrej Zary wd719x_writeb(wd, WD719X_AMR_INT_STATUS, WD719X_INT_NONE); 412*48a31030SOndrej Zary if (ret) { 413*48a31030SOndrej Zary dev_warn(&wd->pdev->dev, "Unable to initialize RISC\n"); 414*48a31030SOndrej Zary goto wd719x_init_end; 415*48a31030SOndrej Zary } 416*48a31030SOndrej Zary /* RISC is up and running */ 417*48a31030SOndrej Zary 418*48a31030SOndrej Zary /* Read FW version from RISC */ 419*48a31030SOndrej Zary ret = wd719x_direct_cmd(wd, WD719X_CMD_READ_FIRMVER, 0, 0, 0, 0, 420*48a31030SOndrej Zary WD719X_WAIT_FOR_RISC); 421*48a31030SOndrej Zary if (ret) { 422*48a31030SOndrej Zary dev_warn(&wd->pdev->dev, "Unable to read firmware version\n"); 423*48a31030SOndrej Zary goto wd719x_init_end; 424*48a31030SOndrej Zary } 425*48a31030SOndrej Zary dev_info(&wd->pdev->dev, "RISC initialized with firmware version %.2x.%.2x\n", 426*48a31030SOndrej Zary wd719x_readb(wd, WD719X_AMR_SCB_OUT + 1), 427*48a31030SOndrej Zary wd719x_readb(wd, WD719X_AMR_SCB_OUT)); 428*48a31030SOndrej Zary 429*48a31030SOndrej Zary /* RESET SCSI bus */ 430*48a31030SOndrej Zary ret = wd719x_direct_cmd(wd, WD719X_CMD_BUSRESET, 0, 0, 0, 0, 431*48a31030SOndrej Zary WD719X_WAIT_FOR_SCSI_RESET); 432*48a31030SOndrej Zary if (ret) { 433*48a31030SOndrej Zary dev_warn(&wd->pdev->dev, "SCSI bus reset failed\n"); 434*48a31030SOndrej Zary goto wd719x_init_end; 435*48a31030SOndrej Zary } 436*48a31030SOndrej Zary 437*48a31030SOndrej Zary /* use HostParameter structure to set Spider's Host Parameter Block */ 438*48a31030SOndrej Zary ret = wd719x_direct_cmd(wd, WD719X_CMD_SET_PARAM, 0, 439*48a31030SOndrej Zary sizeof(struct wd719x_host_param), 0, 440*48a31030SOndrej Zary wd->params_phys, WD719X_WAIT_FOR_RISC); 441*48a31030SOndrej Zary if (ret) { 442*48a31030SOndrej Zary dev_warn(&wd->pdev->dev, "Failed to set HOST PARAMETERS\n"); 443*48a31030SOndrej Zary goto wd719x_init_end; 444*48a31030SOndrej Zary } 445*48a31030SOndrej Zary 446*48a31030SOndrej Zary /* initiate SCAM (does nothing if disabled in BIOS) */ 447*48a31030SOndrej Zary /* bug?: we should pass a mask of static IDs which we don't have */ 448*48a31030SOndrej Zary ret = wd719x_direct_cmd(wd, WD719X_CMD_INIT_SCAM, 0, 0, 0, 0, 449*48a31030SOndrej Zary WD719X_WAIT_FOR_SCSI_RESET); 450*48a31030SOndrej Zary if (ret) { 451*48a31030SOndrej Zary dev_warn(&wd->pdev->dev, "SCAM initialization failed\n"); 452*48a31030SOndrej Zary goto wd719x_init_end; 453*48a31030SOndrej Zary } 454*48a31030SOndrej Zary 455*48a31030SOndrej Zary /* clear AMR_BIOS_SHARE_INT register */ 456*48a31030SOndrej Zary wd719x_writeb(wd, WD719X_AMR_BIOS_SHARE_INT, 0); 457*48a31030SOndrej Zary 458*48a31030SOndrej Zary wd719x_init_end: 459*48a31030SOndrej Zary release_firmware(fw_wcs); 460*48a31030SOndrej Zary release_firmware(fw_risc); 461*48a31030SOndrej Zary 462*48a31030SOndrej Zary return ret; 463*48a31030SOndrej Zary } 464*48a31030SOndrej Zary 465*48a31030SOndrej Zary static int wd719x_abort(struct scsi_cmnd *cmd) 466*48a31030SOndrej Zary { 467*48a31030SOndrej Zary int action, result; 468*48a31030SOndrej Zary unsigned long flags; 469*48a31030SOndrej Zary struct wd719x_scb *scb = (struct wd719x_scb *)cmd->host_scribble; 470*48a31030SOndrej Zary struct wd719x *wd = shost_priv(cmd->device->host); 471*48a31030SOndrej Zary 472*48a31030SOndrej Zary dev_info(&wd->pdev->dev, "abort command, tag: %x\n", cmd->tag); 473*48a31030SOndrej Zary 474*48a31030SOndrej Zary action = /*cmd->tag ? WD719X_CMD_ABORT_TAG : */WD719X_CMD_ABORT; 475*48a31030SOndrej Zary 476*48a31030SOndrej Zary spin_lock_irqsave(wd->sh->host_lock, flags); 477*48a31030SOndrej Zary result = wd719x_direct_cmd(wd, action, cmd->device->id, 478*48a31030SOndrej Zary cmd->device->lun, cmd->tag, scb->phys, 0); 479*48a31030SOndrej Zary spin_unlock_irqrestore(wd->sh->host_lock, flags); 480*48a31030SOndrej Zary if (result) 481*48a31030SOndrej Zary return FAILED; 482*48a31030SOndrej Zary 483*48a31030SOndrej Zary return SUCCESS; 484*48a31030SOndrej Zary } 485*48a31030SOndrej Zary 486*48a31030SOndrej Zary static int wd719x_reset(struct scsi_cmnd *cmd, u8 opcode, u8 device) 487*48a31030SOndrej Zary { 488*48a31030SOndrej Zary int result; 489*48a31030SOndrej Zary unsigned long flags; 490*48a31030SOndrej Zary struct wd719x *wd = shost_priv(cmd->device->host); 491*48a31030SOndrej Zary 492*48a31030SOndrej Zary dev_info(&wd->pdev->dev, "%s reset requested\n", 493*48a31030SOndrej Zary (opcode == WD719X_CMD_BUSRESET) ? "bus" : "device"); 494*48a31030SOndrej Zary 495*48a31030SOndrej Zary spin_lock_irqsave(wd->sh->host_lock, flags); 496*48a31030SOndrej Zary result = wd719x_direct_cmd(wd, opcode, device, 0, 0, 0, 497*48a31030SOndrej Zary WD719X_WAIT_FOR_SCSI_RESET); 498*48a31030SOndrej Zary spin_unlock_irqrestore(wd->sh->host_lock, flags); 499*48a31030SOndrej Zary if (result) 500*48a31030SOndrej Zary return FAILED; 501*48a31030SOndrej Zary 502*48a31030SOndrej Zary return SUCCESS; 503*48a31030SOndrej Zary } 504*48a31030SOndrej Zary 505*48a31030SOndrej Zary static int wd719x_dev_reset(struct scsi_cmnd *cmd) 506*48a31030SOndrej Zary { 507*48a31030SOndrej Zary return wd719x_reset(cmd, WD719X_CMD_RESET, cmd->device->id); 508*48a31030SOndrej Zary } 509*48a31030SOndrej Zary 510*48a31030SOndrej Zary static int wd719x_bus_reset(struct scsi_cmnd *cmd) 511*48a31030SOndrej Zary { 512*48a31030SOndrej Zary return wd719x_reset(cmd, WD719X_CMD_BUSRESET, 0); 513*48a31030SOndrej Zary } 514*48a31030SOndrej Zary 515*48a31030SOndrej Zary static int wd719x_host_reset(struct scsi_cmnd *cmd) 516*48a31030SOndrej Zary { 517*48a31030SOndrej Zary struct wd719x *wd = shost_priv(cmd->device->host); 518*48a31030SOndrej Zary struct wd719x_scb *scb, *tmp; 519*48a31030SOndrej Zary unsigned long flags; 520*48a31030SOndrej Zary int result; 521*48a31030SOndrej Zary 522*48a31030SOndrej Zary dev_info(&wd->pdev->dev, "host reset requested\n"); 523*48a31030SOndrej Zary spin_lock_irqsave(wd->sh->host_lock, flags); 524*48a31030SOndrej Zary /* Try to reinit the RISC */ 525*48a31030SOndrej Zary if (wd719x_chip_init(wd) == 0) 526*48a31030SOndrej Zary result = SUCCESS; 527*48a31030SOndrej Zary else 528*48a31030SOndrej Zary result = FAILED; 529*48a31030SOndrej Zary 530*48a31030SOndrej Zary /* flush all SCBs */ 531*48a31030SOndrej Zary list_for_each_entry_safe(scb, tmp, &wd->active_scbs, list) { 532*48a31030SOndrej Zary struct scsi_cmnd *tmp_cmd = scb->cmd; 533*48a31030SOndrej Zary wd719x_finish_cmd(tmp_cmd, result); 534*48a31030SOndrej Zary } 535*48a31030SOndrej Zary spin_unlock_irqrestore(wd->sh->host_lock, flags); 536*48a31030SOndrej Zary 537*48a31030SOndrej Zary return result; 538*48a31030SOndrej Zary } 539*48a31030SOndrej Zary 540*48a31030SOndrej Zary static int wd719x_biosparam(struct scsi_device *sdev, struct block_device *bdev, 541*48a31030SOndrej Zary sector_t capacity, int geom[]) 542*48a31030SOndrej Zary { 543*48a31030SOndrej Zary if (capacity >= 0x200000) { 544*48a31030SOndrej Zary geom[0] = 255; /* heads */ 545*48a31030SOndrej Zary geom[1] = 63; /* sectors */ 546*48a31030SOndrej Zary } else { 547*48a31030SOndrej Zary geom[0] = 64; /* heads */ 548*48a31030SOndrej Zary geom[1] = 32; /* sectors */ 549*48a31030SOndrej Zary } 550*48a31030SOndrej Zary geom[2] = sector_div(capacity, geom[0] * geom[1]); /* cylinders */ 551*48a31030SOndrej Zary 552*48a31030SOndrej Zary return 0; 553*48a31030SOndrej Zary } 554*48a31030SOndrej Zary 555*48a31030SOndrej Zary /* process a SCB-completion interrupt */ 556*48a31030SOndrej Zary static inline void wd719x_interrupt_SCB(struct wd719x *wd, 557*48a31030SOndrej Zary union wd719x_regs regs, 558*48a31030SOndrej Zary struct wd719x_scb *scb) 559*48a31030SOndrej Zary { 560*48a31030SOndrej Zary struct scsi_cmnd *cmd; 561*48a31030SOndrej Zary int result; 562*48a31030SOndrej Zary 563*48a31030SOndrej Zary /* now have to find result from card */ 564*48a31030SOndrej Zary switch (regs.bytes.SUE) { 565*48a31030SOndrej Zary case WD719X_SUE_NOERRORS: 566*48a31030SOndrej Zary result = DID_OK; 567*48a31030SOndrej Zary break; 568*48a31030SOndrej Zary case WD719X_SUE_REJECTED: 569*48a31030SOndrej Zary dev_err(&wd->pdev->dev, "command rejected\n"); 570*48a31030SOndrej Zary result = DID_ERROR; 571*48a31030SOndrej Zary break; 572*48a31030SOndrej Zary case WD719X_SUE_SCBQFULL: 573*48a31030SOndrej Zary dev_err(&wd->pdev->dev, "SCB queue is full\n"); 574*48a31030SOndrej Zary result = DID_ERROR; 575*48a31030SOndrej Zary break; 576*48a31030SOndrej Zary case WD719X_SUE_TERM: 577*48a31030SOndrej Zary dev_dbg(&wd->pdev->dev, "SCB terminated by direct command\n"); 578*48a31030SOndrej Zary result = DID_ABORT; /* or DID_RESET? */ 579*48a31030SOndrej Zary break; 580*48a31030SOndrej Zary case WD719X_SUE_CHAN1ABORT: 581*48a31030SOndrej Zary case WD719X_SUE_CHAN23ABORT: 582*48a31030SOndrej Zary result = DID_ABORT; 583*48a31030SOndrej Zary dev_err(&wd->pdev->dev, "DMA abort\n"); 584*48a31030SOndrej Zary break; 585*48a31030SOndrej Zary case WD719X_SUE_CHAN1PAR: 586*48a31030SOndrej Zary case WD719X_SUE_CHAN23PAR: 587*48a31030SOndrej Zary result = DID_PARITY; 588*48a31030SOndrej Zary dev_err(&wd->pdev->dev, "DMA parity error\n"); 589*48a31030SOndrej Zary break; 590*48a31030SOndrej Zary case WD719X_SUE_TIMEOUT: 591*48a31030SOndrej Zary result = DID_TIME_OUT; 592*48a31030SOndrej Zary dev_dbg(&wd->pdev->dev, "selection timeout\n"); 593*48a31030SOndrej Zary break; 594*48a31030SOndrej Zary case WD719X_SUE_RESET: 595*48a31030SOndrej Zary dev_dbg(&wd->pdev->dev, "bus reset occured\n"); 596*48a31030SOndrej Zary result = DID_RESET; 597*48a31030SOndrej Zary break; 598*48a31030SOndrej Zary case WD719X_SUE_BUSERROR: 599*48a31030SOndrej Zary dev_dbg(&wd->pdev->dev, "SCSI bus error\n"); 600*48a31030SOndrej Zary result = DID_ERROR; 601*48a31030SOndrej Zary break; 602*48a31030SOndrej Zary case WD719X_SUE_WRONGWAY: 603*48a31030SOndrej Zary dev_err(&wd->pdev->dev, "wrong data transfer direction\n"); 604*48a31030SOndrej Zary result = DID_ERROR; 605*48a31030SOndrej Zary break; 606*48a31030SOndrej Zary case WD719X_SUE_BADPHASE: 607*48a31030SOndrej Zary dev_err(&wd->pdev->dev, "invalid SCSI phase\n"); 608*48a31030SOndrej Zary result = DID_ERROR; 609*48a31030SOndrej Zary break; 610*48a31030SOndrej Zary case WD719X_SUE_TOOLONG: 611*48a31030SOndrej Zary dev_err(&wd->pdev->dev, "record too long\n"); 612*48a31030SOndrej Zary result = DID_ERROR; 613*48a31030SOndrej Zary break; 614*48a31030SOndrej Zary case WD719X_SUE_BUSFREE: 615*48a31030SOndrej Zary dev_err(&wd->pdev->dev, "unexpected bus free\n"); 616*48a31030SOndrej Zary result = DID_NO_CONNECT; /* or DID_ERROR ???*/ 617*48a31030SOndrej Zary break; 618*48a31030SOndrej Zary case WD719X_SUE_ARSDONE: 619*48a31030SOndrej Zary dev_dbg(&wd->pdev->dev, "auto request sense\n"); 620*48a31030SOndrej Zary if (regs.bytes.SCSI == 0) 621*48a31030SOndrej Zary result = DID_OK; 622*48a31030SOndrej Zary else 623*48a31030SOndrej Zary result = DID_PARITY; 624*48a31030SOndrej Zary break; 625*48a31030SOndrej Zary case WD719X_SUE_IGNORED: 626*48a31030SOndrej Zary dev_err(&wd->pdev->dev, "target id %d ignored command\n", 627*48a31030SOndrej Zary scb->cmd->device->id); 628*48a31030SOndrej Zary result = DID_NO_CONNECT; 629*48a31030SOndrej Zary break; 630*48a31030SOndrej Zary case WD719X_SUE_WRONGTAGS: 631*48a31030SOndrej Zary dev_err(&wd->pdev->dev, "reversed tags\n"); 632*48a31030SOndrej Zary result = DID_ERROR; 633*48a31030SOndrej Zary break; 634*48a31030SOndrej Zary case WD719X_SUE_BADTAGS: 635*48a31030SOndrej Zary dev_err(&wd->pdev->dev, "tag type not supported by target\n"); 636*48a31030SOndrej Zary result = DID_ERROR; 637*48a31030SOndrej Zary break; 638*48a31030SOndrej Zary case WD719X_SUE_NOSCAMID: 639*48a31030SOndrej Zary dev_err(&wd->pdev->dev, "no SCAM soft ID available\n"); 640*48a31030SOndrej Zary result = DID_ERROR; 641*48a31030SOndrej Zary break; 642*48a31030SOndrej Zary default: 643*48a31030SOndrej Zary dev_warn(&wd->pdev->dev, "unknown SUE error code: 0x%x\n", 644*48a31030SOndrej Zary regs.bytes.SUE); 645*48a31030SOndrej Zary result = DID_ERROR; 646*48a31030SOndrej Zary break; 647*48a31030SOndrej Zary } 648*48a31030SOndrej Zary cmd = scb->cmd; 649*48a31030SOndrej Zary 650*48a31030SOndrej Zary wd719x_finish_cmd(cmd, result); 651*48a31030SOndrej Zary } 652*48a31030SOndrej Zary 653*48a31030SOndrej Zary static irqreturn_t wd719x_interrupt(int irq, void *dev_id) 654*48a31030SOndrej Zary { 655*48a31030SOndrej Zary struct wd719x *wd = dev_id; 656*48a31030SOndrej Zary union wd719x_regs regs; 657*48a31030SOndrej Zary unsigned long flags; 658*48a31030SOndrej Zary u32 SCB_out; 659*48a31030SOndrej Zary 660*48a31030SOndrej Zary spin_lock_irqsave(wd->sh->host_lock, flags); 661*48a31030SOndrej Zary /* read SCB pointer back from card */ 662*48a31030SOndrej Zary SCB_out = wd719x_readl(wd, WD719X_AMR_SCB_OUT); 663*48a31030SOndrej Zary /* read all status info at once */ 664*48a31030SOndrej Zary regs.all = cpu_to_le32(wd719x_readl(wd, WD719X_AMR_OP_CODE)); 665*48a31030SOndrej Zary 666*48a31030SOndrej Zary switch (regs.bytes.INT) { 667*48a31030SOndrej Zary case WD719X_INT_NONE: 668*48a31030SOndrej Zary spin_unlock_irqrestore(wd->sh->host_lock, flags); 669*48a31030SOndrej Zary return IRQ_NONE; 670*48a31030SOndrej Zary case WD719X_INT_LINKNOSTATUS: 671*48a31030SOndrej Zary dev_err(&wd->pdev->dev, "linked command completed with no status\n"); 672*48a31030SOndrej Zary break; 673*48a31030SOndrej Zary case WD719X_INT_BADINT: 674*48a31030SOndrej Zary dev_err(&wd->pdev->dev, "unsolicited interrupt\n"); 675*48a31030SOndrej Zary break; 676*48a31030SOndrej Zary case WD719X_INT_NOERRORS: 677*48a31030SOndrej Zary case WD719X_INT_LINKNOERRORS: 678*48a31030SOndrej Zary case WD719X_INT_ERRORSLOGGED: 679*48a31030SOndrej Zary case WD719X_INT_SPIDERFAILED: 680*48a31030SOndrej Zary /* was the cmd completed a direct or SCB command? */ 681*48a31030SOndrej Zary if (regs.bytes.OPC == WD719X_CMD_PROCESS_SCB) { 682*48a31030SOndrej Zary struct wd719x_scb *scb; 683*48a31030SOndrej Zary list_for_each_entry(scb, &wd->active_scbs, list) 684*48a31030SOndrej Zary if (SCB_out == scb->phys) 685*48a31030SOndrej Zary break; 686*48a31030SOndrej Zary if (SCB_out == scb->phys) 687*48a31030SOndrej Zary wd719x_interrupt_SCB(wd, regs, scb); 688*48a31030SOndrej Zary else 689*48a31030SOndrej Zary dev_err(&wd->pdev->dev, "card returned invalid SCB pointer\n"); 690*48a31030SOndrej Zary } else 691*48a31030SOndrej Zary dev_warn(&wd->pdev->dev, "direct command 0x%x completed\n", 692*48a31030SOndrej Zary regs.bytes.OPC); 693*48a31030SOndrej Zary break; 694*48a31030SOndrej Zary case WD719X_INT_PIOREADY: 695*48a31030SOndrej Zary dev_err(&wd->pdev->dev, "card indicates PIO data ready but we never use PIO\n"); 696*48a31030SOndrej Zary /* interrupt will not be cleared until all data is read */ 697*48a31030SOndrej Zary break; 698*48a31030SOndrej Zary default: 699*48a31030SOndrej Zary dev_err(&wd->pdev->dev, "unknown interrupt reason: %d\n", 700*48a31030SOndrej Zary regs.bytes.INT); 701*48a31030SOndrej Zary 702*48a31030SOndrej Zary } 703*48a31030SOndrej Zary /* clear interrupt so another can happen */ 704*48a31030SOndrej Zary wd719x_writeb(wd, WD719X_AMR_INT_STATUS, WD719X_INT_NONE); 705*48a31030SOndrej Zary spin_unlock_irqrestore(wd->sh->host_lock, flags); 706*48a31030SOndrej Zary 707*48a31030SOndrej Zary return IRQ_HANDLED; 708*48a31030SOndrej Zary } 709*48a31030SOndrej Zary 710*48a31030SOndrej Zary static void wd719x_eeprom_reg_read(struct eeprom_93cx6 *eeprom) 711*48a31030SOndrej Zary { 712*48a31030SOndrej Zary struct wd719x *wd = eeprom->data; 713*48a31030SOndrej Zary u8 reg = wd719x_readb(wd, WD719X_PCI_GPIO_DATA); 714*48a31030SOndrej Zary 715*48a31030SOndrej Zary eeprom->reg_data_out = reg & WD719X_EE_DO; 716*48a31030SOndrej Zary } 717*48a31030SOndrej Zary 718*48a31030SOndrej Zary static void wd719x_eeprom_reg_write(struct eeprom_93cx6 *eeprom) 719*48a31030SOndrej Zary { 720*48a31030SOndrej Zary struct wd719x *wd = eeprom->data; 721*48a31030SOndrej Zary u8 reg = 0; 722*48a31030SOndrej Zary 723*48a31030SOndrej Zary if (eeprom->reg_data_in) 724*48a31030SOndrej Zary reg |= WD719X_EE_DI; 725*48a31030SOndrej Zary if (eeprom->reg_data_clock) 726*48a31030SOndrej Zary reg |= WD719X_EE_CLK; 727*48a31030SOndrej Zary if (eeprom->reg_chip_select) 728*48a31030SOndrej Zary reg |= WD719X_EE_CS; 729*48a31030SOndrej Zary 730*48a31030SOndrej Zary wd719x_writeb(wd, WD719X_PCI_GPIO_DATA, reg); 731*48a31030SOndrej Zary } 732*48a31030SOndrej Zary 733*48a31030SOndrej Zary /* read config from EEPROM so it can be downloaded by the RISC on (re-)init */ 734*48a31030SOndrej Zary static void wd719x_read_eeprom(struct wd719x *wd) 735*48a31030SOndrej Zary { 736*48a31030SOndrej Zary struct eeprom_93cx6 eeprom; 737*48a31030SOndrej Zary u8 gpio; 738*48a31030SOndrej Zary struct wd719x_eeprom_header header; 739*48a31030SOndrej Zary 740*48a31030SOndrej Zary eeprom.data = wd; 741*48a31030SOndrej Zary eeprom.register_read = wd719x_eeprom_reg_read; 742*48a31030SOndrej Zary eeprom.register_write = wd719x_eeprom_reg_write; 743*48a31030SOndrej Zary eeprom.width = PCI_EEPROM_WIDTH_93C46; 744*48a31030SOndrej Zary 745*48a31030SOndrej Zary /* set all outputs to low */ 746*48a31030SOndrej Zary wd719x_writeb(wd, WD719X_PCI_GPIO_DATA, 0); 747*48a31030SOndrej Zary /* configure GPIO pins */ 748*48a31030SOndrej Zary gpio = wd719x_readb(wd, WD719X_PCI_GPIO_CONTROL); 749*48a31030SOndrej Zary /* GPIO outputs */ 750*48a31030SOndrej Zary gpio &= (~(WD719X_EE_CLK | WD719X_EE_DI | WD719X_EE_CS)); 751*48a31030SOndrej Zary /* GPIO input */ 752*48a31030SOndrej Zary gpio |= WD719X_EE_DO; 753*48a31030SOndrej Zary wd719x_writeb(wd, WD719X_PCI_GPIO_CONTROL, gpio); 754*48a31030SOndrej Zary 755*48a31030SOndrej Zary /* read EEPROM header */ 756*48a31030SOndrej Zary eeprom_93cx6_multireadb(&eeprom, 0, (u8 *)&header, sizeof(header)); 757*48a31030SOndrej Zary 758*48a31030SOndrej Zary if (header.sig1 == 'W' && header.sig2 == 'D') 759*48a31030SOndrej Zary eeprom_93cx6_multireadb(&eeprom, header.cfg_offset, 760*48a31030SOndrej Zary (u8 *)wd->params, 761*48a31030SOndrej Zary sizeof(struct wd719x_host_param)); 762*48a31030SOndrej Zary else { /* default EEPROM values */ 763*48a31030SOndrej Zary dev_warn(&wd->pdev->dev, "EEPROM signature is invalid (0x%02x 0x%02x), using default values\n", 764*48a31030SOndrej Zary header.sig1, header.sig2); 765*48a31030SOndrej Zary wd->params->ch_1_th = 0x10; /* 16 DWs = 64 B */ 766*48a31030SOndrej Zary wd->params->scsi_conf = 0x4c; /* 48ma, spue, parity check */ 767*48a31030SOndrej Zary wd->params->own_scsi_id = 0x07; /* ID 7, SCAM disabled */ 768*48a31030SOndrej Zary wd->params->sel_timeout = 0x4d; /* 250 ms */ 769*48a31030SOndrej Zary wd->params->sleep_timer = 0x01; 770*48a31030SOndrej Zary wd->params->cdb_size = cpu_to_le16(0x5555); /* all 6 B */ 771*48a31030SOndrej Zary wd->params->scsi_pad = 0x1b; 772*48a31030SOndrej Zary if (wd->type == WD719X_TYPE_7193) /* narrow card - disable */ 773*48a31030SOndrej Zary wd->params->wide = cpu_to_le32(0x00000000); 774*48a31030SOndrej Zary else /* initiate & respond to WIDE messages */ 775*48a31030SOndrej Zary wd->params->wide = cpu_to_le32(0xffffffff); 776*48a31030SOndrej Zary wd->params->sync = cpu_to_le32(0xffffffff); 777*48a31030SOndrej Zary wd->params->soft_mask = 0x00; /* all disabled */ 778*48a31030SOndrej Zary wd->params->unsol_mask = 0x00; /* all disabled */ 779*48a31030SOndrej Zary } 780*48a31030SOndrej Zary /* disable TAGGED messages */ 781*48a31030SOndrej Zary wd->params->tag_en = cpu_to_le16(0x0000); 782*48a31030SOndrej Zary } 783*48a31030SOndrej Zary 784*48a31030SOndrej Zary /* Read card type from GPIO bits 1 and 3 */ 785*48a31030SOndrej Zary static enum wd719x_card_type wd719x_detect_type(struct wd719x *wd) 786*48a31030SOndrej Zary { 787*48a31030SOndrej Zary u8 card = wd719x_readb(wd, WD719X_PCI_GPIO_CONTROL); 788*48a31030SOndrej Zary 789*48a31030SOndrej Zary card |= WD719X_GPIO_ID_BITS; 790*48a31030SOndrej Zary wd719x_writeb(wd, WD719X_PCI_GPIO_CONTROL, card); 791*48a31030SOndrej Zary card = wd719x_readb(wd, WD719X_PCI_GPIO_DATA) & WD719X_GPIO_ID_BITS; 792*48a31030SOndrej Zary switch (card) { 793*48a31030SOndrej Zary case 0x08: 794*48a31030SOndrej Zary return WD719X_TYPE_7193; 795*48a31030SOndrej Zary case 0x02: 796*48a31030SOndrej Zary return WD719X_TYPE_7197; 797*48a31030SOndrej Zary case 0x00: 798*48a31030SOndrej Zary return WD719X_TYPE_7296; 799*48a31030SOndrej Zary default: 800*48a31030SOndrej Zary dev_warn(&wd->pdev->dev, "unknown card type 0x%x\n", card); 801*48a31030SOndrej Zary return WD719X_TYPE_UNKNOWN; 802*48a31030SOndrej Zary } 803*48a31030SOndrej Zary } 804*48a31030SOndrej Zary 805*48a31030SOndrej Zary static int wd719x_board_found(struct Scsi_Host *sh) 806*48a31030SOndrej Zary { 807*48a31030SOndrej Zary struct wd719x *wd = shost_priv(sh); 808*48a31030SOndrej Zary char *card_types[] = { "Unknown card", "WD7193", "WD7197", "WD7296" }; 809*48a31030SOndrej Zary int ret; 810*48a31030SOndrej Zary 811*48a31030SOndrej Zary INIT_LIST_HEAD(&wd->active_scbs); 812*48a31030SOndrej Zary INIT_LIST_HEAD(&wd->free_scbs); 813*48a31030SOndrej Zary 814*48a31030SOndrej Zary sh->base = pci_resource_start(wd->pdev, 0); 815*48a31030SOndrej Zary 816*48a31030SOndrej Zary wd->type = wd719x_detect_type(wd); 817*48a31030SOndrej Zary 818*48a31030SOndrej Zary wd->sh = sh; 819*48a31030SOndrej Zary sh->irq = wd->pdev->irq; 820*48a31030SOndrej Zary wd->fw_virt = NULL; 821*48a31030SOndrej Zary 822*48a31030SOndrej Zary /* memory area for host (EEPROM) parameters */ 823*48a31030SOndrej Zary wd->params = pci_alloc_consistent(wd->pdev, 824*48a31030SOndrej Zary sizeof(struct wd719x_host_param), 825*48a31030SOndrej Zary &wd->params_phys); 826*48a31030SOndrej Zary if (!wd->params) { 827*48a31030SOndrej Zary dev_warn(&wd->pdev->dev, "unable to allocate parameter buffer\n"); 828*48a31030SOndrej Zary return -ENOMEM; 829*48a31030SOndrej Zary } 830*48a31030SOndrej Zary 831*48a31030SOndrej Zary /* memory area for the RISC for hash table of outstanding requests */ 832*48a31030SOndrej Zary wd->hash_virt = pci_alloc_consistent(wd->pdev, WD719X_HASH_TABLE_SIZE, 833*48a31030SOndrej Zary &wd->hash_phys); 834*48a31030SOndrej Zary if (!wd->hash_virt) { 835*48a31030SOndrej Zary dev_warn(&wd->pdev->dev, "unable to allocate hash buffer\n"); 836*48a31030SOndrej Zary ret = -ENOMEM; 837*48a31030SOndrej Zary goto fail_free_params; 838*48a31030SOndrej Zary } 839*48a31030SOndrej Zary 840*48a31030SOndrej Zary ret = request_irq(wd->pdev->irq, wd719x_interrupt, IRQF_SHARED, 841*48a31030SOndrej Zary "wd719x", wd); 842*48a31030SOndrej Zary if (ret) { 843*48a31030SOndrej Zary dev_warn(&wd->pdev->dev, "unable to assign IRQ %d\n", 844*48a31030SOndrej Zary wd->pdev->irq); 845*48a31030SOndrej Zary goto fail_free_hash; 846*48a31030SOndrej Zary } 847*48a31030SOndrej Zary 848*48a31030SOndrej Zary /* read parameters from EEPROM */ 849*48a31030SOndrej Zary wd719x_read_eeprom(wd); 850*48a31030SOndrej Zary 851*48a31030SOndrej Zary ret = wd719x_chip_init(wd); 852*48a31030SOndrej Zary if (ret) 853*48a31030SOndrej Zary goto fail_free_irq; 854*48a31030SOndrej Zary 855*48a31030SOndrej Zary sh->this_id = wd->params->own_scsi_id & WD719X_EE_SCSI_ID_MASK; 856*48a31030SOndrej Zary 857*48a31030SOndrej Zary dev_info(&wd->pdev->dev, "%s at I/O 0x%lx, IRQ %u, SCSI ID %d\n", 858*48a31030SOndrej Zary card_types[wd->type], sh->base, sh->irq, sh->this_id); 859*48a31030SOndrej Zary 860*48a31030SOndrej Zary return 0; 861*48a31030SOndrej Zary 862*48a31030SOndrej Zary fail_free_irq: 863*48a31030SOndrej Zary free_irq(wd->pdev->irq, wd); 864*48a31030SOndrej Zary fail_free_hash: 865*48a31030SOndrej Zary pci_free_consistent(wd->pdev, WD719X_HASH_TABLE_SIZE, wd->hash_virt, 866*48a31030SOndrej Zary wd->hash_phys); 867*48a31030SOndrej Zary fail_free_params: 868*48a31030SOndrej Zary pci_free_consistent(wd->pdev, sizeof(struct wd719x_host_param), 869*48a31030SOndrej Zary wd->params, wd->params_phys); 870*48a31030SOndrej Zary 871*48a31030SOndrej Zary return ret; 872*48a31030SOndrej Zary } 873*48a31030SOndrej Zary 874*48a31030SOndrej Zary static struct scsi_host_template wd719x_template = { 875*48a31030SOndrej Zary .name = "Western Digital 719x", 876*48a31030SOndrej Zary .queuecommand = wd719x_queuecommand, 877*48a31030SOndrej Zary .eh_abort_handler = wd719x_abort, 878*48a31030SOndrej Zary .eh_device_reset_handler = wd719x_dev_reset, 879*48a31030SOndrej Zary .eh_bus_reset_handler = wd719x_bus_reset, 880*48a31030SOndrej Zary .eh_host_reset_handler = wd719x_host_reset, 881*48a31030SOndrej Zary .bios_param = wd719x_biosparam, 882*48a31030SOndrej Zary .proc_name = "wd719x", 883*48a31030SOndrej Zary .can_queue = 255, 884*48a31030SOndrej Zary .this_id = 7, 885*48a31030SOndrej Zary .sg_tablesize = WD719X_SG, 886*48a31030SOndrej Zary .cmd_per_lun = WD719X_CMD_PER_LUN, 887*48a31030SOndrej Zary .use_clustering = ENABLE_CLUSTERING, 888*48a31030SOndrej Zary }; 889*48a31030SOndrej Zary 890*48a31030SOndrej Zary static int wd719x_pci_probe(struct pci_dev *pdev, const struct pci_device_id *d) 891*48a31030SOndrej Zary { 892*48a31030SOndrej Zary int err; 893*48a31030SOndrej Zary struct Scsi_Host *sh; 894*48a31030SOndrej Zary struct wd719x *wd; 895*48a31030SOndrej Zary 896*48a31030SOndrej Zary err = pci_enable_device(pdev); 897*48a31030SOndrej Zary if (err) 898*48a31030SOndrej Zary goto fail; 899*48a31030SOndrej Zary 900*48a31030SOndrej Zary if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32))) { 901*48a31030SOndrej Zary dev_warn(&pdev->dev, "Unable to set 32-bit DMA mask\n"); 902*48a31030SOndrej Zary goto disable_device; 903*48a31030SOndrej Zary } 904*48a31030SOndrej Zary 905*48a31030SOndrej Zary err = pci_request_regions(pdev, "wd719x"); 906*48a31030SOndrej Zary if (err) 907*48a31030SOndrej Zary goto disable_device; 908*48a31030SOndrej Zary pci_set_master(pdev); 909*48a31030SOndrej Zary 910*48a31030SOndrej Zary err = -ENODEV; 911*48a31030SOndrej Zary if (pci_resource_len(pdev, 0) == 0) 912*48a31030SOndrej Zary goto release_region; 913*48a31030SOndrej Zary 914*48a31030SOndrej Zary err = -ENOMEM; 915*48a31030SOndrej Zary sh = scsi_host_alloc(&wd719x_template, sizeof(struct wd719x)); 916*48a31030SOndrej Zary if (!sh) 917*48a31030SOndrej Zary goto release_region; 918*48a31030SOndrej Zary 919*48a31030SOndrej Zary wd = shost_priv(sh); 920*48a31030SOndrej Zary wd->base = pci_iomap(pdev, 0, 0); 921*48a31030SOndrej Zary if (!wd->base) 922*48a31030SOndrej Zary goto free_host; 923*48a31030SOndrej Zary wd->pdev = pdev; 924*48a31030SOndrej Zary 925*48a31030SOndrej Zary err = wd719x_board_found(sh); 926*48a31030SOndrej Zary if (err) 927*48a31030SOndrej Zary goto unmap; 928*48a31030SOndrej Zary 929*48a31030SOndrej Zary err = scsi_add_host(sh, &wd->pdev->dev); 930*48a31030SOndrej Zary if (err) 931*48a31030SOndrej Zary goto destroy; 932*48a31030SOndrej Zary 933*48a31030SOndrej Zary scsi_scan_host(sh); 934*48a31030SOndrej Zary 935*48a31030SOndrej Zary pci_set_drvdata(pdev, sh); 936*48a31030SOndrej Zary return 0; 937*48a31030SOndrej Zary 938*48a31030SOndrej Zary destroy: 939*48a31030SOndrej Zary wd719x_destroy(wd); 940*48a31030SOndrej Zary unmap: 941*48a31030SOndrej Zary pci_iounmap(pdev, wd->base); 942*48a31030SOndrej Zary free_host: 943*48a31030SOndrej Zary scsi_host_put(sh); 944*48a31030SOndrej Zary release_region: 945*48a31030SOndrej Zary pci_release_regions(pdev); 946*48a31030SOndrej Zary disable_device: 947*48a31030SOndrej Zary pci_disable_device(pdev); 948*48a31030SOndrej Zary fail: 949*48a31030SOndrej Zary return err; 950*48a31030SOndrej Zary } 951*48a31030SOndrej Zary 952*48a31030SOndrej Zary 953*48a31030SOndrej Zary static void wd719x_pci_remove(struct pci_dev *pdev) 954*48a31030SOndrej Zary { 955*48a31030SOndrej Zary struct Scsi_Host *sh = pci_get_drvdata(pdev); 956*48a31030SOndrej Zary struct wd719x *wd = shost_priv(sh); 957*48a31030SOndrej Zary 958*48a31030SOndrej Zary scsi_remove_host(sh); 959*48a31030SOndrej Zary wd719x_destroy(wd); 960*48a31030SOndrej Zary pci_iounmap(pdev, wd->base); 961*48a31030SOndrej Zary pci_release_regions(pdev); 962*48a31030SOndrej Zary pci_disable_device(pdev); 963*48a31030SOndrej Zary 964*48a31030SOndrej Zary scsi_host_put(sh); 965*48a31030SOndrej Zary } 966*48a31030SOndrej Zary 967*48a31030SOndrej Zary static DEFINE_PCI_DEVICE_TABLE(wd719x_pci_table) = { 968*48a31030SOndrej Zary { PCI_DEVICE(PCI_VENDOR_ID_WD, 0x3296) }, 969*48a31030SOndrej Zary {} 970*48a31030SOndrej Zary }; 971*48a31030SOndrej Zary 972*48a31030SOndrej Zary MODULE_DEVICE_TABLE(pci, wd719x_pci_table); 973*48a31030SOndrej Zary 974*48a31030SOndrej Zary static struct pci_driver wd719x_pci_driver = { 975*48a31030SOndrej Zary .name = "wd719x", 976*48a31030SOndrej Zary .id_table = wd719x_pci_table, 977*48a31030SOndrej Zary .probe = wd719x_pci_probe, 978*48a31030SOndrej Zary .remove = wd719x_pci_remove, 979*48a31030SOndrej Zary }; 980*48a31030SOndrej Zary 981*48a31030SOndrej Zary static int __init wd719x_init(void) 982*48a31030SOndrej Zary { 983*48a31030SOndrej Zary return pci_register_driver(&wd719x_pci_driver); 984*48a31030SOndrej Zary } 985*48a31030SOndrej Zary 986*48a31030SOndrej Zary static void __exit wd719x_exit(void) 987*48a31030SOndrej Zary { 988*48a31030SOndrej Zary pci_unregister_driver(&wd719x_pci_driver); 989*48a31030SOndrej Zary } 990*48a31030SOndrej Zary 991*48a31030SOndrej Zary module_init(wd719x_init); 992*48a31030SOndrej Zary module_exit(wd719x_exit); 993*48a31030SOndrej Zary 994*48a31030SOndrej Zary MODULE_DESCRIPTION("Western Digital WD7193/7197/7296 SCSI driver"); 995*48a31030SOndrej Zary MODULE_AUTHOR("Ondrej Zary, Aaron Dewell, Juergen Gaertner"); 996*48a31030SOndrej Zary MODULE_LICENSE("GPL"); 997*48a31030SOndrej Zary MODULE_FIRMWARE("wd719x-wcs.bin"); 998*48a31030SOndrej Zary MODULE_FIRMWARE("wd719x-risc.bin"); 999