Linux 4.19.133
[linux/fpc-iii.git] / drivers / ata / libata-sff.c
blob873cc0906055129eff641428db31c118cbdbeb21
1 /*
2 * libata-sff.c - helper library for PCI IDE BMDMA
4 * Maintained by: Tejun Heo <tj@kernel.org>
5 * Please ALWAYS copy linux-ide@vger.kernel.org
6 * on emails.
8 * Copyright 2003-2006 Red Hat, Inc. All rights reserved.
9 * Copyright 2003-2006 Jeff Garzik
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.
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.
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.
27 * libata documentation is available via 'make {ps|pdf}docs',
28 * as Documentation/driver-api/libata.rst
30 * Hardware documentation available from http://www.t13.org/ and
31 * http://www.sata-io.org/
35 #include <linux/kernel.h>
36 #include <linux/gfp.h>
37 #include <linux/pci.h>
38 #include <linux/module.h>
39 #include <linux/libata.h>
40 #include <linux/highmem.h>
42 #include "libata.h"
44 static struct workqueue_struct *ata_sff_wq;
46 const struct ata_port_operations ata_sff_port_ops = {
47 .inherits = &ata_base_port_ops,
49 .qc_prep = ata_noop_qc_prep,
50 .qc_issue = ata_sff_qc_issue,
51 .qc_fill_rtf = ata_sff_qc_fill_rtf,
53 .freeze = ata_sff_freeze,
54 .thaw = ata_sff_thaw,
55 .prereset = ata_sff_prereset,
56 .softreset = ata_sff_softreset,
57 .hardreset = sata_sff_hardreset,
58 .postreset = ata_sff_postreset,
59 .error_handler = ata_sff_error_handler,
61 .sff_dev_select = ata_sff_dev_select,
62 .sff_check_status = ata_sff_check_status,
63 .sff_tf_load = ata_sff_tf_load,
64 .sff_tf_read = ata_sff_tf_read,
65 .sff_exec_command = ata_sff_exec_command,
66 .sff_data_xfer = ata_sff_data_xfer,
67 .sff_drain_fifo = ata_sff_drain_fifo,
69 .lost_interrupt = ata_sff_lost_interrupt,
71 EXPORT_SYMBOL_GPL(ata_sff_port_ops);
73 /**
74 * ata_sff_check_status - Read device status reg & clear interrupt
75 * @ap: port where the device is
77 * Reads ATA taskfile status register for currently-selected device
78 * and return its value. This also clears pending interrupts
79 * from this device
81 * LOCKING:
82 * Inherited from caller.
84 u8 ata_sff_check_status(struct ata_port *ap)
86 return ioread8(ap->ioaddr.status_addr);
88 EXPORT_SYMBOL_GPL(ata_sff_check_status);
90 /**
91 * ata_sff_altstatus - Read device alternate status reg
92 * @ap: port where the device is
94 * Reads ATA taskfile alternate status register for
95 * currently-selected device and return its value.
97 * Note: may NOT be used as the check_altstatus() entry in
98 * ata_port_operations.
100 * LOCKING:
101 * Inherited from caller.
103 static u8 ata_sff_altstatus(struct ata_port *ap)
105 if (ap->ops->sff_check_altstatus)
106 return ap->ops->sff_check_altstatus(ap);
108 return ioread8(ap->ioaddr.altstatus_addr);
112 * ata_sff_irq_status - Check if the device is busy
113 * @ap: port where the device is
115 * Determine if the port is currently busy. Uses altstatus
116 * if available in order to avoid clearing shared IRQ status
117 * when finding an IRQ source. Non ctl capable devices don't
118 * share interrupt lines fortunately for us.
120 * LOCKING:
121 * Inherited from caller.
123 static u8 ata_sff_irq_status(struct ata_port *ap)
125 u8 status;
127 if (ap->ops->sff_check_altstatus || ap->ioaddr.altstatus_addr) {
128 status = ata_sff_altstatus(ap);
129 /* Not us: We are busy */
130 if (status & ATA_BUSY)
131 return status;
133 /* Clear INTRQ latch */
134 status = ap->ops->sff_check_status(ap);
135 return status;
139 * ata_sff_sync - Flush writes
140 * @ap: Port to wait for.
142 * CAUTION:
143 * If we have an mmio device with no ctl and no altstatus
144 * method this will fail. No such devices are known to exist.
146 * LOCKING:
147 * Inherited from caller.
150 static void ata_sff_sync(struct ata_port *ap)
152 if (ap->ops->sff_check_altstatus)
153 ap->ops->sff_check_altstatus(ap);
154 else if (ap->ioaddr.altstatus_addr)
155 ioread8(ap->ioaddr.altstatus_addr);
159 * ata_sff_pause - Flush writes and wait 400nS
160 * @ap: Port to pause for.
162 * CAUTION:
163 * If we have an mmio device with no ctl and no altstatus
164 * method this will fail. No such devices are known to exist.
166 * LOCKING:
167 * Inherited from caller.
170 void ata_sff_pause(struct ata_port *ap)
172 ata_sff_sync(ap);
173 ndelay(400);
175 EXPORT_SYMBOL_GPL(ata_sff_pause);
178 * ata_sff_dma_pause - Pause before commencing DMA
179 * @ap: Port to pause for.
181 * Perform I/O fencing and ensure sufficient cycle delays occur
182 * for the HDMA1:0 transition
185 void ata_sff_dma_pause(struct ata_port *ap)
187 if (ap->ops->sff_check_altstatus || ap->ioaddr.altstatus_addr) {
188 /* An altstatus read will cause the needed delay without
189 messing up the IRQ status */
190 ata_sff_altstatus(ap);
191 return;
193 /* There are no DMA controllers without ctl. BUG here to ensure
194 we never violate the HDMA1:0 transition timing and risk
195 corruption. */
196 BUG();
198 EXPORT_SYMBOL_GPL(ata_sff_dma_pause);
201 * ata_sff_busy_sleep - sleep until BSY clears, or timeout
202 * @ap: port containing status register to be polled
203 * @tmout_pat: impatience timeout in msecs
204 * @tmout: overall timeout in msecs
206 * Sleep until ATA Status register bit BSY clears,
207 * or a timeout occurs.
209 * LOCKING:
210 * Kernel thread context (may sleep).
212 * RETURNS:
213 * 0 on success, -errno otherwise.
215 int ata_sff_busy_sleep(struct ata_port *ap,
216 unsigned long tmout_pat, unsigned long tmout)
218 unsigned long timer_start, timeout;
219 u8 status;
221 status = ata_sff_busy_wait(ap, ATA_BUSY, 300);
222 timer_start = jiffies;
223 timeout = ata_deadline(timer_start, tmout_pat);
224 while (status != 0xff && (status & ATA_BUSY) &&
225 time_before(jiffies, timeout)) {
226 ata_msleep(ap, 50);
227 status = ata_sff_busy_wait(ap, ATA_BUSY, 3);
230 if (status != 0xff && (status & ATA_BUSY))
231 ata_port_warn(ap,
232 "port is slow to respond, please be patient (Status 0x%x)\n",
233 status);
235 timeout = ata_deadline(timer_start, tmout);
236 while (status != 0xff && (status & ATA_BUSY) &&
237 time_before(jiffies, timeout)) {
238 ata_msleep(ap, 50);
239 status = ap->ops->sff_check_status(ap);
242 if (status == 0xff)
243 return -ENODEV;
245 if (status & ATA_BUSY) {
246 ata_port_err(ap,
247 "port failed to respond (%lu secs, Status 0x%x)\n",
248 DIV_ROUND_UP(tmout, 1000), status);
249 return -EBUSY;
252 return 0;
254 EXPORT_SYMBOL_GPL(ata_sff_busy_sleep);
256 static int ata_sff_check_ready(struct ata_link *link)
258 u8 status = link->ap->ops->sff_check_status(link->ap);
260 return ata_check_ready(status);
264 * ata_sff_wait_ready - sleep until BSY clears, or timeout
265 * @link: SFF link to wait ready status for
266 * @deadline: deadline jiffies for the operation
268 * Sleep until ATA Status register bit BSY clears, or timeout
269 * occurs.
271 * LOCKING:
272 * Kernel thread context (may sleep).
274 * RETURNS:
275 * 0 on success, -errno otherwise.
277 int ata_sff_wait_ready(struct ata_link *link, unsigned long deadline)
279 return ata_wait_ready(link, deadline, ata_sff_check_ready);
281 EXPORT_SYMBOL_GPL(ata_sff_wait_ready);
284 * ata_sff_set_devctl - Write device control reg
285 * @ap: port where the device is
286 * @ctl: value to write
288 * Writes ATA taskfile device control register.
290 * Note: may NOT be used as the sff_set_devctl() entry in
291 * ata_port_operations.
293 * LOCKING:
294 * Inherited from caller.
296 static void ata_sff_set_devctl(struct ata_port *ap, u8 ctl)
298 if (ap->ops->sff_set_devctl)
299 ap->ops->sff_set_devctl(ap, ctl);
300 else
301 iowrite8(ctl, ap->ioaddr.ctl_addr);
305 * ata_sff_dev_select - Select device 0/1 on ATA bus
306 * @ap: ATA channel to manipulate
307 * @device: ATA device (numbered from zero) to select
309 * Use the method defined in the ATA specification to
310 * make either device 0, or device 1, active on the
311 * ATA channel. Works with both PIO and MMIO.
313 * May be used as the dev_select() entry in ata_port_operations.
315 * LOCKING:
316 * caller.
318 void ata_sff_dev_select(struct ata_port *ap, unsigned int device)
320 u8 tmp;
322 if (device == 0)
323 tmp = ATA_DEVICE_OBS;
324 else
325 tmp = ATA_DEVICE_OBS | ATA_DEV1;
327 iowrite8(tmp, ap->ioaddr.device_addr);
328 ata_sff_pause(ap); /* needed; also flushes, for mmio */
330 EXPORT_SYMBOL_GPL(ata_sff_dev_select);
333 * ata_dev_select - Select device 0/1 on ATA bus
334 * @ap: ATA channel to manipulate
335 * @device: ATA device (numbered from zero) to select
336 * @wait: non-zero to wait for Status register BSY bit to clear
337 * @can_sleep: non-zero if context allows sleeping
339 * Use the method defined in the ATA specification to
340 * make either device 0, or device 1, active on the
341 * ATA channel.
343 * This is a high-level version of ata_sff_dev_select(), which
344 * additionally provides the services of inserting the proper
345 * pauses and status polling, where needed.
347 * LOCKING:
348 * caller.
350 static void ata_dev_select(struct ata_port *ap, unsigned int device,
351 unsigned int wait, unsigned int can_sleep)
353 if (ata_msg_probe(ap))
354 ata_port_info(ap, "ata_dev_select: ENTER, device %u, wait %u\n",
355 device, wait);
357 if (wait)
358 ata_wait_idle(ap);
360 ap->ops->sff_dev_select(ap, device);
362 if (wait) {
363 if (can_sleep && ap->link.device[device].class == ATA_DEV_ATAPI)
364 ata_msleep(ap, 150);
365 ata_wait_idle(ap);
370 * ata_sff_irq_on - Enable interrupts on a port.
371 * @ap: Port on which interrupts are enabled.
373 * Enable interrupts on a legacy IDE device using MMIO or PIO,
374 * wait for idle, clear any pending interrupts.
376 * Note: may NOT be used as the sff_irq_on() entry in
377 * ata_port_operations.
379 * LOCKING:
380 * Inherited from caller.
382 void ata_sff_irq_on(struct ata_port *ap)
384 struct ata_ioports *ioaddr = &ap->ioaddr;
386 if (ap->ops->sff_irq_on) {
387 ap->ops->sff_irq_on(ap);
388 return;
391 ap->ctl &= ~ATA_NIEN;
392 ap->last_ctl = ap->ctl;
394 if (ap->ops->sff_set_devctl || ioaddr->ctl_addr)
395 ata_sff_set_devctl(ap, ap->ctl);
396 ata_wait_idle(ap);
398 if (ap->ops->sff_irq_clear)
399 ap->ops->sff_irq_clear(ap);
401 EXPORT_SYMBOL_GPL(ata_sff_irq_on);
404 * ata_sff_tf_load - send taskfile registers to host controller
405 * @ap: Port to which output is sent
406 * @tf: ATA taskfile register set
408 * Outputs ATA taskfile to standard ATA host controller.
410 * LOCKING:
411 * Inherited from caller.
413 void ata_sff_tf_load(struct ata_port *ap, const struct ata_taskfile *tf)
415 struct ata_ioports *ioaddr = &ap->ioaddr;
416 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
418 if (tf->ctl != ap->last_ctl) {
419 if (ioaddr->ctl_addr)
420 iowrite8(tf->ctl, ioaddr->ctl_addr);
421 ap->last_ctl = tf->ctl;
422 ata_wait_idle(ap);
425 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
426 WARN_ON_ONCE(!ioaddr->ctl_addr);
427 iowrite8(tf->hob_feature, ioaddr->feature_addr);
428 iowrite8(tf->hob_nsect, ioaddr->nsect_addr);
429 iowrite8(tf->hob_lbal, ioaddr->lbal_addr);
430 iowrite8(tf->hob_lbam, ioaddr->lbam_addr);
431 iowrite8(tf->hob_lbah, ioaddr->lbah_addr);
432 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
433 tf->hob_feature,
434 tf->hob_nsect,
435 tf->hob_lbal,
436 tf->hob_lbam,
437 tf->hob_lbah);
440 if (is_addr) {
441 iowrite8(tf->feature, ioaddr->feature_addr);
442 iowrite8(tf->nsect, ioaddr->nsect_addr);
443 iowrite8(tf->lbal, ioaddr->lbal_addr);
444 iowrite8(tf->lbam, ioaddr->lbam_addr);
445 iowrite8(tf->lbah, ioaddr->lbah_addr);
446 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
447 tf->feature,
448 tf->nsect,
449 tf->lbal,
450 tf->lbam,
451 tf->lbah);
454 if (tf->flags & ATA_TFLAG_DEVICE) {
455 iowrite8(tf->device, ioaddr->device_addr);
456 VPRINTK("device 0x%X\n", tf->device);
459 ata_wait_idle(ap);
461 EXPORT_SYMBOL_GPL(ata_sff_tf_load);
464 * ata_sff_tf_read - input device's ATA taskfile shadow registers
465 * @ap: Port from which input is read
466 * @tf: ATA taskfile register set for storing input
468 * Reads ATA taskfile registers for currently-selected device
469 * into @tf. Assumes the device has a fully SFF compliant task file
470 * layout and behaviour. If you device does not (eg has a different
471 * status method) then you will need to provide a replacement tf_read
473 * LOCKING:
474 * Inherited from caller.
476 void ata_sff_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
478 struct ata_ioports *ioaddr = &ap->ioaddr;
480 tf->command = ata_sff_check_status(ap);
481 tf->feature = ioread8(ioaddr->error_addr);
482 tf->nsect = ioread8(ioaddr->nsect_addr);
483 tf->lbal = ioread8(ioaddr->lbal_addr);
484 tf->lbam = ioread8(ioaddr->lbam_addr);
485 tf->lbah = ioread8(ioaddr->lbah_addr);
486 tf->device = ioread8(ioaddr->device_addr);
488 if (tf->flags & ATA_TFLAG_LBA48) {
489 if (likely(ioaddr->ctl_addr)) {
490 iowrite8(tf->ctl | ATA_HOB, ioaddr->ctl_addr);
491 tf->hob_feature = ioread8(ioaddr->error_addr);
492 tf->hob_nsect = ioread8(ioaddr->nsect_addr);
493 tf->hob_lbal = ioread8(ioaddr->lbal_addr);
494 tf->hob_lbam = ioread8(ioaddr->lbam_addr);
495 tf->hob_lbah = ioread8(ioaddr->lbah_addr);
496 iowrite8(tf->ctl, ioaddr->ctl_addr);
497 ap->last_ctl = tf->ctl;
498 } else
499 WARN_ON_ONCE(1);
502 EXPORT_SYMBOL_GPL(ata_sff_tf_read);
505 * ata_sff_exec_command - issue ATA command to host controller
506 * @ap: port to which command is being issued
507 * @tf: ATA taskfile register set
509 * Issues ATA command, with proper synchronization with interrupt
510 * handler / other threads.
512 * LOCKING:
513 * spin_lock_irqsave(host lock)
515 void ata_sff_exec_command(struct ata_port *ap, const struct ata_taskfile *tf)
517 DPRINTK("ata%u: cmd 0x%X\n", ap->print_id, tf->command);
519 iowrite8(tf->command, ap->ioaddr.command_addr);
520 ata_sff_pause(ap);
522 EXPORT_SYMBOL_GPL(ata_sff_exec_command);
525 * ata_tf_to_host - issue ATA taskfile to host controller
526 * @ap: port to which command is being issued
527 * @tf: ATA taskfile register set
529 * Issues ATA taskfile register set to ATA host controller,
530 * with proper synchronization with interrupt handler and
531 * other threads.
533 * LOCKING:
534 * spin_lock_irqsave(host lock)
536 static inline void ata_tf_to_host(struct ata_port *ap,
537 const struct ata_taskfile *tf)
539 ap->ops->sff_tf_load(ap, tf);
540 ap->ops->sff_exec_command(ap, tf);
544 * ata_sff_data_xfer - Transfer data by PIO
545 * @qc: queued command
546 * @buf: data buffer
547 * @buflen: buffer length
548 * @rw: read/write
550 * Transfer data from/to the device data register by PIO.
552 * LOCKING:
553 * Inherited from caller.
555 * RETURNS:
556 * Bytes consumed.
558 unsigned int ata_sff_data_xfer(struct ata_queued_cmd *qc, unsigned char *buf,
559 unsigned int buflen, int rw)
561 struct ata_port *ap = qc->dev->link->ap;
562 void __iomem *data_addr = ap->ioaddr.data_addr;
563 unsigned int words = buflen >> 1;
565 /* Transfer multiple of 2 bytes */
566 if (rw == READ)
567 ioread16_rep(data_addr, buf, words);
568 else
569 iowrite16_rep(data_addr, buf, words);
571 /* Transfer trailing byte, if any. */
572 if (unlikely(buflen & 0x01)) {
573 unsigned char pad[2] = { };
575 /* Point buf to the tail of buffer */
576 buf += buflen - 1;
579 * Use io*16_rep() accessors here as well to avoid pointlessly
580 * swapping bytes to and from on the big endian machines...
582 if (rw == READ) {
583 ioread16_rep(data_addr, pad, 1);
584 *buf = pad[0];
585 } else {
586 pad[0] = *buf;
587 iowrite16_rep(data_addr, pad, 1);
589 words++;
592 return words << 1;
594 EXPORT_SYMBOL_GPL(ata_sff_data_xfer);
597 * ata_sff_data_xfer32 - Transfer data by PIO
598 * @qc: queued command
599 * @buf: data buffer
600 * @buflen: buffer length
601 * @rw: read/write
603 * Transfer data from/to the device data register by PIO using 32bit
604 * I/O operations.
606 * LOCKING:
607 * Inherited from caller.
609 * RETURNS:
610 * Bytes consumed.
613 unsigned int ata_sff_data_xfer32(struct ata_queued_cmd *qc, unsigned char *buf,
614 unsigned int buflen, int rw)
616 struct ata_device *dev = qc->dev;
617 struct ata_port *ap = dev->link->ap;
618 void __iomem *data_addr = ap->ioaddr.data_addr;
619 unsigned int words = buflen >> 2;
620 int slop = buflen & 3;
622 if (!(ap->pflags & ATA_PFLAG_PIO32))
623 return ata_sff_data_xfer(qc, buf, buflen, rw);
625 /* Transfer multiple of 4 bytes */
626 if (rw == READ)
627 ioread32_rep(data_addr, buf, words);
628 else
629 iowrite32_rep(data_addr, buf, words);
631 /* Transfer trailing bytes, if any */
632 if (unlikely(slop)) {
633 unsigned char pad[4] = { };
635 /* Point buf to the tail of buffer */
636 buf += buflen - slop;
639 * Use io*_rep() accessors here as well to avoid pointlessly
640 * swapping bytes to and from on the big endian machines...
642 if (rw == READ) {
643 if (slop < 3)
644 ioread16_rep(data_addr, pad, 1);
645 else
646 ioread32_rep(data_addr, pad, 1);
647 memcpy(buf, pad, slop);
648 } else {
649 memcpy(pad, buf, slop);
650 if (slop < 3)
651 iowrite16_rep(data_addr, pad, 1);
652 else
653 iowrite32_rep(data_addr, pad, 1);
656 return (buflen + 1) & ~1;
658 EXPORT_SYMBOL_GPL(ata_sff_data_xfer32);
661 * ata_pio_sector - Transfer a sector of data.
662 * @qc: Command on going
664 * Transfer qc->sect_size bytes of data from/to the ATA device.
666 * LOCKING:
667 * Inherited from caller.
669 static void ata_pio_sector(struct ata_queued_cmd *qc)
671 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
672 struct ata_port *ap = qc->ap;
673 struct page *page;
674 unsigned int offset;
675 unsigned char *buf;
677 if (!qc->cursg) {
678 qc->curbytes = qc->nbytes;
679 return;
681 if (qc->curbytes == qc->nbytes - qc->sect_size)
682 ap->hsm_task_state = HSM_ST_LAST;
684 page = sg_page(qc->cursg);
685 offset = qc->cursg->offset + qc->cursg_ofs;
687 /* get the current page and offset */
688 page = nth_page(page, (offset >> PAGE_SHIFT));
689 offset %= PAGE_SIZE;
691 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
693 /* do the actual data transfer */
694 buf = kmap_atomic(page);
695 ap->ops->sff_data_xfer(qc, buf + offset, qc->sect_size, do_write);
696 kunmap_atomic(buf);
698 if (!do_write && !PageSlab(page))
699 flush_dcache_page(page);
701 qc->curbytes += qc->sect_size;
702 qc->cursg_ofs += qc->sect_size;
704 if (qc->cursg_ofs == qc->cursg->length) {
705 qc->cursg = sg_next(qc->cursg);
706 if (!qc->cursg)
707 ap->hsm_task_state = HSM_ST_LAST;
708 qc->cursg_ofs = 0;
713 * ata_pio_sectors - Transfer one or many sectors.
714 * @qc: Command on going
716 * Transfer one or many sectors of data from/to the
717 * ATA device for the DRQ request.
719 * LOCKING:
720 * Inherited from caller.
722 static void ata_pio_sectors(struct ata_queued_cmd *qc)
724 if (is_multi_taskfile(&qc->tf)) {
725 /* READ/WRITE MULTIPLE */
726 unsigned int nsect;
728 WARN_ON_ONCE(qc->dev->multi_count == 0);
730 nsect = min((qc->nbytes - qc->curbytes) / qc->sect_size,
731 qc->dev->multi_count);
732 while (nsect--)
733 ata_pio_sector(qc);
734 } else
735 ata_pio_sector(qc);
737 ata_sff_sync(qc->ap); /* flush */
741 * atapi_send_cdb - Write CDB bytes to hardware
742 * @ap: Port to which ATAPI device is attached.
743 * @qc: Taskfile currently active
745 * When device has indicated its readiness to accept
746 * a CDB, this function is called. Send the CDB.
748 * LOCKING:
749 * caller.
751 static void atapi_send_cdb(struct ata_port *ap, struct ata_queued_cmd *qc)
753 /* send SCSI cdb */
754 DPRINTK("send cdb\n");
755 WARN_ON_ONCE(qc->dev->cdb_len < 12);
757 ap->ops->sff_data_xfer(qc, qc->cdb, qc->dev->cdb_len, 1);
758 ata_sff_sync(ap);
759 /* FIXME: If the CDB is for DMA do we need to do the transition delay
760 or is bmdma_start guaranteed to do it ? */
761 switch (qc->tf.protocol) {
762 case ATAPI_PROT_PIO:
763 ap->hsm_task_state = HSM_ST;
764 break;
765 case ATAPI_PROT_NODATA:
766 ap->hsm_task_state = HSM_ST_LAST;
767 break;
768 #ifdef CONFIG_ATA_BMDMA
769 case ATAPI_PROT_DMA:
770 ap->hsm_task_state = HSM_ST_LAST;
771 /* initiate bmdma */
772 ap->ops->bmdma_start(qc);
773 break;
774 #endif /* CONFIG_ATA_BMDMA */
775 default:
776 BUG();
781 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
782 * @qc: Command on going
783 * @bytes: number of bytes
785 * Transfer Transfer data from/to the ATAPI device.
787 * LOCKING:
788 * Inherited from caller.
791 static int __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
793 int rw = (qc->tf.flags & ATA_TFLAG_WRITE) ? WRITE : READ;
794 struct ata_port *ap = qc->ap;
795 struct ata_device *dev = qc->dev;
796 struct ata_eh_info *ehi = &dev->link->eh_info;
797 struct scatterlist *sg;
798 struct page *page;
799 unsigned char *buf;
800 unsigned int offset, count, consumed;
802 next_sg:
803 sg = qc->cursg;
804 if (unlikely(!sg)) {
805 ata_ehi_push_desc(ehi, "unexpected or too much trailing data "
806 "buf=%u cur=%u bytes=%u",
807 qc->nbytes, qc->curbytes, bytes);
808 return -1;
811 page = sg_page(sg);
812 offset = sg->offset + qc->cursg_ofs;
814 /* get the current page and offset */
815 page = nth_page(page, (offset >> PAGE_SHIFT));
816 offset %= PAGE_SIZE;
818 /* don't overrun current sg */
819 count = min(sg->length - qc->cursg_ofs, bytes);
821 /* don't cross page boundaries */
822 count = min(count, (unsigned int)PAGE_SIZE - offset);
824 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
826 /* do the actual data transfer */
827 buf = kmap_atomic(page);
828 consumed = ap->ops->sff_data_xfer(qc, buf + offset, count, rw);
829 kunmap_atomic(buf);
831 bytes -= min(bytes, consumed);
832 qc->curbytes += count;
833 qc->cursg_ofs += count;
835 if (qc->cursg_ofs == sg->length) {
836 qc->cursg = sg_next(qc->cursg);
837 qc->cursg_ofs = 0;
841 * There used to be a WARN_ON_ONCE(qc->cursg && count != consumed);
842 * Unfortunately __atapi_pio_bytes doesn't know enough to do the WARN
843 * check correctly as it doesn't know if it is the last request being
844 * made. Somebody should implement a proper sanity check.
846 if (bytes)
847 goto next_sg;
848 return 0;
852 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
853 * @qc: Command on going
855 * Transfer Transfer data from/to the ATAPI device.
857 * LOCKING:
858 * Inherited from caller.
860 static void atapi_pio_bytes(struct ata_queued_cmd *qc)
862 struct ata_port *ap = qc->ap;
863 struct ata_device *dev = qc->dev;
864 struct ata_eh_info *ehi = &dev->link->eh_info;
865 unsigned int ireason, bc_lo, bc_hi, bytes;
866 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
868 /* Abuse qc->result_tf for temp storage of intermediate TF
869 * here to save some kernel stack usage.
870 * For normal completion, qc->result_tf is not relevant. For
871 * error, qc->result_tf is later overwritten by ata_qc_complete().
872 * So, the correctness of qc->result_tf is not affected.
874 ap->ops->sff_tf_read(ap, &qc->result_tf);
875 ireason = qc->result_tf.nsect;
876 bc_lo = qc->result_tf.lbam;
877 bc_hi = qc->result_tf.lbah;
878 bytes = (bc_hi << 8) | bc_lo;
880 /* shall be cleared to zero, indicating xfer of data */
881 if (unlikely(ireason & ATAPI_COD))
882 goto atapi_check;
884 /* make sure transfer direction matches expected */
885 i_write = ((ireason & ATAPI_IO) == 0) ? 1 : 0;
886 if (unlikely(do_write != i_write))
887 goto atapi_check;
889 if (unlikely(!bytes))
890 goto atapi_check;
892 VPRINTK("ata%u: xfering %d bytes\n", ap->print_id, bytes);
894 if (unlikely(__atapi_pio_bytes(qc, bytes)))
895 goto err_out;
896 ata_sff_sync(ap); /* flush */
898 return;
900 atapi_check:
901 ata_ehi_push_desc(ehi, "ATAPI check failed (ireason=0x%x bytes=%u)",
902 ireason, bytes);
903 err_out:
904 qc->err_mask |= AC_ERR_HSM;
905 ap->hsm_task_state = HSM_ST_ERR;
909 * ata_hsm_ok_in_wq - Check if the qc can be handled in the workqueue.
910 * @ap: the target ata_port
911 * @qc: qc on going
913 * RETURNS:
914 * 1 if ok in workqueue, 0 otherwise.
916 static inline int ata_hsm_ok_in_wq(struct ata_port *ap,
917 struct ata_queued_cmd *qc)
919 if (qc->tf.flags & ATA_TFLAG_POLLING)
920 return 1;
922 if (ap->hsm_task_state == HSM_ST_FIRST) {
923 if (qc->tf.protocol == ATA_PROT_PIO &&
924 (qc->tf.flags & ATA_TFLAG_WRITE))
925 return 1;
927 if (ata_is_atapi(qc->tf.protocol) &&
928 !(qc->dev->flags & ATA_DFLAG_CDB_INTR))
929 return 1;
932 return 0;
936 * ata_hsm_qc_complete - finish a qc running on standard HSM
937 * @qc: Command to complete
938 * @in_wq: 1 if called from workqueue, 0 otherwise
940 * Finish @qc which is running on standard HSM.
942 * LOCKING:
943 * If @in_wq is zero, spin_lock_irqsave(host lock).
944 * Otherwise, none on entry and grabs host lock.
946 static void ata_hsm_qc_complete(struct ata_queued_cmd *qc, int in_wq)
948 struct ata_port *ap = qc->ap;
950 if (ap->ops->error_handler) {
951 if (in_wq) {
952 /* EH might have kicked in while host lock is
953 * released.
955 qc = ata_qc_from_tag(ap, qc->tag);
956 if (qc) {
957 if (likely(!(qc->err_mask & AC_ERR_HSM))) {
958 ata_sff_irq_on(ap);
959 ata_qc_complete(qc);
960 } else
961 ata_port_freeze(ap);
963 } else {
964 if (likely(!(qc->err_mask & AC_ERR_HSM)))
965 ata_qc_complete(qc);
966 else
967 ata_port_freeze(ap);
969 } else {
970 if (in_wq) {
971 ata_sff_irq_on(ap);
972 ata_qc_complete(qc);
973 } else
974 ata_qc_complete(qc);
979 * ata_sff_hsm_move - move the HSM to the next state.
980 * @ap: the target ata_port
981 * @qc: qc on going
982 * @status: current device status
983 * @in_wq: 1 if called from workqueue, 0 otherwise
985 * RETURNS:
986 * 1 when poll next status needed, 0 otherwise.
988 int ata_sff_hsm_move(struct ata_port *ap, struct ata_queued_cmd *qc,
989 u8 status, int in_wq)
991 struct ata_link *link = qc->dev->link;
992 struct ata_eh_info *ehi = &link->eh_info;
993 int poll_next;
995 lockdep_assert_held(ap->lock);
997 WARN_ON_ONCE((qc->flags & ATA_QCFLAG_ACTIVE) == 0);
999 /* Make sure ata_sff_qc_issue() does not throw things
1000 * like DMA polling into the workqueue. Notice that
1001 * in_wq is not equivalent to (qc->tf.flags & ATA_TFLAG_POLLING).
1003 WARN_ON_ONCE(in_wq != ata_hsm_ok_in_wq(ap, qc));
1005 fsm_start:
1006 DPRINTK("ata%u: protocol %d task_state %d (dev_stat 0x%X)\n",
1007 ap->print_id, qc->tf.protocol, ap->hsm_task_state, status);
1009 switch (ap->hsm_task_state) {
1010 case HSM_ST_FIRST:
1011 /* Send first data block or PACKET CDB */
1013 /* If polling, we will stay in the work queue after
1014 * sending the data. Otherwise, interrupt handler
1015 * takes over after sending the data.
1017 poll_next = (qc->tf.flags & ATA_TFLAG_POLLING);
1019 /* check device status */
1020 if (unlikely((status & ATA_DRQ) == 0)) {
1021 /* handle BSY=0, DRQ=0 as error */
1022 if (likely(status & (ATA_ERR | ATA_DF)))
1023 /* device stops HSM for abort/error */
1024 qc->err_mask |= AC_ERR_DEV;
1025 else {
1026 /* HSM violation. Let EH handle this */
1027 ata_ehi_push_desc(ehi,
1028 "ST_FIRST: !(DRQ|ERR|DF)");
1029 qc->err_mask |= AC_ERR_HSM;
1032 ap->hsm_task_state = HSM_ST_ERR;
1033 goto fsm_start;
1036 /* Device should not ask for data transfer (DRQ=1)
1037 * when it finds something wrong.
1038 * We ignore DRQ here and stop the HSM by
1039 * changing hsm_task_state to HSM_ST_ERR and
1040 * let the EH abort the command or reset the device.
1042 if (unlikely(status & (ATA_ERR | ATA_DF))) {
1043 /* Some ATAPI tape drives forget to clear the ERR bit
1044 * when doing the next command (mostly request sense).
1045 * We ignore ERR here to workaround and proceed sending
1046 * the CDB.
1048 if (!(qc->dev->horkage & ATA_HORKAGE_STUCK_ERR)) {
1049 ata_ehi_push_desc(ehi, "ST_FIRST: "
1050 "DRQ=1 with device error, "
1051 "dev_stat 0x%X", status);
1052 qc->err_mask |= AC_ERR_HSM;
1053 ap->hsm_task_state = HSM_ST_ERR;
1054 goto fsm_start;
1058 if (qc->tf.protocol == ATA_PROT_PIO) {
1059 /* PIO data out protocol.
1060 * send first data block.
1063 /* ata_pio_sectors() might change the state
1064 * to HSM_ST_LAST. so, the state is changed here
1065 * before ata_pio_sectors().
1067 ap->hsm_task_state = HSM_ST;
1068 ata_pio_sectors(qc);
1069 } else
1070 /* send CDB */
1071 atapi_send_cdb(ap, qc);
1073 /* if polling, ata_sff_pio_task() handles the rest.
1074 * otherwise, interrupt handler takes over from here.
1076 break;
1078 case HSM_ST:
1079 /* complete command or read/write the data register */
1080 if (qc->tf.protocol == ATAPI_PROT_PIO) {
1081 /* ATAPI PIO protocol */
1082 if ((status & ATA_DRQ) == 0) {
1083 /* No more data to transfer or device error.
1084 * Device error will be tagged in HSM_ST_LAST.
1086 ap->hsm_task_state = HSM_ST_LAST;
1087 goto fsm_start;
1090 /* Device should not ask for data transfer (DRQ=1)
1091 * when it finds something wrong.
1092 * We ignore DRQ here and stop the HSM by
1093 * changing hsm_task_state to HSM_ST_ERR and
1094 * let the EH abort the command or reset the device.
1096 if (unlikely(status & (ATA_ERR | ATA_DF))) {
1097 ata_ehi_push_desc(ehi, "ST-ATAPI: "
1098 "DRQ=1 with device error, "
1099 "dev_stat 0x%X", status);
1100 qc->err_mask |= AC_ERR_HSM;
1101 ap->hsm_task_state = HSM_ST_ERR;
1102 goto fsm_start;
1105 atapi_pio_bytes(qc);
1107 if (unlikely(ap->hsm_task_state == HSM_ST_ERR))
1108 /* bad ireason reported by device */
1109 goto fsm_start;
1111 } else {
1112 /* ATA PIO protocol */
1113 if (unlikely((status & ATA_DRQ) == 0)) {
1114 /* handle BSY=0, DRQ=0 as error */
1115 if (likely(status & (ATA_ERR | ATA_DF))) {
1116 /* device stops HSM for abort/error */
1117 qc->err_mask |= AC_ERR_DEV;
1119 /* If diagnostic failed and this is
1120 * IDENTIFY, it's likely a phantom
1121 * device. Mark hint.
1123 if (qc->dev->horkage &
1124 ATA_HORKAGE_DIAGNOSTIC)
1125 qc->err_mask |=
1126 AC_ERR_NODEV_HINT;
1127 } else {
1128 /* HSM violation. Let EH handle this.
1129 * Phantom devices also trigger this
1130 * condition. Mark hint.
1132 ata_ehi_push_desc(ehi, "ST-ATA: "
1133 "DRQ=0 without device error, "
1134 "dev_stat 0x%X", status);
1135 qc->err_mask |= AC_ERR_HSM |
1136 AC_ERR_NODEV_HINT;
1139 ap->hsm_task_state = HSM_ST_ERR;
1140 goto fsm_start;
1143 /* For PIO reads, some devices may ask for
1144 * data transfer (DRQ=1) alone with ERR=1.
1145 * We respect DRQ here and transfer one
1146 * block of junk data before changing the
1147 * hsm_task_state to HSM_ST_ERR.
1149 * For PIO writes, ERR=1 DRQ=1 doesn't make
1150 * sense since the data block has been
1151 * transferred to the device.
1153 if (unlikely(status & (ATA_ERR | ATA_DF))) {
1154 /* data might be corrputed */
1155 qc->err_mask |= AC_ERR_DEV;
1157 if (!(qc->tf.flags & ATA_TFLAG_WRITE)) {
1158 ata_pio_sectors(qc);
1159 status = ata_wait_idle(ap);
1162 if (status & (ATA_BUSY | ATA_DRQ)) {
1163 ata_ehi_push_desc(ehi, "ST-ATA: "
1164 "BUSY|DRQ persists on ERR|DF, "
1165 "dev_stat 0x%X", status);
1166 qc->err_mask |= AC_ERR_HSM;
1169 /* There are oddball controllers with
1170 * status register stuck at 0x7f and
1171 * lbal/m/h at zero which makes it
1172 * pass all other presence detection
1173 * mechanisms we have. Set NODEV_HINT
1174 * for it. Kernel bz#7241.
1176 if (status == 0x7f)
1177 qc->err_mask |= AC_ERR_NODEV_HINT;
1179 /* ata_pio_sectors() might change the
1180 * state to HSM_ST_LAST. so, the state
1181 * is changed after ata_pio_sectors().
1183 ap->hsm_task_state = HSM_ST_ERR;
1184 goto fsm_start;
1187 ata_pio_sectors(qc);
1189 if (ap->hsm_task_state == HSM_ST_LAST &&
1190 (!(qc->tf.flags & ATA_TFLAG_WRITE))) {
1191 /* all data read */
1192 status = ata_wait_idle(ap);
1193 goto fsm_start;
1197 poll_next = 1;
1198 break;
1200 case HSM_ST_LAST:
1201 if (unlikely(!ata_ok(status))) {
1202 qc->err_mask |= __ac_err_mask(status);
1203 ap->hsm_task_state = HSM_ST_ERR;
1204 goto fsm_start;
1207 /* no more data to transfer */
1208 DPRINTK("ata%u: dev %u command complete, drv_stat 0x%x\n",
1209 ap->print_id, qc->dev->devno, status);
1211 WARN_ON_ONCE(qc->err_mask & (AC_ERR_DEV | AC_ERR_HSM));
1213 ap->hsm_task_state = HSM_ST_IDLE;
1215 /* complete taskfile transaction */
1216 ata_hsm_qc_complete(qc, in_wq);
1218 poll_next = 0;
1219 break;
1221 case HSM_ST_ERR:
1222 ap->hsm_task_state = HSM_ST_IDLE;
1224 /* complete taskfile transaction */
1225 ata_hsm_qc_complete(qc, in_wq);
1227 poll_next = 0;
1228 break;
1229 default:
1230 poll_next = 0;
1231 WARN(true, "ata%d: SFF host state machine in invalid state %d",
1232 ap->print_id, ap->hsm_task_state);
1235 return poll_next;
1237 EXPORT_SYMBOL_GPL(ata_sff_hsm_move);
1239 void ata_sff_queue_work(struct work_struct *work)
1241 queue_work(ata_sff_wq, work);
1243 EXPORT_SYMBOL_GPL(ata_sff_queue_work);
1245 void ata_sff_queue_delayed_work(struct delayed_work *dwork, unsigned long delay)
1247 queue_delayed_work(ata_sff_wq, dwork, delay);
1249 EXPORT_SYMBOL_GPL(ata_sff_queue_delayed_work);
1251 void ata_sff_queue_pio_task(struct ata_link *link, unsigned long delay)
1253 struct ata_port *ap = link->ap;
1255 WARN_ON((ap->sff_pio_task_link != NULL) &&
1256 (ap->sff_pio_task_link != link));
1257 ap->sff_pio_task_link = link;
1259 /* may fail if ata_sff_flush_pio_task() in progress */
1260 ata_sff_queue_delayed_work(&ap->sff_pio_task, msecs_to_jiffies(delay));
1262 EXPORT_SYMBOL_GPL(ata_sff_queue_pio_task);
1264 void ata_sff_flush_pio_task(struct ata_port *ap)
1266 DPRINTK("ENTER\n");
1268 cancel_delayed_work_sync(&ap->sff_pio_task);
1271 * We wanna reset the HSM state to IDLE. If we do so without
1272 * grabbing the port lock, critical sections protected by it which
1273 * expect the HSM state to stay stable may get surprised. For
1274 * example, we may set IDLE in between the time
1275 * __ata_sff_port_intr() checks for HSM_ST_IDLE and before it calls
1276 * ata_sff_hsm_move() causing ata_sff_hsm_move() to BUG().
1278 spin_lock_irq(ap->lock);
1279 ap->hsm_task_state = HSM_ST_IDLE;
1280 spin_unlock_irq(ap->lock);
1282 ap->sff_pio_task_link = NULL;
1284 if (ata_msg_ctl(ap))
1285 ata_port_dbg(ap, "%s: EXIT\n", __func__);
1288 static void ata_sff_pio_task(struct work_struct *work)
1290 struct ata_port *ap =
1291 container_of(work, struct ata_port, sff_pio_task.work);
1292 struct ata_link *link = ap->sff_pio_task_link;
1293 struct ata_queued_cmd *qc;
1294 u8 status;
1295 int poll_next;
1297 spin_lock_irq(ap->lock);
1299 BUG_ON(ap->sff_pio_task_link == NULL);
1300 /* qc can be NULL if timeout occurred */
1301 qc = ata_qc_from_tag(ap, link->active_tag);
1302 if (!qc) {
1303 ap->sff_pio_task_link = NULL;
1304 goto out_unlock;
1307 fsm_start:
1308 WARN_ON_ONCE(ap->hsm_task_state == HSM_ST_IDLE);
1311 * This is purely heuristic. This is a fast path.
1312 * Sometimes when we enter, BSY will be cleared in
1313 * a chk-status or two. If not, the drive is probably seeking
1314 * or something. Snooze for a couple msecs, then
1315 * chk-status again. If still busy, queue delayed work.
1317 status = ata_sff_busy_wait(ap, ATA_BUSY, 5);
1318 if (status & ATA_BUSY) {
1319 spin_unlock_irq(ap->lock);
1320 ata_msleep(ap, 2);
1321 spin_lock_irq(ap->lock);
1323 status = ata_sff_busy_wait(ap, ATA_BUSY, 10);
1324 if (status & ATA_BUSY) {
1325 ata_sff_queue_pio_task(link, ATA_SHORT_PAUSE);
1326 goto out_unlock;
1331 * hsm_move() may trigger another command to be processed.
1332 * clean the link beforehand.
1334 ap->sff_pio_task_link = NULL;
1335 /* move the HSM */
1336 poll_next = ata_sff_hsm_move(ap, qc, status, 1);
1338 /* another command or interrupt handler
1339 * may be running at this point.
1341 if (poll_next)
1342 goto fsm_start;
1343 out_unlock:
1344 spin_unlock_irq(ap->lock);
1348 * ata_sff_qc_issue - issue taskfile to a SFF controller
1349 * @qc: command to issue to device
1351 * This function issues a PIO or NODATA command to a SFF
1352 * controller.
1354 * LOCKING:
1355 * spin_lock_irqsave(host lock)
1357 * RETURNS:
1358 * Zero on success, AC_ERR_* mask on failure
1360 unsigned int ata_sff_qc_issue(struct ata_queued_cmd *qc)
1362 struct ata_port *ap = qc->ap;
1363 struct ata_link *link = qc->dev->link;
1365 /* Use polling pio if the LLD doesn't handle
1366 * interrupt driven pio and atapi CDB interrupt.
1368 if (ap->flags & ATA_FLAG_PIO_POLLING)
1369 qc->tf.flags |= ATA_TFLAG_POLLING;
1371 /* select the device */
1372 ata_dev_select(ap, qc->dev->devno, 1, 0);
1374 /* start the command */
1375 switch (qc->tf.protocol) {
1376 case ATA_PROT_NODATA:
1377 if (qc->tf.flags & ATA_TFLAG_POLLING)
1378 ata_qc_set_polling(qc);
1380 ata_tf_to_host(ap, &qc->tf);
1381 ap->hsm_task_state = HSM_ST_LAST;
1383 if (qc->tf.flags & ATA_TFLAG_POLLING)
1384 ata_sff_queue_pio_task(link, 0);
1386 break;
1388 case ATA_PROT_PIO:
1389 if (qc->tf.flags & ATA_TFLAG_POLLING)
1390 ata_qc_set_polling(qc);
1392 ata_tf_to_host(ap, &qc->tf);
1394 if (qc->tf.flags & ATA_TFLAG_WRITE) {
1395 /* PIO data out protocol */
1396 ap->hsm_task_state = HSM_ST_FIRST;
1397 ata_sff_queue_pio_task(link, 0);
1399 /* always send first data block using the
1400 * ata_sff_pio_task() codepath.
1402 } else {
1403 /* PIO data in protocol */
1404 ap->hsm_task_state = HSM_ST;
1406 if (qc->tf.flags & ATA_TFLAG_POLLING)
1407 ata_sff_queue_pio_task(link, 0);
1409 /* if polling, ata_sff_pio_task() handles the
1410 * rest. otherwise, interrupt handler takes
1411 * over from here.
1415 break;
1417 case ATAPI_PROT_PIO:
1418 case ATAPI_PROT_NODATA:
1419 if (qc->tf.flags & ATA_TFLAG_POLLING)
1420 ata_qc_set_polling(qc);
1422 ata_tf_to_host(ap, &qc->tf);
1424 ap->hsm_task_state = HSM_ST_FIRST;
1426 /* send cdb by polling if no cdb interrupt */
1427 if ((!(qc->dev->flags & ATA_DFLAG_CDB_INTR)) ||
1428 (qc->tf.flags & ATA_TFLAG_POLLING))
1429 ata_sff_queue_pio_task(link, 0);
1430 break;
1432 default:
1433 return AC_ERR_SYSTEM;
1436 return 0;
1438 EXPORT_SYMBOL_GPL(ata_sff_qc_issue);
1441 * ata_sff_qc_fill_rtf - fill result TF using ->sff_tf_read
1442 * @qc: qc to fill result TF for
1444 * @qc is finished and result TF needs to be filled. Fill it
1445 * using ->sff_tf_read.
1447 * LOCKING:
1448 * spin_lock_irqsave(host lock)
1450 * RETURNS:
1451 * true indicating that result TF is successfully filled.
1453 bool ata_sff_qc_fill_rtf(struct ata_queued_cmd *qc)
1455 qc->ap->ops->sff_tf_read(qc->ap, &qc->result_tf);
1456 return true;
1458 EXPORT_SYMBOL_GPL(ata_sff_qc_fill_rtf);
1460 static unsigned int ata_sff_idle_irq(struct ata_port *ap)
1462 ap->stats.idle_irq++;
1464 #ifdef ATA_IRQ_TRAP
1465 if ((ap->stats.idle_irq % 1000) == 0) {
1466 ap->ops->sff_check_status(ap);
1467 if (ap->ops->sff_irq_clear)
1468 ap->ops->sff_irq_clear(ap);
1469 ata_port_warn(ap, "irq trap\n");
1470 return 1;
1472 #endif
1473 return 0; /* irq not handled */
1476 static unsigned int __ata_sff_port_intr(struct ata_port *ap,
1477 struct ata_queued_cmd *qc,
1478 bool hsmv_on_idle)
1480 u8 status;
1482 VPRINTK("ata%u: protocol %d task_state %d\n",
1483 ap->print_id, qc->tf.protocol, ap->hsm_task_state);
1485 /* Check whether we are expecting interrupt in this state */
1486 switch (ap->hsm_task_state) {
1487 case HSM_ST_FIRST:
1488 /* Some pre-ATAPI-4 devices assert INTRQ
1489 * at this state when ready to receive CDB.
1492 /* Check the ATA_DFLAG_CDB_INTR flag is enough here.
1493 * The flag was turned on only for atapi devices. No
1494 * need to check ata_is_atapi(qc->tf.protocol) again.
1496 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
1497 return ata_sff_idle_irq(ap);
1498 break;
1499 case HSM_ST_IDLE:
1500 return ata_sff_idle_irq(ap);
1501 default:
1502 break;
1505 /* check main status, clearing INTRQ if needed */
1506 status = ata_sff_irq_status(ap);
1507 if (status & ATA_BUSY) {
1508 if (hsmv_on_idle) {
1509 /* BMDMA engine is already stopped, we're screwed */
1510 qc->err_mask |= AC_ERR_HSM;
1511 ap->hsm_task_state = HSM_ST_ERR;
1512 } else
1513 return ata_sff_idle_irq(ap);
1516 /* clear irq events */
1517 if (ap->ops->sff_irq_clear)
1518 ap->ops->sff_irq_clear(ap);
1520 ata_sff_hsm_move(ap, qc, status, 0);
1522 return 1; /* irq handled */
1526 * ata_sff_port_intr - Handle SFF port interrupt
1527 * @ap: Port on which interrupt arrived (possibly...)
1528 * @qc: Taskfile currently active in engine
1530 * Handle port interrupt for given queued command.
1532 * LOCKING:
1533 * spin_lock_irqsave(host lock)
1535 * RETURNS:
1536 * One if interrupt was handled, zero if not (shared irq).
1538 unsigned int ata_sff_port_intr(struct ata_port *ap, struct ata_queued_cmd *qc)
1540 return __ata_sff_port_intr(ap, qc, false);
1542 EXPORT_SYMBOL_GPL(ata_sff_port_intr);
1544 static inline irqreturn_t __ata_sff_interrupt(int irq, void *dev_instance,
1545 unsigned int (*port_intr)(struct ata_port *, struct ata_queued_cmd *))
1547 struct ata_host *host = dev_instance;
1548 bool retried = false;
1549 unsigned int i;
1550 unsigned int handled, idle, polling;
1551 unsigned long flags;
1553 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
1554 spin_lock_irqsave(&host->lock, flags);
1556 retry:
1557 handled = idle = polling = 0;
1558 for (i = 0; i < host->n_ports; i++) {
1559 struct ata_port *ap = host->ports[i];
1560 struct ata_queued_cmd *qc;
1562 qc = ata_qc_from_tag(ap, ap->link.active_tag);
1563 if (qc) {
1564 if (!(qc->tf.flags & ATA_TFLAG_POLLING))
1565 handled |= port_intr(ap, qc);
1566 else
1567 polling |= 1 << i;
1568 } else
1569 idle |= 1 << i;
1573 * If no port was expecting IRQ but the controller is actually
1574 * asserting IRQ line, nobody cared will ensue. Check IRQ
1575 * pending status if available and clear spurious IRQ.
1577 if (!handled && !retried) {
1578 bool retry = false;
1580 for (i = 0; i < host->n_ports; i++) {
1581 struct ata_port *ap = host->ports[i];
1583 if (polling & (1 << i))
1584 continue;
1586 if (!ap->ops->sff_irq_check ||
1587 !ap->ops->sff_irq_check(ap))
1588 continue;
1590 if (idle & (1 << i)) {
1591 ap->ops->sff_check_status(ap);
1592 if (ap->ops->sff_irq_clear)
1593 ap->ops->sff_irq_clear(ap);
1594 } else {
1595 /* clear INTRQ and check if BUSY cleared */
1596 if (!(ap->ops->sff_check_status(ap) & ATA_BUSY))
1597 retry |= true;
1599 * With command in flight, we can't do
1600 * sff_irq_clear() w/o racing with completion.
1605 if (retry) {
1606 retried = true;
1607 goto retry;
1611 spin_unlock_irqrestore(&host->lock, flags);
1613 return IRQ_RETVAL(handled);
1617 * ata_sff_interrupt - Default SFF ATA host interrupt handler
1618 * @irq: irq line (unused)
1619 * @dev_instance: pointer to our ata_host information structure
1621 * Default interrupt handler for PCI IDE devices. Calls
1622 * ata_sff_port_intr() for each port that is not disabled.
1624 * LOCKING:
1625 * Obtains host lock during operation.
1627 * RETURNS:
1628 * IRQ_NONE or IRQ_HANDLED.
1630 irqreturn_t ata_sff_interrupt(int irq, void *dev_instance)
1632 return __ata_sff_interrupt(irq, dev_instance, ata_sff_port_intr);
1634 EXPORT_SYMBOL_GPL(ata_sff_interrupt);
1637 * ata_sff_lost_interrupt - Check for an apparent lost interrupt
1638 * @ap: port that appears to have timed out
1640 * Called from the libata error handlers when the core code suspects
1641 * an interrupt has been lost. If it has complete anything we can and
1642 * then return. Interface must support altstatus for this faster
1643 * recovery to occur.
1645 * Locking:
1646 * Caller holds host lock
1649 void ata_sff_lost_interrupt(struct ata_port *ap)
1651 u8 status;
1652 struct ata_queued_cmd *qc;
1654 /* Only one outstanding command per SFF channel */
1655 qc = ata_qc_from_tag(ap, ap->link.active_tag);
1656 /* We cannot lose an interrupt on a non-existent or polled command */
1657 if (!qc || qc->tf.flags & ATA_TFLAG_POLLING)
1658 return;
1659 /* See if the controller thinks it is still busy - if so the command
1660 isn't a lost IRQ but is still in progress */
1661 status = ata_sff_altstatus(ap);
1662 if (status & ATA_BUSY)
1663 return;
1665 /* There was a command running, we are no longer busy and we have
1666 no interrupt. */
1667 ata_port_warn(ap, "lost interrupt (Status 0x%x)\n",
1668 status);
1669 /* Run the host interrupt logic as if the interrupt had not been
1670 lost */
1671 ata_sff_port_intr(ap, qc);
1673 EXPORT_SYMBOL_GPL(ata_sff_lost_interrupt);
1676 * ata_sff_freeze - Freeze SFF controller port
1677 * @ap: port to freeze
1679 * Freeze SFF controller port.
1681 * LOCKING:
1682 * Inherited from caller.
1684 void ata_sff_freeze(struct ata_port *ap)
1686 ap->ctl |= ATA_NIEN;
1687 ap->last_ctl = ap->ctl;
1689 if (ap->ops->sff_set_devctl || ap->ioaddr.ctl_addr)
1690 ata_sff_set_devctl(ap, ap->ctl);
1692 /* Under certain circumstances, some controllers raise IRQ on
1693 * ATA_NIEN manipulation. Also, many controllers fail to mask
1694 * previously pending IRQ on ATA_NIEN assertion. Clear it.
1696 ap->ops->sff_check_status(ap);
1698 if (ap->ops->sff_irq_clear)
1699 ap->ops->sff_irq_clear(ap);
1701 EXPORT_SYMBOL_GPL(ata_sff_freeze);
1704 * ata_sff_thaw - Thaw SFF controller port
1705 * @ap: port to thaw
1707 * Thaw SFF controller port.
1709 * LOCKING:
1710 * Inherited from caller.
1712 void ata_sff_thaw(struct ata_port *ap)
1714 /* clear & re-enable interrupts */
1715 ap->ops->sff_check_status(ap);
1716 if (ap->ops->sff_irq_clear)
1717 ap->ops->sff_irq_clear(ap);
1718 ata_sff_irq_on(ap);
1720 EXPORT_SYMBOL_GPL(ata_sff_thaw);
1723 * ata_sff_prereset - prepare SFF link for reset
1724 * @link: SFF link to be reset
1725 * @deadline: deadline jiffies for the operation
1727 * SFF link @link is about to be reset. Initialize it. It first
1728 * calls ata_std_prereset() and wait for !BSY if the port is
1729 * being softreset.
1731 * LOCKING:
1732 * Kernel thread context (may sleep)
1734 * RETURNS:
1735 * 0 on success, -errno otherwise.
1737 int ata_sff_prereset(struct ata_link *link, unsigned long deadline)
1739 struct ata_eh_context *ehc = &link->eh_context;
1740 int rc;
1742 rc = ata_std_prereset(link, deadline);
1743 if (rc)
1744 return rc;
1746 /* if we're about to do hardreset, nothing more to do */
1747 if (ehc->i.action & ATA_EH_HARDRESET)
1748 return 0;
1750 /* wait for !BSY if we don't know that no device is attached */
1751 if (!ata_link_offline(link)) {
1752 rc = ata_sff_wait_ready(link, deadline);
1753 if (rc && rc != -ENODEV) {
1754 ata_link_warn(link,
1755 "device not ready (errno=%d), forcing hardreset\n",
1756 rc);
1757 ehc->i.action |= ATA_EH_HARDRESET;
1761 return 0;
1763 EXPORT_SYMBOL_GPL(ata_sff_prereset);
1766 * ata_devchk - PATA device presence detection
1767 * @ap: ATA channel to examine
1768 * @device: Device to examine (starting at zero)
1770 * This technique was originally described in
1771 * Hale Landis's ATADRVR (www.ata-atapi.com), and
1772 * later found its way into the ATA/ATAPI spec.
1774 * Write a pattern to the ATA shadow registers,
1775 * and if a device is present, it will respond by
1776 * correctly storing and echoing back the
1777 * ATA shadow register contents.
1779 * LOCKING:
1780 * caller.
1782 static unsigned int ata_devchk(struct ata_port *ap, unsigned int device)
1784 struct ata_ioports *ioaddr = &ap->ioaddr;
1785 u8 nsect, lbal;
1787 ap->ops->sff_dev_select(ap, device);
1789 iowrite8(0x55, ioaddr->nsect_addr);
1790 iowrite8(0xaa, ioaddr->lbal_addr);
1792 iowrite8(0xaa, ioaddr->nsect_addr);
1793 iowrite8(0x55, ioaddr->lbal_addr);
1795 iowrite8(0x55, ioaddr->nsect_addr);
1796 iowrite8(0xaa, ioaddr->lbal_addr);
1798 nsect = ioread8(ioaddr->nsect_addr);
1799 lbal = ioread8(ioaddr->lbal_addr);
1801 if ((nsect == 0x55) && (lbal == 0xaa))
1802 return 1; /* we found a device */
1804 return 0; /* nothing found */
1808 * ata_sff_dev_classify - Parse returned ATA device signature
1809 * @dev: ATA device to classify (starting at zero)
1810 * @present: device seems present
1811 * @r_err: Value of error register on completion
1813 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
1814 * an ATA/ATAPI-defined set of values is placed in the ATA
1815 * shadow registers, indicating the results of device detection
1816 * and diagnostics.
1818 * Select the ATA device, and read the values from the ATA shadow
1819 * registers. Then parse according to the Error register value,
1820 * and the spec-defined values examined by ata_dev_classify().
1822 * LOCKING:
1823 * caller.
1825 * RETURNS:
1826 * Device type - %ATA_DEV_ATA, %ATA_DEV_ATAPI or %ATA_DEV_NONE.
1828 unsigned int ata_sff_dev_classify(struct ata_device *dev, int present,
1829 u8 *r_err)
1831 struct ata_port *ap = dev->link->ap;
1832 struct ata_taskfile tf;
1833 unsigned int class;
1834 u8 err;
1836 ap->ops->sff_dev_select(ap, dev->devno);
1838 memset(&tf, 0, sizeof(tf));
1840 ap->ops->sff_tf_read(ap, &tf);
1841 err = tf.feature;
1842 if (r_err)
1843 *r_err = err;
1845 /* see if device passed diags: continue and warn later */
1846 if (err == 0)
1847 /* diagnostic fail : do nothing _YET_ */
1848 dev->horkage |= ATA_HORKAGE_DIAGNOSTIC;
1849 else if (err == 1)
1850 /* do nothing */ ;
1851 else if ((dev->devno == 0) && (err == 0x81))
1852 /* do nothing */ ;
1853 else
1854 return ATA_DEV_NONE;
1856 /* determine if device is ATA or ATAPI */
1857 class = ata_dev_classify(&tf);
1859 if (class == ATA_DEV_UNKNOWN) {
1860 /* If the device failed diagnostic, it's likely to
1861 * have reported incorrect device signature too.
1862 * Assume ATA device if the device seems present but
1863 * device signature is invalid with diagnostic
1864 * failure.
1866 if (present && (dev->horkage & ATA_HORKAGE_DIAGNOSTIC))
1867 class = ATA_DEV_ATA;
1868 else
1869 class = ATA_DEV_NONE;
1870 } else if ((class == ATA_DEV_ATA) &&
1871 (ap->ops->sff_check_status(ap) == 0))
1872 class = ATA_DEV_NONE;
1874 return class;
1876 EXPORT_SYMBOL_GPL(ata_sff_dev_classify);
1879 * ata_sff_wait_after_reset - wait for devices to become ready after reset
1880 * @link: SFF link which is just reset
1881 * @devmask: mask of present devices
1882 * @deadline: deadline jiffies for the operation
1884 * Wait devices attached to SFF @link to become ready after
1885 * reset. It contains preceding 150ms wait to avoid accessing TF
1886 * status register too early.
1888 * LOCKING:
1889 * Kernel thread context (may sleep).
1891 * RETURNS:
1892 * 0 on success, -ENODEV if some or all of devices in @devmask
1893 * don't seem to exist. -errno on other errors.
1895 int ata_sff_wait_after_reset(struct ata_link *link, unsigned int devmask,
1896 unsigned long deadline)
1898 struct ata_port *ap = link->ap;
1899 struct ata_ioports *ioaddr = &ap->ioaddr;
1900 unsigned int dev0 = devmask & (1 << 0);
1901 unsigned int dev1 = devmask & (1 << 1);
1902 int rc, ret = 0;
1904 ata_msleep(ap, ATA_WAIT_AFTER_RESET);
1906 /* always check readiness of the master device */
1907 rc = ata_sff_wait_ready(link, deadline);
1908 /* -ENODEV means the odd clown forgot the D7 pulldown resistor
1909 * and TF status is 0xff, bail out on it too.
1911 if (rc)
1912 return rc;
1914 /* if device 1 was found in ata_devchk, wait for register
1915 * access briefly, then wait for BSY to clear.
1917 if (dev1) {
1918 int i;
1920 ap->ops->sff_dev_select(ap, 1);
1922 /* Wait for register access. Some ATAPI devices fail
1923 * to set nsect/lbal after reset, so don't waste too
1924 * much time on it. We're gonna wait for !BSY anyway.
1926 for (i = 0; i < 2; i++) {
1927 u8 nsect, lbal;
1929 nsect = ioread8(ioaddr->nsect_addr);
1930 lbal = ioread8(ioaddr->lbal_addr);
1931 if ((nsect == 1) && (lbal == 1))
1932 break;
1933 ata_msleep(ap, 50); /* give drive a breather */
1936 rc = ata_sff_wait_ready(link, deadline);
1937 if (rc) {
1938 if (rc != -ENODEV)
1939 return rc;
1940 ret = rc;
1944 /* is all this really necessary? */
1945 ap->ops->sff_dev_select(ap, 0);
1946 if (dev1)
1947 ap->ops->sff_dev_select(ap, 1);
1948 if (dev0)
1949 ap->ops->sff_dev_select(ap, 0);
1951 return ret;
1953 EXPORT_SYMBOL_GPL(ata_sff_wait_after_reset);
1955 static int ata_bus_softreset(struct ata_port *ap, unsigned int devmask,
1956 unsigned long deadline)
1958 struct ata_ioports *ioaddr = &ap->ioaddr;
1960 DPRINTK("ata%u: bus reset via SRST\n", ap->print_id);
1962 if (ap->ioaddr.ctl_addr) {
1963 /* software reset. causes dev0 to be selected */
1964 iowrite8(ap->ctl, ioaddr->ctl_addr);
1965 udelay(20); /* FIXME: flush */
1966 iowrite8(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
1967 udelay(20); /* FIXME: flush */
1968 iowrite8(ap->ctl, ioaddr->ctl_addr);
1969 ap->last_ctl = ap->ctl;
1972 /* wait the port to become ready */
1973 return ata_sff_wait_after_reset(&ap->link, devmask, deadline);
1977 * ata_sff_softreset - reset host port via ATA SRST
1978 * @link: ATA link to reset
1979 * @classes: resulting classes of attached devices
1980 * @deadline: deadline jiffies for the operation
1982 * Reset host port using ATA SRST.
1984 * LOCKING:
1985 * Kernel thread context (may sleep)
1987 * RETURNS:
1988 * 0 on success, -errno otherwise.
1990 int ata_sff_softreset(struct ata_link *link, unsigned int *classes,
1991 unsigned long deadline)
1993 struct ata_port *ap = link->ap;
1994 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
1995 unsigned int devmask = 0;
1996 int rc;
1997 u8 err;
1999 DPRINTK("ENTER\n");
2001 /* determine if device 0/1 are present */
2002 if (ata_devchk(ap, 0))
2003 devmask |= (1 << 0);
2004 if (slave_possible && ata_devchk(ap, 1))
2005 devmask |= (1 << 1);
2007 /* select device 0 again */
2008 ap->ops->sff_dev_select(ap, 0);
2010 /* issue bus reset */
2011 DPRINTK("about to softreset, devmask=%x\n", devmask);
2012 rc = ata_bus_softreset(ap, devmask, deadline);
2013 /* if link is occupied, -ENODEV too is an error */
2014 if (rc && (rc != -ENODEV || sata_scr_valid(link))) {
2015 ata_link_err(link, "SRST failed (errno=%d)\n", rc);
2016 return rc;
2019 /* determine by signature whether we have ATA or ATAPI devices */
2020 classes[0] = ata_sff_dev_classify(&link->device[0],
2021 devmask & (1 << 0), &err);
2022 if (slave_possible && err != 0x81)
2023 classes[1] = ata_sff_dev_classify(&link->device[1],
2024 devmask & (1 << 1), &err);
2026 DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]);
2027 return 0;
2029 EXPORT_SYMBOL_GPL(ata_sff_softreset);
2032 * sata_sff_hardreset - reset host port via SATA phy reset
2033 * @link: link to reset
2034 * @class: resulting class of attached device
2035 * @deadline: deadline jiffies for the operation
2037 * SATA phy-reset host port using DET bits of SControl register,
2038 * wait for !BSY and classify the attached device.
2040 * LOCKING:
2041 * Kernel thread context (may sleep)
2043 * RETURNS:
2044 * 0 on success, -errno otherwise.
2046 int sata_sff_hardreset(struct ata_link *link, unsigned int *class,
2047 unsigned long deadline)
2049 struct ata_eh_context *ehc = &link->eh_context;
2050 const unsigned long *timing = sata_ehc_deb_timing(ehc);
2051 bool online;
2052 int rc;
2054 rc = sata_link_hardreset(link, timing, deadline, &online,
2055 ata_sff_check_ready);
2056 if (online)
2057 *class = ata_sff_dev_classify(link->device, 1, NULL);
2059 DPRINTK("EXIT, class=%u\n", *class);
2060 return rc;
2062 EXPORT_SYMBOL_GPL(sata_sff_hardreset);
2065 * ata_sff_postreset - SFF postreset callback
2066 * @link: the target SFF ata_link
2067 * @classes: classes of attached devices
2069 * This function is invoked after a successful reset. It first
2070 * calls ata_std_postreset() and performs SFF specific postreset
2071 * processing.
2073 * LOCKING:
2074 * Kernel thread context (may sleep)
2076 void ata_sff_postreset(struct ata_link *link, unsigned int *classes)
2078 struct ata_port *ap = link->ap;
2080 ata_std_postreset(link, classes);
2082 /* is double-select really necessary? */
2083 if (classes[0] != ATA_DEV_NONE)
2084 ap->ops->sff_dev_select(ap, 1);
2085 if (classes[1] != ATA_DEV_NONE)
2086 ap->ops->sff_dev_select(ap, 0);
2088 /* bail out if no device is present */
2089 if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
2090 DPRINTK("EXIT, no device\n");
2091 return;
2094 /* set up device control */
2095 if (ap->ops->sff_set_devctl || ap->ioaddr.ctl_addr) {
2096 ata_sff_set_devctl(ap, ap->ctl);
2097 ap->last_ctl = ap->ctl;
2100 EXPORT_SYMBOL_GPL(ata_sff_postreset);
2103 * ata_sff_drain_fifo - Stock FIFO drain logic for SFF controllers
2104 * @qc: command
2106 * Drain the FIFO and device of any stuck data following a command
2107 * failing to complete. In some cases this is necessary before a
2108 * reset will recover the device.
2112 void ata_sff_drain_fifo(struct ata_queued_cmd *qc)
2114 int count;
2115 struct ata_port *ap;
2117 /* We only need to flush incoming data when a command was running */
2118 if (qc == NULL || qc->dma_dir == DMA_TO_DEVICE)
2119 return;
2121 ap = qc->ap;
2122 /* Drain up to 64K of data before we give up this recovery method */
2123 for (count = 0; (ap->ops->sff_check_status(ap) & ATA_DRQ)
2124 && count < 65536; count += 2)
2125 ioread16(ap->ioaddr.data_addr);
2127 /* Can become DEBUG later */
2128 if (count)
2129 ata_port_dbg(ap, "drained %d bytes to clear DRQ\n", count);
2132 EXPORT_SYMBOL_GPL(ata_sff_drain_fifo);
2135 * ata_sff_error_handler - Stock error handler for SFF controller
2136 * @ap: port to handle error for
2138 * Stock error handler for SFF controller. It can handle both
2139 * PATA and SATA controllers. Many controllers should be able to
2140 * use this EH as-is or with some added handling before and
2141 * after.
2143 * LOCKING:
2144 * Kernel thread context (may sleep)
2146 void ata_sff_error_handler(struct ata_port *ap)
2148 ata_reset_fn_t softreset = ap->ops->softreset;
2149 ata_reset_fn_t hardreset = ap->ops->hardreset;
2150 struct ata_queued_cmd *qc;
2151 unsigned long flags;
2153 qc = __ata_qc_from_tag(ap, ap->link.active_tag);
2154 if (qc && !(qc->flags & ATA_QCFLAG_FAILED))
2155 qc = NULL;
2157 spin_lock_irqsave(ap->lock, flags);
2160 * We *MUST* do FIFO draining before we issue a reset as
2161 * several devices helpfully clear their internal state and
2162 * will lock solid if we touch the data port post reset. Pass
2163 * qc in case anyone wants to do different PIO/DMA recovery or
2164 * has per command fixups
2166 if (ap->ops->sff_drain_fifo)
2167 ap->ops->sff_drain_fifo(qc);
2169 spin_unlock_irqrestore(ap->lock, flags);
2171 /* ignore built-in hardresets if SCR access is not available */
2172 if ((hardreset == sata_std_hardreset ||
2173 hardreset == sata_sff_hardreset) && !sata_scr_valid(&ap->link))
2174 hardreset = NULL;
2176 ata_do_eh(ap, ap->ops->prereset, softreset, hardreset,
2177 ap->ops->postreset);
2179 EXPORT_SYMBOL_GPL(ata_sff_error_handler);
2182 * ata_sff_std_ports - initialize ioaddr with standard port offsets.
2183 * @ioaddr: IO address structure to be initialized
2185 * Utility function which initializes data_addr, error_addr,
2186 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
2187 * device_addr, status_addr, and command_addr to standard offsets
2188 * relative to cmd_addr.
2190 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
2192 void ata_sff_std_ports(struct ata_ioports *ioaddr)
2194 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
2195 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
2196 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
2197 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
2198 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
2199 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
2200 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
2201 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
2202 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
2203 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
2205 EXPORT_SYMBOL_GPL(ata_sff_std_ports);
2207 #ifdef CONFIG_PCI
2209 static int ata_resources_present(struct pci_dev *pdev, int port)
2211 int i;
2213 /* Check the PCI resources for this channel are enabled */
2214 port = port * 2;
2215 for (i = 0; i < 2; i++) {
2216 if (pci_resource_start(pdev, port + i) == 0 ||
2217 pci_resource_len(pdev, port + i) == 0)
2218 return 0;
2220 return 1;
2224 * ata_pci_sff_init_host - acquire native PCI ATA resources and init host
2225 * @host: target ATA host
2227 * Acquire native PCI ATA resources for @host and initialize the
2228 * first two ports of @host accordingly. Ports marked dummy are
2229 * skipped and allocation failure makes the port dummy.
2231 * Note that native PCI resources are valid even for legacy hosts
2232 * as we fix up pdev resources array early in boot, so this
2233 * function can be used for both native and legacy SFF hosts.
2235 * LOCKING:
2236 * Inherited from calling layer (may sleep).
2238 * RETURNS:
2239 * 0 if at least one port is initialized, -ENODEV if no port is
2240 * available.
2242 int ata_pci_sff_init_host(struct ata_host *host)
2244 struct device *gdev = host->dev;
2245 struct pci_dev *pdev = to_pci_dev(gdev);
2246 unsigned int mask = 0;
2247 int i, rc;
2249 /* request, iomap BARs and init port addresses accordingly */
2250 for (i = 0; i < 2; i++) {
2251 struct ata_port *ap = host->ports[i];
2252 int base = i * 2;
2253 void __iomem * const *iomap;
2255 if (ata_port_is_dummy(ap))
2256 continue;
2258 /* Discard disabled ports. Some controllers show
2259 * their unused channels this way. Disabled ports are
2260 * made dummy.
2262 if (!ata_resources_present(pdev, i)) {
2263 ap->ops = &ata_dummy_port_ops;
2264 continue;
2267 rc = pcim_iomap_regions(pdev, 0x3 << base,
2268 dev_driver_string(gdev));
2269 if (rc) {
2270 dev_warn(gdev,
2271 "failed to request/iomap BARs for port %d (errno=%d)\n",
2272 i, rc);
2273 if (rc == -EBUSY)
2274 pcim_pin_device(pdev);
2275 ap->ops = &ata_dummy_port_ops;
2276 continue;
2278 host->iomap = iomap = pcim_iomap_table(pdev);
2280 ap->ioaddr.cmd_addr = iomap[base];
2281 ap->ioaddr.altstatus_addr =
2282 ap->ioaddr.ctl_addr = (void __iomem *)
2283 ((unsigned long)iomap[base + 1] | ATA_PCI_CTL_OFS);
2284 ata_sff_std_ports(&ap->ioaddr);
2286 ata_port_desc(ap, "cmd 0x%llx ctl 0x%llx",
2287 (unsigned long long)pci_resource_start(pdev, base),
2288 (unsigned long long)pci_resource_start(pdev, base + 1));
2290 mask |= 1 << i;
2293 if (!mask) {
2294 dev_err(gdev, "no available native port\n");
2295 return -ENODEV;
2298 return 0;
2300 EXPORT_SYMBOL_GPL(ata_pci_sff_init_host);
2303 * ata_pci_sff_prepare_host - helper to prepare PCI PIO-only SFF ATA host
2304 * @pdev: target PCI device
2305 * @ppi: array of port_info, must be enough for two ports
2306 * @r_host: out argument for the initialized ATA host
2308 * Helper to allocate PIO-only SFF ATA host for @pdev, acquire
2309 * all PCI resources and initialize it accordingly in one go.
2311 * LOCKING:
2312 * Inherited from calling layer (may sleep).
2314 * RETURNS:
2315 * 0 on success, -errno otherwise.
2317 int ata_pci_sff_prepare_host(struct pci_dev *pdev,
2318 const struct ata_port_info * const *ppi,
2319 struct ata_host **r_host)
2321 struct ata_host *host;
2322 int rc;
2324 if (!devres_open_group(&pdev->dev, NULL, GFP_KERNEL))
2325 return -ENOMEM;
2327 host = ata_host_alloc_pinfo(&pdev->dev, ppi, 2);
2328 if (!host) {
2329 dev_err(&pdev->dev, "failed to allocate ATA host\n");
2330 rc = -ENOMEM;
2331 goto err_out;
2334 rc = ata_pci_sff_init_host(host);
2335 if (rc)
2336 goto err_out;
2338 devres_remove_group(&pdev->dev, NULL);
2339 *r_host = host;
2340 return 0;
2342 err_out:
2343 devres_release_group(&pdev->dev, NULL);
2344 return rc;
2346 EXPORT_SYMBOL_GPL(ata_pci_sff_prepare_host);
2349 * ata_pci_sff_activate_host - start SFF host, request IRQ and register it
2350 * @host: target SFF ATA host
2351 * @irq_handler: irq_handler used when requesting IRQ(s)
2352 * @sht: scsi_host_template to use when registering the host
2354 * This is the counterpart of ata_host_activate() for SFF ATA
2355 * hosts. This separate helper is necessary because SFF hosts
2356 * use two separate interrupts in legacy mode.
2358 * LOCKING:
2359 * Inherited from calling layer (may sleep).
2361 * RETURNS:
2362 * 0 on success, -errno otherwise.
2364 int ata_pci_sff_activate_host(struct ata_host *host,
2365 irq_handler_t irq_handler,
2366 struct scsi_host_template *sht)
2368 struct device *dev = host->dev;
2369 struct pci_dev *pdev = to_pci_dev(dev);
2370 const char *drv_name = dev_driver_string(host->dev);
2371 int legacy_mode = 0, rc;
2373 rc = ata_host_start(host);
2374 if (rc)
2375 return rc;
2377 if ((pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
2378 u8 tmp8, mask = 0;
2381 * ATA spec says we should use legacy mode when one
2382 * port is in legacy mode, but disabled ports on some
2383 * PCI hosts appear as fixed legacy ports, e.g SB600/700
2384 * on which the secondary port is not wired, so
2385 * ignore ports that are marked as 'dummy' during
2386 * this check
2388 pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
2389 if (!ata_port_is_dummy(host->ports[0]))
2390 mask |= (1 << 0);
2391 if (!ata_port_is_dummy(host->ports[1]))
2392 mask |= (1 << 2);
2393 if ((tmp8 & mask) != mask)
2394 legacy_mode = 1;
2397 if (!devres_open_group(dev, NULL, GFP_KERNEL))
2398 return -ENOMEM;
2400 if (!legacy_mode && pdev->irq) {
2401 int i;
2403 rc = devm_request_irq(dev, pdev->irq, irq_handler,
2404 IRQF_SHARED, drv_name, host);
2405 if (rc)
2406 goto out;
2408 for (i = 0; i < 2; i++) {
2409 if (ata_port_is_dummy(host->ports[i]))
2410 continue;
2411 ata_port_desc(host->ports[i], "irq %d", pdev->irq);
2413 } else if (legacy_mode) {
2414 if (!ata_port_is_dummy(host->ports[0])) {
2415 rc = devm_request_irq(dev, ATA_PRIMARY_IRQ(pdev),
2416 irq_handler, IRQF_SHARED,
2417 drv_name, host);
2418 if (rc)
2419 goto out;
2421 ata_port_desc(host->ports[0], "irq %d",
2422 ATA_PRIMARY_IRQ(pdev));
2425 if (!ata_port_is_dummy(host->ports[1])) {
2426 rc = devm_request_irq(dev, ATA_SECONDARY_IRQ(pdev),
2427 irq_handler, IRQF_SHARED,
2428 drv_name, host);
2429 if (rc)
2430 goto out;
2432 ata_port_desc(host->ports[1], "irq %d",
2433 ATA_SECONDARY_IRQ(pdev));
2437 rc = ata_host_register(host, sht);
2438 out:
2439 if (rc == 0)
2440 devres_remove_group(dev, NULL);
2441 else
2442 devres_release_group(dev, NULL);
2444 return rc;
2446 EXPORT_SYMBOL_GPL(ata_pci_sff_activate_host);
2448 static const struct ata_port_info *ata_sff_find_valid_pi(
2449 const struct ata_port_info * const *ppi)
2451 int i;
2453 /* look up the first valid port_info */
2454 for (i = 0; i < 2 && ppi[i]; i++)
2455 if (ppi[i]->port_ops != &ata_dummy_port_ops)
2456 return ppi[i];
2458 return NULL;
2461 static int ata_pci_init_one(struct pci_dev *pdev,
2462 const struct ata_port_info * const *ppi,
2463 struct scsi_host_template *sht, void *host_priv,
2464 int hflags, bool bmdma)
2466 struct device *dev = &pdev->dev;
2467 const struct ata_port_info *pi;
2468 struct ata_host *host = NULL;
2469 int rc;
2471 DPRINTK("ENTER\n");
2473 pi = ata_sff_find_valid_pi(ppi);
2474 if (!pi) {
2475 dev_err(&pdev->dev, "no valid port_info specified\n");
2476 return -EINVAL;
2479 if (!devres_open_group(dev, NULL, GFP_KERNEL))
2480 return -ENOMEM;
2482 rc = pcim_enable_device(pdev);
2483 if (rc)
2484 goto out;
2486 #ifdef CONFIG_ATA_BMDMA
2487 if (bmdma)
2488 /* prepare and activate BMDMA host */
2489 rc = ata_pci_bmdma_prepare_host(pdev, ppi, &host);
2490 else
2491 #endif
2492 /* prepare and activate SFF host */
2493 rc = ata_pci_sff_prepare_host(pdev, ppi, &host);
2494 if (rc)
2495 goto out;
2496 host->private_data = host_priv;
2497 host->flags |= hflags;
2499 #ifdef CONFIG_ATA_BMDMA
2500 if (bmdma) {
2501 pci_set_master(pdev);
2502 rc = ata_pci_sff_activate_host(host, ata_bmdma_interrupt, sht);
2503 } else
2504 #endif
2505 rc = ata_pci_sff_activate_host(host, ata_sff_interrupt, sht);
2506 out:
2507 if (rc == 0)
2508 devres_remove_group(&pdev->dev, NULL);
2509 else
2510 devres_release_group(&pdev->dev, NULL);
2512 return rc;
2516 * ata_pci_sff_init_one - Initialize/register PIO-only PCI IDE controller
2517 * @pdev: Controller to be initialized
2518 * @ppi: array of port_info, must be enough for two ports
2519 * @sht: scsi_host_template to use when registering the host
2520 * @host_priv: host private_data
2521 * @hflag: host flags
2523 * This is a helper function which can be called from a driver's
2524 * xxx_init_one() probe function if the hardware uses traditional
2525 * IDE taskfile registers and is PIO only.
2527 * ASSUMPTION:
2528 * Nobody makes a single channel controller that appears solely as
2529 * the secondary legacy port on PCI.
2531 * LOCKING:
2532 * Inherited from PCI layer (may sleep).
2534 * RETURNS:
2535 * Zero on success, negative on errno-based value on error.
2537 int ata_pci_sff_init_one(struct pci_dev *pdev,
2538 const struct ata_port_info * const *ppi,
2539 struct scsi_host_template *sht, void *host_priv, int hflag)
2541 return ata_pci_init_one(pdev, ppi, sht, host_priv, hflag, 0);
2543 EXPORT_SYMBOL_GPL(ata_pci_sff_init_one);
2545 #endif /* CONFIG_PCI */
2548 * BMDMA support
2551 #ifdef CONFIG_ATA_BMDMA
2553 const struct ata_port_operations ata_bmdma_port_ops = {
2554 .inherits = &ata_sff_port_ops,
2556 .error_handler = ata_bmdma_error_handler,
2557 .post_internal_cmd = ata_bmdma_post_internal_cmd,
2559 .qc_prep = ata_bmdma_qc_prep,
2560 .qc_issue = ata_bmdma_qc_issue,
2562 .sff_irq_clear = ata_bmdma_irq_clear,
2563 .bmdma_setup = ata_bmdma_setup,
2564 .bmdma_start = ata_bmdma_start,
2565 .bmdma_stop = ata_bmdma_stop,
2566 .bmdma_status = ata_bmdma_status,
2568 .port_start = ata_bmdma_port_start,
2570 EXPORT_SYMBOL_GPL(ata_bmdma_port_ops);
2572 const struct ata_port_operations ata_bmdma32_port_ops = {
2573 .inherits = &ata_bmdma_port_ops,
2575 .sff_data_xfer = ata_sff_data_xfer32,
2576 .port_start = ata_bmdma_port_start32,
2578 EXPORT_SYMBOL_GPL(ata_bmdma32_port_ops);
2581 * ata_bmdma_fill_sg - Fill PCI IDE PRD table
2582 * @qc: Metadata associated with taskfile to be transferred
2584 * Fill PCI IDE PRD (scatter-gather) table with segments
2585 * associated with the current disk command.
2587 * LOCKING:
2588 * spin_lock_irqsave(host lock)
2591 static void ata_bmdma_fill_sg(struct ata_queued_cmd *qc)
2593 struct ata_port *ap = qc->ap;
2594 struct ata_bmdma_prd *prd = ap->bmdma_prd;
2595 struct scatterlist *sg;
2596 unsigned int si, pi;
2598 pi = 0;
2599 for_each_sg(qc->sg, sg, qc->n_elem, si) {
2600 u32 addr, offset;
2601 u32 sg_len, len;
2603 /* determine if physical DMA addr spans 64K boundary.
2604 * Note h/w doesn't support 64-bit, so we unconditionally
2605 * truncate dma_addr_t to u32.
2607 addr = (u32) sg_dma_address(sg);
2608 sg_len = sg_dma_len(sg);
2610 while (sg_len) {
2611 offset = addr & 0xffff;
2612 len = sg_len;
2613 if ((offset + sg_len) > 0x10000)
2614 len = 0x10000 - offset;
2616 prd[pi].addr = cpu_to_le32(addr);
2617 prd[pi].flags_len = cpu_to_le32(len & 0xffff);
2618 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", pi, addr, len);
2620 pi++;
2621 sg_len -= len;
2622 addr += len;
2626 prd[pi - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2630 * ata_bmdma_fill_sg_dumb - Fill PCI IDE PRD table
2631 * @qc: Metadata associated with taskfile to be transferred
2633 * Fill PCI IDE PRD (scatter-gather) table with segments
2634 * associated with the current disk command. Perform the fill
2635 * so that we avoid writing any length 64K records for
2636 * controllers that don't follow the spec.
2638 * LOCKING:
2639 * spin_lock_irqsave(host lock)
2642 static void ata_bmdma_fill_sg_dumb(struct ata_queued_cmd *qc)
2644 struct ata_port *ap = qc->ap;
2645 struct ata_bmdma_prd *prd = ap->bmdma_prd;
2646 struct scatterlist *sg;
2647 unsigned int si, pi;
2649 pi = 0;
2650 for_each_sg(qc->sg, sg, qc->n_elem, si) {
2651 u32 addr, offset;
2652 u32 sg_len, len, blen;
2654 /* determine if physical DMA addr spans 64K boundary.
2655 * Note h/w doesn't support 64-bit, so we unconditionally
2656 * truncate dma_addr_t to u32.
2658 addr = (u32) sg_dma_address(sg);
2659 sg_len = sg_dma_len(sg);
2661 while (sg_len) {
2662 offset = addr & 0xffff;
2663 len = sg_len;
2664 if ((offset + sg_len) > 0x10000)
2665 len = 0x10000 - offset;
2667 blen = len & 0xffff;
2668 prd[pi].addr = cpu_to_le32(addr);
2669 if (blen == 0) {
2670 /* Some PATA chipsets like the CS5530 can't
2671 cope with 0x0000 meaning 64K as the spec
2672 says */
2673 prd[pi].flags_len = cpu_to_le32(0x8000);
2674 blen = 0x8000;
2675 prd[++pi].addr = cpu_to_le32(addr + 0x8000);
2677 prd[pi].flags_len = cpu_to_le32(blen);
2678 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", pi, addr, len);
2680 pi++;
2681 sg_len -= len;
2682 addr += len;
2686 prd[pi - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2690 * ata_bmdma_qc_prep - Prepare taskfile for submission
2691 * @qc: Metadata associated with taskfile to be prepared
2693 * Prepare ATA taskfile for submission.
2695 * LOCKING:
2696 * spin_lock_irqsave(host lock)
2698 void ata_bmdma_qc_prep(struct ata_queued_cmd *qc)
2700 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2701 return;
2703 ata_bmdma_fill_sg(qc);
2705 EXPORT_SYMBOL_GPL(ata_bmdma_qc_prep);
2708 * ata_bmdma_dumb_qc_prep - Prepare taskfile for submission
2709 * @qc: Metadata associated with taskfile to be prepared
2711 * Prepare ATA taskfile for submission.
2713 * LOCKING:
2714 * spin_lock_irqsave(host lock)
2716 void ata_bmdma_dumb_qc_prep(struct ata_queued_cmd *qc)
2718 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2719 return;
2721 ata_bmdma_fill_sg_dumb(qc);
2723 EXPORT_SYMBOL_GPL(ata_bmdma_dumb_qc_prep);
2726 * ata_bmdma_qc_issue - issue taskfile to a BMDMA controller
2727 * @qc: command to issue to device
2729 * This function issues a PIO, NODATA or DMA command to a
2730 * SFF/BMDMA controller. PIO and NODATA are handled by
2731 * ata_sff_qc_issue().
2733 * LOCKING:
2734 * spin_lock_irqsave(host lock)
2736 * RETURNS:
2737 * Zero on success, AC_ERR_* mask on failure
2739 unsigned int ata_bmdma_qc_issue(struct ata_queued_cmd *qc)
2741 struct ata_port *ap = qc->ap;
2742 struct ata_link *link = qc->dev->link;
2744 /* defer PIO handling to sff_qc_issue */
2745 if (!ata_is_dma(qc->tf.protocol))
2746 return ata_sff_qc_issue(qc);
2748 /* select the device */
2749 ata_dev_select(ap, qc->dev->devno, 1, 0);
2751 /* start the command */
2752 switch (qc->tf.protocol) {
2753 case ATA_PROT_DMA:
2754 WARN_ON_ONCE(qc->tf.flags & ATA_TFLAG_POLLING);
2756 ap->ops->sff_tf_load(ap, &qc->tf); /* load tf registers */
2757 ap->ops->bmdma_setup(qc); /* set up bmdma */
2758 ap->ops->bmdma_start(qc); /* initiate bmdma */
2759 ap->hsm_task_state = HSM_ST_LAST;
2760 break;
2762 case ATAPI_PROT_DMA:
2763 WARN_ON_ONCE(qc->tf.flags & ATA_TFLAG_POLLING);
2765 ap->ops->sff_tf_load(ap, &qc->tf); /* load tf registers */
2766 ap->ops->bmdma_setup(qc); /* set up bmdma */
2767 ap->hsm_task_state = HSM_ST_FIRST;
2769 /* send cdb by polling if no cdb interrupt */
2770 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
2771 ata_sff_queue_pio_task(link, 0);
2772 break;
2774 default:
2775 WARN_ON(1);
2776 return AC_ERR_SYSTEM;
2779 return 0;
2781 EXPORT_SYMBOL_GPL(ata_bmdma_qc_issue);
2784 * ata_bmdma_port_intr - Handle BMDMA port interrupt
2785 * @ap: Port on which interrupt arrived (possibly...)
2786 * @qc: Taskfile currently active in engine
2788 * Handle port interrupt for given queued command.
2790 * LOCKING:
2791 * spin_lock_irqsave(host lock)
2793 * RETURNS:
2794 * One if interrupt was handled, zero if not (shared irq).
2796 unsigned int ata_bmdma_port_intr(struct ata_port *ap, struct ata_queued_cmd *qc)
2798 struct ata_eh_info *ehi = &ap->link.eh_info;
2799 u8 host_stat = 0;
2800 bool bmdma_stopped = false;
2801 unsigned int handled;
2803 if (ap->hsm_task_state == HSM_ST_LAST && ata_is_dma(qc->tf.protocol)) {
2804 /* check status of DMA engine */
2805 host_stat = ap->ops->bmdma_status(ap);
2806 VPRINTK("ata%u: host_stat 0x%X\n", ap->print_id, host_stat);
2808 /* if it's not our irq... */
2809 if (!(host_stat & ATA_DMA_INTR))
2810 return ata_sff_idle_irq(ap);
2812 /* before we do anything else, clear DMA-Start bit */
2813 ap->ops->bmdma_stop(qc);
2814 bmdma_stopped = true;
2816 if (unlikely(host_stat & ATA_DMA_ERR)) {
2817 /* error when transferring data to/from memory */
2818 qc->err_mask |= AC_ERR_HOST_BUS;
2819 ap->hsm_task_state = HSM_ST_ERR;
2823 handled = __ata_sff_port_intr(ap, qc, bmdma_stopped);
2825 if (unlikely(qc->err_mask) && ata_is_dma(qc->tf.protocol))
2826 ata_ehi_push_desc(ehi, "BMDMA stat 0x%x", host_stat);
2828 return handled;
2830 EXPORT_SYMBOL_GPL(ata_bmdma_port_intr);
2833 * ata_bmdma_interrupt - Default BMDMA ATA host interrupt handler
2834 * @irq: irq line (unused)
2835 * @dev_instance: pointer to our ata_host information structure
2837 * Default interrupt handler for PCI IDE devices. Calls
2838 * ata_bmdma_port_intr() for each port that is not disabled.
2840 * LOCKING:
2841 * Obtains host lock during operation.
2843 * RETURNS:
2844 * IRQ_NONE or IRQ_HANDLED.
2846 irqreturn_t ata_bmdma_interrupt(int irq, void *dev_instance)
2848 return __ata_sff_interrupt(irq, dev_instance, ata_bmdma_port_intr);
2850 EXPORT_SYMBOL_GPL(ata_bmdma_interrupt);
2853 * ata_bmdma_error_handler - Stock error handler for BMDMA controller
2854 * @ap: port to handle error for
2856 * Stock error handler for BMDMA controller. It can handle both
2857 * PATA and SATA controllers. Most BMDMA controllers should be
2858 * able to use this EH as-is or with some added handling before
2859 * and after.
2861 * LOCKING:
2862 * Kernel thread context (may sleep)
2864 void ata_bmdma_error_handler(struct ata_port *ap)
2866 struct ata_queued_cmd *qc;
2867 unsigned long flags;
2868 bool thaw = false;
2870 qc = __ata_qc_from_tag(ap, ap->link.active_tag);
2871 if (qc && !(qc->flags & ATA_QCFLAG_FAILED))
2872 qc = NULL;
2874 /* reset PIO HSM and stop DMA engine */
2875 spin_lock_irqsave(ap->lock, flags);
2877 if (qc && ata_is_dma(qc->tf.protocol)) {
2878 u8 host_stat;
2880 host_stat = ap->ops->bmdma_status(ap);
2882 /* BMDMA controllers indicate host bus error by
2883 * setting DMA_ERR bit and timing out. As it wasn't
2884 * really a timeout event, adjust error mask and
2885 * cancel frozen state.
2887 if (qc->err_mask == AC_ERR_TIMEOUT && (host_stat & ATA_DMA_ERR)) {
2888 qc->err_mask = AC_ERR_HOST_BUS;
2889 thaw = true;
2892 ap->ops->bmdma_stop(qc);
2894 /* if we're gonna thaw, make sure IRQ is clear */
2895 if (thaw) {
2896 ap->ops->sff_check_status(ap);
2897 if (ap->ops->sff_irq_clear)
2898 ap->ops->sff_irq_clear(ap);
2902 spin_unlock_irqrestore(ap->lock, flags);
2904 if (thaw)
2905 ata_eh_thaw_port(ap);
2907 ata_sff_error_handler(ap);
2909 EXPORT_SYMBOL_GPL(ata_bmdma_error_handler);
2912 * ata_bmdma_post_internal_cmd - Stock post_internal_cmd for BMDMA
2913 * @qc: internal command to clean up
2915 * LOCKING:
2916 * Kernel thread context (may sleep)
2918 void ata_bmdma_post_internal_cmd(struct ata_queued_cmd *qc)
2920 struct ata_port *ap = qc->ap;
2921 unsigned long flags;
2923 if (ata_is_dma(qc->tf.protocol)) {
2924 spin_lock_irqsave(ap->lock, flags);
2925 ap->ops->bmdma_stop(qc);
2926 spin_unlock_irqrestore(ap->lock, flags);
2929 EXPORT_SYMBOL_GPL(ata_bmdma_post_internal_cmd);
2932 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
2933 * @ap: Port associated with this ATA transaction.
2935 * Clear interrupt and error flags in DMA status register.
2937 * May be used as the irq_clear() entry in ata_port_operations.
2939 * LOCKING:
2940 * spin_lock_irqsave(host lock)
2942 void ata_bmdma_irq_clear(struct ata_port *ap)
2944 void __iomem *mmio = ap->ioaddr.bmdma_addr;
2946 if (!mmio)
2947 return;
2949 iowrite8(ioread8(mmio + ATA_DMA_STATUS), mmio + ATA_DMA_STATUS);
2951 EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
2954 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
2955 * @qc: Info associated with this ATA transaction.
2957 * LOCKING:
2958 * spin_lock_irqsave(host lock)
2960 void ata_bmdma_setup(struct ata_queued_cmd *qc)
2962 struct ata_port *ap = qc->ap;
2963 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
2964 u8 dmactl;
2966 /* load PRD table addr. */
2967 mb(); /* make sure PRD table writes are visible to controller */
2968 iowrite32(ap->bmdma_prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
2970 /* specify data direction, triple-check start bit is clear */
2971 dmactl = ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
2972 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
2973 if (!rw)
2974 dmactl |= ATA_DMA_WR;
2975 iowrite8(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
2977 /* issue r/w command */
2978 ap->ops->sff_exec_command(ap, &qc->tf);
2980 EXPORT_SYMBOL_GPL(ata_bmdma_setup);
2983 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
2984 * @qc: Info associated with this ATA transaction.
2986 * LOCKING:
2987 * spin_lock_irqsave(host lock)
2989 void ata_bmdma_start(struct ata_queued_cmd *qc)
2991 struct ata_port *ap = qc->ap;
2992 u8 dmactl;
2994 /* start host DMA transaction */
2995 dmactl = ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
2996 iowrite8(dmactl | ATA_DMA_START, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
2998 /* Strictly, one may wish to issue an ioread8() here, to
2999 * flush the mmio write. However, control also passes
3000 * to the hardware at this point, and it will interrupt
3001 * us when we are to resume control. So, in effect,
3002 * we don't care when the mmio write flushes.
3003 * Further, a read of the DMA status register _immediately_
3004 * following the write may not be what certain flaky hardware
3005 * is expected, so I think it is best to not add a readb()
3006 * without first all the MMIO ATA cards/mobos.
3007 * Or maybe I'm just being paranoid.
3009 * FIXME: The posting of this write means I/O starts are
3010 * unnecessarily delayed for MMIO
3013 EXPORT_SYMBOL_GPL(ata_bmdma_start);
3016 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
3017 * @qc: Command we are ending DMA for
3019 * Clears the ATA_DMA_START flag in the dma control register
3021 * May be used as the bmdma_stop() entry in ata_port_operations.
3023 * LOCKING:
3024 * spin_lock_irqsave(host lock)
3026 void ata_bmdma_stop(struct ata_queued_cmd *qc)
3028 struct ata_port *ap = qc->ap;
3029 void __iomem *mmio = ap->ioaddr.bmdma_addr;
3031 /* clear start/stop bit */
3032 iowrite8(ioread8(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
3033 mmio + ATA_DMA_CMD);
3035 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
3036 ata_sff_dma_pause(ap);
3038 EXPORT_SYMBOL_GPL(ata_bmdma_stop);
3041 * ata_bmdma_status - Read PCI IDE BMDMA status
3042 * @ap: Port associated with this ATA transaction.
3044 * Read and return BMDMA status register.
3046 * May be used as the bmdma_status() entry in ata_port_operations.
3048 * LOCKING:
3049 * spin_lock_irqsave(host lock)
3051 u8 ata_bmdma_status(struct ata_port *ap)
3053 return ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
3055 EXPORT_SYMBOL_GPL(ata_bmdma_status);
3059 * ata_bmdma_port_start - Set port up for bmdma.
3060 * @ap: Port to initialize
3062 * Called just after data structures for each port are
3063 * initialized. Allocates space for PRD table.
3065 * May be used as the port_start() entry in ata_port_operations.
3067 * LOCKING:
3068 * Inherited from caller.
3070 int ata_bmdma_port_start(struct ata_port *ap)
3072 if (ap->mwdma_mask || ap->udma_mask) {
3073 ap->bmdma_prd =
3074 dmam_alloc_coherent(ap->host->dev, ATA_PRD_TBL_SZ,
3075 &ap->bmdma_prd_dma, GFP_KERNEL);
3076 if (!ap->bmdma_prd)
3077 return -ENOMEM;
3080 return 0;
3082 EXPORT_SYMBOL_GPL(ata_bmdma_port_start);
3085 * ata_bmdma_port_start32 - Set port up for dma.
3086 * @ap: Port to initialize
3088 * Called just after data structures for each port are
3089 * initialized. Enables 32bit PIO and allocates space for PRD
3090 * table.
3092 * May be used as the port_start() entry in ata_port_operations for
3093 * devices that are capable of 32bit PIO.
3095 * LOCKING:
3096 * Inherited from caller.
3098 int ata_bmdma_port_start32(struct ata_port *ap)
3100 ap->pflags |= ATA_PFLAG_PIO32 | ATA_PFLAG_PIO32CHANGE;
3101 return ata_bmdma_port_start(ap);
3103 EXPORT_SYMBOL_GPL(ata_bmdma_port_start32);
3105 #ifdef CONFIG_PCI
3108 * ata_pci_bmdma_clear_simplex - attempt to kick device out of simplex
3109 * @pdev: PCI device
3111 * Some PCI ATA devices report simplex mode but in fact can be told to
3112 * enter non simplex mode. This implements the necessary logic to
3113 * perform the task on such devices. Calling it on other devices will
3114 * have -undefined- behaviour.
3116 int ata_pci_bmdma_clear_simplex(struct pci_dev *pdev)
3118 unsigned long bmdma = pci_resource_start(pdev, 4);
3119 u8 simplex;
3121 if (bmdma == 0)
3122 return -ENOENT;
3124 simplex = inb(bmdma + 0x02);
3125 outb(simplex & 0x60, bmdma + 0x02);
3126 simplex = inb(bmdma + 0x02);
3127 if (simplex & 0x80)
3128 return -EOPNOTSUPP;
3129 return 0;
3131 EXPORT_SYMBOL_GPL(ata_pci_bmdma_clear_simplex);
3133 static void ata_bmdma_nodma(struct ata_host *host, const char *reason)
3135 int i;
3137 dev_err(host->dev, "BMDMA: %s, falling back to PIO\n", reason);
3139 for (i = 0; i < 2; i++) {
3140 host->ports[i]->mwdma_mask = 0;
3141 host->ports[i]->udma_mask = 0;
3146 * ata_pci_bmdma_init - acquire PCI BMDMA resources and init ATA host
3147 * @host: target ATA host
3149 * Acquire PCI BMDMA resources and initialize @host accordingly.
3151 * LOCKING:
3152 * Inherited from calling layer (may sleep).
3154 void ata_pci_bmdma_init(struct ata_host *host)
3156 struct device *gdev = host->dev;
3157 struct pci_dev *pdev = to_pci_dev(gdev);
3158 int i, rc;
3160 /* No BAR4 allocation: No DMA */
3161 if (pci_resource_start(pdev, 4) == 0) {
3162 ata_bmdma_nodma(host, "BAR4 is zero");
3163 return;
3167 * Some controllers require BMDMA region to be initialized
3168 * even if DMA is not in use to clear IRQ status via
3169 * ->sff_irq_clear method. Try to initialize bmdma_addr
3170 * regardless of dma masks.
3172 rc = dma_set_mask(&pdev->dev, ATA_DMA_MASK);
3173 if (rc)
3174 ata_bmdma_nodma(host, "failed to set dma mask");
3175 if (!rc) {
3176 rc = dma_set_coherent_mask(&pdev->dev, ATA_DMA_MASK);
3177 if (rc)
3178 ata_bmdma_nodma(host,
3179 "failed to set consistent dma mask");
3182 /* request and iomap DMA region */
3183 rc = pcim_iomap_regions(pdev, 1 << 4, dev_driver_string(gdev));
3184 if (rc) {
3185 ata_bmdma_nodma(host, "failed to request/iomap BAR4");
3186 return;
3188 host->iomap = pcim_iomap_table(pdev);
3190 for (i = 0; i < 2; i++) {
3191 struct ata_port *ap = host->ports[i];
3192 void __iomem *bmdma = host->iomap[4] + 8 * i;
3194 if (ata_port_is_dummy(ap))
3195 continue;
3197 ap->ioaddr.bmdma_addr = bmdma;
3198 if ((!(ap->flags & ATA_FLAG_IGN_SIMPLEX)) &&
3199 (ioread8(bmdma + 2) & 0x80))
3200 host->flags |= ATA_HOST_SIMPLEX;
3202 ata_port_desc(ap, "bmdma 0x%llx",
3203 (unsigned long long)pci_resource_start(pdev, 4) + 8 * i);
3206 EXPORT_SYMBOL_GPL(ata_pci_bmdma_init);
3209 * ata_pci_bmdma_prepare_host - helper to prepare PCI BMDMA ATA host
3210 * @pdev: target PCI device
3211 * @ppi: array of port_info, must be enough for two ports
3212 * @r_host: out argument for the initialized ATA host
3214 * Helper to allocate BMDMA ATA host for @pdev, acquire all PCI
3215 * resources and initialize it accordingly in one go.
3217 * LOCKING:
3218 * Inherited from calling layer (may sleep).
3220 * RETURNS:
3221 * 0 on success, -errno otherwise.
3223 int ata_pci_bmdma_prepare_host(struct pci_dev *pdev,
3224 const struct ata_port_info * const * ppi,
3225 struct ata_host **r_host)
3227 int rc;
3229 rc = ata_pci_sff_prepare_host(pdev, ppi, r_host);
3230 if (rc)
3231 return rc;
3233 ata_pci_bmdma_init(*r_host);
3234 return 0;
3236 EXPORT_SYMBOL_GPL(ata_pci_bmdma_prepare_host);
3239 * ata_pci_bmdma_init_one - Initialize/register BMDMA PCI IDE controller
3240 * @pdev: Controller to be initialized
3241 * @ppi: array of port_info, must be enough for two ports
3242 * @sht: scsi_host_template to use when registering the host
3243 * @host_priv: host private_data
3244 * @hflags: host flags
3246 * This function is similar to ata_pci_sff_init_one() but also
3247 * takes care of BMDMA initialization.
3249 * LOCKING:
3250 * Inherited from PCI layer (may sleep).
3252 * RETURNS:
3253 * Zero on success, negative on errno-based value on error.
3255 int ata_pci_bmdma_init_one(struct pci_dev *pdev,
3256 const struct ata_port_info * const * ppi,
3257 struct scsi_host_template *sht, void *host_priv,
3258 int hflags)
3260 return ata_pci_init_one(pdev, ppi, sht, host_priv, hflags, 1);
3262 EXPORT_SYMBOL_GPL(ata_pci_bmdma_init_one);
3264 #endif /* CONFIG_PCI */
3265 #endif /* CONFIG_ATA_BMDMA */
3268 * ata_sff_port_init - Initialize SFF/BMDMA ATA port
3269 * @ap: Port to initialize
3271 * Called on port allocation to initialize SFF/BMDMA specific
3272 * fields.
3274 * LOCKING:
3275 * None.
3277 void ata_sff_port_init(struct ata_port *ap)
3279 INIT_DELAYED_WORK(&ap->sff_pio_task, ata_sff_pio_task);
3280 ap->ctl = ATA_DEVCTL_OBS;
3281 ap->last_ctl = 0xFF;
3284 int __init ata_sff_init(void)
3286 ata_sff_wq = alloc_workqueue("ata_sff", WQ_MEM_RECLAIM, WQ_MAX_ACTIVE);
3287 if (!ata_sff_wq)
3288 return -ENOMEM;
3290 return 0;
3293 void ata_sff_exit(void)
3295 destroy_workqueue(ata_sff_wq);