1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * SCSI low-level driver for the 53c94 SCSI bus adaptor found 4 * on Power Macintosh computers, controlling the external SCSI chain. 5 * We assume the 53c94 is connected to a DBDMA (descriptor-based DMA) 6 * controller. 7 * 8 * Paul Mackerras, August 1996. 9 * Copyright (C) 1996 Paul Mackerras. 10 */ 11 #include <linux/kernel.h> 12 #include <linux/delay.h> 13 #include <linux/types.h> 14 #include <linux/string.h> 15 #include <linux/slab.h> 16 #include <linux/blkdev.h> 17 #include <linux/proc_fs.h> 18 #include <linux/stat.h> 19 #include <linux/spinlock.h> 20 #include <linux/interrupt.h> 21 #include <linux/module.h> 22 #include <linux/pci.h> 23 #include <linux/pgtable.h> 24 #include <asm/dbdma.h> 25 #include <asm/io.h> 26 #include <asm/prom.h> 27 #include <asm/macio.h> 28 29 #include <scsi/scsi.h> 30 #include <scsi/scsi_cmnd.h> 31 #include <scsi/scsi_device.h> 32 #include <scsi/scsi_host.h> 33 34 #include "mac53c94.h" 35 36 enum fsc_phase { 37 idle, 38 selecting, 39 dataing, 40 completing, 41 busfreeing, 42 }; 43 44 struct fsc_state { 45 struct mac53c94_regs __iomem *regs; 46 int intr; 47 struct dbdma_regs __iomem *dma; 48 int dmaintr; 49 int clk_freq; 50 struct Scsi_Host *host; 51 struct scsi_cmnd *request_q; 52 struct scsi_cmnd *request_qtail; 53 struct scsi_cmnd *current_req; /* req we're currently working on */ 54 enum fsc_phase phase; /* what we're currently trying to do */ 55 struct dbdma_cmd *dma_cmds; /* space for dbdma commands, aligned */ 56 void *dma_cmd_space; 57 struct pci_dev *pdev; 58 dma_addr_t dma_addr; 59 struct macio_dev *mdev; 60 }; 61 62 static void mac53c94_init(struct fsc_state *); 63 static void mac53c94_start(struct fsc_state *); 64 static void mac53c94_interrupt(int, void *); 65 static irqreturn_t do_mac53c94_interrupt(int, void *); 66 static void cmd_done(struct fsc_state *, int result); 67 static void set_dma_cmds(struct fsc_state *, struct scsi_cmnd *); 68 69 70 static int mac53c94_queue_lck(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *)) 71 { 72 struct fsc_state *state; 73 74 #if 0 75 if (cmd->sc_data_direction == DMA_TO_DEVICE) { 76 int i; 77 printk(KERN_DEBUG "mac53c94_queue %p: command is", cmd); 78 for (i = 0; i < cmd->cmd_len; ++i) 79 printk(KERN_CONT " %.2x", cmd->cmnd[i]); 80 printk(KERN_CONT "\n"); 81 printk(KERN_DEBUG "use_sg=%d request_bufflen=%d request_buffer=%p\n", 82 scsi_sg_count(cmd), scsi_bufflen(cmd), scsi_sglist(cmd)); 83 } 84 #endif 85 86 cmd->host_scribble = NULL; 87 88 state = (struct fsc_state *) cmd->device->host->hostdata; 89 90 if (state->request_q == NULL) 91 state->request_q = cmd; 92 else 93 state->request_qtail->host_scribble = (void *) cmd; 94 state->request_qtail = cmd; 95 96 if (state->phase == idle) 97 mac53c94_start(state); 98 99 return 0; 100 } 101 102 static DEF_SCSI_QCMD(mac53c94_queue) 103 104 static int mac53c94_host_reset(struct scsi_cmnd *cmd) 105 { 106 struct fsc_state *state = (struct fsc_state *) cmd->device->host->hostdata; 107 struct mac53c94_regs __iomem *regs = state->regs; 108 struct dbdma_regs __iomem *dma = state->dma; 109 unsigned long flags; 110 111 spin_lock_irqsave(cmd->device->host->host_lock, flags); 112 113 writel((RUN|PAUSE|FLUSH|WAKE) << 16, &dma->control); 114 writeb(CMD_SCSI_RESET, ®s->command); /* assert RST */ 115 udelay(100); /* leave it on for a while (>= 25us) */ 116 writeb(CMD_RESET, ®s->command); 117 udelay(20); 118 mac53c94_init(state); 119 writeb(CMD_NOP, ®s->command); 120 121 spin_unlock_irqrestore(cmd->device->host->host_lock, flags); 122 return SUCCESS; 123 } 124 125 static void mac53c94_init(struct fsc_state *state) 126 { 127 struct mac53c94_regs __iomem *regs = state->regs; 128 struct dbdma_regs __iomem *dma = state->dma; 129 int x; 130 131 writeb(state->host->this_id | CF1_PAR_ENABLE, ®s->config1); 132 writeb(TIMO_VAL(250), ®s->sel_timeout); /* 250ms */ 133 writeb(CLKF_VAL(state->clk_freq), ®s->clk_factor); 134 writeb(CF2_FEATURE_EN, ®s->config2); 135 writeb(0, ®s->config3); 136 writeb(0, ®s->sync_period); 137 writeb(0, ®s->sync_offset); 138 x = readb(®s->interrupt); 139 writel((RUN|PAUSE|FLUSH|WAKE) << 16, &dma->control); 140 } 141 142 /* 143 * Start the next command for a 53C94. 144 * Should be called with interrupts disabled. 145 */ 146 static void mac53c94_start(struct fsc_state *state) 147 { 148 struct scsi_cmnd *cmd; 149 struct mac53c94_regs __iomem *regs = state->regs; 150 int i; 151 152 if (state->phase != idle || state->current_req != NULL) 153 panic("inappropriate mac53c94_start (state=%p)", state); 154 if (state->request_q == NULL) 155 return; 156 state->current_req = cmd = state->request_q; 157 state->request_q = (struct scsi_cmnd *) cmd->host_scribble; 158 159 /* Off we go */ 160 writeb(0, ®s->count_lo); 161 writeb(0, ®s->count_mid); 162 writeb(0, ®s->count_hi); 163 writeb(CMD_NOP + CMD_DMA_MODE, ®s->command); 164 udelay(1); 165 writeb(CMD_FLUSH, ®s->command); 166 udelay(1); 167 writeb(cmd->device->id, ®s->dest_id); 168 writeb(0, ®s->sync_period); 169 writeb(0, ®s->sync_offset); 170 171 /* load the command into the FIFO */ 172 for (i = 0; i < cmd->cmd_len; ++i) 173 writeb(cmd->cmnd[i], ®s->fifo); 174 175 /* do select without ATN XXX */ 176 writeb(CMD_SELECT, ®s->command); 177 state->phase = selecting; 178 179 set_dma_cmds(state, cmd); 180 } 181 182 static irqreturn_t do_mac53c94_interrupt(int irq, void *dev_id) 183 { 184 unsigned long flags; 185 struct Scsi_Host *dev = ((struct fsc_state *) dev_id)->current_req->device->host; 186 187 spin_lock_irqsave(dev->host_lock, flags); 188 mac53c94_interrupt(irq, dev_id); 189 spin_unlock_irqrestore(dev->host_lock, flags); 190 return IRQ_HANDLED; 191 } 192 193 static void mac53c94_interrupt(int irq, void *dev_id) 194 { 195 struct fsc_state *state = (struct fsc_state *) dev_id; 196 struct mac53c94_regs __iomem *regs = state->regs; 197 struct dbdma_regs __iomem *dma = state->dma; 198 struct scsi_cmnd *cmd = state->current_req; 199 int nb, stat, seq, intr; 200 static int mac53c94_errors; 201 202 /* 203 * Apparently, reading the interrupt register unlatches 204 * the status and sequence step registers. 205 */ 206 seq = readb(®s->seqstep); 207 stat = readb(®s->status); 208 intr = readb(®s->interrupt); 209 210 #if 0 211 printk(KERN_DEBUG "mac53c94_intr, intr=%x stat=%x seq=%x phase=%d\n", 212 intr, stat, seq, state->phase); 213 #endif 214 215 if (intr & INTR_RESET) { 216 /* SCSI bus was reset */ 217 printk(KERN_INFO "external SCSI bus reset detected\n"); 218 writeb(CMD_NOP, ®s->command); 219 writel(RUN << 16, &dma->control); /* stop dma */ 220 cmd_done(state, DID_RESET << 16); 221 return; 222 } 223 if (intr & INTR_ILL_CMD) { 224 printk(KERN_ERR "53c94: invalid cmd, intr=%x stat=%x seq=%x phase=%d\n", 225 intr, stat, seq, state->phase); 226 cmd_done(state, DID_ERROR << 16); 227 return; 228 } 229 if (stat & STAT_ERROR) { 230 #if 0 231 /* XXX these seem to be harmless? */ 232 printk("53c94: bad error, intr=%x stat=%x seq=%x phase=%d\n", 233 intr, stat, seq, state->phase); 234 #endif 235 ++mac53c94_errors; 236 writeb(CMD_NOP + CMD_DMA_MODE, ®s->command); 237 } 238 if (cmd == 0) { 239 printk(KERN_DEBUG "53c94: interrupt with no command active?\n"); 240 return; 241 } 242 if (stat & STAT_PARITY) { 243 printk(KERN_ERR "mac53c94: parity error\n"); 244 cmd_done(state, DID_PARITY << 16); 245 return; 246 } 247 switch (state->phase) { 248 case selecting: 249 if (intr & INTR_DISCONNECT) { 250 /* selection timed out */ 251 cmd_done(state, DID_BAD_TARGET << 16); 252 return; 253 } 254 if (intr != INTR_BUS_SERV + INTR_DONE) { 255 printk(KERN_DEBUG "got intr %x during selection\n", intr); 256 cmd_done(state, DID_ERROR << 16); 257 return; 258 } 259 if ((seq & SS_MASK) != SS_DONE) { 260 printk(KERN_DEBUG "seq step %x after command\n", seq); 261 cmd_done(state, DID_ERROR << 16); 262 return; 263 } 264 writeb(CMD_NOP, ®s->command); 265 /* set DMA controller going if any data to transfer */ 266 if ((stat & (STAT_MSG|STAT_CD)) == 0 267 && (scsi_sg_count(cmd) > 0 || scsi_bufflen(cmd))) { 268 nb = cmd->SCp.this_residual; 269 if (nb > 0xfff0) 270 nb = 0xfff0; 271 cmd->SCp.this_residual -= nb; 272 writeb(nb, ®s->count_lo); 273 writeb(nb >> 8, ®s->count_mid); 274 writeb(CMD_DMA_MODE + CMD_NOP, ®s->command); 275 writel(virt_to_phys(state->dma_cmds), &dma->cmdptr); 276 writel((RUN << 16) | RUN, &dma->control); 277 writeb(CMD_DMA_MODE + CMD_XFER_DATA, ®s->command); 278 state->phase = dataing; 279 break; 280 } else if ((stat & STAT_PHASE) == STAT_CD + STAT_IO) { 281 /* up to status phase already */ 282 writeb(CMD_I_COMPLETE, ®s->command); 283 state->phase = completing; 284 } else { 285 printk(KERN_DEBUG "in unexpected phase %x after cmd\n", 286 stat & STAT_PHASE); 287 cmd_done(state, DID_ERROR << 16); 288 return; 289 } 290 break; 291 292 case dataing: 293 if (intr != INTR_BUS_SERV) { 294 printk(KERN_DEBUG "got intr %x before status\n", intr); 295 cmd_done(state, DID_ERROR << 16); 296 return; 297 } 298 if (cmd->SCp.this_residual != 0 299 && (stat & (STAT_MSG|STAT_CD)) == 0) { 300 /* Set up the count regs to transfer more */ 301 nb = cmd->SCp.this_residual; 302 if (nb > 0xfff0) 303 nb = 0xfff0; 304 cmd->SCp.this_residual -= nb; 305 writeb(nb, ®s->count_lo); 306 writeb(nb >> 8, ®s->count_mid); 307 writeb(CMD_DMA_MODE + CMD_NOP, ®s->command); 308 writeb(CMD_DMA_MODE + CMD_XFER_DATA, ®s->command); 309 break; 310 } 311 if ((stat & STAT_PHASE) != STAT_CD + STAT_IO) { 312 printk(KERN_DEBUG "intr %x before data xfer complete\n", intr); 313 } 314 writel(RUN << 16, &dma->control); /* stop dma */ 315 scsi_dma_unmap(cmd); 316 /* should check dma status */ 317 writeb(CMD_I_COMPLETE, ®s->command); 318 state->phase = completing; 319 break; 320 case completing: 321 if (intr != INTR_DONE) { 322 printk(KERN_DEBUG "got intr %x on completion\n", intr); 323 cmd_done(state, DID_ERROR << 16); 324 return; 325 } 326 cmd->SCp.Status = readb(®s->fifo); 327 cmd->SCp.Message = readb(®s->fifo); 328 writeb(CMD_ACCEPT_MSG, ®s->command); 329 state->phase = busfreeing; 330 break; 331 case busfreeing: 332 if (intr != INTR_DISCONNECT) { 333 printk(KERN_DEBUG "got intr %x when expected disconnect\n", intr); 334 } 335 cmd_done(state, (DID_OK << 16) + (cmd->SCp.Message << 8) 336 + cmd->SCp.Status); 337 break; 338 default: 339 printk(KERN_DEBUG "don't know about phase %d\n", state->phase); 340 } 341 } 342 343 static void cmd_done(struct fsc_state *state, int result) 344 { 345 struct scsi_cmnd *cmd; 346 347 cmd = state->current_req; 348 if (cmd) { 349 cmd->result = result; 350 scsi_done(cmd); 351 state->current_req = NULL; 352 } 353 state->phase = idle; 354 mac53c94_start(state); 355 } 356 357 /* 358 * Set up DMA commands for transferring data. 359 */ 360 static void set_dma_cmds(struct fsc_state *state, struct scsi_cmnd *cmd) 361 { 362 int i, dma_cmd, total, nseg; 363 struct scatterlist *scl; 364 struct dbdma_cmd *dcmds; 365 dma_addr_t dma_addr; 366 u32 dma_len; 367 368 nseg = scsi_dma_map(cmd); 369 BUG_ON(nseg < 0); 370 if (!nseg) 371 return; 372 373 dma_cmd = cmd->sc_data_direction == DMA_TO_DEVICE ? 374 OUTPUT_MORE : INPUT_MORE; 375 dcmds = state->dma_cmds; 376 total = 0; 377 378 scsi_for_each_sg(cmd, scl, nseg, i) { 379 dma_addr = sg_dma_address(scl); 380 dma_len = sg_dma_len(scl); 381 if (dma_len > 0xffff) 382 panic("mac53c94: scatterlist element >= 64k"); 383 total += dma_len; 384 dcmds->req_count = cpu_to_le16(dma_len); 385 dcmds->command = cpu_to_le16(dma_cmd); 386 dcmds->phy_addr = cpu_to_le32(dma_addr); 387 dcmds->xfer_status = 0; 388 ++dcmds; 389 } 390 391 dma_cmd += OUTPUT_LAST - OUTPUT_MORE; 392 dcmds[-1].command = cpu_to_le16(dma_cmd); 393 dcmds->command = cpu_to_le16(DBDMA_STOP); 394 cmd->SCp.this_residual = total; 395 } 396 397 static struct scsi_host_template mac53c94_template = { 398 .proc_name = "53c94", 399 .name = "53C94", 400 .queuecommand = mac53c94_queue, 401 .eh_host_reset_handler = mac53c94_host_reset, 402 .can_queue = 1, 403 .this_id = 7, 404 .sg_tablesize = SG_ALL, 405 .max_segment_size = 65535, 406 }; 407 408 static int mac53c94_probe(struct macio_dev *mdev, const struct of_device_id *match) 409 { 410 struct device_node *node = macio_get_of_node(mdev); 411 struct pci_dev *pdev = macio_get_pci_dev(mdev); 412 struct fsc_state *state; 413 struct Scsi_Host *host; 414 void *dma_cmd_space; 415 const unsigned char *clkprop; 416 int proplen, rc = -ENODEV; 417 418 if (macio_resource_count(mdev) != 2 || macio_irq_count(mdev) != 2) { 419 printk(KERN_ERR "mac53c94: expected 2 addrs and intrs" 420 " (got %d/%d)\n", 421 macio_resource_count(mdev), macio_irq_count(mdev)); 422 return -ENODEV; 423 } 424 425 if (macio_request_resources(mdev, "mac53c94") != 0) { 426 printk(KERN_ERR "mac53c94: unable to request memory resources"); 427 return -EBUSY; 428 } 429 430 host = scsi_host_alloc(&mac53c94_template, sizeof(struct fsc_state)); 431 if (host == NULL) { 432 printk(KERN_ERR "mac53c94: couldn't register host"); 433 rc = -ENOMEM; 434 goto out_release; 435 } 436 437 state = (struct fsc_state *) host->hostdata; 438 macio_set_drvdata(mdev, state); 439 state->host = host; 440 state->pdev = pdev; 441 state->mdev = mdev; 442 443 state->regs = (struct mac53c94_regs __iomem *) 444 ioremap(macio_resource_start(mdev, 0), 0x1000); 445 state->intr = macio_irq(mdev, 0); 446 state->dma = (struct dbdma_regs __iomem *) 447 ioremap(macio_resource_start(mdev, 1), 0x1000); 448 state->dmaintr = macio_irq(mdev, 1); 449 if (state->regs == NULL || state->dma == NULL) { 450 printk(KERN_ERR "mac53c94: ioremap failed for %pOF\n", node); 451 goto out_free; 452 } 453 454 clkprop = of_get_property(node, "clock-frequency", &proplen); 455 if (clkprop == NULL || proplen != sizeof(int)) { 456 printk(KERN_ERR "%pOF: can't get clock frequency, " 457 "assuming 25MHz\n", node); 458 state->clk_freq = 25000000; 459 } else 460 state->clk_freq = *(int *)clkprop; 461 462 /* Space for dma command list: +1 for stop command, 463 * +1 to allow for aligning. 464 * XXX FIXME: Use DMA consistent routines 465 */ 466 dma_cmd_space = kmalloc_array(host->sg_tablesize + 2, 467 sizeof(struct dbdma_cmd), 468 GFP_KERNEL); 469 if (!dma_cmd_space) { 470 printk(KERN_ERR "mac53c94: couldn't allocate dma " 471 "command space for %pOF\n", node); 472 rc = -ENOMEM; 473 goto out_free; 474 } 475 476 state->dma_cmds = (struct dbdma_cmd *)DBDMA_ALIGN(dma_cmd_space); 477 memset(state->dma_cmds, 0, (host->sg_tablesize + 1) 478 * sizeof(struct dbdma_cmd)); 479 state->dma_cmd_space = dma_cmd_space; 480 481 mac53c94_init(state); 482 483 if (request_irq(state->intr, do_mac53c94_interrupt, 0, "53C94",state)) { 484 printk(KERN_ERR "mac53C94: can't get irq %d for %pOF\n", 485 state->intr, node); 486 goto out_free_dma; 487 } 488 489 rc = scsi_add_host(host, &mdev->ofdev.dev); 490 if (rc != 0) 491 goto out_release_irq; 492 493 scsi_scan_host(host); 494 return 0; 495 496 out_release_irq: 497 free_irq(state->intr, state); 498 out_free_dma: 499 kfree(state->dma_cmd_space); 500 out_free: 501 if (state->dma != NULL) 502 iounmap(state->dma); 503 if (state->regs != NULL) 504 iounmap(state->regs); 505 scsi_host_put(host); 506 out_release: 507 macio_release_resources(mdev); 508 509 return rc; 510 } 511 512 static int mac53c94_remove(struct macio_dev *mdev) 513 { 514 struct fsc_state *fp = (struct fsc_state *)macio_get_drvdata(mdev); 515 struct Scsi_Host *host = fp->host; 516 517 scsi_remove_host(host); 518 519 free_irq(fp->intr, fp); 520 521 if (fp->regs) 522 iounmap(fp->regs); 523 if (fp->dma) 524 iounmap(fp->dma); 525 kfree(fp->dma_cmd_space); 526 527 scsi_host_put(host); 528 529 macio_release_resources(mdev); 530 531 return 0; 532 } 533 534 535 static struct of_device_id mac53c94_match[] = 536 { 537 { 538 .name = "53c94", 539 }, 540 {}, 541 }; 542 MODULE_DEVICE_TABLE (of, mac53c94_match); 543 544 static struct macio_driver mac53c94_driver = 545 { 546 .driver = { 547 .name = "mac53c94", 548 .owner = THIS_MODULE, 549 .of_match_table = mac53c94_match, 550 }, 551 .probe = mac53c94_probe, 552 .remove = mac53c94_remove, 553 }; 554 555 556 static int __init init_mac53c94(void) 557 { 558 return macio_register_driver(&mac53c94_driver); 559 } 560 561 static void __exit exit_mac53c94(void) 562 { 563 return macio_unregister_driver(&mac53c94_driver); 564 } 565 566 module_init(init_mac53c94); 567 module_exit(exit_mac53c94); 568 569 MODULE_DESCRIPTION("PowerMac 53c94 SCSI driver"); 570 MODULE_AUTHOR("Paul Mackerras <paulus@samba.org>"); 571 MODULE_LICENSE("GPL"); 572