atp870u.c 81.5 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 44 45 46
#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);
static void is885(struct atp_unit *dev, unsigned int wkport,unsigned char c);
static void tscam_885(void);

47
static irqreturn_t atp870u_intr_handle(int irq, void *dev_id)
L
Linus Torvalds 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
{
	unsigned long flags;
	unsigned short int tmpcip, id;
	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
	int errstus;
	struct Scsi_Host *host = dev_id;
	struct atp_unit *dev = (struct atp_unit *)&host->hostdata;

	for (c = 0; c < 2; c++) {
O
Ondrej Zary 已提交
63
		j = inb(dev->ioport[c] + 0x1f);
L
Linus Torvalds 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
		if ((j & 0x80) != 0)
		{			
	   		goto ch_sel;
		}
		dev->in_int[c] = 0;
	}
	return IRQ_NONE;
ch_sel:
#ifdef ED_DBGP	
	printk("atp870u_intr_handle enter\n");
#endif	
	dev->in_int[c] = 1;
	cmdp = inb(dev->ioport[c] + 0x10);
	if (dev->working[c] != 0) {
		if (dev->dev_id == ATP885_DEVID) {
O
Ondrej Zary 已提交
79 80
			if ((inb(dev->ioport[c] + 0x16) & 0x80) == 0)
				outb((inb(dev->ioport[c] + 0x16) | 0x80), dev->ioport[c] + 0x16);
L
Linus Torvalds 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
		}		
		tmpcip = dev->pciport[c];
		if ((inb(tmpcip) & 0x08) != 0)
		{
			tmpcip += 0x2;
			for (k=0; k < 1000; k++) {
				if ((inb(tmpcip) & 0x08) == 0) {
					goto stop_dma;
				}
				if ((inb(tmpcip) & 0x01) == 0) {
					goto stop_dma;
				}
			}
		}
stop_dma:
		tmpcip = dev->pciport[c];
		outb(0x00, tmpcip);
		
O
Ondrej Zary 已提交
99
		i = inb(dev->ioport[c] + 0x17);
L
Linus Torvalds 已提交
100 101 102 103 104 105 106
		
		if (dev->dev_id == ATP885_DEVID) {
			tmpcip += 2;
			outb(0x06, tmpcip);
			tmpcip -= 2;
		}

O
Ondrej Zary 已提交
107
		target_id = inb(dev->ioport[c] + 0x15);
L
Linus Torvalds 已提交
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135

		/*
		 *	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;
O
Ondrej Zary 已提交
136 137 138
				((unsigned char *) &adrcnt)[2] = inb(dev->ioport[c] + 0x12);
				((unsigned char *) &adrcnt)[1] = inb(dev->ioport[c] + 0x13);
				((unsigned char *) &adrcnt)[0] = inb(dev->ioport[c] + 0x14);
L
Linus Torvalds 已提交
139 140 141 142 143 144 145 146
				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 已提交
147
				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 已提交
148 149 150 151 152 153 154
#endif		
			}

			/*
			 *      Flip wide
			 */			
			if (dev->wide_id[c] != 0) {
O
Ondrej Zary 已提交
155 156 157
				outb(0x01, dev->ioport[c] + 0x1b);
				while ((inb(dev->ioport[c] + 0x1b) & 0x01) != 0x01) {
					outb(0x01, dev->ioport[c] + 0x1b);
L
Linus Torvalds 已提交
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
				}
			}		
			/*
			 *	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				
			goto handled;
		}

		if (i == 0x40) {
		     dev->last_cmd[c] |= 0x40;
		     dev->in_int[c] = 0;
		     goto handled;
		}

		if (i == 0x21) {
			if ((dev->last_cmd[c] & 0xf0) != 0x40) {
			   dev->last_cmd[c] = 0xff;
			}
			adrcnt = 0;
O
Ondrej Zary 已提交
193 194 195
			((unsigned char *) &adrcnt)[2] = inb(dev->ioport[c] + 0x12);
			((unsigned char *) &adrcnt)[1] = inb(dev->ioport[c] + 0x13);
			((unsigned char *) &adrcnt)[0] = inb(dev->ioport[c] + 0x14);
L
Linus Torvalds 已提交
196 197 198 199
			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;
O
Ondrej Zary 已提交
200 201
			outb(0x41, dev->ioport[c] + 0x10);
			outb(0x08, dev->ioport[c] + 0x18);
L
Linus Torvalds 已提交
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
			dev->in_int[c] = 0;
			goto handled;
		}

		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;
			if (cmdp == 0x44 || i==0x80) {
O
Ondrej Zary 已提交
221
				lun = inb(dev->ioport[c] + 0x1d) & 0x07;
L
Linus Torvalds 已提交
222 223 224 225 226 227 228 229 230
			} else {
				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;
O
Ondrej Zary 已提交
231 232 233
					((unsigned char *) &adrcnt)[2] = inb(dev->ioport[c] + 0x12);
					((unsigned char *) &adrcnt)[1] = inb(dev->ioport[c] + 0x13);
					((unsigned char *) &adrcnt)[0] = inb(dev->ioport[c] + 0x14);
L
Linus Torvalds 已提交
234 235 236 237
					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;
O
Ondrej Zary 已提交
238
					outb(0x08, dev->ioport[c] + 0x18);
L
Linus Torvalds 已提交
239 240 241 242 243 244
					dev->in_int[c] = 0;
					goto handled;
				} else {
#ifdef ED_DBGP
					printk("cmdp != 0x41\n");
#endif						
O
Ondrej Zary 已提交
245
					outb(0x46, dev->ioport[c] + 0x10);
L
Linus Torvalds 已提交
246
					dev->id[c][target_id].dirct = 0x00;
O
Ondrej Zary 已提交
247 248 249 250
					outb(0x00, dev->ioport[c] + 0x12);
					outb(0x00, dev->ioport[c] + 0x13);
					outb(0x00, dev->ioport[c] + 0x14);
					outb(0x08, dev->ioport[c] + 0x18);
L
Linus Torvalds 已提交
251 252 253 254 255 256 257 258 259 260
					dev->in_int[c] = 0;
					goto handled;
				}
			}
			if (dev->last_cmd[c] != 0xff) {
			   dev->last_cmd[c] |= 0x40;
			}
			if (dev->dev_id == ATP885_DEVID) {
				j = inb(dev->baseport + 0x29) & 0xfe;
				outb(j, dev->baseport + 0x29);
O
Ondrej Zary 已提交
261 262 263 264
			} else
				outb(0x45, dev->ioport[c] + 0x10);

			target_id = inb(dev->ioport[c] + 0x16);
L
Linus Torvalds 已提交
265 266 267 268 269 270 271 272
			/*
			 *	Remap wide identifiers
			 */
			if ((target_id & 0x10) != 0) {
				target_id = (target_id & 0x07) | 0x08;
			} else {
				target_id &= 0x07;
			}
O
Ondrej Zary 已提交
273 274
			if (dev->dev_id == ATP885_DEVID)
				outb(0x45, dev->ioport[c] + 0x10);
L
Linus Torvalds 已提交
275 276
			workreq = dev->id[c][target_id].curr_req;
#ifdef ED_DBGP			
J
Jeff Garzik 已提交
277 278
			scmd_printk(KERN_DEBUG, workreq, "CDB");
			for (l = 0; l < workreq->cmd_len; l++)
L
Linus Torvalds 已提交
279
				printk(KERN_DEBUG " %x",workreq->cmnd[l]);
J
Jeff Garzik 已提交
280
			printk("\n");
L
Linus Torvalds 已提交
281 282
#endif	
			
O
Ondrej Zary 已提交
283 284
			outb(lun, dev->ioport[c] + 0x0f);
			outb(dev->id[c][target_id].devsp, dev->ioport[c] + 0x11);
L
Linus Torvalds 已提交
285 286 287
			adrcnt = dev->id[c][target_id].tran_len;
			k = dev->id[c][target_id].last_len;

O
Ondrej Zary 已提交
288 289 290
			outb(((unsigned char *) &k)[2], dev->ioport[c] + 0x12);
			outb(((unsigned char *) &k)[1], dev->ioport[c] + 0x13);
			outb(((unsigned char *) &k)[0], dev->ioport[c] + 0x14);
L
Linus Torvalds 已提交
291
#ifdef ED_DBGP			
O
Ondrej Zary 已提交
292
			printk("k %x, k[0] 0x%x k[1] 0x%x k[2] 0x%x\n", k, inb(dev->ioport[c] + 0x14), inb(dev->ioport[c] + 0x13), inb(dev->ioport[c] + 0x12));
L
Linus Torvalds 已提交
293 294 295 296 297 298 299 300
#endif			
			/* Remap wide */
			j = target_id;
			if (target_id > 7) {
				j = (j & 0x07) | 0x40;
			}
			/* Add direction */
			j |= dev->id[c][target_id].dirct;
O
Ondrej Zary 已提交
301 302
			outb(j, dev->ioport[c] + 0x15);
			outb(0x80, dev->ioport[c] + 0x16);
L
Linus Torvalds 已提交
303 304 305 306 307 308 309 310 311 312 313 314 315
			
			/* enable 32 bit fifo transfer */	
			if (dev->dev_id == ATP885_DEVID) {
				tmpcip = dev->pciport[c] + 1;
				i=inb(tmpcip) & 0xf3;
				//j=workreq->cmnd[0];	    		    	
				if ((workreq->cmnd[0] == 0x08) || (workreq->cmnd[0] == 0x28) || (workreq->cmnd[0] == 0x0a) || (workreq->cmnd[0] == 0x2a)) {
				   i |= 0x0c;
				}
				outb(i,tmpcip);		    		    		
			} else if ((dev->dev_id == ATP880_DEVID1) ||
	    		    	   (dev->dev_id == ATP880_DEVID2) ) {
				if ((workreq->cmnd[0] == 0x08) || (workreq->cmnd[0] == 0x28) || (workreq->cmnd[0] == 0x0a) || (workreq->cmnd[0] == 0x2a)) {
O
Ondrej Zary 已提交
316
					outb((unsigned char) ((inb(dev->ioport[c] - 0x05) & 0x3f) | 0xc0), dev->ioport[c] - 0x05);///minus 0x05???
L
Linus Torvalds 已提交
317
				} else {
O
Ondrej Zary 已提交
318
					outb((unsigned char) (inb(dev->ioport[c] - 0x05) & 0x3f), dev->ioport[c] - 0x05);///minus 0x05???
L
Linus Torvalds 已提交
319 320 321
				}
			} else {				
				if ((workreq->cmnd[0] == 0x08) || (workreq->cmnd[0] == 0x28) || (workreq->cmnd[0] == 0x0a) || (workreq->cmnd[0] == 0x2a)) {
O
Ondrej Zary 已提交
322
					outb((unsigned char) ((inb(dev->ioport[c] + 0x3a) & 0xf3) | 0x08), dev->ioport[c] + 0x3a);
L
Linus Torvalds 已提交
323
				} else {
O
Ondrej Zary 已提交
324
					outb((unsigned char) (inb(dev->ioport[c] + 0x3a) & 0xf3), dev->ioport[c] + 0x3a);
L
Linus Torvalds 已提交
325 326 327 328 329 330 331 332 333 334 335
				}														
			}	
			j = 0;
			id = 1;
			id = id << target_id;
			/*
			 *	Is this a wide device
			 */
			if ((id & dev->wide_id[c]) != 0) {
				j |= 0x01;
			}
O
Ondrej Zary 已提交
336 337 338
			outb(j, dev->ioport[c] + 0x1b);
			while ((inb(dev->ioport[c] + 0x1b) & 0x01) != j) {
				outb(j,dev->ioport[c] + 0x1b);
L
Linus Torvalds 已提交
339 340
			}
			if (dev->id[c][target_id].last_len == 0) {
O
Ondrej Zary 已提交
341
				outb(0x08, dev->ioport[c] + 0x18);
L
Linus Torvalds 已提交
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
				dev->in_int[c] = 0;
#ifdef ED_DBGP
				printk("dev->id[c][target_id].last_len = 0\n");
#endif					
				goto handled;
			}
#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;
					}
				}				
			}
			tmpcip = dev->pciport[c] + 0x04;
			outl(dev->id[c][target_id].prdaddr, tmpcip);
#ifdef ED_DBGP
			printk("dev->id[%d][%d].prdaddr 0x%8x\n", c, target_id, dev->id[c][target_id].prdaddr);
#endif
			if (dev->dev_id == ATP885_DEVID) {
				tmpcip -= 0x04;
			} else {
				tmpcip -= 0x02;
				outb(0x06, tmpcip);
				outb(0x00, tmpcip);
				tmpcip -= 0x02;
			}
			/*
			 *	Check transfer direction
			 */
			if (dev->id[c][target_id].dirct != 0) {
O
Ondrej Zary 已提交
391
				outb(0x08, dev->ioport[c] + 0x18);
L
Linus Torvalds 已提交
392 393 394 395 396 397 398
				outb(0x01, tmpcip);
				dev->in_int[c] = 0;
#ifdef ED_DBGP
				printk("status 0x80 return dirct != 0\n");
#endif				
				goto handled;
			}
O
Ondrej Zary 已提交
399
			outb(0x08, dev->ioport[c] + 0x18);
L
Linus Torvalds 已提交
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 426 427
			outb(0x09, tmpcip);
			dev->in_int[c] = 0;
#ifdef ED_DBGP
			printk("status 0x80 return dirct = 0\n");
#endif			
			goto handled;
		}

		/*
		 *	Current scsi request on this target
		 */

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

		if (i == 0x42) {
			if ((dev->last_cmd[c] & 0xf0) != 0x40)
			{
			   dev->last_cmd[c] = 0xff;
			}
			errstus = 0x02;
			workreq->result = errstus;
			goto go_42;
		}
		if (i == 0x16) {
			if ((dev->last_cmd[c] & 0xf0) != 0x40) {
			   dev->last_cmd[c] = 0xff;
			}
			errstus = 0;
O
Ondrej Zary 已提交
428
			errstus = inb(dev->ioport[c] + 0x0f);
L
Linus Torvalds 已提交
429 430 431 432 433 434 435 436 437 438 439 440 441
			if (((dev->r1f[c][target_id] & 0x10) != 0)&&(dev->dev_id==ATP885_DEVID)) {
			   printk(KERN_WARNING "AEC67162 CRC ERROR !\n");
			   errstus = 0x02;
			}
			workreq->result = errstus;
go_42:
			if (dev->dev_id == ATP885_DEVID) {		
				j = inb(dev->baseport + 0x29) | 0x01;
				outb(j, dev->baseport + 0x29);
			}
			/*
			 *	Complete the command
			 */
442 443
			scsi_dma_unmap(workreq);

L
Linus Torvalds 已提交
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
			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) {
O
Ondrej Zary 已提交
459 460 461
				outb(0x01, dev->ioport[c] + 0x1b);
				while ((inb(dev->ioport[c] + 0x1b) & 0x01) != 0x01) {
					outb(0x01, dev->ioport[c] + 0x1b);
L
Linus Torvalds 已提交
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
				}       
			} 
			/*
			 *	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;
			goto handled;
		}
		if ((dev->last_cmd[c] & 0xf0) != 0x40) {
		   dev->last_cmd[c] = 0xff;
		}
		if (i == 0x4f) {
			i = 0x89;
		}
		i &= 0x0f;
		if (i == 0x09) {
			tmpcip += 4;
			outl(dev->id[c][target_id].prdaddr, tmpcip);
			tmpcip = tmpcip - 2;
			outb(0x06, tmpcip);
			outb(0x00, tmpcip);
			tmpcip = tmpcip - 2;
O
Ondrej Zary 已提交
493
			outb(0x41, dev->ioport[c] + 0x10);
L
Linus Torvalds 已提交
494 495
			if (dev->dev_id == ATP885_DEVID) {
				k = dev->id[c][target_id].last_len;
O
Ondrej Zary 已提交
496 497 498
				outb((unsigned char) (((unsigned char *) (&k))[2]), dev->ioport[c] + 0x12);
				outb((unsigned char) (((unsigned char *) (&k))[1]), dev->ioport[c] + 0x13);
				outb((unsigned char) (((unsigned char *) (&k))[0]), dev->ioport[c] + 0x14);
L
Linus Torvalds 已提交
499 500 501 502
				dev->id[c][target_id].dirct = 0x00;
			} else {
				dev->id[c][target_id].dirct = 0x00;
			}
O
Ondrej Zary 已提交
503
			outb(0x08, dev->ioport[c] + 0x18);
L
Linus Torvalds 已提交
504 505 506 507 508 509 510 511 512 513 514
			outb(0x09, tmpcip);
			dev->in_int[c] = 0;
			goto handled;
		}
		if (i == 0x08) {
			tmpcip += 4;
			outl(dev->id[c][target_id].prdaddr, tmpcip);
			tmpcip = tmpcip - 2;
			outb(0x06, tmpcip);
			outb(0x00, tmpcip);
			tmpcip = tmpcip - 2;
O
Ondrej Zary 已提交
515
			outb(0x41, dev->ioport[c] + 0x10);
L
Linus Torvalds 已提交
516 517
			if (dev->dev_id == ATP885_DEVID) {		
				k = dev->id[c][target_id].last_len;
O
Ondrej Zary 已提交
518 519 520
				outb((unsigned char) (((unsigned char *) (&k))[2]), dev->ioport[c] + 0x12);
				outb((unsigned char) (((unsigned char *) (&k))[1]), dev->ioport[c] + 0x13);
				outb((unsigned char) (((unsigned char *) (&k))[0]), dev->ioport[c] + 0x14);
L
Linus Torvalds 已提交
521
			}
O
Ondrej Zary 已提交
522
			outb((unsigned char) (inb(dev->ioport[c] + 0x15) | 0x20), dev->ioport[c] + 0x15);
L
Linus Torvalds 已提交
523
			dev->id[c][target_id].dirct = 0x20;
O
Ondrej Zary 已提交
524
			outb(0x08, dev->ioport[c] + 0x18);
L
Linus Torvalds 已提交
525 526 527 528 529
			outb(0x01, tmpcip);
			dev->in_int[c] = 0;
			goto handled;
		}
		if (i == 0x0a) {
O
Ondrej Zary 已提交
530
			outb(0x30, dev->ioport[c] + 0x10);
L
Linus Torvalds 已提交
531
		} else {
O
Ondrej Zary 已提交
532
			outb(0x46, dev->ioport[c] + 0x10);
L
Linus Torvalds 已提交
533 534
		}
		dev->id[c][target_id].dirct = 0x00;
O
Ondrej Zary 已提交
535 536 537 538
		outb(0x00, dev->ioport[c] + 0x12);
		outb(0x00, dev->ioport[c] + 0x13);
		outb(0x00, dev->ioport[c] + 0x14);
		outb(0x08, dev->ioport[c] + 0x18);
L
Linus Torvalds 已提交
539 540 541
		dev->in_int[c] = 0;
		goto handled;
	} else {
O
Ondrej Zary 已提交
542
//		inb(dev->ioport[c] + 0x17);
L
Linus Torvalds 已提交
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
//		dev->working[c] = 0;
		dev->in_int[c] = 0;
		goto handled;
	}
	
handled:
#ifdef ED_DBGP
	printk("atp870u_intr_handle exit\n");
#endif			
	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 已提交
561
static int atp870u_queuecommand_lck(struct scsi_cmnd *req_p,
L
Linus Torvalds 已提交
562 563 564
			 void (*done) (struct scsi_cmnd *))
{
	unsigned char c;
O
Ondrej Zary 已提交
565
	unsigned int m;
L
Linus Torvalds 已提交
566 567 568
	struct atp_unit *dev;
	struct Scsi_Host *host;

569
	c = scmd_channel(req_p);
L
Linus Torvalds 已提交
570
	req_p->sense_buffer[0]=0;
571
	scsi_set_resid(req_p, 0);
572
	if (scmd_channel(req_p) > 1) {
L
Linus Torvalds 已提交
573 574 575 576 577 578 579 580 581 582 583 584 585 586
		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;
587
	m = m << scmd_id(req_p);
L
Linus Torvalds 已提交
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 632 633 634

	/*
	 *      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	
O
Ondrej Zary 已提交
635
	printk("dev->ioport[c] = %x inb(dev->ioport[c] + 0x1c) = %x dev->in_int[%d] = %d dev->in_snd[%d] = %d\n",dev->ioport[c],inb(dev->ioport[c] + 0x1c),c,dev->in_int[c],c,dev->in_snd[c]);
L
Linus Torvalds 已提交
636
#endif
O
Ondrej Zary 已提交
637
	if ((inb(dev->ioport[c] + 0x1c) == 0) && (dev->in_int[c] == 0) && (dev->in_snd[c] == 0)) {
L
Linus Torvalds 已提交
638 639 640 641 642 643 644 645 646 647 648
#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 已提交
649 650
static DEF_SCSI_QCMD(atp870u_queuecommand)

L
Linus Torvalds 已提交
651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702
/**
 *	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)
{
	struct scsi_cmnd *workreq;
	unsigned int i;//,k;
	unsigned char  j, target_id;
	unsigned char *prd;
	unsigned short int tmpcip, w;
	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;
		if (workreq != NULL) {	/* check NULL pointer */
		   goto cmd_subp;
		}
		dev->last_cmd[c] = 0xff;	
		if (dev->quhd[c] == dev->quend[c]) {
		   	dev->in_snd[c] = 0;
		   	return ;
		}
	}
	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]];
703
	if (dev->id[c][scmd_id(workreq)].curr_req == NULL) {
704 705
		dev->id[c][scmd_id(workreq)].curr_req = workreq;
		dev->last_cmd[c] = scmd_id(workreq);
L
Linus Torvalds 已提交
706 707 708 709 710 711 712
		goto cmd_subp;
	}	
	dev->quhd[c] = j;
	dev->working[c]--;
	dev->in_snd[c] = 0;
	return;
cmd_subp:
O
Ondrej Zary 已提交
713
	if ((inb(dev->ioport[c] + 0x1f) & 0xb0) != 0) {
L
Linus Torvalds 已提交
714 715
		goto abortsnd;
	}
O
Ondrej Zary 已提交
716
	if (inb(dev->ioport[c] + 0x1c) == 0) {
L
Linus Torvalds 已提交
717 718 719 720 721 722 723 724 725 726 727 728
		goto oktosend;
	}
abortsnd:
#ifdef ED_DBGP
	printk("Abort to Send\n");
#endif
	dev->last_cmd[c] |= 0x40;
	dev->in_snd[c] = 0;
	return;
oktosend:
#ifdef ED_DBGP
	printk("OK to Send\n");
729
	scmd_printk(KERN_DEBUG, workreq, "CDB");
L
Linus Torvalds 已提交
730 731 732
	for(i=0;i<workreq->cmd_len;i++) {
		printk(" %x",workreq->cmnd[i]);
	}
733
	printk("\n");
L
Linus Torvalds 已提交
734
#endif	
735 736
	l = scsi_bufflen(workreq);

L
Linus Torvalds 已提交
737 738 739
	if (dev->dev_id == ATP885_DEVID) {
		j = inb(dev->baseport + 0x29) & 0xfe;
		outb(j, dev->baseport + 0x29);
740
		dev->r1f[c][scmd_id(workreq)] = 0;
L
Linus Torvalds 已提交
741 742 743
	}
	
	if (workreq->cmnd[0] == READ_CAPACITY) {
744 745
		if (l > 8)
			l = 8;
L
Linus Torvalds 已提交
746 747
	}
	if (workreq->cmnd[0] == 0x00) {
748
		l = 0;
L
Linus Torvalds 已提交
749 750 751
	}

	j = 0;
752
	target_id = scmd_id(workreq);
L
Linus Torvalds 已提交
753 754 755 756 757 758 759 760 761

	/*
	 *	Wide ?
	 */
	w = 1;
	w = w << target_id;
	if ((w & dev->wide_id[c]) != 0) {
		j |= 0x01;
	}
O
Ondrej Zary 已提交
762 763 764
	outb(j, dev->ioport[c] + 0x1b);
	while ((inb(dev->ioport[c] + 0x1b) & 0x01) != j) {
		outb(j,dev->ioport[c] + 0x1b);
L
Linus Torvalds 已提交
765 766 767 768 769 770 771 772
#ifdef ED_DBGP
		printk("send_s870 while loop 1\n");
#endif
	}
	/*
	 *	Write the command
	 */

O
Ondrej Zary 已提交
773 774
	outb(workreq->cmd_len, dev->ioport[c] + 0x00);
	outb(0x2c, dev->ioport[c] + 0x01);
L
Linus Torvalds 已提交
775
	if (dev->dev_id == ATP885_DEVID) {
O
Ondrej Zary 已提交
776
		outb(0x7f, dev->ioport[c] + 0x02);
L
Linus Torvalds 已提交
777
	} else {
O
Ondrej Zary 已提交
778
		outb(0xcf, dev->ioport[c] + 0x02);
L
Linus Torvalds 已提交
779 780
	}	
	for (i = 0; i < workreq->cmd_len; i++) {
O
Ondrej Zary 已提交
781
		outb(workreq->cmnd[i], dev->ioport[c] + 0x03 + i);
L
Linus Torvalds 已提交
782
	}
O
Ondrej Zary 已提交
783
	outb(workreq->device->lun, dev->ioport[c] + 0x0f);
L
Linus Torvalds 已提交
784 785 786
	/*
	 *	Write the target
	 */
O
Ondrej Zary 已提交
787
	outb(dev->id[c][target_id].devsp, dev->ioport[c] + 0x11);
L
Linus Torvalds 已提交
788 789 790
#ifdef ED_DBGP	
	printk("dev->id[%d][%d].devsp = %2x\n",c,target_id,dev->id[c][target_id].devsp);
#endif
791 792

	sg_count = scsi_dma_map(workreq);
L
Linus Torvalds 已提交
793 794 795
	/*
	 *	Write transfer size
	 */
O
Ondrej Zary 已提交
796 797 798
	outb((unsigned char) (((unsigned char *) (&l))[2]), dev->ioport[c] + 0x12);
	outb((unsigned char) (((unsigned char *) (&l))[1]), dev->ioport[c] + 0x13);
	outb((unsigned char) (((unsigned char *) (&l))[0]), dev->ioport[c] + 0x14);
L
Linus Torvalds 已提交
799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814
	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
	 */
	if (workreq->sc_data_direction == DMA_TO_DEVICE) {
O
Ondrej Zary 已提交
815
		outb((unsigned char) (j | 0x20), dev->ioport[c] + 0x15);
L
Linus Torvalds 已提交
816
	} else {
O
Ondrej Zary 已提交
817
		outb(j, dev->ioport[c] + 0x15);
L
Linus Torvalds 已提交
818
	}
O
Ondrej Zary 已提交
819 820
	outb((unsigned char) (inb(dev->ioport[c] + 0x16) | 0x80), dev->ioport[c] + 0x16);
	outb(0x80, dev->ioport[c] + 0x16);
L
Linus Torvalds 已提交
821 822
	dev->id[c][target_id].dirct = 0;
	if (l == 0) {
O
Ondrej Zary 已提交
823
		if (inb(dev->ioport[c] + 0x1c) == 0) {
L
Linus Torvalds 已提交
824 825 826
#ifdef ED_DBGP
			printk("change SCSI_CMD_REG 0x08\n");	
#endif				
O
Ondrej Zary 已提交
827
			outb(0x08, dev->ioport[c] + 0x18);
L
Linus Torvalds 已提交
828 829 830 831 832 833 834 835 836 837 838 839 840 841 842
		} else {
			dev->last_cmd[c] |= 0x40;
		}
		dev->in_snd[c] = 0;
		return;
	}
	tmpcip = dev->pciport[c];
	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.
	 */

843 844
	if (l) {
		struct scatterlist *sgpnt;
L
Linus Torvalds 已提交
845
		i = 0;
846 847 848
		scsi_for_each_sg(workreq, sgpnt, sg_count, j) {
			bttl = sg_dma_address(sgpnt);
			l=sg_dma_len(sgpnt);
L
Linus Torvalds 已提交
849
#ifdef ED_DBGP		
850
			printk("1. bttl %x, l %x\n",bttl, l);
L
Linus Torvalds 已提交
851
#endif			
852
			while (l > 0x10000) {
L
Linus Torvalds 已提交
853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874
				(((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			
	}
	tmpcip += 4;
#ifdef ED_DBGP		
	printk("send_s870: prdaddr_2 0x%8x tmpcip %x target_id %d\n", dev->id[c][target_id].prdaddr,tmpcip,target_id);
#endif	
875
	dev->id[c][target_id].prdaddr = dev->id[c][target_id].prd_bus;
L
Linus Torvalds 已提交
876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892
	outl(dev->id[c][target_id].prdaddr, tmpcip);
	tmpcip = tmpcip - 2;
	outb(0x06, tmpcip);
	outb(0x00, tmpcip);
	if (dev->dev_id == ATP885_DEVID) {
		tmpcip--;
		j=inb(tmpcip) & 0xf3;
		if ((workreq->cmnd[0] == 0x08) || (workreq->cmnd[0] == 0x28) ||
	    	(workreq->cmnd[0] == 0x0a) || (workreq->cmnd[0] == 0x2a)) {
	   		j |= 0x0c;
		}
		outb(j,tmpcip);
		tmpcip--;	    	
	} else if ((dev->dev_id == ATP880_DEVID1) ||
	    	   (dev->dev_id == ATP880_DEVID2)) {
		tmpcip =tmpcip -2;	
		if ((workreq->cmnd[0] == 0x08) || (workreq->cmnd[0] == 0x28) || (workreq->cmnd[0] == 0x0a) || (workreq->cmnd[0] == 0x2a)) {
O
Ondrej Zary 已提交
893
			outb((unsigned char) ((inb(dev->ioport[c] - 0x05) & 0x3f) | 0xc0), dev->ioport[c] - 0x05);
L
Linus Torvalds 已提交
894
		} else {
O
Ondrej Zary 已提交
895
			outb((unsigned char) (inb(dev->ioport[c] - 0x05) & 0x3f), dev->ioport[c] - 0x05);
L
Linus Torvalds 已提交
896 897 898 899
		}		
	} else {		
		tmpcip =tmpcip -2;
		if ((workreq->cmnd[0] == 0x08) || (workreq->cmnd[0] == 0x28) || (workreq->cmnd[0] == 0x0a) || (workreq->cmnd[0] == 0x2a)) {
O
Ondrej Zary 已提交
900
			outb((inb(dev->ioport[c] + 0x3a) & 0xf3) | 0x08, dev->ioport[c] + 0x3a);
L
Linus Torvalds 已提交
901
		} else {
O
Ondrej Zary 已提交
902
			outb(inb(dev->ioport[c] + 0x3a) & 0xf3, dev->ioport[c] + 0x3a);
L
Linus Torvalds 已提交
903 904 905 906 907
		}		
	}	

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

O
Ondrej Zary 已提交
939
	outw(*val, dev->ioport[0] + 0x1c);
L
Linus Torvalds 已提交
940 941
FUN_D7:
	for (i = 0; i < 10; i++) {	/* stable >= bus settle delay(400 ns)  */
O
Ondrej Zary 已提交
942
		k = inw(dev->ioport[0] + 0x1c);
L
Linus Torvalds 已提交
943 944 945 946 947 948
		j = (unsigned char) (k >> 8);
		if ((k & 0x8000) != 0) {	/* DB7 all release?    */
			goto FUN_D7;
		}
	}
	*val |= 0x4000;		/* assert DB6           */
O
Ondrej Zary 已提交
949
	outw(*val, dev->ioport[0] + 0x1c);
L
Linus Torvalds 已提交
950
	*val &= 0xdfff;		/* assert DB5           */
O
Ondrej Zary 已提交
951
	outw(*val, dev->ioport[0] + 0x1c);
L
Linus Torvalds 已提交
952 953
FUN_D5:
	for (i = 0; i < 10; i++) {	/* stable >= bus settle delay(400 ns) */
O
Ondrej Zary 已提交
954
		if ((inw(dev->ioport[0] + 0x1c) & 0x2000) != 0) {	/* DB5 all release?       */
L
Linus Torvalds 已提交
955 956 957 958 959
			goto FUN_D5;
		}
	}
	*val |= 0x8000;		/* no DB4-0, assert DB7    */
	*val &= 0xe0ff;
O
Ondrej Zary 已提交
960
	outw(*val, dev->ioport[0] + 0x1c);
L
Linus Torvalds 已提交
961
	*val &= 0xbfff;		/* release DB6             */
O
Ondrej Zary 已提交
962
	outw(*val, dev->ioport[0] + 0x1c);
L
Linus Torvalds 已提交
963 964
FUN_D6:
	for (i = 0; i < 10; i++) {	/* stable >= bus settle delay(400 ns)  */
O
Ondrej Zary 已提交
965
		if ((inw(dev->ioport[0] + 0x1c) & 0x4000) != 0) {	/* DB6 all release?  */
L
Linus Torvalds 已提交
966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991
			goto FUN_D6;
		}
	}

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

O
Ondrej Zary 已提交
992 993 994
	outb(0x08, dev->ioport[0] + 1);
	outb(0x7f, dev->ioport[0] + 2);
	outb(0x20, dev->ioport[0] + 0x11);
L
Linus Torvalds 已提交
995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006

	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;
O
Ondrej Zary 已提交
1007 1008 1009 1010 1011 1012 1013
	outb(0x02, dev->ioport[0] + 0x02);	/* 2*2=4ms,3EH 2/32*3E=3.9ms */
	outb(0, dev->ioport[0] + 0x03);
	outb(0, dev->ioport[0] + 0x04);
	outb(0, dev->ioport[0] + 0x05);
	outb(0, dev->ioport[0] + 0x06);
	outb(0, dev->ioport[0] + 0x07);
	outb(0, dev->ioport[0] + 0x08);
L
Linus Torvalds 已提交
1014 1015 1016 1017 1018 1019 1020

	for (i = 0; i < j; i++) {
		m = 1;
		m = m << i;
		if ((m & assignid_map) != 0) {
			continue;
		}
O
Ondrej Zary 已提交
1021 1022 1023 1024
		outb(0, dev->ioport[0] + 0x0f);
		outb(0, dev->ioport[0] + 0x12);
		outb(0, dev->ioport[0] + 0x13);
		outb(0, dev->ioport[0] + 0x14);
L
Linus Torvalds 已提交
1025 1026 1027 1028 1029
		if (i > 7) {
			k = (i & 0x07) | 0x40;
		} else {
			k = i;
		}
O
Ondrej Zary 已提交
1030
		outb(k, dev->ioport[0] + 0x15);
L
Linus Torvalds 已提交
1031
		if (dev->chip_ver == 4) {
O
Ondrej Zary 已提交
1032
			outb(0x01, dev->ioport[0] + 0x1b);
L
Linus Torvalds 已提交
1033
		} else {
O
Ondrej Zary 已提交
1034
			outb(0x00, dev->ioport[0] + 0x1b);
L
Linus Torvalds 已提交
1035 1036
		}
wait_rdyok:
O
Ondrej Zary 已提交
1037
		outb(0x09, dev->ioport[0] + 0x18);
L
Linus Torvalds 已提交
1038

O
Ondrej Zary 已提交
1039
		while ((inb(dev->ioport[0] + 0x1f) & 0x80) == 0x00)
L
Linus Torvalds 已提交
1040
			cpu_relax();
O
Ondrej Zary 已提交
1041
		k = inb(dev->ioport[0] + 0x17);
L
Linus Torvalds 已提交
1042 1043 1044 1045
		if (k != 0x16) {
			if ((k == 0x85) || (k == 0x42)) {
				continue;
			}
O
Ondrej Zary 已提交
1046
			outb(0x41, dev->ioport[0] + 0x10);
L
Linus Torvalds 已提交
1047 1048 1049 1050 1051
			goto wait_rdyok;
		}
		assignid_map |= m;

	}
O
Ondrej Zary 已提交
1052 1053
	outb(0x7f, dev->ioport[0] + 0x02);
	outb(0x02, dev->ioport[0] + 0x1b);
L
Linus Torvalds 已提交
1054 1055 1056 1057

	outb(0, 0x80);

	val = 0x0080;		/* bsy  */
O
Ondrej Zary 已提交
1058
	outw(val, dev->ioport[0] + 0x1c);
L
Linus Torvalds 已提交
1059
	val |= 0x0040;		/* sel  */
O
Ondrej Zary 已提交
1060
	outw(val, dev->ioport[0] + 0x1c);
L
Linus Torvalds 已提交
1061
	val |= 0x0004;		/* msg  */
O
Ondrej Zary 已提交
1062
	outw(val, dev->ioport[0] + 0x1c);
L
Linus Torvalds 已提交
1063 1064
	inb(0x80);		/* 2 deskew delay(45ns*2=90ns) */
	val &= 0x007f;		/* no bsy  */
O
Ondrej Zary 已提交
1065
	outw(val, dev->ioport[0] + 0x1c);
L
Linus Torvalds 已提交
1066 1067
	mdelay(128);
	val &= 0x00fb;		/* after 1ms no msg */
O
Ondrej Zary 已提交
1068
	outw(val, dev->ioport[0] + 0x1c);
L
Linus Torvalds 已提交
1069
wait_nomsg:
O
Ondrej Zary 已提交
1070
	if ((inb(dev->ioport[0] + 0x1c) & 0x04) != 0) {
L
Linus Torvalds 已提交
1071 1072 1073 1074 1075
		goto wait_nomsg;
	}
	outb(1, 0x80);
	udelay(100);
	for (n = 0; n < 0x30000; n++) {
O
Ondrej Zary 已提交
1076
		if ((inb(dev->ioport[0] + 0x1c) & 0x80) != 0) {	/* bsy ? */
L
Linus Torvalds 已提交
1077 1078 1079 1080 1081 1082
			goto wait_io;
		}
	}
	goto TCM_SYNC;
wait_io:
	for (n = 0; n < 0x30000; n++) {
O
Ondrej Zary 已提交
1083
		if ((inb(dev->ioport[0] + 0x1c) & 0x81) == 0x0081) {
L
Linus Torvalds 已提交
1084 1085 1086 1087 1088 1089 1090
			goto wait_io1;
		}
	}
	goto TCM_SYNC;
wait_io1:
	inb(0x80);
	val |= 0x8003;		/* io,cd,db7  */
O
Ondrej Zary 已提交
1091
	outw(val, dev->ioport[0] + 0x1c);
L
Linus Torvalds 已提交
1092 1093
	inb(0x80);
	val &= 0x00bf;		/* no sel     */
O
Ondrej Zary 已提交
1094
	outw(val, dev->ioport[0] + 0x1c);
L
Linus Torvalds 已提交
1095 1096
	outb(2, 0x80);
TCM_SYNC:
1097 1098 1099 1100 1101 1102 1103 1104 1105 1106
	/*
	 * 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);
O
Ondrej Zary 已提交
1107 1108 1109 1110 1111 1112
	if ((inb(dev->ioport[0] + 0x1c) & 0x80) == 0x00) {	/* bsy ? */
		outw(0, dev->ioport[0] + 0x1c);
		outb(0, dev->ioport[0] + 0x1b);
		outb(0, dev->ioport[0] + 0x15);
		outb(0x09, dev->ioport[0] + 0x18);
		while ((inb(dev->ioport[0] + 0x1f) & 0x80) == 0)
L
Linus Torvalds 已提交
1113
			cpu_relax();
O
Ondrej Zary 已提交
1114
		inb(dev->ioport[0] + 0x17);
L
Linus Torvalds 已提交
1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127
		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;
TCM_ID:
O
Ondrej Zary 已提交
1128
	if ((inw(dev->ioport[0] + 0x1c) & 0x2000) == 0) {
L
Linus Torvalds 已提交
1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155
		goto TCM_ID;
	}
	outb(5, 0x80);
	val &= 0x00ff;		/* get ID_STRING */
	val |= 0x2000;
	k = fun_scam(dev, &val);
	if ((k & 0x03) == 0) {
		goto TCM_5;
	}
	mbuf[j] <<= 0x01;
	mbuf[j] &= 0xfe;
	if ((k & 0x02) != 0) {
		mbuf[j] |= 0x01;
	}
	i--;
	if (i > 0) {
		goto TCM_ID;
	}
	j++;
	i = 8;
	goto TCM_ID;

TCM_5:			/* isolation complete..  */
/*    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 已提交
1156
	if ((j & 0x20) != 0) {	/* bit5=1:ID up to 7      */
L
Linus Torvalds 已提交
1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219
		i = 7;
	}
	if ((j & 0x06) == 0) {	/* IDvalid?             */
		goto G2Q5;
	}
	k = mbuf[1];
small_id:
	m = 1;
	m <<= k;
	if ((m & assignid_map) == 0) {
		goto G2Q_QUIN;
	}
	if (k > 0) {
		k--;
		goto small_id;
	}
G2Q5:			/* srch from max acceptable ID#  */
	k = i;			/* max acceptable ID#            */
G2Q_LP:
	m = 1;
	m <<= k;
	if ((m & assignid_map) == 0) {
		goto G2Q_QUIN;
	}
	if (k > 0) {
		k--;
		goto G2Q_LP;
	}
G2Q_QUIN:		/* k=binID#,       */
	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);

	goto TCM_SYNC;

}

static void is870(struct atp_unit *dev, unsigned int wkport)
{
	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 };
	
O
Ondrej Zary 已提交
1220
	outb((unsigned char) (inb(wkport + 0x3a) | 0x10), wkport + 0x3a);
L
Linus Torvalds 已提交
1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235

	for (i = 0; i < 16; i++) {
		if ((dev->chip_ver != 4) && (i > 7)) {
			break;
		}
		m = 1;
		m = m << i;
		if ((m & dev->active_id[0]) != 0) {
			continue;
		}
		if (i == dev->host_id[0]) {
			printk(KERN_INFO "         ID: %2d  Host Adapter\n", dev->host_id[0]);
			continue;
		}
		if (dev->chip_ver == 4) {
O
Ondrej Zary 已提交
1236
			outb(0x01, wkport + 0x1b);
L
Linus Torvalds 已提交
1237
		} else {
O
Ondrej Zary 已提交
1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252
			outb(0x00, wkport + 0x1b);
		}
		outb(0x08, wkport + 1);
		outb(0x7f, wkport + 2);
		outb(satn[0], wkport + 3);
		outb(satn[1], wkport + 4);
		outb(satn[2], wkport + 5);
		outb(satn[3], wkport + 6);
		outb(satn[4], wkport + 7);
		outb(satn[5], wkport + 8);
		outb(0, wkport + 0x0f);
		outb(dev->id[0][i].devsp, wkport + 0x11);
		outb(0, wkport + 0x12);
		outb(satn[6], wkport + 0x13);
		outb(satn[7], wkport + 0x14);
L
Linus Torvalds 已提交
1253 1254 1255 1256
		j = i;
		if ((j & 0x08) != 0) {
			j = (j & 0x07) | 0x40;
		}
O
Ondrej Zary 已提交
1257 1258
		outb(j, wkport + 0x15);
		outb(satn[8], wkport + 0x18);
L
Linus Torvalds 已提交
1259

O
Ondrej Zary 已提交
1260
		while ((inb(wkport + 0x1f) & 0x80) == 0x00)
L
Linus Torvalds 已提交
1261 1262
			cpu_relax();

O
Ondrej Zary 已提交
1263
		if (inb(wkport + 0x17) != 0x11 && inb(wkport + 0x17) != 0x8e)
L
Linus Torvalds 已提交
1264 1265
			continue;

O
Ondrej Zary 已提交
1266
		while (inb(wkport + 0x17) != 0x8e)
L
Linus Torvalds 已提交
1267 1268 1269 1270
			cpu_relax();

		dev->active_id[0] |= m;

O
Ondrej Zary 已提交
1271 1272
		outb(0x30, wkport + 0x10);
		outb(0x00, wkport + 0x04);
L
Linus Torvalds 已提交
1273 1274

phase_cmd:
O
Ondrej Zary 已提交
1275 1276
		outb(0x08, wkport + 0x18);
		while ((inb(wkport + 0x1f) & 0x80) == 0x00)
L
Linus Torvalds 已提交
1277
			cpu_relax();
O
Ondrej Zary 已提交
1278
		j = inb(wkport + 0x17);
L
Linus Torvalds 已提交
1279
		if (j != 0x16) {
O
Ondrej Zary 已提交
1280
			outb(0x41, wkport + 0x10);
L
Linus Torvalds 已提交
1281 1282 1283
			goto phase_cmd;
		}
sel_ok:
O
Ondrej Zary 已提交
1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297
		outb(inqd[0], wkport + 3);
		outb(inqd[1], wkport + 4);
		outb(inqd[2], wkport + 5);
		outb(inqd[3], wkport + 6);
		outb(inqd[4], wkport + 7);
		outb(inqd[5], wkport + 8);
		outb(0, wkport + 0x0f);
		outb(dev->id[0][i].devsp, wkport + 0x11);
		outb(0, wkport + 0x12);
		outb(inqd[6], wkport + 0x13);
		outb(inqd[7], wkport + 0x14);
		outb(inqd[8], wkport + 0x18);

		while ((inb(wkport + 0x1f) & 0x80) == 0x00)
L
Linus Torvalds 已提交
1298 1299
			cpu_relax();
			
O
Ondrej Zary 已提交
1300
		if (inb(wkport + 0x17) != 0x11 && inb(wkport + 0x17) != 0x8e)
L
Linus Torvalds 已提交
1301 1302
			continue;

O
Ondrej Zary 已提交
1303
		while (inb(wkport + 0x17) != 0x8e)
L
Linus Torvalds 已提交
1304 1305 1306
			cpu_relax();
			
		if (dev->chip_ver == 4)
O
Ondrej Zary 已提交
1307
			outb(0x00, wkport + 0x1b);
L
Linus Torvalds 已提交
1308

O
Ondrej Zary 已提交
1309
		outb(0x08, wkport + 0x18);
L
Linus Torvalds 已提交
1310 1311
		j = 0;
rd_inq_data:
O
Ondrej Zary 已提交
1312
		k = inb(wkport + 0x1f);
L
Linus Torvalds 已提交
1313
		if ((k & 0x01) != 0) {
O
Ondrej Zary 已提交
1314
			mbuf[j++] = inb(wkport + 0x19);
L
Linus Torvalds 已提交
1315 1316 1317 1318 1319
			goto rd_inq_data;
		}
		if ((k & 0x80) == 0) {
			goto rd_inq_data;
		}
O
Ondrej Zary 已提交
1320
		j = inb(wkport + 0x17);
L
Linus Torvalds 已提交
1321 1322 1323
		if (j == 0x16) {
			goto inq_ok;
		}
O
Ondrej Zary 已提交
1324 1325 1326 1327 1328 1329 1330
		outb(0x46, wkport + 0x10);
		outb(0, wkport + 0x12);
		outb(0, wkport + 0x13);
		outb(0, wkport + 0x14);
		outb(0x08, wkport + 0x18);

		while ((inb(wkport + 0x1f) & 0x80) == 0x00)
L
Linus Torvalds 已提交
1331 1332
			cpu_relax();
			
O
Ondrej Zary 已提交
1333
		if (inb(wkport + 0x17) != 0x16) {
L
Linus Torvalds 已提交
1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350
			goto sel_ok;
		}
inq_ok:
		mbuf[36] = 0;
		printk(KERN_INFO "         ID: %2d  %s\n", i, &mbuf[8]);
		dev->id[0][i].devtype = mbuf[0];
		rmb = mbuf[1];
		n = mbuf[7];
		if (dev->chip_ver != 4) {
			goto not_wide;
		}
		if ((mbuf[7] & 0x60) == 0) {
			goto not_wide;
		}
		if ((dev->global_map[0] & 0x20) == 0) {
			goto not_wide;
		}
O
Ondrej Zary 已提交
1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365
		outb(0x01, wkport + 0x1b);
		outb(satn[0], wkport + 3);
		outb(satn[1], wkport + 4);
		outb(satn[2], wkport + 5);
		outb(satn[3], wkport + 6);
		outb(satn[4], wkport + 7);
		outb(satn[5], wkport + 8);
		outb(0, wkport + 0x0f);
		outb(dev->id[0][i].devsp, wkport + 0x11);
		outb(0, wkport + 0x12);
		outb(satn[6], wkport + 0x13);
		outb(satn[7], wkport + 0x14);
		outb(satn[8], wkport + 0x18);

		while ((inb(wkport + 0x1f) & 0x80) == 0x00)
L
Linus Torvalds 已提交
1366 1367
			cpu_relax();
			
O
Ondrej Zary 已提交
1368
		if (inb(wkport + 0x17) != 0x11 && inb(wkport + 0x17) != 0x8e)
L
Linus Torvalds 已提交
1369 1370
			continue;

O
Ondrej Zary 已提交
1371
		while (inb(wkport + 0x17) != 0x8e)
L
Linus Torvalds 已提交
1372 1373 1374 1375
			cpu_relax();
			
try_wide:
		j = 0;
O
Ondrej Zary 已提交
1376 1377 1378 1379 1380 1381
		outb(0x05, wkport + 0x14);
		outb(0x20, wkport + 0x18);

		while ((inb(wkport + 0x1f) & 0x80) == 0) {
			if ((inb(wkport + 0x1f) & 0x01) != 0)
				outb(wide[j++], wkport + 0x19);
L
Linus Torvalds 已提交
1382 1383
		}
		
O
Ondrej Zary 已提交
1384
		while ((inb(wkport + 0x17) & 0x80) == 0x00)
L
Linus Torvalds 已提交
1385 1386
			cpu_relax();
			
O
Ondrej Zary 已提交
1387
		j = inb(wkport + 0x17) & 0x0f;
L
Linus Torvalds 已提交
1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398
		if (j == 0x0f) {
			goto widep_in;
		}
		if (j == 0x0a) {
			goto widep_cmd;
		}
		if (j == 0x0e) {
			goto try_wide;
		}
		continue;
widep_out:
O
Ondrej Zary 已提交
1399 1400 1401 1402
		outb(0x20, wkport + 0x18);
		while ((inb(wkport + 0x1f) & 0x80) == 0) {
			if ((inb(wkport + 0x1f) & 0x01) != 0)
				outb(0, wkport + 0x19);
L
Linus Torvalds 已提交
1403
		}
O
Ondrej Zary 已提交
1404
		j = inb(wkport + 0x17) & 0x0f;
L
Linus Torvalds 已提交
1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415
		if (j == 0x0f) {
			goto widep_in;
		}
		if (j == 0x0a) {
			goto widep_cmd;
		}
		if (j == 0x0e) {
			goto widep_out;
		}
		continue;
widep_in:
O
Ondrej Zary 已提交
1416 1417
		outb(0xff, wkport + 0x14);
		outb(0x20, wkport + 0x18);
L
Linus Torvalds 已提交
1418 1419
		k = 0;
widep_in1:
O
Ondrej Zary 已提交
1420
		j = inb(wkport + 0x1f);
L
Linus Torvalds 已提交
1421
		if ((j & 0x01) != 0) {
O
Ondrej Zary 已提交
1422
			mbuf[k++] = inb(wkport + 0x19);
L
Linus Torvalds 已提交
1423 1424 1425 1426 1427
			goto widep_in1;
		}
		if ((j & 0x80) == 0x00) {
			goto widep_in1;
		}
O
Ondrej Zary 已提交
1428
		j = inb(wkport + 0x17) & 0x0f;
L
Linus Torvalds 已提交
1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439
		if (j == 0x0f) {
			goto widep_in;
		}
		if (j == 0x0a) {
			goto widep_cmd;
		}
		if (j == 0x0e) {
			goto widep_out;
		}
		continue;
widep_cmd:
O
Ondrej Zary 已提交
1440 1441 1442
		outb(0x30, wkport + 0x10);
		outb(0x00, wkport + 0x14);
		outb(0x08, wkport + 0x18);
L
Linus Torvalds 已提交
1443
		
O
Ondrej Zary 已提交
1444
		while ((inb(wkport + 0x1f) & 0x80) == 0x00)
L
Linus Torvalds 已提交
1445 1446
			cpu_relax();

O
Ondrej Zary 已提交
1447
		j = inb(wkport + 0x17);
L
Linus Torvalds 已提交
1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478
		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[0] |= m;
not_wide:
		if ((dev->id[0][i].devtype == 0x00) || (dev->id[0][i].devtype == 0x07) || ((dev->id[0][i].devtype == 0x05) && ((n & 0x10) != 0))) {
			goto set_sync;
		}
		continue;
set_sync:
		j = 0;
		if ((m & dev->wide_id[0]) != 0) {
			j |= 0x01;
		}
O
Ondrej Zary 已提交
1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493
		outb(j, wkport + 0x1b);
		outb(satn[0], wkport + 3);
		outb(satn[1], wkport + 4);
		outb(satn[2], wkport + 5);
		outb(satn[3], wkport + 6);
		outb(satn[4], wkport + 7);
		outb(satn[5], wkport + 8);
		outb(0, wkport + 0x0f);
		outb(dev->id[0][i].devsp, wkport + 0x11);
		outb(0, wkport + 0x12);
		outb(satn[6], wkport + 0x13);
		outb(satn[7], wkport + 0x14);
		outb(satn[8], wkport + 0x18);

		while ((inb(wkport + 0x1f) & 0x80) == 0x00)
L
Linus Torvalds 已提交
1494 1495
			cpu_relax();
			
O
Ondrej Zary 已提交
1496
		if (inb(wkport + 0x17) != 0x11 && inb(wkport + 0x17) != 0x8e)
L
Linus Torvalds 已提交
1497 1498
			continue;

O
Ondrej Zary 已提交
1499
		while (inb(wkport + 0x17) != 0x8e)
L
Linus Torvalds 已提交
1500 1501 1502 1503
			cpu_relax();
			
try_sync:
		j = 0;
O
Ondrej Zary 已提交
1504 1505 1506 1507 1508
		outb(0x06, wkport + 0x14);
		outb(0x20, wkport + 0x18);

		while ((inb(wkport + 0x1f) & 0x80) == 0) {
			if ((inb(wkport + 0x1f) & 0x01) != 0) {
L
Linus Torvalds 已提交
1509
				if ((m & dev->wide_id[0]) != 0) {
O
Ondrej Zary 已提交
1510
					outb(synw[j++], wkport + 0x19);
L
Linus Torvalds 已提交
1511 1512
				} else {
					if ((m & dev->ultra_map[0]) != 0) {
O
Ondrej Zary 已提交
1513
						outb(synu[j++], wkport + 0x19);
L
Linus Torvalds 已提交
1514
					} else {
O
Ondrej Zary 已提交
1515
						outb(synn[j++], wkport + 0x19);
L
Linus Torvalds 已提交
1516 1517 1518 1519 1520
					}
				}
			}
		}
		
O
Ondrej Zary 已提交
1521
		while ((inb(wkport + 0x17) & 0x80) == 0x00)
L
Linus Torvalds 已提交
1522 1523
			cpu_relax();
			
O
Ondrej Zary 已提交
1524
		j = inb(wkport + 0x17) & 0x0f;
L
Linus Torvalds 已提交
1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535
		if (j == 0x0f) {
			goto phase_ins;
		}
		if (j == 0x0a) {
			goto phase_cmds;
		}
		if (j == 0x0e) {
			goto try_sync;
		}
		continue;
phase_outs:
O
Ondrej Zary 已提交
1536 1537 1538 1539
		outb(0x20, wkport + 0x18);
		while ((inb(wkport + 0x1f) & 0x80) == 0x00) {
			if ((inb(wkport + 0x1f) & 0x01) != 0x00)
				outb(0x00, wkport + 0x19);
L
Linus Torvalds 已提交
1540
		}
O
Ondrej Zary 已提交
1541
		j = inb(wkport + 0x17);
L
Linus Torvalds 已提交
1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556
		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:
O
Ondrej Zary 已提交
1557 1558
		outb(0xff, wkport + 0x14);
		outb(0x20, wkport + 0x18);
L
Linus Torvalds 已提交
1559 1560
		k = 0;
phase_ins1:
O
Ondrej Zary 已提交
1561
		j = inb(wkport + 0x1f);
L
Linus Torvalds 已提交
1562
		if ((j & 0x01) != 0x00) {
O
Ondrej Zary 已提交
1563
			mbuf[k++] = inb(wkport + 0x19);
L
Linus Torvalds 已提交
1564 1565 1566 1567 1568 1569
			goto phase_ins1;
		}
		if ((j & 0x80) == 0x00) {
			goto phase_ins1;
		}

O
Ondrej Zary 已提交
1570
		while ((inb(wkport + 0x17) & 0x80) == 0x00)
L
Linus Torvalds 已提交
1571 1572
			cpu_relax();
			
O
Ondrej Zary 已提交
1573
		j = inb(wkport + 0x17);
L
Linus Torvalds 已提交
1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588
		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:
O
Ondrej Zary 已提交
1589
		outb(0x30, wkport + 0x10);
L
Linus Torvalds 已提交
1590
tar_dcons:
O
Ondrej Zary 已提交
1591 1592
		outb(0x00, wkport + 0x14);
		outb(0x08, wkport + 0x18);
L
Linus Torvalds 已提交
1593
		
O
Ondrej Zary 已提交
1594
		while ((inb(wkport + 0x1f) & 0x80) == 0x00)
L
Linus Torvalds 已提交
1595 1596
			cpu_relax();
			
O
Ondrej Zary 已提交
1597
		j = inb(wkport + 0x17);
L
Linus Torvalds 已提交
1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636
		if (j != 0x16) {
			continue;
		}
		if (mbuf[0] != 0x01) {
			continue;
		}
		if (mbuf[1] != 0x03) {
			continue;
		}
		if (mbuf[4] == 0x00) {
			continue;
		}
		if (mbuf[3] > 0x64) {
			continue;
		}
		if (mbuf[4] > 0x0c) {
			mbuf[4] = 0x0c;
		}
		dev->id[0][i].devsp = mbuf[4];
		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;
set_syn_ok:
		dev->id[0][i].devsp = (dev->id[0][i].devsp & 0x0f) | j;
	}
O
Ondrej Zary 已提交
1637
	outb((unsigned char) (inb(wkport + 0x3a) & 0xef), wkport + 0x3a);
L
Linus Torvalds 已提交
1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665
}

static void is880(struct atp_unit *dev, unsigned int wkport)
{
	unsigned char i, j, k, rmb, n, lvdmode;
	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 };
	unsigned char synu[6] = { 0x80, 1, 3, 1, 0x0a, 0x0e };
	static unsigned char synw[6] = { 0x80, 1, 3, 1, 0x19, 0x0e };
	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 };

	lvdmode = inb(wkport + 0x3f) & 0x40;

	for (i = 0; i < 16; i++) {
		m = 1;
		m = m << i;
		if ((m & dev->active_id[0]) != 0) {
			continue;
		}
		if (i == dev->host_id[0]) {
			printk(KERN_INFO "         ID: %2d  Host Adapter\n", dev->host_id[0]);
			continue;
		}
O
Ondrej Zary 已提交
1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679
		outb(0x01, wkport + 0x5b);
		outb(0x08, wkport + 0x41);
		outb(0x7f, wkport + 0x42);
		outb(satn[0], wkport + 0x43);
		outb(satn[1], wkport + 0x44);
		outb(satn[2], wkport + 0x45);
		outb(satn[3], wkport + 0x46);
		outb(satn[4], wkport + 0x47);
		outb(satn[5], wkport + 0x48);
		outb(0, wkport + 0x4f);
		outb(dev->id[0][i].devsp, wkport + 0x51);
		outb(0, wkport + 0x52);
		outb(satn[6], wkport + 0x53);
		outb(satn[7], wkport + 0x54);
L
Linus Torvalds 已提交
1680 1681 1682 1683
		j = i;
		if ((j & 0x08) != 0) {
			j = (j & 0x07) | 0x40;
		}
O
Ondrej Zary 已提交
1684 1685
		outb(j, wkport + 0x55);
		outb(satn[8], wkport + 0x58);
L
Linus Torvalds 已提交
1686

O
Ondrej Zary 已提交
1687
		while ((inb(wkport + 0x5f) & 0x80) == 0x00)
L
Linus Torvalds 已提交
1688 1689
			cpu_relax();

O
Ondrej Zary 已提交
1690
		if (inb(wkport + 0x57) != 0x11 && inb(wkport + 0x57) != 0x8e)
L
Linus Torvalds 已提交
1691 1692
			continue;

O
Ondrej Zary 已提交
1693
		while (inb(wkport + 0x57) != 0x8e)
L
Linus Torvalds 已提交
1694 1695 1696 1697
			cpu_relax();
			
		dev->active_id[0] |= m;

O
Ondrej Zary 已提交
1698 1699
		outb(0x30, wkport + 0x50);
		outb(0x00, wkport + 0x54);
L
Linus Torvalds 已提交
1700 1701

phase_cmd:
O
Ondrej Zary 已提交
1702
		outb(0x08, wkport + 0x58);
L
Linus Torvalds 已提交
1703
		
O
Ondrej Zary 已提交
1704
		while ((inb(wkport + 0x5f) & 0x80) == 0x00)
L
Linus Torvalds 已提交
1705 1706
			cpu_relax();

O
Ondrej Zary 已提交
1707
		j = inb(wkport + 0x57);
L
Linus Torvalds 已提交
1708
		if (j != 0x16) {
O
Ondrej Zary 已提交
1709
			outb(0x41, wkport + 0x50);
L
Linus Torvalds 已提交
1710 1711 1712
			goto phase_cmd;
		}
sel_ok:
O
Ondrej Zary 已提交
1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724
		outb(inqd[0], wkport + 0x43);
		outb(inqd[1], wkport + 0x44);
		outb(inqd[2], wkport + 0x45);
		outb(inqd[3], wkport + 0x46);
		outb(inqd[4], wkport + 0x47);
		outb(inqd[5], wkport + 0x48);
		outb(0, wkport + 0x4f);
		outb(dev->id[0][i].devsp, wkport + 0x51);
		outb(0, wkport + 0x52);
		outb(inqd[6], wkport + 0x53);
		outb(inqd[7], wkport + 0x54);
		outb(inqd[8], wkport + 0x58);
L
Linus Torvalds 已提交
1725
		
O
Ondrej Zary 已提交
1726
		while ((inb(wkport + 0x5f) & 0x80) == 0x00)
L
Linus Torvalds 已提交
1727 1728
			cpu_relax();
			
O
Ondrej Zary 已提交
1729
		if (inb(wkport + 0x57) != 0x11 && inb(wkport + 0x57) != 0x8e)
L
Linus Torvalds 已提交
1730 1731
			continue;

O
Ondrej Zary 已提交
1732
		while (inb(wkport + 0x57) != 0x8e)
L
Linus Torvalds 已提交
1733 1734
			cpu_relax();
			
O
Ondrej Zary 已提交
1735 1736
		outb(0x00, wkport + 0x5b);
		outb(0x08, wkport + 0x58);
L
Linus Torvalds 已提交
1737 1738
		j = 0;
rd_inq_data:
O
Ondrej Zary 已提交
1739
		k = inb(wkport + 0x5f);
L
Linus Torvalds 已提交
1740
		if ((k & 0x01) != 0) {
O
Ondrej Zary 已提交
1741
			mbuf[j++] = inb(wkport + 0x59);
L
Linus Torvalds 已提交
1742 1743 1744 1745 1746
			goto rd_inq_data;
		}
		if ((k & 0x80) == 0) {
			goto rd_inq_data;
		}
O
Ondrej Zary 已提交
1747
		j = inb(wkport + 0x57);
L
Linus Torvalds 已提交
1748 1749 1750
		if (j == 0x16) {
			goto inq_ok;
		}
O
Ondrej Zary 已提交
1751 1752 1753 1754 1755 1756
		outb(0x46, wkport + 0x50);
		outb(0, wkport + 0x52);
		outb(0, wkport + 0x53);
		outb(0, wkport + 0x54);
		outb(0x08, wkport + 0x58);
		while ((inb(wkport + 0x5f) & 0x80) == 0x00)
L
Linus Torvalds 已提交
1757 1758
			cpu_relax();
			
O
Ondrej Zary 已提交
1759
		if (inb(wkport + 0x57) != 0x16)
L
Linus Torvalds 已提交
1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781
			goto sel_ok;

inq_ok:
		mbuf[36] = 0;
		printk(KERN_INFO "         ID: %2d  %s\n", i, &mbuf[8]);
		dev->id[0][i].devtype = mbuf[0];
		rmb = mbuf[1];
		n = mbuf[7];
		if ((mbuf[7] & 0x60) == 0) {
			goto not_wide;
		}
		if ((i < 8) && ((dev->global_map[0] & 0x20) == 0)) {
			goto not_wide;
		}
		if (lvdmode == 0) {
			goto chg_wide;
		}
		if (dev->sp[0][i] != 0x04)	// force u2
		{
			goto chg_wide;
		}

O
Ondrej Zary 已提交
1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796
		outb(0x01, wkport + 0x5b);
		outb(satn[0], wkport + 0x43);
		outb(satn[1], wkport + 0x44);
		outb(satn[2], wkport + 0x45);
		outb(satn[3], wkport + 0x46);
		outb(satn[4], wkport + 0x47);
		outb(satn[5], wkport + 0x48);
		outb(0, wkport + 0x4f);
		outb(dev->id[0][i].devsp, wkport + 0x51);
		outb(0, wkport + 0x52);
		outb(satn[6], wkport + 0x53);
		outb(satn[7], wkport + 0x54);
		outb(satn[8], wkport + 0x58);

		while ((inb(wkport + 0x5f) & 0x80) == 0x00)
L
Linus Torvalds 已提交
1797 1798
			cpu_relax();

O
Ondrej Zary 已提交
1799
		if (inb(wkport + 0x57) != 0x11 && inb(wkport + 0x57) != 0x8e)
L
Linus Torvalds 已提交
1800 1801
			continue;

O
Ondrej Zary 已提交
1802
		while (inb(wkport + 0x57) != 0x8e)
L
Linus Torvalds 已提交
1803 1804 1805 1806
			cpu_relax();

try_u3:
		j = 0;
O
Ondrej Zary 已提交
1807 1808 1809 1810 1811 1812
		outb(0x09, wkport + 0x54);
		outb(0x20, wkport + 0x58);

		while ((inb(wkport + 0x5f) & 0x80) == 0) {
			if ((inb(wkport + 0x5f) & 0x01) != 0)
				outb(u3[j++], wkport + 0x59);
L
Linus Torvalds 已提交
1813 1814
		}

O
Ondrej Zary 已提交
1815
		while ((inb(wkport + 0x57) & 0x80) == 0x00)
L
Linus Torvalds 已提交
1816 1817
			cpu_relax();
			
O
Ondrej Zary 已提交
1818
		j = inb(wkport + 0x57) & 0x0f;
L
Linus Torvalds 已提交
1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829
		if (j == 0x0f) {
			goto u3p_in;
		}
		if (j == 0x0a) {
			goto u3p_cmd;
		}
		if (j == 0x0e) {
			goto try_u3;
		}
		continue;
u3p_out:
O
Ondrej Zary 已提交
1830 1831 1832 1833
		outb(0x20, wkport + 0x58);
		while ((inb(wkport + 0x5f) & 0x80) == 0) {
			if ((inb(wkport + 0x5f) & 0x01) != 0)
				outb(0, wkport + 0x59);
L
Linus Torvalds 已提交
1834
		}
O
Ondrej Zary 已提交
1835
		j = inb(wkport + 0x57) & 0x0f;
L
Linus Torvalds 已提交
1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846
		if (j == 0x0f) {
			goto u3p_in;
		}
		if (j == 0x0a) {
			goto u3p_cmd;
		}
		if (j == 0x0e) {
			goto u3p_out;
		}
		continue;
u3p_in:
O
Ondrej Zary 已提交
1847 1848
		outb(0x09, wkport + 0x54);
		outb(0x20, wkport + 0x58);
L
Linus Torvalds 已提交
1849 1850
		k = 0;
u3p_in1:
O
Ondrej Zary 已提交
1851
		j = inb(wkport + 0x5f);
L
Linus Torvalds 已提交
1852
		if ((j & 0x01) != 0) {
O
Ondrej Zary 已提交
1853
			mbuf[k++] = inb(wkport + 0x59);
L
Linus Torvalds 已提交
1854 1855 1856 1857 1858
			goto u3p_in1;
		}
		if ((j & 0x80) == 0x00) {
			goto u3p_in1;
		}
O
Ondrej Zary 已提交
1859
		j = inb(wkport + 0x57) & 0x0f;
L
Linus Torvalds 已提交
1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870
		if (j == 0x0f) {
			goto u3p_in;
		}
		if (j == 0x0a) {
			goto u3p_cmd;
		}
		if (j == 0x0e) {
			goto u3p_out;
		}
		continue;
u3p_cmd:
O
Ondrej Zary 已提交
1871 1872 1873
		outb(0x30, wkport + 0x50);
		outb(0x00, wkport + 0x54);
		outb(0x08, wkport + 0x58);
L
Linus Torvalds 已提交
1874
		
O
Ondrej Zary 已提交
1875
		while ((inb(wkport + 0x5f) & 0x80) == 0x00)
L
Linus Torvalds 已提交
1876 1877
			cpu_relax();
			
O
Ondrej Zary 已提交
1878
		j = inb(wkport + 0x57);
L
Linus Torvalds 已提交
1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901
		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[0] |= m;
			dev->id[0][i].devsp = 0xce;
			continue;
		}
chg_wide:
O
Ondrej Zary 已提交
1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916
		outb(0x01, wkport + 0x5b);
		outb(satn[0], wkport + 0x43);
		outb(satn[1], wkport + 0x44);
		outb(satn[2], wkport + 0x45);
		outb(satn[3], wkport + 0x46);
		outb(satn[4], wkport + 0x47);
		outb(satn[5], wkport + 0x48);
		outb(0, wkport + 0x4f);
		outb(dev->id[0][i].devsp, wkport + 0x51);
		outb(0, wkport + 0x52);
		outb(satn[6], wkport + 0x53);
		outb(satn[7], wkport + 0x54);
		outb(satn[8], wkport + 0x58);

		while ((inb(wkport + 0x5f) & 0x80) == 0x00)
L
Linus Torvalds 已提交
1917 1918
			cpu_relax();
			
O
Ondrej Zary 已提交
1919
		if (inb(wkport + 0x57) != 0x11 && inb(wkport + 0x57) != 0x8e)
L
Linus Torvalds 已提交
1920 1921
			continue;

O
Ondrej Zary 已提交
1922
		while (inb(wkport + 0x57) != 0x8e)
L
Linus Torvalds 已提交
1923 1924 1925 1926
			cpu_relax();
			
try_wide:
		j = 0;
O
Ondrej Zary 已提交
1927 1928 1929 1930 1931 1932
		outb(0x05, wkport + 0x54);
		outb(0x20, wkport + 0x58);

		while ((inb(wkport + 0x5f) & 0x80) == 0) {
			if ((inb(wkport + 0x5f) & 0x01) != 0)
				outb(wide[j++], wkport + 0x59);
L
Linus Torvalds 已提交
1933
		}
O
Ondrej Zary 已提交
1934
		while ((inb(wkport + 0x57) & 0x80) == 0x00)
L
Linus Torvalds 已提交
1935 1936
			cpu_relax();
			
O
Ondrej Zary 已提交
1937
		j = inb(wkport + 0x57) & 0x0f;
L
Linus Torvalds 已提交
1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948
		if (j == 0x0f) {
			goto widep_in;
		}
		if (j == 0x0a) {
			goto widep_cmd;
		}
		if (j == 0x0e) {
			goto try_wide;
		}
		continue;
widep_out:
O
Ondrej Zary 已提交
1949 1950 1951 1952
		outb(0x20, wkport + 0x58);
		while ((inb(wkport + 0x5f) & 0x80) == 0) {
			if ((inb(wkport + 0x5f) & 0x01) != 0)
				outb(0, wkport + 0x59);
L
Linus Torvalds 已提交
1953
		}
O
Ondrej Zary 已提交
1954
		j = inb(wkport + 0x57) & 0x0f;
L
Linus Torvalds 已提交
1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965
		if (j == 0x0f) {
			goto widep_in;
		}
		if (j == 0x0a) {
			goto widep_cmd;
		}
		if (j == 0x0e) {
			goto widep_out;
		}
		continue;
widep_in:
O
Ondrej Zary 已提交
1966 1967
		outb(0xff, wkport + 0x54);
		outb(0x20, wkport + 0x58);
L
Linus Torvalds 已提交
1968 1969
		k = 0;
widep_in1:
O
Ondrej Zary 已提交
1970
		j = inb(wkport + 0x5f);
L
Linus Torvalds 已提交
1971
		if ((j & 0x01) != 0) {
O
Ondrej Zary 已提交
1972
			mbuf[k++] = inb(wkport + 0x59);
L
Linus Torvalds 已提交
1973 1974 1975 1976 1977
			goto widep_in1;
		}
		if ((j & 0x80) == 0x00) {
			goto widep_in1;
		}
O
Ondrej Zary 已提交
1978
		j = inb(wkport + 0x57) & 0x0f;
L
Linus Torvalds 已提交
1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989
		if (j == 0x0f) {
			goto widep_in;
		}
		if (j == 0x0a) {
			goto widep_cmd;
		}
		if (j == 0x0e) {
			goto widep_out;
		}
		continue;
widep_cmd:
O
Ondrej Zary 已提交
1990 1991 1992 1993 1994
		outb(0x30, wkport + 0x50);
		outb(0x00, wkport + 0x54);
		outb(0x08, wkport + 0x58);

		while ((inb(wkport + 0x5f) & 0x80) == 0x00)
L
Linus Torvalds 已提交
1995 1996
			cpu_relax();

O
Ondrej Zary 已提交
1997
		j = inb(wkport + 0x57);
L
Linus Torvalds 已提交
1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041
		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[0] |= m;
not_wide:
		if ((dev->id[0][i].devtype == 0x00) || (dev->id[0][i].devtype == 0x07) || ((dev->id[0][i].devtype == 0x05) && ((n & 0x10) != 0))) {
			m = 1;
			m = m << i;
			if ((dev->async[0] & m) != 0) {
				goto set_sync;
			}
		}
		continue;
set_sync:
		if (dev->sp[0][i] == 0x02) {
			synu[4] = 0x0c;
			synuw[4] = 0x0c;
		} else {
			if (dev->sp[0][i] >= 0x03) {
				synu[4] = 0x0a;
				synuw[4] = 0x0a;
			}
		}
		j = 0;
		if ((m & dev->wide_id[0]) != 0) {
			j |= 0x01;
		}
O
Ondrej Zary 已提交
2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056
		outb(j, wkport + 0x5b);
		outb(satn[0], wkport + 0x43);
		outb(satn[1], wkport + 0x44);
		outb(satn[2], wkport + 0x45);
		outb(satn[3], wkport + 0x46);
		outb(satn[4], wkport + 0x47);
		outb(satn[5], wkport + 0x48);
		outb(0, wkport + 0x4f);
		outb(dev->id[0][i].devsp, wkport + 0x51);
		outb(0, wkport + 0x52);
		outb(satn[6], wkport + 0x53);
		outb(satn[7], wkport + 0x54);
		outb(satn[8], wkport + 0x58);

		while ((inb(wkport + 0x5f) & 0x80) == 0x00)
L
Linus Torvalds 已提交
2057 2058
			cpu_relax();

O
Ondrej Zary 已提交
2059
		if ((inb(wkport + 0x57) != 0x11) && (inb(wkport + 0x57) != 0x8e)) {
L
Linus Torvalds 已提交
2060 2061
			continue;
		}
O
Ondrej Zary 已提交
2062
		while (inb(wkport + 0x57) != 0x8e)
L
Linus Torvalds 已提交
2063 2064 2065 2066
			cpu_relax();

try_sync:
		j = 0;
O
Ondrej Zary 已提交
2067 2068 2069 2070 2071
		outb(0x06, wkport + 0x54);
		outb(0x20, wkport + 0x58);

		while ((inb(wkport + 0x5f) & 0x80) == 0) {
			if ((inb(wkport + 0x5f) & 0x01) != 0) {
L
Linus Torvalds 已提交
2072 2073
				if ((m & dev->wide_id[0]) != 0) {
					if ((m & dev->ultra_map[0]) != 0) {
O
Ondrej Zary 已提交
2074
						outb(synuw[j++], wkport + 0x59);
L
Linus Torvalds 已提交
2075
					} else {
O
Ondrej Zary 已提交
2076
						outb(synw[j++], wkport + 0x59);
L
Linus Torvalds 已提交
2077 2078 2079
					}
				} else {
					if ((m & dev->ultra_map[0]) != 0) {
O
Ondrej Zary 已提交
2080
						outb(synu[j++], wkport + 0x59);
L
Linus Torvalds 已提交
2081
					} else {
O
Ondrej Zary 已提交
2082
						outb(synn[j++], wkport + 0x59);
L
Linus Torvalds 已提交
2083 2084 2085 2086 2087
					}
				}
			}
		}

O
Ondrej Zary 已提交
2088
		while ((inb(wkport + 0x57) & 0x80) == 0x00)
L
Linus Torvalds 已提交
2089 2090
			cpu_relax();

O
Ondrej Zary 已提交
2091
		j = inb(wkport + 0x57) & 0x0f;
L
Linus Torvalds 已提交
2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102
		if (j == 0x0f) {
			goto phase_ins;
		}
		if (j == 0x0a) {
			goto phase_cmds;
		}
		if (j == 0x0e) {
			goto try_sync;
		}
		continue;
phase_outs:
O
Ondrej Zary 已提交
2103 2104 2105 2106
		outb(0x20, wkport + 0x58);
		while ((inb(wkport + 0x5f) & 0x80) == 0x00) {
			if ((inb(wkport + 0x5f) & 0x01) != 0x00)
				outb(0x00, wkport + 0x59);
L
Linus Torvalds 已提交
2107
		}
O
Ondrej Zary 已提交
2108
		j = inb(wkport + 0x57);
L
Linus Torvalds 已提交
2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123
		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:
O
Ondrej Zary 已提交
2124 2125
		outb(0x06, wkport + 0x54);
		outb(0x20, wkport + 0x58);
L
Linus Torvalds 已提交
2126 2127
		k = 0;
phase_ins1:
O
Ondrej Zary 已提交
2128
		j = inb(wkport + 0x5f);
L
Linus Torvalds 已提交
2129
		if ((j & 0x01) != 0x00) {
O
Ondrej Zary 已提交
2130
			mbuf[k++] = inb(wkport + 0x59);
L
Linus Torvalds 已提交
2131 2132 2133 2134 2135 2136
			goto phase_ins1;
		}
		if ((j & 0x80) == 0x00) {
			goto phase_ins1;
		}

O
Ondrej Zary 已提交
2137
		while ((inb(wkport + 0x57) & 0x80) == 0x00)
L
Linus Torvalds 已提交
2138 2139
			cpu_relax();

O
Ondrej Zary 已提交
2140
		j = inb(wkport + 0x57);
L
Linus Torvalds 已提交
2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155
		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:
O
Ondrej Zary 已提交
2156
		outb(0x30, wkport + 0x50);
L
Linus Torvalds 已提交
2157
tar_dcons:
O
Ondrej Zary 已提交
2158 2159
		outb(0x00, wkport + 0x54);
		outb(0x08, wkport + 0x58);
L
Linus Torvalds 已提交
2160

O
Ondrej Zary 已提交
2161
		while ((inb(wkport + 0x5f) & 0x80) == 0x00)
L
Linus Torvalds 已提交
2162 2163
			cpu_relax();

O
Ondrej Zary 已提交
2164
		j = inb(wkport + 0x57);
L
Linus Torvalds 已提交
2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217
		if (j != 0x16) {
			continue;
		}
		if (mbuf[0] != 0x01) {
			continue;
		}
		if (mbuf[1] != 0x03) {
			continue;
		}
		if (mbuf[4] == 0x00) {
			continue;
		}
		if (mbuf[3] > 0x64) {
			continue;
		}
		if (mbuf[4] > 0x0e) {
			mbuf[4] = 0x0e;
		}
		dev->id[0][i].devsp = mbuf[4];
		if (mbuf[3] < 0x0c) {
			j = 0xb0;
			goto set_syn_ok;
		}
		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;
set_syn_ok:
		dev->id[0][i].devsp = (dev->id[0][i].devsp & 0x0f) | j;
	}
}

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;
2218
			pci_free_consistent(atp_dev->pdev, 1024, atp_dev->id[j][k].prd_table, atp_dev->id[j][k].prd_bus);
L
Linus Torvalds 已提交
2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229
			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++) {
2230
	   			atp_dev->id[c][k].prd_table = pci_alloc_consistent(atp_dev->pdev, 1024, &(atp_dev->id[c][k].prd_bus));
L
Linus Torvalds 已提交
2231 2232 2233 2234 2235
	   			if (!atp_dev->id[c][k].prd_table) {
	   				printk("atp870u_init_tables fail\n");
				atp870u_free_tables(host);
				return -ENOMEM;
			}
2236
			atp_dev->id[c][k].prdaddr = atp_dev->id[c][k].prd_bus;
L
Linus Torvalds 已提交
2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266
			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;
}

/* return non-zero on detection */
static int atp870u_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
{
	unsigned char k, m, c;
	unsigned long flags;
O
Ondrej Zary 已提交
2267
	unsigned int base_io, error,n;
L
Linus Torvalds 已提交
2268 2269
	unsigned char host_id;
	struct Scsi_Host *shpnt = NULL;
2270
	struct atp_unit *atpdev, *p;
L
Linus Torvalds 已提交
2271 2272
	unsigned char setupdata[2][16];
	int count = 0;
2273 2274 2275 2276 2277

	atpdev = kzalloc(sizeof(*atpdev), GFP_KERNEL);
	if (!atpdev)
		return -ENOMEM;

L
Linus Torvalds 已提交
2278
	if (pci_enable_device(pdev))
2279
		goto err_eio;
L
Linus Torvalds 已提交
2280

2281
        if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(32))) {
L
Linus Torvalds 已提交
2282 2283 2284
                printk(KERN_INFO "atp870u: use 32bit DMA mask.\n");
        } else {
                printk(KERN_ERR "atp870u: DMA mask required but not available.\n");
2285
		goto err_eio;
L
Linus Torvalds 已提交
2286 2287 2288 2289 2290 2291 2292
        }

	/*
	 * It's probably easier to weed out some revisions like
	 * this than via the PCI device table
	 */
	if (ent->device == PCI_DEVICE_ID_ARTOP_AEC7610) {
2293
		atpdev->chip_ver = pdev->revision;
2294 2295
		if (atpdev->chip_ver < 2)
			goto err_eio;
L
Linus Torvalds 已提交
2296 2297 2298 2299 2300 2301 2302 2303
	}

	switch (ent->device) {
	case PCI_DEVICE_ID_ARTOP_AEC7612UW:
	case PCI_DEVICE_ID_ARTOP_AEC7612SUW:
	case ATP880_DEVID1:	
	case ATP880_DEVID2:	
	case ATP885_DEVID:	
2304
		atpdev->chip_ver = 0x04;
L
Linus Torvalds 已提交
2305 2306 2307 2308 2309
	default:
		break;
	}
	base_io = pci_resource_start(pdev, 0);
	base_io &= 0xfffffff8;
2310

L
Linus Torvalds 已提交
2311
	if ((ent->device == ATP880_DEVID1)||(ent->device == ATP880_DEVID2)) {
2312
		atpdev->chip_ver = pdev->revision;
L
Linus Torvalds 已提交
2313 2314 2315 2316 2317 2318 2319
		pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0x80);//JCC082803

		host_id = inb(base_io + 0x39);
		host_id >>= 0x04;

		printk(KERN_INFO "   ACARD AEC-67160 PCI Ultra3 LVD Host Adapter: %d"
			"    IO:%x, IRQ:%d.\n", count, base_io, pdev->irq);
2320 2321 2322 2323
		atpdev->ioport[0] = base_io + 0x40;
		atpdev->pciport[0] = base_io + 0x28;
		atpdev->dev_id = ent->device;
		atpdev->host_id[0] = host_id;
L
Linus Torvalds 已提交
2324

O
Ondrej Zary 已提交
2325 2326 2327
		atpdev->scam_on = inb(base_io + 0x22);
		atpdev->global_map[0] = inb(base_io + 0x35);
		atpdev->ultra_map[0] = inw(base_io + 0x3c);
L
Linus Torvalds 已提交
2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339

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

		m = 0;
		outw(n, base_io + 0x34);
		n += 0x0002;
		if (inb(base_io + 0x30) == 0xff)
			goto flash_ok_880;

2340 2341 2342 2343
		atpdev->sp[0][m++] = inb(base_io + 0x30);
		atpdev->sp[0][m++] = inb(base_io + 0x31);
		atpdev->sp[0][m++] = inb(base_io + 0x32);
		atpdev->sp[0][m++] = inb(base_io + 0x33);
L
Linus Torvalds 已提交
2344 2345
		outw(n, base_io + 0x34);
		n += 0x0002;
2346 2347 2348 2349
		atpdev->sp[0][m++] = inb(base_io + 0x30);
		atpdev->sp[0][m++] = inb(base_io + 0x31);
		atpdev->sp[0][m++] = inb(base_io + 0x32);
		atpdev->sp[0][m++] = inb(base_io + 0x33);
L
Linus Torvalds 已提交
2350 2351
		outw(n, base_io + 0x34);
		n += 0x0002;
2352 2353 2354 2355
		atpdev->sp[0][m++] = inb(base_io + 0x30);
		atpdev->sp[0][m++] = inb(base_io + 0x31);
		atpdev->sp[0][m++] = inb(base_io + 0x32);
		atpdev->sp[0][m++] = inb(base_io + 0x33);
L
Linus Torvalds 已提交
2356 2357
		outw(n, base_io + 0x34);
		n += 0x0002;
2358 2359 2360 2361
		atpdev->sp[0][m++] = inb(base_io + 0x30);
		atpdev->sp[0][m++] = inb(base_io + 0x31);
		atpdev->sp[0][m++] = inb(base_io + 0x32);
		atpdev->sp[0][m++] = inb(base_io + 0x33);
L
Linus Torvalds 已提交
2362 2363 2364 2365
		n += 0x0018;
		goto next_fblk_880;
flash_ok_880:
		outw(0, base_io + 0x34);
2366 2367
		atpdev->ultra_map[0] = 0;
		atpdev->async[0] = 0;
L
Linus Torvalds 已提交
2368 2369 2370
		for (k = 0; k < 16; k++) {
			n = 1;
			n = n << k;
2371 2372
			if (atpdev->sp[0][k] > 1) {
				atpdev->ultra_map[0] |= n;
L
Linus Torvalds 已提交
2373
			} else {
2374 2375
				if (atpdev->sp[0][k] == 0)
					atpdev->async[0] |= n;
L
Linus Torvalds 已提交
2376 2377
 			}
	 	}
2378 2379
		atpdev->async[0] = ~(atpdev->async[0]);
		outb(atpdev->global_map[0], base_io + 0x35);
L
Linus Torvalds 已提交
2380 2381 2382
 
		shpnt = scsi_host_alloc(&atp870u_template, sizeof(struct atp_unit));
		if (!shpnt)
2383
			goto err_nomem;
L
Linus Torvalds 已提交
2384 2385 2386

		p = (struct atp_unit *)&shpnt->hostdata;

2387 2388
		atpdev->host = shpnt;
		atpdev->pdev = pdev;
L
Linus Torvalds 已提交
2389
		pci_set_drvdata(pdev, p);
2390
		memcpy(p, atpdev, sizeof(*atpdev));
L
Linus Torvalds 已提交
2391 2392 2393 2394 2395
		if (atp870u_init_tables(shpnt) < 0) {
			printk(KERN_ERR "Unable to allocate tables for Acard controller\n");
			goto unregister;
		}

2396
		if (request_irq(pdev->irq, atp870u_intr_handle, IRQF_SHARED, "atp880i", shpnt)) {
L
Linus Torvalds 已提交
2397 2398 2399 2400 2401
 			printk(KERN_ERR "Unable to allocate IRQ%d for Acard controller.\n", pdev->irq);
			goto free_tables;
		}

		spin_lock_irqsave(shpnt->host_lock, flags);
O
Ondrej Zary 已提交
2402 2403 2404
		k = inb(base_io + 0x38) & 0x80;
		outb(k, base_io + 0x38);
		outb(0x20, base_io + 0x3b);
L
Linus Torvalds 已提交
2405
		mdelay(32);
O
Ondrej Zary 已提交
2406
		outb(0, base_io + 0x3b);
L
Linus Torvalds 已提交
2407
		mdelay(32);
O
Ondrej Zary 已提交
2408 2409 2410 2411 2412
		inb(base_io + 0x5b);
		inb(base_io + 0x57);
		outb((host_id | 0x08), base_io + 0x40);
		outb(0, base_io + 0x58);
		while ((inb(base_io + 0x5f) & 0x80) == 0)
L
Linus Torvalds 已提交
2413
			mdelay(1);
O
Ondrej Zary 已提交
2414 2415 2416 2417
		inb(base_io + 0x57);
		outb(8, base_io + 0x41);
		outb(0x7f, base_io + 0x42);
		outb(0x20, base_io + 0x51);
L
Linus Torvalds 已提交
2418 2419 2420

		tscam(shpnt);
		is880(p, base_io);
O
Ondrej Zary 已提交
2421
		outb(0xb0, base_io + 0x38);
L
Linus Torvalds 已提交
2422 2423 2424 2425 2426 2427 2428 2429 2430 2431
		shpnt->max_id = 16;
		shpnt->this_id = host_id;
		shpnt->unique_id = base_io;
		shpnt->io_port = base_io;
		shpnt->n_io_port = 0x60;	/* Number of bytes of I/O space used */
		shpnt->irq = pdev->irq;			
	} else if (ent->device == ATP885_DEVID) {	
			printk(KERN_INFO "   ACARD AEC-67162 PCI Ultra3 LVD Host Adapter:  IO:%x, IRQ:%d.\n"
			       , base_io, pdev->irq);
        	
2432 2433 2434 2435 2436 2437 2438
		atpdev->pdev = pdev;
		atpdev->dev_id  = ent->device;
		atpdev->baseport = base_io;
		atpdev->ioport[0] = base_io + 0x80;
		atpdev->ioport[1] = base_io + 0xc0;
		atpdev->pciport[0] = base_io + 0x40;
		atpdev->pciport[1] = base_io + 0x50;
L
Linus Torvalds 已提交
2439 2440 2441
				
		shpnt = scsi_host_alloc(&atp870u_template, sizeof(struct atp_unit));
		if (!shpnt)
2442
			goto err_nomem;
L
Linus Torvalds 已提交
2443 2444 2445
        	
		p = (struct atp_unit *)&shpnt->hostdata;
        	
2446 2447
		atpdev->host = shpnt;
		atpdev->pdev = pdev;
L
Linus Torvalds 已提交
2448
		pci_set_drvdata(pdev, p);
2449
		memcpy(p, atpdev, sizeof(struct atp_unit));
L
Linus Torvalds 已提交
2450 2451 2452 2453 2454 2455
		if (atp870u_init_tables(shpnt) < 0)
			goto unregister;
			
#ifdef ED_DBGP		
	printk("request_irq() shpnt %p hostdata %p\n", shpnt, p);
#endif	        
2456
		if (request_irq(pdev->irq, atp870u_intr_handle, IRQF_SHARED, "atp870u", shpnt)) {
L
Linus Torvalds 已提交
2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538
				printk(KERN_ERR "Unable to allocate IRQ for Acard controller.\n");
			goto free_tables;
		}
		
		spin_lock_irqsave(shpnt->host_lock, flags);        					
        			
		c=inb(base_io + 0x29);
		outb((c | 0x04),base_io + 0x29);
        	
		n=0x1f80;
next_fblk_885:
		if (n >= 0x2000) {
		   goto flash_ok_885;
		}
		outw(n,base_io + 0x3c);
		if (inl(base_io + 0x38) == 0xffffffff) {
		   goto flash_ok_885;
		}
		for (m=0; m < 2; m++) {
		    p->global_map[m]= 0;
		    for (k=0; k < 4; k++) {
			outw(n++,base_io + 0x3c);
			((unsigned long *)&setupdata[m][0])[k]=inl(base_io + 0x38);
		    }
		    for (k=0; k < 4; k++) {
			outw(n++,base_io + 0x3c);
			((unsigned long *)&p->sp[m][0])[k]=inl(base_io + 0x38);
		    }
		    n += 8;
		}
		goto next_fblk_885;
flash_ok_885:
#ifdef ED_DBGP
		printk( "Flash Read OK\n");
#endif	
		c=inb(base_io + 0x29);
		outb((c & 0xfb),base_io + 0x29);
		for (c=0;c < 2;c++) {
		    p->ultra_map[c]=0;
		    p->async[c] = 0;
		    for (k=0; k < 16; k++) {
			n=1;
			n = n << k;
			if (p->sp[c][k] > 1) {
			   p->ultra_map[c] |= n;
			} else {
			   if (p->sp[c][k] == 0) {
			      p->async[c] |= n;
			   }
			}
		    }
		    p->async[c] = ~(p->async[c]);

		    if (p->global_map[c] == 0) {
		       k=setupdata[c][1];
		       if ((k & 0x40) != 0)
			  p->global_map[c] |= 0x20;
		       k &= 0x07;
		       p->global_map[c] |= k;
		       if ((setupdata[c][2] & 0x04) != 0)
			  p->global_map[c] |= 0x08;
		       p->host_id[c] = setupdata[c][0] & 0x07;
		    }
		}

		k = inb(base_io + 0x28) & 0x8f;
		k |= 0x10;
		outb(k, base_io + 0x28);
		outb(0x80, base_io + 0x41);
		outb(0x80, base_io + 0x51);
		mdelay(100);
		outb(0, base_io + 0x41);
		outb(0, base_io + 0x51);
		mdelay(1000);
		inb(base_io + 0x9b);
		inb(base_io + 0x97);
		inb(base_io + 0xdb);
		inb(base_io + 0xd7);
		k=p->host_id[0];
		if (k > 7)
		   k = (k & 0x07) | 0x40;
		k |= 0x08;
O
Ondrej Zary 已提交
2539 2540
		outb(k, base_io + 0x80);
		outb(0, base_io + 0x98);
L
Linus Torvalds 已提交
2541

O
Ondrej Zary 已提交
2542
		while ((inb(base_io + 0x9f) & 0x80) == 0)
L
Linus Torvalds 已提交
2543 2544
			cpu_relax();
	
O
Ondrej Zary 已提交
2545 2546 2547 2548 2549
		inb(base_io + 0x97);
		outb(8, base_io + 0x81);
		outb(0x7f, base_io + 0x82);
		outb(0x20, base_io + 0x91);

L
Linus Torvalds 已提交
2550 2551 2552 2553
		k=p->host_id[1];
		if (k > 7)
		   k = (k & 0x07) | 0x40;
		k |= 0x08;
O
Ondrej Zary 已提交
2554 2555
		outb(k, base_io + 0xc0);
		outb(0, base_io + 0xd8);
L
Linus Torvalds 已提交
2556

O
Ondrej Zary 已提交
2557
		while ((inb(base_io + 0xdf) & 0x80) == 0)
L
Linus Torvalds 已提交
2558 2559
			cpu_relax();

O
Ondrej Zary 已提交
2560 2561 2562 2563
		inb(base_io + 0xd7);
		outb(8, base_io + 0xc1);
		outb(0x7f, base_io + 0xc2);
		outb(0x20, base_io + 0xd1);
L
Linus Torvalds 已提交
2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595

		tscam_885();
		printk(KERN_INFO "   Scanning Channel A SCSI Device ...\n");
		is885(p, base_io + 0x80, 0);
		printk(KERN_INFO "   Scanning Channel B SCSI Device ...\n");
		is885(p, base_io + 0xc0, 1);

		k = inb(base_io + 0x28) & 0xcf;
		k |= 0xc0;
		outb(k, base_io + 0x28);
		k = inb(base_io + 0x1f) | 0x80;
		outb(k, base_io + 0x1f);
		k = inb(base_io + 0x29) | 0x01;
		outb(k, base_io + 0x29);
#ifdef ED_DBGP
		//printk("atp885: atp_host[0] 0x%p\n", atp_host[0]);
#endif		
		shpnt->max_id = 16;
		shpnt->max_lun = (p->global_map[0] & 0x07) + 1;
		shpnt->max_channel = 1;
		shpnt->this_id = p->host_id[0];
		shpnt->unique_id = base_io;
		shpnt->io_port = base_io;
		shpnt->n_io_port = 0xff;	/* Number of bytes of I/O space used */
		shpnt->irq = pdev->irq;
				
	} else {
		error = pci_read_config_byte(pdev, 0x49, &host_id);

		printk(KERN_INFO "   ACARD AEC-671X PCI Ultra/W SCSI-2/3 Host Adapter: %d "
			"IO:%x, IRQ:%d.\n", count, base_io, pdev->irq);

2596 2597 2598
		atpdev->ioport[0] = base_io;
		atpdev->pciport[0] = base_io + 0x20;
		atpdev->dev_id = ent->device;
L
Linus Torvalds 已提交
2599
		host_id &= 0x07;
2600
		atpdev->host_id[0] = host_id;
O
Ondrej Zary 已提交
2601 2602 2603
		atpdev->scam_on = inb(base_io + 0x22);
		atpdev->global_map[0] = inb(base_io + 0x2d);
		atpdev->ultra_map[0] = inw(base_io + 0x2e);
L
Linus Torvalds 已提交
2604

2605 2606 2607 2608
		if (atpdev->ultra_map[0] == 0) {
			atpdev->scam_on = 0x00;
			atpdev->global_map[0] = 0x20;
			atpdev->ultra_map[0] = 0xffff;
L
Linus Torvalds 已提交
2609 2610 2611 2612
		}

		shpnt = scsi_host_alloc(&atp870u_template, sizeof(struct atp_unit));
		if (!shpnt)
2613
			goto err_nomem;
L
Linus Torvalds 已提交
2614 2615 2616

		p = (struct atp_unit *)&shpnt->hostdata;
		
2617 2618
		atpdev->host = shpnt;
		atpdev->pdev = pdev;
L
Linus Torvalds 已提交
2619
		pci_set_drvdata(pdev, p);
2620
		memcpy(p, atpdev, sizeof(*atpdev));
L
Linus Torvalds 已提交
2621 2622 2623
		if (atp870u_init_tables(shpnt) < 0)
			goto unregister;

2624
		if (request_irq(pdev->irq, atp870u_intr_handle, IRQF_SHARED, "atp870i", shpnt)) {
L
Linus Torvalds 已提交
2625 2626 2627 2628 2629
			printk(KERN_ERR "Unable to allocate IRQ%d for Acard controller.\n", pdev->irq);
			goto free_tables;
		}

		spin_lock_irqsave(shpnt->host_lock, flags);
2630
		if (atpdev->chip_ver > 0x07) {	/* check if atp876 chip then enable terminator */
O
Ondrej Zary 已提交
2631
			outb(0x00, base_io + 0x3e);
L
Linus Torvalds 已提交
2632 2633
		}
 
O
Ondrej Zary 已提交
2634 2635 2636
		k = (inb(base_io + 0x3a) & 0xf3) | 0x10;
		outb(k, base_io + 0x3a);
		outb((k & 0xdf), base_io + 0x3a);
L
Linus Torvalds 已提交
2637
		mdelay(32);
O
Ondrej Zary 已提交
2638
		outb(k, base_io + 0x3a);
L
Linus Torvalds 已提交
2639
		mdelay(32);
O
Ondrej Zary 已提交
2640 2641 2642
		outb((host_id | 0x08), base_io + 0);
		outb(0, base_io + 0x18);
		while ((inb(base_io + 0x1f) & 0x80) == 0)
L
Linus Torvalds 已提交
2643 2644
			mdelay(1);

O
Ondrej Zary 已提交
2645 2646 2647 2648
		inb(base_io + 0x17);
		outb(8, base_io + 1);
		outb(0x7f, base_io + 2);
		outb(0x20, base_io + 0x11);
L
Linus Torvalds 已提交
2649 2650 2651

		tscam(shpnt);
		is870(p, base_io);
O
Ondrej Zary 已提交
2652 2653
		outb((inb(base_io + 0x3a) & 0xef), base_io + 0x3a);
		outb((inb(base_io + 0x3b) | 0x20), base_io + 0x3b);
2654
		if (atpdev->chip_ver == 4)
L
Linus Torvalds 已提交
2655 2656
			shpnt->max_id = 16;
		else		
2657
			shpnt->max_id = 8;
L
Linus Torvalds 已提交
2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702
		shpnt->this_id = host_id;
		shpnt->unique_id = base_io;
		shpnt->io_port = base_io;
		shpnt->n_io_port = 0x40;	/* Number of bytes of I/O space used */
		shpnt->irq = pdev->irq;		
	} 
		spin_unlock_irqrestore(shpnt->host_lock, flags);
		if(ent->device==ATP885_DEVID) {
			if(!request_region(base_io, 0xff, "atp870u")) /* Register the IO ports that we use */
				goto request_io_fail;
		} else if((ent->device==ATP880_DEVID1)||(ent->device==ATP880_DEVID2)) {
			if(!request_region(base_io, 0x60, "atp870u")) /* Register the IO ports that we use */
				goto request_io_fail;
		} else {
			if(!request_region(base_io, 0x40, "atp870u")) /* Register the IO ports that we use */
				goto request_io_fail;
		}				
		count++;
		if (scsi_add_host(shpnt, &pdev->dev))
			goto scsi_add_fail;
		scsi_scan_host(shpnt);
#ifdef ED_DBGP			
		printk("atp870u_prob : exit\n");
#endif		
		return 0;

scsi_add_fail:
	printk("atp870u_prob:scsi_add_fail\n");
	if(ent->device==ATP885_DEVID) {
		release_region(base_io, 0xff);
	} else if((ent->device==ATP880_DEVID1)||(ent->device==ATP880_DEVID2)) {
		release_region(base_io, 0x60);
	} else {
		release_region(base_io, 0x40);
	}
request_io_fail:
	printk("atp870u_prob:request_io_fail\n");
	free_irq(pdev->irq, shpnt);
free_tables:
	printk("atp870u_prob:free_table\n");
	atp870u_free_tables(shpnt);
unregister:
	printk("atp870u_prob:unregister\n");
	scsi_host_put(shpnt);
	return -1;		
2703 2704 2705 2706 2707 2708
err_eio:
	kfree(atpdev);
	return -EIO;
err_nomem:
	kfree(atpdev);
	return -ENOMEM;
L
Linus Torvalds 已提交
2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723
}

/* 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;
2724
	c = scmd_channel(SCpnt);
L
Linus Torvalds 已提交
2725 2726 2727 2728
	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++) {
O
Ondrej Zary 已提交
2729
		printk(" r%2x=%2x", j, inb(dev->ioport[c] + j));
L
Linus Torvalds 已提交
2730
	}
O
Ondrej Zary 已提交
2731 2732 2733 2734
	printk(" r1c=%2x", inb(dev->ioport[c] + 0x1c));
	printk(" r1f=%2x in_snd=%2x ", inb(dev->ioport[c] + 0x1f), dev->in_snd[c]);
	printk(" d00=%2x", inb(dev->pciport[c]));
	printk(" d02=%2x", inb(dev->pciport[c] + 0x02));
L
Linus Torvalds 已提交
2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756
	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 已提交
2757
static int atp870u_show_info(struct seq_file *m, struct Scsi_Host *HBAptr)
L
Linus Torvalds 已提交
2758
{
2759 2760
	seq_puts(m, "ACARD AEC-671X Driver Version: 2.6+ac\n\n"
		"Adapter Configuration:\n");
A
Al Viro 已提交
2761 2762 2763
	seq_printf(m, "               Base IO: %#.4lx\n", HBAptr->io_port);
	seq_printf(m, "                   IRQ: %d\n", HBAptr->irq);
	return 0;
L
Linus Torvalds 已提交
2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807
}


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);
	printk(KERN_INFO "free_irq : %d\n",pshost->irq);
	free_irq(pshost->irq, pshost);
	release_region(pshost->io_port, pshost->n_io_port);
	printk(KERN_INFO "atp870u_free_tables : %p\n",pshost);
	atp870u_free_tables(pshost);
	printk(KERN_INFO "scsi_host_put : %p\n",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 已提交
2808
     .show_info			= atp870u_show_info,
L
Linus Torvalds 已提交
2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839
     .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,
2840
	.remove		= atp870u_remove,
L
Linus Torvalds 已提交
2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551
};

static int __init atp870u_init(void)
{
#ifdef ED_DBGP	
	printk("atp870u_init: Entry\n");
#endif	
	return pci_register_driver(&atp870u_driver);
}

static void __exit atp870u_exit(void)
{
#ifdef ED_DBGP	
	printk("atp870u_exit: Entry\n");
#endif
	pci_unregister_driver(&atp870u_driver);
}

static void tscam_885(void)
{
	unsigned char i;

	for (i = 0; i < 0x2; i++) {
		mdelay(300);
	}
	return;
}



static void is885(struct atp_unit *dev, unsigned int wkport,unsigned char c)
{
	unsigned int tmport;
	unsigned char i, j, k, rmb, n, lvdmode;
	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};
	unsigned char synu[6] =  {0x80, 1, 3, 1, 0x0a, 0x0e};
	static unsigned char synw[6] =	{0x80, 1, 3, 1, 0x19, 0x0e};
	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 };

	lvdmode=inb(wkport + 0x1b) >> 7;

	for (i = 0; i < 16; i++) {
		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;
		}
		tmport = wkport + 0x1b;
		outb(0x01, tmport);
		tmport = wkport + 0x01;
		outb(0x08, tmport++);
		outb(0x7f, tmport++);
		outb(satn[0], tmport++);
		outb(satn[1], tmport++);
		outb(satn[2], tmport++);
		outb(satn[3], tmport++);
		outb(satn[4], tmport++);
		outb(satn[5], tmport++);
		tmport += 0x06;
		outb(0, tmport);
		tmport += 0x02;
		outb(dev->id[c][i].devsp, tmport++);
		
		outb(0, tmport++);
		outb(satn[6], tmport++);
		outb(satn[7], tmport++);
		j = i;
		if ((j & 0x08) != 0) {
			j = (j & 0x07) | 0x40;
		}
		outb(j, tmport);
		tmport += 0x03;
		outb(satn[8], tmport);
		tmport += 0x07;

		while ((inb(tmport) & 0x80) == 0x00)
			cpu_relax();
		tmport -= 0x08;
		if ((inb(tmport) != 0x11) && (inb(tmport) != 0x8e)) {
			continue;
		}
		while (inb(tmport) != 0x8e)
			cpu_relax();
		dev->active_id[c] |= m;

		tmport = wkport + 0x10;
		outb(0x30, tmport);
		tmport = wkport + 0x14;
		outb(0x00, tmport);

phase_cmd:
		tmport = wkport + 0x18;
		outb(0x08, tmport);
		tmport += 0x07;
		while ((inb(tmport) & 0x80) == 0x00)
			cpu_relax();
		tmport -= 0x08;
		j = inb(tmport);
		if (j != 0x16) {
			tmport = wkport + 0x10;
			outb(0x41, tmport);
			goto phase_cmd;
		}
sel_ok:
		tmport = wkport + 0x03;
		outb(inqd[0], tmport++);
		outb(inqd[1], tmport++);
		outb(inqd[2], tmport++);
		outb(inqd[3], tmport++);
		outb(inqd[4], tmport++);
		outb(inqd[5], tmport);
		tmport += 0x07;
		outb(0, tmport);
		tmport += 0x02;
		outb(dev->id[c][i].devsp, tmport++);
		outb(0, tmport++);
		outb(inqd[6], tmport++);
		outb(inqd[7], tmport++);
		tmport += 0x03;
		outb(inqd[8], tmport);
		tmport += 0x07;
		while ((inb(tmport) & 0x80) == 0x00)
			cpu_relax();
		tmport -= 0x08;
		if ((inb(tmport) != 0x11) && (inb(tmport) != 0x8e)) {
			continue;
		}
		while (inb(tmport) != 0x8e)
			cpu_relax();
		tmport = wkport + 0x1b;
		outb(0x00, tmport);
		tmport = wkport + 0x18;
		outb(0x08, tmport);
		tmport += 0x07;
		j = 0;
rd_inq_data:
		k = inb(tmport);
		if ((k & 0x01) != 0) {
			tmport -= 0x06;
			mbuf[j++] = inb(tmport);
			tmport += 0x06;
			goto rd_inq_data;
		}
		if ((k & 0x80) == 0) {
			goto rd_inq_data;
		}
		tmport -= 0x08;
		j = inb(tmport);
		if (j == 0x16) {
			goto inq_ok;
		}
		tmport = wkport + 0x10;
		outb(0x46, tmport);
		tmport += 0x02;
		outb(0, tmport++);
		outb(0, tmport++);
		outb(0, tmport++);
		tmport += 0x03;
		outb(0x08, tmport);
		tmport += 0x07;
		while ((inb(tmport) & 0x80) == 0x00)
			cpu_relax();
		tmport -= 0x08;
		if (inb(tmport) != 0x16) {
			goto sel_ok;
		}
inq_ok:
		mbuf[36] = 0;
		printk( KERN_INFO"         ID: %2d  %s\n", i, &mbuf[8]);
		dev->id[c][i].devtype = mbuf[0];
		rmb = mbuf[1];
		n = mbuf[7];
		if ((mbuf[7] & 0x60) == 0) {
			goto not_wide;
		}
		if ((i < 8) && ((dev->global_map[c] & 0x20) == 0)) {
			goto not_wide;
		}
		if (lvdmode == 0) {
		   goto chg_wide;
		}
		if (dev->sp[c][i] != 0x04) {	// force u2
		   goto chg_wide;
		}

		tmport = wkport + 0x1b;
		outb(0x01, tmport);
		tmport = wkport + 0x03;
		outb(satn[0], tmport++);
		outb(satn[1], tmport++);
		outb(satn[2], tmport++);
		outb(satn[3], tmport++);
		outb(satn[4], tmport++);
		outb(satn[5], tmport++);
		tmport += 0x06;
		outb(0, tmport);
		tmport += 0x02;
		outb(dev->id[c][i].devsp, tmport++);
		outb(0, tmport++);
		outb(satn[6], tmport++);
		outb(satn[7], tmport++);
		tmport += 0x03;
		outb(satn[8], tmport);
		tmport += 0x07;

		while ((inb(tmport) & 0x80) == 0x00)
			cpu_relax();
		tmport -= 0x08;
		if ((inb(tmport) != 0x11) && (inb(tmport) != 0x8e)) {
			continue;
		}
		while (inb(tmport) != 0x8e)
			cpu_relax();
try_u3:
		j = 0;
		tmport = wkport + 0x14;
		outb(0x09, tmport);
		tmport += 0x04;
		outb(0x20, tmport);
		tmport += 0x07;

		while ((inb(tmport) & 0x80) == 0) {
			if ((inb(tmport) & 0x01) != 0) {
				tmport -= 0x06;
				outb(u3[j++], tmport);
				tmport += 0x06;
			}
			cpu_relax();
		}
		tmport -= 0x08;
		while ((inb(tmport) & 0x80) == 0x00)
			cpu_relax();
		j = inb(tmport) & 0x0f;
		if (j == 0x0f) {
			goto u3p_in;
		}
		if (j == 0x0a) {
			goto u3p_cmd;
		}
		if (j == 0x0e) {
			goto try_u3;
		}
		continue;
u3p_out:
		tmport = wkport + 0x18;
		outb(0x20, tmport);
		tmport += 0x07;
		while ((inb(tmport) & 0x80) == 0) {
			if ((inb(tmport) & 0x01) != 0) {
				tmport -= 0x06;
				outb(0, tmport);
				tmport += 0x06;
			}
			cpu_relax();
		}
		tmport -= 0x08;
		j = inb(tmport) & 0x0f;
		if (j == 0x0f) {
			goto u3p_in;
		}
		if (j == 0x0a) {
			goto u3p_cmd;
		}
		if (j == 0x0e) {
			goto u3p_out;
		}
		continue;
u3p_in:
		tmport = wkport + 0x14;
		outb(0x09, tmport);
		tmport += 0x04;
		outb(0x20, tmport);
		tmport += 0x07;
		k = 0;
u3p_in1:
		j = inb(tmport);
		if ((j & 0x01) != 0) {
			tmport -= 0x06;
			mbuf[k++] = inb(tmport);
			tmport += 0x06;
			goto u3p_in1;
		}
		if ((j & 0x80) == 0x00) {
			goto u3p_in1;
		}
		tmport -= 0x08;
		j = inb(tmport) & 0x0f;
		if (j == 0x0f) {
			goto u3p_in;
		}
		if (j == 0x0a) {
			goto u3p_cmd;
		}
		if (j == 0x0e) {
			goto u3p_out;
		}
		continue;
u3p_cmd:
		tmport = wkport + 0x10;
		outb(0x30, tmport);
		tmport = wkport + 0x14;
		outb(0x00, tmport);
		tmport += 0x04;
		outb(0x08, tmport);
		tmport += 0x07;
		while ((inb(tmport) & 0x80) == 0x00);
		tmport -= 0x08;
		j = inb(tmport);
		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:
		tmport = wkport + 0x1b;
		outb(0x01, tmport);
		tmport = wkport + 0x03;
		outb(satn[0], tmport++);
		outb(satn[1], tmport++);
		outb(satn[2], tmport++);
		outb(satn[3], tmport++);
		outb(satn[4], tmport++);
		outb(satn[5], tmport++);
		tmport += 0x06;
		outb(0, tmport);
		tmport += 0x02;
		outb(dev->id[c][i].devsp, tmport++);
		outb(0, tmport++);
		outb(satn[6], tmport++);
		outb(satn[7], tmport++);
		tmport += 0x03;
		outb(satn[8], tmport);
		tmport += 0x07;

		while ((inb(tmport) & 0x80) == 0x00)
			cpu_relax();
		tmport -= 0x08;
		if ((inb(tmport) != 0x11) && (inb(tmport) != 0x8e)) {
			continue;
		}
		while (inb(tmport) != 0x8e)
			cpu_relax();
try_wide:
		j = 0;
		tmport = wkport + 0x14;
		outb(0x05, tmport);
		tmport += 0x04;
		outb(0x20, tmport);
		tmport += 0x07;

		while ((inb(tmport) & 0x80) == 0) {
			if ((inb(tmport) & 0x01) != 0) {
				tmport -= 0x06;
				outb(wide[j++], tmport);
				tmport += 0x06;
			}
			cpu_relax();
		}
		tmport -= 0x08;
		while ((inb(tmport) & 0x80) == 0x00)
			cpu_relax();
		j = inb(tmport) & 0x0f;
		if (j == 0x0f) {
			goto widep_in;
		}
		if (j == 0x0a) {
			goto widep_cmd;
		}
		if (j == 0x0e) {
			goto try_wide;
		}
		continue;
widep_out:
		tmport = wkport + 0x18;
		outb(0x20, tmport);
		tmport += 0x07;
		while ((inb(tmport) & 0x80) == 0) {
			if ((inb(tmport) & 0x01) != 0) {
				tmport -= 0x06;
				outb(0, tmport);
				tmport += 0x06;
			}
			cpu_relax();
		}
		tmport -= 0x08;
		j = inb(tmport) & 0x0f;
		if (j == 0x0f) {
			goto widep_in;
		}
		if (j == 0x0a) {
			goto widep_cmd;
		}
		if (j == 0x0e) {
			goto widep_out;
		}
		continue;
widep_in:
		tmport = wkport + 0x14;
		outb(0xff, tmport);
		tmport += 0x04;
		outb(0x20, tmport);
		tmport += 0x07;
		k = 0;
widep_in1:
		j = inb(tmport);
		if ((j & 0x01) != 0) {
			tmport -= 0x06;
			mbuf[k++] = inb(tmport);
			tmport += 0x06;
			goto widep_in1;
		}
		if ((j & 0x80) == 0x00) {
			goto widep_in1;
		}
		tmport -= 0x08;
		j = inb(tmport) & 0x0f;
		if (j == 0x0f) {
			goto widep_in;
		}
		if (j == 0x0a) {
			goto widep_cmd;
		}
		if (j == 0x0e) {
			goto widep_out;
		}
		continue;
widep_cmd:
		tmport = wkport + 0x10;
		outb(0x30, tmport);
		tmport = wkport + 0x14;
		outb(0x00, tmport);
		tmport += 0x04;
		outb(0x08, tmport);
		tmport += 0x07;
		while ((inb(tmport) & 0x80) == 0x00)
			cpu_relax();
		tmport -= 0x08;
		j = inb(tmport);
		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:
		if ((dev->id[c][i].devtype == 0x00) || (dev->id[c][i].devtype == 0x07) ||
		    ((dev->id[c][i].devtype == 0x05) && ((n & 0x10) != 0))) {
			m = 1;
			m = m << i;
			if ((dev->async[c] & m) != 0) {
			   goto set_sync;
			}
		}
		continue;
set_sync:
		if (dev->sp[c][i] == 0x02) {
		   synu[4]=0x0c;
		   synuw[4]=0x0c;
		} else {
		   if (dev->sp[c][i] >= 0x03) {
		      synu[4]=0x0a;
		      synuw[4]=0x0a;
		   }
		}
		tmport = wkport + 0x1b;
		j = 0;
		if ((m & dev->wide_id[c]) != 0) {
			j |= 0x01;
		}
		outb(j, tmport);
		tmport = wkport + 0x03;
		outb(satn[0], tmport++);
		outb(satn[1], tmport++);
		outb(satn[2], tmport++);
		outb(satn[3], tmport++);
		outb(satn[4], tmport++);
		outb(satn[5], tmport++);
		tmport += 0x06;
		outb(0, tmport);
		tmport += 0x02;
		outb(dev->id[c][i].devsp, tmport++);
		outb(0, tmport++);
		outb(satn[6], tmport++);
		outb(satn[7], tmport++);
		tmport += 0x03;
		outb(satn[8], tmport);
		tmport += 0x07;

		while ((inb(tmport) & 0x80) == 0x00)
			cpu_relax();
		tmport -= 0x08;
		if ((inb(tmport) != 0x11) && (inb(tmport) != 0x8e)) {
			continue;
		}
		while (inb(tmport) != 0x8e)
			cpu_relax();
try_sync:
		j = 0;
		tmport = wkport + 0x14;
		outb(0x06, tmport);
		tmport += 0x04;
		outb(0x20, tmport);
		tmport += 0x07;

		while ((inb(tmport) & 0x80) == 0) {
			if ((inb(tmport) & 0x01) != 0) {
				tmport -= 0x06;
				if ((m & dev->wide_id[c]) != 0) {
					if ((m & dev->ultra_map[c]) != 0) {
						outb(synuw[j++], tmport);
					} else {
						outb(synw[j++], tmport);
					}
				} else {
					if ((m & dev->ultra_map[c]) != 0) {
						outb(synu[j++], tmport);
					} else {
						outb(synn[j++], tmport);
					}
				}
				tmport += 0x06;
			}
		}
		tmport -= 0x08;
		while ((inb(tmport) & 0x80) == 0x00)
			cpu_relax();
		j = inb(tmport) & 0x0f;
		if (j == 0x0f) {
			goto phase_ins;
		}
		if (j == 0x0a) {
			goto phase_cmds;
		}
		if (j == 0x0e) {
			goto try_sync;
		}
		continue;
phase_outs:
		tmport = wkport + 0x18;
		outb(0x20, tmport);
		tmport += 0x07;
		while ((inb(tmport) & 0x80) == 0x00) {
			if ((inb(tmport) & 0x01) != 0x00) {
				tmport -= 0x06;
				outb(0x00, tmport);
				tmport += 0x06;
			}
			cpu_relax();
		}
		tmport -= 0x08;
		j = inb(tmport);
		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:
		tmport = wkport + 0x14;
		outb(0x06, tmport);
		tmport += 0x04;
		outb(0x20, tmport);
		tmport += 0x07;
		k = 0;
phase_ins1:
		j = inb(tmport);
		if ((j & 0x01) != 0x00) {
			tmport -= 0x06;
			mbuf[k++] = inb(tmport);
			tmport += 0x06;
			goto phase_ins1;
		}
		if ((j & 0x80) == 0x00) {
			goto phase_ins1;
		}
		tmport -= 0x08;
		while ((inb(tmport) & 0x80) == 0x00);
		j = inb(tmport);
		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:
		tmport = wkport + 0x10;
		outb(0x30, tmport);
tar_dcons:
		tmport = wkport + 0x14;
		outb(0x00, tmport);
		tmport += 0x04;
		outb(0x08, tmport);
		tmport += 0x07;
		while ((inb(tmport) & 0x80) == 0x00)
			cpu_relax();
		tmport -= 0x08;
		j = inb(tmport);
		if (j != 0x16) {
			continue;
		}
		if (mbuf[0] != 0x01) {
			continue;
		}
		if (mbuf[1] != 0x03) {
			continue;
		}
		if (mbuf[4] == 0x00) {
			continue;
		}
		if (mbuf[3] > 0x64) {
			continue;
		}
		if (mbuf[4] > 0x0e) {
			mbuf[4] = 0x0e;
		}
		dev->id[c][i].devsp = mbuf[4];
		if (mbuf[3] < 0x0c){
			j = 0xb0;
			goto set_syn_ok;
		}
		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;
	      set_syn_ok:
		dev->id[c][i].devsp = (dev->id[c][i].devsp & 0x0f) | j;
#ifdef ED_DBGP		
		printk("dev->id[%2d][%2d].devsp = %2x\n",c,i,dev->id[c][i].devsp);
#endif
	}
	tmport = wkport + 0x16;
	outb(0x80, tmport);
}

module_init(atp870u_init);
module_exit(atp870u_exit);