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