2 * Driver for Western Digital WD7193, WD7197 and WD7296 SCSI cards
3 * Copyright 2013 Ondrej Zary
6 * Aaron Dewell <dewell@woods.net>
7 * Gaerti <Juergen.Gaertner@mbox.si.uni-hannover.de>
9 * HW documentation available in book:
11 * SPIDER Command Protocol
13 * SCSI Storage Products (MCP)
14 * Western Digital Corporation
17 * http://web.archive.org/web/20070717175254/http://sun1.rrzn.uni-hannover.de/gaertner.juergen/wd719x/Linux/Docu/Spider/
22 * 1. SCSI command is transformed to SCB (Spider Control Block) by the
23 * queuecommand function.
24 * 2. The address of the SCB is stored in a list to be able to access it, if
25 * something goes wrong.
26 * 3. The address of the SCB is written to the Controller, which loads the SCB
27 * via BM-DMA and processes it.
28 * 4. After it has finished, it generates an interrupt, and sets registers.
31 * - abort/reset functions
37 #include <linux/interrupt.h>
38 #include <linux/module.h>
39 #include <linux/delay.h>
40 #include <linux/pci.h>
41 #include <linux/firmware.h>
42 #include <linux/eeprom_93cx6.h>
43 #include <scsi/scsi_cmnd.h>
44 #include <scsi/scsi_device.h>
45 #include <scsi/scsi_host.h>
48 /* low-level register access */
49 static inline u8
wd719x_readb(struct wd719x
*wd
, u8 reg
)
51 return ioread8(wd
->base
+ reg
);
54 static inline u32
wd719x_readl(struct wd719x
*wd
, u8 reg
)
56 return ioread32(wd
->base
+ reg
);
59 static inline void wd719x_writeb(struct wd719x
*wd
, u8 reg
, u8 val
)
61 iowrite8(val
, wd
->base
+ reg
);
64 static inline void wd719x_writew(struct wd719x
*wd
, u8 reg
, u16 val
)
66 iowrite16(val
, wd
->base
+ reg
);
69 static inline void wd719x_writel(struct wd719x
*wd
, u8 reg
, u32 val
)
71 iowrite32(val
, wd
->base
+ reg
);
74 /* wait until the command register is ready */
75 static inline int wd719x_wait_ready(struct wd719x
*wd
)
80 if (wd719x_readb(wd
, WD719X_AMR_COMMAND
) == WD719X_CMD_READY
)
83 } while (i
++ < WD719X_WAIT_FOR_CMD_READY
);
85 dev_err(&wd
->pdev
->dev
, "command register is not ready: 0x%02x\n",
86 wd719x_readb(wd
, WD719X_AMR_COMMAND
));
91 /* poll interrupt status register until command finishes */
92 static inline int wd719x_wait_done(struct wd719x
*wd
, int timeout
)
97 status
= wd719x_readb(wd
, WD719X_AMR_INT_STATUS
);
105 dev_err(&wd
->pdev
->dev
, "direct command timed out\n");
109 if (status
!= WD719X_INT_NOERRORS
) {
110 dev_err(&wd
->pdev
->dev
, "direct command failed, status 0x%02x, SUE 0x%02x\n",
111 status
, wd719x_readb(wd
, WD719X_AMR_SCB_ERROR
));
118 static int wd719x_direct_cmd(struct wd719x
*wd
, u8 opcode
, u8 dev
, u8 lun
,
119 u8 tag
, dma_addr_t data
, int timeout
)
123 /* clear interrupt status register (allow command register to clear) */
124 wd719x_writeb(wd
, WD719X_AMR_INT_STATUS
, WD719X_INT_NONE
);
126 /* Wait for the Command register to become free */
127 if (wd719x_wait_ready(wd
))
130 /* make sure we get NO interrupts */
131 dev
|= WD719X_DISABLE_INT
;
132 wd719x_writeb(wd
, WD719X_AMR_CMD_PARAM
, dev
);
133 wd719x_writeb(wd
, WD719X_AMR_CMD_PARAM_2
, lun
);
134 wd719x_writeb(wd
, WD719X_AMR_CMD_PARAM_3
, tag
);
136 wd719x_writel(wd
, WD719X_AMR_SCB_IN
, data
);
138 /* clear interrupt status register again */
139 wd719x_writeb(wd
, WD719X_AMR_INT_STATUS
, WD719X_INT_NONE
);
141 /* Now, write the command */
142 wd719x_writeb(wd
, WD719X_AMR_COMMAND
, opcode
);
144 if (timeout
) /* wait for the command to complete */
145 ret
= wd719x_wait_done(wd
, timeout
);
147 /* clear interrupt status register (clean up) */
148 if (opcode
!= WD719X_CMD_READ_FIRMVER
)
149 wd719x_writeb(wd
, WD719X_AMR_INT_STATUS
, WD719X_INT_NONE
);
154 static void wd719x_destroy(struct wd719x
*wd
)
157 if (wd719x_direct_cmd(wd
, WD719X_CMD_SLEEP
, 0, 0, 0, 0,
158 WD719X_WAIT_FOR_RISC
))
159 dev_warn(&wd
->pdev
->dev
, "RISC sleep command failed\n");
161 wd719x_writeb(wd
, WD719X_PCI_MODE_SELECT
, 0);
163 WARN_ON_ONCE(!list_empty(&wd
->active_scbs
));
165 /* free internal buffers */
166 dma_free_coherent(&wd
->pdev
->dev
, wd
->fw_size
, wd
->fw_virt
,
169 dma_free_coherent(&wd
->pdev
->dev
, WD719X_HASH_TABLE_SIZE
, wd
->hash_virt
,
171 wd
->hash_virt
= NULL
;
172 dma_free_coherent(&wd
->pdev
->dev
, sizeof(struct wd719x_host_param
),
173 wd
->params
, wd
->params_phys
);
175 free_irq(wd
->pdev
->irq
, wd
);
178 /* finish a SCSI command, unmap buffers */
179 static void wd719x_finish_cmd(struct wd719x_scb
*scb
, int result
)
181 struct scsi_cmnd
*cmd
= scb
->cmd
;
182 struct wd719x
*wd
= shost_priv(cmd
->device
->host
);
184 list_del(&scb
->list
);
186 dma_unmap_single(&wd
->pdev
->dev
, scb
->phys
,
187 sizeof(struct wd719x_scb
), DMA_BIDIRECTIONAL
);
189 dma_unmap_single(&wd
->pdev
->dev
, cmd
->SCp
.dma_handle
,
190 SCSI_SENSE_BUFFERSIZE
, DMA_FROM_DEVICE
);
192 cmd
->result
= result
<< 16;
196 /* Build a SCB and send it to the card */
197 static int wd719x_queuecommand(struct Scsi_Host
*sh
, struct scsi_cmnd
*cmd
)
201 struct wd719x_scb
*scb
= scsi_cmd_priv(cmd
);
202 struct wd719x
*wd
= shost_priv(sh
);
206 scb
->CDB_tag
= 0; /* Tagged queueing not supported yet */
207 scb
->devid
= cmd
->device
->id
;
208 scb
->lun
= cmd
->device
->lun
;
210 /* copy the command */
211 memcpy(scb
->CDB
, cmd
->cmnd
, cmd
->cmd_len
);
214 scb
->phys
= dma_map_single(&wd
->pdev
->dev
, scb
, sizeof(*scb
),
217 if (dma_mapping_error(&wd
->pdev
->dev
, scb
->phys
))
220 /* map sense buffer */
221 scb
->sense_buf_length
= SCSI_SENSE_BUFFERSIZE
;
222 cmd
->SCp
.dma_handle
= dma_map_single(&wd
->pdev
->dev
, cmd
->sense_buffer
,
223 SCSI_SENSE_BUFFERSIZE
, DMA_FROM_DEVICE
);
224 if (dma_mapping_error(&wd
->pdev
->dev
, cmd
->SCp
.dma_handle
))
226 scb
->sense_buf
= cpu_to_le32(cmd
->SCp
.dma_handle
);
228 /* request autosense */
229 scb
->SCB_options
|= WD719X_SCB_FLAGS_AUTO_REQUEST_SENSE
;
231 /* check direction */
232 if (cmd
->sc_data_direction
== DMA_TO_DEVICE
)
233 scb
->SCB_options
|= WD719X_SCB_FLAGS_CHECK_DIRECTION
234 | WD719X_SCB_FLAGS_PCI_TO_SCSI
;
235 else if (cmd
->sc_data_direction
== DMA_FROM_DEVICE
)
236 scb
->SCB_options
|= WD719X_SCB_FLAGS_CHECK_DIRECTION
;
239 count_sg
= scsi_dma_map(cmd
);
241 goto out_unmap_sense
;
242 BUG_ON(count_sg
> WD719X_SG
);
245 struct scatterlist
*sg
;
247 scb
->data_length
= cpu_to_le32(count_sg
*
248 sizeof(struct wd719x_sglist
));
249 scb
->data_p
= cpu_to_le32(scb
->phys
+
250 offsetof(struct wd719x_scb
, sg_list
));
252 scsi_for_each_sg(cmd
, sg
, count_sg
, i
) {
253 scb
->sg_list
[i
].ptr
= cpu_to_le32(sg_dma_address(sg
));
254 scb
->sg_list
[i
].length
= cpu_to_le32(sg_dma_len(sg
));
256 scb
->SCB_options
|= WD719X_SCB_FLAGS_DO_SCATTER_GATHER
;
257 } else { /* zero length */
258 scb
->data_length
= 0;
262 spin_lock_irqsave(wd
->sh
->host_lock
, flags
);
264 /* check if the Command register is free */
265 if (wd719x_readb(wd
, WD719X_AMR_COMMAND
) != WD719X_CMD_READY
) {
266 spin_unlock_irqrestore(wd
->sh
->host_lock
, flags
);
267 return SCSI_MLQUEUE_HOST_BUSY
;
270 list_add(&scb
->list
, &wd
->active_scbs
);
272 /* write pointer to the AMR */
273 wd719x_writel(wd
, WD719X_AMR_SCB_IN
, scb
->phys
);
274 /* send SCB opcode */
275 wd719x_writeb(wd
, WD719X_AMR_COMMAND
, WD719X_CMD_PROCESS_SCB
);
277 spin_unlock_irqrestore(wd
->sh
->host_lock
, flags
);
281 dma_unmap_single(&wd
->pdev
->dev
, cmd
->SCp
.dma_handle
,
282 SCSI_SENSE_BUFFERSIZE
, DMA_FROM_DEVICE
);
284 dma_unmap_single(&wd
->pdev
->dev
, scb
->phys
, sizeof(*scb
),
287 cmd
->result
= DID_ERROR
<< 16;
292 static int wd719x_chip_init(struct wd719x
*wd
)
296 const struct firmware
*fw_wcs
, *fw_risc
;
297 const char fwname_wcs
[] = "wd719x-wcs.bin";
298 const char fwname_risc
[] = "wd719x-risc.bin";
300 memset(wd
->hash_virt
, 0, WD719X_HASH_TABLE_SIZE
);
302 /* WCS (sequencer) firmware */
303 ret
= request_firmware(&fw_wcs
, fwname_wcs
, &wd
->pdev
->dev
);
305 dev_err(&wd
->pdev
->dev
, "Unable to load firmware %s: %d\n",
310 ret
= request_firmware(&fw_risc
, fwname_risc
, &wd
->pdev
->dev
);
312 dev_err(&wd
->pdev
->dev
, "Unable to load firmware %s: %d\n",
314 release_firmware(fw_wcs
);
317 wd
->fw_size
= ALIGN(fw_wcs
->size
, 4) + fw_risc
->size
;
320 wd
->fw_virt
= dma_alloc_coherent(&wd
->pdev
->dev
, wd
->fw_size
,
321 &wd
->fw_phys
, GFP_KERNEL
);
324 goto wd719x_init_end
;
327 /* make a fresh copy of WCS and RISC code */
328 memcpy(wd
->fw_virt
, fw_wcs
->data
, fw_wcs
->size
);
329 memcpy(wd
->fw_virt
+ ALIGN(fw_wcs
->size
, 4), fw_risc
->data
,
332 /* Reset the Spider Chip and adapter itself */
333 wd719x_writeb(wd
, WD719X_PCI_PORT_RESET
, WD719X_PCI_RESET
);
334 udelay(WD719X_WAIT_FOR_RISC
);
335 /* Clear PIO mode bits set by BIOS */
336 wd719x_writeb(wd
, WD719X_AMR_CMD_PARAM
, 0);
337 /* ensure RISC is not running */
338 wd719x_writeb(wd
, WD719X_PCI_MODE_SELECT
, 0);
339 /* ensure command port is ready */
340 wd719x_writeb(wd
, WD719X_AMR_COMMAND
, 0);
341 if (wd719x_wait_ready(wd
)) {
343 goto wd719x_init_end
;
346 /* Transfer the first 2K words of RISC code to kick start the uP */
347 risc_init
[0] = wd
->fw_phys
; /* WCS FW */
348 risc_init
[1] = wd
->fw_phys
+ ALIGN(fw_wcs
->size
, 4); /* RISC FW */
349 risc_init
[2] = wd
->hash_phys
; /* hash table */
351 /* clear DMA status */
352 wd719x_writeb(wd
, WD719X_PCI_CHANNEL2_3STATUS
, 0);
354 /* address to read firmware from */
355 wd719x_writel(wd
, WD719X_PCI_EXTERNAL_ADDR
, risc_init
[1]);
356 /* base address to write firmware to (on card) */
357 wd719x_writew(wd
, WD719X_PCI_INTERNAL_ADDR
, WD719X_PRAM_BASE_ADDR
);
358 /* size: first 2K words */
359 wd719x_writew(wd
, WD719X_PCI_DMA_TRANSFER_SIZE
, 2048 * 2);
361 wd719x_writeb(wd
, WD719X_PCI_CHANNEL2_3CMD
, WD719X_START_CHANNEL2_3DMA
);
363 /* wait for DMA to complete */
364 i
= WD719X_WAIT_FOR_RISC
;
366 u8 status
= wd719x_readb(wd
, WD719X_PCI_CHANNEL2_3STATUS
);
367 if (status
== WD719X_START_CHANNEL2_3DONE
)
369 if (status
== WD719X_START_CHANNEL2_3ABORT
) {
370 dev_warn(&wd
->pdev
->dev
, "RISC bootstrap failed: DMA aborted\n");
372 goto wd719x_init_end
;
377 dev_warn(&wd
->pdev
->dev
, "RISC bootstrap failed: DMA timeout\n");
379 goto wd719x_init_end
;
382 /* firmware is loaded, now initialize and wake up the RISC */
383 /* write RISC initialization long words to Spider */
384 wd719x_writel(wd
, WD719X_AMR_SCB_IN
, risc_init
[0]);
385 wd719x_writel(wd
, WD719X_AMR_SCB_IN
+ 4, risc_init
[1]);
386 wd719x_writel(wd
, WD719X_AMR_SCB_IN
+ 8, risc_init
[2]);
388 /* disable interrupts during initialization of RISC */
389 wd719x_writeb(wd
, WD719X_AMR_CMD_PARAM
, WD719X_DISABLE_INT
);
391 /* issue INITIALIZE RISC comand */
392 wd719x_writeb(wd
, WD719X_AMR_COMMAND
, WD719X_CMD_INIT_RISC
);
393 /* enable advanced mode (wake up RISC) */
394 wd719x_writeb(wd
, WD719X_PCI_MODE_SELECT
, WD719X_ENABLE_ADVANCE_MODE
);
395 udelay(WD719X_WAIT_FOR_RISC
);
397 ret
= wd719x_wait_done(wd
, WD719X_WAIT_FOR_RISC
);
398 /* clear interrupt status register */
399 wd719x_writeb(wd
, WD719X_AMR_INT_STATUS
, WD719X_INT_NONE
);
401 dev_warn(&wd
->pdev
->dev
, "Unable to initialize RISC\n");
402 goto wd719x_init_end
;
404 /* RISC is up and running */
406 /* Read FW version from RISC */
407 ret
= wd719x_direct_cmd(wd
, WD719X_CMD_READ_FIRMVER
, 0, 0, 0, 0,
408 WD719X_WAIT_FOR_RISC
);
410 dev_warn(&wd
->pdev
->dev
, "Unable to read firmware version\n");
411 goto wd719x_init_end
;
413 dev_info(&wd
->pdev
->dev
, "RISC initialized with firmware version %.2x.%.2x\n",
414 wd719x_readb(wd
, WD719X_AMR_SCB_OUT
+ 1),
415 wd719x_readb(wd
, WD719X_AMR_SCB_OUT
));
418 ret
= wd719x_direct_cmd(wd
, WD719X_CMD_BUSRESET
, 0, 0, 0, 0,
419 WD719X_WAIT_FOR_SCSI_RESET
);
421 dev_warn(&wd
->pdev
->dev
, "SCSI bus reset failed\n");
422 goto wd719x_init_end
;
425 /* use HostParameter structure to set Spider's Host Parameter Block */
426 ret
= wd719x_direct_cmd(wd
, WD719X_CMD_SET_PARAM
, 0,
427 sizeof(struct wd719x_host_param
), 0,
428 wd
->params_phys
, WD719X_WAIT_FOR_RISC
);
430 dev_warn(&wd
->pdev
->dev
, "Failed to set HOST PARAMETERS\n");
431 goto wd719x_init_end
;
434 /* initiate SCAM (does nothing if disabled in BIOS) */
435 /* bug?: we should pass a mask of static IDs which we don't have */
436 ret
= wd719x_direct_cmd(wd
, WD719X_CMD_INIT_SCAM
, 0, 0, 0, 0,
437 WD719X_WAIT_FOR_SCSI_RESET
);
439 dev_warn(&wd
->pdev
->dev
, "SCAM initialization failed\n");
440 goto wd719x_init_end
;
443 /* clear AMR_BIOS_SHARE_INT register */
444 wd719x_writeb(wd
, WD719X_AMR_BIOS_SHARE_INT
, 0);
447 release_firmware(fw_wcs
);
448 release_firmware(fw_risc
);
453 static int wd719x_abort(struct scsi_cmnd
*cmd
)
457 struct wd719x_scb
*scb
= scsi_cmd_priv(cmd
);
458 struct wd719x
*wd
= shost_priv(cmd
->device
->host
);
460 dev_info(&wd
->pdev
->dev
, "abort command, tag: %x\n", cmd
->tag
);
462 action
= /*cmd->tag ? WD719X_CMD_ABORT_TAG : */WD719X_CMD_ABORT
;
464 spin_lock_irqsave(wd
->sh
->host_lock
, flags
);
465 result
= wd719x_direct_cmd(wd
, action
, cmd
->device
->id
,
466 cmd
->device
->lun
, cmd
->tag
, scb
->phys
, 0);
467 spin_unlock_irqrestore(wd
->sh
->host_lock
, flags
);
474 static int wd719x_reset(struct scsi_cmnd
*cmd
, u8 opcode
, u8 device
)
478 struct wd719x
*wd
= shost_priv(cmd
->device
->host
);
480 dev_info(&wd
->pdev
->dev
, "%s reset requested\n",
481 (opcode
== WD719X_CMD_BUSRESET
) ? "bus" : "device");
483 spin_lock_irqsave(wd
->sh
->host_lock
, flags
);
484 result
= wd719x_direct_cmd(wd
, opcode
, device
, 0, 0, 0,
485 WD719X_WAIT_FOR_SCSI_RESET
);
486 spin_unlock_irqrestore(wd
->sh
->host_lock
, flags
);
493 static int wd719x_dev_reset(struct scsi_cmnd
*cmd
)
495 return wd719x_reset(cmd
, WD719X_CMD_RESET
, cmd
->device
->id
);
498 static int wd719x_bus_reset(struct scsi_cmnd
*cmd
)
500 return wd719x_reset(cmd
, WD719X_CMD_BUSRESET
, 0);
503 static int wd719x_host_reset(struct scsi_cmnd
*cmd
)
505 struct wd719x
*wd
= shost_priv(cmd
->device
->host
);
506 struct wd719x_scb
*scb
, *tmp
;
510 dev_info(&wd
->pdev
->dev
, "host reset requested\n");
511 spin_lock_irqsave(wd
->sh
->host_lock
, flags
);
512 /* Try to reinit the RISC */
513 if (wd719x_chip_init(wd
) == 0)
519 list_for_each_entry_safe(scb
, tmp
, &wd
->active_scbs
, list
)
520 wd719x_finish_cmd(scb
, result
);
521 spin_unlock_irqrestore(wd
->sh
->host_lock
, flags
);
526 static int wd719x_biosparam(struct scsi_device
*sdev
, struct block_device
*bdev
,
527 sector_t capacity
, int geom
[])
529 if (capacity
>= 0x200000) {
530 geom
[0] = 255; /* heads */
531 geom
[1] = 63; /* sectors */
533 geom
[0] = 64; /* heads */
534 geom
[1] = 32; /* sectors */
536 geom
[2] = sector_div(capacity
, geom
[0] * geom
[1]); /* cylinders */
541 /* process a SCB-completion interrupt */
542 static inline void wd719x_interrupt_SCB(struct wd719x
*wd
,
543 union wd719x_regs regs
,
544 struct wd719x_scb
*scb
)
548 /* now have to find result from card */
549 switch (regs
.bytes
.SUE
) {
550 case WD719X_SUE_NOERRORS
:
553 case WD719X_SUE_REJECTED
:
554 dev_err(&wd
->pdev
->dev
, "command rejected\n");
557 case WD719X_SUE_SCBQFULL
:
558 dev_err(&wd
->pdev
->dev
, "SCB queue is full\n");
561 case WD719X_SUE_TERM
:
562 dev_dbg(&wd
->pdev
->dev
, "SCB terminated by direct command\n");
563 result
= DID_ABORT
; /* or DID_RESET? */
565 case WD719X_SUE_CHAN1ABORT
:
566 case WD719X_SUE_CHAN23ABORT
:
568 dev_err(&wd
->pdev
->dev
, "DMA abort\n");
570 case WD719X_SUE_CHAN1PAR
:
571 case WD719X_SUE_CHAN23PAR
:
573 dev_err(&wd
->pdev
->dev
, "DMA parity error\n");
575 case WD719X_SUE_TIMEOUT
:
576 result
= DID_TIME_OUT
;
577 dev_dbg(&wd
->pdev
->dev
, "selection timeout\n");
579 case WD719X_SUE_RESET
:
580 dev_dbg(&wd
->pdev
->dev
, "bus reset occurred\n");
583 case WD719X_SUE_BUSERROR
:
584 dev_dbg(&wd
->pdev
->dev
, "SCSI bus error\n");
587 case WD719X_SUE_WRONGWAY
:
588 dev_err(&wd
->pdev
->dev
, "wrong data transfer direction\n");
591 case WD719X_SUE_BADPHASE
:
592 dev_err(&wd
->pdev
->dev
, "invalid SCSI phase\n");
595 case WD719X_SUE_TOOLONG
:
596 dev_err(&wd
->pdev
->dev
, "record too long\n");
599 case WD719X_SUE_BUSFREE
:
600 dev_err(&wd
->pdev
->dev
, "unexpected bus free\n");
601 result
= DID_NO_CONNECT
; /* or DID_ERROR ???*/
603 case WD719X_SUE_ARSDONE
:
604 dev_dbg(&wd
->pdev
->dev
, "auto request sense\n");
605 if (regs
.bytes
.SCSI
== 0)
610 case WD719X_SUE_IGNORED
:
611 dev_err(&wd
->pdev
->dev
, "target id %d ignored command\n",
612 scb
->cmd
->device
->id
);
613 result
= DID_NO_CONNECT
;
615 case WD719X_SUE_WRONGTAGS
:
616 dev_err(&wd
->pdev
->dev
, "reversed tags\n");
619 case WD719X_SUE_BADTAGS
:
620 dev_err(&wd
->pdev
->dev
, "tag type not supported by target\n");
623 case WD719X_SUE_NOSCAMID
:
624 dev_err(&wd
->pdev
->dev
, "no SCAM soft ID available\n");
628 dev_warn(&wd
->pdev
->dev
, "unknown SUE error code: 0x%x\n",
634 wd719x_finish_cmd(scb
, result
);
637 static irqreturn_t
wd719x_interrupt(int irq
, void *dev_id
)
639 struct wd719x
*wd
= dev_id
;
640 union wd719x_regs regs
;
644 spin_lock_irqsave(wd
->sh
->host_lock
, flags
);
645 /* read SCB pointer back from card */
646 SCB_out
= wd719x_readl(wd
, WD719X_AMR_SCB_OUT
);
647 /* read all status info at once */
648 regs
.all
= cpu_to_le32(wd719x_readl(wd
, WD719X_AMR_OP_CODE
));
650 switch (regs
.bytes
.INT
) {
651 case WD719X_INT_NONE
:
652 spin_unlock_irqrestore(wd
->sh
->host_lock
, flags
);
654 case WD719X_INT_LINKNOSTATUS
:
655 dev_err(&wd
->pdev
->dev
, "linked command completed with no status\n");
657 case WD719X_INT_BADINT
:
658 dev_err(&wd
->pdev
->dev
, "unsolicited interrupt\n");
660 case WD719X_INT_NOERRORS
:
661 case WD719X_INT_LINKNOERRORS
:
662 case WD719X_INT_ERRORSLOGGED
:
663 case WD719X_INT_SPIDERFAILED
:
664 /* was the cmd completed a direct or SCB command? */
665 if (regs
.bytes
.OPC
== WD719X_CMD_PROCESS_SCB
) {
666 struct wd719x_scb
*scb
;
667 list_for_each_entry(scb
, &wd
->active_scbs
, list
)
668 if (SCB_out
== scb
->phys
)
670 if (SCB_out
== scb
->phys
)
671 wd719x_interrupt_SCB(wd
, regs
, scb
);
673 dev_err(&wd
->pdev
->dev
, "card returned invalid SCB pointer\n");
675 dev_warn(&wd
->pdev
->dev
, "direct command 0x%x completed\n",
678 case WD719X_INT_PIOREADY
:
679 dev_err(&wd
->pdev
->dev
, "card indicates PIO data ready but we never use PIO\n");
680 /* interrupt will not be cleared until all data is read */
683 dev_err(&wd
->pdev
->dev
, "unknown interrupt reason: %d\n",
687 /* clear interrupt so another can happen */
688 wd719x_writeb(wd
, WD719X_AMR_INT_STATUS
, WD719X_INT_NONE
);
689 spin_unlock_irqrestore(wd
->sh
->host_lock
, flags
);
694 static void wd719x_eeprom_reg_read(struct eeprom_93cx6
*eeprom
)
696 struct wd719x
*wd
= eeprom
->data
;
697 u8 reg
= wd719x_readb(wd
, WD719X_PCI_GPIO_DATA
);
699 eeprom
->reg_data_out
= reg
& WD719X_EE_DO
;
702 static void wd719x_eeprom_reg_write(struct eeprom_93cx6
*eeprom
)
704 struct wd719x
*wd
= eeprom
->data
;
707 if (eeprom
->reg_data_in
)
709 if (eeprom
->reg_data_clock
)
710 reg
|= WD719X_EE_CLK
;
711 if (eeprom
->reg_chip_select
)
714 wd719x_writeb(wd
, WD719X_PCI_GPIO_DATA
, reg
);
717 /* read config from EEPROM so it can be downloaded by the RISC on (re-)init */
718 static void wd719x_read_eeprom(struct wd719x
*wd
)
720 struct eeprom_93cx6 eeprom
;
722 struct wd719x_eeprom_header header
;
725 eeprom
.register_read
= wd719x_eeprom_reg_read
;
726 eeprom
.register_write
= wd719x_eeprom_reg_write
;
727 eeprom
.width
= PCI_EEPROM_WIDTH_93C46
;
729 /* set all outputs to low */
730 wd719x_writeb(wd
, WD719X_PCI_GPIO_DATA
, 0);
731 /* configure GPIO pins */
732 gpio
= wd719x_readb(wd
, WD719X_PCI_GPIO_CONTROL
);
734 gpio
&= (~(WD719X_EE_CLK
| WD719X_EE_DI
| WD719X_EE_CS
));
736 gpio
|= WD719X_EE_DO
;
737 wd719x_writeb(wd
, WD719X_PCI_GPIO_CONTROL
, gpio
);
739 /* read EEPROM header */
740 eeprom_93cx6_multireadb(&eeprom
, 0, (u8
*)&header
, sizeof(header
));
742 if (header
.sig1
== 'W' && header
.sig2
== 'D')
743 eeprom_93cx6_multireadb(&eeprom
, header
.cfg_offset
,
745 sizeof(struct wd719x_host_param
));
746 else { /* default EEPROM values */
747 dev_warn(&wd
->pdev
->dev
, "EEPROM signature is invalid (0x%02x 0x%02x), using default values\n",
748 header
.sig1
, header
.sig2
);
749 wd
->params
->ch_1_th
= 0x10; /* 16 DWs = 64 B */
750 wd
->params
->scsi_conf
= 0x4c; /* 48ma, spue, parity check */
751 wd
->params
->own_scsi_id
= 0x07; /* ID 7, SCAM disabled */
752 wd
->params
->sel_timeout
= 0x4d; /* 250 ms */
753 wd
->params
->sleep_timer
= 0x01;
754 wd
->params
->cdb_size
= cpu_to_le16(0x5555); /* all 6 B */
755 wd
->params
->scsi_pad
= 0x1b;
756 if (wd
->type
== WD719X_TYPE_7193
) /* narrow card - disable */
757 wd
->params
->wide
= cpu_to_le32(0x00000000);
758 else /* initiate & respond to WIDE messages */
759 wd
->params
->wide
= cpu_to_le32(0xffffffff);
760 wd
->params
->sync
= cpu_to_le32(0xffffffff);
761 wd
->params
->soft_mask
= 0x00; /* all disabled */
762 wd
->params
->unsol_mask
= 0x00; /* all disabled */
764 /* disable TAGGED messages */
765 wd
->params
->tag_en
= cpu_to_le16(0x0000);
768 /* Read card type from GPIO bits 1 and 3 */
769 static enum wd719x_card_type
wd719x_detect_type(struct wd719x
*wd
)
771 u8 card
= wd719x_readb(wd
, WD719X_PCI_GPIO_CONTROL
);
773 card
|= WD719X_GPIO_ID_BITS
;
774 wd719x_writeb(wd
, WD719X_PCI_GPIO_CONTROL
, card
);
775 card
= wd719x_readb(wd
, WD719X_PCI_GPIO_DATA
) & WD719X_GPIO_ID_BITS
;
778 return WD719X_TYPE_7193
;
780 return WD719X_TYPE_7197
;
782 return WD719X_TYPE_7296
;
784 dev_warn(&wd
->pdev
->dev
, "unknown card type 0x%x\n", card
);
785 return WD719X_TYPE_UNKNOWN
;
789 static int wd719x_board_found(struct Scsi_Host
*sh
)
791 struct wd719x
*wd
= shost_priv(sh
);
792 static const char * const card_types
[] = {
793 "Unknown card", "WD7193", "WD7197", "WD7296"
797 INIT_LIST_HEAD(&wd
->active_scbs
);
799 sh
->base
= pci_resource_start(wd
->pdev
, 0);
801 wd
->type
= wd719x_detect_type(wd
);
804 sh
->irq
= wd
->pdev
->irq
;
807 /* memory area for host (EEPROM) parameters */
808 wd
->params
= dma_alloc_coherent(&wd
->pdev
->dev
,
809 sizeof(struct wd719x_host_param
),
810 &wd
->params_phys
, GFP_KERNEL
);
812 dev_warn(&wd
->pdev
->dev
, "unable to allocate parameter buffer\n");
816 /* memory area for the RISC for hash table of outstanding requests */
817 wd
->hash_virt
= dma_alloc_coherent(&wd
->pdev
->dev
,
818 WD719X_HASH_TABLE_SIZE
,
819 &wd
->hash_phys
, GFP_KERNEL
);
820 if (!wd
->hash_virt
) {
821 dev_warn(&wd
->pdev
->dev
, "unable to allocate hash buffer\n");
823 goto fail_free_params
;
826 ret
= request_irq(wd
->pdev
->irq
, wd719x_interrupt
, IRQF_SHARED
,
829 dev_warn(&wd
->pdev
->dev
, "unable to assign IRQ %d\n",
834 /* read parameters from EEPROM */
835 wd719x_read_eeprom(wd
);
837 ret
= wd719x_chip_init(wd
);
841 sh
->this_id
= wd
->params
->own_scsi_id
& WD719X_EE_SCSI_ID_MASK
;
843 dev_info(&wd
->pdev
->dev
, "%s at I/O 0x%lx, IRQ %u, SCSI ID %d\n",
844 card_types
[wd
->type
], sh
->base
, sh
->irq
, sh
->this_id
);
849 free_irq(wd
->pdev
->irq
, wd
);
851 dma_free_coherent(&wd
->pdev
->dev
, WD719X_HASH_TABLE_SIZE
, wd
->hash_virt
,
854 dma_free_coherent(&wd
->pdev
->dev
, sizeof(struct wd719x_host_param
),
855 wd
->params
, wd
->params_phys
);
860 static struct scsi_host_template wd719x_template
= {
861 .module
= THIS_MODULE
,
862 .name
= "Western Digital 719x",
863 .cmd_size
= sizeof(struct wd719x_scb
),
864 .queuecommand
= wd719x_queuecommand
,
865 .eh_abort_handler
= wd719x_abort
,
866 .eh_device_reset_handler
= wd719x_dev_reset
,
867 .eh_bus_reset_handler
= wd719x_bus_reset
,
868 .eh_host_reset_handler
= wd719x_host_reset
,
869 .bios_param
= wd719x_biosparam
,
870 .proc_name
= "wd719x",
873 .sg_tablesize
= WD719X_SG
,
876 static int wd719x_pci_probe(struct pci_dev
*pdev
, const struct pci_device_id
*d
)
879 struct Scsi_Host
*sh
;
882 err
= pci_enable_device(pdev
);
886 if (dma_set_mask(&pdev
->dev
, DMA_BIT_MASK(32))) {
887 dev_warn(&pdev
->dev
, "Unable to set 32-bit DMA mask\n");
891 err
= pci_request_regions(pdev
, "wd719x");
894 pci_set_master(pdev
);
897 if (pci_resource_len(pdev
, 0) == 0)
901 sh
= scsi_host_alloc(&wd719x_template
, sizeof(struct wd719x
));
906 wd
->base
= pci_iomap(pdev
, 0, 0);
911 err
= wd719x_board_found(sh
);
915 err
= scsi_add_host(sh
, &wd
->pdev
->dev
);
921 pci_set_drvdata(pdev
, sh
);
927 pci_iounmap(pdev
, wd
->base
);
931 pci_release_regions(pdev
);
933 pci_disable_device(pdev
);
939 static void wd719x_pci_remove(struct pci_dev
*pdev
)
941 struct Scsi_Host
*sh
= pci_get_drvdata(pdev
);
942 struct wd719x
*wd
= shost_priv(sh
);
944 scsi_remove_host(sh
);
946 pci_iounmap(pdev
, wd
->base
);
947 pci_release_regions(pdev
);
948 pci_disable_device(pdev
);
953 static const struct pci_device_id wd719x_pci_table
[] = {
954 { PCI_DEVICE(PCI_VENDOR_ID_WD
, 0x3296) },
958 MODULE_DEVICE_TABLE(pci
, wd719x_pci_table
);
960 static struct pci_driver wd719x_pci_driver
= {
962 .id_table
= wd719x_pci_table
,
963 .probe
= wd719x_pci_probe
,
964 .remove
= wd719x_pci_remove
,
967 module_pci_driver(wd719x_pci_driver
);
969 MODULE_DESCRIPTION("Western Digital WD7193/7197/7296 SCSI driver");
970 MODULE_AUTHOR("Ondrej Zary, Aaron Dewell, Juergen Gaertner");
971 MODULE_LICENSE("GPL");
972 MODULE_FIRMWARE("wd719x-wcs.bin");
973 MODULE_FIRMWARE("wd719x-risc.bin");