blob: 04024a55666052e25da6b29fd73cfdd98f971a9b [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 Heo624d5c52008-03-25 22:16:41 +090047
Tejun Heo9363c382008-04-07 22:47:16 +090048 .freeze = ata_sff_freeze,
49 .thaw = ata_sff_thaw,
50 .softreset = ata_sff_softreset,
51 .error_handler = ata_sff_error_handler,
52 .post_internal_cmd = ata_sff_post_internal_cmd,
Tejun Heo624d5c52008-03-25 22:16:41 +090053
Tejun Heo5682ed32008-04-07 22:47:16 +090054 .sff_dev_select = ata_sff_dev_select,
55 .sff_check_status = ata_sff_check_status,
56 .sff_tf_load = ata_sff_tf_load,
57 .sff_tf_read = ata_sff_tf_read,
58 .sff_exec_command = ata_sff_exec_command,
59 .sff_data_xfer = ata_sff_data_xfer,
60 .sff_irq_on = ata_sff_irq_on,
Tejun Heo624d5c52008-03-25 22:16:41 +090061
62 .port_start = ata_sff_port_start,
63};
64
65const struct ata_port_operations ata_bmdma_port_ops = {
66 .inherits = &ata_sff_port_ops,
67
Tejun Heo9363c382008-04-07 22:47:16 +090068 .mode_filter = ata_bmdma_mode_filter,
Tejun Heo624d5c52008-03-25 22:16:41 +090069
70 .bmdma_setup = ata_bmdma_setup,
71 .bmdma_start = ata_bmdma_start,
72 .bmdma_stop = ata_bmdma_stop,
73 .bmdma_status = ata_bmdma_status,
Tejun Heo5682ed32008-04-07 22:47:16 +090074 .sff_irq_clear = ata_sff_irq_clear,
Tejun Heo624d5c52008-03-25 22:16:41 +090075};
76
77/**
78 * ata_fill_sg - Fill PCI IDE PRD table
79 * @qc: Metadata associated with taskfile to be transferred
80 *
81 * Fill PCI IDE PRD (scatter-gather) table with segments
82 * associated with the current disk command.
83 *
84 * LOCKING:
85 * spin_lock_irqsave(host lock)
86 *
87 */
88static void ata_fill_sg(struct ata_queued_cmd *qc)
89{
90 struct ata_port *ap = qc->ap;
91 struct scatterlist *sg;
92 unsigned int si, pi;
93
94 pi = 0;
95 for_each_sg(qc->sg, sg, qc->n_elem, si) {
96 u32 addr, offset;
97 u32 sg_len, len;
98
99 /* determine if physical DMA addr spans 64K boundary.
100 * Note h/w doesn't support 64-bit, so we unconditionally
101 * truncate dma_addr_t to u32.
102 */
103 addr = (u32) sg_dma_address(sg);
104 sg_len = sg_dma_len(sg);
105
106 while (sg_len) {
107 offset = addr & 0xffff;
108 len = sg_len;
109 if ((offset + sg_len) > 0x10000)
110 len = 0x10000 - offset;
111
112 ap->prd[pi].addr = cpu_to_le32(addr);
113 ap->prd[pi].flags_len = cpu_to_le32(len & 0xffff);
114 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", pi, addr, len);
115
116 pi++;
117 sg_len -= len;
118 addr += len;
119 }
120 }
121
122 ap->prd[pi - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
123}
124
125/**
126 * ata_fill_sg_dumb - Fill PCI IDE PRD table
127 * @qc: Metadata associated with taskfile to be transferred
128 *
129 * Fill PCI IDE PRD (scatter-gather) table with segments
130 * associated with the current disk command. Perform the fill
131 * so that we avoid writing any length 64K records for
132 * controllers that don't follow the spec.
133 *
134 * LOCKING:
135 * spin_lock_irqsave(host lock)
136 *
137 */
138static void ata_fill_sg_dumb(struct ata_queued_cmd *qc)
139{
140 struct ata_port *ap = qc->ap;
141 struct scatterlist *sg;
142 unsigned int si, pi;
143
144 pi = 0;
145 for_each_sg(qc->sg, sg, qc->n_elem, si) {
146 u32 addr, offset;
147 u32 sg_len, len, blen;
148
149 /* determine if physical DMA addr spans 64K boundary.
150 * Note h/w doesn't support 64-bit, so we unconditionally
151 * truncate dma_addr_t to u32.
152 */
153 addr = (u32) sg_dma_address(sg);
154 sg_len = sg_dma_len(sg);
155
156 while (sg_len) {
157 offset = addr & 0xffff;
158 len = sg_len;
159 if ((offset + sg_len) > 0x10000)
160 len = 0x10000 - offset;
161
162 blen = len & 0xffff;
163 ap->prd[pi].addr = cpu_to_le32(addr);
164 if (blen == 0) {
165 /* Some PATA chipsets like the CS5530 can't
166 cope with 0x0000 meaning 64K as the spec says */
167 ap->prd[pi].flags_len = cpu_to_le32(0x8000);
168 blen = 0x8000;
169 ap->prd[++pi].addr = cpu_to_le32(addr + 0x8000);
170 }
171 ap->prd[pi].flags_len = cpu_to_le32(blen);
172 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", pi, addr, len);
173
174 pi++;
175 sg_len -= len;
176 addr += len;
177 }
178 }
179
180 ap->prd[pi - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
181}
182
183/**
Tejun Heo9363c382008-04-07 22:47:16 +0900184 * ata_sff_qc_prep - Prepare taskfile for submission
Tejun Heo624d5c52008-03-25 22:16:41 +0900185 * @qc: Metadata associated with taskfile to be prepared
186 *
187 * Prepare ATA taskfile for submission.
188 *
189 * LOCKING:
190 * spin_lock_irqsave(host lock)
191 */
Tejun Heo9363c382008-04-07 22:47:16 +0900192void ata_sff_qc_prep(struct ata_queued_cmd *qc)
Tejun Heo624d5c52008-03-25 22:16:41 +0900193{
194 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
195 return;
196
197 ata_fill_sg(qc);
198}
199
200/**
Tejun Heo9363c382008-04-07 22:47:16 +0900201 * ata_sff_dumb_qc_prep - Prepare taskfile for submission
Tejun Heo624d5c52008-03-25 22:16:41 +0900202 * @qc: Metadata associated with taskfile to be prepared
203 *
204 * Prepare ATA taskfile for submission.
205 *
206 * LOCKING:
207 * spin_lock_irqsave(host lock)
208 */
Tejun Heo9363c382008-04-07 22:47:16 +0900209void ata_sff_dumb_qc_prep(struct ata_queued_cmd *qc)
Tejun Heo624d5c52008-03-25 22:16:41 +0900210{
211 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
212 return;
213
214 ata_fill_sg_dumb(qc);
215}
216
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500217/**
Tejun Heo9363c382008-04-07 22:47:16 +0900218 * ata_sff_check_status - Read device status reg & clear interrupt
Tejun Heo272f7882008-03-25 22:16:40 +0900219 * @ap: port where the device is
220 *
221 * Reads ATA taskfile status register for currently-selected device
222 * and return its value. This also clears pending interrupts
223 * from this device
224 *
225 * LOCKING:
226 * Inherited from caller.
227 */
Tejun Heo9363c382008-04-07 22:47:16 +0900228u8 ata_sff_check_status(struct ata_port *ap)
Tejun Heo272f7882008-03-25 22:16:40 +0900229{
230 return ioread8(ap->ioaddr.status_addr);
231}
232
233/**
Tejun Heo9363c382008-04-07 22:47:16 +0900234 * ata_sff_altstatus - Read device alternate status reg
Tejun Heo272f7882008-03-25 22:16:40 +0900235 * @ap: port where the device is
236 *
237 * Reads ATA taskfile alternate status register for
238 * currently-selected device and return its value.
239 *
240 * Note: may NOT be used as the check_altstatus() entry in
241 * ata_port_operations.
242 *
243 * LOCKING:
244 * Inherited from caller.
245 */
Tejun Heo9363c382008-04-07 22:47:16 +0900246u8 ata_sff_altstatus(struct ata_port *ap)
Tejun Heo272f7882008-03-25 22:16:40 +0900247{
Tejun Heo5682ed32008-04-07 22:47:16 +0900248 if (ap->ops->sff_check_altstatus)
249 return ap->ops->sff_check_altstatus(ap);
Tejun Heo272f7882008-03-25 22:16:40 +0900250
251 return ioread8(ap->ioaddr.altstatus_addr);
252}
253
254/**
Tejun Heo9363c382008-04-07 22:47:16 +0900255 * ata_sff_busy_sleep - sleep until BSY clears, or timeout
Tejun Heo624d5c52008-03-25 22:16:41 +0900256 * @ap: port containing status register to be polled
257 * @tmout_pat: impatience timeout
258 * @tmout: overall timeout
259 *
260 * Sleep until ATA Status register bit BSY clears,
261 * or a timeout occurs.
262 *
263 * LOCKING:
264 * Kernel thread context (may sleep).
265 *
266 * RETURNS:
267 * 0 on success, -errno otherwise.
268 */
Tejun Heo9363c382008-04-07 22:47:16 +0900269int ata_sff_busy_sleep(struct ata_port *ap,
270 unsigned long tmout_pat, unsigned long tmout)
Tejun Heo624d5c52008-03-25 22:16:41 +0900271{
272 unsigned long timer_start, timeout;
273 u8 status;
274
Tejun Heo9363c382008-04-07 22:47:16 +0900275 status = ata_sff_busy_wait(ap, ATA_BUSY, 300);
Tejun Heo624d5c52008-03-25 22:16:41 +0900276 timer_start = jiffies;
277 timeout = timer_start + tmout_pat;
278 while (status != 0xff && (status & ATA_BUSY) &&
279 time_before(jiffies, timeout)) {
280 msleep(50);
Tejun Heo9363c382008-04-07 22:47:16 +0900281 status = ata_sff_busy_wait(ap, ATA_BUSY, 3);
Tejun Heo624d5c52008-03-25 22:16:41 +0900282 }
283
284 if (status != 0xff && (status & ATA_BUSY))
285 ata_port_printk(ap, KERN_WARNING,
286 "port is slow to respond, please be patient "
287 "(Status 0x%x)\n", status);
288
289 timeout = timer_start + tmout;
290 while (status != 0xff && (status & ATA_BUSY) &&
291 time_before(jiffies, timeout)) {
292 msleep(50);
Tejun Heo5682ed32008-04-07 22:47:16 +0900293 status = ap->ops->sff_check_status(ap);
Tejun Heo624d5c52008-03-25 22:16:41 +0900294 }
295
296 if (status == 0xff)
297 return -ENODEV;
298
299 if (status & ATA_BUSY) {
300 ata_port_printk(ap, KERN_ERR, "port failed to respond "
301 "(%lu secs, Status 0x%x)\n",
302 tmout / HZ, status);
303 return -EBUSY;
304 }
305
306 return 0;
307}
308
309/**
Tejun Heo9363c382008-04-07 22:47:16 +0900310 * ata_sff_wait_ready - sleep until BSY clears, or timeout
Tejun Heo624d5c52008-03-25 22:16:41 +0900311 * @ap: port containing status register to be polled
312 * @deadline: deadline jiffies for the operation
313 *
314 * Sleep until ATA Status register bit BSY clears, or timeout
315 * occurs.
316 *
317 * LOCKING:
318 * Kernel thread context (may sleep).
319 *
320 * RETURNS:
321 * 0 on success, -errno otherwise.
322 */
Tejun Heo9363c382008-04-07 22:47:16 +0900323int ata_sff_wait_ready(struct ata_port *ap, unsigned long deadline)
Tejun Heo624d5c52008-03-25 22:16:41 +0900324{
325 unsigned long start = jiffies;
326 int warned = 0;
327
328 while (1) {
Tejun Heo5682ed32008-04-07 22:47:16 +0900329 u8 status = ap->ops->sff_check_status(ap);
Tejun Heo624d5c52008-03-25 22:16:41 +0900330 unsigned long now = jiffies;
331
332 if (!(status & ATA_BUSY))
333 return 0;
334 if (!ata_link_online(&ap->link) && status == 0xff)
335 return -ENODEV;
336 if (time_after(now, deadline))
337 return -EBUSY;
338
339 if (!warned && time_after(now, start + 5 * HZ) &&
340 (deadline - now > 3 * HZ)) {
341 ata_port_printk(ap, KERN_WARNING,
342 "port is slow to respond, please be patient "
343 "(Status 0x%x)\n", status);
344 warned = 1;
345 }
346
347 msleep(50);
348 }
349}
350
351/**
Tejun Heo9363c382008-04-07 22:47:16 +0900352 * ata_sff_dev_select - Select device 0/1 on ATA bus
Tejun Heo624d5c52008-03-25 22:16:41 +0900353 * @ap: ATA channel to manipulate
354 * @device: ATA device (numbered from zero) to select
355 *
356 * Use the method defined in the ATA specification to
357 * make either device 0, or device 1, active on the
358 * ATA channel. Works with both PIO and MMIO.
359 *
360 * May be used as the dev_select() entry in ata_port_operations.
361 *
362 * LOCKING:
363 * caller.
364 */
Tejun Heo9363c382008-04-07 22:47:16 +0900365void ata_sff_dev_select(struct ata_port *ap, unsigned int device)
Tejun Heo624d5c52008-03-25 22:16:41 +0900366{
367 u8 tmp;
368
369 if (device == 0)
370 tmp = ATA_DEVICE_OBS;
371 else
372 tmp = ATA_DEVICE_OBS | ATA_DEV1;
373
374 iowrite8(tmp, ap->ioaddr.device_addr);
Tejun Heo9363c382008-04-07 22:47:16 +0900375 ata_sff_pause(ap); /* needed; also flushes, for mmio */
Tejun Heo624d5c52008-03-25 22:16:41 +0900376}
377
378/**
379 * ata_dev_select - Select device 0/1 on ATA bus
380 * @ap: ATA channel to manipulate
381 * @device: ATA device (numbered from zero) to select
382 * @wait: non-zero to wait for Status register BSY bit to clear
383 * @can_sleep: non-zero if context allows sleeping
384 *
385 * Use the method defined in the ATA specification to
386 * make either device 0, or device 1, active on the
387 * ATA channel.
388 *
Tejun Heo9363c382008-04-07 22:47:16 +0900389 * This is a high-level version of ata_sff_dev_select(), which
390 * additionally provides the services of inserting the proper
391 * pauses and status polling, where needed.
Tejun Heo624d5c52008-03-25 22:16:41 +0900392 *
393 * LOCKING:
394 * caller.
395 */
396void ata_dev_select(struct ata_port *ap, unsigned int device,
397 unsigned int wait, unsigned int can_sleep)
398{
399 if (ata_msg_probe(ap))
400 ata_port_printk(ap, KERN_INFO, "ata_dev_select: ENTER, "
401 "device %u, wait %u\n", device, wait);
402
403 if (wait)
404 ata_wait_idle(ap);
405
Tejun Heo5682ed32008-04-07 22:47:16 +0900406 ap->ops->sff_dev_select(ap, device);
Tejun Heo624d5c52008-03-25 22:16:41 +0900407
408 if (wait) {
409 if (can_sleep && ap->link.device[device].class == ATA_DEV_ATAPI)
410 msleep(150);
411 ata_wait_idle(ap);
412 }
413}
414
415/**
Tejun Heo9363c382008-04-07 22:47:16 +0900416 * ata_sff_irq_on - Enable interrupts on a port.
Tejun Heo90088bb2006-10-09 11:10:26 +0900417 * @ap: Port on which interrupts are enabled.
418 *
419 * Enable interrupts on a legacy IDE device using MMIO or PIO,
420 * wait for idle, clear any pending interrupts.
421 *
422 * LOCKING:
423 * Inherited from caller.
424 */
Tejun Heo9363c382008-04-07 22:47:16 +0900425u8 ata_sff_irq_on(struct ata_port *ap)
Tejun Heo90088bb2006-10-09 11:10:26 +0900426{
427 struct ata_ioports *ioaddr = &ap->ioaddr;
428 u8 tmp;
429
430 ap->ctl &= ~ATA_NIEN;
431 ap->last_ctl = ap->ctl;
432
Tejun Heof659f0e42008-03-06 13:12:54 +0900433 if (ioaddr->ctl_addr)
434 iowrite8(ap->ctl, ioaddr->ctl_addr);
Tejun Heo90088bb2006-10-09 11:10:26 +0900435 tmp = ata_wait_idle(ap);
436
Tejun Heo5682ed32008-04-07 22:47:16 +0900437 ap->ops->sff_irq_clear(ap);
Tejun Heo90088bb2006-10-09 11:10:26 +0900438
439 return tmp;
440}
441
442/**
Tejun Heo9363c382008-04-07 22:47:16 +0900443 * ata_sff_irq_clear - Clear PCI IDE BMDMA interrupt.
Tejun Heo272f7882008-03-25 22:16:40 +0900444 * @ap: Port associated with this ATA transaction.
445 *
446 * Clear interrupt and error flags in DMA status register.
447 *
448 * May be used as the irq_clear() entry in ata_port_operations.
449 *
450 * LOCKING:
451 * spin_lock_irqsave(host lock)
452 */
Tejun Heo9363c382008-04-07 22:47:16 +0900453void ata_sff_irq_clear(struct ata_port *ap)
Tejun Heo272f7882008-03-25 22:16:40 +0900454{
455 void __iomem *mmio = ap->ioaddr.bmdma_addr;
456
457 if (!mmio)
458 return;
459
460 iowrite8(ioread8(mmio + ATA_DMA_STATUS), mmio + ATA_DMA_STATUS);
461}
462
463/**
Tejun Heo9363c382008-04-07 22:47:16 +0900464 * ata_sff_tf_load - send taskfile registers to host controller
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500465 * @ap: Port to which output is sent
466 * @tf: ATA taskfile register set
467 *
468 * Outputs ATA taskfile to standard ATA host controller.
469 *
470 * LOCKING:
471 * Inherited from caller.
472 */
Tejun Heo9363c382008-04-07 22:47:16 +0900473void ata_sff_tf_load(struct ata_port *ap, const struct ata_taskfile *tf)
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500474{
Tejun Heo0d5ff562007-02-01 15:06:36 +0900475 struct ata_ioports *ioaddr = &ap->ioaddr;
476 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
477
478 if (tf->ctl != ap->last_ctl) {
Tejun Heof659f0e42008-03-06 13:12:54 +0900479 if (ioaddr->ctl_addr)
480 iowrite8(tf->ctl, ioaddr->ctl_addr);
Tejun Heo0d5ff562007-02-01 15:06:36 +0900481 ap->last_ctl = tf->ctl;
482 ata_wait_idle(ap);
483 }
484
485 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
Tejun Heof659f0e42008-03-06 13:12:54 +0900486 WARN_ON(!ioaddr->ctl_addr);
Tejun Heo0d5ff562007-02-01 15:06:36 +0900487 iowrite8(tf->hob_feature, ioaddr->feature_addr);
488 iowrite8(tf->hob_nsect, ioaddr->nsect_addr);
489 iowrite8(tf->hob_lbal, ioaddr->lbal_addr);
490 iowrite8(tf->hob_lbam, ioaddr->lbam_addr);
491 iowrite8(tf->hob_lbah, ioaddr->lbah_addr);
492 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
493 tf->hob_feature,
494 tf->hob_nsect,
495 tf->hob_lbal,
496 tf->hob_lbam,
497 tf->hob_lbah);
498 }
499
500 if (is_addr) {
501 iowrite8(tf->feature, ioaddr->feature_addr);
502 iowrite8(tf->nsect, ioaddr->nsect_addr);
503 iowrite8(tf->lbal, ioaddr->lbal_addr);
504 iowrite8(tf->lbam, ioaddr->lbam_addr);
505 iowrite8(tf->lbah, ioaddr->lbah_addr);
506 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
507 tf->feature,
508 tf->nsect,
509 tf->lbal,
510 tf->lbam,
511 tf->lbah);
512 }
513
514 if (tf->flags & ATA_TFLAG_DEVICE) {
515 iowrite8(tf->device, ioaddr->device_addr);
516 VPRINTK("device 0x%X\n", tf->device);
517 }
518
519 ata_wait_idle(ap);
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500520}
521
522/**
Tejun Heo9363c382008-04-07 22:47:16 +0900523 * ata_sff_tf_read - input device's ATA taskfile shadow registers
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500524 * @ap: Port from which input is read
525 * @tf: ATA taskfile register set for storing input
526 *
527 * Reads ATA taskfile registers for currently-selected device
Alan Cox76548ed2007-11-19 14:34:56 +0000528 * into @tf. Assumes the device has a fully SFF compliant task file
529 * layout and behaviour. If you device does not (eg has a different
530 * status method) then you will need to provide a replacement tf_read
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500531 *
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500532 * LOCKING:
533 * Inherited from caller.
534 */
Tejun Heo9363c382008-04-07 22:47:16 +0900535void ata_sff_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500536{
Tejun Heo0d5ff562007-02-01 15:06:36 +0900537 struct ata_ioports *ioaddr = &ap->ioaddr;
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500538
Tejun Heo9363c382008-04-07 22:47:16 +0900539 tf->command = ata_sff_check_status(ap);
Tejun Heo0d5ff562007-02-01 15:06:36 +0900540 tf->feature = ioread8(ioaddr->error_addr);
541 tf->nsect = ioread8(ioaddr->nsect_addr);
542 tf->lbal = ioread8(ioaddr->lbal_addr);
543 tf->lbam = ioread8(ioaddr->lbam_addr);
544 tf->lbah = ioread8(ioaddr->lbah_addr);
545 tf->device = ioread8(ioaddr->device_addr);
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500546
Tejun Heo0d5ff562007-02-01 15:06:36 +0900547 if (tf->flags & ATA_TFLAG_LBA48) {
Tejun Heof659f0e42008-03-06 13:12:54 +0900548 if (likely(ioaddr->ctl_addr)) {
549 iowrite8(tf->ctl | ATA_HOB, ioaddr->ctl_addr);
550 tf->hob_feature = ioread8(ioaddr->error_addr);
551 tf->hob_nsect = ioread8(ioaddr->nsect_addr);
552 tf->hob_lbal = ioread8(ioaddr->lbal_addr);
553 tf->hob_lbam = ioread8(ioaddr->lbam_addr);
554 tf->hob_lbah = ioread8(ioaddr->lbah_addr);
555 iowrite8(tf->ctl, ioaddr->ctl_addr);
556 ap->last_ctl = tf->ctl;
557 } else
558 WARN_ON(1);
Tejun Heo0d5ff562007-02-01 15:06:36 +0900559 }
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500560}
561
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500562/**
Tejun Heo9363c382008-04-07 22:47:16 +0900563 * ata_sff_exec_command - issue ATA command to host controller
Tejun Heo272f7882008-03-25 22:16:40 +0900564 * @ap: port to which command is being issued
565 * @tf: ATA taskfile register set
Jeff Garzik1fdffbc2006-02-09 05:15:27 -0500566 *
Tejun Heo272f7882008-03-25 22:16:40 +0900567 * Issues ATA command, with proper synchronization with interrupt
568 * handler / other threads.
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500569 *
570 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400571 * spin_lock_irqsave(host lock)
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500572 */
Tejun Heo9363c382008-04-07 22:47:16 +0900573void ata_sff_exec_command(struct ata_port *ap, const struct ata_taskfile *tf)
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500574{
Tejun Heo272f7882008-03-25 22:16:40 +0900575 DPRINTK("ata%u: cmd 0x%X\n", ap->print_id, tf->command);
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500576
Tejun Heo272f7882008-03-25 22:16:40 +0900577 iowrite8(tf->command, ap->ioaddr.command_addr);
Tejun Heo9363c382008-04-07 22:47:16 +0900578 ata_sff_pause(ap);
Jeff Garzik2cc432e2006-03-23 00:32:03 -0500579}
580
Tejun Heo6d97dbd2006-05-15 20:58:24 +0900581/**
Tejun Heo624d5c52008-03-25 22:16:41 +0900582 * ata_tf_to_host - issue ATA taskfile to host controller
583 * @ap: port to which command is being issued
584 * @tf: ATA taskfile register set
585 *
586 * Issues ATA taskfile register set to ATA host controller,
587 * with proper synchronization with interrupt handler and
588 * other threads.
589 *
590 * LOCKING:
591 * spin_lock_irqsave(host lock)
592 */
593static inline void ata_tf_to_host(struct ata_port *ap,
594 const struct ata_taskfile *tf)
595{
Tejun Heo5682ed32008-04-07 22:47:16 +0900596 ap->ops->sff_tf_load(ap, tf);
597 ap->ops->sff_exec_command(ap, tf);
Tejun Heo624d5c52008-03-25 22:16:41 +0900598}
599
600/**
Tejun Heo9363c382008-04-07 22:47:16 +0900601 * ata_sff_data_xfer - Transfer data by PIO
Tejun Heo624d5c52008-03-25 22:16:41 +0900602 * @dev: device to target
603 * @buf: data buffer
604 * @buflen: buffer length
605 * @rw: read/write
606 *
607 * Transfer data from/to the device data register by PIO.
608 *
609 * LOCKING:
610 * Inherited from caller.
611 *
612 * RETURNS:
613 * Bytes consumed.
614 */
Tejun Heo9363c382008-04-07 22:47:16 +0900615unsigned int ata_sff_data_xfer(struct ata_device *dev, unsigned char *buf,
616 unsigned int buflen, int rw)
Tejun Heo624d5c52008-03-25 22:16:41 +0900617{
618 struct ata_port *ap = dev->link->ap;
619 void __iomem *data_addr = ap->ioaddr.data_addr;
620 unsigned int words = buflen >> 1;
621
622 /* Transfer multiple of 2 bytes */
623 if (rw == READ)
624 ioread16_rep(data_addr, buf, words);
625 else
626 iowrite16_rep(data_addr, buf, words);
627
628 /* Transfer trailing 1 byte, if any. */
629 if (unlikely(buflen & 0x01)) {
630 __le16 align_buf[1] = { 0 };
631 unsigned char *trailing_buf = buf + buflen - 1;
632
633 if (rw == READ) {
634 align_buf[0] = cpu_to_le16(ioread16(data_addr));
635 memcpy(trailing_buf, align_buf, 1);
636 } else {
637 memcpy(align_buf, trailing_buf, 1);
638 iowrite16(le16_to_cpu(align_buf[0]), data_addr);
639 }
640 words++;
641 }
642
643 return words << 1;
644}
645
646/**
Tejun Heo9363c382008-04-07 22:47:16 +0900647 * ata_sff_data_xfer_noirq - Transfer data by PIO
Tejun Heo624d5c52008-03-25 22:16:41 +0900648 * @dev: device to target
649 * @buf: data buffer
650 * @buflen: buffer length
651 * @rw: read/write
652 *
653 * Transfer data from/to the device data register by PIO. Do the
654 * transfer with interrupts disabled.
655 *
656 * LOCKING:
657 * Inherited from caller.
658 *
659 * RETURNS:
660 * Bytes consumed.
661 */
Tejun Heo9363c382008-04-07 22:47:16 +0900662unsigned int ata_sff_data_xfer_noirq(struct ata_device *dev, unsigned char *buf,
663 unsigned int buflen, int rw)
Tejun Heo624d5c52008-03-25 22:16:41 +0900664{
665 unsigned long flags;
666 unsigned int consumed;
667
668 local_irq_save(flags);
Tejun Heo9363c382008-04-07 22:47:16 +0900669 consumed = ata_sff_data_xfer(dev, buf, buflen, rw);
Tejun Heo624d5c52008-03-25 22:16:41 +0900670 local_irq_restore(flags);
671
672 return consumed;
673}
674
675/**
676 * ata_pio_sector - Transfer a sector of data.
677 * @qc: Command on going
678 *
679 * Transfer qc->sect_size bytes of data from/to the ATA device.
680 *
681 * LOCKING:
682 * Inherited from caller.
683 */
684static void ata_pio_sector(struct ata_queued_cmd *qc)
685{
686 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
687 struct ata_port *ap = qc->ap;
688 struct page *page;
689 unsigned int offset;
690 unsigned char *buf;
691
692 if (qc->curbytes == qc->nbytes - qc->sect_size)
693 ap->hsm_task_state = HSM_ST_LAST;
694
695 page = sg_page(qc->cursg);
696 offset = qc->cursg->offset + qc->cursg_ofs;
697
698 /* get the current page and offset */
699 page = nth_page(page, (offset >> PAGE_SHIFT));
700 offset %= PAGE_SIZE;
701
702 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
703
704 if (PageHighMem(page)) {
705 unsigned long flags;
706
707 /* FIXME: use a bounce buffer */
708 local_irq_save(flags);
709 buf = kmap_atomic(page, KM_IRQ0);
710
711 /* do the actual data transfer */
Tejun Heo5682ed32008-04-07 22:47:16 +0900712 ap->ops->sff_data_xfer(qc->dev, buf + offset, qc->sect_size,
713 do_write);
Tejun Heo624d5c52008-03-25 22:16:41 +0900714
715 kunmap_atomic(buf, KM_IRQ0);
716 local_irq_restore(flags);
717 } else {
718 buf = page_address(page);
Tejun Heo5682ed32008-04-07 22:47:16 +0900719 ap->ops->sff_data_xfer(qc->dev, buf + offset, qc->sect_size,
720 do_write);
Tejun Heo624d5c52008-03-25 22:16:41 +0900721 }
722
723 qc->curbytes += qc->sect_size;
724 qc->cursg_ofs += qc->sect_size;
725
726 if (qc->cursg_ofs == qc->cursg->length) {
727 qc->cursg = sg_next(qc->cursg);
728 qc->cursg_ofs = 0;
729 }
730}
731
732/**
733 * ata_pio_sectors - Transfer one or many sectors.
734 * @qc: Command on going
735 *
736 * Transfer one or many sectors of data from/to the
737 * ATA device for the DRQ request.
738 *
739 * LOCKING:
740 * Inherited from caller.
741 */
742static void ata_pio_sectors(struct ata_queued_cmd *qc)
743{
744 if (is_multi_taskfile(&qc->tf)) {
745 /* READ/WRITE MULTIPLE */
746 unsigned int nsect;
747
748 WARN_ON(qc->dev->multi_count == 0);
749
750 nsect = min((qc->nbytes - qc->curbytes) / qc->sect_size,
751 qc->dev->multi_count);
752 while (nsect--)
753 ata_pio_sector(qc);
754 } else
755 ata_pio_sector(qc);
756
Tejun Heo9363c382008-04-07 22:47:16 +0900757 ata_sff_altstatus(qc->ap); /* flush */
Tejun Heo624d5c52008-03-25 22:16:41 +0900758}
759
760/**
761 * atapi_send_cdb - Write CDB bytes to hardware
762 * @ap: Port to which ATAPI device is attached.
763 * @qc: Taskfile currently active
764 *
765 * When device has indicated its readiness to accept
766 * a CDB, this function is called. Send the CDB.
767 *
768 * LOCKING:
769 * caller.
770 */
771static void atapi_send_cdb(struct ata_port *ap, struct ata_queued_cmd *qc)
772{
773 /* send SCSI cdb */
774 DPRINTK("send cdb\n");
775 WARN_ON(qc->dev->cdb_len < 12);
776
Tejun Heo5682ed32008-04-07 22:47:16 +0900777 ap->ops->sff_data_xfer(qc->dev, qc->cdb, qc->dev->cdb_len, 1);
Tejun Heo9363c382008-04-07 22:47:16 +0900778 ata_sff_altstatus(ap); /* flush */
Tejun Heo624d5c52008-03-25 22:16:41 +0900779
780 switch (qc->tf.protocol) {
781 case ATAPI_PROT_PIO:
782 ap->hsm_task_state = HSM_ST;
783 break;
784 case ATAPI_PROT_NODATA:
785 ap->hsm_task_state = HSM_ST_LAST;
786 break;
787 case ATAPI_PROT_DMA:
788 ap->hsm_task_state = HSM_ST_LAST;
789 /* initiate bmdma */
790 ap->ops->bmdma_start(qc);
791 break;
792 }
793}
794
795/**
796 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
797 * @qc: Command on going
798 * @bytes: number of bytes
799 *
800 * Transfer Transfer data from/to the ATAPI device.
801 *
802 * LOCKING:
803 * Inherited from caller.
804 *
805 */
806static int __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
807{
808 int rw = (qc->tf.flags & ATA_TFLAG_WRITE) ? WRITE : READ;
809 struct ata_port *ap = qc->ap;
810 struct ata_device *dev = qc->dev;
811 struct ata_eh_info *ehi = &dev->link->eh_info;
812 struct scatterlist *sg;
813 struct page *page;
814 unsigned char *buf;
815 unsigned int offset, count, consumed;
816
817next_sg:
818 sg = qc->cursg;
819 if (unlikely(!sg)) {
820 ata_ehi_push_desc(ehi, "unexpected or too much trailing data "
821 "buf=%u cur=%u bytes=%u",
822 qc->nbytes, qc->curbytes, bytes);
823 return -1;
824 }
825
826 page = sg_page(sg);
827 offset = sg->offset + qc->cursg_ofs;
828
829 /* get the current page and offset */
830 page = nth_page(page, (offset >> PAGE_SHIFT));
831 offset %= PAGE_SIZE;
832
833 /* don't overrun current sg */
834 count = min(sg->length - qc->cursg_ofs, bytes);
835
836 /* don't cross page boundaries */
837 count = min(count, (unsigned int)PAGE_SIZE - offset);
838
839 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
840
841 if (PageHighMem(page)) {
842 unsigned long flags;
843
844 /* FIXME: use bounce buffer */
845 local_irq_save(flags);
846 buf = kmap_atomic(page, KM_IRQ0);
847
848 /* do the actual data transfer */
Tejun Heo5682ed32008-04-07 22:47:16 +0900849 consumed = ap->ops->sff_data_xfer(dev, buf + offset, count, rw);
Tejun Heo624d5c52008-03-25 22:16:41 +0900850
851 kunmap_atomic(buf, KM_IRQ0);
852 local_irq_restore(flags);
853 } else {
854 buf = page_address(page);
Tejun Heo5682ed32008-04-07 22:47:16 +0900855 consumed = ap->ops->sff_data_xfer(dev, buf + offset, count, rw);
Tejun Heo624d5c52008-03-25 22:16:41 +0900856 }
857
858 bytes -= min(bytes, consumed);
859 qc->curbytes += count;
860 qc->cursg_ofs += count;
861
862 if (qc->cursg_ofs == sg->length) {
863 qc->cursg = sg_next(qc->cursg);
864 qc->cursg_ofs = 0;
865 }
866
867 /* consumed can be larger than count only for the last transfer */
868 WARN_ON(qc->cursg && count != consumed);
869
870 if (bytes)
871 goto next_sg;
872 return 0;
873}
874
875/**
876 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
877 * @qc: Command on going
878 *
879 * Transfer Transfer data from/to the ATAPI device.
880 *
881 * LOCKING:
882 * Inherited from caller.
883 */
884static void atapi_pio_bytes(struct ata_queued_cmd *qc)
885{
886 struct ata_port *ap = qc->ap;
887 struct ata_device *dev = qc->dev;
888 struct ata_eh_info *ehi = &dev->link->eh_info;
889 unsigned int ireason, bc_lo, bc_hi, bytes;
890 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
891
892 /* Abuse qc->result_tf for temp storage of intermediate TF
893 * here to save some kernel stack usage.
894 * For normal completion, qc->result_tf is not relevant. For
895 * error, qc->result_tf is later overwritten by ata_qc_complete().
896 * So, the correctness of qc->result_tf is not affected.
897 */
Tejun Heo5682ed32008-04-07 22:47:16 +0900898 ap->ops->sff_tf_read(ap, &qc->result_tf);
Tejun Heo624d5c52008-03-25 22:16:41 +0900899 ireason = qc->result_tf.nsect;
900 bc_lo = qc->result_tf.lbam;
901 bc_hi = qc->result_tf.lbah;
902 bytes = (bc_hi << 8) | bc_lo;
903
904 /* shall be cleared to zero, indicating xfer of data */
905 if (unlikely(ireason & (1 << 0)))
906 goto atapi_check;
907
908 /* make sure transfer direction matches expected */
909 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
910 if (unlikely(do_write != i_write))
911 goto atapi_check;
912
913 if (unlikely(!bytes))
914 goto atapi_check;
915
916 VPRINTK("ata%u: xfering %d bytes\n", ap->print_id, bytes);
917
918 if (unlikely(__atapi_pio_bytes(qc, bytes)))
919 goto err_out;
Tejun Heo9363c382008-04-07 22:47:16 +0900920 ata_sff_altstatus(ap); /* flush */
Tejun Heo624d5c52008-03-25 22:16:41 +0900921
922 return;
923
924 atapi_check:
925 ata_ehi_push_desc(ehi, "ATAPI check failed (ireason=0x%x bytes=%u)",
926 ireason, bytes);
927 err_out:
928 qc->err_mask |= AC_ERR_HSM;
929 ap->hsm_task_state = HSM_ST_ERR;
930}
931
932/**
933 * ata_hsm_ok_in_wq - Check if the qc can be handled in the workqueue.
934 * @ap: the target ata_port
935 * @qc: qc on going
936 *
937 * RETURNS:
938 * 1 if ok in workqueue, 0 otherwise.
939 */
940static inline int ata_hsm_ok_in_wq(struct ata_port *ap, struct ata_queued_cmd *qc)
941{
942 if (qc->tf.flags & ATA_TFLAG_POLLING)
943 return 1;
944
945 if (ap->hsm_task_state == HSM_ST_FIRST) {
946 if (qc->tf.protocol == ATA_PROT_PIO &&
947 (qc->tf.flags & ATA_TFLAG_WRITE))
948 return 1;
949
950 if (ata_is_atapi(qc->tf.protocol) &&
951 !(qc->dev->flags & ATA_DFLAG_CDB_INTR))
952 return 1;
953 }
954
955 return 0;
956}
957
958/**
959 * ata_hsm_qc_complete - finish a qc running on standard HSM
960 * @qc: Command to complete
961 * @in_wq: 1 if called from workqueue, 0 otherwise
962 *
963 * Finish @qc which is running on standard HSM.
964 *
965 * LOCKING:
966 * If @in_wq is zero, spin_lock_irqsave(host lock).
967 * Otherwise, none on entry and grabs host lock.
968 */
969static void ata_hsm_qc_complete(struct ata_queued_cmd *qc, int in_wq)
970{
971 struct ata_port *ap = qc->ap;
972 unsigned long flags;
973
974 if (ap->ops->error_handler) {
975 if (in_wq) {
976 spin_lock_irqsave(ap->lock, flags);
977
978 /* EH might have kicked in while host lock is
979 * released.
980 */
981 qc = ata_qc_from_tag(ap, qc->tag);
982 if (qc) {
983 if (likely(!(qc->err_mask & AC_ERR_HSM))) {
Tejun Heo5682ed32008-04-07 22:47:16 +0900984 ap->ops->sff_irq_on(ap);
Tejun Heo624d5c52008-03-25 22:16:41 +0900985 ata_qc_complete(qc);
986 } else
987 ata_port_freeze(ap);
988 }
989
990 spin_unlock_irqrestore(ap->lock, flags);
991 } else {
992 if (likely(!(qc->err_mask & AC_ERR_HSM)))
993 ata_qc_complete(qc);
994 else
995 ata_port_freeze(ap);
996 }
997 } else {
998 if (in_wq) {
999 spin_lock_irqsave(ap->lock, flags);
Tejun Heo5682ed32008-04-07 22:47:16 +09001000 ap->ops->sff_irq_on(ap);
Tejun Heo624d5c52008-03-25 22:16:41 +09001001 ata_qc_complete(qc);
1002 spin_unlock_irqrestore(ap->lock, flags);
1003 } else
1004 ata_qc_complete(qc);
1005 }
1006}
1007
1008/**
Tejun Heo9363c382008-04-07 22:47:16 +09001009 * ata_sff_hsm_move - move the HSM to the next state.
Tejun Heo624d5c52008-03-25 22:16:41 +09001010 * @ap: the target ata_port
1011 * @qc: qc on going
1012 * @status: current device status
1013 * @in_wq: 1 if called from workqueue, 0 otherwise
1014 *
1015 * RETURNS:
1016 * 1 when poll next status needed, 0 otherwise.
1017 */
Tejun Heo9363c382008-04-07 22:47:16 +09001018int ata_sff_hsm_move(struct ata_port *ap, struct ata_queued_cmd *qc,
1019 u8 status, int in_wq)
Tejun Heo624d5c52008-03-25 22:16:41 +09001020{
1021 unsigned long flags = 0;
1022 int poll_next;
1023
1024 WARN_ON((qc->flags & ATA_QCFLAG_ACTIVE) == 0);
1025
Tejun Heo9363c382008-04-07 22:47:16 +09001026 /* Make sure ata_sff_qc_issue() does not throw things
Tejun Heo624d5c52008-03-25 22:16:41 +09001027 * like DMA polling into the workqueue. Notice that
1028 * in_wq is not equivalent to (qc->tf.flags & ATA_TFLAG_POLLING).
1029 */
1030 WARN_ON(in_wq != ata_hsm_ok_in_wq(ap, qc));
1031
1032fsm_start:
1033 DPRINTK("ata%u: protocol %d task_state %d (dev_stat 0x%X)\n",
1034 ap->print_id, qc->tf.protocol, ap->hsm_task_state, status);
1035
1036 switch (ap->hsm_task_state) {
1037 case HSM_ST_FIRST:
1038 /* Send first data block or PACKET CDB */
1039
1040 /* If polling, we will stay in the work queue after
1041 * sending the data. Otherwise, interrupt handler
1042 * takes over after sending the data.
1043 */
1044 poll_next = (qc->tf.flags & ATA_TFLAG_POLLING);
1045
1046 /* check device status */
1047 if (unlikely((status & ATA_DRQ) == 0)) {
1048 /* handle BSY=0, DRQ=0 as error */
1049 if (likely(status & (ATA_ERR | ATA_DF)))
1050 /* device stops HSM for abort/error */
1051 qc->err_mask |= AC_ERR_DEV;
1052 else
1053 /* HSM violation. Let EH handle this */
1054 qc->err_mask |= AC_ERR_HSM;
1055
1056 ap->hsm_task_state = HSM_ST_ERR;
1057 goto fsm_start;
1058 }
1059
1060 /* Device should not ask for data transfer (DRQ=1)
1061 * when it finds something wrong.
1062 * We ignore DRQ here and stop the HSM by
1063 * changing hsm_task_state to HSM_ST_ERR and
1064 * let the EH abort the command or reset the device.
1065 */
1066 if (unlikely(status & (ATA_ERR | ATA_DF))) {
1067 /* Some ATAPI tape drives forget to clear the ERR bit
1068 * when doing the next command (mostly request sense).
1069 * We ignore ERR here to workaround and proceed sending
1070 * the CDB.
1071 */
1072 if (!(qc->dev->horkage & ATA_HORKAGE_STUCK_ERR)) {
1073 ata_port_printk(ap, KERN_WARNING,
1074 "DRQ=1 with device error, "
1075 "dev_stat 0x%X\n", status);
1076 qc->err_mask |= AC_ERR_HSM;
1077 ap->hsm_task_state = HSM_ST_ERR;
1078 goto fsm_start;
1079 }
1080 }
1081
1082 /* Send the CDB (atapi) or the first data block (ata pio out).
1083 * During the state transition, interrupt handler shouldn't
1084 * be invoked before the data transfer is complete and
1085 * hsm_task_state is changed. Hence, the following locking.
1086 */
1087 if (in_wq)
1088 spin_lock_irqsave(ap->lock, flags);
1089
1090 if (qc->tf.protocol == ATA_PROT_PIO) {
1091 /* PIO data out protocol.
1092 * send first data block.
1093 */
1094
1095 /* ata_pio_sectors() might change the state
1096 * to HSM_ST_LAST. so, the state is changed here
1097 * before ata_pio_sectors().
1098 */
1099 ap->hsm_task_state = HSM_ST;
1100 ata_pio_sectors(qc);
1101 } else
1102 /* send CDB */
1103 atapi_send_cdb(ap, qc);
1104
1105 if (in_wq)
1106 spin_unlock_irqrestore(ap->lock, flags);
1107
1108 /* if polling, ata_pio_task() handles the rest.
1109 * otherwise, interrupt handler takes over from here.
1110 */
1111 break;
1112
1113 case HSM_ST:
1114 /* complete command or read/write the data register */
1115 if (qc->tf.protocol == ATAPI_PROT_PIO) {
1116 /* ATAPI PIO protocol */
1117 if ((status & ATA_DRQ) == 0) {
1118 /* No more data to transfer or device error.
1119 * Device error will be tagged in HSM_ST_LAST.
1120 */
1121 ap->hsm_task_state = HSM_ST_LAST;
1122 goto fsm_start;
1123 }
1124
1125 /* Device should not ask for data transfer (DRQ=1)
1126 * when it finds something wrong.
1127 * We ignore DRQ here and stop the HSM by
1128 * changing hsm_task_state to HSM_ST_ERR and
1129 * let the EH abort the command or reset the device.
1130 */
1131 if (unlikely(status & (ATA_ERR | ATA_DF))) {
1132 ata_port_printk(ap, KERN_WARNING, "DRQ=1 with "
1133 "device error, dev_stat 0x%X\n",
1134 status);
1135 qc->err_mask |= AC_ERR_HSM;
1136 ap->hsm_task_state = HSM_ST_ERR;
1137 goto fsm_start;
1138 }
1139
1140 atapi_pio_bytes(qc);
1141
1142 if (unlikely(ap->hsm_task_state == HSM_ST_ERR))
1143 /* bad ireason reported by device */
1144 goto fsm_start;
1145
1146 } else {
1147 /* ATA PIO protocol */
1148 if (unlikely((status & ATA_DRQ) == 0)) {
1149 /* handle BSY=0, DRQ=0 as error */
1150 if (likely(status & (ATA_ERR | ATA_DF)))
1151 /* device stops HSM for abort/error */
1152 qc->err_mask |= AC_ERR_DEV;
1153 else
1154 /* HSM violation. Let EH handle this.
1155 * Phantom devices also trigger this
1156 * condition. Mark hint.
1157 */
1158 qc->err_mask |= AC_ERR_HSM |
1159 AC_ERR_NODEV_HINT;
1160
1161 ap->hsm_task_state = HSM_ST_ERR;
1162 goto fsm_start;
1163 }
1164
1165 /* For PIO reads, some devices may ask for
1166 * data transfer (DRQ=1) alone with ERR=1.
1167 * We respect DRQ here and transfer one
1168 * block of junk data before changing the
1169 * hsm_task_state to HSM_ST_ERR.
1170 *
1171 * For PIO writes, ERR=1 DRQ=1 doesn't make
1172 * sense since the data block has been
1173 * transferred to the device.
1174 */
1175 if (unlikely(status & (ATA_ERR | ATA_DF))) {
1176 /* data might be corrputed */
1177 qc->err_mask |= AC_ERR_DEV;
1178
1179 if (!(qc->tf.flags & ATA_TFLAG_WRITE)) {
1180 ata_pio_sectors(qc);
1181 status = ata_wait_idle(ap);
1182 }
1183
1184 if (status & (ATA_BUSY | ATA_DRQ))
1185 qc->err_mask |= AC_ERR_HSM;
1186
1187 /* ata_pio_sectors() might change the
1188 * state to HSM_ST_LAST. so, the state
1189 * is changed after ata_pio_sectors().
1190 */
1191 ap->hsm_task_state = HSM_ST_ERR;
1192 goto fsm_start;
1193 }
1194
1195 ata_pio_sectors(qc);
1196
1197 if (ap->hsm_task_state == HSM_ST_LAST &&
1198 (!(qc->tf.flags & ATA_TFLAG_WRITE))) {
1199 /* all data read */
1200 status = ata_wait_idle(ap);
1201 goto fsm_start;
1202 }
1203 }
1204
1205 poll_next = 1;
1206 break;
1207
1208 case HSM_ST_LAST:
1209 if (unlikely(!ata_ok(status))) {
1210 qc->err_mask |= __ac_err_mask(status);
1211 ap->hsm_task_state = HSM_ST_ERR;
1212 goto fsm_start;
1213 }
1214
1215 /* no more data to transfer */
1216 DPRINTK("ata%u: dev %u command complete, drv_stat 0x%x\n",
1217 ap->print_id, qc->dev->devno, status);
1218
1219 WARN_ON(qc->err_mask);
1220
1221 ap->hsm_task_state = HSM_ST_IDLE;
1222
1223 /* complete taskfile transaction */
1224 ata_hsm_qc_complete(qc, in_wq);
1225
1226 poll_next = 0;
1227 break;
1228
1229 case HSM_ST_ERR:
1230 /* make sure qc->err_mask is available to
1231 * know what's wrong and recover
1232 */
1233 WARN_ON(qc->err_mask == 0);
1234
1235 ap->hsm_task_state = HSM_ST_IDLE;
1236
1237 /* complete taskfile transaction */
1238 ata_hsm_qc_complete(qc, in_wq);
1239
1240 poll_next = 0;
1241 break;
1242 default:
1243 poll_next = 0;
1244 BUG();
1245 }
1246
1247 return poll_next;
1248}
1249
1250void ata_pio_task(struct work_struct *work)
1251{
1252 struct ata_port *ap =
1253 container_of(work, struct ata_port, port_task.work);
1254 struct ata_queued_cmd *qc = ap->port_task_data;
1255 u8 status;
1256 int poll_next;
1257
1258fsm_start:
1259 WARN_ON(ap->hsm_task_state == HSM_ST_IDLE);
1260
1261 /*
1262 * This is purely heuristic. This is a fast path.
1263 * Sometimes when we enter, BSY will be cleared in
1264 * a chk-status or two. If not, the drive is probably seeking
1265 * or something. Snooze for a couple msecs, then
1266 * chk-status again. If still busy, queue delayed work.
1267 */
Tejun Heo9363c382008-04-07 22:47:16 +09001268 status = ata_sff_busy_wait(ap, ATA_BUSY, 5);
Tejun Heo624d5c52008-03-25 22:16:41 +09001269 if (status & ATA_BUSY) {
1270 msleep(2);
Tejun Heo9363c382008-04-07 22:47:16 +09001271 status = ata_sff_busy_wait(ap, ATA_BUSY, 10);
Tejun Heo624d5c52008-03-25 22:16:41 +09001272 if (status & ATA_BUSY) {
1273 ata_pio_queue_task(ap, qc, ATA_SHORT_PAUSE);
1274 return;
1275 }
1276 }
1277
1278 /* move the HSM */
Tejun Heo9363c382008-04-07 22:47:16 +09001279 poll_next = ata_sff_hsm_move(ap, qc, status, 1);
Tejun Heo624d5c52008-03-25 22:16:41 +09001280
1281 /* another command or interrupt handler
1282 * may be running at this point.
1283 */
1284 if (poll_next)
1285 goto fsm_start;
1286}
1287
1288/**
Tejun Heo9363c382008-04-07 22:47:16 +09001289 * ata_sff_qc_issue - issue taskfile to device in proto-dependent manner
Tejun Heo624d5c52008-03-25 22:16:41 +09001290 * @qc: command to issue to device
1291 *
1292 * Using various libata functions and hooks, this function
1293 * starts an ATA command. ATA commands are grouped into
1294 * classes called "protocols", and issuing each type of protocol
1295 * is slightly different.
1296 *
1297 * May be used as the qc_issue() entry in ata_port_operations.
1298 *
1299 * LOCKING:
1300 * spin_lock_irqsave(host lock)
1301 *
1302 * RETURNS:
1303 * Zero on success, AC_ERR_* mask on failure
1304 */
Tejun Heo9363c382008-04-07 22:47:16 +09001305unsigned int ata_sff_qc_issue(struct ata_queued_cmd *qc)
Tejun Heo624d5c52008-03-25 22:16:41 +09001306{
1307 struct ata_port *ap = qc->ap;
1308
1309 /* Use polling pio if the LLD doesn't handle
1310 * interrupt driven pio and atapi CDB interrupt.
1311 */
1312 if (ap->flags & ATA_FLAG_PIO_POLLING) {
1313 switch (qc->tf.protocol) {
1314 case ATA_PROT_PIO:
1315 case ATA_PROT_NODATA:
1316 case ATAPI_PROT_PIO:
1317 case ATAPI_PROT_NODATA:
1318 qc->tf.flags |= ATA_TFLAG_POLLING;
1319 break;
1320 case ATAPI_PROT_DMA:
1321 if (qc->dev->flags & ATA_DFLAG_CDB_INTR)
1322 /* see ata_dma_blacklisted() */
1323 BUG();
1324 break;
1325 default:
1326 break;
1327 }
1328 }
1329
1330 /* select the device */
1331 ata_dev_select(ap, qc->dev->devno, 1, 0);
1332
1333 /* start the command */
1334 switch (qc->tf.protocol) {
1335 case ATA_PROT_NODATA:
1336 if (qc->tf.flags & ATA_TFLAG_POLLING)
1337 ata_qc_set_polling(qc);
1338
1339 ata_tf_to_host(ap, &qc->tf);
1340 ap->hsm_task_state = HSM_ST_LAST;
1341
1342 if (qc->tf.flags & ATA_TFLAG_POLLING)
1343 ata_pio_queue_task(ap, qc, 0);
1344
1345 break;
1346
1347 case ATA_PROT_DMA:
1348 WARN_ON(qc->tf.flags & ATA_TFLAG_POLLING);
1349
Tejun Heo5682ed32008-04-07 22:47:16 +09001350 ap->ops->sff_tf_load(ap, &qc->tf); /* load tf registers */
Tejun Heo624d5c52008-03-25 22:16:41 +09001351 ap->ops->bmdma_setup(qc); /* set up bmdma */
1352 ap->ops->bmdma_start(qc); /* initiate bmdma */
1353 ap->hsm_task_state = HSM_ST_LAST;
1354 break;
1355
1356 case ATA_PROT_PIO:
1357 if (qc->tf.flags & ATA_TFLAG_POLLING)
1358 ata_qc_set_polling(qc);
1359
1360 ata_tf_to_host(ap, &qc->tf);
1361
1362 if (qc->tf.flags & ATA_TFLAG_WRITE) {
1363 /* PIO data out protocol */
1364 ap->hsm_task_state = HSM_ST_FIRST;
1365 ata_pio_queue_task(ap, qc, 0);
1366
1367 /* always send first data block using
1368 * the ata_pio_task() codepath.
1369 */
1370 } else {
1371 /* PIO data in protocol */
1372 ap->hsm_task_state = HSM_ST;
1373
1374 if (qc->tf.flags & ATA_TFLAG_POLLING)
1375 ata_pio_queue_task(ap, qc, 0);
1376
1377 /* if polling, ata_pio_task() handles the rest.
1378 * otherwise, interrupt handler takes over from here.
1379 */
1380 }
1381
1382 break;
1383
1384 case ATAPI_PROT_PIO:
1385 case ATAPI_PROT_NODATA:
1386 if (qc->tf.flags & ATA_TFLAG_POLLING)
1387 ata_qc_set_polling(qc);
1388
1389 ata_tf_to_host(ap, &qc->tf);
1390
1391 ap->hsm_task_state = HSM_ST_FIRST;
1392
1393 /* send cdb by polling if no cdb interrupt */
1394 if ((!(qc->dev->flags & ATA_DFLAG_CDB_INTR)) ||
1395 (qc->tf.flags & ATA_TFLAG_POLLING))
1396 ata_pio_queue_task(ap, qc, 0);
1397 break;
1398
1399 case ATAPI_PROT_DMA:
1400 WARN_ON(qc->tf.flags & ATA_TFLAG_POLLING);
1401
Tejun Heo5682ed32008-04-07 22:47:16 +09001402 ap->ops->sff_tf_load(ap, &qc->tf); /* load tf registers */
Tejun Heo624d5c52008-03-25 22:16:41 +09001403 ap->ops->bmdma_setup(qc); /* set up bmdma */
1404 ap->hsm_task_state = HSM_ST_FIRST;
1405
1406 /* send cdb by polling if no cdb interrupt */
1407 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
1408 ata_pio_queue_task(ap, qc, 0);
1409 break;
1410
1411 default:
1412 WARN_ON(1);
1413 return AC_ERR_SYSTEM;
1414 }
1415
1416 return 0;
1417}
1418
1419/**
Tejun Heo9363c382008-04-07 22:47:16 +09001420 * ata_sff_host_intr - Handle host interrupt for given (port, task)
Tejun Heo624d5c52008-03-25 22:16:41 +09001421 * @ap: Port on which interrupt arrived (possibly...)
1422 * @qc: Taskfile currently active in engine
1423 *
1424 * Handle host interrupt for given queued command. Currently,
1425 * only DMA interrupts are handled. All other commands are
1426 * handled via polling with interrupts disabled (nIEN bit).
1427 *
1428 * LOCKING:
1429 * spin_lock_irqsave(host lock)
1430 *
1431 * RETURNS:
1432 * One if interrupt was handled, zero if not (shared irq).
1433 */
Tejun Heo9363c382008-04-07 22:47:16 +09001434inline unsigned int ata_sff_host_intr(struct ata_port *ap,
1435 struct ata_queued_cmd *qc)
Tejun Heo624d5c52008-03-25 22:16:41 +09001436{
1437 struct ata_eh_info *ehi = &ap->link.eh_info;
1438 u8 status, host_stat = 0;
1439
1440 VPRINTK("ata%u: protocol %d task_state %d\n",
1441 ap->print_id, qc->tf.protocol, ap->hsm_task_state);
1442
1443 /* Check whether we are expecting interrupt in this state */
1444 switch (ap->hsm_task_state) {
1445 case HSM_ST_FIRST:
1446 /* Some pre-ATAPI-4 devices assert INTRQ
1447 * at this state when ready to receive CDB.
1448 */
1449
1450 /* Check the ATA_DFLAG_CDB_INTR flag is enough here.
1451 * The flag was turned on only for atapi devices. No
1452 * need to check ata_is_atapi(qc->tf.protocol) again.
1453 */
1454 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
1455 goto idle_irq;
1456 break;
1457 case HSM_ST_LAST:
1458 if (qc->tf.protocol == ATA_PROT_DMA ||
1459 qc->tf.protocol == ATAPI_PROT_DMA) {
1460 /* check status of DMA engine */
1461 host_stat = ap->ops->bmdma_status(ap);
1462 VPRINTK("ata%u: host_stat 0x%X\n",
1463 ap->print_id, host_stat);
1464
1465 /* if it's not our irq... */
1466 if (!(host_stat & ATA_DMA_INTR))
1467 goto idle_irq;
1468
1469 /* before we do anything else, clear DMA-Start bit */
1470 ap->ops->bmdma_stop(qc);
1471
1472 if (unlikely(host_stat & ATA_DMA_ERR)) {
1473 /* error when transfering data to/from memory */
1474 qc->err_mask |= AC_ERR_HOST_BUS;
1475 ap->hsm_task_state = HSM_ST_ERR;
1476 }
1477 }
1478 break;
1479 case HSM_ST:
1480 break;
1481 default:
1482 goto idle_irq;
1483 }
1484
1485 /* check altstatus */
Tejun Heo9363c382008-04-07 22:47:16 +09001486 status = ata_sff_altstatus(ap);
Tejun Heo624d5c52008-03-25 22:16:41 +09001487 if (status & ATA_BUSY)
1488 goto idle_irq;
1489
1490 /* check main status, clearing INTRQ */
Tejun Heo5682ed32008-04-07 22:47:16 +09001491 status = ap->ops->sff_check_status(ap);
Tejun Heo624d5c52008-03-25 22:16:41 +09001492 if (unlikely(status & ATA_BUSY))
1493 goto idle_irq;
1494
1495 /* ack bmdma irq events */
Tejun Heo5682ed32008-04-07 22:47:16 +09001496 ap->ops->sff_irq_clear(ap);
Tejun Heo624d5c52008-03-25 22:16:41 +09001497
Tejun Heo9363c382008-04-07 22:47:16 +09001498 ata_sff_hsm_move(ap, qc, status, 0);
Tejun Heo624d5c52008-03-25 22:16:41 +09001499
1500 if (unlikely(qc->err_mask) && (qc->tf.protocol == ATA_PROT_DMA ||
1501 qc->tf.protocol == ATAPI_PROT_DMA))
1502 ata_ehi_push_desc(ehi, "BMDMA stat 0x%x", host_stat);
1503
1504 return 1; /* irq handled */
1505
1506idle_irq:
1507 ap->stats.idle_irq++;
1508
1509#ifdef ATA_IRQ_TRAP
1510 if ((ap->stats.idle_irq % 1000) == 0) {
Tejun Heo5682ed32008-04-07 22:47:16 +09001511 ap->ops->sff_check_status(ap);
1512 ap->ops->sff_irq_clear(ap);
Tejun Heo624d5c52008-03-25 22:16:41 +09001513 ata_port_printk(ap, KERN_WARNING, "irq trap\n");
1514 return 1;
1515 }
1516#endif
1517 return 0; /* irq not handled */
1518}
1519
1520/**
Tejun Heo9363c382008-04-07 22:47:16 +09001521 * ata_sff_interrupt - Default ATA host interrupt handler
Tejun Heo624d5c52008-03-25 22:16:41 +09001522 * @irq: irq line (unused)
1523 * @dev_instance: pointer to our ata_host information structure
1524 *
1525 * Default interrupt handler for PCI IDE devices. Calls
Tejun Heo9363c382008-04-07 22:47:16 +09001526 * ata_sff_host_intr() for each port that is not disabled.
Tejun Heo624d5c52008-03-25 22:16:41 +09001527 *
1528 * LOCKING:
1529 * Obtains host lock during operation.
1530 *
1531 * RETURNS:
1532 * IRQ_NONE or IRQ_HANDLED.
1533 */
Tejun Heo9363c382008-04-07 22:47:16 +09001534irqreturn_t ata_sff_interrupt(int irq, void *dev_instance)
Tejun Heo624d5c52008-03-25 22:16:41 +09001535{
1536 struct ata_host *host = dev_instance;
1537 unsigned int i;
1538 unsigned int handled = 0;
1539 unsigned long flags;
1540
1541 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
1542 spin_lock_irqsave(&host->lock, flags);
1543
1544 for (i = 0; i < host->n_ports; i++) {
1545 struct ata_port *ap;
1546
1547 ap = host->ports[i];
1548 if (ap &&
1549 !(ap->flags & ATA_FLAG_DISABLED)) {
1550 struct ata_queued_cmd *qc;
1551
1552 qc = ata_qc_from_tag(ap, ap->link.active_tag);
1553 if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING)) &&
1554 (qc->flags & ATA_QCFLAG_ACTIVE))
Tejun Heo9363c382008-04-07 22:47:16 +09001555 handled |= ata_sff_host_intr(ap, qc);
Tejun Heo624d5c52008-03-25 22:16:41 +09001556 }
1557 }
1558
1559 spin_unlock_irqrestore(&host->lock, flags);
1560
1561 return IRQ_RETVAL(handled);
1562}
1563
1564/**
Tejun Heo9363c382008-04-07 22:47:16 +09001565 * ata_sff_freeze - Freeze SFF controller port
Tejun Heo6d97dbd2006-05-15 20:58:24 +09001566 * @ap: port to freeze
1567 *
1568 * Freeze BMDMA controller port.
1569 *
1570 * LOCKING:
1571 * Inherited from caller.
1572 */
Tejun Heo9363c382008-04-07 22:47:16 +09001573void ata_sff_freeze(struct ata_port *ap)
Tejun Heo6d97dbd2006-05-15 20:58:24 +09001574{
1575 struct ata_ioports *ioaddr = &ap->ioaddr;
1576
1577 ap->ctl |= ATA_NIEN;
1578 ap->last_ctl = ap->ctl;
1579
Tejun Heof659f0e42008-03-06 13:12:54 +09001580 if (ioaddr->ctl_addr)
1581 iowrite8(ap->ctl, ioaddr->ctl_addr);
Tejun Heo0f0a3ad2006-11-17 12:24:22 +09001582
1583 /* Under certain circumstances, some controllers raise IRQ on
1584 * ATA_NIEN manipulation. Also, many controllers fail to mask
1585 * previously pending IRQ on ATA_NIEN assertion. Clear it.
1586 */
Tejun Heo5682ed32008-04-07 22:47:16 +09001587 ap->ops->sff_check_status(ap);
Tejun Heo0f0a3ad2006-11-17 12:24:22 +09001588
Tejun Heo5682ed32008-04-07 22:47:16 +09001589 ap->ops->sff_irq_clear(ap);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09001590}
1591
1592/**
Tejun Heo9363c382008-04-07 22:47:16 +09001593 * ata_sff_thaw - Thaw SFF controller port
Tejun Heo6d97dbd2006-05-15 20:58:24 +09001594 * @ap: port to thaw
1595 *
Tejun Heo9363c382008-04-07 22:47:16 +09001596 * Thaw SFF controller port.
Tejun Heo6d97dbd2006-05-15 20:58:24 +09001597 *
1598 * LOCKING:
1599 * Inherited from caller.
1600 */
Tejun Heo9363c382008-04-07 22:47:16 +09001601void ata_sff_thaw(struct ata_port *ap)
Tejun Heo6d97dbd2006-05-15 20:58:24 +09001602{
1603 /* clear & re-enable interrupts */
Tejun Heo5682ed32008-04-07 22:47:16 +09001604 ap->ops->sff_check_status(ap);
1605 ap->ops->sff_irq_clear(ap);
1606 ap->ops->sff_irq_on(ap);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09001607}
1608
1609/**
Tejun Heo624d5c52008-03-25 22:16:41 +09001610 * ata_devchk - PATA device presence detection
1611 * @ap: ATA channel to examine
1612 * @device: Device to examine (starting at zero)
1613 *
1614 * This technique was originally described in
1615 * Hale Landis's ATADRVR (www.ata-atapi.com), and
1616 * later found its way into the ATA/ATAPI spec.
1617 *
1618 * Write a pattern to the ATA shadow registers,
1619 * and if a device is present, it will respond by
1620 * correctly storing and echoing back the
1621 * ATA shadow register contents.
1622 *
1623 * LOCKING:
1624 * caller.
1625 */
1626static unsigned int ata_devchk(struct ata_port *ap, unsigned int device)
1627{
1628 struct ata_ioports *ioaddr = &ap->ioaddr;
1629 u8 nsect, lbal;
1630
Tejun Heo5682ed32008-04-07 22:47:16 +09001631 ap->ops->sff_dev_select(ap, device);
Tejun Heo624d5c52008-03-25 22:16:41 +09001632
1633 iowrite8(0x55, ioaddr->nsect_addr);
1634 iowrite8(0xaa, ioaddr->lbal_addr);
1635
1636 iowrite8(0xaa, ioaddr->nsect_addr);
1637 iowrite8(0x55, ioaddr->lbal_addr);
1638
1639 iowrite8(0x55, ioaddr->nsect_addr);
1640 iowrite8(0xaa, ioaddr->lbal_addr);
1641
1642 nsect = ioread8(ioaddr->nsect_addr);
1643 lbal = ioread8(ioaddr->lbal_addr);
1644
1645 if ((nsect == 0x55) && (lbal == 0xaa))
1646 return 1; /* we found a device */
1647
1648 return 0; /* nothing found */
1649}
1650
1651/**
Tejun Heo9363c382008-04-07 22:47:16 +09001652 * ata_sff_dev_classify - Parse returned ATA device signature
Tejun Heo624d5c52008-03-25 22:16:41 +09001653 * @dev: ATA device to classify (starting at zero)
1654 * @present: device seems present
1655 * @r_err: Value of error register on completion
1656 *
1657 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
1658 * an ATA/ATAPI-defined set of values is placed in the ATA
1659 * shadow registers, indicating the results of device detection
1660 * and diagnostics.
1661 *
1662 * Select the ATA device, and read the values from the ATA shadow
1663 * registers. Then parse according to the Error register value,
1664 * and the spec-defined values examined by ata_dev_classify().
1665 *
1666 * LOCKING:
1667 * caller.
1668 *
1669 * RETURNS:
1670 * Device type - %ATA_DEV_ATA, %ATA_DEV_ATAPI or %ATA_DEV_NONE.
1671 */
Tejun Heo9363c382008-04-07 22:47:16 +09001672unsigned int ata_sff_dev_classify(struct ata_device *dev, int present,
Tejun Heo624d5c52008-03-25 22:16:41 +09001673 u8 *r_err)
1674{
1675 struct ata_port *ap = dev->link->ap;
1676 struct ata_taskfile tf;
1677 unsigned int class;
1678 u8 err;
1679
Tejun Heo5682ed32008-04-07 22:47:16 +09001680 ap->ops->sff_dev_select(ap, dev->devno);
Tejun Heo624d5c52008-03-25 22:16:41 +09001681
1682 memset(&tf, 0, sizeof(tf));
1683
Tejun Heo5682ed32008-04-07 22:47:16 +09001684 ap->ops->sff_tf_read(ap, &tf);
Tejun Heo624d5c52008-03-25 22:16:41 +09001685 err = tf.feature;
1686 if (r_err)
1687 *r_err = err;
1688
1689 /* see if device passed diags: continue and warn later */
1690 if (err == 0)
1691 /* diagnostic fail : do nothing _YET_ */
1692 dev->horkage |= ATA_HORKAGE_DIAGNOSTIC;
1693 else if (err == 1)
1694 /* do nothing */ ;
1695 else if ((dev->devno == 0) && (err == 0x81))
1696 /* do nothing */ ;
1697 else
1698 return ATA_DEV_NONE;
1699
1700 /* determine if device is ATA or ATAPI */
1701 class = ata_dev_classify(&tf);
1702
1703 if (class == ATA_DEV_UNKNOWN) {
1704 /* If the device failed diagnostic, it's likely to
1705 * have reported incorrect device signature too.
1706 * Assume ATA device if the device seems present but
1707 * device signature is invalid with diagnostic
1708 * failure.
1709 */
1710 if (present && (dev->horkage & ATA_HORKAGE_DIAGNOSTIC))
1711 class = ATA_DEV_ATA;
1712 else
1713 class = ATA_DEV_NONE;
Tejun Heo5682ed32008-04-07 22:47:16 +09001714 } else if ((class == ATA_DEV_ATA) &&
1715 (ap->ops->sff_check_status(ap) == 0))
Tejun Heo624d5c52008-03-25 22:16:41 +09001716 class = ATA_DEV_NONE;
1717
1718 return class;
1719}
1720
1721static int ata_bus_post_reset(struct ata_port *ap, unsigned int devmask,
1722 unsigned long deadline)
1723{
1724 struct ata_ioports *ioaddr = &ap->ioaddr;
1725 unsigned int dev0 = devmask & (1 << 0);
1726 unsigned int dev1 = devmask & (1 << 1);
1727 int rc, ret = 0;
1728
1729 /* if device 0 was found in ata_devchk, wait for its
1730 * BSY bit to clear
1731 */
1732 if (dev0) {
Tejun Heo9363c382008-04-07 22:47:16 +09001733 rc = ata_sff_wait_ready(ap, deadline);
Tejun Heo624d5c52008-03-25 22:16:41 +09001734 if (rc) {
1735 if (rc != -ENODEV)
1736 return rc;
1737 ret = rc;
1738 }
1739 }
1740
1741 /* if device 1 was found in ata_devchk, wait for register
1742 * access briefly, then wait for BSY to clear.
1743 */
1744 if (dev1) {
1745 int i;
1746
Tejun Heo5682ed32008-04-07 22:47:16 +09001747 ap->ops->sff_dev_select(ap, 1);
Tejun Heo624d5c52008-03-25 22:16:41 +09001748
1749 /* Wait for register access. Some ATAPI devices fail
1750 * to set nsect/lbal after reset, so don't waste too
1751 * much time on it. We're gonna wait for !BSY anyway.
1752 */
1753 for (i = 0; i < 2; i++) {
1754 u8 nsect, lbal;
1755
1756 nsect = ioread8(ioaddr->nsect_addr);
1757 lbal = ioread8(ioaddr->lbal_addr);
1758 if ((nsect == 1) && (lbal == 1))
1759 break;
1760 msleep(50); /* give drive a breather */
1761 }
1762
Tejun Heo9363c382008-04-07 22:47:16 +09001763 rc = ata_sff_wait_ready(ap, deadline);
Tejun Heo624d5c52008-03-25 22:16:41 +09001764 if (rc) {
1765 if (rc != -ENODEV)
1766 return rc;
1767 ret = rc;
1768 }
1769 }
1770
1771 /* is all this really necessary? */
Tejun Heo5682ed32008-04-07 22:47:16 +09001772 ap->ops->sff_dev_select(ap, 0);
Tejun Heo624d5c52008-03-25 22:16:41 +09001773 if (dev1)
Tejun Heo5682ed32008-04-07 22:47:16 +09001774 ap->ops->sff_dev_select(ap, 1);
Tejun Heo624d5c52008-03-25 22:16:41 +09001775 if (dev0)
Tejun Heo5682ed32008-04-07 22:47:16 +09001776 ap->ops->sff_dev_select(ap, 0);
Tejun Heo624d5c52008-03-25 22:16:41 +09001777
1778 return ret;
1779}
1780
1781/**
Tejun Heo9363c382008-04-07 22:47:16 +09001782 * ata_sff_wait_after_reset - wait before checking status after reset
Tejun Heo624d5c52008-03-25 22:16:41 +09001783 * @ap: port containing status register to be polled
1784 * @deadline: deadline jiffies for the operation
1785 *
1786 * After reset, we need to pause a while before reading status.
1787 * Also, certain combination of controller and device report 0xff
1788 * for some duration (e.g. until SATA PHY is up and running)
1789 * which is interpreted as empty port in ATA world. This
1790 * function also waits for such devices to get out of 0xff
1791 * status.
1792 *
1793 * LOCKING:
1794 * Kernel thread context (may sleep).
1795 */
Tejun Heo9363c382008-04-07 22:47:16 +09001796void ata_sff_wait_after_reset(struct ata_port *ap, unsigned long deadline)
Tejun Heo624d5c52008-03-25 22:16:41 +09001797{
1798 unsigned long until = jiffies + ATA_TMOUT_FF_WAIT;
1799
1800 if (time_before(until, deadline))
1801 deadline = until;
1802
1803 /* Spec mandates ">= 2ms" before checking status. We wait
1804 * 150ms, because that was the magic delay used for ATAPI
1805 * devices in Hale Landis's ATADRVR, for the period of time
1806 * between when the ATA command register is written, and then
1807 * status is checked. Because waiting for "a while" before
1808 * checking status is fine, post SRST, we perform this magic
1809 * delay here as well.
1810 *
1811 * Old drivers/ide uses the 2mS rule and then waits for ready.
1812 */
1813 msleep(150);
1814
1815 /* Wait for 0xff to clear. Some SATA devices take a long time
1816 * to clear 0xff after reset. For example, HHD424020F7SV00
1817 * iVDR needs >= 800ms while. Quantum GoVault needs even more
1818 * than that.
1819 *
1820 * Note that some PATA controllers (pata_ali) explode if
1821 * status register is read more than once when there's no
1822 * device attached.
1823 */
1824 if (ap->flags & ATA_FLAG_SATA) {
1825 while (1) {
Tejun Heo5682ed32008-04-07 22:47:16 +09001826 u8 status = ap->ops->sff_check_status(ap);
Tejun Heo624d5c52008-03-25 22:16:41 +09001827
1828 if (status != 0xff || time_after(jiffies, deadline))
1829 return;
1830
1831 msleep(50);
1832 }
1833 }
1834}
1835
1836static int ata_bus_softreset(struct ata_port *ap, unsigned int devmask,
1837 unsigned long deadline)
1838{
1839 struct ata_ioports *ioaddr = &ap->ioaddr;
1840
1841 DPRINTK("ata%u: bus reset via SRST\n", ap->print_id);
1842
1843 /* software reset. causes dev0 to be selected */
1844 iowrite8(ap->ctl, ioaddr->ctl_addr);
1845 udelay(20); /* FIXME: flush */
1846 iowrite8(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
1847 udelay(20); /* FIXME: flush */
1848 iowrite8(ap->ctl, ioaddr->ctl_addr);
1849
1850 /* wait a while before checking status */
Tejun Heo9363c382008-04-07 22:47:16 +09001851 ata_sff_wait_after_reset(ap, deadline);
Tejun Heo624d5c52008-03-25 22:16:41 +09001852
1853 /* Before we perform post reset processing we want to see if
1854 * the bus shows 0xFF because the odd clown forgets the D7
1855 * pulldown resistor.
1856 */
Tejun Heo5682ed32008-04-07 22:47:16 +09001857 if (ap->ops->sff_check_status(ap) == 0xFF)
Tejun Heo624d5c52008-03-25 22:16:41 +09001858 return -ENODEV;
1859
1860 return ata_bus_post_reset(ap, devmask, deadline);
1861}
1862
1863/**
Tejun Heo9363c382008-04-07 22:47:16 +09001864 * ata_sff_softreset - reset host port via ATA SRST
Tejun Heo624d5c52008-03-25 22:16:41 +09001865 * @link: ATA link to reset
1866 * @classes: resulting classes of attached devices
1867 * @deadline: deadline jiffies for the operation
1868 *
1869 * Reset host port using ATA SRST.
1870 *
1871 * LOCKING:
1872 * Kernel thread context (may sleep)
1873 *
1874 * RETURNS:
1875 * 0 on success, -errno otherwise.
1876 */
Tejun Heo9363c382008-04-07 22:47:16 +09001877int ata_sff_softreset(struct ata_link *link, unsigned int *classes,
Tejun Heo624d5c52008-03-25 22:16:41 +09001878 unsigned long deadline)
1879{
1880 struct ata_port *ap = link->ap;
1881 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
1882 unsigned int devmask = 0;
1883 int rc;
1884 u8 err;
1885
1886 DPRINTK("ENTER\n");
1887
1888 if (ata_link_offline(link)) {
1889 classes[0] = ATA_DEV_NONE;
1890 goto out;
1891 }
1892
1893 /* determine if device 0/1 are present */
1894 if (ata_devchk(ap, 0))
1895 devmask |= (1 << 0);
1896 if (slave_possible && ata_devchk(ap, 1))
1897 devmask |= (1 << 1);
1898
1899 /* select device 0 again */
Tejun Heo5682ed32008-04-07 22:47:16 +09001900 ap->ops->sff_dev_select(ap, 0);
Tejun Heo624d5c52008-03-25 22:16:41 +09001901
1902 /* issue bus reset */
1903 DPRINTK("about to softreset, devmask=%x\n", devmask);
1904 rc = ata_bus_softreset(ap, devmask, deadline);
1905 /* if link is occupied, -ENODEV too is an error */
1906 if (rc && (rc != -ENODEV || sata_scr_valid(link))) {
1907 ata_link_printk(link, KERN_ERR, "SRST failed (errno=%d)\n", rc);
1908 return rc;
1909 }
1910
1911 /* determine by signature whether we have ATA or ATAPI devices */
Tejun Heo9363c382008-04-07 22:47:16 +09001912 classes[0] = ata_sff_dev_classify(&link->device[0],
Tejun Heo624d5c52008-03-25 22:16:41 +09001913 devmask & (1 << 0), &err);
1914 if (slave_possible && err != 0x81)
Tejun Heo9363c382008-04-07 22:47:16 +09001915 classes[1] = ata_sff_dev_classify(&link->device[1],
Tejun Heo624d5c52008-03-25 22:16:41 +09001916 devmask & (1 << 1), &err);
1917
1918 out:
1919 DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]);
1920 return 0;
1921}
1922
1923/**
Tejun Heo9363c382008-04-07 22:47:16 +09001924 * sata_sff_hardreset - reset host port via SATA phy reset
Tejun Heo624d5c52008-03-25 22:16:41 +09001925 * @link: link to reset
1926 * @class: resulting class of attached device
1927 * @deadline: deadline jiffies for the operation
1928 *
1929 * SATA phy-reset host port using DET bits of SControl register,
1930 * wait for !BSY and classify the attached device.
1931 *
1932 * LOCKING:
1933 * Kernel thread context (may sleep)
1934 *
1935 * RETURNS:
1936 * 0 on success, -errno otherwise.
1937 */
Tejun Heo9363c382008-04-07 22:47:16 +09001938int sata_sff_hardreset(struct ata_link *link, unsigned int *class,
Tejun Heo624d5c52008-03-25 22:16:41 +09001939 unsigned long deadline)
1940{
1941 struct ata_port *ap = link->ap;
1942 const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
1943 int rc;
1944
1945 DPRINTK("ENTER\n");
1946
1947 /* do hardreset */
1948 rc = sata_link_hardreset(link, timing, deadline);
1949 if (rc) {
1950 ata_link_printk(link, KERN_ERR,
1951 "COMRESET failed (errno=%d)\n", rc);
1952 return rc;
1953 }
1954
1955 /* TODO: phy layer with polling, timeouts, etc. */
1956 if (ata_link_offline(link)) {
1957 *class = ATA_DEV_NONE;
1958 DPRINTK("EXIT, link offline\n");
1959 return 0;
1960 }
1961
1962 /* wait a while before checking status */
Tejun Heo9363c382008-04-07 22:47:16 +09001963 ata_sff_wait_after_reset(ap, deadline);
Tejun Heo624d5c52008-03-25 22:16:41 +09001964
1965 /* If PMP is supported, we have to do follow-up SRST. Note
1966 * that some PMPs don't send D2H Reg FIS after hardreset at
1967 * all if the first port is empty. Wait for it just for a
1968 * second and request follow-up SRST.
1969 */
1970 if (ap->flags & ATA_FLAG_PMP) {
Tejun Heo9363c382008-04-07 22:47:16 +09001971 ata_sff_wait_ready(ap, jiffies + HZ);
Tejun Heo624d5c52008-03-25 22:16:41 +09001972 return -EAGAIN;
1973 }
1974
Tejun Heo9363c382008-04-07 22:47:16 +09001975 rc = ata_sff_wait_ready(ap, deadline);
Tejun Heo624d5c52008-03-25 22:16:41 +09001976 /* link occupied, -ENODEV too is an error */
1977 if (rc) {
1978 ata_link_printk(link, KERN_ERR,
1979 "COMRESET failed (errno=%d)\n", rc);
1980 return rc;
1981 }
1982
Tejun Heo5682ed32008-04-07 22:47:16 +09001983 ap->ops->sff_dev_select(ap, 0); /* probably unnecessary */
Tejun Heo624d5c52008-03-25 22:16:41 +09001984
Tejun Heo9363c382008-04-07 22:47:16 +09001985 *class = ata_sff_dev_classify(link->device, 1, NULL);
Tejun Heo624d5c52008-03-25 22:16:41 +09001986
1987 DPRINTK("EXIT, class=%u\n", *class);
1988 return 0;
1989}
1990
1991/**
Tejun Heo9363c382008-04-07 22:47:16 +09001992 * ata_sff_error_handler - Stock error handler for BMDMA controller
Tejun Heo6d97dbd2006-05-15 20:58:24 +09001993 * @ap: port to handle error for
Tejun Heo6d97dbd2006-05-15 20:58:24 +09001994 *
Tejun Heo9363c382008-04-07 22:47:16 +09001995 * Stock error handler for SFF controller. It can handle both
Tejun Heo6d97dbd2006-05-15 20:58:24 +09001996 * PATA and SATA controllers. Many controllers should be able to
1997 * use this EH as-is or with some added handling before and
1998 * after.
1999 *
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002000 * LOCKING:
2001 * Kernel thread context (may sleep)
2002 */
Tejun Heo9363c382008-04-07 22:47:16 +09002003void ata_sff_error_handler(struct ata_port *ap)
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002004{
Tejun Heoa1efdab2008-03-25 12:22:50 +09002005 ata_reset_fn_t softreset = ap->ops->softreset;
2006 ata_reset_fn_t hardreset = ap->ops->hardreset;
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002007 struct ata_queued_cmd *qc;
2008 unsigned long flags;
2009 int thaw = 0;
2010
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002011 qc = __ata_qc_from_tag(ap, ap->link.active_tag);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002012 if (qc && !(qc->flags & ATA_QCFLAG_FAILED))
2013 qc = NULL;
2014
2015 /* reset PIO HSM and stop DMA engine */
Jeff Garzikba6a1302006-06-22 23:46:10 -04002016 spin_lock_irqsave(ap->lock, flags);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002017
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002018 ap->hsm_task_state = HSM_ST_IDLE;
2019
Tejun Heoed82f962008-03-25 21:34:39 +09002020 if (ap->ioaddr.bmdma_addr &&
2021 qc && (qc->tf.protocol == ATA_PROT_DMA ||
Tejun Heo0dc36882007-12-18 16:34:43 -05002022 qc->tf.protocol == ATAPI_PROT_DMA)) {
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002023 u8 host_stat;
2024
Robert Hancockfbbb2622006-10-27 19:08:41 -07002025 host_stat = ap->ops->bmdma_status(ap);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002026
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002027 /* BMDMA controllers indicate host bus error by
2028 * setting DMA_ERR bit and timing out. As it wasn't
2029 * really a timeout event, adjust error mask and
2030 * cancel frozen state.
2031 */
Alan18d90de2007-01-24 11:42:38 +00002032 if (qc->err_mask == AC_ERR_TIMEOUT && (host_stat & ATA_DMA_ERR)) {
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002033 qc->err_mask = AC_ERR_HOST_BUS;
2034 thaw = 1;
2035 }
2036
2037 ap->ops->bmdma_stop(qc);
2038 }
2039
Tejun Heo9363c382008-04-07 22:47:16 +09002040 ata_sff_altstatus(ap);
Tejun Heo5682ed32008-04-07 22:47:16 +09002041 ap->ops->sff_check_status(ap);
2042 ap->ops->sff_irq_clear(ap);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002043
Jeff Garzikba6a1302006-06-22 23:46:10 -04002044 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002045
2046 if (thaw)
2047 ata_eh_thaw_port(ap);
2048
2049 /* PIO and DMA engines have been stopped, perform recovery */
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002050
Tejun Heo9363c382008-04-07 22:47:16 +09002051 /* ata_sff_softreset and sata_sff_hardreset are inherited to
Tejun Heoa1efdab2008-03-25 12:22:50 +09002052 * all SFF drivers from ata_sff_port_ops. Ignore softreset if
2053 * ctl isn't accessible. Ignore hardreset if SCR access isn't
2054 * available.
2055 */
Tejun Heo9363c382008-04-07 22:47:16 +09002056 if (softreset == ata_sff_softreset && !ap->ioaddr.ctl_addr)
Tejun Heoa1efdab2008-03-25 12:22:50 +09002057 softreset = NULL;
Tejun Heo9363c382008-04-07 22:47:16 +09002058 if (hardreset == sata_sff_hardreset && !sata_scr_valid(&ap->link))
Tejun Heoa1efdab2008-03-25 12:22:50 +09002059 hardreset = NULL;
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002060
Tejun Heoa1efdab2008-03-25 12:22:50 +09002061 ata_do_eh(ap, ap->ops->prereset, softreset, hardreset,
2062 ap->ops->postreset);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002063}
2064
2065/**
Tejun Heo9363c382008-04-07 22:47:16 +09002066 * ata_sff_post_internal_cmd - Stock post_internal_cmd for SFF controller
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002067 * @qc: internal command to clean up
2068 *
2069 * LOCKING:
2070 * Kernel thread context (may sleep)
2071 */
Tejun Heo9363c382008-04-07 22:47:16 +09002072void ata_sff_post_internal_cmd(struct ata_queued_cmd *qc)
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002073{
Alan61dd08c2007-01-25 15:09:05 +00002074 if (qc->ap->ioaddr.bmdma_addr)
2075 ata_bmdma_stop(qc);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09002076}
2077
Alan Coxd92e74d2007-06-07 16:19:15 +01002078/**
2079 * ata_sff_port_start - Set port up for dma.
2080 * @ap: Port to initialize
2081 *
2082 * Called just after data structures for each port are
2083 * initialized. Allocates space for PRD table if the device
2084 * is DMA capable SFF.
2085 *
2086 * May be used as the port_start() entry in ata_port_operations.
2087 *
2088 * LOCKING:
2089 * Inherited from caller.
2090 */
Alan Coxd92e74d2007-06-07 16:19:15 +01002091int ata_sff_port_start(struct ata_port *ap)
2092{
2093 if (ap->ioaddr.bmdma_addr)
2094 return ata_port_start(ap);
2095 return 0;
2096}
2097
Tejun Heo272f7882008-03-25 22:16:40 +09002098/**
Tejun Heo9363c382008-04-07 22:47:16 +09002099 * ata_sff_std_ports - initialize ioaddr with standard port offsets.
Tejun Heo624d5c52008-03-25 22:16:41 +09002100 * @ioaddr: IO address structure to be initialized
2101 *
2102 * Utility function which initializes data_addr, error_addr,
2103 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
2104 * device_addr, status_addr, and command_addr to standard offsets
2105 * relative to cmd_addr.
2106 *
2107 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
2108 */
Tejun Heo9363c382008-04-07 22:47:16 +09002109void ata_sff_std_ports(struct ata_ioports *ioaddr)
Tejun Heo624d5c52008-03-25 22:16:41 +09002110{
2111 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
2112 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
2113 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
2114 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
2115 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
2116 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
2117 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
2118 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
2119 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
2120 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
2121}
2122
Tejun Heo9363c382008-04-07 22:47:16 +09002123unsigned long ata_bmdma_mode_filter(struct ata_device *adev,
2124 unsigned long xfer_mask)
Tejun Heo071ce342008-03-25 22:16:42 +09002125{
2126 /* Filter out DMA modes if the device has been configured by
2127 the BIOS as PIO only */
2128
2129 if (adev->link->ap->ioaddr.bmdma_addr == NULL)
2130 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
2131 return xfer_mask;
2132}
2133
Tejun Heo624d5c52008-03-25 22:16:41 +09002134/**
Tejun Heo272f7882008-03-25 22:16:40 +09002135 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
2136 * @qc: Info associated with this ATA transaction.
2137 *
2138 * LOCKING:
2139 * spin_lock_irqsave(host lock)
2140 */
2141void ata_bmdma_setup(struct ata_queued_cmd *qc)
2142{
2143 struct ata_port *ap = qc->ap;
2144 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
2145 u8 dmactl;
2146
2147 /* load PRD table addr. */
2148 mb(); /* make sure PRD table writes are visible to controller */
2149 iowrite32(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
2150
2151 /* specify data direction, triple-check start bit is clear */
2152 dmactl = ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
2153 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
2154 if (!rw)
2155 dmactl |= ATA_DMA_WR;
2156 iowrite8(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
2157
2158 /* issue r/w command */
Tejun Heo5682ed32008-04-07 22:47:16 +09002159 ap->ops->sff_exec_command(ap, &qc->tf);
Tejun Heo272f7882008-03-25 22:16:40 +09002160}
2161
2162/**
2163 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
2164 * @qc: Info associated with this ATA transaction.
2165 *
2166 * LOCKING:
2167 * spin_lock_irqsave(host lock)
2168 */
2169void ata_bmdma_start(struct ata_queued_cmd *qc)
2170{
2171 struct ata_port *ap = qc->ap;
2172 u8 dmactl;
2173
2174 /* start host DMA transaction */
2175 dmactl = ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
2176 iowrite8(dmactl | ATA_DMA_START, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
2177
2178 /* Strictly, one may wish to issue an ioread8() here, to
2179 * flush the mmio write. However, control also passes
2180 * to the hardware at this point, and it will interrupt
2181 * us when we are to resume control. So, in effect,
2182 * we don't care when the mmio write flushes.
2183 * Further, a read of the DMA status register _immediately_
2184 * following the write may not be what certain flaky hardware
2185 * is expected, so I think it is best to not add a readb()
2186 * without first all the MMIO ATA cards/mobos.
2187 * Or maybe I'm just being paranoid.
2188 *
2189 * FIXME: The posting of this write means I/O starts are
2190 * unneccessarily delayed for MMIO
2191 */
2192}
2193
2194/**
2195 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
2196 * @qc: Command we are ending DMA for
2197 *
2198 * Clears the ATA_DMA_START flag in the dma control register
2199 *
2200 * May be used as the bmdma_stop() entry in ata_port_operations.
2201 *
2202 * LOCKING:
2203 * spin_lock_irqsave(host lock)
2204 */
2205void ata_bmdma_stop(struct ata_queued_cmd *qc)
2206{
2207 struct ata_port *ap = qc->ap;
2208 void __iomem *mmio = ap->ioaddr.bmdma_addr;
2209
2210 /* clear start/stop bit */
2211 iowrite8(ioread8(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
2212 mmio + ATA_DMA_CMD);
2213
2214 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
Tejun Heo9363c382008-04-07 22:47:16 +09002215 ata_sff_altstatus(ap); /* dummy read */
Tejun Heo272f7882008-03-25 22:16:40 +09002216}
2217
2218/**
2219 * ata_bmdma_status - Read PCI IDE BMDMA status
2220 * @ap: Port associated with this ATA transaction.
2221 *
2222 * Read and return BMDMA status register.
2223 *
2224 * May be used as the bmdma_status() entry in ata_port_operations.
2225 *
2226 * LOCKING:
2227 * spin_lock_irqsave(host lock)
2228 */
2229u8 ata_bmdma_status(struct ata_port *ap)
2230{
2231 return ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
2232}
2233
2234/**
Tejun Heo624d5c52008-03-25 22:16:41 +09002235 * ata_bus_reset - reset host port and associated ATA channel
2236 * @ap: port to reset
2237 *
2238 * This is typically the first time we actually start issuing
2239 * commands to the ATA channel. We wait for BSY to clear, then
2240 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
2241 * result. Determine what devices, if any, are on the channel
2242 * by looking at the device 0/1 error register. Look at the signature
2243 * stored in each device's taskfile registers, to determine if
2244 * the device is ATA or ATAPI.
2245 *
2246 * LOCKING:
2247 * PCI/etc. bus probe sem.
2248 * Obtains host lock.
2249 *
2250 * SIDE EFFECTS:
2251 * Sets ATA_FLAG_DISABLED if bus reset fails.
2252 *
2253 * DEPRECATED:
2254 * This function is only for drivers which still use old EH and
2255 * will be removed soon.
Tejun Heo272f7882008-03-25 22:16:40 +09002256 */
Tejun Heo624d5c52008-03-25 22:16:41 +09002257void ata_bus_reset(struct ata_port *ap)
Tejun Heo272f7882008-03-25 22:16:40 +09002258{
Tejun Heo624d5c52008-03-25 22:16:41 +09002259 struct ata_device *device = ap->link.device;
2260 struct ata_ioports *ioaddr = &ap->ioaddr;
2261 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2262 u8 err;
2263 unsigned int dev0, dev1 = 0, devmask = 0;
2264 int rc;
2265
2266 DPRINTK("ENTER, host %u, port %u\n", ap->print_id, ap->port_no);
2267
2268 /* determine if device 0/1 are present */
2269 if (ap->flags & ATA_FLAG_SATA_RESET)
2270 dev0 = 1;
2271 else {
2272 dev0 = ata_devchk(ap, 0);
2273 if (slave_possible)
2274 dev1 = ata_devchk(ap, 1);
2275 }
2276
2277 if (dev0)
2278 devmask |= (1 << 0);
2279 if (dev1)
2280 devmask |= (1 << 1);
2281
2282 /* select device 0 again */
Tejun Heo5682ed32008-04-07 22:47:16 +09002283 ap->ops->sff_dev_select(ap, 0);
Tejun Heo624d5c52008-03-25 22:16:41 +09002284
2285 /* issue bus reset */
2286 if (ap->flags & ATA_FLAG_SRST) {
2287 rc = ata_bus_softreset(ap, devmask, jiffies + 40 * HZ);
2288 if (rc && rc != -ENODEV)
2289 goto err_out;
2290 }
2291
2292 /*
2293 * determine by signature whether we have ATA or ATAPI devices
2294 */
Tejun Heo9363c382008-04-07 22:47:16 +09002295 device[0].class = ata_sff_dev_classify(&device[0], dev0, &err);
Tejun Heo624d5c52008-03-25 22:16:41 +09002296 if ((slave_possible) && (err != 0x81))
Tejun Heo9363c382008-04-07 22:47:16 +09002297 device[1].class = ata_sff_dev_classify(&device[1], dev1, &err);
Tejun Heo624d5c52008-03-25 22:16:41 +09002298
2299 /* is double-select really necessary? */
2300 if (device[1].class != ATA_DEV_NONE)
Tejun Heo5682ed32008-04-07 22:47:16 +09002301 ap->ops->sff_dev_select(ap, 1);
Tejun Heo624d5c52008-03-25 22:16:41 +09002302 if (device[0].class != ATA_DEV_NONE)
Tejun Heo5682ed32008-04-07 22:47:16 +09002303 ap->ops->sff_dev_select(ap, 0);
Tejun Heo624d5c52008-03-25 22:16:41 +09002304
2305 /* if no devices were detected, disable this port */
2306 if ((device[0].class == ATA_DEV_NONE) &&
2307 (device[1].class == ATA_DEV_NONE))
2308 goto err_out;
2309
2310 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
2311 /* set up device control for ATA_FLAG_SATA_RESET */
2312 iowrite8(ap->ctl, ioaddr->ctl_addr);
2313 }
2314
2315 DPRINTK("EXIT\n");
2316 return;
2317
2318err_out:
2319 ata_port_printk(ap, KERN_ERR, "disabling port\n");
2320 ata_port_disable(ap);
2321
2322 DPRINTK("EXIT\n");
Tejun Heo272f7882008-03-25 22:16:40 +09002323}
2324
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002325#ifdef CONFIG_PCI
Alan4112e162007-01-08 12:10:05 +00002326
Tejun Heo272f7882008-03-25 22:16:40 +09002327/**
Tejun Heo9363c382008-04-07 22:47:16 +09002328 * ata_pci_bmdma_clear_simplex - attempt to kick device out of simplex
Tejun Heo272f7882008-03-25 22:16:40 +09002329 * @pdev: PCI device
2330 *
2331 * Some PCI ATA devices report simplex mode but in fact can be told to
2332 * enter non simplex mode. This implements the necessary logic to
2333 * perform the task on such devices. Calling it on other devices will
2334 * have -undefined- behaviour.
2335 */
Tejun Heo9363c382008-04-07 22:47:16 +09002336int ata_pci_bmdma_clear_simplex(struct pci_dev *pdev)
Alan4112e162007-01-08 12:10:05 +00002337{
Tejun Heo272f7882008-03-25 22:16:40 +09002338 unsigned long bmdma = pci_resource_start(pdev, 4);
2339 u8 simplex;
Jeff Garzika84471f2007-02-26 05:51:33 -05002340
Tejun Heo272f7882008-03-25 22:16:40 +09002341 if (bmdma == 0)
2342 return -ENOENT;
2343
2344 simplex = inb(bmdma + 0x02);
2345 outb(simplex & 0x60, bmdma + 0x02);
2346 simplex = inb(bmdma + 0x02);
2347 if (simplex & 0x80)
2348 return -EOPNOTSUPP;
2349 return 0;
2350}
2351
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002352/**
Tejun Heo9363c382008-04-07 22:47:16 +09002353 * ata_pci_bmdma_init - acquire PCI BMDMA resources and init ATA host
Tejun Heo0f834de2007-04-17 23:44:07 +09002354 * @host: target ATA host
2355 *
2356 * Acquire PCI BMDMA resources and initialize @host accordingly.
2357 *
2358 * LOCKING:
2359 * Inherited from calling layer (may sleep).
2360 *
2361 * RETURNS:
2362 * 0 on success, -errno otherwise.
2363 */
Tejun Heo9363c382008-04-07 22:47:16 +09002364int ata_pci_bmdma_init(struct ata_host *host)
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002365{
Tejun Heo0f834de2007-04-17 23:44:07 +09002366 struct device *gdev = host->dev;
2367 struct pci_dev *pdev = to_pci_dev(gdev);
2368 int i, rc;
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002369
Alan Cox6fdc99a2007-07-26 18:41:30 +01002370 /* No BAR4 allocation: No DMA */
2371 if (pci_resource_start(pdev, 4) == 0)
2372 return 0;
2373
Tejun Heo0f834de2007-04-17 23:44:07 +09002374 /* TODO: If we get no DMA mask we should fall back to PIO */
2375 rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
2376 if (rc)
2377 return rc;
2378 rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
2379 if (rc)
2380 return rc;
2381
2382 /* request and iomap DMA region */
Tejun Heo35a10a82008-01-04 18:42:21 +09002383 rc = pcim_iomap_regions(pdev, 1 << 4, dev_driver_string(gdev));
Tejun Heo0f834de2007-04-17 23:44:07 +09002384 if (rc) {
2385 dev_printk(KERN_ERR, gdev, "failed to request/iomap BAR4\n");
2386 return -ENOMEM;
2387 }
2388 host->iomap = pcim_iomap_table(pdev);
2389
Tejun Heo1626aeb2007-05-04 12:43:58 +02002390 for (i = 0; i < 2; i++) {
Tejun Heo0f834de2007-04-17 23:44:07 +09002391 struct ata_port *ap = host->ports[i];
Tejun Heo0f834de2007-04-17 23:44:07 +09002392 void __iomem *bmdma = host->iomap[4] + 8 * i;
2393
2394 if (ata_port_is_dummy(ap))
2395 continue;
2396
Tejun Heo21b0ad42007-04-17 23:44:07 +09002397 ap->ioaddr.bmdma_addr = bmdma;
Tejun Heo0f834de2007-04-17 23:44:07 +09002398 if ((!(ap->flags & ATA_FLAG_IGN_SIMPLEX)) &&
2399 (ioread8(bmdma + 2) & 0x80))
2400 host->flags |= ATA_HOST_SIMPLEX;
Tejun Heocbcdd872007-08-18 13:14:55 +09002401
2402 ata_port_desc(ap, "bmdma 0x%llx",
2403 (unsigned long long)pci_resource_start(pdev, 4) + 8 * i);
Tejun Heo0d5ff562007-02-01 15:06:36 +09002404 }
2405
Tejun Heo0f834de2007-04-17 23:44:07 +09002406 return 0;
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002407}
2408
Tejun Heo272f7882008-03-25 22:16:40 +09002409static int ata_resources_present(struct pci_dev *pdev, int port)
2410{
2411 int i;
2412
2413 /* Check the PCI resources for this channel are enabled */
2414 port = port * 2;
2415 for (i = 0; i < 2; i ++) {
2416 if (pci_resource_start(pdev, port + i) == 0 ||
2417 pci_resource_len(pdev, port + i) == 0)
2418 return 0;
2419 }
2420 return 1;
2421}
2422
Tejun Heod491b272007-04-17 23:44:07 +09002423/**
Tejun Heo9363c382008-04-07 22:47:16 +09002424 * ata_pci_sff_init_host - acquire native PCI ATA resources and init host
Tejun Heod491b272007-04-17 23:44:07 +09002425 * @host: target ATA host
Tejun Heod491b272007-04-17 23:44:07 +09002426 *
Tejun Heo1626aeb2007-05-04 12:43:58 +02002427 * Acquire native PCI ATA resources for @host and initialize the
2428 * first two ports of @host accordingly. Ports marked dummy are
2429 * skipped and allocation failure makes the port dummy.
Tejun Heod491b272007-04-17 23:44:07 +09002430 *
Tejun Heod583bc12007-07-04 18:02:07 +09002431 * Note that native PCI resources are valid even for legacy hosts
2432 * as we fix up pdev resources array early in boot, so this
2433 * function can be used for both native and legacy SFF hosts.
2434 *
Tejun Heod491b272007-04-17 23:44:07 +09002435 * LOCKING:
2436 * Inherited from calling layer (may sleep).
2437 *
2438 * RETURNS:
Tejun Heo1626aeb2007-05-04 12:43:58 +02002439 * 0 if at least one port is initialized, -ENODEV if no port is
2440 * available.
Tejun Heod491b272007-04-17 23:44:07 +09002441 */
Tejun Heo9363c382008-04-07 22:47:16 +09002442int ata_pci_sff_init_host(struct ata_host *host)
Tejun Heod491b272007-04-17 23:44:07 +09002443{
2444 struct device *gdev = host->dev;
2445 struct pci_dev *pdev = to_pci_dev(gdev);
Tejun Heo1626aeb2007-05-04 12:43:58 +02002446 unsigned int mask = 0;
Tejun Heod491b272007-04-17 23:44:07 +09002447 int i, rc;
2448
Tejun Heod491b272007-04-17 23:44:07 +09002449 /* request, iomap BARs and init port addresses accordingly */
2450 for (i = 0; i < 2; i++) {
2451 struct ata_port *ap = host->ports[i];
2452 int base = i * 2;
2453 void __iomem * const *iomap;
2454
Tejun Heo1626aeb2007-05-04 12:43:58 +02002455 if (ata_port_is_dummy(ap))
Tejun Heod491b272007-04-17 23:44:07 +09002456 continue;
2457
Tejun Heo1626aeb2007-05-04 12:43:58 +02002458 /* Discard disabled ports. Some controllers show
2459 * their unused channels this way. Disabled ports are
2460 * made dummy.
2461 */
2462 if (!ata_resources_present(pdev, i)) {
2463 ap->ops = &ata_dummy_port_ops;
2464 continue;
2465 }
2466
Tejun Heo35a10a82008-01-04 18:42:21 +09002467 rc = pcim_iomap_regions(pdev, 0x3 << base,
2468 dev_driver_string(gdev));
Tejun Heod491b272007-04-17 23:44:07 +09002469 if (rc) {
Tejun Heo1626aeb2007-05-04 12:43:58 +02002470 dev_printk(KERN_WARNING, gdev,
2471 "failed to request/iomap BARs for port %d "
2472 "(errno=%d)\n", i, rc);
Tejun Heod491b272007-04-17 23:44:07 +09002473 if (rc == -EBUSY)
2474 pcim_pin_device(pdev);
Tejun Heo1626aeb2007-05-04 12:43:58 +02002475 ap->ops = &ata_dummy_port_ops;
2476 continue;
Tejun Heod491b272007-04-17 23:44:07 +09002477 }
2478 host->iomap = iomap = pcim_iomap_table(pdev);
2479
2480 ap->ioaddr.cmd_addr = iomap[base];
2481 ap->ioaddr.altstatus_addr =
2482 ap->ioaddr.ctl_addr = (void __iomem *)
2483 ((unsigned long)iomap[base + 1] | ATA_PCI_CTL_OFS);
Tejun Heo9363c382008-04-07 22:47:16 +09002484 ata_sff_std_ports(&ap->ioaddr);
Tejun Heo1626aeb2007-05-04 12:43:58 +02002485
Tejun Heocbcdd872007-08-18 13:14:55 +09002486 ata_port_desc(ap, "cmd 0x%llx ctl 0x%llx",
2487 (unsigned long long)pci_resource_start(pdev, base),
2488 (unsigned long long)pci_resource_start(pdev, base + 1));
2489
Tejun Heo1626aeb2007-05-04 12:43:58 +02002490 mask |= 1 << i;
2491 }
2492
2493 if (!mask) {
2494 dev_printk(KERN_ERR, gdev, "no available native port\n");
2495 return -ENODEV;
Tejun Heod491b272007-04-17 23:44:07 +09002496 }
2497
2498 return 0;
2499}
2500
Tejun Heo21b0ad42007-04-17 23:44:07 +09002501/**
Tejun Heo9363c382008-04-07 22:47:16 +09002502 * ata_pci_sff_prepare_host - helper to prepare native PCI ATA host
Tejun Heo21b0ad42007-04-17 23:44:07 +09002503 * @pdev: target PCI device
Tejun Heo1626aeb2007-05-04 12:43:58 +02002504 * @ppi: array of port_info, must be enough for two ports
Tejun Heo21b0ad42007-04-17 23:44:07 +09002505 * @r_host: out argument for the initialized ATA host
2506 *
2507 * Helper to allocate ATA host for @pdev, acquire all native PCI
2508 * resources and initialize it accordingly in one go.
2509 *
2510 * LOCKING:
2511 * Inherited from calling layer (may sleep).
2512 *
2513 * RETURNS:
2514 * 0 on success, -errno otherwise.
2515 */
Tejun Heo9363c382008-04-07 22:47:16 +09002516int ata_pci_sff_prepare_host(struct pci_dev *pdev,
Tejun Heod583bc12007-07-04 18:02:07 +09002517 const struct ata_port_info * const * ppi,
2518 struct ata_host **r_host)
Tejun Heo21b0ad42007-04-17 23:44:07 +09002519{
2520 struct ata_host *host;
Tejun Heo21b0ad42007-04-17 23:44:07 +09002521 int rc;
2522
2523 if (!devres_open_group(&pdev->dev, NULL, GFP_KERNEL))
2524 return -ENOMEM;
2525
2526 host = ata_host_alloc_pinfo(&pdev->dev, ppi, 2);
2527 if (!host) {
2528 dev_printk(KERN_ERR, &pdev->dev,
2529 "failed to allocate ATA host\n");
2530 rc = -ENOMEM;
2531 goto err_out;
2532 }
2533
Tejun Heo9363c382008-04-07 22:47:16 +09002534 rc = ata_pci_sff_init_host(host);
Tejun Heo21b0ad42007-04-17 23:44:07 +09002535 if (rc)
2536 goto err_out;
2537
2538 /* init DMA related stuff */
Tejun Heo9363c382008-04-07 22:47:16 +09002539 rc = ata_pci_bmdma_init(host);
Tejun Heo21b0ad42007-04-17 23:44:07 +09002540 if (rc)
2541 goto err_bmdma;
2542
2543 devres_remove_group(&pdev->dev, NULL);
2544 *r_host = host;
2545 return 0;
2546
2547 err_bmdma:
2548 /* This is necessary because PCI and iomap resources are
2549 * merged and releasing the top group won't release the
2550 * acquired resources if some of those have been acquired
2551 * before entering this function.
2552 */
2553 pcim_iounmap_regions(pdev, 0xf);
2554 err_out:
2555 devres_release_group(&pdev->dev, NULL);
2556 return rc;
2557}
2558
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002559/**
Tejun Heo9363c382008-04-07 22:47:16 +09002560 * ata_pci_sff_activate_host - start SFF host, request IRQ and register it
Tejun Heo4e6b79f2008-01-18 18:36:28 +09002561 * @host: target SFF ATA host
2562 * @irq_handler: irq_handler used when requesting IRQ(s)
2563 * @sht: scsi_host_template to use when registering the host
2564 *
2565 * This is the counterpart of ata_host_activate() for SFF ATA
2566 * hosts. This separate helper is necessary because SFF hosts
2567 * use two separate interrupts in legacy mode.
2568 *
2569 * LOCKING:
2570 * Inherited from calling layer (may sleep).
2571 *
2572 * RETURNS:
2573 * 0 on success, -errno otherwise.
2574 */
Tejun Heo9363c382008-04-07 22:47:16 +09002575int ata_pci_sff_activate_host(struct ata_host *host,
Tejun Heo4e6b79f2008-01-18 18:36:28 +09002576 irq_handler_t irq_handler,
2577 struct scsi_host_template *sht)
2578{
2579 struct device *dev = host->dev;
2580 struct pci_dev *pdev = to_pci_dev(dev);
2581 const char *drv_name = dev_driver_string(host->dev);
2582 int legacy_mode = 0, rc;
2583
2584 rc = ata_host_start(host);
2585 if (rc)
2586 return rc;
2587
2588 if ((pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
2589 u8 tmp8, mask;
2590
2591 /* TODO: What if one channel is in native mode ... */
2592 pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
2593 mask = (1 << 2) | (1 << 0);
2594 if ((tmp8 & mask) != mask)
2595 legacy_mode = 1;
2596#if defined(CONFIG_NO_ATA_LEGACY)
2597 /* Some platforms with PCI limits cannot address compat
2598 port space. In that case we punt if their firmware has
2599 left a device in compatibility mode */
2600 if (legacy_mode) {
2601 printk(KERN_ERR "ata: Compatibility mode ATA is not supported on this platform, skipping.\n");
2602 return -EOPNOTSUPP;
2603 }
2604#endif
2605 }
2606
2607 if (!devres_open_group(dev, NULL, GFP_KERNEL))
2608 return -ENOMEM;
2609
2610 if (!legacy_mode && pdev->irq) {
2611 rc = devm_request_irq(dev, pdev->irq, irq_handler,
2612 IRQF_SHARED, drv_name, host);
2613 if (rc)
2614 goto out;
2615
2616 ata_port_desc(host->ports[0], "irq %d", pdev->irq);
2617 ata_port_desc(host->ports[1], "irq %d", pdev->irq);
2618 } else if (legacy_mode) {
2619 if (!ata_port_is_dummy(host->ports[0])) {
2620 rc = devm_request_irq(dev, ATA_PRIMARY_IRQ(pdev),
2621 irq_handler, IRQF_SHARED,
2622 drv_name, host);
2623 if (rc)
2624 goto out;
2625
2626 ata_port_desc(host->ports[0], "irq %d",
2627 ATA_PRIMARY_IRQ(pdev));
2628 }
2629
2630 if (!ata_port_is_dummy(host->ports[1])) {
2631 rc = devm_request_irq(dev, ATA_SECONDARY_IRQ(pdev),
2632 irq_handler, IRQF_SHARED,
2633 drv_name, host);
2634 if (rc)
2635 goto out;
2636
2637 ata_port_desc(host->ports[1], "irq %d",
2638 ATA_SECONDARY_IRQ(pdev));
2639 }
2640 }
2641
2642 rc = ata_host_register(host, sht);
2643 out:
2644 if (rc == 0)
2645 devres_remove_group(dev, NULL);
2646 else
2647 devres_release_group(dev, NULL);
2648
2649 return rc;
2650}
2651
2652/**
Tejun Heo9363c382008-04-07 22:47:16 +09002653 * ata_pci_sff_init_one - Initialize/register PCI IDE host controller
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002654 * @pdev: Controller to be initialized
Tejun Heo1626aeb2007-05-04 12:43:58 +02002655 * @ppi: array of port_info, must be enough for two ports
Tejun Heo1bd5b712008-03-25 12:22:49 +09002656 * @sht: scsi_host_template to use when registering the host
Tejun Heo887125e2008-03-25 12:22:49 +09002657 * @host_priv: host private_data
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002658 *
2659 * This is a helper function which can be called from a driver's
2660 * xxx_init_one() probe function if the hardware uses traditional
2661 * IDE taskfile registers.
2662 *
2663 * This function calls pci_enable_device(), reserves its register
2664 * regions, sets the dma mask, enables bus master mode, and calls
2665 * ata_device_add()
2666 *
Alan Cox2ec7df02006-08-10 16:59:10 +09002667 * ASSUMPTION:
2668 * Nobody makes a single channel controller that appears solely as
2669 * the secondary legacy port on PCI.
2670 *
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002671 * LOCKING:
2672 * Inherited from PCI layer (may sleep).
2673 *
2674 * RETURNS:
2675 * Zero on success, negative on errno-based value on error.
2676 */
Tejun Heo9363c382008-04-07 22:47:16 +09002677int ata_pci_sff_init_one(struct pci_dev *pdev,
2678 const struct ata_port_info * const * ppi,
2679 struct scsi_host_template *sht, void *host_priv)
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002680{
Tejun Heof0d36ef2007-01-20 16:00:28 +09002681 struct device *dev = &pdev->dev;
Tejun Heo1626aeb2007-05-04 12:43:58 +02002682 const struct ata_port_info *pi = NULL;
Tejun Heo0f834de2007-04-17 23:44:07 +09002683 struct ata_host *host = NULL;
Tejun Heo1626aeb2007-05-04 12:43:58 +02002684 int i, rc;
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002685
2686 DPRINTK("ENTER\n");
2687
Tejun Heo1626aeb2007-05-04 12:43:58 +02002688 /* look up the first valid port_info */
2689 for (i = 0; i < 2 && ppi[i]; i++) {
2690 if (ppi[i]->port_ops != &ata_dummy_port_ops) {
2691 pi = ppi[i];
2692 break;
2693 }
2694 }
2695
2696 if (!pi) {
2697 dev_printk(KERN_ERR, &pdev->dev,
2698 "no valid port_info specified\n");
2699 return -EINVAL;
2700 }
2701
Tejun Heof0d36ef2007-01-20 16:00:28 +09002702 if (!devres_open_group(dev, NULL, GFP_KERNEL))
2703 return -ENOMEM;
2704
Tejun Heof0d36ef2007-01-20 16:00:28 +09002705 rc = pcim_enable_device(pdev);
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002706 if (rc)
Tejun Heo4e6b79f2008-01-18 18:36:28 +09002707 goto out;
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002708
Tejun Heo4e6b79f2008-01-18 18:36:28 +09002709 /* prepare and activate SFF host */
Tejun Heo9363c382008-04-07 22:47:16 +09002710 rc = ata_pci_sff_prepare_host(pdev, ppi, &host);
Tejun Heod583bc12007-07-04 18:02:07 +09002711 if (rc)
Tejun Heo4e6b79f2008-01-18 18:36:28 +09002712 goto out;
Tejun Heo887125e2008-03-25 12:22:49 +09002713 host->private_data = host_priv;
Tejun Heod491b272007-04-17 23:44:07 +09002714
Tejun Heod491b272007-04-17 23:44:07 +09002715 pci_set_master(pdev);
Tejun Heo9363c382008-04-07 22:47:16 +09002716 rc = ata_pci_sff_activate_host(host, ata_sff_interrupt, sht);
Tejun Heo4e6b79f2008-01-18 18:36:28 +09002717 out:
2718 if (rc == 0)
2719 devres_remove_group(&pdev->dev, NULL);
2720 else
2721 devres_release_group(&pdev->dev, NULL);
Tejun Heod491b272007-04-17 23:44:07 +09002722
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002723 return rc;
2724}
2725
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002726#endif /* CONFIG_PCI */
2727
Tejun Heo624d5c52008-03-25 22:16:41 +09002728EXPORT_SYMBOL_GPL(ata_sff_port_ops);
2729EXPORT_SYMBOL_GPL(ata_bmdma_port_ops);
Tejun Heo9363c382008-04-07 22:47:16 +09002730EXPORT_SYMBOL_GPL(ata_sff_qc_prep);
2731EXPORT_SYMBOL_GPL(ata_sff_dumb_qc_prep);
2732EXPORT_SYMBOL_GPL(ata_sff_dev_select);
2733EXPORT_SYMBOL_GPL(ata_sff_check_status);
2734EXPORT_SYMBOL_GPL(ata_sff_altstatus);
2735EXPORT_SYMBOL_GPL(ata_sff_busy_sleep);
2736EXPORT_SYMBOL_GPL(ata_sff_wait_ready);
2737EXPORT_SYMBOL_GPL(ata_sff_tf_load);
2738EXPORT_SYMBOL_GPL(ata_sff_tf_read);
2739EXPORT_SYMBOL_GPL(ata_sff_exec_command);
2740EXPORT_SYMBOL_GPL(ata_sff_data_xfer);
2741EXPORT_SYMBOL_GPL(ata_sff_data_xfer_noirq);
2742EXPORT_SYMBOL_GPL(ata_sff_irq_on);
2743EXPORT_SYMBOL_GPL(ata_sff_irq_clear);
2744EXPORT_SYMBOL_GPL(ata_sff_hsm_move);
2745EXPORT_SYMBOL_GPL(ata_sff_qc_issue);
2746EXPORT_SYMBOL_GPL(ata_sff_host_intr);
2747EXPORT_SYMBOL_GPL(ata_sff_interrupt);
2748EXPORT_SYMBOL_GPL(ata_sff_freeze);
2749EXPORT_SYMBOL_GPL(ata_sff_thaw);
2750EXPORT_SYMBOL_GPL(ata_sff_prereset);
2751EXPORT_SYMBOL_GPL(ata_sff_dev_classify);
2752EXPORT_SYMBOL_GPL(ata_sff_wait_after_reset);
2753EXPORT_SYMBOL_GPL(ata_sff_softreset);
2754EXPORT_SYMBOL_GPL(sata_sff_hardreset);
2755EXPORT_SYMBOL_GPL(ata_sff_postreset);
2756EXPORT_SYMBOL_GPL(ata_sff_error_handler);
2757EXPORT_SYMBOL_GPL(ata_sff_post_internal_cmd);
Tejun Heo624d5c52008-03-25 22:16:41 +09002758EXPORT_SYMBOL_GPL(ata_sff_port_start);
Tejun Heo9363c382008-04-07 22:47:16 +09002759EXPORT_SYMBOL_GPL(ata_sff_std_ports);
2760EXPORT_SYMBOL_GPL(ata_bmdma_mode_filter);
Tejun Heo624d5c52008-03-25 22:16:41 +09002761EXPORT_SYMBOL_GPL(ata_bmdma_setup);
2762EXPORT_SYMBOL_GPL(ata_bmdma_start);
2763EXPORT_SYMBOL_GPL(ata_bmdma_stop);
2764EXPORT_SYMBOL_GPL(ata_bmdma_status);
2765EXPORT_SYMBOL_GPL(ata_bus_reset);
2766#ifdef CONFIG_PCI
Tejun Heo9363c382008-04-07 22:47:16 +09002767EXPORT_SYMBOL_GPL(ata_pci_bmdma_clear_simplex);
2768EXPORT_SYMBOL_GPL(ata_pci_bmdma_init);
2769EXPORT_SYMBOL_GPL(ata_pci_sff_init_host);
2770EXPORT_SYMBOL_GPL(ata_pci_sff_prepare_host);
2771EXPORT_SYMBOL_GPL(ata_pci_sff_activate_host);
2772EXPORT_SYMBOL_GPL(ata_pci_sff_init_one);
Tejun Heo624d5c52008-03-25 22:16:41 +09002773#endif /* CONFIG_PCI */