atp870u: Simplify _probe()
[deliverable/linux.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
dd5a5f79 957static void tscam(struct Scsi_Host *host, bool wide_chip)
1da177e4
LT
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;
dd5a5f79 986 if (!wide_chip) {
1da177e4
LT
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 1014 atp_writeb_io(dev, 0, 0x15, k);
dd5a5f79 1015 if (wide_chip)
6a3cebb6
OZ
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;
6c9b9c55 1248 unsigned int 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
b1e85063
OZ
1255 if (ent->device == PCI_DEVICE_ID_ARTOP_AEC7610 && pdev->revision < 2) {
1256 dev_err(&pdev->dev, "ATP850S chips (AEC6710L/F cards) are not supported.\n");
1257 return -ENODEV;
1258 }
1259
bdd5ac40
OZ
1260 err = pci_enable_device(pdev);
1261 if (err)
1262 goto fail;
1da177e4 1263
34a2c35d 1264 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32))) {
1da177e4 1265 printk(KERN_ERR "atp870u: DMA mask required but not available.\n");
bdd5ac40
OZ
1266 err = -EIO;
1267 goto disable_device;
1da177e4
LT
1268 }
1269
bdd5ac40
OZ
1270 err = -ENOMEM;
1271 shpnt = scsi_host_alloc(&atp870u_template, sizeof(struct atp_unit));
1272 if (!shpnt)
1273 goto disable_device;
1274
1275 atpdev = shost_priv(shpnt);
1276
1277 atpdev->host = shpnt;
1278 atpdev->pdev = pdev;
1279 pci_set_drvdata(pdev, atpdev);
1280
6c9b9c55
OZ
1281 shpnt->io_port = pci_resource_start(pdev, 0);
1282 shpnt->io_port &= 0xfffffff8;
1283 shpnt->n_io_port = pci_resource_len(pdev, 0);
1284 atpdev->baseport = shpnt->io_port;
1285 shpnt->unique_id = shpnt->io_port;
1286 shpnt->irq = pdev->irq;
dc6a78f1 1287
1da177e4 1288 if ((ent->device == ATP880_DEVID1)||(ent->device == ATP880_DEVID2)) {
1da177e4
LT
1289 pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0x80);//JCC082803
1290
6c9b9c55
OZ
1291 atpdev->ioport[0] = shpnt->io_port + 0x40;
1292 atpdev->pciport[0] = shpnt->io_port + 0x28;
d804bb25
OZ
1293
1294 host_id = atp_readb_base(atpdev, 0x39);
1da177e4
LT
1295 host_id >>= 0x04;
1296
c48442d1 1297 printk(KERN_INFO " ACARD AEC-67160 PCI Ultra3 LVD Host Adapter:"
6c9b9c55 1298 " IO:%lx, IRQ:%d.\n", shpnt->io_port, shpnt->irq);
dc6a78f1
RD
1299 atpdev->dev_id = ent->device;
1300 atpdev->host_id[0] = host_id;
1da177e4 1301
d804bb25
OZ
1302 atpdev->scam_on = atp_readb_base(atpdev, 0x22);
1303 atpdev->global_map[0] = atp_readb_base(atpdev, 0x35);
1304 atpdev->ultra_map[0] = atp_readw_base(atpdev, 0x3c);
1da177e4
LT
1305
1306 n = 0x3f09;
1307next_fblk_880:
1308 if (n >= 0x4000)
1309 goto flash_ok_880;
1310
1311 m = 0;
d804bb25 1312 atp_writew_base(atpdev, 0x34, n);
1da177e4 1313 n += 0x0002;
d804bb25 1314 if (atp_readb_base(atpdev, 0x30) == 0xff)
1da177e4
LT
1315 goto flash_ok_880;
1316
d804bb25
OZ
1317 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x30);
1318 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x31);
1319 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x32);
1320 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x33);
1321 atp_writew_base(atpdev, 0x34, n);
1da177e4 1322 n += 0x0002;
d804bb25
OZ
1323 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x30);
1324 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x31);
1325 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x32);
1326 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x33);
1327 atp_writew_base(atpdev, 0x34, n);
1da177e4 1328 n += 0x0002;
d804bb25
OZ
1329 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x30);
1330 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x31);
1331 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x32);
1332 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x33);
1333 atp_writew_base(atpdev, 0x34, n);
1da177e4 1334 n += 0x0002;
d804bb25
OZ
1335 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x30);
1336 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x31);
1337 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x32);
1338 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x33);
1da177e4
LT
1339 n += 0x0018;
1340 goto next_fblk_880;
1341flash_ok_880:
d804bb25 1342 atp_writew_base(atpdev, 0x34, 0);
dc6a78f1
RD
1343 atpdev->ultra_map[0] = 0;
1344 atpdev->async[0] = 0;
1da177e4
LT
1345 for (k = 0; k < 16; k++) {
1346 n = 1;
1347 n = n << k;
dc6a78f1
RD
1348 if (atpdev->sp[0][k] > 1) {
1349 atpdev->ultra_map[0] |= n;
1da177e4 1350 } else {
dc6a78f1
RD
1351 if (atpdev->sp[0][k] == 0)
1352 atpdev->async[0] |= n;
1da177e4
LT
1353 }
1354 }
dc6a78f1 1355 atpdev->async[0] = ~(atpdev->async[0]);
d804bb25 1356 atp_writeb_base(atpdev, 0x35, atpdev->global_map[0]);
1da177e4 1357
1da177e4
LT
1358 if (atp870u_init_tables(shpnt) < 0) {
1359 printk(KERN_ERR "Unable to allocate tables for Acard controller\n");
bdd5ac40 1360 err = -ENOMEM;
1da177e4
LT
1361 goto unregister;
1362 }
1363
6c9b9c55 1364 err = request_irq(shpnt->irq, atp870u_intr_handle, IRQF_SHARED, "atp880i", shpnt);
bdd5ac40 1365 if (err) {
6c9b9c55 1366 printk(KERN_ERR "Unable to allocate IRQ%d for Acard controller.\n", shpnt->irq);
1da177e4
LT
1367 goto free_tables;
1368 }
1369
1370 spin_lock_irqsave(shpnt->host_lock, flags);
bdd5ac40
OZ
1371 k = atp_readb_base(atpdev, 0x38) & 0x80;
1372 atp_writeb_base(atpdev, 0x38, k);
1373 atp_writeb_base(atpdev, 0x3b, 0x20);
1da177e4 1374 mdelay(32);
bdd5ac40 1375 atp_writeb_base(atpdev, 0x3b, 0);
1da177e4 1376 mdelay(32);
bdd5ac40
OZ
1377 atp_readb_io(atpdev, 0, 0x1b);
1378 atp_readb_io(atpdev, 0, 0x17);
6a1961bc 1379
bdd5ac40 1380 atp_set_host_id(atpdev, 0, host_id);
1da177e4 1381
dd5a5f79 1382 tscam(shpnt, true);
bdd5ac40
OZ
1383 atp_is(atpdev, 0, true, atp_readb_base(atpdev, 0x3f) & 0x40);
1384 atp_writeb_base(atpdev, 0x38, 0xb0);
1da177e4
LT
1385 shpnt->max_id = 16;
1386 shpnt->this_id = host_id;
1da177e4 1387 } else if (ent->device == ATP885_DEVID) {
6c9b9c55
OZ
1388 printk(KERN_INFO " ACARD AEC-67162 PCI Ultra3 LVD Host Adapter: IO:%lx, IRQ:%d.\n"
1389 , shpnt->io_port, shpnt->irq);
1da177e4 1390
dc6a78f1
RD
1391 atpdev->pdev = pdev;
1392 atpdev->dev_id = ent->device;
6c9b9c55
OZ
1393 atpdev->ioport[0] = shpnt->io_port + 0x80;
1394 atpdev->ioport[1] = shpnt->io_port + 0xc0;
1395 atpdev->pciport[0] = shpnt->io_port + 0x40;
1396 atpdev->pciport[1] = shpnt->io_port + 0x50;
1da177e4 1397
bdd5ac40
OZ
1398 if (atp870u_init_tables(shpnt) < 0) {
1399 err = -ENOMEM;
1da177e4 1400 goto unregister;
bdd5ac40 1401 }
1da177e4
LT
1402
1403#ifdef ED_DBGP
bdd5ac40 1404 printk("request_irq() shpnt %p hostdata %p\n", shpnt, atpdev);
1da177e4 1405#endif
6c9b9c55 1406 err = request_irq(shpnt->irq, atp870u_intr_handle, IRQF_SHARED, "atp870u", shpnt);
bdd5ac40 1407 if (err) {
1da177e4
LT
1408 printk(KERN_ERR "Unable to allocate IRQ for Acard controller.\n");
1409 goto free_tables;
1410 }
1411
1412 spin_lock_irqsave(shpnt->host_lock, flags);
1413
bdd5ac40
OZ
1414 c = atp_readb_base(atpdev, 0x29);
1415 atp_writeb_base(atpdev, 0x29, c | 0x04);
1da177e4
LT
1416
1417 n=0x1f80;
1418next_fblk_885:
1419 if (n >= 0x2000) {
1420 goto flash_ok_885;
1421 }
bdd5ac40
OZ
1422 atp_writew_base(atpdev, 0x3c, n);
1423 if (atp_readl_base(atpdev, 0x38) == 0xffffffff) {
1da177e4
LT
1424 goto flash_ok_885;
1425 }
1426 for (m=0; m < 2; m++) {
bdd5ac40 1427 atpdev->global_map[m]= 0;
1da177e4 1428 for (k=0; k < 4; k++) {
bdd5ac40
OZ
1429 atp_writew_base(atpdev, 0x3c, n++);
1430 ((unsigned long *)&setupdata[m][0])[k] = atp_readl_base(atpdev, 0x38);
1da177e4
LT
1431 }
1432 for (k=0; k < 4; k++) {
bdd5ac40
OZ
1433 atp_writew_base(atpdev, 0x3c, n++);
1434 ((unsigned long *)&atpdev->sp[m][0])[k] = atp_readl_base(atpdev, 0x38);
1da177e4
LT
1435 }
1436 n += 8;
1437 }
1438 goto next_fblk_885;
1439flash_ok_885:
1440#ifdef ED_DBGP
1441 printk( "Flash Read OK\n");
1442#endif
bdd5ac40
OZ
1443 c = atp_readb_base(atpdev, 0x29);
1444 atp_writeb_base(atpdev, 0x29, c & 0xfb);
1da177e4 1445 for (c=0;c < 2;c++) {
bdd5ac40
OZ
1446 atpdev->ultra_map[c]=0;
1447 atpdev->async[c] = 0;
1da177e4
LT
1448 for (k=0; k < 16; k++) {
1449 n=1;
1450 n = n << k;
bdd5ac40
OZ
1451 if (atpdev->sp[c][k] > 1) {
1452 atpdev->ultra_map[c] |= n;
1da177e4 1453 } else {
bdd5ac40
OZ
1454 if (atpdev->sp[c][k] == 0) {
1455 atpdev->async[c] |= n;
1da177e4
LT
1456 }
1457 }
1458 }
bdd5ac40 1459 atpdev->async[c] = ~(atpdev->async[c]);
1da177e4 1460
bdd5ac40 1461 if (atpdev->global_map[c] == 0) {
1da177e4
LT
1462 k=setupdata[c][1];
1463 if ((k & 0x40) != 0)
bdd5ac40 1464 atpdev->global_map[c] |= 0x20;
1da177e4 1465 k &= 0x07;
bdd5ac40 1466 atpdev->global_map[c] |= k;
1da177e4 1467 if ((setupdata[c][2] & 0x04) != 0)
bdd5ac40
OZ
1468 atpdev->global_map[c] |= 0x08;
1469 atpdev->host_id[c] = setupdata[c][0] & 0x07;
1da177e4
LT
1470 }
1471 }
1472
bdd5ac40 1473 k = atp_readb_base(atpdev, 0x28) & 0x8f;
1da177e4 1474 k |= 0x10;
bdd5ac40
OZ
1475 atp_writeb_base(atpdev, 0x28, k);
1476 atp_writeb_pci(atpdev, 0, 1, 0x80);
1477 atp_writeb_pci(atpdev, 1, 1, 0x80);
1da177e4 1478 mdelay(100);
bdd5ac40
OZ
1479 atp_writeb_pci(atpdev, 0, 1, 0);
1480 atp_writeb_pci(atpdev, 1, 1, 0);
1da177e4 1481 mdelay(1000);
bdd5ac40
OZ
1482 atp_readb_io(atpdev, 0, 0x1b);
1483 atp_readb_io(atpdev, 0, 0x17);
1484 atp_readb_io(atpdev, 1, 0x1b);
1485 atp_readb_io(atpdev, 1, 0x17);
6a1961bc 1486
bdd5ac40 1487 k=atpdev->host_id[0];
1da177e4
LT
1488 if (k > 7)
1489 k = (k & 0x07) | 0x40;
bdd5ac40 1490 atp_set_host_id(atpdev, 0, k);
493c5201 1491
bdd5ac40 1492 k=atpdev->host_id[1];
1da177e4
LT
1493 if (k > 7)
1494 k = (k & 0x07) | 0x40;
bdd5ac40 1495 atp_set_host_id(atpdev, 1, k);
1da177e4 1496
c4ad92bc 1497 mdelay(600); /* this delay used to be called tscam_885() */
1da177e4 1498 printk(KERN_INFO " Scanning Channel A SCSI Device ...\n");
bdd5ac40
OZ
1499 atp_is(atpdev, 0, true, atp_readb_io(atpdev, 0, 0x1b) >> 7);
1500 atp_writeb_io(atpdev, 0, 0x16, 0x80);
1da177e4 1501 printk(KERN_INFO " Scanning Channel B SCSI Device ...\n");
bdd5ac40
OZ
1502 atp_is(atpdev, 1, true, atp_readb_io(atpdev, 1, 0x1b) >> 7);
1503 atp_writeb_io(atpdev, 1, 0x16, 0x80);
1504 k = atp_readb_base(atpdev, 0x28) & 0xcf;
1da177e4 1505 k |= 0xc0;
bdd5ac40
OZ
1506 atp_writeb_base(atpdev, 0x28, k);
1507 k = atp_readb_base(atpdev, 0x1f) | 0x80;
1508 atp_writeb_base(atpdev, 0x1f, k);
1509 k = atp_readb_base(atpdev, 0x29) | 0x01;
1510 atp_writeb_base(atpdev, 0x29, k);
1da177e4
LT
1511#ifdef ED_DBGP
1512 //printk("atp885: atp_host[0] 0x%p\n", atp_host[0]);
1513#endif
1514 shpnt->max_id = 16;
bdd5ac40 1515 shpnt->max_lun = (atpdev->global_map[0] & 0x07) + 1;
1da177e4 1516 shpnt->max_channel = 1;
bdd5ac40 1517 shpnt->this_id = atpdev->host_id[0];
1da177e4 1518 } else {
dd5a5f79
OZ
1519 bool wide_chip =
1520 (ent->device == PCI_DEVICE_ID_ARTOP_AEC7610 &&
1521 pdev->revision == 4) ||
1522 (ent->device == PCI_DEVICE_ID_ARTOP_AEC7612UW) ||
1523 (ent->device == PCI_DEVICE_ID_ARTOP_AEC7612SUW);
1da177e4
LT
1524 error = pci_read_config_byte(pdev, 0x49, &host_id);
1525
c48442d1 1526 printk(KERN_INFO " ACARD AEC-671X PCI Ultra/W SCSI-2/3 Host Adapter: "
6c9b9c55 1527 "IO:%lx, IRQ:%d.\n", shpnt->io_port, shpnt->irq);
1da177e4 1528
6c9b9c55
OZ
1529 atpdev->ioport[0] = shpnt->io_port;
1530 atpdev->pciport[0] = shpnt->io_port + 0x20;
dc6a78f1 1531 atpdev->dev_id = ent->device;
1da177e4 1532 host_id &= 0x07;
dc6a78f1 1533 atpdev->host_id[0] = host_id;
d804bb25
OZ
1534 atpdev->scam_on = atp_readb_pci(atpdev, 0, 2);
1535 atpdev->global_map[0] = atp_readb_base(atpdev, 0x2d);
1536 atpdev->ultra_map[0] = atp_readw_base(atpdev, 0x2e);
1da177e4 1537
dc6a78f1
RD
1538 if (atpdev->ultra_map[0] == 0) {
1539 atpdev->scam_on = 0x00;
1540 atpdev->global_map[0] = 0x20;
1541 atpdev->ultra_map[0] = 0xffff;
1da177e4
LT
1542 }
1543
1da177e4 1544
bdd5ac40
OZ
1545 if (atp870u_init_tables(shpnt) < 0) {
1546 err = -ENOMEM;
1da177e4 1547 goto unregister;
bdd5ac40 1548 }
1da177e4 1549
6c9b9c55 1550 err = request_irq(shpnt->irq, atp870u_intr_handle, IRQF_SHARED, "atp870i", shpnt);
bdd5ac40 1551 if (err) {
6c9b9c55 1552 printk(KERN_ERR "Unable to allocate IRQ%d for Acard controller.\n", shpnt->irq);
1da177e4
LT
1553 goto free_tables;
1554 }
1555
1556 spin_lock_irqsave(shpnt->host_lock, flags);
dd5a5f79 1557 if (pdev->revision > 0x07) /* check if atp876 chip then enable terminator */
bdd5ac40 1558 atp_writeb_base(atpdev, 0x3e, 0x00);
1da177e4 1559
bdd5ac40
OZ
1560 k = (atp_readb_base(atpdev, 0x3a) & 0xf3) | 0x10;
1561 atp_writeb_base(atpdev, 0x3a, k);
1562 atp_writeb_base(atpdev, 0x3a, k & 0xdf);
1da177e4 1563 mdelay(32);
bdd5ac40 1564 atp_writeb_base(atpdev, 0x3a, k);
1da177e4 1565 mdelay(32);
bdd5ac40 1566 atp_set_host_id(atpdev, 0, host_id);
1da177e4 1567
dd5a5f79
OZ
1568
1569 tscam(shpnt, wide_chip);
bdd5ac40 1570 atp_writeb_base(atpdev, 0x3a, atp_readb_base(atpdev, 0x3a) | 0x10);
dd5a5f79 1571 atp_is(atpdev, 0, wide_chip, 0);
bdd5ac40
OZ
1572 atp_writeb_base(atpdev, 0x3a, atp_readb_base(atpdev, 0x3a) & 0xef);
1573 atp_writeb_base(atpdev, 0x3b, atp_readb_base(atpdev, 0x3b) | 0x20);
dd5a5f79 1574 shpnt->max_id = wide_chip ? 16 : 8;
1da177e4 1575 shpnt->this_id = host_id;
6c9b9c55 1576
1da177e4
LT
1577 }
1578 spin_unlock_irqrestore(shpnt->host_lock, flags);
6c9b9c55 1579 if (!request_region(shpnt->io_port, shpnt->n_io_port, "atp870u")) {
bdd5ac40 1580 err = -EBUSY;
30ebc7ef 1581 goto request_io_fail;
bdd5ac40
OZ
1582 }
1583 err = scsi_add_host(shpnt, &pdev->dev);
1584 if (err)
1da177e4
LT
1585 goto scsi_add_fail;
1586 scsi_scan_host(shpnt);
1587#ifdef ED_DBGP
1588 printk("atp870u_prob : exit\n");
1589#endif
1590 return 0;
1591
1592scsi_add_fail:
1593 printk("atp870u_prob:scsi_add_fail\n");
6c9b9c55 1594 release_region(shpnt->io_port, shpnt->n_io_port);
1da177e4
LT
1595request_io_fail:
1596 printk("atp870u_prob:request_io_fail\n");
6c9b9c55 1597 free_irq(shpnt->irq, shpnt);
1da177e4
LT
1598free_tables:
1599 printk("atp870u_prob:free_table\n");
1600 atp870u_free_tables(shpnt);
1601unregister:
1da177e4 1602 scsi_host_put(shpnt);
bdd5ac40
OZ
1603disable_device:
1604 pci_disable_device(pdev);
1605fail:
1606 return err;
1da177e4
LT
1607}
1608
1609/* The abort command does not leave the device in a clean state where
1610 it is available to be used again. Until this gets worked out, we will
1611 leave it commented out. */
1612
1613static int atp870u_abort(struct scsi_cmnd * SCpnt)
1614{
1615 unsigned char j, k, c;
1616 struct scsi_cmnd *workrequ;
1da177e4
LT
1617 struct atp_unit *dev;
1618 struct Scsi_Host *host;
1619 host = SCpnt->device->host;
1620
1621 dev = (struct atp_unit *)&host->hostdata;
422c0d61 1622 c = scmd_channel(SCpnt);
1da177e4
LT
1623 printk(" atp870u: abort Channel = %x \n", c);
1624 printk("working=%x last_cmd=%x ", dev->working[c], dev->last_cmd[c]);
1625 printk(" quhdu=%x quendu=%x ", dev->quhd[c], dev->quend[c]);
1da177e4 1626 for (j = 0; j < 0x18; j++) {
6a3cebb6 1627 printk(" r%2x=%2x", j, atp_readb_io(dev, c, j));
1da177e4 1628 }
6a3cebb6
OZ
1629 printk(" r1c=%2x", atp_readb_io(dev, c, 0x1c));
1630 printk(" r1f=%2x in_snd=%2x ", atp_readb_io(dev, c, 0x1f), dev->in_snd[c]);
1631 printk(" d00=%2x", atp_readb_pci(dev, c, 0x00));
1632 printk(" d02=%2x", atp_readb_pci(dev, c, 0x02));
1da177e4
LT
1633 for(j=0;j<16;j++) {
1634 if (dev->id[c][j].curr_req != NULL) {
1635 workrequ = dev->id[c][j].curr_req;
1636 printk("\n que cdb= ");
1637 for (k=0; k < workrequ->cmd_len; k++) {
1638 printk(" %2x ",workrequ->cmnd[k]);
1639 }
1640 printk(" last_lenu= %x ",(unsigned int)dev->id[c][j].last_len);
1641 }
1642 }
1643 return SUCCESS;
1644}
1645
1646static const char *atp870u_info(struct Scsi_Host *notused)
1647{
1648 static char buffer[128];
1649
1650 strcpy(buffer, "ACARD AEC-6710/6712/67160 PCI Ultra/W/LVD SCSI-3 Adapter Driver V2.6+ac ");
1651
1652 return buffer;
1653}
1654
d773e422 1655static int atp870u_show_info(struct seq_file *m, struct Scsi_Host *HBAptr)
1da177e4 1656{
3d30079c
RV
1657 seq_puts(m, "ACARD AEC-671X Driver Version: 2.6+ac\n\n"
1658 "Adapter Configuration:\n");
d773e422
AV
1659 seq_printf(m, " Base IO: %#.4lx\n", HBAptr->io_port);
1660 seq_printf(m, " IRQ: %d\n", HBAptr->irq);
1661 return 0;
1da177e4
LT
1662}
1663
1664
1665static int atp870u_biosparam(struct scsi_device *disk, struct block_device *dev,
1666 sector_t capacity, int *ip)
1667{
1668 int heads, sectors, cylinders;
1669
1670 heads = 64;
1671 sectors = 32;
1672 cylinders = (unsigned long)capacity / (heads * sectors);
1673 if (cylinders > 1024) {
1674 heads = 255;
1675 sectors = 63;
1676 cylinders = (unsigned long)capacity / (heads * sectors);
1677 }
1678 ip[0] = heads;
1679 ip[1] = sectors;
1680 ip[2] = cylinders;
1681
1682 return 0;
1683}
1684
1685static void atp870u_remove (struct pci_dev *pdev)
1686{
1687 struct atp_unit *devext = pci_get_drvdata(pdev);
1688 struct Scsi_Host *pshost = devext->host;
1689
1690
1691 scsi_remove_host(pshost);
1da177e4
LT
1692 free_irq(pshost->irq, pshost);
1693 release_region(pshost->io_port, pshost->n_io_port);
1da177e4 1694 atp870u_free_tables(pshost);
1da177e4 1695 scsi_host_put(pshost);
1da177e4
LT
1696}
1697MODULE_LICENSE("GPL");
1698
1699static struct scsi_host_template atp870u_template = {
1700 .module = THIS_MODULE,
1701 .name = "atp870u" /* name */,
1702 .proc_name = "atp870u",
d773e422 1703 .show_info = atp870u_show_info,
1da177e4
LT
1704 .info = atp870u_info /* info */,
1705 .queuecommand = atp870u_queuecommand /* queuecommand */,
1706 .eh_abort_handler = atp870u_abort /* abort */,
1707 .bios_param = atp870u_biosparam /* biosparm */,
1708 .can_queue = qcnt /* can_queue */,
1709 .this_id = 7 /* SCSI ID */,
1710 .sg_tablesize = ATP870U_SCATTER /*SG_ALL*/ /*SG_NONE*/,
1da177e4
LT
1711 .use_clustering = ENABLE_CLUSTERING,
1712 .max_sectors = ATP870U_MAX_SECTORS,
1713};
1714
1715static struct pci_device_id atp870u_id_table[] = {
1716 { PCI_DEVICE(PCI_VENDOR_ID_ARTOP, ATP885_DEVID) },
1717 { PCI_DEVICE(PCI_VENDOR_ID_ARTOP, ATP880_DEVID1) },
1718 { PCI_DEVICE(PCI_VENDOR_ID_ARTOP, ATP880_DEVID2) },
1719 { PCI_DEVICE(PCI_VENDOR_ID_ARTOP, PCI_DEVICE_ID_ARTOP_AEC7610) },
1720 { PCI_DEVICE(PCI_VENDOR_ID_ARTOP, PCI_DEVICE_ID_ARTOP_AEC7612UW) },
1721 { PCI_DEVICE(PCI_VENDOR_ID_ARTOP, PCI_DEVICE_ID_ARTOP_AEC7612U) },
1722 { PCI_DEVICE(PCI_VENDOR_ID_ARTOP, PCI_DEVICE_ID_ARTOP_AEC7612S) },
1723 { PCI_DEVICE(PCI_VENDOR_ID_ARTOP, PCI_DEVICE_ID_ARTOP_AEC7612D) },
1724 { PCI_DEVICE(PCI_VENDOR_ID_ARTOP, PCI_DEVICE_ID_ARTOP_AEC7612SUW) },
1725 { PCI_DEVICE(PCI_VENDOR_ID_ARTOP, PCI_DEVICE_ID_ARTOP_8060) },
1726 { 0, },
1727};
1728
1729MODULE_DEVICE_TABLE(pci, atp870u_id_table);
1730
1731static struct pci_driver atp870u_driver = {
1732 .id_table = atp870u_id_table,
1733 .name = "atp870u",
1734 .probe = atp870u_probe,
6f039790 1735 .remove = atp870u_remove,
1da177e4
LT
1736};
1737
1ccd7d68 1738module_pci_driver(atp870u_driver);
1da177e4 1739
4192a40f 1740static void atp_is(struct atp_unit *dev, unsigned char c, bool wide_chip, unsigned char lvdmode)
1da177e4 1741{
fa50b308 1742 unsigned char i, j, k, rmb, n;
1da177e4
LT
1743 unsigned short int m;
1744 static unsigned char mbuf[512];
80b52a7f
OZ
1745 static unsigned char satn[9] = { 0, 0, 0, 0, 0, 0, 0, 6, 6 };
1746 static unsigned char inqd[9] = { 0x12, 0, 0, 0, 0x24, 0, 0, 0x24, 6 };
1747 static unsigned char synn[6] = { 0x80, 1, 3, 1, 0x19, 0x0e };
1748 unsigned char synu[6] = { 0x80, 1, 3, 1, 0x0a, 0x0e };
1749 static unsigned char synw[6] = { 0x80, 1, 3, 1, 0x19, 0x0e };
460da918 1750 static unsigned char synw_870[6] = { 0x80, 1, 3, 1, 0x0c, 0x07 };
80b52a7f
OZ
1751 unsigned char synuw[6] = { 0x80, 1, 3, 1, 0x0a, 0x0e };
1752 static unsigned char wide[6] = { 0x80, 1, 2, 3, 1, 0 };
1753 static unsigned char u3[9] = { 0x80, 1, 6, 4, 0x09, 00, 0x0e, 0x01, 0x02 };
1da177e4 1754
1da177e4 1755 for (i = 0; i < 16; i++) {
197fb8d8
OZ
1756 if (!wide_chip && (i > 7))
1757 break;
1da177e4
LT
1758 m = 1;
1759 m = m << i;
1760 if ((m & dev->active_id[c]) != 0) {
1761 continue;
1762 }
1763 if (i == dev->host_id[c]) {
1764 printk(KERN_INFO " ID: %2d Host Adapter\n", dev->host_id[c]);
1765 continue;
1766 }
197fb8d8 1767 atp_writeb_io(dev, c, 0x1b, wide_chip ? 0x01 : 0x00);
5d2a5a4f
OZ
1768 atp_writeb_io(dev, c, 1, 0x08);
1769 atp_writeb_io(dev, c, 2, 0x7f);
1770 atp_writeb_io(dev, c, 3, satn[0]);
1771 atp_writeb_io(dev, c, 4, satn[1]);
1772 atp_writeb_io(dev, c, 5, satn[2]);
1773 atp_writeb_io(dev, c, 6, satn[3]);
1774 atp_writeb_io(dev, c, 7, satn[4]);
1775 atp_writeb_io(dev, c, 8, satn[5]);
1776 atp_writeb_io(dev, c, 0x0f, 0);
1777 atp_writeb_io(dev, c, 0x11, dev->id[c][i].devsp);
5d2a5a4f
OZ
1778 atp_writeb_io(dev, c, 0x12, 0);
1779 atp_writeb_io(dev, c, 0x13, satn[6]);
1780 atp_writeb_io(dev, c, 0x14, satn[7]);
1da177e4
LT
1781 j = i;
1782 if ((j & 0x08) != 0) {
1783 j = (j & 0x07) | 0x40;
1784 }
5d2a5a4f
OZ
1785 atp_writeb_io(dev, c, 0x15, j);
1786 atp_writeb_io(dev, c, 0x18, satn[8]);
1da177e4 1787
5d2a5a4f 1788 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00)
1da177e4 1789 cpu_relax();
80b52a7f
OZ
1790
1791 if (atp_readb_io(dev, c, 0x17) != 0x11 && atp_readb_io(dev, c, 0x17) != 0x8e)
1da177e4 1792 continue;
80b52a7f 1793
5d2a5a4f 1794 while (atp_readb_io(dev, c, 0x17) != 0x8e)
1da177e4 1795 cpu_relax();
80b52a7f 1796
1da177e4
LT
1797 dev->active_id[c] |= m;
1798
5d2a5a4f 1799 atp_writeb_io(dev, c, 0x10, 0x30);
460da918
OZ
1800 if (dev->dev_id == ATP885_DEVID || dev->dev_id == ATP880_DEVID1 || dev->dev_id == ATP880_DEVID2)
1801 atp_writeb_io(dev, c, 0x14, 0x00);
1802 else /* result of is870() merge - is this a bug? */
1803 atp_writeb_io(dev, c, 0x04, 0x00);
1da177e4
LT
1804
1805phase_cmd:
5d2a5a4f 1806 atp_writeb_io(dev, c, 0x18, 0x08);
80b52a7f 1807
5d2a5a4f 1808 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00)
1da177e4 1809 cpu_relax();
80b52a7f 1810
5d2a5a4f 1811 j = atp_readb_io(dev, c, 0x17);
1da177e4 1812 if (j != 0x16) {
5d2a5a4f 1813 atp_writeb_io(dev, c, 0x10, 0x41);
1da177e4
LT
1814 goto phase_cmd;
1815 }
1816sel_ok:
5d2a5a4f
OZ
1817 atp_writeb_io(dev, c, 3, inqd[0]);
1818 atp_writeb_io(dev, c, 4, inqd[1]);
1819 atp_writeb_io(dev, c, 5, inqd[2]);
1820 atp_writeb_io(dev, c, 6, inqd[3]);
1821 atp_writeb_io(dev, c, 7, inqd[4]);
1822 atp_writeb_io(dev, c, 8, inqd[5]);
1823 atp_writeb_io(dev, c, 0x0f, 0);
1824 atp_writeb_io(dev, c, 0x11, dev->id[c][i].devsp);
1825 atp_writeb_io(dev, c, 0x12, 0);
1826 atp_writeb_io(dev, c, 0x13, inqd[6]);
1827 atp_writeb_io(dev, c, 0x14, inqd[7]);
1828 atp_writeb_io(dev, c, 0x18, inqd[8]);
80b52a7f 1829
5d2a5a4f 1830 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00)
1da177e4 1831 cpu_relax();
80b52a7f
OZ
1832
1833 if (atp_readb_io(dev, c, 0x17) != 0x11 && atp_readb_io(dev, c, 0x17) != 0x8e)
1da177e4 1834 continue;
80b52a7f 1835
5d2a5a4f 1836 while (atp_readb_io(dev, c, 0x17) != 0x8e)
1da177e4 1837 cpu_relax();
80b52a7f 1838
197fb8d8
OZ
1839 if (wide_chip)
1840 atp_writeb_io(dev, c, 0x1b, 0x00);
1841
5d2a5a4f 1842 atp_writeb_io(dev, c, 0x18, 0x08);
1da177e4
LT
1843 j = 0;
1844rd_inq_data:
5d2a5a4f 1845 k = atp_readb_io(dev, c, 0x1f);
1da177e4 1846 if ((k & 0x01) != 0) {
5d2a5a4f 1847 mbuf[j++] = atp_readb_io(dev, c, 0x19);
1da177e4
LT
1848 goto rd_inq_data;
1849 }
1850 if ((k & 0x80) == 0) {
1851 goto rd_inq_data;
1852 }
5d2a5a4f 1853 j = atp_readb_io(dev, c, 0x17);
1da177e4
LT
1854 if (j == 0x16) {
1855 goto inq_ok;
1856 }
5d2a5a4f
OZ
1857 atp_writeb_io(dev, c, 0x10, 0x46);
1858 atp_writeb_io(dev, c, 0x12, 0);
1859 atp_writeb_io(dev, c, 0x13, 0);
1860 atp_writeb_io(dev, c, 0x14, 0);
1861 atp_writeb_io(dev, c, 0x18, 0x08);
80b52a7f 1862
5d2a5a4f 1863 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00)
1da177e4 1864 cpu_relax();
80b52a7f
OZ
1865
1866 if (atp_readb_io(dev, c, 0x17) != 0x16)
1da177e4 1867 goto sel_ok;
80b52a7f 1868
1da177e4
LT
1869inq_ok:
1870 mbuf[36] = 0;
80b52a7f 1871 printk(KERN_INFO " ID: %2d %s\n", i, &mbuf[8]);
1da177e4
LT
1872 dev->id[c][i].devtype = mbuf[0];
1873 rmb = mbuf[1];
1874 n = mbuf[7];
197fb8d8
OZ
1875 if (!wide_chip)
1876 goto not_wide;
1da177e4
LT
1877 if ((mbuf[7] & 0x60) == 0) {
1878 goto not_wide;
1879 }
197fb8d8
OZ
1880 if (dev->dev_id == ATP885_DEVID || dev->dev_id == ATP880_DEVID1 || dev->dev_id == ATP880_DEVID2) {
1881 if ((i < 8) && ((dev->global_map[c] & 0x20) == 0))
1882 goto not_wide;
1883 } else { /* result of is870() merge - is this a bug? */
1884 if ((dev->global_map[c] & 0x20) == 0)
1885 goto not_wide;
1da177e4
LT
1886 }
1887 if (lvdmode == 0) {
80b52a7f 1888 goto chg_wide;
1da177e4 1889 }
80b52a7f
OZ
1890 if (dev->sp[c][i] != 0x04) // force u2
1891 {
1892 goto chg_wide;
1da177e4
LT
1893 }
1894
5d2a5a4f
OZ
1895 atp_writeb_io(dev, c, 0x1b, 0x01);
1896 atp_writeb_io(dev, c, 3, satn[0]);
1897 atp_writeb_io(dev, c, 4, satn[1]);
1898 atp_writeb_io(dev, c, 5, satn[2]);
1899 atp_writeb_io(dev, c, 6, satn[3]);
1900 atp_writeb_io(dev, c, 7, satn[4]);
1901 atp_writeb_io(dev, c, 8, satn[5]);
1902 atp_writeb_io(dev, c, 0x0f, 0);
1903 atp_writeb_io(dev, c, 0x11, dev->id[c][i].devsp);
1904 atp_writeb_io(dev, c, 0x12, 0);
1905 atp_writeb_io(dev, c, 0x13, satn[6]);
1906 atp_writeb_io(dev, c, 0x14, satn[7]);
1907 atp_writeb_io(dev, c, 0x18, satn[8]);
1908
1909 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00)
1da177e4 1910 cpu_relax();
80b52a7f
OZ
1911
1912 if (atp_readb_io(dev, c, 0x17) != 0x11 && atp_readb_io(dev, c, 0x17) != 0x8e)
1da177e4 1913 continue;
80b52a7f 1914
5d2a5a4f 1915 while (atp_readb_io(dev, c, 0x17) != 0x8e)
1da177e4 1916 cpu_relax();
80b52a7f 1917
1da177e4
LT
1918try_u3:
1919 j = 0;
5d2a5a4f
OZ
1920 atp_writeb_io(dev, c, 0x14, 0x09);
1921 atp_writeb_io(dev, c, 0x18, 0x20);
e2c22b45 1922
5d2a5a4f
OZ
1923 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0) {
1924 if ((atp_readb_io(dev, c, 0x1f) & 0x01) != 0)
1925 atp_writeb_io(dev, c, 0x19, u3[j++]);
1da177e4
LT
1926 cpu_relax();
1927 }
80b52a7f 1928
5d2a5a4f 1929 while ((atp_readb_io(dev, c, 0x17) & 0x80) == 0x00)
1da177e4 1930 cpu_relax();
80b52a7f 1931
5d2a5a4f 1932 j = atp_readb_io(dev, c, 0x17) & 0x0f;
1da177e4
LT
1933 if (j == 0x0f) {
1934 goto u3p_in;
1935 }
1936 if (j == 0x0a) {
1937 goto u3p_cmd;
1938 }
1939 if (j == 0x0e) {
1940 goto try_u3;
1941 }
1942 continue;
1943u3p_out:
5d2a5a4f
OZ
1944 atp_writeb_io(dev, c, 0x18, 0x20);
1945 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0) {
1946 if ((atp_readb_io(dev, c, 0x1f) & 0x01) != 0)
1947 atp_writeb_io(dev, c, 0x19, 0);
1da177e4
LT
1948 cpu_relax();
1949 }
5d2a5a4f 1950 j = atp_readb_io(dev, c, 0x17) & 0x0f;
1da177e4
LT
1951 if (j == 0x0f) {
1952 goto u3p_in;
1953 }
1954 if (j == 0x0a) {
1955 goto u3p_cmd;
1956 }
1957 if (j == 0x0e) {
1958 goto u3p_out;
1959 }
1960 continue;
1961u3p_in:
5d2a5a4f
OZ
1962 atp_writeb_io(dev, c, 0x14, 0x09);
1963 atp_writeb_io(dev, c, 0x18, 0x20);
1da177e4
LT
1964 k = 0;
1965u3p_in1:
5d2a5a4f 1966 j = atp_readb_io(dev, c, 0x1f);
1da177e4 1967 if ((j & 0x01) != 0) {
5d2a5a4f 1968 mbuf[k++] = atp_readb_io(dev, c, 0x19);
1da177e4
LT
1969 goto u3p_in1;
1970 }
1971 if ((j & 0x80) == 0x00) {
1972 goto u3p_in1;
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_cmd:
5d2a5a4f
OZ
1986 atp_writeb_io(dev, c, 0x10, 0x30);
1987 atp_writeb_io(dev, c, 0x14, 0x00);
1988 atp_writeb_io(dev, c, 0x18, 0x08);
80b52a7f 1989
5d2a5a4f 1990 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00);
80b52a7f 1991
5d2a5a4f 1992 j = atp_readb_io(dev, c, 0x17);
1da177e4
LT
1993 if (j != 0x16) {
1994 if (j == 0x4e) {
1995 goto u3p_out;
1996 }
1997 continue;
1998 }
1999 if (mbuf[0] != 0x01) {
2000 goto chg_wide;
2001 }
2002 if (mbuf[1] != 0x06) {
2003 goto chg_wide;
2004 }
2005 if (mbuf[2] != 0x04) {
2006 goto chg_wide;
2007 }
2008 if (mbuf[3] == 0x09) {
2009 m = 1;
2010 m = m << i;
2011 dev->wide_id[c] |= m;
2012 dev->id[c][i].devsp = 0xce;
2013#ifdef ED_DBGP
2014 printk("dev->id[%2d][%2d].devsp = %2x\n",c,i,dev->id[c][i].devsp);
2015#endif
2016 continue;
2017 }
2018chg_wide:
5d2a5a4f
OZ
2019 atp_writeb_io(dev, c, 0x1b, 0x01);
2020 atp_writeb_io(dev, c, 3, satn[0]);
2021 atp_writeb_io(dev, c, 4, satn[1]);
2022 atp_writeb_io(dev, c, 5, satn[2]);
2023 atp_writeb_io(dev, c, 6, satn[3]);
2024 atp_writeb_io(dev, c, 7, satn[4]);
2025 atp_writeb_io(dev, c, 8, satn[5]);
2026 atp_writeb_io(dev, c, 0x0f, 0);
2027 atp_writeb_io(dev, c, 0x11, dev->id[c][i].devsp);
2028 atp_writeb_io(dev, c, 0x12, 0);
2029 atp_writeb_io(dev, c, 0x13, satn[6]);
2030 atp_writeb_io(dev, c, 0x14, satn[7]);
2031 atp_writeb_io(dev, c, 0x18, satn[8]);
2032
2033 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00)
1da177e4 2034 cpu_relax();
80b52a7f
OZ
2035
2036 if (atp_readb_io(dev, c, 0x17) != 0x11 && atp_readb_io(dev, c, 0x17) != 0x8e)
1da177e4 2037 continue;
80b52a7f 2038
5d2a5a4f 2039 while (atp_readb_io(dev, c, 0x17) != 0x8e)
1da177e4 2040 cpu_relax();
80b52a7f 2041
1da177e4
LT
2042try_wide:
2043 j = 0;
5d2a5a4f
OZ
2044 atp_writeb_io(dev, c, 0x14, 0x05);
2045 atp_writeb_io(dev, c, 0x18, 0x20);
e2c22b45 2046
5d2a5a4f
OZ
2047 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0) {
2048 if ((atp_readb_io(dev, c, 0x1f) & 0x01) != 0)
2049 atp_writeb_io(dev, c, 0x19, wide[j++]);
1da177e4
LT
2050 cpu_relax();
2051 }
80b52a7f 2052
5d2a5a4f 2053 while ((atp_readb_io(dev, c, 0x17) & 0x80) == 0x00)
1da177e4 2054 cpu_relax();
80b52a7f 2055
5d2a5a4f 2056 j = atp_readb_io(dev, c, 0x17) & 0x0f;
1da177e4
LT
2057 if (j == 0x0f) {
2058 goto widep_in;
2059 }
2060 if (j == 0x0a) {
2061 goto widep_cmd;
2062 }
2063 if (j == 0x0e) {
2064 goto try_wide;
2065 }
2066 continue;
2067widep_out:
5d2a5a4f
OZ
2068 atp_writeb_io(dev, c, 0x18, 0x20);
2069 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0) {
2070 if ((atp_readb_io(dev, c, 0x1f) & 0x01) != 0)
2071 atp_writeb_io(dev, c, 0x19, 0);
1da177e4
LT
2072 cpu_relax();
2073 }
5d2a5a4f 2074 j = atp_readb_io(dev, c, 0x17) & 0x0f;
1da177e4
LT
2075 if (j == 0x0f) {
2076 goto widep_in;
2077 }
2078 if (j == 0x0a) {
2079 goto widep_cmd;
2080 }
2081 if (j == 0x0e) {
2082 goto widep_out;
2083 }
2084 continue;
2085widep_in:
5d2a5a4f
OZ
2086 atp_writeb_io(dev, c, 0x14, 0xff);
2087 atp_writeb_io(dev, c, 0x18, 0x20);
1da177e4
LT
2088 k = 0;
2089widep_in1:
5d2a5a4f 2090 j = atp_readb_io(dev, c, 0x1f);
1da177e4 2091 if ((j & 0x01) != 0) {
5d2a5a4f 2092 mbuf[k++] = atp_readb_io(dev, c, 0x19);
1da177e4
LT
2093 goto widep_in1;
2094 }
2095 if ((j & 0x80) == 0x00) {
2096 goto widep_in1;
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_cmd:
5d2a5a4f
OZ
2110 atp_writeb_io(dev, c, 0x10, 0x30);
2111 atp_writeb_io(dev, c, 0x14, 0x00);
2112 atp_writeb_io(dev, c, 0x18, 0x08);
80b52a7f 2113
5d2a5a4f 2114 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00)
1da177e4 2115 cpu_relax();
80b52a7f 2116
5d2a5a4f 2117 j = atp_readb_io(dev, c, 0x17);
1da177e4
LT
2118 if (j != 0x16) {
2119 if (j == 0x4e) {
2120 goto widep_out;
2121 }
2122 continue;
2123 }
2124 if (mbuf[0] != 0x01) {
2125 goto not_wide;
2126 }
2127 if (mbuf[1] != 0x02) {
2128 goto not_wide;
2129 }
2130 if (mbuf[2] != 0x03) {
2131 goto not_wide;
2132 }
2133 if (mbuf[3] != 0x01) {
2134 goto not_wide;
2135 }
2136 m = 1;
2137 m = m << i;
2138 dev->wide_id[c] |= m;
2139not_wide:
80b52a7f 2140 if ((dev->id[c][i].devtype == 0x00) || (dev->id[c][i].devtype == 0x07) || ((dev->id[c][i].devtype == 0x05) && ((n & 0x10) != 0))) {
1da177e4
LT
2141 m = 1;
2142 m = m << i;
2143 if ((dev->async[c] & m) != 0) {
80b52a7f 2144 goto set_sync;
1da177e4
LT
2145 }
2146 }
2147 continue;
2148set_sync:
460da918 2149 if ((dev->dev_id != ATP885_DEVID && dev->dev_id != ATP880_DEVID1 && dev->dev_id != ATP880_DEVID2) || (dev->sp[c][i] == 0x02)) {
80b52a7f
OZ
2150 synu[4] = 0x0c;
2151 synuw[4] = 0x0c;
1da177e4 2152 } else {
80b52a7f
OZ
2153 if (dev->sp[c][i] >= 0x03) {
2154 synu[4] = 0x0a;
2155 synuw[4] = 0x0a;
2156 }
1da177e4 2157 }
1da177e4
LT
2158 j = 0;
2159 if ((m & dev->wide_id[c]) != 0) {
2160 j |= 0x01;
2161 }
5d2a5a4f
OZ
2162 atp_writeb_io(dev, c, 0x1b, j);
2163 atp_writeb_io(dev, c, 3, satn[0]);
2164 atp_writeb_io(dev, c, 4, satn[1]);
2165 atp_writeb_io(dev, c, 5, satn[2]);
2166 atp_writeb_io(dev, c, 6, satn[3]);
2167 atp_writeb_io(dev, c, 7, satn[4]);
2168 atp_writeb_io(dev, c, 8, satn[5]);
2169 atp_writeb_io(dev, c, 0x0f, 0);
2170 atp_writeb_io(dev, c, 0x11, dev->id[c][i].devsp);
2171 atp_writeb_io(dev, c, 0x12, 0);
2172 atp_writeb_io(dev, c, 0x13, satn[6]);
2173 atp_writeb_io(dev, c, 0x14, satn[7]);
2174 atp_writeb_io(dev, c, 0x18, satn[8]);
2175
2176 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00)
1da177e4 2177 cpu_relax();
80b52a7f
OZ
2178
2179 if (atp_readb_io(dev, c, 0x17) != 0x11 && atp_readb_io(dev, c, 0x17) != 0x8e)
1da177e4 2180 continue;
80b52a7f 2181
5d2a5a4f 2182 while (atp_readb_io(dev, c, 0x17) != 0x8e)
1da177e4 2183 cpu_relax();
80b52a7f 2184
1da177e4
LT
2185try_sync:
2186 j = 0;
5d2a5a4f
OZ
2187 atp_writeb_io(dev, c, 0x14, 0x06);
2188 atp_writeb_io(dev, c, 0x18, 0x20);
e2c22b45 2189
5d2a5a4f
OZ
2190 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0) {
2191 if ((atp_readb_io(dev, c, 0x1f) & 0x01) != 0) {
1da177e4 2192 if ((m & dev->wide_id[c]) != 0) {
460da918
OZ
2193 if (dev->dev_id == ATP885_DEVID || dev->dev_id == ATP880_DEVID1 || dev->dev_id == ATP880_DEVID2) {
2194 if ((m & dev->ultra_map[c]) != 0) {
2195 atp_writeb_io(dev, c, 0x19, synuw[j++]);
2196 } else {
2197 atp_writeb_io(dev, c, 0x19, synw[j++]);
2198 }
2199 } else
2200 atp_writeb_io(dev, c, 0x19, synw_870[j++]);
1da177e4
LT
2201 } else {
2202 if ((m & dev->ultra_map[c]) != 0) {
5d2a5a4f 2203 atp_writeb_io(dev, c, 0x19, synu[j++]);
1da177e4 2204 } else {
5d2a5a4f 2205 atp_writeb_io(dev, c, 0x19, synn[j++]);
1da177e4
LT
2206 }
2207 }
1da177e4
LT
2208 }
2209 }
80b52a7f 2210
5d2a5a4f 2211 while ((atp_readb_io(dev, c, 0x17) & 0x80) == 0x00)
1da177e4 2212 cpu_relax();
80b52a7f 2213
5d2a5a4f 2214 j = atp_readb_io(dev, c, 0x17) & 0x0f;
1da177e4
LT
2215 if (j == 0x0f) {
2216 goto phase_ins;
2217 }
2218 if (j == 0x0a) {
2219 goto phase_cmds;
2220 }
2221 if (j == 0x0e) {
2222 goto try_sync;
2223 }
2224 continue;
2225phase_outs:
5d2a5a4f
OZ
2226 atp_writeb_io(dev, c, 0x18, 0x20);
2227 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00) {
2228 if ((atp_readb_io(dev, c, 0x1f) & 0x01) != 0x00)
2229 atp_writeb_io(dev, c, 0x19, 0x00);
1da177e4
LT
2230 cpu_relax();
2231 }
5d2a5a4f 2232 j = atp_readb_io(dev, c, 0x17);
1da177e4
LT
2233 if (j == 0x85) {
2234 goto tar_dcons;
2235 }
2236 j &= 0x0f;
2237 if (j == 0x0f) {
2238 goto phase_ins;
2239 }
2240 if (j == 0x0a) {
2241 goto phase_cmds;
2242 }
2243 if (j == 0x0e) {
2244 goto phase_outs;
2245 }
2246 continue;
2247phase_ins:
460da918
OZ
2248 if (dev->dev_id == ATP885_DEVID || dev->dev_id == ATP880_DEVID1 || dev->dev_id == ATP880_DEVID2)
2249 atp_writeb_io(dev, c, 0x14, 0x06);
2250 else
2251 atp_writeb_io(dev, c, 0x14, 0xff);
5d2a5a4f 2252 atp_writeb_io(dev, c, 0x18, 0x20);
1da177e4
LT
2253 k = 0;
2254phase_ins1:
5d2a5a4f 2255 j = atp_readb_io(dev, c, 0x1f);
1da177e4 2256 if ((j & 0x01) != 0x00) {
5d2a5a4f 2257 mbuf[k++] = atp_readb_io(dev, c, 0x19);
1da177e4
LT
2258 goto phase_ins1;
2259 }
2260 if ((j & 0x80) == 0x00) {
2261 goto phase_ins1;
2262 }
80b52a7f 2263
5d2a5a4f 2264 while ((atp_readb_io(dev, c, 0x17) & 0x80) == 0x00);
80b52a7f 2265
5d2a5a4f 2266 j = atp_readb_io(dev, c, 0x17);
1da177e4
LT
2267 if (j == 0x85) {
2268 goto tar_dcons;
2269 }
2270 j &= 0x0f;
2271 if (j == 0x0f) {
2272 goto phase_ins;
2273 }
2274 if (j == 0x0a) {
2275 goto phase_cmds;
2276 }
2277 if (j == 0x0e) {
2278 goto phase_outs;
2279 }
2280 continue;
2281phase_cmds:
5d2a5a4f 2282 atp_writeb_io(dev, c, 0x10, 0x30);
1da177e4 2283tar_dcons:
5d2a5a4f
OZ
2284 atp_writeb_io(dev, c, 0x14, 0x00);
2285 atp_writeb_io(dev, c, 0x18, 0x08);
80b52a7f 2286
5d2a5a4f 2287 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00)
1da177e4 2288 cpu_relax();
80b52a7f 2289
5d2a5a4f 2290 j = atp_readb_io(dev, c, 0x17);
1da177e4
LT
2291 if (j != 0x16) {
2292 continue;
2293 }
2294 if (mbuf[0] != 0x01) {
2295 continue;
2296 }
2297 if (mbuf[1] != 0x03) {
2298 continue;
2299 }
2300 if (mbuf[4] == 0x00) {
2301 continue;
2302 }
2303 if (mbuf[3] > 0x64) {
2304 continue;
2305 }
460da918
OZ
2306 if (dev->dev_id == ATP885_DEVID || dev->dev_id == ATP880_DEVID1 || dev->dev_id == ATP880_DEVID2) {
2307 if (mbuf[4] > 0x0e) {
2308 mbuf[4] = 0x0e;
2309 }
2310 } else {
2311 if (mbuf[4] > 0x0c) {
2312 mbuf[4] = 0x0c;
2313 }
1da177e4
LT
2314 }
2315 dev->id[c][i].devsp = mbuf[4];
460da918
OZ
2316 if (dev->dev_id == ATP885_DEVID || dev->dev_id == ATP880_DEVID1 || dev->dev_id == ATP880_DEVID2)
2317 if (mbuf[3] < 0x0c) {
2318 j = 0xb0;
2319 goto set_syn_ok;
2320 }
1da177e4
LT
2321 if ((mbuf[3] < 0x0d) && (rmb == 0)) {
2322 j = 0xa0;
2323 goto set_syn_ok;
2324 }
2325 if (mbuf[3] < 0x1a) {
2326 j = 0x20;
2327 goto set_syn_ok;
2328 }
2329 if (mbuf[3] < 0x33) {
2330 j = 0x40;
2331 goto set_syn_ok;
2332 }
2333 if (mbuf[3] < 0x4c) {
2334 j = 0x50;
2335 goto set_syn_ok;
2336 }
2337 j = 0x60;
80b52a7f 2338set_syn_ok:
1da177e4 2339 dev->id[c][i].devsp = (dev->id[c][i].devsp & 0x0f) | j;
80b52a7f 2340#ifdef ED_DBGP
1da177e4
LT
2341 printk("dev->id[%2d][%2d].devsp = %2x\n",c,i,dev->id[c][i].devsp);
2342#endif
2343 }
1da177e4 2344}
This page took 1.092274 seconds and 5 git commands to generate.