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