2 * SCSI low-level driver for the MESH (Macintosh Enhanced SCSI Hardware)
3 * bus adaptor found on Power Macintosh computers.
4 * We assume the MESH is connected to a DBDMA (descriptor-based DMA)
7 * Paul Mackerras, August 1996.
8 * Copyright (C) 1996 Paul Mackerras.
10 * Apr. 14 2002 - BenH Rework bus reset code for new error handler
11 * Add delay after initial bus reset
12 * Add module parameters
14 * - handle aborts correctly
15 * - retry arbitration if lost (unless higher levels do this for us)
17 #include <linux/config.h>
18 #include <linux/module.h>
19 #include <linux/kernel.h>
20 #include <linux/delay.h>
21 #include <linux/types.h>
22 #include <linux/string.h>
23 #include <linux/slab.h>
24 #include <linux/blk.h>
25 #include <linux/proc_fs.h>
26 #include <linux/stat.h>
27 #include <linux/tqueue.h>
28 #include <linux/interrupt.h>
29 #include <linux/reboot.h>
30 #include <linux/spinlock.h>
31 #include <linux/pci.h>
32 #include <asm/dbdma.h>
34 #include <asm/pgtable.h>
36 #include <asm/system.h>
38 #include <asm/hydra.h>
39 #include <asm/processor.h>
40 #include <asm/machdep.h>
41 #include <asm/pmac_feature.h>
42 #include <asm/pci-bridge.h>
43 #ifdef CONFIG_PMAC_PBOOK
44 #include <linux/adb.h>
45 #include <linux/pmu.h>
54 #define KERN_DEBUG KERN_WARNING
57 MODULE_AUTHOR("Paul Mackerras (paulus@samba.org)");
58 MODULE_DESCRIPTION("PowerMac MESH SCSI driver");
59 MODULE_LICENSE("GPL");
61 MODULE_PARM(sync_rate, "i");
62 MODULE_PARM_DESC(sync_rate, "Synchronous rate (0..10, 0=async)");
63 MODULE_PARM(sync_targets, "i");
64 MODULE_PARM_DESC(sync_targets, "Bitmask of targets allowed to set synchronous");
65 MODULE_PARM(resel_targets, "i");
66 MODULE_PARM_DESC(resel_targets, "Bitmask of targets allowed to set disconnect");
67 MODULE_PARM(debug_targets, "i");
68 MODULE_PARM_DESC(debug_targets, "Bitmask of debugged targets");
69 MODULE_PARM(init_reset_delay, "i");
70 MODULE_PARM_DESC(init_reset_delay, "Initial bus reset delay (0=no reset)");
72 static int sync_rate = CONFIG_SCSI_MESH_SYNC_RATE;
73 static int sync_targets = 0xff;
74 static int resel_targets = 0xff;
75 static int debug_targets = 0; /* print debug for these targets */
76 static int init_reset_delay = CONFIG_SCSI_MESH_RESET_DELAY_MS;
78 static int mesh_sync_period = 100;
79 static int mesh_sync_offset = 0;
80 static unsigned char use_active_neg = 0; /* bit mask for SEQ_ACTIVE_NEG if used */
82 #define ALLOW_SYNC(tgt) ((sync_targets >> (tgt)) & 1)
83 #define ALLOW_RESEL(tgt) ((resel_targets >> (tgt)) & 1)
84 #define ALLOW_DEBUG(tgt) ((debug_targets >> (tgt)) & 1)
85 #define DEBUG_TARGET(cmd) ((cmd) && ALLOW_DEBUG((cmd)->target))
90 #define NUM_DBG_EVENTS 13
91 #undef DBG_USE_TB /* bombs on 601 */
132 enum sdtr_phase sdtr_state;
134 int data_goes_out; /* guess as to data direction */
135 Scsi_Cmnd *current_req;
140 struct dbglog log[N_DBG_LOG];
145 volatile struct mesh_regs *mesh;
147 volatile struct dbdma_regs *dma;
149 struct Scsi_Host *host;
150 struct mesh_state *next;
151 Scsi_Cmnd *request_q;
152 Scsi_Cmnd *request_qtail;
153 enum mesh_phase phase; /* what we're currently trying to do */
154 enum msg_phase msgphase;
155 int conn_tgt; /* target we're connected to */
156 Scsi_Cmnd *current_req; /* req we're currently working on */
168 struct dbdma_cmd *dma_cmds; /* space for dbdma commands, aligned */
170 struct mesh_target tgts[8];
172 struct device_node *ofnode;
173 struct pci_dev* pdev;
177 struct dbglog log[N_DBG_SLOG];
183 static void dlog(struct mesh_state *ms, char *fmt, int a);
184 static void dumplog(struct mesh_state *ms, int tgt);
185 static void dumpslog(struct mesh_state *ms);
188 static inline void dlog(struct mesh_state *ms, char *fmt, int a)
190 static inline void dumplog(struct mesh_state *ms, int tgt)
192 static inline void dumpslog(struct mesh_state *ms)
195 #endif /* MESH_DBG */
196 #define MKWORD(a, b, c, d) (((a) << 24) + ((b) << 16) + ((c) << 8) + (d))
198 static struct mesh_state *all_meshes;
200 static void mesh_init(struct mesh_state *);
201 static int mesh_notify_reboot(struct notifier_block *, unsigned long, void *);
202 static void mesh_dump_regs(struct mesh_state *);
203 static void mesh_start(struct mesh_state *);
204 static void mesh_start_cmd(struct mesh_state *, Scsi_Cmnd *);
205 static void add_sdtr_msg(struct mesh_state *);
206 static void set_sdtr(struct mesh_state *, int, int);
207 static void start_phase(struct mesh_state *);
208 static void get_msgin(struct mesh_state *);
209 static int msgin_length(struct mesh_state *);
210 static void cmd_complete(struct mesh_state *);
211 static void phase_mismatch(struct mesh_state *);
212 static void reselected(struct mesh_state *);
213 static void handle_reset(struct mesh_state *);
214 static void handle_error(struct mesh_state *);
215 static void handle_exception(struct mesh_state *);
216 static void mesh_interrupt(int, void *, struct pt_regs *);
217 static void do_mesh_interrupt(int, void *, struct pt_regs *);
218 static void handle_msgin(struct mesh_state *);
219 static void mesh_done(struct mesh_state *, int);
220 static void mesh_completed(struct mesh_state *, Scsi_Cmnd *);
221 static void set_dma_cmds(struct mesh_state *, Scsi_Cmnd *);
222 static void halt_dma(struct mesh_state *);
223 static int data_goes_out(Scsi_Cmnd *);
224 static void do_abort(struct mesh_state *ms);
225 static void set_mesh_power(struct mesh_state *ms, int state);
227 #ifdef CONFIG_PMAC_PBOOK
228 static int mesh_notify_sleep(struct pmu_sleep_notifier *self, int when);
229 static struct pmu_sleep_notifier mesh_sleep_notifier = {
235 static struct notifier_block mesh_notifier = {
242 mesh_detect(Scsi_Host_Template *tp)
244 struct device_node *mesh;
245 int nmeshes, tgt, *cfp, minper;
246 struct mesh_state *ms, **prev_statep;
247 struct Scsi_Host *mesh_host;
250 /* The SCSI layer is dumb ! It calls us with the IO request
251 * lock held and interrupts disabled here... This doesn't happen
254 spin_unlock_irq(&io_request_lock);
256 if (_machine == _MACH_Pmac) {
257 use_active_neg = (find_devices("mac-io") ? 0 : SEQ_ACTIVE_NEG);
260 use_active_neg = SEQ_ACTIVE_NEG;
263 /* Calculate sync rate from module parameters */
267 printk(KERN_INFO "mesh: configured for synchronous %d MB/s\n", sync_rate);
268 mesh_sync_period = 1000 / sync_rate; /* ns */
269 mesh_sync_offset = 15;
271 printk(KERN_INFO "mesh: configured for asynchronous\n");
274 prev_statep = &all_meshes;
276 * On powermacs, the MESH node has device_type "mesh".
277 * On chrp machines, its device_type is "scsi" with
278 * "chrp,mesh0" as its `compatible' property.
280 mesh = find_devices("mesh");
282 mesh = find_compatible_devices("scsi", "chrp,mesh0");
283 for (; mesh != 0; mesh = mesh->next) {
284 u8 pci_bus, pci_devfn;
285 struct pci_dev* pdev = NULL;
287 if (mesh->n_addrs != 2 || mesh->n_intrs != 2) {
288 printk(KERN_ERR "mesh: expected 2 addrs and 2 intrs"
289 " (got %d,%d)\n", mesh->n_addrs, mesh->n_intrs);
292 if (mesh->parent != NULL
293 && pci_device_from_OF_node(mesh->parent, &pci_bus,
295 pdev = pci_find_slot(pci_bus, pci_devfn);
297 printk(KERN_ERR "mesh: Can't locate PCI entry\n");
301 mesh_host = scsi_register(tp, sizeof(struct mesh_state));
302 if (mesh_host == 0) {
303 printk(KERN_ERR "mesh: couldn't register host");
306 mesh_host->unique_id = nmeshes;
308 note_scsi_host(mesh, mesh_host);
311 ms = (struct mesh_state *) mesh_host->hostdata;
313 panic("no mesh state");
314 memset(ms, 0, sizeof(*ms));
315 ms->host = mesh_host;
318 ms->mesh = (volatile struct mesh_regs *)
319 ioremap(mesh->addrs[0].address, 0x1000);
320 ms->dma = (volatile struct dbdma_regs *)
321 ioremap(mesh->addrs[1].address, 0x1000);
322 ms->meshintr = mesh->intrs[0].line;
323 ms->dmaintr = mesh->intrs[1].line;
325 /* Space for dma command list: +1 for stop command,
326 +1 to allow for aligning. */
327 dma_cmd_space = kmalloc((mesh_host->sg_tablesize + 2) *
328 sizeof(struct dbdma_cmd), GFP_KERNEL);
329 if (dma_cmd_space == 0)
330 panic("mesh: couldn't allocate dma command space");
331 ms->dma_cmds = (struct dbdma_cmd *) DBDMA_ALIGN(dma_cmd_space);
332 memset(ms->dma_cmds, 0, (mesh_host->sg_tablesize + 1)
333 * sizeof(struct dbdma_cmd));
334 ms->dma_cmd_space = dma_cmd_space;
337 for (tgt = 0; tgt < 8; ++tgt) {
338 ms->tgts[tgt].sdtr_state = do_sdtr;
339 ms->tgts[tgt].sync_params = ASYNC_PARAMS;
340 ms->tgts[tgt].current_req = 0;
343 prev_statep = &ms->next;
345 if ((cfp = (int *) get_property(mesh, "clock-frequency",
349 printk(KERN_INFO "mesh: assuming 50MHz clock frequency\n");
350 ms->clk_freq = 50000000;
352 /* The maximum sync rate is clock / 5; increase
353 mesh_sync_period if necessary. */
354 minper = 1000000000 / (ms->clk_freq / 5); /* ns */
355 if (mesh_sync_period < minper)
356 mesh_sync_period = minper;
358 set_mesh_power(ms, 1);
362 if (request_irq(ms->meshintr, do_mesh_interrupt, 0, "MESH", ms)) {
363 printk(KERN_ERR "MESH: can't get irq %d\n", ms->meshintr);
369 if ((_machine == _MACH_Pmac) && (nmeshes > 0)) {
370 #ifdef CONFIG_PMAC_PBOOK
371 pmu_register_sleep_notifier(&mesh_sleep_notifier);
372 #endif /* CONFIG_PMAC_PBOOK */
373 register_reboot_notifier(&mesh_notifier);
376 /* Return to the SCSI layer in the same state we got called */
377 spin_lock_irq(&io_request_lock);
383 mesh_release(struct Scsi_Host *host)
385 struct mesh_state *ms = (struct mesh_state *) host->hostdata;
390 iounmap((void *) ms->mesh);
392 iounmap((void *) ms->dma);
393 kfree(ms->dma_cmd_space);
394 free_irq(ms->meshintr, ms);
395 pmac_call_feature(PMAC_FTR_MESH_ENABLE, ms->ofnode, 0, 0);
400 set_mesh_power(struct mesh_state *ms, int state)
402 if (_machine != _MACH_Pmac)
405 pmac_call_feature(PMAC_FTR_MESH_ENABLE, ms->ofnode, 0, 1);
408 pmac_call_feature(PMAC_FTR_MESH_ENABLE, ms->ofnode, 0, 0);
413 #ifdef CONFIG_PMAC_PBOOK
415 * notify clients before sleep and reset bus afterwards
418 mesh_notify_sleep(struct pmu_sleep_notifier *self, int when)
420 struct mesh_state *ms;
423 case PBOOK_SLEEP_REQUEST:
424 /* XXX We should wait for current transactions and queue
425 * new ones that would be posted beyond this point
428 case PBOOK_SLEEP_REJECT:
431 case PBOOK_SLEEP_NOW:
432 for (ms = all_meshes; ms != 0; ms = ms->next) {
435 scsi_block_requests(ms->host);
436 spin_lock_irqsave(&io_request_lock, flags);
437 while(ms->phase != idle) {
438 spin_unlock_irqrestore(&io_request_lock, flags);
439 current->state = TASK_UNINTERRUPTIBLE;
441 spin_lock_irqsave(&io_request_lock, flags);
443 ms->phase = sleeping;
444 spin_unlock_irqrestore(&io_request_lock, flags);
445 disable_irq(ms->meshintr);
446 set_mesh_power(ms, 0);
450 for (ms = all_meshes; ms != 0; ms = ms->next) {
453 set_mesh_power(ms, 1);
455 spin_lock_irqsave(&io_request_lock, flags);
457 spin_unlock_irqrestore(&io_request_lock, flags);
458 enable_irq(ms->meshintr);
459 scsi_unblock_requests(ms->host);
463 return PBOOK_SLEEP_OK;
465 #endif /* CONFIG_PMAC_PBOOK */
468 * Called by midlayer with host locked to queue a new
472 mesh_queue(Scsi_Cmnd *cmd, void (*done)(Scsi_Cmnd *))
474 struct mesh_state *ms;
476 cmd->scsi_done = done;
477 cmd->host_scribble = NULL;
479 ms = (struct mesh_state *) cmd->host->hostdata;
481 if (ms->request_q == NULL)
484 ms->request_qtail->host_scribble = (void *) cmd;
485 ms->request_qtail = cmd;
487 if (ms->phase == idle)
493 /* Todo: here we can at least try to remove the command from the
494 * queue if it isn't connected yet, and for pending command, assert
495 * ATN until the bus gets freed.
498 mesh_abort(Scsi_Cmnd *cmd)
500 struct mesh_state *ms = (struct mesh_state *) cmd->host->hostdata;
502 printk(KERN_DEBUG "mesh_abort(%p)\n", cmd);
504 dumplog(ms, cmd->target);
506 return SCSI_ABORT_SNOOZE;
510 mesh_dump_regs(struct mesh_state *ms)
512 volatile struct mesh_regs *mr = ms->mesh;
513 volatile struct dbdma_regs *md = ms->dma;
515 struct mesh_target *tp;
517 printk(KERN_DEBUG "mesh: state at %p, regs at %p, dma at %p\n",
519 printk(KERN_DEBUG " ct=%4x seq=%2x bs=%4x fc=%2x "
520 "exc=%2x err=%2x im=%2x int=%2x sp=%2x\n",
521 (mr->count_hi << 8) + mr->count_lo, mr->sequence,
522 (mr->bus_status1 << 8) + mr->bus_status0, mr->fifo_count,
523 mr->exception, mr->error, mr->intr_mask, mr->interrupt,
525 while(in_8(&mr->fifo_count))
526 printk(KERN_DEBUG " fifo data=%.2x\n",in_8(&mr->fifo));
527 printk(KERN_DEBUG " dma stat=%x cmdptr=%x\n",
528 in_le32(&md->status), in_le32(&md->cmdptr));
529 printk(KERN_DEBUG " phase=%d msgphase=%d conn_tgt=%d data_ptr=%d\n",
530 ms->phase, ms->msgphase, ms->conn_tgt, ms->data_ptr);
531 printk(KERN_DEBUG " dma_st=%d dma_ct=%d n_msgout=%d\n",
532 ms->dma_started, ms->dma_count, ms->n_msgout);
533 for (t = 0; t < 8; ++t) {
535 if (tp->current_req == NULL)
537 printk(KERN_DEBUG " target %d: req=%p goes_out=%d saved_ptr=%d\n",
538 t, tp->current_req, tp->data_goes_out, tp->saved_ptr);
543 * Called by the midlayer with the lock held to reset the
545 * The midlayer will wait for devices to come back, we don't need
546 * to do that ourselves
549 mesh_host_reset(Scsi_Cmnd *cmd)
551 struct mesh_state *ms = (struct mesh_state *) cmd->host->hostdata;
552 volatile struct mesh_regs *mr = ms->mesh;
553 volatile struct dbdma_regs *md = ms->dma;
555 printk(KERN_DEBUG "mesh_host_reset\n");
557 /* Reset the controller & dbdma channel */
558 out_le32(&md->control, (RUN|PAUSE|FLUSH|WAKE) << 16); /* stop dma */
559 out_8(&mr->exception, 0xff); /* clear all exception bits */
560 out_8(&mr->error, 0xff); /* clear all error bits */
561 out_8(&mr->sequence, SEQ_RESETMESH);
563 out_8(&mr->intr_mask, INT_ERROR | INT_EXCEPTION | INT_CMDDONE);
564 out_8(&mr->source_id, ms->host->this_id);
565 out_8(&mr->sel_timeout, 25); /* 250ms */
566 out_8(&mr->sync_params, ASYNC_PARAMS);
569 out_8(&mr->bus_status1, BS1_RST); /* assert RST */
570 udelay(30); /* leave it on for >= 25us */
571 out_8(&mr->bus_status1, 0); /* negate RST */
573 /* Complete pending commands */
580 * If we leave drives set for synchronous transfers (especially
581 * CDROMs), and reboot to MacOS, it gets confused, poor thing.
582 * So, on reboot we reset the SCSI bus.
585 mesh_notify_reboot(struct notifier_block *this, unsigned long code, void *x)
587 struct mesh_state *ms;
588 volatile struct mesh_regs *mr;
590 if (code == SYS_DOWN) {
591 printk(KERN_INFO "resetting MESH scsi bus(es)\n");
592 for (ms = all_meshes; ms != 0; ms = ms->next) {
594 out_8(&mr->intr_mask, 0);
595 out_8(&mr->interrupt,
596 INT_ERROR | INT_EXCEPTION | INT_CMDDONE);
597 out_8(&mr->bus_status1, BS1_RST);
599 out_8(&mr->bus_status1, 0);
605 /* Called with meshinterrupt disabled, initialize the chipset
606 * and eventually do the initial bus reset. The lock must not be
607 * held since we can schedule.
610 mesh_init(struct mesh_state *ms)
612 volatile struct mesh_regs *mr = ms->mesh;
613 volatile struct dbdma_regs *md = ms->dma;
617 /* Reset controller */
618 out_le32(&md->control, (RUN|PAUSE|FLUSH|WAKE) << 16); /* stop dma */
619 out_8(&mr->exception, 0xff); /* clear all exception bits */
620 out_8(&mr->error, 0xff); /* clear all error bits */
621 out_8(&mr->sequence, SEQ_RESETMESH);
623 out_8(&mr->intr_mask, INT_ERROR | INT_EXCEPTION | INT_CMDDONE);
624 out_8(&mr->source_id, ms->host->this_id);
625 out_8(&mr->sel_timeout, 25); /* 250ms */
626 out_8(&mr->sync_params, ASYNC_PARAMS);
628 if (init_reset_delay) {
629 printk(KERN_INFO "mesh: performing initial bus reset...\n");
632 out_8(&mr->bus_status1, BS1_RST); /* assert RST */
633 udelay(30); /* leave it on for >= 25us */
634 out_8(&mr->bus_status1, 0); /* negate RST */
636 /* Wait for bus to come back */
637 current->state = TASK_UNINTERRUPTIBLE;
638 schedule_timeout((init_reset_delay * HZ) / 1000);
641 /* Reconfigure controller */
642 out_8(&mr->interrupt, 0xff); /* clear all interrupt bits */
643 out_8(&mr->sequence, SEQ_FLUSHFIFO);
645 out_8(&mr->sync_params, ASYNC_PARAMS);
646 out_8(&mr->sequence, SEQ_ENBRESEL);
649 ms->msgphase = msg_none;
653 * Start the next command for a MESH.
654 * Should be called with interrupts disabled.
657 mesh_start(struct mesh_state *ms)
659 Scsi_Cmnd *cmd, *prev, *next;
661 if (ms->phase != idle || ms->current_req != NULL) {
662 printk(KERN_ERR "inappropriate mesh_start (phase=%d, ms=%p)",
667 while (ms->phase == idle) {
669 for (cmd = ms->request_q; ; cmd = (Scsi_Cmnd *) cmd->host_scribble) {
672 if (ms->tgts[cmd->target].current_req == NULL)
676 next = (Scsi_Cmnd *) cmd->host_scribble;
678 ms->request_q = next;
680 prev->host_scribble = (void *) next;
682 ms->request_qtail = prev;
684 mesh_start_cmd(ms, cmd);
689 mesh_start_cmd(struct mesh_state *ms, Scsi_Cmnd *cmd)
691 volatile struct mesh_regs *mr = ms->mesh;
694 ms->current_req = cmd;
695 ms->tgts[cmd->target].data_goes_out = data_goes_out(cmd);
696 ms->tgts[cmd->target].current_req = cmd;
699 if (DEBUG_TARGET(cmd)) {
701 printk(KERN_DEBUG "mesh_start: %p ser=%lu tgt=%d cmd=",
702 cmd, cmd->serial_number, cmd->target);
703 for (i = 0; i < cmd->cmd_len; ++i)
704 printk(" %x", cmd->cmnd[i]);
705 printk(" use_sg=%d buffer=%p bufflen=%u\n",
706 cmd->use_sg, cmd->request_buffer, cmd->request_bufflen);
710 panic("mesh: double DMA start !\n");
712 ms->phase = arbitrating;
713 ms->msgphase = msg_none;
717 ms->last_n_msgout = 0;
718 ms->expect_reply = 0;
719 ms->conn_tgt = cmd->target;
720 ms->tgts[cmd->target].saved_ptr = 0;
724 ms->tgts[cmd->target].n_log = 0;
725 dlog(ms, "start cmd=%x", (int) cmd);
729 dlog(ms, "about to arb, intr/exc/err/fc=%.8x",
730 MKWORD(mr->interrupt, mr->exception, mr->error, mr->fifo_count));
731 out_8(&mr->interrupt, INT_CMDDONE);
732 out_8(&mr->sequence, SEQ_ENBRESEL);
735 if (mr->bus_status1 & (BS1_BSY | BS1_SEL)) {
737 * Some other device has the bus or is arbitrating for it -
738 * probably a target which is about to reselect us.
740 dlog(ms, "busy b4 arb, intr/exc/err/fc=%.8x",
741 MKWORD(mr->interrupt, mr->exception,
742 mr->error, mr->fifo_count));
743 for (t = 100; t > 0; --t) {
744 if ((mr->bus_status1 & (BS1_BSY | BS1_SEL)) == 0)
746 if (in_8(&mr->interrupt) != 0) {
747 dlog(ms, "intr b4 arb, intr/exc/err/fc=%.8x",
748 MKWORD(mr->interrupt, mr->exception,
749 mr->error, mr->fifo_count));
750 mesh_interrupt(0, (void *)ms, 0);
751 if (ms->phase != arbitrating)
756 if (mr->bus_status1 & (BS1_BSY | BS1_SEL)) {
757 /* XXX should try again in a little while */
758 ms->stat = DID_BUS_BUSY;
766 * Apparently the mesh has a bug where it will assert both its
767 * own bit and the target's bit on the bus during arbitration.
769 out_8(&mr->dest_id, mr->source_id);
772 * There appears to be a race with reselection sometimes,
773 * where a target reselects us just as we issue the
774 * arbitrate command. It seems that then the arbitrate
775 * command just hangs waiting for the bus to be free
776 * without giving us a reselection exception.
777 * The only way I have found to get it to respond correctly
778 * is this: disable reselection before issuing the arbitrate
779 * command, then after issuing it, if it looks like a target
780 * is trying to reselect us, reset the mesh and then enable
783 out_8(&mr->sequence, SEQ_DISRESEL);
784 if (in_8(&mr->interrupt) != 0) {
785 dlog(ms, "intr after disresel, intr/exc/err/fc=%.8x",
786 MKWORD(mr->interrupt, mr->exception,
787 mr->error, mr->fifo_count));
788 mesh_interrupt(0, (void *)ms, 0);
789 if (ms->phase != arbitrating)
791 dlog(ms, "after intr after disresel, intr/exc/err/fc=%.8x",
792 MKWORD(mr->interrupt, mr->exception,
793 mr->error, mr->fifo_count));
796 out_8(&mr->sequence, SEQ_ARBITRATE);
798 for (t = 230; t > 0; --t) {
799 if (in_8(&mr->interrupt) != 0)
803 dlog(ms, "after arb, intr/exc/err/fc=%.8x",
804 MKWORD(mr->interrupt, mr->exception, mr->error, mr->fifo_count));
805 if (mr->interrupt == 0 && (mr->bus_status1 & BS1_SEL)
806 && (mr->bus_status0 & BS0_IO)) {
807 /* looks like a reselection - try resetting the mesh */
808 dlog(ms, "resel? after arb, intr/exc/err/fc=%.8x",
809 MKWORD(mr->interrupt, mr->exception, mr->error, mr->fifo_count));
810 out_8(&mr->sequence, SEQ_RESETMESH);
812 out_8(&mr->interrupt, INT_ERROR | INT_EXCEPTION | INT_CMDDONE);
813 out_8(&mr->intr_mask, INT_ERROR | INT_EXCEPTION | INT_CMDDONE);
814 out_8(&mr->sequence, SEQ_ENBRESEL);
815 for (t = 10; t > 0 && mr->interrupt == 0; --t)
817 dlog(ms, "tried reset after arb, intr/exc/err/fc=%.8x",
818 MKWORD(mr->interrupt, mr->exception, mr->error, mr->fifo_count));
819 #ifndef MESH_MULTIPLE_HOSTS
820 if (mr->interrupt == 0 && (mr->bus_status1 & BS1_SEL)
821 && (mr->bus_status0 & BS0_IO)) {
822 printk(KERN_ERR "mesh: controller not responding"
823 " to reselection!\n");
825 * If this is a target reselecting us, and the
826 * mesh isn't responding, the higher levels of
827 * the scsi code will eventually time out and
836 add_sdtr_msg(struct mesh_state *ms)
838 int i = ms->n_msgout;
840 ms->msgout[i] = EXTENDED_MESSAGE;
842 ms->msgout[i+2] = EXTENDED_SDTR;
843 ms->msgout[i+3] = mesh_sync_period/4;
844 ms->msgout[i+4] = (ALLOW_SYNC(ms->conn_tgt)? mesh_sync_offset: 0);
845 ms->n_msgout = i + 5;
849 set_sdtr(struct mesh_state *ms, int period, int offset)
851 struct mesh_target *tp = &ms->tgts[ms->conn_tgt];
852 volatile struct mesh_regs *mr = ms->mesh;
855 tp->sdtr_state = sdtr_done;
858 if (SYNC_OFF(tp->sync_params))
859 printk(KERN_INFO "mesh: target %d now asynchronous\n",
861 tp->sync_params = ASYNC_PARAMS;
862 out_8(&mr->sync_params, ASYNC_PARAMS);
866 * We need to compute ceil(clk_freq * period / 500e6) - 2
867 * without incurring overflow.
869 v = (ms->clk_freq / 5000) * period;
871 /* special case: sync_period == 5 * clk_period */
873 /* units of tr are 100kB/s */
874 tr = (ms->clk_freq + 250000) / 500000;
876 /* sync_period == (v + 2) * 2 * clk_period */
877 v = (v + 99999) / 100000 - 2;
880 tr = ((ms->clk_freq / (v + 2)) + 199999) / 200000;
883 offset = 15; /* can't happen */
884 tp->sync_params = SYNC_PARAMS(offset, v);
885 out_8(&mr->sync_params, tp->sync_params);
886 printk(KERN_INFO "mesh: target %d synchronous at %d.%d MB/s\n",
887 ms->conn_tgt, tr/10, tr%10);
891 start_phase(struct mesh_state *ms)
894 volatile struct mesh_regs *mr = ms->mesh;
895 volatile struct dbdma_regs *md = ms->dma;
896 Scsi_Cmnd *cmd = ms->current_req;
897 struct mesh_target *tp = &ms->tgts[ms->conn_tgt];
899 dlog(ms, "start_phase nmo/exc/fc/seq = %.8x",
900 MKWORD(ms->n_msgout, mr->exception, mr->fifo_count, mr->sequence));
901 out_8(&mr->interrupt, INT_ERROR | INT_EXCEPTION | INT_CMDDONE);
902 seq = use_active_neg + (ms->n_msgout? SEQ_ATN: 0);
903 switch (ms->msgphase) {
908 out_8(&mr->count_hi, 0);
909 out_8(&mr->count_lo, 1);
910 out_8(&mr->sequence, SEQ_MSGIN + seq);
916 * To make sure ATN drops before we assert ACK for
917 * the last byte of the message, we have to do the
918 * last byte specially.
920 if (ms->n_msgout <= 0) {
921 printk(KERN_ERR "mesh: msg_out but n_msgout=%d\n",
924 ms->msgphase = msg_none;
927 if (ALLOW_DEBUG(ms->conn_tgt)) {
928 printk(KERN_DEBUG "mesh: sending %d msg bytes:",
930 for (i = 0; i < ms->n_msgout; ++i)
931 printk(" %x", ms->msgout[i]);
934 dlog(ms, "msgout msg=%.8x", MKWORD(ms->n_msgout, ms->msgout[0],
935 ms->msgout[1], ms->msgout[2]));
936 out_8(&mr->count_hi, 0);
937 out_8(&mr->sequence, SEQ_FLUSHFIFO);
940 * If ATN is not already asserted, we assert it, then
941 * issue a SEQ_MSGOUT to get the mesh to drop ACK.
943 if ((mr->bus_status0 & BS0_ATN) == 0) {
944 dlog(ms, "bus0 was %.2x explictly asserting ATN", mr->bus_status0);
945 out_8(&mr->bus_status0, BS0_ATN); /* explicit ATN */
947 out_8(&mr->count_lo, 1);
948 out_8(&mr->sequence, SEQ_MSGOUT + seq);
949 out_8(&mr->bus_status0, 0); /* release explicit ATN */
950 dlog(ms,"hace: after explicit ATN bus0=%.2x",mr->bus_status0);
952 if (ms->n_msgout == 1) {
954 * We can't issue the SEQ_MSGOUT without ATN
955 * until the target has asserted REQ. The logic
956 * in cmd_complete handles both situations:
957 * REQ already asserted or not.
961 out_8(&mr->count_lo, ms->n_msgout - 1);
962 out_8(&mr->sequence, SEQ_MSGOUT + seq);
963 for (i = 0; i < ms->n_msgout - 1; ++i)
964 out_8(&mr->fifo, ms->msgout[i]);
969 printk(KERN_ERR "mesh bug: start_phase msgphase=%d\n",
975 out_8(&mr->dest_id, ms->conn_tgt);
976 out_8(&mr->sequence, SEQ_SELECT + SEQ_ATN);
979 out_8(&mr->sync_params, tp->sync_params);
980 out_8(&mr->count_hi, 0);
982 out_8(&mr->count_lo, cmd->cmd_len);
983 out_8(&mr->sequence, SEQ_COMMAND + seq);
984 for (i = 0; i < cmd->cmd_len; ++i)
985 out_8(&mr->fifo, cmd->cmnd[i]);
987 out_8(&mr->count_lo, 6);
988 out_8(&mr->sequence, SEQ_COMMAND + seq);
989 for (i = 0; i < 6; ++i)
994 /* transfer data, if any */
995 if (!ms->dma_started) {
996 set_dma_cmds(ms, cmd);
997 out_le32(&md->cmdptr, virt_to_phys(ms->dma_cmds));
998 out_le32(&md->control, (RUN << 16) | RUN);
1004 ms->dma_count -= nb;
1006 out_8(&mr->count_lo, nb);
1007 out_8(&mr->count_hi, nb >> 8);
1008 out_8(&mr->sequence, (tp->data_goes_out?
1009 SEQ_DATAOUT: SEQ_DATAIN) + SEQ_DMA_MODE + seq);
1012 out_8(&mr->count_hi, 0);
1013 out_8(&mr->count_lo, 1);
1014 out_8(&mr->sequence, SEQ_STATUS + seq);
1018 out_8(&mr->sequence, SEQ_ENBRESEL);
1020 dlog(ms, "enbresel intr/exc/err/fc=%.8x",
1021 MKWORD(mr->interrupt, mr->exception, mr->error,
1023 out_8(&mr->sequence, SEQ_BUSFREE);
1026 printk(KERN_ERR "mesh: start_phase called with phase=%d\n",
1034 get_msgin(struct mesh_state *ms)
1036 volatile struct mesh_regs *mr = ms->mesh;
1042 ms->n_msgin = i + n;
1044 ms->msgin[i++] = in_8(&mr->fifo);
1049 msgin_length(struct mesh_state *ms)
1054 if (ms->n_msgin > 0) {
1057 /* extended message */
1058 n = ms->n_msgin < 2? 2: ms->msgin[1] + 2;
1059 } else if (0x20 <= b && b <= 0x2f) {
1060 /* 2-byte message */
1068 cmd_complete(struct mesh_state *ms)
1070 volatile struct mesh_regs *mr = ms->mesh;
1071 Scsi_Cmnd *cmd = ms->current_req;
1072 struct mesh_target *tp = &ms->tgts[ms->conn_tgt];
1075 dlog(ms, "cmd_complete fc=%x", mr->fifo_count);
1076 seq = use_active_neg + (ms->n_msgout? SEQ_ATN: 0);
1077 switch (ms->msgphase) {
1079 /* huh? we expected a phase mismatch */
1081 ms->msgphase = msg_in;
1085 /* should have some message bytes in fifo */
1087 n = msgin_length(ms);
1088 if (ms->n_msgin < n) {
1089 out_8(&mr->count_lo, n - ms->n_msgin);
1090 out_8(&mr->sequence, SEQ_MSGIN + seq);
1092 ms->msgphase = msg_none;
1099 out_8(&mr->sequence, SEQ_FLUSHFIFO);
1101 out_8(&mr->count_lo, 1);
1102 out_8(&mr->sequence, SEQ_MSGIN + SEQ_ATN + use_active_neg);
1107 * To get the right timing on ATN wrt ACK, we have
1108 * to get the MESH to drop ACK, wait until REQ gets
1109 * asserted, then drop ATN. To do this we first
1110 * issue a SEQ_MSGOUT with ATN and wait for REQ,
1111 * then change the command to a SEQ_MSGOUT w/o ATN.
1112 * If we don't see REQ in a reasonable time, we
1113 * change the command to SEQ_MSGIN with ATN,
1114 * wait for the phase mismatch interrupt, then
1115 * issue the SEQ_MSGOUT without ATN.
1117 out_8(&mr->count_lo, 1);
1118 out_8(&mr->sequence, SEQ_MSGOUT + use_active_neg + SEQ_ATN);
1119 t = 30; /* wait up to 30us */
1120 while ((mr->bus_status0 & BS0_REQ) == 0 && --t >= 0)
1122 dlog(ms, "last_mbyte err/exc/fc/cl=%.8x",
1123 MKWORD(mr->error, mr->exception,
1124 mr->fifo_count, mr->count_lo));
1125 if (in_8(&mr->interrupt) & (INT_ERROR | INT_EXCEPTION)) {
1126 /* whoops, target didn't do what we expected */
1127 ms->last_n_msgout = ms->n_msgout;
1129 if (in_8(&mr->interrupt) & INT_ERROR) {
1130 printk(KERN_ERR "mesh: error %x in msg_out\n",
1135 if (in_8(&mr->exception) != EXC_PHASEMM)
1136 printk(KERN_ERR "mesh: exc %x in msg_out\n",
1137 in_8(&mr->exception));
1139 printk(KERN_DEBUG "mesh: bs0=%x in msg_out\n",
1140 in_8(&mr->bus_status0));
1141 handle_exception(ms);
1144 if (mr->bus_status0 & BS0_REQ) {
1145 out_8(&mr->sequence, SEQ_MSGOUT + use_active_neg);
1147 out_8(&mr->fifo, ms->msgout[ms->n_msgout-1]);
1148 ms->msgphase = msg_out_last;
1150 out_8(&mr->sequence, SEQ_MSGIN + use_active_neg + SEQ_ATN);
1151 ms->msgphase = msg_out_xxx;
1156 ms->last_n_msgout = ms->n_msgout;
1158 ms->msgphase = ms->expect_reply? msg_in: msg_none;
1163 switch (ms->phase) {
1165 printk(KERN_ERR "mesh: interrupt in idle phase?\n");
1169 dlog(ms, "Selecting phase at command completion",0);
1170 ms->msgout[0] = IDENTIFY(ALLOW_RESEL(ms->conn_tgt),
1171 (cmd? cmd->lun: 0));
1173 ms->expect_reply = 0;
1175 ms->msgout[0] = ABORT;
1177 } else if (tp->sdtr_state == do_sdtr) {
1178 /* add SDTR message */
1180 ms->expect_reply = 1;
1181 tp->sdtr_state = sdtr_sent;
1183 ms->msgphase = msg_out;
1185 * We need to wait for REQ before dropping ATN.
1186 * We wait for at most 30us, then fall back to
1187 * a scheme where we issue a SEQ_COMMAND with ATN,
1188 * which will give us a phase mismatch interrupt
1189 * when REQ does come, and then we send the message.
1191 t = 230; /* wait up to 230us */
1192 while ((mr->bus_status0 & BS0_REQ) == 0) {
1194 dlog(ms, "impatient for req", ms->n_msgout);
1195 ms->msgphase = msg_none;
1202 if (ms->dma_count != 0) {
1207 * We can get a phase mismatch here if the target
1208 * changes to the status phase, even though we have
1209 * had a command complete interrupt. Then, if we
1210 * issue the SEQ_STATUS command, we'll get a sequence
1211 * error interrupt. Which isn't so bad except that
1212 * occasionally the mesh actually executes the
1213 * SEQ_STATUS *as well as* giving us the sequence
1214 * error and phase mismatch exception.
1216 out_8(&mr->sequence, 0);
1217 out_8(&mr->interrupt,
1218 INT_ERROR | INT_EXCEPTION | INT_CMDDONE);
1223 cmd->SCp.Status = mr->fifo;
1224 if (DEBUG_TARGET(cmd))
1225 printk(KERN_DEBUG "mesh: status is %x\n",
1228 ms->msgphase = msg_in;
1234 ms->current_req = 0;
1247 static void phase_mismatch(struct mesh_state *ms)
1249 volatile struct mesh_regs *mr = ms->mesh;
1252 dlog(ms, "phasemm ch/cl/seq/fc=%.8x",
1253 MKWORD(mr->count_hi, mr->count_lo, mr->sequence, mr->fifo_count));
1254 phase = mr->bus_status0 & BS0_PHASE;
1255 if (ms->msgphase == msg_out_xxx && phase == BP_MSGOUT) {
1256 /* output the last byte of the message, without ATN */
1257 out_8(&mr->count_lo, 1);
1258 out_8(&mr->sequence, SEQ_MSGOUT + use_active_neg);
1260 out_8(&mr->fifo, ms->msgout[ms->n_msgout-1]);
1261 ms->msgphase = msg_out_last;
1265 if (ms->msgphase == msg_in) {
1271 if (ms->dma_started)
1273 if (mr->fifo_count) {
1274 out_8(&mr->sequence, SEQ_FLUSHFIFO);
1278 ms->msgphase = msg_none;
1281 ms->tgts[ms->conn_tgt].data_goes_out = 0;
1282 ms->phase = dataing;
1285 ms->tgts[ms->conn_tgt].data_goes_out = 1;
1286 ms->phase = dataing;
1289 ms->phase = commanding;
1292 ms->phase = statusing;
1295 ms->msgphase = msg_in;
1299 ms->msgphase = msg_out;
1300 if (ms->n_msgout == 0) {
1304 if (ms->last_n_msgout == 0) {
1306 "mesh: no msg to repeat\n");
1307 ms->msgout[0] = NOP;
1308 ms->last_n_msgout = 1;
1310 ms->n_msgout = ms->last_n_msgout;
1315 printk(KERN_DEBUG "mesh: unknown scsi phase %x\n", phase);
1316 ms->stat = DID_ERROR;
1325 reselected(struct mesh_state *ms)
1327 volatile struct mesh_regs *mr = ms->mesh;
1329 struct mesh_target *tp;
1332 switch (ms->phase) {
1336 if ((cmd = ms->current_req) != NULL) {
1337 /* put the command back on the queue */
1338 cmd->host_scribble = (void *) ms->request_q;
1339 if (ms->request_q == NULL)
1340 ms->request_qtail = cmd;
1341 ms->request_q = cmd;
1342 tp = &ms->tgts[cmd->target];
1343 tp->current_req = NULL;
1347 ms->phase = reselecting;
1353 printk(KERN_ERR "mesh: reselected in phase %d/%d tgt %d\n",
1354 ms->msgphase, ms->phase, ms->conn_tgt);
1355 dumplog(ms, ms->conn_tgt);
1359 if (ms->dma_started) {
1360 printk(KERN_ERR "mesh: reselected with DMA started !\n");
1363 ms->current_req = NULL;
1364 ms->phase = dataing;
1365 ms->msgphase = msg_in;
1367 ms->last_n_msgout = 0;
1368 prev = ms->conn_tgt;
1371 * We seem to get abortive reselections sometimes.
1373 while ((mr->bus_status1 & BS1_BSY) == 0) {
1374 static int mesh_aborted_resels;
1375 mesh_aborted_resels++;
1376 out_8(&mr->interrupt, INT_ERROR | INT_EXCEPTION | INT_CMDDONE);
1378 out_8(&mr->sequence, SEQ_ENBRESEL);
1380 dlog(ms, "extra resel err/exc/fc = %.6x",
1381 MKWORD(0, mr->error, mr->exception, mr->fifo_count));
1383 out_8(&mr->interrupt, INT_ERROR | INT_EXCEPTION | INT_CMDDONE);
1385 out_8(&mr->sequence, SEQ_ENBRESEL);
1387 out_8(&mr->sync_params, ASYNC_PARAMS);
1390 * Find out who reselected us.
1392 if (mr->fifo_count == 0) {
1393 printk(KERN_ERR "mesh: reselection but nothing in fifo?\n");
1394 ms->conn_tgt = ms->host->this_id;
1397 /* get the last byte in the fifo */
1399 b = in_8(&mr->fifo);
1400 dlog(ms, "reseldata %x", b);
1401 } while (in_8(&mr->fifo_count));
1402 for (t = 0; t < 8; ++t)
1403 if ((b & (1 << t)) != 0 && t != ms->host->this_id)
1405 if (b != (1 << t) + (1 << ms->host->this_id)) {
1406 printk(KERN_ERR "mesh: bad reselection data %x\n", b);
1407 ms->conn_tgt = ms->host->this_id;
1413 * Set up to continue with that target's transfer.
1417 out_8(&mr->sync_params, tp->sync_params);
1418 if (ALLOW_DEBUG(t)) {
1419 printk(KERN_DEBUG "mesh: reselected by target %d\n", t);
1420 printk(KERN_DEBUG "mesh: saved_ptr=%x goes_out=%d cmd=%p\n",
1421 tp->saved_ptr, tp->data_goes_out, tp->current_req);
1423 ms->current_req = tp->current_req;
1424 if (tp->current_req == NULL) {
1425 printk(KERN_ERR "mesh: reselected by tgt %d but no cmd!\n", t);
1428 ms->data_ptr = tp->saved_ptr;
1429 dlog(ms, "resel prev tgt=%d", prev);
1430 dlog(ms, "resel err/exc=%.4x", MKWORD(0, 0, mr->error, mr->exception));
1435 dumplog(ms, ms->conn_tgt);
1442 static void do_abort(struct mesh_state *ms)
1444 ms->msgout[0] = ABORT;
1447 ms->stat = DID_ABORT;
1448 dlog(ms, "abort", 0);
1452 handle_reset(struct mesh_state *ms)
1455 struct mesh_target *tp;
1457 volatile struct mesh_regs *mr = ms->mesh;
1459 for (tgt = 0; tgt < 8; ++tgt) {
1460 tp = &ms->tgts[tgt];
1461 if ((cmd = tp->current_req) != NULL) {
1462 cmd->result = DID_RESET << 16;
1463 tp->current_req = NULL;
1464 mesh_completed(ms, cmd);
1466 ms->tgts[tgt].sdtr_state = do_sdtr;
1467 ms->tgts[tgt].sync_params = ASYNC_PARAMS;
1469 ms->current_req = NULL;
1470 while ((cmd = ms->request_q) != NULL) {
1471 ms->request_q = (Scsi_Cmnd *) cmd->host_scribble;
1472 cmd->result = DID_RESET << 16;
1473 mesh_completed(ms, cmd);
1476 ms->msgphase = msg_none;
1477 out_8(&mr->interrupt, INT_ERROR | INT_EXCEPTION | INT_CMDDONE);
1478 out_8(&mr->sequence, SEQ_FLUSHFIFO);
1480 out_8(&mr->sync_params, ASYNC_PARAMS);
1481 out_8(&mr->sequence, SEQ_ENBRESEL);
1485 do_mesh_interrupt(int irq, void *dev_id, struct pt_regs *ptregs)
1487 unsigned long flags;
1489 spin_lock_irqsave(&io_request_lock, flags);
1490 mesh_interrupt(irq, dev_id, ptregs);
1491 spin_unlock_irqrestore(&io_request_lock, flags);
1494 static void handle_error(struct mesh_state *ms)
1496 int err, exc, count;
1497 volatile struct mesh_regs *mr = ms->mesh;
1499 err = in_8(&mr->error);
1500 exc = in_8(&mr->exception);
1501 out_8(&mr->interrupt, INT_ERROR | INT_EXCEPTION | INT_CMDDONE);
1502 dlog(ms, "error err/exc/fc/cl=%.8x",
1503 MKWORD(err, exc, mr->fifo_count, mr->count_lo));
1504 if (err & ERR_SCSIRESET) {
1505 /* SCSI bus was reset */
1506 printk(KERN_INFO "mesh: SCSI bus reset detected: "
1507 "waiting for end...");
1508 while ((mr->bus_status1 & BS1_RST) != 0)
1512 /* request_q is empty, no point in mesh_start() */
1515 if (err & ERR_UNEXPDISC) {
1516 /* Unexpected disconnect */
1517 if (exc & EXC_RESELECTED) {
1521 if (!ms->aborting) {
1522 printk(KERN_WARNING "mesh: target %d aborted\n",
1524 dumplog(ms, ms->conn_tgt);
1527 out_8(&mr->interrupt, INT_CMDDONE);
1528 ms->stat = DID_ABORT;
1532 if (err & ERR_PARITY) {
1533 if (ms->msgphase == msg_in) {
1534 printk(KERN_ERR "mesh: msg parity error, target %d\n",
1536 ms->msgout[0] = MSG_PARITY_ERROR;
1538 ms->msgphase = msg_in_bad;
1542 if (ms->stat == DID_OK) {
1543 printk(KERN_ERR "mesh: parity error, target %d\n",
1545 ms->stat = DID_PARITY;
1547 count = (mr->count_hi << 8) + mr->count_lo;
1551 /* reissue the data transfer command */
1552 out_8(&mr->sequence, mr->sequence);
1556 if (err & ERR_SEQERR) {
1557 if (exc & EXC_RESELECTED) {
1558 /* This can happen if we issue a command to
1559 get the bus just after the target reselects us. */
1560 static int mesh_resel_seqerr;
1561 mesh_resel_seqerr++;
1565 if (exc == EXC_PHASEMM) {
1566 static int mesh_phasemm_seqerr;
1567 mesh_phasemm_seqerr++;
1571 printk(KERN_ERR "mesh: sequence error (err=%x exc=%x)\n",
1574 printk(KERN_ERR "mesh: unknown error %x (exc=%x)\n", err, exc);
1577 dumplog(ms, ms->conn_tgt);
1578 if (ms->phase > selecting && (mr->bus_status1 & BS1_BSY)) {
1579 /* try to do what the target wants */
1584 ms->stat = DID_ERROR;
1588 static void handle_exception(struct mesh_state *ms)
1591 volatile struct mesh_regs *mr = ms->mesh;
1593 exc = in_8(&mr->exception);
1594 out_8(&mr->interrupt, INT_EXCEPTION | INT_CMDDONE);
1595 if (exc & EXC_RESELECTED) {
1596 static int mesh_resel_exc;
1599 } else if (exc == EXC_ARBLOST) {
1600 printk(KERN_DEBUG "mesh: lost arbitration\n");
1601 ms->stat = DID_BUS_BUSY;
1603 } else if (exc == EXC_SELTO) {
1604 /* selection timed out */
1605 ms->stat = DID_BAD_TARGET;
1607 } else if (exc == EXC_PHASEMM) {
1608 /* target wants to do something different:
1609 find out what it wants and do it. */
1612 printk(KERN_ERR "mesh: can't cope with exception %x\n", exc);
1614 dumplog(ms, ms->conn_tgt);
1621 mesh_interrupt(int irq, void *dev_id, struct pt_regs *ptregs)
1623 struct mesh_state *ms = (struct mesh_state *) dev_id;
1624 volatile struct mesh_regs *mr = ms->mesh;
1628 if (ALLOW_DEBUG(ms->conn_tgt))
1629 printk(KERN_DEBUG "mesh_intr, bs0=%x int=%x exc=%x err=%x "
1630 "phase=%d msgphase=%d\n", mr->bus_status0,
1631 mr->interrupt, mr->exception, mr->error,
1632 ms->phase, ms->msgphase);
1634 while ((intr = in_8(&mr->interrupt)) != 0) {
1635 dlog(ms, "interrupt intr/err/exc/seq=%.8x",
1636 MKWORD(intr, mr->error, mr->exception, mr->sequence));
1637 if (intr & INT_ERROR) {
1639 } else if (intr & INT_EXCEPTION) {
1640 handle_exception(ms);
1641 } else if (intr & INT_CMDDONE) {
1642 out_8(&mr->interrupt, INT_CMDDONE);
1649 handle_msgin(struct mesh_state *ms)
1652 Scsi_Cmnd *cmd = ms->current_req;
1653 struct mesh_target *tp = &ms->tgts[ms->conn_tgt];
1655 if (ms->n_msgin == 0)
1657 code = ms->msgin[0];
1658 if (ALLOW_DEBUG(ms->conn_tgt)) {
1659 printk(KERN_DEBUG "got %d message bytes:", ms->n_msgin);
1660 for (i = 0; i < ms->n_msgin; ++i)
1661 printk(" %x", ms->msgin[i]);
1664 dlog(ms, "msgin msg=%.8x",
1665 MKWORD(ms->n_msgin, code, ms->msgin[1], ms->msgin[2]));
1667 ms->expect_reply = 0;
1669 if (ms->n_msgin < msgin_length(ms))
1672 cmd->SCp.Message = code;
1674 case COMMAND_COMPLETE:
1676 case EXTENDED_MESSAGE:
1677 switch (ms->msgin[2]) {
1678 case EXTENDED_MODIFY_DATA_POINTER:
1679 ms->data_ptr += (ms->msgin[3] << 24) + ms->msgin[6]
1680 + (ms->msgin[4] << 16) + (ms->msgin[5] << 8);
1683 if (tp->sdtr_state != sdtr_sent) {
1684 /* reply with an SDTR */
1686 /* limit period to at least his value,
1687 offset to no more than his */
1688 if (ms->msgout[3] < ms->msgin[3])
1689 ms->msgout[3] = ms->msgin[3];
1690 if (ms->msgout[4] > ms->msgin[4])
1691 ms->msgout[4] = ms->msgin[4];
1692 set_sdtr(ms, ms->msgout[3], ms->msgout[4]);
1693 ms->msgphase = msg_out;
1695 set_sdtr(ms, ms->msgin[3], ms->msgin[4]);
1703 tp->saved_ptr = ms->data_ptr;
1705 case RESTORE_POINTERS:
1706 ms->data_ptr = tp->saved_ptr;
1709 ms->phase = disconnecting;
1713 case MESSAGE_REJECT:
1714 if (tp->sdtr_state == sdtr_sent)
1720 if (IDENTIFY_BASE <= code && code <= IDENTIFY_BASE + 7) {
1723 ms->msgphase = msg_out;
1724 } else if (code != cmd->lun + IDENTIFY_BASE) {
1725 printk(KERN_WARNING "mesh: lun mismatch "
1726 "(%d != %d) on reselection from "
1727 "target %d\n", i, cmd->lun,
1737 printk(KERN_WARNING "mesh: rejecting message from target %d:",
1739 for (i = 0; i < ms->n_msgin; ++i)
1740 printk(" %x", ms->msgin[i]);
1742 ms->msgout[0] = MESSAGE_REJECT;
1744 ms->msgphase = msg_out;
1748 mesh_done(struct mesh_state *ms, int start_next)
1751 struct mesh_target *tp = &ms->tgts[ms->conn_tgt];
1753 cmd = ms->current_req;
1754 ms->current_req = 0;
1755 tp->current_req = 0;
1757 cmd->result = (ms->stat << 16) + cmd->SCp.Status;
1758 if (ms->stat == DID_OK)
1759 cmd->result += (cmd->SCp.Message << 8);
1760 if (DEBUG_TARGET(cmd)) {
1761 printk(KERN_DEBUG "mesh_done: result = %x, data_ptr=%d, buflen=%d\n",
1762 cmd->result, ms->data_ptr, cmd->request_bufflen);
1763 if ((cmd->cmnd[0] == 0 || cmd->cmnd[0] == 0x12 || cmd->cmnd[0] == 3)
1764 && cmd->request_buffer != 0) {
1765 unsigned char *b = cmd->request_buffer;
1766 printk(KERN_DEBUG "buffer = %x %x %x %x %x %x %x %x\n",
1767 b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]);
1770 cmd->SCp.this_residual -= ms->data_ptr;
1771 mesh_completed(ms, cmd);
1774 out_8(&ms->mesh->sequence, SEQ_ENBRESEL);
1782 mesh_completed(struct mesh_state *ms, Scsi_Cmnd *cmd)
1784 (*cmd->scsi_done)(cmd);
1788 * Set up DMA commands for transferring data.
1791 set_dma_cmds(struct mesh_state *ms, Scsi_Cmnd *cmd)
1793 int i, dma_cmd, total, off, dtot;
1794 struct scatterlist *scl;
1795 struct dbdma_cmd *dcmds;
1797 dma_cmd = ms->tgts[ms->conn_tgt].data_goes_out?
1798 OUTPUT_MORE: INPUT_MORE;
1799 dcmds = ms->dma_cmds;
1802 cmd->SCp.this_residual = cmd->request_bufflen;
1803 if (cmd->use_sg > 0) {
1806 scl = (struct scatterlist *) cmd->buffer;
1808 nseg = pci_map_sg(ms->pdev, scl, cmd->use_sg,
1809 scsi_to_pci_dma_dir(cmd ->sc_data_direction));
1810 for (i = 0; i <nseg; ++i, ++scl) {
1811 u32 dma_addr = sg_dma_address(scl);
1812 u32 dma_len = sg_dma_len(scl);
1814 total += scl->length;
1815 if (off >= dma_len) {
1819 if (dma_len > 0xffff)
1820 panic("mesh: scatterlist element >= 64k");
1821 st_le16(&dcmds->req_count, dma_len - off);
1822 st_le16(&dcmds->command, dma_cmd);
1823 st_le32(&dcmds->phy_addr, dma_addr + off);
1824 dcmds->xfer_status = 0;
1826 dtot += dma_len - off;
1829 } else if (ms->data_ptr < cmd->request_bufflen) {
1830 dtot = cmd->request_bufflen - ms->data_ptr;
1832 panic("mesh: transfer size >= 64k");
1833 st_le16(&dcmds->req_count, dtot);
1834 /* XXX Use pci DMA API here ... */
1835 st_le32(&dcmds->phy_addr,
1836 virt_to_phys(cmd->request_buffer) + ms->data_ptr);
1837 dcmds->xfer_status = 0;
1842 /* Either the target has overrun our buffer,
1843 or the caller didn't provide a buffer. */
1844 static char mesh_extra_buf[64];
1846 dtot = sizeof(mesh_extra_buf);
1847 st_le16(&dcmds->req_count, dtot);
1848 st_le32(&dcmds->phy_addr, virt_to_phys(mesh_extra_buf));
1849 dcmds->xfer_status = 0;
1852 dma_cmd += OUTPUT_LAST - OUTPUT_MORE;
1853 st_le16(&dcmds[-1].command, dma_cmd);
1854 memset(dcmds, 0, sizeof(*dcmds));
1855 st_le16(&dcmds->command, DBDMA_STOP);
1856 ms->dma_count = dtot;
1860 halt_dma(struct mesh_state *ms)
1862 volatile struct dbdma_regs *md = ms->dma;
1863 volatile struct mesh_regs *mr = ms->mesh;
1864 Scsi_Cmnd *cmd = ms->current_req;
1867 if (!ms->tgts[ms->conn_tgt].data_goes_out) {
1868 /* wait a little while until the fifo drains */
1870 while (t > 0 && mr->fifo_count != 0
1871 && (in_le32(&md->status) & ACTIVE) != 0) {
1876 out_le32(&md->control, RUN << 16); /* turn off RUN bit */
1877 nb = (mr->count_hi << 8) + mr->count_lo;
1878 dlog(ms, "halt_dma fc/count=%.6x",
1879 MKWORD(0, mr->fifo_count, 0, nb));
1880 if (ms->tgts[ms->conn_tgt].data_goes_out)
1881 nb += mr->fifo_count;
1882 /* nb is the number of bytes not yet transferred
1883 to/from the target. */
1885 dlog(ms, "data_ptr %x", ms->data_ptr);
1886 if (ms->data_ptr < 0) {
1887 printk(KERN_ERR "mesh: halt_dma: data_ptr=%d (nb=%d, ms=%p)\n",
1888 ms->data_ptr, nb, ms);
1891 dumplog(ms, ms->conn_tgt);
1893 #endif /* MESH_DBG */
1894 } else if (cmd && cmd->request_bufflen != 0 &&
1895 ms->data_ptr > cmd->request_bufflen) {
1896 printk(KERN_DEBUG "mesh: target %d overrun, "
1897 "data_ptr=%x total=%x goes_out=%d\n",
1898 ms->conn_tgt, ms->data_ptr, cmd->request_bufflen,
1899 ms->tgts[ms->conn_tgt].data_goes_out);
1901 if (cmd->use_sg != 0) {
1902 struct scatterlist *sg;
1903 sg = (struct scatterlist *)cmd->request_buffer;
1904 pci_unmap_sg(ms->pdev, sg, cmd->use_sg,
1905 scsi_to_pci_dma_dir(cmd->sc_data_direction));
1907 ms->dma_started = 0;
1911 * Work out whether we expect data to go out from the host adaptor or into it.
1912 * (If this information is available from somewhere else in the scsi
1913 * code, somebody please let me know :-)
1916 data_goes_out(Scsi_Cmnd *cmd)
1918 switch (cmd->cmnd[0]) {
1919 case CHANGE_DEFINITION:
1927 case MODE_SELECT_10:
1928 case REASSIGN_BLOCKS:
1931 case SEARCH_EQUAL_12:
1933 case SEARCH_HIGH_12:
1936 case SEND_DIAGNOSTIC:
1937 case SEND_VOLUME_TAG:
1945 case WRITE_LONG_2: /* alternate code for WRITE_LONG */
1948 case WRITE_VERIFY_12:
1956 static inline u32 readtb(void)
1961 /* Beware: if you enable this, it will crash on 601s. */
1962 asm ("mftb %0" : "=r" (tb) : );
1969 static void dlog(struct mesh_state *ms, char *fmt, int a)
1971 struct mesh_target *tp = &ms->tgts[ms->conn_tgt];
1972 struct dbglog *tlp, *slp;
1974 tlp = &tp->log[tp->log_ix];
1975 slp = &ms->log[ms->log_ix];
1978 tlp->phase = (ms->msgphase << 4) + ms->phase;
1979 tlp->bs0 = ms->mesh->bus_status0;
1980 tlp->bs1 = ms->mesh->bus_status1;
1981 tlp->tgt = ms->conn_tgt;
1984 if (++tp->log_ix >= N_DBG_LOG)
1986 if (tp->n_log < N_DBG_LOG)
1988 if (++ms->log_ix >= N_DBG_SLOG)
1990 if (ms->n_log < N_DBG_SLOG)
1994 static void dumplog(struct mesh_state *ms, int t)
1996 struct mesh_target *tp = &ms->tgts[t];
2002 i = tp->log_ix - tp->n_log;
2008 printk(KERN_DEBUG "mesh log %d: bs=%.2x%.2x ph=%.2x ",
2009 t, lp->bs1, lp->bs0, lp->phase);
2011 printk("tb=%10u ", lp->tb);
2013 printk(lp->fmt, lp->d);
2015 if (++i >= N_DBG_LOG)
2017 } while (i != tp->log_ix);
2020 static void dumpslog(struct mesh_state *ms)
2027 i = ms->log_ix - ms->n_log;
2033 printk(KERN_DEBUG "mesh log: bs=%.2x%.2x ph=%.2x t%d ",
2034 lp->bs1, lp->bs0, lp->phase, lp->tgt);
2036 printk("tb=%10u ", lp->tb);
2038 printk(lp->fmt, lp->d);
2040 if (++i >= N_DBG_SLOG)
2042 } while (i != ms->log_ix);
2044 #endif /* MESH_DBG */
2046 static Scsi_Host_Template driver_template = SCSI_MESH;
2048 #include "scsi_module.c"