Linux 5.1.15
[linux/fpc-iii.git] / drivers / scsi / wd719x.c
blobe3310e9488d22be38819286ea40fe40044c0b8d1
1 /*
2 * Driver for Western Digital WD7193, WD7197 and WD7296 SCSI cards
3 * Copyright 2013 Ondrej Zary
5 * Original driver by
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
12 * by Chandru M. Sippy
13 * SCSI Storage Products (MCP)
14 * Western Digital Corporation
15 * 09-15-95
17 * http://web.archive.org/web/20070717175254/http://sun1.rrzn.uni-hannover.de/gaertner.juergen/wd719x/Linux/Docu/Spider/
21 * Driver workflow:
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.
30 * flaws:
31 * - abort/reset functions
33 * ToDo:
34 * - tagged queueing
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>
46 #include "wd719x.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)
77 int i = 0;
79 do {
80 if (wd719x_readb(wd, WD719X_AMR_COMMAND) == WD719X_CMD_READY)
81 return 0;
82 udelay(1);
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));
88 return -ETIMEDOUT;
91 /* poll interrupt status register until command finishes */
92 static inline int wd719x_wait_done(struct wd719x *wd, int timeout)
94 u8 status;
96 while (timeout > 0) {
97 status = wd719x_readb(wd, WD719X_AMR_INT_STATUS);
98 if (status)
99 break;
100 timeout--;
101 udelay(1);
104 if (timeout <= 0) {
105 dev_err(&wd->pdev->dev, "direct command timed out\n");
106 return -ETIMEDOUT;
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));
112 return -EIO;
115 return 0;
118 static int wd719x_direct_cmd(struct wd719x *wd, u8 opcode, u8 dev, u8 lun,
119 u8 tag, dma_addr_t data, int timeout)
121 int ret = 0;
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))
128 return -ETIMEDOUT;
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);
135 if (data)
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);
151 return ret;
154 static void wd719x_destroy(struct wd719x *wd)
156 /* stop the RISC */
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");
160 /* disable RISC */
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,
167 wd->fw_phys);
168 wd->fw_virt = NULL;
169 dma_free_coherent(&wd->pdev->dev, WD719X_HASH_TABLE_SIZE, wd->hash_virt,
170 wd->hash_phys);
171 wd->hash_virt = NULL;
172 dma_free_coherent(&wd->pdev->dev, sizeof(struct wd719x_host_param),
173 wd->params, wd->params_phys);
174 wd->params = NULL;
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);
188 scsi_dma_unmap(cmd);
189 dma_unmap_single(&wd->pdev->dev, cmd->SCp.dma_handle,
190 SCSI_SENSE_BUFFERSIZE, DMA_FROM_DEVICE);
192 cmd->result = result << 16;
193 cmd->scsi_done(cmd);
196 /* Build a SCB and send it to the card */
197 static int wd719x_queuecommand(struct Scsi_Host *sh, struct scsi_cmnd *cmd)
199 int i, count_sg;
200 unsigned long flags;
201 struct wd719x_scb *scb = scsi_cmd_priv(cmd);
202 struct wd719x *wd = shost_priv(sh);
204 scb->cmd = cmd;
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);
213 /* map SCB */
214 scb->phys = dma_map_single(&wd->pdev->dev, scb, sizeof(*scb),
215 DMA_BIDIRECTIONAL);
217 if (dma_mapping_error(&wd->pdev->dev, scb->phys))
218 goto out_error;
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))
225 goto out_unmap_scb;
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;
238 /* Scather/gather */
239 count_sg = scsi_dma_map(cmd);
240 if (count_sg < 0)
241 goto out_unmap_sense;
242 BUG_ON(count_sg > WD719X_SG);
244 if (count_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;
259 scb->data_p = 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);
278 return 0;
280 out_unmap_sense:
281 dma_unmap_single(&wd->pdev->dev, cmd->SCp.dma_handle,
282 SCSI_SENSE_BUFFERSIZE, DMA_FROM_DEVICE);
283 out_unmap_scb:
284 dma_unmap_single(&wd->pdev->dev, scb->phys, sizeof(*scb),
285 DMA_BIDIRECTIONAL);
286 out_error:
287 cmd->result = DID_ERROR << 16;
288 cmd->scsi_done(cmd);
289 return 0;
292 static int wd719x_chip_init(struct wd719x *wd)
294 int i, ret;
295 u32 risc_init[3];
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);
304 if (ret) {
305 dev_err(&wd->pdev->dev, "Unable to load firmware %s: %d\n",
306 fwname_wcs, ret);
307 return ret;
309 /* RISC firmware */
310 ret = request_firmware(&fw_risc, fwname_risc, &wd->pdev->dev);
311 if (ret) {
312 dev_err(&wd->pdev->dev, "Unable to load firmware %s: %d\n",
313 fwname_risc, ret);
314 release_firmware(fw_wcs);
315 return ret;
317 wd->fw_size = ALIGN(fw_wcs->size, 4) + fw_risc->size;
319 if (!wd->fw_virt)
320 wd->fw_virt = dma_alloc_coherent(&wd->pdev->dev, wd->fw_size,
321 &wd->fw_phys, GFP_KERNEL);
322 if (!wd->fw_virt) {
323 ret = -ENOMEM;
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,
330 fw_risc->size);
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)) {
342 ret = -ETIMEDOUT;
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);
360 /* start DMA */
361 wd719x_writeb(wd, WD719X_PCI_CHANNEL2_3CMD, WD719X_START_CHANNEL2_3DMA);
363 /* wait for DMA to complete */
364 i = WD719X_WAIT_FOR_RISC;
365 while (i-- > 0) {
366 u8 status = wd719x_readb(wd, WD719X_PCI_CHANNEL2_3STATUS);
367 if (status == WD719X_START_CHANNEL2_3DONE)
368 break;
369 if (status == WD719X_START_CHANNEL2_3ABORT) {
370 dev_warn(&wd->pdev->dev, "RISC bootstrap failed: DMA aborted\n");
371 ret = -EIO;
372 goto wd719x_init_end;
374 udelay(1);
376 if (i < 1) {
377 dev_warn(&wd->pdev->dev, "RISC bootstrap failed: DMA timeout\n");
378 ret = -ETIMEDOUT;
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);
400 if (ret) {
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);
409 if (ret) {
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));
417 /* RESET SCSI bus */
418 ret = wd719x_direct_cmd(wd, WD719X_CMD_BUSRESET, 0, 0, 0, 0,
419 WD719X_WAIT_FOR_SCSI_RESET);
420 if (ret) {
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);
429 if (ret) {
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);
438 if (ret) {
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);
446 wd719x_init_end:
447 release_firmware(fw_wcs);
448 release_firmware(fw_risc);
450 return ret;
453 static int wd719x_abort(struct scsi_cmnd *cmd)
455 int action, result;
456 unsigned long flags;
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);
468 if (result)
469 return FAILED;
471 return SUCCESS;
474 static int wd719x_reset(struct scsi_cmnd *cmd, u8 opcode, u8 device)
476 int result;
477 unsigned long flags;
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);
487 if (result)
488 return FAILED;
490 return SUCCESS;
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;
507 unsigned long flags;
508 int result;
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)
514 result = SUCCESS;
515 else
516 result = FAILED;
518 /* flush all SCBs */
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);
523 return result;
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 */
532 } else {
533 geom[0] = 64; /* heads */
534 geom[1] = 32; /* sectors */
536 geom[2] = sector_div(capacity, geom[0] * geom[1]); /* cylinders */
538 return 0;
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)
546 int result;
548 /* now have to find result from card */
549 switch (regs.bytes.SUE) {
550 case WD719X_SUE_NOERRORS:
551 result = DID_OK;
552 break;
553 case WD719X_SUE_REJECTED:
554 dev_err(&wd->pdev->dev, "command rejected\n");
555 result = DID_ERROR;
556 break;
557 case WD719X_SUE_SCBQFULL:
558 dev_err(&wd->pdev->dev, "SCB queue is full\n");
559 result = DID_ERROR;
560 break;
561 case WD719X_SUE_TERM:
562 dev_dbg(&wd->pdev->dev, "SCB terminated by direct command\n");
563 result = DID_ABORT; /* or DID_RESET? */
564 break;
565 case WD719X_SUE_CHAN1ABORT:
566 case WD719X_SUE_CHAN23ABORT:
567 result = DID_ABORT;
568 dev_err(&wd->pdev->dev, "DMA abort\n");
569 break;
570 case WD719X_SUE_CHAN1PAR:
571 case WD719X_SUE_CHAN23PAR:
572 result = DID_PARITY;
573 dev_err(&wd->pdev->dev, "DMA parity error\n");
574 break;
575 case WD719X_SUE_TIMEOUT:
576 result = DID_TIME_OUT;
577 dev_dbg(&wd->pdev->dev, "selection timeout\n");
578 break;
579 case WD719X_SUE_RESET:
580 dev_dbg(&wd->pdev->dev, "bus reset occurred\n");
581 result = DID_RESET;
582 break;
583 case WD719X_SUE_BUSERROR:
584 dev_dbg(&wd->pdev->dev, "SCSI bus error\n");
585 result = DID_ERROR;
586 break;
587 case WD719X_SUE_WRONGWAY:
588 dev_err(&wd->pdev->dev, "wrong data transfer direction\n");
589 result = DID_ERROR;
590 break;
591 case WD719X_SUE_BADPHASE:
592 dev_err(&wd->pdev->dev, "invalid SCSI phase\n");
593 result = DID_ERROR;
594 break;
595 case WD719X_SUE_TOOLONG:
596 dev_err(&wd->pdev->dev, "record too long\n");
597 result = DID_ERROR;
598 break;
599 case WD719X_SUE_BUSFREE:
600 dev_err(&wd->pdev->dev, "unexpected bus free\n");
601 result = DID_NO_CONNECT; /* or DID_ERROR ???*/
602 break;
603 case WD719X_SUE_ARSDONE:
604 dev_dbg(&wd->pdev->dev, "auto request sense\n");
605 if (regs.bytes.SCSI == 0)
606 result = DID_OK;
607 else
608 result = DID_PARITY;
609 break;
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;
614 break;
615 case WD719X_SUE_WRONGTAGS:
616 dev_err(&wd->pdev->dev, "reversed tags\n");
617 result = DID_ERROR;
618 break;
619 case WD719X_SUE_BADTAGS:
620 dev_err(&wd->pdev->dev, "tag type not supported by target\n");
621 result = DID_ERROR;
622 break;
623 case WD719X_SUE_NOSCAMID:
624 dev_err(&wd->pdev->dev, "no SCAM soft ID available\n");
625 result = DID_ERROR;
626 break;
627 default:
628 dev_warn(&wd->pdev->dev, "unknown SUE error code: 0x%x\n",
629 regs.bytes.SUE);
630 result = DID_ERROR;
631 break;
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;
641 unsigned long flags;
642 u32 SCB_out;
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);
653 return IRQ_NONE;
654 case WD719X_INT_LINKNOSTATUS:
655 dev_err(&wd->pdev->dev, "linked command completed with no status\n");
656 break;
657 case WD719X_INT_BADINT:
658 dev_err(&wd->pdev->dev, "unsolicited interrupt\n");
659 break;
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)
669 break;
670 if (SCB_out == scb->phys)
671 wd719x_interrupt_SCB(wd, regs, scb);
672 else
673 dev_err(&wd->pdev->dev, "card returned invalid SCB pointer\n");
674 } else
675 dev_warn(&wd->pdev->dev, "direct command 0x%x completed\n",
676 regs.bytes.OPC);
677 break;
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 */
681 break;
682 default:
683 dev_err(&wd->pdev->dev, "unknown interrupt reason: %d\n",
684 regs.bytes.INT);
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);
691 return IRQ_HANDLED;
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;
705 u8 reg = 0;
707 if (eeprom->reg_data_in)
708 reg |= WD719X_EE_DI;
709 if (eeprom->reg_data_clock)
710 reg |= WD719X_EE_CLK;
711 if (eeprom->reg_chip_select)
712 reg |= WD719X_EE_CS;
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;
721 u8 gpio;
722 struct wd719x_eeprom_header header;
724 eeprom.data = wd;
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);
733 /* GPIO outputs */
734 gpio &= (~(WD719X_EE_CLK | WD719X_EE_DI | WD719X_EE_CS));
735 /* GPIO input */
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,
744 (u8 *)wd->params,
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;
776 switch (card) {
777 case 0x08:
778 return WD719X_TYPE_7193;
779 case 0x02:
780 return WD719X_TYPE_7197;
781 case 0x00:
782 return WD719X_TYPE_7296;
783 default:
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"
795 int ret;
797 INIT_LIST_HEAD(&wd->active_scbs);
799 sh->base = pci_resource_start(wd->pdev, 0);
801 wd->type = wd719x_detect_type(wd);
803 wd->sh = sh;
804 sh->irq = wd->pdev->irq;
805 wd->fw_virt = NULL;
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);
811 if (!wd->params) {
812 dev_warn(&wd->pdev->dev, "unable to allocate parameter buffer\n");
813 return -ENOMEM;
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");
822 ret = -ENOMEM;
823 goto fail_free_params;
826 ret = request_irq(wd->pdev->irq, wd719x_interrupt, IRQF_SHARED,
827 "wd719x", wd);
828 if (ret) {
829 dev_warn(&wd->pdev->dev, "unable to assign IRQ %d\n",
830 wd->pdev->irq);
831 goto fail_free_hash;
834 /* read parameters from EEPROM */
835 wd719x_read_eeprom(wd);
837 ret = wd719x_chip_init(wd);
838 if (ret)
839 goto fail_free_irq;
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);
846 return 0;
848 fail_free_irq:
849 free_irq(wd->pdev->irq, wd);
850 fail_free_hash:
851 dma_free_coherent(&wd->pdev->dev, WD719X_HASH_TABLE_SIZE, wd->hash_virt,
852 wd->hash_phys);
853 fail_free_params:
854 dma_free_coherent(&wd->pdev->dev, sizeof(struct wd719x_host_param),
855 wd->params, wd->params_phys);
857 return ret;
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",
871 .can_queue = 255,
872 .this_id = 7,
873 .sg_tablesize = WD719X_SG,
876 static int wd719x_pci_probe(struct pci_dev *pdev, const struct pci_device_id *d)
878 int err;
879 struct Scsi_Host *sh;
880 struct wd719x *wd;
882 err = pci_enable_device(pdev);
883 if (err)
884 goto fail;
886 if (dma_set_mask(&pdev->dev, DMA_BIT_MASK(32))) {
887 dev_warn(&pdev->dev, "Unable to set 32-bit DMA mask\n");
888 goto disable_device;
891 err = pci_request_regions(pdev, "wd719x");
892 if (err)
893 goto disable_device;
894 pci_set_master(pdev);
896 err = -ENODEV;
897 if (pci_resource_len(pdev, 0) == 0)
898 goto release_region;
900 err = -ENOMEM;
901 sh = scsi_host_alloc(&wd719x_template, sizeof(struct wd719x));
902 if (!sh)
903 goto release_region;
905 wd = shost_priv(sh);
906 wd->base = pci_iomap(pdev, 0, 0);
907 if (!wd->base)
908 goto free_host;
909 wd->pdev = pdev;
911 err = wd719x_board_found(sh);
912 if (err)
913 goto unmap;
915 err = scsi_add_host(sh, &wd->pdev->dev);
916 if (err)
917 goto destroy;
919 scsi_scan_host(sh);
921 pci_set_drvdata(pdev, sh);
922 return 0;
924 destroy:
925 wd719x_destroy(wd);
926 unmap:
927 pci_iounmap(pdev, wd->base);
928 free_host:
929 scsi_host_put(sh);
930 release_region:
931 pci_release_regions(pdev);
932 disable_device:
933 pci_disable_device(pdev);
934 fail:
935 return err;
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);
945 wd719x_destroy(wd);
946 pci_iounmap(pdev, wd->base);
947 pci_release_regions(pdev);
948 pci_disable_device(pdev);
950 scsi_host_put(sh);
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 = {
961 .name = "wd719x",
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");