blob: 9033d164c4ece13f876777fd7fbb549e4c27456b [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
81/**
82 * ata_fill_sg - Fill PCI IDE PRD table
83 * @qc: Metadata associated with taskfile to be transferred
84 *
85 * Fill PCI IDE PRD (scatter-gather) table with segments
86 * associated with the current disk command.
87 *
88 * LOCKING:
89 * spin_lock_irqsave(host lock)
90 *
91 */
92static void ata_fill_sg(struct ata_queued_cmd *qc)
93{
94 struct ata_port *ap = qc->ap;
95 struct scatterlist *sg;
96 unsigned int si, pi;
97
98 pi = 0;
99 for_each_sg(qc->sg, sg, qc->n_elem, si) {
100 u32 addr, offset;
101 u32 sg_len, len;
102
103 /* determine if physical DMA addr spans 64K boundary.
104 * Note h/w doesn't support 64-bit, so we unconditionally
105 * truncate dma_addr_t to u32.
106 */
107 addr = (u32) sg_dma_address(sg);
108 sg_len = sg_dma_len(sg);
109
110 while (sg_len) {
111 offset = addr & 0xffff;
112 len = sg_len;
113 if ((offset + sg_len) > 0x10000)
114 len = 0x10000 - offset;
115
116 ap->prd[pi].addr = cpu_to_le32(addr);
117 ap->prd[pi].flags_len = cpu_to_le32(len & 0xffff);
118 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", pi, addr, len);
119
120 pi++;
121 sg_len -= len;
122 addr += len;
123 }
124 }
125
126 ap->prd[pi - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
127}
128
129/**
130 * ata_fill_sg_dumb - Fill PCI IDE PRD table
131 * @qc: Metadata associated with taskfile to be transferred
132 *
133 * Fill PCI IDE PRD (scatter-gather) table with segments
134 * associated with the current disk command. Perform the fill
135 * so that we avoid writing any length 64K records for
136 * controllers that don't follow the spec.
137 *
138 * LOCKING:
139 * spin_lock_irqsave(host lock)
140 *
141 */
142static void ata_fill_sg_dumb(struct ata_queued_cmd *qc)
143{
144 struct ata_port *ap = qc->ap;
145 struct scatterlist *sg;
146 unsigned int si, pi;
147
148 pi = 0;
149 for_each_sg(qc->sg, sg, qc->n_elem, si) {
150 u32 addr, offset;
151 u32 sg_len, len, blen;
152
153 /* determine if physical DMA addr spans 64K boundary.
154 * Note h/w doesn't support 64-bit, so we unconditionally
155 * truncate dma_addr_t to u32.
156 */
157 addr = (u32) sg_dma_address(sg);
158 sg_len = sg_dma_len(sg);
159
160 while (sg_len) {
161 offset = addr & 0xffff;
162 len = sg_len;
163 if ((offset + sg_len) > 0x10000)
164 len = 0x10000 - offset;
165
166 blen = len & 0xffff;
167 ap->prd[pi].addr = cpu_to_le32(addr);
168 if (blen == 0) {
169 /* Some PATA chipsets like the CS5530 can't
170 cope with 0x0000 meaning 64K as the spec says */
171 ap->prd[pi].flags_len = cpu_to_le32(0x8000);
172 blen = 0x8000;
173 ap->prd[++pi].addr = cpu_to_le32(addr + 0x8000);
174 }
175 ap->prd[pi].flags_len = cpu_to_le32(blen);
176 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", pi, addr, len);
177
178 pi++;
179 sg_len -= len;
180 addr += len;
181 }
182 }
183
184 ap->prd[pi - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
185}
186
187/**
Tejun Heo9363c382008-04-07 22:47:16 +0900188 * ata_sff_qc_prep - Prepare taskfile for submission
Tejun Heo624d5c52008-03-25 22:16:41 +0900189 * @qc: Metadata associated with taskfile to be prepared
190 *
191 * Prepare ATA taskfile for submission.
192 *
193 * LOCKING:
194 * spin_lock_irqsave(host lock)
195 */
Tejun Heo9363c382008-04-07 22:47:16 +0900196void ata_sff_qc_prep(struct ata_queued_cmd *qc)
Tejun Heo624d5c52008-03-25 22:16:41 +0900197{
198 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
199 return;
200
201 ata_fill_sg(qc);
202}
203
204/**
Tejun Heo9363c382008-04-07 22:47:16 +0900205 * ata_sff_dumb_qc_prep - Prepare taskfile for submission
Tejun Heo624d5c52008-03-25 22:16:41 +0900206 * @qc: Metadata associated with taskfile to be prepared
207 *
208 * Prepare ATA taskfile for submission.
209 *
210 * LOCKING:
211 * spin_lock_irqsave(host lock)
212 */
Tejun Heo9363c382008-04-07 22:47:16 +0900213void ata_sff_dumb_qc_prep(struct ata_queued_cmd *qc)
Tejun Heo624d5c52008-03-25 22:16:41 +0900214{
215 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
216 return;
217
218 ata_fill_sg_dumb(qc);
219}
220
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500221/**
Tejun Heo9363c382008-04-07 22:47:16 +0900222 * ata_sff_check_status - Read device status reg & clear interrupt
Tejun Heo272f7882008-03-25 22:16:40 +0900223 * @ap: port where the device is
224 *
225 * Reads ATA taskfile status register for currently-selected device
226 * and return its value. This also clears pending interrupts
227 * from this device
228 *
229 * LOCKING:
230 * Inherited from caller.
231 */
Tejun Heo9363c382008-04-07 22:47:16 +0900232u8 ata_sff_check_status(struct ata_port *ap)
Tejun Heo272f7882008-03-25 22:16:40 +0900233{
234 return ioread8(ap->ioaddr.status_addr);
235}
236
237/**
Tejun Heo9363c382008-04-07 22:47:16 +0900238 * ata_sff_altstatus - Read device alternate status reg
Tejun Heo272f7882008-03-25 22:16:40 +0900239 * @ap: port where the device is
240 *
241 * Reads ATA taskfile alternate status register for
242 * currently-selected device and return its value.
243 *
244 * Note: may NOT be used as the check_altstatus() entry in
245 * ata_port_operations.
246 *
247 * LOCKING:
248 * Inherited from caller.
249 */
Alan Coxa57c1ba2008-05-29 22:10:58 +0100250static u8 ata_sff_altstatus(struct ata_port *ap)
Tejun Heo272f7882008-03-25 22:16:40 +0900251{
Tejun Heo5682ed32008-04-07 22:47:16 +0900252 if (ap->ops->sff_check_altstatus)
253 return ap->ops->sff_check_altstatus(ap);
Tejun Heo272f7882008-03-25 22:16:40 +0900254
255 return ioread8(ap->ioaddr.altstatus_addr);
256}
257
258/**
Alan Coxa57c1ba2008-05-29 22:10:58 +0100259 * ata_sff_irq_status - Check if the device is busy
260 * @ap: port where the device is
261 *
262 * Determine if the port is currently busy. Uses altstatus
263 * if available in order to avoid clearing shared IRQ status
264 * when finding an IRQ source. Non ctl capable devices don't
265 * share interrupt lines fortunately for us.
266 *
267 * LOCKING:
268 * Inherited from caller.
269 */
270static u8 ata_sff_irq_status(struct ata_port *ap)
271{
272 u8 status;
273
274 if (ap->ops->sff_check_altstatus || ap->ioaddr.altstatus_addr) {
275 status = ata_sff_altstatus(ap);
276 /* Not us: We are busy */
277 if (status & ATA_BUSY)
278 return status;
279 }
280 /* Clear INTRQ latch */
Hugh Dickins6311c902008-06-05 14:44:39 +0100281 status = ap->ops->sff_check_status(ap);
Alan Coxa57c1ba2008-05-29 22:10:58 +0100282 return status;
283}
284
285/**
286 * ata_sff_sync - Flush writes
287 * @ap: Port to wait for.
288 *
289 * CAUTION:
290 * If we have an mmio device with no ctl and no altstatus
291 * method this will fail. No such devices are known to exist.
292 *
293 * LOCKING:
294 * Inherited from caller.
295 */
296
297static void ata_sff_sync(struct ata_port *ap)
298{
299 if (ap->ops->sff_check_altstatus)
300 ap->ops->sff_check_altstatus(ap);
301 else if (ap->ioaddr.altstatus_addr)
302 ioread8(ap->ioaddr.altstatus_addr);
303}
304
305/**
306 * ata_sff_pause - Flush writes and wait 400nS
307 * @ap: Port to pause for.
308 *
309 * CAUTION:
310 * If we have an mmio device with no ctl and no altstatus
311 * method this will fail. No such devices are known to exist.
312 *
313 * LOCKING:
314 * Inherited from caller.
315 */
316
317void ata_sff_pause(struct ata_port *ap)
318{
319 ata_sff_sync(ap);
320 ndelay(400);
321}
322
323/**
324 * ata_sff_dma_pause - Pause before commencing DMA
325 * @ap: Port to pause for.
326 *
327 * Perform I/O fencing and ensure sufficient cycle delays occur
328 * for the HDMA1:0 transition
329 */
330
331void ata_sff_dma_pause(struct ata_port *ap)
332{
333 if (ap->ops->sff_check_altstatus || ap->ioaddr.altstatus_addr) {
334 /* An altstatus read will cause the needed delay without
335 messing up the IRQ status */
336 ata_sff_altstatus(ap);
337 return;
338 }
339 /* There are no DMA controllers without ctl. BUG here to ensure
340 we never violate the HDMA1:0 transition timing and risk
341 corruption. */
342 BUG();
343}
344
345/**
Tejun Heo9363c382008-04-07 22:47:16 +0900346 * ata_sff_busy_sleep - sleep until BSY clears, or timeout
Tejun Heo624d5c52008-03-25 22:16:41 +0900347 * @ap: port containing status register to be polled
Tejun Heo341c2c92008-05-20 02:17:51 +0900348 * @tmout_pat: impatience timeout in msecs
349 * @tmout: overall timeout in msecs
Tejun Heo624d5c52008-03-25 22:16:41 +0900350 *
351 * Sleep until ATA Status register bit BSY clears,
352 * or a timeout occurs.
353 *
354 * LOCKING:
355 * Kernel thread context (may sleep).
356 *
357 * RETURNS:
358 * 0 on success, -errno otherwise.
359 */
Tejun Heo9363c382008-04-07 22:47:16 +0900360int ata_sff_busy_sleep(struct ata_port *ap,
361 unsigned long tmout_pat, unsigned long tmout)
Tejun Heo624d5c52008-03-25 22:16:41 +0900362{
363 unsigned long timer_start, timeout;
364 u8 status;
365
Tejun Heo9363c382008-04-07 22:47:16 +0900366 status = ata_sff_busy_wait(ap, ATA_BUSY, 300);
Tejun Heo624d5c52008-03-25 22:16:41 +0900367 timer_start = jiffies;
Tejun Heo341c2c92008-05-20 02:17:51 +0900368 timeout = ata_deadline(timer_start, tmout_pat);
Tejun Heo624d5c52008-03-25 22:16:41 +0900369 while (status != 0xff && (status & ATA_BUSY) &&
370 time_before(jiffies, timeout)) {
371 msleep(50);
Tejun Heo9363c382008-04-07 22:47:16 +0900372 status = ata_sff_busy_wait(ap, ATA_BUSY, 3);
Tejun Heo624d5c52008-03-25 22:16:41 +0900373 }
374
375 if (status != 0xff && (status & ATA_BUSY))
376 ata_port_printk(ap, KERN_WARNING,
377 "port is slow to respond, please be patient "
378 "(Status 0x%x)\n", status);
379
Tejun Heo341c2c92008-05-20 02:17:51 +0900380 timeout = ata_deadline(timer_start, tmout);
Tejun Heo624d5c52008-03-25 22:16:41 +0900381 while (status != 0xff && (status & ATA_BUSY) &&
382 time_before(jiffies, timeout)) {
383 msleep(50);
Tejun Heo5682ed32008-04-07 22:47:16 +0900384 status = ap->ops->sff_check_status(ap);
Tejun Heo624d5c52008-03-25 22:16:41 +0900385 }
386
387 if (status == 0xff)
388 return -ENODEV;
389
390 if (status & ATA_BUSY) {
391 ata_port_printk(ap, KERN_ERR, "port failed to respond "
392 "(%lu secs, Status 0x%x)\n",
Tejun Heo341c2c92008-05-20 02:17:51 +0900393 DIV_ROUND_UP(tmout, 1000), status);
Tejun Heo624d5c52008-03-25 22:16:41 +0900394 return -EBUSY;
395 }
396
397 return 0;
398}
399
Tejun Heoaa2731a2008-04-07 22:47:19 +0900400static int ata_sff_check_ready(struct ata_link *link)
401{
402 u8 status = link->ap->ops->sff_check_status(link->ap);
403
Tejun Heo78ab88f2008-05-01 23:41:41 +0900404 return ata_check_ready(status);
Tejun Heoaa2731a2008-04-07 22:47:19 +0900405}
406
Tejun Heo624d5c52008-03-25 22:16:41 +0900407/**
Tejun Heo9363c382008-04-07 22:47:16 +0900408 * ata_sff_wait_ready - sleep until BSY clears, or timeout
Tejun Heo705e76b2008-04-07 22:47:19 +0900409 * @link: SFF link to wait ready status for
Tejun Heo624d5c52008-03-25 22:16:41 +0900410 * @deadline: deadline jiffies for the operation
411 *
412 * Sleep until ATA Status register bit BSY clears, or timeout
413 * occurs.
414 *
415 * LOCKING:
416 * Kernel thread context (may sleep).
417 *
418 * RETURNS:
419 * 0 on success, -errno otherwise.
420 */
Tejun Heo705e76b2008-04-07 22:47:19 +0900421int ata_sff_wait_ready(struct ata_link *link, unsigned long deadline)
Tejun Heo624d5c52008-03-25 22:16:41 +0900422{
Tejun Heoaa2731a2008-04-07 22:47:19 +0900423 return ata_wait_ready(link, deadline, ata_sff_check_ready);
Tejun Heo624d5c52008-03-25 22:16:41 +0900424}
425
426/**
Tejun Heo9363c382008-04-07 22:47:16 +0900427 * ata_sff_dev_select - Select device 0/1 on ATA bus
Tejun Heo624d5c52008-03-25 22:16:41 +0900428 * @ap: ATA channel to manipulate
429 * @device: ATA device (numbered from zero) to select
430 *
431 * Use the method defined in the ATA specification to
432 * make either device 0, or device 1, active on the
433 * ATA channel. Works with both PIO and MMIO.
434 *
435 * May be used as the dev_select() entry in ata_port_operations.
436 *
437 * LOCKING:
438 * caller.
439 */
Tejun Heo9363c382008-04-07 22:47:16 +0900440void ata_sff_dev_select(struct ata_port *ap, unsigned int device)
Tejun Heo624d5c52008-03-25 22:16:41 +0900441{
442 u8 tmp;
443
444 if (device == 0)
445 tmp = ATA_DEVICE_OBS;
446 else
447 tmp = ATA_DEVICE_OBS | ATA_DEV1;
448
449 iowrite8(tmp, ap->ioaddr.device_addr);
Tejun Heo9363c382008-04-07 22:47:16 +0900450 ata_sff_pause(ap); /* needed; also flushes, for mmio */
Tejun Heo624d5c52008-03-25 22:16:41 +0900451}
452
453/**
454 * ata_dev_select - Select device 0/1 on ATA bus
455 * @ap: ATA channel to manipulate
456 * @device: ATA device (numbered from zero) to select
457 * @wait: non-zero to wait for Status register BSY bit to clear
458 * @can_sleep: non-zero if context allows sleeping
459 *
460 * Use the method defined in the ATA specification to
461 * make either device 0, or device 1, active on the
462 * ATA channel.
463 *
Tejun Heo9363c382008-04-07 22:47:16 +0900464 * This is a high-level version of ata_sff_dev_select(), which
465 * additionally provides the services of inserting the proper
466 * pauses and status polling, where needed.
Tejun Heo624d5c52008-03-25 22:16:41 +0900467 *
468 * LOCKING:
469 * caller.
470 */
471void ata_dev_select(struct ata_port *ap, unsigned int device,
472 unsigned int wait, unsigned int can_sleep)
473{
474 if (ata_msg_probe(ap))
475 ata_port_printk(ap, KERN_INFO, "ata_dev_select: ENTER, "
476 "device %u, wait %u\n", device, wait);
477
478 if (wait)
479 ata_wait_idle(ap);
480
Tejun Heo5682ed32008-04-07 22:47:16 +0900481 ap->ops->sff_dev_select(ap, device);
Tejun Heo624d5c52008-03-25 22:16:41 +0900482
483 if (wait) {
484 if (can_sleep && ap->link.device[device].class == ATA_DEV_ATAPI)
485 msleep(150);
486 ata_wait_idle(ap);
487 }
488}
489
490/**
Tejun Heo9363c382008-04-07 22:47:16 +0900491 * ata_sff_irq_on - Enable interrupts on a port.
Tejun Heo90088bb2006-10-09 11:10:26 +0900492 * @ap: Port on which interrupts are enabled.
493 *
494 * Enable interrupts on a legacy IDE device using MMIO or PIO,
495 * wait for idle, clear any pending interrupts.
496 *
497 * LOCKING:
498 * Inherited from caller.
499 */
Tejun Heo9363c382008-04-07 22:47:16 +0900500u8 ata_sff_irq_on(struct ata_port *ap)
Tejun Heo90088bb2006-10-09 11:10:26 +0900501{
502 struct ata_ioports *ioaddr = &ap->ioaddr;
503 u8 tmp;
504
505 ap->ctl &= ~ATA_NIEN;
506 ap->last_ctl = ap->ctl;
507
Tejun Heof659f0e42008-03-06 13:12:54 +0900508 if (ioaddr->ctl_addr)
509 iowrite8(ap->ctl, ioaddr->ctl_addr);
Tejun Heo90088bb2006-10-09 11:10:26 +0900510 tmp = ata_wait_idle(ap);
511
Tejun Heo5682ed32008-04-07 22:47:16 +0900512 ap->ops->sff_irq_clear(ap);
Tejun Heo90088bb2006-10-09 11:10:26 +0900513
514 return tmp;
515}
516
517/**
Tejun Heo9363c382008-04-07 22:47:16 +0900518 * ata_sff_irq_clear - Clear PCI IDE BMDMA interrupt.
Tejun Heo272f7882008-03-25 22:16:40 +0900519 * @ap: Port associated with this ATA transaction.
520 *
521 * Clear interrupt and error flags in DMA status register.
522 *
523 * May be used as the irq_clear() entry in ata_port_operations.
524 *
525 * LOCKING:
526 * spin_lock_irqsave(host lock)
527 */
Tejun Heo9363c382008-04-07 22:47:16 +0900528void ata_sff_irq_clear(struct ata_port *ap)
Tejun Heo272f7882008-03-25 22:16:40 +0900529{
530 void __iomem *mmio = ap->ioaddr.bmdma_addr;
531
532 if (!mmio)
533 return;
534
535 iowrite8(ioread8(mmio + ATA_DMA_STATUS), mmio + ATA_DMA_STATUS);
536}
537
538/**
Tejun Heo9363c382008-04-07 22:47:16 +0900539 * ata_sff_tf_load - send taskfile registers to host controller
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500540 * @ap: Port to which output is sent
541 * @tf: ATA taskfile register set
542 *
543 * Outputs ATA taskfile to standard ATA host controller.
544 *
545 * LOCKING:
546 * Inherited from caller.
547 */
Tejun Heo9363c382008-04-07 22:47:16 +0900548void ata_sff_tf_load(struct ata_port *ap, const struct ata_taskfile *tf)
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500549{
Tejun Heo0d5ff562007-02-01 15:06:36 +0900550 struct ata_ioports *ioaddr = &ap->ioaddr;
551 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
552
553 if (tf->ctl != ap->last_ctl) {
Tejun Heof659f0e42008-03-06 13:12:54 +0900554 if (ioaddr->ctl_addr)
555 iowrite8(tf->ctl, ioaddr->ctl_addr);
Tejun Heo0d5ff562007-02-01 15:06:36 +0900556 ap->last_ctl = tf->ctl;
557 ata_wait_idle(ap);
558 }
559
560 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
Tejun Heof659f0e42008-03-06 13:12:54 +0900561 WARN_ON(!ioaddr->ctl_addr);
Tejun Heo0d5ff562007-02-01 15:06:36 +0900562 iowrite8(tf->hob_feature, ioaddr->feature_addr);
563 iowrite8(tf->hob_nsect, ioaddr->nsect_addr);
564 iowrite8(tf->hob_lbal, ioaddr->lbal_addr);
565 iowrite8(tf->hob_lbam, ioaddr->lbam_addr);
566 iowrite8(tf->hob_lbah, ioaddr->lbah_addr);
567 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
568 tf->hob_feature,
569 tf->hob_nsect,
570 tf->hob_lbal,
571 tf->hob_lbam,
572 tf->hob_lbah);
573 }
574
575 if (is_addr) {
576 iowrite8(tf->feature, ioaddr->feature_addr);
577 iowrite8(tf->nsect, ioaddr->nsect_addr);
578 iowrite8(tf->lbal, ioaddr->lbal_addr);
579 iowrite8(tf->lbam, ioaddr->lbam_addr);
580 iowrite8(tf->lbah, ioaddr->lbah_addr);
581 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
582 tf->feature,
583 tf->nsect,
584 tf->lbal,
585 tf->lbam,
586 tf->lbah);
587 }
588
589 if (tf->flags & ATA_TFLAG_DEVICE) {
590 iowrite8(tf->device, ioaddr->device_addr);
591 VPRINTK("device 0x%X\n", tf->device);
592 }
593
594 ata_wait_idle(ap);
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500595}
596
597/**
Tejun Heo9363c382008-04-07 22:47:16 +0900598 * ata_sff_tf_read - input device's ATA taskfile shadow registers
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500599 * @ap: Port from which input is read
600 * @tf: ATA taskfile register set for storing input
601 *
602 * Reads ATA taskfile registers for currently-selected device
Alan Cox76548ed2007-11-19 14:34:56 +0000603 * into @tf. Assumes the device has a fully SFF compliant task file
604 * layout and behaviour. If you device does not (eg has a different
605 * status method) then you will need to provide a replacement tf_read
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500606 *
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500607 * LOCKING:
608 * Inherited from caller.
609 */
Tejun Heo9363c382008-04-07 22:47:16 +0900610void ata_sff_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500611{
Tejun Heo0d5ff562007-02-01 15:06:36 +0900612 struct ata_ioports *ioaddr = &ap->ioaddr;
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500613
Tejun Heo9363c382008-04-07 22:47:16 +0900614 tf->command = ata_sff_check_status(ap);
Tejun Heo0d5ff562007-02-01 15:06:36 +0900615 tf->feature = ioread8(ioaddr->error_addr);
616 tf->nsect = ioread8(ioaddr->nsect_addr);
617 tf->lbal = ioread8(ioaddr->lbal_addr);
618 tf->lbam = ioread8(ioaddr->lbam_addr);
619 tf->lbah = ioread8(ioaddr->lbah_addr);
620 tf->device = ioread8(ioaddr->device_addr);
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500621
Tejun Heo0d5ff562007-02-01 15:06:36 +0900622 if (tf->flags & ATA_TFLAG_LBA48) {
Tejun Heof659f0e42008-03-06 13:12:54 +0900623 if (likely(ioaddr->ctl_addr)) {
624 iowrite8(tf->ctl | ATA_HOB, ioaddr->ctl_addr);
625 tf->hob_feature = ioread8(ioaddr->error_addr);
626 tf->hob_nsect = ioread8(ioaddr->nsect_addr);
627 tf->hob_lbal = ioread8(ioaddr->lbal_addr);
628 tf->hob_lbam = ioread8(ioaddr->lbam_addr);
629 tf->hob_lbah = ioread8(ioaddr->lbah_addr);
630 iowrite8(tf->ctl, ioaddr->ctl_addr);
631 ap->last_ctl = tf->ctl;
632 } else
633 WARN_ON(1);
Tejun Heo0d5ff562007-02-01 15:06:36 +0900634 }
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500635}
636
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500637/**
Tejun Heo9363c382008-04-07 22:47:16 +0900638 * ata_sff_exec_command - issue ATA command to host controller
Tejun Heo272f7882008-03-25 22:16:40 +0900639 * @ap: port to which command is being issued
640 * @tf: ATA taskfile register set
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500641 *
Tejun Heo272f7882008-03-25 22:16:40 +0900642 * Issues ATA command, with proper synchronization with interrupt
643 * handler / other threads.
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500644 *
645 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400646 * spin_lock_irqsave(host lock)
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500647 */
Tejun Heo9363c382008-04-07 22:47:16 +0900648void ata_sff_exec_command(struct ata_port *ap, const struct ata_taskfile *tf)
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500649{
Tejun Heo272f7882008-03-25 22:16:40 +0900650 DPRINTK("ata%u: cmd 0x%X\n", ap->print_id, tf->command);
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500651
Tejun Heo272f7882008-03-25 22:16:40 +0900652 iowrite8(tf->command, ap->ioaddr.command_addr);
Tejun Heo9363c382008-04-07 22:47:16 +0900653 ata_sff_pause(ap);
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500654}
655
Tejun Heo6d97dbd2006-05-15 20:58:24 +0900656/**
Tejun Heo624d5c52008-03-25 22:16:41 +0900657 * ata_tf_to_host - issue ATA taskfile to host controller
658 * @ap: port to which command is being issued
659 * @tf: ATA taskfile register set
660 *
661 * Issues ATA taskfile register set to ATA host controller,
662 * with proper synchronization with interrupt handler and
663 * other threads.
664 *
665 * LOCKING:
666 * spin_lock_irqsave(host lock)
667 */
668static inline void ata_tf_to_host(struct ata_port *ap,
669 const struct ata_taskfile *tf)
670{
Tejun Heo5682ed32008-04-07 22:47:16 +0900671 ap->ops->sff_tf_load(ap, tf);
672 ap->ops->sff_exec_command(ap, tf);
Tejun Heo624d5c52008-03-25 22:16:41 +0900673}
674
675/**
Tejun Heo9363c382008-04-07 22:47:16 +0900676 * ata_sff_data_xfer - Transfer data by PIO
Tejun Heo624d5c52008-03-25 22:16:41 +0900677 * @dev: device to target
678 * @buf: data buffer
679 * @buflen: buffer length
680 * @rw: read/write
681 *
682 * Transfer data from/to the device data register by PIO.
683 *
684 * LOCKING:
685 * Inherited from caller.
686 *
687 * RETURNS:
688 * Bytes consumed.
689 */
Tejun Heo9363c382008-04-07 22:47:16 +0900690unsigned int ata_sff_data_xfer(struct ata_device *dev, unsigned char *buf,
691 unsigned int buflen, int rw)
Tejun Heo624d5c52008-03-25 22:16:41 +0900692{
693 struct ata_port *ap = dev->link->ap;
694 void __iomem *data_addr = ap->ioaddr.data_addr;
695 unsigned int words = buflen >> 1;
696
697 /* Transfer multiple of 2 bytes */
698 if (rw == READ)
699 ioread16_rep(data_addr, buf, words);
700 else
701 iowrite16_rep(data_addr, buf, words);
702
703 /* Transfer trailing 1 byte, if any. */
704 if (unlikely(buflen & 0x01)) {
705 __le16 align_buf[1] = { 0 };
706 unsigned char *trailing_buf = buf + buflen - 1;
707
708 if (rw == READ) {
709 align_buf[0] = cpu_to_le16(ioread16(data_addr));
710 memcpy(trailing_buf, align_buf, 1);
711 } else {
712 memcpy(align_buf, trailing_buf, 1);
713 iowrite16(le16_to_cpu(align_buf[0]), data_addr);
714 }
715 words++;
716 }
717
718 return words << 1;
719}
720
721/**
Tejun Heo9363c382008-04-07 22:47:16 +0900722 * ata_sff_data_xfer_noirq - Transfer data by PIO
Tejun Heo624d5c52008-03-25 22:16:41 +0900723 * @dev: device to target
724 * @buf: data buffer
725 * @buflen: buffer length
726 * @rw: read/write
727 *
728 * Transfer data from/to the device data register by PIO. Do the
729 * transfer with interrupts disabled.
730 *
731 * LOCKING:
732 * Inherited from caller.
733 *
734 * RETURNS:
735 * Bytes consumed.
736 */
Tejun Heo9363c382008-04-07 22:47:16 +0900737unsigned int ata_sff_data_xfer_noirq(struct ata_device *dev, unsigned char *buf,
738 unsigned int buflen, int rw)
Tejun Heo624d5c52008-03-25 22:16:41 +0900739{
740 unsigned long flags;
741 unsigned int consumed;
742
743 local_irq_save(flags);
Tejun Heo9363c382008-04-07 22:47:16 +0900744 consumed = ata_sff_data_xfer(dev, buf, buflen, rw);
Tejun Heo624d5c52008-03-25 22:16:41 +0900745 local_irq_restore(flags);
746
747 return consumed;
748}
749
750/**
751 * ata_pio_sector - Transfer a sector of data.
752 * @qc: Command on going
753 *
754 * Transfer qc->sect_size bytes of data from/to the ATA device.
755 *
756 * LOCKING:
757 * Inherited from caller.
758 */
759static void ata_pio_sector(struct ata_queued_cmd *qc)
760{
761 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
762 struct ata_port *ap = qc->ap;
763 struct page *page;
764 unsigned int offset;
765 unsigned char *buf;
766
767 if (qc->curbytes == qc->nbytes - qc->sect_size)
768 ap->hsm_task_state = HSM_ST_LAST;
769
770 page = sg_page(qc->cursg);
771 offset = qc->cursg->offset + qc->cursg_ofs;
772
773 /* get the current page and offset */
774 page = nth_page(page, (offset >> PAGE_SHIFT));
775 offset %= PAGE_SIZE;
776
777 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
778
779 if (PageHighMem(page)) {
780 unsigned long flags;
781
782 /* FIXME: use a bounce buffer */
783 local_irq_save(flags);
784 buf = kmap_atomic(page, KM_IRQ0);
785
786 /* do the actual data transfer */
Tejun Heo5682ed32008-04-07 22:47:16 +0900787 ap->ops->sff_data_xfer(qc->dev, buf + offset, qc->sect_size,
788 do_write);
Tejun Heo624d5c52008-03-25 22:16:41 +0900789
790 kunmap_atomic(buf, KM_IRQ0);
791 local_irq_restore(flags);
792 } else {
793 buf = page_address(page);
Tejun Heo5682ed32008-04-07 22:47:16 +0900794 ap->ops->sff_data_xfer(qc->dev, buf + offset, qc->sect_size,
795 do_write);
Tejun Heo624d5c52008-03-25 22:16:41 +0900796 }
797
798 qc->curbytes += qc->sect_size;
799 qc->cursg_ofs += qc->sect_size;
800
801 if (qc->cursg_ofs == qc->cursg->length) {
802 qc->cursg = sg_next(qc->cursg);
803 qc->cursg_ofs = 0;
804 }
805}
806
807/**
808 * ata_pio_sectors - Transfer one or many sectors.
809 * @qc: Command on going
810 *
811 * Transfer one or many sectors of data from/to the
812 * ATA device for the DRQ request.
813 *
814 * LOCKING:
815 * Inherited from caller.
816 */
817static void ata_pio_sectors(struct ata_queued_cmd *qc)
818{
819 if (is_multi_taskfile(&qc->tf)) {
820 /* READ/WRITE MULTIPLE */
821 unsigned int nsect;
822
823 WARN_ON(qc->dev->multi_count == 0);
824
825 nsect = min((qc->nbytes - qc->curbytes) / qc->sect_size,
826 qc->dev->multi_count);
827 while (nsect--)
828 ata_pio_sector(qc);
829 } else
830 ata_pio_sector(qc);
831
Alan Coxa57c1ba2008-05-29 22:10:58 +0100832 ata_sff_sync(qc->ap); /* flush */
Tejun Heo624d5c52008-03-25 22:16:41 +0900833}
834
835/**
836 * atapi_send_cdb - Write CDB bytes to hardware
837 * @ap: Port to which ATAPI device is attached.
838 * @qc: Taskfile currently active
839 *
840 * When device has indicated its readiness to accept
841 * a CDB, this function is called. Send the CDB.
842 *
843 * LOCKING:
844 * caller.
845 */
846static void atapi_send_cdb(struct ata_port *ap, struct ata_queued_cmd *qc)
847{
848 /* send SCSI cdb */
849 DPRINTK("send cdb\n");
850 WARN_ON(qc->dev->cdb_len < 12);
851
Tejun Heo5682ed32008-04-07 22:47:16 +0900852 ap->ops->sff_data_xfer(qc->dev, qc->cdb, qc->dev->cdb_len, 1);
Alan Coxa57c1ba2008-05-29 22:10:58 +0100853 ata_sff_sync(ap);
854 /* FIXME: If the CDB is for DMA do we need to do the transition delay
855 or is bmdma_start guaranteed to do it ? */
Tejun Heo624d5c52008-03-25 22:16:41 +0900856 switch (qc->tf.protocol) {
857 case ATAPI_PROT_PIO:
858 ap->hsm_task_state = HSM_ST;
859 break;
860 case ATAPI_PROT_NODATA:
861 ap->hsm_task_state = HSM_ST_LAST;
862 break;
863 case ATAPI_PROT_DMA:
864 ap->hsm_task_state = HSM_ST_LAST;
865 /* initiate bmdma */
866 ap->ops->bmdma_start(qc);
867 break;
868 }
869}
870
871/**
872 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
873 * @qc: Command on going
874 * @bytes: number of bytes
875 *
876 * Transfer Transfer data from/to the ATAPI device.
877 *
878 * LOCKING:
879 * Inherited from caller.
880 *
881 */
882static int __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
883{
884 int rw = (qc->tf.flags & ATA_TFLAG_WRITE) ? WRITE : READ;
885 struct ata_port *ap = qc->ap;
886 struct ata_device *dev = qc->dev;
887 struct ata_eh_info *ehi = &dev->link->eh_info;
888 struct scatterlist *sg;
889 struct page *page;
890 unsigned char *buf;
891 unsigned int offset, count, consumed;
892
893next_sg:
894 sg = qc->cursg;
895 if (unlikely(!sg)) {
896 ata_ehi_push_desc(ehi, "unexpected or too much trailing data "
897 "buf=%u cur=%u bytes=%u",
898 qc->nbytes, qc->curbytes, bytes);
899 return -1;
900 }
901
902 page = sg_page(sg);
903 offset = sg->offset + qc->cursg_ofs;
904
905 /* get the current page and offset */
906 page = nth_page(page, (offset >> PAGE_SHIFT));
907 offset %= PAGE_SIZE;
908
909 /* don't overrun current sg */
910 count = min(sg->length - qc->cursg_ofs, bytes);
911
912 /* don't cross page boundaries */
913 count = min(count, (unsigned int)PAGE_SIZE - offset);
914
915 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
916
917 if (PageHighMem(page)) {
918 unsigned long flags;
919
920 /* FIXME: use bounce buffer */
921 local_irq_save(flags);
922 buf = kmap_atomic(page, KM_IRQ0);
923
924 /* do the actual data transfer */
Tejun Heo5682ed32008-04-07 22:47:16 +0900925 consumed = ap->ops->sff_data_xfer(dev, buf + offset, count, rw);
Tejun Heo624d5c52008-03-25 22:16:41 +0900926
927 kunmap_atomic(buf, KM_IRQ0);
928 local_irq_restore(flags);
929 } else {
930 buf = page_address(page);
Tejun Heo5682ed32008-04-07 22:47:16 +0900931 consumed = ap->ops->sff_data_xfer(dev, buf + offset, count, rw);
Tejun Heo624d5c52008-03-25 22:16:41 +0900932 }
933
934 bytes -= min(bytes, consumed);
935 qc->curbytes += count;
936 qc->cursg_ofs += count;
937
938 if (qc->cursg_ofs == sg->length) {
939 qc->cursg = sg_next(qc->cursg);
940 qc->cursg_ofs = 0;
941 }
942
943 /* consumed can be larger than count only for the last transfer */
944 WARN_ON(qc->cursg && count != consumed);
945
946 if (bytes)
947 goto next_sg;
948 return 0;
949}
950
951/**
952 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
953 * @qc: Command on going
954 *
955 * Transfer Transfer data from/to the ATAPI device.
956 *
957 * LOCKING:
958 * Inherited from caller.
959 */
960static void atapi_pio_bytes(struct ata_queued_cmd *qc)
961{
962 struct ata_port *ap = qc->ap;
963 struct ata_device *dev = qc->dev;
964 struct ata_eh_info *ehi = &dev->link->eh_info;
965 unsigned int ireason, bc_lo, bc_hi, bytes;
966 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
967
968 /* Abuse qc->result_tf for temp storage of intermediate TF
969 * here to save some kernel stack usage.
970 * For normal completion, qc->result_tf is not relevant. For
971 * error, qc->result_tf is later overwritten by ata_qc_complete().
972 * So, the correctness of qc->result_tf is not affected.
973 */
Tejun Heo5682ed32008-04-07 22:47:16 +0900974 ap->ops->sff_tf_read(ap, &qc->result_tf);
Tejun Heo624d5c52008-03-25 22:16:41 +0900975 ireason = qc->result_tf.nsect;
976 bc_lo = qc->result_tf.lbam;
977 bc_hi = qc->result_tf.lbah;
978 bytes = (bc_hi << 8) | bc_lo;
979
980 /* shall be cleared to zero, indicating xfer of data */
981 if (unlikely(ireason & (1 << 0)))
982 goto atapi_check;
983
984 /* make sure transfer direction matches expected */
985 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
986 if (unlikely(do_write != i_write))
987 goto atapi_check;
988
989 if (unlikely(!bytes))
990 goto atapi_check;
991
992 VPRINTK("ata%u: xfering %d bytes\n", ap->print_id, bytes);
993
994 if (unlikely(__atapi_pio_bytes(qc, bytes)))
995 goto err_out;
Alan Coxa57c1ba2008-05-29 22:10:58 +0100996 ata_sff_sync(ap); /* flush */
Tejun Heo624d5c52008-03-25 22:16:41 +0900997
998 return;
999
1000 atapi_check:
1001 ata_ehi_push_desc(ehi, "ATAPI check failed (ireason=0x%x bytes=%u)",
1002 ireason, bytes);
1003 err_out:
1004 qc->err_mask |= AC_ERR_HSM;
1005 ap->hsm_task_state = HSM_ST_ERR;
1006}
1007
1008/**
1009 * ata_hsm_ok_in_wq - Check if the qc can be handled in the workqueue.
1010 * @ap: the target ata_port
1011 * @qc: qc on going
1012 *
1013 * RETURNS:
1014 * 1 if ok in workqueue, 0 otherwise.
1015 */
1016static inline int ata_hsm_ok_in_wq(struct ata_port *ap, struct ata_queued_cmd *qc)
1017{
1018 if (qc->tf.flags & ATA_TFLAG_POLLING)
1019 return 1;
1020
1021 if (ap->hsm_task_state == HSM_ST_FIRST) {
1022 if (qc->tf.protocol == ATA_PROT_PIO &&
1023 (qc->tf.flags & ATA_TFLAG_WRITE))
1024 return 1;
1025
1026 if (ata_is_atapi(qc->tf.protocol) &&
1027 !(qc->dev->flags & ATA_DFLAG_CDB_INTR))
1028 return 1;
1029 }
1030
1031 return 0;
1032}
1033
1034/**
1035 * ata_hsm_qc_complete - finish a qc running on standard HSM
1036 * @qc: Command to complete
1037 * @in_wq: 1 if called from workqueue, 0 otherwise
1038 *
1039 * Finish @qc which is running on standard HSM.
1040 *
1041 * LOCKING:
1042 * If @in_wq is zero, spin_lock_irqsave(host lock).
1043 * Otherwise, none on entry and grabs host lock.
1044 */
1045static void ata_hsm_qc_complete(struct ata_queued_cmd *qc, int in_wq)
1046{
1047 struct ata_port *ap = qc->ap;
1048 unsigned long flags;
1049
1050 if (ap->ops->error_handler) {
1051 if (in_wq) {
1052 spin_lock_irqsave(ap->lock, flags);
1053
1054 /* EH might have kicked in while host lock is
1055 * released.
1056 */
1057 qc = ata_qc_from_tag(ap, qc->tag);
1058 if (qc) {
1059 if (likely(!(qc->err_mask & AC_ERR_HSM))) {
Tejun Heo5682ed32008-04-07 22:47:16 +09001060 ap->ops->sff_irq_on(ap);
Tejun Heo624d5c52008-03-25 22:16:41 +09001061 ata_qc_complete(qc);
1062 } else
1063 ata_port_freeze(ap);
1064 }
1065
1066 spin_unlock_irqrestore(ap->lock, flags);
1067 } else {
1068 if (likely(!(qc->err_mask & AC_ERR_HSM)))
1069 ata_qc_complete(qc);
1070 else
1071 ata_port_freeze(ap);
1072 }
1073 } else {
1074 if (in_wq) {
1075 spin_lock_irqsave(ap->lock, flags);
Tejun Heo5682ed32008-04-07 22:47:16 +09001076 ap->ops->sff_irq_on(ap);
Tejun Heo624d5c52008-03-25 22:16:41 +09001077 ata_qc_complete(qc);
1078 spin_unlock_irqrestore(ap->lock, flags);
1079 } else
1080 ata_qc_complete(qc);
1081 }
1082}
1083
1084/**
Tejun Heo9363c382008-04-07 22:47:16 +09001085 * ata_sff_hsm_move - move the HSM to the next state.
Tejun Heo624d5c52008-03-25 22:16:41 +09001086 * @ap: the target ata_port
1087 * @qc: qc on going
1088 * @status: current device status
1089 * @in_wq: 1 if called from workqueue, 0 otherwise
1090 *
1091 * RETURNS:
1092 * 1 when poll next status needed, 0 otherwise.
1093 */
Tejun Heo9363c382008-04-07 22:47:16 +09001094int ata_sff_hsm_move(struct ata_port *ap, struct ata_queued_cmd *qc,
1095 u8 status, int in_wq)
Tejun Heo624d5c52008-03-25 22:16:41 +09001096{
Tejun Heoa836d3e2008-06-28 01:39:43 +09001097 struct ata_eh_info *ehi = &ap->link.eh_info;
Tejun Heo624d5c52008-03-25 22:16:41 +09001098 unsigned long flags = 0;
1099 int poll_next;
1100
1101 WARN_ON((qc->flags & ATA_QCFLAG_ACTIVE) == 0);
1102
Tejun Heo9363c382008-04-07 22:47:16 +09001103 /* Make sure ata_sff_qc_issue() does not throw things
Tejun Heo624d5c52008-03-25 22:16:41 +09001104 * like DMA polling into the workqueue. Notice that
1105 * in_wq is not equivalent to (qc->tf.flags & ATA_TFLAG_POLLING).
1106 */
1107 WARN_ON(in_wq != ata_hsm_ok_in_wq(ap, qc));
1108
1109fsm_start:
1110 DPRINTK("ata%u: protocol %d task_state %d (dev_stat 0x%X)\n",
1111 ap->print_id, qc->tf.protocol, ap->hsm_task_state, status);
1112
1113 switch (ap->hsm_task_state) {
1114 case HSM_ST_FIRST:
1115 /* Send first data block or PACKET CDB */
1116
1117 /* If polling, we will stay in the work queue after
1118 * sending the data. Otherwise, interrupt handler
1119 * takes over after sending the data.
1120 */
1121 poll_next = (qc->tf.flags & ATA_TFLAG_POLLING);
1122
1123 /* check device status */
1124 if (unlikely((status & ATA_DRQ) == 0)) {
1125 /* handle BSY=0, DRQ=0 as error */
1126 if (likely(status & (ATA_ERR | ATA_DF)))
1127 /* device stops HSM for abort/error */
1128 qc->err_mask |= AC_ERR_DEV;
Tejun Heoa836d3e2008-06-28 01:39:43 +09001129 else {
Tejun Heo624d5c52008-03-25 22:16:41 +09001130 /* HSM violation. Let EH handle this */
Tejun Heoa836d3e2008-06-28 01:39:43 +09001131 ata_ehi_push_desc(ehi,
1132 "ST_FIRST: !(DRQ|ERR|DF)");
Tejun Heo624d5c52008-03-25 22:16:41 +09001133 qc->err_mask |= AC_ERR_HSM;
Tejun Heoa836d3e2008-06-28 01:39:43 +09001134 }
Tejun Heo624d5c52008-03-25 22:16:41 +09001135
1136 ap->hsm_task_state = HSM_ST_ERR;
1137 goto fsm_start;
1138 }
1139
1140 /* Device should not ask for data transfer (DRQ=1)
1141 * when it finds something wrong.
1142 * We ignore DRQ here and stop the HSM by
1143 * changing hsm_task_state to HSM_ST_ERR and
1144 * let the EH abort the command or reset the device.
1145 */
1146 if (unlikely(status & (ATA_ERR | ATA_DF))) {
1147 /* Some ATAPI tape drives forget to clear the ERR bit
1148 * when doing the next command (mostly request sense).
1149 * We ignore ERR here to workaround and proceed sending
1150 * the CDB.
1151 */
1152 if (!(qc->dev->horkage & ATA_HORKAGE_STUCK_ERR)) {
Tejun Heoa836d3e2008-06-28 01:39:43 +09001153 ata_ehi_push_desc(ehi, "ST_FIRST: "
1154 "DRQ=1 with device error, "
1155 "dev_stat 0x%X", status);
Tejun Heo624d5c52008-03-25 22:16:41 +09001156 qc->err_mask |= AC_ERR_HSM;
1157 ap->hsm_task_state = HSM_ST_ERR;
1158 goto fsm_start;
1159 }
1160 }
1161
1162 /* Send the CDB (atapi) or the first data block (ata pio out).
1163 * During the state transition, interrupt handler shouldn't
1164 * be invoked before the data transfer is complete and
1165 * hsm_task_state is changed. Hence, the following locking.
1166 */
1167 if (in_wq)
1168 spin_lock_irqsave(ap->lock, flags);
1169
1170 if (qc->tf.protocol == ATA_PROT_PIO) {
1171 /* PIO data out protocol.
1172 * send first data block.
1173 */
1174
1175 /* ata_pio_sectors() might change the state
1176 * to HSM_ST_LAST. so, the state is changed here
1177 * before ata_pio_sectors().
1178 */
1179 ap->hsm_task_state = HSM_ST;
1180 ata_pio_sectors(qc);
1181 } else
1182 /* send CDB */
1183 atapi_send_cdb(ap, qc);
1184
1185 if (in_wq)
1186 spin_unlock_irqrestore(ap->lock, flags);
1187
1188 /* if polling, ata_pio_task() handles the rest.
1189 * otherwise, interrupt handler takes over from here.
1190 */
1191 break;
1192
1193 case HSM_ST:
1194 /* complete command or read/write the data register */
1195 if (qc->tf.protocol == ATAPI_PROT_PIO) {
1196 /* ATAPI PIO protocol */
1197 if ((status & ATA_DRQ) == 0) {
1198 /* No more data to transfer or device error.
1199 * Device error will be tagged in HSM_ST_LAST.
1200 */
1201 ap->hsm_task_state = HSM_ST_LAST;
1202 goto fsm_start;
1203 }
1204
1205 /* Device should not ask for data transfer (DRQ=1)
1206 * when it finds something wrong.
1207 * We ignore DRQ here and stop the HSM by
1208 * changing hsm_task_state to HSM_ST_ERR and
1209 * let the EH abort the command or reset the device.
1210 */
1211 if (unlikely(status & (ATA_ERR | ATA_DF))) {
Tejun Heoa836d3e2008-06-28 01:39:43 +09001212 ata_ehi_push_desc(ehi, "ST-ATAPI: "
1213 "DRQ=1 with device error, "
1214 "dev_stat 0x%X", status);
Tejun Heo624d5c52008-03-25 22:16:41 +09001215 qc->err_mask |= AC_ERR_HSM;
1216 ap->hsm_task_state = HSM_ST_ERR;
1217 goto fsm_start;
1218 }
1219
1220 atapi_pio_bytes(qc);
1221
1222 if (unlikely(ap->hsm_task_state == HSM_ST_ERR))
1223 /* bad ireason reported by device */
1224 goto fsm_start;
1225
1226 } else {
1227 /* ATA PIO protocol */
1228 if (unlikely((status & ATA_DRQ) == 0)) {
1229 /* handle BSY=0, DRQ=0 as error */
Tejun Heo6a6b97d2008-11-13 10:04:46 +09001230 if (likely(status & (ATA_ERR | ATA_DF))) {
Tejun Heo624d5c52008-03-25 22:16:41 +09001231 /* device stops HSM for abort/error */
1232 qc->err_mask |= AC_ERR_DEV;
Tejun Heo6a6b97d2008-11-13 10:04:46 +09001233
1234 /* If diagnostic failed and this is
1235 * IDENTIFY, it's likely a phantom
1236 * device. Mark hint.
1237 */
1238 if (qc->dev->horkage &
1239 ATA_HORKAGE_DIAGNOSTIC)
1240 qc->err_mask |=
1241 AC_ERR_NODEV_HINT;
1242 } else {
Tejun Heo624d5c52008-03-25 22:16:41 +09001243 /* HSM violation. Let EH handle this.
1244 * Phantom devices also trigger this
1245 * condition. Mark hint.
1246 */
Tejun Heoa836d3e2008-06-28 01:39:43 +09001247 ata_ehi_push_desc(ehi, "ST-ATA: "
1248 "DRQ=1 with device error, "
1249 "dev_stat 0x%X", status);
Tejun Heo624d5c52008-03-25 22:16:41 +09001250 qc->err_mask |= AC_ERR_HSM |
1251 AC_ERR_NODEV_HINT;
Tejun Heoa836d3e2008-06-28 01:39:43 +09001252 }
Tejun Heo624d5c52008-03-25 22:16:41 +09001253
1254 ap->hsm_task_state = HSM_ST_ERR;
1255 goto fsm_start;
1256 }
1257
1258 /* For PIO reads, some devices may ask for
1259 * data transfer (DRQ=1) alone with ERR=1.
1260 * We respect DRQ here and transfer one
1261 * block of junk data before changing the
1262 * hsm_task_state to HSM_ST_ERR.
1263 *
1264 * For PIO writes, ERR=1 DRQ=1 doesn't make
1265 * sense since the data block has been
1266 * transferred to the device.
1267 */
1268 if (unlikely(status & (ATA_ERR | ATA_DF))) {
1269 /* data might be corrputed */
1270 qc->err_mask |= AC_ERR_DEV;
1271
1272 if (!(qc->tf.flags & ATA_TFLAG_WRITE)) {
1273 ata_pio_sectors(qc);
1274 status = ata_wait_idle(ap);
1275 }
1276
Tejun Heoa836d3e2008-06-28 01:39:43 +09001277 if (status & (ATA_BUSY | ATA_DRQ)) {
1278 ata_ehi_push_desc(ehi, "ST-ATA: "
1279 "BUSY|DRQ persists on ERR|DF, "
1280 "dev_stat 0x%X", status);
Tejun Heo624d5c52008-03-25 22:16:41 +09001281 qc->err_mask |= AC_ERR_HSM;
Tejun Heoa836d3e2008-06-28 01:39:43 +09001282 }
Tejun Heo624d5c52008-03-25 22:16:41 +09001283
1284 /* ata_pio_sectors() might change the
1285 * state to HSM_ST_LAST. so, the state
1286 * is changed after ata_pio_sectors().
1287 */
1288 ap->hsm_task_state = HSM_ST_ERR;
1289 goto fsm_start;
1290 }
1291
1292 ata_pio_sectors(qc);
1293
1294 if (ap->hsm_task_state == HSM_ST_LAST &&
1295 (!(qc->tf.flags & ATA_TFLAG_WRITE))) {
1296 /* all data read */
1297 status = ata_wait_idle(ap);
1298 goto fsm_start;
1299 }
1300 }
1301
1302 poll_next = 1;
1303 break;
1304
1305 case HSM_ST_LAST:
1306 if (unlikely(!ata_ok(status))) {
1307 qc->err_mask |= __ac_err_mask(status);
1308 ap->hsm_task_state = HSM_ST_ERR;
1309 goto fsm_start;
1310 }
1311
1312 /* no more data to transfer */
1313 DPRINTK("ata%u: dev %u command complete, drv_stat 0x%x\n",
1314 ap->print_id, qc->dev->devno, status);
1315
Tejun Heo411cb382008-04-23 20:48:36 +09001316 WARN_ON(qc->err_mask & (AC_ERR_DEV | AC_ERR_HSM));
Tejun Heo624d5c52008-03-25 22:16:41 +09001317
1318 ap->hsm_task_state = HSM_ST_IDLE;
1319
1320 /* complete taskfile transaction */
1321 ata_hsm_qc_complete(qc, in_wq);
1322
1323 poll_next = 0;
1324 break;
1325
1326 case HSM_ST_ERR:
Tejun Heo624d5c52008-03-25 22:16:41 +09001327 ap->hsm_task_state = HSM_ST_IDLE;
1328
1329 /* complete taskfile transaction */
1330 ata_hsm_qc_complete(qc, in_wq);
1331
1332 poll_next = 0;
1333 break;
1334 default:
1335 poll_next = 0;
1336 BUG();
1337 }
1338
1339 return poll_next;
1340}
1341
1342void ata_pio_task(struct work_struct *work)
1343{
1344 struct ata_port *ap =
1345 container_of(work, struct ata_port, port_task.work);
1346 struct ata_queued_cmd *qc = ap->port_task_data;
1347 u8 status;
1348 int poll_next;
1349
1350fsm_start:
1351 WARN_ON(ap->hsm_task_state == HSM_ST_IDLE);
1352
1353 /*
1354 * This is purely heuristic. This is a fast path.
1355 * Sometimes when we enter, BSY will be cleared in
1356 * a chk-status or two. If not, the drive is probably seeking
1357 * or something. Snooze for a couple msecs, then
1358 * chk-status again. If still busy, queue delayed work.
1359 */
Tejun Heo9363c382008-04-07 22:47:16 +09001360 status = ata_sff_busy_wait(ap, ATA_BUSY, 5);
Tejun Heo624d5c52008-03-25 22:16:41 +09001361 if (status & ATA_BUSY) {
1362 msleep(2);
Tejun Heo9363c382008-04-07 22:47:16 +09001363 status = ata_sff_busy_wait(ap, ATA_BUSY, 10);
Tejun Heo624d5c52008-03-25 22:16:41 +09001364 if (status & ATA_BUSY) {
1365 ata_pio_queue_task(ap, qc, ATA_SHORT_PAUSE);
1366 return;
1367 }
1368 }
1369
1370 /* move the HSM */
Tejun Heo9363c382008-04-07 22:47:16 +09001371 poll_next = ata_sff_hsm_move(ap, qc, status, 1);
Tejun Heo624d5c52008-03-25 22:16:41 +09001372
1373 /* another command or interrupt handler
1374 * may be running at this point.
1375 */
1376 if (poll_next)
1377 goto fsm_start;
1378}
1379
1380/**
Tejun Heo9363c382008-04-07 22:47:16 +09001381 * ata_sff_qc_issue - issue taskfile to device in proto-dependent manner
Tejun Heo624d5c52008-03-25 22:16:41 +09001382 * @qc: command to issue to device
1383 *
1384 * Using various libata functions and hooks, this function
1385 * starts an ATA command. ATA commands are grouped into
1386 * classes called "protocols", and issuing each type of protocol
1387 * is slightly different.
1388 *
1389 * May be used as the qc_issue() entry in ata_port_operations.
1390 *
1391 * LOCKING:
1392 * spin_lock_irqsave(host lock)
1393 *
1394 * RETURNS:
1395 * Zero on success, AC_ERR_* mask on failure
1396 */
Tejun Heo9363c382008-04-07 22:47:16 +09001397unsigned int ata_sff_qc_issue(struct ata_queued_cmd *qc)
Tejun Heo624d5c52008-03-25 22:16:41 +09001398{
1399 struct ata_port *ap = qc->ap;
1400
1401 /* Use polling pio if the LLD doesn't handle
1402 * interrupt driven pio and atapi CDB interrupt.
1403 */
1404 if (ap->flags & ATA_FLAG_PIO_POLLING) {
1405 switch (qc->tf.protocol) {
1406 case ATA_PROT_PIO:
1407 case ATA_PROT_NODATA:
1408 case ATAPI_PROT_PIO:
1409 case ATAPI_PROT_NODATA:
1410 qc->tf.flags |= ATA_TFLAG_POLLING;
1411 break;
1412 case ATAPI_PROT_DMA:
1413 if (qc->dev->flags & ATA_DFLAG_CDB_INTR)
1414 /* see ata_dma_blacklisted() */
1415 BUG();
1416 break;
1417 default:
1418 break;
1419 }
1420 }
1421
1422 /* select the device */
1423 ata_dev_select(ap, qc->dev->devno, 1, 0);
1424
1425 /* start the command */
1426 switch (qc->tf.protocol) {
1427 case ATA_PROT_NODATA:
1428 if (qc->tf.flags & ATA_TFLAG_POLLING)
1429 ata_qc_set_polling(qc);
1430
1431 ata_tf_to_host(ap, &qc->tf);
1432 ap->hsm_task_state = HSM_ST_LAST;
1433
1434 if (qc->tf.flags & ATA_TFLAG_POLLING)
1435 ata_pio_queue_task(ap, qc, 0);
1436
1437 break;
1438
1439 case ATA_PROT_DMA:
1440 WARN_ON(qc->tf.flags & ATA_TFLAG_POLLING);
1441
Tejun Heo5682ed32008-04-07 22:47:16 +09001442 ap->ops->sff_tf_load(ap, &qc->tf); /* load tf registers */
Tejun Heo624d5c52008-03-25 22:16:41 +09001443 ap->ops->bmdma_setup(qc); /* set up bmdma */
1444 ap->ops->bmdma_start(qc); /* initiate bmdma */
1445 ap->hsm_task_state = HSM_ST_LAST;
1446 break;
1447
1448 case ATA_PROT_PIO:
1449 if (qc->tf.flags & ATA_TFLAG_POLLING)
1450 ata_qc_set_polling(qc);
1451
1452 ata_tf_to_host(ap, &qc->tf);
1453
1454 if (qc->tf.flags & ATA_TFLAG_WRITE) {
1455 /* PIO data out protocol */
1456 ap->hsm_task_state = HSM_ST_FIRST;
1457 ata_pio_queue_task(ap, qc, 0);
1458
1459 /* always send first data block using
1460 * the ata_pio_task() codepath.
1461 */
1462 } else {
1463 /* PIO data in protocol */
1464 ap->hsm_task_state = HSM_ST;
1465
1466 if (qc->tf.flags & ATA_TFLAG_POLLING)
1467 ata_pio_queue_task(ap, qc, 0);
1468
1469 /* if polling, ata_pio_task() handles the rest.
1470 * otherwise, interrupt handler takes over from here.
1471 */
1472 }
1473
1474 break;
1475
1476 case ATAPI_PROT_PIO:
1477 case ATAPI_PROT_NODATA:
1478 if (qc->tf.flags & ATA_TFLAG_POLLING)
1479 ata_qc_set_polling(qc);
1480
1481 ata_tf_to_host(ap, &qc->tf);
1482
1483 ap->hsm_task_state = HSM_ST_FIRST;
1484
1485 /* send cdb by polling if no cdb interrupt */
1486 if ((!(qc->dev->flags & ATA_DFLAG_CDB_INTR)) ||
1487 (qc->tf.flags & ATA_TFLAG_POLLING))
1488 ata_pio_queue_task(ap, qc, 0);
1489 break;
1490
1491 case ATAPI_PROT_DMA:
1492 WARN_ON(qc->tf.flags & ATA_TFLAG_POLLING);
1493
Tejun Heo5682ed32008-04-07 22:47:16 +09001494 ap->ops->sff_tf_load(ap, &qc->tf); /* load tf registers */
Tejun Heo624d5c52008-03-25 22:16:41 +09001495 ap->ops->bmdma_setup(qc); /* set up bmdma */
1496 ap->hsm_task_state = HSM_ST_FIRST;
1497
1498 /* send cdb by polling if no cdb interrupt */
1499 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
1500 ata_pio_queue_task(ap, qc, 0);
1501 break;
1502
1503 default:
1504 WARN_ON(1);
1505 return AC_ERR_SYSTEM;
1506 }
1507
1508 return 0;
1509}
1510
1511/**
Tejun Heo22183bf2008-04-07 22:47:20 +09001512 * ata_sff_qc_fill_rtf - fill result TF using ->sff_tf_read
1513 * @qc: qc to fill result TF for
1514 *
1515 * @qc is finished and result TF needs to be filled. Fill it
1516 * using ->sff_tf_read.
1517 *
1518 * LOCKING:
1519 * spin_lock_irqsave(host lock)
1520 *
1521 * RETURNS:
1522 * true indicating that result TF is successfully filled.
1523 */
1524bool ata_sff_qc_fill_rtf(struct ata_queued_cmd *qc)
1525{
1526 qc->ap->ops->sff_tf_read(qc->ap, &qc->result_tf);
1527 return true;
1528}
1529
1530/**
Tejun Heo9363c382008-04-07 22:47:16 +09001531 * ata_sff_host_intr - Handle host interrupt for given (port, task)
Tejun Heo624d5c52008-03-25 22:16:41 +09001532 * @ap: Port on which interrupt arrived (possibly...)
1533 * @qc: Taskfile currently active in engine
1534 *
1535 * Handle host interrupt for given queued command. Currently,
1536 * only DMA interrupts are handled. All other commands are
1537 * handled via polling with interrupts disabled (nIEN bit).
1538 *
1539 * LOCKING:
1540 * spin_lock_irqsave(host lock)
1541 *
1542 * RETURNS:
1543 * One if interrupt was handled, zero if not (shared irq).
1544 */
Tejun Heo9363c382008-04-07 22:47:16 +09001545inline unsigned int ata_sff_host_intr(struct ata_port *ap,
1546 struct ata_queued_cmd *qc)
Tejun Heo624d5c52008-03-25 22:16:41 +09001547{
1548 struct ata_eh_info *ehi = &ap->link.eh_info;
1549 u8 status, host_stat = 0;
1550
1551 VPRINTK("ata%u: protocol %d task_state %d\n",
1552 ap->print_id, qc->tf.protocol, ap->hsm_task_state);
1553
1554 /* Check whether we are expecting interrupt in this state */
1555 switch (ap->hsm_task_state) {
1556 case HSM_ST_FIRST:
1557 /* Some pre-ATAPI-4 devices assert INTRQ
1558 * at this state when ready to receive CDB.
1559 */
1560
1561 /* Check the ATA_DFLAG_CDB_INTR flag is enough here.
1562 * The flag was turned on only for atapi devices. No
1563 * need to check ata_is_atapi(qc->tf.protocol) again.
1564 */
1565 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
1566 goto idle_irq;
1567 break;
1568 case HSM_ST_LAST:
1569 if (qc->tf.protocol == ATA_PROT_DMA ||
1570 qc->tf.protocol == ATAPI_PROT_DMA) {
1571 /* check status of DMA engine */
1572 host_stat = ap->ops->bmdma_status(ap);
1573 VPRINTK("ata%u: host_stat 0x%X\n",
1574 ap->print_id, host_stat);
1575
1576 /* if it's not our irq... */
1577 if (!(host_stat & ATA_DMA_INTR))
1578 goto idle_irq;
1579
1580 /* before we do anything else, clear DMA-Start bit */
1581 ap->ops->bmdma_stop(qc);
1582
1583 if (unlikely(host_stat & ATA_DMA_ERR)) {
1584 /* error when transfering data to/from memory */
1585 qc->err_mask |= AC_ERR_HOST_BUS;
1586 ap->hsm_task_state = HSM_ST_ERR;
1587 }
1588 }
1589 break;
1590 case HSM_ST:
1591 break;
1592 default:
1593 goto idle_irq;
1594 }
1595
Tejun Heo624d5c52008-03-25 22:16:41 +09001596
Alan Coxa57c1ba2008-05-29 22:10:58 +01001597 /* check main status, clearing INTRQ if needed */
1598 status = ata_sff_irq_status(ap);
1599 if (status & ATA_BUSY)
Tejun Heo624d5c52008-03-25 22:16:41 +09001600 goto idle_irq;
1601
1602 /* ack bmdma irq events */
Tejun Heo5682ed32008-04-07 22:47:16 +09001603 ap->ops->sff_irq_clear(ap);
Tejun Heo624d5c52008-03-25 22:16:41 +09001604
Tejun Heo9363c382008-04-07 22:47:16 +09001605 ata_sff_hsm_move(ap, qc, status, 0);
Tejun Heo624d5c52008-03-25 22:16:41 +09001606
1607 if (unlikely(qc->err_mask) && (qc->tf.protocol == ATA_PROT_DMA ||
1608 qc->tf.protocol == ATAPI_PROT_DMA))
1609 ata_ehi_push_desc(ehi, "BMDMA stat 0x%x", host_stat);
1610
1611 return 1; /* irq handled */
1612
1613idle_irq:
1614 ap->stats.idle_irq++;
1615
1616#ifdef ATA_IRQ_TRAP
1617 if ((ap->stats.idle_irq % 1000) == 0) {
Tejun Heo5682ed32008-04-07 22:47:16 +09001618 ap->ops->sff_check_status(ap);
1619 ap->ops->sff_irq_clear(ap);
Tejun Heo624d5c52008-03-25 22:16:41 +09001620 ata_port_printk(ap, KERN_WARNING, "irq trap\n");
1621 return 1;
1622 }
1623#endif
1624 return 0; /* irq not handled */
1625}
1626
1627/**
Tejun Heo9363c382008-04-07 22:47:16 +09001628 * ata_sff_interrupt - Default ATA host interrupt handler
Tejun Heo624d5c52008-03-25 22:16:41 +09001629 * @irq: irq line (unused)
1630 * @dev_instance: pointer to our ata_host information structure
1631 *
1632 * Default interrupt handler for PCI IDE devices. Calls
Tejun Heo9363c382008-04-07 22:47:16 +09001633 * ata_sff_host_intr() for each port that is not disabled.
Tejun Heo624d5c52008-03-25 22:16:41 +09001634 *
1635 * LOCKING:
1636 * Obtains host lock during operation.
1637 *
1638 * RETURNS:
1639 * IRQ_NONE or IRQ_HANDLED.
1640 */
Tejun Heo9363c382008-04-07 22:47:16 +09001641irqreturn_t ata_sff_interrupt(int irq, void *dev_instance)
Tejun Heo624d5c52008-03-25 22:16:41 +09001642{
1643 struct ata_host *host = dev_instance;
1644 unsigned int i;
1645 unsigned int handled = 0;
1646 unsigned long flags;
1647
1648 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
1649 spin_lock_irqsave(&host->lock, flags);
1650
1651 for (i = 0; i < host->n_ports; i++) {
1652 struct ata_port *ap;
1653
1654 ap = host->ports[i];
1655 if (ap &&
1656 !(ap->flags & ATA_FLAG_DISABLED)) {
1657 struct ata_queued_cmd *qc;
1658
1659 qc = ata_qc_from_tag(ap, ap->link.active_tag);
1660 if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING)) &&
1661 (qc->flags & ATA_QCFLAG_ACTIVE))
Tejun Heo9363c382008-04-07 22:47:16 +09001662 handled |= ata_sff_host_intr(ap, qc);
Tejun Heo624d5c52008-03-25 22:16:41 +09001663 }
1664 }
1665
1666 spin_unlock_irqrestore(&host->lock, flags);
1667
1668 return IRQ_RETVAL(handled);
1669}
1670
1671/**
Tejun Heo9363c382008-04-07 22:47:16 +09001672 * ata_sff_freeze - Freeze SFF controller port
Tejun Heo6d97dbd2006-05-15 20:58:24 +09001673 * @ap: port to freeze
1674 *
1675 * Freeze BMDMA controller port.
1676 *
1677 * LOCKING:
1678 * Inherited from caller.
1679 */
Tejun Heo9363c382008-04-07 22:47:16 +09001680void ata_sff_freeze(struct ata_port *ap)
Tejun Heo6d97dbd2006-05-15 20:58:24 +09001681{
1682 struct ata_ioports *ioaddr = &ap->ioaddr;
1683
1684 ap->ctl |= ATA_NIEN;
1685 ap->last_ctl = ap->ctl;
1686
Tejun Heof659f0e42008-03-06 13:12:54 +09001687 if (ioaddr->ctl_addr)
1688 iowrite8(ap->ctl, ioaddr->ctl_addr);
Tejun Heo0f0a3ad2006-11-17 12:24:22 +09001689
1690 /* Under certain circumstances, some controllers raise IRQ on
1691 * ATA_NIEN manipulation. Also, many controllers fail to mask
1692 * previously pending IRQ on ATA_NIEN assertion. Clear it.
1693 */
Tejun Heo5682ed32008-04-07 22:47:16 +09001694 ap->ops->sff_check_status(ap);
Tejun Heo0f0a3ad2006-11-17 12:24:22 +09001695
Tejun Heo5682ed32008-04-07 22:47:16 +09001696 ap->ops->sff_irq_clear(ap);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09001697}
1698
1699/**
Tejun Heo9363c382008-04-07 22:47:16 +09001700 * ata_sff_thaw - Thaw SFF controller port
Tejun Heo6d97dbd2006-05-15 20:58:24 +09001701 * @ap: port to thaw
1702 *
Tejun Heo9363c382008-04-07 22:47:16 +09001703 * Thaw SFF controller port.
Tejun Heo6d97dbd2006-05-15 20:58:24 +09001704 *
1705 * LOCKING:
1706 * Inherited from caller.
1707 */
Tejun Heo9363c382008-04-07 22:47:16 +09001708void ata_sff_thaw(struct ata_port *ap)
Tejun Heo6d97dbd2006-05-15 20:58:24 +09001709{
1710 /* clear & re-enable interrupts */
Tejun Heo5682ed32008-04-07 22:47:16 +09001711 ap->ops->sff_check_status(ap);
1712 ap->ops->sff_irq_clear(ap);
1713 ap->ops->sff_irq_on(ap);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09001714}
1715
1716/**
Tejun Heo0aa11132008-04-07 22:47:18 +09001717 * ata_sff_prereset - prepare SFF link for reset
1718 * @link: SFF link to be reset
1719 * @deadline: deadline jiffies for the operation
1720 *
1721 * SFF link @link is about to be reset. Initialize it. It first
1722 * calls ata_std_prereset() and wait for !BSY if the port is
1723 * being softreset.
1724 *
1725 * LOCKING:
1726 * Kernel thread context (may sleep)
1727 *
1728 * RETURNS:
1729 * 0 on success, -errno otherwise.
1730 */
1731int ata_sff_prereset(struct ata_link *link, unsigned long deadline)
1732{
Tejun Heo0aa11132008-04-07 22:47:18 +09001733 struct ata_eh_context *ehc = &link->eh_context;
1734 int rc;
1735
1736 rc = ata_std_prereset(link, deadline);
1737 if (rc)
1738 return rc;
1739
1740 /* if we're about to do hardreset, nothing more to do */
1741 if (ehc->i.action & ATA_EH_HARDRESET)
1742 return 0;
1743
1744 /* wait for !BSY if we don't know that no device is attached */
1745 if (!ata_link_offline(link)) {
Tejun Heo705e76b2008-04-07 22:47:19 +09001746 rc = ata_sff_wait_ready(link, deadline);
Tejun Heo0aa11132008-04-07 22:47:18 +09001747 if (rc && rc != -ENODEV) {
1748 ata_link_printk(link, KERN_WARNING, "device not ready "
1749 "(errno=%d), forcing hardreset\n", rc);
1750 ehc->i.action |= ATA_EH_HARDRESET;
1751 }
1752 }
1753
1754 return 0;
1755}
1756
1757/**
Tejun Heo624d5c52008-03-25 22:16:41 +09001758 * ata_devchk - PATA device presence detection
1759 * @ap: ATA channel to examine
1760 * @device: Device to examine (starting at zero)
1761 *
1762 * This technique was originally described in
1763 * Hale Landis's ATADRVR (www.ata-atapi.com), and
1764 * later found its way into the ATA/ATAPI spec.
1765 *
1766 * Write a pattern to the ATA shadow registers,
1767 * and if a device is present, it will respond by
1768 * correctly storing and echoing back the
1769 * ATA shadow register contents.
1770 *
1771 * LOCKING:
1772 * caller.
1773 */
1774static unsigned int ata_devchk(struct ata_port *ap, unsigned int device)
1775{
1776 struct ata_ioports *ioaddr = &ap->ioaddr;
1777 u8 nsect, lbal;
1778
Tejun Heo5682ed32008-04-07 22:47:16 +09001779 ap->ops->sff_dev_select(ap, device);
Tejun Heo624d5c52008-03-25 22:16:41 +09001780
1781 iowrite8(0x55, ioaddr->nsect_addr);
1782 iowrite8(0xaa, ioaddr->lbal_addr);
1783
1784 iowrite8(0xaa, ioaddr->nsect_addr);
1785 iowrite8(0x55, ioaddr->lbal_addr);
1786
1787 iowrite8(0x55, ioaddr->nsect_addr);
1788 iowrite8(0xaa, ioaddr->lbal_addr);
1789
1790 nsect = ioread8(ioaddr->nsect_addr);
1791 lbal = ioread8(ioaddr->lbal_addr);
1792
1793 if ((nsect == 0x55) && (lbal == 0xaa))
1794 return 1; /* we found a device */
1795
1796 return 0; /* nothing found */
1797}
1798
1799/**
Tejun Heo9363c382008-04-07 22:47:16 +09001800 * ata_sff_dev_classify - Parse returned ATA device signature
Tejun Heo624d5c52008-03-25 22:16:41 +09001801 * @dev: ATA device to classify (starting at zero)
1802 * @present: device seems present
1803 * @r_err: Value of error register on completion
1804 *
1805 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
1806 * an ATA/ATAPI-defined set of values is placed in the ATA
1807 * shadow registers, indicating the results of device detection
1808 * and diagnostics.
1809 *
1810 * Select the ATA device, and read the values from the ATA shadow
1811 * registers. Then parse according to the Error register value,
1812 * and the spec-defined values examined by ata_dev_classify().
1813 *
1814 * LOCKING:
1815 * caller.
1816 *
1817 * RETURNS:
1818 * Device type - %ATA_DEV_ATA, %ATA_DEV_ATAPI or %ATA_DEV_NONE.
1819 */
Tejun Heo9363c382008-04-07 22:47:16 +09001820unsigned int ata_sff_dev_classify(struct ata_device *dev, int present,
Tejun Heo624d5c52008-03-25 22:16:41 +09001821 u8 *r_err)
1822{
1823 struct ata_port *ap = dev->link->ap;
1824 struct ata_taskfile tf;
1825 unsigned int class;
1826 u8 err;
1827
Tejun Heo5682ed32008-04-07 22:47:16 +09001828 ap->ops->sff_dev_select(ap, dev->devno);
Tejun Heo624d5c52008-03-25 22:16:41 +09001829
1830 memset(&tf, 0, sizeof(tf));
1831
Tejun Heo5682ed32008-04-07 22:47:16 +09001832 ap->ops->sff_tf_read(ap, &tf);
Tejun Heo624d5c52008-03-25 22:16:41 +09001833 err = tf.feature;
1834 if (r_err)
1835 *r_err = err;
1836
1837 /* see if device passed diags: continue and warn later */
1838 if (err == 0)
1839 /* diagnostic fail : do nothing _YET_ */
1840 dev->horkage |= ATA_HORKAGE_DIAGNOSTIC;
1841 else if (err == 1)
1842 /* do nothing */ ;
1843 else if ((dev->devno == 0) && (err == 0x81))
1844 /* do nothing */ ;
1845 else
1846 return ATA_DEV_NONE;
1847
1848 /* determine if device is ATA or ATAPI */
1849 class = ata_dev_classify(&tf);
1850
1851 if (class == ATA_DEV_UNKNOWN) {
1852 /* If the device failed diagnostic, it's likely to
1853 * have reported incorrect device signature too.
1854 * Assume ATA device if the device seems present but
1855 * device signature is invalid with diagnostic
1856 * failure.
1857 */
1858 if (present && (dev->horkage & ATA_HORKAGE_DIAGNOSTIC))
1859 class = ATA_DEV_ATA;
1860 else
1861 class = ATA_DEV_NONE;
Tejun Heo5682ed32008-04-07 22:47:16 +09001862 } else if ((class == ATA_DEV_ATA) &&
1863 (ap->ops->sff_check_status(ap) == 0))
Tejun Heo624d5c52008-03-25 22:16:41 +09001864 class = ATA_DEV_NONE;
1865
1866 return class;
1867}
1868
Tejun Heo705e76b2008-04-07 22:47:19 +09001869/**
1870 * ata_sff_wait_after_reset - wait for devices to become ready after reset
1871 * @link: SFF link which is just reset
1872 * @devmask: mask of present devices
1873 * @deadline: deadline jiffies for the operation
1874 *
1875 * Wait devices attached to SFF @link to become ready after
1876 * reset. It contains preceding 150ms wait to avoid accessing TF
1877 * status register too early.
1878 *
1879 * LOCKING:
1880 * Kernel thread context (may sleep).
1881 *
1882 * RETURNS:
1883 * 0 on success, -ENODEV if some or all of devices in @devmask
1884 * don't seem to exist. -errno on other errors.
1885 */
1886int ata_sff_wait_after_reset(struct ata_link *link, unsigned int devmask,
1887 unsigned long deadline)
Tejun Heo624d5c52008-03-25 22:16:41 +09001888{
Tejun Heo705e76b2008-04-07 22:47:19 +09001889 struct ata_port *ap = link->ap;
Tejun Heo624d5c52008-03-25 22:16:41 +09001890 struct ata_ioports *ioaddr = &ap->ioaddr;
1891 unsigned int dev0 = devmask & (1 << 0);
1892 unsigned int dev1 = devmask & (1 << 1);
1893 int rc, ret = 0;
1894
Tejun Heo341c2c92008-05-20 02:17:51 +09001895 msleep(ATA_WAIT_AFTER_RESET);
Tejun Heo705e76b2008-04-07 22:47:19 +09001896
1897 /* always check readiness of the master device */
1898 rc = ata_sff_wait_ready(link, deadline);
1899 /* -ENODEV means the odd clown forgot the D7 pulldown resistor
1900 * and TF status is 0xff, bail out on it too.
Tejun Heo624d5c52008-03-25 22:16:41 +09001901 */
Tejun Heo705e76b2008-04-07 22:47:19 +09001902 if (rc)
1903 return rc;
Tejun Heo624d5c52008-03-25 22:16:41 +09001904
1905 /* if device 1 was found in ata_devchk, wait for register
1906 * access briefly, then wait for BSY to clear.
1907 */
1908 if (dev1) {
1909 int i;
1910
Tejun Heo5682ed32008-04-07 22:47:16 +09001911 ap->ops->sff_dev_select(ap, 1);
Tejun Heo624d5c52008-03-25 22:16:41 +09001912
1913 /* Wait for register access. Some ATAPI devices fail
1914 * to set nsect/lbal after reset, so don't waste too
1915 * much time on it. We're gonna wait for !BSY anyway.
1916 */
1917 for (i = 0; i < 2; i++) {
1918 u8 nsect, lbal;
1919
1920 nsect = ioread8(ioaddr->nsect_addr);
1921 lbal = ioread8(ioaddr->lbal_addr);
1922 if ((nsect == 1) && (lbal == 1))
1923 break;
1924 msleep(50); /* give drive a breather */
1925 }
1926
Tejun Heo705e76b2008-04-07 22:47:19 +09001927 rc = ata_sff_wait_ready(link, deadline);
Tejun Heo624d5c52008-03-25 22:16:41 +09001928 if (rc) {
1929 if (rc != -ENODEV)
1930 return rc;
1931 ret = rc;
1932 }
1933 }
1934
1935 /* is all this really necessary? */
Tejun Heo5682ed32008-04-07 22:47:16 +09001936 ap->ops->sff_dev_select(ap, 0);
Tejun Heo624d5c52008-03-25 22:16:41 +09001937 if (dev1)
Tejun Heo5682ed32008-04-07 22:47:16 +09001938 ap->ops->sff_dev_select(ap, 1);
Tejun Heo624d5c52008-03-25 22:16:41 +09001939 if (dev0)
Tejun Heo5682ed32008-04-07 22:47:16 +09001940 ap->ops->sff_dev_select(ap, 0);
Tejun Heo624d5c52008-03-25 22:16:41 +09001941
1942 return ret;
1943}
1944
Tejun Heo624d5c52008-03-25 22:16:41 +09001945static int ata_bus_softreset(struct ata_port *ap, unsigned int devmask,
1946 unsigned long deadline)
1947{
1948 struct ata_ioports *ioaddr = &ap->ioaddr;
1949
1950 DPRINTK("ata%u: bus reset via SRST\n", ap->print_id);
1951
1952 /* software reset. causes dev0 to be selected */
1953 iowrite8(ap->ctl, ioaddr->ctl_addr);
1954 udelay(20); /* FIXME: flush */
1955 iowrite8(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
1956 udelay(20); /* FIXME: flush */
1957 iowrite8(ap->ctl, ioaddr->ctl_addr);
1958
Tejun Heo705e76b2008-04-07 22:47:19 +09001959 /* wait the port to become ready */
1960 return ata_sff_wait_after_reset(&ap->link, devmask, deadline);
Tejun Heo624d5c52008-03-25 22:16:41 +09001961}
1962
1963/**
Tejun Heo9363c382008-04-07 22:47:16 +09001964 * ata_sff_softreset - reset host port via ATA SRST
Tejun Heo624d5c52008-03-25 22:16:41 +09001965 * @link: ATA link to reset
1966 * @classes: resulting classes of attached devices
1967 * @deadline: deadline jiffies for the operation
1968 *
1969 * Reset host port using ATA SRST.
1970 *
1971 * LOCKING:
1972 * Kernel thread context (may sleep)
1973 *
1974 * RETURNS:
1975 * 0 on success, -errno otherwise.
1976 */
Tejun Heo9363c382008-04-07 22:47:16 +09001977int ata_sff_softreset(struct ata_link *link, unsigned int *classes,
Tejun Heo624d5c52008-03-25 22:16:41 +09001978 unsigned long deadline)
1979{
1980 struct ata_port *ap = link->ap;
1981 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
1982 unsigned int devmask = 0;
1983 int rc;
1984 u8 err;
1985
1986 DPRINTK("ENTER\n");
1987
Tejun Heo624d5c52008-03-25 22:16:41 +09001988 /* determine if device 0/1 are present */
1989 if (ata_devchk(ap, 0))
1990 devmask |= (1 << 0);
1991 if (slave_possible && ata_devchk(ap, 1))
1992 devmask |= (1 << 1);
1993
1994 /* select device 0 again */
Tejun Heo5682ed32008-04-07 22:47:16 +09001995 ap->ops->sff_dev_select(ap, 0);
Tejun Heo624d5c52008-03-25 22:16:41 +09001996
1997 /* issue bus reset */
1998 DPRINTK("about to softreset, devmask=%x\n", devmask);
1999 rc = ata_bus_softreset(ap, devmask, deadline);
2000 /* if link is occupied, -ENODEV too is an error */
2001 if (rc && (rc != -ENODEV || sata_scr_valid(link))) {
2002 ata_link_printk(link, KERN_ERR, "SRST failed (errno=%d)\n", rc);
2003 return rc;
2004 }
2005
2006 /* determine by signature whether we have ATA or ATAPI devices */
Tejun Heo9363c382008-04-07 22:47:16 +09002007 classes[0] = ata_sff_dev_classify(&link->device[0],
Tejun Heo624d5c52008-03-25 22:16:41 +09002008 devmask & (1 << 0), &err);
2009 if (slave_possible && err != 0x81)
Tejun Heo9363c382008-04-07 22:47:16 +09002010 classes[1] = ata_sff_dev_classify(&link->device[1],
Tejun Heo624d5c52008-03-25 22:16:41 +09002011 devmask & (1 << 1), &err);
2012
Tejun Heo624d5c52008-03-25 22:16:41 +09002013 DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]);
2014 return 0;
2015}
2016
2017/**
Tejun Heo9363c382008-04-07 22:47:16 +09002018 * sata_sff_hardreset - reset host port via SATA phy reset
Tejun Heo624d5c52008-03-25 22:16:41 +09002019 * @link: link to reset
2020 * @class: resulting class of attached device
2021 * @deadline: deadline jiffies for the operation
2022 *
2023 * SATA phy-reset host port using DET bits of SControl register,
2024 * wait for !BSY and classify the attached device.
2025 *
2026 * LOCKING:
2027 * Kernel thread context (may sleep)
2028 *
2029 * RETURNS:
2030 * 0 on success, -errno otherwise.
2031 */
Tejun Heo9363c382008-04-07 22:47:16 +09002032int sata_sff_hardreset(struct ata_link *link, unsigned int *class,
Tejun Heo624d5c52008-03-25 22:16:41 +09002033 unsigned long deadline)
2034{
Tejun Heo9dadd452008-04-07 22:47:19 +09002035 struct ata_eh_context *ehc = &link->eh_context;
2036 const unsigned long *timing = sata_ehc_deb_timing(ehc);
2037 bool online;
Tejun Heo624d5c52008-03-25 22:16:41 +09002038 int rc;
2039
Tejun Heo9dadd452008-04-07 22:47:19 +09002040 rc = sata_link_hardreset(link, timing, deadline, &online,
2041 ata_sff_check_ready);
Tejun Heo9dadd452008-04-07 22:47:19 +09002042 if (online)
2043 *class = ata_sff_dev_classify(link->device, 1, NULL);
Tejun Heo624d5c52008-03-25 22:16:41 +09002044
2045 DPRINTK("EXIT, class=%u\n", *class);
Tejun Heo9dadd452008-04-07 22:47:19 +09002046 return rc;
Tejun Heo624d5c52008-03-25 22:16:41 +09002047}
2048
2049/**
Tejun Heo203c75b2008-04-07 22:47:18 +09002050 * ata_sff_postreset - SFF postreset callback
2051 * @link: the target SFF ata_link
2052 * @classes: classes of attached devices
2053 *
2054 * This function is invoked after a successful reset. It first
2055 * calls ata_std_postreset() and performs SFF specific postreset
2056 * processing.
2057 *
2058 * LOCKING:
2059 * Kernel thread context (may sleep)
2060 */
2061void ata_sff_postreset(struct ata_link *link, unsigned int *classes)
2062{
2063 struct ata_port *ap = link->ap;
2064
2065 ata_std_postreset(link, classes);
2066
2067 /* is double-select really necessary? */
2068 if (classes[0] != ATA_DEV_NONE)
2069 ap->ops->sff_dev_select(ap, 1);
2070 if (classes[1] != ATA_DEV_NONE)
2071 ap->ops->sff_dev_select(ap, 0);
2072
2073 /* bail out if no device is present */
2074 if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
2075 DPRINTK("EXIT, no device\n");
2076 return;
2077 }
2078
2079 /* set up device control */
2080 if (ap->ioaddr.ctl_addr)
2081 iowrite8(ap->ctl, ap->ioaddr.ctl_addr);
2082}
2083
2084/**
Tejun Heo9363c382008-04-07 22:47:16 +09002085 * ata_sff_error_handler - Stock error handler for BMDMA controller
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002086 * @ap: port to handle error for
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002087 *
Tejun Heo9363c382008-04-07 22:47:16 +09002088 * Stock error handler for SFF controller. It can handle both
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002089 * PATA and SATA controllers. Many controllers should be able to
2090 * use this EH as-is or with some added handling before and
2091 * after.
2092 *
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002093 * LOCKING:
2094 * Kernel thread context (may sleep)
2095 */
Tejun Heo9363c382008-04-07 22:47:16 +09002096void ata_sff_error_handler(struct ata_port *ap)
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002097{
Tejun Heoa1efdab2008-03-25 12:22:50 +09002098 ata_reset_fn_t softreset = ap->ops->softreset;
2099 ata_reset_fn_t hardreset = ap->ops->hardreset;
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002100 struct ata_queued_cmd *qc;
2101 unsigned long flags;
2102 int thaw = 0;
2103
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002104 qc = __ata_qc_from_tag(ap, ap->link.active_tag);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002105 if (qc && !(qc->flags & ATA_QCFLAG_FAILED))
2106 qc = NULL;
2107
2108 /* reset PIO HSM and stop DMA engine */
Jeff Garzikba6a1302006-06-22 23:46:10 -04002109 spin_lock_irqsave(ap->lock, flags);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002110
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002111 ap->hsm_task_state = HSM_ST_IDLE;
2112
Tejun Heoed82f962008-03-25 21:34:39 +09002113 if (ap->ioaddr.bmdma_addr &&
2114 qc && (qc->tf.protocol == ATA_PROT_DMA ||
Tejun Heo0dc36882007-12-18 16:34:43 -05002115 qc->tf.protocol == ATAPI_PROT_DMA)) {
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002116 u8 host_stat;
2117
Robert Hancockfbbb2622006-10-27 19:08:41 -07002118 host_stat = ap->ops->bmdma_status(ap);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002119
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002120 /* BMDMA controllers indicate host bus error by
2121 * setting DMA_ERR bit and timing out. As it wasn't
2122 * really a timeout event, adjust error mask and
2123 * cancel frozen state.
2124 */
Alan18d90de2007-01-24 11:42:38 +00002125 if (qc->err_mask == AC_ERR_TIMEOUT && (host_stat & ATA_DMA_ERR)) {
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002126 qc->err_mask = AC_ERR_HOST_BUS;
2127 thaw = 1;
2128 }
2129
2130 ap->ops->bmdma_stop(qc);
2131 }
2132
Alan Coxa57c1ba2008-05-29 22:10:58 +01002133 ata_sff_sync(ap); /* FIXME: We don't need this */
Tejun Heo5682ed32008-04-07 22:47:16 +09002134 ap->ops->sff_check_status(ap);
2135 ap->ops->sff_irq_clear(ap);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002136
Jeff Garzikba6a1302006-06-22 23:46:10 -04002137 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002138
2139 if (thaw)
2140 ata_eh_thaw_port(ap);
2141
2142 /* PIO and DMA engines have been stopped, perform recovery */
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002143
Tejun Heo57c9efd2008-04-07 22:47:19 +09002144 /* Ignore ata_sff_softreset if ctl isn't accessible and
2145 * built-in hardresets if SCR access isn't available.
Tejun Heoa1efdab2008-03-25 12:22:50 +09002146 */
Tejun Heo9363c382008-04-07 22:47:16 +09002147 if (softreset == ata_sff_softreset && !ap->ioaddr.ctl_addr)
Tejun Heoa1efdab2008-03-25 12:22:50 +09002148 softreset = NULL;
Tejun Heo57c9efd2008-04-07 22:47:19 +09002149 if (ata_is_builtin_hardreset(hardreset) && !sata_scr_valid(&ap->link))
Tejun Heoa1efdab2008-03-25 12:22:50 +09002150 hardreset = NULL;
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002151
Tejun Heoa1efdab2008-03-25 12:22:50 +09002152 ata_do_eh(ap, ap->ops->prereset, softreset, hardreset,
2153 ap->ops->postreset);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002154}
2155
2156/**
Tejun Heo9363c382008-04-07 22:47:16 +09002157 * ata_sff_post_internal_cmd - Stock post_internal_cmd for SFF controller
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002158 * @qc: internal command to clean up
2159 *
2160 * LOCKING:
2161 * Kernel thread context (may sleep)
2162 */
Tejun Heo9363c382008-04-07 22:47:16 +09002163void ata_sff_post_internal_cmd(struct ata_queued_cmd *qc)
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002164{
Tejun Heo570106d2008-10-20 13:10:21 +09002165 struct ata_port *ap = qc->ap;
2166 unsigned long flags;
2167
2168 spin_lock_irqsave(ap->lock, flags);
2169
2170 ap->hsm_task_state = HSM_ST_IDLE;
2171
2172 if (ap->ioaddr.bmdma_addr)
Alan61dd08c2007-01-25 15:09:05 +00002173 ata_bmdma_stop(qc);
Tejun Heo570106d2008-10-20 13:10:21 +09002174
2175 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002176}
2177
Alan Coxd92e74d2007-06-07 16:19:15 +01002178/**
2179 * ata_sff_port_start - Set port up for dma.
2180 * @ap: Port to initialize
2181 *
2182 * Called just after data structures for each port are
2183 * initialized. Allocates space for PRD table if the device
2184 * is DMA capable SFF.
2185 *
2186 * May be used as the port_start() entry in ata_port_operations.
2187 *
2188 * LOCKING:
2189 * Inherited from caller.
2190 */
Alan Coxd92e74d2007-06-07 16:19:15 +01002191int ata_sff_port_start(struct ata_port *ap)
2192{
2193 if (ap->ioaddr.bmdma_addr)
2194 return ata_port_start(ap);
2195 return 0;
2196}
2197
Tejun Heo272f7882008-03-25 22:16:40 +09002198/**
Tejun Heo9363c382008-04-07 22:47:16 +09002199 * ata_sff_std_ports - initialize ioaddr with standard port offsets.
Tejun Heo624d5c52008-03-25 22:16:41 +09002200 * @ioaddr: IO address structure to be initialized
2201 *
2202 * Utility function which initializes data_addr, error_addr,
2203 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
2204 * device_addr, status_addr, and command_addr to standard offsets
2205 * relative to cmd_addr.
2206 *
2207 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
2208 */
Tejun Heo9363c382008-04-07 22:47:16 +09002209void ata_sff_std_ports(struct ata_ioports *ioaddr)
Tejun Heo624d5c52008-03-25 22:16:41 +09002210{
2211 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
2212 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
2213 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
2214 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
2215 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
2216 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
2217 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
2218 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
2219 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
2220 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
2221}
2222
Tejun Heo9363c382008-04-07 22:47:16 +09002223unsigned long ata_bmdma_mode_filter(struct ata_device *adev,
2224 unsigned long xfer_mask)
Tejun Heo071ce342008-03-25 22:16:42 +09002225{
2226 /* Filter out DMA modes if the device has been configured by
2227 the BIOS as PIO only */
2228
2229 if (adev->link->ap->ioaddr.bmdma_addr == NULL)
2230 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
2231 return xfer_mask;
2232}
2233
Tejun Heo624d5c52008-03-25 22:16:41 +09002234/**
Tejun Heo272f7882008-03-25 22:16:40 +09002235 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
2236 * @qc: Info associated with this ATA transaction.
2237 *
2238 * LOCKING:
2239 * spin_lock_irqsave(host lock)
2240 */
2241void ata_bmdma_setup(struct ata_queued_cmd *qc)
2242{
2243 struct ata_port *ap = qc->ap;
2244 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
2245 u8 dmactl;
2246
2247 /* load PRD table addr. */
2248 mb(); /* make sure PRD table writes are visible to controller */
2249 iowrite32(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
2250
2251 /* specify data direction, triple-check start bit is clear */
2252 dmactl = ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
2253 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
2254 if (!rw)
2255 dmactl |= ATA_DMA_WR;
2256 iowrite8(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
2257
2258 /* issue r/w command */
Tejun Heo5682ed32008-04-07 22:47:16 +09002259 ap->ops->sff_exec_command(ap, &qc->tf);
Tejun Heo272f7882008-03-25 22:16:40 +09002260}
2261
2262/**
2263 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
2264 * @qc: Info associated with this ATA transaction.
2265 *
2266 * LOCKING:
2267 * spin_lock_irqsave(host lock)
2268 */
2269void ata_bmdma_start(struct ata_queued_cmd *qc)
2270{
2271 struct ata_port *ap = qc->ap;
2272 u8 dmactl;
2273
2274 /* start host DMA transaction */
2275 dmactl = ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
2276 iowrite8(dmactl | ATA_DMA_START, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
2277
2278 /* Strictly, one may wish to issue an ioread8() here, to
2279 * flush the mmio write. However, control also passes
2280 * to the hardware at this point, and it will interrupt
2281 * us when we are to resume control. So, in effect,
2282 * we don't care when the mmio write flushes.
2283 * Further, a read of the DMA status register _immediately_
2284 * following the write may not be what certain flaky hardware
2285 * is expected, so I think it is best to not add a readb()
2286 * without first all the MMIO ATA cards/mobos.
2287 * Or maybe I'm just being paranoid.
2288 *
2289 * FIXME: The posting of this write means I/O starts are
2290 * unneccessarily delayed for MMIO
2291 */
2292}
2293
2294/**
2295 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
2296 * @qc: Command we are ending DMA for
2297 *
2298 * Clears the ATA_DMA_START flag in the dma control register
2299 *
2300 * May be used as the bmdma_stop() entry in ata_port_operations.
2301 *
2302 * LOCKING:
2303 * spin_lock_irqsave(host lock)
2304 */
2305void ata_bmdma_stop(struct ata_queued_cmd *qc)
2306{
2307 struct ata_port *ap = qc->ap;
2308 void __iomem *mmio = ap->ioaddr.bmdma_addr;
2309
2310 /* clear start/stop bit */
2311 iowrite8(ioread8(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
2312 mmio + ATA_DMA_CMD);
2313
2314 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
Alan Coxa57c1ba2008-05-29 22:10:58 +01002315 ata_sff_dma_pause(ap);
Tejun Heo272f7882008-03-25 22:16:40 +09002316}
2317
2318/**
2319 * ata_bmdma_status - Read PCI IDE BMDMA status
2320 * @ap: Port associated with this ATA transaction.
2321 *
2322 * Read and return BMDMA status register.
2323 *
2324 * May be used as the bmdma_status() entry in ata_port_operations.
2325 *
2326 * LOCKING:
2327 * spin_lock_irqsave(host lock)
2328 */
2329u8 ata_bmdma_status(struct ata_port *ap)
2330{
2331 return ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
2332}
2333
2334/**
Tejun Heo624d5c52008-03-25 22:16:41 +09002335 * ata_bus_reset - reset host port and associated ATA channel
2336 * @ap: port to reset
2337 *
2338 * This is typically the first time we actually start issuing
2339 * commands to the ATA channel. We wait for BSY to clear, then
2340 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
2341 * result. Determine what devices, if any, are on the channel
2342 * by looking at the device 0/1 error register. Look at the signature
2343 * stored in each device's taskfile registers, to determine if
2344 * the device is ATA or ATAPI.
2345 *
2346 * LOCKING:
2347 * PCI/etc. bus probe sem.
2348 * Obtains host lock.
2349 *
2350 * SIDE EFFECTS:
2351 * Sets ATA_FLAG_DISABLED if bus reset fails.
2352 *
2353 * DEPRECATED:
2354 * This function is only for drivers which still use old EH and
2355 * will be removed soon.
Tejun Heo272f7882008-03-25 22:16:40 +09002356 */
Tejun Heo624d5c52008-03-25 22:16:41 +09002357void ata_bus_reset(struct ata_port *ap)
Tejun Heo272f7882008-03-25 22:16:40 +09002358{
Tejun Heo624d5c52008-03-25 22:16:41 +09002359 struct ata_device *device = ap->link.device;
2360 struct ata_ioports *ioaddr = &ap->ioaddr;
2361 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2362 u8 err;
2363 unsigned int dev0, dev1 = 0, devmask = 0;
2364 int rc;
2365
2366 DPRINTK("ENTER, host %u, port %u\n", ap->print_id, ap->port_no);
2367
2368 /* determine if device 0/1 are present */
2369 if (ap->flags & ATA_FLAG_SATA_RESET)
2370 dev0 = 1;
2371 else {
2372 dev0 = ata_devchk(ap, 0);
2373 if (slave_possible)
2374 dev1 = ata_devchk(ap, 1);
2375 }
2376
2377 if (dev0)
2378 devmask |= (1 << 0);
2379 if (dev1)
2380 devmask |= (1 << 1);
2381
2382 /* select device 0 again */
Tejun Heo5682ed32008-04-07 22:47:16 +09002383 ap->ops->sff_dev_select(ap, 0);
Tejun Heo624d5c52008-03-25 22:16:41 +09002384
2385 /* issue bus reset */
2386 if (ap->flags & ATA_FLAG_SRST) {
Tejun Heo341c2c92008-05-20 02:17:51 +09002387 rc = ata_bus_softreset(ap, devmask,
2388 ata_deadline(jiffies, 40000));
Tejun Heo624d5c52008-03-25 22:16:41 +09002389 if (rc && rc != -ENODEV)
2390 goto err_out;
2391 }
2392
2393 /*
2394 * determine by signature whether we have ATA or ATAPI devices
2395 */
Tejun Heo9363c382008-04-07 22:47:16 +09002396 device[0].class = ata_sff_dev_classify(&device[0], dev0, &err);
Tejun Heo624d5c52008-03-25 22:16:41 +09002397 if ((slave_possible) && (err != 0x81))
Tejun Heo9363c382008-04-07 22:47:16 +09002398 device[1].class = ata_sff_dev_classify(&device[1], dev1, &err);
Tejun Heo624d5c52008-03-25 22:16:41 +09002399
2400 /* is double-select really necessary? */
2401 if (device[1].class != ATA_DEV_NONE)
Tejun Heo5682ed32008-04-07 22:47:16 +09002402 ap->ops->sff_dev_select(ap, 1);
Tejun Heo624d5c52008-03-25 22:16:41 +09002403 if (device[0].class != ATA_DEV_NONE)
Tejun Heo5682ed32008-04-07 22:47:16 +09002404 ap->ops->sff_dev_select(ap, 0);
Tejun Heo624d5c52008-03-25 22:16:41 +09002405
2406 /* if no devices were detected, disable this port */
2407 if ((device[0].class == ATA_DEV_NONE) &&
2408 (device[1].class == ATA_DEV_NONE))
2409 goto err_out;
2410
2411 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
2412 /* set up device control for ATA_FLAG_SATA_RESET */
2413 iowrite8(ap->ctl, ioaddr->ctl_addr);
2414 }
2415
2416 DPRINTK("EXIT\n");
2417 return;
2418
2419err_out:
2420 ata_port_printk(ap, KERN_ERR, "disabling port\n");
2421 ata_port_disable(ap);
2422
2423 DPRINTK("EXIT\n");
Tejun Heo272f7882008-03-25 22:16:40 +09002424}
2425
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002426#ifdef CONFIG_PCI
Alan4112e162007-01-08 12:10:05 +00002427
Tejun Heo272f7882008-03-25 22:16:40 +09002428/**
Tejun Heo9363c382008-04-07 22:47:16 +09002429 * ata_pci_bmdma_clear_simplex - attempt to kick device out of simplex
Tejun Heo272f7882008-03-25 22:16:40 +09002430 * @pdev: PCI device
2431 *
2432 * Some PCI ATA devices report simplex mode but in fact can be told to
2433 * enter non simplex mode. This implements the necessary logic to
2434 * perform the task on such devices. Calling it on other devices will
2435 * have -undefined- behaviour.
2436 */
Tejun Heo9363c382008-04-07 22:47:16 +09002437int ata_pci_bmdma_clear_simplex(struct pci_dev *pdev)
Alan4112e162007-01-08 12:10:05 +00002438{
Tejun Heo272f7882008-03-25 22:16:40 +09002439 unsigned long bmdma = pci_resource_start(pdev, 4);
2440 u8 simplex;
Jeff Garzika84471f2007-02-26 05:51:33 -05002441
Tejun Heo272f7882008-03-25 22:16:40 +09002442 if (bmdma == 0)
2443 return -ENOENT;
2444
2445 simplex = inb(bmdma + 0x02);
2446 outb(simplex & 0x60, bmdma + 0x02);
2447 simplex = inb(bmdma + 0x02);
2448 if (simplex & 0x80)
2449 return -EOPNOTSUPP;
2450 return 0;
2451}
2452
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002453/**
Tejun Heo9363c382008-04-07 22:47:16 +09002454 * ata_pci_bmdma_init - acquire PCI BMDMA resources and init ATA host
Tejun Heo0f834de2007-04-17 23:44:07 +09002455 * @host: target ATA host
2456 *
2457 * Acquire PCI BMDMA resources and initialize @host accordingly.
2458 *
2459 * LOCKING:
2460 * Inherited from calling layer (may sleep).
2461 *
2462 * RETURNS:
2463 * 0 on success, -errno otherwise.
2464 */
Tejun Heo9363c382008-04-07 22:47:16 +09002465int ata_pci_bmdma_init(struct ata_host *host)
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002466{
Tejun Heo0f834de2007-04-17 23:44:07 +09002467 struct device *gdev = host->dev;
2468 struct pci_dev *pdev = to_pci_dev(gdev);
2469 int i, rc;
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002470
Alan Cox6fdc99a2007-07-26 18:41:30 +01002471 /* No BAR4 allocation: No DMA */
2472 if (pci_resource_start(pdev, 4) == 0)
2473 return 0;
2474
Tejun Heo0f834de2007-04-17 23:44:07 +09002475 /* TODO: If we get no DMA mask we should fall back to PIO */
2476 rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
2477 if (rc)
2478 return rc;
2479 rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
2480 if (rc)
2481 return rc;
2482
2483 /* request and iomap DMA region */
Tejun Heo35a10a82008-01-04 18:42:21 +09002484 rc = pcim_iomap_regions(pdev, 1 << 4, dev_driver_string(gdev));
Tejun Heo0f834de2007-04-17 23:44:07 +09002485 if (rc) {
2486 dev_printk(KERN_ERR, gdev, "failed to request/iomap BAR4\n");
2487 return -ENOMEM;
2488 }
2489 host->iomap = pcim_iomap_table(pdev);
2490
Tejun Heo1626aeb2007-05-04 12:43:58 +02002491 for (i = 0; i < 2; i++) {
Tejun Heo0f834de2007-04-17 23:44:07 +09002492 struct ata_port *ap = host->ports[i];
Tejun Heo0f834de2007-04-17 23:44:07 +09002493 void __iomem *bmdma = host->iomap[4] + 8 * i;
2494
2495 if (ata_port_is_dummy(ap))
2496 continue;
2497
Tejun Heo21b0ad42007-04-17 23:44:07 +09002498 ap->ioaddr.bmdma_addr = bmdma;
Tejun Heo0f834de2007-04-17 23:44:07 +09002499 if ((!(ap->flags & ATA_FLAG_IGN_SIMPLEX)) &&
2500 (ioread8(bmdma + 2) & 0x80))
2501 host->flags |= ATA_HOST_SIMPLEX;
Tejun Heocbcdd872007-08-18 13:14:55 +09002502
2503 ata_port_desc(ap, "bmdma 0x%llx",
2504 (unsigned long long)pci_resource_start(pdev, 4) + 8 * i);
Tejun Heo0d5ff562007-02-01 15:06:36 +09002505 }
2506
Tejun Heo0f834de2007-04-17 23:44:07 +09002507 return 0;
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002508}
2509
Tejun Heo272f7882008-03-25 22:16:40 +09002510static int ata_resources_present(struct pci_dev *pdev, int port)
2511{
2512 int i;
2513
2514 /* Check the PCI resources for this channel are enabled */
2515 port = port * 2;
2516 for (i = 0; i < 2; i ++) {
2517 if (pci_resource_start(pdev, port + i) == 0 ||
2518 pci_resource_len(pdev, port + i) == 0)
2519 return 0;
2520 }
2521 return 1;
2522}
2523
Tejun Heod491b272007-04-17 23:44:07 +09002524/**
Tejun Heo9363c382008-04-07 22:47:16 +09002525 * ata_pci_sff_init_host - acquire native PCI ATA resources and init host
Tejun Heod491b272007-04-17 23:44:07 +09002526 * @host: target ATA host
Tejun Heod491b272007-04-17 23:44:07 +09002527 *
Tejun Heo1626aeb2007-05-04 12:43:58 +02002528 * Acquire native PCI ATA resources for @host and initialize the
2529 * first two ports of @host accordingly. Ports marked dummy are
2530 * skipped and allocation failure makes the port dummy.
Tejun Heod491b272007-04-17 23:44:07 +09002531 *
Tejun Heod583bc12007-07-04 18:02:07 +09002532 * Note that native PCI resources are valid even for legacy hosts
2533 * as we fix up pdev resources array early in boot, so this
2534 * function can be used for both native and legacy SFF hosts.
2535 *
Tejun Heod491b272007-04-17 23:44:07 +09002536 * LOCKING:
2537 * Inherited from calling layer (may sleep).
2538 *
2539 * RETURNS:
Tejun Heo1626aeb2007-05-04 12:43:58 +02002540 * 0 if at least one port is initialized, -ENODEV if no port is
2541 * available.
Tejun Heod491b272007-04-17 23:44:07 +09002542 */
Tejun Heo9363c382008-04-07 22:47:16 +09002543int ata_pci_sff_init_host(struct ata_host *host)
Tejun Heod491b272007-04-17 23:44:07 +09002544{
2545 struct device *gdev = host->dev;
2546 struct pci_dev *pdev = to_pci_dev(gdev);
Tejun Heo1626aeb2007-05-04 12:43:58 +02002547 unsigned int mask = 0;
Tejun Heod491b272007-04-17 23:44:07 +09002548 int i, rc;
2549
Tejun Heod491b272007-04-17 23:44:07 +09002550 /* request, iomap BARs and init port addresses accordingly */
2551 for (i = 0; i < 2; i++) {
2552 struct ata_port *ap = host->ports[i];
2553 int base = i * 2;
2554 void __iomem * const *iomap;
2555
Tejun Heo1626aeb2007-05-04 12:43:58 +02002556 if (ata_port_is_dummy(ap))
Tejun Heod491b272007-04-17 23:44:07 +09002557 continue;
2558
Tejun Heo1626aeb2007-05-04 12:43:58 +02002559 /* Discard disabled ports. Some controllers show
2560 * their unused channels this way. Disabled ports are
2561 * made dummy.
2562 */
2563 if (!ata_resources_present(pdev, i)) {
2564 ap->ops = &ata_dummy_port_ops;
2565 continue;
2566 }
2567
Tejun Heo35a10a82008-01-04 18:42:21 +09002568 rc = pcim_iomap_regions(pdev, 0x3 << base,
2569 dev_driver_string(gdev));
Tejun Heod491b272007-04-17 23:44:07 +09002570 if (rc) {
Tejun Heo1626aeb2007-05-04 12:43:58 +02002571 dev_printk(KERN_WARNING, gdev,
2572 "failed to request/iomap BARs for port %d "
2573 "(errno=%d)\n", i, rc);
Tejun Heod491b272007-04-17 23:44:07 +09002574 if (rc == -EBUSY)
2575 pcim_pin_device(pdev);
Tejun Heo1626aeb2007-05-04 12:43:58 +02002576 ap->ops = &ata_dummy_port_ops;
2577 continue;
Tejun Heod491b272007-04-17 23:44:07 +09002578 }
2579 host->iomap = iomap = pcim_iomap_table(pdev);
2580
2581 ap->ioaddr.cmd_addr = iomap[base];
2582 ap->ioaddr.altstatus_addr =
2583 ap->ioaddr.ctl_addr = (void __iomem *)
2584 ((unsigned long)iomap[base + 1] | ATA_PCI_CTL_OFS);
Tejun Heo9363c382008-04-07 22:47:16 +09002585 ata_sff_std_ports(&ap->ioaddr);
Tejun Heo1626aeb2007-05-04 12:43:58 +02002586
Tejun Heocbcdd872007-08-18 13:14:55 +09002587 ata_port_desc(ap, "cmd 0x%llx ctl 0x%llx",
2588 (unsigned long long)pci_resource_start(pdev, base),
2589 (unsigned long long)pci_resource_start(pdev, base + 1));
2590
Tejun Heo1626aeb2007-05-04 12:43:58 +02002591 mask |= 1 << i;
2592 }
2593
2594 if (!mask) {
2595 dev_printk(KERN_ERR, gdev, "no available native port\n");
2596 return -ENODEV;
Tejun Heod491b272007-04-17 23:44:07 +09002597 }
2598
2599 return 0;
2600}
2601
Tejun Heo21b0ad42007-04-17 23:44:07 +09002602/**
Tejun Heo9363c382008-04-07 22:47:16 +09002603 * ata_pci_sff_prepare_host - helper to prepare native PCI ATA host
Tejun Heo21b0ad42007-04-17 23:44:07 +09002604 * @pdev: target PCI device
Tejun Heo1626aeb2007-05-04 12:43:58 +02002605 * @ppi: array of port_info, must be enough for two ports
Tejun Heo21b0ad42007-04-17 23:44:07 +09002606 * @r_host: out argument for the initialized ATA host
2607 *
2608 * Helper to allocate ATA host for @pdev, acquire all native PCI
2609 * resources and initialize it accordingly in one go.
2610 *
2611 * LOCKING:
2612 * Inherited from calling layer (may sleep).
2613 *
2614 * RETURNS:
2615 * 0 on success, -errno otherwise.
2616 */
Tejun Heo9363c382008-04-07 22:47:16 +09002617int ata_pci_sff_prepare_host(struct pci_dev *pdev,
Tejun Heod583bc12007-07-04 18:02:07 +09002618 const struct ata_port_info * const * ppi,
2619 struct ata_host **r_host)
Tejun Heo21b0ad42007-04-17 23:44:07 +09002620{
2621 struct ata_host *host;
Tejun Heo21b0ad42007-04-17 23:44:07 +09002622 int rc;
2623
2624 if (!devres_open_group(&pdev->dev, NULL, GFP_KERNEL))
2625 return -ENOMEM;
2626
2627 host = ata_host_alloc_pinfo(&pdev->dev, ppi, 2);
2628 if (!host) {
2629 dev_printk(KERN_ERR, &pdev->dev,
2630 "failed to allocate ATA host\n");
2631 rc = -ENOMEM;
2632 goto err_out;
2633 }
2634
Tejun Heo9363c382008-04-07 22:47:16 +09002635 rc = ata_pci_sff_init_host(host);
Tejun Heo21b0ad42007-04-17 23:44:07 +09002636 if (rc)
2637 goto err_out;
2638
2639 /* init DMA related stuff */
Tejun Heo9363c382008-04-07 22:47:16 +09002640 rc = ata_pci_bmdma_init(host);
Tejun Heo21b0ad42007-04-17 23:44:07 +09002641 if (rc)
2642 goto err_bmdma;
2643
2644 devres_remove_group(&pdev->dev, NULL);
2645 *r_host = host;
2646 return 0;
2647
2648 err_bmdma:
2649 /* This is necessary because PCI and iomap resources are
2650 * merged and releasing the top group won't release the
2651 * acquired resources if some of those have been acquired
2652 * before entering this function.
2653 */
2654 pcim_iounmap_regions(pdev, 0xf);
2655 err_out:
2656 devres_release_group(&pdev->dev, NULL);
2657 return rc;
2658}
2659
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002660/**
Tejun Heo9363c382008-04-07 22:47:16 +09002661 * ata_pci_sff_activate_host - start SFF host, request IRQ and register it
Tejun Heo4e6b79f2008-01-18 18:36:28 +09002662 * @host: target SFF ATA host
2663 * @irq_handler: irq_handler used when requesting IRQ(s)
2664 * @sht: scsi_host_template to use when registering the host
2665 *
2666 * This is the counterpart of ata_host_activate() for SFF ATA
2667 * hosts. This separate helper is necessary because SFF hosts
2668 * use two separate interrupts in legacy mode.
2669 *
2670 * LOCKING:
2671 * Inherited from calling layer (may sleep).
2672 *
2673 * RETURNS:
2674 * 0 on success, -errno otherwise.
2675 */
Tejun Heo9363c382008-04-07 22:47:16 +09002676int ata_pci_sff_activate_host(struct ata_host *host,
Tejun Heo4e6b79f2008-01-18 18:36:28 +09002677 irq_handler_t irq_handler,
2678 struct scsi_host_template *sht)
2679{
2680 struct device *dev = host->dev;
2681 struct pci_dev *pdev = to_pci_dev(dev);
2682 const char *drv_name = dev_driver_string(host->dev);
2683 int legacy_mode = 0, rc;
2684
2685 rc = ata_host_start(host);
2686 if (rc)
2687 return rc;
2688
2689 if ((pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
2690 u8 tmp8, mask;
2691
2692 /* TODO: What if one channel is in native mode ... */
2693 pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
2694 mask = (1 << 2) | (1 << 0);
2695 if ((tmp8 & mask) != mask)
2696 legacy_mode = 1;
2697#if defined(CONFIG_NO_ATA_LEGACY)
2698 /* Some platforms with PCI limits cannot address compat
2699 port space. In that case we punt if their firmware has
2700 left a device in compatibility mode */
2701 if (legacy_mode) {
2702 printk(KERN_ERR "ata: Compatibility mode ATA is not supported on this platform, skipping.\n");
2703 return -EOPNOTSUPP;
2704 }
2705#endif
2706 }
2707
2708 if (!devres_open_group(dev, NULL, GFP_KERNEL))
2709 return -ENOMEM;
2710
2711 if (!legacy_mode && pdev->irq) {
2712 rc = devm_request_irq(dev, pdev->irq, irq_handler,
2713 IRQF_SHARED, drv_name, host);
2714 if (rc)
2715 goto out;
2716
2717 ata_port_desc(host->ports[0], "irq %d", pdev->irq);
2718 ata_port_desc(host->ports[1], "irq %d", pdev->irq);
2719 } else if (legacy_mode) {
2720 if (!ata_port_is_dummy(host->ports[0])) {
2721 rc = devm_request_irq(dev, ATA_PRIMARY_IRQ(pdev),
2722 irq_handler, IRQF_SHARED,
2723 drv_name, host);
2724 if (rc)
2725 goto out;
2726
2727 ata_port_desc(host->ports[0], "irq %d",
2728 ATA_PRIMARY_IRQ(pdev));
2729 }
2730
2731 if (!ata_port_is_dummy(host->ports[1])) {
2732 rc = devm_request_irq(dev, ATA_SECONDARY_IRQ(pdev),
2733 irq_handler, IRQF_SHARED,
2734 drv_name, host);
2735 if (rc)
2736 goto out;
2737
2738 ata_port_desc(host->ports[1], "irq %d",
2739 ATA_SECONDARY_IRQ(pdev));
2740 }
2741 }
2742
2743 rc = ata_host_register(host, sht);
2744 out:
2745 if (rc == 0)
2746 devres_remove_group(dev, NULL);
2747 else
2748 devres_release_group(dev, NULL);
2749
2750 return rc;
2751}
2752
2753/**
Tejun Heo9363c382008-04-07 22:47:16 +09002754 * ata_pci_sff_init_one - Initialize/register PCI IDE host controller
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002755 * @pdev: Controller to be initialized
Tejun Heo1626aeb2007-05-04 12:43:58 +02002756 * @ppi: array of port_info, must be enough for two ports
Tejun Heo1bd5b712008-03-25 12:22:49 +09002757 * @sht: scsi_host_template to use when registering the host
Tejun Heo887125e2008-03-25 12:22:49 +09002758 * @host_priv: host private_data
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002759 *
2760 * This is a helper function which can be called from a driver's
2761 * xxx_init_one() probe function if the hardware uses traditional
2762 * IDE taskfile registers.
2763 *
2764 * This function calls pci_enable_device(), reserves its register
2765 * regions, sets the dma mask, enables bus master mode, and calls
2766 * ata_device_add()
2767 *
Alan Cox2ec7df02006-08-10 16:59:10 +09002768 * ASSUMPTION:
2769 * Nobody makes a single channel controller that appears solely as
2770 * the secondary legacy port on PCI.
2771 *
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002772 * LOCKING:
2773 * Inherited from PCI layer (may sleep).
2774 *
2775 * RETURNS:
2776 * Zero on success, negative on errno-based value on error.
2777 */
Tejun Heo9363c382008-04-07 22:47:16 +09002778int ata_pci_sff_init_one(struct pci_dev *pdev,
2779 const struct ata_port_info * const * ppi,
2780 struct scsi_host_template *sht, void *host_priv)
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002781{
Tejun Heof0d36ef2007-01-20 16:00:28 +09002782 struct device *dev = &pdev->dev;
Tejun Heo1626aeb2007-05-04 12:43:58 +02002783 const struct ata_port_info *pi = NULL;
Tejun Heo0f834de2007-04-17 23:44:07 +09002784 struct ata_host *host = NULL;
Tejun Heo1626aeb2007-05-04 12:43:58 +02002785 int i, rc;
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002786
2787 DPRINTK("ENTER\n");
2788
Tejun Heo1626aeb2007-05-04 12:43:58 +02002789 /* look up the first valid port_info */
2790 for (i = 0; i < 2 && ppi[i]; i++) {
2791 if (ppi[i]->port_ops != &ata_dummy_port_ops) {
2792 pi = ppi[i];
2793 break;
2794 }
2795 }
2796
2797 if (!pi) {
2798 dev_printk(KERN_ERR, &pdev->dev,
2799 "no valid port_info specified\n");
2800 return -EINVAL;
2801 }
2802
Tejun Heof0d36ef2007-01-20 16:00:28 +09002803 if (!devres_open_group(dev, NULL, GFP_KERNEL))
2804 return -ENOMEM;
2805
Tejun Heof0d36ef2007-01-20 16:00:28 +09002806 rc = pcim_enable_device(pdev);
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002807 if (rc)
Tejun Heo4e6b79f2008-01-18 18:36:28 +09002808 goto out;
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002809
Tejun Heo4e6b79f2008-01-18 18:36:28 +09002810 /* prepare and activate SFF host */
Tejun Heo9363c382008-04-07 22:47:16 +09002811 rc = ata_pci_sff_prepare_host(pdev, ppi, &host);
Tejun Heod583bc12007-07-04 18:02:07 +09002812 if (rc)
Tejun Heo4e6b79f2008-01-18 18:36:28 +09002813 goto out;
Tejun Heo887125e2008-03-25 12:22:49 +09002814 host->private_data = host_priv;
Tejun Heod491b272007-04-17 23:44:07 +09002815
Tejun Heod491b272007-04-17 23:44:07 +09002816 pci_set_master(pdev);
Tejun Heo9363c382008-04-07 22:47:16 +09002817 rc = ata_pci_sff_activate_host(host, ata_sff_interrupt, sht);
Tejun Heo4e6b79f2008-01-18 18:36:28 +09002818 out:
2819 if (rc == 0)
2820 devres_remove_group(&pdev->dev, NULL);
2821 else
2822 devres_release_group(&pdev->dev, NULL);
Tejun Heod491b272007-04-17 23:44:07 +09002823
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002824 return rc;
2825}
2826
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002827#endif /* CONFIG_PCI */
2828
Tejun Heo624d5c52008-03-25 22:16:41 +09002829EXPORT_SYMBOL_GPL(ata_sff_port_ops);
2830EXPORT_SYMBOL_GPL(ata_bmdma_port_ops);
Tejun Heo9363c382008-04-07 22:47:16 +09002831EXPORT_SYMBOL_GPL(ata_sff_qc_prep);
2832EXPORT_SYMBOL_GPL(ata_sff_dumb_qc_prep);
2833EXPORT_SYMBOL_GPL(ata_sff_dev_select);
2834EXPORT_SYMBOL_GPL(ata_sff_check_status);
Alan Coxa57c1ba2008-05-29 22:10:58 +01002835EXPORT_SYMBOL_GPL(ata_sff_dma_pause);
2836EXPORT_SYMBOL_GPL(ata_sff_pause);
Tejun Heo9363c382008-04-07 22:47:16 +09002837EXPORT_SYMBOL_GPL(ata_sff_busy_sleep);
2838EXPORT_SYMBOL_GPL(ata_sff_wait_ready);
2839EXPORT_SYMBOL_GPL(ata_sff_tf_load);
2840EXPORT_SYMBOL_GPL(ata_sff_tf_read);
2841EXPORT_SYMBOL_GPL(ata_sff_exec_command);
2842EXPORT_SYMBOL_GPL(ata_sff_data_xfer);
2843EXPORT_SYMBOL_GPL(ata_sff_data_xfer_noirq);
2844EXPORT_SYMBOL_GPL(ata_sff_irq_on);
2845EXPORT_SYMBOL_GPL(ata_sff_irq_clear);
2846EXPORT_SYMBOL_GPL(ata_sff_hsm_move);
2847EXPORT_SYMBOL_GPL(ata_sff_qc_issue);
Tejun Heo22183bf2008-04-07 22:47:20 +09002848EXPORT_SYMBOL_GPL(ata_sff_qc_fill_rtf);
Tejun Heo9363c382008-04-07 22:47:16 +09002849EXPORT_SYMBOL_GPL(ata_sff_host_intr);
2850EXPORT_SYMBOL_GPL(ata_sff_interrupt);
2851EXPORT_SYMBOL_GPL(ata_sff_freeze);
2852EXPORT_SYMBOL_GPL(ata_sff_thaw);
2853EXPORT_SYMBOL_GPL(ata_sff_prereset);
2854EXPORT_SYMBOL_GPL(ata_sff_dev_classify);
2855EXPORT_SYMBOL_GPL(ata_sff_wait_after_reset);
2856EXPORT_SYMBOL_GPL(ata_sff_softreset);
2857EXPORT_SYMBOL_GPL(sata_sff_hardreset);
2858EXPORT_SYMBOL_GPL(ata_sff_postreset);
2859EXPORT_SYMBOL_GPL(ata_sff_error_handler);
2860EXPORT_SYMBOL_GPL(ata_sff_post_internal_cmd);
Tejun Heo624d5c52008-03-25 22:16:41 +09002861EXPORT_SYMBOL_GPL(ata_sff_port_start);
Tejun Heo9363c382008-04-07 22:47:16 +09002862EXPORT_SYMBOL_GPL(ata_sff_std_ports);
2863EXPORT_SYMBOL_GPL(ata_bmdma_mode_filter);
Tejun Heo624d5c52008-03-25 22:16:41 +09002864EXPORT_SYMBOL_GPL(ata_bmdma_setup);
2865EXPORT_SYMBOL_GPL(ata_bmdma_start);
2866EXPORT_SYMBOL_GPL(ata_bmdma_stop);
2867EXPORT_SYMBOL_GPL(ata_bmdma_status);
2868EXPORT_SYMBOL_GPL(ata_bus_reset);
2869#ifdef CONFIG_PCI
Tejun Heo9363c382008-04-07 22:47:16 +09002870EXPORT_SYMBOL_GPL(ata_pci_bmdma_clear_simplex);
2871EXPORT_SYMBOL_GPL(ata_pci_bmdma_init);
2872EXPORT_SYMBOL_GPL(ata_pci_sff_init_host);
2873EXPORT_SYMBOL_GPL(ata_pci_sff_prepare_host);
2874EXPORT_SYMBOL_GPL(ata_pci_sff_activate_host);
2875EXPORT_SYMBOL_GPL(ata_pci_sff_init_one);
Tejun Heo624d5c52008-03-25 22:16:41 +09002876#endif /* CONFIG_PCI */