]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/scsi/atp870u.c
atp870u: Improve _probe()
[mirror_ubuntu-artful-kernel.git] / drivers / scsi / atp870u.c
CommitLineData
1da177e4
LT
1/*
2 * Copyright (C) 1997 Wu Ching Chen
3 * 2.1.x update (C) 1998 Krzysztof G. Baranowski
fa195afe
AC
4 * 2.5.x update (C) 2002 Red Hat
5 * 2.6.x update (C) 2004 Red Hat
1da177e4
LT
6 *
7 * Marcelo Tosatti <marcelo@conectiva.com.br> : SMP fixes
8 *
9 * Wu Ching Chen : NULL pointer fixes 2000/06/02
10 * support atp876 chip
11 * enable 32 bit fifo transfer
12 * support cdrom & remove device run ultra speed
13 * fix disconnect bug 2000/12/21
14 * support atp880 chip lvd u160 2001/05/15
15 * fix prd table bug 2001/09/12 (7.1)
16 *
17 * atp885 support add by ACARD Hao Ping Lian 2005/01/05
18 */
19#include <linux/module.h>
20#include <linux/init.h>
21#include <linux/interrupt.h>
22#include <linux/kernel.h>
23#include <linux/types.h>
24#include <linux/string.h>
25#include <linux/ioport.h>
26#include <linux/delay.h>
27#include <linux/proc_fs.h>
28#include <linux/spinlock.h>
29#include <linux/pci.h>
30#include <linux/blkdev.h>
910638ae 31#include <linux/dma-mapping.h>
5a0e3ad6 32#include <linux/slab.h>
1da177e4
LT
33#include <asm/io.h>
34
35#include <scsi/scsi.h>
36#include <scsi/scsi_cmnd.h>
37#include <scsi/scsi_device.h>
38#include <scsi/scsi_host.h>
39
40#include "atp870u.h"
41
42static struct scsi_host_template atp870u_template;
43static void send_s870(struct atp_unit *dev,unsigned char c);
4192a40f 44static void atp_is(struct atp_unit *dev, unsigned char c, bool wide_chip, unsigned char lvdmode);
1da177e4 45
6a3cebb6
OZ
46static inline void atp_writeb_base(struct atp_unit *atp, u8 reg, u8 val)
47{
48 outb(val, atp->baseport + reg);
49}
50
d804bb25
OZ
51static inline void atp_writew_base(struct atp_unit *atp, u8 reg, u16 val)
52{
53 outw(val, atp->baseport + reg);
54}
55
6a3cebb6
OZ
56static inline void atp_writeb_io(struct atp_unit *atp, u8 channel, u8 reg, u8 val)
57{
58 outb(val, atp->ioport[channel] + reg);
59}
60
61static inline void atp_writew_io(struct atp_unit *atp, u8 channel, u8 reg, u16 val)
62{
63 outw(val, atp->ioport[channel] + reg);
64}
65
66static inline void atp_writeb_pci(struct atp_unit *atp, u8 channel, u8 reg, u8 val)
67{
68 outb(val, atp->pciport[channel] + reg);
69}
70
71static inline void atp_writel_pci(struct atp_unit *atp, u8 channel, u8 reg, u32 val)
72{
73 outl(val, atp->pciport[channel] + reg);
74}
75
76static inline u8 atp_readb_base(struct atp_unit *atp, u8 reg)
77{
78 return inb(atp->baseport + reg);
79}
80
d804bb25
OZ
81static inline u16 atp_readw_base(struct atp_unit *atp, u8 reg)
82{
83 return inw(atp->baseport + reg);
84}
85
86static inline u32 atp_readl_base(struct atp_unit *atp, u8 reg)
87{
88 return inl(atp->baseport + reg);
89}
90
6a3cebb6
OZ
91static inline u8 atp_readb_io(struct atp_unit *atp, u8 channel, u8 reg)
92{
93 return inb(atp->ioport[channel] + reg);
94}
95
96static inline u16 atp_readw_io(struct atp_unit *atp, u8 channel, u8 reg)
97{
98 return inw(atp->ioport[channel] + reg);
99}
100
101static inline u8 atp_readb_pci(struct atp_unit *atp, u8 channel, u8 reg)
102{
103 return inb(atp->pciport[channel] + reg);
104}
105
7d12e780 106static irqreturn_t atp870u_intr_handle(int irq, void *dev_id)
1da177e4
LT
107{
108 unsigned long flags;
bc0fe4c9 109 unsigned short int id;
1da177e4
LT
110 unsigned char i, j, c, target_id, lun,cmdp;
111 unsigned char *prd;
112 struct scsi_cmnd *workreq;
1da177e4
LT
113 unsigned long adrcnt, k;
114#ifdef ED_DBGP
115 unsigned long l;
116#endif
1da177e4
LT
117 struct Scsi_Host *host = dev_id;
118 struct atp_unit *dev = (struct atp_unit *)&host->hostdata;
119
120 for (c = 0; c < 2; c++) {
6a3cebb6 121 j = atp_readb_io(dev, c, 0x1f);
1da177e4 122 if ((j & 0x80) != 0)
78614ecd 123 break;
1da177e4
LT
124 dev->in_int[c] = 0;
125 }
78614ecd
OZ
126 if ((j & 0x80) == 0)
127 return IRQ_NONE;
1da177e4
LT
128#ifdef ED_DBGP
129 printk("atp870u_intr_handle enter\n");
130#endif
131 dev->in_int[c] = 1;
6a3cebb6 132 cmdp = atp_readb_io(dev, c, 0x10);
1da177e4
LT
133 if (dev->working[c] != 0) {
134 if (dev->dev_id == ATP885_DEVID) {
6a3cebb6
OZ
135 if ((atp_readb_io(dev, c, 0x16) & 0x80) == 0)
136 atp_writeb_io(dev, c, 0x16, (atp_readb_io(dev, c, 0x16) | 0x80));
1da177e4 137 }
6a3cebb6 138 if ((atp_readb_pci(dev, c, 0x00) & 0x08) != 0)
1da177e4 139 {
1da177e4 140 for (k=0; k < 1000; k++) {
6a3cebb6 141 if ((atp_readb_pci(dev, c, 2) & 0x08) == 0)
78614ecd 142 break;
6a3cebb6 143 if ((atp_readb_pci(dev, c, 2) & 0x01) == 0)
78614ecd 144 break;
1da177e4
LT
145 }
146 }
6a3cebb6 147 atp_writeb_pci(dev, c, 0, 0x00);
1da177e4 148
6a3cebb6 149 i = atp_readb_io(dev, c, 0x17);
1da177e4 150
bc0fe4c9 151 if (dev->dev_id == ATP885_DEVID)
6a3cebb6 152 atp_writeb_pci(dev, c, 2, 0x06);
1da177e4 153
6a3cebb6 154 target_id = atp_readb_io(dev, c, 0x15);
1da177e4
LT
155
156 /*
157 * Remap wide devices onto id numbers
158 */
159
160 if ((target_id & 0x40) != 0) {
161 target_id = (target_id & 0x07) | 0x08;
162 } else {
163 target_id &= 0x07;
164 }
165
166 if ((j & 0x40) != 0) {
167 if (dev->last_cmd[c] == 0xff) {
168 dev->last_cmd[c] = target_id;
169 }
170 dev->last_cmd[c] |= 0x40;
171 }
172 if (dev->dev_id == ATP885_DEVID)
173 dev->r1f[c][target_id] |= j;
174#ifdef ED_DBGP
175 printk("atp870u_intr_handle status = %x\n",i);
176#endif
177 if (i == 0x85) {
178 if ((dev->last_cmd[c] & 0xf0) != 0x40) {
179 dev->last_cmd[c] = 0xff;
180 }
181 if (dev->dev_id == ATP885_DEVID) {
1da177e4 182 adrcnt = 0;
6a3cebb6
OZ
183 ((unsigned char *) &adrcnt)[2] = atp_readb_io(dev, c, 0x12);
184 ((unsigned char *) &adrcnt)[1] = atp_readb_io(dev, c, 0x13);
185 ((unsigned char *) &adrcnt)[0] = atp_readb_io(dev, c, 0x14);
1da177e4
LT
186 if (dev->id[c][target_id].last_len != adrcnt)
187 {
188 k = dev->id[c][target_id].last_len;
189 k -= adrcnt;
190 dev->id[c][target_id].tran_len = k;
191 dev->id[c][target_id].last_len = adrcnt;
192 }
193#ifdef ED_DBGP
3a38e53e 194 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);
1da177e4
LT
195#endif
196 }
197
198 /*
199 * Flip wide
200 */
201 if (dev->wide_id[c] != 0) {
6a3cebb6
OZ
202 atp_writeb_io(dev, c, 0x1b, 0x01);
203 while ((atp_readb_io(dev, c, 0x1b) & 0x01) != 0x01)
204 atp_writeb_io(dev, c, 0x1b, 0x01);
1da177e4
LT
205 }
206 /*
207 * Issue more commands
208 */
209 spin_lock_irqsave(dev->host->host_lock, flags);
210 if (((dev->quhd[c] != dev->quend[c]) || (dev->last_cmd[c] != 0xff)) &&
211 (dev->in_snd[c] == 0)) {
212#ifdef ED_DBGP
213 printk("Call sent_s870\n");
214#endif
215 send_s870(dev,c);
216 }
217 spin_unlock_irqrestore(dev->host->host_lock, flags);
218 /*
219 * Done
220 */
221 dev->in_int[c] = 0;
222#ifdef ED_DBGP
223 printk("Status 0x85 return\n");
224#endif
78614ecd 225 return IRQ_HANDLED;
1da177e4
LT
226 }
227
228 if (i == 0x40) {
229 dev->last_cmd[c] |= 0x40;
230 dev->in_int[c] = 0;
78614ecd 231 return IRQ_HANDLED;
1da177e4
LT
232 }
233
234 if (i == 0x21) {
235 if ((dev->last_cmd[c] & 0xf0) != 0x40) {
236 dev->last_cmd[c] = 0xff;
237 }
1da177e4 238 adrcnt = 0;
6a3cebb6
OZ
239 ((unsigned char *) &adrcnt)[2] = atp_readb_io(dev, c, 0x12);
240 ((unsigned char *) &adrcnt)[1] = atp_readb_io(dev, c, 0x13);
241 ((unsigned char *) &adrcnt)[0] = atp_readb_io(dev, c, 0x14);
1da177e4
LT
242 k = dev->id[c][target_id].last_len;
243 k -= adrcnt;
244 dev->id[c][target_id].tran_len = k;
245 dev->id[c][target_id].last_len = adrcnt;
6a3cebb6
OZ
246 atp_writeb_io(dev, c, 0x10, 0x41);
247 atp_writeb_io(dev, c, 0x18, 0x08);
1da177e4 248 dev->in_int[c] = 0;
78614ecd 249 return IRQ_HANDLED;
1da177e4
LT
250 }
251
252 if (dev->dev_id == ATP885_DEVID) {
253 if ((i == 0x4c) || (i == 0x4d) || (i == 0x8c) || (i == 0x8d)) {
254 if ((i == 0x4c) || (i == 0x8c))
255 i=0x48;
256 else
257 i=0x49;
258 }
259
260 }
261 if ((i == 0x80) || (i == 0x8f)) {
262#ifdef ED_DBGP
263 printk(KERN_DEBUG "Device reselect\n");
264#endif
265 lun = 0;
6a3cebb6
OZ
266 if (cmdp == 0x44 || i == 0x80)
267 lun = atp_readb_io(dev, c, 0x1d) & 0x07;
268 else {
1da177e4
LT
269 if ((dev->last_cmd[c] & 0xf0) != 0x40) {
270 dev->last_cmd[c] = 0xff;
271 }
272 if (cmdp == 0x41) {
273#ifdef ED_DBGP
274 printk("cmdp = 0x41\n");
275#endif
1da177e4 276 adrcnt = 0;
6a3cebb6
OZ
277 ((unsigned char *) &adrcnt)[2] = atp_readb_io(dev, c, 0x12);
278 ((unsigned char *) &adrcnt)[1] = atp_readb_io(dev, c, 0x13);
279 ((unsigned char *) &adrcnt)[0] = atp_readb_io(dev, c, 0x14);
1da177e4
LT
280 k = dev->id[c][target_id].last_len;
281 k -= adrcnt;
282 dev->id[c][target_id].tran_len = k;
283 dev->id[c][target_id].last_len = adrcnt;
6a3cebb6 284 atp_writeb_io(dev, c, 0x18, 0x08);
1da177e4 285 dev->in_int[c] = 0;
78614ecd 286 return IRQ_HANDLED;
1da177e4
LT
287 } else {
288#ifdef ED_DBGP
289 printk("cmdp != 0x41\n");
290#endif
6a3cebb6 291 atp_writeb_io(dev, c, 0x10, 0x46);
1da177e4 292 dev->id[c][target_id].dirct = 0x00;
6a3cebb6
OZ
293 atp_writeb_io(dev, c, 0x12, 0x00);
294 atp_writeb_io(dev, c, 0x13, 0x00);
295 atp_writeb_io(dev, c, 0x14, 0x00);
296 atp_writeb_io(dev, c, 0x18, 0x08);
1da177e4 297 dev->in_int[c] = 0;
78614ecd 298 return IRQ_HANDLED;
1da177e4
LT
299 }
300 }
301 if (dev->last_cmd[c] != 0xff) {
302 dev->last_cmd[c] |= 0x40;
303 }
304 if (dev->dev_id == ATP885_DEVID) {
6a3cebb6
OZ
305 j = atp_readb_base(dev, 0x29) & 0xfe;
306 atp_writeb_base(dev, 0x29, j);
3a38e53e 307 } else
6a3cebb6 308 atp_writeb_io(dev, c, 0x10, 0x45);
3a38e53e 309
6a3cebb6 310 target_id = atp_readb_io(dev, c, 0x16);
1da177e4
LT
311 /*
312 * Remap wide identifiers
313 */
314 if ((target_id & 0x10) != 0) {
315 target_id = (target_id & 0x07) | 0x08;
316 } else {
317 target_id &= 0x07;
318 }
3a38e53e 319 if (dev->dev_id == ATP885_DEVID)
6a3cebb6 320 atp_writeb_io(dev, c, 0x10, 0x45);
1da177e4
LT
321 workreq = dev->id[c][target_id].curr_req;
322#ifdef ED_DBGP
017560fc
JG
323 scmd_printk(KERN_DEBUG, workreq, "CDB");
324 for (l = 0; l < workreq->cmd_len; l++)
1da177e4 325 printk(KERN_DEBUG " %x",workreq->cmnd[l]);
017560fc 326 printk("\n");
1da177e4
LT
327#endif
328
6a3cebb6
OZ
329 atp_writeb_io(dev, c, 0x0f, lun);
330 atp_writeb_io(dev, c, 0x11, dev->id[c][target_id].devsp);
1da177e4
LT
331 adrcnt = dev->id[c][target_id].tran_len;
332 k = dev->id[c][target_id].last_len;
333
6a3cebb6
OZ
334 atp_writeb_io(dev, c, 0x12, ((unsigned char *) &k)[2]);
335 atp_writeb_io(dev, c, 0x13, ((unsigned char *) &k)[1]);
336 atp_writeb_io(dev, c, 0x14, ((unsigned char *) &k)[0]);
1da177e4 337#ifdef ED_DBGP
6a3cebb6 338 printk("k %x, k[0] 0x%x k[1] 0x%x k[2] 0x%x\n", k, atp_readb_io(dev, c, 0x14), atp_readb_io(dev, c, 0x13), atp_readb_io(dev, c, 0x12));
1da177e4
LT
339#endif
340 /* Remap wide */
341 j = target_id;
342 if (target_id > 7) {
343 j = (j & 0x07) | 0x40;
344 }
345 /* Add direction */
346 j |= dev->id[c][target_id].dirct;
6a3cebb6
OZ
347 atp_writeb_io(dev, c, 0x15, j);
348 atp_writeb_io(dev, c, 0x16, 0x80);
1da177e4
LT
349
350 /* enable 32 bit fifo transfer */
351 if (dev->dev_id == ATP885_DEVID) {
6a3cebb6 352 i = atp_readb_pci(dev, c, 1) & 0xf3;
1da177e4
LT
353 //j=workreq->cmnd[0];
354 if ((workreq->cmnd[0] == 0x08) || (workreq->cmnd[0] == 0x28) || (workreq->cmnd[0] == 0x0a) || (workreq->cmnd[0] == 0x2a)) {
355 i |= 0x0c;
356 }
6a3cebb6 357 atp_writeb_pci(dev, c, 1, i);
1da177e4
LT
358 } else if ((dev->dev_id == ATP880_DEVID1) ||
359 (dev->dev_id == ATP880_DEVID2) ) {
6a3cebb6
OZ
360 if ((workreq->cmnd[0] == 0x08) || (workreq->cmnd[0] == 0x28) || (workreq->cmnd[0] == 0x0a) || (workreq->cmnd[0] == 0x2a))
361 atp_writeb_base(dev, 0x3b, (atp_readb_base(dev, 0x3b) & 0x3f) | 0xc0);
362 else
363 atp_writeb_base(dev, 0x3b, atp_readb_base(dev, 0x3b) & 0x3f);
1da177e4 364 } else {
6a3cebb6 365 if ((workreq->cmnd[0] == 0x08) || (workreq->cmnd[0] == 0x28) || (workreq->cmnd[0] == 0x0a) || (workreq->cmnd[0] == 0x2a))
c751d9f1 366 atp_writeb_base(dev, 0x3a, (atp_readb_base(dev, 0x3a) & 0xf3) | 0x08);
6a3cebb6 367 else
c751d9f1 368 atp_writeb_base(dev, 0x3a, atp_readb_base(dev, 0x3a) & 0xf3);
1da177e4 369 }
1da177e4
LT
370 j = 0;
371 id = 1;
372 id = id << target_id;
373 /*
374 * Is this a wide device
375 */
376 if ((id & dev->wide_id[c]) != 0) {
377 j |= 0x01;
378 }
6a3cebb6
OZ
379 atp_writeb_io(dev, c, 0x1b, j);
380 while ((atp_readb_io(dev, c, 0x1b) & 0x01) != j)
381 atp_writeb_io(dev, c, 0x1b, j);
1da177e4 382 if (dev->id[c][target_id].last_len == 0) {
6a3cebb6 383 atp_writeb_io(dev, c, 0x18, 0x08);
1da177e4
LT
384 dev->in_int[c] = 0;
385#ifdef ED_DBGP
386 printk("dev->id[c][target_id].last_len = 0\n");
387#endif
78614ecd 388 return IRQ_HANDLED;
1da177e4
LT
389 }
390#ifdef ED_DBGP
391 printk("target_id = %d adrcnt = %d\n",target_id,adrcnt);
392#endif
393 prd = dev->id[c][target_id].prd_pos;
394 while (adrcnt != 0) {
395 id = ((unsigned short int *)prd)[2];
396 if (id == 0) {
397 k = 0x10000;
398 } else {
399 k = id;
400 }
401 if (k > adrcnt) {
402 ((unsigned short int *)prd)[2] = (unsigned short int)
403 (k - adrcnt);
404 ((unsigned long *)prd)[0] += adrcnt;
405 adrcnt = 0;
406 dev->id[c][target_id].prd_pos = prd;
407 } else {
408 adrcnt -= k;
409 dev->id[c][target_id].prdaddr += 0x08;
410 prd += 0x08;
411 if (adrcnt == 0) {
412 dev->id[c][target_id].prd_pos = prd;
413 }
414 }
415 }
6a3cebb6 416 atp_writel_pci(dev, c, 0x04, dev->id[c][target_id].prdaddr);
1da177e4
LT
417#ifdef ED_DBGP
418 printk("dev->id[%d][%d].prdaddr 0x%8x\n", c, target_id, dev->id[c][target_id].prdaddr);
419#endif
bc0fe4c9 420 if (dev->dev_id != ATP885_DEVID) {
6a3cebb6
OZ
421 atp_writeb_pci(dev, c, 2, 0x06);
422 atp_writeb_pci(dev, c, 2, 0x00);
1da177e4 423 }
1da177e4
LT
424 /*
425 * Check transfer direction
426 */
427 if (dev->id[c][target_id].dirct != 0) {
6a3cebb6
OZ
428 atp_writeb_io(dev, c, 0x18, 0x08);
429 atp_writeb_pci(dev, c, 0, 0x01);
1da177e4
LT
430 dev->in_int[c] = 0;
431#ifdef ED_DBGP
432 printk("status 0x80 return dirct != 0\n");
433#endif
78614ecd 434 return IRQ_HANDLED;
1da177e4 435 }
6a3cebb6
OZ
436 atp_writeb_io(dev, c, 0x18, 0x08);
437 atp_writeb_pci(dev, c, 0, 0x09);
1da177e4
LT
438 dev->in_int[c] = 0;
439#ifdef ED_DBGP
440 printk("status 0x80 return dirct = 0\n");
441#endif
78614ecd 442 return IRQ_HANDLED;
1da177e4
LT
443 }
444
445 /*
446 * Current scsi request on this target
447 */
448
449 workreq = dev->id[c][target_id].curr_req;
450
78614ecd 451 if (i == 0x42 || i == 0x16) {
1da177e4
LT
452 if ((dev->last_cmd[c] & 0xf0) != 0x40) {
453 dev->last_cmd[c] = 0xff;
454 }
78614ecd 455 if (i == 0x16) {
6a3cebb6 456 workreq->result = atp_readb_io(dev, c, 0x0f);
78614ecd
OZ
457 if (((dev->r1f[c][target_id] & 0x10) != 0)&&(dev->dev_id==ATP885_DEVID)) {
458 printk(KERN_WARNING "AEC67162 CRC ERROR !\n");
459 workreq->result = 0x02;
460 }
461 } else
462 workreq->result = 0x02;
463
1da177e4 464 if (dev->dev_id == ATP885_DEVID) {
6a3cebb6
OZ
465 j = atp_readb_base(dev, 0x29) | 0x01;
466 atp_writeb_base(dev, 0x29, j);
1da177e4
LT
467 }
468 /*
469 * Complete the command
470 */
fe7ed98f
BH
471 scsi_dma_unmap(workreq);
472
1da177e4
LT
473 spin_lock_irqsave(dev->host->host_lock, flags);
474 (*workreq->scsi_done) (workreq);
475#ifdef ED_DBGP
476 printk("workreq->scsi_done\n");
477#endif
478 /*
479 * Clear it off the queue
480 */
481 dev->id[c][target_id].curr_req = NULL;
482 dev->working[c]--;
483 spin_unlock_irqrestore(dev->host->host_lock, flags);
484 /*
485 * Take it back wide
486 */
487 if (dev->wide_id[c] != 0) {
6a3cebb6
OZ
488 atp_writeb_io(dev, c, 0x1b, 0x01);
489 while ((atp_readb_io(dev, c, 0x1b) & 0x01) != 0x01)
490 atp_writeb_io(dev, c, 0x1b, 0x01);
1da177e4
LT
491 }
492 /*
493 * If there is stuff to send and nothing going then send it
494 */
495 spin_lock_irqsave(dev->host->host_lock, flags);
496 if (((dev->last_cmd[c] != 0xff) || (dev->quhd[c] != dev->quend[c])) &&
497 (dev->in_snd[c] == 0)) {
498#ifdef ED_DBGP
499 printk("Call sent_s870(scsi_done)\n");
500#endif
501 send_s870(dev,c);
502 }
503 spin_unlock_irqrestore(dev->host->host_lock, flags);
504 dev->in_int[c] = 0;
78614ecd 505 return IRQ_HANDLED;
1da177e4
LT
506 }
507 if ((dev->last_cmd[c] & 0xf0) != 0x40) {
508 dev->last_cmd[c] = 0xff;
509 }
510 if (i == 0x4f) {
511 i = 0x89;
512 }
513 i &= 0x0f;
514 if (i == 0x09) {
6a3cebb6
OZ
515 atp_writel_pci(dev, c, 4, dev->id[c][target_id].prdaddr);
516 atp_writeb_pci(dev, c, 2, 0x06);
517 atp_writeb_pci(dev, c, 2, 0x00);
518 atp_writeb_io(dev, c, 0x10, 0x41);
1da177e4 519 if (dev->dev_id == ATP885_DEVID) {
1da177e4 520 k = dev->id[c][target_id].last_len;
6a3cebb6
OZ
521 atp_writeb_io(dev, c, 0x12, ((unsigned char *) (&k))[2]);
522 atp_writeb_io(dev, c, 0x13, ((unsigned char *) (&k))[1]);
523 atp_writeb_io(dev, c, 0x14, ((unsigned char *) (&k))[0]);
1da177e4 524 dev->id[c][target_id].dirct = 0x00;
1da177e4
LT
525 } else {
526 dev->id[c][target_id].dirct = 0x00;
1da177e4 527 }
6a3cebb6
OZ
528 atp_writeb_io(dev, c, 0x18, 0x08);
529 atp_writeb_pci(dev, c, 0, 0x09);
1da177e4 530 dev->in_int[c] = 0;
78614ecd 531 return IRQ_HANDLED;
1da177e4
LT
532 }
533 if (i == 0x08) {
6a3cebb6
OZ
534 atp_writel_pci(dev, c, 4, dev->id[c][target_id].prdaddr);
535 atp_writeb_pci(dev, c, 2, 0x06);
536 atp_writeb_pci(dev, c, 2, 0x00);
537 atp_writeb_io(dev, c, 0x10, 0x41);
1da177e4 538 if (dev->dev_id == ATP885_DEVID) {
1da177e4 539 k = dev->id[c][target_id].last_len;
6a3cebb6
OZ
540 atp_writeb_io(dev, c, 0x12, ((unsigned char *) (&k))[2]);
541 atp_writeb_io(dev, c, 0x13, ((unsigned char *) (&k))[1]);
542 atp_writeb_io(dev, c, 0x14, ((unsigned char *) (&k))[0]);
1da177e4 543 }
6a3cebb6 544 atp_writeb_io(dev, c, 0x15, atp_readb_io(dev, c, 0x15) | 0x20);
1da177e4 545 dev->id[c][target_id].dirct = 0x20;
6a3cebb6
OZ
546 atp_writeb_io(dev, c, 0x18, 0x08);
547 atp_writeb_pci(dev, c, 0, 0x01);
1da177e4 548 dev->in_int[c] = 0;
78614ecd 549 return IRQ_HANDLED;
1da177e4 550 }
6a3cebb6
OZ
551 if (i == 0x0a)
552 atp_writeb_io(dev, c, 0x10, 0x30);
553 else
554 atp_writeb_io(dev, c, 0x10, 0x46);
1da177e4 555 dev->id[c][target_id].dirct = 0x00;
6a3cebb6
OZ
556 atp_writeb_io(dev, c, 0x12, 0x00);
557 atp_writeb_io(dev, c, 0x13, 0x00);
558 atp_writeb_io(dev, c, 0x14, 0x00);
559 atp_writeb_io(dev, c, 0x18, 0x08);
1da177e4 560 }
78614ecd
OZ
561 dev->in_int[c] = 0;
562
1da177e4
LT
563 return IRQ_HANDLED;
564}
565/**
566 * atp870u_queuecommand - Queue SCSI command
567 * @req_p: request block
568 * @done: completion function
569 *
570 * Queue a command to the ATP queue. Called with the host lock held.
571 */
f281233d 572static int atp870u_queuecommand_lck(struct scsi_cmnd *req_p,
1da177e4
LT
573 void (*done) (struct scsi_cmnd *))
574{
575 unsigned char c;
3b836464 576 unsigned int m;
1da177e4
LT
577 struct atp_unit *dev;
578 struct Scsi_Host *host;
579
422c0d61 580 c = scmd_channel(req_p);
1da177e4 581 req_p->sense_buffer[0]=0;
fe7ed98f 582 scsi_set_resid(req_p, 0);
422c0d61 583 if (scmd_channel(req_p) > 1) {
1da177e4
LT
584 req_p->result = 0x00040000;
585 done(req_p);
586#ifdef ED_DBGP
587 printk("atp870u_queuecommand : req_p->device->channel > 1\n");
588#endif
589 return 0;
590 }
591
592 host = req_p->device->host;
593 dev = (struct atp_unit *)&host->hostdata;
594
595
596
597 m = 1;
422c0d61 598 m = m << scmd_id(req_p);
1da177e4
LT
599
600 /*
601 * Fake a timeout for missing targets
602 */
603
604 if ((m & dev->active_id[c]) == 0) {
605 req_p->result = 0x00040000;
606 done(req_p);
607 return 0;
608 }
609
610 if (done) {
611 req_p->scsi_done = done;
612 } else {
613#ifdef ED_DBGP
614 printk( "atp870u_queuecommand: done can't be NULL\n");
615#endif
616 req_p->result = 0;
617 done(req_p);
618 return 0;
619 }
620
621 /*
622 * Count new command
623 */
624 dev->quend[c]++;
625 if (dev->quend[c] >= qcnt) {
626 dev->quend[c] = 0;
627 }
628
629 /*
630 * Check queue state
631 */
632 if (dev->quhd[c] == dev->quend[c]) {
633 if (dev->quend[c] == 0) {
634 dev->quend[c] = qcnt;
635 }
636#ifdef ED_DBGP
637 printk("atp870u_queuecommand : dev->quhd[c] == dev->quend[c]\n");
638#endif
639 dev->quend[c]--;
640 req_p->result = 0x00020000;
641 done(req_p);
642 return 0;
643 }
644 dev->quereq[c][dev->quend[c]] = req_p;
1da177e4 645#ifdef ED_DBGP
6a3cebb6 646 printk("dev->ioport[c] = %x atp_readb_io(dev, c, 0x1c) = %x dev->in_int[%d] = %d dev->in_snd[%d] = %d\n",dev->ioport[c],atp_readb_io(dev, c, 0x1c),c,dev->in_int[c],c,dev->in_snd[c]);
1da177e4 647#endif
6a3cebb6 648 if ((atp_readb_io(dev, c, 0x1c) == 0) && (dev->in_int[c] == 0) && (dev->in_snd[c] == 0)) {
1da177e4
LT
649#ifdef ED_DBGP
650 printk("Call sent_s870(atp870u_queuecommand)\n");
651#endif
652 send_s870(dev,c);
653 }
654#ifdef ED_DBGP
655 printk("atp870u_queuecommand : exit\n");
656#endif
657 return 0;
658}
659
f281233d
JG
660static DEF_SCSI_QCMD(atp870u_queuecommand)
661
1da177e4
LT
662/**
663 * send_s870 - send a command to the controller
664 * @host: host
665 *
666 * On entry there is work queued to be done. We move some of that work to the
667 * controller itself.
668 *
669 * Caller holds the host lock.
670 */
671static void send_s870(struct atp_unit *dev,unsigned char c)
672{
468b8968 673 struct scsi_cmnd *workreq = NULL;
1da177e4
LT
674 unsigned int i;//,k;
675 unsigned char j, target_id;
676 unsigned char *prd;
c2bab403 677 unsigned short int w;
1da177e4 678 unsigned long l, bttl = 0;
1da177e4
LT
679 unsigned long sg_count;
680
681 if (dev->in_snd[c] != 0) {
682#ifdef ED_DBGP
683 printk("cmnd in_snd\n");
684#endif
685 return;
686 }
687#ifdef ED_DBGP
688 printk("Sent_s870 enter\n");
689#endif
690 dev->in_snd[c] = 1;
691 if ((dev->last_cmd[c] != 0xff) && ((dev->last_cmd[c] & 0x40) != 0)) {
692 dev->last_cmd[c] &= 0x0f;
693 workreq = dev->id[c][dev->last_cmd[c]].curr_req;
468b8968
OZ
694 if (!workreq) {
695 dev->last_cmd[c] = 0xff;
696 if (dev->quhd[c] == dev->quend[c]) {
697 dev->in_snd[c] = 0;
698 return;
699 }
1da177e4
LT
700 }
701 }
468b8968
OZ
702 if (!workreq) {
703 if ((dev->last_cmd[c] != 0xff) && (dev->working[c] != 0)) {
704 dev->in_snd[c] = 0;
705 return;
706 }
707 dev->working[c]++;
708 j = dev->quhd[c];
709 dev->quhd[c]++;
710 if (dev->quhd[c] >= qcnt)
711 dev->quhd[c] = 0;
712 workreq = dev->quereq[c][dev->quhd[c]];
713 if (dev->id[c][scmd_id(workreq)].curr_req != NULL) {
714 dev->quhd[c] = j;
715 dev->working[c]--;
716 dev->in_snd[c] = 0;
717 return;
718 }
422c0d61
JG
719 dev->id[c][scmd_id(workreq)].curr_req = workreq;
720 dev->last_cmd[c] = scmd_id(workreq);
1da177e4 721 }
6a3cebb6 722 if ((atp_readb_io(dev, c, 0x1f) & 0xb0) != 0 || atp_readb_io(dev, c, 0x1c) != 0) {
1da177e4 723#ifdef ED_DBGP
468b8968 724 printk("Abort to Send\n");
1da177e4 725#endif
468b8968
OZ
726 dev->last_cmd[c] |= 0x40;
727 dev->in_snd[c] = 0;
728 return;
729 }
1da177e4
LT
730#ifdef ED_DBGP
731 printk("OK to Send\n");
422c0d61 732 scmd_printk(KERN_DEBUG, workreq, "CDB");
1da177e4
LT
733 for(i=0;i<workreq->cmd_len;i++) {
734 printk(" %x",workreq->cmnd[i]);
735 }
422c0d61 736 printk("\n");
1da177e4 737#endif
fe7ed98f
BH
738 l = scsi_bufflen(workreq);
739
1da177e4 740 if (dev->dev_id == ATP885_DEVID) {
6a3cebb6
OZ
741 j = atp_readb_base(dev, 0x29) & 0xfe;
742 atp_writeb_base(dev, 0x29, j);
422c0d61 743 dev->r1f[c][scmd_id(workreq)] = 0;
1da177e4
LT
744 }
745
746 if (workreq->cmnd[0] == READ_CAPACITY) {
fe7ed98f
BH
747 if (l > 8)
748 l = 8;
1da177e4
LT
749 }
750 if (workreq->cmnd[0] == 0x00) {
fe7ed98f 751 l = 0;
1da177e4
LT
752 }
753
1da177e4 754 j = 0;
422c0d61 755 target_id = scmd_id(workreq);
1da177e4
LT
756
757 /*
758 * Wide ?
759 */
760 w = 1;
761 w = w << target_id;
762 if ((w & dev->wide_id[c]) != 0) {
763 j |= 0x01;
764 }
6a3cebb6
OZ
765 atp_writeb_io(dev, c, 0x1b, j);
766 while ((atp_readb_io(dev, c, 0x1b) & 0x01) != j) {
767 atp_writeb_pci(dev, c, 0x1b, j);
1da177e4
LT
768#ifdef ED_DBGP
769 printk("send_s870 while loop 1\n");
770#endif
771 }
772 /*
773 * Write the command
774 */
775
6a3cebb6
OZ
776 atp_writeb_io(dev, c, 0x00, workreq->cmd_len);
777 atp_writeb_io(dev, c, 0x01, 0x2c);
778 if (dev->dev_id == ATP885_DEVID)
779 atp_writeb_io(dev, c, 0x02, 0x7f);
780 else
781 atp_writeb_io(dev, c, 0x02, 0xcf);
782 for (i = 0; i < workreq->cmd_len; i++)
783 atp_writeb_io(dev, c, 0x03 + i, workreq->cmnd[i]);
784 atp_writeb_io(dev, c, 0x0f, workreq->device->lun);
1da177e4
LT
785 /*
786 * Write the target
787 */
6a3cebb6 788 atp_writeb_io(dev, c, 0x11, dev->id[c][target_id].devsp);
1da177e4
LT
789#ifdef ED_DBGP
790 printk("dev->id[%d][%d].devsp = %2x\n",c,target_id,dev->id[c][target_id].devsp);
791#endif
fe7ed98f
BH
792
793 sg_count = scsi_dma_map(workreq);
1da177e4
LT
794 /*
795 * Write transfer size
796 */
6a3cebb6
OZ
797 atp_writeb_io(dev, c, 0x12, ((unsigned char *) (&l))[2]);
798 atp_writeb_io(dev, c, 0x13, ((unsigned char *) (&l))[1]);
799 atp_writeb_io(dev, c, 0x14, ((unsigned char *) (&l))[0]);
1da177e4
LT
800 j = target_id;
801 dev->id[c][j].last_len = l;
802 dev->id[c][j].tran_len = 0;
803#ifdef ED_DBGP
804 printk("dev->id[%2d][%2d].last_len = %d\n",c,j,dev->id[c][j].last_len);
805#endif
806 /*
807 * Flip the wide bits
808 */
809 if ((j & 0x08) != 0) {
810 j = (j & 0x07) | 0x40;
811 }
812 /*
813 * Check transfer direction
814 */
6a3cebb6
OZ
815 if (workreq->sc_data_direction == DMA_TO_DEVICE)
816 atp_writeb_io(dev, c, 0x15, j | 0x20);
817 else
818 atp_writeb_io(dev, c, 0x15, j);
819 atp_writeb_io(dev, c, 0x16, atp_readb_io(dev, c, 0x16) | 0x80);
820 atp_writeb_io(dev, c, 0x16, 0x80);
1da177e4
LT
821 dev->id[c][target_id].dirct = 0;
822 if (l == 0) {
6a3cebb6 823 if (atp_readb_io(dev, c, 0x1c) == 0) {
1da177e4
LT
824#ifdef ED_DBGP
825 printk("change SCSI_CMD_REG 0x08\n");
826#endif
6a3cebb6
OZ
827 atp_writeb_io(dev, c, 0x18, 0x08);
828 } else
1da177e4 829 dev->last_cmd[c] |= 0x40;
1da177e4
LT
830 dev->in_snd[c] = 0;
831 return;
832 }
1da177e4
LT
833 prd = dev->id[c][target_id].prd_table;
834 dev->id[c][target_id].prd_pos = prd;
835
836 /*
837 * Now write the request list. Either as scatter/gather or as
838 * a linear chain.
839 */
840
fe7ed98f
BH
841 if (l) {
842 struct scatterlist *sgpnt;
1da177e4 843 i = 0;
fe7ed98f
BH
844 scsi_for_each_sg(workreq, sgpnt, sg_count, j) {
845 bttl = sg_dma_address(sgpnt);
846 l=sg_dma_len(sgpnt);
1da177e4 847#ifdef ED_DBGP
fe7ed98f 848 printk("1. bttl %x, l %x\n",bttl, l);
1da177e4 849#endif
fe7ed98f 850 while (l > 0x10000) {
1da177e4
LT
851 (((u16 *) (prd))[i + 3]) = 0x0000;
852 (((u16 *) (prd))[i + 2]) = 0x0000;
853 (((u32 *) (prd))[i >> 1]) = cpu_to_le32(bttl);
854 l -= 0x10000;
855 bttl += 0x10000;
856 i += 0x04;
857 }
858 (((u32 *) (prd))[i >> 1]) = cpu_to_le32(bttl);
859 (((u16 *) (prd))[i + 2]) = cpu_to_le16(l);
860 (((u16 *) (prd))[i + 3]) = 0;
861 i += 0x04;
862 }
863 (((u16 *) (prd))[i - 1]) = cpu_to_le16(0x8000);
864#ifdef ED_DBGP
865 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]));
866 printk("2. bttl %x, l %x\n",bttl, l);
867#endif
1da177e4 868 }
1da177e4 869#ifdef ED_DBGP
c2bab403 870 printk("send_s870: prdaddr_2 0x%8x target_id %d\n", dev->id[c][target_id].prdaddr,target_id);
1da177e4 871#endif
b5683557 872 dev->id[c][target_id].prdaddr = dev->id[c][target_id].prd_bus;
6a3cebb6
OZ
873 atp_writel_pci(dev, c, 4, dev->id[c][target_id].prdaddr);
874 atp_writeb_pci(dev, c, 2, 0x06);
875 atp_writeb_pci(dev, c, 2, 0x00);
1da177e4 876 if (dev->dev_id == ATP885_DEVID) {
6a3cebb6 877 j = atp_readb_pci(dev, c, 1) & 0xf3;
1da177e4
LT
878 if ((workreq->cmnd[0] == 0x08) || (workreq->cmnd[0] == 0x28) ||
879 (workreq->cmnd[0] == 0x0a) || (workreq->cmnd[0] == 0x2a)) {
880 j |= 0x0c;
881 }
6a3cebb6 882 atp_writeb_pci(dev, c, 1, j);
1da177e4
LT
883 } else if ((dev->dev_id == ATP880_DEVID1) ||
884 (dev->dev_id == ATP880_DEVID2)) {
6a3cebb6
OZ
885 if ((workreq->cmnd[0] == 0x08) || (workreq->cmnd[0] == 0x28) || (workreq->cmnd[0] == 0x0a) || (workreq->cmnd[0] == 0x2a))
886 atp_writeb_base(dev, 0x3b, (atp_readb_base(dev, 0x3b) & 0x3f) | 0xc0);
887 else
888 atp_writeb_base(dev, 0x3b, atp_readb_base(dev, 0x3b) & 0x3f);
1da177e4 889 } else {
6a3cebb6 890 if ((workreq->cmnd[0] == 0x08) || (workreq->cmnd[0] == 0x28) || (workreq->cmnd[0] == 0x0a) || (workreq->cmnd[0] == 0x2a))
c751d9f1 891 atp_writeb_base(dev, 0x3a, (atp_readb_base(dev, 0x3a) & 0xf3) | 0x08);
6a3cebb6 892 else
c751d9f1 893 atp_writeb_base(dev, 0x3a, atp_readb_base(dev, 0x3a) & 0xf3);
1da177e4 894 }
1da177e4
LT
895
896 if(workreq->sc_data_direction == DMA_TO_DEVICE) {
897 dev->id[c][target_id].dirct = 0x20;
6a3cebb6
OZ
898 if (atp_readb_io(dev, c, 0x1c) == 0) {
899 atp_writeb_io(dev, c, 0x18, 0x08);
900 atp_writeb_pci(dev, c, 0, 0x01);
1da177e4
LT
901#ifdef ED_DBGP
902 printk( "start DMA(to target)\n");
903#endif
904 } else {
905 dev->last_cmd[c] |= 0x40;
906 }
907 dev->in_snd[c] = 0;
908 return;
909 }
6a3cebb6
OZ
910 if (atp_readb_io(dev, c, 0x1c) == 0) {
911 atp_writeb_io(dev, c, 0x18, 0x08);
912 atp_writeb_pci(dev, c, 0, 0x09);
1da177e4
LT
913#ifdef ED_DBGP
914 printk( "start DMA(to host)\n");
915#endif
916 } else {
917 dev->last_cmd[c] |= 0x40;
918 }
919 dev->in_snd[c] = 0;
920 return;
921
922}
923
924static unsigned char fun_scam(struct atp_unit *dev, unsigned short int *val)
925{
1da177e4
LT
926 unsigned short int i, k;
927 unsigned char j;
928
6a3cebb6 929 atp_writew_io(dev, 0, 0x1c, *val);
1da177e4 930 for (i = 0; i < 10; i++) { /* stable >= bus settle delay(400 ns) */
6a3cebb6 931 k = atp_readw_io(dev, 0, 0x1c);
1da177e4 932 j = (unsigned char) (k >> 8);
832e9ac6
OZ
933 if ((k & 0x8000) != 0) /* DB7 all release? */
934 i = 0;
1da177e4
LT
935 }
936 *val |= 0x4000; /* assert DB6 */
6a3cebb6 937 atp_writew_io(dev, 0, 0x1c, *val);
1da177e4 938 *val &= 0xdfff; /* assert DB5 */
6a3cebb6 939 atp_writew_io(dev, 0, 0x1c, *val);
1da177e4 940 for (i = 0; i < 10; i++) { /* stable >= bus settle delay(400 ns) */
6a3cebb6 941 if ((atp_readw_io(dev, 0, 0x1c) & 0x2000) != 0) /* DB5 all release? */
832e9ac6 942 i = 0;
1da177e4
LT
943 }
944 *val |= 0x8000; /* no DB4-0, assert DB7 */
945 *val &= 0xe0ff;
6a3cebb6 946 atp_writew_io(dev, 0, 0x1c, *val);
1da177e4 947 *val &= 0xbfff; /* release DB6 */
6a3cebb6 948 atp_writew_io(dev, 0, 0x1c, *val);
1da177e4 949 for (i = 0; i < 10; i++) { /* stable >= bus settle delay(400 ns) */
6a3cebb6 950 if ((atp_readw_io(dev, 0, 0x1c) & 0x4000) != 0) /* DB6 all release? */
832e9ac6 951 i = 0;
1da177e4
LT
952 }
953
954 return j;
955}
956
957static void tscam(struct Scsi_Host *host)
958{
959
1da177e4
LT
960 unsigned char i, j, k;
961 unsigned long n;
962 unsigned short int m, assignid_map, val;
963 unsigned char mbuf[33], quintet[2];
964 struct atp_unit *dev = (struct atp_unit *)&host->hostdata;
965 static unsigned char g2q_tab[8] = {
966 0x38, 0x31, 0x32, 0x2b, 0x34, 0x2d, 0x2e, 0x27
967 };
968
969/* I can't believe we need this before we've even done anything. Remove it
970 * and see if anyone bitches.
971 for (i = 0; i < 0x10; i++) {
972 udelay(0xffff);
973 }
974 */
975
6a3cebb6
OZ
976 atp_writeb_io(dev, 0, 1, 0x08);
977 atp_writeb_io(dev, 0, 2, 0x7f);
978 atp_writeb_io(dev, 0, 0x11, 0x20);
1da177e4
LT
979
980 if ((dev->scam_on & 0x40) == 0) {
981 return;
982 }
983 m = 1;
984 m <<= dev->host_id[0];
985 j = 16;
986 if (dev->chip_ver < 4) {
987 m |= 0xff00;
988 j = 8;
989 }
990 assignid_map = m;
6a3cebb6
OZ
991 atp_writeb_io(dev, 0, 0x02, 0x02); /* 2*2=4ms,3EH 2/32*3E=3.9ms */
992 atp_writeb_io(dev, 0, 0x03, 0);
993 atp_writeb_io(dev, 0, 0x04, 0);
994 atp_writeb_io(dev, 0, 0x05, 0);
995 atp_writeb_io(dev, 0, 0x06, 0);
996 atp_writeb_io(dev, 0, 0x07, 0);
997 atp_writeb_io(dev, 0, 0x08, 0);
1da177e4
LT
998
999 for (i = 0; i < j; i++) {
1000 m = 1;
1001 m = m << i;
1002 if ((m & assignid_map) != 0) {
1003 continue;
1004 }
6a3cebb6
OZ
1005 atp_writeb_io(dev, 0, 0x0f, 0);
1006 atp_writeb_io(dev, 0, 0x12, 0);
1007 atp_writeb_io(dev, 0, 0x13, 0);
1008 atp_writeb_io(dev, 0, 0x14, 0);
1da177e4
LT
1009 if (i > 7) {
1010 k = (i & 0x07) | 0x40;
1011 } else {
1012 k = i;
1013 }
6a3cebb6
OZ
1014 atp_writeb_io(dev, 0, 0x15, k);
1015 if (dev->chip_ver == 4)
1016 atp_writeb_io(dev, 0, 0x1b, 0x01);
1017 else
1018 atp_writeb_io(dev, 0, 0x1b, 0x00);
58c4d046 1019 do {
6a3cebb6 1020 atp_writeb_io(dev, 0, 0x18, 0x09);
1da177e4 1021
6a3cebb6 1022 while ((atp_readb_io(dev, 0, 0x1f) & 0x80) == 0x00)
58c4d046 1023 cpu_relax();
6a3cebb6 1024 k = atp_readb_io(dev, 0, 0x17);
58c4d046
OZ
1025 if ((k == 0x85) || (k == 0x42))
1026 break;
1027 if (k != 0x16)
6a3cebb6 1028 atp_writeb_io(dev, 0, 0x10, 0x41);
58c4d046
OZ
1029 } while (k != 0x16);
1030 if ((k == 0x85) || (k == 0x42))
1031 continue;
1da177e4
LT
1032 assignid_map |= m;
1033
1034 }
6a3cebb6
OZ
1035 atp_writeb_io(dev, 0, 0x02, 0x7f);
1036 atp_writeb_io(dev, 0, 0x1b, 0x02);
1da177e4 1037
2bbbac45 1038 udelay(2);
1da177e4
LT
1039
1040 val = 0x0080; /* bsy */
6a3cebb6 1041 atp_writew_io(dev, 0, 0x1c, val);
1da177e4 1042 val |= 0x0040; /* sel */
6a3cebb6 1043 atp_writew_io(dev, 0, 0x1c, val);
1da177e4 1044 val |= 0x0004; /* msg */
6a3cebb6 1045 atp_writew_io(dev, 0, 0x1c, val);
2bbbac45 1046 udelay(2); /* 2 deskew delay(45ns*2=90ns) */
1da177e4 1047 val &= 0x007f; /* no bsy */
6a3cebb6 1048 atp_writew_io(dev, 0, 0x1c, val);
1da177e4
LT
1049 mdelay(128);
1050 val &= 0x00fb; /* after 1ms no msg */
6a3cebb6
OZ
1051 atp_writew_io(dev, 0, 0x1c, val);
1052 while ((atp_readb_io(dev, 0, 0x1c) & 0x04) != 0)
58c4d046 1053 ;
2bbbac45 1054 udelay(2);
1da177e4 1055 udelay(100);
c7fcc089 1056 for (n = 0; n < 0x30000; n++)
6a3cebb6 1057 if ((atp_readb_io(dev, 0, 0x1c) & 0x80) != 0) /* bsy ? */
c7fcc089
OZ
1058 break;
1059 if (n < 0x30000)
1060 for (n = 0; n < 0x30000; n++)
6a3cebb6 1061 if ((atp_readb_io(dev, 0, 0x1c) & 0x81) == 0x0081) {
2bbbac45 1062 udelay(2);
c7fcc089 1063 val |= 0x8003; /* io,cd,db7 */
6a3cebb6 1064 atp_writew_io(dev, 0, 0x1c, val);
2bbbac45 1065 udelay(2);
c7fcc089 1066 val &= 0x00bf; /* no sel */
6a3cebb6 1067 atp_writew_io(dev, 0, 0x1c, val);
2bbbac45 1068 udelay(2);
c7fcc089
OZ
1069 break;
1070 }
1071 while (1) {
0f6d93aa
MM
1072 /*
1073 * The funny division into multiple delays is to accomodate
1074 * arches like ARM where udelay() multiplies its argument by
1075 * a large number to initialize a loop counter. To avoid
1076 * overflow, the maximum supported udelay is 2000 microseconds.
1077 *
1078 * XXX it would be more polite to find a way to use msleep()
1079 */
1080 mdelay(2);
1081 udelay(48);
6a3cebb6
OZ
1082 if ((atp_readb_io(dev, 0, 0x1c) & 0x80) == 0x00) { /* bsy ? */
1083 atp_writew_io(dev, 0, 0x1c, 0);
1084 atp_writeb_io(dev, 0, 0x1b, 0);
1085 atp_writeb_io(dev, 0, 0x15, 0);
1086 atp_writeb_io(dev, 0, 0x18, 0x09);
1087 while ((atp_readb_io(dev, 0, 0x1f) & 0x80) == 0)
1da177e4 1088 cpu_relax();
6a3cebb6 1089 atp_readb_io(dev, 0, 0x17);
1da177e4
LT
1090 return;
1091 }
1092 val &= 0x00ff; /* synchronization */
1093 val |= 0x3f00;
1094 fun_scam(dev, &val);
2bbbac45 1095 udelay(2);
1da177e4
LT
1096 val &= 0x00ff; /* isolation */
1097 val |= 0x2000;
1098 fun_scam(dev, &val);
2bbbac45 1099 udelay(2);
1da177e4
LT
1100 i = 8;
1101 j = 0;
c7fcc089
OZ
1102
1103 while (1) {
6a3cebb6 1104 if ((atp_readw_io(dev, 0, 0x1c) & 0x2000) == 0)
c7fcc089 1105 continue;
2bbbac45 1106 udelay(2);
c7fcc089
OZ
1107 val &= 0x00ff; /* get ID_STRING */
1108 val |= 0x2000;
1109 k = fun_scam(dev, &val);
1110 if ((k & 0x03) == 0)
1111 break;
1112 mbuf[j] <<= 0x01;
1113 mbuf[j] &= 0xfe;
1114 if ((k & 0x02) != 0)
1115 mbuf[j] |= 0x01;
1116 i--;
1117 if (i > 0)
1118 continue;
1119 j++;
1120 i = 8;
1da177e4 1121 }
1da177e4 1122
c7fcc089 1123 /* isolation complete.. */
1da177e4
LT
1124/* mbuf[32]=0;
1125 printk(" \n%x %x %x %s\n ",assignid_map,mbuf[0],mbuf[1],&mbuf[2]); */
1126 i = 15;
1127 j = mbuf[0];
25985edc 1128 if ((j & 0x20) != 0) { /* bit5=1:ID up to 7 */
1da177e4
LT
1129 i = 7;
1130 }
c7fcc089
OZ
1131 if ((j & 0x06) != 0) { /* IDvalid? */
1132 k = mbuf[1];
1133 while (1) {
1134 m = 1;
1135 m <<= k;
1136 if ((m & assignid_map) == 0)
1137 break;
1138 if (k > 0)
1139 k--;
1140 else
1141 break;
1142 }
1da177e4 1143 }
c7fcc089
OZ
1144 if ((m & assignid_map) != 0) { /* srch from max acceptable ID# */
1145 k = i; /* max acceptable ID# */
1146 while (1) {
1147 m = 1;
1148 m <<= k;
1149 if ((m & assignid_map) == 0)
1150 break;
1151 if (k > 0)
1152 k--;
1153 else
1154 break;
1155 }
1da177e4 1156 }
c7fcc089 1157 /* k=binID#, */
1da177e4
LT
1158 assignid_map |= m;
1159 if (k < 8) {
1160 quintet[0] = 0x38; /* 1st dft ID<8 */
1161 } else {
1162 quintet[0] = 0x31; /* 1st ID>=8 */
1163 }
1164 k &= 0x07;
1165 quintet[1] = g2q_tab[k];
1166
1167 val &= 0x00ff; /* AssignID 1stQuintet,AH=001xxxxx */
1168 m = quintet[0] << 8;
1169 val |= m;
1170 fun_scam(dev, &val);
1171 val &= 0x00ff; /* AssignID 2ndQuintet,AH=001xxxxx */
1172 m = quintet[1] << 8;
1173 val |= m;
1174 fun_scam(dev, &val);
1175
c7fcc089 1176 }
1da177e4
LT
1177}
1178
1da177e4
LT
1179static void atp870u_free_tables(struct Scsi_Host *host)
1180{
1181 struct atp_unit *atp_dev = (struct atp_unit *)&host->hostdata;
1182 int j, k;
1183 for (j=0; j < 2; j++) {
1184 for (k = 0; k < 16; k++) {
1185 if (!atp_dev->id[j][k].prd_table)
1186 continue;
b5683557 1187 pci_free_consistent(atp_dev->pdev, 1024, atp_dev->id[j][k].prd_table, atp_dev->id[j][k].prd_bus);
1da177e4
LT
1188 atp_dev->id[j][k].prd_table = NULL;
1189 }
1190 }
1191}
1192
1193static int atp870u_init_tables(struct Scsi_Host *host)
1194{
1195 struct atp_unit *atp_dev = (struct atp_unit *)&host->hostdata;
1196 int c,k;
1197 for(c=0;c < 2;c++) {
1198 for(k=0;k<16;k++) {
b5683557 1199 atp_dev->id[c][k].prd_table = pci_alloc_consistent(atp_dev->pdev, 1024, &(atp_dev->id[c][k].prd_bus));
1da177e4
LT
1200 if (!atp_dev->id[c][k].prd_table) {
1201 printk("atp870u_init_tables fail\n");
1202 atp870u_free_tables(host);
1203 return -ENOMEM;
1204 }
b5683557 1205 atp_dev->id[c][k].prdaddr = atp_dev->id[c][k].prd_bus;
1da177e4
LT
1206 atp_dev->id[c][k].devsp=0x20;
1207 atp_dev->id[c][k].devtype = 0x7f;
1208 atp_dev->id[c][k].curr_req = NULL;
1209 }
1210
1211 atp_dev->active_id[c] = 0;
1212 atp_dev->wide_id[c] = 0;
1213 atp_dev->host_id[c] = 0x07;
1214 atp_dev->quhd[c] = 0;
1215 atp_dev->quend[c] = 0;
1216 atp_dev->last_cmd[c] = 0xff;
1217 atp_dev->in_snd[c] = 0;
1218 atp_dev->in_int[c] = 0;
1219
1220 for (k = 0; k < qcnt; k++) {
1221 atp_dev->quereq[c][k] = NULL;
1222 }
1223 for (k = 0; k < 16; k++) {
1224 atp_dev->id[c][k].curr_req = NULL;
1225 atp_dev->sp[c][k] = 0x04;
1226 }
1227 }
1228 return 0;
1229}
1230
6a1961bc
OZ
1231static void atp_set_host_id(struct atp_unit *atp, u8 c, u8 host_id)
1232{
1233 atp_writeb_io(atp, c, 0, host_id | 0x08);
1234 atp_writeb_io(atp, c, 0x18, 0);
1235 while ((atp_readb_io(atp, c, 0x1f) & 0x80) == 0)
1236 mdelay(1);
1237 atp_readb_io(atp, c, 0x17);
1238 atp_writeb_io(atp, c, 1, 8);
1239 atp_writeb_io(atp, c, 2, 0x7f);
1240 atp_writeb_io(atp, c, 0x11, 0x20);
1241}
1242
1da177e4
LT
1243/* return non-zero on detection */
1244static int atp870u_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1245{
1246 unsigned char k, m, c;
1247 unsigned long flags;
493c5201 1248 unsigned int base_io, error,n;
1da177e4
LT
1249 unsigned char host_id;
1250 struct Scsi_Host *shpnt = NULL;
bdd5ac40 1251 struct atp_unit *atpdev;
1da177e4 1252 unsigned char setupdata[2][16];
bdd5ac40 1253 int err;
dc6a78f1 1254
bdd5ac40
OZ
1255 err = pci_enable_device(pdev);
1256 if (err)
1257 goto fail;
1da177e4 1258
34a2c35d 1259 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32))) {
1da177e4 1260 printk(KERN_ERR "atp870u: DMA mask required but not available.\n");
bdd5ac40
OZ
1261 err = -EIO;
1262 goto disable_device;
1da177e4
LT
1263 }
1264
bdd5ac40
OZ
1265 err = -ENOMEM;
1266 shpnt = scsi_host_alloc(&atp870u_template, sizeof(struct atp_unit));
1267 if (!shpnt)
1268 goto disable_device;
1269
1270 atpdev = shost_priv(shpnt);
1271
1272 atpdev->host = shpnt;
1273 atpdev->pdev = pdev;
1274 pci_set_drvdata(pdev, atpdev);
1275
1da177e4
LT
1276 /*
1277 * It's probably easier to weed out some revisions like
1278 * this than via the PCI device table
1279 */
1280 if (ent->device == PCI_DEVICE_ID_ARTOP_AEC7610) {
7d7311c4 1281 atpdev->chip_ver = pdev->revision;
bdd5ac40
OZ
1282 if (atpdev->chip_ver < 2) {
1283 err = -ENODEV;
1284 goto unregister;
1285 }
1da177e4
LT
1286 }
1287
1288 switch (ent->device) {
1289 case PCI_DEVICE_ID_ARTOP_AEC7612UW:
1290 case PCI_DEVICE_ID_ARTOP_AEC7612SUW:
1291 case ATP880_DEVID1:
1292 case ATP880_DEVID2:
1293 case ATP885_DEVID:
dc6a78f1 1294 atpdev->chip_ver = 0x04;
1da177e4
LT
1295 default:
1296 break;
1297 }
1298 base_io = pci_resource_start(pdev, 0);
1299 base_io &= 0xfffffff8;
6a3cebb6 1300 atpdev->baseport = base_io;
dc6a78f1 1301
1da177e4 1302 if ((ent->device == ATP880_DEVID1)||(ent->device == ATP880_DEVID2)) {
7d7311c4 1303 atpdev->chip_ver = pdev->revision;
1da177e4
LT
1304 pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0x80);//JCC082803
1305
d804bb25
OZ
1306 atpdev->ioport[0] = base_io + 0x40;
1307 atpdev->pciport[0] = base_io + 0x28;
1308
1309 host_id = atp_readb_base(atpdev, 0x39);
1da177e4
LT
1310 host_id >>= 0x04;
1311
c48442d1
OZ
1312 printk(KERN_INFO " ACARD AEC-67160 PCI Ultra3 LVD Host Adapter:"
1313 " IO:%x, IRQ:%d.\n", base_io, pdev->irq);
dc6a78f1
RD
1314 atpdev->dev_id = ent->device;
1315 atpdev->host_id[0] = host_id;
1da177e4 1316
d804bb25
OZ
1317 atpdev->scam_on = atp_readb_base(atpdev, 0x22);
1318 atpdev->global_map[0] = atp_readb_base(atpdev, 0x35);
1319 atpdev->ultra_map[0] = atp_readw_base(atpdev, 0x3c);
1da177e4
LT
1320
1321 n = 0x3f09;
1322next_fblk_880:
1323 if (n >= 0x4000)
1324 goto flash_ok_880;
1325
1326 m = 0;
d804bb25 1327 atp_writew_base(atpdev, 0x34, n);
1da177e4 1328 n += 0x0002;
d804bb25 1329 if (atp_readb_base(atpdev, 0x30) == 0xff)
1da177e4
LT
1330 goto flash_ok_880;
1331
d804bb25
OZ
1332 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x30);
1333 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x31);
1334 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x32);
1335 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x33);
1336 atp_writew_base(atpdev, 0x34, n);
1da177e4 1337 n += 0x0002;
d804bb25
OZ
1338 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x30);
1339 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x31);
1340 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x32);
1341 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x33);
1342 atp_writew_base(atpdev, 0x34, n);
1da177e4 1343 n += 0x0002;
d804bb25
OZ
1344 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x30);
1345 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x31);
1346 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x32);
1347 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x33);
1348 atp_writew_base(atpdev, 0x34, n);
1da177e4 1349 n += 0x0002;
d804bb25
OZ
1350 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x30);
1351 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x31);
1352 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x32);
1353 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x33);
1da177e4
LT
1354 n += 0x0018;
1355 goto next_fblk_880;
1356flash_ok_880:
d804bb25 1357 atp_writew_base(atpdev, 0x34, 0);
dc6a78f1
RD
1358 atpdev->ultra_map[0] = 0;
1359 atpdev->async[0] = 0;
1da177e4
LT
1360 for (k = 0; k < 16; k++) {
1361 n = 1;
1362 n = n << k;
dc6a78f1
RD
1363 if (atpdev->sp[0][k] > 1) {
1364 atpdev->ultra_map[0] |= n;
1da177e4 1365 } else {
dc6a78f1
RD
1366 if (atpdev->sp[0][k] == 0)
1367 atpdev->async[0] |= n;
1da177e4
LT
1368 }
1369 }
dc6a78f1 1370 atpdev->async[0] = ~(atpdev->async[0]);
d804bb25 1371 atp_writeb_base(atpdev, 0x35, atpdev->global_map[0]);
1da177e4 1372
1da177e4
LT
1373 if (atp870u_init_tables(shpnt) < 0) {
1374 printk(KERN_ERR "Unable to allocate tables for Acard controller\n");
bdd5ac40 1375 err = -ENOMEM;
1da177e4
LT
1376 goto unregister;
1377 }
1378
bdd5ac40
OZ
1379 err = request_irq(pdev->irq, atp870u_intr_handle, IRQF_SHARED, "atp880i", shpnt);
1380 if (err) {
1da177e4
LT
1381 printk(KERN_ERR "Unable to allocate IRQ%d for Acard controller.\n", pdev->irq);
1382 goto free_tables;
1383 }
1384
1385 spin_lock_irqsave(shpnt->host_lock, flags);
bdd5ac40
OZ
1386 k = atp_readb_base(atpdev, 0x38) & 0x80;
1387 atp_writeb_base(atpdev, 0x38, k);
1388 atp_writeb_base(atpdev, 0x3b, 0x20);
1da177e4 1389 mdelay(32);
bdd5ac40 1390 atp_writeb_base(atpdev, 0x3b, 0);
1da177e4 1391 mdelay(32);
bdd5ac40
OZ
1392 atp_readb_io(atpdev, 0, 0x1b);
1393 atp_readb_io(atpdev, 0, 0x17);
6a1961bc 1394
bdd5ac40 1395 atp_set_host_id(atpdev, 0, host_id);
1da177e4
LT
1396
1397 tscam(shpnt);
bdd5ac40
OZ
1398 atp_is(atpdev, 0, true, atp_readb_base(atpdev, 0x3f) & 0x40);
1399 atp_writeb_base(atpdev, 0x38, 0xb0);
1da177e4
LT
1400 shpnt->max_id = 16;
1401 shpnt->this_id = host_id;
1402 shpnt->unique_id = base_io;
1403 shpnt->io_port = base_io;
1404 shpnt->n_io_port = 0x60; /* Number of bytes of I/O space used */
1405 shpnt->irq = pdev->irq;
1406 } else if (ent->device == ATP885_DEVID) {
1407 printk(KERN_INFO " ACARD AEC-67162 PCI Ultra3 LVD Host Adapter: IO:%x, IRQ:%d.\n"
1408 , base_io, pdev->irq);
1409
dc6a78f1
RD
1410 atpdev->pdev = pdev;
1411 atpdev->dev_id = ent->device;
dc6a78f1
RD
1412 atpdev->ioport[0] = base_io + 0x80;
1413 atpdev->ioport[1] = base_io + 0xc0;
1414 atpdev->pciport[0] = base_io + 0x40;
1415 atpdev->pciport[1] = base_io + 0x50;
1da177e4 1416
bdd5ac40
OZ
1417 if (atp870u_init_tables(shpnt) < 0) {
1418 err = -ENOMEM;
1da177e4 1419 goto unregister;
bdd5ac40 1420 }
1da177e4
LT
1421
1422#ifdef ED_DBGP
bdd5ac40 1423 printk("request_irq() shpnt %p hostdata %p\n", shpnt, atpdev);
1da177e4 1424#endif
bdd5ac40
OZ
1425 err = request_irq(pdev->irq, atp870u_intr_handle, IRQF_SHARED, "atp870u", shpnt);
1426 if (err) {
1da177e4
LT
1427 printk(KERN_ERR "Unable to allocate IRQ for Acard controller.\n");
1428 goto free_tables;
1429 }
1430
1431 spin_lock_irqsave(shpnt->host_lock, flags);
1432
bdd5ac40
OZ
1433 c = atp_readb_base(atpdev, 0x29);
1434 atp_writeb_base(atpdev, 0x29, c | 0x04);
1da177e4
LT
1435
1436 n=0x1f80;
1437next_fblk_885:
1438 if (n >= 0x2000) {
1439 goto flash_ok_885;
1440 }
bdd5ac40
OZ
1441 atp_writew_base(atpdev, 0x3c, n);
1442 if (atp_readl_base(atpdev, 0x38) == 0xffffffff) {
1da177e4
LT
1443 goto flash_ok_885;
1444 }
1445 for (m=0; m < 2; m++) {
bdd5ac40 1446 atpdev->global_map[m]= 0;
1da177e4 1447 for (k=0; k < 4; k++) {
bdd5ac40
OZ
1448 atp_writew_base(atpdev, 0x3c, n++);
1449 ((unsigned long *)&setupdata[m][0])[k] = atp_readl_base(atpdev, 0x38);
1da177e4
LT
1450 }
1451 for (k=0; k < 4; k++) {
bdd5ac40
OZ
1452 atp_writew_base(atpdev, 0x3c, n++);
1453 ((unsigned long *)&atpdev->sp[m][0])[k] = atp_readl_base(atpdev, 0x38);
1da177e4
LT
1454 }
1455 n += 8;
1456 }
1457 goto next_fblk_885;
1458flash_ok_885:
1459#ifdef ED_DBGP
1460 printk( "Flash Read OK\n");
1461#endif
bdd5ac40
OZ
1462 c = atp_readb_base(atpdev, 0x29);
1463 atp_writeb_base(atpdev, 0x29, c & 0xfb);
1da177e4 1464 for (c=0;c < 2;c++) {
bdd5ac40
OZ
1465 atpdev->ultra_map[c]=0;
1466 atpdev->async[c] = 0;
1da177e4
LT
1467 for (k=0; k < 16; k++) {
1468 n=1;
1469 n = n << k;
bdd5ac40
OZ
1470 if (atpdev->sp[c][k] > 1) {
1471 atpdev->ultra_map[c] |= n;
1da177e4 1472 } else {
bdd5ac40
OZ
1473 if (atpdev->sp[c][k] == 0) {
1474 atpdev->async[c] |= n;
1da177e4
LT
1475 }
1476 }
1477 }
bdd5ac40 1478 atpdev->async[c] = ~(atpdev->async[c]);
1da177e4 1479
bdd5ac40 1480 if (atpdev->global_map[c] == 0) {
1da177e4
LT
1481 k=setupdata[c][1];
1482 if ((k & 0x40) != 0)
bdd5ac40 1483 atpdev->global_map[c] |= 0x20;
1da177e4 1484 k &= 0x07;
bdd5ac40 1485 atpdev->global_map[c] |= k;
1da177e4 1486 if ((setupdata[c][2] & 0x04) != 0)
bdd5ac40
OZ
1487 atpdev->global_map[c] |= 0x08;
1488 atpdev->host_id[c] = setupdata[c][0] & 0x07;
1da177e4
LT
1489 }
1490 }
1491
bdd5ac40 1492 k = atp_readb_base(atpdev, 0x28) & 0x8f;
1da177e4 1493 k |= 0x10;
bdd5ac40
OZ
1494 atp_writeb_base(atpdev, 0x28, k);
1495 atp_writeb_pci(atpdev, 0, 1, 0x80);
1496 atp_writeb_pci(atpdev, 1, 1, 0x80);
1da177e4 1497 mdelay(100);
bdd5ac40
OZ
1498 atp_writeb_pci(atpdev, 0, 1, 0);
1499 atp_writeb_pci(atpdev, 1, 1, 0);
1da177e4 1500 mdelay(1000);
bdd5ac40
OZ
1501 atp_readb_io(atpdev, 0, 0x1b);
1502 atp_readb_io(atpdev, 0, 0x17);
1503 atp_readb_io(atpdev, 1, 0x1b);
1504 atp_readb_io(atpdev, 1, 0x17);
6a1961bc 1505
bdd5ac40 1506 k=atpdev->host_id[0];
1da177e4
LT
1507 if (k > 7)
1508 k = (k & 0x07) | 0x40;
bdd5ac40 1509 atp_set_host_id(atpdev, 0, k);
493c5201 1510
bdd5ac40 1511 k=atpdev->host_id[1];
1da177e4
LT
1512 if (k > 7)
1513 k = (k & 0x07) | 0x40;
bdd5ac40 1514 atp_set_host_id(atpdev, 1, k);
1da177e4 1515
c4ad92bc 1516 mdelay(600); /* this delay used to be called tscam_885() */
1da177e4 1517 printk(KERN_INFO " Scanning Channel A SCSI Device ...\n");
bdd5ac40
OZ
1518 atp_is(atpdev, 0, true, atp_readb_io(atpdev, 0, 0x1b) >> 7);
1519 atp_writeb_io(atpdev, 0, 0x16, 0x80);
1da177e4 1520 printk(KERN_INFO " Scanning Channel B SCSI Device ...\n");
bdd5ac40
OZ
1521 atp_is(atpdev, 1, true, atp_readb_io(atpdev, 1, 0x1b) >> 7);
1522 atp_writeb_io(atpdev, 1, 0x16, 0x80);
1523 k = atp_readb_base(atpdev, 0x28) & 0xcf;
1da177e4 1524 k |= 0xc0;
bdd5ac40
OZ
1525 atp_writeb_base(atpdev, 0x28, k);
1526 k = atp_readb_base(atpdev, 0x1f) | 0x80;
1527 atp_writeb_base(atpdev, 0x1f, k);
1528 k = atp_readb_base(atpdev, 0x29) | 0x01;
1529 atp_writeb_base(atpdev, 0x29, k);
1da177e4
LT
1530#ifdef ED_DBGP
1531 //printk("atp885: atp_host[0] 0x%p\n", atp_host[0]);
1532#endif
1533 shpnt->max_id = 16;
bdd5ac40 1534 shpnt->max_lun = (atpdev->global_map[0] & 0x07) + 1;
1da177e4 1535 shpnt->max_channel = 1;
bdd5ac40 1536 shpnt->this_id = atpdev->host_id[0];
1da177e4
LT
1537 shpnt->unique_id = base_io;
1538 shpnt->io_port = base_io;
1539 shpnt->n_io_port = 0xff; /* Number of bytes of I/O space used */
1540 shpnt->irq = pdev->irq;
1541
1542 } else {
1543 error = pci_read_config_byte(pdev, 0x49, &host_id);
1544
c48442d1
OZ
1545 printk(KERN_INFO " ACARD AEC-671X PCI Ultra/W SCSI-2/3 Host Adapter: "
1546 "IO:%x, IRQ:%d.\n", base_io, pdev->irq);
1da177e4 1547
dc6a78f1
RD
1548 atpdev->ioport[0] = base_io;
1549 atpdev->pciport[0] = base_io + 0x20;
1550 atpdev->dev_id = ent->device;
1da177e4 1551 host_id &= 0x07;
dc6a78f1 1552 atpdev->host_id[0] = host_id;
d804bb25
OZ
1553 atpdev->scam_on = atp_readb_pci(atpdev, 0, 2);
1554 atpdev->global_map[0] = atp_readb_base(atpdev, 0x2d);
1555 atpdev->ultra_map[0] = atp_readw_base(atpdev, 0x2e);
1da177e4 1556
dc6a78f1
RD
1557 if (atpdev->ultra_map[0] == 0) {
1558 atpdev->scam_on = 0x00;
1559 atpdev->global_map[0] = 0x20;
1560 atpdev->ultra_map[0] = 0xffff;
1da177e4
LT
1561 }
1562
1da177e4 1563
bdd5ac40
OZ
1564 if (atp870u_init_tables(shpnt) < 0) {
1565 err = -ENOMEM;
1da177e4 1566 goto unregister;
bdd5ac40 1567 }
1da177e4 1568
bdd5ac40
OZ
1569 err = request_irq(pdev->irq, atp870u_intr_handle, IRQF_SHARED, "atp870i", shpnt);
1570 if (err) {
1da177e4
LT
1571 printk(KERN_ERR "Unable to allocate IRQ%d for Acard controller.\n", pdev->irq);
1572 goto free_tables;
1573 }
1574
1575 spin_lock_irqsave(shpnt->host_lock, flags);
d804bb25 1576 if (atpdev->chip_ver > 0x07) /* check if atp876 chip then enable terminator */
bdd5ac40 1577 atp_writeb_base(atpdev, 0x3e, 0x00);
1da177e4 1578
bdd5ac40
OZ
1579 k = (atp_readb_base(atpdev, 0x3a) & 0xf3) | 0x10;
1580 atp_writeb_base(atpdev, 0x3a, k);
1581 atp_writeb_base(atpdev, 0x3a, k & 0xdf);
1da177e4 1582 mdelay(32);
bdd5ac40 1583 atp_writeb_base(atpdev, 0x3a, k);
1da177e4 1584 mdelay(32);
bdd5ac40 1585 atp_set_host_id(atpdev, 0, host_id);
1da177e4
LT
1586
1587 tscam(shpnt);
bdd5ac40
OZ
1588 atp_writeb_base(atpdev, 0x3a, atp_readb_base(atpdev, 0x3a) | 0x10);
1589 atp_is(atpdev, 0, atpdev->chip_ver == 4, 0);
1590 atp_writeb_base(atpdev, 0x3a, atp_readb_base(atpdev, 0x3a) & 0xef);
1591 atp_writeb_base(atpdev, 0x3b, atp_readb_base(atpdev, 0x3b) | 0x20);
dc6a78f1 1592 if (atpdev->chip_ver == 4)
1da177e4
LT
1593 shpnt->max_id = 16;
1594 else
2b89dad0 1595 shpnt->max_id = 8;
1da177e4
LT
1596 shpnt->this_id = host_id;
1597 shpnt->unique_id = base_io;
1598 shpnt->io_port = base_io;
1599 shpnt->n_io_port = 0x40; /* Number of bytes of I/O space used */
1600 shpnt->irq = pdev->irq;
1601 }
1602 spin_unlock_irqrestore(shpnt->host_lock, flags);
bdd5ac40
OZ
1603 if (!request_region(base_io, shpnt->n_io_port, "atp870u")) {
1604 err = -EBUSY;
30ebc7ef 1605 goto request_io_fail;
bdd5ac40
OZ
1606 }
1607 err = scsi_add_host(shpnt, &pdev->dev);
1608 if (err)
1da177e4
LT
1609 goto scsi_add_fail;
1610 scsi_scan_host(shpnt);
1611#ifdef ED_DBGP
1612 printk("atp870u_prob : exit\n");
1613#endif
1614 return 0;
1615
1616scsi_add_fail:
1617 printk("atp870u_prob:scsi_add_fail\n");
30ebc7ef 1618 release_region(base_io, shpnt->n_io_port);
1da177e4
LT
1619request_io_fail:
1620 printk("atp870u_prob:request_io_fail\n");
1621 free_irq(pdev->irq, shpnt);
1622free_tables:
1623 printk("atp870u_prob:free_table\n");
1624 atp870u_free_tables(shpnt);
1625unregister:
1da177e4 1626 scsi_host_put(shpnt);
bdd5ac40
OZ
1627disable_device:
1628 pci_disable_device(pdev);
1629fail:
1630 return err;
1da177e4
LT
1631}
1632
1633/* The abort command does not leave the device in a clean state where
1634 it is available to be used again. Until this gets worked out, we will
1635 leave it commented out. */
1636
1637static int atp870u_abort(struct scsi_cmnd * SCpnt)
1638{
1639 unsigned char j, k, c;
1640 struct scsi_cmnd *workrequ;
1da177e4
LT
1641 struct atp_unit *dev;
1642 struct Scsi_Host *host;
1643 host = SCpnt->device->host;
1644
1645 dev = (struct atp_unit *)&host->hostdata;
422c0d61 1646 c = scmd_channel(SCpnt);
1da177e4
LT
1647 printk(" atp870u: abort Channel = %x \n", c);
1648 printk("working=%x last_cmd=%x ", dev->working[c], dev->last_cmd[c]);
1649 printk(" quhdu=%x quendu=%x ", dev->quhd[c], dev->quend[c]);
1da177e4 1650 for (j = 0; j < 0x18; j++) {
6a3cebb6 1651 printk(" r%2x=%2x", j, atp_readb_io(dev, c, j));
1da177e4 1652 }
6a3cebb6
OZ
1653 printk(" r1c=%2x", atp_readb_io(dev, c, 0x1c));
1654 printk(" r1f=%2x in_snd=%2x ", atp_readb_io(dev, c, 0x1f), dev->in_snd[c]);
1655 printk(" d00=%2x", atp_readb_pci(dev, c, 0x00));
1656 printk(" d02=%2x", atp_readb_pci(dev, c, 0x02));
1da177e4
LT
1657 for(j=0;j<16;j++) {
1658 if (dev->id[c][j].curr_req != NULL) {
1659 workrequ = dev->id[c][j].curr_req;
1660 printk("\n que cdb= ");
1661 for (k=0; k < workrequ->cmd_len; k++) {
1662 printk(" %2x ",workrequ->cmnd[k]);
1663 }
1664 printk(" last_lenu= %x ",(unsigned int)dev->id[c][j].last_len);
1665 }
1666 }
1667 return SUCCESS;
1668}
1669
1670static const char *atp870u_info(struct Scsi_Host *notused)
1671{
1672 static char buffer[128];
1673
1674 strcpy(buffer, "ACARD AEC-6710/6712/67160 PCI Ultra/W/LVD SCSI-3 Adapter Driver V2.6+ac ");
1675
1676 return buffer;
1677}
1678
d773e422 1679static int atp870u_show_info(struct seq_file *m, struct Scsi_Host *HBAptr)
1da177e4 1680{
3d30079c
RV
1681 seq_puts(m, "ACARD AEC-671X Driver Version: 2.6+ac\n\n"
1682 "Adapter Configuration:\n");
d773e422
AV
1683 seq_printf(m, " Base IO: %#.4lx\n", HBAptr->io_port);
1684 seq_printf(m, " IRQ: %d\n", HBAptr->irq);
1685 return 0;
1da177e4
LT
1686}
1687
1688
1689static int atp870u_biosparam(struct scsi_device *disk, struct block_device *dev,
1690 sector_t capacity, int *ip)
1691{
1692 int heads, sectors, cylinders;
1693
1694 heads = 64;
1695 sectors = 32;
1696 cylinders = (unsigned long)capacity / (heads * sectors);
1697 if (cylinders > 1024) {
1698 heads = 255;
1699 sectors = 63;
1700 cylinders = (unsigned long)capacity / (heads * sectors);
1701 }
1702 ip[0] = heads;
1703 ip[1] = sectors;
1704 ip[2] = cylinders;
1705
1706 return 0;
1707}
1708
1709static void atp870u_remove (struct pci_dev *pdev)
1710{
1711 struct atp_unit *devext = pci_get_drvdata(pdev);
1712 struct Scsi_Host *pshost = devext->host;
1713
1714
1715 scsi_remove_host(pshost);
1da177e4
LT
1716 free_irq(pshost->irq, pshost);
1717 release_region(pshost->io_port, pshost->n_io_port);
1da177e4 1718 atp870u_free_tables(pshost);
1da177e4 1719 scsi_host_put(pshost);
1da177e4
LT
1720}
1721MODULE_LICENSE("GPL");
1722
1723static struct scsi_host_template atp870u_template = {
1724 .module = THIS_MODULE,
1725 .name = "atp870u" /* name */,
1726 .proc_name = "atp870u",
d773e422 1727 .show_info = atp870u_show_info,
1da177e4
LT
1728 .info = atp870u_info /* info */,
1729 .queuecommand = atp870u_queuecommand /* queuecommand */,
1730 .eh_abort_handler = atp870u_abort /* abort */,
1731 .bios_param = atp870u_biosparam /* biosparm */,
1732 .can_queue = qcnt /* can_queue */,
1733 .this_id = 7 /* SCSI ID */,
1734 .sg_tablesize = ATP870U_SCATTER /*SG_ALL*/ /*SG_NONE*/,
1da177e4
LT
1735 .use_clustering = ENABLE_CLUSTERING,
1736 .max_sectors = ATP870U_MAX_SECTORS,
1737};
1738
1739static struct pci_device_id atp870u_id_table[] = {
1740 { PCI_DEVICE(PCI_VENDOR_ID_ARTOP, ATP885_DEVID) },
1741 { PCI_DEVICE(PCI_VENDOR_ID_ARTOP, ATP880_DEVID1) },
1742 { PCI_DEVICE(PCI_VENDOR_ID_ARTOP, ATP880_DEVID2) },
1743 { PCI_DEVICE(PCI_VENDOR_ID_ARTOP, PCI_DEVICE_ID_ARTOP_AEC7610) },
1744 { PCI_DEVICE(PCI_VENDOR_ID_ARTOP, PCI_DEVICE_ID_ARTOP_AEC7612UW) },
1745 { PCI_DEVICE(PCI_VENDOR_ID_ARTOP, PCI_DEVICE_ID_ARTOP_AEC7612U) },
1746 { PCI_DEVICE(PCI_VENDOR_ID_ARTOP, PCI_DEVICE_ID_ARTOP_AEC7612S) },
1747 { PCI_DEVICE(PCI_VENDOR_ID_ARTOP, PCI_DEVICE_ID_ARTOP_AEC7612D) },
1748 { PCI_DEVICE(PCI_VENDOR_ID_ARTOP, PCI_DEVICE_ID_ARTOP_AEC7612SUW) },
1749 { PCI_DEVICE(PCI_VENDOR_ID_ARTOP, PCI_DEVICE_ID_ARTOP_8060) },
1750 { 0, },
1751};
1752
1753MODULE_DEVICE_TABLE(pci, atp870u_id_table);
1754
1755static struct pci_driver atp870u_driver = {
1756 .id_table = atp870u_id_table,
1757 .name = "atp870u",
1758 .probe = atp870u_probe,
6f039790 1759 .remove = atp870u_remove,
1da177e4
LT
1760};
1761
1ccd7d68 1762module_pci_driver(atp870u_driver);
1da177e4 1763
4192a40f 1764static void atp_is(struct atp_unit *dev, unsigned char c, bool wide_chip, unsigned char lvdmode)
1da177e4 1765{
fa50b308 1766 unsigned char i, j, k, rmb, n;
1da177e4
LT
1767 unsigned short int m;
1768 static unsigned char mbuf[512];
80b52a7f
OZ
1769 static unsigned char satn[9] = { 0, 0, 0, 0, 0, 0, 0, 6, 6 };
1770 static unsigned char inqd[9] = { 0x12, 0, 0, 0, 0x24, 0, 0, 0x24, 6 };
1771 static unsigned char synn[6] = { 0x80, 1, 3, 1, 0x19, 0x0e };
1772 unsigned char synu[6] = { 0x80, 1, 3, 1, 0x0a, 0x0e };
1773 static unsigned char synw[6] = { 0x80, 1, 3, 1, 0x19, 0x0e };
460da918 1774 static unsigned char synw_870[6] = { 0x80, 1, 3, 1, 0x0c, 0x07 };
80b52a7f
OZ
1775 unsigned char synuw[6] = { 0x80, 1, 3, 1, 0x0a, 0x0e };
1776 static unsigned char wide[6] = { 0x80, 1, 2, 3, 1, 0 };
1777 static unsigned char u3[9] = { 0x80, 1, 6, 4, 0x09, 00, 0x0e, 0x01, 0x02 };
1da177e4 1778
1da177e4 1779 for (i = 0; i < 16; i++) {
197fb8d8
OZ
1780 if (!wide_chip && (i > 7))
1781 break;
1da177e4
LT
1782 m = 1;
1783 m = m << i;
1784 if ((m & dev->active_id[c]) != 0) {
1785 continue;
1786 }
1787 if (i == dev->host_id[c]) {
1788 printk(KERN_INFO " ID: %2d Host Adapter\n", dev->host_id[c]);
1789 continue;
1790 }
197fb8d8 1791 atp_writeb_io(dev, c, 0x1b, wide_chip ? 0x01 : 0x00);
5d2a5a4f
OZ
1792 atp_writeb_io(dev, c, 1, 0x08);
1793 atp_writeb_io(dev, c, 2, 0x7f);
1794 atp_writeb_io(dev, c, 3, satn[0]);
1795 atp_writeb_io(dev, c, 4, satn[1]);
1796 atp_writeb_io(dev, c, 5, satn[2]);
1797 atp_writeb_io(dev, c, 6, satn[3]);
1798 atp_writeb_io(dev, c, 7, satn[4]);
1799 atp_writeb_io(dev, c, 8, satn[5]);
1800 atp_writeb_io(dev, c, 0x0f, 0);
1801 atp_writeb_io(dev, c, 0x11, dev->id[c][i].devsp);
5d2a5a4f
OZ
1802 atp_writeb_io(dev, c, 0x12, 0);
1803 atp_writeb_io(dev, c, 0x13, satn[6]);
1804 atp_writeb_io(dev, c, 0x14, satn[7]);
1da177e4
LT
1805 j = i;
1806 if ((j & 0x08) != 0) {
1807 j = (j & 0x07) | 0x40;
1808 }
5d2a5a4f
OZ
1809 atp_writeb_io(dev, c, 0x15, j);
1810 atp_writeb_io(dev, c, 0x18, satn[8]);
1da177e4 1811
5d2a5a4f 1812 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00)
1da177e4 1813 cpu_relax();
80b52a7f
OZ
1814
1815 if (atp_readb_io(dev, c, 0x17) != 0x11 && atp_readb_io(dev, c, 0x17) != 0x8e)
1da177e4 1816 continue;
80b52a7f 1817
5d2a5a4f 1818 while (atp_readb_io(dev, c, 0x17) != 0x8e)
1da177e4 1819 cpu_relax();
80b52a7f 1820
1da177e4
LT
1821 dev->active_id[c] |= m;
1822
5d2a5a4f 1823 atp_writeb_io(dev, c, 0x10, 0x30);
460da918
OZ
1824 if (dev->dev_id == ATP885_DEVID || dev->dev_id == ATP880_DEVID1 || dev->dev_id == ATP880_DEVID2)
1825 atp_writeb_io(dev, c, 0x14, 0x00);
1826 else /* result of is870() merge - is this a bug? */
1827 atp_writeb_io(dev, c, 0x04, 0x00);
1da177e4
LT
1828
1829phase_cmd:
5d2a5a4f 1830 atp_writeb_io(dev, c, 0x18, 0x08);
80b52a7f 1831
5d2a5a4f 1832 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00)
1da177e4 1833 cpu_relax();
80b52a7f 1834
5d2a5a4f 1835 j = atp_readb_io(dev, c, 0x17);
1da177e4 1836 if (j != 0x16) {
5d2a5a4f 1837 atp_writeb_io(dev, c, 0x10, 0x41);
1da177e4
LT
1838 goto phase_cmd;
1839 }
1840sel_ok:
5d2a5a4f
OZ
1841 atp_writeb_io(dev, c, 3, inqd[0]);
1842 atp_writeb_io(dev, c, 4, inqd[1]);
1843 atp_writeb_io(dev, c, 5, inqd[2]);
1844 atp_writeb_io(dev, c, 6, inqd[3]);
1845 atp_writeb_io(dev, c, 7, inqd[4]);
1846 atp_writeb_io(dev, c, 8, inqd[5]);
1847 atp_writeb_io(dev, c, 0x0f, 0);
1848 atp_writeb_io(dev, c, 0x11, dev->id[c][i].devsp);
1849 atp_writeb_io(dev, c, 0x12, 0);
1850 atp_writeb_io(dev, c, 0x13, inqd[6]);
1851 atp_writeb_io(dev, c, 0x14, inqd[7]);
1852 atp_writeb_io(dev, c, 0x18, inqd[8]);
80b52a7f 1853
5d2a5a4f 1854 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00)
1da177e4 1855 cpu_relax();
80b52a7f
OZ
1856
1857 if (atp_readb_io(dev, c, 0x17) != 0x11 && atp_readb_io(dev, c, 0x17) != 0x8e)
1da177e4 1858 continue;
80b52a7f 1859
5d2a5a4f 1860 while (atp_readb_io(dev, c, 0x17) != 0x8e)
1da177e4 1861 cpu_relax();
80b52a7f 1862
197fb8d8
OZ
1863 if (wide_chip)
1864 atp_writeb_io(dev, c, 0x1b, 0x00);
1865
5d2a5a4f 1866 atp_writeb_io(dev, c, 0x18, 0x08);
1da177e4
LT
1867 j = 0;
1868rd_inq_data:
5d2a5a4f 1869 k = atp_readb_io(dev, c, 0x1f);
1da177e4 1870 if ((k & 0x01) != 0) {
5d2a5a4f 1871 mbuf[j++] = atp_readb_io(dev, c, 0x19);
1da177e4
LT
1872 goto rd_inq_data;
1873 }
1874 if ((k & 0x80) == 0) {
1875 goto rd_inq_data;
1876 }
5d2a5a4f 1877 j = atp_readb_io(dev, c, 0x17);
1da177e4
LT
1878 if (j == 0x16) {
1879 goto inq_ok;
1880 }
5d2a5a4f
OZ
1881 atp_writeb_io(dev, c, 0x10, 0x46);
1882 atp_writeb_io(dev, c, 0x12, 0);
1883 atp_writeb_io(dev, c, 0x13, 0);
1884 atp_writeb_io(dev, c, 0x14, 0);
1885 atp_writeb_io(dev, c, 0x18, 0x08);
80b52a7f 1886
5d2a5a4f 1887 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00)
1da177e4 1888 cpu_relax();
80b52a7f
OZ
1889
1890 if (atp_readb_io(dev, c, 0x17) != 0x16)
1da177e4 1891 goto sel_ok;
80b52a7f 1892
1da177e4
LT
1893inq_ok:
1894 mbuf[36] = 0;
80b52a7f 1895 printk(KERN_INFO " ID: %2d %s\n", i, &mbuf[8]);
1da177e4
LT
1896 dev->id[c][i].devtype = mbuf[0];
1897 rmb = mbuf[1];
1898 n = mbuf[7];
197fb8d8
OZ
1899 if (!wide_chip)
1900 goto not_wide;
1da177e4
LT
1901 if ((mbuf[7] & 0x60) == 0) {
1902 goto not_wide;
1903 }
197fb8d8
OZ
1904 if (dev->dev_id == ATP885_DEVID || dev->dev_id == ATP880_DEVID1 || dev->dev_id == ATP880_DEVID2) {
1905 if ((i < 8) && ((dev->global_map[c] & 0x20) == 0))
1906 goto not_wide;
1907 } else { /* result of is870() merge - is this a bug? */
1908 if ((dev->global_map[c] & 0x20) == 0)
1909 goto not_wide;
1da177e4
LT
1910 }
1911 if (lvdmode == 0) {
80b52a7f 1912 goto chg_wide;
1da177e4 1913 }
80b52a7f
OZ
1914 if (dev->sp[c][i] != 0x04) // force u2
1915 {
1916 goto chg_wide;
1da177e4
LT
1917 }
1918
5d2a5a4f
OZ
1919 atp_writeb_io(dev, c, 0x1b, 0x01);
1920 atp_writeb_io(dev, c, 3, satn[0]);
1921 atp_writeb_io(dev, c, 4, satn[1]);
1922 atp_writeb_io(dev, c, 5, satn[2]);
1923 atp_writeb_io(dev, c, 6, satn[3]);
1924 atp_writeb_io(dev, c, 7, satn[4]);
1925 atp_writeb_io(dev, c, 8, satn[5]);
1926 atp_writeb_io(dev, c, 0x0f, 0);
1927 atp_writeb_io(dev, c, 0x11, dev->id[c][i].devsp);
1928 atp_writeb_io(dev, c, 0x12, 0);
1929 atp_writeb_io(dev, c, 0x13, satn[6]);
1930 atp_writeb_io(dev, c, 0x14, satn[7]);
1931 atp_writeb_io(dev, c, 0x18, satn[8]);
1932
1933 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00)
1da177e4 1934 cpu_relax();
80b52a7f
OZ
1935
1936 if (atp_readb_io(dev, c, 0x17) != 0x11 && atp_readb_io(dev, c, 0x17) != 0x8e)
1da177e4 1937 continue;
80b52a7f 1938
5d2a5a4f 1939 while (atp_readb_io(dev, c, 0x17) != 0x8e)
1da177e4 1940 cpu_relax();
80b52a7f 1941
1da177e4
LT
1942try_u3:
1943 j = 0;
5d2a5a4f
OZ
1944 atp_writeb_io(dev, c, 0x14, 0x09);
1945 atp_writeb_io(dev, c, 0x18, 0x20);
e2c22b45 1946
5d2a5a4f
OZ
1947 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0) {
1948 if ((atp_readb_io(dev, c, 0x1f) & 0x01) != 0)
1949 atp_writeb_io(dev, c, 0x19, u3[j++]);
1da177e4
LT
1950 cpu_relax();
1951 }
80b52a7f 1952
5d2a5a4f 1953 while ((atp_readb_io(dev, c, 0x17) & 0x80) == 0x00)
1da177e4 1954 cpu_relax();
80b52a7f 1955
5d2a5a4f 1956 j = atp_readb_io(dev, c, 0x17) & 0x0f;
1da177e4
LT
1957 if (j == 0x0f) {
1958 goto u3p_in;
1959 }
1960 if (j == 0x0a) {
1961 goto u3p_cmd;
1962 }
1963 if (j == 0x0e) {
1964 goto try_u3;
1965 }
1966 continue;
1967u3p_out:
5d2a5a4f
OZ
1968 atp_writeb_io(dev, c, 0x18, 0x20);
1969 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0) {
1970 if ((atp_readb_io(dev, c, 0x1f) & 0x01) != 0)
1971 atp_writeb_io(dev, c, 0x19, 0);
1da177e4
LT
1972 cpu_relax();
1973 }
5d2a5a4f 1974 j = atp_readb_io(dev, c, 0x17) & 0x0f;
1da177e4
LT
1975 if (j == 0x0f) {
1976 goto u3p_in;
1977 }
1978 if (j == 0x0a) {
1979 goto u3p_cmd;
1980 }
1981 if (j == 0x0e) {
1982 goto u3p_out;
1983 }
1984 continue;
1985u3p_in:
5d2a5a4f
OZ
1986 atp_writeb_io(dev, c, 0x14, 0x09);
1987 atp_writeb_io(dev, c, 0x18, 0x20);
1da177e4
LT
1988 k = 0;
1989u3p_in1:
5d2a5a4f 1990 j = atp_readb_io(dev, c, 0x1f);
1da177e4 1991 if ((j & 0x01) != 0) {
5d2a5a4f 1992 mbuf[k++] = atp_readb_io(dev, c, 0x19);
1da177e4
LT
1993 goto u3p_in1;
1994 }
1995 if ((j & 0x80) == 0x00) {
1996 goto u3p_in1;
1997 }
5d2a5a4f 1998 j = atp_readb_io(dev, c, 0x17) & 0x0f;
1da177e4
LT
1999 if (j == 0x0f) {
2000 goto u3p_in;
2001 }
2002 if (j == 0x0a) {
2003 goto u3p_cmd;
2004 }
2005 if (j == 0x0e) {
2006 goto u3p_out;
2007 }
2008 continue;
2009u3p_cmd:
5d2a5a4f
OZ
2010 atp_writeb_io(dev, c, 0x10, 0x30);
2011 atp_writeb_io(dev, c, 0x14, 0x00);
2012 atp_writeb_io(dev, c, 0x18, 0x08);
80b52a7f 2013
5d2a5a4f 2014 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00);
80b52a7f 2015
5d2a5a4f 2016 j = atp_readb_io(dev, c, 0x17);
1da177e4
LT
2017 if (j != 0x16) {
2018 if (j == 0x4e) {
2019 goto u3p_out;
2020 }
2021 continue;
2022 }
2023 if (mbuf[0] != 0x01) {
2024 goto chg_wide;
2025 }
2026 if (mbuf[1] != 0x06) {
2027 goto chg_wide;
2028 }
2029 if (mbuf[2] != 0x04) {
2030 goto chg_wide;
2031 }
2032 if (mbuf[3] == 0x09) {
2033 m = 1;
2034 m = m << i;
2035 dev->wide_id[c] |= m;
2036 dev->id[c][i].devsp = 0xce;
2037#ifdef ED_DBGP
2038 printk("dev->id[%2d][%2d].devsp = %2x\n",c,i,dev->id[c][i].devsp);
2039#endif
2040 continue;
2041 }
2042chg_wide:
5d2a5a4f
OZ
2043 atp_writeb_io(dev, c, 0x1b, 0x01);
2044 atp_writeb_io(dev, c, 3, satn[0]);
2045 atp_writeb_io(dev, c, 4, satn[1]);
2046 atp_writeb_io(dev, c, 5, satn[2]);
2047 atp_writeb_io(dev, c, 6, satn[3]);
2048 atp_writeb_io(dev, c, 7, satn[4]);
2049 atp_writeb_io(dev, c, 8, satn[5]);
2050 atp_writeb_io(dev, c, 0x0f, 0);
2051 atp_writeb_io(dev, c, 0x11, dev->id[c][i].devsp);
2052 atp_writeb_io(dev, c, 0x12, 0);
2053 atp_writeb_io(dev, c, 0x13, satn[6]);
2054 atp_writeb_io(dev, c, 0x14, satn[7]);
2055 atp_writeb_io(dev, c, 0x18, satn[8]);
2056
2057 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00)
1da177e4 2058 cpu_relax();
80b52a7f
OZ
2059
2060 if (atp_readb_io(dev, c, 0x17) != 0x11 && atp_readb_io(dev, c, 0x17) != 0x8e)
1da177e4 2061 continue;
80b52a7f 2062
5d2a5a4f 2063 while (atp_readb_io(dev, c, 0x17) != 0x8e)
1da177e4 2064 cpu_relax();
80b52a7f 2065
1da177e4
LT
2066try_wide:
2067 j = 0;
5d2a5a4f
OZ
2068 atp_writeb_io(dev, c, 0x14, 0x05);
2069 atp_writeb_io(dev, c, 0x18, 0x20);
e2c22b45 2070
5d2a5a4f
OZ
2071 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0) {
2072 if ((atp_readb_io(dev, c, 0x1f) & 0x01) != 0)
2073 atp_writeb_io(dev, c, 0x19, wide[j++]);
1da177e4
LT
2074 cpu_relax();
2075 }
80b52a7f 2076
5d2a5a4f 2077 while ((atp_readb_io(dev, c, 0x17) & 0x80) == 0x00)
1da177e4 2078 cpu_relax();
80b52a7f 2079
5d2a5a4f 2080 j = atp_readb_io(dev, c, 0x17) & 0x0f;
1da177e4
LT
2081 if (j == 0x0f) {
2082 goto widep_in;
2083 }
2084 if (j == 0x0a) {
2085 goto widep_cmd;
2086 }
2087 if (j == 0x0e) {
2088 goto try_wide;
2089 }
2090 continue;
2091widep_out:
5d2a5a4f
OZ
2092 atp_writeb_io(dev, c, 0x18, 0x20);
2093 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0) {
2094 if ((atp_readb_io(dev, c, 0x1f) & 0x01) != 0)
2095 atp_writeb_io(dev, c, 0x19, 0);
1da177e4
LT
2096 cpu_relax();
2097 }
5d2a5a4f 2098 j = atp_readb_io(dev, c, 0x17) & 0x0f;
1da177e4
LT
2099 if (j == 0x0f) {
2100 goto widep_in;
2101 }
2102 if (j == 0x0a) {
2103 goto widep_cmd;
2104 }
2105 if (j == 0x0e) {
2106 goto widep_out;
2107 }
2108 continue;
2109widep_in:
5d2a5a4f
OZ
2110 atp_writeb_io(dev, c, 0x14, 0xff);
2111 atp_writeb_io(dev, c, 0x18, 0x20);
1da177e4
LT
2112 k = 0;
2113widep_in1:
5d2a5a4f 2114 j = atp_readb_io(dev, c, 0x1f);
1da177e4 2115 if ((j & 0x01) != 0) {
5d2a5a4f 2116 mbuf[k++] = atp_readb_io(dev, c, 0x19);
1da177e4
LT
2117 goto widep_in1;
2118 }
2119 if ((j & 0x80) == 0x00) {
2120 goto widep_in1;
2121 }
5d2a5a4f 2122 j = atp_readb_io(dev, c, 0x17) & 0x0f;
1da177e4
LT
2123 if (j == 0x0f) {
2124 goto widep_in;
2125 }
2126 if (j == 0x0a) {
2127 goto widep_cmd;
2128 }
2129 if (j == 0x0e) {
2130 goto widep_out;
2131 }
2132 continue;
2133widep_cmd:
5d2a5a4f
OZ
2134 atp_writeb_io(dev, c, 0x10, 0x30);
2135 atp_writeb_io(dev, c, 0x14, 0x00);
2136 atp_writeb_io(dev, c, 0x18, 0x08);
80b52a7f 2137
5d2a5a4f 2138 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00)
1da177e4 2139 cpu_relax();
80b52a7f 2140
5d2a5a4f 2141 j = atp_readb_io(dev, c, 0x17);
1da177e4
LT
2142 if (j != 0x16) {
2143 if (j == 0x4e) {
2144 goto widep_out;
2145 }
2146 continue;
2147 }
2148 if (mbuf[0] != 0x01) {
2149 goto not_wide;
2150 }
2151 if (mbuf[1] != 0x02) {
2152 goto not_wide;
2153 }
2154 if (mbuf[2] != 0x03) {
2155 goto not_wide;
2156 }
2157 if (mbuf[3] != 0x01) {
2158 goto not_wide;
2159 }
2160 m = 1;
2161 m = m << i;
2162 dev->wide_id[c] |= m;
2163not_wide:
80b52a7f 2164 if ((dev->id[c][i].devtype == 0x00) || (dev->id[c][i].devtype == 0x07) || ((dev->id[c][i].devtype == 0x05) && ((n & 0x10) != 0))) {
1da177e4
LT
2165 m = 1;
2166 m = m << i;
2167 if ((dev->async[c] & m) != 0) {
80b52a7f 2168 goto set_sync;
1da177e4
LT
2169 }
2170 }
2171 continue;
2172set_sync:
460da918 2173 if ((dev->dev_id != ATP885_DEVID && dev->dev_id != ATP880_DEVID1 && dev->dev_id != ATP880_DEVID2) || (dev->sp[c][i] == 0x02)) {
80b52a7f
OZ
2174 synu[4] = 0x0c;
2175 synuw[4] = 0x0c;
1da177e4 2176 } else {
80b52a7f
OZ
2177 if (dev->sp[c][i] >= 0x03) {
2178 synu[4] = 0x0a;
2179 synuw[4] = 0x0a;
2180 }
1da177e4 2181 }
1da177e4
LT
2182 j = 0;
2183 if ((m & dev->wide_id[c]) != 0) {
2184 j |= 0x01;
2185 }
5d2a5a4f
OZ
2186 atp_writeb_io(dev, c, 0x1b, j);
2187 atp_writeb_io(dev, c, 3, satn[0]);
2188 atp_writeb_io(dev, c, 4, satn[1]);
2189 atp_writeb_io(dev, c, 5, satn[2]);
2190 atp_writeb_io(dev, c, 6, satn[3]);
2191 atp_writeb_io(dev, c, 7, satn[4]);
2192 atp_writeb_io(dev, c, 8, satn[5]);
2193 atp_writeb_io(dev, c, 0x0f, 0);
2194 atp_writeb_io(dev, c, 0x11, dev->id[c][i].devsp);
2195 atp_writeb_io(dev, c, 0x12, 0);
2196 atp_writeb_io(dev, c, 0x13, satn[6]);
2197 atp_writeb_io(dev, c, 0x14, satn[7]);
2198 atp_writeb_io(dev, c, 0x18, satn[8]);
2199
2200 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00)
1da177e4 2201 cpu_relax();
80b52a7f
OZ
2202
2203 if (atp_readb_io(dev, c, 0x17) != 0x11 && atp_readb_io(dev, c, 0x17) != 0x8e)
1da177e4 2204 continue;
80b52a7f 2205
5d2a5a4f 2206 while (atp_readb_io(dev, c, 0x17) != 0x8e)
1da177e4 2207 cpu_relax();
80b52a7f 2208
1da177e4
LT
2209try_sync:
2210 j = 0;
5d2a5a4f
OZ
2211 atp_writeb_io(dev, c, 0x14, 0x06);
2212 atp_writeb_io(dev, c, 0x18, 0x20);
e2c22b45 2213
5d2a5a4f
OZ
2214 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0) {
2215 if ((atp_readb_io(dev, c, 0x1f) & 0x01) != 0) {
1da177e4 2216 if ((m & dev->wide_id[c]) != 0) {
460da918
OZ
2217 if (dev->dev_id == ATP885_DEVID || dev->dev_id == ATP880_DEVID1 || dev->dev_id == ATP880_DEVID2) {
2218 if ((m & dev->ultra_map[c]) != 0) {
2219 atp_writeb_io(dev, c, 0x19, synuw[j++]);
2220 } else {
2221 atp_writeb_io(dev, c, 0x19, synw[j++]);
2222 }
2223 } else
2224 atp_writeb_io(dev, c, 0x19, synw_870[j++]);
1da177e4
LT
2225 } else {
2226 if ((m & dev->ultra_map[c]) != 0) {
5d2a5a4f 2227 atp_writeb_io(dev, c, 0x19, synu[j++]);
1da177e4 2228 } else {
5d2a5a4f 2229 atp_writeb_io(dev, c, 0x19, synn[j++]);
1da177e4
LT
2230 }
2231 }
1da177e4
LT
2232 }
2233 }
80b52a7f 2234
5d2a5a4f 2235 while ((atp_readb_io(dev, c, 0x17) & 0x80) == 0x00)
1da177e4 2236 cpu_relax();
80b52a7f 2237
5d2a5a4f 2238 j = atp_readb_io(dev, c, 0x17) & 0x0f;
1da177e4
LT
2239 if (j == 0x0f) {
2240 goto phase_ins;
2241 }
2242 if (j == 0x0a) {
2243 goto phase_cmds;
2244 }
2245 if (j == 0x0e) {
2246 goto try_sync;
2247 }
2248 continue;
2249phase_outs:
5d2a5a4f
OZ
2250 atp_writeb_io(dev, c, 0x18, 0x20);
2251 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00) {
2252 if ((atp_readb_io(dev, c, 0x1f) & 0x01) != 0x00)
2253 atp_writeb_io(dev, c, 0x19, 0x00);
1da177e4
LT
2254 cpu_relax();
2255 }
5d2a5a4f 2256 j = atp_readb_io(dev, c, 0x17);
1da177e4
LT
2257 if (j == 0x85) {
2258 goto tar_dcons;
2259 }
2260 j &= 0x0f;
2261 if (j == 0x0f) {
2262 goto phase_ins;
2263 }
2264 if (j == 0x0a) {
2265 goto phase_cmds;
2266 }
2267 if (j == 0x0e) {
2268 goto phase_outs;
2269 }
2270 continue;
2271phase_ins:
460da918
OZ
2272 if (dev->dev_id == ATP885_DEVID || dev->dev_id == ATP880_DEVID1 || dev->dev_id == ATP880_DEVID2)
2273 atp_writeb_io(dev, c, 0x14, 0x06);
2274 else
2275 atp_writeb_io(dev, c, 0x14, 0xff);
5d2a5a4f 2276 atp_writeb_io(dev, c, 0x18, 0x20);
1da177e4
LT
2277 k = 0;
2278phase_ins1:
5d2a5a4f 2279 j = atp_readb_io(dev, c, 0x1f);
1da177e4 2280 if ((j & 0x01) != 0x00) {
5d2a5a4f 2281 mbuf[k++] = atp_readb_io(dev, c, 0x19);
1da177e4
LT
2282 goto phase_ins1;
2283 }
2284 if ((j & 0x80) == 0x00) {
2285 goto phase_ins1;
2286 }
80b52a7f 2287
5d2a5a4f 2288 while ((atp_readb_io(dev, c, 0x17) & 0x80) == 0x00);
80b52a7f 2289
5d2a5a4f 2290 j = atp_readb_io(dev, c, 0x17);
1da177e4
LT
2291 if (j == 0x85) {
2292 goto tar_dcons;
2293 }
2294 j &= 0x0f;
2295 if (j == 0x0f) {
2296 goto phase_ins;
2297 }
2298 if (j == 0x0a) {
2299 goto phase_cmds;
2300 }
2301 if (j == 0x0e) {
2302 goto phase_outs;
2303 }
2304 continue;
2305phase_cmds:
5d2a5a4f 2306 atp_writeb_io(dev, c, 0x10, 0x30);
1da177e4 2307tar_dcons:
5d2a5a4f
OZ
2308 atp_writeb_io(dev, c, 0x14, 0x00);
2309 atp_writeb_io(dev, c, 0x18, 0x08);
80b52a7f 2310
5d2a5a4f 2311 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00)
1da177e4 2312 cpu_relax();
80b52a7f 2313
5d2a5a4f 2314 j = atp_readb_io(dev, c, 0x17);
1da177e4
LT
2315 if (j != 0x16) {
2316 continue;
2317 }
2318 if (mbuf[0] != 0x01) {
2319 continue;
2320 }
2321 if (mbuf[1] != 0x03) {
2322 continue;
2323 }
2324 if (mbuf[4] == 0x00) {
2325 continue;
2326 }
2327 if (mbuf[3] > 0x64) {
2328 continue;
2329 }
460da918
OZ
2330 if (dev->dev_id == ATP885_DEVID || dev->dev_id == ATP880_DEVID1 || dev->dev_id == ATP880_DEVID2) {
2331 if (mbuf[4] > 0x0e) {
2332 mbuf[4] = 0x0e;
2333 }
2334 } else {
2335 if (mbuf[4] > 0x0c) {
2336 mbuf[4] = 0x0c;
2337 }
1da177e4
LT
2338 }
2339 dev->id[c][i].devsp = mbuf[4];
460da918
OZ
2340 if (dev->dev_id == ATP885_DEVID || dev->dev_id == ATP880_DEVID1 || dev->dev_id == ATP880_DEVID2)
2341 if (mbuf[3] < 0x0c) {
2342 j = 0xb0;
2343 goto set_syn_ok;
2344 }
1da177e4
LT
2345 if ((mbuf[3] < 0x0d) && (rmb == 0)) {
2346 j = 0xa0;
2347 goto set_syn_ok;
2348 }
2349 if (mbuf[3] < 0x1a) {
2350 j = 0x20;
2351 goto set_syn_ok;
2352 }
2353 if (mbuf[3] < 0x33) {
2354 j = 0x40;
2355 goto set_syn_ok;
2356 }
2357 if (mbuf[3] < 0x4c) {
2358 j = 0x50;
2359 goto set_syn_ok;
2360 }
2361 j = 0x60;
80b52a7f 2362set_syn_ok:
1da177e4 2363 dev->id[c][i].devsp = (dev->id[c][i].devsp & 0x0f) | j;
80b52a7f 2364#ifdef ED_DBGP
1da177e4
LT
2365 printk("dev->id[%2d][%2d].devsp = %2x\n",c,i,dev->id[c][i].devsp);
2366#endif
2367 }
1da177e4 2368}