blob: b58549fac4603df3c6dc7b356c3d8568b041fa8d [file] [log] [blame]
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05001/*
Dave Jonesf3a03b02007-07-16 11:23:03 -04002 * libata-sff.c - helper library for PCI IDE BMDMA
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05003 *
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 Garzik1fdffbc2006-02-09 05:15:27 -050035#include <linux/kernel.h>
36#include <linux/pci.h>
37#include <linux/libata.h>
Tejun Heo624d5c52008-03-25 22:16:41 +090038#include <linux/highmem.h>
Jeff Garzik1fdffbc2006-02-09 05:15:27 -050039
40#include "libata.h"
41
Tejun Heo624d5c52008-03-25 22:16:41 +090042const struct ata_port_operations ata_sff_port_ops = {
43 .inherits = &ata_base_port_ops,
44
Tejun Heo9363c382008-04-07 22:47:16 +090045 .qc_prep = ata_sff_qc_prep,
46 .qc_issue = ata_sff_qc_issue,
Tejun Heo4c9bf4e2008-04-07 22:47:20 +090047 .qc_fill_rtf = ata_sff_qc_fill_rtf,
Tejun Heo624d5c52008-03-25 22:16:41 +090048
Tejun Heo9363c382008-04-07 22:47:16 +090049 .freeze = ata_sff_freeze,
50 .thaw = ata_sff_thaw,
Tejun Heo0aa11132008-04-07 22:47:18 +090051 .prereset = ata_sff_prereset,
Tejun Heo9363c382008-04-07 22:47:16 +090052 .softreset = ata_sff_softreset,
Tejun Heo57c9efd2008-04-07 22:47:19 +090053 .hardreset = sata_sff_hardreset,
Tejun Heo203c75b2008-04-07 22:47:18 +090054 .postreset = ata_sff_postreset,
Tejun Heo9363c382008-04-07 22:47:16 +090055 .error_handler = ata_sff_error_handler,
56 .post_internal_cmd = ata_sff_post_internal_cmd,
Tejun Heo624d5c52008-03-25 22:16:41 +090057
Tejun Heo5682ed32008-04-07 22:47:16 +090058 .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 Heo288623a2008-04-07 22:47:17 +090065 .sff_irq_clear = ata_sff_irq_clear,
Tejun Heo624d5c52008-03-25 22:16:41 +090066
67 .port_start = ata_sff_port_start,
68};
69
70const struct ata_port_operations ata_bmdma_port_ops = {
71 .inherits = &ata_sff_port_ops,
72
Tejun Heo9363c382008-04-07 22:47:16 +090073 .mode_filter = ata_bmdma_mode_filter,
Tejun Heo624d5c52008-03-25 22:16:41 +090074
75 .bmdma_setup = ata_bmdma_setup,
76 .bmdma_start = ata_bmdma_start,
77 .bmdma_stop = ata_bmdma_stop,
78 .bmdma_status = ata_bmdma_status,
Tejun Heo624d5c52008-03-25 22:16:41 +090079};
80
Alan Cox871af122009-01-05 14:16:39 +000081const struct ata_port_operations ata_bmdma32_port_ops = {
82 .inherits = &ata_bmdma_port_ops,
83
84 .sff_data_xfer = ata_sff_data_xfer32,
85};
86EXPORT_SYMBOL_GPL(ata_bmdma32_port_ops);
87
Tejun Heo624d5c52008-03-25 22:16:41 +090088/**
89 * ata_fill_sg - Fill PCI IDE PRD table
90 * @qc: Metadata associated with taskfile to be transferred
91 *
92 * Fill PCI IDE PRD (scatter-gather) table with segments
93 * associated with the current disk command.
94 *
95 * LOCKING:
96 * spin_lock_irqsave(host lock)
97 *
98 */
99static void ata_fill_sg(struct ata_queued_cmd *qc)
100{
101 struct ata_port *ap = qc->ap;
102 struct scatterlist *sg;
103 unsigned int si, pi;
104
105 pi = 0;
106 for_each_sg(qc->sg, sg, qc->n_elem, si) {
107 u32 addr, offset;
108 u32 sg_len, len;
109
110 /* determine if physical DMA addr spans 64K boundary.
111 * Note h/w doesn't support 64-bit, so we unconditionally
112 * truncate dma_addr_t to u32.
113 */
114 addr = (u32) sg_dma_address(sg);
115 sg_len = sg_dma_len(sg);
116
117 while (sg_len) {
118 offset = addr & 0xffff;
119 len = sg_len;
120 if ((offset + sg_len) > 0x10000)
121 len = 0x10000 - offset;
122
123 ap->prd[pi].addr = cpu_to_le32(addr);
124 ap->prd[pi].flags_len = cpu_to_le32(len & 0xffff);
125 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", pi, addr, len);
126
127 pi++;
128 sg_len -= len;
129 addr += len;
130 }
131 }
132
133 ap->prd[pi - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
134}
135
136/**
137 * ata_fill_sg_dumb - Fill PCI IDE PRD table
138 * @qc: Metadata associated with taskfile to be transferred
139 *
140 * Fill PCI IDE PRD (scatter-gather) table with segments
141 * associated with the current disk command. Perform the fill
142 * so that we avoid writing any length 64K records for
143 * controllers that don't follow the spec.
144 *
145 * LOCKING:
146 * spin_lock_irqsave(host lock)
147 *
148 */
149static void ata_fill_sg_dumb(struct ata_queued_cmd *qc)
150{
151 struct ata_port *ap = qc->ap;
152 struct scatterlist *sg;
153 unsigned int si, pi;
154
155 pi = 0;
156 for_each_sg(qc->sg, sg, qc->n_elem, si) {
157 u32 addr, offset;
158 u32 sg_len, len, blen;
159
160 /* determine if physical DMA addr spans 64K boundary.
161 * Note h/w doesn't support 64-bit, so we unconditionally
162 * truncate dma_addr_t to u32.
163 */
164 addr = (u32) sg_dma_address(sg);
165 sg_len = sg_dma_len(sg);
166
167 while (sg_len) {
168 offset = addr & 0xffff;
169 len = sg_len;
170 if ((offset + sg_len) > 0x10000)
171 len = 0x10000 - offset;
172
173 blen = len & 0xffff;
174 ap->prd[pi].addr = cpu_to_le32(addr);
175 if (blen == 0) {
176 /* Some PATA chipsets like the CS5530 can't
177 cope with 0x0000 meaning 64K as the spec says */
178 ap->prd[pi].flags_len = cpu_to_le32(0x8000);
179 blen = 0x8000;
180 ap->prd[++pi].addr = cpu_to_le32(addr + 0x8000);
181 }
182 ap->prd[pi].flags_len = cpu_to_le32(blen);
183 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", pi, addr, len);
184
185 pi++;
186 sg_len -= len;
187 addr += len;
188 }
189 }
190
191 ap->prd[pi - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
192}
193
194/**
Tejun Heo9363c382008-04-07 22:47:16 +0900195 * ata_sff_qc_prep - Prepare taskfile for submission
Tejun Heo624d5c52008-03-25 22:16:41 +0900196 * @qc: Metadata associated with taskfile to be prepared
197 *
198 * Prepare ATA taskfile for submission.
199 *
200 * LOCKING:
201 * spin_lock_irqsave(host lock)
202 */
Tejun Heo9363c382008-04-07 22:47:16 +0900203void ata_sff_qc_prep(struct ata_queued_cmd *qc)
Tejun Heo624d5c52008-03-25 22:16:41 +0900204{
205 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
206 return;
207
208 ata_fill_sg(qc);
209}
210
211/**
Tejun Heo9363c382008-04-07 22:47:16 +0900212 * ata_sff_dumb_qc_prep - Prepare taskfile for submission
Tejun Heo624d5c52008-03-25 22:16:41 +0900213 * @qc: Metadata associated with taskfile to be prepared
214 *
215 * Prepare ATA taskfile for submission.
216 *
217 * LOCKING:
218 * spin_lock_irqsave(host lock)
219 */
Tejun Heo9363c382008-04-07 22:47:16 +0900220void ata_sff_dumb_qc_prep(struct ata_queued_cmd *qc)
Tejun Heo624d5c52008-03-25 22:16:41 +0900221{
222 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
223 return;
224
225 ata_fill_sg_dumb(qc);
226}
227
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500228/**
Tejun Heo9363c382008-04-07 22:47:16 +0900229 * ata_sff_check_status - Read device status reg & clear interrupt
Tejun Heo272f7882008-03-25 22:16:40 +0900230 * @ap: port where the device is
231 *
232 * Reads ATA taskfile status register for currently-selected device
233 * and return its value. This also clears pending interrupts
234 * from this device
235 *
236 * LOCKING:
237 * Inherited from caller.
238 */
Tejun Heo9363c382008-04-07 22:47:16 +0900239u8 ata_sff_check_status(struct ata_port *ap)
Tejun Heo272f7882008-03-25 22:16:40 +0900240{
241 return ioread8(ap->ioaddr.status_addr);
242}
243
244/**
Tejun Heo9363c382008-04-07 22:47:16 +0900245 * ata_sff_altstatus - Read device alternate status reg
Tejun Heo272f7882008-03-25 22:16:40 +0900246 * @ap: port where the device is
247 *
248 * Reads ATA taskfile alternate status register for
249 * currently-selected device and return its value.
250 *
251 * Note: may NOT be used as the check_altstatus() entry in
252 * ata_port_operations.
253 *
254 * LOCKING:
255 * Inherited from caller.
256 */
Alan Coxa57c1ba2008-05-29 22:10:58 +0100257static u8 ata_sff_altstatus(struct ata_port *ap)
Tejun Heo272f7882008-03-25 22:16:40 +0900258{
Tejun Heo5682ed32008-04-07 22:47:16 +0900259 if (ap->ops->sff_check_altstatus)
260 return ap->ops->sff_check_altstatus(ap);
Tejun Heo272f7882008-03-25 22:16:40 +0900261
262 return ioread8(ap->ioaddr.altstatus_addr);
263}
264
265/**
Alan Coxa57c1ba2008-05-29 22:10:58 +0100266 * ata_sff_irq_status - Check if the device is busy
267 * @ap: port where the device is
268 *
269 * Determine if the port is currently busy. Uses altstatus
270 * if available in order to avoid clearing shared IRQ status
271 * when finding an IRQ source. Non ctl capable devices don't
272 * share interrupt lines fortunately for us.
273 *
274 * LOCKING:
275 * Inherited from caller.
276 */
277static u8 ata_sff_irq_status(struct ata_port *ap)
278{
279 u8 status;
280
281 if (ap->ops->sff_check_altstatus || ap->ioaddr.altstatus_addr) {
282 status = ata_sff_altstatus(ap);
283 /* Not us: We are busy */
284 if (status & ATA_BUSY)
285 return status;
286 }
287 /* Clear INTRQ latch */
Hugh Dickins6311c902008-06-05 14:44:39 +0100288 status = ap->ops->sff_check_status(ap);
Alan Coxa57c1ba2008-05-29 22:10:58 +0100289 return status;
290}
291
292/**
293 * ata_sff_sync - Flush writes
294 * @ap: Port to wait for.
295 *
296 * CAUTION:
297 * If we have an mmio device with no ctl and no altstatus
298 * method this will fail. No such devices are known to exist.
299 *
300 * LOCKING:
301 * Inherited from caller.
302 */
303
304static void ata_sff_sync(struct ata_port *ap)
305{
306 if (ap->ops->sff_check_altstatus)
307 ap->ops->sff_check_altstatus(ap);
308 else if (ap->ioaddr.altstatus_addr)
309 ioread8(ap->ioaddr.altstatus_addr);
310}
311
312/**
313 * ata_sff_pause - Flush writes and wait 400nS
314 * @ap: Port to pause for.
315 *
316 * CAUTION:
317 * If we have an mmio device with no ctl and no altstatus
318 * method this will fail. No such devices are known to exist.
319 *
320 * LOCKING:
321 * Inherited from caller.
322 */
323
324void ata_sff_pause(struct ata_port *ap)
325{
326 ata_sff_sync(ap);
327 ndelay(400);
328}
329
330/**
331 * ata_sff_dma_pause - Pause before commencing DMA
332 * @ap: Port to pause for.
333 *
334 * Perform I/O fencing and ensure sufficient cycle delays occur
335 * for the HDMA1:0 transition
336 */
337
338void ata_sff_dma_pause(struct ata_port *ap)
339{
340 if (ap->ops->sff_check_altstatus || ap->ioaddr.altstatus_addr) {
341 /* An altstatus read will cause the needed delay without
342 messing up the IRQ status */
343 ata_sff_altstatus(ap);
344 return;
345 }
346 /* There are no DMA controllers without ctl. BUG here to ensure
347 we never violate the HDMA1:0 transition timing and risk
348 corruption. */
349 BUG();
350}
351
352/**
Tejun Heo9363c382008-04-07 22:47:16 +0900353 * ata_sff_busy_sleep - sleep until BSY clears, or timeout
Tejun Heo624d5c52008-03-25 22:16:41 +0900354 * @ap: port containing status register to be polled
Tejun Heo341c2c92008-05-20 02:17:51 +0900355 * @tmout_pat: impatience timeout in msecs
356 * @tmout: overall timeout in msecs
Tejun Heo624d5c52008-03-25 22:16:41 +0900357 *
358 * Sleep until ATA Status register bit BSY clears,
359 * or a timeout occurs.
360 *
361 * LOCKING:
362 * Kernel thread context (may sleep).
363 *
364 * RETURNS:
365 * 0 on success, -errno otherwise.
366 */
Tejun Heo9363c382008-04-07 22:47:16 +0900367int ata_sff_busy_sleep(struct ata_port *ap,
368 unsigned long tmout_pat, unsigned long tmout)
Tejun Heo624d5c52008-03-25 22:16:41 +0900369{
370 unsigned long timer_start, timeout;
371 u8 status;
372
Tejun Heo9363c382008-04-07 22:47:16 +0900373 status = ata_sff_busy_wait(ap, ATA_BUSY, 300);
Tejun Heo624d5c52008-03-25 22:16:41 +0900374 timer_start = jiffies;
Tejun Heo341c2c92008-05-20 02:17:51 +0900375 timeout = ata_deadline(timer_start, tmout_pat);
Tejun Heo624d5c52008-03-25 22:16:41 +0900376 while (status != 0xff && (status & ATA_BUSY) &&
377 time_before(jiffies, timeout)) {
378 msleep(50);
Tejun Heo9363c382008-04-07 22:47:16 +0900379 status = ata_sff_busy_wait(ap, ATA_BUSY, 3);
Tejun Heo624d5c52008-03-25 22:16:41 +0900380 }
381
382 if (status != 0xff && (status & ATA_BUSY))
383 ata_port_printk(ap, KERN_WARNING,
384 "port is slow to respond, please be patient "
385 "(Status 0x%x)\n", status);
386
Tejun Heo341c2c92008-05-20 02:17:51 +0900387 timeout = ata_deadline(timer_start, tmout);
Tejun Heo624d5c52008-03-25 22:16:41 +0900388 while (status != 0xff && (status & ATA_BUSY) &&
389 time_before(jiffies, timeout)) {
390 msleep(50);
Tejun Heo5682ed32008-04-07 22:47:16 +0900391 status = ap->ops->sff_check_status(ap);
Tejun Heo624d5c52008-03-25 22:16:41 +0900392 }
393
394 if (status == 0xff)
395 return -ENODEV;
396
397 if (status & ATA_BUSY) {
398 ata_port_printk(ap, KERN_ERR, "port failed to respond "
399 "(%lu secs, Status 0x%x)\n",
Tejun Heo341c2c92008-05-20 02:17:51 +0900400 DIV_ROUND_UP(tmout, 1000), status);
Tejun Heo624d5c52008-03-25 22:16:41 +0900401 return -EBUSY;
402 }
403
404 return 0;
405}
406
Tejun Heoaa2731a2008-04-07 22:47:19 +0900407static int ata_sff_check_ready(struct ata_link *link)
408{
409 u8 status = link->ap->ops->sff_check_status(link->ap);
410
Tejun Heo78ab88f2008-05-01 23:41:41 +0900411 return ata_check_ready(status);
Tejun Heoaa2731a2008-04-07 22:47:19 +0900412}
413
Tejun Heo624d5c52008-03-25 22:16:41 +0900414/**
Tejun Heo9363c382008-04-07 22:47:16 +0900415 * ata_sff_wait_ready - sleep until BSY clears, or timeout
Tejun Heo705e76b2008-04-07 22:47:19 +0900416 * @link: SFF link to wait ready status for
Tejun Heo624d5c52008-03-25 22:16:41 +0900417 * @deadline: deadline jiffies for the operation
418 *
419 * Sleep until ATA Status register bit BSY clears, or timeout
420 * occurs.
421 *
422 * LOCKING:
423 * Kernel thread context (may sleep).
424 *
425 * RETURNS:
426 * 0 on success, -errno otherwise.
427 */
Tejun Heo705e76b2008-04-07 22:47:19 +0900428int ata_sff_wait_ready(struct ata_link *link, unsigned long deadline)
Tejun Heo624d5c52008-03-25 22:16:41 +0900429{
Tejun Heoaa2731a2008-04-07 22:47:19 +0900430 return ata_wait_ready(link, deadline, ata_sff_check_ready);
Tejun Heo624d5c52008-03-25 22:16:41 +0900431}
432
433/**
Tejun Heo9363c382008-04-07 22:47:16 +0900434 * ata_sff_dev_select - Select device 0/1 on ATA bus
Tejun Heo624d5c52008-03-25 22:16:41 +0900435 * @ap: ATA channel to manipulate
436 * @device: ATA device (numbered from zero) to select
437 *
438 * Use the method defined in the ATA specification to
439 * make either device 0, or device 1, active on the
440 * ATA channel. Works with both PIO and MMIO.
441 *
442 * May be used as the dev_select() entry in ata_port_operations.
443 *
444 * LOCKING:
445 * caller.
446 */
Tejun Heo9363c382008-04-07 22:47:16 +0900447void ata_sff_dev_select(struct ata_port *ap, unsigned int device)
Tejun Heo624d5c52008-03-25 22:16:41 +0900448{
449 u8 tmp;
450
451 if (device == 0)
452 tmp = ATA_DEVICE_OBS;
453 else
454 tmp = ATA_DEVICE_OBS | ATA_DEV1;
455
456 iowrite8(tmp, ap->ioaddr.device_addr);
Tejun Heo9363c382008-04-07 22:47:16 +0900457 ata_sff_pause(ap); /* needed; also flushes, for mmio */
Tejun Heo624d5c52008-03-25 22:16:41 +0900458}
459
460/**
461 * ata_dev_select - Select device 0/1 on ATA bus
462 * @ap: ATA channel to manipulate
463 * @device: ATA device (numbered from zero) to select
464 * @wait: non-zero to wait for Status register BSY bit to clear
465 * @can_sleep: non-zero if context allows sleeping
466 *
467 * Use the method defined in the ATA specification to
468 * make either device 0, or device 1, active on the
469 * ATA channel.
470 *
Tejun Heo9363c382008-04-07 22:47:16 +0900471 * This is a high-level version of ata_sff_dev_select(), which
472 * additionally provides the services of inserting the proper
473 * pauses and status polling, where needed.
Tejun Heo624d5c52008-03-25 22:16:41 +0900474 *
475 * LOCKING:
476 * caller.
477 */
478void ata_dev_select(struct ata_port *ap, unsigned int device,
479 unsigned int wait, unsigned int can_sleep)
480{
481 if (ata_msg_probe(ap))
482 ata_port_printk(ap, KERN_INFO, "ata_dev_select: ENTER, "
483 "device %u, wait %u\n", device, wait);
484
485 if (wait)
486 ata_wait_idle(ap);
487
Tejun Heo5682ed32008-04-07 22:47:16 +0900488 ap->ops->sff_dev_select(ap, device);
Tejun Heo624d5c52008-03-25 22:16:41 +0900489
490 if (wait) {
491 if (can_sleep && ap->link.device[device].class == ATA_DEV_ATAPI)
492 msleep(150);
493 ata_wait_idle(ap);
494 }
495}
496
497/**
Tejun Heo9363c382008-04-07 22:47:16 +0900498 * ata_sff_irq_on - Enable interrupts on a port.
Tejun Heo90088bb2006-10-09 11:10:26 +0900499 * @ap: Port on which interrupts are enabled.
500 *
501 * Enable interrupts on a legacy IDE device using MMIO or PIO,
502 * wait for idle, clear any pending interrupts.
503 *
504 * LOCKING:
505 * Inherited from caller.
506 */
Tejun Heo9363c382008-04-07 22:47:16 +0900507u8 ata_sff_irq_on(struct ata_port *ap)
Tejun Heo90088bb2006-10-09 11:10:26 +0900508{
509 struct ata_ioports *ioaddr = &ap->ioaddr;
510 u8 tmp;
511
512 ap->ctl &= ~ATA_NIEN;
513 ap->last_ctl = ap->ctl;
514
Tejun Heof659f0e42008-03-06 13:12:54 +0900515 if (ioaddr->ctl_addr)
516 iowrite8(ap->ctl, ioaddr->ctl_addr);
Tejun Heo90088bb2006-10-09 11:10:26 +0900517 tmp = ata_wait_idle(ap);
518
Tejun Heo5682ed32008-04-07 22:47:16 +0900519 ap->ops->sff_irq_clear(ap);
Tejun Heo90088bb2006-10-09 11:10:26 +0900520
521 return tmp;
522}
523
524/**
Tejun Heo9363c382008-04-07 22:47:16 +0900525 * ata_sff_irq_clear - Clear PCI IDE BMDMA interrupt.
Tejun Heo272f7882008-03-25 22:16:40 +0900526 * @ap: Port associated with this ATA transaction.
527 *
528 * Clear interrupt and error flags in DMA status register.
529 *
530 * May be used as the irq_clear() entry in ata_port_operations.
531 *
532 * LOCKING:
533 * spin_lock_irqsave(host lock)
534 */
Tejun Heo9363c382008-04-07 22:47:16 +0900535void ata_sff_irq_clear(struct ata_port *ap)
Tejun Heo272f7882008-03-25 22:16:40 +0900536{
537 void __iomem *mmio = ap->ioaddr.bmdma_addr;
538
539 if (!mmio)
540 return;
541
542 iowrite8(ioread8(mmio + ATA_DMA_STATUS), mmio + ATA_DMA_STATUS);
543}
544
545/**
Tejun Heo9363c382008-04-07 22:47:16 +0900546 * ata_sff_tf_load - send taskfile registers to host controller
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500547 * @ap: Port to which output is sent
548 * @tf: ATA taskfile register set
549 *
550 * Outputs ATA taskfile to standard ATA host controller.
551 *
552 * LOCKING:
553 * Inherited from caller.
554 */
Tejun Heo9363c382008-04-07 22:47:16 +0900555void ata_sff_tf_load(struct ata_port *ap, const struct ata_taskfile *tf)
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500556{
Tejun Heo0d5ff562007-02-01 15:06:36 +0900557 struct ata_ioports *ioaddr = &ap->ioaddr;
558 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
559
560 if (tf->ctl != ap->last_ctl) {
Tejun Heof659f0e42008-03-06 13:12:54 +0900561 if (ioaddr->ctl_addr)
562 iowrite8(tf->ctl, ioaddr->ctl_addr);
Tejun Heo0d5ff562007-02-01 15:06:36 +0900563 ap->last_ctl = tf->ctl;
564 ata_wait_idle(ap);
565 }
566
567 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
Tejun Heof659f0e42008-03-06 13:12:54 +0900568 WARN_ON(!ioaddr->ctl_addr);
Tejun Heo0d5ff562007-02-01 15:06:36 +0900569 iowrite8(tf->hob_feature, ioaddr->feature_addr);
570 iowrite8(tf->hob_nsect, ioaddr->nsect_addr);
571 iowrite8(tf->hob_lbal, ioaddr->lbal_addr);
572 iowrite8(tf->hob_lbam, ioaddr->lbam_addr);
573 iowrite8(tf->hob_lbah, ioaddr->lbah_addr);
574 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
575 tf->hob_feature,
576 tf->hob_nsect,
577 tf->hob_lbal,
578 tf->hob_lbam,
579 tf->hob_lbah);
580 }
581
582 if (is_addr) {
583 iowrite8(tf->feature, ioaddr->feature_addr);
584 iowrite8(tf->nsect, ioaddr->nsect_addr);
585 iowrite8(tf->lbal, ioaddr->lbal_addr);
586 iowrite8(tf->lbam, ioaddr->lbam_addr);
587 iowrite8(tf->lbah, ioaddr->lbah_addr);
588 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
589 tf->feature,
590 tf->nsect,
591 tf->lbal,
592 tf->lbam,
593 tf->lbah);
594 }
595
596 if (tf->flags & ATA_TFLAG_DEVICE) {
597 iowrite8(tf->device, ioaddr->device_addr);
598 VPRINTK("device 0x%X\n", tf->device);
599 }
600
601 ata_wait_idle(ap);
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500602}
603
604/**
Tejun Heo9363c382008-04-07 22:47:16 +0900605 * ata_sff_tf_read - input device's ATA taskfile shadow registers
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500606 * @ap: Port from which input is read
607 * @tf: ATA taskfile register set for storing input
608 *
609 * Reads ATA taskfile registers for currently-selected device
Alan Cox76548ed2007-11-19 14:34:56 +0000610 * into @tf. Assumes the device has a fully SFF compliant task file
611 * layout and behaviour. If you device does not (eg has a different
612 * status method) then you will need to provide a replacement tf_read
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500613 *
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500614 * LOCKING:
615 * Inherited from caller.
616 */
Tejun Heo9363c382008-04-07 22:47:16 +0900617void ata_sff_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500618{
Tejun Heo0d5ff562007-02-01 15:06:36 +0900619 struct ata_ioports *ioaddr = &ap->ioaddr;
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500620
Tejun Heo9363c382008-04-07 22:47:16 +0900621 tf->command = ata_sff_check_status(ap);
Tejun Heo0d5ff562007-02-01 15:06:36 +0900622 tf->feature = ioread8(ioaddr->error_addr);
623 tf->nsect = ioread8(ioaddr->nsect_addr);
624 tf->lbal = ioread8(ioaddr->lbal_addr);
625 tf->lbam = ioread8(ioaddr->lbam_addr);
626 tf->lbah = ioread8(ioaddr->lbah_addr);
627 tf->device = ioread8(ioaddr->device_addr);
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500628
Tejun Heo0d5ff562007-02-01 15:06:36 +0900629 if (tf->flags & ATA_TFLAG_LBA48) {
Tejun Heof659f0e42008-03-06 13:12:54 +0900630 if (likely(ioaddr->ctl_addr)) {
631 iowrite8(tf->ctl | ATA_HOB, ioaddr->ctl_addr);
632 tf->hob_feature = ioread8(ioaddr->error_addr);
633 tf->hob_nsect = ioread8(ioaddr->nsect_addr);
634 tf->hob_lbal = ioread8(ioaddr->lbal_addr);
635 tf->hob_lbam = ioread8(ioaddr->lbam_addr);
636 tf->hob_lbah = ioread8(ioaddr->lbah_addr);
637 iowrite8(tf->ctl, ioaddr->ctl_addr);
638 ap->last_ctl = tf->ctl;
639 } else
640 WARN_ON(1);
Tejun Heo0d5ff562007-02-01 15:06:36 +0900641 }
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500642}
643
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500644/**
Tejun Heo9363c382008-04-07 22:47:16 +0900645 * ata_sff_exec_command - issue ATA command to host controller
Tejun Heo272f7882008-03-25 22:16:40 +0900646 * @ap: port to which command is being issued
647 * @tf: ATA taskfile register set
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500648 *
Tejun Heo272f7882008-03-25 22:16:40 +0900649 * Issues ATA command, with proper synchronization with interrupt
650 * handler / other threads.
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500651 *
652 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400653 * spin_lock_irqsave(host lock)
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500654 */
Tejun Heo9363c382008-04-07 22:47:16 +0900655void ata_sff_exec_command(struct ata_port *ap, const struct ata_taskfile *tf)
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500656{
Tejun Heo272f7882008-03-25 22:16:40 +0900657 DPRINTK("ata%u: cmd 0x%X\n", ap->print_id, tf->command);
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500658
Tejun Heo272f7882008-03-25 22:16:40 +0900659 iowrite8(tf->command, ap->ioaddr.command_addr);
Tejun Heo9363c382008-04-07 22:47:16 +0900660 ata_sff_pause(ap);
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500661}
662
Tejun Heo6d97dbd2006-05-15 20:58:24 +0900663/**
Tejun Heo624d5c52008-03-25 22:16:41 +0900664 * ata_tf_to_host - issue ATA taskfile to host controller
665 * @ap: port to which command is being issued
666 * @tf: ATA taskfile register set
667 *
668 * Issues ATA taskfile register set to ATA host controller,
669 * with proper synchronization with interrupt handler and
670 * other threads.
671 *
672 * LOCKING:
673 * spin_lock_irqsave(host lock)
674 */
675static inline void ata_tf_to_host(struct ata_port *ap,
676 const struct ata_taskfile *tf)
677{
Tejun Heo5682ed32008-04-07 22:47:16 +0900678 ap->ops->sff_tf_load(ap, tf);
679 ap->ops->sff_exec_command(ap, tf);
Tejun Heo624d5c52008-03-25 22:16:41 +0900680}
681
682/**
Tejun Heo9363c382008-04-07 22:47:16 +0900683 * ata_sff_data_xfer - Transfer data by PIO
Tejun Heo624d5c52008-03-25 22:16:41 +0900684 * @dev: device to target
685 * @buf: data buffer
686 * @buflen: buffer length
687 * @rw: read/write
688 *
689 * Transfer data from/to the device data register by PIO.
690 *
691 * LOCKING:
692 * Inherited from caller.
693 *
694 * RETURNS:
695 * Bytes consumed.
696 */
Tejun Heo9363c382008-04-07 22:47:16 +0900697unsigned int ata_sff_data_xfer(struct ata_device *dev, unsigned char *buf,
698 unsigned int buflen, int rw)
Tejun Heo624d5c52008-03-25 22:16:41 +0900699{
700 struct ata_port *ap = dev->link->ap;
701 void __iomem *data_addr = ap->ioaddr.data_addr;
702 unsigned int words = buflen >> 1;
703
704 /* Transfer multiple of 2 bytes */
705 if (rw == READ)
706 ioread16_rep(data_addr, buf, words);
707 else
708 iowrite16_rep(data_addr, buf, words);
709
710 /* Transfer trailing 1 byte, if any. */
711 if (unlikely(buflen & 0x01)) {
712 __le16 align_buf[1] = { 0 };
713 unsigned char *trailing_buf = buf + buflen - 1;
714
715 if (rw == READ) {
716 align_buf[0] = cpu_to_le16(ioread16(data_addr));
717 memcpy(trailing_buf, align_buf, 1);
718 } else {
719 memcpy(align_buf, trailing_buf, 1);
720 iowrite16(le16_to_cpu(align_buf[0]), data_addr);
721 }
722 words++;
723 }
724
725 return words << 1;
726}
727
728/**
Alan Cox871af122009-01-05 14:16:39 +0000729 * ata_sff_data_xfer32 - Transfer data by PIO
730 * @dev: device to target
731 * @buf: data buffer
732 * @buflen: buffer length
733 * @rw: read/write
734 *
735 * Transfer data from/to the device data register by PIO using 32bit
736 * I/O operations.
737 *
738 * LOCKING:
739 * Inherited from caller.
740 *
741 * RETURNS:
742 * Bytes consumed.
743 */
744
745unsigned int ata_sff_data_xfer32(struct ata_device *dev, unsigned char *buf,
746 unsigned int buflen, int rw)
747{
748 struct ata_port *ap = dev->link->ap;
749 void __iomem *data_addr = ap->ioaddr.data_addr;
750 unsigned int words = buflen >> 2;
751 int slop = buflen & 3;
752
753 /* Transfer multiple of 4 bytes */
754 if (rw == READ)
755 ioread32_rep(data_addr, buf, words);
756 else
757 iowrite32_rep(data_addr, buf, words);
758
759 if (unlikely(slop)) {
760 __le32 pad;
761 if (rw == READ) {
762 pad = cpu_to_le32(ioread32(ap->ioaddr.data_addr));
763 memcpy(buf + buflen - slop, &pad, slop);
764 } else {
765 memcpy(&pad, buf + buflen - slop, slop);
766 iowrite32(le32_to_cpu(pad), ap->ioaddr.data_addr);
767 }
768 words++;
769 }
770 return words << 2;
771}
772EXPORT_SYMBOL_GPL(ata_sff_data_xfer32);
773
774/**
Tejun Heo9363c382008-04-07 22:47:16 +0900775 * ata_sff_data_xfer_noirq - Transfer data by PIO
Tejun Heo624d5c52008-03-25 22:16:41 +0900776 * @dev: device to target
777 * @buf: data buffer
778 * @buflen: buffer length
779 * @rw: read/write
780 *
781 * Transfer data from/to the device data register by PIO. Do the
782 * transfer with interrupts disabled.
783 *
784 * LOCKING:
785 * Inherited from caller.
786 *
787 * RETURNS:
788 * Bytes consumed.
789 */
Tejun Heo9363c382008-04-07 22:47:16 +0900790unsigned int ata_sff_data_xfer_noirq(struct ata_device *dev, unsigned char *buf,
791 unsigned int buflen, int rw)
Tejun Heo624d5c52008-03-25 22:16:41 +0900792{
793 unsigned long flags;
794 unsigned int consumed;
795
796 local_irq_save(flags);
Tejun Heo9363c382008-04-07 22:47:16 +0900797 consumed = ata_sff_data_xfer(dev, buf, buflen, rw);
Tejun Heo624d5c52008-03-25 22:16:41 +0900798 local_irq_restore(flags);
799
800 return consumed;
801}
802
803/**
804 * ata_pio_sector - Transfer a sector of data.
805 * @qc: Command on going
806 *
807 * Transfer qc->sect_size bytes of data from/to the ATA device.
808 *
809 * LOCKING:
810 * Inherited from caller.
811 */
812static void ata_pio_sector(struct ata_queued_cmd *qc)
813{
814 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
815 struct ata_port *ap = qc->ap;
816 struct page *page;
817 unsigned int offset;
818 unsigned char *buf;
819
820 if (qc->curbytes == qc->nbytes - qc->sect_size)
821 ap->hsm_task_state = HSM_ST_LAST;
822
823 page = sg_page(qc->cursg);
824 offset = qc->cursg->offset + qc->cursg_ofs;
825
826 /* get the current page and offset */
827 page = nth_page(page, (offset >> PAGE_SHIFT));
828 offset %= PAGE_SIZE;
829
830 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
831
832 if (PageHighMem(page)) {
833 unsigned long flags;
834
835 /* FIXME: use a bounce buffer */
836 local_irq_save(flags);
837 buf = kmap_atomic(page, KM_IRQ0);
838
839 /* do the actual data transfer */
Tejun Heo5682ed32008-04-07 22:47:16 +0900840 ap->ops->sff_data_xfer(qc->dev, buf + offset, qc->sect_size,
841 do_write);
Tejun Heo624d5c52008-03-25 22:16:41 +0900842
843 kunmap_atomic(buf, KM_IRQ0);
844 local_irq_restore(flags);
845 } else {
846 buf = page_address(page);
Tejun Heo5682ed32008-04-07 22:47:16 +0900847 ap->ops->sff_data_xfer(qc->dev, buf + offset, qc->sect_size,
848 do_write);
Tejun Heo624d5c52008-03-25 22:16:41 +0900849 }
850
851 qc->curbytes += qc->sect_size;
852 qc->cursg_ofs += qc->sect_size;
853
854 if (qc->cursg_ofs == qc->cursg->length) {
855 qc->cursg = sg_next(qc->cursg);
856 qc->cursg_ofs = 0;
857 }
858}
859
860/**
861 * ata_pio_sectors - Transfer one or many sectors.
862 * @qc: Command on going
863 *
864 * Transfer one or many sectors of data from/to the
865 * ATA device for the DRQ request.
866 *
867 * LOCKING:
868 * Inherited from caller.
869 */
870static void ata_pio_sectors(struct ata_queued_cmd *qc)
871{
872 if (is_multi_taskfile(&qc->tf)) {
873 /* READ/WRITE MULTIPLE */
874 unsigned int nsect;
875
876 WARN_ON(qc->dev->multi_count == 0);
877
878 nsect = min((qc->nbytes - qc->curbytes) / qc->sect_size,
879 qc->dev->multi_count);
880 while (nsect--)
881 ata_pio_sector(qc);
882 } else
883 ata_pio_sector(qc);
884
Alan Coxa57c1ba2008-05-29 22:10:58 +0100885 ata_sff_sync(qc->ap); /* flush */
Tejun Heo624d5c52008-03-25 22:16:41 +0900886}
887
888/**
889 * atapi_send_cdb - Write CDB bytes to hardware
890 * @ap: Port to which ATAPI device is attached.
891 * @qc: Taskfile currently active
892 *
893 * When device has indicated its readiness to accept
894 * a CDB, this function is called. Send the CDB.
895 *
896 * LOCKING:
897 * caller.
898 */
899static void atapi_send_cdb(struct ata_port *ap, struct ata_queued_cmd *qc)
900{
901 /* send SCSI cdb */
902 DPRINTK("send cdb\n");
903 WARN_ON(qc->dev->cdb_len < 12);
904
Tejun Heo5682ed32008-04-07 22:47:16 +0900905 ap->ops->sff_data_xfer(qc->dev, qc->cdb, qc->dev->cdb_len, 1);
Alan Coxa57c1ba2008-05-29 22:10:58 +0100906 ata_sff_sync(ap);
907 /* FIXME: If the CDB is for DMA do we need to do the transition delay
908 or is bmdma_start guaranteed to do it ? */
Tejun Heo624d5c52008-03-25 22:16:41 +0900909 switch (qc->tf.protocol) {
910 case ATAPI_PROT_PIO:
911 ap->hsm_task_state = HSM_ST;
912 break;
913 case ATAPI_PROT_NODATA:
914 ap->hsm_task_state = HSM_ST_LAST;
915 break;
916 case ATAPI_PROT_DMA:
917 ap->hsm_task_state = HSM_ST_LAST;
918 /* initiate bmdma */
919 ap->ops->bmdma_start(qc);
920 break;
921 }
922}
923
924/**
925 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
926 * @qc: Command on going
927 * @bytes: number of bytes
928 *
929 * Transfer Transfer data from/to the ATAPI device.
930 *
931 * LOCKING:
932 * Inherited from caller.
933 *
934 */
935static int __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
936{
937 int rw = (qc->tf.flags & ATA_TFLAG_WRITE) ? WRITE : READ;
938 struct ata_port *ap = qc->ap;
939 struct ata_device *dev = qc->dev;
940 struct ata_eh_info *ehi = &dev->link->eh_info;
941 struct scatterlist *sg;
942 struct page *page;
943 unsigned char *buf;
944 unsigned int offset, count, consumed;
945
946next_sg:
947 sg = qc->cursg;
948 if (unlikely(!sg)) {
949 ata_ehi_push_desc(ehi, "unexpected or too much trailing data "
950 "buf=%u cur=%u bytes=%u",
951 qc->nbytes, qc->curbytes, bytes);
952 return -1;
953 }
954
955 page = sg_page(sg);
956 offset = sg->offset + qc->cursg_ofs;
957
958 /* get the current page and offset */
959 page = nth_page(page, (offset >> PAGE_SHIFT));
960 offset %= PAGE_SIZE;
961
962 /* don't overrun current sg */
963 count = min(sg->length - qc->cursg_ofs, bytes);
964
965 /* don't cross page boundaries */
966 count = min(count, (unsigned int)PAGE_SIZE - offset);
967
968 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
969
970 if (PageHighMem(page)) {
971 unsigned long flags;
972
973 /* FIXME: use bounce buffer */
974 local_irq_save(flags);
975 buf = kmap_atomic(page, KM_IRQ0);
976
977 /* do the actual data transfer */
Tejun Heo5682ed32008-04-07 22:47:16 +0900978 consumed = ap->ops->sff_data_xfer(dev, buf + offset, count, rw);
Tejun Heo624d5c52008-03-25 22:16:41 +0900979
980 kunmap_atomic(buf, KM_IRQ0);
981 local_irq_restore(flags);
982 } else {
983 buf = page_address(page);
Tejun Heo5682ed32008-04-07 22:47:16 +0900984 consumed = ap->ops->sff_data_xfer(dev, buf + offset, count, rw);
Tejun Heo624d5c52008-03-25 22:16:41 +0900985 }
986
987 bytes -= min(bytes, consumed);
988 qc->curbytes += count;
989 qc->cursg_ofs += count;
990
991 if (qc->cursg_ofs == sg->length) {
992 qc->cursg = sg_next(qc->cursg);
993 qc->cursg_ofs = 0;
994 }
995
996 /* consumed can be larger than count only for the last transfer */
997 WARN_ON(qc->cursg && count != consumed);
998
999 if (bytes)
1000 goto next_sg;
1001 return 0;
1002}
1003
1004/**
1005 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
1006 * @qc: Command on going
1007 *
1008 * Transfer Transfer data from/to the ATAPI device.
1009 *
1010 * LOCKING:
1011 * Inherited from caller.
1012 */
1013static void atapi_pio_bytes(struct ata_queued_cmd *qc)
1014{
1015 struct ata_port *ap = qc->ap;
1016 struct ata_device *dev = qc->dev;
1017 struct ata_eh_info *ehi = &dev->link->eh_info;
1018 unsigned int ireason, bc_lo, bc_hi, bytes;
1019 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
1020
1021 /* Abuse qc->result_tf for temp storage of intermediate TF
1022 * here to save some kernel stack usage.
1023 * For normal completion, qc->result_tf is not relevant. For
1024 * error, qc->result_tf is later overwritten by ata_qc_complete().
1025 * So, the correctness of qc->result_tf is not affected.
1026 */
Tejun Heo5682ed32008-04-07 22:47:16 +09001027 ap->ops->sff_tf_read(ap, &qc->result_tf);
Tejun Heo624d5c52008-03-25 22:16:41 +09001028 ireason = qc->result_tf.nsect;
1029 bc_lo = qc->result_tf.lbam;
1030 bc_hi = qc->result_tf.lbah;
1031 bytes = (bc_hi << 8) | bc_lo;
1032
1033 /* shall be cleared to zero, indicating xfer of data */
1034 if (unlikely(ireason & (1 << 0)))
1035 goto atapi_check;
1036
1037 /* make sure transfer direction matches expected */
1038 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
1039 if (unlikely(do_write != i_write))
1040 goto atapi_check;
1041
1042 if (unlikely(!bytes))
1043 goto atapi_check;
1044
1045 VPRINTK("ata%u: xfering %d bytes\n", ap->print_id, bytes);
1046
1047 if (unlikely(__atapi_pio_bytes(qc, bytes)))
1048 goto err_out;
Alan Coxa57c1ba2008-05-29 22:10:58 +01001049 ata_sff_sync(ap); /* flush */
Tejun Heo624d5c52008-03-25 22:16:41 +09001050
1051 return;
1052
1053 atapi_check:
1054 ata_ehi_push_desc(ehi, "ATAPI check failed (ireason=0x%x bytes=%u)",
1055 ireason, bytes);
1056 err_out:
1057 qc->err_mask |= AC_ERR_HSM;
1058 ap->hsm_task_state = HSM_ST_ERR;
1059}
1060
1061/**
1062 * ata_hsm_ok_in_wq - Check if the qc can be handled in the workqueue.
1063 * @ap: the target ata_port
1064 * @qc: qc on going
1065 *
1066 * RETURNS:
1067 * 1 if ok in workqueue, 0 otherwise.
1068 */
1069static inline int ata_hsm_ok_in_wq(struct ata_port *ap, struct ata_queued_cmd *qc)
1070{
1071 if (qc->tf.flags & ATA_TFLAG_POLLING)
1072 return 1;
1073
1074 if (ap->hsm_task_state == HSM_ST_FIRST) {
1075 if (qc->tf.protocol == ATA_PROT_PIO &&
1076 (qc->tf.flags & ATA_TFLAG_WRITE))
1077 return 1;
1078
1079 if (ata_is_atapi(qc->tf.protocol) &&
1080 !(qc->dev->flags & ATA_DFLAG_CDB_INTR))
1081 return 1;
1082 }
1083
1084 return 0;
1085}
1086
1087/**
1088 * ata_hsm_qc_complete - finish a qc running on standard HSM
1089 * @qc: Command to complete
1090 * @in_wq: 1 if called from workqueue, 0 otherwise
1091 *
1092 * Finish @qc which is running on standard HSM.
1093 *
1094 * LOCKING:
1095 * If @in_wq is zero, spin_lock_irqsave(host lock).
1096 * Otherwise, none on entry and grabs host lock.
1097 */
1098static void ata_hsm_qc_complete(struct ata_queued_cmd *qc, int in_wq)
1099{
1100 struct ata_port *ap = qc->ap;
1101 unsigned long flags;
1102
1103 if (ap->ops->error_handler) {
1104 if (in_wq) {
1105 spin_lock_irqsave(ap->lock, flags);
1106
1107 /* EH might have kicked in while host lock is
1108 * released.
1109 */
1110 qc = ata_qc_from_tag(ap, qc->tag);
1111 if (qc) {
1112 if (likely(!(qc->err_mask & AC_ERR_HSM))) {
Tejun Heo5682ed32008-04-07 22:47:16 +09001113 ap->ops->sff_irq_on(ap);
Tejun Heo624d5c52008-03-25 22:16:41 +09001114 ata_qc_complete(qc);
1115 } else
1116 ata_port_freeze(ap);
1117 }
1118
1119 spin_unlock_irqrestore(ap->lock, flags);
1120 } else {
1121 if (likely(!(qc->err_mask & AC_ERR_HSM)))
1122 ata_qc_complete(qc);
1123 else
1124 ata_port_freeze(ap);
1125 }
1126 } else {
1127 if (in_wq) {
1128 spin_lock_irqsave(ap->lock, flags);
Tejun Heo5682ed32008-04-07 22:47:16 +09001129 ap->ops->sff_irq_on(ap);
Tejun Heo624d5c52008-03-25 22:16:41 +09001130 ata_qc_complete(qc);
1131 spin_unlock_irqrestore(ap->lock, flags);
1132 } else
1133 ata_qc_complete(qc);
1134 }
1135}
1136
1137/**
Tejun Heo9363c382008-04-07 22:47:16 +09001138 * ata_sff_hsm_move - move the HSM to the next state.
Tejun Heo624d5c52008-03-25 22:16:41 +09001139 * @ap: the target ata_port
1140 * @qc: qc on going
1141 * @status: current device status
1142 * @in_wq: 1 if called from workqueue, 0 otherwise
1143 *
1144 * RETURNS:
1145 * 1 when poll next status needed, 0 otherwise.
1146 */
Tejun Heo9363c382008-04-07 22:47:16 +09001147int ata_sff_hsm_move(struct ata_port *ap, struct ata_queued_cmd *qc,
1148 u8 status, int in_wq)
Tejun Heo624d5c52008-03-25 22:16:41 +09001149{
Tejun Heoa836d3e2008-06-28 01:39:43 +09001150 struct ata_eh_info *ehi = &ap->link.eh_info;
Tejun Heo624d5c52008-03-25 22:16:41 +09001151 unsigned long flags = 0;
1152 int poll_next;
1153
1154 WARN_ON((qc->flags & ATA_QCFLAG_ACTIVE) == 0);
1155
Tejun Heo9363c382008-04-07 22:47:16 +09001156 /* Make sure ata_sff_qc_issue() does not throw things
Tejun Heo624d5c52008-03-25 22:16:41 +09001157 * like DMA polling into the workqueue. Notice that
1158 * in_wq is not equivalent to (qc->tf.flags & ATA_TFLAG_POLLING).
1159 */
1160 WARN_ON(in_wq != ata_hsm_ok_in_wq(ap, qc));
1161
1162fsm_start:
1163 DPRINTK("ata%u: protocol %d task_state %d (dev_stat 0x%X)\n",
1164 ap->print_id, qc->tf.protocol, ap->hsm_task_state, status);
1165
1166 switch (ap->hsm_task_state) {
1167 case HSM_ST_FIRST:
1168 /* Send first data block or PACKET CDB */
1169
1170 /* If polling, we will stay in the work queue after
1171 * sending the data. Otherwise, interrupt handler
1172 * takes over after sending the data.
1173 */
1174 poll_next = (qc->tf.flags & ATA_TFLAG_POLLING);
1175
1176 /* check device status */
1177 if (unlikely((status & ATA_DRQ) == 0)) {
1178 /* handle BSY=0, DRQ=0 as error */
1179 if (likely(status & (ATA_ERR | ATA_DF)))
1180 /* device stops HSM for abort/error */
1181 qc->err_mask |= AC_ERR_DEV;
Tejun Heoa836d3e2008-06-28 01:39:43 +09001182 else {
Tejun Heo624d5c52008-03-25 22:16:41 +09001183 /* HSM violation. Let EH handle this */
Tejun Heoa836d3e2008-06-28 01:39:43 +09001184 ata_ehi_push_desc(ehi,
1185 "ST_FIRST: !(DRQ|ERR|DF)");
Tejun Heo624d5c52008-03-25 22:16:41 +09001186 qc->err_mask |= AC_ERR_HSM;
Tejun Heoa836d3e2008-06-28 01:39:43 +09001187 }
Tejun Heo624d5c52008-03-25 22:16:41 +09001188
1189 ap->hsm_task_state = HSM_ST_ERR;
1190 goto fsm_start;
1191 }
1192
1193 /* Device should not ask for data transfer (DRQ=1)
1194 * when it finds something wrong.
1195 * We ignore DRQ here and stop the HSM by
1196 * changing hsm_task_state to HSM_ST_ERR and
1197 * let the EH abort the command or reset the device.
1198 */
1199 if (unlikely(status & (ATA_ERR | ATA_DF))) {
1200 /* Some ATAPI tape drives forget to clear the ERR bit
1201 * when doing the next command (mostly request sense).
1202 * We ignore ERR here to workaround and proceed sending
1203 * the CDB.
1204 */
1205 if (!(qc->dev->horkage & ATA_HORKAGE_STUCK_ERR)) {
Tejun Heoa836d3e2008-06-28 01:39:43 +09001206 ata_ehi_push_desc(ehi, "ST_FIRST: "
1207 "DRQ=1 with device error, "
1208 "dev_stat 0x%X", status);
Tejun Heo624d5c52008-03-25 22:16:41 +09001209 qc->err_mask |= AC_ERR_HSM;
1210 ap->hsm_task_state = HSM_ST_ERR;
1211 goto fsm_start;
1212 }
1213 }
1214
1215 /* Send the CDB (atapi) or the first data block (ata pio out).
1216 * During the state transition, interrupt handler shouldn't
1217 * be invoked before the data transfer is complete and
1218 * hsm_task_state is changed. Hence, the following locking.
1219 */
1220 if (in_wq)
1221 spin_lock_irqsave(ap->lock, flags);
1222
1223 if (qc->tf.protocol == ATA_PROT_PIO) {
1224 /* PIO data out protocol.
1225 * send first data block.
1226 */
1227
1228 /* ata_pio_sectors() might change the state
1229 * to HSM_ST_LAST. so, the state is changed here
1230 * before ata_pio_sectors().
1231 */
1232 ap->hsm_task_state = HSM_ST;
1233 ata_pio_sectors(qc);
1234 } else
1235 /* send CDB */
1236 atapi_send_cdb(ap, qc);
1237
1238 if (in_wq)
1239 spin_unlock_irqrestore(ap->lock, flags);
1240
1241 /* if polling, ata_pio_task() handles the rest.
1242 * otherwise, interrupt handler takes over from here.
1243 */
1244 break;
1245
1246 case HSM_ST:
1247 /* complete command or read/write the data register */
1248 if (qc->tf.protocol == ATAPI_PROT_PIO) {
1249 /* ATAPI PIO protocol */
1250 if ((status & ATA_DRQ) == 0) {
1251 /* No more data to transfer or device error.
1252 * Device error will be tagged in HSM_ST_LAST.
1253 */
1254 ap->hsm_task_state = HSM_ST_LAST;
1255 goto fsm_start;
1256 }
1257
1258 /* Device should not ask for data transfer (DRQ=1)
1259 * when it finds something wrong.
1260 * We ignore DRQ here and stop the HSM by
1261 * changing hsm_task_state to HSM_ST_ERR and
1262 * let the EH abort the command or reset the device.
1263 */
1264 if (unlikely(status & (ATA_ERR | ATA_DF))) {
Tejun Heoa836d3e2008-06-28 01:39:43 +09001265 ata_ehi_push_desc(ehi, "ST-ATAPI: "
1266 "DRQ=1 with device error, "
1267 "dev_stat 0x%X", status);
Tejun Heo624d5c52008-03-25 22:16:41 +09001268 qc->err_mask |= AC_ERR_HSM;
1269 ap->hsm_task_state = HSM_ST_ERR;
1270 goto fsm_start;
1271 }
1272
1273 atapi_pio_bytes(qc);
1274
1275 if (unlikely(ap->hsm_task_state == HSM_ST_ERR))
1276 /* bad ireason reported by device */
1277 goto fsm_start;
1278
1279 } else {
1280 /* ATA PIO protocol */
1281 if (unlikely((status & ATA_DRQ) == 0)) {
1282 /* handle BSY=0, DRQ=0 as error */
Tejun Heo6a6b97d2008-11-13 10:04:46 +09001283 if (likely(status & (ATA_ERR | ATA_DF))) {
Tejun Heo624d5c52008-03-25 22:16:41 +09001284 /* device stops HSM for abort/error */
1285 qc->err_mask |= AC_ERR_DEV;
Tejun Heo6a6b97d2008-11-13 10:04:46 +09001286
1287 /* If diagnostic failed and this is
1288 * IDENTIFY, it's likely a phantom
1289 * device. Mark hint.
1290 */
1291 if (qc->dev->horkage &
1292 ATA_HORKAGE_DIAGNOSTIC)
1293 qc->err_mask |=
1294 AC_ERR_NODEV_HINT;
1295 } else {
Tejun Heo624d5c52008-03-25 22:16:41 +09001296 /* HSM violation. Let EH handle this.
1297 * Phantom devices also trigger this
1298 * condition. Mark hint.
1299 */
Tejun Heoa836d3e2008-06-28 01:39:43 +09001300 ata_ehi_push_desc(ehi, "ST-ATA: "
1301 "DRQ=1 with device error, "
1302 "dev_stat 0x%X", status);
Tejun Heo624d5c52008-03-25 22:16:41 +09001303 qc->err_mask |= AC_ERR_HSM |
1304 AC_ERR_NODEV_HINT;
Tejun Heoa836d3e2008-06-28 01:39:43 +09001305 }
Tejun Heo624d5c52008-03-25 22:16:41 +09001306
1307 ap->hsm_task_state = HSM_ST_ERR;
1308 goto fsm_start;
1309 }
1310
1311 /* For PIO reads, some devices may ask for
1312 * data transfer (DRQ=1) alone with ERR=1.
1313 * We respect DRQ here and transfer one
1314 * block of junk data before changing the
1315 * hsm_task_state to HSM_ST_ERR.
1316 *
1317 * For PIO writes, ERR=1 DRQ=1 doesn't make
1318 * sense since the data block has been
1319 * transferred to the device.
1320 */
1321 if (unlikely(status & (ATA_ERR | ATA_DF))) {
1322 /* data might be corrputed */
1323 qc->err_mask |= AC_ERR_DEV;
1324
1325 if (!(qc->tf.flags & ATA_TFLAG_WRITE)) {
1326 ata_pio_sectors(qc);
1327 status = ata_wait_idle(ap);
1328 }
1329
Tejun Heoa836d3e2008-06-28 01:39:43 +09001330 if (status & (ATA_BUSY | ATA_DRQ)) {
1331 ata_ehi_push_desc(ehi, "ST-ATA: "
1332 "BUSY|DRQ persists on ERR|DF, "
1333 "dev_stat 0x%X", status);
Tejun Heo624d5c52008-03-25 22:16:41 +09001334 qc->err_mask |= AC_ERR_HSM;
Tejun Heoa836d3e2008-06-28 01:39:43 +09001335 }
Tejun Heo624d5c52008-03-25 22:16:41 +09001336
1337 /* ata_pio_sectors() might change the
1338 * state to HSM_ST_LAST. so, the state
1339 * is changed after ata_pio_sectors().
1340 */
1341 ap->hsm_task_state = HSM_ST_ERR;
1342 goto fsm_start;
1343 }
1344
1345 ata_pio_sectors(qc);
1346
1347 if (ap->hsm_task_state == HSM_ST_LAST &&
1348 (!(qc->tf.flags & ATA_TFLAG_WRITE))) {
1349 /* all data read */
1350 status = ata_wait_idle(ap);
1351 goto fsm_start;
1352 }
1353 }
1354
1355 poll_next = 1;
1356 break;
1357
1358 case HSM_ST_LAST:
1359 if (unlikely(!ata_ok(status))) {
1360 qc->err_mask |= __ac_err_mask(status);
1361 ap->hsm_task_state = HSM_ST_ERR;
1362 goto fsm_start;
1363 }
1364
1365 /* no more data to transfer */
1366 DPRINTK("ata%u: dev %u command complete, drv_stat 0x%x\n",
1367 ap->print_id, qc->dev->devno, status);
1368
Tejun Heo411cb3862008-04-23 20:48:36 +09001369 WARN_ON(qc->err_mask & (AC_ERR_DEV | AC_ERR_HSM));
Tejun Heo624d5c52008-03-25 22:16:41 +09001370
1371 ap->hsm_task_state = HSM_ST_IDLE;
1372
1373 /* complete taskfile transaction */
1374 ata_hsm_qc_complete(qc, in_wq);
1375
1376 poll_next = 0;
1377 break;
1378
1379 case HSM_ST_ERR:
Tejun Heo624d5c52008-03-25 22:16:41 +09001380 ap->hsm_task_state = HSM_ST_IDLE;
1381
1382 /* complete taskfile transaction */
1383 ata_hsm_qc_complete(qc, in_wq);
1384
1385 poll_next = 0;
1386 break;
1387 default:
1388 poll_next = 0;
1389 BUG();
1390 }
1391
1392 return poll_next;
1393}
1394
1395void ata_pio_task(struct work_struct *work)
1396{
1397 struct ata_port *ap =
1398 container_of(work, struct ata_port, port_task.work);
1399 struct ata_queued_cmd *qc = ap->port_task_data;
1400 u8 status;
1401 int poll_next;
1402
1403fsm_start:
1404 WARN_ON(ap->hsm_task_state == HSM_ST_IDLE);
1405
1406 /*
1407 * This is purely heuristic. This is a fast path.
1408 * Sometimes when we enter, BSY will be cleared in
1409 * a chk-status or two. If not, the drive is probably seeking
1410 * or something. Snooze for a couple msecs, then
1411 * chk-status again. If still busy, queue delayed work.
1412 */
Tejun Heo9363c382008-04-07 22:47:16 +09001413 status = ata_sff_busy_wait(ap, ATA_BUSY, 5);
Tejun Heo624d5c52008-03-25 22:16:41 +09001414 if (status & ATA_BUSY) {
1415 msleep(2);
Tejun Heo9363c382008-04-07 22:47:16 +09001416 status = ata_sff_busy_wait(ap, ATA_BUSY, 10);
Tejun Heo624d5c52008-03-25 22:16:41 +09001417 if (status & ATA_BUSY) {
1418 ata_pio_queue_task(ap, qc, ATA_SHORT_PAUSE);
1419 return;
1420 }
1421 }
1422
1423 /* move the HSM */
Tejun Heo9363c382008-04-07 22:47:16 +09001424 poll_next = ata_sff_hsm_move(ap, qc, status, 1);
Tejun Heo624d5c52008-03-25 22:16:41 +09001425
1426 /* another command or interrupt handler
1427 * may be running at this point.
1428 */
1429 if (poll_next)
1430 goto fsm_start;
1431}
1432
1433/**
Tejun Heo9363c382008-04-07 22:47:16 +09001434 * ata_sff_qc_issue - issue taskfile to device in proto-dependent manner
Tejun Heo624d5c52008-03-25 22:16:41 +09001435 * @qc: command to issue to device
1436 *
1437 * Using various libata functions and hooks, this function
1438 * starts an ATA command. ATA commands are grouped into
1439 * classes called "protocols", and issuing each type of protocol
1440 * is slightly different.
1441 *
1442 * May be used as the qc_issue() entry in ata_port_operations.
1443 *
1444 * LOCKING:
1445 * spin_lock_irqsave(host lock)
1446 *
1447 * RETURNS:
1448 * Zero on success, AC_ERR_* mask on failure
1449 */
Tejun Heo9363c382008-04-07 22:47:16 +09001450unsigned int ata_sff_qc_issue(struct ata_queued_cmd *qc)
Tejun Heo624d5c52008-03-25 22:16:41 +09001451{
1452 struct ata_port *ap = qc->ap;
1453
1454 /* Use polling pio if the LLD doesn't handle
1455 * interrupt driven pio and atapi CDB interrupt.
1456 */
1457 if (ap->flags & ATA_FLAG_PIO_POLLING) {
1458 switch (qc->tf.protocol) {
1459 case ATA_PROT_PIO:
1460 case ATA_PROT_NODATA:
1461 case ATAPI_PROT_PIO:
1462 case ATAPI_PROT_NODATA:
1463 qc->tf.flags |= ATA_TFLAG_POLLING;
1464 break;
1465 case ATAPI_PROT_DMA:
1466 if (qc->dev->flags & ATA_DFLAG_CDB_INTR)
1467 /* see ata_dma_blacklisted() */
1468 BUG();
1469 break;
1470 default:
1471 break;
1472 }
1473 }
1474
1475 /* select the device */
1476 ata_dev_select(ap, qc->dev->devno, 1, 0);
1477
1478 /* start the command */
1479 switch (qc->tf.protocol) {
1480 case ATA_PROT_NODATA:
1481 if (qc->tf.flags & ATA_TFLAG_POLLING)
1482 ata_qc_set_polling(qc);
1483
1484 ata_tf_to_host(ap, &qc->tf);
1485 ap->hsm_task_state = HSM_ST_LAST;
1486
1487 if (qc->tf.flags & ATA_TFLAG_POLLING)
1488 ata_pio_queue_task(ap, qc, 0);
1489
1490 break;
1491
1492 case ATA_PROT_DMA:
1493 WARN_ON(qc->tf.flags & ATA_TFLAG_POLLING);
1494
Tejun Heo5682ed32008-04-07 22:47:16 +09001495 ap->ops->sff_tf_load(ap, &qc->tf); /* load tf registers */
Tejun Heo624d5c52008-03-25 22:16:41 +09001496 ap->ops->bmdma_setup(qc); /* set up bmdma */
1497 ap->ops->bmdma_start(qc); /* initiate bmdma */
1498 ap->hsm_task_state = HSM_ST_LAST;
1499 break;
1500
1501 case ATA_PROT_PIO:
1502 if (qc->tf.flags & ATA_TFLAG_POLLING)
1503 ata_qc_set_polling(qc);
1504
1505 ata_tf_to_host(ap, &qc->tf);
1506
1507 if (qc->tf.flags & ATA_TFLAG_WRITE) {
1508 /* PIO data out protocol */
1509 ap->hsm_task_state = HSM_ST_FIRST;
1510 ata_pio_queue_task(ap, qc, 0);
1511
1512 /* always send first data block using
1513 * the ata_pio_task() codepath.
1514 */
1515 } else {
1516 /* PIO data in protocol */
1517 ap->hsm_task_state = HSM_ST;
1518
1519 if (qc->tf.flags & ATA_TFLAG_POLLING)
1520 ata_pio_queue_task(ap, qc, 0);
1521
1522 /* if polling, ata_pio_task() handles the rest.
1523 * otherwise, interrupt handler takes over from here.
1524 */
1525 }
1526
1527 break;
1528
1529 case ATAPI_PROT_PIO:
1530 case ATAPI_PROT_NODATA:
1531 if (qc->tf.flags & ATA_TFLAG_POLLING)
1532 ata_qc_set_polling(qc);
1533
1534 ata_tf_to_host(ap, &qc->tf);
1535
1536 ap->hsm_task_state = HSM_ST_FIRST;
1537
1538 /* send cdb by polling if no cdb interrupt */
1539 if ((!(qc->dev->flags & ATA_DFLAG_CDB_INTR)) ||
1540 (qc->tf.flags & ATA_TFLAG_POLLING))
1541 ata_pio_queue_task(ap, qc, 0);
1542 break;
1543
1544 case ATAPI_PROT_DMA:
1545 WARN_ON(qc->tf.flags & ATA_TFLAG_POLLING);
1546
Tejun Heo5682ed32008-04-07 22:47:16 +09001547 ap->ops->sff_tf_load(ap, &qc->tf); /* load tf registers */
Tejun Heo624d5c52008-03-25 22:16:41 +09001548 ap->ops->bmdma_setup(qc); /* set up bmdma */
1549 ap->hsm_task_state = HSM_ST_FIRST;
1550
1551 /* send cdb by polling if no cdb interrupt */
1552 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
1553 ata_pio_queue_task(ap, qc, 0);
1554 break;
1555
1556 default:
1557 WARN_ON(1);
1558 return AC_ERR_SYSTEM;
1559 }
1560
1561 return 0;
1562}
1563
1564/**
Tejun Heo22183bf2008-04-07 22:47:20 +09001565 * ata_sff_qc_fill_rtf - fill result TF using ->sff_tf_read
1566 * @qc: qc to fill result TF for
1567 *
1568 * @qc is finished and result TF needs to be filled. Fill it
1569 * using ->sff_tf_read.
1570 *
1571 * LOCKING:
1572 * spin_lock_irqsave(host lock)
1573 *
1574 * RETURNS:
1575 * true indicating that result TF is successfully filled.
1576 */
1577bool ata_sff_qc_fill_rtf(struct ata_queued_cmd *qc)
1578{
1579 qc->ap->ops->sff_tf_read(qc->ap, &qc->result_tf);
1580 return true;
1581}
1582
1583/**
Tejun Heo9363c382008-04-07 22:47:16 +09001584 * ata_sff_host_intr - Handle host interrupt for given (port, task)
Tejun Heo624d5c52008-03-25 22:16:41 +09001585 * @ap: Port on which interrupt arrived (possibly...)
1586 * @qc: Taskfile currently active in engine
1587 *
1588 * Handle host interrupt for given queued command. Currently,
1589 * only DMA interrupts are handled. All other commands are
1590 * handled via polling with interrupts disabled (nIEN bit).
1591 *
1592 * LOCKING:
1593 * spin_lock_irqsave(host lock)
1594 *
1595 * RETURNS:
1596 * One if interrupt was handled, zero if not (shared irq).
1597 */
Tejun Heo9363c382008-04-07 22:47:16 +09001598inline unsigned int ata_sff_host_intr(struct ata_port *ap,
1599 struct ata_queued_cmd *qc)
Tejun Heo624d5c52008-03-25 22:16:41 +09001600{
1601 struct ata_eh_info *ehi = &ap->link.eh_info;
1602 u8 status, host_stat = 0;
1603
1604 VPRINTK("ata%u: protocol %d task_state %d\n",
1605 ap->print_id, qc->tf.protocol, ap->hsm_task_state);
1606
1607 /* Check whether we are expecting interrupt in this state */
1608 switch (ap->hsm_task_state) {
1609 case HSM_ST_FIRST:
1610 /* Some pre-ATAPI-4 devices assert INTRQ
1611 * at this state when ready to receive CDB.
1612 */
1613
1614 /* Check the ATA_DFLAG_CDB_INTR flag is enough here.
1615 * The flag was turned on only for atapi devices. No
1616 * need to check ata_is_atapi(qc->tf.protocol) again.
1617 */
1618 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
1619 goto idle_irq;
1620 break;
1621 case HSM_ST_LAST:
1622 if (qc->tf.protocol == ATA_PROT_DMA ||
1623 qc->tf.protocol == ATAPI_PROT_DMA) {
1624 /* check status of DMA engine */
1625 host_stat = ap->ops->bmdma_status(ap);
1626 VPRINTK("ata%u: host_stat 0x%X\n",
1627 ap->print_id, host_stat);
1628
1629 /* if it's not our irq... */
1630 if (!(host_stat & ATA_DMA_INTR))
1631 goto idle_irq;
1632
1633 /* before we do anything else, clear DMA-Start bit */
1634 ap->ops->bmdma_stop(qc);
1635
1636 if (unlikely(host_stat & ATA_DMA_ERR)) {
1637 /* error when transfering data to/from memory */
1638 qc->err_mask |= AC_ERR_HOST_BUS;
1639 ap->hsm_task_state = HSM_ST_ERR;
1640 }
1641 }
1642 break;
1643 case HSM_ST:
1644 break;
1645 default:
1646 goto idle_irq;
1647 }
1648
Tejun Heo624d5c52008-03-25 22:16:41 +09001649
Alan Coxa57c1ba2008-05-29 22:10:58 +01001650 /* check main status, clearing INTRQ if needed */
1651 status = ata_sff_irq_status(ap);
1652 if (status & ATA_BUSY)
Tejun Heo624d5c52008-03-25 22:16:41 +09001653 goto idle_irq;
1654
1655 /* ack bmdma irq events */
Tejun Heo5682ed32008-04-07 22:47:16 +09001656 ap->ops->sff_irq_clear(ap);
Tejun Heo624d5c52008-03-25 22:16:41 +09001657
Tejun Heo9363c382008-04-07 22:47:16 +09001658 ata_sff_hsm_move(ap, qc, status, 0);
Tejun Heo624d5c52008-03-25 22:16:41 +09001659
1660 if (unlikely(qc->err_mask) && (qc->tf.protocol == ATA_PROT_DMA ||
1661 qc->tf.protocol == ATAPI_PROT_DMA))
1662 ata_ehi_push_desc(ehi, "BMDMA stat 0x%x", host_stat);
1663
1664 return 1; /* irq handled */
1665
1666idle_irq:
1667 ap->stats.idle_irq++;
1668
1669#ifdef ATA_IRQ_TRAP
1670 if ((ap->stats.idle_irq % 1000) == 0) {
Tejun Heo5682ed32008-04-07 22:47:16 +09001671 ap->ops->sff_check_status(ap);
1672 ap->ops->sff_irq_clear(ap);
Tejun Heo624d5c52008-03-25 22:16:41 +09001673 ata_port_printk(ap, KERN_WARNING, "irq trap\n");
1674 return 1;
1675 }
1676#endif
1677 return 0; /* irq not handled */
1678}
1679
1680/**
Tejun Heo9363c382008-04-07 22:47:16 +09001681 * ata_sff_interrupt - Default ATA host interrupt handler
Tejun Heo624d5c52008-03-25 22:16:41 +09001682 * @irq: irq line (unused)
1683 * @dev_instance: pointer to our ata_host information structure
1684 *
1685 * Default interrupt handler for PCI IDE devices. Calls
Tejun Heo9363c382008-04-07 22:47:16 +09001686 * ata_sff_host_intr() for each port that is not disabled.
Tejun Heo624d5c52008-03-25 22:16:41 +09001687 *
1688 * LOCKING:
1689 * Obtains host lock during operation.
1690 *
1691 * RETURNS:
1692 * IRQ_NONE or IRQ_HANDLED.
1693 */
Tejun Heo9363c382008-04-07 22:47:16 +09001694irqreturn_t ata_sff_interrupt(int irq, void *dev_instance)
Tejun Heo624d5c52008-03-25 22:16:41 +09001695{
1696 struct ata_host *host = dev_instance;
1697 unsigned int i;
1698 unsigned int handled = 0;
1699 unsigned long flags;
1700
1701 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
1702 spin_lock_irqsave(&host->lock, flags);
1703
1704 for (i = 0; i < host->n_ports; i++) {
1705 struct ata_port *ap;
1706
1707 ap = host->ports[i];
1708 if (ap &&
1709 !(ap->flags & ATA_FLAG_DISABLED)) {
1710 struct ata_queued_cmd *qc;
1711
1712 qc = ata_qc_from_tag(ap, ap->link.active_tag);
1713 if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING)) &&
1714 (qc->flags & ATA_QCFLAG_ACTIVE))
Tejun Heo9363c382008-04-07 22:47:16 +09001715 handled |= ata_sff_host_intr(ap, qc);
Tejun Heo624d5c52008-03-25 22:16:41 +09001716 }
1717 }
1718
1719 spin_unlock_irqrestore(&host->lock, flags);
1720
1721 return IRQ_RETVAL(handled);
1722}
1723
1724/**
Tejun Heo9363c382008-04-07 22:47:16 +09001725 * ata_sff_freeze - Freeze SFF controller port
Tejun Heo6d97dbd2006-05-15 20:58:24 +09001726 * @ap: port to freeze
1727 *
1728 * Freeze BMDMA controller port.
1729 *
1730 * LOCKING:
1731 * Inherited from caller.
1732 */
Tejun Heo9363c382008-04-07 22:47:16 +09001733void ata_sff_freeze(struct ata_port *ap)
Tejun Heo6d97dbd2006-05-15 20:58:24 +09001734{
1735 struct ata_ioports *ioaddr = &ap->ioaddr;
1736
1737 ap->ctl |= ATA_NIEN;
1738 ap->last_ctl = ap->ctl;
1739
Tejun Heof659f0e42008-03-06 13:12:54 +09001740 if (ioaddr->ctl_addr)
1741 iowrite8(ap->ctl, ioaddr->ctl_addr);
Tejun Heo0f0a3ad2006-11-17 12:24:22 +09001742
1743 /* Under certain circumstances, some controllers raise IRQ on
1744 * ATA_NIEN manipulation. Also, many controllers fail to mask
1745 * previously pending IRQ on ATA_NIEN assertion. Clear it.
1746 */
Tejun Heo5682ed32008-04-07 22:47:16 +09001747 ap->ops->sff_check_status(ap);
Tejun Heo0f0a3ad2006-11-17 12:24:22 +09001748
Tejun Heo5682ed32008-04-07 22:47:16 +09001749 ap->ops->sff_irq_clear(ap);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09001750}
1751
1752/**
Tejun Heo9363c382008-04-07 22:47:16 +09001753 * ata_sff_thaw - Thaw SFF controller port
Tejun Heo6d97dbd2006-05-15 20:58:24 +09001754 * @ap: port to thaw
1755 *
Tejun Heo9363c382008-04-07 22:47:16 +09001756 * Thaw SFF controller port.
Tejun Heo6d97dbd2006-05-15 20:58:24 +09001757 *
1758 * LOCKING:
1759 * Inherited from caller.
1760 */
Tejun Heo9363c382008-04-07 22:47:16 +09001761void ata_sff_thaw(struct ata_port *ap)
Tejun Heo6d97dbd2006-05-15 20:58:24 +09001762{
1763 /* clear & re-enable interrupts */
Tejun Heo5682ed32008-04-07 22:47:16 +09001764 ap->ops->sff_check_status(ap);
1765 ap->ops->sff_irq_clear(ap);
1766 ap->ops->sff_irq_on(ap);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09001767}
1768
1769/**
Tejun Heo0aa11132008-04-07 22:47:18 +09001770 * ata_sff_prereset - prepare SFF link for reset
1771 * @link: SFF link to be reset
1772 * @deadline: deadline jiffies for the operation
1773 *
1774 * SFF link @link is about to be reset. Initialize it. It first
1775 * calls ata_std_prereset() and wait for !BSY if the port is
1776 * being softreset.
1777 *
1778 * LOCKING:
1779 * Kernel thread context (may sleep)
1780 *
1781 * RETURNS:
1782 * 0 on success, -errno otherwise.
1783 */
1784int ata_sff_prereset(struct ata_link *link, unsigned long deadline)
1785{
Tejun Heo0aa11132008-04-07 22:47:18 +09001786 struct ata_eh_context *ehc = &link->eh_context;
1787 int rc;
1788
1789 rc = ata_std_prereset(link, deadline);
1790 if (rc)
1791 return rc;
1792
1793 /* if we're about to do hardreset, nothing more to do */
1794 if (ehc->i.action & ATA_EH_HARDRESET)
1795 return 0;
1796
1797 /* wait for !BSY if we don't know that no device is attached */
1798 if (!ata_link_offline(link)) {
Tejun Heo705e76b2008-04-07 22:47:19 +09001799 rc = ata_sff_wait_ready(link, deadline);
Tejun Heo0aa11132008-04-07 22:47:18 +09001800 if (rc && rc != -ENODEV) {
1801 ata_link_printk(link, KERN_WARNING, "device not ready "
1802 "(errno=%d), forcing hardreset\n", rc);
1803 ehc->i.action |= ATA_EH_HARDRESET;
1804 }
1805 }
1806
1807 return 0;
1808}
1809
1810/**
Tejun Heo624d5c52008-03-25 22:16:41 +09001811 * ata_devchk - PATA device presence detection
1812 * @ap: ATA channel to examine
1813 * @device: Device to examine (starting at zero)
1814 *
1815 * This technique was originally described in
1816 * Hale Landis's ATADRVR (www.ata-atapi.com), and
1817 * later found its way into the ATA/ATAPI spec.
1818 *
1819 * Write a pattern to the ATA shadow registers,
1820 * and if a device is present, it will respond by
1821 * correctly storing and echoing back the
1822 * ATA shadow register contents.
1823 *
1824 * LOCKING:
1825 * caller.
1826 */
1827static unsigned int ata_devchk(struct ata_port *ap, unsigned int device)
1828{
1829 struct ata_ioports *ioaddr = &ap->ioaddr;
1830 u8 nsect, lbal;
1831
Tejun Heo5682ed32008-04-07 22:47:16 +09001832 ap->ops->sff_dev_select(ap, device);
Tejun Heo624d5c52008-03-25 22:16:41 +09001833
1834 iowrite8(0x55, ioaddr->nsect_addr);
1835 iowrite8(0xaa, ioaddr->lbal_addr);
1836
1837 iowrite8(0xaa, ioaddr->nsect_addr);
1838 iowrite8(0x55, ioaddr->lbal_addr);
1839
1840 iowrite8(0x55, ioaddr->nsect_addr);
1841 iowrite8(0xaa, ioaddr->lbal_addr);
1842
1843 nsect = ioread8(ioaddr->nsect_addr);
1844 lbal = ioread8(ioaddr->lbal_addr);
1845
1846 if ((nsect == 0x55) && (lbal == 0xaa))
1847 return 1; /* we found a device */
1848
1849 return 0; /* nothing found */
1850}
1851
1852/**
Tejun Heo9363c382008-04-07 22:47:16 +09001853 * ata_sff_dev_classify - Parse returned ATA device signature
Tejun Heo624d5c52008-03-25 22:16:41 +09001854 * @dev: ATA device to classify (starting at zero)
1855 * @present: device seems present
1856 * @r_err: Value of error register on completion
1857 *
1858 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
1859 * an ATA/ATAPI-defined set of values is placed in the ATA
1860 * shadow registers, indicating the results of device detection
1861 * and diagnostics.
1862 *
1863 * Select the ATA device, and read the values from the ATA shadow
1864 * registers. Then parse according to the Error register value,
1865 * and the spec-defined values examined by ata_dev_classify().
1866 *
1867 * LOCKING:
1868 * caller.
1869 *
1870 * RETURNS:
1871 * Device type - %ATA_DEV_ATA, %ATA_DEV_ATAPI or %ATA_DEV_NONE.
1872 */
Tejun Heo9363c382008-04-07 22:47:16 +09001873unsigned int ata_sff_dev_classify(struct ata_device *dev, int present,
Tejun Heo624d5c52008-03-25 22:16:41 +09001874 u8 *r_err)
1875{
1876 struct ata_port *ap = dev->link->ap;
1877 struct ata_taskfile tf;
1878 unsigned int class;
1879 u8 err;
1880
Tejun Heo5682ed32008-04-07 22:47:16 +09001881 ap->ops->sff_dev_select(ap, dev->devno);
Tejun Heo624d5c52008-03-25 22:16:41 +09001882
1883 memset(&tf, 0, sizeof(tf));
1884
Tejun Heo5682ed32008-04-07 22:47:16 +09001885 ap->ops->sff_tf_read(ap, &tf);
Tejun Heo624d5c52008-03-25 22:16:41 +09001886 err = tf.feature;
1887 if (r_err)
1888 *r_err = err;
1889
1890 /* see if device passed diags: continue and warn later */
1891 if (err == 0)
1892 /* diagnostic fail : do nothing _YET_ */
1893 dev->horkage |= ATA_HORKAGE_DIAGNOSTIC;
1894 else if (err == 1)
1895 /* do nothing */ ;
1896 else if ((dev->devno == 0) && (err == 0x81))
1897 /* do nothing */ ;
1898 else
1899 return ATA_DEV_NONE;
1900
1901 /* determine if device is ATA or ATAPI */
1902 class = ata_dev_classify(&tf);
1903
1904 if (class == ATA_DEV_UNKNOWN) {
1905 /* If the device failed diagnostic, it's likely to
1906 * have reported incorrect device signature too.
1907 * Assume ATA device if the device seems present but
1908 * device signature is invalid with diagnostic
1909 * failure.
1910 */
1911 if (present && (dev->horkage & ATA_HORKAGE_DIAGNOSTIC))
1912 class = ATA_DEV_ATA;
1913 else
1914 class = ATA_DEV_NONE;
Tejun Heo5682ed32008-04-07 22:47:16 +09001915 } else if ((class == ATA_DEV_ATA) &&
1916 (ap->ops->sff_check_status(ap) == 0))
Tejun Heo624d5c52008-03-25 22:16:41 +09001917 class = ATA_DEV_NONE;
1918
1919 return class;
1920}
1921
Tejun Heo705e76b2008-04-07 22:47:19 +09001922/**
1923 * ata_sff_wait_after_reset - wait for devices to become ready after reset
1924 * @link: SFF link which is just reset
1925 * @devmask: mask of present devices
1926 * @deadline: deadline jiffies for the operation
1927 *
1928 * Wait devices attached to SFF @link to become ready after
1929 * reset. It contains preceding 150ms wait to avoid accessing TF
1930 * status register too early.
1931 *
1932 * LOCKING:
1933 * Kernel thread context (may sleep).
1934 *
1935 * RETURNS:
1936 * 0 on success, -ENODEV if some or all of devices in @devmask
1937 * don't seem to exist. -errno on other errors.
1938 */
1939int ata_sff_wait_after_reset(struct ata_link *link, unsigned int devmask,
1940 unsigned long deadline)
Tejun Heo624d5c52008-03-25 22:16:41 +09001941{
Tejun Heo705e76b2008-04-07 22:47:19 +09001942 struct ata_port *ap = link->ap;
Tejun Heo624d5c52008-03-25 22:16:41 +09001943 struct ata_ioports *ioaddr = &ap->ioaddr;
1944 unsigned int dev0 = devmask & (1 << 0);
1945 unsigned int dev1 = devmask & (1 << 1);
1946 int rc, ret = 0;
1947
Tejun Heo341c2c92008-05-20 02:17:51 +09001948 msleep(ATA_WAIT_AFTER_RESET);
Tejun Heo705e76b2008-04-07 22:47:19 +09001949
1950 /* always check readiness of the master device */
1951 rc = ata_sff_wait_ready(link, deadline);
1952 /* -ENODEV means the odd clown forgot the D7 pulldown resistor
1953 * and TF status is 0xff, bail out on it too.
Tejun Heo624d5c52008-03-25 22:16:41 +09001954 */
Tejun Heo705e76b2008-04-07 22:47:19 +09001955 if (rc)
1956 return rc;
Tejun Heo624d5c52008-03-25 22:16:41 +09001957
1958 /* if device 1 was found in ata_devchk, wait for register
1959 * access briefly, then wait for BSY to clear.
1960 */
1961 if (dev1) {
1962 int i;
1963
Tejun Heo5682ed32008-04-07 22:47:16 +09001964 ap->ops->sff_dev_select(ap, 1);
Tejun Heo624d5c52008-03-25 22:16:41 +09001965
1966 /* Wait for register access. Some ATAPI devices fail
1967 * to set nsect/lbal after reset, so don't waste too
1968 * much time on it. We're gonna wait for !BSY anyway.
1969 */
1970 for (i = 0; i < 2; i++) {
1971 u8 nsect, lbal;
1972
1973 nsect = ioread8(ioaddr->nsect_addr);
1974 lbal = ioread8(ioaddr->lbal_addr);
1975 if ((nsect == 1) && (lbal == 1))
1976 break;
1977 msleep(50); /* give drive a breather */
1978 }
1979
Tejun Heo705e76b2008-04-07 22:47:19 +09001980 rc = ata_sff_wait_ready(link, deadline);
Tejun Heo624d5c52008-03-25 22:16:41 +09001981 if (rc) {
1982 if (rc != -ENODEV)
1983 return rc;
1984 ret = rc;
1985 }
1986 }
1987
1988 /* is all this really necessary? */
Tejun Heo5682ed32008-04-07 22:47:16 +09001989 ap->ops->sff_dev_select(ap, 0);
Tejun Heo624d5c52008-03-25 22:16:41 +09001990 if (dev1)
Tejun Heo5682ed32008-04-07 22:47:16 +09001991 ap->ops->sff_dev_select(ap, 1);
Tejun Heo624d5c52008-03-25 22:16:41 +09001992 if (dev0)
Tejun Heo5682ed32008-04-07 22:47:16 +09001993 ap->ops->sff_dev_select(ap, 0);
Tejun Heo624d5c52008-03-25 22:16:41 +09001994
1995 return ret;
1996}
1997
Tejun Heo624d5c52008-03-25 22:16:41 +09001998static int ata_bus_softreset(struct ata_port *ap, unsigned int devmask,
1999 unsigned long deadline)
2000{
2001 struct ata_ioports *ioaddr = &ap->ioaddr;
2002
2003 DPRINTK("ata%u: bus reset via SRST\n", ap->print_id);
2004
2005 /* software reset. causes dev0 to be selected */
2006 iowrite8(ap->ctl, ioaddr->ctl_addr);
2007 udelay(20); /* FIXME: flush */
2008 iowrite8(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
2009 udelay(20); /* FIXME: flush */
2010 iowrite8(ap->ctl, ioaddr->ctl_addr);
2011
Tejun Heo705e76b2008-04-07 22:47:19 +09002012 /* wait the port to become ready */
2013 return ata_sff_wait_after_reset(&ap->link, devmask, deadline);
Tejun Heo624d5c52008-03-25 22:16:41 +09002014}
2015
2016/**
Tejun Heo9363c382008-04-07 22:47:16 +09002017 * ata_sff_softreset - reset host port via ATA SRST
Tejun Heo624d5c52008-03-25 22:16:41 +09002018 * @link: ATA link to reset
2019 * @classes: resulting classes of attached devices
2020 * @deadline: deadline jiffies for the operation
2021 *
2022 * Reset host port using ATA SRST.
2023 *
2024 * LOCKING:
2025 * Kernel thread context (may sleep)
2026 *
2027 * RETURNS:
2028 * 0 on success, -errno otherwise.
2029 */
Tejun Heo9363c382008-04-07 22:47:16 +09002030int ata_sff_softreset(struct ata_link *link, unsigned int *classes,
Tejun Heo624d5c52008-03-25 22:16:41 +09002031 unsigned long deadline)
2032{
2033 struct ata_port *ap = link->ap;
2034 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2035 unsigned int devmask = 0;
2036 int rc;
2037 u8 err;
2038
2039 DPRINTK("ENTER\n");
2040
Tejun Heo624d5c52008-03-25 22:16:41 +09002041 /* determine if device 0/1 are present */
2042 if (ata_devchk(ap, 0))
2043 devmask |= (1 << 0);
2044 if (slave_possible && ata_devchk(ap, 1))
2045 devmask |= (1 << 1);
2046
2047 /* select device 0 again */
Tejun Heo5682ed32008-04-07 22:47:16 +09002048 ap->ops->sff_dev_select(ap, 0);
Tejun Heo624d5c52008-03-25 22:16:41 +09002049
2050 /* issue bus reset */
2051 DPRINTK("about to softreset, devmask=%x\n", devmask);
2052 rc = ata_bus_softreset(ap, devmask, deadline);
2053 /* if link is occupied, -ENODEV too is an error */
2054 if (rc && (rc != -ENODEV || sata_scr_valid(link))) {
2055 ata_link_printk(link, KERN_ERR, "SRST failed (errno=%d)\n", rc);
2056 return rc;
2057 }
2058
2059 /* determine by signature whether we have ATA or ATAPI devices */
Tejun Heo9363c382008-04-07 22:47:16 +09002060 classes[0] = ata_sff_dev_classify(&link->device[0],
Tejun Heo624d5c52008-03-25 22:16:41 +09002061 devmask & (1 << 0), &err);
2062 if (slave_possible && err != 0x81)
Tejun Heo9363c382008-04-07 22:47:16 +09002063 classes[1] = ata_sff_dev_classify(&link->device[1],
Tejun Heo624d5c52008-03-25 22:16:41 +09002064 devmask & (1 << 1), &err);
2065
Tejun Heo624d5c52008-03-25 22:16:41 +09002066 DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]);
2067 return 0;
2068}
2069
2070/**
Tejun Heo9363c382008-04-07 22:47:16 +09002071 * sata_sff_hardreset - reset host port via SATA phy reset
Tejun Heo624d5c52008-03-25 22:16:41 +09002072 * @link: link to reset
2073 * @class: resulting class of attached device
2074 * @deadline: deadline jiffies for the operation
2075 *
2076 * SATA phy-reset host port using DET bits of SControl register,
2077 * wait for !BSY and classify the attached device.
2078 *
2079 * LOCKING:
2080 * Kernel thread context (may sleep)
2081 *
2082 * RETURNS:
2083 * 0 on success, -errno otherwise.
2084 */
Tejun Heo9363c382008-04-07 22:47:16 +09002085int sata_sff_hardreset(struct ata_link *link, unsigned int *class,
Tejun Heo624d5c52008-03-25 22:16:41 +09002086 unsigned long deadline)
2087{
Tejun Heo9dadd452008-04-07 22:47:19 +09002088 struct ata_eh_context *ehc = &link->eh_context;
2089 const unsigned long *timing = sata_ehc_deb_timing(ehc);
2090 bool online;
Tejun Heo624d5c52008-03-25 22:16:41 +09002091 int rc;
2092
Tejun Heo9dadd452008-04-07 22:47:19 +09002093 rc = sata_link_hardreset(link, timing, deadline, &online,
2094 ata_sff_check_ready);
Tejun Heo9dadd452008-04-07 22:47:19 +09002095 if (online)
2096 *class = ata_sff_dev_classify(link->device, 1, NULL);
Tejun Heo624d5c52008-03-25 22:16:41 +09002097
2098 DPRINTK("EXIT, class=%u\n", *class);
Tejun Heo9dadd452008-04-07 22:47:19 +09002099 return rc;
Tejun Heo624d5c52008-03-25 22:16:41 +09002100}
2101
2102/**
Tejun Heo203c75b2008-04-07 22:47:18 +09002103 * ata_sff_postreset - SFF postreset callback
2104 * @link: the target SFF ata_link
2105 * @classes: classes of attached devices
2106 *
2107 * This function is invoked after a successful reset. It first
2108 * calls ata_std_postreset() and performs SFF specific postreset
2109 * processing.
2110 *
2111 * LOCKING:
2112 * Kernel thread context (may sleep)
2113 */
2114void ata_sff_postreset(struct ata_link *link, unsigned int *classes)
2115{
2116 struct ata_port *ap = link->ap;
2117
2118 ata_std_postreset(link, classes);
2119
2120 /* is double-select really necessary? */
2121 if (classes[0] != ATA_DEV_NONE)
2122 ap->ops->sff_dev_select(ap, 1);
2123 if (classes[1] != ATA_DEV_NONE)
2124 ap->ops->sff_dev_select(ap, 0);
2125
2126 /* bail out if no device is present */
2127 if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
2128 DPRINTK("EXIT, no device\n");
2129 return;
2130 }
2131
2132 /* set up device control */
2133 if (ap->ioaddr.ctl_addr)
2134 iowrite8(ap->ctl, ap->ioaddr.ctl_addr);
2135}
2136
2137/**
Tejun Heo9363c382008-04-07 22:47:16 +09002138 * ata_sff_error_handler - Stock error handler for BMDMA controller
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002139 * @ap: port to handle error for
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002140 *
Tejun Heo9363c382008-04-07 22:47:16 +09002141 * Stock error handler for SFF controller. It can handle both
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002142 * PATA and SATA controllers. Many controllers should be able to
2143 * use this EH as-is or with some added handling before and
2144 * after.
2145 *
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002146 * LOCKING:
2147 * Kernel thread context (may sleep)
2148 */
Tejun Heo9363c382008-04-07 22:47:16 +09002149void ata_sff_error_handler(struct ata_port *ap)
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002150{
Tejun Heoa1efdab2008-03-25 12:22:50 +09002151 ata_reset_fn_t softreset = ap->ops->softreset;
2152 ata_reset_fn_t hardreset = ap->ops->hardreset;
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002153 struct ata_queued_cmd *qc;
2154 unsigned long flags;
2155 int thaw = 0;
2156
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002157 qc = __ata_qc_from_tag(ap, ap->link.active_tag);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002158 if (qc && !(qc->flags & ATA_QCFLAG_FAILED))
2159 qc = NULL;
2160
2161 /* reset PIO HSM and stop DMA engine */
Jeff Garzikba6a1302006-06-22 23:46:10 -04002162 spin_lock_irqsave(ap->lock, flags);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002163
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002164 ap->hsm_task_state = HSM_ST_IDLE;
2165
Tejun Heoed82f962008-03-25 21:34:39 +09002166 if (ap->ioaddr.bmdma_addr &&
2167 qc && (qc->tf.protocol == ATA_PROT_DMA ||
Tejun Heo0dc36882007-12-18 16:34:43 -05002168 qc->tf.protocol == ATAPI_PROT_DMA)) {
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002169 u8 host_stat;
2170
Robert Hancockfbbb2622006-10-27 19:08:41 -07002171 host_stat = ap->ops->bmdma_status(ap);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002172
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002173 /* BMDMA controllers indicate host bus error by
2174 * setting DMA_ERR bit and timing out. As it wasn't
2175 * really a timeout event, adjust error mask and
2176 * cancel frozen state.
2177 */
Alan18d90de2007-01-24 11:42:38 +00002178 if (qc->err_mask == AC_ERR_TIMEOUT && (host_stat & ATA_DMA_ERR)) {
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002179 qc->err_mask = AC_ERR_HOST_BUS;
2180 thaw = 1;
2181 }
2182
2183 ap->ops->bmdma_stop(qc);
2184 }
2185
Alan Coxa57c1ba2008-05-29 22:10:58 +01002186 ata_sff_sync(ap); /* FIXME: We don't need this */
Tejun Heo5682ed32008-04-07 22:47:16 +09002187 ap->ops->sff_check_status(ap);
2188 ap->ops->sff_irq_clear(ap);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002189
Jeff Garzikba6a1302006-06-22 23:46:10 -04002190 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002191
2192 if (thaw)
2193 ata_eh_thaw_port(ap);
2194
2195 /* PIO and DMA engines have been stopped, perform recovery */
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002196
Tejun Heo57c9efd2008-04-07 22:47:19 +09002197 /* Ignore ata_sff_softreset if ctl isn't accessible and
2198 * built-in hardresets if SCR access isn't available.
Tejun Heoa1efdab2008-03-25 12:22:50 +09002199 */
Tejun Heo9363c382008-04-07 22:47:16 +09002200 if (softreset == ata_sff_softreset && !ap->ioaddr.ctl_addr)
Tejun Heoa1efdab2008-03-25 12:22:50 +09002201 softreset = NULL;
Tejun Heo57c9efd2008-04-07 22:47:19 +09002202 if (ata_is_builtin_hardreset(hardreset) && !sata_scr_valid(&ap->link))
Tejun Heoa1efdab2008-03-25 12:22:50 +09002203 hardreset = NULL;
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002204
Tejun Heoa1efdab2008-03-25 12:22:50 +09002205 ata_do_eh(ap, ap->ops->prereset, softreset, hardreset,
2206 ap->ops->postreset);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002207}
2208
2209/**
Tejun Heo9363c382008-04-07 22:47:16 +09002210 * ata_sff_post_internal_cmd - Stock post_internal_cmd for SFF controller
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002211 * @qc: internal command to clean up
2212 *
2213 * LOCKING:
2214 * Kernel thread context (may sleep)
2215 */
Tejun Heo9363c382008-04-07 22:47:16 +09002216void ata_sff_post_internal_cmd(struct ata_queued_cmd *qc)
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002217{
Tejun Heo570106d2008-10-20 13:10:21 +09002218 struct ata_port *ap = qc->ap;
2219 unsigned long flags;
2220
2221 spin_lock_irqsave(ap->lock, flags);
2222
2223 ap->hsm_task_state = HSM_ST_IDLE;
2224
2225 if (ap->ioaddr.bmdma_addr)
Alan61dd08c2007-01-25 15:09:05 +00002226 ata_bmdma_stop(qc);
Tejun Heo570106d2008-10-20 13:10:21 +09002227
2228 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002229}
2230
Alan Coxd92e74d2007-06-07 16:19:15 +01002231/**
2232 * ata_sff_port_start - Set port up for dma.
2233 * @ap: Port to initialize
2234 *
2235 * Called just after data structures for each port are
2236 * initialized. Allocates space for PRD table if the device
2237 * is DMA capable SFF.
2238 *
2239 * May be used as the port_start() entry in ata_port_operations.
2240 *
2241 * LOCKING:
2242 * Inherited from caller.
2243 */
Alan Coxd92e74d2007-06-07 16:19:15 +01002244int ata_sff_port_start(struct ata_port *ap)
2245{
2246 if (ap->ioaddr.bmdma_addr)
2247 return ata_port_start(ap);
2248 return 0;
2249}
2250
Tejun Heo272f7882008-03-25 22:16:40 +09002251/**
Tejun Heo9363c382008-04-07 22:47:16 +09002252 * ata_sff_std_ports - initialize ioaddr with standard port offsets.
Tejun Heo624d5c52008-03-25 22:16:41 +09002253 * @ioaddr: IO address structure to be initialized
2254 *
2255 * Utility function which initializes data_addr, error_addr,
2256 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
2257 * device_addr, status_addr, and command_addr to standard offsets
2258 * relative to cmd_addr.
2259 *
2260 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
2261 */
Tejun Heo9363c382008-04-07 22:47:16 +09002262void ata_sff_std_ports(struct ata_ioports *ioaddr)
Tejun Heo624d5c52008-03-25 22:16:41 +09002263{
2264 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
2265 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
2266 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
2267 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
2268 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
2269 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
2270 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
2271 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
2272 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
2273 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
2274}
2275
Tejun Heo9363c382008-04-07 22:47:16 +09002276unsigned long ata_bmdma_mode_filter(struct ata_device *adev,
2277 unsigned long xfer_mask)
Tejun Heo071ce342008-03-25 22:16:42 +09002278{
2279 /* Filter out DMA modes if the device has been configured by
2280 the BIOS as PIO only */
2281
2282 if (adev->link->ap->ioaddr.bmdma_addr == NULL)
2283 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
2284 return xfer_mask;
2285}
2286
Tejun Heo624d5c52008-03-25 22:16:41 +09002287/**
Tejun Heo272f7882008-03-25 22:16:40 +09002288 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
2289 * @qc: Info associated with this ATA transaction.
2290 *
2291 * LOCKING:
2292 * spin_lock_irqsave(host lock)
2293 */
2294void ata_bmdma_setup(struct ata_queued_cmd *qc)
2295{
2296 struct ata_port *ap = qc->ap;
2297 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
2298 u8 dmactl;
2299
2300 /* load PRD table addr. */
2301 mb(); /* make sure PRD table writes are visible to controller */
2302 iowrite32(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
2303
2304 /* specify data direction, triple-check start bit is clear */
2305 dmactl = ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
2306 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
2307 if (!rw)
2308 dmactl |= ATA_DMA_WR;
2309 iowrite8(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
2310
2311 /* issue r/w command */
Tejun Heo5682ed32008-04-07 22:47:16 +09002312 ap->ops->sff_exec_command(ap, &qc->tf);
Tejun Heo272f7882008-03-25 22:16:40 +09002313}
2314
2315/**
2316 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
2317 * @qc: Info associated with this ATA transaction.
2318 *
2319 * LOCKING:
2320 * spin_lock_irqsave(host lock)
2321 */
2322void ata_bmdma_start(struct ata_queued_cmd *qc)
2323{
2324 struct ata_port *ap = qc->ap;
2325 u8 dmactl;
2326
2327 /* start host DMA transaction */
2328 dmactl = ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
2329 iowrite8(dmactl | ATA_DMA_START, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
2330
2331 /* Strictly, one may wish to issue an ioread8() here, to
2332 * flush the mmio write. However, control also passes
2333 * to the hardware at this point, and it will interrupt
2334 * us when we are to resume control. So, in effect,
2335 * we don't care when the mmio write flushes.
2336 * Further, a read of the DMA status register _immediately_
2337 * following the write may not be what certain flaky hardware
2338 * is expected, so I think it is best to not add a readb()
2339 * without first all the MMIO ATA cards/mobos.
2340 * Or maybe I'm just being paranoid.
2341 *
2342 * FIXME: The posting of this write means I/O starts are
2343 * unneccessarily delayed for MMIO
2344 */
2345}
2346
2347/**
2348 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
2349 * @qc: Command we are ending DMA for
2350 *
2351 * Clears the ATA_DMA_START flag in the dma control register
2352 *
2353 * May be used as the bmdma_stop() entry in ata_port_operations.
2354 *
2355 * LOCKING:
2356 * spin_lock_irqsave(host lock)
2357 */
2358void ata_bmdma_stop(struct ata_queued_cmd *qc)
2359{
2360 struct ata_port *ap = qc->ap;
2361 void __iomem *mmio = ap->ioaddr.bmdma_addr;
2362
2363 /* clear start/stop bit */
2364 iowrite8(ioread8(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
2365 mmio + ATA_DMA_CMD);
2366
2367 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
Alan Coxa57c1ba2008-05-29 22:10:58 +01002368 ata_sff_dma_pause(ap);
Tejun Heo272f7882008-03-25 22:16:40 +09002369}
2370
2371/**
2372 * ata_bmdma_status - Read PCI IDE BMDMA status
2373 * @ap: Port associated with this ATA transaction.
2374 *
2375 * Read and return BMDMA status register.
2376 *
2377 * May be used as the bmdma_status() entry in ata_port_operations.
2378 *
2379 * LOCKING:
2380 * spin_lock_irqsave(host lock)
2381 */
2382u8 ata_bmdma_status(struct ata_port *ap)
2383{
2384 return ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
2385}
2386
2387/**
Tejun Heo624d5c52008-03-25 22:16:41 +09002388 * ata_bus_reset - reset host port and associated ATA channel
2389 * @ap: port to reset
2390 *
2391 * This is typically the first time we actually start issuing
2392 * commands to the ATA channel. We wait for BSY to clear, then
2393 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
2394 * result. Determine what devices, if any, are on the channel
2395 * by looking at the device 0/1 error register. Look at the signature
2396 * stored in each device's taskfile registers, to determine if
2397 * the device is ATA or ATAPI.
2398 *
2399 * LOCKING:
2400 * PCI/etc. bus probe sem.
2401 * Obtains host lock.
2402 *
2403 * SIDE EFFECTS:
2404 * Sets ATA_FLAG_DISABLED if bus reset fails.
2405 *
2406 * DEPRECATED:
2407 * This function is only for drivers which still use old EH and
2408 * will be removed soon.
Tejun Heo272f7882008-03-25 22:16:40 +09002409 */
Tejun Heo624d5c52008-03-25 22:16:41 +09002410void ata_bus_reset(struct ata_port *ap)
Tejun Heo272f7882008-03-25 22:16:40 +09002411{
Tejun Heo624d5c52008-03-25 22:16:41 +09002412 struct ata_device *device = ap->link.device;
2413 struct ata_ioports *ioaddr = &ap->ioaddr;
2414 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2415 u8 err;
2416 unsigned int dev0, dev1 = 0, devmask = 0;
2417 int rc;
2418
2419 DPRINTK("ENTER, host %u, port %u\n", ap->print_id, ap->port_no);
2420
2421 /* determine if device 0/1 are present */
2422 if (ap->flags & ATA_FLAG_SATA_RESET)
2423 dev0 = 1;
2424 else {
2425 dev0 = ata_devchk(ap, 0);
2426 if (slave_possible)
2427 dev1 = ata_devchk(ap, 1);
2428 }
2429
2430 if (dev0)
2431 devmask |= (1 << 0);
2432 if (dev1)
2433 devmask |= (1 << 1);
2434
2435 /* select device 0 again */
Tejun Heo5682ed32008-04-07 22:47:16 +09002436 ap->ops->sff_dev_select(ap, 0);
Tejun Heo624d5c52008-03-25 22:16:41 +09002437
2438 /* issue bus reset */
2439 if (ap->flags & ATA_FLAG_SRST) {
Tejun Heo341c2c92008-05-20 02:17:51 +09002440 rc = ata_bus_softreset(ap, devmask,
2441 ata_deadline(jiffies, 40000));
Tejun Heo624d5c52008-03-25 22:16:41 +09002442 if (rc && rc != -ENODEV)
2443 goto err_out;
2444 }
2445
2446 /*
2447 * determine by signature whether we have ATA or ATAPI devices
2448 */
Tejun Heo9363c382008-04-07 22:47:16 +09002449 device[0].class = ata_sff_dev_classify(&device[0], dev0, &err);
Tejun Heo624d5c52008-03-25 22:16:41 +09002450 if ((slave_possible) && (err != 0x81))
Tejun Heo9363c382008-04-07 22:47:16 +09002451 device[1].class = ata_sff_dev_classify(&device[1], dev1, &err);
Tejun Heo624d5c52008-03-25 22:16:41 +09002452
2453 /* is double-select really necessary? */
2454 if (device[1].class != ATA_DEV_NONE)
Tejun Heo5682ed32008-04-07 22:47:16 +09002455 ap->ops->sff_dev_select(ap, 1);
Tejun Heo624d5c52008-03-25 22:16:41 +09002456 if (device[0].class != ATA_DEV_NONE)
Tejun Heo5682ed32008-04-07 22:47:16 +09002457 ap->ops->sff_dev_select(ap, 0);
Tejun Heo624d5c52008-03-25 22:16:41 +09002458
2459 /* if no devices were detected, disable this port */
2460 if ((device[0].class == ATA_DEV_NONE) &&
2461 (device[1].class == ATA_DEV_NONE))
2462 goto err_out;
2463
2464 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
2465 /* set up device control for ATA_FLAG_SATA_RESET */
2466 iowrite8(ap->ctl, ioaddr->ctl_addr);
2467 }
2468
2469 DPRINTK("EXIT\n");
2470 return;
2471
2472err_out:
2473 ata_port_printk(ap, KERN_ERR, "disabling port\n");
2474 ata_port_disable(ap);
2475
2476 DPRINTK("EXIT\n");
Tejun Heo272f7882008-03-25 22:16:40 +09002477}
2478
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002479#ifdef CONFIG_PCI
Alan4112e162007-01-08 12:10:05 +00002480
Tejun Heo272f7882008-03-25 22:16:40 +09002481/**
Tejun Heo9363c382008-04-07 22:47:16 +09002482 * ata_pci_bmdma_clear_simplex - attempt to kick device out of simplex
Tejun Heo272f7882008-03-25 22:16:40 +09002483 * @pdev: PCI device
2484 *
2485 * Some PCI ATA devices report simplex mode but in fact can be told to
2486 * enter non simplex mode. This implements the necessary logic to
2487 * perform the task on such devices. Calling it on other devices will
2488 * have -undefined- behaviour.
2489 */
Tejun Heo9363c382008-04-07 22:47:16 +09002490int ata_pci_bmdma_clear_simplex(struct pci_dev *pdev)
Alan4112e162007-01-08 12:10:05 +00002491{
Tejun Heo272f7882008-03-25 22:16:40 +09002492 unsigned long bmdma = pci_resource_start(pdev, 4);
2493 u8 simplex;
Jeff Garzika84471f2007-02-26 05:51:33 -05002494
Tejun Heo272f7882008-03-25 22:16:40 +09002495 if (bmdma == 0)
2496 return -ENOENT;
2497
2498 simplex = inb(bmdma + 0x02);
2499 outb(simplex & 0x60, bmdma + 0x02);
2500 simplex = inb(bmdma + 0x02);
2501 if (simplex & 0x80)
2502 return -EOPNOTSUPP;
2503 return 0;
2504}
2505
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002506/**
Tejun Heo9363c382008-04-07 22:47:16 +09002507 * ata_pci_bmdma_init - acquire PCI BMDMA resources and init ATA host
Tejun Heo0f834de2007-04-17 23:44:07 +09002508 * @host: target ATA host
2509 *
2510 * Acquire PCI BMDMA resources and initialize @host accordingly.
2511 *
2512 * LOCKING:
2513 * Inherited from calling layer (may sleep).
2514 *
2515 * RETURNS:
2516 * 0 on success, -errno otherwise.
2517 */
Tejun Heo9363c382008-04-07 22:47:16 +09002518int ata_pci_bmdma_init(struct ata_host *host)
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002519{
Tejun Heo0f834de2007-04-17 23:44:07 +09002520 struct device *gdev = host->dev;
2521 struct pci_dev *pdev = to_pci_dev(gdev);
2522 int i, rc;
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002523
Alan Cox6fdc99a2007-07-26 18:41:30 +01002524 /* No BAR4 allocation: No DMA */
2525 if (pci_resource_start(pdev, 4) == 0)
2526 return 0;
2527
Tejun Heo0f834de2007-04-17 23:44:07 +09002528 /* TODO: If we get no DMA mask we should fall back to PIO */
2529 rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
2530 if (rc)
2531 return rc;
2532 rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
2533 if (rc)
2534 return rc;
2535
2536 /* request and iomap DMA region */
Tejun Heo35a10a82008-01-04 18:42:21 +09002537 rc = pcim_iomap_regions(pdev, 1 << 4, dev_driver_string(gdev));
Tejun Heo0f834de2007-04-17 23:44:07 +09002538 if (rc) {
2539 dev_printk(KERN_ERR, gdev, "failed to request/iomap BAR4\n");
2540 return -ENOMEM;
2541 }
2542 host->iomap = pcim_iomap_table(pdev);
2543
Tejun Heo1626aeb2007-05-04 12:43:58 +02002544 for (i = 0; i < 2; i++) {
Tejun Heo0f834de2007-04-17 23:44:07 +09002545 struct ata_port *ap = host->ports[i];
Tejun Heo0f834de2007-04-17 23:44:07 +09002546 void __iomem *bmdma = host->iomap[4] + 8 * i;
2547
2548 if (ata_port_is_dummy(ap))
2549 continue;
2550
Tejun Heo21b0ad42007-04-17 23:44:07 +09002551 ap->ioaddr.bmdma_addr = bmdma;
Tejun Heo0f834de2007-04-17 23:44:07 +09002552 if ((!(ap->flags & ATA_FLAG_IGN_SIMPLEX)) &&
2553 (ioread8(bmdma + 2) & 0x80))
2554 host->flags |= ATA_HOST_SIMPLEX;
Tejun Heocbcdd872007-08-18 13:14:55 +09002555
2556 ata_port_desc(ap, "bmdma 0x%llx",
2557 (unsigned long long)pci_resource_start(pdev, 4) + 8 * i);
Tejun Heo0d5ff562007-02-01 15:06:36 +09002558 }
2559
Tejun Heo0f834de2007-04-17 23:44:07 +09002560 return 0;
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002561}
2562
Tejun Heo272f7882008-03-25 22:16:40 +09002563static int ata_resources_present(struct pci_dev *pdev, int port)
2564{
2565 int i;
2566
2567 /* Check the PCI resources for this channel are enabled */
2568 port = port * 2;
2569 for (i = 0; i < 2; i ++) {
2570 if (pci_resource_start(pdev, port + i) == 0 ||
2571 pci_resource_len(pdev, port + i) == 0)
2572 return 0;
2573 }
2574 return 1;
2575}
2576
Tejun Heod491b272007-04-17 23:44:07 +09002577/**
Tejun Heo9363c382008-04-07 22:47:16 +09002578 * ata_pci_sff_init_host - acquire native PCI ATA resources and init host
Tejun Heod491b272007-04-17 23:44:07 +09002579 * @host: target ATA host
Tejun Heod491b272007-04-17 23:44:07 +09002580 *
Tejun Heo1626aeb2007-05-04 12:43:58 +02002581 * Acquire native PCI ATA resources for @host and initialize the
2582 * first two ports of @host accordingly. Ports marked dummy are
2583 * skipped and allocation failure makes the port dummy.
Tejun Heod491b272007-04-17 23:44:07 +09002584 *
Tejun Heod583bc12007-07-04 18:02:07 +09002585 * Note that native PCI resources are valid even for legacy hosts
2586 * as we fix up pdev resources array early in boot, so this
2587 * function can be used for both native and legacy SFF hosts.
2588 *
Tejun Heod491b272007-04-17 23:44:07 +09002589 * LOCKING:
2590 * Inherited from calling layer (may sleep).
2591 *
2592 * RETURNS:
Tejun Heo1626aeb2007-05-04 12:43:58 +02002593 * 0 if at least one port is initialized, -ENODEV if no port is
2594 * available.
Tejun Heod491b272007-04-17 23:44:07 +09002595 */
Tejun Heo9363c382008-04-07 22:47:16 +09002596int ata_pci_sff_init_host(struct ata_host *host)
Tejun Heod491b272007-04-17 23:44:07 +09002597{
2598 struct device *gdev = host->dev;
2599 struct pci_dev *pdev = to_pci_dev(gdev);
Tejun Heo1626aeb2007-05-04 12:43:58 +02002600 unsigned int mask = 0;
Tejun Heod491b272007-04-17 23:44:07 +09002601 int i, rc;
2602
Tejun Heod491b272007-04-17 23:44:07 +09002603 /* request, iomap BARs and init port addresses accordingly */
2604 for (i = 0; i < 2; i++) {
2605 struct ata_port *ap = host->ports[i];
2606 int base = i * 2;
2607 void __iomem * const *iomap;
2608
Tejun Heo1626aeb2007-05-04 12:43:58 +02002609 if (ata_port_is_dummy(ap))
Tejun Heod491b272007-04-17 23:44:07 +09002610 continue;
2611
Tejun Heo1626aeb2007-05-04 12:43:58 +02002612 /* Discard disabled ports. Some controllers show
2613 * their unused channels this way. Disabled ports are
2614 * made dummy.
2615 */
2616 if (!ata_resources_present(pdev, i)) {
2617 ap->ops = &ata_dummy_port_ops;
2618 continue;
2619 }
2620
Tejun Heo35a10a82008-01-04 18:42:21 +09002621 rc = pcim_iomap_regions(pdev, 0x3 << base,
2622 dev_driver_string(gdev));
Tejun Heod491b272007-04-17 23:44:07 +09002623 if (rc) {
Tejun Heo1626aeb2007-05-04 12:43:58 +02002624 dev_printk(KERN_WARNING, gdev,
2625 "failed to request/iomap BARs for port %d "
2626 "(errno=%d)\n", i, rc);
Tejun Heod491b272007-04-17 23:44:07 +09002627 if (rc == -EBUSY)
2628 pcim_pin_device(pdev);
Tejun Heo1626aeb2007-05-04 12:43:58 +02002629 ap->ops = &ata_dummy_port_ops;
2630 continue;
Tejun Heod491b272007-04-17 23:44:07 +09002631 }
2632 host->iomap = iomap = pcim_iomap_table(pdev);
2633
2634 ap->ioaddr.cmd_addr = iomap[base];
2635 ap->ioaddr.altstatus_addr =
2636 ap->ioaddr.ctl_addr = (void __iomem *)
2637 ((unsigned long)iomap[base + 1] | ATA_PCI_CTL_OFS);
Tejun Heo9363c382008-04-07 22:47:16 +09002638 ata_sff_std_ports(&ap->ioaddr);
Tejun Heo1626aeb2007-05-04 12:43:58 +02002639
Tejun Heocbcdd872007-08-18 13:14:55 +09002640 ata_port_desc(ap, "cmd 0x%llx ctl 0x%llx",
2641 (unsigned long long)pci_resource_start(pdev, base),
2642 (unsigned long long)pci_resource_start(pdev, base + 1));
2643
Tejun Heo1626aeb2007-05-04 12:43:58 +02002644 mask |= 1 << i;
2645 }
2646
2647 if (!mask) {
2648 dev_printk(KERN_ERR, gdev, "no available native port\n");
2649 return -ENODEV;
Tejun Heod491b272007-04-17 23:44:07 +09002650 }
2651
2652 return 0;
2653}
2654
Tejun Heo21b0ad42007-04-17 23:44:07 +09002655/**
Tejun Heo9363c382008-04-07 22:47:16 +09002656 * ata_pci_sff_prepare_host - helper to prepare native PCI ATA host
Tejun Heo21b0ad42007-04-17 23:44:07 +09002657 * @pdev: target PCI device
Tejun Heo1626aeb2007-05-04 12:43:58 +02002658 * @ppi: array of port_info, must be enough for two ports
Tejun Heo21b0ad42007-04-17 23:44:07 +09002659 * @r_host: out argument for the initialized ATA host
2660 *
2661 * Helper to allocate ATA host for @pdev, acquire all native PCI
2662 * resources and initialize it accordingly in one go.
2663 *
2664 * LOCKING:
2665 * Inherited from calling layer (may sleep).
2666 *
2667 * RETURNS:
2668 * 0 on success, -errno otherwise.
2669 */
Tejun Heo9363c382008-04-07 22:47:16 +09002670int ata_pci_sff_prepare_host(struct pci_dev *pdev,
Tejun Heod583bc12007-07-04 18:02:07 +09002671 const struct ata_port_info * const * ppi,
2672 struct ata_host **r_host)
Tejun Heo21b0ad42007-04-17 23:44:07 +09002673{
2674 struct ata_host *host;
Tejun Heo21b0ad42007-04-17 23:44:07 +09002675 int rc;
2676
2677 if (!devres_open_group(&pdev->dev, NULL, GFP_KERNEL))
2678 return -ENOMEM;
2679
2680 host = ata_host_alloc_pinfo(&pdev->dev, ppi, 2);
2681 if (!host) {
2682 dev_printk(KERN_ERR, &pdev->dev,
2683 "failed to allocate ATA host\n");
2684 rc = -ENOMEM;
2685 goto err_out;
2686 }
2687
Tejun Heo9363c382008-04-07 22:47:16 +09002688 rc = ata_pci_sff_init_host(host);
Tejun Heo21b0ad42007-04-17 23:44:07 +09002689 if (rc)
2690 goto err_out;
2691
2692 /* init DMA related stuff */
Tejun Heo9363c382008-04-07 22:47:16 +09002693 rc = ata_pci_bmdma_init(host);
Tejun Heo21b0ad42007-04-17 23:44:07 +09002694 if (rc)
2695 goto err_bmdma;
2696
2697 devres_remove_group(&pdev->dev, NULL);
2698 *r_host = host;
2699 return 0;
2700
2701 err_bmdma:
2702 /* This is necessary because PCI and iomap resources are
2703 * merged and releasing the top group won't release the
2704 * acquired resources if some of those have been acquired
2705 * before entering this function.
2706 */
2707 pcim_iounmap_regions(pdev, 0xf);
2708 err_out:
2709 devres_release_group(&pdev->dev, NULL);
2710 return rc;
2711}
2712
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002713/**
Tejun Heo9363c382008-04-07 22:47:16 +09002714 * ata_pci_sff_activate_host - start SFF host, request IRQ and register it
Tejun Heo4e6b79f2008-01-18 18:36:28 +09002715 * @host: target SFF ATA host
2716 * @irq_handler: irq_handler used when requesting IRQ(s)
2717 * @sht: scsi_host_template to use when registering the host
2718 *
2719 * This is the counterpart of ata_host_activate() for SFF ATA
2720 * hosts. This separate helper is necessary because SFF hosts
2721 * use two separate interrupts in legacy mode.
2722 *
2723 * LOCKING:
2724 * Inherited from calling layer (may sleep).
2725 *
2726 * RETURNS:
2727 * 0 on success, -errno otherwise.
2728 */
Tejun Heo9363c382008-04-07 22:47:16 +09002729int ata_pci_sff_activate_host(struct ata_host *host,
Tejun Heo4e6b79f2008-01-18 18:36:28 +09002730 irq_handler_t irq_handler,
2731 struct scsi_host_template *sht)
2732{
2733 struct device *dev = host->dev;
2734 struct pci_dev *pdev = to_pci_dev(dev);
2735 const char *drv_name = dev_driver_string(host->dev);
2736 int legacy_mode = 0, rc;
2737
2738 rc = ata_host_start(host);
2739 if (rc)
2740 return rc;
2741
2742 if ((pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
2743 u8 tmp8, mask;
2744
2745 /* TODO: What if one channel is in native mode ... */
2746 pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
2747 mask = (1 << 2) | (1 << 0);
2748 if ((tmp8 & mask) != mask)
2749 legacy_mode = 1;
2750#if defined(CONFIG_NO_ATA_LEGACY)
2751 /* Some platforms with PCI limits cannot address compat
2752 port space. In that case we punt if their firmware has
2753 left a device in compatibility mode */
2754 if (legacy_mode) {
2755 printk(KERN_ERR "ata: Compatibility mode ATA is not supported on this platform, skipping.\n");
2756 return -EOPNOTSUPP;
2757 }
2758#endif
2759 }
2760
2761 if (!devres_open_group(dev, NULL, GFP_KERNEL))
2762 return -ENOMEM;
2763
2764 if (!legacy_mode && pdev->irq) {
2765 rc = devm_request_irq(dev, pdev->irq, irq_handler,
2766 IRQF_SHARED, drv_name, host);
2767 if (rc)
2768 goto out;
2769
2770 ata_port_desc(host->ports[0], "irq %d", pdev->irq);
2771 ata_port_desc(host->ports[1], "irq %d", pdev->irq);
2772 } else if (legacy_mode) {
2773 if (!ata_port_is_dummy(host->ports[0])) {
2774 rc = devm_request_irq(dev, ATA_PRIMARY_IRQ(pdev),
2775 irq_handler, IRQF_SHARED,
2776 drv_name, host);
2777 if (rc)
2778 goto out;
2779
2780 ata_port_desc(host->ports[0], "irq %d",
2781 ATA_PRIMARY_IRQ(pdev));
2782 }
2783
2784 if (!ata_port_is_dummy(host->ports[1])) {
2785 rc = devm_request_irq(dev, ATA_SECONDARY_IRQ(pdev),
2786 irq_handler, IRQF_SHARED,
2787 drv_name, host);
2788 if (rc)
2789 goto out;
2790
2791 ata_port_desc(host->ports[1], "irq %d",
2792 ATA_SECONDARY_IRQ(pdev));
2793 }
2794 }
2795
2796 rc = ata_host_register(host, sht);
2797 out:
2798 if (rc == 0)
2799 devres_remove_group(dev, NULL);
2800 else
2801 devres_release_group(dev, NULL);
2802
2803 return rc;
2804}
2805
2806/**
Tejun Heo9363c382008-04-07 22:47:16 +09002807 * ata_pci_sff_init_one - Initialize/register PCI IDE host controller
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002808 * @pdev: Controller to be initialized
Tejun Heo1626aeb2007-05-04 12:43:58 +02002809 * @ppi: array of port_info, must be enough for two ports
Tejun Heo1bd5b712008-03-25 12:22:49 +09002810 * @sht: scsi_host_template to use when registering the host
Tejun Heo887125e2008-03-25 12:22:49 +09002811 * @host_priv: host private_data
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002812 *
2813 * This is a helper function which can be called from a driver's
2814 * xxx_init_one() probe function if the hardware uses traditional
2815 * IDE taskfile registers.
2816 *
2817 * This function calls pci_enable_device(), reserves its register
2818 * regions, sets the dma mask, enables bus master mode, and calls
2819 * ata_device_add()
2820 *
Alan Cox2ec7df02006-08-10 16:59:10 +09002821 * ASSUMPTION:
2822 * Nobody makes a single channel controller that appears solely as
2823 * the secondary legacy port on PCI.
2824 *
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002825 * LOCKING:
2826 * Inherited from PCI layer (may sleep).
2827 *
2828 * RETURNS:
2829 * Zero on success, negative on errno-based value on error.
2830 */
Tejun Heo9363c382008-04-07 22:47:16 +09002831int ata_pci_sff_init_one(struct pci_dev *pdev,
2832 const struct ata_port_info * const * ppi,
2833 struct scsi_host_template *sht, void *host_priv)
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002834{
Tejun Heof0d36ef2007-01-20 16:00:28 +09002835 struct device *dev = &pdev->dev;
Tejun Heo1626aeb2007-05-04 12:43:58 +02002836 const struct ata_port_info *pi = NULL;
Tejun Heo0f834de2007-04-17 23:44:07 +09002837 struct ata_host *host = NULL;
Tejun Heo1626aeb2007-05-04 12:43:58 +02002838 int i, rc;
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002839
2840 DPRINTK("ENTER\n");
2841
Tejun Heo1626aeb2007-05-04 12:43:58 +02002842 /* look up the first valid port_info */
2843 for (i = 0; i < 2 && ppi[i]; i++) {
2844 if (ppi[i]->port_ops != &ata_dummy_port_ops) {
2845 pi = ppi[i];
2846 break;
2847 }
2848 }
2849
2850 if (!pi) {
2851 dev_printk(KERN_ERR, &pdev->dev,
2852 "no valid port_info specified\n");
2853 return -EINVAL;
2854 }
2855
Tejun Heof0d36ef2007-01-20 16:00:28 +09002856 if (!devres_open_group(dev, NULL, GFP_KERNEL))
2857 return -ENOMEM;
2858
Tejun Heof0d36ef2007-01-20 16:00:28 +09002859 rc = pcim_enable_device(pdev);
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002860 if (rc)
Tejun Heo4e6b79f2008-01-18 18:36:28 +09002861 goto out;
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002862
Tejun Heo4e6b79f2008-01-18 18:36:28 +09002863 /* prepare and activate SFF host */
Tejun Heo9363c382008-04-07 22:47:16 +09002864 rc = ata_pci_sff_prepare_host(pdev, ppi, &host);
Tejun Heod583bc12007-07-04 18:02:07 +09002865 if (rc)
Tejun Heo4e6b79f2008-01-18 18:36:28 +09002866 goto out;
Tejun Heo887125e2008-03-25 12:22:49 +09002867 host->private_data = host_priv;
Tejun Heod491b272007-04-17 23:44:07 +09002868
Tejun Heod491b272007-04-17 23:44:07 +09002869 pci_set_master(pdev);
Tejun Heo9363c382008-04-07 22:47:16 +09002870 rc = ata_pci_sff_activate_host(host, ata_sff_interrupt, sht);
Tejun Heo4e6b79f2008-01-18 18:36:28 +09002871 out:
2872 if (rc == 0)
2873 devres_remove_group(&pdev->dev, NULL);
2874 else
2875 devres_release_group(&pdev->dev, NULL);
Tejun Heod491b272007-04-17 23:44:07 +09002876
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002877 return rc;
2878}
2879
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002880#endif /* CONFIG_PCI */
2881
Tejun Heo624d5c52008-03-25 22:16:41 +09002882EXPORT_SYMBOL_GPL(ata_sff_port_ops);
2883EXPORT_SYMBOL_GPL(ata_bmdma_port_ops);
Tejun Heo9363c382008-04-07 22:47:16 +09002884EXPORT_SYMBOL_GPL(ata_sff_qc_prep);
2885EXPORT_SYMBOL_GPL(ata_sff_dumb_qc_prep);
2886EXPORT_SYMBOL_GPL(ata_sff_dev_select);
2887EXPORT_SYMBOL_GPL(ata_sff_check_status);
Alan Coxa57c1ba2008-05-29 22:10:58 +01002888EXPORT_SYMBOL_GPL(ata_sff_dma_pause);
2889EXPORT_SYMBOL_GPL(ata_sff_pause);
Tejun Heo9363c382008-04-07 22:47:16 +09002890EXPORT_SYMBOL_GPL(ata_sff_busy_sleep);
2891EXPORT_SYMBOL_GPL(ata_sff_wait_ready);
2892EXPORT_SYMBOL_GPL(ata_sff_tf_load);
2893EXPORT_SYMBOL_GPL(ata_sff_tf_read);
2894EXPORT_SYMBOL_GPL(ata_sff_exec_command);
2895EXPORT_SYMBOL_GPL(ata_sff_data_xfer);
2896EXPORT_SYMBOL_GPL(ata_sff_data_xfer_noirq);
2897EXPORT_SYMBOL_GPL(ata_sff_irq_on);
2898EXPORT_SYMBOL_GPL(ata_sff_irq_clear);
2899EXPORT_SYMBOL_GPL(ata_sff_hsm_move);
2900EXPORT_SYMBOL_GPL(ata_sff_qc_issue);
Tejun Heo22183bf2008-04-07 22:47:20 +09002901EXPORT_SYMBOL_GPL(ata_sff_qc_fill_rtf);
Tejun Heo9363c382008-04-07 22:47:16 +09002902EXPORT_SYMBOL_GPL(ata_sff_host_intr);
2903EXPORT_SYMBOL_GPL(ata_sff_interrupt);
2904EXPORT_SYMBOL_GPL(ata_sff_freeze);
2905EXPORT_SYMBOL_GPL(ata_sff_thaw);
2906EXPORT_SYMBOL_GPL(ata_sff_prereset);
2907EXPORT_SYMBOL_GPL(ata_sff_dev_classify);
2908EXPORT_SYMBOL_GPL(ata_sff_wait_after_reset);
2909EXPORT_SYMBOL_GPL(ata_sff_softreset);
2910EXPORT_SYMBOL_GPL(sata_sff_hardreset);
2911EXPORT_SYMBOL_GPL(ata_sff_postreset);
2912EXPORT_SYMBOL_GPL(ata_sff_error_handler);
2913EXPORT_SYMBOL_GPL(ata_sff_post_internal_cmd);
Tejun Heo624d5c52008-03-25 22:16:41 +09002914EXPORT_SYMBOL_GPL(ata_sff_port_start);
Tejun Heo9363c382008-04-07 22:47:16 +09002915EXPORT_SYMBOL_GPL(ata_sff_std_ports);
2916EXPORT_SYMBOL_GPL(ata_bmdma_mode_filter);
Tejun Heo624d5c52008-03-25 22:16:41 +09002917EXPORT_SYMBOL_GPL(ata_bmdma_setup);
2918EXPORT_SYMBOL_GPL(ata_bmdma_start);
2919EXPORT_SYMBOL_GPL(ata_bmdma_stop);
2920EXPORT_SYMBOL_GPL(ata_bmdma_status);
2921EXPORT_SYMBOL_GPL(ata_bus_reset);
2922#ifdef CONFIG_PCI
Tejun Heo9363c382008-04-07 22:47:16 +09002923EXPORT_SYMBOL_GPL(ata_pci_bmdma_clear_simplex);
2924EXPORT_SYMBOL_GPL(ata_pci_bmdma_init);
2925EXPORT_SYMBOL_GPL(ata_pci_sff_init_host);
2926EXPORT_SYMBOL_GPL(ata_pci_sff_prepare_host);
2927EXPORT_SYMBOL_GPL(ata_pci_sff_activate_host);
2928EXPORT_SYMBOL_GPL(ata_pci_sff_init_one);
Tejun Heo624d5c52008-03-25 22:16:41 +09002929#endif /* CONFIG_PCI */