blob: d0a26e9553a1dbddfda3950612bb17fde0241f6e [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);
Tejun Heo6aff8f12006-02-15 18:24:09 +090065static unsigned int ata_dev_init_params(struct ata_port *ap,
66 struct ata_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067static void ata_set_mode(struct ata_port *ap);
68static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev);
Jeff Garzik057ace52005-10-22 14:27:05 -040069static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070static int fgb(u32 bitmap);
Jeff Garzik057ace52005-10-22 14:27:05 -040071static int ata_choose_xfer_mode(const struct ata_port *ap,
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 u8 *xfer_mode_out,
73 unsigned int *xfer_shift_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75static unsigned int ata_unique_id = 1;
76static struct workqueue_struct *ata_wq;
77
Jeff Garzik1623c812005-08-30 03:37:42 -040078int atapi_enabled = 0;
79module_param(atapi_enabled, int, 0444);
80MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)");
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082MODULE_AUTHOR("Jeff Garzik");
83MODULE_DESCRIPTION("Library module for ATA devices");
84MODULE_LICENSE("GPL");
85MODULE_VERSION(DRV_VERSION);
86
Edward Falk0baab862005-06-02 18:17:13 -040087
88/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
90 * @tf: Taskfile to convert
91 * @fis: Buffer into which data will output
92 * @pmp: Port multiplier port
93 *
94 * Converts a standard ATA taskfile to a Serial ATA
95 * FIS structure (Register - Host to Device).
96 *
97 * LOCKING:
98 * Inherited from caller.
99 */
100
Jeff Garzik057ace52005-10-22 14:27:05 -0400101void ata_tf_to_fis(const struct ata_taskfile *tf, u8 *fis, u8 pmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
103 fis[0] = 0x27; /* Register - Host to Device FIS */
104 fis[1] = (pmp & 0xf) | (1 << 7); /* Port multiplier number,
105 bit 7 indicates Command FIS */
106 fis[2] = tf->command;
107 fis[3] = tf->feature;
108
109 fis[4] = tf->lbal;
110 fis[5] = tf->lbam;
111 fis[6] = tf->lbah;
112 fis[7] = tf->device;
113
114 fis[8] = tf->hob_lbal;
115 fis[9] = tf->hob_lbam;
116 fis[10] = tf->hob_lbah;
117 fis[11] = tf->hob_feature;
118
119 fis[12] = tf->nsect;
120 fis[13] = tf->hob_nsect;
121 fis[14] = 0;
122 fis[15] = tf->ctl;
123
124 fis[16] = 0;
125 fis[17] = 0;
126 fis[18] = 0;
127 fis[19] = 0;
128}
129
130/**
131 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
132 * @fis: Buffer from which data will be input
133 * @tf: Taskfile to output
134 *
Mark Lorde12a1be2005-11-12 18:55:45 -0500135 * Converts a serial ATA FIS structure to a standard ATA taskfile.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 *
137 * LOCKING:
138 * Inherited from caller.
139 */
140
Jeff Garzik057ace52005-10-22 14:27:05 -0400141void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
143 tf->command = fis[2]; /* status */
144 tf->feature = fis[3]; /* error */
145
146 tf->lbal = fis[4];
147 tf->lbam = fis[5];
148 tf->lbah = fis[6];
149 tf->device = fis[7];
150
151 tf->hob_lbal = fis[8];
152 tf->hob_lbam = fis[9];
153 tf->hob_lbah = fis[10];
154
155 tf->nsect = fis[12];
156 tf->hob_nsect = fis[13];
157}
158
Albert Lee8cbd6df2005-10-12 15:06:27 +0800159static const u8 ata_rw_cmds[] = {
160 /* pio multi */
161 ATA_CMD_READ_MULTI,
162 ATA_CMD_WRITE_MULTI,
163 ATA_CMD_READ_MULTI_EXT,
164 ATA_CMD_WRITE_MULTI_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100165 0,
166 0,
167 0,
168 ATA_CMD_WRITE_MULTI_FUA_EXT,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800169 /* pio */
170 ATA_CMD_PIO_READ,
171 ATA_CMD_PIO_WRITE,
172 ATA_CMD_PIO_READ_EXT,
173 ATA_CMD_PIO_WRITE_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100174 0,
175 0,
176 0,
177 0,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800178 /* dma */
179 ATA_CMD_READ,
180 ATA_CMD_WRITE,
181 ATA_CMD_READ_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100182 ATA_CMD_WRITE_EXT,
183 0,
184 0,
185 0,
186 ATA_CMD_WRITE_FUA_EXT
Albert Lee8cbd6df2005-10-12 15:06:27 +0800187};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
189/**
Albert Lee8cbd6df2005-10-12 15:06:27 +0800190 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
191 * @qc: command to examine and configure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 *
Albert Lee8cbd6df2005-10-12 15:06:27 +0800193 * Examine the device configuration and tf->flags to calculate
194 * the proper read/write commands and protocol to use.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 *
196 * LOCKING:
197 * caller.
198 */
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100199int ata_rwcmd_protocol(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
Albert Lee8cbd6df2005-10-12 15:06:27 +0800201 struct ata_taskfile *tf = &qc->tf;
202 struct ata_device *dev = qc->dev;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100203 u8 cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100205 int index, fua, lba48, write;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800206
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100207 fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800208 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
209 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Albert Lee8cbd6df2005-10-12 15:06:27 +0800211 if (dev->flags & ATA_DFLAG_PIO) {
212 tf->protocol = ATA_PROT_PIO;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100213 index = dev->multi_count ? 0 : 8;
Alan Cox8d238e02006-01-17 20:50:31 +0000214 } else if (lba48 && (qc->ap->flags & ATA_FLAG_PIO_LBA48)) {
215 /* Unable to use DMA due to host limitation */
216 tf->protocol = ATA_PROT_PIO;
Albert Lee0565c262006-02-13 18:55:25 +0800217 index = dev->multi_count ? 0 : 8;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800218 } else {
219 tf->protocol = ATA_PROT_DMA;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100220 index = 16;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100223 cmd = ata_rw_cmds[index + fua + lba48 + write];
224 if (cmd) {
225 tf->command = cmd;
226 return 0;
227 }
228 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229}
230
Arjan van de Ven98ac62d2005-11-28 10:06:23 +0100231static const char * const xfer_mode_str[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 "UDMA/16",
233 "UDMA/25",
234 "UDMA/33",
235 "UDMA/44",
236 "UDMA/66",
237 "UDMA/100",
238 "UDMA/133",
239 "UDMA7",
240 "MWDMA0",
241 "MWDMA1",
242 "MWDMA2",
243 "PIO0",
244 "PIO1",
245 "PIO2",
246 "PIO3",
247 "PIO4",
248};
249
250/**
251 * ata_udma_string - convert UDMA bit offset to string
252 * @mask: mask of bits supported; only highest bit counts.
253 *
254 * Determine string which represents the highest speed
255 * (highest bit in @udma_mask).
256 *
257 * LOCKING:
258 * None.
259 *
260 * RETURNS:
261 * Constant C string representing highest speed listed in
262 * @udma_mask, or the constant C string "<n/a>".
263 */
264
265static const char *ata_mode_string(unsigned int mask)
266{
267 int i;
268
269 for (i = 7; i >= 0; i--)
270 if (mask & (1 << i))
271 goto out;
272 for (i = ATA_SHIFT_MWDMA + 2; i >= ATA_SHIFT_MWDMA; i--)
273 if (mask & (1 << i))
274 goto out;
275 for (i = ATA_SHIFT_PIO + 4; i >= ATA_SHIFT_PIO; i--)
276 if (mask & (1 << i))
277 goto out;
278
279 return "<n/a>";
280
281out:
282 return xfer_mode_str[i];
283}
284
285/**
286 * ata_pio_devchk - PATA device presence detection
287 * @ap: ATA channel to examine
288 * @device: Device to examine (starting at zero)
289 *
290 * This technique was originally described in
291 * Hale Landis's ATADRVR (www.ata-atapi.com), and
292 * later found its way into the ATA/ATAPI spec.
293 *
294 * Write a pattern to the ATA shadow registers,
295 * and if a device is present, it will respond by
296 * correctly storing and echoing back the
297 * ATA shadow register contents.
298 *
299 * LOCKING:
300 * caller.
301 */
302
303static unsigned int ata_pio_devchk(struct ata_port *ap,
304 unsigned int device)
305{
306 struct ata_ioports *ioaddr = &ap->ioaddr;
307 u8 nsect, lbal;
308
309 ap->ops->dev_select(ap, device);
310
311 outb(0x55, ioaddr->nsect_addr);
312 outb(0xaa, ioaddr->lbal_addr);
313
314 outb(0xaa, ioaddr->nsect_addr);
315 outb(0x55, ioaddr->lbal_addr);
316
317 outb(0x55, ioaddr->nsect_addr);
318 outb(0xaa, ioaddr->lbal_addr);
319
320 nsect = inb(ioaddr->nsect_addr);
321 lbal = inb(ioaddr->lbal_addr);
322
323 if ((nsect == 0x55) && (lbal == 0xaa))
324 return 1; /* we found a device */
325
326 return 0; /* nothing found */
327}
328
329/**
330 * ata_mmio_devchk - PATA device presence detection
331 * @ap: ATA channel to examine
332 * @device: Device to examine (starting at zero)
333 *
334 * This technique was originally described in
335 * Hale Landis's ATADRVR (www.ata-atapi.com), and
336 * later found its way into the ATA/ATAPI spec.
337 *
338 * Write a pattern to the ATA shadow registers,
339 * and if a device is present, it will respond by
340 * correctly storing and echoing back the
341 * ATA shadow register contents.
342 *
343 * LOCKING:
344 * caller.
345 */
346
347static unsigned int ata_mmio_devchk(struct ata_port *ap,
348 unsigned int device)
349{
350 struct ata_ioports *ioaddr = &ap->ioaddr;
351 u8 nsect, lbal;
352
353 ap->ops->dev_select(ap, device);
354
355 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
356 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
357
358 writeb(0xaa, (void __iomem *) ioaddr->nsect_addr);
359 writeb(0x55, (void __iomem *) ioaddr->lbal_addr);
360
361 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
362 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
363
364 nsect = readb((void __iomem *) ioaddr->nsect_addr);
365 lbal = readb((void __iomem *) ioaddr->lbal_addr);
366
367 if ((nsect == 0x55) && (lbal == 0xaa))
368 return 1; /* we found a device */
369
370 return 0; /* nothing found */
371}
372
373/**
374 * ata_devchk - PATA device presence detection
375 * @ap: ATA channel to examine
376 * @device: Device to examine (starting at zero)
377 *
378 * Dispatch ATA device presence detection, depending
379 * on whether we are using PIO or MMIO to talk to the
380 * ATA shadow registers.
381 *
382 * LOCKING:
383 * caller.
384 */
385
386static unsigned int ata_devchk(struct ata_port *ap,
387 unsigned int device)
388{
389 if (ap->flags & ATA_FLAG_MMIO)
390 return ata_mmio_devchk(ap, device);
391 return ata_pio_devchk(ap, device);
392}
393
394/**
395 * ata_dev_classify - determine device type based on ATA-spec signature
396 * @tf: ATA taskfile register set for device to be identified
397 *
398 * Determine from taskfile register contents whether a device is
399 * ATA or ATAPI, as per "Signature and persistence" section
400 * of ATA/PI spec (volume 1, sect 5.14).
401 *
402 * LOCKING:
403 * None.
404 *
405 * RETURNS:
406 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
407 * the event of failure.
408 */
409
Jeff Garzik057ace52005-10-22 14:27:05 -0400410unsigned int ata_dev_classify(const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411{
412 /* Apple's open source Darwin code hints that some devices only
413 * put a proper signature into the LBA mid/high registers,
414 * So, we only check those. It's sufficient for uniqueness.
415 */
416
417 if (((tf->lbam == 0) && (tf->lbah == 0)) ||
418 ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
419 DPRINTK("found ATA device by sig\n");
420 return ATA_DEV_ATA;
421 }
422
423 if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
424 ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
425 DPRINTK("found ATAPI device by sig\n");
426 return ATA_DEV_ATAPI;
427 }
428
429 DPRINTK("unknown device\n");
430 return ATA_DEV_UNKNOWN;
431}
432
433/**
434 * ata_dev_try_classify - Parse returned ATA device signature
435 * @ap: ATA channel to examine
436 * @device: Device to examine (starting at zero)
Tejun Heob4dc7622006-01-24 17:05:22 +0900437 * @r_err: Value of error register on completion
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 *
439 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
440 * an ATA/ATAPI-defined set of values is placed in the ATA
441 * shadow registers, indicating the results of device detection
442 * and diagnostics.
443 *
444 * Select the ATA device, and read the values from the ATA shadow
445 * registers. Then parse according to the Error register value,
446 * and the spec-defined values examined by ata_dev_classify().
447 *
448 * LOCKING:
449 * caller.
Tejun Heob4dc7622006-01-24 17:05:22 +0900450 *
451 * RETURNS:
452 * Device type - %ATA_DEV_ATA, %ATA_DEV_ATAPI or %ATA_DEV_NONE.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 */
454
Tejun Heob4dc7622006-01-24 17:05:22 +0900455static unsigned int
456ata_dev_try_classify(struct ata_port *ap, unsigned int device, u8 *r_err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 struct ata_taskfile tf;
459 unsigned int class;
460 u8 err;
461
462 ap->ops->dev_select(ap, device);
463
464 memset(&tf, 0, sizeof(tf));
465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 ap->ops->tf_read(ap, &tf);
Jeff Garzik0169e282005-10-29 21:25:10 -0400467 err = tf.feature;
Tejun Heob4dc7622006-01-24 17:05:22 +0900468 if (r_err)
469 *r_err = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
471 /* see if device passed diags */
472 if (err == 1)
473 /* do nothing */ ;
474 else if ((device == 0) && (err == 0x81))
475 /* do nothing */ ;
476 else
Tejun Heob4dc7622006-01-24 17:05:22 +0900477 return ATA_DEV_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Tejun Heob4dc7622006-01-24 17:05:22 +0900479 /* determine if device is ATA or ATAPI */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 class = ata_dev_classify(&tf);
Tejun Heob4dc7622006-01-24 17:05:22 +0900481
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 if (class == ATA_DEV_UNKNOWN)
Tejun Heob4dc7622006-01-24 17:05:22 +0900483 return ATA_DEV_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
Tejun Heob4dc7622006-01-24 17:05:22 +0900485 return ATA_DEV_NONE;
486 return class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487}
488
489/**
Tejun Heo6a62a042006-02-13 10:02:46 +0900490 * ata_id_string - Convert IDENTIFY DEVICE page into string
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 * @id: IDENTIFY DEVICE results we will examine
492 * @s: string into which data is output
493 * @ofs: offset into identify device page
494 * @len: length of string to return. must be an even number.
495 *
496 * The strings in the IDENTIFY DEVICE page are broken up into
497 * 16-bit chunks. Run through the string, and output each
498 * 8-bit chunk linearly, regardless of platform.
499 *
500 * LOCKING:
501 * caller.
502 */
503
Tejun Heo6a62a042006-02-13 10:02:46 +0900504void ata_id_string(const u16 *id, unsigned char *s,
505 unsigned int ofs, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
507 unsigned int c;
508
509 while (len > 0) {
510 c = id[ofs] >> 8;
511 *s = c;
512 s++;
513
514 c = id[ofs] & 0xff;
515 *s = c;
516 s++;
517
518 ofs++;
519 len -= 2;
520 }
521}
522
Tejun Heo0e949ff2006-02-12 22:47:04 +0900523/**
Tejun Heo6a62a042006-02-13 10:02:46 +0900524 * ata_id_c_string - Convert IDENTIFY DEVICE page into C string
Tejun Heo0e949ff2006-02-12 22:47:04 +0900525 * @id: IDENTIFY DEVICE results we will examine
526 * @s: string into which data is output
527 * @ofs: offset into identify device page
528 * @len: length of string to return. must be an odd number.
529 *
Tejun Heo6a62a042006-02-13 10:02:46 +0900530 * This function is identical to ata_id_string except that it
Tejun Heo0e949ff2006-02-12 22:47:04 +0900531 * trims trailing spaces and terminates the resulting string with
532 * null. @len must be actual maximum length (even number) + 1.
533 *
534 * LOCKING:
535 * caller.
536 */
Tejun Heo6a62a042006-02-13 10:02:46 +0900537void ata_id_c_string(const u16 *id, unsigned char *s,
538 unsigned int ofs, unsigned int len)
Tejun Heo0e949ff2006-02-12 22:47:04 +0900539{
540 unsigned char *p;
541
542 WARN_ON(!(len & 1));
543
Tejun Heo6a62a042006-02-13 10:02:46 +0900544 ata_id_string(id, s, ofs, len - 1);
Tejun Heo0e949ff2006-02-12 22:47:04 +0900545
546 p = s + strnlen(s, len - 1);
547 while (p > s && p[-1] == ' ')
548 p--;
549 *p = '\0';
550}
Edward Falk0baab862005-06-02 18:17:13 -0400551
Tejun Heo29407402006-02-12 22:47:04 +0900552static u64 ata_id_n_sectors(const u16 *id)
553{
554 if (ata_id_has_lba(id)) {
555 if (ata_id_has_lba48(id))
556 return ata_id_u64(id, 100);
557 else
558 return ata_id_u32(id, 60);
559 } else {
560 if (ata_id_current_chs_valid(id))
561 return ata_id_u32(id, 57);
562 else
563 return id[1] * id[3] * id[6];
564 }
565}
566
Edward Falk0baab862005-06-02 18:17:13 -0400567/**
568 * ata_noop_dev_select - Select device 0/1 on ATA bus
569 * @ap: ATA channel to manipulate
570 * @device: ATA device (numbered from zero) to select
571 *
572 * This function performs no actual function.
573 *
574 * May be used as the dev_select() entry in ata_port_operations.
575 *
576 * LOCKING:
577 * caller.
578 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
580{
581}
582
Edward Falk0baab862005-06-02 18:17:13 -0400583
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584/**
585 * ata_std_dev_select - Select device 0/1 on ATA bus
586 * @ap: ATA channel to manipulate
587 * @device: ATA device (numbered from zero) to select
588 *
589 * Use the method defined in the ATA specification to
590 * make either device 0, or device 1, active on the
Edward Falk0baab862005-06-02 18:17:13 -0400591 * ATA channel. Works with both PIO and MMIO.
592 *
593 * May be used as the dev_select() entry in ata_port_operations.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 *
595 * LOCKING:
596 * caller.
597 */
598
599void ata_std_dev_select (struct ata_port *ap, unsigned int device)
600{
601 u8 tmp;
602
603 if (device == 0)
604 tmp = ATA_DEVICE_OBS;
605 else
606 tmp = ATA_DEVICE_OBS | ATA_DEV1;
607
608 if (ap->flags & ATA_FLAG_MMIO) {
609 writeb(tmp, (void __iomem *) ap->ioaddr.device_addr);
610 } else {
611 outb(tmp, ap->ioaddr.device_addr);
612 }
613 ata_pause(ap); /* needed; also flushes, for mmio */
614}
615
616/**
617 * ata_dev_select - Select device 0/1 on ATA bus
618 * @ap: ATA channel to manipulate
619 * @device: ATA device (numbered from zero) to select
620 * @wait: non-zero to wait for Status register BSY bit to clear
621 * @can_sleep: non-zero if context allows sleeping
622 *
623 * Use the method defined in the ATA specification to
624 * make either device 0, or device 1, active on the
625 * ATA channel.
626 *
627 * This is a high-level version of ata_std_dev_select(),
628 * which additionally provides the services of inserting
629 * the proper pauses and status polling, where needed.
630 *
631 * LOCKING:
632 * caller.
633 */
634
635void ata_dev_select(struct ata_port *ap, unsigned int device,
636 unsigned int wait, unsigned int can_sleep)
637{
638 VPRINTK("ENTER, ata%u: device %u, wait %u\n",
639 ap->id, device, wait);
640
641 if (wait)
642 ata_wait_idle(ap);
643
644 ap->ops->dev_select(ap, device);
645
646 if (wait) {
647 if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
648 msleep(150);
649 ata_wait_idle(ap);
650 }
651}
652
653/**
654 * ata_dump_id - IDENTIFY DEVICE info debugging output
Tejun Heo0bd33002006-02-12 22:47:05 +0900655 * @id: IDENTIFY DEVICE page to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 *
Tejun Heo0bd33002006-02-12 22:47:05 +0900657 * Dump selected 16-bit words from the given IDENTIFY DEVICE
658 * page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 *
660 * LOCKING:
661 * caller.
662 */
663
Tejun Heo0bd33002006-02-12 22:47:05 +0900664static inline void ata_dump_id(const u16 *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
666 DPRINTK("49==0x%04x "
667 "53==0x%04x "
668 "63==0x%04x "
669 "64==0x%04x "
670 "75==0x%04x \n",
Tejun Heo0bd33002006-02-12 22:47:05 +0900671 id[49],
672 id[53],
673 id[63],
674 id[64],
675 id[75]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 DPRINTK("80==0x%04x "
677 "81==0x%04x "
678 "82==0x%04x "
679 "83==0x%04x "
680 "84==0x%04x \n",
Tejun Heo0bd33002006-02-12 22:47:05 +0900681 id[80],
682 id[81],
683 id[82],
684 id[83],
685 id[84]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 DPRINTK("88==0x%04x "
687 "93==0x%04x\n",
Tejun Heo0bd33002006-02-12 22:47:05 +0900688 id[88],
689 id[93]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690}
691
Alan Cox11e29e22005-10-21 18:46:32 -0400692/*
693 * Compute the PIO modes available for this device. This is not as
694 * trivial as it seems if we must consider early devices correctly.
695 *
696 * FIXME: pre IDE drive timing (do we care ?).
697 */
698
Jeff Garzik057ace52005-10-22 14:27:05 -0400699static unsigned int ata_pio_modes(const struct ata_device *adev)
Alan Cox11e29e22005-10-21 18:46:32 -0400700{
701 u16 modes;
702
Alan Coxffa29452006-01-09 17:14:40 +0000703 /* Usual case. Word 53 indicates word 64 is valid */
704 if (adev->id[ATA_ID_FIELD_VALID] & (1 << 1)) {
Alan Cox11e29e22005-10-21 18:46:32 -0400705 modes = adev->id[ATA_ID_PIO_MODES] & 0x03;
706 modes <<= 3;
707 modes |= 0x7;
708 return modes;
709 }
710
Alan Coxffa29452006-01-09 17:14:40 +0000711 /* If word 64 isn't valid then Word 51 high byte holds the PIO timing
712 number for the maximum. Turn it into a mask and return it */
713 modes = (2 << ((adev->id[ATA_ID_OLD_PIO_MODES] >> 8) & 0xFF)) - 1 ;
Alan Cox11e29e22005-10-21 18:46:32 -0400714 return modes;
Alan Coxffa29452006-01-09 17:14:40 +0000715 /* But wait.. there's more. Design your standards by committee and
716 you too can get a free iordy field to process. However its the
717 speeds not the modes that are supported... Note drivers using the
718 timing API will get this right anyway */
Alan Cox11e29e22005-10-21 18:46:32 -0400719}
720
Tejun Heo95064372006-01-23 13:09:37 +0900721static inline void
722ata_queue_packet_task(struct ata_port *ap)
723{
Tejun Heoc18d06f2006-02-02 00:56:10 +0900724 if (!(ap->flags & ATA_FLAG_FLUSH_PIO_TASK))
725 queue_work(ata_wq, &ap->packet_task);
Tejun Heo95064372006-01-23 13:09:37 +0900726}
727
728static inline void
729ata_queue_pio_task(struct ata_port *ap)
730{
Tejun Heoc18d06f2006-02-02 00:56:10 +0900731 if (!(ap->flags & ATA_FLAG_FLUSH_PIO_TASK))
732 queue_work(ata_wq, &ap->pio_task);
Tejun Heo95064372006-01-23 13:09:37 +0900733}
734
735static inline void
736ata_queue_delayed_pio_task(struct ata_port *ap, unsigned long delay)
737{
Tejun Heoc18d06f2006-02-02 00:56:10 +0900738 if (!(ap->flags & ATA_FLAG_FLUSH_PIO_TASK))
739 queue_delayed_work(ata_wq, &ap->pio_task, delay);
740}
741
742/**
743 * ata_flush_pio_tasks - Flush pio_task and packet_task
744 * @ap: the target ata_port
745 *
746 * After this function completes, pio_task and packet_task are
747 * guranteed not to be running or scheduled.
748 *
749 * LOCKING:
750 * Kernel thread context (may sleep)
751 */
752
753static void ata_flush_pio_tasks(struct ata_port *ap)
754{
755 int tmp = 0;
756 unsigned long flags;
757
758 DPRINTK("ENTER\n");
759
760 spin_lock_irqsave(&ap->host_set->lock, flags);
761 ap->flags |= ATA_FLAG_FLUSH_PIO_TASK;
762 spin_unlock_irqrestore(&ap->host_set->lock, flags);
763
764 DPRINTK("flush #1\n");
765 flush_workqueue(ata_wq);
766
767 /*
768 * At this point, if a task is running, it's guaranteed to see
769 * the FLUSH flag; thus, it will never queue pio tasks again.
770 * Cancel and flush.
771 */
772 tmp |= cancel_delayed_work(&ap->pio_task);
773 tmp |= cancel_delayed_work(&ap->packet_task);
774 if (!tmp) {
775 DPRINTK("flush #2\n");
776 flush_workqueue(ata_wq);
777 }
778
779 spin_lock_irqsave(&ap->host_set->lock, flags);
780 ap->flags &= ~ATA_FLAG_FLUSH_PIO_TASK;
781 spin_unlock_irqrestore(&ap->host_set->lock, flags);
782
783 DPRINTK("EXIT\n");
Tejun Heo95064372006-01-23 13:09:37 +0900784}
785
Tejun Heo77853bf2006-01-23 13:09:36 +0900786void ata_qc_complete_internal(struct ata_queued_cmd *qc)
Tejun Heoa2a7a662005-12-13 14:48:31 +0900787{
Tejun Heo77853bf2006-01-23 13:09:36 +0900788 struct completion *waiting = qc->private_data;
Tejun Heoa2a7a662005-12-13 14:48:31 +0900789
Tejun Heo77853bf2006-01-23 13:09:36 +0900790 qc->ap->ops->tf_read(qc->ap, &qc->tf);
Tejun Heoa2a7a662005-12-13 14:48:31 +0900791 complete(waiting);
Tejun Heoa2a7a662005-12-13 14:48:31 +0900792}
793
794/**
795 * ata_exec_internal - execute libata internal command
796 * @ap: Port to which the command is sent
797 * @dev: Device to which the command is sent
798 * @tf: Taskfile registers for the command and the result
799 * @dma_dir: Data tranfer direction of the command
800 * @buf: Data buffer of the command
801 * @buflen: Length of data buffer
802 *
803 * Executes libata internal command with timeout. @tf contains
804 * command on entry and result on return. Timeout and error
805 * conditions are reported via return value. No recovery action
806 * is taken after a command times out. It's caller's duty to
807 * clean up after timeout.
808 *
809 * LOCKING:
810 * None. Should be called with kernel context, might sleep.
811 */
812
813static unsigned
814ata_exec_internal(struct ata_port *ap, struct ata_device *dev,
815 struct ata_taskfile *tf,
816 int dma_dir, void *buf, unsigned int buflen)
817{
818 u8 command = tf->command;
819 struct ata_queued_cmd *qc;
820 DECLARE_COMPLETION(wait);
821 unsigned long flags;
Tejun Heo77853bf2006-01-23 13:09:36 +0900822 unsigned int err_mask;
Tejun Heoa2a7a662005-12-13 14:48:31 +0900823
824 spin_lock_irqsave(&ap->host_set->lock, flags);
825
826 qc = ata_qc_new_init(ap, dev);
827 BUG_ON(qc == NULL);
828
829 qc->tf = *tf;
830 qc->dma_dir = dma_dir;
831 if (dma_dir != DMA_NONE) {
832 ata_sg_init_one(qc, buf, buflen);
833 qc->nsect = buflen / ATA_SECT_SIZE;
834 }
835
Tejun Heo77853bf2006-01-23 13:09:36 +0900836 qc->private_data = &wait;
Tejun Heoa2a7a662005-12-13 14:48:31 +0900837 qc->complete_fn = ata_qc_complete_internal;
838
Tejun Heo9a3d9eb2006-01-23 13:09:36 +0900839 qc->err_mask = ata_qc_issue(qc);
840 if (qc->err_mask)
Tejun Heo8e436af2006-01-23 13:09:36 +0900841 ata_qc_complete(qc);
Tejun Heoa2a7a662005-12-13 14:48:31 +0900842
843 spin_unlock_irqrestore(&ap->host_set->lock, flags);
844
845 if (!wait_for_completion_timeout(&wait, ATA_TMOUT_INTERNAL)) {
846 spin_lock_irqsave(&ap->host_set->lock, flags);
847
848 /* We're racing with irq here. If we lose, the
849 * following test prevents us from completing the qc
850 * again. If completion irq occurs after here but
851 * before the caller cleans up, it will result in a
852 * spurious interrupt. We can live with that.
853 */
Tejun Heo77853bf2006-01-23 13:09:36 +0900854 if (qc->flags & ATA_QCFLAG_ACTIVE) {
Tejun Heo11a56d22006-01-23 13:09:36 +0900855 qc->err_mask = AC_ERR_TIMEOUT;
Tejun Heoa2a7a662005-12-13 14:48:31 +0900856 ata_qc_complete(qc);
857 printk(KERN_WARNING "ata%u: qc timeout (cmd 0x%x)\n",
858 ap->id, command);
859 }
860
861 spin_unlock_irqrestore(&ap->host_set->lock, flags);
862 }
863
Tejun Heo77853bf2006-01-23 13:09:36 +0900864 *tf = qc->tf;
865 err_mask = qc->err_mask;
866
867 ata_qc_free(qc);
868
869 return err_mask;
Tejun Heoa2a7a662005-12-13 14:48:31 +0900870}
871
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872/**
Alan Cox1bc4ccf2006-01-09 17:18:14 +0000873 * ata_pio_need_iordy - check if iordy needed
874 * @adev: ATA device
875 *
876 * Check if the current speed of the device requires IORDY. Used
877 * by various controllers for chip configuration.
878 */
879
880unsigned int ata_pio_need_iordy(const struct ata_device *adev)
881{
882 int pio;
883 int speed = adev->pio_mode - XFER_PIO_0;
884
885 if (speed < 2)
886 return 0;
887 if (speed > 2)
888 return 1;
889
890 /* If we have no drive specific rule, then PIO 2 is non IORDY */
891
892 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
893 pio = adev->id[ATA_ID_EIDE_PIO];
894 /* Is the speed faster than the drive allows non IORDY ? */
895 if (pio) {
896 /* This is cycle times not frequency - watch the logic! */
897 if (pio > 240) /* PIO2 is 240nS per cycle */
898 return 1;
899 return 0;
900 }
901 }
902 return 0;
903}
904
905/**
Tejun Heo49016ac2006-02-21 02:12:11 +0900906 * ata_dev_read_id - Read ID data from the specified device
907 * @ap: port on which target device resides
908 * @dev: target device
909 * @p_class: pointer to class of the target device (may be changed)
910 * @post_reset: is this read ID post-reset?
911 * @id: buffer to fill IDENTIFY page into
912 *
913 * Read ID data from the specified device. ATA_CMD_ID_ATA is
914 * performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
915 * devices. This function also takes care of EDD signature
916 * misreporting (to be removed once EDD support is gone) and
917 * issues ATA_CMD_INIT_DEV_PARAMS for pre-ATA4 drives.
918 *
919 * LOCKING:
920 * Kernel thread context (may sleep)
921 *
922 * RETURNS:
923 * 0 on success, -errno otherwise.
924 */
925static int ata_dev_read_id(struct ata_port *ap, struct ata_device *dev,
926 unsigned int *p_class, int post_reset, u16 *id)
927{
928 unsigned int class = *p_class;
929 unsigned int using_edd;
930 struct ata_taskfile tf;
931 unsigned int err_mask = 0;
932 const char *reason;
933 int rc;
934
935 DPRINTK("ENTER, host %u, dev %u\n", ap->id, dev->devno);
936
937 if (ap->ops->probe_reset ||
938 ap->flags & (ATA_FLAG_SRST | ATA_FLAG_SATA_RESET))
939 using_edd = 0;
940 else
941 using_edd = 1;
942
943 ata_dev_select(ap, dev->devno, 1, 1); /* select device 0/1 */
944
945 retry:
946 ata_tf_init(ap, &tf, dev->devno);
947
948 switch (class) {
949 case ATA_DEV_ATA:
950 tf.command = ATA_CMD_ID_ATA;
951 break;
952 case ATA_DEV_ATAPI:
953 tf.command = ATA_CMD_ID_ATAPI;
954 break;
955 default:
956 rc = -ENODEV;
957 reason = "unsupported class";
958 goto err_out;
959 }
960
961 tf.protocol = ATA_PROT_PIO;
962
963 err_mask = ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE,
964 id, sizeof(id[0]) * ATA_ID_WORDS);
965
966 if (err_mask) {
967 rc = -EIO;
968 reason = "I/O error";
969
970 if (err_mask & ~AC_ERR_DEV)
971 goto err_out;
972
973 /*
974 * arg! EDD works for all test cases, but seems to return
975 * the ATA signature for some ATAPI devices. Until the
976 * reason for this is found and fixed, we fix up the mess
977 * here. If IDENTIFY DEVICE returns command aborted
978 * (as ATAPI devices do), then we issue an
979 * IDENTIFY PACKET DEVICE.
980 *
981 * ATA software reset (SRST, the default) does not appear
982 * to have this problem.
983 */
984 if ((using_edd) && (class == ATA_DEV_ATA)) {
985 u8 err = tf.feature;
986 if (err & ATA_ABORTED) {
987 class = ATA_DEV_ATAPI;
988 goto retry;
989 }
990 }
991 goto err_out;
992 }
993
994 swap_buf_le16(id, ATA_ID_WORDS);
995
996 /* print device capabilities */
997 printk(KERN_DEBUG "ata%u: dev %u cfg "
998 "49:%04x 82:%04x 83:%04x 84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
999 ap->id, dev->devno,
1000 id[49], id[82], id[83], id[84], id[85], id[86], id[87], id[88]);
1001
1002 /* sanity check */
1003 if ((class == ATA_DEV_ATA) != ata_id_is_ata(id)) {
1004 rc = -EINVAL;
1005 reason = "device reports illegal type";
1006 goto err_out;
1007 }
1008
1009 if (post_reset && class == ATA_DEV_ATA) {
1010 /*
1011 * The exact sequence expected by certain pre-ATA4 drives is:
1012 * SRST RESET
1013 * IDENTIFY
1014 * INITIALIZE DEVICE PARAMETERS
1015 * anything else..
1016 * Some drives were very specific about that exact sequence.
1017 */
1018 if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
1019 err_mask = ata_dev_init_params(ap, dev);
1020 if (err_mask) {
1021 rc = -EIO;
1022 reason = "INIT_DEV_PARAMS failed";
1023 goto err_out;
1024 }
1025
1026 /* current CHS translation info (id[53-58]) might be
1027 * changed. reread the identify device info.
1028 */
1029 post_reset = 0;
1030 goto retry;
1031 }
1032 }
1033
1034 *p_class = class;
1035 return 0;
1036
1037 err_out:
1038 printk(KERN_WARNING "ata%u: dev %u failed to IDENTIFY (%s)\n",
1039 ap->id, dev->devno, reason);
1040 kfree(id);
1041 return rc;
1042}
1043
1044/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 * ata_dev_identify - obtain IDENTIFY x DEVICE page
1046 * @ap: port on which device we wish to probe resides
1047 * @device: device bus address, starting at zero
1048 *
1049 * Following bus reset, we issue the IDENTIFY [PACKET] DEVICE
1050 * command, and read back the 512-byte device information page.
1051 * The device information page is fed to us via the standard
1052 * PIO-IN protocol, but we hand-code it here. (TODO: investigate
1053 * using standard PIO-IN paths)
1054 *
1055 * After reading the device information page, we use several
1056 * bits of information from it to initialize data structures
1057 * that will be used during the lifetime of the ata_device.
1058 * Other data from the info page is used to disqualify certain
1059 * older ATA devices we do not wish to support.
1060 *
1061 * LOCKING:
1062 * Inherited from caller. Some functions called by this function
1063 * obtain the host_set lock.
1064 */
1065
1066static void ata_dev_identify(struct ata_port *ap, unsigned int device)
1067{
1068 struct ata_device *dev = &ap->device[device];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 unsigned long xfer_modes;
Tejun Heo6e7846e2006-02-12 23:32:58 +09001070 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
1072 if (!ata_dev_present(dev)) {
1073 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
1074 ap->id, device);
1075 return;
1076 }
1077
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 DPRINTK("ENTER, host %u, dev %u\n", ap->id, device);
1079
Tejun Heo49016ac2006-02-21 02:12:11 +09001080 rc = ata_dev_read_id(ap, dev, &dev->class, 1, dev->id);
1081 if (rc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
1084 /*
1085 * common ATA, ATAPI feature tests
1086 */
1087
Albert Lee8bf62ec2005-05-12 15:29:42 -04001088 /* we require DMA support (bits 8 of word 49) */
1089 if (!ata_id_has_dma(dev->id)) {
1090 printk(KERN_DEBUG "ata%u: no dma\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 goto err_out_nosup;
1092 }
1093
1094 /* quick-n-dirty find max transfer mode; for printk only */
1095 xfer_modes = dev->id[ATA_ID_UDMA_MODES];
1096 if (!xfer_modes)
1097 xfer_modes = (dev->id[ATA_ID_MWDMA_MODES]) << ATA_SHIFT_MWDMA;
Alan Cox11e29e22005-10-21 18:46:32 -04001098 if (!xfer_modes)
1099 xfer_modes = ata_pio_modes(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Tejun Heo0bd33002006-02-12 22:47:05 +09001101 ata_dump_id(dev->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
1103 /* ATA-specific feature tests */
1104 if (dev->class == ATA_DEV_ATA) {
Tejun Heo29407402006-02-12 22:47:04 +09001105 dev->n_sectors = ata_id_n_sectors(dev->id);
1106
Albert Lee8bf62ec2005-05-12 15:29:42 -04001107 if (ata_id_has_lba(dev->id)) {
1108 dev->flags |= ATA_DFLAG_LBA;
1109
Tejun Heo29407402006-02-12 22:47:04 +09001110 if (ata_id_has_lba48(dev->id))
Albert Lee8bf62ec2005-05-12 15:29:42 -04001111 dev->flags |= ATA_DFLAG_LBA48;
Albert Lee8bf62ec2005-05-12 15:29:42 -04001112
1113 /* print device info to dmesg */
1114 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors:%s\n",
1115 ap->id, device,
Tejun Heo49016ac2006-02-21 02:12:11 +09001116 ata_id_major_version(dev->id),
Albert Lee8bf62ec2005-05-12 15:29:42 -04001117 ata_mode_string(xfer_modes),
1118 (unsigned long long)dev->n_sectors,
1119 dev->flags & ATA_DFLAG_LBA48 ? " LBA48" : " LBA");
1120 } else {
1121 /* CHS */
1122
1123 /* Default translation */
1124 dev->cylinders = dev->id[1];
1125 dev->heads = dev->id[3];
1126 dev->sectors = dev->id[6];
Albert Lee8bf62ec2005-05-12 15:29:42 -04001127
1128 if (ata_id_current_chs_valid(dev->id)) {
1129 /* Current CHS translation is valid. */
1130 dev->cylinders = dev->id[54];
1131 dev->heads = dev->id[55];
1132 dev->sectors = dev->id[56];
Albert Lee8bf62ec2005-05-12 15:29:42 -04001133 }
1134
1135 /* print device info to dmesg */
1136 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors: CHS %d/%d/%d\n",
1137 ap->id, device,
Tejun Heo49016ac2006-02-21 02:12:11 +09001138 ata_id_major_version(dev->id),
Albert Lee8bf62ec2005-05-12 15:29:42 -04001139 ata_mode_string(xfer_modes),
1140 (unsigned long long)dev->n_sectors,
1141 (int)dev->cylinders, (int)dev->heads, (int)dev->sectors);
1142
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 }
1144
Tejun Heo6e7846e2006-02-12 23:32:58 +09001145 dev->cdb_len = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 }
1147
1148 /* ATAPI-specific feature tests */
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05001149 else if (dev->class == ATA_DEV_ATAPI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 rc = atapi_cdb_len(dev->id);
1151 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
1152 printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
1153 goto err_out_nosup;
1154 }
Tejun Heo6e7846e2006-02-12 23:32:58 +09001155 dev->cdb_len = (unsigned int) rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
1157 /* print device info to dmesg */
1158 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
1159 ap->id, device,
1160 ata_mode_string(xfer_modes));
1161 }
1162
Tejun Heo6e7846e2006-02-12 23:32:58 +09001163 ap->host->max_cmd_len = 0;
1164 for (i = 0; i < ATA_MAX_DEVICES; i++)
1165 ap->host->max_cmd_len = max_t(unsigned int,
1166 ap->host->max_cmd_len,
1167 ap->device[i].cdb_len);
1168
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
1170 return;
1171
1172err_out_nosup:
1173 printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n",
1174 ap->id, device);
1175err_out:
1176 dev->class++; /* converts ATA_DEV_xxx into ATA_DEV_xxx_UNSUP */
1177 DPRINTK("EXIT, err\n");
1178}
1179
Brad Campbell6f2f3812005-05-12 15:07:47 -04001180
Tejun Heo8eabd022006-02-12 23:32:58 +09001181static inline u8 ata_dev_knobble(const struct ata_port *ap,
1182 struct ata_device *dev)
Brad Campbell6f2f3812005-05-12 15:07:47 -04001183{
Tejun Heo8eabd022006-02-12 23:32:58 +09001184 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
Brad Campbell6f2f3812005-05-12 15:07:47 -04001185}
1186
1187/**
Randy Dunlapc893a3a2006-01-28 13:15:32 -05001188 * ata_dev_config - Run device specific handlers & check for SATA->PATA bridges
1189 * @ap: Bus
1190 * @i: Device
Brad Campbell6f2f3812005-05-12 15:07:47 -04001191 *
Randy Dunlapc893a3a2006-01-28 13:15:32 -05001192 * LOCKING:
Brad Campbell6f2f3812005-05-12 15:07:47 -04001193 */
Jeff Garzik8a60a072005-07-31 13:13:24 -04001194
Brad Campbell6f2f3812005-05-12 15:07:47 -04001195void ata_dev_config(struct ata_port *ap, unsigned int i)
1196{
1197 /* limit bridge transfers to udma5, 200 sectors */
Tejun Heo8eabd022006-02-12 23:32:58 +09001198 if (ata_dev_knobble(ap, &ap->device[i])) {
Brad Campbell6f2f3812005-05-12 15:07:47 -04001199 printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
Tejun Heo8eabd022006-02-12 23:32:58 +09001200 ap->id, i);
Brad Campbell6f2f3812005-05-12 15:07:47 -04001201 ap->udma_mask &= ATA_UDMA5;
Tejun Heob00eec12006-02-12 23:32:59 +09001202 ap->device[i].max_sectors = ATA_MAX_SECTORS;
Brad Campbell6f2f3812005-05-12 15:07:47 -04001203 }
1204
1205 if (ap->ops->dev_config)
1206 ap->ops->dev_config(ap, &ap->device[i]);
1207}
1208
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209/**
1210 * ata_bus_probe - Reset and probe ATA bus
1211 * @ap: Bus to probe
1212 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001213 * Master ATA bus probing function. Initiates a hardware-dependent
1214 * bus reset, then attempts to identify any devices found on
1215 * the bus.
1216 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001218 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 *
1220 * RETURNS:
1221 * Zero on success, non-zero on error.
1222 */
1223
1224static int ata_bus_probe(struct ata_port *ap)
1225{
1226 unsigned int i, found = 0;
1227
Tejun Heoc19ba8a2006-01-24 17:05:22 +09001228 if (ap->ops->probe_reset) {
1229 unsigned int classes[ATA_MAX_DEVICES];
1230 int rc;
1231
1232 ata_port_probe(ap);
1233
1234 rc = ap->ops->probe_reset(ap, classes);
1235 if (rc == 0) {
Tejun Heo06ab7822006-02-12 15:01:49 +09001236 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1237 if (classes[i] == ATA_DEV_UNKNOWN)
1238 classes[i] = ATA_DEV_NONE;
Tejun Heoc19ba8a2006-01-24 17:05:22 +09001239 ap->device[i].class = classes[i];
Tejun Heo06ab7822006-02-12 15:01:49 +09001240 }
Tejun Heoc19ba8a2006-01-24 17:05:22 +09001241 } else {
1242 printk(KERN_ERR "ata%u: probe reset failed, "
1243 "disabling port\n", ap->id);
1244 ata_port_disable(ap);
1245 }
1246 } else
1247 ap->ops->phy_reset(ap);
1248
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1250 goto err_out;
1251
1252 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1253 ata_dev_identify(ap, i);
1254 if (ata_dev_present(&ap->device[i])) {
1255 found = 1;
Brad Campbell6f2f3812005-05-12 15:07:47 -04001256 ata_dev_config(ap,i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 }
1258 }
1259
1260 if ((!found) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1261 goto err_out_disable;
1262
1263 ata_set_mode(ap);
1264 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1265 goto err_out_disable;
1266
1267 return 0;
1268
1269err_out_disable:
1270 ap->ops->port_disable(ap);
1271err_out:
1272 return -1;
1273}
1274
1275/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001276 * ata_port_probe - Mark port as enabled
1277 * @ap: Port for which we indicate enablement
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001279 * Modify @ap data structure such that the system
1280 * thinks that the entire port is enabled.
1281 *
1282 * LOCKING: host_set lock, or some other form of
1283 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 */
1285
1286void ata_port_probe(struct ata_port *ap)
1287{
1288 ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1289}
1290
1291/**
Tejun Heo3be680b2005-12-19 22:35:02 +09001292 * sata_print_link_status - Print SATA link status
1293 * @ap: SATA port to printk link status about
1294 *
1295 * This function prints link speed and status of a SATA link.
1296 *
1297 * LOCKING:
1298 * None.
1299 */
1300static void sata_print_link_status(struct ata_port *ap)
1301{
1302 u32 sstatus, tmp;
1303 const char *speed;
1304
1305 if (!ap->ops->scr_read)
1306 return;
1307
1308 sstatus = scr_read(ap, SCR_STATUS);
1309
1310 if (sata_dev_present(ap)) {
1311 tmp = (sstatus >> 4) & 0xf;
1312 if (tmp & (1 << 0))
1313 speed = "1.5";
1314 else if (tmp & (1 << 1))
1315 speed = "3.0";
1316 else
1317 speed = "<unknown>";
1318 printk(KERN_INFO "ata%u: SATA link up %s Gbps (SStatus %X)\n",
1319 ap->id, speed, sstatus);
1320 } else {
1321 printk(KERN_INFO "ata%u: SATA link down (SStatus %X)\n",
1322 ap->id, sstatus);
1323 }
1324}
1325
1326/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001327 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1328 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001330 * This function issues commands to standard SATA Sxxx
1331 * PHY registers, to wake up the phy (and device), and
1332 * clear any reset condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333 *
1334 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001335 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 *
1337 */
1338void __sata_phy_reset(struct ata_port *ap)
1339{
1340 u32 sstatus;
1341 unsigned long timeout = jiffies + (HZ * 5);
1342
1343 if (ap->flags & ATA_FLAG_SATA_RESET) {
Brett Russcdcca892005-03-28 15:10:27 -05001344 /* issue phy wake/reset */
1345 scr_write_flush(ap, SCR_CONTROL, 0x301);
Tejun Heo62ba2842005-06-26 23:27:19 +09001346 /* Couldn't find anything in SATA I/II specs, but
1347 * AHCI-1.1 10.4.2 says at least 1 ms. */
1348 mdelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 }
Brett Russcdcca892005-03-28 15:10:27 -05001350 scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
1352 /* wait for phy to become ready, if necessary */
1353 do {
1354 msleep(200);
1355 sstatus = scr_read(ap, SCR_STATUS);
1356 if ((sstatus & 0xf) != 1)
1357 break;
1358 } while (time_before(jiffies, timeout));
1359
Tejun Heo3be680b2005-12-19 22:35:02 +09001360 /* print link status */
1361 sata_print_link_status(ap);
Jeff Garzik656563e2005-11-20 03:36:45 -05001362
Tejun Heo3be680b2005-12-19 22:35:02 +09001363 /* TODO: phy layer with polling, timeouts, etc. */
1364 if (sata_dev_present(ap))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 ata_port_probe(ap);
Tejun Heo3be680b2005-12-19 22:35:02 +09001366 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 ata_port_disable(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
1369 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1370 return;
1371
1372 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1373 ata_port_disable(ap);
1374 return;
1375 }
1376
1377 ap->cbl = ATA_CBL_SATA;
1378}
1379
1380/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001381 * sata_phy_reset - Reset SATA bus.
1382 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001384 * This function resets the SATA bus, and then probes
1385 * the bus for devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386 *
1387 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001388 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 *
1390 */
1391void sata_phy_reset(struct ata_port *ap)
1392{
1393 __sata_phy_reset(ap);
1394 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1395 return;
1396 ata_bus_reset(ap);
1397}
1398
1399/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001400 * ata_port_disable - Disable port.
1401 * @ap: Port to be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001403 * Modify @ap data structure such that the system
1404 * thinks that the entire port is disabled, and should
1405 * never attempt to probe or communicate with devices
1406 * on this port.
1407 *
1408 * LOCKING: host_set lock, or some other form of
1409 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 */
1411
1412void ata_port_disable(struct ata_port *ap)
1413{
1414 ap->device[0].class = ATA_DEV_NONE;
1415 ap->device[1].class = ATA_DEV_NONE;
1416 ap->flags |= ATA_FLAG_PORT_DISABLED;
1417}
1418
Alan Cox452503f2005-10-21 19:01:32 -04001419/*
1420 * This mode timing computation functionality is ported over from
1421 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
1422 */
1423/*
1424 * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
1425 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
1426 * for PIO 5, which is a nonstandard extension and UDMA6, which
1427 * is currently supported only by Maxtor drives.
1428 */
1429
1430static const struct ata_timing ata_timing[] = {
1431
1432 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
1433 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
1434 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
1435 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
1436
1437 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
1438 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
1439 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
1440
1441/* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
1442
1443 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
1444 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
1445 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
1446
1447 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
1448 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
1449 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
1450
1451/* { XFER_PIO_5, 20, 50, 30, 100, 50, 30, 100, 0 }, */
1452 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
1453 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
1454
1455 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
1456 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
1457 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
1458
1459/* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
1460
1461 { 0xFF }
1462};
1463
1464#define ENOUGH(v,unit) (((v)-1)/(unit)+1)
1465#define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
1466
1467static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
1468{
1469 q->setup = EZ(t->setup * 1000, T);
1470 q->act8b = EZ(t->act8b * 1000, T);
1471 q->rec8b = EZ(t->rec8b * 1000, T);
1472 q->cyc8b = EZ(t->cyc8b * 1000, T);
1473 q->active = EZ(t->active * 1000, T);
1474 q->recover = EZ(t->recover * 1000, T);
1475 q->cycle = EZ(t->cycle * 1000, T);
1476 q->udma = EZ(t->udma * 1000, UT);
1477}
1478
1479void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
1480 struct ata_timing *m, unsigned int what)
1481{
1482 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
1483 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
1484 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
1485 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
1486 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
1487 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
1488 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
1489 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
1490}
1491
1492static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
1493{
1494 const struct ata_timing *t;
1495
1496 for (t = ata_timing; t->mode != speed; t++)
Alan Cox91190752005-10-26 12:17:46 -04001497 if (t->mode == 0xFF)
Alan Cox452503f2005-10-21 19:01:32 -04001498 return NULL;
1499 return t;
1500}
1501
1502int ata_timing_compute(struct ata_device *adev, unsigned short speed,
1503 struct ata_timing *t, int T, int UT)
1504{
1505 const struct ata_timing *s;
1506 struct ata_timing p;
1507
1508 /*
1509 * Find the mode.
Albert Lee75b1f2f2005-11-16 17:06:18 +08001510 */
Alan Cox452503f2005-10-21 19:01:32 -04001511
1512 if (!(s = ata_timing_find_mode(speed)))
1513 return -EINVAL;
1514
Albert Lee75b1f2f2005-11-16 17:06:18 +08001515 memcpy(t, s, sizeof(*s));
1516
Alan Cox452503f2005-10-21 19:01:32 -04001517 /*
1518 * If the drive is an EIDE drive, it can tell us it needs extended
1519 * PIO/MW_DMA cycle timing.
1520 */
1521
1522 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
1523 memset(&p, 0, sizeof(p));
1524 if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
1525 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
1526 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
1527 } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
1528 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
1529 }
1530 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
1531 }
1532
1533 /*
1534 * Convert the timing to bus clock counts.
1535 */
1536
Albert Lee75b1f2f2005-11-16 17:06:18 +08001537 ata_timing_quantize(t, t, T, UT);
Alan Cox452503f2005-10-21 19:01:32 -04001538
1539 /*
Randy Dunlapc893a3a2006-01-28 13:15:32 -05001540 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
1541 * S.M.A.R.T * and some other commands. We have to ensure that the
1542 * DMA cycle timing is slower/equal than the fastest PIO timing.
Alan Cox452503f2005-10-21 19:01:32 -04001543 */
1544
1545 if (speed > XFER_PIO_4) {
1546 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
1547 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
1548 }
1549
1550 /*
Randy Dunlapc893a3a2006-01-28 13:15:32 -05001551 * Lengthen active & recovery time so that cycle time is correct.
Alan Cox452503f2005-10-21 19:01:32 -04001552 */
1553
1554 if (t->act8b + t->rec8b < t->cyc8b) {
1555 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
1556 t->rec8b = t->cyc8b - t->act8b;
1557 }
1558
1559 if (t->active + t->recover < t->cycle) {
1560 t->active += (t->cycle - (t->active + t->recover)) / 2;
1561 t->recover = t->cycle - t->active;
1562 }
1563
1564 return 0;
1565}
1566
Jeff Garzik057ace52005-10-22 14:27:05 -04001567static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 unsigned int shift;
1569 u8 base;
1570} xfer_mode_classes[] = {
1571 { ATA_SHIFT_UDMA, XFER_UDMA_0 },
1572 { ATA_SHIFT_MWDMA, XFER_MW_DMA_0 },
1573 { ATA_SHIFT_PIO, XFER_PIO_0 },
1574};
1575
Arjan van de Ven858119e2006-01-14 13:20:43 -08001576static u8 base_from_shift(unsigned int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577{
1578 int i;
1579
1580 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++)
1581 if (xfer_mode_classes[i].shift == shift)
1582 return xfer_mode_classes[i].base;
1583
1584 return 0xff;
1585}
1586
1587static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
1588{
1589 int ofs, idx;
1590 u8 base;
1591
1592 if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1593 return;
1594
1595 if (dev->xfer_shift == ATA_SHIFT_PIO)
1596 dev->flags |= ATA_DFLAG_PIO;
1597
1598 ata_dev_set_xfermode(ap, dev);
1599
1600 base = base_from_shift(dev->xfer_shift);
1601 ofs = dev->xfer_mode - base;
1602 idx = ofs + dev->xfer_shift;
1603 WARN_ON(idx >= ARRAY_SIZE(xfer_mode_str));
1604
1605 DPRINTK("idx=%d xfer_shift=%u, xfer_mode=0x%x, base=0x%x, offset=%d\n",
1606 idx, dev->xfer_shift, (int)dev->xfer_mode, (int)base, ofs);
1607
1608 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
1609 ap->id, dev->devno, xfer_mode_str[idx]);
1610}
1611
1612static int ata_host_set_pio(struct ata_port *ap)
1613{
1614 unsigned int mask;
1615 int x, i;
1616 u8 base, xfer_mode;
1617
1618 mask = ata_get_mode_mask(ap, ATA_SHIFT_PIO);
1619 x = fgb(mask);
1620 if (x < 0) {
1621 printk(KERN_WARNING "ata%u: no PIO support\n", ap->id);
1622 return -1;
1623 }
1624
1625 base = base_from_shift(ATA_SHIFT_PIO);
1626 xfer_mode = base + x;
1627
1628 DPRINTK("base 0x%x xfer_mode 0x%x mask 0x%x x %d\n",
1629 (int)base, (int)xfer_mode, mask, x);
1630
1631 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1632 struct ata_device *dev = &ap->device[i];
1633 if (ata_dev_present(dev)) {
1634 dev->pio_mode = xfer_mode;
1635 dev->xfer_mode = xfer_mode;
1636 dev->xfer_shift = ATA_SHIFT_PIO;
1637 if (ap->ops->set_piomode)
1638 ap->ops->set_piomode(ap, dev);
1639 }
1640 }
1641
1642 return 0;
1643}
1644
1645static void ata_host_set_dma(struct ata_port *ap, u8 xfer_mode,
1646 unsigned int xfer_shift)
1647{
1648 int i;
1649
1650 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1651 struct ata_device *dev = &ap->device[i];
1652 if (ata_dev_present(dev)) {
1653 dev->dma_mode = xfer_mode;
1654 dev->xfer_mode = xfer_mode;
1655 dev->xfer_shift = xfer_shift;
1656 if (ap->ops->set_dmamode)
1657 ap->ops->set_dmamode(ap, dev);
1658 }
1659 }
1660}
1661
1662/**
1663 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1664 * @ap: port on which timings will be programmed
1665 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001666 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1667 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001669 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 */
1671static void ata_set_mode(struct ata_port *ap)
1672{
Albert Lee8cbd6df2005-10-12 15:06:27 +08001673 unsigned int xfer_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 u8 xfer_mode;
1675 int rc;
1676
1677 /* step 1: always set host PIO timings */
1678 rc = ata_host_set_pio(ap);
1679 if (rc)
1680 goto err_out;
1681
1682 /* step 2: choose the best data xfer mode */
1683 xfer_mode = xfer_shift = 0;
1684 rc = ata_choose_xfer_mode(ap, &xfer_mode, &xfer_shift);
1685 if (rc)
1686 goto err_out;
1687
1688 /* step 3: if that xfer mode isn't PIO, set host DMA timings */
1689 if (xfer_shift != ATA_SHIFT_PIO)
1690 ata_host_set_dma(ap, xfer_mode, xfer_shift);
1691
1692 /* step 4: update devices' xfer mode */
1693 ata_dev_set_mode(ap, &ap->device[0]);
1694 ata_dev_set_mode(ap, &ap->device[1]);
1695
1696 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1697 return;
1698
1699 if (ap->ops->post_set_mode)
1700 ap->ops->post_set_mode(ap);
1701
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 return;
1703
1704err_out:
1705 ata_port_disable(ap);
1706}
1707
1708/**
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05001709 * ata_tf_to_host - issue ATA taskfile to host controller
1710 * @ap: port to which command is being issued
1711 * @tf: ATA taskfile register set
1712 *
1713 * Issues ATA taskfile register set to ATA host controller,
1714 * with proper synchronization with interrupt handler and
1715 * other threads.
1716 *
1717 * LOCKING:
1718 * spin_lock_irqsave(host_set lock)
1719 */
1720
1721static inline void ata_tf_to_host(struct ata_port *ap,
1722 const struct ata_taskfile *tf)
1723{
1724 ap->ops->tf_load(ap, tf);
1725 ap->ops->exec_command(ap, tf);
1726}
1727
1728/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 * ata_busy_sleep - sleep until BSY clears, or timeout
1730 * @ap: port containing status register to be polled
1731 * @tmout_pat: impatience timeout
1732 * @tmout: overall timeout
1733 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001734 * Sleep until ATA Status register bit BSY clears,
1735 * or a timeout occurs.
1736 *
1737 * LOCKING: None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 */
1739
Tejun Heo6f8b9952006-01-24 17:05:21 +09001740unsigned int ata_busy_sleep (struct ata_port *ap,
1741 unsigned long tmout_pat, unsigned long tmout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742{
1743 unsigned long timer_start, timeout;
1744 u8 status;
1745
1746 status = ata_busy_wait(ap, ATA_BUSY, 300);
1747 timer_start = jiffies;
1748 timeout = timer_start + tmout_pat;
1749 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1750 msleep(50);
1751 status = ata_busy_wait(ap, ATA_BUSY, 3);
1752 }
1753
1754 if (status & ATA_BUSY)
1755 printk(KERN_WARNING "ata%u is slow to respond, "
1756 "please be patient\n", ap->id);
1757
1758 timeout = timer_start + tmout;
1759 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1760 msleep(50);
1761 status = ata_chk_status(ap);
1762 }
1763
1764 if (status & ATA_BUSY) {
1765 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
1766 ap->id, tmout / HZ);
1767 return 1;
1768 }
1769
1770 return 0;
1771}
1772
1773static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
1774{
1775 struct ata_ioports *ioaddr = &ap->ioaddr;
1776 unsigned int dev0 = devmask & (1 << 0);
1777 unsigned int dev1 = devmask & (1 << 1);
1778 unsigned long timeout;
1779
1780 /* if device 0 was found in ata_devchk, wait for its
1781 * BSY bit to clear
1782 */
1783 if (dev0)
1784 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1785
1786 /* if device 1 was found in ata_devchk, wait for
1787 * register access, then wait for BSY to clear
1788 */
1789 timeout = jiffies + ATA_TMOUT_BOOT;
1790 while (dev1) {
1791 u8 nsect, lbal;
1792
1793 ap->ops->dev_select(ap, 1);
1794 if (ap->flags & ATA_FLAG_MMIO) {
1795 nsect = readb((void __iomem *) ioaddr->nsect_addr);
1796 lbal = readb((void __iomem *) ioaddr->lbal_addr);
1797 } else {
1798 nsect = inb(ioaddr->nsect_addr);
1799 lbal = inb(ioaddr->lbal_addr);
1800 }
1801 if ((nsect == 1) && (lbal == 1))
1802 break;
1803 if (time_after(jiffies, timeout)) {
1804 dev1 = 0;
1805 break;
1806 }
1807 msleep(50); /* give drive a breather */
1808 }
1809 if (dev1)
1810 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1811
1812 /* is all this really necessary? */
1813 ap->ops->dev_select(ap, 0);
1814 if (dev1)
1815 ap->ops->dev_select(ap, 1);
1816 if (dev0)
1817 ap->ops->dev_select(ap, 0);
1818}
1819
1820/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001821 * ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
1822 * @ap: Port to reset and probe
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001824 * Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
1825 * probe the bus. Not often used these days.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 *
1827 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001828 * PCI/etc. bus probe sem.
Jeff Garzike5338252005-10-30 21:37:17 -05001829 * Obtains host_set lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 *
1831 */
1832
1833static unsigned int ata_bus_edd(struct ata_port *ap)
1834{
1835 struct ata_taskfile tf;
Jeff Garzike5338252005-10-30 21:37:17 -05001836 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837
1838 /* set up execute-device-diag (bus reset) taskfile */
1839 /* also, take interrupts to a known state (disabled) */
1840 DPRINTK("execute-device-diag\n");
1841 ata_tf_init(ap, &tf, 0);
1842 tf.ctl |= ATA_NIEN;
1843 tf.command = ATA_CMD_EDD;
1844 tf.protocol = ATA_PROT_NODATA;
1845
1846 /* do bus reset */
Jeff Garzike5338252005-10-30 21:37:17 -05001847 spin_lock_irqsave(&ap->host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 ata_tf_to_host(ap, &tf);
Jeff Garzike5338252005-10-30 21:37:17 -05001849 spin_unlock_irqrestore(&ap->host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850
1851 /* spec says at least 2ms. but who knows with those
1852 * crazy ATAPI devices...
1853 */
1854 msleep(150);
1855
1856 return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1857}
1858
1859static unsigned int ata_bus_softreset(struct ata_port *ap,
1860 unsigned int devmask)
1861{
1862 struct ata_ioports *ioaddr = &ap->ioaddr;
1863
1864 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
1865
1866 /* software reset. causes dev0 to be selected */
1867 if (ap->flags & ATA_FLAG_MMIO) {
1868 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1869 udelay(20); /* FIXME: flush */
1870 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
1871 udelay(20); /* FIXME: flush */
1872 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1873 } else {
1874 outb(ap->ctl, ioaddr->ctl_addr);
1875 udelay(10);
1876 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
1877 udelay(10);
1878 outb(ap->ctl, ioaddr->ctl_addr);
1879 }
1880
1881 /* spec mandates ">= 2ms" before checking status.
1882 * We wait 150ms, because that was the magic delay used for
1883 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
1884 * between when the ATA command register is written, and then
1885 * status is checked. Because waiting for "a while" before
1886 * checking status is fine, post SRST, we perform this magic
1887 * delay here as well.
1888 */
1889 msleep(150);
1890
1891 ata_bus_post_reset(ap, devmask);
1892
1893 return 0;
1894}
1895
1896/**
1897 * ata_bus_reset - reset host port and associated ATA channel
1898 * @ap: port to reset
1899 *
1900 * This is typically the first time we actually start issuing
1901 * commands to the ATA channel. We wait for BSY to clear, then
1902 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
1903 * result. Determine what devices, if any, are on the channel
1904 * by looking at the device 0/1 error register. Look at the signature
1905 * stored in each device's taskfile registers, to determine if
1906 * the device is ATA or ATAPI.
1907 *
1908 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001909 * PCI/etc. bus probe sem.
1910 * Obtains host_set lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 *
1912 * SIDE EFFECTS:
1913 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
1914 */
1915
1916void ata_bus_reset(struct ata_port *ap)
1917{
1918 struct ata_ioports *ioaddr = &ap->ioaddr;
1919 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
1920 u8 err;
1921 unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
1922
1923 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
1924
1925 /* determine if device 0/1 are present */
1926 if (ap->flags & ATA_FLAG_SATA_RESET)
1927 dev0 = 1;
1928 else {
1929 dev0 = ata_devchk(ap, 0);
1930 if (slave_possible)
1931 dev1 = ata_devchk(ap, 1);
1932 }
1933
1934 if (dev0)
1935 devmask |= (1 << 0);
1936 if (dev1)
1937 devmask |= (1 << 1);
1938
1939 /* select device 0 again */
1940 ap->ops->dev_select(ap, 0);
1941
1942 /* issue bus reset */
1943 if (ap->flags & ATA_FLAG_SRST)
1944 rc = ata_bus_softreset(ap, devmask);
1945 else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
1946 /* set up device control */
1947 if (ap->flags & ATA_FLAG_MMIO)
1948 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1949 else
1950 outb(ap->ctl, ioaddr->ctl_addr);
1951 rc = ata_bus_edd(ap);
1952 }
1953
1954 if (rc)
1955 goto err_out;
1956
1957 /*
1958 * determine by signature whether we have ATA or ATAPI devices
1959 */
Tejun Heob4dc7622006-01-24 17:05:22 +09001960 ap->device[0].class = ata_dev_try_classify(ap, 0, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 if ((slave_possible) && (err != 0x81))
Tejun Heob4dc7622006-01-24 17:05:22 +09001962 ap->device[1].class = ata_dev_try_classify(ap, 1, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963
1964 /* re-enable interrupts */
1965 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
1966 ata_irq_on(ap);
1967
1968 /* is double-select really necessary? */
1969 if (ap->device[1].class != ATA_DEV_NONE)
1970 ap->ops->dev_select(ap, 1);
1971 if (ap->device[0].class != ATA_DEV_NONE)
1972 ap->ops->dev_select(ap, 0);
1973
1974 /* if no devices were detected, disable this port */
1975 if ((ap->device[0].class == ATA_DEV_NONE) &&
1976 (ap->device[1].class == ATA_DEV_NONE))
1977 goto err_out;
1978
1979 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
1980 /* set up device control for ATA_FLAG_SATA_RESET */
1981 if (ap->flags & ATA_FLAG_MMIO)
1982 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1983 else
1984 outb(ap->ctl, ioaddr->ctl_addr);
1985 }
1986
1987 DPRINTK("EXIT\n");
1988 return;
1989
1990err_out:
1991 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
1992 ap->ops->port_disable(ap);
1993
1994 DPRINTK("EXIT\n");
1995}
1996
Tejun Heo7a7921e2006-02-02 18:20:00 +09001997static int sata_phy_resume(struct ata_port *ap)
1998{
1999 unsigned long timeout = jiffies + (HZ * 5);
2000 u32 sstatus;
2001
2002 scr_write_flush(ap, SCR_CONTROL, 0x300);
2003
2004 /* Wait for phy to become ready, if necessary. */
2005 do {
2006 msleep(200);
2007 sstatus = scr_read(ap, SCR_STATUS);
2008 if ((sstatus & 0xf) != 1)
2009 return 0;
2010 } while (time_before(jiffies, timeout));
2011
2012 return -1;
2013}
2014
Tejun Heoc2bd5802006-01-24 17:05:22 +09002015/**
Tejun Heo8a19ac82006-02-02 18:20:00 +09002016 * ata_std_probeinit - initialize probing
2017 * @ap: port to be probed
2018 *
2019 * @ap is about to be probed. Initialize it. This function is
2020 * to be used as standard callback for ata_drive_probe_reset().
Tejun Heo3a397462006-02-10 23:58:48 +09002021 *
2022 * NOTE!!! Do not use this function as probeinit if a low level
2023 * driver implements only hardreset. Just pass NULL as probeinit
2024 * in that case. Using this function is probably okay but doing
2025 * so makes reset sequence different from the original
2026 * ->phy_reset implementation and Jeff nervous. :-P
Tejun Heo8a19ac82006-02-02 18:20:00 +09002027 */
2028extern void ata_std_probeinit(struct ata_port *ap)
2029{
Tejun Heo3a397462006-02-10 23:58:48 +09002030 if (ap->flags & ATA_FLAG_SATA && ap->ops->scr_read) {
Tejun Heo8a19ac82006-02-02 18:20:00 +09002031 sata_phy_resume(ap);
Tejun Heo3a397462006-02-10 23:58:48 +09002032 if (sata_dev_present(ap))
2033 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2034 }
Tejun Heo8a19ac82006-02-02 18:20:00 +09002035}
2036
2037/**
Tejun Heoc2bd5802006-01-24 17:05:22 +09002038 * ata_std_softreset - reset host port via ATA SRST
2039 * @ap: port to reset
2040 * @verbose: fail verbosely
2041 * @classes: resulting classes of attached devices
2042 *
2043 * Reset host port using ATA SRST. This function is to be used
2044 * as standard callback for ata_drive_*_reset() functions.
2045 *
2046 * LOCKING:
2047 * Kernel thread context (may sleep)
2048 *
2049 * RETURNS:
2050 * 0 on success, -errno otherwise.
2051 */
2052int ata_std_softreset(struct ata_port *ap, int verbose, unsigned int *classes)
2053{
2054 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2055 unsigned int devmask = 0, err_mask;
2056 u8 err;
2057
2058 DPRINTK("ENTER\n");
2059
Tejun Heo3a397462006-02-10 23:58:48 +09002060 if (ap->ops->scr_read && !sata_dev_present(ap)) {
2061 classes[0] = ATA_DEV_NONE;
2062 goto out;
2063 }
2064
Tejun Heoc2bd5802006-01-24 17:05:22 +09002065 /* determine if device 0/1 are present */
2066 if (ata_devchk(ap, 0))
2067 devmask |= (1 << 0);
2068 if (slave_possible && ata_devchk(ap, 1))
2069 devmask |= (1 << 1);
2070
Tejun Heoc2bd5802006-01-24 17:05:22 +09002071 /* select device 0 again */
2072 ap->ops->dev_select(ap, 0);
2073
2074 /* issue bus reset */
2075 DPRINTK("about to softreset, devmask=%x\n", devmask);
2076 err_mask = ata_bus_softreset(ap, devmask);
2077 if (err_mask) {
2078 if (verbose)
2079 printk(KERN_ERR "ata%u: SRST failed (err_mask=0x%x)\n",
2080 ap->id, err_mask);
2081 else
2082 DPRINTK("EXIT, softreset failed (err_mask=0x%x)\n",
2083 err_mask);
2084 return -EIO;
2085 }
2086
2087 /* determine by signature whether we have ATA or ATAPI devices */
2088 classes[0] = ata_dev_try_classify(ap, 0, &err);
2089 if (slave_possible && err != 0x81)
2090 classes[1] = ata_dev_try_classify(ap, 1, &err);
2091
Tejun Heo3a397462006-02-10 23:58:48 +09002092 out:
Tejun Heoc2bd5802006-01-24 17:05:22 +09002093 DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]);
2094 return 0;
2095}
2096
2097/**
2098 * sata_std_hardreset - reset host port via SATA phy reset
2099 * @ap: port to reset
2100 * @verbose: fail verbosely
2101 * @class: resulting class of attached device
2102 *
2103 * SATA phy-reset host port using DET bits of SControl register.
2104 * This function is to be used as standard callback for
2105 * ata_drive_*_reset().
2106 *
2107 * LOCKING:
2108 * Kernel thread context (may sleep)
2109 *
2110 * RETURNS:
2111 * 0 on success, -errno otherwise.
2112 */
2113int sata_std_hardreset(struct ata_port *ap, int verbose, unsigned int *class)
2114{
Tejun Heoc2bd5802006-01-24 17:05:22 +09002115 DPRINTK("ENTER\n");
2116
2117 /* Issue phy wake/reset */
2118 scr_write_flush(ap, SCR_CONTROL, 0x301);
2119
2120 /*
2121 * Couldn't find anything in SATA I/II specs, but AHCI-1.1
2122 * 10.4.2 says at least 1 ms.
2123 */
2124 msleep(1);
2125
Tejun Heo7a7921e2006-02-02 18:20:00 +09002126 /* Bring phy back */
2127 sata_phy_resume(ap);
Tejun Heoc2bd5802006-01-24 17:05:22 +09002128
Tejun Heoc2bd5802006-01-24 17:05:22 +09002129 /* TODO: phy layer with polling, timeouts, etc. */
2130 if (!sata_dev_present(ap)) {
2131 *class = ATA_DEV_NONE;
2132 DPRINTK("EXIT, link offline\n");
2133 return 0;
2134 }
2135
2136 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
2137 if (verbose)
2138 printk(KERN_ERR "ata%u: COMRESET failed "
2139 "(device not ready)\n", ap->id);
2140 else
2141 DPRINTK("EXIT, device not ready\n");
2142 return -EIO;
2143 }
2144
Tejun Heo3a397462006-02-10 23:58:48 +09002145 ap->ops->dev_select(ap, 0); /* probably unnecessary */
2146
Tejun Heoc2bd5802006-01-24 17:05:22 +09002147 *class = ata_dev_try_classify(ap, 0, NULL);
2148
2149 DPRINTK("EXIT, class=%u\n", *class);
2150 return 0;
2151}
2152
2153/**
2154 * ata_std_postreset - standard postreset callback
2155 * @ap: the target ata_port
2156 * @classes: classes of attached devices
2157 *
2158 * This function is invoked after a successful reset. Note that
2159 * the device might have been reset more than once using
2160 * different reset methods before postreset is invoked.
Tejun Heoc2bd5802006-01-24 17:05:22 +09002161 *
2162 * This function is to be used as standard callback for
2163 * ata_drive_*_reset().
2164 *
2165 * LOCKING:
2166 * Kernel thread context (may sleep)
2167 */
2168void ata_std_postreset(struct ata_port *ap, unsigned int *classes)
2169{
2170 DPRINTK("ENTER\n");
2171
Tejun Heo56497bd2006-02-15 15:01:42 +09002172 /* set cable type if it isn't already set */
Tejun Heoc2bd5802006-01-24 17:05:22 +09002173 if (ap->cbl == ATA_CBL_NONE && ap->flags & ATA_FLAG_SATA)
2174 ap->cbl = ATA_CBL_SATA;
2175
2176 /* print link status */
2177 if (ap->cbl == ATA_CBL_SATA)
2178 sata_print_link_status(ap);
2179
Tejun Heo3a397462006-02-10 23:58:48 +09002180 /* re-enable interrupts */
2181 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2182 ata_irq_on(ap);
Tejun Heoc2bd5802006-01-24 17:05:22 +09002183
2184 /* is double-select really necessary? */
2185 if (classes[0] != ATA_DEV_NONE)
2186 ap->ops->dev_select(ap, 1);
2187 if (classes[1] != ATA_DEV_NONE)
2188 ap->ops->dev_select(ap, 0);
2189
Tejun Heo3a397462006-02-10 23:58:48 +09002190 /* bail out if no device is present */
2191 if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
2192 DPRINTK("EXIT, no device\n");
2193 return;
2194 }
2195
2196 /* set up device control */
2197 if (ap->ioaddr.ctl_addr) {
2198 if (ap->flags & ATA_FLAG_MMIO)
2199 writeb(ap->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
2200 else
2201 outb(ap->ctl, ap->ioaddr.ctl_addr);
2202 }
Tejun Heoc2bd5802006-01-24 17:05:22 +09002203
2204 DPRINTK("EXIT\n");
2205}
2206
2207/**
2208 * ata_std_probe_reset - standard probe reset method
2209 * @ap: prot to perform probe-reset
2210 * @classes: resulting classes of attached devices
2211 *
2212 * The stock off-the-shelf ->probe_reset method.
2213 *
2214 * LOCKING:
2215 * Kernel thread context (may sleep)
2216 *
2217 * RETURNS:
2218 * 0 on success, -errno otherwise.
2219 */
2220int ata_std_probe_reset(struct ata_port *ap, unsigned int *classes)
2221{
2222 ata_reset_fn_t hardreset;
2223
2224 hardreset = NULL;
Tejun Heob911fc32006-02-02 18:20:00 +09002225 if (ap->flags & ATA_FLAG_SATA && ap->ops->scr_read)
Tejun Heoc2bd5802006-01-24 17:05:22 +09002226 hardreset = sata_std_hardreset;
2227
Tejun Heo8a19ac82006-02-02 18:20:00 +09002228 return ata_drive_probe_reset(ap, ata_std_probeinit,
Tejun Heo7944ea92006-02-02 18:20:00 +09002229 ata_std_softreset, hardreset,
Tejun Heoc2bd5802006-01-24 17:05:22 +09002230 ata_std_postreset, classes);
2231}
2232
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002233static int do_probe_reset(struct ata_port *ap, ata_reset_fn_t reset,
2234 ata_postreset_fn_t postreset,
2235 unsigned int *classes)
2236{
2237 int i, rc;
2238
2239 for (i = 0; i < ATA_MAX_DEVICES; i++)
2240 classes[i] = ATA_DEV_UNKNOWN;
2241
2242 rc = reset(ap, 0, classes);
2243 if (rc)
2244 return rc;
2245
2246 /* If any class isn't ATA_DEV_UNKNOWN, consider classification
2247 * is complete and convert all ATA_DEV_UNKNOWN to
2248 * ATA_DEV_NONE.
2249 */
2250 for (i = 0; i < ATA_MAX_DEVICES; i++)
2251 if (classes[i] != ATA_DEV_UNKNOWN)
2252 break;
2253
2254 if (i < ATA_MAX_DEVICES)
2255 for (i = 0; i < ATA_MAX_DEVICES; i++)
2256 if (classes[i] == ATA_DEV_UNKNOWN)
2257 classes[i] = ATA_DEV_NONE;
2258
2259 if (postreset)
2260 postreset(ap, classes);
2261
2262 return classes[0] != ATA_DEV_UNKNOWN ? 0 : -ENODEV;
2263}
2264
2265/**
2266 * ata_drive_probe_reset - Perform probe reset with given methods
2267 * @ap: port to reset
Tejun Heo7944ea92006-02-02 18:20:00 +09002268 * @probeinit: probeinit method (can be NULL)
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002269 * @softreset: softreset method (can be NULL)
2270 * @hardreset: hardreset method (can be NULL)
2271 * @postreset: postreset method (can be NULL)
2272 * @classes: resulting classes of attached devices
2273 *
2274 * Reset the specified port and classify attached devices using
2275 * given methods. This function prefers softreset but tries all
2276 * possible reset sequences to reset and classify devices. This
2277 * function is intended to be used for constructing ->probe_reset
2278 * callback by low level drivers.
2279 *
2280 * Reset methods should follow the following rules.
2281 *
2282 * - Return 0 on sucess, -errno on failure.
2283 * - If classification is supported, fill classes[] with
2284 * recognized class codes.
2285 * - If classification is not supported, leave classes[] alone.
2286 * - If verbose is non-zero, print error message on failure;
2287 * otherwise, shut up.
2288 *
2289 * LOCKING:
2290 * Kernel thread context (may sleep)
2291 *
2292 * RETURNS:
2293 * 0 on success, -EINVAL if no reset method is avaliable, -ENODEV
2294 * if classification fails, and any error code from reset
2295 * methods.
2296 */
Tejun Heo7944ea92006-02-02 18:20:00 +09002297int ata_drive_probe_reset(struct ata_port *ap, ata_probeinit_fn_t probeinit,
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002298 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2299 ata_postreset_fn_t postreset, unsigned int *classes)
2300{
2301 int rc = -EINVAL;
2302
Tejun Heo7944ea92006-02-02 18:20:00 +09002303 if (probeinit)
2304 probeinit(ap);
2305
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002306 if (softreset) {
2307 rc = do_probe_reset(ap, softreset, postreset, classes);
2308 if (rc == 0)
2309 return 0;
2310 }
2311
2312 if (!hardreset)
2313 return rc;
2314
2315 rc = do_probe_reset(ap, hardreset, postreset, classes);
2316 if (rc == 0 || rc != -ENODEV)
2317 return rc;
2318
2319 if (softreset)
2320 rc = do_probe_reset(ap, softreset, postreset, classes);
2321
2322 return rc;
2323}
2324
Jeff Garzik057ace52005-10-22 14:27:05 -04002325static void ata_pr_blacklisted(const struct ata_port *ap,
2326 const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327{
2328 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, disabling DMA\n",
2329 ap->id, dev->devno);
2330}
2331
Arjan van de Ven98ac62d2005-11-28 10:06:23 +01002332static const char * const ata_dma_blacklist [] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 "WDC AC11000H",
2334 "WDC AC22100H",
2335 "WDC AC32500H",
2336 "WDC AC33100H",
2337 "WDC AC31600H",
2338 "WDC AC32100H",
2339 "WDC AC23200L",
2340 "Compaq CRD-8241B",
2341 "CRD-8400B",
2342 "CRD-8480B",
2343 "CRD-8482B",
2344 "CRD-84",
2345 "SanDisk SDP3B",
2346 "SanDisk SDP3B-64",
2347 "SANYO CD-ROM CRD",
2348 "HITACHI CDR-8",
2349 "HITACHI CDR-8335",
2350 "HITACHI CDR-8435",
2351 "Toshiba CD-ROM XM-6202B",
Jeff Garzike9222562005-06-28 00:03:37 -04002352 "TOSHIBA CD-ROM XM-1702BC",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 "CD-532E-A",
2354 "E-IDE CD-ROM CR-840",
2355 "CD-ROM Drive/F5A",
2356 "WPI CDD-820",
2357 "SAMSUNG CD-ROM SC-148C",
2358 "SAMSUNG CD-ROM SC",
2359 "SanDisk SDP3B-64",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 "ATAPI CD-ROM DRIVE 40X MAXIMUM",
2361 "_NEC DV5800A",
2362};
2363
Jeff Garzik057ace52005-10-22 14:27:05 -04002364static int ata_dma_blacklisted(const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365{
Tejun Heo2e026712006-02-12 22:47:04 +09002366 unsigned char model_num[41];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 int i;
2368
Tejun Heo6a62a042006-02-13 10:02:46 +09002369 ata_id_c_string(dev->id, model_num, ATA_ID_PROD_OFS, sizeof(model_num));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370
2371 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i++)
Tejun Heo2e026712006-02-12 22:47:04 +09002372 if (!strcmp(ata_dma_blacklist[i], model_num))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 return 1;
2374
2375 return 0;
2376}
2377
Jeff Garzik057ace52005-10-22 14:27:05 -04002378static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379{
Jeff Garzik057ace52005-10-22 14:27:05 -04002380 const struct ata_device *master, *slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 unsigned int mask;
2382
2383 master = &ap->device[0];
2384 slave = &ap->device[1];
2385
Tejun Heoa4631472006-02-11 19:11:13 +09002386 WARN_ON(!ata_dev_present(master) && !ata_dev_present(slave));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387
2388 if (shift == ATA_SHIFT_UDMA) {
2389 mask = ap->udma_mask;
2390 if (ata_dev_present(master)) {
2391 mask &= (master->id[ATA_ID_UDMA_MODES] & 0xff);
Jeff Garzik057ace52005-10-22 14:27:05 -04002392 if (ata_dma_blacklisted(master)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 mask = 0;
2394 ata_pr_blacklisted(ap, master);
2395 }
2396 }
2397 if (ata_dev_present(slave)) {
2398 mask &= (slave->id[ATA_ID_UDMA_MODES] & 0xff);
Jeff Garzik057ace52005-10-22 14:27:05 -04002399 if (ata_dma_blacklisted(slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 mask = 0;
2401 ata_pr_blacklisted(ap, slave);
2402 }
2403 }
2404 }
2405 else if (shift == ATA_SHIFT_MWDMA) {
2406 mask = ap->mwdma_mask;
2407 if (ata_dev_present(master)) {
2408 mask &= (master->id[ATA_ID_MWDMA_MODES] & 0x07);
Jeff Garzik057ace52005-10-22 14:27:05 -04002409 if (ata_dma_blacklisted(master)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002410 mask = 0;
2411 ata_pr_blacklisted(ap, master);
2412 }
2413 }
2414 if (ata_dev_present(slave)) {
2415 mask &= (slave->id[ATA_ID_MWDMA_MODES] & 0x07);
Jeff Garzik057ace52005-10-22 14:27:05 -04002416 if (ata_dma_blacklisted(slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 mask = 0;
2418 ata_pr_blacklisted(ap, slave);
2419 }
2420 }
2421 }
2422 else if (shift == ATA_SHIFT_PIO) {
2423 mask = ap->pio_mask;
2424 if (ata_dev_present(master)) {
2425 /* spec doesn't return explicit support for
2426 * PIO0-2, so we fake it
2427 */
2428 u16 tmp_mode = master->id[ATA_ID_PIO_MODES] & 0x03;
2429 tmp_mode <<= 3;
2430 tmp_mode |= 0x7;
2431 mask &= tmp_mode;
2432 }
2433 if (ata_dev_present(slave)) {
2434 /* spec doesn't return explicit support for
2435 * PIO0-2, so we fake it
2436 */
2437 u16 tmp_mode = slave->id[ATA_ID_PIO_MODES] & 0x03;
2438 tmp_mode <<= 3;
2439 tmp_mode |= 0x7;
2440 mask &= tmp_mode;
2441 }
2442 }
2443 else {
2444 mask = 0xffffffff; /* shut up compiler warning */
2445 BUG();
2446 }
2447
2448 return mask;
2449}
2450
2451/* find greatest bit */
2452static int fgb(u32 bitmap)
2453{
2454 unsigned int i;
2455 int x = -1;
2456
2457 for (i = 0; i < 32; i++)
2458 if (bitmap & (1 << i))
2459 x = i;
2460
2461 return x;
2462}
2463
2464/**
2465 * ata_choose_xfer_mode - attempt to find best transfer mode
2466 * @ap: Port for which an xfer mode will be selected
2467 * @xfer_mode_out: (output) SET FEATURES - XFER MODE code
2468 * @xfer_shift_out: (output) bit shift that selects this mode
2469 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04002470 * Based on host and device capabilities, determine the
2471 * maximum transfer mode that is amenable to all.
2472 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002474 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475 *
2476 * RETURNS:
2477 * Zero on success, negative on error.
2478 */
2479
Jeff Garzik057ace52005-10-22 14:27:05 -04002480static int ata_choose_xfer_mode(const struct ata_port *ap,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481 u8 *xfer_mode_out,
2482 unsigned int *xfer_shift_out)
2483{
2484 unsigned int mask, shift;
2485 int x, i;
2486
2487 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++) {
2488 shift = xfer_mode_classes[i].shift;
2489 mask = ata_get_mode_mask(ap, shift);
2490
2491 x = fgb(mask);
2492 if (x >= 0) {
2493 *xfer_mode_out = xfer_mode_classes[i].base + x;
2494 *xfer_shift_out = shift;
2495 return 0;
2496 }
2497 }
2498
2499 return -1;
2500}
2501
2502/**
2503 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2504 * @ap: Port associated with device @dev
2505 * @dev: Device to which command will be sent
2506 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002507 * Issue SET FEATURES - XFER MODE command to device @dev
2508 * on port @ap.
2509 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002510 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002511 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 */
2513
2514static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
2515{
Tejun Heoa0123702005-12-13 14:49:31 +09002516 struct ata_taskfile tf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517
2518 /* set up set-features taskfile */
2519 DPRINTK("set features - xfer mode\n");
2520
Tejun Heoa0123702005-12-13 14:49:31 +09002521 ata_tf_init(ap, &tf, dev->devno);
2522 tf.command = ATA_CMD_SET_FEATURES;
2523 tf.feature = SETFEATURES_XFER;
2524 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2525 tf.protocol = ATA_PROT_NODATA;
2526 tf.nsect = dev->xfer_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527
Tejun Heoa0123702005-12-13 14:49:31 +09002528 if (ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0)) {
2529 printk(KERN_ERR "ata%u: failed to set xfermode, disabled\n",
2530 ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 ata_port_disable(ap);
Tejun Heoa0123702005-12-13 14:49:31 +09002532 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533
2534 DPRINTK("EXIT\n");
2535}
2536
2537/**
Albert Lee59a10b12005-10-12 15:09:42 +08002538 * ata_dev_reread_id - Reread the device identify device info
2539 * @ap: port where the device is
2540 * @dev: device to reread the identify device info
2541 *
2542 * LOCKING:
2543 */
2544
2545static void ata_dev_reread_id(struct ata_port *ap, struct ata_device *dev)
2546{
Tejun Heoa0123702005-12-13 14:49:31 +09002547 struct ata_taskfile tf;
Albert Lee59a10b12005-10-12 15:09:42 +08002548
Tejun Heoa0123702005-12-13 14:49:31 +09002549 ata_tf_init(ap, &tf, dev->devno);
Albert Lee59a10b12005-10-12 15:09:42 +08002550
2551 if (dev->class == ATA_DEV_ATA) {
Tejun Heoa0123702005-12-13 14:49:31 +09002552 tf.command = ATA_CMD_ID_ATA;
Albert Lee59a10b12005-10-12 15:09:42 +08002553 DPRINTK("do ATA identify\n");
2554 } else {
Tejun Heoa0123702005-12-13 14:49:31 +09002555 tf.command = ATA_CMD_ID_ATAPI;
Albert Lee59a10b12005-10-12 15:09:42 +08002556 DPRINTK("do ATAPI identify\n");
2557 }
2558
Tejun Heoa0123702005-12-13 14:49:31 +09002559 tf.flags |= ATA_TFLAG_DEVICE;
2560 tf.protocol = ATA_PROT_PIO;
Albert Lee59a10b12005-10-12 15:09:42 +08002561
Tejun Heoa0123702005-12-13 14:49:31 +09002562 if (ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE,
2563 dev->id, sizeof(dev->id)))
Albert Lee59a10b12005-10-12 15:09:42 +08002564 goto err_out;
2565
Albert Lee59a10b12005-10-12 15:09:42 +08002566 swap_buf_le16(dev->id, ATA_ID_WORDS);
2567
Tejun Heo0bd33002006-02-12 22:47:05 +09002568 ata_dump_id(dev->id);
Albert Lee59a10b12005-10-12 15:09:42 +08002569
2570 DPRINTK("EXIT\n");
2571
2572 return;
2573err_out:
Tejun Heoa0123702005-12-13 14:49:31 +09002574 printk(KERN_ERR "ata%u: failed to reread ID, disabled\n", ap->id);
Albert Lee59a10b12005-10-12 15:09:42 +08002575 ata_port_disable(ap);
2576}
2577
2578/**
Albert Lee8bf62ec2005-05-12 15:29:42 -04002579 * ata_dev_init_params - Issue INIT DEV PARAMS command
2580 * @ap: Port associated with device @dev
2581 * @dev: Device to which command will be sent
2582 *
2583 * LOCKING:
Tejun Heo6aff8f12006-02-15 18:24:09 +09002584 * Kernel thread context (may sleep)
2585 *
2586 * RETURNS:
2587 * 0 on success, AC_ERR_* mask otherwise.
Albert Lee8bf62ec2005-05-12 15:29:42 -04002588 */
2589
Tejun Heo6aff8f12006-02-15 18:24:09 +09002590static unsigned int ata_dev_init_params(struct ata_port *ap,
2591 struct ata_device *dev)
Albert Lee8bf62ec2005-05-12 15:29:42 -04002592{
Tejun Heoa0123702005-12-13 14:49:31 +09002593 struct ata_taskfile tf;
Tejun Heo6aff8f12006-02-15 18:24:09 +09002594 unsigned int err_mask;
Albert Lee8bf62ec2005-05-12 15:29:42 -04002595 u16 sectors = dev->id[6];
2596 u16 heads = dev->id[3];
2597
2598 /* Number of sectors per track 1-255. Number of heads 1-16 */
2599 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
Tejun Heo6aff8f12006-02-15 18:24:09 +09002600 return 0;
Albert Lee8bf62ec2005-05-12 15:29:42 -04002601
2602 /* set up init dev params taskfile */
2603 DPRINTK("init dev params \n");
2604
Tejun Heoa0123702005-12-13 14:49:31 +09002605 ata_tf_init(ap, &tf, dev->devno);
2606 tf.command = ATA_CMD_INIT_DEV_PARAMS;
2607 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2608 tf.protocol = ATA_PROT_NODATA;
2609 tf.nsect = sectors;
2610 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
Albert Lee8bf62ec2005-05-12 15:29:42 -04002611
Tejun Heo6aff8f12006-02-15 18:24:09 +09002612 err_mask = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
Albert Lee8bf62ec2005-05-12 15:29:42 -04002613
Tejun Heo6aff8f12006-02-15 18:24:09 +09002614 DPRINTK("EXIT, err_mask=%x\n", err_mask);
2615 return err_mask;
Albert Lee8bf62ec2005-05-12 15:29:42 -04002616}
2617
2618/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002619 * ata_sg_clean - Unmap DMA memory associated with command
2620 * @qc: Command containing DMA memory to be released
2621 *
2622 * Unmap all mapped DMA memory associated with this command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623 *
2624 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002625 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626 */
2627
2628static void ata_sg_clean(struct ata_queued_cmd *qc)
2629{
2630 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002631 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002633 void *pad_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002634
Tejun Heoa4631472006-02-11 19:11:13 +09002635 WARN_ON(!(qc->flags & ATA_QCFLAG_DMAMAP));
2636 WARN_ON(sg == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637
2638 if (qc->flags & ATA_QCFLAG_SINGLE)
Jeff Garzikf1318832006-02-20 16:55:56 -05002639 WARN_ON(qc->n_elem > 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05002641 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002643 /* if we padded the buffer out to 32-bit bound, and data
2644 * xfer direction is from-device, we must copy from the
2645 * pad buffer back into the supplied buffer
2646 */
2647 if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
2648 pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2649
2650 if (qc->flags & ATA_QCFLAG_SG) {
Jeff Garzike1410f22005-11-14 14:06:26 -05002651 if (qc->n_elem)
2652 dma_unmap_sg(ap->host_set->dev, sg, qc->n_elem, dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002653 /* restore last sg */
2654 sg[qc->orig_n_elem - 1].length += qc->pad_len;
2655 if (pad_buf) {
2656 struct scatterlist *psg = &qc->pad_sgent;
2657 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2658 memcpy(addr + psg->offset, pad_buf, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05002659 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002660 }
2661 } else {
Tejun Heo2e242fa2006-02-20 23:48:38 +09002662 if (qc->n_elem)
Jeff Garzike1410f22005-11-14 14:06:26 -05002663 dma_unmap_single(ap->host_set->dev,
2664 sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
2665 dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002666 /* restore sg */
2667 sg->length += qc->pad_len;
2668 if (pad_buf)
2669 memcpy(qc->buf_virt + sg->length - qc->pad_len,
2670 pad_buf, qc->pad_len);
2671 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672
2673 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002674 qc->__sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675}
2676
2677/**
2678 * ata_fill_sg - Fill PCI IDE PRD table
2679 * @qc: Metadata associated with taskfile to be transferred
2680 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002681 * Fill PCI IDE PRD (scatter-gather) table with segments
2682 * associated with the current disk command.
2683 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 * LOCKING:
Jeff Garzik780a87f2005-05-30 15:41:05 -04002685 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 *
2687 */
2688static void ata_fill_sg(struct ata_queued_cmd *qc)
2689{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002691 struct scatterlist *sg;
2692 unsigned int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693
Tejun Heoa4631472006-02-11 19:11:13 +09002694 WARN_ON(qc->__sg == NULL);
Jeff Garzikf1318832006-02-20 16:55:56 -05002695 WARN_ON(qc->n_elem == 0 && qc->pad_len == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696
2697 idx = 0;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002698 ata_for_each_sg(sg, qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 u32 addr, offset;
2700 u32 sg_len, len;
2701
2702 /* determine if physical DMA addr spans 64K boundary.
2703 * Note h/w doesn't support 64-bit, so we unconditionally
2704 * truncate dma_addr_t to u32.
2705 */
2706 addr = (u32) sg_dma_address(sg);
2707 sg_len = sg_dma_len(sg);
2708
2709 while (sg_len) {
2710 offset = addr & 0xffff;
2711 len = sg_len;
2712 if ((offset + sg_len) > 0x10000)
2713 len = 0x10000 - offset;
2714
2715 ap->prd[idx].addr = cpu_to_le32(addr);
2716 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
2717 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
2718
2719 idx++;
2720 sg_len -= len;
2721 addr += len;
2722 }
2723 }
2724
2725 if (idx)
2726 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2727}
2728/**
2729 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2730 * @qc: Metadata associated with taskfile to check
2731 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002732 * Allow low-level driver to filter ATA PACKET commands, returning
2733 * a status indicating whether or not it is OK to use DMA for the
2734 * supplied PACKET command.
2735 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002737 * spin_lock_irqsave(host_set lock)
2738 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 * RETURNS: 0 when ATAPI DMA can be used
2740 * nonzero otherwise
2741 */
2742int ata_check_atapi_dma(struct ata_queued_cmd *qc)
2743{
2744 struct ata_port *ap = qc->ap;
2745 int rc = 0; /* Assume ATAPI DMA is OK by default */
2746
2747 if (ap->ops->check_atapi_dma)
2748 rc = ap->ops->check_atapi_dma(qc);
2749
2750 return rc;
2751}
2752/**
2753 * ata_qc_prep - Prepare taskfile for submission
2754 * @qc: Metadata associated with taskfile to be prepared
2755 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002756 * Prepare ATA taskfile for submission.
2757 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758 * LOCKING:
2759 * spin_lock_irqsave(host_set lock)
2760 */
2761void ata_qc_prep(struct ata_queued_cmd *qc)
2762{
2763 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2764 return;
2765
2766 ata_fill_sg(qc);
2767}
2768
Jeff Garzik0cba6322005-05-30 19:49:12 -04002769/**
2770 * ata_sg_init_one - Associate command with memory buffer
2771 * @qc: Command to be associated
2772 * @buf: Memory buffer
2773 * @buflen: Length of memory buffer, in bytes.
2774 *
2775 * Initialize the data-related elements of queued_cmd @qc
2776 * to point to a single memory buffer, @buf of byte length @buflen.
2777 *
2778 * LOCKING:
2779 * spin_lock_irqsave(host_set lock)
2780 */
2781
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
2783{
2784 struct scatterlist *sg;
2785
2786 qc->flags |= ATA_QCFLAG_SINGLE;
2787
2788 memset(&qc->sgent, 0, sizeof(qc->sgent));
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002789 qc->__sg = &qc->sgent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790 qc->n_elem = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002791 qc->orig_n_elem = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 qc->buf_virt = buf;
2793
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002794 sg = qc->__sg;
Jeff Garzikf0612bb2005-10-30 01:58:18 -05002795 sg_init_one(sg, buf, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796}
2797
Jeff Garzik0cba6322005-05-30 19:49:12 -04002798/**
2799 * ata_sg_init - Associate command with scatter-gather table.
2800 * @qc: Command to be associated
2801 * @sg: Scatter-gather table.
2802 * @n_elem: Number of elements in s/g table.
2803 *
2804 * Initialize the data-related elements of queued_cmd @qc
2805 * to point to a scatter-gather table @sg, containing @n_elem
2806 * elements.
2807 *
2808 * LOCKING:
2809 * spin_lock_irqsave(host_set lock)
2810 */
2811
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
2813 unsigned int n_elem)
2814{
2815 qc->flags |= ATA_QCFLAG_SG;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002816 qc->__sg = sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 qc->n_elem = n_elem;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002818 qc->orig_n_elem = n_elem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819}
2820
2821/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002822 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
2823 * @qc: Command with memory buffer to be mapped.
2824 *
2825 * DMA-map the memory buffer associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826 *
2827 * LOCKING:
2828 * spin_lock_irqsave(host_set lock)
2829 *
2830 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002831 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 */
2833
2834static int ata_sg_setup_one(struct ata_queued_cmd *qc)
2835{
2836 struct ata_port *ap = qc->ap;
2837 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002838 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839 dma_addr_t dma_address;
Tejun Heo2e242fa2006-02-20 23:48:38 +09002840 int trim_sg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002842 /* we must lengthen transfers to end on a 32-bit boundary */
2843 qc->pad_len = sg->length & 3;
2844 if (qc->pad_len) {
2845 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2846 struct scatterlist *psg = &qc->pad_sgent;
2847
Tejun Heoa4631472006-02-11 19:11:13 +09002848 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002849
2850 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2851
2852 if (qc->tf.flags & ATA_TFLAG_WRITE)
2853 memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
2854 qc->pad_len);
2855
2856 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2857 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2858 /* trim sg */
2859 sg->length -= qc->pad_len;
Tejun Heo2e242fa2006-02-20 23:48:38 +09002860 if (sg->length == 0)
2861 trim_sg = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002862
2863 DPRINTK("padding done, sg->length=%u pad_len=%u\n",
2864 sg->length, qc->pad_len);
2865 }
2866
Tejun Heo2e242fa2006-02-20 23:48:38 +09002867 if (trim_sg) {
2868 qc->n_elem--;
Jeff Garzike1410f22005-11-14 14:06:26 -05002869 goto skip_map;
2870 }
2871
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 dma_address = dma_map_single(ap->host_set->dev, qc->buf_virt,
Albert Lee32529e02005-05-26 03:49:42 -04002873 sg->length, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05002874 if (dma_mapping_error(dma_address)) {
2875 /* restore sg */
2876 sg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05002878 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879
2880 sg_dma_address(sg) = dma_address;
Albert Lee32529e02005-05-26 03:49:42 -04002881 sg_dma_len(sg) = sg->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002882
Tejun Heo2e242fa2006-02-20 23:48:38 +09002883skip_map:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
2885 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2886
2887 return 0;
2888}
2889
2890/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002891 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
2892 * @qc: Command with scatter-gather table to be mapped.
2893 *
2894 * DMA-map the scatter-gather table associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 *
2896 * LOCKING:
2897 * spin_lock_irqsave(host_set lock)
2898 *
2899 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002900 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 *
2902 */
2903
2904static int ata_sg_setup(struct ata_queued_cmd *qc)
2905{
2906 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002907 struct scatterlist *sg = qc->__sg;
2908 struct scatterlist *lsg = &sg[qc->n_elem - 1];
Jeff Garzike1410f22005-11-14 14:06:26 -05002909 int n_elem, pre_n_elem, dir, trim_sg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910
2911 VPRINTK("ENTER, ata%u\n", ap->id);
Tejun Heoa4631472006-02-11 19:11:13 +09002912 WARN_ON(!(qc->flags & ATA_QCFLAG_SG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002914 /* we must lengthen transfers to end on a 32-bit boundary */
2915 qc->pad_len = lsg->length & 3;
2916 if (qc->pad_len) {
2917 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2918 struct scatterlist *psg = &qc->pad_sgent;
2919 unsigned int offset;
2920
Tejun Heoa4631472006-02-11 19:11:13 +09002921 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002922
2923 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2924
2925 /*
2926 * psg->page/offset are used to copy to-be-written
2927 * data in this function or read data in ata_sg_clean.
2928 */
2929 offset = lsg->offset + lsg->length - qc->pad_len;
2930 psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
2931 psg->offset = offset_in_page(offset);
2932
2933 if (qc->tf.flags & ATA_TFLAG_WRITE) {
2934 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2935 memcpy(pad_buf, addr + psg->offset, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05002936 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002937 }
2938
2939 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2940 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2941 /* trim last sg */
2942 lsg->length -= qc->pad_len;
Jeff Garzike1410f22005-11-14 14:06:26 -05002943 if (lsg->length == 0)
2944 trim_sg = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002945
2946 DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
2947 qc->n_elem - 1, lsg->length, qc->pad_len);
2948 }
2949
Jeff Garzike1410f22005-11-14 14:06:26 -05002950 pre_n_elem = qc->n_elem;
2951 if (trim_sg && pre_n_elem)
2952 pre_n_elem--;
2953
2954 if (!pre_n_elem) {
2955 n_elem = 0;
2956 goto skip_map;
2957 }
2958
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 dir = qc->dma_dir;
Jeff Garzike1410f22005-11-14 14:06:26 -05002960 n_elem = dma_map_sg(ap->host_set->dev, sg, pre_n_elem, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05002961 if (n_elem < 1) {
2962 /* restore last sg */
2963 lsg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05002965 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966
2967 DPRINTK("%d sg elements mapped\n", n_elem);
2968
Jeff Garzike1410f22005-11-14 14:06:26 -05002969skip_map:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 qc->n_elem = n_elem;
2971
2972 return 0;
2973}
2974
2975/**
Tejun Heo40e8c822005-08-22 17:12:45 +09002976 * ata_poll_qc_complete - turn irq back on and finish qc
2977 * @qc: Command to complete
Randy Dunlap8e8b77d2005-11-01 21:29:27 -08002978 * @err_mask: ATA status register content
Tejun Heo40e8c822005-08-22 17:12:45 +09002979 *
2980 * LOCKING:
2981 * None. (grabs host lock)
2982 */
2983
Albert Leea22e2eb2005-12-05 15:38:02 +08002984void ata_poll_qc_complete(struct ata_queued_cmd *qc)
Tejun Heo40e8c822005-08-22 17:12:45 +09002985{
2986 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04002987 unsigned long flags;
Tejun Heo40e8c822005-08-22 17:12:45 +09002988
Jeff Garzikb8f61532005-08-25 22:01:20 -04002989 spin_lock_irqsave(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09002990 ap->flags &= ~ATA_FLAG_NOINTR;
2991 ata_irq_on(ap);
Albert Leea22e2eb2005-12-05 15:38:02 +08002992 ata_qc_complete(qc);
Jeff Garzikb8f61532005-08-25 22:01:20 -04002993 spin_unlock_irqrestore(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09002994}
2995
2996/**
Randy Dunlapc893a3a2006-01-28 13:15:32 -05002997 * ata_pio_poll - poll using PIO, depending on current state
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002998 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 *
3000 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003001 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 *
3003 * RETURNS:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003004 * timeout value to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07003005 */
3006
3007static unsigned long ata_pio_poll(struct ata_port *ap)
3008{
Albert Leec14b8332005-12-05 15:36:08 +08003009 struct ata_queued_cmd *qc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 u8 status;
Albert Lee14be71f2005-09-27 17:36:35 +08003011 unsigned int poll_state = HSM_ST_UNKNOWN;
3012 unsigned int reg_state = HSM_ST_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013
Albert Leec14b8332005-12-05 15:36:08 +08003014 qc = ata_qc_from_tag(ap, ap->active_tag);
Tejun Heoa4631472006-02-11 19:11:13 +09003015 WARN_ON(qc == NULL);
Albert Leec14b8332005-12-05 15:36:08 +08003016
Albert Lee14be71f2005-09-27 17:36:35 +08003017 switch (ap->hsm_task_state) {
3018 case HSM_ST:
3019 case HSM_ST_POLL:
3020 poll_state = HSM_ST_POLL;
3021 reg_state = HSM_ST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 break;
Albert Lee14be71f2005-09-27 17:36:35 +08003023 case HSM_ST_LAST:
3024 case HSM_ST_LAST_POLL:
3025 poll_state = HSM_ST_LAST_POLL;
3026 reg_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 break;
3028 default:
3029 BUG();
3030 break;
3031 }
3032
3033 status = ata_chk_status(ap);
3034 if (status & ATA_BUSY) {
3035 if (time_after(jiffies, ap->pio_task_timeout)) {
Tejun Heo11a56d22006-01-23 13:09:36 +09003036 qc->err_mask |= AC_ERR_TIMEOUT;
Albert Lee7c398332005-11-09 13:03:30 +08003037 ap->hsm_task_state = HSM_ST_TMOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 return 0;
3039 }
Albert Lee14be71f2005-09-27 17:36:35 +08003040 ap->hsm_task_state = poll_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 return ATA_SHORT_PAUSE;
3042 }
3043
Albert Lee14be71f2005-09-27 17:36:35 +08003044 ap->hsm_task_state = reg_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045 return 0;
3046}
3047
3048/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003049 * ata_pio_complete - check if drive is busy or idle
3050 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051 *
3052 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003053 * None. (executing in kernel thread context)
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003054 *
3055 * RETURNS:
3056 * Non-zero if qc completed, zero otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057 */
3058
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003059static int ata_pio_complete (struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060{
3061 struct ata_queued_cmd *qc;
3062 u8 drv_stat;
3063
3064 /*
Alan Cox31433ea2005-08-26 15:56:47 +01003065 * This is purely heuristic. This is a fast path. Sometimes when
3066 * we enter, BSY will be cleared in a chk-status or two. If not,
3067 * the drive is probably seeking or something. Snooze for a couple
3068 * msecs, then chk-status again. If still busy, fall back to
Albert Lee14be71f2005-09-27 17:36:35 +08003069 * HSM_ST_POLL state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 */
Albert Leefe79e682005-12-06 11:34:59 +08003071 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
3072 if (drv_stat & ATA_BUSY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 msleep(2);
Albert Leefe79e682005-12-06 11:34:59 +08003074 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
3075 if (drv_stat & ATA_BUSY) {
Albert Lee14be71f2005-09-27 17:36:35 +08003076 ap->hsm_task_state = HSM_ST_LAST_POLL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003078 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 }
3080 }
3081
Albert Leec14b8332005-12-05 15:36:08 +08003082 qc = ata_qc_from_tag(ap, ap->active_tag);
Tejun Heoa4631472006-02-11 19:11:13 +09003083 WARN_ON(qc == NULL);
Albert Leec14b8332005-12-05 15:36:08 +08003084
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 drv_stat = ata_wait_idle(ap);
3086 if (!ata_ok(drv_stat)) {
Albert Lee1c848982005-12-05 15:40:15 +08003087 qc->err_mask |= __ac_err_mask(drv_stat);
Albert Lee14be71f2005-09-27 17:36:35 +08003088 ap->hsm_task_state = HSM_ST_ERR;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003089 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 }
3091
Albert Lee14be71f2005-09-27 17:36:35 +08003092 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093
Tejun Heoa4631472006-02-11 19:11:13 +09003094 WARN_ON(qc->err_mask);
Albert Leea22e2eb2005-12-05 15:38:02 +08003095 ata_poll_qc_complete(qc);
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003096
3097 /* another command may start at this point */
3098
3099 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100}
3101
Edward Falk0baab862005-06-02 18:17:13 -04003102
3103/**
Randy Dunlapc893a3a2006-01-28 13:15:32 -05003104 * swap_buf_le16 - swap halves of 16-bit words in place
Edward Falk0baab862005-06-02 18:17:13 -04003105 * @buf: Buffer to swap
3106 * @buf_words: Number of 16-bit words in buffer.
3107 *
3108 * Swap halves of 16-bit words if needed to convert from
3109 * little-endian byte order to native cpu byte order, or
3110 * vice-versa.
3111 *
3112 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003113 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04003114 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115void swap_buf_le16(u16 *buf, unsigned int buf_words)
3116{
3117#ifdef __BIG_ENDIAN
3118 unsigned int i;
3119
3120 for (i = 0; i < buf_words; i++)
3121 buf[i] = le16_to_cpu(buf[i]);
3122#endif /* __BIG_ENDIAN */
3123}
3124
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003125/**
3126 * ata_mmio_data_xfer - Transfer data by MMIO
3127 * @ap: port to read/write
3128 * @buf: data buffer
3129 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04003130 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003131 *
3132 * Transfer data from/to the device data register by MMIO.
3133 *
3134 * LOCKING:
3135 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003136 */
3137
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
3139 unsigned int buflen, int write_data)
3140{
3141 unsigned int i;
3142 unsigned int words = buflen >> 1;
3143 u16 *buf16 = (u16 *) buf;
3144 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
3145
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003146 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 if (write_data) {
3148 for (i = 0; i < words; i++)
3149 writew(le16_to_cpu(buf16[i]), mmio);
3150 } else {
3151 for (i = 0; i < words; i++)
3152 buf16[i] = cpu_to_le16(readw(mmio));
3153 }
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003154
3155 /* Transfer trailing 1 byte, if any. */
3156 if (unlikely(buflen & 0x01)) {
3157 u16 align_buf[1] = { 0 };
3158 unsigned char *trailing_buf = buf + buflen - 1;
3159
3160 if (write_data) {
3161 memcpy(align_buf, trailing_buf, 1);
3162 writew(le16_to_cpu(align_buf[0]), mmio);
3163 } else {
3164 align_buf[0] = cpu_to_le16(readw(mmio));
3165 memcpy(trailing_buf, align_buf, 1);
3166 }
3167 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168}
3169
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003170/**
3171 * ata_pio_data_xfer - Transfer data by PIO
3172 * @ap: port to read/write
3173 * @buf: data buffer
3174 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04003175 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003176 *
3177 * Transfer data from/to the device data register by PIO.
3178 *
3179 * LOCKING:
3180 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003181 */
3182
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
3184 unsigned int buflen, int write_data)
3185{
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003186 unsigned int words = buflen >> 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003188 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 if (write_data)
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003190 outsw(ap->ioaddr.data_addr, buf, words);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 else
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003192 insw(ap->ioaddr.data_addr, buf, words);
3193
3194 /* Transfer trailing 1 byte, if any. */
3195 if (unlikely(buflen & 0x01)) {
3196 u16 align_buf[1] = { 0 };
3197 unsigned char *trailing_buf = buf + buflen - 1;
3198
3199 if (write_data) {
3200 memcpy(align_buf, trailing_buf, 1);
3201 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
3202 } else {
3203 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
3204 memcpy(trailing_buf, align_buf, 1);
3205 }
3206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207}
3208
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003209/**
3210 * ata_data_xfer - Transfer data from/to the data register.
3211 * @ap: port to read/write
3212 * @buf: data buffer
3213 * @buflen: buffer length
3214 * @do_write: read/write
3215 *
3216 * Transfer data from/to the device data register.
3217 *
3218 * LOCKING:
3219 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003220 */
3221
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
3223 unsigned int buflen, int do_write)
3224{
Alan Coxa1bd9e62006-01-17 20:53:50 +00003225 /* Make the crap hardware pay the costs not the good stuff */
3226 if (unlikely(ap->flags & ATA_FLAG_IRQ_MASK)) {
3227 unsigned long flags;
3228 local_irq_save(flags);
3229 if (ap->flags & ATA_FLAG_MMIO)
3230 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3231 else
3232 ata_pio_data_xfer(ap, buf, buflen, do_write);
3233 local_irq_restore(flags);
3234 } else {
3235 if (ap->flags & ATA_FLAG_MMIO)
3236 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3237 else
3238 ata_pio_data_xfer(ap, buf, buflen, do_write);
3239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240}
3241
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003242/**
3243 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
3244 * @qc: Command on going
3245 *
3246 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
3247 *
3248 * LOCKING:
3249 * Inherited from caller.
3250 */
3251
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252static void ata_pio_sector(struct ata_queued_cmd *qc)
3253{
3254 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003255 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256 struct ata_port *ap = qc->ap;
3257 struct page *page;
3258 unsigned int offset;
3259 unsigned char *buf;
3260
3261 if (qc->cursect == (qc->nsect - 1))
Albert Lee14be71f2005-09-27 17:36:35 +08003262 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263
3264 page = sg[qc->cursg].page;
3265 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
3266
3267 /* get the current page and offset */
3268 page = nth_page(page, (offset >> PAGE_SHIFT));
3269 offset %= PAGE_SIZE;
3270
3271 buf = kmap(page) + offset;
3272
3273 qc->cursect++;
3274 qc->cursg_ofs++;
3275
Albert Lee32529e02005-05-26 03:49:42 -04003276 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 qc->cursg++;
3278 qc->cursg_ofs = 0;
3279 }
3280
3281 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3282
3283 /* do the actual data transfer */
3284 do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3285 ata_data_xfer(ap, buf, ATA_SECT_SIZE, do_write);
3286
3287 kunmap(page);
3288}
3289
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003290/**
3291 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
3292 * @qc: Command on going
3293 * @bytes: number of bytes
3294 *
3295 * Transfer Transfer data from/to the ATAPI device.
3296 *
3297 * LOCKING:
3298 * Inherited from caller.
3299 *
3300 */
3301
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
3303{
3304 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003305 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003306 struct ata_port *ap = qc->ap;
3307 struct page *page;
3308 unsigned char *buf;
3309 unsigned int offset, count;
3310
Albert Lee563a6e12005-08-12 14:17:50 +08003311 if (qc->curbytes + bytes >= qc->nbytes)
Albert Lee14be71f2005-09-27 17:36:35 +08003312 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313
3314next_sg:
Albert Lee563a6e12005-08-12 14:17:50 +08003315 if (unlikely(qc->cursg >= qc->n_elem)) {
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003316 /*
Albert Lee563a6e12005-08-12 14:17:50 +08003317 * The end of qc->sg is reached and the device expects
3318 * more data to transfer. In order not to overrun qc->sg
3319 * and fulfill length specified in the byte count register,
3320 * - for read case, discard trailing data from the device
3321 * - for write case, padding zero data to the device
3322 */
3323 u16 pad_buf[1] = { 0 };
3324 unsigned int words = bytes >> 1;
3325 unsigned int i;
3326
3327 if (words) /* warning if bytes > 1 */
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003328 printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
Albert Lee563a6e12005-08-12 14:17:50 +08003329 ap->id, bytes);
3330
3331 for (i = 0; i < words; i++)
3332 ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
3333
Albert Lee14be71f2005-09-27 17:36:35 +08003334 ap->hsm_task_state = HSM_ST_LAST;
Albert Lee563a6e12005-08-12 14:17:50 +08003335 return;
3336 }
3337
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003338 sg = &qc->__sg[qc->cursg];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339
Linus Torvalds1da177e2005-04-16 15:20:36 -07003340 page = sg->page;
3341 offset = sg->offset + qc->cursg_ofs;
3342
3343 /* get the current page and offset */
3344 page = nth_page(page, (offset >> PAGE_SHIFT));
3345 offset %= PAGE_SIZE;
3346
Albert Lee6952df02005-06-06 15:56:03 +08003347 /* don't overrun current sg */
Albert Lee32529e02005-05-26 03:49:42 -04003348 count = min(sg->length - qc->cursg_ofs, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349
3350 /* don't cross page boundaries */
3351 count = min(count, (unsigned int)PAGE_SIZE - offset);
3352
3353 buf = kmap(page) + offset;
3354
3355 bytes -= count;
3356 qc->curbytes += count;
3357 qc->cursg_ofs += count;
3358
Albert Lee32529e02005-05-26 03:49:42 -04003359 if (qc->cursg_ofs == sg->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360 qc->cursg++;
3361 qc->cursg_ofs = 0;
3362 }
3363
3364 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3365
3366 /* do the actual data transfer */
3367 ata_data_xfer(ap, buf, count, do_write);
3368
3369 kunmap(page);
3370
Albert Lee563a6e12005-08-12 14:17:50 +08003371 if (bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 goto next_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373}
3374
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003375/**
3376 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
3377 * @qc: Command on going
3378 *
3379 * Transfer Transfer data from/to the ATAPI device.
3380 *
3381 * LOCKING:
3382 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003383 */
3384
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385static void atapi_pio_bytes(struct ata_queued_cmd *qc)
3386{
3387 struct ata_port *ap = qc->ap;
3388 struct ata_device *dev = qc->dev;
3389 unsigned int ireason, bc_lo, bc_hi, bytes;
3390 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
3391
3392 ap->ops->tf_read(ap, &qc->tf);
3393 ireason = qc->tf.nsect;
3394 bc_lo = qc->tf.lbam;
3395 bc_hi = qc->tf.lbah;
3396 bytes = (bc_hi << 8) | bc_lo;
3397
3398 /* shall be cleared to zero, indicating xfer of data */
3399 if (ireason & (1 << 0))
3400 goto err_out;
3401
3402 /* make sure transfer direction matches expected */
3403 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
3404 if (do_write != i_write)
3405 goto err_out;
3406
3407 __atapi_pio_bytes(qc, bytes);
3408
3409 return;
3410
3411err_out:
3412 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
3413 ap->id, dev->devno);
Tejun Heo11a56d22006-01-23 13:09:36 +09003414 qc->err_mask |= AC_ERR_HSM;
Albert Lee14be71f2005-09-27 17:36:35 +08003415 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416}
3417
3418/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003419 * ata_pio_block - start PIO on a block
3420 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421 *
3422 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003423 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 */
3425
3426static void ata_pio_block(struct ata_port *ap)
3427{
3428 struct ata_queued_cmd *qc;
3429 u8 status;
3430
3431 /*
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003432 * This is purely heuristic. This is a fast path.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433 * Sometimes when we enter, BSY will be cleared in
3434 * a chk-status or two. If not, the drive is probably seeking
3435 * or something. Snooze for a couple msecs, then
3436 * chk-status again. If still busy, fall back to
Albert Lee14be71f2005-09-27 17:36:35 +08003437 * HSM_ST_POLL state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438 */
3439 status = ata_busy_wait(ap, ATA_BUSY, 5);
3440 if (status & ATA_BUSY) {
3441 msleep(2);
3442 status = ata_busy_wait(ap, ATA_BUSY, 10);
3443 if (status & ATA_BUSY) {
Albert Lee14be71f2005-09-27 17:36:35 +08003444 ap->hsm_task_state = HSM_ST_POLL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
3446 return;
3447 }
3448 }
3449
3450 qc = ata_qc_from_tag(ap, ap->active_tag);
Tejun Heoa4631472006-02-11 19:11:13 +09003451 WARN_ON(qc == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452
Albert Leefe79e682005-12-06 11:34:59 +08003453 /* check error */
3454 if (status & (ATA_ERR | ATA_DF)) {
3455 qc->err_mask |= AC_ERR_DEV;
3456 ap->hsm_task_state = HSM_ST_ERR;
3457 return;
3458 }
3459
3460 /* transfer data if any */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461 if (is_atapi_taskfile(&qc->tf)) {
Albert Leefe79e682005-12-06 11:34:59 +08003462 /* DRQ=0 means no more data to transfer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003463 if ((status & ATA_DRQ) == 0) {
Albert Lee14be71f2005-09-27 17:36:35 +08003464 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465 return;
3466 }
3467
3468 atapi_pio_bytes(qc);
3469 } else {
3470 /* handle BSY=0, DRQ=0 as error */
3471 if ((status & ATA_DRQ) == 0) {
Tejun Heo11a56d22006-01-23 13:09:36 +09003472 qc->err_mask |= AC_ERR_HSM;
Albert Lee14be71f2005-09-27 17:36:35 +08003473 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474 return;
3475 }
3476
3477 ata_pio_sector(qc);
3478 }
3479}
3480
3481static void ata_pio_error(struct ata_port *ap)
3482{
3483 struct ata_queued_cmd *qc;
Jeff Garzika7dac442005-10-30 04:44:42 -05003484
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 qc = ata_qc_from_tag(ap, ap->active_tag);
Tejun Heoa4631472006-02-11 19:11:13 +09003486 WARN_ON(qc == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487
Albert Lee0565c262006-02-13 18:55:25 +08003488 if (qc->tf.command != ATA_CMD_PACKET)
3489 printk(KERN_WARNING "ata%u: PIO error\n", ap->id);
3490
Albert Lee1c848982005-12-05 15:40:15 +08003491 /* make sure qc->err_mask is available to
3492 * know what's wrong and recover
3493 */
Tejun Heoa4631472006-02-11 19:11:13 +09003494 WARN_ON(qc->err_mask == 0);
Albert Lee1c848982005-12-05 15:40:15 +08003495
Albert Lee14be71f2005-09-27 17:36:35 +08003496 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497
Albert Leea22e2eb2005-12-05 15:38:02 +08003498 ata_poll_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499}
3500
3501static void ata_pio_task(void *_data)
3502{
3503 struct ata_port *ap = _data;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003504 unsigned long timeout;
3505 int qc_completed;
3506
3507fsm_start:
3508 timeout = 0;
3509 qc_completed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510
Albert Lee14be71f2005-09-27 17:36:35 +08003511 switch (ap->hsm_task_state) {
3512 case HSM_ST_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003513 return;
3514
Albert Lee14be71f2005-09-27 17:36:35 +08003515 case HSM_ST:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 ata_pio_block(ap);
3517 break;
3518
Albert Lee14be71f2005-09-27 17:36:35 +08003519 case HSM_ST_LAST:
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003520 qc_completed = ata_pio_complete(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 break;
3522
Albert Lee14be71f2005-09-27 17:36:35 +08003523 case HSM_ST_POLL:
3524 case HSM_ST_LAST_POLL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 timeout = ata_pio_poll(ap);
3526 break;
3527
Albert Lee14be71f2005-09-27 17:36:35 +08003528 case HSM_ST_TMOUT:
3529 case HSM_ST_ERR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530 ata_pio_error(ap);
3531 return;
3532 }
3533
3534 if (timeout)
Tejun Heo95064372006-01-23 13:09:37 +09003535 ata_queue_delayed_pio_task(ap, timeout);
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003536 else if (!qc_completed)
3537 goto fsm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538}
3539
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540/**
3541 * ata_qc_timeout - Handle timeout of queued command
3542 * @qc: Command that timed out
3543 *
3544 * Some part of the kernel (currently, only the SCSI layer)
3545 * has noticed that the active command on port @ap has not
3546 * completed after a specified length of time. Handle this
3547 * condition by disabling DMA (if necessary) and completing
3548 * transactions, with error if necessary.
3549 *
3550 * This also handles the case of the "lost interrupt", where
3551 * for some reason (possibly hardware bug, possibly driver bug)
3552 * an interrupt was not delivered to the driver, even though the
3553 * transaction completed successfully.
3554 *
3555 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003556 * Inherited from SCSI layer (none, can sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557 */
3558
3559static void ata_qc_timeout(struct ata_queued_cmd *qc)
3560{
3561 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003562 struct ata_host_set *host_set = ap->host_set;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563 u8 host_stat = 0, drv_stat;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003564 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565
3566 DPRINTK("ENTER\n");
3567
Tejun Heoc18d06f2006-02-02 00:56:10 +09003568 ata_flush_pio_tasks(ap);
3569 ap->hsm_task_state = HSM_ST_IDLE;
3570
Jeff Garzikb8f61532005-08-25 22:01:20 -04003571 spin_lock_irqsave(&host_set->lock, flags);
3572
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573 switch (qc->tf.protocol) {
3574
3575 case ATA_PROT_DMA:
3576 case ATA_PROT_ATAPI_DMA:
3577 host_stat = ap->ops->bmdma_status(ap);
3578
3579 /* before we do anything else, clear DMA-Start bit */
Alan Coxb73fc892005-08-26 16:03:19 +01003580 ap->ops->bmdma_stop(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581
3582 /* fall through */
3583
3584 default:
3585 ata_altstatus(ap);
3586 drv_stat = ata_chk_status(ap);
3587
3588 /* ack bmdma irq events */
3589 ap->ops->irq_clear(ap);
3590
3591 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
3592 ap->id, qc->tf.command, drv_stat, host_stat);
3593
3594 /* complete taskfile transaction */
Albert Leea22e2eb2005-12-05 15:38:02 +08003595 qc->err_mask |= ac_err_mask(drv_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003596 break;
3597 }
Jeff Garzikb8f61532005-08-25 22:01:20 -04003598
3599 spin_unlock_irqrestore(&host_set->lock, flags);
3600
Tejun Heoa72ec4c2006-01-23 13:09:37 +09003601 ata_eh_qc_complete(qc);
3602
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603 DPRINTK("EXIT\n");
3604}
3605
3606/**
3607 * ata_eng_timeout - Handle timeout of queued command
3608 * @ap: Port on which timed-out command is active
3609 *
3610 * Some part of the kernel (currently, only the SCSI layer)
3611 * has noticed that the active command on port @ap has not
3612 * completed after a specified length of time. Handle this
3613 * condition by disabling DMA (if necessary) and completing
3614 * transactions, with error if necessary.
3615 *
3616 * This also handles the case of the "lost interrupt", where
3617 * for some reason (possibly hardware bug, possibly driver bug)
3618 * an interrupt was not delivered to the driver, even though the
3619 * transaction completed successfully.
3620 *
3621 * LOCKING:
3622 * Inherited from SCSI layer (none, can sleep)
3623 */
3624
3625void ata_eng_timeout(struct ata_port *ap)
3626{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627 DPRINTK("ENTER\n");
3628
Tejun Heof6379022006-02-10 15:10:48 +09003629 ata_qc_timeout(ata_qc_from_tag(ap, ap->active_tag));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631 DPRINTK("EXIT\n");
3632}
3633
3634/**
3635 * ata_qc_new - Request an available ATA command, for queueing
3636 * @ap: Port associated with device @dev
3637 * @dev: Device from whom we request an available command structure
3638 *
3639 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003640 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003641 */
3642
3643static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
3644{
3645 struct ata_queued_cmd *qc = NULL;
3646 unsigned int i;
3647
3648 for (i = 0; i < ATA_MAX_QUEUE; i++)
3649 if (!test_and_set_bit(i, &ap->qactive)) {
3650 qc = ata_qc_from_tag(ap, i);
3651 break;
3652 }
3653
3654 if (qc)
3655 qc->tag = i;
3656
3657 return qc;
3658}
3659
3660/**
3661 * ata_qc_new_init - Request an available ATA command, and initialize it
3662 * @ap: Port associated with device @dev
3663 * @dev: Device from whom we request an available command structure
3664 *
3665 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003666 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667 */
3668
3669struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
3670 struct ata_device *dev)
3671{
3672 struct ata_queued_cmd *qc;
3673
3674 qc = ata_qc_new(ap);
3675 if (qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003676 qc->scsicmd = NULL;
3677 qc->ap = ap;
3678 qc->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05003680 ata_qc_reinit(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681 }
3682
3683 return qc;
3684}
3685
Linus Torvalds1da177e2005-04-16 15:20:36 -07003686/**
3687 * ata_qc_free - free unused ata_queued_cmd
3688 * @qc: Command to complete
3689 *
3690 * Designed to free unused ata_queued_cmd object
3691 * in case something prevents using it.
3692 *
3693 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003694 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003695 */
3696void ata_qc_free(struct ata_queued_cmd *qc)
3697{
Tejun Heo4ba946e2006-01-23 13:09:36 +09003698 struct ata_port *ap = qc->ap;
3699 unsigned int tag;
3700
Tejun Heoa4631472006-02-11 19:11:13 +09003701 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702
Tejun Heo4ba946e2006-01-23 13:09:36 +09003703 qc->flags = 0;
3704 tag = qc->tag;
3705 if (likely(ata_tag_valid(tag))) {
3706 if (tag == ap->active_tag)
3707 ap->active_tag = ATA_TAG_POISON;
3708 qc->tag = ATA_TAG_POISON;
3709 clear_bit(tag, &ap->qactive);
3710 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711}
3712
Tejun Heo76014422006-02-11 15:13:49 +09003713void __ata_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714{
Tejun Heoa4631472006-02-11 19:11:13 +09003715 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
3716 WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717
3718 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
3719 ata_sg_clean(qc);
3720
Albert Lee3f3791d2005-08-16 14:25:38 +08003721 /* atapi: mark qc as inactive to prevent the interrupt handler
3722 * from completing the command twice later, before the error handler
3723 * is called. (when rc != 0 and atapi request sense is needed)
3724 */
3725 qc->flags &= ~ATA_QCFLAG_ACTIVE;
3726
Linus Torvalds1da177e2005-04-16 15:20:36 -07003727 /* call completion callback */
Tejun Heo77853bf2006-01-23 13:09:36 +09003728 qc->complete_fn(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729}
3730
3731static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
3732{
3733 struct ata_port *ap = qc->ap;
3734
3735 switch (qc->tf.protocol) {
3736 case ATA_PROT_DMA:
3737 case ATA_PROT_ATAPI_DMA:
3738 return 1;
3739
3740 case ATA_PROT_ATAPI:
3741 case ATA_PROT_PIO:
3742 case ATA_PROT_PIO_MULT:
3743 if (ap->flags & ATA_FLAG_PIO_DMA)
3744 return 1;
3745
3746 /* fall through */
3747
3748 default:
3749 return 0;
3750 }
3751
3752 /* never reached */
3753}
3754
3755/**
3756 * ata_qc_issue - issue taskfile to device
3757 * @qc: command to issue to device
3758 *
3759 * Prepare an ATA command to submission to device.
3760 * This includes mapping the data into a DMA-able
3761 * area, filling in the S/G table, and finally
3762 * writing the taskfile to hardware, starting the command.
3763 *
3764 * LOCKING:
3765 * spin_lock_irqsave(host_set lock)
3766 *
3767 * RETURNS:
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003768 * Zero on success, AC_ERR_* mask on failure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769 */
3770
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003771unsigned int ata_qc_issue(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772{
3773 struct ata_port *ap = qc->ap;
3774
3775 if (ata_should_dma_map(qc)) {
3776 if (qc->flags & ATA_QCFLAG_SG) {
3777 if (ata_sg_setup(qc))
Tejun Heo8e436af2006-01-23 13:09:36 +09003778 goto sg_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
3780 if (ata_sg_setup_one(qc))
Tejun Heo8e436af2006-01-23 13:09:36 +09003781 goto sg_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782 }
3783 } else {
3784 qc->flags &= ~ATA_QCFLAG_DMAMAP;
3785 }
3786
3787 ap->ops->qc_prep(qc);
3788
3789 qc->ap->active_tag = qc->tag;
3790 qc->flags |= ATA_QCFLAG_ACTIVE;
3791
3792 return ap->ops->qc_issue(qc);
3793
Tejun Heo8e436af2006-01-23 13:09:36 +09003794sg_err:
3795 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003796 return AC_ERR_SYSTEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797}
3798
Edward Falk0baab862005-06-02 18:17:13 -04003799
Linus Torvalds1da177e2005-04-16 15:20:36 -07003800/**
3801 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
3802 * @qc: command to issue to device
3803 *
3804 * Using various libata functions and hooks, this function
3805 * starts an ATA command. ATA commands are grouped into
3806 * classes called "protocols", and issuing each type of protocol
3807 * is slightly different.
3808 *
Edward Falk0baab862005-06-02 18:17:13 -04003809 * May be used as the qc_issue() entry in ata_port_operations.
3810 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811 * LOCKING:
3812 * spin_lock_irqsave(host_set lock)
3813 *
3814 * RETURNS:
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003815 * Zero on success, AC_ERR_* mask on failure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003816 */
3817
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003818unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819{
3820 struct ata_port *ap = qc->ap;
3821
3822 ata_dev_select(ap, qc->dev->devno, 1, 0);
3823
3824 switch (qc->tf.protocol) {
3825 case ATA_PROT_NODATA:
Jeff Garzike5338252005-10-30 21:37:17 -05003826 ata_tf_to_host(ap, &qc->tf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003827 break;
3828
3829 case ATA_PROT_DMA:
3830 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3831 ap->ops->bmdma_setup(qc); /* set up bmdma */
3832 ap->ops->bmdma_start(qc); /* initiate bmdma */
3833 break;
3834
3835 case ATA_PROT_PIO: /* load tf registers, initiate polling pio */
3836 ata_qc_set_polling(qc);
Jeff Garzike5338252005-10-30 21:37:17 -05003837 ata_tf_to_host(ap, &qc->tf);
Albert Lee14be71f2005-09-27 17:36:35 +08003838 ap->hsm_task_state = HSM_ST;
Tejun Heo95064372006-01-23 13:09:37 +09003839 ata_queue_pio_task(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003840 break;
3841
3842 case ATA_PROT_ATAPI:
3843 ata_qc_set_polling(qc);
Jeff Garzike5338252005-10-30 21:37:17 -05003844 ata_tf_to_host(ap, &qc->tf);
Tejun Heo95064372006-01-23 13:09:37 +09003845 ata_queue_packet_task(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003846 break;
3847
3848 case ATA_PROT_ATAPI_NODATA:
Tejun Heoc1389502005-08-22 14:59:24 +09003849 ap->flags |= ATA_FLAG_NOINTR;
Jeff Garzike5338252005-10-30 21:37:17 -05003850 ata_tf_to_host(ap, &qc->tf);
Tejun Heo95064372006-01-23 13:09:37 +09003851 ata_queue_packet_task(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852 break;
3853
3854 case ATA_PROT_ATAPI_DMA:
Tejun Heoc1389502005-08-22 14:59:24 +09003855 ap->flags |= ATA_FLAG_NOINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003856 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3857 ap->ops->bmdma_setup(qc); /* set up bmdma */
Tejun Heo95064372006-01-23 13:09:37 +09003858 ata_queue_packet_task(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859 break;
3860
3861 default:
3862 WARN_ON(1);
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003863 return AC_ERR_SYSTEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003864 }
3865
3866 return 0;
3867}
3868
3869/**
Edward Falk0baab862005-06-02 18:17:13 -04003870 * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871 * @qc: Info associated with this ATA transaction.
3872 *
3873 * LOCKING:
3874 * spin_lock_irqsave(host_set lock)
3875 */
3876
3877static void ata_bmdma_setup_mmio (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 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3883
3884 /* load PRD table addr. */
3885 mb(); /* make sure PRD table writes are visible to controller */
3886 writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
3887
3888 /* specify data direction, triple-check start bit is clear */
3889 dmactl = readb(mmio + ATA_DMA_CMD);
3890 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3891 if (!rw)
3892 dmactl |= ATA_DMA_WR;
3893 writeb(dmactl, mmio + ATA_DMA_CMD);
3894
3895 /* issue r/w command */
3896 ap->ops->exec_command(ap, &qc->tf);
3897}
3898
3899/**
Alan Coxb73fc892005-08-26 16:03:19 +01003900 * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003901 * @qc: Info associated with this ATA transaction.
3902 *
3903 * LOCKING:
3904 * spin_lock_irqsave(host_set lock)
3905 */
3906
3907static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
3908{
3909 struct ata_port *ap = qc->ap;
3910 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3911 u8 dmactl;
3912
3913 /* start host DMA transaction */
3914 dmactl = readb(mmio + ATA_DMA_CMD);
3915 writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
3916
3917 /* Strictly, one may wish to issue a readb() here, to
3918 * flush the mmio write. However, control also passes
3919 * to the hardware at this point, and it will interrupt
3920 * us when we are to resume control. So, in effect,
3921 * we don't care when the mmio write flushes.
3922 * Further, a read of the DMA status register _immediately_
3923 * following the write may not be what certain flaky hardware
3924 * is expected, so I think it is best to not add a readb()
3925 * without first all the MMIO ATA cards/mobos.
3926 * Or maybe I'm just being paranoid.
3927 */
3928}
3929
3930/**
3931 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
3932 * @qc: Info associated with this ATA transaction.
3933 *
3934 * LOCKING:
3935 * spin_lock_irqsave(host_set lock)
3936 */
3937
3938static void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
3939{
3940 struct ata_port *ap = qc->ap;
3941 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3942 u8 dmactl;
3943
3944 /* load PRD table addr. */
3945 outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
3946
3947 /* specify data direction, triple-check start bit is clear */
3948 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3949 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3950 if (!rw)
3951 dmactl |= ATA_DMA_WR;
3952 outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3953
3954 /* issue r/w command */
3955 ap->ops->exec_command(ap, &qc->tf);
3956}
3957
3958/**
3959 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
3960 * @qc: Info associated with this ATA transaction.
3961 *
3962 * LOCKING:
3963 * spin_lock_irqsave(host_set lock)
3964 */
3965
3966static void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
3967{
3968 struct ata_port *ap = qc->ap;
3969 u8 dmactl;
3970
3971 /* start host DMA transaction */
3972 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3973 outb(dmactl | ATA_DMA_START,
3974 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3975}
3976
Edward Falk0baab862005-06-02 18:17:13 -04003977
3978/**
3979 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
3980 * @qc: Info associated with this ATA transaction.
3981 *
3982 * Writes the ATA_DMA_START flag to the DMA command register.
3983 *
3984 * May be used as the bmdma_start() entry in ata_port_operations.
3985 *
3986 * LOCKING:
3987 * spin_lock_irqsave(host_set lock)
3988 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989void ata_bmdma_start(struct ata_queued_cmd *qc)
3990{
3991 if (qc->ap->flags & ATA_FLAG_MMIO)
3992 ata_bmdma_start_mmio(qc);
3993 else
3994 ata_bmdma_start_pio(qc);
3995}
3996
Edward Falk0baab862005-06-02 18:17:13 -04003997
3998/**
3999 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
4000 * @qc: Info associated with this ATA transaction.
4001 *
4002 * Writes address of PRD table to device's PRD Table Address
4003 * register, sets the DMA control register, and calls
4004 * ops->exec_command() to start the transfer.
4005 *
4006 * May be used as the bmdma_setup() entry in ata_port_operations.
4007 *
4008 * LOCKING:
4009 * spin_lock_irqsave(host_set lock)
4010 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011void ata_bmdma_setup(struct ata_queued_cmd *qc)
4012{
4013 if (qc->ap->flags & ATA_FLAG_MMIO)
4014 ata_bmdma_setup_mmio(qc);
4015 else
4016 ata_bmdma_setup_pio(qc);
4017}
4018
Edward Falk0baab862005-06-02 18:17:13 -04004019
4020/**
4021 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
Jeff Garzikdecc6d02005-06-02 18:42:33 -04004022 * @ap: Port associated with this ATA transaction.
Edward Falk0baab862005-06-02 18:17:13 -04004023 *
4024 * Clear interrupt and error flags in DMA status register.
4025 *
4026 * May be used as the irq_clear() entry in ata_port_operations.
4027 *
4028 * LOCKING:
4029 * spin_lock_irqsave(host_set lock)
4030 */
4031
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032void ata_bmdma_irq_clear(struct ata_port *ap)
4033{
4034 if (ap->flags & ATA_FLAG_MMIO) {
4035 void __iomem *mmio = ((void __iomem *) ap->ioaddr.bmdma_addr) + ATA_DMA_STATUS;
4036 writeb(readb(mmio), mmio);
4037 } else {
4038 unsigned long addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
4039 outb(inb(addr), addr);
4040 }
4041
4042}
4043
Edward Falk0baab862005-06-02 18:17:13 -04004044
4045/**
4046 * ata_bmdma_status - Read PCI IDE BMDMA status
Jeff Garzikdecc6d02005-06-02 18:42:33 -04004047 * @ap: Port associated with this ATA transaction.
Edward Falk0baab862005-06-02 18:17:13 -04004048 *
4049 * Read and return BMDMA status register.
4050 *
4051 * May be used as the bmdma_status() entry in ata_port_operations.
4052 *
4053 * LOCKING:
4054 * spin_lock_irqsave(host_set lock)
4055 */
4056
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057u8 ata_bmdma_status(struct ata_port *ap)
4058{
4059 u8 host_stat;
4060 if (ap->flags & ATA_FLAG_MMIO) {
4061 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4062 host_stat = readb(mmio + ATA_DMA_STATUS);
4063 } else
Albert Leeee500aa2005-09-27 17:34:38 +08004064 host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065 return host_stat;
4066}
4067
Edward Falk0baab862005-06-02 18:17:13 -04004068
4069/**
4070 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
Alan Coxb73fc892005-08-26 16:03:19 +01004071 * @qc: Command we are ending DMA for
Edward Falk0baab862005-06-02 18:17:13 -04004072 *
4073 * Clears the ATA_DMA_START flag in the dma control register
4074 *
4075 * May be used as the bmdma_stop() entry in ata_port_operations.
4076 *
4077 * LOCKING:
4078 * spin_lock_irqsave(host_set lock)
4079 */
4080
Alan Coxb73fc892005-08-26 16:03:19 +01004081void ata_bmdma_stop(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082{
Alan Coxb73fc892005-08-26 16:03:19 +01004083 struct ata_port *ap = qc->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084 if (ap->flags & ATA_FLAG_MMIO) {
4085 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4086
4087 /* clear start/stop bit */
4088 writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
4089 mmio + ATA_DMA_CMD);
4090 } else {
4091 /* clear start/stop bit */
4092 outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
4093 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4094 }
4095
4096 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
4097 ata_altstatus(ap); /* dummy read */
4098}
4099
4100/**
4101 * ata_host_intr - Handle host interrupt for given (port, task)
4102 * @ap: Port on which interrupt arrived (possibly...)
4103 * @qc: Taskfile currently active in engine
4104 *
4105 * Handle host interrupt for given queued command. Currently,
4106 * only DMA interrupts are handled. All other commands are
4107 * handled via polling with interrupts disabled (nIEN bit).
4108 *
4109 * LOCKING:
4110 * spin_lock_irqsave(host_set lock)
4111 *
4112 * RETURNS:
4113 * One if interrupt was handled, zero if not (shared irq).
4114 */
4115
4116inline unsigned int ata_host_intr (struct ata_port *ap,
4117 struct ata_queued_cmd *qc)
4118{
4119 u8 status, host_stat;
4120
4121 switch (qc->tf.protocol) {
4122
4123 case ATA_PROT_DMA:
4124 case ATA_PROT_ATAPI_DMA:
4125 case ATA_PROT_ATAPI:
4126 /* check status of DMA engine */
4127 host_stat = ap->ops->bmdma_status(ap);
4128 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
4129
4130 /* if it's not our irq... */
4131 if (!(host_stat & ATA_DMA_INTR))
4132 goto idle_irq;
4133
4134 /* before we do anything else, clear DMA-Start bit */
Alan Coxb73fc892005-08-26 16:03:19 +01004135 ap->ops->bmdma_stop(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004136
4137 /* fall through */
4138
4139 case ATA_PROT_ATAPI_NODATA:
4140 case ATA_PROT_NODATA:
4141 /* check altstatus */
4142 status = ata_altstatus(ap);
4143 if (status & ATA_BUSY)
4144 goto idle_irq;
4145
4146 /* check main status, clearing INTRQ */
4147 status = ata_chk_status(ap);
4148 if (unlikely(status & ATA_BUSY))
4149 goto idle_irq;
4150 DPRINTK("ata%u: protocol %d (dev_stat 0x%X)\n",
4151 ap->id, qc->tf.protocol, status);
4152
4153 /* ack bmdma irq events */
4154 ap->ops->irq_clear(ap);
4155
4156 /* complete taskfile transaction */
Albert Leea22e2eb2005-12-05 15:38:02 +08004157 qc->err_mask |= ac_err_mask(status);
4158 ata_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159 break;
4160
4161 default:
4162 goto idle_irq;
4163 }
4164
4165 return 1; /* irq handled */
4166
4167idle_irq:
4168 ap->stats.idle_irq++;
4169
4170#ifdef ATA_IRQ_TRAP
4171 if ((ap->stats.idle_irq % 1000) == 0) {
4172 handled = 1;
4173 ata_irq_ack(ap, 0); /* debug trap */
4174 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
4175 }
4176#endif
4177 return 0; /* irq not handled */
4178}
4179
4180/**
4181 * ata_interrupt - Default ATA host interrupt handler
Jeff Garzik0cba6322005-05-30 19:49:12 -04004182 * @irq: irq line (unused)
4183 * @dev_instance: pointer to our ata_host_set information structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184 * @regs: unused
4185 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004186 * Default interrupt handler for PCI IDE devices. Calls
4187 * ata_host_intr() for each port that is not disabled.
4188 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004190 * Obtains host_set lock during operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191 *
4192 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004193 * IRQ_NONE or IRQ_HANDLED.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194 */
4195
4196irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
4197{
4198 struct ata_host_set *host_set = dev_instance;
4199 unsigned int i;
4200 unsigned int handled = 0;
4201 unsigned long flags;
4202
4203 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
4204 spin_lock_irqsave(&host_set->lock, flags);
4205
4206 for (i = 0; i < host_set->n_ports; i++) {
4207 struct ata_port *ap;
4208
4209 ap = host_set->ports[i];
Tejun Heoc1389502005-08-22 14:59:24 +09004210 if (ap &&
4211 !(ap->flags & (ATA_FLAG_PORT_DISABLED | ATA_FLAG_NOINTR))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212 struct ata_queued_cmd *qc;
4213
4214 qc = ata_qc_from_tag(ap, ap->active_tag);
Albert Lee21b1ed72005-04-29 17:34:59 +08004215 if (qc && (!(qc->tf.ctl & ATA_NIEN)) &&
4216 (qc->flags & ATA_QCFLAG_ACTIVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217 handled |= ata_host_intr(ap, qc);
4218 }
4219 }
4220
4221 spin_unlock_irqrestore(&host_set->lock, flags);
4222
4223 return IRQ_RETVAL(handled);
4224}
4225
4226/**
4227 * atapi_packet_task - Write CDB bytes to hardware
4228 * @_data: Port to which ATAPI device is attached.
4229 *
4230 * When device has indicated its readiness to accept
4231 * a CDB, this function is called. Send the CDB.
4232 * If DMA is to be performed, exit immediately.
4233 * Otherwise, we are in polling mode, so poll
4234 * status under operation succeeds or fails.
4235 *
4236 * LOCKING:
4237 * Kernel thread context (may sleep)
4238 */
4239
4240static void atapi_packet_task(void *_data)
4241{
4242 struct ata_port *ap = _data;
4243 struct ata_queued_cmd *qc;
4244 u8 status;
4245
4246 qc = ata_qc_from_tag(ap, ap->active_tag);
Tejun Heoa4631472006-02-11 19:11:13 +09004247 WARN_ON(qc == NULL);
4248 WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249
4250 /* sleep-wait for BSY to clear */
4251 DPRINTK("busy wait\n");
Albert Leed8fe4522005-12-05 15:42:17 +08004252 if (ata_busy_sleep(ap, ATA_TMOUT_CDB_QUICK, ATA_TMOUT_CDB)) {
Tejun Heo11a56d22006-01-23 13:09:36 +09004253 qc->err_mask |= AC_ERR_TIMEOUT;
Albert Leed8fe4522005-12-05 15:42:17 +08004254 goto err_out;
4255 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256
4257 /* make sure DRQ is set */
4258 status = ata_chk_status(ap);
Albert Leed8fe4522005-12-05 15:42:17 +08004259 if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ) {
Tejun Heo11a56d22006-01-23 13:09:36 +09004260 qc->err_mask |= AC_ERR_HSM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004261 goto err_out;
Albert Leed8fe4522005-12-05 15:42:17 +08004262 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263
4264 /* send SCSI cdb */
4265 DPRINTK("send cdb\n");
Tejun Heo6e7846e2006-02-12 23:32:58 +09004266 WARN_ON(qc->dev->cdb_len < 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267
Tejun Heoc1389502005-08-22 14:59:24 +09004268 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA ||
4269 qc->tf.protocol == ATA_PROT_ATAPI_NODATA) {
4270 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271
Tejun Heoc1389502005-08-22 14:59:24 +09004272 /* Once we're done issuing command and kicking bmdma,
4273 * irq handler takes over. To not lose irq, we need
4274 * to clear NOINTR flag before sending cdb, but
4275 * interrupt handler shouldn't be invoked before we're
4276 * finished. Hence, the following locking.
4277 */
4278 spin_lock_irqsave(&ap->host_set->lock, flags);
4279 ap->flags &= ~ATA_FLAG_NOINTR;
Tejun Heo6e7846e2006-02-12 23:32:58 +09004280 ata_data_xfer(ap, qc->cdb, qc->dev->cdb_len, 1);
Tejun Heoc1389502005-08-22 14:59:24 +09004281 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA)
4282 ap->ops->bmdma_start(qc); /* initiate bmdma */
4283 spin_unlock_irqrestore(&ap->host_set->lock, flags);
4284 } else {
Tejun Heo6e7846e2006-02-12 23:32:58 +09004285 ata_data_xfer(ap, qc->cdb, qc->dev->cdb_len, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286
Tejun Heoc1389502005-08-22 14:59:24 +09004287 /* PIO commands are handled by polling */
Albert Lee14be71f2005-09-27 17:36:35 +08004288 ap->hsm_task_state = HSM_ST;
Tejun Heo95064372006-01-23 13:09:37 +09004289 ata_queue_pio_task(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290 }
4291
4292 return;
4293
4294err_out:
Albert Leea22e2eb2005-12-05 15:38:02 +08004295 ata_poll_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004296}
4297
Edward Falk0baab862005-06-02 18:17:13 -04004298
Jens Axboe9b847542006-01-06 09:28:07 +01004299/*
4300 * Execute a 'simple' command, that only consists of the opcode 'cmd' itself,
4301 * without filling any other registers
4302 */
4303static int ata_do_simple_cmd(struct ata_port *ap, struct ata_device *dev,
4304 u8 cmd)
4305{
4306 struct ata_taskfile tf;
4307 int err;
4308
4309 ata_tf_init(ap, &tf, dev->devno);
4310
4311 tf.command = cmd;
4312 tf.flags |= ATA_TFLAG_DEVICE;
4313 tf.protocol = ATA_PROT_NODATA;
4314
4315 err = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
4316 if (err)
4317 printk(KERN_ERR "%s: ata command failed: %d\n",
4318 __FUNCTION__, err);
4319
4320 return err;
4321}
4322
4323static int ata_flush_cache(struct ata_port *ap, struct ata_device *dev)
4324{
4325 u8 cmd;
4326
4327 if (!ata_try_flush_cache(dev))
4328 return 0;
4329
4330 if (ata_id_has_flush_ext(dev->id))
4331 cmd = ATA_CMD_FLUSH_EXT;
4332 else
4333 cmd = ATA_CMD_FLUSH;
4334
4335 return ata_do_simple_cmd(ap, dev, cmd);
4336}
4337
4338static int ata_standby_drive(struct ata_port *ap, struct ata_device *dev)
4339{
4340 return ata_do_simple_cmd(ap, dev, ATA_CMD_STANDBYNOW1);
4341}
4342
4343static int ata_start_drive(struct ata_port *ap, struct ata_device *dev)
4344{
4345 return ata_do_simple_cmd(ap, dev, ATA_CMD_IDLEIMMEDIATE);
4346}
4347
4348/**
4349 * ata_device_resume - wakeup a previously suspended devices
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004350 * @ap: port the device is connected to
4351 * @dev: the device to resume
Jens Axboe9b847542006-01-06 09:28:07 +01004352 *
4353 * Kick the drive back into action, by sending it an idle immediate
4354 * command and making sure its transfer mode matches between drive
4355 * and host.
4356 *
4357 */
4358int ata_device_resume(struct ata_port *ap, struct ata_device *dev)
4359{
4360 if (ap->flags & ATA_FLAG_SUSPENDED) {
4361 ap->flags &= ~ATA_FLAG_SUSPENDED;
4362 ata_set_mode(ap);
4363 }
4364 if (!ata_dev_present(dev))
4365 return 0;
4366 if (dev->class == ATA_DEV_ATA)
4367 ata_start_drive(ap, dev);
4368
4369 return 0;
4370}
4371
4372/**
4373 * ata_device_suspend - prepare a device for suspend
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004374 * @ap: port the device is connected to
4375 * @dev: the device to suspend
Jens Axboe9b847542006-01-06 09:28:07 +01004376 *
4377 * Flush the cache on the drive, if appropriate, then issue a
4378 * standbynow command.
Jens Axboe9b847542006-01-06 09:28:07 +01004379 */
4380int ata_device_suspend(struct ata_port *ap, struct ata_device *dev)
4381{
4382 if (!ata_dev_present(dev))
4383 return 0;
4384 if (dev->class == ATA_DEV_ATA)
4385 ata_flush_cache(ap, dev);
4386
4387 ata_standby_drive(ap, dev);
4388 ap->flags |= ATA_FLAG_SUSPENDED;
4389 return 0;
4390}
4391
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004392/**
4393 * ata_port_start - Set port up for dma.
4394 * @ap: Port to initialize
4395 *
4396 * Called just after data structures for each port are
4397 * initialized. Allocates space for PRD table.
4398 *
4399 * May be used as the port_start() entry in ata_port_operations.
4400 *
4401 * LOCKING:
4402 * Inherited from caller.
4403 */
4404
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405int ata_port_start (struct ata_port *ap)
4406{
4407 struct device *dev = ap->host_set->dev;
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004408 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004409
4410 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
4411 if (!ap->prd)
4412 return -ENOMEM;
4413
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004414 rc = ata_pad_alloc(ap, dev);
4415 if (rc) {
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004416 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004417 return rc;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004418 }
4419
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
4421
4422 return 0;
4423}
4424
Edward Falk0baab862005-06-02 18:17:13 -04004425
4426/**
4427 * ata_port_stop - Undo ata_port_start()
4428 * @ap: Port to shut down
4429 *
4430 * Frees the PRD table.
4431 *
4432 * May be used as the port_stop() entry in ata_port_operations.
4433 *
4434 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004435 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04004436 */
4437
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438void ata_port_stop (struct ata_port *ap)
4439{
4440 struct device *dev = ap->host_set->dev;
4441
4442 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004443 ata_pad_free(ap, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444}
4445
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04004446void ata_host_stop (struct ata_host_set *host_set)
4447{
4448 if (host_set->mmio_base)
4449 iounmap(host_set->mmio_base);
4450}
4451
4452
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453/**
4454 * ata_host_remove - Unregister SCSI host structure with upper layers
4455 * @ap: Port to unregister
4456 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
4457 *
4458 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004459 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460 */
4461
4462static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
4463{
4464 struct Scsi_Host *sh = ap->host;
4465
4466 DPRINTK("ENTER\n");
4467
4468 if (do_unregister)
4469 scsi_remove_host(sh);
4470
4471 ap->ops->port_stop(ap);
4472}
4473
4474/**
4475 * ata_host_init - Initialize an ata_port structure
4476 * @ap: Structure to initialize
4477 * @host: associated SCSI mid-layer structure
4478 * @host_set: Collection of hosts to which @ap belongs
4479 * @ent: Probe information provided by low-level driver
4480 * @port_no: Port number associated with this ata_port
4481 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004482 * Initialize a new ata_port structure, and its associated
4483 * scsi_host.
4484 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004486 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487 */
4488
4489static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
4490 struct ata_host_set *host_set,
Jeff Garzik057ace52005-10-22 14:27:05 -04004491 const struct ata_probe_ent *ent, unsigned int port_no)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492{
4493 unsigned int i;
4494
4495 host->max_id = 16;
4496 host->max_lun = 1;
4497 host->max_channel = 1;
4498 host->unique_id = ata_unique_id++;
4499 host->max_cmd_len = 12;
Christoph Hellwig12413192005-06-11 01:05:01 +02004500
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501 ap->flags = ATA_FLAG_PORT_DISABLED;
4502 ap->id = host->unique_id;
4503 ap->host = host;
4504 ap->ctl = ATA_DEVCTL_OBS;
4505 ap->host_set = host_set;
4506 ap->port_no = port_no;
4507 ap->hard_port_no =
4508 ent->legacy_mode ? ent->hard_port_no : port_no;
4509 ap->pio_mask = ent->pio_mask;
4510 ap->mwdma_mask = ent->mwdma_mask;
4511 ap->udma_mask = ent->udma_mask;
4512 ap->flags |= ent->host_flags;
4513 ap->ops = ent->port_ops;
4514 ap->cbl = ATA_CBL_NONE;
4515 ap->active_tag = ATA_TAG_POISON;
4516 ap->last_ctl = 0xFF;
4517
4518 INIT_WORK(&ap->packet_task, atapi_packet_task, ap);
4519 INIT_WORK(&ap->pio_task, ata_pio_task, ap);
Tejun Heoa72ec4c2006-01-23 13:09:37 +09004520 INIT_LIST_HEAD(&ap->eh_done_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521
4522 for (i = 0; i < ATA_MAX_DEVICES; i++)
4523 ap->device[i].devno = i;
4524
4525#ifdef ATA_IRQ_TRAP
4526 ap->stats.unhandled_irq = 1;
4527 ap->stats.idle_irq = 1;
4528#endif
4529
4530 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
4531}
4532
4533/**
4534 * ata_host_add - Attach low-level ATA driver to system
4535 * @ent: Information provided by low-level driver
4536 * @host_set: Collections of ports to which we add
4537 * @port_no: Port number associated with this host
4538 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004539 * Attach low-level ATA driver to system.
4540 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004542 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543 *
4544 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004545 * New ata_port on success, for NULL on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546 */
4547
Jeff Garzik057ace52005-10-22 14:27:05 -04004548static struct ata_port * ata_host_add(const struct ata_probe_ent *ent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549 struct ata_host_set *host_set,
4550 unsigned int port_no)
4551{
4552 struct Scsi_Host *host;
4553 struct ata_port *ap;
4554 int rc;
4555
4556 DPRINTK("ENTER\n");
4557 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
4558 if (!host)
4559 return NULL;
4560
4561 ap = (struct ata_port *) &host->hostdata[0];
4562
4563 ata_host_init(ap, host, host_set, ent, port_no);
4564
4565 rc = ap->ops->port_start(ap);
4566 if (rc)
4567 goto err_out;
4568
4569 return ap;
4570
4571err_out:
4572 scsi_host_put(host);
4573 return NULL;
4574}
4575
4576/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004577 * ata_device_add - Register hardware device with ATA and SCSI layers
4578 * @ent: Probe information describing hardware device to be registered
4579 *
4580 * This function processes the information provided in the probe
4581 * information struct @ent, allocates the necessary ATA and SCSI
4582 * host information structures, initializes them, and registers
4583 * everything with requisite kernel subsystems.
4584 *
4585 * This function requests irqs, probes the ATA bus, and probes
4586 * the SCSI bus.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587 *
4588 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004589 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590 *
4591 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004592 * Number of ports registered. Zero on error (no ports registered).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593 */
4594
Jeff Garzik057ace52005-10-22 14:27:05 -04004595int ata_device_add(const struct ata_probe_ent *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596{
4597 unsigned int count = 0, i;
4598 struct device *dev = ent->dev;
4599 struct ata_host_set *host_set;
4600
4601 DPRINTK("ENTER\n");
4602 /* alloc a container for our list of ATA ports (buses) */
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004603 host_set = kzalloc(sizeof(struct ata_host_set) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
4605 if (!host_set)
4606 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607 spin_lock_init(&host_set->lock);
4608
4609 host_set->dev = dev;
4610 host_set->n_ports = ent->n_ports;
4611 host_set->irq = ent->irq;
4612 host_set->mmio_base = ent->mmio_base;
4613 host_set->private_data = ent->private_data;
4614 host_set->ops = ent->port_ops;
4615
4616 /* register each port bound to this device */
4617 for (i = 0; i < ent->n_ports; i++) {
4618 struct ata_port *ap;
4619 unsigned long xfer_mode_mask;
4620
4621 ap = ata_host_add(ent, host_set, i);
4622 if (!ap)
4623 goto err_out;
4624
4625 host_set->ports[i] = ap;
4626 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
4627 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
4628 (ap->pio_mask << ATA_SHIFT_PIO);
4629
4630 /* print per-port info to dmesg */
4631 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
4632 "bmdma 0x%lX irq %lu\n",
4633 ap->id,
4634 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
4635 ata_mode_string(xfer_mode_mask),
4636 ap->ioaddr.cmd_addr,
4637 ap->ioaddr.ctl_addr,
4638 ap->ioaddr.bmdma_addr,
4639 ent->irq);
4640
4641 ata_chk_status(ap);
4642 host_set->ops->irq_clear(ap);
4643 count++;
4644 }
4645
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004646 if (!count)
4647 goto err_free_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648
4649 /* obtain irq, that is shared between channels */
4650 if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
4651 DRV_NAME, host_set))
4652 goto err_out;
4653
4654 /* perform each probe synchronously */
4655 DPRINTK("probe begin\n");
4656 for (i = 0; i < count; i++) {
4657 struct ata_port *ap;
4658 int rc;
4659
4660 ap = host_set->ports[i];
4661
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004662 DPRINTK("ata%u: bus probe begin\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663 rc = ata_bus_probe(ap);
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004664 DPRINTK("ata%u: bus probe end\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665
4666 if (rc) {
4667 /* FIXME: do something useful here?
4668 * Current libata behavior will
4669 * tear down everything when
4670 * the module is removed
4671 * or the h/w is unplugged.
4672 */
4673 }
4674
4675 rc = scsi_add_host(ap->host, dev);
4676 if (rc) {
4677 printk(KERN_ERR "ata%u: scsi_add_host failed\n",
4678 ap->id);
4679 /* FIXME: do something useful here */
4680 /* FIXME: handle unconditional calls to
4681 * scsi_scan_host and ata_host_remove, below,
4682 * at the very least
4683 */
4684 }
4685 }
4686
4687 /* probes are done, now scan each port's disk(s) */
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004688 DPRINTK("host probe begin\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689 for (i = 0; i < count; i++) {
4690 struct ata_port *ap = host_set->ports[i];
4691
Jeff Garzik644dd0c2005-10-03 15:55:19 -04004692 ata_scsi_scan_host(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693 }
4694
4695 dev_set_drvdata(dev, host_set);
4696
4697 VPRINTK("EXIT, returning %u\n", ent->n_ports);
4698 return ent->n_ports; /* success */
4699
4700err_out:
4701 for (i = 0; i < count; i++) {
4702 ata_host_remove(host_set->ports[i], 1);
4703 scsi_host_put(host_set->ports[i]->host);
4704 }
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004705err_free_ret:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706 kfree(host_set);
4707 VPRINTK("EXIT, returning 0\n");
4708 return 0;
4709}
4710
4711/**
Alan Cox17b14452005-09-15 15:44:00 +01004712 * ata_host_set_remove - PCI layer callback for device removal
4713 * @host_set: ATA host set that was removed
4714 *
4715 * Unregister all objects associated with this host set. Free those
4716 * objects.
4717 *
4718 * LOCKING:
4719 * Inherited from calling layer (may sleep).
4720 */
4721
Alan Cox17b14452005-09-15 15:44:00 +01004722void ata_host_set_remove(struct ata_host_set *host_set)
4723{
4724 struct ata_port *ap;
4725 unsigned int i;
4726
4727 for (i = 0; i < host_set->n_ports; i++) {
4728 ap = host_set->ports[i];
4729 scsi_remove_host(ap->host);
4730 }
4731
4732 free_irq(host_set->irq, host_set);
4733
4734 for (i = 0; i < host_set->n_ports; i++) {
4735 ap = host_set->ports[i];
4736
4737 ata_scsi_release(ap->host);
4738
4739 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
4740 struct ata_ioports *ioaddr = &ap->ioaddr;
4741
4742 if (ioaddr->cmd_addr == 0x1f0)
4743 release_region(0x1f0, 8);
4744 else if (ioaddr->cmd_addr == 0x170)
4745 release_region(0x170, 8);
4746 }
4747
4748 scsi_host_put(ap->host);
4749 }
4750
4751 if (host_set->ops->host_stop)
4752 host_set->ops->host_stop(host_set);
4753
4754 kfree(host_set);
4755}
4756
4757/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004758 * ata_scsi_release - SCSI layer callback hook for host unload
4759 * @host: libata host to be unloaded
4760 *
4761 * Performs all duties necessary to shut down a libata port...
4762 * Kill port kthread, disable port, and release resources.
4763 *
4764 * LOCKING:
4765 * Inherited from SCSI layer.
4766 *
4767 * RETURNS:
4768 * One.
4769 */
4770
4771int ata_scsi_release(struct Scsi_Host *host)
4772{
4773 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
4774
4775 DPRINTK("ENTER\n");
4776
4777 ap->ops->port_disable(ap);
4778 ata_host_remove(ap, 0);
4779
4780 DPRINTK("EXIT\n");
4781 return 1;
4782}
4783
4784/**
4785 * ata_std_ports - initialize ioaddr with standard port offsets.
4786 * @ioaddr: IO address structure to be initialized
Edward Falk0baab862005-06-02 18:17:13 -04004787 *
4788 * Utility function which initializes data_addr, error_addr,
4789 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
4790 * device_addr, status_addr, and command_addr to standard offsets
4791 * relative to cmd_addr.
4792 *
4793 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794 */
Edward Falk0baab862005-06-02 18:17:13 -04004795
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796void ata_std_ports(struct ata_ioports *ioaddr)
4797{
4798 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
4799 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
4800 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
4801 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
4802 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
4803 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
4804 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
4805 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
4806 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
4807 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
4808}
4809
Edward Falk0baab862005-06-02 18:17:13 -04004810
Jeff Garzik374b1872005-08-30 05:42:52 -04004811#ifdef CONFIG_PCI
4812
4813void ata_pci_host_stop (struct ata_host_set *host_set)
4814{
4815 struct pci_dev *pdev = to_pci_dev(host_set->dev);
4816
4817 pci_iounmap(pdev, host_set->mmio_base);
4818}
4819
Edward Falk0baab862005-06-02 18:17:13 -04004820/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821 * ata_pci_remove_one - PCI layer callback for device removal
4822 * @pdev: PCI device that was removed
4823 *
4824 * PCI layer indicates to libata via this hook that
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004825 * hot-unplug or module unload event has occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826 * Handle this by unregistering all objects associated
4827 * with this PCI device. Free those objects. Then finally
4828 * release PCI resources and disable device.
4829 *
4830 * LOCKING:
4831 * Inherited from PCI layer (may sleep).
4832 */
4833
4834void ata_pci_remove_one (struct pci_dev *pdev)
4835{
4836 struct device *dev = pci_dev_to_dev(pdev);
4837 struct ata_host_set *host_set = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838
Alan Cox17b14452005-09-15 15:44:00 +01004839 ata_host_set_remove(host_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840 pci_release_regions(pdev);
4841 pci_disable_device(pdev);
4842 dev_set_drvdata(dev, NULL);
4843}
4844
4845/* move to PCI subsystem */
Jeff Garzik057ace52005-10-22 14:27:05 -04004846int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847{
4848 unsigned long tmp = 0;
4849
4850 switch (bits->width) {
4851 case 1: {
4852 u8 tmp8 = 0;
4853 pci_read_config_byte(pdev, bits->reg, &tmp8);
4854 tmp = tmp8;
4855 break;
4856 }
4857 case 2: {
4858 u16 tmp16 = 0;
4859 pci_read_config_word(pdev, bits->reg, &tmp16);
4860 tmp = tmp16;
4861 break;
4862 }
4863 case 4: {
4864 u32 tmp32 = 0;
4865 pci_read_config_dword(pdev, bits->reg, &tmp32);
4866 tmp = tmp32;
4867 break;
4868 }
4869
4870 default:
4871 return -EINVAL;
4872 }
4873
4874 tmp &= bits->mask;
4875
4876 return (tmp == bits->val) ? 1 : 0;
4877}
Jens Axboe9b847542006-01-06 09:28:07 +01004878
4879int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t state)
4880{
4881 pci_save_state(pdev);
4882 pci_disable_device(pdev);
4883 pci_set_power_state(pdev, PCI_D3hot);
4884 return 0;
4885}
4886
4887int ata_pci_device_resume(struct pci_dev *pdev)
4888{
4889 pci_set_power_state(pdev, PCI_D0);
4890 pci_restore_state(pdev);
4891 pci_enable_device(pdev);
4892 pci_set_master(pdev);
4893 return 0;
4894}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895#endif /* CONFIG_PCI */
4896
4897
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898static int __init ata_init(void)
4899{
4900 ata_wq = create_workqueue("ata");
4901 if (!ata_wq)
4902 return -ENOMEM;
4903
4904 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
4905 return 0;
4906}
4907
4908static void __exit ata_exit(void)
4909{
4910 destroy_workqueue(ata_wq);
4911}
4912
4913module_init(ata_init);
4914module_exit(ata_exit);
4915
Jeff Garzik67846b32005-10-05 02:58:32 -04004916static unsigned long ratelimit_time;
4917static spinlock_t ata_ratelimit_lock = SPIN_LOCK_UNLOCKED;
4918
4919int ata_ratelimit(void)
4920{
4921 int rc;
4922 unsigned long flags;
4923
4924 spin_lock_irqsave(&ata_ratelimit_lock, flags);
4925
4926 if (time_after(jiffies, ratelimit_time)) {
4927 rc = 1;
4928 ratelimit_time = jiffies + (HZ/5);
4929 } else
4930 rc = 0;
4931
4932 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
4933
4934 return rc;
4935}
4936
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937/*
4938 * libata is essentially a library of internal helper functions for
4939 * low-level ATA host controller drivers. As such, the API/ABI is
4940 * likely to change as new drivers are added and updated.
4941 * Do not depend on ABI/API stability.
4942 */
4943
4944EXPORT_SYMBOL_GPL(ata_std_bios_param);
4945EXPORT_SYMBOL_GPL(ata_std_ports);
4946EXPORT_SYMBOL_GPL(ata_device_add);
Alan Cox17b14452005-09-15 15:44:00 +01004947EXPORT_SYMBOL_GPL(ata_host_set_remove);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004948EXPORT_SYMBOL_GPL(ata_sg_init);
4949EXPORT_SYMBOL_GPL(ata_sg_init_one);
Tejun Heo76014422006-02-11 15:13:49 +09004950EXPORT_SYMBOL_GPL(__ata_qc_complete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004951EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
4952EXPORT_SYMBOL_GPL(ata_eng_timeout);
4953EXPORT_SYMBOL_GPL(ata_tf_load);
4954EXPORT_SYMBOL_GPL(ata_tf_read);
4955EXPORT_SYMBOL_GPL(ata_noop_dev_select);
4956EXPORT_SYMBOL_GPL(ata_std_dev_select);
4957EXPORT_SYMBOL_GPL(ata_tf_to_fis);
4958EXPORT_SYMBOL_GPL(ata_tf_from_fis);
4959EXPORT_SYMBOL_GPL(ata_check_status);
4960EXPORT_SYMBOL_GPL(ata_altstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961EXPORT_SYMBOL_GPL(ata_exec_command);
4962EXPORT_SYMBOL_GPL(ata_port_start);
4963EXPORT_SYMBOL_GPL(ata_port_stop);
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04004964EXPORT_SYMBOL_GPL(ata_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965EXPORT_SYMBOL_GPL(ata_interrupt);
4966EXPORT_SYMBOL_GPL(ata_qc_prep);
4967EXPORT_SYMBOL_GPL(ata_bmdma_setup);
4968EXPORT_SYMBOL_GPL(ata_bmdma_start);
4969EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
4970EXPORT_SYMBOL_GPL(ata_bmdma_status);
4971EXPORT_SYMBOL_GPL(ata_bmdma_stop);
4972EXPORT_SYMBOL_GPL(ata_port_probe);
4973EXPORT_SYMBOL_GPL(sata_phy_reset);
4974EXPORT_SYMBOL_GPL(__sata_phy_reset);
4975EXPORT_SYMBOL_GPL(ata_bus_reset);
Tejun Heo8a19ac82006-02-02 18:20:00 +09004976EXPORT_SYMBOL_GPL(ata_std_probeinit);
Tejun Heoc2bd5802006-01-24 17:05:22 +09004977EXPORT_SYMBOL_GPL(ata_std_softreset);
4978EXPORT_SYMBOL_GPL(sata_std_hardreset);
4979EXPORT_SYMBOL_GPL(ata_std_postreset);
4980EXPORT_SYMBOL_GPL(ata_std_probe_reset);
Tejun Heoa62c0fc2006-01-24 17:05:22 +09004981EXPORT_SYMBOL_GPL(ata_drive_probe_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982EXPORT_SYMBOL_GPL(ata_port_disable);
Jeff Garzik67846b32005-10-05 02:58:32 -04004983EXPORT_SYMBOL_GPL(ata_ratelimit);
Tejun Heo6f8b9952006-01-24 17:05:21 +09004984EXPORT_SYMBOL_GPL(ata_busy_sleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004985EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
4986EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
Tejun Heof29841e2006-02-10 15:10:48 +09004987EXPORT_SYMBOL_GPL(ata_scsi_timed_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004988EXPORT_SYMBOL_GPL(ata_scsi_error);
4989EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
4990EXPORT_SYMBOL_GPL(ata_scsi_release);
4991EXPORT_SYMBOL_GPL(ata_host_intr);
4992EXPORT_SYMBOL_GPL(ata_dev_classify);
Tejun Heo6a62a042006-02-13 10:02:46 +09004993EXPORT_SYMBOL_GPL(ata_id_string);
4994EXPORT_SYMBOL_GPL(ata_id_c_string);
Brad Campbell6f2f3812005-05-12 15:07:47 -04004995EXPORT_SYMBOL_GPL(ata_dev_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996EXPORT_SYMBOL_GPL(ata_scsi_simulate);
Tejun Heoa72ec4c2006-01-23 13:09:37 +09004997EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
4998EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999
Alan Cox1bc4ccf2006-01-09 17:18:14 +00005000EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
Alan Cox452503f2005-10-21 19:01:32 -04005001EXPORT_SYMBOL_GPL(ata_timing_compute);
5002EXPORT_SYMBOL_GPL(ata_timing_merge);
5003
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004#ifdef CONFIG_PCI
5005EXPORT_SYMBOL_GPL(pci_test_config_bits);
Jeff Garzik374b1872005-08-30 05:42:52 -04005006EXPORT_SYMBOL_GPL(ata_pci_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
5008EXPORT_SYMBOL_GPL(ata_pci_init_one);
5009EXPORT_SYMBOL_GPL(ata_pci_remove_one);
Jens Axboe9b847542006-01-06 09:28:07 +01005010EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
5011EXPORT_SYMBOL_GPL(ata_pci_device_resume);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012#endif /* CONFIG_PCI */
Jens Axboe9b847542006-01-06 09:28:07 +01005013
5014EXPORT_SYMBOL_GPL(ata_device_suspend);
5015EXPORT_SYMBOL_GPL(ata_device_resume);
5016EXPORT_SYMBOL_GPL(ata_scsi_device_suspend);
5017EXPORT_SYMBOL_GPL(ata_scsi_device_resume);