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