blob: a0b6ca3d1a0c2f4dd7e5e8a094b4da9be78b25f5 [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
Albert Lee59a10b12005-10-12 15:09:42 +080064static void ata_dev_reread_id(struct ata_port *ap, struct ata_device *dev);
Albert Lee8bf62ece2005-05-12 15:29:42 -040065static void ata_dev_init_params(struct ata_port *ap, 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74static unsigned int ata_unique_id = 1;
75static struct workqueue_struct *ata_wq;
76
Jeff Garzik1623c812005-08-30 03:37:42 -040077int atapi_enabled = 0;
78module_param(atapi_enabled, int, 0444);
79MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)");
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081MODULE_AUTHOR("Jeff Garzik");
82MODULE_DESCRIPTION("Library module for ATA devices");
83MODULE_LICENSE("GPL");
84MODULE_VERSION(DRV_VERSION);
85
Edward Falk0baab862005-06-02 18:17:13 -040086
87/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
89 * @tf: Taskfile to convert
90 * @fis: Buffer into which data will output
91 * @pmp: Port multiplier port
92 *
93 * Converts a standard ATA taskfile to a Serial ATA
94 * FIS structure (Register - Host to Device).
95 *
96 * LOCKING:
97 * Inherited from caller.
98 */
99
Jeff Garzik057ace52005-10-22 14:27:05 -0400100void ata_tf_to_fis(const struct ata_taskfile *tf, u8 *fis, u8 pmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
102 fis[0] = 0x27; /* Register - Host to Device FIS */
103 fis[1] = (pmp & 0xf) | (1 << 7); /* Port multiplier number,
104 bit 7 indicates Command FIS */
105 fis[2] = tf->command;
106 fis[3] = tf->feature;
107
108 fis[4] = tf->lbal;
109 fis[5] = tf->lbam;
110 fis[6] = tf->lbah;
111 fis[7] = tf->device;
112
113 fis[8] = tf->hob_lbal;
114 fis[9] = tf->hob_lbam;
115 fis[10] = tf->hob_lbah;
116 fis[11] = tf->hob_feature;
117
118 fis[12] = tf->nsect;
119 fis[13] = tf->hob_nsect;
120 fis[14] = 0;
121 fis[15] = tf->ctl;
122
123 fis[16] = 0;
124 fis[17] = 0;
125 fis[18] = 0;
126 fis[19] = 0;
127}
128
129/**
130 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
131 * @fis: Buffer from which data will be input
132 * @tf: Taskfile to output
133 *
Mark Lorde12a1be2005-11-12 18:55:45 -0500134 * Converts a serial ATA FIS structure to a standard ATA taskfile.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 *
136 * LOCKING:
137 * Inherited from caller.
138 */
139
Jeff Garzik057ace52005-10-22 14:27:05 -0400140void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
142 tf->command = fis[2]; /* status */
143 tf->feature = fis[3]; /* error */
144
145 tf->lbal = fis[4];
146 tf->lbam = fis[5];
147 tf->lbah = fis[6];
148 tf->device = fis[7];
149
150 tf->hob_lbal = fis[8];
151 tf->hob_lbam = fis[9];
152 tf->hob_lbah = fis[10];
153
154 tf->nsect = fis[12];
155 tf->hob_nsect = fis[13];
156}
157
Albert Lee8cbd6df2005-10-12 15:06:27 +0800158static const u8 ata_rw_cmds[] = {
159 /* pio multi */
160 ATA_CMD_READ_MULTI,
161 ATA_CMD_WRITE_MULTI,
162 ATA_CMD_READ_MULTI_EXT,
163 ATA_CMD_WRITE_MULTI_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100164 0,
165 0,
166 0,
167 ATA_CMD_WRITE_MULTI_FUA_EXT,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800168 /* pio */
169 ATA_CMD_PIO_READ,
170 ATA_CMD_PIO_WRITE,
171 ATA_CMD_PIO_READ_EXT,
172 ATA_CMD_PIO_WRITE_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100173 0,
174 0,
175 0,
176 0,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800177 /* dma */
178 ATA_CMD_READ,
179 ATA_CMD_WRITE,
180 ATA_CMD_READ_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100181 ATA_CMD_WRITE_EXT,
182 0,
183 0,
184 0,
185 ATA_CMD_WRITE_FUA_EXT
Albert Lee8cbd6df2005-10-12 15:06:27 +0800186};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188/**
Albert Lee8cbd6df2005-10-12 15:06:27 +0800189 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
190 * @qc: command to examine and configure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 *
Albert Lee8cbd6df2005-10-12 15:06:27 +0800192 * Examine the device configuration and tf->flags to calculate
193 * the proper read/write commands and protocol to use.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 *
195 * LOCKING:
196 * caller.
197 */
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100198int ata_rwcmd_protocol(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
Albert Lee8cbd6df2005-10-12 15:06:27 +0800200 struct ata_taskfile *tf = &qc->tf;
201 struct ata_device *dev = qc->dev;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100202 u8 cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100204 int index, fua, lba48, write;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800205
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100206 fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800207 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
208 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Albert Lee8cbd6df2005-10-12 15:06:27 +0800210 if (dev->flags & ATA_DFLAG_PIO) {
211 tf->protocol = ATA_PROT_PIO;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100212 index = dev->multi_count ? 0 : 8;
Alan Cox8d238e02006-01-17 20:50:31 +0000213 } else if (lba48 && (qc->ap->flags & ATA_FLAG_PIO_LBA48)) {
214 /* Unable to use DMA due to host limitation */
215 tf->protocol = ATA_PROT_PIO;
216 index = dev->multi_count ? 0 : 4;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800217 } else {
218 tf->protocol = ATA_PROT_DMA;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100219 index = 16;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100222 cmd = ata_rw_cmds[index + fua + lba48 + write];
223 if (cmd) {
224 tf->command = cmd;
225 return 0;
226 }
227 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228}
229
Arjan van de Ven98ac62d2005-11-28 10:06:23 +0100230static const char * const xfer_mode_str[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 "UDMA/16",
232 "UDMA/25",
233 "UDMA/33",
234 "UDMA/44",
235 "UDMA/66",
236 "UDMA/100",
237 "UDMA/133",
238 "UDMA7",
239 "MWDMA0",
240 "MWDMA1",
241 "MWDMA2",
242 "PIO0",
243 "PIO1",
244 "PIO2",
245 "PIO3",
246 "PIO4",
247};
248
249/**
250 * ata_udma_string - convert UDMA bit offset to string
251 * @mask: mask of bits supported; only highest bit counts.
252 *
253 * Determine string which represents the highest speed
254 * (highest bit in @udma_mask).
255 *
256 * LOCKING:
257 * None.
258 *
259 * RETURNS:
260 * Constant C string representing highest speed listed in
261 * @udma_mask, or the constant C string "<n/a>".
262 */
263
264static const char *ata_mode_string(unsigned int mask)
265{
266 int i;
267
268 for (i = 7; i >= 0; i--)
269 if (mask & (1 << i))
270 goto out;
271 for (i = ATA_SHIFT_MWDMA + 2; i >= ATA_SHIFT_MWDMA; i--)
272 if (mask & (1 << i))
273 goto out;
274 for (i = ATA_SHIFT_PIO + 4; i >= ATA_SHIFT_PIO; i--)
275 if (mask & (1 << i))
276 goto out;
277
278 return "<n/a>";
279
280out:
281 return xfer_mode_str[i];
282}
283
284/**
285 * ata_pio_devchk - PATA device presence detection
286 * @ap: ATA channel to examine
287 * @device: Device to examine (starting at zero)
288 *
289 * This technique was originally described in
290 * Hale Landis's ATADRVR (www.ata-atapi.com), and
291 * later found its way into the ATA/ATAPI spec.
292 *
293 * Write a pattern to the ATA shadow registers,
294 * and if a device is present, it will respond by
295 * correctly storing and echoing back the
296 * ATA shadow register contents.
297 *
298 * LOCKING:
299 * caller.
300 */
301
302static unsigned int ata_pio_devchk(struct ata_port *ap,
303 unsigned int device)
304{
305 struct ata_ioports *ioaddr = &ap->ioaddr;
306 u8 nsect, lbal;
307
308 ap->ops->dev_select(ap, device);
309
310 outb(0x55, ioaddr->nsect_addr);
311 outb(0xaa, ioaddr->lbal_addr);
312
313 outb(0xaa, ioaddr->nsect_addr);
314 outb(0x55, ioaddr->lbal_addr);
315
316 outb(0x55, ioaddr->nsect_addr);
317 outb(0xaa, ioaddr->lbal_addr);
318
319 nsect = inb(ioaddr->nsect_addr);
320 lbal = inb(ioaddr->lbal_addr);
321
322 if ((nsect == 0x55) && (lbal == 0xaa))
323 return 1; /* we found a device */
324
325 return 0; /* nothing found */
326}
327
328/**
329 * ata_mmio_devchk - PATA device presence detection
330 * @ap: ATA channel to examine
331 * @device: Device to examine (starting at zero)
332 *
333 * This technique was originally described in
334 * Hale Landis's ATADRVR (www.ata-atapi.com), and
335 * later found its way into the ATA/ATAPI spec.
336 *
337 * Write a pattern to the ATA shadow registers,
338 * and if a device is present, it will respond by
339 * correctly storing and echoing back the
340 * ATA shadow register contents.
341 *
342 * LOCKING:
343 * caller.
344 */
345
346static unsigned int ata_mmio_devchk(struct ata_port *ap,
347 unsigned int device)
348{
349 struct ata_ioports *ioaddr = &ap->ioaddr;
350 u8 nsect, lbal;
351
352 ap->ops->dev_select(ap, device);
353
354 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
355 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
356
357 writeb(0xaa, (void __iomem *) ioaddr->nsect_addr);
358 writeb(0x55, (void __iomem *) ioaddr->lbal_addr);
359
360 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
361 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
362
363 nsect = readb((void __iomem *) ioaddr->nsect_addr);
364 lbal = readb((void __iomem *) ioaddr->lbal_addr);
365
366 if ((nsect == 0x55) && (lbal == 0xaa))
367 return 1; /* we found a device */
368
369 return 0; /* nothing found */
370}
371
372/**
373 * ata_devchk - PATA device presence detection
374 * @ap: ATA channel to examine
375 * @device: Device to examine (starting at zero)
376 *
377 * Dispatch ATA device presence detection, depending
378 * on whether we are using PIO or MMIO to talk to the
379 * ATA shadow registers.
380 *
381 * LOCKING:
382 * caller.
383 */
384
385static unsigned int ata_devchk(struct ata_port *ap,
386 unsigned int device)
387{
388 if (ap->flags & ATA_FLAG_MMIO)
389 return ata_mmio_devchk(ap, device);
390 return ata_pio_devchk(ap, device);
391}
392
393/**
394 * ata_dev_classify - determine device type based on ATA-spec signature
395 * @tf: ATA taskfile register set for device to be identified
396 *
397 * Determine from taskfile register contents whether a device is
398 * ATA or ATAPI, as per "Signature and persistence" section
399 * of ATA/PI spec (volume 1, sect 5.14).
400 *
401 * LOCKING:
402 * None.
403 *
404 * RETURNS:
405 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
406 * the event of failure.
407 */
408
Jeff Garzik057ace52005-10-22 14:27:05 -0400409unsigned int ata_dev_classify(const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
411 /* Apple's open source Darwin code hints that some devices only
412 * put a proper signature into the LBA mid/high registers,
413 * So, we only check those. It's sufficient for uniqueness.
414 */
415
416 if (((tf->lbam == 0) && (tf->lbah == 0)) ||
417 ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
418 DPRINTK("found ATA device by sig\n");
419 return ATA_DEV_ATA;
420 }
421
422 if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
423 ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
424 DPRINTK("found ATAPI device by sig\n");
425 return ATA_DEV_ATAPI;
426 }
427
428 DPRINTK("unknown device\n");
429 return ATA_DEV_UNKNOWN;
430}
431
432/**
433 * ata_dev_try_classify - Parse returned ATA device signature
434 * @ap: ATA channel to examine
435 * @device: Device to examine (starting at zero)
Tejun Heob4dc7622006-01-24 17:05:22 +0900436 * @r_err: Value of error register on completion
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 *
438 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
439 * an ATA/ATAPI-defined set of values is placed in the ATA
440 * shadow registers, indicating the results of device detection
441 * and diagnostics.
442 *
443 * Select the ATA device, and read the values from the ATA shadow
444 * registers. Then parse according to the Error register value,
445 * and the spec-defined values examined by ata_dev_classify().
446 *
447 * LOCKING:
448 * caller.
Tejun Heob4dc7622006-01-24 17:05:22 +0900449 *
450 * RETURNS:
451 * Device type - %ATA_DEV_ATA, %ATA_DEV_ATAPI or %ATA_DEV_NONE.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 */
453
Tejun Heob4dc7622006-01-24 17:05:22 +0900454static unsigned int
455ata_dev_try_classify(struct ata_port *ap, unsigned int device, u8 *r_err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 struct ata_taskfile tf;
458 unsigned int class;
459 u8 err;
460
461 ap->ops->dev_select(ap, device);
462
463 memset(&tf, 0, sizeof(tf));
464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 ap->ops->tf_read(ap, &tf);
Jeff Garzik0169e282005-10-29 21:25:10 -0400466 err = tf.feature;
Tejun Heob4dc7622006-01-24 17:05:22 +0900467 if (r_err)
468 *r_err = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
470 /* see if device passed diags */
471 if (err == 1)
472 /* do nothing */ ;
473 else if ((device == 0) && (err == 0x81))
474 /* do nothing */ ;
475 else
Tejun Heob4dc7622006-01-24 17:05:22 +0900476 return ATA_DEV_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
Tejun Heob4dc7622006-01-24 17:05:22 +0900478 /* determine if device is ATA or ATAPI */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 class = ata_dev_classify(&tf);
Tejun Heob4dc7622006-01-24 17:05:22 +0900480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 if (class == ATA_DEV_UNKNOWN)
Tejun Heob4dc7622006-01-24 17:05:22 +0900482 return ATA_DEV_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
Tejun Heob4dc7622006-01-24 17:05:22 +0900484 return ATA_DEV_NONE;
485 return class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486}
487
488/**
489 * ata_dev_id_string - Convert IDENTIFY DEVICE page into string
490 * @id: IDENTIFY DEVICE results we will examine
491 * @s: string into which data is output
492 * @ofs: offset into identify device page
493 * @len: length of string to return. must be an even number.
494 *
495 * The strings in the IDENTIFY DEVICE page are broken up into
496 * 16-bit chunks. Run through the string, and output each
497 * 8-bit chunk linearly, regardless of platform.
498 *
499 * LOCKING:
500 * caller.
501 */
502
Jeff Garzik057ace52005-10-22 14:27:05 -0400503void ata_dev_id_string(const u16 *id, unsigned char *s,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 unsigned int ofs, unsigned int len)
505{
506 unsigned int c;
507
508 while (len > 0) {
509 c = id[ofs] >> 8;
510 *s = c;
511 s++;
512
513 c = id[ofs] & 0xff;
514 *s = c;
515 s++;
516
517 ofs++;
518 len -= 2;
519 }
520}
521
Tejun Heo0e949ff2006-02-12 22:47:04 +0900522/**
523 * ata_dev_id_c_string - Convert IDENTIFY DEVICE page into C string
524 * @id: IDENTIFY DEVICE results we will examine
525 * @s: string into which data is output
526 * @ofs: offset into identify device page
527 * @len: length of string to return. must be an odd number.
528 *
529 * This function is identical to ata_dev_id_string except that it
530 * trims trailing spaces and terminates the resulting string with
531 * null. @len must be actual maximum length (even number) + 1.
532 *
533 * LOCKING:
534 * caller.
535 */
536void ata_dev_id_c_string(const u16 *id, unsigned char *s,
537 unsigned int ofs, unsigned int len)
538{
539 unsigned char *p;
540
541 WARN_ON(!(len & 1));
542
543 ata_dev_id_string(id, s, ofs, len - 1);
544
545 p = s + strnlen(s, len - 1);
546 while (p > s && p[-1] == ' ')
547 p--;
548 *p = '\0';
549}
Edward Falk0baab862005-06-02 18:17:13 -0400550
Tejun Heo29407402006-02-12 22:47:04 +0900551static u64 ata_id_n_sectors(const u16 *id)
552{
553 if (ata_id_has_lba(id)) {
554 if (ata_id_has_lba48(id))
555 return ata_id_u64(id, 100);
556 else
557 return ata_id_u32(id, 60);
558 } else {
559 if (ata_id_current_chs_valid(id))
560 return ata_id_u32(id, 57);
561 else
562 return id[1] * id[3] * id[6];
563 }
564}
565
Edward Falk0baab862005-06-02 18:17:13 -0400566/**
567 * ata_noop_dev_select - Select device 0/1 on ATA bus
568 * @ap: ATA channel to manipulate
569 * @device: ATA device (numbered from zero) to select
570 *
571 * This function performs no actual function.
572 *
573 * May be used as the dev_select() entry in ata_port_operations.
574 *
575 * LOCKING:
576 * caller.
577 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
579{
580}
581
Edward Falk0baab862005-06-02 18:17:13 -0400582
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583/**
584 * ata_std_dev_select - Select device 0/1 on ATA bus
585 * @ap: ATA channel to manipulate
586 * @device: ATA device (numbered from zero) to select
587 *
588 * Use the method defined in the ATA specification to
589 * make either device 0, or device 1, active on the
Edward Falk0baab862005-06-02 18:17:13 -0400590 * ATA channel. Works with both PIO and MMIO.
591 *
592 * May be used as the dev_select() entry in ata_port_operations.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 *
594 * LOCKING:
595 * caller.
596 */
597
598void ata_std_dev_select (struct ata_port *ap, unsigned int device)
599{
600 u8 tmp;
601
602 if (device == 0)
603 tmp = ATA_DEVICE_OBS;
604 else
605 tmp = ATA_DEVICE_OBS | ATA_DEV1;
606
607 if (ap->flags & ATA_FLAG_MMIO) {
608 writeb(tmp, (void __iomem *) ap->ioaddr.device_addr);
609 } else {
610 outb(tmp, ap->ioaddr.device_addr);
611 }
612 ata_pause(ap); /* needed; also flushes, for mmio */
613}
614
615/**
616 * ata_dev_select - Select device 0/1 on ATA bus
617 * @ap: ATA channel to manipulate
618 * @device: ATA device (numbered from zero) to select
619 * @wait: non-zero to wait for Status register BSY bit to clear
620 * @can_sleep: non-zero if context allows sleeping
621 *
622 * Use the method defined in the ATA specification to
623 * make either device 0, or device 1, active on the
624 * ATA channel.
625 *
626 * This is a high-level version of ata_std_dev_select(),
627 * which additionally provides the services of inserting
628 * the proper pauses and status polling, where needed.
629 *
630 * LOCKING:
631 * caller.
632 */
633
634void ata_dev_select(struct ata_port *ap, unsigned int device,
635 unsigned int wait, unsigned int can_sleep)
636{
637 VPRINTK("ENTER, ata%u: device %u, wait %u\n",
638 ap->id, device, wait);
639
640 if (wait)
641 ata_wait_idle(ap);
642
643 ap->ops->dev_select(ap, device);
644
645 if (wait) {
646 if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
647 msleep(150);
648 ata_wait_idle(ap);
649 }
650}
651
652/**
653 * ata_dump_id - IDENTIFY DEVICE info debugging output
Tejun Heo0bd33002006-02-12 22:47:05 +0900654 * @id: IDENTIFY DEVICE page to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 *
Tejun Heo0bd33002006-02-12 22:47:05 +0900656 * Dump selected 16-bit words from the given IDENTIFY DEVICE
657 * page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 *
659 * LOCKING:
660 * caller.
661 */
662
Tejun Heo0bd33002006-02-12 22:47:05 +0900663static inline void ata_dump_id(const u16 *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664{
665 DPRINTK("49==0x%04x "
666 "53==0x%04x "
667 "63==0x%04x "
668 "64==0x%04x "
669 "75==0x%04x \n",
Tejun Heo0bd33002006-02-12 22:47:05 +0900670 id[49],
671 id[53],
672 id[63],
673 id[64],
674 id[75]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 DPRINTK("80==0x%04x "
676 "81==0x%04x "
677 "82==0x%04x "
678 "83==0x%04x "
679 "84==0x%04x \n",
Tejun Heo0bd33002006-02-12 22:47:05 +0900680 id[80],
681 id[81],
682 id[82],
683 id[83],
684 id[84]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 DPRINTK("88==0x%04x "
686 "93==0x%04x\n",
Tejun Heo0bd33002006-02-12 22:47:05 +0900687 id[88],
688 id[93]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689}
690
Alan Cox11e29e22005-10-21 18:46:32 -0400691/*
692 * Compute the PIO modes available for this device. This is not as
693 * trivial as it seems if we must consider early devices correctly.
694 *
695 * FIXME: pre IDE drive timing (do we care ?).
696 */
697
Jeff Garzik057ace52005-10-22 14:27:05 -0400698static unsigned int ata_pio_modes(const struct ata_device *adev)
Alan Cox11e29e22005-10-21 18:46:32 -0400699{
700 u16 modes;
701
Alan Coxffa29452006-01-09 17:14:40 +0000702 /* Usual case. Word 53 indicates word 64 is valid */
703 if (adev->id[ATA_ID_FIELD_VALID] & (1 << 1)) {
Alan Cox11e29e22005-10-21 18:46:32 -0400704 modes = adev->id[ATA_ID_PIO_MODES] & 0x03;
705 modes <<= 3;
706 modes |= 0x7;
707 return modes;
708 }
709
Alan Coxffa29452006-01-09 17:14:40 +0000710 /* If word 64 isn't valid then Word 51 high byte holds the PIO timing
711 number for the maximum. Turn it into a mask and return it */
712 modes = (2 << ((adev->id[ATA_ID_OLD_PIO_MODES] >> 8) & 0xFF)) - 1 ;
Alan Cox11e29e22005-10-21 18:46:32 -0400713 return modes;
Alan Coxffa29452006-01-09 17:14:40 +0000714 /* But wait.. there's more. Design your standards by committee and
715 you too can get a free iordy field to process. However its the
716 speeds not the modes that are supported... Note drivers using the
717 timing API will get this right anyway */
Alan Cox11e29e22005-10-21 18:46:32 -0400718}
719
Tejun Heo95064372006-01-23 13:09:37 +0900720static inline void
721ata_queue_packet_task(struct ata_port *ap)
722{
Tejun Heoc18d06f2006-02-02 00:56:10 +0900723 if (!(ap->flags & ATA_FLAG_FLUSH_PIO_TASK))
724 queue_work(ata_wq, &ap->packet_task);
Tejun Heo95064372006-01-23 13:09:37 +0900725}
726
727static inline void
728ata_queue_pio_task(struct ata_port *ap)
729{
Tejun Heoc18d06f2006-02-02 00:56:10 +0900730 if (!(ap->flags & ATA_FLAG_FLUSH_PIO_TASK))
731 queue_work(ata_wq, &ap->pio_task);
Tejun Heo95064372006-01-23 13:09:37 +0900732}
733
734static inline void
735ata_queue_delayed_pio_task(struct ata_port *ap, unsigned long delay)
736{
Tejun Heoc18d06f2006-02-02 00:56:10 +0900737 if (!(ap->flags & ATA_FLAG_FLUSH_PIO_TASK))
738 queue_delayed_work(ata_wq, &ap->pio_task, delay);
739}
740
741/**
742 * ata_flush_pio_tasks - Flush pio_task and packet_task
743 * @ap: the target ata_port
744 *
745 * After this function completes, pio_task and packet_task are
746 * guranteed not to be running or scheduled.
747 *
748 * LOCKING:
749 * Kernel thread context (may sleep)
750 */
751
752static void ata_flush_pio_tasks(struct ata_port *ap)
753{
754 int tmp = 0;
755 unsigned long flags;
756
757 DPRINTK("ENTER\n");
758
759 spin_lock_irqsave(&ap->host_set->lock, flags);
760 ap->flags |= ATA_FLAG_FLUSH_PIO_TASK;
761 spin_unlock_irqrestore(&ap->host_set->lock, flags);
762
763 DPRINTK("flush #1\n");
764 flush_workqueue(ata_wq);
765
766 /*
767 * At this point, if a task is running, it's guaranteed to see
768 * the FLUSH flag; thus, it will never queue pio tasks again.
769 * Cancel and flush.
770 */
771 tmp |= cancel_delayed_work(&ap->pio_task);
772 tmp |= cancel_delayed_work(&ap->packet_task);
773 if (!tmp) {
774 DPRINTK("flush #2\n");
775 flush_workqueue(ata_wq);
776 }
777
778 spin_lock_irqsave(&ap->host_set->lock, flags);
779 ap->flags &= ~ATA_FLAG_FLUSH_PIO_TASK;
780 spin_unlock_irqrestore(&ap->host_set->lock, flags);
781
782 DPRINTK("EXIT\n");
Tejun Heo95064372006-01-23 13:09:37 +0900783}
784
Tejun Heo77853bf2006-01-23 13:09:36 +0900785void ata_qc_complete_internal(struct ata_queued_cmd *qc)
Tejun Heoa2a7a662005-12-13 14:48:31 +0900786{
Tejun Heo77853bf2006-01-23 13:09:36 +0900787 struct completion *waiting = qc->private_data;
Tejun Heoa2a7a662005-12-13 14:48:31 +0900788
Tejun Heo77853bf2006-01-23 13:09:36 +0900789 qc->ap->ops->tf_read(qc->ap, &qc->tf);
Tejun Heoa2a7a662005-12-13 14:48:31 +0900790 complete(waiting);
Tejun Heoa2a7a662005-12-13 14:48:31 +0900791}
792
793/**
794 * ata_exec_internal - execute libata internal command
795 * @ap: Port to which the command is sent
796 * @dev: Device to which the command is sent
797 * @tf: Taskfile registers for the command and the result
798 * @dma_dir: Data tranfer direction of the command
799 * @buf: Data buffer of the command
800 * @buflen: Length of data buffer
801 *
802 * Executes libata internal command with timeout. @tf contains
803 * command on entry and result on return. Timeout and error
804 * conditions are reported via return value. No recovery action
805 * is taken after a command times out. It's caller's duty to
806 * clean up after timeout.
807 *
808 * LOCKING:
809 * None. Should be called with kernel context, might sleep.
810 */
811
812static unsigned
813ata_exec_internal(struct ata_port *ap, struct ata_device *dev,
814 struct ata_taskfile *tf,
815 int dma_dir, void *buf, unsigned int buflen)
816{
817 u8 command = tf->command;
818 struct ata_queued_cmd *qc;
819 DECLARE_COMPLETION(wait);
820 unsigned long flags;
Tejun Heo77853bf2006-01-23 13:09:36 +0900821 unsigned int err_mask;
Tejun Heoa2a7a662005-12-13 14:48:31 +0900822
823 spin_lock_irqsave(&ap->host_set->lock, flags);
824
825 qc = ata_qc_new_init(ap, dev);
826 BUG_ON(qc == NULL);
827
828 qc->tf = *tf;
829 qc->dma_dir = dma_dir;
830 if (dma_dir != DMA_NONE) {
831 ata_sg_init_one(qc, buf, buflen);
832 qc->nsect = buflen / ATA_SECT_SIZE;
833 }
834
Tejun Heo77853bf2006-01-23 13:09:36 +0900835 qc->private_data = &wait;
Tejun Heoa2a7a662005-12-13 14:48:31 +0900836 qc->complete_fn = ata_qc_complete_internal;
837
Tejun Heo9a3d9eb2006-01-23 13:09:36 +0900838 qc->err_mask = ata_qc_issue(qc);
839 if (qc->err_mask)
Tejun Heo8e436af2006-01-23 13:09:36 +0900840 ata_qc_complete(qc);
Tejun Heoa2a7a662005-12-13 14:48:31 +0900841
842 spin_unlock_irqrestore(&ap->host_set->lock, flags);
843
844 if (!wait_for_completion_timeout(&wait, ATA_TMOUT_INTERNAL)) {
845 spin_lock_irqsave(&ap->host_set->lock, flags);
846
847 /* We're racing with irq here. If we lose, the
848 * following test prevents us from completing the qc
849 * again. If completion irq occurs after here but
850 * before the caller cleans up, it will result in a
851 * spurious interrupt. We can live with that.
852 */
Tejun Heo77853bf2006-01-23 13:09:36 +0900853 if (qc->flags & ATA_QCFLAG_ACTIVE) {
Tejun Heo11a56d22006-01-23 13:09:36 +0900854 qc->err_mask = AC_ERR_TIMEOUT;
Tejun Heoa2a7a662005-12-13 14:48:31 +0900855 ata_qc_complete(qc);
856 printk(KERN_WARNING "ata%u: qc timeout (cmd 0x%x)\n",
857 ap->id, command);
858 }
859
860 spin_unlock_irqrestore(&ap->host_set->lock, flags);
861 }
862
Tejun Heo77853bf2006-01-23 13:09:36 +0900863 *tf = qc->tf;
864 err_mask = qc->err_mask;
865
866 ata_qc_free(qc);
867
868 return err_mask;
Tejun Heoa2a7a662005-12-13 14:48:31 +0900869}
870
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871/**
Alan Cox1bc4ccf2006-01-09 17:18:14 +0000872 * ata_pio_need_iordy - check if iordy needed
873 * @adev: ATA device
874 *
875 * Check if the current speed of the device requires IORDY. Used
876 * by various controllers for chip configuration.
877 */
878
879unsigned int ata_pio_need_iordy(const struct ata_device *adev)
880{
881 int pio;
882 int speed = adev->pio_mode - XFER_PIO_0;
883
884 if (speed < 2)
885 return 0;
886 if (speed > 2)
887 return 1;
888
889 /* If we have no drive specific rule, then PIO 2 is non IORDY */
890
891 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
892 pio = adev->id[ATA_ID_EIDE_PIO];
893 /* Is the speed faster than the drive allows non IORDY ? */
894 if (pio) {
895 /* This is cycle times not frequency - watch the logic! */
896 if (pio > 240) /* PIO2 is 240nS per cycle */
897 return 1;
898 return 0;
899 }
900 }
901 return 0;
902}
903
904/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 * ata_dev_identify - obtain IDENTIFY x DEVICE page
906 * @ap: port on which device we wish to probe resides
907 * @device: device bus address, starting at zero
908 *
909 * Following bus reset, we issue the IDENTIFY [PACKET] DEVICE
910 * command, and read back the 512-byte device information page.
911 * The device information page is fed to us via the standard
912 * PIO-IN protocol, but we hand-code it here. (TODO: investigate
913 * using standard PIO-IN paths)
914 *
915 * After reading the device information page, we use several
916 * bits of information from it to initialize data structures
917 * that will be used during the lifetime of the ata_device.
918 * Other data from the info page is used to disqualify certain
919 * older ATA devices we do not wish to support.
920 *
921 * LOCKING:
922 * Inherited from caller. Some functions called by this function
923 * obtain the host_set lock.
924 */
925
926static void ata_dev_identify(struct ata_port *ap, unsigned int device)
927{
928 struct ata_device *dev = &ap->device[device];
Albert Lee8bf62ece2005-05-12 15:29:42 -0400929 unsigned int major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 unsigned long xfer_modes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 unsigned int using_edd;
Tejun Heoa0123702005-12-13 14:49:31 +0900932 struct ata_taskfile tf;
933 unsigned int err_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 int rc;
935
936 if (!ata_dev_present(dev)) {
937 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
938 ap->id, device);
939 return;
940 }
941
942 if (ap->flags & (ATA_FLAG_SRST | ATA_FLAG_SATA_RESET))
943 using_edd = 0;
944 else
945 using_edd = 1;
946
947 DPRINTK("ENTER, host %u, dev %u\n", ap->id, device);
948
Tejun Heoa46314742006-02-11 19:11:13 +0900949 WARN_ON(dev->class != ATA_DEV_ATA && dev->class != ATA_DEV_ATAPI &&
950 dev->class != ATA_DEV_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
952 ata_dev_select(ap, device, 1, 1); /* select device 0/1 */
953
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954retry:
Tejun Heoa0123702005-12-13 14:49:31 +0900955 ata_tf_init(ap, &tf, device);
956
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 if (dev->class == ATA_DEV_ATA) {
Tejun Heoa0123702005-12-13 14:49:31 +0900958 tf.command = ATA_CMD_ID_ATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 DPRINTK("do ATA identify\n");
960 } else {
Tejun Heoa0123702005-12-13 14:49:31 +0900961 tf.command = ATA_CMD_ID_ATAPI;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 DPRINTK("do ATAPI identify\n");
963 }
964
Tejun Heoa0123702005-12-13 14:49:31 +0900965 tf.protocol = ATA_PROT_PIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
Tejun Heoa0123702005-12-13 14:49:31 +0900967 err_mask = ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE,
968 dev->id, sizeof(dev->id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
Tejun Heoa0123702005-12-13 14:49:31 +0900970 if (err_mask) {
971 if (err_mask & ~AC_ERR_DEV)
972 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 /*
975 * arg! EDD works for all test cases, but seems to return
976 * the ATA signature for some ATAPI devices. Until the
977 * reason for this is found and fixed, we fix up the mess
978 * here. If IDENTIFY DEVICE returns command aborted
979 * (as ATAPI devices do), then we issue an
980 * IDENTIFY PACKET DEVICE.
981 *
982 * ATA software reset (SRST, the default) does not appear
983 * to have this problem.
984 */
Albert Lee7c398332005-11-09 13:03:30 +0800985 if ((using_edd) && (dev->class == ATA_DEV_ATA)) {
Tejun Heoa0123702005-12-13 14:49:31 +0900986 u8 err = tf.feature;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 if (err & ATA_ABORTED) {
988 dev->class = ATA_DEV_ATAPI;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 goto retry;
990 }
991 }
992 goto err_out;
993 }
994
995 swap_buf_le16(dev->id, ATA_ID_WORDS);
996
997 /* print device capabilities */
998 printk(KERN_DEBUG "ata%u: dev %u cfg "
999 "49:%04x 82:%04x 83:%04x 84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1000 ap->id, device, dev->id[49],
1001 dev->id[82], dev->id[83], dev->id[84],
1002 dev->id[85], dev->id[86], dev->id[87],
1003 dev->id[88]);
1004
1005 /*
1006 * common ATA, ATAPI feature tests
1007 */
1008
Albert Lee8bf62ece2005-05-12 15:29:42 -04001009 /* we require DMA support (bits 8 of word 49) */
1010 if (!ata_id_has_dma(dev->id)) {
1011 printk(KERN_DEBUG "ata%u: no dma\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 goto err_out_nosup;
1013 }
1014
1015 /* quick-n-dirty find max transfer mode; for printk only */
1016 xfer_modes = dev->id[ATA_ID_UDMA_MODES];
1017 if (!xfer_modes)
1018 xfer_modes = (dev->id[ATA_ID_MWDMA_MODES]) << ATA_SHIFT_MWDMA;
Alan Cox11e29e22005-10-21 18:46:32 -04001019 if (!xfer_modes)
1020 xfer_modes = ata_pio_modes(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
Tejun Heo0bd33002006-02-12 22:47:05 +09001022 ata_dump_id(dev->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
1024 /* ATA-specific feature tests */
1025 if (dev->class == ATA_DEV_ATA) {
Tejun Heo29407402006-02-12 22:47:04 +09001026 dev->n_sectors = ata_id_n_sectors(dev->id);
1027
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 if (!ata_id_is_ata(dev->id)) /* sanity check */
1029 goto err_out_nosup;
1030
Albert Lee8bf62ece2005-05-12 15:29:42 -04001031 /* get major version */
Tejun Heo3d2ca912006-02-12 22:47:04 +09001032 major_version = ata_id_major_version(dev->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
Albert Lee8bf62ece2005-05-12 15:29:42 -04001034 /*
1035 * The exact sequence expected by certain pre-ATA4 drives is:
1036 * SRST RESET
1037 * IDENTIFY
1038 * INITIALIZE DEVICE PARAMETERS
1039 * anything else..
1040 * Some drives were very specific about that exact sequence.
1041 */
Albert Lee59a10b12005-10-12 15:09:42 +08001042 if (major_version < 4 || (!ata_id_has_lba(dev->id))) {
Albert Lee8bf62ece2005-05-12 15:29:42 -04001043 ata_dev_init_params(ap, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
Albert Lee59a10b12005-10-12 15:09:42 +08001045 /* current CHS translation info (id[53-58]) might be
1046 * changed. reread the identify device info.
1047 */
1048 ata_dev_reread_id(ap, dev);
1049 }
1050
Albert Lee8bf62ece2005-05-12 15:29:42 -04001051 if (ata_id_has_lba(dev->id)) {
1052 dev->flags |= ATA_DFLAG_LBA;
1053
Tejun Heo29407402006-02-12 22:47:04 +09001054 if (ata_id_has_lba48(dev->id))
Albert Lee8bf62ece2005-05-12 15:29:42 -04001055 dev->flags |= ATA_DFLAG_LBA48;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001056
1057 /* print device info to dmesg */
1058 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors:%s\n",
1059 ap->id, device,
1060 major_version,
1061 ata_mode_string(xfer_modes),
1062 (unsigned long long)dev->n_sectors,
1063 dev->flags & ATA_DFLAG_LBA48 ? " LBA48" : " LBA");
1064 } else {
1065 /* CHS */
1066
1067 /* Default translation */
1068 dev->cylinders = dev->id[1];
1069 dev->heads = dev->id[3];
1070 dev->sectors = dev->id[6];
Albert Lee8bf62ece2005-05-12 15:29:42 -04001071
1072 if (ata_id_current_chs_valid(dev->id)) {
1073 /* Current CHS translation is valid. */
1074 dev->cylinders = dev->id[54];
1075 dev->heads = dev->id[55];
1076 dev->sectors = dev->id[56];
Albert Lee8bf62ece2005-05-12 15:29:42 -04001077 }
1078
1079 /* print device info to dmesg */
1080 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors: CHS %d/%d/%d\n",
1081 ap->id, device,
1082 major_version,
1083 ata_mode_string(xfer_modes),
1084 (unsigned long long)dev->n_sectors,
1085 (int)dev->cylinders, (int)dev->heads, (int)dev->sectors);
1086
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 }
1088
1089 ap->host->max_cmd_len = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 }
1091
1092 /* ATAPI-specific feature tests */
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05001093 else if (dev->class == ATA_DEV_ATAPI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 if (ata_id_is_ata(dev->id)) /* sanity check */
1095 goto err_out_nosup;
1096
1097 rc = atapi_cdb_len(dev->id);
1098 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
1099 printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
1100 goto err_out_nosup;
1101 }
1102 ap->cdb_len = (unsigned int) rc;
1103 ap->host->max_cmd_len = (unsigned char) ap->cdb_len;
1104
1105 /* print device info to dmesg */
1106 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
1107 ap->id, device,
1108 ata_mode_string(xfer_modes));
1109 }
1110
1111 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
1112 return;
1113
1114err_out_nosup:
1115 printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n",
1116 ap->id, device);
1117err_out:
1118 dev->class++; /* converts ATA_DEV_xxx into ATA_DEV_xxx_UNSUP */
1119 DPRINTK("EXIT, err\n");
1120}
1121
Brad Campbell6f2f3812005-05-12 15:07:47 -04001122
Jeff Garzik057ace52005-10-22 14:27:05 -04001123static inline u8 ata_dev_knobble(const struct ata_port *ap)
Brad Campbell6f2f3812005-05-12 15:07:47 -04001124{
1125 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(ap->device->id)));
1126}
1127
1128/**
Randy Dunlapc893a3a2006-01-28 13:15:32 -05001129 * ata_dev_config - Run device specific handlers & check for SATA->PATA bridges
1130 * @ap: Bus
1131 * @i: Device
Brad Campbell6f2f3812005-05-12 15:07:47 -04001132 *
Randy Dunlapc893a3a2006-01-28 13:15:32 -05001133 * LOCKING:
Brad Campbell6f2f3812005-05-12 15:07:47 -04001134 */
Jeff Garzik8a60a072005-07-31 13:13:24 -04001135
Brad Campbell6f2f3812005-05-12 15:07:47 -04001136void ata_dev_config(struct ata_port *ap, unsigned int i)
1137{
1138 /* limit bridge transfers to udma5, 200 sectors */
1139 if (ata_dev_knobble(ap)) {
1140 printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
1141 ap->id, ap->device->devno);
1142 ap->udma_mask &= ATA_UDMA5;
1143 ap->host->max_sectors = ATA_MAX_SECTORS;
1144 ap->host->hostt->max_sectors = ATA_MAX_SECTORS;
Alan Cox9d824d02006-01-17 20:51:55 +00001145 ap->device[i].flags |= ATA_DFLAG_LOCK_SECTORS;
Brad Campbell6f2f3812005-05-12 15:07:47 -04001146 }
1147
1148 if (ap->ops->dev_config)
1149 ap->ops->dev_config(ap, &ap->device[i]);
1150}
1151
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152/**
1153 * ata_bus_probe - Reset and probe ATA bus
1154 * @ap: Bus to probe
1155 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001156 * Master ATA bus probing function. Initiates a hardware-dependent
1157 * bus reset, then attempts to identify any devices found on
1158 * the bus.
1159 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001161 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 *
1163 * RETURNS:
1164 * Zero on success, non-zero on error.
1165 */
1166
1167static int ata_bus_probe(struct ata_port *ap)
1168{
1169 unsigned int i, found = 0;
1170
Tejun Heoc19ba8a2006-01-24 17:05:22 +09001171 if (ap->ops->probe_reset) {
1172 unsigned int classes[ATA_MAX_DEVICES];
1173 int rc;
1174
1175 ata_port_probe(ap);
1176
1177 rc = ap->ops->probe_reset(ap, classes);
1178 if (rc == 0) {
Tejun Heo06ab7822006-02-12 15:01:49 +09001179 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1180 if (classes[i] == ATA_DEV_UNKNOWN)
1181 classes[i] = ATA_DEV_NONE;
Tejun Heoc19ba8a2006-01-24 17:05:22 +09001182 ap->device[i].class = classes[i];
Tejun Heo06ab7822006-02-12 15:01:49 +09001183 }
Tejun Heoc19ba8a2006-01-24 17:05:22 +09001184 } else {
1185 printk(KERN_ERR "ata%u: probe reset failed, "
1186 "disabling port\n", ap->id);
1187 ata_port_disable(ap);
1188 }
1189 } else
1190 ap->ops->phy_reset(ap);
1191
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1193 goto err_out;
1194
1195 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1196 ata_dev_identify(ap, i);
1197 if (ata_dev_present(&ap->device[i])) {
1198 found = 1;
Brad Campbell6f2f3812005-05-12 15:07:47 -04001199 ata_dev_config(ap,i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 }
1201 }
1202
1203 if ((!found) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1204 goto err_out_disable;
1205
1206 ata_set_mode(ap);
1207 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1208 goto err_out_disable;
1209
1210 return 0;
1211
1212err_out_disable:
1213 ap->ops->port_disable(ap);
1214err_out:
1215 return -1;
1216}
1217
1218/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001219 * ata_port_probe - Mark port as enabled
1220 * @ap: Port for which we indicate enablement
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001222 * Modify @ap data structure such that the system
1223 * thinks that the entire port is enabled.
1224 *
1225 * LOCKING: host_set lock, or some other form of
1226 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 */
1228
1229void ata_port_probe(struct ata_port *ap)
1230{
1231 ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1232}
1233
1234/**
Tejun Heo3be680b2005-12-19 22:35:02 +09001235 * sata_print_link_status - Print SATA link status
1236 * @ap: SATA port to printk link status about
1237 *
1238 * This function prints link speed and status of a SATA link.
1239 *
1240 * LOCKING:
1241 * None.
1242 */
1243static void sata_print_link_status(struct ata_port *ap)
1244{
1245 u32 sstatus, tmp;
1246 const char *speed;
1247
1248 if (!ap->ops->scr_read)
1249 return;
1250
1251 sstatus = scr_read(ap, SCR_STATUS);
1252
1253 if (sata_dev_present(ap)) {
1254 tmp = (sstatus >> 4) & 0xf;
1255 if (tmp & (1 << 0))
1256 speed = "1.5";
1257 else if (tmp & (1 << 1))
1258 speed = "3.0";
1259 else
1260 speed = "<unknown>";
1261 printk(KERN_INFO "ata%u: SATA link up %s Gbps (SStatus %X)\n",
1262 ap->id, speed, sstatus);
1263 } else {
1264 printk(KERN_INFO "ata%u: SATA link down (SStatus %X)\n",
1265 ap->id, sstatus);
1266 }
1267}
1268
1269/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001270 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1271 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001273 * This function issues commands to standard SATA Sxxx
1274 * PHY registers, to wake up the phy (and device), and
1275 * clear any reset condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 *
1277 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001278 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 *
1280 */
1281void __sata_phy_reset(struct ata_port *ap)
1282{
1283 u32 sstatus;
1284 unsigned long timeout = jiffies + (HZ * 5);
1285
1286 if (ap->flags & ATA_FLAG_SATA_RESET) {
Brett Russcdcca89e2005-03-28 15:10:27 -05001287 /* issue phy wake/reset */
1288 scr_write_flush(ap, SCR_CONTROL, 0x301);
Tejun Heo62ba2842005-06-26 23:27:19 +09001289 /* Couldn't find anything in SATA I/II specs, but
1290 * AHCI-1.1 10.4.2 says at least 1 ms. */
1291 mdelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 }
Brett Russcdcca89e2005-03-28 15:10:27 -05001293 scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
1295 /* wait for phy to become ready, if necessary */
1296 do {
1297 msleep(200);
1298 sstatus = scr_read(ap, SCR_STATUS);
1299 if ((sstatus & 0xf) != 1)
1300 break;
1301 } while (time_before(jiffies, timeout));
1302
Tejun Heo3be680b2005-12-19 22:35:02 +09001303 /* print link status */
1304 sata_print_link_status(ap);
Jeff Garzik656563e2005-11-20 03:36:45 -05001305
Tejun Heo3be680b2005-12-19 22:35:02 +09001306 /* TODO: phy layer with polling, timeouts, etc. */
1307 if (sata_dev_present(ap))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 ata_port_probe(ap);
Tejun Heo3be680b2005-12-19 22:35:02 +09001309 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 ata_port_disable(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311
1312 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1313 return;
1314
1315 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1316 ata_port_disable(ap);
1317 return;
1318 }
1319
1320 ap->cbl = ATA_CBL_SATA;
1321}
1322
1323/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001324 * sata_phy_reset - Reset SATA bus.
1325 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001327 * This function resets the SATA bus, and then probes
1328 * the bus for devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 *
1330 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001331 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 *
1333 */
1334void sata_phy_reset(struct ata_port *ap)
1335{
1336 __sata_phy_reset(ap);
1337 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1338 return;
1339 ata_bus_reset(ap);
1340}
1341
1342/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001343 * ata_port_disable - Disable port.
1344 * @ap: Port to be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001346 * Modify @ap data structure such that the system
1347 * thinks that the entire port is disabled, and should
1348 * never attempt to probe or communicate with devices
1349 * on this port.
1350 *
1351 * LOCKING: host_set lock, or some other form of
1352 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 */
1354
1355void ata_port_disable(struct ata_port *ap)
1356{
1357 ap->device[0].class = ATA_DEV_NONE;
1358 ap->device[1].class = ATA_DEV_NONE;
1359 ap->flags |= ATA_FLAG_PORT_DISABLED;
1360}
1361
Alan Cox452503f2005-10-21 19:01:32 -04001362/*
1363 * This mode timing computation functionality is ported over from
1364 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
1365 */
1366/*
1367 * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
1368 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
1369 * for PIO 5, which is a nonstandard extension and UDMA6, which
1370 * is currently supported only by Maxtor drives.
1371 */
1372
1373static const struct ata_timing ata_timing[] = {
1374
1375 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
1376 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
1377 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
1378 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
1379
1380 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
1381 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
1382 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
1383
1384/* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
1385
1386 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
1387 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
1388 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
1389
1390 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
1391 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
1392 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
1393
1394/* { XFER_PIO_5, 20, 50, 30, 100, 50, 30, 100, 0 }, */
1395 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
1396 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
1397
1398 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
1399 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
1400 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
1401
1402/* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
1403
1404 { 0xFF }
1405};
1406
1407#define ENOUGH(v,unit) (((v)-1)/(unit)+1)
1408#define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
1409
1410static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
1411{
1412 q->setup = EZ(t->setup * 1000, T);
1413 q->act8b = EZ(t->act8b * 1000, T);
1414 q->rec8b = EZ(t->rec8b * 1000, T);
1415 q->cyc8b = EZ(t->cyc8b * 1000, T);
1416 q->active = EZ(t->active * 1000, T);
1417 q->recover = EZ(t->recover * 1000, T);
1418 q->cycle = EZ(t->cycle * 1000, T);
1419 q->udma = EZ(t->udma * 1000, UT);
1420}
1421
1422void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
1423 struct ata_timing *m, unsigned int what)
1424{
1425 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
1426 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
1427 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
1428 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
1429 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
1430 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
1431 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
1432 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
1433}
1434
1435static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
1436{
1437 const struct ata_timing *t;
1438
1439 for (t = ata_timing; t->mode != speed; t++)
Alan Cox91190752005-10-26 12:17:46 -04001440 if (t->mode == 0xFF)
Alan Cox452503f2005-10-21 19:01:32 -04001441 return NULL;
1442 return t;
1443}
1444
1445int ata_timing_compute(struct ata_device *adev, unsigned short speed,
1446 struct ata_timing *t, int T, int UT)
1447{
1448 const struct ata_timing *s;
1449 struct ata_timing p;
1450
1451 /*
1452 * Find the mode.
Albert Lee75b1f2f2005-11-16 17:06:18 +08001453 */
Alan Cox452503f2005-10-21 19:01:32 -04001454
1455 if (!(s = ata_timing_find_mode(speed)))
1456 return -EINVAL;
1457
Albert Lee75b1f2f2005-11-16 17:06:18 +08001458 memcpy(t, s, sizeof(*s));
1459
Alan Cox452503f2005-10-21 19:01:32 -04001460 /*
1461 * If the drive is an EIDE drive, it can tell us it needs extended
1462 * PIO/MW_DMA cycle timing.
1463 */
1464
1465 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
1466 memset(&p, 0, sizeof(p));
1467 if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
1468 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
1469 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
1470 } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
1471 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
1472 }
1473 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
1474 }
1475
1476 /*
1477 * Convert the timing to bus clock counts.
1478 */
1479
Albert Lee75b1f2f2005-11-16 17:06:18 +08001480 ata_timing_quantize(t, t, T, UT);
Alan Cox452503f2005-10-21 19:01:32 -04001481
1482 /*
Randy Dunlapc893a3a2006-01-28 13:15:32 -05001483 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
1484 * S.M.A.R.T * and some other commands. We have to ensure that the
1485 * DMA cycle timing is slower/equal than the fastest PIO timing.
Alan Cox452503f2005-10-21 19:01:32 -04001486 */
1487
1488 if (speed > XFER_PIO_4) {
1489 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
1490 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
1491 }
1492
1493 /*
Randy Dunlapc893a3a2006-01-28 13:15:32 -05001494 * Lengthen active & recovery time so that cycle time is correct.
Alan Cox452503f2005-10-21 19:01:32 -04001495 */
1496
1497 if (t->act8b + t->rec8b < t->cyc8b) {
1498 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
1499 t->rec8b = t->cyc8b - t->act8b;
1500 }
1501
1502 if (t->active + t->recover < t->cycle) {
1503 t->active += (t->cycle - (t->active + t->recover)) / 2;
1504 t->recover = t->cycle - t->active;
1505 }
1506
1507 return 0;
1508}
1509
Jeff Garzik057ace52005-10-22 14:27:05 -04001510static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 unsigned int shift;
1512 u8 base;
1513} xfer_mode_classes[] = {
1514 { ATA_SHIFT_UDMA, XFER_UDMA_0 },
1515 { ATA_SHIFT_MWDMA, XFER_MW_DMA_0 },
1516 { ATA_SHIFT_PIO, XFER_PIO_0 },
1517};
1518
Arjan van de Ven858119e2006-01-14 13:20:43 -08001519static u8 base_from_shift(unsigned int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520{
1521 int i;
1522
1523 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++)
1524 if (xfer_mode_classes[i].shift == shift)
1525 return xfer_mode_classes[i].base;
1526
1527 return 0xff;
1528}
1529
1530static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
1531{
1532 int ofs, idx;
1533 u8 base;
1534
1535 if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1536 return;
1537
1538 if (dev->xfer_shift == ATA_SHIFT_PIO)
1539 dev->flags |= ATA_DFLAG_PIO;
1540
1541 ata_dev_set_xfermode(ap, dev);
1542
1543 base = base_from_shift(dev->xfer_shift);
1544 ofs = dev->xfer_mode - base;
1545 idx = ofs + dev->xfer_shift;
1546 WARN_ON(idx >= ARRAY_SIZE(xfer_mode_str));
1547
1548 DPRINTK("idx=%d xfer_shift=%u, xfer_mode=0x%x, base=0x%x, offset=%d\n",
1549 idx, dev->xfer_shift, (int)dev->xfer_mode, (int)base, ofs);
1550
1551 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
1552 ap->id, dev->devno, xfer_mode_str[idx]);
1553}
1554
1555static int ata_host_set_pio(struct ata_port *ap)
1556{
1557 unsigned int mask;
1558 int x, i;
1559 u8 base, xfer_mode;
1560
1561 mask = ata_get_mode_mask(ap, ATA_SHIFT_PIO);
1562 x = fgb(mask);
1563 if (x < 0) {
1564 printk(KERN_WARNING "ata%u: no PIO support\n", ap->id);
1565 return -1;
1566 }
1567
1568 base = base_from_shift(ATA_SHIFT_PIO);
1569 xfer_mode = base + x;
1570
1571 DPRINTK("base 0x%x xfer_mode 0x%x mask 0x%x x %d\n",
1572 (int)base, (int)xfer_mode, mask, x);
1573
1574 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1575 struct ata_device *dev = &ap->device[i];
1576 if (ata_dev_present(dev)) {
1577 dev->pio_mode = xfer_mode;
1578 dev->xfer_mode = xfer_mode;
1579 dev->xfer_shift = ATA_SHIFT_PIO;
1580 if (ap->ops->set_piomode)
1581 ap->ops->set_piomode(ap, dev);
1582 }
1583 }
1584
1585 return 0;
1586}
1587
1588static void ata_host_set_dma(struct ata_port *ap, u8 xfer_mode,
1589 unsigned int xfer_shift)
1590{
1591 int i;
1592
1593 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1594 struct ata_device *dev = &ap->device[i];
1595 if (ata_dev_present(dev)) {
1596 dev->dma_mode = xfer_mode;
1597 dev->xfer_mode = xfer_mode;
1598 dev->xfer_shift = xfer_shift;
1599 if (ap->ops->set_dmamode)
1600 ap->ops->set_dmamode(ap, dev);
1601 }
1602 }
1603}
1604
1605/**
1606 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1607 * @ap: port on which timings will be programmed
1608 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001609 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1610 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001612 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 */
1614static void ata_set_mode(struct ata_port *ap)
1615{
Albert Lee8cbd6df2005-10-12 15:06:27 +08001616 unsigned int xfer_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 u8 xfer_mode;
1618 int rc;
1619
1620 /* step 1: always set host PIO timings */
1621 rc = ata_host_set_pio(ap);
1622 if (rc)
1623 goto err_out;
1624
1625 /* step 2: choose the best data xfer mode */
1626 xfer_mode = xfer_shift = 0;
1627 rc = ata_choose_xfer_mode(ap, &xfer_mode, &xfer_shift);
1628 if (rc)
1629 goto err_out;
1630
1631 /* step 3: if that xfer mode isn't PIO, set host DMA timings */
1632 if (xfer_shift != ATA_SHIFT_PIO)
1633 ata_host_set_dma(ap, xfer_mode, xfer_shift);
1634
1635 /* step 4: update devices' xfer mode */
1636 ata_dev_set_mode(ap, &ap->device[0]);
1637 ata_dev_set_mode(ap, &ap->device[1]);
1638
1639 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1640 return;
1641
1642 if (ap->ops->post_set_mode)
1643 ap->ops->post_set_mode(ap);
1644
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 return;
1646
1647err_out:
1648 ata_port_disable(ap);
1649}
1650
1651/**
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05001652 * ata_tf_to_host - issue ATA taskfile to host controller
1653 * @ap: port to which command is being issued
1654 * @tf: ATA taskfile register set
1655 *
1656 * Issues ATA taskfile register set to ATA host controller,
1657 * with proper synchronization with interrupt handler and
1658 * other threads.
1659 *
1660 * LOCKING:
1661 * spin_lock_irqsave(host_set lock)
1662 */
1663
1664static inline void ata_tf_to_host(struct ata_port *ap,
1665 const struct ata_taskfile *tf)
1666{
1667 ap->ops->tf_load(ap, tf);
1668 ap->ops->exec_command(ap, tf);
1669}
1670
1671/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 * ata_busy_sleep - sleep until BSY clears, or timeout
1673 * @ap: port containing status register to be polled
1674 * @tmout_pat: impatience timeout
1675 * @tmout: overall timeout
1676 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001677 * Sleep until ATA Status register bit BSY clears,
1678 * or a timeout occurs.
1679 *
1680 * LOCKING: None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 */
1682
Tejun Heo6f8b9952006-01-24 17:05:21 +09001683unsigned int ata_busy_sleep (struct ata_port *ap,
1684 unsigned long tmout_pat, unsigned long tmout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685{
1686 unsigned long timer_start, timeout;
1687 u8 status;
1688
1689 status = ata_busy_wait(ap, ATA_BUSY, 300);
1690 timer_start = jiffies;
1691 timeout = timer_start + tmout_pat;
1692 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1693 msleep(50);
1694 status = ata_busy_wait(ap, ATA_BUSY, 3);
1695 }
1696
1697 if (status & ATA_BUSY)
1698 printk(KERN_WARNING "ata%u is slow to respond, "
1699 "please be patient\n", ap->id);
1700
1701 timeout = timer_start + tmout;
1702 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1703 msleep(50);
1704 status = ata_chk_status(ap);
1705 }
1706
1707 if (status & ATA_BUSY) {
1708 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
1709 ap->id, tmout / HZ);
1710 return 1;
1711 }
1712
1713 return 0;
1714}
1715
1716static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
1717{
1718 struct ata_ioports *ioaddr = &ap->ioaddr;
1719 unsigned int dev0 = devmask & (1 << 0);
1720 unsigned int dev1 = devmask & (1 << 1);
1721 unsigned long timeout;
1722
1723 /* if device 0 was found in ata_devchk, wait for its
1724 * BSY bit to clear
1725 */
1726 if (dev0)
1727 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1728
1729 /* if device 1 was found in ata_devchk, wait for
1730 * register access, then wait for BSY to clear
1731 */
1732 timeout = jiffies + ATA_TMOUT_BOOT;
1733 while (dev1) {
1734 u8 nsect, lbal;
1735
1736 ap->ops->dev_select(ap, 1);
1737 if (ap->flags & ATA_FLAG_MMIO) {
1738 nsect = readb((void __iomem *) ioaddr->nsect_addr);
1739 lbal = readb((void __iomem *) ioaddr->lbal_addr);
1740 } else {
1741 nsect = inb(ioaddr->nsect_addr);
1742 lbal = inb(ioaddr->lbal_addr);
1743 }
1744 if ((nsect == 1) && (lbal == 1))
1745 break;
1746 if (time_after(jiffies, timeout)) {
1747 dev1 = 0;
1748 break;
1749 }
1750 msleep(50); /* give drive a breather */
1751 }
1752 if (dev1)
1753 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1754
1755 /* is all this really necessary? */
1756 ap->ops->dev_select(ap, 0);
1757 if (dev1)
1758 ap->ops->dev_select(ap, 1);
1759 if (dev0)
1760 ap->ops->dev_select(ap, 0);
1761}
1762
1763/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001764 * ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
1765 * @ap: Port to reset and probe
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001767 * Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
1768 * probe the bus. Not often used these days.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 *
1770 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001771 * PCI/etc. bus probe sem.
Jeff Garzike5338252005-10-30 21:37:17 -05001772 * Obtains host_set lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 *
1774 */
1775
1776static unsigned int ata_bus_edd(struct ata_port *ap)
1777{
1778 struct ata_taskfile tf;
Jeff Garzike5338252005-10-30 21:37:17 -05001779 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
1781 /* set up execute-device-diag (bus reset) taskfile */
1782 /* also, take interrupts to a known state (disabled) */
1783 DPRINTK("execute-device-diag\n");
1784 ata_tf_init(ap, &tf, 0);
1785 tf.ctl |= ATA_NIEN;
1786 tf.command = ATA_CMD_EDD;
1787 tf.protocol = ATA_PROT_NODATA;
1788
1789 /* do bus reset */
Jeff Garzike5338252005-10-30 21:37:17 -05001790 spin_lock_irqsave(&ap->host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 ata_tf_to_host(ap, &tf);
Jeff Garzike5338252005-10-30 21:37:17 -05001792 spin_unlock_irqrestore(&ap->host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
1794 /* spec says at least 2ms. but who knows with those
1795 * crazy ATAPI devices...
1796 */
1797 msleep(150);
1798
1799 return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1800}
1801
1802static unsigned int ata_bus_softreset(struct ata_port *ap,
1803 unsigned int devmask)
1804{
1805 struct ata_ioports *ioaddr = &ap->ioaddr;
1806
1807 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
1808
1809 /* software reset. causes dev0 to be selected */
1810 if (ap->flags & ATA_FLAG_MMIO) {
1811 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1812 udelay(20); /* FIXME: flush */
1813 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
1814 udelay(20); /* FIXME: flush */
1815 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1816 } else {
1817 outb(ap->ctl, ioaddr->ctl_addr);
1818 udelay(10);
1819 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
1820 udelay(10);
1821 outb(ap->ctl, ioaddr->ctl_addr);
1822 }
1823
1824 /* spec mandates ">= 2ms" before checking status.
1825 * We wait 150ms, because that was the magic delay used for
1826 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
1827 * between when the ATA command register is written, and then
1828 * status is checked. Because waiting for "a while" before
1829 * checking status is fine, post SRST, we perform this magic
1830 * delay here as well.
1831 */
1832 msleep(150);
1833
1834 ata_bus_post_reset(ap, devmask);
1835
1836 return 0;
1837}
1838
1839/**
1840 * ata_bus_reset - reset host port and associated ATA channel
1841 * @ap: port to reset
1842 *
1843 * This is typically the first time we actually start issuing
1844 * commands to the ATA channel. We wait for BSY to clear, then
1845 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
1846 * result. Determine what devices, if any, are on the channel
1847 * by looking at the device 0/1 error register. Look at the signature
1848 * stored in each device's taskfile registers, to determine if
1849 * the device is ATA or ATAPI.
1850 *
1851 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001852 * PCI/etc. bus probe sem.
1853 * Obtains host_set lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 *
1855 * SIDE EFFECTS:
1856 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
1857 */
1858
1859void ata_bus_reset(struct ata_port *ap)
1860{
1861 struct ata_ioports *ioaddr = &ap->ioaddr;
1862 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
1863 u8 err;
1864 unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
1865
1866 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
1867
1868 /* determine if device 0/1 are present */
1869 if (ap->flags & ATA_FLAG_SATA_RESET)
1870 dev0 = 1;
1871 else {
1872 dev0 = ata_devchk(ap, 0);
1873 if (slave_possible)
1874 dev1 = ata_devchk(ap, 1);
1875 }
1876
1877 if (dev0)
1878 devmask |= (1 << 0);
1879 if (dev1)
1880 devmask |= (1 << 1);
1881
1882 /* select device 0 again */
1883 ap->ops->dev_select(ap, 0);
1884
1885 /* issue bus reset */
1886 if (ap->flags & ATA_FLAG_SRST)
1887 rc = ata_bus_softreset(ap, devmask);
1888 else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
1889 /* set up device control */
1890 if (ap->flags & ATA_FLAG_MMIO)
1891 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1892 else
1893 outb(ap->ctl, ioaddr->ctl_addr);
1894 rc = ata_bus_edd(ap);
1895 }
1896
1897 if (rc)
1898 goto err_out;
1899
1900 /*
1901 * determine by signature whether we have ATA or ATAPI devices
1902 */
Tejun Heob4dc7622006-01-24 17:05:22 +09001903 ap->device[0].class = ata_dev_try_classify(ap, 0, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 if ((slave_possible) && (err != 0x81))
Tejun Heob4dc7622006-01-24 17:05:22 +09001905 ap->device[1].class = ata_dev_try_classify(ap, 1, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906
1907 /* re-enable interrupts */
1908 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
1909 ata_irq_on(ap);
1910
1911 /* is double-select really necessary? */
1912 if (ap->device[1].class != ATA_DEV_NONE)
1913 ap->ops->dev_select(ap, 1);
1914 if (ap->device[0].class != ATA_DEV_NONE)
1915 ap->ops->dev_select(ap, 0);
1916
1917 /* if no devices were detected, disable this port */
1918 if ((ap->device[0].class == ATA_DEV_NONE) &&
1919 (ap->device[1].class == ATA_DEV_NONE))
1920 goto err_out;
1921
1922 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
1923 /* set up device control for ATA_FLAG_SATA_RESET */
1924 if (ap->flags & ATA_FLAG_MMIO)
1925 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1926 else
1927 outb(ap->ctl, ioaddr->ctl_addr);
1928 }
1929
1930 DPRINTK("EXIT\n");
1931 return;
1932
1933err_out:
1934 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
1935 ap->ops->port_disable(ap);
1936
1937 DPRINTK("EXIT\n");
1938}
1939
Tejun Heo7a7921e2006-02-02 18:20:00 +09001940static int sata_phy_resume(struct ata_port *ap)
1941{
1942 unsigned long timeout = jiffies + (HZ * 5);
1943 u32 sstatus;
1944
1945 scr_write_flush(ap, SCR_CONTROL, 0x300);
1946
1947 /* Wait for phy to become ready, if necessary. */
1948 do {
1949 msleep(200);
1950 sstatus = scr_read(ap, SCR_STATUS);
1951 if ((sstatus & 0xf) != 1)
1952 return 0;
1953 } while (time_before(jiffies, timeout));
1954
1955 return -1;
1956}
1957
Tejun Heoc2bd5802006-01-24 17:05:22 +09001958/**
Tejun Heo8a19ac82006-02-02 18:20:00 +09001959 * ata_std_probeinit - initialize probing
1960 * @ap: port to be probed
1961 *
1962 * @ap is about to be probed. Initialize it. This function is
1963 * to be used as standard callback for ata_drive_probe_reset().
Tejun Heo3a397462006-02-10 23:58:48 +09001964 *
1965 * NOTE!!! Do not use this function as probeinit if a low level
1966 * driver implements only hardreset. Just pass NULL as probeinit
1967 * in that case. Using this function is probably okay but doing
1968 * so makes reset sequence different from the original
1969 * ->phy_reset implementation and Jeff nervous. :-P
Tejun Heo8a19ac82006-02-02 18:20:00 +09001970 */
1971extern void ata_std_probeinit(struct ata_port *ap)
1972{
Tejun Heo3a397462006-02-10 23:58:48 +09001973 if (ap->flags & ATA_FLAG_SATA && ap->ops->scr_read) {
Tejun Heo8a19ac82006-02-02 18:20:00 +09001974 sata_phy_resume(ap);
Tejun Heo3a397462006-02-10 23:58:48 +09001975 if (sata_dev_present(ap))
1976 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1977 }
Tejun Heo8a19ac82006-02-02 18:20:00 +09001978}
1979
1980/**
Tejun Heoc2bd5802006-01-24 17:05:22 +09001981 * ata_std_softreset - reset host port via ATA SRST
1982 * @ap: port to reset
1983 * @verbose: fail verbosely
1984 * @classes: resulting classes of attached devices
1985 *
1986 * Reset host port using ATA SRST. This function is to be used
1987 * as standard callback for ata_drive_*_reset() functions.
1988 *
1989 * LOCKING:
1990 * Kernel thread context (may sleep)
1991 *
1992 * RETURNS:
1993 * 0 on success, -errno otherwise.
1994 */
1995int ata_std_softreset(struct ata_port *ap, int verbose, unsigned int *classes)
1996{
1997 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
1998 unsigned int devmask = 0, err_mask;
1999 u8 err;
2000
2001 DPRINTK("ENTER\n");
2002
Tejun Heo3a397462006-02-10 23:58:48 +09002003 if (ap->ops->scr_read && !sata_dev_present(ap)) {
2004 classes[0] = ATA_DEV_NONE;
2005 goto out;
2006 }
2007
Tejun Heoc2bd5802006-01-24 17:05:22 +09002008 /* determine if device 0/1 are present */
2009 if (ata_devchk(ap, 0))
2010 devmask |= (1 << 0);
2011 if (slave_possible && ata_devchk(ap, 1))
2012 devmask |= (1 << 1);
2013
Tejun Heoc2bd5802006-01-24 17:05:22 +09002014 /* select device 0 again */
2015 ap->ops->dev_select(ap, 0);
2016
2017 /* issue bus reset */
2018 DPRINTK("about to softreset, devmask=%x\n", devmask);
2019 err_mask = ata_bus_softreset(ap, devmask);
2020 if (err_mask) {
2021 if (verbose)
2022 printk(KERN_ERR "ata%u: SRST failed (err_mask=0x%x)\n",
2023 ap->id, err_mask);
2024 else
2025 DPRINTK("EXIT, softreset failed (err_mask=0x%x)\n",
2026 err_mask);
2027 return -EIO;
2028 }
2029
2030 /* determine by signature whether we have ATA or ATAPI devices */
2031 classes[0] = ata_dev_try_classify(ap, 0, &err);
2032 if (slave_possible && err != 0x81)
2033 classes[1] = ata_dev_try_classify(ap, 1, &err);
2034
Tejun Heo3a397462006-02-10 23:58:48 +09002035 out:
Tejun Heoc2bd5802006-01-24 17:05:22 +09002036 DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]);
2037 return 0;
2038}
2039
2040/**
2041 * sata_std_hardreset - reset host port via SATA phy reset
2042 * @ap: port to reset
2043 * @verbose: fail verbosely
2044 * @class: resulting class of attached device
2045 *
2046 * SATA phy-reset host port using DET bits of SControl register.
2047 * This function is to be used as standard callback for
2048 * ata_drive_*_reset().
2049 *
2050 * LOCKING:
2051 * Kernel thread context (may sleep)
2052 *
2053 * RETURNS:
2054 * 0 on success, -errno otherwise.
2055 */
2056int sata_std_hardreset(struct ata_port *ap, int verbose, unsigned int *class)
2057{
Tejun Heoc2bd5802006-01-24 17:05:22 +09002058 DPRINTK("ENTER\n");
2059
2060 /* Issue phy wake/reset */
2061 scr_write_flush(ap, SCR_CONTROL, 0x301);
2062
2063 /*
2064 * Couldn't find anything in SATA I/II specs, but AHCI-1.1
2065 * 10.4.2 says at least 1 ms.
2066 */
2067 msleep(1);
2068
Tejun Heo7a7921e2006-02-02 18:20:00 +09002069 /* Bring phy back */
2070 sata_phy_resume(ap);
Tejun Heoc2bd5802006-01-24 17:05:22 +09002071
Tejun Heoc2bd5802006-01-24 17:05:22 +09002072 /* TODO: phy layer with polling, timeouts, etc. */
2073 if (!sata_dev_present(ap)) {
2074 *class = ATA_DEV_NONE;
2075 DPRINTK("EXIT, link offline\n");
2076 return 0;
2077 }
2078
2079 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
2080 if (verbose)
2081 printk(KERN_ERR "ata%u: COMRESET failed "
2082 "(device not ready)\n", ap->id);
2083 else
2084 DPRINTK("EXIT, device not ready\n");
2085 return -EIO;
2086 }
2087
Tejun Heo3a397462006-02-10 23:58:48 +09002088 ap->ops->dev_select(ap, 0); /* probably unnecessary */
2089
Tejun Heoc2bd5802006-01-24 17:05:22 +09002090 *class = ata_dev_try_classify(ap, 0, NULL);
2091
2092 DPRINTK("EXIT, class=%u\n", *class);
2093 return 0;
2094}
2095
2096/**
2097 * ata_std_postreset - standard postreset callback
2098 * @ap: the target ata_port
2099 * @classes: classes of attached devices
2100 *
2101 * This function is invoked after a successful reset. Note that
2102 * the device might have been reset more than once using
2103 * different reset methods before postreset is invoked.
2104 * postreset is also reponsible for setting cable type.
2105 *
2106 * This function is to be used as standard callback for
2107 * ata_drive_*_reset().
2108 *
2109 * LOCKING:
2110 * Kernel thread context (may sleep)
2111 */
2112void ata_std_postreset(struct ata_port *ap, unsigned int *classes)
2113{
2114 DPRINTK("ENTER\n");
2115
2116 /* set cable type */
2117 if (ap->cbl == ATA_CBL_NONE && ap->flags & ATA_FLAG_SATA)
2118 ap->cbl = ATA_CBL_SATA;
2119
2120 /* print link status */
2121 if (ap->cbl == ATA_CBL_SATA)
2122 sata_print_link_status(ap);
2123
Tejun Heo3a397462006-02-10 23:58:48 +09002124 /* re-enable interrupts */
2125 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2126 ata_irq_on(ap);
Tejun Heoc2bd5802006-01-24 17:05:22 +09002127
2128 /* is double-select really necessary? */
2129 if (classes[0] != ATA_DEV_NONE)
2130 ap->ops->dev_select(ap, 1);
2131 if (classes[1] != ATA_DEV_NONE)
2132 ap->ops->dev_select(ap, 0);
2133
Tejun Heo3a397462006-02-10 23:58:48 +09002134 /* bail out if no device is present */
2135 if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
2136 DPRINTK("EXIT, no device\n");
2137 return;
2138 }
2139
2140 /* set up device control */
2141 if (ap->ioaddr.ctl_addr) {
2142 if (ap->flags & ATA_FLAG_MMIO)
2143 writeb(ap->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
2144 else
2145 outb(ap->ctl, ap->ioaddr.ctl_addr);
2146 }
Tejun Heoc2bd5802006-01-24 17:05:22 +09002147
2148 DPRINTK("EXIT\n");
2149}
2150
2151/**
2152 * ata_std_probe_reset - standard probe reset method
2153 * @ap: prot to perform probe-reset
2154 * @classes: resulting classes of attached devices
2155 *
2156 * The stock off-the-shelf ->probe_reset method.
2157 *
2158 * LOCKING:
2159 * Kernel thread context (may sleep)
2160 *
2161 * RETURNS:
2162 * 0 on success, -errno otherwise.
2163 */
2164int ata_std_probe_reset(struct ata_port *ap, unsigned int *classes)
2165{
2166 ata_reset_fn_t hardreset;
2167
2168 hardreset = NULL;
Tejun Heob911fc32006-02-02 18:20:00 +09002169 if (ap->flags & ATA_FLAG_SATA && ap->ops->scr_read)
Tejun Heoc2bd5802006-01-24 17:05:22 +09002170 hardreset = sata_std_hardreset;
2171
Tejun Heo8a19ac82006-02-02 18:20:00 +09002172 return ata_drive_probe_reset(ap, ata_std_probeinit,
Tejun Heo7944ea92006-02-02 18:20:00 +09002173 ata_std_softreset, hardreset,
Tejun Heoc2bd5802006-01-24 17:05:22 +09002174 ata_std_postreset, classes);
2175}
2176
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002177static int do_probe_reset(struct ata_port *ap, ata_reset_fn_t reset,
2178 ata_postreset_fn_t postreset,
2179 unsigned int *classes)
2180{
2181 int i, rc;
2182
2183 for (i = 0; i < ATA_MAX_DEVICES; i++)
2184 classes[i] = ATA_DEV_UNKNOWN;
2185
2186 rc = reset(ap, 0, classes);
2187 if (rc)
2188 return rc;
2189
2190 /* If any class isn't ATA_DEV_UNKNOWN, consider classification
2191 * is complete and convert all ATA_DEV_UNKNOWN to
2192 * ATA_DEV_NONE.
2193 */
2194 for (i = 0; i < ATA_MAX_DEVICES; i++)
2195 if (classes[i] != ATA_DEV_UNKNOWN)
2196 break;
2197
2198 if (i < ATA_MAX_DEVICES)
2199 for (i = 0; i < ATA_MAX_DEVICES; i++)
2200 if (classes[i] == ATA_DEV_UNKNOWN)
2201 classes[i] = ATA_DEV_NONE;
2202
2203 if (postreset)
2204 postreset(ap, classes);
2205
2206 return classes[0] != ATA_DEV_UNKNOWN ? 0 : -ENODEV;
2207}
2208
2209/**
2210 * ata_drive_probe_reset - Perform probe reset with given methods
2211 * @ap: port to reset
Tejun Heo7944ea92006-02-02 18:20:00 +09002212 * @probeinit: probeinit method (can be NULL)
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002213 * @softreset: softreset method (can be NULL)
2214 * @hardreset: hardreset method (can be NULL)
2215 * @postreset: postreset method (can be NULL)
2216 * @classes: resulting classes of attached devices
2217 *
2218 * Reset the specified port and classify attached devices using
2219 * given methods. This function prefers softreset but tries all
2220 * possible reset sequences to reset and classify devices. This
2221 * function is intended to be used for constructing ->probe_reset
2222 * callback by low level drivers.
2223 *
2224 * Reset methods should follow the following rules.
2225 *
2226 * - Return 0 on sucess, -errno on failure.
2227 * - If classification is supported, fill classes[] with
2228 * recognized class codes.
2229 * - If classification is not supported, leave classes[] alone.
2230 * - If verbose is non-zero, print error message on failure;
2231 * otherwise, shut up.
2232 *
2233 * LOCKING:
2234 * Kernel thread context (may sleep)
2235 *
2236 * RETURNS:
2237 * 0 on success, -EINVAL if no reset method is avaliable, -ENODEV
2238 * if classification fails, and any error code from reset
2239 * methods.
2240 */
Tejun Heo7944ea92006-02-02 18:20:00 +09002241int ata_drive_probe_reset(struct ata_port *ap, ata_probeinit_fn_t probeinit,
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002242 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2243 ata_postreset_fn_t postreset, unsigned int *classes)
2244{
2245 int rc = -EINVAL;
2246
Tejun Heo7944ea92006-02-02 18:20:00 +09002247 if (probeinit)
2248 probeinit(ap);
2249
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002250 if (softreset) {
2251 rc = do_probe_reset(ap, softreset, postreset, classes);
2252 if (rc == 0)
2253 return 0;
2254 }
2255
2256 if (!hardreset)
2257 return rc;
2258
2259 rc = do_probe_reset(ap, hardreset, postreset, classes);
2260 if (rc == 0 || rc != -ENODEV)
2261 return rc;
2262
2263 if (softreset)
2264 rc = do_probe_reset(ap, softreset, postreset, classes);
2265
2266 return rc;
2267}
2268
Jeff Garzik057ace52005-10-22 14:27:05 -04002269static void ata_pr_blacklisted(const struct ata_port *ap,
2270 const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271{
2272 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, disabling DMA\n",
2273 ap->id, dev->devno);
2274}
2275
Arjan van de Ven98ac62d2005-11-28 10:06:23 +01002276static const char * const ata_dma_blacklist [] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277 "WDC AC11000H",
2278 "WDC AC22100H",
2279 "WDC AC32500H",
2280 "WDC AC33100H",
2281 "WDC AC31600H",
2282 "WDC AC32100H",
2283 "WDC AC23200L",
2284 "Compaq CRD-8241B",
2285 "CRD-8400B",
2286 "CRD-8480B",
2287 "CRD-8482B",
2288 "CRD-84",
2289 "SanDisk SDP3B",
2290 "SanDisk SDP3B-64",
2291 "SANYO CD-ROM CRD",
2292 "HITACHI CDR-8",
2293 "HITACHI CDR-8335",
2294 "HITACHI CDR-8435",
2295 "Toshiba CD-ROM XM-6202B",
Jeff Garzike9222562005-06-28 00:03:37 -04002296 "TOSHIBA CD-ROM XM-1702BC",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 "CD-532E-A",
2298 "E-IDE CD-ROM CR-840",
2299 "CD-ROM Drive/F5A",
2300 "WPI CDD-820",
2301 "SAMSUNG CD-ROM SC-148C",
2302 "SAMSUNG CD-ROM SC",
2303 "SanDisk SDP3B-64",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 "ATAPI CD-ROM DRIVE 40X MAXIMUM",
2305 "_NEC DV5800A",
2306};
2307
Jeff Garzik057ace52005-10-22 14:27:05 -04002308static int ata_dma_blacklisted(const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309{
Tejun Heo2e026712006-02-12 22:47:04 +09002310 unsigned char model_num[41];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 int i;
2312
Tejun Heo2e026712006-02-12 22:47:04 +09002313 ata_dev_id_c_string(dev->id, model_num, ATA_ID_PROD_OFS,
2314 sizeof(model_num));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315
2316 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i++)
Tejun Heo2e026712006-02-12 22:47:04 +09002317 if (!strcmp(ata_dma_blacklist[i], model_num))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 return 1;
2319
2320 return 0;
2321}
2322
Jeff Garzik057ace52005-10-22 14:27:05 -04002323static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324{
Jeff Garzik057ace52005-10-22 14:27:05 -04002325 const struct ata_device *master, *slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326 unsigned int mask;
2327
2328 master = &ap->device[0];
2329 slave = &ap->device[1];
2330
Tejun Heoa46314742006-02-11 19:11:13 +09002331 WARN_ON(!ata_dev_present(master) && !ata_dev_present(slave));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332
2333 if (shift == ATA_SHIFT_UDMA) {
2334 mask = ap->udma_mask;
2335 if (ata_dev_present(master)) {
2336 mask &= (master->id[ATA_ID_UDMA_MODES] & 0xff);
Jeff Garzik057ace52005-10-22 14:27:05 -04002337 if (ata_dma_blacklisted(master)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 mask = 0;
2339 ata_pr_blacklisted(ap, master);
2340 }
2341 }
2342 if (ata_dev_present(slave)) {
2343 mask &= (slave->id[ATA_ID_UDMA_MODES] & 0xff);
Jeff Garzik057ace52005-10-22 14:27:05 -04002344 if (ata_dma_blacklisted(slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 mask = 0;
2346 ata_pr_blacklisted(ap, slave);
2347 }
2348 }
2349 }
2350 else if (shift == ATA_SHIFT_MWDMA) {
2351 mask = ap->mwdma_mask;
2352 if (ata_dev_present(master)) {
2353 mask &= (master->id[ATA_ID_MWDMA_MODES] & 0x07);
Jeff Garzik057ace52005-10-22 14:27:05 -04002354 if (ata_dma_blacklisted(master)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 mask = 0;
2356 ata_pr_blacklisted(ap, master);
2357 }
2358 }
2359 if (ata_dev_present(slave)) {
2360 mask &= (slave->id[ATA_ID_MWDMA_MODES] & 0x07);
Jeff Garzik057ace52005-10-22 14:27:05 -04002361 if (ata_dma_blacklisted(slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 mask = 0;
2363 ata_pr_blacklisted(ap, slave);
2364 }
2365 }
2366 }
2367 else if (shift == ATA_SHIFT_PIO) {
2368 mask = ap->pio_mask;
2369 if (ata_dev_present(master)) {
2370 /* spec doesn't return explicit support for
2371 * PIO0-2, so we fake it
2372 */
2373 u16 tmp_mode = master->id[ATA_ID_PIO_MODES] & 0x03;
2374 tmp_mode <<= 3;
2375 tmp_mode |= 0x7;
2376 mask &= tmp_mode;
2377 }
2378 if (ata_dev_present(slave)) {
2379 /* spec doesn't return explicit support for
2380 * PIO0-2, so we fake it
2381 */
2382 u16 tmp_mode = slave->id[ATA_ID_PIO_MODES] & 0x03;
2383 tmp_mode <<= 3;
2384 tmp_mode |= 0x7;
2385 mask &= tmp_mode;
2386 }
2387 }
2388 else {
2389 mask = 0xffffffff; /* shut up compiler warning */
2390 BUG();
2391 }
2392
2393 return mask;
2394}
2395
2396/* find greatest bit */
2397static int fgb(u32 bitmap)
2398{
2399 unsigned int i;
2400 int x = -1;
2401
2402 for (i = 0; i < 32; i++)
2403 if (bitmap & (1 << i))
2404 x = i;
2405
2406 return x;
2407}
2408
2409/**
2410 * ata_choose_xfer_mode - attempt to find best transfer mode
2411 * @ap: Port for which an xfer mode will be selected
2412 * @xfer_mode_out: (output) SET FEATURES - XFER MODE code
2413 * @xfer_shift_out: (output) bit shift that selects this mode
2414 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04002415 * Based on host and device capabilities, determine the
2416 * maximum transfer mode that is amenable to all.
2417 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002419 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 *
2421 * RETURNS:
2422 * Zero on success, negative on error.
2423 */
2424
Jeff Garzik057ace52005-10-22 14:27:05 -04002425static int ata_choose_xfer_mode(const struct ata_port *ap,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426 u8 *xfer_mode_out,
2427 unsigned int *xfer_shift_out)
2428{
2429 unsigned int mask, shift;
2430 int x, i;
2431
2432 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++) {
2433 shift = xfer_mode_classes[i].shift;
2434 mask = ata_get_mode_mask(ap, shift);
2435
2436 x = fgb(mask);
2437 if (x >= 0) {
2438 *xfer_mode_out = xfer_mode_classes[i].base + x;
2439 *xfer_shift_out = shift;
2440 return 0;
2441 }
2442 }
2443
2444 return -1;
2445}
2446
2447/**
2448 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2449 * @ap: Port associated with device @dev
2450 * @dev: Device to which command will be sent
2451 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002452 * Issue SET FEATURES - XFER MODE command to device @dev
2453 * on port @ap.
2454 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002456 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 */
2458
2459static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
2460{
Tejun Heoa0123702005-12-13 14:49:31 +09002461 struct ata_taskfile tf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462
2463 /* set up set-features taskfile */
2464 DPRINTK("set features - xfer mode\n");
2465
Tejun Heoa0123702005-12-13 14:49:31 +09002466 ata_tf_init(ap, &tf, dev->devno);
2467 tf.command = ATA_CMD_SET_FEATURES;
2468 tf.feature = SETFEATURES_XFER;
2469 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2470 tf.protocol = ATA_PROT_NODATA;
2471 tf.nsect = dev->xfer_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472
Tejun Heoa0123702005-12-13 14:49:31 +09002473 if (ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0)) {
2474 printk(KERN_ERR "ata%u: failed to set xfermode, disabled\n",
2475 ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 ata_port_disable(ap);
Tejun Heoa0123702005-12-13 14:49:31 +09002477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478
2479 DPRINTK("EXIT\n");
2480}
2481
2482/**
Albert Lee59a10b12005-10-12 15:09:42 +08002483 * ata_dev_reread_id - Reread the device identify device info
2484 * @ap: port where the device is
2485 * @dev: device to reread the identify device info
2486 *
2487 * LOCKING:
2488 */
2489
2490static void ata_dev_reread_id(struct ata_port *ap, struct ata_device *dev)
2491{
Tejun Heoa0123702005-12-13 14:49:31 +09002492 struct ata_taskfile tf;
Albert Lee59a10b12005-10-12 15:09:42 +08002493
Tejun Heoa0123702005-12-13 14:49:31 +09002494 ata_tf_init(ap, &tf, dev->devno);
Albert Lee59a10b12005-10-12 15:09:42 +08002495
2496 if (dev->class == ATA_DEV_ATA) {
Tejun Heoa0123702005-12-13 14:49:31 +09002497 tf.command = ATA_CMD_ID_ATA;
Albert Lee59a10b12005-10-12 15:09:42 +08002498 DPRINTK("do ATA identify\n");
2499 } else {
Tejun Heoa0123702005-12-13 14:49:31 +09002500 tf.command = ATA_CMD_ID_ATAPI;
Albert Lee59a10b12005-10-12 15:09:42 +08002501 DPRINTK("do ATAPI identify\n");
2502 }
2503
Tejun Heoa0123702005-12-13 14:49:31 +09002504 tf.flags |= ATA_TFLAG_DEVICE;
2505 tf.protocol = ATA_PROT_PIO;
Albert Lee59a10b12005-10-12 15:09:42 +08002506
Tejun Heoa0123702005-12-13 14:49:31 +09002507 if (ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE,
2508 dev->id, sizeof(dev->id)))
Albert Lee59a10b12005-10-12 15:09:42 +08002509 goto err_out;
2510
Albert Lee59a10b12005-10-12 15:09:42 +08002511 swap_buf_le16(dev->id, ATA_ID_WORDS);
2512
Tejun Heo0bd33002006-02-12 22:47:05 +09002513 ata_dump_id(dev->id);
Albert Lee59a10b12005-10-12 15:09:42 +08002514
2515 DPRINTK("EXIT\n");
2516
2517 return;
2518err_out:
Tejun Heoa0123702005-12-13 14:49:31 +09002519 printk(KERN_ERR "ata%u: failed to reread ID, disabled\n", ap->id);
Albert Lee59a10b12005-10-12 15:09:42 +08002520 ata_port_disable(ap);
2521}
2522
2523/**
Albert Lee8bf62ece2005-05-12 15:29:42 -04002524 * ata_dev_init_params - Issue INIT DEV PARAMS command
2525 * @ap: Port associated with device @dev
2526 * @dev: Device to which command will be sent
2527 *
2528 * LOCKING:
2529 */
2530
2531static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev)
2532{
Tejun Heoa0123702005-12-13 14:49:31 +09002533 struct ata_taskfile tf;
Albert Lee8bf62ece2005-05-12 15:29:42 -04002534 u16 sectors = dev->id[6];
2535 u16 heads = dev->id[3];
2536
2537 /* Number of sectors per track 1-255. Number of heads 1-16 */
2538 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
2539 return;
2540
2541 /* set up init dev params taskfile */
2542 DPRINTK("init dev params \n");
2543
Tejun Heoa0123702005-12-13 14:49:31 +09002544 ata_tf_init(ap, &tf, dev->devno);
2545 tf.command = ATA_CMD_INIT_DEV_PARAMS;
2546 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2547 tf.protocol = ATA_PROT_NODATA;
2548 tf.nsect = sectors;
2549 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
Albert Lee8bf62ece2005-05-12 15:29:42 -04002550
Tejun Heoa0123702005-12-13 14:49:31 +09002551 if (ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0)) {
2552 printk(KERN_ERR "ata%u: failed to init parameters, disabled\n",
2553 ap->id);
Albert Lee8bf62ece2005-05-12 15:29:42 -04002554 ata_port_disable(ap);
Tejun Heoa0123702005-12-13 14:49:31 +09002555 }
Albert Lee8bf62ece2005-05-12 15:29:42 -04002556
2557 DPRINTK("EXIT\n");
2558}
2559
2560/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002561 * ata_sg_clean - Unmap DMA memory associated with command
2562 * @qc: Command containing DMA memory to be released
2563 *
2564 * Unmap all mapped DMA memory associated with this command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 *
2566 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002567 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002568 */
2569
2570static void ata_sg_clean(struct ata_queued_cmd *qc)
2571{
2572 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002573 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002575 void *pad_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576
Tejun Heoa46314742006-02-11 19:11:13 +09002577 WARN_ON(!(qc->flags & ATA_QCFLAG_DMAMAP));
2578 WARN_ON(sg == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579
2580 if (qc->flags & ATA_QCFLAG_SINGLE)
Tejun Heoa46314742006-02-11 19:11:13 +09002581 WARN_ON(qc->n_elem != 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05002583 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002585 /* if we padded the buffer out to 32-bit bound, and data
2586 * xfer direction is from-device, we must copy from the
2587 * pad buffer back into the supplied buffer
2588 */
2589 if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
2590 pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2591
2592 if (qc->flags & ATA_QCFLAG_SG) {
Jeff Garzike1410f22005-11-14 14:06:26 -05002593 if (qc->n_elem)
2594 dma_unmap_sg(ap->host_set->dev, sg, qc->n_elem, dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002595 /* restore last sg */
2596 sg[qc->orig_n_elem - 1].length += qc->pad_len;
2597 if (pad_buf) {
2598 struct scatterlist *psg = &qc->pad_sgent;
2599 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2600 memcpy(addr + psg->offset, pad_buf, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05002601 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002602 }
2603 } else {
Jeff Garzike1410f22005-11-14 14:06:26 -05002604 if (sg_dma_len(&sg[0]) > 0)
2605 dma_unmap_single(ap->host_set->dev,
2606 sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
2607 dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002608 /* restore sg */
2609 sg->length += qc->pad_len;
2610 if (pad_buf)
2611 memcpy(qc->buf_virt + sg->length - qc->pad_len,
2612 pad_buf, qc->pad_len);
2613 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614
2615 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002616 qc->__sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617}
2618
2619/**
2620 * ata_fill_sg - Fill PCI IDE PRD table
2621 * @qc: Metadata associated with taskfile to be transferred
2622 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002623 * Fill PCI IDE PRD (scatter-gather) table with segments
2624 * associated with the current disk command.
2625 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 * LOCKING:
Jeff Garzik780a87f2005-05-30 15:41:05 -04002627 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628 *
2629 */
2630static void ata_fill_sg(struct ata_queued_cmd *qc)
2631{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002633 struct scatterlist *sg;
2634 unsigned int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635
Tejun Heoa46314742006-02-11 19:11:13 +09002636 WARN_ON(qc->__sg == NULL);
2637 WARN_ON(qc->n_elem == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638
2639 idx = 0;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002640 ata_for_each_sg(sg, qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 u32 addr, offset;
2642 u32 sg_len, len;
2643
2644 /* determine if physical DMA addr spans 64K boundary.
2645 * Note h/w doesn't support 64-bit, so we unconditionally
2646 * truncate dma_addr_t to u32.
2647 */
2648 addr = (u32) sg_dma_address(sg);
2649 sg_len = sg_dma_len(sg);
2650
2651 while (sg_len) {
2652 offset = addr & 0xffff;
2653 len = sg_len;
2654 if ((offset + sg_len) > 0x10000)
2655 len = 0x10000 - offset;
2656
2657 ap->prd[idx].addr = cpu_to_le32(addr);
2658 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
2659 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
2660
2661 idx++;
2662 sg_len -= len;
2663 addr += len;
2664 }
2665 }
2666
2667 if (idx)
2668 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2669}
2670/**
2671 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2672 * @qc: Metadata associated with taskfile to check
2673 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002674 * Allow low-level driver to filter ATA PACKET commands, returning
2675 * a status indicating whether or not it is OK to use DMA for the
2676 * supplied PACKET command.
2677 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002679 * spin_lock_irqsave(host_set lock)
2680 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 * RETURNS: 0 when ATAPI DMA can be used
2682 * nonzero otherwise
2683 */
2684int ata_check_atapi_dma(struct ata_queued_cmd *qc)
2685{
2686 struct ata_port *ap = qc->ap;
2687 int rc = 0; /* Assume ATAPI DMA is OK by default */
2688
2689 if (ap->ops->check_atapi_dma)
2690 rc = ap->ops->check_atapi_dma(qc);
2691
2692 return rc;
2693}
2694/**
2695 * ata_qc_prep - Prepare taskfile for submission
2696 * @qc: Metadata associated with taskfile to be prepared
2697 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002698 * Prepare ATA taskfile for submission.
2699 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002700 * LOCKING:
2701 * spin_lock_irqsave(host_set lock)
2702 */
2703void ata_qc_prep(struct ata_queued_cmd *qc)
2704{
2705 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2706 return;
2707
2708 ata_fill_sg(qc);
2709}
2710
Jeff Garzik0cba6322005-05-30 19:49:12 -04002711/**
2712 * ata_sg_init_one - Associate command with memory buffer
2713 * @qc: Command to be associated
2714 * @buf: Memory buffer
2715 * @buflen: Length of memory buffer, in bytes.
2716 *
2717 * Initialize the data-related elements of queued_cmd @qc
2718 * to point to a single memory buffer, @buf of byte length @buflen.
2719 *
2720 * LOCKING:
2721 * spin_lock_irqsave(host_set lock)
2722 */
2723
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
2725{
2726 struct scatterlist *sg;
2727
2728 qc->flags |= ATA_QCFLAG_SINGLE;
2729
2730 memset(&qc->sgent, 0, sizeof(qc->sgent));
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002731 qc->__sg = &qc->sgent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 qc->n_elem = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002733 qc->orig_n_elem = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 qc->buf_virt = buf;
2735
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002736 sg = qc->__sg;
Jeff Garzikf0612bb2005-10-30 01:58:18 -05002737 sg_init_one(sg, buf, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738}
2739
Jeff Garzik0cba6322005-05-30 19:49:12 -04002740/**
2741 * ata_sg_init - Associate command with scatter-gather table.
2742 * @qc: Command to be associated
2743 * @sg: Scatter-gather table.
2744 * @n_elem: Number of elements in s/g table.
2745 *
2746 * Initialize the data-related elements of queued_cmd @qc
2747 * to point to a scatter-gather table @sg, containing @n_elem
2748 * elements.
2749 *
2750 * LOCKING:
2751 * spin_lock_irqsave(host_set lock)
2752 */
2753
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
2755 unsigned int n_elem)
2756{
2757 qc->flags |= ATA_QCFLAG_SG;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002758 qc->__sg = sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 qc->n_elem = n_elem;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002760 qc->orig_n_elem = n_elem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761}
2762
2763/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002764 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
2765 * @qc: Command with memory buffer to be mapped.
2766 *
2767 * DMA-map the memory buffer associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768 *
2769 * LOCKING:
2770 * spin_lock_irqsave(host_set lock)
2771 *
2772 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002773 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 */
2775
2776static int ata_sg_setup_one(struct ata_queued_cmd *qc)
2777{
2778 struct ata_port *ap = qc->ap;
2779 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002780 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 dma_addr_t dma_address;
2782
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002783 /* we must lengthen transfers to end on a 32-bit boundary */
2784 qc->pad_len = sg->length & 3;
2785 if (qc->pad_len) {
2786 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2787 struct scatterlist *psg = &qc->pad_sgent;
2788
Tejun Heoa46314742006-02-11 19:11:13 +09002789 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002790
2791 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2792
2793 if (qc->tf.flags & ATA_TFLAG_WRITE)
2794 memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
2795 qc->pad_len);
2796
2797 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2798 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2799 /* trim sg */
2800 sg->length -= qc->pad_len;
2801
2802 DPRINTK("padding done, sg->length=%u pad_len=%u\n",
2803 sg->length, qc->pad_len);
2804 }
2805
Jeff Garzike1410f22005-11-14 14:06:26 -05002806 if (!sg->length) {
2807 sg_dma_address(sg) = 0;
2808 goto skip_map;
2809 }
2810
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 dma_address = dma_map_single(ap->host_set->dev, qc->buf_virt,
Albert Lee32529e02005-05-26 03:49:42 -04002812 sg->length, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05002813 if (dma_mapping_error(dma_address)) {
2814 /* restore sg */
2815 sg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05002817 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818
2819 sg_dma_address(sg) = dma_address;
Jeff Garzike1410f22005-11-14 14:06:26 -05002820skip_map:
Albert Lee32529e02005-05-26 03:49:42 -04002821 sg_dma_len(sg) = sg->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822
2823 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
2824 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2825
2826 return 0;
2827}
2828
2829/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002830 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
2831 * @qc: Command with scatter-gather table to be mapped.
2832 *
2833 * DMA-map the scatter-gather table associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 *
2835 * LOCKING:
2836 * spin_lock_irqsave(host_set lock)
2837 *
2838 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002839 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 *
2841 */
2842
2843static int ata_sg_setup(struct ata_queued_cmd *qc)
2844{
2845 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002846 struct scatterlist *sg = qc->__sg;
2847 struct scatterlist *lsg = &sg[qc->n_elem - 1];
Jeff Garzike1410f22005-11-14 14:06:26 -05002848 int n_elem, pre_n_elem, dir, trim_sg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849
2850 VPRINTK("ENTER, ata%u\n", ap->id);
Tejun Heoa46314742006-02-11 19:11:13 +09002851 WARN_ON(!(qc->flags & ATA_QCFLAG_SG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002853 /* we must lengthen transfers to end on a 32-bit boundary */
2854 qc->pad_len = lsg->length & 3;
2855 if (qc->pad_len) {
2856 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2857 struct scatterlist *psg = &qc->pad_sgent;
2858 unsigned int offset;
2859
Tejun Heoa46314742006-02-11 19:11:13 +09002860 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002861
2862 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2863
2864 /*
2865 * psg->page/offset are used to copy to-be-written
2866 * data in this function or read data in ata_sg_clean.
2867 */
2868 offset = lsg->offset + lsg->length - qc->pad_len;
2869 psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
2870 psg->offset = offset_in_page(offset);
2871
2872 if (qc->tf.flags & ATA_TFLAG_WRITE) {
2873 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2874 memcpy(pad_buf, addr + psg->offset, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05002875 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002876 }
2877
2878 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2879 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2880 /* trim last sg */
2881 lsg->length -= qc->pad_len;
Jeff Garzike1410f22005-11-14 14:06:26 -05002882 if (lsg->length == 0)
2883 trim_sg = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002884
2885 DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
2886 qc->n_elem - 1, lsg->length, qc->pad_len);
2887 }
2888
Jeff Garzike1410f22005-11-14 14:06:26 -05002889 pre_n_elem = qc->n_elem;
2890 if (trim_sg && pre_n_elem)
2891 pre_n_elem--;
2892
2893 if (!pre_n_elem) {
2894 n_elem = 0;
2895 goto skip_map;
2896 }
2897
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 dir = qc->dma_dir;
Jeff Garzike1410f22005-11-14 14:06:26 -05002899 n_elem = dma_map_sg(ap->host_set->dev, sg, pre_n_elem, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05002900 if (n_elem < 1) {
2901 /* restore last sg */
2902 lsg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002903 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05002904 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905
2906 DPRINTK("%d sg elements mapped\n", n_elem);
2907
Jeff Garzike1410f22005-11-14 14:06:26 -05002908skip_map:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 qc->n_elem = n_elem;
2910
2911 return 0;
2912}
2913
2914/**
Tejun Heo40e8c822005-08-22 17:12:45 +09002915 * ata_poll_qc_complete - turn irq back on and finish qc
2916 * @qc: Command to complete
Randy Dunlap8e8b77d2005-11-01 21:29:27 -08002917 * @err_mask: ATA status register content
Tejun Heo40e8c822005-08-22 17:12:45 +09002918 *
2919 * LOCKING:
2920 * None. (grabs host lock)
2921 */
2922
Albert Leea22e2eb2005-12-05 15:38:02 +08002923void ata_poll_qc_complete(struct ata_queued_cmd *qc)
Tejun Heo40e8c822005-08-22 17:12:45 +09002924{
2925 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04002926 unsigned long flags;
Tejun Heo40e8c822005-08-22 17:12:45 +09002927
Jeff Garzikb8f61532005-08-25 22:01:20 -04002928 spin_lock_irqsave(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09002929 ap->flags &= ~ATA_FLAG_NOINTR;
2930 ata_irq_on(ap);
Albert Leea22e2eb2005-12-05 15:38:02 +08002931 ata_qc_complete(qc);
Jeff Garzikb8f61532005-08-25 22:01:20 -04002932 spin_unlock_irqrestore(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09002933}
2934
2935/**
Randy Dunlapc893a3a2006-01-28 13:15:32 -05002936 * ata_pio_poll - poll using PIO, depending on current state
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002937 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 *
2939 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002940 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 *
2942 * RETURNS:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002943 * timeout value to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944 */
2945
2946static unsigned long ata_pio_poll(struct ata_port *ap)
2947{
Albert Leec14b8332005-12-05 15:36:08 +08002948 struct ata_queued_cmd *qc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 u8 status;
Albert Lee14be71f2005-09-27 17:36:35 +08002950 unsigned int poll_state = HSM_ST_UNKNOWN;
2951 unsigned int reg_state = HSM_ST_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952
Albert Leec14b8332005-12-05 15:36:08 +08002953 qc = ata_qc_from_tag(ap, ap->active_tag);
Tejun Heoa46314742006-02-11 19:11:13 +09002954 WARN_ON(qc == NULL);
Albert Leec14b8332005-12-05 15:36:08 +08002955
Albert Lee14be71f2005-09-27 17:36:35 +08002956 switch (ap->hsm_task_state) {
2957 case HSM_ST:
2958 case HSM_ST_POLL:
2959 poll_state = HSM_ST_POLL;
2960 reg_state = HSM_ST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 break;
Albert Lee14be71f2005-09-27 17:36:35 +08002962 case HSM_ST_LAST:
2963 case HSM_ST_LAST_POLL:
2964 poll_state = HSM_ST_LAST_POLL;
2965 reg_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 break;
2967 default:
2968 BUG();
2969 break;
2970 }
2971
2972 status = ata_chk_status(ap);
2973 if (status & ATA_BUSY) {
2974 if (time_after(jiffies, ap->pio_task_timeout)) {
Tejun Heo11a56d22006-01-23 13:09:36 +09002975 qc->err_mask |= AC_ERR_TIMEOUT;
Albert Lee7c398332005-11-09 13:03:30 +08002976 ap->hsm_task_state = HSM_ST_TMOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 return 0;
2978 }
Albert Lee14be71f2005-09-27 17:36:35 +08002979 ap->hsm_task_state = poll_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 return ATA_SHORT_PAUSE;
2981 }
2982
Albert Lee14be71f2005-09-27 17:36:35 +08002983 ap->hsm_task_state = reg_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 return 0;
2985}
2986
2987/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002988 * ata_pio_complete - check if drive is busy or idle
2989 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 *
2991 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002992 * None. (executing in kernel thread context)
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002993 *
2994 * RETURNS:
2995 * Non-zero if qc completed, zero otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 */
2997
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002998static int ata_pio_complete (struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999{
3000 struct ata_queued_cmd *qc;
3001 u8 drv_stat;
3002
3003 /*
Alan Cox31433ea2005-08-26 15:56:47 +01003004 * This is purely heuristic. This is a fast path. Sometimes when
3005 * we enter, BSY will be cleared in a chk-status or two. If not,
3006 * the drive is probably seeking or something. Snooze for a couple
3007 * msecs, then chk-status again. If still busy, fall back to
Albert Lee14be71f2005-09-27 17:36:35 +08003008 * HSM_ST_POLL state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 */
Albert Leefe79e682005-12-06 11:34:59 +08003010 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
3011 if (drv_stat & ATA_BUSY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 msleep(2);
Albert Leefe79e682005-12-06 11:34:59 +08003013 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
3014 if (drv_stat & ATA_BUSY) {
Albert Lee14be71f2005-09-27 17:36:35 +08003015 ap->hsm_task_state = HSM_ST_LAST_POLL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003017 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 }
3019 }
3020
Albert Leec14b8332005-12-05 15:36:08 +08003021 qc = ata_qc_from_tag(ap, ap->active_tag);
Tejun Heoa46314742006-02-11 19:11:13 +09003022 WARN_ON(qc == NULL);
Albert Leec14b8332005-12-05 15:36:08 +08003023
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 drv_stat = ata_wait_idle(ap);
3025 if (!ata_ok(drv_stat)) {
Albert Lee1c848982005-12-05 15:40:15 +08003026 qc->err_mask |= __ac_err_mask(drv_stat);
Albert Lee14be71f2005-09-27 17:36:35 +08003027 ap->hsm_task_state = HSM_ST_ERR;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003028 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003029 }
3030
Albert Lee14be71f2005-09-27 17:36:35 +08003031 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032
Tejun Heoa46314742006-02-11 19:11:13 +09003033 WARN_ON(qc->err_mask);
Albert Leea22e2eb2005-12-05 15:38:02 +08003034 ata_poll_qc_complete(qc);
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003035
3036 /* another command may start at this point */
3037
3038 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039}
3040
Edward Falk0baab862005-06-02 18:17:13 -04003041
3042/**
Randy Dunlapc893a3a2006-01-28 13:15:32 -05003043 * swap_buf_le16 - swap halves of 16-bit words in place
Edward Falk0baab862005-06-02 18:17:13 -04003044 * @buf: Buffer to swap
3045 * @buf_words: Number of 16-bit words in buffer.
3046 *
3047 * Swap halves of 16-bit words if needed to convert from
3048 * little-endian byte order to native cpu byte order, or
3049 * vice-versa.
3050 *
3051 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003052 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04003053 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054void swap_buf_le16(u16 *buf, unsigned int buf_words)
3055{
3056#ifdef __BIG_ENDIAN
3057 unsigned int i;
3058
3059 for (i = 0; i < buf_words; i++)
3060 buf[i] = le16_to_cpu(buf[i]);
3061#endif /* __BIG_ENDIAN */
3062}
3063
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003064/**
3065 * ata_mmio_data_xfer - Transfer data by MMIO
3066 * @ap: port to read/write
3067 * @buf: data buffer
3068 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04003069 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003070 *
3071 * Transfer data from/to the device data register by MMIO.
3072 *
3073 * LOCKING:
3074 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003075 */
3076
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
3078 unsigned int buflen, int write_data)
3079{
3080 unsigned int i;
3081 unsigned int words = buflen >> 1;
3082 u16 *buf16 = (u16 *) buf;
3083 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
3084
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003085 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 if (write_data) {
3087 for (i = 0; i < words; i++)
3088 writew(le16_to_cpu(buf16[i]), mmio);
3089 } else {
3090 for (i = 0; i < words; i++)
3091 buf16[i] = cpu_to_le16(readw(mmio));
3092 }
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003093
3094 /* Transfer trailing 1 byte, if any. */
3095 if (unlikely(buflen & 0x01)) {
3096 u16 align_buf[1] = { 0 };
3097 unsigned char *trailing_buf = buf + buflen - 1;
3098
3099 if (write_data) {
3100 memcpy(align_buf, trailing_buf, 1);
3101 writew(le16_to_cpu(align_buf[0]), mmio);
3102 } else {
3103 align_buf[0] = cpu_to_le16(readw(mmio));
3104 memcpy(trailing_buf, align_buf, 1);
3105 }
3106 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107}
3108
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003109/**
3110 * ata_pio_data_xfer - Transfer data by PIO
3111 * @ap: port to read/write
3112 * @buf: data buffer
3113 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04003114 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003115 *
3116 * Transfer data from/to the device data register by PIO.
3117 *
3118 * LOCKING:
3119 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003120 */
3121
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
3123 unsigned int buflen, int write_data)
3124{
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003125 unsigned int words = buflen >> 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003127 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 if (write_data)
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003129 outsw(ap->ioaddr.data_addr, buf, words);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130 else
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003131 insw(ap->ioaddr.data_addr, buf, words);
3132
3133 /* Transfer trailing 1 byte, if any. */
3134 if (unlikely(buflen & 0x01)) {
3135 u16 align_buf[1] = { 0 };
3136 unsigned char *trailing_buf = buf + buflen - 1;
3137
3138 if (write_data) {
3139 memcpy(align_buf, trailing_buf, 1);
3140 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
3141 } else {
3142 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
3143 memcpy(trailing_buf, align_buf, 1);
3144 }
3145 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146}
3147
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003148/**
3149 * ata_data_xfer - Transfer data from/to the data register.
3150 * @ap: port to read/write
3151 * @buf: data buffer
3152 * @buflen: buffer length
3153 * @do_write: read/write
3154 *
3155 * Transfer data from/to the device data register.
3156 *
3157 * LOCKING:
3158 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003159 */
3160
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
3162 unsigned int buflen, int do_write)
3163{
Alan Coxa1bd9e62006-01-17 20:53:50 +00003164 /* Make the crap hardware pay the costs not the good stuff */
3165 if (unlikely(ap->flags & ATA_FLAG_IRQ_MASK)) {
3166 unsigned long flags;
3167 local_irq_save(flags);
3168 if (ap->flags & ATA_FLAG_MMIO)
3169 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3170 else
3171 ata_pio_data_xfer(ap, buf, buflen, do_write);
3172 local_irq_restore(flags);
3173 } else {
3174 if (ap->flags & ATA_FLAG_MMIO)
3175 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3176 else
3177 ata_pio_data_xfer(ap, buf, buflen, do_write);
3178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179}
3180
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003181/**
3182 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
3183 * @qc: Command on going
3184 *
3185 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
3186 *
3187 * LOCKING:
3188 * Inherited from caller.
3189 */
3190
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191static void ata_pio_sector(struct ata_queued_cmd *qc)
3192{
3193 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003194 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195 struct ata_port *ap = qc->ap;
3196 struct page *page;
3197 unsigned int offset;
3198 unsigned char *buf;
3199
3200 if (qc->cursect == (qc->nsect - 1))
Albert Lee14be71f2005-09-27 17:36:35 +08003201 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202
3203 page = sg[qc->cursg].page;
3204 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
3205
3206 /* get the current page and offset */
3207 page = nth_page(page, (offset >> PAGE_SHIFT));
3208 offset %= PAGE_SIZE;
3209
3210 buf = kmap(page) + offset;
3211
3212 qc->cursect++;
3213 qc->cursg_ofs++;
3214
Albert Lee32529e02005-05-26 03:49:42 -04003215 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 qc->cursg++;
3217 qc->cursg_ofs = 0;
3218 }
3219
3220 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3221
3222 /* do the actual data transfer */
3223 do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3224 ata_data_xfer(ap, buf, ATA_SECT_SIZE, do_write);
3225
3226 kunmap(page);
3227}
3228
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003229/**
3230 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
3231 * @qc: Command on going
3232 * @bytes: number of bytes
3233 *
3234 * Transfer Transfer data from/to the ATAPI device.
3235 *
3236 * LOCKING:
3237 * Inherited from caller.
3238 *
3239 */
3240
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
3242{
3243 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003244 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 struct ata_port *ap = qc->ap;
3246 struct page *page;
3247 unsigned char *buf;
3248 unsigned int offset, count;
3249
Albert Lee563a6e12005-08-12 14:17:50 +08003250 if (qc->curbytes + bytes >= qc->nbytes)
Albert Lee14be71f2005-09-27 17:36:35 +08003251 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252
3253next_sg:
Albert Lee563a6e12005-08-12 14:17:50 +08003254 if (unlikely(qc->cursg >= qc->n_elem)) {
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003255 /*
Albert Lee563a6e12005-08-12 14:17:50 +08003256 * The end of qc->sg is reached and the device expects
3257 * more data to transfer. In order not to overrun qc->sg
3258 * and fulfill length specified in the byte count register,
3259 * - for read case, discard trailing data from the device
3260 * - for write case, padding zero data to the device
3261 */
3262 u16 pad_buf[1] = { 0 };
3263 unsigned int words = bytes >> 1;
3264 unsigned int i;
3265
3266 if (words) /* warning if bytes > 1 */
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003267 printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
Albert Lee563a6e12005-08-12 14:17:50 +08003268 ap->id, bytes);
3269
3270 for (i = 0; i < words; i++)
3271 ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
3272
Albert Lee14be71f2005-09-27 17:36:35 +08003273 ap->hsm_task_state = HSM_ST_LAST;
Albert Lee563a6e12005-08-12 14:17:50 +08003274 return;
3275 }
3276
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003277 sg = &qc->__sg[qc->cursg];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 page = sg->page;
3280 offset = sg->offset + qc->cursg_ofs;
3281
3282 /* get the current page and offset */
3283 page = nth_page(page, (offset >> PAGE_SHIFT));
3284 offset %= PAGE_SIZE;
3285
Albert Lee6952df02005-06-06 15:56:03 +08003286 /* don't overrun current sg */
Albert Lee32529e02005-05-26 03:49:42 -04003287 count = min(sg->length - qc->cursg_ofs, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288
3289 /* don't cross page boundaries */
3290 count = min(count, (unsigned int)PAGE_SIZE - offset);
3291
3292 buf = kmap(page) + offset;
3293
3294 bytes -= count;
3295 qc->curbytes += count;
3296 qc->cursg_ofs += count;
3297
Albert Lee32529e02005-05-26 03:49:42 -04003298 if (qc->cursg_ofs == sg->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003299 qc->cursg++;
3300 qc->cursg_ofs = 0;
3301 }
3302
3303 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3304
3305 /* do the actual data transfer */
3306 ata_data_xfer(ap, buf, count, do_write);
3307
3308 kunmap(page);
3309
Albert Lee563a6e12005-08-12 14:17:50 +08003310 if (bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 goto next_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312}
3313
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003314/**
3315 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
3316 * @qc: Command on going
3317 *
3318 * Transfer Transfer data from/to the ATAPI device.
3319 *
3320 * LOCKING:
3321 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003322 */
3323
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324static void atapi_pio_bytes(struct ata_queued_cmd *qc)
3325{
3326 struct ata_port *ap = qc->ap;
3327 struct ata_device *dev = qc->dev;
3328 unsigned int ireason, bc_lo, bc_hi, bytes;
3329 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
3330
3331 ap->ops->tf_read(ap, &qc->tf);
3332 ireason = qc->tf.nsect;
3333 bc_lo = qc->tf.lbam;
3334 bc_hi = qc->tf.lbah;
3335 bytes = (bc_hi << 8) | bc_lo;
3336
3337 /* shall be cleared to zero, indicating xfer of data */
3338 if (ireason & (1 << 0))
3339 goto err_out;
3340
3341 /* make sure transfer direction matches expected */
3342 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
3343 if (do_write != i_write)
3344 goto err_out;
3345
3346 __atapi_pio_bytes(qc, bytes);
3347
3348 return;
3349
3350err_out:
3351 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
3352 ap->id, dev->devno);
Tejun Heo11a56d22006-01-23 13:09:36 +09003353 qc->err_mask |= AC_ERR_HSM;
Albert Lee14be71f2005-09-27 17:36:35 +08003354 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003355}
3356
3357/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003358 * ata_pio_block - start PIO on a block
3359 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 *
3361 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003362 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 */
3364
3365static void ata_pio_block(struct ata_port *ap)
3366{
3367 struct ata_queued_cmd *qc;
3368 u8 status;
3369
3370 /*
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003371 * This is purely heuristic. This is a fast path.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 * Sometimes when we enter, BSY will be cleared in
3373 * a chk-status or two. If not, the drive is probably seeking
3374 * or something. Snooze for a couple msecs, then
3375 * chk-status again. If still busy, fall back to
Albert Lee14be71f2005-09-27 17:36:35 +08003376 * HSM_ST_POLL state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 */
3378 status = ata_busy_wait(ap, ATA_BUSY, 5);
3379 if (status & ATA_BUSY) {
3380 msleep(2);
3381 status = ata_busy_wait(ap, ATA_BUSY, 10);
3382 if (status & ATA_BUSY) {
Albert Lee14be71f2005-09-27 17:36:35 +08003383 ap->hsm_task_state = HSM_ST_POLL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
3385 return;
3386 }
3387 }
3388
3389 qc = ata_qc_from_tag(ap, ap->active_tag);
Tejun Heoa46314742006-02-11 19:11:13 +09003390 WARN_ON(qc == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391
Albert Leefe79e682005-12-06 11:34:59 +08003392 /* check error */
3393 if (status & (ATA_ERR | ATA_DF)) {
3394 qc->err_mask |= AC_ERR_DEV;
3395 ap->hsm_task_state = HSM_ST_ERR;
3396 return;
3397 }
3398
3399 /* transfer data if any */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400 if (is_atapi_taskfile(&qc->tf)) {
Albert Leefe79e682005-12-06 11:34:59 +08003401 /* DRQ=0 means no more data to transfer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 if ((status & ATA_DRQ) == 0) {
Albert Lee14be71f2005-09-27 17:36:35 +08003403 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 return;
3405 }
3406
3407 atapi_pio_bytes(qc);
3408 } else {
3409 /* handle BSY=0, DRQ=0 as error */
3410 if ((status & ATA_DRQ) == 0) {
Tejun Heo11a56d22006-01-23 13:09:36 +09003411 qc->err_mask |= AC_ERR_HSM;
Albert Lee14be71f2005-09-27 17:36:35 +08003412 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413 return;
3414 }
3415
3416 ata_pio_sector(qc);
3417 }
3418}
3419
3420static void ata_pio_error(struct ata_port *ap)
3421{
3422 struct ata_queued_cmd *qc;
Jeff Garzika7dac442005-10-30 04:44:42 -05003423
3424 printk(KERN_WARNING "ata%u: PIO error\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425
3426 qc = ata_qc_from_tag(ap, ap->active_tag);
Tejun Heoa46314742006-02-11 19:11:13 +09003427 WARN_ON(qc == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428
Albert Lee1c848982005-12-05 15:40:15 +08003429 /* make sure qc->err_mask is available to
3430 * know what's wrong and recover
3431 */
Tejun Heoa46314742006-02-11 19:11:13 +09003432 WARN_ON(qc->err_mask == 0);
Albert Lee1c848982005-12-05 15:40:15 +08003433
Albert Lee14be71f2005-09-27 17:36:35 +08003434 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435
Albert Leea22e2eb2005-12-05 15:38:02 +08003436 ata_poll_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437}
3438
3439static void ata_pio_task(void *_data)
3440{
3441 struct ata_port *ap = _data;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003442 unsigned long timeout;
3443 int qc_completed;
3444
3445fsm_start:
3446 timeout = 0;
3447 qc_completed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448
Albert Lee14be71f2005-09-27 17:36:35 +08003449 switch (ap->hsm_task_state) {
3450 case HSM_ST_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451 return;
3452
Albert Lee14be71f2005-09-27 17:36:35 +08003453 case HSM_ST:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454 ata_pio_block(ap);
3455 break;
3456
Albert Lee14be71f2005-09-27 17:36:35 +08003457 case HSM_ST_LAST:
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003458 qc_completed = ata_pio_complete(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 break;
3460
Albert Lee14be71f2005-09-27 17:36:35 +08003461 case HSM_ST_POLL:
3462 case HSM_ST_LAST_POLL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 timeout = ata_pio_poll(ap);
3464 break;
3465
Albert Lee14be71f2005-09-27 17:36:35 +08003466 case HSM_ST_TMOUT:
3467 case HSM_ST_ERR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 ata_pio_error(ap);
3469 return;
3470 }
3471
3472 if (timeout)
Tejun Heo95064372006-01-23 13:09:37 +09003473 ata_queue_delayed_pio_task(ap, timeout);
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003474 else if (!qc_completed)
3475 goto fsm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476}
3477
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478/**
3479 * ata_qc_timeout - Handle timeout of queued command
3480 * @qc: Command that timed out
3481 *
3482 * Some part of the kernel (currently, only the SCSI layer)
3483 * has noticed that the active command on port @ap has not
3484 * completed after a specified length of time. Handle this
3485 * condition by disabling DMA (if necessary) and completing
3486 * transactions, with error if necessary.
3487 *
3488 * This also handles the case of the "lost interrupt", where
3489 * for some reason (possibly hardware bug, possibly driver bug)
3490 * an interrupt was not delivered to the driver, even though the
3491 * transaction completed successfully.
3492 *
3493 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003494 * Inherited from SCSI layer (none, can sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495 */
3496
3497static void ata_qc_timeout(struct ata_queued_cmd *qc)
3498{
3499 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003500 struct ata_host_set *host_set = ap->host_set;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501 u8 host_stat = 0, drv_stat;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003502 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503
3504 DPRINTK("ENTER\n");
3505
Tejun Heoc18d06f2006-02-02 00:56:10 +09003506 ata_flush_pio_tasks(ap);
3507 ap->hsm_task_state = HSM_ST_IDLE;
3508
Jeff Garzikb8f61532005-08-25 22:01:20 -04003509 spin_lock_irqsave(&host_set->lock, flags);
3510
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511 switch (qc->tf.protocol) {
3512
3513 case ATA_PROT_DMA:
3514 case ATA_PROT_ATAPI_DMA:
3515 host_stat = ap->ops->bmdma_status(ap);
3516
3517 /* before we do anything else, clear DMA-Start bit */
Alan Coxb73fc892005-08-26 16:03:19 +01003518 ap->ops->bmdma_stop(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003519
3520 /* fall through */
3521
3522 default:
3523 ata_altstatus(ap);
3524 drv_stat = ata_chk_status(ap);
3525
3526 /* ack bmdma irq events */
3527 ap->ops->irq_clear(ap);
3528
3529 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
3530 ap->id, qc->tf.command, drv_stat, host_stat);
3531
3532 /* complete taskfile transaction */
Albert Leea22e2eb2005-12-05 15:38:02 +08003533 qc->err_mask |= ac_err_mask(drv_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534 break;
3535 }
Jeff Garzikb8f61532005-08-25 22:01:20 -04003536
3537 spin_unlock_irqrestore(&host_set->lock, flags);
3538
Tejun Heoa72ec4c2006-01-23 13:09:37 +09003539 ata_eh_qc_complete(qc);
3540
Linus Torvalds1da177e2005-04-16 15:20:36 -07003541 DPRINTK("EXIT\n");
3542}
3543
3544/**
3545 * ata_eng_timeout - Handle timeout of queued command
3546 * @ap: Port on which timed-out command is active
3547 *
3548 * Some part of the kernel (currently, only the SCSI layer)
3549 * has noticed that the active command on port @ap has not
3550 * completed after a specified length of time. Handle this
3551 * condition by disabling DMA (if necessary) and completing
3552 * transactions, with error if necessary.
3553 *
3554 * This also handles the case of the "lost interrupt", where
3555 * for some reason (possibly hardware bug, possibly driver bug)
3556 * an interrupt was not delivered to the driver, even though the
3557 * transaction completed successfully.
3558 *
3559 * LOCKING:
3560 * Inherited from SCSI layer (none, can sleep)
3561 */
3562
3563void ata_eng_timeout(struct ata_port *ap)
3564{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565 DPRINTK("ENTER\n");
3566
Tejun Heof6379022006-02-10 15:10:48 +09003567 ata_qc_timeout(ata_qc_from_tag(ap, ap->active_tag));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569 DPRINTK("EXIT\n");
3570}
3571
3572/**
3573 * ata_qc_new - Request an available ATA command, for queueing
3574 * @ap: Port associated with device @dev
3575 * @dev: Device from whom we request an available command structure
3576 *
3577 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003578 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579 */
3580
3581static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
3582{
3583 struct ata_queued_cmd *qc = NULL;
3584 unsigned int i;
3585
3586 for (i = 0; i < ATA_MAX_QUEUE; i++)
3587 if (!test_and_set_bit(i, &ap->qactive)) {
3588 qc = ata_qc_from_tag(ap, i);
3589 break;
3590 }
3591
3592 if (qc)
3593 qc->tag = i;
3594
3595 return qc;
3596}
3597
3598/**
3599 * ata_qc_new_init - Request an available ATA command, and initialize it
3600 * @ap: Port associated with device @dev
3601 * @dev: Device from whom we request an available command structure
3602 *
3603 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003604 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 */
3606
3607struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
3608 struct ata_device *dev)
3609{
3610 struct ata_queued_cmd *qc;
3611
3612 qc = ata_qc_new(ap);
3613 if (qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003614 qc->scsicmd = NULL;
3615 qc->ap = ap;
3616 qc->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05003618 ata_qc_reinit(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 }
3620
3621 return qc;
3622}
3623
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624/**
3625 * ata_qc_free - free unused ata_queued_cmd
3626 * @qc: Command to complete
3627 *
3628 * Designed to free unused ata_queued_cmd object
3629 * in case something prevents using it.
3630 *
3631 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003632 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633 */
3634void ata_qc_free(struct ata_queued_cmd *qc)
3635{
Tejun Heo4ba946e2006-01-23 13:09:36 +09003636 struct ata_port *ap = qc->ap;
3637 unsigned int tag;
3638
Tejun Heoa46314742006-02-11 19:11:13 +09003639 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003640
Tejun Heo4ba946e2006-01-23 13:09:36 +09003641 qc->flags = 0;
3642 tag = qc->tag;
3643 if (likely(ata_tag_valid(tag))) {
3644 if (tag == ap->active_tag)
3645 ap->active_tag = ATA_TAG_POISON;
3646 qc->tag = ATA_TAG_POISON;
3647 clear_bit(tag, &ap->qactive);
3648 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649}
3650
Tejun Heo76014422006-02-11 15:13:49 +09003651void __ata_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003652{
Tejun Heoa46314742006-02-11 19:11:13 +09003653 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
3654 WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655
3656 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
3657 ata_sg_clean(qc);
3658
Albert Lee3f3791d2005-08-16 14:25:38 +08003659 /* atapi: mark qc as inactive to prevent the interrupt handler
3660 * from completing the command twice later, before the error handler
3661 * is called. (when rc != 0 and atapi request sense is needed)
3662 */
3663 qc->flags &= ~ATA_QCFLAG_ACTIVE;
3664
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665 /* call completion callback */
Tejun Heo77853bf2006-01-23 13:09:36 +09003666 qc->complete_fn(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667}
3668
3669static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
3670{
3671 struct ata_port *ap = qc->ap;
3672
3673 switch (qc->tf.protocol) {
3674 case ATA_PROT_DMA:
3675 case ATA_PROT_ATAPI_DMA:
3676 return 1;
3677
3678 case ATA_PROT_ATAPI:
3679 case ATA_PROT_PIO:
3680 case ATA_PROT_PIO_MULT:
3681 if (ap->flags & ATA_FLAG_PIO_DMA)
3682 return 1;
3683
3684 /* fall through */
3685
3686 default:
3687 return 0;
3688 }
3689
3690 /* never reached */
3691}
3692
3693/**
3694 * ata_qc_issue - issue taskfile to device
3695 * @qc: command to issue to device
3696 *
3697 * Prepare an ATA command to submission to device.
3698 * This includes mapping the data into a DMA-able
3699 * area, filling in the S/G table, and finally
3700 * writing the taskfile to hardware, starting the command.
3701 *
3702 * LOCKING:
3703 * spin_lock_irqsave(host_set lock)
3704 *
3705 * RETURNS:
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003706 * Zero on success, AC_ERR_* mask on failure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707 */
3708
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003709unsigned int ata_qc_issue(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710{
3711 struct ata_port *ap = qc->ap;
3712
3713 if (ata_should_dma_map(qc)) {
3714 if (qc->flags & ATA_QCFLAG_SG) {
3715 if (ata_sg_setup(qc))
Tejun Heo8e436af2006-01-23 13:09:36 +09003716 goto sg_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
3718 if (ata_sg_setup_one(qc))
Tejun Heo8e436af2006-01-23 13:09:36 +09003719 goto sg_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720 }
3721 } else {
3722 qc->flags &= ~ATA_QCFLAG_DMAMAP;
3723 }
3724
3725 ap->ops->qc_prep(qc);
3726
3727 qc->ap->active_tag = qc->tag;
3728 qc->flags |= ATA_QCFLAG_ACTIVE;
3729
3730 return ap->ops->qc_issue(qc);
3731
Tejun Heo8e436af2006-01-23 13:09:36 +09003732sg_err:
3733 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003734 return AC_ERR_SYSTEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003735}
3736
Edward Falk0baab862005-06-02 18:17:13 -04003737
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738/**
3739 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
3740 * @qc: command to issue to device
3741 *
3742 * Using various libata functions and hooks, this function
3743 * starts an ATA command. ATA commands are grouped into
3744 * classes called "protocols", and issuing each type of protocol
3745 * is slightly different.
3746 *
Edward Falk0baab862005-06-02 18:17:13 -04003747 * May be used as the qc_issue() entry in ata_port_operations.
3748 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749 * LOCKING:
3750 * spin_lock_irqsave(host_set lock)
3751 *
3752 * RETURNS:
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003753 * Zero on success, AC_ERR_* mask on failure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754 */
3755
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003756unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757{
3758 struct ata_port *ap = qc->ap;
3759
3760 ata_dev_select(ap, qc->dev->devno, 1, 0);
3761
3762 switch (qc->tf.protocol) {
3763 case ATA_PROT_NODATA:
Jeff Garzike5338252005-10-30 21:37:17 -05003764 ata_tf_to_host(ap, &qc->tf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003765 break;
3766
3767 case ATA_PROT_DMA:
3768 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3769 ap->ops->bmdma_setup(qc); /* set up bmdma */
3770 ap->ops->bmdma_start(qc); /* initiate bmdma */
3771 break;
3772
3773 case ATA_PROT_PIO: /* load tf registers, initiate polling pio */
3774 ata_qc_set_polling(qc);
Jeff Garzike5338252005-10-30 21:37:17 -05003775 ata_tf_to_host(ap, &qc->tf);
Albert Lee14be71f2005-09-27 17:36:35 +08003776 ap->hsm_task_state = HSM_ST;
Tejun Heo95064372006-01-23 13:09:37 +09003777 ata_queue_pio_task(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778 break;
3779
3780 case ATA_PROT_ATAPI:
3781 ata_qc_set_polling(qc);
Jeff Garzike5338252005-10-30 21:37:17 -05003782 ata_tf_to_host(ap, &qc->tf);
Tejun Heo95064372006-01-23 13:09:37 +09003783 ata_queue_packet_task(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003784 break;
3785
3786 case ATA_PROT_ATAPI_NODATA:
Tejun Heoc1389502005-08-22 14:59:24 +09003787 ap->flags |= ATA_FLAG_NOINTR;
Jeff Garzike5338252005-10-30 21:37:17 -05003788 ata_tf_to_host(ap, &qc->tf);
Tejun Heo95064372006-01-23 13:09:37 +09003789 ata_queue_packet_task(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790 break;
3791
3792 case ATA_PROT_ATAPI_DMA:
Tejun Heoc1389502005-08-22 14:59:24 +09003793 ap->flags |= ATA_FLAG_NOINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003794 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3795 ap->ops->bmdma_setup(qc); /* set up bmdma */
Tejun Heo95064372006-01-23 13:09:37 +09003796 ata_queue_packet_task(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797 break;
3798
3799 default:
3800 WARN_ON(1);
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003801 return AC_ERR_SYSTEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802 }
3803
3804 return 0;
3805}
3806
3807/**
Edward Falk0baab862005-06-02 18:17:13 -04003808 * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809 * @qc: Info associated with this ATA transaction.
3810 *
3811 * LOCKING:
3812 * spin_lock_irqsave(host_set lock)
3813 */
3814
3815static void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc)
3816{
3817 struct ata_port *ap = qc->ap;
3818 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3819 u8 dmactl;
3820 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3821
3822 /* load PRD table addr. */
3823 mb(); /* make sure PRD table writes are visible to controller */
3824 writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
3825
3826 /* specify data direction, triple-check start bit is clear */
3827 dmactl = readb(mmio + ATA_DMA_CMD);
3828 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3829 if (!rw)
3830 dmactl |= ATA_DMA_WR;
3831 writeb(dmactl, mmio + ATA_DMA_CMD);
3832
3833 /* issue r/w command */
3834 ap->ops->exec_command(ap, &qc->tf);
3835}
3836
3837/**
Alan Coxb73fc892005-08-26 16:03:19 +01003838 * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839 * @qc: Info associated with this ATA transaction.
3840 *
3841 * LOCKING:
3842 * spin_lock_irqsave(host_set lock)
3843 */
3844
3845static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
3846{
3847 struct ata_port *ap = qc->ap;
3848 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3849 u8 dmactl;
3850
3851 /* start host DMA transaction */
3852 dmactl = readb(mmio + ATA_DMA_CMD);
3853 writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
3854
3855 /* Strictly, one may wish to issue a readb() here, to
3856 * flush the mmio write. However, control also passes
3857 * to the hardware at this point, and it will interrupt
3858 * us when we are to resume control. So, in effect,
3859 * we don't care when the mmio write flushes.
3860 * Further, a read of the DMA status register _immediately_
3861 * following the write may not be what certain flaky hardware
3862 * is expected, so I think it is best to not add a readb()
3863 * without first all the MMIO ATA cards/mobos.
3864 * Or maybe I'm just being paranoid.
3865 */
3866}
3867
3868/**
3869 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
3870 * @qc: Info associated with this ATA transaction.
3871 *
3872 * LOCKING:
3873 * spin_lock_irqsave(host_set lock)
3874 */
3875
3876static void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
3877{
3878 struct ata_port *ap = qc->ap;
3879 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3880 u8 dmactl;
3881
3882 /* load PRD table addr. */
3883 outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
3884
3885 /* specify data direction, triple-check start bit is clear */
3886 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3887 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3888 if (!rw)
3889 dmactl |= ATA_DMA_WR;
3890 outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3891
3892 /* issue r/w command */
3893 ap->ops->exec_command(ap, &qc->tf);
3894}
3895
3896/**
3897 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
3898 * @qc: Info associated with this ATA transaction.
3899 *
3900 * LOCKING:
3901 * spin_lock_irqsave(host_set lock)
3902 */
3903
3904static void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
3905{
3906 struct ata_port *ap = qc->ap;
3907 u8 dmactl;
3908
3909 /* start host DMA transaction */
3910 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3911 outb(dmactl | ATA_DMA_START,
3912 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3913}
3914
Edward Falk0baab862005-06-02 18:17:13 -04003915
3916/**
3917 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
3918 * @qc: Info associated with this ATA transaction.
3919 *
3920 * Writes the ATA_DMA_START flag to the DMA command register.
3921 *
3922 * May be used as the bmdma_start() entry in ata_port_operations.
3923 *
3924 * LOCKING:
3925 * spin_lock_irqsave(host_set lock)
3926 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927void ata_bmdma_start(struct ata_queued_cmd *qc)
3928{
3929 if (qc->ap->flags & ATA_FLAG_MMIO)
3930 ata_bmdma_start_mmio(qc);
3931 else
3932 ata_bmdma_start_pio(qc);
3933}
3934
Edward Falk0baab862005-06-02 18:17:13 -04003935
3936/**
3937 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
3938 * @qc: Info associated with this ATA transaction.
3939 *
3940 * Writes address of PRD table to device's PRD Table Address
3941 * register, sets the DMA control register, and calls
3942 * ops->exec_command() to start the transfer.
3943 *
3944 * May be used as the bmdma_setup() entry in ata_port_operations.
3945 *
3946 * LOCKING:
3947 * spin_lock_irqsave(host_set lock)
3948 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949void ata_bmdma_setup(struct ata_queued_cmd *qc)
3950{
3951 if (qc->ap->flags & ATA_FLAG_MMIO)
3952 ata_bmdma_setup_mmio(qc);
3953 else
3954 ata_bmdma_setup_pio(qc);
3955}
3956
Edward Falk0baab862005-06-02 18:17:13 -04003957
3958/**
3959 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
Jeff Garzikdecc6d02005-06-02 18:42:33 -04003960 * @ap: Port associated with this ATA transaction.
Edward Falk0baab862005-06-02 18:17:13 -04003961 *
3962 * Clear interrupt and error flags in DMA status register.
3963 *
3964 * May be used as the irq_clear() entry in ata_port_operations.
3965 *
3966 * LOCKING:
3967 * spin_lock_irqsave(host_set lock)
3968 */
3969
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970void ata_bmdma_irq_clear(struct ata_port *ap)
3971{
3972 if (ap->flags & ATA_FLAG_MMIO) {
3973 void __iomem *mmio = ((void __iomem *) ap->ioaddr.bmdma_addr) + ATA_DMA_STATUS;
3974 writeb(readb(mmio), mmio);
3975 } else {
3976 unsigned long addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
3977 outb(inb(addr), addr);
3978 }
3979
3980}
3981
Edward Falk0baab862005-06-02 18:17:13 -04003982
3983/**
3984 * ata_bmdma_status - Read PCI IDE BMDMA status
Jeff Garzikdecc6d02005-06-02 18:42:33 -04003985 * @ap: Port associated with this ATA transaction.
Edward Falk0baab862005-06-02 18:17:13 -04003986 *
3987 * Read and return BMDMA status register.
3988 *
3989 * May be used as the bmdma_status() entry in ata_port_operations.
3990 *
3991 * LOCKING:
3992 * spin_lock_irqsave(host_set lock)
3993 */
3994
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995u8 ata_bmdma_status(struct ata_port *ap)
3996{
3997 u8 host_stat;
3998 if (ap->flags & ATA_FLAG_MMIO) {
3999 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4000 host_stat = readb(mmio + ATA_DMA_STATUS);
4001 } else
Albert Leeee500aa2005-09-27 17:34:38 +08004002 host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003 return host_stat;
4004}
4005
Edward Falk0baab862005-06-02 18:17:13 -04004006
4007/**
4008 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
Alan Coxb73fc892005-08-26 16:03:19 +01004009 * @qc: Command we are ending DMA for
Edward Falk0baab862005-06-02 18:17:13 -04004010 *
4011 * Clears the ATA_DMA_START flag in the dma control register
4012 *
4013 * May be used as the bmdma_stop() entry in ata_port_operations.
4014 *
4015 * LOCKING:
4016 * spin_lock_irqsave(host_set lock)
4017 */
4018
Alan Coxb73fc892005-08-26 16:03:19 +01004019void ata_bmdma_stop(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020{
Alan Coxb73fc892005-08-26 16:03:19 +01004021 struct ata_port *ap = qc->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004022 if (ap->flags & ATA_FLAG_MMIO) {
4023 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4024
4025 /* clear start/stop bit */
4026 writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
4027 mmio + ATA_DMA_CMD);
4028 } else {
4029 /* clear start/stop bit */
4030 outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
4031 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4032 }
4033
4034 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
4035 ata_altstatus(ap); /* dummy read */
4036}
4037
4038/**
4039 * ata_host_intr - Handle host interrupt for given (port, task)
4040 * @ap: Port on which interrupt arrived (possibly...)
4041 * @qc: Taskfile currently active in engine
4042 *
4043 * Handle host interrupt for given queued command. Currently,
4044 * only DMA interrupts are handled. All other commands are
4045 * handled via polling with interrupts disabled (nIEN bit).
4046 *
4047 * LOCKING:
4048 * spin_lock_irqsave(host_set lock)
4049 *
4050 * RETURNS:
4051 * One if interrupt was handled, zero if not (shared irq).
4052 */
4053
4054inline unsigned int ata_host_intr (struct ata_port *ap,
4055 struct ata_queued_cmd *qc)
4056{
4057 u8 status, host_stat;
4058
4059 switch (qc->tf.protocol) {
4060
4061 case ATA_PROT_DMA:
4062 case ATA_PROT_ATAPI_DMA:
4063 case ATA_PROT_ATAPI:
4064 /* check status of DMA engine */
4065 host_stat = ap->ops->bmdma_status(ap);
4066 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
4067
4068 /* if it's not our irq... */
4069 if (!(host_stat & ATA_DMA_INTR))
4070 goto idle_irq;
4071
4072 /* before we do anything else, clear DMA-Start bit */
Alan Coxb73fc892005-08-26 16:03:19 +01004073 ap->ops->bmdma_stop(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074
4075 /* fall through */
4076
4077 case ATA_PROT_ATAPI_NODATA:
4078 case ATA_PROT_NODATA:
4079 /* check altstatus */
4080 status = ata_altstatus(ap);
4081 if (status & ATA_BUSY)
4082 goto idle_irq;
4083
4084 /* check main status, clearing INTRQ */
4085 status = ata_chk_status(ap);
4086 if (unlikely(status & ATA_BUSY))
4087 goto idle_irq;
4088 DPRINTK("ata%u: protocol %d (dev_stat 0x%X)\n",
4089 ap->id, qc->tf.protocol, status);
4090
4091 /* ack bmdma irq events */
4092 ap->ops->irq_clear(ap);
4093
4094 /* complete taskfile transaction */
Albert Leea22e2eb2005-12-05 15:38:02 +08004095 qc->err_mask |= ac_err_mask(status);
4096 ata_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097 break;
4098
4099 default:
4100 goto idle_irq;
4101 }
4102
4103 return 1; /* irq handled */
4104
4105idle_irq:
4106 ap->stats.idle_irq++;
4107
4108#ifdef ATA_IRQ_TRAP
4109 if ((ap->stats.idle_irq % 1000) == 0) {
4110 handled = 1;
4111 ata_irq_ack(ap, 0); /* debug trap */
4112 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
4113 }
4114#endif
4115 return 0; /* irq not handled */
4116}
4117
4118/**
4119 * ata_interrupt - Default ATA host interrupt handler
Jeff Garzik0cba6322005-05-30 19:49:12 -04004120 * @irq: irq line (unused)
4121 * @dev_instance: pointer to our ata_host_set information structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122 * @regs: unused
4123 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004124 * Default interrupt handler for PCI IDE devices. Calls
4125 * ata_host_intr() for each port that is not disabled.
4126 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004128 * Obtains host_set lock during operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004129 *
4130 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004131 * IRQ_NONE or IRQ_HANDLED.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132 */
4133
4134irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
4135{
4136 struct ata_host_set *host_set = dev_instance;
4137 unsigned int i;
4138 unsigned int handled = 0;
4139 unsigned long flags;
4140
4141 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
4142 spin_lock_irqsave(&host_set->lock, flags);
4143
4144 for (i = 0; i < host_set->n_ports; i++) {
4145 struct ata_port *ap;
4146
4147 ap = host_set->ports[i];
Tejun Heoc1389502005-08-22 14:59:24 +09004148 if (ap &&
4149 !(ap->flags & (ATA_FLAG_PORT_DISABLED | ATA_FLAG_NOINTR))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150 struct ata_queued_cmd *qc;
4151
4152 qc = ata_qc_from_tag(ap, ap->active_tag);
Albert Lee21b1ed72005-04-29 17:34:59 +08004153 if (qc && (!(qc->tf.ctl & ATA_NIEN)) &&
4154 (qc->flags & ATA_QCFLAG_ACTIVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 handled |= ata_host_intr(ap, qc);
4156 }
4157 }
4158
4159 spin_unlock_irqrestore(&host_set->lock, flags);
4160
4161 return IRQ_RETVAL(handled);
4162}
4163
4164/**
4165 * atapi_packet_task - Write CDB bytes to hardware
4166 * @_data: Port to which ATAPI device is attached.
4167 *
4168 * When device has indicated its readiness to accept
4169 * a CDB, this function is called. Send the CDB.
4170 * If DMA is to be performed, exit immediately.
4171 * Otherwise, we are in polling mode, so poll
4172 * status under operation succeeds or fails.
4173 *
4174 * LOCKING:
4175 * Kernel thread context (may sleep)
4176 */
4177
4178static void atapi_packet_task(void *_data)
4179{
4180 struct ata_port *ap = _data;
4181 struct ata_queued_cmd *qc;
4182 u8 status;
4183
4184 qc = ata_qc_from_tag(ap, ap->active_tag);
Tejun Heoa46314742006-02-11 19:11:13 +09004185 WARN_ON(qc == NULL);
4186 WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187
4188 /* sleep-wait for BSY to clear */
4189 DPRINTK("busy wait\n");
Albert Leed8fe4522005-12-05 15:42:17 +08004190 if (ata_busy_sleep(ap, ATA_TMOUT_CDB_QUICK, ATA_TMOUT_CDB)) {
Tejun Heo11a56d22006-01-23 13:09:36 +09004191 qc->err_mask |= AC_ERR_TIMEOUT;
Albert Leed8fe4522005-12-05 15:42:17 +08004192 goto err_out;
4193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194
4195 /* make sure DRQ is set */
4196 status = ata_chk_status(ap);
Albert Leed8fe4522005-12-05 15:42:17 +08004197 if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ) {
Tejun Heo11a56d22006-01-23 13:09:36 +09004198 qc->err_mask |= AC_ERR_HSM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 goto err_out;
Albert Leed8fe4522005-12-05 15:42:17 +08004200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201
4202 /* send SCSI cdb */
4203 DPRINTK("send cdb\n");
Tejun Heoa46314742006-02-11 19:11:13 +09004204 WARN_ON(ap->cdb_len < 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205
Tejun Heoc1389502005-08-22 14:59:24 +09004206 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA ||
4207 qc->tf.protocol == ATA_PROT_ATAPI_NODATA) {
4208 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004209
Tejun Heoc1389502005-08-22 14:59:24 +09004210 /* Once we're done issuing command and kicking bmdma,
4211 * irq handler takes over. To not lose irq, we need
4212 * to clear NOINTR flag before sending cdb, but
4213 * interrupt handler shouldn't be invoked before we're
4214 * finished. Hence, the following locking.
4215 */
4216 spin_lock_irqsave(&ap->host_set->lock, flags);
4217 ap->flags &= ~ATA_FLAG_NOINTR;
4218 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
4219 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA)
4220 ap->ops->bmdma_start(qc); /* initiate bmdma */
4221 spin_unlock_irqrestore(&ap->host_set->lock, flags);
4222 } else {
4223 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004224
Tejun Heoc1389502005-08-22 14:59:24 +09004225 /* PIO commands are handled by polling */
Albert Lee14be71f2005-09-27 17:36:35 +08004226 ap->hsm_task_state = HSM_ST;
Tejun Heo95064372006-01-23 13:09:37 +09004227 ata_queue_pio_task(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228 }
4229
4230 return;
4231
4232err_out:
Albert Leea22e2eb2005-12-05 15:38:02 +08004233 ata_poll_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234}
4235
Edward Falk0baab862005-06-02 18:17:13 -04004236
Jens Axboe9b847542006-01-06 09:28:07 +01004237/*
4238 * Execute a 'simple' command, that only consists of the opcode 'cmd' itself,
4239 * without filling any other registers
4240 */
4241static int ata_do_simple_cmd(struct ata_port *ap, struct ata_device *dev,
4242 u8 cmd)
4243{
4244 struct ata_taskfile tf;
4245 int err;
4246
4247 ata_tf_init(ap, &tf, dev->devno);
4248
4249 tf.command = cmd;
4250 tf.flags |= ATA_TFLAG_DEVICE;
4251 tf.protocol = ATA_PROT_NODATA;
4252
4253 err = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
4254 if (err)
4255 printk(KERN_ERR "%s: ata command failed: %d\n",
4256 __FUNCTION__, err);
4257
4258 return err;
4259}
4260
4261static int ata_flush_cache(struct ata_port *ap, struct ata_device *dev)
4262{
4263 u8 cmd;
4264
4265 if (!ata_try_flush_cache(dev))
4266 return 0;
4267
4268 if (ata_id_has_flush_ext(dev->id))
4269 cmd = ATA_CMD_FLUSH_EXT;
4270 else
4271 cmd = ATA_CMD_FLUSH;
4272
4273 return ata_do_simple_cmd(ap, dev, cmd);
4274}
4275
4276static int ata_standby_drive(struct ata_port *ap, struct ata_device *dev)
4277{
4278 return ata_do_simple_cmd(ap, dev, ATA_CMD_STANDBYNOW1);
4279}
4280
4281static int ata_start_drive(struct ata_port *ap, struct ata_device *dev)
4282{
4283 return ata_do_simple_cmd(ap, dev, ATA_CMD_IDLEIMMEDIATE);
4284}
4285
4286/**
4287 * ata_device_resume - wakeup a previously suspended devices
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004288 * @ap: port the device is connected to
4289 * @dev: the device to resume
Jens Axboe9b847542006-01-06 09:28:07 +01004290 *
4291 * Kick the drive back into action, by sending it an idle immediate
4292 * command and making sure its transfer mode matches between drive
4293 * and host.
4294 *
4295 */
4296int ata_device_resume(struct ata_port *ap, struct ata_device *dev)
4297{
4298 if (ap->flags & ATA_FLAG_SUSPENDED) {
4299 ap->flags &= ~ATA_FLAG_SUSPENDED;
4300 ata_set_mode(ap);
4301 }
4302 if (!ata_dev_present(dev))
4303 return 0;
4304 if (dev->class == ATA_DEV_ATA)
4305 ata_start_drive(ap, dev);
4306
4307 return 0;
4308}
4309
4310/**
4311 * ata_device_suspend - prepare a device for suspend
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004312 * @ap: port the device is connected to
4313 * @dev: the device to suspend
Jens Axboe9b847542006-01-06 09:28:07 +01004314 *
4315 * Flush the cache on the drive, if appropriate, then issue a
4316 * standbynow command.
Jens Axboe9b847542006-01-06 09:28:07 +01004317 */
4318int ata_device_suspend(struct ata_port *ap, struct ata_device *dev)
4319{
4320 if (!ata_dev_present(dev))
4321 return 0;
4322 if (dev->class == ATA_DEV_ATA)
4323 ata_flush_cache(ap, dev);
4324
4325 ata_standby_drive(ap, dev);
4326 ap->flags |= ATA_FLAG_SUSPENDED;
4327 return 0;
4328}
4329
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004330/**
4331 * ata_port_start - Set port up for dma.
4332 * @ap: Port to initialize
4333 *
4334 * Called just after data structures for each port are
4335 * initialized. Allocates space for PRD table.
4336 *
4337 * May be used as the port_start() entry in ata_port_operations.
4338 *
4339 * LOCKING:
4340 * Inherited from caller.
4341 */
4342
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343int ata_port_start (struct ata_port *ap)
4344{
4345 struct device *dev = ap->host_set->dev;
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004346 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347
4348 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
4349 if (!ap->prd)
4350 return -ENOMEM;
4351
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004352 rc = ata_pad_alloc(ap, dev);
4353 if (rc) {
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004354 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004355 return rc;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004356 }
4357
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
4359
4360 return 0;
4361}
4362
Edward Falk0baab862005-06-02 18:17:13 -04004363
4364/**
4365 * ata_port_stop - Undo ata_port_start()
4366 * @ap: Port to shut down
4367 *
4368 * Frees the PRD table.
4369 *
4370 * May be used as the port_stop() entry in ata_port_operations.
4371 *
4372 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004373 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04004374 */
4375
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376void ata_port_stop (struct ata_port *ap)
4377{
4378 struct device *dev = ap->host_set->dev;
4379
4380 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004381 ata_pad_free(ap, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382}
4383
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04004384void ata_host_stop (struct ata_host_set *host_set)
4385{
4386 if (host_set->mmio_base)
4387 iounmap(host_set->mmio_base);
4388}
4389
4390
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391/**
4392 * ata_host_remove - Unregister SCSI host structure with upper layers
4393 * @ap: Port to unregister
4394 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
4395 *
4396 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004397 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398 */
4399
4400static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
4401{
4402 struct Scsi_Host *sh = ap->host;
4403
4404 DPRINTK("ENTER\n");
4405
4406 if (do_unregister)
4407 scsi_remove_host(sh);
4408
4409 ap->ops->port_stop(ap);
4410}
4411
4412/**
4413 * ata_host_init - Initialize an ata_port structure
4414 * @ap: Structure to initialize
4415 * @host: associated SCSI mid-layer structure
4416 * @host_set: Collection of hosts to which @ap belongs
4417 * @ent: Probe information provided by low-level driver
4418 * @port_no: Port number associated with this ata_port
4419 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004420 * Initialize a new ata_port structure, and its associated
4421 * scsi_host.
4422 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004424 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425 */
4426
4427static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
4428 struct ata_host_set *host_set,
Jeff Garzik057ace52005-10-22 14:27:05 -04004429 const struct ata_probe_ent *ent, unsigned int port_no)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430{
4431 unsigned int i;
4432
4433 host->max_id = 16;
4434 host->max_lun = 1;
4435 host->max_channel = 1;
4436 host->unique_id = ata_unique_id++;
4437 host->max_cmd_len = 12;
Christoph Hellwig12413192005-06-11 01:05:01 +02004438
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 ap->flags = ATA_FLAG_PORT_DISABLED;
4440 ap->id = host->unique_id;
4441 ap->host = host;
4442 ap->ctl = ATA_DEVCTL_OBS;
4443 ap->host_set = host_set;
4444 ap->port_no = port_no;
4445 ap->hard_port_no =
4446 ent->legacy_mode ? ent->hard_port_no : port_no;
4447 ap->pio_mask = ent->pio_mask;
4448 ap->mwdma_mask = ent->mwdma_mask;
4449 ap->udma_mask = ent->udma_mask;
4450 ap->flags |= ent->host_flags;
4451 ap->ops = ent->port_ops;
4452 ap->cbl = ATA_CBL_NONE;
4453 ap->active_tag = ATA_TAG_POISON;
4454 ap->last_ctl = 0xFF;
4455
4456 INIT_WORK(&ap->packet_task, atapi_packet_task, ap);
4457 INIT_WORK(&ap->pio_task, ata_pio_task, ap);
Tejun Heoa72ec4c2006-01-23 13:09:37 +09004458 INIT_LIST_HEAD(&ap->eh_done_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459
4460 for (i = 0; i < ATA_MAX_DEVICES; i++)
4461 ap->device[i].devno = i;
4462
4463#ifdef ATA_IRQ_TRAP
4464 ap->stats.unhandled_irq = 1;
4465 ap->stats.idle_irq = 1;
4466#endif
4467
4468 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
4469}
4470
4471/**
4472 * ata_host_add - Attach low-level ATA driver to system
4473 * @ent: Information provided by low-level driver
4474 * @host_set: Collections of ports to which we add
4475 * @port_no: Port number associated with this host
4476 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004477 * Attach low-level ATA driver to system.
4478 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004480 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481 *
4482 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004483 * New ata_port on success, for NULL on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484 */
4485
Jeff Garzik057ace52005-10-22 14:27:05 -04004486static struct ata_port * ata_host_add(const struct ata_probe_ent *ent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487 struct ata_host_set *host_set,
4488 unsigned int port_no)
4489{
4490 struct Scsi_Host *host;
4491 struct ata_port *ap;
4492 int rc;
4493
4494 DPRINTK("ENTER\n");
4495 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
4496 if (!host)
4497 return NULL;
4498
4499 ap = (struct ata_port *) &host->hostdata[0];
4500
4501 ata_host_init(ap, host, host_set, ent, port_no);
4502
4503 rc = ap->ops->port_start(ap);
4504 if (rc)
4505 goto err_out;
4506
4507 return ap;
4508
4509err_out:
4510 scsi_host_put(host);
4511 return NULL;
4512}
4513
4514/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004515 * ata_device_add - Register hardware device with ATA and SCSI layers
4516 * @ent: Probe information describing hardware device to be registered
4517 *
4518 * This function processes the information provided in the probe
4519 * information struct @ent, allocates the necessary ATA and SCSI
4520 * host information structures, initializes them, and registers
4521 * everything with requisite kernel subsystems.
4522 *
4523 * This function requests irqs, probes the ATA bus, and probes
4524 * the SCSI bus.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525 *
4526 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004527 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528 *
4529 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004530 * Number of ports registered. Zero on error (no ports registered).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531 */
4532
Jeff Garzik057ace52005-10-22 14:27:05 -04004533int ata_device_add(const struct ata_probe_ent *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534{
4535 unsigned int count = 0, i;
4536 struct device *dev = ent->dev;
4537 struct ata_host_set *host_set;
4538
4539 DPRINTK("ENTER\n");
4540 /* alloc a container for our list of ATA ports (buses) */
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004541 host_set = kzalloc(sizeof(struct ata_host_set) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004542 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
4543 if (!host_set)
4544 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545 spin_lock_init(&host_set->lock);
4546
4547 host_set->dev = dev;
4548 host_set->n_ports = ent->n_ports;
4549 host_set->irq = ent->irq;
4550 host_set->mmio_base = ent->mmio_base;
4551 host_set->private_data = ent->private_data;
4552 host_set->ops = ent->port_ops;
4553
4554 /* register each port bound to this device */
4555 for (i = 0; i < ent->n_ports; i++) {
4556 struct ata_port *ap;
4557 unsigned long xfer_mode_mask;
4558
4559 ap = ata_host_add(ent, host_set, i);
4560 if (!ap)
4561 goto err_out;
4562
4563 host_set->ports[i] = ap;
4564 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
4565 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
4566 (ap->pio_mask << ATA_SHIFT_PIO);
4567
4568 /* print per-port info to dmesg */
4569 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
4570 "bmdma 0x%lX irq %lu\n",
4571 ap->id,
4572 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
4573 ata_mode_string(xfer_mode_mask),
4574 ap->ioaddr.cmd_addr,
4575 ap->ioaddr.ctl_addr,
4576 ap->ioaddr.bmdma_addr,
4577 ent->irq);
4578
4579 ata_chk_status(ap);
4580 host_set->ops->irq_clear(ap);
4581 count++;
4582 }
4583
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004584 if (!count)
4585 goto err_free_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586
4587 /* obtain irq, that is shared between channels */
4588 if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
4589 DRV_NAME, host_set))
4590 goto err_out;
4591
4592 /* perform each probe synchronously */
4593 DPRINTK("probe begin\n");
4594 for (i = 0; i < count; i++) {
4595 struct ata_port *ap;
4596 int rc;
4597
4598 ap = host_set->ports[i];
4599
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004600 DPRINTK("ata%u: bus probe begin\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601 rc = ata_bus_probe(ap);
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004602 DPRINTK("ata%u: bus probe end\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603
4604 if (rc) {
4605 /* FIXME: do something useful here?
4606 * Current libata behavior will
4607 * tear down everything when
4608 * the module is removed
4609 * or the h/w is unplugged.
4610 */
4611 }
4612
4613 rc = scsi_add_host(ap->host, dev);
4614 if (rc) {
4615 printk(KERN_ERR "ata%u: scsi_add_host failed\n",
4616 ap->id);
4617 /* FIXME: do something useful here */
4618 /* FIXME: handle unconditional calls to
4619 * scsi_scan_host and ata_host_remove, below,
4620 * at the very least
4621 */
4622 }
4623 }
4624
4625 /* probes are done, now scan each port's disk(s) */
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004626 DPRINTK("host probe begin\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627 for (i = 0; i < count; i++) {
4628 struct ata_port *ap = host_set->ports[i];
4629
Jeff Garzik644dd0c2005-10-03 15:55:19 -04004630 ata_scsi_scan_host(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631 }
4632
4633 dev_set_drvdata(dev, host_set);
4634
4635 VPRINTK("EXIT, returning %u\n", ent->n_ports);
4636 return ent->n_ports; /* success */
4637
4638err_out:
4639 for (i = 0; i < count; i++) {
4640 ata_host_remove(host_set->ports[i], 1);
4641 scsi_host_put(host_set->ports[i]->host);
4642 }
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004643err_free_ret:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644 kfree(host_set);
4645 VPRINTK("EXIT, returning 0\n");
4646 return 0;
4647}
4648
4649/**
Alan Cox17b14452005-09-15 15:44:00 +01004650 * ata_host_set_remove - PCI layer callback for device removal
4651 * @host_set: ATA host set that was removed
4652 *
4653 * Unregister all objects associated with this host set. Free those
4654 * objects.
4655 *
4656 * LOCKING:
4657 * Inherited from calling layer (may sleep).
4658 */
4659
Alan Cox17b14452005-09-15 15:44:00 +01004660void ata_host_set_remove(struct ata_host_set *host_set)
4661{
4662 struct ata_port *ap;
4663 unsigned int i;
4664
4665 for (i = 0; i < host_set->n_ports; i++) {
4666 ap = host_set->ports[i];
4667 scsi_remove_host(ap->host);
4668 }
4669
4670 free_irq(host_set->irq, host_set);
4671
4672 for (i = 0; i < host_set->n_ports; i++) {
4673 ap = host_set->ports[i];
4674
4675 ata_scsi_release(ap->host);
4676
4677 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
4678 struct ata_ioports *ioaddr = &ap->ioaddr;
4679
4680 if (ioaddr->cmd_addr == 0x1f0)
4681 release_region(0x1f0, 8);
4682 else if (ioaddr->cmd_addr == 0x170)
4683 release_region(0x170, 8);
4684 }
4685
4686 scsi_host_put(ap->host);
4687 }
4688
4689 if (host_set->ops->host_stop)
4690 host_set->ops->host_stop(host_set);
4691
4692 kfree(host_set);
4693}
4694
4695/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696 * ata_scsi_release - SCSI layer callback hook for host unload
4697 * @host: libata host to be unloaded
4698 *
4699 * Performs all duties necessary to shut down a libata port...
4700 * Kill port kthread, disable port, and release resources.
4701 *
4702 * LOCKING:
4703 * Inherited from SCSI layer.
4704 *
4705 * RETURNS:
4706 * One.
4707 */
4708
4709int ata_scsi_release(struct Scsi_Host *host)
4710{
4711 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
4712
4713 DPRINTK("ENTER\n");
4714
4715 ap->ops->port_disable(ap);
4716 ata_host_remove(ap, 0);
4717
4718 DPRINTK("EXIT\n");
4719 return 1;
4720}
4721
4722/**
4723 * ata_std_ports - initialize ioaddr with standard port offsets.
4724 * @ioaddr: IO address structure to be initialized
Edward Falk0baab862005-06-02 18:17:13 -04004725 *
4726 * Utility function which initializes data_addr, error_addr,
4727 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
4728 * device_addr, status_addr, and command_addr to standard offsets
4729 * relative to cmd_addr.
4730 *
4731 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732 */
Edward Falk0baab862005-06-02 18:17:13 -04004733
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734void ata_std_ports(struct ata_ioports *ioaddr)
4735{
4736 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
4737 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
4738 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
4739 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
4740 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
4741 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
4742 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
4743 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
4744 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
4745 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
4746}
4747
Edward Falk0baab862005-06-02 18:17:13 -04004748
Jeff Garzik374b1872005-08-30 05:42:52 -04004749#ifdef CONFIG_PCI
4750
4751void ata_pci_host_stop (struct ata_host_set *host_set)
4752{
4753 struct pci_dev *pdev = to_pci_dev(host_set->dev);
4754
4755 pci_iounmap(pdev, host_set->mmio_base);
4756}
4757
Edward Falk0baab862005-06-02 18:17:13 -04004758/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759 * ata_pci_remove_one - PCI layer callback for device removal
4760 * @pdev: PCI device that was removed
4761 *
4762 * PCI layer indicates to libata via this hook that
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004763 * hot-unplug or module unload event has occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764 * Handle this by unregistering all objects associated
4765 * with this PCI device. Free those objects. Then finally
4766 * release PCI resources and disable device.
4767 *
4768 * LOCKING:
4769 * Inherited from PCI layer (may sleep).
4770 */
4771
4772void ata_pci_remove_one (struct pci_dev *pdev)
4773{
4774 struct device *dev = pci_dev_to_dev(pdev);
4775 struct ata_host_set *host_set = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776
Alan Cox17b14452005-09-15 15:44:00 +01004777 ata_host_set_remove(host_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778 pci_release_regions(pdev);
4779 pci_disable_device(pdev);
4780 dev_set_drvdata(dev, NULL);
4781}
4782
4783/* move to PCI subsystem */
Jeff Garzik057ace52005-10-22 14:27:05 -04004784int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785{
4786 unsigned long tmp = 0;
4787
4788 switch (bits->width) {
4789 case 1: {
4790 u8 tmp8 = 0;
4791 pci_read_config_byte(pdev, bits->reg, &tmp8);
4792 tmp = tmp8;
4793 break;
4794 }
4795 case 2: {
4796 u16 tmp16 = 0;
4797 pci_read_config_word(pdev, bits->reg, &tmp16);
4798 tmp = tmp16;
4799 break;
4800 }
4801 case 4: {
4802 u32 tmp32 = 0;
4803 pci_read_config_dword(pdev, bits->reg, &tmp32);
4804 tmp = tmp32;
4805 break;
4806 }
4807
4808 default:
4809 return -EINVAL;
4810 }
4811
4812 tmp &= bits->mask;
4813
4814 return (tmp == bits->val) ? 1 : 0;
4815}
Jens Axboe9b847542006-01-06 09:28:07 +01004816
4817int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t state)
4818{
4819 pci_save_state(pdev);
4820 pci_disable_device(pdev);
4821 pci_set_power_state(pdev, PCI_D3hot);
4822 return 0;
4823}
4824
4825int ata_pci_device_resume(struct pci_dev *pdev)
4826{
4827 pci_set_power_state(pdev, PCI_D0);
4828 pci_restore_state(pdev);
4829 pci_enable_device(pdev);
4830 pci_set_master(pdev);
4831 return 0;
4832}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833#endif /* CONFIG_PCI */
4834
4835
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836static int __init ata_init(void)
4837{
4838 ata_wq = create_workqueue("ata");
4839 if (!ata_wq)
4840 return -ENOMEM;
4841
4842 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
4843 return 0;
4844}
4845
4846static void __exit ata_exit(void)
4847{
4848 destroy_workqueue(ata_wq);
4849}
4850
4851module_init(ata_init);
4852module_exit(ata_exit);
4853
Jeff Garzik67846b32005-10-05 02:58:32 -04004854static unsigned long ratelimit_time;
4855static spinlock_t ata_ratelimit_lock = SPIN_LOCK_UNLOCKED;
4856
4857int ata_ratelimit(void)
4858{
4859 int rc;
4860 unsigned long flags;
4861
4862 spin_lock_irqsave(&ata_ratelimit_lock, flags);
4863
4864 if (time_after(jiffies, ratelimit_time)) {
4865 rc = 1;
4866 ratelimit_time = jiffies + (HZ/5);
4867 } else
4868 rc = 0;
4869
4870 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
4871
4872 return rc;
4873}
4874
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875/*
4876 * libata is essentially a library of internal helper functions for
4877 * low-level ATA host controller drivers. As such, the API/ABI is
4878 * likely to change as new drivers are added and updated.
4879 * Do not depend on ABI/API stability.
4880 */
4881
4882EXPORT_SYMBOL_GPL(ata_std_bios_param);
4883EXPORT_SYMBOL_GPL(ata_std_ports);
4884EXPORT_SYMBOL_GPL(ata_device_add);
Alan Cox17b14452005-09-15 15:44:00 +01004885EXPORT_SYMBOL_GPL(ata_host_set_remove);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886EXPORT_SYMBOL_GPL(ata_sg_init);
4887EXPORT_SYMBOL_GPL(ata_sg_init_one);
Tejun Heo76014422006-02-11 15:13:49 +09004888EXPORT_SYMBOL_GPL(__ata_qc_complete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
4890EXPORT_SYMBOL_GPL(ata_eng_timeout);
4891EXPORT_SYMBOL_GPL(ata_tf_load);
4892EXPORT_SYMBOL_GPL(ata_tf_read);
4893EXPORT_SYMBOL_GPL(ata_noop_dev_select);
4894EXPORT_SYMBOL_GPL(ata_std_dev_select);
4895EXPORT_SYMBOL_GPL(ata_tf_to_fis);
4896EXPORT_SYMBOL_GPL(ata_tf_from_fis);
4897EXPORT_SYMBOL_GPL(ata_check_status);
4898EXPORT_SYMBOL_GPL(ata_altstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004899EXPORT_SYMBOL_GPL(ata_exec_command);
4900EXPORT_SYMBOL_GPL(ata_port_start);
4901EXPORT_SYMBOL_GPL(ata_port_stop);
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04004902EXPORT_SYMBOL_GPL(ata_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903EXPORT_SYMBOL_GPL(ata_interrupt);
4904EXPORT_SYMBOL_GPL(ata_qc_prep);
4905EXPORT_SYMBOL_GPL(ata_bmdma_setup);
4906EXPORT_SYMBOL_GPL(ata_bmdma_start);
4907EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
4908EXPORT_SYMBOL_GPL(ata_bmdma_status);
4909EXPORT_SYMBOL_GPL(ata_bmdma_stop);
4910EXPORT_SYMBOL_GPL(ata_port_probe);
4911EXPORT_SYMBOL_GPL(sata_phy_reset);
4912EXPORT_SYMBOL_GPL(__sata_phy_reset);
4913EXPORT_SYMBOL_GPL(ata_bus_reset);
Tejun Heo8a19ac82006-02-02 18:20:00 +09004914EXPORT_SYMBOL_GPL(ata_std_probeinit);
Tejun Heoc2bd5802006-01-24 17:05:22 +09004915EXPORT_SYMBOL_GPL(ata_std_softreset);
4916EXPORT_SYMBOL_GPL(sata_std_hardreset);
4917EXPORT_SYMBOL_GPL(ata_std_postreset);
4918EXPORT_SYMBOL_GPL(ata_std_probe_reset);
Tejun Heoa62c0fc2006-01-24 17:05:22 +09004919EXPORT_SYMBOL_GPL(ata_drive_probe_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004920EXPORT_SYMBOL_GPL(ata_port_disable);
Jeff Garzik67846b32005-10-05 02:58:32 -04004921EXPORT_SYMBOL_GPL(ata_ratelimit);
Tejun Heo6f8b9952006-01-24 17:05:21 +09004922EXPORT_SYMBOL_GPL(ata_busy_sleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
4924EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
Tejun Heof29841e2006-02-10 15:10:48 +09004925EXPORT_SYMBOL_GPL(ata_scsi_timed_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004926EXPORT_SYMBOL_GPL(ata_scsi_error);
4927EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
4928EXPORT_SYMBOL_GPL(ata_scsi_release);
4929EXPORT_SYMBOL_GPL(ata_host_intr);
4930EXPORT_SYMBOL_GPL(ata_dev_classify);
4931EXPORT_SYMBOL_GPL(ata_dev_id_string);
Tejun Heo0e949ff2006-02-12 22:47:04 +09004932EXPORT_SYMBOL_GPL(ata_dev_id_c_string);
Brad Campbell6f2f3812005-05-12 15:07:47 -04004933EXPORT_SYMBOL_GPL(ata_dev_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934EXPORT_SYMBOL_GPL(ata_scsi_simulate);
Tejun Heoa72ec4c2006-01-23 13:09:37 +09004935EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
4936EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937
Alan Cox1bc4ccf2006-01-09 17:18:14 +00004938EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
Alan Cox452503f2005-10-21 19:01:32 -04004939EXPORT_SYMBOL_GPL(ata_timing_compute);
4940EXPORT_SYMBOL_GPL(ata_timing_merge);
4941
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942#ifdef CONFIG_PCI
4943EXPORT_SYMBOL_GPL(pci_test_config_bits);
Jeff Garzik374b1872005-08-30 05:42:52 -04004944EXPORT_SYMBOL_GPL(ata_pci_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
4946EXPORT_SYMBOL_GPL(ata_pci_init_one);
4947EXPORT_SYMBOL_GPL(ata_pci_remove_one);
Jens Axboe9b847542006-01-06 09:28:07 +01004948EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
4949EXPORT_SYMBOL_GPL(ata_pci_device_resume);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950#endif /* CONFIG_PCI */
Jens Axboe9b847542006-01-06 09:28:07 +01004951
4952EXPORT_SYMBOL_GPL(ata_device_suspend);
4953EXPORT_SYMBOL_GPL(ata_device_resume);
4954EXPORT_SYMBOL_GPL(ata_scsi_device_suspend);
4955EXPORT_SYMBOL_GPL(ata_scsi_device_resume);