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
)
156 struct wd719x_scb
*scb
;
159 if (wd719x_direct_cmd(wd
, WD719X_CMD_SLEEP
, 0, 0, 0, 0,
160 WD719X_WAIT_FOR_RISC
))
161 dev_warn(&wd
->pdev
->dev
, "RISC sleep command failed\n");
163 wd719x_writeb(wd
, WD719X_PCI_MODE_SELECT
, 0);
166 list_for_each_entry(scb
, &wd
->active_scbs
, list
)
167 pci_free_consistent(wd
->pdev
, sizeof(struct wd719x_scb
), scb
,
169 list_for_each_entry(scb
, &wd
->free_scbs
, list
)
170 pci_free_consistent(wd
->pdev
, sizeof(struct wd719x_scb
), scb
,
172 /* free internal buffers */
173 pci_free_consistent(wd
->pdev
, wd
->fw_size
, wd
->fw_virt
, wd
->fw_phys
);
175 pci_free_consistent(wd
->pdev
, WD719X_HASH_TABLE_SIZE
, wd
->hash_virt
,
177 wd
->hash_virt
= NULL
;
178 pci_free_consistent(wd
->pdev
, sizeof(struct wd719x_host_param
),
179 wd
->params
, wd
->params_phys
);
181 free_irq(wd
->pdev
->irq
, wd
);
184 /* finish a SCSI command, mark SCB (if any) as free, unmap buffers */
185 static void wd719x_finish_cmd(struct scsi_cmnd
*cmd
, int result
)
187 struct wd719x
*wd
= shost_priv(cmd
->device
->host
);
188 struct wd719x_scb
*scb
= (struct wd719x_scb
*) cmd
->host_scribble
;
191 list_move(&scb
->list
, &wd
->free_scbs
);
192 dma_unmap_single(&wd
->pdev
->dev
, cmd
->SCp
.dma_handle
,
193 SCSI_SENSE_BUFFERSIZE
, DMA_FROM_DEVICE
);
196 cmd
->result
= result
<< 16;
200 /* Build a SCB and send it to the card */
201 static int wd719x_queuecommand(struct Scsi_Host
*sh
, struct scsi_cmnd
*cmd
)
205 struct wd719x_scb
*scb
;
206 struct wd719x
*wd
= shost_priv(sh
);
209 cmd
->host_scribble
= NULL
;
211 /* get a free SCB - either from existing ones or allocate a new one */
212 spin_lock_irqsave(wd
->sh
->host_lock
, flags
);
213 scb
= list_first_entry_or_null(&wd
->free_scbs
, struct wd719x_scb
, list
);
215 list_del(&scb
->list
);
218 spin_unlock_irqrestore(wd
->sh
->host_lock
, flags
);
219 scb
= pci_alloc_consistent(wd
->pdev
, sizeof(struct wd719x_scb
),
221 spin_lock_irqsave(wd
->sh
->host_lock
, flags
);
223 dev_err(&wd
->pdev
->dev
, "unable to allocate SCB\n");
224 wd719x_finish_cmd(cmd
, DID_ERROR
);
225 spin_unlock_irqrestore(wd
->sh
->host_lock
, flags
);
229 memset(scb
, 0, sizeof(struct wd719x_scb
));
230 list_add(&scb
->list
, &wd
->active_scbs
);
234 cmd
->host_scribble
= (char *) scb
;
236 scb
->CDB_tag
= 0; /* Tagged queueing not supported yet */
237 scb
->devid
= cmd
->device
->id
;
238 scb
->lun
= cmd
->device
->lun
;
240 /* copy the command */
241 memcpy(scb
->CDB
, cmd
->cmnd
, cmd
->cmd_len
);
243 /* map sense buffer */
244 scb
->sense_buf_length
= SCSI_SENSE_BUFFERSIZE
;
245 cmd
->SCp
.dma_handle
= dma_map_single(&wd
->pdev
->dev
, cmd
->sense_buffer
,
246 SCSI_SENSE_BUFFERSIZE
, DMA_FROM_DEVICE
);
247 scb
->sense_buf
= cpu_to_le32(cmd
->SCp
.dma_handle
);
249 /* request autosense */
250 scb
->SCB_options
|= WD719X_SCB_FLAGS_AUTO_REQUEST_SENSE
;
252 /* check direction */
253 if (cmd
->sc_data_direction
== DMA_TO_DEVICE
)
254 scb
->SCB_options
|= WD719X_SCB_FLAGS_CHECK_DIRECTION
255 | WD719X_SCB_FLAGS_PCI_TO_SCSI
;
256 else if (cmd
->sc_data_direction
== DMA_FROM_DEVICE
)
257 scb
->SCB_options
|= WD719X_SCB_FLAGS_CHECK_DIRECTION
;
260 count_sg
= scsi_dma_map(cmd
);
262 wd719x_finish_cmd(cmd
, DID_ERROR
);
263 spin_unlock_irqrestore(wd
->sh
->host_lock
, flags
);
266 BUG_ON(count_sg
> WD719X_SG
);
269 struct scatterlist
*sg
;
271 scb
->data_length
= cpu_to_le32(count_sg
*
272 sizeof(struct wd719x_sglist
));
273 scb
->data_p
= cpu_to_le32(scb
->phys
+
274 offsetof(struct wd719x_scb
, sg_list
));
276 scsi_for_each_sg(cmd
, sg
, count_sg
, i
) {
277 scb
->sg_list
[i
].ptr
= cpu_to_le32(sg_dma_address(sg
));
278 scb
->sg_list
[i
].length
= cpu_to_le32(sg_dma_len(sg
));
280 scb
->SCB_options
|= WD719X_SCB_FLAGS_DO_SCATTER_GATHER
;
281 } else { /* zero length */
282 scb
->data_length
= 0;
286 /* check if the Command register is free */
287 if (wd719x_readb(wd
, WD719X_AMR_COMMAND
) != WD719X_CMD_READY
) {
288 spin_unlock_irqrestore(wd
->sh
->host_lock
, flags
);
289 return SCSI_MLQUEUE_HOST_BUSY
;
292 /* write pointer to the AMR */
293 wd719x_writel(wd
, WD719X_AMR_SCB_IN
, scb
->phys
);
294 /* send SCB opcode */
295 wd719x_writeb(wd
, WD719X_AMR_COMMAND
, WD719X_CMD_PROCESS_SCB
);
297 spin_unlock_irqrestore(wd
->sh
->host_lock
, flags
);
302 static int wd719x_chip_init(struct wd719x
*wd
)
306 const struct firmware
*fw_wcs
, *fw_risc
;
307 const char fwname_wcs
[] = "wd719x-wcs.bin";
308 const char fwname_risc
[] = "wd719x-risc.bin";
310 memset(wd
->hash_virt
, 0, WD719X_HASH_TABLE_SIZE
);
312 /* WCS (sequencer) firmware */
313 ret
= request_firmware(&fw_wcs
, fwname_wcs
, &wd
->pdev
->dev
);
315 dev_err(&wd
->pdev
->dev
, "Unable to load firmware %s: %d\n",
320 ret
= request_firmware(&fw_risc
, fwname_risc
, &wd
->pdev
->dev
);
322 dev_err(&wd
->pdev
->dev
, "Unable to load firmware %s: %d\n",
324 release_firmware(fw_wcs
);
327 wd
->fw_size
= ALIGN(fw_wcs
->size
, 4) + fw_risc
->size
;
330 wd
->fw_virt
= pci_alloc_consistent(wd
->pdev
, wd
->fw_size
,
334 goto wd719x_init_end
;
337 /* make a fresh copy of WCS and RISC code */
338 memcpy(wd
->fw_virt
, fw_wcs
->data
, fw_wcs
->size
);
339 memcpy(wd
->fw_virt
+ ALIGN(fw_wcs
->size
, 4), fw_risc
->data
,
342 /* Reset the Spider Chip and adapter itself */
343 wd719x_writeb(wd
, WD719X_PCI_PORT_RESET
, WD719X_PCI_RESET
);
344 udelay(WD719X_WAIT_FOR_RISC
);
345 /* Clear PIO mode bits set by BIOS */
346 wd719x_writeb(wd
, WD719X_AMR_CMD_PARAM
, 0);
347 /* ensure RISC is not running */
348 wd719x_writeb(wd
, WD719X_PCI_MODE_SELECT
, 0);
349 /* ensure command port is ready */
350 wd719x_writeb(wd
, WD719X_AMR_COMMAND
, 0);
351 if (wd719x_wait_ready(wd
)) {
353 goto wd719x_init_end
;
356 /* Transfer the first 2K words of RISC code to kick start the uP */
357 risc_init
[0] = wd
->fw_phys
; /* WCS FW */
358 risc_init
[1] = wd
->fw_phys
+ ALIGN(fw_wcs
->size
, 4); /* RISC FW */
359 risc_init
[2] = wd
->hash_phys
; /* hash table */
361 /* clear DMA status */
362 wd719x_writeb(wd
, WD719X_PCI_CHANNEL2_3STATUS
, 0);
364 /* address to read firmware from */
365 wd719x_writel(wd
, WD719X_PCI_EXTERNAL_ADDR
, risc_init
[1]);
366 /* base address to write firmware to (on card) */
367 wd719x_writew(wd
, WD719X_PCI_INTERNAL_ADDR
, WD719X_PRAM_BASE_ADDR
);
368 /* size: first 2K words */
369 wd719x_writew(wd
, WD719X_PCI_DMA_TRANSFER_SIZE
, 2048 * 2);
371 wd719x_writeb(wd
, WD719X_PCI_CHANNEL2_3CMD
, WD719X_START_CHANNEL2_3DMA
);
373 /* wait for DMA to complete */
374 i
= WD719X_WAIT_FOR_RISC
;
376 u8 status
= wd719x_readb(wd
, WD719X_PCI_CHANNEL2_3STATUS
);
377 if (status
== WD719X_START_CHANNEL2_3DONE
)
379 if (status
== WD719X_START_CHANNEL2_3ABORT
) {
380 dev_warn(&wd
->pdev
->dev
, "RISC bootstrap failed: DMA aborted\n");
382 goto wd719x_init_end
;
387 dev_warn(&wd
->pdev
->dev
, "RISC bootstrap failed: DMA timeout\n");
389 goto wd719x_init_end
;
392 /* firmware is loaded, now initialize and wake up the RISC */
393 /* write RISC initialization long words to Spider */
394 wd719x_writel(wd
, WD719X_AMR_SCB_IN
, risc_init
[0]);
395 wd719x_writel(wd
, WD719X_AMR_SCB_IN
+ 4, risc_init
[1]);
396 wd719x_writel(wd
, WD719X_AMR_SCB_IN
+ 8, risc_init
[2]);
398 /* disable interrupts during initialization of RISC */
399 wd719x_writeb(wd
, WD719X_AMR_CMD_PARAM
, WD719X_DISABLE_INT
);
401 /* issue INITIALIZE RISC comand */
402 wd719x_writeb(wd
, WD719X_AMR_COMMAND
, WD719X_CMD_INIT_RISC
);
403 /* enable advanced mode (wake up RISC) */
404 wd719x_writeb(wd
, WD719X_PCI_MODE_SELECT
, WD719X_ENABLE_ADVANCE_MODE
);
405 udelay(WD719X_WAIT_FOR_RISC
);
407 ret
= wd719x_wait_done(wd
, WD719X_WAIT_FOR_RISC
);
408 /* clear interrupt status register */
409 wd719x_writeb(wd
, WD719X_AMR_INT_STATUS
, WD719X_INT_NONE
);
411 dev_warn(&wd
->pdev
->dev
, "Unable to initialize RISC\n");
412 goto wd719x_init_end
;
414 /* RISC is up and running */
416 /* Read FW version from RISC */
417 ret
= wd719x_direct_cmd(wd
, WD719X_CMD_READ_FIRMVER
, 0, 0, 0, 0,
418 WD719X_WAIT_FOR_RISC
);
420 dev_warn(&wd
->pdev
->dev
, "Unable to read firmware version\n");
421 goto wd719x_init_end
;
423 dev_info(&wd
->pdev
->dev
, "RISC initialized with firmware version %.2x.%.2x\n",
424 wd719x_readb(wd
, WD719X_AMR_SCB_OUT
+ 1),
425 wd719x_readb(wd
, WD719X_AMR_SCB_OUT
));
428 ret
= wd719x_direct_cmd(wd
, WD719X_CMD_BUSRESET
, 0, 0, 0, 0,
429 WD719X_WAIT_FOR_SCSI_RESET
);
431 dev_warn(&wd
->pdev
->dev
, "SCSI bus reset failed\n");
432 goto wd719x_init_end
;
435 /* use HostParameter structure to set Spider's Host Parameter Block */
436 ret
= wd719x_direct_cmd(wd
, WD719X_CMD_SET_PARAM
, 0,
437 sizeof(struct wd719x_host_param
), 0,
438 wd
->params_phys
, WD719X_WAIT_FOR_RISC
);
440 dev_warn(&wd
->pdev
->dev
, "Failed to set HOST PARAMETERS\n");
441 goto wd719x_init_end
;
444 /* initiate SCAM (does nothing if disabled in BIOS) */
445 /* bug?: we should pass a mask of static IDs which we don't have */
446 ret
= wd719x_direct_cmd(wd
, WD719X_CMD_INIT_SCAM
, 0, 0, 0, 0,
447 WD719X_WAIT_FOR_SCSI_RESET
);
449 dev_warn(&wd
->pdev
->dev
, "SCAM initialization failed\n");
450 goto wd719x_init_end
;
453 /* clear AMR_BIOS_SHARE_INT register */
454 wd719x_writeb(wd
, WD719X_AMR_BIOS_SHARE_INT
, 0);
457 release_firmware(fw_wcs
);
458 release_firmware(fw_risc
);
463 static int wd719x_abort(struct scsi_cmnd
*cmd
)
467 struct wd719x_scb
*scb
= (struct wd719x_scb
*)cmd
->host_scribble
;
468 struct wd719x
*wd
= shost_priv(cmd
->device
->host
);
470 dev_info(&wd
->pdev
->dev
, "abort command, tag: %x\n", cmd
->tag
);
472 action
= /*cmd->tag ? WD719X_CMD_ABORT_TAG : */WD719X_CMD_ABORT
;
474 spin_lock_irqsave(wd
->sh
->host_lock
, flags
);
475 result
= wd719x_direct_cmd(wd
, action
, cmd
->device
->id
,
476 cmd
->device
->lun
, cmd
->tag
, scb
->phys
, 0);
477 spin_unlock_irqrestore(wd
->sh
->host_lock
, flags
);
484 static int wd719x_reset(struct scsi_cmnd
*cmd
, u8 opcode
, u8 device
)
488 struct wd719x
*wd
= shost_priv(cmd
->device
->host
);
490 dev_info(&wd
->pdev
->dev
, "%s reset requested\n",
491 (opcode
== WD719X_CMD_BUSRESET
) ? "bus" : "device");
493 spin_lock_irqsave(wd
->sh
->host_lock
, flags
);
494 result
= wd719x_direct_cmd(wd
, opcode
, device
, 0, 0, 0,
495 WD719X_WAIT_FOR_SCSI_RESET
);
496 spin_unlock_irqrestore(wd
->sh
->host_lock
, flags
);
503 static int wd719x_dev_reset(struct scsi_cmnd
*cmd
)
505 return wd719x_reset(cmd
, WD719X_CMD_RESET
, cmd
->device
->id
);
508 static int wd719x_bus_reset(struct scsi_cmnd
*cmd
)
510 return wd719x_reset(cmd
, WD719X_CMD_BUSRESET
, 0);
513 static int wd719x_host_reset(struct scsi_cmnd
*cmd
)
515 struct wd719x
*wd
= shost_priv(cmd
->device
->host
);
516 struct wd719x_scb
*scb
, *tmp
;
520 dev_info(&wd
->pdev
->dev
, "host reset requested\n");
521 spin_lock_irqsave(wd
->sh
->host_lock
, flags
);
522 /* Try to reinit the RISC */
523 if (wd719x_chip_init(wd
) == 0)
529 list_for_each_entry_safe(scb
, tmp
, &wd
->active_scbs
, list
) {
530 struct scsi_cmnd
*tmp_cmd
= scb
->cmd
;
531 wd719x_finish_cmd(tmp_cmd
, result
);
533 spin_unlock_irqrestore(wd
->sh
->host_lock
, flags
);
538 static int wd719x_biosparam(struct scsi_device
*sdev
, struct block_device
*bdev
,
539 sector_t capacity
, int geom
[])
541 if (capacity
>= 0x200000) {
542 geom
[0] = 255; /* heads */
543 geom
[1] = 63; /* sectors */
545 geom
[0] = 64; /* heads */
546 geom
[1] = 32; /* sectors */
548 geom
[2] = sector_div(capacity
, geom
[0] * geom
[1]); /* cylinders */
553 /* process a SCB-completion interrupt */
554 static inline void wd719x_interrupt_SCB(struct wd719x
*wd
,
555 union wd719x_regs regs
,
556 struct wd719x_scb
*scb
)
558 struct scsi_cmnd
*cmd
;
561 /* now have to find result from card */
562 switch (regs
.bytes
.SUE
) {
563 case WD719X_SUE_NOERRORS
:
566 case WD719X_SUE_REJECTED
:
567 dev_err(&wd
->pdev
->dev
, "command rejected\n");
570 case WD719X_SUE_SCBQFULL
:
571 dev_err(&wd
->pdev
->dev
, "SCB queue is full\n");
574 case WD719X_SUE_TERM
:
575 dev_dbg(&wd
->pdev
->dev
, "SCB terminated by direct command\n");
576 result
= DID_ABORT
; /* or DID_RESET? */
578 case WD719X_SUE_CHAN1ABORT
:
579 case WD719X_SUE_CHAN23ABORT
:
581 dev_err(&wd
->pdev
->dev
, "DMA abort\n");
583 case WD719X_SUE_CHAN1PAR
:
584 case WD719X_SUE_CHAN23PAR
:
586 dev_err(&wd
->pdev
->dev
, "DMA parity error\n");
588 case WD719X_SUE_TIMEOUT
:
589 result
= DID_TIME_OUT
;
590 dev_dbg(&wd
->pdev
->dev
, "selection timeout\n");
592 case WD719X_SUE_RESET
:
593 dev_dbg(&wd
->pdev
->dev
, "bus reset occurred\n");
596 case WD719X_SUE_BUSERROR
:
597 dev_dbg(&wd
->pdev
->dev
, "SCSI bus error\n");
600 case WD719X_SUE_WRONGWAY
:
601 dev_err(&wd
->pdev
->dev
, "wrong data transfer direction\n");
604 case WD719X_SUE_BADPHASE
:
605 dev_err(&wd
->pdev
->dev
, "invalid SCSI phase\n");
608 case WD719X_SUE_TOOLONG
:
609 dev_err(&wd
->pdev
->dev
, "record too long\n");
612 case WD719X_SUE_BUSFREE
:
613 dev_err(&wd
->pdev
->dev
, "unexpected bus free\n");
614 result
= DID_NO_CONNECT
; /* or DID_ERROR ???*/
616 case WD719X_SUE_ARSDONE
:
617 dev_dbg(&wd
->pdev
->dev
, "auto request sense\n");
618 if (regs
.bytes
.SCSI
== 0)
623 case WD719X_SUE_IGNORED
:
624 dev_err(&wd
->pdev
->dev
, "target id %d ignored command\n",
625 scb
->cmd
->device
->id
);
626 result
= DID_NO_CONNECT
;
628 case WD719X_SUE_WRONGTAGS
:
629 dev_err(&wd
->pdev
->dev
, "reversed tags\n");
632 case WD719X_SUE_BADTAGS
:
633 dev_err(&wd
->pdev
->dev
, "tag type not supported by target\n");
636 case WD719X_SUE_NOSCAMID
:
637 dev_err(&wd
->pdev
->dev
, "no SCAM soft ID available\n");
641 dev_warn(&wd
->pdev
->dev
, "unknown SUE error code: 0x%x\n",
648 wd719x_finish_cmd(cmd
, result
);
651 static irqreturn_t
wd719x_interrupt(int irq
, void *dev_id
)
653 struct wd719x
*wd
= dev_id
;
654 union wd719x_regs regs
;
658 spin_lock_irqsave(wd
->sh
->host_lock
, flags
);
659 /* read SCB pointer back from card */
660 SCB_out
= wd719x_readl(wd
, WD719X_AMR_SCB_OUT
);
661 /* read all status info at once */
662 regs
.all
= cpu_to_le32(wd719x_readl(wd
, WD719X_AMR_OP_CODE
));
664 switch (regs
.bytes
.INT
) {
665 case WD719X_INT_NONE
:
666 spin_unlock_irqrestore(wd
->sh
->host_lock
, flags
);
668 case WD719X_INT_LINKNOSTATUS
:
669 dev_err(&wd
->pdev
->dev
, "linked command completed with no status\n");
671 case WD719X_INT_BADINT
:
672 dev_err(&wd
->pdev
->dev
, "unsolicited interrupt\n");
674 case WD719X_INT_NOERRORS
:
675 case WD719X_INT_LINKNOERRORS
:
676 case WD719X_INT_ERRORSLOGGED
:
677 case WD719X_INT_SPIDERFAILED
:
678 /* was the cmd completed a direct or SCB command? */
679 if (regs
.bytes
.OPC
== WD719X_CMD_PROCESS_SCB
) {
680 struct wd719x_scb
*scb
;
681 list_for_each_entry(scb
, &wd
->active_scbs
, list
)
682 if (SCB_out
== scb
->phys
)
684 if (SCB_out
== scb
->phys
)
685 wd719x_interrupt_SCB(wd
, regs
, scb
);
687 dev_err(&wd
->pdev
->dev
, "card returned invalid SCB pointer\n");
689 dev_warn(&wd
->pdev
->dev
, "direct command 0x%x completed\n",
692 case WD719X_INT_PIOREADY
:
693 dev_err(&wd
->pdev
->dev
, "card indicates PIO data ready but we never use PIO\n");
694 /* interrupt will not be cleared until all data is read */
697 dev_err(&wd
->pdev
->dev
, "unknown interrupt reason: %d\n",
701 /* clear interrupt so another can happen */
702 wd719x_writeb(wd
, WD719X_AMR_INT_STATUS
, WD719X_INT_NONE
);
703 spin_unlock_irqrestore(wd
->sh
->host_lock
, flags
);
708 static void wd719x_eeprom_reg_read(struct eeprom_93cx6
*eeprom
)
710 struct wd719x
*wd
= eeprom
->data
;
711 u8 reg
= wd719x_readb(wd
, WD719X_PCI_GPIO_DATA
);
713 eeprom
->reg_data_out
= reg
& WD719X_EE_DO
;
716 static void wd719x_eeprom_reg_write(struct eeprom_93cx6
*eeprom
)
718 struct wd719x
*wd
= eeprom
->data
;
721 if (eeprom
->reg_data_in
)
723 if (eeprom
->reg_data_clock
)
724 reg
|= WD719X_EE_CLK
;
725 if (eeprom
->reg_chip_select
)
728 wd719x_writeb(wd
, WD719X_PCI_GPIO_DATA
, reg
);
731 /* read config from EEPROM so it can be downloaded by the RISC on (re-)init */
732 static void wd719x_read_eeprom(struct wd719x
*wd
)
734 struct eeprom_93cx6 eeprom
;
736 struct wd719x_eeprom_header header
;
739 eeprom
.register_read
= wd719x_eeprom_reg_read
;
740 eeprom
.register_write
= wd719x_eeprom_reg_write
;
741 eeprom
.width
= PCI_EEPROM_WIDTH_93C46
;
743 /* set all outputs to low */
744 wd719x_writeb(wd
, WD719X_PCI_GPIO_DATA
, 0);
745 /* configure GPIO pins */
746 gpio
= wd719x_readb(wd
, WD719X_PCI_GPIO_CONTROL
);
748 gpio
&= (~(WD719X_EE_CLK
| WD719X_EE_DI
| WD719X_EE_CS
));
750 gpio
|= WD719X_EE_DO
;
751 wd719x_writeb(wd
, WD719X_PCI_GPIO_CONTROL
, gpio
);
753 /* read EEPROM header */
754 eeprom_93cx6_multireadb(&eeprom
, 0, (u8
*)&header
, sizeof(header
));
756 if (header
.sig1
== 'W' && header
.sig2
== 'D')
757 eeprom_93cx6_multireadb(&eeprom
, header
.cfg_offset
,
759 sizeof(struct wd719x_host_param
));
760 else { /* default EEPROM values */
761 dev_warn(&wd
->pdev
->dev
, "EEPROM signature is invalid (0x%02x 0x%02x), using default values\n",
762 header
.sig1
, header
.sig2
);
763 wd
->params
->ch_1_th
= 0x10; /* 16 DWs = 64 B */
764 wd
->params
->scsi_conf
= 0x4c; /* 48ma, spue, parity check */
765 wd
->params
->own_scsi_id
= 0x07; /* ID 7, SCAM disabled */
766 wd
->params
->sel_timeout
= 0x4d; /* 250 ms */
767 wd
->params
->sleep_timer
= 0x01;
768 wd
->params
->cdb_size
= cpu_to_le16(0x5555); /* all 6 B */
769 wd
->params
->scsi_pad
= 0x1b;
770 if (wd
->type
== WD719X_TYPE_7193
) /* narrow card - disable */
771 wd
->params
->wide
= cpu_to_le32(0x00000000);
772 else /* initiate & respond to WIDE messages */
773 wd
->params
->wide
= cpu_to_le32(0xffffffff);
774 wd
->params
->sync
= cpu_to_le32(0xffffffff);
775 wd
->params
->soft_mask
= 0x00; /* all disabled */
776 wd
->params
->unsol_mask
= 0x00; /* all disabled */
778 /* disable TAGGED messages */
779 wd
->params
->tag_en
= cpu_to_le16(0x0000);
782 /* Read card type from GPIO bits 1 and 3 */
783 static enum wd719x_card_type
wd719x_detect_type(struct wd719x
*wd
)
785 u8 card
= wd719x_readb(wd
, WD719X_PCI_GPIO_CONTROL
);
787 card
|= WD719X_GPIO_ID_BITS
;
788 wd719x_writeb(wd
, WD719X_PCI_GPIO_CONTROL
, card
);
789 card
= wd719x_readb(wd
, WD719X_PCI_GPIO_DATA
) & WD719X_GPIO_ID_BITS
;
792 return WD719X_TYPE_7193
;
794 return WD719X_TYPE_7197
;
796 return WD719X_TYPE_7296
;
798 dev_warn(&wd
->pdev
->dev
, "unknown card type 0x%x\n", card
);
799 return WD719X_TYPE_UNKNOWN
;
803 static int wd719x_board_found(struct Scsi_Host
*sh
)
805 struct wd719x
*wd
= shost_priv(sh
);
806 char *card_types
[] = { "Unknown card", "WD7193", "WD7197", "WD7296" };
809 INIT_LIST_HEAD(&wd
->active_scbs
);
810 INIT_LIST_HEAD(&wd
->free_scbs
);
812 sh
->base
= pci_resource_start(wd
->pdev
, 0);
814 wd
->type
= wd719x_detect_type(wd
);
817 sh
->irq
= wd
->pdev
->irq
;
820 /* memory area for host (EEPROM) parameters */
821 wd
->params
= pci_alloc_consistent(wd
->pdev
,
822 sizeof(struct wd719x_host_param
),
825 dev_warn(&wd
->pdev
->dev
, "unable to allocate parameter buffer\n");
829 /* memory area for the RISC for hash table of outstanding requests */
830 wd
->hash_virt
= pci_alloc_consistent(wd
->pdev
, WD719X_HASH_TABLE_SIZE
,
832 if (!wd
->hash_virt
) {
833 dev_warn(&wd
->pdev
->dev
, "unable to allocate hash buffer\n");
835 goto fail_free_params
;
838 ret
= request_irq(wd
->pdev
->irq
, wd719x_interrupt
, IRQF_SHARED
,
841 dev_warn(&wd
->pdev
->dev
, "unable to assign IRQ %d\n",
846 /* read parameters from EEPROM */
847 wd719x_read_eeprom(wd
);
849 ret
= wd719x_chip_init(wd
);
853 sh
->this_id
= wd
->params
->own_scsi_id
& WD719X_EE_SCSI_ID_MASK
;
855 dev_info(&wd
->pdev
->dev
, "%s at I/O 0x%lx, IRQ %u, SCSI ID %d\n",
856 card_types
[wd
->type
], sh
->base
, sh
->irq
, sh
->this_id
);
861 free_irq(wd
->pdev
->irq
, wd
);
863 pci_free_consistent(wd
->pdev
, WD719X_HASH_TABLE_SIZE
, wd
->hash_virt
,
866 pci_free_consistent(wd
->pdev
, sizeof(struct wd719x_host_param
),
867 wd
->params
, wd
->params_phys
);
872 static struct scsi_host_template wd719x_template
= {
873 .module
= THIS_MODULE
,
874 .name
= "Western Digital 719x",
875 .queuecommand
= wd719x_queuecommand
,
876 .eh_abort_handler
= wd719x_abort
,
877 .eh_device_reset_handler
= wd719x_dev_reset
,
878 .eh_bus_reset_handler
= wd719x_bus_reset
,
879 .eh_host_reset_handler
= wd719x_host_reset
,
880 .bios_param
= wd719x_biosparam
,
881 .proc_name
= "wd719x",
884 .sg_tablesize
= WD719X_SG
,
885 .use_clustering
= ENABLE_CLUSTERING
,
888 static int wd719x_pci_probe(struct pci_dev
*pdev
, const struct pci_device_id
*d
)
891 struct Scsi_Host
*sh
;
894 err
= pci_enable_device(pdev
);
898 if (pci_set_dma_mask(pdev
, DMA_BIT_MASK(32))) {
899 dev_warn(&pdev
->dev
, "Unable to set 32-bit DMA mask\n");
903 err
= pci_request_regions(pdev
, "wd719x");
906 pci_set_master(pdev
);
909 if (pci_resource_len(pdev
, 0) == 0)
913 sh
= scsi_host_alloc(&wd719x_template
, sizeof(struct wd719x
));
918 wd
->base
= pci_iomap(pdev
, 0, 0);
923 err
= wd719x_board_found(sh
);
927 err
= scsi_add_host(sh
, &wd
->pdev
->dev
);
933 pci_set_drvdata(pdev
, sh
);
939 pci_iounmap(pdev
, wd
->base
);
943 pci_release_regions(pdev
);
945 pci_disable_device(pdev
);
951 static void wd719x_pci_remove(struct pci_dev
*pdev
)
953 struct Scsi_Host
*sh
= pci_get_drvdata(pdev
);
954 struct wd719x
*wd
= shost_priv(sh
);
956 scsi_remove_host(sh
);
958 pci_iounmap(pdev
, wd
->base
);
959 pci_release_regions(pdev
);
960 pci_disable_device(pdev
);
965 static DEFINE_PCI_DEVICE_TABLE(wd719x_pci_table
) = {
966 { PCI_DEVICE(PCI_VENDOR_ID_WD
, 0x3296) },
970 MODULE_DEVICE_TABLE(pci
, wd719x_pci_table
);
972 static struct pci_driver wd719x_pci_driver
= {
974 .id_table
= wd719x_pci_table
,
975 .probe
= wd719x_pci_probe
,
976 .remove
= wd719x_pci_remove
,
979 static int __init
wd719x_init(void)
981 return pci_register_driver(&wd719x_pci_driver
);
984 static void __exit
wd719x_exit(void)
986 pci_unregister_driver(&wd719x_pci_driver
);
989 module_init(wd719x_init
);
990 module_exit(wd719x_exit
);
992 MODULE_DESCRIPTION("Western Digital WD7193/7197/7296 SCSI driver");
993 MODULE_AUTHOR("Ondrej Zary, Aaron Dewell, Juergen Gaertner");
994 MODULE_LICENSE("GPL");
995 MODULE_FIRMWARE("wd719x-wcs.bin");
996 MODULE_FIRMWARE("wd719x-risc.bin");