109c434b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
248a31030SOndrej Zary /*
348a31030SOndrej Zary * Driver for Western Digital WD7193, WD7197 and WD7296 SCSI cards
448a31030SOndrej Zary * Copyright 2013 Ondrej Zary
548a31030SOndrej Zary *
648a31030SOndrej Zary * Original driver by
748a31030SOndrej Zary * Aaron Dewell <dewell@woods.net>
848a31030SOndrej Zary * Gaerti <Juergen.Gaertner@mbox.si.uni-hannover.de>
948a31030SOndrej Zary *
1048a31030SOndrej Zary * HW documentation available in book:
1148a31030SOndrej Zary *
1248a31030SOndrej Zary * SPIDER Command Protocol
1348a31030SOndrej Zary * by Chandru M. Sippy
1448a31030SOndrej Zary * SCSI Storage Products (MCP)
1548a31030SOndrej Zary * Western Digital Corporation
1648a31030SOndrej Zary * 09-15-95
1748a31030SOndrej Zary *
1848a31030SOndrej Zary * http://web.archive.org/web/20070717175254/http://sun1.rrzn.uni-hannover.de/gaertner.juergen/wd719x/Linux/Docu/Spider/
1948a31030SOndrej Zary */
2048a31030SOndrej Zary
2148a31030SOndrej Zary /*
2248a31030SOndrej Zary * Driver workflow:
2348a31030SOndrej Zary * 1. SCSI command is transformed to SCB (Spider Control Block) by the
2448a31030SOndrej Zary * queuecommand function.
2548a31030SOndrej Zary * 2. The address of the SCB is stored in a list to be able to access it, if
2648a31030SOndrej Zary * something goes wrong.
2748a31030SOndrej Zary * 3. The address of the SCB is written to the Controller, which loads the SCB
2848a31030SOndrej Zary * via BM-DMA and processes it.
2948a31030SOndrej Zary * 4. After it has finished, it generates an interrupt, and sets registers.
3048a31030SOndrej Zary *
3148a31030SOndrej Zary * flaws:
3248a31030SOndrej Zary * - abort/reset functions
3348a31030SOndrej Zary *
3448a31030SOndrej Zary * ToDo:
3548a31030SOndrej Zary * - tagged queueing
3648a31030SOndrej Zary */
3748a31030SOndrej Zary
3848a31030SOndrej Zary #include <linux/interrupt.h>
3948a31030SOndrej Zary #include <linux/module.h>
4048a31030SOndrej Zary #include <linux/delay.h>
4148a31030SOndrej Zary #include <linux/pci.h>
4248a31030SOndrej Zary #include <linux/firmware.h>
4348a31030SOndrej Zary #include <linux/eeprom_93cx6.h>
4448a31030SOndrej Zary #include <scsi/scsi_cmnd.h>
4548a31030SOndrej Zary #include <scsi/scsi_device.h>
4648a31030SOndrej Zary #include <scsi/scsi_host.h>
4748a31030SOndrej Zary #include "wd719x.h"
4848a31030SOndrej Zary
4948a31030SOndrej Zary /* low-level register access */
wd719x_readb(struct wd719x * wd,u8 reg)5048a31030SOndrej Zary static inline u8 wd719x_readb(struct wd719x *wd, u8 reg)
5148a31030SOndrej Zary {
5248a31030SOndrej Zary return ioread8(wd->base + reg);
5348a31030SOndrej Zary }
5448a31030SOndrej Zary
wd719x_readl(struct wd719x * wd,u8 reg)5548a31030SOndrej Zary static inline u32 wd719x_readl(struct wd719x *wd, u8 reg)
5648a31030SOndrej Zary {
5748a31030SOndrej Zary return ioread32(wd->base + reg);
5848a31030SOndrej Zary }
5948a31030SOndrej Zary
wd719x_writeb(struct wd719x * wd,u8 reg,u8 val)6048a31030SOndrej Zary static inline void wd719x_writeb(struct wd719x *wd, u8 reg, u8 val)
6148a31030SOndrej Zary {
6248a31030SOndrej Zary iowrite8(val, wd->base + reg);
6348a31030SOndrej Zary }
6448a31030SOndrej Zary
wd719x_writew(struct wd719x * wd,u8 reg,u16 val)6548a31030SOndrej Zary static inline void wd719x_writew(struct wd719x *wd, u8 reg, u16 val)
6648a31030SOndrej Zary {
6748a31030SOndrej Zary iowrite16(val, wd->base + reg);
6848a31030SOndrej Zary }
6948a31030SOndrej Zary
wd719x_writel(struct wd719x * wd,u8 reg,u32 val)7048a31030SOndrej Zary static inline void wd719x_writel(struct wd719x *wd, u8 reg, u32 val)
7148a31030SOndrej Zary {
7248a31030SOndrej Zary iowrite32(val, wd->base + reg);
7348a31030SOndrej Zary }
7448a31030SOndrej Zary
7548a31030SOndrej Zary /* wait until the command register is ready */
wd719x_wait_ready(struct wd719x * wd)7648a31030SOndrej Zary static inline int wd719x_wait_ready(struct wd719x *wd)
7748a31030SOndrej Zary {
7848a31030SOndrej Zary int i = 0;
7948a31030SOndrej Zary
8048a31030SOndrej Zary do {
8148a31030SOndrej Zary if (wd719x_readb(wd, WD719X_AMR_COMMAND) == WD719X_CMD_READY)
8248a31030SOndrej Zary return 0;
8348a31030SOndrej Zary udelay(1);
8448a31030SOndrej Zary } while (i++ < WD719X_WAIT_FOR_CMD_READY);
8548a31030SOndrej Zary
8648a31030SOndrej Zary dev_err(&wd->pdev->dev, "command register is not ready: 0x%02x\n",
8748a31030SOndrej Zary wd719x_readb(wd, WD719X_AMR_COMMAND));
8848a31030SOndrej Zary
8948a31030SOndrej Zary return -ETIMEDOUT;
9048a31030SOndrej Zary }
9148a31030SOndrej Zary
9248a31030SOndrej Zary /* poll interrupt status register until command finishes */
wd719x_wait_done(struct wd719x * wd,int timeout)9348a31030SOndrej Zary static inline int wd719x_wait_done(struct wd719x *wd, int timeout)
9448a31030SOndrej Zary {
9548a31030SOndrej Zary u8 status;
9648a31030SOndrej Zary
9748a31030SOndrej Zary while (timeout > 0) {
9848a31030SOndrej Zary status = wd719x_readb(wd, WD719X_AMR_INT_STATUS);
9948a31030SOndrej Zary if (status)
10048a31030SOndrej Zary break;
10148a31030SOndrej Zary timeout--;
10248a31030SOndrej Zary udelay(1);
10348a31030SOndrej Zary }
10448a31030SOndrej Zary
10548a31030SOndrej Zary if (timeout <= 0) {
10648a31030SOndrej Zary dev_err(&wd->pdev->dev, "direct command timed out\n");
10748a31030SOndrej Zary return -ETIMEDOUT;
10848a31030SOndrej Zary }
10948a31030SOndrej Zary
11048a31030SOndrej Zary if (status != WD719X_INT_NOERRORS) {
1115da1faa0SOndrej Zary u8 sue = wd719x_readb(wd, WD719X_AMR_SCB_ERROR);
1125da1faa0SOndrej Zary /* we get this after wd719x_dev_reset, it's not an error */
1135da1faa0SOndrej Zary if (sue == WD719X_SUE_TERM)
1145da1faa0SOndrej Zary return 0;
1155da1faa0SOndrej Zary /* we get this after wd719x_bus_reset, it's not an error */
1165da1faa0SOndrej Zary if (sue == WD719X_SUE_RESET)
1175da1faa0SOndrej Zary return 0;
11848a31030SOndrej Zary dev_err(&wd->pdev->dev, "direct command failed, status 0x%02x, SUE 0x%02x\n",
1195da1faa0SOndrej Zary status, sue);
12048a31030SOndrej Zary return -EIO;
12148a31030SOndrej Zary }
12248a31030SOndrej Zary
12348a31030SOndrej Zary return 0;
12448a31030SOndrej Zary }
12548a31030SOndrej Zary
wd719x_direct_cmd(struct wd719x * wd,u8 opcode,u8 dev,u8 lun,u8 tag,dma_addr_t data,int timeout)12648a31030SOndrej Zary static int wd719x_direct_cmd(struct wd719x *wd, u8 opcode, u8 dev, u8 lun,
12748a31030SOndrej Zary u8 tag, dma_addr_t data, int timeout)
12848a31030SOndrej Zary {
12948a31030SOndrej Zary int ret = 0;
13048a31030SOndrej Zary
13148a31030SOndrej Zary /* clear interrupt status register (allow command register to clear) */
13248a31030SOndrej Zary wd719x_writeb(wd, WD719X_AMR_INT_STATUS, WD719X_INT_NONE);
13348a31030SOndrej Zary
13448a31030SOndrej Zary /* Wait for the Command register to become free */
13548a31030SOndrej Zary if (wd719x_wait_ready(wd))
13648a31030SOndrej Zary return -ETIMEDOUT;
13748a31030SOndrej Zary
1385da1faa0SOndrej Zary /* disable interrupts except for RESET/ABORT (it breaks them) */
1395da1faa0SOndrej Zary if (opcode != WD719X_CMD_BUSRESET && opcode != WD719X_CMD_ABORT &&
1405da1faa0SOndrej Zary opcode != WD719X_CMD_ABORT_TAG && opcode != WD719X_CMD_RESET)
14148a31030SOndrej Zary dev |= WD719X_DISABLE_INT;
14248a31030SOndrej Zary wd719x_writeb(wd, WD719X_AMR_CMD_PARAM, dev);
14348a31030SOndrej Zary wd719x_writeb(wd, WD719X_AMR_CMD_PARAM_2, lun);
14448a31030SOndrej Zary wd719x_writeb(wd, WD719X_AMR_CMD_PARAM_3, tag);
14548a31030SOndrej Zary if (data)
14648a31030SOndrej Zary wd719x_writel(wd, WD719X_AMR_SCB_IN, data);
14748a31030SOndrej Zary
14848a31030SOndrej Zary /* clear interrupt status register again */
14948a31030SOndrej Zary wd719x_writeb(wd, WD719X_AMR_INT_STATUS, WD719X_INT_NONE);
15048a31030SOndrej Zary
15148a31030SOndrej Zary /* Now, write the command */
15248a31030SOndrej Zary wd719x_writeb(wd, WD719X_AMR_COMMAND, opcode);
15348a31030SOndrej Zary
15448a31030SOndrej Zary if (timeout) /* wait for the command to complete */
15548a31030SOndrej Zary ret = wd719x_wait_done(wd, timeout);
15648a31030SOndrej Zary
15748a31030SOndrej Zary /* clear interrupt status register (clean up) */
15848a31030SOndrej Zary if (opcode != WD719X_CMD_READ_FIRMVER)
15948a31030SOndrej Zary wd719x_writeb(wd, WD719X_AMR_INT_STATUS, WD719X_INT_NONE);
16048a31030SOndrej Zary
16148a31030SOndrej Zary return ret;
16248a31030SOndrej Zary }
16348a31030SOndrej Zary
wd719x_destroy(struct wd719x * wd)16448a31030SOndrej Zary static void wd719x_destroy(struct wd719x *wd)
16548a31030SOndrej Zary {
16648a31030SOndrej Zary /* stop the RISC */
16748a31030SOndrej Zary if (wd719x_direct_cmd(wd, WD719X_CMD_SLEEP, 0, 0, 0, 0,
16848a31030SOndrej Zary WD719X_WAIT_FOR_RISC))
16948a31030SOndrej Zary dev_warn(&wd->pdev->dev, "RISC sleep command failed\n");
17048a31030SOndrej Zary /* disable RISC */
17148a31030SOndrej Zary wd719x_writeb(wd, WD719X_PCI_MODE_SELECT, 0);
17248a31030SOndrej Zary
173d9c30dbcSChristoph Hellwig WARN_ON_ONCE(!list_empty(&wd->active_scbs));
174d9c30dbcSChristoph Hellwig
17548a31030SOndrej Zary /* free internal buffers */
176236bd823SChristoph Hellwig dma_free_coherent(&wd->pdev->dev, wd->fw_size, wd->fw_virt,
177236bd823SChristoph Hellwig wd->fw_phys);
17848a31030SOndrej Zary wd->fw_virt = NULL;
179236bd823SChristoph Hellwig dma_free_coherent(&wd->pdev->dev, WD719X_HASH_TABLE_SIZE, wd->hash_virt,
18048a31030SOndrej Zary wd->hash_phys);
18148a31030SOndrej Zary wd->hash_virt = NULL;
182236bd823SChristoph Hellwig dma_free_coherent(&wd->pdev->dev, sizeof(struct wd719x_host_param),
18348a31030SOndrej Zary wd->params, wd->params_phys);
18448a31030SOndrej Zary wd->params = NULL;
18548a31030SOndrej Zary free_irq(wd->pdev->irq, wd);
18648a31030SOndrej Zary }
18748a31030SOndrej Zary
188fde46e96SChristoph Hellwig /* finish a SCSI command, unmap buffers */
wd719x_finish_cmd(struct wd719x_scb * scb,int result)189fde46e96SChristoph Hellwig static void wd719x_finish_cmd(struct wd719x_scb *scb, int result)
19048a31030SOndrej Zary {
191fde46e96SChristoph Hellwig struct scsi_cmnd *cmd = scb->cmd;
19248a31030SOndrej Zary struct wd719x *wd = shost_priv(cmd->device->host);
19348a31030SOndrej Zary
194fde46e96SChristoph Hellwig list_del(&scb->list);
195fde46e96SChristoph Hellwig
196fde46e96SChristoph Hellwig dma_unmap_single(&wd->pdev->dev, scb->phys,
197fde46e96SChristoph Hellwig sizeof(struct wd719x_scb), DMA_BIDIRECTIONAL);
198fde46e96SChristoph Hellwig scsi_dma_unmap(cmd);
19970d1b920SBart Van Assche dma_unmap_single(&wd->pdev->dev, scb->dma_handle,
20048a31030SOndrej Zary SCSI_SENSE_BUFFERSIZE, DMA_FROM_DEVICE);
201fde46e96SChristoph Hellwig
20248a31030SOndrej Zary cmd->result = result << 16;
203f11e4da6SBart Van Assche scsi_done(cmd);
20448a31030SOndrej Zary }
20548a31030SOndrej Zary
20648a31030SOndrej Zary /* Build a SCB and send it to the card */
wd719x_queuecommand(struct Scsi_Host * sh,struct scsi_cmnd * cmd)20748a31030SOndrej Zary static int wd719x_queuecommand(struct Scsi_Host *sh, struct scsi_cmnd *cmd)
20848a31030SOndrej Zary {
20948a31030SOndrej Zary int i, count_sg;
21048a31030SOndrej Zary unsigned long flags;
211fde46e96SChristoph Hellwig struct wd719x_scb *scb = scsi_cmd_priv(cmd);
21248a31030SOndrej Zary struct wd719x *wd = shost_priv(sh);
21348a31030SOndrej Zary
21448a31030SOndrej Zary scb->cmd = cmd;
21548a31030SOndrej Zary
21648a31030SOndrej Zary scb->CDB_tag = 0; /* Tagged queueing not supported yet */
21748a31030SOndrej Zary scb->devid = cmd->device->id;
21848a31030SOndrej Zary scb->lun = cmd->device->lun;
21948a31030SOndrej Zary
22048a31030SOndrej Zary /* copy the command */
22148a31030SOndrej Zary memcpy(scb->CDB, cmd->cmnd, cmd->cmd_len);
22248a31030SOndrej Zary
223fde46e96SChristoph Hellwig /* map SCB */
224fde46e96SChristoph Hellwig scb->phys = dma_map_single(&wd->pdev->dev, scb, sizeof(*scb),
225fde46e96SChristoph Hellwig DMA_BIDIRECTIONAL);
226fde46e96SChristoph Hellwig
227fde46e96SChristoph Hellwig if (dma_mapping_error(&wd->pdev->dev, scb->phys))
228fde46e96SChristoph Hellwig goto out_error;
229fde46e96SChristoph Hellwig
23048a31030SOndrej Zary /* map sense buffer */
23148a31030SOndrej Zary scb->sense_buf_length = SCSI_SENSE_BUFFERSIZE;
23270d1b920SBart Van Assche scb->dma_handle = dma_map_single(&wd->pdev->dev, cmd->sense_buffer,
23348a31030SOndrej Zary SCSI_SENSE_BUFFERSIZE, DMA_FROM_DEVICE);
23470d1b920SBart Van Assche if (dma_mapping_error(&wd->pdev->dev, scb->dma_handle))
235fde46e96SChristoph Hellwig goto out_unmap_scb;
23670d1b920SBart Van Assche scb->sense_buf = cpu_to_le32(scb->dma_handle);
23748a31030SOndrej Zary
23848a31030SOndrej Zary /* request autosense */
23948a31030SOndrej Zary scb->SCB_options |= WD719X_SCB_FLAGS_AUTO_REQUEST_SENSE;
24048a31030SOndrej Zary
24148a31030SOndrej Zary /* check direction */
24248a31030SOndrej Zary if (cmd->sc_data_direction == DMA_TO_DEVICE)
24348a31030SOndrej Zary scb->SCB_options |= WD719X_SCB_FLAGS_CHECK_DIRECTION
24448a31030SOndrej Zary | WD719X_SCB_FLAGS_PCI_TO_SCSI;
24548a31030SOndrej Zary else if (cmd->sc_data_direction == DMA_FROM_DEVICE)
24648a31030SOndrej Zary scb->SCB_options |= WD719X_SCB_FLAGS_CHECK_DIRECTION;
24748a31030SOndrej Zary
24848a31030SOndrej Zary /* Scather/gather */
24948a31030SOndrej Zary count_sg = scsi_dma_map(cmd);
250fde46e96SChristoph Hellwig if (count_sg < 0)
251fde46e96SChristoph Hellwig goto out_unmap_sense;
25248a31030SOndrej Zary BUG_ON(count_sg > WD719X_SG);
25348a31030SOndrej Zary
25448a31030SOndrej Zary if (count_sg) {
25548a31030SOndrej Zary struct scatterlist *sg;
25648a31030SOndrej Zary
25748a31030SOndrej Zary scb->data_length = cpu_to_le32(count_sg *
25848a31030SOndrej Zary sizeof(struct wd719x_sglist));
25948a31030SOndrej Zary scb->data_p = cpu_to_le32(scb->phys +
26048a31030SOndrej Zary offsetof(struct wd719x_scb, sg_list));
26148a31030SOndrej Zary
26248a31030SOndrej Zary scsi_for_each_sg(cmd, sg, count_sg, i) {
26348a31030SOndrej Zary scb->sg_list[i].ptr = cpu_to_le32(sg_dma_address(sg));
26448a31030SOndrej Zary scb->sg_list[i].length = cpu_to_le32(sg_dma_len(sg));
26548a31030SOndrej Zary }
26648a31030SOndrej Zary scb->SCB_options |= WD719X_SCB_FLAGS_DO_SCATTER_GATHER;
26748a31030SOndrej Zary } else { /* zero length */
26848a31030SOndrej Zary scb->data_length = 0;
26948a31030SOndrej Zary scb->data_p = 0;
27048a31030SOndrej Zary }
27148a31030SOndrej Zary
272fde46e96SChristoph Hellwig spin_lock_irqsave(wd->sh->host_lock, flags);
273fde46e96SChristoph Hellwig
27448a31030SOndrej Zary /* check if the Command register is free */
27548a31030SOndrej Zary if (wd719x_readb(wd, WD719X_AMR_COMMAND) != WD719X_CMD_READY) {
27648a31030SOndrej Zary spin_unlock_irqrestore(wd->sh->host_lock, flags);
27748a31030SOndrej Zary return SCSI_MLQUEUE_HOST_BUSY;
27848a31030SOndrej Zary }
27948a31030SOndrej Zary
280fde46e96SChristoph Hellwig list_add(&scb->list, &wd->active_scbs);
281fde46e96SChristoph Hellwig
28248a31030SOndrej Zary /* write pointer to the AMR */
28348a31030SOndrej Zary wd719x_writel(wd, WD719X_AMR_SCB_IN, scb->phys);
28448a31030SOndrej Zary /* send SCB opcode */
28548a31030SOndrej Zary wd719x_writeb(wd, WD719X_AMR_COMMAND, WD719X_CMD_PROCESS_SCB);
28648a31030SOndrej Zary
28748a31030SOndrej Zary spin_unlock_irqrestore(wd->sh->host_lock, flags);
288fde46e96SChristoph Hellwig return 0;
28948a31030SOndrej Zary
290fde46e96SChristoph Hellwig out_unmap_sense:
29170d1b920SBart Van Assche dma_unmap_single(&wd->pdev->dev, scb->dma_handle,
292fde46e96SChristoph Hellwig SCSI_SENSE_BUFFERSIZE, DMA_FROM_DEVICE);
293fde46e96SChristoph Hellwig out_unmap_scb:
294fde46e96SChristoph Hellwig dma_unmap_single(&wd->pdev->dev, scb->phys, sizeof(*scb),
295fde46e96SChristoph Hellwig DMA_BIDIRECTIONAL);
296fde46e96SChristoph Hellwig out_error:
297fde46e96SChristoph Hellwig cmd->result = DID_ERROR << 16;
298f11e4da6SBart Van Assche scsi_done(cmd);
29948a31030SOndrej Zary return 0;
30048a31030SOndrej Zary }
30148a31030SOndrej Zary
wd719x_chip_init(struct wd719x * wd)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)
330236bd823SChristoph Hellwig wd->fw_virt = dma_alloc_coherent(&wd->pdev->dev, wd->fw_size,
331236bd823SChristoph Hellwig &wd->fw_phys, GFP_KERNEL);
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
wd719x_abort(struct scsi_cmnd * cmd)46348a31030SOndrej Zary static int wd719x_abort(struct scsi_cmnd *cmd)
46448a31030SOndrej Zary {
46548a31030SOndrej Zary int action, result;
46648a31030SOndrej Zary unsigned long flags;
467fde46e96SChristoph Hellwig struct wd719x_scb *scb = scsi_cmd_priv(cmd);
46848a31030SOndrej Zary struct wd719x *wd = shost_priv(cmd->device->host);
469e2a1dc57SJohn Garry struct device *dev = &wd->pdev->dev;
47048a31030SOndrej Zary
471e2a1dc57SJohn Garry dev_info(dev, "abort command, tag: %x\n", scsi_cmd_to_rq(cmd)->tag);
47248a31030SOndrej Zary
473e2a1dc57SJohn Garry action = WD719X_CMD_ABORT;
47448a31030SOndrej Zary
47548a31030SOndrej Zary spin_lock_irqsave(wd->sh->host_lock, flags);
47648a31030SOndrej Zary result = wd719x_direct_cmd(wd, action, cmd->device->id,
477e2a1dc57SJohn Garry cmd->device->lun, scsi_cmd_to_rq(cmd)->tag,
478e2a1dc57SJohn Garry scb->phys, 0);
4795da1faa0SOndrej Zary wd719x_finish_cmd(scb, DID_ABORT);
48048a31030SOndrej Zary spin_unlock_irqrestore(wd->sh->host_lock, flags);
48148a31030SOndrej Zary if (result)
48248a31030SOndrej Zary return FAILED;
48348a31030SOndrej Zary
48448a31030SOndrej Zary return SUCCESS;
48548a31030SOndrej Zary }
48648a31030SOndrej Zary
wd719x_reset(struct scsi_cmnd * cmd,u8 opcode,u8 device)48748a31030SOndrej Zary static int wd719x_reset(struct scsi_cmnd *cmd, u8 opcode, u8 device)
48848a31030SOndrej Zary {
48948a31030SOndrej Zary int result;
49048a31030SOndrej Zary unsigned long flags;
49148a31030SOndrej Zary struct wd719x *wd = shost_priv(cmd->device->host);
4925da1faa0SOndrej Zary struct wd719x_scb *scb, *tmp;
49348a31030SOndrej Zary
49448a31030SOndrej Zary dev_info(&wd->pdev->dev, "%s reset requested\n",
49548a31030SOndrej Zary (opcode == WD719X_CMD_BUSRESET) ? "bus" : "device");
49648a31030SOndrej Zary
49748a31030SOndrej Zary spin_lock_irqsave(wd->sh->host_lock, flags);
49848a31030SOndrej Zary result = wd719x_direct_cmd(wd, opcode, device, 0, 0, 0,
49948a31030SOndrej Zary WD719X_WAIT_FOR_SCSI_RESET);
5005da1faa0SOndrej Zary /* flush all SCBs (or all for a device if dev_reset) */
5015da1faa0SOndrej Zary list_for_each_entry_safe(scb, tmp, &wd->active_scbs, list) {
5025da1faa0SOndrej Zary if (opcode == WD719X_CMD_BUSRESET ||
5035da1faa0SOndrej Zary scb->cmd->device->id == device)
5045da1faa0SOndrej Zary wd719x_finish_cmd(scb, DID_RESET);
5055da1faa0SOndrej Zary }
50648a31030SOndrej Zary spin_unlock_irqrestore(wd->sh->host_lock, flags);
50748a31030SOndrej Zary if (result)
50848a31030SOndrej Zary return FAILED;
50948a31030SOndrej Zary
51048a31030SOndrej Zary return SUCCESS;
51148a31030SOndrej Zary }
51248a31030SOndrej Zary
wd719x_dev_reset(struct scsi_cmnd * cmd)51348a31030SOndrej Zary static int wd719x_dev_reset(struct scsi_cmnd *cmd)
51448a31030SOndrej Zary {
51548a31030SOndrej Zary return wd719x_reset(cmd, WD719X_CMD_RESET, cmd->device->id);
51648a31030SOndrej Zary }
51748a31030SOndrej Zary
wd719x_bus_reset(struct scsi_cmnd * cmd)51848a31030SOndrej Zary static int wd719x_bus_reset(struct scsi_cmnd *cmd)
51948a31030SOndrej Zary {
52048a31030SOndrej Zary return wd719x_reset(cmd, WD719X_CMD_BUSRESET, 0);
52148a31030SOndrej Zary }
52248a31030SOndrej Zary
wd719x_host_reset(struct scsi_cmnd * cmd)52348a31030SOndrej Zary static int wd719x_host_reset(struct scsi_cmnd *cmd)
52448a31030SOndrej Zary {
52548a31030SOndrej Zary struct wd719x *wd = shost_priv(cmd->device->host);
52648a31030SOndrej Zary struct wd719x_scb *scb, *tmp;
52748a31030SOndrej Zary unsigned long flags;
52848a31030SOndrej Zary
52948a31030SOndrej Zary dev_info(&wd->pdev->dev, "host reset requested\n");
53048a31030SOndrej Zary spin_lock_irqsave(wd->sh->host_lock, flags);
5315da1faa0SOndrej Zary /* stop the RISC */
5325da1faa0SOndrej Zary if (wd719x_direct_cmd(wd, WD719X_CMD_SLEEP, 0, 0, 0, 0,
5335da1faa0SOndrej Zary WD719X_WAIT_FOR_RISC))
5345da1faa0SOndrej Zary dev_warn(&wd->pdev->dev, "RISC sleep command failed\n");
5355da1faa0SOndrej Zary /* disable RISC */
5365da1faa0SOndrej Zary wd719x_writeb(wd, WD719X_PCI_MODE_SELECT, 0);
53748a31030SOndrej Zary
53848a31030SOndrej Zary /* flush all SCBs */
539fde46e96SChristoph Hellwig list_for_each_entry_safe(scb, tmp, &wd->active_scbs, list)
5405da1faa0SOndrej Zary wd719x_finish_cmd(scb, DID_RESET);
54148a31030SOndrej Zary spin_unlock_irqrestore(wd->sh->host_lock, flags);
54248a31030SOndrej Zary
5435da1faa0SOndrej Zary /* Try to reinit the RISC */
5445da1faa0SOndrej Zary return wd719x_chip_init(wd) == 0 ? SUCCESS : FAILED;
54548a31030SOndrej Zary }
54648a31030SOndrej Zary
wd719x_biosparam(struct scsi_device * sdev,struct block_device * bdev,sector_t capacity,int geom[])54748a31030SOndrej Zary static int wd719x_biosparam(struct scsi_device *sdev, struct block_device *bdev,
54848a31030SOndrej Zary sector_t capacity, int geom[])
54948a31030SOndrej Zary {
55048a31030SOndrej Zary if (capacity >= 0x200000) {
55148a31030SOndrej Zary geom[0] = 255; /* heads */
55248a31030SOndrej Zary geom[1] = 63; /* sectors */
55348a31030SOndrej Zary } else {
55448a31030SOndrej Zary geom[0] = 64; /* heads */
55548a31030SOndrej Zary geom[1] = 32; /* sectors */
55648a31030SOndrej Zary }
55748a31030SOndrej Zary geom[2] = sector_div(capacity, geom[0] * geom[1]); /* cylinders */
55848a31030SOndrej Zary
55948a31030SOndrej Zary return 0;
56048a31030SOndrej Zary }
56148a31030SOndrej Zary
56248a31030SOndrej Zary /* process a SCB-completion interrupt */
wd719x_interrupt_SCB(struct wd719x * wd,union wd719x_regs regs,struct wd719x_scb * scb)56348a31030SOndrej Zary static inline void wd719x_interrupt_SCB(struct wd719x *wd,
56448a31030SOndrej Zary union wd719x_regs regs,
56548a31030SOndrej Zary struct wd719x_scb *scb)
56648a31030SOndrej Zary {
56748a31030SOndrej Zary int result;
56848a31030SOndrej Zary
56948a31030SOndrej Zary /* now have to find result from card */
57048a31030SOndrej Zary switch (regs.bytes.SUE) {
57148a31030SOndrej Zary case WD719X_SUE_NOERRORS:
57248a31030SOndrej Zary result = DID_OK;
57348a31030SOndrej Zary break;
57448a31030SOndrej Zary case WD719X_SUE_REJECTED:
57548a31030SOndrej Zary dev_err(&wd->pdev->dev, "command rejected\n");
57648a31030SOndrej Zary result = DID_ERROR;
57748a31030SOndrej Zary break;
57848a31030SOndrej Zary case WD719X_SUE_SCBQFULL:
57948a31030SOndrej Zary dev_err(&wd->pdev->dev, "SCB queue is full\n");
58048a31030SOndrej Zary result = DID_ERROR;
58148a31030SOndrej Zary break;
58248a31030SOndrej Zary case WD719X_SUE_TERM:
58348a31030SOndrej Zary dev_dbg(&wd->pdev->dev, "SCB terminated by direct command\n");
58448a31030SOndrej Zary result = DID_ABORT; /* or DID_RESET? */
58548a31030SOndrej Zary break;
58648a31030SOndrej Zary case WD719X_SUE_CHAN1ABORT:
58748a31030SOndrej Zary case WD719X_SUE_CHAN23ABORT:
58848a31030SOndrej Zary result = DID_ABORT;
58948a31030SOndrej Zary dev_err(&wd->pdev->dev, "DMA abort\n");
59048a31030SOndrej Zary break;
59148a31030SOndrej Zary case WD719X_SUE_CHAN1PAR:
59248a31030SOndrej Zary case WD719X_SUE_CHAN23PAR:
59348a31030SOndrej Zary result = DID_PARITY;
59448a31030SOndrej Zary dev_err(&wd->pdev->dev, "DMA parity error\n");
59548a31030SOndrej Zary break;
59648a31030SOndrej Zary case WD719X_SUE_TIMEOUT:
59748a31030SOndrej Zary result = DID_TIME_OUT;
59848a31030SOndrej Zary dev_dbg(&wd->pdev->dev, "selection timeout\n");
59948a31030SOndrej Zary break;
60048a31030SOndrej Zary case WD719X_SUE_RESET:
601804ff603SMasanari Iida dev_dbg(&wd->pdev->dev, "bus reset occurred\n");
60248a31030SOndrej Zary result = DID_RESET;
60348a31030SOndrej Zary break;
60448a31030SOndrej Zary case WD719X_SUE_BUSERROR:
60548a31030SOndrej Zary dev_dbg(&wd->pdev->dev, "SCSI bus error\n");
60648a31030SOndrej Zary result = DID_ERROR;
60748a31030SOndrej Zary break;
60848a31030SOndrej Zary case WD719X_SUE_WRONGWAY:
60948a31030SOndrej Zary dev_err(&wd->pdev->dev, "wrong data transfer direction\n");
61048a31030SOndrej Zary result = DID_ERROR;
61148a31030SOndrej Zary break;
61248a31030SOndrej Zary case WD719X_SUE_BADPHASE:
61348a31030SOndrej Zary dev_err(&wd->pdev->dev, "invalid SCSI phase\n");
61448a31030SOndrej Zary result = DID_ERROR;
61548a31030SOndrej Zary break;
61648a31030SOndrej Zary case WD719X_SUE_TOOLONG:
61748a31030SOndrej Zary dev_err(&wd->pdev->dev, "record too long\n");
61848a31030SOndrej Zary result = DID_ERROR;
61948a31030SOndrej Zary break;
62048a31030SOndrej Zary case WD719X_SUE_BUSFREE:
62148a31030SOndrej Zary dev_err(&wd->pdev->dev, "unexpected bus free\n");
62248a31030SOndrej Zary result = DID_NO_CONNECT; /* or DID_ERROR ???*/
62348a31030SOndrej Zary break;
62448a31030SOndrej Zary case WD719X_SUE_ARSDONE:
62548a31030SOndrej Zary dev_dbg(&wd->pdev->dev, "auto request sense\n");
62648a31030SOndrej Zary if (regs.bytes.SCSI == 0)
62748a31030SOndrej Zary result = DID_OK;
62848a31030SOndrej Zary else
62948a31030SOndrej Zary result = DID_PARITY;
63048a31030SOndrej Zary break;
63148a31030SOndrej Zary case WD719X_SUE_IGNORED:
63248a31030SOndrej Zary dev_err(&wd->pdev->dev, "target id %d ignored command\n",
63348a31030SOndrej Zary scb->cmd->device->id);
63448a31030SOndrej Zary result = DID_NO_CONNECT;
63548a31030SOndrej Zary break;
63648a31030SOndrej Zary case WD719X_SUE_WRONGTAGS:
63748a31030SOndrej Zary dev_err(&wd->pdev->dev, "reversed tags\n");
63848a31030SOndrej Zary result = DID_ERROR;
63948a31030SOndrej Zary break;
64048a31030SOndrej Zary case WD719X_SUE_BADTAGS:
64148a31030SOndrej Zary dev_err(&wd->pdev->dev, "tag type not supported by target\n");
64248a31030SOndrej Zary result = DID_ERROR;
64348a31030SOndrej Zary break;
64448a31030SOndrej Zary case WD719X_SUE_NOSCAMID:
64548a31030SOndrej Zary dev_err(&wd->pdev->dev, "no SCAM soft ID available\n");
64648a31030SOndrej Zary result = DID_ERROR;
64748a31030SOndrej Zary break;
64848a31030SOndrej Zary default:
64948a31030SOndrej Zary dev_warn(&wd->pdev->dev, "unknown SUE error code: 0x%x\n",
65048a31030SOndrej Zary regs.bytes.SUE);
65148a31030SOndrej Zary result = DID_ERROR;
65248a31030SOndrej Zary break;
65348a31030SOndrej Zary }
65448a31030SOndrej Zary
655fde46e96SChristoph Hellwig wd719x_finish_cmd(scb, result);
65648a31030SOndrej Zary }
65748a31030SOndrej Zary
wd719x_interrupt(int irq,void * dev_id)65848a31030SOndrej Zary static irqreturn_t wd719x_interrupt(int irq, void *dev_id)
65948a31030SOndrej Zary {
66048a31030SOndrej Zary struct wd719x *wd = dev_id;
66148a31030SOndrej Zary union wd719x_regs regs;
66248a31030SOndrej Zary unsigned long flags;
66348a31030SOndrej Zary u32 SCB_out;
66448a31030SOndrej Zary
66548a31030SOndrej Zary spin_lock_irqsave(wd->sh->host_lock, flags);
66648a31030SOndrej Zary /* read SCB pointer back from card */
66748a31030SOndrej Zary SCB_out = wd719x_readl(wd, WD719X_AMR_SCB_OUT);
66848a31030SOndrej Zary /* read all status info at once */
66948a31030SOndrej Zary regs.all = cpu_to_le32(wd719x_readl(wd, WD719X_AMR_OP_CODE));
67048a31030SOndrej Zary
67148a31030SOndrej Zary switch (regs.bytes.INT) {
67248a31030SOndrej Zary case WD719X_INT_NONE:
67348a31030SOndrej Zary spin_unlock_irqrestore(wd->sh->host_lock, flags);
67448a31030SOndrej Zary return IRQ_NONE;
67548a31030SOndrej Zary case WD719X_INT_LINKNOSTATUS:
67648a31030SOndrej Zary dev_err(&wd->pdev->dev, "linked command completed with no status\n");
67748a31030SOndrej Zary break;
67848a31030SOndrej Zary case WD719X_INT_BADINT:
67948a31030SOndrej Zary dev_err(&wd->pdev->dev, "unsolicited interrupt\n");
68048a31030SOndrej Zary break;
68148a31030SOndrej Zary case WD719X_INT_NOERRORS:
68248a31030SOndrej Zary case WD719X_INT_LINKNOERRORS:
68348a31030SOndrej Zary case WD719X_INT_ERRORSLOGGED:
68448a31030SOndrej Zary case WD719X_INT_SPIDERFAILED:
68548a31030SOndrej Zary /* was the cmd completed a direct or SCB command? */
68648a31030SOndrej Zary if (regs.bytes.OPC == WD719X_CMD_PROCESS_SCB) {
68748a31030SOndrej Zary struct wd719x_scb *scb;
68848a31030SOndrej Zary list_for_each_entry(scb, &wd->active_scbs, list)
68948a31030SOndrej Zary if (SCB_out == scb->phys)
69048a31030SOndrej Zary break;
69148a31030SOndrej Zary if (SCB_out == scb->phys)
69248a31030SOndrej Zary wd719x_interrupt_SCB(wd, regs, scb);
69348a31030SOndrej Zary else
69448a31030SOndrej Zary dev_err(&wd->pdev->dev, "card returned invalid SCB pointer\n");
69548a31030SOndrej Zary } else
6965da1faa0SOndrej Zary dev_dbg(&wd->pdev->dev, "direct command 0x%x completed\n",
69748a31030SOndrej Zary regs.bytes.OPC);
69848a31030SOndrej Zary break;
69948a31030SOndrej Zary case WD719X_INT_PIOREADY:
70048a31030SOndrej Zary dev_err(&wd->pdev->dev, "card indicates PIO data ready but we never use PIO\n");
70148a31030SOndrej Zary /* interrupt will not be cleared until all data is read */
70248a31030SOndrej Zary break;
70348a31030SOndrej Zary default:
70448a31030SOndrej Zary dev_err(&wd->pdev->dev, "unknown interrupt reason: %d\n",
70548a31030SOndrej Zary regs.bytes.INT);
70648a31030SOndrej Zary
70748a31030SOndrej Zary }
70848a31030SOndrej Zary /* clear interrupt so another can happen */
70948a31030SOndrej Zary wd719x_writeb(wd, WD719X_AMR_INT_STATUS, WD719X_INT_NONE);
71048a31030SOndrej Zary spin_unlock_irqrestore(wd->sh->host_lock, flags);
71148a31030SOndrej Zary
71248a31030SOndrej Zary return IRQ_HANDLED;
71348a31030SOndrej Zary }
71448a31030SOndrej Zary
wd719x_eeprom_reg_read(struct eeprom_93cx6 * eeprom)71548a31030SOndrej Zary static void wd719x_eeprom_reg_read(struct eeprom_93cx6 *eeprom)
71648a31030SOndrej Zary {
71748a31030SOndrej Zary struct wd719x *wd = eeprom->data;
71848a31030SOndrej Zary u8 reg = wd719x_readb(wd, WD719X_PCI_GPIO_DATA);
71948a31030SOndrej Zary
72048a31030SOndrej Zary eeprom->reg_data_out = reg & WD719X_EE_DO;
72148a31030SOndrej Zary }
72248a31030SOndrej Zary
wd719x_eeprom_reg_write(struct eeprom_93cx6 * eeprom)72348a31030SOndrej Zary static void wd719x_eeprom_reg_write(struct eeprom_93cx6 *eeprom)
72448a31030SOndrej Zary {
72548a31030SOndrej Zary struct wd719x *wd = eeprom->data;
72648a31030SOndrej Zary u8 reg = 0;
72748a31030SOndrej Zary
72848a31030SOndrej Zary if (eeprom->reg_data_in)
72948a31030SOndrej Zary reg |= WD719X_EE_DI;
73048a31030SOndrej Zary if (eeprom->reg_data_clock)
73148a31030SOndrej Zary reg |= WD719X_EE_CLK;
73248a31030SOndrej Zary if (eeprom->reg_chip_select)
73348a31030SOndrej Zary reg |= WD719X_EE_CS;
73448a31030SOndrej Zary
73548a31030SOndrej Zary wd719x_writeb(wd, WD719X_PCI_GPIO_DATA, reg);
73648a31030SOndrej Zary }
73748a31030SOndrej Zary
73848a31030SOndrej Zary /* read config from EEPROM so it can be downloaded by the RISC on (re-)init */
wd719x_read_eeprom(struct wd719x * wd)73948a31030SOndrej Zary static void wd719x_read_eeprom(struct wd719x *wd)
74048a31030SOndrej Zary {
74148a31030SOndrej Zary struct eeprom_93cx6 eeprom;
74248a31030SOndrej Zary u8 gpio;
74348a31030SOndrej Zary struct wd719x_eeprom_header header;
74448a31030SOndrej Zary
74548a31030SOndrej Zary eeprom.data = wd;
74648a31030SOndrej Zary eeprom.register_read = wd719x_eeprom_reg_read;
74748a31030SOndrej Zary eeprom.register_write = wd719x_eeprom_reg_write;
74848a31030SOndrej Zary eeprom.width = PCI_EEPROM_WIDTH_93C46;
74948a31030SOndrej Zary
75048a31030SOndrej Zary /* set all outputs to low */
75148a31030SOndrej Zary wd719x_writeb(wd, WD719X_PCI_GPIO_DATA, 0);
75248a31030SOndrej Zary /* configure GPIO pins */
75348a31030SOndrej Zary gpio = wd719x_readb(wd, WD719X_PCI_GPIO_CONTROL);
75448a31030SOndrej Zary /* GPIO outputs */
75548a31030SOndrej Zary gpio &= (~(WD719X_EE_CLK | WD719X_EE_DI | WD719X_EE_CS));
75648a31030SOndrej Zary /* GPIO input */
75748a31030SOndrej Zary gpio |= WD719X_EE_DO;
75848a31030SOndrej Zary wd719x_writeb(wd, WD719X_PCI_GPIO_CONTROL, gpio);
75948a31030SOndrej Zary
76048a31030SOndrej Zary /* read EEPROM header */
76148a31030SOndrej Zary eeprom_93cx6_multireadb(&eeprom, 0, (u8 *)&header, sizeof(header));
76248a31030SOndrej Zary
76348a31030SOndrej Zary if (header.sig1 == 'W' && header.sig2 == 'D')
76448a31030SOndrej Zary eeprom_93cx6_multireadb(&eeprom, header.cfg_offset,
76548a31030SOndrej Zary (u8 *)wd->params,
76648a31030SOndrej Zary sizeof(struct wd719x_host_param));
76748a31030SOndrej Zary else { /* default EEPROM values */
76848a31030SOndrej Zary dev_warn(&wd->pdev->dev, "EEPROM signature is invalid (0x%02x 0x%02x), using default values\n",
76948a31030SOndrej Zary header.sig1, header.sig2);
77048a31030SOndrej Zary wd->params->ch_1_th = 0x10; /* 16 DWs = 64 B */
77148a31030SOndrej Zary wd->params->scsi_conf = 0x4c; /* 48ma, spue, parity check */
77248a31030SOndrej Zary wd->params->own_scsi_id = 0x07; /* ID 7, SCAM disabled */
77348a31030SOndrej Zary wd->params->sel_timeout = 0x4d; /* 250 ms */
77448a31030SOndrej Zary wd->params->sleep_timer = 0x01;
77548a31030SOndrej Zary wd->params->cdb_size = cpu_to_le16(0x5555); /* all 6 B */
77648a31030SOndrej Zary wd->params->scsi_pad = 0x1b;
77748a31030SOndrej Zary if (wd->type == WD719X_TYPE_7193) /* narrow card - disable */
77848a31030SOndrej Zary wd->params->wide = cpu_to_le32(0x00000000);
77948a31030SOndrej Zary else /* initiate & respond to WIDE messages */
78048a31030SOndrej Zary wd->params->wide = cpu_to_le32(0xffffffff);
78148a31030SOndrej Zary wd->params->sync = cpu_to_le32(0xffffffff);
78248a31030SOndrej Zary wd->params->soft_mask = 0x00; /* all disabled */
78348a31030SOndrej Zary wd->params->unsol_mask = 0x00; /* all disabled */
78448a31030SOndrej Zary }
78548a31030SOndrej Zary /* disable TAGGED messages */
78648a31030SOndrej Zary wd->params->tag_en = cpu_to_le16(0x0000);
78748a31030SOndrej Zary }
78848a31030SOndrej Zary
78948a31030SOndrej Zary /* Read card type from GPIO bits 1 and 3 */
wd719x_detect_type(struct wd719x * wd)79048a31030SOndrej Zary static enum wd719x_card_type wd719x_detect_type(struct wd719x *wd)
79148a31030SOndrej Zary {
79248a31030SOndrej Zary u8 card = wd719x_readb(wd, WD719X_PCI_GPIO_CONTROL);
79348a31030SOndrej Zary
79448a31030SOndrej Zary card |= WD719X_GPIO_ID_BITS;
79548a31030SOndrej Zary wd719x_writeb(wd, WD719X_PCI_GPIO_CONTROL, card);
79648a31030SOndrej Zary card = wd719x_readb(wd, WD719X_PCI_GPIO_DATA) & WD719X_GPIO_ID_BITS;
79748a31030SOndrej Zary switch (card) {
79848a31030SOndrej Zary case 0x08:
79948a31030SOndrej Zary return WD719X_TYPE_7193;
80048a31030SOndrej Zary case 0x02:
80148a31030SOndrej Zary return WD719X_TYPE_7197;
80248a31030SOndrej Zary case 0x00:
80348a31030SOndrej Zary return WD719X_TYPE_7296;
80448a31030SOndrej Zary default:
80548a31030SOndrej Zary dev_warn(&wd->pdev->dev, "unknown card type 0x%x\n", card);
80648a31030SOndrej Zary return WD719X_TYPE_UNKNOWN;
80748a31030SOndrej Zary }
80848a31030SOndrej Zary }
80948a31030SOndrej Zary
wd719x_board_found(struct Scsi_Host * sh)81048a31030SOndrej Zary static int wd719x_board_found(struct Scsi_Host *sh)
81148a31030SOndrej Zary {
81248a31030SOndrej Zary struct wd719x *wd = shost_priv(sh);
813d828e5c6SColin Ian King static const char * const card_types[] = {
814d828e5c6SColin Ian King "Unknown card", "WD7193", "WD7197", "WD7296"
815d828e5c6SColin Ian King };
81648a31030SOndrej Zary int ret;
81748a31030SOndrej Zary
81848a31030SOndrej Zary INIT_LIST_HEAD(&wd->active_scbs);
81948a31030SOndrej Zary
82048a31030SOndrej Zary sh->base = pci_resource_start(wd->pdev, 0);
82148a31030SOndrej Zary
82248a31030SOndrej Zary wd->type = wd719x_detect_type(wd);
82348a31030SOndrej Zary
82448a31030SOndrej Zary wd->sh = sh;
82548a31030SOndrej Zary sh->irq = wd->pdev->irq;
82648a31030SOndrej Zary wd->fw_virt = NULL;
82748a31030SOndrej Zary
82848a31030SOndrej Zary /* memory area for host (EEPROM) parameters */
829236bd823SChristoph Hellwig wd->params = dma_alloc_coherent(&wd->pdev->dev,
83048a31030SOndrej Zary sizeof(struct wd719x_host_param),
831236bd823SChristoph Hellwig &wd->params_phys, GFP_KERNEL);
83248a31030SOndrej Zary if (!wd->params) {
83348a31030SOndrej Zary dev_warn(&wd->pdev->dev, "unable to allocate parameter buffer\n");
83448a31030SOndrej Zary return -ENOMEM;
83548a31030SOndrej Zary }
83648a31030SOndrej Zary
83748a31030SOndrej Zary /* memory area for the RISC for hash table of outstanding requests */
838236bd823SChristoph Hellwig wd->hash_virt = dma_alloc_coherent(&wd->pdev->dev,
839236bd823SChristoph Hellwig WD719X_HASH_TABLE_SIZE,
840236bd823SChristoph Hellwig &wd->hash_phys, GFP_KERNEL);
84148a31030SOndrej Zary if (!wd->hash_virt) {
84248a31030SOndrej Zary dev_warn(&wd->pdev->dev, "unable to allocate hash buffer\n");
84348a31030SOndrej Zary ret = -ENOMEM;
84448a31030SOndrej Zary goto fail_free_params;
84548a31030SOndrej Zary }
84648a31030SOndrej Zary
84748a31030SOndrej Zary ret = request_irq(wd->pdev->irq, wd719x_interrupt, IRQF_SHARED,
84848a31030SOndrej Zary "wd719x", wd);
84948a31030SOndrej Zary if (ret) {
85048a31030SOndrej Zary dev_warn(&wd->pdev->dev, "unable to assign IRQ %d\n",
85148a31030SOndrej Zary wd->pdev->irq);
85248a31030SOndrej Zary goto fail_free_hash;
85348a31030SOndrej Zary }
85448a31030SOndrej Zary
85548a31030SOndrej Zary /* read parameters from EEPROM */
85648a31030SOndrej Zary wd719x_read_eeprom(wd);
85748a31030SOndrej Zary
85848a31030SOndrej Zary ret = wd719x_chip_init(wd);
85948a31030SOndrej Zary if (ret)
86048a31030SOndrej Zary goto fail_free_irq;
86148a31030SOndrej Zary
86248a31030SOndrej Zary sh->this_id = wd->params->own_scsi_id & WD719X_EE_SCSI_ID_MASK;
86348a31030SOndrej Zary
86448a31030SOndrej Zary dev_info(&wd->pdev->dev, "%s at I/O 0x%lx, IRQ %u, SCSI ID %d\n",
86548a31030SOndrej Zary card_types[wd->type], sh->base, sh->irq, sh->this_id);
86648a31030SOndrej Zary
86748a31030SOndrej Zary return 0;
86848a31030SOndrej Zary
86948a31030SOndrej Zary fail_free_irq:
87048a31030SOndrej Zary free_irq(wd->pdev->irq, wd);
87148a31030SOndrej Zary fail_free_hash:
872236bd823SChristoph Hellwig dma_free_coherent(&wd->pdev->dev, WD719X_HASH_TABLE_SIZE, wd->hash_virt,
87348a31030SOndrej Zary wd->hash_phys);
87448a31030SOndrej Zary fail_free_params:
875236bd823SChristoph Hellwig dma_free_coherent(&wd->pdev->dev, sizeof(struct wd719x_host_param),
87648a31030SOndrej Zary wd->params, wd->params_phys);
87748a31030SOndrej Zary
87848a31030SOndrej Zary return ret;
87948a31030SOndrej Zary }
88048a31030SOndrej Zary
881*a5b78e81SBart Van Assche static const struct scsi_host_template wd719x_template = {
8822ecf8e0aSOndrej Zary .module = THIS_MODULE,
88348a31030SOndrej Zary .name = "Western Digital 719x",
884fde46e96SChristoph Hellwig .cmd_size = sizeof(struct wd719x_scb),
88548a31030SOndrej Zary .queuecommand = wd719x_queuecommand,
88648a31030SOndrej Zary .eh_abort_handler = wd719x_abort,
88748a31030SOndrej Zary .eh_device_reset_handler = wd719x_dev_reset,
88848a31030SOndrej Zary .eh_bus_reset_handler = wd719x_bus_reset,
88948a31030SOndrej Zary .eh_host_reset_handler = wd719x_host_reset,
89048a31030SOndrej Zary .bios_param = wd719x_biosparam,
89148a31030SOndrej Zary .proc_name = "wd719x",
89248a31030SOndrej Zary .can_queue = 255,
89348a31030SOndrej Zary .this_id = 7,
89448a31030SOndrej Zary .sg_tablesize = WD719X_SG,
89548a31030SOndrej Zary };
89648a31030SOndrej Zary
wd719x_pci_probe(struct pci_dev * pdev,const struct pci_device_id * d)89748a31030SOndrej Zary static int wd719x_pci_probe(struct pci_dev *pdev, const struct pci_device_id *d)
89848a31030SOndrej Zary {
89948a31030SOndrej Zary int err;
90048a31030SOndrej Zary struct Scsi_Host *sh;
90148a31030SOndrej Zary struct wd719x *wd;
90248a31030SOndrej Zary
90348a31030SOndrej Zary err = pci_enable_device(pdev);
90448a31030SOndrej Zary if (err)
90548a31030SOndrej Zary goto fail;
90648a31030SOndrej Zary
90798cdcd6cSZheyu Ma err = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
90898cdcd6cSZheyu Ma if (err) {
90948a31030SOndrej Zary dev_warn(&pdev->dev, "Unable to set 32-bit DMA mask\n");
91048a31030SOndrej Zary goto disable_device;
91148a31030SOndrej Zary }
91248a31030SOndrej Zary
91348a31030SOndrej Zary err = pci_request_regions(pdev, "wd719x");
91448a31030SOndrej Zary if (err)
91548a31030SOndrej Zary goto disable_device;
91648a31030SOndrej Zary pci_set_master(pdev);
91748a31030SOndrej Zary
91848a31030SOndrej Zary err = -ENODEV;
91948a31030SOndrej Zary if (pci_resource_len(pdev, 0) == 0)
92048a31030SOndrej Zary goto release_region;
92148a31030SOndrej Zary
92248a31030SOndrej Zary err = -ENOMEM;
92348a31030SOndrej Zary sh = scsi_host_alloc(&wd719x_template, sizeof(struct wd719x));
92448a31030SOndrej Zary if (!sh)
92548a31030SOndrej Zary goto release_region;
92648a31030SOndrej Zary
92748a31030SOndrej Zary wd = shost_priv(sh);
92848a31030SOndrej Zary wd->base = pci_iomap(pdev, 0, 0);
92948a31030SOndrej Zary if (!wd->base)
93048a31030SOndrej Zary goto free_host;
93148a31030SOndrej Zary wd->pdev = pdev;
93248a31030SOndrej Zary
93348a31030SOndrej Zary err = wd719x_board_found(sh);
93448a31030SOndrej Zary if (err)
93548a31030SOndrej Zary goto unmap;
93648a31030SOndrej Zary
93748a31030SOndrej Zary err = scsi_add_host(sh, &wd->pdev->dev);
93848a31030SOndrej Zary if (err)
93948a31030SOndrej Zary goto destroy;
94048a31030SOndrej Zary
94148a31030SOndrej Zary scsi_scan_host(sh);
94248a31030SOndrej Zary
94348a31030SOndrej Zary pci_set_drvdata(pdev, sh);
94448a31030SOndrej Zary return 0;
94548a31030SOndrej Zary
94648a31030SOndrej Zary destroy:
94748a31030SOndrej Zary wd719x_destroy(wd);
94848a31030SOndrej Zary unmap:
94948a31030SOndrej Zary pci_iounmap(pdev, wd->base);
95048a31030SOndrej Zary free_host:
95148a31030SOndrej Zary scsi_host_put(sh);
95248a31030SOndrej Zary release_region:
95348a31030SOndrej Zary pci_release_regions(pdev);
95448a31030SOndrej Zary disable_device:
95548a31030SOndrej Zary pci_disable_device(pdev);
95648a31030SOndrej Zary fail:
95748a31030SOndrej Zary return err;
95848a31030SOndrej Zary }
95948a31030SOndrej Zary
96048a31030SOndrej Zary
wd719x_pci_remove(struct pci_dev * pdev)96148a31030SOndrej Zary static void wd719x_pci_remove(struct pci_dev *pdev)
96248a31030SOndrej Zary {
96348a31030SOndrej Zary struct Scsi_Host *sh = pci_get_drvdata(pdev);
96448a31030SOndrej Zary struct wd719x *wd = shost_priv(sh);
96548a31030SOndrej Zary
96648a31030SOndrej Zary scsi_remove_host(sh);
96748a31030SOndrej Zary wd719x_destroy(wd);
96848a31030SOndrej Zary pci_iounmap(pdev, wd->base);
96948a31030SOndrej Zary pci_release_regions(pdev);
97048a31030SOndrej Zary pci_disable_device(pdev);
97148a31030SOndrej Zary
97248a31030SOndrej Zary scsi_host_put(sh);
97348a31030SOndrej Zary }
97448a31030SOndrej Zary
9758a793beaSJoe Perches static const struct pci_device_id wd719x_pci_table[] = {
97648a31030SOndrej Zary { PCI_DEVICE(PCI_VENDOR_ID_WD, 0x3296) },
97748a31030SOndrej Zary {}
97848a31030SOndrej Zary };
97948a31030SOndrej Zary
98048a31030SOndrej Zary MODULE_DEVICE_TABLE(pci, wd719x_pci_table);
98148a31030SOndrej Zary
98248a31030SOndrej Zary static struct pci_driver wd719x_pci_driver = {
98348a31030SOndrej Zary .name = "wd719x",
98448a31030SOndrej Zary .id_table = wd719x_pci_table,
98548a31030SOndrej Zary .probe = wd719x_pci_probe,
98648a31030SOndrej Zary .remove = wd719x_pci_remove,
98748a31030SOndrej Zary };
98848a31030SOndrej Zary
9893e1bbc56SYueHaibing module_pci_driver(wd719x_pci_driver);
99048a31030SOndrej Zary
99148a31030SOndrej Zary MODULE_DESCRIPTION("Western Digital WD7193/7197/7296 SCSI driver");
99248a31030SOndrej Zary MODULE_AUTHOR("Ondrej Zary, Aaron Dewell, Juergen Gaertner");
99348a31030SOndrej Zary MODULE_LICENSE("GPL");
99448a31030SOndrej Zary MODULE_FIRMWARE("wd719x-wcs.bin");
99548a31030SOndrej Zary MODULE_FIRMWARE("wd719x-risc.bin");
996