xref: /linux/drivers/scsi/wd719x.c (revision 09c434b8a0047c69e48499de0107de312901e798)
1*09c434b8SThomas 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 */
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 
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 
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 
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 
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 */
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 */
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) {
11148a31030SOndrej Zary 		dev_err(&wd->pdev->dev, "direct command failed, status 0x%02x, SUE 0x%02x\n",
11248a31030SOndrej Zary 			status, wd719x_readb(wd, WD719X_AMR_SCB_ERROR));
11348a31030SOndrej Zary 		return -EIO;
11448a31030SOndrej Zary 	}
11548a31030SOndrej Zary 
11648a31030SOndrej Zary 	return 0;
11748a31030SOndrej Zary }
11848a31030SOndrej Zary 
11948a31030SOndrej Zary static int wd719x_direct_cmd(struct wd719x *wd, u8 opcode, u8 dev, u8 lun,
12048a31030SOndrej Zary 			     u8 tag, dma_addr_t data, int timeout)
12148a31030SOndrej Zary {
12248a31030SOndrej Zary 	int ret = 0;
12348a31030SOndrej Zary 
12448a31030SOndrej Zary 	/* clear interrupt status register (allow command register to clear) */
12548a31030SOndrej Zary 	wd719x_writeb(wd, WD719X_AMR_INT_STATUS, WD719X_INT_NONE);
12648a31030SOndrej Zary 
12748a31030SOndrej Zary 	/* Wait for the Command register to become free */
12848a31030SOndrej Zary 	if (wd719x_wait_ready(wd))
12948a31030SOndrej Zary 		return -ETIMEDOUT;
13048a31030SOndrej Zary 
13148a31030SOndrej Zary 	/* make sure we get NO interrupts */
13248a31030SOndrej Zary 	dev |= WD719X_DISABLE_INT;
13348a31030SOndrej Zary 	wd719x_writeb(wd, WD719X_AMR_CMD_PARAM, dev);
13448a31030SOndrej Zary 	wd719x_writeb(wd, WD719X_AMR_CMD_PARAM_2, lun);
13548a31030SOndrej Zary 	wd719x_writeb(wd, WD719X_AMR_CMD_PARAM_3, tag);
13648a31030SOndrej Zary 	if (data)
13748a31030SOndrej Zary 		wd719x_writel(wd, WD719X_AMR_SCB_IN, data);
13848a31030SOndrej Zary 
13948a31030SOndrej Zary 	/* clear interrupt status register again */
14048a31030SOndrej Zary 	wd719x_writeb(wd, WD719X_AMR_INT_STATUS, WD719X_INT_NONE);
14148a31030SOndrej Zary 
14248a31030SOndrej Zary 	/* Now, write the command */
14348a31030SOndrej Zary 	wd719x_writeb(wd, WD719X_AMR_COMMAND, opcode);
14448a31030SOndrej Zary 
14548a31030SOndrej Zary 	if (timeout)	/* wait for the command to complete */
14648a31030SOndrej Zary 		ret = wd719x_wait_done(wd, timeout);
14748a31030SOndrej Zary 
14848a31030SOndrej Zary 	/* clear interrupt status register (clean up) */
14948a31030SOndrej Zary 	if (opcode != WD719X_CMD_READ_FIRMVER)
15048a31030SOndrej Zary 		wd719x_writeb(wd, WD719X_AMR_INT_STATUS, WD719X_INT_NONE);
15148a31030SOndrej Zary 
15248a31030SOndrej Zary 	return ret;
15348a31030SOndrej Zary }
15448a31030SOndrej Zary 
15548a31030SOndrej Zary static void wd719x_destroy(struct wd719x *wd)
15648a31030SOndrej Zary {
15748a31030SOndrej Zary 	/* stop the RISC */
15848a31030SOndrej Zary 	if (wd719x_direct_cmd(wd, WD719X_CMD_SLEEP, 0, 0, 0, 0,
15948a31030SOndrej Zary 			      WD719X_WAIT_FOR_RISC))
16048a31030SOndrej Zary 		dev_warn(&wd->pdev->dev, "RISC sleep command failed\n");
16148a31030SOndrej Zary 	/* disable RISC */
16248a31030SOndrej Zary 	wd719x_writeb(wd, WD719X_PCI_MODE_SELECT, 0);
16348a31030SOndrej Zary 
164d9c30dbcSChristoph Hellwig 	WARN_ON_ONCE(!list_empty(&wd->active_scbs));
165d9c30dbcSChristoph Hellwig 
16648a31030SOndrej Zary 	/* free internal buffers */
167236bd823SChristoph Hellwig 	dma_free_coherent(&wd->pdev->dev, wd->fw_size, wd->fw_virt,
168236bd823SChristoph Hellwig 			  wd->fw_phys);
16948a31030SOndrej Zary 	wd->fw_virt = NULL;
170236bd823SChristoph Hellwig 	dma_free_coherent(&wd->pdev->dev, WD719X_HASH_TABLE_SIZE, wd->hash_virt,
17148a31030SOndrej Zary 			  wd->hash_phys);
17248a31030SOndrej Zary 	wd->hash_virt = NULL;
173236bd823SChristoph Hellwig 	dma_free_coherent(&wd->pdev->dev, sizeof(struct wd719x_host_param),
17448a31030SOndrej Zary 			  wd->params, wd->params_phys);
17548a31030SOndrej Zary 	wd->params = NULL;
17648a31030SOndrej Zary 	free_irq(wd->pdev->irq, wd);
17748a31030SOndrej Zary }
17848a31030SOndrej Zary 
179fde46e96SChristoph Hellwig /* finish a SCSI command, unmap buffers */
180fde46e96SChristoph Hellwig static void wd719x_finish_cmd(struct wd719x_scb *scb, int result)
18148a31030SOndrej Zary {
182fde46e96SChristoph Hellwig 	struct scsi_cmnd *cmd = scb->cmd;
18348a31030SOndrej Zary 	struct wd719x *wd = shost_priv(cmd->device->host);
18448a31030SOndrej Zary 
185fde46e96SChristoph Hellwig 	list_del(&scb->list);
186fde46e96SChristoph Hellwig 
187fde46e96SChristoph Hellwig 	dma_unmap_single(&wd->pdev->dev, scb->phys,
188fde46e96SChristoph Hellwig 			sizeof(struct wd719x_scb), DMA_BIDIRECTIONAL);
189fde46e96SChristoph Hellwig 	scsi_dma_unmap(cmd);
19048a31030SOndrej Zary 	dma_unmap_single(&wd->pdev->dev, cmd->SCp.dma_handle,
19148a31030SOndrej Zary 			 SCSI_SENSE_BUFFERSIZE, DMA_FROM_DEVICE);
192fde46e96SChristoph Hellwig 
19348a31030SOndrej Zary 	cmd->result = result << 16;
19448a31030SOndrej Zary 	cmd->scsi_done(cmd);
19548a31030SOndrej Zary }
19648a31030SOndrej Zary 
19748a31030SOndrej Zary /* Build a SCB and send it to the card */
19848a31030SOndrej Zary static int wd719x_queuecommand(struct Scsi_Host *sh, struct scsi_cmnd *cmd)
19948a31030SOndrej Zary {
20048a31030SOndrej Zary 	int i, count_sg;
20148a31030SOndrej Zary 	unsigned long flags;
202fde46e96SChristoph Hellwig 	struct wd719x_scb *scb = scsi_cmd_priv(cmd);
20348a31030SOndrej Zary 	struct wd719x *wd = shost_priv(sh);
20448a31030SOndrej Zary 
20548a31030SOndrej Zary 	scb->cmd = cmd;
20648a31030SOndrej Zary 
20748a31030SOndrej Zary 	scb->CDB_tag = 0;	/* Tagged queueing not supported yet */
20848a31030SOndrej Zary 	scb->devid = cmd->device->id;
20948a31030SOndrej Zary 	scb->lun = cmd->device->lun;
21048a31030SOndrej Zary 
21148a31030SOndrej Zary 	/* copy the command */
21248a31030SOndrej Zary 	memcpy(scb->CDB, cmd->cmnd, cmd->cmd_len);
21348a31030SOndrej Zary 
214fde46e96SChristoph Hellwig 	/* map SCB */
215fde46e96SChristoph Hellwig 	scb->phys = dma_map_single(&wd->pdev->dev, scb, sizeof(*scb),
216fde46e96SChristoph Hellwig 				   DMA_BIDIRECTIONAL);
217fde46e96SChristoph Hellwig 
218fde46e96SChristoph Hellwig 	if (dma_mapping_error(&wd->pdev->dev, scb->phys))
219fde46e96SChristoph Hellwig 		goto out_error;
220fde46e96SChristoph Hellwig 
22148a31030SOndrej Zary 	/* map sense buffer */
22248a31030SOndrej Zary 	scb->sense_buf_length = SCSI_SENSE_BUFFERSIZE;
22348a31030SOndrej Zary 	cmd->SCp.dma_handle = dma_map_single(&wd->pdev->dev, cmd->sense_buffer,
22448a31030SOndrej Zary 			SCSI_SENSE_BUFFERSIZE, DMA_FROM_DEVICE);
225fde46e96SChristoph Hellwig 	if (dma_mapping_error(&wd->pdev->dev, cmd->SCp.dma_handle))
226fde46e96SChristoph Hellwig 		goto out_unmap_scb;
22748a31030SOndrej Zary 	scb->sense_buf = cpu_to_le32(cmd->SCp.dma_handle);
22848a31030SOndrej Zary 
22948a31030SOndrej Zary 	/* request autosense */
23048a31030SOndrej Zary 	scb->SCB_options |= WD719X_SCB_FLAGS_AUTO_REQUEST_SENSE;
23148a31030SOndrej Zary 
23248a31030SOndrej Zary 	/* check direction */
23348a31030SOndrej Zary 	if (cmd->sc_data_direction == DMA_TO_DEVICE)
23448a31030SOndrej Zary 		scb->SCB_options |= WD719X_SCB_FLAGS_CHECK_DIRECTION
23548a31030SOndrej Zary 				 |  WD719X_SCB_FLAGS_PCI_TO_SCSI;
23648a31030SOndrej Zary 	else if (cmd->sc_data_direction == DMA_FROM_DEVICE)
23748a31030SOndrej Zary 		scb->SCB_options |= WD719X_SCB_FLAGS_CHECK_DIRECTION;
23848a31030SOndrej Zary 
23948a31030SOndrej Zary 	/* Scather/gather */
24048a31030SOndrej Zary 	count_sg = scsi_dma_map(cmd);
241fde46e96SChristoph Hellwig 	if (count_sg < 0)
242fde46e96SChristoph Hellwig 		goto out_unmap_sense;
24348a31030SOndrej Zary 	BUG_ON(count_sg > WD719X_SG);
24448a31030SOndrej Zary 
24548a31030SOndrej Zary 	if (count_sg) {
24648a31030SOndrej Zary 		struct scatterlist *sg;
24748a31030SOndrej Zary 
24848a31030SOndrej Zary 		scb->data_length = cpu_to_le32(count_sg *
24948a31030SOndrej Zary 					       sizeof(struct wd719x_sglist));
25048a31030SOndrej Zary 		scb->data_p = cpu_to_le32(scb->phys +
25148a31030SOndrej Zary 					  offsetof(struct wd719x_scb, sg_list));
25248a31030SOndrej Zary 
25348a31030SOndrej Zary 		scsi_for_each_sg(cmd, sg, count_sg, i) {
25448a31030SOndrej Zary 			scb->sg_list[i].ptr = cpu_to_le32(sg_dma_address(sg));
25548a31030SOndrej Zary 			scb->sg_list[i].length = cpu_to_le32(sg_dma_len(sg));
25648a31030SOndrej Zary 		}
25748a31030SOndrej Zary 		scb->SCB_options |= WD719X_SCB_FLAGS_DO_SCATTER_GATHER;
25848a31030SOndrej Zary 	} else { /* zero length */
25948a31030SOndrej Zary 		scb->data_length = 0;
26048a31030SOndrej Zary 		scb->data_p = 0;
26148a31030SOndrej Zary 	}
26248a31030SOndrej Zary 
263fde46e96SChristoph Hellwig 	spin_lock_irqsave(wd->sh->host_lock, flags);
264fde46e96SChristoph Hellwig 
26548a31030SOndrej Zary 	/* check if the Command register is free */
26648a31030SOndrej Zary 	if (wd719x_readb(wd, WD719X_AMR_COMMAND) != WD719X_CMD_READY) {
26748a31030SOndrej Zary 		spin_unlock_irqrestore(wd->sh->host_lock, flags);
26848a31030SOndrej Zary 		return SCSI_MLQUEUE_HOST_BUSY;
26948a31030SOndrej Zary 	}
27048a31030SOndrej Zary 
271fde46e96SChristoph Hellwig 	list_add(&scb->list, &wd->active_scbs);
272fde46e96SChristoph Hellwig 
27348a31030SOndrej Zary 	/* write pointer to the AMR */
27448a31030SOndrej Zary 	wd719x_writel(wd, WD719X_AMR_SCB_IN, scb->phys);
27548a31030SOndrej Zary 	/* send SCB opcode */
27648a31030SOndrej Zary 	wd719x_writeb(wd, WD719X_AMR_COMMAND, WD719X_CMD_PROCESS_SCB);
27748a31030SOndrej Zary 
27848a31030SOndrej Zary 	spin_unlock_irqrestore(wd->sh->host_lock, flags);
279fde46e96SChristoph Hellwig 	return 0;
28048a31030SOndrej Zary 
281fde46e96SChristoph Hellwig out_unmap_sense:
282fde46e96SChristoph Hellwig 	dma_unmap_single(&wd->pdev->dev, cmd->SCp.dma_handle,
283fde46e96SChristoph Hellwig 			 SCSI_SENSE_BUFFERSIZE, DMA_FROM_DEVICE);
284fde46e96SChristoph Hellwig out_unmap_scb:
285fde46e96SChristoph Hellwig 	dma_unmap_single(&wd->pdev->dev, scb->phys, sizeof(*scb),
286fde46e96SChristoph Hellwig 			 DMA_BIDIRECTIONAL);
287fde46e96SChristoph Hellwig out_error:
288fde46e96SChristoph Hellwig 	cmd->result = DID_ERROR << 16;
289fde46e96SChristoph Hellwig 	cmd->scsi_done(cmd);
29048a31030SOndrej Zary 	return 0;
29148a31030SOndrej Zary }
29248a31030SOndrej Zary 
29348a31030SOndrej Zary static int wd719x_chip_init(struct wd719x *wd)
29448a31030SOndrej Zary {
29548a31030SOndrej Zary 	int i, ret;
29648a31030SOndrej Zary 	u32 risc_init[3];
29748a31030SOndrej Zary 	const struct firmware *fw_wcs, *fw_risc;
29848a31030SOndrej Zary 	const char fwname_wcs[] = "wd719x-wcs.bin";
29948a31030SOndrej Zary 	const char fwname_risc[] = "wd719x-risc.bin";
30048a31030SOndrej Zary 
30148a31030SOndrej Zary 	memset(wd->hash_virt, 0, WD719X_HASH_TABLE_SIZE);
30248a31030SOndrej Zary 
30348a31030SOndrej Zary 	/* WCS (sequencer) firmware */
30448a31030SOndrej Zary 	ret = request_firmware(&fw_wcs, fwname_wcs, &wd->pdev->dev);
30548a31030SOndrej Zary 	if (ret) {
30648a31030SOndrej Zary 		dev_err(&wd->pdev->dev, "Unable to load firmware %s: %d\n",
30748a31030SOndrej Zary 			fwname_wcs, ret);
30848a31030SOndrej Zary 		return ret;
30948a31030SOndrej Zary 	}
31048a31030SOndrej Zary 	/* RISC firmware */
31148a31030SOndrej Zary 	ret = request_firmware(&fw_risc, fwname_risc, &wd->pdev->dev);
31248a31030SOndrej Zary 	if (ret) {
31348a31030SOndrej Zary 		dev_err(&wd->pdev->dev, "Unable to load firmware %s: %d\n",
31448a31030SOndrej Zary 			fwname_risc, ret);
31548a31030SOndrej Zary 		release_firmware(fw_wcs);
31648a31030SOndrej Zary 		return ret;
31748a31030SOndrej Zary 	}
31848a31030SOndrej Zary 	wd->fw_size = ALIGN(fw_wcs->size, 4) + fw_risc->size;
31948a31030SOndrej Zary 
32048a31030SOndrej Zary 	if (!wd->fw_virt)
321236bd823SChristoph Hellwig 		wd->fw_virt = dma_alloc_coherent(&wd->pdev->dev, wd->fw_size,
322236bd823SChristoph Hellwig 						 &wd->fw_phys, GFP_KERNEL);
32348a31030SOndrej Zary 	if (!wd->fw_virt) {
32448a31030SOndrej Zary 		ret = -ENOMEM;
32548a31030SOndrej Zary 		goto wd719x_init_end;
32648a31030SOndrej Zary 	}
32748a31030SOndrej Zary 
32848a31030SOndrej Zary 	/* make a fresh copy of WCS and RISC code */
32948a31030SOndrej Zary 	memcpy(wd->fw_virt, fw_wcs->data, fw_wcs->size);
33048a31030SOndrej Zary 	memcpy(wd->fw_virt + ALIGN(fw_wcs->size, 4), fw_risc->data,
33148a31030SOndrej Zary 		fw_risc->size);
33248a31030SOndrej Zary 
33348a31030SOndrej Zary 	/* Reset the Spider Chip and adapter itself */
33448a31030SOndrej Zary 	wd719x_writeb(wd, WD719X_PCI_PORT_RESET, WD719X_PCI_RESET);
33548a31030SOndrej Zary 	udelay(WD719X_WAIT_FOR_RISC);
33648a31030SOndrej Zary 	/* Clear PIO mode bits set by BIOS */
33748a31030SOndrej Zary 	wd719x_writeb(wd, WD719X_AMR_CMD_PARAM, 0);
33848a31030SOndrej Zary 	/* ensure RISC is not running */
33948a31030SOndrej Zary 	wd719x_writeb(wd, WD719X_PCI_MODE_SELECT, 0);
34048a31030SOndrej Zary 	/* ensure command port is ready */
34148a31030SOndrej Zary 	wd719x_writeb(wd, WD719X_AMR_COMMAND, 0);
34248a31030SOndrej Zary 	if (wd719x_wait_ready(wd)) {
34348a31030SOndrej Zary 		ret = -ETIMEDOUT;
34448a31030SOndrej Zary 		goto wd719x_init_end;
34548a31030SOndrej Zary 	}
34648a31030SOndrej Zary 
34748a31030SOndrej Zary 	/* Transfer the first 2K words of RISC code to kick start the uP */
34848a31030SOndrej Zary 	risc_init[0] = wd->fw_phys;				/* WCS FW */
34948a31030SOndrej Zary 	risc_init[1] = wd->fw_phys + ALIGN(fw_wcs->size, 4);	/* RISC FW */
35048a31030SOndrej Zary 	risc_init[2] = wd->hash_phys;				/* hash table */
35148a31030SOndrej Zary 
35248a31030SOndrej Zary 	/* clear DMA status */
35348a31030SOndrej Zary 	wd719x_writeb(wd, WD719X_PCI_CHANNEL2_3STATUS, 0);
35448a31030SOndrej Zary 
35548a31030SOndrej Zary 	/* address to read firmware from */
35648a31030SOndrej Zary 	wd719x_writel(wd, WD719X_PCI_EXTERNAL_ADDR, risc_init[1]);
35748a31030SOndrej Zary 	/* base address to write firmware to (on card) */
35848a31030SOndrej Zary 	wd719x_writew(wd, WD719X_PCI_INTERNAL_ADDR, WD719X_PRAM_BASE_ADDR);
35948a31030SOndrej Zary 	/* size: first 2K words */
36048a31030SOndrej Zary 	wd719x_writew(wd, WD719X_PCI_DMA_TRANSFER_SIZE, 2048 * 2);
36148a31030SOndrej Zary 	/* start DMA */
36248a31030SOndrej Zary 	wd719x_writeb(wd, WD719X_PCI_CHANNEL2_3CMD, WD719X_START_CHANNEL2_3DMA);
36348a31030SOndrej Zary 
36448a31030SOndrej Zary 	/* wait for DMA to complete */
36548a31030SOndrej Zary 	i = WD719X_WAIT_FOR_RISC;
36648a31030SOndrej Zary 	while (i-- > 0) {
36748a31030SOndrej Zary 		u8 status = wd719x_readb(wd, WD719X_PCI_CHANNEL2_3STATUS);
36848a31030SOndrej Zary 		if (status == WD719X_START_CHANNEL2_3DONE)
36948a31030SOndrej Zary 			break;
37048a31030SOndrej Zary 		if (status == WD719X_START_CHANNEL2_3ABORT) {
37148a31030SOndrej Zary 			dev_warn(&wd->pdev->dev, "RISC bootstrap failed: DMA aborted\n");
37248a31030SOndrej Zary 			ret = -EIO;
37348a31030SOndrej Zary 			goto wd719x_init_end;
37448a31030SOndrej Zary 		}
37548a31030SOndrej Zary 		udelay(1);
37648a31030SOndrej Zary 	}
37748a31030SOndrej Zary 	if (i < 1) {
37848a31030SOndrej Zary 		dev_warn(&wd->pdev->dev, "RISC bootstrap failed: DMA timeout\n");
37948a31030SOndrej Zary 		ret = -ETIMEDOUT;
38048a31030SOndrej Zary 		goto wd719x_init_end;
38148a31030SOndrej Zary 	}
38248a31030SOndrej Zary 
38348a31030SOndrej Zary 	/* firmware is loaded, now initialize and wake up the RISC */
38448a31030SOndrej Zary 	/* write RISC initialization long words to Spider */
38548a31030SOndrej Zary 	wd719x_writel(wd, WD719X_AMR_SCB_IN, risc_init[0]);
38648a31030SOndrej Zary 	wd719x_writel(wd, WD719X_AMR_SCB_IN + 4, risc_init[1]);
38748a31030SOndrej Zary 	wd719x_writel(wd, WD719X_AMR_SCB_IN + 8, risc_init[2]);
38848a31030SOndrej Zary 
38948a31030SOndrej Zary 	/* disable interrupts during initialization of RISC */
39048a31030SOndrej Zary 	wd719x_writeb(wd, WD719X_AMR_CMD_PARAM, WD719X_DISABLE_INT);
39148a31030SOndrej Zary 
39248a31030SOndrej Zary 	/* issue INITIALIZE RISC comand */
39348a31030SOndrej Zary 	wd719x_writeb(wd, WD719X_AMR_COMMAND, WD719X_CMD_INIT_RISC);
39448a31030SOndrej Zary 	/* enable advanced mode (wake up RISC) */
39548a31030SOndrej Zary 	wd719x_writeb(wd, WD719X_PCI_MODE_SELECT, WD719X_ENABLE_ADVANCE_MODE);
39648a31030SOndrej Zary 	udelay(WD719X_WAIT_FOR_RISC);
39748a31030SOndrej Zary 
39848a31030SOndrej Zary 	ret = wd719x_wait_done(wd, WD719X_WAIT_FOR_RISC);
39948a31030SOndrej Zary 	/* clear interrupt status register */
40048a31030SOndrej Zary 	wd719x_writeb(wd, WD719X_AMR_INT_STATUS, WD719X_INT_NONE);
40148a31030SOndrej Zary 	if (ret) {
40248a31030SOndrej Zary 		dev_warn(&wd->pdev->dev, "Unable to initialize RISC\n");
40348a31030SOndrej Zary 		goto wd719x_init_end;
40448a31030SOndrej Zary 	}
40548a31030SOndrej Zary 	/* RISC is up and running */
40648a31030SOndrej Zary 
40748a31030SOndrej Zary 	/* Read FW version from RISC */
40848a31030SOndrej Zary 	ret = wd719x_direct_cmd(wd, WD719X_CMD_READ_FIRMVER, 0, 0, 0, 0,
40948a31030SOndrej Zary 				WD719X_WAIT_FOR_RISC);
41048a31030SOndrej Zary 	if (ret) {
41148a31030SOndrej Zary 		dev_warn(&wd->pdev->dev, "Unable to read firmware version\n");
41248a31030SOndrej Zary 		goto wd719x_init_end;
41348a31030SOndrej Zary 	}
41448a31030SOndrej Zary 	dev_info(&wd->pdev->dev, "RISC initialized with firmware version %.2x.%.2x\n",
41548a31030SOndrej Zary 			wd719x_readb(wd, WD719X_AMR_SCB_OUT + 1),
41648a31030SOndrej Zary 			wd719x_readb(wd, WD719X_AMR_SCB_OUT));
41748a31030SOndrej Zary 
41848a31030SOndrej Zary 	/* RESET SCSI bus */
41948a31030SOndrej Zary 	ret = wd719x_direct_cmd(wd, WD719X_CMD_BUSRESET, 0, 0, 0, 0,
42048a31030SOndrej Zary 				WD719X_WAIT_FOR_SCSI_RESET);
42148a31030SOndrej Zary 	if (ret) {
42248a31030SOndrej Zary 		dev_warn(&wd->pdev->dev, "SCSI bus reset failed\n");
42348a31030SOndrej Zary 		goto wd719x_init_end;
42448a31030SOndrej Zary 	}
42548a31030SOndrej Zary 
42648a31030SOndrej Zary 	/* use HostParameter structure to set Spider's Host Parameter Block */
42748a31030SOndrej Zary 	ret = wd719x_direct_cmd(wd, WD719X_CMD_SET_PARAM, 0,
42848a31030SOndrej Zary 				sizeof(struct wd719x_host_param), 0,
42948a31030SOndrej Zary 				wd->params_phys, WD719X_WAIT_FOR_RISC);
43048a31030SOndrej Zary 	if (ret) {
43148a31030SOndrej Zary 		dev_warn(&wd->pdev->dev, "Failed to set HOST PARAMETERS\n");
43248a31030SOndrej Zary 		goto wd719x_init_end;
43348a31030SOndrej Zary 	}
43448a31030SOndrej Zary 
43548a31030SOndrej Zary 	/* initiate SCAM (does nothing if disabled in BIOS) */
43648a31030SOndrej Zary 	/* bug?: we should pass a mask of static IDs which we don't have */
43748a31030SOndrej Zary 	ret = wd719x_direct_cmd(wd, WD719X_CMD_INIT_SCAM, 0, 0, 0, 0,
43848a31030SOndrej Zary 				WD719X_WAIT_FOR_SCSI_RESET);
43948a31030SOndrej Zary 	if (ret) {
44048a31030SOndrej Zary 		dev_warn(&wd->pdev->dev, "SCAM initialization failed\n");
44148a31030SOndrej Zary 		goto wd719x_init_end;
44248a31030SOndrej Zary 	}
44348a31030SOndrej Zary 
44448a31030SOndrej Zary 	/* clear AMR_BIOS_SHARE_INT register */
44548a31030SOndrej Zary 	wd719x_writeb(wd, WD719X_AMR_BIOS_SHARE_INT, 0);
44648a31030SOndrej Zary 
44748a31030SOndrej Zary wd719x_init_end:
44848a31030SOndrej Zary 	release_firmware(fw_wcs);
44948a31030SOndrej Zary 	release_firmware(fw_risc);
45048a31030SOndrej Zary 
45148a31030SOndrej Zary 	return ret;
45248a31030SOndrej Zary }
45348a31030SOndrej Zary 
45448a31030SOndrej Zary static int wd719x_abort(struct scsi_cmnd *cmd)
45548a31030SOndrej Zary {
45648a31030SOndrej Zary 	int action, result;
45748a31030SOndrej Zary 	unsigned long flags;
458fde46e96SChristoph Hellwig 	struct wd719x_scb *scb = scsi_cmd_priv(cmd);
45948a31030SOndrej Zary 	struct wd719x *wd = shost_priv(cmd->device->host);
46048a31030SOndrej Zary 
46148a31030SOndrej Zary 	dev_info(&wd->pdev->dev, "abort command, tag: %x\n", cmd->tag);
46248a31030SOndrej Zary 
46348a31030SOndrej Zary 	action = /*cmd->tag ? WD719X_CMD_ABORT_TAG : */WD719X_CMD_ABORT;
46448a31030SOndrej Zary 
46548a31030SOndrej Zary 	spin_lock_irqsave(wd->sh->host_lock, flags);
46648a31030SOndrej Zary 	result = wd719x_direct_cmd(wd, action, cmd->device->id,
46748a31030SOndrej Zary 				   cmd->device->lun, cmd->tag, scb->phys, 0);
46848a31030SOndrej Zary 	spin_unlock_irqrestore(wd->sh->host_lock, flags);
46948a31030SOndrej Zary 	if (result)
47048a31030SOndrej Zary 		return FAILED;
47148a31030SOndrej Zary 
47248a31030SOndrej Zary 	return SUCCESS;
47348a31030SOndrej Zary }
47448a31030SOndrej Zary 
47548a31030SOndrej Zary static int wd719x_reset(struct scsi_cmnd *cmd, u8 opcode, u8 device)
47648a31030SOndrej Zary {
47748a31030SOndrej Zary 	int result;
47848a31030SOndrej Zary 	unsigned long flags;
47948a31030SOndrej Zary 	struct wd719x *wd = shost_priv(cmd->device->host);
48048a31030SOndrej Zary 
48148a31030SOndrej Zary 	dev_info(&wd->pdev->dev, "%s reset requested\n",
48248a31030SOndrej Zary 		 (opcode == WD719X_CMD_BUSRESET) ? "bus" : "device");
48348a31030SOndrej Zary 
48448a31030SOndrej Zary 	spin_lock_irqsave(wd->sh->host_lock, flags);
48548a31030SOndrej Zary 	result = wd719x_direct_cmd(wd, opcode, device, 0, 0, 0,
48648a31030SOndrej Zary 				   WD719X_WAIT_FOR_SCSI_RESET);
48748a31030SOndrej Zary 	spin_unlock_irqrestore(wd->sh->host_lock, flags);
48848a31030SOndrej Zary 	if (result)
48948a31030SOndrej Zary 		return FAILED;
49048a31030SOndrej Zary 
49148a31030SOndrej Zary 	return SUCCESS;
49248a31030SOndrej Zary }
49348a31030SOndrej Zary 
49448a31030SOndrej Zary static int wd719x_dev_reset(struct scsi_cmnd *cmd)
49548a31030SOndrej Zary {
49648a31030SOndrej Zary 	return wd719x_reset(cmd, WD719X_CMD_RESET, cmd->device->id);
49748a31030SOndrej Zary }
49848a31030SOndrej Zary 
49948a31030SOndrej Zary static int wd719x_bus_reset(struct scsi_cmnd *cmd)
50048a31030SOndrej Zary {
50148a31030SOndrej Zary 	return wd719x_reset(cmd, WD719X_CMD_BUSRESET, 0);
50248a31030SOndrej Zary }
50348a31030SOndrej Zary 
50448a31030SOndrej Zary static int wd719x_host_reset(struct scsi_cmnd *cmd)
50548a31030SOndrej Zary {
50648a31030SOndrej Zary 	struct wd719x *wd = shost_priv(cmd->device->host);
50748a31030SOndrej Zary 	struct wd719x_scb *scb, *tmp;
50848a31030SOndrej Zary 	unsigned long flags;
50948a31030SOndrej Zary 	int result;
51048a31030SOndrej Zary 
51148a31030SOndrej Zary 	dev_info(&wd->pdev->dev, "host reset requested\n");
51248a31030SOndrej Zary 	spin_lock_irqsave(wd->sh->host_lock, flags);
51348a31030SOndrej Zary 	/* Try to reinit the RISC */
51448a31030SOndrej Zary 	if (wd719x_chip_init(wd) == 0)
51548a31030SOndrej Zary 		result = SUCCESS;
51648a31030SOndrej Zary 	else
51748a31030SOndrej Zary 		result = FAILED;
51848a31030SOndrej Zary 
51948a31030SOndrej Zary 	/* flush all SCBs */
520fde46e96SChristoph Hellwig 	list_for_each_entry_safe(scb, tmp, &wd->active_scbs, list)
521fde46e96SChristoph Hellwig 		wd719x_finish_cmd(scb, result);
52248a31030SOndrej Zary 	spin_unlock_irqrestore(wd->sh->host_lock, flags);
52348a31030SOndrej Zary 
52448a31030SOndrej Zary 	return result;
52548a31030SOndrej Zary }
52648a31030SOndrej Zary 
52748a31030SOndrej Zary static int wd719x_biosparam(struct scsi_device *sdev, struct block_device *bdev,
52848a31030SOndrej Zary 			    sector_t capacity, int geom[])
52948a31030SOndrej Zary {
53048a31030SOndrej Zary 	if (capacity >= 0x200000) {
53148a31030SOndrej Zary 		geom[0] = 255;	/* heads */
53248a31030SOndrej Zary 		geom[1] = 63;	/* sectors */
53348a31030SOndrej Zary 	} else {
53448a31030SOndrej Zary 		geom[0] = 64;	/* heads */
53548a31030SOndrej Zary 		geom[1] = 32;	/* sectors */
53648a31030SOndrej Zary 	}
53748a31030SOndrej Zary 	geom[2] = sector_div(capacity, geom[0] * geom[1]);	/* cylinders */
53848a31030SOndrej Zary 
53948a31030SOndrej Zary 	return 0;
54048a31030SOndrej Zary }
54148a31030SOndrej Zary 
54248a31030SOndrej Zary /* process a SCB-completion interrupt */
54348a31030SOndrej Zary static inline void wd719x_interrupt_SCB(struct wd719x *wd,
54448a31030SOndrej Zary 					union wd719x_regs regs,
54548a31030SOndrej Zary 					struct wd719x_scb *scb)
54648a31030SOndrej Zary {
54748a31030SOndrej Zary 	int result;
54848a31030SOndrej Zary 
54948a31030SOndrej Zary 	/* now have to find result from card */
55048a31030SOndrej Zary 	switch (regs.bytes.SUE) {
55148a31030SOndrej Zary 	case WD719X_SUE_NOERRORS:
55248a31030SOndrej Zary 		result = DID_OK;
55348a31030SOndrej Zary 		break;
55448a31030SOndrej Zary 	case WD719X_SUE_REJECTED:
55548a31030SOndrej Zary 		dev_err(&wd->pdev->dev, "command rejected\n");
55648a31030SOndrej Zary 		result = DID_ERROR;
55748a31030SOndrej Zary 		break;
55848a31030SOndrej Zary 	case WD719X_SUE_SCBQFULL:
55948a31030SOndrej Zary 		dev_err(&wd->pdev->dev, "SCB queue is full\n");
56048a31030SOndrej Zary 		result = DID_ERROR;
56148a31030SOndrej Zary 		break;
56248a31030SOndrej Zary 	case WD719X_SUE_TERM:
56348a31030SOndrej Zary 		dev_dbg(&wd->pdev->dev, "SCB terminated by direct command\n");
56448a31030SOndrej Zary 		result = DID_ABORT;	/* or DID_RESET? */
56548a31030SOndrej Zary 		break;
56648a31030SOndrej Zary 	case WD719X_SUE_CHAN1ABORT:
56748a31030SOndrej Zary 	case WD719X_SUE_CHAN23ABORT:
56848a31030SOndrej Zary 		result = DID_ABORT;
56948a31030SOndrej Zary 		dev_err(&wd->pdev->dev, "DMA abort\n");
57048a31030SOndrej Zary 		break;
57148a31030SOndrej Zary 	case WD719X_SUE_CHAN1PAR:
57248a31030SOndrej Zary 	case WD719X_SUE_CHAN23PAR:
57348a31030SOndrej Zary 		result = DID_PARITY;
57448a31030SOndrej Zary 		dev_err(&wd->pdev->dev, "DMA parity error\n");
57548a31030SOndrej Zary 		break;
57648a31030SOndrej Zary 	case WD719X_SUE_TIMEOUT:
57748a31030SOndrej Zary 		result = DID_TIME_OUT;
57848a31030SOndrej Zary 		dev_dbg(&wd->pdev->dev, "selection timeout\n");
57948a31030SOndrej Zary 		break;
58048a31030SOndrej Zary 	case WD719X_SUE_RESET:
581804ff603SMasanari Iida 		dev_dbg(&wd->pdev->dev, "bus reset occurred\n");
58248a31030SOndrej Zary 		result = DID_RESET;
58348a31030SOndrej Zary 		break;
58448a31030SOndrej Zary 	case WD719X_SUE_BUSERROR:
58548a31030SOndrej Zary 		dev_dbg(&wd->pdev->dev, "SCSI bus error\n");
58648a31030SOndrej Zary 		result = DID_ERROR;
58748a31030SOndrej Zary 		break;
58848a31030SOndrej Zary 	case WD719X_SUE_WRONGWAY:
58948a31030SOndrej Zary 		dev_err(&wd->pdev->dev, "wrong data transfer direction\n");
59048a31030SOndrej Zary 		result = DID_ERROR;
59148a31030SOndrej Zary 		break;
59248a31030SOndrej Zary 	case WD719X_SUE_BADPHASE:
59348a31030SOndrej Zary 		dev_err(&wd->pdev->dev, "invalid SCSI phase\n");
59448a31030SOndrej Zary 		result = DID_ERROR;
59548a31030SOndrej Zary 		break;
59648a31030SOndrej Zary 	case WD719X_SUE_TOOLONG:
59748a31030SOndrej Zary 		dev_err(&wd->pdev->dev, "record too long\n");
59848a31030SOndrej Zary 		result = DID_ERROR;
59948a31030SOndrej Zary 		break;
60048a31030SOndrej Zary 	case WD719X_SUE_BUSFREE:
60148a31030SOndrej Zary 		dev_err(&wd->pdev->dev, "unexpected bus free\n");
60248a31030SOndrej Zary 		result = DID_NO_CONNECT; /* or DID_ERROR ???*/
60348a31030SOndrej Zary 		break;
60448a31030SOndrej Zary 	case WD719X_SUE_ARSDONE:
60548a31030SOndrej Zary 		dev_dbg(&wd->pdev->dev, "auto request sense\n");
60648a31030SOndrej Zary 		if (regs.bytes.SCSI == 0)
60748a31030SOndrej Zary 			result = DID_OK;
60848a31030SOndrej Zary 		else
60948a31030SOndrej Zary 			result = DID_PARITY;
61048a31030SOndrej Zary 		break;
61148a31030SOndrej Zary 	case WD719X_SUE_IGNORED:
61248a31030SOndrej Zary 		dev_err(&wd->pdev->dev, "target id %d ignored command\n",
61348a31030SOndrej Zary 			scb->cmd->device->id);
61448a31030SOndrej Zary 		result = DID_NO_CONNECT;
61548a31030SOndrej Zary 		break;
61648a31030SOndrej Zary 	case WD719X_SUE_WRONGTAGS:
61748a31030SOndrej Zary 		dev_err(&wd->pdev->dev, "reversed tags\n");
61848a31030SOndrej Zary 		result = DID_ERROR;
61948a31030SOndrej Zary 		break;
62048a31030SOndrej Zary 	case WD719X_SUE_BADTAGS:
62148a31030SOndrej Zary 		dev_err(&wd->pdev->dev, "tag type not supported by target\n");
62248a31030SOndrej Zary 		result = DID_ERROR;
62348a31030SOndrej Zary 		break;
62448a31030SOndrej Zary 	case WD719X_SUE_NOSCAMID:
62548a31030SOndrej Zary 		dev_err(&wd->pdev->dev, "no SCAM soft ID available\n");
62648a31030SOndrej Zary 		result = DID_ERROR;
62748a31030SOndrej Zary 		break;
62848a31030SOndrej Zary 	default:
62948a31030SOndrej Zary 		dev_warn(&wd->pdev->dev, "unknown SUE error code: 0x%x\n",
63048a31030SOndrej Zary 			 regs.bytes.SUE);
63148a31030SOndrej Zary 		result = DID_ERROR;
63248a31030SOndrej Zary 		break;
63348a31030SOndrej Zary 	}
63448a31030SOndrej Zary 
635fde46e96SChristoph Hellwig 	wd719x_finish_cmd(scb, result);
63648a31030SOndrej Zary }
63748a31030SOndrej Zary 
63848a31030SOndrej Zary static irqreturn_t wd719x_interrupt(int irq, void *dev_id)
63948a31030SOndrej Zary {
64048a31030SOndrej Zary 	struct wd719x *wd = dev_id;
64148a31030SOndrej Zary 	union wd719x_regs regs;
64248a31030SOndrej Zary 	unsigned long flags;
64348a31030SOndrej Zary 	u32 SCB_out;
64448a31030SOndrej Zary 
64548a31030SOndrej Zary 	spin_lock_irqsave(wd->sh->host_lock, flags);
64648a31030SOndrej Zary 	/* read SCB pointer back from card */
64748a31030SOndrej Zary 	SCB_out = wd719x_readl(wd, WD719X_AMR_SCB_OUT);
64848a31030SOndrej Zary 	/* read all status info at once */
64948a31030SOndrej Zary 	regs.all = cpu_to_le32(wd719x_readl(wd, WD719X_AMR_OP_CODE));
65048a31030SOndrej Zary 
65148a31030SOndrej Zary 	switch (regs.bytes.INT) {
65248a31030SOndrej Zary 	case WD719X_INT_NONE:
65348a31030SOndrej Zary 		spin_unlock_irqrestore(wd->sh->host_lock, flags);
65448a31030SOndrej Zary 		return IRQ_NONE;
65548a31030SOndrej Zary 	case WD719X_INT_LINKNOSTATUS:
65648a31030SOndrej Zary 		dev_err(&wd->pdev->dev, "linked command completed with no status\n");
65748a31030SOndrej Zary 		break;
65848a31030SOndrej Zary 	case WD719X_INT_BADINT:
65948a31030SOndrej Zary 		dev_err(&wd->pdev->dev, "unsolicited interrupt\n");
66048a31030SOndrej Zary 		break;
66148a31030SOndrej Zary 	case WD719X_INT_NOERRORS:
66248a31030SOndrej Zary 	case WD719X_INT_LINKNOERRORS:
66348a31030SOndrej Zary 	case WD719X_INT_ERRORSLOGGED:
66448a31030SOndrej Zary 	case WD719X_INT_SPIDERFAILED:
66548a31030SOndrej Zary 		/* was the cmd completed a direct or SCB command? */
66648a31030SOndrej Zary 		if (regs.bytes.OPC == WD719X_CMD_PROCESS_SCB) {
66748a31030SOndrej Zary 			struct wd719x_scb *scb;
66848a31030SOndrej Zary 			list_for_each_entry(scb, &wd->active_scbs, list)
66948a31030SOndrej Zary 				if (SCB_out == scb->phys)
67048a31030SOndrej Zary 					break;
67148a31030SOndrej Zary 			if (SCB_out == scb->phys)
67248a31030SOndrej Zary 				wd719x_interrupt_SCB(wd, regs, scb);
67348a31030SOndrej Zary 			else
67448a31030SOndrej Zary 				dev_err(&wd->pdev->dev, "card returned invalid SCB pointer\n");
67548a31030SOndrej Zary 		} else
67648a31030SOndrej Zary 			dev_warn(&wd->pdev->dev, "direct command 0x%x completed\n",
67748a31030SOndrej Zary 				 regs.bytes.OPC);
67848a31030SOndrej Zary 		break;
67948a31030SOndrej Zary 	case WD719X_INT_PIOREADY:
68048a31030SOndrej Zary 		dev_err(&wd->pdev->dev, "card indicates PIO data ready but we never use PIO\n");
68148a31030SOndrej Zary 		/* interrupt will not be cleared until all data is read */
68248a31030SOndrej Zary 		break;
68348a31030SOndrej Zary 	default:
68448a31030SOndrej Zary 		dev_err(&wd->pdev->dev, "unknown interrupt reason: %d\n",
68548a31030SOndrej Zary 			regs.bytes.INT);
68648a31030SOndrej Zary 
68748a31030SOndrej Zary 	}
68848a31030SOndrej Zary 	/* clear interrupt so another can happen */
68948a31030SOndrej Zary 	wd719x_writeb(wd, WD719X_AMR_INT_STATUS, WD719X_INT_NONE);
69048a31030SOndrej Zary 	spin_unlock_irqrestore(wd->sh->host_lock, flags);
69148a31030SOndrej Zary 
69248a31030SOndrej Zary 	return IRQ_HANDLED;
69348a31030SOndrej Zary }
69448a31030SOndrej Zary 
69548a31030SOndrej Zary static void wd719x_eeprom_reg_read(struct eeprom_93cx6 *eeprom)
69648a31030SOndrej Zary {
69748a31030SOndrej Zary 	struct wd719x *wd = eeprom->data;
69848a31030SOndrej Zary 	u8 reg = wd719x_readb(wd, WD719X_PCI_GPIO_DATA);
69948a31030SOndrej Zary 
70048a31030SOndrej Zary 	eeprom->reg_data_out = reg & WD719X_EE_DO;
70148a31030SOndrej Zary }
70248a31030SOndrej Zary 
70348a31030SOndrej Zary static void wd719x_eeprom_reg_write(struct eeprom_93cx6 *eeprom)
70448a31030SOndrej Zary {
70548a31030SOndrej Zary 	struct wd719x *wd = eeprom->data;
70648a31030SOndrej Zary 	u8 reg = 0;
70748a31030SOndrej Zary 
70848a31030SOndrej Zary 	if (eeprom->reg_data_in)
70948a31030SOndrej Zary 		reg |= WD719X_EE_DI;
71048a31030SOndrej Zary 	if (eeprom->reg_data_clock)
71148a31030SOndrej Zary 		reg |= WD719X_EE_CLK;
71248a31030SOndrej Zary 	if (eeprom->reg_chip_select)
71348a31030SOndrej Zary 		reg |= WD719X_EE_CS;
71448a31030SOndrej Zary 
71548a31030SOndrej Zary 	wd719x_writeb(wd, WD719X_PCI_GPIO_DATA, reg);
71648a31030SOndrej Zary }
71748a31030SOndrej Zary 
71848a31030SOndrej Zary /* read config from EEPROM so it can be downloaded by the RISC on (re-)init */
71948a31030SOndrej Zary static void wd719x_read_eeprom(struct wd719x *wd)
72048a31030SOndrej Zary {
72148a31030SOndrej Zary 	struct eeprom_93cx6 eeprom;
72248a31030SOndrej Zary 	u8 gpio;
72348a31030SOndrej Zary 	struct wd719x_eeprom_header header;
72448a31030SOndrej Zary 
72548a31030SOndrej Zary 	eeprom.data = wd;
72648a31030SOndrej Zary 	eeprom.register_read = wd719x_eeprom_reg_read;
72748a31030SOndrej Zary 	eeprom.register_write = wd719x_eeprom_reg_write;
72848a31030SOndrej Zary 	eeprom.width = PCI_EEPROM_WIDTH_93C46;
72948a31030SOndrej Zary 
73048a31030SOndrej Zary 	/* set all outputs to low */
73148a31030SOndrej Zary 	wd719x_writeb(wd, WD719X_PCI_GPIO_DATA, 0);
73248a31030SOndrej Zary 	/* configure GPIO pins */
73348a31030SOndrej Zary 	gpio = wd719x_readb(wd, WD719X_PCI_GPIO_CONTROL);
73448a31030SOndrej Zary 	/* GPIO outputs */
73548a31030SOndrej Zary 	gpio &= (~(WD719X_EE_CLK | WD719X_EE_DI | WD719X_EE_CS));
73648a31030SOndrej Zary 	/* GPIO input */
73748a31030SOndrej Zary 	gpio |= WD719X_EE_DO;
73848a31030SOndrej Zary 	wd719x_writeb(wd, WD719X_PCI_GPIO_CONTROL, gpio);
73948a31030SOndrej Zary 
74048a31030SOndrej Zary 	/* read EEPROM header */
74148a31030SOndrej Zary 	eeprom_93cx6_multireadb(&eeprom, 0, (u8 *)&header, sizeof(header));
74248a31030SOndrej Zary 
74348a31030SOndrej Zary 	if (header.sig1 == 'W' && header.sig2 == 'D')
74448a31030SOndrej Zary 		eeprom_93cx6_multireadb(&eeprom, header.cfg_offset,
74548a31030SOndrej Zary 					(u8 *)wd->params,
74648a31030SOndrej Zary 					sizeof(struct wd719x_host_param));
74748a31030SOndrej Zary 	else { /* default EEPROM values */
74848a31030SOndrej Zary 		dev_warn(&wd->pdev->dev, "EEPROM signature is invalid (0x%02x 0x%02x), using default values\n",
74948a31030SOndrej Zary 			 header.sig1, header.sig2);
75048a31030SOndrej Zary 		wd->params->ch_1_th	= 0x10;	/* 16 DWs = 64 B */
75148a31030SOndrej Zary 		wd->params->scsi_conf	= 0x4c;	/* 48ma, spue, parity check */
75248a31030SOndrej Zary 		wd->params->own_scsi_id	= 0x07;	/* ID 7, SCAM disabled */
75348a31030SOndrej Zary 		wd->params->sel_timeout = 0x4d;	/* 250 ms */
75448a31030SOndrej Zary 		wd->params->sleep_timer	= 0x01;
75548a31030SOndrej Zary 		wd->params->cdb_size	= cpu_to_le16(0x5555);	/* all 6 B */
75648a31030SOndrej Zary 		wd->params->scsi_pad	= 0x1b;
75748a31030SOndrej Zary 		if (wd->type == WD719X_TYPE_7193) /* narrow card - disable */
75848a31030SOndrej Zary 			wd->params->wide = cpu_to_le32(0x00000000);
75948a31030SOndrej Zary 		else	/* initiate & respond to WIDE messages */
76048a31030SOndrej Zary 			wd->params->wide = cpu_to_le32(0xffffffff);
76148a31030SOndrej Zary 		wd->params->sync	= cpu_to_le32(0xffffffff);
76248a31030SOndrej Zary 		wd->params->soft_mask	= 0x00;	/* all disabled */
76348a31030SOndrej Zary 		wd->params->unsol_mask	= 0x00;	/* all disabled */
76448a31030SOndrej Zary 	}
76548a31030SOndrej Zary 	/* disable TAGGED messages */
76648a31030SOndrej Zary 	wd->params->tag_en = cpu_to_le16(0x0000);
76748a31030SOndrej Zary }
76848a31030SOndrej Zary 
76948a31030SOndrej Zary /* Read card type from GPIO bits 1 and 3 */
77048a31030SOndrej Zary static enum wd719x_card_type wd719x_detect_type(struct wd719x *wd)
77148a31030SOndrej Zary {
77248a31030SOndrej Zary 	u8 card = wd719x_readb(wd, WD719X_PCI_GPIO_CONTROL);
77348a31030SOndrej Zary 
77448a31030SOndrej Zary 	card |= WD719X_GPIO_ID_BITS;
77548a31030SOndrej Zary 	wd719x_writeb(wd, WD719X_PCI_GPIO_CONTROL, card);
77648a31030SOndrej Zary 	card = wd719x_readb(wd, WD719X_PCI_GPIO_DATA) & WD719X_GPIO_ID_BITS;
77748a31030SOndrej Zary 	switch (card) {
77848a31030SOndrej Zary 	case 0x08:
77948a31030SOndrej Zary 		return WD719X_TYPE_7193;
78048a31030SOndrej Zary 	case 0x02:
78148a31030SOndrej Zary 		return WD719X_TYPE_7197;
78248a31030SOndrej Zary 	case 0x00:
78348a31030SOndrej Zary 		return WD719X_TYPE_7296;
78448a31030SOndrej Zary 	default:
78548a31030SOndrej Zary 		dev_warn(&wd->pdev->dev, "unknown card type 0x%x\n", card);
78648a31030SOndrej Zary 		return WD719X_TYPE_UNKNOWN;
78748a31030SOndrej Zary 	}
78848a31030SOndrej Zary }
78948a31030SOndrej Zary 
79048a31030SOndrej Zary static int wd719x_board_found(struct Scsi_Host *sh)
79148a31030SOndrej Zary {
79248a31030SOndrej Zary 	struct wd719x *wd = shost_priv(sh);
793d828e5c6SColin Ian King 	static const char * const card_types[] = {
794d828e5c6SColin Ian King 		"Unknown card", "WD7193", "WD7197", "WD7296"
795d828e5c6SColin Ian King 	};
79648a31030SOndrej Zary 	int ret;
79748a31030SOndrej Zary 
79848a31030SOndrej Zary 	INIT_LIST_HEAD(&wd->active_scbs);
79948a31030SOndrej Zary 
80048a31030SOndrej Zary 	sh->base = pci_resource_start(wd->pdev, 0);
80148a31030SOndrej Zary 
80248a31030SOndrej Zary 	wd->type = wd719x_detect_type(wd);
80348a31030SOndrej Zary 
80448a31030SOndrej Zary 	wd->sh = sh;
80548a31030SOndrej Zary 	sh->irq = wd->pdev->irq;
80648a31030SOndrej Zary 	wd->fw_virt = NULL;
80748a31030SOndrej Zary 
80848a31030SOndrej Zary 	/* memory area for host (EEPROM) parameters */
809236bd823SChristoph Hellwig 	wd->params = dma_alloc_coherent(&wd->pdev->dev,
81048a31030SOndrej Zary 					sizeof(struct wd719x_host_param),
811236bd823SChristoph Hellwig 					&wd->params_phys, GFP_KERNEL);
81248a31030SOndrej Zary 	if (!wd->params) {
81348a31030SOndrej Zary 		dev_warn(&wd->pdev->dev, "unable to allocate parameter buffer\n");
81448a31030SOndrej Zary 		return -ENOMEM;
81548a31030SOndrej Zary 	}
81648a31030SOndrej Zary 
81748a31030SOndrej Zary 	/* memory area for the RISC for hash table of outstanding requests */
818236bd823SChristoph Hellwig 	wd->hash_virt = dma_alloc_coherent(&wd->pdev->dev,
819236bd823SChristoph Hellwig 					   WD719X_HASH_TABLE_SIZE,
820236bd823SChristoph Hellwig 					   &wd->hash_phys, GFP_KERNEL);
82148a31030SOndrej Zary 	if (!wd->hash_virt) {
82248a31030SOndrej Zary 		dev_warn(&wd->pdev->dev, "unable to allocate hash buffer\n");
82348a31030SOndrej Zary 		ret = -ENOMEM;
82448a31030SOndrej Zary 		goto fail_free_params;
82548a31030SOndrej Zary 	}
82648a31030SOndrej Zary 
82748a31030SOndrej Zary 	ret = request_irq(wd->pdev->irq, wd719x_interrupt, IRQF_SHARED,
82848a31030SOndrej Zary 			  "wd719x", wd);
82948a31030SOndrej Zary 	if (ret) {
83048a31030SOndrej Zary 		dev_warn(&wd->pdev->dev, "unable to assign IRQ %d\n",
83148a31030SOndrej Zary 			 wd->pdev->irq);
83248a31030SOndrej Zary 		goto fail_free_hash;
83348a31030SOndrej Zary 	}
83448a31030SOndrej Zary 
83548a31030SOndrej Zary 	/* read parameters from EEPROM */
83648a31030SOndrej Zary 	wd719x_read_eeprom(wd);
83748a31030SOndrej Zary 
83848a31030SOndrej Zary 	ret = wd719x_chip_init(wd);
83948a31030SOndrej Zary 	if (ret)
84048a31030SOndrej Zary 		goto fail_free_irq;
84148a31030SOndrej Zary 
84248a31030SOndrej Zary 	sh->this_id = wd->params->own_scsi_id & WD719X_EE_SCSI_ID_MASK;
84348a31030SOndrej Zary 
84448a31030SOndrej Zary 	dev_info(&wd->pdev->dev, "%s at I/O 0x%lx, IRQ %u, SCSI ID %d\n",
84548a31030SOndrej Zary 		 card_types[wd->type], sh->base, sh->irq, sh->this_id);
84648a31030SOndrej Zary 
84748a31030SOndrej Zary 	return 0;
84848a31030SOndrej Zary 
84948a31030SOndrej Zary fail_free_irq:
85048a31030SOndrej Zary 	free_irq(wd->pdev->irq, wd);
85148a31030SOndrej Zary fail_free_hash:
852236bd823SChristoph Hellwig 	dma_free_coherent(&wd->pdev->dev, WD719X_HASH_TABLE_SIZE, wd->hash_virt,
85348a31030SOndrej Zary 			    wd->hash_phys);
85448a31030SOndrej Zary fail_free_params:
855236bd823SChristoph Hellwig 	dma_free_coherent(&wd->pdev->dev, sizeof(struct wd719x_host_param),
85648a31030SOndrej Zary 			    wd->params, wd->params_phys);
85748a31030SOndrej Zary 
85848a31030SOndrej Zary 	return ret;
85948a31030SOndrej Zary }
86048a31030SOndrej Zary 
86148a31030SOndrej Zary static struct scsi_host_template wd719x_template = {
8622ecf8e0aSOndrej Zary 	.module				= THIS_MODULE,
86348a31030SOndrej Zary 	.name				= "Western Digital 719x",
864fde46e96SChristoph Hellwig 	.cmd_size			= sizeof(struct wd719x_scb),
86548a31030SOndrej Zary 	.queuecommand			= wd719x_queuecommand,
86648a31030SOndrej Zary 	.eh_abort_handler		= wd719x_abort,
86748a31030SOndrej Zary 	.eh_device_reset_handler	= wd719x_dev_reset,
86848a31030SOndrej Zary 	.eh_bus_reset_handler		= wd719x_bus_reset,
86948a31030SOndrej Zary 	.eh_host_reset_handler		= wd719x_host_reset,
87048a31030SOndrej Zary 	.bios_param			= wd719x_biosparam,
87148a31030SOndrej Zary 	.proc_name			= "wd719x",
87248a31030SOndrej Zary 	.can_queue			= 255,
87348a31030SOndrej Zary 	.this_id			= 7,
87448a31030SOndrej Zary 	.sg_tablesize			= WD719X_SG,
87548a31030SOndrej Zary };
87648a31030SOndrej Zary 
87748a31030SOndrej Zary static int wd719x_pci_probe(struct pci_dev *pdev, const struct pci_device_id *d)
87848a31030SOndrej Zary {
87948a31030SOndrej Zary 	int err;
88048a31030SOndrej Zary 	struct Scsi_Host *sh;
88148a31030SOndrej Zary 	struct wd719x *wd;
88248a31030SOndrej Zary 
88348a31030SOndrej Zary 	err = pci_enable_device(pdev);
88448a31030SOndrej Zary 	if (err)
88548a31030SOndrej Zary 		goto fail;
88648a31030SOndrej Zary 
887236bd823SChristoph Hellwig 	if (dma_set_mask(&pdev->dev, DMA_BIT_MASK(32))) {
88848a31030SOndrej Zary 		dev_warn(&pdev->dev, "Unable to set 32-bit DMA mask\n");
88948a31030SOndrej Zary 		goto disable_device;
89048a31030SOndrej Zary 	}
89148a31030SOndrej Zary 
89248a31030SOndrej Zary 	err = pci_request_regions(pdev, "wd719x");
89348a31030SOndrej Zary 	if (err)
89448a31030SOndrej Zary 		goto disable_device;
89548a31030SOndrej Zary 	pci_set_master(pdev);
89648a31030SOndrej Zary 
89748a31030SOndrej Zary 	err = -ENODEV;
89848a31030SOndrej Zary 	if (pci_resource_len(pdev, 0) == 0)
89948a31030SOndrej Zary 		goto release_region;
90048a31030SOndrej Zary 
90148a31030SOndrej Zary 	err = -ENOMEM;
90248a31030SOndrej Zary 	sh = scsi_host_alloc(&wd719x_template, sizeof(struct wd719x));
90348a31030SOndrej Zary 	if (!sh)
90448a31030SOndrej Zary 		goto release_region;
90548a31030SOndrej Zary 
90648a31030SOndrej Zary 	wd = shost_priv(sh);
90748a31030SOndrej Zary 	wd->base = pci_iomap(pdev, 0, 0);
90848a31030SOndrej Zary 	if (!wd->base)
90948a31030SOndrej Zary 		goto free_host;
91048a31030SOndrej Zary 	wd->pdev = pdev;
91148a31030SOndrej Zary 
91248a31030SOndrej Zary 	err = wd719x_board_found(sh);
91348a31030SOndrej Zary 	if (err)
91448a31030SOndrej Zary 		goto unmap;
91548a31030SOndrej Zary 
91648a31030SOndrej Zary 	err = scsi_add_host(sh, &wd->pdev->dev);
91748a31030SOndrej Zary 	if (err)
91848a31030SOndrej Zary 		goto destroy;
91948a31030SOndrej Zary 
92048a31030SOndrej Zary 	scsi_scan_host(sh);
92148a31030SOndrej Zary 
92248a31030SOndrej Zary 	pci_set_drvdata(pdev, sh);
92348a31030SOndrej Zary 	return 0;
92448a31030SOndrej Zary 
92548a31030SOndrej Zary destroy:
92648a31030SOndrej Zary 	wd719x_destroy(wd);
92748a31030SOndrej Zary unmap:
92848a31030SOndrej Zary 	pci_iounmap(pdev, wd->base);
92948a31030SOndrej Zary free_host:
93048a31030SOndrej Zary 	scsi_host_put(sh);
93148a31030SOndrej Zary release_region:
93248a31030SOndrej Zary 	pci_release_regions(pdev);
93348a31030SOndrej Zary disable_device:
93448a31030SOndrej Zary 	pci_disable_device(pdev);
93548a31030SOndrej Zary fail:
93648a31030SOndrej Zary 	return err;
93748a31030SOndrej Zary }
93848a31030SOndrej Zary 
93948a31030SOndrej Zary 
94048a31030SOndrej Zary static void wd719x_pci_remove(struct pci_dev *pdev)
94148a31030SOndrej Zary {
94248a31030SOndrej Zary 	struct Scsi_Host *sh = pci_get_drvdata(pdev);
94348a31030SOndrej Zary 	struct wd719x *wd = shost_priv(sh);
94448a31030SOndrej Zary 
94548a31030SOndrej Zary 	scsi_remove_host(sh);
94648a31030SOndrej Zary 	wd719x_destroy(wd);
94748a31030SOndrej Zary 	pci_iounmap(pdev, wd->base);
94848a31030SOndrej Zary 	pci_release_regions(pdev);
94948a31030SOndrej Zary 	pci_disable_device(pdev);
95048a31030SOndrej Zary 
95148a31030SOndrej Zary 	scsi_host_put(sh);
95248a31030SOndrej Zary }
95348a31030SOndrej Zary 
9548a793beaSJoe Perches static const struct pci_device_id wd719x_pci_table[] = {
95548a31030SOndrej Zary 	{ PCI_DEVICE(PCI_VENDOR_ID_WD, 0x3296) },
95648a31030SOndrej Zary 	{}
95748a31030SOndrej Zary };
95848a31030SOndrej Zary 
95948a31030SOndrej Zary MODULE_DEVICE_TABLE(pci, wd719x_pci_table);
96048a31030SOndrej Zary 
96148a31030SOndrej Zary static struct pci_driver wd719x_pci_driver = {
96248a31030SOndrej Zary 	.name =		"wd719x",
96348a31030SOndrej Zary 	.id_table =	wd719x_pci_table,
96448a31030SOndrej Zary 	.probe =	wd719x_pci_probe,
96548a31030SOndrej Zary 	.remove =	wd719x_pci_remove,
96648a31030SOndrej Zary };
96748a31030SOndrej Zary 
9683e1bbc56SYueHaibing module_pci_driver(wd719x_pci_driver);
96948a31030SOndrej Zary 
97048a31030SOndrej Zary MODULE_DESCRIPTION("Western Digital WD7193/7197/7296 SCSI driver");
97148a31030SOndrej Zary MODULE_AUTHOR("Ondrej Zary, Aaron Dewell, Juergen Gaertner");
97248a31030SOndrej Zary MODULE_LICENSE("GPL");
97348a31030SOndrej Zary MODULE_FIRMWARE("wd719x-wcs.bin");
97448a31030SOndrej Zary MODULE_FIRMWARE("wd719x-risc.bin");
975