[libata] ensure ->tf_read() hook reads Status and Error registers
[deliverable/linux.git] / drivers / scsi / libata-core.c
CommitLineData
1da177e4 1/*
af36d7f0
JG
2 * libata-core.c - helper library for ATA
3 *
4 * Maintained by: Jeff Garzik <jgarzik@pobox.com>
5 * Please ALWAYS copy linux-ide@vger.kernel.org
6 * on emails.
7 *
8 * Copyright 2003-2004 Red Hat, Inc. All rights reserved.
9 * Copyright 2003-2004 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 *
1da177e4
LT
33 */
34
35#include <linux/config.h>
36#include <linux/kernel.h>
37#include <linux/module.h>
38#include <linux/pci.h>
39#include <linux/init.h>
40#include <linux/list.h>
41#include <linux/mm.h>
42#include <linux/highmem.h>
43#include <linux/spinlock.h>
44#include <linux/blkdev.h>
45#include <linux/delay.h>
46#include <linux/timer.h>
47#include <linux/interrupt.h>
48#include <linux/completion.h>
49#include <linux/suspend.h>
50#include <linux/workqueue.h>
67846b30 51#include <linux/jiffies.h>
1da177e4
LT
52#include <scsi/scsi.h>
53#include "scsi.h"
54#include "scsi_priv.h"
55#include <scsi/scsi_host.h>
56#include <linux/libata.h>
57#include <asm/io.h>
58#include <asm/semaphore.h>
59#include <asm/byteorder.h>
60
61#include "libata.h"
62
63static unsigned int ata_busy_sleep (struct ata_port *ap,
64 unsigned long tmout_pat,
65 unsigned long tmout);
59a10b17 66static void ata_dev_reread_id(struct ata_port *ap, struct ata_device *dev);
8bf62ece 67static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev);
1da177e4
LT
68static void ata_set_mode(struct ata_port *ap);
69static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev);
057ace5e 70static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift);
1da177e4 71static int fgb(u32 bitmap);
057ace5e 72static int ata_choose_xfer_mode(const struct ata_port *ap,
1da177e4
LT
73 u8 *xfer_mode_out,
74 unsigned int *xfer_shift_out);
1da177e4
LT
75static void __ata_qc_complete(struct ata_queued_cmd *qc);
76
77static unsigned int ata_unique_id = 1;
78static struct workqueue_struct *ata_wq;
79
1623c81e
JG
80int atapi_enabled = 0;
81module_param(atapi_enabled, int, 0444);
82MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)");
83
1da177e4
LT
84MODULE_AUTHOR("Jeff Garzik");
85MODULE_DESCRIPTION("Library module for ATA devices");
86MODULE_LICENSE("GPL");
87MODULE_VERSION(DRV_VERSION);
88
89/**
6f0ef4fa 90 * ata_tf_load_pio - send taskfile registers to host controller
1da177e4
LT
91 * @ap: Port to which output is sent
92 * @tf: ATA taskfile register set
93 *
94 * Outputs ATA taskfile to standard ATA host controller.
95 *
96 * LOCKING:
97 * Inherited from caller.
98 */
99
057ace5e 100static void ata_tf_load_pio(struct ata_port *ap, const struct ata_taskfile *tf)
1da177e4
LT
101{
102 struct ata_ioports *ioaddr = &ap->ioaddr;
103 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
104
105 if (tf->ctl != ap->last_ctl) {
106 outb(tf->ctl, ioaddr->ctl_addr);
107 ap->last_ctl = tf->ctl;
108 ata_wait_idle(ap);
109 }
110
111 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
112 outb(tf->hob_feature, ioaddr->feature_addr);
113 outb(tf->hob_nsect, ioaddr->nsect_addr);
114 outb(tf->hob_lbal, ioaddr->lbal_addr);
115 outb(tf->hob_lbam, ioaddr->lbam_addr);
116 outb(tf->hob_lbah, ioaddr->lbah_addr);
117 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
118 tf->hob_feature,
119 tf->hob_nsect,
120 tf->hob_lbal,
121 tf->hob_lbam,
122 tf->hob_lbah);
123 }
124
125 if (is_addr) {
126 outb(tf->feature, ioaddr->feature_addr);
127 outb(tf->nsect, ioaddr->nsect_addr);
128 outb(tf->lbal, ioaddr->lbal_addr);
129 outb(tf->lbam, ioaddr->lbam_addr);
130 outb(tf->lbah, ioaddr->lbah_addr);
131 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
132 tf->feature,
133 tf->nsect,
134 tf->lbal,
135 tf->lbam,
136 tf->lbah);
137 }
138
139 if (tf->flags & ATA_TFLAG_DEVICE) {
140 outb(tf->device, ioaddr->device_addr);
141 VPRINTK("device 0x%X\n", tf->device);
142 }
143
144 ata_wait_idle(ap);
145}
146
147/**
148 * ata_tf_load_mmio - send taskfile registers to host controller
149 * @ap: Port to which output is sent
150 * @tf: ATA taskfile register set
151 *
152 * Outputs ATA taskfile to standard ATA host controller using MMIO.
153 *
154 * LOCKING:
155 * Inherited from caller.
156 */
157
057ace5e 158static void ata_tf_load_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
1da177e4
LT
159{
160 struct ata_ioports *ioaddr = &ap->ioaddr;
161 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
162
163 if (tf->ctl != ap->last_ctl) {
164 writeb(tf->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
165 ap->last_ctl = tf->ctl;
166 ata_wait_idle(ap);
167 }
168
169 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
170 writeb(tf->hob_feature, (void __iomem *) ioaddr->feature_addr);
171 writeb(tf->hob_nsect, (void __iomem *) ioaddr->nsect_addr);
172 writeb(tf->hob_lbal, (void __iomem *) ioaddr->lbal_addr);
173 writeb(tf->hob_lbam, (void __iomem *) ioaddr->lbam_addr);
174 writeb(tf->hob_lbah, (void __iomem *) ioaddr->lbah_addr);
175 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
176 tf->hob_feature,
177 tf->hob_nsect,
178 tf->hob_lbal,
179 tf->hob_lbam,
180 tf->hob_lbah);
181 }
182
183 if (is_addr) {
184 writeb(tf->feature, (void __iomem *) ioaddr->feature_addr);
185 writeb(tf->nsect, (void __iomem *) ioaddr->nsect_addr);
186 writeb(tf->lbal, (void __iomem *) ioaddr->lbal_addr);
187 writeb(tf->lbam, (void __iomem *) ioaddr->lbam_addr);
188 writeb(tf->lbah, (void __iomem *) ioaddr->lbah_addr);
189 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
190 tf->feature,
191 tf->nsect,
192 tf->lbal,
193 tf->lbam,
194 tf->lbah);
195 }
196
197 if (tf->flags & ATA_TFLAG_DEVICE) {
198 writeb(tf->device, (void __iomem *) ioaddr->device_addr);
199 VPRINTK("device 0x%X\n", tf->device);
200 }
201
202 ata_wait_idle(ap);
203}
204
0baab86b
EF
205
206/**
207 * ata_tf_load - send taskfile registers to host controller
208 * @ap: Port to which output is sent
209 * @tf: ATA taskfile register set
210 *
211 * Outputs ATA taskfile to standard ATA host controller using MMIO
212 * or PIO as indicated by the ATA_FLAG_MMIO flag.
213 * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
214 * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
215 * hob_lbal, hob_lbam, and hob_lbah.
216 *
217 * This function waits for idle (!BUSY and !DRQ) after writing
218 * registers. If the control register has a new value, this
219 * function also waits for idle after writing control and before
220 * writing the remaining registers.
221 *
222 * May be used as the tf_load() entry in ata_port_operations.
223 *
224 * LOCKING:
225 * Inherited from caller.
226 */
057ace5e 227void ata_tf_load(struct ata_port *ap, const struct ata_taskfile *tf)
1da177e4
LT
228{
229 if (ap->flags & ATA_FLAG_MMIO)
230 ata_tf_load_mmio(ap, tf);
231 else
232 ata_tf_load_pio(ap, tf);
233}
234
235/**
0baab86b 236 * ata_exec_command_pio - issue ATA command to host controller
1da177e4
LT
237 * @ap: port to which command is being issued
238 * @tf: ATA taskfile register set
239 *
0baab86b 240 * Issues PIO write to ATA command register, with proper
1da177e4
LT
241 * synchronization with interrupt handler / other threads.
242 *
243 * LOCKING:
244 * spin_lock_irqsave(host_set lock)
245 */
246
057ace5e 247static void ata_exec_command_pio(struct ata_port *ap, const struct ata_taskfile *tf)
1da177e4
LT
248{
249 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
250
251 outb(tf->command, ap->ioaddr.command_addr);
252 ata_pause(ap);
253}
254
255
256/**
257 * ata_exec_command_mmio - issue ATA command to host controller
258 * @ap: port to which command is being issued
259 * @tf: ATA taskfile register set
260 *
261 * Issues MMIO write to ATA command register, with proper
262 * synchronization with interrupt handler / other threads.
263 *
264 * LOCKING:
265 * spin_lock_irqsave(host_set lock)
266 */
267
057ace5e 268static void ata_exec_command_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
1da177e4
LT
269{
270 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
271
272 writeb(tf->command, (void __iomem *) ap->ioaddr.command_addr);
273 ata_pause(ap);
274}
275
0baab86b
EF
276
277/**
278 * ata_exec_command - issue ATA command to host controller
279 * @ap: port to which command is being issued
280 * @tf: ATA taskfile register set
281 *
282 * Issues PIO/MMIO write to ATA command register, with proper
283 * synchronization with interrupt handler / other threads.
284 *
285 * LOCKING:
286 * spin_lock_irqsave(host_set lock)
287 */
057ace5e 288void ata_exec_command(struct ata_port *ap, const struct ata_taskfile *tf)
1da177e4
LT
289{
290 if (ap->flags & ATA_FLAG_MMIO)
291 ata_exec_command_mmio(ap, tf);
292 else
293 ata_exec_command_pio(ap, tf);
294}
295
296/**
297 * ata_exec - issue ATA command to host controller
298 * @ap: port to which command is being issued
299 * @tf: ATA taskfile register set
300 *
301 * Issues PIO/MMIO write to ATA command register, with proper
302 * synchronization with interrupt handler / other threads.
303 *
304 * LOCKING:
305 * Obtains host_set lock.
306 */
307
057ace5e 308static inline void ata_exec(struct ata_port *ap, const struct ata_taskfile *tf)
1da177e4
LT
309{
310 unsigned long flags;
311
312 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
313 spin_lock_irqsave(&ap->host_set->lock, flags);
314 ap->ops->exec_command(ap, tf);
315 spin_unlock_irqrestore(&ap->host_set->lock, flags);
316}
317
318/**
319 * ata_tf_to_host - issue ATA taskfile to host controller
320 * @ap: port to which command is being issued
321 * @tf: ATA taskfile register set
322 *
323 * Issues ATA taskfile register set to ATA host controller,
324 * with proper synchronization with interrupt handler and
325 * other threads.
326 *
327 * LOCKING:
328 * Obtains host_set lock.
329 */
330
057ace5e 331static void ata_tf_to_host(struct ata_port *ap, const struct ata_taskfile *tf)
1da177e4
LT
332{
333 ap->ops->tf_load(ap, tf);
334
335 ata_exec(ap, tf);
336}
337
338/**
339 * ata_tf_to_host_nolock - issue ATA taskfile to host controller
340 * @ap: port to which command is being issued
341 * @tf: ATA taskfile register set
342 *
343 * Issues ATA taskfile register set to ATA host controller,
344 * with proper synchronization with interrupt handler and
345 * other threads.
346 *
347 * LOCKING:
348 * spin_lock_irqsave(host_set lock)
349 */
350
057ace5e 351void ata_tf_to_host_nolock(struct ata_port *ap, const struct ata_taskfile *tf)
1da177e4
LT
352{
353 ap->ops->tf_load(ap, tf);
354 ap->ops->exec_command(ap, tf);
355}
356
357/**
0baab86b 358 * ata_tf_read_pio - input device's ATA taskfile shadow registers
1da177e4
LT
359 * @ap: Port from which input is read
360 * @tf: ATA taskfile register set for storing input
361 *
362 * Reads ATA taskfile registers for currently-selected device
363 * into @tf.
364 *
365 * LOCKING:
366 * Inherited from caller.
367 */
368
369static void ata_tf_read_pio(struct ata_port *ap, struct ata_taskfile *tf)
370{
371 struct ata_ioports *ioaddr = &ap->ioaddr;
372
ac19bff2
JG
373 tf->command = ata_check_status(ap);
374 tf->feature = ata_chk_err(ap);
1da177e4
LT
375 tf->nsect = inb(ioaddr->nsect_addr);
376 tf->lbal = inb(ioaddr->lbal_addr);
377 tf->lbam = inb(ioaddr->lbam_addr);
378 tf->lbah = inb(ioaddr->lbah_addr);
379 tf->device = inb(ioaddr->device_addr);
380
381 if (tf->flags & ATA_TFLAG_LBA48) {
382 outb(tf->ctl | ATA_HOB, ioaddr->ctl_addr);
383 tf->hob_feature = inb(ioaddr->error_addr);
384 tf->hob_nsect = inb(ioaddr->nsect_addr);
385 tf->hob_lbal = inb(ioaddr->lbal_addr);
386 tf->hob_lbam = inb(ioaddr->lbam_addr);
387 tf->hob_lbah = inb(ioaddr->lbah_addr);
388 }
389}
390
391/**
392 * ata_tf_read_mmio - input device's ATA taskfile shadow registers
393 * @ap: Port from which input is read
394 * @tf: ATA taskfile register set for storing input
395 *
396 * Reads ATA taskfile registers for currently-selected device
397 * into @tf via MMIO.
398 *
399 * LOCKING:
400 * Inherited from caller.
401 */
402
403static void ata_tf_read_mmio(struct ata_port *ap, struct ata_taskfile *tf)
404{
405 struct ata_ioports *ioaddr = &ap->ioaddr;
406
ac19bff2
JG
407 tf->command = ata_check_status(ap);
408 tf->feature = ata_chk_err(ap);
1da177e4
LT
409 tf->nsect = readb((void __iomem *)ioaddr->nsect_addr);
410 tf->lbal = readb((void __iomem *)ioaddr->lbal_addr);
411 tf->lbam = readb((void __iomem *)ioaddr->lbam_addr);
412 tf->lbah = readb((void __iomem *)ioaddr->lbah_addr);
413 tf->device = readb((void __iomem *)ioaddr->device_addr);
414
415 if (tf->flags & ATA_TFLAG_LBA48) {
416 writeb(tf->ctl | ATA_HOB, (void __iomem *) ap->ioaddr.ctl_addr);
417 tf->hob_feature = readb((void __iomem *)ioaddr->error_addr);
418 tf->hob_nsect = readb((void __iomem *)ioaddr->nsect_addr);
419 tf->hob_lbal = readb((void __iomem *)ioaddr->lbal_addr);
420 tf->hob_lbam = readb((void __iomem *)ioaddr->lbam_addr);
421 tf->hob_lbah = readb((void __iomem *)ioaddr->lbah_addr);
422 }
423}
424
0baab86b
EF
425
426/**
427 * ata_tf_read - input device's ATA taskfile shadow registers
428 * @ap: Port from which input is read
429 * @tf: ATA taskfile register set for storing input
430 *
431 * Reads ATA taskfile registers for currently-selected device
432 * into @tf.
433 *
434 * Reads nsect, lbal, lbam, lbah, and device. If ATA_TFLAG_LBA48
435 * is set, also reads the hob registers.
436 *
437 * May be used as the tf_read() entry in ata_port_operations.
438 *
439 * LOCKING:
440 * Inherited from caller.
441 */
1da177e4
LT
442void ata_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
443{
444 if (ap->flags & ATA_FLAG_MMIO)
445 ata_tf_read_mmio(ap, tf);
446 else
447 ata_tf_read_pio(ap, tf);
448}
449
450/**
451 * ata_check_status_pio - Read device status reg & clear interrupt
452 * @ap: port where the device is
453 *
454 * Reads ATA taskfile status register for currently-selected device
0baab86b 455 * and return its value. This also clears pending interrupts
1da177e4
LT
456 * from this device
457 *
458 * LOCKING:
459 * Inherited from caller.
460 */
461static u8 ata_check_status_pio(struct ata_port *ap)
462{
463 return inb(ap->ioaddr.status_addr);
464}
465
466/**
467 * ata_check_status_mmio - Read device status reg & clear interrupt
468 * @ap: port where the device is
469 *
470 * Reads ATA taskfile status register for currently-selected device
0baab86b 471 * via MMIO and return its value. This also clears pending interrupts
1da177e4
LT
472 * from this device
473 *
474 * LOCKING:
475 * Inherited from caller.
476 */
477static u8 ata_check_status_mmio(struct ata_port *ap)
478{
479 return readb((void __iomem *) ap->ioaddr.status_addr);
480}
481
0baab86b
EF
482
483/**
484 * ata_check_status - Read device status reg & clear interrupt
485 * @ap: port where the device is
486 *
487 * Reads ATA taskfile status register for currently-selected device
488 * and return its value. This also clears pending interrupts
489 * from this device
490 *
491 * May be used as the check_status() entry in ata_port_operations.
492 *
493 * LOCKING:
494 * Inherited from caller.
495 */
1da177e4
LT
496u8 ata_check_status(struct ata_port *ap)
497{
498 if (ap->flags & ATA_FLAG_MMIO)
499 return ata_check_status_mmio(ap);
500 return ata_check_status_pio(ap);
501}
502
0baab86b
EF
503
504/**
505 * ata_altstatus - Read device alternate status reg
506 * @ap: port where the device is
507 *
508 * Reads ATA taskfile alternate status register for
509 * currently-selected device and return its value.
510 *
511 * Note: may NOT be used as the check_altstatus() entry in
512 * ata_port_operations.
513 *
514 * LOCKING:
515 * Inherited from caller.
516 */
1da177e4
LT
517u8 ata_altstatus(struct ata_port *ap)
518{
519 if (ap->ops->check_altstatus)
520 return ap->ops->check_altstatus(ap);
521
522 if (ap->flags & ATA_FLAG_MMIO)
523 return readb((void __iomem *)ap->ioaddr.altstatus_addr);
524 return inb(ap->ioaddr.altstatus_addr);
525}
526
0baab86b
EF
527
528/**
529 * ata_chk_err - Read device error reg
530 * @ap: port where the device is
531 *
532 * Reads ATA taskfile error register for
533 * currently-selected device and return its value.
534 *
535 * Note: may NOT be used as the check_err() entry in
536 * ata_port_operations.
537 *
538 * LOCKING:
539 * Inherited from caller.
540 */
1da177e4
LT
541u8 ata_chk_err(struct ata_port *ap)
542{
543 if (ap->ops->check_err)
544 return ap->ops->check_err(ap);
545
546 if (ap->flags & ATA_FLAG_MMIO) {
547 return readb((void __iomem *) ap->ioaddr.error_addr);
548 }
549 return inb(ap->ioaddr.error_addr);
550}
551
552/**
553 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
554 * @tf: Taskfile to convert
555 * @fis: Buffer into which data will output
556 * @pmp: Port multiplier port
557 *
558 * Converts a standard ATA taskfile to a Serial ATA
559 * FIS structure (Register - Host to Device).
560 *
561 * LOCKING:
562 * Inherited from caller.
563 */
564
057ace5e 565void ata_tf_to_fis(const struct ata_taskfile *tf, u8 *fis, u8 pmp)
1da177e4
LT
566{
567 fis[0] = 0x27; /* Register - Host to Device FIS */
568 fis[1] = (pmp & 0xf) | (1 << 7); /* Port multiplier number,
569 bit 7 indicates Command FIS */
570 fis[2] = tf->command;
571 fis[3] = tf->feature;
572
573 fis[4] = tf->lbal;
574 fis[5] = tf->lbam;
575 fis[6] = tf->lbah;
576 fis[7] = tf->device;
577
578 fis[8] = tf->hob_lbal;
579 fis[9] = tf->hob_lbam;
580 fis[10] = tf->hob_lbah;
581 fis[11] = tf->hob_feature;
582
583 fis[12] = tf->nsect;
584 fis[13] = tf->hob_nsect;
585 fis[14] = 0;
586 fis[15] = tf->ctl;
587
588 fis[16] = 0;
589 fis[17] = 0;
590 fis[18] = 0;
591 fis[19] = 0;
592}
593
594/**
595 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
596 * @fis: Buffer from which data will be input
597 * @tf: Taskfile to output
598 *
599 * Converts a standard ATA taskfile to a Serial ATA
600 * FIS structure (Register - Host to Device).
601 *
602 * LOCKING:
603 * Inherited from caller.
604 */
605
057ace5e 606void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
1da177e4
LT
607{
608 tf->command = fis[2]; /* status */
609 tf->feature = fis[3]; /* error */
610
611 tf->lbal = fis[4];
612 tf->lbam = fis[5];
613 tf->lbah = fis[6];
614 tf->device = fis[7];
615
616 tf->hob_lbal = fis[8];
617 tf->hob_lbam = fis[9];
618 tf->hob_lbah = fis[10];
619
620 tf->nsect = fis[12];
621 tf->hob_nsect = fis[13];
622}
623
8cbd6df1
AL
624static const u8 ata_rw_cmds[] = {
625 /* pio multi */
626 ATA_CMD_READ_MULTI,
627 ATA_CMD_WRITE_MULTI,
628 ATA_CMD_READ_MULTI_EXT,
629 ATA_CMD_WRITE_MULTI_EXT,
630 /* pio */
631 ATA_CMD_PIO_READ,
632 ATA_CMD_PIO_WRITE,
633 ATA_CMD_PIO_READ_EXT,
634 ATA_CMD_PIO_WRITE_EXT,
635 /* dma */
636 ATA_CMD_READ,
637 ATA_CMD_WRITE,
638 ATA_CMD_READ_EXT,
639 ATA_CMD_WRITE_EXT
640};
1da177e4
LT
641
642/**
8cbd6df1
AL
643 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
644 * @qc: command to examine and configure
1da177e4 645 *
8cbd6df1
AL
646 * Examine the device configuration and tf->flags to calculate
647 * the proper read/write commands and protocol to use.
1da177e4
LT
648 *
649 * LOCKING:
650 * caller.
651 */
8cbd6df1 652void ata_rwcmd_protocol(struct ata_queued_cmd *qc)
1da177e4 653{
8cbd6df1
AL
654 struct ata_taskfile *tf = &qc->tf;
655 struct ata_device *dev = qc->dev;
1da177e4 656
8cbd6df1
AL
657 int index, lba48, write;
658
659 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
660 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
1da177e4 661
8cbd6df1
AL
662 if (dev->flags & ATA_DFLAG_PIO) {
663 tf->protocol = ATA_PROT_PIO;
664 index = dev->multi_count ? 0 : 4;
665 } else {
666 tf->protocol = ATA_PROT_DMA;
667 index = 8;
668 }
1da177e4 669
8cbd6df1 670 tf->command = ata_rw_cmds[index + lba48 + write];
1da177e4
LT
671}
672
673static const char * xfer_mode_str[] = {
674 "UDMA/16",
675 "UDMA/25",
676 "UDMA/33",
677 "UDMA/44",
678 "UDMA/66",
679 "UDMA/100",
680 "UDMA/133",
681 "UDMA7",
682 "MWDMA0",
683 "MWDMA1",
684 "MWDMA2",
685 "PIO0",
686 "PIO1",
687 "PIO2",
688 "PIO3",
689 "PIO4",
690};
691
692/**
693 * ata_udma_string - convert UDMA bit offset to string
694 * @mask: mask of bits supported; only highest bit counts.
695 *
696 * Determine string which represents the highest speed
697 * (highest bit in @udma_mask).
698 *
699 * LOCKING:
700 * None.
701 *
702 * RETURNS:
703 * Constant C string representing highest speed listed in
704 * @udma_mask, or the constant C string "<n/a>".
705 */
706
707static const char *ata_mode_string(unsigned int mask)
708{
709 int i;
710
711 for (i = 7; i >= 0; i--)
712 if (mask & (1 << i))
713 goto out;
714 for (i = ATA_SHIFT_MWDMA + 2; i >= ATA_SHIFT_MWDMA; i--)
715 if (mask & (1 << i))
716 goto out;
717 for (i = ATA_SHIFT_PIO + 4; i >= ATA_SHIFT_PIO; i--)
718 if (mask & (1 << i))
719 goto out;
720
721 return "<n/a>";
722
723out:
724 return xfer_mode_str[i];
725}
726
727/**
728 * ata_pio_devchk - PATA device presence detection
729 * @ap: ATA channel to examine
730 * @device: Device to examine (starting at zero)
731 *
732 * This technique was originally described in
733 * Hale Landis's ATADRVR (www.ata-atapi.com), and
734 * later found its way into the ATA/ATAPI spec.
735 *
736 * Write a pattern to the ATA shadow registers,
737 * and if a device is present, it will respond by
738 * correctly storing and echoing back the
739 * ATA shadow register contents.
740 *
741 * LOCKING:
742 * caller.
743 */
744
745static unsigned int ata_pio_devchk(struct ata_port *ap,
746 unsigned int device)
747{
748 struct ata_ioports *ioaddr = &ap->ioaddr;
749 u8 nsect, lbal;
750
751 ap->ops->dev_select(ap, device);
752
753 outb(0x55, ioaddr->nsect_addr);
754 outb(0xaa, ioaddr->lbal_addr);
755
756 outb(0xaa, ioaddr->nsect_addr);
757 outb(0x55, ioaddr->lbal_addr);
758
759 outb(0x55, ioaddr->nsect_addr);
760 outb(0xaa, ioaddr->lbal_addr);
761
762 nsect = inb(ioaddr->nsect_addr);
763 lbal = inb(ioaddr->lbal_addr);
764
765 if ((nsect == 0x55) && (lbal == 0xaa))
766 return 1; /* we found a device */
767
768 return 0; /* nothing found */
769}
770
771/**
772 * ata_mmio_devchk - PATA device presence detection
773 * @ap: ATA channel to examine
774 * @device: Device to examine (starting at zero)
775 *
776 * This technique was originally described in
777 * Hale Landis's ATADRVR (www.ata-atapi.com), and
778 * later found its way into the ATA/ATAPI spec.
779 *
780 * Write a pattern to the ATA shadow registers,
781 * and if a device is present, it will respond by
782 * correctly storing and echoing back the
783 * ATA shadow register contents.
784 *
785 * LOCKING:
786 * caller.
787 */
788
789static unsigned int ata_mmio_devchk(struct ata_port *ap,
790 unsigned int device)
791{
792 struct ata_ioports *ioaddr = &ap->ioaddr;
793 u8 nsect, lbal;
794
795 ap->ops->dev_select(ap, device);
796
797 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
798 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
799
800 writeb(0xaa, (void __iomem *) ioaddr->nsect_addr);
801 writeb(0x55, (void __iomem *) ioaddr->lbal_addr);
802
803 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
804 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
805
806 nsect = readb((void __iomem *) ioaddr->nsect_addr);
807 lbal = readb((void __iomem *) ioaddr->lbal_addr);
808
809 if ((nsect == 0x55) && (lbal == 0xaa))
810 return 1; /* we found a device */
811
812 return 0; /* nothing found */
813}
814
815/**
816 * ata_devchk - PATA device presence detection
817 * @ap: ATA channel to examine
818 * @device: Device to examine (starting at zero)
819 *
820 * Dispatch ATA device presence detection, depending
821 * on whether we are using PIO or MMIO to talk to the
822 * ATA shadow registers.
823 *
824 * LOCKING:
825 * caller.
826 */
827
828static unsigned int ata_devchk(struct ata_port *ap,
829 unsigned int device)
830{
831 if (ap->flags & ATA_FLAG_MMIO)
832 return ata_mmio_devchk(ap, device);
833 return ata_pio_devchk(ap, device);
834}
835
836/**
837 * ata_dev_classify - determine device type based on ATA-spec signature
838 * @tf: ATA taskfile register set for device to be identified
839 *
840 * Determine from taskfile register contents whether a device is
841 * ATA or ATAPI, as per "Signature and persistence" section
842 * of ATA/PI spec (volume 1, sect 5.14).
843 *
844 * LOCKING:
845 * None.
846 *
847 * RETURNS:
848 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
849 * the event of failure.
850 */
851
057ace5e 852unsigned int ata_dev_classify(const struct ata_taskfile *tf)
1da177e4
LT
853{
854 /* Apple's open source Darwin code hints that some devices only
855 * put a proper signature into the LBA mid/high registers,
856 * So, we only check those. It's sufficient for uniqueness.
857 */
858
859 if (((tf->lbam == 0) && (tf->lbah == 0)) ||
860 ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
861 DPRINTK("found ATA device by sig\n");
862 return ATA_DEV_ATA;
863 }
864
865 if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
866 ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
867 DPRINTK("found ATAPI device by sig\n");
868 return ATA_DEV_ATAPI;
869 }
870
871 DPRINTK("unknown device\n");
872 return ATA_DEV_UNKNOWN;
873}
874
875/**
876 * ata_dev_try_classify - Parse returned ATA device signature
877 * @ap: ATA channel to examine
878 * @device: Device to examine (starting at zero)
879 *
880 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
881 * an ATA/ATAPI-defined set of values is placed in the ATA
882 * shadow registers, indicating the results of device detection
883 * and diagnostics.
884 *
885 * Select the ATA device, and read the values from the ATA shadow
886 * registers. Then parse according to the Error register value,
887 * and the spec-defined values examined by ata_dev_classify().
888 *
889 * LOCKING:
890 * caller.
891 */
892
893static u8 ata_dev_try_classify(struct ata_port *ap, unsigned int device)
894{
895 struct ata_device *dev = &ap->device[device];
896 struct ata_taskfile tf;
897 unsigned int class;
898 u8 err;
899
900 ap->ops->dev_select(ap, device);
901
902 memset(&tf, 0, sizeof(tf));
903
904 err = ata_chk_err(ap);
905 ap->ops->tf_read(ap, &tf);
906
907 dev->class = ATA_DEV_NONE;
908
909 /* see if device passed diags */
910 if (err == 1)
911 /* do nothing */ ;
912 else if ((device == 0) && (err == 0x81))
913 /* do nothing */ ;
914 else
915 return err;
916
917 /* determine if device if ATA or ATAPI */
918 class = ata_dev_classify(&tf);
919 if (class == ATA_DEV_UNKNOWN)
920 return err;
921 if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
922 return err;
923
924 dev->class = class;
925
926 return err;
927}
928
929/**
930 * ata_dev_id_string - Convert IDENTIFY DEVICE page into string
931 * @id: IDENTIFY DEVICE results we will examine
932 * @s: string into which data is output
933 * @ofs: offset into identify device page
934 * @len: length of string to return. must be an even number.
935 *
936 * The strings in the IDENTIFY DEVICE page are broken up into
937 * 16-bit chunks. Run through the string, and output each
938 * 8-bit chunk linearly, regardless of platform.
939 *
940 * LOCKING:
941 * caller.
942 */
943
057ace5e 944void ata_dev_id_string(const u16 *id, unsigned char *s,
1da177e4
LT
945 unsigned int ofs, unsigned int len)
946{
947 unsigned int c;
948
949 while (len > 0) {
950 c = id[ofs] >> 8;
951 *s = c;
952 s++;
953
954 c = id[ofs] & 0xff;
955 *s = c;
956 s++;
957
958 ofs++;
959 len -= 2;
960 }
961}
962
0baab86b
EF
963
964/**
965 * ata_noop_dev_select - Select device 0/1 on ATA bus
966 * @ap: ATA channel to manipulate
967 * @device: ATA device (numbered from zero) to select
968 *
969 * This function performs no actual function.
970 *
971 * May be used as the dev_select() entry in ata_port_operations.
972 *
973 * LOCKING:
974 * caller.
975 */
1da177e4
LT
976void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
977{
978}
979
0baab86b 980
1da177e4
LT
981/**
982 * ata_std_dev_select - Select device 0/1 on ATA bus
983 * @ap: ATA channel to manipulate
984 * @device: ATA device (numbered from zero) to select
985 *
986 * Use the method defined in the ATA specification to
987 * make either device 0, or device 1, active on the
0baab86b
EF
988 * ATA channel. Works with both PIO and MMIO.
989 *
990 * May be used as the dev_select() entry in ata_port_operations.
1da177e4
LT
991 *
992 * LOCKING:
993 * caller.
994 */
995
996void ata_std_dev_select (struct ata_port *ap, unsigned int device)
997{
998 u8 tmp;
999
1000 if (device == 0)
1001 tmp = ATA_DEVICE_OBS;
1002 else
1003 tmp = ATA_DEVICE_OBS | ATA_DEV1;
1004
1005 if (ap->flags & ATA_FLAG_MMIO) {
1006 writeb(tmp, (void __iomem *) ap->ioaddr.device_addr);
1007 } else {
1008 outb(tmp, ap->ioaddr.device_addr);
1009 }
1010 ata_pause(ap); /* needed; also flushes, for mmio */
1011}
1012
1013/**
1014 * ata_dev_select - Select device 0/1 on ATA bus
1015 * @ap: ATA channel to manipulate
1016 * @device: ATA device (numbered from zero) to select
1017 * @wait: non-zero to wait for Status register BSY bit to clear
1018 * @can_sleep: non-zero if context allows sleeping
1019 *
1020 * Use the method defined in the ATA specification to
1021 * make either device 0, or device 1, active on the
1022 * ATA channel.
1023 *
1024 * This is a high-level version of ata_std_dev_select(),
1025 * which additionally provides the services of inserting
1026 * the proper pauses and status polling, where needed.
1027 *
1028 * LOCKING:
1029 * caller.
1030 */
1031
1032void ata_dev_select(struct ata_port *ap, unsigned int device,
1033 unsigned int wait, unsigned int can_sleep)
1034{
1035 VPRINTK("ENTER, ata%u: device %u, wait %u\n",
1036 ap->id, device, wait);
1037
1038 if (wait)
1039 ata_wait_idle(ap);
1040
1041 ap->ops->dev_select(ap, device);
1042
1043 if (wait) {
1044 if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
1045 msleep(150);
1046 ata_wait_idle(ap);
1047 }
1048}
1049
1050/**
1051 * ata_dump_id - IDENTIFY DEVICE info debugging output
1052 * @dev: Device whose IDENTIFY DEVICE page we will dump
1053 *
1054 * Dump selected 16-bit words from a detected device's
1055 * IDENTIFY PAGE page.
1056 *
1057 * LOCKING:
1058 * caller.
1059 */
1060
057ace5e 1061static inline void ata_dump_id(const struct ata_device *dev)
1da177e4
LT
1062{
1063 DPRINTK("49==0x%04x "
1064 "53==0x%04x "
1065 "63==0x%04x "
1066 "64==0x%04x "
1067 "75==0x%04x \n",
1068 dev->id[49],
1069 dev->id[53],
1070 dev->id[63],
1071 dev->id[64],
1072 dev->id[75]);
1073 DPRINTK("80==0x%04x "
1074 "81==0x%04x "
1075 "82==0x%04x "
1076 "83==0x%04x "
1077 "84==0x%04x \n",
1078 dev->id[80],
1079 dev->id[81],
1080 dev->id[82],
1081 dev->id[83],
1082 dev->id[84]);
1083 DPRINTK("88==0x%04x "
1084 "93==0x%04x\n",
1085 dev->id[88],
1086 dev->id[93]);
1087}
1088
11e29e21
AC
1089/*
1090 * Compute the PIO modes available for this device. This is not as
1091 * trivial as it seems if we must consider early devices correctly.
1092 *
1093 * FIXME: pre IDE drive timing (do we care ?).
1094 */
1095
057ace5e 1096static unsigned int ata_pio_modes(const struct ata_device *adev)
11e29e21
AC
1097{
1098 u16 modes;
1099
1100 /* Usual case. Word 53 indicates word 88 is valid */
1101 if (adev->id[ATA_ID_FIELD_VALID] & (1 << 2)) {
1102 modes = adev->id[ATA_ID_PIO_MODES] & 0x03;
1103 modes <<= 3;
1104 modes |= 0x7;
1105 return modes;
1106 }
1107
1108 /* If word 88 isn't valid then Word 51 holds the PIO timing number
1109 for the maximum. Turn it into a mask and return it */
1110 modes = (2 << (adev->id[ATA_ID_OLD_PIO_MODES] & 0xFF)) - 1 ;
1111 return modes;
1112}
1113
1da177e4
LT
1114/**
1115 * ata_dev_identify - obtain IDENTIFY x DEVICE page
1116 * @ap: port on which device we wish to probe resides
1117 * @device: device bus address, starting at zero
1118 *
1119 * Following bus reset, we issue the IDENTIFY [PACKET] DEVICE
1120 * command, and read back the 512-byte device information page.
1121 * The device information page is fed to us via the standard
1122 * PIO-IN protocol, but we hand-code it here. (TODO: investigate
1123 * using standard PIO-IN paths)
1124 *
1125 * After reading the device information page, we use several
1126 * bits of information from it to initialize data structures
1127 * that will be used during the lifetime of the ata_device.
1128 * Other data from the info page is used to disqualify certain
1129 * older ATA devices we do not wish to support.
1130 *
1131 * LOCKING:
1132 * Inherited from caller. Some functions called by this function
1133 * obtain the host_set lock.
1134 */
1135
1136static void ata_dev_identify(struct ata_port *ap, unsigned int device)
1137{
1138 struct ata_device *dev = &ap->device[device];
8bf62ece 1139 unsigned int major_version;
1da177e4
LT
1140 u16 tmp;
1141 unsigned long xfer_modes;
1142 u8 status;
1143 unsigned int using_edd;
1144 DECLARE_COMPLETION(wait);
1145 struct ata_queued_cmd *qc;
1146 unsigned long flags;
1147 int rc;
1148
1149 if (!ata_dev_present(dev)) {
1150 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
1151 ap->id, device);
1152 return;
1153 }
1154
1155 if (ap->flags & (ATA_FLAG_SRST | ATA_FLAG_SATA_RESET))
1156 using_edd = 0;
1157 else
1158 using_edd = 1;
1159
1160 DPRINTK("ENTER, host %u, dev %u\n", ap->id, device);
1161
1162 assert (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ATAPI ||
1163 dev->class == ATA_DEV_NONE);
1164
1165 ata_dev_select(ap, device, 1, 1); /* select device 0/1 */
1166
1167 qc = ata_qc_new_init(ap, dev);
1168 BUG_ON(qc == NULL);
1169
1170 ata_sg_init_one(qc, dev->id, sizeof(dev->id));
1171 qc->dma_dir = DMA_FROM_DEVICE;
1172 qc->tf.protocol = ATA_PROT_PIO;
1173 qc->nsect = 1;
1174
1175retry:
1176 if (dev->class == ATA_DEV_ATA) {
1177 qc->tf.command = ATA_CMD_ID_ATA;
1178 DPRINTK("do ATA identify\n");
1179 } else {
1180 qc->tf.command = ATA_CMD_ID_ATAPI;
1181 DPRINTK("do ATAPI identify\n");
1182 }
1183
1184 qc->waiting = &wait;
1185 qc->complete_fn = ata_qc_complete_noop;
1186
1187 spin_lock_irqsave(&ap->host_set->lock, flags);
1188 rc = ata_qc_issue(qc);
1189 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1190
1191 if (rc)
1192 goto err_out;
1193 else
1194 wait_for_completion(&wait);
1195
1196 status = ata_chk_status(ap);
1197 if (status & ATA_ERR) {
1198 /*
1199 * arg! EDD works for all test cases, but seems to return
1200 * the ATA signature for some ATAPI devices. Until the
1201 * reason for this is found and fixed, we fix up the mess
1202 * here. If IDENTIFY DEVICE returns command aborted
1203 * (as ATAPI devices do), then we issue an
1204 * IDENTIFY PACKET DEVICE.
1205 *
1206 * ATA software reset (SRST, the default) does not appear
1207 * to have this problem.
1208 */
1209 if ((using_edd) && (qc->tf.command == ATA_CMD_ID_ATA)) {
1210 u8 err = ata_chk_err(ap);
1211 if (err & ATA_ABORTED) {
1212 dev->class = ATA_DEV_ATAPI;
1213 qc->cursg = 0;
1214 qc->cursg_ofs = 0;
1215 qc->cursect = 0;
1216 qc->nsect = 1;
1217 goto retry;
1218 }
1219 }
1220 goto err_out;
1221 }
1222
1223 swap_buf_le16(dev->id, ATA_ID_WORDS);
1224
1225 /* print device capabilities */
1226 printk(KERN_DEBUG "ata%u: dev %u cfg "
1227 "49:%04x 82:%04x 83:%04x 84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1228 ap->id, device, dev->id[49],
1229 dev->id[82], dev->id[83], dev->id[84],
1230 dev->id[85], dev->id[86], dev->id[87],
1231 dev->id[88]);
1232
1233 /*
1234 * common ATA, ATAPI feature tests
1235 */
1236
8bf62ece
AL
1237 /* we require DMA support (bits 8 of word 49) */
1238 if (!ata_id_has_dma(dev->id)) {
1239 printk(KERN_DEBUG "ata%u: no dma\n", ap->id);
1da177e4
LT
1240 goto err_out_nosup;
1241 }
1242
1243 /* quick-n-dirty find max transfer mode; for printk only */
1244 xfer_modes = dev->id[ATA_ID_UDMA_MODES];
1245 if (!xfer_modes)
1246 xfer_modes = (dev->id[ATA_ID_MWDMA_MODES]) << ATA_SHIFT_MWDMA;
11e29e21
AC
1247 if (!xfer_modes)
1248 xfer_modes = ata_pio_modes(dev);
1da177e4
LT
1249
1250 ata_dump_id(dev);
1251
1252 /* ATA-specific feature tests */
1253 if (dev->class == ATA_DEV_ATA) {
1254 if (!ata_id_is_ata(dev->id)) /* sanity check */
1255 goto err_out_nosup;
1256
8bf62ece 1257 /* get major version */
1da177e4 1258 tmp = dev->id[ATA_ID_MAJOR_VER];
8bf62ece
AL
1259 for (major_version = 14; major_version >= 1; major_version--)
1260 if (tmp & (1 << major_version))
1da177e4
LT
1261 break;
1262
8bf62ece
AL
1263 /*
1264 * The exact sequence expected by certain pre-ATA4 drives is:
1265 * SRST RESET
1266 * IDENTIFY
1267 * INITIALIZE DEVICE PARAMETERS
1268 * anything else..
1269 * Some drives were very specific about that exact sequence.
1270 */
59a10b17 1271 if (major_version < 4 || (!ata_id_has_lba(dev->id))) {
8bf62ece
AL
1272 ata_dev_init_params(ap, dev);
1273
59a10b17
AL
1274 /* current CHS translation info (id[53-58]) might be
1275 * changed. reread the identify device info.
1276 */
1277 ata_dev_reread_id(ap, dev);
1278 }
1279
8bf62ece
AL
1280 if (ata_id_has_lba(dev->id)) {
1281 dev->flags |= ATA_DFLAG_LBA;
1282
1283 if (ata_id_has_lba48(dev->id)) {
1284 dev->flags |= ATA_DFLAG_LBA48;
1285 dev->n_sectors = ata_id_u64(dev->id, 100);
1286 } else {
1287 dev->n_sectors = ata_id_u32(dev->id, 60);
1288 }
1289
1290 /* print device info to dmesg */
1291 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors:%s\n",
1292 ap->id, device,
1293 major_version,
1294 ata_mode_string(xfer_modes),
1295 (unsigned long long)dev->n_sectors,
1296 dev->flags & ATA_DFLAG_LBA48 ? " LBA48" : " LBA");
1297 } else {
1298 /* CHS */
1299
1300 /* Default translation */
1301 dev->cylinders = dev->id[1];
1302 dev->heads = dev->id[3];
1303 dev->sectors = dev->id[6];
1304 dev->n_sectors = dev->cylinders * dev->heads * dev->sectors;
1305
1306 if (ata_id_current_chs_valid(dev->id)) {
1307 /* Current CHS translation is valid. */
1308 dev->cylinders = dev->id[54];
1309 dev->heads = dev->id[55];
1310 dev->sectors = dev->id[56];
1311
1312 dev->n_sectors = ata_id_u32(dev->id, 57);
1313 }
1314
1315 /* print device info to dmesg */
1316 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors: CHS %d/%d/%d\n",
1317 ap->id, device,
1318 major_version,
1319 ata_mode_string(xfer_modes),
1320 (unsigned long long)dev->n_sectors,
1321 (int)dev->cylinders, (int)dev->heads, (int)dev->sectors);
1da177e4 1322
1da177e4
LT
1323 }
1324
1325 ap->host->max_cmd_len = 16;
1da177e4
LT
1326 }
1327
1328 /* ATAPI-specific feature tests */
1329 else {
1330 if (ata_id_is_ata(dev->id)) /* sanity check */
1331 goto err_out_nosup;
1332
1333 rc = atapi_cdb_len(dev->id);
1334 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
1335 printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
1336 goto err_out_nosup;
1337 }
1338 ap->cdb_len = (unsigned int) rc;
1339 ap->host->max_cmd_len = (unsigned char) ap->cdb_len;
1340
1341 /* print device info to dmesg */
1342 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
1343 ap->id, device,
1344 ata_mode_string(xfer_modes));
1345 }
1346
1347 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
1348 return;
1349
1350err_out_nosup:
1351 printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n",
1352 ap->id, device);
1353err_out:
1354 dev->class++; /* converts ATA_DEV_xxx into ATA_DEV_xxx_UNSUP */
1355 DPRINTK("EXIT, err\n");
1356}
1357
6f2f3812 1358
057ace5e 1359static inline u8 ata_dev_knobble(const struct ata_port *ap)
6f2f3812
BC
1360{
1361 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(ap->device->id)));
1362}
1363
1364/**
1365 * ata_dev_config - Run device specific handlers and check for
1366 * SATA->PATA bridges
8a60a071 1367 * @ap: Bus
6f2f3812
BC
1368 * @i: Device
1369 *
1370 * LOCKING:
1371 */
8a60a071 1372
6f2f3812
BC
1373void ata_dev_config(struct ata_port *ap, unsigned int i)
1374{
1375 /* limit bridge transfers to udma5, 200 sectors */
1376 if (ata_dev_knobble(ap)) {
1377 printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
1378 ap->id, ap->device->devno);
1379 ap->udma_mask &= ATA_UDMA5;
1380 ap->host->max_sectors = ATA_MAX_SECTORS;
1381 ap->host->hostt->max_sectors = ATA_MAX_SECTORS;
1382 ap->device->flags |= ATA_DFLAG_LOCK_SECTORS;
1383 }
1384
1385 if (ap->ops->dev_config)
1386 ap->ops->dev_config(ap, &ap->device[i]);
1387}
1388
1da177e4
LT
1389/**
1390 * ata_bus_probe - Reset and probe ATA bus
1391 * @ap: Bus to probe
1392 *
0cba632b
JG
1393 * Master ATA bus probing function. Initiates a hardware-dependent
1394 * bus reset, then attempts to identify any devices found on
1395 * the bus.
1396 *
1da177e4 1397 * LOCKING:
0cba632b 1398 * PCI/etc. bus probe sem.
1da177e4
LT
1399 *
1400 * RETURNS:
1401 * Zero on success, non-zero on error.
1402 */
1403
1404static int ata_bus_probe(struct ata_port *ap)
1405{
1406 unsigned int i, found = 0;
1407
1408 ap->ops->phy_reset(ap);
1409 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1410 goto err_out;
1411
1412 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1413 ata_dev_identify(ap, i);
1414 if (ata_dev_present(&ap->device[i])) {
1415 found = 1;
6f2f3812 1416 ata_dev_config(ap,i);
1da177e4
LT
1417 }
1418 }
1419
1420 if ((!found) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1421 goto err_out_disable;
1422
1423 ata_set_mode(ap);
1424 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1425 goto err_out_disable;
1426
1427 return 0;
1428
1429err_out_disable:
1430 ap->ops->port_disable(ap);
1431err_out:
1432 return -1;
1433}
1434
1435/**
0cba632b
JG
1436 * ata_port_probe - Mark port as enabled
1437 * @ap: Port for which we indicate enablement
1da177e4 1438 *
0cba632b
JG
1439 * Modify @ap data structure such that the system
1440 * thinks that the entire port is enabled.
1441 *
1442 * LOCKING: host_set lock, or some other form of
1443 * serialization.
1da177e4
LT
1444 */
1445
1446void ata_port_probe(struct ata_port *ap)
1447{
1448 ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1449}
1450
1451/**
780a87f7
JG
1452 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1453 * @ap: SATA port associated with target SATA PHY.
1da177e4 1454 *
780a87f7
JG
1455 * This function issues commands to standard SATA Sxxx
1456 * PHY registers, to wake up the phy (and device), and
1457 * clear any reset condition.
1da177e4
LT
1458 *
1459 * LOCKING:
0cba632b 1460 * PCI/etc. bus probe sem.
1da177e4
LT
1461 *
1462 */
1463void __sata_phy_reset(struct ata_port *ap)
1464{
1465 u32 sstatus;
1466 unsigned long timeout = jiffies + (HZ * 5);
1467
1468 if (ap->flags & ATA_FLAG_SATA_RESET) {
cdcca89e
BR
1469 /* issue phy wake/reset */
1470 scr_write_flush(ap, SCR_CONTROL, 0x301);
62ba2841
TH
1471 /* Couldn't find anything in SATA I/II specs, but
1472 * AHCI-1.1 10.4.2 says at least 1 ms. */
1473 mdelay(1);
1da177e4 1474 }
cdcca89e 1475 scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
1da177e4
LT
1476
1477 /* wait for phy to become ready, if necessary */
1478 do {
1479 msleep(200);
1480 sstatus = scr_read(ap, SCR_STATUS);
1481 if ((sstatus & 0xf) != 1)
1482 break;
1483 } while (time_before(jiffies, timeout));
1484
1485 /* TODO: phy layer with polling, timeouts, etc. */
1486 if (sata_dev_present(ap))
1487 ata_port_probe(ap);
1488 else {
1489 sstatus = scr_read(ap, SCR_STATUS);
1490 printk(KERN_INFO "ata%u: no device found (phy stat %08x)\n",
1491 ap->id, sstatus);
1492 ata_port_disable(ap);
1493 }
1494
1495 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1496 return;
1497
1498 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1499 ata_port_disable(ap);
1500 return;
1501 }
1502
1503 ap->cbl = ATA_CBL_SATA;
1504}
1505
1506/**
780a87f7
JG
1507 * sata_phy_reset - Reset SATA bus.
1508 * @ap: SATA port associated with target SATA PHY.
1da177e4 1509 *
780a87f7
JG
1510 * This function resets the SATA bus, and then probes
1511 * the bus for devices.
1da177e4
LT
1512 *
1513 * LOCKING:
0cba632b 1514 * PCI/etc. bus probe sem.
1da177e4
LT
1515 *
1516 */
1517void sata_phy_reset(struct ata_port *ap)
1518{
1519 __sata_phy_reset(ap);
1520 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1521 return;
1522 ata_bus_reset(ap);
1523}
1524
1525/**
780a87f7
JG
1526 * ata_port_disable - Disable port.
1527 * @ap: Port to be disabled.
1da177e4 1528 *
780a87f7
JG
1529 * Modify @ap data structure such that the system
1530 * thinks that the entire port is disabled, and should
1531 * never attempt to probe or communicate with devices
1532 * on this port.
1533 *
1534 * LOCKING: host_set lock, or some other form of
1535 * serialization.
1da177e4
LT
1536 */
1537
1538void ata_port_disable(struct ata_port *ap)
1539{
1540 ap->device[0].class = ATA_DEV_NONE;
1541 ap->device[1].class = ATA_DEV_NONE;
1542 ap->flags |= ATA_FLAG_PORT_DISABLED;
1543}
1544
452503f9
AC
1545/*
1546 * This mode timing computation functionality is ported over from
1547 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
1548 */
1549/*
1550 * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
1551 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
1552 * for PIO 5, which is a nonstandard extension and UDMA6, which
1553 * is currently supported only by Maxtor drives.
1554 */
1555
1556static const struct ata_timing ata_timing[] = {
1557
1558 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
1559 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
1560 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
1561 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
1562
1563 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
1564 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
1565 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
1566
1567/* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
1568
1569 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
1570 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
1571 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
1572
1573 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
1574 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
1575 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
1576
1577/* { XFER_PIO_5, 20, 50, 30, 100, 50, 30, 100, 0 }, */
1578 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
1579 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
1580
1581 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
1582 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
1583 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
1584
1585/* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
1586
1587 { 0xFF }
1588};
1589
1590#define ENOUGH(v,unit) (((v)-1)/(unit)+1)
1591#define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
1592
1593static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
1594{
1595 q->setup = EZ(t->setup * 1000, T);
1596 q->act8b = EZ(t->act8b * 1000, T);
1597 q->rec8b = EZ(t->rec8b * 1000, T);
1598 q->cyc8b = EZ(t->cyc8b * 1000, T);
1599 q->active = EZ(t->active * 1000, T);
1600 q->recover = EZ(t->recover * 1000, T);
1601 q->cycle = EZ(t->cycle * 1000, T);
1602 q->udma = EZ(t->udma * 1000, UT);
1603}
1604
1605void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
1606 struct ata_timing *m, unsigned int what)
1607{
1608 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
1609 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
1610 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
1611 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
1612 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
1613 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
1614 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
1615 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
1616}
1617
1618static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
1619{
1620 const struct ata_timing *t;
1621
1622 for (t = ata_timing; t->mode != speed; t++)
91190758 1623 if (t->mode == 0xFF)
452503f9
AC
1624 return NULL;
1625 return t;
1626}
1627
1628int ata_timing_compute(struct ata_device *adev, unsigned short speed,
1629 struct ata_timing *t, int T, int UT)
1630{
1631 const struct ata_timing *s;
1632 struct ata_timing p;
1633
1634 /*
1635 * Find the mode.
1636 */
1637
1638 if (!(s = ata_timing_find_mode(speed)))
1639 return -EINVAL;
1640
1641 /*
1642 * If the drive is an EIDE drive, it can tell us it needs extended
1643 * PIO/MW_DMA cycle timing.
1644 */
1645
1646 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
1647 memset(&p, 0, sizeof(p));
1648 if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
1649 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
1650 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
1651 } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
1652 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
1653 }
1654 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
1655 }
1656
1657 /*
1658 * Convert the timing to bus clock counts.
1659 */
1660
1661 ata_timing_quantize(s, t, T, UT);
1662
1663 /*
1664 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY, S.M.A.R.T
1665 * and some other commands. We have to ensure that the DMA cycle timing is
1666 * slower/equal than the fastest PIO timing.
1667 */
1668
1669 if (speed > XFER_PIO_4) {
1670 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
1671 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
1672 }
1673
1674 /*
1675 * Lenghten active & recovery time so that cycle time is correct.
1676 */
1677
1678 if (t->act8b + t->rec8b < t->cyc8b) {
1679 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
1680 t->rec8b = t->cyc8b - t->act8b;
1681 }
1682
1683 if (t->active + t->recover < t->cycle) {
1684 t->active += (t->cycle - (t->active + t->recover)) / 2;
1685 t->recover = t->cycle - t->active;
1686 }
1687
1688 return 0;
1689}
1690
057ace5e 1691static const struct {
1da177e4
LT
1692 unsigned int shift;
1693 u8 base;
1694} xfer_mode_classes[] = {
1695 { ATA_SHIFT_UDMA, XFER_UDMA_0 },
1696 { ATA_SHIFT_MWDMA, XFER_MW_DMA_0 },
1697 { ATA_SHIFT_PIO, XFER_PIO_0 },
1698};
1699
1700static inline u8 base_from_shift(unsigned int shift)
1701{
1702 int i;
1703
1704 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++)
1705 if (xfer_mode_classes[i].shift == shift)
1706 return xfer_mode_classes[i].base;
1707
1708 return 0xff;
1709}
1710
1711static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
1712{
1713 int ofs, idx;
1714 u8 base;
1715
1716 if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1717 return;
1718
1719 if (dev->xfer_shift == ATA_SHIFT_PIO)
1720 dev->flags |= ATA_DFLAG_PIO;
1721
1722 ata_dev_set_xfermode(ap, dev);
1723
1724 base = base_from_shift(dev->xfer_shift);
1725 ofs = dev->xfer_mode - base;
1726 idx = ofs + dev->xfer_shift;
1727 WARN_ON(idx >= ARRAY_SIZE(xfer_mode_str));
1728
1729 DPRINTK("idx=%d xfer_shift=%u, xfer_mode=0x%x, base=0x%x, offset=%d\n",
1730 idx, dev->xfer_shift, (int)dev->xfer_mode, (int)base, ofs);
1731
1732 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
1733 ap->id, dev->devno, xfer_mode_str[idx]);
1734}
1735
1736static int ata_host_set_pio(struct ata_port *ap)
1737{
1738 unsigned int mask;
1739 int x, i;
1740 u8 base, xfer_mode;
1741
1742 mask = ata_get_mode_mask(ap, ATA_SHIFT_PIO);
1743 x = fgb(mask);
1744 if (x < 0) {
1745 printk(KERN_WARNING "ata%u: no PIO support\n", ap->id);
1746 return -1;
1747 }
1748
1749 base = base_from_shift(ATA_SHIFT_PIO);
1750 xfer_mode = base + x;
1751
1752 DPRINTK("base 0x%x xfer_mode 0x%x mask 0x%x x %d\n",
1753 (int)base, (int)xfer_mode, mask, x);
1754
1755 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1756 struct ata_device *dev = &ap->device[i];
1757 if (ata_dev_present(dev)) {
1758 dev->pio_mode = xfer_mode;
1759 dev->xfer_mode = xfer_mode;
1760 dev->xfer_shift = ATA_SHIFT_PIO;
1761 if (ap->ops->set_piomode)
1762 ap->ops->set_piomode(ap, dev);
1763 }
1764 }
1765
1766 return 0;
1767}
1768
1769static void ata_host_set_dma(struct ata_port *ap, u8 xfer_mode,
1770 unsigned int xfer_shift)
1771{
1772 int i;
1773
1774 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1775 struct ata_device *dev = &ap->device[i];
1776 if (ata_dev_present(dev)) {
1777 dev->dma_mode = xfer_mode;
1778 dev->xfer_mode = xfer_mode;
1779 dev->xfer_shift = xfer_shift;
1780 if (ap->ops->set_dmamode)
1781 ap->ops->set_dmamode(ap, dev);
1782 }
1783 }
1784}
1785
1786/**
1787 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1788 * @ap: port on which timings will be programmed
1789 *
780a87f7
JG
1790 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1791 *
1da177e4 1792 * LOCKING:
0cba632b 1793 * PCI/etc. bus probe sem.
1da177e4
LT
1794 *
1795 */
1796static void ata_set_mode(struct ata_port *ap)
1797{
8cbd6df1 1798 unsigned int xfer_shift;
1da177e4
LT
1799 u8 xfer_mode;
1800 int rc;
1801
1802 /* step 1: always set host PIO timings */
1803 rc = ata_host_set_pio(ap);
1804 if (rc)
1805 goto err_out;
1806
1807 /* step 2: choose the best data xfer mode */
1808 xfer_mode = xfer_shift = 0;
1809 rc = ata_choose_xfer_mode(ap, &xfer_mode, &xfer_shift);
1810 if (rc)
1811 goto err_out;
1812
1813 /* step 3: if that xfer mode isn't PIO, set host DMA timings */
1814 if (xfer_shift != ATA_SHIFT_PIO)
1815 ata_host_set_dma(ap, xfer_mode, xfer_shift);
1816
1817 /* step 4: update devices' xfer mode */
1818 ata_dev_set_mode(ap, &ap->device[0]);
1819 ata_dev_set_mode(ap, &ap->device[1]);
1820
1821 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1822 return;
1823
1824 if (ap->ops->post_set_mode)
1825 ap->ops->post_set_mode(ap);
1826
1da177e4
LT
1827 return;
1828
1829err_out:
1830 ata_port_disable(ap);
1831}
1832
1833/**
1834 * ata_busy_sleep - sleep until BSY clears, or timeout
1835 * @ap: port containing status register to be polled
1836 * @tmout_pat: impatience timeout
1837 * @tmout: overall timeout
1838 *
780a87f7
JG
1839 * Sleep until ATA Status register bit BSY clears,
1840 * or a timeout occurs.
1841 *
1842 * LOCKING: None.
1da177e4
LT
1843 *
1844 */
1845
1846static unsigned int ata_busy_sleep (struct ata_port *ap,
1847 unsigned long tmout_pat,
1848 unsigned long tmout)
1849{
1850 unsigned long timer_start, timeout;
1851 u8 status;
1852
1853 status = ata_busy_wait(ap, ATA_BUSY, 300);
1854 timer_start = jiffies;
1855 timeout = timer_start + tmout_pat;
1856 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1857 msleep(50);
1858 status = ata_busy_wait(ap, ATA_BUSY, 3);
1859 }
1860
1861 if (status & ATA_BUSY)
1862 printk(KERN_WARNING "ata%u is slow to respond, "
1863 "please be patient\n", ap->id);
1864
1865 timeout = timer_start + tmout;
1866 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1867 msleep(50);
1868 status = ata_chk_status(ap);
1869 }
1870
1871 if (status & ATA_BUSY) {
1872 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
1873 ap->id, tmout / HZ);
1874 return 1;
1875 }
1876
1877 return 0;
1878}
1879
1880static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
1881{
1882 struct ata_ioports *ioaddr = &ap->ioaddr;
1883 unsigned int dev0 = devmask & (1 << 0);
1884 unsigned int dev1 = devmask & (1 << 1);
1885 unsigned long timeout;
1886
1887 /* if device 0 was found in ata_devchk, wait for its
1888 * BSY bit to clear
1889 */
1890 if (dev0)
1891 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1892
1893 /* if device 1 was found in ata_devchk, wait for
1894 * register access, then wait for BSY to clear
1895 */
1896 timeout = jiffies + ATA_TMOUT_BOOT;
1897 while (dev1) {
1898 u8 nsect, lbal;
1899
1900 ap->ops->dev_select(ap, 1);
1901 if (ap->flags & ATA_FLAG_MMIO) {
1902 nsect = readb((void __iomem *) ioaddr->nsect_addr);
1903 lbal = readb((void __iomem *) ioaddr->lbal_addr);
1904 } else {
1905 nsect = inb(ioaddr->nsect_addr);
1906 lbal = inb(ioaddr->lbal_addr);
1907 }
1908 if ((nsect == 1) && (lbal == 1))
1909 break;
1910 if (time_after(jiffies, timeout)) {
1911 dev1 = 0;
1912 break;
1913 }
1914 msleep(50); /* give drive a breather */
1915 }
1916 if (dev1)
1917 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1918
1919 /* is all this really necessary? */
1920 ap->ops->dev_select(ap, 0);
1921 if (dev1)
1922 ap->ops->dev_select(ap, 1);
1923 if (dev0)
1924 ap->ops->dev_select(ap, 0);
1925}
1926
1927/**
0cba632b
JG
1928 * ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
1929 * @ap: Port to reset and probe
1930 *
1931 * Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
1932 * probe the bus. Not often used these days.
1da177e4
LT
1933 *
1934 * LOCKING:
0cba632b 1935 * PCI/etc. bus probe sem.
1da177e4
LT
1936 *
1937 */
1938
1939static unsigned int ata_bus_edd(struct ata_port *ap)
1940{
1941 struct ata_taskfile tf;
1942
1943 /* set up execute-device-diag (bus reset) taskfile */
1944 /* also, take interrupts to a known state (disabled) */
1945 DPRINTK("execute-device-diag\n");
1946 ata_tf_init(ap, &tf, 0);
1947 tf.ctl |= ATA_NIEN;
1948 tf.command = ATA_CMD_EDD;
1949 tf.protocol = ATA_PROT_NODATA;
1950
1951 /* do bus reset */
1952 ata_tf_to_host(ap, &tf);
1953
1954 /* spec says at least 2ms. but who knows with those
1955 * crazy ATAPI devices...
1956 */
1957 msleep(150);
1958
1959 return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1960}
1961
1962static unsigned int ata_bus_softreset(struct ata_port *ap,
1963 unsigned int devmask)
1964{
1965 struct ata_ioports *ioaddr = &ap->ioaddr;
1966
1967 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
1968
1969 /* software reset. causes dev0 to be selected */
1970 if (ap->flags & ATA_FLAG_MMIO) {
1971 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1972 udelay(20); /* FIXME: flush */
1973 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
1974 udelay(20); /* FIXME: flush */
1975 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1976 } else {
1977 outb(ap->ctl, ioaddr->ctl_addr);
1978 udelay(10);
1979 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
1980 udelay(10);
1981 outb(ap->ctl, ioaddr->ctl_addr);
1982 }
1983
1984 /* spec mandates ">= 2ms" before checking status.
1985 * We wait 150ms, because that was the magic delay used for
1986 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
1987 * between when the ATA command register is written, and then
1988 * status is checked. Because waiting for "a while" before
1989 * checking status is fine, post SRST, we perform this magic
1990 * delay here as well.
1991 */
1992 msleep(150);
1993
1994 ata_bus_post_reset(ap, devmask);
1995
1996 return 0;
1997}
1998
1999/**
2000 * ata_bus_reset - reset host port and associated ATA channel
2001 * @ap: port to reset
2002 *
2003 * This is typically the first time we actually start issuing
2004 * commands to the ATA channel. We wait for BSY to clear, then
2005 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
2006 * result. Determine what devices, if any, are on the channel
2007 * by looking at the device 0/1 error register. Look at the signature
2008 * stored in each device's taskfile registers, to determine if
2009 * the device is ATA or ATAPI.
2010 *
2011 * LOCKING:
0cba632b
JG
2012 * PCI/etc. bus probe sem.
2013 * Obtains host_set lock.
1da177e4
LT
2014 *
2015 * SIDE EFFECTS:
2016 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
2017 */
2018
2019void ata_bus_reset(struct ata_port *ap)
2020{
2021 struct ata_ioports *ioaddr = &ap->ioaddr;
2022 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2023 u8 err;
2024 unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
2025
2026 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
2027
2028 /* determine if device 0/1 are present */
2029 if (ap->flags & ATA_FLAG_SATA_RESET)
2030 dev0 = 1;
2031 else {
2032 dev0 = ata_devchk(ap, 0);
2033 if (slave_possible)
2034 dev1 = ata_devchk(ap, 1);
2035 }
2036
2037 if (dev0)
2038 devmask |= (1 << 0);
2039 if (dev1)
2040 devmask |= (1 << 1);
2041
2042 /* select device 0 again */
2043 ap->ops->dev_select(ap, 0);
2044
2045 /* issue bus reset */
2046 if (ap->flags & ATA_FLAG_SRST)
2047 rc = ata_bus_softreset(ap, devmask);
2048 else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
2049 /* set up device control */
2050 if (ap->flags & ATA_FLAG_MMIO)
2051 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2052 else
2053 outb(ap->ctl, ioaddr->ctl_addr);
2054 rc = ata_bus_edd(ap);
2055 }
2056
2057 if (rc)
2058 goto err_out;
2059
2060 /*
2061 * determine by signature whether we have ATA or ATAPI devices
2062 */
2063 err = ata_dev_try_classify(ap, 0);
2064 if ((slave_possible) && (err != 0x81))
2065 ata_dev_try_classify(ap, 1);
2066
2067 /* re-enable interrupts */
2068 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2069 ata_irq_on(ap);
2070
2071 /* is double-select really necessary? */
2072 if (ap->device[1].class != ATA_DEV_NONE)
2073 ap->ops->dev_select(ap, 1);
2074 if (ap->device[0].class != ATA_DEV_NONE)
2075 ap->ops->dev_select(ap, 0);
2076
2077 /* if no devices were detected, disable this port */
2078 if ((ap->device[0].class == ATA_DEV_NONE) &&
2079 (ap->device[1].class == ATA_DEV_NONE))
2080 goto err_out;
2081
2082 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
2083 /* set up device control for ATA_FLAG_SATA_RESET */
2084 if (ap->flags & ATA_FLAG_MMIO)
2085 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2086 else
2087 outb(ap->ctl, ioaddr->ctl_addr);
2088 }
2089
2090 DPRINTK("EXIT\n");
2091 return;
2092
2093err_out:
2094 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
2095 ap->ops->port_disable(ap);
2096
2097 DPRINTK("EXIT\n");
2098}
2099
057ace5e
JG
2100static void ata_pr_blacklisted(const struct ata_port *ap,
2101 const struct ata_device *dev)
1da177e4
LT
2102{
2103 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, disabling DMA\n",
2104 ap->id, dev->devno);
2105}
2106
2107static const char * ata_dma_blacklist [] = {
2108 "WDC AC11000H",
2109 "WDC AC22100H",
2110 "WDC AC32500H",
2111 "WDC AC33100H",
2112 "WDC AC31600H",
2113 "WDC AC32100H",
2114 "WDC AC23200L",
2115 "Compaq CRD-8241B",
2116 "CRD-8400B",
2117 "CRD-8480B",
2118 "CRD-8482B",
2119 "CRD-84",
2120 "SanDisk SDP3B",
2121 "SanDisk SDP3B-64",
2122 "SANYO CD-ROM CRD",
2123 "HITACHI CDR-8",
2124 "HITACHI CDR-8335",
2125 "HITACHI CDR-8435",
2126 "Toshiba CD-ROM XM-6202B",
e922256a 2127 "TOSHIBA CD-ROM XM-1702BC",
1da177e4
LT
2128 "CD-532E-A",
2129 "E-IDE CD-ROM CR-840",
2130 "CD-ROM Drive/F5A",
2131 "WPI CDD-820",
2132 "SAMSUNG CD-ROM SC-148C",
2133 "SAMSUNG CD-ROM SC",
2134 "SanDisk SDP3B-64",
1da177e4
LT
2135 "ATAPI CD-ROM DRIVE 40X MAXIMUM",
2136 "_NEC DV5800A",
2137};
2138
057ace5e 2139static int ata_dma_blacklisted(const struct ata_device *dev)
1da177e4
LT
2140{
2141 unsigned char model_num[40];
2142 char *s;
2143 unsigned int len;
2144 int i;
2145
2146 ata_dev_id_string(dev->id, model_num, ATA_ID_PROD_OFS,
2147 sizeof(model_num));
2148 s = &model_num[0];
2149 len = strnlen(s, sizeof(model_num));
2150
2151 /* ATAPI specifies that empty space is blank-filled; remove blanks */
2152 while ((len > 0) && (s[len - 1] == ' ')) {
2153 len--;
2154 s[len] = 0;
2155 }
2156
2157 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i++)
2158 if (!strncmp(ata_dma_blacklist[i], s, len))
2159 return 1;
2160
2161 return 0;
2162}
2163
057ace5e 2164static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift)
1da177e4 2165{
057ace5e 2166 const struct ata_device *master, *slave;
1da177e4
LT
2167 unsigned int mask;
2168
2169 master = &ap->device[0];
2170 slave = &ap->device[1];
2171
2172 assert (ata_dev_present(master) || ata_dev_present(slave));
2173
2174 if (shift == ATA_SHIFT_UDMA) {
2175 mask = ap->udma_mask;
2176 if (ata_dev_present(master)) {
2177 mask &= (master->id[ATA_ID_UDMA_MODES] & 0xff);
057ace5e 2178 if (ata_dma_blacklisted(master)) {
1da177e4
LT
2179 mask = 0;
2180 ata_pr_blacklisted(ap, master);
2181 }
2182 }
2183 if (ata_dev_present(slave)) {
2184 mask &= (slave->id[ATA_ID_UDMA_MODES] & 0xff);
057ace5e 2185 if (ata_dma_blacklisted(slave)) {
1da177e4
LT
2186 mask = 0;
2187 ata_pr_blacklisted(ap, slave);
2188 }
2189 }
2190 }
2191 else if (shift == ATA_SHIFT_MWDMA) {
2192 mask = ap->mwdma_mask;
2193 if (ata_dev_present(master)) {
2194 mask &= (master->id[ATA_ID_MWDMA_MODES] & 0x07);
057ace5e 2195 if (ata_dma_blacklisted(master)) {
1da177e4
LT
2196 mask = 0;
2197 ata_pr_blacklisted(ap, master);
2198 }
2199 }
2200 if (ata_dev_present(slave)) {
2201 mask &= (slave->id[ATA_ID_MWDMA_MODES] & 0x07);
057ace5e 2202 if (ata_dma_blacklisted(slave)) {
1da177e4
LT
2203 mask = 0;
2204 ata_pr_blacklisted(ap, slave);
2205 }
2206 }
2207 }
2208 else if (shift == ATA_SHIFT_PIO) {
2209 mask = ap->pio_mask;
2210 if (ata_dev_present(master)) {
2211 /* spec doesn't return explicit support for
2212 * PIO0-2, so we fake it
2213 */
2214 u16 tmp_mode = master->id[ATA_ID_PIO_MODES] & 0x03;
2215 tmp_mode <<= 3;
2216 tmp_mode |= 0x7;
2217 mask &= tmp_mode;
2218 }
2219 if (ata_dev_present(slave)) {
2220 /* spec doesn't return explicit support for
2221 * PIO0-2, so we fake it
2222 */
2223 u16 tmp_mode = slave->id[ATA_ID_PIO_MODES] & 0x03;
2224 tmp_mode <<= 3;
2225 tmp_mode |= 0x7;
2226 mask &= tmp_mode;
2227 }
2228 }
2229 else {
2230 mask = 0xffffffff; /* shut up compiler warning */
2231 BUG();
2232 }
2233
2234 return mask;
2235}
2236
2237/* find greatest bit */
2238static int fgb(u32 bitmap)
2239{
2240 unsigned int i;
2241 int x = -1;
2242
2243 for (i = 0; i < 32; i++)
2244 if (bitmap & (1 << i))
2245 x = i;
2246
2247 return x;
2248}
2249
2250/**
2251 * ata_choose_xfer_mode - attempt to find best transfer mode
2252 * @ap: Port for which an xfer mode will be selected
2253 * @xfer_mode_out: (output) SET FEATURES - XFER MODE code
2254 * @xfer_shift_out: (output) bit shift that selects this mode
2255 *
0cba632b
JG
2256 * Based on host and device capabilities, determine the
2257 * maximum transfer mode that is amenable to all.
2258 *
1da177e4 2259 * LOCKING:
0cba632b 2260 * PCI/etc. bus probe sem.
1da177e4
LT
2261 *
2262 * RETURNS:
2263 * Zero on success, negative on error.
2264 */
2265
057ace5e 2266static int ata_choose_xfer_mode(const struct ata_port *ap,
1da177e4
LT
2267 u8 *xfer_mode_out,
2268 unsigned int *xfer_shift_out)
2269{
2270 unsigned int mask, shift;
2271 int x, i;
2272
2273 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++) {
2274 shift = xfer_mode_classes[i].shift;
2275 mask = ata_get_mode_mask(ap, shift);
2276
2277 x = fgb(mask);
2278 if (x >= 0) {
2279 *xfer_mode_out = xfer_mode_classes[i].base + x;
2280 *xfer_shift_out = shift;
2281 return 0;
2282 }
2283 }
2284
2285 return -1;
2286}
2287
2288/**
2289 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2290 * @ap: Port associated with device @dev
2291 * @dev: Device to which command will be sent
2292 *
780a87f7
JG
2293 * Issue SET FEATURES - XFER MODE command to device @dev
2294 * on port @ap.
2295 *
1da177e4 2296 * LOCKING:
0cba632b 2297 * PCI/etc. bus probe sem.
1da177e4
LT
2298 */
2299
2300static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
2301{
2302 DECLARE_COMPLETION(wait);
2303 struct ata_queued_cmd *qc;
2304 int rc;
2305 unsigned long flags;
2306
2307 /* set up set-features taskfile */
2308 DPRINTK("set features - xfer mode\n");
2309
2310 qc = ata_qc_new_init(ap, dev);
2311 BUG_ON(qc == NULL);
2312
2313 qc->tf.command = ATA_CMD_SET_FEATURES;
2314 qc->tf.feature = SETFEATURES_XFER;
2315 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2316 qc->tf.protocol = ATA_PROT_NODATA;
2317 qc->tf.nsect = dev->xfer_mode;
2318
2319 qc->waiting = &wait;
2320 qc->complete_fn = ata_qc_complete_noop;
2321
2322 spin_lock_irqsave(&ap->host_set->lock, flags);
2323 rc = ata_qc_issue(qc);
2324 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2325
2326 if (rc)
2327 ata_port_disable(ap);
2328 else
2329 wait_for_completion(&wait);
2330
2331 DPRINTK("EXIT\n");
2332}
2333
59a10b17
AL
2334/**
2335 * ata_dev_reread_id - Reread the device identify device info
2336 * @ap: port where the device is
2337 * @dev: device to reread the identify device info
2338 *
2339 * LOCKING:
2340 */
2341
2342static void ata_dev_reread_id(struct ata_port *ap, struct ata_device *dev)
2343{
2344 DECLARE_COMPLETION(wait);
2345 struct ata_queued_cmd *qc;
2346 unsigned long flags;
2347 int rc;
2348
2349 qc = ata_qc_new_init(ap, dev);
2350 BUG_ON(qc == NULL);
2351
2352 ata_sg_init_one(qc, dev->id, sizeof(dev->id));
2353 qc->dma_dir = DMA_FROM_DEVICE;
2354
2355 if (dev->class == ATA_DEV_ATA) {
2356 qc->tf.command = ATA_CMD_ID_ATA;
2357 DPRINTK("do ATA identify\n");
2358 } else {
2359 qc->tf.command = ATA_CMD_ID_ATAPI;
2360 DPRINTK("do ATAPI identify\n");
2361 }
2362
2363 qc->tf.flags |= ATA_TFLAG_DEVICE;
2364 qc->tf.protocol = ATA_PROT_PIO;
2365 qc->nsect = 1;
2366
2367 qc->waiting = &wait;
2368 qc->complete_fn = ata_qc_complete_noop;
2369
2370 spin_lock_irqsave(&ap->host_set->lock, flags);
2371 rc = ata_qc_issue(qc);
2372 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2373
2374 if (rc)
2375 goto err_out;
2376
2377 wait_for_completion(&wait);
2378
2379 swap_buf_le16(dev->id, ATA_ID_WORDS);
2380
2381 ata_dump_id(dev);
2382
2383 DPRINTK("EXIT\n");
2384
2385 return;
2386err_out:
2387 ata_port_disable(ap);
2388}
2389
8bf62ece
AL
2390/**
2391 * ata_dev_init_params - Issue INIT DEV PARAMS command
2392 * @ap: Port associated with device @dev
2393 * @dev: Device to which command will be sent
2394 *
2395 * LOCKING:
2396 */
2397
2398static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev)
2399{
2400 DECLARE_COMPLETION(wait);
2401 struct ata_queued_cmd *qc;
2402 int rc;
2403 unsigned long flags;
2404 u16 sectors = dev->id[6];
2405 u16 heads = dev->id[3];
2406
2407 /* Number of sectors per track 1-255. Number of heads 1-16 */
2408 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
2409 return;
2410
2411 /* set up init dev params taskfile */
2412 DPRINTK("init dev params \n");
2413
2414 qc = ata_qc_new_init(ap, dev);
2415 BUG_ON(qc == NULL);
2416
2417 qc->tf.command = ATA_CMD_INIT_DEV_PARAMS;
2418 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2419 qc->tf.protocol = ATA_PROT_NODATA;
2420 qc->tf.nsect = sectors;
2421 qc->tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
2422
2423 qc->waiting = &wait;
2424 qc->complete_fn = ata_qc_complete_noop;
2425
2426 spin_lock_irqsave(&ap->host_set->lock, flags);
2427 rc = ata_qc_issue(qc);
2428 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2429
2430 if (rc)
2431 ata_port_disable(ap);
2432 else
2433 wait_for_completion(&wait);
2434
2435 DPRINTK("EXIT\n");
2436}
2437
1da177e4 2438/**
0cba632b
JG
2439 * ata_sg_clean - Unmap DMA memory associated with command
2440 * @qc: Command containing DMA memory to be released
2441 *
2442 * Unmap all mapped DMA memory associated with this command.
1da177e4
LT
2443 *
2444 * LOCKING:
0cba632b 2445 * spin_lock_irqsave(host_set lock)
1da177e4
LT
2446 */
2447
2448static void ata_sg_clean(struct ata_queued_cmd *qc)
2449{
2450 struct ata_port *ap = qc->ap;
2451 struct scatterlist *sg = qc->sg;
2452 int dir = qc->dma_dir;
2453
2454 assert(qc->flags & ATA_QCFLAG_DMAMAP);
2455 assert(sg != NULL);
2456
2457 if (qc->flags & ATA_QCFLAG_SINGLE)
2458 assert(qc->n_elem == 1);
2459
2460 DPRINTK("unmapping %u sg elements\n", qc->n_elem);
2461
2462 if (qc->flags & ATA_QCFLAG_SG)
2463 dma_unmap_sg(ap->host_set->dev, sg, qc->n_elem, dir);
2464 else
2465 dma_unmap_single(ap->host_set->dev, sg_dma_address(&sg[0]),
2466 sg_dma_len(&sg[0]), dir);
2467
2468 qc->flags &= ~ATA_QCFLAG_DMAMAP;
2469 qc->sg = NULL;
2470}
2471
2472/**
2473 * ata_fill_sg - Fill PCI IDE PRD table
2474 * @qc: Metadata associated with taskfile to be transferred
2475 *
780a87f7
JG
2476 * Fill PCI IDE PRD (scatter-gather) table with segments
2477 * associated with the current disk command.
2478 *
1da177e4 2479 * LOCKING:
780a87f7 2480 * spin_lock_irqsave(host_set lock)
1da177e4
LT
2481 *
2482 */
2483static void ata_fill_sg(struct ata_queued_cmd *qc)
2484{
2485 struct scatterlist *sg = qc->sg;
2486 struct ata_port *ap = qc->ap;
2487 unsigned int idx, nelem;
2488
2489 assert(sg != NULL);
2490 assert(qc->n_elem > 0);
2491
2492 idx = 0;
2493 for (nelem = qc->n_elem; nelem; nelem--,sg++) {
2494 u32 addr, offset;
2495 u32 sg_len, len;
2496
2497 /* determine if physical DMA addr spans 64K boundary.
2498 * Note h/w doesn't support 64-bit, so we unconditionally
2499 * truncate dma_addr_t to u32.
2500 */
2501 addr = (u32) sg_dma_address(sg);
2502 sg_len = sg_dma_len(sg);
2503
2504 while (sg_len) {
2505 offset = addr & 0xffff;
2506 len = sg_len;
2507 if ((offset + sg_len) > 0x10000)
2508 len = 0x10000 - offset;
2509
2510 ap->prd[idx].addr = cpu_to_le32(addr);
2511 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
2512 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
2513
2514 idx++;
2515 sg_len -= len;
2516 addr += len;
2517 }
2518 }
2519
2520 if (idx)
2521 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2522}
2523/**
2524 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2525 * @qc: Metadata associated with taskfile to check
2526 *
780a87f7
JG
2527 * Allow low-level driver to filter ATA PACKET commands, returning
2528 * a status indicating whether or not it is OK to use DMA for the
2529 * supplied PACKET command.
2530 *
1da177e4 2531 * LOCKING:
0cba632b
JG
2532 * spin_lock_irqsave(host_set lock)
2533 *
1da177e4
LT
2534 * RETURNS: 0 when ATAPI DMA can be used
2535 * nonzero otherwise
2536 */
2537int ata_check_atapi_dma(struct ata_queued_cmd *qc)
2538{
2539 struct ata_port *ap = qc->ap;
2540 int rc = 0; /* Assume ATAPI DMA is OK by default */
2541
2542 if (ap->ops->check_atapi_dma)
2543 rc = ap->ops->check_atapi_dma(qc);
2544
2545 return rc;
2546}
2547/**
2548 * ata_qc_prep - Prepare taskfile for submission
2549 * @qc: Metadata associated with taskfile to be prepared
2550 *
780a87f7
JG
2551 * Prepare ATA taskfile for submission.
2552 *
1da177e4
LT
2553 * LOCKING:
2554 * spin_lock_irqsave(host_set lock)
2555 */
2556void ata_qc_prep(struct ata_queued_cmd *qc)
2557{
2558 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2559 return;
2560
2561 ata_fill_sg(qc);
2562}
2563
0cba632b
JG
2564/**
2565 * ata_sg_init_one - Associate command with memory buffer
2566 * @qc: Command to be associated
2567 * @buf: Memory buffer
2568 * @buflen: Length of memory buffer, in bytes.
2569 *
2570 * Initialize the data-related elements of queued_cmd @qc
2571 * to point to a single memory buffer, @buf of byte length @buflen.
2572 *
2573 * LOCKING:
2574 * spin_lock_irqsave(host_set lock)
2575 */
2576
1da177e4
LT
2577void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
2578{
2579 struct scatterlist *sg;
2580
2581 qc->flags |= ATA_QCFLAG_SINGLE;
2582
2583 memset(&qc->sgent, 0, sizeof(qc->sgent));
2584 qc->sg = &qc->sgent;
2585 qc->n_elem = 1;
2586 qc->buf_virt = buf;
2587
2588 sg = qc->sg;
2589 sg->page = virt_to_page(buf);
2590 sg->offset = (unsigned long) buf & ~PAGE_MASK;
32529e01 2591 sg->length = buflen;
1da177e4
LT
2592}
2593
0cba632b
JG
2594/**
2595 * ata_sg_init - Associate command with scatter-gather table.
2596 * @qc: Command to be associated
2597 * @sg: Scatter-gather table.
2598 * @n_elem: Number of elements in s/g table.
2599 *
2600 * Initialize the data-related elements of queued_cmd @qc
2601 * to point to a scatter-gather table @sg, containing @n_elem
2602 * elements.
2603 *
2604 * LOCKING:
2605 * spin_lock_irqsave(host_set lock)
2606 */
2607
1da177e4
LT
2608void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
2609 unsigned int n_elem)
2610{
2611 qc->flags |= ATA_QCFLAG_SG;
2612 qc->sg = sg;
2613 qc->n_elem = n_elem;
2614}
2615
2616/**
0cba632b
JG
2617 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
2618 * @qc: Command with memory buffer to be mapped.
2619 *
2620 * DMA-map the memory buffer associated with queued_cmd @qc.
1da177e4
LT
2621 *
2622 * LOCKING:
2623 * spin_lock_irqsave(host_set lock)
2624 *
2625 * RETURNS:
0cba632b 2626 * Zero on success, negative on error.
1da177e4
LT
2627 */
2628
2629static int ata_sg_setup_one(struct ata_queued_cmd *qc)
2630{
2631 struct ata_port *ap = qc->ap;
2632 int dir = qc->dma_dir;
2633 struct scatterlist *sg = qc->sg;
2634 dma_addr_t dma_address;
2635
2636 dma_address = dma_map_single(ap->host_set->dev, qc->buf_virt,
32529e01 2637 sg->length, dir);
1da177e4
LT
2638 if (dma_mapping_error(dma_address))
2639 return -1;
2640
2641 sg_dma_address(sg) = dma_address;
32529e01 2642 sg_dma_len(sg) = sg->length;
1da177e4
LT
2643
2644 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
2645 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2646
2647 return 0;
2648}
2649
2650/**
0cba632b
JG
2651 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
2652 * @qc: Command with scatter-gather table to be mapped.
2653 *
2654 * DMA-map the scatter-gather table associated with queued_cmd @qc.
1da177e4
LT
2655 *
2656 * LOCKING:
2657 * spin_lock_irqsave(host_set lock)
2658 *
2659 * RETURNS:
0cba632b 2660 * Zero on success, negative on error.
1da177e4
LT
2661 *
2662 */
2663
2664static int ata_sg_setup(struct ata_queued_cmd *qc)
2665{
2666 struct ata_port *ap = qc->ap;
2667 struct scatterlist *sg = qc->sg;
2668 int n_elem, dir;
2669
2670 VPRINTK("ENTER, ata%u\n", ap->id);
2671 assert(qc->flags & ATA_QCFLAG_SG);
2672
2673 dir = qc->dma_dir;
2674 n_elem = dma_map_sg(ap->host_set->dev, sg, qc->n_elem, dir);
2675 if (n_elem < 1)
2676 return -1;
2677
2678 DPRINTK("%d sg elements mapped\n", n_elem);
2679
2680 qc->n_elem = n_elem;
2681
2682 return 0;
2683}
2684
40e8c82c
TH
2685/**
2686 * ata_poll_qc_complete - turn irq back on and finish qc
2687 * @qc: Command to complete
2688 * @drv_stat: ATA status register content
2689 *
2690 * LOCKING:
2691 * None. (grabs host lock)
2692 */
2693
2694void ata_poll_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
2695{
2696 struct ata_port *ap = qc->ap;
b8f6153e 2697 unsigned long flags;
40e8c82c 2698
b8f6153e 2699 spin_lock_irqsave(&ap->host_set->lock, flags);
40e8c82c
TH
2700 ap->flags &= ~ATA_FLAG_NOINTR;
2701 ata_irq_on(ap);
2702 ata_qc_complete(qc, drv_stat);
b8f6153e 2703 spin_unlock_irqrestore(&ap->host_set->lock, flags);
40e8c82c
TH
2704}
2705
1da177e4
LT
2706/**
2707 * ata_pio_poll -
6f0ef4fa 2708 * @ap: the target ata_port
1da177e4
LT
2709 *
2710 * LOCKING:
0cba632b 2711 * None. (executing in kernel thread context)
1da177e4
LT
2712 *
2713 * RETURNS:
6f0ef4fa 2714 * timeout value to use
1da177e4
LT
2715 */
2716
2717static unsigned long ata_pio_poll(struct ata_port *ap)
2718{
2719 u8 status;
14be71f4
AL
2720 unsigned int poll_state = HSM_ST_UNKNOWN;
2721 unsigned int reg_state = HSM_ST_UNKNOWN;
2722 const unsigned int tmout_state = HSM_ST_TMOUT;
2723
2724 switch (ap->hsm_task_state) {
2725 case HSM_ST:
2726 case HSM_ST_POLL:
2727 poll_state = HSM_ST_POLL;
2728 reg_state = HSM_ST;
1da177e4 2729 break;
14be71f4
AL
2730 case HSM_ST_LAST:
2731 case HSM_ST_LAST_POLL:
2732 poll_state = HSM_ST_LAST_POLL;
2733 reg_state = HSM_ST_LAST;
1da177e4
LT
2734 break;
2735 default:
2736 BUG();
2737 break;
2738 }
2739
2740 status = ata_chk_status(ap);
2741 if (status & ATA_BUSY) {
2742 if (time_after(jiffies, ap->pio_task_timeout)) {
14be71f4 2743 ap->hsm_task_state = tmout_state;
1da177e4
LT
2744 return 0;
2745 }
14be71f4 2746 ap->hsm_task_state = poll_state;
1da177e4
LT
2747 return ATA_SHORT_PAUSE;
2748 }
2749
14be71f4 2750 ap->hsm_task_state = reg_state;
1da177e4
LT
2751 return 0;
2752}
2753
2754/**
6f0ef4fa
RD
2755 * ata_pio_complete - check if drive is busy or idle
2756 * @ap: the target ata_port
1da177e4
LT
2757 *
2758 * LOCKING:
0cba632b 2759 * None. (executing in kernel thread context)
7fb6ec28
JG
2760 *
2761 * RETURNS:
2762 * Non-zero if qc completed, zero otherwise.
1da177e4
LT
2763 */
2764
7fb6ec28 2765static int ata_pio_complete (struct ata_port *ap)
1da177e4
LT
2766{
2767 struct ata_queued_cmd *qc;
2768 u8 drv_stat;
2769
2770 /*
31433ea3
AC
2771 * This is purely heuristic. This is a fast path. Sometimes when
2772 * we enter, BSY will be cleared in a chk-status or two. If not,
2773 * the drive is probably seeking or something. Snooze for a couple
2774 * msecs, then chk-status again. If still busy, fall back to
14be71f4 2775 * HSM_ST_POLL state.
1da177e4
LT
2776 */
2777 drv_stat = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 10);
2778 if (drv_stat & (ATA_BUSY | ATA_DRQ)) {
2779 msleep(2);
2780 drv_stat = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 10);
2781 if (drv_stat & (ATA_BUSY | ATA_DRQ)) {
14be71f4 2782 ap->hsm_task_state = HSM_ST_LAST_POLL;
1da177e4 2783 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
7fb6ec28 2784 return 0;
1da177e4
LT
2785 }
2786 }
2787
2788 drv_stat = ata_wait_idle(ap);
2789 if (!ata_ok(drv_stat)) {
14be71f4 2790 ap->hsm_task_state = HSM_ST_ERR;
7fb6ec28 2791 return 0;
1da177e4
LT
2792 }
2793
2794 qc = ata_qc_from_tag(ap, ap->active_tag);
2795 assert(qc != NULL);
2796
14be71f4 2797 ap->hsm_task_state = HSM_ST_IDLE;
1da177e4 2798
40e8c82c 2799 ata_poll_qc_complete(qc, drv_stat);
7fb6ec28
JG
2800
2801 /* another command may start at this point */
2802
2803 return 1;
1da177e4
LT
2804}
2805
0baab86b
EF
2806
2807/**
6f0ef4fa 2808 * swap_buf_le16 - swap halves of 16-words in place
0baab86b
EF
2809 * @buf: Buffer to swap
2810 * @buf_words: Number of 16-bit words in buffer.
2811 *
2812 * Swap halves of 16-bit words if needed to convert from
2813 * little-endian byte order to native cpu byte order, or
2814 * vice-versa.
2815 *
2816 * LOCKING:
6f0ef4fa 2817 * Inherited from caller.
0baab86b 2818 */
1da177e4
LT
2819void swap_buf_le16(u16 *buf, unsigned int buf_words)
2820{
2821#ifdef __BIG_ENDIAN
2822 unsigned int i;
2823
2824 for (i = 0; i < buf_words; i++)
2825 buf[i] = le16_to_cpu(buf[i]);
2826#endif /* __BIG_ENDIAN */
2827}
2828
6ae4cfb5
AL
2829/**
2830 * ata_mmio_data_xfer - Transfer data by MMIO
2831 * @ap: port to read/write
2832 * @buf: data buffer
2833 * @buflen: buffer length
344babaa 2834 * @write_data: read/write
6ae4cfb5
AL
2835 *
2836 * Transfer data from/to the device data register by MMIO.
2837 *
2838 * LOCKING:
2839 * Inherited from caller.
6ae4cfb5
AL
2840 */
2841
1da177e4
LT
2842static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
2843 unsigned int buflen, int write_data)
2844{
2845 unsigned int i;
2846 unsigned int words = buflen >> 1;
2847 u16 *buf16 = (u16 *) buf;
2848 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
2849
6ae4cfb5 2850 /* Transfer multiple of 2 bytes */
1da177e4
LT
2851 if (write_data) {
2852 for (i = 0; i < words; i++)
2853 writew(le16_to_cpu(buf16[i]), mmio);
2854 } else {
2855 for (i = 0; i < words; i++)
2856 buf16[i] = cpu_to_le16(readw(mmio));
2857 }
6ae4cfb5
AL
2858
2859 /* Transfer trailing 1 byte, if any. */
2860 if (unlikely(buflen & 0x01)) {
2861 u16 align_buf[1] = { 0 };
2862 unsigned char *trailing_buf = buf + buflen - 1;
2863
2864 if (write_data) {
2865 memcpy(align_buf, trailing_buf, 1);
2866 writew(le16_to_cpu(align_buf[0]), mmio);
2867 } else {
2868 align_buf[0] = cpu_to_le16(readw(mmio));
2869 memcpy(trailing_buf, align_buf, 1);
2870 }
2871 }
1da177e4
LT
2872}
2873
6ae4cfb5
AL
2874/**
2875 * ata_pio_data_xfer - Transfer data by PIO
2876 * @ap: port to read/write
2877 * @buf: data buffer
2878 * @buflen: buffer length
344babaa 2879 * @write_data: read/write
6ae4cfb5
AL
2880 *
2881 * Transfer data from/to the device data register by PIO.
2882 *
2883 * LOCKING:
2884 * Inherited from caller.
6ae4cfb5
AL
2885 */
2886
1da177e4
LT
2887static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
2888 unsigned int buflen, int write_data)
2889{
6ae4cfb5 2890 unsigned int words = buflen >> 1;
1da177e4 2891
6ae4cfb5 2892 /* Transfer multiple of 2 bytes */
1da177e4 2893 if (write_data)
6ae4cfb5 2894 outsw(ap->ioaddr.data_addr, buf, words);
1da177e4 2895 else
6ae4cfb5
AL
2896 insw(ap->ioaddr.data_addr, buf, words);
2897
2898 /* Transfer trailing 1 byte, if any. */
2899 if (unlikely(buflen & 0x01)) {
2900 u16 align_buf[1] = { 0 };
2901 unsigned char *trailing_buf = buf + buflen - 1;
2902
2903 if (write_data) {
2904 memcpy(align_buf, trailing_buf, 1);
2905 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
2906 } else {
2907 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
2908 memcpy(trailing_buf, align_buf, 1);
2909 }
2910 }
1da177e4
LT
2911}
2912
6ae4cfb5
AL
2913/**
2914 * ata_data_xfer - Transfer data from/to the data register.
2915 * @ap: port to read/write
2916 * @buf: data buffer
2917 * @buflen: buffer length
2918 * @do_write: read/write
2919 *
2920 * Transfer data from/to the device data register.
2921 *
2922 * LOCKING:
2923 * Inherited from caller.
6ae4cfb5
AL
2924 */
2925
1da177e4
LT
2926static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
2927 unsigned int buflen, int do_write)
2928{
2929 if (ap->flags & ATA_FLAG_MMIO)
2930 ata_mmio_data_xfer(ap, buf, buflen, do_write);
2931 else
2932 ata_pio_data_xfer(ap, buf, buflen, do_write);
2933}
2934
6ae4cfb5
AL
2935/**
2936 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
2937 * @qc: Command on going
2938 *
2939 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
2940 *
2941 * LOCKING:
2942 * Inherited from caller.
2943 */
2944
1da177e4
LT
2945static void ata_pio_sector(struct ata_queued_cmd *qc)
2946{
2947 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
2948 struct scatterlist *sg = qc->sg;
2949 struct ata_port *ap = qc->ap;
2950 struct page *page;
2951 unsigned int offset;
2952 unsigned char *buf;
2953
2954 if (qc->cursect == (qc->nsect - 1))
14be71f4 2955 ap->hsm_task_state = HSM_ST_LAST;
1da177e4
LT
2956
2957 page = sg[qc->cursg].page;
2958 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
2959
2960 /* get the current page and offset */
2961 page = nth_page(page, (offset >> PAGE_SHIFT));
2962 offset %= PAGE_SIZE;
2963
2964 buf = kmap(page) + offset;
2965
2966 qc->cursect++;
2967 qc->cursg_ofs++;
2968
32529e01 2969 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
1da177e4
LT
2970 qc->cursg++;
2971 qc->cursg_ofs = 0;
2972 }
2973
2974 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2975
2976 /* do the actual data transfer */
2977 do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
2978 ata_data_xfer(ap, buf, ATA_SECT_SIZE, do_write);
2979
2980 kunmap(page);
2981}
2982
6ae4cfb5
AL
2983/**
2984 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
2985 * @qc: Command on going
2986 * @bytes: number of bytes
2987 *
2988 * Transfer Transfer data from/to the ATAPI device.
2989 *
2990 * LOCKING:
2991 * Inherited from caller.
2992 *
2993 */
2994
1da177e4
LT
2995static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
2996{
2997 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
2998 struct scatterlist *sg = qc->sg;
2999 struct ata_port *ap = qc->ap;
3000 struct page *page;
3001 unsigned char *buf;
3002 unsigned int offset, count;
3003
563a6e1f 3004 if (qc->curbytes + bytes >= qc->nbytes)
14be71f4 3005 ap->hsm_task_state = HSM_ST_LAST;
1da177e4
LT
3006
3007next_sg:
563a6e1f 3008 if (unlikely(qc->cursg >= qc->n_elem)) {
7fb6ec28 3009 /*
563a6e1f
AL
3010 * The end of qc->sg is reached and the device expects
3011 * more data to transfer. In order not to overrun qc->sg
3012 * and fulfill length specified in the byte count register,
3013 * - for read case, discard trailing data from the device
3014 * - for write case, padding zero data to the device
3015 */
3016 u16 pad_buf[1] = { 0 };
3017 unsigned int words = bytes >> 1;
3018 unsigned int i;
3019
3020 if (words) /* warning if bytes > 1 */
7fb6ec28 3021 printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
563a6e1f
AL
3022 ap->id, bytes);
3023
3024 for (i = 0; i < words; i++)
3025 ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
3026
14be71f4 3027 ap->hsm_task_state = HSM_ST_LAST;
563a6e1f
AL
3028 return;
3029 }
3030
1da177e4
LT
3031 sg = &qc->sg[qc->cursg];
3032
1da177e4
LT
3033 page = sg->page;
3034 offset = sg->offset + qc->cursg_ofs;
3035
3036 /* get the current page and offset */
3037 page = nth_page(page, (offset >> PAGE_SHIFT));
3038 offset %= PAGE_SIZE;
3039
6952df03 3040 /* don't overrun current sg */
32529e01 3041 count = min(sg->length - qc->cursg_ofs, bytes);
1da177e4
LT
3042
3043 /* don't cross page boundaries */
3044 count = min(count, (unsigned int)PAGE_SIZE - offset);
3045
3046 buf = kmap(page) + offset;
3047
3048 bytes -= count;
3049 qc->curbytes += count;
3050 qc->cursg_ofs += count;
3051
32529e01 3052 if (qc->cursg_ofs == sg->length) {
1da177e4
LT
3053 qc->cursg++;
3054 qc->cursg_ofs = 0;
3055 }
3056
3057 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3058
3059 /* do the actual data transfer */
3060 ata_data_xfer(ap, buf, count, do_write);
3061
3062 kunmap(page);
3063
563a6e1f 3064 if (bytes)
1da177e4 3065 goto next_sg;
1da177e4
LT
3066}
3067
6ae4cfb5
AL
3068/**
3069 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
3070 * @qc: Command on going
3071 *
3072 * Transfer Transfer data from/to the ATAPI device.
3073 *
3074 * LOCKING:
3075 * Inherited from caller.
6ae4cfb5
AL
3076 */
3077
1da177e4
LT
3078static void atapi_pio_bytes(struct ata_queued_cmd *qc)
3079{
3080 struct ata_port *ap = qc->ap;
3081 struct ata_device *dev = qc->dev;
3082 unsigned int ireason, bc_lo, bc_hi, bytes;
3083 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
3084
3085 ap->ops->tf_read(ap, &qc->tf);
3086 ireason = qc->tf.nsect;
3087 bc_lo = qc->tf.lbam;
3088 bc_hi = qc->tf.lbah;
3089 bytes = (bc_hi << 8) | bc_lo;
3090
3091 /* shall be cleared to zero, indicating xfer of data */
3092 if (ireason & (1 << 0))
3093 goto err_out;
3094
3095 /* make sure transfer direction matches expected */
3096 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
3097 if (do_write != i_write)
3098 goto err_out;
3099
3100 __atapi_pio_bytes(qc, bytes);
3101
3102 return;
3103
3104err_out:
3105 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
3106 ap->id, dev->devno);
14be71f4 3107 ap->hsm_task_state = HSM_ST_ERR;
1da177e4
LT
3108}
3109
3110/**
6f0ef4fa
RD
3111 * ata_pio_block - start PIO on a block
3112 * @ap: the target ata_port
1da177e4
LT
3113 *
3114 * LOCKING:
0cba632b 3115 * None. (executing in kernel thread context)
1da177e4
LT
3116 */
3117
3118static void ata_pio_block(struct ata_port *ap)
3119{
3120 struct ata_queued_cmd *qc;
3121 u8 status;
3122
3123 /*
6f0ef4fa 3124 * This is purely heuristic. This is a fast path.
1da177e4
LT
3125 * Sometimes when we enter, BSY will be cleared in
3126 * a chk-status or two. If not, the drive is probably seeking
3127 * or something. Snooze for a couple msecs, then
3128 * chk-status again. If still busy, fall back to
14be71f4 3129 * HSM_ST_POLL state.
1da177e4
LT
3130 */
3131 status = ata_busy_wait(ap, ATA_BUSY, 5);
3132 if (status & ATA_BUSY) {
3133 msleep(2);
3134 status = ata_busy_wait(ap, ATA_BUSY, 10);
3135 if (status & ATA_BUSY) {
14be71f4 3136 ap->hsm_task_state = HSM_ST_POLL;
1da177e4
LT
3137 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
3138 return;
3139 }
3140 }
3141
3142 qc = ata_qc_from_tag(ap, ap->active_tag);
3143 assert(qc != NULL);
3144
3145 if (is_atapi_taskfile(&qc->tf)) {
3146 /* no more data to transfer or unsupported ATAPI command */
3147 if ((status & ATA_DRQ) == 0) {
14be71f4 3148 ap->hsm_task_state = HSM_ST_LAST;
1da177e4
LT
3149 return;
3150 }
3151
3152 atapi_pio_bytes(qc);
3153 } else {
3154 /* handle BSY=0, DRQ=0 as error */
3155 if ((status & ATA_DRQ) == 0) {
14be71f4 3156 ap->hsm_task_state = HSM_ST_ERR;
1da177e4
LT
3157 return;
3158 }
3159
3160 ata_pio_sector(qc);
3161 }
3162}
3163
3164static void ata_pio_error(struct ata_port *ap)
3165{
3166 struct ata_queued_cmd *qc;
3167 u8 drv_stat;
3168
3169 qc = ata_qc_from_tag(ap, ap->active_tag);
3170 assert(qc != NULL);
3171
3172 drv_stat = ata_chk_status(ap);
3173 printk(KERN_WARNING "ata%u: PIO error, drv_stat 0x%x\n",
3174 ap->id, drv_stat);
3175
14be71f4 3176 ap->hsm_task_state = HSM_ST_IDLE;
1da177e4 3177
40e8c82c 3178 ata_poll_qc_complete(qc, drv_stat | ATA_ERR);
1da177e4
LT
3179}
3180
3181static void ata_pio_task(void *_data)
3182{
3183 struct ata_port *ap = _data;
7fb6ec28
JG
3184 unsigned long timeout;
3185 int qc_completed;
3186
3187fsm_start:
3188 timeout = 0;
3189 qc_completed = 0;
1da177e4 3190
14be71f4
AL
3191 switch (ap->hsm_task_state) {
3192 case HSM_ST_IDLE:
1da177e4
LT
3193 return;
3194
14be71f4 3195 case HSM_ST:
1da177e4
LT
3196 ata_pio_block(ap);
3197 break;
3198
14be71f4 3199 case HSM_ST_LAST:
7fb6ec28 3200 qc_completed = ata_pio_complete(ap);
1da177e4
LT
3201 break;
3202
14be71f4
AL
3203 case HSM_ST_POLL:
3204 case HSM_ST_LAST_POLL:
1da177e4
LT
3205 timeout = ata_pio_poll(ap);
3206 break;
3207
14be71f4
AL
3208 case HSM_ST_TMOUT:
3209 case HSM_ST_ERR:
1da177e4
LT
3210 ata_pio_error(ap);
3211 return;
3212 }
3213
3214 if (timeout)
7fb6ec28
JG
3215 queue_delayed_work(ata_wq, &ap->pio_task, timeout);
3216 else if (!qc_completed)
3217 goto fsm_start;
1da177e4
LT
3218}
3219
1da177e4
LT
3220/**
3221 * ata_qc_timeout - Handle timeout of queued command
3222 * @qc: Command that timed out
3223 *
3224 * Some part of the kernel (currently, only the SCSI layer)
3225 * has noticed that the active command on port @ap has not
3226 * completed after a specified length of time. Handle this
3227 * condition by disabling DMA (if necessary) and completing
3228 * transactions, with error if necessary.
3229 *
3230 * This also handles the case of the "lost interrupt", where
3231 * for some reason (possibly hardware bug, possibly driver bug)
3232 * an interrupt was not delivered to the driver, even though the
3233 * transaction completed successfully.
3234 *
3235 * LOCKING:
0cba632b 3236 * Inherited from SCSI layer (none, can sleep)
1da177e4
LT
3237 */
3238
3239static void ata_qc_timeout(struct ata_queued_cmd *qc)
3240{
3241 struct ata_port *ap = qc->ap;
b8f6153e 3242 struct ata_host_set *host_set = ap->host_set;
1da177e4
LT
3243 struct ata_device *dev = qc->dev;
3244 u8 host_stat = 0, drv_stat;
b8f6153e 3245 unsigned long flags;
1da177e4
LT
3246
3247 DPRINTK("ENTER\n");
3248
3249 /* FIXME: doesn't this conflict with timeout handling? */
3250 if (qc->dev->class == ATA_DEV_ATAPI && qc->scsicmd) {
3251 struct scsi_cmnd *cmd = qc->scsicmd;
3252
3111b0d1 3253 if (!(cmd->eh_eflags & SCSI_EH_CANCEL_CMD)) {
1da177e4
LT
3254
3255 /* finish completing original command */
b8f6153e 3256 spin_lock_irqsave(&host_set->lock, flags);
1da177e4 3257 __ata_qc_complete(qc);
b8f6153e 3258 spin_unlock_irqrestore(&host_set->lock, flags);
1da177e4
LT
3259
3260 atapi_request_sense(ap, dev, cmd);
3261
3262 cmd->result = (CHECK_CONDITION << 1) | (DID_OK << 16);
3263 scsi_finish_command(cmd);
3264
3265 goto out;
3266 }
3267 }
3268
b8f6153e
JG
3269 spin_lock_irqsave(&host_set->lock, flags);
3270
1da177e4
LT
3271 /* hack alert! We cannot use the supplied completion
3272 * function from inside the ->eh_strategy_handler() thread.
3273 * libata is the only user of ->eh_strategy_handler() in
3274 * any kernel, so the default scsi_done() assumes it is
3275 * not being called from the SCSI EH.
3276 */
3277 qc->scsidone = scsi_finish_command;
3278
3279 switch (qc->tf.protocol) {
3280
3281 case ATA_PROT_DMA:
3282 case ATA_PROT_ATAPI_DMA:
3283 host_stat = ap->ops->bmdma_status(ap);
3284
3285 /* before we do anything else, clear DMA-Start bit */
b73fc89f 3286 ap->ops->bmdma_stop(qc);
1da177e4
LT
3287
3288 /* fall through */
3289
3290 default:
3291 ata_altstatus(ap);
3292 drv_stat = ata_chk_status(ap);
3293
3294 /* ack bmdma irq events */
3295 ap->ops->irq_clear(ap);
3296
3297 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
3298 ap->id, qc->tf.command, drv_stat, host_stat);
3299
3300 /* complete taskfile transaction */
3301 ata_qc_complete(qc, drv_stat);
3302 break;
3303 }
b8f6153e
JG
3304
3305 spin_unlock_irqrestore(&host_set->lock, flags);
3306
1da177e4
LT
3307out:
3308 DPRINTK("EXIT\n");
3309}
3310
3311/**
3312 * ata_eng_timeout - Handle timeout of queued command
3313 * @ap: Port on which timed-out command is active
3314 *
3315 * Some part of the kernel (currently, only the SCSI layer)
3316 * has noticed that the active command on port @ap has not
3317 * completed after a specified length of time. Handle this
3318 * condition by disabling DMA (if necessary) and completing
3319 * transactions, with error if necessary.
3320 *
3321 * This also handles the case of the "lost interrupt", where
3322 * for some reason (possibly hardware bug, possibly driver bug)
3323 * an interrupt was not delivered to the driver, even though the
3324 * transaction completed successfully.
3325 *
3326 * LOCKING:
3327 * Inherited from SCSI layer (none, can sleep)
3328 */
3329
3330void ata_eng_timeout(struct ata_port *ap)
3331{
3332 struct ata_queued_cmd *qc;
3333
3334 DPRINTK("ENTER\n");
3335
3336 qc = ata_qc_from_tag(ap, ap->active_tag);
e12669e7
JG
3337 if (qc)
3338 ata_qc_timeout(qc);
3339 else {
1da177e4
LT
3340 printk(KERN_ERR "ata%u: BUG: timeout without command\n",
3341 ap->id);
3342 goto out;
3343 }
3344
1da177e4
LT
3345out:
3346 DPRINTK("EXIT\n");
3347}
3348
3349/**
3350 * ata_qc_new - Request an available ATA command, for queueing
3351 * @ap: Port associated with device @dev
3352 * @dev: Device from whom we request an available command structure
3353 *
3354 * LOCKING:
0cba632b 3355 * None.
1da177e4
LT
3356 */
3357
3358static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
3359{
3360 struct ata_queued_cmd *qc = NULL;
3361 unsigned int i;
3362
3363 for (i = 0; i < ATA_MAX_QUEUE; i++)
3364 if (!test_and_set_bit(i, &ap->qactive)) {
3365 qc = ata_qc_from_tag(ap, i);
3366 break;
3367 }
3368
3369 if (qc)
3370 qc->tag = i;
3371
3372 return qc;
3373}
3374
3375/**
3376 * ata_qc_new_init - Request an available ATA command, and initialize it
3377 * @ap: Port associated with device @dev
3378 * @dev: Device from whom we request an available command structure
3379 *
3380 * LOCKING:
0cba632b 3381 * None.
1da177e4
LT
3382 */
3383
3384struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
3385 struct ata_device *dev)
3386{
3387 struct ata_queued_cmd *qc;
3388
3389 qc = ata_qc_new(ap);
3390 if (qc) {
3391 qc->sg = NULL;
3392 qc->flags = 0;
3393 qc->scsicmd = NULL;
3394 qc->ap = ap;
3395 qc->dev = dev;
3396 qc->cursect = qc->cursg = qc->cursg_ofs = 0;
3397 qc->nsect = 0;
3398 qc->nbytes = qc->curbytes = 0;
3399
3400 ata_tf_init(ap, &qc->tf, dev->devno);
1da177e4
LT
3401 }
3402
3403 return qc;
3404}
3405
a939c963 3406int ata_qc_complete_noop(struct ata_queued_cmd *qc, u8 drv_stat)
1da177e4
LT
3407{
3408 return 0;
3409}
3410
3411static void __ata_qc_complete(struct ata_queued_cmd *qc)
3412{
3413 struct ata_port *ap = qc->ap;
3414 unsigned int tag, do_clear = 0;
3415
3416 qc->flags = 0;
3417 tag = qc->tag;
3418 if (likely(ata_tag_valid(tag))) {
3419 if (tag == ap->active_tag)
3420 ap->active_tag = ATA_TAG_POISON;
3421 qc->tag = ATA_TAG_POISON;
3422 do_clear = 1;
3423 }
3424
3425 if (qc->waiting) {
3426 struct completion *waiting = qc->waiting;
3427 qc->waiting = NULL;
3428 complete(waiting);
3429 }
3430
3431 if (likely(do_clear))
3432 clear_bit(tag, &ap->qactive);
3433}
3434
3435/**
3436 * ata_qc_free - free unused ata_queued_cmd
3437 * @qc: Command to complete
3438 *
3439 * Designed to free unused ata_queued_cmd object
3440 * in case something prevents using it.
3441 *
3442 * LOCKING:
0cba632b 3443 * spin_lock_irqsave(host_set lock)
1da177e4
LT
3444 */
3445void ata_qc_free(struct ata_queued_cmd *qc)
3446{
3447 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
3448 assert(qc->waiting == NULL); /* nothing should be waiting */
3449
3450 __ata_qc_complete(qc);
3451}
3452
3453/**
3454 * ata_qc_complete - Complete an active ATA command
3455 * @qc: Command to complete
0cba632b
JG
3456 * @drv_stat: ATA Status register contents
3457 *
3458 * Indicate to the mid and upper layers that an ATA
3459 * command has completed, with either an ok or not-ok status.
1da177e4
LT
3460 *
3461 * LOCKING:
0cba632b 3462 * spin_lock_irqsave(host_set lock)
1da177e4
LT
3463 */
3464
3465void ata_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
3466{
3467 int rc;
3468
3469 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
3470 assert(qc->flags & ATA_QCFLAG_ACTIVE);
3471
3472 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
3473 ata_sg_clean(qc);
3474
3f3791d3
AL
3475 /* atapi: mark qc as inactive to prevent the interrupt handler
3476 * from completing the command twice later, before the error handler
3477 * is called. (when rc != 0 and atapi request sense is needed)
3478 */
3479 qc->flags &= ~ATA_QCFLAG_ACTIVE;
3480
1da177e4
LT
3481 /* call completion callback */
3482 rc = qc->complete_fn(qc, drv_stat);
3483
3484 /* if callback indicates not to complete command (non-zero),
3485 * return immediately
3486 */
3487 if (rc != 0)
3488 return;
3489
3490 __ata_qc_complete(qc);
3491
3492 VPRINTK("EXIT\n");
3493}
3494
3495static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
3496{
3497 struct ata_port *ap = qc->ap;
3498
3499 switch (qc->tf.protocol) {
3500 case ATA_PROT_DMA:
3501 case ATA_PROT_ATAPI_DMA:
3502 return 1;
3503
3504 case ATA_PROT_ATAPI:
3505 case ATA_PROT_PIO:
3506 case ATA_PROT_PIO_MULT:
3507 if (ap->flags & ATA_FLAG_PIO_DMA)
3508 return 1;
3509
3510 /* fall through */
3511
3512 default:
3513 return 0;
3514 }
3515
3516 /* never reached */
3517}
3518
3519/**
3520 * ata_qc_issue - issue taskfile to device
3521 * @qc: command to issue to device
3522 *
3523 * Prepare an ATA command to submission to device.
3524 * This includes mapping the data into a DMA-able
3525 * area, filling in the S/G table, and finally
3526 * writing the taskfile to hardware, starting the command.
3527 *
3528 * LOCKING:
3529 * spin_lock_irqsave(host_set lock)
3530 *
3531 * RETURNS:
3532 * Zero on success, negative on error.
3533 */
3534
3535int ata_qc_issue(struct ata_queued_cmd *qc)
3536{
3537 struct ata_port *ap = qc->ap;
3538
3539 if (ata_should_dma_map(qc)) {
3540 if (qc->flags & ATA_QCFLAG_SG) {
3541 if (ata_sg_setup(qc))
3542 goto err_out;
3543 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
3544 if (ata_sg_setup_one(qc))
3545 goto err_out;
3546 }
3547 } else {
3548 qc->flags &= ~ATA_QCFLAG_DMAMAP;
3549 }
3550
3551 ap->ops->qc_prep(qc);
3552
3553 qc->ap->active_tag = qc->tag;
3554 qc->flags |= ATA_QCFLAG_ACTIVE;
3555
3556 return ap->ops->qc_issue(qc);
3557
3558err_out:
3559 return -1;
3560}
3561
0baab86b 3562
1da177e4
LT
3563/**
3564 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
3565 * @qc: command to issue to device
3566 *
3567 * Using various libata functions and hooks, this function
3568 * starts an ATA command. ATA commands are grouped into
3569 * classes called "protocols", and issuing each type of protocol
3570 * is slightly different.
3571 *
0baab86b
EF
3572 * May be used as the qc_issue() entry in ata_port_operations.
3573 *
1da177e4
LT
3574 * LOCKING:
3575 * spin_lock_irqsave(host_set lock)
3576 *
3577 * RETURNS:
3578 * Zero on success, negative on error.
3579 */
3580
3581int ata_qc_issue_prot(struct ata_queued_cmd *qc)
3582{
3583 struct ata_port *ap = qc->ap;
3584
3585 ata_dev_select(ap, qc->dev->devno, 1, 0);
3586
3587 switch (qc->tf.protocol) {
3588 case ATA_PROT_NODATA:
3589 ata_tf_to_host_nolock(ap, &qc->tf);
3590 break;
3591
3592 case ATA_PROT_DMA:
3593 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3594 ap->ops->bmdma_setup(qc); /* set up bmdma */
3595 ap->ops->bmdma_start(qc); /* initiate bmdma */
3596 break;
3597
3598 case ATA_PROT_PIO: /* load tf registers, initiate polling pio */
3599 ata_qc_set_polling(qc);
3600 ata_tf_to_host_nolock(ap, &qc->tf);
14be71f4 3601 ap->hsm_task_state = HSM_ST;
1da177e4
LT
3602 queue_work(ata_wq, &ap->pio_task);
3603 break;
3604
3605 case ATA_PROT_ATAPI:
3606 ata_qc_set_polling(qc);
3607 ata_tf_to_host_nolock(ap, &qc->tf);
3608 queue_work(ata_wq, &ap->packet_task);
3609 break;
3610
3611 case ATA_PROT_ATAPI_NODATA:
c1389503 3612 ap->flags |= ATA_FLAG_NOINTR;
1da177e4
LT
3613 ata_tf_to_host_nolock(ap, &qc->tf);
3614 queue_work(ata_wq, &ap->packet_task);
3615 break;
3616
3617 case ATA_PROT_ATAPI_DMA:
c1389503 3618 ap->flags |= ATA_FLAG_NOINTR;
1da177e4
LT
3619 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3620 ap->ops->bmdma_setup(qc); /* set up bmdma */
3621 queue_work(ata_wq, &ap->packet_task);
3622 break;
3623
3624 default:
3625 WARN_ON(1);
3626 return -1;
3627 }
3628
3629 return 0;
3630}
3631
3632/**
0baab86b 3633 * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
1da177e4
LT
3634 * @qc: Info associated with this ATA transaction.
3635 *
3636 * LOCKING:
3637 * spin_lock_irqsave(host_set lock)
3638 */
3639
3640static void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc)
3641{
3642 struct ata_port *ap = qc->ap;
3643 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3644 u8 dmactl;
3645 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3646
3647 /* load PRD table addr. */
3648 mb(); /* make sure PRD table writes are visible to controller */
3649 writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
3650
3651 /* specify data direction, triple-check start bit is clear */
3652 dmactl = readb(mmio + ATA_DMA_CMD);
3653 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3654 if (!rw)
3655 dmactl |= ATA_DMA_WR;
3656 writeb(dmactl, mmio + ATA_DMA_CMD);
3657
3658 /* issue r/w command */
3659 ap->ops->exec_command(ap, &qc->tf);
3660}
3661
3662/**
b73fc89f 3663 * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
1da177e4
LT
3664 * @qc: Info associated with this ATA transaction.
3665 *
3666 * LOCKING:
3667 * spin_lock_irqsave(host_set lock)
3668 */
3669
3670static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
3671{
3672 struct ata_port *ap = qc->ap;
3673 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3674 u8 dmactl;
3675
3676 /* start host DMA transaction */
3677 dmactl = readb(mmio + ATA_DMA_CMD);
3678 writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
3679
3680 /* Strictly, one may wish to issue a readb() here, to
3681 * flush the mmio write. However, control also passes
3682 * to the hardware at this point, and it will interrupt
3683 * us when we are to resume control. So, in effect,
3684 * we don't care when the mmio write flushes.
3685 * Further, a read of the DMA status register _immediately_
3686 * following the write may not be what certain flaky hardware
3687 * is expected, so I think it is best to not add a readb()
3688 * without first all the MMIO ATA cards/mobos.
3689 * Or maybe I'm just being paranoid.
3690 */
3691}
3692
3693/**
3694 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
3695 * @qc: Info associated with this ATA transaction.
3696 *
3697 * LOCKING:
3698 * spin_lock_irqsave(host_set lock)
3699 */
3700
3701static void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
3702{
3703 struct ata_port *ap = qc->ap;
3704 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3705 u8 dmactl;
3706
3707 /* load PRD table addr. */
3708 outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
3709
3710 /* specify data direction, triple-check start bit is clear */
3711 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3712 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3713 if (!rw)
3714 dmactl |= ATA_DMA_WR;
3715 outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3716
3717 /* issue r/w command */
3718 ap->ops->exec_command(ap, &qc->tf);
3719}
3720
3721/**
3722 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
3723 * @qc: Info associated with this ATA transaction.
3724 *
3725 * LOCKING:
3726 * spin_lock_irqsave(host_set lock)
3727 */
3728
3729static void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
3730{
3731 struct ata_port *ap = qc->ap;
3732 u8 dmactl;
3733
3734 /* start host DMA transaction */
3735 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3736 outb(dmactl | ATA_DMA_START,
3737 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3738}
3739
0baab86b
EF
3740
3741/**
3742 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
3743 * @qc: Info associated with this ATA transaction.
3744 *
3745 * Writes the ATA_DMA_START flag to the DMA command register.
3746 *
3747 * May be used as the bmdma_start() entry in ata_port_operations.
3748 *
3749 * LOCKING:
3750 * spin_lock_irqsave(host_set lock)
3751 */
1da177e4
LT
3752void ata_bmdma_start(struct ata_queued_cmd *qc)
3753{
3754 if (qc->ap->flags & ATA_FLAG_MMIO)
3755 ata_bmdma_start_mmio(qc);
3756 else
3757 ata_bmdma_start_pio(qc);
3758}
3759
0baab86b
EF
3760
3761/**
3762 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
3763 * @qc: Info associated with this ATA transaction.
3764 *
3765 * Writes address of PRD table to device's PRD Table Address
3766 * register, sets the DMA control register, and calls
3767 * ops->exec_command() to start the transfer.
3768 *
3769 * May be used as the bmdma_setup() entry in ata_port_operations.
3770 *
3771 * LOCKING:
3772 * spin_lock_irqsave(host_set lock)
3773 */
1da177e4
LT
3774void ata_bmdma_setup(struct ata_queued_cmd *qc)
3775{
3776 if (qc->ap->flags & ATA_FLAG_MMIO)
3777 ata_bmdma_setup_mmio(qc);
3778 else
3779 ata_bmdma_setup_pio(qc);
3780}
3781
0baab86b
EF
3782
3783/**
3784 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
decc6d0b 3785 * @ap: Port associated with this ATA transaction.
0baab86b
EF
3786 *
3787 * Clear interrupt and error flags in DMA status register.
3788 *
3789 * May be used as the irq_clear() entry in ata_port_operations.
3790 *
3791 * LOCKING:
3792 * spin_lock_irqsave(host_set lock)
3793 */
3794
1da177e4
LT
3795void ata_bmdma_irq_clear(struct ata_port *ap)
3796{
3797 if (ap->flags & ATA_FLAG_MMIO) {
3798 void __iomem *mmio = ((void __iomem *) ap->ioaddr.bmdma_addr) + ATA_DMA_STATUS;
3799 writeb(readb(mmio), mmio);
3800 } else {
3801 unsigned long addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
3802 outb(inb(addr), addr);
3803 }
3804
3805}
3806
0baab86b
EF
3807
3808/**
3809 * ata_bmdma_status - Read PCI IDE BMDMA status
decc6d0b 3810 * @ap: Port associated with this ATA transaction.
0baab86b
EF
3811 *
3812 * Read and return BMDMA status register.
3813 *
3814 * May be used as the bmdma_status() entry in ata_port_operations.
3815 *
3816 * LOCKING:
3817 * spin_lock_irqsave(host_set lock)
3818 */
3819
1da177e4
LT
3820u8 ata_bmdma_status(struct ata_port *ap)
3821{
3822 u8 host_stat;
3823 if (ap->flags & ATA_FLAG_MMIO) {
3824 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3825 host_stat = readb(mmio + ATA_DMA_STATUS);
3826 } else
ee500aab 3827 host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
1da177e4
LT
3828 return host_stat;
3829}
3830
0baab86b
EF
3831
3832/**
3833 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
b73fc89f 3834 * @qc: Command we are ending DMA for
0baab86b
EF
3835 *
3836 * Clears the ATA_DMA_START flag in the dma control register
3837 *
3838 * May be used as the bmdma_stop() entry in ata_port_operations.
3839 *
3840 * LOCKING:
3841 * spin_lock_irqsave(host_set lock)
3842 */
3843
b73fc89f 3844void ata_bmdma_stop(struct ata_queued_cmd *qc)
1da177e4 3845{
b73fc89f 3846 struct ata_port *ap = qc->ap;
1da177e4
LT
3847 if (ap->flags & ATA_FLAG_MMIO) {
3848 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3849
3850 /* clear start/stop bit */
3851 writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
3852 mmio + ATA_DMA_CMD);
3853 } else {
3854 /* clear start/stop bit */
3855 outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
3856 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3857 }
3858
3859 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
3860 ata_altstatus(ap); /* dummy read */
3861}
3862
3863/**
3864 * ata_host_intr - Handle host interrupt for given (port, task)
3865 * @ap: Port on which interrupt arrived (possibly...)
3866 * @qc: Taskfile currently active in engine
3867 *
3868 * Handle host interrupt for given queued command. Currently,
3869 * only DMA interrupts are handled. All other commands are
3870 * handled via polling with interrupts disabled (nIEN bit).
3871 *
3872 * LOCKING:
3873 * spin_lock_irqsave(host_set lock)
3874 *
3875 * RETURNS:
3876 * One if interrupt was handled, zero if not (shared irq).
3877 */
3878
3879inline unsigned int ata_host_intr (struct ata_port *ap,
3880 struct ata_queued_cmd *qc)
3881{
3882 u8 status, host_stat;
3883
3884 switch (qc->tf.protocol) {
3885
3886 case ATA_PROT_DMA:
3887 case ATA_PROT_ATAPI_DMA:
3888 case ATA_PROT_ATAPI:
3889 /* check status of DMA engine */
3890 host_stat = ap->ops->bmdma_status(ap);
3891 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
3892
3893 /* if it's not our irq... */
3894 if (!(host_stat & ATA_DMA_INTR))
3895 goto idle_irq;
3896
3897 /* before we do anything else, clear DMA-Start bit */
b73fc89f 3898 ap->ops->bmdma_stop(qc);
1da177e4
LT
3899
3900 /* fall through */
3901
3902 case ATA_PROT_ATAPI_NODATA:
3903 case ATA_PROT_NODATA:
3904 /* check altstatus */
3905 status = ata_altstatus(ap);
3906 if (status & ATA_BUSY)
3907 goto idle_irq;
3908
3909 /* check main status, clearing INTRQ */
3910 status = ata_chk_status(ap);
3911 if (unlikely(status & ATA_BUSY))
3912 goto idle_irq;
3913 DPRINTK("ata%u: protocol %d (dev_stat 0x%X)\n",
3914 ap->id, qc->tf.protocol, status);
3915
3916 /* ack bmdma irq events */
3917 ap->ops->irq_clear(ap);
3918
3919 /* complete taskfile transaction */
3920 ata_qc_complete(qc, status);
3921 break;
3922
3923 default:
3924 goto idle_irq;
3925 }
3926
3927 return 1; /* irq handled */
3928
3929idle_irq:
3930 ap->stats.idle_irq++;
3931
3932#ifdef ATA_IRQ_TRAP
3933 if ((ap->stats.idle_irq % 1000) == 0) {
3934 handled = 1;
3935 ata_irq_ack(ap, 0); /* debug trap */
3936 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
3937 }
3938#endif
3939 return 0; /* irq not handled */
3940}
3941
3942/**
3943 * ata_interrupt - Default ATA host interrupt handler
0cba632b
JG
3944 * @irq: irq line (unused)
3945 * @dev_instance: pointer to our ata_host_set information structure
1da177e4
LT
3946 * @regs: unused
3947 *
0cba632b
JG
3948 * Default interrupt handler for PCI IDE devices. Calls
3949 * ata_host_intr() for each port that is not disabled.
3950 *
1da177e4 3951 * LOCKING:
0cba632b 3952 * Obtains host_set lock during operation.
1da177e4
LT
3953 *
3954 * RETURNS:
0cba632b 3955 * IRQ_NONE or IRQ_HANDLED.
1da177e4
LT
3956 */
3957
3958irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
3959{
3960 struct ata_host_set *host_set = dev_instance;
3961 unsigned int i;
3962 unsigned int handled = 0;
3963 unsigned long flags;
3964
3965 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
3966 spin_lock_irqsave(&host_set->lock, flags);
3967
3968 for (i = 0; i < host_set->n_ports; i++) {
3969 struct ata_port *ap;
3970
3971 ap = host_set->ports[i];
c1389503
TH
3972 if (ap &&
3973 !(ap->flags & (ATA_FLAG_PORT_DISABLED | ATA_FLAG_NOINTR))) {
1da177e4
LT
3974 struct ata_queued_cmd *qc;
3975
3976 qc = ata_qc_from_tag(ap, ap->active_tag);
21b1ed74
AL
3977 if (qc && (!(qc->tf.ctl & ATA_NIEN)) &&
3978 (qc->flags & ATA_QCFLAG_ACTIVE))
1da177e4
LT
3979 handled |= ata_host_intr(ap, qc);
3980 }
3981 }
3982
3983 spin_unlock_irqrestore(&host_set->lock, flags);
3984
3985 return IRQ_RETVAL(handled);
3986}
3987
3988/**
3989 * atapi_packet_task - Write CDB bytes to hardware
3990 * @_data: Port to which ATAPI device is attached.
3991 *
3992 * When device has indicated its readiness to accept
3993 * a CDB, this function is called. Send the CDB.
3994 * If DMA is to be performed, exit immediately.
3995 * Otherwise, we are in polling mode, so poll
3996 * status under operation succeeds or fails.
3997 *
3998 * LOCKING:
3999 * Kernel thread context (may sleep)
4000 */
4001
4002static void atapi_packet_task(void *_data)
4003{
4004 struct ata_port *ap = _data;
4005 struct ata_queued_cmd *qc;
4006 u8 status;
4007
4008 qc = ata_qc_from_tag(ap, ap->active_tag);
4009 assert(qc != NULL);
4010 assert(qc->flags & ATA_QCFLAG_ACTIVE);
4011
4012 /* sleep-wait for BSY to clear */
4013 DPRINTK("busy wait\n");
4014 if (ata_busy_sleep(ap, ATA_TMOUT_CDB_QUICK, ATA_TMOUT_CDB))
4015 goto err_out;
4016
4017 /* make sure DRQ is set */
4018 status = ata_chk_status(ap);
4019 if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ)
4020 goto err_out;
4021
4022 /* send SCSI cdb */
4023 DPRINTK("send cdb\n");
4024 assert(ap->cdb_len >= 12);
1da177e4 4025
c1389503
TH
4026 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA ||
4027 qc->tf.protocol == ATA_PROT_ATAPI_NODATA) {
4028 unsigned long flags;
1da177e4 4029
c1389503
TH
4030 /* Once we're done issuing command and kicking bmdma,
4031 * irq handler takes over. To not lose irq, we need
4032 * to clear NOINTR flag before sending cdb, but
4033 * interrupt handler shouldn't be invoked before we're
4034 * finished. Hence, the following locking.
4035 */
4036 spin_lock_irqsave(&ap->host_set->lock, flags);
4037 ap->flags &= ~ATA_FLAG_NOINTR;
4038 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
4039 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA)
4040 ap->ops->bmdma_start(qc); /* initiate bmdma */
4041 spin_unlock_irqrestore(&ap->host_set->lock, flags);
4042 } else {
4043 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
1da177e4 4044
c1389503 4045 /* PIO commands are handled by polling */
14be71f4 4046 ap->hsm_task_state = HSM_ST;
1da177e4
LT
4047 queue_work(ata_wq, &ap->pio_task);
4048 }
4049
4050 return;
4051
4052err_out:
40e8c82c 4053 ata_poll_qc_complete(qc, ATA_ERR);
1da177e4
LT
4054}
4055
0baab86b
EF
4056
4057/**
4058 * ata_port_start - Set port up for dma.
4059 * @ap: Port to initialize
4060 *
4061 * Called just after data structures for each port are
4062 * initialized. Allocates space for PRD table.
4063 *
4064 * May be used as the port_start() entry in ata_port_operations.
4065 *
4066 * LOCKING:
6f0ef4fa 4067 * Inherited from caller.
0baab86b
EF
4068 */
4069
1da177e4
LT
4070int ata_port_start (struct ata_port *ap)
4071{
4072 struct device *dev = ap->host_set->dev;
4073
4074 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
4075 if (!ap->prd)
4076 return -ENOMEM;
4077
4078 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
4079
4080 return 0;
4081}
4082
0baab86b
EF
4083
4084/**
4085 * ata_port_stop - Undo ata_port_start()
4086 * @ap: Port to shut down
4087 *
4088 * Frees the PRD table.
4089 *
4090 * May be used as the port_stop() entry in ata_port_operations.
4091 *
4092 * LOCKING:
6f0ef4fa 4093 * Inherited from caller.
0baab86b
EF
4094 */
4095
1da177e4
LT
4096void ata_port_stop (struct ata_port *ap)
4097{
4098 struct device *dev = ap->host_set->dev;
4099
4100 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
4101}
4102
aa8f0dc6
JG
4103void ata_host_stop (struct ata_host_set *host_set)
4104{
4105 if (host_set->mmio_base)
4106 iounmap(host_set->mmio_base);
4107}
4108
4109
1da177e4
LT
4110/**
4111 * ata_host_remove - Unregister SCSI host structure with upper layers
4112 * @ap: Port to unregister
4113 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
4114 *
4115 * LOCKING:
6f0ef4fa 4116 * Inherited from caller.
1da177e4
LT
4117 */
4118
4119static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
4120{
4121 struct Scsi_Host *sh = ap->host;
4122
4123 DPRINTK("ENTER\n");
4124
4125 if (do_unregister)
4126 scsi_remove_host(sh);
4127
4128 ap->ops->port_stop(ap);
4129}
4130
4131/**
4132 * ata_host_init - Initialize an ata_port structure
4133 * @ap: Structure to initialize
4134 * @host: associated SCSI mid-layer structure
4135 * @host_set: Collection of hosts to which @ap belongs
4136 * @ent: Probe information provided by low-level driver
4137 * @port_no: Port number associated with this ata_port
4138 *
0cba632b
JG
4139 * Initialize a new ata_port structure, and its associated
4140 * scsi_host.
4141 *
1da177e4 4142 * LOCKING:
0cba632b 4143 * Inherited from caller.
1da177e4
LT
4144 */
4145
4146static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
4147 struct ata_host_set *host_set,
057ace5e 4148 const struct ata_probe_ent *ent, unsigned int port_no)
1da177e4
LT
4149{
4150 unsigned int i;
4151
4152 host->max_id = 16;
4153 host->max_lun = 1;
4154 host->max_channel = 1;
4155 host->unique_id = ata_unique_id++;
4156 host->max_cmd_len = 12;
12413197 4157
1da177e4
LT
4158 scsi_assign_lock(host, &host_set->lock);
4159
4160 ap->flags = ATA_FLAG_PORT_DISABLED;
4161 ap->id = host->unique_id;
4162 ap->host = host;
4163 ap->ctl = ATA_DEVCTL_OBS;
4164 ap->host_set = host_set;
4165 ap->port_no = port_no;
4166 ap->hard_port_no =
4167 ent->legacy_mode ? ent->hard_port_no : port_no;
4168 ap->pio_mask = ent->pio_mask;
4169 ap->mwdma_mask = ent->mwdma_mask;
4170 ap->udma_mask = ent->udma_mask;
4171 ap->flags |= ent->host_flags;
4172 ap->ops = ent->port_ops;
4173 ap->cbl = ATA_CBL_NONE;
4174 ap->active_tag = ATA_TAG_POISON;
4175 ap->last_ctl = 0xFF;
4176
4177 INIT_WORK(&ap->packet_task, atapi_packet_task, ap);
4178 INIT_WORK(&ap->pio_task, ata_pio_task, ap);
4179
4180 for (i = 0; i < ATA_MAX_DEVICES; i++)
4181 ap->device[i].devno = i;
4182
4183#ifdef ATA_IRQ_TRAP
4184 ap->stats.unhandled_irq = 1;
4185 ap->stats.idle_irq = 1;
4186#endif
4187
4188 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
4189}
4190
4191/**
4192 * ata_host_add - Attach low-level ATA driver to system
4193 * @ent: Information provided by low-level driver
4194 * @host_set: Collections of ports to which we add
4195 * @port_no: Port number associated with this host
4196 *
0cba632b
JG
4197 * Attach low-level ATA driver to system.
4198 *
1da177e4 4199 * LOCKING:
0cba632b 4200 * PCI/etc. bus probe sem.
1da177e4
LT
4201 *
4202 * RETURNS:
0cba632b 4203 * New ata_port on success, for NULL on error.
1da177e4
LT
4204 */
4205
057ace5e 4206static struct ata_port * ata_host_add(const struct ata_probe_ent *ent,
1da177e4
LT
4207 struct ata_host_set *host_set,
4208 unsigned int port_no)
4209{
4210 struct Scsi_Host *host;
4211 struct ata_port *ap;
4212 int rc;
4213
4214 DPRINTK("ENTER\n");
4215 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
4216 if (!host)
4217 return NULL;
4218
4219 ap = (struct ata_port *) &host->hostdata[0];
4220
4221 ata_host_init(ap, host, host_set, ent, port_no);
4222
4223 rc = ap->ops->port_start(ap);
4224 if (rc)
4225 goto err_out;
4226
4227 return ap;
4228
4229err_out:
4230 scsi_host_put(host);
4231 return NULL;
4232}
4233
4234/**
0cba632b
JG
4235 * ata_device_add - Register hardware device with ATA and SCSI layers
4236 * @ent: Probe information describing hardware device to be registered
4237 *
4238 * This function processes the information provided in the probe
4239 * information struct @ent, allocates the necessary ATA and SCSI
4240 * host information structures, initializes them, and registers
4241 * everything with requisite kernel subsystems.
4242 *
4243 * This function requests irqs, probes the ATA bus, and probes
4244 * the SCSI bus.
1da177e4
LT
4245 *
4246 * LOCKING:
0cba632b 4247 * PCI/etc. bus probe sem.
1da177e4
LT
4248 *
4249 * RETURNS:
0cba632b 4250 * Number of ports registered. Zero on error (no ports registered).
1da177e4
LT
4251 */
4252
057ace5e 4253int ata_device_add(const struct ata_probe_ent *ent)
1da177e4
LT
4254{
4255 unsigned int count = 0, i;
4256 struct device *dev = ent->dev;
4257 struct ata_host_set *host_set;
4258
4259 DPRINTK("ENTER\n");
4260 /* alloc a container for our list of ATA ports (buses) */
4261 host_set = kmalloc(sizeof(struct ata_host_set) +
4262 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
4263 if (!host_set)
4264 return 0;
4265 memset(host_set, 0, sizeof(struct ata_host_set) + (ent->n_ports * sizeof(void *)));
4266 spin_lock_init(&host_set->lock);
4267
4268 host_set->dev = dev;
4269 host_set->n_ports = ent->n_ports;
4270 host_set->irq = ent->irq;
4271 host_set->mmio_base = ent->mmio_base;
4272 host_set->private_data = ent->private_data;
4273 host_set->ops = ent->port_ops;
4274
4275 /* register each port bound to this device */
4276 for (i = 0; i < ent->n_ports; i++) {
4277 struct ata_port *ap;
4278 unsigned long xfer_mode_mask;
4279
4280 ap = ata_host_add(ent, host_set, i);
4281 if (!ap)
4282 goto err_out;
4283
4284 host_set->ports[i] = ap;
4285 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
4286 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
4287 (ap->pio_mask << ATA_SHIFT_PIO);
4288
4289 /* print per-port info to dmesg */
4290 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
4291 "bmdma 0x%lX irq %lu\n",
4292 ap->id,
4293 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
4294 ata_mode_string(xfer_mode_mask),
4295 ap->ioaddr.cmd_addr,
4296 ap->ioaddr.ctl_addr,
4297 ap->ioaddr.bmdma_addr,
4298 ent->irq);
4299
4300 ata_chk_status(ap);
4301 host_set->ops->irq_clear(ap);
4302 count++;
4303 }
4304
4305 if (!count) {
4306 kfree(host_set);
4307 return 0;
4308 }
4309
4310 /* obtain irq, that is shared between channels */
4311 if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
4312 DRV_NAME, host_set))
4313 goto err_out;
4314
4315 /* perform each probe synchronously */
4316 DPRINTK("probe begin\n");
4317 for (i = 0; i < count; i++) {
4318 struct ata_port *ap;
4319 int rc;
4320
4321 ap = host_set->ports[i];
4322
4323 DPRINTK("ata%u: probe begin\n", ap->id);
4324 rc = ata_bus_probe(ap);
4325 DPRINTK("ata%u: probe end\n", ap->id);
4326
4327 if (rc) {
4328 /* FIXME: do something useful here?
4329 * Current libata behavior will
4330 * tear down everything when
4331 * the module is removed
4332 * or the h/w is unplugged.
4333 */
4334 }
4335
4336 rc = scsi_add_host(ap->host, dev);
4337 if (rc) {
4338 printk(KERN_ERR "ata%u: scsi_add_host failed\n",
4339 ap->id);
4340 /* FIXME: do something useful here */
4341 /* FIXME: handle unconditional calls to
4342 * scsi_scan_host and ata_host_remove, below,
4343 * at the very least
4344 */
4345 }
4346 }
4347
4348 /* probes are done, now scan each port's disk(s) */
4349 DPRINTK("probe begin\n");
4350 for (i = 0; i < count; i++) {
4351 struct ata_port *ap = host_set->ports[i];
4352
644dd0cc 4353 ata_scsi_scan_host(ap);
1da177e4
LT
4354 }
4355
4356 dev_set_drvdata(dev, host_set);
4357
4358 VPRINTK("EXIT, returning %u\n", ent->n_ports);
4359 return ent->n_ports; /* success */
4360
4361err_out:
4362 for (i = 0; i < count; i++) {
4363 ata_host_remove(host_set->ports[i], 1);
4364 scsi_host_put(host_set->ports[i]->host);
4365 }
4366 kfree(host_set);
4367 VPRINTK("EXIT, returning 0\n");
4368 return 0;
4369}
4370
17b14451
AC
4371/**
4372 * ata_host_set_remove - PCI layer callback for device removal
4373 * @host_set: ATA host set that was removed
4374 *
4375 * Unregister all objects associated with this host set. Free those
4376 * objects.
4377 *
4378 * LOCKING:
4379 * Inherited from calling layer (may sleep).
4380 */
4381
17b14451
AC
4382void ata_host_set_remove(struct ata_host_set *host_set)
4383{
4384 struct ata_port *ap;
4385 unsigned int i;
4386
4387 for (i = 0; i < host_set->n_ports; i++) {
4388 ap = host_set->ports[i];
4389 scsi_remove_host(ap->host);
4390 }
4391
4392 free_irq(host_set->irq, host_set);
4393
4394 for (i = 0; i < host_set->n_ports; i++) {
4395 ap = host_set->ports[i];
4396
4397 ata_scsi_release(ap->host);
4398
4399 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
4400 struct ata_ioports *ioaddr = &ap->ioaddr;
4401
4402 if (ioaddr->cmd_addr == 0x1f0)
4403 release_region(0x1f0, 8);
4404 else if (ioaddr->cmd_addr == 0x170)
4405 release_region(0x170, 8);
4406 }
4407
4408 scsi_host_put(ap->host);
4409 }
4410
4411 if (host_set->ops->host_stop)
4412 host_set->ops->host_stop(host_set);
4413
4414 kfree(host_set);
4415}
4416
1da177e4
LT
4417/**
4418 * ata_scsi_release - SCSI layer callback hook for host unload
4419 * @host: libata host to be unloaded
4420 *
4421 * Performs all duties necessary to shut down a libata port...
4422 * Kill port kthread, disable port, and release resources.
4423 *
4424 * LOCKING:
4425 * Inherited from SCSI layer.
4426 *
4427 * RETURNS:
4428 * One.
4429 */
4430
4431int ata_scsi_release(struct Scsi_Host *host)
4432{
4433 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
4434
4435 DPRINTK("ENTER\n");
4436
4437 ap->ops->port_disable(ap);
4438 ata_host_remove(ap, 0);
4439
4440 DPRINTK("EXIT\n");
4441 return 1;
4442}
4443
4444/**
4445 * ata_std_ports - initialize ioaddr with standard port offsets.
4446 * @ioaddr: IO address structure to be initialized
0baab86b
EF
4447 *
4448 * Utility function which initializes data_addr, error_addr,
4449 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
4450 * device_addr, status_addr, and command_addr to standard offsets
4451 * relative to cmd_addr.
4452 *
4453 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
1da177e4 4454 */
0baab86b 4455
1da177e4
LT
4456void ata_std_ports(struct ata_ioports *ioaddr)
4457{
4458 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
4459 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
4460 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
4461 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
4462 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
4463 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
4464 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
4465 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
4466 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
4467 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
4468}
4469
4470static struct ata_probe_ent *
057ace5e 4471ata_probe_ent_alloc(struct device *dev, const struct ata_port_info *port)
1da177e4
LT
4472{
4473 struct ata_probe_ent *probe_ent;
4474
4475 probe_ent = kmalloc(sizeof(*probe_ent), GFP_KERNEL);
4476 if (!probe_ent) {
4477 printk(KERN_ERR DRV_NAME "(%s): out of memory\n",
4478 kobject_name(&(dev->kobj)));
4479 return NULL;
4480 }
4481
4482 memset(probe_ent, 0, sizeof(*probe_ent));
4483
4484 INIT_LIST_HEAD(&probe_ent->node);
4485 probe_ent->dev = dev;
4486
4487 probe_ent->sht = port->sht;
4488 probe_ent->host_flags = port->host_flags;
4489 probe_ent->pio_mask = port->pio_mask;
4490 probe_ent->mwdma_mask = port->mwdma_mask;
4491 probe_ent->udma_mask = port->udma_mask;
4492 probe_ent->port_ops = port->port_ops;
4493
4494 return probe_ent;
4495}
4496
0baab86b
EF
4497
4498
374b1873
JG
4499#ifdef CONFIG_PCI
4500
4501void ata_pci_host_stop (struct ata_host_set *host_set)
4502{
4503 struct pci_dev *pdev = to_pci_dev(host_set->dev);
4504
4505 pci_iounmap(pdev, host_set->mmio_base);
4506}
4507
0baab86b
EF
4508/**
4509 * ata_pci_init_native_mode - Initialize native-mode driver
4510 * @pdev: pci device to be initialized
4511 * @port: array[2] of pointers to port info structures.
47a86593 4512 * @ports: bitmap of ports present
0baab86b
EF
4513 *
4514 * Utility function which allocates and initializes an
4515 * ata_probe_ent structure for a standard dual-port
4516 * PIO-based IDE controller. The returned ata_probe_ent
4517 * structure can be passed to ata_device_add(). The returned
4518 * ata_probe_ent structure should then be freed with kfree().
47a86593
AC
4519 *
4520 * The caller need only pass the address of the primary port, the
4521 * secondary will be deduced automatically. If the device has non
4522 * standard secondary port mappings this function can be called twice,
4523 * once for each interface.
0baab86b
EF
4524 */
4525
1da177e4 4526struct ata_probe_ent *
47a86593 4527ata_pci_init_native_mode(struct pci_dev *pdev, struct ata_port_info **port, int ports)
1da177e4
LT
4528{
4529 struct ata_probe_ent *probe_ent =
4530 ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
47a86593
AC
4531 int p = 0;
4532
1da177e4
LT
4533 if (!probe_ent)
4534 return NULL;
4535
1da177e4
LT
4536 probe_ent->irq = pdev->irq;
4537 probe_ent->irq_flags = SA_SHIRQ;
4538
47a86593
AC
4539 if (ports & ATA_PORT_PRIMARY) {
4540 probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 0);
4541 probe_ent->port[p].altstatus_addr =
4542 probe_ent->port[p].ctl_addr =
4543 pci_resource_start(pdev, 1) | ATA_PCI_CTL_OFS;
4544 probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4);
4545 ata_std_ports(&probe_ent->port[p]);
4546 p++;
4547 }
1da177e4 4548
47a86593
AC
4549 if (ports & ATA_PORT_SECONDARY) {
4550 probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 2);
4551 probe_ent->port[p].altstatus_addr =
4552 probe_ent->port[p].ctl_addr =
4553 pci_resource_start(pdev, 3) | ATA_PCI_CTL_OFS;
4554 probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4) + 8;
4555 ata_std_ports(&probe_ent->port[p]);
4556 p++;
4557 }
1da177e4 4558
47a86593 4559 probe_ent->n_ports = p;
1da177e4
LT
4560 return probe_ent;
4561}
4562
47a86593 4563static struct ata_probe_ent *ata_pci_init_legacy_port(struct pci_dev *pdev, struct ata_port_info **port, int port_num)
1da177e4 4564{
47a86593 4565 struct ata_probe_ent *probe_ent;
1da177e4
LT
4566
4567 probe_ent = ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
4568 if (!probe_ent)
4569 return NULL;
1da177e4 4570
1da177e4 4571 probe_ent->legacy_mode = 1;
47a86593
AC
4572 probe_ent->n_ports = 1;
4573 probe_ent->hard_port_no = port_num;
4574
4575 switch(port_num)
4576 {
4577 case 0:
4578 probe_ent->irq = 14;
4579 probe_ent->port[0].cmd_addr = 0x1f0;
4580 probe_ent->port[0].altstatus_addr =
4581 probe_ent->port[0].ctl_addr = 0x3f6;
4582 break;
4583 case 1:
4584 probe_ent->irq = 15;
4585 probe_ent->port[0].cmd_addr = 0x170;
4586 probe_ent->port[0].altstatus_addr =
4587 probe_ent->port[0].ctl_addr = 0x376;
4588 break;
4589 }
4590 probe_ent->port[0].bmdma_addr = pci_resource_start(pdev, 4) + 8 * port_num;
1da177e4 4591 ata_std_ports(&probe_ent->port[0]);
1da177e4
LT
4592 return probe_ent;
4593}
4594
4595/**
4596 * ata_pci_init_one - Initialize/register PCI IDE host controller
4597 * @pdev: Controller to be initialized
4598 * @port_info: Information from low-level host driver
4599 * @n_ports: Number of ports attached to host controller
4600 *
0baab86b
EF
4601 * This is a helper function which can be called from a driver's
4602 * xxx_init_one() probe function if the hardware uses traditional
4603 * IDE taskfile registers.
4604 *
4605 * This function calls pci_enable_device(), reserves its register
4606 * regions, sets the dma mask, enables bus master mode, and calls
4607 * ata_device_add()
4608 *
1da177e4
LT
4609 * LOCKING:
4610 * Inherited from PCI layer (may sleep).
4611 *
4612 * RETURNS:
0cba632b 4613 * Zero on success, negative on errno-based value on error.
1da177e4
LT
4614 */
4615
4616int ata_pci_init_one (struct pci_dev *pdev, struct ata_port_info **port_info,
4617 unsigned int n_ports)
4618{
47a86593 4619 struct ata_probe_ent *probe_ent = NULL, *probe_ent2 = NULL;
1da177e4
LT
4620 struct ata_port_info *port[2];
4621 u8 tmp8, mask;
4622 unsigned int legacy_mode = 0;
4623 int disable_dev_on_err = 1;
4624 int rc;
4625
4626 DPRINTK("ENTER\n");
4627
4628 port[0] = port_info[0];
4629 if (n_ports > 1)
4630 port[1] = port_info[1];
4631 else
4632 port[1] = port[0];
4633
4634 if ((port[0]->host_flags & ATA_FLAG_NO_LEGACY) == 0
4635 && (pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
47a86593 4636 /* TODO: What if one channel is in native mode ... */
1da177e4
LT
4637 pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
4638 mask = (1 << 2) | (1 << 0);
4639 if ((tmp8 & mask) != mask)
4640 legacy_mode = (1 << 3);
4641 }
4642
4643 /* FIXME... */
47a86593
AC
4644 if ((!legacy_mode) && (n_ports > 2)) {
4645 printk(KERN_ERR "ata: BUG: native mode, n_ports > 2\n");
4646 n_ports = 2;
4647 /* For now */
1da177e4
LT
4648 }
4649
47a86593
AC
4650 /* FIXME: Really for ATA it isn't safe because the device may be
4651 multi-purpose and we want to leave it alone if it was already
4652 enabled. Secondly for shared use as Arjan says we want refcounting
4653
4654 Checking dev->is_enabled is insufficient as this is not set at
4655 boot for the primary video which is BIOS enabled
4656 */
4657
1da177e4
LT
4658 rc = pci_enable_device(pdev);
4659 if (rc)
4660 return rc;
4661
4662 rc = pci_request_regions(pdev, DRV_NAME);
4663 if (rc) {
4664 disable_dev_on_err = 0;
4665 goto err_out;
4666 }
4667
47a86593 4668 /* FIXME: Should use platform specific mappers for legacy port ranges */
1da177e4
LT
4669 if (legacy_mode) {
4670 if (!request_region(0x1f0, 8, "libata")) {
4671 struct resource *conflict, res;
4672 res.start = 0x1f0;
4673 res.end = 0x1f0 + 8 - 1;
4674 conflict = ____request_resource(&ioport_resource, &res);
4675 if (!strcmp(conflict->name, "libata"))
4676 legacy_mode |= (1 << 0);
4677 else {
4678 disable_dev_on_err = 0;
4679 printk(KERN_WARNING "ata: 0x1f0 IDE port busy\n");
4680 }
4681 } else
4682 legacy_mode |= (1 << 0);
4683
4684 if (!request_region(0x170, 8, "libata")) {
4685 struct resource *conflict, res;
4686 res.start = 0x170;
4687 res.end = 0x170 + 8 - 1;
4688 conflict = ____request_resource(&ioport_resource, &res);
4689 if (!strcmp(conflict->name, "libata"))
4690 legacy_mode |= (1 << 1);
4691 else {
4692 disable_dev_on_err = 0;
4693 printk(KERN_WARNING "ata: 0x170 IDE port busy\n");
4694 }
4695 } else
4696 legacy_mode |= (1 << 1);
4697 }
4698
4699 /* we have legacy mode, but all ports are unavailable */
4700 if (legacy_mode == (1 << 3)) {
4701 rc = -EBUSY;
4702 goto err_out_regions;
4703 }
4704
4705 rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
4706 if (rc)
4707 goto err_out_regions;
4708 rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
4709 if (rc)
4710 goto err_out_regions;
4711
4712 if (legacy_mode) {
47a86593
AC
4713 if (legacy_mode & (1 << 0))
4714 probe_ent = ata_pci_init_legacy_port(pdev, port, 0);
4715 if (legacy_mode & (1 << 1))
4716 probe_ent2 = ata_pci_init_legacy_port(pdev, port, 1);
4717 } else {
4718 if (n_ports == 2)
4719 probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY | ATA_PORT_SECONDARY);
4720 else
4721 probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY);
4722 }
4723 if (!probe_ent && !probe_ent2) {
1da177e4
LT
4724 rc = -ENOMEM;
4725 goto err_out_regions;
4726 }
4727
4728 pci_set_master(pdev);
4729
4730 /* FIXME: check ata_device_add return */
4731 if (legacy_mode) {
4732 if (legacy_mode & (1 << 0))
4733 ata_device_add(probe_ent);
4734 if (legacy_mode & (1 << 1))
4735 ata_device_add(probe_ent2);
4736 } else
4737 ata_device_add(probe_ent);
4738
4739 kfree(probe_ent);
4740 kfree(probe_ent2);
4741
4742 return 0;
4743
4744err_out_regions:
4745 if (legacy_mode & (1 << 0))
4746 release_region(0x1f0, 8);
4747 if (legacy_mode & (1 << 1))
4748 release_region(0x170, 8);
4749 pci_release_regions(pdev);
4750err_out:
4751 if (disable_dev_on_err)
4752 pci_disable_device(pdev);
4753 return rc;
4754}
4755
4756/**
4757 * ata_pci_remove_one - PCI layer callback for device removal
4758 * @pdev: PCI device that was removed
4759 *
4760 * PCI layer indicates to libata via this hook that
6f0ef4fa 4761 * hot-unplug or module unload event has occurred.
1da177e4
LT
4762 * Handle this by unregistering all objects associated
4763 * with this PCI device. Free those objects. Then finally
4764 * release PCI resources and disable device.
4765 *
4766 * LOCKING:
4767 * Inherited from PCI layer (may sleep).
4768 */
4769
4770void ata_pci_remove_one (struct pci_dev *pdev)
4771{
4772 struct device *dev = pci_dev_to_dev(pdev);
4773 struct ata_host_set *host_set = dev_get_drvdata(dev);
1da177e4 4774
17b14451 4775 ata_host_set_remove(host_set);
1da177e4
LT
4776 pci_release_regions(pdev);
4777 pci_disable_device(pdev);
4778 dev_set_drvdata(dev, NULL);
4779}
4780
4781/* move to PCI subsystem */
057ace5e 4782int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
1da177e4
LT
4783{
4784 unsigned long tmp = 0;
4785
4786 switch (bits->width) {
4787 case 1: {
4788 u8 tmp8 = 0;
4789 pci_read_config_byte(pdev, bits->reg, &tmp8);
4790 tmp = tmp8;
4791 break;
4792 }
4793 case 2: {
4794 u16 tmp16 = 0;
4795 pci_read_config_word(pdev, bits->reg, &tmp16);
4796 tmp = tmp16;
4797 break;
4798 }
4799 case 4: {
4800 u32 tmp32 = 0;
4801 pci_read_config_dword(pdev, bits->reg, &tmp32);
4802 tmp = tmp32;
4803 break;
4804 }
4805
4806 default:
4807 return -EINVAL;
4808 }
4809
4810 tmp &= bits->mask;
4811
4812 return (tmp == bits->val) ? 1 : 0;
4813}
4814#endif /* CONFIG_PCI */
4815
4816
1da177e4
LT
4817static int __init ata_init(void)
4818{
4819 ata_wq = create_workqueue("ata");
4820 if (!ata_wq)
4821 return -ENOMEM;
4822
4823 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
4824 return 0;
4825}
4826
4827static void __exit ata_exit(void)
4828{
4829 destroy_workqueue(ata_wq);
4830}
4831
4832module_init(ata_init);
4833module_exit(ata_exit);
4834
67846b30
JG
4835static unsigned long ratelimit_time;
4836static spinlock_t ata_ratelimit_lock = SPIN_LOCK_UNLOCKED;
4837
4838int ata_ratelimit(void)
4839{
4840 int rc;
4841 unsigned long flags;
4842
4843 spin_lock_irqsave(&ata_ratelimit_lock, flags);
4844
4845 if (time_after(jiffies, ratelimit_time)) {
4846 rc = 1;
4847 ratelimit_time = jiffies + (HZ/5);
4848 } else
4849 rc = 0;
4850
4851 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
4852
4853 return rc;
4854}
4855
1da177e4
LT
4856/*
4857 * libata is essentially a library of internal helper functions for
4858 * low-level ATA host controller drivers. As such, the API/ABI is
4859 * likely to change as new drivers are added and updated.
4860 * Do not depend on ABI/API stability.
4861 */
4862
4863EXPORT_SYMBOL_GPL(ata_std_bios_param);
4864EXPORT_SYMBOL_GPL(ata_std_ports);
4865EXPORT_SYMBOL_GPL(ata_device_add);
17b14451 4866EXPORT_SYMBOL_GPL(ata_host_set_remove);
1da177e4
LT
4867EXPORT_SYMBOL_GPL(ata_sg_init);
4868EXPORT_SYMBOL_GPL(ata_sg_init_one);
4869EXPORT_SYMBOL_GPL(ata_qc_complete);
4870EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
4871EXPORT_SYMBOL_GPL(ata_eng_timeout);
4872EXPORT_SYMBOL_GPL(ata_tf_load);
4873EXPORT_SYMBOL_GPL(ata_tf_read);
4874EXPORT_SYMBOL_GPL(ata_noop_dev_select);
4875EXPORT_SYMBOL_GPL(ata_std_dev_select);
4876EXPORT_SYMBOL_GPL(ata_tf_to_fis);
4877EXPORT_SYMBOL_GPL(ata_tf_from_fis);
4878EXPORT_SYMBOL_GPL(ata_check_status);
4879EXPORT_SYMBOL_GPL(ata_altstatus);
4880EXPORT_SYMBOL_GPL(ata_chk_err);
4881EXPORT_SYMBOL_GPL(ata_exec_command);
4882EXPORT_SYMBOL_GPL(ata_port_start);
4883EXPORT_SYMBOL_GPL(ata_port_stop);
aa8f0dc6 4884EXPORT_SYMBOL_GPL(ata_host_stop);
1da177e4
LT
4885EXPORT_SYMBOL_GPL(ata_interrupt);
4886EXPORT_SYMBOL_GPL(ata_qc_prep);
4887EXPORT_SYMBOL_GPL(ata_bmdma_setup);
4888EXPORT_SYMBOL_GPL(ata_bmdma_start);
4889EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
4890EXPORT_SYMBOL_GPL(ata_bmdma_status);
4891EXPORT_SYMBOL_GPL(ata_bmdma_stop);
4892EXPORT_SYMBOL_GPL(ata_port_probe);
4893EXPORT_SYMBOL_GPL(sata_phy_reset);
4894EXPORT_SYMBOL_GPL(__sata_phy_reset);
4895EXPORT_SYMBOL_GPL(ata_bus_reset);
4896EXPORT_SYMBOL_GPL(ata_port_disable);
67846b30 4897EXPORT_SYMBOL_GPL(ata_ratelimit);
1da177e4
LT
4898EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
4899EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
4900EXPORT_SYMBOL_GPL(ata_scsi_error);
4901EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
4902EXPORT_SYMBOL_GPL(ata_scsi_release);
4903EXPORT_SYMBOL_GPL(ata_host_intr);
4904EXPORT_SYMBOL_GPL(ata_dev_classify);
4905EXPORT_SYMBOL_GPL(ata_dev_id_string);
6f2f3812 4906EXPORT_SYMBOL_GPL(ata_dev_config);
1da177e4
LT
4907EXPORT_SYMBOL_GPL(ata_scsi_simulate);
4908
452503f9
AC
4909EXPORT_SYMBOL_GPL(ata_timing_compute);
4910EXPORT_SYMBOL_GPL(ata_timing_merge);
4911
1da177e4
LT
4912#ifdef CONFIG_PCI
4913EXPORT_SYMBOL_GPL(pci_test_config_bits);
374b1873 4914EXPORT_SYMBOL_GPL(ata_pci_host_stop);
1da177e4
LT
4915EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
4916EXPORT_SYMBOL_GPL(ata_pci_init_one);
4917EXPORT_SYMBOL_GPL(ata_pci_remove_one);
4918#endif /* CONFIG_PCI */
This page took 0.313835 seconds and 5 git commands to generate.