blob: 7f53a50a0ab4ab0221b52aa422f66b4b69a8b643 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (C) 1997 Wu Ching Chen
3 * 2.1.x update (C) 1998 Krzysztof G. Baranowski
Alan Coxfa195af2008-10-27 15:16:36 +00004 * 2.5.x update (C) 2002 Red Hat
5 * 2.6.x update (C) 2004 Red Hat
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 *
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>
Matthias Gehre910638a2006-03-28 01:56:48 -080031#include <linux/dma-mapping.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#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);
Ondrej Zary4192a402015-11-17 19:24:06 +010044static void atp_is(struct atp_unit *dev, unsigned char c, bool wide_chip, unsigned char lvdmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Ondrej Zary6a3cebb2015-11-17 19:23:54 +010046static inline void atp_writeb_base(struct atp_unit *atp, u8 reg, u8 val)
47{
48 outb(val, atp->baseport + reg);
49}
50
Ondrej Zaryd804bb22015-11-17 19:24:07 +010051static inline void atp_writew_base(struct atp_unit *atp, u8 reg, u16 val)
52{
53 outw(val, atp->baseport + reg);
54}
55
Ondrej Zary6a3cebb2015-11-17 19:23:54 +010056static 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
Ondrej Zaryd804bb22015-11-17 19:24:07 +010081static 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
Ondrej Zary6a3cebb2015-11-17 19:23:54 +010091static 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
David Howells7d12e782006-10-05 14:55:46 +0100106static irqreturn_t atp870u_intr_handle(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
108 unsigned long flags;
Ondrej Zarybc0fe4c2015-11-17 19:23:47 +0100109 unsigned short int id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 unsigned char i, j, c, target_id, lun,cmdp;
111 unsigned char *prd;
112 struct scsi_cmnd *workreq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 unsigned long adrcnt, k;
114#ifdef ED_DBGP
115 unsigned long l;
116#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 struct Scsi_Host *host = dev_id;
118 struct atp_unit *dev = (struct atp_unit *)&host->hostdata;
119
120 for (c = 0; c < 2; c++) {
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100121 j = atp_readb_io(dev, c, 0x1f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 if ((j & 0x80) != 0)
Ondrej Zary78614ec2015-11-17 19:23:49 +0100123 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 dev->in_int[c] = 0;
125 }
Ondrej Zary78614ec2015-11-17 19:23:49 +0100126 if ((j & 0x80) == 0)
127 return IRQ_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128#ifdef ED_DBGP
129 printk("atp870u_intr_handle enter\n");
130#endif
131 dev->in_int[c] = 1;
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100132 cmdp = atp_readb_io(dev, c, 0x10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 if (dev->working[c] != 0) {
134 if (dev->dev_id == ATP885_DEVID) {
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100135 if ((atp_readb_io(dev, c, 0x16) & 0x80) == 0)
136 atp_writeb_io(dev, c, 0x16, (atp_readb_io(dev, c, 0x16) | 0x80));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 }
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100138 if ((atp_readb_pci(dev, c, 0x00) & 0x08) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 for (k=0; k < 1000; k++) {
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100141 if ((atp_readb_pci(dev, c, 2) & 0x08) == 0)
Ondrej Zary78614ec2015-11-17 19:23:49 +0100142 break;
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100143 if ((atp_readb_pci(dev, c, 2) & 0x01) == 0)
Ondrej Zary78614ec2015-11-17 19:23:49 +0100144 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 }
146 }
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100147 atp_writeb_pci(dev, c, 0, 0x00);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100149 i = atp_readb_io(dev, c, 0x17);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Ondrej Zarybc0fe4c2015-11-17 19:23:47 +0100151 if (dev->dev_id == ATP885_DEVID)
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100152 atp_writeb_pci(dev, c, 2, 0x06);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100154 target_id = atp_readb_io(dev, c, 0x15);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
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) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 adrcnt = 0;
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100183 ((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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 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
Ondrej Zary3a38e532015-11-17 19:23:39 +0100194 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195#endif
196 }
197
198 /*
199 * Flip wide
200 */
201 if (dev->wide_id[c] != 0) {
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100202 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 }
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
Ondrej Zary78614ec2015-11-17 19:23:49 +0100225 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 }
227
228 if (i == 0x40) {
229 dev->last_cmd[c] |= 0x40;
230 dev->in_int[c] = 0;
Ondrej Zary78614ec2015-11-17 19:23:49 +0100231 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 }
233
234 if (i == 0x21) {
235 if ((dev->last_cmd[c] & 0xf0) != 0x40) {
236 dev->last_cmd[c] = 0xff;
237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 adrcnt = 0;
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100239 ((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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 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;
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100246 atp_writeb_io(dev, c, 0x10, 0x41);
247 atp_writeb_io(dev, c, 0x18, 0x08);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 dev->in_int[c] = 0;
Ondrej Zary78614ec2015-11-17 19:23:49 +0100249 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 }
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;
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100266 if (cmdp == 0x44 || i == 0x80)
267 lun = atp_readb_io(dev, c, 0x1d) & 0x07;
268 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 adrcnt = 0;
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100277 ((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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 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;
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100284 atp_writeb_io(dev, c, 0x18, 0x08);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 dev->in_int[c] = 0;
Ondrej Zary78614ec2015-11-17 19:23:49 +0100286 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 } else {
288#ifdef ED_DBGP
289 printk("cmdp != 0x41\n");
290#endif
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100291 atp_writeb_io(dev, c, 0x10, 0x46);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 dev->id[c][target_id].dirct = 0x00;
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100293 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 dev->in_int[c] = 0;
Ondrej Zary78614ec2015-11-17 19:23:49 +0100298 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 }
300 }
301 if (dev->last_cmd[c] != 0xff) {
302 dev->last_cmd[c] |= 0x40;
303 }
304 if (dev->dev_id == ATP885_DEVID) {
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100305 j = atp_readb_base(dev, 0x29) & 0xfe;
306 atp_writeb_base(dev, 0x29, j);
Ondrej Zary3a38e532015-11-17 19:23:39 +0100307 } else
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100308 atp_writeb_io(dev, c, 0x10, 0x45);
Ondrej Zary3a38e532015-11-17 19:23:39 +0100309
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100310 target_id = atp_readb_io(dev, c, 0x16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 /*
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 }
Ondrej Zary3a38e532015-11-17 19:23:39 +0100319 if (dev->dev_id == ATP885_DEVID)
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100320 atp_writeb_io(dev, c, 0x10, 0x45);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 workreq = dev->id[c][target_id].curr_req;
322#ifdef ED_DBGP
Jeff Garzik017560f2005-10-24 18:04:36 -0400323 scmd_printk(KERN_DEBUG, workreq, "CDB");
324 for (l = 0; l < workreq->cmd_len; l++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 printk(KERN_DEBUG " %x",workreq->cmnd[l]);
Jeff Garzik017560f2005-10-24 18:04:36 -0400326 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327#endif
328
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100329 atp_writeb_io(dev, c, 0x0f, lun);
330 atp_writeb_io(dev, c, 0x11, dev->id[c][target_id].devsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 adrcnt = dev->id[c][target_id].tran_len;
332 k = dev->id[c][target_id].last_len;
333
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100334 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]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337#ifdef ED_DBGP
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100338 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));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339#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;
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100347 atp_writeb_io(dev, c, 0x15, j);
348 atp_writeb_io(dev, c, 0x16, 0x80);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
350 /* enable 32 bit fifo transfer */
351 if (dev->dev_id == ATP885_DEVID) {
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100352 i = atp_readb_pci(dev, c, 1) & 0xf3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 //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 }
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100357 atp_writeb_pci(dev, c, 1, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 } else if ((dev->dev_id == ATP880_DEVID1) ||
359 (dev->dev_id == ATP880_DEVID2) ) {
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100360 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 } else {
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100365 if ((workreq->cmnd[0] == 0x08) || (workreq->cmnd[0] == 0x28) || (workreq->cmnd[0] == 0x0a) || (workreq->cmnd[0] == 0x2a))
Ondrej Zaryc751d9f2015-11-17 19:24:09 +0100366 atp_writeb_base(dev, 0x3a, (atp_readb_base(dev, 0x3a) & 0xf3) | 0x08);
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100367 else
Ondrej Zaryc751d9f2015-11-17 19:24:09 +0100368 atp_writeb_base(dev, 0x3a, atp_readb_base(dev, 0x3a) & 0xf3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 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 }
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100379 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 if (dev->id[c][target_id].last_len == 0) {
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100383 atp_writeb_io(dev, c, 0x18, 0x08);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 dev->in_int[c] = 0;
385#ifdef ED_DBGP
386 printk("dev->id[c][target_id].last_len = 0\n");
387#endif
Ondrej Zary78614ec2015-11-17 19:23:49 +0100388 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 }
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 }
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100416 atp_writel_pci(dev, c, 0x04, dev->id[c][target_id].prdaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417#ifdef ED_DBGP
418 printk("dev->id[%d][%d].prdaddr 0x%8x\n", c, target_id, dev->id[c][target_id].prdaddr);
419#endif
Ondrej Zarybc0fe4c2015-11-17 19:23:47 +0100420 if (dev->dev_id != ATP885_DEVID) {
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100421 atp_writeb_pci(dev, c, 2, 0x06);
422 atp_writeb_pci(dev, c, 2, 0x00);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 /*
425 * Check transfer direction
426 */
427 if (dev->id[c][target_id].dirct != 0) {
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100428 atp_writeb_io(dev, c, 0x18, 0x08);
429 atp_writeb_pci(dev, c, 0, 0x01);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 dev->in_int[c] = 0;
431#ifdef ED_DBGP
432 printk("status 0x80 return dirct != 0\n");
433#endif
Ondrej Zary78614ec2015-11-17 19:23:49 +0100434 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 }
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100436 atp_writeb_io(dev, c, 0x18, 0x08);
437 atp_writeb_pci(dev, c, 0, 0x09);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 dev->in_int[c] = 0;
439#ifdef ED_DBGP
440 printk("status 0x80 return dirct = 0\n");
441#endif
Ondrej Zary78614ec2015-11-17 19:23:49 +0100442 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 }
444
445 /*
446 * Current scsi request on this target
447 */
448
449 workreq = dev->id[c][target_id].curr_req;
450
Ondrej Zary78614ec2015-11-17 19:23:49 +0100451 if (i == 0x42 || i == 0x16) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 if ((dev->last_cmd[c] & 0xf0) != 0x40) {
453 dev->last_cmd[c] = 0xff;
454 }
Ondrej Zary78614ec2015-11-17 19:23:49 +0100455 if (i == 0x16) {
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100456 workreq->result = atp_readb_io(dev, c, 0x0f);
Ondrej Zary78614ec2015-11-17 19:23:49 +0100457 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 if (dev->dev_id == ATP885_DEVID) {
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100465 j = atp_readb_base(dev, 0x29) | 0x01;
466 atp_writeb_base(dev, 0x29, j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 }
468 /*
469 * Complete the command
470 */
Boaz Harroshfe7ed982007-09-09 21:06:23 +0300471 scsi_dma_unmap(workreq);
472
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 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) {
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100488 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 }
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;
Ondrej Zary78614ec2015-11-17 19:23:49 +0100505 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 }
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) {
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100515 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 if (dev->dev_id == ATP885_DEVID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 k = dev->id[c][target_id].last_len;
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100521 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]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 dev->id[c][target_id].dirct = 0x00;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 } else {
526 dev->id[c][target_id].dirct = 0x00;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 }
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100528 atp_writeb_io(dev, c, 0x18, 0x08);
529 atp_writeb_pci(dev, c, 0, 0x09);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 dev->in_int[c] = 0;
Ondrej Zary78614ec2015-11-17 19:23:49 +0100531 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 }
533 if (i == 0x08) {
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100534 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 if (dev->dev_id == ATP885_DEVID) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 k = dev->id[c][target_id].last_len;
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100540 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]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 }
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100544 atp_writeb_io(dev, c, 0x15, atp_readb_io(dev, c, 0x15) | 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 dev->id[c][target_id].dirct = 0x20;
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100546 atp_writeb_io(dev, c, 0x18, 0x08);
547 atp_writeb_pci(dev, c, 0, 0x01);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 dev->in_int[c] = 0;
Ondrej Zary78614ec2015-11-17 19:23:49 +0100549 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 }
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100551 if (i == 0x0a)
552 atp_writeb_io(dev, c, 0x10, 0x30);
553 else
554 atp_writeb_io(dev, c, 0x10, 0x46);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 dev->id[c][target_id].dirct = 0x00;
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100556 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 }
Ondrej Zary78614ec2015-11-17 19:23:49 +0100561 dev->in_int[c] = 0;
562
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 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 */
Jeff Garzikf2812332010-11-16 02:10:29 -0500572static int atp870u_queuecommand_lck(struct scsi_cmnd *req_p,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 void (*done) (struct scsi_cmnd *))
574{
575 unsigned char c;
Ondrej Zary3b836462015-11-17 19:23:40 +0100576 unsigned int m;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 struct atp_unit *dev;
578 struct Scsi_Host *host;
579
Jeff Garzik422c0d62005-10-24 18:05:09 -0400580 c = scmd_channel(req_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 req_p->sense_buffer[0]=0;
Boaz Harroshfe7ed982007-09-09 21:06:23 +0300582 scsi_set_resid(req_p, 0);
Jeff Garzik422c0d62005-10-24 18:05:09 -0400583 if (scmd_channel(req_p) > 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 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;
Jeff Garzik422c0d62005-10-24 18:05:09 -0400598 m = m << scmd_id(req_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645#ifdef ED_DBGP
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100646 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]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647#endif
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100648 if ((atp_readb_io(dev, c, 0x1c) == 0) && (dev->in_int[c] == 0) && (dev->in_snd[c] == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649#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
Jeff Garzikf2812332010-11-16 02:10:29 -0500660static DEF_SCSI_QCMD(atp870u_queuecommand)
661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662/**
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{
Ondrej Zary468b8962015-11-17 19:23:50 +0100673 struct scsi_cmnd *workreq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 unsigned int i;//,k;
675 unsigned char j, target_id;
676 unsigned char *prd;
Ondrej Zaryc2bab402015-11-17 19:23:48 +0100677 unsigned short int w;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 unsigned long l, bttl = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 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;
Ondrej Zary468b8962015-11-17 19:23:50 +0100694 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 }
701 }
Ondrej Zary468b8962015-11-17 19:23:50 +0100702 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 }
Jeff Garzik422c0d62005-10-24 18:05:09 -0400719 dev->id[c][scmd_id(workreq)].curr_req = workreq;
720 dev->last_cmd[c] = scmd_id(workreq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 }
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100722 if ((atp_readb_io(dev, c, 0x1f) & 0xb0) != 0 || atp_readb_io(dev, c, 0x1c) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723#ifdef ED_DBGP
Ondrej Zary468b8962015-11-17 19:23:50 +0100724 printk("Abort to Send\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725#endif
Ondrej Zary468b8962015-11-17 19:23:50 +0100726 dev->last_cmd[c] |= 0x40;
727 dev->in_snd[c] = 0;
728 return;
729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730#ifdef ED_DBGP
731 printk("OK to Send\n");
Jeff Garzik422c0d62005-10-24 18:05:09 -0400732 scmd_printk(KERN_DEBUG, workreq, "CDB");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 for(i=0;i<workreq->cmd_len;i++) {
734 printk(" %x",workreq->cmnd[i]);
735 }
Jeff Garzik422c0d62005-10-24 18:05:09 -0400736 printk("\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737#endif
Boaz Harroshfe7ed982007-09-09 21:06:23 +0300738 l = scsi_bufflen(workreq);
739
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 if (dev->dev_id == ATP885_DEVID) {
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100741 j = atp_readb_base(dev, 0x29) & 0xfe;
742 atp_writeb_base(dev, 0x29, j);
Jeff Garzik422c0d62005-10-24 18:05:09 -0400743 dev->r1f[c][scmd_id(workreq)] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 }
745
746 if (workreq->cmnd[0] == READ_CAPACITY) {
Boaz Harroshfe7ed982007-09-09 21:06:23 +0300747 if (l > 8)
748 l = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 }
750 if (workreq->cmnd[0] == 0x00) {
Boaz Harroshfe7ed982007-09-09 21:06:23 +0300751 l = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 }
753
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 j = 0;
Jeff Garzik422c0d62005-10-24 18:05:09 -0400755 target_id = scmd_id(workreq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
757 /*
758 * Wide ?
759 */
760 w = 1;
761 w = w << target_id;
762 if ((w & dev->wide_id[c]) != 0) {
763 j |= 0x01;
764 }
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100765 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768#ifdef ED_DBGP
769 printk("send_s870 while loop 1\n");
770#endif
771 }
772 /*
773 * Write the command
774 */
775
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100776 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 /*
786 * Write the target
787 */
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100788 atp_writeb_io(dev, c, 0x11, dev->id[c][target_id].devsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789#ifdef ED_DBGP
790 printk("dev->id[%d][%d].devsp = %2x\n",c,target_id,dev->id[c][target_id].devsp);
791#endif
Boaz Harroshfe7ed982007-09-09 21:06:23 +0300792
793 sg_count = scsi_dma_map(workreq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 /*
795 * Write transfer size
796 */
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100797 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]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 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 */
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100815 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 dev->id[c][target_id].dirct = 0;
822 if (l == 0) {
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100823 if (atp_readb_io(dev, c, 0x1c) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824#ifdef ED_DBGP
825 printk("change SCSI_CMD_REG 0x08\n");
826#endif
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100827 atp_writeb_io(dev, c, 0x18, 0x08);
828 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 dev->last_cmd[c] |= 0x40;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 dev->in_snd[c] = 0;
831 return;
832 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 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
Boaz Harroshfe7ed982007-09-09 21:06:23 +0300841 if (l) {
842 struct scatterlist *sgpnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 i = 0;
Boaz Harroshfe7ed982007-09-09 21:06:23 +0300844 scsi_for_each_sg(workreq, sgpnt, sg_count, j) {
845 bttl = sg_dma_address(sgpnt);
846 l=sg_dma_len(sgpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847#ifdef ED_DBGP
Boaz Harroshfe7ed982007-09-09 21:06:23 +0300848 printk("1. bttl %x, l %x\n",bttl, l);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849#endif
Boaz Harroshfe7ed982007-09-09 21:06:23 +0300850 while (l > 0x10000) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 (((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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869#ifdef ED_DBGP
Ondrej Zaryc2bab402015-11-17 19:23:48 +0100870 printk("send_s870: prdaddr_2 0x%8x target_id %d\n", dev->id[c][target_id].prdaddr,target_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871#endif
James Bottomleyb5683552005-09-15 08:59:36 -0500872 dev->id[c][target_id].prdaddr = dev->id[c][target_id].prd_bus;
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100873 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 if (dev->dev_id == ATP885_DEVID) {
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100877 j = atp_readb_pci(dev, c, 1) & 0xf3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 if ((workreq->cmnd[0] == 0x08) || (workreq->cmnd[0] == 0x28) ||
879 (workreq->cmnd[0] == 0x0a) || (workreq->cmnd[0] == 0x2a)) {
880 j |= 0x0c;
881 }
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100882 atp_writeb_pci(dev, c, 1, j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 } else if ((dev->dev_id == ATP880_DEVID1) ||
884 (dev->dev_id == ATP880_DEVID2)) {
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100885 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 } else {
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100890 if ((workreq->cmnd[0] == 0x08) || (workreq->cmnd[0] == 0x28) || (workreq->cmnd[0] == 0x0a) || (workreq->cmnd[0] == 0x2a))
Ondrej Zaryc751d9f2015-11-17 19:24:09 +0100891 atp_writeb_base(dev, 0x3a, (atp_readb_base(dev, 0x3a) & 0xf3) | 0x08);
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100892 else
Ondrej Zaryc751d9f2015-11-17 19:24:09 +0100893 atp_writeb_base(dev, 0x3a, atp_readb_base(dev, 0x3a) & 0xf3);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
896 if(workreq->sc_data_direction == DMA_TO_DEVICE) {
897 dev->id[c][target_id].dirct = 0x20;
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100898 if (atp_readb_io(dev, c, 0x1c) == 0) {
899 atp_writeb_io(dev, c, 0x18, 0x08);
900 atp_writeb_pci(dev, c, 0, 0x01);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901#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 }
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100910 if (atp_readb_io(dev, c, 0x1c) == 0) {
911 atp_writeb_io(dev, c, 0x18, 0x08);
912 atp_writeb_pci(dev, c, 0, 0x09);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913#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{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 unsigned short int i, k;
927 unsigned char j;
928
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100929 atp_writew_io(dev, 0, 0x1c, *val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 for (i = 0; i < 10; i++) { /* stable >= bus settle delay(400 ns) */
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100931 k = atp_readw_io(dev, 0, 0x1c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 j = (unsigned char) (k >> 8);
Ondrej Zary832e9ac2015-11-17 19:23:51 +0100933 if ((k & 0x8000) != 0) /* DB7 all release? */
934 i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 }
936 *val |= 0x4000; /* assert DB6 */
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100937 atp_writew_io(dev, 0, 0x1c, *val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 *val &= 0xdfff; /* assert DB5 */
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100939 atp_writew_io(dev, 0, 0x1c, *val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 for (i = 0; i < 10; i++) { /* stable >= bus settle delay(400 ns) */
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100941 if ((atp_readw_io(dev, 0, 0x1c) & 0x2000) != 0) /* DB5 all release? */
Ondrej Zary832e9ac2015-11-17 19:23:51 +0100942 i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 }
944 *val |= 0x8000; /* no DB4-0, assert DB7 */
945 *val &= 0xe0ff;
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100946 atp_writew_io(dev, 0, 0x1c, *val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 *val &= 0xbfff; /* release DB6 */
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100948 atp_writew_io(dev, 0, 0x1c, *val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 for (i = 0; i < 10; i++) { /* stable >= bus settle delay(400 ns) */
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100950 if ((atp_readw_io(dev, 0, 0x1c) & 0x4000) != 0) /* DB6 all release? */
Ondrej Zary832e9ac2015-11-17 19:23:51 +0100951 i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952 }
953
954 return j;
955}
956
957static void tscam(struct Scsi_Host *host)
958{
959
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 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
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100976 atp_writeb_io(dev, 0, 1, 0x08);
977 atp_writeb_io(dev, 0, 2, 0x7f);
978 atp_writeb_io(dev, 0, 0x11, 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979
980 if ((dev->scam_on & 0x40) == 0) {
981 return;
982 }
983 m = 1;
984 m <<= dev->host_id[0];
985 j = 16;
986 if (dev->chip_ver < 4) {
987 m |= 0xff00;
988 j = 8;
989 }
990 assignid_map = m;
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100991 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998
999 for (i = 0; i < j; i++) {
1000 m = 1;
1001 m = m << i;
1002 if ((m & assignid_map) != 0) {
1003 continue;
1004 }
Ondrej Zary6a3cebb2015-11-17 19:23:54 +01001005 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 if (i > 7) {
1010 k = (i & 0x07) | 0x40;
1011 } else {
1012 k = i;
1013 }
Ondrej Zary6a3cebb2015-11-17 19:23:54 +01001014 atp_writeb_io(dev, 0, 0x15, k);
1015 if (dev->chip_ver == 4)
1016 atp_writeb_io(dev, 0, 0x1b, 0x01);
1017 else
1018 atp_writeb_io(dev, 0, 0x1b, 0x00);
Ondrej Zary58c4d042015-11-17 19:23:52 +01001019 do {
Ondrej Zary6a3cebb2015-11-17 19:23:54 +01001020 atp_writeb_io(dev, 0, 0x18, 0x09);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
Ondrej Zary6a3cebb2015-11-17 19:23:54 +01001022 while ((atp_readb_io(dev, 0, 0x1f) & 0x80) == 0x00)
Ondrej Zary58c4d042015-11-17 19:23:52 +01001023 cpu_relax();
Ondrej Zary6a3cebb2015-11-17 19:23:54 +01001024 k = atp_readb_io(dev, 0, 0x17);
Ondrej Zary58c4d042015-11-17 19:23:52 +01001025 if ((k == 0x85) || (k == 0x42))
1026 break;
1027 if (k != 0x16)
Ondrej Zary6a3cebb2015-11-17 19:23:54 +01001028 atp_writeb_io(dev, 0, 0x10, 0x41);
Ondrej Zary58c4d042015-11-17 19:23:52 +01001029 } while (k != 0x16);
1030 if ((k == 0x85) || (k == 0x42))
1031 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 assignid_map |= m;
1033
1034 }
Ondrej Zary6a3cebb2015-11-17 19:23:54 +01001035 atp_writeb_io(dev, 0, 0x02, 0x7f);
1036 atp_writeb_io(dev, 0, 0x1b, 0x02);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
Ondrej Zary2bbbac42015-11-17 19:24:08 +01001038 udelay(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
1040 val = 0x0080; /* bsy */
Ondrej Zary6a3cebb2015-11-17 19:23:54 +01001041 atp_writew_io(dev, 0, 0x1c, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 val |= 0x0040; /* sel */
Ondrej Zary6a3cebb2015-11-17 19:23:54 +01001043 atp_writew_io(dev, 0, 0x1c, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 val |= 0x0004; /* msg */
Ondrej Zary6a3cebb2015-11-17 19:23:54 +01001045 atp_writew_io(dev, 0, 0x1c, val);
Ondrej Zary2bbbac42015-11-17 19:24:08 +01001046 udelay(2); /* 2 deskew delay(45ns*2=90ns) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 val &= 0x007f; /* no bsy */
Ondrej Zary6a3cebb2015-11-17 19:23:54 +01001048 atp_writew_io(dev, 0, 0x1c, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 mdelay(128);
1050 val &= 0x00fb; /* after 1ms no msg */
Ondrej Zary6a3cebb2015-11-17 19:23:54 +01001051 atp_writew_io(dev, 0, 0x1c, val);
1052 while ((atp_readb_io(dev, 0, 0x1c) & 0x04) != 0)
Ondrej Zary58c4d042015-11-17 19:23:52 +01001053 ;
Ondrej Zary2bbbac42015-11-17 19:24:08 +01001054 udelay(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 udelay(100);
Ondrej Zaryc7fcc082015-11-17 19:23:53 +01001056 for (n = 0; n < 0x30000; n++)
Ondrej Zary6a3cebb2015-11-17 19:23:54 +01001057 if ((atp_readb_io(dev, 0, 0x1c) & 0x80) != 0) /* bsy ? */
Ondrej Zaryc7fcc082015-11-17 19:23:53 +01001058 break;
1059 if (n < 0x30000)
1060 for (n = 0; n < 0x30000; n++)
Ondrej Zary6a3cebb2015-11-17 19:23:54 +01001061 if ((atp_readb_io(dev, 0, 0x1c) & 0x81) == 0x0081) {
Ondrej Zary2bbbac42015-11-17 19:24:08 +01001062 udelay(2);
Ondrej Zaryc7fcc082015-11-17 19:23:53 +01001063 val |= 0x8003; /* io,cd,db7 */
Ondrej Zary6a3cebb2015-11-17 19:23:54 +01001064 atp_writew_io(dev, 0, 0x1c, val);
Ondrej Zary2bbbac42015-11-17 19:24:08 +01001065 udelay(2);
Ondrej Zaryc7fcc082015-11-17 19:23:53 +01001066 val &= 0x00bf; /* no sel */
Ondrej Zary6a3cebb2015-11-17 19:23:54 +01001067 atp_writew_io(dev, 0, 0x1c, val);
Ondrej Zary2bbbac42015-11-17 19:24:08 +01001068 udelay(2);
Ondrej Zaryc7fcc082015-11-17 19:23:53 +01001069 break;
1070 }
1071 while (1) {
Martin Michlmayr0f6d93a2012-10-04 17:11:25 -07001072 /*
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);
Ondrej Zary6a3cebb2015-11-17 19:23:54 +01001082 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)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 cpu_relax();
Ondrej Zary6a3cebb2015-11-17 19:23:54 +01001089 atp_readb_io(dev, 0, 0x17);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 return;
1091 }
1092 val &= 0x00ff; /* synchronization */
1093 val |= 0x3f00;
1094 fun_scam(dev, &val);
Ondrej Zary2bbbac42015-11-17 19:24:08 +01001095 udelay(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 val &= 0x00ff; /* isolation */
1097 val |= 0x2000;
1098 fun_scam(dev, &val);
Ondrej Zary2bbbac42015-11-17 19:24:08 +01001099 udelay(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 i = 8;
1101 j = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
Ondrej Zaryc7fcc082015-11-17 19:23:53 +01001103 while (1) {
Ondrej Zary6a3cebb2015-11-17 19:23:54 +01001104 if ((atp_readw_io(dev, 0, 0x1c) & 0x2000) == 0)
Ondrej Zaryc7fcc082015-11-17 19:23:53 +01001105 continue;
Ondrej Zary2bbbac42015-11-17 19:24:08 +01001106 udelay(2);
Ondrej Zaryc7fcc082015-11-17 19:23:53 +01001107 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;
1121 }
1122
1123 /* isolation complete.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124/* 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];
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001128 if ((j & 0x20) != 0) { /* bit5=1:ID up to 7 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 i = 7;
1130 }
Ondrej Zaryc7fcc082015-11-17 19:23:53 +01001131 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 }
Ondrej Zaryc7fcc082015-11-17 19:23:53 +01001144 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 }
Ondrej Zaryc7fcc082015-11-17 19:23:53 +01001157 /* k=binID#, */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 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
Ondrej Zaryc7fcc082015-11-17 19:23:53 +01001176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177}
1178
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179static 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;
James Bottomleyb5683552005-09-15 08:59:36 -05001187 pci_free_consistent(atp_dev->pdev, 1024, atp_dev->id[j][k].prd_table, atp_dev->id[j][k].prd_bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 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++) {
James Bottomleyb5683552005-09-15 08:59:36 -05001199 atp_dev->id[c][k].prd_table = pci_alloc_consistent(atp_dev->pdev, 1024, &(atp_dev->id[c][k].prd_bus));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 if (!atp_dev->id[c][k].prd_table) {
1201 printk("atp870u_init_tables fail\n");
1202 atp870u_free_tables(host);
1203 return -ENOMEM;
1204 }
James Bottomleyb5683552005-09-15 08:59:36 -05001205 atp_dev->id[c][k].prdaddr = atp_dev->id[c][k].prd_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 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
Ondrej Zary6a1961b2015-11-17 19:24:10 +01001231static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243/* 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;
Ondrej Zary493c5202015-11-17 19:23:44 +01001248 unsigned int base_io, error,n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 unsigned char host_id;
1250 struct Scsi_Host *shpnt = NULL;
Randy Dunlapdc6a78f2006-06-27 22:01:28 -07001251 struct atp_unit *atpdev, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 unsigned char setupdata[2][16];
1253 int count = 0;
Randy Dunlapdc6a78f2006-06-27 22:01:28 -07001254
1255 atpdev = kzalloc(sizeof(*atpdev), GFP_KERNEL);
1256 if (!atpdev)
1257 return -ENOMEM;
1258
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 if (pci_enable_device(pdev))
Randy Dunlapdc6a78f2006-06-27 22:01:28 -07001260 goto err_eio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
Ondrej Zary34a2c352015-11-17 19:24:11 +01001262 if (pci_set_dma_mask(pdev, DMA_BIT_MASK(32))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 printk(KERN_ERR "atp870u: DMA mask required but not available.\n");
Randy Dunlapdc6a78f2006-06-27 22:01:28 -07001264 goto err_eio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 }
1266
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 /*
1268 * It's probably easier to weed out some revisions like
1269 * this than via the PCI device table
1270 */
1271 if (ent->device == PCI_DEVICE_ID_ARTOP_AEC7610) {
Sergei Shtylyov7d7311c2012-03-14 22:04:30 +03001272 atpdev->chip_ver = pdev->revision;
Randy Dunlapdc6a78f2006-06-27 22:01:28 -07001273 if (atpdev->chip_ver < 2)
1274 goto err_eio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 }
1276
1277 switch (ent->device) {
1278 case PCI_DEVICE_ID_ARTOP_AEC7612UW:
1279 case PCI_DEVICE_ID_ARTOP_AEC7612SUW:
1280 case ATP880_DEVID1:
1281 case ATP880_DEVID2:
1282 case ATP885_DEVID:
Randy Dunlapdc6a78f2006-06-27 22:01:28 -07001283 atpdev->chip_ver = 0x04;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 default:
1285 break;
1286 }
1287 base_io = pci_resource_start(pdev, 0);
1288 base_io &= 0xfffffff8;
Ondrej Zary6a3cebb2015-11-17 19:23:54 +01001289 atpdev->baseport = base_io;
Randy Dunlapdc6a78f2006-06-27 22:01:28 -07001290
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 if ((ent->device == ATP880_DEVID1)||(ent->device == ATP880_DEVID2)) {
Sergei Shtylyov7d7311c2012-03-14 22:04:30 +03001292 atpdev->chip_ver = pdev->revision;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0x80);//JCC082803
1294
Ondrej Zaryd804bb22015-11-17 19:24:07 +01001295 atpdev->ioport[0] = base_io + 0x40;
1296 atpdev->pciport[0] = base_io + 0x28;
1297
1298 host_id = atp_readb_base(atpdev, 0x39);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 host_id >>= 0x04;
1300
1301 printk(KERN_INFO " ACARD AEC-67160 PCI Ultra3 LVD Host Adapter: %d"
1302 " IO:%x, IRQ:%d.\n", count, base_io, pdev->irq);
Randy Dunlapdc6a78f2006-06-27 22:01:28 -07001303 atpdev->dev_id = ent->device;
1304 atpdev->host_id[0] = host_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
Ondrej Zaryd804bb22015-11-17 19:24:07 +01001306 atpdev->scam_on = atp_readb_base(atpdev, 0x22);
1307 atpdev->global_map[0] = atp_readb_base(atpdev, 0x35);
1308 atpdev->ultra_map[0] = atp_readw_base(atpdev, 0x3c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
1310 n = 0x3f09;
1311next_fblk_880:
1312 if (n >= 0x4000)
1313 goto flash_ok_880;
1314
1315 m = 0;
Ondrej Zaryd804bb22015-11-17 19:24:07 +01001316 atp_writew_base(atpdev, 0x34, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 n += 0x0002;
Ondrej Zaryd804bb22015-11-17 19:24:07 +01001318 if (atp_readb_base(atpdev, 0x30) == 0xff)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 goto flash_ok_880;
1320
Ondrej Zaryd804bb22015-11-17 19:24:07 +01001321 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x30);
1322 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x31);
1323 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x32);
1324 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x33);
1325 atp_writew_base(atpdev, 0x34, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 n += 0x0002;
Ondrej Zaryd804bb22015-11-17 19:24:07 +01001327 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x30);
1328 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x31);
1329 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x32);
1330 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x33);
1331 atp_writew_base(atpdev, 0x34, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 n += 0x0002;
Ondrej Zaryd804bb22015-11-17 19:24:07 +01001333 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x30);
1334 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x31);
1335 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x32);
1336 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x33);
1337 atp_writew_base(atpdev, 0x34, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 n += 0x0002;
Ondrej Zaryd804bb22015-11-17 19:24:07 +01001339 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x30);
1340 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x31);
1341 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x32);
1342 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x33);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 n += 0x0018;
1344 goto next_fblk_880;
1345flash_ok_880:
Ondrej Zaryd804bb22015-11-17 19:24:07 +01001346 atp_writew_base(atpdev, 0x34, 0);
Randy Dunlapdc6a78f2006-06-27 22:01:28 -07001347 atpdev->ultra_map[0] = 0;
1348 atpdev->async[0] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 for (k = 0; k < 16; k++) {
1350 n = 1;
1351 n = n << k;
Randy Dunlapdc6a78f2006-06-27 22:01:28 -07001352 if (atpdev->sp[0][k] > 1) {
1353 atpdev->ultra_map[0] |= n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 } else {
Randy Dunlapdc6a78f2006-06-27 22:01:28 -07001355 if (atpdev->sp[0][k] == 0)
1356 atpdev->async[0] |= n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 }
1358 }
Randy Dunlapdc6a78f2006-06-27 22:01:28 -07001359 atpdev->async[0] = ~(atpdev->async[0]);
Ondrej Zaryd804bb22015-11-17 19:24:07 +01001360 atp_writeb_base(atpdev, 0x35, atpdev->global_map[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361
1362 shpnt = scsi_host_alloc(&atp870u_template, sizeof(struct atp_unit));
1363 if (!shpnt)
Randy Dunlapdc6a78f2006-06-27 22:01:28 -07001364 goto err_nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
1366 p = (struct atp_unit *)&shpnt->hostdata;
1367
Randy Dunlapdc6a78f2006-06-27 22:01:28 -07001368 atpdev->host = shpnt;
1369 atpdev->pdev = pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 pci_set_drvdata(pdev, p);
Randy Dunlapdc6a78f2006-06-27 22:01:28 -07001371 memcpy(p, atpdev, sizeof(*atpdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 if (atp870u_init_tables(shpnt) < 0) {
1373 printk(KERN_ERR "Unable to allocate tables for Acard controller\n");
1374 goto unregister;
1375 }
1376
Thomas Gleixner1d6f3592006-07-01 19:29:42 -07001377 if (request_irq(pdev->irq, atp870u_intr_handle, IRQF_SHARED, "atp880i", shpnt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 printk(KERN_ERR "Unable to allocate IRQ%d for Acard controller.\n", pdev->irq);
1379 goto free_tables;
1380 }
1381
1382 spin_lock_irqsave(shpnt->host_lock, flags);
Ondrej Zaryd804bb22015-11-17 19:24:07 +01001383 k = atp_readb_base(p, 0x38) & 0x80;
1384 atp_writeb_base(p, 0x38, k);
1385 atp_writeb_base(p, 0x3b, 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 mdelay(32);
Ondrej Zaryd804bb22015-11-17 19:24:07 +01001387 atp_writeb_base(p, 0x3b, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 mdelay(32);
Ondrej Zaryd804bb22015-11-17 19:24:07 +01001389 atp_readb_io(p, 0, 0x1b);
1390 atp_readb_io(p, 0, 0x17);
Ondrej Zary6a1961b2015-11-17 19:24:10 +01001391
1392 atp_set_host_id(p, 0, host_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393
1394 tscam(shpnt);
Ondrej Zary4192a402015-11-17 19:24:06 +01001395 atp_is(p, 0, true, atp_readb_base(p, 0x3f) & 0x40);
Ondrej Zaryd804bb22015-11-17 19:24:07 +01001396 atp_writeb_base(p, 0x38, 0xb0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 shpnt->max_id = 16;
1398 shpnt->this_id = host_id;
1399 shpnt->unique_id = base_io;
1400 shpnt->io_port = base_io;
1401 shpnt->n_io_port = 0x60; /* Number of bytes of I/O space used */
1402 shpnt->irq = pdev->irq;
1403 } else if (ent->device == ATP885_DEVID) {
1404 printk(KERN_INFO " ACARD AEC-67162 PCI Ultra3 LVD Host Adapter: IO:%x, IRQ:%d.\n"
1405 , base_io, pdev->irq);
1406
Randy Dunlapdc6a78f2006-06-27 22:01:28 -07001407 atpdev->pdev = pdev;
1408 atpdev->dev_id = ent->device;
Randy Dunlapdc6a78f2006-06-27 22:01:28 -07001409 atpdev->ioport[0] = base_io + 0x80;
1410 atpdev->ioport[1] = base_io + 0xc0;
1411 atpdev->pciport[0] = base_io + 0x40;
1412 atpdev->pciport[1] = base_io + 0x50;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413
1414 shpnt = scsi_host_alloc(&atp870u_template, sizeof(struct atp_unit));
1415 if (!shpnt)
Randy Dunlapdc6a78f2006-06-27 22:01:28 -07001416 goto err_nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417
1418 p = (struct atp_unit *)&shpnt->hostdata;
1419
Randy Dunlapdc6a78f2006-06-27 22:01:28 -07001420 atpdev->host = shpnt;
1421 atpdev->pdev = pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 pci_set_drvdata(pdev, p);
Randy Dunlapdc6a78f2006-06-27 22:01:28 -07001423 memcpy(p, atpdev, sizeof(struct atp_unit));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424 if (atp870u_init_tables(shpnt) < 0)
1425 goto unregister;
1426
1427#ifdef ED_DBGP
1428 printk("request_irq() shpnt %p hostdata %p\n", shpnt, p);
1429#endif
Thomas Gleixner1d6f3592006-07-01 19:29:42 -07001430 if (request_irq(pdev->irq, atp870u_intr_handle, IRQF_SHARED, "atp870u", shpnt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 printk(KERN_ERR "Unable to allocate IRQ for Acard controller.\n");
1432 goto free_tables;
1433 }
1434
1435 spin_lock_irqsave(shpnt->host_lock, flags);
1436
Ondrej Zaryd804bb22015-11-17 19:24:07 +01001437 c = atp_readb_base(p, 0x29);
1438 atp_writeb_base(p, 0x29, c | 0x04);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
1440 n=0x1f80;
1441next_fblk_885:
1442 if (n >= 0x2000) {
1443 goto flash_ok_885;
1444 }
Ondrej Zaryd804bb22015-11-17 19:24:07 +01001445 atp_writew_base(p, 0x3c, n);
1446 if (atp_readl_base(p, 0x38) == 0xffffffff) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 goto flash_ok_885;
1448 }
1449 for (m=0; m < 2; m++) {
1450 p->global_map[m]= 0;
1451 for (k=0; k < 4; k++) {
Ondrej Zaryd804bb22015-11-17 19:24:07 +01001452 atp_writew_base(p, 0x3c, n++);
1453 ((unsigned long *)&setupdata[m][0])[k] = atp_readl_base(p, 0x38);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 }
1455 for (k=0; k < 4; k++) {
Ondrej Zaryd804bb22015-11-17 19:24:07 +01001456 atp_writew_base(p, 0x3c, n++);
1457 ((unsigned long *)&p->sp[m][0])[k] = atp_readl_base(p, 0x38);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 }
1459 n += 8;
1460 }
1461 goto next_fblk_885;
1462flash_ok_885:
1463#ifdef ED_DBGP
1464 printk( "Flash Read OK\n");
1465#endif
Ondrej Zaryd804bb22015-11-17 19:24:07 +01001466 c = atp_readb_base(p, 0x29);
1467 atp_writeb_base(p, 0x29, c & 0xfb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468 for (c=0;c < 2;c++) {
1469 p->ultra_map[c]=0;
1470 p->async[c] = 0;
1471 for (k=0; k < 16; k++) {
1472 n=1;
1473 n = n << k;
1474 if (p->sp[c][k] > 1) {
1475 p->ultra_map[c] |= n;
1476 } else {
1477 if (p->sp[c][k] == 0) {
1478 p->async[c] |= n;
1479 }
1480 }
1481 }
1482 p->async[c] = ~(p->async[c]);
1483
1484 if (p->global_map[c] == 0) {
1485 k=setupdata[c][1];
1486 if ((k & 0x40) != 0)
1487 p->global_map[c] |= 0x20;
1488 k &= 0x07;
1489 p->global_map[c] |= k;
1490 if ((setupdata[c][2] & 0x04) != 0)
1491 p->global_map[c] |= 0x08;
1492 p->host_id[c] = setupdata[c][0] & 0x07;
1493 }
1494 }
1495
Ondrej Zaryd804bb22015-11-17 19:24:07 +01001496 k = atp_readb_base(p, 0x28) & 0x8f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 k |= 0x10;
Ondrej Zaryd804bb22015-11-17 19:24:07 +01001498 atp_writeb_base(p, 0x28, k);
1499 atp_writeb_pci(p, 0, 1, 0x80);
1500 atp_writeb_pci(p, 1, 1, 0x80);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 mdelay(100);
Ondrej Zaryd804bb22015-11-17 19:24:07 +01001502 atp_writeb_pci(p, 0, 1, 0);
1503 atp_writeb_pci(p, 1, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504 mdelay(1000);
Ondrej Zaryd804bb22015-11-17 19:24:07 +01001505 atp_readb_io(p, 0, 0x1b);
1506 atp_readb_io(p, 0, 0x17);
1507 atp_readb_io(p, 1, 0x1b);
1508 atp_readb_io(p, 1, 0x17);
Ondrej Zary6a1961b2015-11-17 19:24:10 +01001509
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 k=p->host_id[0];
1511 if (k > 7)
1512 k = (k & 0x07) | 0x40;
Ondrej Zary6a1961b2015-11-17 19:24:10 +01001513 atp_set_host_id(p, 0, k);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 k=p->host_id[1];
1516 if (k > 7)
1517 k = (k & 0x07) | 0x40;
Ondrej Zary6a1961b2015-11-17 19:24:10 +01001518 atp_set_host_id(p, 1, k);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
Ondrej Zaryc4ad92b2015-11-17 19:24:12 +01001520 mdelay(600); /* this delay used to be called tscam_885() */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 printk(KERN_INFO " Scanning Channel A SCSI Device ...\n");
Ondrej Zary4192a402015-11-17 19:24:06 +01001522 atp_is(p, 0, true, atp_readb_io(p, 0, 0x1b) >> 7);
Ondrej Zaryfa50b302015-11-17 19:24:00 +01001523 atp_writeb_io(p, 0, 0x16, 0x80);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524 printk(KERN_INFO " Scanning Channel B SCSI Device ...\n");
Ondrej Zary4192a402015-11-17 19:24:06 +01001525 atp_is(p, 1, true, atp_readb_io(p, 1, 0x1b) >> 7);
Ondrej Zaryfa50b302015-11-17 19:24:00 +01001526 atp_writeb_io(p, 1, 0x16, 0x80);
Ondrej Zaryd804bb22015-11-17 19:24:07 +01001527 k = atp_readb_base(p, 0x28) & 0xcf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 k |= 0xc0;
Ondrej Zaryd804bb22015-11-17 19:24:07 +01001529 atp_writeb_base(p, 0x28, k);
1530 k = atp_readb_base(p, 0x1f) | 0x80;
1531 atp_writeb_base(p, 0x1f, k);
1532 k = atp_readb_base(p, 0x29) | 0x01;
1533 atp_writeb_base(p, 0x29, k);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534#ifdef ED_DBGP
1535 //printk("atp885: atp_host[0] 0x%p\n", atp_host[0]);
1536#endif
1537 shpnt->max_id = 16;
1538 shpnt->max_lun = (p->global_map[0] & 0x07) + 1;
1539 shpnt->max_channel = 1;
1540 shpnt->this_id = p->host_id[0];
1541 shpnt->unique_id = base_io;
1542 shpnt->io_port = base_io;
1543 shpnt->n_io_port = 0xff; /* Number of bytes of I/O space used */
1544 shpnt->irq = pdev->irq;
1545
1546 } else {
1547 error = pci_read_config_byte(pdev, 0x49, &host_id);
1548
1549 printk(KERN_INFO " ACARD AEC-671X PCI Ultra/W SCSI-2/3 Host Adapter: %d "
1550 "IO:%x, IRQ:%d.\n", count, base_io, pdev->irq);
1551
Randy Dunlapdc6a78f2006-06-27 22:01:28 -07001552 atpdev->ioport[0] = base_io;
1553 atpdev->pciport[0] = base_io + 0x20;
1554 atpdev->dev_id = ent->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 host_id &= 0x07;
Randy Dunlapdc6a78f2006-06-27 22:01:28 -07001556 atpdev->host_id[0] = host_id;
Ondrej Zaryd804bb22015-11-17 19:24:07 +01001557 atpdev->scam_on = atp_readb_pci(atpdev, 0, 2);
1558 atpdev->global_map[0] = atp_readb_base(atpdev, 0x2d);
1559 atpdev->ultra_map[0] = atp_readw_base(atpdev, 0x2e);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560
Randy Dunlapdc6a78f2006-06-27 22:01:28 -07001561 if (atpdev->ultra_map[0] == 0) {
1562 atpdev->scam_on = 0x00;
1563 atpdev->global_map[0] = 0x20;
1564 atpdev->ultra_map[0] = 0xffff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 }
1566
1567 shpnt = scsi_host_alloc(&atp870u_template, sizeof(struct atp_unit));
1568 if (!shpnt)
Randy Dunlapdc6a78f2006-06-27 22:01:28 -07001569 goto err_nomem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570
1571 p = (struct atp_unit *)&shpnt->hostdata;
1572
Randy Dunlapdc6a78f2006-06-27 22:01:28 -07001573 atpdev->host = shpnt;
1574 atpdev->pdev = pdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 pci_set_drvdata(pdev, p);
Randy Dunlapdc6a78f2006-06-27 22:01:28 -07001576 memcpy(p, atpdev, sizeof(*atpdev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 if (atp870u_init_tables(shpnt) < 0)
1578 goto unregister;
1579
Thomas Gleixner1d6f3592006-07-01 19:29:42 -07001580 if (request_irq(pdev->irq, atp870u_intr_handle, IRQF_SHARED, "atp870i", shpnt)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 printk(KERN_ERR "Unable to allocate IRQ%d for Acard controller.\n", pdev->irq);
1582 goto free_tables;
1583 }
1584
1585 spin_lock_irqsave(shpnt->host_lock, flags);
Ondrej Zaryd804bb22015-11-17 19:24:07 +01001586 if (atpdev->chip_ver > 0x07) /* check if atp876 chip then enable terminator */
1587 atp_writeb_base(p, 0x3e, 0x00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588
Ondrej Zaryd804bb22015-11-17 19:24:07 +01001589 k = (atp_readb_base(p, 0x3a) & 0xf3) | 0x10;
1590 atp_writeb_base(p, 0x3a, k);
1591 atp_writeb_base(p, 0x3a, k & 0xdf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 mdelay(32);
Ondrej Zaryd804bb22015-11-17 19:24:07 +01001593 atp_writeb_base(p, 0x3a, k);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 mdelay(32);
Ondrej Zary6a1961b2015-11-17 19:24:10 +01001595 atp_set_host_id(p, 0, host_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
1597 tscam(shpnt);
Ondrej Zaryd804bb22015-11-17 19:24:07 +01001598 atp_writeb_base(p, 0x3a, atp_readb_base(p, 0x3a) | 0x10);
Ondrej Zary4192a402015-11-17 19:24:06 +01001599 atp_is(p, 0, p->chip_ver == 4, 0);
Ondrej Zaryd804bb22015-11-17 19:24:07 +01001600 atp_writeb_base(p, 0x3a, atp_readb_base(p, 0x3a) & 0xef);
1601 atp_writeb_base(p, 0x3b, atp_readb_base(p, 0x3b) | 0x20);
Randy Dunlapdc6a78f2006-06-27 22:01:28 -07001602 if (atpdev->chip_ver == 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 shpnt->max_id = 16;
1604 else
Hannes Reinecke2b89dad2006-05-23 10:29:28 +02001605 shpnt->max_id = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 shpnt->this_id = host_id;
1607 shpnt->unique_id = base_io;
1608 shpnt->io_port = base_io;
1609 shpnt->n_io_port = 0x40; /* Number of bytes of I/O space used */
1610 shpnt->irq = pdev->irq;
1611 }
1612 spin_unlock_irqrestore(shpnt->host_lock, flags);
1613 if(ent->device==ATP885_DEVID) {
1614 if(!request_region(base_io, 0xff, "atp870u")) /* Register the IO ports that we use */
1615 goto request_io_fail;
1616 } else if((ent->device==ATP880_DEVID1)||(ent->device==ATP880_DEVID2)) {
1617 if(!request_region(base_io, 0x60, "atp870u")) /* Register the IO ports that we use */
1618 goto request_io_fail;
1619 } else {
1620 if(!request_region(base_io, 0x40, "atp870u")) /* Register the IO ports that we use */
1621 goto request_io_fail;
1622 }
1623 count++;
1624 if (scsi_add_host(shpnt, &pdev->dev))
1625 goto scsi_add_fail;
1626 scsi_scan_host(shpnt);
1627#ifdef ED_DBGP
1628 printk("atp870u_prob : exit\n");
1629#endif
1630 return 0;
1631
1632scsi_add_fail:
1633 printk("atp870u_prob:scsi_add_fail\n");
1634 if(ent->device==ATP885_DEVID) {
1635 release_region(base_io, 0xff);
1636 } else if((ent->device==ATP880_DEVID1)||(ent->device==ATP880_DEVID2)) {
1637 release_region(base_io, 0x60);
1638 } else {
1639 release_region(base_io, 0x40);
1640 }
1641request_io_fail:
1642 printk("atp870u_prob:request_io_fail\n");
1643 free_irq(pdev->irq, shpnt);
1644free_tables:
1645 printk("atp870u_prob:free_table\n");
1646 atp870u_free_tables(shpnt);
1647unregister:
1648 printk("atp870u_prob:unregister\n");
1649 scsi_host_put(shpnt);
1650 return -1;
Randy Dunlapdc6a78f2006-06-27 22:01:28 -07001651err_eio:
1652 kfree(atpdev);
1653 return -EIO;
1654err_nomem:
1655 kfree(atpdev);
1656 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657}
1658
1659/* The abort command does not leave the device in a clean state where
1660 it is available to be used again. Until this gets worked out, we will
1661 leave it commented out. */
1662
1663static int atp870u_abort(struct scsi_cmnd * SCpnt)
1664{
1665 unsigned char j, k, c;
1666 struct scsi_cmnd *workrequ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 struct atp_unit *dev;
1668 struct Scsi_Host *host;
1669 host = SCpnt->device->host;
1670
1671 dev = (struct atp_unit *)&host->hostdata;
Jeff Garzik422c0d62005-10-24 18:05:09 -04001672 c = scmd_channel(SCpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 printk(" atp870u: abort Channel = %x \n", c);
1674 printk("working=%x last_cmd=%x ", dev->working[c], dev->last_cmd[c]);
1675 printk(" quhdu=%x quendu=%x ", dev->quhd[c], dev->quend[c]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 for (j = 0; j < 0x18; j++) {
Ondrej Zary6a3cebb2015-11-17 19:23:54 +01001677 printk(" r%2x=%2x", j, atp_readb_io(dev, c, j));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 }
Ondrej Zary6a3cebb2015-11-17 19:23:54 +01001679 printk(" r1c=%2x", atp_readb_io(dev, c, 0x1c));
1680 printk(" r1f=%2x in_snd=%2x ", atp_readb_io(dev, c, 0x1f), dev->in_snd[c]);
1681 printk(" d00=%2x", atp_readb_pci(dev, c, 0x00));
1682 printk(" d02=%2x", atp_readb_pci(dev, c, 0x02));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 for(j=0;j<16;j++) {
1684 if (dev->id[c][j].curr_req != NULL) {
1685 workrequ = dev->id[c][j].curr_req;
1686 printk("\n que cdb= ");
1687 for (k=0; k < workrequ->cmd_len; k++) {
1688 printk(" %2x ",workrequ->cmnd[k]);
1689 }
1690 printk(" last_lenu= %x ",(unsigned int)dev->id[c][j].last_len);
1691 }
1692 }
1693 return SUCCESS;
1694}
1695
1696static const char *atp870u_info(struct Scsi_Host *notused)
1697{
1698 static char buffer[128];
1699
1700 strcpy(buffer, "ACARD AEC-6710/6712/67160 PCI Ultra/W/LVD SCSI-3 Adapter Driver V2.6+ac ");
1701
1702 return buffer;
1703}
1704
Al Virod773e422013-03-31 03:26:26 -04001705static int atp870u_show_info(struct seq_file *m, struct Scsi_Host *HBAptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706{
Rasmus Villemoes3d300792014-12-03 00:10:53 +01001707 seq_puts(m, "ACARD AEC-671X Driver Version: 2.6+ac\n\n"
1708 "Adapter Configuration:\n");
Al Virod773e422013-03-31 03:26:26 -04001709 seq_printf(m, " Base IO: %#.4lx\n", HBAptr->io_port);
1710 seq_printf(m, " IRQ: %d\n", HBAptr->irq);
1711 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712}
1713
1714
1715static int atp870u_biosparam(struct scsi_device *disk, struct block_device *dev,
1716 sector_t capacity, int *ip)
1717{
1718 int heads, sectors, cylinders;
1719
1720 heads = 64;
1721 sectors = 32;
1722 cylinders = (unsigned long)capacity / (heads * sectors);
1723 if (cylinders > 1024) {
1724 heads = 255;
1725 sectors = 63;
1726 cylinders = (unsigned long)capacity / (heads * sectors);
1727 }
1728 ip[0] = heads;
1729 ip[1] = sectors;
1730 ip[2] = cylinders;
1731
1732 return 0;
1733}
1734
1735static void atp870u_remove (struct pci_dev *pdev)
1736{
1737 struct atp_unit *devext = pci_get_drvdata(pdev);
1738 struct Scsi_Host *pshost = devext->host;
1739
1740
1741 scsi_remove_host(pshost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 free_irq(pshost->irq, pshost);
1743 release_region(pshost->io_port, pshost->n_io_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 atp870u_free_tables(pshost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 scsi_host_put(pshost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746}
1747MODULE_LICENSE("GPL");
1748
1749static struct scsi_host_template atp870u_template = {
1750 .module = THIS_MODULE,
1751 .name = "atp870u" /* name */,
1752 .proc_name = "atp870u",
Al Virod773e422013-03-31 03:26:26 -04001753 .show_info = atp870u_show_info,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 .info = atp870u_info /* info */,
1755 .queuecommand = atp870u_queuecommand /* queuecommand */,
1756 .eh_abort_handler = atp870u_abort /* abort */,
1757 .bios_param = atp870u_biosparam /* biosparm */,
1758 .can_queue = qcnt /* can_queue */,
1759 .this_id = 7 /* SCSI ID */,
1760 .sg_tablesize = ATP870U_SCATTER /*SG_ALL*/ /*SG_NONE*/,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 .use_clustering = ENABLE_CLUSTERING,
1762 .max_sectors = ATP870U_MAX_SECTORS,
1763};
1764
1765static struct pci_device_id atp870u_id_table[] = {
1766 { PCI_DEVICE(PCI_VENDOR_ID_ARTOP, ATP885_DEVID) },
1767 { PCI_DEVICE(PCI_VENDOR_ID_ARTOP, ATP880_DEVID1) },
1768 { PCI_DEVICE(PCI_VENDOR_ID_ARTOP, ATP880_DEVID2) },
1769 { PCI_DEVICE(PCI_VENDOR_ID_ARTOP, PCI_DEVICE_ID_ARTOP_AEC7610) },
1770 { PCI_DEVICE(PCI_VENDOR_ID_ARTOP, PCI_DEVICE_ID_ARTOP_AEC7612UW) },
1771 { PCI_DEVICE(PCI_VENDOR_ID_ARTOP, PCI_DEVICE_ID_ARTOP_AEC7612U) },
1772 { PCI_DEVICE(PCI_VENDOR_ID_ARTOP, PCI_DEVICE_ID_ARTOP_AEC7612S) },
1773 { PCI_DEVICE(PCI_VENDOR_ID_ARTOP, PCI_DEVICE_ID_ARTOP_AEC7612D) },
1774 { PCI_DEVICE(PCI_VENDOR_ID_ARTOP, PCI_DEVICE_ID_ARTOP_AEC7612SUW) },
1775 { PCI_DEVICE(PCI_VENDOR_ID_ARTOP, PCI_DEVICE_ID_ARTOP_8060) },
1776 { 0, },
1777};
1778
1779MODULE_DEVICE_TABLE(pci, atp870u_id_table);
1780
1781static struct pci_driver atp870u_driver = {
1782 .id_table = atp870u_id_table,
1783 .name = "atp870u",
1784 .probe = atp870u_probe,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08001785 .remove = atp870u_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786};
1787
Ondrej Zary1ccd7d62015-11-17 19:24:13 +01001788module_pci_driver(atp870u_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789
Ondrej Zary4192a402015-11-17 19:24:06 +01001790static void atp_is(struct atp_unit *dev, unsigned char c, bool wide_chip, unsigned char lvdmode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791{
Ondrej Zaryfa50b302015-11-17 19:24:00 +01001792 unsigned char i, j, k, rmb, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 unsigned short int m;
1794 static unsigned char mbuf[512];
Ondrej Zary80b52a72015-11-17 19:23:58 +01001795 static unsigned char satn[9] = { 0, 0, 0, 0, 0, 0, 0, 6, 6 };
1796 static unsigned char inqd[9] = { 0x12, 0, 0, 0, 0x24, 0, 0, 0x24, 6 };
1797 static unsigned char synn[6] = { 0x80, 1, 3, 1, 0x19, 0x0e };
1798 unsigned char synu[6] = { 0x80, 1, 3, 1, 0x0a, 0x0e };
1799 static unsigned char synw[6] = { 0x80, 1, 3, 1, 0x19, 0x0e };
Ondrej Zary460da9182015-11-17 19:24:03 +01001800 static unsigned char synw_870[6] = { 0x80, 1, 3, 1, 0x0c, 0x07 };
Ondrej Zary80b52a72015-11-17 19:23:58 +01001801 unsigned char synuw[6] = { 0x80, 1, 3, 1, 0x0a, 0x0e };
1802 static unsigned char wide[6] = { 0x80, 1, 2, 3, 1, 0 };
1803 static unsigned char u3[9] = { 0x80, 1, 6, 4, 0x09, 00, 0x0e, 0x01, 0x02 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 for (i = 0; i < 16; i++) {
Ondrej Zary197fb8d2015-11-17 19:24:02 +01001806 if (!wide_chip && (i > 7))
1807 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 m = 1;
1809 m = m << i;
1810 if ((m & dev->active_id[c]) != 0) {
1811 continue;
1812 }
1813 if (i == dev->host_id[c]) {
1814 printk(KERN_INFO " ID: %2d Host Adapter\n", dev->host_id[c]);
1815 continue;
1816 }
Ondrej Zary197fb8d2015-11-17 19:24:02 +01001817 atp_writeb_io(dev, c, 0x1b, wide_chip ? 0x01 : 0x00);
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01001818 atp_writeb_io(dev, c, 1, 0x08);
1819 atp_writeb_io(dev, c, 2, 0x7f);
1820 atp_writeb_io(dev, c, 3, satn[0]);
1821 atp_writeb_io(dev, c, 4, satn[1]);
1822 atp_writeb_io(dev, c, 5, satn[2]);
1823 atp_writeb_io(dev, c, 6, satn[3]);
1824 atp_writeb_io(dev, c, 7, satn[4]);
1825 atp_writeb_io(dev, c, 8, satn[5]);
1826 atp_writeb_io(dev, c, 0x0f, 0);
1827 atp_writeb_io(dev, c, 0x11, dev->id[c][i].devsp);
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01001828 atp_writeb_io(dev, c, 0x12, 0);
1829 atp_writeb_io(dev, c, 0x13, satn[6]);
1830 atp_writeb_io(dev, c, 0x14, satn[7]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 j = i;
1832 if ((j & 0x08) != 0) {
1833 j = (j & 0x07) | 0x40;
1834 }
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01001835 atp_writeb_io(dev, c, 0x15, j);
1836 atp_writeb_io(dev, c, 0x18, satn[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01001838 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 cpu_relax();
Ondrej Zary80b52a72015-11-17 19:23:58 +01001840
1841 if (atp_readb_io(dev, c, 0x17) != 0x11 && atp_readb_io(dev, c, 0x17) != 0x8e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 continue;
Ondrej Zary80b52a72015-11-17 19:23:58 +01001843
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01001844 while (atp_readb_io(dev, c, 0x17) != 0x8e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 cpu_relax();
Ondrej Zary80b52a72015-11-17 19:23:58 +01001846
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 dev->active_id[c] |= m;
1848
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01001849 atp_writeb_io(dev, c, 0x10, 0x30);
Ondrej Zary460da9182015-11-17 19:24:03 +01001850 if (dev->dev_id == ATP885_DEVID || dev->dev_id == ATP880_DEVID1 || dev->dev_id == ATP880_DEVID2)
1851 atp_writeb_io(dev, c, 0x14, 0x00);
1852 else /* result of is870() merge - is this a bug? */
1853 atp_writeb_io(dev, c, 0x04, 0x00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854
1855phase_cmd:
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01001856 atp_writeb_io(dev, c, 0x18, 0x08);
Ondrej Zary80b52a72015-11-17 19:23:58 +01001857
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01001858 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 cpu_relax();
Ondrej Zary80b52a72015-11-17 19:23:58 +01001860
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01001861 j = atp_readb_io(dev, c, 0x17);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 if (j != 0x16) {
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01001863 atp_writeb_io(dev, c, 0x10, 0x41);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 goto phase_cmd;
1865 }
1866sel_ok:
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01001867 atp_writeb_io(dev, c, 3, inqd[0]);
1868 atp_writeb_io(dev, c, 4, inqd[1]);
1869 atp_writeb_io(dev, c, 5, inqd[2]);
1870 atp_writeb_io(dev, c, 6, inqd[3]);
1871 atp_writeb_io(dev, c, 7, inqd[4]);
1872 atp_writeb_io(dev, c, 8, inqd[5]);
1873 atp_writeb_io(dev, c, 0x0f, 0);
1874 atp_writeb_io(dev, c, 0x11, dev->id[c][i].devsp);
1875 atp_writeb_io(dev, c, 0x12, 0);
1876 atp_writeb_io(dev, c, 0x13, inqd[6]);
1877 atp_writeb_io(dev, c, 0x14, inqd[7]);
1878 atp_writeb_io(dev, c, 0x18, inqd[8]);
Ondrej Zary80b52a72015-11-17 19:23:58 +01001879
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01001880 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 cpu_relax();
Ondrej Zary80b52a72015-11-17 19:23:58 +01001882
1883 if (atp_readb_io(dev, c, 0x17) != 0x11 && atp_readb_io(dev, c, 0x17) != 0x8e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 continue;
Ondrej Zary80b52a72015-11-17 19:23:58 +01001885
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01001886 while (atp_readb_io(dev, c, 0x17) != 0x8e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 cpu_relax();
Ondrej Zary80b52a72015-11-17 19:23:58 +01001888
Ondrej Zary197fb8d2015-11-17 19:24:02 +01001889 if (wide_chip)
1890 atp_writeb_io(dev, c, 0x1b, 0x00);
1891
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01001892 atp_writeb_io(dev, c, 0x18, 0x08);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 j = 0;
1894rd_inq_data:
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01001895 k = atp_readb_io(dev, c, 0x1f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 if ((k & 0x01) != 0) {
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01001897 mbuf[j++] = atp_readb_io(dev, c, 0x19);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 goto rd_inq_data;
1899 }
1900 if ((k & 0x80) == 0) {
1901 goto rd_inq_data;
1902 }
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01001903 j = atp_readb_io(dev, c, 0x17);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 if (j == 0x16) {
1905 goto inq_ok;
1906 }
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01001907 atp_writeb_io(dev, c, 0x10, 0x46);
1908 atp_writeb_io(dev, c, 0x12, 0);
1909 atp_writeb_io(dev, c, 0x13, 0);
1910 atp_writeb_io(dev, c, 0x14, 0);
1911 atp_writeb_io(dev, c, 0x18, 0x08);
Ondrej Zary80b52a72015-11-17 19:23:58 +01001912
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01001913 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 cpu_relax();
Ondrej Zary80b52a72015-11-17 19:23:58 +01001915
1916 if (atp_readb_io(dev, c, 0x17) != 0x16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 goto sel_ok;
Ondrej Zary80b52a72015-11-17 19:23:58 +01001918
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919inq_ok:
1920 mbuf[36] = 0;
Ondrej Zary80b52a72015-11-17 19:23:58 +01001921 printk(KERN_INFO " ID: %2d %s\n", i, &mbuf[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 dev->id[c][i].devtype = mbuf[0];
1923 rmb = mbuf[1];
1924 n = mbuf[7];
Ondrej Zary197fb8d2015-11-17 19:24:02 +01001925 if (!wide_chip)
1926 goto not_wide;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 if ((mbuf[7] & 0x60) == 0) {
1928 goto not_wide;
1929 }
Ondrej Zary197fb8d2015-11-17 19:24:02 +01001930 if (dev->dev_id == ATP885_DEVID || dev->dev_id == ATP880_DEVID1 || dev->dev_id == ATP880_DEVID2) {
1931 if ((i < 8) && ((dev->global_map[c] & 0x20) == 0))
1932 goto not_wide;
1933 } else { /* result of is870() merge - is this a bug? */
1934 if ((dev->global_map[c] & 0x20) == 0)
1935 goto not_wide;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 }
1937 if (lvdmode == 0) {
Ondrej Zary80b52a72015-11-17 19:23:58 +01001938 goto chg_wide;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 }
Ondrej Zary80b52a72015-11-17 19:23:58 +01001940 if (dev->sp[c][i] != 0x04) // force u2
1941 {
1942 goto chg_wide;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 }
1944
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01001945 atp_writeb_io(dev, c, 0x1b, 0x01);
1946 atp_writeb_io(dev, c, 3, satn[0]);
1947 atp_writeb_io(dev, c, 4, satn[1]);
1948 atp_writeb_io(dev, c, 5, satn[2]);
1949 atp_writeb_io(dev, c, 6, satn[3]);
1950 atp_writeb_io(dev, c, 7, satn[4]);
1951 atp_writeb_io(dev, c, 8, satn[5]);
1952 atp_writeb_io(dev, c, 0x0f, 0);
1953 atp_writeb_io(dev, c, 0x11, dev->id[c][i].devsp);
1954 atp_writeb_io(dev, c, 0x12, 0);
1955 atp_writeb_io(dev, c, 0x13, satn[6]);
1956 atp_writeb_io(dev, c, 0x14, satn[7]);
1957 atp_writeb_io(dev, c, 0x18, satn[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01001959 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 cpu_relax();
Ondrej Zary80b52a72015-11-17 19:23:58 +01001961
1962 if (atp_readb_io(dev, c, 0x17) != 0x11 && atp_readb_io(dev, c, 0x17) != 0x8e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 continue;
Ondrej Zary80b52a72015-11-17 19:23:58 +01001964
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01001965 while (atp_readb_io(dev, c, 0x17) != 0x8e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 cpu_relax();
Ondrej Zary80b52a72015-11-17 19:23:58 +01001967
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968try_u3:
1969 j = 0;
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01001970 atp_writeb_io(dev, c, 0x14, 0x09);
1971 atp_writeb_io(dev, c, 0x18, 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01001973 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0) {
1974 if ((atp_readb_io(dev, c, 0x1f) & 0x01) != 0)
1975 atp_writeb_io(dev, c, 0x19, u3[j++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 cpu_relax();
1977 }
Ondrej Zary80b52a72015-11-17 19:23:58 +01001978
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01001979 while ((atp_readb_io(dev, c, 0x17) & 0x80) == 0x00)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 cpu_relax();
Ondrej Zary80b52a72015-11-17 19:23:58 +01001981
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01001982 j = atp_readb_io(dev, c, 0x17) & 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 if (j == 0x0f) {
1984 goto u3p_in;
1985 }
1986 if (j == 0x0a) {
1987 goto u3p_cmd;
1988 }
1989 if (j == 0x0e) {
1990 goto try_u3;
1991 }
1992 continue;
1993u3p_out:
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01001994 atp_writeb_io(dev, c, 0x18, 0x20);
1995 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0) {
1996 if ((atp_readb_io(dev, c, 0x1f) & 0x01) != 0)
1997 atp_writeb_io(dev, c, 0x19, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 cpu_relax();
1999 }
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002000 j = atp_readb_io(dev, c, 0x17) & 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 if (j == 0x0f) {
2002 goto u3p_in;
2003 }
2004 if (j == 0x0a) {
2005 goto u3p_cmd;
2006 }
2007 if (j == 0x0e) {
2008 goto u3p_out;
2009 }
2010 continue;
2011u3p_in:
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002012 atp_writeb_io(dev, c, 0x14, 0x09);
2013 atp_writeb_io(dev, c, 0x18, 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 k = 0;
2015u3p_in1:
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002016 j = atp_readb_io(dev, c, 0x1f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 if ((j & 0x01) != 0) {
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002018 mbuf[k++] = atp_readb_io(dev, c, 0x19);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 goto u3p_in1;
2020 }
2021 if ((j & 0x80) == 0x00) {
2022 goto u3p_in1;
2023 }
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002024 j = atp_readb_io(dev, c, 0x17) & 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 if (j == 0x0f) {
2026 goto u3p_in;
2027 }
2028 if (j == 0x0a) {
2029 goto u3p_cmd;
2030 }
2031 if (j == 0x0e) {
2032 goto u3p_out;
2033 }
2034 continue;
2035u3p_cmd:
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002036 atp_writeb_io(dev, c, 0x10, 0x30);
2037 atp_writeb_io(dev, c, 0x14, 0x00);
2038 atp_writeb_io(dev, c, 0x18, 0x08);
Ondrej Zary80b52a72015-11-17 19:23:58 +01002039
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002040 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00);
Ondrej Zary80b52a72015-11-17 19:23:58 +01002041
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002042 j = atp_readb_io(dev, c, 0x17);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 if (j != 0x16) {
2044 if (j == 0x4e) {
2045 goto u3p_out;
2046 }
2047 continue;
2048 }
2049 if (mbuf[0] != 0x01) {
2050 goto chg_wide;
2051 }
2052 if (mbuf[1] != 0x06) {
2053 goto chg_wide;
2054 }
2055 if (mbuf[2] != 0x04) {
2056 goto chg_wide;
2057 }
2058 if (mbuf[3] == 0x09) {
2059 m = 1;
2060 m = m << i;
2061 dev->wide_id[c] |= m;
2062 dev->id[c][i].devsp = 0xce;
2063#ifdef ED_DBGP
2064 printk("dev->id[%2d][%2d].devsp = %2x\n",c,i,dev->id[c][i].devsp);
2065#endif
2066 continue;
2067 }
2068chg_wide:
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002069 atp_writeb_io(dev, c, 0x1b, 0x01);
2070 atp_writeb_io(dev, c, 3, satn[0]);
2071 atp_writeb_io(dev, c, 4, satn[1]);
2072 atp_writeb_io(dev, c, 5, satn[2]);
2073 atp_writeb_io(dev, c, 6, satn[3]);
2074 atp_writeb_io(dev, c, 7, satn[4]);
2075 atp_writeb_io(dev, c, 8, satn[5]);
2076 atp_writeb_io(dev, c, 0x0f, 0);
2077 atp_writeb_io(dev, c, 0x11, dev->id[c][i].devsp);
2078 atp_writeb_io(dev, c, 0x12, 0);
2079 atp_writeb_io(dev, c, 0x13, satn[6]);
2080 atp_writeb_io(dev, c, 0x14, satn[7]);
2081 atp_writeb_io(dev, c, 0x18, satn[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002083 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 cpu_relax();
Ondrej Zary80b52a72015-11-17 19:23:58 +01002085
2086 if (atp_readb_io(dev, c, 0x17) != 0x11 && atp_readb_io(dev, c, 0x17) != 0x8e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 continue;
Ondrej Zary80b52a72015-11-17 19:23:58 +01002088
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002089 while (atp_readb_io(dev, c, 0x17) != 0x8e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 cpu_relax();
Ondrej Zary80b52a72015-11-17 19:23:58 +01002091
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092try_wide:
2093 j = 0;
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002094 atp_writeb_io(dev, c, 0x14, 0x05);
2095 atp_writeb_io(dev, c, 0x18, 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002097 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0) {
2098 if ((atp_readb_io(dev, c, 0x1f) & 0x01) != 0)
2099 atp_writeb_io(dev, c, 0x19, wide[j++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 cpu_relax();
2101 }
Ondrej Zary80b52a72015-11-17 19:23:58 +01002102
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002103 while ((atp_readb_io(dev, c, 0x17) & 0x80) == 0x00)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 cpu_relax();
Ondrej Zary80b52a72015-11-17 19:23:58 +01002105
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002106 j = atp_readb_io(dev, c, 0x17) & 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 if (j == 0x0f) {
2108 goto widep_in;
2109 }
2110 if (j == 0x0a) {
2111 goto widep_cmd;
2112 }
2113 if (j == 0x0e) {
2114 goto try_wide;
2115 }
2116 continue;
2117widep_out:
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002118 atp_writeb_io(dev, c, 0x18, 0x20);
2119 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0) {
2120 if ((atp_readb_io(dev, c, 0x1f) & 0x01) != 0)
2121 atp_writeb_io(dev, c, 0x19, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 cpu_relax();
2123 }
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002124 j = atp_readb_io(dev, c, 0x17) & 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 if (j == 0x0f) {
2126 goto widep_in;
2127 }
2128 if (j == 0x0a) {
2129 goto widep_cmd;
2130 }
2131 if (j == 0x0e) {
2132 goto widep_out;
2133 }
2134 continue;
2135widep_in:
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002136 atp_writeb_io(dev, c, 0x14, 0xff);
2137 atp_writeb_io(dev, c, 0x18, 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 k = 0;
2139widep_in1:
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002140 j = atp_readb_io(dev, c, 0x1f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 if ((j & 0x01) != 0) {
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002142 mbuf[k++] = atp_readb_io(dev, c, 0x19);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143 goto widep_in1;
2144 }
2145 if ((j & 0x80) == 0x00) {
2146 goto widep_in1;
2147 }
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002148 j = atp_readb_io(dev, c, 0x17) & 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 if (j == 0x0f) {
2150 goto widep_in;
2151 }
2152 if (j == 0x0a) {
2153 goto widep_cmd;
2154 }
2155 if (j == 0x0e) {
2156 goto widep_out;
2157 }
2158 continue;
2159widep_cmd:
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002160 atp_writeb_io(dev, c, 0x10, 0x30);
2161 atp_writeb_io(dev, c, 0x14, 0x00);
2162 atp_writeb_io(dev, c, 0x18, 0x08);
Ondrej Zary80b52a72015-11-17 19:23:58 +01002163
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002164 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 cpu_relax();
Ondrej Zary80b52a72015-11-17 19:23:58 +01002166
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002167 j = atp_readb_io(dev, c, 0x17);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 if (j != 0x16) {
2169 if (j == 0x4e) {
2170 goto widep_out;
2171 }
2172 continue;
2173 }
2174 if (mbuf[0] != 0x01) {
2175 goto not_wide;
2176 }
2177 if (mbuf[1] != 0x02) {
2178 goto not_wide;
2179 }
2180 if (mbuf[2] != 0x03) {
2181 goto not_wide;
2182 }
2183 if (mbuf[3] != 0x01) {
2184 goto not_wide;
2185 }
2186 m = 1;
2187 m = m << i;
2188 dev->wide_id[c] |= m;
2189not_wide:
Ondrej Zary80b52a72015-11-17 19:23:58 +01002190 if ((dev->id[c][i].devtype == 0x00) || (dev->id[c][i].devtype == 0x07) || ((dev->id[c][i].devtype == 0x05) && ((n & 0x10) != 0))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 m = 1;
2192 m = m << i;
2193 if ((dev->async[c] & m) != 0) {
Ondrej Zary80b52a72015-11-17 19:23:58 +01002194 goto set_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 }
2196 }
2197 continue;
2198set_sync:
Ondrej Zary460da9182015-11-17 19:24:03 +01002199 if ((dev->dev_id != ATP885_DEVID && dev->dev_id != ATP880_DEVID1 && dev->dev_id != ATP880_DEVID2) || (dev->sp[c][i] == 0x02)) {
Ondrej Zary80b52a72015-11-17 19:23:58 +01002200 synu[4] = 0x0c;
2201 synuw[4] = 0x0c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 } else {
Ondrej Zary80b52a72015-11-17 19:23:58 +01002203 if (dev->sp[c][i] >= 0x03) {
2204 synu[4] = 0x0a;
2205 synuw[4] = 0x0a;
2206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 j = 0;
2209 if ((m & dev->wide_id[c]) != 0) {
2210 j |= 0x01;
2211 }
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002212 atp_writeb_io(dev, c, 0x1b, j);
2213 atp_writeb_io(dev, c, 3, satn[0]);
2214 atp_writeb_io(dev, c, 4, satn[1]);
2215 atp_writeb_io(dev, c, 5, satn[2]);
2216 atp_writeb_io(dev, c, 6, satn[3]);
2217 atp_writeb_io(dev, c, 7, satn[4]);
2218 atp_writeb_io(dev, c, 8, satn[5]);
2219 atp_writeb_io(dev, c, 0x0f, 0);
2220 atp_writeb_io(dev, c, 0x11, dev->id[c][i].devsp);
2221 atp_writeb_io(dev, c, 0x12, 0);
2222 atp_writeb_io(dev, c, 0x13, satn[6]);
2223 atp_writeb_io(dev, c, 0x14, satn[7]);
2224 atp_writeb_io(dev, c, 0x18, satn[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002226 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 cpu_relax();
Ondrej Zary80b52a72015-11-17 19:23:58 +01002228
2229 if (atp_readb_io(dev, c, 0x17) != 0x11 && atp_readb_io(dev, c, 0x17) != 0x8e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 continue;
Ondrej Zary80b52a72015-11-17 19:23:58 +01002231
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002232 while (atp_readb_io(dev, c, 0x17) != 0x8e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 cpu_relax();
Ondrej Zary80b52a72015-11-17 19:23:58 +01002234
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235try_sync:
2236 j = 0;
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002237 atp_writeb_io(dev, c, 0x14, 0x06);
2238 atp_writeb_io(dev, c, 0x18, 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002240 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0) {
2241 if ((atp_readb_io(dev, c, 0x1f) & 0x01) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 if ((m & dev->wide_id[c]) != 0) {
Ondrej Zary460da9182015-11-17 19:24:03 +01002243 if (dev->dev_id == ATP885_DEVID || dev->dev_id == ATP880_DEVID1 || dev->dev_id == ATP880_DEVID2) {
2244 if ((m & dev->ultra_map[c]) != 0) {
2245 atp_writeb_io(dev, c, 0x19, synuw[j++]);
2246 } else {
2247 atp_writeb_io(dev, c, 0x19, synw[j++]);
2248 }
2249 } else
2250 atp_writeb_io(dev, c, 0x19, synw_870[j++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 } else {
2252 if ((m & dev->ultra_map[c]) != 0) {
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002253 atp_writeb_io(dev, c, 0x19, synu[j++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 } else {
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002255 atp_writeb_io(dev, c, 0x19, synn[j++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 }
2257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 }
2259 }
Ondrej Zary80b52a72015-11-17 19:23:58 +01002260
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002261 while ((atp_readb_io(dev, c, 0x17) & 0x80) == 0x00)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 cpu_relax();
Ondrej Zary80b52a72015-11-17 19:23:58 +01002263
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002264 j = atp_readb_io(dev, c, 0x17) & 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 if (j == 0x0f) {
2266 goto phase_ins;
2267 }
2268 if (j == 0x0a) {
2269 goto phase_cmds;
2270 }
2271 if (j == 0x0e) {
2272 goto try_sync;
2273 }
2274 continue;
2275phase_outs:
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002276 atp_writeb_io(dev, c, 0x18, 0x20);
2277 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00) {
2278 if ((atp_readb_io(dev, c, 0x1f) & 0x01) != 0x00)
2279 atp_writeb_io(dev, c, 0x19, 0x00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 cpu_relax();
2281 }
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002282 j = atp_readb_io(dev, c, 0x17);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 if (j == 0x85) {
2284 goto tar_dcons;
2285 }
2286 j &= 0x0f;
2287 if (j == 0x0f) {
2288 goto phase_ins;
2289 }
2290 if (j == 0x0a) {
2291 goto phase_cmds;
2292 }
2293 if (j == 0x0e) {
2294 goto phase_outs;
2295 }
2296 continue;
2297phase_ins:
Ondrej Zary460da9182015-11-17 19:24:03 +01002298 if (dev->dev_id == ATP885_DEVID || dev->dev_id == ATP880_DEVID1 || dev->dev_id == ATP880_DEVID2)
2299 atp_writeb_io(dev, c, 0x14, 0x06);
2300 else
2301 atp_writeb_io(dev, c, 0x14, 0xff);
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002302 atp_writeb_io(dev, c, 0x18, 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 k = 0;
2304phase_ins1:
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002305 j = atp_readb_io(dev, c, 0x1f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 if ((j & 0x01) != 0x00) {
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002307 mbuf[k++] = atp_readb_io(dev, c, 0x19);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 goto phase_ins1;
2309 }
2310 if ((j & 0x80) == 0x00) {
2311 goto phase_ins1;
2312 }
Ondrej Zary80b52a72015-11-17 19:23:58 +01002313
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002314 while ((atp_readb_io(dev, c, 0x17) & 0x80) == 0x00);
Ondrej Zary80b52a72015-11-17 19:23:58 +01002315
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002316 j = atp_readb_io(dev, c, 0x17);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 if (j == 0x85) {
2318 goto tar_dcons;
2319 }
2320 j &= 0x0f;
2321 if (j == 0x0f) {
2322 goto phase_ins;
2323 }
2324 if (j == 0x0a) {
2325 goto phase_cmds;
2326 }
2327 if (j == 0x0e) {
2328 goto phase_outs;
2329 }
2330 continue;
2331phase_cmds:
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002332 atp_writeb_io(dev, c, 0x10, 0x30);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333tar_dcons:
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002334 atp_writeb_io(dev, c, 0x14, 0x00);
2335 atp_writeb_io(dev, c, 0x18, 0x08);
Ondrej Zary80b52a72015-11-17 19:23:58 +01002336
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002337 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 cpu_relax();
Ondrej Zary80b52a72015-11-17 19:23:58 +01002339
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002340 j = atp_readb_io(dev, c, 0x17);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 if (j != 0x16) {
2342 continue;
2343 }
2344 if (mbuf[0] != 0x01) {
2345 continue;
2346 }
2347 if (mbuf[1] != 0x03) {
2348 continue;
2349 }
2350 if (mbuf[4] == 0x00) {
2351 continue;
2352 }
2353 if (mbuf[3] > 0x64) {
2354 continue;
2355 }
Ondrej Zary460da9182015-11-17 19:24:03 +01002356 if (dev->dev_id == ATP885_DEVID || dev->dev_id == ATP880_DEVID1 || dev->dev_id == ATP880_DEVID2) {
2357 if (mbuf[4] > 0x0e) {
2358 mbuf[4] = 0x0e;
2359 }
2360 } else {
2361 if (mbuf[4] > 0x0c) {
2362 mbuf[4] = 0x0c;
2363 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 }
2365 dev->id[c][i].devsp = mbuf[4];
Ondrej Zary460da9182015-11-17 19:24:03 +01002366 if (dev->dev_id == ATP885_DEVID || dev->dev_id == ATP880_DEVID1 || dev->dev_id == ATP880_DEVID2)
2367 if (mbuf[3] < 0x0c) {
2368 j = 0xb0;
2369 goto set_syn_ok;
2370 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 if ((mbuf[3] < 0x0d) && (rmb == 0)) {
2372 j = 0xa0;
2373 goto set_syn_ok;
2374 }
2375 if (mbuf[3] < 0x1a) {
2376 j = 0x20;
2377 goto set_syn_ok;
2378 }
2379 if (mbuf[3] < 0x33) {
2380 j = 0x40;
2381 goto set_syn_ok;
2382 }
2383 if (mbuf[3] < 0x4c) {
2384 j = 0x50;
2385 goto set_syn_ok;
2386 }
2387 j = 0x60;
Ondrej Zary80b52a72015-11-17 19:23:58 +01002388set_syn_ok:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 dev->id[c][i].devsp = (dev->id[c][i].devsp & 0x0f) | j;
Ondrej Zary80b52a72015-11-17 19:23:58 +01002390#ifdef ED_DBGP
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 printk("dev->id[%2d][%2d].devsp = %2x\n",c,i,dev->id[c][i].devsp);
2392#endif
2393 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394}