blob: bdfb0a88cd6f675975cc44a172484c8e9085b2f2 [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 Lee8bf62ec2005-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,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100565 0,
566 0,
567 0,
568 ATA_CMD_WRITE_MULTI_FUA_EXT,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800569 /* pio */
570 ATA_CMD_PIO_READ,
571 ATA_CMD_PIO_WRITE,
572 ATA_CMD_PIO_READ_EXT,
573 ATA_CMD_PIO_WRITE_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100574 0,
575 0,
576 0,
577 0,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800578 /* dma */
579 ATA_CMD_READ,
580 ATA_CMD_WRITE,
581 ATA_CMD_READ_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100582 ATA_CMD_WRITE_EXT,
583 0,
584 0,
585 0,
586 ATA_CMD_WRITE_FUA_EXT
Albert Lee8cbd6df2005-10-12 15:06:27 +0800587};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
589/**
Albert Lee8cbd6df2005-10-12 15:06:27 +0800590 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
591 * @qc: command to examine and configure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 *
Albert Lee8cbd6df2005-10-12 15:06:27 +0800593 * Examine the device configuration and tf->flags to calculate
594 * the proper read/write commands and protocol to use.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 *
596 * LOCKING:
597 * caller.
598 */
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100599int ata_rwcmd_protocol(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600{
Albert Lee8cbd6df2005-10-12 15:06:27 +0800601 struct ata_taskfile *tf = &qc->tf;
602 struct ata_device *dev = qc->dev;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100603 u8 cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100605 int index, fua, lba48, write;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800606
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100607 fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800608 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
609 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610
Albert Lee8cbd6df2005-10-12 15:06:27 +0800611 if (dev->flags & ATA_DFLAG_PIO) {
612 tf->protocol = ATA_PROT_PIO;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100613 index = dev->multi_count ? 0 : 8;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800614 } else {
615 tf->protocol = ATA_PROT_DMA;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100616 index = 16;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800617 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100619 cmd = ata_rw_cmds[index + fua + lba48 + write];
620 if (cmd) {
621 tf->command = cmd;
622 return 0;
623 }
624 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625}
626
Arjan van de Ven98ac62d2005-11-28 10:06:23 +0100627static const char * const xfer_mode_str[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 "UDMA/16",
629 "UDMA/25",
630 "UDMA/33",
631 "UDMA/44",
632 "UDMA/66",
633 "UDMA/100",
634 "UDMA/133",
635 "UDMA7",
636 "MWDMA0",
637 "MWDMA1",
638 "MWDMA2",
639 "PIO0",
640 "PIO1",
641 "PIO2",
642 "PIO3",
643 "PIO4",
644};
645
646/**
647 * ata_udma_string - convert UDMA bit offset to string
648 * @mask: mask of bits supported; only highest bit counts.
649 *
650 * Determine string which represents the highest speed
651 * (highest bit in @udma_mask).
652 *
653 * LOCKING:
654 * None.
655 *
656 * RETURNS:
657 * Constant C string representing highest speed listed in
658 * @udma_mask, or the constant C string "<n/a>".
659 */
660
661static const char *ata_mode_string(unsigned int mask)
662{
663 int i;
664
665 for (i = 7; i >= 0; i--)
666 if (mask & (1 << i))
667 goto out;
668 for (i = ATA_SHIFT_MWDMA + 2; i >= ATA_SHIFT_MWDMA; i--)
669 if (mask & (1 << i))
670 goto out;
671 for (i = ATA_SHIFT_PIO + 4; i >= ATA_SHIFT_PIO; i--)
672 if (mask & (1 << i))
673 goto out;
674
675 return "<n/a>";
676
677out:
678 return xfer_mode_str[i];
679}
680
681/**
682 * ata_pio_devchk - PATA device presence detection
683 * @ap: ATA channel to examine
684 * @device: Device to examine (starting at zero)
685 *
686 * This technique was originally described in
687 * Hale Landis's ATADRVR (www.ata-atapi.com), and
688 * later found its way into the ATA/ATAPI spec.
689 *
690 * Write a pattern to the ATA shadow registers,
691 * and if a device is present, it will respond by
692 * correctly storing and echoing back the
693 * ATA shadow register contents.
694 *
695 * LOCKING:
696 * caller.
697 */
698
699static unsigned int ata_pio_devchk(struct ata_port *ap,
700 unsigned int device)
701{
702 struct ata_ioports *ioaddr = &ap->ioaddr;
703 u8 nsect, lbal;
704
705 ap->ops->dev_select(ap, device);
706
707 outb(0x55, ioaddr->nsect_addr);
708 outb(0xaa, ioaddr->lbal_addr);
709
710 outb(0xaa, ioaddr->nsect_addr);
711 outb(0x55, ioaddr->lbal_addr);
712
713 outb(0x55, ioaddr->nsect_addr);
714 outb(0xaa, ioaddr->lbal_addr);
715
716 nsect = inb(ioaddr->nsect_addr);
717 lbal = inb(ioaddr->lbal_addr);
718
719 if ((nsect == 0x55) && (lbal == 0xaa))
720 return 1; /* we found a device */
721
722 return 0; /* nothing found */
723}
724
725/**
726 * ata_mmio_devchk - PATA device presence detection
727 * @ap: ATA channel to examine
728 * @device: Device to examine (starting at zero)
729 *
730 * This technique was originally described in
731 * Hale Landis's ATADRVR (www.ata-atapi.com), and
732 * later found its way into the ATA/ATAPI spec.
733 *
734 * Write a pattern to the ATA shadow registers,
735 * and if a device is present, it will respond by
736 * correctly storing and echoing back the
737 * ATA shadow register contents.
738 *
739 * LOCKING:
740 * caller.
741 */
742
743static unsigned int ata_mmio_devchk(struct ata_port *ap,
744 unsigned int device)
745{
746 struct ata_ioports *ioaddr = &ap->ioaddr;
747 u8 nsect, lbal;
748
749 ap->ops->dev_select(ap, device);
750
751 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
752 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
753
754 writeb(0xaa, (void __iomem *) ioaddr->nsect_addr);
755 writeb(0x55, (void __iomem *) ioaddr->lbal_addr);
756
757 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
758 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
759
760 nsect = readb((void __iomem *) ioaddr->nsect_addr);
761 lbal = readb((void __iomem *) ioaddr->lbal_addr);
762
763 if ((nsect == 0x55) && (lbal == 0xaa))
764 return 1; /* we found a device */
765
766 return 0; /* nothing found */
767}
768
769/**
770 * ata_devchk - PATA device presence detection
771 * @ap: ATA channel to examine
772 * @device: Device to examine (starting at zero)
773 *
774 * Dispatch ATA device presence detection, depending
775 * on whether we are using PIO or MMIO to talk to the
776 * ATA shadow registers.
777 *
778 * LOCKING:
779 * caller.
780 */
781
782static unsigned int ata_devchk(struct ata_port *ap,
783 unsigned int device)
784{
785 if (ap->flags & ATA_FLAG_MMIO)
786 return ata_mmio_devchk(ap, device);
787 return ata_pio_devchk(ap, device);
788}
789
790/**
791 * ata_dev_classify - determine device type based on ATA-spec signature
792 * @tf: ATA taskfile register set for device to be identified
793 *
794 * Determine from taskfile register contents whether a device is
795 * ATA or ATAPI, as per "Signature and persistence" section
796 * of ATA/PI spec (volume 1, sect 5.14).
797 *
798 * LOCKING:
799 * None.
800 *
801 * RETURNS:
802 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
803 * the event of failure.
804 */
805
Jeff Garzik057ace52005-10-22 14:27:05 -0400806unsigned int ata_dev_classify(const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
808 /* Apple's open source Darwin code hints that some devices only
809 * put a proper signature into the LBA mid/high registers,
810 * So, we only check those. It's sufficient for uniqueness.
811 */
812
813 if (((tf->lbam == 0) && (tf->lbah == 0)) ||
814 ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
815 DPRINTK("found ATA device by sig\n");
816 return ATA_DEV_ATA;
817 }
818
819 if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
820 ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
821 DPRINTK("found ATAPI device by sig\n");
822 return ATA_DEV_ATAPI;
823 }
824
825 DPRINTK("unknown device\n");
826 return ATA_DEV_UNKNOWN;
827}
828
829/**
830 * ata_dev_try_classify - Parse returned ATA device signature
831 * @ap: ATA channel to examine
832 * @device: Device to examine (starting at zero)
833 *
834 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
835 * an ATA/ATAPI-defined set of values is placed in the ATA
836 * shadow registers, indicating the results of device detection
837 * and diagnostics.
838 *
839 * Select the ATA device, and read the values from the ATA shadow
840 * registers. Then parse according to the Error register value,
841 * and the spec-defined values examined by ata_dev_classify().
842 *
843 * LOCKING:
844 * caller.
845 */
846
847static u8 ata_dev_try_classify(struct ata_port *ap, unsigned int device)
848{
849 struct ata_device *dev = &ap->device[device];
850 struct ata_taskfile tf;
851 unsigned int class;
852 u8 err;
853
854 ap->ops->dev_select(ap, device);
855
856 memset(&tf, 0, sizeof(tf));
857
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 ap->ops->tf_read(ap, &tf);
Jeff Garzik0169e282005-10-29 21:25:10 -0400859 err = tf.feature;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
861 dev->class = ATA_DEV_NONE;
862
863 /* see if device passed diags */
864 if (err == 1)
865 /* do nothing */ ;
866 else if ((device == 0) && (err == 0x81))
867 /* do nothing */ ;
868 else
869 return err;
870
871 /* determine if device if ATA or ATAPI */
872 class = ata_dev_classify(&tf);
873 if (class == ATA_DEV_UNKNOWN)
874 return err;
875 if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
876 return err;
877
878 dev->class = class;
879
880 return err;
881}
882
883/**
884 * ata_dev_id_string - Convert IDENTIFY DEVICE page into string
885 * @id: IDENTIFY DEVICE results we will examine
886 * @s: string into which data is output
887 * @ofs: offset into identify device page
888 * @len: length of string to return. must be an even number.
889 *
890 * The strings in the IDENTIFY DEVICE page are broken up into
891 * 16-bit chunks. Run through the string, and output each
892 * 8-bit chunk linearly, regardless of platform.
893 *
894 * LOCKING:
895 * caller.
896 */
897
Jeff Garzik057ace52005-10-22 14:27:05 -0400898void ata_dev_id_string(const u16 *id, unsigned char *s,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 unsigned int ofs, unsigned int len)
900{
901 unsigned int c;
902
903 while (len > 0) {
904 c = id[ofs] >> 8;
905 *s = c;
906 s++;
907
908 c = id[ofs] & 0xff;
909 *s = c;
910 s++;
911
912 ofs++;
913 len -= 2;
914 }
915}
916
Edward Falk0baab862005-06-02 18:17:13 -0400917
918/**
919 * ata_noop_dev_select - Select device 0/1 on ATA bus
920 * @ap: ATA channel to manipulate
921 * @device: ATA device (numbered from zero) to select
922 *
923 * This function performs no actual function.
924 *
925 * May be used as the dev_select() entry in ata_port_operations.
926 *
927 * LOCKING:
928 * caller.
929 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
931{
932}
933
Edward Falk0baab862005-06-02 18:17:13 -0400934
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935/**
936 * ata_std_dev_select - Select device 0/1 on ATA bus
937 * @ap: ATA channel to manipulate
938 * @device: ATA device (numbered from zero) to select
939 *
940 * Use the method defined in the ATA specification to
941 * make either device 0, or device 1, active on the
Edward Falk0baab862005-06-02 18:17:13 -0400942 * ATA channel. Works with both PIO and MMIO.
943 *
944 * May be used as the dev_select() entry in ata_port_operations.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 *
946 * LOCKING:
947 * caller.
948 */
949
950void ata_std_dev_select (struct ata_port *ap, unsigned int device)
951{
952 u8 tmp;
953
954 if (device == 0)
955 tmp = ATA_DEVICE_OBS;
956 else
957 tmp = ATA_DEVICE_OBS | ATA_DEV1;
958
959 if (ap->flags & ATA_FLAG_MMIO) {
960 writeb(tmp, (void __iomem *) ap->ioaddr.device_addr);
961 } else {
962 outb(tmp, ap->ioaddr.device_addr);
963 }
964 ata_pause(ap); /* needed; also flushes, for mmio */
965}
966
967/**
968 * ata_dev_select - Select device 0/1 on ATA bus
969 * @ap: ATA channel to manipulate
970 * @device: ATA device (numbered from zero) to select
971 * @wait: non-zero to wait for Status register BSY bit to clear
972 * @can_sleep: non-zero if context allows sleeping
973 *
974 * Use the method defined in the ATA specification to
975 * make either device 0, or device 1, active on the
976 * ATA channel.
977 *
978 * This is a high-level version of ata_std_dev_select(),
979 * which additionally provides the services of inserting
980 * the proper pauses and status polling, where needed.
981 *
982 * LOCKING:
983 * caller.
984 */
985
986void ata_dev_select(struct ata_port *ap, unsigned int device,
987 unsigned int wait, unsigned int can_sleep)
988{
989 VPRINTK("ENTER, ata%u: device %u, wait %u\n",
990 ap->id, device, wait);
991
992 if (wait)
993 ata_wait_idle(ap);
994
995 ap->ops->dev_select(ap, device);
996
997 if (wait) {
998 if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
999 msleep(150);
1000 ata_wait_idle(ap);
1001 }
1002}
1003
1004/**
1005 * ata_dump_id - IDENTIFY DEVICE info debugging output
1006 * @dev: Device whose IDENTIFY DEVICE page we will dump
1007 *
1008 * Dump selected 16-bit words from a detected device's
1009 * IDENTIFY PAGE page.
1010 *
1011 * LOCKING:
1012 * caller.
1013 */
1014
Jeff Garzik057ace52005-10-22 14:27:05 -04001015static inline void ata_dump_id(const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016{
1017 DPRINTK("49==0x%04x "
1018 "53==0x%04x "
1019 "63==0x%04x "
1020 "64==0x%04x "
1021 "75==0x%04x \n",
1022 dev->id[49],
1023 dev->id[53],
1024 dev->id[63],
1025 dev->id[64],
1026 dev->id[75]);
1027 DPRINTK("80==0x%04x "
1028 "81==0x%04x "
1029 "82==0x%04x "
1030 "83==0x%04x "
1031 "84==0x%04x \n",
1032 dev->id[80],
1033 dev->id[81],
1034 dev->id[82],
1035 dev->id[83],
1036 dev->id[84]);
1037 DPRINTK("88==0x%04x "
1038 "93==0x%04x\n",
1039 dev->id[88],
1040 dev->id[93]);
1041}
1042
Alan Cox11e29e22005-10-21 18:46:32 -04001043/*
1044 * Compute the PIO modes available for this device. This is not as
1045 * trivial as it seems if we must consider early devices correctly.
1046 *
1047 * FIXME: pre IDE drive timing (do we care ?).
1048 */
1049
Jeff Garzik057ace52005-10-22 14:27:05 -04001050static unsigned int ata_pio_modes(const struct ata_device *adev)
Alan Cox11e29e22005-10-21 18:46:32 -04001051{
1052 u16 modes;
1053
1054 /* Usual case. Word 53 indicates word 88 is valid */
1055 if (adev->id[ATA_ID_FIELD_VALID] & (1 << 2)) {
1056 modes = adev->id[ATA_ID_PIO_MODES] & 0x03;
1057 modes <<= 3;
1058 modes |= 0x7;
1059 return modes;
1060 }
1061
1062 /* If word 88 isn't valid then Word 51 holds the PIO timing number
1063 for the maximum. Turn it into a mask and return it */
1064 modes = (2 << (adev->id[ATA_ID_OLD_PIO_MODES] & 0xFF)) - 1 ;
1065 return modes;
1066}
1067
Tejun Heoa2a7a662005-12-13 14:48:31 +09001068struct ata_exec_internal_arg {
1069 unsigned int err_mask;
1070 struct ata_taskfile *tf;
1071 struct completion *waiting;
1072};
1073
1074int ata_qc_complete_internal(struct ata_queued_cmd *qc)
1075{
1076 struct ata_exec_internal_arg *arg = qc->private_data;
1077 struct completion *waiting = arg->waiting;
1078
1079 if (!(qc->err_mask & ~AC_ERR_DEV))
1080 qc->ap->ops->tf_read(qc->ap, arg->tf);
1081 arg->err_mask = qc->err_mask;
1082 arg->waiting = NULL;
1083 complete(waiting);
1084
1085 return 0;
1086}
1087
1088/**
1089 * ata_exec_internal - execute libata internal command
1090 * @ap: Port to which the command is sent
1091 * @dev: Device to which the command is sent
1092 * @tf: Taskfile registers for the command and the result
1093 * @dma_dir: Data tranfer direction of the command
1094 * @buf: Data buffer of the command
1095 * @buflen: Length of data buffer
1096 *
1097 * Executes libata internal command with timeout. @tf contains
1098 * command on entry and result on return. Timeout and error
1099 * conditions are reported via return value. No recovery action
1100 * is taken after a command times out. It's caller's duty to
1101 * clean up after timeout.
1102 *
1103 * LOCKING:
1104 * None. Should be called with kernel context, might sleep.
1105 */
1106
1107static unsigned
1108ata_exec_internal(struct ata_port *ap, struct ata_device *dev,
1109 struct ata_taskfile *tf,
1110 int dma_dir, void *buf, unsigned int buflen)
1111{
1112 u8 command = tf->command;
1113 struct ata_queued_cmd *qc;
1114 DECLARE_COMPLETION(wait);
1115 unsigned long flags;
1116 struct ata_exec_internal_arg arg;
1117
1118 spin_lock_irqsave(&ap->host_set->lock, flags);
1119
1120 qc = ata_qc_new_init(ap, dev);
1121 BUG_ON(qc == NULL);
1122
1123 qc->tf = *tf;
1124 qc->dma_dir = dma_dir;
1125 if (dma_dir != DMA_NONE) {
1126 ata_sg_init_one(qc, buf, buflen);
1127 qc->nsect = buflen / ATA_SECT_SIZE;
1128 }
1129
1130 arg.waiting = &wait;
1131 arg.tf = tf;
1132 qc->private_data = &arg;
1133 qc->complete_fn = ata_qc_complete_internal;
1134
1135 if (ata_qc_issue(qc))
1136 goto issue_fail;
1137
1138 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1139
1140 if (!wait_for_completion_timeout(&wait, ATA_TMOUT_INTERNAL)) {
1141 spin_lock_irqsave(&ap->host_set->lock, flags);
1142
1143 /* We're racing with irq here. If we lose, the
1144 * following test prevents us from completing the qc
1145 * again. If completion irq occurs after here but
1146 * before the caller cleans up, it will result in a
1147 * spurious interrupt. We can live with that.
1148 */
1149 if (arg.waiting) {
1150 qc->err_mask = AC_ERR_OTHER;
1151 ata_qc_complete(qc);
1152 printk(KERN_WARNING "ata%u: qc timeout (cmd 0x%x)\n",
1153 ap->id, command);
1154 }
1155
1156 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1157 }
1158
1159 return arg.err_mask;
1160
1161 issue_fail:
1162 ata_qc_free(qc);
1163 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1164 return AC_ERR_OTHER;
1165}
1166
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167/**
1168 * ata_dev_identify - obtain IDENTIFY x DEVICE page
1169 * @ap: port on which device we wish to probe resides
1170 * @device: device bus address, starting at zero
1171 *
1172 * Following bus reset, we issue the IDENTIFY [PACKET] DEVICE
1173 * command, and read back the 512-byte device information page.
1174 * The device information page is fed to us via the standard
1175 * PIO-IN protocol, but we hand-code it here. (TODO: investigate
1176 * using standard PIO-IN paths)
1177 *
1178 * After reading the device information page, we use several
1179 * bits of information from it to initialize data structures
1180 * that will be used during the lifetime of the ata_device.
1181 * Other data from the info page is used to disqualify certain
1182 * older ATA devices we do not wish to support.
1183 *
1184 * LOCKING:
1185 * Inherited from caller. Some functions called by this function
1186 * obtain the host_set lock.
1187 */
1188
1189static void ata_dev_identify(struct ata_port *ap, unsigned int device)
1190{
1191 struct ata_device *dev = &ap->device[device];
Albert Lee8bf62ec2005-05-12 15:29:42 -04001192 unsigned int major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 u16 tmp;
1194 unsigned long xfer_modes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 unsigned int using_edd;
Tejun Heoa0123702005-12-13 14:49:31 +09001196 struct ata_taskfile tf;
1197 unsigned int err_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 int rc;
1199
1200 if (!ata_dev_present(dev)) {
1201 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
1202 ap->id, device);
1203 return;
1204 }
1205
1206 if (ap->flags & (ATA_FLAG_SRST | ATA_FLAG_SATA_RESET))
1207 using_edd = 0;
1208 else
1209 using_edd = 1;
1210
1211 DPRINTK("ENTER, host %u, dev %u\n", ap->id, device);
1212
1213 assert (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ATAPI ||
1214 dev->class == ATA_DEV_NONE);
1215
1216 ata_dev_select(ap, device, 1, 1); /* select device 0/1 */
1217
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218retry:
Tejun Heoa0123702005-12-13 14:49:31 +09001219 ata_tf_init(ap, &tf, device);
1220
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 if (dev->class == ATA_DEV_ATA) {
Tejun Heoa0123702005-12-13 14:49:31 +09001222 tf.command = ATA_CMD_ID_ATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 DPRINTK("do ATA identify\n");
1224 } else {
Tejun Heoa0123702005-12-13 14:49:31 +09001225 tf.command = ATA_CMD_ID_ATAPI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 DPRINTK("do ATAPI identify\n");
1227 }
1228
Tejun Heoa0123702005-12-13 14:49:31 +09001229 tf.protocol = ATA_PROT_PIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
Tejun Heoa0123702005-12-13 14:49:31 +09001231 err_mask = ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE,
1232 dev->id, sizeof(dev->id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
Tejun Heoa0123702005-12-13 14:49:31 +09001234 if (err_mask) {
1235 if (err_mask & ~AC_ERR_DEV)
1236 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 /*
1239 * arg! EDD works for all test cases, but seems to return
1240 * the ATA signature for some ATAPI devices. Until the
1241 * reason for this is found and fixed, we fix up the mess
1242 * here. If IDENTIFY DEVICE returns command aborted
1243 * (as ATAPI devices do), then we issue an
1244 * IDENTIFY PACKET DEVICE.
1245 *
1246 * ATA software reset (SRST, the default) does not appear
1247 * to have this problem.
1248 */
Albert Lee7c398332005-11-09 13:03:30 +08001249 if ((using_edd) && (dev->class == ATA_DEV_ATA)) {
Tejun Heoa0123702005-12-13 14:49:31 +09001250 u8 err = tf.feature;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 if (err & ATA_ABORTED) {
1252 dev->class = ATA_DEV_ATAPI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 goto retry;
1254 }
1255 }
1256 goto err_out;
1257 }
1258
1259 swap_buf_le16(dev->id, ATA_ID_WORDS);
1260
1261 /* print device capabilities */
1262 printk(KERN_DEBUG "ata%u: dev %u cfg "
1263 "49:%04x 82:%04x 83:%04x 84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1264 ap->id, device, dev->id[49],
1265 dev->id[82], dev->id[83], dev->id[84],
1266 dev->id[85], dev->id[86], dev->id[87],
1267 dev->id[88]);
1268
1269 /*
1270 * common ATA, ATAPI feature tests
1271 */
1272
Albert Lee8bf62ec2005-05-12 15:29:42 -04001273 /* we require DMA support (bits 8 of word 49) */
1274 if (!ata_id_has_dma(dev->id)) {
1275 printk(KERN_DEBUG "ata%u: no dma\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 goto err_out_nosup;
1277 }
1278
1279 /* quick-n-dirty find max transfer mode; for printk only */
1280 xfer_modes = dev->id[ATA_ID_UDMA_MODES];
1281 if (!xfer_modes)
1282 xfer_modes = (dev->id[ATA_ID_MWDMA_MODES]) << ATA_SHIFT_MWDMA;
Alan Cox11e29e22005-10-21 18:46:32 -04001283 if (!xfer_modes)
1284 xfer_modes = ata_pio_modes(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
1286 ata_dump_id(dev);
1287
1288 /* ATA-specific feature tests */
1289 if (dev->class == ATA_DEV_ATA) {
1290 if (!ata_id_is_ata(dev->id)) /* sanity check */
1291 goto err_out_nosup;
1292
Albert Lee8bf62ec2005-05-12 15:29:42 -04001293 /* get major version */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 tmp = dev->id[ATA_ID_MAJOR_VER];
Albert Lee8bf62ec2005-05-12 15:29:42 -04001295 for (major_version = 14; major_version >= 1; major_version--)
1296 if (tmp & (1 << major_version))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 break;
1298
Albert Lee8bf62ec2005-05-12 15:29:42 -04001299 /*
1300 * The exact sequence expected by certain pre-ATA4 drives is:
1301 * SRST RESET
1302 * IDENTIFY
1303 * INITIALIZE DEVICE PARAMETERS
1304 * anything else..
1305 * Some drives were very specific about that exact sequence.
1306 */
Albert Lee59a10b12005-10-12 15:09:42 +08001307 if (major_version < 4 || (!ata_id_has_lba(dev->id))) {
Albert Lee8bf62ec2005-05-12 15:29:42 -04001308 ata_dev_init_params(ap, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
Albert Lee59a10b12005-10-12 15:09:42 +08001310 /* current CHS translation info (id[53-58]) might be
1311 * changed. reread the identify device info.
1312 */
1313 ata_dev_reread_id(ap, dev);
1314 }
1315
Albert Lee8bf62ec2005-05-12 15:29:42 -04001316 if (ata_id_has_lba(dev->id)) {
1317 dev->flags |= ATA_DFLAG_LBA;
1318
1319 if (ata_id_has_lba48(dev->id)) {
1320 dev->flags |= ATA_DFLAG_LBA48;
1321 dev->n_sectors = ata_id_u64(dev->id, 100);
1322 } else {
1323 dev->n_sectors = ata_id_u32(dev->id, 60);
1324 }
1325
1326 /* print device info to dmesg */
1327 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors:%s\n",
1328 ap->id, device,
1329 major_version,
1330 ata_mode_string(xfer_modes),
1331 (unsigned long long)dev->n_sectors,
1332 dev->flags & ATA_DFLAG_LBA48 ? " LBA48" : " LBA");
1333 } else {
1334 /* CHS */
1335
1336 /* Default translation */
1337 dev->cylinders = dev->id[1];
1338 dev->heads = dev->id[3];
1339 dev->sectors = dev->id[6];
1340 dev->n_sectors = dev->cylinders * dev->heads * dev->sectors;
1341
1342 if (ata_id_current_chs_valid(dev->id)) {
1343 /* Current CHS translation is valid. */
1344 dev->cylinders = dev->id[54];
1345 dev->heads = dev->id[55];
1346 dev->sectors = dev->id[56];
1347
1348 dev->n_sectors = ata_id_u32(dev->id, 57);
1349 }
1350
1351 /* print device info to dmesg */
1352 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors: CHS %d/%d/%d\n",
1353 ap->id, device,
1354 major_version,
1355 ata_mode_string(xfer_modes),
1356 (unsigned long long)dev->n_sectors,
1357 (int)dev->cylinders, (int)dev->heads, (int)dev->sectors);
1358
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 }
1360
1361 ap->host->max_cmd_len = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 }
1363
1364 /* ATAPI-specific feature tests */
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05001365 else if (dev->class == ATA_DEV_ATAPI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 if (ata_id_is_ata(dev->id)) /* sanity check */
1367 goto err_out_nosup;
1368
1369 rc = atapi_cdb_len(dev->id);
1370 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
1371 printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
1372 goto err_out_nosup;
1373 }
1374 ap->cdb_len = (unsigned int) rc;
1375 ap->host->max_cmd_len = (unsigned char) ap->cdb_len;
1376
1377 /* print device info to dmesg */
1378 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
1379 ap->id, device,
1380 ata_mode_string(xfer_modes));
1381 }
1382
1383 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
1384 return;
1385
1386err_out_nosup:
1387 printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n",
1388 ap->id, device);
1389err_out:
1390 dev->class++; /* converts ATA_DEV_xxx into ATA_DEV_xxx_UNSUP */
1391 DPRINTK("EXIT, err\n");
1392}
1393
Brad Campbell6f2f3812005-05-12 15:07:47 -04001394
Jeff Garzik057ace52005-10-22 14:27:05 -04001395static inline u8 ata_dev_knobble(const struct ata_port *ap)
Brad Campbell6f2f3812005-05-12 15:07:47 -04001396{
1397 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(ap->device->id)));
1398}
1399
1400/**
1401 * ata_dev_config - Run device specific handlers and check for
1402 * SATA->PATA bridges
Jeff Garzik8a60a072005-07-31 13:13:24 -04001403 * @ap: Bus
Brad Campbell6f2f3812005-05-12 15:07:47 -04001404 * @i: Device
1405 *
1406 * LOCKING:
1407 */
Jeff Garzik8a60a072005-07-31 13:13:24 -04001408
Brad Campbell6f2f3812005-05-12 15:07:47 -04001409void ata_dev_config(struct ata_port *ap, unsigned int i)
1410{
1411 /* limit bridge transfers to udma5, 200 sectors */
1412 if (ata_dev_knobble(ap)) {
1413 printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
1414 ap->id, ap->device->devno);
1415 ap->udma_mask &= ATA_UDMA5;
1416 ap->host->max_sectors = ATA_MAX_SECTORS;
1417 ap->host->hostt->max_sectors = ATA_MAX_SECTORS;
1418 ap->device->flags |= ATA_DFLAG_LOCK_SECTORS;
1419 }
1420
1421 if (ap->ops->dev_config)
1422 ap->ops->dev_config(ap, &ap->device[i]);
1423}
1424
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425/**
1426 * ata_bus_probe - Reset and probe ATA bus
1427 * @ap: Bus to probe
1428 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001429 * Master ATA bus probing function. Initiates a hardware-dependent
1430 * bus reset, then attempts to identify any devices found on
1431 * the bus.
1432 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001434 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 *
1436 * RETURNS:
1437 * Zero on success, non-zero on error.
1438 */
1439
1440static int ata_bus_probe(struct ata_port *ap)
1441{
1442 unsigned int i, found = 0;
1443
1444 ap->ops->phy_reset(ap);
1445 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1446 goto err_out;
1447
1448 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1449 ata_dev_identify(ap, i);
1450 if (ata_dev_present(&ap->device[i])) {
1451 found = 1;
Brad Campbell6f2f3812005-05-12 15:07:47 -04001452 ata_dev_config(ap,i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 }
1454 }
1455
1456 if ((!found) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1457 goto err_out_disable;
1458
1459 ata_set_mode(ap);
1460 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1461 goto err_out_disable;
1462
1463 return 0;
1464
1465err_out_disable:
1466 ap->ops->port_disable(ap);
1467err_out:
1468 return -1;
1469}
1470
1471/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001472 * ata_port_probe - Mark port as enabled
1473 * @ap: Port for which we indicate enablement
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001475 * Modify @ap data structure such that the system
1476 * thinks that the entire port is enabled.
1477 *
1478 * LOCKING: host_set lock, or some other form of
1479 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 */
1481
1482void ata_port_probe(struct ata_port *ap)
1483{
1484 ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1485}
1486
1487/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001488 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1489 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001491 * This function issues commands to standard SATA Sxxx
1492 * PHY registers, to wake up the phy (and device), and
1493 * clear any reset condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 *
1495 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001496 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 *
1498 */
1499void __sata_phy_reset(struct ata_port *ap)
1500{
1501 u32 sstatus;
1502 unsigned long timeout = jiffies + (HZ * 5);
1503
1504 if (ap->flags & ATA_FLAG_SATA_RESET) {
Brett Russcdcca892005-03-28 15:10:27 -05001505 /* issue phy wake/reset */
1506 scr_write_flush(ap, SCR_CONTROL, 0x301);
Tejun Heo62ba2842005-06-26 23:27:19 +09001507 /* Couldn't find anything in SATA I/II specs, but
1508 * AHCI-1.1 10.4.2 says at least 1 ms. */
1509 mdelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 }
Brett Russcdcca892005-03-28 15:10:27 -05001511 scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512
1513 /* wait for phy to become ready, if necessary */
1514 do {
1515 msleep(200);
1516 sstatus = scr_read(ap, SCR_STATUS);
1517 if ((sstatus & 0xf) != 1)
1518 break;
1519 } while (time_before(jiffies, timeout));
1520
1521 /* TODO: phy layer with polling, timeouts, etc. */
Jeff Garzik656563e2005-11-20 03:36:45 -05001522 sstatus = scr_read(ap, SCR_STATUS);
1523 if (sata_dev_present(ap)) {
1524 const char *speed;
1525 u32 tmp;
1526
1527 tmp = (sstatus >> 4) & 0xf;
1528 if (tmp & (1 << 0))
1529 speed = "1.5";
1530 else if (tmp & (1 << 1))
1531 speed = "3.0";
1532 else
1533 speed = "<unknown>";
1534 printk(KERN_INFO "ata%u: SATA link up %s Gbps (SStatus %X)\n",
1535 ap->id, speed, sstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 ata_port_probe(ap);
Jeff Garzik656563e2005-11-20 03:36:45 -05001537 } else {
1538 printk(KERN_INFO "ata%u: SATA link down (SStatus %X)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 ap->id, sstatus);
1540 ata_port_disable(ap);
1541 }
1542
1543 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1544 return;
1545
1546 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1547 ata_port_disable(ap);
1548 return;
1549 }
1550
1551 ap->cbl = ATA_CBL_SATA;
1552}
1553
1554/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001555 * sata_phy_reset - Reset SATA bus.
1556 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001558 * This function resets the SATA bus, and then probes
1559 * the bus for devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560 *
1561 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001562 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 *
1564 */
1565void sata_phy_reset(struct ata_port *ap)
1566{
1567 __sata_phy_reset(ap);
1568 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1569 return;
1570 ata_bus_reset(ap);
1571}
1572
1573/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001574 * ata_port_disable - Disable port.
1575 * @ap: Port to be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001577 * Modify @ap data structure such that the system
1578 * thinks that the entire port is disabled, and should
1579 * never attempt to probe or communicate with devices
1580 * on this port.
1581 *
1582 * LOCKING: host_set lock, or some other form of
1583 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 */
1585
1586void ata_port_disable(struct ata_port *ap)
1587{
1588 ap->device[0].class = ATA_DEV_NONE;
1589 ap->device[1].class = ATA_DEV_NONE;
1590 ap->flags |= ATA_FLAG_PORT_DISABLED;
1591}
1592
Alan Cox452503f2005-10-21 19:01:32 -04001593/*
1594 * This mode timing computation functionality is ported over from
1595 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
1596 */
1597/*
1598 * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
1599 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
1600 * for PIO 5, which is a nonstandard extension and UDMA6, which
1601 * is currently supported only by Maxtor drives.
1602 */
1603
1604static const struct ata_timing ata_timing[] = {
1605
1606 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
1607 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
1608 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
1609 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
1610
1611 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
1612 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
1613 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
1614
1615/* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
1616
1617 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
1618 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
1619 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
1620
1621 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
1622 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
1623 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
1624
1625/* { XFER_PIO_5, 20, 50, 30, 100, 50, 30, 100, 0 }, */
1626 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
1627 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
1628
1629 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
1630 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
1631 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
1632
1633/* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
1634
1635 { 0xFF }
1636};
1637
1638#define ENOUGH(v,unit) (((v)-1)/(unit)+1)
1639#define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
1640
1641static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
1642{
1643 q->setup = EZ(t->setup * 1000, T);
1644 q->act8b = EZ(t->act8b * 1000, T);
1645 q->rec8b = EZ(t->rec8b * 1000, T);
1646 q->cyc8b = EZ(t->cyc8b * 1000, T);
1647 q->active = EZ(t->active * 1000, T);
1648 q->recover = EZ(t->recover * 1000, T);
1649 q->cycle = EZ(t->cycle * 1000, T);
1650 q->udma = EZ(t->udma * 1000, UT);
1651}
1652
1653void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
1654 struct ata_timing *m, unsigned int what)
1655{
1656 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
1657 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
1658 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
1659 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
1660 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
1661 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
1662 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
1663 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
1664}
1665
1666static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
1667{
1668 const struct ata_timing *t;
1669
1670 for (t = ata_timing; t->mode != speed; t++)
Alan Cox91190752005-10-26 12:17:46 -04001671 if (t->mode == 0xFF)
Alan Cox452503f2005-10-21 19:01:32 -04001672 return NULL;
1673 return t;
1674}
1675
1676int ata_timing_compute(struct ata_device *adev, unsigned short speed,
1677 struct ata_timing *t, int T, int UT)
1678{
1679 const struct ata_timing *s;
1680 struct ata_timing p;
1681
1682 /*
1683 * Find the mode.
Albert Lee75b1f2f2005-11-16 17:06:18 +08001684 */
Alan Cox452503f2005-10-21 19:01:32 -04001685
1686 if (!(s = ata_timing_find_mode(speed)))
1687 return -EINVAL;
1688
Albert Lee75b1f2f2005-11-16 17:06:18 +08001689 memcpy(t, s, sizeof(*s));
1690
Alan Cox452503f2005-10-21 19:01:32 -04001691 /*
1692 * If the drive is an EIDE drive, it can tell us it needs extended
1693 * PIO/MW_DMA cycle timing.
1694 */
1695
1696 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
1697 memset(&p, 0, sizeof(p));
1698 if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
1699 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
1700 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
1701 } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
1702 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
1703 }
1704 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
1705 }
1706
1707 /*
1708 * Convert the timing to bus clock counts.
1709 */
1710
Albert Lee75b1f2f2005-11-16 17:06:18 +08001711 ata_timing_quantize(t, t, T, UT);
Alan Cox452503f2005-10-21 19:01:32 -04001712
1713 /*
1714 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY, S.M.A.R.T
1715 * and some other commands. We have to ensure that the DMA cycle timing is
1716 * slower/equal than the fastest PIO timing.
1717 */
1718
1719 if (speed > XFER_PIO_4) {
1720 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
1721 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
1722 }
1723
1724 /*
1725 * Lenghten active & recovery time so that cycle time is correct.
1726 */
1727
1728 if (t->act8b + t->rec8b < t->cyc8b) {
1729 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
1730 t->rec8b = t->cyc8b - t->act8b;
1731 }
1732
1733 if (t->active + t->recover < t->cycle) {
1734 t->active += (t->cycle - (t->active + t->recover)) / 2;
1735 t->recover = t->cycle - t->active;
1736 }
1737
1738 return 0;
1739}
1740
Jeff Garzik057ace52005-10-22 14:27:05 -04001741static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 unsigned int shift;
1743 u8 base;
1744} xfer_mode_classes[] = {
1745 { ATA_SHIFT_UDMA, XFER_UDMA_0 },
1746 { ATA_SHIFT_MWDMA, XFER_MW_DMA_0 },
1747 { ATA_SHIFT_PIO, XFER_PIO_0 },
1748};
1749
1750static inline u8 base_from_shift(unsigned int shift)
1751{
1752 int i;
1753
1754 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++)
1755 if (xfer_mode_classes[i].shift == shift)
1756 return xfer_mode_classes[i].base;
1757
1758 return 0xff;
1759}
1760
1761static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
1762{
1763 int ofs, idx;
1764 u8 base;
1765
1766 if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1767 return;
1768
1769 if (dev->xfer_shift == ATA_SHIFT_PIO)
1770 dev->flags |= ATA_DFLAG_PIO;
1771
1772 ata_dev_set_xfermode(ap, dev);
1773
1774 base = base_from_shift(dev->xfer_shift);
1775 ofs = dev->xfer_mode - base;
1776 idx = ofs + dev->xfer_shift;
1777 WARN_ON(idx >= ARRAY_SIZE(xfer_mode_str));
1778
1779 DPRINTK("idx=%d xfer_shift=%u, xfer_mode=0x%x, base=0x%x, offset=%d\n",
1780 idx, dev->xfer_shift, (int)dev->xfer_mode, (int)base, ofs);
1781
1782 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
1783 ap->id, dev->devno, xfer_mode_str[idx]);
1784}
1785
1786static int ata_host_set_pio(struct ata_port *ap)
1787{
1788 unsigned int mask;
1789 int x, i;
1790 u8 base, xfer_mode;
1791
1792 mask = ata_get_mode_mask(ap, ATA_SHIFT_PIO);
1793 x = fgb(mask);
1794 if (x < 0) {
1795 printk(KERN_WARNING "ata%u: no PIO support\n", ap->id);
1796 return -1;
1797 }
1798
1799 base = base_from_shift(ATA_SHIFT_PIO);
1800 xfer_mode = base + x;
1801
1802 DPRINTK("base 0x%x xfer_mode 0x%x mask 0x%x x %d\n",
1803 (int)base, (int)xfer_mode, mask, x);
1804
1805 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1806 struct ata_device *dev = &ap->device[i];
1807 if (ata_dev_present(dev)) {
1808 dev->pio_mode = xfer_mode;
1809 dev->xfer_mode = xfer_mode;
1810 dev->xfer_shift = ATA_SHIFT_PIO;
1811 if (ap->ops->set_piomode)
1812 ap->ops->set_piomode(ap, dev);
1813 }
1814 }
1815
1816 return 0;
1817}
1818
1819static void ata_host_set_dma(struct ata_port *ap, u8 xfer_mode,
1820 unsigned int xfer_shift)
1821{
1822 int i;
1823
1824 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1825 struct ata_device *dev = &ap->device[i];
1826 if (ata_dev_present(dev)) {
1827 dev->dma_mode = xfer_mode;
1828 dev->xfer_mode = xfer_mode;
1829 dev->xfer_shift = xfer_shift;
1830 if (ap->ops->set_dmamode)
1831 ap->ops->set_dmamode(ap, dev);
1832 }
1833 }
1834}
1835
1836/**
1837 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1838 * @ap: port on which timings will be programmed
1839 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001840 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1841 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001843 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 *
1845 */
1846static void ata_set_mode(struct ata_port *ap)
1847{
Albert Lee8cbd6df2005-10-12 15:06:27 +08001848 unsigned int xfer_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 u8 xfer_mode;
1850 int rc;
1851
1852 /* step 1: always set host PIO timings */
1853 rc = ata_host_set_pio(ap);
1854 if (rc)
1855 goto err_out;
1856
1857 /* step 2: choose the best data xfer mode */
1858 xfer_mode = xfer_shift = 0;
1859 rc = ata_choose_xfer_mode(ap, &xfer_mode, &xfer_shift);
1860 if (rc)
1861 goto err_out;
1862
1863 /* step 3: if that xfer mode isn't PIO, set host DMA timings */
1864 if (xfer_shift != ATA_SHIFT_PIO)
1865 ata_host_set_dma(ap, xfer_mode, xfer_shift);
1866
1867 /* step 4: update devices' xfer mode */
1868 ata_dev_set_mode(ap, &ap->device[0]);
1869 ata_dev_set_mode(ap, &ap->device[1]);
1870
1871 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1872 return;
1873
1874 if (ap->ops->post_set_mode)
1875 ap->ops->post_set_mode(ap);
1876
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 return;
1878
1879err_out:
1880 ata_port_disable(ap);
1881}
1882
1883/**
1884 * ata_busy_sleep - sleep until BSY clears, or timeout
1885 * @ap: port containing status register to be polled
1886 * @tmout_pat: impatience timeout
1887 * @tmout: overall timeout
1888 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001889 * Sleep until ATA Status register bit BSY clears,
1890 * or a timeout occurs.
1891 *
1892 * LOCKING: None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 *
1894 */
1895
1896static unsigned int ata_busy_sleep (struct ata_port *ap,
1897 unsigned long tmout_pat,
1898 unsigned long tmout)
1899{
1900 unsigned long timer_start, timeout;
1901 u8 status;
1902
1903 status = ata_busy_wait(ap, ATA_BUSY, 300);
1904 timer_start = jiffies;
1905 timeout = timer_start + tmout_pat;
1906 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1907 msleep(50);
1908 status = ata_busy_wait(ap, ATA_BUSY, 3);
1909 }
1910
1911 if (status & ATA_BUSY)
1912 printk(KERN_WARNING "ata%u is slow to respond, "
1913 "please be patient\n", ap->id);
1914
1915 timeout = timer_start + tmout;
1916 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1917 msleep(50);
1918 status = ata_chk_status(ap);
1919 }
1920
1921 if (status & ATA_BUSY) {
1922 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
1923 ap->id, tmout / HZ);
1924 return 1;
1925 }
1926
1927 return 0;
1928}
1929
1930static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
1931{
1932 struct ata_ioports *ioaddr = &ap->ioaddr;
1933 unsigned int dev0 = devmask & (1 << 0);
1934 unsigned int dev1 = devmask & (1 << 1);
1935 unsigned long timeout;
1936
1937 /* if device 0 was found in ata_devchk, wait for its
1938 * BSY bit to clear
1939 */
1940 if (dev0)
1941 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1942
1943 /* if device 1 was found in ata_devchk, wait for
1944 * register access, then wait for BSY to clear
1945 */
1946 timeout = jiffies + ATA_TMOUT_BOOT;
1947 while (dev1) {
1948 u8 nsect, lbal;
1949
1950 ap->ops->dev_select(ap, 1);
1951 if (ap->flags & ATA_FLAG_MMIO) {
1952 nsect = readb((void __iomem *) ioaddr->nsect_addr);
1953 lbal = readb((void __iomem *) ioaddr->lbal_addr);
1954 } else {
1955 nsect = inb(ioaddr->nsect_addr);
1956 lbal = inb(ioaddr->lbal_addr);
1957 }
1958 if ((nsect == 1) && (lbal == 1))
1959 break;
1960 if (time_after(jiffies, timeout)) {
1961 dev1 = 0;
1962 break;
1963 }
1964 msleep(50); /* give drive a breather */
1965 }
1966 if (dev1)
1967 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1968
1969 /* is all this really necessary? */
1970 ap->ops->dev_select(ap, 0);
1971 if (dev1)
1972 ap->ops->dev_select(ap, 1);
1973 if (dev0)
1974 ap->ops->dev_select(ap, 0);
1975}
1976
1977/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001978 * ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
1979 * @ap: Port to reset and probe
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001981 * Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
1982 * probe the bus. Not often used these days.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 *
1984 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001985 * PCI/etc. bus probe sem.
Jeff Garzike5338252005-10-30 21:37:17 -05001986 * Obtains host_set lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 *
1988 */
1989
1990static unsigned int ata_bus_edd(struct ata_port *ap)
1991{
1992 struct ata_taskfile tf;
Jeff Garzike5338252005-10-30 21:37:17 -05001993 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994
1995 /* set up execute-device-diag (bus reset) taskfile */
1996 /* also, take interrupts to a known state (disabled) */
1997 DPRINTK("execute-device-diag\n");
1998 ata_tf_init(ap, &tf, 0);
1999 tf.ctl |= ATA_NIEN;
2000 tf.command = ATA_CMD_EDD;
2001 tf.protocol = ATA_PROT_NODATA;
2002
2003 /* do bus reset */
Jeff Garzike5338252005-10-30 21:37:17 -05002004 spin_lock_irqsave(&ap->host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005 ata_tf_to_host(ap, &tf);
Jeff Garzike5338252005-10-30 21:37:17 -05002006 spin_unlock_irqrestore(&ap->host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007
2008 /* spec says at least 2ms. but who knows with those
2009 * crazy ATAPI devices...
2010 */
2011 msleep(150);
2012
2013 return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2014}
2015
2016static unsigned int ata_bus_softreset(struct ata_port *ap,
2017 unsigned int devmask)
2018{
2019 struct ata_ioports *ioaddr = &ap->ioaddr;
2020
2021 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
2022
2023 /* software reset. causes dev0 to be selected */
2024 if (ap->flags & ATA_FLAG_MMIO) {
2025 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2026 udelay(20); /* FIXME: flush */
2027 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
2028 udelay(20); /* FIXME: flush */
2029 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2030 } else {
2031 outb(ap->ctl, ioaddr->ctl_addr);
2032 udelay(10);
2033 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
2034 udelay(10);
2035 outb(ap->ctl, ioaddr->ctl_addr);
2036 }
2037
2038 /* spec mandates ">= 2ms" before checking status.
2039 * We wait 150ms, because that was the magic delay used for
2040 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
2041 * between when the ATA command register is written, and then
2042 * status is checked. Because waiting for "a while" before
2043 * checking status is fine, post SRST, we perform this magic
2044 * delay here as well.
2045 */
2046 msleep(150);
2047
2048 ata_bus_post_reset(ap, devmask);
2049
2050 return 0;
2051}
2052
2053/**
2054 * ata_bus_reset - reset host port and associated ATA channel
2055 * @ap: port to reset
2056 *
2057 * This is typically the first time we actually start issuing
2058 * commands to the ATA channel. We wait for BSY to clear, then
2059 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
2060 * result. Determine what devices, if any, are on the channel
2061 * by looking at the device 0/1 error register. Look at the signature
2062 * stored in each device's taskfile registers, to determine if
2063 * the device is ATA or ATAPI.
2064 *
2065 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002066 * PCI/etc. bus probe sem.
2067 * Obtains host_set lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 *
2069 * SIDE EFFECTS:
2070 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
2071 */
2072
2073void ata_bus_reset(struct ata_port *ap)
2074{
2075 struct ata_ioports *ioaddr = &ap->ioaddr;
2076 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2077 u8 err;
2078 unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
2079
2080 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
2081
2082 /* determine if device 0/1 are present */
2083 if (ap->flags & ATA_FLAG_SATA_RESET)
2084 dev0 = 1;
2085 else {
2086 dev0 = ata_devchk(ap, 0);
2087 if (slave_possible)
2088 dev1 = ata_devchk(ap, 1);
2089 }
2090
2091 if (dev0)
2092 devmask |= (1 << 0);
2093 if (dev1)
2094 devmask |= (1 << 1);
2095
2096 /* select device 0 again */
2097 ap->ops->dev_select(ap, 0);
2098
2099 /* issue bus reset */
2100 if (ap->flags & ATA_FLAG_SRST)
2101 rc = ata_bus_softreset(ap, devmask);
2102 else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
2103 /* set up device control */
2104 if (ap->flags & ATA_FLAG_MMIO)
2105 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2106 else
2107 outb(ap->ctl, ioaddr->ctl_addr);
2108 rc = ata_bus_edd(ap);
2109 }
2110
2111 if (rc)
2112 goto err_out;
2113
2114 /*
2115 * determine by signature whether we have ATA or ATAPI devices
2116 */
2117 err = ata_dev_try_classify(ap, 0);
2118 if ((slave_possible) && (err != 0x81))
2119 ata_dev_try_classify(ap, 1);
2120
2121 /* re-enable interrupts */
2122 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2123 ata_irq_on(ap);
2124
2125 /* is double-select really necessary? */
2126 if (ap->device[1].class != ATA_DEV_NONE)
2127 ap->ops->dev_select(ap, 1);
2128 if (ap->device[0].class != ATA_DEV_NONE)
2129 ap->ops->dev_select(ap, 0);
2130
2131 /* if no devices were detected, disable this port */
2132 if ((ap->device[0].class == ATA_DEV_NONE) &&
2133 (ap->device[1].class == ATA_DEV_NONE))
2134 goto err_out;
2135
2136 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
2137 /* set up device control for ATA_FLAG_SATA_RESET */
2138 if (ap->flags & ATA_FLAG_MMIO)
2139 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2140 else
2141 outb(ap->ctl, ioaddr->ctl_addr);
2142 }
2143
2144 DPRINTK("EXIT\n");
2145 return;
2146
2147err_out:
2148 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
2149 ap->ops->port_disable(ap);
2150
2151 DPRINTK("EXIT\n");
2152}
2153
Jeff Garzik057ace52005-10-22 14:27:05 -04002154static void ata_pr_blacklisted(const struct ata_port *ap,
2155 const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156{
2157 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, disabling DMA\n",
2158 ap->id, dev->devno);
2159}
2160
Arjan van de Ven98ac62d2005-11-28 10:06:23 +01002161static const char * const ata_dma_blacklist [] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 "WDC AC11000H",
2163 "WDC AC22100H",
2164 "WDC AC32500H",
2165 "WDC AC33100H",
2166 "WDC AC31600H",
2167 "WDC AC32100H",
2168 "WDC AC23200L",
2169 "Compaq CRD-8241B",
2170 "CRD-8400B",
2171 "CRD-8480B",
2172 "CRD-8482B",
2173 "CRD-84",
2174 "SanDisk SDP3B",
2175 "SanDisk SDP3B-64",
2176 "SANYO CD-ROM CRD",
2177 "HITACHI CDR-8",
2178 "HITACHI CDR-8335",
2179 "HITACHI CDR-8435",
2180 "Toshiba CD-ROM XM-6202B",
Jeff Garzike9222562005-06-28 00:03:37 -04002181 "TOSHIBA CD-ROM XM-1702BC",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 "CD-532E-A",
2183 "E-IDE CD-ROM CR-840",
2184 "CD-ROM Drive/F5A",
2185 "WPI CDD-820",
2186 "SAMSUNG CD-ROM SC-148C",
2187 "SAMSUNG CD-ROM SC",
2188 "SanDisk SDP3B-64",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 "ATAPI CD-ROM DRIVE 40X MAXIMUM",
2190 "_NEC DV5800A",
2191};
2192
Jeff Garzik057ace52005-10-22 14:27:05 -04002193static int ata_dma_blacklisted(const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194{
2195 unsigned char model_num[40];
2196 char *s;
2197 unsigned int len;
2198 int i;
2199
2200 ata_dev_id_string(dev->id, model_num, ATA_ID_PROD_OFS,
2201 sizeof(model_num));
2202 s = &model_num[0];
2203 len = strnlen(s, sizeof(model_num));
2204
2205 /* ATAPI specifies that empty space is blank-filled; remove blanks */
2206 while ((len > 0) && (s[len - 1] == ' ')) {
2207 len--;
2208 s[len] = 0;
2209 }
2210
2211 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i++)
2212 if (!strncmp(ata_dma_blacklist[i], s, len))
2213 return 1;
2214
2215 return 0;
2216}
2217
Jeff Garzik057ace52005-10-22 14:27:05 -04002218static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219{
Jeff Garzik057ace52005-10-22 14:27:05 -04002220 const struct ata_device *master, *slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 unsigned int mask;
2222
2223 master = &ap->device[0];
2224 slave = &ap->device[1];
2225
2226 assert (ata_dev_present(master) || ata_dev_present(slave));
2227
2228 if (shift == ATA_SHIFT_UDMA) {
2229 mask = ap->udma_mask;
2230 if (ata_dev_present(master)) {
2231 mask &= (master->id[ATA_ID_UDMA_MODES] & 0xff);
Jeff Garzik057ace52005-10-22 14:27:05 -04002232 if (ata_dma_blacklisted(master)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 mask = 0;
2234 ata_pr_blacklisted(ap, master);
2235 }
2236 }
2237 if (ata_dev_present(slave)) {
2238 mask &= (slave->id[ATA_ID_UDMA_MODES] & 0xff);
Jeff Garzik057ace52005-10-22 14:27:05 -04002239 if (ata_dma_blacklisted(slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 mask = 0;
2241 ata_pr_blacklisted(ap, slave);
2242 }
2243 }
2244 }
2245 else if (shift == ATA_SHIFT_MWDMA) {
2246 mask = ap->mwdma_mask;
2247 if (ata_dev_present(master)) {
2248 mask &= (master->id[ATA_ID_MWDMA_MODES] & 0x07);
Jeff Garzik057ace52005-10-22 14:27:05 -04002249 if (ata_dma_blacklisted(master)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 mask = 0;
2251 ata_pr_blacklisted(ap, master);
2252 }
2253 }
2254 if (ata_dev_present(slave)) {
2255 mask &= (slave->id[ATA_ID_MWDMA_MODES] & 0x07);
Jeff Garzik057ace52005-10-22 14:27:05 -04002256 if (ata_dma_blacklisted(slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 mask = 0;
2258 ata_pr_blacklisted(ap, slave);
2259 }
2260 }
2261 }
2262 else if (shift == ATA_SHIFT_PIO) {
2263 mask = ap->pio_mask;
2264 if (ata_dev_present(master)) {
2265 /* spec doesn't return explicit support for
2266 * PIO0-2, so we fake it
2267 */
2268 u16 tmp_mode = master->id[ATA_ID_PIO_MODES] & 0x03;
2269 tmp_mode <<= 3;
2270 tmp_mode |= 0x7;
2271 mask &= tmp_mode;
2272 }
2273 if (ata_dev_present(slave)) {
2274 /* spec doesn't return explicit support for
2275 * PIO0-2, so we fake it
2276 */
2277 u16 tmp_mode = slave->id[ATA_ID_PIO_MODES] & 0x03;
2278 tmp_mode <<= 3;
2279 tmp_mode |= 0x7;
2280 mask &= tmp_mode;
2281 }
2282 }
2283 else {
2284 mask = 0xffffffff; /* shut up compiler warning */
2285 BUG();
2286 }
2287
2288 return mask;
2289}
2290
2291/* find greatest bit */
2292static int fgb(u32 bitmap)
2293{
2294 unsigned int i;
2295 int x = -1;
2296
2297 for (i = 0; i < 32; i++)
2298 if (bitmap & (1 << i))
2299 x = i;
2300
2301 return x;
2302}
2303
2304/**
2305 * ata_choose_xfer_mode - attempt to find best transfer mode
2306 * @ap: Port for which an xfer mode will be selected
2307 * @xfer_mode_out: (output) SET FEATURES - XFER MODE code
2308 * @xfer_shift_out: (output) bit shift that selects this mode
2309 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04002310 * Based on host and device capabilities, determine the
2311 * maximum transfer mode that is amenable to all.
2312 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002314 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 *
2316 * RETURNS:
2317 * Zero on success, negative on error.
2318 */
2319
Jeff Garzik057ace52005-10-22 14:27:05 -04002320static int ata_choose_xfer_mode(const struct ata_port *ap,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 u8 *xfer_mode_out,
2322 unsigned int *xfer_shift_out)
2323{
2324 unsigned int mask, shift;
2325 int x, i;
2326
2327 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++) {
2328 shift = xfer_mode_classes[i].shift;
2329 mask = ata_get_mode_mask(ap, shift);
2330
2331 x = fgb(mask);
2332 if (x >= 0) {
2333 *xfer_mode_out = xfer_mode_classes[i].base + x;
2334 *xfer_shift_out = shift;
2335 return 0;
2336 }
2337 }
2338
2339 return -1;
2340}
2341
2342/**
2343 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2344 * @ap: Port associated with device @dev
2345 * @dev: Device to which command will be sent
2346 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002347 * Issue SET FEATURES - XFER MODE command to device @dev
2348 * on port @ap.
2349 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002351 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 */
2353
2354static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
2355{
Tejun Heoa0123702005-12-13 14:49:31 +09002356 struct ata_taskfile tf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357
2358 /* set up set-features taskfile */
2359 DPRINTK("set features - xfer mode\n");
2360
Tejun Heoa0123702005-12-13 14:49:31 +09002361 ata_tf_init(ap, &tf, dev->devno);
2362 tf.command = ATA_CMD_SET_FEATURES;
2363 tf.feature = SETFEATURES_XFER;
2364 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2365 tf.protocol = ATA_PROT_NODATA;
2366 tf.nsect = dev->xfer_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367
Tejun Heoa0123702005-12-13 14:49:31 +09002368 if (ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0)) {
2369 printk(KERN_ERR "ata%u: failed to set xfermode, disabled\n",
2370 ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 ata_port_disable(ap);
Tejun Heoa0123702005-12-13 14:49:31 +09002372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373
2374 DPRINTK("EXIT\n");
2375}
2376
2377/**
Albert Lee59a10b12005-10-12 15:09:42 +08002378 * ata_dev_reread_id - Reread the device identify device info
2379 * @ap: port where the device is
2380 * @dev: device to reread the identify device info
2381 *
2382 * LOCKING:
2383 */
2384
2385static void ata_dev_reread_id(struct ata_port *ap, struct ata_device *dev)
2386{
Tejun Heoa0123702005-12-13 14:49:31 +09002387 struct ata_taskfile tf;
Albert Lee59a10b12005-10-12 15:09:42 +08002388
Tejun Heoa0123702005-12-13 14:49:31 +09002389 ata_tf_init(ap, &tf, dev->devno);
Albert Lee59a10b12005-10-12 15:09:42 +08002390
2391 if (dev->class == ATA_DEV_ATA) {
Tejun Heoa0123702005-12-13 14:49:31 +09002392 tf.command = ATA_CMD_ID_ATA;
Albert Lee59a10b12005-10-12 15:09:42 +08002393 DPRINTK("do ATA identify\n");
2394 } else {
Tejun Heoa0123702005-12-13 14:49:31 +09002395 tf.command = ATA_CMD_ID_ATAPI;
Albert Lee59a10b12005-10-12 15:09:42 +08002396 DPRINTK("do ATAPI identify\n");
2397 }
2398
Tejun Heoa0123702005-12-13 14:49:31 +09002399 tf.flags |= ATA_TFLAG_DEVICE;
2400 tf.protocol = ATA_PROT_PIO;
Albert Lee59a10b12005-10-12 15:09:42 +08002401
Tejun Heoa0123702005-12-13 14:49:31 +09002402 if (ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE,
2403 dev->id, sizeof(dev->id)))
Albert Lee59a10b12005-10-12 15:09:42 +08002404 goto err_out;
2405
Albert Lee59a10b12005-10-12 15:09:42 +08002406 swap_buf_le16(dev->id, ATA_ID_WORDS);
2407
2408 ata_dump_id(dev);
2409
2410 DPRINTK("EXIT\n");
2411
2412 return;
2413err_out:
Tejun Heoa0123702005-12-13 14:49:31 +09002414 printk(KERN_ERR "ata%u: failed to reread ID, disabled\n", ap->id);
Albert Lee59a10b12005-10-12 15:09:42 +08002415 ata_port_disable(ap);
2416}
2417
2418/**
Albert Lee8bf62ec2005-05-12 15:29:42 -04002419 * ata_dev_init_params - Issue INIT DEV PARAMS command
2420 * @ap: Port associated with device @dev
2421 * @dev: Device to which command will be sent
2422 *
2423 * LOCKING:
2424 */
2425
2426static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev)
2427{
Tejun Heoa0123702005-12-13 14:49:31 +09002428 struct ata_taskfile tf;
Albert Lee8bf62ec2005-05-12 15:29:42 -04002429 u16 sectors = dev->id[6];
2430 u16 heads = dev->id[3];
2431
2432 /* Number of sectors per track 1-255. Number of heads 1-16 */
2433 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
2434 return;
2435
2436 /* set up init dev params taskfile */
2437 DPRINTK("init dev params \n");
2438
Tejun Heoa0123702005-12-13 14:49:31 +09002439 ata_tf_init(ap, &tf, dev->devno);
2440 tf.command = ATA_CMD_INIT_DEV_PARAMS;
2441 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2442 tf.protocol = ATA_PROT_NODATA;
2443 tf.nsect = sectors;
2444 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
Albert Lee8bf62ec2005-05-12 15:29:42 -04002445
Tejun Heoa0123702005-12-13 14:49:31 +09002446 if (ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0)) {
2447 printk(KERN_ERR "ata%u: failed to init parameters, disabled\n",
2448 ap->id);
Albert Lee8bf62ec2005-05-12 15:29:42 -04002449 ata_port_disable(ap);
Tejun Heoa0123702005-12-13 14:49:31 +09002450 }
Albert Lee8bf62ec2005-05-12 15:29:42 -04002451
2452 DPRINTK("EXIT\n");
2453}
2454
2455/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002456 * ata_sg_clean - Unmap DMA memory associated with command
2457 * @qc: Command containing DMA memory to be released
2458 *
2459 * Unmap all mapped DMA memory associated with this command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460 *
2461 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002462 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463 */
2464
2465static void ata_sg_clean(struct ata_queued_cmd *qc)
2466{
2467 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002468 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002470 void *pad_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471
2472 assert(qc->flags & ATA_QCFLAG_DMAMAP);
2473 assert(sg != NULL);
2474
2475 if (qc->flags & ATA_QCFLAG_SINGLE)
2476 assert(qc->n_elem == 1);
2477
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05002478 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002480 /* if we padded the buffer out to 32-bit bound, and data
2481 * xfer direction is from-device, we must copy from the
2482 * pad buffer back into the supplied buffer
2483 */
2484 if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
2485 pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2486
2487 if (qc->flags & ATA_QCFLAG_SG) {
Jeff Garzike1410f22005-11-14 14:06:26 -05002488 if (qc->n_elem)
2489 dma_unmap_sg(ap->host_set->dev, sg, qc->n_elem, dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002490 /* restore last sg */
2491 sg[qc->orig_n_elem - 1].length += qc->pad_len;
2492 if (pad_buf) {
2493 struct scatterlist *psg = &qc->pad_sgent;
2494 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2495 memcpy(addr + psg->offset, pad_buf, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05002496 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002497 }
2498 } else {
Jeff Garzike1410f22005-11-14 14:06:26 -05002499 if (sg_dma_len(&sg[0]) > 0)
2500 dma_unmap_single(ap->host_set->dev,
2501 sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
2502 dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002503 /* restore sg */
2504 sg->length += qc->pad_len;
2505 if (pad_buf)
2506 memcpy(qc->buf_virt + sg->length - qc->pad_len,
2507 pad_buf, qc->pad_len);
2508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509
2510 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002511 qc->__sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512}
2513
2514/**
2515 * ata_fill_sg - Fill PCI IDE PRD table
2516 * @qc: Metadata associated with taskfile to be transferred
2517 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002518 * Fill PCI IDE PRD (scatter-gather) table with segments
2519 * associated with the current disk command.
2520 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 * LOCKING:
Jeff Garzik780a87f2005-05-30 15:41:05 -04002522 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523 *
2524 */
2525static void ata_fill_sg(struct ata_queued_cmd *qc)
2526{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002528 struct scatterlist *sg;
2529 unsigned int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002531 assert(qc->__sg != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 assert(qc->n_elem > 0);
2533
2534 idx = 0;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002535 ata_for_each_sg(sg, qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 u32 addr, offset;
2537 u32 sg_len, len;
2538
2539 /* determine if physical DMA addr spans 64K boundary.
2540 * Note h/w doesn't support 64-bit, so we unconditionally
2541 * truncate dma_addr_t to u32.
2542 */
2543 addr = (u32) sg_dma_address(sg);
2544 sg_len = sg_dma_len(sg);
2545
2546 while (sg_len) {
2547 offset = addr & 0xffff;
2548 len = sg_len;
2549 if ((offset + sg_len) > 0x10000)
2550 len = 0x10000 - offset;
2551
2552 ap->prd[idx].addr = cpu_to_le32(addr);
2553 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
2554 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
2555
2556 idx++;
2557 sg_len -= len;
2558 addr += len;
2559 }
2560 }
2561
2562 if (idx)
2563 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2564}
2565/**
2566 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2567 * @qc: Metadata associated with taskfile to check
2568 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002569 * Allow low-level driver to filter ATA PACKET commands, returning
2570 * a status indicating whether or not it is OK to use DMA for the
2571 * supplied PACKET command.
2572 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002574 * spin_lock_irqsave(host_set lock)
2575 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576 * RETURNS: 0 when ATAPI DMA can be used
2577 * nonzero otherwise
2578 */
2579int ata_check_atapi_dma(struct ata_queued_cmd *qc)
2580{
2581 struct ata_port *ap = qc->ap;
2582 int rc = 0; /* Assume ATAPI DMA is OK by default */
2583
2584 if (ap->ops->check_atapi_dma)
2585 rc = ap->ops->check_atapi_dma(qc);
2586
2587 return rc;
2588}
2589/**
2590 * ata_qc_prep - Prepare taskfile for submission
2591 * @qc: Metadata associated with taskfile to be prepared
2592 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002593 * Prepare ATA taskfile for submission.
2594 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 * LOCKING:
2596 * spin_lock_irqsave(host_set lock)
2597 */
2598void ata_qc_prep(struct ata_queued_cmd *qc)
2599{
2600 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2601 return;
2602
2603 ata_fill_sg(qc);
2604}
2605
Jeff Garzik0cba6322005-05-30 19:49:12 -04002606/**
2607 * ata_sg_init_one - Associate command with memory buffer
2608 * @qc: Command to be associated
2609 * @buf: Memory buffer
2610 * @buflen: Length of memory buffer, in bytes.
2611 *
2612 * Initialize the data-related elements of queued_cmd @qc
2613 * to point to a single memory buffer, @buf of byte length @buflen.
2614 *
2615 * LOCKING:
2616 * spin_lock_irqsave(host_set lock)
2617 */
2618
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
2620{
2621 struct scatterlist *sg;
2622
2623 qc->flags |= ATA_QCFLAG_SINGLE;
2624
2625 memset(&qc->sgent, 0, sizeof(qc->sgent));
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002626 qc->__sg = &qc->sgent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 qc->n_elem = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002628 qc->orig_n_elem = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 qc->buf_virt = buf;
2630
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002631 sg = qc->__sg;
Jeff Garzikf0612bb2005-10-30 01:58:18 -05002632 sg_init_one(sg, buf, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633}
2634
Jeff Garzik0cba6322005-05-30 19:49:12 -04002635/**
2636 * ata_sg_init - Associate command with scatter-gather table.
2637 * @qc: Command to be associated
2638 * @sg: Scatter-gather table.
2639 * @n_elem: Number of elements in s/g table.
2640 *
2641 * Initialize the data-related elements of queued_cmd @qc
2642 * to point to a scatter-gather table @sg, containing @n_elem
2643 * elements.
2644 *
2645 * LOCKING:
2646 * spin_lock_irqsave(host_set lock)
2647 */
2648
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
2650 unsigned int n_elem)
2651{
2652 qc->flags |= ATA_QCFLAG_SG;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002653 qc->__sg = sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 qc->n_elem = n_elem;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002655 qc->orig_n_elem = n_elem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656}
2657
2658/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002659 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
2660 * @qc: Command with memory buffer to be mapped.
2661 *
2662 * DMA-map the memory buffer associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 *
2664 * LOCKING:
2665 * spin_lock_irqsave(host_set lock)
2666 *
2667 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002668 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 */
2670
2671static int ata_sg_setup_one(struct ata_queued_cmd *qc)
2672{
2673 struct ata_port *ap = qc->ap;
2674 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002675 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 dma_addr_t dma_address;
2677
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002678 /* we must lengthen transfers to end on a 32-bit boundary */
2679 qc->pad_len = sg->length & 3;
2680 if (qc->pad_len) {
2681 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2682 struct scatterlist *psg = &qc->pad_sgent;
2683
2684 assert(qc->dev->class == ATA_DEV_ATAPI);
2685
2686 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2687
2688 if (qc->tf.flags & ATA_TFLAG_WRITE)
2689 memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
2690 qc->pad_len);
2691
2692 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2693 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2694 /* trim sg */
2695 sg->length -= qc->pad_len;
2696
2697 DPRINTK("padding done, sg->length=%u pad_len=%u\n",
2698 sg->length, qc->pad_len);
2699 }
2700
Jeff Garzike1410f22005-11-14 14:06:26 -05002701 if (!sg->length) {
2702 sg_dma_address(sg) = 0;
2703 goto skip_map;
2704 }
2705
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 dma_address = dma_map_single(ap->host_set->dev, qc->buf_virt,
Albert Lee32529e02005-05-26 03:49:42 -04002707 sg->length, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05002708 if (dma_mapping_error(dma_address)) {
2709 /* restore sg */
2710 sg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05002712 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713
2714 sg_dma_address(sg) = dma_address;
Jeff Garzike1410f22005-11-14 14:06:26 -05002715skip_map:
Albert Lee32529e02005-05-26 03:49:42 -04002716 sg_dma_len(sg) = sg->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717
2718 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
2719 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2720
2721 return 0;
2722}
2723
2724/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002725 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
2726 * @qc: Command with scatter-gather table to be mapped.
2727 *
2728 * DMA-map the scatter-gather table associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 *
2730 * LOCKING:
2731 * spin_lock_irqsave(host_set lock)
2732 *
2733 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002734 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 *
2736 */
2737
2738static int ata_sg_setup(struct ata_queued_cmd *qc)
2739{
2740 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002741 struct scatterlist *sg = qc->__sg;
2742 struct scatterlist *lsg = &sg[qc->n_elem - 1];
Jeff Garzike1410f22005-11-14 14:06:26 -05002743 int n_elem, pre_n_elem, dir, trim_sg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744
2745 VPRINTK("ENTER, ata%u\n", ap->id);
2746 assert(qc->flags & ATA_QCFLAG_SG);
2747
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002748 /* we must lengthen transfers to end on a 32-bit boundary */
2749 qc->pad_len = lsg->length & 3;
2750 if (qc->pad_len) {
2751 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2752 struct scatterlist *psg = &qc->pad_sgent;
2753 unsigned int offset;
2754
2755 assert(qc->dev->class == ATA_DEV_ATAPI);
2756
2757 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2758
2759 /*
2760 * psg->page/offset are used to copy to-be-written
2761 * data in this function or read data in ata_sg_clean.
2762 */
2763 offset = lsg->offset + lsg->length - qc->pad_len;
2764 psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
2765 psg->offset = offset_in_page(offset);
2766
2767 if (qc->tf.flags & ATA_TFLAG_WRITE) {
2768 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2769 memcpy(pad_buf, addr + psg->offset, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05002770 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002771 }
2772
2773 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2774 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2775 /* trim last sg */
2776 lsg->length -= qc->pad_len;
Jeff Garzike1410f22005-11-14 14:06:26 -05002777 if (lsg->length == 0)
2778 trim_sg = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002779
2780 DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
2781 qc->n_elem - 1, lsg->length, qc->pad_len);
2782 }
2783
Jeff Garzike1410f22005-11-14 14:06:26 -05002784 pre_n_elem = qc->n_elem;
2785 if (trim_sg && pre_n_elem)
2786 pre_n_elem--;
2787
2788 if (!pre_n_elem) {
2789 n_elem = 0;
2790 goto skip_map;
2791 }
2792
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 dir = qc->dma_dir;
Jeff Garzike1410f22005-11-14 14:06:26 -05002794 n_elem = dma_map_sg(ap->host_set->dev, sg, pre_n_elem, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05002795 if (n_elem < 1) {
2796 /* restore last sg */
2797 lsg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05002799 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800
2801 DPRINTK("%d sg elements mapped\n", n_elem);
2802
Jeff Garzike1410f22005-11-14 14:06:26 -05002803skip_map:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804 qc->n_elem = n_elem;
2805
2806 return 0;
2807}
2808
2809/**
Tejun Heo40e8c822005-08-22 17:12:45 +09002810 * ata_poll_qc_complete - turn irq back on and finish qc
2811 * @qc: Command to complete
Randy Dunlap8e8b77d2005-11-01 21:29:27 -08002812 * @err_mask: ATA status register content
Tejun Heo40e8c822005-08-22 17:12:45 +09002813 *
2814 * LOCKING:
2815 * None. (grabs host lock)
2816 */
2817
Albert Leea22e2eb2005-12-05 15:38:02 +08002818void ata_poll_qc_complete(struct ata_queued_cmd *qc)
Tejun Heo40e8c822005-08-22 17:12:45 +09002819{
2820 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04002821 unsigned long flags;
Tejun Heo40e8c822005-08-22 17:12:45 +09002822
Jeff Garzikb8f61532005-08-25 22:01:20 -04002823 spin_lock_irqsave(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09002824 ap->flags &= ~ATA_FLAG_NOINTR;
2825 ata_irq_on(ap);
Albert Leea22e2eb2005-12-05 15:38:02 +08002826 ata_qc_complete(qc);
Jeff Garzikb8f61532005-08-25 22:01:20 -04002827 spin_unlock_irqrestore(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09002828}
2829
2830/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 * ata_pio_poll -
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002832 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833 *
2834 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002835 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 *
2837 * RETURNS:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002838 * timeout value to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 */
2840
2841static unsigned long ata_pio_poll(struct ata_port *ap)
2842{
Albert Leec14b8332005-12-05 15:36:08 +08002843 struct ata_queued_cmd *qc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 u8 status;
Albert Lee14be71f2005-09-27 17:36:35 +08002845 unsigned int poll_state = HSM_ST_UNKNOWN;
2846 unsigned int reg_state = HSM_ST_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847
Albert Leec14b8332005-12-05 15:36:08 +08002848 qc = ata_qc_from_tag(ap, ap->active_tag);
2849 assert(qc != NULL);
2850
Albert Lee14be71f2005-09-27 17:36:35 +08002851 switch (ap->hsm_task_state) {
2852 case HSM_ST:
2853 case HSM_ST_POLL:
2854 poll_state = HSM_ST_POLL;
2855 reg_state = HSM_ST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 break;
Albert Lee14be71f2005-09-27 17:36:35 +08002857 case HSM_ST_LAST:
2858 case HSM_ST_LAST_POLL:
2859 poll_state = HSM_ST_LAST_POLL;
2860 reg_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 break;
2862 default:
2863 BUG();
2864 break;
2865 }
2866
2867 status = ata_chk_status(ap);
2868 if (status & ATA_BUSY) {
2869 if (time_after(jiffies, ap->pio_task_timeout)) {
Albert Lee1c848982005-12-05 15:40:15 +08002870 qc->err_mask |= AC_ERR_ATA_BUS;
Albert Lee7c398332005-11-09 13:03:30 +08002871 ap->hsm_task_state = HSM_ST_TMOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 return 0;
2873 }
Albert Lee14be71f2005-09-27 17:36:35 +08002874 ap->hsm_task_state = poll_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875 return ATA_SHORT_PAUSE;
2876 }
2877
Albert Lee14be71f2005-09-27 17:36:35 +08002878 ap->hsm_task_state = reg_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 return 0;
2880}
2881
2882/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002883 * ata_pio_complete - check if drive is busy or idle
2884 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 *
2886 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002887 * None. (executing in kernel thread context)
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002888 *
2889 * RETURNS:
2890 * Non-zero if qc completed, zero otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891 */
2892
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002893static int ata_pio_complete (struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002894{
2895 struct ata_queued_cmd *qc;
2896 u8 drv_stat;
2897
2898 /*
Alan Cox31433ea2005-08-26 15:56:47 +01002899 * This is purely heuristic. This is a fast path. Sometimes when
2900 * we enter, BSY will be cleared in a chk-status or two. If not,
2901 * the drive is probably seeking or something. Snooze for a couple
2902 * msecs, then chk-status again. If still busy, fall back to
Albert Lee14be71f2005-09-27 17:36:35 +08002903 * HSM_ST_POLL state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 */
Albert Leefe79e682005-12-06 11:34:59 +08002905 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
2906 if (drv_stat & ATA_BUSY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 msleep(2);
Albert Leefe79e682005-12-06 11:34:59 +08002908 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
2909 if (drv_stat & ATA_BUSY) {
Albert Lee14be71f2005-09-27 17:36:35 +08002910 ap->hsm_task_state = HSM_ST_LAST_POLL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002912 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 }
2914 }
2915
Albert Leec14b8332005-12-05 15:36:08 +08002916 qc = ata_qc_from_tag(ap, ap->active_tag);
2917 assert(qc != NULL);
2918
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 drv_stat = ata_wait_idle(ap);
2920 if (!ata_ok(drv_stat)) {
Albert Lee1c848982005-12-05 15:40:15 +08002921 qc->err_mask |= __ac_err_mask(drv_stat);
Albert Lee14be71f2005-09-27 17:36:35 +08002922 ap->hsm_task_state = HSM_ST_ERR;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002923 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 }
2925
Albert Lee14be71f2005-09-27 17:36:35 +08002926 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927
Albert Leea22e2eb2005-12-05 15:38:02 +08002928 assert(qc->err_mask == 0);
2929 ata_poll_qc_complete(qc);
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002930
2931 /* another command may start at this point */
2932
2933 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934}
2935
Edward Falk0baab862005-06-02 18:17:13 -04002936
2937/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002938 * swap_buf_le16 - swap halves of 16-words in place
Edward Falk0baab862005-06-02 18:17:13 -04002939 * @buf: Buffer to swap
2940 * @buf_words: Number of 16-bit words in buffer.
2941 *
2942 * Swap halves of 16-bit words if needed to convert from
2943 * little-endian byte order to native cpu byte order, or
2944 * vice-versa.
2945 *
2946 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002947 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04002948 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949void swap_buf_le16(u16 *buf, unsigned int buf_words)
2950{
2951#ifdef __BIG_ENDIAN
2952 unsigned int i;
2953
2954 for (i = 0; i < buf_words; i++)
2955 buf[i] = le16_to_cpu(buf[i]);
2956#endif /* __BIG_ENDIAN */
2957}
2958
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002959/**
2960 * ata_mmio_data_xfer - Transfer data by MMIO
2961 * @ap: port to read/write
2962 * @buf: data buffer
2963 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04002964 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002965 *
2966 * Transfer data from/to the device data register by MMIO.
2967 *
2968 * LOCKING:
2969 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002970 */
2971
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
2973 unsigned int buflen, int write_data)
2974{
2975 unsigned int i;
2976 unsigned int words = buflen >> 1;
2977 u16 *buf16 = (u16 *) buf;
2978 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
2979
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002980 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 if (write_data) {
2982 for (i = 0; i < words; i++)
2983 writew(le16_to_cpu(buf16[i]), mmio);
2984 } else {
2985 for (i = 0; i < words; i++)
2986 buf16[i] = cpu_to_le16(readw(mmio));
2987 }
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002988
2989 /* Transfer trailing 1 byte, if any. */
2990 if (unlikely(buflen & 0x01)) {
2991 u16 align_buf[1] = { 0 };
2992 unsigned char *trailing_buf = buf + buflen - 1;
2993
2994 if (write_data) {
2995 memcpy(align_buf, trailing_buf, 1);
2996 writew(le16_to_cpu(align_buf[0]), mmio);
2997 } else {
2998 align_buf[0] = cpu_to_le16(readw(mmio));
2999 memcpy(trailing_buf, align_buf, 1);
3000 }
3001 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002}
3003
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003004/**
3005 * ata_pio_data_xfer - Transfer data by PIO
3006 * @ap: port to read/write
3007 * @buf: data buffer
3008 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04003009 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003010 *
3011 * Transfer data from/to the device data register by PIO.
3012 *
3013 * LOCKING:
3014 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003015 */
3016
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
3018 unsigned int buflen, int write_data)
3019{
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003020 unsigned int words = buflen >> 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003022 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023 if (write_data)
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003024 outsw(ap->ioaddr.data_addr, buf, words);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 else
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003026 insw(ap->ioaddr.data_addr, buf, words);
3027
3028 /* Transfer trailing 1 byte, if any. */
3029 if (unlikely(buflen & 0x01)) {
3030 u16 align_buf[1] = { 0 };
3031 unsigned char *trailing_buf = buf + buflen - 1;
3032
3033 if (write_data) {
3034 memcpy(align_buf, trailing_buf, 1);
3035 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
3036 } else {
3037 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
3038 memcpy(trailing_buf, align_buf, 1);
3039 }
3040 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041}
3042
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003043/**
3044 * ata_data_xfer - Transfer data from/to the data register.
3045 * @ap: port to read/write
3046 * @buf: data buffer
3047 * @buflen: buffer length
3048 * @do_write: read/write
3049 *
3050 * Transfer data from/to the device data register.
3051 *
3052 * LOCKING:
3053 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003054 */
3055
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
3057 unsigned int buflen, int do_write)
3058{
3059 if (ap->flags & ATA_FLAG_MMIO)
3060 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3061 else
3062 ata_pio_data_xfer(ap, buf, buflen, do_write);
3063}
3064
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003065/**
3066 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
3067 * @qc: Command on going
3068 *
3069 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
3070 *
3071 * LOCKING:
3072 * Inherited from caller.
3073 */
3074
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075static void ata_pio_sector(struct ata_queued_cmd *qc)
3076{
3077 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003078 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 struct ata_port *ap = qc->ap;
3080 struct page *page;
3081 unsigned int offset;
3082 unsigned char *buf;
3083
3084 if (qc->cursect == (qc->nsect - 1))
Albert Lee14be71f2005-09-27 17:36:35 +08003085 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086
3087 page = sg[qc->cursg].page;
3088 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
3089
3090 /* get the current page and offset */
3091 page = nth_page(page, (offset >> PAGE_SHIFT));
3092 offset %= PAGE_SIZE;
3093
3094 buf = kmap(page) + offset;
3095
3096 qc->cursect++;
3097 qc->cursg_ofs++;
3098
Albert Lee32529e02005-05-26 03:49:42 -04003099 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100 qc->cursg++;
3101 qc->cursg_ofs = 0;
3102 }
3103
3104 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3105
3106 /* do the actual data transfer */
3107 do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3108 ata_data_xfer(ap, buf, ATA_SECT_SIZE, do_write);
3109
3110 kunmap(page);
3111}
3112
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003113/**
3114 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
3115 * @qc: Command on going
3116 * @bytes: number of bytes
3117 *
3118 * Transfer Transfer data from/to the ATAPI device.
3119 *
3120 * LOCKING:
3121 * Inherited from caller.
3122 *
3123 */
3124
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
3126{
3127 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003128 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 struct ata_port *ap = qc->ap;
3130 struct page *page;
3131 unsigned char *buf;
3132 unsigned int offset, count;
3133
Albert Lee563a6e12005-08-12 14:17:50 +08003134 if (qc->curbytes + bytes >= qc->nbytes)
Albert Lee14be71f2005-09-27 17:36:35 +08003135 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136
3137next_sg:
Albert Lee563a6e12005-08-12 14:17:50 +08003138 if (unlikely(qc->cursg >= qc->n_elem)) {
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003139 /*
Albert Lee563a6e12005-08-12 14:17:50 +08003140 * The end of qc->sg is reached and the device expects
3141 * more data to transfer. In order not to overrun qc->sg
3142 * and fulfill length specified in the byte count register,
3143 * - for read case, discard trailing data from the device
3144 * - for write case, padding zero data to the device
3145 */
3146 u16 pad_buf[1] = { 0 };
3147 unsigned int words = bytes >> 1;
3148 unsigned int i;
3149
3150 if (words) /* warning if bytes > 1 */
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003151 printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
Albert Lee563a6e12005-08-12 14:17:50 +08003152 ap->id, bytes);
3153
3154 for (i = 0; i < words; i++)
3155 ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
3156
Albert Lee14be71f2005-09-27 17:36:35 +08003157 ap->hsm_task_state = HSM_ST_LAST;
Albert Lee563a6e12005-08-12 14:17:50 +08003158 return;
3159 }
3160
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003161 sg = &qc->__sg[qc->cursg];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 page = sg->page;
3164 offset = sg->offset + qc->cursg_ofs;
3165
3166 /* get the current page and offset */
3167 page = nth_page(page, (offset >> PAGE_SHIFT));
3168 offset %= PAGE_SIZE;
3169
Albert Lee6952df02005-06-06 15:56:03 +08003170 /* don't overrun current sg */
Albert Lee32529e02005-05-26 03:49:42 -04003171 count = min(sg->length - qc->cursg_ofs, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172
3173 /* don't cross page boundaries */
3174 count = min(count, (unsigned int)PAGE_SIZE - offset);
3175
3176 buf = kmap(page) + offset;
3177
3178 bytes -= count;
3179 qc->curbytes += count;
3180 qc->cursg_ofs += count;
3181
Albert Lee32529e02005-05-26 03:49:42 -04003182 if (qc->cursg_ofs == sg->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183 qc->cursg++;
3184 qc->cursg_ofs = 0;
3185 }
3186
3187 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3188
3189 /* do the actual data transfer */
3190 ata_data_xfer(ap, buf, count, do_write);
3191
3192 kunmap(page);
3193
Albert Lee563a6e12005-08-12 14:17:50 +08003194 if (bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 goto next_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196}
3197
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003198/**
3199 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
3200 * @qc: Command on going
3201 *
3202 * Transfer Transfer data from/to the ATAPI device.
3203 *
3204 * LOCKING:
3205 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003206 */
3207
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208static void atapi_pio_bytes(struct ata_queued_cmd *qc)
3209{
3210 struct ata_port *ap = qc->ap;
3211 struct ata_device *dev = qc->dev;
3212 unsigned int ireason, bc_lo, bc_hi, bytes;
3213 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
3214
3215 ap->ops->tf_read(ap, &qc->tf);
3216 ireason = qc->tf.nsect;
3217 bc_lo = qc->tf.lbam;
3218 bc_hi = qc->tf.lbah;
3219 bytes = (bc_hi << 8) | bc_lo;
3220
3221 /* shall be cleared to zero, indicating xfer of data */
3222 if (ireason & (1 << 0))
3223 goto err_out;
3224
3225 /* make sure transfer direction matches expected */
3226 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
3227 if (do_write != i_write)
3228 goto err_out;
3229
3230 __atapi_pio_bytes(qc, bytes);
3231
3232 return;
3233
3234err_out:
3235 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
3236 ap->id, dev->devno);
Albert Lee1c848982005-12-05 15:40:15 +08003237 qc->err_mask |= AC_ERR_ATA_BUS;
Albert Lee14be71f2005-09-27 17:36:35 +08003238 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239}
3240
3241/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003242 * ata_pio_block - start PIO on a block
3243 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244 *
3245 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003246 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247 */
3248
3249static void ata_pio_block(struct ata_port *ap)
3250{
3251 struct ata_queued_cmd *qc;
3252 u8 status;
3253
3254 /*
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003255 * This is purely heuristic. This is a fast path.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 * Sometimes when we enter, BSY will be cleared in
3257 * a chk-status or two. If not, the drive is probably seeking
3258 * or something. Snooze for a couple msecs, then
3259 * chk-status again. If still busy, fall back to
Albert Lee14be71f2005-09-27 17:36:35 +08003260 * HSM_ST_POLL state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 */
3262 status = ata_busy_wait(ap, ATA_BUSY, 5);
3263 if (status & ATA_BUSY) {
3264 msleep(2);
3265 status = ata_busy_wait(ap, ATA_BUSY, 10);
3266 if (status & ATA_BUSY) {
Albert Lee14be71f2005-09-27 17:36:35 +08003267 ap->hsm_task_state = HSM_ST_POLL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
3269 return;
3270 }
3271 }
3272
3273 qc = ata_qc_from_tag(ap, ap->active_tag);
3274 assert(qc != NULL);
3275
Albert Leefe79e682005-12-06 11:34:59 +08003276 /* check error */
3277 if (status & (ATA_ERR | ATA_DF)) {
3278 qc->err_mask |= AC_ERR_DEV;
3279 ap->hsm_task_state = HSM_ST_ERR;
3280 return;
3281 }
3282
3283 /* transfer data if any */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 if (is_atapi_taskfile(&qc->tf)) {
Albert Leefe79e682005-12-06 11:34:59 +08003285 /* DRQ=0 means no more data to transfer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003286 if ((status & ATA_DRQ) == 0) {
Albert Lee14be71f2005-09-27 17:36:35 +08003287 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 return;
3289 }
3290
3291 atapi_pio_bytes(qc);
3292 } else {
3293 /* handle BSY=0, DRQ=0 as error */
3294 if ((status & ATA_DRQ) == 0) {
Albert Lee1c848982005-12-05 15:40:15 +08003295 qc->err_mask |= AC_ERR_ATA_BUS;
Albert Lee14be71f2005-09-27 17:36:35 +08003296 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 return;
3298 }
3299
3300 ata_pio_sector(qc);
3301 }
3302}
3303
3304static void ata_pio_error(struct ata_port *ap)
3305{
3306 struct ata_queued_cmd *qc;
Jeff Garzika7dac442005-10-30 04:44:42 -05003307
3308 printk(KERN_WARNING "ata%u: PIO error\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003309
3310 qc = ata_qc_from_tag(ap, ap->active_tag);
3311 assert(qc != NULL);
3312
Albert Lee1c848982005-12-05 15:40:15 +08003313 /* make sure qc->err_mask is available to
3314 * know what's wrong and recover
3315 */
3316 assert(qc->err_mask);
3317
Albert Lee14be71f2005-09-27 17:36:35 +08003318 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003319
Albert Leea22e2eb2005-12-05 15:38:02 +08003320 ata_poll_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003321}
3322
3323static void ata_pio_task(void *_data)
3324{
3325 struct ata_port *ap = _data;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003326 unsigned long timeout;
3327 int qc_completed;
3328
3329fsm_start:
3330 timeout = 0;
3331 qc_completed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332
Albert Lee14be71f2005-09-27 17:36:35 +08003333 switch (ap->hsm_task_state) {
3334 case HSM_ST_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335 return;
3336
Albert Lee14be71f2005-09-27 17:36:35 +08003337 case HSM_ST:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 ata_pio_block(ap);
3339 break;
3340
Albert Lee14be71f2005-09-27 17:36:35 +08003341 case HSM_ST_LAST:
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003342 qc_completed = ata_pio_complete(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 break;
3344
Albert Lee14be71f2005-09-27 17:36:35 +08003345 case HSM_ST_POLL:
3346 case HSM_ST_LAST_POLL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 timeout = ata_pio_poll(ap);
3348 break;
3349
Albert Lee14be71f2005-09-27 17:36:35 +08003350 case HSM_ST_TMOUT:
3351 case HSM_ST_ERR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 ata_pio_error(ap);
3353 return;
3354 }
3355
3356 if (timeout)
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003357 queue_delayed_work(ata_wq, &ap->pio_task, timeout);
3358 else if (!qc_completed)
3359 goto fsm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360}
3361
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362/**
3363 * ata_qc_timeout - Handle timeout of queued command
3364 * @qc: Command that timed out
3365 *
3366 * Some part of the kernel (currently, only the SCSI layer)
3367 * has noticed that the active command on port @ap has not
3368 * completed after a specified length of time. Handle this
3369 * condition by disabling DMA (if necessary) and completing
3370 * transactions, with error if necessary.
3371 *
3372 * This also handles the case of the "lost interrupt", where
3373 * for some reason (possibly hardware bug, possibly driver bug)
3374 * an interrupt was not delivered to the driver, even though the
3375 * transaction completed successfully.
3376 *
3377 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003378 * Inherited from SCSI layer (none, can sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 */
3380
3381static void ata_qc_timeout(struct ata_queued_cmd *qc)
3382{
3383 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003384 struct ata_host_set *host_set = ap->host_set;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 u8 host_stat = 0, drv_stat;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003386 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387
3388 DPRINTK("ENTER\n");
3389
Jeff Garzikb8f61532005-08-25 22:01:20 -04003390 spin_lock_irqsave(&host_set->lock, flags);
3391
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 /* hack alert! We cannot use the supplied completion
3393 * function from inside the ->eh_strategy_handler() thread.
3394 * libata is the only user of ->eh_strategy_handler() in
3395 * any kernel, so the default scsi_done() assumes it is
3396 * not being called from the SCSI EH.
3397 */
3398 qc->scsidone = scsi_finish_command;
3399
3400 switch (qc->tf.protocol) {
3401
3402 case ATA_PROT_DMA:
3403 case ATA_PROT_ATAPI_DMA:
3404 host_stat = ap->ops->bmdma_status(ap);
3405
3406 /* before we do anything else, clear DMA-Start bit */
Alan Coxb73fc892005-08-26 16:03:19 +01003407 ap->ops->bmdma_stop(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408
3409 /* fall through */
3410
3411 default:
3412 ata_altstatus(ap);
3413 drv_stat = ata_chk_status(ap);
3414
3415 /* ack bmdma irq events */
3416 ap->ops->irq_clear(ap);
3417
3418 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
3419 ap->id, qc->tf.command, drv_stat, host_stat);
3420
3421 /* complete taskfile transaction */
Albert Leea22e2eb2005-12-05 15:38:02 +08003422 qc->err_mask |= ac_err_mask(drv_stat);
3423 ata_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 break;
3425 }
Jeff Garzikb8f61532005-08-25 22:01:20 -04003426
3427 spin_unlock_irqrestore(&host_set->lock, flags);
3428
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 DPRINTK("EXIT\n");
3430}
3431
3432/**
3433 * ata_eng_timeout - Handle timeout of queued command
3434 * @ap: Port on which timed-out command is active
3435 *
3436 * Some part of the kernel (currently, only the SCSI layer)
3437 * has noticed that the active command on port @ap has not
3438 * completed after a specified length of time. Handle this
3439 * condition by disabling DMA (if necessary) and completing
3440 * transactions, with error if necessary.
3441 *
3442 * This also handles the case of the "lost interrupt", where
3443 * for some reason (possibly hardware bug, possibly driver bug)
3444 * an interrupt was not delivered to the driver, even though the
3445 * transaction completed successfully.
3446 *
3447 * LOCKING:
3448 * Inherited from SCSI layer (none, can sleep)
3449 */
3450
3451void ata_eng_timeout(struct ata_port *ap)
3452{
3453 struct ata_queued_cmd *qc;
3454
3455 DPRINTK("ENTER\n");
3456
3457 qc = ata_qc_from_tag(ap, ap->active_tag);
Jeff Garzike12669e2005-10-05 18:39:23 -04003458 if (qc)
3459 ata_qc_timeout(qc);
3460 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461 printk(KERN_ERR "ata%u: BUG: timeout without command\n",
3462 ap->id);
3463 goto out;
3464 }
3465
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466out:
3467 DPRINTK("EXIT\n");
3468}
3469
3470/**
3471 * ata_qc_new - Request an available ATA command, for queueing
3472 * @ap: Port associated with device @dev
3473 * @dev: Device from whom we request an available command structure
3474 *
3475 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003476 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477 */
3478
3479static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
3480{
3481 struct ata_queued_cmd *qc = NULL;
3482 unsigned int i;
3483
3484 for (i = 0; i < ATA_MAX_QUEUE; i++)
3485 if (!test_and_set_bit(i, &ap->qactive)) {
3486 qc = ata_qc_from_tag(ap, i);
3487 break;
3488 }
3489
3490 if (qc)
3491 qc->tag = i;
3492
3493 return qc;
3494}
3495
3496/**
3497 * ata_qc_new_init - Request an available ATA command, and initialize it
3498 * @ap: Port associated with device @dev
3499 * @dev: Device from whom we request an available command structure
3500 *
3501 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003502 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 */
3504
3505struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
3506 struct ata_device *dev)
3507{
3508 struct ata_queued_cmd *qc;
3509
3510 qc = ata_qc_new(ap);
3511 if (qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512 qc->scsicmd = NULL;
3513 qc->ap = ap;
3514 qc->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05003516 ata_qc_reinit(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517 }
3518
3519 return qc;
3520}
3521
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522static void __ata_qc_complete(struct ata_queued_cmd *qc)
3523{
3524 struct ata_port *ap = qc->ap;
Tejun Heob5632302005-12-13 14:51:25 +09003525 unsigned int tag;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526
3527 qc->flags = 0;
3528 tag = qc->tag;
3529 if (likely(ata_tag_valid(tag))) {
3530 if (tag == ap->active_tag)
3531 ap->active_tag = ATA_TAG_POISON;
3532 qc->tag = ATA_TAG_POISON;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533 clear_bit(tag, &ap->qactive);
Tejun Heob5632302005-12-13 14:51:25 +09003534 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535}
3536
3537/**
3538 * ata_qc_free - free unused ata_queued_cmd
3539 * @qc: Command to complete
3540 *
3541 * Designed to free unused ata_queued_cmd object
3542 * in case something prevents using it.
3543 *
3544 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003545 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546 */
3547void ata_qc_free(struct ata_queued_cmd *qc)
3548{
3549 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003550
3551 __ata_qc_complete(qc);
3552}
3553
3554/**
3555 * ata_qc_complete - Complete an active ATA command
3556 * @qc: Command to complete
Randy Dunlap8e8b77d2005-11-01 21:29:27 -08003557 * @err_mask: ATA Status register contents
Jeff Garzik0cba6322005-05-30 19:49:12 -04003558 *
3559 * Indicate to the mid and upper layers that an ATA
3560 * command has completed, with either an ok or not-ok status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561 *
3562 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003563 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564 */
3565
Albert Leea22e2eb2005-12-05 15:38:02 +08003566void ata_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567{
3568 int rc;
3569
3570 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
3571 assert(qc->flags & ATA_QCFLAG_ACTIVE);
3572
3573 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
3574 ata_sg_clean(qc);
3575
Albert Lee3f3791d2005-08-16 14:25:38 +08003576 /* atapi: mark qc as inactive to prevent the interrupt handler
3577 * from completing the command twice later, before the error handler
3578 * is called. (when rc != 0 and atapi request sense is needed)
3579 */
3580 qc->flags &= ~ATA_QCFLAG_ACTIVE;
3581
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582 /* call completion callback */
Albert Leea22e2eb2005-12-05 15:38:02 +08003583 rc = qc->complete_fn(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584
3585 /* if callback indicates not to complete command (non-zero),
3586 * return immediately
3587 */
3588 if (rc != 0)
3589 return;
3590
3591 __ata_qc_complete(qc);
3592
3593 VPRINTK("EXIT\n");
3594}
3595
3596static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
3597{
3598 struct ata_port *ap = qc->ap;
3599
3600 switch (qc->tf.protocol) {
3601 case ATA_PROT_DMA:
3602 case ATA_PROT_ATAPI_DMA:
3603 return 1;
3604
3605 case ATA_PROT_ATAPI:
3606 case ATA_PROT_PIO:
3607 case ATA_PROT_PIO_MULT:
3608 if (ap->flags & ATA_FLAG_PIO_DMA)
3609 return 1;
3610
3611 /* fall through */
3612
3613 default:
3614 return 0;
3615 }
3616
3617 /* never reached */
3618}
3619
3620/**
3621 * ata_qc_issue - issue taskfile to device
3622 * @qc: command to issue to device
3623 *
3624 * Prepare an ATA command to submission to device.
3625 * This includes mapping the data into a DMA-able
3626 * area, filling in the S/G table, and finally
3627 * writing the taskfile to hardware, starting the command.
3628 *
3629 * LOCKING:
3630 * spin_lock_irqsave(host_set lock)
3631 *
3632 * RETURNS:
3633 * Zero on success, negative on error.
3634 */
3635
3636int ata_qc_issue(struct ata_queued_cmd *qc)
3637{
3638 struct ata_port *ap = qc->ap;
3639
3640 if (ata_should_dma_map(qc)) {
3641 if (qc->flags & ATA_QCFLAG_SG) {
3642 if (ata_sg_setup(qc))
3643 goto err_out;
3644 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
3645 if (ata_sg_setup_one(qc))
3646 goto err_out;
3647 }
3648 } else {
3649 qc->flags &= ~ATA_QCFLAG_DMAMAP;
3650 }
3651
3652 ap->ops->qc_prep(qc);
3653
3654 qc->ap->active_tag = qc->tag;
3655 qc->flags |= ATA_QCFLAG_ACTIVE;
3656
3657 return ap->ops->qc_issue(qc);
3658
3659err_out:
3660 return -1;
3661}
3662
Edward Falk0baab862005-06-02 18:17:13 -04003663
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664/**
3665 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
3666 * @qc: command to issue to device
3667 *
3668 * Using various libata functions and hooks, this function
3669 * starts an ATA command. ATA commands are grouped into
3670 * classes called "protocols", and issuing each type of protocol
3671 * is slightly different.
3672 *
Edward Falk0baab862005-06-02 18:17:13 -04003673 * May be used as the qc_issue() entry in ata_port_operations.
3674 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675 * LOCKING:
3676 * spin_lock_irqsave(host_set lock)
3677 *
3678 * RETURNS:
3679 * Zero on success, negative on error.
3680 */
3681
3682int ata_qc_issue_prot(struct ata_queued_cmd *qc)
3683{
3684 struct ata_port *ap = qc->ap;
3685
3686 ata_dev_select(ap, qc->dev->devno, 1, 0);
3687
3688 switch (qc->tf.protocol) {
3689 case ATA_PROT_NODATA:
Jeff Garzike5338252005-10-30 21:37:17 -05003690 ata_tf_to_host(ap, &qc->tf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003691 break;
3692
3693 case ATA_PROT_DMA:
3694 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3695 ap->ops->bmdma_setup(qc); /* set up bmdma */
3696 ap->ops->bmdma_start(qc); /* initiate bmdma */
3697 break;
3698
3699 case ATA_PROT_PIO: /* load tf registers, initiate polling pio */
3700 ata_qc_set_polling(qc);
Jeff Garzike5338252005-10-30 21:37:17 -05003701 ata_tf_to_host(ap, &qc->tf);
Albert Lee14be71f2005-09-27 17:36:35 +08003702 ap->hsm_task_state = HSM_ST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703 queue_work(ata_wq, &ap->pio_task);
3704 break;
3705
3706 case ATA_PROT_ATAPI:
3707 ata_qc_set_polling(qc);
Jeff Garzike5338252005-10-30 21:37:17 -05003708 ata_tf_to_host(ap, &qc->tf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709 queue_work(ata_wq, &ap->packet_task);
3710 break;
3711
3712 case ATA_PROT_ATAPI_NODATA:
Tejun Heoc1389502005-08-22 14:59:24 +09003713 ap->flags |= ATA_FLAG_NOINTR;
Jeff Garzike5338252005-10-30 21:37:17 -05003714 ata_tf_to_host(ap, &qc->tf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715 queue_work(ata_wq, &ap->packet_task);
3716 break;
3717
3718 case ATA_PROT_ATAPI_DMA:
Tejun Heoc1389502005-08-22 14:59:24 +09003719 ap->flags |= ATA_FLAG_NOINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3721 ap->ops->bmdma_setup(qc); /* set up bmdma */
3722 queue_work(ata_wq, &ap->packet_task);
3723 break;
3724
3725 default:
3726 WARN_ON(1);
3727 return -1;
3728 }
3729
3730 return 0;
3731}
3732
3733/**
Edward Falk0baab862005-06-02 18:17:13 -04003734 * ata_bmdma_setup_mmio - Set up 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_setup_mmio (struct ata_queued_cmd *qc)
3742{
3743 struct ata_port *ap = qc->ap;
3744 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3745 u8 dmactl;
3746 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3747
3748 /* load PRD table addr. */
3749 mb(); /* make sure PRD table writes are visible to controller */
3750 writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
3751
3752 /* specify data direction, triple-check start bit is clear */
3753 dmactl = readb(mmio + ATA_DMA_CMD);
3754 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3755 if (!rw)
3756 dmactl |= ATA_DMA_WR;
3757 writeb(dmactl, mmio + ATA_DMA_CMD);
3758
3759 /* issue r/w command */
3760 ap->ops->exec_command(ap, &qc->tf);
3761}
3762
3763/**
Alan Coxb73fc892005-08-26 16:03:19 +01003764 * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765 * @qc: Info associated with this ATA transaction.
3766 *
3767 * LOCKING:
3768 * spin_lock_irqsave(host_set lock)
3769 */
3770
3771static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
3772{
3773 struct ata_port *ap = qc->ap;
3774 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3775 u8 dmactl;
3776
3777 /* start host DMA transaction */
3778 dmactl = readb(mmio + ATA_DMA_CMD);
3779 writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
3780
3781 /* Strictly, one may wish to issue a readb() here, to
3782 * flush the mmio write. However, control also passes
3783 * to the hardware at this point, and it will interrupt
3784 * us when we are to resume control. So, in effect,
3785 * we don't care when the mmio write flushes.
3786 * Further, a read of the DMA status register _immediately_
3787 * following the write may not be what certain flaky hardware
3788 * is expected, so I think it is best to not add a readb()
3789 * without first all the MMIO ATA cards/mobos.
3790 * Or maybe I'm just being paranoid.
3791 */
3792}
3793
3794/**
3795 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
3796 * @qc: Info associated with this ATA transaction.
3797 *
3798 * LOCKING:
3799 * spin_lock_irqsave(host_set lock)
3800 */
3801
3802static void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
3803{
3804 struct ata_port *ap = qc->ap;
3805 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3806 u8 dmactl;
3807
3808 /* load PRD table addr. */
3809 outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
3810
3811 /* specify data direction, triple-check start bit is clear */
3812 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3813 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3814 if (!rw)
3815 dmactl |= ATA_DMA_WR;
3816 outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3817
3818 /* issue r/w command */
3819 ap->ops->exec_command(ap, &qc->tf);
3820}
3821
3822/**
3823 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
3824 * @qc: Info associated with this ATA transaction.
3825 *
3826 * LOCKING:
3827 * spin_lock_irqsave(host_set lock)
3828 */
3829
3830static void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
3831{
3832 struct ata_port *ap = qc->ap;
3833 u8 dmactl;
3834
3835 /* start host DMA transaction */
3836 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3837 outb(dmactl | ATA_DMA_START,
3838 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3839}
3840
Edward Falk0baab862005-06-02 18:17:13 -04003841
3842/**
3843 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
3844 * @qc: Info associated with this ATA transaction.
3845 *
3846 * Writes the ATA_DMA_START flag to the DMA command register.
3847 *
3848 * May be used as the bmdma_start() entry in ata_port_operations.
3849 *
3850 * LOCKING:
3851 * spin_lock_irqsave(host_set lock)
3852 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003853void ata_bmdma_start(struct ata_queued_cmd *qc)
3854{
3855 if (qc->ap->flags & ATA_FLAG_MMIO)
3856 ata_bmdma_start_mmio(qc);
3857 else
3858 ata_bmdma_start_pio(qc);
3859}
3860
Edward Falk0baab862005-06-02 18:17:13 -04003861
3862/**
3863 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
3864 * @qc: Info associated with this ATA transaction.
3865 *
3866 * Writes address of PRD table to device's PRD Table Address
3867 * register, sets the DMA control register, and calls
3868 * ops->exec_command() to start the transfer.
3869 *
3870 * May be used as the bmdma_setup() entry in ata_port_operations.
3871 *
3872 * LOCKING:
3873 * spin_lock_irqsave(host_set lock)
3874 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875void ata_bmdma_setup(struct ata_queued_cmd *qc)
3876{
3877 if (qc->ap->flags & ATA_FLAG_MMIO)
3878 ata_bmdma_setup_mmio(qc);
3879 else
3880 ata_bmdma_setup_pio(qc);
3881}
3882
Edward Falk0baab862005-06-02 18:17:13 -04003883
3884/**
3885 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
Jeff Garzikdecc6d02005-06-02 18:42:33 -04003886 * @ap: Port associated with this ATA transaction.
Edward Falk0baab862005-06-02 18:17:13 -04003887 *
3888 * Clear interrupt and error flags in DMA status register.
3889 *
3890 * May be used as the irq_clear() entry in ata_port_operations.
3891 *
3892 * LOCKING:
3893 * spin_lock_irqsave(host_set lock)
3894 */
3895
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896void ata_bmdma_irq_clear(struct ata_port *ap)
3897{
3898 if (ap->flags & ATA_FLAG_MMIO) {
3899 void __iomem *mmio = ((void __iomem *) ap->ioaddr.bmdma_addr) + ATA_DMA_STATUS;
3900 writeb(readb(mmio), mmio);
3901 } else {
3902 unsigned long addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
3903 outb(inb(addr), addr);
3904 }
3905
3906}
3907
Edward Falk0baab862005-06-02 18:17:13 -04003908
3909/**
3910 * ata_bmdma_status - Read PCI IDE BMDMA status
Jeff Garzikdecc6d02005-06-02 18:42:33 -04003911 * @ap: Port associated with this ATA transaction.
Edward Falk0baab862005-06-02 18:17:13 -04003912 *
3913 * Read and return BMDMA status register.
3914 *
3915 * May be used as the bmdma_status() entry in ata_port_operations.
3916 *
3917 * LOCKING:
3918 * spin_lock_irqsave(host_set lock)
3919 */
3920
Linus Torvalds1da177e2005-04-16 15:20:36 -07003921u8 ata_bmdma_status(struct ata_port *ap)
3922{
3923 u8 host_stat;
3924 if (ap->flags & ATA_FLAG_MMIO) {
3925 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3926 host_stat = readb(mmio + ATA_DMA_STATUS);
3927 } else
Albert Leeee500aa2005-09-27 17:34:38 +08003928 host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003929 return host_stat;
3930}
3931
Edward Falk0baab862005-06-02 18:17:13 -04003932
3933/**
3934 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
Alan Coxb73fc892005-08-26 16:03:19 +01003935 * @qc: Command we are ending DMA for
Edward Falk0baab862005-06-02 18:17:13 -04003936 *
3937 * Clears the ATA_DMA_START flag in the dma control register
3938 *
3939 * May be used as the bmdma_stop() entry in ata_port_operations.
3940 *
3941 * LOCKING:
3942 * spin_lock_irqsave(host_set lock)
3943 */
3944
Alan Coxb73fc892005-08-26 16:03:19 +01003945void ata_bmdma_stop(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003946{
Alan Coxb73fc892005-08-26 16:03:19 +01003947 struct ata_port *ap = qc->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948 if (ap->flags & ATA_FLAG_MMIO) {
3949 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3950
3951 /* clear start/stop bit */
3952 writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
3953 mmio + ATA_DMA_CMD);
3954 } else {
3955 /* clear start/stop bit */
3956 outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
3957 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3958 }
3959
3960 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
3961 ata_altstatus(ap); /* dummy read */
3962}
3963
3964/**
3965 * ata_host_intr - Handle host interrupt for given (port, task)
3966 * @ap: Port on which interrupt arrived (possibly...)
3967 * @qc: Taskfile currently active in engine
3968 *
3969 * Handle host interrupt for given queued command. Currently,
3970 * only DMA interrupts are handled. All other commands are
3971 * handled via polling with interrupts disabled (nIEN bit).
3972 *
3973 * LOCKING:
3974 * spin_lock_irqsave(host_set lock)
3975 *
3976 * RETURNS:
3977 * One if interrupt was handled, zero if not (shared irq).
3978 */
3979
3980inline unsigned int ata_host_intr (struct ata_port *ap,
3981 struct ata_queued_cmd *qc)
3982{
3983 u8 status, host_stat;
3984
3985 switch (qc->tf.protocol) {
3986
3987 case ATA_PROT_DMA:
3988 case ATA_PROT_ATAPI_DMA:
3989 case ATA_PROT_ATAPI:
3990 /* check status of DMA engine */
3991 host_stat = ap->ops->bmdma_status(ap);
3992 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
3993
3994 /* if it's not our irq... */
3995 if (!(host_stat & ATA_DMA_INTR))
3996 goto idle_irq;
3997
3998 /* before we do anything else, clear DMA-Start bit */
Alan Coxb73fc892005-08-26 16:03:19 +01003999 ap->ops->bmdma_stop(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000
4001 /* fall through */
4002
4003 case ATA_PROT_ATAPI_NODATA:
4004 case ATA_PROT_NODATA:
4005 /* check altstatus */
4006 status = ata_altstatus(ap);
4007 if (status & ATA_BUSY)
4008 goto idle_irq;
4009
4010 /* check main status, clearing INTRQ */
4011 status = ata_chk_status(ap);
4012 if (unlikely(status & ATA_BUSY))
4013 goto idle_irq;
4014 DPRINTK("ata%u: protocol %d (dev_stat 0x%X)\n",
4015 ap->id, qc->tf.protocol, status);
4016
4017 /* ack bmdma irq events */
4018 ap->ops->irq_clear(ap);
4019
4020 /* complete taskfile transaction */
Albert Leea22e2eb2005-12-05 15:38:02 +08004021 qc->err_mask |= ac_err_mask(status);
4022 ata_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023 break;
4024
4025 default:
4026 goto idle_irq;
4027 }
4028
4029 return 1; /* irq handled */
4030
4031idle_irq:
4032 ap->stats.idle_irq++;
4033
4034#ifdef ATA_IRQ_TRAP
4035 if ((ap->stats.idle_irq % 1000) == 0) {
4036 handled = 1;
4037 ata_irq_ack(ap, 0); /* debug trap */
4038 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
4039 }
4040#endif
4041 return 0; /* irq not handled */
4042}
4043
4044/**
4045 * ata_interrupt - Default ATA host interrupt handler
Jeff Garzik0cba6322005-05-30 19:49:12 -04004046 * @irq: irq line (unused)
4047 * @dev_instance: pointer to our ata_host_set information structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048 * @regs: unused
4049 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004050 * Default interrupt handler for PCI IDE devices. Calls
4051 * ata_host_intr() for each port that is not disabled.
4052 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004054 * Obtains host_set lock during operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055 *
4056 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004057 * IRQ_NONE or IRQ_HANDLED.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058 */
4059
4060irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
4061{
4062 struct ata_host_set *host_set = dev_instance;
4063 unsigned int i;
4064 unsigned int handled = 0;
4065 unsigned long flags;
4066
4067 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
4068 spin_lock_irqsave(&host_set->lock, flags);
4069
4070 for (i = 0; i < host_set->n_ports; i++) {
4071 struct ata_port *ap;
4072
4073 ap = host_set->ports[i];
Tejun Heoc1389502005-08-22 14:59:24 +09004074 if (ap &&
4075 !(ap->flags & (ATA_FLAG_PORT_DISABLED | ATA_FLAG_NOINTR))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076 struct ata_queued_cmd *qc;
4077
4078 qc = ata_qc_from_tag(ap, ap->active_tag);
Albert Lee21b1ed72005-04-29 17:34:59 +08004079 if (qc && (!(qc->tf.ctl & ATA_NIEN)) &&
4080 (qc->flags & ATA_QCFLAG_ACTIVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081 handled |= ata_host_intr(ap, qc);
4082 }
4083 }
4084
4085 spin_unlock_irqrestore(&host_set->lock, flags);
4086
4087 return IRQ_RETVAL(handled);
4088}
4089
4090/**
4091 * atapi_packet_task - Write CDB bytes to hardware
4092 * @_data: Port to which ATAPI device is attached.
4093 *
4094 * When device has indicated its readiness to accept
4095 * a CDB, this function is called. Send the CDB.
4096 * If DMA is to be performed, exit immediately.
4097 * Otherwise, we are in polling mode, so poll
4098 * status under operation succeeds or fails.
4099 *
4100 * LOCKING:
4101 * Kernel thread context (may sleep)
4102 */
4103
4104static void atapi_packet_task(void *_data)
4105{
4106 struct ata_port *ap = _data;
4107 struct ata_queued_cmd *qc;
4108 u8 status;
4109
4110 qc = ata_qc_from_tag(ap, ap->active_tag);
4111 assert(qc != NULL);
4112 assert(qc->flags & ATA_QCFLAG_ACTIVE);
4113
4114 /* sleep-wait for BSY to clear */
4115 DPRINTK("busy wait\n");
Albert Leed8fe4522005-12-05 15:42:17 +08004116 if (ata_busy_sleep(ap, ATA_TMOUT_CDB_QUICK, ATA_TMOUT_CDB)) {
4117 qc->err_mask |= AC_ERR_ATA_BUS;
4118 goto err_out;
4119 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120
4121 /* make sure DRQ is set */
4122 status = ata_chk_status(ap);
Albert Leed8fe4522005-12-05 15:42:17 +08004123 if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ) {
4124 qc->err_mask |= AC_ERR_ATA_BUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125 goto err_out;
Albert Leed8fe4522005-12-05 15:42:17 +08004126 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127
4128 /* send SCSI cdb */
4129 DPRINTK("send cdb\n");
4130 assert(ap->cdb_len >= 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131
Tejun Heoc1389502005-08-22 14:59:24 +09004132 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA ||
4133 qc->tf.protocol == ATA_PROT_ATAPI_NODATA) {
4134 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135
Tejun Heoc1389502005-08-22 14:59:24 +09004136 /* Once we're done issuing command and kicking bmdma,
4137 * irq handler takes over. To not lose irq, we need
4138 * to clear NOINTR flag before sending cdb, but
4139 * interrupt handler shouldn't be invoked before we're
4140 * finished. Hence, the following locking.
4141 */
4142 spin_lock_irqsave(&ap->host_set->lock, flags);
4143 ap->flags &= ~ATA_FLAG_NOINTR;
4144 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
4145 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA)
4146 ap->ops->bmdma_start(qc); /* initiate bmdma */
4147 spin_unlock_irqrestore(&ap->host_set->lock, flags);
4148 } else {
4149 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150
Tejun Heoc1389502005-08-22 14:59:24 +09004151 /* PIO commands are handled by polling */
Albert Lee14be71f2005-09-27 17:36:35 +08004152 ap->hsm_task_state = HSM_ST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 queue_work(ata_wq, &ap->pio_task);
4154 }
4155
4156 return;
4157
4158err_out:
Albert Leea22e2eb2005-12-05 15:38:02 +08004159 ata_poll_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160}
4161
Edward Falk0baab862005-06-02 18:17:13 -04004162
4163/**
4164 * ata_port_start - Set port up for dma.
4165 * @ap: Port to initialize
4166 *
4167 * Called just after data structures for each port are
4168 * initialized. Allocates space for PRD table.
4169 *
4170 * May be used as the port_start() entry in ata_port_operations.
4171 *
4172 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004173 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04004174 */
4175
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176int ata_port_start (struct ata_port *ap)
4177{
4178 struct device *dev = ap->host_set->dev;
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004179 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180
4181 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
4182 if (!ap->prd)
4183 return -ENOMEM;
4184
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004185 rc = ata_pad_alloc(ap, dev);
4186 if (rc) {
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004187 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004188 return rc;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004189 }
4190
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
4192
4193 return 0;
4194}
4195
Edward Falk0baab862005-06-02 18:17:13 -04004196
4197/**
4198 * ata_port_stop - Undo ata_port_start()
4199 * @ap: Port to shut down
4200 *
4201 * Frees the PRD table.
4202 *
4203 * May be used as the port_stop() entry in ata_port_operations.
4204 *
4205 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004206 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04004207 */
4208
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209void ata_port_stop (struct ata_port *ap)
4210{
4211 struct device *dev = ap->host_set->dev;
4212
4213 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004214 ata_pad_free(ap, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215}
4216
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04004217void ata_host_stop (struct ata_host_set *host_set)
4218{
4219 if (host_set->mmio_base)
4220 iounmap(host_set->mmio_base);
4221}
4222
4223
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224/**
4225 * ata_host_remove - Unregister SCSI host structure with upper layers
4226 * @ap: Port to unregister
4227 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
4228 *
4229 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004230 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231 */
4232
4233static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
4234{
4235 struct Scsi_Host *sh = ap->host;
4236
4237 DPRINTK("ENTER\n");
4238
4239 if (do_unregister)
4240 scsi_remove_host(sh);
4241
4242 ap->ops->port_stop(ap);
4243}
4244
4245/**
4246 * ata_host_init - Initialize an ata_port structure
4247 * @ap: Structure to initialize
4248 * @host: associated SCSI mid-layer structure
4249 * @host_set: Collection of hosts to which @ap belongs
4250 * @ent: Probe information provided by low-level driver
4251 * @port_no: Port number associated with this ata_port
4252 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004253 * Initialize a new ata_port structure, and its associated
4254 * scsi_host.
4255 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004257 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258 */
4259
4260static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
4261 struct ata_host_set *host_set,
Jeff Garzik057ace52005-10-22 14:27:05 -04004262 const struct ata_probe_ent *ent, unsigned int port_no)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263{
4264 unsigned int i;
4265
4266 host->max_id = 16;
4267 host->max_lun = 1;
4268 host->max_channel = 1;
4269 host->unique_id = ata_unique_id++;
4270 host->max_cmd_len = 12;
Christoph Hellwig12413192005-06-11 01:05:01 +02004271
Linus Torvalds1da177e2005-04-16 15:20:36 -07004272 ap->flags = ATA_FLAG_PORT_DISABLED;
4273 ap->id = host->unique_id;
4274 ap->host = host;
4275 ap->ctl = ATA_DEVCTL_OBS;
4276 ap->host_set = host_set;
4277 ap->port_no = port_no;
4278 ap->hard_port_no =
4279 ent->legacy_mode ? ent->hard_port_no : port_no;
4280 ap->pio_mask = ent->pio_mask;
4281 ap->mwdma_mask = ent->mwdma_mask;
4282 ap->udma_mask = ent->udma_mask;
4283 ap->flags |= ent->host_flags;
4284 ap->ops = ent->port_ops;
4285 ap->cbl = ATA_CBL_NONE;
4286 ap->active_tag = ATA_TAG_POISON;
4287 ap->last_ctl = 0xFF;
4288
4289 INIT_WORK(&ap->packet_task, atapi_packet_task, ap);
4290 INIT_WORK(&ap->pio_task, ata_pio_task, ap);
4291
4292 for (i = 0; i < ATA_MAX_DEVICES; i++)
4293 ap->device[i].devno = i;
4294
4295#ifdef ATA_IRQ_TRAP
4296 ap->stats.unhandled_irq = 1;
4297 ap->stats.idle_irq = 1;
4298#endif
4299
4300 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
4301}
4302
4303/**
4304 * ata_host_add - Attach low-level ATA driver to system
4305 * @ent: Information provided by low-level driver
4306 * @host_set: Collections of ports to which we add
4307 * @port_no: Port number associated with this host
4308 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004309 * Attach low-level ATA driver to system.
4310 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004311 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004312 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004313 *
4314 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004315 * New ata_port on success, for NULL on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316 */
4317
Jeff Garzik057ace52005-10-22 14:27:05 -04004318static struct ata_port * ata_host_add(const struct ata_probe_ent *ent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319 struct ata_host_set *host_set,
4320 unsigned int port_no)
4321{
4322 struct Scsi_Host *host;
4323 struct ata_port *ap;
4324 int rc;
4325
4326 DPRINTK("ENTER\n");
4327 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
4328 if (!host)
4329 return NULL;
4330
4331 ap = (struct ata_port *) &host->hostdata[0];
4332
4333 ata_host_init(ap, host, host_set, ent, port_no);
4334
4335 rc = ap->ops->port_start(ap);
4336 if (rc)
4337 goto err_out;
4338
4339 return ap;
4340
4341err_out:
4342 scsi_host_put(host);
4343 return NULL;
4344}
4345
4346/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004347 * ata_device_add - Register hardware device with ATA and SCSI layers
4348 * @ent: Probe information describing hardware device to be registered
4349 *
4350 * This function processes the information provided in the probe
4351 * information struct @ent, allocates the necessary ATA and SCSI
4352 * host information structures, initializes them, and registers
4353 * everything with requisite kernel subsystems.
4354 *
4355 * This function requests irqs, probes the ATA bus, and probes
4356 * the SCSI bus.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357 *
4358 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004359 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360 *
4361 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004362 * Number of ports registered. Zero on error (no ports registered).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363 */
4364
Jeff Garzik057ace52005-10-22 14:27:05 -04004365int ata_device_add(const struct ata_probe_ent *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366{
4367 unsigned int count = 0, i;
4368 struct device *dev = ent->dev;
4369 struct ata_host_set *host_set;
4370
4371 DPRINTK("ENTER\n");
4372 /* alloc a container for our list of ATA ports (buses) */
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004373 host_set = kzalloc(sizeof(struct ata_host_set) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
4375 if (!host_set)
4376 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377 spin_lock_init(&host_set->lock);
4378
4379 host_set->dev = dev;
4380 host_set->n_ports = ent->n_ports;
4381 host_set->irq = ent->irq;
4382 host_set->mmio_base = ent->mmio_base;
4383 host_set->private_data = ent->private_data;
4384 host_set->ops = ent->port_ops;
4385
4386 /* register each port bound to this device */
4387 for (i = 0; i < ent->n_ports; i++) {
4388 struct ata_port *ap;
4389 unsigned long xfer_mode_mask;
4390
4391 ap = ata_host_add(ent, host_set, i);
4392 if (!ap)
4393 goto err_out;
4394
4395 host_set->ports[i] = ap;
4396 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
4397 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
4398 (ap->pio_mask << ATA_SHIFT_PIO);
4399
4400 /* print per-port info to dmesg */
4401 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
4402 "bmdma 0x%lX irq %lu\n",
4403 ap->id,
4404 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
4405 ata_mode_string(xfer_mode_mask),
4406 ap->ioaddr.cmd_addr,
4407 ap->ioaddr.ctl_addr,
4408 ap->ioaddr.bmdma_addr,
4409 ent->irq);
4410
4411 ata_chk_status(ap);
4412 host_set->ops->irq_clear(ap);
4413 count++;
4414 }
4415
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004416 if (!count)
4417 goto err_free_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418
4419 /* obtain irq, that is shared between channels */
4420 if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
4421 DRV_NAME, host_set))
4422 goto err_out;
4423
4424 /* perform each probe synchronously */
4425 DPRINTK("probe begin\n");
4426 for (i = 0; i < count; i++) {
4427 struct ata_port *ap;
4428 int rc;
4429
4430 ap = host_set->ports[i];
4431
4432 DPRINTK("ata%u: probe begin\n", ap->id);
4433 rc = ata_bus_probe(ap);
4434 DPRINTK("ata%u: probe end\n", ap->id);
4435
4436 if (rc) {
4437 /* FIXME: do something useful here?
4438 * Current libata behavior will
4439 * tear down everything when
4440 * the module is removed
4441 * or the h/w is unplugged.
4442 */
4443 }
4444
4445 rc = scsi_add_host(ap->host, dev);
4446 if (rc) {
4447 printk(KERN_ERR "ata%u: scsi_add_host failed\n",
4448 ap->id);
4449 /* FIXME: do something useful here */
4450 /* FIXME: handle unconditional calls to
4451 * scsi_scan_host and ata_host_remove, below,
4452 * at the very least
4453 */
4454 }
4455 }
4456
4457 /* probes are done, now scan each port's disk(s) */
4458 DPRINTK("probe begin\n");
4459 for (i = 0; i < count; i++) {
4460 struct ata_port *ap = host_set->ports[i];
4461
Jeff Garzik644dd0c2005-10-03 15:55:19 -04004462 ata_scsi_scan_host(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463 }
4464
4465 dev_set_drvdata(dev, host_set);
4466
4467 VPRINTK("EXIT, returning %u\n", ent->n_ports);
4468 return ent->n_ports; /* success */
4469
4470err_out:
4471 for (i = 0; i < count; i++) {
4472 ata_host_remove(host_set->ports[i], 1);
4473 scsi_host_put(host_set->ports[i]->host);
4474 }
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004475err_free_ret:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476 kfree(host_set);
4477 VPRINTK("EXIT, returning 0\n");
4478 return 0;
4479}
4480
4481/**
Alan Cox17b14452005-09-15 15:44:00 +01004482 * ata_host_set_remove - PCI layer callback for device removal
4483 * @host_set: ATA host set that was removed
4484 *
4485 * Unregister all objects associated with this host set. Free those
4486 * objects.
4487 *
4488 * LOCKING:
4489 * Inherited from calling layer (may sleep).
4490 */
4491
Alan Cox17b14452005-09-15 15:44:00 +01004492void ata_host_set_remove(struct ata_host_set *host_set)
4493{
4494 struct ata_port *ap;
4495 unsigned int i;
4496
4497 for (i = 0; i < host_set->n_ports; i++) {
4498 ap = host_set->ports[i];
4499 scsi_remove_host(ap->host);
4500 }
4501
4502 free_irq(host_set->irq, host_set);
4503
4504 for (i = 0; i < host_set->n_ports; i++) {
4505 ap = host_set->ports[i];
4506
4507 ata_scsi_release(ap->host);
4508
4509 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
4510 struct ata_ioports *ioaddr = &ap->ioaddr;
4511
4512 if (ioaddr->cmd_addr == 0x1f0)
4513 release_region(0x1f0, 8);
4514 else if (ioaddr->cmd_addr == 0x170)
4515 release_region(0x170, 8);
4516 }
4517
4518 scsi_host_put(ap->host);
4519 }
4520
4521 if (host_set->ops->host_stop)
4522 host_set->ops->host_stop(host_set);
4523
4524 kfree(host_set);
4525}
4526
4527/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528 * ata_scsi_release - SCSI layer callback hook for host unload
4529 * @host: libata host to be unloaded
4530 *
4531 * Performs all duties necessary to shut down a libata port...
4532 * Kill port kthread, disable port, and release resources.
4533 *
4534 * LOCKING:
4535 * Inherited from SCSI layer.
4536 *
4537 * RETURNS:
4538 * One.
4539 */
4540
4541int ata_scsi_release(struct Scsi_Host *host)
4542{
4543 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
4544
4545 DPRINTK("ENTER\n");
4546
4547 ap->ops->port_disable(ap);
4548 ata_host_remove(ap, 0);
4549
4550 DPRINTK("EXIT\n");
4551 return 1;
4552}
4553
4554/**
4555 * ata_std_ports - initialize ioaddr with standard port offsets.
4556 * @ioaddr: IO address structure to be initialized
Edward Falk0baab862005-06-02 18:17:13 -04004557 *
4558 * Utility function which initializes data_addr, error_addr,
4559 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
4560 * device_addr, status_addr, and command_addr to standard offsets
4561 * relative to cmd_addr.
4562 *
4563 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564 */
Edward Falk0baab862005-06-02 18:17:13 -04004565
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566void ata_std_ports(struct ata_ioports *ioaddr)
4567{
4568 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
4569 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
4570 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
4571 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
4572 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
4573 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
4574 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
4575 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
4576 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
4577 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
4578}
4579
4580static struct ata_probe_ent *
Jeff Garzik057ace52005-10-22 14:27:05 -04004581ata_probe_ent_alloc(struct device *dev, const struct ata_port_info *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582{
4583 struct ata_probe_ent *probe_ent;
4584
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004585 probe_ent = kzalloc(sizeof(*probe_ent), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586 if (!probe_ent) {
4587 printk(KERN_ERR DRV_NAME "(%s): out of memory\n",
4588 kobject_name(&(dev->kobj)));
4589 return NULL;
4590 }
4591
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592 INIT_LIST_HEAD(&probe_ent->node);
4593 probe_ent->dev = dev;
4594
4595 probe_ent->sht = port->sht;
4596 probe_ent->host_flags = port->host_flags;
4597 probe_ent->pio_mask = port->pio_mask;
4598 probe_ent->mwdma_mask = port->mwdma_mask;
4599 probe_ent->udma_mask = port->udma_mask;
4600 probe_ent->port_ops = port->port_ops;
4601
4602 return probe_ent;
4603}
4604
Edward Falk0baab862005-06-02 18:17:13 -04004605
4606
Jeff Garzik374b1872005-08-30 05:42:52 -04004607#ifdef CONFIG_PCI
4608
4609void ata_pci_host_stop (struct ata_host_set *host_set)
4610{
4611 struct pci_dev *pdev = to_pci_dev(host_set->dev);
4612
4613 pci_iounmap(pdev, host_set->mmio_base);
4614}
4615
Edward Falk0baab862005-06-02 18:17:13 -04004616/**
4617 * ata_pci_init_native_mode - Initialize native-mode driver
4618 * @pdev: pci device to be initialized
4619 * @port: array[2] of pointers to port info structures.
Alan Cox47a86592005-10-04 08:09:19 -04004620 * @ports: bitmap of ports present
Edward Falk0baab862005-06-02 18:17:13 -04004621 *
4622 * Utility function which allocates and initializes an
4623 * ata_probe_ent structure for a standard dual-port
4624 * PIO-based IDE controller. The returned ata_probe_ent
4625 * structure can be passed to ata_device_add(). The returned
4626 * ata_probe_ent structure should then be freed with kfree().
Alan Cox47a86592005-10-04 08:09:19 -04004627 *
4628 * The caller need only pass the address of the primary port, the
4629 * secondary will be deduced automatically. If the device has non
4630 * standard secondary port mappings this function can be called twice,
4631 * once for each interface.
Edward Falk0baab862005-06-02 18:17:13 -04004632 */
4633
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634struct ata_probe_ent *
Alan Cox47a86592005-10-04 08:09:19 -04004635ata_pci_init_native_mode(struct pci_dev *pdev, struct ata_port_info **port, int ports)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636{
4637 struct ata_probe_ent *probe_ent =
4638 ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
Alan Cox47a86592005-10-04 08:09:19 -04004639 int p = 0;
4640
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641 if (!probe_ent)
4642 return NULL;
4643
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644 probe_ent->irq = pdev->irq;
4645 probe_ent->irq_flags = SA_SHIRQ;
Alan Coxe99f8b52005-11-08 14:09:44 +00004646 probe_ent->private_data = port[0]->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647
Alan Cox47a86592005-10-04 08:09:19 -04004648 if (ports & ATA_PORT_PRIMARY) {
4649 probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 0);
4650 probe_ent->port[p].altstatus_addr =
4651 probe_ent->port[p].ctl_addr =
4652 pci_resource_start(pdev, 1) | ATA_PCI_CTL_OFS;
4653 probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4);
4654 ata_std_ports(&probe_ent->port[p]);
4655 p++;
4656 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657
Alan Cox47a86592005-10-04 08:09:19 -04004658 if (ports & ATA_PORT_SECONDARY) {
4659 probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 2);
4660 probe_ent->port[p].altstatus_addr =
4661 probe_ent->port[p].ctl_addr =
4662 pci_resource_start(pdev, 3) | ATA_PCI_CTL_OFS;
4663 probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4) + 8;
4664 ata_std_ports(&probe_ent->port[p]);
4665 p++;
4666 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667
Alan Cox47a86592005-10-04 08:09:19 -04004668 probe_ent->n_ports = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669 return probe_ent;
4670}
4671
Jeff Garzik0f0d5192005-10-30 06:41:29 -05004672static 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 -07004673{
Alan Cox47a86592005-10-04 08:09:19 -04004674 struct ata_probe_ent *probe_ent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675
Jeff Garzik0f0d5192005-10-30 06:41:29 -05004676 probe_ent = ata_probe_ent_alloc(pci_dev_to_dev(pdev), port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677 if (!probe_ent)
4678 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680 probe_ent->legacy_mode = 1;
Alan Cox47a86592005-10-04 08:09:19 -04004681 probe_ent->n_ports = 1;
4682 probe_ent->hard_port_no = port_num;
Alan Coxe99f8b52005-11-08 14:09:44 +00004683 probe_ent->private_data = port->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684
Alan Cox47a86592005-10-04 08:09:19 -04004685 switch(port_num)
4686 {
4687 case 0:
4688 probe_ent->irq = 14;
4689 probe_ent->port[0].cmd_addr = 0x1f0;
4690 probe_ent->port[0].altstatus_addr =
4691 probe_ent->port[0].ctl_addr = 0x3f6;
4692 break;
4693 case 1:
4694 probe_ent->irq = 15;
4695 probe_ent->port[0].cmd_addr = 0x170;
4696 probe_ent->port[0].altstatus_addr =
4697 probe_ent->port[0].ctl_addr = 0x376;
4698 break;
4699 }
4700 probe_ent->port[0].bmdma_addr = pci_resource_start(pdev, 4) + 8 * port_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701 ata_std_ports(&probe_ent->port[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702 return probe_ent;
4703}
4704
4705/**
4706 * ata_pci_init_one - Initialize/register PCI IDE host controller
4707 * @pdev: Controller to be initialized
4708 * @port_info: Information from low-level host driver
4709 * @n_ports: Number of ports attached to host controller
4710 *
Edward Falk0baab862005-06-02 18:17:13 -04004711 * This is a helper function which can be called from a driver's
4712 * xxx_init_one() probe function if the hardware uses traditional
4713 * IDE taskfile registers.
4714 *
4715 * This function calls pci_enable_device(), reserves its register
4716 * regions, sets the dma mask, enables bus master mode, and calls
4717 * ata_device_add()
4718 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719 * LOCKING:
4720 * Inherited from PCI layer (may sleep).
4721 *
4722 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004723 * Zero on success, negative on errno-based value on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724 */
4725
4726int ata_pci_init_one (struct pci_dev *pdev, struct ata_port_info **port_info,
4727 unsigned int n_ports)
4728{
Alan Cox47a86592005-10-04 08:09:19 -04004729 struct ata_probe_ent *probe_ent = NULL, *probe_ent2 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730 struct ata_port_info *port[2];
4731 u8 tmp8, mask;
4732 unsigned int legacy_mode = 0;
4733 int disable_dev_on_err = 1;
4734 int rc;
4735
4736 DPRINTK("ENTER\n");
4737
4738 port[0] = port_info[0];
4739 if (n_ports > 1)
4740 port[1] = port_info[1];
4741 else
4742 port[1] = port[0];
4743
4744 if ((port[0]->host_flags & ATA_FLAG_NO_LEGACY) == 0
4745 && (pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
Alan Cox47a86592005-10-04 08:09:19 -04004746 /* TODO: What if one channel is in native mode ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747 pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
4748 mask = (1 << 2) | (1 << 0);
4749 if ((tmp8 & mask) != mask)
4750 legacy_mode = (1 << 3);
4751 }
4752
4753 /* FIXME... */
Alan Cox47a86592005-10-04 08:09:19 -04004754 if ((!legacy_mode) && (n_ports > 2)) {
4755 printk(KERN_ERR "ata: BUG: native mode, n_ports > 2\n");
4756 n_ports = 2;
4757 /* For now */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758 }
4759
Alan Cox47a86592005-10-04 08:09:19 -04004760 /* FIXME: Really for ATA it isn't safe because the device may be
4761 multi-purpose and we want to leave it alone if it was already
4762 enabled. Secondly for shared use as Arjan says we want refcounting
4763
4764 Checking dev->is_enabled is insufficient as this is not set at
4765 boot for the primary video which is BIOS enabled
4766 */
4767
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768 rc = pci_enable_device(pdev);
4769 if (rc)
4770 return rc;
4771
4772 rc = pci_request_regions(pdev, DRV_NAME);
4773 if (rc) {
4774 disable_dev_on_err = 0;
4775 goto err_out;
4776 }
4777
Alan Cox47a86592005-10-04 08:09:19 -04004778 /* FIXME: Should use platform specific mappers for legacy port ranges */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779 if (legacy_mode) {
4780 if (!request_region(0x1f0, 8, "libata")) {
4781 struct resource *conflict, res;
4782 res.start = 0x1f0;
4783 res.end = 0x1f0 + 8 - 1;
4784 conflict = ____request_resource(&ioport_resource, &res);
4785 if (!strcmp(conflict->name, "libata"))
4786 legacy_mode |= (1 << 0);
4787 else {
4788 disable_dev_on_err = 0;
4789 printk(KERN_WARNING "ata: 0x1f0 IDE port busy\n");
4790 }
4791 } else
4792 legacy_mode |= (1 << 0);
4793
4794 if (!request_region(0x170, 8, "libata")) {
4795 struct resource *conflict, res;
4796 res.start = 0x170;
4797 res.end = 0x170 + 8 - 1;
4798 conflict = ____request_resource(&ioport_resource, &res);
4799 if (!strcmp(conflict->name, "libata"))
4800 legacy_mode |= (1 << 1);
4801 else {
4802 disable_dev_on_err = 0;
4803 printk(KERN_WARNING "ata: 0x170 IDE port busy\n");
4804 }
4805 } else
4806 legacy_mode |= (1 << 1);
4807 }
4808
4809 /* we have legacy mode, but all ports are unavailable */
4810 if (legacy_mode == (1 << 3)) {
4811 rc = -EBUSY;
4812 goto err_out_regions;
4813 }
4814
4815 rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
4816 if (rc)
4817 goto err_out_regions;
4818 rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
4819 if (rc)
4820 goto err_out_regions;
4821
4822 if (legacy_mode) {
Alan Cox47a86592005-10-04 08:09:19 -04004823 if (legacy_mode & (1 << 0))
Jeff Garzik0f0d5192005-10-30 06:41:29 -05004824 probe_ent = ata_pci_init_legacy_port(pdev, port[0], 0);
Alan Cox47a86592005-10-04 08:09:19 -04004825 if (legacy_mode & (1 << 1))
Jeff Garzik0f0d5192005-10-30 06:41:29 -05004826 probe_ent2 = ata_pci_init_legacy_port(pdev, port[1], 1);
Alan Cox47a86592005-10-04 08:09:19 -04004827 } else {
4828 if (n_ports == 2)
4829 probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY | ATA_PORT_SECONDARY);
4830 else
4831 probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY);
4832 }
4833 if (!probe_ent && !probe_ent2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834 rc = -ENOMEM;
4835 goto err_out_regions;
4836 }
4837
4838 pci_set_master(pdev);
4839
4840 /* FIXME: check ata_device_add return */
4841 if (legacy_mode) {
4842 if (legacy_mode & (1 << 0))
4843 ata_device_add(probe_ent);
4844 if (legacy_mode & (1 << 1))
4845 ata_device_add(probe_ent2);
4846 } else
4847 ata_device_add(probe_ent);
4848
4849 kfree(probe_ent);
4850 kfree(probe_ent2);
4851
4852 return 0;
4853
4854err_out_regions:
4855 if (legacy_mode & (1 << 0))
4856 release_region(0x1f0, 8);
4857 if (legacy_mode & (1 << 1))
4858 release_region(0x170, 8);
4859 pci_release_regions(pdev);
4860err_out:
4861 if (disable_dev_on_err)
4862 pci_disable_device(pdev);
4863 return rc;
4864}
4865
4866/**
4867 * ata_pci_remove_one - PCI layer callback for device removal
4868 * @pdev: PCI device that was removed
4869 *
4870 * PCI layer indicates to libata via this hook that
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004871 * hot-unplug or module unload event has occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872 * Handle this by unregistering all objects associated
4873 * with this PCI device. Free those objects. Then finally
4874 * release PCI resources and disable device.
4875 *
4876 * LOCKING:
4877 * Inherited from PCI layer (may sleep).
4878 */
4879
4880void ata_pci_remove_one (struct pci_dev *pdev)
4881{
4882 struct device *dev = pci_dev_to_dev(pdev);
4883 struct ata_host_set *host_set = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884
Alan Cox17b14452005-09-15 15:44:00 +01004885 ata_host_set_remove(host_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886 pci_release_regions(pdev);
4887 pci_disable_device(pdev);
4888 dev_set_drvdata(dev, NULL);
4889}
4890
4891/* move to PCI subsystem */
Jeff Garzik057ace52005-10-22 14:27:05 -04004892int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893{
4894 unsigned long tmp = 0;
4895
4896 switch (bits->width) {
4897 case 1: {
4898 u8 tmp8 = 0;
4899 pci_read_config_byte(pdev, bits->reg, &tmp8);
4900 tmp = tmp8;
4901 break;
4902 }
4903 case 2: {
4904 u16 tmp16 = 0;
4905 pci_read_config_word(pdev, bits->reg, &tmp16);
4906 tmp = tmp16;
4907 break;
4908 }
4909 case 4: {
4910 u32 tmp32 = 0;
4911 pci_read_config_dword(pdev, bits->reg, &tmp32);
4912 tmp = tmp32;
4913 break;
4914 }
4915
4916 default:
4917 return -EINVAL;
4918 }
4919
4920 tmp &= bits->mask;
4921
4922 return (tmp == bits->val) ? 1 : 0;
4923}
4924#endif /* CONFIG_PCI */
4925
4926
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927static int __init ata_init(void)
4928{
4929 ata_wq = create_workqueue("ata");
4930 if (!ata_wq)
4931 return -ENOMEM;
4932
4933 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
4934 return 0;
4935}
4936
4937static void __exit ata_exit(void)
4938{
4939 destroy_workqueue(ata_wq);
4940}
4941
4942module_init(ata_init);
4943module_exit(ata_exit);
4944
Jeff Garzik67846b32005-10-05 02:58:32 -04004945static unsigned long ratelimit_time;
4946static spinlock_t ata_ratelimit_lock = SPIN_LOCK_UNLOCKED;
4947
4948int ata_ratelimit(void)
4949{
4950 int rc;
4951 unsigned long flags;
4952
4953 spin_lock_irqsave(&ata_ratelimit_lock, flags);
4954
4955 if (time_after(jiffies, ratelimit_time)) {
4956 rc = 1;
4957 ratelimit_time = jiffies + (HZ/5);
4958 } else
4959 rc = 0;
4960
4961 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
4962
4963 return rc;
4964}
4965
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966/*
4967 * libata is essentially a library of internal helper functions for
4968 * low-level ATA host controller drivers. As such, the API/ABI is
4969 * likely to change as new drivers are added and updated.
4970 * Do not depend on ABI/API stability.
4971 */
4972
4973EXPORT_SYMBOL_GPL(ata_std_bios_param);
4974EXPORT_SYMBOL_GPL(ata_std_ports);
4975EXPORT_SYMBOL_GPL(ata_device_add);
Alan Cox17b14452005-09-15 15:44:00 +01004976EXPORT_SYMBOL_GPL(ata_host_set_remove);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977EXPORT_SYMBOL_GPL(ata_sg_init);
4978EXPORT_SYMBOL_GPL(ata_sg_init_one);
4979EXPORT_SYMBOL_GPL(ata_qc_complete);
4980EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
4981EXPORT_SYMBOL_GPL(ata_eng_timeout);
4982EXPORT_SYMBOL_GPL(ata_tf_load);
4983EXPORT_SYMBOL_GPL(ata_tf_read);
4984EXPORT_SYMBOL_GPL(ata_noop_dev_select);
4985EXPORT_SYMBOL_GPL(ata_std_dev_select);
4986EXPORT_SYMBOL_GPL(ata_tf_to_fis);
4987EXPORT_SYMBOL_GPL(ata_tf_from_fis);
4988EXPORT_SYMBOL_GPL(ata_check_status);
4989EXPORT_SYMBOL_GPL(ata_altstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990EXPORT_SYMBOL_GPL(ata_exec_command);
4991EXPORT_SYMBOL_GPL(ata_port_start);
4992EXPORT_SYMBOL_GPL(ata_port_stop);
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04004993EXPORT_SYMBOL_GPL(ata_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994EXPORT_SYMBOL_GPL(ata_interrupt);
4995EXPORT_SYMBOL_GPL(ata_qc_prep);
4996EXPORT_SYMBOL_GPL(ata_bmdma_setup);
4997EXPORT_SYMBOL_GPL(ata_bmdma_start);
4998EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
4999EXPORT_SYMBOL_GPL(ata_bmdma_status);
5000EXPORT_SYMBOL_GPL(ata_bmdma_stop);
5001EXPORT_SYMBOL_GPL(ata_port_probe);
5002EXPORT_SYMBOL_GPL(sata_phy_reset);
5003EXPORT_SYMBOL_GPL(__sata_phy_reset);
5004EXPORT_SYMBOL_GPL(ata_bus_reset);
5005EXPORT_SYMBOL_GPL(ata_port_disable);
Jeff Garzik67846b32005-10-05 02:58:32 -04005006EXPORT_SYMBOL_GPL(ata_ratelimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
5008EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
5009EXPORT_SYMBOL_GPL(ata_scsi_error);
5010EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
5011EXPORT_SYMBOL_GPL(ata_scsi_release);
5012EXPORT_SYMBOL_GPL(ata_host_intr);
5013EXPORT_SYMBOL_GPL(ata_dev_classify);
5014EXPORT_SYMBOL_GPL(ata_dev_id_string);
Brad Campbell6f2f3812005-05-12 15:07:47 -04005015EXPORT_SYMBOL_GPL(ata_dev_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016EXPORT_SYMBOL_GPL(ata_scsi_simulate);
5017
Alan Cox452503f2005-10-21 19:01:32 -04005018EXPORT_SYMBOL_GPL(ata_timing_compute);
5019EXPORT_SYMBOL_GPL(ata_timing_merge);
5020
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021#ifdef CONFIG_PCI
5022EXPORT_SYMBOL_GPL(pci_test_config_bits);
Jeff Garzik374b1872005-08-30 05:42:52 -04005023EXPORT_SYMBOL_GPL(ata_pci_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005024EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
5025EXPORT_SYMBOL_GPL(ata_pci_init_one);
5026EXPORT_SYMBOL_GPL(ata_pci_remove_one);
5027#endif /* CONFIG_PCI */