blob: 01070366bb19f9916604b0fc614633061edafd43 [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,
565 /* pio */
566 ATA_CMD_PIO_READ,
567 ATA_CMD_PIO_WRITE,
568 ATA_CMD_PIO_READ_EXT,
569 ATA_CMD_PIO_WRITE_EXT,
570 /* dma */
571 ATA_CMD_READ,
572 ATA_CMD_WRITE,
573 ATA_CMD_READ_EXT,
574 ATA_CMD_WRITE_EXT
575};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577/**
Albert Lee8cbd6df2005-10-12 15:06:27 +0800578 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
579 * @qc: command to examine and configure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 *
Albert Lee8cbd6df2005-10-12 15:06:27 +0800581 * Examine the device configuration and tf->flags to calculate
582 * the proper read/write commands and protocol to use.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 *
584 * LOCKING:
585 * caller.
586 */
Albert Lee8cbd6df2005-10-12 15:06:27 +0800587void ata_rwcmd_protocol(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
Albert Lee8cbd6df2005-10-12 15:06:27 +0800589 struct ata_taskfile *tf = &qc->tf;
590 struct ata_device *dev = qc->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Albert Lee8cbd6df2005-10-12 15:06:27 +0800592 int index, lba48, write;
593
594 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
595 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
Albert Lee8cbd6df2005-10-12 15:06:27 +0800597 if (dev->flags & ATA_DFLAG_PIO) {
598 tf->protocol = ATA_PROT_PIO;
599 index = dev->multi_count ? 0 : 4;
600 } else {
601 tf->protocol = ATA_PROT_DMA;
602 index = 8;
603 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
Albert Lee8cbd6df2005-10-12 15:06:27 +0800605 tf->command = ata_rw_cmds[index + lba48 + write];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606}
607
Arjan van de Ven98ac62d2005-11-28 10:06:23 +0100608static const char * const xfer_mode_str[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 "UDMA/16",
610 "UDMA/25",
611 "UDMA/33",
612 "UDMA/44",
613 "UDMA/66",
614 "UDMA/100",
615 "UDMA/133",
616 "UDMA7",
617 "MWDMA0",
618 "MWDMA1",
619 "MWDMA2",
620 "PIO0",
621 "PIO1",
622 "PIO2",
623 "PIO3",
624 "PIO4",
625};
626
627/**
628 * ata_udma_string - convert UDMA bit offset to string
629 * @mask: mask of bits supported; only highest bit counts.
630 *
631 * Determine string which represents the highest speed
632 * (highest bit in @udma_mask).
633 *
634 * LOCKING:
635 * None.
636 *
637 * RETURNS:
638 * Constant C string representing highest speed listed in
639 * @udma_mask, or the constant C string "<n/a>".
640 */
641
642static const char *ata_mode_string(unsigned int mask)
643{
644 int i;
645
646 for (i = 7; i >= 0; i--)
647 if (mask & (1 << i))
648 goto out;
649 for (i = ATA_SHIFT_MWDMA + 2; i >= ATA_SHIFT_MWDMA; i--)
650 if (mask & (1 << i))
651 goto out;
652 for (i = ATA_SHIFT_PIO + 4; i >= ATA_SHIFT_PIO; i--)
653 if (mask & (1 << i))
654 goto out;
655
656 return "<n/a>";
657
658out:
659 return xfer_mode_str[i];
660}
661
662/**
663 * ata_pio_devchk - PATA device presence detection
664 * @ap: ATA channel to examine
665 * @device: Device to examine (starting at zero)
666 *
667 * This technique was originally described in
668 * Hale Landis's ATADRVR (www.ata-atapi.com), and
669 * later found its way into the ATA/ATAPI spec.
670 *
671 * Write a pattern to the ATA shadow registers,
672 * and if a device is present, it will respond by
673 * correctly storing and echoing back the
674 * ATA shadow register contents.
675 *
676 * LOCKING:
677 * caller.
678 */
679
680static unsigned int ata_pio_devchk(struct ata_port *ap,
681 unsigned int device)
682{
683 struct ata_ioports *ioaddr = &ap->ioaddr;
684 u8 nsect, lbal;
685
686 ap->ops->dev_select(ap, device);
687
688 outb(0x55, ioaddr->nsect_addr);
689 outb(0xaa, ioaddr->lbal_addr);
690
691 outb(0xaa, ioaddr->nsect_addr);
692 outb(0x55, ioaddr->lbal_addr);
693
694 outb(0x55, ioaddr->nsect_addr);
695 outb(0xaa, ioaddr->lbal_addr);
696
697 nsect = inb(ioaddr->nsect_addr);
698 lbal = inb(ioaddr->lbal_addr);
699
700 if ((nsect == 0x55) && (lbal == 0xaa))
701 return 1; /* we found a device */
702
703 return 0; /* nothing found */
704}
705
706/**
707 * ata_mmio_devchk - PATA device presence detection
708 * @ap: ATA channel to examine
709 * @device: Device to examine (starting at zero)
710 *
711 * This technique was originally described in
712 * Hale Landis's ATADRVR (www.ata-atapi.com), and
713 * later found its way into the ATA/ATAPI spec.
714 *
715 * Write a pattern to the ATA shadow registers,
716 * and if a device is present, it will respond by
717 * correctly storing and echoing back the
718 * ATA shadow register contents.
719 *
720 * LOCKING:
721 * caller.
722 */
723
724static unsigned int ata_mmio_devchk(struct ata_port *ap,
725 unsigned int device)
726{
727 struct ata_ioports *ioaddr = &ap->ioaddr;
728 u8 nsect, lbal;
729
730 ap->ops->dev_select(ap, device);
731
732 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
733 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
734
735 writeb(0xaa, (void __iomem *) ioaddr->nsect_addr);
736 writeb(0x55, (void __iomem *) ioaddr->lbal_addr);
737
738 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
739 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
740
741 nsect = readb((void __iomem *) ioaddr->nsect_addr);
742 lbal = readb((void __iomem *) ioaddr->lbal_addr);
743
744 if ((nsect == 0x55) && (lbal == 0xaa))
745 return 1; /* we found a device */
746
747 return 0; /* nothing found */
748}
749
750/**
751 * ata_devchk - PATA device presence detection
752 * @ap: ATA channel to examine
753 * @device: Device to examine (starting at zero)
754 *
755 * Dispatch ATA device presence detection, depending
756 * on whether we are using PIO or MMIO to talk to the
757 * ATA shadow registers.
758 *
759 * LOCKING:
760 * caller.
761 */
762
763static unsigned int ata_devchk(struct ata_port *ap,
764 unsigned int device)
765{
766 if (ap->flags & ATA_FLAG_MMIO)
767 return ata_mmio_devchk(ap, device);
768 return ata_pio_devchk(ap, device);
769}
770
771/**
772 * ata_dev_classify - determine device type based on ATA-spec signature
773 * @tf: ATA taskfile register set for device to be identified
774 *
775 * Determine from taskfile register contents whether a device is
776 * ATA or ATAPI, as per "Signature and persistence" section
777 * of ATA/PI spec (volume 1, sect 5.14).
778 *
779 * LOCKING:
780 * None.
781 *
782 * RETURNS:
783 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
784 * the event of failure.
785 */
786
Jeff Garzik057ace52005-10-22 14:27:05 -0400787unsigned int ata_dev_classify(const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788{
789 /* Apple's open source Darwin code hints that some devices only
790 * put a proper signature into the LBA mid/high registers,
791 * So, we only check those. It's sufficient for uniqueness.
792 */
793
794 if (((tf->lbam == 0) && (tf->lbah == 0)) ||
795 ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
796 DPRINTK("found ATA device by sig\n");
797 return ATA_DEV_ATA;
798 }
799
800 if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
801 ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
802 DPRINTK("found ATAPI device by sig\n");
803 return ATA_DEV_ATAPI;
804 }
805
806 DPRINTK("unknown device\n");
807 return ATA_DEV_UNKNOWN;
808}
809
810/**
811 * ata_dev_try_classify - Parse returned ATA device signature
812 * @ap: ATA channel to examine
813 * @device: Device to examine (starting at zero)
814 *
815 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
816 * an ATA/ATAPI-defined set of values is placed in the ATA
817 * shadow registers, indicating the results of device detection
818 * and diagnostics.
819 *
820 * Select the ATA device, and read the values from the ATA shadow
821 * registers. Then parse according to the Error register value,
822 * and the spec-defined values examined by ata_dev_classify().
823 *
824 * LOCKING:
825 * caller.
826 */
827
828static u8 ata_dev_try_classify(struct ata_port *ap, unsigned int device)
829{
830 struct ata_device *dev = &ap->device[device];
831 struct ata_taskfile tf;
832 unsigned int class;
833 u8 err;
834
835 ap->ops->dev_select(ap, device);
836
837 memset(&tf, 0, sizeof(tf));
838
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 ap->ops->tf_read(ap, &tf);
Jeff Garzik0169e282005-10-29 21:25:10 -0400840 err = tf.feature;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
842 dev->class = ATA_DEV_NONE;
843
844 /* see if device passed diags */
845 if (err == 1)
846 /* do nothing */ ;
847 else if ((device == 0) && (err == 0x81))
848 /* do nothing */ ;
849 else
850 return err;
851
852 /* determine if device if ATA or ATAPI */
853 class = ata_dev_classify(&tf);
854 if (class == ATA_DEV_UNKNOWN)
855 return err;
856 if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
857 return err;
858
859 dev->class = class;
860
861 return err;
862}
863
864/**
865 * ata_dev_id_string - Convert IDENTIFY DEVICE page into string
866 * @id: IDENTIFY DEVICE results we will examine
867 * @s: string into which data is output
868 * @ofs: offset into identify device page
869 * @len: length of string to return. must be an even number.
870 *
871 * The strings in the IDENTIFY DEVICE page are broken up into
872 * 16-bit chunks. Run through the string, and output each
873 * 8-bit chunk linearly, regardless of platform.
874 *
875 * LOCKING:
876 * caller.
877 */
878
Jeff Garzik057ace52005-10-22 14:27:05 -0400879void ata_dev_id_string(const u16 *id, unsigned char *s,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 unsigned int ofs, unsigned int len)
881{
882 unsigned int c;
883
884 while (len > 0) {
885 c = id[ofs] >> 8;
886 *s = c;
887 s++;
888
889 c = id[ofs] & 0xff;
890 *s = c;
891 s++;
892
893 ofs++;
894 len -= 2;
895 }
896}
897
Edward Falk0baab862005-06-02 18:17:13 -0400898
899/**
900 * ata_noop_dev_select - Select device 0/1 on ATA bus
901 * @ap: ATA channel to manipulate
902 * @device: ATA device (numbered from zero) to select
903 *
904 * This function performs no actual function.
905 *
906 * May be used as the dev_select() entry in ata_port_operations.
907 *
908 * LOCKING:
909 * caller.
910 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
912{
913}
914
Edward Falk0baab862005-06-02 18:17:13 -0400915
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916/**
917 * ata_std_dev_select - Select device 0/1 on ATA bus
918 * @ap: ATA channel to manipulate
919 * @device: ATA device (numbered from zero) to select
920 *
921 * Use the method defined in the ATA specification to
922 * make either device 0, or device 1, active on the
Edward Falk0baab862005-06-02 18:17:13 -0400923 * ATA channel. Works with both PIO and MMIO.
924 *
925 * May be used as the dev_select() entry in ata_port_operations.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 *
927 * LOCKING:
928 * caller.
929 */
930
931void ata_std_dev_select (struct ata_port *ap, unsigned int device)
932{
933 u8 tmp;
934
935 if (device == 0)
936 tmp = ATA_DEVICE_OBS;
937 else
938 tmp = ATA_DEVICE_OBS | ATA_DEV1;
939
940 if (ap->flags & ATA_FLAG_MMIO) {
941 writeb(tmp, (void __iomem *) ap->ioaddr.device_addr);
942 } else {
943 outb(tmp, ap->ioaddr.device_addr);
944 }
945 ata_pause(ap); /* needed; also flushes, for mmio */
946}
947
948/**
949 * ata_dev_select - Select device 0/1 on ATA bus
950 * @ap: ATA channel to manipulate
951 * @device: ATA device (numbered from zero) to select
952 * @wait: non-zero to wait for Status register BSY bit to clear
953 * @can_sleep: non-zero if context allows sleeping
954 *
955 * Use the method defined in the ATA specification to
956 * make either device 0, or device 1, active on the
957 * ATA channel.
958 *
959 * This is a high-level version of ata_std_dev_select(),
960 * which additionally provides the services of inserting
961 * the proper pauses and status polling, where needed.
962 *
963 * LOCKING:
964 * caller.
965 */
966
967void ata_dev_select(struct ata_port *ap, unsigned int device,
968 unsigned int wait, unsigned int can_sleep)
969{
970 VPRINTK("ENTER, ata%u: device %u, wait %u\n",
971 ap->id, device, wait);
972
973 if (wait)
974 ata_wait_idle(ap);
975
976 ap->ops->dev_select(ap, device);
977
978 if (wait) {
979 if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
980 msleep(150);
981 ata_wait_idle(ap);
982 }
983}
984
985/**
986 * ata_dump_id - IDENTIFY DEVICE info debugging output
987 * @dev: Device whose IDENTIFY DEVICE page we will dump
988 *
989 * Dump selected 16-bit words from a detected device's
990 * IDENTIFY PAGE page.
991 *
992 * LOCKING:
993 * caller.
994 */
995
Jeff Garzik057ace52005-10-22 14:27:05 -0400996static inline void ata_dump_id(const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997{
998 DPRINTK("49==0x%04x "
999 "53==0x%04x "
1000 "63==0x%04x "
1001 "64==0x%04x "
1002 "75==0x%04x \n",
1003 dev->id[49],
1004 dev->id[53],
1005 dev->id[63],
1006 dev->id[64],
1007 dev->id[75]);
1008 DPRINTK("80==0x%04x "
1009 "81==0x%04x "
1010 "82==0x%04x "
1011 "83==0x%04x "
1012 "84==0x%04x \n",
1013 dev->id[80],
1014 dev->id[81],
1015 dev->id[82],
1016 dev->id[83],
1017 dev->id[84]);
1018 DPRINTK("88==0x%04x "
1019 "93==0x%04x\n",
1020 dev->id[88],
1021 dev->id[93]);
1022}
1023
Alan Cox11e29e22005-10-21 18:46:32 -04001024/*
1025 * Compute the PIO modes available for this device. This is not as
1026 * trivial as it seems if we must consider early devices correctly.
1027 *
1028 * FIXME: pre IDE drive timing (do we care ?).
1029 */
1030
Jeff Garzik057ace52005-10-22 14:27:05 -04001031static unsigned int ata_pio_modes(const struct ata_device *adev)
Alan Cox11e29e22005-10-21 18:46:32 -04001032{
1033 u16 modes;
1034
1035 /* Usual case. Word 53 indicates word 88 is valid */
1036 if (adev->id[ATA_ID_FIELD_VALID] & (1 << 2)) {
1037 modes = adev->id[ATA_ID_PIO_MODES] & 0x03;
1038 modes <<= 3;
1039 modes |= 0x7;
1040 return modes;
1041 }
1042
1043 /* If word 88 isn't valid then Word 51 holds the PIO timing number
1044 for the maximum. Turn it into a mask and return it */
1045 modes = (2 << (adev->id[ATA_ID_OLD_PIO_MODES] & 0xFF)) - 1 ;
1046 return modes;
1047}
1048
Tejun Heoa2a7a662005-12-13 14:48:31 +09001049struct ata_exec_internal_arg {
1050 unsigned int err_mask;
1051 struct ata_taskfile *tf;
1052 struct completion *waiting;
1053};
1054
1055int ata_qc_complete_internal(struct ata_queued_cmd *qc)
1056{
1057 struct ata_exec_internal_arg *arg = qc->private_data;
1058 struct completion *waiting = arg->waiting;
1059
1060 if (!(qc->err_mask & ~AC_ERR_DEV))
1061 qc->ap->ops->tf_read(qc->ap, arg->tf);
1062 arg->err_mask = qc->err_mask;
1063 arg->waiting = NULL;
1064 complete(waiting);
1065
1066 return 0;
1067}
1068
1069/**
1070 * ata_exec_internal - execute libata internal command
1071 * @ap: Port to which the command is sent
1072 * @dev: Device to which the command is sent
1073 * @tf: Taskfile registers for the command and the result
1074 * @dma_dir: Data tranfer direction of the command
1075 * @buf: Data buffer of the command
1076 * @buflen: Length of data buffer
1077 *
1078 * Executes libata internal command with timeout. @tf contains
1079 * command on entry and result on return. Timeout and error
1080 * conditions are reported via return value. No recovery action
1081 * is taken after a command times out. It's caller's duty to
1082 * clean up after timeout.
1083 *
1084 * LOCKING:
1085 * None. Should be called with kernel context, might sleep.
1086 */
1087
1088static unsigned
1089ata_exec_internal(struct ata_port *ap, struct ata_device *dev,
1090 struct ata_taskfile *tf,
1091 int dma_dir, void *buf, unsigned int buflen)
1092{
1093 u8 command = tf->command;
1094 struct ata_queued_cmd *qc;
1095 DECLARE_COMPLETION(wait);
1096 unsigned long flags;
1097 struct ata_exec_internal_arg arg;
1098
1099 spin_lock_irqsave(&ap->host_set->lock, flags);
1100
1101 qc = ata_qc_new_init(ap, dev);
1102 BUG_ON(qc == NULL);
1103
1104 qc->tf = *tf;
1105 qc->dma_dir = dma_dir;
1106 if (dma_dir != DMA_NONE) {
1107 ata_sg_init_one(qc, buf, buflen);
1108 qc->nsect = buflen / ATA_SECT_SIZE;
1109 }
1110
1111 arg.waiting = &wait;
1112 arg.tf = tf;
1113 qc->private_data = &arg;
1114 qc->complete_fn = ata_qc_complete_internal;
1115
1116 if (ata_qc_issue(qc))
1117 goto issue_fail;
1118
1119 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1120
1121 if (!wait_for_completion_timeout(&wait, ATA_TMOUT_INTERNAL)) {
1122 spin_lock_irqsave(&ap->host_set->lock, flags);
1123
1124 /* We're racing with irq here. If we lose, the
1125 * following test prevents us from completing the qc
1126 * again. If completion irq occurs after here but
1127 * before the caller cleans up, it will result in a
1128 * spurious interrupt. We can live with that.
1129 */
1130 if (arg.waiting) {
1131 qc->err_mask = AC_ERR_OTHER;
1132 ata_qc_complete(qc);
1133 printk(KERN_WARNING "ata%u: qc timeout (cmd 0x%x)\n",
1134 ap->id, command);
1135 }
1136
1137 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1138 }
1139
1140 return arg.err_mask;
1141
1142 issue_fail:
1143 ata_qc_free(qc);
1144 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1145 return AC_ERR_OTHER;
1146}
1147
Jeff Garzik64f043d2005-11-17 10:50:01 -05001148static int ata_qc_wait_err(struct ata_queued_cmd *qc,
1149 struct completion *wait)
1150{
1151 int rc = 0;
1152
1153 if (wait_for_completion_timeout(wait, 30 * HZ) < 1) {
1154 /* timeout handling */
Albert Leea22e2eb2005-12-05 15:38:02 +08001155 qc->err_mask |= ac_err_mask(ata_chk_status(qc->ap));
Jeff Garzik64f043d2005-11-17 10:50:01 -05001156
Albert Leea22e2eb2005-12-05 15:38:02 +08001157 if (!qc->err_mask) {
Jeff Garzik64f043d2005-11-17 10:50:01 -05001158 printk(KERN_WARNING "ata%u: slow completion (cmd %x)\n",
1159 qc->ap->id, qc->tf.command);
1160 } else {
1161 printk(KERN_WARNING "ata%u: qc timeout (cmd %x)\n",
1162 qc->ap->id, qc->tf.command);
1163 rc = -EIO;
1164 }
1165
Albert Leea22e2eb2005-12-05 15:38:02 +08001166 ata_qc_complete(qc);
Jeff Garzik64f043d2005-11-17 10:50:01 -05001167 }
1168
1169 return rc;
1170}
1171
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172/**
1173 * ata_dev_identify - obtain IDENTIFY x DEVICE page
1174 * @ap: port on which device we wish to probe resides
1175 * @device: device bus address, starting at zero
1176 *
1177 * Following bus reset, we issue the IDENTIFY [PACKET] DEVICE
1178 * command, and read back the 512-byte device information page.
1179 * The device information page is fed to us via the standard
1180 * PIO-IN protocol, but we hand-code it here. (TODO: investigate
1181 * using standard PIO-IN paths)
1182 *
1183 * After reading the device information page, we use several
1184 * bits of information from it to initialize data structures
1185 * that will be used during the lifetime of the ata_device.
1186 * Other data from the info page is used to disqualify certain
1187 * older ATA devices we do not wish to support.
1188 *
1189 * LOCKING:
1190 * Inherited from caller. Some functions called by this function
1191 * obtain the host_set lock.
1192 */
1193
1194static void ata_dev_identify(struct ata_port *ap, unsigned int device)
1195{
1196 struct ata_device *dev = &ap->device[device];
Albert Lee8bf62ec2005-05-12 15:29:42 -04001197 unsigned int major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 u16 tmp;
1199 unsigned long xfer_modes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 unsigned int using_edd;
Tejun Heoa0123702005-12-13 14:49:31 +09001201 struct ata_taskfile tf;
1202 unsigned int err_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 int rc;
1204
1205 if (!ata_dev_present(dev)) {
1206 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
1207 ap->id, device);
1208 return;
1209 }
1210
1211 if (ap->flags & (ATA_FLAG_SRST | ATA_FLAG_SATA_RESET))
1212 using_edd = 0;
1213 else
1214 using_edd = 1;
1215
1216 DPRINTK("ENTER, host %u, dev %u\n", ap->id, device);
1217
1218 assert (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ATAPI ||
1219 dev->class == ATA_DEV_NONE);
1220
1221 ata_dev_select(ap, device, 1, 1); /* select device 0/1 */
1222
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223retry:
Tejun Heoa0123702005-12-13 14:49:31 +09001224 ata_tf_init(ap, &tf, device);
1225
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 if (dev->class == ATA_DEV_ATA) {
Tejun Heoa0123702005-12-13 14:49:31 +09001227 tf.command = ATA_CMD_ID_ATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 DPRINTK("do ATA identify\n");
1229 } else {
Tejun Heoa0123702005-12-13 14:49:31 +09001230 tf.command = ATA_CMD_ID_ATAPI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 DPRINTK("do ATAPI identify\n");
1232 }
1233
Tejun Heoa0123702005-12-13 14:49:31 +09001234 tf.protocol = ATA_PROT_PIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
Tejun Heoa0123702005-12-13 14:49:31 +09001236 err_mask = ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE,
1237 dev->id, sizeof(dev->id));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
Tejun Heoa0123702005-12-13 14:49:31 +09001239 if (err_mask) {
1240 if (err_mask & ~AC_ERR_DEV)
1241 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 /*
1244 * arg! EDD works for all test cases, but seems to return
1245 * the ATA signature for some ATAPI devices. Until the
1246 * reason for this is found and fixed, we fix up the mess
1247 * here. If IDENTIFY DEVICE returns command aborted
1248 * (as ATAPI devices do), then we issue an
1249 * IDENTIFY PACKET DEVICE.
1250 *
1251 * ATA software reset (SRST, the default) does not appear
1252 * to have this problem.
1253 */
Albert Lee7c398332005-11-09 13:03:30 +08001254 if ((using_edd) && (dev->class == ATA_DEV_ATA)) {
Tejun Heoa0123702005-12-13 14:49:31 +09001255 u8 err = tf.feature;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 if (err & ATA_ABORTED) {
1257 dev->class = ATA_DEV_ATAPI;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 goto retry;
1259 }
1260 }
1261 goto err_out;
1262 }
1263
1264 swap_buf_le16(dev->id, ATA_ID_WORDS);
1265
1266 /* print device capabilities */
1267 printk(KERN_DEBUG "ata%u: dev %u cfg "
1268 "49:%04x 82:%04x 83:%04x 84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1269 ap->id, device, dev->id[49],
1270 dev->id[82], dev->id[83], dev->id[84],
1271 dev->id[85], dev->id[86], dev->id[87],
1272 dev->id[88]);
1273
1274 /*
1275 * common ATA, ATAPI feature tests
1276 */
1277
Albert Lee8bf62ec2005-05-12 15:29:42 -04001278 /* we require DMA support (bits 8 of word 49) */
1279 if (!ata_id_has_dma(dev->id)) {
1280 printk(KERN_DEBUG "ata%u: no dma\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 goto err_out_nosup;
1282 }
1283
1284 /* quick-n-dirty find max transfer mode; for printk only */
1285 xfer_modes = dev->id[ATA_ID_UDMA_MODES];
1286 if (!xfer_modes)
1287 xfer_modes = (dev->id[ATA_ID_MWDMA_MODES]) << ATA_SHIFT_MWDMA;
Alan Cox11e29e22005-10-21 18:46:32 -04001288 if (!xfer_modes)
1289 xfer_modes = ata_pio_modes(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290
1291 ata_dump_id(dev);
1292
1293 /* ATA-specific feature tests */
1294 if (dev->class == ATA_DEV_ATA) {
1295 if (!ata_id_is_ata(dev->id)) /* sanity check */
1296 goto err_out_nosup;
1297
Albert Lee8bf62ec2005-05-12 15:29:42 -04001298 /* get major version */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 tmp = dev->id[ATA_ID_MAJOR_VER];
Albert Lee8bf62ec2005-05-12 15:29:42 -04001300 for (major_version = 14; major_version >= 1; major_version--)
1301 if (tmp & (1 << major_version))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 break;
1303
Albert Lee8bf62ec2005-05-12 15:29:42 -04001304 /*
1305 * The exact sequence expected by certain pre-ATA4 drives is:
1306 * SRST RESET
1307 * IDENTIFY
1308 * INITIALIZE DEVICE PARAMETERS
1309 * anything else..
1310 * Some drives were very specific about that exact sequence.
1311 */
Albert Lee59a10b12005-10-12 15:09:42 +08001312 if (major_version < 4 || (!ata_id_has_lba(dev->id))) {
Albert Lee8bf62ec2005-05-12 15:29:42 -04001313 ata_dev_init_params(ap, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314
Albert Lee59a10b12005-10-12 15:09:42 +08001315 /* current CHS translation info (id[53-58]) might be
1316 * changed. reread the identify device info.
1317 */
1318 ata_dev_reread_id(ap, dev);
1319 }
1320
Albert Lee8bf62ec2005-05-12 15:29:42 -04001321 if (ata_id_has_lba(dev->id)) {
1322 dev->flags |= ATA_DFLAG_LBA;
1323
1324 if (ata_id_has_lba48(dev->id)) {
1325 dev->flags |= ATA_DFLAG_LBA48;
1326 dev->n_sectors = ata_id_u64(dev->id, 100);
1327 } else {
1328 dev->n_sectors = ata_id_u32(dev->id, 60);
1329 }
1330
1331 /* print device info to dmesg */
1332 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors:%s\n",
1333 ap->id, device,
1334 major_version,
1335 ata_mode_string(xfer_modes),
1336 (unsigned long long)dev->n_sectors,
1337 dev->flags & ATA_DFLAG_LBA48 ? " LBA48" : " LBA");
1338 } else {
1339 /* CHS */
1340
1341 /* Default translation */
1342 dev->cylinders = dev->id[1];
1343 dev->heads = dev->id[3];
1344 dev->sectors = dev->id[6];
1345 dev->n_sectors = dev->cylinders * dev->heads * dev->sectors;
1346
1347 if (ata_id_current_chs_valid(dev->id)) {
1348 /* Current CHS translation is valid. */
1349 dev->cylinders = dev->id[54];
1350 dev->heads = dev->id[55];
1351 dev->sectors = dev->id[56];
1352
1353 dev->n_sectors = ata_id_u32(dev->id, 57);
1354 }
1355
1356 /* print device info to dmesg */
1357 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors: CHS %d/%d/%d\n",
1358 ap->id, device,
1359 major_version,
1360 ata_mode_string(xfer_modes),
1361 (unsigned long long)dev->n_sectors,
1362 (int)dev->cylinders, (int)dev->heads, (int)dev->sectors);
1363
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 }
1365
1366 ap->host->max_cmd_len = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 }
1368
1369 /* ATAPI-specific feature tests */
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05001370 else if (dev->class == ATA_DEV_ATAPI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 if (ata_id_is_ata(dev->id)) /* sanity check */
1372 goto err_out_nosup;
1373
1374 rc = atapi_cdb_len(dev->id);
1375 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
1376 printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
1377 goto err_out_nosup;
1378 }
1379 ap->cdb_len = (unsigned int) rc;
1380 ap->host->max_cmd_len = (unsigned char) ap->cdb_len;
1381
1382 /* print device info to dmesg */
1383 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
1384 ap->id, device,
1385 ata_mode_string(xfer_modes));
1386 }
1387
1388 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
1389 return;
1390
1391err_out_nosup:
1392 printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n",
1393 ap->id, device);
1394err_out:
1395 dev->class++; /* converts ATA_DEV_xxx into ATA_DEV_xxx_UNSUP */
1396 DPRINTK("EXIT, err\n");
1397}
1398
Brad Campbell6f2f3812005-05-12 15:07:47 -04001399
Jeff Garzik057ace52005-10-22 14:27:05 -04001400static inline u8 ata_dev_knobble(const struct ata_port *ap)
Brad Campbell6f2f3812005-05-12 15:07:47 -04001401{
1402 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(ap->device->id)));
1403}
1404
1405/**
1406 * ata_dev_config - Run device specific handlers and check for
1407 * SATA->PATA bridges
Jeff Garzik8a60a072005-07-31 13:13:24 -04001408 * @ap: Bus
Brad Campbell6f2f3812005-05-12 15:07:47 -04001409 * @i: Device
1410 *
1411 * LOCKING:
1412 */
Jeff Garzik8a60a072005-07-31 13:13:24 -04001413
Brad Campbell6f2f3812005-05-12 15:07:47 -04001414void ata_dev_config(struct ata_port *ap, unsigned int i)
1415{
1416 /* limit bridge transfers to udma5, 200 sectors */
1417 if (ata_dev_knobble(ap)) {
1418 printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
1419 ap->id, ap->device->devno);
1420 ap->udma_mask &= ATA_UDMA5;
1421 ap->host->max_sectors = ATA_MAX_SECTORS;
1422 ap->host->hostt->max_sectors = ATA_MAX_SECTORS;
1423 ap->device->flags |= ATA_DFLAG_LOCK_SECTORS;
1424 }
1425
1426 if (ap->ops->dev_config)
1427 ap->ops->dev_config(ap, &ap->device[i]);
1428}
1429
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430/**
1431 * ata_bus_probe - Reset and probe ATA bus
1432 * @ap: Bus to probe
1433 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001434 * Master ATA bus probing function. Initiates a hardware-dependent
1435 * bus reset, then attempts to identify any devices found on
1436 * the bus.
1437 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001439 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 *
1441 * RETURNS:
1442 * Zero on success, non-zero on error.
1443 */
1444
1445static int ata_bus_probe(struct ata_port *ap)
1446{
1447 unsigned int i, found = 0;
1448
1449 ap->ops->phy_reset(ap);
1450 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1451 goto err_out;
1452
1453 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1454 ata_dev_identify(ap, i);
1455 if (ata_dev_present(&ap->device[i])) {
1456 found = 1;
Brad Campbell6f2f3812005-05-12 15:07:47 -04001457 ata_dev_config(ap,i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 }
1459 }
1460
1461 if ((!found) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1462 goto err_out_disable;
1463
1464 ata_set_mode(ap);
1465 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1466 goto err_out_disable;
1467
1468 return 0;
1469
1470err_out_disable:
1471 ap->ops->port_disable(ap);
1472err_out:
1473 return -1;
1474}
1475
1476/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001477 * ata_port_probe - Mark port as enabled
1478 * @ap: Port for which we indicate enablement
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001480 * Modify @ap data structure such that the system
1481 * thinks that the entire port is enabled.
1482 *
1483 * LOCKING: host_set lock, or some other form of
1484 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485 */
1486
1487void ata_port_probe(struct ata_port *ap)
1488{
1489 ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1490}
1491
1492/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001493 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1494 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001496 * This function issues commands to standard SATA Sxxx
1497 * PHY registers, to wake up the phy (and device), and
1498 * clear any reset condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 *
1500 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001501 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 *
1503 */
1504void __sata_phy_reset(struct ata_port *ap)
1505{
1506 u32 sstatus;
1507 unsigned long timeout = jiffies + (HZ * 5);
1508
1509 if (ap->flags & ATA_FLAG_SATA_RESET) {
Brett Russcdcca892005-03-28 15:10:27 -05001510 /* issue phy wake/reset */
1511 scr_write_flush(ap, SCR_CONTROL, 0x301);
Tejun Heo62ba2842005-06-26 23:27:19 +09001512 /* Couldn't find anything in SATA I/II specs, but
1513 * AHCI-1.1 10.4.2 says at least 1 ms. */
1514 mdelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 }
Brett Russcdcca892005-03-28 15:10:27 -05001516 scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517
1518 /* wait for phy to become ready, if necessary */
1519 do {
1520 msleep(200);
1521 sstatus = scr_read(ap, SCR_STATUS);
1522 if ((sstatus & 0xf) != 1)
1523 break;
1524 } while (time_before(jiffies, timeout));
1525
1526 /* TODO: phy layer with polling, timeouts, etc. */
Jeff Garzik656563e2005-11-20 03:36:45 -05001527 sstatus = scr_read(ap, SCR_STATUS);
1528 if (sata_dev_present(ap)) {
1529 const char *speed;
1530 u32 tmp;
1531
1532 tmp = (sstatus >> 4) & 0xf;
1533 if (tmp & (1 << 0))
1534 speed = "1.5";
1535 else if (tmp & (1 << 1))
1536 speed = "3.0";
1537 else
1538 speed = "<unknown>";
1539 printk(KERN_INFO "ata%u: SATA link up %s Gbps (SStatus %X)\n",
1540 ap->id, speed, sstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 ata_port_probe(ap);
Jeff Garzik656563e2005-11-20 03:36:45 -05001542 } else {
1543 printk(KERN_INFO "ata%u: SATA link down (SStatus %X)\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 ap->id, sstatus);
1545 ata_port_disable(ap);
1546 }
1547
1548 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1549 return;
1550
1551 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1552 ata_port_disable(ap);
1553 return;
1554 }
1555
1556 ap->cbl = ATA_CBL_SATA;
1557}
1558
1559/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001560 * sata_phy_reset - Reset SATA bus.
1561 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001563 * This function resets the SATA bus, and then probes
1564 * the bus for devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 *
1566 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001567 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 *
1569 */
1570void sata_phy_reset(struct ata_port *ap)
1571{
1572 __sata_phy_reset(ap);
1573 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1574 return;
1575 ata_bus_reset(ap);
1576}
1577
1578/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001579 * ata_port_disable - Disable port.
1580 * @ap: Port to be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001582 * Modify @ap data structure such that the system
1583 * thinks that the entire port is disabled, and should
1584 * never attempt to probe or communicate with devices
1585 * on this port.
1586 *
1587 * LOCKING: host_set lock, or some other form of
1588 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 */
1590
1591void ata_port_disable(struct ata_port *ap)
1592{
1593 ap->device[0].class = ATA_DEV_NONE;
1594 ap->device[1].class = ATA_DEV_NONE;
1595 ap->flags |= ATA_FLAG_PORT_DISABLED;
1596}
1597
Alan Cox452503f2005-10-21 19:01:32 -04001598/*
1599 * This mode timing computation functionality is ported over from
1600 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
1601 */
1602/*
1603 * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
1604 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
1605 * for PIO 5, which is a nonstandard extension and UDMA6, which
1606 * is currently supported only by Maxtor drives.
1607 */
1608
1609static const struct ata_timing ata_timing[] = {
1610
1611 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
1612 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
1613 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
1614 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
1615
1616 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
1617 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
1618 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
1619
1620/* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
1621
1622 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
1623 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
1624 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
1625
1626 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
1627 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
1628 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
1629
1630/* { XFER_PIO_5, 20, 50, 30, 100, 50, 30, 100, 0 }, */
1631 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
1632 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
1633
1634 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
1635 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
1636 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
1637
1638/* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
1639
1640 { 0xFF }
1641};
1642
1643#define ENOUGH(v,unit) (((v)-1)/(unit)+1)
1644#define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
1645
1646static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
1647{
1648 q->setup = EZ(t->setup * 1000, T);
1649 q->act8b = EZ(t->act8b * 1000, T);
1650 q->rec8b = EZ(t->rec8b * 1000, T);
1651 q->cyc8b = EZ(t->cyc8b * 1000, T);
1652 q->active = EZ(t->active * 1000, T);
1653 q->recover = EZ(t->recover * 1000, T);
1654 q->cycle = EZ(t->cycle * 1000, T);
1655 q->udma = EZ(t->udma * 1000, UT);
1656}
1657
1658void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
1659 struct ata_timing *m, unsigned int what)
1660{
1661 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
1662 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
1663 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
1664 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
1665 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
1666 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
1667 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
1668 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
1669}
1670
1671static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
1672{
1673 const struct ata_timing *t;
1674
1675 for (t = ata_timing; t->mode != speed; t++)
Alan Cox91190752005-10-26 12:17:46 -04001676 if (t->mode == 0xFF)
Alan Cox452503f2005-10-21 19:01:32 -04001677 return NULL;
1678 return t;
1679}
1680
1681int ata_timing_compute(struct ata_device *adev, unsigned short speed,
1682 struct ata_timing *t, int T, int UT)
1683{
1684 const struct ata_timing *s;
1685 struct ata_timing p;
1686
1687 /*
1688 * Find the mode.
Albert Lee75b1f2f2005-11-16 17:06:18 +08001689 */
Alan Cox452503f2005-10-21 19:01:32 -04001690
1691 if (!(s = ata_timing_find_mode(speed)))
1692 return -EINVAL;
1693
Albert Lee75b1f2f2005-11-16 17:06:18 +08001694 memcpy(t, s, sizeof(*s));
1695
Alan Cox452503f2005-10-21 19:01:32 -04001696 /*
1697 * If the drive is an EIDE drive, it can tell us it needs extended
1698 * PIO/MW_DMA cycle timing.
1699 */
1700
1701 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
1702 memset(&p, 0, sizeof(p));
1703 if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
1704 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
1705 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
1706 } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
1707 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
1708 }
1709 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
1710 }
1711
1712 /*
1713 * Convert the timing to bus clock counts.
1714 */
1715
Albert Lee75b1f2f2005-11-16 17:06:18 +08001716 ata_timing_quantize(t, t, T, UT);
Alan Cox452503f2005-10-21 19:01:32 -04001717
1718 /*
1719 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY, S.M.A.R.T
1720 * and some other commands. We have to ensure that the DMA cycle timing is
1721 * slower/equal than the fastest PIO timing.
1722 */
1723
1724 if (speed > XFER_PIO_4) {
1725 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
1726 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
1727 }
1728
1729 /*
1730 * Lenghten active & recovery time so that cycle time is correct.
1731 */
1732
1733 if (t->act8b + t->rec8b < t->cyc8b) {
1734 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
1735 t->rec8b = t->cyc8b - t->act8b;
1736 }
1737
1738 if (t->active + t->recover < t->cycle) {
1739 t->active += (t->cycle - (t->active + t->recover)) / 2;
1740 t->recover = t->cycle - t->active;
1741 }
1742
1743 return 0;
1744}
1745
Jeff Garzik057ace52005-10-22 14:27:05 -04001746static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 unsigned int shift;
1748 u8 base;
1749} xfer_mode_classes[] = {
1750 { ATA_SHIFT_UDMA, XFER_UDMA_0 },
1751 { ATA_SHIFT_MWDMA, XFER_MW_DMA_0 },
1752 { ATA_SHIFT_PIO, XFER_PIO_0 },
1753};
1754
1755static inline u8 base_from_shift(unsigned int shift)
1756{
1757 int i;
1758
1759 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++)
1760 if (xfer_mode_classes[i].shift == shift)
1761 return xfer_mode_classes[i].base;
1762
1763 return 0xff;
1764}
1765
1766static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
1767{
1768 int ofs, idx;
1769 u8 base;
1770
1771 if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1772 return;
1773
1774 if (dev->xfer_shift == ATA_SHIFT_PIO)
1775 dev->flags |= ATA_DFLAG_PIO;
1776
1777 ata_dev_set_xfermode(ap, dev);
1778
1779 base = base_from_shift(dev->xfer_shift);
1780 ofs = dev->xfer_mode - base;
1781 idx = ofs + dev->xfer_shift;
1782 WARN_ON(idx >= ARRAY_SIZE(xfer_mode_str));
1783
1784 DPRINTK("idx=%d xfer_shift=%u, xfer_mode=0x%x, base=0x%x, offset=%d\n",
1785 idx, dev->xfer_shift, (int)dev->xfer_mode, (int)base, ofs);
1786
1787 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
1788 ap->id, dev->devno, xfer_mode_str[idx]);
1789}
1790
1791static int ata_host_set_pio(struct ata_port *ap)
1792{
1793 unsigned int mask;
1794 int x, i;
1795 u8 base, xfer_mode;
1796
1797 mask = ata_get_mode_mask(ap, ATA_SHIFT_PIO);
1798 x = fgb(mask);
1799 if (x < 0) {
1800 printk(KERN_WARNING "ata%u: no PIO support\n", ap->id);
1801 return -1;
1802 }
1803
1804 base = base_from_shift(ATA_SHIFT_PIO);
1805 xfer_mode = base + x;
1806
1807 DPRINTK("base 0x%x xfer_mode 0x%x mask 0x%x x %d\n",
1808 (int)base, (int)xfer_mode, mask, x);
1809
1810 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1811 struct ata_device *dev = &ap->device[i];
1812 if (ata_dev_present(dev)) {
1813 dev->pio_mode = xfer_mode;
1814 dev->xfer_mode = xfer_mode;
1815 dev->xfer_shift = ATA_SHIFT_PIO;
1816 if (ap->ops->set_piomode)
1817 ap->ops->set_piomode(ap, dev);
1818 }
1819 }
1820
1821 return 0;
1822}
1823
1824static void ata_host_set_dma(struct ata_port *ap, u8 xfer_mode,
1825 unsigned int xfer_shift)
1826{
1827 int i;
1828
1829 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1830 struct ata_device *dev = &ap->device[i];
1831 if (ata_dev_present(dev)) {
1832 dev->dma_mode = xfer_mode;
1833 dev->xfer_mode = xfer_mode;
1834 dev->xfer_shift = xfer_shift;
1835 if (ap->ops->set_dmamode)
1836 ap->ops->set_dmamode(ap, dev);
1837 }
1838 }
1839}
1840
1841/**
1842 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1843 * @ap: port on which timings will be programmed
1844 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001845 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1846 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001848 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 *
1850 */
1851static void ata_set_mode(struct ata_port *ap)
1852{
Albert Lee8cbd6df2005-10-12 15:06:27 +08001853 unsigned int xfer_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 u8 xfer_mode;
1855 int rc;
1856
1857 /* step 1: always set host PIO timings */
1858 rc = ata_host_set_pio(ap);
1859 if (rc)
1860 goto err_out;
1861
1862 /* step 2: choose the best data xfer mode */
1863 xfer_mode = xfer_shift = 0;
1864 rc = ata_choose_xfer_mode(ap, &xfer_mode, &xfer_shift);
1865 if (rc)
1866 goto err_out;
1867
1868 /* step 3: if that xfer mode isn't PIO, set host DMA timings */
1869 if (xfer_shift != ATA_SHIFT_PIO)
1870 ata_host_set_dma(ap, xfer_mode, xfer_shift);
1871
1872 /* step 4: update devices' xfer mode */
1873 ata_dev_set_mode(ap, &ap->device[0]);
1874 ata_dev_set_mode(ap, &ap->device[1]);
1875
1876 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1877 return;
1878
1879 if (ap->ops->post_set_mode)
1880 ap->ops->post_set_mode(ap);
1881
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 return;
1883
1884err_out:
1885 ata_port_disable(ap);
1886}
1887
1888/**
1889 * ata_busy_sleep - sleep until BSY clears, or timeout
1890 * @ap: port containing status register to be polled
1891 * @tmout_pat: impatience timeout
1892 * @tmout: overall timeout
1893 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001894 * Sleep until ATA Status register bit BSY clears,
1895 * or a timeout occurs.
1896 *
1897 * LOCKING: None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 *
1899 */
1900
1901static unsigned int ata_busy_sleep (struct ata_port *ap,
1902 unsigned long tmout_pat,
1903 unsigned long tmout)
1904{
1905 unsigned long timer_start, timeout;
1906 u8 status;
1907
1908 status = ata_busy_wait(ap, ATA_BUSY, 300);
1909 timer_start = jiffies;
1910 timeout = timer_start + tmout_pat;
1911 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1912 msleep(50);
1913 status = ata_busy_wait(ap, ATA_BUSY, 3);
1914 }
1915
1916 if (status & ATA_BUSY)
1917 printk(KERN_WARNING "ata%u is slow to respond, "
1918 "please be patient\n", ap->id);
1919
1920 timeout = timer_start + tmout;
1921 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1922 msleep(50);
1923 status = ata_chk_status(ap);
1924 }
1925
1926 if (status & ATA_BUSY) {
1927 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
1928 ap->id, tmout / HZ);
1929 return 1;
1930 }
1931
1932 return 0;
1933}
1934
1935static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
1936{
1937 struct ata_ioports *ioaddr = &ap->ioaddr;
1938 unsigned int dev0 = devmask & (1 << 0);
1939 unsigned int dev1 = devmask & (1 << 1);
1940 unsigned long timeout;
1941
1942 /* if device 0 was found in ata_devchk, wait for its
1943 * BSY bit to clear
1944 */
1945 if (dev0)
1946 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1947
1948 /* if device 1 was found in ata_devchk, wait for
1949 * register access, then wait for BSY to clear
1950 */
1951 timeout = jiffies + ATA_TMOUT_BOOT;
1952 while (dev1) {
1953 u8 nsect, lbal;
1954
1955 ap->ops->dev_select(ap, 1);
1956 if (ap->flags & ATA_FLAG_MMIO) {
1957 nsect = readb((void __iomem *) ioaddr->nsect_addr);
1958 lbal = readb((void __iomem *) ioaddr->lbal_addr);
1959 } else {
1960 nsect = inb(ioaddr->nsect_addr);
1961 lbal = inb(ioaddr->lbal_addr);
1962 }
1963 if ((nsect == 1) && (lbal == 1))
1964 break;
1965 if (time_after(jiffies, timeout)) {
1966 dev1 = 0;
1967 break;
1968 }
1969 msleep(50); /* give drive a breather */
1970 }
1971 if (dev1)
1972 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1973
1974 /* is all this really necessary? */
1975 ap->ops->dev_select(ap, 0);
1976 if (dev1)
1977 ap->ops->dev_select(ap, 1);
1978 if (dev0)
1979 ap->ops->dev_select(ap, 0);
1980}
1981
1982/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001983 * ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
1984 * @ap: Port to reset and probe
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001986 * Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
1987 * probe the bus. Not often used these days.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 *
1989 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001990 * PCI/etc. bus probe sem.
Jeff Garzike5338252005-10-30 21:37:17 -05001991 * Obtains host_set lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 *
1993 */
1994
1995static unsigned int ata_bus_edd(struct ata_port *ap)
1996{
1997 struct ata_taskfile tf;
Jeff Garzike5338252005-10-30 21:37:17 -05001998 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999
2000 /* set up execute-device-diag (bus reset) taskfile */
2001 /* also, take interrupts to a known state (disabled) */
2002 DPRINTK("execute-device-diag\n");
2003 ata_tf_init(ap, &tf, 0);
2004 tf.ctl |= ATA_NIEN;
2005 tf.command = ATA_CMD_EDD;
2006 tf.protocol = ATA_PROT_NODATA;
2007
2008 /* do bus reset */
Jeff Garzike5338252005-10-30 21:37:17 -05002009 spin_lock_irqsave(&ap->host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 ata_tf_to_host(ap, &tf);
Jeff Garzike5338252005-10-30 21:37:17 -05002011 spin_unlock_irqrestore(&ap->host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
2013 /* spec says at least 2ms. but who knows with those
2014 * crazy ATAPI devices...
2015 */
2016 msleep(150);
2017
2018 return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2019}
2020
2021static unsigned int ata_bus_softreset(struct ata_port *ap,
2022 unsigned int devmask)
2023{
2024 struct ata_ioports *ioaddr = &ap->ioaddr;
2025
2026 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
2027
2028 /* software reset. causes dev0 to be selected */
2029 if (ap->flags & ATA_FLAG_MMIO) {
2030 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2031 udelay(20); /* FIXME: flush */
2032 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
2033 udelay(20); /* FIXME: flush */
2034 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2035 } else {
2036 outb(ap->ctl, ioaddr->ctl_addr);
2037 udelay(10);
2038 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
2039 udelay(10);
2040 outb(ap->ctl, ioaddr->ctl_addr);
2041 }
2042
2043 /* spec mandates ">= 2ms" before checking status.
2044 * We wait 150ms, because that was the magic delay used for
2045 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
2046 * between when the ATA command register is written, and then
2047 * status is checked. Because waiting for "a while" before
2048 * checking status is fine, post SRST, we perform this magic
2049 * delay here as well.
2050 */
2051 msleep(150);
2052
2053 ata_bus_post_reset(ap, devmask);
2054
2055 return 0;
2056}
2057
2058/**
2059 * ata_bus_reset - reset host port and associated ATA channel
2060 * @ap: port to reset
2061 *
2062 * This is typically the first time we actually start issuing
2063 * commands to the ATA channel. We wait for BSY to clear, then
2064 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
2065 * result. Determine what devices, if any, are on the channel
2066 * by looking at the device 0/1 error register. Look at the signature
2067 * stored in each device's taskfile registers, to determine if
2068 * the device is ATA or ATAPI.
2069 *
2070 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002071 * PCI/etc. bus probe sem.
2072 * Obtains host_set lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002073 *
2074 * SIDE EFFECTS:
2075 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
2076 */
2077
2078void ata_bus_reset(struct ata_port *ap)
2079{
2080 struct ata_ioports *ioaddr = &ap->ioaddr;
2081 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2082 u8 err;
2083 unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
2084
2085 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
2086
2087 /* determine if device 0/1 are present */
2088 if (ap->flags & ATA_FLAG_SATA_RESET)
2089 dev0 = 1;
2090 else {
2091 dev0 = ata_devchk(ap, 0);
2092 if (slave_possible)
2093 dev1 = ata_devchk(ap, 1);
2094 }
2095
2096 if (dev0)
2097 devmask |= (1 << 0);
2098 if (dev1)
2099 devmask |= (1 << 1);
2100
2101 /* select device 0 again */
2102 ap->ops->dev_select(ap, 0);
2103
2104 /* issue bus reset */
2105 if (ap->flags & ATA_FLAG_SRST)
2106 rc = ata_bus_softreset(ap, devmask);
2107 else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
2108 /* set up device control */
2109 if (ap->flags & ATA_FLAG_MMIO)
2110 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2111 else
2112 outb(ap->ctl, ioaddr->ctl_addr);
2113 rc = ata_bus_edd(ap);
2114 }
2115
2116 if (rc)
2117 goto err_out;
2118
2119 /*
2120 * determine by signature whether we have ATA or ATAPI devices
2121 */
2122 err = ata_dev_try_classify(ap, 0);
2123 if ((slave_possible) && (err != 0x81))
2124 ata_dev_try_classify(ap, 1);
2125
2126 /* re-enable interrupts */
2127 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2128 ata_irq_on(ap);
2129
2130 /* is double-select really necessary? */
2131 if (ap->device[1].class != ATA_DEV_NONE)
2132 ap->ops->dev_select(ap, 1);
2133 if (ap->device[0].class != ATA_DEV_NONE)
2134 ap->ops->dev_select(ap, 0);
2135
2136 /* if no devices were detected, disable this port */
2137 if ((ap->device[0].class == ATA_DEV_NONE) &&
2138 (ap->device[1].class == ATA_DEV_NONE))
2139 goto err_out;
2140
2141 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
2142 /* set up device control for ATA_FLAG_SATA_RESET */
2143 if (ap->flags & ATA_FLAG_MMIO)
2144 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2145 else
2146 outb(ap->ctl, ioaddr->ctl_addr);
2147 }
2148
2149 DPRINTK("EXIT\n");
2150 return;
2151
2152err_out:
2153 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
2154 ap->ops->port_disable(ap);
2155
2156 DPRINTK("EXIT\n");
2157}
2158
Jeff Garzik057ace52005-10-22 14:27:05 -04002159static void ata_pr_blacklisted(const struct ata_port *ap,
2160 const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161{
2162 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, disabling DMA\n",
2163 ap->id, dev->devno);
2164}
2165
Arjan van de Ven98ac62d2005-11-28 10:06:23 +01002166static const char * const ata_dma_blacklist [] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 "WDC AC11000H",
2168 "WDC AC22100H",
2169 "WDC AC32500H",
2170 "WDC AC33100H",
2171 "WDC AC31600H",
2172 "WDC AC32100H",
2173 "WDC AC23200L",
2174 "Compaq CRD-8241B",
2175 "CRD-8400B",
2176 "CRD-8480B",
2177 "CRD-8482B",
2178 "CRD-84",
2179 "SanDisk SDP3B",
2180 "SanDisk SDP3B-64",
2181 "SANYO CD-ROM CRD",
2182 "HITACHI CDR-8",
2183 "HITACHI CDR-8335",
2184 "HITACHI CDR-8435",
2185 "Toshiba CD-ROM XM-6202B",
Jeff Garzike9222562005-06-28 00:03:37 -04002186 "TOSHIBA CD-ROM XM-1702BC",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 "CD-532E-A",
2188 "E-IDE CD-ROM CR-840",
2189 "CD-ROM Drive/F5A",
2190 "WPI CDD-820",
2191 "SAMSUNG CD-ROM SC-148C",
2192 "SAMSUNG CD-ROM SC",
2193 "SanDisk SDP3B-64",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 "ATAPI CD-ROM DRIVE 40X MAXIMUM",
2195 "_NEC DV5800A",
2196};
2197
Jeff Garzik057ace52005-10-22 14:27:05 -04002198static int ata_dma_blacklisted(const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199{
2200 unsigned char model_num[40];
2201 char *s;
2202 unsigned int len;
2203 int i;
2204
2205 ata_dev_id_string(dev->id, model_num, ATA_ID_PROD_OFS,
2206 sizeof(model_num));
2207 s = &model_num[0];
2208 len = strnlen(s, sizeof(model_num));
2209
2210 /* ATAPI specifies that empty space is blank-filled; remove blanks */
2211 while ((len > 0) && (s[len - 1] == ' ')) {
2212 len--;
2213 s[len] = 0;
2214 }
2215
2216 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i++)
2217 if (!strncmp(ata_dma_blacklist[i], s, len))
2218 return 1;
2219
2220 return 0;
2221}
2222
Jeff Garzik057ace52005-10-22 14:27:05 -04002223static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224{
Jeff Garzik057ace52005-10-22 14:27:05 -04002225 const struct ata_device *master, *slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 unsigned int mask;
2227
2228 master = &ap->device[0];
2229 slave = &ap->device[1];
2230
2231 assert (ata_dev_present(master) || ata_dev_present(slave));
2232
2233 if (shift == ATA_SHIFT_UDMA) {
2234 mask = ap->udma_mask;
2235 if (ata_dev_present(master)) {
2236 mask &= (master->id[ATA_ID_UDMA_MODES] & 0xff);
Jeff Garzik057ace52005-10-22 14:27:05 -04002237 if (ata_dma_blacklisted(master)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 mask = 0;
2239 ata_pr_blacklisted(ap, master);
2240 }
2241 }
2242 if (ata_dev_present(slave)) {
2243 mask &= (slave->id[ATA_ID_UDMA_MODES] & 0xff);
Jeff Garzik057ace52005-10-22 14:27:05 -04002244 if (ata_dma_blacklisted(slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245 mask = 0;
2246 ata_pr_blacklisted(ap, slave);
2247 }
2248 }
2249 }
2250 else if (shift == ATA_SHIFT_MWDMA) {
2251 mask = ap->mwdma_mask;
2252 if (ata_dev_present(master)) {
2253 mask &= (master->id[ATA_ID_MWDMA_MODES] & 0x07);
Jeff Garzik057ace52005-10-22 14:27:05 -04002254 if (ata_dma_blacklisted(master)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 mask = 0;
2256 ata_pr_blacklisted(ap, master);
2257 }
2258 }
2259 if (ata_dev_present(slave)) {
2260 mask &= (slave->id[ATA_ID_MWDMA_MODES] & 0x07);
Jeff Garzik057ace52005-10-22 14:27:05 -04002261 if (ata_dma_blacklisted(slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 mask = 0;
2263 ata_pr_blacklisted(ap, slave);
2264 }
2265 }
2266 }
2267 else if (shift == ATA_SHIFT_PIO) {
2268 mask = ap->pio_mask;
2269 if (ata_dev_present(master)) {
2270 /* spec doesn't return explicit support for
2271 * PIO0-2, so we fake it
2272 */
2273 u16 tmp_mode = master->id[ATA_ID_PIO_MODES] & 0x03;
2274 tmp_mode <<= 3;
2275 tmp_mode |= 0x7;
2276 mask &= tmp_mode;
2277 }
2278 if (ata_dev_present(slave)) {
2279 /* spec doesn't return explicit support for
2280 * PIO0-2, so we fake it
2281 */
2282 u16 tmp_mode = slave->id[ATA_ID_PIO_MODES] & 0x03;
2283 tmp_mode <<= 3;
2284 tmp_mode |= 0x7;
2285 mask &= tmp_mode;
2286 }
2287 }
2288 else {
2289 mask = 0xffffffff; /* shut up compiler warning */
2290 BUG();
2291 }
2292
2293 return mask;
2294}
2295
2296/* find greatest bit */
2297static int fgb(u32 bitmap)
2298{
2299 unsigned int i;
2300 int x = -1;
2301
2302 for (i = 0; i < 32; i++)
2303 if (bitmap & (1 << i))
2304 x = i;
2305
2306 return x;
2307}
2308
2309/**
2310 * ata_choose_xfer_mode - attempt to find best transfer mode
2311 * @ap: Port for which an xfer mode will be selected
2312 * @xfer_mode_out: (output) SET FEATURES - XFER MODE code
2313 * @xfer_shift_out: (output) bit shift that selects this mode
2314 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04002315 * Based on host and device capabilities, determine the
2316 * maximum transfer mode that is amenable to all.
2317 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002319 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 *
2321 * RETURNS:
2322 * Zero on success, negative on error.
2323 */
2324
Jeff Garzik057ace52005-10-22 14:27:05 -04002325static int ata_choose_xfer_mode(const struct ata_port *ap,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 u8 *xfer_mode_out,
2327 unsigned int *xfer_shift_out)
2328{
2329 unsigned int mask, shift;
2330 int x, i;
2331
2332 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++) {
2333 shift = xfer_mode_classes[i].shift;
2334 mask = ata_get_mode_mask(ap, shift);
2335
2336 x = fgb(mask);
2337 if (x >= 0) {
2338 *xfer_mode_out = xfer_mode_classes[i].base + x;
2339 *xfer_shift_out = shift;
2340 return 0;
2341 }
2342 }
2343
2344 return -1;
2345}
2346
2347/**
2348 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2349 * @ap: Port associated with device @dev
2350 * @dev: Device to which command will be sent
2351 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002352 * Issue SET FEATURES - XFER MODE command to device @dev
2353 * on port @ap.
2354 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002356 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 */
2358
2359static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
2360{
Tejun Heoa0123702005-12-13 14:49:31 +09002361 struct ata_taskfile tf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362
2363 /* set up set-features taskfile */
2364 DPRINTK("set features - xfer mode\n");
2365
Tejun Heoa0123702005-12-13 14:49:31 +09002366 ata_tf_init(ap, &tf, dev->devno);
2367 tf.command = ATA_CMD_SET_FEATURES;
2368 tf.feature = SETFEATURES_XFER;
2369 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2370 tf.protocol = ATA_PROT_NODATA;
2371 tf.nsect = dev->xfer_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372
Tejun Heoa0123702005-12-13 14:49:31 +09002373 if (ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0)) {
2374 printk(KERN_ERR "ata%u: failed to set xfermode, disabled\n",
2375 ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 ata_port_disable(ap);
Tejun Heoa0123702005-12-13 14:49:31 +09002377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378
2379 DPRINTK("EXIT\n");
2380}
2381
2382/**
Albert Lee59a10b12005-10-12 15:09:42 +08002383 * ata_dev_reread_id - Reread the device identify device info
2384 * @ap: port where the device is
2385 * @dev: device to reread the identify device info
2386 *
2387 * LOCKING:
2388 */
2389
2390static void ata_dev_reread_id(struct ata_port *ap, struct ata_device *dev)
2391{
Tejun Heoa0123702005-12-13 14:49:31 +09002392 struct ata_taskfile tf;
Albert Lee59a10b12005-10-12 15:09:42 +08002393
Tejun Heoa0123702005-12-13 14:49:31 +09002394 ata_tf_init(ap, &tf, dev->devno);
Albert Lee59a10b12005-10-12 15:09:42 +08002395
2396 if (dev->class == ATA_DEV_ATA) {
Tejun Heoa0123702005-12-13 14:49:31 +09002397 tf.command = ATA_CMD_ID_ATA;
Albert Lee59a10b12005-10-12 15:09:42 +08002398 DPRINTK("do ATA identify\n");
2399 } else {
Tejun Heoa0123702005-12-13 14:49:31 +09002400 tf.command = ATA_CMD_ID_ATAPI;
Albert Lee59a10b12005-10-12 15:09:42 +08002401 DPRINTK("do ATAPI identify\n");
2402 }
2403
Tejun Heoa0123702005-12-13 14:49:31 +09002404 tf.flags |= ATA_TFLAG_DEVICE;
2405 tf.protocol = ATA_PROT_PIO;
Albert Lee59a10b12005-10-12 15:09:42 +08002406
Tejun Heoa0123702005-12-13 14:49:31 +09002407 if (ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE,
2408 dev->id, sizeof(dev->id)))
Albert Lee59a10b12005-10-12 15:09:42 +08002409 goto err_out;
2410
Albert Lee59a10b12005-10-12 15:09:42 +08002411 swap_buf_le16(dev->id, ATA_ID_WORDS);
2412
2413 ata_dump_id(dev);
2414
2415 DPRINTK("EXIT\n");
2416
2417 return;
2418err_out:
Tejun Heoa0123702005-12-13 14:49:31 +09002419 printk(KERN_ERR "ata%u: failed to reread ID, disabled\n", ap->id);
Albert Lee59a10b12005-10-12 15:09:42 +08002420 ata_port_disable(ap);
2421}
2422
2423/**
Albert Lee8bf62ec2005-05-12 15:29:42 -04002424 * ata_dev_init_params - Issue INIT DEV PARAMS command
2425 * @ap: Port associated with device @dev
2426 * @dev: Device to which command will be sent
2427 *
2428 * LOCKING:
2429 */
2430
2431static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev)
2432{
Tejun Heoa0123702005-12-13 14:49:31 +09002433 struct ata_taskfile tf;
Albert Lee8bf62ec2005-05-12 15:29:42 -04002434 u16 sectors = dev->id[6];
2435 u16 heads = dev->id[3];
2436
2437 /* Number of sectors per track 1-255. Number of heads 1-16 */
2438 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
2439 return;
2440
2441 /* set up init dev params taskfile */
2442 DPRINTK("init dev params \n");
2443
Tejun Heoa0123702005-12-13 14:49:31 +09002444 ata_tf_init(ap, &tf, dev->devno);
2445 tf.command = ATA_CMD_INIT_DEV_PARAMS;
2446 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2447 tf.protocol = ATA_PROT_NODATA;
2448 tf.nsect = sectors;
2449 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
Albert Lee8bf62ec2005-05-12 15:29:42 -04002450
Tejun Heoa0123702005-12-13 14:49:31 +09002451 if (ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0)) {
2452 printk(KERN_ERR "ata%u: failed to init parameters, disabled\n",
2453 ap->id);
Albert Lee8bf62ec2005-05-12 15:29:42 -04002454 ata_port_disable(ap);
Tejun Heoa0123702005-12-13 14:49:31 +09002455 }
Albert Lee8bf62ec2005-05-12 15:29:42 -04002456
2457 DPRINTK("EXIT\n");
2458}
2459
2460/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002461 * ata_sg_clean - Unmap DMA memory associated with command
2462 * @qc: Command containing DMA memory to be released
2463 *
2464 * Unmap all mapped DMA memory associated with this command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465 *
2466 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002467 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002468 */
2469
2470static void ata_sg_clean(struct ata_queued_cmd *qc)
2471{
2472 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002473 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002475 void *pad_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476
2477 assert(qc->flags & ATA_QCFLAG_DMAMAP);
2478 assert(sg != NULL);
2479
2480 if (qc->flags & ATA_QCFLAG_SINGLE)
2481 assert(qc->n_elem == 1);
2482
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05002483 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002485 /* if we padded the buffer out to 32-bit bound, and data
2486 * xfer direction is from-device, we must copy from the
2487 * pad buffer back into the supplied buffer
2488 */
2489 if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
2490 pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2491
2492 if (qc->flags & ATA_QCFLAG_SG) {
Jeff Garzike1410f22005-11-14 14:06:26 -05002493 if (qc->n_elem)
2494 dma_unmap_sg(ap->host_set->dev, sg, qc->n_elem, dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002495 /* restore last sg */
2496 sg[qc->orig_n_elem - 1].length += qc->pad_len;
2497 if (pad_buf) {
2498 struct scatterlist *psg = &qc->pad_sgent;
2499 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2500 memcpy(addr + psg->offset, pad_buf, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05002501 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002502 }
2503 } else {
Jeff Garzike1410f22005-11-14 14:06:26 -05002504 if (sg_dma_len(&sg[0]) > 0)
2505 dma_unmap_single(ap->host_set->dev,
2506 sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
2507 dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002508 /* restore sg */
2509 sg->length += qc->pad_len;
2510 if (pad_buf)
2511 memcpy(qc->buf_virt + sg->length - qc->pad_len,
2512 pad_buf, qc->pad_len);
2513 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514
2515 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002516 qc->__sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517}
2518
2519/**
2520 * ata_fill_sg - Fill PCI IDE PRD table
2521 * @qc: Metadata associated with taskfile to be transferred
2522 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002523 * Fill PCI IDE PRD (scatter-gather) table with segments
2524 * associated with the current disk command.
2525 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 * LOCKING:
Jeff Garzik780a87f2005-05-30 15:41:05 -04002527 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 *
2529 */
2530static void ata_fill_sg(struct ata_queued_cmd *qc)
2531{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002533 struct scatterlist *sg;
2534 unsigned int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002536 assert(qc->__sg != NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 assert(qc->n_elem > 0);
2538
2539 idx = 0;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002540 ata_for_each_sg(sg, qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541 u32 addr, offset;
2542 u32 sg_len, len;
2543
2544 /* determine if physical DMA addr spans 64K boundary.
2545 * Note h/w doesn't support 64-bit, so we unconditionally
2546 * truncate dma_addr_t to u32.
2547 */
2548 addr = (u32) sg_dma_address(sg);
2549 sg_len = sg_dma_len(sg);
2550
2551 while (sg_len) {
2552 offset = addr & 0xffff;
2553 len = sg_len;
2554 if ((offset + sg_len) > 0x10000)
2555 len = 0x10000 - offset;
2556
2557 ap->prd[idx].addr = cpu_to_le32(addr);
2558 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
2559 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
2560
2561 idx++;
2562 sg_len -= len;
2563 addr += len;
2564 }
2565 }
2566
2567 if (idx)
2568 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2569}
2570/**
2571 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2572 * @qc: Metadata associated with taskfile to check
2573 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002574 * Allow low-level driver to filter ATA PACKET commands, returning
2575 * a status indicating whether or not it is OK to use DMA for the
2576 * supplied PACKET command.
2577 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002579 * spin_lock_irqsave(host_set lock)
2580 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002581 * RETURNS: 0 when ATAPI DMA can be used
2582 * nonzero otherwise
2583 */
2584int ata_check_atapi_dma(struct ata_queued_cmd *qc)
2585{
2586 struct ata_port *ap = qc->ap;
2587 int rc = 0; /* Assume ATAPI DMA is OK by default */
2588
2589 if (ap->ops->check_atapi_dma)
2590 rc = ap->ops->check_atapi_dma(qc);
2591
2592 return rc;
2593}
2594/**
2595 * ata_qc_prep - Prepare taskfile for submission
2596 * @qc: Metadata associated with taskfile to be prepared
2597 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002598 * Prepare ATA taskfile for submission.
2599 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 * LOCKING:
2601 * spin_lock_irqsave(host_set lock)
2602 */
2603void ata_qc_prep(struct ata_queued_cmd *qc)
2604{
2605 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2606 return;
2607
2608 ata_fill_sg(qc);
2609}
2610
Jeff Garzik0cba6322005-05-30 19:49:12 -04002611/**
2612 * ata_sg_init_one - Associate command with memory buffer
2613 * @qc: Command to be associated
2614 * @buf: Memory buffer
2615 * @buflen: Length of memory buffer, in bytes.
2616 *
2617 * Initialize the data-related elements of queued_cmd @qc
2618 * to point to a single memory buffer, @buf of byte length @buflen.
2619 *
2620 * LOCKING:
2621 * spin_lock_irqsave(host_set lock)
2622 */
2623
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
2625{
2626 struct scatterlist *sg;
2627
2628 qc->flags |= ATA_QCFLAG_SINGLE;
2629
2630 memset(&qc->sgent, 0, sizeof(qc->sgent));
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002631 qc->__sg = &qc->sgent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 qc->n_elem = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002633 qc->orig_n_elem = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634 qc->buf_virt = buf;
2635
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002636 sg = qc->__sg;
Jeff Garzikf0612bb2005-10-30 01:58:18 -05002637 sg_init_one(sg, buf, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638}
2639
Jeff Garzik0cba6322005-05-30 19:49:12 -04002640/**
2641 * ata_sg_init - Associate command with scatter-gather table.
2642 * @qc: Command to be associated
2643 * @sg: Scatter-gather table.
2644 * @n_elem: Number of elements in s/g table.
2645 *
2646 * Initialize the data-related elements of queued_cmd @qc
2647 * to point to a scatter-gather table @sg, containing @n_elem
2648 * elements.
2649 *
2650 * LOCKING:
2651 * spin_lock_irqsave(host_set lock)
2652 */
2653
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
2655 unsigned int n_elem)
2656{
2657 qc->flags |= ATA_QCFLAG_SG;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002658 qc->__sg = sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 qc->n_elem = n_elem;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002660 qc->orig_n_elem = n_elem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661}
2662
2663/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002664 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
2665 * @qc: Command with memory buffer to be mapped.
2666 *
2667 * DMA-map the memory buffer associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 *
2669 * LOCKING:
2670 * spin_lock_irqsave(host_set lock)
2671 *
2672 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002673 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 */
2675
2676static int ata_sg_setup_one(struct ata_queued_cmd *qc)
2677{
2678 struct ata_port *ap = qc->ap;
2679 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002680 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 dma_addr_t dma_address;
2682
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002683 /* we must lengthen transfers to end on a 32-bit boundary */
2684 qc->pad_len = sg->length & 3;
2685 if (qc->pad_len) {
2686 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2687 struct scatterlist *psg = &qc->pad_sgent;
2688
2689 assert(qc->dev->class == ATA_DEV_ATAPI);
2690
2691 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2692
2693 if (qc->tf.flags & ATA_TFLAG_WRITE)
2694 memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
2695 qc->pad_len);
2696
2697 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2698 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2699 /* trim sg */
2700 sg->length -= qc->pad_len;
2701
2702 DPRINTK("padding done, sg->length=%u pad_len=%u\n",
2703 sg->length, qc->pad_len);
2704 }
2705
Jeff Garzike1410f22005-11-14 14:06:26 -05002706 if (!sg->length) {
2707 sg_dma_address(sg) = 0;
2708 goto skip_map;
2709 }
2710
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711 dma_address = dma_map_single(ap->host_set->dev, qc->buf_virt,
Albert Lee32529e02005-05-26 03:49:42 -04002712 sg->length, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05002713 if (dma_mapping_error(dma_address)) {
2714 /* restore sg */
2715 sg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05002717 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718
2719 sg_dma_address(sg) = dma_address;
Jeff Garzike1410f22005-11-14 14:06:26 -05002720skip_map:
Albert Lee32529e02005-05-26 03:49:42 -04002721 sg_dma_len(sg) = sg->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002722
2723 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
2724 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2725
2726 return 0;
2727}
2728
2729/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002730 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
2731 * @qc: Command with scatter-gather table to be mapped.
2732 *
2733 * DMA-map the scatter-gather table associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 *
2735 * LOCKING:
2736 * spin_lock_irqsave(host_set lock)
2737 *
2738 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002739 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 *
2741 */
2742
2743static int ata_sg_setup(struct ata_queued_cmd *qc)
2744{
2745 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002746 struct scatterlist *sg = qc->__sg;
2747 struct scatterlist *lsg = &sg[qc->n_elem - 1];
Jeff Garzike1410f22005-11-14 14:06:26 -05002748 int n_elem, pre_n_elem, dir, trim_sg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749
2750 VPRINTK("ENTER, ata%u\n", ap->id);
2751 assert(qc->flags & ATA_QCFLAG_SG);
2752
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002753 /* we must lengthen transfers to end on a 32-bit boundary */
2754 qc->pad_len = lsg->length & 3;
2755 if (qc->pad_len) {
2756 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2757 struct scatterlist *psg = &qc->pad_sgent;
2758 unsigned int offset;
2759
2760 assert(qc->dev->class == ATA_DEV_ATAPI);
2761
2762 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2763
2764 /*
2765 * psg->page/offset are used to copy to-be-written
2766 * data in this function or read data in ata_sg_clean.
2767 */
2768 offset = lsg->offset + lsg->length - qc->pad_len;
2769 psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
2770 psg->offset = offset_in_page(offset);
2771
2772 if (qc->tf.flags & ATA_TFLAG_WRITE) {
2773 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2774 memcpy(pad_buf, addr + psg->offset, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05002775 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002776 }
2777
2778 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2779 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2780 /* trim last sg */
2781 lsg->length -= qc->pad_len;
Jeff Garzike1410f22005-11-14 14:06:26 -05002782 if (lsg->length == 0)
2783 trim_sg = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002784
2785 DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
2786 qc->n_elem - 1, lsg->length, qc->pad_len);
2787 }
2788
Jeff Garzike1410f22005-11-14 14:06:26 -05002789 pre_n_elem = qc->n_elem;
2790 if (trim_sg && pre_n_elem)
2791 pre_n_elem--;
2792
2793 if (!pre_n_elem) {
2794 n_elem = 0;
2795 goto skip_map;
2796 }
2797
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 dir = qc->dma_dir;
Jeff Garzike1410f22005-11-14 14:06:26 -05002799 n_elem = dma_map_sg(ap->host_set->dev, sg, pre_n_elem, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05002800 if (n_elem < 1) {
2801 /* restore last sg */
2802 lsg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05002804 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805
2806 DPRINTK("%d sg elements mapped\n", n_elem);
2807
Jeff Garzike1410f22005-11-14 14:06:26 -05002808skip_map:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 qc->n_elem = n_elem;
2810
2811 return 0;
2812}
2813
2814/**
Tejun Heo40e8c822005-08-22 17:12:45 +09002815 * ata_poll_qc_complete - turn irq back on and finish qc
2816 * @qc: Command to complete
Randy Dunlap8e8b77d2005-11-01 21:29:27 -08002817 * @err_mask: ATA status register content
Tejun Heo40e8c822005-08-22 17:12:45 +09002818 *
2819 * LOCKING:
2820 * None. (grabs host lock)
2821 */
2822
Albert Leea22e2eb2005-12-05 15:38:02 +08002823void ata_poll_qc_complete(struct ata_queued_cmd *qc)
Tejun Heo40e8c822005-08-22 17:12:45 +09002824{
2825 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04002826 unsigned long flags;
Tejun Heo40e8c822005-08-22 17:12:45 +09002827
Jeff Garzikb8f61532005-08-25 22:01:20 -04002828 spin_lock_irqsave(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09002829 ap->flags &= ~ATA_FLAG_NOINTR;
2830 ata_irq_on(ap);
Albert Leea22e2eb2005-12-05 15:38:02 +08002831 ata_qc_complete(qc);
Jeff Garzikb8f61532005-08-25 22:01:20 -04002832 spin_unlock_irqrestore(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09002833}
2834
2835/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836 * ata_pio_poll -
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002837 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 *
2839 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002840 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 *
2842 * RETURNS:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002843 * timeout value to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 */
2845
2846static unsigned long ata_pio_poll(struct ata_port *ap)
2847{
Albert Leec14b8332005-12-05 15:36:08 +08002848 struct ata_queued_cmd *qc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 u8 status;
Albert Lee14be71f2005-09-27 17:36:35 +08002850 unsigned int poll_state = HSM_ST_UNKNOWN;
2851 unsigned int reg_state = HSM_ST_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852
Albert Leec14b8332005-12-05 15:36:08 +08002853 qc = ata_qc_from_tag(ap, ap->active_tag);
2854 assert(qc != NULL);
2855
Albert Lee14be71f2005-09-27 17:36:35 +08002856 switch (ap->hsm_task_state) {
2857 case HSM_ST:
2858 case HSM_ST_POLL:
2859 poll_state = HSM_ST_POLL;
2860 reg_state = HSM_ST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 break;
Albert Lee14be71f2005-09-27 17:36:35 +08002862 case HSM_ST_LAST:
2863 case HSM_ST_LAST_POLL:
2864 poll_state = HSM_ST_LAST_POLL;
2865 reg_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 break;
2867 default:
2868 BUG();
2869 break;
2870 }
2871
2872 status = ata_chk_status(ap);
2873 if (status & ATA_BUSY) {
2874 if (time_after(jiffies, ap->pio_task_timeout)) {
Albert Lee1c848982005-12-05 15:40:15 +08002875 qc->err_mask |= AC_ERR_ATA_BUS;
Albert Lee7c398332005-11-09 13:03:30 +08002876 ap->hsm_task_state = HSM_ST_TMOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 return 0;
2878 }
Albert Lee14be71f2005-09-27 17:36:35 +08002879 ap->hsm_task_state = poll_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 return ATA_SHORT_PAUSE;
2881 }
2882
Albert Lee14be71f2005-09-27 17:36:35 +08002883 ap->hsm_task_state = reg_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 return 0;
2885}
2886
2887/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002888 * ata_pio_complete - check if drive is busy or idle
2889 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 *
2891 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002892 * None. (executing in kernel thread context)
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002893 *
2894 * RETURNS:
2895 * Non-zero if qc completed, zero otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 */
2897
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002898static int ata_pio_complete (struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899{
2900 struct ata_queued_cmd *qc;
2901 u8 drv_stat;
2902
2903 /*
Alan Cox31433ea2005-08-26 15:56:47 +01002904 * This is purely heuristic. This is a fast path. Sometimes when
2905 * we enter, BSY will be cleared in a chk-status or two. If not,
2906 * the drive is probably seeking or something. Snooze for a couple
2907 * msecs, then chk-status again. If still busy, fall back to
Albert Lee14be71f2005-09-27 17:36:35 +08002908 * HSM_ST_POLL state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 */
Albert Leefe79e682005-12-06 11:34:59 +08002910 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
2911 if (drv_stat & ATA_BUSY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912 msleep(2);
Albert Leefe79e682005-12-06 11:34:59 +08002913 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
2914 if (drv_stat & ATA_BUSY) {
Albert Lee14be71f2005-09-27 17:36:35 +08002915 ap->hsm_task_state = HSM_ST_LAST_POLL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002917 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 }
2919 }
2920
Albert Leec14b8332005-12-05 15:36:08 +08002921 qc = ata_qc_from_tag(ap, ap->active_tag);
2922 assert(qc != NULL);
2923
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 drv_stat = ata_wait_idle(ap);
2925 if (!ata_ok(drv_stat)) {
Albert Lee1c848982005-12-05 15:40:15 +08002926 qc->err_mask |= __ac_err_mask(drv_stat);
Albert Lee14be71f2005-09-27 17:36:35 +08002927 ap->hsm_task_state = HSM_ST_ERR;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002928 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 }
2930
Albert Lee14be71f2005-09-27 17:36:35 +08002931 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932
Albert Leea22e2eb2005-12-05 15:38:02 +08002933 assert(qc->err_mask == 0);
2934 ata_poll_qc_complete(qc);
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002935
2936 /* another command may start at this point */
2937
2938 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939}
2940
Edward Falk0baab862005-06-02 18:17:13 -04002941
2942/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002943 * swap_buf_le16 - swap halves of 16-words in place
Edward Falk0baab862005-06-02 18:17:13 -04002944 * @buf: Buffer to swap
2945 * @buf_words: Number of 16-bit words in buffer.
2946 *
2947 * Swap halves of 16-bit words if needed to convert from
2948 * little-endian byte order to native cpu byte order, or
2949 * vice-versa.
2950 *
2951 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002952 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04002953 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954void swap_buf_le16(u16 *buf, unsigned int buf_words)
2955{
2956#ifdef __BIG_ENDIAN
2957 unsigned int i;
2958
2959 for (i = 0; i < buf_words; i++)
2960 buf[i] = le16_to_cpu(buf[i]);
2961#endif /* __BIG_ENDIAN */
2962}
2963
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002964/**
2965 * ata_mmio_data_xfer - Transfer data by MMIO
2966 * @ap: port to read/write
2967 * @buf: data buffer
2968 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04002969 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002970 *
2971 * Transfer data from/to the device data register by MMIO.
2972 *
2973 * LOCKING:
2974 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002975 */
2976
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
2978 unsigned int buflen, int write_data)
2979{
2980 unsigned int i;
2981 unsigned int words = buflen >> 1;
2982 u16 *buf16 = (u16 *) buf;
2983 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
2984
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002985 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 if (write_data) {
2987 for (i = 0; i < words; i++)
2988 writew(le16_to_cpu(buf16[i]), mmio);
2989 } else {
2990 for (i = 0; i < words; i++)
2991 buf16[i] = cpu_to_le16(readw(mmio));
2992 }
Albert Lee6ae4cfb2005-08-12 14:15:34 +08002993
2994 /* Transfer trailing 1 byte, if any. */
2995 if (unlikely(buflen & 0x01)) {
2996 u16 align_buf[1] = { 0 };
2997 unsigned char *trailing_buf = buf + buflen - 1;
2998
2999 if (write_data) {
3000 memcpy(align_buf, trailing_buf, 1);
3001 writew(le16_to_cpu(align_buf[0]), mmio);
3002 } else {
3003 align_buf[0] = cpu_to_le16(readw(mmio));
3004 memcpy(trailing_buf, align_buf, 1);
3005 }
3006 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007}
3008
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003009/**
3010 * ata_pio_data_xfer - Transfer data by PIO
3011 * @ap: port to read/write
3012 * @buf: data buffer
3013 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04003014 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003015 *
3016 * Transfer data from/to the device data register by PIO.
3017 *
3018 * LOCKING:
3019 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003020 */
3021
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
3023 unsigned int buflen, int write_data)
3024{
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003025 unsigned int words = buflen >> 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003026
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003027 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 if (write_data)
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003029 outsw(ap->ioaddr.data_addr, buf, words);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 else
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003031 insw(ap->ioaddr.data_addr, buf, words);
3032
3033 /* Transfer trailing 1 byte, if any. */
3034 if (unlikely(buflen & 0x01)) {
3035 u16 align_buf[1] = { 0 };
3036 unsigned char *trailing_buf = buf + buflen - 1;
3037
3038 if (write_data) {
3039 memcpy(align_buf, trailing_buf, 1);
3040 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
3041 } else {
3042 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
3043 memcpy(trailing_buf, align_buf, 1);
3044 }
3045 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046}
3047
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003048/**
3049 * ata_data_xfer - Transfer data from/to the data register.
3050 * @ap: port to read/write
3051 * @buf: data buffer
3052 * @buflen: buffer length
3053 * @do_write: read/write
3054 *
3055 * Transfer data from/to the device data register.
3056 *
3057 * LOCKING:
3058 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003059 */
3060
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
3062 unsigned int buflen, int do_write)
3063{
3064 if (ap->flags & ATA_FLAG_MMIO)
3065 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3066 else
3067 ata_pio_data_xfer(ap, buf, buflen, do_write);
3068}
3069
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003070/**
3071 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
3072 * @qc: Command on going
3073 *
3074 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
3075 *
3076 * LOCKING:
3077 * Inherited from caller.
3078 */
3079
Linus Torvalds1da177e2005-04-16 15:20:36 -07003080static void ata_pio_sector(struct ata_queued_cmd *qc)
3081{
3082 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003083 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 struct ata_port *ap = qc->ap;
3085 struct page *page;
3086 unsigned int offset;
3087 unsigned char *buf;
3088
3089 if (qc->cursect == (qc->nsect - 1))
Albert Lee14be71f2005-09-27 17:36:35 +08003090 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091
3092 page = sg[qc->cursg].page;
3093 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
3094
3095 /* get the current page and offset */
3096 page = nth_page(page, (offset >> PAGE_SHIFT));
3097 offset %= PAGE_SIZE;
3098
3099 buf = kmap(page) + offset;
3100
3101 qc->cursect++;
3102 qc->cursg_ofs++;
3103
Albert Lee32529e02005-05-26 03:49:42 -04003104 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 qc->cursg++;
3106 qc->cursg_ofs = 0;
3107 }
3108
3109 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3110
3111 /* do the actual data transfer */
3112 do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3113 ata_data_xfer(ap, buf, ATA_SECT_SIZE, do_write);
3114
3115 kunmap(page);
3116}
3117
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003118/**
3119 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
3120 * @qc: Command on going
3121 * @bytes: number of bytes
3122 *
3123 * Transfer Transfer data from/to the ATAPI device.
3124 *
3125 * LOCKING:
3126 * Inherited from caller.
3127 *
3128 */
3129
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
3131{
3132 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003133 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 struct ata_port *ap = qc->ap;
3135 struct page *page;
3136 unsigned char *buf;
3137 unsigned int offset, count;
3138
Albert Lee563a6e12005-08-12 14:17:50 +08003139 if (qc->curbytes + bytes >= qc->nbytes)
Albert Lee14be71f2005-09-27 17:36:35 +08003140 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141
3142next_sg:
Albert Lee563a6e12005-08-12 14:17:50 +08003143 if (unlikely(qc->cursg >= qc->n_elem)) {
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003144 /*
Albert Lee563a6e12005-08-12 14:17:50 +08003145 * The end of qc->sg is reached and the device expects
3146 * more data to transfer. In order not to overrun qc->sg
3147 * and fulfill length specified in the byte count register,
3148 * - for read case, discard trailing data from the device
3149 * - for write case, padding zero data to the device
3150 */
3151 u16 pad_buf[1] = { 0 };
3152 unsigned int words = bytes >> 1;
3153 unsigned int i;
3154
3155 if (words) /* warning if bytes > 1 */
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003156 printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
Albert Lee563a6e12005-08-12 14:17:50 +08003157 ap->id, bytes);
3158
3159 for (i = 0; i < words; i++)
3160 ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
3161
Albert Lee14be71f2005-09-27 17:36:35 +08003162 ap->hsm_task_state = HSM_ST_LAST;
Albert Lee563a6e12005-08-12 14:17:50 +08003163 return;
3164 }
3165
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003166 sg = &qc->__sg[qc->cursg];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168 page = sg->page;
3169 offset = sg->offset + qc->cursg_ofs;
3170
3171 /* get the current page and offset */
3172 page = nth_page(page, (offset >> PAGE_SHIFT));
3173 offset %= PAGE_SIZE;
3174
Albert Lee6952df02005-06-06 15:56:03 +08003175 /* don't overrun current sg */
Albert Lee32529e02005-05-26 03:49:42 -04003176 count = min(sg->length - qc->cursg_ofs, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177
3178 /* don't cross page boundaries */
3179 count = min(count, (unsigned int)PAGE_SIZE - offset);
3180
3181 buf = kmap(page) + offset;
3182
3183 bytes -= count;
3184 qc->curbytes += count;
3185 qc->cursg_ofs += count;
3186
Albert Lee32529e02005-05-26 03:49:42 -04003187 if (qc->cursg_ofs == sg->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 qc->cursg++;
3189 qc->cursg_ofs = 0;
3190 }
3191
3192 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3193
3194 /* do the actual data transfer */
3195 ata_data_xfer(ap, buf, count, do_write);
3196
3197 kunmap(page);
3198
Albert Lee563a6e12005-08-12 14:17:50 +08003199 if (bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 goto next_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201}
3202
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003203/**
3204 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
3205 * @qc: Command on going
3206 *
3207 * Transfer Transfer data from/to the ATAPI device.
3208 *
3209 * LOCKING:
3210 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003211 */
3212
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213static void atapi_pio_bytes(struct ata_queued_cmd *qc)
3214{
3215 struct ata_port *ap = qc->ap;
3216 struct ata_device *dev = qc->dev;
3217 unsigned int ireason, bc_lo, bc_hi, bytes;
3218 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
3219
3220 ap->ops->tf_read(ap, &qc->tf);
3221 ireason = qc->tf.nsect;
3222 bc_lo = qc->tf.lbam;
3223 bc_hi = qc->tf.lbah;
3224 bytes = (bc_hi << 8) | bc_lo;
3225
3226 /* shall be cleared to zero, indicating xfer of data */
3227 if (ireason & (1 << 0))
3228 goto err_out;
3229
3230 /* make sure transfer direction matches expected */
3231 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
3232 if (do_write != i_write)
3233 goto err_out;
3234
3235 __atapi_pio_bytes(qc, bytes);
3236
3237 return;
3238
3239err_out:
3240 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
3241 ap->id, dev->devno);
Albert Lee1c848982005-12-05 15:40:15 +08003242 qc->err_mask |= AC_ERR_ATA_BUS;
Albert Lee14be71f2005-09-27 17:36:35 +08003243 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244}
3245
3246/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003247 * ata_pio_block - start PIO on a block
3248 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 *
3250 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003251 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 */
3253
3254static void ata_pio_block(struct ata_port *ap)
3255{
3256 struct ata_queued_cmd *qc;
3257 u8 status;
3258
3259 /*
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003260 * This is purely heuristic. This is a fast path.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 * Sometimes when we enter, BSY will be cleared in
3262 * a chk-status or two. If not, the drive is probably seeking
3263 * or something. Snooze for a couple msecs, then
3264 * chk-status again. If still busy, fall back to
Albert Lee14be71f2005-09-27 17:36:35 +08003265 * HSM_ST_POLL state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 */
3267 status = ata_busy_wait(ap, ATA_BUSY, 5);
3268 if (status & ATA_BUSY) {
3269 msleep(2);
3270 status = ata_busy_wait(ap, ATA_BUSY, 10);
3271 if (status & ATA_BUSY) {
Albert Lee14be71f2005-09-27 17:36:35 +08003272 ap->hsm_task_state = HSM_ST_POLL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
3274 return;
3275 }
3276 }
3277
3278 qc = ata_qc_from_tag(ap, ap->active_tag);
3279 assert(qc != NULL);
3280
Albert Leefe79e682005-12-06 11:34:59 +08003281 /* check error */
3282 if (status & (ATA_ERR | ATA_DF)) {
3283 qc->err_mask |= AC_ERR_DEV;
3284 ap->hsm_task_state = HSM_ST_ERR;
3285 return;
3286 }
3287
3288 /* transfer data if any */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289 if (is_atapi_taskfile(&qc->tf)) {
Albert Leefe79e682005-12-06 11:34:59 +08003290 /* DRQ=0 means no more data to transfer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 if ((status & ATA_DRQ) == 0) {
Albert Lee14be71f2005-09-27 17:36:35 +08003292 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293 return;
3294 }
3295
3296 atapi_pio_bytes(qc);
3297 } else {
3298 /* handle BSY=0, DRQ=0 as error */
3299 if ((status & ATA_DRQ) == 0) {
Albert Lee1c848982005-12-05 15:40:15 +08003300 qc->err_mask |= AC_ERR_ATA_BUS;
Albert Lee14be71f2005-09-27 17:36:35 +08003301 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 return;
3303 }
3304
3305 ata_pio_sector(qc);
3306 }
3307}
3308
3309static void ata_pio_error(struct ata_port *ap)
3310{
3311 struct ata_queued_cmd *qc;
Jeff Garzika7dac442005-10-30 04:44:42 -05003312
3313 printk(KERN_WARNING "ata%u: PIO error\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314
3315 qc = ata_qc_from_tag(ap, ap->active_tag);
3316 assert(qc != NULL);
3317
Albert Lee1c848982005-12-05 15:40:15 +08003318 /* make sure qc->err_mask is available to
3319 * know what's wrong and recover
3320 */
3321 assert(qc->err_mask);
3322
Albert Lee14be71f2005-09-27 17:36:35 +08003323 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324
Albert Leea22e2eb2005-12-05 15:38:02 +08003325 ata_poll_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326}
3327
3328static void ata_pio_task(void *_data)
3329{
3330 struct ata_port *ap = _data;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003331 unsigned long timeout;
3332 int qc_completed;
3333
3334fsm_start:
3335 timeout = 0;
3336 qc_completed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337
Albert Lee14be71f2005-09-27 17:36:35 +08003338 switch (ap->hsm_task_state) {
3339 case HSM_ST_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 return;
3341
Albert Lee14be71f2005-09-27 17:36:35 +08003342 case HSM_ST:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 ata_pio_block(ap);
3344 break;
3345
Albert Lee14be71f2005-09-27 17:36:35 +08003346 case HSM_ST_LAST:
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003347 qc_completed = ata_pio_complete(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348 break;
3349
Albert Lee14be71f2005-09-27 17:36:35 +08003350 case HSM_ST_POLL:
3351 case HSM_ST_LAST_POLL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 timeout = ata_pio_poll(ap);
3353 break;
3354
Albert Lee14be71f2005-09-27 17:36:35 +08003355 case HSM_ST_TMOUT:
3356 case HSM_ST_ERR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 ata_pio_error(ap);
3358 return;
3359 }
3360
3361 if (timeout)
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003362 queue_delayed_work(ata_wq, &ap->pio_task, timeout);
3363 else if (!qc_completed)
3364 goto fsm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365}
3366
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367/**
3368 * ata_qc_timeout - Handle timeout of queued command
3369 * @qc: Command that timed out
3370 *
3371 * Some part of the kernel (currently, only the SCSI layer)
3372 * has noticed that the active command on port @ap has not
3373 * completed after a specified length of time. Handle this
3374 * condition by disabling DMA (if necessary) and completing
3375 * transactions, with error if necessary.
3376 *
3377 * This also handles the case of the "lost interrupt", where
3378 * for some reason (possibly hardware bug, possibly driver bug)
3379 * an interrupt was not delivered to the driver, even though the
3380 * transaction completed successfully.
3381 *
3382 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003383 * Inherited from SCSI layer (none, can sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 */
3385
3386static void ata_qc_timeout(struct ata_queued_cmd *qc)
3387{
3388 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003389 struct ata_host_set *host_set = ap->host_set;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 u8 host_stat = 0, drv_stat;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003391 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392
3393 DPRINTK("ENTER\n");
3394
Jeff Garzikb8f61532005-08-25 22:01:20 -04003395 spin_lock_irqsave(&host_set->lock, flags);
3396
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 /* hack alert! We cannot use the supplied completion
3398 * function from inside the ->eh_strategy_handler() thread.
3399 * libata is the only user of ->eh_strategy_handler() in
3400 * any kernel, so the default scsi_done() assumes it is
3401 * not being called from the SCSI EH.
3402 */
3403 qc->scsidone = scsi_finish_command;
3404
3405 switch (qc->tf.protocol) {
3406
3407 case ATA_PROT_DMA:
3408 case ATA_PROT_ATAPI_DMA:
3409 host_stat = ap->ops->bmdma_status(ap);
3410
3411 /* before we do anything else, clear DMA-Start bit */
Alan Coxb73fc892005-08-26 16:03:19 +01003412 ap->ops->bmdma_stop(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413
3414 /* fall through */
3415
3416 default:
3417 ata_altstatus(ap);
3418 drv_stat = ata_chk_status(ap);
3419
3420 /* ack bmdma irq events */
3421 ap->ops->irq_clear(ap);
3422
3423 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
3424 ap->id, qc->tf.command, drv_stat, host_stat);
3425
3426 /* complete taskfile transaction */
Albert Leea22e2eb2005-12-05 15:38:02 +08003427 qc->err_mask |= ac_err_mask(drv_stat);
3428 ata_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 break;
3430 }
Jeff Garzikb8f61532005-08-25 22:01:20 -04003431
3432 spin_unlock_irqrestore(&host_set->lock, flags);
3433
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434 DPRINTK("EXIT\n");
3435}
3436
3437/**
3438 * ata_eng_timeout - Handle timeout of queued command
3439 * @ap: Port on which timed-out command is active
3440 *
3441 * Some part of the kernel (currently, only the SCSI layer)
3442 * has noticed that the active command on port @ap has not
3443 * completed after a specified length of time. Handle this
3444 * condition by disabling DMA (if necessary) and completing
3445 * transactions, with error if necessary.
3446 *
3447 * This also handles the case of the "lost interrupt", where
3448 * for some reason (possibly hardware bug, possibly driver bug)
3449 * an interrupt was not delivered to the driver, even though the
3450 * transaction completed successfully.
3451 *
3452 * LOCKING:
3453 * Inherited from SCSI layer (none, can sleep)
3454 */
3455
3456void ata_eng_timeout(struct ata_port *ap)
3457{
3458 struct ata_queued_cmd *qc;
3459
3460 DPRINTK("ENTER\n");
3461
3462 qc = ata_qc_from_tag(ap, ap->active_tag);
Jeff Garzike12669e2005-10-05 18:39:23 -04003463 if (qc)
3464 ata_qc_timeout(qc);
3465 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466 printk(KERN_ERR "ata%u: BUG: timeout without command\n",
3467 ap->id);
3468 goto out;
3469 }
3470
Linus Torvalds1da177e2005-04-16 15:20:36 -07003471out:
3472 DPRINTK("EXIT\n");
3473}
3474
3475/**
3476 * ata_qc_new - Request an available ATA command, for queueing
3477 * @ap: Port associated with device @dev
3478 * @dev: Device from whom we request an available command structure
3479 *
3480 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003481 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482 */
3483
3484static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
3485{
3486 struct ata_queued_cmd *qc = NULL;
3487 unsigned int i;
3488
3489 for (i = 0; i < ATA_MAX_QUEUE; i++)
3490 if (!test_and_set_bit(i, &ap->qactive)) {
3491 qc = ata_qc_from_tag(ap, i);
3492 break;
3493 }
3494
3495 if (qc)
3496 qc->tag = i;
3497
3498 return qc;
3499}
3500
3501/**
3502 * ata_qc_new_init - Request an available ATA command, and initialize it
3503 * @ap: Port associated with device @dev
3504 * @dev: Device from whom we request an available command structure
3505 *
3506 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003507 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 */
3509
3510struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
3511 struct ata_device *dev)
3512{
3513 struct ata_queued_cmd *qc;
3514
3515 qc = ata_qc_new(ap);
3516 if (qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003517 qc->scsicmd = NULL;
3518 qc->ap = ap;
3519 qc->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05003521 ata_qc_reinit(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 }
3523
3524 return qc;
3525}
3526
Albert Leea22e2eb2005-12-05 15:38:02 +08003527int ata_qc_complete_noop(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003528{
3529 return 0;
3530}
3531
3532static void __ata_qc_complete(struct ata_queued_cmd *qc)
3533{
3534 struct ata_port *ap = qc->ap;
3535 unsigned int tag, do_clear = 0;
3536
3537 qc->flags = 0;
3538 tag = qc->tag;
3539 if (likely(ata_tag_valid(tag))) {
3540 if (tag == ap->active_tag)
3541 ap->active_tag = ATA_TAG_POISON;
3542 qc->tag = ATA_TAG_POISON;
3543 do_clear = 1;
3544 }
3545
3546 if (qc->waiting) {
3547 struct completion *waiting = qc->waiting;
3548 qc->waiting = NULL;
3549 complete(waiting);
3550 }
3551
3552 if (likely(do_clear))
3553 clear_bit(tag, &ap->qactive);
3554}
3555
3556/**
3557 * ata_qc_free - free unused ata_queued_cmd
3558 * @qc: Command to complete
3559 *
3560 * Designed to free unused ata_queued_cmd object
3561 * in case something prevents using it.
3562 *
3563 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003564 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565 */
3566void ata_qc_free(struct ata_queued_cmd *qc)
3567{
3568 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
3569 assert(qc->waiting == NULL); /* nothing should be waiting */
3570
3571 __ata_qc_complete(qc);
3572}
3573
3574/**
3575 * ata_qc_complete - Complete an active ATA command
3576 * @qc: Command to complete
Randy Dunlap8e8b77d2005-11-01 21:29:27 -08003577 * @err_mask: ATA Status register contents
Jeff Garzik0cba6322005-05-30 19:49:12 -04003578 *
3579 * Indicate to the mid and upper layers that an ATA
3580 * command has completed, with either an ok or not-ok status.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 *
3582 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003583 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 */
3585
Albert Leea22e2eb2005-12-05 15:38:02 +08003586void ata_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003587{
3588 int rc;
3589
3590 assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */
3591 assert(qc->flags & ATA_QCFLAG_ACTIVE);
3592
3593 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
3594 ata_sg_clean(qc);
3595
Albert Lee3f3791d2005-08-16 14:25:38 +08003596 /* atapi: mark qc as inactive to prevent the interrupt handler
3597 * from completing the command twice later, before the error handler
3598 * is called. (when rc != 0 and atapi request sense is needed)
3599 */
3600 qc->flags &= ~ATA_QCFLAG_ACTIVE;
3601
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602 /* call completion callback */
Albert Leea22e2eb2005-12-05 15:38:02 +08003603 rc = qc->complete_fn(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003604
3605 /* if callback indicates not to complete command (non-zero),
3606 * return immediately
3607 */
3608 if (rc != 0)
3609 return;
3610
3611 __ata_qc_complete(qc);
3612
3613 VPRINTK("EXIT\n");
3614}
3615
3616static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
3617{
3618 struct ata_port *ap = qc->ap;
3619
3620 switch (qc->tf.protocol) {
3621 case ATA_PROT_DMA:
3622 case ATA_PROT_ATAPI_DMA:
3623 return 1;
3624
3625 case ATA_PROT_ATAPI:
3626 case ATA_PROT_PIO:
3627 case ATA_PROT_PIO_MULT:
3628 if (ap->flags & ATA_FLAG_PIO_DMA)
3629 return 1;
3630
3631 /* fall through */
3632
3633 default:
3634 return 0;
3635 }
3636
3637 /* never reached */
3638}
3639
3640/**
3641 * ata_qc_issue - issue taskfile to device
3642 * @qc: command to issue to device
3643 *
3644 * Prepare an ATA command to submission to device.
3645 * This includes mapping the data into a DMA-able
3646 * area, filling in the S/G table, and finally
3647 * writing the taskfile to hardware, starting the command.
3648 *
3649 * LOCKING:
3650 * spin_lock_irqsave(host_set lock)
3651 *
3652 * RETURNS:
3653 * Zero on success, negative on error.
3654 */
3655
3656int ata_qc_issue(struct ata_queued_cmd *qc)
3657{
3658 struct ata_port *ap = qc->ap;
3659
3660 if (ata_should_dma_map(qc)) {
3661 if (qc->flags & ATA_QCFLAG_SG) {
3662 if (ata_sg_setup(qc))
3663 goto err_out;
3664 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
3665 if (ata_sg_setup_one(qc))
3666 goto err_out;
3667 }
3668 } else {
3669 qc->flags &= ~ATA_QCFLAG_DMAMAP;
3670 }
3671
3672 ap->ops->qc_prep(qc);
3673
3674 qc->ap->active_tag = qc->tag;
3675 qc->flags |= ATA_QCFLAG_ACTIVE;
3676
3677 return ap->ops->qc_issue(qc);
3678
3679err_out:
3680 return -1;
3681}
3682
Edward Falk0baab862005-06-02 18:17:13 -04003683
Linus Torvalds1da177e2005-04-16 15:20:36 -07003684/**
3685 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
3686 * @qc: command to issue to device
3687 *
3688 * Using various libata functions and hooks, this function
3689 * starts an ATA command. ATA commands are grouped into
3690 * classes called "protocols", and issuing each type of protocol
3691 * is slightly different.
3692 *
Edward Falk0baab862005-06-02 18:17:13 -04003693 * May be used as the qc_issue() entry in ata_port_operations.
3694 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695 * LOCKING:
3696 * spin_lock_irqsave(host_set lock)
3697 *
3698 * RETURNS:
3699 * Zero on success, negative on error.
3700 */
3701
3702int ata_qc_issue_prot(struct ata_queued_cmd *qc)
3703{
3704 struct ata_port *ap = qc->ap;
3705
3706 ata_dev_select(ap, qc->dev->devno, 1, 0);
3707
3708 switch (qc->tf.protocol) {
3709 case ATA_PROT_NODATA:
Jeff Garzike5338252005-10-30 21:37:17 -05003710 ata_tf_to_host(ap, &qc->tf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711 break;
3712
3713 case ATA_PROT_DMA:
3714 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3715 ap->ops->bmdma_setup(qc); /* set up bmdma */
3716 ap->ops->bmdma_start(qc); /* initiate bmdma */
3717 break;
3718
3719 case ATA_PROT_PIO: /* load tf registers, initiate polling pio */
3720 ata_qc_set_polling(qc);
Jeff Garzike5338252005-10-30 21:37:17 -05003721 ata_tf_to_host(ap, &qc->tf);
Albert Lee14be71f2005-09-27 17:36:35 +08003722 ap->hsm_task_state = HSM_ST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723 queue_work(ata_wq, &ap->pio_task);
3724 break;
3725
3726 case ATA_PROT_ATAPI:
3727 ata_qc_set_polling(qc);
Jeff Garzike5338252005-10-30 21:37:17 -05003728 ata_tf_to_host(ap, &qc->tf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729 queue_work(ata_wq, &ap->packet_task);
3730 break;
3731
3732 case ATA_PROT_ATAPI_NODATA:
Tejun Heoc1389502005-08-22 14:59:24 +09003733 ap->flags |= ATA_FLAG_NOINTR;
Jeff Garzike5338252005-10-30 21:37:17 -05003734 ata_tf_to_host(ap, &qc->tf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735 queue_work(ata_wq, &ap->packet_task);
3736 break;
3737
3738 case ATA_PROT_ATAPI_DMA:
Tejun Heoc1389502005-08-22 14:59:24 +09003739 ap->flags |= ATA_FLAG_NOINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3741 ap->ops->bmdma_setup(qc); /* set up bmdma */
3742 queue_work(ata_wq, &ap->packet_task);
3743 break;
3744
3745 default:
3746 WARN_ON(1);
3747 return -1;
3748 }
3749
3750 return 0;
3751}
3752
3753/**
Edward Falk0baab862005-06-02 18:17:13 -04003754 * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755 * @qc: Info associated with this ATA transaction.
3756 *
3757 * LOCKING:
3758 * spin_lock_irqsave(host_set lock)
3759 */
3760
3761static void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc)
3762{
3763 struct ata_port *ap = qc->ap;
3764 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3765 u8 dmactl;
3766 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3767
3768 /* load PRD table addr. */
3769 mb(); /* make sure PRD table writes are visible to controller */
3770 writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
3771
3772 /* specify data direction, triple-check start bit is clear */
3773 dmactl = readb(mmio + ATA_DMA_CMD);
3774 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3775 if (!rw)
3776 dmactl |= ATA_DMA_WR;
3777 writeb(dmactl, mmio + ATA_DMA_CMD);
3778
3779 /* issue r/w command */
3780 ap->ops->exec_command(ap, &qc->tf);
3781}
3782
3783/**
Alan Coxb73fc892005-08-26 16:03:19 +01003784 * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785 * @qc: Info associated with this ATA transaction.
3786 *
3787 * LOCKING:
3788 * spin_lock_irqsave(host_set lock)
3789 */
3790
3791static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
3792{
3793 struct ata_port *ap = qc->ap;
3794 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3795 u8 dmactl;
3796
3797 /* start host DMA transaction */
3798 dmactl = readb(mmio + ATA_DMA_CMD);
3799 writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
3800
3801 /* Strictly, one may wish to issue a readb() here, to
3802 * flush the mmio write. However, control also passes
3803 * to the hardware at this point, and it will interrupt
3804 * us when we are to resume control. So, in effect,
3805 * we don't care when the mmio write flushes.
3806 * Further, a read of the DMA status register _immediately_
3807 * following the write may not be what certain flaky hardware
3808 * is expected, so I think it is best to not add a readb()
3809 * without first all the MMIO ATA cards/mobos.
3810 * Or maybe I'm just being paranoid.
3811 */
3812}
3813
3814/**
3815 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
3816 * @qc: Info associated with this ATA transaction.
3817 *
3818 * LOCKING:
3819 * spin_lock_irqsave(host_set lock)
3820 */
3821
3822static void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
3823{
3824 struct ata_port *ap = qc->ap;
3825 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3826 u8 dmactl;
3827
3828 /* load PRD table addr. */
3829 outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
3830
3831 /* specify data direction, triple-check start bit is clear */
3832 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3833 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3834 if (!rw)
3835 dmactl |= ATA_DMA_WR;
3836 outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3837
3838 /* issue r/w command */
3839 ap->ops->exec_command(ap, &qc->tf);
3840}
3841
3842/**
3843 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
3844 * @qc: Info associated with this ATA transaction.
3845 *
3846 * LOCKING:
3847 * spin_lock_irqsave(host_set lock)
3848 */
3849
3850static void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
3851{
3852 struct ata_port *ap = qc->ap;
3853 u8 dmactl;
3854
3855 /* start host DMA transaction */
3856 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3857 outb(dmactl | ATA_DMA_START,
3858 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3859}
3860
Edward Falk0baab862005-06-02 18:17:13 -04003861
3862/**
3863 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
3864 * @qc: Info associated with this ATA transaction.
3865 *
3866 * Writes the ATA_DMA_START flag to the DMA command register.
3867 *
3868 * May be used as the bmdma_start() entry in ata_port_operations.
3869 *
3870 * LOCKING:
3871 * spin_lock_irqsave(host_set lock)
3872 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873void ata_bmdma_start(struct ata_queued_cmd *qc)
3874{
3875 if (qc->ap->flags & ATA_FLAG_MMIO)
3876 ata_bmdma_start_mmio(qc);
3877 else
3878 ata_bmdma_start_pio(qc);
3879}
3880
Edward Falk0baab862005-06-02 18:17:13 -04003881
3882/**
3883 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
3884 * @qc: Info associated with this ATA transaction.
3885 *
3886 * Writes address of PRD table to device's PRD Table Address
3887 * register, sets the DMA control register, and calls
3888 * ops->exec_command() to start the transfer.
3889 *
3890 * May be used as the bmdma_setup() entry in ata_port_operations.
3891 *
3892 * LOCKING:
3893 * spin_lock_irqsave(host_set lock)
3894 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895void ata_bmdma_setup(struct ata_queued_cmd *qc)
3896{
3897 if (qc->ap->flags & ATA_FLAG_MMIO)
3898 ata_bmdma_setup_mmio(qc);
3899 else
3900 ata_bmdma_setup_pio(qc);
3901}
3902
Edward Falk0baab862005-06-02 18:17:13 -04003903
3904/**
3905 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
Jeff Garzikdecc6d02005-06-02 18:42:33 -04003906 * @ap: Port associated with this ATA transaction.
Edward Falk0baab862005-06-02 18:17:13 -04003907 *
3908 * Clear interrupt and error flags in DMA status register.
3909 *
3910 * May be used as the irq_clear() entry in ata_port_operations.
3911 *
3912 * LOCKING:
3913 * spin_lock_irqsave(host_set lock)
3914 */
3915
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916void ata_bmdma_irq_clear(struct ata_port *ap)
3917{
3918 if (ap->flags & ATA_FLAG_MMIO) {
3919 void __iomem *mmio = ((void __iomem *) ap->ioaddr.bmdma_addr) + ATA_DMA_STATUS;
3920 writeb(readb(mmio), mmio);
3921 } else {
3922 unsigned long addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
3923 outb(inb(addr), addr);
3924 }
3925
3926}
3927
Edward Falk0baab862005-06-02 18:17:13 -04003928
3929/**
3930 * ata_bmdma_status - Read PCI IDE BMDMA status
Jeff Garzikdecc6d02005-06-02 18:42:33 -04003931 * @ap: Port associated with this ATA transaction.
Edward Falk0baab862005-06-02 18:17:13 -04003932 *
3933 * Read and return BMDMA status register.
3934 *
3935 * May be used as the bmdma_status() entry in ata_port_operations.
3936 *
3937 * LOCKING:
3938 * spin_lock_irqsave(host_set lock)
3939 */
3940
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941u8 ata_bmdma_status(struct ata_port *ap)
3942{
3943 u8 host_stat;
3944 if (ap->flags & ATA_FLAG_MMIO) {
3945 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3946 host_stat = readb(mmio + ATA_DMA_STATUS);
3947 } else
Albert Leeee500aa2005-09-27 17:34:38 +08003948 host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949 return host_stat;
3950}
3951
Edward Falk0baab862005-06-02 18:17:13 -04003952
3953/**
3954 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
Alan Coxb73fc892005-08-26 16:03:19 +01003955 * @qc: Command we are ending DMA for
Edward Falk0baab862005-06-02 18:17:13 -04003956 *
3957 * Clears the ATA_DMA_START flag in the dma control register
3958 *
3959 * May be used as the bmdma_stop() entry in ata_port_operations.
3960 *
3961 * LOCKING:
3962 * spin_lock_irqsave(host_set lock)
3963 */
3964
Alan Coxb73fc892005-08-26 16:03:19 +01003965void ata_bmdma_stop(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966{
Alan Coxb73fc892005-08-26 16:03:19 +01003967 struct ata_port *ap = qc->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003968 if (ap->flags & ATA_FLAG_MMIO) {
3969 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3970
3971 /* clear start/stop bit */
3972 writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
3973 mmio + ATA_DMA_CMD);
3974 } else {
3975 /* clear start/stop bit */
3976 outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
3977 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3978 }
3979
3980 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
3981 ata_altstatus(ap); /* dummy read */
3982}
3983
3984/**
3985 * ata_host_intr - Handle host interrupt for given (port, task)
3986 * @ap: Port on which interrupt arrived (possibly...)
3987 * @qc: Taskfile currently active in engine
3988 *
3989 * Handle host interrupt for given queued command. Currently,
3990 * only DMA interrupts are handled. All other commands are
3991 * handled via polling with interrupts disabled (nIEN bit).
3992 *
3993 * LOCKING:
3994 * spin_lock_irqsave(host_set lock)
3995 *
3996 * RETURNS:
3997 * One if interrupt was handled, zero if not (shared irq).
3998 */
3999
4000inline unsigned int ata_host_intr (struct ata_port *ap,
4001 struct ata_queued_cmd *qc)
4002{
4003 u8 status, host_stat;
4004
4005 switch (qc->tf.protocol) {
4006
4007 case ATA_PROT_DMA:
4008 case ATA_PROT_ATAPI_DMA:
4009 case ATA_PROT_ATAPI:
4010 /* check status of DMA engine */
4011 host_stat = ap->ops->bmdma_status(ap);
4012 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
4013
4014 /* if it's not our irq... */
4015 if (!(host_stat & ATA_DMA_INTR))
4016 goto idle_irq;
4017
4018 /* before we do anything else, clear DMA-Start bit */
Alan Coxb73fc892005-08-26 16:03:19 +01004019 ap->ops->bmdma_stop(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020
4021 /* fall through */
4022
4023 case ATA_PROT_ATAPI_NODATA:
4024 case ATA_PROT_NODATA:
4025 /* check altstatus */
4026 status = ata_altstatus(ap);
4027 if (status & ATA_BUSY)
4028 goto idle_irq;
4029
4030 /* check main status, clearing INTRQ */
4031 status = ata_chk_status(ap);
4032 if (unlikely(status & ATA_BUSY))
4033 goto idle_irq;
4034 DPRINTK("ata%u: protocol %d (dev_stat 0x%X)\n",
4035 ap->id, qc->tf.protocol, status);
4036
4037 /* ack bmdma irq events */
4038 ap->ops->irq_clear(ap);
4039
4040 /* complete taskfile transaction */
Albert Leea22e2eb2005-12-05 15:38:02 +08004041 qc->err_mask |= ac_err_mask(status);
4042 ata_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043 break;
4044
4045 default:
4046 goto idle_irq;
4047 }
4048
4049 return 1; /* irq handled */
4050
4051idle_irq:
4052 ap->stats.idle_irq++;
4053
4054#ifdef ATA_IRQ_TRAP
4055 if ((ap->stats.idle_irq % 1000) == 0) {
4056 handled = 1;
4057 ata_irq_ack(ap, 0); /* debug trap */
4058 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
4059 }
4060#endif
4061 return 0; /* irq not handled */
4062}
4063
4064/**
4065 * ata_interrupt - Default ATA host interrupt handler
Jeff Garzik0cba6322005-05-30 19:49:12 -04004066 * @irq: irq line (unused)
4067 * @dev_instance: pointer to our ata_host_set information structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068 * @regs: unused
4069 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004070 * Default interrupt handler for PCI IDE devices. Calls
4071 * ata_host_intr() for each port that is not disabled.
4072 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004074 * Obtains host_set lock during operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075 *
4076 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004077 * IRQ_NONE or IRQ_HANDLED.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078 */
4079
4080irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
4081{
4082 struct ata_host_set *host_set = dev_instance;
4083 unsigned int i;
4084 unsigned int handled = 0;
4085 unsigned long flags;
4086
4087 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
4088 spin_lock_irqsave(&host_set->lock, flags);
4089
4090 for (i = 0; i < host_set->n_ports; i++) {
4091 struct ata_port *ap;
4092
4093 ap = host_set->ports[i];
Tejun Heoc1389502005-08-22 14:59:24 +09004094 if (ap &&
4095 !(ap->flags & (ATA_FLAG_PORT_DISABLED | ATA_FLAG_NOINTR))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096 struct ata_queued_cmd *qc;
4097
4098 qc = ata_qc_from_tag(ap, ap->active_tag);
Albert Lee21b1ed72005-04-29 17:34:59 +08004099 if (qc && (!(qc->tf.ctl & ATA_NIEN)) &&
4100 (qc->flags & ATA_QCFLAG_ACTIVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101 handled |= ata_host_intr(ap, qc);
4102 }
4103 }
4104
4105 spin_unlock_irqrestore(&host_set->lock, flags);
4106
4107 return IRQ_RETVAL(handled);
4108}
4109
4110/**
4111 * atapi_packet_task - Write CDB bytes to hardware
4112 * @_data: Port to which ATAPI device is attached.
4113 *
4114 * When device has indicated its readiness to accept
4115 * a CDB, this function is called. Send the CDB.
4116 * If DMA is to be performed, exit immediately.
4117 * Otherwise, we are in polling mode, so poll
4118 * status under operation succeeds or fails.
4119 *
4120 * LOCKING:
4121 * Kernel thread context (may sleep)
4122 */
4123
4124static void atapi_packet_task(void *_data)
4125{
4126 struct ata_port *ap = _data;
4127 struct ata_queued_cmd *qc;
4128 u8 status;
4129
4130 qc = ata_qc_from_tag(ap, ap->active_tag);
4131 assert(qc != NULL);
4132 assert(qc->flags & ATA_QCFLAG_ACTIVE);
4133
4134 /* sleep-wait for BSY to clear */
4135 DPRINTK("busy wait\n");
Albert Leed8fe4522005-12-05 15:42:17 +08004136 if (ata_busy_sleep(ap, ATA_TMOUT_CDB_QUICK, ATA_TMOUT_CDB)) {
4137 qc->err_mask |= AC_ERR_ATA_BUS;
4138 goto err_out;
4139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140
4141 /* make sure DRQ is set */
4142 status = ata_chk_status(ap);
Albert Leed8fe4522005-12-05 15:42:17 +08004143 if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ) {
4144 qc->err_mask |= AC_ERR_ATA_BUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145 goto err_out;
Albert Leed8fe4522005-12-05 15:42:17 +08004146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147
4148 /* send SCSI cdb */
4149 DPRINTK("send cdb\n");
4150 assert(ap->cdb_len >= 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151
Tejun Heoc1389502005-08-22 14:59:24 +09004152 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA ||
4153 qc->tf.protocol == ATA_PROT_ATAPI_NODATA) {
4154 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155
Tejun Heoc1389502005-08-22 14:59:24 +09004156 /* Once we're done issuing command and kicking bmdma,
4157 * irq handler takes over. To not lose irq, we need
4158 * to clear NOINTR flag before sending cdb, but
4159 * interrupt handler shouldn't be invoked before we're
4160 * finished. Hence, the following locking.
4161 */
4162 spin_lock_irqsave(&ap->host_set->lock, flags);
4163 ap->flags &= ~ATA_FLAG_NOINTR;
4164 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
4165 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA)
4166 ap->ops->bmdma_start(qc); /* initiate bmdma */
4167 spin_unlock_irqrestore(&ap->host_set->lock, flags);
4168 } else {
4169 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170
Tejun Heoc1389502005-08-22 14:59:24 +09004171 /* PIO commands are handled by polling */
Albert Lee14be71f2005-09-27 17:36:35 +08004172 ap->hsm_task_state = HSM_ST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173 queue_work(ata_wq, &ap->pio_task);
4174 }
4175
4176 return;
4177
4178err_out:
Albert Leea22e2eb2005-12-05 15:38:02 +08004179 ata_poll_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180}
4181
Edward Falk0baab862005-06-02 18:17:13 -04004182
4183/**
4184 * ata_port_start - Set port up for dma.
4185 * @ap: Port to initialize
4186 *
4187 * Called just after data structures for each port are
4188 * initialized. Allocates space for PRD table.
4189 *
4190 * May be used as the port_start() entry in ata_port_operations.
4191 *
4192 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004193 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04004194 */
4195
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196int ata_port_start (struct ata_port *ap)
4197{
4198 struct device *dev = ap->host_set->dev;
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004199 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200
4201 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
4202 if (!ap->prd)
4203 return -ENOMEM;
4204
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004205 rc = ata_pad_alloc(ap, dev);
4206 if (rc) {
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004207 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004208 return rc;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004209 }
4210
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
4212
4213 return 0;
4214}
4215
Edward Falk0baab862005-06-02 18:17:13 -04004216
4217/**
4218 * ata_port_stop - Undo ata_port_start()
4219 * @ap: Port to shut down
4220 *
4221 * Frees the PRD table.
4222 *
4223 * May be used as the port_stop() entry in ata_port_operations.
4224 *
4225 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004226 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04004227 */
4228
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229void ata_port_stop (struct ata_port *ap)
4230{
4231 struct device *dev = ap->host_set->dev;
4232
4233 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004234 ata_pad_free(ap, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235}
4236
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04004237void ata_host_stop (struct ata_host_set *host_set)
4238{
4239 if (host_set->mmio_base)
4240 iounmap(host_set->mmio_base);
4241}
4242
4243
Linus Torvalds1da177e2005-04-16 15:20:36 -07004244/**
4245 * ata_host_remove - Unregister SCSI host structure with upper layers
4246 * @ap: Port to unregister
4247 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
4248 *
4249 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004250 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251 */
4252
4253static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
4254{
4255 struct Scsi_Host *sh = ap->host;
4256
4257 DPRINTK("ENTER\n");
4258
4259 if (do_unregister)
4260 scsi_remove_host(sh);
4261
4262 ap->ops->port_stop(ap);
4263}
4264
4265/**
4266 * ata_host_init - Initialize an ata_port structure
4267 * @ap: Structure to initialize
4268 * @host: associated SCSI mid-layer structure
4269 * @host_set: Collection of hosts to which @ap belongs
4270 * @ent: Probe information provided by low-level driver
4271 * @port_no: Port number associated with this ata_port
4272 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004273 * Initialize a new ata_port structure, and its associated
4274 * scsi_host.
4275 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004277 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278 */
4279
4280static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
4281 struct ata_host_set *host_set,
Jeff Garzik057ace52005-10-22 14:27:05 -04004282 const struct ata_probe_ent *ent, unsigned int port_no)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283{
4284 unsigned int i;
4285
4286 host->max_id = 16;
4287 host->max_lun = 1;
4288 host->max_channel = 1;
4289 host->unique_id = ata_unique_id++;
4290 host->max_cmd_len = 12;
Christoph Hellwig12413192005-06-11 01:05:01 +02004291
Linus Torvalds1da177e2005-04-16 15:20:36 -07004292 ap->flags = ATA_FLAG_PORT_DISABLED;
4293 ap->id = host->unique_id;
4294 ap->host = host;
4295 ap->ctl = ATA_DEVCTL_OBS;
4296 ap->host_set = host_set;
4297 ap->port_no = port_no;
4298 ap->hard_port_no =
4299 ent->legacy_mode ? ent->hard_port_no : port_no;
4300 ap->pio_mask = ent->pio_mask;
4301 ap->mwdma_mask = ent->mwdma_mask;
4302 ap->udma_mask = ent->udma_mask;
4303 ap->flags |= ent->host_flags;
4304 ap->ops = ent->port_ops;
4305 ap->cbl = ATA_CBL_NONE;
4306 ap->active_tag = ATA_TAG_POISON;
4307 ap->last_ctl = 0xFF;
4308
4309 INIT_WORK(&ap->packet_task, atapi_packet_task, ap);
4310 INIT_WORK(&ap->pio_task, ata_pio_task, ap);
4311
4312 for (i = 0; i < ATA_MAX_DEVICES; i++)
4313 ap->device[i].devno = i;
4314
4315#ifdef ATA_IRQ_TRAP
4316 ap->stats.unhandled_irq = 1;
4317 ap->stats.idle_irq = 1;
4318#endif
4319
4320 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
4321}
4322
4323/**
4324 * ata_host_add - Attach low-level ATA driver to system
4325 * @ent: Information provided by low-level driver
4326 * @host_set: Collections of ports to which we add
4327 * @port_no: Port number associated with this host
4328 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004329 * Attach low-level ATA driver to system.
4330 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004332 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333 *
4334 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004335 * New ata_port on success, for NULL on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336 */
4337
Jeff Garzik057ace52005-10-22 14:27:05 -04004338static struct ata_port * ata_host_add(const struct ata_probe_ent *ent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339 struct ata_host_set *host_set,
4340 unsigned int port_no)
4341{
4342 struct Scsi_Host *host;
4343 struct ata_port *ap;
4344 int rc;
4345
4346 DPRINTK("ENTER\n");
4347 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
4348 if (!host)
4349 return NULL;
4350
4351 ap = (struct ata_port *) &host->hostdata[0];
4352
4353 ata_host_init(ap, host, host_set, ent, port_no);
4354
4355 rc = ap->ops->port_start(ap);
4356 if (rc)
4357 goto err_out;
4358
4359 return ap;
4360
4361err_out:
4362 scsi_host_put(host);
4363 return NULL;
4364}
4365
4366/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004367 * ata_device_add - Register hardware device with ATA and SCSI layers
4368 * @ent: Probe information describing hardware device to be registered
4369 *
4370 * This function processes the information provided in the probe
4371 * information struct @ent, allocates the necessary ATA and SCSI
4372 * host information structures, initializes them, and registers
4373 * everything with requisite kernel subsystems.
4374 *
4375 * This function requests irqs, probes the ATA bus, and probes
4376 * the SCSI bus.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377 *
4378 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004379 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380 *
4381 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004382 * Number of ports registered. Zero on error (no ports registered).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383 */
4384
Jeff Garzik057ace52005-10-22 14:27:05 -04004385int ata_device_add(const struct ata_probe_ent *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386{
4387 unsigned int count = 0, i;
4388 struct device *dev = ent->dev;
4389 struct ata_host_set *host_set;
4390
4391 DPRINTK("ENTER\n");
4392 /* alloc a container for our list of ATA ports (buses) */
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004393 host_set = kzalloc(sizeof(struct ata_host_set) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
4395 if (!host_set)
4396 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397 spin_lock_init(&host_set->lock);
4398
4399 host_set->dev = dev;
4400 host_set->n_ports = ent->n_ports;
4401 host_set->irq = ent->irq;
4402 host_set->mmio_base = ent->mmio_base;
4403 host_set->private_data = ent->private_data;
4404 host_set->ops = ent->port_ops;
4405
4406 /* register each port bound to this device */
4407 for (i = 0; i < ent->n_ports; i++) {
4408 struct ata_port *ap;
4409 unsigned long xfer_mode_mask;
4410
4411 ap = ata_host_add(ent, host_set, i);
4412 if (!ap)
4413 goto err_out;
4414
4415 host_set->ports[i] = ap;
4416 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
4417 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
4418 (ap->pio_mask << ATA_SHIFT_PIO);
4419
4420 /* print per-port info to dmesg */
4421 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
4422 "bmdma 0x%lX irq %lu\n",
4423 ap->id,
4424 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
4425 ata_mode_string(xfer_mode_mask),
4426 ap->ioaddr.cmd_addr,
4427 ap->ioaddr.ctl_addr,
4428 ap->ioaddr.bmdma_addr,
4429 ent->irq);
4430
4431 ata_chk_status(ap);
4432 host_set->ops->irq_clear(ap);
4433 count++;
4434 }
4435
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004436 if (!count)
4437 goto err_free_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438
4439 /* obtain irq, that is shared between channels */
4440 if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
4441 DRV_NAME, host_set))
4442 goto err_out;
4443
4444 /* perform each probe synchronously */
4445 DPRINTK("probe begin\n");
4446 for (i = 0; i < count; i++) {
4447 struct ata_port *ap;
4448 int rc;
4449
4450 ap = host_set->ports[i];
4451
4452 DPRINTK("ata%u: probe begin\n", ap->id);
4453 rc = ata_bus_probe(ap);
4454 DPRINTK("ata%u: probe end\n", ap->id);
4455
4456 if (rc) {
4457 /* FIXME: do something useful here?
4458 * Current libata behavior will
4459 * tear down everything when
4460 * the module is removed
4461 * or the h/w is unplugged.
4462 */
4463 }
4464
4465 rc = scsi_add_host(ap->host, dev);
4466 if (rc) {
4467 printk(KERN_ERR "ata%u: scsi_add_host failed\n",
4468 ap->id);
4469 /* FIXME: do something useful here */
4470 /* FIXME: handle unconditional calls to
4471 * scsi_scan_host and ata_host_remove, below,
4472 * at the very least
4473 */
4474 }
4475 }
4476
4477 /* probes are done, now scan each port's disk(s) */
4478 DPRINTK("probe begin\n");
4479 for (i = 0; i < count; i++) {
4480 struct ata_port *ap = host_set->ports[i];
4481
Jeff Garzik644dd0c2005-10-03 15:55:19 -04004482 ata_scsi_scan_host(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483 }
4484
4485 dev_set_drvdata(dev, host_set);
4486
4487 VPRINTK("EXIT, returning %u\n", ent->n_ports);
4488 return ent->n_ports; /* success */
4489
4490err_out:
4491 for (i = 0; i < count; i++) {
4492 ata_host_remove(host_set->ports[i], 1);
4493 scsi_host_put(host_set->ports[i]->host);
4494 }
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004495err_free_ret:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496 kfree(host_set);
4497 VPRINTK("EXIT, returning 0\n");
4498 return 0;
4499}
4500
4501/**
Alan Cox17b14452005-09-15 15:44:00 +01004502 * ata_host_set_remove - PCI layer callback for device removal
4503 * @host_set: ATA host set that was removed
4504 *
4505 * Unregister all objects associated with this host set. Free those
4506 * objects.
4507 *
4508 * LOCKING:
4509 * Inherited from calling layer (may sleep).
4510 */
4511
Alan Cox17b14452005-09-15 15:44:00 +01004512void ata_host_set_remove(struct ata_host_set *host_set)
4513{
4514 struct ata_port *ap;
4515 unsigned int i;
4516
4517 for (i = 0; i < host_set->n_ports; i++) {
4518 ap = host_set->ports[i];
4519 scsi_remove_host(ap->host);
4520 }
4521
4522 free_irq(host_set->irq, host_set);
4523
4524 for (i = 0; i < host_set->n_ports; i++) {
4525 ap = host_set->ports[i];
4526
4527 ata_scsi_release(ap->host);
4528
4529 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
4530 struct ata_ioports *ioaddr = &ap->ioaddr;
4531
4532 if (ioaddr->cmd_addr == 0x1f0)
4533 release_region(0x1f0, 8);
4534 else if (ioaddr->cmd_addr == 0x170)
4535 release_region(0x170, 8);
4536 }
4537
4538 scsi_host_put(ap->host);
4539 }
4540
4541 if (host_set->ops->host_stop)
4542 host_set->ops->host_stop(host_set);
4543
4544 kfree(host_set);
4545}
4546
4547/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548 * ata_scsi_release - SCSI layer callback hook for host unload
4549 * @host: libata host to be unloaded
4550 *
4551 * Performs all duties necessary to shut down a libata port...
4552 * Kill port kthread, disable port, and release resources.
4553 *
4554 * LOCKING:
4555 * Inherited from SCSI layer.
4556 *
4557 * RETURNS:
4558 * One.
4559 */
4560
4561int ata_scsi_release(struct Scsi_Host *host)
4562{
4563 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
4564
4565 DPRINTK("ENTER\n");
4566
4567 ap->ops->port_disable(ap);
4568 ata_host_remove(ap, 0);
4569
4570 DPRINTK("EXIT\n");
4571 return 1;
4572}
4573
4574/**
4575 * ata_std_ports - initialize ioaddr with standard port offsets.
4576 * @ioaddr: IO address structure to be initialized
Edward Falk0baab862005-06-02 18:17:13 -04004577 *
4578 * Utility function which initializes data_addr, error_addr,
4579 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
4580 * device_addr, status_addr, and command_addr to standard offsets
4581 * relative to cmd_addr.
4582 *
4583 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584 */
Edward Falk0baab862005-06-02 18:17:13 -04004585
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586void ata_std_ports(struct ata_ioports *ioaddr)
4587{
4588 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
4589 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
4590 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
4591 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
4592 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
4593 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
4594 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
4595 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
4596 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
4597 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
4598}
4599
4600static struct ata_probe_ent *
Jeff Garzik057ace52005-10-22 14:27:05 -04004601ata_probe_ent_alloc(struct device *dev, const struct ata_port_info *port)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602{
4603 struct ata_probe_ent *probe_ent;
4604
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004605 probe_ent = kzalloc(sizeof(*probe_ent), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606 if (!probe_ent) {
4607 printk(KERN_ERR DRV_NAME "(%s): out of memory\n",
4608 kobject_name(&(dev->kobj)));
4609 return NULL;
4610 }
4611
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612 INIT_LIST_HEAD(&probe_ent->node);
4613 probe_ent->dev = dev;
4614
4615 probe_ent->sht = port->sht;
4616 probe_ent->host_flags = port->host_flags;
4617 probe_ent->pio_mask = port->pio_mask;
4618 probe_ent->mwdma_mask = port->mwdma_mask;
4619 probe_ent->udma_mask = port->udma_mask;
4620 probe_ent->port_ops = port->port_ops;
4621
4622 return probe_ent;
4623}
4624
Edward Falk0baab862005-06-02 18:17:13 -04004625
4626
Jeff Garzik374b1872005-08-30 05:42:52 -04004627#ifdef CONFIG_PCI
4628
4629void ata_pci_host_stop (struct ata_host_set *host_set)
4630{
4631 struct pci_dev *pdev = to_pci_dev(host_set->dev);
4632
4633 pci_iounmap(pdev, host_set->mmio_base);
4634}
4635
Edward Falk0baab862005-06-02 18:17:13 -04004636/**
4637 * ata_pci_init_native_mode - Initialize native-mode driver
4638 * @pdev: pci device to be initialized
4639 * @port: array[2] of pointers to port info structures.
Alan Cox47a86592005-10-04 08:09:19 -04004640 * @ports: bitmap of ports present
Edward Falk0baab862005-06-02 18:17:13 -04004641 *
4642 * Utility function which allocates and initializes an
4643 * ata_probe_ent structure for a standard dual-port
4644 * PIO-based IDE controller. The returned ata_probe_ent
4645 * structure can be passed to ata_device_add(). The returned
4646 * ata_probe_ent structure should then be freed with kfree().
Alan Cox47a86592005-10-04 08:09:19 -04004647 *
4648 * The caller need only pass the address of the primary port, the
4649 * secondary will be deduced automatically. If the device has non
4650 * standard secondary port mappings this function can be called twice,
4651 * once for each interface.
Edward Falk0baab862005-06-02 18:17:13 -04004652 */
4653
Linus Torvalds1da177e2005-04-16 15:20:36 -07004654struct ata_probe_ent *
Alan Cox47a86592005-10-04 08:09:19 -04004655ata_pci_init_native_mode(struct pci_dev *pdev, struct ata_port_info **port, int ports)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656{
4657 struct ata_probe_ent *probe_ent =
4658 ata_probe_ent_alloc(pci_dev_to_dev(pdev), port[0]);
Alan Cox47a86592005-10-04 08:09:19 -04004659 int p = 0;
4660
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661 if (!probe_ent)
4662 return NULL;
4663
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664 probe_ent->irq = pdev->irq;
4665 probe_ent->irq_flags = SA_SHIRQ;
Alan Coxe99f8b52005-11-08 14:09:44 +00004666 probe_ent->private_data = port[0]->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667
Alan Cox47a86592005-10-04 08:09:19 -04004668 if (ports & ATA_PORT_PRIMARY) {
4669 probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 0);
4670 probe_ent->port[p].altstatus_addr =
4671 probe_ent->port[p].ctl_addr =
4672 pci_resource_start(pdev, 1) | ATA_PCI_CTL_OFS;
4673 probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4);
4674 ata_std_ports(&probe_ent->port[p]);
4675 p++;
4676 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677
Alan Cox47a86592005-10-04 08:09:19 -04004678 if (ports & ATA_PORT_SECONDARY) {
4679 probe_ent->port[p].cmd_addr = pci_resource_start(pdev, 2);
4680 probe_ent->port[p].altstatus_addr =
4681 probe_ent->port[p].ctl_addr =
4682 pci_resource_start(pdev, 3) | ATA_PCI_CTL_OFS;
4683 probe_ent->port[p].bmdma_addr = pci_resource_start(pdev, 4) + 8;
4684 ata_std_ports(&probe_ent->port[p]);
4685 p++;
4686 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687
Alan Cox47a86592005-10-04 08:09:19 -04004688 probe_ent->n_ports = p;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689 return probe_ent;
4690}
4691
Jeff Garzik0f0d5192005-10-30 06:41:29 -05004692static 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 -07004693{
Alan Cox47a86592005-10-04 08:09:19 -04004694 struct ata_probe_ent *probe_ent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695
Jeff Garzik0f0d5192005-10-30 06:41:29 -05004696 probe_ent = ata_probe_ent_alloc(pci_dev_to_dev(pdev), port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697 if (!probe_ent)
4698 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700 probe_ent->legacy_mode = 1;
Alan Cox47a86592005-10-04 08:09:19 -04004701 probe_ent->n_ports = 1;
4702 probe_ent->hard_port_no = port_num;
Alan Coxe99f8b52005-11-08 14:09:44 +00004703 probe_ent->private_data = port->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004704
Alan Cox47a86592005-10-04 08:09:19 -04004705 switch(port_num)
4706 {
4707 case 0:
4708 probe_ent->irq = 14;
4709 probe_ent->port[0].cmd_addr = 0x1f0;
4710 probe_ent->port[0].altstatus_addr =
4711 probe_ent->port[0].ctl_addr = 0x3f6;
4712 break;
4713 case 1:
4714 probe_ent->irq = 15;
4715 probe_ent->port[0].cmd_addr = 0x170;
4716 probe_ent->port[0].altstatus_addr =
4717 probe_ent->port[0].ctl_addr = 0x376;
4718 break;
4719 }
4720 probe_ent->port[0].bmdma_addr = pci_resource_start(pdev, 4) + 8 * port_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721 ata_std_ports(&probe_ent->port[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722 return probe_ent;
4723}
4724
4725/**
4726 * ata_pci_init_one - Initialize/register PCI IDE host controller
4727 * @pdev: Controller to be initialized
4728 * @port_info: Information from low-level host driver
4729 * @n_ports: Number of ports attached to host controller
4730 *
Edward Falk0baab862005-06-02 18:17:13 -04004731 * This is a helper function which can be called from a driver's
4732 * xxx_init_one() probe function if the hardware uses traditional
4733 * IDE taskfile registers.
4734 *
4735 * This function calls pci_enable_device(), reserves its register
4736 * regions, sets the dma mask, enables bus master mode, and calls
4737 * ata_device_add()
4738 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739 * LOCKING:
4740 * Inherited from PCI layer (may sleep).
4741 *
4742 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004743 * Zero on success, negative on errno-based value on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744 */
4745
4746int ata_pci_init_one (struct pci_dev *pdev, struct ata_port_info **port_info,
4747 unsigned int n_ports)
4748{
Alan Cox47a86592005-10-04 08:09:19 -04004749 struct ata_probe_ent *probe_ent = NULL, *probe_ent2 = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750 struct ata_port_info *port[2];
4751 u8 tmp8, mask;
4752 unsigned int legacy_mode = 0;
4753 int disable_dev_on_err = 1;
4754 int rc;
4755
4756 DPRINTK("ENTER\n");
4757
4758 port[0] = port_info[0];
4759 if (n_ports > 1)
4760 port[1] = port_info[1];
4761 else
4762 port[1] = port[0];
4763
4764 if ((port[0]->host_flags & ATA_FLAG_NO_LEGACY) == 0
4765 && (pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) {
Alan Cox47a86592005-10-04 08:09:19 -04004766 /* TODO: What if one channel is in native mode ... */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767 pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8);
4768 mask = (1 << 2) | (1 << 0);
4769 if ((tmp8 & mask) != mask)
4770 legacy_mode = (1 << 3);
4771 }
4772
4773 /* FIXME... */
Alan Cox47a86592005-10-04 08:09:19 -04004774 if ((!legacy_mode) && (n_ports > 2)) {
4775 printk(KERN_ERR "ata: BUG: native mode, n_ports > 2\n");
4776 n_ports = 2;
4777 /* For now */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778 }
4779
Alan Cox47a86592005-10-04 08:09:19 -04004780 /* FIXME: Really for ATA it isn't safe because the device may be
4781 multi-purpose and we want to leave it alone if it was already
4782 enabled. Secondly for shared use as Arjan says we want refcounting
4783
4784 Checking dev->is_enabled is insufficient as this is not set at
4785 boot for the primary video which is BIOS enabled
4786 */
4787
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788 rc = pci_enable_device(pdev);
4789 if (rc)
4790 return rc;
4791
4792 rc = pci_request_regions(pdev, DRV_NAME);
4793 if (rc) {
4794 disable_dev_on_err = 0;
4795 goto err_out;
4796 }
4797
Alan Cox47a86592005-10-04 08:09:19 -04004798 /* FIXME: Should use platform specific mappers for legacy port ranges */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004799 if (legacy_mode) {
4800 if (!request_region(0x1f0, 8, "libata")) {
4801 struct resource *conflict, res;
4802 res.start = 0x1f0;
4803 res.end = 0x1f0 + 8 - 1;
4804 conflict = ____request_resource(&ioport_resource, &res);
4805 if (!strcmp(conflict->name, "libata"))
4806 legacy_mode |= (1 << 0);
4807 else {
4808 disable_dev_on_err = 0;
4809 printk(KERN_WARNING "ata: 0x1f0 IDE port busy\n");
4810 }
4811 } else
4812 legacy_mode |= (1 << 0);
4813
4814 if (!request_region(0x170, 8, "libata")) {
4815 struct resource *conflict, res;
4816 res.start = 0x170;
4817 res.end = 0x170 + 8 - 1;
4818 conflict = ____request_resource(&ioport_resource, &res);
4819 if (!strcmp(conflict->name, "libata"))
4820 legacy_mode |= (1 << 1);
4821 else {
4822 disable_dev_on_err = 0;
4823 printk(KERN_WARNING "ata: 0x170 IDE port busy\n");
4824 }
4825 } else
4826 legacy_mode |= (1 << 1);
4827 }
4828
4829 /* we have legacy mode, but all ports are unavailable */
4830 if (legacy_mode == (1 << 3)) {
4831 rc = -EBUSY;
4832 goto err_out_regions;
4833 }
4834
4835 rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
4836 if (rc)
4837 goto err_out_regions;
4838 rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
4839 if (rc)
4840 goto err_out_regions;
4841
4842 if (legacy_mode) {
Alan Cox47a86592005-10-04 08:09:19 -04004843 if (legacy_mode & (1 << 0))
Jeff Garzik0f0d5192005-10-30 06:41:29 -05004844 probe_ent = ata_pci_init_legacy_port(pdev, port[0], 0);
Alan Cox47a86592005-10-04 08:09:19 -04004845 if (legacy_mode & (1 << 1))
Jeff Garzik0f0d5192005-10-30 06:41:29 -05004846 probe_ent2 = ata_pci_init_legacy_port(pdev, port[1], 1);
Alan Cox47a86592005-10-04 08:09:19 -04004847 } else {
4848 if (n_ports == 2)
4849 probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY | ATA_PORT_SECONDARY);
4850 else
4851 probe_ent = ata_pci_init_native_mode(pdev, port, ATA_PORT_PRIMARY);
4852 }
4853 if (!probe_ent && !probe_ent2) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854 rc = -ENOMEM;
4855 goto err_out_regions;
4856 }
4857
4858 pci_set_master(pdev);
4859
4860 /* FIXME: check ata_device_add return */
4861 if (legacy_mode) {
4862 if (legacy_mode & (1 << 0))
4863 ata_device_add(probe_ent);
4864 if (legacy_mode & (1 << 1))
4865 ata_device_add(probe_ent2);
4866 } else
4867 ata_device_add(probe_ent);
4868
4869 kfree(probe_ent);
4870 kfree(probe_ent2);
4871
4872 return 0;
4873
4874err_out_regions:
4875 if (legacy_mode & (1 << 0))
4876 release_region(0x1f0, 8);
4877 if (legacy_mode & (1 << 1))
4878 release_region(0x170, 8);
4879 pci_release_regions(pdev);
4880err_out:
4881 if (disable_dev_on_err)
4882 pci_disable_device(pdev);
4883 return rc;
4884}
4885
4886/**
4887 * ata_pci_remove_one - PCI layer callback for device removal
4888 * @pdev: PCI device that was removed
4889 *
4890 * PCI layer indicates to libata via this hook that
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004891 * hot-unplug or module unload event has occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004892 * Handle this by unregistering all objects associated
4893 * with this PCI device. Free those objects. Then finally
4894 * release PCI resources and disable device.
4895 *
4896 * LOCKING:
4897 * Inherited from PCI layer (may sleep).
4898 */
4899
4900void ata_pci_remove_one (struct pci_dev *pdev)
4901{
4902 struct device *dev = pci_dev_to_dev(pdev);
4903 struct ata_host_set *host_set = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904
Alan Cox17b14452005-09-15 15:44:00 +01004905 ata_host_set_remove(host_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906 pci_release_regions(pdev);
4907 pci_disable_device(pdev);
4908 dev_set_drvdata(dev, NULL);
4909}
4910
4911/* move to PCI subsystem */
Jeff Garzik057ace52005-10-22 14:27:05 -04004912int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913{
4914 unsigned long tmp = 0;
4915
4916 switch (bits->width) {
4917 case 1: {
4918 u8 tmp8 = 0;
4919 pci_read_config_byte(pdev, bits->reg, &tmp8);
4920 tmp = tmp8;
4921 break;
4922 }
4923 case 2: {
4924 u16 tmp16 = 0;
4925 pci_read_config_word(pdev, bits->reg, &tmp16);
4926 tmp = tmp16;
4927 break;
4928 }
4929 case 4: {
4930 u32 tmp32 = 0;
4931 pci_read_config_dword(pdev, bits->reg, &tmp32);
4932 tmp = tmp32;
4933 break;
4934 }
4935
4936 default:
4937 return -EINVAL;
4938 }
4939
4940 tmp &= bits->mask;
4941
4942 return (tmp == bits->val) ? 1 : 0;
4943}
4944#endif /* CONFIG_PCI */
4945
4946
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947static int __init ata_init(void)
4948{
4949 ata_wq = create_workqueue("ata");
4950 if (!ata_wq)
4951 return -ENOMEM;
4952
4953 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
4954 return 0;
4955}
4956
4957static void __exit ata_exit(void)
4958{
4959 destroy_workqueue(ata_wq);
4960}
4961
4962module_init(ata_init);
4963module_exit(ata_exit);
4964
Jeff Garzik67846b32005-10-05 02:58:32 -04004965static unsigned long ratelimit_time;
4966static spinlock_t ata_ratelimit_lock = SPIN_LOCK_UNLOCKED;
4967
4968int ata_ratelimit(void)
4969{
4970 int rc;
4971 unsigned long flags;
4972
4973 spin_lock_irqsave(&ata_ratelimit_lock, flags);
4974
4975 if (time_after(jiffies, ratelimit_time)) {
4976 rc = 1;
4977 ratelimit_time = jiffies + (HZ/5);
4978 } else
4979 rc = 0;
4980
4981 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
4982
4983 return rc;
4984}
4985
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986/*
4987 * libata is essentially a library of internal helper functions for
4988 * low-level ATA host controller drivers. As such, the API/ABI is
4989 * likely to change as new drivers are added and updated.
4990 * Do not depend on ABI/API stability.
4991 */
4992
4993EXPORT_SYMBOL_GPL(ata_std_bios_param);
4994EXPORT_SYMBOL_GPL(ata_std_ports);
4995EXPORT_SYMBOL_GPL(ata_device_add);
Alan Cox17b14452005-09-15 15:44:00 +01004996EXPORT_SYMBOL_GPL(ata_host_set_remove);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997EXPORT_SYMBOL_GPL(ata_sg_init);
4998EXPORT_SYMBOL_GPL(ata_sg_init_one);
4999EXPORT_SYMBOL_GPL(ata_qc_complete);
5000EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
5001EXPORT_SYMBOL_GPL(ata_eng_timeout);
5002EXPORT_SYMBOL_GPL(ata_tf_load);
5003EXPORT_SYMBOL_GPL(ata_tf_read);
5004EXPORT_SYMBOL_GPL(ata_noop_dev_select);
5005EXPORT_SYMBOL_GPL(ata_std_dev_select);
5006EXPORT_SYMBOL_GPL(ata_tf_to_fis);
5007EXPORT_SYMBOL_GPL(ata_tf_from_fis);
5008EXPORT_SYMBOL_GPL(ata_check_status);
5009EXPORT_SYMBOL_GPL(ata_altstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010EXPORT_SYMBOL_GPL(ata_exec_command);
5011EXPORT_SYMBOL_GPL(ata_port_start);
5012EXPORT_SYMBOL_GPL(ata_port_stop);
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04005013EXPORT_SYMBOL_GPL(ata_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014EXPORT_SYMBOL_GPL(ata_interrupt);
5015EXPORT_SYMBOL_GPL(ata_qc_prep);
5016EXPORT_SYMBOL_GPL(ata_bmdma_setup);
5017EXPORT_SYMBOL_GPL(ata_bmdma_start);
5018EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
5019EXPORT_SYMBOL_GPL(ata_bmdma_status);
5020EXPORT_SYMBOL_GPL(ata_bmdma_stop);
5021EXPORT_SYMBOL_GPL(ata_port_probe);
5022EXPORT_SYMBOL_GPL(sata_phy_reset);
5023EXPORT_SYMBOL_GPL(__sata_phy_reset);
5024EXPORT_SYMBOL_GPL(ata_bus_reset);
5025EXPORT_SYMBOL_GPL(ata_port_disable);
Jeff Garzik67846b32005-10-05 02:58:32 -04005026EXPORT_SYMBOL_GPL(ata_ratelimit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
5028EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
5029EXPORT_SYMBOL_GPL(ata_scsi_error);
5030EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
5031EXPORT_SYMBOL_GPL(ata_scsi_release);
5032EXPORT_SYMBOL_GPL(ata_host_intr);
5033EXPORT_SYMBOL_GPL(ata_dev_classify);
5034EXPORT_SYMBOL_GPL(ata_dev_id_string);
Brad Campbell6f2f3812005-05-12 15:07:47 -04005035EXPORT_SYMBOL_GPL(ata_dev_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005036EXPORT_SYMBOL_GPL(ata_scsi_simulate);
5037
Alan Cox452503f2005-10-21 19:01:32 -04005038EXPORT_SYMBOL_GPL(ata_timing_compute);
5039EXPORT_SYMBOL_GPL(ata_timing_merge);
5040
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041#ifdef CONFIG_PCI
5042EXPORT_SYMBOL_GPL(pci_test_config_bits);
Jeff Garzik374b1872005-08-30 05:42:52 -04005043EXPORT_SYMBOL_GPL(ata_pci_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
5045EXPORT_SYMBOL_GPL(ata_pci_init_one);
5046EXPORT_SYMBOL_GPL(ata_pci_remove_one);
5047#endif /* CONFIG_PCI */