blob: 2a748af269c2c220f4b4fe211b24d5e7b8d7b0b8 [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Copyright (C) 1997 Wu Ching Chen
4 * 2.1.x update (C) 1998 Krzysztof G. Baranowski
Alan Coxfa195af2008-10-27 15:16:36 +00005 * 2.5.x update (C) 2002 Red Hat
6 * 2.6.x update (C) 2004 Red Hat
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
8 * Marcelo Tosatti <marcelo@conectiva.com.br> : SMP fixes
9 *
10 * Wu Ching Chen : NULL pointer fixes 2000/06/02
11 * support atp876 chip
12 * enable 32 bit fifo transfer
13 * support cdrom & remove device run ultra speed
14 * fix disconnect bug 2000/12/21
15 * support atp880 chip lvd u160 2001/05/15
16 * fix prd table bug 2001/09/12 (7.1)
17 *
18 * atp885 support add by ACARD Hao Ping Lian 2005/01/05
19 */
20#include <linux/module.h>
21#include <linux/init.h>
22#include <linux/interrupt.h>
23#include <linux/kernel.h>
24#include <linux/types.h>
25#include <linux/string.h>
26#include <linux/ioport.h>
27#include <linux/delay.h>
28#include <linux/proc_fs.h>
29#include <linux/spinlock.h>
30#include <linux/pci.h>
31#include <linux/blkdev.h>
Matthias Gehre910638a2006-03-28 01:56:48 -080032#include <linux/dma-mapping.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <asm/io.h>
35
36#include <scsi/scsi.h>
37#include <scsi/scsi_cmnd.h>
38#include <scsi/scsi_device.h>
39#include <scsi/scsi_host.h>
40
41#include "atp870u.h"
42
Bart Van Asschef44e1c62023-03-22 12:54:24 -070043static const struct scsi_host_template atp870u_template;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044static void send_s870(struct atp_unit *dev,unsigned char c);
Hannes Reineckebcd5c592021-01-13 10:04:30 +010045static void atp_is(struct atp_unit *dev, unsigned char c, bool wide_chip,
46 unsigned char lvdmode);
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Ondrej Zary6a3cebb2015-11-17 19:23:54 +010048static inline void atp_writeb_base(struct atp_unit *atp, u8 reg, u8 val)
49{
50 outb(val, atp->baseport + reg);
51}
52
Ondrej Zaryd804bb22015-11-17 19:24:07 +010053static inline void atp_writew_base(struct atp_unit *atp, u8 reg, u16 val)
54{
55 outw(val, atp->baseport + reg);
56}
57
Ondrej Zary6a3cebb2015-11-17 19:23:54 +010058static inline void atp_writeb_io(struct atp_unit *atp, u8 channel, u8 reg, u8 val)
59{
60 outb(val, atp->ioport[channel] + reg);
61}
62
63static inline void atp_writew_io(struct atp_unit *atp, u8 channel, u8 reg, u16 val)
64{
65 outw(val, atp->ioport[channel] + reg);
66}
67
68static inline void atp_writeb_pci(struct atp_unit *atp, u8 channel, u8 reg, u8 val)
69{
70 outb(val, atp->pciport[channel] + reg);
71}
72
73static inline void atp_writel_pci(struct atp_unit *atp, u8 channel, u8 reg, u32 val)
74{
75 outl(val, atp->pciport[channel] + reg);
76}
77
78static inline u8 atp_readb_base(struct atp_unit *atp, u8 reg)
79{
80 return inb(atp->baseport + reg);
81}
82
Ondrej Zaryd804bb22015-11-17 19:24:07 +010083static inline u16 atp_readw_base(struct atp_unit *atp, u8 reg)
84{
85 return inw(atp->baseport + reg);
86}
87
88static inline u32 atp_readl_base(struct atp_unit *atp, u8 reg)
89{
90 return inl(atp->baseport + reg);
91}
92
Ondrej Zary6a3cebb2015-11-17 19:23:54 +010093static inline u8 atp_readb_io(struct atp_unit *atp, u8 channel, u8 reg)
94{
95 return inb(atp->ioport[channel] + reg);
96}
97
98static inline u16 atp_readw_io(struct atp_unit *atp, u8 channel, u8 reg)
99{
100 return inw(atp->ioport[channel] + reg);
101}
102
103static inline u8 atp_readb_pci(struct atp_unit *atp, u8 channel, u8 reg)
104{
105 return inb(atp->pciport[channel] + reg);
106}
107
Ondrej Zaryb922a442015-11-17 19:24:21 +0100108static inline bool is880(struct atp_unit *atp)
109{
110 return atp->pdev->device == ATP880_DEVID1 ||
111 atp->pdev->device == ATP880_DEVID2;
112}
113
114static inline bool is885(struct atp_unit *atp)
115{
116 return atp->pdev->device == ATP885_DEVID;
117}
118
David Howells7d12e782006-10-05 14:55:46 +0100119static irqreturn_t atp870u_intr_handle(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120{
121 unsigned long flags;
Ondrej Zarybc0fe4c2015-11-17 19:23:47 +0100122 unsigned short int id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 unsigned char i, j, c, target_id, lun,cmdp;
124 unsigned char *prd;
125 struct scsi_cmnd *workreq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 unsigned long adrcnt, k;
127#ifdef ED_DBGP
128 unsigned long l;
129#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 struct Scsi_Host *host = dev_id;
131 struct atp_unit *dev = (struct atp_unit *)&host->hostdata;
132
133 for (c = 0; c < 2; c++) {
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100134 j = atp_readb_io(dev, c, 0x1f);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 if ((j & 0x80) != 0)
Ondrej Zary78614ec2015-11-17 19:23:49 +0100136 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 dev->in_int[c] = 0;
138 }
Ondrej Zary78614ec2015-11-17 19:23:49 +0100139 if ((j & 0x80) == 0)
140 return IRQ_NONE;
Hannes Reineckebcd5c592021-01-13 10:04:30 +0100141#ifdef ED_DBGP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 printk("atp870u_intr_handle enter\n");
Hannes Reineckebcd5c592021-01-13 10:04:30 +0100143#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 dev->in_int[c] = 1;
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100145 cmdp = atp_readb_io(dev, c, 0x10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 if (dev->working[c] != 0) {
Ondrej Zaryb922a442015-11-17 19:24:21 +0100147 if (is885(dev)) {
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100148 if ((atp_readb_io(dev, c, 0x16) & 0x80) == 0)
Hannes Reineckebcd5c592021-01-13 10:04:30 +0100149 atp_writeb_io(dev, c, 0x16,
150 (atp_readb_io(dev, c, 0x16) | 0x80));
151 }
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100152 if ((atp_readb_pci(dev, c, 0x00) & 0x08) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 for (k=0; k < 1000; k++) {
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100155 if ((atp_readb_pci(dev, c, 2) & 0x08) == 0)
Ondrej Zary78614ec2015-11-17 19:23:49 +0100156 break;
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100157 if ((atp_readb_pci(dev, c, 2) & 0x01) == 0)
Ondrej Zary78614ec2015-11-17 19:23:49 +0100158 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 }
160 }
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100161 atp_writeb_pci(dev, c, 0, 0x00);
Hannes Reineckebcd5c592021-01-13 10:04:30 +0100162
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100163 i = atp_readb_io(dev, c, 0x17);
Hannes Reineckebcd5c592021-01-13 10:04:30 +0100164
Ondrej Zaryb922a442015-11-17 19:24:21 +0100165 if (is885(dev))
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100166 atp_writeb_pci(dev, c, 2, 0x06);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100168 target_id = atp_readb_io(dev, c, 0x15);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170 /*
171 * Remap wide devices onto id numbers
172 */
173
174 if ((target_id & 0x40) != 0) {
175 target_id = (target_id & 0x07) | 0x08;
176 } else {
177 target_id &= 0x07;
178 }
179
180 if ((j & 0x40) != 0) {
181 if (dev->last_cmd[c] == 0xff) {
182 dev->last_cmd[c] = target_id;
183 }
184 dev->last_cmd[c] |= 0x40;
185 }
Ondrej Zaryb922a442015-11-17 19:24:21 +0100186 if (is885(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 dev->r1f[c][target_id] |= j;
188#ifdef ED_DBGP
189 printk("atp870u_intr_handle status = %x\n",i);
Hannes Reineckebcd5c592021-01-13 10:04:30 +0100190#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 if (i == 0x85) {
192 if ((dev->last_cmd[c] & 0xf0) != 0x40) {
193 dev->last_cmd[c] = 0xff;
194 }
Ondrej Zaryb922a442015-11-17 19:24:21 +0100195 if (is885(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 adrcnt = 0;
Hannes Reineckebcd5c592021-01-13 10:04:30 +0100197 ((unsigned char *) &adrcnt)[2] =
198 atp_readb_io(dev, c, 0x12);
199 ((unsigned char *) &adrcnt)[1] =
200 atp_readb_io(dev, c, 0x13);
201 ((unsigned char *) &adrcnt)[0] =
202 atp_readb_io(dev, c, 0x14);
Colin Ian King22c07382019-01-22 15:10:30 +0000203 if (dev->id[c][target_id].last_len != adrcnt) {
204 k = dev->id[c][target_id].last_len;
Hannes Reineckebcd5c592021-01-13 10:04:30 +0100205 k -= adrcnt;
206 dev->id[c][target_id].tran_len = k;
Colin Ian King22c07382019-01-22 15:10:30 +0000207 dev->id[c][target_id].last_len = adrcnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 }
209#ifdef ED_DBGP
Hannes Reineckebcd5c592021-01-13 10:04:30 +0100210 printk("dev->id[c][target_id].last_len = %d "
211 "dev->id[c][target_id].tran_len = %d\n",
212 dev->id[c][target_id].last_len,
213 dev->id[c][target_id].tran_len);
214#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 }
216
217 /*
218 * Flip wide
Hannes Reineckebcd5c592021-01-13 10:04:30 +0100219 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 if (dev->wide_id[c] != 0) {
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100221 atp_writeb_io(dev, c, 0x1b, 0x01);
222 while ((atp_readb_io(dev, c, 0x1b) & 0x01) != 0x01)
223 atp_writeb_io(dev, c, 0x1b, 0x01);
Hannes Reineckebcd5c592021-01-13 10:04:30 +0100224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 /*
226 * Issue more commands
227 */
Hannes Reineckebcd5c592021-01-13 10:04:30 +0100228 spin_lock_irqsave(dev->host->host_lock, flags);
229 if (((dev->quhd[c] != dev->quend[c]) ||
230 (dev->last_cmd[c] != 0xff)) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 (dev->in_snd[c] == 0)) {
232#ifdef ED_DBGP
233 printk("Call sent_s870\n");
Hannes Reineckebcd5c592021-01-13 10:04:30 +0100234#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 send_s870(dev,c);
236 }
237 spin_unlock_irqrestore(dev->host->host_lock, flags);
238 /*
239 * Done
240 */
241 dev->in_int[c] = 0;
242#ifdef ED_DBGP
243 printk("Status 0x85 return\n");
Hannes Reineckebcd5c592021-01-13 10:04:30 +0100244#endif
Ondrej Zary78614ec2015-11-17 19:23:49 +0100245 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 }
247
248 if (i == 0x40) {
249 dev->last_cmd[c] |= 0x40;
250 dev->in_int[c] = 0;
Ondrej Zary78614ec2015-11-17 19:23:49 +0100251 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 }
253
254 if (i == 0x21) {
255 if ((dev->last_cmd[c] & 0xf0) != 0x40) {
256 dev->last_cmd[c] = 0xff;
257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 adrcnt = 0;
Hannes Reineckebcd5c592021-01-13 10:04:30 +0100259 ((unsigned char *) &adrcnt)[2] =
260 atp_readb_io(dev, c, 0x12);
261 ((unsigned char *) &adrcnt)[1] =
262 atp_readb_io(dev, c, 0x13);
263 ((unsigned char *) &adrcnt)[0] =
264 atp_readb_io(dev, c, 0x14);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 k = dev->id[c][target_id].last_len;
266 k -= adrcnt;
267 dev->id[c][target_id].tran_len = k;
268 dev->id[c][target_id].last_len = adrcnt;
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100269 atp_writeb_io(dev, c, 0x10, 0x41);
270 atp_writeb_io(dev, c, 0x18, 0x08);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 dev->in_int[c] = 0;
Ondrej Zary78614ec2015-11-17 19:23:49 +0100272 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 }
274
Ondrej Zaryb922a442015-11-17 19:24:21 +0100275 if (is885(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 if ((i == 0x4c) || (i == 0x4d) || (i == 0x8c) || (i == 0x8d)) {
Hannes Reineckebcd5c592021-01-13 10:04:30 +0100277 if ((i == 0x4c) || (i == 0x8c))
278 i=0x48;
279 else
280 i=0x49;
281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 }
283 if ((i == 0x80) || (i == 0x8f)) {
284#ifdef ED_DBGP
285 printk(KERN_DEBUG "Device reselect\n");
Hannes Reineckebcd5c592021-01-13 10:04:30 +0100286#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 lun = 0;
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100288 if (cmdp == 0x44 || i == 0x80)
289 lun = atp_readb_io(dev, c, 0x1d) & 0x07;
290 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 if ((dev->last_cmd[c] & 0xf0) != 0x40) {
292 dev->last_cmd[c] = 0xff;
293 }
294 if (cmdp == 0x41) {
295#ifdef ED_DBGP
296 printk("cmdp = 0x41\n");
Hannes Reineckebcd5c592021-01-13 10:04:30 +0100297#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 adrcnt = 0;
Hannes Reineckebcd5c592021-01-13 10:04:30 +0100299 ((unsigned char *) &adrcnt)[2] =
300 atp_readb_io(dev, c, 0x12);
301 ((unsigned char *) &adrcnt)[1] =
302 atp_readb_io(dev, c, 0x13);
303 ((unsigned char *) &adrcnt)[0] =
304 atp_readb_io(dev, c, 0x14);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 k = dev->id[c][target_id].last_len;
306 k -= adrcnt;
307 dev->id[c][target_id].tran_len = k;
308 dev->id[c][target_id].last_len = adrcnt;
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100309 atp_writeb_io(dev, c, 0x18, 0x08);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 dev->in_int[c] = 0;
Ondrej Zary78614ec2015-11-17 19:23:49 +0100311 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 } else {
313#ifdef ED_DBGP
314 printk("cmdp != 0x41\n");
Hannes Reineckebcd5c592021-01-13 10:04:30 +0100315#endif
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100316 atp_writeb_io(dev, c, 0x10, 0x46);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 dev->id[c][target_id].dirct = 0x00;
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100318 atp_writeb_io(dev, c, 0x12, 0x00);
319 atp_writeb_io(dev, c, 0x13, 0x00);
320 atp_writeb_io(dev, c, 0x14, 0x00);
321 atp_writeb_io(dev, c, 0x18, 0x08);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 dev->in_int[c] = 0;
Ondrej Zary78614ec2015-11-17 19:23:49 +0100323 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 }
325 }
326 if (dev->last_cmd[c] != 0xff) {
327 dev->last_cmd[c] |= 0x40;
328 }
Ondrej Zaryb922a442015-11-17 19:24:21 +0100329 if (is885(dev)) {
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100330 j = atp_readb_base(dev, 0x29) & 0xfe;
331 atp_writeb_base(dev, 0x29, j);
Ondrej Zary3a38e532015-11-17 19:23:39 +0100332 } else
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100333 atp_writeb_io(dev, c, 0x10, 0x45);
Ondrej Zary3a38e532015-11-17 19:23:39 +0100334
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100335 target_id = atp_readb_io(dev, c, 0x16);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 /*
337 * Remap wide identifiers
338 */
339 if ((target_id & 0x10) != 0) {
340 target_id = (target_id & 0x07) | 0x08;
341 } else {
342 target_id &= 0x07;
343 }
Ondrej Zaryb922a442015-11-17 19:24:21 +0100344 if (is885(dev))
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100345 atp_writeb_io(dev, c, 0x10, 0x45);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 workreq = dev->id[c][target_id].curr_req;
Hannes Reineckebcd5c592021-01-13 10:04:30 +0100347#ifdef ED_DBGP
Jeff Garzik017560f2005-10-24 18:04:36 -0400348 scmd_printk(KERN_DEBUG, workreq, "CDB");
349 for (l = 0; l < workreq->cmd_len; l++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 printk(KERN_DEBUG " %x",workreq->cmnd[l]);
Jeff Garzik017560f2005-10-24 18:04:36 -0400351 printk("\n");
Hannes Reineckebcd5c592021-01-13 10:04:30 +0100352#endif
353
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100354 atp_writeb_io(dev, c, 0x0f, lun);
355 atp_writeb_io(dev, c, 0x11, dev->id[c][target_id].devsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 adrcnt = dev->id[c][target_id].tran_len;
357 k = dev->id[c][target_id].last_len;
358
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100359 atp_writeb_io(dev, c, 0x12, ((unsigned char *) &k)[2]);
360 atp_writeb_io(dev, c, 0x13, ((unsigned char *) &k)[1]);
361 atp_writeb_io(dev, c, 0x14, ((unsigned char *) &k)[0]);
Hannes Reineckebcd5c592021-01-13 10:04:30 +0100362#ifdef ED_DBGP
363 printk("k %x, k[0] 0x%x k[1] 0x%x k[2] 0x%x\n", k,
364 atp_readb_io(dev, c, 0x14),
365 atp_readb_io(dev, c, 0x13),
366 atp_readb_io(dev, c, 0x12));
367#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 /* Remap wide */
369 j = target_id;
370 if (target_id > 7) {
371 j = (j & 0x07) | 0x40;
372 }
373 /* Add direction */
374 j |= dev->id[c][target_id].dirct;
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100375 atp_writeb_io(dev, c, 0x15, j);
376 atp_writeb_io(dev, c, 0x16, 0x80);
Hannes Reineckebcd5c592021-01-13 10:04:30 +0100377
378 /* enable 32 bit fifo transfer */
Ondrej Zaryb922a442015-11-17 19:24:21 +0100379 if (is885(dev)) {
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100380 i = atp_readb_pci(dev, c, 1) & 0xf3;
Hannes Reineckebcd5c592021-01-13 10:04:30 +0100381 //j=workreq->cmnd[0];
Hannes Reineckef3272252021-01-13 10:04:49 +0100382 if ((workreq->cmnd[0] == READ_6) ||
383 (workreq->cmnd[0] == READ_10) ||
384 (workreq->cmnd[0] == WRITE_6) ||
385 (workreq->cmnd[0] == WRITE_10)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 i |= 0x0c;
387 }
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100388 atp_writeb_pci(dev, c, 1, i);
Ondrej Zaryb922a442015-11-17 19:24:21 +0100389 } else if (is880(dev)) {
Hannes Reineckef3272252021-01-13 10:04:49 +0100390 if ((workreq->cmnd[0] == READ_6) ||
391 (workreq->cmnd[0] == READ_10) ||
392 (workreq->cmnd[0] == WRITE_6) ||
393 (workreq->cmnd[0] == WRITE_10))
Hannes Reineckebcd5c592021-01-13 10:04:30 +0100394 atp_writeb_base(dev, 0x3b,
395 (atp_readb_base(dev, 0x3b) & 0x3f) | 0xc0);
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100396 else
Hannes Reineckebcd5c592021-01-13 10:04:30 +0100397 atp_writeb_base(dev, 0x3b,
398 atp_readb_base(dev, 0x3b) & 0x3f);
399 } else {
Hannes Reineckef3272252021-01-13 10:04:49 +0100400 if ((workreq->cmnd[0] == READ_6) ||
401 (workreq->cmnd[0] == READ_10) ||
402 (workreq->cmnd[0] == WRITE_6) ||
403 (workreq->cmnd[0] == WRITE_10))
Hannes Reineckebcd5c592021-01-13 10:04:30 +0100404 atp_writeb_base(dev, 0x3a,
405 (atp_readb_base(dev, 0x3a) & 0xf3) | 0x08);
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100406 else
Hannes Reineckebcd5c592021-01-13 10:04:30 +0100407 atp_writeb_base(dev, 0x3a,
408 atp_readb_base(dev, 0x3a) & 0xf3);
409 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 j = 0;
411 id = 1;
412 id = id << target_id;
413 /*
414 * Is this a wide device
415 */
416 if ((id & dev->wide_id[c]) != 0) {
417 j |= 0x01;
418 }
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100419 atp_writeb_io(dev, c, 0x1b, j);
420 while ((atp_readb_io(dev, c, 0x1b) & 0x01) != j)
421 atp_writeb_io(dev, c, 0x1b, j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 if (dev->id[c][target_id].last_len == 0) {
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100423 atp_writeb_io(dev, c, 0x18, 0x08);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 dev->in_int[c] = 0;
425#ifdef ED_DBGP
426 printk("dev->id[c][target_id].last_len = 0\n");
Hannes Reineckebcd5c592021-01-13 10:04:30 +0100427#endif
Ondrej Zary78614ec2015-11-17 19:23:49 +0100428 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 }
430#ifdef ED_DBGP
431 printk("target_id = %d adrcnt = %d\n",target_id,adrcnt);
Hannes Reineckebcd5c592021-01-13 10:04:30 +0100432#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 prd = dev->id[c][target_id].prd_pos;
434 while (adrcnt != 0) {
435 id = ((unsigned short int *)prd)[2];
436 if (id == 0) {
437 k = 0x10000;
438 } else {
439 k = id;
440 }
441 if (k > adrcnt) {
Hannes Reineckebcd5c592021-01-13 10:04:30 +0100442 ((unsigned short int *)prd)[2] =
443 (unsigned short int)(k - adrcnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 ((unsigned long *)prd)[0] += adrcnt;
445 adrcnt = 0;
446 dev->id[c][target_id].prd_pos = prd;
447 } else {
448 adrcnt -= k;
449 dev->id[c][target_id].prdaddr += 0x08;
450 prd += 0x08;
451 if (adrcnt == 0) {
452 dev->id[c][target_id].prd_pos = prd;
453 }
Hannes Reineckebcd5c592021-01-13 10:04:30 +0100454 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 }
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100456 atp_writel_pci(dev, c, 0x04, dev->id[c][target_id].prdaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457#ifdef ED_DBGP
Hannes Reineckebcd5c592021-01-13 10:04:30 +0100458 printk("dev->id[%d][%d].prdaddr 0x%8x\n",
459 c, target_id, dev->id[c][target_id].prdaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460#endif
Ondrej Zaryb922a442015-11-17 19:24:21 +0100461 if (!is885(dev)) {
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100462 atp_writeb_pci(dev, c, 2, 0x06);
463 atp_writeb_pci(dev, c, 2, 0x00);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 /*
466 * Check transfer direction
467 */
468 if (dev->id[c][target_id].dirct != 0) {
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100469 atp_writeb_io(dev, c, 0x18, 0x08);
470 atp_writeb_pci(dev, c, 0, 0x01);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 dev->in_int[c] = 0;
472#ifdef ED_DBGP
473 printk("status 0x80 return dirct != 0\n");
Hannes Reineckebcd5c592021-01-13 10:04:30 +0100474#endif
Ondrej Zary78614ec2015-11-17 19:23:49 +0100475 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 }
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100477 atp_writeb_io(dev, c, 0x18, 0x08);
478 atp_writeb_pci(dev, c, 0, 0x09);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 dev->in_int[c] = 0;
480#ifdef ED_DBGP
481 printk("status 0x80 return dirct = 0\n");
Hannes Reineckebcd5c592021-01-13 10:04:30 +0100482#endif
Ondrej Zary78614ec2015-11-17 19:23:49 +0100483 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 }
485
486 /*
487 * Current scsi request on this target
488 */
489
490 workreq = dev->id[c][target_id].curr_req;
491
Ondrej Zary78614ec2015-11-17 19:23:49 +0100492 if (i == 0x42 || i == 0x16) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 if ((dev->last_cmd[c] & 0xf0) != 0x40) {
494 dev->last_cmd[c] = 0xff;
495 }
Ondrej Zary78614ec2015-11-17 19:23:49 +0100496 if (i == 0x16) {
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100497 workreq->result = atp_readb_io(dev, c, 0x0f);
Ondrej Zaryb922a442015-11-17 19:24:21 +0100498 if (((dev->r1f[c][target_id] & 0x10) != 0) && is885(dev)) {
Ondrej Zary78614ec2015-11-17 19:23:49 +0100499 printk(KERN_WARNING "AEC67162 CRC ERROR !\n");
Hannes Reineckef3272252021-01-13 10:04:49 +0100500 workreq->result = SAM_STAT_CHECK_CONDITION;
Ondrej Zary78614ec2015-11-17 19:23:49 +0100501 }
502 } else
Hannes Reineckef3272252021-01-13 10:04:49 +0100503 workreq->result = SAM_STAT_CHECK_CONDITION;
Ondrej Zary78614ec2015-11-17 19:23:49 +0100504
Ondrej Zaryb922a442015-11-17 19:24:21 +0100505 if (is885(dev)) {
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100506 j = atp_readb_base(dev, 0x29) | 0x01;
507 atp_writeb_base(dev, 0x29, j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 }
509 /*
510 * Complete the command
511 */
Boaz Harroshfe7ed982007-09-09 21:06:23 +0300512 scsi_dma_unmap(workreq);
513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 spin_lock_irqsave(dev->host->host_lock, flags);
Bart Van Assche681fa522021-10-07 13:28:18 -0700515 scsi_done(workreq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516#ifdef ED_DBGP
517 printk("workreq->scsi_done\n");
Hannes Reineckebcd5c592021-01-13 10:04:30 +0100518#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 /*
520 * Clear it off the queue
521 */
522 dev->id[c][target_id].curr_req = NULL;
523 dev->working[c]--;
524 spin_unlock_irqrestore(dev->host->host_lock, flags);
525 /*
526 * Take it back wide
527 */
528 if (dev->wide_id[c] != 0) {
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100529 atp_writeb_io(dev, c, 0x1b, 0x01);
530 while ((atp_readb_io(dev, c, 0x1b) & 0x01) != 0x01)
531 atp_writeb_io(dev, c, 0x1b, 0x01);
Hannes Reineckebcd5c592021-01-13 10:04:30 +0100532 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 /*
534 * If there is stuff to send and nothing going then send it
535 */
536 spin_lock_irqsave(dev->host->host_lock, flags);
Hannes Reineckebcd5c592021-01-13 10:04:30 +0100537 if (((dev->last_cmd[c] != 0xff) ||
538 (dev->quhd[c] != dev->quend[c])) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 (dev->in_snd[c] == 0)) {
540#ifdef ED_DBGP
541 printk("Call sent_s870(scsi_done)\n");
Hannes Reineckebcd5c592021-01-13 10:04:30 +0100542#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 send_s870(dev,c);
544 }
545 spin_unlock_irqrestore(dev->host->host_lock, flags);
546 dev->in_int[c] = 0;
Ondrej Zary78614ec2015-11-17 19:23:49 +0100547 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 }
549 if ((dev->last_cmd[c] & 0xf0) != 0x40) {
550 dev->last_cmd[c] = 0xff;
551 }
552 if (i == 0x4f) {
553 i = 0x89;
554 }
555 i &= 0x0f;
556 if (i == 0x09) {
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100557 atp_writel_pci(dev, c, 4, dev->id[c][target_id].prdaddr);
558 atp_writeb_pci(dev, c, 2, 0x06);
559 atp_writeb_pci(dev, c, 2, 0x00);
560 atp_writeb_io(dev, c, 0x10, 0x41);
Ondrej Zaryb922a442015-11-17 19:24:21 +0100561 if (is885(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 k = dev->id[c][target_id].last_len;
Hannes Reineckebcd5c592021-01-13 10:04:30 +0100563 atp_writeb_io(dev, c, 0x12,
564 ((unsigned char *) (&k))[2]);
565 atp_writeb_io(dev, c, 0x13,
566 ((unsigned char *) (&k))[1]);
567 atp_writeb_io(dev, c, 0x14,
568 ((unsigned char *) (&k))[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 dev->id[c][target_id].dirct = 0x00;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 } else {
571 dev->id[c][target_id].dirct = 0x00;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 }
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100573 atp_writeb_io(dev, c, 0x18, 0x08);
574 atp_writeb_pci(dev, c, 0, 0x09);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 dev->in_int[c] = 0;
Ondrej Zary78614ec2015-11-17 19:23:49 +0100576 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 }
578 if (i == 0x08) {
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100579 atp_writel_pci(dev, c, 4, dev->id[c][target_id].prdaddr);
580 atp_writeb_pci(dev, c, 2, 0x06);
581 atp_writeb_pci(dev, c, 2, 0x00);
582 atp_writeb_io(dev, c, 0x10, 0x41);
Ondrej Zaryb922a442015-11-17 19:24:21 +0100583 if (is885(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 k = dev->id[c][target_id].last_len;
Hannes Reineckebcd5c592021-01-13 10:04:30 +0100585 atp_writeb_io(dev, c, 0x12,
586 ((unsigned char *) (&k))[2]);
587 atp_writeb_io(dev, c, 0x13,
588 ((unsigned char *) (&k))[1]);
589 atp_writeb_io(dev, c, 0x14,
590 ((unsigned char *) (&k))[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 }
Hannes Reineckebcd5c592021-01-13 10:04:30 +0100592 atp_writeb_io(dev, c, 0x15,
593 atp_readb_io(dev, c, 0x15) | 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 dev->id[c][target_id].dirct = 0x20;
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100595 atp_writeb_io(dev, c, 0x18, 0x08);
596 atp_writeb_pci(dev, c, 0, 0x01);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 dev->in_int[c] = 0;
Ondrej Zary78614ec2015-11-17 19:23:49 +0100598 return IRQ_HANDLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 }
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100600 if (i == 0x0a)
601 atp_writeb_io(dev, c, 0x10, 0x30);
602 else
603 atp_writeb_io(dev, c, 0x10, 0x46);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 dev->id[c][target_id].dirct = 0x00;
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100605 atp_writeb_io(dev, c, 0x12, 0x00);
606 atp_writeb_io(dev, c, 0x13, 0x00);
607 atp_writeb_io(dev, c, 0x14, 0x00);
608 atp_writeb_io(dev, c, 0x18, 0x08);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 }
Ondrej Zary78614ec2015-11-17 19:23:49 +0100610 dev->in_int[c] = 0;
611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 return IRQ_HANDLED;
613}
614/**
Lee Jones6b71f602021-03-12 09:47:34 +0000615 * atp870u_queuecommand_lck - Queue SCSI command
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 * @req_p: request block
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 *
618 * Queue a command to the ATP queue. Called with the host lock held.
619 */
Bart Van Asscheaf049df2021-10-07 13:46:14 -0700620static int atp870u_queuecommand_lck(struct scsi_cmnd *req_p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621{
Bart Van Asscheaf049df2021-10-07 13:46:14 -0700622 void (*done)(struct scsi_cmnd *) = scsi_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 unsigned char c;
Ondrej Zary3b836462015-11-17 19:23:40 +0100624 unsigned int m;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 struct atp_unit *dev;
626 struct Scsi_Host *host;
627
Jeff Garzik422c0d62005-10-24 18:05:09 -0400628 c = scmd_channel(req_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 req_p->sense_buffer[0]=0;
Boaz Harroshfe7ed982007-09-09 21:06:23 +0300630 scsi_set_resid(req_p, 0);
Jeff Garzik422c0d62005-10-24 18:05:09 -0400631 if (scmd_channel(req_p) > 1) {
Hannes Reineckef3272252021-01-13 10:04:49 +0100632 req_p->result = DID_BAD_TARGET << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 done(req_p);
Hannes Reineckebcd5c592021-01-13 10:04:30 +0100634#ifdef ED_DBGP
635 printk("atp870u_queuecommand : req_p->device->channel > 1\n");
636#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 return 0;
638 }
639
640 host = req_p->device->host;
641 dev = (struct atp_unit *)&host->hostdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 m = 1;
Jeff Garzik422c0d62005-10-24 18:05:09 -0400644 m = m << scmd_id(req_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
646 /*
647 * Fake a timeout for missing targets
648 */
649
650 if ((m & dev->active_id[c]) == 0) {
Hannes Reineckef3272252021-01-13 10:04:49 +0100651 req_p->result = DID_BAD_TARGET << 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 done(req_p);
653 return 0;
654 }
655
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 /*
657 * Count new command
658 */
659 dev->quend[c]++;
660 if (dev->quend[c] >= qcnt) {
661 dev->quend[c] = 0;
662 }
Hannes Reineckebcd5c592021-01-13 10:04:30 +0100663
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 /*
665 * Check queue state
666 */
667 if (dev->quhd[c] == dev->quend[c]) {
668 if (dev->quend[c] == 0) {
669 dev->quend[c] = qcnt;
670 }
Hannes Reineckebcd5c592021-01-13 10:04:30 +0100671#ifdef ED_DBGP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 printk("atp870u_queuecommand : dev->quhd[c] == dev->quend[c]\n");
Hannes Reineckebcd5c592021-01-13 10:04:30 +0100673#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 dev->quend[c]--;
Hannes Reineckef3272252021-01-13 10:04:49 +0100675 req_p->result = DID_BUS_BUSY << 16;
Hannes Reineckebcd5c592021-01-13 10:04:30 +0100676 done(req_p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 return 0;
678 }
679 dev->quereq[c][dev->quend[c]] = req_p;
Hannes Reineckebcd5c592021-01-13 10:04:30 +0100680#ifdef ED_DBGP
681 printk("dev->ioport[c] = %x atp_readb_io(dev, c, 0x1c) = %x "
682 "dev->in_int[%d] = %d dev->in_snd[%d] = %d\n",
683 dev->ioport[c], atp_readb_io(dev, c, 0x1c), c,
684 dev->in_int[c],c,dev->in_snd[c]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685#endif
Hannes Reineckebcd5c592021-01-13 10:04:30 +0100686 if ((atp_readb_io(dev, c, 0x1c) == 0) &&
687 (dev->in_int[c] == 0) &&
688 (dev->in_snd[c] == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689#ifdef ED_DBGP
690 printk("Call sent_s870(atp870u_queuecommand)\n");
Hannes Reineckebcd5c592021-01-13 10:04:30 +0100691#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 send_s870(dev,c);
693 }
Hannes Reineckebcd5c592021-01-13 10:04:30 +0100694#ifdef ED_DBGP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 printk("atp870u_queuecommand : exit\n");
Hannes Reineckebcd5c592021-01-13 10:04:30 +0100696#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 return 0;
698}
699
Jeff Garzikf2812332010-11-16 02:10:29 -0500700static DEF_SCSI_QCMD(atp870u_queuecommand)
701
Lee Jones6b71f602021-03-12 09:47:34 +0000702/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 * send_s870 - send a command to the controller
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 *
705 * On entry there is work queued to be done. We move some of that work to the
Hannes Reineckebcd5c592021-01-13 10:04:30 +0100706 * controller itself.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 *
708 * Caller holds the host lock.
709 */
Lee Jones6b71f602021-03-12 09:47:34 +0000710static void send_s870(struct atp_unit *dev, unsigned char c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711{
Ondrej Zary468b8962015-11-17 19:23:50 +0100712 struct scsi_cmnd *workreq = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 unsigned int i;//,k;
714 unsigned char j, target_id;
715 unsigned char *prd;
Ondrej Zaryc2bab402015-11-17 19:23:48 +0100716 unsigned short int w;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 unsigned long l, bttl = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 unsigned long sg_count;
719
720 if (dev->in_snd[c] != 0) {
Hannes Reineckebcd5c592021-01-13 10:04:30 +0100721#ifdef ED_DBGP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 printk("cmnd in_snd\n");
723#endif
724 return;
725 }
726#ifdef ED_DBGP
727 printk("Sent_s870 enter\n");
728#endif
729 dev->in_snd[c] = 1;
730 if ((dev->last_cmd[c] != 0xff) && ((dev->last_cmd[c] & 0x40) != 0)) {
731 dev->last_cmd[c] &= 0x0f;
732 workreq = dev->id[c][dev->last_cmd[c]].curr_req;
Ondrej Zary468b8962015-11-17 19:23:50 +0100733 if (!workreq) {
734 dev->last_cmd[c] = 0xff;
735 if (dev->quhd[c] == dev->quend[c]) {
736 dev->in_snd[c] = 0;
737 return;
738 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 }
740 }
Ondrej Zary468b8962015-11-17 19:23:50 +0100741 if (!workreq) {
742 if ((dev->last_cmd[c] != 0xff) && (dev->working[c] != 0)) {
743 dev->in_snd[c] = 0;
744 return;
745 }
746 dev->working[c]++;
747 j = dev->quhd[c];
748 dev->quhd[c]++;
749 if (dev->quhd[c] >= qcnt)
750 dev->quhd[c] = 0;
751 workreq = dev->quereq[c][dev->quhd[c]];
752 if (dev->id[c][scmd_id(workreq)].curr_req != NULL) {
753 dev->quhd[c] = j;
754 dev->working[c]--;
755 dev->in_snd[c] = 0;
756 return;
757 }
Jeff Garzik422c0d62005-10-24 18:05:09 -0400758 dev->id[c][scmd_id(workreq)].curr_req = workreq;
759 dev->last_cmd[c] = scmd_id(workreq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 }
Hannes Reineckebcd5c592021-01-13 10:04:30 +0100761 if ((atp_readb_io(dev, c, 0x1f) & 0xb0) != 0 ||
762 atp_readb_io(dev, c, 0x1c) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763#ifdef ED_DBGP
Ondrej Zary468b8962015-11-17 19:23:50 +0100764 printk("Abort to Send\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765#endif
Ondrej Zary468b8962015-11-17 19:23:50 +0100766 dev->last_cmd[c] |= 0x40;
767 dev->in_snd[c] = 0;
768 return;
769 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770#ifdef ED_DBGP
771 printk("OK to Send\n");
Jeff Garzik422c0d62005-10-24 18:05:09 -0400772 scmd_printk(KERN_DEBUG, workreq, "CDB");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 for(i=0;i<workreq->cmd_len;i++) {
774 printk(" %x",workreq->cmnd[i]);
775 }
Jeff Garzik422c0d62005-10-24 18:05:09 -0400776 printk("\n");
Hannes Reineckebcd5c592021-01-13 10:04:30 +0100777#endif
Boaz Harroshfe7ed982007-09-09 21:06:23 +0300778 l = scsi_bufflen(workreq);
779
Ondrej Zaryb922a442015-11-17 19:24:21 +0100780 if (is885(dev)) {
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100781 j = atp_readb_base(dev, 0x29) & 0xfe;
782 atp_writeb_base(dev, 0x29, j);
Jeff Garzik422c0d62005-10-24 18:05:09 -0400783 dev->r1f[c][scmd_id(workreq)] = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 }
Hannes Reineckebcd5c592021-01-13 10:04:30 +0100785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 if (workreq->cmnd[0] == READ_CAPACITY) {
Boaz Harroshfe7ed982007-09-09 21:06:23 +0300787 if (l > 8)
788 l = 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 }
Hannes Reineckef3272252021-01-13 10:04:49 +0100790 if (workreq->cmnd[0] == TEST_UNIT_READY) {
Boaz Harroshfe7ed982007-09-09 21:06:23 +0300791 l = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 }
793
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 j = 0;
Jeff Garzik422c0d62005-10-24 18:05:09 -0400795 target_id = scmd_id(workreq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
797 /*
798 * Wide ?
799 */
800 w = 1;
801 w = w << target_id;
802 if ((w & dev->wide_id[c]) != 0) {
803 j |= 0x01;
804 }
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100805 atp_writeb_io(dev, c, 0x1b, j);
806 while ((atp_readb_io(dev, c, 0x1b) & 0x01) != j) {
807 atp_writeb_pci(dev, c, 0x1b, j);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808#ifdef ED_DBGP
809 printk("send_s870 while loop 1\n");
810#endif
811 }
812 /*
813 * Write the command
814 */
815
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100816 atp_writeb_io(dev, c, 0x00, workreq->cmd_len);
817 atp_writeb_io(dev, c, 0x01, 0x2c);
Ondrej Zaryb922a442015-11-17 19:24:21 +0100818 if (is885(dev))
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100819 atp_writeb_io(dev, c, 0x02, 0x7f);
820 else
821 atp_writeb_io(dev, c, 0x02, 0xcf);
822 for (i = 0; i < workreq->cmd_len; i++)
823 atp_writeb_io(dev, c, 0x03 + i, workreq->cmnd[i]);
824 atp_writeb_io(dev, c, 0x0f, workreq->device->lun);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 /*
826 * Write the target
827 */
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100828 atp_writeb_io(dev, c, 0x11, dev->id[c][target_id].devsp);
Hannes Reineckebcd5c592021-01-13 10:04:30 +0100829#ifdef ED_DBGP
830 printk("dev->id[%d][%d].devsp = %2x\n",c,target_id,
831 dev->id[c][target_id].devsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832#endif
Boaz Harroshfe7ed982007-09-09 21:06:23 +0300833
834 sg_count = scsi_dma_map(workreq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 /*
836 * Write transfer size
837 */
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100838 atp_writeb_io(dev, c, 0x12, ((unsigned char *) (&l))[2]);
839 atp_writeb_io(dev, c, 0x13, ((unsigned char *) (&l))[1]);
840 atp_writeb_io(dev, c, 0x14, ((unsigned char *) (&l))[0]);
Hannes Reineckebcd5c592021-01-13 10:04:30 +0100841 j = target_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 dev->id[c][j].last_len = l;
843 dev->id[c][j].tran_len = 0;
Hannes Reineckebcd5c592021-01-13 10:04:30 +0100844#ifdef ED_DBGP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 printk("dev->id[%2d][%2d].last_len = %d\n",c,j,dev->id[c][j].last_len);
Hannes Reineckebcd5c592021-01-13 10:04:30 +0100846#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 /*
848 * Flip the wide bits
849 */
850 if ((j & 0x08) != 0) {
851 j = (j & 0x07) | 0x40;
852 }
853 /*
854 * Check transfer direction
855 */
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100856 if (workreq->sc_data_direction == DMA_TO_DEVICE)
857 atp_writeb_io(dev, c, 0x15, j | 0x20);
858 else
859 atp_writeb_io(dev, c, 0x15, j);
860 atp_writeb_io(dev, c, 0x16, atp_readb_io(dev, c, 0x16) | 0x80);
861 atp_writeb_io(dev, c, 0x16, 0x80);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 dev->id[c][target_id].dirct = 0;
863 if (l == 0) {
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100864 if (atp_readb_io(dev, c, 0x1c) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865#ifdef ED_DBGP
Hannes Reineckebcd5c592021-01-13 10:04:30 +0100866 printk("change SCSI_CMD_REG 0x08\n");
867#endif
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100868 atp_writeb_io(dev, c, 0x18, 0x08);
869 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 dev->last_cmd[c] |= 0x40;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 dev->in_snd[c] = 0;
872 return;
873 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 prd = dev->id[c][target_id].prd_table;
875 dev->id[c][target_id].prd_pos = prd;
876
877 /*
878 * Now write the request list. Either as scatter/gather or as
879 * a linear chain.
880 */
881
Boaz Harroshfe7ed982007-09-09 21:06:23 +0300882 if (l) {
883 struct scatterlist *sgpnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 i = 0;
Boaz Harroshfe7ed982007-09-09 21:06:23 +0300885 scsi_for_each_sg(workreq, sgpnt, sg_count, j) {
886 bttl = sg_dma_address(sgpnt);
887 l=sg_dma_len(sgpnt);
Hannes Reineckebcd5c592021-01-13 10:04:30 +0100888#ifdef ED_DBGP
Boaz Harroshfe7ed982007-09-09 21:06:23 +0300889 printk("1. bttl %x, l %x\n",bttl, l);
Hannes Reineckebcd5c592021-01-13 10:04:30 +0100890#endif
Boaz Harroshfe7ed982007-09-09 21:06:23 +0300891 while (l > 0x10000) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 (((u16 *) (prd))[i + 3]) = 0x0000;
893 (((u16 *) (prd))[i + 2]) = 0x0000;
894 (((u32 *) (prd))[i >> 1]) = cpu_to_le32(bttl);
895 l -= 0x10000;
896 bttl += 0x10000;
897 i += 0x04;
898 }
899 (((u32 *) (prd))[i >> 1]) = cpu_to_le32(bttl);
900 (((u16 *) (prd))[i + 2]) = cpu_to_le16(l);
901 (((u16 *) (prd))[i + 3]) = 0;
Hannes Reineckebcd5c592021-01-13 10:04:30 +0100902 i += 0x04;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 }
Hannes Reineckebcd5c592021-01-13 10:04:30 +0100904 (((u16 *) (prd))[i - 1]) = cpu_to_le16(0x8000);
905#ifdef ED_DBGP
906 printk("prd %4x %4x %4x %4x\n",
907 (((unsigned short int *)prd)[0]),
908 (((unsigned short int *)prd)[1]),
909 (((unsigned short int *)prd)[2]),
910 (((unsigned short int *)prd)[3]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 printk("2. bttl %x, l %x\n",bttl, l);
Hannes Reineckebcd5c592021-01-13 10:04:30 +0100912#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 }
Hannes Reineckebcd5c592021-01-13 10:04:30 +0100914#ifdef ED_DBGP
915 printk("send_s870: prdaddr_2 0x%8x target_id %d\n",
916 dev->id[c][target_id].prdaddr,target_id);
917#endif
James Bottomleyb5683552005-09-15 08:59:36 -0500918 dev->id[c][target_id].prdaddr = dev->id[c][target_id].prd_bus;
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100919 atp_writel_pci(dev, c, 4, dev->id[c][target_id].prdaddr);
920 atp_writeb_pci(dev, c, 2, 0x06);
921 atp_writeb_pci(dev, c, 2, 0x00);
Ondrej Zaryb922a442015-11-17 19:24:21 +0100922 if (is885(dev)) {
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100923 j = atp_readb_pci(dev, c, 1) & 0xf3;
Hannes Reineckef3272252021-01-13 10:04:49 +0100924 if ((workreq->cmnd[0] == READ_6) ||
925 (workreq->cmnd[0] == READ_10) ||
926 (workreq->cmnd[0] == WRITE_6) ||
927 (workreq->cmnd[0] == WRITE_10)) {
Hannes Reineckebcd5c592021-01-13 10:04:30 +0100928 j |= 0x0c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 }
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100930 atp_writeb_pci(dev, c, 1, j);
Ondrej Zaryb922a442015-11-17 19:24:21 +0100931 } else if (is880(dev)) {
Hannes Reineckef3272252021-01-13 10:04:49 +0100932 if ((workreq->cmnd[0] == READ_6) ||
933 (workreq->cmnd[0] == READ_10) ||
934 (workreq->cmnd[0] == WRITE_6) ||
935 (workreq->cmnd[0] == WRITE_10))
Hannes Reineckebcd5c592021-01-13 10:04:30 +0100936 atp_writeb_base(dev, 0x3b,
937 (atp_readb_base(dev, 0x3b) & 0x3f) | 0xc0);
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100938 else
Hannes Reineckebcd5c592021-01-13 10:04:30 +0100939 atp_writeb_base(dev, 0x3b,
940 atp_readb_base(dev, 0x3b) & 0x3f);
941 } else {
Hannes Reineckef3272252021-01-13 10:04:49 +0100942 if ((workreq->cmnd[0] == READ_6) ||
943 (workreq->cmnd[0] == READ_10) ||
944 (workreq->cmnd[0] == WRITE_6) ||
945 (workreq->cmnd[0] == WRITE_10))
Hannes Reineckebcd5c592021-01-13 10:04:30 +0100946 atp_writeb_base(dev, 0x3a,
947 (atp_readb_base(dev, 0x3a) & 0xf3) | 0x08);
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100948 else
Hannes Reineckebcd5c592021-01-13 10:04:30 +0100949 atp_writeb_base(dev, 0x3a,
950 atp_readb_base(dev, 0x3a) & 0xf3);
951 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
953 if(workreq->sc_data_direction == DMA_TO_DEVICE) {
954 dev->id[c][target_id].dirct = 0x20;
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100955 if (atp_readb_io(dev, c, 0x1c) == 0) {
956 atp_writeb_io(dev, c, 0x18, 0x08);
957 atp_writeb_pci(dev, c, 0, 0x01);
Hannes Reineckebcd5c592021-01-13 10:04:30 +0100958#ifdef ED_DBGP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 printk( "start DMA(to target)\n");
Hannes Reineckebcd5c592021-01-13 10:04:30 +0100960#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 } else {
962 dev->last_cmd[c] |= 0x40;
963 }
964 dev->in_snd[c] = 0;
965 return;
966 }
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100967 if (atp_readb_io(dev, c, 0x1c) == 0) {
968 atp_writeb_io(dev, c, 0x18, 0x08);
969 atp_writeb_pci(dev, c, 0, 0x09);
Hannes Reineckebcd5c592021-01-13 10:04:30 +0100970#ifdef ED_DBGP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 printk( "start DMA(to host)\n");
Hannes Reineckebcd5c592021-01-13 10:04:30 +0100972#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 } else {
974 dev->last_cmd[c] |= 0x40;
975 }
976 dev->in_snd[c] = 0;
977 return;
978
979}
980
981static unsigned char fun_scam(struct atp_unit *dev, unsigned short int *val)
982{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 unsigned short int i, k;
984 unsigned char j;
985
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100986 atp_writew_io(dev, 0, 0x1c, *val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 for (i = 0; i < 10; i++) { /* stable >= bus settle delay(400 ns) */
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100988 k = atp_readw_io(dev, 0, 0x1c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 j = (unsigned char) (k >> 8);
Ondrej Zary832e9ac2015-11-17 19:23:51 +0100990 if ((k & 0x8000) != 0) /* DB7 all release? */
991 i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 }
993 *val |= 0x4000; /* assert DB6 */
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100994 atp_writew_io(dev, 0, 0x1c, *val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 *val &= 0xdfff; /* assert DB5 */
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100996 atp_writew_io(dev, 0, 0x1c, *val);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 for (i = 0; i < 10; i++) { /* stable >= bus settle delay(400 ns) */
Ondrej Zary6a3cebb2015-11-17 19:23:54 +0100998 if ((atp_readw_io(dev, 0, 0x1c) & 0x2000) != 0) /* DB5 all release? */
Ondrej Zary832e9ac2015-11-17 19:23:51 +0100999 i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 }
1001 *val |= 0x8000; /* no DB4-0, assert DB7 */
1002 *val &= 0xe0ff;
Ondrej Zary6a3cebb2015-11-17 19:23:54 +01001003 atp_writew_io(dev, 0, 0x1c, *val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 *val &= 0xbfff; /* release DB6 */
Ondrej Zary6a3cebb2015-11-17 19:23:54 +01001005 atp_writew_io(dev, 0, 0x1c, *val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 for (i = 0; i < 10; i++) { /* stable >= bus settle delay(400 ns) */
Ondrej Zary6a3cebb2015-11-17 19:23:54 +01001007 if ((atp_readw_io(dev, 0, 0x1c) & 0x4000) != 0) /* DB6 all release? */
Ondrej Zary832e9ac2015-11-17 19:23:51 +01001008 i = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 }
1010
1011 return j;
1012}
1013
Ondrej Zary8177c502015-11-17 19:24:24 +01001014static void tscam(struct Scsi_Host *host, bool wide_chip, u8 scam_on)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015{
1016
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 unsigned char i, j, k;
1018 unsigned long n;
1019 unsigned short int m, assignid_map, val;
1020 unsigned char mbuf[33], quintet[2];
1021 struct atp_unit *dev = (struct atp_unit *)&host->hostdata;
1022 static unsigned char g2q_tab[8] = {
1023 0x38, 0x31, 0x32, 0x2b, 0x34, 0x2d, 0x2e, 0x27
1024 };
1025
1026/* I can't believe we need this before we've even done anything. Remove it
1027 * and see if anyone bitches.
1028 for (i = 0; i < 0x10; i++) {
1029 udelay(0xffff);
1030 }
1031 */
1032
Ondrej Zary6a3cebb2015-11-17 19:23:54 +01001033 atp_writeb_io(dev, 0, 1, 0x08);
1034 atp_writeb_io(dev, 0, 2, 0x7f);
1035 atp_writeb_io(dev, 0, 0x11, 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
Ondrej Zary8177c502015-11-17 19:24:24 +01001037 if ((scam_on & 0x40) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 return;
1039 }
1040 m = 1;
1041 m <<= dev->host_id[0];
1042 j = 16;
Ondrej Zarydd5a5f72015-11-17 19:24:19 +01001043 if (!wide_chip) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 m |= 0xff00;
1045 j = 8;
1046 }
1047 assignid_map = m;
Ondrej Zary6a3cebb2015-11-17 19:23:54 +01001048 atp_writeb_io(dev, 0, 0x02, 0x02); /* 2*2=4ms,3EH 2/32*3E=3.9ms */
1049 atp_writeb_io(dev, 0, 0x03, 0);
1050 atp_writeb_io(dev, 0, 0x04, 0);
1051 atp_writeb_io(dev, 0, 0x05, 0);
1052 atp_writeb_io(dev, 0, 0x06, 0);
1053 atp_writeb_io(dev, 0, 0x07, 0);
1054 atp_writeb_io(dev, 0, 0x08, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
1056 for (i = 0; i < j; i++) {
1057 m = 1;
1058 m = m << i;
1059 if ((m & assignid_map) != 0) {
1060 continue;
1061 }
Ondrej Zary6a3cebb2015-11-17 19:23:54 +01001062 atp_writeb_io(dev, 0, 0x0f, 0);
1063 atp_writeb_io(dev, 0, 0x12, 0);
1064 atp_writeb_io(dev, 0, 0x13, 0);
1065 atp_writeb_io(dev, 0, 0x14, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 if (i > 7) {
1067 k = (i & 0x07) | 0x40;
1068 } else {
1069 k = i;
1070 }
Ondrej Zary6a3cebb2015-11-17 19:23:54 +01001071 atp_writeb_io(dev, 0, 0x15, k);
Ondrej Zarydd5a5f72015-11-17 19:24:19 +01001072 if (wide_chip)
Ondrej Zary6a3cebb2015-11-17 19:23:54 +01001073 atp_writeb_io(dev, 0, 0x1b, 0x01);
1074 else
1075 atp_writeb_io(dev, 0, 0x1b, 0x00);
Ondrej Zary58c4d042015-11-17 19:23:52 +01001076 do {
Ondrej Zary6a3cebb2015-11-17 19:23:54 +01001077 atp_writeb_io(dev, 0, 0x18, 0x09);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
Ondrej Zary6a3cebb2015-11-17 19:23:54 +01001079 while ((atp_readb_io(dev, 0, 0x1f) & 0x80) == 0x00)
Ondrej Zary58c4d042015-11-17 19:23:52 +01001080 cpu_relax();
Ondrej Zary6a3cebb2015-11-17 19:23:54 +01001081 k = atp_readb_io(dev, 0, 0x17);
Ondrej Zary58c4d042015-11-17 19:23:52 +01001082 if ((k == 0x85) || (k == 0x42))
1083 break;
1084 if (k != 0x16)
Ondrej Zary6a3cebb2015-11-17 19:23:54 +01001085 atp_writeb_io(dev, 0, 0x10, 0x41);
Ondrej Zary58c4d042015-11-17 19:23:52 +01001086 } while (k != 0x16);
1087 if ((k == 0x85) || (k == 0x42))
1088 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 assignid_map |= m;
1090
1091 }
Ondrej Zary6a3cebb2015-11-17 19:23:54 +01001092 atp_writeb_io(dev, 0, 0x02, 0x7f);
1093 atp_writeb_io(dev, 0, 0x1b, 0x02);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094
Ondrej Zary2bbbac42015-11-17 19:24:08 +01001095 udelay(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
1097 val = 0x0080; /* bsy */
Ondrej Zary6a3cebb2015-11-17 19:23:54 +01001098 atp_writew_io(dev, 0, 0x1c, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 val |= 0x0040; /* sel */
Ondrej Zary6a3cebb2015-11-17 19:23:54 +01001100 atp_writew_io(dev, 0, 0x1c, val);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 val |= 0x0004; /* msg */
Ondrej Zary6a3cebb2015-11-17 19:23:54 +01001102 atp_writew_io(dev, 0, 0x1c, val);
Ondrej Zary2bbbac42015-11-17 19:24:08 +01001103 udelay(2); /* 2 deskew delay(45ns*2=90ns) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 val &= 0x007f; /* no bsy */
Ondrej Zary6a3cebb2015-11-17 19:23:54 +01001105 atp_writew_io(dev, 0, 0x1c, val);
Jia-Ju Baidcaa0c12018-07-27 17:13:17 +08001106 msleep(128);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 val &= 0x00fb; /* after 1ms no msg */
Ondrej Zary6a3cebb2015-11-17 19:23:54 +01001108 atp_writew_io(dev, 0, 0x1c, val);
1109 while ((atp_readb_io(dev, 0, 0x1c) & 0x04) != 0)
Ondrej Zary58c4d042015-11-17 19:23:52 +01001110 ;
Ondrej Zary2bbbac42015-11-17 19:24:08 +01001111 udelay(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 udelay(100);
Ondrej Zaryc7fcc0892015-11-17 19:23:53 +01001113 for (n = 0; n < 0x30000; n++)
Ondrej Zary6a3cebb2015-11-17 19:23:54 +01001114 if ((atp_readb_io(dev, 0, 0x1c) & 0x80) != 0) /* bsy ? */
Ondrej Zaryc7fcc0892015-11-17 19:23:53 +01001115 break;
1116 if (n < 0x30000)
1117 for (n = 0; n < 0x30000; n++)
Ondrej Zary6a3cebb2015-11-17 19:23:54 +01001118 if ((atp_readb_io(dev, 0, 0x1c) & 0x81) == 0x0081) {
Ondrej Zary2bbbac42015-11-17 19:24:08 +01001119 udelay(2);
Ondrej Zaryc7fcc0892015-11-17 19:23:53 +01001120 val |= 0x8003; /* io,cd,db7 */
Ondrej Zary6a3cebb2015-11-17 19:23:54 +01001121 atp_writew_io(dev, 0, 0x1c, val);
Ondrej Zary2bbbac42015-11-17 19:24:08 +01001122 udelay(2);
Ondrej Zaryc7fcc0892015-11-17 19:23:53 +01001123 val &= 0x00bf; /* no sel */
Ondrej Zary6a3cebb2015-11-17 19:23:54 +01001124 atp_writew_io(dev, 0, 0x1c, val);
Ondrej Zary2bbbac42015-11-17 19:24:08 +01001125 udelay(2);
Ondrej Zaryc7fcc0892015-11-17 19:23:53 +01001126 break;
1127 }
1128 while (1) {
Martin Michlmayr0f6d93a2012-10-04 17:11:25 -07001129 /*
1130 * The funny division into multiple delays is to accomodate
1131 * arches like ARM where udelay() multiplies its argument by
1132 * a large number to initialize a loop counter. To avoid
1133 * overflow, the maximum supported udelay is 2000 microseconds.
1134 *
1135 * XXX it would be more polite to find a way to use msleep()
1136 */
1137 mdelay(2);
1138 udelay(48);
Ondrej Zary6a3cebb2015-11-17 19:23:54 +01001139 if ((atp_readb_io(dev, 0, 0x1c) & 0x80) == 0x00) { /* bsy ? */
1140 atp_writew_io(dev, 0, 0x1c, 0);
1141 atp_writeb_io(dev, 0, 0x1b, 0);
1142 atp_writeb_io(dev, 0, 0x15, 0);
1143 atp_writeb_io(dev, 0, 0x18, 0x09);
1144 while ((atp_readb_io(dev, 0, 0x1f) & 0x80) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 cpu_relax();
Ondrej Zary6a3cebb2015-11-17 19:23:54 +01001146 atp_readb_io(dev, 0, 0x17);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 return;
1148 }
1149 val &= 0x00ff; /* synchronization */
1150 val |= 0x3f00;
1151 fun_scam(dev, &val);
Ondrej Zary2bbbac42015-11-17 19:24:08 +01001152 udelay(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 val &= 0x00ff; /* isolation */
1154 val |= 0x2000;
1155 fun_scam(dev, &val);
Ondrej Zary2bbbac42015-11-17 19:24:08 +01001156 udelay(2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 i = 8;
1158 j = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
Ondrej Zaryc7fcc0892015-11-17 19:23:53 +01001160 while (1) {
Ondrej Zary6a3cebb2015-11-17 19:23:54 +01001161 if ((atp_readw_io(dev, 0, 0x1c) & 0x2000) == 0)
Ondrej Zaryc7fcc0892015-11-17 19:23:53 +01001162 continue;
Ondrej Zary2bbbac42015-11-17 19:24:08 +01001163 udelay(2);
Ondrej Zaryc7fcc0892015-11-17 19:23:53 +01001164 val &= 0x00ff; /* get ID_STRING */
1165 val |= 0x2000;
1166 k = fun_scam(dev, &val);
1167 if ((k & 0x03) == 0)
1168 break;
1169 mbuf[j] <<= 0x01;
1170 mbuf[j] &= 0xfe;
1171 if ((k & 0x02) != 0)
1172 mbuf[j] |= 0x01;
1173 i--;
1174 if (i > 0)
1175 continue;
1176 j++;
1177 i = 8;
1178 }
1179
1180 /* isolation complete.. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181/* mbuf[32]=0;
1182 printk(" \n%x %x %x %s\n ",assignid_map,mbuf[0],mbuf[1],&mbuf[2]); */
1183 i = 15;
1184 j = mbuf[0];
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001185 if ((j & 0x20) != 0) { /* bit5=1:ID up to 7 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 i = 7;
1187 }
Ondrej Zaryc7fcc0892015-11-17 19:23:53 +01001188 if ((j & 0x06) != 0) { /* IDvalid? */
1189 k = mbuf[1];
1190 while (1) {
1191 m = 1;
1192 m <<= k;
1193 if ((m & assignid_map) == 0)
1194 break;
1195 if (k > 0)
1196 k--;
1197 else
1198 break;
1199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 }
Ondrej Zaryc7fcc0892015-11-17 19:23:53 +01001201 if ((m & assignid_map) != 0) { /* srch from max acceptable ID# */
1202 k = i; /* max acceptable ID# */
1203 while (1) {
1204 m = 1;
1205 m <<= k;
1206 if ((m & assignid_map) == 0)
1207 break;
1208 if (k > 0)
1209 k--;
1210 else
1211 break;
1212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 }
Ondrej Zaryc7fcc0892015-11-17 19:23:53 +01001214 /* k=binID#, */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 assignid_map |= m;
1216 if (k < 8) {
1217 quintet[0] = 0x38; /* 1st dft ID<8 */
1218 } else {
1219 quintet[0] = 0x31; /* 1st ID>=8 */
1220 }
1221 k &= 0x07;
1222 quintet[1] = g2q_tab[k];
1223
1224 val &= 0x00ff; /* AssignID 1stQuintet,AH=001xxxxx */
1225 m = quintet[0] << 8;
1226 val |= m;
1227 fun_scam(dev, &val);
1228 val &= 0x00ff; /* AssignID 2ndQuintet,AH=001xxxxx */
1229 m = quintet[1] << 8;
1230 val |= m;
1231 fun_scam(dev, &val);
1232
Ondrej Zaryc7fcc0892015-11-17 19:23:53 +01001233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234}
1235
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236static void atp870u_free_tables(struct Scsi_Host *host)
1237{
1238 struct atp_unit *atp_dev = (struct atp_unit *)&host->hostdata;
1239 int j, k;
1240 for (j=0; j < 2; j++) {
1241 for (k = 0; k < 16; k++) {
1242 if (!atp_dev->id[j][k].prd_table)
1243 continue;
Hannes Reineckebcd5c592021-01-13 10:04:30 +01001244 dma_free_coherent(&atp_dev->pdev->dev, 1024,
1245 atp_dev->id[j][k].prd_table,
1246 atp_dev->id[j][k].prd_bus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 atp_dev->id[j][k].prd_table = NULL;
1248 }
1249 }
1250}
1251
1252static int atp870u_init_tables(struct Scsi_Host *host)
1253{
1254 struct atp_unit *atp_dev = (struct atp_unit *)&host->hostdata;
1255 int c,k;
1256 for(c=0;c < 2;c++) {
Hannes Reineckebcd5c592021-01-13 10:04:30 +01001257 for(k=0;k<16;k++) {
1258 atp_dev->id[c][k].prd_table =
1259 dma_alloc_coherent(&atp_dev->pdev->dev, 1024,
1260 &(atp_dev->id[c][k].prd_bus),
1261 GFP_KERNEL);
1262 if (!atp_dev->id[c][k].prd_table) {
1263 printk("atp870u_init_tables fail\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 atp870u_free_tables(host);
1265 return -ENOMEM;
1266 }
James Bottomleyb5683552005-09-15 08:59:36 -05001267 atp_dev->id[c][k].prdaddr = atp_dev->id[c][k].prd_bus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 atp_dev->id[c][k].devsp=0x20;
1269 atp_dev->id[c][k].devtype = 0x7f;
Hannes Reineckebcd5c592021-01-13 10:04:30 +01001270 atp_dev->id[c][k].curr_req = NULL;
1271 }
1272
1273 atp_dev->active_id[c] = 0;
1274 atp_dev->wide_id[c] = 0;
1275 atp_dev->host_id[c] = 0x07;
1276 atp_dev->quhd[c] = 0;
1277 atp_dev->quend[c] = 0;
1278 atp_dev->last_cmd[c] = 0xff;
1279 atp_dev->in_snd[c] = 0;
1280 atp_dev->in_int[c] = 0;
1281
1282 for (k = 0; k < qcnt; k++) {
1283 atp_dev->quereq[c][k] = NULL;
1284 }
1285 for (k = 0; k < 16; k++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 atp_dev->id[c][k].curr_req = NULL;
1287 atp_dev->sp[c][k] = 0x04;
Hannes Reineckebcd5c592021-01-13 10:04:30 +01001288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 }
1290 return 0;
1291}
1292
Ondrej Zary6a1961b2015-11-17 19:24:10 +01001293static void atp_set_host_id(struct atp_unit *atp, u8 c, u8 host_id)
1294{
1295 atp_writeb_io(atp, c, 0, host_id | 0x08);
1296 atp_writeb_io(atp, c, 0x18, 0);
1297 while ((atp_readb_io(atp, c, 0x1f) & 0x80) == 0)
1298 mdelay(1);
1299 atp_readb_io(atp, c, 0x17);
1300 atp_writeb_io(atp, c, 1, 8);
1301 atp_writeb_io(atp, c, 2, 0x7f);
1302 atp_writeb_io(atp, c, 0x11, 0x20);
1303}
1304
Ondrej Zary41902302015-11-17 19:24:28 +01001305static void atp870_init(struct Scsi_Host *shpnt)
1306{
1307 struct atp_unit *atpdev = shost_priv(shpnt);
1308 struct pci_dev *pdev = atpdev->pdev;
1309 unsigned char k, host_id;
1310 u8 scam_on;
1311 bool wide_chip =
1312 (pdev->device == PCI_DEVICE_ID_ARTOP_AEC7610 &&
1313 pdev->revision == 4) ||
1314 (pdev->device == PCI_DEVICE_ID_ARTOP_AEC7612UW) ||
1315 (pdev->device == PCI_DEVICE_ID_ARTOP_AEC7612SUW);
1316
1317 pci_read_config_byte(pdev, 0x49, &host_id);
1318
Hannes Reineckebcd5c592021-01-13 10:04:30 +01001319 dev_info(&pdev->dev, "ACARD AEC-671X PCI Ultra/W SCSI-2/3 "
1320 "Host Adapter: IO:%lx, IRQ:%d.\n",
Ondrej Zary41902302015-11-17 19:24:28 +01001321 shpnt->io_port, shpnt->irq);
1322
1323 atpdev->ioport[0] = shpnt->io_port;
1324 atpdev->pciport[0] = shpnt->io_port + 0x20;
1325 host_id &= 0x07;
1326 atpdev->host_id[0] = host_id;
1327 scam_on = atp_readb_pci(atpdev, 0, 2);
1328 atpdev->global_map[0] = atp_readb_base(atpdev, 0x2d);
1329 atpdev->ultra_map[0] = atp_readw_base(atpdev, 0x2e);
1330
1331 if (atpdev->ultra_map[0] == 0) {
1332 scam_on = 0x00;
1333 atpdev->global_map[0] = 0x20;
1334 atpdev->ultra_map[0] = 0xffff;
1335 }
1336
1337 if (pdev->revision > 0x07) /* check if atp876 chip */
1338 atp_writeb_base(atpdev, 0x3e, 0x00); /* enable terminator */
1339
1340 k = (atp_readb_base(atpdev, 0x3a) & 0xf3) | 0x10;
1341 atp_writeb_base(atpdev, 0x3a, k);
1342 atp_writeb_base(atpdev, 0x3a, k & 0xdf);
Jia-Ju Baidcaa0c12018-07-27 17:13:17 +08001343 msleep(32);
Ondrej Zary41902302015-11-17 19:24:28 +01001344 atp_writeb_base(atpdev, 0x3a, k);
Jia-Ju Baidcaa0c12018-07-27 17:13:17 +08001345 msleep(32);
Ondrej Zary41902302015-11-17 19:24:28 +01001346 atp_set_host_id(atpdev, 0, host_id);
1347
1348 tscam(shpnt, wide_chip, scam_on);
1349 atp_writeb_base(atpdev, 0x3a, atp_readb_base(atpdev, 0x3a) | 0x10);
1350 atp_is(atpdev, 0, wide_chip, 0);
1351 atp_writeb_base(atpdev, 0x3a, atp_readb_base(atpdev, 0x3a) & 0xef);
1352 atp_writeb_base(atpdev, 0x3b, atp_readb_base(atpdev, 0x3b) | 0x20);
1353 shpnt->max_id = wide_chip ? 16 : 8;
1354 shpnt->this_id = host_id;
1355}
1356
Ondrej Zaryc7e6a022015-11-17 19:24:26 +01001357static void atp880_init(struct Scsi_Host *shpnt)
1358{
1359 struct atp_unit *atpdev = shost_priv(shpnt);
1360 struct pci_dev *pdev = atpdev->pdev;
1361 unsigned char k, m, host_id;
1362 unsigned int n;
1363
1364 pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0x80);
1365
1366 atpdev->ioport[0] = shpnt->io_port + 0x40;
1367 atpdev->pciport[0] = shpnt->io_port + 0x28;
1368
1369 host_id = atp_readb_base(atpdev, 0x39) >> 4;
1370
Hannes Reineckebcd5c592021-01-13 10:04:30 +01001371 dev_info(&pdev->dev, "ACARD AEC-67160 PCI Ultra3 LVD "
1372 "Host Adapter: IO:%lx, IRQ:%d.\n",
Ondrej Zaryc7e6a022015-11-17 19:24:26 +01001373 shpnt->io_port, shpnt->irq);
1374 atpdev->host_id[0] = host_id;
1375
1376 atpdev->global_map[0] = atp_readb_base(atpdev, 0x35);
1377 atpdev->ultra_map[0] = atp_readw_base(atpdev, 0x3c);
1378
1379 n = 0x3f09;
1380 while (n < 0x4000) {
1381 m = 0;
1382 atp_writew_base(atpdev, 0x34, n);
1383 n += 0x0002;
1384 if (atp_readb_base(atpdev, 0x30) == 0xff)
1385 break;
1386
1387 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x30);
1388 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x31);
1389 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x32);
1390 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x33);
1391 atp_writew_base(atpdev, 0x34, n);
1392 n += 0x0002;
1393 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x30);
1394 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x31);
1395 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x32);
1396 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x33);
1397 atp_writew_base(atpdev, 0x34, n);
1398 n += 0x0002;
1399 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x30);
1400 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x31);
1401 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x32);
1402 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x33);
1403 atp_writew_base(atpdev, 0x34, n);
1404 n += 0x0002;
1405 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x30);
1406 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x31);
1407 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x32);
1408 atpdev->sp[0][m++] = atp_readb_base(atpdev, 0x33);
1409 n += 0x0018;
1410 }
1411 atp_writew_base(atpdev, 0x34, 0);
1412 atpdev->ultra_map[0] = 0;
1413 atpdev->async[0] = 0;
1414 for (k = 0; k < 16; k++) {
1415 n = 1 << k;
1416 if (atpdev->sp[0][k] > 1)
1417 atpdev->ultra_map[0] |= n;
1418 else
1419 if (atpdev->sp[0][k] == 0)
1420 atpdev->async[0] |= n;
1421 }
1422 atpdev->async[0] = ~(atpdev->async[0]);
1423 atp_writeb_base(atpdev, 0x35, atpdev->global_map[0]);
1424
1425 k = atp_readb_base(atpdev, 0x38) & 0x80;
1426 atp_writeb_base(atpdev, 0x38, k);
1427 atp_writeb_base(atpdev, 0x3b, 0x20);
Jia-Ju Baidcaa0c12018-07-27 17:13:17 +08001428 msleep(32);
Ondrej Zaryc7e6a022015-11-17 19:24:26 +01001429 atp_writeb_base(atpdev, 0x3b, 0);
Jia-Ju Baidcaa0c12018-07-27 17:13:17 +08001430 msleep(32);
Ondrej Zaryc7e6a022015-11-17 19:24:26 +01001431 atp_readb_io(atpdev, 0, 0x1b);
1432 atp_readb_io(atpdev, 0, 0x17);
1433
1434 atp_set_host_id(atpdev, 0, host_id);
1435
1436 tscam(shpnt, true, atp_readb_base(atpdev, 0x22));
1437 atp_is(atpdev, 0, true, atp_readb_base(atpdev, 0x3f) & 0x40);
1438 atp_writeb_base(atpdev, 0x38, 0xb0);
1439 shpnt->max_id = 16;
1440 shpnt->this_id = host_id;
1441}
1442
Ondrej Zaryecc6ff92015-11-17 19:24:27 +01001443static void atp885_init(struct Scsi_Host *shpnt)
1444{
1445 struct atp_unit *atpdev = shost_priv(shpnt);
1446 struct pci_dev *pdev = atpdev->pdev;
1447 unsigned char k, m, c;
1448 unsigned int n;
1449 unsigned char setupdata[2][16];
1450
Hannes Reineckebcd5c592021-01-13 10:04:30 +01001451 dev_info(&pdev->dev, "ACARD AEC-67162 PCI Ultra3 LVD "
1452 "Host Adapter: IO:%lx, IRQ:%d.\n",
Ondrej Zaryecc6ff92015-11-17 19:24:27 +01001453 shpnt->io_port, shpnt->irq);
1454
1455 atpdev->ioport[0] = shpnt->io_port + 0x80;
1456 atpdev->ioport[1] = shpnt->io_port + 0xc0;
1457 atpdev->pciport[0] = shpnt->io_port + 0x40;
1458 atpdev->pciport[1] = shpnt->io_port + 0x50;
1459
1460 c = atp_readb_base(atpdev, 0x29);
1461 atp_writeb_base(atpdev, 0x29, c | 0x04);
1462
1463 n = 0x1f80;
1464 while (n < 0x2000) {
1465 atp_writew_base(atpdev, 0x3c, n);
1466 if (atp_readl_base(atpdev, 0x38) == 0xffffffff)
1467 break;
1468 for (m = 0; m < 2; m++) {
1469 atpdev->global_map[m] = 0;
1470 for (k = 0; k < 4; k++) {
1471 atp_writew_base(atpdev, 0x3c, n++);
Hannes Reineckebcd5c592021-01-13 10:04:30 +01001472 ((u32 *)&setupdata[m][0])[k] =
1473 atp_readl_base(atpdev, 0x38);
Ondrej Zaryecc6ff92015-11-17 19:24:27 +01001474 }
1475 for (k = 0; k < 4; k++) {
1476 atp_writew_base(atpdev, 0x3c, n++);
Hannes Reineckebcd5c592021-01-13 10:04:30 +01001477 ((u32 *)&atpdev->sp[m][0])[k] =
1478 atp_readl_base(atpdev, 0x38);
Ondrej Zaryecc6ff92015-11-17 19:24:27 +01001479 }
1480 n += 8;
1481 }
1482 }
1483 c = atp_readb_base(atpdev, 0x29);
1484 atp_writeb_base(atpdev, 0x29, c & 0xfb);
1485 for (c = 0; c < 2; c++) {
1486 atpdev->ultra_map[c] = 0;
1487 atpdev->async[c] = 0;
1488 for (k = 0; k < 16; k++) {
1489 n = 1 << k;
1490 if (atpdev->sp[c][k] > 1)
1491 atpdev->ultra_map[c] |= n;
1492 else
1493 if (atpdev->sp[c][k] == 0)
1494 atpdev->async[c] |= n;
1495 }
1496 atpdev->async[c] = ~(atpdev->async[c]);
1497
1498 if (atpdev->global_map[c] == 0) {
1499 k = setupdata[c][1];
1500 if ((k & 0x40) != 0)
1501 atpdev->global_map[c] |= 0x20;
1502 k &= 0x07;
1503 atpdev->global_map[c] |= k;
1504 if ((setupdata[c][2] & 0x04) != 0)
1505 atpdev->global_map[c] |= 0x08;
1506 atpdev->host_id[c] = setupdata[c][0] & 0x07;
1507 }
1508 }
1509
1510 k = atp_readb_base(atpdev, 0x28) & 0x8f;
1511 k |= 0x10;
1512 atp_writeb_base(atpdev, 0x28, k);
1513 atp_writeb_pci(atpdev, 0, 1, 0x80);
1514 atp_writeb_pci(atpdev, 1, 1, 0x80);
Jia-Ju Baidcaa0c12018-07-27 17:13:17 +08001515 msleep(100);
Ondrej Zaryecc6ff92015-11-17 19:24:27 +01001516 atp_writeb_pci(atpdev, 0, 1, 0);
1517 atp_writeb_pci(atpdev, 1, 1, 0);
Jia-Ju Baidcaa0c12018-07-27 17:13:17 +08001518 msleep(1000);
Ondrej Zaryecc6ff92015-11-17 19:24:27 +01001519 atp_readb_io(atpdev, 0, 0x1b);
1520 atp_readb_io(atpdev, 0, 0x17);
1521 atp_readb_io(atpdev, 1, 0x1b);
1522 atp_readb_io(atpdev, 1, 0x17);
1523
1524 k = atpdev->host_id[0];
1525 if (k > 7)
1526 k = (k & 0x07) | 0x40;
1527 atp_set_host_id(atpdev, 0, k);
1528
1529 k = atpdev->host_id[1];
1530 if (k > 7)
1531 k = (k & 0x07) | 0x40;
1532 atp_set_host_id(atpdev, 1, k);
1533
Jia-Ju Baidcaa0c12018-07-27 17:13:17 +08001534 msleep(600); /* this delay used to be called tscam_885() */
Ondrej Zaryecc6ff92015-11-17 19:24:27 +01001535 dev_info(&pdev->dev, "Scanning Channel A SCSI Device ...\n");
1536 atp_is(atpdev, 0, true, atp_readb_io(atpdev, 0, 0x1b) >> 7);
1537 atp_writeb_io(atpdev, 0, 0x16, 0x80);
1538 dev_info(&pdev->dev, "Scanning Channel B SCSI Device ...\n");
1539 atp_is(atpdev, 1, true, atp_readb_io(atpdev, 1, 0x1b) >> 7);
1540 atp_writeb_io(atpdev, 1, 0x16, 0x80);
1541 k = atp_readb_base(atpdev, 0x28) & 0xcf;
1542 k |= 0xc0;
1543 atp_writeb_base(atpdev, 0x28, k);
1544 k = atp_readb_base(atpdev, 0x1f) | 0x80;
1545 atp_writeb_base(atpdev, 0x1f, k);
1546 k = atp_readb_base(atpdev, 0x29) | 0x01;
1547 atp_writeb_base(atpdev, 0x29, k);
1548 shpnt->max_id = 16;
1549 shpnt->max_lun = (atpdev->global_map[0] & 0x07) + 1;
1550 shpnt->max_channel = 1;
1551 shpnt->this_id = atpdev->host_id[0];
1552}
1553
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554/* return non-zero on detection */
1555static int atp870u_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
1556{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 struct Scsi_Host *shpnt = NULL;
Ondrej Zarybdd5ac42015-11-17 19:24:17 +01001558 struct atp_unit *atpdev;
Ondrej Zarybdd5ac42015-11-17 19:24:17 +01001559 int err;
Randy Dunlapdc6a78f2006-06-27 22:01:28 -07001560
Ondrej Zaryb1e85062015-11-17 19:24:18 +01001561 if (ent->device == PCI_DEVICE_ID_ARTOP_AEC7610 && pdev->revision < 2) {
1562 dev_err(&pdev->dev, "ATP850S chips (AEC6710L/F cards) are not supported.\n");
1563 return -ENODEV;
1564 }
1565
Ondrej Zarybdd5ac42015-11-17 19:24:17 +01001566 err = pci_enable_device(pdev);
1567 if (err)
1568 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569
Christoph Hellwig48ecddb2018-10-10 18:14:14 +02001570 if (dma_set_mask(&pdev->dev, DMA_BIT_MASK(32))) {
Hannes Reineckebcd5c592021-01-13 10:04:30 +01001571 printk(KERN_ERR "atp870u: DMA mask required but not available.\n");
1572 err = -EIO;
1573 goto disable_device;
1574 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575
Ondrej Zary11ec1312015-11-17 19:24:22 +01001576 err = pci_request_regions(pdev, "atp870u");
1577 if (err)
1578 goto disable_device;
1579 pci_set_master(pdev);
1580
Hannes Reineckebcd5c592021-01-13 10:04:30 +01001581 err = -ENOMEM;
Ondrej Zarybdd5ac42015-11-17 19:24:17 +01001582 shpnt = scsi_host_alloc(&atp870u_template, sizeof(struct atp_unit));
1583 if (!shpnt)
Ondrej Zary11ec1312015-11-17 19:24:22 +01001584 goto release_region;
Ondrej Zarybdd5ac42015-11-17 19:24:17 +01001585
1586 atpdev = shost_priv(shpnt);
1587
1588 atpdev->host = shpnt;
1589 atpdev->pdev = pdev;
1590 pci_set_drvdata(pdev, atpdev);
1591
Ondrej Zary6c9b9c52015-11-17 19:24:20 +01001592 shpnt->io_port = pci_resource_start(pdev, 0);
1593 shpnt->io_port &= 0xfffffff8;
1594 shpnt->n_io_port = pci_resource_len(pdev, 0);
1595 atpdev->baseport = shpnt->io_port;
1596 shpnt->unique_id = shpnt->io_port;
1597 shpnt->irq = pdev->irq;
Randy Dunlapdc6a78f2006-06-27 22:01:28 -07001598
Ondrej Zaryf5f53a32015-11-17 19:24:25 +01001599 err = atp870u_init_tables(shpnt);
1600 if (err) {
1601 dev_err(&pdev->dev, "Unable to allocate tables for Acard controller\n");
1602 goto unregister;
1603 }
1604
Ondrej Zaryc7e6a022015-11-17 19:24:26 +01001605 if (is880(atpdev))
1606 atp880_init(shpnt);
Ondrej Zaryecc6ff92015-11-17 19:24:27 +01001607 else if (is885(atpdev))
1608 atp885_init(shpnt);
Ondrej Zary41902302015-11-17 19:24:28 +01001609 else
1610 atp870_init(shpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611
Ondrej Zary1729c0d2015-11-17 19:24:23 +01001612 err = request_irq(shpnt->irq, atp870u_intr_handle, IRQF_SHARED, "atp870u", shpnt);
1613 if (err) {
1614 dev_err(&pdev->dev, "Unable to allocate IRQ %d.\n", shpnt->irq);
1615 goto free_tables;
1616 }
1617
1618 err = scsi_add_host(shpnt, &pdev->dev);
1619 if (err)
1620 goto scsi_add_fail;
1621 scsi_scan_host(shpnt);
1622
1623 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
1625scsi_add_fail:
Ondrej Zary6c9b9c52015-11-17 19:24:20 +01001626 free_irq(shpnt->irq, shpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627free_tables:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 atp870u_free_tables(shpnt);
1629unregister:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 scsi_host_put(shpnt);
Ondrej Zary11ec1312015-11-17 19:24:22 +01001631release_region:
1632 pci_release_regions(pdev);
Ondrej Zarybdd5ac42015-11-17 19:24:17 +01001633disable_device:
1634 pci_disable_device(pdev);
1635fail:
1636 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637}
1638
1639/* The abort command does not leave the device in a clean state where
1640 it is available to be used again. Until this gets worked out, we will
1641 leave it commented out. */
1642
1643static int atp870u_abort(struct scsi_cmnd * SCpnt)
1644{
1645 unsigned char j, k, c;
1646 struct scsi_cmnd *workrequ;
Hannes Reineckebcd5c592021-01-13 10:04:30 +01001647 struct atp_unit *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 struct Scsi_Host *host;
1649 host = SCpnt->device->host;
1650
1651 dev = (struct atp_unit *)&host->hostdata;
Jeff Garzik422c0d62005-10-24 18:05:09 -04001652 c = scmd_channel(SCpnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 printk(" atp870u: abort Channel = %x \n", c);
1654 printk("working=%x last_cmd=%x ", dev->working[c], dev->last_cmd[c]);
1655 printk(" quhdu=%x quendu=%x ", dev->quhd[c], dev->quend[c]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 for (j = 0; j < 0x18; j++) {
Ondrej Zary6a3cebb2015-11-17 19:23:54 +01001657 printk(" r%2x=%2x", j, atp_readb_io(dev, c, j));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 }
Ondrej Zary6a3cebb2015-11-17 19:23:54 +01001659 printk(" r1c=%2x", atp_readb_io(dev, c, 0x1c));
1660 printk(" r1f=%2x in_snd=%2x ", atp_readb_io(dev, c, 0x1f), dev->in_snd[c]);
1661 printk(" d00=%2x", atp_readb_pci(dev, c, 0x00));
1662 printk(" d02=%2x", atp_readb_pci(dev, c, 0x02));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 for(j=0;j<16;j++) {
1664 if (dev->id[c][j].curr_req != NULL) {
1665 workrequ = dev->id[c][j].curr_req;
1666 printk("\n que cdb= ");
1667 for (k=0; k < workrequ->cmd_len; k++) {
1668 printk(" %2x ",workrequ->cmnd[k]);
1669 }
1670 printk(" last_lenu= %x ",(unsigned int)dev->id[c][j].last_len);
1671 }
1672 }
1673 return SUCCESS;
1674}
1675
1676static const char *atp870u_info(struct Scsi_Host *notused)
1677{
1678 static char buffer[128];
1679
1680 strcpy(buffer, "ACARD AEC-6710/6712/67160 PCI Ultra/W/LVD SCSI-3 Adapter Driver V2.6+ac ");
1681
1682 return buffer;
1683}
1684
Al Virod773e422013-03-31 03:26:26 -04001685static int atp870u_show_info(struct seq_file *m, struct Scsi_Host *HBAptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686{
Rasmus Villemoes3d300792014-12-03 00:10:53 +01001687 seq_puts(m, "ACARD AEC-671X Driver Version: 2.6+ac\n\n"
1688 "Adapter Configuration:\n");
Al Virod773e422013-03-31 03:26:26 -04001689 seq_printf(m, " Base IO: %#.4lx\n", HBAptr->io_port);
1690 seq_printf(m, " IRQ: %d\n", HBAptr->irq);
1691 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692}
1693
1694
1695static int atp870u_biosparam(struct scsi_device *disk, struct block_device *dev,
1696 sector_t capacity, int *ip)
1697{
1698 int heads, sectors, cylinders;
1699
1700 heads = 64;
1701 sectors = 32;
1702 cylinders = (unsigned long)capacity / (heads * sectors);
1703 if (cylinders > 1024) {
1704 heads = 255;
1705 sectors = 63;
1706 cylinders = (unsigned long)capacity / (heads * sectors);
1707 }
1708 ip[0] = heads;
1709 ip[1] = sectors;
1710 ip[2] = cylinders;
1711
1712 return 0;
1713}
1714
1715static void atp870u_remove (struct pci_dev *pdev)
Hannes Reineckebcd5c592021-01-13 10:04:30 +01001716{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717 struct atp_unit *devext = pci_get_drvdata(pdev);
1718 struct Scsi_Host *pshost = devext->host;
Hannes Reineckebcd5c592021-01-13 10:04:30 +01001719
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 scsi_remove_host(pshost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 free_irq(pshost->irq, pshost);
Ondrej Zary11ec1312015-11-17 19:24:22 +01001722 pci_release_regions(pdev);
1723 pci_disable_device(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 atp870u_free_tables(pshost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 scsi_host_put(pshost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726}
1727MODULE_LICENSE("GPL");
1728
Bart Van Asschef44e1c62023-03-22 12:54:24 -07001729static const struct scsi_host_template atp870u_template = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 .module = THIS_MODULE,
Hannes Reineckebcd5c592021-01-13 10:04:30 +01001731 .name = "atp870u" /* name */,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 .proc_name = "atp870u",
Al Virod773e422013-03-31 03:26:26 -04001733 .show_info = atp870u_show_info,
Hannes Reineckebcd5c592021-01-13 10:04:30 +01001734 .info = atp870u_info /* info */,
1735 .queuecommand = atp870u_queuecommand /* queuecommand */,
1736 .eh_abort_handler = atp870u_abort /* abort */,
1737 .bios_param = atp870u_biosparam /* biosparm */,
1738 .can_queue = qcnt /* can_queue */,
1739 .this_id = 7 /* SCSI ID */,
1740 .sg_tablesize = ATP870U_SCATTER /*SG_ALL*/,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 .max_sectors = ATP870U_MAX_SECTORS,
1742};
1743
1744static struct pci_device_id atp870u_id_table[] = {
1745 { PCI_DEVICE(PCI_VENDOR_ID_ARTOP, ATP885_DEVID) },
Hannes Reineckebcd5c592021-01-13 10:04:30 +01001746 { PCI_DEVICE(PCI_VENDOR_ID_ARTOP, ATP880_DEVID1) },
1747 { PCI_DEVICE(PCI_VENDOR_ID_ARTOP, ATP880_DEVID2) },
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 { PCI_DEVICE(PCI_VENDOR_ID_ARTOP, PCI_DEVICE_ID_ARTOP_AEC7610) },
1749 { PCI_DEVICE(PCI_VENDOR_ID_ARTOP, PCI_DEVICE_ID_ARTOP_AEC7612UW) },
1750 { PCI_DEVICE(PCI_VENDOR_ID_ARTOP, PCI_DEVICE_ID_ARTOP_AEC7612U) },
1751 { PCI_DEVICE(PCI_VENDOR_ID_ARTOP, PCI_DEVICE_ID_ARTOP_AEC7612S) },
1752 { PCI_DEVICE(PCI_VENDOR_ID_ARTOP, PCI_DEVICE_ID_ARTOP_AEC7612D) },
1753 { PCI_DEVICE(PCI_VENDOR_ID_ARTOP, PCI_DEVICE_ID_ARTOP_AEC7612SUW) },
1754 { PCI_DEVICE(PCI_VENDOR_ID_ARTOP, PCI_DEVICE_ID_ARTOP_8060) },
1755 { 0, },
1756};
1757
1758MODULE_DEVICE_TABLE(pci, atp870u_id_table);
1759
1760static struct pci_driver atp870u_driver = {
1761 .id_table = atp870u_id_table,
1762 .name = "atp870u",
1763 .probe = atp870u_probe,
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -08001764 .remove = atp870u_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765};
1766
Ondrej Zary1ccd7d62015-11-17 19:24:13 +01001767module_pci_driver(atp870u_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768
Hannes Reineckebcd5c592021-01-13 10:04:30 +01001769static void atp_is(struct atp_unit *dev, unsigned char c, bool wide_chip,
1770 unsigned char lvdmode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771{
Ondrej Zaryfa50b302015-11-17 19:24:00 +01001772 unsigned char i, j, k, rmb, n;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 unsigned short int m;
1774 static unsigned char mbuf[512];
Ondrej Zary80b52a72015-11-17 19:23:58 +01001775 static unsigned char satn[9] = { 0, 0, 0, 0, 0, 0, 0, 6, 6 };
1776 static unsigned char inqd[9] = { 0x12, 0, 0, 0, 0x24, 0, 0, 0x24, 6 };
1777 static unsigned char synn[6] = { 0x80, 1, 3, 1, 0x19, 0x0e };
1778 unsigned char synu[6] = { 0x80, 1, 3, 1, 0x0a, 0x0e };
1779 static unsigned char synw[6] = { 0x80, 1, 3, 1, 0x19, 0x0e };
Ondrej Zary460da9182015-11-17 19:24:03 +01001780 static unsigned char synw_870[6] = { 0x80, 1, 3, 1, 0x0c, 0x07 };
Ondrej Zary80b52a72015-11-17 19:23:58 +01001781 unsigned char synuw[6] = { 0x80, 1, 3, 1, 0x0a, 0x0e };
1782 static unsigned char wide[6] = { 0x80, 1, 2, 3, 1, 0 };
1783 static unsigned char u3[9] = { 0x80, 1, 6, 4, 0x09, 00, 0x0e, 0x01, 0x02 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 for (i = 0; i < 16; i++) {
Ondrej Zary197fb8d2015-11-17 19:24:02 +01001786 if (!wide_chip && (i > 7))
1787 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 m = 1;
1789 m = m << i;
1790 if ((m & dev->active_id[c]) != 0) {
1791 continue;
1792 }
1793 if (i == dev->host_id[c]) {
1794 printk(KERN_INFO " ID: %2d Host Adapter\n", dev->host_id[c]);
1795 continue;
1796 }
Ondrej Zary197fb8d2015-11-17 19:24:02 +01001797 atp_writeb_io(dev, c, 0x1b, wide_chip ? 0x01 : 0x00);
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01001798 atp_writeb_io(dev, c, 1, 0x08);
1799 atp_writeb_io(dev, c, 2, 0x7f);
1800 atp_writeb_io(dev, c, 3, satn[0]);
1801 atp_writeb_io(dev, c, 4, satn[1]);
1802 atp_writeb_io(dev, c, 5, satn[2]);
1803 atp_writeb_io(dev, c, 6, satn[3]);
1804 atp_writeb_io(dev, c, 7, satn[4]);
1805 atp_writeb_io(dev, c, 8, satn[5]);
1806 atp_writeb_io(dev, c, 0x0f, 0);
1807 atp_writeb_io(dev, c, 0x11, dev->id[c][i].devsp);
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01001808 atp_writeb_io(dev, c, 0x12, 0);
1809 atp_writeb_io(dev, c, 0x13, satn[6]);
1810 atp_writeb_io(dev, c, 0x14, satn[7]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 j = i;
1812 if ((j & 0x08) != 0) {
1813 j = (j & 0x07) | 0x40;
1814 }
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01001815 atp_writeb_io(dev, c, 0x15, j);
1816 atp_writeb_io(dev, c, 0x18, satn[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01001818 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 cpu_relax();
Ondrej Zary80b52a72015-11-17 19:23:58 +01001820
1821 if (atp_readb_io(dev, c, 0x17) != 0x11 && atp_readb_io(dev, c, 0x17) != 0x8e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 continue;
Ondrej Zary80b52a72015-11-17 19:23:58 +01001823
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01001824 while (atp_readb_io(dev, c, 0x17) != 0x8e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 cpu_relax();
Ondrej Zary80b52a72015-11-17 19:23:58 +01001826
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 dev->active_id[c] |= m;
1828
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01001829 atp_writeb_io(dev, c, 0x10, 0x30);
Ondrej Zaryb922a442015-11-17 19:24:21 +01001830 if (is885(dev) || is880(dev))
Ondrej Zary460da9182015-11-17 19:24:03 +01001831 atp_writeb_io(dev, c, 0x14, 0x00);
1832 else /* result of is870() merge - is this a bug? */
1833 atp_writeb_io(dev, c, 0x04, 0x00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
1835phase_cmd:
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01001836 atp_writeb_io(dev, c, 0x18, 0x08);
Ondrej Zary80b52a72015-11-17 19:23:58 +01001837
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
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01001841 j = atp_readb_io(dev, c, 0x17);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 if (j != 0x16) {
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01001843 atp_writeb_io(dev, c, 0x10, 0x41);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 goto phase_cmd;
1845 }
1846sel_ok:
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01001847 atp_writeb_io(dev, c, 3, inqd[0]);
1848 atp_writeb_io(dev, c, 4, inqd[1]);
1849 atp_writeb_io(dev, c, 5, inqd[2]);
1850 atp_writeb_io(dev, c, 6, inqd[3]);
1851 atp_writeb_io(dev, c, 7, inqd[4]);
1852 atp_writeb_io(dev, c, 8, inqd[5]);
1853 atp_writeb_io(dev, c, 0x0f, 0);
1854 atp_writeb_io(dev, c, 0x11, dev->id[c][i].devsp);
1855 atp_writeb_io(dev, c, 0x12, 0);
1856 atp_writeb_io(dev, c, 0x13, inqd[6]);
1857 atp_writeb_io(dev, c, 0x14, inqd[7]);
1858 atp_writeb_io(dev, c, 0x18, inqd[8]);
Ondrej Zary80b52a72015-11-17 19:23:58 +01001859
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01001860 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 cpu_relax();
Ondrej Zary80b52a72015-11-17 19:23:58 +01001862
1863 if (atp_readb_io(dev, c, 0x17) != 0x11 && atp_readb_io(dev, c, 0x17) != 0x8e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 continue;
Ondrej Zary80b52a72015-11-17 19:23:58 +01001865
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01001866 while (atp_readb_io(dev, c, 0x17) != 0x8e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 cpu_relax();
Ondrej Zary80b52a72015-11-17 19:23:58 +01001868
Ondrej Zary197fb8d2015-11-17 19:24:02 +01001869 if (wide_chip)
1870 atp_writeb_io(dev, c, 0x1b, 0x00);
1871
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01001872 atp_writeb_io(dev, c, 0x18, 0x08);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 j = 0;
1874rd_inq_data:
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01001875 k = atp_readb_io(dev, c, 0x1f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 if ((k & 0x01) != 0) {
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01001877 mbuf[j++] = atp_readb_io(dev, c, 0x19);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 goto rd_inq_data;
1879 }
1880 if ((k & 0x80) == 0) {
1881 goto rd_inq_data;
1882 }
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01001883 j = atp_readb_io(dev, c, 0x17);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 if (j == 0x16) {
1885 goto inq_ok;
1886 }
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01001887 atp_writeb_io(dev, c, 0x10, 0x46);
1888 atp_writeb_io(dev, c, 0x12, 0);
1889 atp_writeb_io(dev, c, 0x13, 0);
1890 atp_writeb_io(dev, c, 0x14, 0);
1891 atp_writeb_io(dev, c, 0x18, 0x08);
Ondrej Zary80b52a72015-11-17 19:23:58 +01001892
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01001893 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 cpu_relax();
Ondrej Zary80b52a72015-11-17 19:23:58 +01001895
1896 if (atp_readb_io(dev, c, 0x17) != 0x16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 goto sel_ok;
Ondrej Zary80b52a72015-11-17 19:23:58 +01001898
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899inq_ok:
1900 mbuf[36] = 0;
Ondrej Zary80b52a72015-11-17 19:23:58 +01001901 printk(KERN_INFO " ID: %2d %s\n", i, &mbuf[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 dev->id[c][i].devtype = mbuf[0];
1903 rmb = mbuf[1];
1904 n = mbuf[7];
Ondrej Zary197fb8d2015-11-17 19:24:02 +01001905 if (!wide_chip)
1906 goto not_wide;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 if ((mbuf[7] & 0x60) == 0) {
1908 goto not_wide;
1909 }
Ondrej Zaryb922a442015-11-17 19:24:21 +01001910 if (is885(dev) || is880(dev)) {
Ondrej Zary197fb8d2015-11-17 19:24:02 +01001911 if ((i < 8) && ((dev->global_map[c] & 0x20) == 0))
1912 goto not_wide;
1913 } else { /* result of is870() merge - is this a bug? */
1914 if ((dev->global_map[c] & 0x20) == 0)
1915 goto not_wide;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 }
1917 if (lvdmode == 0) {
Ondrej Zary80b52a72015-11-17 19:23:58 +01001918 goto chg_wide;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 }
Ondrej Zary80b52a72015-11-17 19:23:58 +01001920 if (dev->sp[c][i] != 0x04) // force u2
1921 {
1922 goto chg_wide;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923 }
1924
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01001925 atp_writeb_io(dev, c, 0x1b, 0x01);
1926 atp_writeb_io(dev, c, 3, satn[0]);
1927 atp_writeb_io(dev, c, 4, satn[1]);
1928 atp_writeb_io(dev, c, 5, satn[2]);
1929 atp_writeb_io(dev, c, 6, satn[3]);
1930 atp_writeb_io(dev, c, 7, satn[4]);
1931 atp_writeb_io(dev, c, 8, satn[5]);
1932 atp_writeb_io(dev, c, 0x0f, 0);
1933 atp_writeb_io(dev, c, 0x11, dev->id[c][i].devsp);
1934 atp_writeb_io(dev, c, 0x12, 0);
1935 atp_writeb_io(dev, c, 0x13, satn[6]);
1936 atp_writeb_io(dev, c, 0x14, satn[7]);
1937 atp_writeb_io(dev, c, 0x18, satn[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01001939 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 cpu_relax();
Ondrej Zary80b52a72015-11-17 19:23:58 +01001941
1942 if (atp_readb_io(dev, c, 0x17) != 0x11 && atp_readb_io(dev, c, 0x17) != 0x8e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 continue;
Ondrej Zary80b52a72015-11-17 19:23:58 +01001944
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01001945 while (atp_readb_io(dev, c, 0x17) != 0x8e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 cpu_relax();
Ondrej Zary80b52a72015-11-17 19:23:58 +01001947
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948try_u3:
1949 j = 0;
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01001950 atp_writeb_io(dev, c, 0x14, 0x09);
1951 atp_writeb_io(dev, c, 0x18, 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01001953 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0) {
1954 if ((atp_readb_io(dev, c, 0x1f) & 0x01) != 0)
1955 atp_writeb_io(dev, c, 0x19, u3[j++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 cpu_relax();
1957 }
Ondrej Zary80b52a72015-11-17 19:23:58 +01001958
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01001959 while ((atp_readb_io(dev, c, 0x17) & 0x80) == 0x00)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 cpu_relax();
Ondrej Zary80b52a72015-11-17 19:23:58 +01001961
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01001962 j = atp_readb_io(dev, c, 0x17) & 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 if (j == 0x0f) {
1964 goto u3p_in;
1965 }
1966 if (j == 0x0a) {
1967 goto u3p_cmd;
1968 }
1969 if (j == 0x0e) {
1970 goto try_u3;
1971 }
1972 continue;
1973u3p_out:
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01001974 atp_writeb_io(dev, c, 0x18, 0x20);
1975 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0) {
1976 if ((atp_readb_io(dev, c, 0x1f) & 0x01) != 0)
1977 atp_writeb_io(dev, c, 0x19, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 cpu_relax();
1979 }
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01001980 j = atp_readb_io(dev, c, 0x17) & 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 if (j == 0x0f) {
1982 goto u3p_in;
1983 }
1984 if (j == 0x0a) {
1985 goto u3p_cmd;
1986 }
1987 if (j == 0x0e) {
1988 goto u3p_out;
1989 }
1990 continue;
1991u3p_in:
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01001992 atp_writeb_io(dev, c, 0x14, 0x09);
1993 atp_writeb_io(dev, c, 0x18, 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 k = 0;
1995u3p_in1:
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01001996 j = atp_readb_io(dev, c, 0x1f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 if ((j & 0x01) != 0) {
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01001998 mbuf[k++] = atp_readb_io(dev, c, 0x19);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 goto u3p_in1;
2000 }
2001 if ((j & 0x80) == 0x00) {
2002 goto u3p_in1;
2003 }
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002004 j = atp_readb_io(dev, c, 0x17) & 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 if (j == 0x0f) {
2006 goto u3p_in;
2007 }
2008 if (j == 0x0a) {
2009 goto u3p_cmd;
2010 }
2011 if (j == 0x0e) {
2012 goto u3p_out;
2013 }
2014 continue;
2015u3p_cmd:
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002016 atp_writeb_io(dev, c, 0x10, 0x30);
2017 atp_writeb_io(dev, c, 0x14, 0x00);
2018 atp_writeb_io(dev, c, 0x18, 0x08);
Ondrej Zary80b52a72015-11-17 19:23:58 +01002019
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002020 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00);
Ondrej Zary80b52a72015-11-17 19:23:58 +01002021
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002022 j = atp_readb_io(dev, c, 0x17);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 if (j != 0x16) {
2024 if (j == 0x4e) {
2025 goto u3p_out;
2026 }
2027 continue;
2028 }
2029 if (mbuf[0] != 0x01) {
2030 goto chg_wide;
2031 }
2032 if (mbuf[1] != 0x06) {
2033 goto chg_wide;
2034 }
2035 if (mbuf[2] != 0x04) {
2036 goto chg_wide;
2037 }
2038 if (mbuf[3] == 0x09) {
2039 m = 1;
2040 m = m << i;
2041 dev->wide_id[c] |= m;
2042 dev->id[c][i].devsp = 0xce;
Hannes Reineckebcd5c592021-01-13 10:04:30 +01002043#ifdef ED_DBGP
2044 printk("dev->id[%2d][%2d].devsp = %2x\n",
2045 c, i, dev->id[c][i].devsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046#endif
2047 continue;
2048 }
2049chg_wide:
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002050 atp_writeb_io(dev, c, 0x1b, 0x01);
2051 atp_writeb_io(dev, c, 3, satn[0]);
2052 atp_writeb_io(dev, c, 4, satn[1]);
2053 atp_writeb_io(dev, c, 5, satn[2]);
2054 atp_writeb_io(dev, c, 6, satn[3]);
2055 atp_writeb_io(dev, c, 7, satn[4]);
2056 atp_writeb_io(dev, c, 8, satn[5]);
2057 atp_writeb_io(dev, c, 0x0f, 0);
2058 atp_writeb_io(dev, c, 0x11, dev->id[c][i].devsp);
2059 atp_writeb_io(dev, c, 0x12, 0);
2060 atp_writeb_io(dev, c, 0x13, satn[6]);
2061 atp_writeb_io(dev, c, 0x14, satn[7]);
2062 atp_writeb_io(dev, c, 0x18, satn[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002064 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 cpu_relax();
Ondrej Zary80b52a72015-11-17 19:23:58 +01002066
Hannes Reineckebcd5c592021-01-13 10:04:30 +01002067 if (atp_readb_io(dev, c, 0x17) != 0x11 &&
2068 atp_readb_io(dev, c, 0x17) != 0x8e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 continue;
Ondrej Zary80b52a72015-11-17 19:23:58 +01002070
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002071 while (atp_readb_io(dev, c, 0x17) != 0x8e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 cpu_relax();
Ondrej Zary80b52a72015-11-17 19:23:58 +01002073
Linus Torvalds1da177e2005-04-16 15:20:36 -07002074try_wide:
2075 j = 0;
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002076 atp_writeb_io(dev, c, 0x14, 0x05);
2077 atp_writeb_io(dev, c, 0x18, 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002079 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0) {
2080 if ((atp_readb_io(dev, c, 0x1f) & 0x01) != 0)
2081 atp_writeb_io(dev, c, 0x19, wide[j++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 cpu_relax();
2083 }
Ondrej Zary80b52a72015-11-17 19:23:58 +01002084
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002085 while ((atp_readb_io(dev, c, 0x17) & 0x80) == 0x00)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 cpu_relax();
Ondrej Zary80b52a72015-11-17 19:23:58 +01002087
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002088 j = atp_readb_io(dev, c, 0x17) & 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 if (j == 0x0f) {
2090 goto widep_in;
2091 }
2092 if (j == 0x0a) {
2093 goto widep_cmd;
2094 }
2095 if (j == 0x0e) {
2096 goto try_wide;
2097 }
2098 continue;
2099widep_out:
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002100 atp_writeb_io(dev, c, 0x18, 0x20);
2101 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0) {
2102 if ((atp_readb_io(dev, c, 0x1f) & 0x01) != 0)
2103 atp_writeb_io(dev, c, 0x19, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 cpu_relax();
2105 }
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 widep_out;
2115 }
2116 continue;
2117widep_in:
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002118 atp_writeb_io(dev, c, 0x14, 0xff);
2119 atp_writeb_io(dev, c, 0x18, 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 k = 0;
2121widep_in1:
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002122 j = atp_readb_io(dev, c, 0x1f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 if ((j & 0x01) != 0) {
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002124 mbuf[k++] = atp_readb_io(dev, c, 0x19);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 goto widep_in1;
2126 }
2127 if ((j & 0x80) == 0x00) {
2128 goto widep_in1;
2129 }
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002130 j = atp_readb_io(dev, c, 0x17) & 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 if (j == 0x0f) {
2132 goto widep_in;
2133 }
2134 if (j == 0x0a) {
2135 goto widep_cmd;
2136 }
2137 if (j == 0x0e) {
2138 goto widep_out;
2139 }
2140 continue;
2141widep_cmd:
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002142 atp_writeb_io(dev, c, 0x10, 0x30);
2143 atp_writeb_io(dev, c, 0x14, 0x00);
2144 atp_writeb_io(dev, c, 0x18, 0x08);
Ondrej Zary80b52a72015-11-17 19:23:58 +01002145
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002146 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 cpu_relax();
Ondrej Zary80b52a72015-11-17 19:23:58 +01002148
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002149 j = atp_readb_io(dev, c, 0x17);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 if (j != 0x16) {
2151 if (j == 0x4e) {
2152 goto widep_out;
2153 }
2154 continue;
2155 }
2156 if (mbuf[0] != 0x01) {
2157 goto not_wide;
2158 }
2159 if (mbuf[1] != 0x02) {
2160 goto not_wide;
2161 }
2162 if (mbuf[2] != 0x03) {
2163 goto not_wide;
2164 }
2165 if (mbuf[3] != 0x01) {
2166 goto not_wide;
2167 }
2168 m = 1;
2169 m = m << i;
2170 dev->wide_id[c] |= m;
2171not_wide:
Hannes Reineckebcd5c592021-01-13 10:04:30 +01002172 if ((dev->id[c][i].devtype == 0x00) ||
2173 (dev->id[c][i].devtype == 0x07) ||
2174 ((dev->id[c][i].devtype == 0x05) && ((n & 0x10) != 0))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 m = 1;
2176 m = m << i;
2177 if ((dev->async[c] & m) != 0) {
Ondrej Zary80b52a72015-11-17 19:23:58 +01002178 goto set_sync;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 }
2180 }
2181 continue;
2182set_sync:
Ondrej Zaryb922a442015-11-17 19:24:21 +01002183 if ((!is885(dev) && !is880(dev)) || (dev->sp[c][i] == 0x02)) {
Ondrej Zary80b52a72015-11-17 19:23:58 +01002184 synu[4] = 0x0c;
2185 synuw[4] = 0x0c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 } else {
Ondrej Zary80b52a72015-11-17 19:23:58 +01002187 if (dev->sp[c][i] >= 0x03) {
2188 synu[4] = 0x0a;
2189 synuw[4] = 0x0a;
2190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 j = 0;
2193 if ((m & dev->wide_id[c]) != 0) {
2194 j |= 0x01;
2195 }
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002196 atp_writeb_io(dev, c, 0x1b, j);
2197 atp_writeb_io(dev, c, 3, satn[0]);
2198 atp_writeb_io(dev, c, 4, satn[1]);
2199 atp_writeb_io(dev, c, 5, satn[2]);
2200 atp_writeb_io(dev, c, 6, satn[3]);
2201 atp_writeb_io(dev, c, 7, satn[4]);
2202 atp_writeb_io(dev, c, 8, satn[5]);
2203 atp_writeb_io(dev, c, 0x0f, 0);
2204 atp_writeb_io(dev, c, 0x11, dev->id[c][i].devsp);
2205 atp_writeb_io(dev, c, 0x12, 0);
2206 atp_writeb_io(dev, c, 0x13, satn[6]);
2207 atp_writeb_io(dev, c, 0x14, satn[7]);
2208 atp_writeb_io(dev, c, 0x18, satn[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002210 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 cpu_relax();
Ondrej Zary80b52a72015-11-17 19:23:58 +01002212
Hannes Reineckebcd5c592021-01-13 10:04:30 +01002213 if (atp_readb_io(dev, c, 0x17) != 0x11 &&
2214 atp_readb_io(dev, c, 0x17) != 0x8e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 continue;
Ondrej Zary80b52a72015-11-17 19:23:58 +01002216
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002217 while (atp_readb_io(dev, c, 0x17) != 0x8e)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 cpu_relax();
Ondrej Zary80b52a72015-11-17 19:23:58 +01002219
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220try_sync:
2221 j = 0;
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002222 atp_writeb_io(dev, c, 0x14, 0x06);
2223 atp_writeb_io(dev, c, 0x18, 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002225 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0) {
2226 if ((atp_readb_io(dev, c, 0x1f) & 0x01) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 if ((m & dev->wide_id[c]) != 0) {
Ondrej Zaryb922a442015-11-17 19:24:21 +01002228 if (is885(dev) || is880(dev)) {
Ondrej Zary460da9182015-11-17 19:24:03 +01002229 if ((m & dev->ultra_map[c]) != 0) {
2230 atp_writeb_io(dev, c, 0x19, synuw[j++]);
2231 } else {
2232 atp_writeb_io(dev, c, 0x19, synw[j++]);
2233 }
2234 } else
2235 atp_writeb_io(dev, c, 0x19, synw_870[j++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236 } else {
2237 if ((m & dev->ultra_map[c]) != 0) {
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002238 atp_writeb_io(dev, c, 0x19, synu[j++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 } else {
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002240 atp_writeb_io(dev, c, 0x19, synn[j++]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 }
2242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 }
2244 }
Ondrej Zary80b52a72015-11-17 19:23:58 +01002245
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002246 while ((atp_readb_io(dev, c, 0x17) & 0x80) == 0x00)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 cpu_relax();
Ondrej Zary80b52a72015-11-17 19:23:58 +01002248
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002249 j = atp_readb_io(dev, c, 0x17) & 0x0f;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 if (j == 0x0f) {
2251 goto phase_ins;
2252 }
2253 if (j == 0x0a) {
2254 goto phase_cmds;
2255 }
2256 if (j == 0x0e) {
2257 goto try_sync;
2258 }
2259 continue;
2260phase_outs:
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002261 atp_writeb_io(dev, c, 0x18, 0x20);
2262 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00) {
2263 if ((atp_readb_io(dev, c, 0x1f) & 0x01) != 0x00)
2264 atp_writeb_io(dev, c, 0x19, 0x00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 cpu_relax();
2266 }
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002267 j = atp_readb_io(dev, c, 0x17);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 if (j == 0x85) {
2269 goto tar_dcons;
2270 }
2271 j &= 0x0f;
2272 if (j == 0x0f) {
2273 goto phase_ins;
2274 }
2275 if (j == 0x0a) {
2276 goto phase_cmds;
2277 }
2278 if (j == 0x0e) {
2279 goto phase_outs;
2280 }
2281 continue;
2282phase_ins:
Ondrej Zaryb922a442015-11-17 19:24:21 +01002283 if (is885(dev) || is880(dev))
Ondrej Zary460da9182015-11-17 19:24:03 +01002284 atp_writeb_io(dev, c, 0x14, 0x06);
2285 else
2286 atp_writeb_io(dev, c, 0x14, 0xff);
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002287 atp_writeb_io(dev, c, 0x18, 0x20);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 k = 0;
2289phase_ins1:
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002290 j = atp_readb_io(dev, c, 0x1f);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 if ((j & 0x01) != 0x00) {
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002292 mbuf[k++] = atp_readb_io(dev, c, 0x19);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 goto phase_ins1;
2294 }
2295 if ((j & 0x80) == 0x00) {
2296 goto phase_ins1;
2297 }
Ondrej Zary80b52a72015-11-17 19:23:58 +01002298
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002299 while ((atp_readb_io(dev, c, 0x17) & 0x80) == 0x00);
Ondrej Zary80b52a72015-11-17 19:23:58 +01002300
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002301 j = atp_readb_io(dev, c, 0x17);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 if (j == 0x85) {
2303 goto tar_dcons;
2304 }
2305 j &= 0x0f;
2306 if (j == 0x0f) {
2307 goto phase_ins;
2308 }
2309 if (j == 0x0a) {
2310 goto phase_cmds;
2311 }
2312 if (j == 0x0e) {
2313 goto phase_outs;
2314 }
2315 continue;
2316phase_cmds:
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002317 atp_writeb_io(dev, c, 0x10, 0x30);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318tar_dcons:
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002319 atp_writeb_io(dev, c, 0x14, 0x00);
2320 atp_writeb_io(dev, c, 0x18, 0x08);
Ondrej Zary80b52a72015-11-17 19:23:58 +01002321
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002322 while ((atp_readb_io(dev, c, 0x1f) & 0x80) == 0x00)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 cpu_relax();
Ondrej Zary80b52a72015-11-17 19:23:58 +01002324
Ondrej Zary5d2a5a42015-11-17 19:23:57 +01002325 j = atp_readb_io(dev, c, 0x17);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 if (j != 0x16) {
2327 continue;
2328 }
2329 if (mbuf[0] != 0x01) {
2330 continue;
2331 }
2332 if (mbuf[1] != 0x03) {
2333 continue;
2334 }
2335 if (mbuf[4] == 0x00) {
2336 continue;
2337 }
2338 if (mbuf[3] > 0x64) {
2339 continue;
2340 }
Ondrej Zaryb922a442015-11-17 19:24:21 +01002341 if (is885(dev) || is880(dev)) {
Ondrej Zary460da9182015-11-17 19:24:03 +01002342 if (mbuf[4] > 0x0e) {
2343 mbuf[4] = 0x0e;
2344 }
2345 } else {
2346 if (mbuf[4] > 0x0c) {
2347 mbuf[4] = 0x0c;
2348 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 }
2350 dev->id[c][i].devsp = mbuf[4];
Ondrej Zaryb922a442015-11-17 19:24:21 +01002351 if (is885(dev) || is880(dev))
Ondrej Zary460da9182015-11-17 19:24:03 +01002352 if (mbuf[3] < 0x0c) {
2353 j = 0xb0;
2354 goto set_syn_ok;
2355 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 if ((mbuf[3] < 0x0d) && (rmb == 0)) {
2357 j = 0xa0;
2358 goto set_syn_ok;
2359 }
2360 if (mbuf[3] < 0x1a) {
2361 j = 0x20;
2362 goto set_syn_ok;
2363 }
2364 if (mbuf[3] < 0x33) {
2365 j = 0x40;
2366 goto set_syn_ok;
2367 }
2368 if (mbuf[3] < 0x4c) {
2369 j = 0x50;
2370 goto set_syn_ok;
2371 }
2372 j = 0x60;
Ondrej Zary80b52a72015-11-17 19:23:58 +01002373set_syn_ok:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374 dev->id[c][i].devsp = (dev->id[c][i].devsp & 0x0f) | j;
Ondrej Zary80b52a72015-11-17 19:23:58 +01002375#ifdef ED_DBGP
Hannes Reineckebcd5c592021-01-13 10:04:30 +01002376 printk("dev->id[%2d][%2d].devsp = %2x\n",
2377 c,i,dev->id[c][i].devsp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378#endif
2379 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380}