blob: 9132698d29b821a94c127e3a15648a228b35c100 [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
Tejun Heo6aff8f12006-02-15 18:24:09 +090064static unsigned int ata_dev_init_params(struct ata_port *ap,
65 struct ata_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066static void ata_set_mode(struct ata_port *ap);
67static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev);
Jeff Garzik057ace52005-10-22 14:27:05 -040068static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069static int fgb(u32 bitmap);
Jeff Garzik057ace52005-10-22 14:27:05 -040070static int ata_choose_xfer_mode(const struct ata_port *ap,
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 u8 *xfer_mode_out,
72 unsigned int *xfer_shift_out);
Jeff Garzike33b9df2005-10-09 09:51:46 -040073static void ata_pio_error(struct ata_port *ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75static unsigned int ata_unique_id = 1;
76static struct workqueue_struct *ata_wq;
77
Jeff Garzik1623c812005-08-30 03:37:42 -040078int atapi_enabled = 0;
79module_param(atapi_enabled, int, 0444);
80MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)");
81
Jeff Garzikc3c013a2006-02-27 22:31:19 -050082int libata_fua = 0;
83module_param_named(fua, libata_fua, int, 0444);
84MODULE_PARM_DESC(fua, "FUA support (0=off, 1=on)");
85
Linus Torvalds1da177e2005-04-16 15:20:36 -070086MODULE_AUTHOR("Jeff Garzik");
87MODULE_DESCRIPTION("Library module for ATA devices");
88MODULE_LICENSE("GPL");
89MODULE_VERSION(DRV_VERSION);
90
Edward Falk0baab862005-06-02 18:17:13 -040091
92/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
94 * @tf: Taskfile to convert
95 * @fis: Buffer into which data will output
96 * @pmp: Port multiplier port
97 *
98 * Converts a standard ATA taskfile to a Serial ATA
99 * FIS structure (Register - Host to Device).
100 *
101 * LOCKING:
102 * Inherited from caller.
103 */
104
Jeff Garzik057ace52005-10-22 14:27:05 -0400105void ata_tf_to_fis(const struct ata_taskfile *tf, u8 *fis, u8 pmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
107 fis[0] = 0x27; /* Register - Host to Device FIS */
108 fis[1] = (pmp & 0xf) | (1 << 7); /* Port multiplier number,
109 bit 7 indicates Command FIS */
110 fis[2] = tf->command;
111 fis[3] = tf->feature;
112
113 fis[4] = tf->lbal;
114 fis[5] = tf->lbam;
115 fis[6] = tf->lbah;
116 fis[7] = tf->device;
117
118 fis[8] = tf->hob_lbal;
119 fis[9] = tf->hob_lbam;
120 fis[10] = tf->hob_lbah;
121 fis[11] = tf->hob_feature;
122
123 fis[12] = tf->nsect;
124 fis[13] = tf->hob_nsect;
125 fis[14] = 0;
126 fis[15] = tf->ctl;
127
128 fis[16] = 0;
129 fis[17] = 0;
130 fis[18] = 0;
131 fis[19] = 0;
132}
133
134/**
135 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
136 * @fis: Buffer from which data will be input
137 * @tf: Taskfile to output
138 *
Mark Lorde12a1be2005-11-12 18:55:45 -0500139 * Converts a serial ATA FIS structure to a standard ATA taskfile.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 *
141 * LOCKING:
142 * Inherited from caller.
143 */
144
Jeff Garzik057ace52005-10-22 14:27:05 -0400145void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
147 tf->command = fis[2]; /* status */
148 tf->feature = fis[3]; /* error */
149
150 tf->lbal = fis[4];
151 tf->lbam = fis[5];
152 tf->lbah = fis[6];
153 tf->device = fis[7];
154
155 tf->hob_lbal = fis[8];
156 tf->hob_lbam = fis[9];
157 tf->hob_lbah = fis[10];
158
159 tf->nsect = fis[12];
160 tf->hob_nsect = fis[13];
161}
162
Albert Lee8cbd6df2005-10-12 15:06:27 +0800163static const u8 ata_rw_cmds[] = {
164 /* pio multi */
165 ATA_CMD_READ_MULTI,
166 ATA_CMD_WRITE_MULTI,
167 ATA_CMD_READ_MULTI_EXT,
168 ATA_CMD_WRITE_MULTI_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100169 0,
170 0,
171 0,
172 ATA_CMD_WRITE_MULTI_FUA_EXT,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800173 /* pio */
174 ATA_CMD_PIO_READ,
175 ATA_CMD_PIO_WRITE,
176 ATA_CMD_PIO_READ_EXT,
177 ATA_CMD_PIO_WRITE_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100178 0,
179 0,
180 0,
181 0,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800182 /* dma */
183 ATA_CMD_READ,
184 ATA_CMD_WRITE,
185 ATA_CMD_READ_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100186 ATA_CMD_WRITE_EXT,
187 0,
188 0,
189 0,
190 ATA_CMD_WRITE_FUA_EXT
Albert Lee8cbd6df2005-10-12 15:06:27 +0800191};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193/**
Albert Lee8cbd6df2005-10-12 15:06:27 +0800194 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
195 * @qc: command to examine and configure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 *
Albert Lee8cbd6df2005-10-12 15:06:27 +0800197 * Examine the device configuration and tf->flags to calculate
198 * the proper read/write commands and protocol to use.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 *
200 * LOCKING:
201 * caller.
202 */
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100203int ata_rwcmd_protocol(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204{
Albert Lee8cbd6df2005-10-12 15:06:27 +0800205 struct ata_taskfile *tf = &qc->tf;
206 struct ata_device *dev = qc->dev;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100207 u8 cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100209 int index, fua, lba48, write;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800210
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100211 fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800212 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
213 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Albert Lee8cbd6df2005-10-12 15:06:27 +0800215 if (dev->flags & ATA_DFLAG_PIO) {
216 tf->protocol = ATA_PROT_PIO;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100217 index = dev->multi_count ? 0 : 8;
Alan Cox8d238e02006-01-17 20:50:31 +0000218 } else if (lba48 && (qc->ap->flags & ATA_FLAG_PIO_LBA48)) {
219 /* Unable to use DMA due to host limitation */
220 tf->protocol = ATA_PROT_PIO;
Albert Leeaef9d532006-02-08 16:37:43 +0800221 index = dev->multi_count ? 0 : 8;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800222 } else {
223 tf->protocol = ATA_PROT_DMA;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100224 index = 16;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800225 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100227 cmd = ata_rw_cmds[index + fua + lba48 + write];
228 if (cmd) {
229 tf->command = cmd;
230 return 0;
231 }
232 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233}
234
Arjan van de Ven98ac62d2005-11-28 10:06:23 +0100235static const char * const xfer_mode_str[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 "UDMA/16",
237 "UDMA/25",
238 "UDMA/33",
239 "UDMA/44",
240 "UDMA/66",
241 "UDMA/100",
242 "UDMA/133",
243 "UDMA7",
244 "MWDMA0",
245 "MWDMA1",
246 "MWDMA2",
247 "PIO0",
248 "PIO1",
249 "PIO2",
250 "PIO3",
251 "PIO4",
252};
253
254/**
255 * ata_udma_string - convert UDMA bit offset to string
256 * @mask: mask of bits supported; only highest bit counts.
257 *
258 * Determine string which represents the highest speed
259 * (highest bit in @udma_mask).
260 *
261 * LOCKING:
262 * None.
263 *
264 * RETURNS:
265 * Constant C string representing highest speed listed in
266 * @udma_mask, or the constant C string "<n/a>".
267 */
268
269static const char *ata_mode_string(unsigned int mask)
270{
271 int i;
272
273 for (i = 7; i >= 0; i--)
274 if (mask & (1 << i))
275 goto out;
276 for (i = ATA_SHIFT_MWDMA + 2; i >= ATA_SHIFT_MWDMA; i--)
277 if (mask & (1 << i))
278 goto out;
279 for (i = ATA_SHIFT_PIO + 4; i >= ATA_SHIFT_PIO; i--)
280 if (mask & (1 << i))
281 goto out;
282
283 return "<n/a>";
284
285out:
286 return xfer_mode_str[i];
287}
288
289/**
290 * ata_pio_devchk - PATA device presence detection
291 * @ap: ATA channel to examine
292 * @device: Device to examine (starting at zero)
293 *
294 * This technique was originally described in
295 * Hale Landis's ATADRVR (www.ata-atapi.com), and
296 * later found its way into the ATA/ATAPI spec.
297 *
298 * Write a pattern to the ATA shadow registers,
299 * and if a device is present, it will respond by
300 * correctly storing and echoing back the
301 * ATA shadow register contents.
302 *
303 * LOCKING:
304 * caller.
305 */
306
307static unsigned int ata_pio_devchk(struct ata_port *ap,
308 unsigned int device)
309{
310 struct ata_ioports *ioaddr = &ap->ioaddr;
311 u8 nsect, lbal;
312
313 ap->ops->dev_select(ap, device);
314
315 outb(0x55, ioaddr->nsect_addr);
316 outb(0xaa, ioaddr->lbal_addr);
317
318 outb(0xaa, ioaddr->nsect_addr);
319 outb(0x55, ioaddr->lbal_addr);
320
321 outb(0x55, ioaddr->nsect_addr);
322 outb(0xaa, ioaddr->lbal_addr);
323
324 nsect = inb(ioaddr->nsect_addr);
325 lbal = inb(ioaddr->lbal_addr);
326
327 if ((nsect == 0x55) && (lbal == 0xaa))
328 return 1; /* we found a device */
329
330 return 0; /* nothing found */
331}
332
333/**
334 * ata_mmio_devchk - PATA device presence detection
335 * @ap: ATA channel to examine
336 * @device: Device to examine (starting at zero)
337 *
338 * This technique was originally described in
339 * Hale Landis's ATADRVR (www.ata-atapi.com), and
340 * later found its way into the ATA/ATAPI spec.
341 *
342 * Write a pattern to the ATA shadow registers,
343 * and if a device is present, it will respond by
344 * correctly storing and echoing back the
345 * ATA shadow register contents.
346 *
347 * LOCKING:
348 * caller.
349 */
350
351static unsigned int ata_mmio_devchk(struct ata_port *ap,
352 unsigned int device)
353{
354 struct ata_ioports *ioaddr = &ap->ioaddr;
355 u8 nsect, lbal;
356
357 ap->ops->dev_select(ap, device);
358
359 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
360 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
361
362 writeb(0xaa, (void __iomem *) ioaddr->nsect_addr);
363 writeb(0x55, (void __iomem *) ioaddr->lbal_addr);
364
365 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
366 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
367
368 nsect = readb((void __iomem *) ioaddr->nsect_addr);
369 lbal = readb((void __iomem *) ioaddr->lbal_addr);
370
371 if ((nsect == 0x55) && (lbal == 0xaa))
372 return 1; /* we found a device */
373
374 return 0; /* nothing found */
375}
376
377/**
378 * ata_devchk - PATA device presence detection
379 * @ap: ATA channel to examine
380 * @device: Device to examine (starting at zero)
381 *
382 * Dispatch ATA device presence detection, depending
383 * on whether we are using PIO or MMIO to talk to the
384 * ATA shadow registers.
385 *
386 * LOCKING:
387 * caller.
388 */
389
390static unsigned int ata_devchk(struct ata_port *ap,
391 unsigned int device)
392{
393 if (ap->flags & ATA_FLAG_MMIO)
394 return ata_mmio_devchk(ap, device);
395 return ata_pio_devchk(ap, device);
396}
397
398/**
399 * ata_dev_classify - determine device type based on ATA-spec signature
400 * @tf: ATA taskfile register set for device to be identified
401 *
402 * Determine from taskfile register contents whether a device is
403 * ATA or ATAPI, as per "Signature and persistence" section
404 * of ATA/PI spec (volume 1, sect 5.14).
405 *
406 * LOCKING:
407 * None.
408 *
409 * RETURNS:
410 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
411 * the event of failure.
412 */
413
Jeff Garzik057ace52005-10-22 14:27:05 -0400414unsigned int ata_dev_classify(const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415{
416 /* Apple's open source Darwin code hints that some devices only
417 * put a proper signature into the LBA mid/high registers,
418 * So, we only check those. It's sufficient for uniqueness.
419 */
420
421 if (((tf->lbam == 0) && (tf->lbah == 0)) ||
422 ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
423 DPRINTK("found ATA device by sig\n");
424 return ATA_DEV_ATA;
425 }
426
427 if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
428 ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
429 DPRINTK("found ATAPI device by sig\n");
430 return ATA_DEV_ATAPI;
431 }
432
433 DPRINTK("unknown device\n");
434 return ATA_DEV_UNKNOWN;
435}
436
437/**
438 * ata_dev_try_classify - Parse returned ATA device signature
439 * @ap: ATA channel to examine
440 * @device: Device to examine (starting at zero)
Tejun Heob4dc7622006-01-24 17:05:22 +0900441 * @r_err: Value of error register on completion
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 *
443 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
444 * an ATA/ATAPI-defined set of values is placed in the ATA
445 * shadow registers, indicating the results of device detection
446 * and diagnostics.
447 *
448 * Select the ATA device, and read the values from the ATA shadow
449 * registers. Then parse according to the Error register value,
450 * and the spec-defined values examined by ata_dev_classify().
451 *
452 * LOCKING:
453 * caller.
Tejun Heob4dc7622006-01-24 17:05:22 +0900454 *
455 * RETURNS:
456 * Device type - %ATA_DEV_ATA, %ATA_DEV_ATAPI or %ATA_DEV_NONE.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 */
458
Tejun Heob4dc7622006-01-24 17:05:22 +0900459static unsigned int
460ata_dev_try_classify(struct ata_port *ap, unsigned int device, u8 *r_err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 struct ata_taskfile tf;
463 unsigned int class;
464 u8 err;
465
466 ap->ops->dev_select(ap, device);
467
468 memset(&tf, 0, sizeof(tf));
469
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 ap->ops->tf_read(ap, &tf);
Jeff Garzik0169e282005-10-29 21:25:10 -0400471 err = tf.feature;
Tejun Heob4dc7622006-01-24 17:05:22 +0900472 if (r_err)
473 *r_err = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
475 /* see if device passed diags */
476 if (err == 1)
477 /* do nothing */ ;
478 else if ((device == 0) && (err == 0x81))
479 /* do nothing */ ;
480 else
Tejun Heob4dc7622006-01-24 17:05:22 +0900481 return ATA_DEV_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Tejun Heob4dc7622006-01-24 17:05:22 +0900483 /* determine if device is ATA or ATAPI */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 class = ata_dev_classify(&tf);
Tejun Heob4dc7622006-01-24 17:05:22 +0900485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 if (class == ATA_DEV_UNKNOWN)
Tejun Heob4dc7622006-01-24 17:05:22 +0900487 return ATA_DEV_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
Tejun Heob4dc7622006-01-24 17:05:22 +0900489 return ATA_DEV_NONE;
490 return class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491}
492
493/**
Tejun Heo6a62a042006-02-13 10:02:46 +0900494 * ata_id_string - Convert IDENTIFY DEVICE page into string
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 * @id: IDENTIFY DEVICE results we will examine
496 * @s: string into which data is output
497 * @ofs: offset into identify device page
498 * @len: length of string to return. must be an even number.
499 *
500 * The strings in the IDENTIFY DEVICE page are broken up into
501 * 16-bit chunks. Run through the string, and output each
502 * 8-bit chunk linearly, regardless of platform.
503 *
504 * LOCKING:
505 * caller.
506 */
507
Tejun Heo6a62a042006-02-13 10:02:46 +0900508void ata_id_string(const u16 *id, unsigned char *s,
509 unsigned int ofs, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510{
511 unsigned int c;
512
513 while (len > 0) {
514 c = id[ofs] >> 8;
515 *s = c;
516 s++;
517
518 c = id[ofs] & 0xff;
519 *s = c;
520 s++;
521
522 ofs++;
523 len -= 2;
524 }
525}
526
Tejun Heo0e949ff2006-02-12 22:47:04 +0900527/**
Tejun Heo6a62a042006-02-13 10:02:46 +0900528 * ata_id_c_string - Convert IDENTIFY DEVICE page into C string
Tejun Heo0e949ff2006-02-12 22:47:04 +0900529 * @id: IDENTIFY DEVICE results we will examine
530 * @s: string into which data is output
531 * @ofs: offset into identify device page
532 * @len: length of string to return. must be an odd number.
533 *
Tejun Heo6a62a042006-02-13 10:02:46 +0900534 * This function is identical to ata_id_string except that it
Tejun Heo0e949ff2006-02-12 22:47:04 +0900535 * trims trailing spaces and terminates the resulting string with
536 * null. @len must be actual maximum length (even number) + 1.
537 *
538 * LOCKING:
539 * caller.
540 */
Tejun Heo6a62a042006-02-13 10:02:46 +0900541void ata_id_c_string(const u16 *id, unsigned char *s,
542 unsigned int ofs, unsigned int len)
Tejun Heo0e949ff2006-02-12 22:47:04 +0900543{
544 unsigned char *p;
545
546 WARN_ON(!(len & 1));
547
Tejun Heo6a62a042006-02-13 10:02:46 +0900548 ata_id_string(id, s, ofs, len - 1);
Tejun Heo0e949ff2006-02-12 22:47:04 +0900549
550 p = s + strnlen(s, len - 1);
551 while (p > s && p[-1] == ' ')
552 p--;
553 *p = '\0';
554}
Edward Falk0baab862005-06-02 18:17:13 -0400555
Tejun Heo29407402006-02-12 22:47:04 +0900556static u64 ata_id_n_sectors(const u16 *id)
557{
558 if (ata_id_has_lba(id)) {
559 if (ata_id_has_lba48(id))
560 return ata_id_u64(id, 100);
561 else
562 return ata_id_u32(id, 60);
563 } else {
564 if (ata_id_current_chs_valid(id))
565 return ata_id_u32(id, 57);
566 else
567 return id[1] * id[3] * id[6];
568 }
569}
Edward Falk0baab862005-06-02 18:17:13 -0400570
571/**
572 * ata_noop_dev_select - Select device 0/1 on ATA bus
573 * @ap: ATA channel to manipulate
574 * @device: ATA device (numbered from zero) to select
575 *
576 * This function performs no actual function.
577 *
578 * May be used as the dev_select() entry in ata_port_operations.
579 *
580 * LOCKING:
581 * caller.
582 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
584{
585}
586
Edward Falk0baab862005-06-02 18:17:13 -0400587
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588/**
589 * ata_std_dev_select - Select device 0/1 on ATA bus
590 * @ap: ATA channel to manipulate
591 * @device: ATA device (numbered from zero) to select
592 *
593 * Use the method defined in the ATA specification to
594 * make either device 0, or device 1, active on the
Edward Falk0baab862005-06-02 18:17:13 -0400595 * ATA channel. Works with both PIO and MMIO.
596 *
597 * May be used as the dev_select() entry in ata_port_operations.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 *
599 * LOCKING:
600 * caller.
601 */
602
603void ata_std_dev_select (struct ata_port *ap, unsigned int device)
604{
605 u8 tmp;
606
607 if (device == 0)
608 tmp = ATA_DEVICE_OBS;
609 else
610 tmp = ATA_DEVICE_OBS | ATA_DEV1;
611
612 if (ap->flags & ATA_FLAG_MMIO) {
613 writeb(tmp, (void __iomem *) ap->ioaddr.device_addr);
614 } else {
615 outb(tmp, ap->ioaddr.device_addr);
616 }
617 ata_pause(ap); /* needed; also flushes, for mmio */
618}
619
620/**
621 * ata_dev_select - Select device 0/1 on ATA bus
622 * @ap: ATA channel to manipulate
623 * @device: ATA device (numbered from zero) to select
624 * @wait: non-zero to wait for Status register BSY bit to clear
625 * @can_sleep: non-zero if context allows sleeping
626 *
627 * Use the method defined in the ATA specification to
628 * make either device 0, or device 1, active on the
629 * ATA channel.
630 *
631 * This is a high-level version of ata_std_dev_select(),
632 * which additionally provides the services of inserting
633 * the proper pauses and status polling, where needed.
634 *
635 * LOCKING:
636 * caller.
637 */
638
639void ata_dev_select(struct ata_port *ap, unsigned int device,
640 unsigned int wait, unsigned int can_sleep)
641{
642 VPRINTK("ENTER, ata%u: device %u, wait %u\n",
643 ap->id, device, wait);
644
645 if (wait)
646 ata_wait_idle(ap);
647
648 ap->ops->dev_select(ap, device);
649
650 if (wait) {
651 if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
652 msleep(150);
653 ata_wait_idle(ap);
654 }
655}
656
657/**
658 * ata_dump_id - IDENTIFY DEVICE info debugging output
Tejun Heo0bd33002006-02-12 22:47:05 +0900659 * @id: IDENTIFY DEVICE page to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 *
Tejun Heo0bd33002006-02-12 22:47:05 +0900661 * Dump selected 16-bit words from the given IDENTIFY DEVICE
662 * page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 *
664 * LOCKING:
665 * caller.
666 */
667
Tejun Heo0bd33002006-02-12 22:47:05 +0900668static inline void ata_dump_id(const u16 *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669{
670 DPRINTK("49==0x%04x "
671 "53==0x%04x "
672 "63==0x%04x "
673 "64==0x%04x "
674 "75==0x%04x \n",
Tejun Heo0bd33002006-02-12 22:47:05 +0900675 id[49],
676 id[53],
677 id[63],
678 id[64],
679 id[75]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 DPRINTK("80==0x%04x "
681 "81==0x%04x "
682 "82==0x%04x "
683 "83==0x%04x "
684 "84==0x%04x \n",
Tejun Heo0bd33002006-02-12 22:47:05 +0900685 id[80],
686 id[81],
687 id[82],
688 id[83],
689 id[84]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 DPRINTK("88==0x%04x "
691 "93==0x%04x\n",
Tejun Heo0bd33002006-02-12 22:47:05 +0900692 id[88],
693 id[93]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694}
695
Alan Cox11e29e22005-10-21 18:46:32 -0400696/*
697 * Compute the PIO modes available for this device. This is not as
698 * trivial as it seems if we must consider early devices correctly.
699 *
700 * FIXME: pre IDE drive timing (do we care ?).
701 */
702
Jeff Garzik057ace52005-10-22 14:27:05 -0400703static unsigned int ata_pio_modes(const struct ata_device *adev)
Alan Cox11e29e22005-10-21 18:46:32 -0400704{
705 u16 modes;
706
Alan Coxffa29452006-01-09 17:14:40 +0000707 /* Usual case. Word 53 indicates word 64 is valid */
708 if (adev->id[ATA_ID_FIELD_VALID] & (1 << 1)) {
Alan Cox11e29e22005-10-21 18:46:32 -0400709 modes = adev->id[ATA_ID_PIO_MODES] & 0x03;
710 modes <<= 3;
711 modes |= 0x7;
712 return modes;
713 }
714
Alan Coxffa29452006-01-09 17:14:40 +0000715 /* If word 64 isn't valid then Word 51 high byte holds the PIO timing
716 number for the maximum. Turn it into a mask and return it */
717 modes = (2 << ((adev->id[ATA_ID_OLD_PIO_MODES] >> 8) & 0xFF)) - 1 ;
Alan Cox11e29e22005-10-21 18:46:32 -0400718 return modes;
Alan Coxffa29452006-01-09 17:14:40 +0000719 /* But wait.. there's more. Design your standards by committee and
720 you too can get a free iordy field to process. However its the
721 speeds not the modes that are supported... Note drivers using the
722 timing API will get this right anyway */
Alan Cox11e29e22005-10-21 18:46:32 -0400723}
724
Tejun Heo95064372006-01-23 13:09:37 +0900725static inline void
Tejun Heo95064372006-01-23 13:09:37 +0900726ata_queue_pio_task(struct ata_port *ap)
727{
Tejun Heoc18d06f2006-02-02 00:56:10 +0900728 if (!(ap->flags & ATA_FLAG_FLUSH_PIO_TASK))
729 queue_work(ata_wq, &ap->pio_task);
Tejun Heo95064372006-01-23 13:09:37 +0900730}
731
732static inline void
733ata_queue_delayed_pio_task(struct ata_port *ap, unsigned long delay)
734{
Tejun Heoc18d06f2006-02-02 00:56:10 +0900735 if (!(ap->flags & ATA_FLAG_FLUSH_PIO_TASK))
736 queue_delayed_work(ata_wq, &ap->pio_task, delay);
737}
738
739/**
Jeff Garzik41232d32006-02-09 04:52:55 -0500740 * ata_flush_pio_tasks - Flush pio_task
Tejun Heoc18d06f2006-02-02 00:56:10 +0900741 * @ap: the target ata_port
742 *
Jeff Garzik41232d32006-02-09 04:52:55 -0500743 * After this function completes, pio_task is
Tejun Heoc18d06f2006-02-02 00:56:10 +0900744 * guranteed not to be running or scheduled.
745 *
746 * LOCKING:
747 * Kernel thread context (may sleep)
748 */
749
750static void ata_flush_pio_tasks(struct ata_port *ap)
751{
752 int tmp = 0;
753 unsigned long flags;
754
755 DPRINTK("ENTER\n");
756
757 spin_lock_irqsave(&ap->host_set->lock, flags);
758 ap->flags |= ATA_FLAG_FLUSH_PIO_TASK;
759 spin_unlock_irqrestore(&ap->host_set->lock, flags);
760
761 DPRINTK("flush #1\n");
762 flush_workqueue(ata_wq);
763
764 /*
765 * At this point, if a task is running, it's guaranteed to see
766 * the FLUSH flag; thus, it will never queue pio tasks again.
767 * Cancel and flush.
768 */
769 tmp |= cancel_delayed_work(&ap->pio_task);
Tejun Heoc18d06f2006-02-02 00:56:10 +0900770 if (!tmp) {
771 DPRINTK("flush #2\n");
772 flush_workqueue(ata_wq);
773 }
774
775 spin_lock_irqsave(&ap->host_set->lock, flags);
776 ap->flags &= ~ATA_FLAG_FLUSH_PIO_TASK;
777 spin_unlock_irqrestore(&ap->host_set->lock, flags);
778
779 DPRINTK("EXIT\n");
Tejun Heo95064372006-01-23 13:09:37 +0900780}
781
Tejun Heo77853bf2006-01-23 13:09:36 +0900782void ata_qc_complete_internal(struct ata_queued_cmd *qc)
Jeff Garzik64f043d2005-11-17 10:50:01 -0500783{
Tejun Heo77853bf2006-01-23 13:09:36 +0900784 struct completion *waiting = qc->private_data;
Jeff Garzik64f043d2005-11-17 10:50:01 -0500785
Tejun Heo77853bf2006-01-23 13:09:36 +0900786 qc->ap->ops->tf_read(qc->ap, &qc->tf);
Tejun Heoa2a7a662005-12-13 14:48:31 +0900787 complete(waiting);
Tejun Heoa2a7a662005-12-13 14:48:31 +0900788}
Jeff Garzik64f043d2005-11-17 10:50:01 -0500789
Tejun Heoa2a7a662005-12-13 14:48:31 +0900790/**
791 * ata_exec_internal - execute libata internal command
792 * @ap: Port to which the command is sent
793 * @dev: Device to which the command is sent
794 * @tf: Taskfile registers for the command and the result
795 * @dma_dir: Data tranfer direction of the command
796 * @buf: Data buffer of the command
797 * @buflen: Length of data buffer
798 *
799 * Executes libata internal command with timeout. @tf contains
800 * command on entry and result on return. Timeout and error
801 * conditions are reported via return value. No recovery action
802 * is taken after a command times out. It's caller's duty to
803 * clean up after timeout.
804 *
805 * LOCKING:
806 * None. Should be called with kernel context, might sleep.
807 */
808
809static unsigned
810ata_exec_internal(struct ata_port *ap, struct ata_device *dev,
811 struct ata_taskfile *tf,
812 int dma_dir, void *buf, unsigned int buflen)
813{
814 u8 command = tf->command;
815 struct ata_queued_cmd *qc;
816 DECLARE_COMPLETION(wait);
817 unsigned long flags;
Tejun Heo77853bf2006-01-23 13:09:36 +0900818 unsigned int err_mask;
Tejun Heoa2a7a662005-12-13 14:48:31 +0900819
820 spin_lock_irqsave(&ap->host_set->lock, flags);
821
822 qc = ata_qc_new_init(ap, dev);
823 BUG_ON(qc == NULL);
824
825 qc->tf = *tf;
826 qc->dma_dir = dma_dir;
827 if (dma_dir != DMA_NONE) {
828 ata_sg_init_one(qc, buf, buflen);
829 qc->nsect = buflen / ATA_SECT_SIZE;
Jeff Garzik64f043d2005-11-17 10:50:01 -0500830 }
831
Tejun Heo77853bf2006-01-23 13:09:36 +0900832 qc->private_data = &wait;
Tejun Heoa2a7a662005-12-13 14:48:31 +0900833 qc->complete_fn = ata_qc_complete_internal;
834
Tejun Heo9a3d9eb2006-01-23 13:09:36 +0900835 qc->err_mask = ata_qc_issue(qc);
836 if (qc->err_mask)
Tejun Heo8e436af2006-01-23 13:09:36 +0900837 ata_qc_complete(qc);
Tejun Heoa2a7a662005-12-13 14:48:31 +0900838
839 spin_unlock_irqrestore(&ap->host_set->lock, flags);
840
841 if (!wait_for_completion_timeout(&wait, ATA_TMOUT_INTERNAL)) {
842 spin_lock_irqsave(&ap->host_set->lock, flags);
843
844 /* We're racing with irq here. If we lose, the
845 * following test prevents us from completing the qc
846 * again. If completion irq occurs after here but
847 * before the caller cleans up, it will result in a
848 * spurious interrupt. We can live with that.
849 */
Tejun Heo77853bf2006-01-23 13:09:36 +0900850 if (qc->flags & ATA_QCFLAG_ACTIVE) {
Tejun Heo11a56d22006-01-23 13:09:36 +0900851 qc->err_mask = AC_ERR_TIMEOUT;
Tejun Heoa2a7a662005-12-13 14:48:31 +0900852 ata_qc_complete(qc);
853 printk(KERN_WARNING "ata%u: qc timeout (cmd 0x%x)\n",
854 ap->id, command);
855 }
856
857 spin_unlock_irqrestore(&ap->host_set->lock, flags);
858 }
859
Tejun Heo77853bf2006-01-23 13:09:36 +0900860 *tf = qc->tf;
861 err_mask = qc->err_mask;
862
863 ata_qc_free(qc);
864
865 return err_mask;
Jeff Garzik64f043d2005-11-17 10:50:01 -0500866}
867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868/**
Alan Cox1bc4ccf2006-01-09 17:18:14 +0000869 * ata_pio_need_iordy - check if iordy needed
870 * @adev: ATA device
871 *
872 * Check if the current speed of the device requires IORDY. Used
873 * by various controllers for chip configuration.
874 */
875
876unsigned int ata_pio_need_iordy(const struct ata_device *adev)
877{
878 int pio;
879 int speed = adev->pio_mode - XFER_PIO_0;
880
881 if (speed < 2)
882 return 0;
883 if (speed > 2)
884 return 1;
885
886 /* If we have no drive specific rule, then PIO 2 is non IORDY */
887
888 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
889 pio = adev->id[ATA_ID_EIDE_PIO];
890 /* Is the speed faster than the drive allows non IORDY ? */
891 if (pio) {
892 /* This is cycle times not frequency - watch the logic! */
893 if (pio > 240) /* PIO2 is 240nS per cycle */
894 return 1;
895 return 0;
896 }
897 }
898 return 0;
899}
900
901/**
Tejun Heo49016ac2006-02-21 02:12:11 +0900902 * ata_dev_read_id - Read ID data from the specified device
903 * @ap: port on which target device resides
904 * @dev: target device
905 * @p_class: pointer to class of the target device (may be changed)
906 * @post_reset: is this read ID post-reset?
907 * @id: buffer to fill IDENTIFY page into
908 *
909 * Read ID data from the specified device. ATA_CMD_ID_ATA is
910 * performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
911 * devices. This function also takes care of EDD signature
912 * misreporting (to be removed once EDD support is gone) and
913 * issues ATA_CMD_INIT_DEV_PARAMS for pre-ATA4 drives.
914 *
915 * LOCKING:
916 * Kernel thread context (may sleep)
917 *
918 * RETURNS:
919 * 0 on success, -errno otherwise.
920 */
921static int ata_dev_read_id(struct ata_port *ap, struct ata_device *dev,
922 unsigned int *p_class, int post_reset, u16 *id)
923{
924 unsigned int class = *p_class;
925 unsigned int using_edd;
926 struct ata_taskfile tf;
927 unsigned int err_mask = 0;
928 const char *reason;
929 int rc;
930
931 DPRINTK("ENTER, host %u, dev %u\n", ap->id, dev->devno);
932
933 if (ap->ops->probe_reset ||
934 ap->flags & (ATA_FLAG_SRST | ATA_FLAG_SATA_RESET))
935 using_edd = 0;
936 else
937 using_edd = 1;
938
939 ata_dev_select(ap, dev->devno, 1, 1); /* select device 0/1 */
940
941 retry:
942 ata_tf_init(ap, &tf, dev->devno);
943
944 switch (class) {
945 case ATA_DEV_ATA:
946 tf.command = ATA_CMD_ID_ATA;
947 break;
948 case ATA_DEV_ATAPI:
949 tf.command = ATA_CMD_ID_ATAPI;
950 break;
951 default:
952 rc = -ENODEV;
953 reason = "unsupported class";
954 goto err_out;
955 }
956
957 tf.protocol = ATA_PROT_PIO;
958
959 err_mask = ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE,
960 id, sizeof(id[0]) * ATA_ID_WORDS);
961
962 if (err_mask) {
963 rc = -EIO;
964 reason = "I/O error";
965
966 if (err_mask & ~AC_ERR_DEV)
967 goto err_out;
968
969 /*
970 * arg! EDD works for all test cases, but seems to return
971 * the ATA signature for some ATAPI devices. Until the
972 * reason for this is found and fixed, we fix up the mess
973 * here. If IDENTIFY DEVICE returns command aborted
974 * (as ATAPI devices do), then we issue an
975 * IDENTIFY PACKET DEVICE.
976 *
977 * ATA software reset (SRST, the default) does not appear
978 * to have this problem.
979 */
980 if ((using_edd) && (class == ATA_DEV_ATA)) {
981 u8 err = tf.feature;
982 if (err & ATA_ABORTED) {
983 class = ATA_DEV_ATAPI;
984 goto retry;
985 }
986 }
987 goto err_out;
988 }
989
990 swap_buf_le16(id, ATA_ID_WORDS);
991
992 /* print device capabilities */
993 printk(KERN_DEBUG "ata%u: dev %u cfg "
994 "49:%04x 82:%04x 83:%04x 84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
995 ap->id, dev->devno,
996 id[49], id[82], id[83], id[84], id[85], id[86], id[87], id[88]);
997
998 /* sanity check */
999 if ((class == ATA_DEV_ATA) != ata_id_is_ata(id)) {
1000 rc = -EINVAL;
1001 reason = "device reports illegal type";
1002 goto err_out;
1003 }
1004
1005 if (post_reset && class == ATA_DEV_ATA) {
1006 /*
1007 * The exact sequence expected by certain pre-ATA4 drives is:
1008 * SRST RESET
1009 * IDENTIFY
1010 * INITIALIZE DEVICE PARAMETERS
1011 * anything else..
1012 * Some drives were very specific about that exact sequence.
1013 */
1014 if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
1015 err_mask = ata_dev_init_params(ap, dev);
1016 if (err_mask) {
1017 rc = -EIO;
1018 reason = "INIT_DEV_PARAMS failed";
1019 goto err_out;
1020 }
1021
1022 /* current CHS translation info (id[53-58]) might be
1023 * changed. reread the identify device info.
1024 */
1025 post_reset = 0;
1026 goto retry;
1027 }
1028 }
1029
1030 *p_class = class;
1031 return 0;
1032
1033 err_out:
1034 printk(KERN_WARNING "ata%u: dev %u failed to IDENTIFY (%s)\n",
1035 ap->id, dev->devno, reason);
Tejun Heo49016ac2006-02-21 02:12:11 +09001036 return rc;
1037}
1038
1039/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 * ata_dev_identify - obtain IDENTIFY x DEVICE page
1041 * @ap: port on which device we wish to probe resides
1042 * @device: device bus address, starting at zero
1043 *
1044 * Following bus reset, we issue the IDENTIFY [PACKET] DEVICE
1045 * command, and read back the 512-byte device information page.
1046 * The device information page is fed to us via the standard
1047 * PIO-IN protocol, but we hand-code it here. (TODO: investigate
1048 * using standard PIO-IN paths)
1049 *
1050 * After reading the device information page, we use several
1051 * bits of information from it to initialize data structures
1052 * that will be used during the lifetime of the ata_device.
1053 * Other data from the info page is used to disqualify certain
1054 * older ATA devices we do not wish to support.
1055 *
1056 * LOCKING:
1057 * Inherited from caller. Some functions called by this function
1058 * obtain the host_set lock.
1059 */
1060
1061static void ata_dev_identify(struct ata_port *ap, unsigned int device)
1062{
1063 struct ata_device *dev = &ap->device[device];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 unsigned long xfer_modes;
Tejun Heo6e7846e2006-02-12 23:32:58 +09001065 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066
1067 if (!ata_dev_present(dev)) {
1068 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
1069 ap->id, device);
1070 return;
1071 }
1072
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 DPRINTK("ENTER, host %u, dev %u\n", ap->id, device);
1074
Tejun Heo49016ac2006-02-21 02:12:11 +09001075 rc = ata_dev_read_id(ap, dev, &dev->class, 1, dev->id);
1076 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078
1079 /*
1080 * common ATA, ATAPI feature tests
1081 */
1082
Albert Lee8bf62ece2005-05-12 15:29:42 -04001083 /* we require DMA support (bits 8 of word 49) */
1084 if (!ata_id_has_dma(dev->id)) {
1085 printk(KERN_DEBUG "ata%u: no dma\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 goto err_out_nosup;
1087 }
1088
1089 /* quick-n-dirty find max transfer mode; for printk only */
1090 xfer_modes = dev->id[ATA_ID_UDMA_MODES];
1091 if (!xfer_modes)
1092 xfer_modes = (dev->id[ATA_ID_MWDMA_MODES]) << ATA_SHIFT_MWDMA;
Alan Cox11e29e22005-10-21 18:46:32 -04001093 if (!xfer_modes)
1094 xfer_modes = ata_pio_modes(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
Tejun Heo0bd33002006-02-12 22:47:05 +09001096 ata_dump_id(dev->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
1098 /* ATA-specific feature tests */
1099 if (dev->class == ATA_DEV_ATA) {
Tejun Heo29407402006-02-12 22:47:04 +09001100 dev->n_sectors = ata_id_n_sectors(dev->id);
1101
Albert Lee8bf62ece2005-05-12 15:29:42 -04001102 if (ata_id_has_lba(dev->id)) {
1103 dev->flags |= ATA_DFLAG_LBA;
1104
Tejun Heo29407402006-02-12 22:47:04 +09001105 if (ata_id_has_lba48(dev->id))
Albert Lee8bf62ece2005-05-12 15:29:42 -04001106 dev->flags |= ATA_DFLAG_LBA48;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001107
1108 /* print device info to dmesg */
1109 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors:%s\n",
1110 ap->id, device,
Tejun Heo49016ac2006-02-21 02:12:11 +09001111 ata_id_major_version(dev->id),
Albert Lee8bf62ece2005-05-12 15:29:42 -04001112 ata_mode_string(xfer_modes),
1113 (unsigned long long)dev->n_sectors,
1114 dev->flags & ATA_DFLAG_LBA48 ? " LBA48" : " LBA");
1115 } else {
1116 /* CHS */
1117
1118 /* Default translation */
1119 dev->cylinders = dev->id[1];
1120 dev->heads = dev->id[3];
1121 dev->sectors = dev->id[6];
Albert Lee8bf62ece2005-05-12 15:29:42 -04001122
1123 if (ata_id_current_chs_valid(dev->id)) {
1124 /* Current CHS translation is valid. */
1125 dev->cylinders = dev->id[54];
1126 dev->heads = dev->id[55];
1127 dev->sectors = dev->id[56];
Albert Lee8bf62ece2005-05-12 15:29:42 -04001128 }
1129
1130 /* print device info to dmesg */
1131 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors: CHS %d/%d/%d\n",
1132 ap->id, device,
Tejun Heo49016ac2006-02-21 02:12:11 +09001133 ata_id_major_version(dev->id),
Albert Lee8bf62ece2005-05-12 15:29:42 -04001134 ata_mode_string(xfer_modes),
1135 (unsigned long long)dev->n_sectors,
1136 (int)dev->cylinders, (int)dev->heads, (int)dev->sectors);
1137
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 }
1139
Albert Lee07f6f7d2005-11-01 19:33:20 +08001140 if (dev->id[59] & 0x100) {
1141 dev->multi_count = dev->id[59] & 0xff;
1142 DPRINTK("ata%u: dev %u multi count %u\n",
1143 ap->id, device, dev->multi_count);
1144 }
1145
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 }
1147
1148 /* ATAPI-specific feature tests */
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05001149 else if (dev->class == ATA_DEV_ATAPI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 rc = atapi_cdb_len(dev->id);
1151 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
1152 printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
1153 goto err_out_nosup;
1154 }
Tejun Heo6e7846e2006-02-12 23:32:58 +09001155 dev->cdb_len = (unsigned int) rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
Albert Lee312f7da2005-09-27 17:38:03 +08001157 if (ata_id_cdb_intr(dev->id))
1158 dev->flags |= ATA_DFLAG_CDB_INTR;
1159
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 /* print device info to dmesg */
1161 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
1162 ap->id, device,
1163 ata_mode_string(xfer_modes));
1164 }
1165
Tejun Heo6e7846e2006-02-12 23:32:58 +09001166 ap->host->max_cmd_len = 0;
1167 for (i = 0; i < ATA_MAX_DEVICES; i++)
1168 ap->host->max_cmd_len = max_t(unsigned int,
1169 ap->host->max_cmd_len,
1170 ap->device[i].cdb_len);
1171
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
1173 return;
1174
1175err_out_nosup:
1176 printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n",
1177 ap->id, device);
1178err_out:
1179 dev->class++; /* converts ATA_DEV_xxx into ATA_DEV_xxx_UNSUP */
1180 DPRINTK("EXIT, err\n");
1181}
1182
Brad Campbell6f2f3812005-05-12 15:07:47 -04001183
Tejun Heo8eabd022006-02-12 23:32:58 +09001184static inline u8 ata_dev_knobble(const struct ata_port *ap,
1185 struct ata_device *dev)
Brad Campbell6f2f3812005-05-12 15:07:47 -04001186{
Tejun Heo8eabd022006-02-12 23:32:58 +09001187 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
Brad Campbell6f2f3812005-05-12 15:07:47 -04001188}
1189
1190/**
Randy Dunlapc893a3a2006-01-28 13:15:32 -05001191 * ata_dev_config - Run device specific handlers & check for SATA->PATA bridges
1192 * @ap: Bus
1193 * @i: Device
Brad Campbell6f2f3812005-05-12 15:07:47 -04001194 *
Randy Dunlapc893a3a2006-01-28 13:15:32 -05001195 * LOCKING:
Brad Campbell6f2f3812005-05-12 15:07:47 -04001196 */
Jeff Garzik8a60a072005-07-31 13:13:24 -04001197
Brad Campbell6f2f3812005-05-12 15:07:47 -04001198void ata_dev_config(struct ata_port *ap, unsigned int i)
1199{
1200 /* limit bridge transfers to udma5, 200 sectors */
Tejun Heo8eabd022006-02-12 23:32:58 +09001201 if (ata_dev_knobble(ap, &ap->device[i])) {
Brad Campbell6f2f3812005-05-12 15:07:47 -04001202 printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
Tejun Heo8eabd022006-02-12 23:32:58 +09001203 ap->id, i);
Brad Campbell6f2f3812005-05-12 15:07:47 -04001204 ap->udma_mask &= ATA_UDMA5;
Tejun Heob00eec12006-02-12 23:32:59 +09001205 ap->device[i].max_sectors = ATA_MAX_SECTORS;
Brad Campbell6f2f3812005-05-12 15:07:47 -04001206 }
1207
1208 if (ap->ops->dev_config)
1209 ap->ops->dev_config(ap, &ap->device[i]);
1210}
1211
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212/**
1213 * ata_bus_probe - Reset and probe ATA bus
1214 * @ap: Bus to probe
1215 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001216 * Master ATA bus probing function. Initiates a hardware-dependent
1217 * bus reset, then attempts to identify any devices found on
1218 * the bus.
1219 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001221 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 *
1223 * RETURNS:
1224 * Zero on success, non-zero on error.
1225 */
1226
1227static int ata_bus_probe(struct ata_port *ap)
1228{
1229 unsigned int i, found = 0;
1230
Tejun Heoc19ba8a2006-01-24 17:05:22 +09001231 if (ap->ops->probe_reset) {
1232 unsigned int classes[ATA_MAX_DEVICES];
1233 int rc;
1234
1235 ata_port_probe(ap);
1236
1237 rc = ap->ops->probe_reset(ap, classes);
1238 if (rc == 0) {
Tejun Heo06ab7822006-02-12 15:01:49 +09001239 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1240 if (classes[i] == ATA_DEV_UNKNOWN)
1241 classes[i] = ATA_DEV_NONE;
Tejun Heoc19ba8a2006-01-24 17:05:22 +09001242 ap->device[i].class = classes[i];
Tejun Heo06ab7822006-02-12 15:01:49 +09001243 }
Tejun Heoc19ba8a2006-01-24 17:05:22 +09001244 } else {
1245 printk(KERN_ERR "ata%u: probe reset failed, "
1246 "disabling port\n", ap->id);
1247 ata_port_disable(ap);
1248 }
1249 } else
1250 ap->ops->phy_reset(ap);
1251
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1253 goto err_out;
1254
1255 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1256 ata_dev_identify(ap, i);
1257 if (ata_dev_present(&ap->device[i])) {
1258 found = 1;
Brad Campbell6f2f3812005-05-12 15:07:47 -04001259 ata_dev_config(ap,i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 }
1261 }
1262
1263 if ((!found) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1264 goto err_out_disable;
1265
1266 ata_set_mode(ap);
1267 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1268 goto err_out_disable;
1269
1270 return 0;
1271
1272err_out_disable:
1273 ap->ops->port_disable(ap);
1274err_out:
1275 return -1;
1276}
1277
1278/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001279 * ata_port_probe - Mark port as enabled
1280 * @ap: Port for which we indicate enablement
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001282 * Modify @ap data structure such that the system
1283 * thinks that the entire port is enabled.
1284 *
1285 * LOCKING: host_set lock, or some other form of
1286 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 */
1288
1289void ata_port_probe(struct ata_port *ap)
1290{
1291 ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1292}
1293
1294/**
Tejun Heo3be680b2005-12-19 22:35:02 +09001295 * sata_print_link_status - Print SATA link status
1296 * @ap: SATA port to printk link status about
1297 *
1298 * This function prints link speed and status of a SATA link.
1299 *
1300 * LOCKING:
1301 * None.
1302 */
1303static void sata_print_link_status(struct ata_port *ap)
1304{
1305 u32 sstatus, tmp;
1306 const char *speed;
1307
1308 if (!ap->ops->scr_read)
1309 return;
1310
1311 sstatus = scr_read(ap, SCR_STATUS);
1312
1313 if (sata_dev_present(ap)) {
1314 tmp = (sstatus >> 4) & 0xf;
1315 if (tmp & (1 << 0))
1316 speed = "1.5";
1317 else if (tmp & (1 << 1))
1318 speed = "3.0";
1319 else
1320 speed = "<unknown>";
1321 printk(KERN_INFO "ata%u: SATA link up %s Gbps (SStatus %X)\n",
1322 ap->id, speed, sstatus);
1323 } else {
1324 printk(KERN_INFO "ata%u: SATA link down (SStatus %X)\n",
1325 ap->id, sstatus);
1326 }
1327}
1328
1329/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001330 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1331 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001333 * This function issues commands to standard SATA Sxxx
1334 * PHY registers, to wake up the phy (and device), and
1335 * clear any reset condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 *
1337 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001338 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 *
1340 */
1341void __sata_phy_reset(struct ata_port *ap)
1342{
1343 u32 sstatus;
1344 unsigned long timeout = jiffies + (HZ * 5);
1345
1346 if (ap->flags & ATA_FLAG_SATA_RESET) {
Brett Russcdcca89e2005-03-28 15:10:27 -05001347 /* issue phy wake/reset */
1348 scr_write_flush(ap, SCR_CONTROL, 0x301);
Tejun Heo62ba2842005-06-26 23:27:19 +09001349 /* Couldn't find anything in SATA I/II specs, but
1350 * AHCI-1.1 10.4.2 says at least 1 ms. */
1351 mdelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 }
Brett Russcdcca89e2005-03-28 15:10:27 -05001353 scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
1355 /* wait for phy to become ready, if necessary */
1356 do {
1357 msleep(200);
1358 sstatus = scr_read(ap, SCR_STATUS);
1359 if ((sstatus & 0xf) != 1)
1360 break;
1361 } while (time_before(jiffies, timeout));
1362
Tejun Heo3be680b2005-12-19 22:35:02 +09001363 /* print link status */
1364 sata_print_link_status(ap);
Jeff Garzik656563e2005-11-20 03:36:45 -05001365
Tejun Heo3be680b2005-12-19 22:35:02 +09001366 /* TODO: phy layer with polling, timeouts, etc. */
1367 if (sata_dev_present(ap))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 ata_port_probe(ap);
Tejun Heo3be680b2005-12-19 22:35:02 +09001369 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 ata_port_disable(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371
1372 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1373 return;
1374
1375 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1376 ata_port_disable(ap);
1377 return;
1378 }
1379
1380 ap->cbl = ATA_CBL_SATA;
1381}
1382
1383/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001384 * sata_phy_reset - Reset SATA bus.
1385 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001387 * This function resets the SATA bus, and then probes
1388 * the bus for devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 *
1390 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001391 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392 *
1393 */
1394void sata_phy_reset(struct ata_port *ap)
1395{
1396 __sata_phy_reset(ap);
1397 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1398 return;
1399 ata_bus_reset(ap);
1400}
1401
1402/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001403 * ata_port_disable - Disable port.
1404 * @ap: Port to be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001406 * Modify @ap data structure such that the system
1407 * thinks that the entire port is disabled, and should
1408 * never attempt to probe or communicate with devices
1409 * on this port.
1410 *
1411 * LOCKING: host_set lock, or some other form of
1412 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 */
1414
1415void ata_port_disable(struct ata_port *ap)
1416{
1417 ap->device[0].class = ATA_DEV_NONE;
1418 ap->device[1].class = ATA_DEV_NONE;
1419 ap->flags |= ATA_FLAG_PORT_DISABLED;
1420}
1421
Alan Cox452503f2005-10-21 19:01:32 -04001422/*
1423 * This mode timing computation functionality is ported over from
1424 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
1425 */
1426/*
1427 * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
1428 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
1429 * for PIO 5, which is a nonstandard extension and UDMA6, which
1430 * is currently supported only by Maxtor drives.
1431 */
1432
1433static const struct ata_timing ata_timing[] = {
1434
1435 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
1436 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
1437 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
1438 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
1439
1440 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
1441 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
1442 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
1443
1444/* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
1445
1446 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
1447 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
1448 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
1449
1450 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
1451 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
1452 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
1453
1454/* { XFER_PIO_5, 20, 50, 30, 100, 50, 30, 100, 0 }, */
1455 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
1456 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
1457
1458 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
1459 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
1460 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
1461
1462/* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
1463
1464 { 0xFF }
1465};
1466
1467#define ENOUGH(v,unit) (((v)-1)/(unit)+1)
1468#define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
1469
1470static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
1471{
1472 q->setup = EZ(t->setup * 1000, T);
1473 q->act8b = EZ(t->act8b * 1000, T);
1474 q->rec8b = EZ(t->rec8b * 1000, T);
1475 q->cyc8b = EZ(t->cyc8b * 1000, T);
1476 q->active = EZ(t->active * 1000, T);
1477 q->recover = EZ(t->recover * 1000, T);
1478 q->cycle = EZ(t->cycle * 1000, T);
1479 q->udma = EZ(t->udma * 1000, UT);
1480}
1481
1482void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
1483 struct ata_timing *m, unsigned int what)
1484{
1485 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
1486 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
1487 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
1488 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
1489 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
1490 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
1491 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
1492 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
1493}
1494
1495static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
1496{
1497 const struct ata_timing *t;
1498
1499 for (t = ata_timing; t->mode != speed; t++)
Alan Cox91190752005-10-26 12:17:46 -04001500 if (t->mode == 0xFF)
Alan Cox452503f2005-10-21 19:01:32 -04001501 return NULL;
1502 return t;
1503}
1504
1505int ata_timing_compute(struct ata_device *adev, unsigned short speed,
1506 struct ata_timing *t, int T, int UT)
1507{
1508 const struct ata_timing *s;
1509 struct ata_timing p;
1510
1511 /*
1512 * Find the mode.
Albert Lee75b1f2f2005-11-16 17:06:18 +08001513 */
Alan Cox452503f2005-10-21 19:01:32 -04001514
1515 if (!(s = ata_timing_find_mode(speed)))
1516 return -EINVAL;
1517
Albert Lee75b1f2f2005-11-16 17:06:18 +08001518 memcpy(t, s, sizeof(*s));
1519
Alan Cox452503f2005-10-21 19:01:32 -04001520 /*
1521 * If the drive is an EIDE drive, it can tell us it needs extended
1522 * PIO/MW_DMA cycle timing.
1523 */
1524
1525 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
1526 memset(&p, 0, sizeof(p));
1527 if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
1528 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
1529 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
1530 } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
1531 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
1532 }
1533 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
1534 }
1535
1536 /*
1537 * Convert the timing to bus clock counts.
1538 */
1539
Albert Lee75b1f2f2005-11-16 17:06:18 +08001540 ata_timing_quantize(t, t, T, UT);
Alan Cox452503f2005-10-21 19:01:32 -04001541
1542 /*
Randy Dunlapc893a3a2006-01-28 13:15:32 -05001543 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
1544 * S.M.A.R.T * and some other commands. We have to ensure that the
1545 * DMA cycle timing is slower/equal than the fastest PIO timing.
Alan Cox452503f2005-10-21 19:01:32 -04001546 */
1547
1548 if (speed > XFER_PIO_4) {
1549 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
1550 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
1551 }
1552
1553 /*
Randy Dunlapc893a3a2006-01-28 13:15:32 -05001554 * Lengthen active & recovery time so that cycle time is correct.
Alan Cox452503f2005-10-21 19:01:32 -04001555 */
1556
1557 if (t->act8b + t->rec8b < t->cyc8b) {
1558 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
1559 t->rec8b = t->cyc8b - t->act8b;
1560 }
1561
1562 if (t->active + t->recover < t->cycle) {
1563 t->active += (t->cycle - (t->active + t->recover)) / 2;
1564 t->recover = t->cycle - t->active;
1565 }
1566
1567 return 0;
1568}
1569
Jeff Garzik057ace52005-10-22 14:27:05 -04001570static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 unsigned int shift;
1572 u8 base;
1573} xfer_mode_classes[] = {
1574 { ATA_SHIFT_UDMA, XFER_UDMA_0 },
1575 { ATA_SHIFT_MWDMA, XFER_MW_DMA_0 },
1576 { ATA_SHIFT_PIO, XFER_PIO_0 },
1577};
1578
Arjan van de Ven858119e2006-01-14 13:20:43 -08001579static u8 base_from_shift(unsigned int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580{
1581 int i;
1582
1583 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++)
1584 if (xfer_mode_classes[i].shift == shift)
1585 return xfer_mode_classes[i].base;
1586
1587 return 0xff;
1588}
1589
1590static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
1591{
1592 int ofs, idx;
1593 u8 base;
1594
1595 if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1596 return;
1597
1598 if (dev->xfer_shift == ATA_SHIFT_PIO)
1599 dev->flags |= ATA_DFLAG_PIO;
1600
1601 ata_dev_set_xfermode(ap, dev);
1602
1603 base = base_from_shift(dev->xfer_shift);
1604 ofs = dev->xfer_mode - base;
1605 idx = ofs + dev->xfer_shift;
1606 WARN_ON(idx >= ARRAY_SIZE(xfer_mode_str));
1607
1608 DPRINTK("idx=%d xfer_shift=%u, xfer_mode=0x%x, base=0x%x, offset=%d\n",
1609 idx, dev->xfer_shift, (int)dev->xfer_mode, (int)base, ofs);
1610
1611 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
1612 ap->id, dev->devno, xfer_mode_str[idx]);
1613}
1614
1615static int ata_host_set_pio(struct ata_port *ap)
1616{
1617 unsigned int mask;
1618 int x, i;
1619 u8 base, xfer_mode;
1620
1621 mask = ata_get_mode_mask(ap, ATA_SHIFT_PIO);
1622 x = fgb(mask);
1623 if (x < 0) {
1624 printk(KERN_WARNING "ata%u: no PIO support\n", ap->id);
1625 return -1;
1626 }
1627
1628 base = base_from_shift(ATA_SHIFT_PIO);
1629 xfer_mode = base + x;
1630
1631 DPRINTK("base 0x%x xfer_mode 0x%x mask 0x%x x %d\n",
1632 (int)base, (int)xfer_mode, mask, x);
1633
1634 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1635 struct ata_device *dev = &ap->device[i];
1636 if (ata_dev_present(dev)) {
1637 dev->pio_mode = xfer_mode;
1638 dev->xfer_mode = xfer_mode;
1639 dev->xfer_shift = ATA_SHIFT_PIO;
1640 if (ap->ops->set_piomode)
1641 ap->ops->set_piomode(ap, dev);
1642 }
1643 }
1644
1645 return 0;
1646}
1647
1648static void ata_host_set_dma(struct ata_port *ap, u8 xfer_mode,
1649 unsigned int xfer_shift)
1650{
1651 int i;
1652
1653 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1654 struct ata_device *dev = &ap->device[i];
1655 if (ata_dev_present(dev)) {
1656 dev->dma_mode = xfer_mode;
1657 dev->xfer_mode = xfer_mode;
1658 dev->xfer_shift = xfer_shift;
1659 if (ap->ops->set_dmamode)
1660 ap->ops->set_dmamode(ap, dev);
1661 }
1662 }
1663}
1664
1665/**
1666 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1667 * @ap: port on which timings will be programmed
1668 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001669 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1670 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001671 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001672 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 */
1674static void ata_set_mode(struct ata_port *ap)
1675{
Albert Lee8cbd6df2005-10-12 15:06:27 +08001676 unsigned int xfer_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 u8 xfer_mode;
1678 int rc;
1679
1680 /* step 1: always set host PIO timings */
1681 rc = ata_host_set_pio(ap);
1682 if (rc)
1683 goto err_out;
1684
1685 /* step 2: choose the best data xfer mode */
1686 xfer_mode = xfer_shift = 0;
1687 rc = ata_choose_xfer_mode(ap, &xfer_mode, &xfer_shift);
1688 if (rc)
1689 goto err_out;
1690
1691 /* step 3: if that xfer mode isn't PIO, set host DMA timings */
1692 if (xfer_shift != ATA_SHIFT_PIO)
1693 ata_host_set_dma(ap, xfer_mode, xfer_shift);
1694
1695 /* step 4: update devices' xfer mode */
1696 ata_dev_set_mode(ap, &ap->device[0]);
1697 ata_dev_set_mode(ap, &ap->device[1]);
1698
1699 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1700 return;
1701
1702 if (ap->ops->post_set_mode)
1703 ap->ops->post_set_mode(ap);
1704
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 return;
1706
1707err_out:
1708 ata_port_disable(ap);
1709}
1710
1711/**
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05001712 * ata_tf_to_host - issue ATA taskfile to host controller
1713 * @ap: port to which command is being issued
1714 * @tf: ATA taskfile register set
1715 *
1716 * Issues ATA taskfile register set to ATA host controller,
1717 * with proper synchronization with interrupt handler and
1718 * other threads.
1719 *
1720 * LOCKING:
1721 * spin_lock_irqsave(host_set lock)
1722 */
1723
1724static inline void ata_tf_to_host(struct ata_port *ap,
1725 const struct ata_taskfile *tf)
1726{
1727 ap->ops->tf_load(ap, tf);
1728 ap->ops->exec_command(ap, tf);
1729}
1730
1731/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 * ata_busy_sleep - sleep until BSY clears, or timeout
1733 * @ap: port containing status register to be polled
1734 * @tmout_pat: impatience timeout
1735 * @tmout: overall timeout
1736 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001737 * Sleep until ATA Status register bit BSY clears,
1738 * or a timeout occurs.
1739 *
1740 * LOCKING: None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 */
1742
Tejun Heo6f8b9952006-01-24 17:05:21 +09001743unsigned int ata_busy_sleep (struct ata_port *ap,
1744 unsigned long tmout_pat, unsigned long tmout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745{
1746 unsigned long timer_start, timeout;
1747 u8 status;
1748
1749 status = ata_busy_wait(ap, ATA_BUSY, 300);
1750 timer_start = jiffies;
1751 timeout = timer_start + tmout_pat;
1752 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1753 msleep(50);
1754 status = ata_busy_wait(ap, ATA_BUSY, 3);
1755 }
1756
1757 if (status & ATA_BUSY)
1758 printk(KERN_WARNING "ata%u is slow to respond, "
1759 "please be patient\n", ap->id);
1760
1761 timeout = timer_start + tmout;
1762 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1763 msleep(50);
1764 status = ata_chk_status(ap);
1765 }
1766
1767 if (status & ATA_BUSY) {
1768 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
1769 ap->id, tmout / HZ);
1770 return 1;
1771 }
1772
1773 return 0;
1774}
1775
1776static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
1777{
1778 struct ata_ioports *ioaddr = &ap->ioaddr;
1779 unsigned int dev0 = devmask & (1 << 0);
1780 unsigned int dev1 = devmask & (1 << 1);
1781 unsigned long timeout;
1782
1783 /* if device 0 was found in ata_devchk, wait for its
1784 * BSY bit to clear
1785 */
1786 if (dev0)
1787 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1788
1789 /* if device 1 was found in ata_devchk, wait for
1790 * register access, then wait for BSY to clear
1791 */
1792 timeout = jiffies + ATA_TMOUT_BOOT;
1793 while (dev1) {
1794 u8 nsect, lbal;
1795
1796 ap->ops->dev_select(ap, 1);
1797 if (ap->flags & ATA_FLAG_MMIO) {
1798 nsect = readb((void __iomem *) ioaddr->nsect_addr);
1799 lbal = readb((void __iomem *) ioaddr->lbal_addr);
1800 } else {
1801 nsect = inb(ioaddr->nsect_addr);
1802 lbal = inb(ioaddr->lbal_addr);
1803 }
1804 if ((nsect == 1) && (lbal == 1))
1805 break;
1806 if (time_after(jiffies, timeout)) {
1807 dev1 = 0;
1808 break;
1809 }
1810 msleep(50); /* give drive a breather */
1811 }
1812 if (dev1)
1813 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1814
1815 /* is all this really necessary? */
1816 ap->ops->dev_select(ap, 0);
1817 if (dev1)
1818 ap->ops->dev_select(ap, 1);
1819 if (dev0)
1820 ap->ops->dev_select(ap, 0);
1821}
1822
1823/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001824 * ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
1825 * @ap: Port to reset and probe
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001827 * Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
1828 * probe the bus. Not often used these days.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 *
1830 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001831 * PCI/etc. bus probe sem.
Jeff Garzike5338252005-10-30 21:37:17 -05001832 * Obtains host_set lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 *
1834 */
1835
1836static unsigned int ata_bus_edd(struct ata_port *ap)
1837{
1838 struct ata_taskfile tf;
Jeff Garzike5338252005-10-30 21:37:17 -05001839 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840
1841 /* set up execute-device-diag (bus reset) taskfile */
1842 /* also, take interrupts to a known state (disabled) */
1843 DPRINTK("execute-device-diag\n");
1844 ata_tf_init(ap, &tf, 0);
1845 tf.ctl |= ATA_NIEN;
1846 tf.command = ATA_CMD_EDD;
1847 tf.protocol = ATA_PROT_NODATA;
1848
1849 /* do bus reset */
Jeff Garzike5338252005-10-30 21:37:17 -05001850 spin_lock_irqsave(&ap->host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 ata_tf_to_host(ap, &tf);
Jeff Garzike5338252005-10-30 21:37:17 -05001852 spin_unlock_irqrestore(&ap->host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853
1854 /* spec says at least 2ms. but who knows with those
1855 * crazy ATAPI devices...
1856 */
1857 msleep(150);
1858
1859 return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1860}
1861
1862static unsigned int ata_bus_softreset(struct ata_port *ap,
1863 unsigned int devmask)
1864{
1865 struct ata_ioports *ioaddr = &ap->ioaddr;
1866
1867 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
1868
1869 /* software reset. causes dev0 to be selected */
1870 if (ap->flags & ATA_FLAG_MMIO) {
1871 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1872 udelay(20); /* FIXME: flush */
1873 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
1874 udelay(20); /* FIXME: flush */
1875 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1876 } else {
1877 outb(ap->ctl, ioaddr->ctl_addr);
1878 udelay(10);
1879 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
1880 udelay(10);
1881 outb(ap->ctl, ioaddr->ctl_addr);
1882 }
1883
1884 /* spec mandates ">= 2ms" before checking status.
1885 * We wait 150ms, because that was the magic delay used for
1886 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
1887 * between when the ATA command register is written, and then
1888 * status is checked. Because waiting for "a while" before
1889 * checking status is fine, post SRST, we perform this magic
1890 * delay here as well.
1891 */
1892 msleep(150);
1893
1894 ata_bus_post_reset(ap, devmask);
1895
1896 return 0;
1897}
1898
1899/**
1900 * ata_bus_reset - reset host port and associated ATA channel
1901 * @ap: port to reset
1902 *
1903 * This is typically the first time we actually start issuing
1904 * commands to the ATA channel. We wait for BSY to clear, then
1905 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
1906 * result. Determine what devices, if any, are on the channel
1907 * by looking at the device 0/1 error register. Look at the signature
1908 * stored in each device's taskfile registers, to determine if
1909 * the device is ATA or ATAPI.
1910 *
1911 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001912 * PCI/etc. bus probe sem.
1913 * Obtains host_set lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 *
1915 * SIDE EFFECTS:
1916 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
1917 */
1918
1919void ata_bus_reset(struct ata_port *ap)
1920{
1921 struct ata_ioports *ioaddr = &ap->ioaddr;
1922 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
1923 u8 err;
1924 unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
1925
1926 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
1927
1928 /* determine if device 0/1 are present */
1929 if (ap->flags & ATA_FLAG_SATA_RESET)
1930 dev0 = 1;
1931 else {
1932 dev0 = ata_devchk(ap, 0);
1933 if (slave_possible)
1934 dev1 = ata_devchk(ap, 1);
1935 }
1936
1937 if (dev0)
1938 devmask |= (1 << 0);
1939 if (dev1)
1940 devmask |= (1 << 1);
1941
1942 /* select device 0 again */
1943 ap->ops->dev_select(ap, 0);
1944
1945 /* issue bus reset */
1946 if (ap->flags & ATA_FLAG_SRST)
1947 rc = ata_bus_softreset(ap, devmask);
1948 else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
1949 /* set up device control */
1950 if (ap->flags & ATA_FLAG_MMIO)
1951 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1952 else
1953 outb(ap->ctl, ioaddr->ctl_addr);
1954 rc = ata_bus_edd(ap);
1955 }
1956
1957 if (rc)
1958 goto err_out;
1959
1960 /*
1961 * determine by signature whether we have ATA or ATAPI devices
1962 */
Tejun Heob4dc7622006-01-24 17:05:22 +09001963 ap->device[0].class = ata_dev_try_classify(ap, 0, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 if ((slave_possible) && (err != 0x81))
Tejun Heob4dc7622006-01-24 17:05:22 +09001965 ap->device[1].class = ata_dev_try_classify(ap, 1, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
1967 /* re-enable interrupts */
1968 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
1969 ata_irq_on(ap);
1970
1971 /* is double-select really necessary? */
1972 if (ap->device[1].class != ATA_DEV_NONE)
1973 ap->ops->dev_select(ap, 1);
1974 if (ap->device[0].class != ATA_DEV_NONE)
1975 ap->ops->dev_select(ap, 0);
1976
1977 /* if no devices were detected, disable this port */
1978 if ((ap->device[0].class == ATA_DEV_NONE) &&
1979 (ap->device[1].class == ATA_DEV_NONE))
1980 goto err_out;
1981
1982 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
1983 /* set up device control for ATA_FLAG_SATA_RESET */
1984 if (ap->flags & ATA_FLAG_MMIO)
1985 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1986 else
1987 outb(ap->ctl, ioaddr->ctl_addr);
1988 }
1989
1990 DPRINTK("EXIT\n");
1991 return;
1992
1993err_out:
1994 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
1995 ap->ops->port_disable(ap);
1996
1997 DPRINTK("EXIT\n");
1998}
1999
Tejun Heo7a7921e2006-02-02 18:20:00 +09002000static int sata_phy_resume(struct ata_port *ap)
2001{
2002 unsigned long timeout = jiffies + (HZ * 5);
2003 u32 sstatus;
2004
2005 scr_write_flush(ap, SCR_CONTROL, 0x300);
2006
2007 /* Wait for phy to become ready, if necessary. */
2008 do {
2009 msleep(200);
2010 sstatus = scr_read(ap, SCR_STATUS);
2011 if ((sstatus & 0xf) != 1)
2012 return 0;
2013 } while (time_before(jiffies, timeout));
2014
2015 return -1;
2016}
2017
Tejun Heoc2bd5802006-01-24 17:05:22 +09002018/**
Tejun Heo8a19ac82006-02-02 18:20:00 +09002019 * ata_std_probeinit - initialize probing
2020 * @ap: port to be probed
2021 *
2022 * @ap is about to be probed. Initialize it. This function is
2023 * to be used as standard callback for ata_drive_probe_reset().
Tejun Heo3a397462006-02-10 23:58:48 +09002024 *
2025 * NOTE!!! Do not use this function as probeinit if a low level
2026 * driver implements only hardreset. Just pass NULL as probeinit
2027 * in that case. Using this function is probably okay but doing
2028 * so makes reset sequence different from the original
2029 * ->phy_reset implementation and Jeff nervous. :-P
Tejun Heo8a19ac82006-02-02 18:20:00 +09002030 */
2031extern void ata_std_probeinit(struct ata_port *ap)
2032{
Tejun Heo3a397462006-02-10 23:58:48 +09002033 if (ap->flags & ATA_FLAG_SATA && ap->ops->scr_read) {
Tejun Heo8a19ac82006-02-02 18:20:00 +09002034 sata_phy_resume(ap);
Tejun Heo3a397462006-02-10 23:58:48 +09002035 if (sata_dev_present(ap))
2036 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2037 }
Tejun Heo8a19ac82006-02-02 18:20:00 +09002038}
2039
2040/**
Tejun Heoc2bd5802006-01-24 17:05:22 +09002041 * ata_std_softreset - reset host port via ATA SRST
2042 * @ap: port to reset
2043 * @verbose: fail verbosely
2044 * @classes: resulting classes of attached devices
2045 *
2046 * Reset host port using ATA SRST. This function is to be used
2047 * as standard callback for ata_drive_*_reset() functions.
2048 *
2049 * LOCKING:
2050 * Kernel thread context (may sleep)
2051 *
2052 * RETURNS:
2053 * 0 on success, -errno otherwise.
2054 */
2055int ata_std_softreset(struct ata_port *ap, int verbose, unsigned int *classes)
2056{
2057 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2058 unsigned int devmask = 0, err_mask;
2059 u8 err;
2060
2061 DPRINTK("ENTER\n");
2062
Tejun Heo3a397462006-02-10 23:58:48 +09002063 if (ap->ops->scr_read && !sata_dev_present(ap)) {
2064 classes[0] = ATA_DEV_NONE;
2065 goto out;
2066 }
2067
Tejun Heoc2bd5802006-01-24 17:05:22 +09002068 /* determine if device 0/1 are present */
2069 if (ata_devchk(ap, 0))
2070 devmask |= (1 << 0);
2071 if (slave_possible && ata_devchk(ap, 1))
2072 devmask |= (1 << 1);
2073
Tejun Heoc2bd5802006-01-24 17:05:22 +09002074 /* select device 0 again */
2075 ap->ops->dev_select(ap, 0);
2076
2077 /* issue bus reset */
2078 DPRINTK("about to softreset, devmask=%x\n", devmask);
2079 err_mask = ata_bus_softreset(ap, devmask);
2080 if (err_mask) {
2081 if (verbose)
2082 printk(KERN_ERR "ata%u: SRST failed (err_mask=0x%x)\n",
2083 ap->id, err_mask);
2084 else
2085 DPRINTK("EXIT, softreset failed (err_mask=0x%x)\n",
2086 err_mask);
2087 return -EIO;
2088 }
2089
2090 /* determine by signature whether we have ATA or ATAPI devices */
2091 classes[0] = ata_dev_try_classify(ap, 0, &err);
2092 if (slave_possible && err != 0x81)
2093 classes[1] = ata_dev_try_classify(ap, 1, &err);
2094
Tejun Heo3a397462006-02-10 23:58:48 +09002095 out:
Tejun Heoc2bd5802006-01-24 17:05:22 +09002096 DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]);
2097 return 0;
2098}
2099
2100/**
2101 * sata_std_hardreset - reset host port via SATA phy reset
2102 * @ap: port to reset
2103 * @verbose: fail verbosely
2104 * @class: resulting class of attached device
2105 *
2106 * SATA phy-reset host port using DET bits of SControl register.
2107 * This function is to be used as standard callback for
2108 * ata_drive_*_reset().
2109 *
2110 * LOCKING:
2111 * Kernel thread context (may sleep)
2112 *
2113 * RETURNS:
2114 * 0 on success, -errno otherwise.
2115 */
2116int sata_std_hardreset(struct ata_port *ap, int verbose, unsigned int *class)
2117{
Tejun Heoc2bd5802006-01-24 17:05:22 +09002118 DPRINTK("ENTER\n");
2119
2120 /* Issue phy wake/reset */
2121 scr_write_flush(ap, SCR_CONTROL, 0x301);
2122
2123 /*
2124 * Couldn't find anything in SATA I/II specs, but AHCI-1.1
2125 * 10.4.2 says at least 1 ms.
2126 */
2127 msleep(1);
2128
Tejun Heo7a7921e2006-02-02 18:20:00 +09002129 /* Bring phy back */
2130 sata_phy_resume(ap);
Tejun Heoc2bd5802006-01-24 17:05:22 +09002131
Tejun Heoc2bd5802006-01-24 17:05:22 +09002132 /* TODO: phy layer with polling, timeouts, etc. */
2133 if (!sata_dev_present(ap)) {
2134 *class = ATA_DEV_NONE;
2135 DPRINTK("EXIT, link offline\n");
2136 return 0;
2137 }
2138
2139 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
2140 if (verbose)
2141 printk(KERN_ERR "ata%u: COMRESET failed "
2142 "(device not ready)\n", ap->id);
2143 else
2144 DPRINTK("EXIT, device not ready\n");
2145 return -EIO;
2146 }
2147
Tejun Heo3a397462006-02-10 23:58:48 +09002148 ap->ops->dev_select(ap, 0); /* probably unnecessary */
2149
Tejun Heoc2bd5802006-01-24 17:05:22 +09002150 *class = ata_dev_try_classify(ap, 0, NULL);
2151
2152 DPRINTK("EXIT, class=%u\n", *class);
2153 return 0;
2154}
2155
2156/**
2157 * ata_std_postreset - standard postreset callback
2158 * @ap: the target ata_port
2159 * @classes: classes of attached devices
2160 *
2161 * This function is invoked after a successful reset. Note that
2162 * the device might have been reset more than once using
2163 * different reset methods before postreset is invoked.
Tejun Heoc2bd5802006-01-24 17:05:22 +09002164 *
2165 * This function is to be used as standard callback for
2166 * ata_drive_*_reset().
2167 *
2168 * LOCKING:
2169 * Kernel thread context (may sleep)
2170 */
2171void ata_std_postreset(struct ata_port *ap, unsigned int *classes)
2172{
2173 DPRINTK("ENTER\n");
2174
Tejun Heo56497bd2006-02-15 15:01:42 +09002175 /* set cable type if it isn't already set */
Tejun Heoc2bd5802006-01-24 17:05:22 +09002176 if (ap->cbl == ATA_CBL_NONE && ap->flags & ATA_FLAG_SATA)
2177 ap->cbl = ATA_CBL_SATA;
2178
2179 /* print link status */
2180 if (ap->cbl == ATA_CBL_SATA)
2181 sata_print_link_status(ap);
2182
Tejun Heo3a397462006-02-10 23:58:48 +09002183 /* re-enable interrupts */
2184 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2185 ata_irq_on(ap);
Tejun Heoc2bd5802006-01-24 17:05:22 +09002186
2187 /* is double-select really necessary? */
2188 if (classes[0] != ATA_DEV_NONE)
2189 ap->ops->dev_select(ap, 1);
2190 if (classes[1] != ATA_DEV_NONE)
2191 ap->ops->dev_select(ap, 0);
2192
Tejun Heo3a397462006-02-10 23:58:48 +09002193 /* bail out if no device is present */
2194 if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
2195 DPRINTK("EXIT, no device\n");
2196 return;
2197 }
2198
2199 /* set up device control */
2200 if (ap->ioaddr.ctl_addr) {
2201 if (ap->flags & ATA_FLAG_MMIO)
2202 writeb(ap->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
2203 else
2204 outb(ap->ctl, ap->ioaddr.ctl_addr);
2205 }
Tejun Heoc2bd5802006-01-24 17:05:22 +09002206
2207 DPRINTK("EXIT\n");
2208}
2209
2210/**
2211 * ata_std_probe_reset - standard probe reset method
2212 * @ap: prot to perform probe-reset
2213 * @classes: resulting classes of attached devices
2214 *
2215 * The stock off-the-shelf ->probe_reset method.
2216 *
2217 * LOCKING:
2218 * Kernel thread context (may sleep)
2219 *
2220 * RETURNS:
2221 * 0 on success, -errno otherwise.
2222 */
2223int ata_std_probe_reset(struct ata_port *ap, unsigned int *classes)
2224{
2225 ata_reset_fn_t hardreset;
2226
2227 hardreset = NULL;
Tejun Heob911fc32006-02-02 18:20:00 +09002228 if (ap->flags & ATA_FLAG_SATA && ap->ops->scr_read)
Tejun Heoc2bd5802006-01-24 17:05:22 +09002229 hardreset = sata_std_hardreset;
2230
Tejun Heo8a19ac82006-02-02 18:20:00 +09002231 return ata_drive_probe_reset(ap, ata_std_probeinit,
Tejun Heo7944ea92006-02-02 18:20:00 +09002232 ata_std_softreset, hardreset,
Tejun Heoc2bd5802006-01-24 17:05:22 +09002233 ata_std_postreset, classes);
2234}
2235
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002236static int do_probe_reset(struct ata_port *ap, ata_reset_fn_t reset,
2237 ata_postreset_fn_t postreset,
2238 unsigned int *classes)
2239{
2240 int i, rc;
2241
2242 for (i = 0; i < ATA_MAX_DEVICES; i++)
2243 classes[i] = ATA_DEV_UNKNOWN;
2244
2245 rc = reset(ap, 0, classes);
2246 if (rc)
2247 return rc;
2248
2249 /* If any class isn't ATA_DEV_UNKNOWN, consider classification
2250 * is complete and convert all ATA_DEV_UNKNOWN to
2251 * ATA_DEV_NONE.
2252 */
2253 for (i = 0; i < ATA_MAX_DEVICES; i++)
2254 if (classes[i] != ATA_DEV_UNKNOWN)
2255 break;
2256
2257 if (i < ATA_MAX_DEVICES)
2258 for (i = 0; i < ATA_MAX_DEVICES; i++)
2259 if (classes[i] == ATA_DEV_UNKNOWN)
2260 classes[i] = ATA_DEV_NONE;
2261
2262 if (postreset)
2263 postreset(ap, classes);
2264
2265 return classes[0] != ATA_DEV_UNKNOWN ? 0 : -ENODEV;
2266}
2267
2268/**
2269 * ata_drive_probe_reset - Perform probe reset with given methods
2270 * @ap: port to reset
Tejun Heo7944ea92006-02-02 18:20:00 +09002271 * @probeinit: probeinit method (can be NULL)
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002272 * @softreset: softreset method (can be NULL)
2273 * @hardreset: hardreset method (can be NULL)
2274 * @postreset: postreset method (can be NULL)
2275 * @classes: resulting classes of attached devices
2276 *
2277 * Reset the specified port and classify attached devices using
2278 * given methods. This function prefers softreset but tries all
2279 * possible reset sequences to reset and classify devices. This
2280 * function is intended to be used for constructing ->probe_reset
2281 * callback by low level drivers.
2282 *
2283 * Reset methods should follow the following rules.
2284 *
2285 * - Return 0 on sucess, -errno on failure.
2286 * - If classification is supported, fill classes[] with
2287 * recognized class codes.
2288 * - If classification is not supported, leave classes[] alone.
2289 * - If verbose is non-zero, print error message on failure;
2290 * otherwise, shut up.
2291 *
2292 * LOCKING:
2293 * Kernel thread context (may sleep)
2294 *
2295 * RETURNS:
2296 * 0 on success, -EINVAL if no reset method is avaliable, -ENODEV
2297 * if classification fails, and any error code from reset
2298 * methods.
2299 */
Tejun Heo7944ea92006-02-02 18:20:00 +09002300int ata_drive_probe_reset(struct ata_port *ap, ata_probeinit_fn_t probeinit,
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002301 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2302 ata_postreset_fn_t postreset, unsigned int *classes)
2303{
2304 int rc = -EINVAL;
2305
Tejun Heo7944ea92006-02-02 18:20:00 +09002306 if (probeinit)
2307 probeinit(ap);
2308
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002309 if (softreset) {
2310 rc = do_probe_reset(ap, softreset, postreset, classes);
2311 if (rc == 0)
2312 return 0;
2313 }
2314
2315 if (!hardreset)
2316 return rc;
2317
2318 rc = do_probe_reset(ap, hardreset, postreset, classes);
2319 if (rc == 0 || rc != -ENODEV)
2320 return rc;
2321
2322 if (softreset)
2323 rc = do_probe_reset(ap, softreset, postreset, classes);
2324
2325 return rc;
2326}
2327
Jeff Garzik057ace52005-10-22 14:27:05 -04002328static void ata_pr_blacklisted(const struct ata_port *ap,
2329 const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330{
2331 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, disabling DMA\n",
2332 ap->id, dev->devno);
2333}
2334
Arjan van de Ven98ac62d2005-11-28 10:06:23 +01002335static const char * const ata_dma_blacklist [] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 "WDC AC11000H",
2337 "WDC AC22100H",
2338 "WDC AC32500H",
2339 "WDC AC33100H",
2340 "WDC AC31600H",
2341 "WDC AC32100H",
2342 "WDC AC23200L",
2343 "Compaq CRD-8241B",
2344 "CRD-8400B",
2345 "CRD-8480B",
2346 "CRD-8482B",
2347 "CRD-84",
2348 "SanDisk SDP3B",
2349 "SanDisk SDP3B-64",
2350 "SANYO CD-ROM CRD",
2351 "HITACHI CDR-8",
2352 "HITACHI CDR-8335",
2353 "HITACHI CDR-8435",
2354 "Toshiba CD-ROM XM-6202B",
Jeff Garzike9222562005-06-28 00:03:37 -04002355 "TOSHIBA CD-ROM XM-1702BC",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 "CD-532E-A",
2357 "E-IDE CD-ROM CR-840",
2358 "CD-ROM Drive/F5A",
2359 "WPI CDD-820",
2360 "SAMSUNG CD-ROM SC-148C",
2361 "SAMSUNG CD-ROM SC",
2362 "SanDisk SDP3B-64",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 "ATAPI CD-ROM DRIVE 40X MAXIMUM",
2364 "_NEC DV5800A",
2365};
2366
Jeff Garzik057ace52005-10-22 14:27:05 -04002367static int ata_dma_blacklisted(const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368{
Tejun Heo2e026712006-02-12 22:47:04 +09002369 unsigned char model_num[41];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 int i;
2371
Tejun Heo6a62a042006-02-13 10:02:46 +09002372 ata_id_c_string(dev->id, model_num, ATA_ID_PROD_OFS, sizeof(model_num));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373
2374 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i++)
Tejun Heo2e026712006-02-12 22:47:04 +09002375 if (!strcmp(ata_dma_blacklist[i], model_num))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 return 1;
2377
2378 return 0;
2379}
2380
Jeff Garzik057ace52005-10-22 14:27:05 -04002381static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382{
Jeff Garzik057ace52005-10-22 14:27:05 -04002383 const struct ata_device *master, *slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 unsigned int mask;
2385
2386 master = &ap->device[0];
2387 slave = &ap->device[1];
2388
Tejun Heoa46314742006-02-11 19:11:13 +09002389 WARN_ON(!ata_dev_present(master) && !ata_dev_present(slave));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390
2391 if (shift == ATA_SHIFT_UDMA) {
2392 mask = ap->udma_mask;
2393 if (ata_dev_present(master)) {
2394 mask &= (master->id[ATA_ID_UDMA_MODES] & 0xff);
Jeff Garzik057ace52005-10-22 14:27:05 -04002395 if (ata_dma_blacklisted(master)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396 mask = 0;
2397 ata_pr_blacklisted(ap, master);
2398 }
2399 }
2400 if (ata_dev_present(slave)) {
2401 mask &= (slave->id[ATA_ID_UDMA_MODES] & 0xff);
Jeff Garzik057ace52005-10-22 14:27:05 -04002402 if (ata_dma_blacklisted(slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 mask = 0;
2404 ata_pr_blacklisted(ap, slave);
2405 }
2406 }
2407 }
2408 else if (shift == ATA_SHIFT_MWDMA) {
2409 mask = ap->mwdma_mask;
2410 if (ata_dev_present(master)) {
2411 mask &= (master->id[ATA_ID_MWDMA_MODES] & 0x07);
Jeff Garzik057ace52005-10-22 14:27:05 -04002412 if (ata_dma_blacklisted(master)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 mask = 0;
2414 ata_pr_blacklisted(ap, master);
2415 }
2416 }
2417 if (ata_dev_present(slave)) {
2418 mask &= (slave->id[ATA_ID_MWDMA_MODES] & 0x07);
Jeff Garzik057ace52005-10-22 14:27:05 -04002419 if (ata_dma_blacklisted(slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 mask = 0;
2421 ata_pr_blacklisted(ap, slave);
2422 }
2423 }
2424 }
2425 else if (shift == ATA_SHIFT_PIO) {
2426 mask = ap->pio_mask;
2427 if (ata_dev_present(master)) {
2428 /* spec doesn't return explicit support for
2429 * PIO0-2, so we fake it
2430 */
2431 u16 tmp_mode = master->id[ATA_ID_PIO_MODES] & 0x03;
2432 tmp_mode <<= 3;
2433 tmp_mode |= 0x7;
2434 mask &= tmp_mode;
2435 }
2436 if (ata_dev_present(slave)) {
2437 /* spec doesn't return explicit support for
2438 * PIO0-2, so we fake it
2439 */
2440 u16 tmp_mode = slave->id[ATA_ID_PIO_MODES] & 0x03;
2441 tmp_mode <<= 3;
2442 tmp_mode |= 0x7;
2443 mask &= tmp_mode;
2444 }
2445 }
2446 else {
2447 mask = 0xffffffff; /* shut up compiler warning */
2448 BUG();
2449 }
2450
2451 return mask;
2452}
2453
2454/* find greatest bit */
2455static int fgb(u32 bitmap)
2456{
2457 unsigned int i;
2458 int x = -1;
2459
2460 for (i = 0; i < 32; i++)
2461 if (bitmap & (1 << i))
2462 x = i;
2463
2464 return x;
2465}
2466
2467/**
2468 * ata_choose_xfer_mode - attempt to find best transfer mode
2469 * @ap: Port for which an xfer mode will be selected
2470 * @xfer_mode_out: (output) SET FEATURES - XFER MODE code
2471 * @xfer_shift_out: (output) bit shift that selects this mode
2472 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04002473 * Based on host and device capabilities, determine the
2474 * maximum transfer mode that is amenable to all.
2475 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002477 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 *
2479 * RETURNS:
2480 * Zero on success, negative on error.
2481 */
2482
Jeff Garzik057ace52005-10-22 14:27:05 -04002483static int ata_choose_xfer_mode(const struct ata_port *ap,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 u8 *xfer_mode_out,
2485 unsigned int *xfer_shift_out)
2486{
2487 unsigned int mask, shift;
2488 int x, i;
2489
2490 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++) {
2491 shift = xfer_mode_classes[i].shift;
2492 mask = ata_get_mode_mask(ap, shift);
2493
2494 x = fgb(mask);
2495 if (x >= 0) {
2496 *xfer_mode_out = xfer_mode_classes[i].base + x;
2497 *xfer_shift_out = shift;
2498 return 0;
2499 }
2500 }
2501
2502 return -1;
2503}
2504
2505/**
2506 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2507 * @ap: Port associated with device @dev
2508 * @dev: Device to which command will be sent
2509 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002510 * Issue SET FEATURES - XFER MODE command to device @dev
2511 * on port @ap.
2512 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002514 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 */
2516
2517static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
2518{
Tejun Heoa0123702005-12-13 14:49:31 +09002519 struct ata_taskfile tf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520
2521 /* set up set-features taskfile */
2522 DPRINTK("set features - xfer mode\n");
2523
Tejun Heoa0123702005-12-13 14:49:31 +09002524 ata_tf_init(ap, &tf, dev->devno);
2525 tf.command = ATA_CMD_SET_FEATURES;
2526 tf.feature = SETFEATURES_XFER;
2527 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2528 tf.protocol = ATA_PROT_NODATA;
2529 tf.nsect = dev->xfer_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530
Tejun Heoa0123702005-12-13 14:49:31 +09002531 if (ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0)) {
2532 printk(KERN_ERR "ata%u: failed to set xfermode, disabled\n",
2533 ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 ata_port_disable(ap);
Tejun Heoa0123702005-12-13 14:49:31 +09002535 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536
2537 DPRINTK("EXIT\n");
2538}
2539
2540/**
Albert Lee8bf62ece2005-05-12 15:29:42 -04002541 * ata_dev_init_params - Issue INIT DEV PARAMS command
2542 * @ap: Port associated with device @dev
2543 * @dev: Device to which command will be sent
2544 *
2545 * LOCKING:
Tejun Heo6aff8f12006-02-15 18:24:09 +09002546 * Kernel thread context (may sleep)
2547 *
2548 * RETURNS:
2549 * 0 on success, AC_ERR_* mask otherwise.
Albert Lee8bf62ece2005-05-12 15:29:42 -04002550 */
2551
Tejun Heo6aff8f12006-02-15 18:24:09 +09002552static unsigned int ata_dev_init_params(struct ata_port *ap,
2553 struct ata_device *dev)
Albert Lee8bf62ece2005-05-12 15:29:42 -04002554{
Tejun Heoa0123702005-12-13 14:49:31 +09002555 struct ata_taskfile tf;
Tejun Heo6aff8f12006-02-15 18:24:09 +09002556 unsigned int err_mask;
Albert Lee8bf62ece2005-05-12 15:29:42 -04002557 u16 sectors = dev->id[6];
2558 u16 heads = dev->id[3];
2559
2560 /* Number of sectors per track 1-255. Number of heads 1-16 */
2561 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
Tejun Heo6aff8f12006-02-15 18:24:09 +09002562 return 0;
Albert Lee8bf62ece2005-05-12 15:29:42 -04002563
2564 /* set up init dev params taskfile */
2565 DPRINTK("init dev params \n");
2566
Tejun Heoa0123702005-12-13 14:49:31 +09002567 ata_tf_init(ap, &tf, dev->devno);
2568 tf.command = ATA_CMD_INIT_DEV_PARAMS;
2569 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2570 tf.protocol = ATA_PROT_NODATA;
2571 tf.nsect = sectors;
2572 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
Albert Lee8bf62ece2005-05-12 15:29:42 -04002573
Tejun Heo6aff8f12006-02-15 18:24:09 +09002574 err_mask = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
Albert Lee8bf62ece2005-05-12 15:29:42 -04002575
Tejun Heo6aff8f12006-02-15 18:24:09 +09002576 DPRINTK("EXIT, err_mask=%x\n", err_mask);
2577 return err_mask;
Albert Lee8bf62ece2005-05-12 15:29:42 -04002578}
2579
2580/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002581 * ata_sg_clean - Unmap DMA memory associated with command
2582 * @qc: Command containing DMA memory to be released
2583 *
2584 * Unmap all mapped DMA memory associated with this command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 *
2586 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002587 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588 */
2589
2590static void ata_sg_clean(struct ata_queued_cmd *qc)
2591{
2592 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002593 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002595 void *pad_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596
Tejun Heoa46314742006-02-11 19:11:13 +09002597 WARN_ON(!(qc->flags & ATA_QCFLAG_DMAMAP));
2598 WARN_ON(sg == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599
2600 if (qc->flags & ATA_QCFLAG_SINGLE)
Jeff Garzikf1318832006-02-20 16:55:56 -05002601 WARN_ON(qc->n_elem > 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05002603 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002605 /* if we padded the buffer out to 32-bit bound, and data
2606 * xfer direction is from-device, we must copy from the
2607 * pad buffer back into the supplied buffer
2608 */
2609 if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
2610 pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2611
2612 if (qc->flags & ATA_QCFLAG_SG) {
Jeff Garzike1410f22005-11-14 14:06:26 -05002613 if (qc->n_elem)
2614 dma_unmap_sg(ap->host_set->dev, sg, qc->n_elem, dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002615 /* restore last sg */
2616 sg[qc->orig_n_elem - 1].length += qc->pad_len;
2617 if (pad_buf) {
2618 struct scatterlist *psg = &qc->pad_sgent;
2619 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2620 memcpy(addr + psg->offset, pad_buf, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05002621 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002622 }
2623 } else {
Tejun Heo2e242fa2006-02-20 23:48:38 +09002624 if (qc->n_elem)
Jeff Garzike1410f22005-11-14 14:06:26 -05002625 dma_unmap_single(ap->host_set->dev,
2626 sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
2627 dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002628 /* restore sg */
2629 sg->length += qc->pad_len;
2630 if (pad_buf)
2631 memcpy(qc->buf_virt + sg->length - qc->pad_len,
2632 pad_buf, qc->pad_len);
2633 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634
2635 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002636 qc->__sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637}
2638
2639/**
2640 * ata_fill_sg - Fill PCI IDE PRD table
2641 * @qc: Metadata associated with taskfile to be transferred
2642 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002643 * Fill PCI IDE PRD (scatter-gather) table with segments
2644 * associated with the current disk command.
2645 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 * LOCKING:
Jeff Garzik780a87f2005-05-30 15:41:05 -04002647 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 *
2649 */
2650static void ata_fill_sg(struct ata_queued_cmd *qc)
2651{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002653 struct scatterlist *sg;
2654 unsigned int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655
Tejun Heoa46314742006-02-11 19:11:13 +09002656 WARN_ON(qc->__sg == NULL);
Jeff Garzikf1318832006-02-20 16:55:56 -05002657 WARN_ON(qc->n_elem == 0 && qc->pad_len == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658
2659 idx = 0;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002660 ata_for_each_sg(sg, qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 u32 addr, offset;
2662 u32 sg_len, len;
2663
2664 /* determine if physical DMA addr spans 64K boundary.
2665 * Note h/w doesn't support 64-bit, so we unconditionally
2666 * truncate dma_addr_t to u32.
2667 */
2668 addr = (u32) sg_dma_address(sg);
2669 sg_len = sg_dma_len(sg);
2670
2671 while (sg_len) {
2672 offset = addr & 0xffff;
2673 len = sg_len;
2674 if ((offset + sg_len) > 0x10000)
2675 len = 0x10000 - offset;
2676
2677 ap->prd[idx].addr = cpu_to_le32(addr);
2678 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
2679 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
2680
2681 idx++;
2682 sg_len -= len;
2683 addr += len;
2684 }
2685 }
2686
2687 if (idx)
2688 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2689}
2690/**
2691 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2692 * @qc: Metadata associated with taskfile to check
2693 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002694 * Allow low-level driver to filter ATA PACKET commands, returning
2695 * a status indicating whether or not it is OK to use DMA for the
2696 * supplied PACKET command.
2697 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002698 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002699 * spin_lock_irqsave(host_set lock)
2700 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 * RETURNS: 0 when ATAPI DMA can be used
2702 * nonzero otherwise
2703 */
2704int ata_check_atapi_dma(struct ata_queued_cmd *qc)
2705{
2706 struct ata_port *ap = qc->ap;
2707 int rc = 0; /* Assume ATAPI DMA is OK by default */
2708
2709 if (ap->ops->check_atapi_dma)
2710 rc = ap->ops->check_atapi_dma(qc);
2711
2712 return rc;
2713}
2714/**
2715 * ata_qc_prep - Prepare taskfile for submission
2716 * @qc: Metadata associated with taskfile to be prepared
2717 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002718 * Prepare ATA taskfile for submission.
2719 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 * LOCKING:
2721 * spin_lock_irqsave(host_set lock)
2722 */
2723void ata_qc_prep(struct ata_queued_cmd *qc)
2724{
2725 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2726 return;
2727
2728 ata_fill_sg(qc);
2729}
2730
Jeff Garzik0cba6322005-05-30 19:49:12 -04002731/**
2732 * ata_sg_init_one - Associate command with memory buffer
2733 * @qc: Command to be associated
2734 * @buf: Memory buffer
2735 * @buflen: Length of memory buffer, in bytes.
2736 *
2737 * Initialize the data-related elements of queued_cmd @qc
2738 * to point to a single memory buffer, @buf of byte length @buflen.
2739 *
2740 * LOCKING:
2741 * spin_lock_irqsave(host_set lock)
2742 */
2743
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
2745{
2746 struct scatterlist *sg;
2747
2748 qc->flags |= ATA_QCFLAG_SINGLE;
2749
2750 memset(&qc->sgent, 0, sizeof(qc->sgent));
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002751 qc->__sg = &qc->sgent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 qc->n_elem = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002753 qc->orig_n_elem = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754 qc->buf_virt = buf;
2755
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002756 sg = qc->__sg;
Jeff Garzikf0612bb2005-10-30 01:58:18 -05002757 sg_init_one(sg, buf, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758}
2759
Jeff Garzik0cba6322005-05-30 19:49:12 -04002760/**
2761 * ata_sg_init - Associate command with scatter-gather table.
2762 * @qc: Command to be associated
2763 * @sg: Scatter-gather table.
2764 * @n_elem: Number of elements in s/g table.
2765 *
2766 * Initialize the data-related elements of queued_cmd @qc
2767 * to point to a scatter-gather table @sg, containing @n_elem
2768 * elements.
2769 *
2770 * LOCKING:
2771 * spin_lock_irqsave(host_set lock)
2772 */
2773
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
2775 unsigned int n_elem)
2776{
2777 qc->flags |= ATA_QCFLAG_SG;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002778 qc->__sg = sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 qc->n_elem = n_elem;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002780 qc->orig_n_elem = n_elem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781}
2782
2783/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002784 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
2785 * @qc: Command with memory buffer to be mapped.
2786 *
2787 * DMA-map the memory buffer associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002788 *
2789 * LOCKING:
2790 * spin_lock_irqsave(host_set lock)
2791 *
2792 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002793 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 */
2795
2796static int ata_sg_setup_one(struct ata_queued_cmd *qc)
2797{
2798 struct ata_port *ap = qc->ap;
2799 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002800 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801 dma_addr_t dma_address;
Tejun Heo2e242fa2006-02-20 23:48:38 +09002802 int trim_sg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002804 /* we must lengthen transfers to end on a 32-bit boundary */
2805 qc->pad_len = sg->length & 3;
2806 if (qc->pad_len) {
2807 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2808 struct scatterlist *psg = &qc->pad_sgent;
2809
Tejun Heoa46314742006-02-11 19:11:13 +09002810 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002811
2812 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2813
2814 if (qc->tf.flags & ATA_TFLAG_WRITE)
2815 memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
2816 qc->pad_len);
2817
2818 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2819 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2820 /* trim sg */
2821 sg->length -= qc->pad_len;
Tejun Heo2e242fa2006-02-20 23:48:38 +09002822 if (sg->length == 0)
2823 trim_sg = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002824
2825 DPRINTK("padding done, sg->length=%u pad_len=%u\n",
2826 sg->length, qc->pad_len);
2827 }
2828
Tejun Heo2e242fa2006-02-20 23:48:38 +09002829 if (trim_sg) {
2830 qc->n_elem--;
Jeff Garzike1410f22005-11-14 14:06:26 -05002831 goto skip_map;
2832 }
2833
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 dma_address = dma_map_single(ap->host_set->dev, qc->buf_virt,
Albert Lee32529e02005-05-26 03:49:42 -04002835 sg->length, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05002836 if (dma_mapping_error(dma_address)) {
2837 /* restore sg */
2838 sg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05002840 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841
2842 sg_dma_address(sg) = dma_address;
Albert Lee32529e02005-05-26 03:49:42 -04002843 sg_dma_len(sg) = sg->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844
Tejun Heo2e242fa2006-02-20 23:48:38 +09002845skip_map:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
2847 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2848
2849 return 0;
2850}
2851
2852/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002853 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
2854 * @qc: Command with scatter-gather table to be mapped.
2855 *
2856 * DMA-map the scatter-gather table associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 *
2858 * LOCKING:
2859 * spin_lock_irqsave(host_set lock)
2860 *
2861 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002862 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 *
2864 */
2865
2866static int ata_sg_setup(struct ata_queued_cmd *qc)
2867{
2868 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002869 struct scatterlist *sg = qc->__sg;
2870 struct scatterlist *lsg = &sg[qc->n_elem - 1];
Jeff Garzike1410f22005-11-14 14:06:26 -05002871 int n_elem, pre_n_elem, dir, trim_sg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872
2873 VPRINTK("ENTER, ata%u\n", ap->id);
Tejun Heoa46314742006-02-11 19:11:13 +09002874 WARN_ON(!(qc->flags & ATA_QCFLAG_SG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002876 /* we must lengthen transfers to end on a 32-bit boundary */
2877 qc->pad_len = lsg->length & 3;
2878 if (qc->pad_len) {
2879 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2880 struct scatterlist *psg = &qc->pad_sgent;
2881 unsigned int offset;
2882
Tejun Heoa46314742006-02-11 19:11:13 +09002883 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002884
2885 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2886
2887 /*
2888 * psg->page/offset are used to copy to-be-written
2889 * data in this function or read data in ata_sg_clean.
2890 */
2891 offset = lsg->offset + lsg->length - qc->pad_len;
2892 psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
2893 psg->offset = offset_in_page(offset);
2894
2895 if (qc->tf.flags & ATA_TFLAG_WRITE) {
2896 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2897 memcpy(pad_buf, addr + psg->offset, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05002898 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002899 }
2900
2901 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2902 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2903 /* trim last sg */
2904 lsg->length -= qc->pad_len;
Jeff Garzike1410f22005-11-14 14:06:26 -05002905 if (lsg->length == 0)
2906 trim_sg = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002907
2908 DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
2909 qc->n_elem - 1, lsg->length, qc->pad_len);
2910 }
2911
Jeff Garzike1410f22005-11-14 14:06:26 -05002912 pre_n_elem = qc->n_elem;
2913 if (trim_sg && pre_n_elem)
2914 pre_n_elem--;
2915
2916 if (!pre_n_elem) {
2917 n_elem = 0;
2918 goto skip_map;
2919 }
2920
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 dir = qc->dma_dir;
Jeff Garzike1410f22005-11-14 14:06:26 -05002922 n_elem = dma_map_sg(ap->host_set->dev, sg, pre_n_elem, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05002923 if (n_elem < 1) {
2924 /* restore last sg */
2925 lsg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05002927 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928
2929 DPRINTK("%d sg elements mapped\n", n_elem);
2930
Jeff Garzike1410f22005-11-14 14:06:26 -05002931skip_map:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 qc->n_elem = n_elem;
2933
2934 return 0;
2935}
2936
2937/**
Tejun Heo40e8c822005-08-22 17:12:45 +09002938 * ata_poll_qc_complete - turn irq back on and finish qc
2939 * @qc: Command to complete
Randy Dunlap8e8b77d2005-11-01 21:29:27 -08002940 * @err_mask: ATA status register content
Tejun Heo40e8c822005-08-22 17:12:45 +09002941 *
2942 * LOCKING:
2943 * None. (grabs host lock)
2944 */
2945
Albert Leea22e2eb2005-12-05 15:38:02 +08002946void ata_poll_qc_complete(struct ata_queued_cmd *qc)
Tejun Heo40e8c822005-08-22 17:12:45 +09002947{
2948 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04002949 unsigned long flags;
Tejun Heo40e8c822005-08-22 17:12:45 +09002950
Jeff Garzikb8f61532005-08-25 22:01:20 -04002951 spin_lock_irqsave(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09002952 ata_irq_on(ap);
Albert Leea22e2eb2005-12-05 15:38:02 +08002953 ata_qc_complete(qc);
Jeff Garzikb8f61532005-08-25 22:01:20 -04002954 spin_unlock_irqrestore(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09002955}
2956
2957/**
Randy Dunlapc893a3a2006-01-28 13:15:32 -05002958 * ata_pio_poll - poll using PIO, depending on current state
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002959 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 *
2961 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002962 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 *
2964 * RETURNS:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002965 * timeout value to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 */
2967
2968static unsigned long ata_pio_poll(struct ata_port *ap)
2969{
Albert Leec14b8332005-12-05 15:36:08 +08002970 struct ata_queued_cmd *qc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 u8 status;
Albert Lee14be71f2005-09-27 17:36:35 +08002972 unsigned int poll_state = HSM_ST_UNKNOWN;
2973 unsigned int reg_state = HSM_ST_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974
Albert Leec14b8332005-12-05 15:36:08 +08002975 qc = ata_qc_from_tag(ap, ap->active_tag);
Tejun Heoa46314742006-02-11 19:11:13 +09002976 WARN_ON(qc == NULL);
Albert Leec14b8332005-12-05 15:36:08 +08002977
Albert Lee14be71f2005-09-27 17:36:35 +08002978 switch (ap->hsm_task_state) {
2979 case HSM_ST:
2980 case HSM_ST_POLL:
2981 poll_state = HSM_ST_POLL;
2982 reg_state = HSM_ST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 break;
Albert Lee14be71f2005-09-27 17:36:35 +08002984 case HSM_ST_LAST:
2985 case HSM_ST_LAST_POLL:
2986 poll_state = HSM_ST_LAST_POLL;
2987 reg_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 break;
2989 default:
2990 BUG();
2991 break;
2992 }
2993
2994 status = ata_chk_status(ap);
2995 if (status & ATA_BUSY) {
2996 if (time_after(jiffies, ap->pio_task_timeout)) {
Tejun Heo11a56d22006-01-23 13:09:36 +09002997 qc->err_mask |= AC_ERR_TIMEOUT;
Albert Lee7c398332005-11-09 13:03:30 +08002998 ap->hsm_task_state = HSM_ST_TMOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 return 0;
3000 }
Albert Lee14be71f2005-09-27 17:36:35 +08003001 ap->hsm_task_state = poll_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 return ATA_SHORT_PAUSE;
3003 }
3004
Albert Lee14be71f2005-09-27 17:36:35 +08003005 ap->hsm_task_state = reg_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006 return 0;
3007}
3008
3009/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003010 * ata_pio_complete - check if drive is busy or idle
3011 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 *
3013 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003014 * None. (executing in kernel thread context)
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003015 *
3016 * RETURNS:
Albert Leefbcdd802005-11-01 19:30:05 +08003017 * Zero if qc completed.
3018 * Non-zero if has next.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 */
3020
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003021static int ata_pio_complete (struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022{
3023 struct ata_queued_cmd *qc;
3024 u8 drv_stat;
3025
3026 /*
Alan Cox31433ea2005-08-26 15:56:47 +01003027 * This is purely heuristic. This is a fast path. Sometimes when
3028 * we enter, BSY will be cleared in a chk-status or two. If not,
3029 * the drive is probably seeking or something. Snooze for a couple
3030 * msecs, then chk-status again. If still busy, fall back to
Albert Lee07f6f7d2005-11-01 19:33:20 +08003031 * HSM_ST_LAST_POLL state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 */
Albert Leefe79e682005-12-06 11:34:59 +08003033 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
3034 if (drv_stat & ATA_BUSY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 msleep(2);
Albert Leefe79e682005-12-06 11:34:59 +08003036 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
3037 if (drv_stat & ATA_BUSY) {
Albert Lee14be71f2005-09-27 17:36:35 +08003038 ap->hsm_task_state = HSM_ST_LAST_POLL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
Albert Leefbcdd802005-11-01 19:30:05 +08003040 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 }
3042 }
3043
Albert Leec14b8332005-12-05 15:36:08 +08003044 qc = ata_qc_from_tag(ap, ap->active_tag);
Tejun Heoa46314742006-02-11 19:11:13 +09003045 WARN_ON(qc == NULL);
Albert Leec14b8332005-12-05 15:36:08 +08003046
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 drv_stat = ata_wait_idle(ap);
3048 if (!ata_ok(drv_stat)) {
Albert Lee1c848982005-12-05 15:40:15 +08003049 qc->err_mask |= __ac_err_mask(drv_stat);
Albert Lee14be71f2005-09-27 17:36:35 +08003050 ap->hsm_task_state = HSM_ST_ERR;
Albert Leefbcdd802005-11-01 19:30:05 +08003051 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 }
3053
Albert Lee14be71f2005-09-27 17:36:35 +08003054 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055
Tejun Heoa46314742006-02-11 19:11:13 +09003056 WARN_ON(qc->err_mask);
Albert Leea22e2eb2005-12-05 15:38:02 +08003057 ata_poll_qc_complete(qc);
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003058
3059 /* another command may start at this point */
3060
Albert Leefbcdd802005-11-01 19:30:05 +08003061 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062}
3063
Edward Falk0baab862005-06-02 18:17:13 -04003064
3065/**
Randy Dunlapc893a3a2006-01-28 13:15:32 -05003066 * swap_buf_le16 - swap halves of 16-bit words in place
Edward Falk0baab862005-06-02 18:17:13 -04003067 * @buf: Buffer to swap
3068 * @buf_words: Number of 16-bit words in buffer.
3069 *
3070 * Swap halves of 16-bit words if needed to convert from
3071 * little-endian byte order to native cpu byte order, or
3072 * vice-versa.
3073 *
3074 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003075 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04003076 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077void swap_buf_le16(u16 *buf, unsigned int buf_words)
3078{
3079#ifdef __BIG_ENDIAN
3080 unsigned int i;
3081
3082 for (i = 0; i < buf_words; i++)
3083 buf[i] = le16_to_cpu(buf[i]);
3084#endif /* __BIG_ENDIAN */
3085}
3086
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003087/**
3088 * ata_mmio_data_xfer - Transfer data by MMIO
3089 * @ap: port to read/write
3090 * @buf: data buffer
3091 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04003092 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003093 *
3094 * Transfer data from/to the device data register by MMIO.
3095 *
3096 * LOCKING:
3097 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003098 */
3099
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
3101 unsigned int buflen, int write_data)
3102{
3103 unsigned int i;
3104 unsigned int words = buflen >> 1;
3105 u16 *buf16 = (u16 *) buf;
3106 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
3107
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003108 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109 if (write_data) {
3110 for (i = 0; i < words; i++)
3111 writew(le16_to_cpu(buf16[i]), mmio);
3112 } else {
3113 for (i = 0; i < words; i++)
3114 buf16[i] = cpu_to_le16(readw(mmio));
3115 }
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003116
3117 /* Transfer trailing 1 byte, if any. */
3118 if (unlikely(buflen & 0x01)) {
3119 u16 align_buf[1] = { 0 };
3120 unsigned char *trailing_buf = buf + buflen - 1;
3121
3122 if (write_data) {
3123 memcpy(align_buf, trailing_buf, 1);
3124 writew(le16_to_cpu(align_buf[0]), mmio);
3125 } else {
3126 align_buf[0] = cpu_to_le16(readw(mmio));
3127 memcpy(trailing_buf, align_buf, 1);
3128 }
3129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130}
3131
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003132/**
3133 * ata_pio_data_xfer - Transfer data by PIO
3134 * @ap: port to read/write
3135 * @buf: data buffer
3136 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04003137 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003138 *
3139 * Transfer data from/to the device data register by PIO.
3140 *
3141 * LOCKING:
3142 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003143 */
3144
Linus Torvalds1da177e2005-04-16 15:20:36 -07003145static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
3146 unsigned int buflen, int write_data)
3147{
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003148 unsigned int words = buflen >> 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003150 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 if (write_data)
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003152 outsw(ap->ioaddr.data_addr, buf, words);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153 else
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003154 insw(ap->ioaddr.data_addr, buf, words);
3155
3156 /* Transfer trailing 1 byte, if any. */
3157 if (unlikely(buflen & 0x01)) {
3158 u16 align_buf[1] = { 0 };
3159 unsigned char *trailing_buf = buf + buflen - 1;
3160
3161 if (write_data) {
3162 memcpy(align_buf, trailing_buf, 1);
3163 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
3164 } else {
3165 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
3166 memcpy(trailing_buf, align_buf, 1);
3167 }
3168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169}
3170
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003171/**
3172 * ata_data_xfer - Transfer data from/to the data register.
3173 * @ap: port to read/write
3174 * @buf: data buffer
3175 * @buflen: buffer length
3176 * @do_write: read/write
3177 *
3178 * Transfer data from/to the device data register.
3179 *
3180 * LOCKING:
3181 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003182 */
3183
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
3185 unsigned int buflen, int do_write)
3186{
Alan Coxa1bd9e62006-01-17 20:53:50 +00003187 /* Make the crap hardware pay the costs not the good stuff */
3188 if (unlikely(ap->flags & ATA_FLAG_IRQ_MASK)) {
3189 unsigned long flags;
3190 local_irq_save(flags);
3191 if (ap->flags & ATA_FLAG_MMIO)
3192 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3193 else
3194 ata_pio_data_xfer(ap, buf, buflen, do_write);
3195 local_irq_restore(flags);
3196 } else {
3197 if (ap->flags & ATA_FLAG_MMIO)
3198 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3199 else
3200 ata_pio_data_xfer(ap, buf, buflen, do_write);
3201 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202}
3203
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003204/**
3205 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
3206 * @qc: Command on going
3207 *
3208 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
3209 *
3210 * LOCKING:
3211 * Inherited from caller.
3212 */
3213
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214static void ata_pio_sector(struct ata_queued_cmd *qc)
3215{
3216 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003217 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 struct ata_port *ap = qc->ap;
3219 struct page *page;
3220 unsigned int offset;
3221 unsigned char *buf;
3222
3223 if (qc->cursect == (qc->nsect - 1))
Albert Lee14be71f2005-09-27 17:36:35 +08003224 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225
3226 page = sg[qc->cursg].page;
3227 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
3228
3229 /* get the current page and offset */
3230 page = nth_page(page, (offset >> PAGE_SHIFT));
3231 offset %= PAGE_SIZE;
3232
Albert Lee7282aa42005-10-09 09:46:07 -04003233 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3234
Albert Lee91b8b312005-10-09 09:48:44 -04003235 if (PageHighMem(page)) {
3236 unsigned long flags;
Albert Lee7282aa42005-10-09 09:46:07 -04003237
Albert Lee91b8b312005-10-09 09:48:44 -04003238 local_irq_save(flags);
3239 buf = kmap_atomic(page, KM_IRQ0);
Albert Lee083958d2005-10-09 09:47:31 -04003240
Albert Lee91b8b312005-10-09 09:48:44 -04003241 /* do the actual data transfer */
3242 ata_data_xfer(ap, buf + offset, ATA_SECT_SIZE, do_write);
3243
3244 kunmap_atomic(buf, KM_IRQ0);
3245 local_irq_restore(flags);
3246 } else {
3247 buf = page_address(page);
3248 ata_data_xfer(ap, buf + offset, ATA_SECT_SIZE, do_write);
3249 }
Albert Lee7282aa42005-10-09 09:46:07 -04003250
Linus Torvalds1da177e2005-04-16 15:20:36 -07003251 qc->cursect++;
3252 qc->cursg_ofs++;
3253
Albert Lee32529e02005-05-26 03:49:42 -04003254 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 qc->cursg++;
3256 qc->cursg_ofs = 0;
3257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258}
3259
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003260/**
Albert Lee07f6f7d2005-11-01 19:33:20 +08003261 * ata_pio_sectors - Transfer one or many 512-byte sectors.
3262 * @qc: Command on going
3263 *
3264 * Transfer one or many ATA_SECT_SIZE of data from/to the
3265 * ATA device for the DRQ request.
3266 *
3267 * LOCKING:
3268 * Inherited from caller.
3269 */
3270
3271static void ata_pio_sectors(struct ata_queued_cmd *qc)
3272{
3273 if (is_multi_taskfile(&qc->tf)) {
3274 /* READ/WRITE MULTIPLE */
3275 unsigned int nsect;
3276
Jeff Garzik587005d2006-02-11 18:17:32 -05003277 WARN_ON(qc->dev->multi_count == 0);
Albert Lee07f6f7d2005-11-01 19:33:20 +08003278
3279 nsect = min(qc->nsect - qc->cursect, qc->dev->multi_count);
3280 while (nsect--)
3281 ata_pio_sector(qc);
3282 } else
3283 ata_pio_sector(qc);
3284}
3285
3286/**
Albert Leec71c1852005-10-04 06:03:45 -04003287 * atapi_send_cdb - Write CDB bytes to hardware
3288 * @ap: Port to which ATAPI device is attached.
3289 * @qc: Taskfile currently active
3290 *
3291 * When device has indicated its readiness to accept
3292 * a CDB, this function is called. Send the CDB.
3293 *
3294 * LOCKING:
3295 * caller.
3296 */
3297
3298static void atapi_send_cdb(struct ata_port *ap, struct ata_queued_cmd *qc)
3299{
3300 /* send SCSI cdb */
3301 DPRINTK("send cdb\n");
Jeff Garzikdb024d52006-02-13 00:23:57 -05003302 WARN_ON(qc->dev->cdb_len < 12);
Albert Leec71c1852005-10-04 06:03:45 -04003303
Jeff Garzikdb024d52006-02-13 00:23:57 -05003304 ata_data_xfer(ap, qc->cdb, qc->dev->cdb_len, 1);
Albert Leec71c1852005-10-04 06:03:45 -04003305 ata_altstatus(ap); /* flush */
3306
3307 switch (qc->tf.protocol) {
3308 case ATA_PROT_ATAPI:
3309 ap->hsm_task_state = HSM_ST;
3310 break;
3311 case ATA_PROT_ATAPI_NODATA:
3312 ap->hsm_task_state = HSM_ST_LAST;
3313 break;
3314 case ATA_PROT_ATAPI_DMA:
3315 ap->hsm_task_state = HSM_ST_LAST;
3316 /* initiate bmdma */
3317 ap->ops->bmdma_start(qc);
3318 break;
3319 }
3320}
3321
3322/**
Albert Leee27486d2005-11-01 19:24:49 +08003323 * ata_pio_first_block - Write first data block to hardware
3324 * @ap: Port to which ATA/ATAPI device is attached.
Albert Leec71c1852005-10-04 06:03:45 -04003325 *
3326 * When device has indicated its readiness to accept
3327 * the data, this function sends out the CDB or
3328 * the first data block by PIO.
3329 * After this,
3330 * - If polling, ata_pio_task() handles the rest.
3331 * - Otherwise, interrupt handler takes over.
3332 *
3333 * LOCKING:
3334 * Kernel thread context (may sleep)
Albert Leefbcdd802005-11-01 19:30:05 +08003335 *
3336 * RETURNS:
3337 * Zero if irq handler takes over
3338 * Non-zero if has next (polling).
Albert Leec71c1852005-10-04 06:03:45 -04003339 */
3340
Albert Leefbcdd802005-11-01 19:30:05 +08003341static int ata_pio_first_block(struct ata_port *ap)
Albert Leec71c1852005-10-04 06:03:45 -04003342{
Albert Leec71c1852005-10-04 06:03:45 -04003343 struct ata_queued_cmd *qc;
3344 u8 status;
3345 unsigned long flags;
Albert Leefbcdd802005-11-01 19:30:05 +08003346 int has_next;
Albert Leec71c1852005-10-04 06:03:45 -04003347
3348 qc = ata_qc_from_tag(ap, ap->active_tag);
Jeff Garzik587005d2006-02-11 18:17:32 -05003349 WARN_ON(qc == NULL);
3350 WARN_ON((qc->flags & ATA_QCFLAG_ACTIVE) == 0);
Albert Leec71c1852005-10-04 06:03:45 -04003351
Albert Leefbcdd802005-11-01 19:30:05 +08003352 /* if polling, we will stay in the work queue after sending the data.
3353 * otherwise, interrupt handler takes over after sending the data.
3354 */
3355 has_next = (qc->tf.flags & ATA_TFLAG_POLLING);
3356
Albert Leec71c1852005-10-04 06:03:45 -04003357 /* sleep-wait for BSY to clear */
3358 DPRINTK("busy wait\n");
Albert Leefbcdd802005-11-01 19:30:05 +08003359 if (ata_busy_sleep(ap, ATA_TMOUT_DATAOUT_QUICK, ATA_TMOUT_DATAOUT)) {
Albert Lee555a8962006-02-08 16:50:29 +08003360 qc->err_mask |= AC_ERR_TIMEOUT;
Albert Leefbcdd802005-11-01 19:30:05 +08003361 ap->hsm_task_state = HSM_ST_TMOUT;
Albert Leec71c1852005-10-04 06:03:45 -04003362 goto err_out;
Albert Leefbcdd802005-11-01 19:30:05 +08003363 }
Albert Leec71c1852005-10-04 06:03:45 -04003364
3365 /* make sure DRQ is set */
3366 status = ata_chk_status(ap);
Albert Leefbcdd802005-11-01 19:30:05 +08003367 if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ) {
3368 /* device status error */
Albert Lee555a8962006-02-08 16:50:29 +08003369 qc->err_mask |= AC_ERR_HSM;
Albert Leefbcdd802005-11-01 19:30:05 +08003370 ap->hsm_task_state = HSM_ST_ERR;
Albert Leec71c1852005-10-04 06:03:45 -04003371 goto err_out;
Albert Leefbcdd802005-11-01 19:30:05 +08003372 }
Albert Leec71c1852005-10-04 06:03:45 -04003373
3374 /* Send the CDB (atapi) or the first data block (ata pio out).
3375 * During the state transition, interrupt handler shouldn't
3376 * be invoked before the data transfer is complete and
3377 * hsm_task_state is changed. Hence, the following locking.
3378 */
3379 spin_lock_irqsave(&ap->host_set->lock, flags);
3380
3381 if (qc->tf.protocol == ATA_PROT_PIO) {
3382 /* PIO data out protocol.
3383 * send first data block.
3384 */
3385
Albert Lee07f6f7d2005-11-01 19:33:20 +08003386 /* ata_pio_sectors() might change the state to HSM_ST_LAST.
3387 * so, the state is changed here before ata_pio_sectors().
Albert Leec71c1852005-10-04 06:03:45 -04003388 */
3389 ap->hsm_task_state = HSM_ST;
Albert Lee07f6f7d2005-11-01 19:33:20 +08003390 ata_pio_sectors(qc);
Albert Leec71c1852005-10-04 06:03:45 -04003391 ata_altstatus(ap); /* flush */
3392 } else
3393 /* send CDB */
3394 atapi_send_cdb(ap, qc);
3395
Albert Leefbcdd802005-11-01 19:30:05 +08003396 spin_unlock_irqrestore(&ap->host_set->lock, flags);
3397
Albert Leec71c1852005-10-04 06:03:45 -04003398 /* if polling, ata_pio_task() handles the rest.
3399 * otherwise, interrupt handler takes over from here.
3400 */
Albert Leefbcdd802005-11-01 19:30:05 +08003401 return has_next;
Albert Leec71c1852005-10-04 06:03:45 -04003402
3403err_out:
Albert Leefbcdd802005-11-01 19:30:05 +08003404 return 1; /* has next */
Albert Leec71c1852005-10-04 06:03:45 -04003405}
3406
3407/**
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003408 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
3409 * @qc: Command on going
3410 * @bytes: number of bytes
3411 *
3412 * Transfer Transfer data from/to the ATAPI device.
3413 *
3414 * LOCKING:
3415 * Inherited from caller.
3416 *
3417 */
3418
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
3420{
3421 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003422 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 struct ata_port *ap = qc->ap;
3424 struct page *page;
3425 unsigned char *buf;
3426 unsigned int offset, count;
3427
Albert Lee563a6e12005-08-12 14:17:50 +08003428 if (qc->curbytes + bytes >= qc->nbytes)
Albert Lee14be71f2005-09-27 17:36:35 +08003429 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430
3431next_sg:
Albert Lee563a6e12005-08-12 14:17:50 +08003432 if (unlikely(qc->cursg >= qc->n_elem)) {
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003433 /*
Albert Lee563a6e12005-08-12 14:17:50 +08003434 * The end of qc->sg is reached and the device expects
3435 * more data to transfer. In order not to overrun qc->sg
3436 * and fulfill length specified in the byte count register,
3437 * - for read case, discard trailing data from the device
3438 * - for write case, padding zero data to the device
3439 */
3440 u16 pad_buf[1] = { 0 };
3441 unsigned int words = bytes >> 1;
3442 unsigned int i;
3443
3444 if (words) /* warning if bytes > 1 */
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003445 printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
Albert Lee563a6e12005-08-12 14:17:50 +08003446 ap->id, bytes);
3447
3448 for (i = 0; i < words; i++)
3449 ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
3450
Albert Lee14be71f2005-09-27 17:36:35 +08003451 ap->hsm_task_state = HSM_ST_LAST;
Albert Lee563a6e12005-08-12 14:17:50 +08003452 return;
3453 }
3454
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003455 sg = &qc->__sg[qc->cursg];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457 page = sg->page;
3458 offset = sg->offset + qc->cursg_ofs;
3459
3460 /* get the current page and offset */
3461 page = nth_page(page, (offset >> PAGE_SHIFT));
3462 offset %= PAGE_SIZE;
3463
Albert Lee6952df02005-06-06 15:56:03 +08003464 /* don't overrun current sg */
Albert Lee32529e02005-05-26 03:49:42 -04003465 count = min(sg->length - qc->cursg_ofs, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466
3467 /* don't cross page boundaries */
3468 count = min(count, (unsigned int)PAGE_SIZE - offset);
3469
Albert Lee7282aa42005-10-09 09:46:07 -04003470 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3471
Albert Lee91b8b312005-10-09 09:48:44 -04003472 if (PageHighMem(page)) {
3473 unsigned long flags;
Albert Lee7282aa42005-10-09 09:46:07 -04003474
Albert Lee91b8b312005-10-09 09:48:44 -04003475 local_irq_save(flags);
3476 buf = kmap_atomic(page, KM_IRQ0);
Albert Lee083958d2005-10-09 09:47:31 -04003477
Albert Lee91b8b312005-10-09 09:48:44 -04003478 /* do the actual data transfer */
3479 ata_data_xfer(ap, buf + offset, count, do_write);
3480
3481 kunmap_atomic(buf, KM_IRQ0);
3482 local_irq_restore(flags);
3483 } else {
3484 buf = page_address(page);
3485 ata_data_xfer(ap, buf + offset, count, do_write);
3486 }
Albert Lee7282aa42005-10-09 09:46:07 -04003487
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488 bytes -= count;
3489 qc->curbytes += count;
3490 qc->cursg_ofs += count;
3491
Albert Lee32529e02005-05-26 03:49:42 -04003492 if (qc->cursg_ofs == sg->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 qc->cursg++;
3494 qc->cursg_ofs = 0;
3495 }
3496
Albert Lee563a6e12005-08-12 14:17:50 +08003497 if (bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 goto next_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499}
3500
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003501/**
3502 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
3503 * @qc: Command on going
3504 *
3505 * Transfer Transfer data from/to the ATAPI device.
3506 *
3507 * LOCKING:
3508 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003509 */
3510
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511static void atapi_pio_bytes(struct ata_queued_cmd *qc)
3512{
3513 struct ata_port *ap = qc->ap;
3514 struct ata_device *dev = qc->dev;
3515 unsigned int ireason, bc_lo, bc_hi, bytes;
3516 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
3517
3518 ap->ops->tf_read(ap, &qc->tf);
3519 ireason = qc->tf.nsect;
3520 bc_lo = qc->tf.lbam;
3521 bc_hi = qc->tf.lbah;
3522 bytes = (bc_hi << 8) | bc_lo;
3523
3524 /* shall be cleared to zero, indicating xfer of data */
3525 if (ireason & (1 << 0))
3526 goto err_out;
3527
3528 /* make sure transfer direction matches expected */
3529 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
3530 if (do_write != i_write)
3531 goto err_out;
3532
Albert Lee312f7da2005-09-27 17:38:03 +08003533 VPRINTK("ata%u: xfering %d bytes\n", ap->id, bytes);
3534
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535 __atapi_pio_bytes(qc, bytes);
3536
3537 return;
3538
3539err_out:
3540 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
3541 ap->id, dev->devno);
Tejun Heo11a56d22006-01-23 13:09:36 +09003542 qc->err_mask |= AC_ERR_HSM;
Albert Lee14be71f2005-09-27 17:36:35 +08003543 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544}
3545
3546/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003547 * ata_pio_block - start PIO on a block
3548 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07003549 *
3550 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003551 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 */
3553
3554static void ata_pio_block(struct ata_port *ap)
3555{
3556 struct ata_queued_cmd *qc;
3557 u8 status;
3558
3559 /*
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003560 * This is purely heuristic. This is a fast path.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561 * Sometimes when we enter, BSY will be cleared in
3562 * a chk-status or two. If not, the drive is probably seeking
3563 * or something. Snooze for a couple msecs, then
3564 * chk-status again. If still busy, fall back to
Albert Lee14be71f2005-09-27 17:36:35 +08003565 * HSM_ST_POLL state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566 */
3567 status = ata_busy_wait(ap, ATA_BUSY, 5);
3568 if (status & ATA_BUSY) {
3569 msleep(2);
3570 status = ata_busy_wait(ap, ATA_BUSY, 10);
3571 if (status & ATA_BUSY) {
Albert Lee14be71f2005-09-27 17:36:35 +08003572 ap->hsm_task_state = HSM_ST_POLL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
3574 return;
3575 }
3576 }
3577
3578 qc = ata_qc_from_tag(ap, ap->active_tag);
Tejun Heoa46314742006-02-11 19:11:13 +09003579 WARN_ON(qc == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580
Albert Leefe79e682005-12-06 11:34:59 +08003581 /* check error */
3582 if (status & (ATA_ERR | ATA_DF)) {
3583 qc->err_mask |= AC_ERR_DEV;
3584 ap->hsm_task_state = HSM_ST_ERR;
3585 return;
3586 }
3587
3588 /* transfer data if any */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589 if (is_atapi_taskfile(&qc->tf)) {
Albert Leefe79e682005-12-06 11:34:59 +08003590 /* DRQ=0 means no more data to transfer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591 if ((status & ATA_DRQ) == 0) {
Albert Lee14be71f2005-09-27 17:36:35 +08003592 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593 return;
3594 }
3595
3596 atapi_pio_bytes(qc);
3597 } else {
3598 /* handle BSY=0, DRQ=0 as error */
3599 if ((status & ATA_DRQ) == 0) {
Tejun Heo11a56d22006-01-23 13:09:36 +09003600 qc->err_mask |= AC_ERR_HSM;
Albert Lee14be71f2005-09-27 17:36:35 +08003601 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602 return;
3603 }
3604
Albert Lee07f6f7d2005-11-01 19:33:20 +08003605 ata_pio_sectors(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606 }
Albert Lee467b16d2005-11-01 19:19:01 +08003607
3608 ata_altstatus(ap); /* flush */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609}
3610
3611static void ata_pio_error(struct ata_port *ap)
3612{
3613 struct ata_queued_cmd *qc;
Jeff Garzika7dac442005-10-30 04:44:42 -05003614
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615 qc = ata_qc_from_tag(ap, ap->active_tag);
Tejun Heoa46314742006-02-11 19:11:13 +09003616 WARN_ON(qc == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617
Albert Lee000080c2005-12-26 16:48:00 +08003618 if (qc->tf.command != ATA_CMD_PACKET)
3619 printk(KERN_WARNING "ata%u: PIO error\n", ap->id);
3620
Albert Lee1c848982005-12-05 15:40:15 +08003621 /* make sure qc->err_mask is available to
3622 * know what's wrong and recover
3623 */
Tejun Heoa46314742006-02-11 19:11:13 +09003624 WARN_ON(qc->err_mask == 0);
Albert Lee1c848982005-12-05 15:40:15 +08003625
Albert Lee14be71f2005-09-27 17:36:35 +08003626 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627
Albert Leea22e2eb2005-12-05 15:38:02 +08003628 ata_poll_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629}
3630
3631static void ata_pio_task(void *_data)
3632{
3633 struct ata_port *ap = _data;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003634 unsigned long timeout;
Albert Leefbcdd802005-11-01 19:30:05 +08003635 int has_next;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003636
3637fsm_start:
3638 timeout = 0;
Albert Leefbcdd802005-11-01 19:30:05 +08003639 has_next = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640
Albert Lee14be71f2005-09-27 17:36:35 +08003641 switch (ap->hsm_task_state) {
Albert Leee27486d2005-11-01 19:24:49 +08003642 case HSM_ST_FIRST:
Albert Leefbcdd802005-11-01 19:30:05 +08003643 has_next = ata_pio_first_block(ap);
3644 break;
Albert Leee27486d2005-11-01 19:24:49 +08003645
Albert Lee14be71f2005-09-27 17:36:35 +08003646 case HSM_ST:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647 ata_pio_block(ap);
3648 break;
3649
Albert Lee14be71f2005-09-27 17:36:35 +08003650 case HSM_ST_LAST:
Albert Leefbcdd802005-11-01 19:30:05 +08003651 has_next = ata_pio_complete(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652 break;
3653
Albert Lee14be71f2005-09-27 17:36:35 +08003654 case HSM_ST_POLL:
3655 case HSM_ST_LAST_POLL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 timeout = ata_pio_poll(ap);
3657 break;
3658
Albert Lee14be71f2005-09-27 17:36:35 +08003659 case HSM_ST_TMOUT:
3660 case HSM_ST_ERR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003661 ata_pio_error(ap);
3662 return;
Albert Lee467b16d2005-11-01 19:19:01 +08003663
3664 default:
3665 BUG();
3666 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667 }
3668
3669 if (timeout)
Tejun Heo95064372006-01-23 13:09:37 +09003670 ata_queue_delayed_pio_task(ap, timeout);
Albert Leefbcdd802005-11-01 19:30:05 +08003671 else if (has_next)
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003672 goto fsm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003673}
3674
Linus Torvalds1da177e2005-04-16 15:20:36 -07003675/**
3676 * ata_qc_timeout - Handle timeout of queued command
3677 * @qc: Command that timed out
3678 *
3679 * Some part of the kernel (currently, only the SCSI layer)
3680 * has noticed that the active command on port @ap has not
3681 * completed after a specified length of time. Handle this
3682 * condition by disabling DMA (if necessary) and completing
3683 * transactions, with error if necessary.
3684 *
3685 * This also handles the case of the "lost interrupt", where
3686 * for some reason (possibly hardware bug, possibly driver bug)
3687 * an interrupt was not delivered to the driver, even though the
3688 * transaction completed successfully.
3689 *
3690 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003691 * Inherited from SCSI layer (none, can sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003692 */
3693
3694static void ata_qc_timeout(struct ata_queued_cmd *qc)
3695{
3696 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003697 struct ata_host_set *host_set = ap->host_set;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003698 u8 host_stat = 0, drv_stat;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003699 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003700
3701 DPRINTK("ENTER\n");
3702
Tejun Heoc18d06f2006-02-02 00:56:10 +09003703 ata_flush_pio_tasks(ap);
3704 ap->hsm_task_state = HSM_ST_IDLE;
3705
Jeff Garzikb8f61532005-08-25 22:01:20 -04003706 spin_lock_irqsave(&host_set->lock, flags);
3707
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708 switch (qc->tf.protocol) {
3709
3710 case ATA_PROT_DMA:
3711 case ATA_PROT_ATAPI_DMA:
3712 host_stat = ap->ops->bmdma_status(ap);
3713
3714 /* before we do anything else, clear DMA-Start bit */
Alan Coxb73fc892005-08-26 16:03:19 +01003715 ap->ops->bmdma_stop(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716
3717 /* fall through */
3718
3719 default:
3720 ata_altstatus(ap);
3721 drv_stat = ata_chk_status(ap);
3722
3723 /* ack bmdma irq events */
3724 ap->ops->irq_clear(ap);
3725
3726 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
3727 ap->id, qc->tf.command, drv_stat, host_stat);
3728
Albert Lee312f7da2005-09-27 17:38:03 +08003729 ap->hsm_task_state = HSM_ST_IDLE;
3730
Linus Torvalds1da177e2005-04-16 15:20:36 -07003731 /* complete taskfile transaction */
Albert Lee555a8962006-02-08 16:50:29 +08003732 qc->err_mask |= AC_ERR_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733 break;
3734 }
Jeff Garzikb8f61532005-08-25 22:01:20 -04003735
3736 spin_unlock_irqrestore(&host_set->lock, flags);
3737
Tejun Heoa72ec4c2006-01-23 13:09:37 +09003738 ata_eh_qc_complete(qc);
3739
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740 DPRINTK("EXIT\n");
3741}
3742
3743/**
3744 * ata_eng_timeout - Handle timeout of queued command
3745 * @ap: Port on which timed-out command is active
3746 *
3747 * Some part of the kernel (currently, only the SCSI layer)
3748 * has noticed that the active command on port @ap has not
3749 * completed after a specified length of time. Handle this
3750 * condition by disabling DMA (if necessary) and completing
3751 * transactions, with error if necessary.
3752 *
3753 * This also handles the case of the "lost interrupt", where
3754 * for some reason (possibly hardware bug, possibly driver bug)
3755 * an interrupt was not delivered to the driver, even though the
3756 * transaction completed successfully.
3757 *
3758 * LOCKING:
3759 * Inherited from SCSI layer (none, can sleep)
3760 */
3761
3762void ata_eng_timeout(struct ata_port *ap)
3763{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764 DPRINTK("ENTER\n");
3765
Tejun Heof6379022006-02-10 15:10:48 +09003766 ata_qc_timeout(ata_qc_from_tag(ap, ap->active_tag));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768 DPRINTK("EXIT\n");
3769}
3770
3771/**
3772 * ata_qc_new - Request an available ATA command, for queueing
3773 * @ap: Port associated with device @dev
3774 * @dev: Device from whom we request an available command structure
3775 *
3776 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003777 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778 */
3779
3780static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
3781{
3782 struct ata_queued_cmd *qc = NULL;
3783 unsigned int i;
3784
3785 for (i = 0; i < ATA_MAX_QUEUE; i++)
3786 if (!test_and_set_bit(i, &ap->qactive)) {
3787 qc = ata_qc_from_tag(ap, i);
3788 break;
3789 }
3790
3791 if (qc)
3792 qc->tag = i;
3793
3794 return qc;
3795}
3796
3797/**
3798 * ata_qc_new_init - Request an available ATA command, and initialize it
3799 * @ap: Port associated with device @dev
3800 * @dev: Device from whom we request an available command structure
3801 *
3802 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003803 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804 */
3805
3806struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
3807 struct ata_device *dev)
3808{
3809 struct ata_queued_cmd *qc;
3810
3811 qc = ata_qc_new(ap);
3812 if (qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813 qc->scsicmd = NULL;
3814 qc->ap = ap;
3815 qc->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05003817 ata_qc_reinit(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818 }
3819
3820 return qc;
3821}
3822
Linus Torvalds1da177e2005-04-16 15:20:36 -07003823/**
3824 * ata_qc_free - free unused ata_queued_cmd
3825 * @qc: Command to complete
3826 *
3827 * Designed to free unused ata_queued_cmd object
3828 * in case something prevents using it.
3829 *
3830 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003831 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003832 */
3833void ata_qc_free(struct ata_queued_cmd *qc)
3834{
Tejun Heo4ba946e2006-01-23 13:09:36 +09003835 struct ata_port *ap = qc->ap;
3836 unsigned int tag;
3837
Tejun Heoa46314742006-02-11 19:11:13 +09003838 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839
Tejun Heo4ba946e2006-01-23 13:09:36 +09003840 qc->flags = 0;
3841 tag = qc->tag;
3842 if (likely(ata_tag_valid(tag))) {
3843 if (tag == ap->active_tag)
3844 ap->active_tag = ATA_TAG_POISON;
3845 qc->tag = ATA_TAG_POISON;
3846 clear_bit(tag, &ap->qactive);
3847 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003848}
3849
Tejun Heo76014422006-02-11 15:13:49 +09003850void __ata_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851{
Tejun Heoa46314742006-02-11 19:11:13 +09003852 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
3853 WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003854
3855 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
3856 ata_sg_clean(qc);
3857
Albert Lee3f3791d2005-08-16 14:25:38 +08003858 /* atapi: mark qc as inactive to prevent the interrupt handler
3859 * from completing the command twice later, before the error handler
3860 * is called. (when rc != 0 and atapi request sense is needed)
3861 */
3862 qc->flags &= ~ATA_QCFLAG_ACTIVE;
3863
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864 /* call completion callback */
Tejun Heo77853bf2006-01-23 13:09:36 +09003865 qc->complete_fn(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866}
3867
3868static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
3869{
3870 struct ata_port *ap = qc->ap;
3871
3872 switch (qc->tf.protocol) {
3873 case ATA_PROT_DMA:
3874 case ATA_PROT_ATAPI_DMA:
3875 return 1;
3876
3877 case ATA_PROT_ATAPI:
3878 case ATA_PROT_PIO:
3879 case ATA_PROT_PIO_MULT:
3880 if (ap->flags & ATA_FLAG_PIO_DMA)
3881 return 1;
3882
3883 /* fall through */
3884
3885 default:
3886 return 0;
3887 }
3888
3889 /* never reached */
3890}
3891
3892/**
3893 * ata_qc_issue - issue taskfile to device
3894 * @qc: command to issue to device
3895 *
3896 * Prepare an ATA command to submission to device.
3897 * This includes mapping the data into a DMA-able
3898 * area, filling in the S/G table, and finally
3899 * writing the taskfile to hardware, starting the command.
3900 *
3901 * LOCKING:
3902 * spin_lock_irqsave(host_set lock)
3903 *
3904 * RETURNS:
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003905 * Zero on success, AC_ERR_* mask on failure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906 */
3907
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003908unsigned int ata_qc_issue(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909{
3910 struct ata_port *ap = qc->ap;
3911
3912 if (ata_should_dma_map(qc)) {
3913 if (qc->flags & ATA_QCFLAG_SG) {
3914 if (ata_sg_setup(qc))
Tejun Heo8e436af2006-01-23 13:09:36 +09003915 goto sg_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
3917 if (ata_sg_setup_one(qc))
Tejun Heo8e436af2006-01-23 13:09:36 +09003918 goto sg_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003919 }
3920 } else {
3921 qc->flags &= ~ATA_QCFLAG_DMAMAP;
3922 }
3923
3924 ap->ops->qc_prep(qc);
3925
3926 qc->ap->active_tag = qc->tag;
3927 qc->flags |= ATA_QCFLAG_ACTIVE;
3928
3929 return ap->ops->qc_issue(qc);
3930
Tejun Heo8e436af2006-01-23 13:09:36 +09003931sg_err:
3932 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003933 return AC_ERR_SYSTEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934}
3935
Edward Falk0baab862005-06-02 18:17:13 -04003936
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937/**
3938 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
3939 * @qc: command to issue to device
3940 *
3941 * Using various libata functions and hooks, this function
3942 * starts an ATA command. ATA commands are grouped into
3943 * classes called "protocols", and issuing each type of protocol
3944 * is slightly different.
3945 *
Edward Falk0baab862005-06-02 18:17:13 -04003946 * May be used as the qc_issue() entry in ata_port_operations.
3947 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003948 * LOCKING:
3949 * spin_lock_irqsave(host_set lock)
3950 *
3951 * RETURNS:
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003952 * Zero on success, AC_ERR_* mask on failure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003953 */
3954
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003955unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956{
3957 struct ata_port *ap = qc->ap;
3958
Albert Leee50362e2005-09-27 17:39:50 +08003959 /* Use polling pio if the LLD doesn't handle
3960 * interrupt driven pio and atapi CDB interrupt.
3961 */
3962 if (ap->flags & ATA_FLAG_PIO_POLLING) {
3963 switch (qc->tf.protocol) {
3964 case ATA_PROT_PIO:
3965 case ATA_PROT_ATAPI:
3966 case ATA_PROT_ATAPI_NODATA:
3967 qc->tf.flags |= ATA_TFLAG_POLLING;
3968 break;
3969 case ATA_PROT_ATAPI_DMA:
3970 if (qc->dev->flags & ATA_DFLAG_CDB_INTR)
3971 BUG();
3972 break;
3973 default:
3974 break;
3975 }
3976 }
3977
Albert Lee312f7da2005-09-27 17:38:03 +08003978 /* select the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979 ata_dev_select(ap, qc->dev->devno, 1, 0);
3980
Albert Lee312f7da2005-09-27 17:38:03 +08003981 /* start the command */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003982 switch (qc->tf.protocol) {
3983 case ATA_PROT_NODATA:
Albert Lee312f7da2005-09-27 17:38:03 +08003984 if (qc->tf.flags & ATA_TFLAG_POLLING)
3985 ata_qc_set_polling(qc);
3986
Jeff Garzike5338252005-10-30 21:37:17 -05003987 ata_tf_to_host(ap, &qc->tf);
Albert Lee312f7da2005-09-27 17:38:03 +08003988 ap->hsm_task_state = HSM_ST_LAST;
3989
3990 if (qc->tf.flags & ATA_TFLAG_POLLING)
Albert Lee20ea0792006-02-08 16:48:49 +08003991 ata_queue_pio_task(ap);
Albert Lee312f7da2005-09-27 17:38:03 +08003992
Linus Torvalds1da177e2005-04-16 15:20:36 -07003993 break;
3994
3995 case ATA_PROT_DMA:
Jeff Garzik587005d2006-02-11 18:17:32 -05003996 WARN_ON(qc->tf.flags & ATA_TFLAG_POLLING);
Albert Lee312f7da2005-09-27 17:38:03 +08003997
Linus Torvalds1da177e2005-04-16 15:20:36 -07003998 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3999 ap->ops->bmdma_setup(qc); /* set up bmdma */
4000 ap->ops->bmdma_start(qc); /* initiate bmdma */
Albert Lee312f7da2005-09-27 17:38:03 +08004001 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002 break;
4003
Albert Lee312f7da2005-09-27 17:38:03 +08004004 case ATA_PROT_PIO:
4005 if (qc->tf.flags & ATA_TFLAG_POLLING)
4006 ata_qc_set_polling(qc);
4007
Jeff Garzike5338252005-10-30 21:37:17 -05004008 ata_tf_to_host(ap, &qc->tf);
Albert Lee312f7da2005-09-27 17:38:03 +08004009
Albert Lee54f00382005-09-30 19:14:19 +08004010 if (qc->tf.flags & ATA_TFLAG_WRITE) {
4011 /* PIO data out protocol */
4012 ap->hsm_task_state = HSM_ST_FIRST;
Jeff Garzikf6ef65e2006-01-27 02:45:00 -05004013 ata_queue_pio_task(ap);
Albert Lee54f00382005-09-30 19:14:19 +08004014
4015 /* always send first data block using
Albert Leee27486d2005-11-01 19:24:49 +08004016 * the ata_pio_task() codepath.
Albert Lee54f00382005-09-30 19:14:19 +08004017 */
Albert Lee312f7da2005-09-27 17:38:03 +08004018 } else {
Albert Lee54f00382005-09-30 19:14:19 +08004019 /* PIO data in protocol */
4020 ap->hsm_task_state = HSM_ST;
Albert Lee312f7da2005-09-27 17:38:03 +08004021
Albert Lee54f00382005-09-30 19:14:19 +08004022 if (qc->tf.flags & ATA_TFLAG_POLLING)
Jeff Garzikf6ef65e2006-01-27 02:45:00 -05004023 ata_queue_pio_task(ap);
Albert Lee312f7da2005-09-27 17:38:03 +08004024
Albert Lee54f00382005-09-30 19:14:19 +08004025 /* if polling, ata_pio_task() handles the rest.
4026 * otherwise, interrupt handler takes over from here.
4027 */
Albert Lee312f7da2005-09-27 17:38:03 +08004028 }
4029
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030 break;
4031
4032 case ATA_PROT_ATAPI:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033 case ATA_PROT_ATAPI_NODATA:
Albert Lee312f7da2005-09-27 17:38:03 +08004034 if (qc->tf.flags & ATA_TFLAG_POLLING)
4035 ata_qc_set_polling(qc);
4036
Jeff Garzike5338252005-10-30 21:37:17 -05004037 ata_tf_to_host(ap, &qc->tf);
Jeff Garzikf6ef65e2006-01-27 02:45:00 -05004038
Albert Lee312f7da2005-09-27 17:38:03 +08004039 ap->hsm_task_state = HSM_ST_FIRST;
4040
4041 /* send cdb by polling if no cdb interrupt */
4042 if ((!(qc->dev->flags & ATA_DFLAG_CDB_INTR)) ||
4043 (qc->tf.flags & ATA_TFLAG_POLLING))
Albert Lee20ea0792006-02-08 16:48:49 +08004044 ata_queue_pio_task(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004045 break;
4046
4047 case ATA_PROT_ATAPI_DMA:
Jeff Garzik587005d2006-02-11 18:17:32 -05004048 WARN_ON(qc->tf.flags & ATA_TFLAG_POLLING);
Albert Lee312f7da2005-09-27 17:38:03 +08004049
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
4051 ap->ops->bmdma_setup(qc); /* set up bmdma */
Albert Lee312f7da2005-09-27 17:38:03 +08004052 ap->hsm_task_state = HSM_ST_FIRST;
4053
4054 /* send cdb by polling if no cdb interrupt */
4055 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
Albert Lee20ea0792006-02-08 16:48:49 +08004056 ata_queue_pio_task(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057 break;
4058
4059 default:
4060 WARN_ON(1);
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09004061 return AC_ERR_SYSTEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062 }
4063
4064 return 0;
4065}
4066
4067/**
Edward Falk0baab862005-06-02 18:17:13 -04004068 * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069 * @qc: Info associated with this ATA transaction.
4070 *
4071 * LOCKING:
4072 * spin_lock_irqsave(host_set lock)
4073 */
4074
4075static void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc)
4076{
4077 struct ata_port *ap = qc->ap;
4078 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
4079 u8 dmactl;
4080 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4081
4082 /* load PRD table addr. */
4083 mb(); /* make sure PRD table writes are visible to controller */
4084 writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
4085
4086 /* specify data direction, triple-check start bit is clear */
4087 dmactl = readb(mmio + ATA_DMA_CMD);
4088 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
4089 if (!rw)
4090 dmactl |= ATA_DMA_WR;
4091 writeb(dmactl, mmio + ATA_DMA_CMD);
4092
4093 /* issue r/w command */
4094 ap->ops->exec_command(ap, &qc->tf);
4095}
4096
4097/**
Alan Coxb73fc892005-08-26 16:03:19 +01004098 * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099 * @qc: Info associated with this ATA transaction.
4100 *
4101 * LOCKING:
4102 * spin_lock_irqsave(host_set lock)
4103 */
4104
4105static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
4106{
4107 struct ata_port *ap = qc->ap;
4108 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4109 u8 dmactl;
4110
4111 /* start host DMA transaction */
4112 dmactl = readb(mmio + ATA_DMA_CMD);
4113 writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
4114
4115 /* Strictly, one may wish to issue a readb() here, to
4116 * flush the mmio write. However, control also passes
4117 * to the hardware at this point, and it will interrupt
4118 * us when we are to resume control. So, in effect,
4119 * we don't care when the mmio write flushes.
4120 * Further, a read of the DMA status register _immediately_
4121 * following the write may not be what certain flaky hardware
4122 * is expected, so I think it is best to not add a readb()
4123 * without first all the MMIO ATA cards/mobos.
4124 * Or maybe I'm just being paranoid.
4125 */
4126}
4127
4128/**
4129 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
4130 * @qc: Info associated with this ATA transaction.
4131 *
4132 * LOCKING:
4133 * spin_lock_irqsave(host_set lock)
4134 */
4135
4136static void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
4137{
4138 struct ata_port *ap = qc->ap;
4139 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
4140 u8 dmactl;
4141
4142 /* load PRD table addr. */
4143 outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
4144
4145 /* specify data direction, triple-check start bit is clear */
4146 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4147 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
4148 if (!rw)
4149 dmactl |= ATA_DMA_WR;
4150 outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4151
4152 /* issue r/w command */
4153 ap->ops->exec_command(ap, &qc->tf);
4154}
4155
4156/**
4157 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
4158 * @qc: Info associated with this ATA transaction.
4159 *
4160 * LOCKING:
4161 * spin_lock_irqsave(host_set lock)
4162 */
4163
4164static void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
4165{
4166 struct ata_port *ap = qc->ap;
4167 u8 dmactl;
4168
4169 /* start host DMA transaction */
4170 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4171 outb(dmactl | ATA_DMA_START,
4172 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4173}
4174
Edward Falk0baab862005-06-02 18:17:13 -04004175
4176/**
4177 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
4178 * @qc: Info associated with this ATA transaction.
4179 *
4180 * Writes the ATA_DMA_START flag to the DMA command register.
4181 *
4182 * May be used as the bmdma_start() entry in ata_port_operations.
4183 *
4184 * LOCKING:
4185 * spin_lock_irqsave(host_set lock)
4186 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187void ata_bmdma_start(struct ata_queued_cmd *qc)
4188{
4189 if (qc->ap->flags & ATA_FLAG_MMIO)
4190 ata_bmdma_start_mmio(qc);
4191 else
4192 ata_bmdma_start_pio(qc);
4193}
4194
Edward Falk0baab862005-06-02 18:17:13 -04004195
4196/**
4197 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
4198 * @qc: Info associated with this ATA transaction.
4199 *
4200 * Writes address of PRD table to device's PRD Table Address
4201 * register, sets the DMA control register, and calls
4202 * ops->exec_command() to start the transfer.
4203 *
4204 * May be used as the bmdma_setup() entry in ata_port_operations.
4205 *
4206 * LOCKING:
4207 * spin_lock_irqsave(host_set lock)
4208 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209void ata_bmdma_setup(struct ata_queued_cmd *qc)
4210{
4211 if (qc->ap->flags & ATA_FLAG_MMIO)
4212 ata_bmdma_setup_mmio(qc);
4213 else
4214 ata_bmdma_setup_pio(qc);
4215}
4216
Edward Falk0baab862005-06-02 18:17:13 -04004217
4218/**
4219 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
Jeff Garzikdecc6d02005-06-02 18:42:33 -04004220 * @ap: Port associated with this ATA transaction.
Edward Falk0baab862005-06-02 18:17:13 -04004221 *
4222 * Clear interrupt and error flags in DMA status register.
4223 *
4224 * May be used as the irq_clear() entry in ata_port_operations.
4225 *
4226 * LOCKING:
4227 * spin_lock_irqsave(host_set lock)
4228 */
4229
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230void ata_bmdma_irq_clear(struct ata_port *ap)
4231{
4232 if (ap->flags & ATA_FLAG_MMIO) {
4233 void __iomem *mmio = ((void __iomem *) ap->ioaddr.bmdma_addr) + ATA_DMA_STATUS;
4234 writeb(readb(mmio), mmio);
4235 } else {
4236 unsigned long addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
4237 outb(inb(addr), addr);
4238 }
4239
4240}
4241
Edward Falk0baab862005-06-02 18:17:13 -04004242
4243/**
4244 * ata_bmdma_status - Read PCI IDE BMDMA status
Jeff Garzikdecc6d02005-06-02 18:42:33 -04004245 * @ap: Port associated with this ATA transaction.
Edward Falk0baab862005-06-02 18:17:13 -04004246 *
4247 * Read and return BMDMA status register.
4248 *
4249 * May be used as the bmdma_status() entry in ata_port_operations.
4250 *
4251 * LOCKING:
4252 * spin_lock_irqsave(host_set lock)
4253 */
4254
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255u8 ata_bmdma_status(struct ata_port *ap)
4256{
4257 u8 host_stat;
4258 if (ap->flags & ATA_FLAG_MMIO) {
4259 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4260 host_stat = readb(mmio + ATA_DMA_STATUS);
4261 } else
Albert Leeee500aa2005-09-27 17:34:38 +08004262 host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263 return host_stat;
4264}
4265
Edward Falk0baab862005-06-02 18:17:13 -04004266
4267/**
4268 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
Alan Coxb73fc892005-08-26 16:03:19 +01004269 * @qc: Command we are ending DMA for
Edward Falk0baab862005-06-02 18:17:13 -04004270 *
4271 * Clears the ATA_DMA_START flag in the dma control register
4272 *
4273 * May be used as the bmdma_stop() entry in ata_port_operations.
4274 *
4275 * LOCKING:
4276 * spin_lock_irqsave(host_set lock)
4277 */
4278
Alan Coxb73fc892005-08-26 16:03:19 +01004279void ata_bmdma_stop(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280{
Alan Coxb73fc892005-08-26 16:03:19 +01004281 struct ata_port *ap = qc->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282 if (ap->flags & ATA_FLAG_MMIO) {
4283 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4284
4285 /* clear start/stop bit */
4286 writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
4287 mmio + ATA_DMA_CMD);
4288 } else {
4289 /* clear start/stop bit */
4290 outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
4291 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4292 }
4293
4294 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
4295 ata_altstatus(ap); /* dummy read */
4296}
4297
4298/**
4299 * ata_host_intr - Handle host interrupt for given (port, task)
4300 * @ap: Port on which interrupt arrived (possibly...)
4301 * @qc: Taskfile currently active in engine
4302 *
4303 * Handle host interrupt for given queued command. Currently,
4304 * only DMA interrupts are handled. All other commands are
4305 * handled via polling with interrupts disabled (nIEN bit).
4306 *
4307 * LOCKING:
4308 * spin_lock_irqsave(host_set lock)
4309 *
4310 * RETURNS:
4311 * One if interrupt was handled, zero if not (shared irq).
4312 */
4313
4314inline unsigned int ata_host_intr (struct ata_port *ap,
4315 struct ata_queued_cmd *qc)
4316{
Albert Lee312f7da2005-09-27 17:38:03 +08004317 u8 status, host_stat = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318
Albert Lee312f7da2005-09-27 17:38:03 +08004319 VPRINTK("ata%u: protocol %d task_state %d\n",
4320 ap->id, qc->tf.protocol, ap->hsm_task_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321
Albert Lee312f7da2005-09-27 17:38:03 +08004322 /* Check whether we are expecting interrupt in this state */
4323 switch (ap->hsm_task_state) {
4324 case HSM_ST_FIRST:
4325 /* Check the ATA_DFLAG_CDB_INTR flag is enough here.
4326 * The flag was turned on only for atapi devices.
4327 * No need to check is_atapi_taskfile(&qc->tf) again.
4328 */
4329 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330 goto idle_irq;
Albert Lee312f7da2005-09-27 17:38:03 +08004331 break;
4332 case HSM_ST_LAST:
4333 if (qc->tf.protocol == ATA_PROT_DMA ||
4334 qc->tf.protocol == ATA_PROT_ATAPI_DMA) {
4335 /* check status of DMA engine */
4336 host_stat = ap->ops->bmdma_status(ap);
4337 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338
Albert Lee312f7da2005-09-27 17:38:03 +08004339 /* if it's not our irq... */
4340 if (!(host_stat & ATA_DMA_INTR))
4341 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342
Albert Lee312f7da2005-09-27 17:38:03 +08004343 /* before we do anything else, clear DMA-Start bit */
4344 ap->ops->bmdma_stop(qc);
Albert Leea4f16612005-12-26 16:40:53 +08004345
4346 if (unlikely(host_stat & ATA_DMA_ERR)) {
4347 /* error when transfering data to/from memory */
4348 qc->err_mask |= AC_ERR_HOST_BUS;
4349 ap->hsm_task_state = HSM_ST_ERR;
4350 }
Albert Lee312f7da2005-09-27 17:38:03 +08004351 }
4352 break;
4353 case HSM_ST:
4354 break;
4355 default:
4356 goto idle_irq;
4357 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358
Albert Lee312f7da2005-09-27 17:38:03 +08004359 /* check altstatus */
4360 status = ata_altstatus(ap);
4361 if (status & ATA_BUSY)
4362 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363
Albert Lee312f7da2005-09-27 17:38:03 +08004364 /* check main status, clearing INTRQ */
4365 status = ata_chk_status(ap);
4366 if (unlikely(status & ATA_BUSY))
4367 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368
Albert Lee312f7da2005-09-27 17:38:03 +08004369 DPRINTK("ata%u: protocol %d task_state %d (dev_stat 0x%X)\n",
4370 ap->id, qc->tf.protocol, ap->hsm_task_state, status);
4371
4372 /* ack bmdma irq events */
4373 ap->ops->irq_clear(ap);
4374
4375 /* check error */
Albert Leea4f16612005-12-26 16:40:53 +08004376 if (unlikely(status & (ATA_ERR | ATA_DF))) {
4377 qc->err_mask |= AC_ERR_DEV;
Albert Lee312f7da2005-09-27 17:38:03 +08004378 ap->hsm_task_state = HSM_ST_ERR;
Albert Leea4f16612005-12-26 16:40:53 +08004379 }
Albert Lee312f7da2005-09-27 17:38:03 +08004380
4381fsm_start:
4382 switch (ap->hsm_task_state) {
4383 case HSM_ST_FIRST:
4384 /* Some pre-ATAPI-4 devices assert INTRQ
4385 * at this state when ready to receive CDB.
4386 */
4387
4388 /* check device status */
4389 if (unlikely((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ)) {
4390 /* Wrong status. Let EH handle this */
Albert Lee555a8962006-02-08 16:50:29 +08004391 qc->err_mask |= AC_ERR_HSM;
Albert Lee312f7da2005-09-27 17:38:03 +08004392 ap->hsm_task_state = HSM_ST_ERR;
4393 goto fsm_start;
4394 }
4395
4396 atapi_send_cdb(ap, qc);
4397
4398 break;
4399
4400 case HSM_ST:
4401 /* complete command or read/write the data register */
4402 if (qc->tf.protocol == ATA_PROT_ATAPI) {
4403 /* ATAPI PIO protocol */
4404 if ((status & ATA_DRQ) == 0) {
4405 /* no more data to transfer */
4406 ap->hsm_task_state = HSM_ST_LAST;
4407 goto fsm_start;
4408 }
4409
4410 atapi_pio_bytes(qc);
4411
4412 if (unlikely(ap->hsm_task_state == HSM_ST_ERR))
4413 /* bad ireason reported by device */
4414 goto fsm_start;
4415
4416 } else {
4417 /* ATA PIO protocol */
4418 if (unlikely((status & ATA_DRQ) == 0)) {
4419 /* handle BSY=0, DRQ=0 as error */
Albert Lee555a8962006-02-08 16:50:29 +08004420 qc->err_mask |= AC_ERR_HSM;
Albert Lee312f7da2005-09-27 17:38:03 +08004421 ap->hsm_task_state = HSM_ST_ERR;
4422 goto fsm_start;
4423 }
4424
Albert Lee07f6f7d2005-11-01 19:33:20 +08004425 ata_pio_sectors(qc);
Albert Lee312f7da2005-09-27 17:38:03 +08004426
4427 if (ap->hsm_task_state == HSM_ST_LAST &&
4428 (!(qc->tf.flags & ATA_TFLAG_WRITE))) {
4429 /* all data read */
4430 ata_altstatus(ap);
4431 status = ata_chk_status(ap);
4432 goto fsm_start;
4433 }
4434 }
4435
4436 ata_altstatus(ap); /* flush */
4437 break;
4438
4439 case HSM_ST_LAST:
4440 if (unlikely(status & ATA_DRQ)) {
4441 /* handle DRQ=1 as error */
Albert Lee555a8962006-02-08 16:50:29 +08004442 qc->err_mask |= AC_ERR_HSM;
Albert Lee312f7da2005-09-27 17:38:03 +08004443 ap->hsm_task_state = HSM_ST_ERR;
4444 goto fsm_start;
4445 }
4446
4447 /* no more data to transfer */
4448 DPRINTK("ata%u: command complete, drv_stat 0x%x\n",
4449 ap->id, status);
4450
4451 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452
4453 /* complete taskfile transaction */
Albert Leea22e2eb2005-12-05 15:38:02 +08004454 qc->err_mask |= ac_err_mask(status);
4455 ata_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004456 break;
4457
Albert Lee312f7da2005-09-27 17:38:03 +08004458 case HSM_ST_ERR:
Albert Lee000080c2005-12-26 16:48:00 +08004459 if (qc->tf.command != ATA_CMD_PACKET)
4460 printk(KERN_ERR "ata%u: command error, drv_stat 0x%x host_stat 0x%x\n",
4461 ap->id, status, host_stat);
Albert Lee312f7da2005-09-27 17:38:03 +08004462
Albert Leea4f16612005-12-26 16:40:53 +08004463 /* make sure qc->err_mask is available to
4464 * know what's wrong and recover
4465 */
Jeff Garzik587005d2006-02-11 18:17:32 -05004466 WARN_ON(qc->err_mask == 0);
Albert Leea4f16612005-12-26 16:40:53 +08004467
Albert Lee312f7da2005-09-27 17:38:03 +08004468 ap->hsm_task_state = HSM_ST_IDLE;
Jeff Garzik278efe92005-12-06 05:01:27 -05004469 ata_qc_complete(qc);
Albert Lee312f7da2005-09-27 17:38:03 +08004470 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471 default:
4472 goto idle_irq;
4473 }
4474
4475 return 1; /* irq handled */
4476
4477idle_irq:
4478 ap->stats.idle_irq++;
4479
4480#ifdef ATA_IRQ_TRAP
4481 if ((ap->stats.idle_irq % 1000) == 0) {
4482 handled = 1;
4483 ata_irq_ack(ap, 0); /* debug trap */
4484 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
4485 }
4486#endif
4487 return 0; /* irq not handled */
4488}
4489
4490/**
4491 * ata_interrupt - Default ATA host interrupt handler
Jeff Garzik0cba6322005-05-30 19:49:12 -04004492 * @irq: irq line (unused)
4493 * @dev_instance: pointer to our ata_host_set information structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494 * @regs: unused
4495 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004496 * Default interrupt handler for PCI IDE devices. Calls
4497 * ata_host_intr() for each port that is not disabled.
4498 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004500 * Obtains host_set lock during operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501 *
4502 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004503 * IRQ_NONE or IRQ_HANDLED.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504 */
4505
4506irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
4507{
4508 struct ata_host_set *host_set = dev_instance;
4509 unsigned int i;
4510 unsigned int handled = 0;
4511 unsigned long flags;
4512
4513 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
4514 spin_lock_irqsave(&host_set->lock, flags);
4515
4516 for (i = 0; i < host_set->n_ports; i++) {
4517 struct ata_port *ap;
4518
4519 ap = host_set->ports[i];
Tejun Heoc1389502005-08-22 14:59:24 +09004520 if (ap &&
Albert Lee312f7da2005-09-27 17:38:03 +08004521 !(ap->flags & ATA_FLAG_PORT_DISABLED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522 struct ata_queued_cmd *qc;
4523
4524 qc = ata_qc_from_tag(ap, ap->active_tag);
Albert Lee312f7da2005-09-27 17:38:03 +08004525 if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING)) &&
Albert Lee21b1ed72005-04-29 17:34:59 +08004526 (qc->flags & ATA_QCFLAG_ACTIVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527 handled |= ata_host_intr(ap, qc);
4528 }
4529 }
4530
4531 spin_unlock_irqrestore(&host_set->lock, flags);
4532
4533 return IRQ_RETVAL(handled);
4534}
4535
Jens Axboe9b847542006-01-06 09:28:07 +01004536/*
4537 * Execute a 'simple' command, that only consists of the opcode 'cmd' itself,
4538 * without filling any other registers
4539 */
4540static int ata_do_simple_cmd(struct ata_port *ap, struct ata_device *dev,
4541 u8 cmd)
4542{
4543 struct ata_taskfile tf;
4544 int err;
4545
4546 ata_tf_init(ap, &tf, dev->devno);
4547
4548 tf.command = cmd;
4549 tf.flags |= ATA_TFLAG_DEVICE;
4550 tf.protocol = ATA_PROT_NODATA;
4551
4552 err = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
4553 if (err)
4554 printk(KERN_ERR "%s: ata command failed: %d\n",
4555 __FUNCTION__, err);
4556
4557 return err;
4558}
4559
4560static int ata_flush_cache(struct ata_port *ap, struct ata_device *dev)
4561{
4562 u8 cmd;
4563
4564 if (!ata_try_flush_cache(dev))
4565 return 0;
4566
4567 if (ata_id_has_flush_ext(dev->id))
4568 cmd = ATA_CMD_FLUSH_EXT;
4569 else
4570 cmd = ATA_CMD_FLUSH;
4571
4572 return ata_do_simple_cmd(ap, dev, cmd);
4573}
4574
4575static int ata_standby_drive(struct ata_port *ap, struct ata_device *dev)
4576{
4577 return ata_do_simple_cmd(ap, dev, ATA_CMD_STANDBYNOW1);
4578}
4579
4580static int ata_start_drive(struct ata_port *ap, struct ata_device *dev)
4581{
4582 return ata_do_simple_cmd(ap, dev, ATA_CMD_IDLEIMMEDIATE);
4583}
4584
4585/**
4586 * ata_device_resume - wakeup a previously suspended devices
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004587 * @ap: port the device is connected to
4588 * @dev: the device to resume
Jens Axboe9b847542006-01-06 09:28:07 +01004589 *
4590 * Kick the drive back into action, by sending it an idle immediate
4591 * command and making sure its transfer mode matches between drive
4592 * and host.
4593 *
4594 */
4595int ata_device_resume(struct ata_port *ap, struct ata_device *dev)
4596{
4597 if (ap->flags & ATA_FLAG_SUSPENDED) {
4598 ap->flags &= ~ATA_FLAG_SUSPENDED;
4599 ata_set_mode(ap);
4600 }
4601 if (!ata_dev_present(dev))
4602 return 0;
4603 if (dev->class == ATA_DEV_ATA)
4604 ata_start_drive(ap, dev);
4605
4606 return 0;
4607}
4608
4609/**
4610 * ata_device_suspend - prepare a device for suspend
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004611 * @ap: port the device is connected to
4612 * @dev: the device to suspend
Jens Axboe9b847542006-01-06 09:28:07 +01004613 *
4614 * Flush the cache on the drive, if appropriate, then issue a
4615 * standbynow command.
Jens Axboe9b847542006-01-06 09:28:07 +01004616 */
4617int ata_device_suspend(struct ata_port *ap, struct ata_device *dev)
4618{
4619 if (!ata_dev_present(dev))
4620 return 0;
4621 if (dev->class == ATA_DEV_ATA)
4622 ata_flush_cache(ap, dev);
4623
4624 ata_standby_drive(ap, dev);
4625 ap->flags |= ATA_FLAG_SUSPENDED;
4626 return 0;
4627}
4628
Albert Lee332b5a52006-02-08 16:51:34 +08004629/**
4630 * ata_port_start - Set port up for dma.
4631 * @ap: Port to initialize
4632 *
4633 * Called just after data structures for each port are
4634 * initialized. Allocates space for PRD table.
4635 *
4636 * May be used as the port_start() entry in ata_port_operations.
4637 *
4638 * LOCKING:
4639 * Inherited from caller.
4640 */
4641
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642int ata_port_start (struct ata_port *ap)
4643{
4644 struct device *dev = ap->host_set->dev;
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004645 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646
4647 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
4648 if (!ap->prd)
4649 return -ENOMEM;
4650
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004651 rc = ata_pad_alloc(ap, dev);
4652 if (rc) {
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004653 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004654 return rc;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004655 }
4656
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
4658
4659 return 0;
4660}
4661
Edward Falk0baab862005-06-02 18:17:13 -04004662
4663/**
4664 * ata_port_stop - Undo ata_port_start()
4665 * @ap: Port to shut down
4666 *
4667 * Frees the PRD table.
4668 *
4669 * May be used as the port_stop() entry in ata_port_operations.
4670 *
4671 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004672 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04004673 */
4674
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675void ata_port_stop (struct ata_port *ap)
4676{
4677 struct device *dev = ap->host_set->dev;
4678
4679 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004680 ata_pad_free(ap, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681}
4682
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04004683void ata_host_stop (struct ata_host_set *host_set)
4684{
4685 if (host_set->mmio_base)
4686 iounmap(host_set->mmio_base);
4687}
4688
4689
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690/**
4691 * ata_host_remove - Unregister SCSI host structure with upper layers
4692 * @ap: Port to unregister
4693 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
4694 *
4695 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004696 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697 */
4698
4699static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
4700{
4701 struct Scsi_Host *sh = ap->host;
4702
4703 DPRINTK("ENTER\n");
4704
4705 if (do_unregister)
4706 scsi_remove_host(sh);
4707
4708 ap->ops->port_stop(ap);
4709}
4710
4711/**
4712 * ata_host_init - Initialize an ata_port structure
4713 * @ap: Structure to initialize
4714 * @host: associated SCSI mid-layer structure
4715 * @host_set: Collection of hosts to which @ap belongs
4716 * @ent: Probe information provided by low-level driver
4717 * @port_no: Port number associated with this ata_port
4718 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004719 * Initialize a new ata_port structure, and its associated
4720 * scsi_host.
4721 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004723 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724 */
4725
4726static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
4727 struct ata_host_set *host_set,
Jeff Garzik057ace52005-10-22 14:27:05 -04004728 const struct ata_probe_ent *ent, unsigned int port_no)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729{
4730 unsigned int i;
4731
4732 host->max_id = 16;
4733 host->max_lun = 1;
4734 host->max_channel = 1;
4735 host->unique_id = ata_unique_id++;
4736 host->max_cmd_len = 12;
Christoph Hellwig12413192005-06-11 01:05:01 +02004737
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738 ap->flags = ATA_FLAG_PORT_DISABLED;
4739 ap->id = host->unique_id;
4740 ap->host = host;
4741 ap->ctl = ATA_DEVCTL_OBS;
4742 ap->host_set = host_set;
4743 ap->port_no = port_no;
4744 ap->hard_port_no =
4745 ent->legacy_mode ? ent->hard_port_no : port_no;
4746 ap->pio_mask = ent->pio_mask;
4747 ap->mwdma_mask = ent->mwdma_mask;
4748 ap->udma_mask = ent->udma_mask;
4749 ap->flags |= ent->host_flags;
4750 ap->ops = ent->port_ops;
4751 ap->cbl = ATA_CBL_NONE;
4752 ap->active_tag = ATA_TAG_POISON;
4753 ap->last_ctl = 0xFF;
4754
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755 INIT_WORK(&ap->pio_task, ata_pio_task, ap);
Tejun Heoa72ec4c2006-01-23 13:09:37 +09004756 INIT_LIST_HEAD(&ap->eh_done_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757
4758 for (i = 0; i < ATA_MAX_DEVICES; i++)
4759 ap->device[i].devno = i;
4760
4761#ifdef ATA_IRQ_TRAP
4762 ap->stats.unhandled_irq = 1;
4763 ap->stats.idle_irq = 1;
4764#endif
4765
4766 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
4767}
4768
4769/**
4770 * ata_host_add - Attach low-level ATA driver to system
4771 * @ent: Information provided by low-level driver
4772 * @host_set: Collections of ports to which we add
4773 * @port_no: Port number associated with this host
4774 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004775 * Attach low-level ATA driver to system.
4776 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004778 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779 *
4780 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004781 * New ata_port on success, for NULL on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782 */
4783
Jeff Garzik057ace52005-10-22 14:27:05 -04004784static struct ata_port * ata_host_add(const struct ata_probe_ent *ent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785 struct ata_host_set *host_set,
4786 unsigned int port_no)
4787{
4788 struct Scsi_Host *host;
4789 struct ata_port *ap;
4790 int rc;
4791
4792 DPRINTK("ENTER\n");
4793 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
4794 if (!host)
4795 return NULL;
4796
4797 ap = (struct ata_port *) &host->hostdata[0];
4798
4799 ata_host_init(ap, host, host_set, ent, port_no);
4800
4801 rc = ap->ops->port_start(ap);
4802 if (rc)
4803 goto err_out;
4804
4805 return ap;
4806
4807err_out:
4808 scsi_host_put(host);
4809 return NULL;
4810}
4811
4812/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004813 * ata_device_add - Register hardware device with ATA and SCSI layers
4814 * @ent: Probe information describing hardware device to be registered
4815 *
4816 * This function processes the information provided in the probe
4817 * information struct @ent, allocates the necessary ATA and SCSI
4818 * host information structures, initializes them, and registers
4819 * everything with requisite kernel subsystems.
4820 *
4821 * This function requests irqs, probes the ATA bus, and probes
4822 * the SCSI bus.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823 *
4824 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004825 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826 *
4827 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004828 * Number of ports registered. Zero on error (no ports registered).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829 */
4830
Jeff Garzik057ace52005-10-22 14:27:05 -04004831int ata_device_add(const struct ata_probe_ent *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832{
4833 unsigned int count = 0, i;
4834 struct device *dev = ent->dev;
4835 struct ata_host_set *host_set;
4836
4837 DPRINTK("ENTER\n");
4838 /* alloc a container for our list of ATA ports (buses) */
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004839 host_set = kzalloc(sizeof(struct ata_host_set) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
4841 if (!host_set)
4842 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843 spin_lock_init(&host_set->lock);
4844
4845 host_set->dev = dev;
4846 host_set->n_ports = ent->n_ports;
4847 host_set->irq = ent->irq;
4848 host_set->mmio_base = ent->mmio_base;
4849 host_set->private_data = ent->private_data;
4850 host_set->ops = ent->port_ops;
4851
4852 /* register each port bound to this device */
4853 for (i = 0; i < ent->n_ports; i++) {
4854 struct ata_port *ap;
4855 unsigned long xfer_mode_mask;
4856
4857 ap = ata_host_add(ent, host_set, i);
4858 if (!ap)
4859 goto err_out;
4860
4861 host_set->ports[i] = ap;
4862 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
4863 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
4864 (ap->pio_mask << ATA_SHIFT_PIO);
4865
4866 /* print per-port info to dmesg */
4867 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
4868 "bmdma 0x%lX irq %lu\n",
4869 ap->id,
4870 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
4871 ata_mode_string(xfer_mode_mask),
4872 ap->ioaddr.cmd_addr,
4873 ap->ioaddr.ctl_addr,
4874 ap->ioaddr.bmdma_addr,
4875 ent->irq);
4876
4877 ata_chk_status(ap);
4878 host_set->ops->irq_clear(ap);
4879 count++;
4880 }
4881
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004882 if (!count)
4883 goto err_free_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004884
4885 /* obtain irq, that is shared between channels */
4886 if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
4887 DRV_NAME, host_set))
4888 goto err_out;
4889
4890 /* perform each probe synchronously */
4891 DPRINTK("probe begin\n");
4892 for (i = 0; i < count; i++) {
4893 struct ata_port *ap;
4894 int rc;
4895
4896 ap = host_set->ports[i];
4897
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004898 DPRINTK("ata%u: bus probe begin\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004899 rc = ata_bus_probe(ap);
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004900 DPRINTK("ata%u: bus probe end\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901
4902 if (rc) {
4903 /* FIXME: do something useful here?
4904 * Current libata behavior will
4905 * tear down everything when
4906 * the module is removed
4907 * or the h/w is unplugged.
4908 */
4909 }
4910
4911 rc = scsi_add_host(ap->host, dev);
4912 if (rc) {
4913 printk(KERN_ERR "ata%u: scsi_add_host failed\n",
4914 ap->id);
4915 /* FIXME: do something useful here */
4916 /* FIXME: handle unconditional calls to
4917 * scsi_scan_host and ata_host_remove, below,
4918 * at the very least
4919 */
4920 }
4921 }
4922
4923 /* probes are done, now scan each port's disk(s) */
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004924 DPRINTK("host probe begin\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925 for (i = 0; i < count; i++) {
4926 struct ata_port *ap = host_set->ports[i];
4927
Jeff Garzik644dd0c2005-10-03 15:55:19 -04004928 ata_scsi_scan_host(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929 }
4930
4931 dev_set_drvdata(dev, host_set);
4932
4933 VPRINTK("EXIT, returning %u\n", ent->n_ports);
4934 return ent->n_ports; /* success */
4935
4936err_out:
4937 for (i = 0; i < count; i++) {
4938 ata_host_remove(host_set->ports[i], 1);
4939 scsi_host_put(host_set->ports[i]->host);
4940 }
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004941err_free_ret:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942 kfree(host_set);
4943 VPRINTK("EXIT, returning 0\n");
4944 return 0;
4945}
4946
4947/**
Alan Cox17b14452005-09-15 15:44:00 +01004948 * ata_host_set_remove - PCI layer callback for device removal
4949 * @host_set: ATA host set that was removed
4950 *
4951 * Unregister all objects associated with this host set. Free those
4952 * objects.
4953 *
4954 * LOCKING:
4955 * Inherited from calling layer (may sleep).
4956 */
4957
Alan Cox17b14452005-09-15 15:44:00 +01004958void ata_host_set_remove(struct ata_host_set *host_set)
4959{
4960 struct ata_port *ap;
4961 unsigned int i;
4962
4963 for (i = 0; i < host_set->n_ports; i++) {
4964 ap = host_set->ports[i];
4965 scsi_remove_host(ap->host);
4966 }
4967
4968 free_irq(host_set->irq, host_set);
4969
4970 for (i = 0; i < host_set->n_ports; i++) {
4971 ap = host_set->ports[i];
4972
4973 ata_scsi_release(ap->host);
4974
4975 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
4976 struct ata_ioports *ioaddr = &ap->ioaddr;
4977
4978 if (ioaddr->cmd_addr == 0x1f0)
4979 release_region(0x1f0, 8);
4980 else if (ioaddr->cmd_addr == 0x170)
4981 release_region(0x170, 8);
4982 }
4983
4984 scsi_host_put(ap->host);
4985 }
4986
4987 if (host_set->ops->host_stop)
4988 host_set->ops->host_stop(host_set);
4989
4990 kfree(host_set);
4991}
4992
4993/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994 * ata_scsi_release - SCSI layer callback hook for host unload
4995 * @host: libata host to be unloaded
4996 *
4997 * Performs all duties necessary to shut down a libata port...
4998 * Kill port kthread, disable port, and release resources.
4999 *
5000 * LOCKING:
5001 * Inherited from SCSI layer.
5002 *
5003 * RETURNS:
5004 * One.
5005 */
5006
5007int ata_scsi_release(struct Scsi_Host *host)
5008{
5009 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
5010
5011 DPRINTK("ENTER\n");
5012
5013 ap->ops->port_disable(ap);
5014 ata_host_remove(ap, 0);
5015
5016 DPRINTK("EXIT\n");
5017 return 1;
5018}
5019
5020/**
5021 * ata_std_ports - initialize ioaddr with standard port offsets.
5022 * @ioaddr: IO address structure to be initialized
Edward Falk0baab862005-06-02 18:17:13 -04005023 *
5024 * Utility function which initializes data_addr, error_addr,
5025 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
5026 * device_addr, status_addr, and command_addr to standard offsets
5027 * relative to cmd_addr.
5028 *
5029 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005030 */
Edward Falk0baab862005-06-02 18:17:13 -04005031
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032void ata_std_ports(struct ata_ioports *ioaddr)
5033{
5034 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
5035 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
5036 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
5037 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
5038 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
5039 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
5040 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
5041 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
5042 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
5043 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
5044}
5045
Edward Falk0baab862005-06-02 18:17:13 -04005046
Jeff Garzik374b1872005-08-30 05:42:52 -04005047#ifdef CONFIG_PCI
5048
5049void ata_pci_host_stop (struct ata_host_set *host_set)
5050{
5051 struct pci_dev *pdev = to_pci_dev(host_set->dev);
5052
5053 pci_iounmap(pdev, host_set->mmio_base);
5054}
5055
Edward Falk0baab862005-06-02 18:17:13 -04005056/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07005057 * ata_pci_remove_one - PCI layer callback for device removal
5058 * @pdev: PCI device that was removed
5059 *
5060 * PCI layer indicates to libata via this hook that
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04005061 * hot-unplug or module unload event has occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005062 * Handle this by unregistering all objects associated
5063 * with this PCI device. Free those objects. Then finally
5064 * release PCI resources and disable device.
5065 *
5066 * LOCKING:
5067 * Inherited from PCI layer (may sleep).
5068 */
5069
5070void ata_pci_remove_one (struct pci_dev *pdev)
5071{
5072 struct device *dev = pci_dev_to_dev(pdev);
5073 struct ata_host_set *host_set = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005074
Alan Cox17b14452005-09-15 15:44:00 +01005075 ata_host_set_remove(host_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005076 pci_release_regions(pdev);
5077 pci_disable_device(pdev);
5078 dev_set_drvdata(dev, NULL);
5079}
5080
5081/* move to PCI subsystem */
Jeff Garzik057ace52005-10-22 14:27:05 -04005082int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083{
5084 unsigned long tmp = 0;
5085
5086 switch (bits->width) {
5087 case 1: {
5088 u8 tmp8 = 0;
5089 pci_read_config_byte(pdev, bits->reg, &tmp8);
5090 tmp = tmp8;
5091 break;
5092 }
5093 case 2: {
5094 u16 tmp16 = 0;
5095 pci_read_config_word(pdev, bits->reg, &tmp16);
5096 tmp = tmp16;
5097 break;
5098 }
5099 case 4: {
5100 u32 tmp32 = 0;
5101 pci_read_config_dword(pdev, bits->reg, &tmp32);
5102 tmp = tmp32;
5103 break;
5104 }
5105
5106 default:
5107 return -EINVAL;
5108 }
5109
5110 tmp &= bits->mask;
5111
5112 return (tmp == bits->val) ? 1 : 0;
5113}
Jens Axboe9b847542006-01-06 09:28:07 +01005114
5115int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t state)
5116{
5117 pci_save_state(pdev);
5118 pci_disable_device(pdev);
5119 pci_set_power_state(pdev, PCI_D3hot);
5120 return 0;
5121}
5122
5123int ata_pci_device_resume(struct pci_dev *pdev)
5124{
5125 pci_set_power_state(pdev, PCI_D0);
5126 pci_restore_state(pdev);
5127 pci_enable_device(pdev);
5128 pci_set_master(pdev);
5129 return 0;
5130}
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131#endif /* CONFIG_PCI */
5132
5133
Linus Torvalds1da177e2005-04-16 15:20:36 -07005134static int __init ata_init(void)
5135{
5136 ata_wq = create_workqueue("ata");
5137 if (!ata_wq)
5138 return -ENOMEM;
5139
5140 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
5141 return 0;
5142}
5143
5144static void __exit ata_exit(void)
5145{
5146 destroy_workqueue(ata_wq);
5147}
5148
5149module_init(ata_init);
5150module_exit(ata_exit);
5151
Jeff Garzik67846b32005-10-05 02:58:32 -04005152static unsigned long ratelimit_time;
5153static spinlock_t ata_ratelimit_lock = SPIN_LOCK_UNLOCKED;
5154
5155int ata_ratelimit(void)
5156{
5157 int rc;
5158 unsigned long flags;
5159
5160 spin_lock_irqsave(&ata_ratelimit_lock, flags);
5161
5162 if (time_after(jiffies, ratelimit_time)) {
5163 rc = 1;
5164 ratelimit_time = jiffies + (HZ/5);
5165 } else
5166 rc = 0;
5167
5168 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
5169
5170 return rc;
5171}
5172
Linus Torvalds1da177e2005-04-16 15:20:36 -07005173/*
5174 * libata is essentially a library of internal helper functions for
5175 * low-level ATA host controller drivers. As such, the API/ABI is
5176 * likely to change as new drivers are added and updated.
5177 * Do not depend on ABI/API stability.
5178 */
5179
5180EXPORT_SYMBOL_GPL(ata_std_bios_param);
5181EXPORT_SYMBOL_GPL(ata_std_ports);
5182EXPORT_SYMBOL_GPL(ata_device_add);
Alan Cox17b14452005-09-15 15:44:00 +01005183EXPORT_SYMBOL_GPL(ata_host_set_remove);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184EXPORT_SYMBOL_GPL(ata_sg_init);
5185EXPORT_SYMBOL_GPL(ata_sg_init_one);
Tejun Heo76014422006-02-11 15:13:49 +09005186EXPORT_SYMBOL_GPL(__ata_qc_complete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005187EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
5188EXPORT_SYMBOL_GPL(ata_eng_timeout);
5189EXPORT_SYMBOL_GPL(ata_tf_load);
5190EXPORT_SYMBOL_GPL(ata_tf_read);
5191EXPORT_SYMBOL_GPL(ata_noop_dev_select);
5192EXPORT_SYMBOL_GPL(ata_std_dev_select);
5193EXPORT_SYMBOL_GPL(ata_tf_to_fis);
5194EXPORT_SYMBOL_GPL(ata_tf_from_fis);
5195EXPORT_SYMBOL_GPL(ata_check_status);
5196EXPORT_SYMBOL_GPL(ata_altstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005197EXPORT_SYMBOL_GPL(ata_exec_command);
5198EXPORT_SYMBOL_GPL(ata_port_start);
5199EXPORT_SYMBOL_GPL(ata_port_stop);
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04005200EXPORT_SYMBOL_GPL(ata_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005201EXPORT_SYMBOL_GPL(ata_interrupt);
5202EXPORT_SYMBOL_GPL(ata_qc_prep);
5203EXPORT_SYMBOL_GPL(ata_bmdma_setup);
5204EXPORT_SYMBOL_GPL(ata_bmdma_start);
5205EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
5206EXPORT_SYMBOL_GPL(ata_bmdma_status);
5207EXPORT_SYMBOL_GPL(ata_bmdma_stop);
5208EXPORT_SYMBOL_GPL(ata_port_probe);
5209EXPORT_SYMBOL_GPL(sata_phy_reset);
5210EXPORT_SYMBOL_GPL(__sata_phy_reset);
5211EXPORT_SYMBOL_GPL(ata_bus_reset);
Tejun Heo8a19ac82006-02-02 18:20:00 +09005212EXPORT_SYMBOL_GPL(ata_std_probeinit);
Tejun Heoc2bd5802006-01-24 17:05:22 +09005213EXPORT_SYMBOL_GPL(ata_std_softreset);
5214EXPORT_SYMBOL_GPL(sata_std_hardreset);
5215EXPORT_SYMBOL_GPL(ata_std_postreset);
5216EXPORT_SYMBOL_GPL(ata_std_probe_reset);
Tejun Heoa62c0fc2006-01-24 17:05:22 +09005217EXPORT_SYMBOL_GPL(ata_drive_probe_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005218EXPORT_SYMBOL_GPL(ata_port_disable);
Jeff Garzik67846b32005-10-05 02:58:32 -04005219EXPORT_SYMBOL_GPL(ata_ratelimit);
Tejun Heo6f8b9952006-01-24 17:05:21 +09005220EXPORT_SYMBOL_GPL(ata_busy_sleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005221EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
5222EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
Tejun Heof29841e2006-02-10 15:10:48 +09005223EXPORT_SYMBOL_GPL(ata_scsi_timed_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005224EXPORT_SYMBOL_GPL(ata_scsi_error);
5225EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
5226EXPORT_SYMBOL_GPL(ata_scsi_release);
5227EXPORT_SYMBOL_GPL(ata_host_intr);
5228EXPORT_SYMBOL_GPL(ata_dev_classify);
Tejun Heo6a62a042006-02-13 10:02:46 +09005229EXPORT_SYMBOL_GPL(ata_id_string);
5230EXPORT_SYMBOL_GPL(ata_id_c_string);
Brad Campbell6f2f3812005-05-12 15:07:47 -04005231EXPORT_SYMBOL_GPL(ata_dev_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005232EXPORT_SYMBOL_GPL(ata_scsi_simulate);
Tejun Heoa72ec4c2006-01-23 13:09:37 +09005233EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
5234EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235
Alan Cox1bc4ccf2006-01-09 17:18:14 +00005236EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
Alan Cox452503f2005-10-21 19:01:32 -04005237EXPORT_SYMBOL_GPL(ata_timing_compute);
5238EXPORT_SYMBOL_GPL(ata_timing_merge);
5239
Linus Torvalds1da177e2005-04-16 15:20:36 -07005240#ifdef CONFIG_PCI
5241EXPORT_SYMBOL_GPL(pci_test_config_bits);
Jeff Garzik374b1872005-08-30 05:42:52 -04005242EXPORT_SYMBOL_GPL(ata_pci_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005243EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
5244EXPORT_SYMBOL_GPL(ata_pci_init_one);
5245EXPORT_SYMBOL_GPL(ata_pci_remove_one);
Jens Axboe9b847542006-01-06 09:28:07 +01005246EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
5247EXPORT_SYMBOL_GPL(ata_pci_device_resume);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005248#endif /* CONFIG_PCI */
Jens Axboe9b847542006-01-06 09:28:07 +01005249
5250EXPORT_SYMBOL_GPL(ata_device_suspend);
5251EXPORT_SYMBOL_GPL(ata_device_resume);
5252EXPORT_SYMBOL_GPL(ata_scsi_device_suspend);
5253EXPORT_SYMBOL_GPL(ata_scsi_device_resume);