1 // SPDX-License-Identifier: GPL-2.0-only
3 * SCSI low-level driver for the MESH (Macintosh Enhanced SCSI Hardware)
4 * bus adaptor found on Power Macintosh computers.
5 * We assume the MESH is connected to a DBDMA (descriptor-based DMA)
8 * Paul Mackerras, August 1996.
9 * Copyright (C) 1996 Paul Mackerras.
11 * Apr. 21 2002 - BenH Rework bus reset code for new error handler
12 * Add delay after initial bus reset
13 * Add module parameters
15 * Sep. 27 2003 - BenH Move to new driver model, fix some write posting
18 * - handle aborts correctly
19 * - retry arbitration if lost (unless higher levels do this for us)
20 * - power down the chip when no device is detected
22 #include <linux/module.h>
23 #include <linux/kernel.h>
24 #include <linux/delay.h>
25 #include <linux/types.h>
26 #include <linux/string.h>
27 #include <linux/blkdev.h>
28 #include <linux/proc_fs.h>
29 #include <linux/stat.h>
30 #include <linux/interrupt.h>
31 #include <linux/reboot.h>
32 #include <linux/spinlock.h>
33 #include <linux/pci.h>
34 #include <linux/pgtable.h>
35 #include <asm/dbdma.h>
39 #include <asm/hydra.h>
40 #include <asm/processor.h>
41 #include <asm/setup.h>
42 #include <asm/pmac_feature.h>
43 #include <asm/macio.h>
45 #include <scsi/scsi.h>
46 #include <scsi/scsi_cmnd.h>
47 #include <scsi/scsi_device.h>
48 #include <scsi/scsi_host.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 static int sync_rate = CONFIG_SCSI_MESH_SYNC_RATE;
62 static int sync_targets = 0xff;
63 static int resel_targets = 0xff;
64 static int debug_targets = 0; /* print debug for these targets */
65 static int init_reset_delay = CONFIG_SCSI_MESH_RESET_DELAY_MS;
67 module_param(sync_rate, int, 0);
68 MODULE_PARM_DESC(sync_rate, "Synchronous rate (0..10, 0=async)");
69 module_param(sync_targets, int, 0);
70 MODULE_PARM_DESC(sync_targets, "Bitmask of targets allowed to set synchronous");
71 module_param(resel_targets, int, 0);
72 MODULE_PARM_DESC(resel_targets, "Bitmask of targets allowed to set disconnect");
73 module_param(debug_targets, int, 0644);
74 MODULE_PARM_DESC(debug_targets, "Bitmask of debugged targets");
75 module_param(init_reset_delay, int, 0);
76 MODULE_PARM_DESC(init_reset_delay, "Initial bus reset delay (0=no reset)");
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)->device->id))
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 struct scsi_cmnd *current_req;
140 struct dbglog log[N_DBG_LOG];
145 volatile struct mesh_regs __iomem *mesh;
147 volatile struct dbdma_regs __iomem *dma;
149 struct Scsi_Host *host;
150 struct mesh_state *next;
151 struct scsi_cmnd *request_q;
152 struct 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 struct scsi_cmnd *current_req; /* req we're currently working on */
168 struct dbdma_cmd *dma_cmds; /* space for dbdma commands, aligned */
169 dma_addr_t dma_cmd_bus;
173 struct mesh_target tgts[8];
174 struct macio_dev *mdev;
175 struct pci_dev* pdev;
179 struct dbglog log[N_DBG_SLOG];
184 * Driver is too messy, we need a few prototypes...
186 static void mesh_done(struct mesh_state *ms, int start_next);
187 static void mesh_interrupt(struct mesh_state *ms);
188 static void cmd_complete(struct mesh_state *ms);
189 static void set_dma_cmds(struct mesh_state *ms, struct scsi_cmnd *cmd);
190 static void halt_dma(struct mesh_state *ms);
191 static void phase_mismatch(struct mesh_state *ms);
195 * Some debugging & logging routines
200 static inline u32 readtb(void)
205 /* Beware: if you enable this, it will crash on 601s. */
206 asm ("mftb %0" : "=r" (tb) : );
213 static void dlog(struct mesh_state *ms, char *fmt, int a)
215 struct mesh_target *tp = &ms->tgts[ms->conn_tgt];
216 struct dbglog *tlp, *slp;
218 tlp = &tp->log[tp->log_ix];
219 slp = &ms->log[ms->log_ix];
222 tlp->phase = (ms->msgphase << 4) + ms->phase;
223 tlp->bs0 = ms->mesh->bus_status0;
224 tlp->bs1 = ms->mesh->bus_status1;
225 tlp->tgt = ms->conn_tgt;
228 if (++tp->log_ix >= N_DBG_LOG)
230 if (tp->n_log < N_DBG_LOG)
232 if (++ms->log_ix >= N_DBG_SLOG)
234 if (ms->n_log < N_DBG_SLOG)
238 static void dumplog(struct mesh_state *ms, int t)
240 struct mesh_target *tp = &ms->tgts[t];
246 i = tp->log_ix - tp->n_log;
252 printk(KERN_DEBUG "mesh log %d: bs=%.2x%.2x ph=%.2x ",
253 t, lp->bs1, lp->bs0, lp->phase);
255 printk("tb=%10u ", lp->tb);
257 printk(lp->fmt, lp->d);
259 if (++i >= N_DBG_LOG)
261 } while (i != tp->log_ix);
264 static void dumpslog(struct mesh_state *ms)
271 i = ms->log_ix - ms->n_log;
277 printk(KERN_DEBUG "mesh log: bs=%.2x%.2x ph=%.2x t%d ",
278 lp->bs1, lp->bs0, lp->phase, lp->tgt);
280 printk("tb=%10u ", lp->tb);
282 printk(lp->fmt, lp->d);
284 if (++i >= N_DBG_SLOG)
286 } while (i != ms->log_ix);
291 static inline void dlog(struct mesh_state *ms, char *fmt, int a)
293 static inline void dumplog(struct mesh_state *ms, int tgt)
295 static inline void dumpslog(struct mesh_state *ms)
298 #endif /* MESH_DBG */
300 #define MKWORD(a, b, c, d) (((a) << 24) + ((b) << 16) + ((c) << 8) + (d))
303 mesh_dump_regs(struct mesh_state *ms)
305 volatile struct mesh_regs __iomem *mr = ms->mesh;
306 volatile struct dbdma_regs __iomem *md = ms->dma;
308 struct mesh_target *tp;
310 printk(KERN_DEBUG "mesh: state at %p, regs at %p, dma at %p\n",
312 printk(KERN_DEBUG " ct=%4x seq=%2x bs=%4x fc=%2x "
313 "exc=%2x err=%2x im=%2x int=%2x sp=%2x\n",
314 (mr->count_hi << 8) + mr->count_lo, mr->sequence,
315 (mr->bus_status1 << 8) + mr->bus_status0, mr->fifo_count,
316 mr->exception, mr->error, mr->intr_mask, mr->interrupt,
318 while(in_8(&mr->fifo_count))
319 printk(KERN_DEBUG " fifo data=%.2x\n",in_8(&mr->fifo));
320 printk(KERN_DEBUG " dma stat=%x cmdptr=%x\n",
321 in_le32(&md->status), in_le32(&md->cmdptr));
322 printk(KERN_DEBUG " phase=%d msgphase=%d conn_tgt=%d data_ptr=%d\n",
323 ms->phase, ms->msgphase, ms->conn_tgt, ms->data_ptr);
324 printk(KERN_DEBUG " dma_st=%d dma_ct=%d n_msgout=%d\n",
325 ms->dma_started, ms->dma_count, ms->n_msgout);
326 for (t = 0; t < 8; ++t) {
328 if (tp->current_req == NULL)
330 printk(KERN_DEBUG " target %d: req=%p goes_out=%d saved_ptr=%d\n",
331 t, tp->current_req, tp->data_goes_out, tp->saved_ptr);
337 * Flush write buffers on the bus path to the mesh
339 static inline void mesh_flush_io(volatile struct mesh_regs __iomem *mr)
341 (void)in_8(&mr->mesh_id);
345 /* Called with meshinterrupt disabled, initialize the chipset
346 * and eventually do the initial bus reset. The lock must not be
347 * held since we can schedule.
349 static void mesh_init(struct mesh_state *ms)
351 volatile struct mesh_regs __iomem *mr = ms->mesh;
352 volatile struct dbdma_regs __iomem *md = ms->dma;
357 /* Reset controller */
358 out_le32(&md->control, (RUN|PAUSE|FLUSH|WAKE) << 16); /* stop dma */
359 out_8(&mr->exception, 0xff); /* clear all exception bits */
360 out_8(&mr->error, 0xff); /* clear all error bits */
361 out_8(&mr->sequence, SEQ_RESETMESH);
364 out_8(&mr->intr_mask, INT_ERROR | INT_EXCEPTION | INT_CMDDONE);
365 out_8(&mr->source_id, ms->host->this_id);
366 out_8(&mr->sel_timeout, 25); /* 250ms */
367 out_8(&mr->sync_params, ASYNC_PARAMS);
369 if (init_reset_delay) {
370 printk(KERN_INFO "mesh: performing initial bus reset...\n");
373 out_8(&mr->bus_status1, BS1_RST); /* assert RST */
375 udelay(30); /* leave it on for >= 25us */
376 out_8(&mr->bus_status1, 0); /* negate RST */
379 /* Wait for bus to come back */
380 msleep(init_reset_delay);
383 /* Reconfigure controller */
384 out_8(&mr->interrupt, 0xff); /* clear all interrupt bits */
385 out_8(&mr->sequence, SEQ_FLUSHFIFO);
388 out_8(&mr->sync_params, ASYNC_PARAMS);
389 out_8(&mr->sequence, SEQ_ENBRESEL);
392 ms->msgphase = msg_none;
396 static void mesh_start_cmd(struct mesh_state *ms, struct scsi_cmnd *cmd)
398 volatile struct mesh_regs __iomem *mr = ms->mesh;
401 id = cmd->device->id;
402 ms->current_req = cmd;
403 ms->tgts[id].data_goes_out = cmd->sc_data_direction == DMA_TO_DEVICE;
404 ms->tgts[id].current_req = cmd;
407 if (DEBUG_TARGET(cmd)) {
409 printk(KERN_DEBUG "mesh_start: %p tgt=%d cmd=", cmd, id);
410 for (i = 0; i < cmd->cmd_len; ++i)
411 printk(" %x", cmd->cmnd[i]);
412 printk(" use_sg=%d buffer=%p bufflen=%u\n",
413 scsi_sg_count(cmd), scsi_sglist(cmd), scsi_bufflen(cmd));
417 panic("mesh: double DMA start !\n");
419 ms->phase = arbitrating;
420 ms->msgphase = msg_none;
424 ms->last_n_msgout = 0;
425 ms->expect_reply = 0;
427 ms->tgts[id].saved_ptr = 0;
431 ms->tgts[id].n_log = 0;
432 dlog(ms, "start cmd=%x", (int) cmd);
436 dlog(ms, "about to arb, intr/exc/err/fc=%.8x",
437 MKWORD(mr->interrupt, mr->exception, mr->error, mr->fifo_count));
438 out_8(&mr->interrupt, INT_CMDDONE);
439 out_8(&mr->sequence, SEQ_ENBRESEL);
443 if (in_8(&mr->bus_status1) & (BS1_BSY | BS1_SEL)) {
445 * Some other device has the bus or is arbitrating for it -
446 * probably a target which is about to reselect us.
448 dlog(ms, "busy b4 arb, intr/exc/err/fc=%.8x",
449 MKWORD(mr->interrupt, mr->exception,
450 mr->error, mr->fifo_count));
451 for (t = 100; t > 0; --t) {
452 if ((in_8(&mr->bus_status1) & (BS1_BSY | BS1_SEL)) == 0)
454 if (in_8(&mr->interrupt) != 0) {
455 dlog(ms, "intr b4 arb, intr/exc/err/fc=%.8x",
456 MKWORD(mr->interrupt, mr->exception,
457 mr->error, mr->fifo_count));
459 if (ms->phase != arbitrating)
464 if (in_8(&mr->bus_status1) & (BS1_BSY | BS1_SEL)) {
465 /* XXX should try again in a little while */
466 ms->stat = DID_BUS_BUSY;
474 * Apparently the mesh has a bug where it will assert both its
475 * own bit and the target's bit on the bus during arbitration.
477 out_8(&mr->dest_id, mr->source_id);
480 * There appears to be a race with reselection sometimes,
481 * where a target reselects us just as we issue the
482 * arbitrate command. It seems that then the arbitrate
483 * command just hangs waiting for the bus to be free
484 * without giving us a reselection exception.
485 * The only way I have found to get it to respond correctly
486 * is this: disable reselection before issuing the arbitrate
487 * command, then after issuing it, if it looks like a target
488 * is trying to reselect us, reset the mesh and then enable
491 out_8(&mr->sequence, SEQ_DISRESEL);
492 if (in_8(&mr->interrupt) != 0) {
493 dlog(ms, "intr after disresel, intr/exc/err/fc=%.8x",
494 MKWORD(mr->interrupt, mr->exception,
495 mr->error, mr->fifo_count));
497 if (ms->phase != arbitrating)
499 dlog(ms, "after intr after disresel, intr/exc/err/fc=%.8x",
500 MKWORD(mr->interrupt, mr->exception,
501 mr->error, mr->fifo_count));
504 out_8(&mr->sequence, SEQ_ARBITRATE);
506 for (t = 230; t > 0; --t) {
507 if (in_8(&mr->interrupt) != 0)
511 dlog(ms, "after arb, intr/exc/err/fc=%.8x",
512 MKWORD(mr->interrupt, mr->exception, mr->error, mr->fifo_count));
513 if (in_8(&mr->interrupt) == 0 && (in_8(&mr->bus_status1) & BS1_SEL)
514 && (in_8(&mr->bus_status0) & BS0_IO)) {
515 /* looks like a reselection - try resetting the mesh */
516 dlog(ms, "resel? after arb, intr/exc/err/fc=%.8x",
517 MKWORD(mr->interrupt, mr->exception, mr->error, mr->fifo_count));
518 out_8(&mr->sequence, SEQ_RESETMESH);
521 out_8(&mr->interrupt, INT_ERROR | INT_EXCEPTION | INT_CMDDONE);
522 out_8(&mr->intr_mask, INT_ERROR | INT_EXCEPTION | INT_CMDDONE);
523 out_8(&mr->sequence, SEQ_ENBRESEL);
525 for (t = 10; t > 0 && in_8(&mr->interrupt) == 0; --t)
527 dlog(ms, "tried reset after arb, intr/exc/err/fc=%.8x",
528 MKWORD(mr->interrupt, mr->exception, mr->error, mr->fifo_count));
529 #ifndef MESH_MULTIPLE_HOSTS
530 if (in_8(&mr->interrupt) == 0 && (in_8(&mr->bus_status1) & BS1_SEL)
531 && (in_8(&mr->bus_status0) & BS0_IO)) {
532 printk(KERN_ERR "mesh: controller not responding"
533 " to reselection!\n");
535 * If this is a target reselecting us, and the
536 * mesh isn't responding, the higher levels of
537 * the scsi code will eventually time out and
546 * Start the next command for a MESH.
547 * Should be called with interrupts disabled.
549 static void mesh_start(struct mesh_state *ms)
551 struct scsi_cmnd *cmd, *prev, *next;
553 if (ms->phase != idle || ms->current_req != NULL) {
554 printk(KERN_ERR "inappropriate mesh_start (phase=%d, ms=%p)",
559 while (ms->phase == idle) {
561 for (cmd = ms->request_q; ; cmd = (struct scsi_cmnd *) cmd->host_scribble) {
564 if (ms->tgts[cmd->device->id].current_req == NULL)
568 next = (struct scsi_cmnd *) cmd->host_scribble;
570 ms->request_q = next;
572 prev->host_scribble = (void *) next;
574 ms->request_qtail = prev;
576 mesh_start_cmd(ms, cmd);
580 static void mesh_done(struct mesh_state *ms, int start_next)
582 struct scsi_cmnd *cmd;
583 struct mesh_target *tp = &ms->tgts[ms->conn_tgt];
585 cmd = ms->current_req;
586 ms->current_req = NULL;
587 tp->current_req = NULL;
589 struct mesh_cmd_priv *mcmd = mesh_priv(cmd);
591 set_host_byte(cmd, ms->stat);
592 set_status_byte(cmd, mcmd->status);
593 if (ms->stat == DID_OK)
594 scsi_msg_to_host_byte(cmd, mcmd->message);
595 if (DEBUG_TARGET(cmd)) {
596 printk(KERN_DEBUG "mesh_done: result = %x, data_ptr=%d, buflen=%d\n",
597 cmd->result, ms->data_ptr, scsi_bufflen(cmd));
599 /* needs to use sg? */
600 if ((cmd->cmnd[0] == 0 || cmd->cmnd[0] == 0x12 || cmd->cmnd[0] == 3)
601 && cmd->request_buffer != 0) {
602 unsigned char *b = cmd->request_buffer;
603 printk(KERN_DEBUG "buffer = %x %x %x %x %x %x %x %x\n",
604 b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]);
608 mcmd->this_residual -= ms->data_ptr;
612 out_8(&ms->mesh->sequence, SEQ_ENBRESEL);
613 mesh_flush_io(ms->mesh);
620 static inline void add_sdtr_msg(struct mesh_state *ms)
622 int i = ms->n_msgout;
624 ms->msgout[i] = EXTENDED_MESSAGE;
626 ms->msgout[i+2] = EXTENDED_SDTR;
627 ms->msgout[i+3] = mesh_sync_period/4;
628 ms->msgout[i+4] = (ALLOW_SYNC(ms->conn_tgt)? mesh_sync_offset: 0);
629 ms->n_msgout = i + 5;
632 static void set_sdtr(struct mesh_state *ms, int period, int offset)
634 struct mesh_target *tp = &ms->tgts[ms->conn_tgt];
635 volatile struct mesh_regs __iomem *mr = ms->mesh;
638 tp->sdtr_state = sdtr_done;
641 if (SYNC_OFF(tp->sync_params))
642 printk(KERN_INFO "mesh: target %d now asynchronous\n",
644 tp->sync_params = ASYNC_PARAMS;
645 out_8(&mr->sync_params, ASYNC_PARAMS);
649 * We need to compute ceil(clk_freq * period / 500e6) - 2
650 * without incurring overflow.
652 v = (ms->clk_freq / 5000) * period;
654 /* special case: sync_period == 5 * clk_period */
656 /* units of tr are 100kB/s */
657 tr = (ms->clk_freq + 250000) / 500000;
659 /* sync_period == (v + 2) * 2 * clk_period */
660 v = (v + 99999) / 100000 - 2;
663 tr = ((ms->clk_freq / (v + 2)) + 199999) / 200000;
666 offset = 15; /* can't happen */
667 tp->sync_params = SYNC_PARAMS(offset, v);
668 out_8(&mr->sync_params, tp->sync_params);
669 printk(KERN_INFO "mesh: target %d synchronous at %d.%d MB/s\n",
670 ms->conn_tgt, tr/10, tr%10);
673 static void start_phase(struct mesh_state *ms)
676 volatile struct mesh_regs __iomem *mr = ms->mesh;
677 volatile struct dbdma_regs __iomem *md = ms->dma;
678 struct scsi_cmnd *cmd = ms->current_req;
679 struct mesh_target *tp = &ms->tgts[ms->conn_tgt];
681 dlog(ms, "start_phase nmo/exc/fc/seq = %.8x",
682 MKWORD(ms->n_msgout, mr->exception, mr->fifo_count, mr->sequence));
683 out_8(&mr->interrupt, INT_ERROR | INT_EXCEPTION | INT_CMDDONE);
684 seq = use_active_neg + (ms->n_msgout? SEQ_ATN: 0);
685 switch (ms->msgphase) {
690 out_8(&mr->count_hi, 0);
691 out_8(&mr->count_lo, 1);
692 out_8(&mr->sequence, SEQ_MSGIN + seq);
698 * To make sure ATN drops before we assert ACK for
699 * the last byte of the message, we have to do the
700 * last byte specially.
702 if (ms->n_msgout <= 0) {
703 printk(KERN_ERR "mesh: msg_out but n_msgout=%d\n",
706 ms->msgphase = msg_none;
709 if (ALLOW_DEBUG(ms->conn_tgt)) {
710 printk(KERN_DEBUG "mesh: sending %d msg bytes:",
712 for (i = 0; i < ms->n_msgout; ++i)
713 printk(" %x", ms->msgout[i]);
716 dlog(ms, "msgout msg=%.8x", MKWORD(ms->n_msgout, ms->msgout[0],
717 ms->msgout[1], ms->msgout[2]));
718 out_8(&mr->count_hi, 0);
719 out_8(&mr->sequence, SEQ_FLUSHFIFO);
723 * If ATN is not already asserted, we assert it, then
724 * issue a SEQ_MSGOUT to get the mesh to drop ACK.
726 if ((in_8(&mr->bus_status0) & BS0_ATN) == 0) {
727 dlog(ms, "bus0 was %.2x explicitly asserting ATN", mr->bus_status0);
728 out_8(&mr->bus_status0, BS0_ATN); /* explicit ATN */
731 out_8(&mr->count_lo, 1);
732 out_8(&mr->sequence, SEQ_MSGOUT + seq);
733 out_8(&mr->bus_status0, 0); /* release explicit ATN */
734 dlog(ms,"hace: after explicit ATN bus0=%.2x",mr->bus_status0);
736 if (ms->n_msgout == 1) {
738 * We can't issue the SEQ_MSGOUT without ATN
739 * until the target has asserted REQ. The logic
740 * in cmd_complete handles both situations:
741 * REQ already asserted or not.
745 out_8(&mr->count_lo, ms->n_msgout - 1);
746 out_8(&mr->sequence, SEQ_MSGOUT + seq);
747 for (i = 0; i < ms->n_msgout - 1; ++i)
748 out_8(&mr->fifo, ms->msgout[i]);
753 printk(KERN_ERR "mesh bug: start_phase msgphase=%d\n",
759 out_8(&mr->dest_id, ms->conn_tgt);
760 out_8(&mr->sequence, SEQ_SELECT + SEQ_ATN);
763 out_8(&mr->sync_params, tp->sync_params);
764 out_8(&mr->count_hi, 0);
766 out_8(&mr->count_lo, cmd->cmd_len);
767 out_8(&mr->sequence, SEQ_COMMAND + seq);
768 for (i = 0; i < cmd->cmd_len; ++i)
769 out_8(&mr->fifo, cmd->cmnd[i]);
771 out_8(&mr->count_lo, 6);
772 out_8(&mr->sequence, SEQ_COMMAND + seq);
773 for (i = 0; i < 6; ++i)
778 /* transfer data, if any */
779 if (!ms->dma_started) {
780 set_dma_cmds(ms, cmd);
781 out_le32(&md->cmdptr, virt_to_phys(ms->dma_cmds));
782 out_le32(&md->control, (RUN << 16) | RUN);
790 out_8(&mr->count_lo, nb);
791 out_8(&mr->count_hi, nb >> 8);
792 out_8(&mr->sequence, (tp->data_goes_out?
793 SEQ_DATAOUT: SEQ_DATAIN) + SEQ_DMA_MODE + seq);
796 out_8(&mr->count_hi, 0);
797 out_8(&mr->count_lo, 1);
798 out_8(&mr->sequence, SEQ_STATUS + seq);
802 out_8(&mr->sequence, SEQ_ENBRESEL);
805 dlog(ms, "enbresel intr/exc/err/fc=%.8x",
806 MKWORD(mr->interrupt, mr->exception, mr->error,
808 out_8(&mr->sequence, SEQ_BUSFREE);
811 printk(KERN_ERR "mesh: start_phase called with phase=%d\n",
818 static inline void get_msgin(struct mesh_state *ms)
820 volatile struct mesh_regs __iomem *mr = ms->mesh;
828 ms->msgin[i++] = in_8(&mr->fifo);
832 static inline int msgin_length(struct mesh_state *ms)
837 if (ms->n_msgin > 0) {
840 /* extended message */
841 n = ms->n_msgin < 2? 2: ms->msgin[1] + 2;
842 } else if (0x20 <= b && b <= 0x2f) {
850 static void reselected(struct mesh_state *ms)
852 volatile struct mesh_regs __iomem *mr = ms->mesh;
853 struct scsi_cmnd *cmd;
854 struct mesh_target *tp;
861 if ((cmd = ms->current_req) != NULL) {
862 /* put the command back on the queue */
863 cmd->host_scribble = (void *) ms->request_q;
864 if (ms->request_q == NULL)
865 ms->request_qtail = cmd;
867 tp = &ms->tgts[cmd->device->id];
868 tp->current_req = NULL;
872 ms->phase = reselecting;
878 printk(KERN_ERR "mesh: reselected in phase %d/%d tgt %d\n",
879 ms->msgphase, ms->phase, ms->conn_tgt);
880 dumplog(ms, ms->conn_tgt);
884 if (ms->dma_started) {
885 printk(KERN_ERR "mesh: reselected with DMA started !\n");
888 ms->current_req = NULL;
890 ms->msgphase = msg_in;
892 ms->last_n_msgout = 0;
896 * We seem to get abortive reselections sometimes.
898 while ((in_8(&mr->bus_status1) & BS1_BSY) == 0) {
899 static int mesh_aborted_resels;
900 mesh_aborted_resels++;
901 out_8(&mr->interrupt, INT_ERROR | INT_EXCEPTION | INT_CMDDONE);
904 out_8(&mr->sequence, SEQ_ENBRESEL);
907 dlog(ms, "extra resel err/exc/fc = %.6x",
908 MKWORD(0, mr->error, mr->exception, mr->fifo_count));
910 out_8(&mr->interrupt, INT_ERROR | INT_EXCEPTION | INT_CMDDONE);
913 out_8(&mr->sequence, SEQ_ENBRESEL);
916 out_8(&mr->sync_params, ASYNC_PARAMS);
919 * Find out who reselected us.
921 if (in_8(&mr->fifo_count) == 0) {
922 printk(KERN_ERR "mesh: reselection but nothing in fifo?\n");
923 ms->conn_tgt = ms->host->this_id;
926 /* get the last byte in the fifo */
929 dlog(ms, "reseldata %x", b);
930 } while (in_8(&mr->fifo_count));
931 for (t = 0; t < 8; ++t)
932 if ((b & (1 << t)) != 0 && t != ms->host->this_id)
934 if (b != (1 << t) + (1 << ms->host->this_id)) {
935 printk(KERN_ERR "mesh: bad reselection data %x\n", b);
936 ms->conn_tgt = ms->host->this_id;
942 * Set up to continue with that target's transfer.
946 out_8(&mr->sync_params, tp->sync_params);
947 if (ALLOW_DEBUG(t)) {
948 printk(KERN_DEBUG "mesh: reselected by target %d\n", t);
949 printk(KERN_DEBUG "mesh: saved_ptr=%x goes_out=%d cmd=%p\n",
950 tp->saved_ptr, tp->data_goes_out, tp->current_req);
952 ms->current_req = tp->current_req;
953 if (tp->current_req == NULL) {
954 printk(KERN_ERR "mesh: reselected by tgt %d but no cmd!\n", t);
957 ms->data_ptr = tp->saved_ptr;
958 dlog(ms, "resel prev tgt=%d", prev);
959 dlog(ms, "resel err/exc=%.4x", MKWORD(0, 0, mr->error, mr->exception));
964 dumplog(ms, ms->conn_tgt);
971 static void do_abort(struct mesh_state *ms)
973 ms->msgout[0] = ABORT;
976 ms->stat = DID_ABORT;
977 dlog(ms, "abort", 0);
980 static void handle_reset(struct mesh_state *ms)
983 struct mesh_target *tp;
984 struct scsi_cmnd *cmd;
985 volatile struct mesh_regs __iomem *mr = ms->mesh;
987 for (tgt = 0; tgt < 8; ++tgt) {
989 if ((cmd = tp->current_req) != NULL) {
990 set_host_byte(cmd, DID_RESET);
991 tp->current_req = NULL;
994 ms->tgts[tgt].sdtr_state = do_sdtr;
995 ms->tgts[tgt].sync_params = ASYNC_PARAMS;
997 ms->current_req = NULL;
998 while ((cmd = ms->request_q) != NULL) {
999 ms->request_q = (struct scsi_cmnd *) cmd->host_scribble;
1000 set_host_byte(cmd, DID_RESET);
1004 ms->msgphase = msg_none;
1005 out_8(&mr->interrupt, INT_ERROR | INT_EXCEPTION | INT_CMDDONE);
1006 out_8(&mr->sequence, SEQ_FLUSHFIFO);
1009 out_8(&mr->sync_params, ASYNC_PARAMS);
1010 out_8(&mr->sequence, SEQ_ENBRESEL);
1013 static irqreturn_t do_mesh_interrupt(int irq, void *dev_id)
1015 unsigned long flags;
1016 struct mesh_state *ms = dev_id;
1017 struct Scsi_Host *dev = ms->host;
1019 spin_lock_irqsave(dev->host_lock, flags);
1021 spin_unlock_irqrestore(dev->host_lock, flags);
1025 static void handle_error(struct mesh_state *ms)
1027 int err, exc, count;
1028 volatile struct mesh_regs __iomem *mr = ms->mesh;
1030 err = in_8(&mr->error);
1031 exc = in_8(&mr->exception);
1032 out_8(&mr->interrupt, INT_ERROR | INT_EXCEPTION | INT_CMDDONE);
1033 dlog(ms, "error err/exc/fc/cl=%.8x",
1034 MKWORD(err, exc, mr->fifo_count, mr->count_lo));
1035 if (err & ERR_SCSIRESET) {
1036 /* SCSI bus was reset */
1037 printk(KERN_INFO "mesh: SCSI bus reset detected: "
1038 "waiting for end...");
1039 while ((in_8(&mr->bus_status1) & BS1_RST) != 0)
1042 if (ms->dma_started)
1045 /* request_q is empty, no point in mesh_start() */
1048 if (err & ERR_UNEXPDISC) {
1049 /* Unexpected disconnect */
1050 if (exc & EXC_RESELECTED) {
1054 if (!ms->aborting) {
1055 printk(KERN_WARNING "mesh: target %d aborted\n",
1057 dumplog(ms, ms->conn_tgt);
1060 out_8(&mr->interrupt, INT_CMDDONE);
1061 ms->stat = DID_ABORT;
1065 if (err & ERR_PARITY) {
1066 if (ms->msgphase == msg_in) {
1067 printk(KERN_ERR "mesh: msg parity error, target %d\n",
1069 ms->msgout[0] = MSG_PARITY_ERROR;
1071 ms->msgphase = msg_in_bad;
1075 if (ms->stat == DID_OK) {
1076 printk(KERN_ERR "mesh: parity error, target %d\n",
1078 ms->stat = DID_PARITY;
1080 count = (mr->count_hi << 8) + mr->count_lo;
1084 /* reissue the data transfer command */
1085 out_8(&mr->sequence, mr->sequence);
1089 if (err & ERR_SEQERR) {
1090 if (exc & EXC_RESELECTED) {
1091 /* This can happen if we issue a command to
1092 get the bus just after the target reselects us. */
1093 static int mesh_resel_seqerr;
1094 mesh_resel_seqerr++;
1098 if (exc == EXC_PHASEMM) {
1099 static int mesh_phasemm_seqerr;
1100 mesh_phasemm_seqerr++;
1104 printk(KERN_ERR "mesh: sequence error (err=%x exc=%x)\n",
1107 printk(KERN_ERR "mesh: unknown error %x (exc=%x)\n", err, exc);
1110 dumplog(ms, ms->conn_tgt);
1111 if (ms->phase > selecting && (in_8(&mr->bus_status1) & BS1_BSY)) {
1112 /* try to do what the target wants */
1117 ms->stat = DID_ERROR;
1121 static void handle_exception(struct mesh_state *ms)
1124 volatile struct mesh_regs __iomem *mr = ms->mesh;
1126 exc = in_8(&mr->exception);
1127 out_8(&mr->interrupt, INT_EXCEPTION | INT_CMDDONE);
1128 if (exc & EXC_RESELECTED) {
1129 static int mesh_resel_exc;
1132 } else if (exc == EXC_ARBLOST) {
1133 printk(KERN_DEBUG "mesh: lost arbitration\n");
1134 ms->stat = DID_BUS_BUSY;
1136 } else if (exc == EXC_SELTO) {
1137 /* selection timed out */
1138 ms->stat = DID_BAD_TARGET;
1140 } else if (exc == EXC_PHASEMM) {
1141 /* target wants to do something different:
1142 find out what it wants and do it. */
1145 printk(KERN_ERR "mesh: can't cope with exception %x\n", exc);
1147 dumplog(ms, ms->conn_tgt);
1153 static void handle_msgin(struct mesh_state *ms)
1156 struct scsi_cmnd *cmd = ms->current_req;
1157 struct mesh_target *tp = &ms->tgts[ms->conn_tgt];
1159 if (ms->n_msgin == 0)
1161 code = ms->msgin[0];
1162 if (ALLOW_DEBUG(ms->conn_tgt)) {
1163 printk(KERN_DEBUG "got %d message bytes:", ms->n_msgin);
1164 for (i = 0; i < ms->n_msgin; ++i)
1165 printk(" %x", ms->msgin[i]);
1168 dlog(ms, "msgin msg=%.8x",
1169 MKWORD(ms->n_msgin, code, ms->msgin[1], ms->msgin[2]));
1171 ms->expect_reply = 0;
1173 if (ms->n_msgin < msgin_length(ms))
1176 mesh_priv(cmd)->message = code;
1178 case COMMAND_COMPLETE:
1180 case EXTENDED_MESSAGE:
1181 switch (ms->msgin[2]) {
1182 case EXTENDED_MODIFY_DATA_POINTER:
1183 ms->data_ptr += (ms->msgin[3] << 24) + ms->msgin[6]
1184 + (ms->msgin[4] << 16) + (ms->msgin[5] << 8);
1187 if (tp->sdtr_state != sdtr_sent) {
1188 /* reply with an SDTR */
1190 /* limit period to at least his value,
1191 offset to no more than his */
1192 if (ms->msgout[3] < ms->msgin[3])
1193 ms->msgout[3] = ms->msgin[3];
1194 if (ms->msgout[4] > ms->msgin[4])
1195 ms->msgout[4] = ms->msgin[4];
1196 set_sdtr(ms, ms->msgout[3], ms->msgout[4]);
1197 ms->msgphase = msg_out;
1199 set_sdtr(ms, ms->msgin[3], ms->msgin[4]);
1207 tp->saved_ptr = ms->data_ptr;
1209 case RESTORE_POINTERS:
1210 ms->data_ptr = tp->saved_ptr;
1213 ms->phase = disconnecting;
1217 case MESSAGE_REJECT:
1218 if (tp->sdtr_state == sdtr_sent)
1224 if (IDENTIFY_BASE <= code && code <= IDENTIFY_BASE + 7) {
1227 ms->msgphase = msg_out;
1228 } else if (code != cmd->device->lun + IDENTIFY_BASE) {
1229 printk(KERN_WARNING "mesh: lun mismatch "
1230 "(%d != %llu) on reselection from "
1231 "target %d\n", code - IDENTIFY_BASE,
1232 cmd->device->lun, ms->conn_tgt);
1241 printk(KERN_WARNING "mesh: rejecting message from target %d:",
1243 for (i = 0; i < ms->n_msgin; ++i)
1244 printk(" %x", ms->msgin[i]);
1246 ms->msgout[0] = MESSAGE_REJECT;
1248 ms->msgphase = msg_out;
1252 * Set up DMA commands for transferring data.
1254 static void set_dma_cmds(struct mesh_state *ms, struct scsi_cmnd *cmd)
1256 int i, dma_cmd, total, off, dtot;
1257 struct scatterlist *scl;
1258 struct dbdma_cmd *dcmds;
1260 dma_cmd = ms->tgts[ms->conn_tgt].data_goes_out?
1261 OUTPUT_MORE: INPUT_MORE;
1262 dcmds = ms->dma_cmds;
1267 mesh_priv(cmd)->this_residual = scsi_bufflen(cmd);
1269 nseg = scsi_dma_map(cmd);
1276 scsi_for_each_sg(cmd, scl, nseg, i) {
1277 u32 dma_addr = sg_dma_address(scl);
1278 u32 dma_len = sg_dma_len(scl);
1280 total += scl->length;
1281 if (off >= dma_len) {
1285 if (dma_len > 0xffff)
1286 panic("mesh: scatterlist element >= 64k");
1287 dcmds->req_count = cpu_to_le16(dma_len - off);
1288 dcmds->command = cpu_to_le16(dma_cmd);
1289 dcmds->phy_addr = cpu_to_le32(dma_addr + off);
1290 dcmds->xfer_status = 0;
1292 dtot += dma_len - off;
1298 /* Either the target has overrun our buffer,
1299 or the caller didn't provide a buffer. */
1300 static char mesh_extra_buf[64];
1302 dtot = sizeof(mesh_extra_buf);
1303 dcmds->req_count = cpu_to_le16(dtot);
1304 dcmds->phy_addr = cpu_to_le32(virt_to_phys(mesh_extra_buf));
1305 dcmds->xfer_status = 0;
1308 dma_cmd += OUTPUT_LAST - OUTPUT_MORE;
1309 dcmds[-1].command = cpu_to_le16(dma_cmd);
1310 memset(dcmds, 0, sizeof(*dcmds));
1311 dcmds->command = cpu_to_le16(DBDMA_STOP);
1312 ms->dma_count = dtot;
1315 static void halt_dma(struct mesh_state *ms)
1317 volatile struct dbdma_regs __iomem *md = ms->dma;
1318 volatile struct mesh_regs __iomem *mr = ms->mesh;
1319 struct scsi_cmnd *cmd = ms->current_req;
1322 if (!ms->tgts[ms->conn_tgt].data_goes_out) {
1323 /* wait a little while until the fifo drains */
1325 while (t > 0 && in_8(&mr->fifo_count) != 0
1326 && (in_le32(&md->status) & ACTIVE) != 0) {
1331 out_le32(&md->control, RUN << 16); /* turn off RUN bit */
1332 nb = (mr->count_hi << 8) + mr->count_lo;
1333 dlog(ms, "halt_dma fc/count=%.6x",
1334 MKWORD(0, mr->fifo_count, 0, nb));
1335 if (ms->tgts[ms->conn_tgt].data_goes_out)
1336 nb += mr->fifo_count;
1337 /* nb is the number of bytes not yet transferred
1338 to/from the target. */
1340 dlog(ms, "data_ptr %x", ms->data_ptr);
1341 if (ms->data_ptr < 0) {
1342 printk(KERN_ERR "mesh: halt_dma: data_ptr=%d (nb=%d, ms=%p)\n",
1343 ms->data_ptr, nb, ms);
1346 dumplog(ms, ms->conn_tgt);
1348 #endif /* MESH_DBG */
1349 } else if (cmd && scsi_bufflen(cmd) &&
1350 ms->data_ptr > scsi_bufflen(cmd)) {
1351 printk(KERN_DEBUG "mesh: target %d overrun, "
1352 "data_ptr=%x total=%x goes_out=%d\n",
1353 ms->conn_tgt, ms->data_ptr, scsi_bufflen(cmd),
1354 ms->tgts[ms->conn_tgt].data_goes_out);
1357 scsi_dma_unmap(cmd);
1358 ms->dma_started = 0;
1361 static void phase_mismatch(struct mesh_state *ms)
1363 volatile struct mesh_regs __iomem *mr = ms->mesh;
1366 dlog(ms, "phasemm ch/cl/seq/fc=%.8x",
1367 MKWORD(mr->count_hi, mr->count_lo, mr->sequence, mr->fifo_count));
1368 phase = in_8(&mr->bus_status0) & BS0_PHASE;
1369 if (ms->msgphase == msg_out_xxx && phase == BP_MSGOUT) {
1370 /* output the last byte of the message, without ATN */
1371 out_8(&mr->count_lo, 1);
1372 out_8(&mr->sequence, SEQ_MSGOUT + use_active_neg);
1375 out_8(&mr->fifo, ms->msgout[ms->n_msgout-1]);
1376 ms->msgphase = msg_out_last;
1380 if (ms->msgphase == msg_in) {
1386 if (ms->dma_started)
1388 if (mr->fifo_count) {
1389 out_8(&mr->sequence, SEQ_FLUSHFIFO);
1394 ms->msgphase = msg_none;
1397 ms->tgts[ms->conn_tgt].data_goes_out = 0;
1398 ms->phase = dataing;
1401 ms->tgts[ms->conn_tgt].data_goes_out = 1;
1402 ms->phase = dataing;
1405 ms->phase = commanding;
1408 ms->phase = statusing;
1411 ms->msgphase = msg_in;
1415 ms->msgphase = msg_out;
1416 if (ms->n_msgout == 0) {
1420 if (ms->last_n_msgout == 0) {
1422 "mesh: no msg to repeat\n");
1423 ms->msgout[0] = NOP;
1424 ms->last_n_msgout = 1;
1426 ms->n_msgout = ms->last_n_msgout;
1431 printk(KERN_DEBUG "mesh: unknown scsi phase %x\n", phase);
1432 ms->stat = DID_ERROR;
1440 static void cmd_complete(struct mesh_state *ms)
1442 volatile struct mesh_regs __iomem *mr = ms->mesh;
1443 struct scsi_cmnd *cmd = ms->current_req;
1444 struct mesh_target *tp = &ms->tgts[ms->conn_tgt];
1447 dlog(ms, "cmd_complete fc=%x", mr->fifo_count);
1448 seq = use_active_neg + (ms->n_msgout? SEQ_ATN: 0);
1449 switch (ms->msgphase) {
1451 /* huh? we expected a phase mismatch */
1453 ms->msgphase = msg_in;
1457 /* should have some message bytes in fifo */
1459 n = msgin_length(ms);
1460 if (ms->n_msgin < n) {
1461 out_8(&mr->count_lo, n - ms->n_msgin);
1462 out_8(&mr->sequence, SEQ_MSGIN + seq);
1464 ms->msgphase = msg_none;
1471 out_8(&mr->sequence, SEQ_FLUSHFIFO);
1474 out_8(&mr->count_lo, 1);
1475 out_8(&mr->sequence, SEQ_MSGIN + SEQ_ATN + use_active_neg);
1480 * To get the right timing on ATN wrt ACK, we have
1481 * to get the MESH to drop ACK, wait until REQ gets
1482 * asserted, then drop ATN. To do this we first
1483 * issue a SEQ_MSGOUT with ATN and wait for REQ,
1484 * then change the command to a SEQ_MSGOUT w/o ATN.
1485 * If we don't see REQ in a reasonable time, we
1486 * change the command to SEQ_MSGIN with ATN,
1487 * wait for the phase mismatch interrupt, then
1488 * issue the SEQ_MSGOUT without ATN.
1490 out_8(&mr->count_lo, 1);
1491 out_8(&mr->sequence, SEQ_MSGOUT + use_active_neg + SEQ_ATN);
1492 t = 30; /* wait up to 30us */
1493 while ((in_8(&mr->bus_status0) & BS0_REQ) == 0 && --t >= 0)
1495 dlog(ms, "last_mbyte err/exc/fc/cl=%.8x",
1496 MKWORD(mr->error, mr->exception,
1497 mr->fifo_count, mr->count_lo));
1498 if (in_8(&mr->interrupt) & (INT_ERROR | INT_EXCEPTION)) {
1499 /* whoops, target didn't do what we expected */
1500 ms->last_n_msgout = ms->n_msgout;
1502 if (in_8(&mr->interrupt) & INT_ERROR) {
1503 printk(KERN_ERR "mesh: error %x in msg_out\n",
1508 if (in_8(&mr->exception) != EXC_PHASEMM)
1509 printk(KERN_ERR "mesh: exc %x in msg_out\n",
1510 in_8(&mr->exception));
1512 printk(KERN_DEBUG "mesh: bs0=%x in msg_out\n",
1513 in_8(&mr->bus_status0));
1514 handle_exception(ms);
1517 if (in_8(&mr->bus_status0) & BS0_REQ) {
1518 out_8(&mr->sequence, SEQ_MSGOUT + use_active_neg);
1521 out_8(&mr->fifo, ms->msgout[ms->n_msgout-1]);
1522 ms->msgphase = msg_out_last;
1524 out_8(&mr->sequence, SEQ_MSGIN + use_active_neg + SEQ_ATN);
1525 ms->msgphase = msg_out_xxx;
1530 ms->last_n_msgout = ms->n_msgout;
1532 ms->msgphase = ms->expect_reply? msg_in: msg_none;
1537 switch (ms->phase) {
1539 printk(KERN_ERR "mesh: interrupt in idle phase?\n");
1543 dlog(ms, "Selecting phase at command completion",0);
1544 ms->msgout[0] = IDENTIFY(ALLOW_RESEL(ms->conn_tgt),
1545 (cmd? cmd->device->lun: 0));
1547 ms->expect_reply = 0;
1549 ms->msgout[0] = ABORT;
1551 } else if (tp->sdtr_state == do_sdtr) {
1552 /* add SDTR message */
1554 ms->expect_reply = 1;
1555 tp->sdtr_state = sdtr_sent;
1557 ms->msgphase = msg_out;
1559 * We need to wait for REQ before dropping ATN.
1560 * We wait for at most 30us, then fall back to
1561 * a scheme where we issue a SEQ_COMMAND with ATN,
1562 * which will give us a phase mismatch interrupt
1563 * when REQ does come, and then we send the message.
1565 t = 230; /* wait up to 230us */
1566 while ((in_8(&mr->bus_status0) & BS0_REQ) == 0) {
1568 dlog(ms, "impatient for req", ms->n_msgout);
1569 ms->msgphase = msg_none;
1576 if (ms->dma_count != 0) {
1581 * We can get a phase mismatch here if the target
1582 * changes to the status phase, even though we have
1583 * had a command complete interrupt. Then, if we
1584 * issue the SEQ_STATUS command, we'll get a sequence
1585 * error interrupt. Which isn't so bad except that
1586 * occasionally the mesh actually executes the
1587 * SEQ_STATUS *as well as* giving us the sequence
1588 * error and phase mismatch exception.
1590 out_8(&mr->sequence, 0);
1591 out_8(&mr->interrupt,
1592 INT_ERROR | INT_EXCEPTION | INT_CMDDONE);
1597 struct mesh_cmd_priv *mcmd = mesh_priv(cmd);
1599 mcmd->status = mr->fifo;
1600 if (DEBUG_TARGET(cmd))
1601 printk(KERN_DEBUG "mesh: status is %x\n",
1604 ms->msgphase = msg_in;
1610 ms->current_req = NULL;
1625 * Called by midlayer with host locked to queue a new
1628 static int mesh_queue_lck(struct scsi_cmnd *cmd)
1630 struct mesh_state *ms;
1632 cmd->host_scribble = NULL;
1634 ms = (struct mesh_state *) cmd->device->host->hostdata;
1636 if (ms->request_q == NULL)
1637 ms->request_q = cmd;
1639 ms->request_qtail->host_scribble = (void *) cmd;
1640 ms->request_qtail = cmd;
1642 if (ms->phase == idle)
1648 static DEF_SCSI_QCMD(mesh_queue)
1651 * Called to handle interrupts, either call by the interrupt
1652 * handler (do_mesh_interrupt) or by other functions in
1653 * exceptional circumstances
1655 static void mesh_interrupt(struct mesh_state *ms)
1657 volatile struct mesh_regs __iomem *mr = ms->mesh;
1661 if (ALLOW_DEBUG(ms->conn_tgt))
1662 printk(KERN_DEBUG "mesh_intr, bs0=%x int=%x exc=%x err=%x "
1663 "phase=%d msgphase=%d\n", mr->bus_status0,
1664 mr->interrupt, mr->exception, mr->error,
1665 ms->phase, ms->msgphase);
1667 while ((intr = in_8(&mr->interrupt)) != 0) {
1668 dlog(ms, "interrupt intr/err/exc/seq=%.8x",
1669 MKWORD(intr, mr->error, mr->exception, mr->sequence));
1670 if (intr & INT_ERROR) {
1672 } else if (intr & INT_EXCEPTION) {
1673 handle_exception(ms);
1674 } else if (intr & INT_CMDDONE) {
1675 out_8(&mr->interrupt, INT_CMDDONE);
1681 /* Todo: here we can at least try to remove the command from the
1682 * queue if it isn't connected yet, and for pending command, assert
1683 * ATN until the bus gets freed.
1685 static int mesh_abort(struct scsi_cmnd *cmd)
1687 struct mesh_state *ms = (struct mesh_state *) cmd->device->host->hostdata;
1689 printk(KERN_DEBUG "mesh_abort(%p)\n", cmd);
1691 dumplog(ms, cmd->device->id);
1697 * Called by the midlayer with the lock held to reset the
1698 * SCSI host and bus.
1699 * The midlayer will wait for devices to come back, we don't need
1700 * to do that ourselves
1702 static int mesh_host_reset(struct scsi_cmnd *cmd)
1704 struct mesh_state *ms = (struct mesh_state *) cmd->device->host->hostdata;
1705 volatile struct mesh_regs __iomem *mr = ms->mesh;
1706 volatile struct dbdma_regs __iomem *md = ms->dma;
1707 unsigned long flags;
1709 printk(KERN_DEBUG "mesh_host_reset\n");
1711 spin_lock_irqsave(ms->host->host_lock, flags);
1713 if (ms->dma_started)
1716 /* Reset the controller & dbdma channel */
1717 out_le32(&md->control, (RUN|PAUSE|FLUSH|WAKE) << 16); /* stop dma */
1718 out_8(&mr->exception, 0xff); /* clear all exception bits */
1719 out_8(&mr->error, 0xff); /* clear all error bits */
1720 out_8(&mr->sequence, SEQ_RESETMESH);
1723 out_8(&mr->intr_mask, INT_ERROR | INT_EXCEPTION | INT_CMDDONE);
1724 out_8(&mr->source_id, ms->host->this_id);
1725 out_8(&mr->sel_timeout, 25); /* 250ms */
1726 out_8(&mr->sync_params, ASYNC_PARAMS);
1729 out_8(&mr->bus_status1, BS1_RST); /* assert RST */
1731 udelay(30); /* leave it on for >= 25us */
1732 out_8(&mr->bus_status1, 0); /* negate RST */
1734 /* Complete pending commands */
1737 spin_unlock_irqrestore(ms->host->host_lock, flags);
1741 static void set_mesh_power(struct mesh_state *ms, int state)
1743 if (!machine_is(powermac))
1746 pmac_call_feature(PMAC_FTR_MESH_ENABLE, macio_get_of_node(ms->mdev), 0, 1);
1749 pmac_call_feature(PMAC_FTR_MESH_ENABLE, macio_get_of_node(ms->mdev), 0, 0);
1756 static int mesh_suspend(struct macio_dev *mdev, pm_message_t mesg)
1758 struct mesh_state *ms = (struct mesh_state *)macio_get_drvdata(mdev);
1759 unsigned long flags;
1761 switch (mesg.event) {
1762 case PM_EVENT_SUSPEND:
1763 case PM_EVENT_HIBERNATE:
1764 case PM_EVENT_FREEZE:
1769 if (ms->phase == sleeping)
1772 scsi_block_requests(ms->host);
1773 spin_lock_irqsave(ms->host->host_lock, flags);
1774 while(ms->phase != idle) {
1775 spin_unlock_irqrestore(ms->host->host_lock, flags);
1777 spin_lock_irqsave(ms->host->host_lock, flags);
1779 ms->phase = sleeping;
1780 spin_unlock_irqrestore(ms->host->host_lock, flags);
1781 disable_irq(ms->meshintr);
1782 set_mesh_power(ms, 0);
1787 static int mesh_resume(struct macio_dev *mdev)
1789 struct mesh_state *ms = (struct mesh_state *)macio_get_drvdata(mdev);
1790 unsigned long flags;
1792 if (ms->phase != sleeping)
1795 set_mesh_power(ms, 1);
1797 spin_lock_irqsave(ms->host->host_lock, flags);
1799 spin_unlock_irqrestore(ms->host->host_lock, flags);
1800 enable_irq(ms->meshintr);
1801 scsi_unblock_requests(ms->host);
1806 #endif /* CONFIG_PM */
1809 * If we leave drives set for synchronous transfers (especially
1810 * CDROMs), and reboot to MacOS, it gets confused, poor thing.
1811 * So, on reboot we reset the SCSI bus.
1813 static int mesh_shutdown(struct macio_dev *mdev)
1815 struct mesh_state *ms = (struct mesh_state *)macio_get_drvdata(mdev);
1816 volatile struct mesh_regs __iomem *mr;
1817 unsigned long flags;
1819 printk(KERN_INFO "resetting MESH scsi bus(es)\n");
1820 spin_lock_irqsave(ms->host->host_lock, flags);
1822 out_8(&mr->intr_mask, 0);
1823 out_8(&mr->interrupt, INT_ERROR | INT_EXCEPTION | INT_CMDDONE);
1824 out_8(&mr->bus_status1, BS1_RST);
1827 out_8(&mr->bus_status1, 0);
1828 spin_unlock_irqrestore(ms->host->host_lock, flags);
1833 static struct scsi_host_template mesh_template = {
1834 .proc_name = "mesh",
1836 .queuecommand = mesh_queue,
1837 .eh_abort_handler = mesh_abort,
1838 .eh_host_reset_handler = mesh_host_reset,
1841 .sg_tablesize = SG_ALL,
1843 .max_segment_size = 65535,
1844 .cmd_size = sizeof(struct mesh_cmd_priv),
1847 static int mesh_probe(struct macio_dev *mdev, const struct of_device_id *match)
1849 struct device_node *mesh = macio_get_of_node(mdev);
1850 struct pci_dev* pdev = macio_get_pci_dev(mdev);
1853 struct mesh_state *ms;
1854 struct Scsi_Host *mesh_host;
1855 void *dma_cmd_space;
1856 dma_addr_t dma_cmd_bus;
1858 switch (mdev->bus->chip->type) {
1859 case macio_heathrow:
1861 case macio_paddington:
1865 use_active_neg = SEQ_ACTIVE_NEG;
1868 if (macio_resource_count(mdev) != 2 || macio_irq_count(mdev) != 2) {
1869 printk(KERN_ERR "mesh: expected 2 addrs and 2 intrs"
1870 " (got %d,%d)\n", macio_resource_count(mdev),
1871 macio_irq_count(mdev));
1875 if (macio_request_resources(mdev, "mesh") != 0) {
1876 printk(KERN_ERR "mesh: unable to request memory resources");
1879 mesh_host = scsi_host_alloc(&mesh_template, sizeof(struct mesh_state));
1880 if (mesh_host == NULL) {
1881 printk(KERN_ERR "mesh: couldn't register host");
1885 mesh_host->base = macio_resource_start(mdev, 0);
1886 mesh_host->irq = macio_irq(mdev, 0);
1887 ms = (struct mesh_state *) mesh_host->hostdata;
1888 macio_set_drvdata(mdev, ms);
1889 ms->host = mesh_host;
1893 ms->mesh = ioremap(macio_resource_start(mdev, 0), 0x1000);
1894 if (ms->mesh == NULL) {
1895 printk(KERN_ERR "mesh: can't map registers\n");
1898 ms->dma = ioremap(macio_resource_start(mdev, 1), 0x1000);
1899 if (ms->dma == NULL) {
1900 printk(KERN_ERR "mesh: can't map registers\n");
1905 ms->meshintr = macio_irq(mdev, 0);
1906 ms->dmaintr = macio_irq(mdev, 1);
1908 /* Space for dma command list: +1 for stop command,
1909 * +1 to allow for aligning.
1911 ms->dma_cmd_size = (mesh_host->sg_tablesize + 2) * sizeof(struct dbdma_cmd);
1913 /* We use the PCI APIs for now until the generic one gets fixed
1914 * enough or until we get some macio-specific versions
1916 dma_cmd_space = dma_alloc_coherent(&macio_get_pci_dev(mdev)->dev,
1917 ms->dma_cmd_size, &dma_cmd_bus,
1919 if (dma_cmd_space == NULL) {
1920 printk(KERN_ERR "mesh: can't allocate DMA table\n");
1924 ms->dma_cmds = (struct dbdma_cmd *) DBDMA_ALIGN(dma_cmd_space);
1925 ms->dma_cmd_space = dma_cmd_space;
1926 ms->dma_cmd_bus = dma_cmd_bus + ((unsigned long)ms->dma_cmds)
1927 - (unsigned long)dma_cmd_space;
1928 ms->current_req = NULL;
1929 for (tgt = 0; tgt < 8; ++tgt) {
1930 ms->tgts[tgt].sdtr_state = do_sdtr;
1931 ms->tgts[tgt].sync_params = ASYNC_PARAMS;
1932 ms->tgts[tgt].current_req = NULL;
1935 if ((cfp = of_get_property(mesh, "clock-frequency", NULL)))
1936 ms->clk_freq = *cfp;
1938 printk(KERN_INFO "mesh: assuming 50MHz clock frequency\n");
1939 ms->clk_freq = 50000000;
1942 /* The maximum sync rate is clock / 5; increase
1943 * mesh_sync_period if necessary.
1945 minper = 1000000000 / (ms->clk_freq / 5); /* ns */
1946 if (mesh_sync_period < minper)
1947 mesh_sync_period = minper;
1949 /* Power up the chip */
1950 set_mesh_power(ms, 1);
1955 /* Request interrupt */
1956 if (request_irq(ms->meshintr, do_mesh_interrupt, 0, "MESH", ms)) {
1957 printk(KERN_ERR "MESH: can't get irq %d\n", ms->meshintr);
1961 /* Add scsi host & scan */
1962 if (scsi_add_host(mesh_host, &mdev->ofdev.dev))
1963 goto out_release_irq;
1964 scsi_scan_host(mesh_host);
1969 free_irq(ms->meshintr, ms);
1971 /* shutdown & reset bus in case of error or macos can be confused
1972 * at reboot if the bus was set to synchronous mode already
1974 mesh_shutdown(mdev);
1975 set_mesh_power(ms, 0);
1976 dma_free_coherent(&macio_get_pci_dev(mdev)->dev, ms->dma_cmd_size,
1977 ms->dma_cmd_space, ms->dma_cmd_bus);
1982 scsi_host_put(mesh_host);
1984 macio_release_resources(mdev);
1989 static int mesh_remove(struct macio_dev *mdev)
1991 struct mesh_state *ms = (struct mesh_state *)macio_get_drvdata(mdev);
1992 struct Scsi_Host *mesh_host = ms->host;
1994 scsi_remove_host(mesh_host);
1996 free_irq(ms->meshintr, ms);
1998 /* Reset scsi bus */
1999 mesh_shutdown(mdev);
2001 /* Shut down chip & termination */
2002 set_mesh_power(ms, 0);
2004 /* Unmap registers & dma controller */
2008 /* Free DMA commands memory */
2009 dma_free_coherent(&macio_get_pci_dev(mdev)->dev, ms->dma_cmd_size,
2010 ms->dma_cmd_space, ms->dma_cmd_bus);
2012 /* Release memory resources */
2013 macio_release_resources(mdev);
2015 scsi_host_put(mesh_host);
2021 static struct of_device_id mesh_match[] =
2028 .compatible = "chrp,mesh0"
2032 MODULE_DEVICE_TABLE (of, mesh_match);
2034 static struct macio_driver mesh_driver =
2038 .owner = THIS_MODULE,
2039 .of_match_table = mesh_match,
2041 .probe = mesh_probe,
2042 .remove = mesh_remove,
2043 .shutdown = mesh_shutdown,
2045 .suspend = mesh_suspend,
2046 .resume = mesh_resume,
2051 static int __init init_mesh(void)
2054 /* Calculate sync rate from module parameters */
2057 if (sync_rate > 0) {
2058 printk(KERN_INFO "mesh: configured for synchronous %d MB/s\n", sync_rate);
2059 mesh_sync_period = 1000 / sync_rate; /* ns */
2060 mesh_sync_offset = 15;
2062 printk(KERN_INFO "mesh: configured for asynchronous\n");
2064 return macio_register_driver(&mesh_driver);
2067 static void __exit exit_mesh(void)
2069 return macio_unregister_driver(&mesh_driver);
2072 module_init(init_mesh);
2073 module_exit(exit_mesh);