1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Driver for Western Digital WD7193, WD7197 and WD7296 SCSI cards 4 * Copyright 2013 Ondrej Zary 5 * 6 * Original driver by 7 * Aaron Dewell <dewell@woods.net> 8 * Gaerti <Juergen.Gaertner@mbox.si.uni-hannover.de> 9 * 10 * HW documentation available in book: 11 * 12 * SPIDER Command Protocol 13 * by Chandru M. Sippy 14 * SCSI Storage Products (MCP) 15 * Western Digital Corporation 16 * 09-15-95 17 * 18 * http://web.archive.org/web/20070717175254/http://sun1.rrzn.uni-hannover.de/gaertner.juergen/wd719x/Linux/Docu/Spider/ 19 */ 20 21 /* 22 * Driver workflow: 23 * 1. SCSI command is transformed to SCB (Spider Control Block) by the 24 * queuecommand function. 25 * 2. The address of the SCB is stored in a list to be able to access it, if 26 * something goes wrong. 27 * 3. The address of the SCB is written to the Controller, which loads the SCB 28 * via BM-DMA and processes it. 29 * 4. After it has finished, it generates an interrupt, and sets registers. 30 * 31 * flaws: 32 * - abort/reset functions 33 * 34 * ToDo: 35 * - tagged queueing 36 */ 37 38 #include <linux/interrupt.h> 39 #include <linux/module.h> 40 #include <linux/delay.h> 41 #include <linux/pci.h> 42 #include <linux/firmware.h> 43 #include <linux/eeprom_93cx6.h> 44 #include <scsi/scsi_cmnd.h> 45 #include <scsi/scsi_device.h> 46 #include <scsi/scsi_host.h> 47 #include "wd719x.h" 48 49 /* low-level register access */ 50 static inline u8 wd719x_readb(struct wd719x *wd, u8 reg) 51 { 52 return ioread8(wd->base + reg); 53 } 54 55 static inline u32 wd719x_readl(struct wd719x *wd, u8 reg) 56 { 57 return ioread32(wd->base + reg); 58 } 59 60 static inline void wd719x_writeb(struct wd719x *wd, u8 reg, u8 val) 61 { 62 iowrite8(val, wd->base + reg); 63 } 64 65 static inline void wd719x_writew(struct wd719x *wd, u8 reg, u16 val) 66 { 67 iowrite16(val, wd->base + reg); 68 } 69 70 static inline void wd719x_writel(struct wd719x *wd, u8 reg, u32 val) 71 { 72 iowrite32(val, wd->base + reg); 73 } 74 75 /* wait until the command register is ready */ 76 static inline int wd719x_wait_ready(struct wd719x *wd) 77 { 78 int i = 0; 79 80 do { 81 if (wd719x_readb(wd, WD719X_AMR_COMMAND) == WD719X_CMD_READY) 82 return 0; 83 udelay(1); 84 } while (i++ < WD719X_WAIT_FOR_CMD_READY); 85 86 dev_err(&wd->pdev->dev, "command register is not ready: 0x%02x\n", 87 wd719x_readb(wd, WD719X_AMR_COMMAND)); 88 89 return -ETIMEDOUT; 90 } 91 92 /* poll interrupt status register until command finishes */ 93 static inline int wd719x_wait_done(struct wd719x *wd, int timeout) 94 { 95 u8 status; 96 97 while (timeout > 0) { 98 status = wd719x_readb(wd, WD719X_AMR_INT_STATUS); 99 if (status) 100 break; 101 timeout--; 102 udelay(1); 103 } 104 105 if (timeout <= 0) { 106 dev_err(&wd->pdev->dev, "direct command timed out\n"); 107 return -ETIMEDOUT; 108 } 109 110 if (status != WD719X_INT_NOERRORS) { 111 dev_err(&wd->pdev->dev, "direct command failed, status 0x%02x, SUE 0x%02x\n", 112 status, wd719x_readb(wd, WD719X_AMR_SCB_ERROR)); 113 return -EIO; 114 } 115 116 return 0; 117 } 118 119 static int wd719x_direct_cmd(struct wd719x *wd, u8 opcode, u8 dev, u8 lun, 120 u8 tag, dma_addr_t data, int timeout) 121 { 122 int ret = 0; 123 124 /* clear interrupt status register (allow command register to clear) */ 125 wd719x_writeb(wd, WD719X_AMR_INT_STATUS, WD719X_INT_NONE); 126 127 /* Wait for the Command register to become free */ 128 if (wd719x_wait_ready(wd)) 129 return -ETIMEDOUT; 130 131 /* make sure we get NO interrupts */ 132 dev |= WD719X_DISABLE_INT; 133 wd719x_writeb(wd, WD719X_AMR_CMD_PARAM, dev); 134 wd719x_writeb(wd, WD719X_AMR_CMD_PARAM_2, lun); 135 wd719x_writeb(wd, WD719X_AMR_CMD_PARAM_3, tag); 136 if (data) 137 wd719x_writel(wd, WD719X_AMR_SCB_IN, data); 138 139 /* clear interrupt status register again */ 140 wd719x_writeb(wd, WD719X_AMR_INT_STATUS, WD719X_INT_NONE); 141 142 /* Now, write the command */ 143 wd719x_writeb(wd, WD719X_AMR_COMMAND, opcode); 144 145 if (timeout) /* wait for the command to complete */ 146 ret = wd719x_wait_done(wd, timeout); 147 148 /* clear interrupt status register (clean up) */ 149 if (opcode != WD719X_CMD_READ_FIRMVER) 150 wd719x_writeb(wd, WD719X_AMR_INT_STATUS, WD719X_INT_NONE); 151 152 return ret; 153 } 154 155 static void wd719x_destroy(struct wd719x *wd) 156 { 157 /* stop the RISC */ 158 if (wd719x_direct_cmd(wd, WD719X_CMD_SLEEP, 0, 0, 0, 0, 159 WD719X_WAIT_FOR_RISC)) 160 dev_warn(&wd->pdev->dev, "RISC sleep command failed\n"); 161 /* disable RISC */ 162 wd719x_writeb(wd, WD719X_PCI_MODE_SELECT, 0); 163 164 WARN_ON_ONCE(!list_empty(&wd->active_scbs)); 165 166 /* free internal buffers */ 167 dma_free_coherent(&wd->pdev->dev, wd->fw_size, wd->fw_virt, 168 wd->fw_phys); 169 wd->fw_virt = NULL; 170 dma_free_coherent(&wd->pdev->dev, WD719X_HASH_TABLE_SIZE, wd->hash_virt, 171 wd->hash_phys); 172 wd->hash_virt = NULL; 173 dma_free_coherent(&wd->pdev->dev, sizeof(struct wd719x_host_param), 174 wd->params, wd->params_phys); 175 wd->params = NULL; 176 free_irq(wd->pdev->irq, wd); 177 } 178 179 /* finish a SCSI command, unmap buffers */ 180 static void wd719x_finish_cmd(struct wd719x_scb *scb, int result) 181 { 182 struct scsi_cmnd *cmd = scb->cmd; 183 struct wd719x *wd = shost_priv(cmd->device->host); 184 185 list_del(&scb->list); 186 187 dma_unmap_single(&wd->pdev->dev, scb->phys, 188 sizeof(struct wd719x_scb), DMA_BIDIRECTIONAL); 189 scsi_dma_unmap(cmd); 190 dma_unmap_single(&wd->pdev->dev, cmd->SCp.dma_handle, 191 SCSI_SENSE_BUFFERSIZE, DMA_FROM_DEVICE); 192 193 cmd->result = result << 16; 194 cmd->scsi_done(cmd); 195 } 196 197 /* Build a SCB and send it to the card */ 198 static int wd719x_queuecommand(struct Scsi_Host *sh, struct scsi_cmnd *cmd) 199 { 200 int i, count_sg; 201 unsigned long flags; 202 struct wd719x_scb *scb = scsi_cmd_priv(cmd); 203 struct wd719x *wd = shost_priv(sh); 204 205 scb->cmd = cmd; 206 207 scb->CDB_tag = 0; /* Tagged queueing not supported yet */ 208 scb->devid = cmd->device->id; 209 scb->lun = cmd->device->lun; 210 211 /* copy the command */ 212 memcpy(scb->CDB, cmd->cmnd, cmd->cmd_len); 213 214 /* map SCB */ 215 scb->phys = dma_map_single(&wd->pdev->dev, scb, sizeof(*scb), 216 DMA_BIDIRECTIONAL); 217 218 if (dma_mapping_error(&wd->pdev->dev, scb->phys)) 219 goto out_error; 220 221 /* map sense buffer */ 222 scb->sense_buf_length = SCSI_SENSE_BUFFERSIZE; 223 cmd->SCp.dma_handle = dma_map_single(&wd->pdev->dev, cmd->sense_buffer, 224 SCSI_SENSE_BUFFERSIZE, DMA_FROM_DEVICE); 225 if (dma_mapping_error(&wd->pdev->dev, cmd->SCp.dma_handle)) 226 goto out_unmap_scb; 227 scb->sense_buf = cpu_to_le32(cmd->SCp.dma_handle); 228 229 /* request autosense */ 230 scb->SCB_options |= WD719X_SCB_FLAGS_AUTO_REQUEST_SENSE; 231 232 /* check direction */ 233 if (cmd->sc_data_direction == DMA_TO_DEVICE) 234 scb->SCB_options |= WD719X_SCB_FLAGS_CHECK_DIRECTION 235 | WD719X_SCB_FLAGS_PCI_TO_SCSI; 236 else if (cmd->sc_data_direction == DMA_FROM_DEVICE) 237 scb->SCB_options |= WD719X_SCB_FLAGS_CHECK_DIRECTION; 238 239 /* Scather/gather */ 240 count_sg = scsi_dma_map(cmd); 241 if (count_sg < 0) 242 goto out_unmap_sense; 243 BUG_ON(count_sg > WD719X_SG); 244 245 if (count_sg) { 246 struct scatterlist *sg; 247 248 scb->data_length = cpu_to_le32(count_sg * 249 sizeof(struct wd719x_sglist)); 250 scb->data_p = cpu_to_le32(scb->phys + 251 offsetof(struct wd719x_scb, sg_list)); 252 253 scsi_for_each_sg(cmd, sg, count_sg, i) { 254 scb->sg_list[i].ptr = cpu_to_le32(sg_dma_address(sg)); 255 scb->sg_list[i].length = cpu_to_le32(sg_dma_len(sg)); 256 } 257 scb->SCB_options |= WD719X_SCB_FLAGS_DO_SCATTER_GATHER; 258 } else { /* zero length */ 259 scb->data_length = 0; 260 scb->data_p = 0; 261 } 262 263 spin_lock_irqsave(wd->sh->host_lock, flags); 264 265 /* check if the Command register is free */ 266 if (wd719x_readb(wd, WD719X_AMR_COMMAND) != WD719X_CMD_READY) { 267 spin_unlock_irqrestore(wd->sh->host_lock, flags); 268 return SCSI_MLQUEUE_HOST_BUSY; 269 } 270 271 list_add(&scb->list, &wd->active_scbs); 272 273 /* write pointer to the AMR */ 274 wd719x_writel(wd, WD719X_AMR_SCB_IN, scb->phys); 275 /* send SCB opcode */ 276 wd719x_writeb(wd, WD719X_AMR_COMMAND, WD719X_CMD_PROCESS_SCB); 277 278 spin_unlock_irqrestore(wd->sh->host_lock, flags); 279 return 0; 280 281 out_unmap_sense: 282 dma_unmap_single(&wd->pdev->dev, cmd->SCp.dma_handle, 283 SCSI_SENSE_BUFFERSIZE, DMA_FROM_DEVICE); 284 out_unmap_scb: 285 dma_unmap_single(&wd->pdev->dev, scb->phys, sizeof(*scb), 286 DMA_BIDIRECTIONAL); 287 out_error: 288 cmd->result = DID_ERROR << 16; 289 cmd->scsi_done(cmd); 290 return 0; 291 } 292 293 static int wd719x_chip_init(struct wd719x *wd) 294 { 295 int i, ret; 296 u32 risc_init[3]; 297 const struct firmware *fw_wcs, *fw_risc; 298 const char fwname_wcs[] = "wd719x-wcs.bin"; 299 const char fwname_risc[] = "wd719x-risc.bin"; 300 301 memset(wd->hash_virt, 0, WD719X_HASH_TABLE_SIZE); 302 303 /* WCS (sequencer) firmware */ 304 ret = request_firmware(&fw_wcs, fwname_wcs, &wd->pdev->dev); 305 if (ret) { 306 dev_err(&wd->pdev->dev, "Unable to load firmware %s: %d\n", 307 fwname_wcs, ret); 308 return ret; 309 } 310 /* RISC firmware */ 311 ret = request_firmware(&fw_risc, fwname_risc, &wd->pdev->dev); 312 if (ret) { 313 dev_err(&wd->pdev->dev, "Unable to load firmware %s: %d\n", 314 fwname_risc, ret); 315 release_firmware(fw_wcs); 316 return ret; 317 } 318 wd->fw_size = ALIGN(fw_wcs->size, 4) + fw_risc->size; 319 320 if (!wd->fw_virt) 321 wd->fw_virt = dma_alloc_coherent(&wd->pdev->dev, wd->fw_size, 322 &wd->fw_phys, GFP_KERNEL); 323 if (!wd->fw_virt) { 324 ret = -ENOMEM; 325 goto wd719x_init_end; 326 } 327 328 /* make a fresh copy of WCS and RISC code */ 329 memcpy(wd->fw_virt, fw_wcs->data, fw_wcs->size); 330 memcpy(wd->fw_virt + ALIGN(fw_wcs->size, 4), fw_risc->data, 331 fw_risc->size); 332 333 /* Reset the Spider Chip and adapter itself */ 334 wd719x_writeb(wd, WD719X_PCI_PORT_RESET, WD719X_PCI_RESET); 335 udelay(WD719X_WAIT_FOR_RISC); 336 /* Clear PIO mode bits set by BIOS */ 337 wd719x_writeb(wd, WD719X_AMR_CMD_PARAM, 0); 338 /* ensure RISC is not running */ 339 wd719x_writeb(wd, WD719X_PCI_MODE_SELECT, 0); 340 /* ensure command port is ready */ 341 wd719x_writeb(wd, WD719X_AMR_COMMAND, 0); 342 if (wd719x_wait_ready(wd)) { 343 ret = -ETIMEDOUT; 344 goto wd719x_init_end; 345 } 346 347 /* Transfer the first 2K words of RISC code to kick start the uP */ 348 risc_init[0] = wd->fw_phys; /* WCS FW */ 349 risc_init[1] = wd->fw_phys + ALIGN(fw_wcs->size, 4); /* RISC FW */ 350 risc_init[2] = wd->hash_phys; /* hash table */ 351 352 /* clear DMA status */ 353 wd719x_writeb(wd, WD719X_PCI_CHANNEL2_3STATUS, 0); 354 355 /* address to read firmware from */ 356 wd719x_writel(wd, WD719X_PCI_EXTERNAL_ADDR, risc_init[1]); 357 /* base address to write firmware to (on card) */ 358 wd719x_writew(wd, WD719X_PCI_INTERNAL_ADDR, WD719X_PRAM_BASE_ADDR); 359 /* size: first 2K words */ 360 wd719x_writew(wd, WD719X_PCI_DMA_TRANSFER_SIZE, 2048 * 2); 361 /* start DMA */ 362 wd719x_writeb(wd, WD719X_PCI_CHANNEL2_3CMD, WD719X_START_CHANNEL2_3DMA); 363 364 /* wait for DMA to complete */ 365 i = WD719X_WAIT_FOR_RISC; 366 while (i-- > 0) { 367 u8 status = wd719x_readb(wd, WD719X_PCI_CHANNEL2_3STATUS); 368 if (status == WD719X_START_CHANNEL2_3DONE) 369 break; 370 if (status == WD719X_START_CHANNEL2_3ABORT) { 371 dev_warn(&wd->pdev->dev, "RISC bootstrap failed: DMA aborted\n"); 372 ret = -EIO; 373 goto wd719x_init_end; 374 } 375 udelay(1); 376 } 377 if (i < 1) { 378 dev_warn(&wd->pdev->dev, "RISC bootstrap failed: DMA timeout\n"); 379 ret = -ETIMEDOUT; 380 goto wd719x_init_end; 381 } 382 383 /* firmware is loaded, now initialize and wake up the RISC */ 384 /* write RISC initialization long words to Spider */ 385 wd719x_writel(wd, WD719X_AMR_SCB_IN, risc_init[0]); 386 wd719x_writel(wd, WD719X_AMR_SCB_IN + 4, risc_init[1]); 387 wd719x_writel(wd, WD719X_AMR_SCB_IN + 8, risc_init[2]); 388 389 /* disable interrupts during initialization of RISC */ 390 wd719x_writeb(wd, WD719X_AMR_CMD_PARAM, WD719X_DISABLE_INT); 391 392 /* issue INITIALIZE RISC comand */ 393 wd719x_writeb(wd, WD719X_AMR_COMMAND, WD719X_CMD_INIT_RISC); 394 /* enable advanced mode (wake up RISC) */ 395 wd719x_writeb(wd, WD719X_PCI_MODE_SELECT, WD719X_ENABLE_ADVANCE_MODE); 396 udelay(WD719X_WAIT_FOR_RISC); 397 398 ret = wd719x_wait_done(wd, WD719X_WAIT_FOR_RISC); 399 /* clear interrupt status register */ 400 wd719x_writeb(wd, WD719X_AMR_INT_STATUS, WD719X_INT_NONE); 401 if (ret) { 402 dev_warn(&wd->pdev->dev, "Unable to initialize RISC\n"); 403 goto wd719x_init_end; 404 } 405 /* RISC is up and running */ 406 407 /* Read FW version from RISC */ 408 ret = wd719x_direct_cmd(wd, WD719X_CMD_READ_FIRMVER, 0, 0, 0, 0, 409 WD719X_WAIT_FOR_RISC); 410 if (ret) { 411 dev_warn(&wd->pdev->dev, "Unable to read firmware version\n"); 412 goto wd719x_init_end; 413 } 414 dev_info(&wd->pdev->dev, "RISC initialized with firmware version %.2x.%.2x\n", 415 wd719x_readb(wd, WD719X_AMR_SCB_OUT + 1), 416 wd719x_readb(wd, WD719X_AMR_SCB_OUT)); 417 418 /* RESET SCSI bus */ 419 ret = wd719x_direct_cmd(wd, WD719X_CMD_BUSRESET, 0, 0, 0, 0, 420 WD719X_WAIT_FOR_SCSI_RESET); 421 if (ret) { 422 dev_warn(&wd->pdev->dev, "SCSI bus reset failed\n"); 423 goto wd719x_init_end; 424 } 425 426 /* use HostParameter structure to set Spider's Host Parameter Block */ 427 ret = wd719x_direct_cmd(wd, WD719X_CMD_SET_PARAM, 0, 428 sizeof(struct wd719x_host_param), 0, 429 wd->params_phys, WD719X_WAIT_FOR_RISC); 430 if (ret) { 431 dev_warn(&wd->pdev->dev, "Failed to set HOST PARAMETERS\n"); 432 goto wd719x_init_end; 433 } 434 435 /* initiate SCAM (does nothing if disabled in BIOS) */ 436 /* bug?: we should pass a mask of static IDs which we don't have */ 437 ret = wd719x_direct_cmd(wd, WD719X_CMD_INIT_SCAM, 0, 0, 0, 0, 438 WD719X_WAIT_FOR_SCSI_RESET); 439 if (ret) { 440 dev_warn(&wd->pdev->dev, "SCAM initialization failed\n"); 441 goto wd719x_init_end; 442 } 443 444 /* clear AMR_BIOS_SHARE_INT register */ 445 wd719x_writeb(wd, WD719X_AMR_BIOS_SHARE_INT, 0); 446 447 wd719x_init_end: 448 release_firmware(fw_wcs); 449 release_firmware(fw_risc); 450 451 return ret; 452 } 453 454 static int wd719x_abort(struct scsi_cmnd *cmd) 455 { 456 int action, result; 457 unsigned long flags; 458 struct wd719x_scb *scb = scsi_cmd_priv(cmd); 459 struct wd719x *wd = shost_priv(cmd->device->host); 460 461 dev_info(&wd->pdev->dev, "abort command, tag: %x\n", cmd->tag); 462 463 action = /*cmd->tag ? WD719X_CMD_ABORT_TAG : */WD719X_CMD_ABORT; 464 465 spin_lock_irqsave(wd->sh->host_lock, flags); 466 result = wd719x_direct_cmd(wd, action, cmd->device->id, 467 cmd->device->lun, cmd->tag, scb->phys, 0); 468 spin_unlock_irqrestore(wd->sh->host_lock, flags); 469 if (result) 470 return FAILED; 471 472 return SUCCESS; 473 } 474 475 static int wd719x_reset(struct scsi_cmnd *cmd, u8 opcode, u8 device) 476 { 477 int result; 478 unsigned long flags; 479 struct wd719x *wd = shost_priv(cmd->device->host); 480 481 dev_info(&wd->pdev->dev, "%s reset requested\n", 482 (opcode == WD719X_CMD_BUSRESET) ? "bus" : "device"); 483 484 spin_lock_irqsave(wd->sh->host_lock, flags); 485 result = wd719x_direct_cmd(wd, opcode, device, 0, 0, 0, 486 WD719X_WAIT_FOR_SCSI_RESET); 487 spin_unlock_irqrestore(wd->sh->host_lock, flags); 488 if (result) 489 return FAILED; 490 491 return SUCCESS; 492 } 493 494 static int wd719x_dev_reset(struct scsi_cmnd *cmd) 495 { 496 return wd719x_reset(cmd, WD719X_CMD_RESET, cmd->device->id); 497 } 498 499 static int wd719x_bus_reset(struct scsi_cmnd *cmd) 500 { 501 return wd719x_reset(cmd, WD719X_CMD_BUSRESET, 0); 502 } 503 504 static int wd719x_host_reset(struct scsi_cmnd *cmd) 505 { 506 struct wd719x *wd = shost_priv(cmd->device->host); 507 struct wd719x_scb *scb, *tmp; 508 unsigned long flags; 509 int result; 510 511 dev_info(&wd->pdev->dev, "host reset requested\n"); 512 spin_lock_irqsave(wd->sh->host_lock, flags); 513 /* Try to reinit the RISC */ 514 if (wd719x_chip_init(wd) == 0) 515 result = SUCCESS; 516 else 517 result = FAILED; 518 519 /* flush all SCBs */ 520 list_for_each_entry_safe(scb, tmp, &wd->active_scbs, list) 521 wd719x_finish_cmd(scb, result); 522 spin_unlock_irqrestore(wd->sh->host_lock, flags); 523 524 return result; 525 } 526 527 static int wd719x_biosparam(struct scsi_device *sdev, struct block_device *bdev, 528 sector_t capacity, int geom[]) 529 { 530 if (capacity >= 0x200000) { 531 geom[0] = 255; /* heads */ 532 geom[1] = 63; /* sectors */ 533 } else { 534 geom[0] = 64; /* heads */ 535 geom[1] = 32; /* sectors */ 536 } 537 geom[2] = sector_div(capacity, geom[0] * geom[1]); /* cylinders */ 538 539 return 0; 540 } 541 542 /* process a SCB-completion interrupt */ 543 static inline void wd719x_interrupt_SCB(struct wd719x *wd, 544 union wd719x_regs regs, 545 struct wd719x_scb *scb) 546 { 547 int result; 548 549 /* now have to find result from card */ 550 switch (regs.bytes.SUE) { 551 case WD719X_SUE_NOERRORS: 552 result = DID_OK; 553 break; 554 case WD719X_SUE_REJECTED: 555 dev_err(&wd->pdev->dev, "command rejected\n"); 556 result = DID_ERROR; 557 break; 558 case WD719X_SUE_SCBQFULL: 559 dev_err(&wd->pdev->dev, "SCB queue is full\n"); 560 result = DID_ERROR; 561 break; 562 case WD719X_SUE_TERM: 563 dev_dbg(&wd->pdev->dev, "SCB terminated by direct command\n"); 564 result = DID_ABORT; /* or DID_RESET? */ 565 break; 566 case WD719X_SUE_CHAN1ABORT: 567 case WD719X_SUE_CHAN23ABORT: 568 result = DID_ABORT; 569 dev_err(&wd->pdev->dev, "DMA abort\n"); 570 break; 571 case WD719X_SUE_CHAN1PAR: 572 case WD719X_SUE_CHAN23PAR: 573 result = DID_PARITY; 574 dev_err(&wd->pdev->dev, "DMA parity error\n"); 575 break; 576 case WD719X_SUE_TIMEOUT: 577 result = DID_TIME_OUT; 578 dev_dbg(&wd->pdev->dev, "selection timeout\n"); 579 break; 580 case WD719X_SUE_RESET: 581 dev_dbg(&wd->pdev->dev, "bus reset occurred\n"); 582 result = DID_RESET; 583 break; 584 case WD719X_SUE_BUSERROR: 585 dev_dbg(&wd->pdev->dev, "SCSI bus error\n"); 586 result = DID_ERROR; 587 break; 588 case WD719X_SUE_WRONGWAY: 589 dev_err(&wd->pdev->dev, "wrong data transfer direction\n"); 590 result = DID_ERROR; 591 break; 592 case WD719X_SUE_BADPHASE: 593 dev_err(&wd->pdev->dev, "invalid SCSI phase\n"); 594 result = DID_ERROR; 595 break; 596 case WD719X_SUE_TOOLONG: 597 dev_err(&wd->pdev->dev, "record too long\n"); 598 result = DID_ERROR; 599 break; 600 case WD719X_SUE_BUSFREE: 601 dev_err(&wd->pdev->dev, "unexpected bus free\n"); 602 result = DID_NO_CONNECT; /* or DID_ERROR ???*/ 603 break; 604 case WD719X_SUE_ARSDONE: 605 dev_dbg(&wd->pdev->dev, "auto request sense\n"); 606 if (regs.bytes.SCSI == 0) 607 result = DID_OK; 608 else 609 result = DID_PARITY; 610 break; 611 case WD719X_SUE_IGNORED: 612 dev_err(&wd->pdev->dev, "target id %d ignored command\n", 613 scb->cmd->device->id); 614 result = DID_NO_CONNECT; 615 break; 616 case WD719X_SUE_WRONGTAGS: 617 dev_err(&wd->pdev->dev, "reversed tags\n"); 618 result = DID_ERROR; 619 break; 620 case WD719X_SUE_BADTAGS: 621 dev_err(&wd->pdev->dev, "tag type not supported by target\n"); 622 result = DID_ERROR; 623 break; 624 case WD719X_SUE_NOSCAMID: 625 dev_err(&wd->pdev->dev, "no SCAM soft ID available\n"); 626 result = DID_ERROR; 627 break; 628 default: 629 dev_warn(&wd->pdev->dev, "unknown SUE error code: 0x%x\n", 630 regs.bytes.SUE); 631 result = DID_ERROR; 632 break; 633 } 634 635 wd719x_finish_cmd(scb, result); 636 } 637 638 static irqreturn_t wd719x_interrupt(int irq, void *dev_id) 639 { 640 struct wd719x *wd = dev_id; 641 union wd719x_regs regs; 642 unsigned long flags; 643 u32 SCB_out; 644 645 spin_lock_irqsave(wd->sh->host_lock, flags); 646 /* read SCB pointer back from card */ 647 SCB_out = wd719x_readl(wd, WD719X_AMR_SCB_OUT); 648 /* read all status info at once */ 649 regs.all = cpu_to_le32(wd719x_readl(wd, WD719X_AMR_OP_CODE)); 650 651 switch (regs.bytes.INT) { 652 case WD719X_INT_NONE: 653 spin_unlock_irqrestore(wd->sh->host_lock, flags); 654 return IRQ_NONE; 655 case WD719X_INT_LINKNOSTATUS: 656 dev_err(&wd->pdev->dev, "linked command completed with no status\n"); 657 break; 658 case WD719X_INT_BADINT: 659 dev_err(&wd->pdev->dev, "unsolicited interrupt\n"); 660 break; 661 case WD719X_INT_NOERRORS: 662 case WD719X_INT_LINKNOERRORS: 663 case WD719X_INT_ERRORSLOGGED: 664 case WD719X_INT_SPIDERFAILED: 665 /* was the cmd completed a direct or SCB command? */ 666 if (regs.bytes.OPC == WD719X_CMD_PROCESS_SCB) { 667 struct wd719x_scb *scb; 668 list_for_each_entry(scb, &wd->active_scbs, list) 669 if (SCB_out == scb->phys) 670 break; 671 if (SCB_out == scb->phys) 672 wd719x_interrupt_SCB(wd, regs, scb); 673 else 674 dev_err(&wd->pdev->dev, "card returned invalid SCB pointer\n"); 675 } else 676 dev_warn(&wd->pdev->dev, "direct command 0x%x completed\n", 677 regs.bytes.OPC); 678 break; 679 case WD719X_INT_PIOREADY: 680 dev_err(&wd->pdev->dev, "card indicates PIO data ready but we never use PIO\n"); 681 /* interrupt will not be cleared until all data is read */ 682 break; 683 default: 684 dev_err(&wd->pdev->dev, "unknown interrupt reason: %d\n", 685 regs.bytes.INT); 686 687 } 688 /* clear interrupt so another can happen */ 689 wd719x_writeb(wd, WD719X_AMR_INT_STATUS, WD719X_INT_NONE); 690 spin_unlock_irqrestore(wd->sh->host_lock, flags); 691 692 return IRQ_HANDLED; 693 } 694 695 static void wd719x_eeprom_reg_read(struct eeprom_93cx6 *eeprom) 696 { 697 struct wd719x *wd = eeprom->data; 698 u8 reg = wd719x_readb(wd, WD719X_PCI_GPIO_DATA); 699 700 eeprom->reg_data_out = reg & WD719X_EE_DO; 701 } 702 703 static void wd719x_eeprom_reg_write(struct eeprom_93cx6 *eeprom) 704 { 705 struct wd719x *wd = eeprom->data; 706 u8 reg = 0; 707 708 if (eeprom->reg_data_in) 709 reg |= WD719X_EE_DI; 710 if (eeprom->reg_data_clock) 711 reg |= WD719X_EE_CLK; 712 if (eeprom->reg_chip_select) 713 reg |= WD719X_EE_CS; 714 715 wd719x_writeb(wd, WD719X_PCI_GPIO_DATA, reg); 716 } 717 718 /* read config from EEPROM so it can be downloaded by the RISC on (re-)init */ 719 static void wd719x_read_eeprom(struct wd719x *wd) 720 { 721 struct eeprom_93cx6 eeprom; 722 u8 gpio; 723 struct wd719x_eeprom_header header; 724 725 eeprom.data = wd; 726 eeprom.register_read = wd719x_eeprom_reg_read; 727 eeprom.register_write = wd719x_eeprom_reg_write; 728 eeprom.width = PCI_EEPROM_WIDTH_93C46; 729 730 /* set all outputs to low */ 731 wd719x_writeb(wd, WD719X_PCI_GPIO_DATA, 0); 732 /* configure GPIO pins */ 733 gpio = wd719x_readb(wd, WD719X_PCI_GPIO_CONTROL); 734 /* GPIO outputs */ 735 gpio &= (~(WD719X_EE_CLK | WD719X_EE_DI | WD719X_EE_CS)); 736 /* GPIO input */ 737 gpio |= WD719X_EE_DO; 738 wd719x_writeb(wd, WD719X_PCI_GPIO_CONTROL, gpio); 739 740 /* read EEPROM header */ 741 eeprom_93cx6_multireadb(&eeprom, 0, (u8 *)&header, sizeof(header)); 742 743 if (header.sig1 == 'W' && header.sig2 == 'D') 744 eeprom_93cx6_multireadb(&eeprom, header.cfg_offset, 745 (u8 *)wd->params, 746 sizeof(struct wd719x_host_param)); 747 else { /* default EEPROM values */ 748 dev_warn(&wd->pdev->dev, "EEPROM signature is invalid (0x%02x 0x%02x), using default values\n", 749 header.sig1, header.sig2); 750 wd->params->ch_1_th = 0x10; /* 16 DWs = 64 B */ 751 wd->params->scsi_conf = 0x4c; /* 48ma, spue, parity check */ 752 wd->params->own_scsi_id = 0x07; /* ID 7, SCAM disabled */ 753 wd->params->sel_timeout = 0x4d; /* 250 ms */ 754 wd->params->sleep_timer = 0x01; 755 wd->params->cdb_size = cpu_to_le16(0x5555); /* all 6 B */ 756 wd->params->scsi_pad = 0x1b; 757 if (wd->type == WD719X_TYPE_7193) /* narrow card - disable */ 758 wd->params->wide = cpu_to_le32(0x00000000); 759 else /* initiate & respond to WIDE messages */ 760 wd->params->wide = cpu_to_le32(0xffffffff); 761 wd->params->sync = cpu_to_le32(0xffffffff); 762 wd->params->soft_mask = 0x00; /* all disabled */ 763 wd->params->unsol_mask = 0x00; /* all disabled */ 764 } 765 /* disable TAGGED messages */ 766 wd->params->tag_en = cpu_to_le16(0x0000); 767 } 768 769 /* Read card type from GPIO bits 1 and 3 */ 770 static enum wd719x_card_type wd719x_detect_type(struct wd719x *wd) 771 { 772 u8 card = wd719x_readb(wd, WD719X_PCI_GPIO_CONTROL); 773 774 card |= WD719X_GPIO_ID_BITS; 775 wd719x_writeb(wd, WD719X_PCI_GPIO_CONTROL, card); 776 card = wd719x_readb(wd, WD719X_PCI_GPIO_DATA) & WD719X_GPIO_ID_BITS; 777 switch (card) { 778 case 0x08: 779 return WD719X_TYPE_7193; 780 case 0x02: 781 return WD719X_TYPE_7197; 782 case 0x00: 783 return WD719X_TYPE_7296; 784 default: 785 dev_warn(&wd->pdev->dev, "unknown card type 0x%x\n", card); 786 return WD719X_TYPE_UNKNOWN; 787 } 788 } 789 790 static int wd719x_board_found(struct Scsi_Host *sh) 791 { 792 struct wd719x *wd = shost_priv(sh); 793 static const char * const card_types[] = { 794 "Unknown card", "WD7193", "WD7197", "WD7296" 795 }; 796 int ret; 797 798 INIT_LIST_HEAD(&wd->active_scbs); 799 800 sh->base = pci_resource_start(wd->pdev, 0); 801 802 wd->type = wd719x_detect_type(wd); 803 804 wd->sh = sh; 805 sh->irq = wd->pdev->irq; 806 wd->fw_virt = NULL; 807 808 /* memory area for host (EEPROM) parameters */ 809 wd->params = dma_alloc_coherent(&wd->pdev->dev, 810 sizeof(struct wd719x_host_param), 811 &wd->params_phys, GFP_KERNEL); 812 if (!wd->params) { 813 dev_warn(&wd->pdev->dev, "unable to allocate parameter buffer\n"); 814 return -ENOMEM; 815 } 816 817 /* memory area for the RISC for hash table of outstanding requests */ 818 wd->hash_virt = dma_alloc_coherent(&wd->pdev->dev, 819 WD719X_HASH_TABLE_SIZE, 820 &wd->hash_phys, GFP_KERNEL); 821 if (!wd->hash_virt) { 822 dev_warn(&wd->pdev->dev, "unable to allocate hash buffer\n"); 823 ret = -ENOMEM; 824 goto fail_free_params; 825 } 826 827 ret = request_irq(wd->pdev->irq, wd719x_interrupt, IRQF_SHARED, 828 "wd719x", wd); 829 if (ret) { 830 dev_warn(&wd->pdev->dev, "unable to assign IRQ %d\n", 831 wd->pdev->irq); 832 goto fail_free_hash; 833 } 834 835 /* read parameters from EEPROM */ 836 wd719x_read_eeprom(wd); 837 838 ret = wd719x_chip_init(wd); 839 if (ret) 840 goto fail_free_irq; 841 842 sh->this_id = wd->params->own_scsi_id & WD719X_EE_SCSI_ID_MASK; 843 844 dev_info(&wd->pdev->dev, "%s at I/O 0x%lx, IRQ %u, SCSI ID %d\n", 845 card_types[wd->type], sh->base, sh->irq, sh->this_id); 846 847 return 0; 848 849 fail_free_irq: 850 free_irq(wd->pdev->irq, wd); 851 fail_free_hash: 852 dma_free_coherent(&wd->pdev->dev, WD719X_HASH_TABLE_SIZE, wd->hash_virt, 853 wd->hash_phys); 854 fail_free_params: 855 dma_free_coherent(&wd->pdev->dev, sizeof(struct wd719x_host_param), 856 wd->params, wd->params_phys); 857 858 return ret; 859 } 860 861 static struct scsi_host_template wd719x_template = { 862 .module = THIS_MODULE, 863 .name = "Western Digital 719x", 864 .cmd_size = sizeof(struct wd719x_scb), 865 .queuecommand = wd719x_queuecommand, 866 .eh_abort_handler = wd719x_abort, 867 .eh_device_reset_handler = wd719x_dev_reset, 868 .eh_bus_reset_handler = wd719x_bus_reset, 869 .eh_host_reset_handler = wd719x_host_reset, 870 .bios_param = wd719x_biosparam, 871 .proc_name = "wd719x", 872 .can_queue = 255, 873 .this_id = 7, 874 .sg_tablesize = WD719X_SG, 875 }; 876 877 static int wd719x_pci_probe(struct pci_dev *pdev, const struct pci_device_id *d) 878 { 879 int err; 880 struct Scsi_Host *sh; 881 struct wd719x *wd; 882 883 err = pci_enable_device(pdev); 884 if (err) 885 goto fail; 886 887 if (dma_set_mask(&pdev->dev, DMA_BIT_MASK(32))) { 888 dev_warn(&pdev->dev, "Unable to set 32-bit DMA mask\n"); 889 goto disable_device; 890 } 891 892 err = pci_request_regions(pdev, "wd719x"); 893 if (err) 894 goto disable_device; 895 pci_set_master(pdev); 896 897 err = -ENODEV; 898 if (pci_resource_len(pdev, 0) == 0) 899 goto release_region; 900 901 err = -ENOMEM; 902 sh = scsi_host_alloc(&wd719x_template, sizeof(struct wd719x)); 903 if (!sh) 904 goto release_region; 905 906 wd = shost_priv(sh); 907 wd->base = pci_iomap(pdev, 0, 0); 908 if (!wd->base) 909 goto free_host; 910 wd->pdev = pdev; 911 912 err = wd719x_board_found(sh); 913 if (err) 914 goto unmap; 915 916 err = scsi_add_host(sh, &wd->pdev->dev); 917 if (err) 918 goto destroy; 919 920 scsi_scan_host(sh); 921 922 pci_set_drvdata(pdev, sh); 923 return 0; 924 925 destroy: 926 wd719x_destroy(wd); 927 unmap: 928 pci_iounmap(pdev, wd->base); 929 free_host: 930 scsi_host_put(sh); 931 release_region: 932 pci_release_regions(pdev); 933 disable_device: 934 pci_disable_device(pdev); 935 fail: 936 return err; 937 } 938 939 940 static void wd719x_pci_remove(struct pci_dev *pdev) 941 { 942 struct Scsi_Host *sh = pci_get_drvdata(pdev); 943 struct wd719x *wd = shost_priv(sh); 944 945 scsi_remove_host(sh); 946 wd719x_destroy(wd); 947 pci_iounmap(pdev, wd->base); 948 pci_release_regions(pdev); 949 pci_disable_device(pdev); 950 951 scsi_host_put(sh); 952 } 953 954 static const struct pci_device_id wd719x_pci_table[] = { 955 { PCI_DEVICE(PCI_VENDOR_ID_WD, 0x3296) }, 956 {} 957 }; 958 959 MODULE_DEVICE_TABLE(pci, wd719x_pci_table); 960 961 static struct pci_driver wd719x_pci_driver = { 962 .name = "wd719x", 963 .id_table = wd719x_pci_table, 964 .probe = wd719x_pci_probe, 965 .remove = wd719x_pci_remove, 966 }; 967 968 module_pci_driver(wd719x_pci_driver); 969 970 MODULE_DESCRIPTION("Western Digital WD7193/7197/7296 SCSI driver"); 971 MODULE_AUTHOR("Ondrej Zary, Aaron Dewell, Juergen Gaertner"); 972 MODULE_LICENSE("GPL"); 973 MODULE_FIRMWARE("wd719x-wcs.bin"); 974 MODULE_FIRMWARE("wd719x-risc.bin"); 975