blob: b3aedb033465898b45eaf6b186dd0ab7809ce182 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Jeff Garzikaf36d7f2005-08-28 20:18:39 -04002 * 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 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 */
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>
Jeff Garzik67846b32005-10-05 02:58:32 -040051#include <linux/jiffies.h>
David Hardeman378f0582005-09-17 17:55:31 +100052#include <linux/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <scsi/scsi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include "scsi_priv.h"
Jeff Garzik193515d2005-11-07 00:59:37 -050055#include <scsi/scsi_cmnd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <scsi/scsi_host.h>
57#include <linux/libata.h>
58#include <asm/io.h>
59#include <asm/semaphore.h>
60#include <asm/byteorder.h>
61
62#include "libata.h"
63
64static unsigned int ata_busy_sleep (struct ata_port *ap,
65 unsigned long tmout_pat,
66 unsigned long tmout);
Albert Lee59a10b12005-10-12 15:09:42 +080067static void ata_dev_reread_id(struct ata_port *ap, struct ata_device *dev);
Albert Lee8bf62ece2005-05-12 15:29:42 -040068static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069static void ata_set_mode(struct ata_port *ap);
70static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev);
Jeff Garzik057ace52005-10-22 14:27:05 -040071static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072static int fgb(u32 bitmap);
Jeff Garzik057ace52005-10-22 14:27:05 -040073static int ata_choose_xfer_mode(const struct ata_port *ap,
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 u8 *xfer_mode_out,
75 unsigned int *xfer_shift_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076static void __ata_qc_complete(struct ata_queued_cmd *qc);
77
78static unsigned int ata_unique_id = 1;
79static struct workqueue_struct *ata_wq;
80
Jeff Garzik1623c812005-08-30 03:37:42 -040081int atapi_enabled = 0;
82module_param(atapi_enabled, int, 0444);
83MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)");
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085MODULE_AUTHOR("Jeff Garzik");
86MODULE_DESCRIPTION("Library module for ATA devices");
87MODULE_LICENSE("GPL");
88MODULE_VERSION(DRV_VERSION);
89
90/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -040091 * ata_tf_load_pio - send taskfile registers to host controller
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 * @ap: Port to which output is sent
93 * @tf: ATA taskfile register set
94 *
95 * Outputs ATA taskfile to standard ATA host controller.
96 *
97 * LOCKING:
98 * Inherited from caller.
99 */
100
Jeff Garzik057ace52005-10-22 14:27:05 -0400101static void ata_tf_load_pio(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
103 struct ata_ioports *ioaddr = &ap->ioaddr;
104 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
105
106 if (tf->ctl != ap->last_ctl) {
107 outb(tf->ctl, ioaddr->ctl_addr);
108 ap->last_ctl = tf->ctl;
109 ata_wait_idle(ap);
110 }
111
112 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
113 outb(tf->hob_feature, ioaddr->feature_addr);
114 outb(tf->hob_nsect, ioaddr->nsect_addr);
115 outb(tf->hob_lbal, ioaddr->lbal_addr);
116 outb(tf->hob_lbam, ioaddr->lbam_addr);
117 outb(tf->hob_lbah, ioaddr->lbah_addr);
118 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
119 tf->hob_feature,
120 tf->hob_nsect,
121 tf->hob_lbal,
122 tf->hob_lbam,
123 tf->hob_lbah);
124 }
125
126 if (is_addr) {
127 outb(tf->feature, ioaddr->feature_addr);
128 outb(tf->nsect, ioaddr->nsect_addr);
129 outb(tf->lbal, ioaddr->lbal_addr);
130 outb(tf->lbam, ioaddr->lbam_addr);
131 outb(tf->lbah, ioaddr->lbah_addr);
132 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
133 tf->feature,
134 tf->nsect,
135 tf->lbal,
136 tf->lbam,
137 tf->lbah);
138 }
139
140 if (tf->flags & ATA_TFLAG_DEVICE) {
141 outb(tf->device, ioaddr->device_addr);
142 VPRINTK("device 0x%X\n", tf->device);
143 }
144
145 ata_wait_idle(ap);
146}
147
148/**
149 * ata_tf_load_mmio - send taskfile registers to host controller
150 * @ap: Port to which output is sent
151 * @tf: ATA taskfile register set
152 *
153 * Outputs ATA taskfile to standard ATA host controller using MMIO.
154 *
155 * LOCKING:
156 * Inherited from caller.
157 */
158
Jeff Garzik057ace52005-10-22 14:27:05 -0400159static void ata_tf_load_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
161 struct ata_ioports *ioaddr = &ap->ioaddr;
162 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
163
164 if (tf->ctl != ap->last_ctl) {
165 writeb(tf->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
166 ap->last_ctl = tf->ctl;
167 ata_wait_idle(ap);
168 }
169
170 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
171 writeb(tf->hob_feature, (void __iomem *) ioaddr->feature_addr);
172 writeb(tf->hob_nsect, (void __iomem *) ioaddr->nsect_addr);
173 writeb(tf->hob_lbal, (void __iomem *) ioaddr->lbal_addr);
174 writeb(tf->hob_lbam, (void __iomem *) ioaddr->lbam_addr);
175 writeb(tf->hob_lbah, (void __iomem *) ioaddr->lbah_addr);
176 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
177 tf->hob_feature,
178 tf->hob_nsect,
179 tf->hob_lbal,
180 tf->hob_lbam,
181 tf->hob_lbah);
182 }
183
184 if (is_addr) {
185 writeb(tf->feature, (void __iomem *) ioaddr->feature_addr);
186 writeb(tf->nsect, (void __iomem *) ioaddr->nsect_addr);
187 writeb(tf->lbal, (void __iomem *) ioaddr->lbal_addr);
188 writeb(tf->lbam, (void __iomem *) ioaddr->lbam_addr);
189 writeb(tf->lbah, (void __iomem *) ioaddr->lbah_addr);
190 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
191 tf->feature,
192 tf->nsect,
193 tf->lbal,
194 tf->lbam,
195 tf->lbah);
196 }
197
198 if (tf->flags & ATA_TFLAG_DEVICE) {
199 writeb(tf->device, (void __iomem *) ioaddr->device_addr);
200 VPRINTK("device 0x%X\n", tf->device);
201 }
202
203 ata_wait_idle(ap);
204}
205
Edward Falk0baab862005-06-02 18:17:13 -0400206
207/**
208 * ata_tf_load - send taskfile registers to host controller
209 * @ap: Port to which output is sent
210 * @tf: ATA taskfile register set
211 *
212 * Outputs ATA taskfile to standard ATA host controller using MMIO
213 * or PIO as indicated by the ATA_FLAG_MMIO flag.
214 * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
215 * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
216 * hob_lbal, hob_lbam, and hob_lbah.
217 *
218 * This function waits for idle (!BUSY and !DRQ) after writing
219 * registers. If the control register has a new value, this
220 * function also waits for idle after writing control and before
221 * writing the remaining registers.
222 *
223 * May be used as the tf_load() entry in ata_port_operations.
224 *
225 * LOCKING:
226 * Inherited from caller.
227 */
Jeff Garzik057ace52005-10-22 14:27:05 -0400228void ata_tf_load(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
230 if (ap->flags & ATA_FLAG_MMIO)
231 ata_tf_load_mmio(ap, tf);
232 else
233 ata_tf_load_pio(ap, tf);
234}
235
236/**
Edward Falk0baab862005-06-02 18:17:13 -0400237 * ata_exec_command_pio - issue ATA command to host controller
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 * @ap: port to which command is being issued
239 * @tf: ATA taskfile register set
240 *
Edward Falk0baab862005-06-02 18:17:13 -0400241 * Issues PIO write to ATA command register, with proper
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 * synchronization with interrupt handler / other threads.
243 *
244 * LOCKING:
245 * spin_lock_irqsave(host_set lock)
246 */
247
Jeff Garzik057ace52005-10-22 14:27:05 -0400248static void ata_exec_command_pio(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
250 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
251
252 outb(tf->command, ap->ioaddr.command_addr);
253 ata_pause(ap);
254}
255
256
257/**
258 * ata_exec_command_mmio - issue ATA command to host controller
259 * @ap: port to which command is being issued
260 * @tf: ATA taskfile register set
261 *
262 * Issues MMIO write to ATA command register, with proper
263 * synchronization with interrupt handler / other threads.
264 *
265 * LOCKING:
266 * spin_lock_irqsave(host_set lock)
267 */
268
Jeff Garzik057ace52005-10-22 14:27:05 -0400269static void ata_exec_command_mmio(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
271 DPRINTK("ata%u: cmd 0x%X\n", ap->id, tf->command);
272
273 writeb(tf->command, (void __iomem *) ap->ioaddr.command_addr);
274 ata_pause(ap);
275}
276
Edward Falk0baab862005-06-02 18:17:13 -0400277
278/**
279 * ata_exec_command - issue ATA command to host controller
280 * @ap: port to which command is being issued
281 * @tf: ATA taskfile register set
282 *
283 * Issues PIO/MMIO write to ATA command register, with proper
284 * synchronization with interrupt handler / other threads.
285 *
286 * LOCKING:
287 * spin_lock_irqsave(host_set lock)
288 */
Jeff Garzik057ace52005-10-22 14:27:05 -0400289void ata_exec_command(struct ata_port *ap, const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
291 if (ap->flags & ATA_FLAG_MMIO)
292 ata_exec_command_mmio(ap, tf);
293 else
294 ata_exec_command_pio(ap, tf);
295}
296
297/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 * ata_tf_to_host - issue ATA taskfile to host controller
299 * @ap: port to which command is being issued
300 * @tf: ATA taskfile register set
301 *
302 * Issues ATA taskfile register set to ATA host controller,
303 * with proper synchronization with interrupt handler and
304 * other threads.
305 *
306 * LOCKING:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 * spin_lock_irqsave(host_set lock)
308 */
309
Jeff Garzike5338252005-10-30 21:37:17 -0500310static inline void ata_tf_to_host(struct ata_port *ap,
311 const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312{
313 ap->ops->tf_load(ap, tf);
314 ap->ops->exec_command(ap, tf);
315}
316
317/**
Edward Falk0baab862005-06-02 18:17:13 -0400318 * ata_tf_read_pio - input device's ATA taskfile shadow registers
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 * @ap: Port from which input is read
320 * @tf: ATA taskfile register set for storing input
321 *
322 * Reads ATA taskfile registers for currently-selected device
323 * into @tf.
324 *
325 * LOCKING:
326 * Inherited from caller.
327 */
328
329static void ata_tf_read_pio(struct ata_port *ap, struct ata_taskfile *tf)
330{
331 struct ata_ioports *ioaddr = &ap->ioaddr;
332
Jeff Garzikac19bff2005-10-29 13:58:21 -0400333 tf->command = ata_check_status(ap);
Jeff Garzik0169e282005-10-29 21:25:10 -0400334 tf->feature = inb(ioaddr->error_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 tf->nsect = inb(ioaddr->nsect_addr);
336 tf->lbal = inb(ioaddr->lbal_addr);
337 tf->lbam = inb(ioaddr->lbam_addr);
338 tf->lbah = inb(ioaddr->lbah_addr);
339 tf->device = inb(ioaddr->device_addr);
340
341 if (tf->flags & ATA_TFLAG_LBA48) {
342 outb(tf->ctl | ATA_HOB, ioaddr->ctl_addr);
343 tf->hob_feature = inb(ioaddr->error_addr);
344 tf->hob_nsect = inb(ioaddr->nsect_addr);
345 tf->hob_lbal = inb(ioaddr->lbal_addr);
346 tf->hob_lbam = inb(ioaddr->lbam_addr);
347 tf->hob_lbah = inb(ioaddr->lbah_addr);
348 }
349}
350
351/**
352 * ata_tf_read_mmio - input device's ATA taskfile shadow registers
353 * @ap: Port from which input is read
354 * @tf: ATA taskfile register set for storing input
355 *
356 * Reads ATA taskfile registers for currently-selected device
357 * into @tf via MMIO.
358 *
359 * LOCKING:
360 * Inherited from caller.
361 */
362
363static void ata_tf_read_mmio(struct ata_port *ap, struct ata_taskfile *tf)
364{
365 struct ata_ioports *ioaddr = &ap->ioaddr;
366
Jeff Garzikac19bff2005-10-29 13:58:21 -0400367 tf->command = ata_check_status(ap);
Jeff Garzik0169e282005-10-29 21:25:10 -0400368 tf->feature = readb((void __iomem *)ioaddr->error_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 tf->nsect = readb((void __iomem *)ioaddr->nsect_addr);
370 tf->lbal = readb((void __iomem *)ioaddr->lbal_addr);
371 tf->lbam = readb((void __iomem *)ioaddr->lbam_addr);
372 tf->lbah = readb((void __iomem *)ioaddr->lbah_addr);
373 tf->device = readb((void __iomem *)ioaddr->device_addr);
374
375 if (tf->flags & ATA_TFLAG_LBA48) {
376 writeb(tf->ctl | ATA_HOB, (void __iomem *) ap->ioaddr.ctl_addr);
377 tf->hob_feature = readb((void __iomem *)ioaddr->error_addr);
378 tf->hob_nsect = readb((void __iomem *)ioaddr->nsect_addr);
379 tf->hob_lbal = readb((void __iomem *)ioaddr->lbal_addr);
380 tf->hob_lbam = readb((void __iomem *)ioaddr->lbam_addr);
381 tf->hob_lbah = readb((void __iomem *)ioaddr->lbah_addr);
382 }
383}
384
Edward Falk0baab862005-06-02 18:17:13 -0400385
386/**
387 * ata_tf_read - input device's ATA taskfile shadow registers
388 * @ap: Port from which input is read
389 * @tf: ATA taskfile register set for storing input
390 *
391 * Reads ATA taskfile registers for currently-selected device
392 * into @tf.
393 *
394 * Reads nsect, lbal, lbam, lbah, and device. If ATA_TFLAG_LBA48
395 * is set, also reads the hob registers.
396 *
397 * May be used as the tf_read() entry in ata_port_operations.
398 *
399 * LOCKING:
400 * Inherited from caller.
401 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402void ata_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
403{
404 if (ap->flags & ATA_FLAG_MMIO)
405 ata_tf_read_mmio(ap, tf);
406 else
407 ata_tf_read_pio(ap, tf);
408}
409
410/**
411 * ata_check_status_pio - Read device status reg & clear interrupt
412 * @ap: port where the device is
413 *
414 * Reads ATA taskfile status register for currently-selected device
Edward Falk0baab862005-06-02 18:17:13 -0400415 * and return its value. This also clears pending interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 * from this device
417 *
418 * LOCKING:
419 * Inherited from caller.
420 */
421static u8 ata_check_status_pio(struct ata_port *ap)
422{
423 return inb(ap->ioaddr.status_addr);
424}
425
426/**
427 * ata_check_status_mmio - Read device status reg & clear interrupt
428 * @ap: port where the device is
429 *
430 * Reads ATA taskfile status register for currently-selected device
Edward Falk0baab862005-06-02 18:17:13 -0400431 * via MMIO and return its value. This also clears pending interrupts
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 * from this device
433 *
434 * LOCKING:
435 * Inherited from caller.
436 */
437static u8 ata_check_status_mmio(struct ata_port *ap)
438{
439 return readb((void __iomem *) ap->ioaddr.status_addr);
440}
441
Edward Falk0baab862005-06-02 18:17:13 -0400442
443/**
444 * ata_check_status - Read device status reg & clear interrupt
445 * @ap: port where the device is
446 *
447 * Reads ATA taskfile status register for currently-selected device
448 * and return its value. This also clears pending interrupts
449 * from this device
450 *
451 * May be used as the check_status() entry in ata_port_operations.
452 *
453 * LOCKING:
454 * Inherited from caller.
455 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456u8 ata_check_status(struct ata_port *ap)
457{
458 if (ap->flags & ATA_FLAG_MMIO)
459 return ata_check_status_mmio(ap);
460 return ata_check_status_pio(ap);
461}
462
Edward Falk0baab862005-06-02 18:17:13 -0400463
464/**
465 * ata_altstatus - Read device alternate status reg
466 * @ap: port where the device is
467 *
468 * Reads ATA taskfile alternate status register for
469 * currently-selected device and return its value.
470 *
471 * Note: may NOT be used as the check_altstatus() entry in
472 * ata_port_operations.
473 *
474 * LOCKING:
475 * Inherited from caller.
476 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477u8 ata_altstatus(struct ata_port *ap)
478{
479 if (ap->ops->check_altstatus)
480 return ap->ops->check_altstatus(ap);
481
482 if (ap->flags & ATA_FLAG_MMIO)
483 return readb((void __iomem *)ap->ioaddr.altstatus_addr);
484 return inb(ap->ioaddr.altstatus_addr);
485}
486
Edward Falk0baab862005-06-02 18:17:13 -0400487
488/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
490 * @tf: Taskfile to convert
491 * @fis: Buffer into which data will output
492 * @pmp: Port multiplier port
493 *
494 * Converts a standard ATA taskfile to a Serial ATA
495 * FIS structure (Register - Host to Device).
496 *
497 * LOCKING:
498 * Inherited from caller.
499 */
500
Jeff Garzik057ace52005-10-22 14:27:05 -0400501void ata_tf_to_fis(const struct ata_taskfile *tf, u8 *fis, u8 pmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
503 fis[0] = 0x27; /* Register - Host to Device FIS */
504 fis[1] = (pmp & 0xf) | (1 << 7); /* Port multiplier number,
505 bit 7 indicates Command FIS */
506 fis[2] = tf->command;
507 fis[3] = tf->feature;
508
509 fis[4] = tf->lbal;
510 fis[5] = tf->lbam;
511 fis[6] = tf->lbah;
512 fis[7] = tf->device;
513
514 fis[8] = tf->hob_lbal;
515 fis[9] = tf->hob_lbam;
516 fis[10] = tf->hob_lbah;
517 fis[11] = tf->hob_feature;
518
519 fis[12] = tf->nsect;
520 fis[13] = tf->hob_nsect;
521 fis[14] = 0;
522 fis[15] = tf->ctl;
523
524 fis[16] = 0;
525 fis[17] = 0;
526 fis[18] = 0;
527 fis[19] = 0;
528}
529
530/**
531 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
532 * @fis: Buffer from which data will be input
533 * @tf: Taskfile to output
534 *
Mark Lorde12a1be2005-11-12 18:55:45 -0500535 * Converts a serial ATA FIS structure to a standard ATA taskfile.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 *
537 * LOCKING:
538 * Inherited from caller.
539 */
540
Jeff Garzik057ace52005-10-22 14:27:05 -0400541void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
543 tf->command = fis[2]; /* status */
544 tf->feature = fis[3]; /* error */
545
546 tf->lbal = fis[4];
547 tf->lbam = fis[5];
548 tf->lbah = fis[6];
549 tf->device = fis[7];
550
551 tf->hob_lbal = fis[8];
552 tf->hob_lbam = fis[9];
553 tf->hob_lbah = fis[10];
554
555 tf->nsect = fis[12];
556 tf->hob_nsect = fis[13];
557}
558
Albert Lee8cbd6df2005-10-12 15:06:27 +0800559static const u8 ata_rw_cmds[] = {
560 /* pio multi */
561 ATA_CMD_READ_MULTI,
562 ATA_CMD_WRITE_MULTI,
563 ATA_CMD_READ_MULTI_EXT,
564 ATA_CMD_WRITE_MULTI_EXT,
565 /* pio */
566 ATA_CMD_PIO_READ,
567 ATA_CMD_PIO_WRITE,
568 ATA_CMD_PIO_READ_EXT,
569 ATA_CMD_PIO_WRITE_EXT,
570 /* dma */
571 ATA_CMD_READ,
572 ATA_CMD_WRITE,
573 ATA_CMD_READ_EXT,
574 ATA_CMD_WRITE_EXT
575};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577/**
Albert Lee8cbd6df2005-10-12 15:06:27 +0800578 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
579 * @qc: command to examine and configure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 *
Albert Lee8cbd6df2005-10-12 15:06:27 +0800581 * Examine the device configuration and tf->flags to calculate
582 * the proper read/write commands and protocol to use.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 *
584 * LOCKING:
585 * caller.
586 */
Albert Lee8cbd6df2005-10-12 15:06:27 +0800587void ata_rwcmd_protocol(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
Albert Lee8cbd6df2005-10-12 15:06:27 +0800589 struct ata_taskfile *tf = &qc->tf;
590 struct ata_device *dev = qc->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Albert Lee8cbd6df2005-10-12 15:06:27 +0800592 int index, lba48, write;
593
594 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
595 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Albert Lee8cbd6df2005-10-12 15:06:27 +0800597 if (dev->flags & ATA_DFLAG_PIO) {
598 tf->protocol = ATA_PROT_PIO;
599 index = dev->multi_count ? 0 : 4;
600 } else {
601 tf->protocol = ATA_PROT_DMA;
602 index = 8;
603 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Albert Lee8cbd6df2005-10-12 15:06:27 +0800605 tf->command = ata_rw_cmds[index + lba48 + write];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606}
607
Arjan van de Ven98ac62d2005-11-28 10:06:23 +0100608static const char * const xfer_mode_str[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 "UDMA/16",
610 "UDMA/25",
611 "UDMA/33",
612 "UDMA/44",
613 "UDMA/66",
614 "UDMA/100",
615 "UDMA/133",
616 "UDMA7",
617 "MWDMA0",
618 "MWDMA1",
619 "MWDMA2",
620 "PIO0",
621 "PIO1",
622 "PIO2",
623 "PIO3",
624 "PIO4",
625};
626
627/**
628 * ata_udma_string - convert UDMA bit offset to string
629 * @mask: mask of bits supported; only highest bit counts.
630 *
631 * Determine string which represents the highest speed
632 * (highest bit in @udma_mask).
633 *
634 * LOCKING:
635 * None.
636 *
637 * RETURNS:
638 * Constant C string representing highest speed listed in
639 * @udma_mask, or the constant C string "<n/a>".
640 */
641
642static const char *ata_mode_string(unsigned int mask)
643{
644 int i;
645
646 for (i = 7; i >= 0; i--)
647 if (mask & (1 << i))
648 goto out;
649 for (i = ATA_SHIFT_MWDMA + 2; i >= ATA_SHIFT_MWDMA; i--)
650 if (mask & (1 << i))
651 goto out;
652 for (i = ATA_SHIFT_PIO + 4; i >= ATA_SHIFT_PIO; i--)
653 if (mask & (1 << i))
654 goto out;
655
656 return "<n/a>";
657
658out:
659 return xfer_mode_str[i];
660}
661
662/**
663 * ata_pio_devchk - PATA device presence detection
664 * @ap: ATA channel to examine
665 * @device: Device to examine (starting at zero)
666 *
667 * This technique was originally described in
668 * Hale Landis's ATADRVR (www.ata-atapi.com), and
669 * later found its way into the ATA/ATAPI spec.
670 *
671 * Write a pattern to the ATA shadow registers,
672 * and if a device is present, it will respond by
673 * correctly storing and echoing back the
674 * ATA shadow register contents.
675 *
676 * LOCKING:
677 * caller.
678 */
679
680static unsigned int ata_pio_devchk(struct ata_port *ap,
681 unsigned int device)
682{
683 struct ata_ioports *ioaddr = &ap->ioaddr;
684 u8 nsect, lbal;
685
686 ap->ops->dev_select(ap, device);
687
688 outb(0x55, ioaddr->nsect_addr);
689 outb(0xaa, ioaddr->lbal_addr);
690
691 outb(0xaa, ioaddr->nsect_addr);
692 outb(0x55, ioaddr->lbal_addr);
693
694 outb(0x55, ioaddr->nsect_addr);
695 outb(0xaa, ioaddr->lbal_addr);
696
697 nsect = inb(ioaddr->nsect_addr);
698 lbal = inb(ioaddr->lbal_addr);
699
700 if ((nsect == 0x55) && (lbal == 0xaa))
701 return 1; /* we found a device */
702
703 return 0; /* nothing found */
704}
705
706/**
707 * ata_mmio_devchk - PATA device presence detection
708 * @ap: ATA channel to examine
709 * @device: Device to examine (starting at zero)
710 *
711 * This technique was originally described in
712 * Hale Landis's ATADRVR (www.ata-atapi.com), and
713 * later found its way into the ATA/ATAPI spec.
714 *
715 * Write a pattern to the ATA shadow registers,
716 * and if a device is present, it will respond by
717 * correctly storing and echoing back the
718 * ATA shadow register contents.
719 *
720 * LOCKING:
721 * caller.
722 */
723
724static unsigned int ata_mmio_devchk(struct ata_port *ap,
725 unsigned int device)
726{
727 struct ata_ioports *ioaddr = &ap->ioaddr;
728 u8 nsect, lbal;
729
730 ap->ops->dev_select(ap, device);
731
732 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
733 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
734
735 writeb(0xaa, (void __iomem *) ioaddr->nsect_addr);
736 writeb(0x55, (void __iomem *) ioaddr->lbal_addr);
737
738 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
739 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
740
741 nsect = readb((void __iomem *) ioaddr->nsect_addr);
742 lbal = readb((void __iomem *) ioaddr->lbal_addr);
743
744 if ((nsect == 0x55) && (lbal == 0xaa))
745 return 1; /* we found a device */
746
747 return 0; /* nothing found */
748}
749
750/**
751 * ata_devchk - PATA device presence detection
752 * @ap: ATA channel to examine
753 * @device: Device to examine (starting at zero)
754 *
755 * Dispatch ATA device presence detection, depending
756 * on whether we are using PIO or MMIO to talk to the
757 * ATA shadow registers.
758 *
759 * LOCKING:
760 * caller.
761 */
762
763static unsigned int ata_devchk(struct ata_port *ap,
764 unsigned int device)
765{
766 if (ap->flags & ATA_FLAG_MMIO)
767 return ata_mmio_devchk(ap, device);
768 return ata_pio_devchk(ap, device);
769}
770
771/**
772 * ata_dev_classify - determine device type based on ATA-spec signature
773 * @tf: ATA taskfile register set for device to be identified
774 *
775 * Determine from taskfile register contents whether a device is
776 * ATA or ATAPI, as per "Signature and persistence" section
777 * of ATA/PI spec (volume 1, sect 5.14).
778 *
779 * LOCKING:
780 * None.
781 *
782 * RETURNS:
783 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
784 * the event of failure.
785 */
786
Jeff Garzik057ace52005-10-22 14:27:05 -0400787unsigned int ata_dev_classify(const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788{
789 /* Apple's open source Darwin code hints that some devices only
790 * put a proper signature into the LBA mid/high registers,
791 * So, we only check those. It's sufficient for uniqueness.
792 */
793
794 if (((tf->lbam == 0) && (tf->lbah == 0)) ||
795 ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
796 DPRINTK("found ATA device by sig\n");
797 return ATA_DEV_ATA;
798 }
799
800 if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
801 ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
802 DPRINTK("found ATAPI device by sig\n");
803 return ATA_DEV_ATAPI;
804 }
805
806 DPRINTK("unknown device\n");
807 return ATA_DEV_UNKNOWN;
808}
809
810/**
811 * ata_dev_try_classify - Parse returned ATA device signature
812 * @ap: ATA channel to examine
813 * @device: Device to examine (starting at zero)
814 *
815 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
816 * an ATA/ATAPI-defined set of values is placed in the ATA
817 * shadow registers, indicating the results of device detection
818 * and diagnostics.
819 *
820 * Select the ATA device, and read the values from the ATA shadow
821 * registers. Then parse according to the Error register value,
822 * and the spec-defined values examined by ata_dev_classify().
823 *
824 * LOCKING:
825 * caller.
826 */
827
828static u8 ata_dev_try_classify(struct ata_port *ap, unsigned int device)
829{
830 struct ata_device *dev = &ap->device[device];
831 struct ata_taskfile tf;
832 unsigned int class;
833 u8 err;
834
835 ap->ops->dev_select(ap, device);
836
837 memset(&tf, 0, sizeof(tf));
838
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 ap->ops->tf_read(ap, &tf);
Jeff Garzik0169e282005-10-29 21:25:10 -0400840 err = tf.feature;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
842 dev->class = ATA_DEV_NONE;
843
844 /* see if device passed diags */
845 if (err == 1)
846 /* do nothing */ ;
847 else if ((device == 0) && (err == 0x81))
848 /* do nothing */ ;
849 else
850 return err;
851
852 /* determine if device if ATA or ATAPI */
853 class = ata_dev_classify(&tf);
854 if (class == ATA_DEV_UNKNOWN)
855 return err;
856 if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
857 return err;
858
859 dev->class = class;
860
861 return err;
862}
863
864/**
865 * ata_dev_id_string - Convert IDENTIFY DEVICE page into string
866 * @id: IDENTIFY DEVICE results we will examine
867 * @s: string into which data is output
868 * @ofs: offset into identify device page
869 * @len: length of string to return. must be an even number.
870 *
871 * The strings in the IDENTIFY DEVICE page are broken up into
872 * 16-bit chunks. Run through the string, and output each
873 * 8-bit chunk linearly, regardless of platform.
874 *
875 * LOCKING:
876 * caller.
877 */
878
Jeff Garzik057ace52005-10-22 14:27:05 -0400879void ata_dev_id_string(const u16 *id, unsigned char *s,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 unsigned int ofs, unsigned int len)
881{
882 unsigned int c;
883
884 while (len > 0) {
885 c = id[ofs] >> 8;
886 *s = c;
887 s++;
888
889 c = id[ofs] & 0xff;
890 *s = c;
891 s++;
892
893 ofs++;
894 len -= 2;
895 }
896}
897
Edward Falk0baab862005-06-02 18:17:13 -0400898
899/**
900 * ata_noop_dev_select - Select device 0/1 on ATA bus
901 * @ap: ATA channel to manipulate
902 * @device: ATA device (numbered from zero) to select
903 *
904 * This function performs no actual function.
905 *
906 * May be used as the dev_select() entry in ata_port_operations.
907 *
908 * LOCKING:
909 * caller.
910 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
912{
913}
914
Edward Falk0baab862005-06-02 18:17:13 -0400915
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916/**
917 * ata_std_dev_select - Select device 0/1 on ATA bus
918 * @ap: ATA channel to manipulate
919 * @device: ATA device (numbered from zero) to select
920 *
921 * Use the method defined in the ATA specification to
922 * make either device 0, or device 1, active on the
Edward Falk0baab862005-06-02 18:17:13 -0400923 * ATA channel. Works with both PIO and MMIO.
924 *
925 * May be used as the dev_select() entry in ata_port_operations.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 *
927 * LOCKING:
928 * caller.
929 */
930
931void ata_std_dev_select (struct ata_port *ap, unsigned int device)
932{
933 u8 tmp;
934
935 if (device == 0)
936 tmp = ATA_DEVICE_OBS;
937 else
938 tmp = ATA_DEVICE_OBS | ATA_DEV1;
939
940 if (ap->flags & ATA_FLAG_MMIO) {
941 writeb(tmp, (void __iomem *) ap->ioaddr.device_addr);
942 } else {
943 outb(tmp, ap->ioaddr.device_addr);
944 }
945 ata_pause(ap); /* needed; also flushes, for mmio */
946}
947
948/**
949 * ata_dev_select - Select device 0/1 on ATA bus
950 * @ap: ATA channel to manipulate
951 * @device: ATA device (numbered from zero) to select
952 * @wait: non-zero to wait for Status register BSY bit to clear
953 * @can_sleep: non-zero if context allows sleeping
954 *
955 * Use the method defined in the ATA specification to
956 * make either device 0, or device 1, active on the
957 * ATA channel.
958 *
959 * This is a high-level version of ata_std_dev_select(),
960 * which additionally provides the services of inserting
961 * the proper pauses and status polling, where needed.
962 *
963 * LOCKING:
964 * caller.
965 */
966
967void ata_dev_select(struct ata_port *ap, unsigned int device,
968 unsigned int wait, unsigned int can_sleep)
969{
970 VPRINTK("ENTER, ata%u: device %u, wait %u\n",
971 ap->id, device, wait);
972
973 if (wait)
974 ata_wait_idle(ap);
975
976 ap->ops->dev_select(ap, device);
977
978 if (wait) {
979 if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
980 msleep(150);
981 ata_wait_idle(ap);
982 }
983}
984
985/**
986 * ata_dump_id - IDENTIFY DEVICE info debugging output
987 * @dev: Device whose IDENTIFY DEVICE page we will dump
988 *
989 * Dump selected 16-bit words from a detected device's
990 * IDENTIFY PAGE page.
991 *
992 * LOCKING:
993 * caller.
994 */
995
Jeff Garzik057ace52005-10-22 14:27:05 -0400996static inline void ata_dump_id(const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997{
998 DPRINTK("49==0x%04x "
999 "53==0x%04x "
1000 "63==0x%04x "
1001 "64==0x%04x "
1002 "75==0x%04x \n",
1003 dev->id[49],
1004 dev->id[53],
1005 dev->id[63],
1006 dev->id[64],
1007 dev->id[75]);
1008 DPRINTK("80==0x%04x "
1009 "81==0x%04x "
1010 "82==0x%04x "
1011 "83==0x%04x "
1012 "84==0x%04x \n",
1013 dev->id[80],
1014 dev->id[81],
1015 dev->id[82],
1016 dev->id[83],
1017 dev->id[84]);
1018 DPRINTK("88==0x%04x "
1019 "93==0x%04x\n",
1020 dev->id[88],
1021 dev->id[93]);
1022}
1023
Alan Cox11e29e22005-10-21 18:46:32 -04001024/*
1025 * Compute the PIO modes available for this device. This is not as
1026 * trivial as it seems if we must consider early devices correctly.
1027 *
1028 * FIXME: pre IDE drive timing (do we care ?).
1029 */
1030
Jeff Garzik057ace52005-10-22 14:27:05 -04001031static unsigned int ata_pio_modes(const struct ata_device *adev)
Alan Cox11e29e22005-10-21 18:46:32 -04001032{
1033 u16 modes;
1034
1035 /* Usual case. Word 53 indicates word 88 is valid */
1036 if (adev->id[ATA_ID_FIELD_VALID] & (1 << 2)) {
1037 modes = adev->id[ATA_ID_PIO_MODES] & 0x03;
1038 modes <<= 3;
1039 modes |= 0x7;
1040 return modes;
1041 }
1042
1043 /* If word 88 isn't valid then Word 51 holds the PIO timing number
1044 for the maximum. Turn it into a mask and return it */
1045 modes = (2 << (adev->id[ATA_ID_OLD_PIO_MODES] & 0xFF)) - 1 ;
1046 return modes;
1047}
1048
Jeff Garzik64f043d2005-11-17 10:50:01 -05001049static int ata_qc_wait_err(struct ata_queued_cmd *qc,
1050 struct completion *wait)
1051{
1052 int rc = 0;
1053
1054 if (wait_for_completion_timeout(wait, 30 * HZ) < 1) {
1055 /* timeout handling */
Albert Leea22e2eb2005-12-05 15:38:02 +08001056 qc->err_mask |= ac_err_mask(ata_chk_status(qc->ap));
Jeff Garzik64f043d2005-11-17 10:50:01 -05001057
Albert Leea22e2eb2005-12-05 15:38:02 +08001058 if (!qc->err_mask) {
Jeff Garzik64f043d2005-11-17 10:50:01 -05001059 printk(KERN_WARNING "ata%u: slow completion (cmd %x)\n",
1060 qc->ap->id, qc->tf.command);
1061 } else {
1062 printk(KERN_WARNING "ata%u: qc timeout (cmd %x)\n",
1063 qc->ap->id, qc->tf.command);
1064 rc = -EIO;
1065 }
1066
Albert Leea22e2eb2005-12-05 15:38:02 +08001067 ata_qc_complete(qc);
Jeff Garzik64f043d2005-11-17 10:50:01 -05001068 }
1069
1070 return rc;
1071}
1072
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073/**
1074 * ata_dev_identify - obtain IDENTIFY x DEVICE page
1075 * @ap: port on which device we wish to probe resides
1076 * @device: device bus address, starting at zero
1077 *
1078 * Following bus reset, we issue the IDENTIFY [PACKET] DEVICE
1079 * command, and read back the 512-byte device information page.
1080 * The device information page is fed to us via the standard
1081 * PIO-IN protocol, but we hand-code it here. (TODO: investigate
1082 * using standard PIO-IN paths)
1083 *
1084 * After reading the device information page, we use several
1085 * bits of information from it to initialize data structures
1086 * that will be used during the lifetime of the ata_device.
1087 * Other data from the info page is used to disqualify certain
1088 * older ATA devices we do not wish to support.
1089 *
1090 * LOCKING:
1091 * Inherited from caller. Some functions called by this function
1092 * obtain the host_set lock.
1093 */
1094
1095static void ata_dev_identify(struct ata_port *ap, unsigned int device)
1096{
1097 struct ata_device *dev = &ap->device[device];
Albert Lee8bf62ece2005-05-12 15:29:42 -04001098 unsigned int major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 u16 tmp;
1100 unsigned long xfer_modes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 unsigned int using_edd;
1102 DECLARE_COMPLETION(wait);
1103 struct ata_queued_cmd *qc;
1104 unsigned long flags;
1105 int rc;
1106
1107 if (!ata_dev_present(dev)) {
1108 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
1109 ap->id, device);
1110 return;
1111 }
1112
1113 if (ap->flags & (ATA_FLAG_SRST | ATA_FLAG_SATA_RESET))
1114 using_edd = 0;
1115 else
1116 using_edd = 1;
1117
1118 DPRINTK("ENTER, host %u, dev %u\n", ap->id, device);
1119
1120 assert (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ATAPI ||
1121 dev->class == ATA_DEV_NONE);
1122
1123 ata_dev_select(ap, device, 1, 1); /* select device 0/1 */
1124
1125 qc = ata_qc_new_init(ap, dev);
1126 BUG_ON(qc == NULL);
1127
1128 ata_sg_init_one(qc, dev->id, sizeof(dev->id));
1129 qc->dma_dir = DMA_FROM_DEVICE;
1130 qc->tf.protocol = ATA_PROT_PIO;
1131 qc->nsect = 1;
1132
1133retry:
1134 if (dev->class == ATA_DEV_ATA) {
1135 qc->tf.command = ATA_CMD_ID_ATA;
1136 DPRINTK("do ATA identify\n");
1137 } else {
1138 qc->tf.command = ATA_CMD_ID_ATAPI;
1139 DPRINTK("do ATAPI identify\n");
1140 }
1141
1142 qc->waiting = &wait;
1143 qc->complete_fn = ata_qc_complete_noop;
1144
1145 spin_lock_irqsave(&ap->host_set->lock, flags);
1146 rc = ata_qc_issue(qc);
1147 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1148
1149 if (rc)
1150 goto err_out;
1151 else
Jeff Garzik64f043d2005-11-17 10:50:01 -05001152 ata_qc_wait_err(qc, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
Jeff Garzik0169e282005-10-29 21:25:10 -04001154 spin_lock_irqsave(&ap->host_set->lock, flags);
1155 ap->ops->tf_read(ap, &qc->tf);
1156 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1157
1158 if (qc->tf.command & ATA_ERR) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 /*
1160 * arg! EDD works for all test cases, but seems to return
1161 * the ATA signature for some ATAPI devices. Until the
1162 * reason for this is found and fixed, we fix up the mess
1163 * here. If IDENTIFY DEVICE returns command aborted
1164 * (as ATAPI devices do), then we issue an
1165 * IDENTIFY PACKET DEVICE.
1166 *
1167 * ATA software reset (SRST, the default) does not appear
1168 * to have this problem.
1169 */
Albert Lee7c398332005-11-09 13:03:30 +08001170 if ((using_edd) && (dev->class == ATA_DEV_ATA)) {
Jeff Garzik0169e282005-10-29 21:25:10 -04001171 u8 err = qc->tf.feature;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 if (err & ATA_ABORTED) {
1173 dev->class = ATA_DEV_ATAPI;
1174 qc->cursg = 0;
1175 qc->cursg_ofs = 0;
1176 qc->cursect = 0;
1177 qc->nsect = 1;
Albert Leea22e2eb2005-12-05 15:38:02 +08001178 qc->err_mask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 goto retry;
1180 }
1181 }
1182 goto err_out;
1183 }
1184
1185 swap_buf_le16(dev->id, ATA_ID_WORDS);
1186
1187 /* print device capabilities */
1188 printk(KERN_DEBUG "ata%u: dev %u cfg "
1189 "49:%04x 82:%04x 83:%04x 84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1190 ap->id, device, dev->id[49],
1191 dev->id[82], dev->id[83], dev->id[84],
1192 dev->id[85], dev->id[86], dev->id[87],
1193 dev->id[88]);
1194
1195 /*
1196 * common ATA, ATAPI feature tests
1197 */
1198
Albert Lee8bf62ece2005-05-12 15:29:42 -04001199 /* we require DMA support (bits 8 of word 49) */
1200 if (!ata_id_has_dma(dev->id)) {
1201 printk(KERN_DEBUG "ata%u: no dma\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 goto err_out_nosup;
1203 }
1204
1205 /* quick-n-dirty find max transfer mode; for printk only */
1206 xfer_modes = dev->id[ATA_ID_UDMA_MODES];
1207 if (!xfer_modes)
1208 xfer_modes = (dev->id[ATA_ID_MWDMA_MODES]) << ATA_SHIFT_MWDMA;
Alan Cox11e29e22005-10-21 18:46:32 -04001209 if (!xfer_modes)
1210 xfer_modes = ata_pio_modes(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
1212 ata_dump_id(dev);
1213
1214 /* ATA-specific feature tests */
1215 if (dev->class == ATA_DEV_ATA) {
1216 if (!ata_id_is_ata(dev->id)) /* sanity check */
1217 goto err_out_nosup;
1218
Albert Lee8bf62ece2005-05-12 15:29:42 -04001219 /* get major version */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 tmp = dev->id[ATA_ID_MAJOR_VER];
Albert Lee8bf62ece2005-05-12 15:29:42 -04001221 for (major_version = 14; major_version >= 1; major_version--)
1222 if (tmp & (1 << major_version))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 break;
1224
Albert Lee8bf62ece2005-05-12 15:29:42 -04001225 /*
1226 * The exact sequence expected by certain pre-ATA4 drives is:
1227 * SRST RESET
1228 * IDENTIFY
1229 * INITIALIZE DEVICE PARAMETERS
1230 * anything else..
1231 * Some drives were very specific about that exact sequence.
1232 */
Albert Lee59a10b12005-10-12 15:09:42 +08001233 if (major_version < 4 || (!ata_id_has_lba(dev->id))) {
Albert Lee8bf62ece2005-05-12 15:29:42 -04001234 ata_dev_init_params(ap, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
Albert Lee59a10b12005-10-12 15:09:42 +08001236 /* current CHS translation info (id[53-58]) might be
1237 * changed. reread the identify device info.
1238 */
1239 ata_dev_reread_id(ap, dev);
1240 }
1241
Albert Lee8bf62ece2005-05-12 15:29:42 -04001242 if (ata_id_has_lba(dev->id)) {
1243 dev->flags |= ATA_DFLAG_LBA;
1244
1245 if (ata_id_has_lba48(dev->id)) {
1246 dev->flags |= ATA_DFLAG_LBA48;
1247 dev->n_sectors = ata_id_u64(dev->id, 100);
1248 } else {
1249 dev->n_sectors = ata_id_u32(dev->id, 60);
1250 }
1251
1252 /* print device info to dmesg */
1253 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors:%s\n",
1254 ap->id, device,
1255 major_version,
1256 ata_mode_string(xfer_modes),
1257 (unsigned long long)dev->n_sectors,
1258 dev->flags & ATA_DFLAG_LBA48 ? " LBA48" : " LBA");
1259 } else {
1260 /* CHS */
1261
1262 /* Default translation */
1263 dev->cylinders = dev->id[1];
1264 dev->heads = dev->id[3];
1265 dev->sectors = dev->id[6];
1266 dev->n_sectors = dev->cylinders * dev->heads * dev->sectors;
1267
1268 if (ata_id_current_chs_valid(dev->id)) {
1269 /* Current CHS translation is valid. */
1270 dev->cylinders = dev->id[54];
1271 dev->heads = dev->id[55];
1272 dev->sectors = dev->id[56];
1273
1274 dev->n_sectors = ata_id_u32(dev->id, 57);
1275 }
1276
1277 /* print device info to dmesg */
1278 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors: CHS %d/%d/%d\n",
1279 ap->id, device,
1280 major_version,
1281 ata_mode_string(xfer_modes),
1282 (unsigned long long)dev->n_sectors,
1283 (int)dev->cylinders, (int)dev->heads, (int)dev->sectors);
1284
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 }
1286
1287 ap->host->max_cmd_len = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 }
1289
1290 /* ATAPI-specific feature tests */
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05001291 else if (dev->class == ATA_DEV_ATAPI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 if (ata_id_is_ata(dev->id)) /* sanity check */
1293 goto err_out_nosup;
1294
1295 rc = atapi_cdb_len(dev->id);
1296 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
1297 printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
1298 goto err_out_nosup;
1299 }
1300 ap->cdb_len = (unsigned int) rc;
1301 ap->host->max_cmd_len = (unsigned char) ap->cdb_len;
1302
1303 /* print device info to dmesg */
1304 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
1305 ap->id, device,
1306 ata_mode_string(xfer_modes));
1307 }
1308
1309 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
1310 return;
1311
1312err_out_nosup:
1313 printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n",
1314 ap->id, device);
1315err_out:
1316 dev->class++; /* converts ATA_DEV_xxx into ATA_DEV_xxx_UNSUP */
1317 DPRINTK("EXIT, err\n");
1318}
1319
Brad Campbell6f2f3812005-05-12 15:07:47 -04001320
Jeff Garzik057ace52005-10-22 14:27:05 -04001321static inline u8 ata_dev_knobble(const struct ata_port *ap)
Brad Campbell6f2f3812005-05-12 15:07:47 -04001322{
1323 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(ap->device->id)));
1324}
1325
1326/**
1327 * ata_dev_config - Run device specific handlers and check for
1328 * SATA->PATA bridges
Jeff Garzik8a60a072005-07-31 13:13:24 -04001329 * @ap: Bus
Brad Campbell6f2f3812005-05-12 15:07:47 -04001330 * @i: Device
1331 *
1332 * LOCKING:
1333 */
Jeff Garzik8a60a072005-07-31 13:13:24 -04001334
Brad Campbell6f2f3812005-05-12 15:07:47 -04001335void ata_dev_config(struct ata_port *ap, unsigned int i)
1336{
1337 /* limit bridge transfers to udma5, 200 sectors */
1338 if (ata_dev_knobble(ap)) {
1339 printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
1340 ap->id, ap->device->devno);
1341 ap->udma_mask &= ATA_UDMA5;
1342 ap->host->max_sectors = ATA_MAX_SECTORS;
1343 ap->host->hostt->max_sectors = ATA_MAX_SECTORS;
1344 ap->device->flags |= ATA_DFLAG_LOCK_SECTORS;
1345 }
1346
1347 if (ap->ops->dev_config)
1348 ap->ops->dev_config(ap, &ap->device[i]);
1349}
1350
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351/**
1352 * ata_bus_probe - Reset and probe ATA bus
1353 * @ap: Bus to probe
1354 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001355 * Master ATA bus probing function. Initiates a hardware-dependent
1356 * bus reset, then attempts to identify any devices found on
1357 * the bus.
1358 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001360 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 *
1362 * RETURNS:
1363 * Zero on success, non-zero on error.
1364 */
1365
1366static int ata_bus_probe(struct ata_port *ap)
1367{
1368 unsigned int i, found = 0;
1369
1370 ap->ops->phy_reset(ap);
1371 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1372 goto err_out;
1373
1374 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1375 ata_dev_identify(ap, i);
1376 if (ata_dev_present(&ap->device[i])) {
1377 found = 1;
Brad Campbell6f2f3812005-05-12 15:07:47 -04001378 ata_dev_config(ap,i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 }
1380 }
1381
1382 if ((!found) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1383 goto err_out_disable;
1384
1385 ata_set_mode(ap);
1386 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1387 goto err_out_disable;
1388
1389 return 0;
1390
1391err_out_disable:
1392 ap->ops->port_disable(ap);
1393err_out:
1394 return -1;
1395}
1396
1397/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001398 * ata_port_probe - Mark port as enabled
1399 * @ap: Port for which we indicate enablement
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001401 * Modify @ap data structure such that the system
1402 * thinks that the entire port is enabled.
1403 *
1404 * LOCKING: host_set lock, or some other form of
1405 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 */
1407
1408void ata_port_probe(struct ata_port *ap)
1409{
1410 ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1411}
1412
1413/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001414 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1415 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001417 * This function issues commands to standard SATA Sxxx
1418 * PHY registers, to wake up the phy (and device), and
1419 * clear any reset condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 *
1421 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001422 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 *
1424 */
1425void __sata_phy_reset(struct ata_port *ap)
1426{
1427 u32 sstatus;
1428 unsigned long timeout = jiffies + (HZ * 5);
1429
1430 if (ap->flags & ATA_FLAG_SATA_RESET) {
Brett Russcdcca89e2005-03-28 15:10:27 -05001431 /* issue phy wake/reset */
1432 scr_write_flush(ap, SCR_CONTROL, 0x301);
Tejun Heo62ba2842005-06-26 23:27:19 +09001433 /* Couldn't find anything in SATA I/II specs, but
1434 * AHCI-1.1 10.4.2 says at least 1 ms. */
1435 mdelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 }
Brett Russcdcca89e2005-03-28 15:10:27 -05001437 scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438
1439 /* wait for phy to become ready, if necessary */
1440 do {
1441 msleep(200);
1442 sstatus = scr_read(ap, SCR_STATUS);
1443 if ((sstatus & 0xf) != 1)
1444 break;
1445 } while (time_before(jiffies, timeout));
1446
1447 /* TODO: phy layer with polling, timeouts, etc. */
Jeff Garzik656563e2005-11-20 03:36:45 -05001448 sstatus = scr_read(ap, SCR_STATUS);
1449 if (sata_dev_present(ap)) {
1450 const char *speed;
1451 u32 tmp;
1452
1453 tmp = (sstatus >> 4) & 0xf;
1454 if (tmp & (1 << 0))
1455 speed = "1.5";
1456 else if (tmp & (1 << 1))
1457 speed = "3.0";
1458 else
1459 speed = "<unknown>";
1460 printk(KERN_INFO "ata%u: SATA link up %s Gbps (SStatus %X)\n",
1461 ap->id, speed, sstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 ata_port_probe(ap);
Jeff Garzik656563e2005-11-20 03:36:45 -05001463 } else {
1464 printk(KERN_INFO "ata%u: SATA link down (SStatus %X)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001465 ap->id, sstatus);
1466 ata_port_disable(ap);
1467 }
1468
1469 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1470 return;
1471
1472 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1473 ata_port_disable(ap);
1474 return;
1475 }
1476
1477 ap->cbl = ATA_CBL_SATA;
1478}
1479
1480/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001481 * sata_phy_reset - Reset SATA bus.
1482 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001484 * This function resets the SATA bus, and then probes
1485 * the bus for devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001486 *
1487 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001488 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489 *
1490 */
1491void sata_phy_reset(struct ata_port *ap)
1492{
1493 __sata_phy_reset(ap);
1494 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1495 return;
1496 ata_bus_reset(ap);
1497}
1498
1499/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001500 * ata_port_disable - Disable port.
1501 * @ap: Port to be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001503 * Modify @ap data structure such that the system
1504 * thinks that the entire port is disabled, and should
1505 * never attempt to probe or communicate with devices
1506 * on this port.
1507 *
1508 * LOCKING: host_set lock, or some other form of
1509 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 */
1511
1512void ata_port_disable(struct ata_port *ap)
1513{
1514 ap->device[0].class = ATA_DEV_NONE;
1515 ap->device[1].class = ATA_DEV_NONE;
1516 ap->flags |= ATA_FLAG_PORT_DISABLED;
1517}
1518
Alan Cox452503f2005-10-21 19:01:32 -04001519/*
1520 * This mode timing computation functionality is ported over from
1521 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
1522 */
1523/*
1524 * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
1525 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
1526 * for PIO 5, which is a nonstandard extension and UDMA6, which
1527 * is currently supported only by Maxtor drives.
1528 */
1529
1530static const struct ata_timing ata_timing[] = {
1531
1532 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
1533 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
1534 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
1535 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
1536
1537 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
1538 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
1539 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
1540
1541/* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
1542
1543 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
1544 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
1545 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
1546
1547 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
1548 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
1549 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
1550
1551/* { XFER_PIO_5, 20, 50, 30, 100, 50, 30, 100, 0 }, */
1552 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
1553 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
1554
1555 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
1556 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
1557 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
1558
1559/* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
1560
1561 { 0xFF }
1562};
1563
1564#define ENOUGH(v,unit) (((v)-1)/(unit)+1)
1565#define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
1566
1567static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
1568{
1569 q->setup = EZ(t->setup * 1000, T);
1570 q->act8b = EZ(t->act8b * 1000, T);
1571 q->rec8b = EZ(t->rec8b * 1000, T);
1572 q->cyc8b = EZ(t->cyc8b * 1000, T);
1573 q->active = EZ(t->active * 1000, T);
1574 q->recover = EZ(t->recover * 1000, T);
1575 q->cycle = EZ(t->cycle * 1000, T);
1576 q->udma = EZ(t->udma * 1000, UT);
1577}
1578
1579void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
1580 struct ata_timing *m, unsigned int what)
1581{
1582 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
1583 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
1584 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
1585 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
1586 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
1587 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
1588 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
1589 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
1590}
1591
1592static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
1593{
1594 const struct ata_timing *t;
1595
1596 for (t = ata_timing; t->mode != speed; t++)
Alan Cox91190752005-10-26 12:17:46 -04001597 if (t->mode == 0xFF)
Alan Cox452503f2005-10-21 19:01:32 -04001598 return NULL;
1599 return t;
1600}
1601
1602int ata_timing_compute(struct ata_device *adev, unsigned short speed,
1603 struct ata_timing *t, int T, int UT)
1604{
1605 const struct ata_timing *s;
1606 struct ata_timing p;
1607
1608 /*
1609 * Find the mode.
Albert Lee75b1f2f2005-11-16 17:06:18 +08001610 */
Alan Cox452503f2005-10-21 19:01:32 -04001611
1612 if (!(s = ata_timing_find_mode(speed)))
1613 return -EINVAL;
1614
Albert Lee75b1f2f2005-11-16 17:06:18 +08001615 memcpy(t, s, sizeof(*s));
1616
Alan Cox452503f2005-10-21 19:01:32 -04001617 /*
1618 * If the drive is an EIDE drive, it can tell us it needs extended
1619 * PIO/MW_DMA cycle timing.
1620 */
1621
1622 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
1623 memset(&p, 0, sizeof(p));
1624 if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
1625 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
1626 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
1627 } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
1628 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
1629 }
1630 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
1631 }
1632
1633 /*
1634 * Convert the timing to bus clock counts.
1635 */
1636
Albert Lee75b1f2f2005-11-16 17:06:18 +08001637 ata_timing_quantize(t, t, T, UT);
Alan Cox452503f2005-10-21 19:01:32 -04001638
1639 /*
1640 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY, S.M.A.R.T
1641 * and some other commands. We have to ensure that the DMA cycle timing is
1642 * slower/equal than the fastest PIO timing.
1643 */
1644
1645 if (speed > XFER_PIO_4) {
1646 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
1647 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
1648 }
1649
1650 /*
1651 * Lenghten active & recovery time so that cycle time is correct.
1652 */
1653
1654 if (t->act8b + t->rec8b < t->cyc8b) {
1655 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
1656 t->rec8b = t->cyc8b - t->act8b;
1657 }
1658
1659 if (t->active + t->recover < t->cycle) {
1660 t->active += (t->cycle - (t->active + t->recover)) / 2;
1661 t->recover = t->cycle - t->active;
1662 }
1663
1664 return 0;
1665}
1666
Jeff Garzik057ace52005-10-22 14:27:05 -04001667static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 unsigned int shift;
1669 u8 base;
1670} xfer_mode_classes[] = {
1671 { ATA_SHIFT_UDMA, XFER_UDMA_0 },
1672 { ATA_SHIFT_MWDMA, XFER_MW_DMA_0 },
1673 { ATA_SHIFT_PIO, XFER_PIO_0 },
1674};
1675
1676static inline u8 base_from_shift(unsigned int shift)
1677{
1678 int i;
1679
1680 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++)
1681 if (xfer_mode_classes[i].shift == shift)
1682 return xfer_mode_classes[i].base;
1683
1684 return 0xff;
1685}
1686
1687static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
1688{
1689 int ofs, idx;
1690 u8 base;
1691
1692 if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1693 return;
1694
1695 if (dev->xfer_shift == ATA_SHIFT_PIO)
1696 dev->flags |= ATA_DFLAG_PIO;
1697
1698 ata_dev_set_xfermode(ap, dev);
1699
1700 base = base_from_shift(dev->xfer_shift);
1701 ofs = dev->xfer_mode - base;
1702 idx = ofs + dev->xfer_shift;
1703 WARN_ON(idx >= ARRAY_SIZE(xfer_mode_str));
1704
1705 DPRINTK("idx=%d xfer_shift=%u, xfer_mode=0x%x, base=0x%x, offset=%d\n",
1706 idx, dev->xfer_shift, (int)dev->xfer_mode, (int)base, ofs);
1707
1708 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
1709 ap->id, dev->devno, xfer_mode_str[idx]);
1710}
1711
1712static int ata_host_set_pio(struct ata_port *ap)
1713{
1714 unsigned int mask;
1715 int x, i;
1716 u8 base, xfer_mode;
1717
1718 mask = ata_get_mode_mask(ap, ATA_SHIFT_PIO);
1719 x = fgb(mask);
1720 if (x < 0) {
1721 printk(KERN_WARNING "ata%u: no PIO support\n", ap->id);
1722 return -1;
1723 }
1724
1725 base = base_from_shift(ATA_SHIFT_PIO);
1726 xfer_mode = base + x;
1727
1728 DPRINTK("base 0x%x xfer_mode 0x%x mask 0x%x x %d\n",
1729 (int)base, (int)xfer_mode, mask, x);
1730
1731 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1732 struct ata_device *dev = &ap->device[i];
1733 if (ata_dev_present(dev)) {
1734 dev->pio_mode = xfer_mode;
1735 dev->xfer_mode = xfer_mode;
1736 dev->xfer_shift = ATA_SHIFT_PIO;
1737 if (ap->ops->set_piomode)
1738 ap->ops->set_piomode(ap, dev);
1739 }
1740 }
1741
1742 return 0;
1743}
1744
1745static void ata_host_set_dma(struct ata_port *ap, u8 xfer_mode,
1746 unsigned int xfer_shift)
1747{
1748 int i;
1749
1750 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1751 struct ata_device *dev = &ap->device[i];
1752 if (ata_dev_present(dev)) {
1753 dev->dma_mode = xfer_mode;
1754 dev->xfer_mode = xfer_mode;
1755 dev->xfer_shift = xfer_shift;
1756 if (ap->ops->set_dmamode)
1757 ap->ops->set_dmamode(ap, dev);
1758 }
1759 }
1760}
1761
1762/**
1763 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1764 * @ap: port on which timings will be programmed
1765 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001766 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1767 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001769 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 *
1771 */
1772static void ata_set_mode(struct ata_port *ap)
1773{
Albert Lee8cbd6df2005-10-12 15:06:27 +08001774 unsigned int xfer_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 u8 xfer_mode;
1776 int rc;
1777
1778 /* step 1: always set host PIO timings */
1779 rc = ata_host_set_pio(ap);
1780 if (rc)
1781 goto err_out;
1782
1783 /* step 2: choose the best data xfer mode */
1784 xfer_mode = xfer_shift = 0;
1785 rc = ata_choose_xfer_mode(ap, &xfer_mode, &xfer_shift);
1786 if (rc)
1787 goto err_out;
1788
1789 /* step 3: if that xfer mode isn't PIO, set host DMA timings */
1790 if (xfer_shift != ATA_SHIFT_PIO)
1791 ata_host_set_dma(ap, xfer_mode, xfer_shift);
1792
1793 /* step 4: update devices' xfer mode */
1794 ata_dev_set_mode(ap, &ap->device[0]);
1795 ata_dev_set_mode(ap, &ap->device[1]);
1796
1797 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1798 return;
1799
1800 if (ap->ops->post_set_mode)
1801 ap->ops->post_set_mode(ap);
1802
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 return;
1804
1805err_out:
1806 ata_port_disable(ap);
1807}
1808
1809/**
1810 * ata_busy_sleep - sleep until BSY clears, or timeout
1811 * @ap: port containing status register to be polled
1812 * @tmout_pat: impatience timeout
1813 * @tmout: overall timeout
1814 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001815 * Sleep until ATA Status register bit BSY clears,
1816 * or a timeout occurs.
1817 *
1818 * LOCKING: None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 *
1820 */
1821
1822static unsigned int ata_busy_sleep (struct ata_port *ap,
1823 unsigned long tmout_pat,
1824 unsigned long tmout)
1825{
1826 unsigned long timer_start, timeout;
1827 u8 status;
1828
1829 status = ata_busy_wait(ap, ATA_BUSY, 300);
1830 timer_start = jiffies;
1831 timeout = timer_start + tmout_pat;
1832 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1833 msleep(50);
1834 status = ata_busy_wait(ap, ATA_BUSY, 3);
1835 }
1836
1837 if (status & ATA_BUSY)
1838 printk(KERN_WARNING "ata%u is slow to respond, "
1839 "please be patient\n", ap->id);
1840
1841 timeout = timer_start + tmout;
1842 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1843 msleep(50);
1844 status = ata_chk_status(ap);
1845 }
1846
1847 if (status & ATA_BUSY) {
1848 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
1849 ap->id, tmout / HZ);
1850 return 1;
1851 }
1852
1853 return 0;
1854}
1855
1856static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
1857{
1858 struct ata_ioports *ioaddr = &ap->ioaddr;
1859 unsigned int dev0 = devmask & (1 << 0);
1860 unsigned int dev1 = devmask & (1 << 1);
1861 unsigned long timeout;
1862
1863 /* if device 0 was found in ata_devchk, wait for its
1864 * BSY bit to clear
1865 */
1866 if (dev0)
1867 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1868
1869 /* if device 1 was found in ata_devchk, wait for
1870 * register access, then wait for BSY to clear
1871 */
1872 timeout = jiffies + ATA_TMOUT_BOOT;
1873 while (dev1) {
1874 u8 nsect, lbal;
1875
1876 ap->ops->dev_select(ap, 1);
1877 if (ap->flags & ATA_FLAG_MMIO) {
1878 nsect = readb((void __iomem *) ioaddr->nsect_addr);
1879 lbal = readb((void __iomem *) ioaddr->lbal_addr);
1880 } else {
1881 nsect = inb(ioaddr->nsect_addr);
1882 lbal = inb(ioaddr->lbal_addr);
1883 }
1884 if ((nsect == 1) && (lbal == 1))
1885 break;
1886 if (time_after(jiffies, timeout)) {
1887 dev1 = 0;
1888 break;
1889 }
1890 msleep(50); /* give drive a breather */
1891 }
1892 if (dev1)
1893 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1894
1895 /* is all this really necessary? */
1896 ap->ops->dev_select(ap, 0);
1897 if (dev1)
1898 ap->ops->dev_select(ap, 1);
1899 if (dev0)
1900 ap->ops->dev_select(ap, 0);
1901}
1902
1903/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001904 * ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
1905 * @ap: Port to reset and probe
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001907 * Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
1908 * probe the bus. Not often used these days.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 *
1910 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001911 * PCI/etc. bus probe sem.
Jeff Garzike5338252005-10-30 21:37:17 -05001912 * Obtains host_set lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 *
1914 */
1915
1916static unsigned int ata_bus_edd(struct ata_port *ap)
1917{
1918 struct ata_taskfile tf;
Jeff Garzike5338252005-10-30 21:37:17 -05001919 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
1921 /* set up execute-device-diag (bus reset) taskfile */
1922 /* also, take interrupts to a known state (disabled) */
1923 DPRINTK("execute-device-diag\n");
1924 ata_tf_init(ap, &tf, 0);
1925 tf.ctl |= ATA_NIEN;
1926 tf.command = ATA_CMD_EDD;
1927 tf.protocol = ATA_PROT_NODATA;
1928
1929 /* do bus reset */
Jeff Garzike5338252005-10-30 21:37:17 -05001930 spin_lock_irqsave(&ap->host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 ata_tf_to_host(ap, &tf);
Jeff Garzike5338252005-10-30 21:37:17 -05001932 spin_unlock_irqrestore(&ap->host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
1934 /* spec says at least 2ms. but who knows with those
1935 * crazy ATAPI devices...
1936 */
1937 msleep(150);
1938
1939 return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1940}
1941
1942static unsigned int ata_bus_softreset(struct ata_port *ap,
1943 unsigned int devmask)
1944{
1945 struct ata_ioports *ioaddr = &ap->ioaddr;
1946
1947 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
1948
1949 /* software reset. causes dev0 to be selected */
1950 if (ap->flags & ATA_FLAG_MMIO) {
1951 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1952 udelay(20); /* FIXME: flush */
1953 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
1954 udelay(20); /* FIXME: flush */
1955 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1956 } else {
1957 outb(ap->ctl, ioaddr->ctl_addr);
1958 udelay(10);
1959 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
1960 udelay(10);
1961 outb(ap->ctl, ioaddr->ctl_addr);
1962 }
1963
1964 /* spec mandates ">= 2ms" before checking status.
1965 * We wait 150ms, because that was the magic delay used for
1966 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
1967 * between when the ATA command register is written, and then
1968 * status is checked. Because waiting for "a while" before
1969 * checking status is fine, post SRST, we perform this magic
1970 * delay here as well.
1971 */
1972 msleep(150);
1973
1974 ata_bus_post_reset(ap, devmask);
1975
1976 return 0;
1977}
1978
1979/**
1980 * ata_bus_reset - reset host port and associated ATA channel
1981 * @ap: port to reset
1982 *
1983 * This is typically the first time we actually start issuing
1984 * commands to the ATA channel. We wait for BSY to clear, then
1985 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
1986 * result. Determine what devices, if any, are on the channel
1987 * by looking at the device 0/1 error register. Look at the signature
1988 * stored in each device's taskfile registers, to determine if
1989 * the device is ATA or ATAPI.
1990 *
1991 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001992 * PCI/etc. bus probe sem.
1993 * Obtains host_set lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 *
1995 * SIDE EFFECTS:
1996 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
1997 */
1998
1999void ata_bus_reset(struct ata_port *ap)
2000{
2001 struct ata_ioports *ioaddr = &ap->ioaddr;
2002 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2003 u8 err;
2004 unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
2005
2006 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
2007
2008 /* determine if device 0/1 are present */
2009 if (ap->flags & ATA_FLAG_SATA_RESET)
2010 dev0 = 1;
2011 else {
2012 dev0 = ata_devchk(ap, 0);
2013 if (slave_possible)
2014 dev1 = ata_devchk(ap, 1);
2015 }
2016
2017 if (dev0)
2018 devmask |= (1 << 0);
2019 if (dev1)
2020 devmask |= (1 << 1);
2021
2022 /* select device 0 again */
2023 ap->ops->dev_select(ap, 0);
2024
2025 /* issue bus reset */
2026 if (ap->flags & ATA_FLAG_SRST)
2027 rc = ata_bus_softreset(ap, devmask);
2028 else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
2029 /* set up device control */
2030 if (ap->flags & ATA_FLAG_MMIO)
2031 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2032 else
2033 outb(ap->ctl, ioaddr->ctl_addr);
2034 rc = ata_bus_edd(ap);
2035 }
2036
2037 if (rc)
2038 goto err_out;
2039
2040 /*
2041 * determine by signature whether we have ATA or ATAPI devices
2042 */
2043 err = ata_dev_try_classify(ap, 0);
2044 if ((slave_possible) && (err != 0x81))
2045 ata_dev_try_classify(ap, 1);
2046
2047 /* re-enable interrupts */
2048 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2049 ata_irq_on(ap);
2050
2051 /* is double-select really necessary? */
2052 if (ap->device[1].class != ATA_DEV_NONE)
2053 ap->ops->dev_select(ap, 1);
2054 if (ap->device[0].class != ATA_DEV_NONE)
2055 ap->ops->dev_select(ap, 0);
2056
2057 /* if no devices were detected, disable this port */
2058 if ((ap->device[0].class == ATA_DEV_NONE) &&
2059 (ap->device[1].class == ATA_DEV_NONE))
2060 goto err_out;
2061
2062 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
2063 /* set up device control for ATA_FLAG_SATA_RESET */
2064 if (ap->flags & ATA_FLAG_MMIO)
2065 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2066 else
2067 outb(ap->ctl, ioaddr->ctl_addr);
2068 }
2069
2070 DPRINTK("EXIT\n");
2071 return;
2072
2073err_out:
2074 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
2075 ap->ops->port_disable(ap);
2076
2077 DPRINTK("EXIT\n");
2078}
2079
Jeff Garzik057ace52005-10-22 14:27:05 -04002080static void ata_pr_blacklisted(const struct ata_port *ap,
2081 const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082{
2083 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, disabling DMA\n",
2084 ap->id, dev->devno);
2085}
2086
Arjan van de Ven98ac62d2005-11-28 10:06:23 +01002087static const char * const ata_dma_blacklist [] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 "WDC AC11000H",
2089 "WDC AC22100H",
2090 "WDC AC32500H",
2091 "WDC AC33100H",
2092 "WDC AC31600H",
2093 "WDC AC32100H",
2094 "WDC AC23200L",
2095 "Compaq CRD-8241B",
2096 "CRD-8400B",
2097 "CRD-8480B",
2098 "CRD-8482B",
2099 "CRD-84",
2100 "SanDisk SDP3B",
2101 "SanDisk SDP3B-64",
2102 "SANYO CD-ROM CRD",
2103 "HITACHI CDR-8",
2104 "HITACHI CDR-8335",
2105 "HITACHI CDR-8435",
2106 "Toshiba CD-ROM XM-6202B",
Jeff Garzike9222562005-06-28 00:03:37 -04002107 "TOSHIBA CD-ROM XM-1702BC",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 "CD-532E-A",
2109 "E-IDE CD-ROM CR-840",
2110 "CD-ROM Drive/F5A",
2111 "WPI CDD-820",
2112 "SAMSUNG CD-ROM SC-148C",
2113 "SAMSUNG CD-ROM SC",
2114 "SanDisk SDP3B-64",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115 "ATAPI CD-ROM DRIVE 40X MAXIMUM",
2116 "_NEC DV5800A",
2117};
2118
Jeff Garzik057ace52005-10-22 14:27:05 -04002119static int ata_dma_blacklisted(const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120{
2121 unsigned char model_num[40];
2122 char *s;
2123 unsigned int len;
2124 int i;
2125
2126 ata_dev_id_string(dev->id, model_num, ATA_ID_PROD_OFS,
2127 sizeof(model_num));
2128 s = &model_num[0];
2129 len = strnlen(s, sizeof(model_num));
2130
2131 /* ATAPI specifies that empty space is blank-filled; remove blanks */
2132 while ((len > 0) && (s[len - 1] == ' ')) {
2133 len--;
2134 s[len] = 0;
2135 }
2136
2137 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i++)
2138 if (!strncmp(ata_dma_blacklist[i], s, len))
2139 return 1;
2140
2141 return 0;
2142}
2143
Jeff Garzik057ace52005-10-22 14:27:05 -04002144static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145{
Jeff Garzik057ace52005-10-22 14:27:05 -04002146 const struct ata_device *master, *slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 unsigned int mask;
2148
2149 master = &ap->device[0];
2150 slave = &ap->device[1];
2151
2152 assert (ata_dev_present(master) || ata_dev_present(slave));
2153
2154 if (shift == ATA_SHIFT_UDMA) {
2155 mask = ap->udma_mask;
2156 if (ata_dev_present(master)) {
2157 mask &= (master->id[ATA_ID_UDMA_MODES] & 0xff);
Jeff Garzik057ace52005-10-22 14:27:05 -04002158 if (ata_dma_blacklisted(master)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 mask = 0;
2160 ata_pr_blacklisted(ap, master);
2161 }
2162 }
2163 if (ata_dev_present(slave)) {
2164 mask &= (slave->id[ATA_ID_UDMA_MODES] & 0xff);
Jeff Garzik057ace52005-10-22 14:27:05 -04002165 if (ata_dma_blacklisted(slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 mask = 0;
2167 ata_pr_blacklisted(ap, slave);
2168 }
2169 }
2170 }
2171 else if (shift == ATA_SHIFT_MWDMA) {
2172 mask = ap->mwdma_mask;
2173 if (ata_dev_present(master)) {
2174 mask &= (master->id[ATA_ID_MWDMA_MODES] & 0x07);
Jeff Garzik057ace52005-10-22 14:27:05 -04002175 if (ata_dma_blacklisted(master)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 mask = 0;
2177 ata_pr_blacklisted(ap, master);
2178 }
2179 }
2180 if (ata_dev_present(slave)) {
2181 mask &= (slave->id[ATA_ID_MWDMA_MODES] & 0x07);
Jeff Garzik057ace52005-10-22 14:27:05 -04002182 if (ata_dma_blacklisted(slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 mask = 0;
2184 ata_pr_blacklisted(ap, slave);
2185 }
2186 }
2187 }
2188 else if (shift == ATA_SHIFT_PIO) {
2189 mask = ap->pio_mask;
2190 if (ata_dev_present(master)) {
2191 /* spec doesn't return explicit support for
2192 * PIO0-2, so we fake it
2193 */
2194 u16 tmp_mode = master->id[ATA_ID_PIO_MODES] & 0x03;
2195 tmp_mode <<= 3;
2196 tmp_mode |= 0x7;
2197 mask &= tmp_mode;
2198 }
2199 if (ata_dev_present(slave)) {
2200 /* spec doesn't return explicit support for
2201 * PIO0-2, so we fake it
2202 */
2203 u16 tmp_mode = slave->id[ATA_ID_PIO_MODES] & 0x03;
2204 tmp_mode <<= 3;
2205 tmp_mode |= 0x7;
2206 mask &= tmp_mode;
2207 }
2208 }
2209 else {
2210 mask = 0xffffffff; /* shut up compiler warning */
2211 BUG();
2212 }
2213
2214 return mask;
2215}
2216
2217/* find greatest bit */
2218static int fgb(u32 bitmap)
2219{
2220 unsigned int i;
2221 int x = -1;
2222
2223 for (i = 0; i < 32; i++)
2224 if (bitmap & (1 << i))
2225 x = i;
2226
2227 return x;
2228}
2229
2230/**
2231 * ata_choose_xfer_mode - attempt to find best transfer mode
2232 * @ap: Port for which an xfer mode will be selected
2233 * @xfer_mode_out: (output) SET FEATURES - XFER MODE code
2234 * @xfer_shift_out: (output) bit shift that selects this mode
2235 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04002236 * Based on host and device capabilities, determine the
2237 * maximum transfer mode that is amenable to all.
2238 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002240 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 *
2242 * RETURNS:
2243 * Zero on success, negative on error.
2244 */
2245
Jeff Garzik057ace52005-10-22 14:27:05 -04002246static int ata_choose_xfer_mode(const struct ata_port *ap,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 u8 *xfer_mode_out,
2248 unsigned int *xfer_shift_out)
2249{
2250 unsigned int mask, shift;
2251 int x, i;
2252
2253 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++) {
2254 shift = xfer_mode_classes[i].shift;
2255 mask = ata_get_mode_mask(ap, shift);
2256
2257 x = fgb(mask);
2258 if (x >= 0) {
2259 *xfer_mode_out = xfer_mode_classes[i].base + x;
2260 *xfer_shift_out = shift;
2261 return 0;
2262 }
2263 }
2264
2265 return -1;
2266}
2267
2268/**
2269 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2270 * @ap: Port associated with device @dev
2271 * @dev: Device to which command will be sent
2272 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002273 * Issue SET FEATURES - XFER MODE command to device @dev
2274 * on port @ap.
2275 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002277 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 */
2279
2280static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
2281{
2282 DECLARE_COMPLETION(wait);
2283 struct ata_queued_cmd *qc;
2284 int rc;
2285 unsigned long flags;
2286
2287 /* set up set-features taskfile */
2288 DPRINTK("set features - xfer mode\n");
2289
2290 qc = ata_qc_new_init(ap, dev);
2291 BUG_ON(qc == NULL);
2292
2293 qc->tf.command = ATA_CMD_SET_FEATURES;
2294 qc->tf.feature = SETFEATURES_XFER;
2295 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2296 qc->tf.protocol = ATA_PROT_NODATA;
2297 qc->tf.nsect = dev->xfer_mode;
2298
2299 qc->waiting = &wait;
2300 qc->complete_fn = ata_qc_complete_noop;
2301
2302 spin_lock_irqsave(&ap->host_set->lock, flags);
2303 rc = ata_qc_issue(qc);
2304 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2305
2306 if (rc)
2307 ata_port_disable(ap);
2308 else
Jeff Garzik64f043d2005-11-17 10:50:01 -05002309 ata_qc_wait_err(qc, &wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310
2311 DPRINTK("EXIT\n");
2312}
2313
2314/**
Albert Lee59a10b12005-10-12 15:09:42 +08002315 * ata_dev_reread_id - Reread the device identify device info
2316 * @ap: port where the device is
2317 * @dev: device to reread the identify device info
2318 *
2319 * LOCKING:
2320 */
2321
2322static void ata_dev_reread_id(struct ata_port *ap, struct ata_device *dev)
2323{
2324 DECLARE_COMPLETION(wait);
2325 struct ata_queued_cmd *qc;
2326 unsigned long flags;
2327 int rc;
2328
2329 qc = ata_qc_new_init(ap, dev);
2330 BUG_ON(qc == NULL);
2331
2332 ata_sg_init_one(qc, dev->id, sizeof(dev->id));
2333 qc->dma_dir = DMA_FROM_DEVICE;
2334
2335 if (dev->class == ATA_DEV_ATA) {
2336 qc->tf.command = ATA_CMD_ID_ATA;
2337 DPRINTK("do ATA identify\n");
2338 } else {
2339 qc->tf.command = ATA_CMD_ID_ATAPI;
2340 DPRINTK("do ATAPI identify\n");
2341 }
2342
2343 qc->tf.flags |= ATA_TFLAG_DEVICE;
2344 qc->tf.protocol = ATA_PROT_PIO;
2345 qc->nsect = 1;
2346
2347 qc->waiting = &wait;
2348 qc->complete_fn = ata_qc_complete_noop;
2349
2350 spin_lock_irqsave(&ap->host_set->lock, flags);
2351 rc = ata_qc_issue(qc);
2352 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2353
2354 if (rc)
2355 goto err_out;
2356
Jeff Garzik64f043d2005-11-17 10:50:01 -05002357 ata_qc_wait_err(qc, &wait);
Albert Lee59a10b12005-10-12 15:09:42 +08002358
2359 swap_buf_le16(dev->id, ATA_ID_WORDS);
2360
2361 ata_dump_id(dev);
2362
2363 DPRINTK("EXIT\n");
2364
2365 return;
2366err_out:
2367 ata_port_disable(ap);
2368}
2369
2370/**
Albert Lee8bf62ece2005-05-12 15:29:42 -04002371 * ata_dev_init_params - Issue INIT DEV PARAMS command
2372 * @ap: Port associated with device @dev
2373 * @dev: Device to which command will be sent
2374 *
2375 * LOCKING:
2376 */
2377
2378static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev)
2379{
2380 DECLARE_COMPLETION(wait);
2381 struct ata_queued_cmd *qc;
2382 int rc;
2383 unsigned long flags;
2384 u16 sectors = dev->id[6];
2385 u16 heads = dev->id[3];
2386
2387 /* Number of sectors per track 1-255. Number of heads 1-16 */
2388 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
2389 return;
2390
2391 /* set up init dev params taskfile */
2392 DPRINTK("init dev params \n");
2393
2394 qc = ata_qc_new_init(ap, dev);
2395 BUG_ON(qc == NULL);
2396
2397 qc->tf.command = ATA_CMD_INIT_DEV_PARAMS;
2398 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2399 qc->tf.protocol = ATA_PROT_NODATA;
2400 qc->tf.nsect = sectors;
2401 qc->tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
2402
2403 qc->waiting = &wait;
2404 qc->complete_fn = ata_qc_complete_noop;
2405
2406 spin_lock_irqsave(&ap->host_set->lock, flags);
2407 rc = ata_qc_issue(qc);
2408 spin_unlock_irqrestore(&ap->host_set->lock, flags);
2409
2410 if (rc)
2411 ata_port_disable(ap);
2412 else
Jeff Garzik64f043d2005-11-17 10:50:01 -05002413 ata_qc_wait_err(qc, &wait);
Albert Lee8bf62ece2005-05-12 15:29:42 -04002414
2415 DPRINTK("EXIT\n");
2416}
2417
2418/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002419 * ata_sg_clean - Unmap DMA memory associated with command
2420 * @qc: Command containing DMA memory to be released
2421 *
2422 * Unmap all mapped DMA memory associated with this command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 *
2424 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002425 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 */
2427
2428static void ata_sg_clean(struct ata_queued_cmd *qc)
2429{
2430 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002431 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002433 void *pad_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434
2435 assert(qc->flags & ATA_QCFLAG_DMAMAP);
2436 assert(sg != NULL);
2437
2438 if (qc->flags & ATA_QCFLAG_SINGLE)
2439 assert(qc->n_elem == 1);
2440
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05002441 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002443 /* if we padded the buffer out to 32-bit bound, and data
2444 * xfer direction is from-device, we must copy from the
2445 * pad buffer back into the supplied buffer
2446 */
2447 if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
2448 pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2449
2450 if (qc->flags & ATA_QCFLAG_SG) {
Jeff Garzike1410f22005-11-14 14:06:26 -05002451 if (qc->n_elem)
2452 dma_unmap_sg(ap->host_set->dev, sg, qc->n_elem, dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002453 /* restore last sg */
2454 sg[qc->orig_n_elem - 1].length += qc->pad_len;
2455 if (pad_buf) {
2456 struct scatterlist *psg = &qc->pad_sgent;
2457 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2458 memcpy(addr + psg->offset, pad_buf, qc->pad_len);
2459 kunmap_atomic(psg->page, KM_IRQ0);
2460 }
2461 } else {
Jeff Garzike1410f22005-11-14 14:06:26 -05002462 if (sg_dma_len(&sg[0]) > 0)
2463 dma_unmap_single(ap->host_set->dev,
2464 sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
2465 dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002466 /* restore sg */
2467 sg->length += qc->pad_len;
2468 if (pad_buf)
2469 memcpy(qc->buf_virt + sg->length - qc->pad_len,
2470 pad_buf, qc->pad_len);
2471 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472
2473 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002474 qc->__sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475}
2476
2477/**
2478 * ata_fill_sg - Fill PCI IDE PRD table
2479 * @qc: Metadata associated with taskfile to be transferred
2480 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002481 * Fill PCI IDE PRD (scatter-gather) table with segments
2482 * associated with the current disk command.
2483 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 * LOCKING:
Jeff Garzik780a87f2005-05-30 15:41:05 -04002485 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 *
2487 */
2488static void ata_fill_sg(struct ata_queued_cmd *qc)
2489{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002491 struct scatterlist *sg;
2492 unsigned int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002494 assert(qc->__sg != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 assert(qc->n_elem > 0);
2496
2497 idx = 0;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002498 ata_for_each_sg(sg, qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 u32 addr, offset;
2500 u32 sg_len, len;
2501
2502 /* determine if physical DMA addr spans 64K boundary.
2503 * Note h/w doesn't support 64-bit, so we unconditionally
2504 * truncate dma_addr_t to u32.
2505 */
2506 addr = (u32) sg_dma_address(sg);
2507 sg_len = sg_dma_len(sg);
2508
2509 while (sg_len) {
2510 offset = addr & 0xffff;
2511 len = sg_len;
2512 if ((offset + sg_len) > 0x10000)
2513 len = 0x10000 - offset;
2514
2515 ap->prd[idx].addr = cpu_to_le32(addr);
2516 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
2517 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
2518
2519 idx++;
2520 sg_len -= len;
2521 addr += len;
2522 }
2523 }
2524
2525 if (idx)
2526 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2527}
2528/**
2529 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2530 * @qc: Metadata associated with taskfile to check
2531 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002532 * Allow low-level driver to filter ATA PACKET commands, returning
2533 * a status indicating whether or not it is OK to use DMA for the
2534 * supplied PACKET command.
2535 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002537 * spin_lock_irqsave(host_set lock)
2538 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 * RETURNS: 0 when ATAPI DMA can be used
2540 * nonzero otherwise
2541 */
2542int ata_check_atapi_dma(struct ata_queued_cmd *qc)
2543{
2544 struct ata_port *ap = qc->ap;
2545 int rc = 0; /* Assume ATAPI DMA is OK by default */
2546
2547 if (ap->ops->check_atapi_dma)
2548 rc = ap->ops->check_atapi_dma(qc);
2549
2550 return rc;
2551}
2552/**
2553 * ata_qc_prep - Prepare taskfile for submission
2554 * @qc: Metadata associated with taskfile to be prepared
2555 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002556 * Prepare ATA taskfile for submission.
2557 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 * LOCKING:
2559 * spin_lock_irqsave(host_set lock)
2560 */
2561void ata_qc_prep(struct ata_queued_cmd *qc)
2562{
2563 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2564 return;
2565
2566 ata_fill_sg(qc);
2567}
2568
Jeff Garzik0cba6322005-05-30 19:49:12 -04002569/**
2570 * ata_sg_init_one - Associate command with memory buffer
2571 * @qc: Command to be associated
2572 * @buf: Memory buffer
2573 * @buflen: Length of memory buffer, in bytes.
2574 *
2575 * Initialize the data-related elements of queued_cmd @qc
2576 * to point to a single memory buffer, @buf of byte length @buflen.
2577 *
2578 * LOCKING:
2579 * spin_lock_irqsave(host_set lock)
2580 */
2581
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
2583{
2584 struct scatterlist *sg;
2585
2586 qc->flags |= ATA_QCFLAG_SINGLE;
2587
2588 memset(&qc->sgent, 0, sizeof(qc->sgent));
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002589 qc->__sg = &qc->sgent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590 qc->n_elem = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002591 qc->orig_n_elem = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 qc->buf_virt = buf;
2593
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002594 sg = qc->__sg;
Jeff Garzikf0612bb2005-10-30 01:58:18 -05002595 sg_init_one(sg, buf, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596}
2597
Jeff Garzik0cba6322005-05-30 19:49:12 -04002598/**
2599 * ata_sg_init - Associate command with scatter-gather table.
2600 * @qc: Command to be associated
2601 * @sg: Scatter-gather table.
2602 * @n_elem: Number of elements in s/g table.
2603 *
2604 * Initialize the data-related elements of queued_cmd @qc
2605 * to point to a scatter-gather table @sg, containing @n_elem
2606 * elements.
2607 *
2608 * LOCKING:
2609 * spin_lock_irqsave(host_set lock)
2610 */
2611
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
2613 unsigned int n_elem)
2614{
2615 qc->flags |= ATA_QCFLAG_SG;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002616 qc->__sg = sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 qc->n_elem = n_elem;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002618 qc->orig_n_elem = n_elem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619}
2620
2621/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002622 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
2623 * @qc: Command with memory buffer to be mapped.
2624 *
2625 * DMA-map the memory buffer associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 *
2627 * LOCKING:
2628 * spin_lock_irqsave(host_set lock)
2629 *
2630 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002631 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 */
2633
2634static int ata_sg_setup_one(struct ata_queued_cmd *qc)
2635{
2636 struct ata_port *ap = qc->ap;
2637 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002638 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 dma_addr_t dma_address;
2640
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002641 /* we must lengthen transfers to end on a 32-bit boundary */
2642 qc->pad_len = sg->length & 3;
2643 if (qc->pad_len) {
2644 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2645 struct scatterlist *psg = &qc->pad_sgent;
2646
2647 assert(qc->dev->class == ATA_DEV_ATAPI);
2648
2649 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2650
2651 if (qc->tf.flags & ATA_TFLAG_WRITE)
2652 memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
2653 qc->pad_len);
2654
2655 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2656 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2657 /* trim sg */
2658 sg->length -= qc->pad_len;
2659
2660 DPRINTK("padding done, sg->length=%u pad_len=%u\n",
2661 sg->length, qc->pad_len);
2662 }
2663
Jeff Garzike1410f22005-11-14 14:06:26 -05002664 if (!sg->length) {
2665 sg_dma_address(sg) = 0;
2666 goto skip_map;
2667 }
2668
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 dma_address = dma_map_single(ap->host_set->dev, qc->buf_virt,
Albert Lee32529e02005-05-26 03:49:42 -04002670 sg->length, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05002671 if (dma_mapping_error(dma_address)) {
2672 /* restore sg */
2673 sg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05002675 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676
2677 sg_dma_address(sg) = dma_address;
Jeff Garzike1410f22005-11-14 14:06:26 -05002678skip_map:
Albert Lee32529e02005-05-26 03:49:42 -04002679 sg_dma_len(sg) = sg->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680
2681 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
2682 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2683
2684 return 0;
2685}
2686
2687/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002688 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
2689 * @qc: Command with scatter-gather table to be mapped.
2690 *
2691 * DMA-map the scatter-gather table associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692 *
2693 * LOCKING:
2694 * spin_lock_irqsave(host_set lock)
2695 *
2696 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002697 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 *
2699 */
2700
2701static int ata_sg_setup(struct ata_queued_cmd *qc)
2702{
2703 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002704 struct scatterlist *sg = qc->__sg;
2705 struct scatterlist *lsg = &sg[qc->n_elem - 1];
Jeff Garzike1410f22005-11-14 14:06:26 -05002706 int n_elem, pre_n_elem, dir, trim_sg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002707
2708 VPRINTK("ENTER, ata%u\n", ap->id);
2709 assert(qc->flags & ATA_QCFLAG_SG);
2710
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002711 /* we must lengthen transfers to end on a 32-bit boundary */
2712 qc->pad_len = lsg->length & 3;
2713 if (qc->pad_len) {
2714 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2715 struct scatterlist *psg = &qc->pad_sgent;
2716 unsigned int offset;
2717
2718 assert(qc->dev->class == ATA_DEV_ATAPI);
2719
2720 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2721
2722 /*
2723 * psg->page/offset are used to copy to-be-written
2724 * data in this function or read data in ata_sg_clean.
2725 */
2726 offset = lsg->offset + lsg->length - qc->pad_len;
2727 psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
2728 psg->offset = offset_in_page(offset);
2729
2730 if (qc->tf.flags & ATA_TFLAG_WRITE) {
2731 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2732 memcpy(pad_buf, addr + psg->offset, qc->pad_len);
2733 kunmap_atomic(psg->page, KM_IRQ0);
2734 }
2735
2736 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2737 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2738 /* trim last sg */
2739 lsg->length -= qc->pad_len;
Jeff Garzike1410f22005-11-14 14:06:26 -05002740 if (lsg->length == 0)
2741 trim_sg = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002742
2743 DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
2744 qc->n_elem - 1, lsg->length, qc->pad_len);
2745 }
2746
Jeff Garzike1410f22005-11-14 14:06:26 -05002747 pre_n_elem = qc->n_elem;
2748 if (trim_sg && pre_n_elem)
2749 pre_n_elem--;
2750
2751 if (!pre_n_elem) {
2752 n_elem = 0;
2753 goto skip_map;
2754 }
2755
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 dir = qc->dma_dir;
Jeff Garzike1410f22005-11-14 14:06:26 -05002757 n_elem = dma_map_sg(ap->host_set->dev, sg, pre_n_elem, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05002758 if (n_elem < 1) {
2759 /* restore last sg */
2760 lsg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05002762 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763
2764 DPRINTK("%d sg elements mapped\n", n_elem);
2765
Jeff Garzike1410f22005-11-14 14:06:26 -05002766skip_map:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 qc->n_elem = n_elem;
2768
2769 return 0;
2770}
2771
2772/**
Tejun Heo40e8c822005-08-22 17:12:45 +09002773 * ata_poll_qc_complete - turn irq back on and finish qc
2774 * @qc: Command to complete
Randy Dunlap8e8b77d2005-11-01 21:29:27 -08002775 * @err_mask: ATA status register content
Tejun Heo40e8c822005-08-22 17:12:45 +09002776 *
2777 * LOCKING:
2778 * None. (grabs host lock)
2779 */
2780
Albert Leea22e2eb2005-12-05 15:38:02 +08002781void ata_poll_qc_complete(struct ata_queued_cmd *qc)
Tejun Heo40e8c822005-08-22 17:12:45 +09002782{
2783 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04002784 unsigned long flags;
Tejun Heo40e8c822005-08-22 17:12:45 +09002785
Jeff Garzikb8f61532005-08-25 22:01:20 -04002786 spin_lock_irqsave(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09002787 ap->flags &= ~ATA_FLAG_NOINTR;
2788 ata_irq_on(ap);
Albert Leea22e2eb2005-12-05 15:38:02 +08002789 ata_qc_complete(qc);
Jeff Garzikb8f61532005-08-25 22:01:20 -04002790 spin_unlock_irqrestore(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09002791}
2792
2793/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 * ata_pio_poll -
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002795 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796 *
2797 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002798 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 *
2800 * RETURNS:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002801 * timeout value to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802 */
2803
2804static unsigned long ata_pio_poll(struct ata_port *ap)
2805{
Albert Leec14b8332005-12-05 15:36:08 +08002806 struct ata_queued_cmd *qc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 u8 status;
Albert Lee14be71f2005-09-27 17:36:35 +08002808 unsigned int poll_state = HSM_ST_UNKNOWN;
2809 unsigned int reg_state = HSM_ST_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810
Albert Leec14b8332005-12-05 15:36:08 +08002811 qc = ata_qc_from_tag(ap, ap->active_tag);
2812 assert(qc != NULL);
2813
Albert Lee14be71f2005-09-27 17:36:35 +08002814 switch (ap->hsm_task_state) {
2815 case HSM_ST:
2816 case HSM_ST_POLL:
2817 poll_state = HSM_ST_POLL;
2818 reg_state = HSM_ST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 break;
Albert Lee14be71f2005-09-27 17:36:35 +08002820 case HSM_ST_LAST:
2821 case HSM_ST_LAST_POLL:
2822 poll_state = HSM_ST_LAST_POLL;
2823 reg_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824 break;
2825 default:
2826 BUG();
2827 break;
2828 }
2829
2830 status = ata_chk_status(ap);
2831 if (status & ATA_BUSY) {
2832 if (time_after(jiffies, ap->pio_task_timeout)) {
Albert Lee1c848982005-12-05 15:40:15 +08002833 qc->err_mask |= AC_ERR_ATA_BUS;
Albert Lee7c398332005-11-09 13:03:30 +08002834 ap->hsm_task_state = HSM_ST_TMOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 return 0;
2836 }
Albert Lee14be71f2005-09-27 17:36:35 +08002837 ap->hsm_task_state = poll_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 return ATA_SHORT_PAUSE;
2839 }
2840
Albert Lee14be71f2005-09-27 17:36:35 +08002841 ap->hsm_task_state = reg_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 return 0;
2843}
2844
2845/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002846 * ata_pio_complete - check if drive is busy or idle
2847 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 *
2849 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002850 * None. (executing in kernel thread context)
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002851 *
2852 * RETURNS:
2853 * Non-zero if qc completed, zero otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 */
2855
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002856static int ata_pio_complete (struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857{
2858 struct ata_queued_cmd *qc;
2859 u8 drv_stat;
2860
2861 /*
Alan Cox31433ea2005-08-26 15:56:47 +01002862 * This is purely heuristic. This is a fast path. Sometimes when
2863 * we enter, BSY will be cleared in a chk-status or two. If not,
2864 * the drive is probably seeking or something. Snooze for a couple
2865 * msecs, then chk-status again. If still busy, fall back to
Albert Lee14be71f2005-09-27 17:36:35 +08002866 * HSM_ST_POLL state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867 */
2868 drv_stat = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 10);
2869 if (drv_stat & (ATA_BUSY | ATA_DRQ)) {
2870 msleep(2);
2871 drv_stat = ata_busy_wait(ap, ATA_BUSY | ATA_DRQ, 10);
2872 if (drv_stat & (ATA_BUSY | ATA_DRQ)) {
Albert Lee14be71f2005-09-27 17:36:35 +08002873 ap->hsm_task_state = HSM_ST_LAST_POLL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002875 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 }
2877 }
2878
Albert Leec14b8332005-12-05 15:36:08 +08002879 qc = ata_qc_from_tag(ap, ap->active_tag);
2880 assert(qc != NULL);
2881
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882 drv_stat = ata_wait_idle(ap);
2883 if (!ata_ok(drv_stat)) {
Albert Lee1c848982005-12-05 15:40:15 +08002884 qc->err_mask |= __ac_err_mask(drv_stat);
Albert Lee14be71f2005-09-27 17:36:35 +08002885 ap->hsm_task_state = HSM_ST_ERR;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002886 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 }
2888
Albert Lee14be71f2005-09-27 17:36:35 +08002889 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890
Albert Leea22e2eb2005-12-05 15:38:02 +08002891 assert(qc->err_mask == 0);
2892 ata_poll_qc_complete(qc);
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002893
2894 /* another command may start at this point */
2895
2896 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897}
2898
Edward Falk0baab862005-06-02 18:17:13 -04002899
2900/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002901 * swap_buf_le16 - swap halves of 16-words in place
Edward Falk0baab862005-06-02 18:17:13 -04002902 * @buf: Buffer to swap
2903 * @buf_words: Number of 16-bit words in buffer.
2904 *
2905 * Swap halves of 16-bit words if needed to convert from
2906 * little-endian byte order to native cpu byte order, or
2907 * vice-versa.
2908 *
2909 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002910 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04002911 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912void swap_buf_le16(u16 *buf, unsigned int buf_words)
2913{
2914#ifdef __BIG_ENDIAN
2915 unsigned int i;
2916
2917 for (i = 0; i < buf_words; i++)
2918 buf[i] = le16_to_cpu(buf[i]);
2919#endif /* __BIG_ENDIAN */
2920}
2921
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002922/**
2923 * ata_mmio_data_xfer - Transfer data by MMIO
2924 * @ap: port to read/write
2925 * @buf: data buffer
2926 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04002927 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002928 *
2929 * Transfer data from/to the device data register by MMIO.
2930 *
2931 * LOCKING:
2932 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002933 */
2934
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
2936 unsigned int buflen, int write_data)
2937{
2938 unsigned int i;
2939 unsigned int words = buflen >> 1;
2940 u16 *buf16 = (u16 *) buf;
2941 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
2942
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002943 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 if (write_data) {
2945 for (i = 0; i < words; i++)
2946 writew(le16_to_cpu(buf16[i]), mmio);
2947 } else {
2948 for (i = 0; i < words; i++)
2949 buf16[i] = cpu_to_le16(readw(mmio));
2950 }
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002951
2952 /* Transfer trailing 1 byte, if any. */
2953 if (unlikely(buflen & 0x01)) {
2954 u16 align_buf[1] = { 0 };
2955 unsigned char *trailing_buf = buf + buflen - 1;
2956
2957 if (write_data) {
2958 memcpy(align_buf, trailing_buf, 1);
2959 writew(le16_to_cpu(align_buf[0]), mmio);
2960 } else {
2961 align_buf[0] = cpu_to_le16(readw(mmio));
2962 memcpy(trailing_buf, align_buf, 1);
2963 }
2964 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965}
2966
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002967/**
2968 * ata_pio_data_xfer - Transfer data by PIO
2969 * @ap: port to read/write
2970 * @buf: data buffer
2971 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04002972 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002973 *
2974 * Transfer data from/to the device data register by PIO.
2975 *
2976 * LOCKING:
2977 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002978 */
2979
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
2981 unsigned int buflen, int write_data)
2982{
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002983 unsigned int words = buflen >> 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002985 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 if (write_data)
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002987 outsw(ap->ioaddr.data_addr, buf, words);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 else
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002989 insw(ap->ioaddr.data_addr, buf, words);
2990
2991 /* Transfer trailing 1 byte, if any. */
2992 if (unlikely(buflen & 0x01)) {
2993 u16 align_buf[1] = { 0 };
2994 unsigned char *trailing_buf = buf + buflen - 1;
2995
2996 if (write_data) {
2997 memcpy(align_buf, trailing_buf, 1);
2998 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
2999 } else {
3000 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
3001 memcpy(trailing_buf, align_buf, 1);
3002 }
3003 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004}
3005
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003006/**
3007 * ata_data_xfer - Transfer data from/to the data register.
3008 * @ap: port to read/write
3009 * @buf: data buffer
3010 * @buflen: buffer length
3011 * @do_write: read/write
3012 *
3013 * Transfer data from/to the device data register.
3014 *
3015 * LOCKING:
3016 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003017 */
3018
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
3020 unsigned int buflen, int do_write)
3021{
3022 if (ap->flags & ATA_FLAG_MMIO)
3023 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3024 else
3025 ata_pio_data_xfer(ap, buf, buflen, do_write);
3026}
3027
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003028/**
3029 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
3030 * @qc: Command on going
3031 *
3032 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
3033 *
3034 * LOCKING:
3035 * Inherited from caller.
3036 */
3037
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038static void ata_pio_sector(struct ata_queued_cmd *qc)
3039{
3040 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003041 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 struct ata_port *ap = qc->ap;
3043 struct page *page;
3044 unsigned int offset;
3045 unsigned char *buf;
3046
3047 if (qc->cursect == (qc->nsect - 1))
Albert Lee14be71f2005-09-27 17:36:35 +08003048 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049
3050 page = sg[qc->cursg].page;
3051 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
3052
3053 /* get the current page and offset */
3054 page = nth_page(page, (offset >> PAGE_SHIFT));
3055 offset %= PAGE_SIZE;
3056
3057 buf = kmap(page) + offset;
3058
3059 qc->cursect++;
3060 qc->cursg_ofs++;
3061
Albert Lee32529e02005-05-26 03:49:42 -04003062 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 qc->cursg++;
3064 qc->cursg_ofs = 0;
3065 }
3066
3067 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3068
3069 /* do the actual data transfer */
3070 do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3071 ata_data_xfer(ap, buf, ATA_SECT_SIZE, do_write);
3072
3073 kunmap(page);
3074}
3075
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003076/**
3077 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
3078 * @qc: Command on going
3079 * @bytes: number of bytes
3080 *
3081 * Transfer Transfer data from/to the ATAPI device.
3082 *
3083 * LOCKING:
3084 * Inherited from caller.
3085 *
3086 */
3087
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
3089{
3090 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003091 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 struct ata_port *ap = qc->ap;
3093 struct page *page;
3094 unsigned char *buf;
3095 unsigned int offset, count;
3096
Albert Lee563a6e12005-08-12 14:17:50 +08003097 if (qc->curbytes + bytes >= qc->nbytes)
Albert Lee14be71f2005-09-27 17:36:35 +08003098 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099
3100next_sg:
Albert Lee563a6e12005-08-12 14:17:50 +08003101 if (unlikely(qc->cursg >= qc->n_elem)) {
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003102 /*
Albert Lee563a6e12005-08-12 14:17:50 +08003103 * The end of qc->sg is reached and the device expects
3104 * more data to transfer. In order not to overrun qc->sg
3105 * and fulfill length specified in the byte count register,
3106 * - for read case, discard trailing data from the device
3107 * - for write case, padding zero data to the device
3108 */
3109 u16 pad_buf[1] = { 0 };
3110 unsigned int words = bytes >> 1;
3111 unsigned int i;
3112
3113 if (words) /* warning if bytes > 1 */
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003114 printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
Albert Lee563a6e12005-08-12 14:17:50 +08003115 ap->id, bytes);
3116
3117 for (i = 0; i < words; i++)
3118 ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
3119
Albert Lee14be71f2005-09-27 17:36:35 +08003120 ap->hsm_task_state = HSM_ST_LAST;
Albert Lee563a6e12005-08-12 14:17:50 +08003121 return;
3122 }
3123
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003124 sg = &qc->__sg[qc->cursg];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 page = sg->page;
3127 offset = sg->offset + qc->cursg_ofs;
3128
3129 /* get the current page and offset */
3130 page = nth_page(page, (offset >> PAGE_SHIFT));
3131 offset %= PAGE_SIZE;
3132
Albert Lee6952df02005-06-06 15:56:03 +08003133 /* don't overrun current sg */
Albert Lee32529e02005-05-26 03:49:42 -04003134 count = min(sg->length - qc->cursg_ofs, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135
3136 /* don't cross page boundaries */
3137 count = min(count, (unsigned int)PAGE_SIZE - offset);
3138
3139 buf = kmap(page) + offset;
3140
3141 bytes -= count;
3142 qc->curbytes += count;
3143 qc->cursg_ofs += count;
3144
Albert Lee32529e02005-05-26 03:49:42 -04003145 if (qc->cursg_ofs == sg->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 qc->cursg++;
3147 qc->cursg_ofs = 0;
3148 }
3149
3150 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3151
3152 /* do the actual data transfer */
3153 ata_data_xfer(ap, buf, count, do_write);
3154
3155 kunmap(page);
3156
Albert Lee563a6e12005-08-12 14:17:50 +08003157 if (bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 goto next_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003159}
3160
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003161/**
3162 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
3163 * @qc: Command on going
3164 *
3165 * Transfer Transfer data from/to the ATAPI device.
3166 *
3167 * LOCKING:
3168 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003169 */
3170
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171static void atapi_pio_bytes(struct ata_queued_cmd *qc)
3172{
3173 struct ata_port *ap = qc->ap;
3174 struct ata_device *dev = qc->dev;
3175 unsigned int ireason, bc_lo, bc_hi, bytes;
3176 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
3177
3178 ap->ops->tf_read(ap, &qc->tf);
3179 ireason = qc->tf.nsect;
3180 bc_lo = qc->tf.lbam;
3181 bc_hi = qc->tf.lbah;
3182 bytes = (bc_hi << 8) | bc_lo;
3183
3184 /* shall be cleared to zero, indicating xfer of data */
3185 if (ireason & (1 << 0))
3186 goto err_out;
3187
3188 /* make sure transfer direction matches expected */
3189 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
3190 if (do_write != i_write)
3191 goto err_out;
3192
3193 __atapi_pio_bytes(qc, bytes);
3194
3195 return;
3196
3197err_out:
3198 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
3199 ap->id, dev->devno);
Albert Lee1c848982005-12-05 15:40:15 +08003200 qc->err_mask |= AC_ERR_ATA_BUS;
Albert Lee14be71f2005-09-27 17:36:35 +08003201 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202}
3203
3204/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003205 * ata_pio_block - start PIO on a block
3206 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 *
3208 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003209 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 */
3211
3212static void ata_pio_block(struct ata_port *ap)
3213{
3214 struct ata_queued_cmd *qc;
3215 u8 status;
3216
3217 /*
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003218 * This is purely heuristic. This is a fast path.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 * Sometimes when we enter, BSY will be cleared in
3220 * a chk-status or two. If not, the drive is probably seeking
3221 * or something. Snooze for a couple msecs, then
3222 * chk-status again. If still busy, fall back to
Albert Lee14be71f2005-09-27 17:36:35 +08003223 * HSM_ST_POLL state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 */
3225 status = ata_busy_wait(ap, ATA_BUSY, 5);
3226 if (status & ATA_BUSY) {
3227 msleep(2);
3228 status = ata_busy_wait(ap, ATA_BUSY, 10);
3229 if (status & ATA_BUSY) {
Albert Lee14be71f2005-09-27 17:36:35 +08003230 ap->hsm_task_state = HSM_ST_POLL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
3232 return;
3233 }
3234 }
3235
3236 qc = ata_qc_from_tag(ap, ap->active_tag);
3237 assert(qc != NULL);
3238
3239 if (is_atapi_taskfile(&qc->tf)) {
3240 /* no more data to transfer or unsupported ATAPI command */
3241 if ((status & ATA_DRQ) == 0) {
Albert Lee14be71f2005-09-27 17:36:35 +08003242 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 return;
3244 }
3245
3246 atapi_pio_bytes(qc);
3247 } else {
3248 /* handle BSY=0, DRQ=0 as error */
3249 if ((status & ATA_DRQ) == 0) {
Albert Lee1c848982005-12-05 15:40:15 +08003250 qc->err_mask |= AC_ERR_ATA_BUS;
Albert Lee14be71f2005-09-27 17:36:35 +08003251 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 return;
3253 }
3254
3255 ata_pio_sector(qc);
3256 }
3257}
3258
3259static void ata_pio_error(struct ata_port *ap)
3260{
3261 struct ata_queued_cmd *qc;
Jeff Garzika7dac442005-10-30 04:44:42 -05003262
3263 printk(KERN_WARNING "ata%u: PIO error\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264
3265 qc = ata_qc_from_tag(ap, ap->active_tag);
3266 assert(qc != NULL);
3267
Albert Lee1c848982005-12-05 15:40:15 +08003268 /* make sure qc->err_mask is available to
3269 * know what's wrong and recover
3270 */
3271 assert(qc->err_mask);
3272
Albert Lee14be71f2005-09-27 17:36:35 +08003273 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274
Albert Leea22e2eb2005-12-05 15:38:02 +08003275 ata_poll_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003276}
3277
3278static void ata_pio_task(void *_data)
3279{
3280 struct ata_port *ap = _data;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003281 unsigned long timeout;
3282 int qc_completed;
3283
3284fsm_start:
3285 timeout = 0;
3286 qc_completed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287
Albert Lee14be71f2005-09-27 17:36:35 +08003288 switch (ap->hsm_task_state) {
3289 case HSM_ST_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290 return;
3291
Albert Lee14be71f2005-09-27 17:36:35 +08003292 case HSM_ST:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 ata_pio_block(ap);
3294 break;
3295
Albert Lee14be71f2005-09-27 17:36:35 +08003296 case HSM_ST_LAST:
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003297 qc_completed = ata_pio_complete(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 break;
3299
Albert Lee14be71f2005-09-27 17:36:35 +08003300 case HSM_ST_POLL:
3301 case HSM_ST_LAST_POLL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 timeout = ata_pio_poll(ap);
3303 break;
3304
Albert Lee14be71f2005-09-27 17:36:35 +08003305 case HSM_ST_TMOUT:
3306 case HSM_ST_ERR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 ata_pio_error(ap);
3308 return;
3309 }
3310
3311 if (timeout)
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003312 queue_delayed_work(ata_wq, &ap->pio_task, timeout);
3313 else if (!qc_completed)
3314 goto fsm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315}
3316
Linus Torvalds1da177e2005-04-16 15:20:36 -07003317/**
3318 * ata_qc_timeout - Handle timeout of queued command
3319 * @qc: Command that timed out
3320 *
3321 * Some part of the kernel (currently, only the SCSI layer)
3322 * has noticed that the active command on port @ap has not
3323 * completed after a specified length of time. Handle this
3324 * condition by disabling DMA (if necessary) and completing
3325 * transactions, with error if necessary.
3326 *
3327 * This also handles the case of the "lost interrupt", where
3328 * for some reason (possibly hardware bug, possibly driver bug)
3329 * an interrupt was not delivered to the driver, even though the
3330 * transaction completed successfully.
3331 *
3332 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003333 * Inherited from SCSI layer (none, can sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334 */
3335
3336static void ata_qc_timeout(struct ata_queued_cmd *qc)
3337{
3338 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003339 struct ata_host_set *host_set = ap->host_set;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 u8 host_stat = 0, drv_stat;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003341 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342
3343 DPRINTK("ENTER\n");
3344
Jeff Garzikb8f61532005-08-25 22:01:20 -04003345 spin_lock_irqsave(&host_set->lock, flags);
3346
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 /* hack alert! We cannot use the supplied completion
3348 * function from inside the ->eh_strategy_handler() thread.
3349 * libata is the only user of ->eh_strategy_handler() in
3350 * any kernel, so the default scsi_done() assumes it is
3351 * not being called from the SCSI EH.
3352 */
3353 qc->scsidone = scsi_finish_command;
3354
3355 switch (qc->tf.protocol) {
3356
3357 case ATA_PROT_DMA:
3358 case ATA_PROT_ATAPI_DMA:
3359 host_stat = ap->ops->bmdma_status(ap);
3360
3361 /* before we do anything else, clear DMA-Start bit */
Alan Coxb73fc892005-08-26 16:03:19 +01003362 ap->ops->bmdma_stop(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363
3364 /* fall through */
3365
3366 default:
3367 ata_altstatus(ap);
3368 drv_stat = ata_chk_status(ap);
3369
3370 /* ack bmdma irq events */
3371 ap->ops->irq_clear(ap);
3372
3373 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
3374 ap->id, qc->tf.command, drv_stat, host_stat);
3375
3376 /* complete taskfile transaction */
Albert Leea22e2eb2005-12-05 15:38:02 +08003377 qc->err_mask |= ac_err_mask(drv_stat);
3378 ata_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 break;
3380 }
Jeff Garzikb8f61532005-08-25 22:01:20 -04003381
3382 spin_unlock_irqrestore(&host_set->lock, flags);
3383
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 DPRINTK("EXIT\n");
3385}
3386
3387/**
3388 * ata_eng_timeout - Handle timeout of queued command
3389 * @ap: Port on which timed-out command is active
3390 *
3391 * Some part of the kernel (currently, only the SCSI layer)
3392 * has noticed that the active command on port @ap has not
3393 * completed after a specified length of time. Handle this
3394 * condition by disabling DMA (if necessary) and completing
3395 * transactions, with error if necessary.
3396 *
3397 * This also handles the case of the "lost interrupt", where
3398 * for some reason (possibly hardware bug, possibly driver bug)
3399 * an interrupt was not delivered to the driver, even though the
3400 * transaction completed successfully.
3401 *
3402 * LOCKING:
3403 * Inherited from SCSI layer (none, can sleep)
3404 */
3405
3406void ata_eng_timeout(struct ata_port *ap)
3407{
3408 struct ata_queued_cmd *qc;
3409
3410 DPRINTK("ENTER\n");
3411
3412 qc = ata_qc_from_tag(ap, ap->active_tag);
Jeff Garzike12669e2005-10-05 18:39:23 -04003413 if (qc)
3414 ata_qc_timeout(qc);
3415 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 printk(KERN_ERR "ata%u: BUG: timeout without command\n",
3417 ap->id);
3418 goto out;
3419 }
3420
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421out:
3422 DPRINTK("EXIT\n");
3423}
3424
3425/**
3426 * ata_qc_new - Request an available ATA command, for queueing
3427 * @ap: Port associated with device @dev
3428 * @dev: Device from whom we request an available command structure
3429 *
3430 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003431 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432 */
3433
3434static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
3435{
3436 struct ata_queued_cmd *qc = NULL;
3437 unsigned int i;
3438
3439 for (i = 0; i < ATA_MAX_QUEUE; i++)
3440 if (!test_and_set_bit(i, &ap->qactive)) {
3441 qc = ata_qc_from_tag(ap, i);
3442 break;
3443 }
3444
3445 if (qc)
3446 qc->tag = i;
3447
3448 return qc;
3449}
3450
3451/**
3452 * ata_qc_new_init - Request an available ATA command, and initialize it
3453 * @ap: Port associated with device @dev
3454 * @dev: Device from whom we request an available command structure
3455 *
3456 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003457 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 */
3459
3460struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
3461 struct ata_device *dev)
3462{
3463 struct ata_queued_cmd *qc;
3464
3465 qc = ata_qc_new(ap);
3466 if (qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467 qc->scsicmd = NULL;
3468 qc->ap = ap;
3469 qc->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003470
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05003471 ata_qc_reinit(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 }
3473
3474 return qc;
3475}
3476
Albert Leea22e2eb2005-12-05 15:38:02 +08003477int ata_qc_complete_noop(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478{
3479 return 0;
3480}
3481
3482static void __ata_qc_complete(struct ata_queued_cmd *qc)
3483{
3484 struct ata_port *ap = qc->ap;
3485 unsigned int tag, do_clear = 0;
3486
3487 qc->flags = 0;
3488 tag = qc->tag;
3489 if (likely(ata_tag_valid(tag))) {
3490 if (tag == ap->active_tag)
3491 ap->active_tag = ATA_TAG_POISON;
3492 qc->tag = ATA_TAG_POISON;
3493 do_clear = 1;
3494 }
3495
3496 if (qc->waiting) {
3497 struct completion *waiting = qc->waiting;
3498 qc->waiting = NULL;
3499 complete(waiting);
3500 }
3501
3502 if (likely(do_clear))
3503 clear_bit(tag, &ap->qactive);
3504}
3505
3506/**
3507 * ata_qc_free - free unused ata_queued_cmd
3508 * @qc: Command to complete
3509 *
3510 * Designed to free unused ata_queued_cmd object
3511 * in case something prevents using it.
3512 *
3513 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003514 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515 */
3516void ata_qc_free(struct ata_queued_cmd *qc)
3517{
3518 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
3519 assert(qc->waiting == NULL); /* nothing should be waiting */
3520
3521 __ata_qc_complete(qc);
3522}
3523
3524/**
3525 * ata_qc_complete - Complete an active ATA command
3526 * @qc: Command to complete
Randy Dunlap8e8b77d2005-11-01 21:29:27 -08003527 * @err_mask: ATA Status register contents
Jeff Garzik0cba6322005-05-30 19:49:12 -04003528 *
3529 * Indicate to the mid and upper layers that an ATA
3530 * command has completed, with either an ok or not-ok status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003531 *
3532 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003533 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534 */
3535
Albert Leea22e2eb2005-12-05 15:38:02 +08003536void ata_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537{
3538 int rc;
3539
3540 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
3541 assert(qc->flags & ATA_QCFLAG_ACTIVE);
3542
3543 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
3544 ata_sg_clean(qc);
3545
Albert Lee3f3791d2005-08-16 14:25:38 +08003546 /* atapi: mark qc as inactive to prevent the interrupt handler
3547 * from completing the command twice later, before the error handler
3548 * is called. (when rc != 0 and atapi request sense is needed)
3549 */
3550 qc->flags &= ~ATA_QCFLAG_ACTIVE;
3551
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 /* call completion callback */
Albert Leea22e2eb2005-12-05 15:38:02 +08003553 rc = qc->complete_fn(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554
3555 /* if callback indicates not to complete command (non-zero),
3556 * return immediately
3557 */
3558 if (rc != 0)
3559 return;
3560
3561 __ata_qc_complete(qc);
3562
3563 VPRINTK("EXIT\n");
3564}
3565
3566static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
3567{
3568 struct ata_port *ap = qc->ap;
3569
3570 switch (qc->tf.protocol) {
3571 case ATA_PROT_DMA:
3572 case ATA_PROT_ATAPI_DMA:
3573 return 1;
3574
3575 case ATA_PROT_ATAPI:
3576 case ATA_PROT_PIO:
3577 case ATA_PROT_PIO_MULT:
3578 if (ap->flags & ATA_FLAG_PIO_DMA)
3579 return 1;
3580
3581 /* fall through */
3582
3583 default:
3584 return 0;
3585 }
3586
3587 /* never reached */
3588}
3589
3590/**
3591 * ata_qc_issue - issue taskfile to device
3592 * @qc: command to issue to device
3593 *
3594 * Prepare an ATA command to submission to device.
3595 * This includes mapping the data into a DMA-able
3596 * area, filling in the S/G table, and finally
3597 * writing the taskfile to hardware, starting the command.
3598 *
3599 * LOCKING:
3600 * spin_lock_irqsave(host_set lock)
3601 *
3602 * RETURNS:
3603 * Zero on success, negative on error.
3604 */
3605
3606int ata_qc_issue(struct ata_queued_cmd *qc)
3607{
3608 struct ata_port *ap = qc->ap;
3609
3610 if (ata_should_dma_map(qc)) {
3611 if (qc->flags & ATA_QCFLAG_SG) {
3612 if (ata_sg_setup(qc))
3613 goto err_out;
3614 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
3615 if (ata_sg_setup_one(qc))
3616 goto err_out;
3617 }
3618 } else {
3619 qc->flags &= ~ATA_QCFLAG_DMAMAP;
3620 }
3621
3622 ap->ops->qc_prep(qc);
3623
3624 qc->ap->active_tag = qc->tag;
3625 qc->flags |= ATA_QCFLAG_ACTIVE;
3626
3627 return ap->ops->qc_issue(qc);
3628
3629err_out:
3630 return -1;
3631}
3632
Edward Falk0baab862005-06-02 18:17:13 -04003633
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634/**
3635 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
3636 * @qc: command to issue to device
3637 *
3638 * Using various libata functions and hooks, this function
3639 * starts an ATA command. ATA commands are grouped into
3640 * classes called "protocols", and issuing each type of protocol
3641 * is slightly different.
3642 *
Edward Falk0baab862005-06-02 18:17:13 -04003643 * May be used as the qc_issue() entry in ata_port_operations.
3644 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 * LOCKING:
3646 * spin_lock_irqsave(host_set lock)
3647 *
3648 * RETURNS:
3649 * Zero on success, negative on error.
3650 */
3651
3652int ata_qc_issue_prot(struct ata_queued_cmd *qc)
3653{
3654 struct ata_port *ap = qc->ap;
3655
3656 ata_dev_select(ap, qc->dev->devno, 1, 0);
3657
3658 switch (qc->tf.protocol) {
3659 case ATA_PROT_NODATA:
Jeff Garzike5338252005-10-30 21:37:17 -05003660 ata_tf_to_host(ap, &qc->tf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661 break;
3662
3663 case ATA_PROT_DMA:
3664 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3665 ap->ops->bmdma_setup(qc); /* set up bmdma */
3666 ap->ops->bmdma_start(qc); /* initiate bmdma */
3667 break;
3668
3669 case ATA_PROT_PIO: /* load tf registers, initiate polling pio */
3670 ata_qc_set_polling(qc);
Jeff Garzike5338252005-10-30 21:37:17 -05003671 ata_tf_to_host(ap, &qc->tf);
Albert Lee14be71f2005-09-27 17:36:35 +08003672 ap->hsm_task_state = HSM_ST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673 queue_work(ata_wq, &ap->pio_task);
3674 break;
3675
3676 case ATA_PROT_ATAPI:
3677 ata_qc_set_polling(qc);
Jeff Garzike5338252005-10-30 21:37:17 -05003678 ata_tf_to_host(ap, &qc->tf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 queue_work(ata_wq, &ap->packet_task);
3680 break;
3681
3682 case ATA_PROT_ATAPI_NODATA:
Tejun Heoc1389502005-08-22 14:59:24 +09003683 ap->flags |= ATA_FLAG_NOINTR;
Jeff Garzike5338252005-10-30 21:37:17 -05003684 ata_tf_to_host(ap, &qc->tf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685 queue_work(ata_wq, &ap->packet_task);
3686 break;
3687
3688 case ATA_PROT_ATAPI_DMA:
Tejun Heoc1389502005-08-22 14:59:24 +09003689 ap->flags |= ATA_FLAG_NOINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3691 ap->ops->bmdma_setup(qc); /* set up bmdma */
3692 queue_work(ata_wq, &ap->packet_task);
3693 break;
3694
3695 default:
3696 WARN_ON(1);
3697 return -1;
3698 }
3699
3700 return 0;
3701}
3702
3703/**
Edward Falk0baab862005-06-02 18:17:13 -04003704 * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705 * @qc: Info associated with this ATA transaction.
3706 *
3707 * LOCKING:
3708 * spin_lock_irqsave(host_set lock)
3709 */
3710
3711static void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc)
3712{
3713 struct ata_port *ap = qc->ap;
3714 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3715 u8 dmactl;
3716 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3717
3718 /* load PRD table addr. */
3719 mb(); /* make sure PRD table writes are visible to controller */
3720 writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
3721
3722 /* specify data direction, triple-check start bit is clear */
3723 dmactl = readb(mmio + ATA_DMA_CMD);
3724 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3725 if (!rw)
3726 dmactl |= ATA_DMA_WR;
3727 writeb(dmactl, mmio + ATA_DMA_CMD);
3728
3729 /* issue r/w command */
3730 ap->ops->exec_command(ap, &qc->tf);
3731}
3732
3733/**
Alan Coxb73fc892005-08-26 16:03:19 +01003734 * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735 * @qc: Info associated with this ATA transaction.
3736 *
3737 * LOCKING:
3738 * spin_lock_irqsave(host_set lock)
3739 */
3740
3741static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
3742{
3743 struct ata_port *ap = qc->ap;
3744 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3745 u8 dmactl;
3746
3747 /* start host DMA transaction */
3748 dmactl = readb(mmio + ATA_DMA_CMD);
3749 writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
3750
3751 /* Strictly, one may wish to issue a readb() here, to
3752 * flush the mmio write. However, control also passes
3753 * to the hardware at this point, and it will interrupt
3754 * us when we are to resume control. So, in effect,
3755 * we don't care when the mmio write flushes.
3756 * Further, a read of the DMA status register _immediately_
3757 * following the write may not be what certain flaky hardware
3758 * is expected, so I think it is best to not add a readb()
3759 * without first all the MMIO ATA cards/mobos.
3760 * Or maybe I'm just being paranoid.
3761 */
3762}
3763
3764/**
3765 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
3766 * @qc: Info associated with this ATA transaction.
3767 *
3768 * LOCKING:
3769 * spin_lock_irqsave(host_set lock)
3770 */
3771
3772static void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
3773{
3774 struct ata_port *ap = qc->ap;
3775 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3776 u8 dmactl;
3777
3778 /* load PRD table addr. */
3779 outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
3780
3781 /* specify data direction, triple-check start bit is clear */
3782 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3783 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3784 if (!rw)
3785 dmactl |= ATA_DMA_WR;
3786 outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3787
3788 /* issue r/w command */
3789 ap->ops->exec_command(ap, &qc->tf);
3790}
3791
3792/**
3793 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
3794 * @qc: Info associated with this ATA transaction.
3795 *
3796 * LOCKING:
3797 * spin_lock_irqsave(host_set lock)
3798 */
3799
3800static void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
3801{
3802 struct ata_port *ap = qc->ap;
3803 u8 dmactl;
3804
3805 /* start host DMA transaction */
3806 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3807 outb(dmactl | ATA_DMA_START,
3808 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3809}
3810
Edward Falk0baab862005-06-02 18:17:13 -04003811
3812/**
3813 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
3814 * @qc: Info associated with this ATA transaction.
3815 *
3816 * Writes the ATA_DMA_START flag to the DMA command register.
3817 *
3818 * May be used as the bmdma_start() entry in ata_port_operations.
3819 *
3820 * LOCKING:
3821 * spin_lock_irqsave(host_set lock)
3822 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823void ata_bmdma_start(struct ata_queued_cmd *qc)
3824{
3825 if (qc->ap->flags & ATA_FLAG_MMIO)
3826 ata_bmdma_start_mmio(qc);
3827 else
3828 ata_bmdma_start_pio(qc);
3829}
3830
Edward Falk0baab862005-06-02 18:17:13 -04003831
3832/**
3833 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
3834 * @qc: Info associated with this ATA transaction.
3835 *
3836 * Writes address of PRD table to device's PRD Table Address
3837 * register, sets the DMA control register, and calls
3838 * ops->exec_command() to start the transfer.
3839 *
3840 * May be used as the bmdma_setup() entry in ata_port_operations.
3841 *
3842 * LOCKING:
3843 * spin_lock_irqsave(host_set lock)
3844 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845void ata_bmdma_setup(struct ata_queued_cmd *qc)
3846{
3847 if (qc->ap->flags & ATA_FLAG_MMIO)
3848 ata_bmdma_setup_mmio(qc);
3849 else
3850 ata_bmdma_setup_pio(qc);
3851}
3852
Edward Falk0baab862005-06-02 18:17:13 -04003853
3854/**
3855 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
Jeff Garzikdecc6d02005-06-02 18:42:33 -04003856 * @ap: Port associated with this ATA transaction.
Edward Falk0baab862005-06-02 18:17:13 -04003857 *
3858 * Clear interrupt and error flags in DMA status register.
3859 *
3860 * May be used as the irq_clear() entry in ata_port_operations.
3861 *
3862 * LOCKING:
3863 * spin_lock_irqsave(host_set lock)
3864 */
3865
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866void ata_bmdma_irq_clear(struct ata_port *ap)
3867{
3868 if (ap->flags & ATA_FLAG_MMIO) {
3869 void __iomem *mmio = ((void __iomem *) ap->ioaddr.bmdma_addr) + ATA_DMA_STATUS;
3870 writeb(readb(mmio), mmio);
3871 } else {
3872 unsigned long addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
3873 outb(inb(addr), addr);
3874 }
3875
3876}
3877
Edward Falk0baab862005-06-02 18:17:13 -04003878
3879/**
3880 * ata_bmdma_status - Read PCI IDE BMDMA status
Jeff Garzikdecc6d02005-06-02 18:42:33 -04003881 * @ap: Port associated with this ATA transaction.
Edward Falk0baab862005-06-02 18:17:13 -04003882 *
3883 * Read and return BMDMA status register.
3884 *
3885 * May be used as the bmdma_status() entry in ata_port_operations.
3886 *
3887 * LOCKING:
3888 * spin_lock_irqsave(host_set lock)
3889 */
3890
Linus Torvalds1da177e2005-04-16 15:20:36 -07003891u8 ata_bmdma_status(struct ata_port *ap)
3892{
3893 u8 host_stat;
3894 if (ap->flags & ATA_FLAG_MMIO) {
3895 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3896 host_stat = readb(mmio + ATA_DMA_STATUS);
3897 } else
Albert Leeee500aa2005-09-27 17:34:38 +08003898 host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003899 return host_stat;
3900}
3901
Edward Falk0baab862005-06-02 18:17:13 -04003902
3903/**
3904 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
Alan Coxb73fc892005-08-26 16:03:19 +01003905 * @qc: Command we are ending DMA for
Edward Falk0baab862005-06-02 18:17:13 -04003906 *
3907 * Clears the ATA_DMA_START flag in the dma control register
3908 *
3909 * May be used as the bmdma_stop() entry in ata_port_operations.
3910 *
3911 * LOCKING:
3912 * spin_lock_irqsave(host_set lock)
3913 */
3914
Alan Coxb73fc892005-08-26 16:03:19 +01003915void ata_bmdma_stop(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916{
Alan Coxb73fc892005-08-26 16:03:19 +01003917 struct ata_port *ap = qc->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918 if (ap->flags & ATA_FLAG_MMIO) {
3919 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3920
3921 /* clear start/stop bit */
3922 writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
3923 mmio + ATA_DMA_CMD);
3924 } else {
3925 /* clear start/stop bit */
3926 outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
3927 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3928 }
3929
3930 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
3931 ata_altstatus(ap); /* dummy read */
3932}
3933
3934/**
3935 * ata_host_intr - Handle host interrupt for given (port, task)
3936 * @ap: Port on which interrupt arrived (possibly...)
3937 * @qc: Taskfile currently active in engine
3938 *
3939 * Handle host interrupt for given queued command. Currently,
3940 * only DMA interrupts are handled. All other commands are
3941 * handled via polling with interrupts disabled (nIEN bit).
3942 *
3943 * LOCKING:
3944 * spin_lock_irqsave(host_set lock)
3945 *
3946 * RETURNS:
3947 * One if interrupt was handled, zero if not (shared irq).
3948 */
3949
3950inline unsigned int ata_host_intr (struct ata_port *ap,
3951 struct ata_queued_cmd *qc)
3952{
3953 u8 status, host_stat;
3954
3955 switch (qc->tf.protocol) {
3956
3957 case ATA_PROT_DMA:
3958 case ATA_PROT_ATAPI_DMA:
3959 case ATA_PROT_ATAPI:
3960 /* check status of DMA engine */
3961 host_stat = ap->ops->bmdma_status(ap);
3962 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
3963
3964 /* if it's not our irq... */
3965 if (!(host_stat & ATA_DMA_INTR))
3966 goto idle_irq;
3967
3968 /* before we do anything else, clear DMA-Start bit */
Alan Coxb73fc892005-08-26 16:03:19 +01003969 ap->ops->bmdma_stop(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970
3971 /* fall through */
3972
3973 case ATA_PROT_ATAPI_NODATA:
3974 case ATA_PROT_NODATA:
3975 /* check altstatus */
3976 status = ata_altstatus(ap);
3977 if (status & ATA_BUSY)
3978 goto idle_irq;
3979
3980 /* check main status, clearing INTRQ */
3981 status = ata_chk_status(ap);
3982 if (unlikely(status & ATA_BUSY))
3983 goto idle_irq;
3984 DPRINTK("ata%u: protocol %d (dev_stat 0x%X)\n",
3985 ap->id, qc->tf.protocol, status);
3986
3987 /* ack bmdma irq events */
3988 ap->ops->irq_clear(ap);
3989
3990 /* complete taskfile transaction */
Albert Leea22e2eb2005-12-05 15:38:02 +08003991 qc->err_mask |= ac_err_mask(status);
3992 ata_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993 break;
3994
3995 default:
3996 goto idle_irq;
3997 }
3998
3999 return 1; /* irq handled */
4000
4001idle_irq:
4002 ap->stats.idle_irq++;
4003
4004#ifdef ATA_IRQ_TRAP
4005 if ((ap->stats.idle_irq % 1000) == 0) {
4006 handled = 1;
4007 ata_irq_ack(ap, 0); /* debug trap */
4008 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
4009 }
4010#endif
4011 return 0; /* irq not handled */
4012}
4013
4014/**
4015 * ata_interrupt - Default ATA host interrupt handler
Jeff Garzik0cba6322005-05-30 19:49:12 -04004016 * @irq: irq line (unused)
4017 * @dev_instance: pointer to our ata_host_set information structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018 * @regs: unused
4019 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004020 * Default interrupt handler for PCI IDE devices. Calls
4021 * ata_host_intr() for each port that is not disabled.
4022 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004024 * Obtains host_set lock during operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025 *
4026 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004027 * IRQ_NONE or IRQ_HANDLED.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004028 */
4029
4030irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
4031{
4032 struct ata_host_set *host_set = dev_instance;
4033 unsigned int i;
4034 unsigned int handled = 0;
4035 unsigned long flags;
4036
4037 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
4038 spin_lock_irqsave(&host_set->lock, flags);
4039
4040 for (i = 0; i < host_set->n_ports; i++) {
4041 struct ata_port *ap;
4042
4043 ap = host_set->ports[i];
Tejun Heoc1389502005-08-22 14:59:24 +09004044 if (ap &&
4045 !(ap->flags & (ATA_FLAG_PORT_DISABLED | ATA_FLAG_NOINTR))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046 struct ata_queued_cmd *qc;
4047
4048 qc = ata_qc_from_tag(ap, ap->active_tag);
Albert Lee21b1ed72005-04-29 17:34:59 +08004049 if (qc && (!(qc->tf.ctl & ATA_NIEN)) &&
4050 (qc->flags & ATA_QCFLAG_ACTIVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051 handled |= ata_host_intr(ap, qc);
4052 }
4053 }
4054
4055 spin_unlock_irqrestore(&host_set->lock, flags);
4056
4057 return IRQ_RETVAL(handled);
4058}
4059
4060/**
4061 * atapi_packet_task - Write CDB bytes to hardware
4062 * @_data: Port to which ATAPI device is attached.
4063 *
4064 * When device has indicated its readiness to accept
4065 * a CDB, this function is called. Send the CDB.
4066 * If DMA is to be performed, exit immediately.
4067 * Otherwise, we are in polling mode, so poll
4068 * status under operation succeeds or fails.
4069 *
4070 * LOCKING:
4071 * Kernel thread context (may sleep)
4072 */
4073
4074static void atapi_packet_task(void *_data)
4075{
4076 struct ata_port *ap = _data;
4077 struct ata_queued_cmd *qc;
4078 u8 status;
4079
4080 qc = ata_qc_from_tag(ap, ap->active_tag);
4081 assert(qc != NULL);
4082 assert(qc->flags & ATA_QCFLAG_ACTIVE);
4083
4084 /* sleep-wait for BSY to clear */
4085 DPRINTK("busy wait\n");
Albert Leed8fe4522005-12-05 15:42:17 +08004086 if (ata_busy_sleep(ap, ATA_TMOUT_CDB_QUICK, ATA_TMOUT_CDB)) {
4087 qc->err_mask |= AC_ERR_ATA_BUS;
4088 goto err_out;
4089 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090
4091 /* make sure DRQ is set */
4092 status = ata_chk_status(ap);
Albert Leed8fe4522005-12-05 15:42:17 +08004093 if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ) {
4094 qc->err_mask |= AC_ERR_ATA_BUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095 goto err_out;
Albert Leed8fe4522005-12-05 15:42:17 +08004096 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097
4098 /* send SCSI cdb */
4099 DPRINTK("send cdb\n");
4100 assert(ap->cdb_len >= 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101
Tejun Heoc1389502005-08-22 14:59:24 +09004102 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA ||
4103 qc->tf.protocol == ATA_PROT_ATAPI_NODATA) {
4104 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105
Tejun Heoc1389502005-08-22 14:59:24 +09004106 /* Once we're done issuing command and kicking bmdma,
4107 * irq handler takes over. To not lose irq, we need
4108 * to clear NOINTR flag before sending cdb, but
4109 * interrupt handler shouldn't be invoked before we're
4110 * finished. Hence, the following locking.
4111 */
4112 spin_lock_irqsave(&ap->host_set->lock, flags);
4113 ap->flags &= ~ATA_FLAG_NOINTR;
4114 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
4115 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA)
4116 ap->ops->bmdma_start(qc); /* initiate bmdma */
4117 spin_unlock_irqrestore(&ap->host_set->lock, flags);
4118 } else {
4119 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120
Tejun Heoc1389502005-08-22 14:59:24 +09004121 /* PIO commands are handled by polling */
Albert Lee14be71f2005-09-27 17:36:35 +08004122 ap->hsm_task_state = HSM_ST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123 queue_work(ata_wq, &ap->pio_task);
4124 }
4125
4126 return;
4127
4128err_out:
Albert Leea22e2eb2005-12-05 15:38:02 +08004129 ata_poll_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130}
4131
Edward Falk0baab862005-06-02 18:17:13 -04004132
4133/**
4134 * ata_port_start - Set port up for dma.
4135 * @ap: Port to initialize
4136 *
4137 * Called just after data structures for each port are
4138 * initialized. Allocates space for PRD table.
4139 *
4140 * May be used as the port_start() entry in ata_port_operations.
4141 *
4142 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004143 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04004144 */
4145
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146int ata_port_start (struct ata_port *ap)
4147{
4148 struct device *dev = ap->host_set->dev;
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004149 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150
4151 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
4152 if (!ap->prd)
4153 return -ENOMEM;
4154
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004155 rc = ata_pad_alloc(ap, dev);
4156 if (rc) {
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004157 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004158 return rc;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004159 }
4160
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
4162
4163 return 0;
4164}
4165
Edward Falk0baab862005-06-02 18:17:13 -04004166
4167/**
4168 * ata_port_stop - Undo ata_port_start()
4169 * @ap: Port to shut down
4170 *
4171 * Frees the PRD table.
4172 *
4173 * May be used as the port_stop() entry in ata_port_operations.
4174 *
4175 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004176 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04004177 */
4178
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179void ata_port_stop (struct ata_port *ap)
4180{
4181 struct device *dev = ap->host_set->dev;
4182
4183 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004184 ata_pad_free(ap, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185}
4186
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04004187void ata_host_stop (struct ata_host_set *host_set)
4188{
4189 if (host_set->mmio_base)
4190 iounmap(host_set->mmio_base);
4191}
4192
4193
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194/**
4195 * ata_host_remove - Unregister SCSI host structure with upper layers
4196 * @ap: Port to unregister
4197 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
4198 *
4199 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004200 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201 */
4202
4203static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
4204{
4205 struct Scsi_Host *sh = ap->host;
4206
4207 DPRINTK("ENTER\n");
4208
4209 if (do_unregister)
4210 scsi_remove_host(sh);
4211
4212 ap->ops->port_stop(ap);
4213}
4214
4215/**
4216 * ata_host_init - Initialize an ata_port structure
4217 * @ap: Structure to initialize
4218 * @host: associated SCSI mid-layer structure
4219 * @host_set: Collection of hosts to which @ap belongs
4220 * @ent: Probe information provided by low-level driver
4221 * @port_no: Port number associated with this ata_port
4222 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004223 * Initialize a new ata_port structure, and its associated
4224 * scsi_host.
4225 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004227 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228 */
4229
4230static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
4231 struct ata_host_set *host_set,
Jeff Garzik057ace52005-10-22 14:27:05 -04004232 const struct ata_probe_ent *ent, unsigned int port_no)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233{
4234 unsigned int i;
4235
4236 host->max_id = 16;
4237 host->max_lun = 1;
4238 host->max_channel = 1;
4239 host->unique_id = ata_unique_id++;
4240 host->max_cmd_len = 12;
Christoph Hellwig12413192005-06-11 01:05:01 +02004241
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242 ap->flags = ATA_FLAG_PORT_DISABLED;
4243 ap->id = host->unique_id;
4244 ap->host = host;
4245 ap->ctl = ATA_DEVCTL_OBS;
4246 ap->host_set = host_set;
4247 ap->port_no = port_no;
4248 ap->hard_port_no =
4249 ent->legacy_mode ? ent->hard_port_no : port_no;
4250 ap->pio_mask = ent->pio_mask;
4251 ap->mwdma_mask = ent->mwdma_mask;
4252 ap->udma_mask = ent->udma_mask;
4253 ap->flags |= ent->host_flags;
4254 ap->ops = ent->port_ops;
4255 ap->cbl = ATA_CBL_NONE;
4256 ap->active_tag = ATA_TAG_POISON;
4257 ap->last_ctl = 0xFF;
4258
4259 INIT_WORK(&ap->packet_task, atapi_packet_task, ap);
4260 INIT_WORK(&ap->pio_task, ata_pio_task, ap);
4261
4262 for (i = 0; i < ATA_MAX_DEVICES; i++)
4263 ap->device[i].devno = i;
4264
4265#ifdef ATA_IRQ_TRAP
4266 ap->stats.unhandled_irq = 1;
4267 ap->stats.idle_irq = 1;
4268#endif
4269
4270 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
4271}
4272
4273/**
4274 * ata_host_add - Attach low-level ATA driver to system
4275 * @ent: Information provided by low-level driver
4276 * @host_set: Collections of ports to which we add
4277 * @port_no: Port number associated with this host
4278 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004279 * Attach low-level ATA driver to system.
4280 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004282 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 *
4284 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004285 * New ata_port on success, for NULL on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 */
4287
Jeff Garzik057ace52005-10-22 14:27:05 -04004288static struct ata_port * ata_host_add(const struct ata_probe_ent *ent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004289 struct ata_host_set *host_set,
4290 unsigned int port_no)
4291{
4292 struct Scsi_Host *host;
4293 struct ata_port *ap;
4294 int rc;
4295
4296 DPRINTK("ENTER\n");
4297 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
4298 if (!host)
4299 return NULL;
4300
4301 ap = (struct ata_port *) &host->hostdata[0];
4302
4303 ata_host_init(ap, host, host_set, ent, port_no);
4304
4305 rc = ap->ops->port_start(ap);
4306 if (rc)
4307 goto err_out;
4308
4309 return ap;
4310
4311err_out:
4312 scsi_host_put(host);
4313 return NULL;
4314}
4315
4316/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004317 * ata_device_add - Register hardware device with ATA and SCSI layers
4318 * @ent: Probe information describing hardware device to be registered
4319 *
4320 * This function processes the information provided in the probe
4321 * information struct @ent, allocates the necessary ATA and SCSI
4322 * host information structures, initializes them, and registers
4323 * everything with requisite kernel subsystems.
4324 *
4325 * This function requests irqs, probes the ATA bus, and probes
4326 * the SCSI bus.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 *
4328 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004329 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330 *
4331 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004332 * Number of ports registered. Zero on error (no ports registered).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333 */
4334
Jeff Garzik057ace52005-10-22 14:27:05 -04004335int ata_device_add(const struct ata_probe_ent *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336{
4337 unsigned int count = 0, i;
4338 struct device *dev = ent->dev;
4339 struct ata_host_set *host_set;
4340
4341 DPRINTK("ENTER\n");
4342 /* alloc a container for our list of ATA ports (buses) */
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004343 host_set = kzalloc(sizeof(struct ata_host_set) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
4345 if (!host_set)
4346 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 spin_lock_init(&host_set->lock);
4348
4349 host_set->dev = dev;
4350 host_set->n_ports = ent->n_ports;
4351 host_set->irq = ent->irq;
4352 host_set->mmio_base = ent->mmio_base;
4353 host_set->private_data = ent->private_data;
4354 host_set->ops = ent->port_ops;
4355
4356 /* register each port bound to this device */
4357 for (i = 0; i < ent->n_ports; i++) {
4358 struct ata_port *ap;
4359 unsigned long xfer_mode_mask;
4360
4361 ap = ata_host_add(ent, host_set, i);
4362 if (!ap)
4363 goto err_out;
4364
4365 host_set->ports[i] = ap;
4366 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
4367 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
4368 (ap->pio_mask << ATA_SHIFT_PIO);
4369
4370 /* print per-port info to dmesg */
4371 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
4372 "bmdma 0x%lX irq %lu\n",
4373 ap->id,
4374 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
4375 ata_mode_string(xfer_mode_mask),
4376 ap->ioaddr.cmd_addr,
4377 ap->ioaddr.ctl_addr,
4378 ap->ioaddr.bmdma_addr,
4379 ent->irq);
4380
4381 ata_chk_status(ap);
4382 host_set->ops->irq_clear(ap);
4383 count++;
4384 }
4385
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004386 if (!count)
4387 goto err_free_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388
4389 /* obtain irq, that is shared between channels */
4390 if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
4391 DRV_NAME, host_set))
4392 goto err_out;
4393
4394 /* perform each probe synchronously */
4395 DPRINTK("probe begin\n");
4396 for (i = 0; i < count; i++) {
4397 struct ata_port *ap;
4398 int rc;
4399
4400 ap = host_set->ports[i];
4401
4402 DPRINTK("ata%u: probe begin\n", ap->id);
4403 rc = ata_bus_probe(ap);
4404 DPRINTK("ata%u: probe end\n", ap->id);
4405
4406 if (rc) {
4407 /* FIXME: do something useful here?
4408 * Current libata behavior will
4409 * tear down everything when
4410 * the module is removed
4411 * or the h/w is unplugged.
4412 */
4413 }
4414
4415 rc = scsi_add_host(ap->host, dev);
4416 if (rc) {
4417 printk(KERN_ERR "ata%u: scsi_add_host failed\n",
4418 ap->id);
4419 /* FIXME: do something useful here */
4420 /* FIXME: handle unconditional calls to
4421 * scsi_scan_host and ata_host_remove, below,
4422 * at the very least
4423 */
4424 }
4425 }
4426
4427 /* probes are done, now scan each port's disk(s) */
4428 DPRINTK("probe begin\n");
4429 for (i = 0; i < count; i++) {
4430 struct ata_port *ap = host_set->ports[i];
4431
Jeff Garzik644dd0c2005-10-03 15:55:19 -04004432 ata_scsi_scan_host(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433 }
4434
4435 dev_set_drvdata(dev, host_set);
4436
4437 VPRINTK("EXIT, returning %u\n", ent->n_ports);
4438 return ent->n_ports; /* success */
4439
4440err_out:
4441 for (i = 0; i < count; i++) {
4442 ata_host_remove(host_set->ports[i], 1);
4443 scsi_host_put(host_set->ports[i]->host);
4444 }
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004445err_free_ret:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446 kfree(host_set);
4447 VPRINTK("EXIT, returning 0\n");
4448 return 0;
4449}
4450
4451/**
Alan Cox17b14452005-09-15 15:44:00 +01004452 * ata_host_set_remove - PCI layer callback for device removal
4453 * @host_set: ATA host set that was removed
4454 *
4455 * Unregister all objects associated with this host set. Free those
4456 * objects.
4457 *
4458 * LOCKING:
4459 * Inherited from calling layer (may sleep).
4460 */
4461
Alan Cox17b14452005-09-15 15:44:00 +01004462void ata_host_set_remove(struct ata_host_set *host_set)
4463{
4464 struct ata_port *ap;
4465 unsigned int i;
4466
4467 for (i = 0; i < host_set->n_ports; i++) {
4468 ap = host_set->ports[i];
4469 scsi_remove_host(ap->host);
4470 }
4471
4472 free_irq(host_set->irq, host_set);
4473
4474 for (i = 0; i < host_set->n_ports; i++) {
4475 ap = host_set->ports[i];
4476
4477 ata_scsi_release(ap->host);
4478
4479 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
4480 struct ata_ioports *ioaddr = &ap->ioaddr;
4481
4482 if (ioaddr->cmd_addr == 0x1f0)
4483 release_region(0x1f0, 8);
4484 else if (ioaddr->cmd_addr == 0x170)
4485 release_region(0x170, 8);
4486 }
4487
4488 scsi_host_put(ap->host);
4489 }
4490
4491 if (host_set->ops->host_stop)
4492 host_set->ops->host_stop(host_set);
4493
4494 kfree(host_set);
4495}
4496
4497/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004498 * ata_scsi_release - SCSI layer callback hook for host unload
4499 * @host: libata host to be unloaded
4500 *
4501 * Performs all duties necessary to shut down a libata port...
4502 * Kill port kthread, disable port, and release resources.
4503 *
4504 * LOCKING:
4505 * Inherited from SCSI layer.
4506 *
4507 * RETURNS:
4508 * One.
4509 */
4510
4511int ata_scsi_release(struct Scsi_Host *host)
4512{
4513 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
4514
4515 DPRINTK("ENTER\n");
4516
4517 ap->ops->port_disable(ap);
4518 ata_host_remove(ap, 0);
4519
4520 DPRINTK("EXIT\n");
4521 return 1;
4522}
4523
4524/**
4525 * ata_std_ports - initialize ioaddr with standard port offsets.
4526 * @ioaddr: IO address structure to be initialized
Edward Falk0baab862005-06-02 18:17:13 -04004527 *
4528 * Utility function which initializes data_addr, error_addr,
4529 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
4530 * device_addr, status_addr, and command_addr to standard offsets
4531 * relative to cmd_addr.
4532 *
4533 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534 */
Edward Falk0baab862005-06-02 18:17:13 -04004535
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536void ata_std_ports(struct ata_ioports *ioaddr)
4537{
4538 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
4539 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
4540 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
4541 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
4542 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
4543 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
4544 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
4545 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
4546 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
4547 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
4548}
4549
4550static struct ata_probe_ent *
Jeff Garzik057ace52005-10-22 14:27:05 -04004551ata_probe_ent_alloc(struct device *dev, const struct ata_port_info *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552{
4553 struct ata_probe_ent *probe_ent;
4554
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004555 probe_ent = kzalloc(sizeof(*probe_ent), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556 if (!probe_ent) {
4557 printk(KERN_ERR DRV_NAME "(%s): out of memory\n",
4558 kobject_name(&(dev->kobj)));
4559 return NULL;
4560 }
4561
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562 INIT_LIST_HEAD(&probe_ent->node);
4563 probe_ent->dev = dev;
4564
4565 probe_ent->sht = port->sht;
4566 probe_ent->host_flags = port->host_flags;
4567 probe_ent->pio_mask = port->pio_mask;
4568 probe_ent->mwdma_mask = port->mwdma_mask;
4569 probe_ent->udma_mask = port->udma_mask;
4570 probe_ent->port_ops = port->port_ops;
4571
4572 return probe_ent;
4573}
4574
Edward Falk0baab862005-06-02 18:17:13 -04004575
4576
Jeff Garzik374b1872005-08-30 05:42:52 -04004577#ifdef CONFIG_PCI
4578
4579void ata_pci_host_stop (struct ata_host_set *host_set)
4580{
4581 struct pci_dev *pdev = to_pci_dev(host_set->dev);
4582
4583 pci_iounmap(pdev, host_set->mmio_base);
4584}
4585
Edward Falk0baab862005-06-02 18:17:13 -04004586/**
4587 * ata_pci_init_native_mode - Initialize native-mode driver
4588 * @pdev: pci device to be initialized
4589 * @port: array[2] of pointers to port info structures.
Alan Cox47a86592005-10-04 08:09:19 -04004590 * @ports: bitmap of ports present
Edward Falk0baab862005-06-02 18:17:13 -04004591 *
4592 * Utility function which allocates and initializes an
4593 * ata_probe_ent structure for a standard dual-port
4594 * PIO-based IDE controller. The returned ata_probe_ent
4595 * structure can be passed to ata_device_add(). The returned
4596 * ata_probe_ent structure should then be freed with kfree().
Alan Cox47a86592005-10-04 08:09:19 -04004597 *
4598 * The caller need only pass the address of the primary port, the
4599 * secondary will be deduced automatically. If the device has non
4600 * standard secondary port mappings this function can be called twice,
4601 * once for each interface.
Edward Falk0baab862005-06-02 18:17:13 -04004602 */
4603
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604struct ata_probe_ent *
Alan Cox47a86592005-10-04 08:09:19 -04004605ata_pci_init_native_mode(struct pci_dev *pdev, struct ata_port_info **port, int ports)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606{
4607 struct ata_probe_ent *probe_ent =
4608 ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
Alan Cox47a86592005-10-04 08:09:19 -04004609 int p = 0;
4610
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611 if (!probe_ent)
4612 return NULL;
4613
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614 probe_ent->irq = pdev->irq;
4615 probe_ent->irq_flags = SA_SHIRQ;
Alan Coxe99f8b52005-11-08 14:09:44 +00004616 probe_ent->private_data = port[0]->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617
Alan Cox47a86592005-10-04 08:09:19 -04004618 if (ports & ATA_PORT_PRIMARY) {
4619 probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 0);
4620 probe_ent->port[p].altstatus_addr =
4621 probe_ent->port[p].ctl_addr =
4622 pci_resource_start(pdev, 1) | ATA_PCI_CTL_OFS;
4623 probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4);
4624 ata_std_ports(&probe_ent->port[p]);
4625 p++;
4626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627
Alan Cox47a86592005-10-04 08:09:19 -04004628 if (ports & ATA_PORT_SECONDARY) {
4629 probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 2);
4630 probe_ent->port[p].altstatus_addr =
4631 probe_ent->port[p].ctl_addr =
4632 pci_resource_start(pdev, 3) | ATA_PCI_CTL_OFS;
4633 probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4) + 8;
4634 ata_std_ports(&probe_ent->port[p]);
4635 p++;
4636 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637
Alan Cox47a86592005-10-04 08:09:19 -04004638 probe_ent->n_ports = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639 return probe_ent;
4640}
4641
Jeff Garzik0f0d5192005-10-30 06:41:29 -05004642static struct ata_probe_ent *ata_pci_init_legacy_port(struct pci_dev *pdev, struct ata_port_info *port, int port_num)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643{
Alan Cox47a86592005-10-04 08:09:19 -04004644 struct ata_probe_ent *probe_ent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645
Jeff Garzik0f0d5192005-10-30 06:41:29 -05004646 probe_ent = ata_probe_ent_alloc(pci_dev_to_dev(pdev), port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647 if (!probe_ent)
4648 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650 probe_ent->legacy_mode = 1;
Alan Cox47a86592005-10-04 08:09:19 -04004651 probe_ent->n_ports = 1;
4652 probe_ent->hard_port_no = port_num;
Alan Coxe99f8b52005-11-08 14:09:44 +00004653 probe_ent->private_data = port->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654
Alan Cox47a86592005-10-04 08:09:19 -04004655 switch(port_num)
4656 {
4657 case 0:
4658 probe_ent->irq = 14;
4659 probe_ent->port[0].cmd_addr = 0x1f0;
4660 probe_ent->port[0].altstatus_addr =
4661 probe_ent->port[0].ctl_addr = 0x3f6;
4662 break;
4663 case 1:
4664 probe_ent->irq = 15;
4665 probe_ent->port[0].cmd_addr = 0x170;
4666 probe_ent->port[0].altstatus_addr =
4667 probe_ent->port[0].ctl_addr = 0x376;
4668 break;
4669 }
4670 probe_ent->port[0].bmdma_addr = pci_resource_start(pdev, 4) + 8 * port_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671 ata_std_ports(&probe_ent->port[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672 return probe_ent;
4673}
4674
4675/**
4676 * ata_pci_init_one - Initialize/register PCI IDE host controller
4677 * @pdev: Controller to be initialized
4678 * @port_info: Information from low-level host driver
4679 * @n_ports: Number of ports attached to host controller
4680 *
Edward Falk0baab862005-06-02 18:17:13 -04004681 * This is a helper function which can be called from a driver's
4682 * xxx_init_one() probe function if the hardware uses traditional
4683 * IDE taskfile registers.
4684 *
4685 * This function calls pci_enable_device(), reserves its register
4686 * regions, sets the dma mask, enables bus master mode, and calls
4687 * ata_device_add()
4688 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689 * LOCKING:
4690 * Inherited from PCI layer (may sleep).
4691 *
4692 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004693 * Zero on success, negative on errno-based value on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694 */
4695
4696int ata_pci_init_one (struct pci_dev *pdev, struct ata_port_info **port_info,
4697 unsigned int n_ports)
4698{
Alan Cox47a86592005-10-04 08:09:19 -04004699 struct ata_probe_ent *probe_ent = NULL, *probe_ent2 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700 struct ata_port_info *port[2];
4701 u8 tmp8, mask;
4702 unsigned int legacy_mode = 0;
4703 int disable_dev_on_err = 1;
4704 int rc;
4705
4706 DPRINTK("ENTER\n");
4707
4708 port[0] = port_info[0];
4709 if (n_ports > 1)
4710 port[1] = port_info[1];
4711 else
4712 port[1] = port[0];
4713
4714 if ((port[0]->host_flags & ATA_FLAG_NO_LEGACY) == 0
4715 && (pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
Alan Cox47a86592005-10-04 08:09:19 -04004716 /* TODO: What if one channel is in native mode ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717 pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
4718 mask = (1 << 2) | (1 << 0);
4719 if ((tmp8 & mask) != mask)
4720 legacy_mode = (1 << 3);
4721 }
4722
4723 /* FIXME... */
Alan Cox47a86592005-10-04 08:09:19 -04004724 if ((!legacy_mode) && (n_ports > 2)) {
4725 printk(KERN_ERR "ata: BUG: native mode, n_ports > 2\n");
4726 n_ports = 2;
4727 /* For now */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004728 }
4729
Alan Cox47a86592005-10-04 08:09:19 -04004730 /* FIXME: Really for ATA it isn't safe because the device may be
4731 multi-purpose and we want to leave it alone if it was already
4732 enabled. Secondly for shared use as Arjan says we want refcounting
4733
4734 Checking dev->is_enabled is insufficient as this is not set at
4735 boot for the primary video which is BIOS enabled
4736 */
4737
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738 rc = pci_enable_device(pdev);
4739 if (rc)
4740 return rc;
4741
4742 rc = pci_request_regions(pdev, DRV_NAME);
4743 if (rc) {
4744 disable_dev_on_err = 0;
4745 goto err_out;
4746 }
4747
Alan Cox47a86592005-10-04 08:09:19 -04004748 /* FIXME: Should use platform specific mappers for legacy port ranges */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749 if (legacy_mode) {
4750 if (!request_region(0x1f0, 8, "libata")) {
4751 struct resource *conflict, res;
4752 res.start = 0x1f0;
4753 res.end = 0x1f0 + 8 - 1;
4754 conflict = ____request_resource(&ioport_resource, &res);
4755 if (!strcmp(conflict->name, "libata"))
4756 legacy_mode |= (1 << 0);
4757 else {
4758 disable_dev_on_err = 0;
4759 printk(KERN_WARNING "ata: 0x1f0 IDE port busy\n");
4760 }
4761 } else
4762 legacy_mode |= (1 << 0);
4763
4764 if (!request_region(0x170, 8, "libata")) {
4765 struct resource *conflict, res;
4766 res.start = 0x170;
4767 res.end = 0x170 + 8 - 1;
4768 conflict = ____request_resource(&ioport_resource, &res);
4769 if (!strcmp(conflict->name, "libata"))
4770 legacy_mode |= (1 << 1);
4771 else {
4772 disable_dev_on_err = 0;
4773 printk(KERN_WARNING "ata: 0x170 IDE port busy\n");
4774 }
4775 } else
4776 legacy_mode |= (1 << 1);
4777 }
4778
4779 /* we have legacy mode, but all ports are unavailable */
4780 if (legacy_mode == (1 << 3)) {
4781 rc = -EBUSY;
4782 goto err_out_regions;
4783 }
4784
4785 rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
4786 if (rc)
4787 goto err_out_regions;
4788 rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
4789 if (rc)
4790 goto err_out_regions;
4791
4792 if (legacy_mode) {
Alan Cox47a86592005-10-04 08:09:19 -04004793 if (legacy_mode & (1 << 0))
Jeff Garzik0f0d5192005-10-30 06:41:29 -05004794 probe_ent = ata_pci_init_legacy_port(pdev, port[0], 0);
Alan Cox47a86592005-10-04 08:09:19 -04004795 if (legacy_mode & (1 << 1))
Jeff Garzik0f0d5192005-10-30 06:41:29 -05004796 probe_ent2 = ata_pci_init_legacy_port(pdev, port[1], 1);
Alan Cox47a86592005-10-04 08:09:19 -04004797 } else {
4798 if (n_ports == 2)
4799 probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY | ATA_PORT_SECONDARY);
4800 else
4801 probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY);
4802 }
4803 if (!probe_ent && !probe_ent2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804 rc = -ENOMEM;
4805 goto err_out_regions;
4806 }
4807
4808 pci_set_master(pdev);
4809
4810 /* FIXME: check ata_device_add return */
4811 if (legacy_mode) {
4812 if (legacy_mode & (1 << 0))
4813 ata_device_add(probe_ent);
4814 if (legacy_mode & (1 << 1))
4815 ata_device_add(probe_ent2);
4816 } else
4817 ata_device_add(probe_ent);
4818
4819 kfree(probe_ent);
4820 kfree(probe_ent2);
4821
4822 return 0;
4823
4824err_out_regions:
4825 if (legacy_mode & (1 << 0))
4826 release_region(0x1f0, 8);
4827 if (legacy_mode & (1 << 1))
4828 release_region(0x170, 8);
4829 pci_release_regions(pdev);
4830err_out:
4831 if (disable_dev_on_err)
4832 pci_disable_device(pdev);
4833 return rc;
4834}
4835
4836/**
4837 * ata_pci_remove_one - PCI layer callback for device removal
4838 * @pdev: PCI device that was removed
4839 *
4840 * PCI layer indicates to libata via this hook that
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004841 * hot-unplug or module unload event has occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004842 * Handle this by unregistering all objects associated
4843 * with this PCI device. Free those objects. Then finally
4844 * release PCI resources and disable device.
4845 *
4846 * LOCKING:
4847 * Inherited from PCI layer (may sleep).
4848 */
4849
4850void ata_pci_remove_one (struct pci_dev *pdev)
4851{
4852 struct device *dev = pci_dev_to_dev(pdev);
4853 struct ata_host_set *host_set = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854
Alan Cox17b14452005-09-15 15:44:00 +01004855 ata_host_set_remove(host_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856 pci_release_regions(pdev);
4857 pci_disable_device(pdev);
4858 dev_set_drvdata(dev, NULL);
4859}
4860
4861/* move to PCI subsystem */
Jeff Garzik057ace52005-10-22 14:27:05 -04004862int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863{
4864 unsigned long tmp = 0;
4865
4866 switch (bits->width) {
4867 case 1: {
4868 u8 tmp8 = 0;
4869 pci_read_config_byte(pdev, bits->reg, &tmp8);
4870 tmp = tmp8;
4871 break;
4872 }
4873 case 2: {
4874 u16 tmp16 = 0;
4875 pci_read_config_word(pdev, bits->reg, &tmp16);
4876 tmp = tmp16;
4877 break;
4878 }
4879 case 4: {
4880 u32 tmp32 = 0;
4881 pci_read_config_dword(pdev, bits->reg, &tmp32);
4882 tmp = tmp32;
4883 break;
4884 }
4885
4886 default:
4887 return -EINVAL;
4888 }
4889
4890 tmp &= bits->mask;
4891
4892 return (tmp == bits->val) ? 1 : 0;
4893}
4894#endif /* CONFIG_PCI */
4895
4896
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897static int __init ata_init(void)
4898{
4899 ata_wq = create_workqueue("ata");
4900 if (!ata_wq)
4901 return -ENOMEM;
4902
4903 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
4904 return 0;
4905}
4906
4907static void __exit ata_exit(void)
4908{
4909 destroy_workqueue(ata_wq);
4910}
4911
4912module_init(ata_init);
4913module_exit(ata_exit);
4914
Jeff Garzik67846b32005-10-05 02:58:32 -04004915static unsigned long ratelimit_time;
4916static spinlock_t ata_ratelimit_lock = SPIN_LOCK_UNLOCKED;
4917
4918int ata_ratelimit(void)
4919{
4920 int rc;
4921 unsigned long flags;
4922
4923 spin_lock_irqsave(&ata_ratelimit_lock, flags);
4924
4925 if (time_after(jiffies, ratelimit_time)) {
4926 rc = 1;
4927 ratelimit_time = jiffies + (HZ/5);
4928 } else
4929 rc = 0;
4930
4931 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
4932
4933 return rc;
4934}
4935
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936/*
4937 * libata is essentially a library of internal helper functions for
4938 * low-level ATA host controller drivers. As such, the API/ABI is
4939 * likely to change as new drivers are added and updated.
4940 * Do not depend on ABI/API stability.
4941 */
4942
4943EXPORT_SYMBOL_GPL(ata_std_bios_param);
4944EXPORT_SYMBOL_GPL(ata_std_ports);
4945EXPORT_SYMBOL_GPL(ata_device_add);
Alan Cox17b14452005-09-15 15:44:00 +01004946EXPORT_SYMBOL_GPL(ata_host_set_remove);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947EXPORT_SYMBOL_GPL(ata_sg_init);
4948EXPORT_SYMBOL_GPL(ata_sg_init_one);
4949EXPORT_SYMBOL_GPL(ata_qc_complete);
4950EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
4951EXPORT_SYMBOL_GPL(ata_eng_timeout);
4952EXPORT_SYMBOL_GPL(ata_tf_load);
4953EXPORT_SYMBOL_GPL(ata_tf_read);
4954EXPORT_SYMBOL_GPL(ata_noop_dev_select);
4955EXPORT_SYMBOL_GPL(ata_std_dev_select);
4956EXPORT_SYMBOL_GPL(ata_tf_to_fis);
4957EXPORT_SYMBOL_GPL(ata_tf_from_fis);
4958EXPORT_SYMBOL_GPL(ata_check_status);
4959EXPORT_SYMBOL_GPL(ata_altstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960EXPORT_SYMBOL_GPL(ata_exec_command);
4961EXPORT_SYMBOL_GPL(ata_port_start);
4962EXPORT_SYMBOL_GPL(ata_port_stop);
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04004963EXPORT_SYMBOL_GPL(ata_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964EXPORT_SYMBOL_GPL(ata_interrupt);
4965EXPORT_SYMBOL_GPL(ata_qc_prep);
4966EXPORT_SYMBOL_GPL(ata_bmdma_setup);
4967EXPORT_SYMBOL_GPL(ata_bmdma_start);
4968EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
4969EXPORT_SYMBOL_GPL(ata_bmdma_status);
4970EXPORT_SYMBOL_GPL(ata_bmdma_stop);
4971EXPORT_SYMBOL_GPL(ata_port_probe);
4972EXPORT_SYMBOL_GPL(sata_phy_reset);
4973EXPORT_SYMBOL_GPL(__sata_phy_reset);
4974EXPORT_SYMBOL_GPL(ata_bus_reset);
4975EXPORT_SYMBOL_GPL(ata_port_disable);
Jeff Garzik67846b32005-10-05 02:58:32 -04004976EXPORT_SYMBOL_GPL(ata_ratelimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
4978EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
4979EXPORT_SYMBOL_GPL(ata_scsi_error);
4980EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
4981EXPORT_SYMBOL_GPL(ata_scsi_release);
4982EXPORT_SYMBOL_GPL(ata_host_intr);
4983EXPORT_SYMBOL_GPL(ata_dev_classify);
4984EXPORT_SYMBOL_GPL(ata_dev_id_string);
Brad Campbell6f2f3812005-05-12 15:07:47 -04004985EXPORT_SYMBOL_GPL(ata_dev_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986EXPORT_SYMBOL_GPL(ata_scsi_simulate);
4987
Alan Cox452503f2005-10-21 19:01:32 -04004988EXPORT_SYMBOL_GPL(ata_timing_compute);
4989EXPORT_SYMBOL_GPL(ata_timing_merge);
4990
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991#ifdef CONFIG_PCI
4992EXPORT_SYMBOL_GPL(pci_test_config_bits);
Jeff Garzik374b1872005-08-30 05:42:52 -04004993EXPORT_SYMBOL_GPL(ata_pci_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
4995EXPORT_SYMBOL_GPL(ata_pci_init_one);
4996EXPORT_SYMBOL_GPL(ata_pci_remove_one);
4997#endif /* CONFIG_PCI */