2 * SCSI low-level driver for the 53c94 SCSI bus adaptor found
3 * on Power Macintosh computers, controlling the external SCSI chain.
4 * We assume the 53c94 is connected to a DBDMA (descriptor-based DMA)
7 * Paul Mackerras, August 1996.
8 * Copyright (C) 1996 Paul Mackerras.
10 #include <linux/kernel.h>
11 #include <linux/delay.h>
12 #include <linux/types.h>
13 #include <linux/string.h>
14 #include <linux/slab.h>
15 #include <linux/blkdev.h>
16 #include <linux/proc_fs.h>
17 #include <linux/stat.h>
18 #include <linux/spinlock.h>
19 #include <linux/interrupt.h>
20 #include <asm/dbdma.h>
22 #include <asm/pgtable.h>
24 #include <asm/system.h>
25 #include <asm/pci-bridge.h>
40 volatile struct mac53c94_regs *regs;
42 volatile struct dbdma_regs *dma;
45 struct Scsi_Host *host;
46 struct fsc_state *next;
48 Scsi_Cmnd *request_qtail;
49 Scsi_Cmnd *current_req; /* req we're currently working on */
50 enum fsc_phase phase; /* what we're currently trying to do */
51 struct dbdma_cmd *dma_cmds; /* space for dbdma commands, aligned */
57 static struct fsc_state *all_53c94s;
59 static void mac53c94_init(struct fsc_state *);
60 static void mac53c94_start(struct fsc_state *);
61 static void mac53c94_interrupt(int, void *, struct pt_regs *);
62 static irqreturn_t do_mac53c94_interrupt(int, void *, struct pt_regs *);
63 static void cmd_done(struct fsc_state *, int result);
64 static void set_dma_cmds(struct fsc_state *, Scsi_Cmnd *);
65 static int data_goes_out(Scsi_Cmnd *);
68 mac53c94_detect(Scsi_Host_Template *tp)
70 struct device_node *node;
72 struct fsc_state *state, **prev_statep;
73 struct Scsi_Host *host;
75 unsigned char *clkprop;
81 prev_statep = &all_53c94s;
82 for (node = find_devices("53c94"); node != 0; node = node->next) {
83 if (node->n_addrs != 2 || node->n_intrs != 2) {
84 printk(KERN_ERR "mac53c94: expected 2 addrs and intrs"
85 " (got %d/%d) for node %s\n",
86 node->n_addrs, node->n_intrs, node->full_name);
91 if (node->parent != NULL
92 && !pci_device_from_OF_node(node->parent, &pbus, &devfn))
93 pdev = pci_find_slot(pbus, devfn);
95 printk(KERN_ERR "mac53c94: can't find PCI device "
96 "for %s\n", node->full_name);
100 host = scsi_register(tp, sizeof(struct fsc_state));
103 host->unique_id = nfscs;
105 state = (struct fsc_state *) host->hostdata;
108 printk(KERN_ERR "mac53c94: no state for %s?!\n",
110 scsi_unregister(host);
116 state->regs = (volatile struct mac53c94_regs *)
117 ioremap(node->addrs[0].address, 0x1000);
118 state->intr = node->intrs[0].line;
119 state->dma = (volatile struct dbdma_regs *)
120 ioremap(node->addrs[1].address, 0x1000);
121 state->dmaintr = node->intrs[1].line;
122 if (state->regs == NULL || state->dma == NULL) {
123 printk(KERN_ERR "mac53c94: ioremap failed for %s\n",
125 if (state->dma != NULL)
127 if (state->regs != NULL)
128 iounmap(state->regs);
129 scsi_unregister(host);
133 clkprop = get_property(node, "clock-frequency", &proplen);
134 if (clkprop == NULL || proplen != sizeof(int)) {
135 printk(KERN_ERR "%s: can't get clock frequency, "
136 "assuming 25MHz\n", node->full_name);
137 state->clk_freq = 25000000;
139 state->clk_freq = *(int *)clkprop;
141 /* Space for dma command list: +1 for stop command,
142 +1 to allow for aligning. */
143 dma_cmd_space = kmalloc((host->sg_tablesize + 2) *
144 sizeof(struct dbdma_cmd), GFP_KERNEL);
145 if (dma_cmd_space == 0) {
146 printk(KERN_ERR "mac53c94: couldn't allocate dma "
147 "command space for %s\n", node->full_name);
150 state->dma_cmds = (struct dbdma_cmd *)
151 DBDMA_ALIGN(dma_cmd_space);
152 memset(state->dma_cmds, 0, (host->sg_tablesize + 1)
153 * sizeof(struct dbdma_cmd));
154 state->dma_cmd_space = dma_cmd_space;
156 *prev_statep = state;
157 prev_statep = &state->next;
159 if (request_irq(state->intr, do_mac53c94_interrupt, 0,
161 printk(KERN_ERR "mac53C94: can't get irq %d for %s\n",
162 state->intr, node->full_name);
165 iounmap(state->regs);
166 scsi_unregister(host);
170 mac53c94_init(state);
178 mac53c94_release(struct Scsi_Host *host)
180 struct fsc_state *fp = (struct fsc_state *) host->hostdata;
185 iounmap((void *) fp->regs);
187 iounmap((void *) fp->dma);
188 kfree(fp->dma_cmd_space);
189 free_irq(fp->intr, fp);
194 mac53c94_queue(Scsi_Cmnd *cmd, void (*done)(Scsi_Cmnd *))
196 struct fsc_state *state;
199 if (data_goes_out(cmd)) {
201 printk(KERN_DEBUG "mac53c94_queue %p: command is", cmd);
202 for (i = 0; i < cmd->cmd_len; ++i)
203 printk(" %.2x", cmd->cmnd[i]);
204 printk("\n" KERN_DEBUG "use_sg=%d request_bufflen=%d request_buffer=%p\n",
205 cmd->use_sg, cmd->request_bufflen, cmd->request_buffer);
209 cmd->scsi_done = done;
210 cmd->host_scribble = NULL;
212 state = (struct fsc_state *) cmd->device->host->hostdata;
214 if (state->request_q == NULL)
215 state->request_q = cmd;
217 state->request_qtail->host_scribble = (void *) cmd;
218 state->request_qtail = cmd;
220 if (state->phase == idle)
221 mac53c94_start(state);
227 mac53c94_abort(Scsi_Cmnd *cmd)
229 return SCSI_ABORT_SNOOZE;
233 mac53c94_host_reset(Scsi_Cmnd *cmd)
235 struct fsc_state *state = (struct fsc_state *) cmd->device->host->hostdata;
236 volatile struct mac53c94_regs *regs = state->regs;
237 volatile struct dbdma_regs *dma = state->dma;
239 st_le32(&dma->control, (RUN|PAUSE|FLUSH|WAKE) << 16);
240 regs->command = CMD_SCSI_RESET; /* assert RST */
242 udelay(100); /* leave it on for a while (>= 25us) */
243 regs->command = CMD_RESET;
246 mac53c94_init(state);
247 regs->command = CMD_NOP;
253 mac53c94_init(struct fsc_state *state)
255 volatile struct mac53c94_regs *regs = state->regs;
256 volatile struct dbdma_regs *dma = state->dma;
259 regs->config1 = state->host->this_id | CF1_PAR_ENABLE;
260 regs->sel_timeout = TIMO_VAL(250); /* 250ms */
261 regs->clk_factor = CLKF_VAL(state->clk_freq);
262 regs->config2 = CF2_FEATURE_EN;
264 regs->sync_period = 0;
265 regs->sync_offset = 0;
268 st_le32(&dma->control, (RUN|PAUSE|FLUSH|WAKE) << 16);
272 * Start the next command for a 53C94.
273 * Should be called with interrupts disabled.
276 mac53c94_start(struct fsc_state *state)
279 volatile struct mac53c94_regs *regs = state->regs;
282 if (state->phase != idle || state->current_req != NULL)
283 panic("inappropriate mac53c94_start (state=%p)", state);
284 if (state->request_q == NULL)
286 state->current_req = cmd = state->request_q;
287 state->request_q = (Scsi_Cmnd *) cmd->host_scribble;
294 regs->command = CMD_NOP + CMD_DMA_MODE;
297 regs->command = CMD_FLUSH;
300 regs->dest_id = cmd->device->id;
301 regs->sync_period = 0;
302 regs->sync_offset = 0;
305 /* load the command into the FIFO */
306 for (i = 0; i < cmd->cmd_len; ++i) {
307 regs->fifo = cmd->cmnd[i];
311 /* do select without ATN XXX */
312 regs->command = CMD_SELECT;
313 state->phase = selecting;
315 if (cmd->use_sg > 0 || cmd->request_bufflen != 0)
316 set_dma_cmds(state, cmd);
320 do_mac53c94_interrupt(int irq, void *dev_id, struct pt_regs *ptregs)
323 struct Scsi_Host *dev = ((struct fsc_state *) dev_id)->current_req->device->host;
325 spin_lock_irqsave(dev->host_lock, flags);
326 mac53c94_interrupt(irq, dev_id, ptregs);
327 spin_unlock_irqrestore(dev->host_lock, flags);
332 mac53c94_interrupt(int irq, void *dev_id, struct pt_regs *ptregs)
334 struct fsc_state *state = (struct fsc_state *) dev_id;
335 volatile struct mac53c94_regs *regs = state->regs;
336 volatile struct dbdma_regs *dma = state->dma;
337 Scsi_Cmnd *cmd = state->current_req;
338 int nb, stat, seq, intr;
339 static int mac53c94_errors;
343 * Apparently, reading the interrupt register unlatches
344 * the status and sequence step registers.
348 intr = regs->interrupt;
351 printk(KERN_DEBUG "mac53c94_intr, intr=%x stat=%x seq=%x phase=%d\n",
352 intr, stat, seq, state->phase);
355 if (intr & INTR_RESET) {
356 /* SCSI bus was reset */
357 printk(KERN_INFO "external SCSI bus reset detected\n");
358 regs->command = CMD_NOP;
359 st_le32(&dma->control, RUN << 16); /* stop dma */
360 cmd_done(state, DID_RESET << 16);
363 if (intr & INTR_ILL_CMD) {
364 printk(KERN_ERR "53c94: invalid cmd, intr=%x stat=%x seq=%x phase=%d\n",
365 intr, stat, seq, state->phase);
366 cmd_done(state, DID_ERROR << 16);
369 if (stat & STAT_ERROR) {
371 /* XXX these seem to be harmless? */
372 printk("53c94: bad error, intr=%x stat=%x seq=%x phase=%d\n",
373 intr, stat, seq, state->phase);
376 regs->command = CMD_NOP + CMD_DMA_MODE;
380 printk(KERN_DEBUG "53c94: interrupt with no command active?\n");
383 if (stat & STAT_PARITY) {
384 printk(KERN_ERR "mac53c94: parity error\n");
385 cmd_done(state, DID_PARITY << 16);
388 switch (state->phase) {
390 if (intr & INTR_DISCONNECT) {
391 /* selection timed out */
392 cmd_done(state, DID_BAD_TARGET << 16);
395 if (intr != INTR_BUS_SERV + INTR_DONE) {
396 printk(KERN_DEBUG "got intr %x during selection\n", intr);
397 cmd_done(state, DID_ERROR << 16);
400 if ((seq & SS_MASK) != SS_DONE) {
401 printk(KERN_DEBUG "seq step %x after command\n", seq);
402 cmd_done(state, DID_ERROR << 16);
405 regs->command = CMD_NOP;
406 /* set DMA controller going if any data to transfer */
407 if ((stat & (STAT_MSG|STAT_CD)) == 0
408 && (cmd->use_sg > 0 || cmd->request_bufflen != 0)) {
409 nb = cmd->SCp.this_residual;
412 cmd->SCp.this_residual -= nb;
414 regs->count_mid = nb >> 8;
416 regs->command = CMD_DMA_MODE + CMD_NOP;
418 st_le32(&dma->cmdptr, virt_to_phys(state->dma_cmds));
419 st_le32(&dma->control, (RUN << 16) | RUN);
421 regs->command = CMD_DMA_MODE + CMD_XFER_DATA;
422 state->phase = dataing;
424 } else if ((stat & STAT_PHASE) == STAT_CD + STAT_IO) {
425 /* up to status phase already */
426 regs->command = CMD_I_COMPLETE;
427 state->phase = completing;
429 printk(KERN_DEBUG "in unexpected phase %x after cmd\n",
431 cmd_done(state, DID_ERROR << 16);
437 if (intr != INTR_BUS_SERV) {
438 printk(KERN_DEBUG "got intr %x before status\n", intr);
439 cmd_done(state, DID_ERROR << 16);
442 if (cmd->SCp.this_residual != 0
443 && (stat & (STAT_MSG|STAT_CD)) == 0) {
444 /* Set up the count regs to transfer more */
445 nb = cmd->SCp.this_residual;
448 cmd->SCp.this_residual -= nb;
450 regs->count_mid = nb >> 8;
452 regs->command = CMD_DMA_MODE + CMD_NOP;
454 regs->command = CMD_DMA_MODE + CMD_XFER_DATA;
457 if ((stat & STAT_PHASE) != STAT_CD + STAT_IO) {
458 printk(KERN_DEBUG "intr %x before data xfer complete\n", intr);
460 out_le32(&dma->control, RUN << 16); /* stop dma */
461 dma_dir = scsi_to_pci_dma_dir(cmd->sc_data_direction);
462 if (cmd->use_sg != 0) {
463 pci_unmap_sg(state->pdev,
464 (struct scatterlist *)cmd->request_buffer,
465 cmd->use_sg, dma_dir);
467 pci_unmap_single(state->pdev, state->dma_addr,
468 cmd->request_bufflen, dma_dir);
470 /* should check dma status */
471 regs->command = CMD_I_COMPLETE;
472 state->phase = completing;
475 if (intr != INTR_DONE) {
476 printk(KERN_DEBUG "got intr %x on completion\n", intr);
477 cmd_done(state, DID_ERROR << 16);
480 cmd->SCp.Status = regs->fifo; eieio();
481 cmd->SCp.Message = regs->fifo; eieio();
483 regs->command = CMD_ACCEPT_MSG;
484 state->phase = busfreeing;
487 if (intr != INTR_DISCONNECT) {
488 printk(KERN_DEBUG "got intr %x when expected disconnect\n", intr);
490 cmd_done(state, (DID_OK << 16) + (cmd->SCp.Message << 8)
494 printk(KERN_DEBUG "don't know about phase %d\n", state->phase);
499 cmd_done(struct fsc_state *state, int result)
503 cmd = state->current_req;
505 cmd->result = result;
506 (*cmd->scsi_done)(cmd);
507 state->current_req = NULL;
510 mac53c94_start(state);
514 * Set up DMA commands for transferring data.
517 set_dma_cmds(struct fsc_state *state, Scsi_Cmnd *cmd)
519 int i, dma_cmd, total;
520 struct scatterlist *scl;
521 struct dbdma_cmd *dcmds;
524 int dma_dir = scsi_to_pci_dma_dir(cmd->sc_data_direction);
526 dma_cmd = data_goes_out(cmd)? OUTPUT_MORE: INPUT_MORE;
527 dcmds = state->dma_cmds;
528 if (cmd->use_sg > 0) {
532 scl = (struct scatterlist *) cmd->buffer;
533 nseg = pci_map_sg(state->pdev, scl, cmd->use_sg, dma_dir);
534 for (i = 0; i < nseg; ++i) {
535 dma_addr = sg_dma_address(scl);
536 dma_len = sg_dma_len(scl);
537 if (dma_len > 0xffff)
538 panic("mac53c94: scatterlist element >= 64k");
540 st_le16(&dcmds->req_count, dma_len);
541 st_le16(&dcmds->command, dma_cmd);
542 st_le32(&dcmds->phy_addr, dma_addr);
543 dcmds->xfer_status = 0;
548 total = cmd->request_bufflen;
550 panic("mac53c94: transfer size >= 64k");
551 dma_addr = pci_map_single(state->pdev, cmd->request_buffer,
553 state->dma_addr = dma_addr;
554 st_le16(&dcmds->req_count, total);
555 st_le32(&dcmds->phy_addr, dma_addr);
556 dcmds->xfer_status = 0;
559 dma_cmd += OUTPUT_LAST - OUTPUT_MORE;
560 st_le16(&dcmds[-1].command, dma_cmd);
561 st_le16(&dcmds->command, DBDMA_STOP);
562 cmd->SCp.this_residual = total;
566 * Work out whether data will be going out from the host adaptor or into it.
569 data_goes_out(Scsi_Cmnd *cmd)
571 switch (cmd->sc_data_direction) {
572 case SCSI_DATA_WRITE:
578 /* for SCSI_DATA_UNKNOWN or SCSI_DATA_NONE, fall back on the
579 old method for now... */
580 switch (cmd->cmnd[0]) {
581 case CHANGE_DEFINITION:
590 case REASSIGN_BLOCKS:
593 case SEARCH_EQUAL_12:
598 case SEND_DIAGNOSTIC:
599 case SEND_VOLUME_TAG:
607 case WRITE_LONG_2: /* alternate code for WRITE_LONG */
610 case WRITE_VERIFY_12:
617 static Scsi_Host_Template driver_template = {
618 .proc_name = "53c94",
620 .detect = mac53c94_detect,
621 .release = mac53c94_release,
622 .queuecommand = mac53c94_queue,
623 .eh_abort_handler = mac53c94_abort,
624 .eh_host_reset_handler = mac53c94_host_reset,
627 .sg_tablesize = SG_ALL,
629 .use_clustering = DISABLE_CLUSTERING,
632 #include "scsi_module.c"