Linux Audio

Check our new training course

Loading...
v6.8
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   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)
   6 * controller.
   7 *
   8 * Paul Mackerras, August 1996.
   9 * Copyright (C) 1996 Paul Mackerras.
  10 *
  11 * Apr. 21 2002  - BenH		Rework bus reset code for new error handler
  12 *                              Add delay after initial bus reset
  13 *                              Add module parameters
  14 *
  15 * Sep. 27 2003  - BenH		Move to new driver model, fix some write posting
  16 *				issues
  17 * To do:
  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
  21 */
  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>
  36#include <asm/io.h>
  37#include <asm/prom.h>
  38#include <asm/irq.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>
  44
  45#include <scsi/scsi.h>
  46#include <scsi/scsi_cmnd.h>
  47#include <scsi/scsi_device.h>
  48#include <scsi/scsi_host.h>
  49
  50#include "mesh.h"
  51
  52#if 1
  53#undef KERN_DEBUG
  54#define KERN_DEBUG KERN_WARNING
  55#endif
  56
  57MODULE_AUTHOR("Paul Mackerras (paulus@samba.org)");
  58MODULE_DESCRIPTION("PowerMac MESH SCSI driver");
  59MODULE_LICENSE("GPL");
  60
  61static int sync_rate = CONFIG_SCSI_MESH_SYNC_RATE;
  62static int sync_targets = 0xff;
  63static int resel_targets = 0xff;
  64static int debug_targets = 0;	/* print debug for these targets */
  65static int init_reset_delay = CONFIG_SCSI_MESH_RESET_DELAY_MS;
  66
  67module_param(sync_rate, int, 0);
  68MODULE_PARM_DESC(sync_rate, "Synchronous rate (0..10, 0=async)");
  69module_param(sync_targets, int, 0);
  70MODULE_PARM_DESC(sync_targets, "Bitmask of targets allowed to set synchronous");
  71module_param(resel_targets, int, 0);
  72MODULE_PARM_DESC(resel_targets, "Bitmask of targets allowed to set disconnect");
  73module_param(debug_targets, int, 0644);
  74MODULE_PARM_DESC(debug_targets, "Bitmask of debugged targets");
  75module_param(init_reset_delay, int, 0);
  76MODULE_PARM_DESC(init_reset_delay, "Initial bus reset delay (0=no reset)");
  77
  78static int mesh_sync_period = 100;
  79static int mesh_sync_offset = 0;
  80static unsigned char use_active_neg = 0;  /* bit mask for SEQ_ACTIVE_NEG if used */
  81
  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))
  86
  87#undef MESH_DBG
  88#define N_DBG_LOG	50
  89#define N_DBG_SLOG	20
  90#define NUM_DBG_EVENTS	13
  91#undef	DBG_USE_TB		/* bombs on 601 */
  92
  93struct dbglog {
  94	char	*fmt;
  95	u32	tb;
  96	u8	phase;
  97	u8	bs0;
  98	u8	bs1;
  99	u8	tgt;
 100	int	d;
 101};
 102
 103enum mesh_phase {
 104	idle,
 105	arbitrating,
 106	selecting,
 107	commanding,
 108	dataing,
 109	statusing,
 110	busfreeing,
 111	disconnecting,
 112	reselecting,
 113	sleeping
 114};
 115
 116enum msg_phase {
 117	msg_none,
 118	msg_out,
 119	msg_out_xxx,
 120	msg_out_last,
 121	msg_in,
 122	msg_in_bad,
 123};
 124
 125enum sdtr_phase {
 126	do_sdtr,
 127	sdtr_sent,
 128	sdtr_done
 129};
 130
 131struct mesh_target {
 132	enum sdtr_phase sdtr_state;
 133	int	sync_params;
 134	int	data_goes_out;		/* guess as to data direction */
 135	struct scsi_cmnd *current_req;
 136	u32	saved_ptr;
 137#ifdef MESH_DBG
 138	int	log_ix;
 139	int	n_log;
 140	struct dbglog log[N_DBG_LOG];
 141#endif
 142};
 143
 144struct mesh_state {
 145	volatile struct	mesh_regs __iomem *mesh;
 146	int	meshintr;
 147	volatile struct	dbdma_regs __iomem *dma;
 148	int	dmaintr;
 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 */
 157	int	data_ptr;
 158	int	dma_started;
 159	int	dma_count;
 160	int	stat;
 161	int	aborting;
 162	int	expect_reply;
 163	int	n_msgin;
 164	u8	msgin[16];
 165	int	n_msgout;
 166	int	last_n_msgout;
 167	u8	msgout[16];
 168	struct dbdma_cmd *dma_cmds;	/* space for dbdma commands, aligned */
 169	dma_addr_t dma_cmd_bus;
 170	void	*dma_cmd_space;
 171	int	dma_cmd_size;
 172	int	clk_freq;
 173	struct mesh_target tgts[8];
 174	struct macio_dev *mdev;
 175	struct pci_dev* pdev;
 176#ifdef MESH_DBG
 177	int	log_ix;
 178	int	n_log;
 179	struct dbglog log[N_DBG_SLOG];
 180#endif
 181};
 182
 183/*
 184 * Driver is too messy, we need a few prototypes...
 185 */
 186static void mesh_done(struct mesh_state *ms, int start_next);
 187static void mesh_interrupt(struct mesh_state *ms);
 188static void cmd_complete(struct mesh_state *ms);
 189static void set_dma_cmds(struct mesh_state *ms, struct scsi_cmnd *cmd);
 190static void halt_dma(struct mesh_state *ms);
 191static void phase_mismatch(struct mesh_state *ms);
 192
 193
 194/*
 195 * Some debugging & logging routines
 196 */
 197
 198#ifdef MESH_DBG
 199
 200static inline u32 readtb(void)
 201{
 202	u32 tb;
 203
 204#ifdef DBG_USE_TB
 205	/* Beware: if you enable this, it will crash on 601s. */
 206	asm ("mftb %0" : "=r" (tb) : );
 207#else
 208	tb = 0;
 209#endif
 210	return tb;
 211}
 212
 213static void dlog(struct mesh_state *ms, char *fmt, int a)
 214{
 215	struct mesh_target *tp = &ms->tgts[ms->conn_tgt];
 216	struct dbglog *tlp, *slp;
 217
 218	tlp = &tp->log[tp->log_ix];
 219	slp = &ms->log[ms->log_ix];
 220	tlp->fmt = fmt;
 221	tlp->tb = readtb();
 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;
 226	tlp->d = a;
 227	*slp = *tlp;
 228	if (++tp->log_ix >= N_DBG_LOG)
 229		tp->log_ix = 0;
 230	if (tp->n_log < N_DBG_LOG)
 231		++tp->n_log;
 232	if (++ms->log_ix >= N_DBG_SLOG)
 233		ms->log_ix = 0;
 234	if (ms->n_log < N_DBG_SLOG)
 235		++ms->n_log;
 236}
 237
 238static void dumplog(struct mesh_state *ms, int t)
 239{
 240	struct mesh_target *tp = &ms->tgts[t];
 241	struct dbglog *lp;
 242	int i;
 243
 244	if (tp->n_log == 0)
 245		return;
 246	i = tp->log_ix - tp->n_log;
 247	if (i < 0)
 248		i += N_DBG_LOG;
 249	tp->n_log = 0;
 250	do {
 251		lp = &tp->log[i];
 252		printk(KERN_DEBUG "mesh log %d: bs=%.2x%.2x ph=%.2x ",
 253		       t, lp->bs1, lp->bs0, lp->phase);
 254#ifdef DBG_USE_TB
 255		printk("tb=%10u ", lp->tb);
 256#endif
 257		printk(lp->fmt, lp->d);
 258		printk("\n");
 259		if (++i >= N_DBG_LOG)
 260			i = 0;
 261	} while (i != tp->log_ix);
 262}
 263
 264static void dumpslog(struct mesh_state *ms)
 265{
 266	struct dbglog *lp;
 267	int i;
 268
 269	if (ms->n_log == 0)
 270		return;
 271	i = ms->log_ix - ms->n_log;
 272	if (i < 0)
 273		i += N_DBG_SLOG;
 274	ms->n_log = 0;
 275	do {
 276		lp = &ms->log[i];
 277		printk(KERN_DEBUG "mesh log: bs=%.2x%.2x ph=%.2x t%d ",
 278		       lp->bs1, lp->bs0, lp->phase, lp->tgt);
 279#ifdef DBG_USE_TB
 280		printk("tb=%10u ", lp->tb);
 281#endif
 282		printk(lp->fmt, lp->d);
 283		printk("\n");
 284		if (++i >= N_DBG_SLOG)
 285			i = 0;
 286	} while (i != ms->log_ix);
 287}
 288
 289#else
 290
 291static inline void dlog(struct mesh_state *ms, char *fmt, int a)
 292{}
 293static inline void dumplog(struct mesh_state *ms, int tgt)
 294{}
 295static inline void dumpslog(struct mesh_state *ms)
 296{}
 297
 298#endif /* MESH_DBG */
 299
 300#define MKWORD(a, b, c, d)	(((a) << 24) + ((b) << 16) + ((c) << 8) + (d))
 301
 302static void
 303mesh_dump_regs(struct mesh_state *ms)
 304{
 305	volatile struct mesh_regs __iomem *mr = ms->mesh;
 306	volatile struct dbdma_regs __iomem *md = ms->dma;
 307	int t;
 308	struct mesh_target *tp;
 309
 310	printk(KERN_DEBUG "mesh: state at %p, regs at %p, dma at %p\n",
 311	       ms, mr, md);
 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,
 317	       mr->sync_params);
 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) {
 327		tp = &ms->tgts[t];
 328		if (tp->current_req == NULL)
 329			continue;
 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);
 332	}
 333}
 334
 335
 336/*
 337 * Flush write buffers on the bus path to the mesh
 338 */
 339static inline void mesh_flush_io(volatile struct mesh_regs __iomem *mr)
 340{
 341	(void)in_8(&mr->mesh_id);
 342}
 343
 344
 
 
 
 
 
 
 
 
 
 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.
 348 */
 349static void mesh_init(struct mesh_state *ms)
 350{
 351	volatile struct mesh_regs __iomem *mr = ms->mesh;
 352	volatile struct dbdma_regs __iomem *md = ms->dma;
 353
 354	mesh_flush_io(mr);
 355	udelay(100);
 356
 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);
 362	mesh_flush_io(mr);
 363	udelay(10);
 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);
 368
 369	if (init_reset_delay) {
 370		printk(KERN_INFO "mesh: performing initial bus reset...\n");
 371		
 372		/* Reset bus */
 373		out_8(&mr->bus_status1, BS1_RST);	/* assert RST */
 374		mesh_flush_io(mr);
 375		udelay(30);			/* leave it on for >= 25us */
 376		out_8(&mr->bus_status1, 0);	/* negate RST */
 377		mesh_flush_io(mr);
 378
 379		/* Wait for bus to come back */
 380		msleep(init_reset_delay);
 381	}
 382	
 383	/* Reconfigure controller */
 384	out_8(&mr->interrupt, 0xff);	/* clear all interrupt bits */
 385	out_8(&mr->sequence, SEQ_FLUSHFIFO);
 386	mesh_flush_io(mr);
 387	udelay(1);
 388	out_8(&mr->sync_params, ASYNC_PARAMS);
 389	out_8(&mr->sequence, SEQ_ENBRESEL);
 390
 391	ms->phase = idle;
 392	ms->msgphase = msg_none;
 393}
 394
 395
 396static void mesh_start_cmd(struct mesh_state *ms, struct scsi_cmnd *cmd)
 397{
 398	volatile struct mesh_regs __iomem *mr = ms->mesh;
 399	int t, id;
 400
 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;
 405
 406#if 1
 407	if (DEBUG_TARGET(cmd)) {
 408		int i;
 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));
 414	}
 415#endif
 416	if (ms->dma_started)
 417		panic("mesh: double DMA start !\n");
 418
 419	ms->phase = arbitrating;
 420	ms->msgphase = msg_none;
 421	ms->data_ptr = 0;
 422	ms->dma_started = 0;
 423	ms->n_msgout = 0;
 424	ms->last_n_msgout = 0;
 425	ms->expect_reply = 0;
 426	ms->conn_tgt = id;
 427	ms->tgts[id].saved_ptr = 0;
 428	ms->stat = DID_OK;
 429	ms->aborting = 0;
 430#ifdef MESH_DBG
 431	ms->tgts[id].n_log = 0;
 432	dlog(ms, "start cmd=%x", (int) cmd);
 433#endif
 434
 435	/* Off we go */
 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);
 440	mesh_flush_io(mr);
 441	udelay(1);
 442
 443	if (in_8(&mr->bus_status1) & (BS1_BSY | BS1_SEL)) {
 444		/*
 445		 * Some other device has the bus or is arbitrating for it -
 446		 * probably a target which is about to reselect us.
 447		 */
 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)
 453				break;
 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));
 458				mesh_interrupt(ms);
 459				if (ms->phase != arbitrating)
 460					return;
 461			}
 462			udelay(1);
 463		}
 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;
 467			ms->phase = idle;
 468			mesh_done(ms, 0);
 469			return;
 470		}
 471	}
 472
 473	/*
 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.
 476	 */
 477	out_8(&mr->dest_id, mr->source_id);
 478
 479	/*
 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
 489	 * reselection.
 490	 */
 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));
 496		mesh_interrupt(ms);
 497		if (ms->phase != arbitrating)
 498			return;
 499		dlog(ms, "after intr after disresel, intr/exc/err/fc=%.8x",
 500		     MKWORD(mr->interrupt, mr->exception,
 501			    mr->error, mr->fifo_count));
 502	}
 503
 504	out_8(&mr->sequence, SEQ_ARBITRATE);
 505
 506	for (t = 230; t > 0; --t) {
 507		if (in_8(&mr->interrupt) != 0)
 508			break;
 509		udelay(1);
 510	}
 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);
 519		mesh_flush_io(mr);
 520		udelay(10);
 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);
 524		mesh_flush_io(mr);
 525		for (t = 10; t > 0 && in_8(&mr->interrupt) == 0; --t)
 526			udelay(1);
 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");
 534			/*
 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
 538			 * reset the bus.
 539			 */
 540		}
 541#endif
 542	}
 543}
 544
 545/*
 546 * Start the next command for a MESH.
 547 * Should be called with interrupts disabled.
 548 */
 549static void mesh_start(struct mesh_state *ms)
 550{
 551	struct scsi_cmnd *cmd, *prev, *next;
 552
 553	if (ms->phase != idle || ms->current_req != NULL) {
 554		printk(KERN_ERR "inappropriate mesh_start (phase=%d, ms=%p)",
 555		       ms->phase, ms);
 556		return;
 557	}
 558
 559	while (ms->phase == idle) {
 560		prev = NULL;
 561		for (cmd = ms->request_q; ; cmd = (struct scsi_cmnd *) cmd->host_scribble) {
 562			if (cmd == NULL)
 563				return;
 564			if (ms->tgts[cmd->device->id].current_req == NULL)
 565				break;
 566			prev = cmd;
 567		}
 568		next = (struct scsi_cmnd *) cmd->host_scribble;
 569		if (prev == NULL)
 570			ms->request_q = next;
 571		else
 572			prev->host_scribble = (void *) next;
 573		if (next == NULL)
 574			ms->request_qtail = prev;
 575
 576		mesh_start_cmd(ms, cmd);
 577	}
 578}
 579
 580static void mesh_done(struct mesh_state *ms, int start_next)
 581{
 582	struct scsi_cmnd *cmd;
 583	struct mesh_target *tp = &ms->tgts[ms->conn_tgt];
 584
 585	cmd = ms->current_req;
 586	ms->current_req = NULL;
 587	tp->current_req = NULL;
 588	if (cmd) {
 589		struct mesh_cmd_priv *mcmd = mesh_priv(cmd);
 590
 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));
 598#if 0
 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]);
 605			}
 606#endif
 607		}
 608		mcmd->this_residual -= ms->data_ptr;
 609		scsi_done(cmd);
 610	}
 611	if (start_next) {
 612		out_8(&ms->mesh->sequence, SEQ_ENBRESEL);
 613		mesh_flush_io(ms->mesh);
 614		udelay(1);
 615		ms->phase = idle;
 616		mesh_start(ms);
 617	}
 618}
 619
 620static inline void add_sdtr_msg(struct mesh_state *ms)
 621{
 622	int i = ms->n_msgout;
 623
 624	ms->msgout[i] = EXTENDED_MESSAGE;
 625	ms->msgout[i+1] = 3;
 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;
 630}
 631
 632static void set_sdtr(struct mesh_state *ms, int period, int offset)
 633{
 634	struct mesh_target *tp = &ms->tgts[ms->conn_tgt];
 635	volatile struct mesh_regs __iomem *mr = ms->mesh;
 636	int v, tr;
 637
 638	tp->sdtr_state = sdtr_done;
 639	if (offset == 0) {
 640		/* asynchronous */
 641		if (SYNC_OFF(tp->sync_params))
 642			printk(KERN_INFO "mesh: target %d now asynchronous\n",
 643			       ms->conn_tgt);
 644		tp->sync_params = ASYNC_PARAMS;
 645		out_8(&mr->sync_params, ASYNC_PARAMS);
 646		return;
 647	}
 648	/*
 649	 * We need to compute ceil(clk_freq * period / 500e6) - 2
 650	 * without incurring overflow.
 651	 */
 652	v = (ms->clk_freq / 5000) * period;
 653	if (v <= 250000) {
 654		/* special case: sync_period == 5 * clk_period */
 655		v = 0;
 656		/* units of tr are 100kB/s */
 657		tr = (ms->clk_freq + 250000) / 500000;
 658	} else {
 659		/* sync_period == (v + 2) * 2 * clk_period */
 660		v = (v + 99999) / 100000 - 2;
 661		if (v > 15)
 662			v = 15;	/* oops */
 663		tr = ((ms->clk_freq / (v + 2)) + 199999) / 200000;
 664	}
 665	if (offset > 15)
 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);
 671}
 672
 673static void start_phase(struct mesh_state *ms)
 674{
 675	int i, seq, nb;
 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];
 680
 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) {
 686	case msg_none:
 687		break;
 688
 689	case msg_in:
 690		out_8(&mr->count_hi, 0);
 691		out_8(&mr->count_lo, 1);
 692		out_8(&mr->sequence, SEQ_MSGIN + seq);
 693		ms->n_msgin = 0;
 694		return;
 695
 696	case msg_out:
 697		/*
 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.
 701		 */
 702		if (ms->n_msgout <= 0) {
 703			printk(KERN_ERR "mesh: msg_out but n_msgout=%d\n",
 704			       ms->n_msgout);
 705			mesh_dump_regs(ms);
 706			ms->msgphase = msg_none;
 707			break;
 708		}
 709		if (ALLOW_DEBUG(ms->conn_tgt)) {
 710			printk(KERN_DEBUG "mesh: sending %d msg bytes:",
 711			       ms->n_msgout);
 712			for (i = 0; i < ms->n_msgout; ++i)
 713				printk(" %x", ms->msgout[i]);
 714			printk("\n");
 715		}
 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);
 720		mesh_flush_io(mr);
 721		udelay(1);
 722		/*
 723		 * If ATN is not already asserted, we assert it, then
 724		 * issue a SEQ_MSGOUT to get the mesh to drop ACK.
 725		 */
 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 */
 729			mesh_flush_io(mr);
 730			udelay(1);
 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);
 735		}
 736		if (ms->n_msgout == 1) {
 737			/*
 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.
 742			 */
 743			cmd_complete(ms);
 744		} else {
 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]);
 749		}
 750		return;
 751
 752	default:
 753		printk(KERN_ERR "mesh bug: start_phase msgphase=%d\n",
 754		       ms->msgphase);
 755	}
 756
 757	switch (ms->phase) {
 758	case selecting:
 759		out_8(&mr->dest_id, ms->conn_tgt);
 760		out_8(&mr->sequence, SEQ_SELECT + SEQ_ATN);
 761		break;
 762	case commanding:
 763		out_8(&mr->sync_params, tp->sync_params);
 764		out_8(&mr->count_hi, 0);
 765		if (cmd) {
 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]);
 770		} else {
 771			out_8(&mr->count_lo, 6);
 772			out_8(&mr->sequence, SEQ_COMMAND + seq);
 773			for (i = 0; i < 6; ++i)
 774				out_8(&mr->fifo, 0);
 775		}
 776		break;
 777	case dataing:
 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);
 783			ms->dma_started = 1;
 784		}
 785		nb = ms->dma_count;
 786		if (nb > 0xfff0)
 787			nb = 0xfff0;
 788		ms->dma_count -= nb;
 789		ms->data_ptr += nb;
 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);
 794		break;
 795	case statusing:
 796		out_8(&mr->count_hi, 0);
 797		out_8(&mr->count_lo, 1);
 798		out_8(&mr->sequence, SEQ_STATUS + seq);
 799		break;
 800	case busfreeing:
 801	case disconnecting:
 802		out_8(&mr->sequence, SEQ_ENBRESEL);
 803		mesh_flush_io(mr);
 804		udelay(1);
 805		dlog(ms, "enbresel intr/exc/err/fc=%.8x",
 806		     MKWORD(mr->interrupt, mr->exception, mr->error,
 807			    mr->fifo_count));
 808		out_8(&mr->sequence, SEQ_BUSFREE);
 809		break;
 810	default:
 811		printk(KERN_ERR "mesh: start_phase called with phase=%d\n",
 812		       ms->phase);
 813		dumpslog(ms);
 814	}
 815
 816}
 817
 818static inline void get_msgin(struct mesh_state *ms)
 819{
 820	volatile struct mesh_regs __iomem *mr = ms->mesh;
 821	int i, n;
 822
 823	n = mr->fifo_count;
 824	if (n != 0) {
 825		i = ms->n_msgin;
 826		ms->n_msgin = i + n;
 827		for (; n > 0; --n)
 828			ms->msgin[i++] = in_8(&mr->fifo);
 829	}
 830}
 831
 832static inline int msgin_length(struct mesh_state *ms)
 833{
 834	int b, n;
 835
 836	n = 1;
 837	if (ms->n_msgin > 0) {
 838		b = ms->msgin[0];
 839		if (b == 1) {
 840			/* extended message */
 841			n = ms->n_msgin < 2? 2: ms->msgin[1] + 2;
 842		} else if (0x20 <= b && b <= 0x2f) {
 843			/* 2-byte message */
 844			n = 2;
 845		}
 846	}
 847	return n;
 848}
 849
 850static void reselected(struct mesh_state *ms)
 851{
 852	volatile struct mesh_regs __iomem *mr = ms->mesh;
 853	struct scsi_cmnd *cmd;
 854	struct mesh_target *tp;
 855	int b, t, prev;
 856
 857	switch (ms->phase) {
 858	case idle:
 859		break;
 860	case arbitrating:
 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;
 866			ms->request_q = cmd;
 867			tp = &ms->tgts[cmd->device->id];
 868			tp->current_req = NULL;
 869		}
 870		break;
 871	case busfreeing:
 872		ms->phase = reselecting;
 873		mesh_done(ms, 0);
 874		break;
 875	case disconnecting:
 876		break;
 877	default:
 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);
 881		dumpslog(ms);
 882	}
 883
 884	if (ms->dma_started) {
 885		printk(KERN_ERR "mesh: reselected with DMA started !\n");
 886		halt_dma(ms);
 887	}
 888	ms->current_req = NULL;
 889	ms->phase = dataing;
 890	ms->msgphase = msg_in;
 891	ms->n_msgout = 0;
 892	ms->last_n_msgout = 0;
 893	prev = ms->conn_tgt;
 894
 895	/*
 896	 * We seem to get abortive reselections sometimes.
 897	 */
 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);
 902		mesh_flush_io(mr);
 903		udelay(1);
 904		out_8(&mr->sequence, SEQ_ENBRESEL);
 905		mesh_flush_io(mr);
 906		udelay(5);
 907		dlog(ms, "extra resel err/exc/fc = %.6x",
 908		     MKWORD(0, mr->error, mr->exception, mr->fifo_count));
 909	}
 910	out_8(&mr->interrupt, INT_ERROR | INT_EXCEPTION | INT_CMDDONE);
 911       	mesh_flush_io(mr);
 912	udelay(1);
 913	out_8(&mr->sequence, SEQ_ENBRESEL);
 914       	mesh_flush_io(mr);
 915	udelay(1);
 916	out_8(&mr->sync_params, ASYNC_PARAMS);
 917
 918	/*
 919	 * Find out who reselected us.
 920	 */
 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;
 924		goto bogus;
 925	}
 926	/* get the last byte in the fifo */
 927	do {
 928		b = in_8(&mr->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)
 933			break;
 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;
 937		goto bogus;
 938	}
 939
 940
 941	/*
 942	 * Set up to continue with that target's transfer.
 943	 */
 944	ms->conn_tgt = t;
 945	tp = &ms->tgts[t];
 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);
 951	}
 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);
 955		goto bogus;
 956	}
 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));
 960	start_phase(ms);
 961	return;
 962
 963bogus:
 964	dumplog(ms, ms->conn_tgt);
 965	dumpslog(ms);
 966	ms->data_ptr = 0;
 967	ms->aborting = 1;
 968	start_phase(ms);
 969}
 970
 971static void do_abort(struct mesh_state *ms)
 972{
 973	ms->msgout[0] = ABORT;
 974	ms->n_msgout = 1;
 975	ms->aborting = 1;
 976	ms->stat = DID_ABORT;
 977	dlog(ms, "abort", 0);
 978}
 979
 980static void handle_reset(struct mesh_state *ms)
 981{
 982	int tgt;
 983	struct mesh_target *tp;
 984	struct scsi_cmnd *cmd;
 985	volatile struct mesh_regs __iomem *mr = ms->mesh;
 986
 987	for (tgt = 0; tgt < 8; ++tgt) {
 988		tp = &ms->tgts[tgt];
 989		if ((cmd = tp->current_req) != NULL) {
 990			set_host_byte(cmd, DID_RESET);
 991			tp->current_req = NULL;
 992			scsi_done(cmd);
 993		}
 994		ms->tgts[tgt].sdtr_state = do_sdtr;
 995		ms->tgts[tgt].sync_params = ASYNC_PARAMS;
 996	}
 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);
1001		scsi_done(cmd);
1002	}
1003	ms->phase = idle;
1004	ms->msgphase = msg_none;
1005	out_8(&mr->interrupt, INT_ERROR | INT_EXCEPTION | INT_CMDDONE);
1006	out_8(&mr->sequence, SEQ_FLUSHFIFO);
1007       	mesh_flush_io(mr);
1008	udelay(1);
1009	out_8(&mr->sync_params, ASYNC_PARAMS);
1010	out_8(&mr->sequence, SEQ_ENBRESEL);
1011}
1012
1013static irqreturn_t do_mesh_interrupt(int irq, void *dev_id)
1014{
1015	unsigned long flags;
1016	struct mesh_state *ms = dev_id;
1017	struct Scsi_Host *dev = ms->host;
1018	
1019	spin_lock_irqsave(dev->host_lock, flags);
1020	mesh_interrupt(ms);
1021	spin_unlock_irqrestore(dev->host_lock, flags);
1022	return IRQ_HANDLED;
1023}
1024
1025static void handle_error(struct mesh_state *ms)
1026{
1027	int err, exc, count;
1028	volatile struct mesh_regs __iomem *mr = ms->mesh;
1029
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)
1040			udelay(1);
1041		printk("done\n");
1042		if (ms->dma_started)
1043			halt_dma(ms);
1044		handle_reset(ms);
1045		/* request_q is empty, no point in mesh_start() */
1046		return;
1047	}
1048	if (err & ERR_UNEXPDISC) {
1049		/* Unexpected disconnect */
1050		if (exc & EXC_RESELECTED) {
1051			reselected(ms);
1052			return;
1053		}
1054		if (!ms->aborting) {
1055			printk(KERN_WARNING "mesh: target %d aborted\n",
1056			       ms->conn_tgt);
1057			dumplog(ms, ms->conn_tgt);
1058			dumpslog(ms);
1059		}
1060		out_8(&mr->interrupt, INT_CMDDONE);
1061		ms->stat = DID_ABORT;
1062		mesh_done(ms, 1);
1063		return;
1064	}
1065	if (err & ERR_PARITY) {
1066		if (ms->msgphase == msg_in) {
1067			printk(KERN_ERR "mesh: msg parity error, target %d\n",
1068			       ms->conn_tgt);
1069			ms->msgout[0] = MSG_PARITY_ERROR;
1070			ms->n_msgout = 1;
1071			ms->msgphase = msg_in_bad;
1072			cmd_complete(ms);
1073			return;
1074		}
1075		if (ms->stat == DID_OK) {
1076			printk(KERN_ERR "mesh: parity error, target %d\n",
1077			       ms->conn_tgt);
1078			ms->stat = DID_PARITY;
1079		}
1080		count = (mr->count_hi << 8) + mr->count_lo;
1081		if (count == 0) {
1082			cmd_complete(ms);
1083		} else {
1084			/* reissue the data transfer command */
1085			out_8(&mr->sequence, mr->sequence);
1086		}
1087		return;
1088	}
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++;
1095			reselected(ms);
1096			return;
1097		}
1098		if (exc == EXC_PHASEMM) {
1099			static int mesh_phasemm_seqerr;
1100			mesh_phasemm_seqerr++;
1101			phase_mismatch(ms);
1102			return;
1103		}
1104		printk(KERN_ERR "mesh: sequence error (err=%x exc=%x)\n",
1105		       err, exc);
1106	} else {
1107		printk(KERN_ERR "mesh: unknown error %x (exc=%x)\n", err, exc);
1108	}
1109	mesh_dump_regs(ms);
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 */
1113		do_abort(ms);
1114		phase_mismatch(ms);
1115		return;
1116	}
1117	ms->stat = DID_ERROR;
1118	mesh_done(ms, 1);
1119}
1120
1121static void handle_exception(struct mesh_state *ms)
1122{
1123	int exc;
1124	volatile struct mesh_regs __iomem *mr = ms->mesh;
1125
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;
1130		mesh_resel_exc++;
1131		reselected(ms);
1132	} else if (exc == EXC_ARBLOST) {
1133		printk(KERN_DEBUG "mesh: lost arbitration\n");
1134		ms->stat = DID_BUS_BUSY;
1135		mesh_done(ms, 1);
1136	} else if (exc == EXC_SELTO) {
1137		/* selection timed out */
1138		ms->stat = DID_BAD_TARGET;
1139		mesh_done(ms, 1);
1140	} else if (exc == EXC_PHASEMM) {
1141		/* target wants to do something different:
1142		   find out what it wants and do it. */
1143		phase_mismatch(ms);
1144	} else {
1145		printk(KERN_ERR "mesh: can't cope with exception %x\n", exc);
1146		mesh_dump_regs(ms);
1147		dumplog(ms, ms->conn_tgt);
1148		do_abort(ms);
1149		phase_mismatch(ms);
1150	}
1151}
1152
1153static void handle_msgin(struct mesh_state *ms)
1154{
1155	int i, code;
1156	struct scsi_cmnd *cmd = ms->current_req;
1157	struct mesh_target *tp = &ms->tgts[ms->conn_tgt];
1158
1159	if (ms->n_msgin == 0)
1160		return;
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]);
1166		printk("\n");
1167	}
1168	dlog(ms, "msgin msg=%.8x",
1169	     MKWORD(ms->n_msgin, code, ms->msgin[1], ms->msgin[2]));
1170
1171	ms->expect_reply = 0;
1172	ms->n_msgout = 0;
1173	if (ms->n_msgin < msgin_length(ms))
1174		goto reject;
1175	if (cmd)
1176		mesh_priv(cmd)->message = code;
1177	switch (code) {
1178	case COMMAND_COMPLETE:
1179		break;
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);
1185			break;
1186		case EXTENDED_SDTR:
1187			if (tp->sdtr_state != sdtr_sent) {
1188				/* reply with an SDTR */
1189				add_sdtr_msg(ms);
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;
1198			} else {
1199				set_sdtr(ms, ms->msgin[3], ms->msgin[4]);
1200			}
1201			break;
1202		default:
1203			goto reject;
1204		}
1205		break;
1206	case SAVE_POINTERS:
1207		tp->saved_ptr = ms->data_ptr;
1208		break;
1209	case RESTORE_POINTERS:
1210		ms->data_ptr = tp->saved_ptr;
1211		break;
1212	case DISCONNECT:
1213		ms->phase = disconnecting;
1214		break;
1215	case ABORT:
1216		break;
1217	case MESSAGE_REJECT:
1218		if (tp->sdtr_state == sdtr_sent)
1219			set_sdtr(ms, 0, 0);
1220		break;
1221	case NOP:
1222		break;
1223	default:
1224		if (IDENTIFY_BASE <= code && code <= IDENTIFY_BASE + 7) {
1225			if (cmd == NULL) {
1226				do_abort(ms);
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);
1233			}
1234			break;
1235		}
1236		goto reject;
1237	}
1238	return;
1239
1240 reject:
1241	printk(KERN_WARNING "mesh: rejecting message from target %d:",
1242	       ms->conn_tgt);
1243	for (i = 0; i < ms->n_msgin; ++i)
1244		printk(" %x", ms->msgin[i]);
1245	printk("\n");
1246	ms->msgout[0] = MESSAGE_REJECT;
1247	ms->n_msgout = 1;
1248	ms->msgphase = msg_out;
1249}
1250
1251/*
1252 * Set up DMA commands for transferring data.
1253 */
1254static void set_dma_cmds(struct mesh_state *ms, struct scsi_cmnd *cmd)
1255{
1256	int i, dma_cmd, total, off, dtot;
1257	struct scatterlist *scl;
1258	struct dbdma_cmd *dcmds;
1259
1260	dma_cmd = ms->tgts[ms->conn_tgt].data_goes_out?
1261		OUTPUT_MORE: INPUT_MORE;
1262	dcmds = ms->dma_cmds;
1263	dtot = 0;
1264	if (cmd) {
1265		int nseg;
1266
1267		mesh_priv(cmd)->this_residual = scsi_bufflen(cmd);
1268
1269		nseg = scsi_dma_map(cmd);
1270		BUG_ON(nseg < 0);
1271
1272		if (nseg) {
1273			total = 0;
1274			off = ms->data_ptr;
1275
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);
1279				
1280				total += scl->length;
1281				if (off >= dma_len) {
1282					off -= dma_len;
1283					continue;
1284				}
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;
1291				++dcmds;
1292				dtot += dma_len - off;
1293				off = 0;
1294			}
1295		}
1296	}
1297	if (dtot == 0) {
1298		/* Either the target has overrun our buffer,
1299		   or the caller didn't provide a buffer. */
1300		static char mesh_extra_buf[64];
1301
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;
1306		++dcmds;
1307	}
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;
1313}
1314
1315static void halt_dma(struct mesh_state *ms)
1316{
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;
1320	int t, nb;
1321
1322	if (!ms->tgts[ms->conn_tgt].data_goes_out) {
1323		/* wait a little while until the fifo drains */
1324		t = 50;
1325		while (t > 0 && in_8(&mr->fifo_count) != 0
1326		       && (in_le32(&md->status) & ACTIVE) != 0) {
1327			--t;
1328			udelay(1);
1329		}
1330	}
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. */
1339	ms->data_ptr -= nb;
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);
1344		ms->data_ptr = 0;
1345#ifdef MESH_DBG
1346		dumplog(ms, ms->conn_tgt);
1347		dumpslog(ms);
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);
1355	}
1356	if (cmd)
1357		scsi_dma_unmap(cmd);
1358	ms->dma_started = 0;
1359}
1360
1361static void phase_mismatch(struct mesh_state *ms)
1362{
1363	volatile struct mesh_regs __iomem *mr = ms->mesh;
1364	int phase;
1365
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);
1373		mesh_flush_io(mr);
1374		udelay(1);
1375		out_8(&mr->fifo, ms->msgout[ms->n_msgout-1]);
1376		ms->msgphase = msg_out_last;
1377		return;
1378	}
1379
1380	if (ms->msgphase == msg_in) {
1381		get_msgin(ms);
1382		if (ms->n_msgin)
1383			handle_msgin(ms);
1384	}
1385
1386	if (ms->dma_started)
1387		halt_dma(ms);
1388	if (mr->fifo_count) {
1389		out_8(&mr->sequence, SEQ_FLUSHFIFO);
1390		mesh_flush_io(mr);
1391		udelay(1);
1392	}
1393
1394	ms->msgphase = msg_none;
1395	switch (phase) {
1396	case BP_DATAIN:
1397		ms->tgts[ms->conn_tgt].data_goes_out = 0;
1398		ms->phase = dataing;
1399		break;
1400	case BP_DATAOUT:
1401		ms->tgts[ms->conn_tgt].data_goes_out = 1;
1402		ms->phase = dataing;
1403		break;
1404	case BP_COMMAND:
1405		ms->phase = commanding;
1406		break;
1407	case BP_STATUS:
1408		ms->phase = statusing;
1409		break;
1410	case BP_MSGIN:
1411		ms->msgphase = msg_in;
1412		ms->n_msgin = 0;
1413		break;
1414	case BP_MSGOUT:
1415		ms->msgphase = msg_out;
1416		if (ms->n_msgout == 0) {
1417			if (ms->aborting) {
1418				do_abort(ms);
1419			} else {
1420				if (ms->last_n_msgout == 0) {
1421					printk(KERN_DEBUG
1422					       "mesh: no msg to repeat\n");
1423					ms->msgout[0] = NOP;
1424					ms->last_n_msgout = 1;
1425				}
1426				ms->n_msgout = ms->last_n_msgout;
1427			}
1428		}
1429		break;
1430	default:
1431		printk(KERN_DEBUG "mesh: unknown scsi phase %x\n", phase);
1432		ms->stat = DID_ERROR;
1433		mesh_done(ms, 1);
1434		return;
1435	}
1436
1437	start_phase(ms);
1438}
1439
1440static void cmd_complete(struct mesh_state *ms)
1441{
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];
1445	int seq, n, t;
1446
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) {
1450	case msg_out_xxx:
1451		/* huh?  we expected a phase mismatch */
1452		ms->n_msgin = 0;
1453		ms->msgphase = msg_in;
1454		fallthrough;
1455
1456	case msg_in:
1457		/* should have some message bytes in fifo */
1458		get_msgin(ms);
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);
1463		} else {
1464			ms->msgphase = msg_none;
1465			handle_msgin(ms);
1466			start_phase(ms);
1467		}
1468		break;
1469
1470	case msg_in_bad:
1471		out_8(&mr->sequence, SEQ_FLUSHFIFO);
1472		mesh_flush_io(mr);
1473		udelay(1);
1474		out_8(&mr->count_lo, 1);
1475		out_8(&mr->sequence, SEQ_MSGIN + SEQ_ATN + use_active_neg);
1476		break;
1477
1478	case msg_out:
1479		/*
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.
1489		 */
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)
1494			udelay(1);
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;
1501			ms->n_msgout = 0;
1502			if (in_8(&mr->interrupt) & INT_ERROR) {
1503				printk(KERN_ERR "mesh: error %x in msg_out\n",
1504				       in_8(&mr->error));
1505				handle_error(ms);
1506				return;
1507			}
1508			if (in_8(&mr->exception) != EXC_PHASEMM)
1509				printk(KERN_ERR "mesh: exc %x in msg_out\n",
1510				       in_8(&mr->exception));
1511			else
1512				printk(KERN_DEBUG "mesh: bs0=%x in msg_out\n",
1513				       in_8(&mr->bus_status0));
1514			handle_exception(ms);
1515			return;
1516		}
1517		if (in_8(&mr->bus_status0) & BS0_REQ) {
1518			out_8(&mr->sequence, SEQ_MSGOUT + use_active_neg);
1519			mesh_flush_io(mr);
1520			udelay(1);
1521			out_8(&mr->fifo, ms->msgout[ms->n_msgout-1]);
1522			ms->msgphase = msg_out_last;
1523		} else {
1524			out_8(&mr->sequence, SEQ_MSGIN + use_active_neg + SEQ_ATN);
1525			ms->msgphase = msg_out_xxx;
1526		}
1527		break;
1528
1529	case msg_out_last:
1530		ms->last_n_msgout = ms->n_msgout;
1531		ms->n_msgout = 0;
1532		ms->msgphase = ms->expect_reply? msg_in: msg_none;
1533		start_phase(ms);
1534		break;
1535
1536	case msg_none:
1537		switch (ms->phase) {
1538		case idle:
1539			printk(KERN_ERR "mesh: interrupt in idle phase?\n");
1540			dumpslog(ms);
1541			return;
1542		case selecting:
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));
1546			ms->n_msgout = 1;
1547			ms->expect_reply = 0;
1548			if (ms->aborting) {
1549				ms->msgout[0] = ABORT;
1550				ms->n_msgout++;
1551			} else if (tp->sdtr_state == do_sdtr) {
1552				/* add SDTR message */
1553				add_sdtr_msg(ms);
1554				ms->expect_reply = 1;
1555				tp->sdtr_state = sdtr_sent;
1556			}
1557			ms->msgphase = msg_out;
1558			/*
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.
1564			 */
1565			t = 230;		/* wait up to 230us */
1566			while ((in_8(&mr->bus_status0) & BS0_REQ) == 0) {
1567				if (--t < 0) {
1568					dlog(ms, "impatient for req", ms->n_msgout);
1569					ms->msgphase = msg_none;
1570					break;
1571				}
1572				udelay(1);
1573			}
1574			break;
1575		case dataing:
1576			if (ms->dma_count != 0) {
1577				start_phase(ms);
1578				return;
1579			}
1580			/*
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.
1589			 */
1590			out_8(&mr->sequence, 0);
1591			out_8(&mr->interrupt,
1592			      INT_ERROR | INT_EXCEPTION | INT_CMDDONE);
1593			halt_dma(ms);
1594			break;
1595		case statusing:
1596			if (cmd) {
1597				struct mesh_cmd_priv *mcmd = mesh_priv(cmd);
1598
1599				mcmd->status = mr->fifo;
1600				if (DEBUG_TARGET(cmd))
1601					printk(KERN_DEBUG "mesh: status is %x\n",
1602					       mcmd->status);
1603			}
1604			ms->msgphase = msg_in;
1605			break;
1606		case busfreeing:
1607			mesh_done(ms, 1);
1608			return;
1609		case disconnecting:
1610			ms->current_req = NULL;
1611			ms->phase = idle;
1612			mesh_start(ms);
1613			return;
1614		default:
1615			break;
1616		}
1617		++ms->phase;
1618		start_phase(ms);
1619		break;
1620	}
1621}
1622
1623
1624/*
1625 * Called by midlayer with host locked to queue a new
1626 * request
1627 */
1628static int mesh_queue_lck(struct scsi_cmnd *cmd)
1629{
1630	struct mesh_state *ms;
1631
 
1632	cmd->host_scribble = NULL;
1633
1634	ms = (struct mesh_state *) cmd->device->host->hostdata;
1635
1636	if (ms->request_q == NULL)
1637		ms->request_q = cmd;
1638	else
1639		ms->request_qtail->host_scribble = (void *) cmd;
1640	ms->request_qtail = cmd;
1641
1642	if (ms->phase == idle)
1643		mesh_start(ms);
1644
1645	return 0;
1646}
1647
1648static DEF_SCSI_QCMD(mesh_queue)
1649
1650/*
1651 * Called to handle interrupts, either call by the interrupt
1652 * handler (do_mesh_interrupt) or by other functions in
1653 * exceptional circumstances
1654 */
1655static void mesh_interrupt(struct mesh_state *ms)
1656{
1657	volatile struct mesh_regs __iomem *mr = ms->mesh;
1658	int intr;
1659
1660#if 0
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);
1666#endif
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) {
1671			handle_error(ms);
1672		} else if (intr & INT_EXCEPTION) {
1673			handle_exception(ms);
1674		} else if (intr & INT_CMDDONE) {
1675			out_8(&mr->interrupt, INT_CMDDONE);
1676			cmd_complete(ms);
1677		}
1678	}
1679}
1680
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.
1684 */
1685static int mesh_abort(struct scsi_cmnd *cmd)
1686{
1687	struct mesh_state *ms = (struct mesh_state *) cmd->device->host->hostdata;
1688
1689	printk(KERN_DEBUG "mesh_abort(%p)\n", cmd);
1690	mesh_dump_regs(ms);
1691	dumplog(ms, cmd->device->id);
1692	dumpslog(ms);
1693	return FAILED;
1694}
1695
1696/*
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
1701 */
1702static int mesh_host_reset(struct scsi_cmnd *cmd)
1703{
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;
1708
1709	printk(KERN_DEBUG "mesh_host_reset\n");
1710
1711	spin_lock_irqsave(ms->host->host_lock, flags);
1712
1713	if (ms->dma_started)
1714		halt_dma(ms);
1715
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);
1721       	mesh_flush_io(mr);
1722	udelay(1);
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);
1727
1728	/* Reset the bus */
1729	out_8(&mr->bus_status1, BS1_RST);	/* assert RST */
1730       	mesh_flush_io(mr);
1731	udelay(30);			/* leave it on for >= 25us */
1732	out_8(&mr->bus_status1, 0);	/* negate RST */
1733
1734	/* Complete pending commands */
1735	handle_reset(ms);
1736	
1737	spin_unlock_irqrestore(ms->host->host_lock, flags);
1738	return SUCCESS;
1739}
1740
1741static void set_mesh_power(struct mesh_state *ms, int state)
1742{
1743	if (!machine_is(powermac))
1744		return;
1745	if (state) {
1746		pmac_call_feature(PMAC_FTR_MESH_ENABLE, macio_get_of_node(ms->mdev), 0, 1);
1747		msleep(200);
1748	} else {
1749		pmac_call_feature(PMAC_FTR_MESH_ENABLE, macio_get_of_node(ms->mdev), 0, 0);
1750		msleep(10);
1751	}
1752}
1753
1754
1755#ifdef CONFIG_PM
1756static int mesh_suspend(struct macio_dev *mdev, pm_message_t mesg)
1757{
1758	struct mesh_state *ms = (struct mesh_state *)macio_get_drvdata(mdev);
1759	unsigned long flags;
1760
1761	switch (mesg.event) {
1762	case PM_EVENT_SUSPEND:
1763	case PM_EVENT_HIBERNATE:
1764	case PM_EVENT_FREEZE:
1765		break;
1766	default:
1767		return 0;
1768	}
1769	if (ms->phase == sleeping)
1770		return 0;
1771
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);
1776		msleep(10);
1777		spin_lock_irqsave(ms->host->host_lock, flags);
1778	}
1779	ms->phase = sleeping;
1780	spin_unlock_irqrestore(ms->host->host_lock, flags);
1781	disable_irq(ms->meshintr);
1782	set_mesh_power(ms, 0);
1783
1784	return 0;
1785}
1786
1787static int mesh_resume(struct macio_dev *mdev)
1788{
1789	struct mesh_state *ms = (struct mesh_state *)macio_get_drvdata(mdev);
1790	unsigned long flags;
1791
1792	if (ms->phase != sleeping)
1793		return 0;
1794
1795	set_mesh_power(ms, 1);
1796	mesh_init(ms);
1797	spin_lock_irqsave(ms->host->host_lock, flags);
1798	mesh_start(ms);
1799	spin_unlock_irqrestore(ms->host->host_lock, flags);
1800	enable_irq(ms->meshintr);
1801	scsi_unblock_requests(ms->host);
1802
1803	return 0;
1804}
1805
1806#endif /* CONFIG_PM */
1807
1808/*
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.
1812 */
1813static int mesh_shutdown(struct macio_dev *mdev)
1814{
1815	struct mesh_state *ms = (struct mesh_state *)macio_get_drvdata(mdev);
1816	volatile struct mesh_regs __iomem *mr;
1817	unsigned long flags;
1818
1819       	printk(KERN_INFO "resetting MESH scsi bus(es)\n");
1820	spin_lock_irqsave(ms->host->host_lock, flags);
1821       	mr = ms->mesh;
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);
1825	mesh_flush_io(mr);
1826	udelay(30);
1827	out_8(&mr->bus_status1, 0);
1828	spin_unlock_irqrestore(ms->host->host_lock, flags);
1829
1830	return 0;
1831}
1832
1833static const struct scsi_host_template mesh_template = {
1834	.proc_name			= "mesh",
1835	.name				= "MESH",
1836	.queuecommand			= mesh_queue,
1837	.eh_abort_handler		= mesh_abort,
1838	.eh_host_reset_handler		= mesh_host_reset,
1839	.can_queue			= 20,
1840	.this_id			= 7,
1841	.sg_tablesize			= SG_ALL,
1842	.cmd_per_lun			= 2,
1843	.max_segment_size		= 65535,
1844	.cmd_size			= sizeof(struct mesh_cmd_priv),
1845};
1846
1847static int mesh_probe(struct macio_dev *mdev, const struct of_device_id *match)
1848{
1849	struct device_node *mesh = macio_get_of_node(mdev);
1850	struct pci_dev* pdev = macio_get_pci_dev(mdev);
1851	int tgt, minper;
1852	const int *cfp;
1853	struct mesh_state *ms;
1854	struct Scsi_Host *mesh_host;
1855	void *dma_cmd_space;
1856	dma_addr_t dma_cmd_bus;
1857
1858	switch (mdev->bus->chip->type) {
1859	case macio_heathrow:
1860	case macio_gatwick:
1861	case macio_paddington:
1862		use_active_neg = 0;
1863		break;
1864	default:
1865		use_active_neg = SEQ_ACTIVE_NEG;
1866	}
1867
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));
1872		return -ENODEV;
1873	}
1874
1875	if (macio_request_resources(mdev, "mesh") != 0) {
1876       		printk(KERN_ERR "mesh: unable to request memory resources");
1877		return -EBUSY;
1878	}
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");
1882		goto out_release;
1883	}
1884	
 
 
 
 
 
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;
1890	ms->mdev = mdev;
1891	ms->pdev = pdev;
1892	
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");
1896		goto out_free;
1897	}		
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");
1901		iounmap(ms->mesh);
1902		goto out_free;
1903	}
1904
1905       	ms->meshintr = macio_irq(mdev, 0);
1906       	ms->dmaintr = macio_irq(mdev, 1);
1907
1908       	/* Space for dma command list: +1 for stop command,
1909       	 * +1 to allow for aligning.
1910	 */
1911	ms->dma_cmd_size = (mesh_host->sg_tablesize + 2) * sizeof(struct dbdma_cmd);
1912
1913	/* We use the PCI APIs for now until the generic one gets fixed
1914	 * enough or until we get some macio-specific versions
1915	 */
1916	dma_cmd_space = dma_alloc_coherent(&macio_get_pci_dev(mdev)->dev,
1917					   ms->dma_cmd_size, &dma_cmd_bus,
1918					   GFP_KERNEL);
1919	if (dma_cmd_space == NULL) {
1920		printk(KERN_ERR "mesh: can't allocate DMA table\n");
1921		goto out_unmap;
1922	}
1923
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;
1933       	}
1934
1935	if ((cfp = of_get_property(mesh, "clock-frequency", NULL)))
1936       		ms->clk_freq = *cfp;
1937	else {
1938       		printk(KERN_INFO "mesh: assuming 50MHz clock frequency\n");
1939	       	ms->clk_freq = 50000000;
1940       	}
1941
1942       	/* The maximum sync rate is clock / 5; increase
1943       	 * mesh_sync_period if necessary.
1944	 */
1945	minper = 1000000000 / (ms->clk_freq / 5); /* ns */
1946	if (mesh_sync_period < minper)
1947		mesh_sync_period = minper;
1948
1949	/* Power up the chip */
1950	set_mesh_power(ms, 1);
1951
1952	/* Set it up */
1953       	mesh_init(ms);
1954
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);
1958		goto out_shutdown;
1959	}
1960
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);
1965
1966	return 0;
1967
1968 out_release_irq:
1969	free_irq(ms->meshintr, ms);
1970 out_shutdown:
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
1973	 */
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);
1978 out_unmap:
1979	iounmap(ms->dma);
1980	iounmap(ms->mesh);
1981 out_free:
1982	scsi_host_put(mesh_host);
1983 out_release:
1984	macio_release_resources(mdev);
1985
1986	return -ENODEV;
1987}
1988
1989static int mesh_remove(struct macio_dev *mdev)
1990{
1991	struct mesh_state *ms = (struct mesh_state *)macio_get_drvdata(mdev);
1992	struct Scsi_Host *mesh_host = ms->host;
1993
1994	scsi_remove_host(mesh_host);
1995
1996	free_irq(ms->meshintr, ms);
1997
1998	/* Reset scsi bus */
1999	mesh_shutdown(mdev);
2000
2001	/* Shut down chip & termination */
2002	set_mesh_power(ms, 0);
2003
2004	/* Unmap registers & dma controller */
2005	iounmap(ms->mesh);
2006       	iounmap(ms->dma);
2007
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);
2011
2012	/* Release memory resources */
2013	macio_release_resources(mdev);
2014
2015	scsi_host_put(mesh_host);
2016
2017	return 0;
2018}
2019
2020
2021static struct of_device_id mesh_match[] = 
2022{
2023	{
2024	.name 		= "mesh",
2025	},
2026	{
2027	.type		= "scsi",
2028	.compatible	= "chrp,mesh0"
2029	},
2030	{},
2031};
2032MODULE_DEVICE_TABLE (of, mesh_match);
2033
2034static struct macio_driver mesh_driver = 
2035{
2036	.driver = {
2037		.name 		= "mesh",
2038		.owner		= THIS_MODULE,
2039		.of_match_table	= mesh_match,
2040	},
2041	.probe		= mesh_probe,
2042	.remove		= mesh_remove,
2043	.shutdown	= mesh_shutdown,
2044#ifdef CONFIG_PM
2045	.suspend	= mesh_suspend,
2046	.resume		= mesh_resume,
2047#endif
2048};
2049
2050
2051static int __init init_mesh(void)
2052{
2053
2054	/* Calculate sync rate from module parameters */
2055	if (sync_rate > 10)
2056		sync_rate = 10;
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;
2061	} else
2062		printk(KERN_INFO "mesh: configured for asynchronous\n");
2063
2064	return macio_register_driver(&mesh_driver);
2065}
2066
2067static void __exit exit_mesh(void)
2068{
2069	return macio_unregister_driver(&mesh_driver);
2070}
2071
2072module_init(init_mesh);
2073module_exit(exit_mesh);
v5.14.15
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   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)
   6 * controller.
   7 *
   8 * Paul Mackerras, August 1996.
   9 * Copyright (C) 1996 Paul Mackerras.
  10 *
  11 * Apr. 21 2002  - BenH		Rework bus reset code for new error handler
  12 *                              Add delay after initial bus reset
  13 *                              Add module parameters
  14 *
  15 * Sep. 27 2003  - BenH		Move to new driver model, fix some write posting
  16 *				issues
  17 * To do:
  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
  21 */
  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>
  36#include <asm/io.h>
  37#include <asm/prom.h>
  38#include <asm/irq.h>
  39#include <asm/hydra.h>
  40#include <asm/processor.h>
  41#include <asm/machdep.h>
  42#include <asm/pmac_feature.h>
  43#include <asm/macio.h>
  44
  45#include <scsi/scsi.h>
  46#include <scsi/scsi_cmnd.h>
  47#include <scsi/scsi_device.h>
  48#include <scsi/scsi_host.h>
  49
  50#include "mesh.h"
  51
  52#if 1
  53#undef KERN_DEBUG
  54#define KERN_DEBUG KERN_WARNING
  55#endif
  56
  57MODULE_AUTHOR("Paul Mackerras (paulus@samba.org)");
  58MODULE_DESCRIPTION("PowerMac MESH SCSI driver");
  59MODULE_LICENSE("GPL");
  60
  61static int sync_rate = CONFIG_SCSI_MESH_SYNC_RATE;
  62static int sync_targets = 0xff;
  63static int resel_targets = 0xff;
  64static int debug_targets = 0;	/* print debug for these targets */
  65static int init_reset_delay = CONFIG_SCSI_MESH_RESET_DELAY_MS;
  66
  67module_param(sync_rate, int, 0);
  68MODULE_PARM_DESC(sync_rate, "Synchronous rate (0..10, 0=async)");
  69module_param(sync_targets, int, 0);
  70MODULE_PARM_DESC(sync_targets, "Bitmask of targets allowed to set synchronous");
  71module_param(resel_targets, int, 0);
  72MODULE_PARM_DESC(resel_targets, "Bitmask of targets allowed to set disconnect");
  73module_param(debug_targets, int, 0644);
  74MODULE_PARM_DESC(debug_targets, "Bitmask of debugged targets");
  75module_param(init_reset_delay, int, 0);
  76MODULE_PARM_DESC(init_reset_delay, "Initial bus reset delay (0=no reset)");
  77
  78static int mesh_sync_period = 100;
  79static int mesh_sync_offset = 0;
  80static unsigned char use_active_neg = 0;  /* bit mask for SEQ_ACTIVE_NEG if used */
  81
  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))
  86
  87#undef MESH_DBG
  88#define N_DBG_LOG	50
  89#define N_DBG_SLOG	20
  90#define NUM_DBG_EVENTS	13
  91#undef	DBG_USE_TB		/* bombs on 601 */
  92
  93struct dbglog {
  94	char	*fmt;
  95	u32	tb;
  96	u8	phase;
  97	u8	bs0;
  98	u8	bs1;
  99	u8	tgt;
 100	int	d;
 101};
 102
 103enum mesh_phase {
 104	idle,
 105	arbitrating,
 106	selecting,
 107	commanding,
 108	dataing,
 109	statusing,
 110	busfreeing,
 111	disconnecting,
 112	reselecting,
 113	sleeping
 114};
 115
 116enum msg_phase {
 117	msg_none,
 118	msg_out,
 119	msg_out_xxx,
 120	msg_out_last,
 121	msg_in,
 122	msg_in_bad,
 123};
 124
 125enum sdtr_phase {
 126	do_sdtr,
 127	sdtr_sent,
 128	sdtr_done
 129};
 130
 131struct mesh_target {
 132	enum sdtr_phase sdtr_state;
 133	int	sync_params;
 134	int	data_goes_out;		/* guess as to data direction */
 135	struct scsi_cmnd *current_req;
 136	u32	saved_ptr;
 137#ifdef MESH_DBG
 138	int	log_ix;
 139	int	n_log;
 140	struct dbglog log[N_DBG_LOG];
 141#endif
 142};
 143
 144struct mesh_state {
 145	volatile struct	mesh_regs __iomem *mesh;
 146	int	meshintr;
 147	volatile struct	dbdma_regs __iomem *dma;
 148	int	dmaintr;
 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 */
 157	int	data_ptr;
 158	int	dma_started;
 159	int	dma_count;
 160	int	stat;
 161	int	aborting;
 162	int	expect_reply;
 163	int	n_msgin;
 164	u8	msgin[16];
 165	int	n_msgout;
 166	int	last_n_msgout;
 167	u8	msgout[16];
 168	struct dbdma_cmd *dma_cmds;	/* space for dbdma commands, aligned */
 169	dma_addr_t dma_cmd_bus;
 170	void	*dma_cmd_space;
 171	int	dma_cmd_size;
 172	int	clk_freq;
 173	struct mesh_target tgts[8];
 174	struct macio_dev *mdev;
 175	struct pci_dev* pdev;
 176#ifdef MESH_DBG
 177	int	log_ix;
 178	int	n_log;
 179	struct dbglog log[N_DBG_SLOG];
 180#endif
 181};
 182
 183/*
 184 * Driver is too messy, we need a few prototypes...
 185 */
 186static void mesh_done(struct mesh_state *ms, int start_next);
 187static void mesh_interrupt(struct mesh_state *ms);
 188static void cmd_complete(struct mesh_state *ms);
 189static void set_dma_cmds(struct mesh_state *ms, struct scsi_cmnd *cmd);
 190static void halt_dma(struct mesh_state *ms);
 191static void phase_mismatch(struct mesh_state *ms);
 192
 193
 194/*
 195 * Some debugging & logging routines
 196 */
 197
 198#ifdef MESH_DBG
 199
 200static inline u32 readtb(void)
 201{
 202	u32 tb;
 203
 204#ifdef DBG_USE_TB
 205	/* Beware: if you enable this, it will crash on 601s. */
 206	asm ("mftb %0" : "=r" (tb) : );
 207#else
 208	tb = 0;
 209#endif
 210	return tb;
 211}
 212
 213static void dlog(struct mesh_state *ms, char *fmt, int a)
 214{
 215	struct mesh_target *tp = &ms->tgts[ms->conn_tgt];
 216	struct dbglog *tlp, *slp;
 217
 218	tlp = &tp->log[tp->log_ix];
 219	slp = &ms->log[ms->log_ix];
 220	tlp->fmt = fmt;
 221	tlp->tb = readtb();
 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;
 226	tlp->d = a;
 227	*slp = *tlp;
 228	if (++tp->log_ix >= N_DBG_LOG)
 229		tp->log_ix = 0;
 230	if (tp->n_log < N_DBG_LOG)
 231		++tp->n_log;
 232	if (++ms->log_ix >= N_DBG_SLOG)
 233		ms->log_ix = 0;
 234	if (ms->n_log < N_DBG_SLOG)
 235		++ms->n_log;
 236}
 237
 238static void dumplog(struct mesh_state *ms, int t)
 239{
 240	struct mesh_target *tp = &ms->tgts[t];
 241	struct dbglog *lp;
 242	int i;
 243
 244	if (tp->n_log == 0)
 245		return;
 246	i = tp->log_ix - tp->n_log;
 247	if (i < 0)
 248		i += N_DBG_LOG;
 249	tp->n_log = 0;
 250	do {
 251		lp = &tp->log[i];
 252		printk(KERN_DEBUG "mesh log %d: bs=%.2x%.2x ph=%.2x ",
 253		       t, lp->bs1, lp->bs0, lp->phase);
 254#ifdef DBG_USE_TB
 255		printk("tb=%10u ", lp->tb);
 256#endif
 257		printk(lp->fmt, lp->d);
 258		printk("\n");
 259		if (++i >= N_DBG_LOG)
 260			i = 0;
 261	} while (i != tp->log_ix);
 262}
 263
 264static void dumpslog(struct mesh_state *ms)
 265{
 266	struct dbglog *lp;
 267	int i;
 268
 269	if (ms->n_log == 0)
 270		return;
 271	i = ms->log_ix - ms->n_log;
 272	if (i < 0)
 273		i += N_DBG_SLOG;
 274	ms->n_log = 0;
 275	do {
 276		lp = &ms->log[i];
 277		printk(KERN_DEBUG "mesh log: bs=%.2x%.2x ph=%.2x t%d ",
 278		       lp->bs1, lp->bs0, lp->phase, lp->tgt);
 279#ifdef DBG_USE_TB
 280		printk("tb=%10u ", lp->tb);
 281#endif
 282		printk(lp->fmt, lp->d);
 283		printk("\n");
 284		if (++i >= N_DBG_SLOG)
 285			i = 0;
 286	} while (i != ms->log_ix);
 287}
 288
 289#else
 290
 291static inline void dlog(struct mesh_state *ms, char *fmt, int a)
 292{}
 293static inline void dumplog(struct mesh_state *ms, int tgt)
 294{}
 295static inline void dumpslog(struct mesh_state *ms)
 296{}
 297
 298#endif /* MESH_DBG */
 299
 300#define MKWORD(a, b, c, d)	(((a) << 24) + ((b) << 16) + ((c) << 8) + (d))
 301
 302static void
 303mesh_dump_regs(struct mesh_state *ms)
 304{
 305	volatile struct mesh_regs __iomem *mr = ms->mesh;
 306	volatile struct dbdma_regs __iomem *md = ms->dma;
 307	int t;
 308	struct mesh_target *tp;
 309
 310	printk(KERN_DEBUG "mesh: state at %p, regs at %p, dma at %p\n",
 311	       ms, mr, md);
 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,
 317	       mr->sync_params);
 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) {
 327		tp = &ms->tgts[t];
 328		if (tp->current_req == NULL)
 329			continue;
 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);
 332	}
 333}
 334
 335
 336/*
 337 * Flush write buffers on the bus path to the mesh
 338 */
 339static inline void mesh_flush_io(volatile struct mesh_regs __iomem *mr)
 340{
 341	(void)in_8(&mr->mesh_id);
 342}
 343
 344
 345/*
 346 * Complete a SCSI command
 347 */
 348static void mesh_completed(struct mesh_state *ms, struct scsi_cmnd *cmd)
 349{
 350	(*cmd->scsi_done)(cmd);
 351}
 352
 353
 354/* Called with  meshinterrupt disabled, initialize the chipset
 355 * and eventually do the initial bus reset. The lock must not be
 356 * held since we can schedule.
 357 */
 358static void mesh_init(struct mesh_state *ms)
 359{
 360	volatile struct mesh_regs __iomem *mr = ms->mesh;
 361	volatile struct dbdma_regs __iomem *md = ms->dma;
 362
 363	mesh_flush_io(mr);
 364	udelay(100);
 365
 366	/* Reset controller */
 367	out_le32(&md->control, (RUN|PAUSE|FLUSH|WAKE) << 16);	/* stop dma */
 368	out_8(&mr->exception, 0xff);	/* clear all exception bits */
 369	out_8(&mr->error, 0xff);	/* clear all error bits */
 370	out_8(&mr->sequence, SEQ_RESETMESH);
 371	mesh_flush_io(mr);
 372	udelay(10);
 373	out_8(&mr->intr_mask, INT_ERROR | INT_EXCEPTION | INT_CMDDONE);
 374	out_8(&mr->source_id, ms->host->this_id);
 375	out_8(&mr->sel_timeout, 25);	/* 250ms */
 376	out_8(&mr->sync_params, ASYNC_PARAMS);
 377
 378	if (init_reset_delay) {
 379		printk(KERN_INFO "mesh: performing initial bus reset...\n");
 380		
 381		/* Reset bus */
 382		out_8(&mr->bus_status1, BS1_RST);	/* assert RST */
 383		mesh_flush_io(mr);
 384		udelay(30);			/* leave it on for >= 25us */
 385		out_8(&mr->bus_status1, 0);	/* negate RST */
 386		mesh_flush_io(mr);
 387
 388		/* Wait for bus to come back */
 389		msleep(init_reset_delay);
 390	}
 391	
 392	/* Reconfigure controller */
 393	out_8(&mr->interrupt, 0xff);	/* clear all interrupt bits */
 394	out_8(&mr->sequence, SEQ_FLUSHFIFO);
 395	mesh_flush_io(mr);
 396	udelay(1);
 397	out_8(&mr->sync_params, ASYNC_PARAMS);
 398	out_8(&mr->sequence, SEQ_ENBRESEL);
 399
 400	ms->phase = idle;
 401	ms->msgphase = msg_none;
 402}
 403
 404
 405static void mesh_start_cmd(struct mesh_state *ms, struct scsi_cmnd *cmd)
 406{
 407	volatile struct mesh_regs __iomem *mr = ms->mesh;
 408	int t, id;
 409
 410	id = cmd->device->id;
 411	ms->current_req = cmd;
 412	ms->tgts[id].data_goes_out = cmd->sc_data_direction == DMA_TO_DEVICE;
 413	ms->tgts[id].current_req = cmd;
 414
 415#if 1
 416	if (DEBUG_TARGET(cmd)) {
 417		int i;
 418		printk(KERN_DEBUG "mesh_start: %p tgt=%d cmd=", cmd, id);
 419		for (i = 0; i < cmd->cmd_len; ++i)
 420			printk(" %x", cmd->cmnd[i]);
 421		printk(" use_sg=%d buffer=%p bufflen=%u\n",
 422		       scsi_sg_count(cmd), scsi_sglist(cmd), scsi_bufflen(cmd));
 423	}
 424#endif
 425	if (ms->dma_started)
 426		panic("mesh: double DMA start !\n");
 427
 428	ms->phase = arbitrating;
 429	ms->msgphase = msg_none;
 430	ms->data_ptr = 0;
 431	ms->dma_started = 0;
 432	ms->n_msgout = 0;
 433	ms->last_n_msgout = 0;
 434	ms->expect_reply = 0;
 435	ms->conn_tgt = id;
 436	ms->tgts[id].saved_ptr = 0;
 437	ms->stat = DID_OK;
 438	ms->aborting = 0;
 439#ifdef MESH_DBG
 440	ms->tgts[id].n_log = 0;
 441	dlog(ms, "start cmd=%x", (int) cmd);
 442#endif
 443
 444	/* Off we go */
 445	dlog(ms, "about to arb, intr/exc/err/fc=%.8x",
 446	     MKWORD(mr->interrupt, mr->exception, mr->error, mr->fifo_count));
 447	out_8(&mr->interrupt, INT_CMDDONE);
 448	out_8(&mr->sequence, SEQ_ENBRESEL);
 449	mesh_flush_io(mr);
 450	udelay(1);
 451
 452	if (in_8(&mr->bus_status1) & (BS1_BSY | BS1_SEL)) {
 453		/*
 454		 * Some other device has the bus or is arbitrating for it -
 455		 * probably a target which is about to reselect us.
 456		 */
 457		dlog(ms, "busy b4 arb, intr/exc/err/fc=%.8x",
 458		     MKWORD(mr->interrupt, mr->exception,
 459			    mr->error, mr->fifo_count));
 460		for (t = 100; t > 0; --t) {
 461			if ((in_8(&mr->bus_status1) & (BS1_BSY | BS1_SEL)) == 0)
 462				break;
 463			if (in_8(&mr->interrupt) != 0) {
 464				dlog(ms, "intr b4 arb, intr/exc/err/fc=%.8x",
 465				     MKWORD(mr->interrupt, mr->exception,
 466					    mr->error, mr->fifo_count));
 467				mesh_interrupt(ms);
 468				if (ms->phase != arbitrating)
 469					return;
 470			}
 471			udelay(1);
 472		}
 473		if (in_8(&mr->bus_status1) & (BS1_BSY | BS1_SEL)) {
 474			/* XXX should try again in a little while */
 475			ms->stat = DID_BUS_BUSY;
 476			ms->phase = idle;
 477			mesh_done(ms, 0);
 478			return;
 479		}
 480	}
 481
 482	/*
 483	 * Apparently the mesh has a bug where it will assert both its
 484	 * own bit and the target's bit on the bus during arbitration.
 485	 */
 486	out_8(&mr->dest_id, mr->source_id);
 487
 488	/*
 489	 * There appears to be a race with reselection sometimes,
 490	 * where a target reselects us just as we issue the
 491	 * arbitrate command.  It seems that then the arbitrate
 492	 * command just hangs waiting for the bus to be free
 493	 * without giving us a reselection exception.
 494	 * The only way I have found to get it to respond correctly
 495	 * is this: disable reselection before issuing the arbitrate
 496	 * command, then after issuing it, if it looks like a target
 497	 * is trying to reselect us, reset the mesh and then enable
 498	 * reselection.
 499	 */
 500	out_8(&mr->sequence, SEQ_DISRESEL);
 501	if (in_8(&mr->interrupt) != 0) {
 502		dlog(ms, "intr after disresel, intr/exc/err/fc=%.8x",
 503		     MKWORD(mr->interrupt, mr->exception,
 504			    mr->error, mr->fifo_count));
 505		mesh_interrupt(ms);
 506		if (ms->phase != arbitrating)
 507			return;
 508		dlog(ms, "after intr after disresel, intr/exc/err/fc=%.8x",
 509		     MKWORD(mr->interrupt, mr->exception,
 510			    mr->error, mr->fifo_count));
 511	}
 512
 513	out_8(&mr->sequence, SEQ_ARBITRATE);
 514
 515	for (t = 230; t > 0; --t) {
 516		if (in_8(&mr->interrupt) != 0)
 517			break;
 518		udelay(1);
 519	}
 520	dlog(ms, "after arb, intr/exc/err/fc=%.8x",
 521	     MKWORD(mr->interrupt, mr->exception, mr->error, mr->fifo_count));
 522	if (in_8(&mr->interrupt) == 0 && (in_8(&mr->bus_status1) & BS1_SEL)
 523	    && (in_8(&mr->bus_status0) & BS0_IO)) {
 524		/* looks like a reselection - try resetting the mesh */
 525		dlog(ms, "resel? after arb, intr/exc/err/fc=%.8x",
 526		     MKWORD(mr->interrupt, mr->exception, mr->error, mr->fifo_count));
 527		out_8(&mr->sequence, SEQ_RESETMESH);
 528		mesh_flush_io(mr);
 529		udelay(10);
 530		out_8(&mr->interrupt, INT_ERROR | INT_EXCEPTION | INT_CMDDONE);
 531		out_8(&mr->intr_mask, INT_ERROR | INT_EXCEPTION | INT_CMDDONE);
 532		out_8(&mr->sequence, SEQ_ENBRESEL);
 533		mesh_flush_io(mr);
 534		for (t = 10; t > 0 && in_8(&mr->interrupt) == 0; --t)
 535			udelay(1);
 536		dlog(ms, "tried reset after arb, intr/exc/err/fc=%.8x",
 537		     MKWORD(mr->interrupt, mr->exception, mr->error, mr->fifo_count));
 538#ifndef MESH_MULTIPLE_HOSTS
 539		if (in_8(&mr->interrupt) == 0 && (in_8(&mr->bus_status1) & BS1_SEL)
 540		    && (in_8(&mr->bus_status0) & BS0_IO)) {
 541			printk(KERN_ERR "mesh: controller not responding"
 542			       " to reselection!\n");
 543			/*
 544			 * If this is a target reselecting us, and the
 545			 * mesh isn't responding, the higher levels of
 546			 * the scsi code will eventually time out and
 547			 * reset the bus.
 548			 */
 549		}
 550#endif
 551	}
 552}
 553
 554/*
 555 * Start the next command for a MESH.
 556 * Should be called with interrupts disabled.
 557 */
 558static void mesh_start(struct mesh_state *ms)
 559{
 560	struct scsi_cmnd *cmd, *prev, *next;
 561
 562	if (ms->phase != idle || ms->current_req != NULL) {
 563		printk(KERN_ERR "inappropriate mesh_start (phase=%d, ms=%p)",
 564		       ms->phase, ms);
 565		return;
 566	}
 567
 568	while (ms->phase == idle) {
 569		prev = NULL;
 570		for (cmd = ms->request_q; ; cmd = (struct scsi_cmnd *) cmd->host_scribble) {
 571			if (cmd == NULL)
 572				return;
 573			if (ms->tgts[cmd->device->id].current_req == NULL)
 574				break;
 575			prev = cmd;
 576		}
 577		next = (struct scsi_cmnd *) cmd->host_scribble;
 578		if (prev == NULL)
 579			ms->request_q = next;
 580		else
 581			prev->host_scribble = (void *) next;
 582		if (next == NULL)
 583			ms->request_qtail = prev;
 584
 585		mesh_start_cmd(ms, cmd);
 586	}
 587}
 588
 589static void mesh_done(struct mesh_state *ms, int start_next)
 590{
 591	struct scsi_cmnd *cmd;
 592	struct mesh_target *tp = &ms->tgts[ms->conn_tgt];
 593
 594	cmd = ms->current_req;
 595	ms->current_req = NULL;
 596	tp->current_req = NULL;
 597	if (cmd) {
 
 
 598		set_host_byte(cmd, ms->stat);
 599		set_status_byte(cmd, cmd->SCp.Status);
 600		if (ms->stat == DID_OK)
 601			scsi_msg_to_host_byte(cmd, cmd->SCp.Message);
 602		if (DEBUG_TARGET(cmd)) {
 603			printk(KERN_DEBUG "mesh_done: result = %x, data_ptr=%d, buflen=%d\n",
 604			       cmd->result, ms->data_ptr, scsi_bufflen(cmd));
 605#if 0
 606			/* needs to use sg? */
 607			if ((cmd->cmnd[0] == 0 || cmd->cmnd[0] == 0x12 || cmd->cmnd[0] == 3)
 608			    && cmd->request_buffer != 0) {
 609				unsigned char *b = cmd->request_buffer;
 610				printk(KERN_DEBUG "buffer = %x %x %x %x %x %x %x %x\n",
 611				       b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7]);
 612			}
 613#endif
 614		}
 615		cmd->SCp.this_residual -= ms->data_ptr;
 616		mesh_completed(ms, cmd);
 617	}
 618	if (start_next) {
 619		out_8(&ms->mesh->sequence, SEQ_ENBRESEL);
 620		mesh_flush_io(ms->mesh);
 621		udelay(1);
 622		ms->phase = idle;
 623		mesh_start(ms);
 624	}
 625}
 626
 627static inline void add_sdtr_msg(struct mesh_state *ms)
 628{
 629	int i = ms->n_msgout;
 630
 631	ms->msgout[i] = EXTENDED_MESSAGE;
 632	ms->msgout[i+1] = 3;
 633	ms->msgout[i+2] = EXTENDED_SDTR;
 634	ms->msgout[i+3] = mesh_sync_period/4;
 635	ms->msgout[i+4] = (ALLOW_SYNC(ms->conn_tgt)? mesh_sync_offset: 0);
 636	ms->n_msgout = i + 5;
 637}
 638
 639static void set_sdtr(struct mesh_state *ms, int period, int offset)
 640{
 641	struct mesh_target *tp = &ms->tgts[ms->conn_tgt];
 642	volatile struct mesh_regs __iomem *mr = ms->mesh;
 643	int v, tr;
 644
 645	tp->sdtr_state = sdtr_done;
 646	if (offset == 0) {
 647		/* asynchronous */
 648		if (SYNC_OFF(tp->sync_params))
 649			printk(KERN_INFO "mesh: target %d now asynchronous\n",
 650			       ms->conn_tgt);
 651		tp->sync_params = ASYNC_PARAMS;
 652		out_8(&mr->sync_params, ASYNC_PARAMS);
 653		return;
 654	}
 655	/*
 656	 * We need to compute ceil(clk_freq * period / 500e6) - 2
 657	 * without incurring overflow.
 658	 */
 659	v = (ms->clk_freq / 5000) * period;
 660	if (v <= 250000) {
 661		/* special case: sync_period == 5 * clk_period */
 662		v = 0;
 663		/* units of tr are 100kB/s */
 664		tr = (ms->clk_freq + 250000) / 500000;
 665	} else {
 666		/* sync_period == (v + 2) * 2 * clk_period */
 667		v = (v + 99999) / 100000 - 2;
 668		if (v > 15)
 669			v = 15;	/* oops */
 670		tr = ((ms->clk_freq / (v + 2)) + 199999) / 200000;
 671	}
 672	if (offset > 15)
 673		offset = 15;	/* can't happen */
 674	tp->sync_params = SYNC_PARAMS(offset, v);
 675	out_8(&mr->sync_params, tp->sync_params);
 676	printk(KERN_INFO "mesh: target %d synchronous at %d.%d MB/s\n",
 677	       ms->conn_tgt, tr/10, tr%10);
 678}
 679
 680static void start_phase(struct mesh_state *ms)
 681{
 682	int i, seq, nb;
 683	volatile struct mesh_regs __iomem *mr = ms->mesh;
 684	volatile struct dbdma_regs __iomem *md = ms->dma;
 685	struct scsi_cmnd *cmd = ms->current_req;
 686	struct mesh_target *tp = &ms->tgts[ms->conn_tgt];
 687
 688	dlog(ms, "start_phase nmo/exc/fc/seq = %.8x",
 689	     MKWORD(ms->n_msgout, mr->exception, mr->fifo_count, mr->sequence));
 690	out_8(&mr->interrupt, INT_ERROR | INT_EXCEPTION | INT_CMDDONE);
 691	seq = use_active_neg + (ms->n_msgout? SEQ_ATN: 0);
 692	switch (ms->msgphase) {
 693	case msg_none:
 694		break;
 695
 696	case msg_in:
 697		out_8(&mr->count_hi, 0);
 698		out_8(&mr->count_lo, 1);
 699		out_8(&mr->sequence, SEQ_MSGIN + seq);
 700		ms->n_msgin = 0;
 701		return;
 702
 703	case msg_out:
 704		/*
 705		 * To make sure ATN drops before we assert ACK for
 706		 * the last byte of the message, we have to do the
 707		 * last byte specially.
 708		 */
 709		if (ms->n_msgout <= 0) {
 710			printk(KERN_ERR "mesh: msg_out but n_msgout=%d\n",
 711			       ms->n_msgout);
 712			mesh_dump_regs(ms);
 713			ms->msgphase = msg_none;
 714			break;
 715		}
 716		if (ALLOW_DEBUG(ms->conn_tgt)) {
 717			printk(KERN_DEBUG "mesh: sending %d msg bytes:",
 718			       ms->n_msgout);
 719			for (i = 0; i < ms->n_msgout; ++i)
 720				printk(" %x", ms->msgout[i]);
 721			printk("\n");
 722		}
 723		dlog(ms, "msgout msg=%.8x", MKWORD(ms->n_msgout, ms->msgout[0],
 724						ms->msgout[1], ms->msgout[2]));
 725		out_8(&mr->count_hi, 0);
 726		out_8(&mr->sequence, SEQ_FLUSHFIFO);
 727		mesh_flush_io(mr);
 728		udelay(1);
 729		/*
 730		 * If ATN is not already asserted, we assert it, then
 731		 * issue a SEQ_MSGOUT to get the mesh to drop ACK.
 732		 */
 733		if ((in_8(&mr->bus_status0) & BS0_ATN) == 0) {
 734			dlog(ms, "bus0 was %.2x explicitly asserting ATN", mr->bus_status0);
 735			out_8(&mr->bus_status0, BS0_ATN); /* explicit ATN */
 736			mesh_flush_io(mr);
 737			udelay(1);
 738			out_8(&mr->count_lo, 1);
 739			out_8(&mr->sequence, SEQ_MSGOUT + seq);
 740			out_8(&mr->bus_status0, 0); /* release explicit ATN */
 741			dlog(ms,"hace: after explicit ATN bus0=%.2x",mr->bus_status0);
 742		}
 743		if (ms->n_msgout == 1) {
 744			/*
 745			 * We can't issue the SEQ_MSGOUT without ATN
 746			 * until the target has asserted REQ.  The logic
 747			 * in cmd_complete handles both situations:
 748			 * REQ already asserted or not.
 749			 */
 750			cmd_complete(ms);
 751		} else {
 752			out_8(&mr->count_lo, ms->n_msgout - 1);
 753			out_8(&mr->sequence, SEQ_MSGOUT + seq);
 754			for (i = 0; i < ms->n_msgout - 1; ++i)
 755				out_8(&mr->fifo, ms->msgout[i]);
 756		}
 757		return;
 758
 759	default:
 760		printk(KERN_ERR "mesh bug: start_phase msgphase=%d\n",
 761		       ms->msgphase);
 762	}
 763
 764	switch (ms->phase) {
 765	case selecting:
 766		out_8(&mr->dest_id, ms->conn_tgt);
 767		out_8(&mr->sequence, SEQ_SELECT + SEQ_ATN);
 768		break;
 769	case commanding:
 770		out_8(&mr->sync_params, tp->sync_params);
 771		out_8(&mr->count_hi, 0);
 772		if (cmd) {
 773			out_8(&mr->count_lo, cmd->cmd_len);
 774			out_8(&mr->sequence, SEQ_COMMAND + seq);
 775			for (i = 0; i < cmd->cmd_len; ++i)
 776				out_8(&mr->fifo, cmd->cmnd[i]);
 777		} else {
 778			out_8(&mr->count_lo, 6);
 779			out_8(&mr->sequence, SEQ_COMMAND + seq);
 780			for (i = 0; i < 6; ++i)
 781				out_8(&mr->fifo, 0);
 782		}
 783		break;
 784	case dataing:
 785		/* transfer data, if any */
 786		if (!ms->dma_started) {
 787			set_dma_cmds(ms, cmd);
 788			out_le32(&md->cmdptr, virt_to_phys(ms->dma_cmds));
 789			out_le32(&md->control, (RUN << 16) | RUN);
 790			ms->dma_started = 1;
 791		}
 792		nb = ms->dma_count;
 793		if (nb > 0xfff0)
 794			nb = 0xfff0;
 795		ms->dma_count -= nb;
 796		ms->data_ptr += nb;
 797		out_8(&mr->count_lo, nb);
 798		out_8(&mr->count_hi, nb >> 8);
 799		out_8(&mr->sequence, (tp->data_goes_out?
 800				SEQ_DATAOUT: SEQ_DATAIN) + SEQ_DMA_MODE + seq);
 801		break;
 802	case statusing:
 803		out_8(&mr->count_hi, 0);
 804		out_8(&mr->count_lo, 1);
 805		out_8(&mr->sequence, SEQ_STATUS + seq);
 806		break;
 807	case busfreeing:
 808	case disconnecting:
 809		out_8(&mr->sequence, SEQ_ENBRESEL);
 810		mesh_flush_io(mr);
 811		udelay(1);
 812		dlog(ms, "enbresel intr/exc/err/fc=%.8x",
 813		     MKWORD(mr->interrupt, mr->exception, mr->error,
 814			    mr->fifo_count));
 815		out_8(&mr->sequence, SEQ_BUSFREE);
 816		break;
 817	default:
 818		printk(KERN_ERR "mesh: start_phase called with phase=%d\n",
 819		       ms->phase);
 820		dumpslog(ms);
 821	}
 822
 823}
 824
 825static inline void get_msgin(struct mesh_state *ms)
 826{
 827	volatile struct mesh_regs __iomem *mr = ms->mesh;
 828	int i, n;
 829
 830	n = mr->fifo_count;
 831	if (n != 0) {
 832		i = ms->n_msgin;
 833		ms->n_msgin = i + n;
 834		for (; n > 0; --n)
 835			ms->msgin[i++] = in_8(&mr->fifo);
 836	}
 837}
 838
 839static inline int msgin_length(struct mesh_state *ms)
 840{
 841	int b, n;
 842
 843	n = 1;
 844	if (ms->n_msgin > 0) {
 845		b = ms->msgin[0];
 846		if (b == 1) {
 847			/* extended message */
 848			n = ms->n_msgin < 2? 2: ms->msgin[1] + 2;
 849		} else if (0x20 <= b && b <= 0x2f) {
 850			/* 2-byte message */
 851			n = 2;
 852		}
 853	}
 854	return n;
 855}
 856
 857static void reselected(struct mesh_state *ms)
 858{
 859	volatile struct mesh_regs __iomem *mr = ms->mesh;
 860	struct scsi_cmnd *cmd;
 861	struct mesh_target *tp;
 862	int b, t, prev;
 863
 864	switch (ms->phase) {
 865	case idle:
 866		break;
 867	case arbitrating:
 868		if ((cmd = ms->current_req) != NULL) {
 869			/* put the command back on the queue */
 870			cmd->host_scribble = (void *) ms->request_q;
 871			if (ms->request_q == NULL)
 872				ms->request_qtail = cmd;
 873			ms->request_q = cmd;
 874			tp = &ms->tgts[cmd->device->id];
 875			tp->current_req = NULL;
 876		}
 877		break;
 878	case busfreeing:
 879		ms->phase = reselecting;
 880		mesh_done(ms, 0);
 881		break;
 882	case disconnecting:
 883		break;
 884	default:
 885		printk(KERN_ERR "mesh: reselected in phase %d/%d tgt %d\n",
 886		       ms->msgphase, ms->phase, ms->conn_tgt);
 887		dumplog(ms, ms->conn_tgt);
 888		dumpslog(ms);
 889	}
 890
 891	if (ms->dma_started) {
 892		printk(KERN_ERR "mesh: reselected with DMA started !\n");
 893		halt_dma(ms);
 894	}
 895	ms->current_req = NULL;
 896	ms->phase = dataing;
 897	ms->msgphase = msg_in;
 898	ms->n_msgout = 0;
 899	ms->last_n_msgout = 0;
 900	prev = ms->conn_tgt;
 901
 902	/*
 903	 * We seem to get abortive reselections sometimes.
 904	 */
 905	while ((in_8(&mr->bus_status1) & BS1_BSY) == 0) {
 906		static int mesh_aborted_resels;
 907		mesh_aborted_resels++;
 908		out_8(&mr->interrupt, INT_ERROR | INT_EXCEPTION | INT_CMDDONE);
 909		mesh_flush_io(mr);
 910		udelay(1);
 911		out_8(&mr->sequence, SEQ_ENBRESEL);
 912		mesh_flush_io(mr);
 913		udelay(5);
 914		dlog(ms, "extra resel err/exc/fc = %.6x",
 915		     MKWORD(0, mr->error, mr->exception, mr->fifo_count));
 916	}
 917	out_8(&mr->interrupt, INT_ERROR | INT_EXCEPTION | INT_CMDDONE);
 918       	mesh_flush_io(mr);
 919	udelay(1);
 920	out_8(&mr->sequence, SEQ_ENBRESEL);
 921       	mesh_flush_io(mr);
 922	udelay(1);
 923	out_8(&mr->sync_params, ASYNC_PARAMS);
 924
 925	/*
 926	 * Find out who reselected us.
 927	 */
 928	if (in_8(&mr->fifo_count) == 0) {
 929		printk(KERN_ERR "mesh: reselection but nothing in fifo?\n");
 930		ms->conn_tgt = ms->host->this_id;
 931		goto bogus;
 932	}
 933	/* get the last byte in the fifo */
 934	do {
 935		b = in_8(&mr->fifo);
 936		dlog(ms, "reseldata %x", b);
 937	} while (in_8(&mr->fifo_count));
 938	for (t = 0; t < 8; ++t)
 939		if ((b & (1 << t)) != 0 && t != ms->host->this_id)
 940			break;
 941	if (b != (1 << t) + (1 << ms->host->this_id)) {
 942		printk(KERN_ERR "mesh: bad reselection data %x\n", b);
 943		ms->conn_tgt = ms->host->this_id;
 944		goto bogus;
 945	}
 946
 947
 948	/*
 949	 * Set up to continue with that target's transfer.
 950	 */
 951	ms->conn_tgt = t;
 952	tp = &ms->tgts[t];
 953	out_8(&mr->sync_params, tp->sync_params);
 954	if (ALLOW_DEBUG(t)) {
 955		printk(KERN_DEBUG "mesh: reselected by target %d\n", t);
 956		printk(KERN_DEBUG "mesh: saved_ptr=%x goes_out=%d cmd=%p\n",
 957		       tp->saved_ptr, tp->data_goes_out, tp->current_req);
 958	}
 959	ms->current_req = tp->current_req;
 960	if (tp->current_req == NULL) {
 961		printk(KERN_ERR "mesh: reselected by tgt %d but no cmd!\n", t);
 962		goto bogus;
 963	}
 964	ms->data_ptr = tp->saved_ptr;
 965	dlog(ms, "resel prev tgt=%d", prev);
 966	dlog(ms, "resel err/exc=%.4x", MKWORD(0, 0, mr->error, mr->exception));
 967	start_phase(ms);
 968	return;
 969
 970bogus:
 971	dumplog(ms, ms->conn_tgt);
 972	dumpslog(ms);
 973	ms->data_ptr = 0;
 974	ms->aborting = 1;
 975	start_phase(ms);
 976}
 977
 978static void do_abort(struct mesh_state *ms)
 979{
 980	ms->msgout[0] = ABORT;
 981	ms->n_msgout = 1;
 982	ms->aborting = 1;
 983	ms->stat = DID_ABORT;
 984	dlog(ms, "abort", 0);
 985}
 986
 987static void handle_reset(struct mesh_state *ms)
 988{
 989	int tgt;
 990	struct mesh_target *tp;
 991	struct scsi_cmnd *cmd;
 992	volatile struct mesh_regs __iomem *mr = ms->mesh;
 993
 994	for (tgt = 0; tgt < 8; ++tgt) {
 995		tp = &ms->tgts[tgt];
 996		if ((cmd = tp->current_req) != NULL) {
 997			set_host_byte(cmd, DID_RESET);
 998			tp->current_req = NULL;
 999			mesh_completed(ms, cmd);
1000		}
1001		ms->tgts[tgt].sdtr_state = do_sdtr;
1002		ms->tgts[tgt].sync_params = ASYNC_PARAMS;
1003	}
1004	ms->current_req = NULL;
1005	while ((cmd = ms->request_q) != NULL) {
1006		ms->request_q = (struct scsi_cmnd *) cmd->host_scribble;
1007		set_host_byte(cmd, DID_RESET);
1008		mesh_completed(ms, cmd);
1009	}
1010	ms->phase = idle;
1011	ms->msgphase = msg_none;
1012	out_8(&mr->interrupt, INT_ERROR | INT_EXCEPTION | INT_CMDDONE);
1013	out_8(&mr->sequence, SEQ_FLUSHFIFO);
1014       	mesh_flush_io(mr);
1015	udelay(1);
1016	out_8(&mr->sync_params, ASYNC_PARAMS);
1017	out_8(&mr->sequence, SEQ_ENBRESEL);
1018}
1019
1020static irqreturn_t do_mesh_interrupt(int irq, void *dev_id)
1021{
1022	unsigned long flags;
1023	struct mesh_state *ms = dev_id;
1024	struct Scsi_Host *dev = ms->host;
1025	
1026	spin_lock_irqsave(dev->host_lock, flags);
1027	mesh_interrupt(ms);
1028	spin_unlock_irqrestore(dev->host_lock, flags);
1029	return IRQ_HANDLED;
1030}
1031
1032static void handle_error(struct mesh_state *ms)
1033{
1034	int err, exc, count;
1035	volatile struct mesh_regs __iomem *mr = ms->mesh;
1036
1037	err = in_8(&mr->error);
1038	exc = in_8(&mr->exception);
1039	out_8(&mr->interrupt, INT_ERROR | INT_EXCEPTION | INT_CMDDONE);
1040	dlog(ms, "error err/exc/fc/cl=%.8x",
1041	     MKWORD(err, exc, mr->fifo_count, mr->count_lo));
1042	if (err & ERR_SCSIRESET) {
1043		/* SCSI bus was reset */
1044		printk(KERN_INFO "mesh: SCSI bus reset detected: "
1045		       "waiting for end...");
1046		while ((in_8(&mr->bus_status1) & BS1_RST) != 0)
1047			udelay(1);
1048		printk("done\n");
1049		if (ms->dma_started)
1050			halt_dma(ms);
1051		handle_reset(ms);
1052		/* request_q is empty, no point in mesh_start() */
1053		return;
1054	}
1055	if (err & ERR_UNEXPDISC) {
1056		/* Unexpected disconnect */
1057		if (exc & EXC_RESELECTED) {
1058			reselected(ms);
1059			return;
1060		}
1061		if (!ms->aborting) {
1062			printk(KERN_WARNING "mesh: target %d aborted\n",
1063			       ms->conn_tgt);
1064			dumplog(ms, ms->conn_tgt);
1065			dumpslog(ms);
1066		}
1067		out_8(&mr->interrupt, INT_CMDDONE);
1068		ms->stat = DID_ABORT;
1069		mesh_done(ms, 1);
1070		return;
1071	}
1072	if (err & ERR_PARITY) {
1073		if (ms->msgphase == msg_in) {
1074			printk(KERN_ERR "mesh: msg parity error, target %d\n",
1075			       ms->conn_tgt);
1076			ms->msgout[0] = MSG_PARITY_ERROR;
1077			ms->n_msgout = 1;
1078			ms->msgphase = msg_in_bad;
1079			cmd_complete(ms);
1080			return;
1081		}
1082		if (ms->stat == DID_OK) {
1083			printk(KERN_ERR "mesh: parity error, target %d\n",
1084			       ms->conn_tgt);
1085			ms->stat = DID_PARITY;
1086		}
1087		count = (mr->count_hi << 8) + mr->count_lo;
1088		if (count == 0) {
1089			cmd_complete(ms);
1090		} else {
1091			/* reissue the data transfer command */
1092			out_8(&mr->sequence, mr->sequence);
1093		}
1094		return;
1095	}
1096	if (err & ERR_SEQERR) {
1097		if (exc & EXC_RESELECTED) {
1098			/* This can happen if we issue a command to
1099			   get the bus just after the target reselects us. */
1100			static int mesh_resel_seqerr;
1101			mesh_resel_seqerr++;
1102			reselected(ms);
1103			return;
1104		}
1105		if (exc == EXC_PHASEMM) {
1106			static int mesh_phasemm_seqerr;
1107			mesh_phasemm_seqerr++;
1108			phase_mismatch(ms);
1109			return;
1110		}
1111		printk(KERN_ERR "mesh: sequence error (err=%x exc=%x)\n",
1112		       err, exc);
1113	} else {
1114		printk(KERN_ERR "mesh: unknown error %x (exc=%x)\n", err, exc);
1115	}
1116	mesh_dump_regs(ms);
1117	dumplog(ms, ms->conn_tgt);
1118	if (ms->phase > selecting && (in_8(&mr->bus_status1) & BS1_BSY)) {
1119		/* try to do what the target wants */
1120		do_abort(ms);
1121		phase_mismatch(ms);
1122		return;
1123	}
1124	ms->stat = DID_ERROR;
1125	mesh_done(ms, 1);
1126}
1127
1128static void handle_exception(struct mesh_state *ms)
1129{
1130	int exc;
1131	volatile struct mesh_regs __iomem *mr = ms->mesh;
1132
1133	exc = in_8(&mr->exception);
1134	out_8(&mr->interrupt, INT_EXCEPTION | INT_CMDDONE);
1135	if (exc & EXC_RESELECTED) {
1136		static int mesh_resel_exc;
1137		mesh_resel_exc++;
1138		reselected(ms);
1139	} else if (exc == EXC_ARBLOST) {
1140		printk(KERN_DEBUG "mesh: lost arbitration\n");
1141		ms->stat = DID_BUS_BUSY;
1142		mesh_done(ms, 1);
1143	} else if (exc == EXC_SELTO) {
1144		/* selection timed out */
1145		ms->stat = DID_BAD_TARGET;
1146		mesh_done(ms, 1);
1147	} else if (exc == EXC_PHASEMM) {
1148		/* target wants to do something different:
1149		   find out what it wants and do it. */
1150		phase_mismatch(ms);
1151	} else {
1152		printk(KERN_ERR "mesh: can't cope with exception %x\n", exc);
1153		mesh_dump_regs(ms);
1154		dumplog(ms, ms->conn_tgt);
1155		do_abort(ms);
1156		phase_mismatch(ms);
1157	}
1158}
1159
1160static void handle_msgin(struct mesh_state *ms)
1161{
1162	int i, code;
1163	struct scsi_cmnd *cmd = ms->current_req;
1164	struct mesh_target *tp = &ms->tgts[ms->conn_tgt];
1165
1166	if (ms->n_msgin == 0)
1167		return;
1168	code = ms->msgin[0];
1169	if (ALLOW_DEBUG(ms->conn_tgt)) {
1170		printk(KERN_DEBUG "got %d message bytes:", ms->n_msgin);
1171		for (i = 0; i < ms->n_msgin; ++i)
1172			printk(" %x", ms->msgin[i]);
1173		printk("\n");
1174	}
1175	dlog(ms, "msgin msg=%.8x",
1176	     MKWORD(ms->n_msgin, code, ms->msgin[1], ms->msgin[2]));
1177
1178	ms->expect_reply = 0;
1179	ms->n_msgout = 0;
1180	if (ms->n_msgin < msgin_length(ms))
1181		goto reject;
1182	if (cmd)
1183		cmd->SCp.Message = code;
1184	switch (code) {
1185	case COMMAND_COMPLETE:
1186		break;
1187	case EXTENDED_MESSAGE:
1188		switch (ms->msgin[2]) {
1189		case EXTENDED_MODIFY_DATA_POINTER:
1190			ms->data_ptr += (ms->msgin[3] << 24) + ms->msgin[6]
1191				+ (ms->msgin[4] << 16) + (ms->msgin[5] << 8);
1192			break;
1193		case EXTENDED_SDTR:
1194			if (tp->sdtr_state != sdtr_sent) {
1195				/* reply with an SDTR */
1196				add_sdtr_msg(ms);
1197				/* limit period to at least his value,
1198				   offset to no more than his */
1199				if (ms->msgout[3] < ms->msgin[3])
1200					ms->msgout[3] = ms->msgin[3];
1201				if (ms->msgout[4] > ms->msgin[4])
1202					ms->msgout[4] = ms->msgin[4];
1203				set_sdtr(ms, ms->msgout[3], ms->msgout[4]);
1204				ms->msgphase = msg_out;
1205			} else {
1206				set_sdtr(ms, ms->msgin[3], ms->msgin[4]);
1207			}
1208			break;
1209		default:
1210			goto reject;
1211		}
1212		break;
1213	case SAVE_POINTERS:
1214		tp->saved_ptr = ms->data_ptr;
1215		break;
1216	case RESTORE_POINTERS:
1217		ms->data_ptr = tp->saved_ptr;
1218		break;
1219	case DISCONNECT:
1220		ms->phase = disconnecting;
1221		break;
1222	case ABORT:
1223		break;
1224	case MESSAGE_REJECT:
1225		if (tp->sdtr_state == sdtr_sent)
1226			set_sdtr(ms, 0, 0);
1227		break;
1228	case NOP:
1229		break;
1230	default:
1231		if (IDENTIFY_BASE <= code && code <= IDENTIFY_BASE + 7) {
1232			if (cmd == NULL) {
1233				do_abort(ms);
1234				ms->msgphase = msg_out;
1235			} else if (code != cmd->device->lun + IDENTIFY_BASE) {
1236				printk(KERN_WARNING "mesh: lun mismatch "
1237				       "(%d != %llu) on reselection from "
1238				       "target %d\n", code - IDENTIFY_BASE,
1239				       cmd->device->lun, ms->conn_tgt);
1240			}
1241			break;
1242		}
1243		goto reject;
1244	}
1245	return;
1246
1247 reject:
1248	printk(KERN_WARNING "mesh: rejecting message from target %d:",
1249	       ms->conn_tgt);
1250	for (i = 0; i < ms->n_msgin; ++i)
1251		printk(" %x", ms->msgin[i]);
1252	printk("\n");
1253	ms->msgout[0] = MESSAGE_REJECT;
1254	ms->n_msgout = 1;
1255	ms->msgphase = msg_out;
1256}
1257
1258/*
1259 * Set up DMA commands for transferring data.
1260 */
1261static void set_dma_cmds(struct mesh_state *ms, struct scsi_cmnd *cmd)
1262{
1263	int i, dma_cmd, total, off, dtot;
1264	struct scatterlist *scl;
1265	struct dbdma_cmd *dcmds;
1266
1267	dma_cmd = ms->tgts[ms->conn_tgt].data_goes_out?
1268		OUTPUT_MORE: INPUT_MORE;
1269	dcmds = ms->dma_cmds;
1270	dtot = 0;
1271	if (cmd) {
1272		int nseg;
1273
1274		cmd->SCp.this_residual = scsi_bufflen(cmd);
1275
1276		nseg = scsi_dma_map(cmd);
1277		BUG_ON(nseg < 0);
1278
1279		if (nseg) {
1280			total = 0;
1281			off = ms->data_ptr;
1282
1283			scsi_for_each_sg(cmd, scl, nseg, i) {
1284				u32 dma_addr = sg_dma_address(scl);
1285				u32 dma_len = sg_dma_len(scl);
1286				
1287				total += scl->length;
1288				if (off >= dma_len) {
1289					off -= dma_len;
1290					continue;
1291				}
1292				if (dma_len > 0xffff)
1293					panic("mesh: scatterlist element >= 64k");
1294				dcmds->req_count = cpu_to_le16(dma_len - off);
1295				dcmds->command = cpu_to_le16(dma_cmd);
1296				dcmds->phy_addr = cpu_to_le32(dma_addr + off);
1297				dcmds->xfer_status = 0;
1298				++dcmds;
1299				dtot += dma_len - off;
1300				off = 0;
1301			}
1302		}
1303	}
1304	if (dtot == 0) {
1305		/* Either the target has overrun our buffer,
1306		   or the caller didn't provide a buffer. */
1307		static char mesh_extra_buf[64];
1308
1309		dtot = sizeof(mesh_extra_buf);
1310		dcmds->req_count = cpu_to_le16(dtot);
1311		dcmds->phy_addr = cpu_to_le32(virt_to_phys(mesh_extra_buf));
1312		dcmds->xfer_status = 0;
1313		++dcmds;
1314	}
1315	dma_cmd += OUTPUT_LAST - OUTPUT_MORE;
1316	dcmds[-1].command = cpu_to_le16(dma_cmd);
1317	memset(dcmds, 0, sizeof(*dcmds));
1318	dcmds->command = cpu_to_le16(DBDMA_STOP);
1319	ms->dma_count = dtot;
1320}
1321
1322static void halt_dma(struct mesh_state *ms)
1323{
1324	volatile struct dbdma_regs __iomem *md = ms->dma;
1325	volatile struct mesh_regs __iomem *mr = ms->mesh;
1326	struct scsi_cmnd *cmd = ms->current_req;
1327	int t, nb;
1328
1329	if (!ms->tgts[ms->conn_tgt].data_goes_out) {
1330		/* wait a little while until the fifo drains */
1331		t = 50;
1332		while (t > 0 && in_8(&mr->fifo_count) != 0
1333		       && (in_le32(&md->status) & ACTIVE) != 0) {
1334			--t;
1335			udelay(1);
1336		}
1337	}
1338	out_le32(&md->control, RUN << 16);	/* turn off RUN bit */
1339	nb = (mr->count_hi << 8) + mr->count_lo;
1340	dlog(ms, "halt_dma fc/count=%.6x",
1341	     MKWORD(0, mr->fifo_count, 0, nb));
1342	if (ms->tgts[ms->conn_tgt].data_goes_out)
1343		nb += mr->fifo_count;
1344	/* nb is the number of bytes not yet transferred
1345	   to/from the target. */
1346	ms->data_ptr -= nb;
1347	dlog(ms, "data_ptr %x", ms->data_ptr);
1348	if (ms->data_ptr < 0) {
1349		printk(KERN_ERR "mesh: halt_dma: data_ptr=%d (nb=%d, ms=%p)\n",
1350		       ms->data_ptr, nb, ms);
1351		ms->data_ptr = 0;
1352#ifdef MESH_DBG
1353		dumplog(ms, ms->conn_tgt);
1354		dumpslog(ms);
1355#endif /* MESH_DBG */
1356	} else if (cmd && scsi_bufflen(cmd) &&
1357		   ms->data_ptr > scsi_bufflen(cmd)) {
1358		printk(KERN_DEBUG "mesh: target %d overrun, "
1359		       "data_ptr=%x total=%x goes_out=%d\n",
1360		       ms->conn_tgt, ms->data_ptr, scsi_bufflen(cmd),
1361		       ms->tgts[ms->conn_tgt].data_goes_out);
1362	}
1363	if (cmd)
1364		scsi_dma_unmap(cmd);
1365	ms->dma_started = 0;
1366}
1367
1368static void phase_mismatch(struct mesh_state *ms)
1369{
1370	volatile struct mesh_regs __iomem *mr = ms->mesh;
1371	int phase;
1372
1373	dlog(ms, "phasemm ch/cl/seq/fc=%.8x",
1374	     MKWORD(mr->count_hi, mr->count_lo, mr->sequence, mr->fifo_count));
1375	phase = in_8(&mr->bus_status0) & BS0_PHASE;
1376	if (ms->msgphase == msg_out_xxx && phase == BP_MSGOUT) {
1377		/* output the last byte of the message, without ATN */
1378		out_8(&mr->count_lo, 1);
1379		out_8(&mr->sequence, SEQ_MSGOUT + use_active_neg);
1380		mesh_flush_io(mr);
1381		udelay(1);
1382		out_8(&mr->fifo, ms->msgout[ms->n_msgout-1]);
1383		ms->msgphase = msg_out_last;
1384		return;
1385	}
1386
1387	if (ms->msgphase == msg_in) {
1388		get_msgin(ms);
1389		if (ms->n_msgin)
1390			handle_msgin(ms);
1391	}
1392
1393	if (ms->dma_started)
1394		halt_dma(ms);
1395	if (mr->fifo_count) {
1396		out_8(&mr->sequence, SEQ_FLUSHFIFO);
1397		mesh_flush_io(mr);
1398		udelay(1);
1399	}
1400
1401	ms->msgphase = msg_none;
1402	switch (phase) {
1403	case BP_DATAIN:
1404		ms->tgts[ms->conn_tgt].data_goes_out = 0;
1405		ms->phase = dataing;
1406		break;
1407	case BP_DATAOUT:
1408		ms->tgts[ms->conn_tgt].data_goes_out = 1;
1409		ms->phase = dataing;
1410		break;
1411	case BP_COMMAND:
1412		ms->phase = commanding;
1413		break;
1414	case BP_STATUS:
1415		ms->phase = statusing;
1416		break;
1417	case BP_MSGIN:
1418		ms->msgphase = msg_in;
1419		ms->n_msgin = 0;
1420		break;
1421	case BP_MSGOUT:
1422		ms->msgphase = msg_out;
1423		if (ms->n_msgout == 0) {
1424			if (ms->aborting) {
1425				do_abort(ms);
1426			} else {
1427				if (ms->last_n_msgout == 0) {
1428					printk(KERN_DEBUG
1429					       "mesh: no msg to repeat\n");
1430					ms->msgout[0] = NOP;
1431					ms->last_n_msgout = 1;
1432				}
1433				ms->n_msgout = ms->last_n_msgout;
1434			}
1435		}
1436		break;
1437	default:
1438		printk(KERN_DEBUG "mesh: unknown scsi phase %x\n", phase);
1439		ms->stat = DID_ERROR;
1440		mesh_done(ms, 1);
1441		return;
1442	}
1443
1444	start_phase(ms);
1445}
1446
1447static void cmd_complete(struct mesh_state *ms)
1448{
1449	volatile struct mesh_regs __iomem *mr = ms->mesh;
1450	struct scsi_cmnd *cmd = ms->current_req;
1451	struct mesh_target *tp = &ms->tgts[ms->conn_tgt];
1452	int seq, n, t;
1453
1454	dlog(ms, "cmd_complete fc=%x", mr->fifo_count);
1455	seq = use_active_neg + (ms->n_msgout? SEQ_ATN: 0);
1456	switch (ms->msgphase) {
1457	case msg_out_xxx:
1458		/* huh?  we expected a phase mismatch */
1459		ms->n_msgin = 0;
1460		ms->msgphase = msg_in;
1461		fallthrough;
1462
1463	case msg_in:
1464		/* should have some message bytes in fifo */
1465		get_msgin(ms);
1466		n = msgin_length(ms);
1467		if (ms->n_msgin < n) {
1468			out_8(&mr->count_lo, n - ms->n_msgin);
1469			out_8(&mr->sequence, SEQ_MSGIN + seq);
1470		} else {
1471			ms->msgphase = msg_none;
1472			handle_msgin(ms);
1473			start_phase(ms);
1474		}
1475		break;
1476
1477	case msg_in_bad:
1478		out_8(&mr->sequence, SEQ_FLUSHFIFO);
1479		mesh_flush_io(mr);
1480		udelay(1);
1481		out_8(&mr->count_lo, 1);
1482		out_8(&mr->sequence, SEQ_MSGIN + SEQ_ATN + use_active_neg);
1483		break;
1484
1485	case msg_out:
1486		/*
1487		 * To get the right timing on ATN wrt ACK, we have
1488		 * to get the MESH to drop ACK, wait until REQ gets
1489		 * asserted, then drop ATN.  To do this we first
1490		 * issue a SEQ_MSGOUT with ATN and wait for REQ,
1491		 * then change the command to a SEQ_MSGOUT w/o ATN.
1492		 * If we don't see REQ in a reasonable time, we
1493		 * change the command to SEQ_MSGIN with ATN,
1494		 * wait for the phase mismatch interrupt, then
1495		 * issue the SEQ_MSGOUT without ATN.
1496		 */
1497		out_8(&mr->count_lo, 1);
1498		out_8(&mr->sequence, SEQ_MSGOUT + use_active_neg + SEQ_ATN);
1499		t = 30;		/* wait up to 30us */
1500		while ((in_8(&mr->bus_status0) & BS0_REQ) == 0 && --t >= 0)
1501			udelay(1);
1502		dlog(ms, "last_mbyte err/exc/fc/cl=%.8x",
1503		     MKWORD(mr->error, mr->exception,
1504			    mr->fifo_count, mr->count_lo));
1505		if (in_8(&mr->interrupt) & (INT_ERROR | INT_EXCEPTION)) {
1506			/* whoops, target didn't do what we expected */
1507			ms->last_n_msgout = ms->n_msgout;
1508			ms->n_msgout = 0;
1509			if (in_8(&mr->interrupt) & INT_ERROR) {
1510				printk(KERN_ERR "mesh: error %x in msg_out\n",
1511				       in_8(&mr->error));
1512				handle_error(ms);
1513				return;
1514			}
1515			if (in_8(&mr->exception) != EXC_PHASEMM)
1516				printk(KERN_ERR "mesh: exc %x in msg_out\n",
1517				       in_8(&mr->exception));
1518			else
1519				printk(KERN_DEBUG "mesh: bs0=%x in msg_out\n",
1520				       in_8(&mr->bus_status0));
1521			handle_exception(ms);
1522			return;
1523		}
1524		if (in_8(&mr->bus_status0) & BS0_REQ) {
1525			out_8(&mr->sequence, SEQ_MSGOUT + use_active_neg);
1526			mesh_flush_io(mr);
1527			udelay(1);
1528			out_8(&mr->fifo, ms->msgout[ms->n_msgout-1]);
1529			ms->msgphase = msg_out_last;
1530		} else {
1531			out_8(&mr->sequence, SEQ_MSGIN + use_active_neg + SEQ_ATN);
1532			ms->msgphase = msg_out_xxx;
1533		}
1534		break;
1535
1536	case msg_out_last:
1537		ms->last_n_msgout = ms->n_msgout;
1538		ms->n_msgout = 0;
1539		ms->msgphase = ms->expect_reply? msg_in: msg_none;
1540		start_phase(ms);
1541		break;
1542
1543	case msg_none:
1544		switch (ms->phase) {
1545		case idle:
1546			printk(KERN_ERR "mesh: interrupt in idle phase?\n");
1547			dumpslog(ms);
1548			return;
1549		case selecting:
1550			dlog(ms, "Selecting phase at command completion",0);
1551			ms->msgout[0] = IDENTIFY(ALLOW_RESEL(ms->conn_tgt),
1552						 (cmd? cmd->device->lun: 0));
1553			ms->n_msgout = 1;
1554			ms->expect_reply = 0;
1555			if (ms->aborting) {
1556				ms->msgout[0] = ABORT;
1557				ms->n_msgout++;
1558			} else if (tp->sdtr_state == do_sdtr) {
1559				/* add SDTR message */
1560				add_sdtr_msg(ms);
1561				ms->expect_reply = 1;
1562				tp->sdtr_state = sdtr_sent;
1563			}
1564			ms->msgphase = msg_out;
1565			/*
1566			 * We need to wait for REQ before dropping ATN.
1567			 * We wait for at most 30us, then fall back to
1568			 * a scheme where we issue a SEQ_COMMAND with ATN,
1569			 * which will give us a phase mismatch interrupt
1570			 * when REQ does come, and then we send the message.
1571			 */
1572			t = 230;		/* wait up to 230us */
1573			while ((in_8(&mr->bus_status0) & BS0_REQ) == 0) {
1574				if (--t < 0) {
1575					dlog(ms, "impatient for req", ms->n_msgout);
1576					ms->msgphase = msg_none;
1577					break;
1578				}
1579				udelay(1);
1580			}
1581			break;
1582		case dataing:
1583			if (ms->dma_count != 0) {
1584				start_phase(ms);
1585				return;
1586			}
1587			/*
1588			 * We can get a phase mismatch here if the target
1589			 * changes to the status phase, even though we have
1590			 * had a command complete interrupt.  Then, if we
1591			 * issue the SEQ_STATUS command, we'll get a sequence
1592			 * error interrupt.  Which isn't so bad except that
1593			 * occasionally the mesh actually executes the
1594			 * SEQ_STATUS *as well as* giving us the sequence
1595			 * error and phase mismatch exception.
1596			 */
1597			out_8(&mr->sequence, 0);
1598			out_8(&mr->interrupt,
1599			      INT_ERROR | INT_EXCEPTION | INT_CMDDONE);
1600			halt_dma(ms);
1601			break;
1602		case statusing:
1603			if (cmd) {
1604				cmd->SCp.Status = mr->fifo;
 
 
1605				if (DEBUG_TARGET(cmd))
1606					printk(KERN_DEBUG "mesh: status is %x\n",
1607					       cmd->SCp.Status);
1608			}
1609			ms->msgphase = msg_in;
1610			break;
1611		case busfreeing:
1612			mesh_done(ms, 1);
1613			return;
1614		case disconnecting:
1615			ms->current_req = NULL;
1616			ms->phase = idle;
1617			mesh_start(ms);
1618			return;
1619		default:
1620			break;
1621		}
1622		++ms->phase;
1623		start_phase(ms);
1624		break;
1625	}
1626}
1627
1628
1629/*
1630 * Called by midlayer with host locked to queue a new
1631 * request
1632 */
1633static int mesh_queue_lck(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
1634{
1635	struct mesh_state *ms;
1636
1637	cmd->scsi_done = done;
1638	cmd->host_scribble = NULL;
1639
1640	ms = (struct mesh_state *) cmd->device->host->hostdata;
1641
1642	if (ms->request_q == NULL)
1643		ms->request_q = cmd;
1644	else
1645		ms->request_qtail->host_scribble = (void *) cmd;
1646	ms->request_qtail = cmd;
1647
1648	if (ms->phase == idle)
1649		mesh_start(ms);
1650
1651	return 0;
1652}
1653
1654static DEF_SCSI_QCMD(mesh_queue)
1655
1656/*
1657 * Called to handle interrupts, either call by the interrupt
1658 * handler (do_mesh_interrupt) or by other functions in
1659 * exceptional circumstances
1660 */
1661static void mesh_interrupt(struct mesh_state *ms)
1662{
1663	volatile struct mesh_regs __iomem *mr = ms->mesh;
1664	int intr;
1665
1666#if 0
1667	if (ALLOW_DEBUG(ms->conn_tgt))
1668		printk(KERN_DEBUG "mesh_intr, bs0=%x int=%x exc=%x err=%x "
1669		       "phase=%d msgphase=%d\n", mr->bus_status0,
1670		       mr->interrupt, mr->exception, mr->error,
1671		       ms->phase, ms->msgphase);
1672#endif
1673	while ((intr = in_8(&mr->interrupt)) != 0) {
1674		dlog(ms, "interrupt intr/err/exc/seq=%.8x", 
1675		     MKWORD(intr, mr->error, mr->exception, mr->sequence));
1676		if (intr & INT_ERROR) {
1677			handle_error(ms);
1678		} else if (intr & INT_EXCEPTION) {
1679			handle_exception(ms);
1680		} else if (intr & INT_CMDDONE) {
1681			out_8(&mr->interrupt, INT_CMDDONE);
1682			cmd_complete(ms);
1683		}
1684	}
1685}
1686
1687/* Todo: here we can at least try to remove the command from the
1688 * queue if it isn't connected yet, and for pending command, assert
1689 * ATN until the bus gets freed.
1690 */
1691static int mesh_abort(struct scsi_cmnd *cmd)
1692{
1693	struct mesh_state *ms = (struct mesh_state *) cmd->device->host->hostdata;
1694
1695	printk(KERN_DEBUG "mesh_abort(%p)\n", cmd);
1696	mesh_dump_regs(ms);
1697	dumplog(ms, cmd->device->id);
1698	dumpslog(ms);
1699	return FAILED;
1700}
1701
1702/*
1703 * Called by the midlayer with the lock held to reset the
1704 * SCSI host and bus.
1705 * The midlayer will wait for devices to come back, we don't need
1706 * to do that ourselves
1707 */
1708static int mesh_host_reset(struct scsi_cmnd *cmd)
1709{
1710	struct mesh_state *ms = (struct mesh_state *) cmd->device->host->hostdata;
1711	volatile struct mesh_regs __iomem *mr = ms->mesh;
1712	volatile struct dbdma_regs __iomem *md = ms->dma;
1713	unsigned long flags;
1714
1715	printk(KERN_DEBUG "mesh_host_reset\n");
1716
1717	spin_lock_irqsave(ms->host->host_lock, flags);
1718
1719	if (ms->dma_started)
1720		halt_dma(ms);
1721
1722	/* Reset the controller & dbdma channel */
1723	out_le32(&md->control, (RUN|PAUSE|FLUSH|WAKE) << 16);	/* stop dma */
1724	out_8(&mr->exception, 0xff);	/* clear all exception bits */
1725	out_8(&mr->error, 0xff);	/* clear all error bits */
1726	out_8(&mr->sequence, SEQ_RESETMESH);
1727       	mesh_flush_io(mr);
1728	udelay(1);
1729	out_8(&mr->intr_mask, INT_ERROR | INT_EXCEPTION | INT_CMDDONE);
1730	out_8(&mr->source_id, ms->host->this_id);
1731	out_8(&mr->sel_timeout, 25);	/* 250ms */
1732	out_8(&mr->sync_params, ASYNC_PARAMS);
1733
1734	/* Reset the bus */
1735	out_8(&mr->bus_status1, BS1_RST);	/* assert RST */
1736       	mesh_flush_io(mr);
1737	udelay(30);			/* leave it on for >= 25us */
1738	out_8(&mr->bus_status1, 0);	/* negate RST */
1739
1740	/* Complete pending commands */
1741	handle_reset(ms);
1742	
1743	spin_unlock_irqrestore(ms->host->host_lock, flags);
1744	return SUCCESS;
1745}
1746
1747static void set_mesh_power(struct mesh_state *ms, int state)
1748{
1749	if (!machine_is(powermac))
1750		return;
1751	if (state) {
1752		pmac_call_feature(PMAC_FTR_MESH_ENABLE, macio_get_of_node(ms->mdev), 0, 1);
1753		msleep(200);
1754	} else {
1755		pmac_call_feature(PMAC_FTR_MESH_ENABLE, macio_get_of_node(ms->mdev), 0, 0);
1756		msleep(10);
1757	}
1758}
1759
1760
1761#ifdef CONFIG_PM
1762static int mesh_suspend(struct macio_dev *mdev, pm_message_t mesg)
1763{
1764	struct mesh_state *ms = (struct mesh_state *)macio_get_drvdata(mdev);
1765	unsigned long flags;
1766
1767	switch (mesg.event) {
1768	case PM_EVENT_SUSPEND:
1769	case PM_EVENT_HIBERNATE:
1770	case PM_EVENT_FREEZE:
1771		break;
1772	default:
1773		return 0;
1774	}
1775	if (ms->phase == sleeping)
1776		return 0;
1777
1778	scsi_block_requests(ms->host);
1779	spin_lock_irqsave(ms->host->host_lock, flags);
1780	while(ms->phase != idle) {
1781		spin_unlock_irqrestore(ms->host->host_lock, flags);
1782		msleep(10);
1783		spin_lock_irqsave(ms->host->host_lock, flags);
1784	}
1785	ms->phase = sleeping;
1786	spin_unlock_irqrestore(ms->host->host_lock, flags);
1787	disable_irq(ms->meshintr);
1788	set_mesh_power(ms, 0);
1789
1790	return 0;
1791}
1792
1793static int mesh_resume(struct macio_dev *mdev)
1794{
1795	struct mesh_state *ms = (struct mesh_state *)macio_get_drvdata(mdev);
1796	unsigned long flags;
1797
1798	if (ms->phase != sleeping)
1799		return 0;
1800
1801	set_mesh_power(ms, 1);
1802	mesh_init(ms);
1803	spin_lock_irqsave(ms->host->host_lock, flags);
1804	mesh_start(ms);
1805	spin_unlock_irqrestore(ms->host->host_lock, flags);
1806	enable_irq(ms->meshintr);
1807	scsi_unblock_requests(ms->host);
1808
1809	return 0;
1810}
1811
1812#endif /* CONFIG_PM */
1813
1814/*
1815 * If we leave drives set for synchronous transfers (especially
1816 * CDROMs), and reboot to MacOS, it gets confused, poor thing.
1817 * So, on reboot we reset the SCSI bus.
1818 */
1819static int mesh_shutdown(struct macio_dev *mdev)
1820{
1821	struct mesh_state *ms = (struct mesh_state *)macio_get_drvdata(mdev);
1822	volatile struct mesh_regs __iomem *mr;
1823	unsigned long flags;
1824
1825       	printk(KERN_INFO "resetting MESH scsi bus(es)\n");
1826	spin_lock_irqsave(ms->host->host_lock, flags);
1827       	mr = ms->mesh;
1828	out_8(&mr->intr_mask, 0);
1829	out_8(&mr->interrupt, INT_ERROR | INT_EXCEPTION | INT_CMDDONE);
1830	out_8(&mr->bus_status1, BS1_RST);
1831	mesh_flush_io(mr);
1832	udelay(30);
1833	out_8(&mr->bus_status1, 0);
1834	spin_unlock_irqrestore(ms->host->host_lock, flags);
1835
1836	return 0;
1837}
1838
1839static struct scsi_host_template mesh_template = {
1840	.proc_name			= "mesh",
1841	.name				= "MESH",
1842	.queuecommand			= mesh_queue,
1843	.eh_abort_handler		= mesh_abort,
1844	.eh_host_reset_handler		= mesh_host_reset,
1845	.can_queue			= 20,
1846	.this_id			= 7,
1847	.sg_tablesize			= SG_ALL,
1848	.cmd_per_lun			= 2,
1849	.max_segment_size		= 65535,
 
1850};
1851
1852static int mesh_probe(struct macio_dev *mdev, const struct of_device_id *match)
1853{
1854	struct device_node *mesh = macio_get_of_node(mdev);
1855	struct pci_dev* pdev = macio_get_pci_dev(mdev);
1856	int tgt, minper;
1857	const int *cfp;
1858	struct mesh_state *ms;
1859	struct Scsi_Host *mesh_host;
1860	void *dma_cmd_space;
1861	dma_addr_t dma_cmd_bus;
1862
1863	switch (mdev->bus->chip->type) {
1864	case macio_heathrow:
1865	case macio_gatwick:
1866	case macio_paddington:
1867		use_active_neg = 0;
1868		break;
1869	default:
1870		use_active_neg = SEQ_ACTIVE_NEG;
1871	}
1872
1873	if (macio_resource_count(mdev) != 2 || macio_irq_count(mdev) != 2) {
1874       		printk(KERN_ERR "mesh: expected 2 addrs and 2 intrs"
1875	       	       " (got %d,%d)\n", macio_resource_count(mdev),
1876		       macio_irq_count(mdev));
1877		return -ENODEV;
1878	}
1879
1880	if (macio_request_resources(mdev, "mesh") != 0) {
1881       		printk(KERN_ERR "mesh: unable to request memory resources");
1882		return -EBUSY;
1883	}
1884       	mesh_host = scsi_host_alloc(&mesh_template, sizeof(struct mesh_state));
1885	if (mesh_host == NULL) {
1886		printk(KERN_ERR "mesh: couldn't register host");
1887		goto out_release;
1888	}
1889	
1890	/* Old junk for root discovery, that will die ultimately */
1891#if !defined(MODULE)
1892       	note_scsi_host(mesh, mesh_host);
1893#endif
1894
1895	mesh_host->base = macio_resource_start(mdev, 0);
1896	mesh_host->irq = macio_irq(mdev, 0);
1897       	ms = (struct mesh_state *) mesh_host->hostdata;
1898	macio_set_drvdata(mdev, ms);
1899	ms->host = mesh_host;
1900	ms->mdev = mdev;
1901	ms->pdev = pdev;
1902	
1903	ms->mesh = ioremap(macio_resource_start(mdev, 0), 0x1000);
1904	if (ms->mesh == NULL) {
1905		printk(KERN_ERR "mesh: can't map registers\n");
1906		goto out_free;
1907	}		
1908	ms->dma = ioremap(macio_resource_start(mdev, 1), 0x1000);
1909	if (ms->dma == NULL) {
1910		printk(KERN_ERR "mesh: can't map registers\n");
1911		iounmap(ms->mesh);
1912		goto out_free;
1913	}
1914
1915       	ms->meshintr = macio_irq(mdev, 0);
1916       	ms->dmaintr = macio_irq(mdev, 1);
1917
1918       	/* Space for dma command list: +1 for stop command,
1919       	 * +1 to allow for aligning.
1920	 */
1921	ms->dma_cmd_size = (mesh_host->sg_tablesize + 2) * sizeof(struct dbdma_cmd);
1922
1923	/* We use the PCI APIs for now until the generic one gets fixed
1924	 * enough or until we get some macio-specific versions
1925	 */
1926	dma_cmd_space = dma_alloc_coherent(&macio_get_pci_dev(mdev)->dev,
1927					   ms->dma_cmd_size, &dma_cmd_bus,
1928					   GFP_KERNEL);
1929	if (dma_cmd_space == NULL) {
1930		printk(KERN_ERR "mesh: can't allocate DMA table\n");
1931		goto out_unmap;
1932	}
1933
1934	ms->dma_cmds = (struct dbdma_cmd *) DBDMA_ALIGN(dma_cmd_space);
1935       	ms->dma_cmd_space = dma_cmd_space;
1936	ms->dma_cmd_bus = dma_cmd_bus + ((unsigned long)ms->dma_cmds)
1937		- (unsigned long)dma_cmd_space;
1938	ms->current_req = NULL;
1939       	for (tgt = 0; tgt < 8; ++tgt) {
1940	       	ms->tgts[tgt].sdtr_state = do_sdtr;
1941	       	ms->tgts[tgt].sync_params = ASYNC_PARAMS;
1942	       	ms->tgts[tgt].current_req = NULL;
1943       	}
1944
1945	if ((cfp = of_get_property(mesh, "clock-frequency", NULL)))
1946       		ms->clk_freq = *cfp;
1947	else {
1948       		printk(KERN_INFO "mesh: assuming 50MHz clock frequency\n");
1949	       	ms->clk_freq = 50000000;
1950       	}
1951
1952       	/* The maximum sync rate is clock / 5; increase
1953       	 * mesh_sync_period if necessary.
1954	 */
1955	minper = 1000000000 / (ms->clk_freq / 5); /* ns */
1956	if (mesh_sync_period < minper)
1957		mesh_sync_period = minper;
1958
1959	/* Power up the chip */
1960	set_mesh_power(ms, 1);
1961
1962	/* Set it up */
1963       	mesh_init(ms);
1964
1965	/* Request interrupt */
1966       	if (request_irq(ms->meshintr, do_mesh_interrupt, 0, "MESH", ms)) {
1967	       	printk(KERN_ERR "MESH: can't get irq %d\n", ms->meshintr);
1968		goto out_shutdown;
1969	}
1970
1971	/* Add scsi host & scan */
1972	if (scsi_add_host(mesh_host, &mdev->ofdev.dev))
1973		goto out_release_irq;
1974	scsi_scan_host(mesh_host);
1975
1976	return 0;
1977
1978 out_release_irq:
1979	free_irq(ms->meshintr, ms);
1980 out_shutdown:
1981	/* shutdown & reset bus in case of error or macos can be confused
1982	 * at reboot if the bus was set to synchronous mode already
1983	 */
1984	mesh_shutdown(mdev);
1985	set_mesh_power(ms, 0);
1986	dma_free_coherent(&macio_get_pci_dev(mdev)->dev, ms->dma_cmd_size,
1987			    ms->dma_cmd_space, ms->dma_cmd_bus);
1988 out_unmap:
1989	iounmap(ms->dma);
1990	iounmap(ms->mesh);
1991 out_free:
1992	scsi_host_put(mesh_host);
1993 out_release:
1994	macio_release_resources(mdev);
1995
1996	return -ENODEV;
1997}
1998
1999static int mesh_remove(struct macio_dev *mdev)
2000{
2001	struct mesh_state *ms = (struct mesh_state *)macio_get_drvdata(mdev);
2002	struct Scsi_Host *mesh_host = ms->host;
2003
2004	scsi_remove_host(mesh_host);
2005
2006	free_irq(ms->meshintr, ms);
2007
2008	/* Reset scsi bus */
2009	mesh_shutdown(mdev);
2010
2011	/* Shut down chip & termination */
2012	set_mesh_power(ms, 0);
2013
2014	/* Unmap registers & dma controller */
2015	iounmap(ms->mesh);
2016       	iounmap(ms->dma);
2017
2018	/* Free DMA commands memory */
2019	dma_free_coherent(&macio_get_pci_dev(mdev)->dev, ms->dma_cmd_size,
2020			    ms->dma_cmd_space, ms->dma_cmd_bus);
2021
2022	/* Release memory resources */
2023	macio_release_resources(mdev);
2024
2025	scsi_host_put(mesh_host);
2026
2027	return 0;
2028}
2029
2030
2031static struct of_device_id mesh_match[] = 
2032{
2033	{
2034	.name 		= "mesh",
2035	},
2036	{
2037	.type		= "scsi",
2038	.compatible	= "chrp,mesh0"
2039	},
2040	{},
2041};
2042MODULE_DEVICE_TABLE (of, mesh_match);
2043
2044static struct macio_driver mesh_driver = 
2045{
2046	.driver = {
2047		.name 		= "mesh",
2048		.owner		= THIS_MODULE,
2049		.of_match_table	= mesh_match,
2050	},
2051	.probe		= mesh_probe,
2052	.remove		= mesh_remove,
2053	.shutdown	= mesh_shutdown,
2054#ifdef CONFIG_PM
2055	.suspend	= mesh_suspend,
2056	.resume		= mesh_resume,
2057#endif
2058};
2059
2060
2061static int __init init_mesh(void)
2062{
2063
2064	/* Calculate sync rate from module parameters */
2065	if (sync_rate > 10)
2066		sync_rate = 10;
2067	if (sync_rate > 0) {
2068		printk(KERN_INFO "mesh: configured for synchronous %d MB/s\n", sync_rate);
2069		mesh_sync_period = 1000 / sync_rate;	/* ns */
2070		mesh_sync_offset = 15;
2071	} else
2072		printk(KERN_INFO "mesh: configured for asynchronous\n");
2073
2074	return macio_register_driver(&mesh_driver);
2075}
2076
2077static void __exit exit_mesh(void)
2078{
2079	return macio_unregister_driver(&mesh_driver);
2080}
2081
2082module_init(init_mesh);
2083module_exit(exit_mesh);