atp870u.c 59.0 KB
Newer Older
L
Linus Torvalds 已提交
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
L
Linus Torvalds 已提交
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>
L
Linus Torvalds 已提交
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);
O
Ondrej Zary 已提交
44
static void atp_is(struct atp_unit *dev, unsigned char c, bool wide_chip, unsigned char lvdmode);
L
Linus Torvalds 已提交
45

46 47 48 49 50
static inline void atp_writeb_base(struct atp_unit *atp, u8 reg, u8 val)
{
	outb(val, atp->baseport + reg);
}

51 52 53 54 55
static inline void atp_writew_base(struct atp_unit *atp, u8 reg, u16 val)
{
	outw(val, atp->baseport + reg);
}

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
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);
}

81 82 83 84 85 86 87 88 89 90
static inline u16 atp_readw_base(struct atp_unit *atp, u8 reg)
{
	return inw(atp->baseport + reg);
}

static inline u32 atp_readl_base(struct atp_unit *atp, u8 reg)
{
	return inl(atp->baseport + reg);
}

91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
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);
}

106 107 108 109 110 111 112 113 114 115 116
static inline bool is880(struct atp_unit *atp)
{
	return atp->pdev->device == ATP880_DEVID1 ||
	       atp->pdev->device == ATP880_DEVID2;
}

static inline bool is885(struct atp_unit *atp)
{
	return atp->pdev->device == ATP885_DEVID;
}

117
static irqreturn_t atp870u_intr_handle(int irq, void *dev_id)
L
Linus Torvalds 已提交
118 119
{
	unsigned long flags;
O
Ondrej Zary 已提交
120
	unsigned short int id;
L
Linus Torvalds 已提交
121 122 123 124 125 126 127 128 129 130 131
	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++) {
132
		j = atp_readb_io(dev, c, 0x1f);
L
Linus Torvalds 已提交
133
		if ((j & 0x80) != 0)
O
Ondrej Zary 已提交
134
			break;
L
Linus Torvalds 已提交
135 136
		dev->in_int[c] = 0;
	}
O
Ondrej Zary 已提交
137 138
	if ((j & 0x80) == 0)
		return IRQ_NONE;
L
Linus Torvalds 已提交
139 140 141 142
#ifdef ED_DBGP	
	printk("atp870u_intr_handle enter\n");
#endif	
	dev->in_int[c] = 1;
143
	cmdp = atp_readb_io(dev, c, 0x10);
L
Linus Torvalds 已提交
144
	if (dev->working[c] != 0) {
145
		if (is885(dev)) {
146 147
			if ((atp_readb_io(dev, c, 0x16) & 0x80) == 0)
				atp_writeb_io(dev, c, 0x16, (atp_readb_io(dev, c, 0x16) | 0x80));
L
Linus Torvalds 已提交
148
		}		
149
		if ((atp_readb_pci(dev, c, 0x00) & 0x08) != 0)
L
Linus Torvalds 已提交
150 151
		{
			for (k=0; k < 1000; k++) {
152
				if ((atp_readb_pci(dev, c, 2) & 0x08) == 0)
O
Ondrej Zary 已提交
153
					break;
154
				if ((atp_readb_pci(dev, c, 2) & 0x01) == 0)
O
Ondrej Zary 已提交
155
					break;
L
Linus Torvalds 已提交
156 157
			}
		}
158
		atp_writeb_pci(dev, c, 0, 0x00);
L
Linus Torvalds 已提交
159
		
160
		i = atp_readb_io(dev, c, 0x17);
L
Linus Torvalds 已提交
161
		
162
		if (is885(dev))
163
			atp_writeb_pci(dev, c, 2, 0x06);
L
Linus Torvalds 已提交
164

165
		target_id = atp_readb_io(dev, c, 0x15);
L
Linus Torvalds 已提交
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182

		/*
		 *	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;
		}
183
		if (is885(dev))
L
Linus Torvalds 已提交
184 185 186 187 188 189 190 191
			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;
			}
192
			if (is885(dev)) {
L
Linus Torvalds 已提交
193
				adrcnt = 0;
194 195 196
				((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);
L
Linus Torvalds 已提交
197 198 199 200 201 202 203 204
				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
O
Ondrej Zary 已提交
205
				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);
L
Linus Torvalds 已提交
206 207 208 209 210 211 212
#endif		
			}

			/*
			 *      Flip wide
			 */			
			if (dev->wide_id[c] != 0) {
213 214 215
				atp_writeb_io(dev, c, 0x1b, 0x01);
				while ((atp_readb_io(dev, c, 0x1b) & 0x01) != 0x01)
					atp_writeb_io(dev, c, 0x1b, 0x01);
L
Linus Torvalds 已提交
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
			}		
			/*
			 *	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				
O
Ondrej Zary 已提交
236
			return IRQ_HANDLED;
L
Linus Torvalds 已提交
237 238 239 240 241
		}

		if (i == 0x40) {
		     dev->last_cmd[c] |= 0x40;
		     dev->in_int[c] = 0;
O
Ondrej Zary 已提交
242
		     return IRQ_HANDLED;
L
Linus Torvalds 已提交
243 244 245 246 247 248 249
		}

		if (i == 0x21) {
			if ((dev->last_cmd[c] & 0xf0) != 0x40) {
			   dev->last_cmd[c] = 0xff;
			}
			adrcnt = 0;
250 251 252
			((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);
L
Linus Torvalds 已提交
253 254 255 256
			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;
257 258
			atp_writeb_io(dev, c, 0x10, 0x41);
			atp_writeb_io(dev, c, 0x18, 0x08);
L
Linus Torvalds 已提交
259
			dev->in_int[c] = 0;
O
Ondrej Zary 已提交
260
			return IRQ_HANDLED;
L
Linus Torvalds 已提交
261 262
		}

263
		if (is885(dev)) {
L
Linus Torvalds 已提交
264 265 266 267 268 269 270 271 272 273 274 275 276
			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;
277 278 279
			if (cmdp == 0x44 || i == 0x80)
				lun = atp_readb_io(dev, c, 0x1d) & 0x07;
			else {
L
Linus Torvalds 已提交
280 281 282 283 284 285 286 287
				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;
288 289 290
					((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);
L
Linus Torvalds 已提交
291 292 293 294
					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;
295
					atp_writeb_io(dev, c, 0x18, 0x08);
L
Linus Torvalds 已提交
296
					dev->in_int[c] = 0;
O
Ondrej Zary 已提交
297
					return IRQ_HANDLED;
L
Linus Torvalds 已提交
298 299 300 301
				} else {
#ifdef ED_DBGP
					printk("cmdp != 0x41\n");
#endif						
302
					atp_writeb_io(dev, c, 0x10, 0x46);
L
Linus Torvalds 已提交
303
					dev->id[c][target_id].dirct = 0x00;
304 305 306 307
					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);
L
Linus Torvalds 已提交
308
					dev->in_int[c] = 0;
O
Ondrej Zary 已提交
309
					return IRQ_HANDLED;
L
Linus Torvalds 已提交
310 311 312 313 314
				}
			}
			if (dev->last_cmd[c] != 0xff) {
			   dev->last_cmd[c] |= 0x40;
			}
315
			if (is885(dev)) {
316 317
				j = atp_readb_base(dev, 0x29) & 0xfe;
				atp_writeb_base(dev, 0x29, j);
O
Ondrej Zary 已提交
318
			} else
319
				atp_writeb_io(dev, c, 0x10, 0x45);
O
Ondrej Zary 已提交
320

321
			target_id = atp_readb_io(dev, c, 0x16);
L
Linus Torvalds 已提交
322 323 324 325 326 327 328 329
			/*
			 *	Remap wide identifiers
			 */
			if ((target_id & 0x10) != 0) {
				target_id = (target_id & 0x07) | 0x08;
			} else {
				target_id &= 0x07;
			}
330
			if (is885(dev))
331
				atp_writeb_io(dev, c, 0x10, 0x45);
L
Linus Torvalds 已提交
332 333
			workreq = dev->id[c][target_id].curr_req;
#ifdef ED_DBGP			
J
Jeff Garzik 已提交
334 335
			scmd_printk(KERN_DEBUG, workreq, "CDB");
			for (l = 0; l < workreq->cmd_len; l++)
L
Linus Torvalds 已提交
336
				printk(KERN_DEBUG " %x",workreq->cmnd[l]);
J
Jeff Garzik 已提交
337
			printk("\n");
L
Linus Torvalds 已提交
338 339
#endif	
			
340 341
			atp_writeb_io(dev, c, 0x0f, lun);
			atp_writeb_io(dev, c, 0x11, dev->id[c][target_id].devsp);
L
Linus Torvalds 已提交
342 343 344
			adrcnt = dev->id[c][target_id].tran_len;
			k = dev->id[c][target_id].last_len;

345 346 347
			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]);
L
Linus Torvalds 已提交
348
#ifdef ED_DBGP			
349
			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));
L
Linus Torvalds 已提交
350 351 352 353 354 355 356 357
#endif			
			/* Remap wide */
			j = target_id;
			if (target_id > 7) {
				j = (j & 0x07) | 0x40;
			}
			/* Add direction */
			j |= dev->id[c][target_id].dirct;
358 359
			atp_writeb_io(dev, c, 0x15, j);
			atp_writeb_io(dev, c, 0x16, 0x80);
L
Linus Torvalds 已提交
360 361
			
			/* enable 32 bit fifo transfer */	
362
			if (is885(dev)) {
363
				i = atp_readb_pci(dev, c, 1) & 0xf3;
L
Linus Torvalds 已提交
364 365 366 367
				//j=workreq->cmnd[0];	    		    	
				if ((workreq->cmnd[0] == 0x08) || (workreq->cmnd[0] == 0x28) || (workreq->cmnd[0] == 0x0a) || (workreq->cmnd[0] == 0x2a)) {
				   i |= 0x0c;
				}
368
				atp_writeb_pci(dev, c, 1, i);
369
			} else if (is880(dev)) {
370 371 372 373
				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);
L
Linus Torvalds 已提交
374
			} else {				
375
				if ((workreq->cmnd[0] == 0x08) || (workreq->cmnd[0] == 0x28) || (workreq->cmnd[0] == 0x0a) || (workreq->cmnd[0] == 0x2a))
376
					atp_writeb_base(dev, 0x3a, (atp_readb_base(dev, 0x3a) & 0xf3) | 0x08);
377
				else
378
					atp_writeb_base(dev, 0x3a, atp_readb_base(dev, 0x3a) & 0xf3);
L
Linus Torvalds 已提交
379 380 381 382 383 384 385 386 387 388
			}	
			j = 0;
			id = 1;
			id = id << target_id;
			/*
			 *	Is this a wide device
			 */
			if ((id & dev->wide_id[c]) != 0) {
				j |= 0x01;
			}
389 390 391
			atp_writeb_io(dev, c, 0x1b, j);
			while ((atp_readb_io(dev, c, 0x1b) & 0x01) != j)
				atp_writeb_io(dev, c, 0x1b, j);
L
Linus Torvalds 已提交
392
			if (dev->id[c][target_id].last_len == 0) {
393
				atp_writeb_io(dev, c, 0x18, 0x08);
L
Linus Torvalds 已提交
394 395 396 397
				dev->in_int[c] = 0;
#ifdef ED_DBGP
				printk("dev->id[c][target_id].last_len = 0\n");
#endif					
O
Ondrej Zary 已提交
398
				return IRQ_HANDLED;
L
Linus Torvalds 已提交
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425
			}
#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;
					}
				}				
			}
426
			atp_writel_pci(dev, c, 0x04, dev->id[c][target_id].prdaddr);
L
Linus Torvalds 已提交
427 428 429
#ifdef ED_DBGP
			printk("dev->id[%d][%d].prdaddr 0x%8x\n", c, target_id, dev->id[c][target_id].prdaddr);
#endif
430
			if (!is885(dev)) {
431 432
				atp_writeb_pci(dev, c, 2, 0x06);
				atp_writeb_pci(dev, c, 2, 0x00);
L
Linus Torvalds 已提交
433 434 435 436 437
			}
			/*
			 *	Check transfer direction
			 */
			if (dev->id[c][target_id].dirct != 0) {
438 439
				atp_writeb_io(dev, c, 0x18, 0x08);
				atp_writeb_pci(dev, c, 0, 0x01);
L
Linus Torvalds 已提交
440 441 442 443
				dev->in_int[c] = 0;
#ifdef ED_DBGP
				printk("status 0x80 return dirct != 0\n");
#endif				
O
Ondrej Zary 已提交
444
				return IRQ_HANDLED;
L
Linus Torvalds 已提交
445
			}
446 447
			atp_writeb_io(dev, c, 0x18, 0x08);
			atp_writeb_pci(dev, c, 0, 0x09);
L
Linus Torvalds 已提交
448 449 450 451
			dev->in_int[c] = 0;
#ifdef ED_DBGP
			printk("status 0x80 return dirct = 0\n");
#endif			
O
Ondrej Zary 已提交
452
			return IRQ_HANDLED;
L
Linus Torvalds 已提交
453 454 455 456 457 458 459 460
		}

		/*
		 *	Current scsi request on this target
		 */

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

O
Ondrej Zary 已提交
461
		if (i == 0x42 || i == 0x16) {
L
Linus Torvalds 已提交
462 463 464
			if ((dev->last_cmd[c] & 0xf0) != 0x40) {
			   dev->last_cmd[c] = 0xff;
			}
O
Ondrej Zary 已提交
465
			if (i == 0x16) {
466
				workreq->result = atp_readb_io(dev, c, 0x0f);
467
				if (((dev->r1f[c][target_id] & 0x10) != 0) && is885(dev)) {
O
Ondrej Zary 已提交
468 469 470 471 472 473
					printk(KERN_WARNING "AEC67162 CRC ERROR !\n");
					workreq->result = 0x02;
				}
			} else
				workreq->result = 0x02;

474
			if (is885(dev)) {
475 476
				j = atp_readb_base(dev, 0x29) | 0x01;
				atp_writeb_base(dev, 0x29, j);
L
Linus Torvalds 已提交
477 478 479 480
			}
			/*
			 *	Complete the command
			 */
481 482
			scsi_dma_unmap(workreq);

L
Linus Torvalds 已提交
483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
			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) {
498 499 500
				atp_writeb_io(dev, c, 0x1b, 0x01);
				while ((atp_readb_io(dev, c, 0x1b) & 0x01) != 0x01)
					atp_writeb_io(dev, c, 0x1b, 0x01);
L
Linus Torvalds 已提交
501 502 503 504 505 506 507 508 509 510 511 512 513 514
			} 
			/*
			 *	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;
O
Ondrej Zary 已提交
515
			return IRQ_HANDLED;
L
Linus Torvalds 已提交
516 517 518 519 520 521 522 523 524
		}
		if ((dev->last_cmd[c] & 0xf0) != 0x40) {
		   dev->last_cmd[c] = 0xff;
		}
		if (i == 0x4f) {
			i = 0x89;
		}
		i &= 0x0f;
		if (i == 0x09) {
525 526 527 528
			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);
529
			if (is885(dev)) {
L
Linus Torvalds 已提交
530
				k = dev->id[c][target_id].last_len;
531 532 533
				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]);
L
Linus Torvalds 已提交
534 535 536 537
				dev->id[c][target_id].dirct = 0x00;
			} else {
				dev->id[c][target_id].dirct = 0x00;
			}
538 539
			atp_writeb_io(dev, c, 0x18, 0x08);
			atp_writeb_pci(dev, c, 0, 0x09);
L
Linus Torvalds 已提交
540
			dev->in_int[c] = 0;
O
Ondrej Zary 已提交
541
			return IRQ_HANDLED;
L
Linus Torvalds 已提交
542 543
		}
		if (i == 0x08) {
544 545 546 547
			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);
548
			if (is885(dev)) {
L
Linus Torvalds 已提交
549
				k = dev->id[c][target_id].last_len;
550 551 552
				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]);
L
Linus Torvalds 已提交
553
			}
554
			atp_writeb_io(dev, c, 0x15, atp_readb_io(dev, c, 0x15) | 0x20);
L
Linus Torvalds 已提交
555
			dev->id[c][target_id].dirct = 0x20;
556 557
			atp_writeb_io(dev, c, 0x18, 0x08);
			atp_writeb_pci(dev, c, 0, 0x01);
L
Linus Torvalds 已提交
558
			dev->in_int[c] = 0;
O
Ondrej Zary 已提交
559
			return IRQ_HANDLED;
L
Linus Torvalds 已提交
560
		}
561 562 563 564
		if (i == 0x0a)
			atp_writeb_io(dev, c, 0x10, 0x30);
		else
			atp_writeb_io(dev, c, 0x10, 0x46);
L
Linus Torvalds 已提交
565
		dev->id[c][target_id].dirct = 0x00;
566 567 568 569
		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);
L
Linus Torvalds 已提交
570
	}
O
Ondrej Zary 已提交
571 572
	dev->in_int[c] = 0;

L
Linus Torvalds 已提交
573 574 575 576 577 578 579 580 581
	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.
 */
J
Jeff Garzik 已提交
582
static int atp870u_queuecommand_lck(struct scsi_cmnd *req_p,
L
Linus Torvalds 已提交
583 584 585
			 void (*done) (struct scsi_cmnd *))
{
	unsigned char c;
O
Ondrej Zary 已提交
586
	unsigned int m;
L
Linus Torvalds 已提交
587 588 589
	struct atp_unit *dev;
	struct Scsi_Host *host;

590
	c = scmd_channel(req_p);
L
Linus Torvalds 已提交
591
	req_p->sense_buffer[0]=0;
592
	scsi_set_resid(req_p, 0);
593
	if (scmd_channel(req_p) > 1) {
L
Linus Torvalds 已提交
594 595 596 597 598 599 600 601 602 603 604 605 606 607
		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;
608
	m = m << scmd_id(req_p);
L
Linus Torvalds 已提交
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655

	/*
	 *      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	
656
	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]);
L
Linus Torvalds 已提交
657
#endif
658
	if ((atp_readb_io(dev, c, 0x1c) == 0) && (dev->in_int[c] == 0) && (dev->in_snd[c] == 0)) {
L
Linus Torvalds 已提交
659 660 661 662 663 664 665 666 667 668 669
#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;
}

J
Jeff Garzik 已提交
670 671
static DEF_SCSI_QCMD(atp870u_queuecommand)

L
Linus Torvalds 已提交
672 673 674 675 676 677 678 679 680 681 682
/**
 *	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)
{
O
Ondrej Zary 已提交
683
	struct scsi_cmnd *workreq = NULL;
L
Linus Torvalds 已提交
684 685 686
	unsigned int i;//,k;
	unsigned char  j, target_id;
	unsigned char *prd;
O
Ondrej Zary 已提交
687
	unsigned short int w;
L
Linus Torvalds 已提交
688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703
	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;
O
Ondrej Zary 已提交
704 705 706 707 708 709
		if (!workreq) {
			dev->last_cmd[c] = 0xff;
			if (dev->quhd[c] == dev->quend[c]) {
				dev->in_snd[c] = 0;
				return;
			}
L
Linus Torvalds 已提交
710 711
		}
	}
O
Ondrej Zary 已提交
712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728
	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;
		}
729 730
		dev->id[c][scmd_id(workreq)].curr_req = workreq;
		dev->last_cmd[c] = scmd_id(workreq);
L
Linus Torvalds 已提交
731
	}
732
	if ((atp_readb_io(dev, c, 0x1f) & 0xb0) != 0 || atp_readb_io(dev, c, 0x1c) != 0) {
L
Linus Torvalds 已提交
733
#ifdef ED_DBGP
O
Ondrej Zary 已提交
734
		printk("Abort to Send\n");
L
Linus Torvalds 已提交
735
#endif
O
Ondrej Zary 已提交
736 737 738 739
		dev->last_cmd[c] |= 0x40;
		dev->in_snd[c] = 0;
		return;
	}
L
Linus Torvalds 已提交
740 741
#ifdef ED_DBGP
	printk("OK to Send\n");
742
	scmd_printk(KERN_DEBUG, workreq, "CDB");
L
Linus Torvalds 已提交
743 744 745
	for(i=0;i<workreq->cmd_len;i++) {
		printk(" %x",workreq->cmnd[i]);
	}
746
	printk("\n");
L
Linus Torvalds 已提交
747
#endif	
748 749
	l = scsi_bufflen(workreq);

750
	if (is885(dev)) {
751 752
		j = atp_readb_base(dev, 0x29) & 0xfe;
		atp_writeb_base(dev, 0x29, j);
753
		dev->r1f[c][scmd_id(workreq)] = 0;
L
Linus Torvalds 已提交
754 755 756
	}
	
	if (workreq->cmnd[0] == READ_CAPACITY) {
757 758
		if (l > 8)
			l = 8;
L
Linus Torvalds 已提交
759 760
	}
	if (workreq->cmnd[0] == 0x00) {
761
		l = 0;
L
Linus Torvalds 已提交
762 763 764
	}

	j = 0;
765
	target_id = scmd_id(workreq);
L
Linus Torvalds 已提交
766 767 768 769 770 771 772 773 774

	/*
	 *	Wide ?
	 */
	w = 1;
	w = w << target_id;
	if ((w & dev->wide_id[c]) != 0) {
		j |= 0x01;
	}
775 776 777
	atp_writeb_io(dev, c, 0x1b, j);
	while ((atp_readb_io(dev, c, 0x1b) & 0x01) != j) {
		atp_writeb_pci(dev, c, 0x1b, j);
L
Linus Torvalds 已提交
778 779 780 781 782 783 784 785
#ifdef ED_DBGP
		printk("send_s870 while loop 1\n");
#endif
	}
	/*
	 *	Write the command
	 */

786 787
	atp_writeb_io(dev, c, 0x00, workreq->cmd_len);
	atp_writeb_io(dev, c, 0x01, 0x2c);
788
	if (is885(dev))
789 790 791 792 793 794
		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);
L
Linus Torvalds 已提交
795 796 797
	/*
	 *	Write the target
	 */
798
	atp_writeb_io(dev, c, 0x11, dev->id[c][target_id].devsp);
L
Linus Torvalds 已提交
799 800 801
#ifdef ED_DBGP	
	printk("dev->id[%d][%d].devsp = %2x\n",c,target_id,dev->id[c][target_id].devsp);
#endif
802 803

	sg_count = scsi_dma_map(workreq);
L
Linus Torvalds 已提交
804 805 806
	/*
	 *	Write transfer size
	 */
807 808 809
	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]);
L
Linus Torvalds 已提交
810 811 812 813 814 815 816 817 818 819 820 821 822 823 824
	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
	 */
825 826 827 828 829 830
	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);
L
Linus Torvalds 已提交
831 832
	dev->id[c][target_id].dirct = 0;
	if (l == 0) {
833
		if (atp_readb_io(dev, c, 0x1c) == 0) {
L
Linus Torvalds 已提交
834 835 836
#ifdef ED_DBGP
			printk("change SCSI_CMD_REG 0x08\n");	
#endif				
837 838
			atp_writeb_io(dev, c, 0x18, 0x08);
		} else
L
Linus Torvalds 已提交
839 840 841 842 843 844 845 846 847 848 849 850
			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.
	 */

851 852
	if (l) {
		struct scatterlist *sgpnt;
L
Linus Torvalds 已提交
853
		i = 0;
854 855 856
		scsi_for_each_sg(workreq, sgpnt, sg_count, j) {
			bttl = sg_dma_address(sgpnt);
			l=sg_dma_len(sgpnt);
L
Linus Torvalds 已提交
857
#ifdef ED_DBGP		
858
			printk("1. bttl %x, l %x\n",bttl, l);
L
Linus Torvalds 已提交
859
#endif			
860
			while (l > 0x10000) {
L
Linus Torvalds 已提交
861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879
				(((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		
O
Ondrej Zary 已提交
880
	printk("send_s870: prdaddr_2 0x%8x target_id %d\n", dev->id[c][target_id].prdaddr,target_id);
L
Linus Torvalds 已提交
881
#endif	
882
	dev->id[c][target_id].prdaddr = dev->id[c][target_id].prd_bus;
883 884 885
	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);
886
	if (is885(dev)) {
887
		j = atp_readb_pci(dev, c, 1) & 0xf3;
L
Linus Torvalds 已提交
888 889 890 891
		if ((workreq->cmnd[0] == 0x08) || (workreq->cmnd[0] == 0x28) ||
	    	(workreq->cmnd[0] == 0x0a) || (workreq->cmnd[0] == 0x2a)) {
	   		j |= 0x0c;
		}
892
		atp_writeb_pci(dev, c, 1, j);
893
	} else if (is880(dev)) {
894 895 896 897
		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);
L
Linus Torvalds 已提交
898
	} else {		
899
		if ((workreq->cmnd[0] == 0x08) || (workreq->cmnd[0] == 0x28) || (workreq->cmnd[0] == 0x0a) || (workreq->cmnd[0] == 0x2a))
900
			atp_writeb_base(dev, 0x3a, (atp_readb_base(dev, 0x3a) & 0xf3) | 0x08);
901
		else
902
			atp_writeb_base(dev, 0x3a, atp_readb_base(dev, 0x3a) & 0xf3);
L
Linus Torvalds 已提交
903 904 905 906
	}	

	if(workreq->sc_data_direction == DMA_TO_DEVICE) {
		dev->id[c][target_id].dirct = 0x20;
907 908 909
		if (atp_readb_io(dev, c, 0x1c) == 0) {
			atp_writeb_io(dev, c, 0x18, 0x08);
			atp_writeb_pci(dev, c, 0, 0x01);
L
Linus Torvalds 已提交
910 911 912 913 914 915 916 917 918
#ifdef ED_DBGP		
		printk( "start DMA(to target)\n");
#endif				
		} else {
			dev->last_cmd[c] |= 0x40;
		}
		dev->in_snd[c] = 0;
		return;
	}
919 920 921
	if (atp_readb_io(dev, c, 0x1c) == 0) {
		atp_writeb_io(dev, c, 0x18, 0x08);
		atp_writeb_pci(dev, c, 0, 0x09);
L
Linus Torvalds 已提交
922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937
#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;

938
	atp_writew_io(dev, 0, 0x1c, *val);
L
Linus Torvalds 已提交
939
	for (i = 0; i < 10; i++) {	/* stable >= bus settle delay(400 ns)  */
940
		k = atp_readw_io(dev, 0, 0x1c);
L
Linus Torvalds 已提交
941
		j = (unsigned char) (k >> 8);
O
Ondrej Zary 已提交
942 943
		if ((k & 0x8000) != 0)	/* DB7 all release?    */
			i = 0;
L
Linus Torvalds 已提交
944 945
	}
	*val |= 0x4000;		/* assert DB6           */
946
	atp_writew_io(dev, 0, 0x1c, *val);
L
Linus Torvalds 已提交
947
	*val &= 0xdfff;		/* assert DB5           */
948
	atp_writew_io(dev, 0, 0x1c, *val);
L
Linus Torvalds 已提交
949
	for (i = 0; i < 10; i++) {	/* stable >= bus settle delay(400 ns) */
950
		if ((atp_readw_io(dev, 0, 0x1c) & 0x2000) != 0)	/* DB5 all release?       */
O
Ondrej Zary 已提交
951
			i = 0;
L
Linus Torvalds 已提交
952 953 954
	}
	*val |= 0x8000;		/* no DB4-0, assert DB7    */
	*val &= 0xe0ff;
955
	atp_writew_io(dev, 0, 0x1c, *val);
L
Linus Torvalds 已提交
956
	*val &= 0xbfff;		/* release DB6             */
957
	atp_writew_io(dev, 0, 0x1c, *val);
L
Linus Torvalds 已提交
958
	for (i = 0; i < 10; i++) {	/* stable >= bus settle delay(400 ns)  */
959
		if ((atp_readw_io(dev, 0, 0x1c) & 0x4000) != 0)	/* DB6 all release?  */
O
Ondrej Zary 已提交
960
			i = 0;
L
Linus Torvalds 已提交
961 962 963 964 965
	}

	return j;
}

966
static void tscam(struct Scsi_Host *host, bool wide_chip)
L
Linus Torvalds 已提交
967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984
{

	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);
	}
 */

985 986 987
	atp_writeb_io(dev, 0, 1, 0x08);
	atp_writeb_io(dev, 0, 2, 0x7f);
	atp_writeb_io(dev, 0, 0x11, 0x20);
L
Linus Torvalds 已提交
988 989 990 991 992 993 994

	if ((dev->scam_on & 0x40) == 0) {
		return;
	}
	m = 1;
	m <<= dev->host_id[0];
	j = 16;
995
	if (!wide_chip) {
L
Linus Torvalds 已提交
996 997 998 999
		m |= 0xff00;
		j = 8;
	}
	assignid_map = m;
1000 1001 1002 1003 1004 1005 1006
	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);
L
Linus Torvalds 已提交
1007 1008 1009 1010 1011 1012 1013

	for (i = 0; i < j; i++) {
		m = 1;
		m = m << i;
		if ((m & assignid_map) != 0) {
			continue;
		}
1014 1015 1016 1017
		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);
L
Linus Torvalds 已提交
1018 1019 1020 1021 1022
		if (i > 7) {
			k = (i & 0x07) | 0x40;
		} else {
			k = i;
		}
1023
		atp_writeb_io(dev, 0, 0x15, k);
1024
		if (wide_chip)
1025 1026 1027
			atp_writeb_io(dev, 0, 0x1b, 0x01);
		else
			atp_writeb_io(dev, 0, 0x1b, 0x00);
O
Ondrej Zary 已提交
1028
		do {
1029
			atp_writeb_io(dev, 0, 0x18, 0x09);
L
Linus Torvalds 已提交
1030

1031
			while ((atp_readb_io(dev, 0, 0x1f) & 0x80) == 0x00)
O
Ondrej Zary 已提交
1032
				cpu_relax();
1033
			k = atp_readb_io(dev, 0, 0x17);
O
Ondrej Zary 已提交
1034 1035 1036
			if ((k == 0x85) || (k == 0x42))
				break;
			if (k != 0x16)
1037
				atp_writeb_io(dev, 0, 0x10, 0x41);
O
Ondrej Zary 已提交
1038 1039 1040
		} while (k != 0x16);
		if ((k == 0x85) || (k == 0x42))
			continue;
L
Linus Torvalds 已提交
1041 1042 1043
		assignid_map |= m;

	}
1044 1045
	atp_writeb_io(dev, 0, 0x02, 0x7f);
	atp_writeb_io(dev, 0, 0x1b, 0x02);
L
Linus Torvalds 已提交
1046

1047
	udelay(2);
L
Linus Torvalds 已提交
1048 1049

	val = 0x0080;		/* bsy  */
1050
	atp_writew_io(dev, 0, 0x1c, val);
L
Linus Torvalds 已提交
1051
	val |= 0x0040;		/* sel  */
1052
	atp_writew_io(dev, 0, 0x1c, val);
L
Linus Torvalds 已提交
1053
	val |= 0x0004;		/* msg  */
1054
	atp_writew_io(dev, 0, 0x1c, val);
1055
	udelay(2);		/* 2 deskew delay(45ns*2=90ns) */
L
Linus Torvalds 已提交
1056
	val &= 0x007f;		/* no bsy  */
1057
	atp_writew_io(dev, 0, 0x1c, val);
L
Linus Torvalds 已提交
1058 1059
	mdelay(128);
	val &= 0x00fb;		/* after 1ms no msg */
1060 1061
	atp_writew_io(dev, 0, 0x1c, val);
	while ((atp_readb_io(dev, 0, 0x1c) & 0x04) != 0)
O
Ondrej Zary 已提交
1062
		;
1063
	udelay(2);
L
Linus Torvalds 已提交
1064
	udelay(100);
O
Ondrej Zary 已提交
1065
	for (n = 0; n < 0x30000; n++)
1066
		if ((atp_readb_io(dev, 0, 0x1c) & 0x80) != 0)	/* bsy ? */
O
Ondrej Zary 已提交
1067 1068 1069
			break;
	if (n < 0x30000)
		for (n = 0; n < 0x30000; n++)
1070
			if ((atp_readb_io(dev, 0, 0x1c) & 0x81) == 0x0081) {
1071
				udelay(2);
O
Ondrej Zary 已提交
1072
				val |= 0x8003;		/* io,cd,db7  */
1073
				atp_writew_io(dev, 0, 0x1c, val);
1074
				udelay(2);
O
Ondrej Zary 已提交
1075
				val &= 0x00bf;		/* no sel     */
1076
				atp_writew_io(dev, 0, 0x1c, val);
1077
				udelay(2);
O
Ondrej Zary 已提交
1078 1079 1080
				break;
			}
	while (1) {
1081 1082 1083 1084 1085 1086 1087 1088 1089 1090
	/*
	 * 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);
1091 1092 1093 1094 1095 1096
	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)
L
Linus Torvalds 已提交
1097
			cpu_relax();
1098
		atp_readb_io(dev, 0, 0x17);
L
Linus Torvalds 已提交
1099 1100 1101 1102 1103
		return;
	}
	val &= 0x00ff;		/* synchronization  */
	val |= 0x3f00;
	fun_scam(dev, &val);
1104
	udelay(2);
L
Linus Torvalds 已提交
1105 1106 1107
	val &= 0x00ff;		/* isolation        */
	val |= 0x2000;
	fun_scam(dev, &val);
1108
	udelay(2);
L
Linus Torvalds 已提交
1109 1110
	i = 8;
	j = 0;
O
Ondrej Zary 已提交
1111 1112

	while (1) {
1113
		if ((atp_readw_io(dev, 0, 0x1c) & 0x2000) == 0)
O
Ondrej Zary 已提交
1114
			continue;
1115
		udelay(2);
O
Ondrej Zary 已提交
1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129
		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;
L
Linus Torvalds 已提交
1130 1131
	}

O
Ondrej Zary 已提交
1132
	/* isolation complete..  */
L
Linus Torvalds 已提交
1133 1134 1135 1136
/*    mbuf[32]=0;
	printk(" \n%x %x %x %s\n ",assignid_map,mbuf[0],mbuf[1],&mbuf[2]); */
	i = 15;
	j = mbuf[0];
L
Lucas De Marchi 已提交
1137
	if ((j & 0x20) != 0) {	/* bit5=1:ID up to 7      */
L
Linus Torvalds 已提交
1138 1139
		i = 7;
	}
O
Ondrej Zary 已提交
1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151
	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;
		}
L
Linus Torvalds 已提交
1152
	}
O
Ondrej Zary 已提交
1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164
	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;
		}
L
Linus Torvalds 已提交
1165
	}
O
Ondrej Zary 已提交
1166
	/* k=binID#,       */
L
Linus Torvalds 已提交
1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184
	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);

O
Ondrej Zary 已提交
1185
	}
L
Linus Torvalds 已提交
1186 1187 1188 1189 1190 1191 1192 1193 1194 1195
}

static void atp870u_free_tables(struct Scsi_Host *host)
{
	struct atp_unit *atp_dev = (struct atp_unit *)&host->hostdata;
	int j, k;
	for (j=0; j < 2; j++) {
		for (k = 0; k < 16; k++) {
			if (!atp_dev->id[j][k].prd_table)
				continue;
1196
			pci_free_consistent(atp_dev->pdev, 1024, atp_dev->id[j][k].prd_table, atp_dev->id[j][k].prd_bus);
L
Linus Torvalds 已提交
1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207
			atp_dev->id[j][k].prd_table = NULL;
		}
	}
}

static int atp870u_init_tables(struct Scsi_Host *host)
{
	struct atp_unit *atp_dev = (struct atp_unit *)&host->hostdata;
	int c,k;
	for(c=0;c < 2;c++) {
	   	for(k=0;k<16;k++) {
1208
	   			atp_dev->id[c][k].prd_table = pci_alloc_consistent(atp_dev->pdev, 1024, &(atp_dev->id[c][k].prd_bus));
L
Linus Torvalds 已提交
1209 1210 1211 1212 1213
	   			if (!atp_dev->id[c][k].prd_table) {
	   				printk("atp870u_init_tables fail\n");
				atp870u_free_tables(host);
				return -ENOMEM;
			}
1214
			atp_dev->id[c][k].prdaddr = atp_dev->id[c][k].prd_bus;
L
Linus Torvalds 已提交
1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239
			atp_dev->id[c][k].devsp=0x20;
			atp_dev->id[c][k].devtype = 0x7f;
			atp_dev->id[c][k].curr_req = NULL;			   
	   	}
	   			
	   	atp_dev->active_id[c] = 0;
	   	atp_dev->wide_id[c] = 0;
	   	atp_dev->host_id[c] = 0x07;
	   	atp_dev->quhd[c] = 0;
	   	atp_dev->quend[c] = 0;
	   	atp_dev->last_cmd[c] = 0xff;
	   	atp_dev->in_snd[c] = 0;
	   	atp_dev->in_int[c] = 0;
	   	
	   	for (k = 0; k < qcnt; k++) {
	   		  atp_dev->quereq[c][k] = NULL;
	   	}	   		   
	   	for (k = 0; k < 16; k++) {
			   atp_dev->id[c][k].curr_req = NULL;
			   atp_dev->sp[c][k] = 0x04;
	   	}		   
	}
	return 0;
}

O
Ondrej Zary 已提交
1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251
static void atp_set_host_id(struct atp_unit *atp, u8 c, u8 host_id)
{
	atp_writeb_io(atp, c, 0, host_id | 0x08);
	atp_writeb_io(atp, c, 0x18, 0);
	while ((atp_readb_io(atp, c, 0x1f) & 0x80) == 0)
		mdelay(1);
	atp_readb_io(atp, c, 0x17);
	atp_writeb_io(atp, c, 1, 8);
	atp_writeb_io(atp, c, 2, 0x7f);
	atp_writeb_io(atp, c, 0x11, 0x20);
}

L
Linus Torvalds 已提交
1252 1253 1254 1255
/* return non-zero on detection */
static int atp870u_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
{
	unsigned char k, m, c;
O
Ondrej Zary 已提交
1256
	unsigned int error,n;
L
Linus Torvalds 已提交
1257 1258
	unsigned char host_id;
	struct Scsi_Host *shpnt = NULL;
O
Ondrej Zary 已提交
1259
	struct atp_unit *atpdev;
L
Linus Torvalds 已提交
1260
	unsigned char setupdata[2][16];
O
Ondrej Zary 已提交
1261
	int err;
1262

1263 1264 1265 1266 1267
	if (ent->device == PCI_DEVICE_ID_ARTOP_AEC7610 && pdev->revision < 2) {
		dev_err(&pdev->dev, "ATP850S chips (AEC6710L/F cards) are not supported.\n");
		return -ENODEV;
	}

O
Ondrej Zary 已提交
1268 1269 1270
	err = pci_enable_device(pdev);
	if (err)
		goto fail;
L
Linus Torvalds 已提交
1271

1272
	if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32))) {
L
Linus Torvalds 已提交
1273
                printk(KERN_ERR "atp870u: DMA mask required but not available.\n");
O
Ondrej Zary 已提交
1274 1275
                err = -EIO;
                goto disable_device;
L
Linus Torvalds 已提交
1276 1277
        }

O
Ondrej Zary 已提交
1278 1279 1280 1281 1282
	err = pci_request_regions(pdev, "atp870u");
	if (err)
		goto disable_device;
	pci_set_master(pdev);

O
Ondrej Zary 已提交
1283 1284 1285
        err = -ENOMEM;
	shpnt = scsi_host_alloc(&atp870u_template, sizeof(struct atp_unit));
	if (!shpnt)
O
Ondrej Zary 已提交
1286
		goto release_region;
O
Ondrej Zary 已提交
1287 1288 1289 1290 1291 1292 1293

	atpdev = shost_priv(shpnt);

	atpdev->host = shpnt;
	atpdev->pdev = pdev;
	pci_set_drvdata(pdev, atpdev);

O
Ondrej Zary 已提交
1294 1295 1296 1297 1298 1299
	shpnt->io_port = pci_resource_start(pdev, 0);
	shpnt->io_port &= 0xfffffff8;
	shpnt->n_io_port = pci_resource_len(pdev, 0);
	atpdev->baseport = shpnt->io_port;
	shpnt->unique_id = shpnt->io_port;
	shpnt->irq = pdev->irq;
1300

1301
	if (is880(atpdev)) {
L
Linus Torvalds 已提交
1302 1303
		pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0x80);//JCC082803

O
Ondrej Zary 已提交
1304 1305
		atpdev->ioport[0] = shpnt->io_port + 0x40;
		atpdev->pciport[0] = shpnt->io_port + 0x28;
1306 1307

		host_id = atp_readb_base(atpdev, 0x39);
L
Linus Torvalds 已提交
1308 1309
		host_id >>= 0x04;

1310
		printk(KERN_INFO "   ACARD AEC-67160 PCI Ultra3 LVD Host Adapter:"
O
Ondrej Zary 已提交
1311
			"    IO:%lx, IRQ:%d.\n", shpnt->io_port, shpnt->irq);
1312
		atpdev->host_id[0] = host_id;
L
Linus Torvalds 已提交
1313

1314 1315 1316
		atpdev->scam_on = atp_readb_base(atpdev, 0x22);
		atpdev->global_map[0] = atp_readb_base(atpdev, 0x35);
		atpdev->ultra_map[0] = atp_readw_base(atpdev, 0x3c);
L
Linus Torvalds 已提交
1317 1318 1319 1320 1321 1322 1323

		n = 0x3f09;
next_fblk_880:
		if (n >= 0x4000)
			goto flash_ok_880;

		m = 0;
1324
		atp_writew_base(atpdev, 0x34, n);
L
Linus Torvalds 已提交
1325
		n += 0x0002;
1326
		if (atp_readb_base(atpdev, 0x30) == 0xff)
L
Linus Torvalds 已提交
1327 1328
			goto flash_ok_880;

1329 1330 1331 1332 1333
		atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x30);
		atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x31);
		atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x32);
		atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x33);
		atp_writew_base(atpdev, 0x34, n);
L
Linus Torvalds 已提交
1334
		n += 0x0002;
1335 1336 1337 1338 1339
		atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x30);
		atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x31);
		atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x32);
		atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x33);
		atp_writew_base(atpdev, 0x34, n);
L
Linus Torvalds 已提交
1340
		n += 0x0002;
1341 1342 1343 1344 1345
		atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x30);
		atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x31);
		atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x32);
		atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x33);
		atp_writew_base(atpdev, 0x34, n);
L
Linus Torvalds 已提交
1346
		n += 0x0002;
1347 1348 1349 1350
		atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x30);
		atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x31);
		atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x32);
		atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x33);
L
Linus Torvalds 已提交
1351 1352 1353
		n += 0x0018;
		goto next_fblk_880;
flash_ok_880:
1354
		atp_writew_base(atpdev, 0x34, 0);
1355 1356
		atpdev->ultra_map[0] = 0;
		atpdev->async[0] = 0;
L
Linus Torvalds 已提交
1357 1358 1359
		for (k = 0; k < 16; k++) {
			n = 1;
			n = n << k;
1360 1361
			if (atpdev->sp[0][k] > 1) {
				atpdev->ultra_map[0] |= n;
L
Linus Torvalds 已提交
1362
			} else {
1363 1364
				if (atpdev->sp[0][k] == 0)
					atpdev->async[0] |= n;
L
Linus Torvalds 已提交
1365 1366
 			}
	 	}
1367
		atpdev->async[0] = ~(atpdev->async[0]);
1368
		atp_writeb_base(atpdev, 0x35, atpdev->global_map[0]);
L
Linus Torvalds 已提交
1369 1370 1371
 
		if (atp870u_init_tables(shpnt) < 0) {
			printk(KERN_ERR "Unable to allocate tables for Acard controller\n");
O
Ondrej Zary 已提交
1372
			err = -ENOMEM;
L
Linus Torvalds 已提交
1373 1374 1375
			goto unregister;
		}

O
Ondrej Zary 已提交
1376 1377 1378
		k = atp_readb_base(atpdev, 0x38) & 0x80;
		atp_writeb_base(atpdev, 0x38, k);
		atp_writeb_base(atpdev, 0x3b, 0x20);
L
Linus Torvalds 已提交
1379
		mdelay(32);
O
Ondrej Zary 已提交
1380
		atp_writeb_base(atpdev, 0x3b, 0);
L
Linus Torvalds 已提交
1381
		mdelay(32);
O
Ondrej Zary 已提交
1382 1383
		atp_readb_io(atpdev, 0, 0x1b);
		atp_readb_io(atpdev, 0, 0x17);
O
Ondrej Zary 已提交
1384

O
Ondrej Zary 已提交
1385
		atp_set_host_id(atpdev, 0, host_id);
L
Linus Torvalds 已提交
1386

1387
		tscam(shpnt, true);
O
Ondrej Zary 已提交
1388 1389
		atp_is(atpdev, 0, true, atp_readb_base(atpdev, 0x3f) & 0x40);
		atp_writeb_base(atpdev, 0x38, 0xb0);
L
Linus Torvalds 已提交
1390 1391
		shpnt->max_id = 16;
		shpnt->this_id = host_id;
1392
	} else if (is885(atpdev)) {
O
Ondrej Zary 已提交
1393 1394
			printk(KERN_INFO "   ACARD AEC-67162 PCI Ultra3 LVD Host Adapter:  IO:%lx, IRQ:%d.\n"
			       , shpnt->io_port, shpnt->irq);
L
Linus Torvalds 已提交
1395
        	
1396
		atpdev->pdev = pdev;
O
Ondrej Zary 已提交
1397 1398 1399 1400
		atpdev->ioport[0] = shpnt->io_port + 0x80;
		atpdev->ioport[1] = shpnt->io_port + 0xc0;
		atpdev->pciport[0] = shpnt->io_port + 0x40;
		atpdev->pciport[1] = shpnt->io_port + 0x50;
L
Linus Torvalds 已提交
1401
				
O
Ondrej Zary 已提交
1402 1403
		if (atp870u_init_tables(shpnt) < 0) {
			err = -ENOMEM;
L
Linus Torvalds 已提交
1404
			goto unregister;
O
Ondrej Zary 已提交
1405
		}
L
Linus Torvalds 已提交
1406
			
O
Ondrej Zary 已提交
1407 1408
		c = atp_readb_base(atpdev, 0x29);
		atp_writeb_base(atpdev, 0x29, c | 0x04);
L
Linus Torvalds 已提交
1409 1410 1411 1412 1413 1414
        	
		n=0x1f80;
next_fblk_885:
		if (n >= 0x2000) {
		   goto flash_ok_885;
		}
O
Ondrej Zary 已提交
1415 1416
		atp_writew_base(atpdev, 0x3c, n);
		if (atp_readl_base(atpdev, 0x38) == 0xffffffff) {
L
Linus Torvalds 已提交
1417 1418 1419
		   goto flash_ok_885;
		}
		for (m=0; m < 2; m++) {
O
Ondrej Zary 已提交
1420
		    atpdev->global_map[m]= 0;
L
Linus Torvalds 已提交
1421
		    for (k=0; k < 4; k++) {
O
Ondrej Zary 已提交
1422 1423
			atp_writew_base(atpdev, 0x3c, n++);
			((unsigned long *)&setupdata[m][0])[k] = atp_readl_base(atpdev, 0x38);
L
Linus Torvalds 已提交
1424 1425
		    }
		    for (k=0; k < 4; k++) {
O
Ondrej Zary 已提交
1426 1427
			atp_writew_base(atpdev, 0x3c, n++);
			((unsigned long *)&atpdev->sp[m][0])[k] = atp_readl_base(atpdev, 0x38);
L
Linus Torvalds 已提交
1428 1429 1430 1431 1432 1433 1434 1435
		    }
		    n += 8;
		}
		goto next_fblk_885;
flash_ok_885:
#ifdef ED_DBGP
		printk( "Flash Read OK\n");
#endif	
O
Ondrej Zary 已提交
1436 1437
		c = atp_readb_base(atpdev, 0x29);
		atp_writeb_base(atpdev, 0x29, c & 0xfb);
L
Linus Torvalds 已提交
1438
		for (c=0;c < 2;c++) {
O
Ondrej Zary 已提交
1439 1440
		    atpdev->ultra_map[c]=0;
		    atpdev->async[c] = 0;
L
Linus Torvalds 已提交
1441 1442 1443
		    for (k=0; k < 16; k++) {
			n=1;
			n = n << k;
O
Ondrej Zary 已提交
1444 1445
			if (atpdev->sp[c][k] > 1) {
			   atpdev->ultra_map[c] |= n;
L
Linus Torvalds 已提交
1446
			} else {
O
Ondrej Zary 已提交
1447 1448
			   if (atpdev->sp[c][k] == 0) {
			      atpdev->async[c] |= n;
L
Linus Torvalds 已提交
1449 1450 1451
			   }
			}
		    }
O
Ondrej Zary 已提交
1452
		    atpdev->async[c] = ~(atpdev->async[c]);
L
Linus Torvalds 已提交
1453

O
Ondrej Zary 已提交
1454
		    if (atpdev->global_map[c] == 0) {
L
Linus Torvalds 已提交
1455 1456
		       k=setupdata[c][1];
		       if ((k & 0x40) != 0)
O
Ondrej Zary 已提交
1457
			  atpdev->global_map[c] |= 0x20;
L
Linus Torvalds 已提交
1458
		       k &= 0x07;
O
Ondrej Zary 已提交
1459
		       atpdev->global_map[c] |= k;
L
Linus Torvalds 已提交
1460
		       if ((setupdata[c][2] & 0x04) != 0)
O
Ondrej Zary 已提交
1461 1462
			  atpdev->global_map[c] |= 0x08;
		       atpdev->host_id[c] = setupdata[c][0] & 0x07;
L
Linus Torvalds 已提交
1463 1464 1465
		    }
		}

O
Ondrej Zary 已提交
1466
		k = atp_readb_base(atpdev, 0x28) & 0x8f;
L
Linus Torvalds 已提交
1467
		k |= 0x10;
O
Ondrej Zary 已提交
1468 1469 1470
		atp_writeb_base(atpdev, 0x28, k);
		atp_writeb_pci(atpdev, 0, 1, 0x80);
		atp_writeb_pci(atpdev, 1, 1, 0x80);
L
Linus Torvalds 已提交
1471
		mdelay(100);
O
Ondrej Zary 已提交
1472 1473
		atp_writeb_pci(atpdev, 0, 1, 0);
		atp_writeb_pci(atpdev, 1, 1, 0);
L
Linus Torvalds 已提交
1474
		mdelay(1000);
O
Ondrej Zary 已提交
1475 1476 1477 1478
		atp_readb_io(atpdev, 0, 0x1b);
		atp_readb_io(atpdev, 0, 0x17);
		atp_readb_io(atpdev, 1, 0x1b);
		atp_readb_io(atpdev, 1, 0x17);
O
Ondrej Zary 已提交
1479

O
Ondrej Zary 已提交
1480
		k=atpdev->host_id[0];
L
Linus Torvalds 已提交
1481 1482
		if (k > 7)
		   k = (k & 0x07) | 0x40;
O
Ondrej Zary 已提交
1483
		atp_set_host_id(atpdev, 0, k);
O
Ondrej Zary 已提交
1484

O
Ondrej Zary 已提交
1485
		k=atpdev->host_id[1];
L
Linus Torvalds 已提交
1486 1487
		if (k > 7)
		   k = (k & 0x07) | 0x40;
O
Ondrej Zary 已提交
1488
		atp_set_host_id(atpdev, 1, k);
L
Linus Torvalds 已提交
1489

O
Ondrej Zary 已提交
1490
		mdelay(600); /* this delay used to be called tscam_885() */
L
Linus Torvalds 已提交
1491
		printk(KERN_INFO "   Scanning Channel A SCSI Device ...\n");
O
Ondrej Zary 已提交
1492 1493
		atp_is(atpdev, 0, true, atp_readb_io(atpdev, 0, 0x1b) >> 7);
		atp_writeb_io(atpdev, 0, 0x16, 0x80);
L
Linus Torvalds 已提交
1494
		printk(KERN_INFO "   Scanning Channel B SCSI Device ...\n");
O
Ondrej Zary 已提交
1495 1496 1497
		atp_is(atpdev, 1, true, atp_readb_io(atpdev, 1, 0x1b) >> 7);
		atp_writeb_io(atpdev, 1, 0x16, 0x80);
		k = atp_readb_base(atpdev, 0x28) & 0xcf;
L
Linus Torvalds 已提交
1498
		k |= 0xc0;
O
Ondrej Zary 已提交
1499 1500 1501 1502 1503
		atp_writeb_base(atpdev, 0x28, k);
		k = atp_readb_base(atpdev, 0x1f) | 0x80;
		atp_writeb_base(atpdev, 0x1f, k);
		k = atp_readb_base(atpdev, 0x29) | 0x01;
		atp_writeb_base(atpdev, 0x29, k);
L
Linus Torvalds 已提交
1504 1505 1506 1507
#ifdef ED_DBGP
		//printk("atp885: atp_host[0] 0x%p\n", atp_host[0]);
#endif		
		shpnt->max_id = 16;
O
Ondrej Zary 已提交
1508
		shpnt->max_lun = (atpdev->global_map[0] & 0x07) + 1;
L
Linus Torvalds 已提交
1509
		shpnt->max_channel = 1;
O
Ondrej Zary 已提交
1510
		shpnt->this_id = atpdev->host_id[0];
L
Linus Torvalds 已提交
1511
	} else {
1512 1513 1514 1515 1516
		bool wide_chip =
			(ent->device == PCI_DEVICE_ID_ARTOP_AEC7610 &&
			 pdev->revision == 4) ||
			(ent->device == PCI_DEVICE_ID_ARTOP_AEC7612UW) ||
			(ent->device == PCI_DEVICE_ID_ARTOP_AEC7612SUW);
L
Linus Torvalds 已提交
1517 1518
		error = pci_read_config_byte(pdev, 0x49, &host_id);

1519
		printk(KERN_INFO "   ACARD AEC-671X PCI Ultra/W SCSI-2/3 Host Adapter: "
O
Ondrej Zary 已提交
1520
			"IO:%lx, IRQ:%d.\n", shpnt->io_port, shpnt->irq);
L
Linus Torvalds 已提交
1521

O
Ondrej Zary 已提交
1522 1523
		atpdev->ioport[0] = shpnt->io_port;
		atpdev->pciport[0] = shpnt->io_port + 0x20;
L
Linus Torvalds 已提交
1524
		host_id &= 0x07;
1525
		atpdev->host_id[0] = host_id;
1526 1527 1528
		atpdev->scam_on = atp_readb_pci(atpdev, 0, 2);
		atpdev->global_map[0] = atp_readb_base(atpdev, 0x2d);
		atpdev->ultra_map[0] = atp_readw_base(atpdev, 0x2e);
L
Linus Torvalds 已提交
1529

1530 1531 1532 1533
		if (atpdev->ultra_map[0] == 0) {
			atpdev->scam_on = 0x00;
			atpdev->global_map[0] = 0x20;
			atpdev->ultra_map[0] = 0xffff;
L
Linus Torvalds 已提交
1534 1535 1536
		}


O
Ondrej Zary 已提交
1537 1538
		if (atp870u_init_tables(shpnt) < 0) {
			err = -ENOMEM;
L
Linus Torvalds 已提交
1539
			goto unregister;
O
Ondrej Zary 已提交
1540
		}
L
Linus Torvalds 已提交
1541

1542
		if (pdev->revision > 0x07)	/* check if atp876 chip then enable terminator */
O
Ondrej Zary 已提交
1543
			atp_writeb_base(atpdev, 0x3e, 0x00);
L
Linus Torvalds 已提交
1544
 
O
Ondrej Zary 已提交
1545 1546 1547
		k = (atp_readb_base(atpdev, 0x3a) & 0xf3) | 0x10;
		atp_writeb_base(atpdev, 0x3a, k);
		atp_writeb_base(atpdev, 0x3a, k & 0xdf);
L
Linus Torvalds 已提交
1548
		mdelay(32);
O
Ondrej Zary 已提交
1549
		atp_writeb_base(atpdev, 0x3a, k);
L
Linus Torvalds 已提交
1550
		mdelay(32);
O
Ondrej Zary 已提交
1551
		atp_set_host_id(atpdev, 0, host_id);
L
Linus Torvalds 已提交
1552

1553 1554

		tscam(shpnt, wide_chip);
O
Ondrej Zary 已提交
1555
		atp_writeb_base(atpdev, 0x3a, atp_readb_base(atpdev, 0x3a) | 0x10);
1556
		atp_is(atpdev, 0, wide_chip, 0);
O
Ondrej Zary 已提交
1557 1558
		atp_writeb_base(atpdev, 0x3a, atp_readb_base(atpdev, 0x3a) & 0xef);
		atp_writeb_base(atpdev, 0x3b, atp_readb_base(atpdev, 0x3b) | 0x20);
1559
		shpnt->max_id = wide_chip ? 16 : 8;
L
Linus Torvalds 已提交
1560
		shpnt->this_id = host_id;
O
Ondrej Zary 已提交
1561

L
Linus Torvalds 已提交
1562
	} 
1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574
	err = request_irq(shpnt->irq, atp870u_intr_handle, IRQF_SHARED, "atp870u", shpnt);
	if (err) {
		dev_err(&pdev->dev, "Unable to allocate IRQ %d.\n", shpnt->irq);
		goto free_tables;
	}

	err = scsi_add_host(shpnt, &pdev->dev);
	if (err)
		goto scsi_add_fail;
	scsi_scan_host(shpnt);

	return 0;
L
Linus Torvalds 已提交
1575 1576

scsi_add_fail:
O
Ondrej Zary 已提交
1577
	free_irq(shpnt->irq, shpnt);
L
Linus Torvalds 已提交
1578 1579 1580 1581
free_tables:
	atp870u_free_tables(shpnt);
unregister:
	scsi_host_put(shpnt);
O
Ondrej Zary 已提交
1582 1583
release_region:
	pci_release_regions(pdev);
O
Ondrej Zary 已提交
1584 1585 1586 1587
disable_device:
	pci_disable_device(pdev);
fail:
	return err;
L
Linus Torvalds 已提交
1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602
}

/* The abort command does not leave the device in a clean state where
   it is available to be used again.  Until this gets worked out, we will
   leave it commented out.  */

static int atp870u_abort(struct scsi_cmnd * SCpnt)
{
	unsigned char  j, k, c;
	struct scsi_cmnd *workrequ;
	struct atp_unit *dev;	
	struct Scsi_Host *host;
	host = SCpnt->device->host;

	dev = (struct atp_unit *)&host->hostdata;
1603
	c = scmd_channel(SCpnt);
L
Linus Torvalds 已提交
1604 1605 1606 1607
	printk(" atp870u: abort Channel = %x \n", c);
	printk("working=%x last_cmd=%x ", dev->working[c], dev->last_cmd[c]);
	printk(" quhdu=%x quendu=%x ", dev->quhd[c], dev->quend[c]);
	for (j = 0; j < 0x18; j++) {
1608
		printk(" r%2x=%2x", j, atp_readb_io(dev, c, j));
L
Linus Torvalds 已提交
1609
	}
1610 1611 1612 1613
	printk(" r1c=%2x", atp_readb_io(dev, c, 0x1c));
	printk(" r1f=%2x in_snd=%2x ", atp_readb_io(dev, c, 0x1f), dev->in_snd[c]);
	printk(" d00=%2x", atp_readb_pci(dev, c, 0x00));
	printk(" d02=%2x", atp_readb_pci(dev, c, 0x02));
L
Linus Torvalds 已提交
1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635
	for(j=0;j<16;j++) {
	   if (dev->id[c][j].curr_req != NULL) {
		workrequ = dev->id[c][j].curr_req;
		printk("\n que cdb= ");
		for (k=0; k < workrequ->cmd_len; k++) {
		    printk(" %2x ",workrequ->cmnd[k]);
		}
		printk(" last_lenu= %x ",(unsigned int)dev->id[c][j].last_len);
	   }
	}
	return SUCCESS;
}

static const char *atp870u_info(struct Scsi_Host *notused)
{
	static char buffer[128];

	strcpy(buffer, "ACARD AEC-6710/6712/67160 PCI Ultra/W/LVD SCSI-3 Adapter Driver V2.6+ac ");

	return buffer;
}

A
Al Viro 已提交
1636
static int atp870u_show_info(struct seq_file *m, struct Scsi_Host *HBAptr)
L
Linus Torvalds 已提交
1637
{
1638 1639
	seq_puts(m, "ACARD AEC-671X Driver Version: 2.6+ac\n\n"
		"Adapter Configuration:\n");
A
Al Viro 已提交
1640 1641 1642
	seq_printf(m, "               Base IO: %#.4lx\n", HBAptr->io_port);
	seq_printf(m, "                   IRQ: %d\n", HBAptr->irq);
	return 0;
L
Linus Torvalds 已提交
1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673
}


static int atp870u_biosparam(struct scsi_device *disk, struct block_device *dev,
			sector_t capacity, int *ip)
{
	int heads, sectors, cylinders;

	heads = 64;
	sectors = 32;
	cylinders = (unsigned long)capacity / (heads * sectors);
	if (cylinders > 1024) {
		heads = 255;
		sectors = 63;
		cylinders = (unsigned long)capacity / (heads * sectors);
	}
	ip[0] = heads;
	ip[1] = sectors;
	ip[2] = cylinders;

	return 0;
}

static void atp870u_remove (struct pci_dev *pdev)
{	
	struct atp_unit *devext = pci_get_drvdata(pdev);
	struct Scsi_Host *pshost = devext->host;
	
	
	scsi_remove_host(pshost);
	free_irq(pshost->irq, pshost);
O
Ondrej Zary 已提交
1674 1675
	pci_release_regions(pdev);
	pci_disable_device(pdev);
L
Linus Torvalds 已提交
1676 1677 1678 1679 1680 1681 1682 1683 1684
	atp870u_free_tables(pshost);
	scsi_host_put(pshost);
}
MODULE_LICENSE("GPL");

static struct scsi_host_template atp870u_template = {
     .module			= THIS_MODULE,
     .name              	= "atp870u"		/* name */,
     .proc_name			= "atp870u",
A
Al Viro 已提交
1685
     .show_info			= atp870u_show_info,
L
Linus Torvalds 已提交
1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716
     .info              	= atp870u_info		/* info */,
     .queuecommand      	= atp870u_queuecommand	/* queuecommand */,
     .eh_abort_handler  	= atp870u_abort		/* abort */,
     .bios_param        	= atp870u_biosparam	/* biosparm */,
     .can_queue         	= qcnt			/* can_queue */,
     .this_id           	= 7			/* SCSI ID */,
     .sg_tablesize      	= ATP870U_SCATTER	/*SG_ALL*/ /*SG_NONE*/,
     .use_clustering    	= ENABLE_CLUSTERING,
     .max_sectors		= ATP870U_MAX_SECTORS,
};

static struct pci_device_id atp870u_id_table[] = {
	{ PCI_DEVICE(PCI_VENDOR_ID_ARTOP, ATP885_DEVID)			  },
	{ PCI_DEVICE(PCI_VENDOR_ID_ARTOP, ATP880_DEVID1)			  },
	{ PCI_DEVICE(PCI_VENDOR_ID_ARTOP, ATP880_DEVID2)			  },
	{ PCI_DEVICE(PCI_VENDOR_ID_ARTOP, PCI_DEVICE_ID_ARTOP_AEC7610)    },
	{ PCI_DEVICE(PCI_VENDOR_ID_ARTOP, PCI_DEVICE_ID_ARTOP_AEC7612UW)  },
	{ PCI_DEVICE(PCI_VENDOR_ID_ARTOP, PCI_DEVICE_ID_ARTOP_AEC7612U)   },
	{ PCI_DEVICE(PCI_VENDOR_ID_ARTOP, PCI_DEVICE_ID_ARTOP_AEC7612S)   },
	{ PCI_DEVICE(PCI_VENDOR_ID_ARTOP, PCI_DEVICE_ID_ARTOP_AEC7612D)	  },
	{ PCI_DEVICE(PCI_VENDOR_ID_ARTOP, PCI_DEVICE_ID_ARTOP_AEC7612SUW) },
	{ PCI_DEVICE(PCI_VENDOR_ID_ARTOP, PCI_DEVICE_ID_ARTOP_8060)	  },
	{ 0, },
};

MODULE_DEVICE_TABLE(pci, atp870u_id_table);

static struct pci_driver atp870u_driver = {
	.id_table	= atp870u_id_table,
	.name		= "atp870u",
	.probe		= atp870u_probe,
1717
	.remove		= atp870u_remove,
L
Linus Torvalds 已提交
1718 1719
};

O
Ondrej Zary 已提交
1720
module_pci_driver(atp870u_driver);
L
Linus Torvalds 已提交
1721

O
Ondrej Zary 已提交
1722
static void atp_is(struct atp_unit *dev, unsigned char c, bool wide_chip, unsigned char lvdmode)
L
Linus Torvalds 已提交
1723
{
1724
	unsigned char i, j, k, rmb, n;
L
Linus Torvalds 已提交
1725 1726
	unsigned short int m;
	static unsigned char mbuf[512];
1727 1728 1729 1730 1731
	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 };
	unsigned char synu[6] = { 0x80, 1, 3, 1, 0x0a, 0x0e };
	static unsigned char synw[6] = { 0x80, 1, 3, 1, 0x19, 0x0e };
1732
	static unsigned char synw_870[6] = { 0x80, 1, 3, 1, 0x0c, 0x07 };
1733 1734 1735
	unsigned char synuw[6] = { 0x80, 1, 3, 1, 0x0a, 0x0e };
	static unsigned char wide[6] = { 0x80, 1, 2, 3, 1, 0 };
	static unsigned char u3[9] = { 0x80, 1, 6, 4, 0x09, 00, 0x0e, 0x01, 0x02 };
L
Linus Torvalds 已提交
1736 1737

	for (i = 0; i < 16; i++) {
1738 1739
		if (!wide_chip && (i > 7))
			break;
L
Linus Torvalds 已提交
1740 1741 1742 1743 1744 1745 1746 1747 1748
		m = 1;
		m = m << i;
		if ((m & dev->active_id[c]) != 0) {
			continue;
		}
		if (i == dev->host_id[c]) {
			printk(KERN_INFO "         ID: %2d  Host Adapter\n", dev->host_id[c]);
			continue;
		}
1749
		atp_writeb_io(dev, c, 0x1b, wide_chip ? 0x01 : 0x00);
1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762
		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]);
L
Linus Torvalds 已提交
1763 1764 1765 1766
		j = i;
		if ((j & 0x08) != 0) {
			j = (j & 0x07) | 0x40;
		}
1767 1768
		atp_writeb_io(dev, c, 0x15, j);
		atp_writeb_io(dev, c, 0x18, satn[8]);
L
Linus Torvalds 已提交
1769

1770
		while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00)
L
Linus Torvalds 已提交
1771
			cpu_relax();
1772 1773

		if (atp_readb_io(dev, c, 0x17) != 0x11 && atp_readb_io(dev, c, 0x17) != 0x8e)
L
Linus Torvalds 已提交
1774
			continue;
1775

1776
		while (atp_readb_io(dev, c, 0x17) != 0x8e)
L
Linus Torvalds 已提交
1777
			cpu_relax();
1778

L
Linus Torvalds 已提交
1779 1780
		dev->active_id[c] |= m;

1781
		atp_writeb_io(dev, c, 0x10, 0x30);
1782
		if (is885(dev) || is880(dev))
1783 1784 1785
			atp_writeb_io(dev, c, 0x14, 0x00);
		else /* result of is870() merge - is this a bug? */
			atp_writeb_io(dev, c, 0x04, 0x00);
L
Linus Torvalds 已提交
1786 1787

phase_cmd:
1788
		atp_writeb_io(dev, c, 0x18, 0x08);
1789

1790
		while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00)
L
Linus Torvalds 已提交
1791
			cpu_relax();
1792

1793
		j = atp_readb_io(dev, c, 0x17);
L
Linus Torvalds 已提交
1794
		if (j != 0x16) {
1795
			atp_writeb_io(dev, c, 0x10, 0x41);
L
Linus Torvalds 已提交
1796 1797 1798
			goto phase_cmd;
		}
sel_ok:
1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810
		atp_writeb_io(dev, c, 3, inqd[0]);
		atp_writeb_io(dev, c, 4, inqd[1]);
		atp_writeb_io(dev, c, 5, inqd[2]);
		atp_writeb_io(dev, c, 6, inqd[3]);
		atp_writeb_io(dev, c, 7, inqd[4]);
		atp_writeb_io(dev, c, 8, inqd[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, inqd[6]);
		atp_writeb_io(dev, c, 0x14, inqd[7]);
		atp_writeb_io(dev, c, 0x18, inqd[8]);
1811

1812
		while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00)
L
Linus Torvalds 已提交
1813
			cpu_relax();
1814 1815

		if (atp_readb_io(dev, c, 0x17) != 0x11 && atp_readb_io(dev, c, 0x17) != 0x8e)
L
Linus Torvalds 已提交
1816
			continue;
1817

1818
		while (atp_readb_io(dev, c, 0x17) != 0x8e)
L
Linus Torvalds 已提交
1819
			cpu_relax();
1820

1821 1822 1823
		if (wide_chip)
			atp_writeb_io(dev, c, 0x1b, 0x00);

1824
		atp_writeb_io(dev, c, 0x18, 0x08);
L
Linus Torvalds 已提交
1825 1826
		j = 0;
rd_inq_data:
1827
		k = atp_readb_io(dev, c, 0x1f);
L
Linus Torvalds 已提交
1828
		if ((k & 0x01) != 0) {
1829
			mbuf[j++] = atp_readb_io(dev, c, 0x19);
L
Linus Torvalds 已提交
1830 1831 1832 1833 1834
			goto rd_inq_data;
		}
		if ((k & 0x80) == 0) {
			goto rd_inq_data;
		}
1835
		j = atp_readb_io(dev, c, 0x17);
L
Linus Torvalds 已提交
1836 1837 1838
		if (j == 0x16) {
			goto inq_ok;
		}
1839 1840 1841 1842 1843
		atp_writeb_io(dev, c, 0x10, 0x46);
		atp_writeb_io(dev, c, 0x12, 0);
		atp_writeb_io(dev, c, 0x13, 0);
		atp_writeb_io(dev, c, 0x14, 0);
		atp_writeb_io(dev, c, 0x18, 0x08);
1844

1845
		while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00)
L
Linus Torvalds 已提交
1846
			cpu_relax();
1847 1848

		if (atp_readb_io(dev, c, 0x17) != 0x16)
L
Linus Torvalds 已提交
1849
			goto sel_ok;
1850

L
Linus Torvalds 已提交
1851 1852
inq_ok:
		mbuf[36] = 0;
1853
		printk(KERN_INFO "         ID: %2d  %s\n", i, &mbuf[8]);
L
Linus Torvalds 已提交
1854 1855 1856
		dev->id[c][i].devtype = mbuf[0];
		rmb = mbuf[1];
		n = mbuf[7];
1857 1858
		if (!wide_chip)
			goto not_wide;
L
Linus Torvalds 已提交
1859 1860 1861
		if ((mbuf[7] & 0x60) == 0) {
			goto not_wide;
		}
1862
		if (is885(dev) || is880(dev)) {
1863 1864 1865 1866 1867
			if ((i < 8) && ((dev->global_map[c] & 0x20) == 0))
				goto not_wide;
		} else { /* result of is870() merge - is this a bug? */
			if ((dev->global_map[c] & 0x20) == 0)
				goto not_wide;
L
Linus Torvalds 已提交
1868 1869
		}
		if (lvdmode == 0) {
1870
			goto chg_wide;
L
Linus Torvalds 已提交
1871
		}
1872 1873 1874
		if (dev->sp[c][i] != 0x04)	// force u2
		{
			goto chg_wide;
L
Linus Torvalds 已提交
1875 1876
		}

1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891
		atp_writeb_io(dev, c, 0x1b, 0x01);
		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]);
		atp_writeb_io(dev, c, 0x18, satn[8]);

		while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00)
L
Linus Torvalds 已提交
1892
			cpu_relax();
1893 1894

		if (atp_readb_io(dev, c, 0x17) != 0x11 && atp_readb_io(dev, c, 0x17) != 0x8e)
L
Linus Torvalds 已提交
1895
			continue;
1896

1897
		while (atp_readb_io(dev, c, 0x17) != 0x8e)
L
Linus Torvalds 已提交
1898
			cpu_relax();
1899

L
Linus Torvalds 已提交
1900 1901
try_u3:
		j = 0;
1902 1903
		atp_writeb_io(dev, c, 0x14, 0x09);
		atp_writeb_io(dev, c, 0x18, 0x20);
O
Ondrej Zary 已提交
1904

1905 1906 1907
		while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0) {
			if ((atp_readb_io(dev, c, 0x1f) & 0x01) != 0)
				atp_writeb_io(dev, c, 0x19, u3[j++]);
L
Linus Torvalds 已提交
1908 1909
			cpu_relax();
		}
1910

1911
		while ((atp_readb_io(dev, c, 0x17) & 0x80) == 0x00)
L
Linus Torvalds 已提交
1912
			cpu_relax();
1913

1914
		j = atp_readb_io(dev, c, 0x17) & 0x0f;
L
Linus Torvalds 已提交
1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925
		if (j == 0x0f) {
			goto u3p_in;
		}
		if (j == 0x0a) {
			goto u3p_cmd;
		}
		if (j == 0x0e) {
			goto try_u3;
		}
		continue;
u3p_out:
1926 1927 1928 1929
		atp_writeb_io(dev, c, 0x18, 0x20);
		while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0) {
			if ((atp_readb_io(dev, c, 0x1f) & 0x01) != 0)
				atp_writeb_io(dev, c, 0x19, 0);
L
Linus Torvalds 已提交
1930 1931
			cpu_relax();
		}
1932
		j = atp_readb_io(dev, c, 0x17) & 0x0f;
L
Linus Torvalds 已提交
1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943
		if (j == 0x0f) {
			goto u3p_in;
		}
		if (j == 0x0a) {
			goto u3p_cmd;
		}
		if (j == 0x0e) {
			goto u3p_out;
		}
		continue;
u3p_in:
1944 1945
		atp_writeb_io(dev, c, 0x14, 0x09);
		atp_writeb_io(dev, c, 0x18, 0x20);
L
Linus Torvalds 已提交
1946 1947
		k = 0;
u3p_in1:
1948
		j = atp_readb_io(dev, c, 0x1f);
L
Linus Torvalds 已提交
1949
		if ((j & 0x01) != 0) {
1950
			mbuf[k++] = atp_readb_io(dev, c, 0x19);
L
Linus Torvalds 已提交
1951 1952 1953 1954 1955
			goto u3p_in1;
		}
		if ((j & 0x80) == 0x00) {
			goto u3p_in1;
		}
1956
		j = atp_readb_io(dev, c, 0x17) & 0x0f;
L
Linus Torvalds 已提交
1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967
		if (j == 0x0f) {
			goto u3p_in;
		}
		if (j == 0x0a) {
			goto u3p_cmd;
		}
		if (j == 0x0e) {
			goto u3p_out;
		}
		continue;
u3p_cmd:
1968 1969 1970
		atp_writeb_io(dev, c, 0x10, 0x30);
		atp_writeb_io(dev, c, 0x14, 0x00);
		atp_writeb_io(dev, c, 0x18, 0x08);
1971

1972
		while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00);
1973

1974
		j = atp_readb_io(dev, c, 0x17);
L
Linus Torvalds 已提交
1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000
		if (j != 0x16) {
			if (j == 0x4e) {
				goto u3p_out;
			}
			continue;
		}
		if (mbuf[0] != 0x01) {
			goto chg_wide;
		}
		if (mbuf[1] != 0x06) {
			goto chg_wide;
		}
		if (mbuf[2] != 0x04) {
			goto chg_wide;
		}
		if (mbuf[3] == 0x09) {
			m = 1;
			m = m << i;
			dev->wide_id[c] |= m;
			dev->id[c][i].devsp = 0xce;
#ifdef ED_DBGP		   
			printk("dev->id[%2d][%2d].devsp = %2x\n",c,i,dev->id[c][i].devsp);
#endif
			continue;
		}
chg_wide:
2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015
		atp_writeb_io(dev, c, 0x1b, 0x01);
		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]);
		atp_writeb_io(dev, c, 0x18, satn[8]);

		while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00)
L
Linus Torvalds 已提交
2016
			cpu_relax();
2017 2018

		if (atp_readb_io(dev, c, 0x17) != 0x11 && atp_readb_io(dev, c, 0x17) != 0x8e)
L
Linus Torvalds 已提交
2019
			continue;
2020

2021
		while (atp_readb_io(dev, c, 0x17) != 0x8e)
L
Linus Torvalds 已提交
2022
			cpu_relax();
2023

L
Linus Torvalds 已提交
2024 2025
try_wide:
		j = 0;
2026 2027
		atp_writeb_io(dev, c, 0x14, 0x05);
		atp_writeb_io(dev, c, 0x18, 0x20);
O
Ondrej Zary 已提交
2028

2029 2030 2031
		while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0) {
			if ((atp_readb_io(dev, c, 0x1f) & 0x01) != 0)
				atp_writeb_io(dev, c, 0x19, wide[j++]);
L
Linus Torvalds 已提交
2032 2033
			cpu_relax();
		}
2034

2035
		while ((atp_readb_io(dev, c, 0x17) & 0x80) == 0x00)
L
Linus Torvalds 已提交
2036
			cpu_relax();
2037

2038
		j = atp_readb_io(dev, c, 0x17) & 0x0f;
L
Linus Torvalds 已提交
2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049
		if (j == 0x0f) {
			goto widep_in;
		}
		if (j == 0x0a) {
			goto widep_cmd;
		}
		if (j == 0x0e) {
			goto try_wide;
		}
		continue;
widep_out:
2050 2051 2052 2053
		atp_writeb_io(dev, c, 0x18, 0x20);
		while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0) {
			if ((atp_readb_io(dev, c, 0x1f) & 0x01) != 0)
				atp_writeb_io(dev, c, 0x19, 0);
L
Linus Torvalds 已提交
2054 2055
			cpu_relax();
		}
2056
		j = atp_readb_io(dev, c, 0x17) & 0x0f;
L
Linus Torvalds 已提交
2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067
		if (j == 0x0f) {
			goto widep_in;
		}
		if (j == 0x0a) {
			goto widep_cmd;
		}
		if (j == 0x0e) {
			goto widep_out;
		}
		continue;
widep_in:
2068 2069
		atp_writeb_io(dev, c, 0x14, 0xff);
		atp_writeb_io(dev, c, 0x18, 0x20);
L
Linus Torvalds 已提交
2070 2071
		k = 0;
widep_in1:
2072
		j = atp_readb_io(dev, c, 0x1f);
L
Linus Torvalds 已提交
2073
		if ((j & 0x01) != 0) {
2074
			mbuf[k++] = atp_readb_io(dev, c, 0x19);
L
Linus Torvalds 已提交
2075 2076 2077 2078 2079
			goto widep_in1;
		}
		if ((j & 0x80) == 0x00) {
			goto widep_in1;
		}
2080
		j = atp_readb_io(dev, c, 0x17) & 0x0f;
L
Linus Torvalds 已提交
2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091
		if (j == 0x0f) {
			goto widep_in;
		}
		if (j == 0x0a) {
			goto widep_cmd;
		}
		if (j == 0x0e) {
			goto widep_out;
		}
		continue;
widep_cmd:
2092 2093 2094
		atp_writeb_io(dev, c, 0x10, 0x30);
		atp_writeb_io(dev, c, 0x14, 0x00);
		atp_writeb_io(dev, c, 0x18, 0x08);
2095

2096
		while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00)
L
Linus Torvalds 已提交
2097
			cpu_relax();
2098

2099
		j = atp_readb_io(dev, c, 0x17);
L
Linus Torvalds 已提交
2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121
		if (j != 0x16) {
			if (j == 0x4e) {
				goto widep_out;
			}
			continue;
		}
		if (mbuf[0] != 0x01) {
			goto not_wide;
		}
		if (mbuf[1] != 0x02) {
			goto not_wide;
		}
		if (mbuf[2] != 0x03) {
			goto not_wide;
		}
		if (mbuf[3] != 0x01) {
			goto not_wide;
		}
		m = 1;
		m = m << i;
		dev->wide_id[c] |= m;
not_wide:
2122
		if ((dev->id[c][i].devtype == 0x00) || (dev->id[c][i].devtype == 0x07) || ((dev->id[c][i].devtype == 0x05) && ((n & 0x10) != 0))) {
L
Linus Torvalds 已提交
2123 2124 2125
			m = 1;
			m = m << i;
			if ((dev->async[c] & m) != 0) {
2126
				goto set_sync;
L
Linus Torvalds 已提交
2127 2128 2129 2130
			}
		}
		continue;
set_sync:
2131
		if ((!is885(dev) && !is880(dev)) || (dev->sp[c][i] == 0x02)) {
2132 2133
			synu[4] = 0x0c;
			synuw[4] = 0x0c;
L
Linus Torvalds 已提交
2134
		} else {
2135 2136 2137 2138
			if (dev->sp[c][i] >= 0x03) {
				synu[4] = 0x0a;
				synuw[4] = 0x0a;
			}
L
Linus Torvalds 已提交
2139 2140 2141 2142 2143
		}
		j = 0;
		if ((m & dev->wide_id[c]) != 0) {
			j |= 0x01;
		}
2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158
		atp_writeb_io(dev, c, 0x1b, j);
		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]);
		atp_writeb_io(dev, c, 0x18, satn[8]);

		while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00)
L
Linus Torvalds 已提交
2159
			cpu_relax();
2160 2161

		if (atp_readb_io(dev, c, 0x17) != 0x11 && atp_readb_io(dev, c, 0x17) != 0x8e)
L
Linus Torvalds 已提交
2162
			continue;
2163

2164
		while (atp_readb_io(dev, c, 0x17) != 0x8e)
L
Linus Torvalds 已提交
2165
			cpu_relax();
2166

L
Linus Torvalds 已提交
2167 2168
try_sync:
		j = 0;
2169 2170
		atp_writeb_io(dev, c, 0x14, 0x06);
		atp_writeb_io(dev, c, 0x18, 0x20);
O
Ondrej Zary 已提交
2171

2172 2173
		while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0) {
			if ((atp_readb_io(dev, c, 0x1f) & 0x01) != 0) {
L
Linus Torvalds 已提交
2174
				if ((m & dev->wide_id[c]) != 0) {
2175
					if (is885(dev) || is880(dev)) {
2176 2177 2178 2179 2180 2181 2182
						if ((m & dev->ultra_map[c]) != 0) {
							atp_writeb_io(dev, c, 0x19, synuw[j++]);
						} else {
							atp_writeb_io(dev, c, 0x19, synw[j++]);
						}
					} else
						atp_writeb_io(dev, c, 0x19, synw_870[j++]);
L
Linus Torvalds 已提交
2183 2184
				} else {
					if ((m & dev->ultra_map[c]) != 0) {
2185
						atp_writeb_io(dev, c, 0x19, synu[j++]);
L
Linus Torvalds 已提交
2186
					} else {
2187
						atp_writeb_io(dev, c, 0x19, synn[j++]);
L
Linus Torvalds 已提交
2188 2189 2190 2191
					}
				}
			}
		}
2192

2193
		while ((atp_readb_io(dev, c, 0x17) & 0x80) == 0x00)
L
Linus Torvalds 已提交
2194
			cpu_relax();
2195

2196
		j = atp_readb_io(dev, c, 0x17) & 0x0f;
L
Linus Torvalds 已提交
2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207
		if (j == 0x0f) {
			goto phase_ins;
		}
		if (j == 0x0a) {
			goto phase_cmds;
		}
		if (j == 0x0e) {
			goto try_sync;
		}
		continue;
phase_outs:
2208 2209 2210 2211
		atp_writeb_io(dev, c, 0x18, 0x20);
		while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00) {
			if ((atp_readb_io(dev, c, 0x1f) & 0x01) != 0x00)
				atp_writeb_io(dev, c, 0x19, 0x00);
L
Linus Torvalds 已提交
2212 2213
			cpu_relax();
		}
2214
		j = atp_readb_io(dev, c, 0x17);
L
Linus Torvalds 已提交
2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229
		if (j == 0x85) {
			goto tar_dcons;
		}
		j &= 0x0f;
		if (j == 0x0f) {
			goto phase_ins;
		}
		if (j == 0x0a) {
			goto phase_cmds;
		}
		if (j == 0x0e) {
			goto phase_outs;
		}
		continue;
phase_ins:
2230
		if (is885(dev) || is880(dev))
2231 2232 2233
			atp_writeb_io(dev, c, 0x14, 0x06);
		else
			atp_writeb_io(dev, c, 0x14, 0xff);
2234
		atp_writeb_io(dev, c, 0x18, 0x20);
L
Linus Torvalds 已提交
2235 2236
		k = 0;
phase_ins1:
2237
		j = atp_readb_io(dev, c, 0x1f);
L
Linus Torvalds 已提交
2238
		if ((j & 0x01) != 0x00) {
2239
			mbuf[k++] = atp_readb_io(dev, c, 0x19);
L
Linus Torvalds 已提交
2240 2241 2242 2243 2244
			goto phase_ins1;
		}
		if ((j & 0x80) == 0x00) {
			goto phase_ins1;
		}
2245

2246
		while ((atp_readb_io(dev, c, 0x17) & 0x80) == 0x00);
2247

2248
		j = atp_readb_io(dev, c, 0x17);
L
Linus Torvalds 已提交
2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263
		if (j == 0x85) {
			goto tar_dcons;
		}
		j &= 0x0f;
		if (j == 0x0f) {
			goto phase_ins;
		}
		if (j == 0x0a) {
			goto phase_cmds;
		}
		if (j == 0x0e) {
			goto phase_outs;
		}
		continue;
phase_cmds:
2264
		atp_writeb_io(dev, c, 0x10, 0x30);
L
Linus Torvalds 已提交
2265
tar_dcons:
2266 2267
		atp_writeb_io(dev, c, 0x14, 0x00);
		atp_writeb_io(dev, c, 0x18, 0x08);
2268

2269
		while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00)
L
Linus Torvalds 已提交
2270
			cpu_relax();
2271

2272
		j = atp_readb_io(dev, c, 0x17);
L
Linus Torvalds 已提交
2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287
		if (j != 0x16) {
			continue;
		}
		if (mbuf[0] != 0x01) {
			continue;
		}
		if (mbuf[1] != 0x03) {
			continue;
		}
		if (mbuf[4] == 0x00) {
			continue;
		}
		if (mbuf[3] > 0x64) {
			continue;
		}
2288
		if (is885(dev) || is880(dev)) {
2289 2290 2291 2292 2293 2294 2295
			if (mbuf[4] > 0x0e) {
				mbuf[4] = 0x0e;
			}
		} else {
			if (mbuf[4] > 0x0c) {
				mbuf[4] = 0x0c;
			}
L
Linus Torvalds 已提交
2296 2297
		}
		dev->id[c][i].devsp = mbuf[4];
2298
		if (is885(dev) || is880(dev))
2299 2300 2301 2302
			if (mbuf[3] < 0x0c) {
				j = 0xb0;
				goto set_syn_ok;
			}
L
Linus Torvalds 已提交
2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319
		if ((mbuf[3] < 0x0d) && (rmb == 0)) {
			j = 0xa0;
			goto set_syn_ok;
		}
		if (mbuf[3] < 0x1a) {
			j = 0x20;
			goto set_syn_ok;
		}
		if (mbuf[3] < 0x33) {
			j = 0x40;
			goto set_syn_ok;
		}
		if (mbuf[3] < 0x4c) {
			j = 0x50;
			goto set_syn_ok;
		}
		j = 0x60;
2320
set_syn_ok:
L
Linus Torvalds 已提交
2321
		dev->id[c][i].devsp = (dev->id[c][i].devsp & 0x0f) | j;
2322
#ifdef ED_DBGP
L
Linus Torvalds 已提交
2323 2324 2325 2326
		printk("dev->id[%2d][%2d].devsp = %2x\n",c,i,dev->id[c][i].devsp);
#endif
	}
}