1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Driver for Future Domain TMC-16x0 and TMC-3260 SCSI host adapters 4 * Copyright 2019 Ondrej Zary 5 * 6 * Original driver by 7 * Rickard E. Faith, faith@cs.unc.edu 8 * 9 * Future Domain BIOS versions supported for autodetect: 10 * 2.0, 3.0, 3.2, 3.4 (1.0), 3.5 (2.0), 3.6, 3.61 11 * Chips supported: 12 * TMC-1800, TMC-18C50, TMC-18C30, TMC-36C70 13 * Boards supported: 14 * Future Domain TMC-1650, TMC-1660, TMC-1670, TMC-1680, TMC-1610M/MER/MEX 15 * Future Domain TMC-3260 (PCI) 16 * Quantum ISA-200S, ISA-250MG 17 * Adaptec AHA-2920A (PCI) [BUT *NOT* AHA-2920C -- use aic7xxx instead] 18 * IBM ? 19 * 20 * NOTE: 21 * 22 * The Adaptec AHA-2920C has an Adaptec AIC-7850 chip on it. 23 * Use the aic7xxx driver for this board. 24 * 25 * The Adaptec AHA-2920A has a Future Domain chip on it, so this is the right 26 * driver for that card. Unfortunately, the boxes will probably just say 27 * "2920", so you'll have to look on the card for a Future Domain logo, or a 28 * letter after the 2920. 29 * 30 * If you have a TMC-8xx or TMC-9xx board, then this is not the driver for 31 * your board. 32 * 33 * DESCRIPTION: 34 * 35 * This is the Linux low-level SCSI driver for Future Domain TMC-1660/1680 36 * TMC-1650/1670, and TMC-3260 SCSI host adapters. The 1650 and 1670 have a 37 * 25-pin external connector, whereas the 1660 and 1680 have a SCSI-2 50-pin 38 * high-density external connector. The 1670 and 1680 have floppy disk 39 * controllers built in. The TMC-3260 is a PCI bus card. 40 * 41 * Future Domain's older boards are based on the TMC-1800 chip, and this 42 * driver was originally written for a TMC-1680 board with the TMC-1800 chip. 43 * More recently, boards are being produced with the TMC-18C50 and TMC-18C30 44 * chips. 45 * 46 * Please note that the drive ordering that Future Domain implemented in BIOS 47 * versions 3.4 and 3.5 is the opposite of the order (currently) used by the 48 * rest of the SCSI industry. 49 * 50 * 51 * REFERENCES USED: 52 * 53 * "TMC-1800 SCSI Chip Specification (FDC-1800T)", Future Domain Corporation, 54 * 1990. 55 * 56 * "Technical Reference Manual: 18C50 SCSI Host Adapter Chip", Future Domain 57 * Corporation, January 1992. 58 * 59 * "LXT SCSI Products: Specifications and OEM Technical Manual (Revision 60 * B/September 1991)", Maxtor Corporation, 1991. 61 * 62 * "7213S product Manual (Revision P3)", Maxtor Corporation, 1992. 63 * 64 * "Draft Proposed American National Standard: Small Computer System 65 * Interface - 2 (SCSI-2)", Global Engineering Documents. (X3T9.2/86-109, 66 * revision 10h, October 17, 1991) 67 * 68 * Private communications, Drew Eckhardt (drew@cs.colorado.edu) and Eric 69 * Youngdale (ericy@cais.com), 1992. 70 * 71 * Private communication, Tuong Le (Future Domain Engineering department), 72 * 1994. (Disk geometry computations for Future Domain BIOS version 3.4, and 73 * TMC-18C30 detection.) 74 * 75 * Hogan, Thom. The Programmer's PC Sourcebook. Microsoft Press, 1988. Page 76 * 60 (2.39: Disk Partition Table Layout). 77 * 78 * "18C30 Technical Reference Manual", Future Domain Corporation, 1993, page 79 * 6-1. 80 */ 81 82 #include <linux/module.h> 83 #include <linux/interrupt.h> 84 #include <linux/delay.h> 85 #include <linux/pci.h> 86 #include <linux/workqueue.h> 87 #include <scsi/scsicam.h> 88 #include <scsi/scsi_cmnd.h> 89 #include <scsi/scsi_device.h> 90 #include <scsi/scsi_host.h> 91 #include "fdomain.h" 92 93 /* 94 * FIFO_COUNT: The host adapter has an 8K cache (host adapters based on the 95 * 18C30 chip have a 2k cache). When this many 512 byte blocks are filled by 96 * the SCSI device, an interrupt will be raised. Therefore, this could be as 97 * low as 0, or as high as 16. Note, however, that values which are too high 98 * or too low seem to prevent any interrupts from occurring, and thereby lock 99 * up the machine. 100 */ 101 #define FIFO_COUNT 2 /* Number of 512 byte blocks before INTR */ 102 #define PARITY_MASK ACTL_PAREN /* Parity enabled, 0 = disabled */ 103 104 enum chip_type { 105 unknown = 0x00, 106 tmc1800 = 0x01, 107 tmc18c50 = 0x02, 108 tmc18c30 = 0x03, 109 }; 110 111 struct fdomain { 112 int base; 113 struct scsi_cmnd *cur_cmd; 114 enum chip_type chip; 115 struct work_struct work; 116 }; 117 118 static inline void fdomain_make_bus_idle(struct fdomain *fd) 119 { 120 outb(0, fd->base + REG_BCTL); 121 outb(0, fd->base + REG_MCTL); 122 if (fd->chip == tmc18c50 || fd->chip == tmc18c30) 123 /* Clear forced intr. */ 124 outb(ACTL_RESET | ACTL_CLRFIRQ | PARITY_MASK, 125 fd->base + REG_ACTL); 126 else 127 outb(ACTL_RESET | PARITY_MASK, fd->base + REG_ACTL); 128 } 129 130 static enum chip_type fdomain_identify(int port) 131 { 132 u16 id = inb(port + REG_ID_LSB) | inb(port + REG_ID_MSB) << 8; 133 134 switch (id) { 135 case 0x6127: 136 return tmc1800; 137 case 0x60e9: /* 18c50 or 18c30 */ 138 break; 139 default: 140 return unknown; 141 } 142 143 /* Try to toggle 32-bit mode. This only works on an 18c30 chip. */ 144 outb(CFG2_32BIT, port + REG_CFG2); 145 if ((inb(port + REG_CFG2) & CFG2_32BIT)) { 146 outb(0, port + REG_CFG2); 147 if ((inb(port + REG_CFG2) & CFG2_32BIT) == 0) 148 return tmc18c30; 149 } 150 /* If that failed, we are an 18c50. */ 151 return tmc18c50; 152 } 153 154 static int fdomain_test_loopback(int base) 155 { 156 int i; 157 158 for (i = 0; i < 255; i++) { 159 outb(i, base + REG_LOOPBACK); 160 if (inb(base + REG_LOOPBACK) != i) 161 return 1; 162 } 163 164 return 0; 165 } 166 167 static void fdomain_reset(int base) 168 { 169 outb(BCTL_RST, base + REG_BCTL); 170 mdelay(20); 171 outb(0, base + REG_BCTL); 172 mdelay(1150); 173 outb(0, base + REG_MCTL); 174 outb(PARITY_MASK, base + REG_ACTL); 175 } 176 177 static int fdomain_select(struct Scsi_Host *sh, int target) 178 { 179 int status; 180 unsigned long timeout; 181 struct fdomain *fd = shost_priv(sh); 182 183 outb(BCTL_BUSEN | BCTL_SEL, fd->base + REG_BCTL); 184 outb(BIT(sh->this_id) | BIT(target), fd->base + REG_SCSI_DATA_NOACK); 185 186 /* Stop arbitration and enable parity */ 187 outb(PARITY_MASK, fd->base + REG_ACTL); 188 189 timeout = 350; /* 350 msec */ 190 191 do { 192 status = inb(fd->base + REG_BSTAT); 193 if (status & BSTAT_BSY) { 194 /* Enable SCSI Bus */ 195 /* (on error, should make bus idle with 0) */ 196 outb(BCTL_BUSEN, fd->base + REG_BCTL); 197 return 0; 198 } 199 mdelay(1); 200 } while (--timeout); 201 fdomain_make_bus_idle(fd); 202 return 1; 203 } 204 205 static void fdomain_finish_cmd(struct fdomain *fd) 206 { 207 outb(0, fd->base + REG_ICTL); 208 fdomain_make_bus_idle(fd); 209 fd->cur_cmd->scsi_done(fd->cur_cmd); 210 fd->cur_cmd = NULL; 211 } 212 213 static void fdomain_read_data(struct scsi_cmnd *cmd) 214 { 215 struct fdomain *fd = shost_priv(cmd->device->host); 216 unsigned char *virt, *ptr; 217 size_t offset, len; 218 219 while ((len = inw(fd->base + REG_FIFO_COUNT)) > 0) { 220 offset = scsi_bufflen(cmd) - scsi_get_resid(cmd); 221 virt = scsi_kmap_atomic_sg(scsi_sglist(cmd), scsi_sg_count(cmd), 222 &offset, &len); 223 ptr = virt + offset; 224 if (len & 1) 225 *ptr++ = inb(fd->base + REG_FIFO); 226 if (len > 1) 227 insw(fd->base + REG_FIFO, ptr, len >> 1); 228 scsi_set_resid(cmd, scsi_get_resid(cmd) - len); 229 scsi_kunmap_atomic_sg(virt); 230 } 231 } 232 233 static void fdomain_write_data(struct scsi_cmnd *cmd) 234 { 235 struct fdomain *fd = shost_priv(cmd->device->host); 236 /* 8k FIFO for pre-tmc18c30 chips, 2k FIFO for tmc18c30 */ 237 int FIFO_Size = fd->chip == tmc18c30 ? 0x800 : 0x2000; 238 unsigned char *virt, *ptr; 239 size_t offset, len; 240 241 while ((len = FIFO_Size - inw(fd->base + REG_FIFO_COUNT)) > 512) { 242 offset = scsi_bufflen(cmd) - scsi_get_resid(cmd); 243 if (len + offset > scsi_bufflen(cmd)) { 244 len = scsi_bufflen(cmd) - offset; 245 if (len == 0) 246 break; 247 } 248 virt = scsi_kmap_atomic_sg(scsi_sglist(cmd), scsi_sg_count(cmd), 249 &offset, &len); 250 ptr = virt + offset; 251 if (len & 1) 252 outb(*ptr++, fd->base + REG_FIFO); 253 if (len > 1) 254 outsw(fd->base + REG_FIFO, ptr, len >> 1); 255 scsi_set_resid(cmd, scsi_get_resid(cmd) - len); 256 scsi_kunmap_atomic_sg(virt); 257 } 258 } 259 260 static void fdomain_work(struct work_struct *work) 261 { 262 struct fdomain *fd = container_of(work, struct fdomain, work); 263 struct Scsi_Host *sh = container_of((void *)fd, struct Scsi_Host, 264 hostdata); 265 struct scsi_cmnd *cmd = fd->cur_cmd; 266 unsigned long flags; 267 int status; 268 int done = 0; 269 270 spin_lock_irqsave(sh->host_lock, flags); 271 272 if (cmd->SCp.phase & in_arbitration) { 273 status = inb(fd->base + REG_ASTAT); 274 if (!(status & ASTAT_ARB)) { 275 set_host_byte(cmd, DID_BUS_BUSY); 276 fdomain_finish_cmd(fd); 277 goto out; 278 } 279 cmd->SCp.phase = in_selection; 280 281 outb(ICTL_SEL | FIFO_COUNT, fd->base + REG_ICTL); 282 outb(BCTL_BUSEN | BCTL_SEL, fd->base + REG_BCTL); 283 outb(BIT(cmd->device->host->this_id) | BIT(scmd_id(cmd)), 284 fd->base + REG_SCSI_DATA_NOACK); 285 /* Stop arbitration and enable parity */ 286 outb(ACTL_IRQEN | PARITY_MASK, fd->base + REG_ACTL); 287 goto out; 288 } else if (cmd->SCp.phase & in_selection) { 289 status = inb(fd->base + REG_BSTAT); 290 if (!(status & BSTAT_BSY)) { 291 /* Try again, for slow devices */ 292 if (fdomain_select(cmd->device->host, scmd_id(cmd))) { 293 set_host_byte(cmd, DID_NO_CONNECT); 294 fdomain_finish_cmd(fd); 295 goto out; 296 } 297 /* Stop arbitration and enable parity */ 298 outb(ACTL_IRQEN | PARITY_MASK, fd->base + REG_ACTL); 299 } 300 cmd->SCp.phase = in_other; 301 outb(ICTL_FIFO | ICTL_REQ | FIFO_COUNT, fd->base + REG_ICTL); 302 outb(BCTL_BUSEN, fd->base + REG_BCTL); 303 goto out; 304 } 305 306 /* cur_cmd->SCp.phase == in_other: this is the body of the routine */ 307 status = inb(fd->base + REG_BSTAT); 308 309 if (status & BSTAT_REQ) { 310 switch (status & (BSTAT_MSG | BSTAT_CMD | BSTAT_IO)) { 311 case BSTAT_CMD: /* COMMAND OUT */ 312 outb(cmd->cmnd[cmd->SCp.sent_command++], 313 fd->base + REG_SCSI_DATA); 314 break; 315 case 0: /* DATA OUT -- tmc18c50/tmc18c30 only */ 316 if (fd->chip != tmc1800 && !cmd->SCp.have_data_in) { 317 cmd->SCp.have_data_in = -1; 318 outb(ACTL_IRQEN | ACTL_FIFOWR | ACTL_FIFOEN | 319 PARITY_MASK, fd->base + REG_ACTL); 320 } 321 break; 322 case BSTAT_IO: /* DATA IN -- tmc18c50/tmc18c30 only */ 323 if (fd->chip != tmc1800 && !cmd->SCp.have_data_in) { 324 cmd->SCp.have_data_in = 1; 325 outb(ACTL_IRQEN | ACTL_FIFOEN | PARITY_MASK, 326 fd->base + REG_ACTL); 327 } 328 break; 329 case BSTAT_CMD | BSTAT_IO: /* STATUS IN */ 330 cmd->SCp.Status = inb(fd->base + REG_SCSI_DATA); 331 break; 332 case BSTAT_MSG | BSTAT_CMD: /* MESSAGE OUT */ 333 outb(MESSAGE_REJECT, fd->base + REG_SCSI_DATA); 334 break; 335 case BSTAT_MSG | BSTAT_CMD | BSTAT_IO: /* MESSAGE IN */ 336 cmd->SCp.Message = inb(fd->base + REG_SCSI_DATA); 337 if (cmd->SCp.Message == COMMAND_COMPLETE) 338 ++done; 339 break; 340 } 341 } 342 343 if (fd->chip == tmc1800 && !cmd->SCp.have_data_in && 344 cmd->SCp.sent_command >= cmd->cmd_len) { 345 if (cmd->sc_data_direction == DMA_TO_DEVICE) { 346 cmd->SCp.have_data_in = -1; 347 outb(ACTL_IRQEN | ACTL_FIFOWR | ACTL_FIFOEN | 348 PARITY_MASK, fd->base + REG_ACTL); 349 } else { 350 cmd->SCp.have_data_in = 1; 351 outb(ACTL_IRQEN | ACTL_FIFOEN | PARITY_MASK, 352 fd->base + REG_ACTL); 353 } 354 } 355 356 if (cmd->SCp.have_data_in == -1) /* DATA OUT */ 357 fdomain_write_data(cmd); 358 359 if (cmd->SCp.have_data_in == 1) /* DATA IN */ 360 fdomain_read_data(cmd); 361 362 if (done) { 363 set_status_byte(cmd, cmd->SCp.Status); 364 set_msg_byte(cmd, cmd->SCp.Message); 365 set_host_byte(cmd, DID_OK); 366 fdomain_finish_cmd(fd); 367 } else { 368 if (cmd->SCp.phase & disconnect) { 369 outb(ICTL_FIFO | ICTL_SEL | ICTL_REQ | FIFO_COUNT, 370 fd->base + REG_ICTL); 371 outb(0, fd->base + REG_BCTL); 372 } else 373 outb(ICTL_FIFO | ICTL_REQ | FIFO_COUNT, 374 fd->base + REG_ICTL); 375 } 376 out: 377 spin_unlock_irqrestore(sh->host_lock, flags); 378 } 379 380 static irqreturn_t fdomain_irq(int irq, void *dev_id) 381 { 382 struct fdomain *fd = dev_id; 383 384 /* Is it our IRQ? */ 385 if ((inb(fd->base + REG_ASTAT) & ASTAT_IRQ) == 0) 386 return IRQ_NONE; 387 388 outb(0, fd->base + REG_ICTL); 389 390 /* We usually have one spurious interrupt after each command. */ 391 if (!fd->cur_cmd) /* Spurious interrupt */ 392 return IRQ_NONE; 393 394 schedule_work(&fd->work); 395 396 return IRQ_HANDLED; 397 } 398 399 static int fdomain_queue(struct Scsi_Host *sh, struct scsi_cmnd *cmd) 400 { 401 struct fdomain *fd = shost_priv(cmd->device->host); 402 unsigned long flags; 403 404 cmd->SCp.Status = 0; 405 cmd->SCp.Message = 0; 406 cmd->SCp.have_data_in = 0; 407 cmd->SCp.sent_command = 0; 408 cmd->SCp.phase = in_arbitration; 409 scsi_set_resid(cmd, scsi_bufflen(cmd)); 410 411 spin_lock_irqsave(sh->host_lock, flags); 412 413 fd->cur_cmd = cmd; 414 415 fdomain_make_bus_idle(fd); 416 417 /* Start arbitration */ 418 outb(0, fd->base + REG_ICTL); 419 outb(0, fd->base + REG_BCTL); /* Disable data drivers */ 420 /* Set our id bit */ 421 outb(BIT(cmd->device->host->this_id), fd->base + REG_SCSI_DATA_NOACK); 422 outb(ICTL_ARB, fd->base + REG_ICTL); 423 /* Start arbitration */ 424 outb(ACTL_ARB | ACTL_IRQEN | PARITY_MASK, fd->base + REG_ACTL); 425 426 spin_unlock_irqrestore(sh->host_lock, flags); 427 428 return 0; 429 } 430 431 static int fdomain_abort(struct scsi_cmnd *cmd) 432 { 433 struct Scsi_Host *sh = cmd->device->host; 434 struct fdomain *fd = shost_priv(sh); 435 unsigned long flags; 436 437 if (!fd->cur_cmd) 438 return FAILED; 439 440 spin_lock_irqsave(sh->host_lock, flags); 441 442 fdomain_make_bus_idle(fd); 443 fd->cur_cmd->SCp.phase |= aborted; 444 445 /* Aborts are not done well. . . */ 446 set_host_byte(fd->cur_cmd, DID_ABORT); 447 fdomain_finish_cmd(fd); 448 spin_unlock_irqrestore(sh->host_lock, flags); 449 return SUCCESS; 450 } 451 452 static int fdomain_host_reset(struct scsi_cmnd *cmd) 453 { 454 struct Scsi_Host *sh = cmd->device->host; 455 struct fdomain *fd = shost_priv(sh); 456 unsigned long flags; 457 458 spin_lock_irqsave(sh->host_lock, flags); 459 fdomain_reset(fd->base); 460 spin_unlock_irqrestore(sh->host_lock, flags); 461 return SUCCESS; 462 } 463 464 static int fdomain_biosparam(struct scsi_device *sdev, 465 struct block_device *bdev, sector_t capacity, 466 int geom[]) 467 { 468 unsigned char *p = scsi_bios_ptable(bdev); 469 470 if (p && p[65] == 0xaa && p[64] == 0x55 /* Partition table valid */ 471 && p[4]) { /* Partition type */ 472 geom[0] = p[5] + 1; /* heads */ 473 geom[1] = p[6] & 0x3f; /* sectors */ 474 } else { 475 if (capacity >= 0x7e0000) { 476 geom[0] = 255; /* heads */ 477 geom[1] = 63; /* sectors */ 478 } else if (capacity >= 0x200000) { 479 geom[0] = 128; /* heads */ 480 geom[1] = 63; /* sectors */ 481 } else { 482 geom[0] = 64; /* heads */ 483 geom[1] = 32; /* sectors */ 484 } 485 } 486 geom[2] = sector_div(capacity, geom[0] * geom[1]); 487 kfree(p); 488 489 return 0; 490 } 491 492 static struct scsi_host_template fdomain_template = { 493 .module = THIS_MODULE, 494 .name = "Future Domain TMC-16x0", 495 .proc_name = "fdomain", 496 .queuecommand = fdomain_queue, 497 .eh_abort_handler = fdomain_abort, 498 .eh_host_reset_handler = fdomain_host_reset, 499 .bios_param = fdomain_biosparam, 500 .can_queue = 1, 501 .this_id = 7, 502 .sg_tablesize = 64, 503 .dma_boundary = PAGE_SIZE - 1, 504 }; 505 506 struct Scsi_Host *fdomain_create(int base, int irq, int this_id, 507 struct device *dev) 508 { 509 struct Scsi_Host *sh; 510 struct fdomain *fd; 511 enum chip_type chip; 512 static const char * const chip_names[] = { 513 "Unknown", "TMC-1800", "TMC-18C50", "TMC-18C30" 514 }; 515 unsigned long irq_flags = 0; 516 517 chip = fdomain_identify(base); 518 if (!chip) 519 return NULL; 520 521 fdomain_reset(base); 522 523 if (fdomain_test_loopback(base)) 524 return NULL; 525 526 if (!irq) { 527 dev_err(dev, "card has no IRQ assigned"); 528 return NULL; 529 } 530 531 sh = scsi_host_alloc(&fdomain_template, sizeof(struct fdomain)); 532 if (!sh) 533 return NULL; 534 535 if (this_id) 536 sh->this_id = this_id & 0x07; 537 538 sh->irq = irq; 539 sh->io_port = base; 540 sh->n_io_port = FDOMAIN_REGION_SIZE; 541 542 fd = shost_priv(sh); 543 fd->base = base; 544 fd->chip = chip; 545 INIT_WORK(&fd->work, fdomain_work); 546 547 if (dev_is_pci(dev) || !strcmp(dev->bus->name, "pcmcia")) 548 irq_flags = IRQF_SHARED; 549 550 if (request_irq(irq, fdomain_irq, irq_flags, "fdomain", fd)) 551 goto fail_put; 552 553 shost_printk(KERN_INFO, sh, "%s chip at 0x%x irq %d SCSI ID %d\n", 554 dev_is_pci(dev) ? "TMC-36C70 (PCI bus)" : chip_names[chip], 555 base, irq, sh->this_id); 556 557 if (scsi_add_host(sh, dev)) 558 goto fail_free_irq; 559 560 scsi_scan_host(sh); 561 562 return sh; 563 564 fail_free_irq: 565 free_irq(irq, fd); 566 fail_put: 567 scsi_host_put(sh); 568 return NULL; 569 } 570 EXPORT_SYMBOL_GPL(fdomain_create); 571 572 int fdomain_destroy(struct Scsi_Host *sh) 573 { 574 struct fdomain *fd = shost_priv(sh); 575 576 cancel_work_sync(&fd->work); 577 scsi_remove_host(sh); 578 if (sh->irq) 579 free_irq(sh->irq, fd); 580 scsi_host_put(sh); 581 return 0; 582 } 583 EXPORT_SYMBOL_GPL(fdomain_destroy); 584 585 #ifdef CONFIG_PM_SLEEP 586 static int fdomain_resume(struct device *dev) 587 { 588 struct fdomain *fd = shost_priv(dev_get_drvdata(dev)); 589 590 fdomain_reset(fd->base); 591 return 0; 592 } 593 594 static SIMPLE_DEV_PM_OPS(fdomain_pm_ops, NULL, fdomain_resume); 595 #endif /* CONFIG_PM_SLEEP */ 596 597 MODULE_AUTHOR("Ondrej Zary, Rickard E. Faith"); 598 MODULE_DESCRIPTION("Future Domain TMC-16x0/TMC-3260 SCSI driver"); 599 MODULE_LICENSE("GPL"); 600