]>
Commit | Line | Data |
---|---|---|
1fdffbce | 1 | /* |
f3a03b09 | 2 | * libata-sff.c - helper library for PCI IDE BMDMA |
1fdffbce JG |
3 | * |
4 | * Maintained by: Jeff Garzik <jgarzik@pobox.com> | |
5 | * Please ALWAYS copy linux-ide@vger.kernel.org | |
6 | * on emails. | |
7 | * | |
8 | * Copyright 2003-2006 Red Hat, Inc. All rights reserved. | |
9 | * Copyright 2003-2006 Jeff Garzik | |
10 | * | |
11 | * | |
12 | * This program is free software; you can redistribute it and/or modify | |
13 | * it under the terms of the GNU General Public License as published by | |
14 | * the Free Software Foundation; either version 2, or (at your option) | |
15 | * any later version. | |
16 | * | |
17 | * This program is distributed in the hope that it will be useful, | |
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | * GNU General Public License for more details. | |
21 | * | |
22 | * You should have received a copy of the GNU General Public License | |
23 | * along with this program; see the file COPYING. If not, write to | |
24 | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
25 | * | |
26 | * | |
27 | * libata documentation is available via 'make {ps|pdf}docs', | |
28 | * as Documentation/DocBook/libata.* | |
29 | * | |
30 | * Hardware documentation available from http://www.t13.org/ and | |
31 | * http://www.sata-io.org/ | |
32 | * | |
33 | */ | |
34 | ||
1fdffbce | 35 | #include <linux/kernel.h> |
5a0e3ad6 | 36 | #include <linux/gfp.h> |
1fdffbce JG |
37 | #include <linux/pci.h> |
38 | #include <linux/libata.h> | |
624d5c51 | 39 | #include <linux/highmem.h> |
1fdffbce JG |
40 | |
41 | #include "libata.h" | |
42 | ||
c429137a TH |
43 | static struct workqueue_struct *ata_sff_wq; |
44 | ||
624d5c51 TH |
45 | const struct ata_port_operations ata_sff_port_ops = { |
46 | .inherits = &ata_base_port_ops, | |
47 | ||
9363c382 TH |
48 | .qc_prep = ata_sff_qc_prep, |
49 | .qc_issue = ata_sff_qc_issue, | |
4c9bf4e7 | 50 | .qc_fill_rtf = ata_sff_qc_fill_rtf, |
9363c382 TH |
51 | |
52 | .freeze = ata_sff_freeze, | |
53 | .thaw = ata_sff_thaw, | |
0aa1113d | 54 | .prereset = ata_sff_prereset, |
9363c382 | 55 | .softreset = ata_sff_softreset, |
57c9efdf | 56 | .hardreset = sata_sff_hardreset, |
203c75b8 | 57 | .postreset = ata_sff_postreset, |
9363c382 | 58 | .error_handler = ata_sff_error_handler, |
9363c382 | 59 | |
5682ed33 TH |
60 | .sff_dev_select = ata_sff_dev_select, |
61 | .sff_check_status = ata_sff_check_status, | |
62 | .sff_tf_load = ata_sff_tf_load, | |
63 | .sff_tf_read = ata_sff_tf_read, | |
64 | .sff_exec_command = ata_sff_exec_command, | |
65 | .sff_data_xfer = ata_sff_data_xfer, | |
288623a0 | 66 | .sff_irq_clear = ata_sff_irq_clear, |
8244cd05 | 67 | .sff_drain_fifo = ata_sff_drain_fifo, |
624d5c51 | 68 | |
c96f1732 | 69 | .lost_interrupt = ata_sff_lost_interrupt, |
624d5c51 | 70 | }; |
0fe40ff8 | 71 | EXPORT_SYMBOL_GPL(ata_sff_port_ops); |
624d5c51 | 72 | |
624d5c51 TH |
73 | /** |
74 | * ata_fill_sg - Fill PCI IDE PRD table | |
75 | * @qc: Metadata associated with taskfile to be transferred | |
76 | * | |
77 | * Fill PCI IDE PRD (scatter-gather) table with segments | |
78 | * associated with the current disk command. | |
79 | * | |
80 | * LOCKING: | |
81 | * spin_lock_irqsave(host lock) | |
82 | * | |
83 | */ | |
84 | static void ata_fill_sg(struct ata_queued_cmd *qc) | |
85 | { | |
86 | struct ata_port *ap = qc->ap; | |
87 | struct scatterlist *sg; | |
88 | unsigned int si, pi; | |
89 | ||
90 | pi = 0; | |
91 | for_each_sg(qc->sg, sg, qc->n_elem, si) { | |
92 | u32 addr, offset; | |
93 | u32 sg_len, len; | |
94 | ||
95 | /* determine if physical DMA addr spans 64K boundary. | |
96 | * Note h/w doesn't support 64-bit, so we unconditionally | |
97 | * truncate dma_addr_t to u32. | |
98 | */ | |
99 | addr = (u32) sg_dma_address(sg); | |
100 | sg_len = sg_dma_len(sg); | |
101 | ||
102 | while (sg_len) { | |
103 | offset = addr & 0xffff; | |
104 | len = sg_len; | |
105 | if ((offset + sg_len) > 0x10000) | |
106 | len = 0x10000 - offset; | |
107 | ||
108 | ap->prd[pi].addr = cpu_to_le32(addr); | |
109 | ap->prd[pi].flags_len = cpu_to_le32(len & 0xffff); | |
110 | VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", pi, addr, len); | |
111 | ||
112 | pi++; | |
113 | sg_len -= len; | |
114 | addr += len; | |
115 | } | |
116 | } | |
117 | ||
118 | ap->prd[pi - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT); | |
119 | } | |
120 | ||
121 | /** | |
122 | * ata_fill_sg_dumb - Fill PCI IDE PRD table | |
123 | * @qc: Metadata associated with taskfile to be transferred | |
124 | * | |
125 | * Fill PCI IDE PRD (scatter-gather) table with segments | |
126 | * associated with the current disk command. Perform the fill | |
127 | * so that we avoid writing any length 64K records for | |
128 | * controllers that don't follow the spec. | |
129 | * | |
130 | * LOCKING: | |
131 | * spin_lock_irqsave(host lock) | |
132 | * | |
133 | */ | |
134 | static void ata_fill_sg_dumb(struct ata_queued_cmd *qc) | |
135 | { | |
136 | struct ata_port *ap = qc->ap; | |
137 | struct scatterlist *sg; | |
138 | unsigned int si, pi; | |
139 | ||
140 | pi = 0; | |
141 | for_each_sg(qc->sg, sg, qc->n_elem, si) { | |
142 | u32 addr, offset; | |
143 | u32 sg_len, len, blen; | |
144 | ||
145 | /* determine if physical DMA addr spans 64K boundary. | |
146 | * Note h/w doesn't support 64-bit, so we unconditionally | |
147 | * truncate dma_addr_t to u32. | |
148 | */ | |
149 | addr = (u32) sg_dma_address(sg); | |
150 | sg_len = sg_dma_len(sg); | |
151 | ||
152 | while (sg_len) { | |
153 | offset = addr & 0xffff; | |
154 | len = sg_len; | |
155 | if ((offset + sg_len) > 0x10000) | |
156 | len = 0x10000 - offset; | |
157 | ||
158 | blen = len & 0xffff; | |
159 | ap->prd[pi].addr = cpu_to_le32(addr); | |
160 | if (blen == 0) { | |
0fe40ff8 AC |
161 | /* Some PATA chipsets like the CS5530 can't |
162 | cope with 0x0000 meaning 64K as the spec | |
163 | says */ | |
624d5c51 TH |
164 | ap->prd[pi].flags_len = cpu_to_le32(0x8000); |
165 | blen = 0x8000; | |
166 | ap->prd[++pi].addr = cpu_to_le32(addr + 0x8000); | |
167 | } | |
168 | ap->prd[pi].flags_len = cpu_to_le32(blen); | |
169 | VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", pi, addr, len); | |
170 | ||
171 | pi++; | |
172 | sg_len -= len; | |
173 | addr += len; | |
174 | } | |
175 | } | |
176 | ||
177 | ap->prd[pi - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT); | |
178 | } | |
179 | ||
180 | /** | |
9363c382 | 181 | * ata_sff_qc_prep - Prepare taskfile for submission |
624d5c51 TH |
182 | * @qc: Metadata associated with taskfile to be prepared |
183 | * | |
184 | * Prepare ATA taskfile for submission. | |
185 | * | |
186 | * LOCKING: | |
187 | * spin_lock_irqsave(host lock) | |
188 | */ | |
9363c382 | 189 | void ata_sff_qc_prep(struct ata_queued_cmd *qc) |
624d5c51 TH |
190 | { |
191 | if (!(qc->flags & ATA_QCFLAG_DMAMAP)) | |
192 | return; | |
193 | ||
194 | ata_fill_sg(qc); | |
195 | } | |
0fe40ff8 | 196 | EXPORT_SYMBOL_GPL(ata_sff_qc_prep); |
624d5c51 TH |
197 | |
198 | /** | |
9363c382 | 199 | * ata_sff_dumb_qc_prep - Prepare taskfile for submission |
624d5c51 TH |
200 | * @qc: Metadata associated with taskfile to be prepared |
201 | * | |
202 | * Prepare ATA taskfile for submission. | |
203 | * | |
204 | * LOCKING: | |
205 | * spin_lock_irqsave(host lock) | |
206 | */ | |
9363c382 | 207 | void ata_sff_dumb_qc_prep(struct ata_queued_cmd *qc) |
624d5c51 TH |
208 | { |
209 | if (!(qc->flags & ATA_QCFLAG_DMAMAP)) | |
210 | return; | |
211 | ||
212 | ata_fill_sg_dumb(qc); | |
213 | } | |
0fe40ff8 | 214 | EXPORT_SYMBOL_GPL(ata_sff_dumb_qc_prep); |
624d5c51 | 215 | |
272f7884 | 216 | /** |
9363c382 | 217 | * ata_sff_check_status - Read device status reg & clear interrupt |
272f7884 TH |
218 | * @ap: port where the device is |
219 | * | |
220 | * Reads ATA taskfile status register for currently-selected device | |
221 | * and return its value. This also clears pending interrupts | |
222 | * from this device | |
223 | * | |
224 | * LOCKING: | |
225 | * Inherited from caller. | |
226 | */ | |
9363c382 | 227 | u8 ata_sff_check_status(struct ata_port *ap) |
272f7884 TH |
228 | { |
229 | return ioread8(ap->ioaddr.status_addr); | |
230 | } | |
0fe40ff8 | 231 | EXPORT_SYMBOL_GPL(ata_sff_check_status); |
272f7884 TH |
232 | |
233 | /** | |
9363c382 | 234 | * ata_sff_altstatus - Read device alternate status reg |
272f7884 TH |
235 | * @ap: port where the device is |
236 | * | |
237 | * Reads ATA taskfile alternate status register for | |
238 | * currently-selected device and return its value. | |
239 | * | |
240 | * Note: may NOT be used as the check_altstatus() entry in | |
241 | * ata_port_operations. | |
242 | * | |
243 | * LOCKING: | |
244 | * Inherited from caller. | |
245 | */ | |
a57c1bad | 246 | static u8 ata_sff_altstatus(struct ata_port *ap) |
624d5c51 | 247 | { |
5682ed33 TH |
248 | if (ap->ops->sff_check_altstatus) |
249 | return ap->ops->sff_check_altstatus(ap); | |
624d5c51 TH |
250 | |
251 | return ioread8(ap->ioaddr.altstatus_addr); | |
252 | } | |
253 | ||
a57c1bad AC |
254 | /** |
255 | * ata_sff_irq_status - Check if the device is busy | |
256 | * @ap: port where the device is | |
257 | * | |
258 | * Determine if the port is currently busy. Uses altstatus | |
259 | * if available in order to avoid clearing shared IRQ status | |
260 | * when finding an IRQ source. Non ctl capable devices don't | |
261 | * share interrupt lines fortunately for us. | |
262 | * | |
263 | * LOCKING: | |
264 | * Inherited from caller. | |
265 | */ | |
266 | static u8 ata_sff_irq_status(struct ata_port *ap) | |
267 | { | |
268 | u8 status; | |
269 | ||
270 | if (ap->ops->sff_check_altstatus || ap->ioaddr.altstatus_addr) { | |
271 | status = ata_sff_altstatus(ap); | |
272 | /* Not us: We are busy */ | |
273 | if (status & ATA_BUSY) | |
0fe40ff8 | 274 | return status; |
a57c1bad AC |
275 | } |
276 | /* Clear INTRQ latch */ | |
6311c90a | 277 | status = ap->ops->sff_check_status(ap); |
a57c1bad AC |
278 | return status; |
279 | } | |
280 | ||
281 | /** | |
282 | * ata_sff_sync - Flush writes | |
283 | * @ap: Port to wait for. | |
284 | * | |
285 | * CAUTION: | |
286 | * If we have an mmio device with no ctl and no altstatus | |
287 | * method this will fail. No such devices are known to exist. | |
288 | * | |
289 | * LOCKING: | |
290 | * Inherited from caller. | |
291 | */ | |
292 | ||
293 | static void ata_sff_sync(struct ata_port *ap) | |
294 | { | |
295 | if (ap->ops->sff_check_altstatus) | |
296 | ap->ops->sff_check_altstatus(ap); | |
297 | else if (ap->ioaddr.altstatus_addr) | |
298 | ioread8(ap->ioaddr.altstatus_addr); | |
299 | } | |
300 | ||
301 | /** | |
302 | * ata_sff_pause - Flush writes and wait 400nS | |
303 | * @ap: Port to pause for. | |
304 | * | |
305 | * CAUTION: | |
306 | * If we have an mmio device with no ctl and no altstatus | |
307 | * method this will fail. No such devices are known to exist. | |
308 | * | |
309 | * LOCKING: | |
310 | * Inherited from caller. | |
311 | */ | |
312 | ||
313 | void ata_sff_pause(struct ata_port *ap) | |
314 | { | |
315 | ata_sff_sync(ap); | |
316 | ndelay(400); | |
317 | } | |
0fe40ff8 | 318 | EXPORT_SYMBOL_GPL(ata_sff_pause); |
a57c1bad AC |
319 | |
320 | /** | |
321 | * ata_sff_dma_pause - Pause before commencing DMA | |
322 | * @ap: Port to pause for. | |
323 | * | |
324 | * Perform I/O fencing and ensure sufficient cycle delays occur | |
325 | * for the HDMA1:0 transition | |
326 | */ | |
0fe40ff8 | 327 | |
a57c1bad AC |
328 | void ata_sff_dma_pause(struct ata_port *ap) |
329 | { | |
330 | if (ap->ops->sff_check_altstatus || ap->ioaddr.altstatus_addr) { | |
331 | /* An altstatus read will cause the needed delay without | |
332 | messing up the IRQ status */ | |
333 | ata_sff_altstatus(ap); | |
334 | return; | |
335 | } | |
336 | /* There are no DMA controllers without ctl. BUG here to ensure | |
337 | we never violate the HDMA1:0 transition timing and risk | |
338 | corruption. */ | |
339 | BUG(); | |
340 | } | |
0fe40ff8 | 341 | EXPORT_SYMBOL_GPL(ata_sff_dma_pause); |
a57c1bad | 342 | |
624d5c51 | 343 | /** |
9363c382 | 344 | * ata_sff_busy_sleep - sleep until BSY clears, or timeout |
624d5c51 | 345 | * @ap: port containing status register to be polled |
341c2c95 TH |
346 | * @tmout_pat: impatience timeout in msecs |
347 | * @tmout: overall timeout in msecs | |
624d5c51 TH |
348 | * |
349 | * Sleep until ATA Status register bit BSY clears, | |
350 | * or a timeout occurs. | |
351 | * | |
352 | * LOCKING: | |
353 | * Kernel thread context (may sleep). | |
354 | * | |
355 | * RETURNS: | |
356 | * 0 on success, -errno otherwise. | |
357 | */ | |
9363c382 TH |
358 | int ata_sff_busy_sleep(struct ata_port *ap, |
359 | unsigned long tmout_pat, unsigned long tmout) | |
624d5c51 TH |
360 | { |
361 | unsigned long timer_start, timeout; | |
362 | u8 status; | |
363 | ||
9363c382 | 364 | status = ata_sff_busy_wait(ap, ATA_BUSY, 300); |
624d5c51 | 365 | timer_start = jiffies; |
341c2c95 | 366 | timeout = ata_deadline(timer_start, tmout_pat); |
624d5c51 TH |
367 | while (status != 0xff && (status & ATA_BUSY) && |
368 | time_before(jiffies, timeout)) { | |
369 | msleep(50); | |
9363c382 | 370 | status = ata_sff_busy_wait(ap, ATA_BUSY, 3); |
624d5c51 TH |
371 | } |
372 | ||
373 | if (status != 0xff && (status & ATA_BUSY)) | |
374 | ata_port_printk(ap, KERN_WARNING, | |
375 | "port is slow to respond, please be patient " | |
376 | "(Status 0x%x)\n", status); | |
377 | ||
341c2c95 | 378 | timeout = ata_deadline(timer_start, tmout); |
624d5c51 TH |
379 | while (status != 0xff && (status & ATA_BUSY) && |
380 | time_before(jiffies, timeout)) { | |
381 | msleep(50); | |
5682ed33 | 382 | status = ap->ops->sff_check_status(ap); |
624d5c51 TH |
383 | } |
384 | ||
385 | if (status == 0xff) | |
386 | return -ENODEV; | |
387 | ||
388 | if (status & ATA_BUSY) { | |
389 | ata_port_printk(ap, KERN_ERR, "port failed to respond " | |
390 | "(%lu secs, Status 0x%x)\n", | |
341c2c95 | 391 | DIV_ROUND_UP(tmout, 1000), status); |
624d5c51 TH |
392 | return -EBUSY; |
393 | } | |
394 | ||
395 | return 0; | |
396 | } | |
0fe40ff8 | 397 | EXPORT_SYMBOL_GPL(ata_sff_busy_sleep); |
624d5c51 | 398 | |
aa2731ad TH |
399 | static int ata_sff_check_ready(struct ata_link *link) |
400 | { | |
401 | u8 status = link->ap->ops->sff_check_status(link->ap); | |
402 | ||
78ab88f0 | 403 | return ata_check_ready(status); |
aa2731ad TH |
404 | } |
405 | ||
624d5c51 | 406 | /** |
9363c382 | 407 | * ata_sff_wait_ready - sleep until BSY clears, or timeout |
705e76be | 408 | * @link: SFF link to wait ready status for |
624d5c51 TH |
409 | * @deadline: deadline jiffies for the operation |
410 | * | |
411 | * Sleep until ATA Status register bit BSY clears, or timeout | |
412 | * occurs. | |
413 | * | |
414 | * LOCKING: | |
415 | * Kernel thread context (may sleep). | |
416 | * | |
417 | * RETURNS: | |
418 | * 0 on success, -errno otherwise. | |
419 | */ | |
705e76be | 420 | int ata_sff_wait_ready(struct ata_link *link, unsigned long deadline) |
624d5c51 | 421 | { |
aa2731ad | 422 | return ata_wait_ready(link, deadline, ata_sff_check_ready); |
624d5c51 | 423 | } |
0fe40ff8 | 424 | EXPORT_SYMBOL_GPL(ata_sff_wait_ready); |
624d5c51 | 425 | |
41dec29b SS |
426 | /** |
427 | * ata_sff_set_devctl - Write device control reg | |
428 | * @ap: port where the device is | |
429 | * @ctl: value to write | |
430 | * | |
431 | * Writes ATA taskfile device control register. | |
432 | * | |
433 | * Note: may NOT be used as the sff_set_devctl() entry in | |
434 | * ata_port_operations. | |
435 | * | |
436 | * LOCKING: | |
437 | * Inherited from caller. | |
438 | */ | |
439 | static void ata_sff_set_devctl(struct ata_port *ap, u8 ctl) | |
440 | { | |
441 | if (ap->ops->sff_set_devctl) | |
442 | ap->ops->sff_set_devctl(ap, ctl); | |
443 | else | |
444 | iowrite8(ctl, ap->ioaddr.ctl_addr); | |
445 | } | |
446 | ||
624d5c51 | 447 | /** |
9363c382 | 448 | * ata_sff_dev_select - Select device 0/1 on ATA bus |
624d5c51 TH |
449 | * @ap: ATA channel to manipulate |
450 | * @device: ATA device (numbered from zero) to select | |
451 | * | |
452 | * Use the method defined in the ATA specification to | |
453 | * make either device 0, or device 1, active on the | |
454 | * ATA channel. Works with both PIO and MMIO. | |
455 | * | |
456 | * May be used as the dev_select() entry in ata_port_operations. | |
457 | * | |
458 | * LOCKING: | |
459 | * caller. | |
460 | */ | |
9363c382 | 461 | void ata_sff_dev_select(struct ata_port *ap, unsigned int device) |
624d5c51 TH |
462 | { |
463 | u8 tmp; | |
464 | ||
465 | if (device == 0) | |
466 | tmp = ATA_DEVICE_OBS; | |
467 | else | |
468 | tmp = ATA_DEVICE_OBS | ATA_DEV1; | |
469 | ||
470 | iowrite8(tmp, ap->ioaddr.device_addr); | |
9363c382 | 471 | ata_sff_pause(ap); /* needed; also flushes, for mmio */ |
624d5c51 | 472 | } |
0fe40ff8 | 473 | EXPORT_SYMBOL_GPL(ata_sff_dev_select); |
624d5c51 TH |
474 | |
475 | /** | |
476 | * ata_dev_select - Select device 0/1 on ATA bus | |
477 | * @ap: ATA channel to manipulate | |
478 | * @device: ATA device (numbered from zero) to select | |
479 | * @wait: non-zero to wait for Status register BSY bit to clear | |
480 | * @can_sleep: non-zero if context allows sleeping | |
481 | * | |
482 | * Use the method defined in the ATA specification to | |
483 | * make either device 0, or device 1, active on the | |
484 | * ATA channel. | |
485 | * | |
9363c382 TH |
486 | * This is a high-level version of ata_sff_dev_select(), which |
487 | * additionally provides the services of inserting the proper | |
488 | * pauses and status polling, where needed. | |
624d5c51 TH |
489 | * |
490 | * LOCKING: | |
491 | * caller. | |
492 | */ | |
c7a8209f | 493 | static void ata_dev_select(struct ata_port *ap, unsigned int device, |
624d5c51 TH |
494 | unsigned int wait, unsigned int can_sleep) |
495 | { | |
496 | if (ata_msg_probe(ap)) | |
497 | ata_port_printk(ap, KERN_INFO, "ata_dev_select: ENTER, " | |
498 | "device %u, wait %u\n", device, wait); | |
499 | ||
500 | if (wait) | |
501 | ata_wait_idle(ap); | |
502 | ||
5682ed33 | 503 | ap->ops->sff_dev_select(ap, device); |
624d5c51 TH |
504 | |
505 | if (wait) { | |
506 | if (can_sleep && ap->link.device[device].class == ATA_DEV_ATAPI) | |
507 | msleep(150); | |
508 | ata_wait_idle(ap); | |
509 | } | |
510 | } | |
511 | ||
512 | /** | |
9363c382 | 513 | * ata_sff_irq_on - Enable interrupts on a port. |
624d5c51 TH |
514 | * @ap: Port on which interrupts are enabled. |
515 | * | |
516 | * Enable interrupts on a legacy IDE device using MMIO or PIO, | |
517 | * wait for idle, clear any pending interrupts. | |
518 | * | |
e42a542b SS |
519 | * Note: may NOT be used as the sff_irq_on() entry in |
520 | * ata_port_operations. | |
521 | * | |
624d5c51 TH |
522 | * LOCKING: |
523 | * Inherited from caller. | |
524 | */ | |
e42a542b | 525 | void ata_sff_irq_on(struct ata_port *ap) |
624d5c51 TH |
526 | { |
527 | struct ata_ioports *ioaddr = &ap->ioaddr; | |
e42a542b SS |
528 | |
529 | if (ap->ops->sff_irq_on) { | |
530 | ap->ops->sff_irq_on(ap); | |
531 | return; | |
532 | } | |
624d5c51 TH |
533 | |
534 | ap->ctl &= ~ATA_NIEN; | |
535 | ap->last_ctl = ap->ctl; | |
536 | ||
e42a542b SS |
537 | if (ap->ops->sff_set_devctl || ioaddr->ctl_addr) |
538 | ata_sff_set_devctl(ap, ap->ctl); | |
539 | ata_wait_idle(ap); | |
624d5c51 | 540 | |
5682ed33 | 541 | ap->ops->sff_irq_clear(ap); |
624d5c51 | 542 | } |
0fe40ff8 | 543 | EXPORT_SYMBOL_GPL(ata_sff_irq_on); |
624d5c51 TH |
544 | |
545 | /** | |
9363c382 | 546 | * ata_sff_irq_clear - Clear PCI IDE BMDMA interrupt. |
624d5c51 TH |
547 | * @ap: Port associated with this ATA transaction. |
548 | * | |
549 | * Clear interrupt and error flags in DMA status register. | |
550 | * | |
551 | * May be used as the irq_clear() entry in ata_port_operations. | |
552 | * | |
553 | * LOCKING: | |
554 | * spin_lock_irqsave(host lock) | |
555 | */ | |
9363c382 | 556 | void ata_sff_irq_clear(struct ata_port *ap) |
624d5c51 TH |
557 | { |
558 | void __iomem *mmio = ap->ioaddr.bmdma_addr; | |
559 | ||
560 | if (!mmio) | |
561 | return; | |
562 | ||
563 | iowrite8(ioread8(mmio + ATA_DMA_STATUS), mmio + ATA_DMA_STATUS); | |
564 | } | |
0fe40ff8 | 565 | EXPORT_SYMBOL_GPL(ata_sff_irq_clear); |
624d5c51 TH |
566 | |
567 | /** | |
9363c382 | 568 | * ata_sff_tf_load - send taskfile registers to host controller |
624d5c51 TH |
569 | * @ap: Port to which output is sent |
570 | * @tf: ATA taskfile register set | |
571 | * | |
572 | * Outputs ATA taskfile to standard ATA host controller. | |
573 | * | |
574 | * LOCKING: | |
575 | * Inherited from caller. | |
576 | */ | |
9363c382 | 577 | void ata_sff_tf_load(struct ata_port *ap, const struct ata_taskfile *tf) |
624d5c51 TH |
578 | { |
579 | struct ata_ioports *ioaddr = &ap->ioaddr; | |
580 | unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR; | |
581 | ||
582 | if (tf->ctl != ap->last_ctl) { | |
583 | if (ioaddr->ctl_addr) | |
584 | iowrite8(tf->ctl, ioaddr->ctl_addr); | |
585 | ap->last_ctl = tf->ctl; | |
624d5c51 TH |
586 | } |
587 | ||
588 | if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) { | |
efcb3cf7 | 589 | WARN_ON_ONCE(!ioaddr->ctl_addr); |
624d5c51 TH |
590 | iowrite8(tf->hob_feature, ioaddr->feature_addr); |
591 | iowrite8(tf->hob_nsect, ioaddr->nsect_addr); | |
592 | iowrite8(tf->hob_lbal, ioaddr->lbal_addr); | |
593 | iowrite8(tf->hob_lbam, ioaddr->lbam_addr); | |
594 | iowrite8(tf->hob_lbah, ioaddr->lbah_addr); | |
595 | VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n", | |
596 | tf->hob_feature, | |
597 | tf->hob_nsect, | |
598 | tf->hob_lbal, | |
599 | tf->hob_lbam, | |
600 | tf->hob_lbah); | |
601 | } | |
602 | ||
603 | if (is_addr) { | |
604 | iowrite8(tf->feature, ioaddr->feature_addr); | |
605 | iowrite8(tf->nsect, ioaddr->nsect_addr); | |
606 | iowrite8(tf->lbal, ioaddr->lbal_addr); | |
607 | iowrite8(tf->lbam, ioaddr->lbam_addr); | |
608 | iowrite8(tf->lbah, ioaddr->lbah_addr); | |
609 | VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n", | |
610 | tf->feature, | |
611 | tf->nsect, | |
612 | tf->lbal, | |
613 | tf->lbam, | |
614 | tf->lbah); | |
615 | } | |
616 | ||
617 | if (tf->flags & ATA_TFLAG_DEVICE) { | |
618 | iowrite8(tf->device, ioaddr->device_addr); | |
619 | VPRINTK("device 0x%X\n", tf->device); | |
620 | } | |
624d5c51 | 621 | } |
0fe40ff8 | 622 | EXPORT_SYMBOL_GPL(ata_sff_tf_load); |
624d5c51 TH |
623 | |
624 | /** | |
9363c382 | 625 | * ata_sff_tf_read - input device's ATA taskfile shadow registers |
624d5c51 TH |
626 | * @ap: Port from which input is read |
627 | * @tf: ATA taskfile register set for storing input | |
628 | * | |
629 | * Reads ATA taskfile registers for currently-selected device | |
630 | * into @tf. Assumes the device has a fully SFF compliant task file | |
631 | * layout and behaviour. If you device does not (eg has a different | |
632 | * status method) then you will need to provide a replacement tf_read | |
633 | * | |
634 | * LOCKING: | |
635 | * Inherited from caller. | |
636 | */ | |
9363c382 | 637 | void ata_sff_tf_read(struct ata_port *ap, struct ata_taskfile *tf) |
624d5c51 TH |
638 | { |
639 | struct ata_ioports *ioaddr = &ap->ioaddr; | |
640 | ||
9363c382 | 641 | tf->command = ata_sff_check_status(ap); |
624d5c51 TH |
642 | tf->feature = ioread8(ioaddr->error_addr); |
643 | tf->nsect = ioread8(ioaddr->nsect_addr); | |
644 | tf->lbal = ioread8(ioaddr->lbal_addr); | |
645 | tf->lbam = ioread8(ioaddr->lbam_addr); | |
646 | tf->lbah = ioread8(ioaddr->lbah_addr); | |
647 | tf->device = ioread8(ioaddr->device_addr); | |
648 | ||
649 | if (tf->flags & ATA_TFLAG_LBA48) { | |
650 | if (likely(ioaddr->ctl_addr)) { | |
651 | iowrite8(tf->ctl | ATA_HOB, ioaddr->ctl_addr); | |
652 | tf->hob_feature = ioread8(ioaddr->error_addr); | |
653 | tf->hob_nsect = ioread8(ioaddr->nsect_addr); | |
654 | tf->hob_lbal = ioread8(ioaddr->lbal_addr); | |
655 | tf->hob_lbam = ioread8(ioaddr->lbam_addr); | |
656 | tf->hob_lbah = ioread8(ioaddr->lbah_addr); | |
657 | iowrite8(tf->ctl, ioaddr->ctl_addr); | |
658 | ap->last_ctl = tf->ctl; | |
659 | } else | |
efcb3cf7 | 660 | WARN_ON_ONCE(1); |
624d5c51 TH |
661 | } |
662 | } | |
0fe40ff8 | 663 | EXPORT_SYMBOL_GPL(ata_sff_tf_read); |
624d5c51 TH |
664 | |
665 | /** | |
9363c382 | 666 | * ata_sff_exec_command - issue ATA command to host controller |
624d5c51 TH |
667 | * @ap: port to which command is being issued |
668 | * @tf: ATA taskfile register set | |
669 | * | |
670 | * Issues ATA command, with proper synchronization with interrupt | |
671 | * handler / other threads. | |
672 | * | |
673 | * LOCKING: | |
674 | * spin_lock_irqsave(host lock) | |
675 | */ | |
9363c382 | 676 | void ata_sff_exec_command(struct ata_port *ap, const struct ata_taskfile *tf) |
624d5c51 TH |
677 | { |
678 | DPRINTK("ata%u: cmd 0x%X\n", ap->print_id, tf->command); | |
679 | ||
680 | iowrite8(tf->command, ap->ioaddr.command_addr); | |
9363c382 | 681 | ata_sff_pause(ap); |
624d5c51 | 682 | } |
0fe40ff8 | 683 | EXPORT_SYMBOL_GPL(ata_sff_exec_command); |
624d5c51 TH |
684 | |
685 | /** | |
686 | * ata_tf_to_host - issue ATA taskfile to host controller | |
687 | * @ap: port to which command is being issued | |
688 | * @tf: ATA taskfile register set | |
689 | * | |
690 | * Issues ATA taskfile register set to ATA host controller, | |
691 | * with proper synchronization with interrupt handler and | |
692 | * other threads. | |
693 | * | |
694 | * LOCKING: | |
695 | * spin_lock_irqsave(host lock) | |
696 | */ | |
697 | static inline void ata_tf_to_host(struct ata_port *ap, | |
698 | const struct ata_taskfile *tf) | |
699 | { | |
5682ed33 TH |
700 | ap->ops->sff_tf_load(ap, tf); |
701 | ap->ops->sff_exec_command(ap, tf); | |
624d5c51 TH |
702 | } |
703 | ||
704 | /** | |
9363c382 | 705 | * ata_sff_data_xfer - Transfer data by PIO |
624d5c51 TH |
706 | * @dev: device to target |
707 | * @buf: data buffer | |
708 | * @buflen: buffer length | |
709 | * @rw: read/write | |
710 | * | |
711 | * Transfer data from/to the device data register by PIO. | |
712 | * | |
713 | * LOCKING: | |
714 | * Inherited from caller. | |
715 | * | |
716 | * RETURNS: | |
717 | * Bytes consumed. | |
718 | */ | |
9363c382 TH |
719 | unsigned int ata_sff_data_xfer(struct ata_device *dev, unsigned char *buf, |
720 | unsigned int buflen, int rw) | |
624d5c51 TH |
721 | { |
722 | struct ata_port *ap = dev->link->ap; | |
723 | void __iomem *data_addr = ap->ioaddr.data_addr; | |
724 | unsigned int words = buflen >> 1; | |
725 | ||
726 | /* Transfer multiple of 2 bytes */ | |
727 | if (rw == READ) | |
728 | ioread16_rep(data_addr, buf, words); | |
729 | else | |
730 | iowrite16_rep(data_addr, buf, words); | |
731 | ||
2102d749 | 732 | /* Transfer trailing byte, if any. */ |
624d5c51 | 733 | if (unlikely(buflen & 0x01)) { |
2102d749 | 734 | unsigned char pad[2]; |
624d5c51 | 735 | |
2102d749 SS |
736 | /* Point buf to the tail of buffer */ |
737 | buf += buflen - 1; | |
738 | ||
739 | /* | |
740 | * Use io*16_rep() accessors here as well to avoid pointlessly | |
972b94ff | 741 | * swapping bytes to and from on the big endian machines... |
2102d749 | 742 | */ |
624d5c51 | 743 | if (rw == READ) { |
2102d749 SS |
744 | ioread16_rep(data_addr, pad, 1); |
745 | *buf = pad[0]; | |
624d5c51 | 746 | } else { |
2102d749 SS |
747 | pad[0] = *buf; |
748 | iowrite16_rep(data_addr, pad, 1); | |
624d5c51 TH |
749 | } |
750 | words++; | |
751 | } | |
752 | ||
753 | return words << 1; | |
754 | } | |
0fe40ff8 | 755 | EXPORT_SYMBOL_GPL(ata_sff_data_xfer); |
624d5c51 | 756 | |
871af121 AC |
757 | /** |
758 | * ata_sff_data_xfer32 - Transfer data by PIO | |
759 | * @dev: device to target | |
760 | * @buf: data buffer | |
761 | * @buflen: buffer length | |
762 | * @rw: read/write | |
763 | * | |
764 | * Transfer data from/to the device data register by PIO using 32bit | |
765 | * I/O operations. | |
766 | * | |
767 | * LOCKING: | |
768 | * Inherited from caller. | |
769 | * | |
770 | * RETURNS: | |
771 | * Bytes consumed. | |
772 | */ | |
773 | ||
774 | unsigned int ata_sff_data_xfer32(struct ata_device *dev, unsigned char *buf, | |
775 | unsigned int buflen, int rw) | |
776 | { | |
777 | struct ata_port *ap = dev->link->ap; | |
778 | void __iomem *data_addr = ap->ioaddr.data_addr; | |
779 | unsigned int words = buflen >> 2; | |
780 | int slop = buflen & 3; | |
972b94ff | 781 | |
e3cf95dd AC |
782 | if (!(ap->pflags & ATA_PFLAG_PIO32)) |
783 | return ata_sff_data_xfer(dev, buf, buflen, rw); | |
871af121 AC |
784 | |
785 | /* Transfer multiple of 4 bytes */ | |
786 | if (rw == READ) | |
787 | ioread32_rep(data_addr, buf, words); | |
788 | else | |
789 | iowrite32_rep(data_addr, buf, words); | |
790 | ||
d1b3525b | 791 | /* Transfer trailing bytes, if any */ |
871af121 | 792 | if (unlikely(slop)) { |
d1b3525b SS |
793 | unsigned char pad[4]; |
794 | ||
795 | /* Point buf to the tail of buffer */ | |
796 | buf += buflen - slop; | |
797 | ||
798 | /* | |
799 | * Use io*_rep() accessors here as well to avoid pointlessly | |
972b94ff | 800 | * swapping bytes to and from on the big endian machines... |
d1b3525b | 801 | */ |
871af121 | 802 | if (rw == READ) { |
d1b3525b SS |
803 | if (slop < 3) |
804 | ioread16_rep(data_addr, pad, 1); | |
805 | else | |
806 | ioread32_rep(data_addr, pad, 1); | |
807 | memcpy(buf, pad, slop); | |
871af121 | 808 | } else { |
d1b3525b SS |
809 | memcpy(pad, buf, slop); |
810 | if (slop < 3) | |
811 | iowrite16_rep(data_addr, pad, 1); | |
812 | else | |
813 | iowrite32_rep(data_addr, pad, 1); | |
871af121 | 814 | } |
871af121 | 815 | } |
d1b3525b | 816 | return (buflen + 1) & ~1; |
871af121 AC |
817 | } |
818 | EXPORT_SYMBOL_GPL(ata_sff_data_xfer32); | |
819 | ||
624d5c51 | 820 | /** |
9363c382 | 821 | * ata_sff_data_xfer_noirq - Transfer data by PIO |
624d5c51 TH |
822 | * @dev: device to target |
823 | * @buf: data buffer | |
824 | * @buflen: buffer length | |
825 | * @rw: read/write | |
826 | * | |
827 | * Transfer data from/to the device data register by PIO. Do the | |
828 | * transfer with interrupts disabled. | |
829 | * | |
830 | * LOCKING: | |
831 | * Inherited from caller. | |
832 | * | |
833 | * RETURNS: | |
834 | * Bytes consumed. | |
835 | */ | |
9363c382 TH |
836 | unsigned int ata_sff_data_xfer_noirq(struct ata_device *dev, unsigned char *buf, |
837 | unsigned int buflen, int rw) | |
624d5c51 TH |
838 | { |
839 | unsigned long flags; | |
840 | unsigned int consumed; | |
841 | ||
842 | local_irq_save(flags); | |
9363c382 | 843 | consumed = ata_sff_data_xfer(dev, buf, buflen, rw); |
624d5c51 TH |
844 | local_irq_restore(flags); |
845 | ||
846 | return consumed; | |
847 | } | |
0fe40ff8 | 848 | EXPORT_SYMBOL_GPL(ata_sff_data_xfer_noirq); |
624d5c51 TH |
849 | |
850 | /** | |
851 | * ata_pio_sector - Transfer a sector of data. | |
852 | * @qc: Command on going | |
853 | * | |
854 | * Transfer qc->sect_size bytes of data from/to the ATA device. | |
855 | * | |
856 | * LOCKING: | |
857 | * Inherited from caller. | |
858 | */ | |
859 | static void ata_pio_sector(struct ata_queued_cmd *qc) | |
860 | { | |
861 | int do_write = (qc->tf.flags & ATA_TFLAG_WRITE); | |
862 | struct ata_port *ap = qc->ap; | |
863 | struct page *page; | |
864 | unsigned int offset; | |
865 | unsigned char *buf; | |
866 | ||
867 | if (qc->curbytes == qc->nbytes - qc->sect_size) | |
868 | ap->hsm_task_state = HSM_ST_LAST; | |
869 | ||
870 | page = sg_page(qc->cursg); | |
871 | offset = qc->cursg->offset + qc->cursg_ofs; | |
872 | ||
873 | /* get the current page and offset */ | |
874 | page = nth_page(page, (offset >> PAGE_SHIFT)); | |
875 | offset %= PAGE_SIZE; | |
876 | ||
877 | DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read"); | |
878 | ||
879 | if (PageHighMem(page)) { | |
880 | unsigned long flags; | |
881 | ||
882 | /* FIXME: use a bounce buffer */ | |
883 | local_irq_save(flags); | |
884 | buf = kmap_atomic(page, KM_IRQ0); | |
885 | ||
886 | /* do the actual data transfer */ | |
5682ed33 TH |
887 | ap->ops->sff_data_xfer(qc->dev, buf + offset, qc->sect_size, |
888 | do_write); | |
624d5c51 TH |
889 | |
890 | kunmap_atomic(buf, KM_IRQ0); | |
891 | local_irq_restore(flags); | |
892 | } else { | |
893 | buf = page_address(page); | |
5682ed33 TH |
894 | ap->ops->sff_data_xfer(qc->dev, buf + offset, qc->sect_size, |
895 | do_write); | |
624d5c51 TH |
896 | } |
897 | ||
3842e835 | 898 | if (!do_write && !PageSlab(page)) |
2d68b7fe CM |
899 | flush_dcache_page(page); |
900 | ||
624d5c51 TH |
901 | qc->curbytes += qc->sect_size; |
902 | qc->cursg_ofs += qc->sect_size; | |
903 | ||
904 | if (qc->cursg_ofs == qc->cursg->length) { | |
905 | qc->cursg = sg_next(qc->cursg); | |
906 | qc->cursg_ofs = 0; | |
907 | } | |
908 | } | |
909 | ||
910 | /** | |
911 | * ata_pio_sectors - Transfer one or many sectors. | |
912 | * @qc: Command on going | |
913 | * | |
914 | * Transfer one or many sectors of data from/to the | |
915 | * ATA device for the DRQ request. | |
916 | * | |
917 | * LOCKING: | |
918 | * Inherited from caller. | |
919 | */ | |
920 | static void ata_pio_sectors(struct ata_queued_cmd *qc) | |
921 | { | |
922 | if (is_multi_taskfile(&qc->tf)) { | |
923 | /* READ/WRITE MULTIPLE */ | |
924 | unsigned int nsect; | |
925 | ||
efcb3cf7 | 926 | WARN_ON_ONCE(qc->dev->multi_count == 0); |
624d5c51 TH |
927 | |
928 | nsect = min((qc->nbytes - qc->curbytes) / qc->sect_size, | |
929 | qc->dev->multi_count); | |
930 | while (nsect--) | |
931 | ata_pio_sector(qc); | |
932 | } else | |
933 | ata_pio_sector(qc); | |
934 | ||
a57c1bad | 935 | ata_sff_sync(qc->ap); /* flush */ |
624d5c51 TH |
936 | } |
937 | ||
938 | /** | |
939 | * atapi_send_cdb - Write CDB bytes to hardware | |
940 | * @ap: Port to which ATAPI device is attached. | |
941 | * @qc: Taskfile currently active | |
942 | * | |
943 | * When device has indicated its readiness to accept | |
944 | * a CDB, this function is called. Send the CDB. | |
945 | * | |
946 | * LOCKING: | |
947 | * caller. | |
948 | */ | |
949 | static void atapi_send_cdb(struct ata_port *ap, struct ata_queued_cmd *qc) | |
950 | { | |
951 | /* send SCSI cdb */ | |
952 | DPRINTK("send cdb\n"); | |
efcb3cf7 | 953 | WARN_ON_ONCE(qc->dev->cdb_len < 12); |
624d5c51 | 954 | |
5682ed33 | 955 | ap->ops->sff_data_xfer(qc->dev, qc->cdb, qc->dev->cdb_len, 1); |
a57c1bad AC |
956 | ata_sff_sync(ap); |
957 | /* FIXME: If the CDB is for DMA do we need to do the transition delay | |
958 | or is bmdma_start guaranteed to do it ? */ | |
624d5c51 TH |
959 | switch (qc->tf.protocol) { |
960 | case ATAPI_PROT_PIO: | |
961 | ap->hsm_task_state = HSM_ST; | |
962 | break; | |
963 | case ATAPI_PROT_NODATA: | |
964 | ap->hsm_task_state = HSM_ST_LAST; | |
965 | break; | |
966 | case ATAPI_PROT_DMA: | |
967 | ap->hsm_task_state = HSM_ST_LAST; | |
968 | /* initiate bmdma */ | |
969 | ap->ops->bmdma_start(qc); | |
970 | break; | |
971 | } | |
972 | } | |
973 | ||
974 | /** | |
975 | * __atapi_pio_bytes - Transfer data from/to the ATAPI device. | |
976 | * @qc: Command on going | |
977 | * @bytes: number of bytes | |
978 | * | |
979 | * Transfer Transfer data from/to the ATAPI device. | |
980 | * | |
981 | * LOCKING: | |
982 | * Inherited from caller. | |
983 | * | |
984 | */ | |
985 | static int __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes) | |
986 | { | |
987 | int rw = (qc->tf.flags & ATA_TFLAG_WRITE) ? WRITE : READ; | |
988 | struct ata_port *ap = qc->ap; | |
989 | struct ata_device *dev = qc->dev; | |
990 | struct ata_eh_info *ehi = &dev->link->eh_info; | |
991 | struct scatterlist *sg; | |
992 | struct page *page; | |
993 | unsigned char *buf; | |
994 | unsigned int offset, count, consumed; | |
995 | ||
996 | next_sg: | |
997 | sg = qc->cursg; | |
998 | if (unlikely(!sg)) { | |
999 | ata_ehi_push_desc(ehi, "unexpected or too much trailing data " | |
1000 | "buf=%u cur=%u bytes=%u", | |
1001 | qc->nbytes, qc->curbytes, bytes); | |
1002 | return -1; | |
1003 | } | |
1004 | ||
1005 | page = sg_page(sg); | |
1006 | offset = sg->offset + qc->cursg_ofs; | |
1007 | ||
1008 | /* get the current page and offset */ | |
1009 | page = nth_page(page, (offset >> PAGE_SHIFT)); | |
1010 | offset %= PAGE_SIZE; | |
1011 | ||
1012 | /* don't overrun current sg */ | |
1013 | count = min(sg->length - qc->cursg_ofs, bytes); | |
1014 | ||
1015 | /* don't cross page boundaries */ | |
1016 | count = min(count, (unsigned int)PAGE_SIZE - offset); | |
1017 | ||
1018 | DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read"); | |
1019 | ||
1020 | if (PageHighMem(page)) { | |
1021 | unsigned long flags; | |
1022 | ||
1023 | /* FIXME: use bounce buffer */ | |
1024 | local_irq_save(flags); | |
1025 | buf = kmap_atomic(page, KM_IRQ0); | |
1026 | ||
1027 | /* do the actual data transfer */ | |
0fe40ff8 AC |
1028 | consumed = ap->ops->sff_data_xfer(dev, buf + offset, |
1029 | count, rw); | |
624d5c51 TH |
1030 | |
1031 | kunmap_atomic(buf, KM_IRQ0); | |
1032 | local_irq_restore(flags); | |
1033 | } else { | |
1034 | buf = page_address(page); | |
0fe40ff8 AC |
1035 | consumed = ap->ops->sff_data_xfer(dev, buf + offset, |
1036 | count, rw); | |
624d5c51 TH |
1037 | } |
1038 | ||
1039 | bytes -= min(bytes, consumed); | |
1040 | qc->curbytes += count; | |
1041 | qc->cursg_ofs += count; | |
1042 | ||
1043 | if (qc->cursg_ofs == sg->length) { | |
1044 | qc->cursg = sg_next(qc->cursg); | |
1045 | qc->cursg_ofs = 0; | |
1046 | } | |
1047 | ||
a0f79f7a CB |
1048 | /* |
1049 | * There used to be a WARN_ON_ONCE(qc->cursg && count != consumed); | |
1050 | * Unfortunately __atapi_pio_bytes doesn't know enough to do the WARN | |
1051 | * check correctly as it doesn't know if it is the last request being | |
1052 | * made. Somebody should implement a proper sanity check. | |
1053 | */ | |
624d5c51 TH |
1054 | if (bytes) |
1055 | goto next_sg; | |
1056 | return 0; | |
1057 | } | |
1058 | ||
1059 | /** | |
1060 | * atapi_pio_bytes - Transfer data from/to the ATAPI device. | |
1061 | * @qc: Command on going | |
1062 | * | |
1063 | * Transfer Transfer data from/to the ATAPI device. | |
1064 | * | |
1065 | * LOCKING: | |
1066 | * Inherited from caller. | |
1067 | */ | |
1068 | static void atapi_pio_bytes(struct ata_queued_cmd *qc) | |
1069 | { | |
1070 | struct ata_port *ap = qc->ap; | |
1071 | struct ata_device *dev = qc->dev; | |
1072 | struct ata_eh_info *ehi = &dev->link->eh_info; | |
1073 | unsigned int ireason, bc_lo, bc_hi, bytes; | |
1074 | int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0; | |
1075 | ||
1076 | /* Abuse qc->result_tf for temp storage of intermediate TF | |
1077 | * here to save some kernel stack usage. | |
1078 | * For normal completion, qc->result_tf is not relevant. For | |
1079 | * error, qc->result_tf is later overwritten by ata_qc_complete(). | |
1080 | * So, the correctness of qc->result_tf is not affected. | |
1081 | */ | |
5682ed33 | 1082 | ap->ops->sff_tf_read(ap, &qc->result_tf); |
624d5c51 TH |
1083 | ireason = qc->result_tf.nsect; |
1084 | bc_lo = qc->result_tf.lbam; | |
1085 | bc_hi = qc->result_tf.lbah; | |
1086 | bytes = (bc_hi << 8) | bc_lo; | |
1087 | ||
1088 | /* shall be cleared to zero, indicating xfer of data */ | |
1089 | if (unlikely(ireason & (1 << 0))) | |
1090 | goto atapi_check; | |
1091 | ||
1092 | /* make sure transfer direction matches expected */ | |
1093 | i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0; | |
1094 | if (unlikely(do_write != i_write)) | |
1095 | goto atapi_check; | |
1096 | ||
1097 | if (unlikely(!bytes)) | |
1098 | goto atapi_check; | |
1099 | ||
1100 | VPRINTK("ata%u: xfering %d bytes\n", ap->print_id, bytes); | |
1101 | ||
1102 | if (unlikely(__atapi_pio_bytes(qc, bytes))) | |
1103 | goto err_out; | |
a57c1bad | 1104 | ata_sff_sync(ap); /* flush */ |
624d5c51 TH |
1105 | |
1106 | return; | |
1107 | ||
1108 | atapi_check: | |
1109 | ata_ehi_push_desc(ehi, "ATAPI check failed (ireason=0x%x bytes=%u)", | |
1110 | ireason, bytes); | |
1111 | err_out: | |
1112 | qc->err_mask |= AC_ERR_HSM; | |
1113 | ap->hsm_task_state = HSM_ST_ERR; | |
1114 | } | |
1115 | ||
1116 | /** | |
1117 | * ata_hsm_ok_in_wq - Check if the qc can be handled in the workqueue. | |
1118 | * @ap: the target ata_port | |
1119 | * @qc: qc on going | |
1120 | * | |
1121 | * RETURNS: | |
1122 | * 1 if ok in workqueue, 0 otherwise. | |
1123 | */ | |
0fe40ff8 AC |
1124 | static inline int ata_hsm_ok_in_wq(struct ata_port *ap, |
1125 | struct ata_queued_cmd *qc) | |
624d5c51 TH |
1126 | { |
1127 | if (qc->tf.flags & ATA_TFLAG_POLLING) | |
1128 | return 1; | |
1129 | ||
1130 | if (ap->hsm_task_state == HSM_ST_FIRST) { | |
1131 | if (qc->tf.protocol == ATA_PROT_PIO && | |
0fe40ff8 | 1132 | (qc->tf.flags & ATA_TFLAG_WRITE)) |
624d5c51 TH |
1133 | return 1; |
1134 | ||
1135 | if (ata_is_atapi(qc->tf.protocol) && | |
0fe40ff8 | 1136 | !(qc->dev->flags & ATA_DFLAG_CDB_INTR)) |
624d5c51 TH |
1137 | return 1; |
1138 | } | |
1139 | ||
1140 | return 0; | |
1141 | } | |
1142 | ||
1143 | /** | |
1144 | * ata_hsm_qc_complete - finish a qc running on standard HSM | |
1145 | * @qc: Command to complete | |
1146 | * @in_wq: 1 if called from workqueue, 0 otherwise | |
1147 | * | |
1148 | * Finish @qc which is running on standard HSM. | |
1149 | * | |
1150 | * LOCKING: | |
1151 | * If @in_wq is zero, spin_lock_irqsave(host lock). | |
1152 | * Otherwise, none on entry and grabs host lock. | |
1153 | */ | |
1154 | static void ata_hsm_qc_complete(struct ata_queued_cmd *qc, int in_wq) | |
1155 | { | |
1156 | struct ata_port *ap = qc->ap; | |
1157 | unsigned long flags; | |
1158 | ||
1159 | if (ap->ops->error_handler) { | |
1160 | if (in_wq) { | |
1161 | spin_lock_irqsave(ap->lock, flags); | |
1162 | ||
1163 | /* EH might have kicked in while host lock is | |
1164 | * released. | |
1165 | */ | |
1166 | qc = ata_qc_from_tag(ap, qc->tag); | |
1167 | if (qc) { | |
1168 | if (likely(!(qc->err_mask & AC_ERR_HSM))) { | |
e42a542b | 1169 | ata_sff_irq_on(ap); |
624d5c51 TH |
1170 | ata_qc_complete(qc); |
1171 | } else | |
1172 | ata_port_freeze(ap); | |
1173 | } | |
1174 | ||
1175 | spin_unlock_irqrestore(ap->lock, flags); | |
1176 | } else { | |
1177 | if (likely(!(qc->err_mask & AC_ERR_HSM))) | |
1178 | ata_qc_complete(qc); | |
1179 | else | |
1180 | ata_port_freeze(ap); | |
1181 | } | |
1182 | } else { | |
1183 | if (in_wq) { | |
1184 | spin_lock_irqsave(ap->lock, flags); | |
e42a542b | 1185 | ata_sff_irq_on(ap); |
624d5c51 TH |
1186 | ata_qc_complete(qc); |
1187 | spin_unlock_irqrestore(ap->lock, flags); | |
1188 | } else | |
1189 | ata_qc_complete(qc); | |
1190 | } | |
1191 | } | |
1192 | ||
1193 | /** | |
9363c382 | 1194 | * ata_sff_hsm_move - move the HSM to the next state. |
624d5c51 TH |
1195 | * @ap: the target ata_port |
1196 | * @qc: qc on going | |
1197 | * @status: current device status | |
1198 | * @in_wq: 1 if called from workqueue, 0 otherwise | |
1199 | * | |
1200 | * RETURNS: | |
1201 | * 1 when poll next status needed, 0 otherwise. | |
1202 | */ | |
9363c382 TH |
1203 | int ata_sff_hsm_move(struct ata_port *ap, struct ata_queued_cmd *qc, |
1204 | u8 status, int in_wq) | |
624d5c51 | 1205 | { |
a836d3e8 | 1206 | struct ata_eh_info *ehi = &ap->link.eh_info; |
624d5c51 TH |
1207 | unsigned long flags = 0; |
1208 | int poll_next; | |
1209 | ||
efcb3cf7 | 1210 | WARN_ON_ONCE((qc->flags & ATA_QCFLAG_ACTIVE) == 0); |
624d5c51 | 1211 | |
9363c382 | 1212 | /* Make sure ata_sff_qc_issue() does not throw things |
624d5c51 TH |
1213 | * like DMA polling into the workqueue. Notice that |
1214 | * in_wq is not equivalent to (qc->tf.flags & ATA_TFLAG_POLLING). | |
1215 | */ | |
efcb3cf7 | 1216 | WARN_ON_ONCE(in_wq != ata_hsm_ok_in_wq(ap, qc)); |
624d5c51 TH |
1217 | |
1218 | fsm_start: | |
1219 | DPRINTK("ata%u: protocol %d task_state %d (dev_stat 0x%X)\n", | |
1220 | ap->print_id, qc->tf.protocol, ap->hsm_task_state, status); | |
1221 | ||
1222 | switch (ap->hsm_task_state) { | |
1223 | case HSM_ST_FIRST: | |
1224 | /* Send first data block or PACKET CDB */ | |
1225 | ||
1226 | /* If polling, we will stay in the work queue after | |
1227 | * sending the data. Otherwise, interrupt handler | |
1228 | * takes over after sending the data. | |
1229 | */ | |
1230 | poll_next = (qc->tf.flags & ATA_TFLAG_POLLING); | |
1231 | ||
1232 | /* check device status */ | |
1233 | if (unlikely((status & ATA_DRQ) == 0)) { | |
1234 | /* handle BSY=0, DRQ=0 as error */ | |
1235 | if (likely(status & (ATA_ERR | ATA_DF))) | |
1236 | /* device stops HSM for abort/error */ | |
1237 | qc->err_mask |= AC_ERR_DEV; | |
a836d3e8 | 1238 | else { |
624d5c51 | 1239 | /* HSM violation. Let EH handle this */ |
a836d3e8 TH |
1240 | ata_ehi_push_desc(ehi, |
1241 | "ST_FIRST: !(DRQ|ERR|DF)"); | |
624d5c51 | 1242 | qc->err_mask |= AC_ERR_HSM; |
a836d3e8 | 1243 | } |
624d5c51 TH |
1244 | |
1245 | ap->hsm_task_state = HSM_ST_ERR; | |
1246 | goto fsm_start; | |
1247 | } | |
1248 | ||
1249 | /* Device should not ask for data transfer (DRQ=1) | |
1250 | * when it finds something wrong. | |
1251 | * We ignore DRQ here and stop the HSM by | |
1252 | * changing hsm_task_state to HSM_ST_ERR and | |
1253 | * let the EH abort the command or reset the device. | |
1254 | */ | |
1255 | if (unlikely(status & (ATA_ERR | ATA_DF))) { | |
1256 | /* Some ATAPI tape drives forget to clear the ERR bit | |
1257 | * when doing the next command (mostly request sense). | |
1258 | * We ignore ERR here to workaround and proceed sending | |
1259 | * the CDB. | |
1260 | */ | |
1261 | if (!(qc->dev->horkage & ATA_HORKAGE_STUCK_ERR)) { | |
a836d3e8 TH |
1262 | ata_ehi_push_desc(ehi, "ST_FIRST: " |
1263 | "DRQ=1 with device error, " | |
1264 | "dev_stat 0x%X", status); | |
624d5c51 TH |
1265 | qc->err_mask |= AC_ERR_HSM; |
1266 | ap->hsm_task_state = HSM_ST_ERR; | |
1267 | goto fsm_start; | |
1268 | } | |
1269 | } | |
1270 | ||
1271 | /* Send the CDB (atapi) or the first data block (ata pio out). | |
1272 | * During the state transition, interrupt handler shouldn't | |
1273 | * be invoked before the data transfer is complete and | |
1274 | * hsm_task_state is changed. Hence, the following locking. | |
1275 | */ | |
1276 | if (in_wq) | |
1277 | spin_lock_irqsave(ap->lock, flags); | |
1278 | ||
1279 | if (qc->tf.protocol == ATA_PROT_PIO) { | |
1280 | /* PIO data out protocol. | |
1281 | * send first data block. | |
1282 | */ | |
1283 | ||
1284 | /* ata_pio_sectors() might change the state | |
1285 | * to HSM_ST_LAST. so, the state is changed here | |
1286 | * before ata_pio_sectors(). | |
1287 | */ | |
1288 | ap->hsm_task_state = HSM_ST; | |
1289 | ata_pio_sectors(qc); | |
1290 | } else | |
1291 | /* send CDB */ | |
1292 | atapi_send_cdb(ap, qc); | |
1293 | ||
1294 | if (in_wq) | |
1295 | spin_unlock_irqrestore(ap->lock, flags); | |
1296 | ||
c429137a | 1297 | /* if polling, ata_sff_pio_task() handles the rest. |
624d5c51 TH |
1298 | * otherwise, interrupt handler takes over from here. |
1299 | */ | |
1300 | break; | |
1301 | ||
1302 | case HSM_ST: | |
1303 | /* complete command or read/write the data register */ | |
1304 | if (qc->tf.protocol == ATAPI_PROT_PIO) { | |
1305 | /* ATAPI PIO protocol */ | |
1306 | if ((status & ATA_DRQ) == 0) { | |
1307 | /* No more data to transfer or device error. | |
1308 | * Device error will be tagged in HSM_ST_LAST. | |
1309 | */ | |
1310 | ap->hsm_task_state = HSM_ST_LAST; | |
1311 | goto fsm_start; | |
1312 | } | |
1313 | ||
1314 | /* Device should not ask for data transfer (DRQ=1) | |
1315 | * when it finds something wrong. | |
1316 | * We ignore DRQ here and stop the HSM by | |
1317 | * changing hsm_task_state to HSM_ST_ERR and | |
1318 | * let the EH abort the command or reset the device. | |
1319 | */ | |
1320 | if (unlikely(status & (ATA_ERR | ATA_DF))) { | |
a836d3e8 TH |
1321 | ata_ehi_push_desc(ehi, "ST-ATAPI: " |
1322 | "DRQ=1 with device error, " | |
1323 | "dev_stat 0x%X", status); | |
624d5c51 TH |
1324 | qc->err_mask |= AC_ERR_HSM; |
1325 | ap->hsm_task_state = HSM_ST_ERR; | |
1326 | goto fsm_start; | |
1327 | } | |
1328 | ||
1329 | atapi_pio_bytes(qc); | |
1330 | ||
1331 | if (unlikely(ap->hsm_task_state == HSM_ST_ERR)) | |
1332 | /* bad ireason reported by device */ | |
1333 | goto fsm_start; | |
1334 | ||
1335 | } else { | |
1336 | /* ATA PIO protocol */ | |
1337 | if (unlikely((status & ATA_DRQ) == 0)) { | |
1338 | /* handle BSY=0, DRQ=0 as error */ | |
6a6b97d3 | 1339 | if (likely(status & (ATA_ERR | ATA_DF))) { |
624d5c51 TH |
1340 | /* device stops HSM for abort/error */ |
1341 | qc->err_mask |= AC_ERR_DEV; | |
6a6b97d3 TH |
1342 | |
1343 | /* If diagnostic failed and this is | |
1344 | * IDENTIFY, it's likely a phantom | |
1345 | * device. Mark hint. | |
1346 | */ | |
1347 | if (qc->dev->horkage & | |
1348 | ATA_HORKAGE_DIAGNOSTIC) | |
1349 | qc->err_mask |= | |
1350 | AC_ERR_NODEV_HINT; | |
1351 | } else { | |
624d5c51 TH |
1352 | /* HSM violation. Let EH handle this. |
1353 | * Phantom devices also trigger this | |
1354 | * condition. Mark hint. | |
1355 | */ | |
a836d3e8 | 1356 | ata_ehi_push_desc(ehi, "ST-ATA: " |
80ee6f54 | 1357 | "DRQ=0 without device error, " |
a836d3e8 | 1358 | "dev_stat 0x%X", status); |
624d5c51 TH |
1359 | qc->err_mask |= AC_ERR_HSM | |
1360 | AC_ERR_NODEV_HINT; | |
a836d3e8 | 1361 | } |
624d5c51 TH |
1362 | |
1363 | ap->hsm_task_state = HSM_ST_ERR; | |
1364 | goto fsm_start; | |
1365 | } | |
1366 | ||
1367 | /* For PIO reads, some devices may ask for | |
1368 | * data transfer (DRQ=1) alone with ERR=1. | |
1369 | * We respect DRQ here and transfer one | |
1370 | * block of junk data before changing the | |
1371 | * hsm_task_state to HSM_ST_ERR. | |
1372 | * | |
1373 | * For PIO writes, ERR=1 DRQ=1 doesn't make | |
1374 | * sense since the data block has been | |
1375 | * transferred to the device. | |
1376 | */ | |
1377 | if (unlikely(status & (ATA_ERR | ATA_DF))) { | |
1378 | /* data might be corrputed */ | |
1379 | qc->err_mask |= AC_ERR_DEV; | |
1380 | ||
1381 | if (!(qc->tf.flags & ATA_TFLAG_WRITE)) { | |
1382 | ata_pio_sectors(qc); | |
1383 | status = ata_wait_idle(ap); | |
1384 | } | |
1385 | ||
a836d3e8 TH |
1386 | if (status & (ATA_BUSY | ATA_DRQ)) { |
1387 | ata_ehi_push_desc(ehi, "ST-ATA: " | |
1388 | "BUSY|DRQ persists on ERR|DF, " | |
1389 | "dev_stat 0x%X", status); | |
624d5c51 | 1390 | qc->err_mask |= AC_ERR_HSM; |
a836d3e8 | 1391 | } |
624d5c51 | 1392 | |
b919930c TH |
1393 | /* There are oddball controllers with |
1394 | * status register stuck at 0x7f and | |
1395 | * lbal/m/h at zero which makes it | |
1396 | * pass all other presence detection | |
1397 | * mechanisms we have. Set NODEV_HINT | |
1398 | * for it. Kernel bz#7241. | |
1399 | */ | |
1400 | if (status == 0x7f) | |
1401 | qc->err_mask |= AC_ERR_NODEV_HINT; | |
1402 | ||
624d5c51 TH |
1403 | /* ata_pio_sectors() might change the |
1404 | * state to HSM_ST_LAST. so, the state | |
1405 | * is changed after ata_pio_sectors(). | |
1406 | */ | |
1407 | ap->hsm_task_state = HSM_ST_ERR; | |
1408 | goto fsm_start; | |
1409 | } | |
1410 | ||
1411 | ata_pio_sectors(qc); | |
1412 | ||
1413 | if (ap->hsm_task_state == HSM_ST_LAST && | |
1414 | (!(qc->tf.flags & ATA_TFLAG_WRITE))) { | |
1415 | /* all data read */ | |
1416 | status = ata_wait_idle(ap); | |
1417 | goto fsm_start; | |
1418 | } | |
1419 | } | |
1420 | ||
1421 | poll_next = 1; | |
1422 | break; | |
1423 | ||
1424 | case HSM_ST_LAST: | |
1425 | if (unlikely(!ata_ok(status))) { | |
1426 | qc->err_mask |= __ac_err_mask(status); | |
1427 | ap->hsm_task_state = HSM_ST_ERR; | |
1428 | goto fsm_start; | |
1429 | } | |
1430 | ||
1431 | /* no more data to transfer */ | |
1432 | DPRINTK("ata%u: dev %u command complete, drv_stat 0x%x\n", | |
1433 | ap->print_id, qc->dev->devno, status); | |
1434 | ||
efcb3cf7 | 1435 | WARN_ON_ONCE(qc->err_mask & (AC_ERR_DEV | AC_ERR_HSM)); |
624d5c51 TH |
1436 | |
1437 | ap->hsm_task_state = HSM_ST_IDLE; | |
1438 | ||
1439 | /* complete taskfile transaction */ | |
1440 | ata_hsm_qc_complete(qc, in_wq); | |
1441 | ||
1442 | poll_next = 0; | |
1443 | break; | |
1444 | ||
1445 | case HSM_ST_ERR: | |
624d5c51 TH |
1446 | ap->hsm_task_state = HSM_ST_IDLE; |
1447 | ||
1448 | /* complete taskfile transaction */ | |
1449 | ata_hsm_qc_complete(qc, in_wq); | |
1450 | ||
1451 | poll_next = 0; | |
1452 | break; | |
1453 | default: | |
1454 | poll_next = 0; | |
1455 | BUG(); | |
1456 | } | |
1457 | ||
1458 | return poll_next; | |
1459 | } | |
0fe40ff8 | 1460 | EXPORT_SYMBOL_GPL(ata_sff_hsm_move); |
624d5c51 | 1461 | |
c429137a TH |
1462 | void ata_sff_queue_pio_task(struct ata_port *ap, unsigned long delay) |
1463 | { | |
1464 | /* may fail if ata_sff_flush_pio_task() in progress */ | |
1465 | queue_delayed_work(ata_sff_wq, &ap->sff_pio_task, | |
1466 | msecs_to_jiffies(delay)); | |
1467 | } | |
1468 | EXPORT_SYMBOL_GPL(ata_sff_queue_pio_task); | |
1469 | ||
1470 | void ata_sff_flush_pio_task(struct ata_port *ap) | |
1471 | { | |
1472 | DPRINTK("ENTER\n"); | |
1473 | ||
1474 | cancel_rearming_delayed_work(&ap->sff_pio_task); | |
1475 | ap->hsm_task_state = HSM_ST_IDLE; | |
1476 | ||
1477 | if (ata_msg_ctl(ap)) | |
1478 | ata_port_printk(ap, KERN_DEBUG, "%s: EXIT\n", __func__); | |
1479 | } | |
1480 | ||
1481 | static void ata_sff_pio_task(struct work_struct *work) | |
624d5c51 TH |
1482 | { |
1483 | struct ata_port *ap = | |
c429137a TH |
1484 | container_of(work, struct ata_port, sff_pio_task.work); |
1485 | struct ata_queued_cmd *qc; | |
624d5c51 TH |
1486 | u8 status; |
1487 | int poll_next; | |
1488 | ||
c429137a TH |
1489 | /* qc can be NULL if timeout occurred */ |
1490 | qc = ata_qc_from_tag(ap, ap->link.active_tag); | |
1491 | if (!qc) | |
1492 | return; | |
1493 | ||
624d5c51 | 1494 | fsm_start: |
efcb3cf7 | 1495 | WARN_ON_ONCE(ap->hsm_task_state == HSM_ST_IDLE); |
624d5c51 TH |
1496 | |
1497 | /* | |
1498 | * This is purely heuristic. This is a fast path. | |
1499 | * Sometimes when we enter, BSY will be cleared in | |
1500 | * a chk-status or two. If not, the drive is probably seeking | |
1501 | * or something. Snooze for a couple msecs, then | |
1502 | * chk-status again. If still busy, queue delayed work. | |
1503 | */ | |
9363c382 | 1504 | status = ata_sff_busy_wait(ap, ATA_BUSY, 5); |
624d5c51 TH |
1505 | if (status & ATA_BUSY) { |
1506 | msleep(2); | |
9363c382 | 1507 | status = ata_sff_busy_wait(ap, ATA_BUSY, 10); |
624d5c51 | 1508 | if (status & ATA_BUSY) { |
c429137a | 1509 | ata_sff_queue_pio_task(ap, ATA_SHORT_PAUSE); |
624d5c51 TH |
1510 | return; |
1511 | } | |
1512 | } | |
1513 | ||
1514 | /* move the HSM */ | |
9363c382 | 1515 | poll_next = ata_sff_hsm_move(ap, qc, status, 1); |
624d5c51 TH |
1516 | |
1517 | /* another command or interrupt handler | |
1518 | * may be running at this point. | |
1519 | */ | |
1520 | if (poll_next) | |
1521 | goto fsm_start; | |
1522 | } | |
1523 | ||
1524 | /** | |
9363c382 | 1525 | * ata_sff_qc_issue - issue taskfile to device in proto-dependent manner |
624d5c51 TH |
1526 | * @qc: command to issue to device |
1527 | * | |
1528 | * Using various libata functions and hooks, this function | |
1529 | * starts an ATA command. ATA commands are grouped into | |
1530 | * classes called "protocols", and issuing each type of protocol | |
1531 | * is slightly different. | |
1532 | * | |
1533 | * May be used as the qc_issue() entry in ata_port_operations. | |
1534 | * | |
1535 | * LOCKING: | |
1536 | * spin_lock_irqsave(host lock) | |
1537 | * | |
1538 | * RETURNS: | |
1539 | * Zero on success, AC_ERR_* mask on failure | |
1540 | */ | |
9363c382 | 1541 | unsigned int ata_sff_qc_issue(struct ata_queued_cmd *qc) |
624d5c51 TH |
1542 | { |
1543 | struct ata_port *ap = qc->ap; | |
1544 | ||
1545 | /* Use polling pio if the LLD doesn't handle | |
1546 | * interrupt driven pio and atapi CDB interrupt. | |
1547 | */ | |
1548 | if (ap->flags & ATA_FLAG_PIO_POLLING) { | |
1549 | switch (qc->tf.protocol) { | |
1550 | case ATA_PROT_PIO: | |
1551 | case ATA_PROT_NODATA: | |
1552 | case ATAPI_PROT_PIO: | |
1553 | case ATAPI_PROT_NODATA: | |
1554 | qc->tf.flags |= ATA_TFLAG_POLLING; | |
1555 | break; | |
1556 | case ATAPI_PROT_DMA: | |
1557 | if (qc->dev->flags & ATA_DFLAG_CDB_INTR) | |
1558 | /* see ata_dma_blacklisted() */ | |
1559 | BUG(); | |
1560 | break; | |
1561 | default: | |
1562 | break; | |
1563 | } | |
1564 | } | |
1565 | ||
1566 | /* select the device */ | |
1567 | ata_dev_select(ap, qc->dev->devno, 1, 0); | |
1568 | ||
1569 | /* start the command */ | |
1570 | switch (qc->tf.protocol) { | |
1571 | case ATA_PROT_NODATA: | |
1572 | if (qc->tf.flags & ATA_TFLAG_POLLING) | |
1573 | ata_qc_set_polling(qc); | |
1574 | ||
1575 | ata_tf_to_host(ap, &qc->tf); | |
1576 | ap->hsm_task_state = HSM_ST_LAST; | |
1577 | ||
1578 | if (qc->tf.flags & ATA_TFLAG_POLLING) | |
c429137a | 1579 | ata_sff_queue_pio_task(ap, 0); |
624d5c51 TH |
1580 | |
1581 | break; | |
1582 | ||
1583 | case ATA_PROT_DMA: | |
efcb3cf7 | 1584 | WARN_ON_ONCE(qc->tf.flags & ATA_TFLAG_POLLING); |
624d5c51 | 1585 | |
5682ed33 | 1586 | ap->ops->sff_tf_load(ap, &qc->tf); /* load tf registers */ |
624d5c51 TH |
1587 | ap->ops->bmdma_setup(qc); /* set up bmdma */ |
1588 | ap->ops->bmdma_start(qc); /* initiate bmdma */ | |
1589 | ap->hsm_task_state = HSM_ST_LAST; | |
1590 | break; | |
1591 | ||
1592 | case ATA_PROT_PIO: | |
1593 | if (qc->tf.flags & ATA_TFLAG_POLLING) | |
1594 | ata_qc_set_polling(qc); | |
1595 | ||
1596 | ata_tf_to_host(ap, &qc->tf); | |
1597 | ||
1598 | if (qc->tf.flags & ATA_TFLAG_WRITE) { | |
1599 | /* PIO data out protocol */ | |
1600 | ap->hsm_task_state = HSM_ST_FIRST; | |
c429137a | 1601 | ata_sff_queue_pio_task(ap, 0); |
624d5c51 | 1602 | |
c429137a TH |
1603 | /* always send first data block using the |
1604 | * ata_sff_pio_task() codepath. | |
624d5c51 TH |
1605 | */ |
1606 | } else { | |
1607 | /* PIO data in protocol */ | |
1608 | ap->hsm_task_state = HSM_ST; | |
1609 | ||
1610 | if (qc->tf.flags & ATA_TFLAG_POLLING) | |
c429137a | 1611 | ata_sff_queue_pio_task(ap, 0); |
624d5c51 | 1612 | |
c429137a TH |
1613 | /* if polling, ata_sff_pio_task() handles the |
1614 | * rest. otherwise, interrupt handler takes | |
1615 | * over from here. | |
624d5c51 TH |
1616 | */ |
1617 | } | |
1618 | ||
1619 | break; | |
1620 | ||
1621 | case ATAPI_PROT_PIO: | |
1622 | case ATAPI_PROT_NODATA: | |
1623 | if (qc->tf.flags & ATA_TFLAG_POLLING) | |
1624 | ata_qc_set_polling(qc); | |
1625 | ||
1626 | ata_tf_to_host(ap, &qc->tf); | |
1627 | ||
1628 | ap->hsm_task_state = HSM_ST_FIRST; | |
1629 | ||
1630 | /* send cdb by polling if no cdb interrupt */ | |
1631 | if ((!(qc->dev->flags & ATA_DFLAG_CDB_INTR)) || | |
1632 | (qc->tf.flags & ATA_TFLAG_POLLING)) | |
c429137a | 1633 | ata_sff_queue_pio_task(ap, 0); |
624d5c51 TH |
1634 | break; |
1635 | ||
1636 | case ATAPI_PROT_DMA: | |
efcb3cf7 | 1637 | WARN_ON_ONCE(qc->tf.flags & ATA_TFLAG_POLLING); |
624d5c51 | 1638 | |
5682ed33 | 1639 | ap->ops->sff_tf_load(ap, &qc->tf); /* load tf registers */ |
624d5c51 TH |
1640 | ap->ops->bmdma_setup(qc); /* set up bmdma */ |
1641 | ap->hsm_task_state = HSM_ST_FIRST; | |
1642 | ||
1643 | /* send cdb by polling if no cdb interrupt */ | |
1644 | if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR)) | |
c429137a | 1645 | ata_sff_queue_pio_task(ap, 0); |
624d5c51 TH |
1646 | break; |
1647 | ||
1648 | default: | |
efcb3cf7 | 1649 | WARN_ON_ONCE(1); |
624d5c51 TH |
1650 | return AC_ERR_SYSTEM; |
1651 | } | |
1652 | ||
1653 | return 0; | |
1654 | } | |
0fe40ff8 | 1655 | EXPORT_SYMBOL_GPL(ata_sff_qc_issue); |
624d5c51 | 1656 | |
22183bf5 TH |
1657 | /** |
1658 | * ata_sff_qc_fill_rtf - fill result TF using ->sff_tf_read | |
1659 | * @qc: qc to fill result TF for | |
1660 | * | |
1661 | * @qc is finished and result TF needs to be filled. Fill it | |
1662 | * using ->sff_tf_read. | |
1663 | * | |
1664 | * LOCKING: | |
1665 | * spin_lock_irqsave(host lock) | |
1666 | * | |
1667 | * RETURNS: | |
1668 | * true indicating that result TF is successfully filled. | |
1669 | */ | |
1670 | bool ata_sff_qc_fill_rtf(struct ata_queued_cmd *qc) | |
1671 | { | |
1672 | qc->ap->ops->sff_tf_read(qc->ap, &qc->result_tf); | |
1673 | return true; | |
1674 | } | |
0fe40ff8 | 1675 | EXPORT_SYMBOL_GPL(ata_sff_qc_fill_rtf); |
22183bf5 | 1676 | |
624d5c51 | 1677 | /** |
9363c382 | 1678 | * ata_sff_host_intr - Handle host interrupt for given (port, task) |
624d5c51 TH |
1679 | * @ap: Port on which interrupt arrived (possibly...) |
1680 | * @qc: Taskfile currently active in engine | |
1681 | * | |
1682 | * Handle host interrupt for given queued command. Currently, | |
1683 | * only DMA interrupts are handled. All other commands are | |
1684 | * handled via polling with interrupts disabled (nIEN bit). | |
1685 | * | |
1686 | * LOCKING: | |
1687 | * spin_lock_irqsave(host lock) | |
1688 | * | |
1689 | * RETURNS: | |
1690 | * One if interrupt was handled, zero if not (shared irq). | |
1691 | */ | |
c96f1732 | 1692 | unsigned int ata_sff_host_intr(struct ata_port *ap, |
9363c382 | 1693 | struct ata_queued_cmd *qc) |
624d5c51 TH |
1694 | { |
1695 | struct ata_eh_info *ehi = &ap->link.eh_info; | |
1696 | u8 status, host_stat = 0; | |
332ac7ff | 1697 | bool bmdma_stopped = false; |
624d5c51 TH |
1698 | |
1699 | VPRINTK("ata%u: protocol %d task_state %d\n", | |
1700 | ap->print_id, qc->tf.protocol, ap->hsm_task_state); | |
1701 | ||
1702 | /* Check whether we are expecting interrupt in this state */ | |
1703 | switch (ap->hsm_task_state) { | |
1704 | case HSM_ST_FIRST: | |
1705 | /* Some pre-ATAPI-4 devices assert INTRQ | |
1706 | * at this state when ready to receive CDB. | |
1707 | */ | |
1708 | ||
1709 | /* Check the ATA_DFLAG_CDB_INTR flag is enough here. | |
1710 | * The flag was turned on only for atapi devices. No | |
1711 | * need to check ata_is_atapi(qc->tf.protocol) again. | |
1712 | */ | |
1713 | if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR)) | |
1714 | goto idle_irq; | |
1715 | break; | |
1716 | case HSM_ST_LAST: | |
1717 | if (qc->tf.protocol == ATA_PROT_DMA || | |
1718 | qc->tf.protocol == ATAPI_PROT_DMA) { | |
1719 | /* check status of DMA engine */ | |
1720 | host_stat = ap->ops->bmdma_status(ap); | |
1721 | VPRINTK("ata%u: host_stat 0x%X\n", | |
1722 | ap->print_id, host_stat); | |
1723 | ||
1724 | /* if it's not our irq... */ | |
1725 | if (!(host_stat & ATA_DMA_INTR)) | |
1726 | goto idle_irq; | |
1727 | ||
1728 | /* before we do anything else, clear DMA-Start bit */ | |
1729 | ap->ops->bmdma_stop(qc); | |
332ac7ff | 1730 | bmdma_stopped = true; |
624d5c51 TH |
1731 | |
1732 | if (unlikely(host_stat & ATA_DMA_ERR)) { | |
1733 | /* error when transfering data to/from memory */ | |
1734 | qc->err_mask |= AC_ERR_HOST_BUS; | |
1735 | ap->hsm_task_state = HSM_ST_ERR; | |
1736 | } | |
1737 | } | |
1738 | break; | |
1739 | case HSM_ST: | |
1740 | break; | |
1741 | default: | |
1742 | goto idle_irq; | |
1743 | } | |
1744 | ||
624d5c51 | 1745 | |
a57c1bad AC |
1746 | /* check main status, clearing INTRQ if needed */ |
1747 | status = ata_sff_irq_status(ap); | |
332ac7ff TH |
1748 | if (status & ATA_BUSY) { |
1749 | if (bmdma_stopped) { | |
1750 | /* BMDMA engine is already stopped, we're screwed */ | |
1751 | qc->err_mask |= AC_ERR_HSM; | |
1752 | ap->hsm_task_state = HSM_ST_ERR; | |
1753 | } else | |
1754 | goto idle_irq; | |
1755 | } | |
624d5c51 | 1756 | |
9f2f7210 | 1757 | /* clear irq events */ |
5682ed33 | 1758 | ap->ops->sff_irq_clear(ap); |
624d5c51 | 1759 | |
9363c382 | 1760 | ata_sff_hsm_move(ap, qc, status, 0); |
624d5c51 TH |
1761 | |
1762 | if (unlikely(qc->err_mask) && (qc->tf.protocol == ATA_PROT_DMA || | |
1763 | qc->tf.protocol == ATAPI_PROT_DMA)) | |
1764 | ata_ehi_push_desc(ehi, "BMDMA stat 0x%x", host_stat); | |
1765 | ||
1766 | return 1; /* irq handled */ | |
1767 | ||
1768 | idle_irq: | |
1769 | ap->stats.idle_irq++; | |
1770 | ||
1771 | #ifdef ATA_IRQ_TRAP | |
1772 | if ((ap->stats.idle_irq % 1000) == 0) { | |
5682ed33 TH |
1773 | ap->ops->sff_check_status(ap); |
1774 | ap->ops->sff_irq_clear(ap); | |
624d5c51 TH |
1775 | ata_port_printk(ap, KERN_WARNING, "irq trap\n"); |
1776 | return 1; | |
1777 | } | |
1778 | #endif | |
1779 | return 0; /* irq not handled */ | |
1780 | } | |
0fe40ff8 | 1781 | EXPORT_SYMBOL_GPL(ata_sff_host_intr); |
624d5c51 TH |
1782 | |
1783 | /** | |
9363c382 | 1784 | * ata_sff_interrupt - Default ATA host interrupt handler |
624d5c51 TH |
1785 | * @irq: irq line (unused) |
1786 | * @dev_instance: pointer to our ata_host information structure | |
1787 | * | |
1788 | * Default interrupt handler for PCI IDE devices. Calls | |
9363c382 | 1789 | * ata_sff_host_intr() for each port that is not disabled. |
624d5c51 TH |
1790 | * |
1791 | * LOCKING: | |
1792 | * Obtains host lock during operation. | |
1793 | * | |
1794 | * RETURNS: | |
1795 | * IRQ_NONE or IRQ_HANDLED. | |
1796 | */ | |
9363c382 | 1797 | irqreturn_t ata_sff_interrupt(int irq, void *dev_instance) |
624d5c51 TH |
1798 | { |
1799 | struct ata_host *host = dev_instance; | |
332ac7ff | 1800 | bool retried = false; |
624d5c51 | 1801 | unsigned int i; |
332ac7ff | 1802 | unsigned int handled, idle, polling; |
624d5c51 TH |
1803 | unsigned long flags; |
1804 | ||
1805 | /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */ | |
1806 | spin_lock_irqsave(&host->lock, flags); | |
1807 | ||
332ac7ff TH |
1808 | retry: |
1809 | handled = idle = polling = 0; | |
624d5c51 | 1810 | for (i = 0; i < host->n_ports; i++) { |
d88ec2e5 TH |
1811 | struct ata_port *ap = host->ports[i]; |
1812 | struct ata_queued_cmd *qc; | |
624d5c51 | 1813 | |
d88ec2e5 | 1814 | qc = ata_qc_from_tag(ap, ap->link.active_tag); |
27943620 TH |
1815 | if (qc) { |
1816 | if (!(qc->tf.flags & ATA_TFLAG_POLLING)) | |
1817 | handled |= ata_sff_host_intr(ap, qc); | |
1818 | else | |
1819 | polling |= 1 << i; | |
332ac7ff TH |
1820 | } else |
1821 | idle |= 1 << i; | |
27943620 TH |
1822 | } |
1823 | ||
1824 | /* | |
1825 | * If no port was expecting IRQ but the controller is actually | |
1826 | * asserting IRQ line, nobody cared will ensue. Check IRQ | |
1827 | * pending status if available and clear spurious IRQ. | |
1828 | */ | |
332ac7ff TH |
1829 | if (!handled && !retried) { |
1830 | bool retry = false; | |
1831 | ||
27943620 TH |
1832 | for (i = 0; i < host->n_ports; i++) { |
1833 | struct ata_port *ap = host->ports[i]; | |
1834 | ||
1835 | if (polling & (1 << i)) | |
1836 | continue; | |
1837 | ||
1838 | if (!ap->ops->sff_irq_check || | |
1839 | !ap->ops->sff_irq_check(ap)) | |
1840 | continue; | |
1841 | ||
332ac7ff TH |
1842 | if (idle & (1 << i)) { |
1843 | ap->ops->sff_check_status(ap); | |
1844 | ap->ops->sff_irq_clear(ap); | |
1845 | } else { | |
1846 | /* clear INTRQ and check if BUSY cleared */ | |
1847 | if (!(ap->ops->sff_check_status(ap) & ATA_BUSY)) | |
1848 | retry |= true; | |
1849 | /* | |
1850 | * With command in flight, we can't do | |
1851 | * sff_irq_clear() w/o racing with completion. | |
1852 | */ | |
1853 | } | |
1854 | } | |
1855 | ||
1856 | if (retry) { | |
1857 | retried = true; | |
1858 | goto retry; | |
27943620 | 1859 | } |
624d5c51 TH |
1860 | } |
1861 | ||
1862 | spin_unlock_irqrestore(&host->lock, flags); | |
1863 | ||
1864 | return IRQ_RETVAL(handled); | |
1865 | } | |
0fe40ff8 | 1866 | EXPORT_SYMBOL_GPL(ata_sff_interrupt); |
624d5c51 | 1867 | |
c96f1732 AC |
1868 | /** |
1869 | * ata_sff_lost_interrupt - Check for an apparent lost interrupt | |
1870 | * @ap: port that appears to have timed out | |
1871 | * | |
1872 | * Called from the libata error handlers when the core code suspects | |
1873 | * an interrupt has been lost. If it has complete anything we can and | |
1874 | * then return. Interface must support altstatus for this faster | |
1875 | * recovery to occur. | |
1876 | * | |
1877 | * Locking: | |
1878 | * Caller holds host lock | |
1879 | */ | |
1880 | ||
1881 | void ata_sff_lost_interrupt(struct ata_port *ap) | |
1882 | { | |
1883 | u8 status; | |
1884 | struct ata_queued_cmd *qc; | |
1885 | ||
1886 | /* Only one outstanding command per SFF channel */ | |
1887 | qc = ata_qc_from_tag(ap, ap->link.active_tag); | |
3e4ec344 TH |
1888 | /* We cannot lose an interrupt on a non-existent or polled command */ |
1889 | if (!qc || qc->tf.flags & ATA_TFLAG_POLLING) | |
c96f1732 AC |
1890 | return; |
1891 | /* See if the controller thinks it is still busy - if so the command | |
1892 | isn't a lost IRQ but is still in progress */ | |
1893 | status = ata_sff_altstatus(ap); | |
1894 | if (status & ATA_BUSY) | |
1895 | return; | |
1896 | ||
1897 | /* There was a command running, we are no longer busy and we have | |
1898 | no interrupt. */ | |
1899 | ata_port_printk(ap, KERN_WARNING, "lost interrupt (Status 0x%x)\n", | |
1900 | status); | |
1901 | /* Run the host interrupt logic as if the interrupt had not been | |
1902 | lost */ | |
1903 | ata_sff_host_intr(ap, qc); | |
1904 | } | |
1905 | EXPORT_SYMBOL_GPL(ata_sff_lost_interrupt); | |
1906 | ||
624d5c51 | 1907 | /** |
9363c382 | 1908 | * ata_sff_freeze - Freeze SFF controller port |
624d5c51 TH |
1909 | * @ap: port to freeze |
1910 | * | |
9f2f7210 | 1911 | * Freeze SFF controller port. |
624d5c51 TH |
1912 | * |
1913 | * LOCKING: | |
1914 | * Inherited from caller. | |
1915 | */ | |
9363c382 | 1916 | void ata_sff_freeze(struct ata_port *ap) |
624d5c51 | 1917 | { |
624d5c51 TH |
1918 | ap->ctl |= ATA_NIEN; |
1919 | ap->last_ctl = ap->ctl; | |
1920 | ||
41dec29b SS |
1921 | if (ap->ops->sff_set_devctl || ap->ioaddr.ctl_addr) |
1922 | ata_sff_set_devctl(ap, ap->ctl); | |
624d5c51 TH |
1923 | |
1924 | /* Under certain circumstances, some controllers raise IRQ on | |
1925 | * ATA_NIEN manipulation. Also, many controllers fail to mask | |
1926 | * previously pending IRQ on ATA_NIEN assertion. Clear it. | |
1927 | */ | |
5682ed33 | 1928 | ap->ops->sff_check_status(ap); |
624d5c51 | 1929 | |
5682ed33 | 1930 | ap->ops->sff_irq_clear(ap); |
624d5c51 | 1931 | } |
0fe40ff8 | 1932 | EXPORT_SYMBOL_GPL(ata_sff_freeze); |
624d5c51 TH |
1933 | |
1934 | /** | |
9363c382 | 1935 | * ata_sff_thaw - Thaw SFF controller port |
624d5c51 TH |
1936 | * @ap: port to thaw |
1937 | * | |
9363c382 | 1938 | * Thaw SFF controller port. |
624d5c51 TH |
1939 | * |
1940 | * LOCKING: | |
1941 | * Inherited from caller. | |
1942 | */ | |
9363c382 | 1943 | void ata_sff_thaw(struct ata_port *ap) |
272f7884 | 1944 | { |
624d5c51 | 1945 | /* clear & re-enable interrupts */ |
5682ed33 TH |
1946 | ap->ops->sff_check_status(ap); |
1947 | ap->ops->sff_irq_clear(ap); | |
e42a542b | 1948 | ata_sff_irq_on(ap); |
272f7884 | 1949 | } |
0fe40ff8 | 1950 | EXPORT_SYMBOL_GPL(ata_sff_thaw); |
272f7884 | 1951 | |
0aa1113d TH |
1952 | /** |
1953 | * ata_sff_prereset - prepare SFF link for reset | |
1954 | * @link: SFF link to be reset | |
1955 | * @deadline: deadline jiffies for the operation | |
1956 | * | |
1957 | * SFF link @link is about to be reset. Initialize it. It first | |
1958 | * calls ata_std_prereset() and wait for !BSY if the port is | |
1959 | * being softreset. | |
1960 | * | |
1961 | * LOCKING: | |
1962 | * Kernel thread context (may sleep) | |
1963 | * | |
1964 | * RETURNS: | |
1965 | * 0 on success, -errno otherwise. | |
1966 | */ | |
1967 | int ata_sff_prereset(struct ata_link *link, unsigned long deadline) | |
1968 | { | |
0aa1113d TH |
1969 | struct ata_eh_context *ehc = &link->eh_context; |
1970 | int rc; | |
1971 | ||
1972 | rc = ata_std_prereset(link, deadline); | |
1973 | if (rc) | |
1974 | return rc; | |
1975 | ||
1976 | /* if we're about to do hardreset, nothing more to do */ | |
1977 | if (ehc->i.action & ATA_EH_HARDRESET) | |
1978 | return 0; | |
1979 | ||
1980 | /* wait for !BSY if we don't know that no device is attached */ | |
1981 | if (!ata_link_offline(link)) { | |
705e76be | 1982 | rc = ata_sff_wait_ready(link, deadline); |
0aa1113d TH |
1983 | if (rc && rc != -ENODEV) { |
1984 | ata_link_printk(link, KERN_WARNING, "device not ready " | |
1985 | "(errno=%d), forcing hardreset\n", rc); | |
1986 | ehc->i.action |= ATA_EH_HARDRESET; | |
1987 | } | |
1988 | } | |
1989 | ||
1990 | return 0; | |
1991 | } | |
0fe40ff8 | 1992 | EXPORT_SYMBOL_GPL(ata_sff_prereset); |
0aa1113d | 1993 | |
90088bb4 | 1994 | /** |
624d5c51 TH |
1995 | * ata_devchk - PATA device presence detection |
1996 | * @ap: ATA channel to examine | |
1997 | * @device: Device to examine (starting at zero) | |
90088bb4 | 1998 | * |
624d5c51 TH |
1999 | * This technique was originally described in |
2000 | * Hale Landis's ATADRVR (www.ata-atapi.com), and | |
2001 | * later found its way into the ATA/ATAPI spec. | |
2002 | * | |
2003 | * Write a pattern to the ATA shadow registers, | |
2004 | * and if a device is present, it will respond by | |
2005 | * correctly storing and echoing back the | |
2006 | * ATA shadow register contents. | |
90088bb4 TH |
2007 | * |
2008 | * LOCKING: | |
624d5c51 | 2009 | * caller. |
90088bb4 | 2010 | */ |
624d5c51 | 2011 | static unsigned int ata_devchk(struct ata_port *ap, unsigned int device) |
90088bb4 TH |
2012 | { |
2013 | struct ata_ioports *ioaddr = &ap->ioaddr; | |
624d5c51 | 2014 | u8 nsect, lbal; |
90088bb4 | 2015 | |
5682ed33 | 2016 | ap->ops->sff_dev_select(ap, device); |
90088bb4 | 2017 | |
624d5c51 TH |
2018 | iowrite8(0x55, ioaddr->nsect_addr); |
2019 | iowrite8(0xaa, ioaddr->lbal_addr); | |
90088bb4 | 2020 | |
624d5c51 TH |
2021 | iowrite8(0xaa, ioaddr->nsect_addr); |
2022 | iowrite8(0x55, ioaddr->lbal_addr); | |
90088bb4 | 2023 | |
624d5c51 TH |
2024 | iowrite8(0x55, ioaddr->nsect_addr); |
2025 | iowrite8(0xaa, ioaddr->lbal_addr); | |
2026 | ||
2027 | nsect = ioread8(ioaddr->nsect_addr); | |
2028 | lbal = ioread8(ioaddr->lbal_addr); | |
2029 | ||
2030 | if ((nsect == 0x55) && (lbal == 0xaa)) | |
2031 | return 1; /* we found a device */ | |
2032 | ||
2033 | return 0; /* nothing found */ | |
90088bb4 TH |
2034 | } |
2035 | ||
272f7884 | 2036 | /** |
9363c382 | 2037 | * ata_sff_dev_classify - Parse returned ATA device signature |
624d5c51 TH |
2038 | * @dev: ATA device to classify (starting at zero) |
2039 | * @present: device seems present | |
2040 | * @r_err: Value of error register on completion | |
272f7884 | 2041 | * |
624d5c51 TH |
2042 | * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs, |
2043 | * an ATA/ATAPI-defined set of values is placed in the ATA | |
2044 | * shadow registers, indicating the results of device detection | |
2045 | * and diagnostics. | |
272f7884 | 2046 | * |
624d5c51 TH |
2047 | * Select the ATA device, and read the values from the ATA shadow |
2048 | * registers. Then parse according to the Error register value, | |
2049 | * and the spec-defined values examined by ata_dev_classify(). | |
272f7884 TH |
2050 | * |
2051 | * LOCKING: | |
624d5c51 TH |
2052 | * caller. |
2053 | * | |
2054 | * RETURNS: | |
2055 | * Device type - %ATA_DEV_ATA, %ATA_DEV_ATAPI or %ATA_DEV_NONE. | |
272f7884 | 2056 | */ |
9363c382 | 2057 | unsigned int ata_sff_dev_classify(struct ata_device *dev, int present, |
624d5c51 | 2058 | u8 *r_err) |
272f7884 | 2059 | { |
624d5c51 TH |
2060 | struct ata_port *ap = dev->link->ap; |
2061 | struct ata_taskfile tf; | |
2062 | unsigned int class; | |
2063 | u8 err; | |
2064 | ||
5682ed33 | 2065 | ap->ops->sff_dev_select(ap, dev->devno); |
624d5c51 TH |
2066 | |
2067 | memset(&tf, 0, sizeof(tf)); | |
2068 | ||
5682ed33 | 2069 | ap->ops->sff_tf_read(ap, &tf); |
624d5c51 TH |
2070 | err = tf.feature; |
2071 | if (r_err) | |
2072 | *r_err = err; | |
2073 | ||
2074 | /* see if device passed diags: continue and warn later */ | |
2075 | if (err == 0) | |
2076 | /* diagnostic fail : do nothing _YET_ */ | |
2077 | dev->horkage |= ATA_HORKAGE_DIAGNOSTIC; | |
2078 | else if (err == 1) | |
2079 | /* do nothing */ ; | |
2080 | else if ((dev->devno == 0) && (err == 0x81)) | |
2081 | /* do nothing */ ; | |
2082 | else | |
2083 | return ATA_DEV_NONE; | |
272f7884 | 2084 | |
624d5c51 TH |
2085 | /* determine if device is ATA or ATAPI */ |
2086 | class = ata_dev_classify(&tf); | |
272f7884 | 2087 | |
624d5c51 TH |
2088 | if (class == ATA_DEV_UNKNOWN) { |
2089 | /* If the device failed diagnostic, it's likely to | |
2090 | * have reported incorrect device signature too. | |
2091 | * Assume ATA device if the device seems present but | |
2092 | * device signature is invalid with diagnostic | |
2093 | * failure. | |
2094 | */ | |
2095 | if (present && (dev->horkage & ATA_HORKAGE_DIAGNOSTIC)) | |
2096 | class = ATA_DEV_ATA; | |
2097 | else | |
2098 | class = ATA_DEV_NONE; | |
5682ed33 TH |
2099 | } else if ((class == ATA_DEV_ATA) && |
2100 | (ap->ops->sff_check_status(ap) == 0)) | |
624d5c51 TH |
2101 | class = ATA_DEV_NONE; |
2102 | ||
2103 | return class; | |
272f7884 | 2104 | } |
0fe40ff8 | 2105 | EXPORT_SYMBOL_GPL(ata_sff_dev_classify); |
272f7884 | 2106 | |
705e76be TH |
2107 | /** |
2108 | * ata_sff_wait_after_reset - wait for devices to become ready after reset | |
2109 | * @link: SFF link which is just reset | |
2110 | * @devmask: mask of present devices | |
2111 | * @deadline: deadline jiffies for the operation | |
2112 | * | |
2113 | * Wait devices attached to SFF @link to become ready after | |
2114 | * reset. It contains preceding 150ms wait to avoid accessing TF | |
2115 | * status register too early. | |
2116 | * | |
2117 | * LOCKING: | |
2118 | * Kernel thread context (may sleep). | |
2119 | * | |
2120 | * RETURNS: | |
2121 | * 0 on success, -ENODEV if some or all of devices in @devmask | |
2122 | * don't seem to exist. -errno on other errors. | |
2123 | */ | |
2124 | int ata_sff_wait_after_reset(struct ata_link *link, unsigned int devmask, | |
2125 | unsigned long deadline) | |
1fdffbce | 2126 | { |
705e76be | 2127 | struct ata_port *ap = link->ap; |
1fdffbce | 2128 | struct ata_ioports *ioaddr = &ap->ioaddr; |
624d5c51 TH |
2129 | unsigned int dev0 = devmask & (1 << 0); |
2130 | unsigned int dev1 = devmask & (1 << 1); | |
2131 | int rc, ret = 0; | |
1fdffbce | 2132 | |
341c2c95 | 2133 | msleep(ATA_WAIT_AFTER_RESET); |
705e76be TH |
2134 | |
2135 | /* always check readiness of the master device */ | |
2136 | rc = ata_sff_wait_ready(link, deadline); | |
2137 | /* -ENODEV means the odd clown forgot the D7 pulldown resistor | |
2138 | * and TF status is 0xff, bail out on it too. | |
624d5c51 | 2139 | */ |
705e76be TH |
2140 | if (rc) |
2141 | return rc; | |
1fdffbce | 2142 | |
624d5c51 TH |
2143 | /* if device 1 was found in ata_devchk, wait for register |
2144 | * access briefly, then wait for BSY to clear. | |
2145 | */ | |
2146 | if (dev1) { | |
2147 | int i; | |
1fdffbce | 2148 | |
5682ed33 | 2149 | ap->ops->sff_dev_select(ap, 1); |
1fdffbce | 2150 | |
624d5c51 TH |
2151 | /* Wait for register access. Some ATAPI devices fail |
2152 | * to set nsect/lbal after reset, so don't waste too | |
2153 | * much time on it. We're gonna wait for !BSY anyway. | |
2154 | */ | |
2155 | for (i = 0; i < 2; i++) { | |
2156 | u8 nsect, lbal; | |
2157 | ||
2158 | nsect = ioread8(ioaddr->nsect_addr); | |
2159 | lbal = ioread8(ioaddr->lbal_addr); | |
2160 | if ((nsect == 1) && (lbal == 1)) | |
2161 | break; | |
2162 | msleep(50); /* give drive a breather */ | |
2163 | } | |
2164 | ||
705e76be | 2165 | rc = ata_sff_wait_ready(link, deadline); |
624d5c51 TH |
2166 | if (rc) { |
2167 | if (rc != -ENODEV) | |
2168 | return rc; | |
2169 | ret = rc; | |
2170 | } | |
1fdffbce JG |
2171 | } |
2172 | ||
624d5c51 | 2173 | /* is all this really necessary? */ |
5682ed33 | 2174 | ap->ops->sff_dev_select(ap, 0); |
624d5c51 | 2175 | if (dev1) |
5682ed33 | 2176 | ap->ops->sff_dev_select(ap, 1); |
624d5c51 | 2177 | if (dev0) |
5682ed33 | 2178 | ap->ops->sff_dev_select(ap, 0); |
624d5c51 TH |
2179 | |
2180 | return ret; | |
1fdffbce | 2181 | } |
0fe40ff8 | 2182 | EXPORT_SYMBOL_GPL(ata_sff_wait_after_reset); |
1fdffbce | 2183 | |
624d5c51 TH |
2184 | static int ata_bus_softreset(struct ata_port *ap, unsigned int devmask, |
2185 | unsigned long deadline) | |
2cc432ee | 2186 | { |
624d5c51 | 2187 | struct ata_ioports *ioaddr = &ap->ioaddr; |
2cc432ee | 2188 | |
624d5c51 TH |
2189 | DPRINTK("ata%u: bus reset via SRST\n", ap->print_id); |
2190 | ||
2191 | /* software reset. causes dev0 to be selected */ | |
2192 | iowrite8(ap->ctl, ioaddr->ctl_addr); | |
2193 | udelay(20); /* FIXME: flush */ | |
2194 | iowrite8(ap->ctl | ATA_SRST, ioaddr->ctl_addr); | |
2195 | udelay(20); /* FIXME: flush */ | |
2196 | iowrite8(ap->ctl, ioaddr->ctl_addr); | |
e3e4385f | 2197 | ap->last_ctl = ap->ctl; |
624d5c51 | 2198 | |
705e76be TH |
2199 | /* wait the port to become ready */ |
2200 | return ata_sff_wait_after_reset(&ap->link, devmask, deadline); | |
2cc432ee JG |
2201 | } |
2202 | ||
6d97dbd7 | 2203 | /** |
9363c382 | 2204 | * ata_sff_softreset - reset host port via ATA SRST |
624d5c51 TH |
2205 | * @link: ATA link to reset |
2206 | * @classes: resulting classes of attached devices | |
2207 | * @deadline: deadline jiffies for the operation | |
6d97dbd7 | 2208 | * |
624d5c51 | 2209 | * Reset host port using ATA SRST. |
6d97dbd7 TH |
2210 | * |
2211 | * LOCKING: | |
624d5c51 TH |
2212 | * Kernel thread context (may sleep) |
2213 | * | |
2214 | * RETURNS: | |
2215 | * 0 on success, -errno otherwise. | |
6d97dbd7 | 2216 | */ |
9363c382 | 2217 | int ata_sff_softreset(struct ata_link *link, unsigned int *classes, |
624d5c51 | 2218 | unsigned long deadline) |
6d97dbd7 | 2219 | { |
624d5c51 TH |
2220 | struct ata_port *ap = link->ap; |
2221 | unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS; | |
2222 | unsigned int devmask = 0; | |
2223 | int rc; | |
2224 | u8 err; | |
6d97dbd7 | 2225 | |
624d5c51 | 2226 | DPRINTK("ENTER\n"); |
6d97dbd7 | 2227 | |
624d5c51 TH |
2228 | /* determine if device 0/1 are present */ |
2229 | if (ata_devchk(ap, 0)) | |
2230 | devmask |= (1 << 0); | |
2231 | if (slave_possible && ata_devchk(ap, 1)) | |
2232 | devmask |= (1 << 1); | |
2233 | ||
2234 | /* select device 0 again */ | |
5682ed33 | 2235 | ap->ops->sff_dev_select(ap, 0); |
624d5c51 TH |
2236 | |
2237 | /* issue bus reset */ | |
2238 | DPRINTK("about to softreset, devmask=%x\n", devmask); | |
2239 | rc = ata_bus_softreset(ap, devmask, deadline); | |
2240 | /* if link is occupied, -ENODEV too is an error */ | |
2241 | if (rc && (rc != -ENODEV || sata_scr_valid(link))) { | |
2242 | ata_link_printk(link, KERN_ERR, "SRST failed (errno=%d)\n", rc); | |
2243 | return rc; | |
2244 | } | |
0f0a3ad3 | 2245 | |
624d5c51 | 2246 | /* determine by signature whether we have ATA or ATAPI devices */ |
9363c382 | 2247 | classes[0] = ata_sff_dev_classify(&link->device[0], |
624d5c51 TH |
2248 | devmask & (1 << 0), &err); |
2249 | if (slave_possible && err != 0x81) | |
9363c382 | 2250 | classes[1] = ata_sff_dev_classify(&link->device[1], |
624d5c51 TH |
2251 | devmask & (1 << 1), &err); |
2252 | ||
624d5c51 TH |
2253 | DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]); |
2254 | return 0; | |
6d97dbd7 | 2255 | } |
0fe40ff8 | 2256 | EXPORT_SYMBOL_GPL(ata_sff_softreset); |
6d97dbd7 TH |
2257 | |
2258 | /** | |
9363c382 | 2259 | * sata_sff_hardreset - reset host port via SATA phy reset |
624d5c51 TH |
2260 | * @link: link to reset |
2261 | * @class: resulting class of attached device | |
2262 | * @deadline: deadline jiffies for the operation | |
6d97dbd7 | 2263 | * |
624d5c51 TH |
2264 | * SATA phy-reset host port using DET bits of SControl register, |
2265 | * wait for !BSY and classify the attached device. | |
6d97dbd7 TH |
2266 | * |
2267 | * LOCKING: | |
624d5c51 TH |
2268 | * Kernel thread context (may sleep) |
2269 | * | |
2270 | * RETURNS: | |
2271 | * 0 on success, -errno otherwise. | |
6d97dbd7 | 2272 | */ |
9363c382 | 2273 | int sata_sff_hardreset(struct ata_link *link, unsigned int *class, |
624d5c51 | 2274 | unsigned long deadline) |
6d97dbd7 | 2275 | { |
9dadd45b TH |
2276 | struct ata_eh_context *ehc = &link->eh_context; |
2277 | const unsigned long *timing = sata_ehc_deb_timing(ehc); | |
2278 | bool online; | |
624d5c51 TH |
2279 | int rc; |
2280 | ||
9dadd45b TH |
2281 | rc = sata_link_hardreset(link, timing, deadline, &online, |
2282 | ata_sff_check_ready); | |
9dadd45b TH |
2283 | if (online) |
2284 | *class = ata_sff_dev_classify(link->device, 1, NULL); | |
624d5c51 TH |
2285 | |
2286 | DPRINTK("EXIT, class=%u\n", *class); | |
9dadd45b | 2287 | return rc; |
6d97dbd7 | 2288 | } |
0fe40ff8 | 2289 | EXPORT_SYMBOL_GPL(sata_sff_hardreset); |
6d97dbd7 | 2290 | |
203c75b8 TH |
2291 | /** |
2292 | * ata_sff_postreset - SFF postreset callback | |
2293 | * @link: the target SFF ata_link | |
2294 | * @classes: classes of attached devices | |
2295 | * | |
2296 | * This function is invoked after a successful reset. It first | |
2297 | * calls ata_std_postreset() and performs SFF specific postreset | |
2298 | * processing. | |
2299 | * | |
2300 | * LOCKING: | |
2301 | * Kernel thread context (may sleep) | |
2302 | */ | |
2303 | void ata_sff_postreset(struct ata_link *link, unsigned int *classes) | |
2304 | { | |
2305 | struct ata_port *ap = link->ap; | |
2306 | ||
2307 | ata_std_postreset(link, classes); | |
2308 | ||
2309 | /* is double-select really necessary? */ | |
2310 | if (classes[0] != ATA_DEV_NONE) | |
2311 | ap->ops->sff_dev_select(ap, 1); | |
2312 | if (classes[1] != ATA_DEV_NONE) | |
2313 | ap->ops->sff_dev_select(ap, 0); | |
2314 | ||
2315 | /* bail out if no device is present */ | |
2316 | if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) { | |
2317 | DPRINTK("EXIT, no device\n"); | |
2318 | return; | |
2319 | } | |
2320 | ||
2321 | /* set up device control */ | |
41dec29b SS |
2322 | if (ap->ops->sff_set_devctl || ap->ioaddr.ctl_addr) { |
2323 | ata_sff_set_devctl(ap, ap->ctl); | |
e3e4385f SM |
2324 | ap->last_ctl = ap->ctl; |
2325 | } | |
203c75b8 | 2326 | } |
0fe40ff8 | 2327 | EXPORT_SYMBOL_GPL(ata_sff_postreset); |
203c75b8 | 2328 | |
3d47aa8e AC |
2329 | /** |
2330 | * ata_sff_drain_fifo - Stock FIFO drain logic for SFF controllers | |
2331 | * @qc: command | |
2332 | * | |
2333 | * Drain the FIFO and device of any stuck data following a command | |
3ad2f3fb | 2334 | * failing to complete. In some cases this is necessary before a |
3d47aa8e AC |
2335 | * reset will recover the device. |
2336 | * | |
2337 | */ | |
2338 | ||
2339 | void ata_sff_drain_fifo(struct ata_queued_cmd *qc) | |
2340 | { | |
2341 | int count; | |
2342 | struct ata_port *ap; | |
2343 | ||
2344 | /* We only need to flush incoming data when a command was running */ | |
2345 | if (qc == NULL || qc->dma_dir == DMA_TO_DEVICE) | |
2346 | return; | |
2347 | ||
2348 | ap = qc->ap; | |
2349 | /* Drain up to 64K of data before we give up this recovery method */ | |
2350 | for (count = 0; (ap->ops->sff_check_status(ap) & ATA_DRQ) | |
9a8fd68b | 2351 | && count < 65536; count += 2) |
3d47aa8e AC |
2352 | ioread16(ap->ioaddr.data_addr); |
2353 | ||
2354 | /* Can become DEBUG later */ | |
2355 | if (count) | |
2356 | ata_port_printk(ap, KERN_DEBUG, | |
2357 | "drained %d bytes to clear DRQ.\n", count); | |
2358 | ||
2359 | } | |
2360 | EXPORT_SYMBOL_GPL(ata_sff_drain_fifo); | |
2361 | ||
6d97dbd7 | 2362 | /** |
fe06e5f9 | 2363 | * ata_sff_error_handler - Stock error handler for SFF controller |
6d97dbd7 | 2364 | * @ap: port to handle error for |
6d97dbd7 | 2365 | * |
9363c382 | 2366 | * Stock error handler for SFF controller. It can handle both |
6d97dbd7 TH |
2367 | * PATA and SATA controllers. Many controllers should be able to |
2368 | * use this EH as-is or with some added handling before and | |
2369 | * after. | |
2370 | * | |
6d97dbd7 TH |
2371 | * LOCKING: |
2372 | * Kernel thread context (may sleep) | |
2373 | */ | |
9363c382 | 2374 | void ata_sff_error_handler(struct ata_port *ap) |
6d97dbd7 | 2375 | { |
a1efdaba TH |
2376 | ata_reset_fn_t softreset = ap->ops->softreset; |
2377 | ata_reset_fn_t hardreset = ap->ops->hardreset; | |
6d97dbd7 TH |
2378 | struct ata_queued_cmd *qc; |
2379 | unsigned long flags; | |
6d97dbd7 | 2380 | |
9af5c9c9 | 2381 | qc = __ata_qc_from_tag(ap, ap->link.active_tag); |
6d97dbd7 TH |
2382 | if (qc && !(qc->flags & ATA_QCFLAG_FAILED)) |
2383 | qc = NULL; | |
2384 | ||
ba6a1308 | 2385 | spin_lock_irqsave(ap->lock, flags); |
6d97dbd7 | 2386 | |
fe06e5f9 TH |
2387 | /* |
2388 | * We *MUST* do FIFO draining before we issue a reset as | |
2389 | * several devices helpfully clear their internal state and | |
2390 | * will lock solid if we touch the data port post reset. Pass | |
2391 | * qc in case anyone wants to do different PIO/DMA recovery or | |
2392 | * has per command fixups | |
3d47aa8e | 2393 | */ |
8244cd05 TH |
2394 | if (ap->ops->sff_drain_fifo) |
2395 | ap->ops->sff_drain_fifo(qc); | |
6d97dbd7 | 2396 | |
ba6a1308 | 2397 | spin_unlock_irqrestore(ap->lock, flags); |
6d97dbd7 | 2398 | |
fe06e5f9 | 2399 | /* ignore ata_sff_softreset if ctl isn't accessible */ |
9363c382 | 2400 | if (softreset == ata_sff_softreset && !ap->ioaddr.ctl_addr) |
a1efdaba | 2401 | softreset = NULL; |
fe06e5f9 TH |
2402 | |
2403 | /* ignore built-in hardresets if SCR access is not available */ | |
2404 | if ((hardreset == sata_std_hardreset || | |
2405 | hardreset == sata_sff_hardreset) && !sata_scr_valid(&ap->link)) | |
a1efdaba | 2406 | hardreset = NULL; |
6d97dbd7 | 2407 | |
a1efdaba TH |
2408 | ata_do_eh(ap, ap->ops->prereset, softreset, hardreset, |
2409 | ap->ops->postreset); | |
6d97dbd7 | 2410 | } |
0fe40ff8 | 2411 | EXPORT_SYMBOL_GPL(ata_sff_error_handler); |
6d97dbd7 | 2412 | |
624d5c51 | 2413 | /** |
9363c382 | 2414 | * ata_sff_std_ports - initialize ioaddr with standard port offsets. |
624d5c51 TH |
2415 | * @ioaddr: IO address structure to be initialized |
2416 | * | |
2417 | * Utility function which initializes data_addr, error_addr, | |
2418 | * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr, | |
2419 | * device_addr, status_addr, and command_addr to standard offsets | |
2420 | * relative to cmd_addr. | |
2421 | * | |
2422 | * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr. | |
2423 | */ | |
9363c382 | 2424 | void ata_sff_std_ports(struct ata_ioports *ioaddr) |
624d5c51 TH |
2425 | { |
2426 | ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA; | |
2427 | ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR; | |
2428 | ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE; | |
2429 | ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT; | |
2430 | ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL; | |
2431 | ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM; | |
2432 | ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH; | |
2433 | ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE; | |
2434 | ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS; | |
2435 | ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD; | |
2436 | } | |
0fe40ff8 | 2437 | EXPORT_SYMBOL_GPL(ata_sff_std_ports); |
624d5c51 | 2438 | |
1fdffbce | 2439 | #ifdef CONFIG_PCI |
4112e16a | 2440 | |
272f7884 TH |
2441 | static int ata_resources_present(struct pci_dev *pdev, int port) |
2442 | { | |
2443 | int i; | |
2444 | ||
2445 | /* Check the PCI resources for this channel are enabled */ | |
2446 | port = port * 2; | |
0fe40ff8 | 2447 | for (i = 0; i < 2; i++) { |
272f7884 TH |
2448 | if (pci_resource_start(pdev, port + i) == 0 || |
2449 | pci_resource_len(pdev, port + i) == 0) | |
2450 | return 0; | |
2451 | } | |
2452 | return 1; | |
2453 | } | |
2454 | ||
d491b27b | 2455 | /** |
9363c382 | 2456 | * ata_pci_sff_init_host - acquire native PCI ATA resources and init host |
d491b27b | 2457 | * @host: target ATA host |
d491b27b | 2458 | * |
1626aeb8 TH |
2459 | * Acquire native PCI ATA resources for @host and initialize the |
2460 | * first two ports of @host accordingly. Ports marked dummy are | |
2461 | * skipped and allocation failure makes the port dummy. | |
d491b27b | 2462 | * |
d583bc18 TH |
2463 | * Note that native PCI resources are valid even for legacy hosts |
2464 | * as we fix up pdev resources array early in boot, so this | |
2465 | * function can be used for both native and legacy SFF hosts. | |
2466 | * | |
d491b27b TH |
2467 | * LOCKING: |
2468 | * Inherited from calling layer (may sleep). | |
2469 | * | |
2470 | * RETURNS: | |
1626aeb8 TH |
2471 | * 0 if at least one port is initialized, -ENODEV if no port is |
2472 | * available. | |
d491b27b | 2473 | */ |
9363c382 | 2474 | int ata_pci_sff_init_host(struct ata_host *host) |
d491b27b TH |
2475 | { |
2476 | struct device *gdev = host->dev; | |
2477 | struct pci_dev *pdev = to_pci_dev(gdev); | |
1626aeb8 | 2478 | unsigned int mask = 0; |
d491b27b TH |
2479 | int i, rc; |
2480 | ||
d491b27b TH |
2481 | /* request, iomap BARs and init port addresses accordingly */ |
2482 | for (i = 0; i < 2; i++) { | |
2483 | struct ata_port *ap = host->ports[i]; | |
2484 | int base = i * 2; | |
2485 | void __iomem * const *iomap; | |
2486 | ||
1626aeb8 TH |
2487 | if (ata_port_is_dummy(ap)) |
2488 | continue; | |
2489 | ||
2490 | /* Discard disabled ports. Some controllers show | |
2491 | * their unused channels this way. Disabled ports are | |
2492 | * made dummy. | |
2493 | */ | |
2494 | if (!ata_resources_present(pdev, i)) { | |
2495 | ap->ops = &ata_dummy_port_ops; | |
d491b27b | 2496 | continue; |
1626aeb8 | 2497 | } |
d491b27b | 2498 | |
35a10a80 TH |
2499 | rc = pcim_iomap_regions(pdev, 0x3 << base, |
2500 | dev_driver_string(gdev)); | |
d491b27b | 2501 | if (rc) { |
1626aeb8 TH |
2502 | dev_printk(KERN_WARNING, gdev, |
2503 | "failed to request/iomap BARs for port %d " | |
2504 | "(errno=%d)\n", i, rc); | |
d491b27b TH |
2505 | if (rc == -EBUSY) |
2506 | pcim_pin_device(pdev); | |
1626aeb8 TH |
2507 | ap->ops = &ata_dummy_port_ops; |
2508 | continue; | |
d491b27b TH |
2509 | } |
2510 | host->iomap = iomap = pcim_iomap_table(pdev); | |
2511 | ||
2512 | ap->ioaddr.cmd_addr = iomap[base]; | |
2513 | ap->ioaddr.altstatus_addr = | |
2514 | ap->ioaddr.ctl_addr = (void __iomem *) | |
2515 | ((unsigned long)iomap[base + 1] | ATA_PCI_CTL_OFS); | |
9363c382 | 2516 | ata_sff_std_ports(&ap->ioaddr); |
1626aeb8 | 2517 | |
cbcdd875 TH |
2518 | ata_port_desc(ap, "cmd 0x%llx ctl 0x%llx", |
2519 | (unsigned long long)pci_resource_start(pdev, base), | |
2520 | (unsigned long long)pci_resource_start(pdev, base + 1)); | |
2521 | ||
1626aeb8 TH |
2522 | mask |= 1 << i; |
2523 | } | |
2524 | ||
2525 | if (!mask) { | |
2526 | dev_printk(KERN_ERR, gdev, "no available native port\n"); | |
2527 | return -ENODEV; | |
d491b27b TH |
2528 | } |
2529 | ||
2530 | return 0; | |
2531 | } | |
0fe40ff8 | 2532 | EXPORT_SYMBOL_GPL(ata_pci_sff_init_host); |
d491b27b | 2533 | |
21b0ad4f | 2534 | /** |
9363c382 | 2535 | * ata_pci_sff_prepare_host - helper to prepare native PCI ATA host |
21b0ad4f | 2536 | * @pdev: target PCI device |
1626aeb8 | 2537 | * @ppi: array of port_info, must be enough for two ports |
21b0ad4f TH |
2538 | * @r_host: out argument for the initialized ATA host |
2539 | * | |
2540 | * Helper to allocate ATA host for @pdev, acquire all native PCI | |
2541 | * resources and initialize it accordingly in one go. | |
2542 | * | |
2543 | * LOCKING: | |
2544 | * Inherited from calling layer (may sleep). | |
2545 | * | |
2546 | * RETURNS: | |
2547 | * 0 on success, -errno otherwise. | |
2548 | */ | |
9363c382 | 2549 | int ata_pci_sff_prepare_host(struct pci_dev *pdev, |
0fe40ff8 | 2550 | const struct ata_port_info * const *ppi, |
d583bc18 | 2551 | struct ata_host **r_host) |
21b0ad4f TH |
2552 | { |
2553 | struct ata_host *host; | |
21b0ad4f TH |
2554 | int rc; |
2555 | ||
2556 | if (!devres_open_group(&pdev->dev, NULL, GFP_KERNEL)) | |
2557 | return -ENOMEM; | |
2558 | ||
2559 | host = ata_host_alloc_pinfo(&pdev->dev, ppi, 2); | |
2560 | if (!host) { | |
2561 | dev_printk(KERN_ERR, &pdev->dev, | |
2562 | "failed to allocate ATA host\n"); | |
2563 | rc = -ENOMEM; | |
2564 | goto err_out; | |
2565 | } | |
2566 | ||
9363c382 | 2567 | rc = ata_pci_sff_init_host(host); |
21b0ad4f TH |
2568 | if (rc) |
2569 | goto err_out; | |
2570 | ||
2571 | /* init DMA related stuff */ | |
c7087652 | 2572 | ata_pci_bmdma_init(host); |
21b0ad4f TH |
2573 | |
2574 | devres_remove_group(&pdev->dev, NULL); | |
2575 | *r_host = host; | |
2576 | return 0; | |
2577 | ||
0fe40ff8 | 2578 | err_out: |
21b0ad4f TH |
2579 | devres_release_group(&pdev->dev, NULL); |
2580 | return rc; | |
2581 | } | |
0fe40ff8 | 2582 | EXPORT_SYMBOL_GPL(ata_pci_sff_prepare_host); |
21b0ad4f | 2583 | |
4e6b79fa | 2584 | /** |
9363c382 | 2585 | * ata_pci_sff_activate_host - start SFF host, request IRQ and register it |
4e6b79fa TH |
2586 | * @host: target SFF ATA host |
2587 | * @irq_handler: irq_handler used when requesting IRQ(s) | |
2588 | * @sht: scsi_host_template to use when registering the host | |
2589 | * | |
2590 | * This is the counterpart of ata_host_activate() for SFF ATA | |
2591 | * hosts. This separate helper is necessary because SFF hosts | |
2592 | * use two separate interrupts in legacy mode. | |
2593 | * | |
2594 | * LOCKING: | |
2595 | * Inherited from calling layer (may sleep). | |
2596 | * | |
2597 | * RETURNS: | |
2598 | * 0 on success, -errno otherwise. | |
2599 | */ | |
9363c382 | 2600 | int ata_pci_sff_activate_host(struct ata_host *host, |
4e6b79fa TH |
2601 | irq_handler_t irq_handler, |
2602 | struct scsi_host_template *sht) | |
2603 | { | |
2604 | struct device *dev = host->dev; | |
2605 | struct pci_dev *pdev = to_pci_dev(dev); | |
2606 | const char *drv_name = dev_driver_string(host->dev); | |
2607 | int legacy_mode = 0, rc; | |
2608 | ||
2609 | rc = ata_host_start(host); | |
2610 | if (rc) | |
2611 | return rc; | |
2612 | ||
2613 | if ((pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) { | |
2614 | u8 tmp8, mask; | |
2615 | ||
2616 | /* TODO: What if one channel is in native mode ... */ | |
2617 | pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8); | |
2618 | mask = (1 << 2) | (1 << 0); | |
2619 | if ((tmp8 & mask) != mask) | |
2620 | legacy_mode = 1; | |
2621 | #if defined(CONFIG_NO_ATA_LEGACY) | |
2622 | /* Some platforms with PCI limits cannot address compat | |
2623 | port space. In that case we punt if their firmware has | |
2624 | left a device in compatibility mode */ | |
2625 | if (legacy_mode) { | |
2626 | printk(KERN_ERR "ata: Compatibility mode ATA is not supported on this platform, skipping.\n"); | |
2627 | return -EOPNOTSUPP; | |
2628 | } | |
2629 | #endif | |
2630 | } | |
2631 | ||
2632 | if (!devres_open_group(dev, NULL, GFP_KERNEL)) | |
2633 | return -ENOMEM; | |
2634 | ||
2635 | if (!legacy_mode && pdev->irq) { | |
2636 | rc = devm_request_irq(dev, pdev->irq, irq_handler, | |
2637 | IRQF_SHARED, drv_name, host); | |
2638 | if (rc) | |
2639 | goto out; | |
2640 | ||
2641 | ata_port_desc(host->ports[0], "irq %d", pdev->irq); | |
2642 | ata_port_desc(host->ports[1], "irq %d", pdev->irq); | |
2643 | } else if (legacy_mode) { | |
2644 | if (!ata_port_is_dummy(host->ports[0])) { | |
2645 | rc = devm_request_irq(dev, ATA_PRIMARY_IRQ(pdev), | |
2646 | irq_handler, IRQF_SHARED, | |
2647 | drv_name, host); | |
2648 | if (rc) | |
2649 | goto out; | |
2650 | ||
2651 | ata_port_desc(host->ports[0], "irq %d", | |
2652 | ATA_PRIMARY_IRQ(pdev)); | |
2653 | } | |
2654 | ||
2655 | if (!ata_port_is_dummy(host->ports[1])) { | |
2656 | rc = devm_request_irq(dev, ATA_SECONDARY_IRQ(pdev), | |
2657 | irq_handler, IRQF_SHARED, | |
2658 | drv_name, host); | |
2659 | if (rc) | |
2660 | goto out; | |
2661 | ||
2662 | ata_port_desc(host->ports[1], "irq %d", | |
2663 | ATA_SECONDARY_IRQ(pdev)); | |
2664 | } | |
2665 | } | |
2666 | ||
2667 | rc = ata_host_register(host, sht); | |
0fe40ff8 | 2668 | out: |
4e6b79fa TH |
2669 | if (rc == 0) |
2670 | devres_remove_group(dev, NULL); | |
2671 | else | |
2672 | devres_release_group(dev, NULL); | |
2673 | ||
2674 | return rc; | |
2675 | } | |
0fe40ff8 | 2676 | EXPORT_SYMBOL_GPL(ata_pci_sff_activate_host); |
4e6b79fa | 2677 | |
1fdffbce | 2678 | /** |
9363c382 | 2679 | * ata_pci_sff_init_one - Initialize/register PCI IDE host controller |
1fdffbce | 2680 | * @pdev: Controller to be initialized |
1626aeb8 | 2681 | * @ppi: array of port_info, must be enough for two ports |
1bd5b715 | 2682 | * @sht: scsi_host_template to use when registering the host |
887125e3 | 2683 | * @host_priv: host private_data |
16ea0fc9 | 2684 | * @hflag: host flags |
1fdffbce JG |
2685 | * |
2686 | * This is a helper function which can be called from a driver's | |
2687 | * xxx_init_one() probe function if the hardware uses traditional | |
2688 | * IDE taskfile registers. | |
2689 | * | |
2690 | * This function calls pci_enable_device(), reserves its register | |
2691 | * regions, sets the dma mask, enables bus master mode, and calls | |
2692 | * ata_device_add() | |
2693 | * | |
2ec7df04 AC |
2694 | * ASSUMPTION: |
2695 | * Nobody makes a single channel controller that appears solely as | |
2696 | * the secondary legacy port on PCI. | |
2697 | * | |
1fdffbce JG |
2698 | * LOCKING: |
2699 | * Inherited from PCI layer (may sleep). | |
2700 | * | |
2701 | * RETURNS: | |
2702 | * Zero on success, negative on errno-based value on error. | |
2703 | */ | |
9363c382 | 2704 | int ata_pci_sff_init_one(struct pci_dev *pdev, |
16ea0fc9 AC |
2705 | const struct ata_port_info * const *ppi, |
2706 | struct scsi_host_template *sht, void *host_priv, int hflag) | |
1fdffbce | 2707 | { |
f0d36efd | 2708 | struct device *dev = &pdev->dev; |
1626aeb8 | 2709 | const struct ata_port_info *pi = NULL; |
0f834de3 | 2710 | struct ata_host *host = NULL; |
1626aeb8 | 2711 | int i, rc; |
1fdffbce JG |
2712 | |
2713 | DPRINTK("ENTER\n"); | |
2714 | ||
1626aeb8 TH |
2715 | /* look up the first valid port_info */ |
2716 | for (i = 0; i < 2 && ppi[i]; i++) { | |
2717 | if (ppi[i]->port_ops != &ata_dummy_port_ops) { | |
2718 | pi = ppi[i]; | |
2719 | break; | |
2720 | } | |
2721 | } | |
f0d36efd | 2722 | |
1626aeb8 TH |
2723 | if (!pi) { |
2724 | dev_printk(KERN_ERR, &pdev->dev, | |
2725 | "no valid port_info specified\n"); | |
2726 | return -EINVAL; | |
2727 | } | |
c791c306 | 2728 | |
1626aeb8 TH |
2729 | if (!devres_open_group(dev, NULL, GFP_KERNEL)) |
2730 | return -ENOMEM; | |
1fdffbce | 2731 | |
f0d36efd | 2732 | rc = pcim_enable_device(pdev); |
1fdffbce | 2733 | if (rc) |
4e6b79fa | 2734 | goto out; |
1fdffbce | 2735 | |
4e6b79fa | 2736 | /* prepare and activate SFF host */ |
9363c382 | 2737 | rc = ata_pci_sff_prepare_host(pdev, ppi, &host); |
d583bc18 | 2738 | if (rc) |
4e6b79fa | 2739 | goto out; |
887125e3 | 2740 | host->private_data = host_priv; |
16ea0fc9 | 2741 | host->flags |= hflag; |
d491b27b | 2742 | |
d491b27b | 2743 | pci_set_master(pdev); |
9363c382 | 2744 | rc = ata_pci_sff_activate_host(host, ata_sff_interrupt, sht); |
0fe40ff8 | 2745 | out: |
4e6b79fa TH |
2746 | if (rc == 0) |
2747 | devres_remove_group(&pdev->dev, NULL); | |
2748 | else | |
2749 | devres_release_group(&pdev->dev, NULL); | |
d491b27b | 2750 | |
1fdffbce JG |
2751 | return rc; |
2752 | } | |
0fe40ff8 | 2753 | EXPORT_SYMBOL_GPL(ata_pci_sff_init_one); |
1fdffbce JG |
2754 | |
2755 | #endif /* CONFIG_PCI */ | |
9f2f7210 TH |
2756 | |
2757 | const struct ata_port_operations ata_bmdma_port_ops = { | |
2758 | .inherits = &ata_sff_port_ops, | |
2759 | ||
fe06e5f9 TH |
2760 | .error_handler = ata_bmdma_error_handler, |
2761 | .post_internal_cmd = ata_bmdma_post_internal_cmd, | |
2762 | ||
9f2f7210 TH |
2763 | .bmdma_setup = ata_bmdma_setup, |
2764 | .bmdma_start = ata_bmdma_start, | |
2765 | .bmdma_stop = ata_bmdma_stop, | |
2766 | .bmdma_status = ata_bmdma_status, | |
c7087652 TH |
2767 | |
2768 | .port_start = ata_bmdma_port_start, | |
9f2f7210 TH |
2769 | }; |
2770 | EXPORT_SYMBOL_GPL(ata_bmdma_port_ops); | |
2771 | ||
2772 | const struct ata_port_operations ata_bmdma32_port_ops = { | |
2773 | .inherits = &ata_bmdma_port_ops, | |
2774 | ||
2775 | .sff_data_xfer = ata_sff_data_xfer32, | |
c7087652 | 2776 | .port_start = ata_bmdma_port_start32, |
9f2f7210 TH |
2777 | }; |
2778 | EXPORT_SYMBOL_GPL(ata_bmdma32_port_ops); | |
2779 | ||
fe06e5f9 TH |
2780 | /** |
2781 | * ata_bmdma_error_handler - Stock error handler for BMDMA controller | |
2782 | * @ap: port to handle error for | |
2783 | * | |
2784 | * Stock error handler for BMDMA controller. It can handle both | |
2785 | * PATA and SATA controllers. Most BMDMA controllers should be | |
2786 | * able to use this EH as-is or with some added handling before | |
2787 | * and after. | |
2788 | * | |
2789 | * LOCKING: | |
2790 | * Kernel thread context (may sleep) | |
2791 | */ | |
2792 | void ata_bmdma_error_handler(struct ata_port *ap) | |
2793 | { | |
2794 | struct ata_queued_cmd *qc; | |
2795 | unsigned long flags; | |
2796 | bool thaw = false; | |
2797 | ||
2798 | qc = __ata_qc_from_tag(ap, ap->link.active_tag); | |
2799 | if (qc && !(qc->flags & ATA_QCFLAG_FAILED)) | |
2800 | qc = NULL; | |
2801 | ||
2802 | /* reset PIO HSM and stop DMA engine */ | |
2803 | spin_lock_irqsave(ap->lock, flags); | |
2804 | ||
2805 | if (qc && ata_is_dma(qc->tf.protocol)) { | |
2806 | u8 host_stat; | |
2807 | ||
2808 | host_stat = ap->ops->bmdma_status(ap); | |
2809 | ||
2810 | /* BMDMA controllers indicate host bus error by | |
2811 | * setting DMA_ERR bit and timing out. As it wasn't | |
2812 | * really a timeout event, adjust error mask and | |
2813 | * cancel frozen state. | |
2814 | */ | |
2815 | if (qc->err_mask == AC_ERR_TIMEOUT && (host_stat & ATA_DMA_ERR)) { | |
2816 | qc->err_mask = AC_ERR_HOST_BUS; | |
2817 | thaw = true; | |
2818 | } | |
2819 | ||
2820 | ap->ops->bmdma_stop(qc); | |
2821 | ||
2822 | /* if we're gonna thaw, make sure IRQ is clear */ | |
2823 | if (thaw) { | |
2824 | ap->ops->sff_check_status(ap); | |
2825 | ap->ops->sff_irq_clear(ap); | |
2826 | } | |
2827 | } | |
2828 | ||
2829 | spin_unlock_irqrestore(ap->lock, flags); | |
2830 | ||
2831 | if (thaw) | |
2832 | ata_eh_thaw_port(ap); | |
2833 | ||
2834 | ata_sff_error_handler(ap); | |
2835 | } | |
2836 | EXPORT_SYMBOL_GPL(ata_bmdma_error_handler); | |
2837 | ||
2838 | /** | |
2839 | * ata_bmdma_post_internal_cmd - Stock post_internal_cmd for BMDMA | |
2840 | * @qc: internal command to clean up | |
2841 | * | |
2842 | * LOCKING: | |
2843 | * Kernel thread context (may sleep) | |
2844 | */ | |
2845 | void ata_bmdma_post_internal_cmd(struct ata_queued_cmd *qc) | |
2846 | { | |
2847 | struct ata_port *ap = qc->ap; | |
2848 | unsigned long flags; | |
2849 | ||
2850 | if (ata_is_dma(qc->tf.protocol)) { | |
2851 | spin_lock_irqsave(ap->lock, flags); | |
2852 | ap->ops->bmdma_stop(qc); | |
2853 | spin_unlock_irqrestore(ap->lock, flags); | |
2854 | } | |
2855 | } | |
2856 | EXPORT_SYMBOL_GPL(ata_bmdma_post_internal_cmd); | |
2857 | ||
9f2f7210 TH |
2858 | /** |
2859 | * ata_bmdma_setup - Set up PCI IDE BMDMA transaction | |
2860 | * @qc: Info associated with this ATA transaction. | |
2861 | * | |
2862 | * LOCKING: | |
2863 | * spin_lock_irqsave(host lock) | |
2864 | */ | |
2865 | void ata_bmdma_setup(struct ata_queued_cmd *qc) | |
2866 | { | |
2867 | struct ata_port *ap = qc->ap; | |
2868 | unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE); | |
2869 | u8 dmactl; | |
2870 | ||
2871 | /* load PRD table addr. */ | |
2872 | mb(); /* make sure PRD table writes are visible to controller */ | |
2873 | iowrite32(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS); | |
2874 | ||
2875 | /* specify data direction, triple-check start bit is clear */ | |
2876 | dmactl = ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_CMD); | |
2877 | dmactl &= ~(ATA_DMA_WR | ATA_DMA_START); | |
2878 | if (!rw) | |
2879 | dmactl |= ATA_DMA_WR; | |
2880 | iowrite8(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD); | |
2881 | ||
2882 | /* issue r/w command */ | |
2883 | ap->ops->sff_exec_command(ap, &qc->tf); | |
2884 | } | |
2885 | EXPORT_SYMBOL_GPL(ata_bmdma_setup); | |
2886 | ||
2887 | /** | |
2888 | * ata_bmdma_start - Start a PCI IDE BMDMA transaction | |
2889 | * @qc: Info associated with this ATA transaction. | |
2890 | * | |
2891 | * LOCKING: | |
2892 | * spin_lock_irqsave(host lock) | |
2893 | */ | |
2894 | void ata_bmdma_start(struct ata_queued_cmd *qc) | |
2895 | { | |
2896 | struct ata_port *ap = qc->ap; | |
2897 | u8 dmactl; | |
2898 | ||
2899 | /* start host DMA transaction */ | |
2900 | dmactl = ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_CMD); | |
2901 | iowrite8(dmactl | ATA_DMA_START, ap->ioaddr.bmdma_addr + ATA_DMA_CMD); | |
2902 | ||
2903 | /* Strictly, one may wish to issue an ioread8() here, to | |
2904 | * flush the mmio write. However, control also passes | |
2905 | * to the hardware at this point, and it will interrupt | |
2906 | * us when we are to resume control. So, in effect, | |
2907 | * we don't care when the mmio write flushes. | |
2908 | * Further, a read of the DMA status register _immediately_ | |
2909 | * following the write may not be what certain flaky hardware | |
2910 | * is expected, so I think it is best to not add a readb() | |
2911 | * without first all the MMIO ATA cards/mobos. | |
2912 | * Or maybe I'm just being paranoid. | |
2913 | * | |
2914 | * FIXME: The posting of this write means I/O starts are | |
2915 | * unneccessarily delayed for MMIO | |
2916 | */ | |
2917 | } | |
2918 | EXPORT_SYMBOL_GPL(ata_bmdma_start); | |
2919 | ||
2920 | /** | |
2921 | * ata_bmdma_stop - Stop PCI IDE BMDMA transfer | |
2922 | * @qc: Command we are ending DMA for | |
2923 | * | |
2924 | * Clears the ATA_DMA_START flag in the dma control register | |
2925 | * | |
2926 | * May be used as the bmdma_stop() entry in ata_port_operations. | |
2927 | * | |
2928 | * LOCKING: | |
2929 | * spin_lock_irqsave(host lock) | |
2930 | */ | |
2931 | void ata_bmdma_stop(struct ata_queued_cmd *qc) | |
2932 | { | |
2933 | struct ata_port *ap = qc->ap; | |
2934 | void __iomem *mmio = ap->ioaddr.bmdma_addr; | |
2935 | ||
2936 | /* clear start/stop bit */ | |
2937 | iowrite8(ioread8(mmio + ATA_DMA_CMD) & ~ATA_DMA_START, | |
2938 | mmio + ATA_DMA_CMD); | |
2939 | ||
2940 | /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */ | |
2941 | ata_sff_dma_pause(ap); | |
2942 | } | |
2943 | EXPORT_SYMBOL_GPL(ata_bmdma_stop); | |
2944 | ||
2945 | /** | |
2946 | * ata_bmdma_status - Read PCI IDE BMDMA status | |
2947 | * @ap: Port associated with this ATA transaction. | |
2948 | * | |
2949 | * Read and return BMDMA status register. | |
2950 | * | |
2951 | * May be used as the bmdma_status() entry in ata_port_operations. | |
2952 | * | |
2953 | * LOCKING: | |
2954 | * spin_lock_irqsave(host lock) | |
2955 | */ | |
2956 | u8 ata_bmdma_status(struct ata_port *ap) | |
2957 | { | |
2958 | return ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS); | |
2959 | } | |
2960 | EXPORT_SYMBOL_GPL(ata_bmdma_status); | |
2961 | ||
c7087652 TH |
2962 | |
2963 | /** | |
2964 | * ata_bmdma_port_start - Set port up for bmdma. | |
2965 | * @ap: Port to initialize | |
2966 | * | |
2967 | * Called just after data structures for each port are | |
2968 | * initialized. Allocates space for PRD table. | |
2969 | * | |
2970 | * May be used as the port_start() entry in ata_port_operations. | |
2971 | * | |
2972 | * LOCKING: | |
2973 | * Inherited from caller. | |
2974 | */ | |
2975 | int ata_bmdma_port_start(struct ata_port *ap) | |
2976 | { | |
2977 | if (ap->mwdma_mask || ap->udma_mask) { | |
2978 | ap->prd = dmam_alloc_coherent(ap->host->dev, ATA_PRD_TBL_SZ, | |
2979 | &ap->prd_dma, GFP_KERNEL); | |
2980 | if (!ap->prd) | |
2981 | return -ENOMEM; | |
2982 | } | |
2983 | ||
2984 | return 0; | |
2985 | } | |
2986 | EXPORT_SYMBOL_GPL(ata_bmdma_port_start); | |
2987 | ||
2988 | /** | |
2989 | * ata_bmdma_port_start32 - Set port up for dma. | |
2990 | * @ap: Port to initialize | |
2991 | * | |
2992 | * Called just after data structures for each port are | |
2993 | * initialized. Enables 32bit PIO and allocates space for PRD | |
2994 | * table. | |
2995 | * | |
2996 | * May be used as the port_start() entry in ata_port_operations for | |
2997 | * devices that are capable of 32bit PIO. | |
2998 | * | |
2999 | * LOCKING: | |
3000 | * Inherited from caller. | |
3001 | */ | |
3002 | int ata_bmdma_port_start32(struct ata_port *ap) | |
3003 | { | |
3004 | ap->pflags |= ATA_PFLAG_PIO32 | ATA_PFLAG_PIO32CHANGE; | |
3005 | return ata_bmdma_port_start(ap); | |
3006 | } | |
3007 | EXPORT_SYMBOL_GPL(ata_bmdma_port_start32); | |
3008 | ||
9f2f7210 TH |
3009 | #ifdef CONFIG_PCI |
3010 | ||
3011 | /** | |
3012 | * ata_pci_bmdma_clear_simplex - attempt to kick device out of simplex | |
3013 | * @pdev: PCI device | |
3014 | * | |
3015 | * Some PCI ATA devices report simplex mode but in fact can be told to | |
3016 | * enter non simplex mode. This implements the necessary logic to | |
3017 | * perform the task on such devices. Calling it on other devices will | |
3018 | * have -undefined- behaviour. | |
3019 | */ | |
3020 | int ata_pci_bmdma_clear_simplex(struct pci_dev *pdev) | |
3021 | { | |
3022 | unsigned long bmdma = pci_resource_start(pdev, 4); | |
3023 | u8 simplex; | |
3024 | ||
3025 | if (bmdma == 0) | |
3026 | return -ENOENT; | |
3027 | ||
3028 | simplex = inb(bmdma + 0x02); | |
3029 | outb(simplex & 0x60, bmdma + 0x02); | |
3030 | simplex = inb(bmdma + 0x02); | |
3031 | if (simplex & 0x80) | |
3032 | return -EOPNOTSUPP; | |
3033 | return 0; | |
3034 | } | |
3035 | EXPORT_SYMBOL_GPL(ata_pci_bmdma_clear_simplex); | |
3036 | ||
c7087652 TH |
3037 | static void ata_bmdma_nodma(struct ata_host *host, const char *reason) |
3038 | { | |
3039 | int i; | |
3040 | ||
3041 | dev_printk(KERN_ERR, host->dev, "BMDMA: %s, falling back to PIO\n", | |
3042 | reason); | |
3043 | ||
3044 | for (i = 0; i < 2; i++) { | |
3045 | host->ports[i]->mwdma_mask = 0; | |
3046 | host->ports[i]->udma_mask = 0; | |
3047 | } | |
3048 | } | |
3049 | ||
9f2f7210 TH |
3050 | /** |
3051 | * ata_pci_bmdma_init - acquire PCI BMDMA resources and init ATA host | |
3052 | * @host: target ATA host | |
3053 | * | |
3054 | * Acquire PCI BMDMA resources and initialize @host accordingly. | |
3055 | * | |
3056 | * LOCKING: | |
3057 | * Inherited from calling layer (may sleep). | |
9f2f7210 | 3058 | */ |
c7087652 | 3059 | void ata_pci_bmdma_init(struct ata_host *host) |
9f2f7210 TH |
3060 | { |
3061 | struct device *gdev = host->dev; | |
3062 | struct pci_dev *pdev = to_pci_dev(gdev); | |
3063 | int i, rc; | |
3064 | ||
3065 | /* No BAR4 allocation: No DMA */ | |
c7087652 TH |
3066 | if (pci_resource_start(pdev, 4) == 0) { |
3067 | ata_bmdma_nodma(host, "BAR4 is zero"); | |
3068 | return; | |
3069 | } | |
9f2f7210 | 3070 | |
c7087652 TH |
3071 | /* |
3072 | * Some controllers require BMDMA region to be initialized | |
3073 | * even if DMA is not in use to clear IRQ status via | |
3074 | * ->sff_irq_clear method. Try to initialize bmdma_addr | |
3075 | * regardless of dma masks. | |
3076 | */ | |
9f2f7210 TH |
3077 | rc = pci_set_dma_mask(pdev, ATA_DMA_MASK); |
3078 | if (rc) | |
c7087652 TH |
3079 | ata_bmdma_nodma(host, "failed to set dma mask"); |
3080 | if (!rc) { | |
3081 | rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK); | |
3082 | if (rc) | |
3083 | ata_bmdma_nodma(host, | |
3084 | "failed to set consistent dma mask"); | |
3085 | } | |
9f2f7210 TH |
3086 | |
3087 | /* request and iomap DMA region */ | |
3088 | rc = pcim_iomap_regions(pdev, 1 << 4, dev_driver_string(gdev)); | |
3089 | if (rc) { | |
c7087652 TH |
3090 | ata_bmdma_nodma(host, "failed to request/iomap BAR4"); |
3091 | return; | |
9f2f7210 TH |
3092 | } |
3093 | host->iomap = pcim_iomap_table(pdev); | |
3094 | ||
3095 | for (i = 0; i < 2; i++) { | |
3096 | struct ata_port *ap = host->ports[i]; | |
3097 | void __iomem *bmdma = host->iomap[4] + 8 * i; | |
3098 | ||
3099 | if (ata_port_is_dummy(ap)) | |
3100 | continue; | |
3101 | ||
3102 | ap->ioaddr.bmdma_addr = bmdma; | |
3103 | if ((!(ap->flags & ATA_FLAG_IGN_SIMPLEX)) && | |
3104 | (ioread8(bmdma + 2) & 0x80)) | |
3105 | host->flags |= ATA_HOST_SIMPLEX; | |
3106 | ||
3107 | ata_port_desc(ap, "bmdma 0x%llx", | |
3108 | (unsigned long long)pci_resource_start(pdev, 4) + 8 * i); | |
3109 | } | |
9f2f7210 TH |
3110 | } |
3111 | EXPORT_SYMBOL_GPL(ata_pci_bmdma_init); | |
3112 | ||
3113 | #endif /* CONFIG_PCI */ | |
270390e1 TH |
3114 | |
3115 | /** | |
3116 | * ata_sff_port_init - Initialize SFF/BMDMA ATA port | |
3117 | * @ap: Port to initialize | |
3118 | * | |
3119 | * Called on port allocation to initialize SFF/BMDMA specific | |
3120 | * fields. | |
3121 | * | |
3122 | * LOCKING: | |
3123 | * None. | |
3124 | */ | |
3125 | void ata_sff_port_init(struct ata_port *ap) | |
3126 | { | |
c429137a | 3127 | INIT_DELAYED_WORK(&ap->sff_pio_task, ata_sff_pio_task); |
5fe7454a TH |
3128 | ap->ctl = ATA_DEVCTL_OBS; |
3129 | ap->last_ctl = 0xFF; | |
270390e1 TH |
3130 | } |
3131 | ||
3132 | int __init ata_sff_init(void) | |
3133 | { | |
c429137a TH |
3134 | /* |
3135 | * FIXME: In UP case, there is only one workqueue thread and if you | |
3136 | * have more than one PIO device, latency is bloody awful, with | |
3137 | * occasional multi-second "hiccups" as one PIO device waits for | |
3138 | * another. It's an ugly wart that users DO occasionally complain | |
3139 | * about; luckily most users have at most one PIO polled device. | |
3140 | */ | |
3141 | ata_sff_wq = create_workqueue("ata_sff"); | |
3142 | if (!ata_sff_wq) | |
3143 | return -ENOMEM; | |
3144 | ||
270390e1 TH |
3145 | return 0; |
3146 | } | |
3147 | ||
3148 | void __exit ata_sff_exit(void) | |
3149 | { | |
c429137a | 3150 | destroy_workqueue(ata_sff_wq); |
270390e1 | 3151 | } |