atp870u.c 72 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3
/* 
 *  Copyright (C) 1997	Wu Ching Chen
 *  2.1.x update (C) 1998  Krzysztof G. Baranowski
4 5
 *  2.5.x update (C) 2002  Red Hat
 *  2.6.x update (C) 2004  Red Hat
Linus Torvalds's avatar
Linus Torvalds committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
 *
 * Marcelo Tosatti <marcelo@conectiva.com.br> : SMP fixes
 *
 * Wu Ching Chen : NULL pointer fixes  2000/06/02
 *		   support atp876 chip
 *		   enable 32 bit fifo transfer
 *		   support cdrom & remove device run ultra speed
 *		   fix disconnect bug  2000/12/21
 *		   support atp880 chip lvd u160 2001/05/15
 *		   fix prd table bug 2001/09/12 (7.1)
 *
 * atp885 support add by ACARD Hao Ping Lian 2005/01/05
 */
#include <linux/module.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/kernel.h>
#include <linux/types.h>
#include <linux/string.h>
#include <linux/ioport.h>
#include <linux/delay.h>
#include <linux/proc_fs.h>
#include <linux/spinlock.h>
#include <linux/pci.h>
#include <linux/blkdev.h>
31
#include <linux/dma-mapping.h>
32
#include <linux/slab.h>
Linus Torvalds's avatar
Linus Torvalds committed
33 34 35 36 37 38 39 40 41 42 43
#include <asm/io.h>

#include <scsi/scsi.h>
#include <scsi/scsi_cmnd.h>
#include <scsi/scsi_device.h>
#include <scsi/scsi_host.h>

#include "atp870u.h"

static struct scsi_host_template atp870u_template;
static void send_s870(struct atp_unit *dev,unsigned char c);
44
static void is885(struct atp_unit *dev, unsigned char c, bool wide_chip, unsigned char lvdmode);
Linus Torvalds's avatar
Linus Torvalds committed
45 46
static void tscam_885(void);

47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
static inline void atp_writeb_base(struct atp_unit *atp, u8 reg, u8 val)
{
	outb(val, atp->baseport + reg);
}

static inline void atp_writeb_io(struct atp_unit *atp, u8 channel, u8 reg, u8 val)
{
	outb(val, atp->ioport[channel] + reg);
}

static inline void atp_writew_io(struct atp_unit *atp, u8 channel, u8 reg, u16 val)
{
	outw(val, atp->ioport[channel] + reg);
}

static inline void atp_writeb_pci(struct atp_unit *atp, u8 channel, u8 reg, u8 val)
{
	outb(val, atp->pciport[channel] + reg);
}

static inline void atp_writel_pci(struct atp_unit *atp, u8 channel, u8 reg, u32 val)
{
	outl(val, atp->pciport[channel] + reg);
}

static inline u8 atp_readb_base(struct atp_unit *atp, u8 reg)
{
	return inb(atp->baseport + reg);
}

static inline u8 atp_readb_io(struct atp_unit *atp, u8 channel, u8 reg)
{
	return inb(atp->ioport[channel] + reg);
}

static inline u16 atp_readw_io(struct atp_unit *atp, u8 channel, u8 reg)
{
	return inw(atp->ioport[channel] + reg);
}

static inline u8 atp_readb_pci(struct atp_unit *atp, u8 channel, u8 reg)
{
	return inb(atp->pciport[channel] + reg);
}

92
static irqreturn_t atp870u_intr_handle(int irq, void *dev_id)
Linus Torvalds's avatar
Linus Torvalds committed
93 94
{
	unsigned long flags;
Ondrej Zary's avatar
Ondrej Zary committed
95
	unsigned short int id;
Linus Torvalds's avatar
Linus Torvalds committed
96 97 98 99 100 101 102 103 104 105 106
	unsigned char i, j, c, target_id, lun,cmdp;
	unsigned char *prd;
	struct scsi_cmnd *workreq;
	unsigned long adrcnt, k;
#ifdef ED_DBGP
	unsigned long l;
#endif
	struct Scsi_Host *host = dev_id;
	struct atp_unit *dev = (struct atp_unit *)&host->hostdata;

	for (c = 0; c < 2; c++) {
107
		j = atp_readb_io(dev, c, 0x1f);
Linus Torvalds's avatar
Linus Torvalds committed
108
		if ((j & 0x80) != 0)
Ondrej Zary's avatar
Ondrej Zary committed
109
			break;
Linus Torvalds's avatar
Linus Torvalds committed
110 111
		dev->in_int[c] = 0;
	}
Ondrej Zary's avatar
Ondrej Zary committed
112 113
	if ((j & 0x80) == 0)
		return IRQ_NONE;
Linus Torvalds's avatar
Linus Torvalds committed
114 115 116 117
#ifdef ED_DBGP	
	printk("atp870u_intr_handle enter\n");
#endif	
	dev->in_int[c] = 1;
118
	cmdp = atp_readb_io(dev, c, 0x10);
Linus Torvalds's avatar
Linus Torvalds committed
119 120
	if (dev->working[c] != 0) {
		if (dev->dev_id == ATP885_DEVID) {
121 122
			if ((atp_readb_io(dev, c, 0x16) & 0x80) == 0)
				atp_writeb_io(dev, c, 0x16, (atp_readb_io(dev, c, 0x16) | 0x80));
Linus Torvalds's avatar
Linus Torvalds committed
123
		}		
124
		if ((atp_readb_pci(dev, c, 0x00) & 0x08) != 0)
Linus Torvalds's avatar
Linus Torvalds committed
125 126
		{
			for (k=0; k < 1000; k++) {
127
				if ((atp_readb_pci(dev, c, 2) & 0x08) == 0)
Ondrej Zary's avatar
Ondrej Zary committed
128
					break;
129
				if ((atp_readb_pci(dev, c, 2) & 0x01) == 0)
Ondrej Zary's avatar
Ondrej Zary committed
130
					break;
Linus Torvalds's avatar
Linus Torvalds committed
131 132
			}
		}
133
		atp_writeb_pci(dev, c, 0, 0x00);
Linus Torvalds's avatar
Linus Torvalds committed
134
		
135
		i = atp_readb_io(dev, c, 0x17);
Linus Torvalds's avatar
Linus Torvalds committed
136
		
Ondrej Zary's avatar
Ondrej Zary committed
137
		if (dev->dev_id == ATP885_DEVID)
138
			atp_writeb_pci(dev, c, 2, 0x06);
Linus Torvalds's avatar
Linus Torvalds committed
139

140
		target_id = atp_readb_io(dev, c, 0x15);
Linus Torvalds's avatar
Linus Torvalds committed
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168

		/*
		 *	Remap wide devices onto id numbers
		 */

		if ((target_id & 0x40) != 0) {
			target_id = (target_id & 0x07) | 0x08;
		} else {
			target_id &= 0x07;
		}

		if ((j & 0x40) != 0) {
		     if (dev->last_cmd[c] == 0xff) {
			dev->last_cmd[c] = target_id;
		     }
		     dev->last_cmd[c] |= 0x40;
		}
		if (dev->dev_id == ATP885_DEVID) 
			dev->r1f[c][target_id] |= j;
#ifdef ED_DBGP
		printk("atp870u_intr_handle status = %x\n",i);
#endif	
		if (i == 0x85) {
			if ((dev->last_cmd[c] & 0xf0) != 0x40) {
			   dev->last_cmd[c] = 0xff;
			}
			if (dev->dev_id == ATP885_DEVID) {
				adrcnt = 0;
169 170 171
				((unsigned char *) &adrcnt)[2] = atp_readb_io(dev, c, 0x12);
				((unsigned char *) &adrcnt)[1] = atp_readb_io(dev, c, 0x13);
				((unsigned char *) &adrcnt)[0] = atp_readb_io(dev, c, 0x14);
Linus Torvalds's avatar
Linus Torvalds committed
172 173 174 175 176 177 178 179
				if (dev->id[c][target_id].last_len != adrcnt)
				{
			   		k = dev->id[c][target_id].last_len;
			   		k -= adrcnt;
			   		dev->id[c][target_id].tran_len = k;			   
			   	dev->id[c][target_id].last_len = adrcnt;			   
				}
#ifdef ED_DBGP
Ondrej Zary's avatar
Ondrej Zary committed
180
				printk("dev->id[c][target_id].last_len = %d dev->id[c][target_id].tran_len = %d\n",dev->id[c][target_id].last_len,dev->id[c][target_id].tran_len);
Linus Torvalds's avatar
Linus Torvalds committed
181 182 183 184 185 186 187
#endif		
			}

			/*
			 *      Flip wide
			 */			
			if (dev->wide_id[c] != 0) {
188 189 190
				atp_writeb_io(dev, c, 0x1b, 0x01);
				while ((atp_readb_io(dev, c, 0x1b) & 0x01) != 0x01)
					atp_writeb_io(dev, c, 0x1b, 0x01);
Linus Torvalds's avatar
Linus Torvalds committed
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
			}		
			/*
			 *	Issue more commands
			 */
			spin_lock_irqsave(dev->host->host_lock, flags);			 			 
			if (((dev->quhd[c] != dev->quend[c]) || (dev->last_cmd[c] != 0xff)) &&
			    (dev->in_snd[c] == 0)) {
#ifdef ED_DBGP
				printk("Call sent_s870\n");
#endif				
				send_s870(dev,c);
			}
			spin_unlock_irqrestore(dev->host->host_lock, flags);
			/*
			 *	Done
			 */
			dev->in_int[c] = 0;
#ifdef ED_DBGP
				printk("Status 0x85 return\n");
#endif				
Ondrej Zary's avatar
Ondrej Zary committed
211
			return IRQ_HANDLED;
Linus Torvalds's avatar
Linus Torvalds committed
212 213 214 215 216
		}

		if (i == 0x40) {
		     dev->last_cmd[c] |= 0x40;
		     dev->in_int[c] = 0;
Ondrej Zary's avatar
Ondrej Zary committed
217
		     return IRQ_HANDLED;
Linus Torvalds's avatar
Linus Torvalds committed
218 219 220 221 222 223 224
		}

		if (i == 0x21) {
			if ((dev->last_cmd[c] & 0xf0) != 0x40) {
			   dev->last_cmd[c] = 0xff;
			}
			adrcnt = 0;
225 226 227
			((unsigned char *) &adrcnt)[2] = atp_readb_io(dev, c, 0x12);
			((unsigned char *) &adrcnt)[1] = atp_readb_io(dev, c, 0x13);
			((unsigned char *) &adrcnt)[0] = atp_readb_io(dev, c, 0x14);
Linus Torvalds's avatar
Linus Torvalds committed
228 229 230 231
			k = dev->id[c][target_id].last_len;
			k -= adrcnt;
			dev->id[c][target_id].tran_len = k;
			dev->id[c][target_id].last_len = adrcnt;
232 233
			atp_writeb_io(dev, c, 0x10, 0x41);
			atp_writeb_io(dev, c, 0x18, 0x08);
Linus Torvalds's avatar
Linus Torvalds committed
234
			dev->in_int[c] = 0;
Ondrej Zary's avatar
Ondrej Zary committed
235
			return IRQ_HANDLED;
Linus Torvalds's avatar
Linus Torvalds committed
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
		}

		if (dev->dev_id == ATP885_DEVID) {
			if ((i == 0x4c) || (i == 0x4d) || (i == 0x8c) || (i == 0x8d)) {
		   		if ((i == 0x4c) || (i == 0x8c)) 
		      			i=0x48;
		   		else 
		      			i=0x49;
		   	}	
			
		}
		if ((i == 0x80) || (i == 0x8f)) {
#ifdef ED_DBGP
			printk(KERN_DEBUG "Device reselect\n");
#endif			
			lun = 0;
252 253 254
			if (cmdp == 0x44 || i == 0x80)
				lun = atp_readb_io(dev, c, 0x1d) & 0x07;
			else {
Linus Torvalds's avatar
Linus Torvalds committed
255 256 257 258 259 260 261 262
				if ((dev->last_cmd[c] & 0xf0) != 0x40) {
				   dev->last_cmd[c] = 0xff;
				}
				if (cmdp == 0x41) {
#ifdef ED_DBGP
					printk("cmdp = 0x41\n");
#endif						
					adrcnt = 0;
263 264 265
					((unsigned char *) &adrcnt)[2] = atp_readb_io(dev, c, 0x12);
					((unsigned char *) &adrcnt)[1] = atp_readb_io(dev, c, 0x13);
					((unsigned char *) &adrcnt)[0] = atp_readb_io(dev, c, 0x14);
Linus Torvalds's avatar
Linus Torvalds committed
266 267 268 269
					k = dev->id[c][target_id].last_len;
					k -= adrcnt;
					dev->id[c][target_id].tran_len = k;
					dev->id[c][target_id].last_len = adrcnt;
270
					atp_writeb_io(dev, c, 0x18, 0x08);
Linus Torvalds's avatar
Linus Torvalds committed
271
					dev->in_int[c] = 0;
Ondrej Zary's avatar
Ondrej Zary committed
272
					return IRQ_HANDLED;
Linus Torvalds's avatar
Linus Torvalds committed
273 274 275 276
				} else {
#ifdef ED_DBGP
					printk("cmdp != 0x41\n");
#endif						
277
					atp_writeb_io(dev, c, 0x10, 0x46);
Linus Torvalds's avatar
Linus Torvalds committed
278
					dev->id[c][target_id].dirct = 0x00;
279 280 281 282
					atp_writeb_io(dev, c, 0x12, 0x00);
					atp_writeb_io(dev, c, 0x13, 0x00);
					atp_writeb_io(dev, c, 0x14, 0x00);
					atp_writeb_io(dev, c, 0x18, 0x08);
Linus Torvalds's avatar
Linus Torvalds committed
283
					dev->in_int[c] = 0;
Ondrej Zary's avatar
Ondrej Zary committed
284
					return IRQ_HANDLED;
Linus Torvalds's avatar
Linus Torvalds committed
285 286 287 288 289 290
				}
			}
			if (dev->last_cmd[c] != 0xff) {
			   dev->last_cmd[c] |= 0x40;
			}
			if (dev->dev_id == ATP885_DEVID) {
291 292
				j = atp_readb_base(dev, 0x29) & 0xfe;
				atp_writeb_base(dev, 0x29, j);
Ondrej Zary's avatar
Ondrej Zary committed
293
			} else
294
				atp_writeb_io(dev, c, 0x10, 0x45);
Ondrej Zary's avatar
Ondrej Zary committed
295

296
			target_id = atp_readb_io(dev, c, 0x16);
Linus Torvalds's avatar
Linus Torvalds committed
297 298 299 300 301 302 303 304
			/*
			 *	Remap wide identifiers
			 */
			if ((target_id & 0x10) != 0) {
				target_id = (target_id & 0x07) | 0x08;
			} else {
				target_id &= 0x07;
			}
Ondrej Zary's avatar
Ondrej Zary committed
305
			if (dev->dev_id == ATP885_DEVID)
306
				atp_writeb_io(dev, c, 0x10, 0x45);
Linus Torvalds's avatar
Linus Torvalds committed
307 308
			workreq = dev->id[c][target_id].curr_req;
#ifdef ED_DBGP			
309 310
			scmd_printk(KERN_DEBUG, workreq, "CDB");
			for (l = 0; l < workreq->cmd_len; l++)
Linus Torvalds's avatar
Linus Torvalds committed
311
				printk(KERN_DEBUG " %x",workreq->cmnd[l]);
312
			printk("\n");
Linus Torvalds's avatar
Linus Torvalds committed
313 314
#endif	
			
315 316
			atp_writeb_io(dev, c, 0x0f, lun);
			atp_writeb_io(dev, c, 0x11, dev->id[c][target_id].devsp);
Linus Torvalds's avatar
Linus Torvalds committed
317 318 319
			adrcnt = dev->id[c][target_id].tran_len;
			k = dev->id[c][target_id].last_len;

320 321 322
			atp_writeb_io(dev, c, 0x12, ((unsigned char *) &k)[2]);
			atp_writeb_io(dev, c, 0x13, ((unsigned char *) &k)[1]);
			atp_writeb_io(dev, c, 0x14, ((unsigned char *) &k)[0]);
Linus Torvalds's avatar
Linus Torvalds committed
323
#ifdef ED_DBGP			
324
			printk("k %x, k[0] 0x%x k[1] 0x%x k[2] 0x%x\n", k, atp_readb_io(dev, c, 0x14), atp_readb_io(dev, c, 0x13), atp_readb_io(dev, c, 0x12));
Linus Torvalds's avatar
Linus Torvalds committed
325 326 327 328 329 330 331 332
#endif			
			/* Remap wide */
			j = target_id;
			if (target_id > 7) {
				j = (j & 0x07) | 0x40;
			}
			/* Add direction */
			j |= dev->id[c][target_id].dirct;
333 334
			atp_writeb_io(dev, c, 0x15, j);
			atp_writeb_io(dev, c, 0x16, 0x80);
Linus Torvalds's avatar
Linus Torvalds committed
335 336 337
			
			/* enable 32 bit fifo transfer */	
			if (dev->dev_id == ATP885_DEVID) {
338
				i = atp_readb_pci(dev, c, 1) & 0xf3;
Linus Torvalds's avatar
Linus Torvalds committed
339 340 341 342
				//j=workreq->cmnd[0];	    		    	
				if ((workreq->cmnd[0] == 0x08) || (workreq->cmnd[0] == 0x28) || (workreq->cmnd[0] == 0x0a) || (workreq->cmnd[0] == 0x2a)) {
				   i |= 0x0c;
				}
343
				atp_writeb_pci(dev, c, 1, i);
Linus Torvalds's avatar
Linus Torvalds committed
344 345
			} else if ((dev->dev_id == ATP880_DEVID1) ||
	    		    	   (dev->dev_id == ATP880_DEVID2) ) {
346 347 348 349
				if ((workreq->cmnd[0] == 0x08) || (workreq->cmnd[0] == 0x28) || (workreq->cmnd[0] == 0x0a) || (workreq->cmnd[0] == 0x2a))
					atp_writeb_base(dev, 0x3b, (atp_readb_base(dev, 0x3b) & 0x3f) | 0xc0);
				else
					atp_writeb_base(dev, 0x3b, atp_readb_base(dev, 0x3b) & 0x3f);
Linus Torvalds's avatar
Linus Torvalds committed
350
			} else {				
351 352 353 354
				if ((workreq->cmnd[0] == 0x08) || (workreq->cmnd[0] == 0x28) || (workreq->cmnd[0] == 0x0a) || (workreq->cmnd[0] == 0x2a))
					atp_writeb_io(dev, c, 0x3a, (atp_readb_io(dev, c, 0x3a) & 0xf3) | 0x08);
				else
					atp_writeb_io(dev, c, 0x3a, atp_readb_io(dev, c, 0x3a) & 0xf3);
Linus Torvalds's avatar
Linus Torvalds committed
355 356 357 358 359 360 361 362 363 364
			}	
			j = 0;
			id = 1;
			id = id << target_id;
			/*
			 *	Is this a wide device
			 */
			if ((id & dev->wide_id[c]) != 0) {
				j |= 0x01;
			}
365 366 367
			atp_writeb_io(dev, c, 0x1b, j);
			while ((atp_readb_io(dev, c, 0x1b) & 0x01) != j)
				atp_writeb_io(dev, c, 0x1b, j);
Linus Torvalds's avatar
Linus Torvalds committed
368
			if (dev->id[c][target_id].last_len == 0) {
369
				atp_writeb_io(dev, c, 0x18, 0x08);
Linus Torvalds's avatar
Linus Torvalds committed
370 371 372 373
				dev->in_int[c] = 0;
#ifdef ED_DBGP
				printk("dev->id[c][target_id].last_len = 0\n");
#endif					
Ondrej Zary's avatar
Ondrej Zary committed
374
				return IRQ_HANDLED;
Linus Torvalds's avatar
Linus Torvalds committed
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
			}
#ifdef ED_DBGP
			printk("target_id = %d adrcnt = %d\n",target_id,adrcnt);
#endif			
			prd = dev->id[c][target_id].prd_pos;
			while (adrcnt != 0) {
				id = ((unsigned short int *)prd)[2];
				if (id == 0) {
					k = 0x10000;
				} else {
					k = id;
				}
				if (k > adrcnt) {
					((unsigned short int *)prd)[2] = (unsigned short int)
					    (k - adrcnt);
					((unsigned long *)prd)[0] += adrcnt;
					adrcnt = 0;
					dev->id[c][target_id].prd_pos = prd;
				} else {
					adrcnt -= k;
					dev->id[c][target_id].prdaddr += 0x08;
					prd += 0x08;
					if (adrcnt == 0) {
						dev->id[c][target_id].prd_pos = prd;
					}
				}				
			}
402
			atp_writel_pci(dev, c, 0x04, dev->id[c][target_id].prdaddr);
Linus Torvalds's avatar
Linus Torvalds committed
403 404 405
#ifdef ED_DBGP
			printk("dev->id[%d][%d].prdaddr 0x%8x\n", c, target_id, dev->id[c][target_id].prdaddr);
#endif
Ondrej Zary's avatar
Ondrej Zary committed
406
			if (dev->dev_id != ATP885_DEVID) {
407 408
				atp_writeb_pci(dev, c, 2, 0x06);
				atp_writeb_pci(dev, c, 2, 0x00);
Linus Torvalds's avatar
Linus Torvalds committed
409 410 411 412 413
			}
			/*
			 *	Check transfer direction
			 */
			if (dev->id[c][target_id].dirct != 0) {
414 415
				atp_writeb_io(dev, c, 0x18, 0x08);
				atp_writeb_pci(dev, c, 0, 0x01);
Linus Torvalds's avatar
Linus Torvalds committed
416 417 418 419
				dev->in_int[c] = 0;
#ifdef ED_DBGP
				printk("status 0x80 return dirct != 0\n");
#endif				
Ondrej Zary's avatar
Ondrej Zary committed
420
				return IRQ_HANDLED;
Linus Torvalds's avatar
Linus Torvalds committed
421
			}
422 423
			atp_writeb_io(dev, c, 0x18, 0x08);
			atp_writeb_pci(dev, c, 0, 0x09);
Linus Torvalds's avatar
Linus Torvalds committed
424 425 426 427
			dev->in_int[c] = 0;
#ifdef ED_DBGP
			printk("status 0x80 return dirct = 0\n");
#endif			
Ondrej Zary's avatar
Ondrej Zary committed
428
			return IRQ_HANDLED;
Linus Torvalds's avatar
Linus Torvalds committed
429 430 431 432 433 434 435 436
		}

		/*
		 *	Current scsi request on this target
		 */

		workreq = dev->id[c][target_id].curr_req;

Ondrej Zary's avatar
Ondrej Zary committed
437
		if (i == 0x42 || i == 0x16) {
Linus Torvalds's avatar
Linus Torvalds committed
438 439 440
			if ((dev->last_cmd[c] & 0xf0) != 0x40) {
			   dev->last_cmd[c] = 0xff;
			}
Ondrej Zary's avatar
Ondrej Zary committed
441
			if (i == 0x16) {
442
				workreq->result = atp_readb_io(dev, c, 0x0f);
Ondrej Zary's avatar
Ondrej Zary committed
443 444 445 446 447 448 449
				if (((dev->r1f[c][target_id] & 0x10) != 0)&&(dev->dev_id==ATP885_DEVID)) {
					printk(KERN_WARNING "AEC67162 CRC ERROR !\n");
					workreq->result = 0x02;
				}
			} else
				workreq->result = 0x02;

Linus Torvalds's avatar
Linus Torvalds committed
450
			if (dev->dev_id == ATP885_DEVID) {		
451 452
				j = atp_readb_base(dev, 0x29) | 0x01;
				atp_writeb_base(dev, 0x29, j);
Linus Torvalds's avatar
Linus Torvalds committed
453 454 455 456
			}
			/*
			 *	Complete the command
			 */
457 458
			scsi_dma_unmap(workreq);

Linus Torvalds's avatar
Linus Torvalds committed
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
			spin_lock_irqsave(dev->host->host_lock, flags);
			(*workreq->scsi_done) (workreq);
#ifdef ED_DBGP
			   printk("workreq->scsi_done\n");
#endif	
			/*
			 *	Clear it off the queue
			 */
			dev->id[c][target_id].curr_req = NULL;
			dev->working[c]--;
			spin_unlock_irqrestore(dev->host->host_lock, flags);
			/*
			 *      Take it back wide
			 */
			if (dev->wide_id[c] != 0) {
474 475 476
				atp_writeb_io(dev, c, 0x1b, 0x01);
				while ((atp_readb_io(dev, c, 0x1b) & 0x01) != 0x01)
					atp_writeb_io(dev, c, 0x1b, 0x01);
Linus Torvalds's avatar
Linus Torvalds committed
477 478 479 480 481 482 483 484 485 486 487 488 489 490
			} 
			/*
			 *	If there is stuff to send and nothing going then send it
			 */
			spin_lock_irqsave(dev->host->host_lock, flags);
			if (((dev->last_cmd[c] != 0xff) || (dev->quhd[c] != dev->quend[c])) &&
			    (dev->in_snd[c] == 0)) {
#ifdef ED_DBGP
			   printk("Call sent_s870(scsi_done)\n");
#endif				   
			   send_s870(dev,c);
			}
			spin_unlock_irqrestore(dev->host->host_lock, flags);
			dev->in_int[c] = 0;
Ondrej Zary's avatar
Ondrej Zary committed
491
			return IRQ_HANDLED;
Linus Torvalds's avatar
Linus Torvalds committed
492 493 494 495 496 497 498 499 500
		}
		if ((dev->last_cmd[c] & 0xf0) != 0x40) {
		   dev->last_cmd[c] = 0xff;
		}
		if (i == 0x4f) {
			i = 0x89;
		}
		i &= 0x0f;
		if (i == 0x09) {
501 502 503 504
			atp_writel_pci(dev, c, 4, dev->id[c][target_id].prdaddr);
			atp_writeb_pci(dev, c, 2, 0x06);
			atp_writeb_pci(dev, c, 2, 0x00);
			atp_writeb_io(dev, c, 0x10, 0x41);
Linus Torvalds's avatar
Linus Torvalds committed
505 506
			if (dev->dev_id == ATP885_DEVID) {
				k = dev->id[c][target_id].last_len;
507 508 509
				atp_writeb_io(dev, c, 0x12, ((unsigned char *) (&k))[2]);
				atp_writeb_io(dev, c, 0x13, ((unsigned char *) (&k))[1]);
				atp_writeb_io(dev, c, 0x14, ((unsigned char *) (&k))[0]);
Linus Torvalds's avatar
Linus Torvalds committed
510 511 512 513
				dev->id[c][target_id].dirct = 0x00;
			} else {
				dev->id[c][target_id].dirct = 0x00;
			}
514 515
			atp_writeb_io(dev, c, 0x18, 0x08);
			atp_writeb_pci(dev, c, 0, 0x09);
Linus Torvalds's avatar
Linus Torvalds committed
516
			dev->in_int[c] = 0;
Ondrej Zary's avatar
Ondrej Zary committed
517
			return IRQ_HANDLED;
Linus Torvalds's avatar
Linus Torvalds committed
518 519
		}
		if (i == 0x08) {
520 521 522 523
			atp_writel_pci(dev, c, 4, dev->id[c][target_id].prdaddr);
			atp_writeb_pci(dev, c, 2, 0x06);
			atp_writeb_pci(dev, c, 2, 0x00);
			atp_writeb_io(dev, c, 0x10, 0x41);
Linus Torvalds's avatar
Linus Torvalds committed
524 525
			if (dev->dev_id == ATP885_DEVID) {		
				k = dev->id[c][target_id].last_len;
526 527 528
				atp_writeb_io(dev, c, 0x12, ((unsigned char *) (&k))[2]);
				atp_writeb_io(dev, c, 0x13, ((unsigned char *) (&k))[1]);
				atp_writeb_io(dev, c, 0x14, ((unsigned char *) (&k))[0]);
Linus Torvalds's avatar
Linus Torvalds committed
529
			}
530
			atp_writeb_io(dev, c, 0x15, atp_readb_io(dev, c, 0x15) | 0x20);
Linus Torvalds's avatar
Linus Torvalds committed
531
			dev->id[c][target_id].dirct = 0x20;
532 533
			atp_writeb_io(dev, c, 0x18, 0x08);
			atp_writeb_pci(dev, c, 0, 0x01);
Linus Torvalds's avatar
Linus Torvalds committed
534
			dev->in_int[c] = 0;
Ondrej Zary's avatar
Ondrej Zary committed
535
			return IRQ_HANDLED;
Linus Torvalds's avatar
Linus Torvalds committed
536
		}
537 538 539 540
		if (i == 0x0a)
			atp_writeb_io(dev, c, 0x10, 0x30);
		else
			atp_writeb_io(dev, c, 0x10, 0x46);
Linus Torvalds's avatar
Linus Torvalds committed
541
		dev->id[c][target_id].dirct = 0x00;
542 543 544 545
		atp_writeb_io(dev, c, 0x12, 0x00);
		atp_writeb_io(dev, c, 0x13, 0x00);
		atp_writeb_io(dev, c, 0x14, 0x00);
		atp_writeb_io(dev, c, 0x18, 0x08);
Linus Torvalds's avatar
Linus Torvalds committed
546
	}
Ondrej Zary's avatar
Ondrej Zary committed
547 548
	dev->in_int[c] = 0;

Linus Torvalds's avatar
Linus Torvalds committed
549 550 551 552 553 554 555 556 557
	return IRQ_HANDLED;
}
/**
 *	atp870u_queuecommand	-	Queue SCSI command
 *	@req_p: request block
 *	@done: completion function
 *
 *	Queue a command to the ATP queue. Called with the host lock held.
 */
Jeff Garzik's avatar
Jeff Garzik committed
558
static int atp870u_queuecommand_lck(struct scsi_cmnd *req_p,
Linus Torvalds's avatar
Linus Torvalds committed
559 560 561
			 void (*done) (struct scsi_cmnd *))
{
	unsigned char c;
Ondrej Zary's avatar
Ondrej Zary committed
562
	unsigned int m;
Linus Torvalds's avatar
Linus Torvalds committed
563 564 565
	struct atp_unit *dev;
	struct Scsi_Host *host;

566
	c = scmd_channel(req_p);
Linus Torvalds's avatar
Linus Torvalds committed
567
	req_p->sense_buffer[0]=0;
568
	scsi_set_resid(req_p, 0);
569
	if (scmd_channel(req_p) > 1) {
Linus Torvalds's avatar
Linus Torvalds committed
570 571 572 573 574 575 576 577 578 579 580 581 582 583
		req_p->result = 0x00040000;
		done(req_p);
#ifdef ED_DBGP		
		printk("atp870u_queuecommand : req_p->device->channel > 1\n");	
#endif			
		return 0;
	}

	host = req_p->device->host;
	dev = (struct atp_unit *)&host->hostdata;
		

		
	m = 1;
584
	m = m << scmd_id(req_p);
Linus Torvalds's avatar
Linus Torvalds committed
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631

	/*
	 *      Fake a timeout for missing targets
	 */

	if ((m & dev->active_id[c]) == 0) {
		req_p->result = 0x00040000;
		done(req_p);
		return 0;
	}

	if (done) {
		req_p->scsi_done = done;
	} else {
#ifdef ED_DBGP		
		printk( "atp870u_queuecommand: done can't be NULL\n");
#endif		
		req_p->result = 0;
		done(req_p);
		return 0;
	}
	
	/*
	 *	Count new command
	 */
	dev->quend[c]++;
	if (dev->quend[c] >= qcnt) {
		dev->quend[c] = 0;
	}
	
	/*
	 *	Check queue state
	 */
	if (dev->quhd[c] == dev->quend[c]) {
		if (dev->quend[c] == 0) {
			dev->quend[c] = qcnt;
		}
#ifdef ED_DBGP		
		printk("atp870u_queuecommand : dev->quhd[c] == dev->quend[c]\n");
#endif		
		dev->quend[c]--;
		req_p->result = 0x00020000;
		done(req_p);	
		return 0;
	}
	dev->quereq[c][dev->quend[c]] = req_p;
#ifdef ED_DBGP	
632
	printk("dev->ioport[c] = %x atp_readb_io(dev, c, 0x1c) = %x dev->in_int[%d] = %d dev->in_snd[%d] = %d\n",dev->ioport[c],atp_readb_io(dev, c, 0x1c),c,dev->in_int[c],c,dev->in_snd[c]);
Linus Torvalds's avatar
Linus Torvalds committed
633
#endif
634
	if ((atp_readb_io(dev, c, 0x1c) == 0) && (dev->in_int[c] == 0) && (dev->in_snd[c] == 0)) {
Linus Torvalds's avatar
Linus Torvalds committed
635 636 637 638 639 640 641 642 643 644 645
#ifdef ED_DBGP
		printk("Call sent_s870(atp870u_queuecommand)\n");
#endif		
		send_s870(dev,c);
	}
#ifdef ED_DBGP	
	printk("atp870u_queuecommand : exit\n");
#endif	
	return 0;
}

Jeff Garzik's avatar
Jeff Garzik committed
646 647
static DEF_SCSI_QCMD(atp870u_queuecommand)

Linus Torvalds's avatar
Linus Torvalds committed
648 649 650 651 652 653 654 655 656 657 658
/**
 *	send_s870	-	send a command to the controller
 *	@host: host
 *
 *	On entry there is work queued to be done. We move some of that work to the
 *	controller itself. 
 *
 *	Caller holds the host lock.
 */
static void send_s870(struct atp_unit *dev,unsigned char c)
{
Ondrej Zary's avatar
Ondrej Zary committed
659
	struct scsi_cmnd *workreq = NULL;
Linus Torvalds's avatar
Linus Torvalds committed
660 661 662
	unsigned int i;//,k;
	unsigned char  j, target_id;
	unsigned char *prd;
Ondrej Zary's avatar
Ondrej Zary committed
663
	unsigned short int w;
Linus Torvalds's avatar
Linus Torvalds committed
664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
	unsigned long l, bttl = 0;
	unsigned long  sg_count;

	if (dev->in_snd[c] != 0) {
#ifdef ED_DBGP		
		printk("cmnd in_snd\n");
#endif
		return;
	}
#ifdef ED_DBGP
	printk("Sent_s870 enter\n");
#endif
	dev->in_snd[c] = 1;
	if ((dev->last_cmd[c] != 0xff) && ((dev->last_cmd[c] & 0x40) != 0)) {
		dev->last_cmd[c] &= 0x0f;
		workreq = dev->id[c][dev->last_cmd[c]].curr_req;
Ondrej Zary's avatar
Ondrej Zary committed
680 681 682 683 684 685
		if (!workreq) {
			dev->last_cmd[c] = 0xff;
			if (dev->quhd[c] == dev->quend[c]) {
				dev->in_snd[c] = 0;
				return;
			}
Linus Torvalds's avatar
Linus Torvalds committed
686 687
		}
	}
Ondrej Zary's avatar
Ondrej Zary committed
688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
	if (!workreq) {
		if ((dev->last_cmd[c] != 0xff) && (dev->working[c] != 0)) {
			dev->in_snd[c] = 0;
			return;
		}
		dev->working[c]++;
		j = dev->quhd[c];
		dev->quhd[c]++;
		if (dev->quhd[c] >= qcnt)
			dev->quhd[c] = 0;
		workreq = dev->quereq[c][dev->quhd[c]];
		if (dev->id[c][scmd_id(workreq)].curr_req != NULL) {
			dev->quhd[c] = j;
			dev->working[c]--;
			dev->in_snd[c] = 0;
			return;
		}
705 706
		dev->id[c][scmd_id(workreq)].curr_req = workreq;
		dev->last_cmd[c] = scmd_id(workreq);
Linus Torvalds's avatar
Linus Torvalds committed
707
	}
708
	if ((atp_readb_io(dev, c, 0x1f) & 0xb0) != 0 || atp_readb_io(dev, c, 0x1c) != 0) {
Linus Torvalds's avatar
Linus Torvalds committed
709
#ifdef ED_DBGP
Ondrej Zary's avatar
Ondrej Zary committed
710
		printk("Abort to Send\n");
Linus Torvalds's avatar
Linus Torvalds committed
711
#endif
Ondrej Zary's avatar
Ondrej Zary committed
712 713 714 715
		dev->last_cmd[c] |= 0x40;
		dev->in_snd[c] = 0;
		return;
	}
Linus Torvalds's avatar
Linus Torvalds committed
716 717
#ifdef ED_DBGP
	printk("OK to Send\n");
718
	scmd_printk(KERN_DEBUG, workreq, "CDB");
Linus Torvalds's avatar
Linus Torvalds committed
719 720 721
	for(i=0;i<workreq->cmd_len;i++) {
		printk(" %x",workreq->cmnd[i]);
	}
722
	printk("\n");
Linus Torvalds's avatar
Linus Torvalds committed
723
#endif	
724 725
	l = scsi_bufflen(workreq);

Linus Torvalds's avatar
Linus Torvalds committed
726
	if (dev->dev_id == ATP885_DEVID) {
727 728
		j = atp_readb_base(dev, 0x29) & 0xfe;
		atp_writeb_base(dev, 0x29, j);
729
		dev->r1f[c][scmd_id(workreq)] = 0;
Linus Torvalds's avatar
Linus Torvalds committed
730 731 732
	}
	
	if (workreq->cmnd[0] == READ_CAPACITY) {
733 734
		if (l > 8)
			l = 8;
Linus Torvalds's avatar
Linus Torvalds committed
735 736
	}
	if (workreq->cmnd[0] == 0x00) {
737
		l = 0;
Linus Torvalds's avatar
Linus Torvalds committed
738 739 740
	}

	j = 0;
741
	target_id = scmd_id(workreq);
Linus Torvalds's avatar
Linus Torvalds committed
742 743 744 745 746 747 748 749 750

	/*
	 *	Wide ?
	 */
	w = 1;
	w = w << target_id;
	if ((w & dev->wide_id[c]) != 0) {
		j |= 0x01;
	}
751 752 753
	atp_writeb_io(dev, c, 0x1b, j);
	while ((atp_readb_io(dev, c, 0x1b) & 0x01) != j) {
		atp_writeb_pci(dev, c, 0x1b, j);
Linus Torvalds's avatar
Linus Torvalds committed
754 755 756 757 758 759 760 761
#ifdef ED_DBGP
		printk("send_s870 while loop 1\n");
#endif
	}
	/*
	 *	Write the command
	 */

762 763 764 765 766 767 768 769 770
	atp_writeb_io(dev, c, 0x00, workreq->cmd_len);
	atp_writeb_io(dev, c, 0x01, 0x2c);
	if (dev->dev_id == ATP885_DEVID)
		atp_writeb_io(dev, c, 0x02, 0x7f);
	else
		atp_writeb_io(dev, c, 0x02, 0xcf);
	for (i = 0; i < workreq->cmd_len; i++)
		atp_writeb_io(dev, c, 0x03 + i, workreq->cmnd[i]);
	atp_writeb_io(dev, c, 0x0f, workreq->device->lun);
Linus Torvalds's avatar
Linus Torvalds committed
771 772 773
	/*
	 *	Write the target
	 */
774
	atp_writeb_io(dev, c, 0x11, dev->id[c][target_id].devsp);
Linus Torvalds's avatar
Linus Torvalds committed
775 776 777
#ifdef ED_DBGP	
	printk("dev->id[%d][%d].devsp = %2x\n",c,target_id,dev->id[c][target_id].devsp);
#endif
778 779

	sg_count = scsi_dma_map(workreq);
Linus Torvalds's avatar
Linus Torvalds committed
780 781 782
	/*
	 *	Write transfer size
	 */
783 784 785
	atp_writeb_io(dev, c, 0x12, ((unsigned char *) (&l))[2]);
	atp_writeb_io(dev, c, 0x13, ((unsigned char *) (&l))[1]);
	atp_writeb_io(dev, c, 0x14, ((unsigned char *) (&l))[0]);
Linus Torvalds's avatar
Linus Torvalds committed
786 787 788 789 790 791 792 793 794 795 796 797 798 799 800
	j = target_id;	
	dev->id[c][j].last_len = l;
	dev->id[c][j].tran_len = 0;
#ifdef ED_DBGP	
	printk("dev->id[%2d][%2d].last_len = %d\n",c,j,dev->id[c][j].last_len);
#endif	
	/*
	 *	Flip the wide bits
	 */
	if ((j & 0x08) != 0) {
		j = (j & 0x07) | 0x40;
	}
	/*
	 *	Check transfer direction
	 */
801 802 803 804 805 806
	if (workreq->sc_data_direction == DMA_TO_DEVICE)
		atp_writeb_io(dev, c, 0x15, j | 0x20);
	else
		atp_writeb_io(dev, c, 0x15, j);
	atp_writeb_io(dev, c, 0x16, atp_readb_io(dev, c, 0x16) | 0x80);
	atp_writeb_io(dev, c, 0x16, 0x80);
Linus Torvalds's avatar
Linus Torvalds committed
807 808
	dev->id[c][target_id].dirct = 0;
	if (l == 0) {
809
		if (atp_readb_io(dev, c, 0x1c) == 0) {
Linus Torvalds's avatar
Linus Torvalds committed
810 811 812
#ifdef ED_DBGP
			printk("change SCSI_CMD_REG 0x08\n");	
#endif				
813 814
			atp_writeb_io(dev, c, 0x18, 0x08);
		} else
Linus Torvalds's avatar
Linus Torvalds committed
815 816 817 818 819 820 821 822 823 824 825 826
			dev->last_cmd[c] |= 0x40;
		dev->in_snd[c] = 0;
		return;
	}
	prd = dev->id[c][target_id].prd_table;
	dev->id[c][target_id].prd_pos = prd;

	/*
	 *	Now write the request list. Either as scatter/gather or as
	 *	a linear chain.
	 */

827 828
	if (l) {
		struct scatterlist *sgpnt;
Linus Torvalds's avatar
Linus Torvalds committed
829
		i = 0;
830 831 832
		scsi_for_each_sg(workreq, sgpnt, sg_count, j) {
			bttl = sg_dma_address(sgpnt);
			l=sg_dma_len(sgpnt);
Linus Torvalds's avatar
Linus Torvalds committed
833
#ifdef ED_DBGP		
834
			printk("1. bttl %x, l %x\n",bttl, l);
Linus Torvalds's avatar
Linus Torvalds committed
835
#endif			
836
			while (l > 0x10000) {
Linus Torvalds's avatar
Linus Torvalds committed
837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855
				(((u16 *) (prd))[i + 3]) = 0x0000;
				(((u16 *) (prd))[i + 2]) = 0x0000;
				(((u32 *) (prd))[i >> 1]) = cpu_to_le32(bttl);
				l -= 0x10000;
				bttl += 0x10000;
				i += 0x04;
			}
			(((u32 *) (prd))[i >> 1]) = cpu_to_le32(bttl);
			(((u16 *) (prd))[i + 2]) = cpu_to_le16(l);
			(((u16 *) (prd))[i + 3]) = 0;
			i += 0x04;			
		}
		(((u16 *) (prd))[i - 1]) = cpu_to_le16(0x8000);	
#ifdef ED_DBGP		
		printk("prd %4x %4x %4x %4x\n",(((unsigned short int *)prd)[0]),(((unsigned short int *)prd)[1]),(((unsigned short int *)prd)[2]),(((unsigned short int *)prd)[3]));
		printk("2. bttl %x, l %x\n",bttl, l);
#endif			
	}
#ifdef ED_DBGP		
Ondrej Zary's avatar
Ondrej Zary committed
856
	printk("send_s870: prdaddr_2 0x%8x target_id %d\n", dev->id[c][target_id].prdaddr,target_id);
Linus Torvalds's avatar
Linus Torvalds committed
857
#endif	
858
	dev->id[c][target_id].prdaddr = dev->id[c][target_id].prd_bus;
859 860 861
	atp_writel_pci(dev, c, 4, dev->id[c][target_id].prdaddr);
	atp_writeb_pci(dev, c, 2, 0x06);
	atp_writeb_pci(dev, c, 2, 0x00);
Linus Torvalds's avatar
Linus Torvalds committed
862
	if (dev->dev_id == ATP885_DEVID) {
863
		j = atp_readb_pci(dev, c, 1) & 0xf3;
Linus Torvalds's avatar
Linus Torvalds committed
864 865 866 867
		if ((workreq->cmnd[0] == 0x08) || (workreq->cmnd[0] == 0x28) ||
	    	(workreq->cmnd[0] == 0x0a) || (workreq->cmnd[0] == 0x2a)) {
	   		j |= 0x0c;
		}
868
		atp_writeb_pci(dev, c, 1, j);
Linus Torvalds's avatar
Linus Torvalds committed
869 870
	} else if ((dev->dev_id == ATP880_DEVID1) ||
	    	   (dev->dev_id == ATP880_DEVID2)) {
871 872 873 874
		if ((workreq->cmnd[0] == 0x08) || (workreq->cmnd[0] == 0x28) || (workreq->cmnd[0] == 0x0a) || (workreq->cmnd[0] == 0x2a))
			atp_writeb_base(dev, 0x3b, (atp_readb_base(dev, 0x3b) & 0x3f) | 0xc0);
		else
			atp_writeb_base(dev, 0x3b, atp_readb_base(dev, 0x3b) & 0x3f);
Linus Torvalds's avatar
Linus Torvalds committed
875
	} else {		
876 877 878 879
		if ((workreq->cmnd[0] == 0x08) || (workreq->cmnd[0] == 0x28) || (workreq->cmnd[0] == 0x0a) || (workreq->cmnd[0] == 0x2a))
			atp_writeb_io(dev, c, 0x3a, (atp_readb_io(dev, c, 0x3a) & 0xf3) | 0x08);
		else
			atp_writeb_io(dev, c, 0x3a, atp_readb_io(dev, c, 0x3a) & 0xf3);
Linus Torvalds's avatar
Linus Torvalds committed
880 881 882 883
	}	

	if(workreq->sc_data_direction == DMA_TO_DEVICE) {
		dev->id[c][target_id].dirct = 0x20;
884 885 886
		if (atp_readb_io(dev, c, 0x1c) == 0) {
			atp_writeb_io(dev, c, 0x18, 0x08);
			atp_writeb_pci(dev, c, 0, 0x01);
Linus Torvalds's avatar
Linus Torvalds committed
887 888 889 890 891 892 893 894 895
#ifdef ED_DBGP		
		printk( "start DMA(to target)\n");
#endif				
		} else {
			dev->last_cmd[c] |= 0x40;
		}
		dev->in_snd[c] = 0;
		return;
	}
896 897 898
	if (atp_readb_io(dev, c, 0x1c) == 0) {
		atp_writeb_io(dev, c, 0x18, 0x08);
		atp_writeb_pci(dev, c, 0, 0x09);
Linus Torvalds's avatar
Linus Torvalds committed
899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914
#ifdef ED_DBGP		
		printk( "start DMA(to host)\n");
#endif			
	} else {
		dev->last_cmd[c] |= 0x40;
	}
	dev->in_snd[c] = 0;
	return;

}

static unsigned char fun_scam(struct atp_unit *dev, unsigned short int *val)
{
	unsigned short int i, k;
	unsigned char j;

915
	atp_writew_io(dev, 0, 0x1c, *val);
Linus Torvalds's avatar
Linus Torvalds committed
916
	for (i = 0; i < 10; i++) {	/* stable >= bus settle delay(400 ns)  */
917
		k = atp_readw_io(dev, 0, 0x1c);
Linus Torvalds's avatar
Linus Torvalds committed
918
		j = (unsigned char) (k >> 8);
Ondrej Zary's avatar
Ondrej Zary committed
919 920
		if ((k & 0x8000) != 0)	/* DB7 all release?    */
			i = 0;
Linus Torvalds's avatar
Linus Torvalds committed
921 922
	}
	*val |= 0x4000;		/* assert DB6           */
923
	atp_writew_io(dev, 0, 0x1c, *val);
Linus Torvalds's avatar
Linus Torvalds committed
924
	*val &= 0xdfff;		/* assert DB5           */
925
	atp_writew_io(dev, 0, 0x1c, *val);
Linus Torvalds's avatar
Linus Torvalds committed
926
	for (i = 0; i < 10; i++) {	/* stable >= bus settle delay(400 ns) */
927
		if ((atp_readw_io(dev, 0, 0x1c) & 0x2000) != 0)	/* DB5 all release?       */
Ondrej Zary's avatar
Ondrej Zary committed
928
			i = 0;
Linus Torvalds's avatar
Linus Torvalds committed
929 930 931
	}
	*val |= 0x8000;		/* no DB4-0, assert DB7    */
	*val &= 0xe0ff;
932
	atp_writew_io(dev, 0, 0x1c, *val);
Linus Torvalds's avatar
Linus Torvalds committed
933
	*val &= 0xbfff;		/* release DB6             */
934
	atp_writew_io(dev, 0, 0x1c, *val);
Linus Torvalds's avatar
Linus Torvalds committed
935
	for (i = 0; i < 10; i++) {	/* stable >= bus settle delay(400 ns)  */
936
		if ((atp_readw_io(dev, 0, 0x1c) & 0x4000) != 0)	/* DB6 all release?  */
Ondrej Zary's avatar
Ondrej Zary committed
937
			i = 0;
Linus Torvalds's avatar
Linus Torvalds committed
938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961
	}

	return j;
}

static void tscam(struct Scsi_Host *host)
{

	unsigned char i, j, k;
	unsigned long n;
	unsigned short int m, assignid_map, val;
	unsigned char mbuf[33], quintet[2];
	struct atp_unit *dev = (struct atp_unit *)&host->hostdata;
	static unsigned char g2q_tab[8] = {
		0x38, 0x31, 0x32, 0x2b, 0x34, 0x2d, 0x2e, 0x27
	};

/*  I can't believe we need this before we've even done anything.  Remove it
 *  and see if anyone bitches.
	for (i = 0; i < 0x10; i++) {
		udelay(0xffff);
	}
 */

962 963 964
	atp_writeb_io(dev, 0, 1, 0x08);
	atp_writeb_io(dev, 0, 2, 0x7f);
	atp_writeb_io(dev, 0, 0x11, 0x20);
Linus Torvalds's avatar
Linus Torvalds committed
965 966 967 968 969 970 971 972 973 974 975 976

	if ((dev->scam_on & 0x40) == 0) {
		return;
	}
	m = 1;
	m <<= dev->host_id[0];
	j = 16;
	if (dev->chip_ver < 4) {
		m |= 0xff00;
		j = 8;
	}
	assignid_map = m;
977 978 979 980 981 982 983
	atp_writeb_io(dev, 0, 0x02, 0x02);	/* 2*2=4ms,3EH 2/32*3E=3.9ms */
	atp_writeb_io(dev, 0, 0x03, 0);
	atp_writeb_io(dev, 0, 0x04, 0);
	atp_writeb_io(dev, 0, 0x05, 0);
	atp_writeb_io(dev, 0, 0x06, 0);
	atp_writeb_io(dev, 0, 0x07, 0);
	atp_writeb_io(dev, 0, 0x08, 0);
Linus Torvalds's avatar
Linus Torvalds committed
984 985 986 987 988 989 990

	for (i = 0; i < j; i++) {
		m = 1;
		m = m << i;
		if ((m & assignid_map) != 0) {
			continue;
		}
991 992 993 994
		atp_writeb_io(dev, 0, 0x0f, 0);
		atp_writeb_io(dev, 0, 0x12, 0);
		atp_writeb_io(dev, 0, 0x13, 0);
		atp_writeb_io(dev, 0, 0x14, 0);
Linus Torvalds's avatar
Linus Torvalds committed
995 996 997 998 999
		if (i > 7) {
			k = (i & 0x07) | 0x40;
		} else {
			k = i;
		}
1000 1001 1002 1003 1004
		atp_writeb_io(dev, 0, 0x15, k);
		if (dev->chip_ver == 4)
			atp_writeb_io(dev, 0, 0x1b, 0x01);
		else
			atp_writeb_io(dev, 0, 0x1b, 0x00);
Ondrej Zary's avatar
Ondrej Zary committed
1005
		do {
1006
			atp_writeb_io(dev, 0, 0x18, 0x09);
Linus Torvalds's avatar
Linus Torvalds committed
1007

1008
			while ((atp_readb_io(dev, 0, 0x1f) & 0x80) == 0x00)
Ondrej Zary's avatar
Ondrej Zary committed
1009
				cpu_relax();
1010
			k = atp_readb_io(dev, 0, 0x17);
Ondrej Zary's avatar
Ondrej Zary committed
1011 1012 1013
			if ((k == 0x85) || (k == 0x42))
				break;
			if (k != 0x16)
1014
				atp_writeb_io(dev, 0, 0x10, 0x41);
Ondrej Zary's avatar
Ondrej Zary committed
1015 1016 1017
		} while (k != 0x16);
		if ((k == 0x85) || (k == 0x42))
			continue;
Linus Torvalds's avatar
Linus Torvalds committed
1018 1019 1020
		assignid_map |= m;

	}
1021 1022
	atp_writeb_io(dev, 0, 0x02, 0x7f);
	atp_writeb_io(dev, 0, 0x1b, 0x02);
Linus Torvalds's avatar
Linus Torvalds committed
1023 1024 1025 1026

	outb(0, 0x80);

	val = 0x0080;		/* bsy  */
1027
	atp_writew_io(dev, 0, 0x1c, val);
Linus Torvalds's avatar
Linus Torvalds committed
1028
	val |= 0x0040;		/* sel  */
1029
	atp_writew_io(dev, 0, 0x1c, val);
Linus Torvalds's avatar
Linus Torvalds committed
1030
	val |= 0x0004;		/* msg  */
1031
	atp_writew_io(dev, 0, 0x1c, val);
Linus Torvalds's avatar
Linus Torvalds committed
1032 1033
	inb(0x80);		/* 2 deskew delay(45ns*2=90ns) */
	val &= 0x007f;		/* no bsy  */
1034
	atp_writew_io(dev, 0, 0x1c, val);
Linus Torvalds's avatar
Linus Torvalds committed
1035 1036
	mdelay(128);
	val &= 0x00fb;		/* after 1ms no msg */
1037 1038
	atp_writew_io(dev, 0, 0x1c, val);
	while ((atp_readb_io(dev, 0, 0x1c) & 0x04) != 0)
Ondrej Zary's avatar
Ondrej Zary committed
1039
		;
Linus Torvalds's avatar
Linus Torvalds committed
1040 1041
	outb(1, 0x80);
	udelay(100);
Ondrej Zary's avatar
Ondrej Zary committed
1042
	for (n = 0; n < 0x30000; n++)
1043
		if ((atp_readb_io(dev, 0, 0x1c) & 0x80) != 0)	/* bsy ? */
Ondrej Zary's avatar
Ondrej Zary committed
1044 1045 1046
			break;
	if (n < 0x30000)
		for (n = 0; n < 0x30000; n++)
1047
			if ((atp_readb_io(dev, 0, 0x1c) & 0x81) == 0x0081) {
Ondrej Zary's avatar
Ondrej Zary committed
1048 1049
				inb(0x80);
				val |= 0x8003;		/* io,cd,db7  */
1050
				atp_writew_io(dev, 0, 0x1c, val);
Ondrej Zary's avatar
Ondrej Zary committed
1051 1052
				inb(0x80);
				val &= 0x00bf;		/* no sel     */
1053
				atp_writew_io(dev, 0, 0x1c, val);
Ondrej Zary's avatar
Ondrej Zary committed
1054 1055 1056 1057
				outb(2, 0x80);
				break;
			}
	while (1) {
1058 1059 1060 1061 1062 1063 1064 1065 1066 1067
	/*
	 * The funny division into multiple delays is to accomodate
	 * arches like ARM where udelay() multiplies its argument by
	 * a large number to initialize a loop counter.  To avoid
	 * overflow, the maximum supported udelay is 2000 microseconds.
	 *
	 * XXX it would be more polite to find a way to use msleep()
	 */
	mdelay(2);
	udelay(48);
1068 1069 1070 1071 1072 1073
	if ((atp_readb_io(dev, 0, 0x1c) & 0x80) == 0x00) {	/* bsy ? */
		atp_writew_io(dev, 0, 0x1c, 0);
		atp_writeb_io(dev, 0, 0x1b, 0);
		atp_writeb_io(dev, 0, 0x15, 0);
		atp_writeb_io(dev, 0, 0x18, 0x09);
		while ((atp_readb_io(dev, 0, 0x1f) & 0x80) == 0)
Linus Torvalds's avatar
Linus Torvalds committed
1074
			cpu_relax();
1075
		atp_readb_io(dev, 0, 0x17);
Linus Torvalds's avatar
Linus Torvalds committed
1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087
		return;
	}
	val &= 0x00ff;		/* synchronization  */
	val |= 0x3f00;
	fun_scam(dev, &val);
	outb(3, 0x80);
	val &= 0x00ff;		/* isolation        */
	val |= 0x2000;
	fun_scam(dev, &val);
	outb(4, 0x80);
	i = 8;
	j = 0;
Ondrej Zary's avatar
Ondrej Zary committed
1088 1089

	while (1) {
1090
		if ((atp_readw_io(dev, 0, 0x1c) & 0x2000) == 0)
Ondrej Zary's avatar
Ondrej Zary committed
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106
			continue;
		outb(5, 0x80);
		val &= 0x00ff;		/* get ID_STRING */
		val |= 0x2000;
		k = fun_scam(dev, &val);
		if ((k & 0x03) == 0)
			break;
		mbuf[j] <<= 0x01;
		mbuf[j] &= 0xfe;
		if ((k & 0x02) != 0)
			mbuf[j] |= 0x01;
		i--;
		if (i > 0)
			continue;
		j++;
		i = 8;
Linus Torvalds's avatar
Linus Torvalds committed
1107 1108
	}

Ondrej Zary's avatar
Ondrej Zary committed
1109
	/* isolation complete..  */
Linus Torvalds's avatar
Linus Torvalds committed
1110 1111 1112 1113
/*    mbuf[32]=0;
	printk(" \n%x %x %x %s\n ",assignid_map,mbuf[0],mbuf[1],&mbuf[2]); */
	i = 15;
	j = mbuf[0];
Lucas De Marchi's avatar
Lucas De Marchi committed
1114
	if ((j & 0x20) != 0) {	/* bit5=1:ID up to 7      */
Linus Torvalds's avatar
Linus Torvalds committed
1115 1116
		i = 7;
	}
Ondrej Zary's avatar
Ondrej Zary committed
1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128
	if ((j & 0x06) != 0) {	/* IDvalid?             */
		k = mbuf[1];
		while (1) {
			m = 1;
			m <<= k;
			if ((m & assignid_map) == 0)
				break;
			if (k > 0)
				k--;
			else
				break;
		}
Linus Torvalds's avatar
Linus Torvalds committed
1129
	}
Ondrej Zary's avatar
Ondrej Zary committed
1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141
	if ((m & assignid_map) != 0) {	/* srch from max acceptable ID#  */
		k = i;			/* max acceptable ID#            */
		while (1) {
			m = 1;
			m <<= k;
			if ((m & assignid_map) == 0)
				break;
			if (k > 0)
				k--;
			else
				break;
		}
Linus Torvalds's avatar
Linus Torvalds committed
1142
	}
Ondrej Zary's avatar
Ondrej Zary committed
1143
	/* k=binID#,       */
Linus Torvalds's avatar
Linus Torvalds committed
1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161
	assignid_map |= m;
	if (k < 8) {
		quintet[0] = 0x38;	/* 1st dft ID<8    */
	} else {
		quintet[0] = 0x31;	/* 1st  ID>=8      */
	}
	k &= 0x07;
	quintet[1] = g2q_tab[k];

	val &= 0x00ff;		/* AssignID 1stQuintet,AH=001xxxxx  */
	m = quintet[0] << 8;
	val |= m;
	fun_scam(dev, &val);
	val &= 0x00ff;		/* AssignID 2ndQuintet,AH=001xxxxx */
	m = quintet[1] << 8;
	val |= m;
	fun_scam(dev, &val);

Ondrej Zary's avatar
Ondrej Zary committed
1162
	}
Linus Torvalds's avatar
Linus Torvalds committed
1163 1164
}

1165
static void is870(struct atp_unit *dev, unsigned char c, bool wide_chip)
Linus Torvalds's avatar
Linus Torvalds committed
1166 1167 1168 1169 1170 1171 1172 1173 1174 1175
{
	unsigned char i, j, k, rmb, n;
	unsigned short int m;
	static unsigned char mbuf[512];
	static unsigned char satn[9] = { 0, 0, 0, 0, 0, 0, 0, 6, 6 };
	static unsigned char inqd[9] = { 0x12, 0, 0, 0, 0x24, 0, 0, 0x24, 6 };
	static unsigned char synn[6] = { 0x80, 1, 3, 1, 0x19, 0x0e };
	static unsigned char synu[6] = { 0x80, 1, 3, 1, 0x0c, 0x0e };
	static unsigned char synw[6] = { 0x80, 1, 3, 1, 0x0c, 0x07 };
	static unsigned char wide[6] = { 0x80, 1, 2, 3, 1, 0 };
1176

1177
	atp_writeb_io(dev, c, 0x3a, atp_readb_io(dev, c, 0x3a) | 0x10);
Linus Torvalds's avatar
Linus Torvalds committed
1178 1179

	for (i = 0; i < 16; i++) {
1180
		if (!wide_chip && (i > 7))
Linus Torvalds's avatar
Linus Torvalds committed
1181 1182 1183
			break;
		m = 1;
		m = m << i;
1184
		if ((m & dev->active_id[c]) != 0) {
Linus Torvalds's avatar
Linus Torvalds committed
1185 1186
			continue;
		}
1187 1188
		if (i == dev->host_id[c]) {
			printk(KERN_INFO "         ID: %2d  Host Adapter\n", dev->host_id[c]);
Linus Torvalds's avatar
Linus Torvalds committed
1189 1190
			continue;
		}
1191
		atp_writeb_io(dev, c, 0x1b, wide_chip ? 0x01 : 0x00);
1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204
		atp_writeb_io(dev, c, 1, 0x08);
		atp_writeb_io(dev, c, 2, 0x7f);
		atp_writeb_io(dev, c, 3, satn[0]);
		atp_writeb_io(dev, c, 4, satn[1]);
		atp_writeb_io(dev, c, 5, satn[2]);
		atp_writeb_io(dev, c, 6, satn[3]);
		atp_writeb_io(dev, c, 7, satn[4]);
		atp_writeb_io(dev, c, 8, satn[5]);
		atp_writeb_io(dev, c, 0x0f, 0);
		atp_writeb_io(dev, c, 0x11, dev->id[c][i].devsp);
		atp_writeb_io(dev, c, 0x12, 0);
		atp_writeb_io(dev, c, 0x13, satn[6]);
		atp_writeb_io(dev, c, 0x14, satn[7]);
Linus Torvalds's avatar
Linus Torvalds committed
1205 1206 1207 1208
		j = i;
		if ((j & 0x08) != 0) {
			j = (j & 0x07) | 0x40;
		}
1209 1210
		atp_writeb_io(dev, c, 0x15, j);
		atp_writeb_io(dev, c, 0x18, satn[8]);
Linus Torvalds's avatar
Linus Torvalds committed
1211

1212
		while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00)
Linus Torvalds's avatar
Linus Torvalds committed
1213 1214
			cpu_relax();

1215
		if (atp_readb_io(dev, c, 0x17) != 0x11 && atp_readb_io(dev, c, 0x17) != 0x8e)
Linus Torvalds's avatar
Linus Torvalds committed
1216 1217
			continue;

1218
		while (atp_readb_io(dev, c, 0x17) != 0x8e)
Linus Torvalds's avatar
Linus Torvalds committed
1219 1220
			cpu_relax();