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