blob: 21ae752cd95c20c31e7b70f2c0992f2600c8423a [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 Lee8bf62ec2005-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 Lee8bf62ec2005-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
Tejun Heo61eb0662006-02-12 23:22:37 +0900942 if (ap->ops->probe_reset ||
943 ap->flags & (ATA_FLAG_SRST | ATA_FLAG_SATA_RESET))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 using_edd = 0;
945 else
946 using_edd = 1;
947
948 DPRINTK("ENTER, host %u, dev %u\n", ap->id, device);
949
Tejun Heoa4631472006-02-11 19:11:13 +0900950 WARN_ON(dev->class != ATA_DEV_ATA && dev->class != ATA_DEV_ATAPI &&
951 dev->class != ATA_DEV_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
953 ata_dev_select(ap, device, 1, 1); /* select device 0/1 */
954
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955retry:
Tejun Heoa0123702005-12-13 14:49:31 +0900956 ata_tf_init(ap, &tf, device);
957
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 if (dev->class == ATA_DEV_ATA) {
Tejun Heoa0123702005-12-13 14:49:31 +0900959 tf.command = ATA_CMD_ID_ATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 DPRINTK("do ATA identify\n");
961 } else {
Tejun Heoa0123702005-12-13 14:49:31 +0900962 tf.command = ATA_CMD_ID_ATAPI;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 DPRINTK("do ATAPI identify\n");
964 }
965
Tejun Heoa0123702005-12-13 14:49:31 +0900966 tf.protocol = ATA_PROT_PIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
Tejun Heoa0123702005-12-13 14:49:31 +0900968 err_mask = ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE,
969 dev->id, sizeof(dev->id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Tejun Heoa0123702005-12-13 14:49:31 +0900971 if (err_mask) {
972 if (err_mask & ~AC_ERR_DEV)
973 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 /*
976 * arg! EDD works for all test cases, but seems to return
977 * the ATA signature for some ATAPI devices. Until the
978 * reason for this is found and fixed, we fix up the mess
979 * here. If IDENTIFY DEVICE returns command aborted
980 * (as ATAPI devices do), then we issue an
981 * IDENTIFY PACKET DEVICE.
982 *
983 * ATA software reset (SRST, the default) does not appear
984 * to have this problem.
985 */
Albert Lee7c398332005-11-09 13:03:30 +0800986 if ((using_edd) && (dev->class == ATA_DEV_ATA)) {
Tejun Heoa0123702005-12-13 14:49:31 +0900987 u8 err = tf.feature;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 if (err & ATA_ABORTED) {
989 dev->class = ATA_DEV_ATAPI;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 goto retry;
991 }
992 }
993 goto err_out;
994 }
995
996 swap_buf_le16(dev->id, ATA_ID_WORDS);
997
998 /* print device capabilities */
999 printk(KERN_DEBUG "ata%u: dev %u cfg "
1000 "49:%04x 82:%04x 83:%04x 84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1001 ap->id, device, dev->id[49],
1002 dev->id[82], dev->id[83], dev->id[84],
1003 dev->id[85], dev->id[86], dev->id[87],
1004 dev->id[88]);
1005
1006 /*
1007 * common ATA, ATAPI feature tests
1008 */
1009
Albert Lee8bf62ec2005-05-12 15:29:42 -04001010 /* we require DMA support (bits 8 of word 49) */
1011 if (!ata_id_has_dma(dev->id)) {
1012 printk(KERN_DEBUG "ata%u: no dma\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 goto err_out_nosup;
1014 }
1015
1016 /* quick-n-dirty find max transfer mode; for printk only */
1017 xfer_modes = dev->id[ATA_ID_UDMA_MODES];
1018 if (!xfer_modes)
1019 xfer_modes = (dev->id[ATA_ID_MWDMA_MODES]) << ATA_SHIFT_MWDMA;
Alan Cox11e29e22005-10-21 18:46:32 -04001020 if (!xfer_modes)
1021 xfer_modes = ata_pio_modes(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
Tejun Heo0bd33002006-02-12 22:47:05 +09001023 ata_dump_id(dev->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
1025 /* ATA-specific feature tests */
1026 if (dev->class == ATA_DEV_ATA) {
Tejun Heo29407402006-02-12 22:47:04 +09001027 dev->n_sectors = ata_id_n_sectors(dev->id);
1028
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 if (!ata_id_is_ata(dev->id)) /* sanity check */
1030 goto err_out_nosup;
1031
Albert Lee8bf62ec2005-05-12 15:29:42 -04001032 /* get major version */
Tejun Heo3d2ca912006-02-12 22:47:04 +09001033 major_version = ata_id_major_version(dev->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
Albert Lee8bf62ec2005-05-12 15:29:42 -04001035 /*
1036 * The exact sequence expected by certain pre-ATA4 drives is:
1037 * SRST RESET
1038 * IDENTIFY
1039 * INITIALIZE DEVICE PARAMETERS
1040 * anything else..
1041 * Some drives were very specific about that exact sequence.
1042 */
Albert Lee59a10b12005-10-12 15:09:42 +08001043 if (major_version < 4 || (!ata_id_has_lba(dev->id))) {
Albert Lee8bf62ec2005-05-12 15:29:42 -04001044 ata_dev_init_params(ap, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
Albert Lee59a10b12005-10-12 15:09:42 +08001046 /* current CHS translation info (id[53-58]) might be
1047 * changed. reread the identify device info.
1048 */
1049 ata_dev_reread_id(ap, dev);
1050 }
1051
Albert Lee8bf62ec2005-05-12 15:29:42 -04001052 if (ata_id_has_lba(dev->id)) {
1053 dev->flags |= ATA_DFLAG_LBA;
1054
Tejun Heo29407402006-02-12 22:47:04 +09001055 if (ata_id_has_lba48(dev->id))
Albert Lee8bf62ec2005-05-12 15:29:42 -04001056 dev->flags |= ATA_DFLAG_LBA48;
Albert Lee8bf62ec2005-05-12 15:29:42 -04001057
1058 /* print device info to dmesg */
1059 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors:%s\n",
1060 ap->id, device,
1061 major_version,
1062 ata_mode_string(xfer_modes),
1063 (unsigned long long)dev->n_sectors,
1064 dev->flags & ATA_DFLAG_LBA48 ? " LBA48" : " LBA");
1065 } else {
1066 /* CHS */
1067
1068 /* Default translation */
1069 dev->cylinders = dev->id[1];
1070 dev->heads = dev->id[3];
1071 dev->sectors = dev->id[6];
Albert Lee8bf62ec2005-05-12 15:29:42 -04001072
1073 if (ata_id_current_chs_valid(dev->id)) {
1074 /* Current CHS translation is valid. */
1075 dev->cylinders = dev->id[54];
1076 dev->heads = dev->id[55];
1077 dev->sectors = dev->id[56];
Albert Lee8bf62ec2005-05-12 15:29:42 -04001078 }
1079
1080 /* print device info to dmesg */
1081 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors: CHS %d/%d/%d\n",
1082 ap->id, device,
1083 major_version,
1084 ata_mode_string(xfer_modes),
1085 (unsigned long long)dev->n_sectors,
1086 (int)dev->cylinders, (int)dev->heads, (int)dev->sectors);
1087
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 }
1089
1090 ap->host->max_cmd_len = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 }
1092
1093 /* ATAPI-specific feature tests */
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05001094 else if (dev->class == ATA_DEV_ATAPI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 if (ata_id_is_ata(dev->id)) /* sanity check */
1096 goto err_out_nosup;
1097
1098 rc = atapi_cdb_len(dev->id);
1099 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
1100 printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
1101 goto err_out_nosup;
1102 }
1103 ap->cdb_len = (unsigned int) rc;
1104 ap->host->max_cmd_len = (unsigned char) ap->cdb_len;
1105
1106 /* print device info to dmesg */
1107 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
1108 ap->id, device,
1109 ata_mode_string(xfer_modes));
1110 }
1111
1112 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
1113 return;
1114
1115err_out_nosup:
1116 printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n",
1117 ap->id, device);
1118err_out:
1119 dev->class++; /* converts ATA_DEV_xxx into ATA_DEV_xxx_UNSUP */
1120 DPRINTK("EXIT, err\n");
1121}
1122
Brad Campbell6f2f3812005-05-12 15:07:47 -04001123
Jeff Garzik057ace52005-10-22 14:27:05 -04001124static inline u8 ata_dev_knobble(const struct ata_port *ap)
Brad Campbell6f2f3812005-05-12 15:07:47 -04001125{
1126 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(ap->device->id)));
1127}
1128
1129/**
Randy Dunlapc893a3a2006-01-28 13:15:32 -05001130 * ata_dev_config - Run device specific handlers & check for SATA->PATA bridges
1131 * @ap: Bus
1132 * @i: Device
Brad Campbell6f2f3812005-05-12 15:07:47 -04001133 *
Randy Dunlapc893a3a2006-01-28 13:15:32 -05001134 * LOCKING:
Brad Campbell6f2f3812005-05-12 15:07:47 -04001135 */
Jeff Garzik8a60a072005-07-31 13:13:24 -04001136
Brad Campbell6f2f3812005-05-12 15:07:47 -04001137void ata_dev_config(struct ata_port *ap, unsigned int i)
1138{
1139 /* limit bridge transfers to udma5, 200 sectors */
1140 if (ata_dev_knobble(ap)) {
1141 printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
1142 ap->id, ap->device->devno);
1143 ap->udma_mask &= ATA_UDMA5;
1144 ap->host->max_sectors = ATA_MAX_SECTORS;
1145 ap->host->hostt->max_sectors = ATA_MAX_SECTORS;
Alan Cox9d824d02006-01-17 20:51:55 +00001146 ap->device[i].flags |= ATA_DFLAG_LOCK_SECTORS;
Brad Campbell6f2f3812005-05-12 15:07:47 -04001147 }
1148
1149 if (ap->ops->dev_config)
1150 ap->ops->dev_config(ap, &ap->device[i]);
1151}
1152
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153/**
1154 * ata_bus_probe - Reset and probe ATA bus
1155 * @ap: Bus to probe
1156 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001157 * Master ATA bus probing function. Initiates a hardware-dependent
1158 * bus reset, then attempts to identify any devices found on
1159 * the bus.
1160 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001162 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 *
1164 * RETURNS:
1165 * Zero on success, non-zero on error.
1166 */
1167
1168static int ata_bus_probe(struct ata_port *ap)
1169{
1170 unsigned int i, found = 0;
1171
Tejun Heoc19ba8a2006-01-24 17:05:22 +09001172 if (ap->ops->probe_reset) {
1173 unsigned int classes[ATA_MAX_DEVICES];
1174 int rc;
1175
1176 ata_port_probe(ap);
1177
1178 rc = ap->ops->probe_reset(ap, classes);
1179 if (rc == 0) {
Tejun Heo06ab7822006-02-12 15:01:49 +09001180 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1181 if (classes[i] == ATA_DEV_UNKNOWN)
1182 classes[i] = ATA_DEV_NONE;
Tejun Heoc19ba8a2006-01-24 17:05:22 +09001183 ap->device[i].class = classes[i];
Tejun Heo06ab7822006-02-12 15:01:49 +09001184 }
Tejun Heoc19ba8a2006-01-24 17:05:22 +09001185 } else {
1186 printk(KERN_ERR "ata%u: probe reset failed, "
1187 "disabling port\n", ap->id);
1188 ata_port_disable(ap);
1189 }
1190 } else
1191 ap->ops->phy_reset(ap);
1192
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1194 goto err_out;
1195
1196 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1197 ata_dev_identify(ap, i);
1198 if (ata_dev_present(&ap->device[i])) {
1199 found = 1;
Brad Campbell6f2f3812005-05-12 15:07:47 -04001200 ata_dev_config(ap,i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 }
1202 }
1203
1204 if ((!found) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1205 goto err_out_disable;
1206
1207 ata_set_mode(ap);
1208 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1209 goto err_out_disable;
1210
1211 return 0;
1212
1213err_out_disable:
1214 ap->ops->port_disable(ap);
1215err_out:
1216 return -1;
1217}
1218
1219/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001220 * ata_port_probe - Mark port as enabled
1221 * @ap: Port for which we indicate enablement
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001223 * Modify @ap data structure such that the system
1224 * thinks that the entire port is enabled.
1225 *
1226 * LOCKING: host_set lock, or some other form of
1227 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 */
1229
1230void ata_port_probe(struct ata_port *ap)
1231{
1232 ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1233}
1234
1235/**
Tejun Heo3be680b2005-12-19 22:35:02 +09001236 * sata_print_link_status - Print SATA link status
1237 * @ap: SATA port to printk link status about
1238 *
1239 * This function prints link speed and status of a SATA link.
1240 *
1241 * LOCKING:
1242 * None.
1243 */
1244static void sata_print_link_status(struct ata_port *ap)
1245{
1246 u32 sstatus, tmp;
1247 const char *speed;
1248
1249 if (!ap->ops->scr_read)
1250 return;
1251
1252 sstatus = scr_read(ap, SCR_STATUS);
1253
1254 if (sata_dev_present(ap)) {
1255 tmp = (sstatus >> 4) & 0xf;
1256 if (tmp & (1 << 0))
1257 speed = "1.5";
1258 else if (tmp & (1 << 1))
1259 speed = "3.0";
1260 else
1261 speed = "<unknown>";
1262 printk(KERN_INFO "ata%u: SATA link up %s Gbps (SStatus %X)\n",
1263 ap->id, speed, sstatus);
1264 } else {
1265 printk(KERN_INFO "ata%u: SATA link down (SStatus %X)\n",
1266 ap->id, sstatus);
1267 }
1268}
1269
1270/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001271 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1272 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001274 * This function issues commands to standard SATA Sxxx
1275 * PHY registers, to wake up the phy (and device), and
1276 * clear any reset condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 *
1278 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001279 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 *
1281 */
1282void __sata_phy_reset(struct ata_port *ap)
1283{
1284 u32 sstatus;
1285 unsigned long timeout = jiffies + (HZ * 5);
1286
1287 if (ap->flags & ATA_FLAG_SATA_RESET) {
Brett Russcdcca892005-03-28 15:10:27 -05001288 /* issue phy wake/reset */
1289 scr_write_flush(ap, SCR_CONTROL, 0x301);
Tejun Heo62ba2842005-06-26 23:27:19 +09001290 /* Couldn't find anything in SATA I/II specs, but
1291 * AHCI-1.1 10.4.2 says at least 1 ms. */
1292 mdelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 }
Brett Russcdcca892005-03-28 15:10:27 -05001294 scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
1296 /* wait for phy to become ready, if necessary */
1297 do {
1298 msleep(200);
1299 sstatus = scr_read(ap, SCR_STATUS);
1300 if ((sstatus & 0xf) != 1)
1301 break;
1302 } while (time_before(jiffies, timeout));
1303
Tejun Heo3be680b2005-12-19 22:35:02 +09001304 /* print link status */
1305 sata_print_link_status(ap);
Jeff Garzik656563e2005-11-20 03:36:45 -05001306
Tejun Heo3be680b2005-12-19 22:35:02 +09001307 /* TODO: phy layer with polling, timeouts, etc. */
1308 if (sata_dev_present(ap))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 ata_port_probe(ap);
Tejun Heo3be680b2005-12-19 22:35:02 +09001310 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 ata_port_disable(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
1313 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1314 return;
1315
1316 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1317 ata_port_disable(ap);
1318 return;
1319 }
1320
1321 ap->cbl = ATA_CBL_SATA;
1322}
1323
1324/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001325 * sata_phy_reset - Reset SATA bus.
1326 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001328 * This function resets the SATA bus, and then probes
1329 * the bus for devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 *
1331 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001332 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 *
1334 */
1335void sata_phy_reset(struct ata_port *ap)
1336{
1337 __sata_phy_reset(ap);
1338 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1339 return;
1340 ata_bus_reset(ap);
1341}
1342
1343/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001344 * ata_port_disable - Disable port.
1345 * @ap: Port to be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001347 * Modify @ap data structure such that the system
1348 * thinks that the entire port is disabled, and should
1349 * never attempt to probe or communicate with devices
1350 * on this port.
1351 *
1352 * LOCKING: host_set lock, or some other form of
1353 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 */
1355
1356void ata_port_disable(struct ata_port *ap)
1357{
1358 ap->device[0].class = ATA_DEV_NONE;
1359 ap->device[1].class = ATA_DEV_NONE;
1360 ap->flags |= ATA_FLAG_PORT_DISABLED;
1361}
1362
Alan Cox452503f2005-10-21 19:01:32 -04001363/*
1364 * This mode timing computation functionality is ported over from
1365 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
1366 */
1367/*
1368 * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
1369 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
1370 * for PIO 5, which is a nonstandard extension and UDMA6, which
1371 * is currently supported only by Maxtor drives.
1372 */
1373
1374static const struct ata_timing ata_timing[] = {
1375
1376 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
1377 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
1378 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
1379 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
1380
1381 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
1382 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
1383 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
1384
1385/* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
1386
1387 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
1388 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
1389 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
1390
1391 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
1392 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
1393 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
1394
1395/* { XFER_PIO_5, 20, 50, 30, 100, 50, 30, 100, 0 }, */
1396 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
1397 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
1398
1399 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
1400 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
1401 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
1402
1403/* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
1404
1405 { 0xFF }
1406};
1407
1408#define ENOUGH(v,unit) (((v)-1)/(unit)+1)
1409#define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
1410
1411static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
1412{
1413 q->setup = EZ(t->setup * 1000, T);
1414 q->act8b = EZ(t->act8b * 1000, T);
1415 q->rec8b = EZ(t->rec8b * 1000, T);
1416 q->cyc8b = EZ(t->cyc8b * 1000, T);
1417 q->active = EZ(t->active * 1000, T);
1418 q->recover = EZ(t->recover * 1000, T);
1419 q->cycle = EZ(t->cycle * 1000, T);
1420 q->udma = EZ(t->udma * 1000, UT);
1421}
1422
1423void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
1424 struct ata_timing *m, unsigned int what)
1425{
1426 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
1427 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
1428 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
1429 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
1430 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
1431 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
1432 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
1433 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
1434}
1435
1436static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
1437{
1438 const struct ata_timing *t;
1439
1440 for (t = ata_timing; t->mode != speed; t++)
Alan Cox91190752005-10-26 12:17:46 -04001441 if (t->mode == 0xFF)
Alan Cox452503f2005-10-21 19:01:32 -04001442 return NULL;
1443 return t;
1444}
1445
1446int ata_timing_compute(struct ata_device *adev, unsigned short speed,
1447 struct ata_timing *t, int T, int UT)
1448{
1449 const struct ata_timing *s;
1450 struct ata_timing p;
1451
1452 /*
1453 * Find the mode.
Albert Lee75b1f2f2005-11-16 17:06:18 +08001454 */
Alan Cox452503f2005-10-21 19:01:32 -04001455
1456 if (!(s = ata_timing_find_mode(speed)))
1457 return -EINVAL;
1458
Albert Lee75b1f2f2005-11-16 17:06:18 +08001459 memcpy(t, s, sizeof(*s));
1460
Alan Cox452503f2005-10-21 19:01:32 -04001461 /*
1462 * If the drive is an EIDE drive, it can tell us it needs extended
1463 * PIO/MW_DMA cycle timing.
1464 */
1465
1466 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
1467 memset(&p, 0, sizeof(p));
1468 if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
1469 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
1470 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
1471 } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
1472 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
1473 }
1474 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
1475 }
1476
1477 /*
1478 * Convert the timing to bus clock counts.
1479 */
1480
Albert Lee75b1f2f2005-11-16 17:06:18 +08001481 ata_timing_quantize(t, t, T, UT);
Alan Cox452503f2005-10-21 19:01:32 -04001482
1483 /*
Randy Dunlapc893a3a2006-01-28 13:15:32 -05001484 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
1485 * S.M.A.R.T * and some other commands. We have to ensure that the
1486 * DMA cycle timing is slower/equal than the fastest PIO timing.
Alan Cox452503f2005-10-21 19:01:32 -04001487 */
1488
1489 if (speed > XFER_PIO_4) {
1490 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
1491 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
1492 }
1493
1494 /*
Randy Dunlapc893a3a2006-01-28 13:15:32 -05001495 * Lengthen active & recovery time so that cycle time is correct.
Alan Cox452503f2005-10-21 19:01:32 -04001496 */
1497
1498 if (t->act8b + t->rec8b < t->cyc8b) {
1499 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
1500 t->rec8b = t->cyc8b - t->act8b;
1501 }
1502
1503 if (t->active + t->recover < t->cycle) {
1504 t->active += (t->cycle - (t->active + t->recover)) / 2;
1505 t->recover = t->cycle - t->active;
1506 }
1507
1508 return 0;
1509}
1510
Jeff Garzik057ace52005-10-22 14:27:05 -04001511static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 unsigned int shift;
1513 u8 base;
1514} xfer_mode_classes[] = {
1515 { ATA_SHIFT_UDMA, XFER_UDMA_0 },
1516 { ATA_SHIFT_MWDMA, XFER_MW_DMA_0 },
1517 { ATA_SHIFT_PIO, XFER_PIO_0 },
1518};
1519
Arjan van de Ven858119e2006-01-14 13:20:43 -08001520static u8 base_from_shift(unsigned int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521{
1522 int i;
1523
1524 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++)
1525 if (xfer_mode_classes[i].shift == shift)
1526 return xfer_mode_classes[i].base;
1527
1528 return 0xff;
1529}
1530
1531static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
1532{
1533 int ofs, idx;
1534 u8 base;
1535
1536 if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1537 return;
1538
1539 if (dev->xfer_shift == ATA_SHIFT_PIO)
1540 dev->flags |= ATA_DFLAG_PIO;
1541
1542 ata_dev_set_xfermode(ap, dev);
1543
1544 base = base_from_shift(dev->xfer_shift);
1545 ofs = dev->xfer_mode - base;
1546 idx = ofs + dev->xfer_shift;
1547 WARN_ON(idx >= ARRAY_SIZE(xfer_mode_str));
1548
1549 DPRINTK("idx=%d xfer_shift=%u, xfer_mode=0x%x, base=0x%x, offset=%d\n",
1550 idx, dev->xfer_shift, (int)dev->xfer_mode, (int)base, ofs);
1551
1552 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
1553 ap->id, dev->devno, xfer_mode_str[idx]);
1554}
1555
1556static int ata_host_set_pio(struct ata_port *ap)
1557{
1558 unsigned int mask;
1559 int x, i;
1560 u8 base, xfer_mode;
1561
1562 mask = ata_get_mode_mask(ap, ATA_SHIFT_PIO);
1563 x = fgb(mask);
1564 if (x < 0) {
1565 printk(KERN_WARNING "ata%u: no PIO support\n", ap->id);
1566 return -1;
1567 }
1568
1569 base = base_from_shift(ATA_SHIFT_PIO);
1570 xfer_mode = base + x;
1571
1572 DPRINTK("base 0x%x xfer_mode 0x%x mask 0x%x x %d\n",
1573 (int)base, (int)xfer_mode, mask, x);
1574
1575 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1576 struct ata_device *dev = &ap->device[i];
1577 if (ata_dev_present(dev)) {
1578 dev->pio_mode = xfer_mode;
1579 dev->xfer_mode = xfer_mode;
1580 dev->xfer_shift = ATA_SHIFT_PIO;
1581 if (ap->ops->set_piomode)
1582 ap->ops->set_piomode(ap, dev);
1583 }
1584 }
1585
1586 return 0;
1587}
1588
1589static void ata_host_set_dma(struct ata_port *ap, u8 xfer_mode,
1590 unsigned int xfer_shift)
1591{
1592 int i;
1593
1594 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1595 struct ata_device *dev = &ap->device[i];
1596 if (ata_dev_present(dev)) {
1597 dev->dma_mode = xfer_mode;
1598 dev->xfer_mode = xfer_mode;
1599 dev->xfer_shift = xfer_shift;
1600 if (ap->ops->set_dmamode)
1601 ap->ops->set_dmamode(ap, dev);
1602 }
1603 }
1604}
1605
1606/**
1607 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1608 * @ap: port on which timings will be programmed
1609 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001610 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1611 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001613 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 */
1615static void ata_set_mode(struct ata_port *ap)
1616{
Albert Lee8cbd6df2005-10-12 15:06:27 +08001617 unsigned int xfer_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 u8 xfer_mode;
1619 int rc;
1620
1621 /* step 1: always set host PIO timings */
1622 rc = ata_host_set_pio(ap);
1623 if (rc)
1624 goto err_out;
1625
1626 /* step 2: choose the best data xfer mode */
1627 xfer_mode = xfer_shift = 0;
1628 rc = ata_choose_xfer_mode(ap, &xfer_mode, &xfer_shift);
1629 if (rc)
1630 goto err_out;
1631
1632 /* step 3: if that xfer mode isn't PIO, set host DMA timings */
1633 if (xfer_shift != ATA_SHIFT_PIO)
1634 ata_host_set_dma(ap, xfer_mode, xfer_shift);
1635
1636 /* step 4: update devices' xfer mode */
1637 ata_dev_set_mode(ap, &ap->device[0]);
1638 ata_dev_set_mode(ap, &ap->device[1]);
1639
1640 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1641 return;
1642
1643 if (ap->ops->post_set_mode)
1644 ap->ops->post_set_mode(ap);
1645
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646 return;
1647
1648err_out:
1649 ata_port_disable(ap);
1650}
1651
1652/**
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05001653 * ata_tf_to_host - issue ATA taskfile to host controller
1654 * @ap: port to which command is being issued
1655 * @tf: ATA taskfile register set
1656 *
1657 * Issues ATA taskfile register set to ATA host controller,
1658 * with proper synchronization with interrupt handler and
1659 * other threads.
1660 *
1661 * LOCKING:
1662 * spin_lock_irqsave(host_set lock)
1663 */
1664
1665static inline void ata_tf_to_host(struct ata_port *ap,
1666 const struct ata_taskfile *tf)
1667{
1668 ap->ops->tf_load(ap, tf);
1669 ap->ops->exec_command(ap, tf);
1670}
1671
1672/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 * ata_busy_sleep - sleep until BSY clears, or timeout
1674 * @ap: port containing status register to be polled
1675 * @tmout_pat: impatience timeout
1676 * @tmout: overall timeout
1677 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001678 * Sleep until ATA Status register bit BSY clears,
1679 * or a timeout occurs.
1680 *
1681 * LOCKING: None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 */
1683
Tejun Heo6f8b9952006-01-24 17:05:21 +09001684unsigned int ata_busy_sleep (struct ata_port *ap,
1685 unsigned long tmout_pat, unsigned long tmout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686{
1687 unsigned long timer_start, timeout;
1688 u8 status;
1689
1690 status = ata_busy_wait(ap, ATA_BUSY, 300);
1691 timer_start = jiffies;
1692 timeout = timer_start + tmout_pat;
1693 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1694 msleep(50);
1695 status = ata_busy_wait(ap, ATA_BUSY, 3);
1696 }
1697
1698 if (status & ATA_BUSY)
1699 printk(KERN_WARNING "ata%u is slow to respond, "
1700 "please be patient\n", ap->id);
1701
1702 timeout = timer_start + tmout;
1703 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1704 msleep(50);
1705 status = ata_chk_status(ap);
1706 }
1707
1708 if (status & ATA_BUSY) {
1709 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
1710 ap->id, tmout / HZ);
1711 return 1;
1712 }
1713
1714 return 0;
1715}
1716
1717static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
1718{
1719 struct ata_ioports *ioaddr = &ap->ioaddr;
1720 unsigned int dev0 = devmask & (1 << 0);
1721 unsigned int dev1 = devmask & (1 << 1);
1722 unsigned long timeout;
1723
1724 /* if device 0 was found in ata_devchk, wait for its
1725 * BSY bit to clear
1726 */
1727 if (dev0)
1728 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1729
1730 /* if device 1 was found in ata_devchk, wait for
1731 * register access, then wait for BSY to clear
1732 */
1733 timeout = jiffies + ATA_TMOUT_BOOT;
1734 while (dev1) {
1735 u8 nsect, lbal;
1736
1737 ap->ops->dev_select(ap, 1);
1738 if (ap->flags & ATA_FLAG_MMIO) {
1739 nsect = readb((void __iomem *) ioaddr->nsect_addr);
1740 lbal = readb((void __iomem *) ioaddr->lbal_addr);
1741 } else {
1742 nsect = inb(ioaddr->nsect_addr);
1743 lbal = inb(ioaddr->lbal_addr);
1744 }
1745 if ((nsect == 1) && (lbal == 1))
1746 break;
1747 if (time_after(jiffies, timeout)) {
1748 dev1 = 0;
1749 break;
1750 }
1751 msleep(50); /* give drive a breather */
1752 }
1753 if (dev1)
1754 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1755
1756 /* is all this really necessary? */
1757 ap->ops->dev_select(ap, 0);
1758 if (dev1)
1759 ap->ops->dev_select(ap, 1);
1760 if (dev0)
1761 ap->ops->dev_select(ap, 0);
1762}
1763
1764/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001765 * ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
1766 * @ap: Port to reset and probe
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001768 * Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
1769 * probe the bus. Not often used these days.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 *
1771 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001772 * PCI/etc. bus probe sem.
Jeff Garzike5338252005-10-30 21:37:17 -05001773 * Obtains host_set lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 *
1775 */
1776
1777static unsigned int ata_bus_edd(struct ata_port *ap)
1778{
1779 struct ata_taskfile tf;
Jeff Garzike5338252005-10-30 21:37:17 -05001780 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781
1782 /* set up execute-device-diag (bus reset) taskfile */
1783 /* also, take interrupts to a known state (disabled) */
1784 DPRINTK("execute-device-diag\n");
1785 ata_tf_init(ap, &tf, 0);
1786 tf.ctl |= ATA_NIEN;
1787 tf.command = ATA_CMD_EDD;
1788 tf.protocol = ATA_PROT_NODATA;
1789
1790 /* do bus reset */
Jeff Garzike5338252005-10-30 21:37:17 -05001791 spin_lock_irqsave(&ap->host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 ata_tf_to_host(ap, &tf);
Jeff Garzike5338252005-10-30 21:37:17 -05001793 spin_unlock_irqrestore(&ap->host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794
1795 /* spec says at least 2ms. but who knows with those
1796 * crazy ATAPI devices...
1797 */
1798 msleep(150);
1799
1800 return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1801}
1802
1803static unsigned int ata_bus_softreset(struct ata_port *ap,
1804 unsigned int devmask)
1805{
1806 struct ata_ioports *ioaddr = &ap->ioaddr;
1807
1808 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
1809
1810 /* software reset. causes dev0 to be selected */
1811 if (ap->flags & ATA_FLAG_MMIO) {
1812 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1813 udelay(20); /* FIXME: flush */
1814 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
1815 udelay(20); /* FIXME: flush */
1816 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1817 } else {
1818 outb(ap->ctl, ioaddr->ctl_addr);
1819 udelay(10);
1820 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
1821 udelay(10);
1822 outb(ap->ctl, ioaddr->ctl_addr);
1823 }
1824
1825 /* spec mandates ">= 2ms" before checking status.
1826 * We wait 150ms, because that was the magic delay used for
1827 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
1828 * between when the ATA command register is written, and then
1829 * status is checked. Because waiting for "a while" before
1830 * checking status is fine, post SRST, we perform this magic
1831 * delay here as well.
1832 */
1833 msleep(150);
1834
1835 ata_bus_post_reset(ap, devmask);
1836
1837 return 0;
1838}
1839
1840/**
1841 * ata_bus_reset - reset host port and associated ATA channel
1842 * @ap: port to reset
1843 *
1844 * This is typically the first time we actually start issuing
1845 * commands to the ATA channel. We wait for BSY to clear, then
1846 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
1847 * result. Determine what devices, if any, are on the channel
1848 * by looking at the device 0/1 error register. Look at the signature
1849 * stored in each device's taskfile registers, to determine if
1850 * the device is ATA or ATAPI.
1851 *
1852 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001853 * PCI/etc. bus probe sem.
1854 * Obtains host_set lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 *
1856 * SIDE EFFECTS:
1857 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
1858 */
1859
1860void ata_bus_reset(struct ata_port *ap)
1861{
1862 struct ata_ioports *ioaddr = &ap->ioaddr;
1863 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
1864 u8 err;
1865 unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
1866
1867 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
1868
1869 /* determine if device 0/1 are present */
1870 if (ap->flags & ATA_FLAG_SATA_RESET)
1871 dev0 = 1;
1872 else {
1873 dev0 = ata_devchk(ap, 0);
1874 if (slave_possible)
1875 dev1 = ata_devchk(ap, 1);
1876 }
1877
1878 if (dev0)
1879 devmask |= (1 << 0);
1880 if (dev1)
1881 devmask |= (1 << 1);
1882
1883 /* select device 0 again */
1884 ap->ops->dev_select(ap, 0);
1885
1886 /* issue bus reset */
1887 if (ap->flags & ATA_FLAG_SRST)
1888 rc = ata_bus_softreset(ap, devmask);
1889 else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
1890 /* set up device control */
1891 if (ap->flags & ATA_FLAG_MMIO)
1892 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1893 else
1894 outb(ap->ctl, ioaddr->ctl_addr);
1895 rc = ata_bus_edd(ap);
1896 }
1897
1898 if (rc)
1899 goto err_out;
1900
1901 /*
1902 * determine by signature whether we have ATA or ATAPI devices
1903 */
Tejun Heob4dc7622006-01-24 17:05:22 +09001904 ap->device[0].class = ata_dev_try_classify(ap, 0, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 if ((slave_possible) && (err != 0x81))
Tejun Heob4dc7622006-01-24 17:05:22 +09001906 ap->device[1].class = ata_dev_try_classify(ap, 1, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907
1908 /* re-enable interrupts */
1909 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
1910 ata_irq_on(ap);
1911
1912 /* is double-select really necessary? */
1913 if (ap->device[1].class != ATA_DEV_NONE)
1914 ap->ops->dev_select(ap, 1);
1915 if (ap->device[0].class != ATA_DEV_NONE)
1916 ap->ops->dev_select(ap, 0);
1917
1918 /* if no devices were detected, disable this port */
1919 if ((ap->device[0].class == ATA_DEV_NONE) &&
1920 (ap->device[1].class == ATA_DEV_NONE))
1921 goto err_out;
1922
1923 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
1924 /* set up device control for ATA_FLAG_SATA_RESET */
1925 if (ap->flags & ATA_FLAG_MMIO)
1926 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1927 else
1928 outb(ap->ctl, ioaddr->ctl_addr);
1929 }
1930
1931 DPRINTK("EXIT\n");
1932 return;
1933
1934err_out:
1935 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
1936 ap->ops->port_disable(ap);
1937
1938 DPRINTK("EXIT\n");
1939}
1940
Tejun Heo7a7921e2006-02-02 18:20:00 +09001941static int sata_phy_resume(struct ata_port *ap)
1942{
1943 unsigned long timeout = jiffies + (HZ * 5);
1944 u32 sstatus;
1945
1946 scr_write_flush(ap, SCR_CONTROL, 0x300);
1947
1948 /* Wait for phy to become ready, if necessary. */
1949 do {
1950 msleep(200);
1951 sstatus = scr_read(ap, SCR_STATUS);
1952 if ((sstatus & 0xf) != 1)
1953 return 0;
1954 } while (time_before(jiffies, timeout));
1955
1956 return -1;
1957}
1958
Tejun Heoc2bd5802006-01-24 17:05:22 +09001959/**
Tejun Heo8a19ac82006-02-02 18:20:00 +09001960 * ata_std_probeinit - initialize probing
1961 * @ap: port to be probed
1962 *
1963 * @ap is about to be probed. Initialize it. This function is
1964 * to be used as standard callback for ata_drive_probe_reset().
Tejun Heo3a397462006-02-10 23:58:48 +09001965 *
1966 * NOTE!!! Do not use this function as probeinit if a low level
1967 * driver implements only hardreset. Just pass NULL as probeinit
1968 * in that case. Using this function is probably okay but doing
1969 * so makes reset sequence different from the original
1970 * ->phy_reset implementation and Jeff nervous. :-P
Tejun Heo8a19ac82006-02-02 18:20:00 +09001971 */
1972extern void ata_std_probeinit(struct ata_port *ap)
1973{
Tejun Heo3a397462006-02-10 23:58:48 +09001974 if (ap->flags & ATA_FLAG_SATA && ap->ops->scr_read) {
Tejun Heo8a19ac82006-02-02 18:20:00 +09001975 sata_phy_resume(ap);
Tejun Heo3a397462006-02-10 23:58:48 +09001976 if (sata_dev_present(ap))
1977 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1978 }
Tejun Heo8a19ac82006-02-02 18:20:00 +09001979}
1980
1981/**
Tejun Heoc2bd5802006-01-24 17:05:22 +09001982 * ata_std_softreset - reset host port via ATA SRST
1983 * @ap: port to reset
1984 * @verbose: fail verbosely
1985 * @classes: resulting classes of attached devices
1986 *
1987 * Reset host port using ATA SRST. This function is to be used
1988 * as standard callback for ata_drive_*_reset() functions.
1989 *
1990 * LOCKING:
1991 * Kernel thread context (may sleep)
1992 *
1993 * RETURNS:
1994 * 0 on success, -errno otherwise.
1995 */
1996int ata_std_softreset(struct ata_port *ap, int verbose, unsigned int *classes)
1997{
1998 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
1999 unsigned int devmask = 0, err_mask;
2000 u8 err;
2001
2002 DPRINTK("ENTER\n");
2003
Tejun Heo3a397462006-02-10 23:58:48 +09002004 if (ap->ops->scr_read && !sata_dev_present(ap)) {
2005 classes[0] = ATA_DEV_NONE;
2006 goto out;
2007 }
2008
Tejun Heoc2bd5802006-01-24 17:05:22 +09002009 /* determine if device 0/1 are present */
2010 if (ata_devchk(ap, 0))
2011 devmask |= (1 << 0);
2012 if (slave_possible && ata_devchk(ap, 1))
2013 devmask |= (1 << 1);
2014
Tejun Heoc2bd5802006-01-24 17:05:22 +09002015 /* select device 0 again */
2016 ap->ops->dev_select(ap, 0);
2017
2018 /* issue bus reset */
2019 DPRINTK("about to softreset, devmask=%x\n", devmask);
2020 err_mask = ata_bus_softreset(ap, devmask);
2021 if (err_mask) {
2022 if (verbose)
2023 printk(KERN_ERR "ata%u: SRST failed (err_mask=0x%x)\n",
2024 ap->id, err_mask);
2025 else
2026 DPRINTK("EXIT, softreset failed (err_mask=0x%x)\n",
2027 err_mask);
2028 return -EIO;
2029 }
2030
2031 /* determine by signature whether we have ATA or ATAPI devices */
2032 classes[0] = ata_dev_try_classify(ap, 0, &err);
2033 if (slave_possible && err != 0x81)
2034 classes[1] = ata_dev_try_classify(ap, 1, &err);
2035
Tejun Heo3a397462006-02-10 23:58:48 +09002036 out:
Tejun Heoc2bd5802006-01-24 17:05:22 +09002037 DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]);
2038 return 0;
2039}
2040
2041/**
2042 * sata_std_hardreset - reset host port via SATA phy reset
2043 * @ap: port to reset
2044 * @verbose: fail verbosely
2045 * @class: resulting class of attached device
2046 *
2047 * SATA phy-reset host port using DET bits of SControl register.
2048 * This function is to be used as standard callback for
2049 * ata_drive_*_reset().
2050 *
2051 * LOCKING:
2052 * Kernel thread context (may sleep)
2053 *
2054 * RETURNS:
2055 * 0 on success, -errno otherwise.
2056 */
2057int sata_std_hardreset(struct ata_port *ap, int verbose, unsigned int *class)
2058{
Tejun Heoc2bd5802006-01-24 17:05:22 +09002059 DPRINTK("ENTER\n");
2060
2061 /* Issue phy wake/reset */
2062 scr_write_flush(ap, SCR_CONTROL, 0x301);
2063
2064 /*
2065 * Couldn't find anything in SATA I/II specs, but AHCI-1.1
2066 * 10.4.2 says at least 1 ms.
2067 */
2068 msleep(1);
2069
Tejun Heo7a7921e2006-02-02 18:20:00 +09002070 /* Bring phy back */
2071 sata_phy_resume(ap);
Tejun Heoc2bd5802006-01-24 17:05:22 +09002072
Tejun Heoc2bd5802006-01-24 17:05:22 +09002073 /* TODO: phy layer with polling, timeouts, etc. */
2074 if (!sata_dev_present(ap)) {
2075 *class = ATA_DEV_NONE;
2076 DPRINTK("EXIT, link offline\n");
2077 return 0;
2078 }
2079
2080 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
2081 if (verbose)
2082 printk(KERN_ERR "ata%u: COMRESET failed "
2083 "(device not ready)\n", ap->id);
2084 else
2085 DPRINTK("EXIT, device not ready\n");
2086 return -EIO;
2087 }
2088
Tejun Heo3a397462006-02-10 23:58:48 +09002089 ap->ops->dev_select(ap, 0); /* probably unnecessary */
2090
Tejun Heoc2bd5802006-01-24 17:05:22 +09002091 *class = ata_dev_try_classify(ap, 0, NULL);
2092
2093 DPRINTK("EXIT, class=%u\n", *class);
2094 return 0;
2095}
2096
2097/**
2098 * ata_std_postreset - standard postreset callback
2099 * @ap: the target ata_port
2100 * @classes: classes of attached devices
2101 *
2102 * This function is invoked after a successful reset. Note that
2103 * the device might have been reset more than once using
2104 * different reset methods before postreset is invoked.
2105 * postreset is also reponsible for setting cable type.
2106 *
2107 * This function is to be used as standard callback for
2108 * ata_drive_*_reset().
2109 *
2110 * LOCKING:
2111 * Kernel thread context (may sleep)
2112 */
2113void ata_std_postreset(struct ata_port *ap, unsigned int *classes)
2114{
2115 DPRINTK("ENTER\n");
2116
2117 /* set cable type */
2118 if (ap->cbl == ATA_CBL_NONE && ap->flags & ATA_FLAG_SATA)
2119 ap->cbl = ATA_CBL_SATA;
2120
2121 /* print link status */
2122 if (ap->cbl == ATA_CBL_SATA)
2123 sata_print_link_status(ap);
2124
Tejun Heo3a397462006-02-10 23:58:48 +09002125 /* re-enable interrupts */
2126 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2127 ata_irq_on(ap);
Tejun Heoc2bd5802006-01-24 17:05:22 +09002128
2129 /* is double-select really necessary? */
2130 if (classes[0] != ATA_DEV_NONE)
2131 ap->ops->dev_select(ap, 1);
2132 if (classes[1] != ATA_DEV_NONE)
2133 ap->ops->dev_select(ap, 0);
2134
Tejun Heo3a397462006-02-10 23:58:48 +09002135 /* bail out if no device is present */
2136 if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
2137 DPRINTK("EXIT, no device\n");
2138 return;
2139 }
2140
2141 /* set up device control */
2142 if (ap->ioaddr.ctl_addr) {
2143 if (ap->flags & ATA_FLAG_MMIO)
2144 writeb(ap->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
2145 else
2146 outb(ap->ctl, ap->ioaddr.ctl_addr);
2147 }
Tejun Heoc2bd5802006-01-24 17:05:22 +09002148
2149 DPRINTK("EXIT\n");
2150}
2151
2152/**
2153 * ata_std_probe_reset - standard probe reset method
2154 * @ap: prot to perform probe-reset
2155 * @classes: resulting classes of attached devices
2156 *
2157 * The stock off-the-shelf ->probe_reset method.
2158 *
2159 * LOCKING:
2160 * Kernel thread context (may sleep)
2161 *
2162 * RETURNS:
2163 * 0 on success, -errno otherwise.
2164 */
2165int ata_std_probe_reset(struct ata_port *ap, unsigned int *classes)
2166{
2167 ata_reset_fn_t hardreset;
2168
2169 hardreset = NULL;
Tejun Heob911fc32006-02-02 18:20:00 +09002170 if (ap->flags & ATA_FLAG_SATA && ap->ops->scr_read)
Tejun Heoc2bd5802006-01-24 17:05:22 +09002171 hardreset = sata_std_hardreset;
2172
Tejun Heo8a19ac82006-02-02 18:20:00 +09002173 return ata_drive_probe_reset(ap, ata_std_probeinit,
Tejun Heo7944ea92006-02-02 18:20:00 +09002174 ata_std_softreset, hardreset,
Tejun Heoc2bd5802006-01-24 17:05:22 +09002175 ata_std_postreset, classes);
2176}
2177
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002178static int do_probe_reset(struct ata_port *ap, ata_reset_fn_t reset,
2179 ata_postreset_fn_t postreset,
2180 unsigned int *classes)
2181{
2182 int i, rc;
2183
2184 for (i = 0; i < ATA_MAX_DEVICES; i++)
2185 classes[i] = ATA_DEV_UNKNOWN;
2186
2187 rc = reset(ap, 0, classes);
2188 if (rc)
2189 return rc;
2190
2191 /* If any class isn't ATA_DEV_UNKNOWN, consider classification
2192 * is complete and convert all ATA_DEV_UNKNOWN to
2193 * ATA_DEV_NONE.
2194 */
2195 for (i = 0; i < ATA_MAX_DEVICES; i++)
2196 if (classes[i] != ATA_DEV_UNKNOWN)
2197 break;
2198
2199 if (i < ATA_MAX_DEVICES)
2200 for (i = 0; i < ATA_MAX_DEVICES; i++)
2201 if (classes[i] == ATA_DEV_UNKNOWN)
2202 classes[i] = ATA_DEV_NONE;
2203
2204 if (postreset)
2205 postreset(ap, classes);
2206
2207 return classes[0] != ATA_DEV_UNKNOWN ? 0 : -ENODEV;
2208}
2209
2210/**
2211 * ata_drive_probe_reset - Perform probe reset with given methods
2212 * @ap: port to reset
Tejun Heo7944ea92006-02-02 18:20:00 +09002213 * @probeinit: probeinit method (can be NULL)
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002214 * @softreset: softreset method (can be NULL)
2215 * @hardreset: hardreset method (can be NULL)
2216 * @postreset: postreset method (can be NULL)
2217 * @classes: resulting classes of attached devices
2218 *
2219 * Reset the specified port and classify attached devices using
2220 * given methods. This function prefers softreset but tries all
2221 * possible reset sequences to reset and classify devices. This
2222 * function is intended to be used for constructing ->probe_reset
2223 * callback by low level drivers.
2224 *
2225 * Reset methods should follow the following rules.
2226 *
2227 * - Return 0 on sucess, -errno on failure.
2228 * - If classification is supported, fill classes[] with
2229 * recognized class codes.
2230 * - If classification is not supported, leave classes[] alone.
2231 * - If verbose is non-zero, print error message on failure;
2232 * otherwise, shut up.
2233 *
2234 * LOCKING:
2235 * Kernel thread context (may sleep)
2236 *
2237 * RETURNS:
2238 * 0 on success, -EINVAL if no reset method is avaliable, -ENODEV
2239 * if classification fails, and any error code from reset
2240 * methods.
2241 */
Tejun Heo7944ea92006-02-02 18:20:00 +09002242int ata_drive_probe_reset(struct ata_port *ap, ata_probeinit_fn_t probeinit,
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002243 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2244 ata_postreset_fn_t postreset, unsigned int *classes)
2245{
2246 int rc = -EINVAL;
2247
Tejun Heo7944ea92006-02-02 18:20:00 +09002248 if (probeinit)
2249 probeinit(ap);
2250
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002251 if (softreset) {
2252 rc = do_probe_reset(ap, softreset, postreset, classes);
2253 if (rc == 0)
2254 return 0;
2255 }
2256
2257 if (!hardreset)
2258 return rc;
2259
2260 rc = do_probe_reset(ap, hardreset, postreset, classes);
2261 if (rc == 0 || rc != -ENODEV)
2262 return rc;
2263
2264 if (softreset)
2265 rc = do_probe_reset(ap, softreset, postreset, classes);
2266
2267 return rc;
2268}
2269
Jeff Garzik057ace52005-10-22 14:27:05 -04002270static void ata_pr_blacklisted(const struct ata_port *ap,
2271 const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272{
2273 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, disabling DMA\n",
2274 ap->id, dev->devno);
2275}
2276
Arjan van de Ven98ac62d2005-11-28 10:06:23 +01002277static const char * const ata_dma_blacklist [] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 "WDC AC11000H",
2279 "WDC AC22100H",
2280 "WDC AC32500H",
2281 "WDC AC33100H",
2282 "WDC AC31600H",
2283 "WDC AC32100H",
2284 "WDC AC23200L",
2285 "Compaq CRD-8241B",
2286 "CRD-8400B",
2287 "CRD-8480B",
2288 "CRD-8482B",
2289 "CRD-84",
2290 "SanDisk SDP3B",
2291 "SanDisk SDP3B-64",
2292 "SANYO CD-ROM CRD",
2293 "HITACHI CDR-8",
2294 "HITACHI CDR-8335",
2295 "HITACHI CDR-8435",
2296 "Toshiba CD-ROM XM-6202B",
Jeff Garzike9222562005-06-28 00:03:37 -04002297 "TOSHIBA CD-ROM XM-1702BC",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 "CD-532E-A",
2299 "E-IDE CD-ROM CR-840",
2300 "CD-ROM Drive/F5A",
2301 "WPI CDD-820",
2302 "SAMSUNG CD-ROM SC-148C",
2303 "SAMSUNG CD-ROM SC",
2304 "SanDisk SDP3B-64",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 "ATAPI CD-ROM DRIVE 40X MAXIMUM",
2306 "_NEC DV5800A",
2307};
2308
Jeff Garzik057ace52005-10-22 14:27:05 -04002309static int ata_dma_blacklisted(const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310{
Tejun Heo2e026712006-02-12 22:47:04 +09002311 unsigned char model_num[41];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 int i;
2313
Tejun Heo2e026712006-02-12 22:47:04 +09002314 ata_dev_id_c_string(dev->id, model_num, ATA_ID_PROD_OFS,
2315 sizeof(model_num));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316
2317 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i++)
Tejun Heo2e026712006-02-12 22:47:04 +09002318 if (!strcmp(ata_dma_blacklist[i], model_num))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319 return 1;
2320
2321 return 0;
2322}
2323
Jeff Garzik057ace52005-10-22 14:27:05 -04002324static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325{
Jeff Garzik057ace52005-10-22 14:27:05 -04002326 const struct ata_device *master, *slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 unsigned int mask;
2328
2329 master = &ap->device[0];
2330 slave = &ap->device[1];
2331
Tejun Heoa4631472006-02-11 19:11:13 +09002332 WARN_ON(!ata_dev_present(master) && !ata_dev_present(slave));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333
2334 if (shift == ATA_SHIFT_UDMA) {
2335 mask = ap->udma_mask;
2336 if (ata_dev_present(master)) {
2337 mask &= (master->id[ATA_ID_UDMA_MODES] & 0xff);
Jeff Garzik057ace52005-10-22 14:27:05 -04002338 if (ata_dma_blacklisted(master)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 mask = 0;
2340 ata_pr_blacklisted(ap, master);
2341 }
2342 }
2343 if (ata_dev_present(slave)) {
2344 mask &= (slave->id[ATA_ID_UDMA_MODES] & 0xff);
Jeff Garzik057ace52005-10-22 14:27:05 -04002345 if (ata_dma_blacklisted(slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 mask = 0;
2347 ata_pr_blacklisted(ap, slave);
2348 }
2349 }
2350 }
2351 else if (shift == ATA_SHIFT_MWDMA) {
2352 mask = ap->mwdma_mask;
2353 if (ata_dev_present(master)) {
2354 mask &= (master->id[ATA_ID_MWDMA_MODES] & 0x07);
Jeff Garzik057ace52005-10-22 14:27:05 -04002355 if (ata_dma_blacklisted(master)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 mask = 0;
2357 ata_pr_blacklisted(ap, master);
2358 }
2359 }
2360 if (ata_dev_present(slave)) {
2361 mask &= (slave->id[ATA_ID_MWDMA_MODES] & 0x07);
Jeff Garzik057ace52005-10-22 14:27:05 -04002362 if (ata_dma_blacklisted(slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 mask = 0;
2364 ata_pr_blacklisted(ap, slave);
2365 }
2366 }
2367 }
2368 else if (shift == ATA_SHIFT_PIO) {
2369 mask = ap->pio_mask;
2370 if (ata_dev_present(master)) {
2371 /* spec doesn't return explicit support for
2372 * PIO0-2, so we fake it
2373 */
2374 u16 tmp_mode = master->id[ATA_ID_PIO_MODES] & 0x03;
2375 tmp_mode <<= 3;
2376 tmp_mode |= 0x7;
2377 mask &= tmp_mode;
2378 }
2379 if (ata_dev_present(slave)) {
2380 /* spec doesn't return explicit support for
2381 * PIO0-2, so we fake it
2382 */
2383 u16 tmp_mode = slave->id[ATA_ID_PIO_MODES] & 0x03;
2384 tmp_mode <<= 3;
2385 tmp_mode |= 0x7;
2386 mask &= tmp_mode;
2387 }
2388 }
2389 else {
2390 mask = 0xffffffff; /* shut up compiler warning */
2391 BUG();
2392 }
2393
2394 return mask;
2395}
2396
2397/* find greatest bit */
2398static int fgb(u32 bitmap)
2399{
2400 unsigned int i;
2401 int x = -1;
2402
2403 for (i = 0; i < 32; i++)
2404 if (bitmap & (1 << i))
2405 x = i;
2406
2407 return x;
2408}
2409
2410/**
2411 * ata_choose_xfer_mode - attempt to find best transfer mode
2412 * @ap: Port for which an xfer mode will be selected
2413 * @xfer_mode_out: (output) SET FEATURES - XFER MODE code
2414 * @xfer_shift_out: (output) bit shift that selects this mode
2415 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04002416 * Based on host and device capabilities, determine the
2417 * maximum transfer mode that is amenable to all.
2418 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002420 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 *
2422 * RETURNS:
2423 * Zero on success, negative on error.
2424 */
2425
Jeff Garzik057ace52005-10-22 14:27:05 -04002426static int ata_choose_xfer_mode(const struct ata_port *ap,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 u8 *xfer_mode_out,
2428 unsigned int *xfer_shift_out)
2429{
2430 unsigned int mask, shift;
2431 int x, i;
2432
2433 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++) {
2434 shift = xfer_mode_classes[i].shift;
2435 mask = ata_get_mode_mask(ap, shift);
2436
2437 x = fgb(mask);
2438 if (x >= 0) {
2439 *xfer_mode_out = xfer_mode_classes[i].base + x;
2440 *xfer_shift_out = shift;
2441 return 0;
2442 }
2443 }
2444
2445 return -1;
2446}
2447
2448/**
2449 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2450 * @ap: Port associated with device @dev
2451 * @dev: Device to which command will be sent
2452 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002453 * Issue SET FEATURES - XFER MODE command to device @dev
2454 * on port @ap.
2455 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002457 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458 */
2459
2460static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
2461{
Tejun Heoa0123702005-12-13 14:49:31 +09002462 struct ata_taskfile tf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002463
2464 /* set up set-features taskfile */
2465 DPRINTK("set features - xfer mode\n");
2466
Tejun Heoa0123702005-12-13 14:49:31 +09002467 ata_tf_init(ap, &tf, dev->devno);
2468 tf.command = ATA_CMD_SET_FEATURES;
2469 tf.feature = SETFEATURES_XFER;
2470 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2471 tf.protocol = ATA_PROT_NODATA;
2472 tf.nsect = dev->xfer_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473
Tejun Heoa0123702005-12-13 14:49:31 +09002474 if (ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0)) {
2475 printk(KERN_ERR "ata%u: failed to set xfermode, disabled\n",
2476 ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002477 ata_port_disable(ap);
Tejun Heoa0123702005-12-13 14:49:31 +09002478 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479
2480 DPRINTK("EXIT\n");
2481}
2482
2483/**
Albert Lee59a10b12005-10-12 15:09:42 +08002484 * ata_dev_reread_id - Reread the device identify device info
2485 * @ap: port where the device is
2486 * @dev: device to reread the identify device info
2487 *
2488 * LOCKING:
2489 */
2490
2491static void ata_dev_reread_id(struct ata_port *ap, struct ata_device *dev)
2492{
Tejun Heoa0123702005-12-13 14:49:31 +09002493 struct ata_taskfile tf;
Albert Lee59a10b12005-10-12 15:09:42 +08002494
Tejun Heoa0123702005-12-13 14:49:31 +09002495 ata_tf_init(ap, &tf, dev->devno);
Albert Lee59a10b12005-10-12 15:09:42 +08002496
2497 if (dev->class == ATA_DEV_ATA) {
Tejun Heoa0123702005-12-13 14:49:31 +09002498 tf.command = ATA_CMD_ID_ATA;
Albert Lee59a10b12005-10-12 15:09:42 +08002499 DPRINTK("do ATA identify\n");
2500 } else {
Tejun Heoa0123702005-12-13 14:49:31 +09002501 tf.command = ATA_CMD_ID_ATAPI;
Albert Lee59a10b12005-10-12 15:09:42 +08002502 DPRINTK("do ATAPI identify\n");
2503 }
2504
Tejun Heoa0123702005-12-13 14:49:31 +09002505 tf.flags |= ATA_TFLAG_DEVICE;
2506 tf.protocol = ATA_PROT_PIO;
Albert Lee59a10b12005-10-12 15:09:42 +08002507
Tejun Heoa0123702005-12-13 14:49:31 +09002508 if (ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE,
2509 dev->id, sizeof(dev->id)))
Albert Lee59a10b12005-10-12 15:09:42 +08002510 goto err_out;
2511
Albert Lee59a10b12005-10-12 15:09:42 +08002512 swap_buf_le16(dev->id, ATA_ID_WORDS);
2513
Tejun Heo0bd33002006-02-12 22:47:05 +09002514 ata_dump_id(dev->id);
Albert Lee59a10b12005-10-12 15:09:42 +08002515
2516 DPRINTK("EXIT\n");
2517
2518 return;
2519err_out:
Tejun Heoa0123702005-12-13 14:49:31 +09002520 printk(KERN_ERR "ata%u: failed to reread ID, disabled\n", ap->id);
Albert Lee59a10b12005-10-12 15:09:42 +08002521 ata_port_disable(ap);
2522}
2523
2524/**
Albert Lee8bf62ec2005-05-12 15:29:42 -04002525 * ata_dev_init_params - Issue INIT DEV PARAMS command
2526 * @ap: Port associated with device @dev
2527 * @dev: Device to which command will be sent
2528 *
2529 * LOCKING:
2530 */
2531
2532static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev)
2533{
Tejun Heoa0123702005-12-13 14:49:31 +09002534 struct ata_taskfile tf;
Albert Lee8bf62ec2005-05-12 15:29:42 -04002535 u16 sectors = dev->id[6];
2536 u16 heads = dev->id[3];
2537
2538 /* Number of sectors per track 1-255. Number of heads 1-16 */
2539 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
2540 return;
2541
2542 /* set up init dev params taskfile */
2543 DPRINTK("init dev params \n");
2544
Tejun Heoa0123702005-12-13 14:49:31 +09002545 ata_tf_init(ap, &tf, dev->devno);
2546 tf.command = ATA_CMD_INIT_DEV_PARAMS;
2547 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2548 tf.protocol = ATA_PROT_NODATA;
2549 tf.nsect = sectors;
2550 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
Albert Lee8bf62ec2005-05-12 15:29:42 -04002551
Tejun Heoa0123702005-12-13 14:49:31 +09002552 if (ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0)) {
2553 printk(KERN_ERR "ata%u: failed to init parameters, disabled\n",
2554 ap->id);
Albert Lee8bf62ec2005-05-12 15:29:42 -04002555 ata_port_disable(ap);
Tejun Heoa0123702005-12-13 14:49:31 +09002556 }
Albert Lee8bf62ec2005-05-12 15:29:42 -04002557
2558 DPRINTK("EXIT\n");
2559}
2560
2561/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002562 * ata_sg_clean - Unmap DMA memory associated with command
2563 * @qc: Command containing DMA memory to be released
2564 *
2565 * Unmap all mapped DMA memory associated with this command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002566 *
2567 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002568 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 */
2570
2571static void ata_sg_clean(struct ata_queued_cmd *qc)
2572{
2573 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002574 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002576 void *pad_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577
Tejun Heoa4631472006-02-11 19:11:13 +09002578 WARN_ON(!(qc->flags & ATA_QCFLAG_DMAMAP));
2579 WARN_ON(sg == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580
2581 if (qc->flags & ATA_QCFLAG_SINGLE)
Tejun Heoa4631472006-02-11 19:11:13 +09002582 WARN_ON(qc->n_elem != 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05002584 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002586 /* if we padded the buffer out to 32-bit bound, and data
2587 * xfer direction is from-device, we must copy from the
2588 * pad buffer back into the supplied buffer
2589 */
2590 if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
2591 pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2592
2593 if (qc->flags & ATA_QCFLAG_SG) {
Jeff Garzike1410f22005-11-14 14:06:26 -05002594 if (qc->n_elem)
2595 dma_unmap_sg(ap->host_set->dev, sg, qc->n_elem, dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002596 /* restore last sg */
2597 sg[qc->orig_n_elem - 1].length += qc->pad_len;
2598 if (pad_buf) {
2599 struct scatterlist *psg = &qc->pad_sgent;
2600 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2601 memcpy(addr + psg->offset, pad_buf, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05002602 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002603 }
2604 } else {
Jeff Garzike1410f22005-11-14 14:06:26 -05002605 if (sg_dma_len(&sg[0]) > 0)
2606 dma_unmap_single(ap->host_set->dev,
2607 sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
2608 dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002609 /* restore sg */
2610 sg->length += qc->pad_len;
2611 if (pad_buf)
2612 memcpy(qc->buf_virt + sg->length - qc->pad_len,
2613 pad_buf, qc->pad_len);
2614 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615
2616 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002617 qc->__sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618}
2619
2620/**
2621 * ata_fill_sg - Fill PCI IDE PRD table
2622 * @qc: Metadata associated with taskfile to be transferred
2623 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002624 * Fill PCI IDE PRD (scatter-gather) table with segments
2625 * associated with the current disk command.
2626 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 * LOCKING:
Jeff Garzik780a87f2005-05-30 15:41:05 -04002628 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 *
2630 */
2631static void ata_fill_sg(struct ata_queued_cmd *qc)
2632{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002633 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002634 struct scatterlist *sg;
2635 unsigned int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636
Tejun Heoa4631472006-02-11 19:11:13 +09002637 WARN_ON(qc->__sg == NULL);
2638 WARN_ON(qc->n_elem == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639
2640 idx = 0;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002641 ata_for_each_sg(sg, qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 u32 addr, offset;
2643 u32 sg_len, len;
2644
2645 /* determine if physical DMA addr spans 64K boundary.
2646 * Note h/w doesn't support 64-bit, so we unconditionally
2647 * truncate dma_addr_t to u32.
2648 */
2649 addr = (u32) sg_dma_address(sg);
2650 sg_len = sg_dma_len(sg);
2651
2652 while (sg_len) {
2653 offset = addr & 0xffff;
2654 len = sg_len;
2655 if ((offset + sg_len) > 0x10000)
2656 len = 0x10000 - offset;
2657
2658 ap->prd[idx].addr = cpu_to_le32(addr);
2659 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
2660 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
2661
2662 idx++;
2663 sg_len -= len;
2664 addr += len;
2665 }
2666 }
2667
2668 if (idx)
2669 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2670}
2671/**
2672 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2673 * @qc: Metadata associated with taskfile to check
2674 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002675 * Allow low-level driver to filter ATA PACKET commands, returning
2676 * a status indicating whether or not it is OK to use DMA for the
2677 * supplied PACKET command.
2678 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002680 * spin_lock_irqsave(host_set lock)
2681 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 * RETURNS: 0 when ATAPI DMA can be used
2683 * nonzero otherwise
2684 */
2685int ata_check_atapi_dma(struct ata_queued_cmd *qc)
2686{
2687 struct ata_port *ap = qc->ap;
2688 int rc = 0; /* Assume ATAPI DMA is OK by default */
2689
2690 if (ap->ops->check_atapi_dma)
2691 rc = ap->ops->check_atapi_dma(qc);
2692
2693 return rc;
2694}
2695/**
2696 * ata_qc_prep - Prepare taskfile for submission
2697 * @qc: Metadata associated with taskfile to be prepared
2698 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002699 * Prepare ATA taskfile for submission.
2700 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701 * LOCKING:
2702 * spin_lock_irqsave(host_set lock)
2703 */
2704void ata_qc_prep(struct ata_queued_cmd *qc)
2705{
2706 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2707 return;
2708
2709 ata_fill_sg(qc);
2710}
2711
Jeff Garzik0cba6322005-05-30 19:49:12 -04002712/**
2713 * ata_sg_init_one - Associate command with memory buffer
2714 * @qc: Command to be associated
2715 * @buf: Memory buffer
2716 * @buflen: Length of memory buffer, in bytes.
2717 *
2718 * Initialize the data-related elements of queued_cmd @qc
2719 * to point to a single memory buffer, @buf of byte length @buflen.
2720 *
2721 * LOCKING:
2722 * spin_lock_irqsave(host_set lock)
2723 */
2724
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
2726{
2727 struct scatterlist *sg;
2728
2729 qc->flags |= ATA_QCFLAG_SINGLE;
2730
2731 memset(&qc->sgent, 0, sizeof(qc->sgent));
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002732 qc->__sg = &qc->sgent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 qc->n_elem = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002734 qc->orig_n_elem = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 qc->buf_virt = buf;
2736
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002737 sg = qc->__sg;
Jeff Garzikf0612bb2005-10-30 01:58:18 -05002738 sg_init_one(sg, buf, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739}
2740
Jeff Garzik0cba6322005-05-30 19:49:12 -04002741/**
2742 * ata_sg_init - Associate command with scatter-gather table.
2743 * @qc: Command to be associated
2744 * @sg: Scatter-gather table.
2745 * @n_elem: Number of elements in s/g table.
2746 *
2747 * Initialize the data-related elements of queued_cmd @qc
2748 * to point to a scatter-gather table @sg, containing @n_elem
2749 * elements.
2750 *
2751 * LOCKING:
2752 * spin_lock_irqsave(host_set lock)
2753 */
2754
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
2756 unsigned int n_elem)
2757{
2758 qc->flags |= ATA_QCFLAG_SG;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002759 qc->__sg = sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760 qc->n_elem = n_elem;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002761 qc->orig_n_elem = n_elem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762}
2763
2764/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002765 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
2766 * @qc: Command with memory buffer to be mapped.
2767 *
2768 * DMA-map the memory buffer associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 *
2770 * LOCKING:
2771 * spin_lock_irqsave(host_set lock)
2772 *
2773 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002774 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 */
2776
2777static int ata_sg_setup_one(struct ata_queued_cmd *qc)
2778{
2779 struct ata_port *ap = qc->ap;
2780 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002781 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 dma_addr_t dma_address;
2783
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002784 /* we must lengthen transfers to end on a 32-bit boundary */
2785 qc->pad_len = sg->length & 3;
2786 if (qc->pad_len) {
2787 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2788 struct scatterlist *psg = &qc->pad_sgent;
2789
Tejun Heoa4631472006-02-11 19:11:13 +09002790 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002791
2792 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2793
2794 if (qc->tf.flags & ATA_TFLAG_WRITE)
2795 memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
2796 qc->pad_len);
2797
2798 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2799 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2800 /* trim sg */
2801 sg->length -= qc->pad_len;
2802
2803 DPRINTK("padding done, sg->length=%u pad_len=%u\n",
2804 sg->length, qc->pad_len);
2805 }
2806
Jeff Garzike1410f22005-11-14 14:06:26 -05002807 if (!sg->length) {
2808 sg_dma_address(sg) = 0;
2809 goto skip_map;
2810 }
2811
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812 dma_address = dma_map_single(ap->host_set->dev, qc->buf_virt,
Albert Lee32529e02005-05-26 03:49:42 -04002813 sg->length, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05002814 if (dma_mapping_error(dma_address)) {
2815 /* restore sg */
2816 sg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05002818 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819
2820 sg_dma_address(sg) = dma_address;
Jeff Garzike1410f22005-11-14 14:06:26 -05002821skip_map:
Albert Lee32529e02005-05-26 03:49:42 -04002822 sg_dma_len(sg) = sg->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823
2824 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
2825 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2826
2827 return 0;
2828}
2829
2830/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002831 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
2832 * @qc: Command with scatter-gather table to be mapped.
2833 *
2834 * DMA-map the scatter-gather table associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 *
2836 * LOCKING:
2837 * spin_lock_irqsave(host_set lock)
2838 *
2839 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002840 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 *
2842 */
2843
2844static int ata_sg_setup(struct ata_queued_cmd *qc)
2845{
2846 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002847 struct scatterlist *sg = qc->__sg;
2848 struct scatterlist *lsg = &sg[qc->n_elem - 1];
Jeff Garzike1410f22005-11-14 14:06:26 -05002849 int n_elem, pre_n_elem, dir, trim_sg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850
2851 VPRINTK("ENTER, ata%u\n", ap->id);
Tejun Heoa4631472006-02-11 19:11:13 +09002852 WARN_ON(!(qc->flags & ATA_QCFLAG_SG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002854 /* we must lengthen transfers to end on a 32-bit boundary */
2855 qc->pad_len = lsg->length & 3;
2856 if (qc->pad_len) {
2857 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2858 struct scatterlist *psg = &qc->pad_sgent;
2859 unsigned int offset;
2860
Tejun Heoa4631472006-02-11 19:11:13 +09002861 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002862
2863 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2864
2865 /*
2866 * psg->page/offset are used to copy to-be-written
2867 * data in this function or read data in ata_sg_clean.
2868 */
2869 offset = lsg->offset + lsg->length - qc->pad_len;
2870 psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
2871 psg->offset = offset_in_page(offset);
2872
2873 if (qc->tf.flags & ATA_TFLAG_WRITE) {
2874 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2875 memcpy(pad_buf, addr + psg->offset, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05002876 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002877 }
2878
2879 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2880 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2881 /* trim last sg */
2882 lsg->length -= qc->pad_len;
Jeff Garzike1410f22005-11-14 14:06:26 -05002883 if (lsg->length == 0)
2884 trim_sg = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002885
2886 DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
2887 qc->n_elem - 1, lsg->length, qc->pad_len);
2888 }
2889
Jeff Garzike1410f22005-11-14 14:06:26 -05002890 pre_n_elem = qc->n_elem;
2891 if (trim_sg && pre_n_elem)
2892 pre_n_elem--;
2893
2894 if (!pre_n_elem) {
2895 n_elem = 0;
2896 goto skip_map;
2897 }
2898
Linus Torvalds1da177e2005-04-16 15:20:36 -07002899 dir = qc->dma_dir;
Jeff Garzike1410f22005-11-14 14:06:26 -05002900 n_elem = dma_map_sg(ap->host_set->dev, sg, pre_n_elem, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05002901 if (n_elem < 1) {
2902 /* restore last sg */
2903 lsg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05002905 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906
2907 DPRINTK("%d sg elements mapped\n", n_elem);
2908
Jeff Garzike1410f22005-11-14 14:06:26 -05002909skip_map:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 qc->n_elem = n_elem;
2911
2912 return 0;
2913}
2914
2915/**
Tejun Heo40e8c822005-08-22 17:12:45 +09002916 * ata_poll_qc_complete - turn irq back on and finish qc
2917 * @qc: Command to complete
Randy Dunlap8e8b77d2005-11-01 21:29:27 -08002918 * @err_mask: ATA status register content
Tejun Heo40e8c822005-08-22 17:12:45 +09002919 *
2920 * LOCKING:
2921 * None. (grabs host lock)
2922 */
2923
Albert Leea22e2eb2005-12-05 15:38:02 +08002924void ata_poll_qc_complete(struct ata_queued_cmd *qc)
Tejun Heo40e8c822005-08-22 17:12:45 +09002925{
2926 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04002927 unsigned long flags;
Tejun Heo40e8c822005-08-22 17:12:45 +09002928
Jeff Garzikb8f61532005-08-25 22:01:20 -04002929 spin_lock_irqsave(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09002930 ap->flags &= ~ATA_FLAG_NOINTR;
2931 ata_irq_on(ap);
Albert Leea22e2eb2005-12-05 15:38:02 +08002932 ata_qc_complete(qc);
Jeff Garzikb8f61532005-08-25 22:01:20 -04002933 spin_unlock_irqrestore(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09002934}
2935
2936/**
Randy Dunlapc893a3a2006-01-28 13:15:32 -05002937 * ata_pio_poll - poll using PIO, depending on current state
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002938 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 *
2940 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002941 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 *
2943 * RETURNS:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002944 * timeout value to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 */
2946
2947static unsigned long ata_pio_poll(struct ata_port *ap)
2948{
Albert Leec14b8332005-12-05 15:36:08 +08002949 struct ata_queued_cmd *qc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 u8 status;
Albert Lee14be71f2005-09-27 17:36:35 +08002951 unsigned int poll_state = HSM_ST_UNKNOWN;
2952 unsigned int reg_state = HSM_ST_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953
Albert Leec14b8332005-12-05 15:36:08 +08002954 qc = ata_qc_from_tag(ap, ap->active_tag);
Tejun Heoa4631472006-02-11 19:11:13 +09002955 WARN_ON(qc == NULL);
Albert Leec14b8332005-12-05 15:36:08 +08002956
Albert Lee14be71f2005-09-27 17:36:35 +08002957 switch (ap->hsm_task_state) {
2958 case HSM_ST:
2959 case HSM_ST_POLL:
2960 poll_state = HSM_ST_POLL;
2961 reg_state = HSM_ST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 break;
Albert Lee14be71f2005-09-27 17:36:35 +08002963 case HSM_ST_LAST:
2964 case HSM_ST_LAST_POLL:
2965 poll_state = HSM_ST_LAST_POLL;
2966 reg_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 break;
2968 default:
2969 BUG();
2970 break;
2971 }
2972
2973 status = ata_chk_status(ap);
2974 if (status & ATA_BUSY) {
2975 if (time_after(jiffies, ap->pio_task_timeout)) {
Tejun Heo11a56d22006-01-23 13:09:36 +09002976 qc->err_mask |= AC_ERR_TIMEOUT;
Albert Lee7c398332005-11-09 13:03:30 +08002977 ap->hsm_task_state = HSM_ST_TMOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 return 0;
2979 }
Albert Lee14be71f2005-09-27 17:36:35 +08002980 ap->hsm_task_state = poll_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 return ATA_SHORT_PAUSE;
2982 }
2983
Albert Lee14be71f2005-09-27 17:36:35 +08002984 ap->hsm_task_state = reg_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 return 0;
2986}
2987
2988/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002989 * ata_pio_complete - check if drive is busy or idle
2990 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 *
2992 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002993 * None. (executing in kernel thread context)
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002994 *
2995 * RETURNS:
2996 * Non-zero if qc completed, zero otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 */
2998
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002999static int ata_pio_complete (struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000{
3001 struct ata_queued_cmd *qc;
3002 u8 drv_stat;
3003
3004 /*
Alan Cox31433ea2005-08-26 15:56:47 +01003005 * This is purely heuristic. This is a fast path. Sometimes when
3006 * we enter, BSY will be cleared in a chk-status or two. If not,
3007 * the drive is probably seeking or something. Snooze for a couple
3008 * msecs, then chk-status again. If still busy, fall back to
Albert Lee14be71f2005-09-27 17:36:35 +08003009 * HSM_ST_POLL state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 */
Albert Leefe79e682005-12-06 11:34:59 +08003011 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
3012 if (drv_stat & ATA_BUSY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 msleep(2);
Albert Leefe79e682005-12-06 11:34:59 +08003014 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
3015 if (drv_stat & ATA_BUSY) {
Albert Lee14be71f2005-09-27 17:36:35 +08003016 ap->hsm_task_state = HSM_ST_LAST_POLL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003018 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 }
3020 }
3021
Albert Leec14b8332005-12-05 15:36:08 +08003022 qc = ata_qc_from_tag(ap, ap->active_tag);
Tejun Heoa4631472006-02-11 19:11:13 +09003023 WARN_ON(qc == NULL);
Albert Leec14b8332005-12-05 15:36:08 +08003024
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 drv_stat = ata_wait_idle(ap);
3026 if (!ata_ok(drv_stat)) {
Albert Lee1c848982005-12-05 15:40:15 +08003027 qc->err_mask |= __ac_err_mask(drv_stat);
Albert Lee14be71f2005-09-27 17:36:35 +08003028 ap->hsm_task_state = HSM_ST_ERR;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003029 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 }
3031
Albert Lee14be71f2005-09-27 17:36:35 +08003032 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033
Tejun Heoa4631472006-02-11 19:11:13 +09003034 WARN_ON(qc->err_mask);
Albert Leea22e2eb2005-12-05 15:38:02 +08003035 ata_poll_qc_complete(qc);
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003036
3037 /* another command may start at this point */
3038
3039 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040}
3041
Edward Falk0baab862005-06-02 18:17:13 -04003042
3043/**
Randy Dunlapc893a3a2006-01-28 13:15:32 -05003044 * swap_buf_le16 - swap halves of 16-bit words in place
Edward Falk0baab862005-06-02 18:17:13 -04003045 * @buf: Buffer to swap
3046 * @buf_words: Number of 16-bit words in buffer.
3047 *
3048 * Swap halves of 16-bit words if needed to convert from
3049 * little-endian byte order to native cpu byte order, or
3050 * vice-versa.
3051 *
3052 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003053 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04003054 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055void swap_buf_le16(u16 *buf, unsigned int buf_words)
3056{
3057#ifdef __BIG_ENDIAN
3058 unsigned int i;
3059
3060 for (i = 0; i < buf_words; i++)
3061 buf[i] = le16_to_cpu(buf[i]);
3062#endif /* __BIG_ENDIAN */
3063}
3064
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003065/**
3066 * ata_mmio_data_xfer - Transfer data by MMIO
3067 * @ap: port to read/write
3068 * @buf: data buffer
3069 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04003070 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003071 *
3072 * Transfer data from/to the device data register by MMIO.
3073 *
3074 * LOCKING:
3075 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003076 */
3077
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
3079 unsigned int buflen, int write_data)
3080{
3081 unsigned int i;
3082 unsigned int words = buflen >> 1;
3083 u16 *buf16 = (u16 *) buf;
3084 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
3085
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003086 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 if (write_data) {
3088 for (i = 0; i < words; i++)
3089 writew(le16_to_cpu(buf16[i]), mmio);
3090 } else {
3091 for (i = 0; i < words; i++)
3092 buf16[i] = cpu_to_le16(readw(mmio));
3093 }
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003094
3095 /* Transfer trailing 1 byte, if any. */
3096 if (unlikely(buflen & 0x01)) {
3097 u16 align_buf[1] = { 0 };
3098 unsigned char *trailing_buf = buf + buflen - 1;
3099
3100 if (write_data) {
3101 memcpy(align_buf, trailing_buf, 1);
3102 writew(le16_to_cpu(align_buf[0]), mmio);
3103 } else {
3104 align_buf[0] = cpu_to_le16(readw(mmio));
3105 memcpy(trailing_buf, align_buf, 1);
3106 }
3107 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108}
3109
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003110/**
3111 * ata_pio_data_xfer - Transfer data by PIO
3112 * @ap: port to read/write
3113 * @buf: data buffer
3114 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04003115 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003116 *
3117 * Transfer data from/to the device data register by PIO.
3118 *
3119 * LOCKING:
3120 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003121 */
3122
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
3124 unsigned int buflen, int write_data)
3125{
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003126 unsigned int words = buflen >> 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003128 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 if (write_data)
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003130 outsw(ap->ioaddr.data_addr, buf, words);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 else
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003132 insw(ap->ioaddr.data_addr, buf, words);
3133
3134 /* Transfer trailing 1 byte, if any. */
3135 if (unlikely(buflen & 0x01)) {
3136 u16 align_buf[1] = { 0 };
3137 unsigned char *trailing_buf = buf + buflen - 1;
3138
3139 if (write_data) {
3140 memcpy(align_buf, trailing_buf, 1);
3141 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
3142 } else {
3143 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
3144 memcpy(trailing_buf, align_buf, 1);
3145 }
3146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147}
3148
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003149/**
3150 * ata_data_xfer - Transfer data from/to the data register.
3151 * @ap: port to read/write
3152 * @buf: data buffer
3153 * @buflen: buffer length
3154 * @do_write: read/write
3155 *
3156 * Transfer data from/to the device data register.
3157 *
3158 * LOCKING:
3159 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003160 */
3161
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
3163 unsigned int buflen, int do_write)
3164{
Alan Coxa1bd9e62006-01-17 20:53:50 +00003165 /* Make the crap hardware pay the costs not the good stuff */
3166 if (unlikely(ap->flags & ATA_FLAG_IRQ_MASK)) {
3167 unsigned long flags;
3168 local_irq_save(flags);
3169 if (ap->flags & ATA_FLAG_MMIO)
3170 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3171 else
3172 ata_pio_data_xfer(ap, buf, buflen, do_write);
3173 local_irq_restore(flags);
3174 } else {
3175 if (ap->flags & ATA_FLAG_MMIO)
3176 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3177 else
3178 ata_pio_data_xfer(ap, buf, buflen, do_write);
3179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180}
3181
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003182/**
3183 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
3184 * @qc: Command on going
3185 *
3186 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
3187 *
3188 * LOCKING:
3189 * Inherited from caller.
3190 */
3191
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192static void ata_pio_sector(struct ata_queued_cmd *qc)
3193{
3194 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003195 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 struct ata_port *ap = qc->ap;
3197 struct page *page;
3198 unsigned int offset;
3199 unsigned char *buf;
3200
3201 if (qc->cursect == (qc->nsect - 1))
Albert Lee14be71f2005-09-27 17:36:35 +08003202 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203
3204 page = sg[qc->cursg].page;
3205 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
3206
3207 /* get the current page and offset */
3208 page = nth_page(page, (offset >> PAGE_SHIFT));
3209 offset %= PAGE_SIZE;
3210
3211 buf = kmap(page) + offset;
3212
3213 qc->cursect++;
3214 qc->cursg_ofs++;
3215
Albert Lee32529e02005-05-26 03:49:42 -04003216 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 qc->cursg++;
3218 qc->cursg_ofs = 0;
3219 }
3220
3221 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3222
3223 /* do the actual data transfer */
3224 do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3225 ata_data_xfer(ap, buf, ATA_SECT_SIZE, do_write);
3226
3227 kunmap(page);
3228}
3229
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003230/**
3231 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
3232 * @qc: Command on going
3233 * @bytes: number of bytes
3234 *
3235 * Transfer Transfer data from/to the ATAPI device.
3236 *
3237 * LOCKING:
3238 * Inherited from caller.
3239 *
3240 */
3241
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
3243{
3244 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003245 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 struct ata_port *ap = qc->ap;
3247 struct page *page;
3248 unsigned char *buf;
3249 unsigned int offset, count;
3250
Albert Lee563a6e12005-08-12 14:17:50 +08003251 if (qc->curbytes + bytes >= qc->nbytes)
Albert Lee14be71f2005-09-27 17:36:35 +08003252 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253
3254next_sg:
Albert Lee563a6e12005-08-12 14:17:50 +08003255 if (unlikely(qc->cursg >= qc->n_elem)) {
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003256 /*
Albert Lee563a6e12005-08-12 14:17:50 +08003257 * The end of qc->sg is reached and the device expects
3258 * more data to transfer. In order not to overrun qc->sg
3259 * and fulfill length specified in the byte count register,
3260 * - for read case, discard trailing data from the device
3261 * - for write case, padding zero data to the device
3262 */
3263 u16 pad_buf[1] = { 0 };
3264 unsigned int words = bytes >> 1;
3265 unsigned int i;
3266
3267 if (words) /* warning if bytes > 1 */
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003268 printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
Albert Lee563a6e12005-08-12 14:17:50 +08003269 ap->id, bytes);
3270
3271 for (i = 0; i < words; i++)
3272 ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
3273
Albert Lee14be71f2005-09-27 17:36:35 +08003274 ap->hsm_task_state = HSM_ST_LAST;
Albert Lee563a6e12005-08-12 14:17:50 +08003275 return;
3276 }
3277
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003278 sg = &qc->__sg[qc->cursg];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279
Linus Torvalds1da177e2005-04-16 15:20:36 -07003280 page = sg->page;
3281 offset = sg->offset + qc->cursg_ofs;
3282
3283 /* get the current page and offset */
3284 page = nth_page(page, (offset >> PAGE_SHIFT));
3285 offset %= PAGE_SIZE;
3286
Albert Lee6952df02005-06-06 15:56:03 +08003287 /* don't overrun current sg */
Albert Lee32529e02005-05-26 03:49:42 -04003288 count = min(sg->length - qc->cursg_ofs, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003289
3290 /* don't cross page boundaries */
3291 count = min(count, (unsigned int)PAGE_SIZE - offset);
3292
3293 buf = kmap(page) + offset;
3294
3295 bytes -= count;
3296 qc->curbytes += count;
3297 qc->cursg_ofs += count;
3298
Albert Lee32529e02005-05-26 03:49:42 -04003299 if (qc->cursg_ofs == sg->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300 qc->cursg++;
3301 qc->cursg_ofs = 0;
3302 }
3303
3304 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3305
3306 /* do the actual data transfer */
3307 ata_data_xfer(ap, buf, count, do_write);
3308
3309 kunmap(page);
3310
Albert Lee563a6e12005-08-12 14:17:50 +08003311 if (bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312 goto next_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313}
3314
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003315/**
3316 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
3317 * @qc: Command on going
3318 *
3319 * Transfer Transfer data from/to the ATAPI device.
3320 *
3321 * LOCKING:
3322 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003323 */
3324
Linus Torvalds1da177e2005-04-16 15:20:36 -07003325static void atapi_pio_bytes(struct ata_queued_cmd *qc)
3326{
3327 struct ata_port *ap = qc->ap;
3328 struct ata_device *dev = qc->dev;
3329 unsigned int ireason, bc_lo, bc_hi, bytes;
3330 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
3331
3332 ap->ops->tf_read(ap, &qc->tf);
3333 ireason = qc->tf.nsect;
3334 bc_lo = qc->tf.lbam;
3335 bc_hi = qc->tf.lbah;
3336 bytes = (bc_hi << 8) | bc_lo;
3337
3338 /* shall be cleared to zero, indicating xfer of data */
3339 if (ireason & (1 << 0))
3340 goto err_out;
3341
3342 /* make sure transfer direction matches expected */
3343 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
3344 if (do_write != i_write)
3345 goto err_out;
3346
3347 __atapi_pio_bytes(qc, bytes);
3348
3349 return;
3350
3351err_out:
3352 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
3353 ap->id, dev->devno);
Tejun Heo11a56d22006-01-23 13:09:36 +09003354 qc->err_mask |= AC_ERR_HSM;
Albert Lee14be71f2005-09-27 17:36:35 +08003355 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356}
3357
3358/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003359 * ata_pio_block - start PIO on a block
3360 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361 *
3362 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003363 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 */
3365
3366static void ata_pio_block(struct ata_port *ap)
3367{
3368 struct ata_queued_cmd *qc;
3369 u8 status;
3370
3371 /*
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003372 * This is purely heuristic. This is a fast path.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 * Sometimes when we enter, BSY will be cleared in
3374 * a chk-status or two. If not, the drive is probably seeking
3375 * or something. Snooze for a couple msecs, then
3376 * chk-status again. If still busy, fall back to
Albert Lee14be71f2005-09-27 17:36:35 +08003377 * HSM_ST_POLL state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 */
3379 status = ata_busy_wait(ap, ATA_BUSY, 5);
3380 if (status & ATA_BUSY) {
3381 msleep(2);
3382 status = ata_busy_wait(ap, ATA_BUSY, 10);
3383 if (status & ATA_BUSY) {
Albert Lee14be71f2005-09-27 17:36:35 +08003384 ap->hsm_task_state = HSM_ST_POLL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
3386 return;
3387 }
3388 }
3389
3390 qc = ata_qc_from_tag(ap, ap->active_tag);
Tejun Heoa4631472006-02-11 19:11:13 +09003391 WARN_ON(qc == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392
Albert Leefe79e682005-12-06 11:34:59 +08003393 /* check error */
3394 if (status & (ATA_ERR | ATA_DF)) {
3395 qc->err_mask |= AC_ERR_DEV;
3396 ap->hsm_task_state = HSM_ST_ERR;
3397 return;
3398 }
3399
3400 /* transfer data if any */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401 if (is_atapi_taskfile(&qc->tf)) {
Albert Leefe79e682005-12-06 11:34:59 +08003402 /* DRQ=0 means no more data to transfer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403 if ((status & ATA_DRQ) == 0) {
Albert Lee14be71f2005-09-27 17:36:35 +08003404 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405 return;
3406 }
3407
3408 atapi_pio_bytes(qc);
3409 } else {
3410 /* handle BSY=0, DRQ=0 as error */
3411 if ((status & ATA_DRQ) == 0) {
Tejun Heo11a56d22006-01-23 13:09:36 +09003412 qc->err_mask |= AC_ERR_HSM;
Albert Lee14be71f2005-09-27 17:36:35 +08003413 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 return;
3415 }
3416
3417 ata_pio_sector(qc);
3418 }
3419}
3420
3421static void ata_pio_error(struct ata_port *ap)
3422{
3423 struct ata_queued_cmd *qc;
Jeff Garzika7dac442005-10-30 04:44:42 -05003424
3425 printk(KERN_WARNING "ata%u: PIO error\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426
3427 qc = ata_qc_from_tag(ap, ap->active_tag);
Tejun Heoa4631472006-02-11 19:11:13 +09003428 WARN_ON(qc == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429
Albert Lee1c848982005-12-05 15:40:15 +08003430 /* make sure qc->err_mask is available to
3431 * know what's wrong and recover
3432 */
Tejun Heoa4631472006-02-11 19:11:13 +09003433 WARN_ON(qc->err_mask == 0);
Albert Lee1c848982005-12-05 15:40:15 +08003434
Albert Lee14be71f2005-09-27 17:36:35 +08003435 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436
Albert Leea22e2eb2005-12-05 15:38:02 +08003437 ata_poll_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438}
3439
3440static void ata_pio_task(void *_data)
3441{
3442 struct ata_port *ap = _data;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003443 unsigned long timeout;
3444 int qc_completed;
3445
3446fsm_start:
3447 timeout = 0;
3448 qc_completed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449
Albert Lee14be71f2005-09-27 17:36:35 +08003450 switch (ap->hsm_task_state) {
3451 case HSM_ST_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 return;
3453
Albert Lee14be71f2005-09-27 17:36:35 +08003454 case HSM_ST:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003455 ata_pio_block(ap);
3456 break;
3457
Albert Lee14be71f2005-09-27 17:36:35 +08003458 case HSM_ST_LAST:
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003459 qc_completed = ata_pio_complete(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460 break;
3461
Albert Lee14be71f2005-09-27 17:36:35 +08003462 case HSM_ST_POLL:
3463 case HSM_ST_LAST_POLL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 timeout = ata_pio_poll(ap);
3465 break;
3466
Albert Lee14be71f2005-09-27 17:36:35 +08003467 case HSM_ST_TMOUT:
3468 case HSM_ST_ERR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 ata_pio_error(ap);
3470 return;
3471 }
3472
3473 if (timeout)
Tejun Heo95064372006-01-23 13:09:37 +09003474 ata_queue_delayed_pio_task(ap, timeout);
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003475 else if (!qc_completed)
3476 goto fsm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003477}
3478
Linus Torvalds1da177e2005-04-16 15:20:36 -07003479/**
3480 * ata_qc_timeout - Handle timeout of queued command
3481 * @qc: Command that timed out
3482 *
3483 * Some part of the kernel (currently, only the SCSI layer)
3484 * has noticed that the active command on port @ap has not
3485 * completed after a specified length of time. Handle this
3486 * condition by disabling DMA (if necessary) and completing
3487 * transactions, with error if necessary.
3488 *
3489 * This also handles the case of the "lost interrupt", where
3490 * for some reason (possibly hardware bug, possibly driver bug)
3491 * an interrupt was not delivered to the driver, even though the
3492 * transaction completed successfully.
3493 *
3494 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003495 * Inherited from SCSI layer (none, can sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496 */
3497
3498static void ata_qc_timeout(struct ata_queued_cmd *qc)
3499{
3500 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003501 struct ata_host_set *host_set = ap->host_set;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003502 u8 host_stat = 0, drv_stat;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003503 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003504
3505 DPRINTK("ENTER\n");
3506
Tejun Heoc18d06f2006-02-02 00:56:10 +09003507 ata_flush_pio_tasks(ap);
3508 ap->hsm_task_state = HSM_ST_IDLE;
3509
Jeff Garzikb8f61532005-08-25 22:01:20 -04003510 spin_lock_irqsave(&host_set->lock, flags);
3511
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512 switch (qc->tf.protocol) {
3513
3514 case ATA_PROT_DMA:
3515 case ATA_PROT_ATAPI_DMA:
3516 host_stat = ap->ops->bmdma_status(ap);
3517
3518 /* before we do anything else, clear DMA-Start bit */
Alan Coxb73fc892005-08-26 16:03:19 +01003519 ap->ops->bmdma_stop(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520
3521 /* fall through */
3522
3523 default:
3524 ata_altstatus(ap);
3525 drv_stat = ata_chk_status(ap);
3526
3527 /* ack bmdma irq events */
3528 ap->ops->irq_clear(ap);
3529
3530 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
3531 ap->id, qc->tf.command, drv_stat, host_stat);
3532
3533 /* complete taskfile transaction */
Albert Leea22e2eb2005-12-05 15:38:02 +08003534 qc->err_mask |= ac_err_mask(drv_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535 break;
3536 }
Jeff Garzikb8f61532005-08-25 22:01:20 -04003537
3538 spin_unlock_irqrestore(&host_set->lock, flags);
3539
Tejun Heoa72ec4c2006-01-23 13:09:37 +09003540 ata_eh_qc_complete(qc);
3541
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542 DPRINTK("EXIT\n");
3543}
3544
3545/**
3546 * ata_eng_timeout - Handle timeout of queued command
3547 * @ap: Port on which timed-out command is active
3548 *
3549 * Some part of the kernel (currently, only the SCSI layer)
3550 * has noticed that the active command on port @ap has not
3551 * completed after a specified length of time. Handle this
3552 * condition by disabling DMA (if necessary) and completing
3553 * transactions, with error if necessary.
3554 *
3555 * This also handles the case of the "lost interrupt", where
3556 * for some reason (possibly hardware bug, possibly driver bug)
3557 * an interrupt was not delivered to the driver, even though the
3558 * transaction completed successfully.
3559 *
3560 * LOCKING:
3561 * Inherited from SCSI layer (none, can sleep)
3562 */
3563
3564void ata_eng_timeout(struct ata_port *ap)
3565{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003566 DPRINTK("ENTER\n");
3567
Tejun Heof6379022006-02-10 15:10:48 +09003568 ata_qc_timeout(ata_qc_from_tag(ap, ap->active_tag));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 DPRINTK("EXIT\n");
3571}
3572
3573/**
3574 * ata_qc_new - Request an available ATA command, for queueing
3575 * @ap: Port associated with device @dev
3576 * @dev: Device from whom we request an available command structure
3577 *
3578 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003579 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580 */
3581
3582static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
3583{
3584 struct ata_queued_cmd *qc = NULL;
3585 unsigned int i;
3586
3587 for (i = 0; i < ATA_MAX_QUEUE; i++)
3588 if (!test_and_set_bit(i, &ap->qactive)) {
3589 qc = ata_qc_from_tag(ap, i);
3590 break;
3591 }
3592
3593 if (qc)
3594 qc->tag = i;
3595
3596 return qc;
3597}
3598
3599/**
3600 * ata_qc_new_init - Request an available ATA command, and initialize it
3601 * @ap: Port associated with device @dev
3602 * @dev: Device from whom we request an available command structure
3603 *
3604 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003605 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003606 */
3607
3608struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
3609 struct ata_device *dev)
3610{
3611 struct ata_queued_cmd *qc;
3612
3613 qc = ata_qc_new(ap);
3614 if (qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615 qc->scsicmd = NULL;
3616 qc->ap = ap;
3617 qc->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05003619 ata_qc_reinit(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620 }
3621
3622 return qc;
3623}
3624
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625/**
3626 * ata_qc_free - free unused ata_queued_cmd
3627 * @qc: Command to complete
3628 *
3629 * Designed to free unused ata_queued_cmd object
3630 * in case something prevents using it.
3631 *
3632 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003633 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634 */
3635void ata_qc_free(struct ata_queued_cmd *qc)
3636{
Tejun Heo4ba946e2006-01-23 13:09:36 +09003637 struct ata_port *ap = qc->ap;
3638 unsigned int tag;
3639
Tejun Heoa4631472006-02-11 19:11:13 +09003640 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641
Tejun Heo4ba946e2006-01-23 13:09:36 +09003642 qc->flags = 0;
3643 tag = qc->tag;
3644 if (likely(ata_tag_valid(tag))) {
3645 if (tag == ap->active_tag)
3646 ap->active_tag = ATA_TAG_POISON;
3647 qc->tag = ATA_TAG_POISON;
3648 clear_bit(tag, &ap->qactive);
3649 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650}
3651
Tejun Heo76014422006-02-11 15:13:49 +09003652void __ata_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653{
Tejun Heoa4631472006-02-11 19:11:13 +09003654 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
3655 WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656
3657 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
3658 ata_sg_clean(qc);
3659
Albert Lee3f3791d2005-08-16 14:25:38 +08003660 /* atapi: mark qc as inactive to prevent the interrupt handler
3661 * from completing the command twice later, before the error handler
3662 * is called. (when rc != 0 and atapi request sense is needed)
3663 */
3664 qc->flags &= ~ATA_QCFLAG_ACTIVE;
3665
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666 /* call completion callback */
Tejun Heo77853bf2006-01-23 13:09:36 +09003667 qc->complete_fn(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668}
3669
3670static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
3671{
3672 struct ata_port *ap = qc->ap;
3673
3674 switch (qc->tf.protocol) {
3675 case ATA_PROT_DMA:
3676 case ATA_PROT_ATAPI_DMA:
3677 return 1;
3678
3679 case ATA_PROT_ATAPI:
3680 case ATA_PROT_PIO:
3681 case ATA_PROT_PIO_MULT:
3682 if (ap->flags & ATA_FLAG_PIO_DMA)
3683 return 1;
3684
3685 /* fall through */
3686
3687 default:
3688 return 0;
3689 }
3690
3691 /* never reached */
3692}
3693
3694/**
3695 * ata_qc_issue - issue taskfile to device
3696 * @qc: command to issue to device
3697 *
3698 * Prepare an ATA command to submission to device.
3699 * This includes mapping the data into a DMA-able
3700 * area, filling in the S/G table, and finally
3701 * writing the taskfile to hardware, starting the command.
3702 *
3703 * LOCKING:
3704 * spin_lock_irqsave(host_set lock)
3705 *
3706 * RETURNS:
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003707 * Zero on success, AC_ERR_* mask on failure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003708 */
3709
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003710unsigned int ata_qc_issue(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711{
3712 struct ata_port *ap = qc->ap;
3713
3714 if (ata_should_dma_map(qc)) {
3715 if (qc->flags & ATA_QCFLAG_SG) {
3716 if (ata_sg_setup(qc))
Tejun Heo8e436af2006-01-23 13:09:36 +09003717 goto sg_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003718 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
3719 if (ata_sg_setup_one(qc))
Tejun Heo8e436af2006-01-23 13:09:36 +09003720 goto sg_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721 }
3722 } else {
3723 qc->flags &= ~ATA_QCFLAG_DMAMAP;
3724 }
3725
3726 ap->ops->qc_prep(qc);
3727
3728 qc->ap->active_tag = qc->tag;
3729 qc->flags |= ATA_QCFLAG_ACTIVE;
3730
3731 return ap->ops->qc_issue(qc);
3732
Tejun Heo8e436af2006-01-23 13:09:36 +09003733sg_err:
3734 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003735 return AC_ERR_SYSTEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736}
3737
Edward Falk0baab862005-06-02 18:17:13 -04003738
Linus Torvalds1da177e2005-04-16 15:20:36 -07003739/**
3740 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
3741 * @qc: command to issue to device
3742 *
3743 * Using various libata functions and hooks, this function
3744 * starts an ATA command. ATA commands are grouped into
3745 * classes called "protocols", and issuing each type of protocol
3746 * is slightly different.
3747 *
Edward Falk0baab862005-06-02 18:17:13 -04003748 * May be used as the qc_issue() entry in ata_port_operations.
3749 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750 * LOCKING:
3751 * spin_lock_irqsave(host_set lock)
3752 *
3753 * RETURNS:
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003754 * Zero on success, AC_ERR_* mask on failure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003755 */
3756
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003757unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003758{
3759 struct ata_port *ap = qc->ap;
3760
3761 ata_dev_select(ap, qc->dev->devno, 1, 0);
3762
3763 switch (qc->tf.protocol) {
3764 case ATA_PROT_NODATA:
Jeff Garzike5338252005-10-30 21:37:17 -05003765 ata_tf_to_host(ap, &qc->tf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766 break;
3767
3768 case ATA_PROT_DMA:
3769 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3770 ap->ops->bmdma_setup(qc); /* set up bmdma */
3771 ap->ops->bmdma_start(qc); /* initiate bmdma */
3772 break;
3773
3774 case ATA_PROT_PIO: /* load tf registers, initiate polling pio */
3775 ata_qc_set_polling(qc);
Jeff Garzike5338252005-10-30 21:37:17 -05003776 ata_tf_to_host(ap, &qc->tf);
Albert Lee14be71f2005-09-27 17:36:35 +08003777 ap->hsm_task_state = HSM_ST;
Tejun Heo95064372006-01-23 13:09:37 +09003778 ata_queue_pio_task(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779 break;
3780
3781 case ATA_PROT_ATAPI:
3782 ata_qc_set_polling(qc);
Jeff Garzike5338252005-10-30 21:37:17 -05003783 ata_tf_to_host(ap, &qc->tf);
Tejun Heo95064372006-01-23 13:09:37 +09003784 ata_queue_packet_task(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003785 break;
3786
3787 case ATA_PROT_ATAPI_NODATA:
Tejun Heoc1389502005-08-22 14:59:24 +09003788 ap->flags |= ATA_FLAG_NOINTR;
Jeff Garzike5338252005-10-30 21:37:17 -05003789 ata_tf_to_host(ap, &qc->tf);
Tejun Heo95064372006-01-23 13:09:37 +09003790 ata_queue_packet_task(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791 break;
3792
3793 case ATA_PROT_ATAPI_DMA:
Tejun Heoc1389502005-08-22 14:59:24 +09003794 ap->flags |= ATA_FLAG_NOINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3796 ap->ops->bmdma_setup(qc); /* set up bmdma */
Tejun Heo95064372006-01-23 13:09:37 +09003797 ata_queue_packet_task(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 break;
3799
3800 default:
3801 WARN_ON(1);
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003802 return AC_ERR_SYSTEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803 }
3804
3805 return 0;
3806}
3807
3808/**
Edward Falk0baab862005-06-02 18:17:13 -04003809 * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810 * @qc: Info associated with this ATA transaction.
3811 *
3812 * LOCKING:
3813 * spin_lock_irqsave(host_set lock)
3814 */
3815
3816static void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc)
3817{
3818 struct ata_port *ap = qc->ap;
3819 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3820 u8 dmactl;
3821 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3822
3823 /* load PRD table addr. */
3824 mb(); /* make sure PRD table writes are visible to controller */
3825 writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
3826
3827 /* specify data direction, triple-check start bit is clear */
3828 dmactl = readb(mmio + ATA_DMA_CMD);
3829 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3830 if (!rw)
3831 dmactl |= ATA_DMA_WR;
3832 writeb(dmactl, mmio + ATA_DMA_CMD);
3833
3834 /* issue r/w command */
3835 ap->ops->exec_command(ap, &qc->tf);
3836}
3837
3838/**
Alan Coxb73fc892005-08-26 16:03:19 +01003839 * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840 * @qc: Info associated with this ATA transaction.
3841 *
3842 * LOCKING:
3843 * spin_lock_irqsave(host_set lock)
3844 */
3845
3846static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
3847{
3848 struct ata_port *ap = qc->ap;
3849 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3850 u8 dmactl;
3851
3852 /* start host DMA transaction */
3853 dmactl = readb(mmio + ATA_DMA_CMD);
3854 writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
3855
3856 /* Strictly, one may wish to issue a readb() here, to
3857 * flush the mmio write. However, control also passes
3858 * to the hardware at this point, and it will interrupt
3859 * us when we are to resume control. So, in effect,
3860 * we don't care when the mmio write flushes.
3861 * Further, a read of the DMA status register _immediately_
3862 * following the write may not be what certain flaky hardware
3863 * is expected, so I think it is best to not add a readb()
3864 * without first all the MMIO ATA cards/mobos.
3865 * Or maybe I'm just being paranoid.
3866 */
3867}
3868
3869/**
3870 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
3871 * @qc: Info associated with this ATA transaction.
3872 *
3873 * LOCKING:
3874 * spin_lock_irqsave(host_set lock)
3875 */
3876
3877static void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
3878{
3879 struct ata_port *ap = qc->ap;
3880 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3881 u8 dmactl;
3882
3883 /* load PRD table addr. */
3884 outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
3885
3886 /* specify data direction, triple-check start bit is clear */
3887 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3888 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3889 if (!rw)
3890 dmactl |= ATA_DMA_WR;
3891 outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3892
3893 /* issue r/w command */
3894 ap->ops->exec_command(ap, &qc->tf);
3895}
3896
3897/**
3898 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
3899 * @qc: Info associated with this ATA transaction.
3900 *
3901 * LOCKING:
3902 * spin_lock_irqsave(host_set lock)
3903 */
3904
3905static void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
3906{
3907 struct ata_port *ap = qc->ap;
3908 u8 dmactl;
3909
3910 /* start host DMA transaction */
3911 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3912 outb(dmactl | ATA_DMA_START,
3913 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3914}
3915
Edward Falk0baab862005-06-02 18:17:13 -04003916
3917/**
3918 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
3919 * @qc: Info associated with this ATA transaction.
3920 *
3921 * Writes the ATA_DMA_START flag to the DMA command register.
3922 *
3923 * May be used as the bmdma_start() entry in ata_port_operations.
3924 *
3925 * LOCKING:
3926 * spin_lock_irqsave(host_set lock)
3927 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928void ata_bmdma_start(struct ata_queued_cmd *qc)
3929{
3930 if (qc->ap->flags & ATA_FLAG_MMIO)
3931 ata_bmdma_start_mmio(qc);
3932 else
3933 ata_bmdma_start_pio(qc);
3934}
3935
Edward Falk0baab862005-06-02 18:17:13 -04003936
3937/**
3938 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
3939 * @qc: Info associated with this ATA transaction.
3940 *
3941 * Writes address of PRD table to device's PRD Table Address
3942 * register, sets the DMA control register, and calls
3943 * ops->exec_command() to start the transfer.
3944 *
3945 * May be used as the bmdma_setup() entry in ata_port_operations.
3946 *
3947 * LOCKING:
3948 * spin_lock_irqsave(host_set lock)
3949 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003950void ata_bmdma_setup(struct ata_queued_cmd *qc)
3951{
3952 if (qc->ap->flags & ATA_FLAG_MMIO)
3953 ata_bmdma_setup_mmio(qc);
3954 else
3955 ata_bmdma_setup_pio(qc);
3956}
3957
Edward Falk0baab862005-06-02 18:17:13 -04003958
3959/**
3960 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
Jeff Garzikdecc6d02005-06-02 18:42:33 -04003961 * @ap: Port associated with this ATA transaction.
Edward Falk0baab862005-06-02 18:17:13 -04003962 *
3963 * Clear interrupt and error flags in DMA status register.
3964 *
3965 * May be used as the irq_clear() entry in ata_port_operations.
3966 *
3967 * LOCKING:
3968 * spin_lock_irqsave(host_set lock)
3969 */
3970
Linus Torvalds1da177e2005-04-16 15:20:36 -07003971void ata_bmdma_irq_clear(struct ata_port *ap)
3972{
3973 if (ap->flags & ATA_FLAG_MMIO) {
3974 void __iomem *mmio = ((void __iomem *) ap->ioaddr.bmdma_addr) + ATA_DMA_STATUS;
3975 writeb(readb(mmio), mmio);
3976 } else {
3977 unsigned long addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
3978 outb(inb(addr), addr);
3979 }
3980
3981}
3982
Edward Falk0baab862005-06-02 18:17:13 -04003983
3984/**
3985 * ata_bmdma_status - Read PCI IDE BMDMA status
Jeff Garzikdecc6d02005-06-02 18:42:33 -04003986 * @ap: Port associated with this ATA transaction.
Edward Falk0baab862005-06-02 18:17:13 -04003987 *
3988 * Read and return BMDMA status register.
3989 *
3990 * May be used as the bmdma_status() entry in ata_port_operations.
3991 *
3992 * LOCKING:
3993 * spin_lock_irqsave(host_set lock)
3994 */
3995
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996u8 ata_bmdma_status(struct ata_port *ap)
3997{
3998 u8 host_stat;
3999 if (ap->flags & ATA_FLAG_MMIO) {
4000 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4001 host_stat = readb(mmio + ATA_DMA_STATUS);
4002 } else
Albert Leeee500aa2005-09-27 17:34:38 +08004003 host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004 return host_stat;
4005}
4006
Edward Falk0baab862005-06-02 18:17:13 -04004007
4008/**
4009 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
Alan Coxb73fc892005-08-26 16:03:19 +01004010 * @qc: Command we are ending DMA for
Edward Falk0baab862005-06-02 18:17:13 -04004011 *
4012 * Clears the ATA_DMA_START flag in the dma control register
4013 *
4014 * May be used as the bmdma_stop() entry in ata_port_operations.
4015 *
4016 * LOCKING:
4017 * spin_lock_irqsave(host_set lock)
4018 */
4019
Alan Coxb73fc892005-08-26 16:03:19 +01004020void ata_bmdma_stop(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021{
Alan Coxb73fc892005-08-26 16:03:19 +01004022 struct ata_port *ap = qc->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023 if (ap->flags & ATA_FLAG_MMIO) {
4024 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4025
4026 /* clear start/stop bit */
4027 writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
4028 mmio + ATA_DMA_CMD);
4029 } else {
4030 /* clear start/stop bit */
4031 outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
4032 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4033 }
4034
4035 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
4036 ata_altstatus(ap); /* dummy read */
4037}
4038
4039/**
4040 * ata_host_intr - Handle host interrupt for given (port, task)
4041 * @ap: Port on which interrupt arrived (possibly...)
4042 * @qc: Taskfile currently active in engine
4043 *
4044 * Handle host interrupt for given queued command. Currently,
4045 * only DMA interrupts are handled. All other commands are
4046 * handled via polling with interrupts disabled (nIEN bit).
4047 *
4048 * LOCKING:
4049 * spin_lock_irqsave(host_set lock)
4050 *
4051 * RETURNS:
4052 * One if interrupt was handled, zero if not (shared irq).
4053 */
4054
4055inline unsigned int ata_host_intr (struct ata_port *ap,
4056 struct ata_queued_cmd *qc)
4057{
4058 u8 status, host_stat;
4059
4060 switch (qc->tf.protocol) {
4061
4062 case ATA_PROT_DMA:
4063 case ATA_PROT_ATAPI_DMA:
4064 case ATA_PROT_ATAPI:
4065 /* check status of DMA engine */
4066 host_stat = ap->ops->bmdma_status(ap);
4067 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
4068
4069 /* if it's not our irq... */
4070 if (!(host_stat & ATA_DMA_INTR))
4071 goto idle_irq;
4072
4073 /* before we do anything else, clear DMA-Start bit */
Alan Coxb73fc892005-08-26 16:03:19 +01004074 ap->ops->bmdma_stop(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075
4076 /* fall through */
4077
4078 case ATA_PROT_ATAPI_NODATA:
4079 case ATA_PROT_NODATA:
4080 /* check altstatus */
4081 status = ata_altstatus(ap);
4082 if (status & ATA_BUSY)
4083 goto idle_irq;
4084
4085 /* check main status, clearing INTRQ */
4086 status = ata_chk_status(ap);
4087 if (unlikely(status & ATA_BUSY))
4088 goto idle_irq;
4089 DPRINTK("ata%u: protocol %d (dev_stat 0x%X)\n",
4090 ap->id, qc->tf.protocol, status);
4091
4092 /* ack bmdma irq events */
4093 ap->ops->irq_clear(ap);
4094
4095 /* complete taskfile transaction */
Albert Leea22e2eb2005-12-05 15:38:02 +08004096 qc->err_mask |= ac_err_mask(status);
4097 ata_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098 break;
4099
4100 default:
4101 goto idle_irq;
4102 }
4103
4104 return 1; /* irq handled */
4105
4106idle_irq:
4107 ap->stats.idle_irq++;
4108
4109#ifdef ATA_IRQ_TRAP
4110 if ((ap->stats.idle_irq % 1000) == 0) {
4111 handled = 1;
4112 ata_irq_ack(ap, 0); /* debug trap */
4113 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
4114 }
4115#endif
4116 return 0; /* irq not handled */
4117}
4118
4119/**
4120 * ata_interrupt - Default ATA host interrupt handler
Jeff Garzik0cba6322005-05-30 19:49:12 -04004121 * @irq: irq line (unused)
4122 * @dev_instance: pointer to our ata_host_set information structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123 * @regs: unused
4124 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004125 * Default interrupt handler for PCI IDE devices. Calls
4126 * ata_host_intr() for each port that is not disabled.
4127 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004129 * Obtains host_set lock during operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130 *
4131 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004132 * IRQ_NONE or IRQ_HANDLED.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133 */
4134
4135irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
4136{
4137 struct ata_host_set *host_set = dev_instance;
4138 unsigned int i;
4139 unsigned int handled = 0;
4140 unsigned long flags;
4141
4142 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
4143 spin_lock_irqsave(&host_set->lock, flags);
4144
4145 for (i = 0; i < host_set->n_ports; i++) {
4146 struct ata_port *ap;
4147
4148 ap = host_set->ports[i];
Tejun Heoc1389502005-08-22 14:59:24 +09004149 if (ap &&
4150 !(ap->flags & (ATA_FLAG_PORT_DISABLED | ATA_FLAG_NOINTR))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151 struct ata_queued_cmd *qc;
4152
4153 qc = ata_qc_from_tag(ap, ap->active_tag);
Albert Lee21b1ed72005-04-29 17:34:59 +08004154 if (qc && (!(qc->tf.ctl & ATA_NIEN)) &&
4155 (qc->flags & ATA_QCFLAG_ACTIVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156 handled |= ata_host_intr(ap, qc);
4157 }
4158 }
4159
4160 spin_unlock_irqrestore(&host_set->lock, flags);
4161
4162 return IRQ_RETVAL(handled);
4163}
4164
4165/**
4166 * atapi_packet_task - Write CDB bytes to hardware
4167 * @_data: Port to which ATAPI device is attached.
4168 *
4169 * When device has indicated its readiness to accept
4170 * a CDB, this function is called. Send the CDB.
4171 * If DMA is to be performed, exit immediately.
4172 * Otherwise, we are in polling mode, so poll
4173 * status under operation succeeds or fails.
4174 *
4175 * LOCKING:
4176 * Kernel thread context (may sleep)
4177 */
4178
4179static void atapi_packet_task(void *_data)
4180{
4181 struct ata_port *ap = _data;
4182 struct ata_queued_cmd *qc;
4183 u8 status;
4184
4185 qc = ata_qc_from_tag(ap, ap->active_tag);
Tejun Heoa4631472006-02-11 19:11:13 +09004186 WARN_ON(qc == NULL);
4187 WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188
4189 /* sleep-wait for BSY to clear */
4190 DPRINTK("busy wait\n");
Albert Leed8fe4522005-12-05 15:42:17 +08004191 if (ata_busy_sleep(ap, ATA_TMOUT_CDB_QUICK, ATA_TMOUT_CDB)) {
Tejun Heo11a56d22006-01-23 13:09:36 +09004192 qc->err_mask |= AC_ERR_TIMEOUT;
Albert Leed8fe4522005-12-05 15:42:17 +08004193 goto err_out;
4194 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195
4196 /* make sure DRQ is set */
4197 status = ata_chk_status(ap);
Albert Leed8fe4522005-12-05 15:42:17 +08004198 if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ) {
Tejun Heo11a56d22006-01-23 13:09:36 +09004199 qc->err_mask |= AC_ERR_HSM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200 goto err_out;
Albert Leed8fe4522005-12-05 15:42:17 +08004201 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202
4203 /* send SCSI cdb */
4204 DPRINTK("send cdb\n");
Tejun Heoa4631472006-02-11 19:11:13 +09004205 WARN_ON(ap->cdb_len < 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206
Tejun Heoc1389502005-08-22 14:59:24 +09004207 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA ||
4208 qc->tf.protocol == ATA_PROT_ATAPI_NODATA) {
4209 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210
Tejun Heoc1389502005-08-22 14:59:24 +09004211 /* Once we're done issuing command and kicking bmdma,
4212 * irq handler takes over. To not lose irq, we need
4213 * to clear NOINTR flag before sending cdb, but
4214 * interrupt handler shouldn't be invoked before we're
4215 * finished. Hence, the following locking.
4216 */
4217 spin_lock_irqsave(&ap->host_set->lock, flags);
4218 ap->flags &= ~ATA_FLAG_NOINTR;
4219 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
4220 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA)
4221 ap->ops->bmdma_start(qc); /* initiate bmdma */
4222 spin_unlock_irqrestore(&ap->host_set->lock, flags);
4223 } else {
4224 ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225
Tejun Heoc1389502005-08-22 14:59:24 +09004226 /* PIO commands are handled by polling */
Albert Lee14be71f2005-09-27 17:36:35 +08004227 ap->hsm_task_state = HSM_ST;
Tejun Heo95064372006-01-23 13:09:37 +09004228 ata_queue_pio_task(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229 }
4230
4231 return;
4232
4233err_out:
Albert Leea22e2eb2005-12-05 15:38:02 +08004234 ata_poll_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235}
4236
Edward Falk0baab862005-06-02 18:17:13 -04004237
Jens Axboe9b847542006-01-06 09:28:07 +01004238/*
4239 * Execute a 'simple' command, that only consists of the opcode 'cmd' itself,
4240 * without filling any other registers
4241 */
4242static int ata_do_simple_cmd(struct ata_port *ap, struct ata_device *dev,
4243 u8 cmd)
4244{
4245 struct ata_taskfile tf;
4246 int err;
4247
4248 ata_tf_init(ap, &tf, dev->devno);
4249
4250 tf.command = cmd;
4251 tf.flags |= ATA_TFLAG_DEVICE;
4252 tf.protocol = ATA_PROT_NODATA;
4253
4254 err = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
4255 if (err)
4256 printk(KERN_ERR "%s: ata command failed: %d\n",
4257 __FUNCTION__, err);
4258
4259 return err;
4260}
4261
4262static int ata_flush_cache(struct ata_port *ap, struct ata_device *dev)
4263{
4264 u8 cmd;
4265
4266 if (!ata_try_flush_cache(dev))
4267 return 0;
4268
4269 if (ata_id_has_flush_ext(dev->id))
4270 cmd = ATA_CMD_FLUSH_EXT;
4271 else
4272 cmd = ATA_CMD_FLUSH;
4273
4274 return ata_do_simple_cmd(ap, dev, cmd);
4275}
4276
4277static int ata_standby_drive(struct ata_port *ap, struct ata_device *dev)
4278{
4279 return ata_do_simple_cmd(ap, dev, ATA_CMD_STANDBYNOW1);
4280}
4281
4282static int ata_start_drive(struct ata_port *ap, struct ata_device *dev)
4283{
4284 return ata_do_simple_cmd(ap, dev, ATA_CMD_IDLEIMMEDIATE);
4285}
4286
4287/**
4288 * ata_device_resume - wakeup a previously suspended devices
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004289 * @ap: port the device is connected to
4290 * @dev: the device to resume
Jens Axboe9b847542006-01-06 09:28:07 +01004291 *
4292 * Kick the drive back into action, by sending it an idle immediate
4293 * command and making sure its transfer mode matches between drive
4294 * and host.
4295 *
4296 */
4297int ata_device_resume(struct ata_port *ap, struct ata_device *dev)
4298{
4299 if (ap->flags & ATA_FLAG_SUSPENDED) {
4300 ap->flags &= ~ATA_FLAG_SUSPENDED;
4301 ata_set_mode(ap);
4302 }
4303 if (!ata_dev_present(dev))
4304 return 0;
4305 if (dev->class == ATA_DEV_ATA)
4306 ata_start_drive(ap, dev);
4307
4308 return 0;
4309}
4310
4311/**
4312 * ata_device_suspend - prepare a device for suspend
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004313 * @ap: port the device is connected to
4314 * @dev: the device to suspend
Jens Axboe9b847542006-01-06 09:28:07 +01004315 *
4316 * Flush the cache on the drive, if appropriate, then issue a
4317 * standbynow command.
Jens Axboe9b847542006-01-06 09:28:07 +01004318 */
4319int ata_device_suspend(struct ata_port *ap, struct ata_device *dev)
4320{
4321 if (!ata_dev_present(dev))
4322 return 0;
4323 if (dev->class == ATA_DEV_ATA)
4324 ata_flush_cache(ap, dev);
4325
4326 ata_standby_drive(ap, dev);
4327 ap->flags |= ATA_FLAG_SUSPENDED;
4328 return 0;
4329}
4330
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004331/**
4332 * ata_port_start - Set port up for dma.
4333 * @ap: Port to initialize
4334 *
4335 * Called just after data structures for each port are
4336 * initialized. Allocates space for PRD table.
4337 *
4338 * May be used as the port_start() entry in ata_port_operations.
4339 *
4340 * LOCKING:
4341 * Inherited from caller.
4342 */
4343
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344int ata_port_start (struct ata_port *ap)
4345{
4346 struct device *dev = ap->host_set->dev;
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004347 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348
4349 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
4350 if (!ap->prd)
4351 return -ENOMEM;
4352
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004353 rc = ata_pad_alloc(ap, dev);
4354 if (rc) {
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004355 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004356 return rc;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004357 }
4358
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
4360
4361 return 0;
4362}
4363
Edward Falk0baab862005-06-02 18:17:13 -04004364
4365/**
4366 * ata_port_stop - Undo ata_port_start()
4367 * @ap: Port to shut down
4368 *
4369 * Frees the PRD table.
4370 *
4371 * May be used as the port_stop() entry in ata_port_operations.
4372 *
4373 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004374 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04004375 */
4376
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377void ata_port_stop (struct ata_port *ap)
4378{
4379 struct device *dev = ap->host_set->dev;
4380
4381 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004382 ata_pad_free(ap, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004383}
4384
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04004385void ata_host_stop (struct ata_host_set *host_set)
4386{
4387 if (host_set->mmio_base)
4388 iounmap(host_set->mmio_base);
4389}
4390
4391
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392/**
4393 * ata_host_remove - Unregister SCSI host structure with upper layers
4394 * @ap: Port to unregister
4395 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
4396 *
4397 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004398 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399 */
4400
4401static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
4402{
4403 struct Scsi_Host *sh = ap->host;
4404
4405 DPRINTK("ENTER\n");
4406
4407 if (do_unregister)
4408 scsi_remove_host(sh);
4409
4410 ap->ops->port_stop(ap);
4411}
4412
4413/**
4414 * ata_host_init - Initialize an ata_port structure
4415 * @ap: Structure to initialize
4416 * @host: associated SCSI mid-layer structure
4417 * @host_set: Collection of hosts to which @ap belongs
4418 * @ent: Probe information provided by low-level driver
4419 * @port_no: Port number associated with this ata_port
4420 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004421 * Initialize a new ata_port structure, and its associated
4422 * scsi_host.
4423 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004425 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004426 */
4427
4428static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
4429 struct ata_host_set *host_set,
Jeff Garzik057ace52005-10-22 14:27:05 -04004430 const struct ata_probe_ent *ent, unsigned int port_no)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431{
4432 unsigned int i;
4433
4434 host->max_id = 16;
4435 host->max_lun = 1;
4436 host->max_channel = 1;
4437 host->unique_id = ata_unique_id++;
4438 host->max_cmd_len = 12;
Christoph Hellwig12413192005-06-11 01:05:01 +02004439
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440 ap->flags = ATA_FLAG_PORT_DISABLED;
4441 ap->id = host->unique_id;
4442 ap->host = host;
4443 ap->ctl = ATA_DEVCTL_OBS;
4444 ap->host_set = host_set;
4445 ap->port_no = port_no;
4446 ap->hard_port_no =
4447 ent->legacy_mode ? ent->hard_port_no : port_no;
4448 ap->pio_mask = ent->pio_mask;
4449 ap->mwdma_mask = ent->mwdma_mask;
4450 ap->udma_mask = ent->udma_mask;
4451 ap->flags |= ent->host_flags;
4452 ap->ops = ent->port_ops;
4453 ap->cbl = ATA_CBL_NONE;
4454 ap->active_tag = ATA_TAG_POISON;
4455 ap->last_ctl = 0xFF;
4456
4457 INIT_WORK(&ap->packet_task, atapi_packet_task, ap);
4458 INIT_WORK(&ap->pio_task, ata_pio_task, ap);
Tejun Heoa72ec4c2006-01-23 13:09:37 +09004459 INIT_LIST_HEAD(&ap->eh_done_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460
4461 for (i = 0; i < ATA_MAX_DEVICES; i++)
4462 ap->device[i].devno = i;
4463
4464#ifdef ATA_IRQ_TRAP
4465 ap->stats.unhandled_irq = 1;
4466 ap->stats.idle_irq = 1;
4467#endif
4468
4469 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
4470}
4471
4472/**
4473 * ata_host_add - Attach low-level ATA driver to system
4474 * @ent: Information provided by low-level driver
4475 * @host_set: Collections of ports to which we add
4476 * @port_no: Port number associated with this host
4477 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004478 * Attach low-level ATA driver to system.
4479 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004481 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482 *
4483 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004484 * New ata_port on success, for NULL on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485 */
4486
Jeff Garzik057ace52005-10-22 14:27:05 -04004487static struct ata_port * ata_host_add(const struct ata_probe_ent *ent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488 struct ata_host_set *host_set,
4489 unsigned int port_no)
4490{
4491 struct Scsi_Host *host;
4492 struct ata_port *ap;
4493 int rc;
4494
4495 DPRINTK("ENTER\n");
4496 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
4497 if (!host)
4498 return NULL;
4499
4500 ap = (struct ata_port *) &host->hostdata[0];
4501
4502 ata_host_init(ap, host, host_set, ent, port_no);
4503
4504 rc = ap->ops->port_start(ap);
4505 if (rc)
4506 goto err_out;
4507
4508 return ap;
4509
4510err_out:
4511 scsi_host_put(host);
4512 return NULL;
4513}
4514
4515/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004516 * ata_device_add - Register hardware device with ATA and SCSI layers
4517 * @ent: Probe information describing hardware device to be registered
4518 *
4519 * This function processes the information provided in the probe
4520 * information struct @ent, allocates the necessary ATA and SCSI
4521 * host information structures, initializes them, and registers
4522 * everything with requisite kernel subsystems.
4523 *
4524 * This function requests irqs, probes the ATA bus, and probes
4525 * the SCSI bus.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526 *
4527 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004528 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529 *
4530 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004531 * Number of ports registered. Zero on error (no ports registered).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532 */
4533
Jeff Garzik057ace52005-10-22 14:27:05 -04004534int ata_device_add(const struct ata_probe_ent *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535{
4536 unsigned int count = 0, i;
4537 struct device *dev = ent->dev;
4538 struct ata_host_set *host_set;
4539
4540 DPRINTK("ENTER\n");
4541 /* alloc a container for our list of ATA ports (buses) */
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004542 host_set = kzalloc(sizeof(struct ata_host_set) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
4544 if (!host_set)
4545 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546 spin_lock_init(&host_set->lock);
4547
4548 host_set->dev = dev;
4549 host_set->n_ports = ent->n_ports;
4550 host_set->irq = ent->irq;
4551 host_set->mmio_base = ent->mmio_base;
4552 host_set->private_data = ent->private_data;
4553 host_set->ops = ent->port_ops;
4554
4555 /* register each port bound to this device */
4556 for (i = 0; i < ent->n_ports; i++) {
4557 struct ata_port *ap;
4558 unsigned long xfer_mode_mask;
4559
4560 ap = ata_host_add(ent, host_set, i);
4561 if (!ap)
4562 goto err_out;
4563
4564 host_set->ports[i] = ap;
4565 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
4566 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
4567 (ap->pio_mask << ATA_SHIFT_PIO);
4568
4569 /* print per-port info to dmesg */
4570 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
4571 "bmdma 0x%lX irq %lu\n",
4572 ap->id,
4573 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
4574 ata_mode_string(xfer_mode_mask),
4575 ap->ioaddr.cmd_addr,
4576 ap->ioaddr.ctl_addr,
4577 ap->ioaddr.bmdma_addr,
4578 ent->irq);
4579
4580 ata_chk_status(ap);
4581 host_set->ops->irq_clear(ap);
4582 count++;
4583 }
4584
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004585 if (!count)
4586 goto err_free_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587
4588 /* obtain irq, that is shared between channels */
4589 if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
4590 DRV_NAME, host_set))
4591 goto err_out;
4592
4593 /* perform each probe synchronously */
4594 DPRINTK("probe begin\n");
4595 for (i = 0; i < count; i++) {
4596 struct ata_port *ap;
4597 int rc;
4598
4599 ap = host_set->ports[i];
4600
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004601 DPRINTK("ata%u: bus probe begin\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602 rc = ata_bus_probe(ap);
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004603 DPRINTK("ata%u: bus probe end\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604
4605 if (rc) {
4606 /* FIXME: do something useful here?
4607 * Current libata behavior will
4608 * tear down everything when
4609 * the module is removed
4610 * or the h/w is unplugged.
4611 */
4612 }
4613
4614 rc = scsi_add_host(ap->host, dev);
4615 if (rc) {
4616 printk(KERN_ERR "ata%u: scsi_add_host failed\n",
4617 ap->id);
4618 /* FIXME: do something useful here */
4619 /* FIXME: handle unconditional calls to
4620 * scsi_scan_host and ata_host_remove, below,
4621 * at the very least
4622 */
4623 }
4624 }
4625
4626 /* probes are done, now scan each port's disk(s) */
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004627 DPRINTK("host probe begin\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628 for (i = 0; i < count; i++) {
4629 struct ata_port *ap = host_set->ports[i];
4630
Jeff Garzik644dd0c2005-10-03 15:55:19 -04004631 ata_scsi_scan_host(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632 }
4633
4634 dev_set_drvdata(dev, host_set);
4635
4636 VPRINTK("EXIT, returning %u\n", ent->n_ports);
4637 return ent->n_ports; /* success */
4638
4639err_out:
4640 for (i = 0; i < count; i++) {
4641 ata_host_remove(host_set->ports[i], 1);
4642 scsi_host_put(host_set->ports[i]->host);
4643 }
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004644err_free_ret:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004645 kfree(host_set);
4646 VPRINTK("EXIT, returning 0\n");
4647 return 0;
4648}
4649
4650/**
Alan Cox17b14452005-09-15 15:44:00 +01004651 * ata_host_set_remove - PCI layer callback for device removal
4652 * @host_set: ATA host set that was removed
4653 *
4654 * Unregister all objects associated with this host set. Free those
4655 * objects.
4656 *
4657 * LOCKING:
4658 * Inherited from calling layer (may sleep).
4659 */
4660
Alan Cox17b14452005-09-15 15:44:00 +01004661void ata_host_set_remove(struct ata_host_set *host_set)
4662{
4663 struct ata_port *ap;
4664 unsigned int i;
4665
4666 for (i = 0; i < host_set->n_ports; i++) {
4667 ap = host_set->ports[i];
4668 scsi_remove_host(ap->host);
4669 }
4670
4671 free_irq(host_set->irq, host_set);
4672
4673 for (i = 0; i < host_set->n_ports; i++) {
4674 ap = host_set->ports[i];
4675
4676 ata_scsi_release(ap->host);
4677
4678 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
4679 struct ata_ioports *ioaddr = &ap->ioaddr;
4680
4681 if (ioaddr->cmd_addr == 0x1f0)
4682 release_region(0x1f0, 8);
4683 else if (ioaddr->cmd_addr == 0x170)
4684 release_region(0x170, 8);
4685 }
4686
4687 scsi_host_put(ap->host);
4688 }
4689
4690 if (host_set->ops->host_stop)
4691 host_set->ops->host_stop(host_set);
4692
4693 kfree(host_set);
4694}
4695
4696/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697 * ata_scsi_release - SCSI layer callback hook for host unload
4698 * @host: libata host to be unloaded
4699 *
4700 * Performs all duties necessary to shut down a libata port...
4701 * Kill port kthread, disable port, and release resources.
4702 *
4703 * LOCKING:
4704 * Inherited from SCSI layer.
4705 *
4706 * RETURNS:
4707 * One.
4708 */
4709
4710int ata_scsi_release(struct Scsi_Host *host)
4711{
4712 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
4713
4714 DPRINTK("ENTER\n");
4715
4716 ap->ops->port_disable(ap);
4717 ata_host_remove(ap, 0);
4718
4719 DPRINTK("EXIT\n");
4720 return 1;
4721}
4722
4723/**
4724 * ata_std_ports - initialize ioaddr with standard port offsets.
4725 * @ioaddr: IO address structure to be initialized
Edward Falk0baab862005-06-02 18:17:13 -04004726 *
4727 * Utility function which initializes data_addr, error_addr,
4728 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
4729 * device_addr, status_addr, and command_addr to standard offsets
4730 * relative to cmd_addr.
4731 *
4732 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733 */
Edward Falk0baab862005-06-02 18:17:13 -04004734
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735void ata_std_ports(struct ata_ioports *ioaddr)
4736{
4737 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
4738 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
4739 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
4740 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
4741 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
4742 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
4743 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
4744 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
4745 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
4746 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
4747}
4748
Edward Falk0baab862005-06-02 18:17:13 -04004749
Jeff Garzik374b1872005-08-30 05:42:52 -04004750#ifdef CONFIG_PCI
4751
4752void ata_pci_host_stop (struct ata_host_set *host_set)
4753{
4754 struct pci_dev *pdev = to_pci_dev(host_set->dev);
4755
4756 pci_iounmap(pdev, host_set->mmio_base);
4757}
4758
Edward Falk0baab862005-06-02 18:17:13 -04004759/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760 * ata_pci_remove_one - PCI layer callback for device removal
4761 * @pdev: PCI device that was removed
4762 *
4763 * PCI layer indicates to libata via this hook that
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004764 * hot-unplug or module unload event has occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765 * Handle this by unregistering all objects associated
4766 * with this PCI device. Free those objects. Then finally
4767 * release PCI resources and disable device.
4768 *
4769 * LOCKING:
4770 * Inherited from PCI layer (may sleep).
4771 */
4772
4773void ata_pci_remove_one (struct pci_dev *pdev)
4774{
4775 struct device *dev = pci_dev_to_dev(pdev);
4776 struct ata_host_set *host_set = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777
Alan Cox17b14452005-09-15 15:44:00 +01004778 ata_host_set_remove(host_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779 pci_release_regions(pdev);
4780 pci_disable_device(pdev);
4781 dev_set_drvdata(dev, NULL);
4782}
4783
4784/* move to PCI subsystem */
Jeff Garzik057ace52005-10-22 14:27:05 -04004785int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004786{
4787 unsigned long tmp = 0;
4788
4789 switch (bits->width) {
4790 case 1: {
4791 u8 tmp8 = 0;
4792 pci_read_config_byte(pdev, bits->reg, &tmp8);
4793 tmp = tmp8;
4794 break;
4795 }
4796 case 2: {
4797 u16 tmp16 = 0;
4798 pci_read_config_word(pdev, bits->reg, &tmp16);
4799 tmp = tmp16;
4800 break;
4801 }
4802 case 4: {
4803 u32 tmp32 = 0;
4804 pci_read_config_dword(pdev, bits->reg, &tmp32);
4805 tmp = tmp32;
4806 break;
4807 }
4808
4809 default:
4810 return -EINVAL;
4811 }
4812
4813 tmp &= bits->mask;
4814
4815 return (tmp == bits->val) ? 1 : 0;
4816}
Jens Axboe9b847542006-01-06 09:28:07 +01004817
4818int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t state)
4819{
4820 pci_save_state(pdev);
4821 pci_disable_device(pdev);
4822 pci_set_power_state(pdev, PCI_D3hot);
4823 return 0;
4824}
4825
4826int ata_pci_device_resume(struct pci_dev *pdev)
4827{
4828 pci_set_power_state(pdev, PCI_D0);
4829 pci_restore_state(pdev);
4830 pci_enable_device(pdev);
4831 pci_set_master(pdev);
4832 return 0;
4833}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834#endif /* CONFIG_PCI */
4835
4836
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837static int __init ata_init(void)
4838{
4839 ata_wq = create_workqueue("ata");
4840 if (!ata_wq)
4841 return -ENOMEM;
4842
4843 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
4844 return 0;
4845}
4846
4847static void __exit ata_exit(void)
4848{
4849 destroy_workqueue(ata_wq);
4850}
4851
4852module_init(ata_init);
4853module_exit(ata_exit);
4854
Jeff Garzik67846b32005-10-05 02:58:32 -04004855static unsigned long ratelimit_time;
4856static spinlock_t ata_ratelimit_lock = SPIN_LOCK_UNLOCKED;
4857
4858int ata_ratelimit(void)
4859{
4860 int rc;
4861 unsigned long flags;
4862
4863 spin_lock_irqsave(&ata_ratelimit_lock, flags);
4864
4865 if (time_after(jiffies, ratelimit_time)) {
4866 rc = 1;
4867 ratelimit_time = jiffies + (HZ/5);
4868 } else
4869 rc = 0;
4870
4871 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
4872
4873 return rc;
4874}
4875
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876/*
4877 * libata is essentially a library of internal helper functions for
4878 * low-level ATA host controller drivers. As such, the API/ABI is
4879 * likely to change as new drivers are added and updated.
4880 * Do not depend on ABI/API stability.
4881 */
4882
4883EXPORT_SYMBOL_GPL(ata_std_bios_param);
4884EXPORT_SYMBOL_GPL(ata_std_ports);
4885EXPORT_SYMBOL_GPL(ata_device_add);
Alan Cox17b14452005-09-15 15:44:00 +01004886EXPORT_SYMBOL_GPL(ata_host_set_remove);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887EXPORT_SYMBOL_GPL(ata_sg_init);
4888EXPORT_SYMBOL_GPL(ata_sg_init_one);
Tejun Heo76014422006-02-11 15:13:49 +09004889EXPORT_SYMBOL_GPL(__ata_qc_complete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
4891EXPORT_SYMBOL_GPL(ata_eng_timeout);
4892EXPORT_SYMBOL_GPL(ata_tf_load);
4893EXPORT_SYMBOL_GPL(ata_tf_read);
4894EXPORT_SYMBOL_GPL(ata_noop_dev_select);
4895EXPORT_SYMBOL_GPL(ata_std_dev_select);
4896EXPORT_SYMBOL_GPL(ata_tf_to_fis);
4897EXPORT_SYMBOL_GPL(ata_tf_from_fis);
4898EXPORT_SYMBOL_GPL(ata_check_status);
4899EXPORT_SYMBOL_GPL(ata_altstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004900EXPORT_SYMBOL_GPL(ata_exec_command);
4901EXPORT_SYMBOL_GPL(ata_port_start);
4902EXPORT_SYMBOL_GPL(ata_port_stop);
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04004903EXPORT_SYMBOL_GPL(ata_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904EXPORT_SYMBOL_GPL(ata_interrupt);
4905EXPORT_SYMBOL_GPL(ata_qc_prep);
4906EXPORT_SYMBOL_GPL(ata_bmdma_setup);
4907EXPORT_SYMBOL_GPL(ata_bmdma_start);
4908EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
4909EXPORT_SYMBOL_GPL(ata_bmdma_status);
4910EXPORT_SYMBOL_GPL(ata_bmdma_stop);
4911EXPORT_SYMBOL_GPL(ata_port_probe);
4912EXPORT_SYMBOL_GPL(sata_phy_reset);
4913EXPORT_SYMBOL_GPL(__sata_phy_reset);
4914EXPORT_SYMBOL_GPL(ata_bus_reset);
Tejun Heo8a19ac82006-02-02 18:20:00 +09004915EXPORT_SYMBOL_GPL(ata_std_probeinit);
Tejun Heoc2bd5802006-01-24 17:05:22 +09004916EXPORT_SYMBOL_GPL(ata_std_softreset);
4917EXPORT_SYMBOL_GPL(sata_std_hardreset);
4918EXPORT_SYMBOL_GPL(ata_std_postreset);
4919EXPORT_SYMBOL_GPL(ata_std_probe_reset);
Tejun Heoa62c0fc2006-01-24 17:05:22 +09004920EXPORT_SYMBOL_GPL(ata_drive_probe_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921EXPORT_SYMBOL_GPL(ata_port_disable);
Jeff Garzik67846b32005-10-05 02:58:32 -04004922EXPORT_SYMBOL_GPL(ata_ratelimit);
Tejun Heo6f8b9952006-01-24 17:05:21 +09004923EXPORT_SYMBOL_GPL(ata_busy_sleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
4925EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
Tejun Heof29841e2006-02-10 15:10:48 +09004926EXPORT_SYMBOL_GPL(ata_scsi_timed_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004927EXPORT_SYMBOL_GPL(ata_scsi_error);
4928EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
4929EXPORT_SYMBOL_GPL(ata_scsi_release);
4930EXPORT_SYMBOL_GPL(ata_host_intr);
4931EXPORT_SYMBOL_GPL(ata_dev_classify);
4932EXPORT_SYMBOL_GPL(ata_dev_id_string);
Tejun Heo0e949ff2006-02-12 22:47:04 +09004933EXPORT_SYMBOL_GPL(ata_dev_id_c_string);
Brad Campbell6f2f3812005-05-12 15:07:47 -04004934EXPORT_SYMBOL_GPL(ata_dev_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935EXPORT_SYMBOL_GPL(ata_scsi_simulate);
Tejun Heoa72ec4c2006-01-23 13:09:37 +09004936EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
4937EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938
Alan Cox1bc4ccf2006-01-09 17:18:14 +00004939EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
Alan Cox452503f2005-10-21 19:01:32 -04004940EXPORT_SYMBOL_GPL(ata_timing_compute);
4941EXPORT_SYMBOL_GPL(ata_timing_merge);
4942
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943#ifdef CONFIG_PCI
4944EXPORT_SYMBOL_GPL(pci_test_config_bits);
Jeff Garzik374b1872005-08-30 05:42:52 -04004945EXPORT_SYMBOL_GPL(ata_pci_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
4947EXPORT_SYMBOL_GPL(ata_pci_init_one);
4948EXPORT_SYMBOL_GPL(ata_pci_remove_one);
Jens Axboe9b847542006-01-06 09:28:07 +01004949EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
4950EXPORT_SYMBOL_GPL(ata_pci_device_resume);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951#endif /* CONFIG_PCI */
Jens Axboe9b847542006-01-06 09:28:07 +01004952
4953EXPORT_SYMBOL_GPL(ata_device_suspend);
4954EXPORT_SYMBOL_GPL(ata_device_resume);
4955EXPORT_SYMBOL_GPL(ata_scsi_device_suspend);
4956EXPORT_SYMBOL_GPL(ata_scsi_device_resume);