blob: 62340f4ef004463c7de20656f37fb3d483121207 [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/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 * ata_dev_identify - obtain IDENTIFY x DEVICE page
907 * @ap: port on which device we wish to probe resides
908 * @device: device bus address, starting at zero
909 *
910 * Following bus reset, we issue the IDENTIFY [PACKET] DEVICE
911 * command, and read back the 512-byte device information page.
912 * The device information page is fed to us via the standard
913 * PIO-IN protocol, but we hand-code it here. (TODO: investigate
914 * using standard PIO-IN paths)
915 *
916 * After reading the device information page, we use several
917 * bits of information from it to initialize data structures
918 * that will be used during the lifetime of the ata_device.
919 * Other data from the info page is used to disqualify certain
920 * older ATA devices we do not wish to support.
921 *
922 * LOCKING:
923 * Inherited from caller. Some functions called by this function
924 * obtain the host_set lock.
925 */
926
927static void ata_dev_identify(struct ata_port *ap, unsigned int device)
928{
929 struct ata_device *dev = &ap->device[device];
Albert Lee8bf62ec2005-05-12 15:29:42 -0400930 unsigned int major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 unsigned long xfer_modes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 unsigned int using_edd;
Tejun Heoa0123702005-12-13 14:49:31 +0900933 struct ata_taskfile tf;
934 unsigned int err_mask;
Tejun Heo6e7846e2006-02-12 23:32:58 +0900935 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936
937 if (!ata_dev_present(dev)) {
938 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
939 ap->id, device);
940 return;
941 }
942
Tejun Heo61eb0662006-02-12 23:22:37 +0900943 if (ap->ops->probe_reset ||
944 ap->flags & (ATA_FLAG_SRST | ATA_FLAG_SATA_RESET))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 using_edd = 0;
946 else
947 using_edd = 1;
948
949 DPRINTK("ENTER, host %u, dev %u\n", ap->id, device);
950
Tejun Heoa4631472006-02-11 19:11:13 +0900951 WARN_ON(dev->class != ATA_DEV_ATA && dev->class != ATA_DEV_ATAPI &&
952 dev->class != ATA_DEV_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953
954 ata_dev_select(ap, device, 1, 1); /* select device 0/1 */
955
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956retry:
Tejun Heoa0123702005-12-13 14:49:31 +0900957 ata_tf_init(ap, &tf, device);
958
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 if (dev->class == ATA_DEV_ATA) {
Tejun Heoa0123702005-12-13 14:49:31 +0900960 tf.command = ATA_CMD_ID_ATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 DPRINTK("do ATA identify\n");
962 } else {
Tejun Heoa0123702005-12-13 14:49:31 +0900963 tf.command = ATA_CMD_ID_ATAPI;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 DPRINTK("do ATAPI identify\n");
965 }
966
Tejun Heoa0123702005-12-13 14:49:31 +0900967 tf.protocol = ATA_PROT_PIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
Tejun Heoa0123702005-12-13 14:49:31 +0900969 err_mask = ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE,
970 dev->id, sizeof(dev->id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
Tejun Heoa0123702005-12-13 14:49:31 +0900972 if (err_mask) {
973 if (err_mask & ~AC_ERR_DEV)
974 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 /*
977 * arg! EDD works for all test cases, but seems to return
978 * the ATA signature for some ATAPI devices. Until the
979 * reason for this is found and fixed, we fix up the mess
980 * here. If IDENTIFY DEVICE returns command aborted
981 * (as ATAPI devices do), then we issue an
982 * IDENTIFY PACKET DEVICE.
983 *
984 * ATA software reset (SRST, the default) does not appear
985 * to have this problem.
986 */
Albert Lee7c398332005-11-09 13:03:30 +0800987 if ((using_edd) && (dev->class == ATA_DEV_ATA)) {
Tejun Heoa0123702005-12-13 14:49:31 +0900988 u8 err = tf.feature;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 if (err & ATA_ABORTED) {
990 dev->class = ATA_DEV_ATAPI;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 goto retry;
992 }
993 }
994 goto err_out;
995 }
996
997 swap_buf_le16(dev->id, ATA_ID_WORDS);
998
999 /* print device capabilities */
1000 printk(KERN_DEBUG "ata%u: dev %u cfg "
1001 "49:%04x 82:%04x 83:%04x 84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1002 ap->id, device, dev->id[49],
1003 dev->id[82], dev->id[83], dev->id[84],
1004 dev->id[85], dev->id[86], dev->id[87],
1005 dev->id[88]);
1006
1007 /*
1008 * common ATA, ATAPI feature tests
1009 */
1010
Albert Lee8bf62ec2005-05-12 15:29:42 -04001011 /* we require DMA support (bits 8 of word 49) */
1012 if (!ata_id_has_dma(dev->id)) {
1013 printk(KERN_DEBUG "ata%u: no dma\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 goto err_out_nosup;
1015 }
1016
1017 /* quick-n-dirty find max transfer mode; for printk only */
1018 xfer_modes = dev->id[ATA_ID_UDMA_MODES];
1019 if (!xfer_modes)
1020 xfer_modes = (dev->id[ATA_ID_MWDMA_MODES]) << ATA_SHIFT_MWDMA;
Alan Cox11e29e22005-10-21 18:46:32 -04001021 if (!xfer_modes)
1022 xfer_modes = ata_pio_modes(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
Tejun Heo0bd33002006-02-12 22:47:05 +09001024 ata_dump_id(dev->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
1026 /* ATA-specific feature tests */
1027 if (dev->class == ATA_DEV_ATA) {
Tejun Heo29407402006-02-12 22:47:04 +09001028 dev->n_sectors = ata_id_n_sectors(dev->id);
1029
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 if (!ata_id_is_ata(dev->id)) /* sanity check */
1031 goto err_out_nosup;
1032
Albert Lee8bf62ec2005-05-12 15:29:42 -04001033 /* get major version */
Tejun Heo3d2ca912006-02-12 22:47:04 +09001034 major_version = ata_id_major_version(dev->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
Albert Lee8bf62ec2005-05-12 15:29:42 -04001036 /*
1037 * The exact sequence expected by certain pre-ATA4 drives is:
1038 * SRST RESET
1039 * IDENTIFY
1040 * INITIALIZE DEVICE PARAMETERS
1041 * anything else..
1042 * Some drives were very specific about that exact sequence.
1043 */
Albert Lee59a10b12005-10-12 15:09:42 +08001044 if (major_version < 4 || (!ata_id_has_lba(dev->id))) {
Tejun Heo6aff8f12006-02-15 18:24:09 +09001045 err_mask = ata_dev_init_params(ap, dev);
1046 if (err_mask) {
1047 printk(KERN_ERR "ata%u: failed to init "
1048 "parameters, disabled\n", ap->id);
1049 goto err_out;
1050 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
Albert Lee59a10b12005-10-12 15:09:42 +08001052 /* current CHS translation info (id[53-58]) might be
1053 * changed. reread the identify device info.
1054 */
1055 ata_dev_reread_id(ap, dev);
1056 }
1057
Albert Lee8bf62ec2005-05-12 15:29:42 -04001058 if (ata_id_has_lba(dev->id)) {
1059 dev->flags |= ATA_DFLAG_LBA;
1060
Tejun Heo29407402006-02-12 22:47:04 +09001061 if (ata_id_has_lba48(dev->id))
Albert Lee8bf62ec2005-05-12 15:29:42 -04001062 dev->flags |= ATA_DFLAG_LBA48;
Albert Lee8bf62ec2005-05-12 15:29:42 -04001063
1064 /* print device info to dmesg */
1065 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors:%s\n",
1066 ap->id, device,
1067 major_version,
1068 ata_mode_string(xfer_modes),
1069 (unsigned long long)dev->n_sectors,
1070 dev->flags & ATA_DFLAG_LBA48 ? " LBA48" : " LBA");
1071 } else {
1072 /* CHS */
1073
1074 /* Default translation */
1075 dev->cylinders = dev->id[1];
1076 dev->heads = dev->id[3];
1077 dev->sectors = dev->id[6];
Albert Lee8bf62ec2005-05-12 15:29:42 -04001078
1079 if (ata_id_current_chs_valid(dev->id)) {
1080 /* Current CHS translation is valid. */
1081 dev->cylinders = dev->id[54];
1082 dev->heads = dev->id[55];
1083 dev->sectors = dev->id[56];
Albert Lee8bf62ec2005-05-12 15:29:42 -04001084 }
1085
1086 /* print device info to dmesg */
1087 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors: CHS %d/%d/%d\n",
1088 ap->id, device,
1089 major_version,
1090 ata_mode_string(xfer_modes),
1091 (unsigned long long)dev->n_sectors,
1092 (int)dev->cylinders, (int)dev->heads, (int)dev->sectors);
1093
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 }
1095
Tejun Heo6e7846e2006-02-12 23:32:58 +09001096 dev->cdb_len = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 }
1098
1099 /* ATAPI-specific feature tests */
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05001100 else if (dev->class == ATA_DEV_ATAPI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 if (ata_id_is_ata(dev->id)) /* sanity check */
1102 goto err_out_nosup;
1103
1104 rc = atapi_cdb_len(dev->id);
1105 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
1106 printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
1107 goto err_out_nosup;
1108 }
Tejun Heo6e7846e2006-02-12 23:32:58 +09001109 dev->cdb_len = (unsigned int) rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110
1111 /* print device info to dmesg */
1112 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
1113 ap->id, device,
1114 ata_mode_string(xfer_modes));
1115 }
1116
Tejun Heo6e7846e2006-02-12 23:32:58 +09001117 ap->host->max_cmd_len = 0;
1118 for (i = 0; i < ATA_MAX_DEVICES; i++)
1119 ap->host->max_cmd_len = max_t(unsigned int,
1120 ap->host->max_cmd_len,
1121 ap->device[i].cdb_len);
1122
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
1124 return;
1125
1126err_out_nosup:
1127 printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n",
1128 ap->id, device);
1129err_out:
1130 dev->class++; /* converts ATA_DEV_xxx into ATA_DEV_xxx_UNSUP */
1131 DPRINTK("EXIT, err\n");
1132}
1133
Brad Campbell6f2f3812005-05-12 15:07:47 -04001134
Tejun Heo8eabd022006-02-12 23:32:58 +09001135static inline u8 ata_dev_knobble(const struct ata_port *ap,
1136 struct ata_device *dev)
Brad Campbell6f2f3812005-05-12 15:07:47 -04001137{
Tejun Heo8eabd022006-02-12 23:32:58 +09001138 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
Brad Campbell6f2f3812005-05-12 15:07:47 -04001139}
1140
1141/**
Randy Dunlapc893a3a2006-01-28 13:15:32 -05001142 * ata_dev_config - Run device specific handlers & check for SATA->PATA bridges
1143 * @ap: Bus
1144 * @i: Device
Brad Campbell6f2f3812005-05-12 15:07:47 -04001145 *
Randy Dunlapc893a3a2006-01-28 13:15:32 -05001146 * LOCKING:
Brad Campbell6f2f3812005-05-12 15:07:47 -04001147 */
Jeff Garzik8a60a072005-07-31 13:13:24 -04001148
Brad Campbell6f2f3812005-05-12 15:07:47 -04001149void ata_dev_config(struct ata_port *ap, unsigned int i)
1150{
1151 /* limit bridge transfers to udma5, 200 sectors */
Tejun Heo8eabd022006-02-12 23:32:58 +09001152 if (ata_dev_knobble(ap, &ap->device[i])) {
Brad Campbell6f2f3812005-05-12 15:07:47 -04001153 printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
Tejun Heo8eabd022006-02-12 23:32:58 +09001154 ap->id, i);
Brad Campbell6f2f3812005-05-12 15:07:47 -04001155 ap->udma_mask &= ATA_UDMA5;
Tejun Heob00eec12006-02-12 23:32:59 +09001156 ap->device[i].max_sectors = ATA_MAX_SECTORS;
Brad Campbell6f2f3812005-05-12 15:07:47 -04001157 }
1158
1159 if (ap->ops->dev_config)
1160 ap->ops->dev_config(ap, &ap->device[i]);
1161}
1162
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163/**
1164 * ata_bus_probe - Reset and probe ATA bus
1165 * @ap: Bus to probe
1166 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001167 * Master ATA bus probing function. Initiates a hardware-dependent
1168 * bus reset, then attempts to identify any devices found on
1169 * the bus.
1170 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001172 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 *
1174 * RETURNS:
1175 * Zero on success, non-zero on error.
1176 */
1177
1178static int ata_bus_probe(struct ata_port *ap)
1179{
1180 unsigned int i, found = 0;
1181
Tejun Heoc19ba8a2006-01-24 17:05:22 +09001182 if (ap->ops->probe_reset) {
1183 unsigned int classes[ATA_MAX_DEVICES];
1184 int rc;
1185
1186 ata_port_probe(ap);
1187
1188 rc = ap->ops->probe_reset(ap, classes);
1189 if (rc == 0) {
Tejun Heo06ab7822006-02-12 15:01:49 +09001190 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1191 if (classes[i] == ATA_DEV_UNKNOWN)
1192 classes[i] = ATA_DEV_NONE;
Tejun Heoc19ba8a2006-01-24 17:05:22 +09001193 ap->device[i].class = classes[i];
Tejun Heo06ab7822006-02-12 15:01:49 +09001194 }
Tejun Heoc19ba8a2006-01-24 17:05:22 +09001195 } else {
1196 printk(KERN_ERR "ata%u: probe reset failed, "
1197 "disabling port\n", ap->id);
1198 ata_port_disable(ap);
1199 }
1200 } else
1201 ap->ops->phy_reset(ap);
1202
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1204 goto err_out;
1205
1206 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1207 ata_dev_identify(ap, i);
1208 if (ata_dev_present(&ap->device[i])) {
1209 found = 1;
Brad Campbell6f2f3812005-05-12 15:07:47 -04001210 ata_dev_config(ap,i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 }
1212 }
1213
1214 if ((!found) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1215 goto err_out_disable;
1216
1217 ata_set_mode(ap);
1218 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1219 goto err_out_disable;
1220
1221 return 0;
1222
1223err_out_disable:
1224 ap->ops->port_disable(ap);
1225err_out:
1226 return -1;
1227}
1228
1229/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001230 * ata_port_probe - Mark port as enabled
1231 * @ap: Port for which we indicate enablement
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001233 * Modify @ap data structure such that the system
1234 * thinks that the entire port is enabled.
1235 *
1236 * LOCKING: host_set lock, or some other form of
1237 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 */
1239
1240void ata_port_probe(struct ata_port *ap)
1241{
1242 ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1243}
1244
1245/**
Tejun Heo3be680b2005-12-19 22:35:02 +09001246 * sata_print_link_status - Print SATA link status
1247 * @ap: SATA port to printk link status about
1248 *
1249 * This function prints link speed and status of a SATA link.
1250 *
1251 * LOCKING:
1252 * None.
1253 */
1254static void sata_print_link_status(struct ata_port *ap)
1255{
1256 u32 sstatus, tmp;
1257 const char *speed;
1258
1259 if (!ap->ops->scr_read)
1260 return;
1261
1262 sstatus = scr_read(ap, SCR_STATUS);
1263
1264 if (sata_dev_present(ap)) {
1265 tmp = (sstatus >> 4) & 0xf;
1266 if (tmp & (1 << 0))
1267 speed = "1.5";
1268 else if (tmp & (1 << 1))
1269 speed = "3.0";
1270 else
1271 speed = "<unknown>";
1272 printk(KERN_INFO "ata%u: SATA link up %s Gbps (SStatus %X)\n",
1273 ap->id, speed, sstatus);
1274 } else {
1275 printk(KERN_INFO "ata%u: SATA link down (SStatus %X)\n",
1276 ap->id, sstatus);
1277 }
1278}
1279
1280/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001281 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1282 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001284 * This function issues commands to standard SATA Sxxx
1285 * PHY registers, to wake up the phy (and device), and
1286 * clear any reset condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 *
1288 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001289 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 *
1291 */
1292void __sata_phy_reset(struct ata_port *ap)
1293{
1294 u32 sstatus;
1295 unsigned long timeout = jiffies + (HZ * 5);
1296
1297 if (ap->flags & ATA_FLAG_SATA_RESET) {
Brett Russcdcca892005-03-28 15:10:27 -05001298 /* issue phy wake/reset */
1299 scr_write_flush(ap, SCR_CONTROL, 0x301);
Tejun Heo62ba2842005-06-26 23:27:19 +09001300 /* Couldn't find anything in SATA I/II specs, but
1301 * AHCI-1.1 10.4.2 says at least 1 ms. */
1302 mdelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 }
Brett Russcdcca892005-03-28 15:10:27 -05001304 scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305
1306 /* wait for phy to become ready, if necessary */
1307 do {
1308 msleep(200);
1309 sstatus = scr_read(ap, SCR_STATUS);
1310 if ((sstatus & 0xf) != 1)
1311 break;
1312 } while (time_before(jiffies, timeout));
1313
Tejun Heo3be680b2005-12-19 22:35:02 +09001314 /* print link status */
1315 sata_print_link_status(ap);
Jeff Garzik656563e2005-11-20 03:36:45 -05001316
Tejun Heo3be680b2005-12-19 22:35:02 +09001317 /* TODO: phy layer with polling, timeouts, etc. */
1318 if (sata_dev_present(ap))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 ata_port_probe(ap);
Tejun Heo3be680b2005-12-19 22:35:02 +09001320 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 ata_port_disable(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322
1323 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1324 return;
1325
1326 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1327 ata_port_disable(ap);
1328 return;
1329 }
1330
1331 ap->cbl = ATA_CBL_SATA;
1332}
1333
1334/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001335 * sata_phy_reset - Reset SATA bus.
1336 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001338 * This function resets the SATA bus, and then probes
1339 * the bus for devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 *
1341 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001342 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 *
1344 */
1345void sata_phy_reset(struct ata_port *ap)
1346{
1347 __sata_phy_reset(ap);
1348 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1349 return;
1350 ata_bus_reset(ap);
1351}
1352
1353/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001354 * ata_port_disable - Disable port.
1355 * @ap: Port to be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001357 * Modify @ap data structure such that the system
1358 * thinks that the entire port is disabled, and should
1359 * never attempt to probe or communicate with devices
1360 * on this port.
1361 *
1362 * LOCKING: host_set lock, or some other form of
1363 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 */
1365
1366void ata_port_disable(struct ata_port *ap)
1367{
1368 ap->device[0].class = ATA_DEV_NONE;
1369 ap->device[1].class = ATA_DEV_NONE;
1370 ap->flags |= ATA_FLAG_PORT_DISABLED;
1371}
1372
Alan Cox452503f2005-10-21 19:01:32 -04001373/*
1374 * This mode timing computation functionality is ported over from
1375 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
1376 */
1377/*
1378 * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
1379 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
1380 * for PIO 5, which is a nonstandard extension and UDMA6, which
1381 * is currently supported only by Maxtor drives.
1382 */
1383
1384static const struct ata_timing ata_timing[] = {
1385
1386 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
1387 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
1388 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
1389 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
1390
1391 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
1392 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
1393 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
1394
1395/* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
1396
1397 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
1398 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
1399 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
1400
1401 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
1402 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
1403 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
1404
1405/* { XFER_PIO_5, 20, 50, 30, 100, 50, 30, 100, 0 }, */
1406 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
1407 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
1408
1409 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
1410 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
1411 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
1412
1413/* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
1414
1415 { 0xFF }
1416};
1417
1418#define ENOUGH(v,unit) (((v)-1)/(unit)+1)
1419#define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
1420
1421static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
1422{
1423 q->setup = EZ(t->setup * 1000, T);
1424 q->act8b = EZ(t->act8b * 1000, T);
1425 q->rec8b = EZ(t->rec8b * 1000, T);
1426 q->cyc8b = EZ(t->cyc8b * 1000, T);
1427 q->active = EZ(t->active * 1000, T);
1428 q->recover = EZ(t->recover * 1000, T);
1429 q->cycle = EZ(t->cycle * 1000, T);
1430 q->udma = EZ(t->udma * 1000, UT);
1431}
1432
1433void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
1434 struct ata_timing *m, unsigned int what)
1435{
1436 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
1437 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
1438 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
1439 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
1440 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
1441 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
1442 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
1443 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
1444}
1445
1446static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
1447{
1448 const struct ata_timing *t;
1449
1450 for (t = ata_timing; t->mode != speed; t++)
Alan Cox91190752005-10-26 12:17:46 -04001451 if (t->mode == 0xFF)
Alan Cox452503f2005-10-21 19:01:32 -04001452 return NULL;
1453 return t;
1454}
1455
1456int ata_timing_compute(struct ata_device *adev, unsigned short speed,
1457 struct ata_timing *t, int T, int UT)
1458{
1459 const struct ata_timing *s;
1460 struct ata_timing p;
1461
1462 /*
1463 * Find the mode.
Albert Lee75b1f2f2005-11-16 17:06:18 +08001464 */
Alan Cox452503f2005-10-21 19:01:32 -04001465
1466 if (!(s = ata_timing_find_mode(speed)))
1467 return -EINVAL;
1468
Albert Lee75b1f2f2005-11-16 17:06:18 +08001469 memcpy(t, s, sizeof(*s));
1470
Alan Cox452503f2005-10-21 19:01:32 -04001471 /*
1472 * If the drive is an EIDE drive, it can tell us it needs extended
1473 * PIO/MW_DMA cycle timing.
1474 */
1475
1476 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
1477 memset(&p, 0, sizeof(p));
1478 if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
1479 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
1480 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
1481 } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
1482 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
1483 }
1484 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
1485 }
1486
1487 /*
1488 * Convert the timing to bus clock counts.
1489 */
1490
Albert Lee75b1f2f2005-11-16 17:06:18 +08001491 ata_timing_quantize(t, t, T, UT);
Alan Cox452503f2005-10-21 19:01:32 -04001492
1493 /*
Randy Dunlapc893a3a2006-01-28 13:15:32 -05001494 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
1495 * S.M.A.R.T * and some other commands. We have to ensure that the
1496 * DMA cycle timing is slower/equal than the fastest PIO timing.
Alan Cox452503f2005-10-21 19:01:32 -04001497 */
1498
1499 if (speed > XFER_PIO_4) {
1500 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
1501 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
1502 }
1503
1504 /*
Randy Dunlapc893a3a2006-01-28 13:15:32 -05001505 * Lengthen active & recovery time so that cycle time is correct.
Alan Cox452503f2005-10-21 19:01:32 -04001506 */
1507
1508 if (t->act8b + t->rec8b < t->cyc8b) {
1509 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
1510 t->rec8b = t->cyc8b - t->act8b;
1511 }
1512
1513 if (t->active + t->recover < t->cycle) {
1514 t->active += (t->cycle - (t->active + t->recover)) / 2;
1515 t->recover = t->cycle - t->active;
1516 }
1517
1518 return 0;
1519}
1520
Jeff Garzik057ace52005-10-22 14:27:05 -04001521static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 unsigned int shift;
1523 u8 base;
1524} xfer_mode_classes[] = {
1525 { ATA_SHIFT_UDMA, XFER_UDMA_0 },
1526 { ATA_SHIFT_MWDMA, XFER_MW_DMA_0 },
1527 { ATA_SHIFT_PIO, XFER_PIO_0 },
1528};
1529
Arjan van de Ven858119e2006-01-14 13:20:43 -08001530static u8 base_from_shift(unsigned int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531{
1532 int i;
1533
1534 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++)
1535 if (xfer_mode_classes[i].shift == shift)
1536 return xfer_mode_classes[i].base;
1537
1538 return 0xff;
1539}
1540
1541static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
1542{
1543 int ofs, idx;
1544 u8 base;
1545
1546 if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1547 return;
1548
1549 if (dev->xfer_shift == ATA_SHIFT_PIO)
1550 dev->flags |= ATA_DFLAG_PIO;
1551
1552 ata_dev_set_xfermode(ap, dev);
1553
1554 base = base_from_shift(dev->xfer_shift);
1555 ofs = dev->xfer_mode - base;
1556 idx = ofs + dev->xfer_shift;
1557 WARN_ON(idx >= ARRAY_SIZE(xfer_mode_str));
1558
1559 DPRINTK("idx=%d xfer_shift=%u, xfer_mode=0x%x, base=0x%x, offset=%d\n",
1560 idx, dev->xfer_shift, (int)dev->xfer_mode, (int)base, ofs);
1561
1562 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
1563 ap->id, dev->devno, xfer_mode_str[idx]);
1564}
1565
1566static int ata_host_set_pio(struct ata_port *ap)
1567{
1568 unsigned int mask;
1569 int x, i;
1570 u8 base, xfer_mode;
1571
1572 mask = ata_get_mode_mask(ap, ATA_SHIFT_PIO);
1573 x = fgb(mask);
1574 if (x < 0) {
1575 printk(KERN_WARNING "ata%u: no PIO support\n", ap->id);
1576 return -1;
1577 }
1578
1579 base = base_from_shift(ATA_SHIFT_PIO);
1580 xfer_mode = base + x;
1581
1582 DPRINTK("base 0x%x xfer_mode 0x%x mask 0x%x x %d\n",
1583 (int)base, (int)xfer_mode, mask, x);
1584
1585 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1586 struct ata_device *dev = &ap->device[i];
1587 if (ata_dev_present(dev)) {
1588 dev->pio_mode = xfer_mode;
1589 dev->xfer_mode = xfer_mode;
1590 dev->xfer_shift = ATA_SHIFT_PIO;
1591 if (ap->ops->set_piomode)
1592 ap->ops->set_piomode(ap, dev);
1593 }
1594 }
1595
1596 return 0;
1597}
1598
1599static void ata_host_set_dma(struct ata_port *ap, u8 xfer_mode,
1600 unsigned int xfer_shift)
1601{
1602 int i;
1603
1604 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1605 struct ata_device *dev = &ap->device[i];
1606 if (ata_dev_present(dev)) {
1607 dev->dma_mode = xfer_mode;
1608 dev->xfer_mode = xfer_mode;
1609 dev->xfer_shift = xfer_shift;
1610 if (ap->ops->set_dmamode)
1611 ap->ops->set_dmamode(ap, dev);
1612 }
1613 }
1614}
1615
1616/**
1617 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1618 * @ap: port on which timings will be programmed
1619 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001620 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1621 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001623 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 */
1625static void ata_set_mode(struct ata_port *ap)
1626{
Albert Lee8cbd6df2005-10-12 15:06:27 +08001627 unsigned int xfer_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 u8 xfer_mode;
1629 int rc;
1630
1631 /* step 1: always set host PIO timings */
1632 rc = ata_host_set_pio(ap);
1633 if (rc)
1634 goto err_out;
1635
1636 /* step 2: choose the best data xfer mode */
1637 xfer_mode = xfer_shift = 0;
1638 rc = ata_choose_xfer_mode(ap, &xfer_mode, &xfer_shift);
1639 if (rc)
1640 goto err_out;
1641
1642 /* step 3: if that xfer mode isn't PIO, set host DMA timings */
1643 if (xfer_shift != ATA_SHIFT_PIO)
1644 ata_host_set_dma(ap, xfer_mode, xfer_shift);
1645
1646 /* step 4: update devices' xfer mode */
1647 ata_dev_set_mode(ap, &ap->device[0]);
1648 ata_dev_set_mode(ap, &ap->device[1]);
1649
1650 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1651 return;
1652
1653 if (ap->ops->post_set_mode)
1654 ap->ops->post_set_mode(ap);
1655
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 return;
1657
1658err_out:
1659 ata_port_disable(ap);
1660}
1661
1662/**
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05001663 * ata_tf_to_host - issue ATA taskfile to host controller
1664 * @ap: port to which command is being issued
1665 * @tf: ATA taskfile register set
1666 *
1667 * Issues ATA taskfile register set to ATA host controller,
1668 * with proper synchronization with interrupt handler and
1669 * other threads.
1670 *
1671 * LOCKING:
1672 * spin_lock_irqsave(host_set lock)
1673 */
1674
1675static inline void ata_tf_to_host(struct ata_port *ap,
1676 const struct ata_taskfile *tf)
1677{
1678 ap->ops->tf_load(ap, tf);
1679 ap->ops->exec_command(ap, tf);
1680}
1681
1682/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 * ata_busy_sleep - sleep until BSY clears, or timeout
1684 * @ap: port containing status register to be polled
1685 * @tmout_pat: impatience timeout
1686 * @tmout: overall timeout
1687 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001688 * Sleep until ATA Status register bit BSY clears,
1689 * or a timeout occurs.
1690 *
1691 * LOCKING: None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 */
1693
Tejun Heo6f8b9952006-01-24 17:05:21 +09001694unsigned int ata_busy_sleep (struct ata_port *ap,
1695 unsigned long tmout_pat, unsigned long tmout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696{
1697 unsigned long timer_start, timeout;
1698 u8 status;
1699
1700 status = ata_busy_wait(ap, ATA_BUSY, 300);
1701 timer_start = jiffies;
1702 timeout = timer_start + tmout_pat;
1703 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1704 msleep(50);
1705 status = ata_busy_wait(ap, ATA_BUSY, 3);
1706 }
1707
1708 if (status & ATA_BUSY)
1709 printk(KERN_WARNING "ata%u is slow to respond, "
1710 "please be patient\n", ap->id);
1711
1712 timeout = timer_start + tmout;
1713 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1714 msleep(50);
1715 status = ata_chk_status(ap);
1716 }
1717
1718 if (status & ATA_BUSY) {
1719 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
1720 ap->id, tmout / HZ);
1721 return 1;
1722 }
1723
1724 return 0;
1725}
1726
1727static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
1728{
1729 struct ata_ioports *ioaddr = &ap->ioaddr;
1730 unsigned int dev0 = devmask & (1 << 0);
1731 unsigned int dev1 = devmask & (1 << 1);
1732 unsigned long timeout;
1733
1734 /* if device 0 was found in ata_devchk, wait for its
1735 * BSY bit to clear
1736 */
1737 if (dev0)
1738 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1739
1740 /* if device 1 was found in ata_devchk, wait for
1741 * register access, then wait for BSY to clear
1742 */
1743 timeout = jiffies + ATA_TMOUT_BOOT;
1744 while (dev1) {
1745 u8 nsect, lbal;
1746
1747 ap->ops->dev_select(ap, 1);
1748 if (ap->flags & ATA_FLAG_MMIO) {
1749 nsect = readb((void __iomem *) ioaddr->nsect_addr);
1750 lbal = readb((void __iomem *) ioaddr->lbal_addr);
1751 } else {
1752 nsect = inb(ioaddr->nsect_addr);
1753 lbal = inb(ioaddr->lbal_addr);
1754 }
1755 if ((nsect == 1) && (lbal == 1))
1756 break;
1757 if (time_after(jiffies, timeout)) {
1758 dev1 = 0;
1759 break;
1760 }
1761 msleep(50); /* give drive a breather */
1762 }
1763 if (dev1)
1764 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1765
1766 /* is all this really necessary? */
1767 ap->ops->dev_select(ap, 0);
1768 if (dev1)
1769 ap->ops->dev_select(ap, 1);
1770 if (dev0)
1771 ap->ops->dev_select(ap, 0);
1772}
1773
1774/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001775 * ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
1776 * @ap: Port to reset and probe
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001778 * Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
1779 * probe the bus. Not often used these days.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 *
1781 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001782 * PCI/etc. bus probe sem.
Jeff Garzike5338252005-10-30 21:37:17 -05001783 * Obtains host_set lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 *
1785 */
1786
1787static unsigned int ata_bus_edd(struct ata_port *ap)
1788{
1789 struct ata_taskfile tf;
Jeff Garzike5338252005-10-30 21:37:17 -05001790 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
1792 /* set up execute-device-diag (bus reset) taskfile */
1793 /* also, take interrupts to a known state (disabled) */
1794 DPRINTK("execute-device-diag\n");
1795 ata_tf_init(ap, &tf, 0);
1796 tf.ctl |= ATA_NIEN;
1797 tf.command = ATA_CMD_EDD;
1798 tf.protocol = ATA_PROT_NODATA;
1799
1800 /* do bus reset */
Jeff Garzike5338252005-10-30 21:37:17 -05001801 spin_lock_irqsave(&ap->host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802 ata_tf_to_host(ap, &tf);
Jeff Garzike5338252005-10-30 21:37:17 -05001803 spin_unlock_irqrestore(&ap->host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
1805 /* spec says at least 2ms. but who knows with those
1806 * crazy ATAPI devices...
1807 */
1808 msleep(150);
1809
1810 return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1811}
1812
1813static unsigned int ata_bus_softreset(struct ata_port *ap,
1814 unsigned int devmask)
1815{
1816 struct ata_ioports *ioaddr = &ap->ioaddr;
1817
1818 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
1819
1820 /* software reset. causes dev0 to be selected */
1821 if (ap->flags & ATA_FLAG_MMIO) {
1822 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1823 udelay(20); /* FIXME: flush */
1824 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
1825 udelay(20); /* FIXME: flush */
1826 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1827 } else {
1828 outb(ap->ctl, ioaddr->ctl_addr);
1829 udelay(10);
1830 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
1831 udelay(10);
1832 outb(ap->ctl, ioaddr->ctl_addr);
1833 }
1834
1835 /* spec mandates ">= 2ms" before checking status.
1836 * We wait 150ms, because that was the magic delay used for
1837 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
1838 * between when the ATA command register is written, and then
1839 * status is checked. Because waiting for "a while" before
1840 * checking status is fine, post SRST, we perform this magic
1841 * delay here as well.
1842 */
1843 msleep(150);
1844
1845 ata_bus_post_reset(ap, devmask);
1846
1847 return 0;
1848}
1849
1850/**
1851 * ata_bus_reset - reset host port and associated ATA channel
1852 * @ap: port to reset
1853 *
1854 * This is typically the first time we actually start issuing
1855 * commands to the ATA channel. We wait for BSY to clear, then
1856 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
1857 * result. Determine what devices, if any, are on the channel
1858 * by looking at the device 0/1 error register. Look at the signature
1859 * stored in each device's taskfile registers, to determine if
1860 * the device is ATA or ATAPI.
1861 *
1862 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001863 * PCI/etc. bus probe sem.
1864 * Obtains host_set lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 *
1866 * SIDE EFFECTS:
1867 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
1868 */
1869
1870void ata_bus_reset(struct ata_port *ap)
1871{
1872 struct ata_ioports *ioaddr = &ap->ioaddr;
1873 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
1874 u8 err;
1875 unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
1876
1877 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
1878
1879 /* determine if device 0/1 are present */
1880 if (ap->flags & ATA_FLAG_SATA_RESET)
1881 dev0 = 1;
1882 else {
1883 dev0 = ata_devchk(ap, 0);
1884 if (slave_possible)
1885 dev1 = ata_devchk(ap, 1);
1886 }
1887
1888 if (dev0)
1889 devmask |= (1 << 0);
1890 if (dev1)
1891 devmask |= (1 << 1);
1892
1893 /* select device 0 again */
1894 ap->ops->dev_select(ap, 0);
1895
1896 /* issue bus reset */
1897 if (ap->flags & ATA_FLAG_SRST)
1898 rc = ata_bus_softreset(ap, devmask);
1899 else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
1900 /* set up device control */
1901 if (ap->flags & ATA_FLAG_MMIO)
1902 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1903 else
1904 outb(ap->ctl, ioaddr->ctl_addr);
1905 rc = ata_bus_edd(ap);
1906 }
1907
1908 if (rc)
1909 goto err_out;
1910
1911 /*
1912 * determine by signature whether we have ATA or ATAPI devices
1913 */
Tejun Heob4dc7622006-01-24 17:05:22 +09001914 ap->device[0].class = ata_dev_try_classify(ap, 0, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 if ((slave_possible) && (err != 0x81))
Tejun Heob4dc7622006-01-24 17:05:22 +09001916 ap->device[1].class = ata_dev_try_classify(ap, 1, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
1918 /* re-enable interrupts */
1919 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
1920 ata_irq_on(ap);
1921
1922 /* is double-select really necessary? */
1923 if (ap->device[1].class != ATA_DEV_NONE)
1924 ap->ops->dev_select(ap, 1);
1925 if (ap->device[0].class != ATA_DEV_NONE)
1926 ap->ops->dev_select(ap, 0);
1927
1928 /* if no devices were detected, disable this port */
1929 if ((ap->device[0].class == ATA_DEV_NONE) &&
1930 (ap->device[1].class == ATA_DEV_NONE))
1931 goto err_out;
1932
1933 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
1934 /* set up device control for ATA_FLAG_SATA_RESET */
1935 if (ap->flags & ATA_FLAG_MMIO)
1936 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1937 else
1938 outb(ap->ctl, ioaddr->ctl_addr);
1939 }
1940
1941 DPRINTK("EXIT\n");
1942 return;
1943
1944err_out:
1945 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
1946 ap->ops->port_disable(ap);
1947
1948 DPRINTK("EXIT\n");
1949}
1950
Tejun Heo7a7921e2006-02-02 18:20:00 +09001951static int sata_phy_resume(struct ata_port *ap)
1952{
1953 unsigned long timeout = jiffies + (HZ * 5);
1954 u32 sstatus;
1955
1956 scr_write_flush(ap, SCR_CONTROL, 0x300);
1957
1958 /* Wait for phy to become ready, if necessary. */
1959 do {
1960 msleep(200);
1961 sstatus = scr_read(ap, SCR_STATUS);
1962 if ((sstatus & 0xf) != 1)
1963 return 0;
1964 } while (time_before(jiffies, timeout));
1965
1966 return -1;
1967}
1968
Tejun Heoc2bd5802006-01-24 17:05:22 +09001969/**
Tejun Heo8a19ac82006-02-02 18:20:00 +09001970 * ata_std_probeinit - initialize probing
1971 * @ap: port to be probed
1972 *
1973 * @ap is about to be probed. Initialize it. This function is
1974 * to be used as standard callback for ata_drive_probe_reset().
Tejun Heo3a397462006-02-10 23:58:48 +09001975 *
1976 * NOTE!!! Do not use this function as probeinit if a low level
1977 * driver implements only hardreset. Just pass NULL as probeinit
1978 * in that case. Using this function is probably okay but doing
1979 * so makes reset sequence different from the original
1980 * ->phy_reset implementation and Jeff nervous. :-P
Tejun Heo8a19ac82006-02-02 18:20:00 +09001981 */
1982extern void ata_std_probeinit(struct ata_port *ap)
1983{
Tejun Heo3a397462006-02-10 23:58:48 +09001984 if (ap->flags & ATA_FLAG_SATA && ap->ops->scr_read) {
Tejun Heo8a19ac82006-02-02 18:20:00 +09001985 sata_phy_resume(ap);
Tejun Heo3a397462006-02-10 23:58:48 +09001986 if (sata_dev_present(ap))
1987 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1988 }
Tejun Heo8a19ac82006-02-02 18:20:00 +09001989}
1990
1991/**
Tejun Heoc2bd5802006-01-24 17:05:22 +09001992 * ata_std_softreset - reset host port via ATA SRST
1993 * @ap: port to reset
1994 * @verbose: fail verbosely
1995 * @classes: resulting classes of attached devices
1996 *
1997 * Reset host port using ATA SRST. This function is to be used
1998 * as standard callback for ata_drive_*_reset() functions.
1999 *
2000 * LOCKING:
2001 * Kernel thread context (may sleep)
2002 *
2003 * RETURNS:
2004 * 0 on success, -errno otherwise.
2005 */
2006int ata_std_softreset(struct ata_port *ap, int verbose, unsigned int *classes)
2007{
2008 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2009 unsigned int devmask = 0, err_mask;
2010 u8 err;
2011
2012 DPRINTK("ENTER\n");
2013
Tejun Heo3a397462006-02-10 23:58:48 +09002014 if (ap->ops->scr_read && !sata_dev_present(ap)) {
2015 classes[0] = ATA_DEV_NONE;
2016 goto out;
2017 }
2018
Tejun Heoc2bd5802006-01-24 17:05:22 +09002019 /* determine if device 0/1 are present */
2020 if (ata_devchk(ap, 0))
2021 devmask |= (1 << 0);
2022 if (slave_possible && ata_devchk(ap, 1))
2023 devmask |= (1 << 1);
2024
Tejun Heoc2bd5802006-01-24 17:05:22 +09002025 /* select device 0 again */
2026 ap->ops->dev_select(ap, 0);
2027
2028 /* issue bus reset */
2029 DPRINTK("about to softreset, devmask=%x\n", devmask);
2030 err_mask = ata_bus_softreset(ap, devmask);
2031 if (err_mask) {
2032 if (verbose)
2033 printk(KERN_ERR "ata%u: SRST failed (err_mask=0x%x)\n",
2034 ap->id, err_mask);
2035 else
2036 DPRINTK("EXIT, softreset failed (err_mask=0x%x)\n",
2037 err_mask);
2038 return -EIO;
2039 }
2040
2041 /* determine by signature whether we have ATA or ATAPI devices */
2042 classes[0] = ata_dev_try_classify(ap, 0, &err);
2043 if (slave_possible && err != 0x81)
2044 classes[1] = ata_dev_try_classify(ap, 1, &err);
2045
Tejun Heo3a397462006-02-10 23:58:48 +09002046 out:
Tejun Heoc2bd5802006-01-24 17:05:22 +09002047 DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]);
2048 return 0;
2049}
2050
2051/**
2052 * sata_std_hardreset - reset host port via SATA phy reset
2053 * @ap: port to reset
2054 * @verbose: fail verbosely
2055 * @class: resulting class of attached device
2056 *
2057 * SATA phy-reset host port using DET bits of SControl register.
2058 * This function is to be used as standard callback for
2059 * ata_drive_*_reset().
2060 *
2061 * LOCKING:
2062 * Kernel thread context (may sleep)
2063 *
2064 * RETURNS:
2065 * 0 on success, -errno otherwise.
2066 */
2067int sata_std_hardreset(struct ata_port *ap, int verbose, unsigned int *class)
2068{
Tejun Heoc2bd5802006-01-24 17:05:22 +09002069 DPRINTK("ENTER\n");
2070
2071 /* Issue phy wake/reset */
2072 scr_write_flush(ap, SCR_CONTROL, 0x301);
2073
2074 /*
2075 * Couldn't find anything in SATA I/II specs, but AHCI-1.1
2076 * 10.4.2 says at least 1 ms.
2077 */
2078 msleep(1);
2079
Tejun Heo7a7921e2006-02-02 18:20:00 +09002080 /* Bring phy back */
2081 sata_phy_resume(ap);
Tejun Heoc2bd5802006-01-24 17:05:22 +09002082
Tejun Heoc2bd5802006-01-24 17:05:22 +09002083 /* TODO: phy layer with polling, timeouts, etc. */
2084 if (!sata_dev_present(ap)) {
2085 *class = ATA_DEV_NONE;
2086 DPRINTK("EXIT, link offline\n");
2087 return 0;
2088 }
2089
2090 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
2091 if (verbose)
2092 printk(KERN_ERR "ata%u: COMRESET failed "
2093 "(device not ready)\n", ap->id);
2094 else
2095 DPRINTK("EXIT, device not ready\n");
2096 return -EIO;
2097 }
2098
Tejun Heo3a397462006-02-10 23:58:48 +09002099 ap->ops->dev_select(ap, 0); /* probably unnecessary */
2100
Tejun Heoc2bd5802006-01-24 17:05:22 +09002101 *class = ata_dev_try_classify(ap, 0, NULL);
2102
2103 DPRINTK("EXIT, class=%u\n", *class);
2104 return 0;
2105}
2106
2107/**
2108 * ata_std_postreset - standard postreset callback
2109 * @ap: the target ata_port
2110 * @classes: classes of attached devices
2111 *
2112 * This function is invoked after a successful reset. Note that
2113 * the device might have been reset more than once using
2114 * different reset methods before postreset is invoked.
Tejun Heoc2bd5802006-01-24 17:05:22 +09002115 *
2116 * This function is to be used as standard callback for
2117 * ata_drive_*_reset().
2118 *
2119 * LOCKING:
2120 * Kernel thread context (may sleep)
2121 */
2122void ata_std_postreset(struct ata_port *ap, unsigned int *classes)
2123{
2124 DPRINTK("ENTER\n");
2125
Tejun Heo56497bd2006-02-15 15:01:42 +09002126 /* set cable type if it isn't already set */
Tejun Heoc2bd5802006-01-24 17:05:22 +09002127 if (ap->cbl == ATA_CBL_NONE && ap->flags & ATA_FLAG_SATA)
2128 ap->cbl = ATA_CBL_SATA;
2129
2130 /* print link status */
2131 if (ap->cbl == ATA_CBL_SATA)
2132 sata_print_link_status(ap);
2133
Tejun Heo3a397462006-02-10 23:58:48 +09002134 /* re-enable interrupts */
2135 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2136 ata_irq_on(ap);
Tejun Heoc2bd5802006-01-24 17:05:22 +09002137
2138 /* is double-select really necessary? */
2139 if (classes[0] != ATA_DEV_NONE)
2140 ap->ops->dev_select(ap, 1);
2141 if (classes[1] != ATA_DEV_NONE)
2142 ap->ops->dev_select(ap, 0);
2143
Tejun Heo3a397462006-02-10 23:58:48 +09002144 /* bail out if no device is present */
2145 if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
2146 DPRINTK("EXIT, no device\n");
2147 return;
2148 }
2149
2150 /* set up device control */
2151 if (ap->ioaddr.ctl_addr) {
2152 if (ap->flags & ATA_FLAG_MMIO)
2153 writeb(ap->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
2154 else
2155 outb(ap->ctl, ap->ioaddr.ctl_addr);
2156 }
Tejun Heoc2bd5802006-01-24 17:05:22 +09002157
2158 DPRINTK("EXIT\n");
2159}
2160
2161/**
2162 * ata_std_probe_reset - standard probe reset method
2163 * @ap: prot to perform probe-reset
2164 * @classes: resulting classes of attached devices
2165 *
2166 * The stock off-the-shelf ->probe_reset method.
2167 *
2168 * LOCKING:
2169 * Kernel thread context (may sleep)
2170 *
2171 * RETURNS:
2172 * 0 on success, -errno otherwise.
2173 */
2174int ata_std_probe_reset(struct ata_port *ap, unsigned int *classes)
2175{
2176 ata_reset_fn_t hardreset;
2177
2178 hardreset = NULL;
Tejun Heob911fc32006-02-02 18:20:00 +09002179 if (ap->flags & ATA_FLAG_SATA && ap->ops->scr_read)
Tejun Heoc2bd5802006-01-24 17:05:22 +09002180 hardreset = sata_std_hardreset;
2181
Tejun Heo8a19ac82006-02-02 18:20:00 +09002182 return ata_drive_probe_reset(ap, ata_std_probeinit,
Tejun Heo7944ea92006-02-02 18:20:00 +09002183 ata_std_softreset, hardreset,
Tejun Heoc2bd5802006-01-24 17:05:22 +09002184 ata_std_postreset, classes);
2185}
2186
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002187static int do_probe_reset(struct ata_port *ap, ata_reset_fn_t reset,
2188 ata_postreset_fn_t postreset,
2189 unsigned int *classes)
2190{
2191 int i, rc;
2192
2193 for (i = 0; i < ATA_MAX_DEVICES; i++)
2194 classes[i] = ATA_DEV_UNKNOWN;
2195
2196 rc = reset(ap, 0, classes);
2197 if (rc)
2198 return rc;
2199
2200 /* If any class isn't ATA_DEV_UNKNOWN, consider classification
2201 * is complete and convert all ATA_DEV_UNKNOWN to
2202 * ATA_DEV_NONE.
2203 */
2204 for (i = 0; i < ATA_MAX_DEVICES; i++)
2205 if (classes[i] != ATA_DEV_UNKNOWN)
2206 break;
2207
2208 if (i < ATA_MAX_DEVICES)
2209 for (i = 0; i < ATA_MAX_DEVICES; i++)
2210 if (classes[i] == ATA_DEV_UNKNOWN)
2211 classes[i] = ATA_DEV_NONE;
2212
2213 if (postreset)
2214 postreset(ap, classes);
2215
2216 return classes[0] != ATA_DEV_UNKNOWN ? 0 : -ENODEV;
2217}
2218
2219/**
2220 * ata_drive_probe_reset - Perform probe reset with given methods
2221 * @ap: port to reset
Tejun Heo7944ea92006-02-02 18:20:00 +09002222 * @probeinit: probeinit method (can be NULL)
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002223 * @softreset: softreset method (can be NULL)
2224 * @hardreset: hardreset method (can be NULL)
2225 * @postreset: postreset method (can be NULL)
2226 * @classes: resulting classes of attached devices
2227 *
2228 * Reset the specified port and classify attached devices using
2229 * given methods. This function prefers softreset but tries all
2230 * possible reset sequences to reset and classify devices. This
2231 * function is intended to be used for constructing ->probe_reset
2232 * callback by low level drivers.
2233 *
2234 * Reset methods should follow the following rules.
2235 *
2236 * - Return 0 on sucess, -errno on failure.
2237 * - If classification is supported, fill classes[] with
2238 * recognized class codes.
2239 * - If classification is not supported, leave classes[] alone.
2240 * - If verbose is non-zero, print error message on failure;
2241 * otherwise, shut up.
2242 *
2243 * LOCKING:
2244 * Kernel thread context (may sleep)
2245 *
2246 * RETURNS:
2247 * 0 on success, -EINVAL if no reset method is avaliable, -ENODEV
2248 * if classification fails, and any error code from reset
2249 * methods.
2250 */
Tejun Heo7944ea92006-02-02 18:20:00 +09002251int ata_drive_probe_reset(struct ata_port *ap, ata_probeinit_fn_t probeinit,
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002252 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2253 ata_postreset_fn_t postreset, unsigned int *classes)
2254{
2255 int rc = -EINVAL;
2256
Tejun Heo7944ea92006-02-02 18:20:00 +09002257 if (probeinit)
2258 probeinit(ap);
2259
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002260 if (softreset) {
2261 rc = do_probe_reset(ap, softreset, postreset, classes);
2262 if (rc == 0)
2263 return 0;
2264 }
2265
2266 if (!hardreset)
2267 return rc;
2268
2269 rc = do_probe_reset(ap, hardreset, postreset, classes);
2270 if (rc == 0 || rc != -ENODEV)
2271 return rc;
2272
2273 if (softreset)
2274 rc = do_probe_reset(ap, softreset, postreset, classes);
2275
2276 return rc;
2277}
2278
Jeff Garzik057ace52005-10-22 14:27:05 -04002279static void ata_pr_blacklisted(const struct ata_port *ap,
2280 const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281{
2282 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, disabling DMA\n",
2283 ap->id, dev->devno);
2284}
2285
Arjan van de Ven98ac62d2005-11-28 10:06:23 +01002286static const char * const ata_dma_blacklist [] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 "WDC AC11000H",
2288 "WDC AC22100H",
2289 "WDC AC32500H",
2290 "WDC AC33100H",
2291 "WDC AC31600H",
2292 "WDC AC32100H",
2293 "WDC AC23200L",
2294 "Compaq CRD-8241B",
2295 "CRD-8400B",
2296 "CRD-8480B",
2297 "CRD-8482B",
2298 "CRD-84",
2299 "SanDisk SDP3B",
2300 "SanDisk SDP3B-64",
2301 "SANYO CD-ROM CRD",
2302 "HITACHI CDR-8",
2303 "HITACHI CDR-8335",
2304 "HITACHI CDR-8435",
2305 "Toshiba CD-ROM XM-6202B",
Jeff Garzike9222562005-06-28 00:03:37 -04002306 "TOSHIBA CD-ROM XM-1702BC",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 "CD-532E-A",
2308 "E-IDE CD-ROM CR-840",
2309 "CD-ROM Drive/F5A",
2310 "WPI CDD-820",
2311 "SAMSUNG CD-ROM SC-148C",
2312 "SAMSUNG CD-ROM SC",
2313 "SanDisk SDP3B-64",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 "ATAPI CD-ROM DRIVE 40X MAXIMUM",
2315 "_NEC DV5800A",
2316};
2317
Jeff Garzik057ace52005-10-22 14:27:05 -04002318static int ata_dma_blacklisted(const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319{
Tejun Heo2e026712006-02-12 22:47:04 +09002320 unsigned char model_num[41];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 int i;
2322
Tejun Heo6a62a042006-02-13 10:02:46 +09002323 ata_id_c_string(dev->id, model_num, ATA_ID_PROD_OFS, sizeof(model_num));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324
2325 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i++)
Tejun Heo2e026712006-02-12 22:47:04 +09002326 if (!strcmp(ata_dma_blacklist[i], model_num))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 return 1;
2328
2329 return 0;
2330}
2331
Jeff Garzik057ace52005-10-22 14:27:05 -04002332static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333{
Jeff Garzik057ace52005-10-22 14:27:05 -04002334 const struct ata_device *master, *slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 unsigned int mask;
2336
2337 master = &ap->device[0];
2338 slave = &ap->device[1];
2339
Tejun Heoa4631472006-02-11 19:11:13 +09002340 WARN_ON(!ata_dev_present(master) && !ata_dev_present(slave));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341
2342 if (shift == ATA_SHIFT_UDMA) {
2343 mask = ap->udma_mask;
2344 if (ata_dev_present(master)) {
2345 mask &= (master->id[ATA_ID_UDMA_MODES] & 0xff);
Jeff Garzik057ace52005-10-22 14:27:05 -04002346 if (ata_dma_blacklisted(master)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 mask = 0;
2348 ata_pr_blacklisted(ap, master);
2349 }
2350 }
2351 if (ata_dev_present(slave)) {
2352 mask &= (slave->id[ATA_ID_UDMA_MODES] & 0xff);
Jeff Garzik057ace52005-10-22 14:27:05 -04002353 if (ata_dma_blacklisted(slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 mask = 0;
2355 ata_pr_blacklisted(ap, slave);
2356 }
2357 }
2358 }
2359 else if (shift == ATA_SHIFT_MWDMA) {
2360 mask = ap->mwdma_mask;
2361 if (ata_dev_present(master)) {
2362 mask &= (master->id[ATA_ID_MWDMA_MODES] & 0x07);
Jeff Garzik057ace52005-10-22 14:27:05 -04002363 if (ata_dma_blacklisted(master)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 mask = 0;
2365 ata_pr_blacklisted(ap, master);
2366 }
2367 }
2368 if (ata_dev_present(slave)) {
2369 mask &= (slave->id[ATA_ID_MWDMA_MODES] & 0x07);
Jeff Garzik057ace52005-10-22 14:27:05 -04002370 if (ata_dma_blacklisted(slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 mask = 0;
2372 ata_pr_blacklisted(ap, slave);
2373 }
2374 }
2375 }
2376 else if (shift == ATA_SHIFT_PIO) {
2377 mask = ap->pio_mask;
2378 if (ata_dev_present(master)) {
2379 /* spec doesn't return explicit support for
2380 * PIO0-2, so we fake it
2381 */
2382 u16 tmp_mode = master->id[ATA_ID_PIO_MODES] & 0x03;
2383 tmp_mode <<= 3;
2384 tmp_mode |= 0x7;
2385 mask &= tmp_mode;
2386 }
2387 if (ata_dev_present(slave)) {
2388 /* spec doesn't return explicit support for
2389 * PIO0-2, so we fake it
2390 */
2391 u16 tmp_mode = slave->id[ATA_ID_PIO_MODES] & 0x03;
2392 tmp_mode <<= 3;
2393 tmp_mode |= 0x7;
2394 mask &= tmp_mode;
2395 }
2396 }
2397 else {
2398 mask = 0xffffffff; /* shut up compiler warning */
2399 BUG();
2400 }
2401
2402 return mask;
2403}
2404
2405/* find greatest bit */
2406static int fgb(u32 bitmap)
2407{
2408 unsigned int i;
2409 int x = -1;
2410
2411 for (i = 0; i < 32; i++)
2412 if (bitmap & (1 << i))
2413 x = i;
2414
2415 return x;
2416}
2417
2418/**
2419 * ata_choose_xfer_mode - attempt to find best transfer mode
2420 * @ap: Port for which an xfer mode will be selected
2421 * @xfer_mode_out: (output) SET FEATURES - XFER MODE code
2422 * @xfer_shift_out: (output) bit shift that selects this mode
2423 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04002424 * Based on host and device capabilities, determine the
2425 * maximum transfer mode that is amenable to all.
2426 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002428 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 *
2430 * RETURNS:
2431 * Zero on success, negative on error.
2432 */
2433
Jeff Garzik057ace52005-10-22 14:27:05 -04002434static int ata_choose_xfer_mode(const struct ata_port *ap,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 u8 *xfer_mode_out,
2436 unsigned int *xfer_shift_out)
2437{
2438 unsigned int mask, shift;
2439 int x, i;
2440
2441 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++) {
2442 shift = xfer_mode_classes[i].shift;
2443 mask = ata_get_mode_mask(ap, shift);
2444
2445 x = fgb(mask);
2446 if (x >= 0) {
2447 *xfer_mode_out = xfer_mode_classes[i].base + x;
2448 *xfer_shift_out = shift;
2449 return 0;
2450 }
2451 }
2452
2453 return -1;
2454}
2455
2456/**
2457 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2458 * @ap: Port associated with device @dev
2459 * @dev: Device to which command will be sent
2460 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002461 * Issue SET FEATURES - XFER MODE command to device @dev
2462 * on port @ap.
2463 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002465 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 */
2467
2468static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
2469{
Tejun Heoa0123702005-12-13 14:49:31 +09002470 struct ata_taskfile tf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471
2472 /* set up set-features taskfile */
2473 DPRINTK("set features - xfer mode\n");
2474
Tejun Heoa0123702005-12-13 14:49:31 +09002475 ata_tf_init(ap, &tf, dev->devno);
2476 tf.command = ATA_CMD_SET_FEATURES;
2477 tf.feature = SETFEATURES_XFER;
2478 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2479 tf.protocol = ATA_PROT_NODATA;
2480 tf.nsect = dev->xfer_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481
Tejun Heoa0123702005-12-13 14:49:31 +09002482 if (ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0)) {
2483 printk(KERN_ERR "ata%u: failed to set xfermode, disabled\n",
2484 ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 ata_port_disable(ap);
Tejun Heoa0123702005-12-13 14:49:31 +09002486 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487
2488 DPRINTK("EXIT\n");
2489}
2490
2491/**
Albert Lee59a10b12005-10-12 15:09:42 +08002492 * ata_dev_reread_id - Reread the device identify device info
2493 * @ap: port where the device is
2494 * @dev: device to reread the identify device info
2495 *
2496 * LOCKING:
2497 */
2498
2499static void ata_dev_reread_id(struct ata_port *ap, struct ata_device *dev)
2500{
Tejun Heoa0123702005-12-13 14:49:31 +09002501 struct ata_taskfile tf;
Albert Lee59a10b12005-10-12 15:09:42 +08002502
Tejun Heoa0123702005-12-13 14:49:31 +09002503 ata_tf_init(ap, &tf, dev->devno);
Albert Lee59a10b12005-10-12 15:09:42 +08002504
2505 if (dev->class == ATA_DEV_ATA) {
Tejun Heoa0123702005-12-13 14:49:31 +09002506 tf.command = ATA_CMD_ID_ATA;
Albert Lee59a10b12005-10-12 15:09:42 +08002507 DPRINTK("do ATA identify\n");
2508 } else {
Tejun Heoa0123702005-12-13 14:49:31 +09002509 tf.command = ATA_CMD_ID_ATAPI;
Albert Lee59a10b12005-10-12 15:09:42 +08002510 DPRINTK("do ATAPI identify\n");
2511 }
2512
Tejun Heoa0123702005-12-13 14:49:31 +09002513 tf.flags |= ATA_TFLAG_DEVICE;
2514 tf.protocol = ATA_PROT_PIO;
Albert Lee59a10b12005-10-12 15:09:42 +08002515
Tejun Heoa0123702005-12-13 14:49:31 +09002516 if (ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE,
2517 dev->id, sizeof(dev->id)))
Albert Lee59a10b12005-10-12 15:09:42 +08002518 goto err_out;
2519
Albert Lee59a10b12005-10-12 15:09:42 +08002520 swap_buf_le16(dev->id, ATA_ID_WORDS);
2521
Tejun Heo0bd33002006-02-12 22:47:05 +09002522 ata_dump_id(dev->id);
Albert Lee59a10b12005-10-12 15:09:42 +08002523
2524 DPRINTK("EXIT\n");
2525
2526 return;
2527err_out:
Tejun Heoa0123702005-12-13 14:49:31 +09002528 printk(KERN_ERR "ata%u: failed to reread ID, disabled\n", ap->id);
Albert Lee59a10b12005-10-12 15:09:42 +08002529 ata_port_disable(ap);
2530}
2531
2532/**
Albert Lee8bf62ec2005-05-12 15:29:42 -04002533 * ata_dev_init_params - Issue INIT DEV PARAMS command
2534 * @ap: Port associated with device @dev
2535 * @dev: Device to which command will be sent
2536 *
2537 * LOCKING:
Tejun Heo6aff8f12006-02-15 18:24:09 +09002538 * Kernel thread context (may sleep)
2539 *
2540 * RETURNS:
2541 * 0 on success, AC_ERR_* mask otherwise.
Albert Lee8bf62ec2005-05-12 15:29:42 -04002542 */
2543
Tejun Heo6aff8f12006-02-15 18:24:09 +09002544static unsigned int ata_dev_init_params(struct ata_port *ap,
2545 struct ata_device *dev)
Albert Lee8bf62ec2005-05-12 15:29:42 -04002546{
Tejun Heoa0123702005-12-13 14:49:31 +09002547 struct ata_taskfile tf;
Tejun Heo6aff8f12006-02-15 18:24:09 +09002548 unsigned int err_mask;
Albert Lee8bf62ec2005-05-12 15:29:42 -04002549 u16 sectors = dev->id[6];
2550 u16 heads = dev->id[3];
2551
2552 /* Number of sectors per track 1-255. Number of heads 1-16 */
2553 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
Tejun Heo6aff8f12006-02-15 18:24:09 +09002554 return 0;
Albert Lee8bf62ec2005-05-12 15:29:42 -04002555
2556 /* set up init dev params taskfile */
2557 DPRINTK("init dev params \n");
2558
Tejun Heoa0123702005-12-13 14:49:31 +09002559 ata_tf_init(ap, &tf, dev->devno);
2560 tf.command = ATA_CMD_INIT_DEV_PARAMS;
2561 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2562 tf.protocol = ATA_PROT_NODATA;
2563 tf.nsect = sectors;
2564 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
Albert Lee8bf62ec2005-05-12 15:29:42 -04002565
Tejun Heo6aff8f12006-02-15 18:24:09 +09002566 err_mask = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
Albert Lee8bf62ec2005-05-12 15:29:42 -04002567
Tejun Heo6aff8f12006-02-15 18:24:09 +09002568 DPRINTK("EXIT, err_mask=%x\n", err_mask);
2569 return err_mask;
Albert Lee8bf62ec2005-05-12 15:29:42 -04002570}
2571
2572/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002573 * ata_sg_clean - Unmap DMA memory associated with command
2574 * @qc: Command containing DMA memory to be released
2575 *
2576 * Unmap all mapped DMA memory associated with this command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 *
2578 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002579 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 */
2581
2582static void ata_sg_clean(struct ata_queued_cmd *qc)
2583{
2584 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002585 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002587 void *pad_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588
Tejun Heoa4631472006-02-11 19:11:13 +09002589 WARN_ON(!(qc->flags & ATA_QCFLAG_DMAMAP));
2590 WARN_ON(sg == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591
2592 if (qc->flags & ATA_QCFLAG_SINGLE)
Tejun Heoa4631472006-02-11 19:11:13 +09002593 WARN_ON(qc->n_elem != 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05002595 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002597 /* if we padded the buffer out to 32-bit bound, and data
2598 * xfer direction is from-device, we must copy from the
2599 * pad buffer back into the supplied buffer
2600 */
2601 if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
2602 pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2603
2604 if (qc->flags & ATA_QCFLAG_SG) {
Jeff Garzike1410f22005-11-14 14:06:26 -05002605 if (qc->n_elem)
2606 dma_unmap_sg(ap->host_set->dev, sg, qc->n_elem, dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002607 /* restore last sg */
2608 sg[qc->orig_n_elem - 1].length += qc->pad_len;
2609 if (pad_buf) {
2610 struct scatterlist *psg = &qc->pad_sgent;
2611 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2612 memcpy(addr + psg->offset, pad_buf, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05002613 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002614 }
2615 } else {
Jeff Garzike1410f22005-11-14 14:06:26 -05002616 if (sg_dma_len(&sg[0]) > 0)
2617 dma_unmap_single(ap->host_set->dev,
2618 sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
2619 dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002620 /* restore sg */
2621 sg->length += qc->pad_len;
2622 if (pad_buf)
2623 memcpy(qc->buf_virt + sg->length - qc->pad_len,
2624 pad_buf, qc->pad_len);
2625 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626
2627 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002628 qc->__sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629}
2630
2631/**
2632 * ata_fill_sg - Fill PCI IDE PRD table
2633 * @qc: Metadata associated with taskfile to be transferred
2634 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002635 * Fill PCI IDE PRD (scatter-gather) table with segments
2636 * associated with the current disk command.
2637 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 * LOCKING:
Jeff Garzik780a87f2005-05-30 15:41:05 -04002639 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 *
2641 */
2642static void ata_fill_sg(struct ata_queued_cmd *qc)
2643{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002645 struct scatterlist *sg;
2646 unsigned int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647
Tejun Heoa4631472006-02-11 19:11:13 +09002648 WARN_ON(qc->__sg == NULL);
2649 WARN_ON(qc->n_elem == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650
2651 idx = 0;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002652 ata_for_each_sg(sg, qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 u32 addr, offset;
2654 u32 sg_len, len;
2655
2656 /* determine if physical DMA addr spans 64K boundary.
2657 * Note h/w doesn't support 64-bit, so we unconditionally
2658 * truncate dma_addr_t to u32.
2659 */
2660 addr = (u32) sg_dma_address(sg);
2661 sg_len = sg_dma_len(sg);
2662
2663 while (sg_len) {
2664 offset = addr & 0xffff;
2665 len = sg_len;
2666 if ((offset + sg_len) > 0x10000)
2667 len = 0x10000 - offset;
2668
2669 ap->prd[idx].addr = cpu_to_le32(addr);
2670 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
2671 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
2672
2673 idx++;
2674 sg_len -= len;
2675 addr += len;
2676 }
2677 }
2678
2679 if (idx)
2680 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2681}
2682/**
2683 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2684 * @qc: Metadata associated with taskfile to check
2685 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002686 * Allow low-level driver to filter ATA PACKET commands, returning
2687 * a status indicating whether or not it is OK to use DMA for the
2688 * supplied PACKET command.
2689 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002691 * spin_lock_irqsave(host_set lock)
2692 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 * RETURNS: 0 when ATAPI DMA can be used
2694 * nonzero otherwise
2695 */
2696int ata_check_atapi_dma(struct ata_queued_cmd *qc)
2697{
2698 struct ata_port *ap = qc->ap;
2699 int rc = 0; /* Assume ATAPI DMA is OK by default */
2700
2701 if (ap->ops->check_atapi_dma)
2702 rc = ap->ops->check_atapi_dma(qc);
2703
2704 return rc;
2705}
2706/**
2707 * ata_qc_prep - Prepare taskfile for submission
2708 * @qc: Metadata associated with taskfile to be prepared
2709 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002710 * Prepare ATA taskfile for submission.
2711 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 * LOCKING:
2713 * spin_lock_irqsave(host_set lock)
2714 */
2715void ata_qc_prep(struct ata_queued_cmd *qc)
2716{
2717 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2718 return;
2719
2720 ata_fill_sg(qc);
2721}
2722
Jeff Garzik0cba6322005-05-30 19:49:12 -04002723/**
2724 * ata_sg_init_one - Associate command with memory buffer
2725 * @qc: Command to be associated
2726 * @buf: Memory buffer
2727 * @buflen: Length of memory buffer, in bytes.
2728 *
2729 * Initialize the data-related elements of queued_cmd @qc
2730 * to point to a single memory buffer, @buf of byte length @buflen.
2731 *
2732 * LOCKING:
2733 * spin_lock_irqsave(host_set lock)
2734 */
2735
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
2737{
2738 struct scatterlist *sg;
2739
2740 qc->flags |= ATA_QCFLAG_SINGLE;
2741
2742 memset(&qc->sgent, 0, sizeof(qc->sgent));
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002743 qc->__sg = &qc->sgent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 qc->n_elem = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002745 qc->orig_n_elem = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 qc->buf_virt = buf;
2747
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002748 sg = qc->__sg;
Jeff Garzikf0612bb2005-10-30 01:58:18 -05002749 sg_init_one(sg, buf, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750}
2751
Jeff Garzik0cba6322005-05-30 19:49:12 -04002752/**
2753 * ata_sg_init - Associate command with scatter-gather table.
2754 * @qc: Command to be associated
2755 * @sg: Scatter-gather table.
2756 * @n_elem: Number of elements in s/g table.
2757 *
2758 * Initialize the data-related elements of queued_cmd @qc
2759 * to point to a scatter-gather table @sg, containing @n_elem
2760 * elements.
2761 *
2762 * LOCKING:
2763 * spin_lock_irqsave(host_set lock)
2764 */
2765
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
2767 unsigned int n_elem)
2768{
2769 qc->flags |= ATA_QCFLAG_SG;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002770 qc->__sg = sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 qc->n_elem = n_elem;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002772 qc->orig_n_elem = n_elem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773}
2774
2775/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002776 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
2777 * @qc: Command with memory buffer to be mapped.
2778 *
2779 * DMA-map the memory buffer associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 *
2781 * LOCKING:
2782 * spin_lock_irqsave(host_set lock)
2783 *
2784 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002785 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 */
2787
2788static int ata_sg_setup_one(struct ata_queued_cmd *qc)
2789{
2790 struct ata_port *ap = qc->ap;
2791 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002792 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793 dma_addr_t dma_address;
2794
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002795 /* we must lengthen transfers to end on a 32-bit boundary */
2796 qc->pad_len = sg->length & 3;
2797 if (qc->pad_len) {
2798 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2799 struct scatterlist *psg = &qc->pad_sgent;
2800
Tejun Heoa4631472006-02-11 19:11:13 +09002801 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002802
2803 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2804
2805 if (qc->tf.flags & ATA_TFLAG_WRITE)
2806 memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
2807 qc->pad_len);
2808
2809 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2810 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2811 /* trim sg */
2812 sg->length -= qc->pad_len;
2813
2814 DPRINTK("padding done, sg->length=%u pad_len=%u\n",
2815 sg->length, qc->pad_len);
2816 }
2817
Jeff Garzike1410f22005-11-14 14:06:26 -05002818 if (!sg->length) {
2819 sg_dma_address(sg) = 0;
2820 goto skip_map;
2821 }
2822
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 dma_address = dma_map_single(ap->host_set->dev, qc->buf_virt,
Albert Lee32529e02005-05-26 03:49:42 -04002824 sg->length, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05002825 if (dma_mapping_error(dma_address)) {
2826 /* restore sg */
2827 sg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05002829 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830
2831 sg_dma_address(sg) = dma_address;
Jeff Garzike1410f22005-11-14 14:06:26 -05002832skip_map:
Albert Lee32529e02005-05-26 03:49:42 -04002833 sg_dma_len(sg) = sg->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834
2835 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
2836 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2837
2838 return 0;
2839}
2840
2841/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002842 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
2843 * @qc: Command with scatter-gather table to be mapped.
2844 *
2845 * DMA-map the scatter-gather table associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 *
2847 * LOCKING:
2848 * spin_lock_irqsave(host_set lock)
2849 *
2850 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002851 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 *
2853 */
2854
2855static int ata_sg_setup(struct ata_queued_cmd *qc)
2856{
2857 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002858 struct scatterlist *sg = qc->__sg;
2859 struct scatterlist *lsg = &sg[qc->n_elem - 1];
Jeff Garzike1410f22005-11-14 14:06:26 -05002860 int n_elem, pre_n_elem, dir, trim_sg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861
2862 VPRINTK("ENTER, ata%u\n", ap->id);
Tejun Heoa4631472006-02-11 19:11:13 +09002863 WARN_ON(!(qc->flags & ATA_QCFLAG_SG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002865 /* we must lengthen transfers to end on a 32-bit boundary */
2866 qc->pad_len = lsg->length & 3;
2867 if (qc->pad_len) {
2868 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2869 struct scatterlist *psg = &qc->pad_sgent;
2870 unsigned int offset;
2871
Tejun Heoa4631472006-02-11 19:11:13 +09002872 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002873
2874 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2875
2876 /*
2877 * psg->page/offset are used to copy to-be-written
2878 * data in this function or read data in ata_sg_clean.
2879 */
2880 offset = lsg->offset + lsg->length - qc->pad_len;
2881 psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
2882 psg->offset = offset_in_page(offset);
2883
2884 if (qc->tf.flags & ATA_TFLAG_WRITE) {
2885 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2886 memcpy(pad_buf, addr + psg->offset, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05002887 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002888 }
2889
2890 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2891 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2892 /* trim last sg */
2893 lsg->length -= qc->pad_len;
Jeff Garzike1410f22005-11-14 14:06:26 -05002894 if (lsg->length == 0)
2895 trim_sg = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002896
2897 DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
2898 qc->n_elem - 1, lsg->length, qc->pad_len);
2899 }
2900
Jeff Garzike1410f22005-11-14 14:06:26 -05002901 pre_n_elem = qc->n_elem;
2902 if (trim_sg && pre_n_elem)
2903 pre_n_elem--;
2904
2905 if (!pre_n_elem) {
2906 n_elem = 0;
2907 goto skip_map;
2908 }
2909
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 dir = qc->dma_dir;
Jeff Garzike1410f22005-11-14 14:06:26 -05002911 n_elem = dma_map_sg(ap->host_set->dev, sg, pre_n_elem, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05002912 if (n_elem < 1) {
2913 /* restore last sg */
2914 lsg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002915 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05002916 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917
2918 DPRINTK("%d sg elements mapped\n", n_elem);
2919
Jeff Garzike1410f22005-11-14 14:06:26 -05002920skip_map:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 qc->n_elem = n_elem;
2922
2923 return 0;
2924}
2925
2926/**
Tejun Heo40e8c822005-08-22 17:12:45 +09002927 * ata_poll_qc_complete - turn irq back on and finish qc
2928 * @qc: Command to complete
Randy Dunlap8e8b77d2005-11-01 21:29:27 -08002929 * @err_mask: ATA status register content
Tejun Heo40e8c822005-08-22 17:12:45 +09002930 *
2931 * LOCKING:
2932 * None. (grabs host lock)
2933 */
2934
Albert Leea22e2eb2005-12-05 15:38:02 +08002935void ata_poll_qc_complete(struct ata_queued_cmd *qc)
Tejun Heo40e8c822005-08-22 17:12:45 +09002936{
2937 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04002938 unsigned long flags;
Tejun Heo40e8c822005-08-22 17:12:45 +09002939
Jeff Garzikb8f61532005-08-25 22:01:20 -04002940 spin_lock_irqsave(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09002941 ap->flags &= ~ATA_FLAG_NOINTR;
2942 ata_irq_on(ap);
Albert Leea22e2eb2005-12-05 15:38:02 +08002943 ata_qc_complete(qc);
Jeff Garzikb8f61532005-08-25 22:01:20 -04002944 spin_unlock_irqrestore(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09002945}
2946
2947/**
Randy Dunlapc893a3a2006-01-28 13:15:32 -05002948 * ata_pio_poll - poll using PIO, depending on current state
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002949 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 *
2951 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002952 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 *
2954 * RETURNS:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002955 * timeout value to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 */
2957
2958static unsigned long ata_pio_poll(struct ata_port *ap)
2959{
Albert Leec14b8332005-12-05 15:36:08 +08002960 struct ata_queued_cmd *qc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 u8 status;
Albert Lee14be71f2005-09-27 17:36:35 +08002962 unsigned int poll_state = HSM_ST_UNKNOWN;
2963 unsigned int reg_state = HSM_ST_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964
Albert Leec14b8332005-12-05 15:36:08 +08002965 qc = ata_qc_from_tag(ap, ap->active_tag);
Tejun Heoa4631472006-02-11 19:11:13 +09002966 WARN_ON(qc == NULL);
Albert Leec14b8332005-12-05 15:36:08 +08002967
Albert Lee14be71f2005-09-27 17:36:35 +08002968 switch (ap->hsm_task_state) {
2969 case HSM_ST:
2970 case HSM_ST_POLL:
2971 poll_state = HSM_ST_POLL;
2972 reg_state = HSM_ST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 break;
Albert Lee14be71f2005-09-27 17:36:35 +08002974 case HSM_ST_LAST:
2975 case HSM_ST_LAST_POLL:
2976 poll_state = HSM_ST_LAST_POLL;
2977 reg_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 break;
2979 default:
2980 BUG();
2981 break;
2982 }
2983
2984 status = ata_chk_status(ap);
2985 if (status & ATA_BUSY) {
2986 if (time_after(jiffies, ap->pio_task_timeout)) {
Tejun Heo11a56d22006-01-23 13:09:36 +09002987 qc->err_mask |= AC_ERR_TIMEOUT;
Albert Lee7c398332005-11-09 13:03:30 +08002988 ap->hsm_task_state = HSM_ST_TMOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 return 0;
2990 }
Albert Lee14be71f2005-09-27 17:36:35 +08002991 ap->hsm_task_state = poll_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002992 return ATA_SHORT_PAUSE;
2993 }
2994
Albert Lee14be71f2005-09-27 17:36:35 +08002995 ap->hsm_task_state = reg_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 return 0;
2997}
2998
2999/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003000 * ata_pio_complete - check if drive is busy or idle
3001 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 *
3003 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003004 * None. (executing in kernel thread context)
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003005 *
3006 * RETURNS:
3007 * Non-zero if qc completed, zero otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 */
3009
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003010static int ata_pio_complete (struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011{
3012 struct ata_queued_cmd *qc;
3013 u8 drv_stat;
3014
3015 /*
Alan Cox31433ea2005-08-26 15:56:47 +01003016 * This is purely heuristic. This is a fast path. Sometimes when
3017 * we enter, BSY will be cleared in a chk-status or two. If not,
3018 * the drive is probably seeking or something. Snooze for a couple
3019 * msecs, then chk-status again. If still busy, fall back to
Albert Lee14be71f2005-09-27 17:36:35 +08003020 * HSM_ST_POLL state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 */
Albert Leefe79e682005-12-06 11:34:59 +08003022 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
3023 if (drv_stat & ATA_BUSY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 msleep(2);
Albert Leefe79e682005-12-06 11:34:59 +08003025 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
3026 if (drv_stat & ATA_BUSY) {
Albert Lee14be71f2005-09-27 17:36:35 +08003027 ap->hsm_task_state = HSM_ST_LAST_POLL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003029 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 }
3031 }
3032
Albert Leec14b8332005-12-05 15:36:08 +08003033 qc = ata_qc_from_tag(ap, ap->active_tag);
Tejun Heoa4631472006-02-11 19:11:13 +09003034 WARN_ON(qc == NULL);
Albert Leec14b8332005-12-05 15:36:08 +08003035
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 drv_stat = ata_wait_idle(ap);
3037 if (!ata_ok(drv_stat)) {
Albert Lee1c848982005-12-05 15:40:15 +08003038 qc->err_mask |= __ac_err_mask(drv_stat);
Albert Lee14be71f2005-09-27 17:36:35 +08003039 ap->hsm_task_state = HSM_ST_ERR;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003040 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 }
3042
Albert Lee14be71f2005-09-27 17:36:35 +08003043 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044
Tejun Heoa4631472006-02-11 19:11:13 +09003045 WARN_ON(qc->err_mask);
Albert Leea22e2eb2005-12-05 15:38:02 +08003046 ata_poll_qc_complete(qc);
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003047
3048 /* another command may start at this point */
3049
3050 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051}
3052
Edward Falk0baab862005-06-02 18:17:13 -04003053
3054/**
Randy Dunlapc893a3a2006-01-28 13:15:32 -05003055 * swap_buf_le16 - swap halves of 16-bit words in place
Edward Falk0baab862005-06-02 18:17:13 -04003056 * @buf: Buffer to swap
3057 * @buf_words: Number of 16-bit words in buffer.
3058 *
3059 * Swap halves of 16-bit words if needed to convert from
3060 * little-endian byte order to native cpu byte order, or
3061 * vice-versa.
3062 *
3063 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003064 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04003065 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066void swap_buf_le16(u16 *buf, unsigned int buf_words)
3067{
3068#ifdef __BIG_ENDIAN
3069 unsigned int i;
3070
3071 for (i = 0; i < buf_words; i++)
3072 buf[i] = le16_to_cpu(buf[i]);
3073#endif /* __BIG_ENDIAN */
3074}
3075
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003076/**
3077 * ata_mmio_data_xfer - Transfer data by MMIO
3078 * @ap: port to read/write
3079 * @buf: data buffer
3080 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04003081 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003082 *
3083 * Transfer data from/to the device data register by MMIO.
3084 *
3085 * LOCKING:
3086 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003087 */
3088
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
3090 unsigned int buflen, int write_data)
3091{
3092 unsigned int i;
3093 unsigned int words = buflen >> 1;
3094 u16 *buf16 = (u16 *) buf;
3095 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
3096
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003097 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 if (write_data) {
3099 for (i = 0; i < words; i++)
3100 writew(le16_to_cpu(buf16[i]), mmio);
3101 } else {
3102 for (i = 0; i < words; i++)
3103 buf16[i] = cpu_to_le16(readw(mmio));
3104 }
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003105
3106 /* Transfer trailing 1 byte, if any. */
3107 if (unlikely(buflen & 0x01)) {
3108 u16 align_buf[1] = { 0 };
3109 unsigned char *trailing_buf = buf + buflen - 1;
3110
3111 if (write_data) {
3112 memcpy(align_buf, trailing_buf, 1);
3113 writew(le16_to_cpu(align_buf[0]), mmio);
3114 } else {
3115 align_buf[0] = cpu_to_le16(readw(mmio));
3116 memcpy(trailing_buf, align_buf, 1);
3117 }
3118 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119}
3120
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003121/**
3122 * ata_pio_data_xfer - Transfer data by PIO
3123 * @ap: port to read/write
3124 * @buf: data buffer
3125 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04003126 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003127 *
3128 * Transfer data from/to the device data register by PIO.
3129 *
3130 * LOCKING:
3131 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003132 */
3133
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
3135 unsigned int buflen, int write_data)
3136{
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003137 unsigned int words = buflen >> 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003139 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 if (write_data)
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003141 outsw(ap->ioaddr.data_addr, buf, words);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142 else
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003143 insw(ap->ioaddr.data_addr, buf, words);
3144
3145 /* Transfer trailing 1 byte, if any. */
3146 if (unlikely(buflen & 0x01)) {
3147 u16 align_buf[1] = { 0 };
3148 unsigned char *trailing_buf = buf + buflen - 1;
3149
3150 if (write_data) {
3151 memcpy(align_buf, trailing_buf, 1);
3152 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
3153 } else {
3154 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
3155 memcpy(trailing_buf, align_buf, 1);
3156 }
3157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158}
3159
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003160/**
3161 * ata_data_xfer - Transfer data from/to the data register.
3162 * @ap: port to read/write
3163 * @buf: data buffer
3164 * @buflen: buffer length
3165 * @do_write: read/write
3166 *
3167 * Transfer data from/to the device data register.
3168 *
3169 * LOCKING:
3170 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003171 */
3172
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
3174 unsigned int buflen, int do_write)
3175{
Alan Coxa1bd9e62006-01-17 20:53:50 +00003176 /* Make the crap hardware pay the costs not the good stuff */
3177 if (unlikely(ap->flags & ATA_FLAG_IRQ_MASK)) {
3178 unsigned long flags;
3179 local_irq_save(flags);
3180 if (ap->flags & ATA_FLAG_MMIO)
3181 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3182 else
3183 ata_pio_data_xfer(ap, buf, buflen, do_write);
3184 local_irq_restore(flags);
3185 } else {
3186 if (ap->flags & ATA_FLAG_MMIO)
3187 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3188 else
3189 ata_pio_data_xfer(ap, buf, buflen, do_write);
3190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191}
3192
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003193/**
3194 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
3195 * @qc: Command on going
3196 *
3197 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
3198 *
3199 * LOCKING:
3200 * Inherited from caller.
3201 */
3202
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203static void ata_pio_sector(struct ata_queued_cmd *qc)
3204{
3205 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003206 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003207 struct ata_port *ap = qc->ap;
3208 struct page *page;
3209 unsigned int offset;
3210 unsigned char *buf;
3211
3212 if (qc->cursect == (qc->nsect - 1))
Albert Lee14be71f2005-09-27 17:36:35 +08003213 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214
3215 page = sg[qc->cursg].page;
3216 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
3217
3218 /* get the current page and offset */
3219 page = nth_page(page, (offset >> PAGE_SHIFT));
3220 offset %= PAGE_SIZE;
3221
3222 buf = kmap(page) + offset;
3223
3224 qc->cursect++;
3225 qc->cursg_ofs++;
3226
Albert Lee32529e02005-05-26 03:49:42 -04003227 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228 qc->cursg++;
3229 qc->cursg_ofs = 0;
3230 }
3231
3232 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3233
3234 /* do the actual data transfer */
3235 do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3236 ata_data_xfer(ap, buf, ATA_SECT_SIZE, do_write);
3237
3238 kunmap(page);
3239}
3240
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003241/**
3242 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
3243 * @qc: Command on going
3244 * @bytes: number of bytes
3245 *
3246 * Transfer Transfer data from/to the ATAPI device.
3247 *
3248 * LOCKING:
3249 * Inherited from caller.
3250 *
3251 */
3252
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
3254{
3255 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003256 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 struct ata_port *ap = qc->ap;
3258 struct page *page;
3259 unsigned char *buf;
3260 unsigned int offset, count;
3261
Albert Lee563a6e12005-08-12 14:17:50 +08003262 if (qc->curbytes + bytes >= qc->nbytes)
Albert Lee14be71f2005-09-27 17:36:35 +08003263 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264
3265next_sg:
Albert Lee563a6e12005-08-12 14:17:50 +08003266 if (unlikely(qc->cursg >= qc->n_elem)) {
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003267 /*
Albert Lee563a6e12005-08-12 14:17:50 +08003268 * The end of qc->sg is reached and the device expects
3269 * more data to transfer. In order not to overrun qc->sg
3270 * and fulfill length specified in the byte count register,
3271 * - for read case, discard trailing data from the device
3272 * - for write case, padding zero data to the device
3273 */
3274 u16 pad_buf[1] = { 0 };
3275 unsigned int words = bytes >> 1;
3276 unsigned int i;
3277
3278 if (words) /* warning if bytes > 1 */
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003279 printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
Albert Lee563a6e12005-08-12 14:17:50 +08003280 ap->id, bytes);
3281
3282 for (i = 0; i < words; i++)
3283 ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
3284
Albert Lee14be71f2005-09-27 17:36:35 +08003285 ap->hsm_task_state = HSM_ST_LAST;
Albert Lee563a6e12005-08-12 14:17:50 +08003286 return;
3287 }
3288
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003289 sg = &qc->__sg[qc->cursg];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003290
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291 page = sg->page;
3292 offset = sg->offset + qc->cursg_ofs;
3293
3294 /* get the current page and offset */
3295 page = nth_page(page, (offset >> PAGE_SHIFT));
3296 offset %= PAGE_SIZE;
3297
Albert Lee6952df02005-06-06 15:56:03 +08003298 /* don't overrun current sg */
Albert Lee32529e02005-05-26 03:49:42 -04003299 count = min(sg->length - qc->cursg_ofs, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003300
3301 /* don't cross page boundaries */
3302 count = min(count, (unsigned int)PAGE_SIZE - offset);
3303
3304 buf = kmap(page) + offset;
3305
3306 bytes -= count;
3307 qc->curbytes += count;
3308 qc->cursg_ofs += count;
3309
Albert Lee32529e02005-05-26 03:49:42 -04003310 if (qc->cursg_ofs == sg->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311 qc->cursg++;
3312 qc->cursg_ofs = 0;
3313 }
3314
3315 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3316
3317 /* do the actual data transfer */
3318 ata_data_xfer(ap, buf, count, do_write);
3319
3320 kunmap(page);
3321
Albert Lee563a6e12005-08-12 14:17:50 +08003322 if (bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003323 goto next_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324}
3325
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003326/**
3327 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
3328 * @qc: Command on going
3329 *
3330 * Transfer Transfer data from/to the ATAPI device.
3331 *
3332 * LOCKING:
3333 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003334 */
3335
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336static void atapi_pio_bytes(struct ata_queued_cmd *qc)
3337{
3338 struct ata_port *ap = qc->ap;
3339 struct ata_device *dev = qc->dev;
3340 unsigned int ireason, bc_lo, bc_hi, bytes;
3341 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
3342
3343 ap->ops->tf_read(ap, &qc->tf);
3344 ireason = qc->tf.nsect;
3345 bc_lo = qc->tf.lbam;
3346 bc_hi = qc->tf.lbah;
3347 bytes = (bc_hi << 8) | bc_lo;
3348
3349 /* shall be cleared to zero, indicating xfer of data */
3350 if (ireason & (1 << 0))
3351 goto err_out;
3352
3353 /* make sure transfer direction matches expected */
3354 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
3355 if (do_write != i_write)
3356 goto err_out;
3357
3358 __atapi_pio_bytes(qc, bytes);
3359
3360 return;
3361
3362err_out:
3363 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
3364 ap->id, dev->devno);
Tejun Heo11a56d22006-01-23 13:09:36 +09003365 qc->err_mask |= AC_ERR_HSM;
Albert Lee14be71f2005-09-27 17:36:35 +08003366 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367}
3368
3369/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003370 * ata_pio_block - start PIO on a block
3371 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07003372 *
3373 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003374 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 */
3376
3377static void ata_pio_block(struct ata_port *ap)
3378{
3379 struct ata_queued_cmd *qc;
3380 u8 status;
3381
3382 /*
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003383 * This is purely heuristic. This is a fast path.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 * Sometimes when we enter, BSY will be cleared in
3385 * a chk-status or two. If not, the drive is probably seeking
3386 * or something. Snooze for a couple msecs, then
3387 * chk-status again. If still busy, fall back to
Albert Lee14be71f2005-09-27 17:36:35 +08003388 * HSM_ST_POLL state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 */
3390 status = ata_busy_wait(ap, ATA_BUSY, 5);
3391 if (status & ATA_BUSY) {
3392 msleep(2);
3393 status = ata_busy_wait(ap, ATA_BUSY, 10);
3394 if (status & ATA_BUSY) {
Albert Lee14be71f2005-09-27 17:36:35 +08003395 ap->hsm_task_state = HSM_ST_POLL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
3397 return;
3398 }
3399 }
3400
3401 qc = ata_qc_from_tag(ap, ap->active_tag);
Tejun Heoa4631472006-02-11 19:11:13 +09003402 WARN_ON(qc == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403
Albert Leefe79e682005-12-06 11:34:59 +08003404 /* check error */
3405 if (status & (ATA_ERR | ATA_DF)) {
3406 qc->err_mask |= AC_ERR_DEV;
3407 ap->hsm_task_state = HSM_ST_ERR;
3408 return;
3409 }
3410
3411 /* transfer data if any */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 if (is_atapi_taskfile(&qc->tf)) {
Albert Leefe79e682005-12-06 11:34:59 +08003413 /* DRQ=0 means no more data to transfer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003414 if ((status & ATA_DRQ) == 0) {
Albert Lee14be71f2005-09-27 17:36:35 +08003415 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 return;
3417 }
3418
3419 atapi_pio_bytes(qc);
3420 } else {
3421 /* handle BSY=0, DRQ=0 as error */
3422 if ((status & ATA_DRQ) == 0) {
Tejun Heo11a56d22006-01-23 13:09:36 +09003423 qc->err_mask |= AC_ERR_HSM;
Albert Lee14be71f2005-09-27 17:36:35 +08003424 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 return;
3426 }
3427
3428 ata_pio_sector(qc);
3429 }
3430}
3431
3432static void ata_pio_error(struct ata_port *ap)
3433{
3434 struct ata_queued_cmd *qc;
Jeff Garzika7dac442005-10-30 04:44:42 -05003435
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 qc = ata_qc_from_tag(ap, ap->active_tag);
Tejun Heoa4631472006-02-11 19:11:13 +09003437 WARN_ON(qc == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438
Albert Lee0565c262006-02-13 18:55:25 +08003439 if (qc->tf.command != ATA_CMD_PACKET)
3440 printk(KERN_WARNING "ata%u: PIO error\n", ap->id);
3441
Albert Lee1c848982005-12-05 15:40:15 +08003442 /* make sure qc->err_mask is available to
3443 * know what's wrong and recover
3444 */
Tejun Heoa4631472006-02-11 19:11:13 +09003445 WARN_ON(qc->err_mask == 0);
Albert Lee1c848982005-12-05 15:40:15 +08003446
Albert Lee14be71f2005-09-27 17:36:35 +08003447 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448
Albert Leea22e2eb2005-12-05 15:38:02 +08003449 ata_poll_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450}
3451
3452static void ata_pio_task(void *_data)
3453{
3454 struct ata_port *ap = _data;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003455 unsigned long timeout;
3456 int qc_completed;
3457
3458fsm_start:
3459 timeout = 0;
3460 qc_completed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003461
Albert Lee14be71f2005-09-27 17:36:35 +08003462 switch (ap->hsm_task_state) {
3463 case HSM_ST_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 return;
3465
Albert Lee14be71f2005-09-27 17:36:35 +08003466 case HSM_ST:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467 ata_pio_block(ap);
3468 break;
3469
Albert Lee14be71f2005-09-27 17:36:35 +08003470 case HSM_ST_LAST:
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003471 qc_completed = ata_pio_complete(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 break;
3473
Albert Lee14be71f2005-09-27 17:36:35 +08003474 case HSM_ST_POLL:
3475 case HSM_ST_LAST_POLL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476 timeout = ata_pio_poll(ap);
3477 break;
3478
Albert Lee14be71f2005-09-27 17:36:35 +08003479 case HSM_ST_TMOUT:
3480 case HSM_ST_ERR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481 ata_pio_error(ap);
3482 return;
3483 }
3484
3485 if (timeout)
Tejun Heo95064372006-01-23 13:09:37 +09003486 ata_queue_delayed_pio_task(ap, timeout);
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003487 else if (!qc_completed)
3488 goto fsm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489}
3490
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491/**
3492 * ata_qc_timeout - Handle timeout of queued command
3493 * @qc: Command that timed out
3494 *
3495 * Some part of the kernel (currently, only the SCSI layer)
3496 * has noticed that the active command on port @ap has not
3497 * completed after a specified length of time. Handle this
3498 * condition by disabling DMA (if necessary) and completing
3499 * transactions, with error if necessary.
3500 *
3501 * This also handles the case of the "lost interrupt", where
3502 * for some reason (possibly hardware bug, possibly driver bug)
3503 * an interrupt was not delivered to the driver, even though the
3504 * transaction completed successfully.
3505 *
3506 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003507 * Inherited from SCSI layer (none, can sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508 */
3509
3510static void ata_qc_timeout(struct ata_queued_cmd *qc)
3511{
3512 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003513 struct ata_host_set *host_set = ap->host_set;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514 u8 host_stat = 0, drv_stat;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003515 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516
3517 DPRINTK("ENTER\n");
3518
Tejun Heoc18d06f2006-02-02 00:56:10 +09003519 ata_flush_pio_tasks(ap);
3520 ap->hsm_task_state = HSM_ST_IDLE;
3521
Jeff Garzikb8f61532005-08-25 22:01:20 -04003522 spin_lock_irqsave(&host_set->lock, flags);
3523
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524 switch (qc->tf.protocol) {
3525
3526 case ATA_PROT_DMA:
3527 case ATA_PROT_ATAPI_DMA:
3528 host_stat = ap->ops->bmdma_status(ap);
3529
3530 /* before we do anything else, clear DMA-Start bit */
Alan Coxb73fc892005-08-26 16:03:19 +01003531 ap->ops->bmdma_stop(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532
3533 /* fall through */
3534
3535 default:
3536 ata_altstatus(ap);
3537 drv_stat = ata_chk_status(ap);
3538
3539 /* ack bmdma irq events */
3540 ap->ops->irq_clear(ap);
3541
3542 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
3543 ap->id, qc->tf.command, drv_stat, host_stat);
3544
3545 /* complete taskfile transaction */
Albert Leea22e2eb2005-12-05 15:38:02 +08003546 qc->err_mask |= ac_err_mask(drv_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003547 break;
3548 }
Jeff Garzikb8f61532005-08-25 22:01:20 -04003549
3550 spin_unlock_irqrestore(&host_set->lock, flags);
3551
Tejun Heoa72ec4c2006-01-23 13:09:37 +09003552 ata_eh_qc_complete(qc);
3553
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554 DPRINTK("EXIT\n");
3555}
3556
3557/**
3558 * ata_eng_timeout - Handle timeout of queued command
3559 * @ap: Port on which timed-out command is active
3560 *
3561 * Some part of the kernel (currently, only the SCSI layer)
3562 * has noticed that the active command on port @ap has not
3563 * completed after a specified length of time. Handle this
3564 * condition by disabling DMA (if necessary) and completing
3565 * transactions, with error if necessary.
3566 *
3567 * This also handles the case of the "lost interrupt", where
3568 * for some reason (possibly hardware bug, possibly driver bug)
3569 * an interrupt was not delivered to the driver, even though the
3570 * transaction completed successfully.
3571 *
3572 * LOCKING:
3573 * Inherited from SCSI layer (none, can sleep)
3574 */
3575
3576void ata_eng_timeout(struct ata_port *ap)
3577{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003578 DPRINTK("ENTER\n");
3579
Tejun Heof6379022006-02-10 15:10:48 +09003580 ata_qc_timeout(ata_qc_from_tag(ap, ap->active_tag));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582 DPRINTK("EXIT\n");
3583}
3584
3585/**
3586 * ata_qc_new - Request an available ATA command, for queueing
3587 * @ap: Port associated with device @dev
3588 * @dev: Device from whom we request an available command structure
3589 *
3590 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003591 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003592 */
3593
3594static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
3595{
3596 struct ata_queued_cmd *qc = NULL;
3597 unsigned int i;
3598
3599 for (i = 0; i < ATA_MAX_QUEUE; i++)
3600 if (!test_and_set_bit(i, &ap->qactive)) {
3601 qc = ata_qc_from_tag(ap, i);
3602 break;
3603 }
3604
3605 if (qc)
3606 qc->tag = i;
3607
3608 return qc;
3609}
3610
3611/**
3612 * ata_qc_new_init - Request an available ATA command, and initialize it
3613 * @ap: Port associated with device @dev
3614 * @dev: Device from whom we request an available command structure
3615 *
3616 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003617 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618 */
3619
3620struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
3621 struct ata_device *dev)
3622{
3623 struct ata_queued_cmd *qc;
3624
3625 qc = ata_qc_new(ap);
3626 if (qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627 qc->scsicmd = NULL;
3628 qc->ap = ap;
3629 qc->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05003631 ata_qc_reinit(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632 }
3633
3634 return qc;
3635}
3636
Linus Torvalds1da177e2005-04-16 15:20:36 -07003637/**
3638 * ata_qc_free - free unused ata_queued_cmd
3639 * @qc: Command to complete
3640 *
3641 * Designed to free unused ata_queued_cmd object
3642 * in case something prevents using it.
3643 *
3644 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003645 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003646 */
3647void ata_qc_free(struct ata_queued_cmd *qc)
3648{
Tejun Heo4ba946e2006-01-23 13:09:36 +09003649 struct ata_port *ap = qc->ap;
3650 unsigned int tag;
3651
Tejun Heoa4631472006-02-11 19:11:13 +09003652 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003653
Tejun Heo4ba946e2006-01-23 13:09:36 +09003654 qc->flags = 0;
3655 tag = qc->tag;
3656 if (likely(ata_tag_valid(tag))) {
3657 if (tag == ap->active_tag)
3658 ap->active_tag = ATA_TAG_POISON;
3659 qc->tag = ATA_TAG_POISON;
3660 clear_bit(tag, &ap->qactive);
3661 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662}
3663
Tejun Heo76014422006-02-11 15:13:49 +09003664void __ata_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665{
Tejun Heoa4631472006-02-11 19:11:13 +09003666 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
3667 WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003668
3669 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
3670 ata_sg_clean(qc);
3671
Albert Lee3f3791d2005-08-16 14:25:38 +08003672 /* atapi: mark qc as inactive to prevent the interrupt handler
3673 * from completing the command twice later, before the error handler
3674 * is called. (when rc != 0 and atapi request sense is needed)
3675 */
3676 qc->flags &= ~ATA_QCFLAG_ACTIVE;
3677
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678 /* call completion callback */
Tejun Heo77853bf2006-01-23 13:09:36 +09003679 qc->complete_fn(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680}
3681
3682static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
3683{
3684 struct ata_port *ap = qc->ap;
3685
3686 switch (qc->tf.protocol) {
3687 case ATA_PROT_DMA:
3688 case ATA_PROT_ATAPI_DMA:
3689 return 1;
3690
3691 case ATA_PROT_ATAPI:
3692 case ATA_PROT_PIO:
3693 case ATA_PROT_PIO_MULT:
3694 if (ap->flags & ATA_FLAG_PIO_DMA)
3695 return 1;
3696
3697 /* fall through */
3698
3699 default:
3700 return 0;
3701 }
3702
3703 /* never reached */
3704}
3705
3706/**
3707 * ata_qc_issue - issue taskfile to device
3708 * @qc: command to issue to device
3709 *
3710 * Prepare an ATA command to submission to device.
3711 * This includes mapping the data into a DMA-able
3712 * area, filling in the S/G table, and finally
3713 * writing the taskfile to hardware, starting the command.
3714 *
3715 * LOCKING:
3716 * spin_lock_irqsave(host_set lock)
3717 *
3718 * RETURNS:
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003719 * Zero on success, AC_ERR_* mask on failure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720 */
3721
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003722unsigned int ata_qc_issue(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003723{
3724 struct ata_port *ap = qc->ap;
3725
3726 if (ata_should_dma_map(qc)) {
3727 if (qc->flags & ATA_QCFLAG_SG) {
3728 if (ata_sg_setup(qc))
Tejun Heo8e436af2006-01-23 13:09:36 +09003729 goto sg_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
3731 if (ata_sg_setup_one(qc))
Tejun Heo8e436af2006-01-23 13:09:36 +09003732 goto sg_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003733 }
3734 } else {
3735 qc->flags &= ~ATA_QCFLAG_DMAMAP;
3736 }
3737
3738 ap->ops->qc_prep(qc);
3739
3740 qc->ap->active_tag = qc->tag;
3741 qc->flags |= ATA_QCFLAG_ACTIVE;
3742
3743 return ap->ops->qc_issue(qc);
3744
Tejun Heo8e436af2006-01-23 13:09:36 +09003745sg_err:
3746 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003747 return AC_ERR_SYSTEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003748}
3749
Edward Falk0baab862005-06-02 18:17:13 -04003750
Linus Torvalds1da177e2005-04-16 15:20:36 -07003751/**
3752 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
3753 * @qc: command to issue to device
3754 *
3755 * Using various libata functions and hooks, this function
3756 * starts an ATA command. ATA commands are grouped into
3757 * classes called "protocols", and issuing each type of protocol
3758 * is slightly different.
3759 *
Edward Falk0baab862005-06-02 18:17:13 -04003760 * May be used as the qc_issue() entry in ata_port_operations.
3761 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762 * LOCKING:
3763 * spin_lock_irqsave(host_set lock)
3764 *
3765 * RETURNS:
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003766 * Zero on success, AC_ERR_* mask on failure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767 */
3768
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003769unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770{
3771 struct ata_port *ap = qc->ap;
3772
3773 ata_dev_select(ap, qc->dev->devno, 1, 0);
3774
3775 switch (qc->tf.protocol) {
3776 case ATA_PROT_NODATA:
Jeff Garzike5338252005-10-30 21:37:17 -05003777 ata_tf_to_host(ap, &qc->tf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003778 break;
3779
3780 case ATA_PROT_DMA:
3781 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3782 ap->ops->bmdma_setup(qc); /* set up bmdma */
3783 ap->ops->bmdma_start(qc); /* initiate bmdma */
3784 break;
3785
3786 case ATA_PROT_PIO: /* load tf registers, initiate polling pio */
3787 ata_qc_set_polling(qc);
Jeff Garzike5338252005-10-30 21:37:17 -05003788 ata_tf_to_host(ap, &qc->tf);
Albert Lee14be71f2005-09-27 17:36:35 +08003789 ap->hsm_task_state = HSM_ST;
Tejun Heo95064372006-01-23 13:09:37 +09003790 ata_queue_pio_task(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791 break;
3792
3793 case ATA_PROT_ATAPI:
3794 ata_qc_set_polling(qc);
Jeff Garzike5338252005-10-30 21:37:17 -05003795 ata_tf_to_host(ap, &qc->tf);
Tejun Heo95064372006-01-23 13:09:37 +09003796 ata_queue_packet_task(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797 break;
3798
3799 case ATA_PROT_ATAPI_NODATA:
Tejun Heoc1389502005-08-22 14:59:24 +09003800 ap->flags |= ATA_FLAG_NOINTR;
Jeff Garzike5338252005-10-30 21:37:17 -05003801 ata_tf_to_host(ap, &qc->tf);
Tejun Heo95064372006-01-23 13:09:37 +09003802 ata_queue_packet_task(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003803 break;
3804
3805 case ATA_PROT_ATAPI_DMA:
Tejun Heoc1389502005-08-22 14:59:24 +09003806 ap->flags |= ATA_FLAG_NOINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3808 ap->ops->bmdma_setup(qc); /* set up bmdma */
Tejun Heo95064372006-01-23 13:09:37 +09003809 ata_queue_packet_task(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810 break;
3811
3812 default:
3813 WARN_ON(1);
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003814 return AC_ERR_SYSTEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815 }
3816
3817 return 0;
3818}
3819
3820/**
Edward Falk0baab862005-06-02 18:17:13 -04003821 * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822 * @qc: Info associated with this ATA transaction.
3823 *
3824 * LOCKING:
3825 * spin_lock_irqsave(host_set lock)
3826 */
3827
3828static void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc)
3829{
3830 struct ata_port *ap = qc->ap;
3831 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3832 u8 dmactl;
3833 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3834
3835 /* load PRD table addr. */
3836 mb(); /* make sure PRD table writes are visible to controller */
3837 writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
3838
3839 /* specify data direction, triple-check start bit is clear */
3840 dmactl = readb(mmio + ATA_DMA_CMD);
3841 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3842 if (!rw)
3843 dmactl |= ATA_DMA_WR;
3844 writeb(dmactl, mmio + ATA_DMA_CMD);
3845
3846 /* issue r/w command */
3847 ap->ops->exec_command(ap, &qc->tf);
3848}
3849
3850/**
Alan Coxb73fc892005-08-26 16:03:19 +01003851 * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852 * @qc: Info associated with this ATA transaction.
3853 *
3854 * LOCKING:
3855 * spin_lock_irqsave(host_set lock)
3856 */
3857
3858static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
3859{
3860 struct ata_port *ap = qc->ap;
3861 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3862 u8 dmactl;
3863
3864 /* start host DMA transaction */
3865 dmactl = readb(mmio + ATA_DMA_CMD);
3866 writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
3867
3868 /* Strictly, one may wish to issue a readb() here, to
3869 * flush the mmio write. However, control also passes
3870 * to the hardware at this point, and it will interrupt
3871 * us when we are to resume control. So, in effect,
3872 * we don't care when the mmio write flushes.
3873 * Further, a read of the DMA status register _immediately_
3874 * following the write may not be what certain flaky hardware
3875 * is expected, so I think it is best to not add a readb()
3876 * without first all the MMIO ATA cards/mobos.
3877 * Or maybe I'm just being paranoid.
3878 */
3879}
3880
3881/**
3882 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
3883 * @qc: Info associated with this ATA transaction.
3884 *
3885 * LOCKING:
3886 * spin_lock_irqsave(host_set lock)
3887 */
3888
3889static void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
3890{
3891 struct ata_port *ap = qc->ap;
3892 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3893 u8 dmactl;
3894
3895 /* load PRD table addr. */
3896 outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
3897
3898 /* specify data direction, triple-check start bit is clear */
3899 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3900 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3901 if (!rw)
3902 dmactl |= ATA_DMA_WR;
3903 outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3904
3905 /* issue r/w command */
3906 ap->ops->exec_command(ap, &qc->tf);
3907}
3908
3909/**
3910 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
3911 * @qc: Info associated with this ATA transaction.
3912 *
3913 * LOCKING:
3914 * spin_lock_irqsave(host_set lock)
3915 */
3916
3917static void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
3918{
3919 struct ata_port *ap = qc->ap;
3920 u8 dmactl;
3921
3922 /* start host DMA transaction */
3923 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3924 outb(dmactl | ATA_DMA_START,
3925 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3926}
3927
Edward Falk0baab862005-06-02 18:17:13 -04003928
3929/**
3930 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
3931 * @qc: Info associated with this ATA transaction.
3932 *
3933 * Writes the ATA_DMA_START flag to the DMA command register.
3934 *
3935 * May be used as the bmdma_start() entry in ata_port_operations.
3936 *
3937 * LOCKING:
3938 * spin_lock_irqsave(host_set lock)
3939 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940void ata_bmdma_start(struct ata_queued_cmd *qc)
3941{
3942 if (qc->ap->flags & ATA_FLAG_MMIO)
3943 ata_bmdma_start_mmio(qc);
3944 else
3945 ata_bmdma_start_pio(qc);
3946}
3947
Edward Falk0baab862005-06-02 18:17:13 -04003948
3949/**
3950 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
3951 * @qc: Info associated with this ATA transaction.
3952 *
3953 * Writes address of PRD table to device's PRD Table Address
3954 * register, sets the DMA control register, and calls
3955 * ops->exec_command() to start the transfer.
3956 *
3957 * May be used as the bmdma_setup() entry in ata_port_operations.
3958 *
3959 * LOCKING:
3960 * spin_lock_irqsave(host_set lock)
3961 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962void ata_bmdma_setup(struct ata_queued_cmd *qc)
3963{
3964 if (qc->ap->flags & ATA_FLAG_MMIO)
3965 ata_bmdma_setup_mmio(qc);
3966 else
3967 ata_bmdma_setup_pio(qc);
3968}
3969
Edward Falk0baab862005-06-02 18:17:13 -04003970
3971/**
3972 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
Jeff Garzikdecc6d02005-06-02 18:42:33 -04003973 * @ap: Port associated with this ATA transaction.
Edward Falk0baab862005-06-02 18:17:13 -04003974 *
3975 * Clear interrupt and error flags in DMA status register.
3976 *
3977 * May be used as the irq_clear() entry in ata_port_operations.
3978 *
3979 * LOCKING:
3980 * spin_lock_irqsave(host_set lock)
3981 */
3982
Linus Torvalds1da177e2005-04-16 15:20:36 -07003983void ata_bmdma_irq_clear(struct ata_port *ap)
3984{
3985 if (ap->flags & ATA_FLAG_MMIO) {
3986 void __iomem *mmio = ((void __iomem *) ap->ioaddr.bmdma_addr) + ATA_DMA_STATUS;
3987 writeb(readb(mmio), mmio);
3988 } else {
3989 unsigned long addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
3990 outb(inb(addr), addr);
3991 }
3992
3993}
3994
Edward Falk0baab862005-06-02 18:17:13 -04003995
3996/**
3997 * ata_bmdma_status - Read PCI IDE BMDMA status
Jeff Garzikdecc6d02005-06-02 18:42:33 -04003998 * @ap: Port associated with this ATA transaction.
Edward Falk0baab862005-06-02 18:17:13 -04003999 *
4000 * Read and return BMDMA status register.
4001 *
4002 * May be used as the bmdma_status() entry in ata_port_operations.
4003 *
4004 * LOCKING:
4005 * spin_lock_irqsave(host_set lock)
4006 */
4007
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008u8 ata_bmdma_status(struct ata_port *ap)
4009{
4010 u8 host_stat;
4011 if (ap->flags & ATA_FLAG_MMIO) {
4012 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4013 host_stat = readb(mmio + ATA_DMA_STATUS);
4014 } else
Albert Leeee500aa2005-09-27 17:34:38 +08004015 host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004016 return host_stat;
4017}
4018
Edward Falk0baab862005-06-02 18:17:13 -04004019
4020/**
4021 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
Alan Coxb73fc892005-08-26 16:03:19 +01004022 * @qc: Command we are ending DMA for
Edward Falk0baab862005-06-02 18:17:13 -04004023 *
4024 * Clears the ATA_DMA_START flag in the dma control register
4025 *
4026 * May be used as the bmdma_stop() entry in ata_port_operations.
4027 *
4028 * LOCKING:
4029 * spin_lock_irqsave(host_set lock)
4030 */
4031
Alan Coxb73fc892005-08-26 16:03:19 +01004032void ata_bmdma_stop(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033{
Alan Coxb73fc892005-08-26 16:03:19 +01004034 struct ata_port *ap = qc->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035 if (ap->flags & ATA_FLAG_MMIO) {
4036 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4037
4038 /* clear start/stop bit */
4039 writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
4040 mmio + ATA_DMA_CMD);
4041 } else {
4042 /* clear start/stop bit */
4043 outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
4044 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4045 }
4046
4047 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
4048 ata_altstatus(ap); /* dummy read */
4049}
4050
4051/**
4052 * ata_host_intr - Handle host interrupt for given (port, task)
4053 * @ap: Port on which interrupt arrived (possibly...)
4054 * @qc: Taskfile currently active in engine
4055 *
4056 * Handle host interrupt for given queued command. Currently,
4057 * only DMA interrupts are handled. All other commands are
4058 * handled via polling with interrupts disabled (nIEN bit).
4059 *
4060 * LOCKING:
4061 * spin_lock_irqsave(host_set lock)
4062 *
4063 * RETURNS:
4064 * One if interrupt was handled, zero if not (shared irq).
4065 */
4066
4067inline unsigned int ata_host_intr (struct ata_port *ap,
4068 struct ata_queued_cmd *qc)
4069{
4070 u8 status, host_stat;
4071
4072 switch (qc->tf.protocol) {
4073
4074 case ATA_PROT_DMA:
4075 case ATA_PROT_ATAPI_DMA:
4076 case ATA_PROT_ATAPI:
4077 /* check status of DMA engine */
4078 host_stat = ap->ops->bmdma_status(ap);
4079 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
4080
4081 /* if it's not our irq... */
4082 if (!(host_stat & ATA_DMA_INTR))
4083 goto idle_irq;
4084
4085 /* before we do anything else, clear DMA-Start bit */
Alan Coxb73fc892005-08-26 16:03:19 +01004086 ap->ops->bmdma_stop(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087
4088 /* fall through */
4089
4090 case ATA_PROT_ATAPI_NODATA:
4091 case ATA_PROT_NODATA:
4092 /* check altstatus */
4093 status = ata_altstatus(ap);
4094 if (status & ATA_BUSY)
4095 goto idle_irq;
4096
4097 /* check main status, clearing INTRQ */
4098 status = ata_chk_status(ap);
4099 if (unlikely(status & ATA_BUSY))
4100 goto idle_irq;
4101 DPRINTK("ata%u: protocol %d (dev_stat 0x%X)\n",
4102 ap->id, qc->tf.protocol, status);
4103
4104 /* ack bmdma irq events */
4105 ap->ops->irq_clear(ap);
4106
4107 /* complete taskfile transaction */
Albert Leea22e2eb2005-12-05 15:38:02 +08004108 qc->err_mask |= ac_err_mask(status);
4109 ata_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110 break;
4111
4112 default:
4113 goto idle_irq;
4114 }
4115
4116 return 1; /* irq handled */
4117
4118idle_irq:
4119 ap->stats.idle_irq++;
4120
4121#ifdef ATA_IRQ_TRAP
4122 if ((ap->stats.idle_irq % 1000) == 0) {
4123 handled = 1;
4124 ata_irq_ack(ap, 0); /* debug trap */
4125 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
4126 }
4127#endif
4128 return 0; /* irq not handled */
4129}
4130
4131/**
4132 * ata_interrupt - Default ATA host interrupt handler
Jeff Garzik0cba6322005-05-30 19:49:12 -04004133 * @irq: irq line (unused)
4134 * @dev_instance: pointer to our ata_host_set information structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135 * @regs: unused
4136 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004137 * Default interrupt handler for PCI IDE devices. Calls
4138 * ata_host_intr() for each port that is not disabled.
4139 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004141 * Obtains host_set lock during operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142 *
4143 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004144 * IRQ_NONE or IRQ_HANDLED.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145 */
4146
4147irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
4148{
4149 struct ata_host_set *host_set = dev_instance;
4150 unsigned int i;
4151 unsigned int handled = 0;
4152 unsigned long flags;
4153
4154 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
4155 spin_lock_irqsave(&host_set->lock, flags);
4156
4157 for (i = 0; i < host_set->n_ports; i++) {
4158 struct ata_port *ap;
4159
4160 ap = host_set->ports[i];
Tejun Heoc1389502005-08-22 14:59:24 +09004161 if (ap &&
4162 !(ap->flags & (ATA_FLAG_PORT_DISABLED | ATA_FLAG_NOINTR))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163 struct ata_queued_cmd *qc;
4164
4165 qc = ata_qc_from_tag(ap, ap->active_tag);
Albert Lee21b1ed72005-04-29 17:34:59 +08004166 if (qc && (!(qc->tf.ctl & ATA_NIEN)) &&
4167 (qc->flags & ATA_QCFLAG_ACTIVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168 handled |= ata_host_intr(ap, qc);
4169 }
4170 }
4171
4172 spin_unlock_irqrestore(&host_set->lock, flags);
4173
4174 return IRQ_RETVAL(handled);
4175}
4176
4177/**
4178 * atapi_packet_task - Write CDB bytes to hardware
4179 * @_data: Port to which ATAPI device is attached.
4180 *
4181 * When device has indicated its readiness to accept
4182 * a CDB, this function is called. Send the CDB.
4183 * If DMA is to be performed, exit immediately.
4184 * Otherwise, we are in polling mode, so poll
4185 * status under operation succeeds or fails.
4186 *
4187 * LOCKING:
4188 * Kernel thread context (may sleep)
4189 */
4190
4191static void atapi_packet_task(void *_data)
4192{
4193 struct ata_port *ap = _data;
4194 struct ata_queued_cmd *qc;
4195 u8 status;
4196
4197 qc = ata_qc_from_tag(ap, ap->active_tag);
Tejun Heoa4631472006-02-11 19:11:13 +09004198 WARN_ON(qc == NULL);
4199 WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200
4201 /* sleep-wait for BSY to clear */
4202 DPRINTK("busy wait\n");
Albert Leed8fe4522005-12-05 15:42:17 +08004203 if (ata_busy_sleep(ap, ATA_TMOUT_CDB_QUICK, ATA_TMOUT_CDB)) {
Tejun Heo11a56d22006-01-23 13:09:36 +09004204 qc->err_mask |= AC_ERR_TIMEOUT;
Albert Leed8fe4522005-12-05 15:42:17 +08004205 goto err_out;
4206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207
4208 /* make sure DRQ is set */
4209 status = ata_chk_status(ap);
Albert Leed8fe4522005-12-05 15:42:17 +08004210 if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ) {
Tejun Heo11a56d22006-01-23 13:09:36 +09004211 qc->err_mask |= AC_ERR_HSM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212 goto err_out;
Albert Leed8fe4522005-12-05 15:42:17 +08004213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214
4215 /* send SCSI cdb */
4216 DPRINTK("send cdb\n");
Tejun Heo6e7846e2006-02-12 23:32:58 +09004217 WARN_ON(qc->dev->cdb_len < 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218
Tejun Heoc1389502005-08-22 14:59:24 +09004219 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA ||
4220 qc->tf.protocol == ATA_PROT_ATAPI_NODATA) {
4221 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004222
Tejun Heoc1389502005-08-22 14:59:24 +09004223 /* Once we're done issuing command and kicking bmdma,
4224 * irq handler takes over. To not lose irq, we need
4225 * to clear NOINTR flag before sending cdb, but
4226 * interrupt handler shouldn't be invoked before we're
4227 * finished. Hence, the following locking.
4228 */
4229 spin_lock_irqsave(&ap->host_set->lock, flags);
4230 ap->flags &= ~ATA_FLAG_NOINTR;
Tejun Heo6e7846e2006-02-12 23:32:58 +09004231 ata_data_xfer(ap, qc->cdb, qc->dev->cdb_len, 1);
Tejun Heoc1389502005-08-22 14:59:24 +09004232 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA)
4233 ap->ops->bmdma_start(qc); /* initiate bmdma */
4234 spin_unlock_irqrestore(&ap->host_set->lock, flags);
4235 } else {
Tejun Heo6e7846e2006-02-12 23:32:58 +09004236 ata_data_xfer(ap, qc->cdb, qc->dev->cdb_len, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237
Tejun Heoc1389502005-08-22 14:59:24 +09004238 /* PIO commands are handled by polling */
Albert Lee14be71f2005-09-27 17:36:35 +08004239 ap->hsm_task_state = HSM_ST;
Tejun Heo95064372006-01-23 13:09:37 +09004240 ata_queue_pio_task(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241 }
4242
4243 return;
4244
4245err_out:
Albert Leea22e2eb2005-12-05 15:38:02 +08004246 ata_poll_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004247}
4248
Edward Falk0baab862005-06-02 18:17:13 -04004249
Jens Axboe9b847542006-01-06 09:28:07 +01004250/*
4251 * Execute a 'simple' command, that only consists of the opcode 'cmd' itself,
4252 * without filling any other registers
4253 */
4254static int ata_do_simple_cmd(struct ata_port *ap, struct ata_device *dev,
4255 u8 cmd)
4256{
4257 struct ata_taskfile tf;
4258 int err;
4259
4260 ata_tf_init(ap, &tf, dev->devno);
4261
4262 tf.command = cmd;
4263 tf.flags |= ATA_TFLAG_DEVICE;
4264 tf.protocol = ATA_PROT_NODATA;
4265
4266 err = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
4267 if (err)
4268 printk(KERN_ERR "%s: ata command failed: %d\n",
4269 __FUNCTION__, err);
4270
4271 return err;
4272}
4273
4274static int ata_flush_cache(struct ata_port *ap, struct ata_device *dev)
4275{
4276 u8 cmd;
4277
4278 if (!ata_try_flush_cache(dev))
4279 return 0;
4280
4281 if (ata_id_has_flush_ext(dev->id))
4282 cmd = ATA_CMD_FLUSH_EXT;
4283 else
4284 cmd = ATA_CMD_FLUSH;
4285
4286 return ata_do_simple_cmd(ap, dev, cmd);
4287}
4288
4289static int ata_standby_drive(struct ata_port *ap, struct ata_device *dev)
4290{
4291 return ata_do_simple_cmd(ap, dev, ATA_CMD_STANDBYNOW1);
4292}
4293
4294static int ata_start_drive(struct ata_port *ap, struct ata_device *dev)
4295{
4296 return ata_do_simple_cmd(ap, dev, ATA_CMD_IDLEIMMEDIATE);
4297}
4298
4299/**
4300 * ata_device_resume - wakeup a previously suspended devices
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004301 * @ap: port the device is connected to
4302 * @dev: the device to resume
Jens Axboe9b847542006-01-06 09:28:07 +01004303 *
4304 * Kick the drive back into action, by sending it an idle immediate
4305 * command and making sure its transfer mode matches between drive
4306 * and host.
4307 *
4308 */
4309int ata_device_resume(struct ata_port *ap, struct ata_device *dev)
4310{
4311 if (ap->flags & ATA_FLAG_SUSPENDED) {
4312 ap->flags &= ~ATA_FLAG_SUSPENDED;
4313 ata_set_mode(ap);
4314 }
4315 if (!ata_dev_present(dev))
4316 return 0;
4317 if (dev->class == ATA_DEV_ATA)
4318 ata_start_drive(ap, dev);
4319
4320 return 0;
4321}
4322
4323/**
4324 * ata_device_suspend - prepare a device for suspend
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004325 * @ap: port the device is connected to
4326 * @dev: the device to suspend
Jens Axboe9b847542006-01-06 09:28:07 +01004327 *
4328 * Flush the cache on the drive, if appropriate, then issue a
4329 * standbynow command.
Jens Axboe9b847542006-01-06 09:28:07 +01004330 */
4331int ata_device_suspend(struct ata_port *ap, struct ata_device *dev)
4332{
4333 if (!ata_dev_present(dev))
4334 return 0;
4335 if (dev->class == ATA_DEV_ATA)
4336 ata_flush_cache(ap, dev);
4337
4338 ata_standby_drive(ap, dev);
4339 ap->flags |= ATA_FLAG_SUSPENDED;
4340 return 0;
4341}
4342
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004343/**
4344 * ata_port_start - Set port up for dma.
4345 * @ap: Port to initialize
4346 *
4347 * Called just after data structures for each port are
4348 * initialized. Allocates space for PRD table.
4349 *
4350 * May be used as the port_start() entry in ata_port_operations.
4351 *
4352 * LOCKING:
4353 * Inherited from caller.
4354 */
4355
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356int ata_port_start (struct ata_port *ap)
4357{
4358 struct device *dev = ap->host_set->dev;
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004359 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360
4361 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
4362 if (!ap->prd)
4363 return -ENOMEM;
4364
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004365 rc = ata_pad_alloc(ap, dev);
4366 if (rc) {
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004367 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004368 return rc;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004369 }
4370
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
4372
4373 return 0;
4374}
4375
Edward Falk0baab862005-06-02 18:17:13 -04004376
4377/**
4378 * ata_port_stop - Undo ata_port_start()
4379 * @ap: Port to shut down
4380 *
4381 * Frees the PRD table.
4382 *
4383 * May be used as the port_stop() entry in ata_port_operations.
4384 *
4385 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004386 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04004387 */
4388
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389void ata_port_stop (struct ata_port *ap)
4390{
4391 struct device *dev = ap->host_set->dev;
4392
4393 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004394 ata_pad_free(ap, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395}
4396
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04004397void ata_host_stop (struct ata_host_set *host_set)
4398{
4399 if (host_set->mmio_base)
4400 iounmap(host_set->mmio_base);
4401}
4402
4403
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404/**
4405 * ata_host_remove - Unregister SCSI host structure with upper layers
4406 * @ap: Port to unregister
4407 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
4408 *
4409 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004410 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411 */
4412
4413static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
4414{
4415 struct Scsi_Host *sh = ap->host;
4416
4417 DPRINTK("ENTER\n");
4418
4419 if (do_unregister)
4420 scsi_remove_host(sh);
4421
4422 ap->ops->port_stop(ap);
4423}
4424
4425/**
4426 * ata_host_init - Initialize an ata_port structure
4427 * @ap: Structure to initialize
4428 * @host: associated SCSI mid-layer structure
4429 * @host_set: Collection of hosts to which @ap belongs
4430 * @ent: Probe information provided by low-level driver
4431 * @port_no: Port number associated with this ata_port
4432 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004433 * Initialize a new ata_port structure, and its associated
4434 * scsi_host.
4435 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004437 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438 */
4439
4440static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
4441 struct ata_host_set *host_set,
Jeff Garzik057ace52005-10-22 14:27:05 -04004442 const struct ata_probe_ent *ent, unsigned int port_no)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443{
4444 unsigned int i;
4445
4446 host->max_id = 16;
4447 host->max_lun = 1;
4448 host->max_channel = 1;
4449 host->unique_id = ata_unique_id++;
4450 host->max_cmd_len = 12;
Christoph Hellwig12413192005-06-11 01:05:01 +02004451
Linus Torvalds1da177e2005-04-16 15:20:36 -07004452 ap->flags = ATA_FLAG_PORT_DISABLED;
4453 ap->id = host->unique_id;
4454 ap->host = host;
4455 ap->ctl = ATA_DEVCTL_OBS;
4456 ap->host_set = host_set;
4457 ap->port_no = port_no;
4458 ap->hard_port_no =
4459 ent->legacy_mode ? ent->hard_port_no : port_no;
4460 ap->pio_mask = ent->pio_mask;
4461 ap->mwdma_mask = ent->mwdma_mask;
4462 ap->udma_mask = ent->udma_mask;
4463 ap->flags |= ent->host_flags;
4464 ap->ops = ent->port_ops;
4465 ap->cbl = ATA_CBL_NONE;
4466 ap->active_tag = ATA_TAG_POISON;
4467 ap->last_ctl = 0xFF;
4468
4469 INIT_WORK(&ap->packet_task, atapi_packet_task, ap);
4470 INIT_WORK(&ap->pio_task, ata_pio_task, ap);
Tejun Heoa72ec4c2006-01-23 13:09:37 +09004471 INIT_LIST_HEAD(&ap->eh_done_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472
4473 for (i = 0; i < ATA_MAX_DEVICES; i++)
4474 ap->device[i].devno = i;
4475
4476#ifdef ATA_IRQ_TRAP
4477 ap->stats.unhandled_irq = 1;
4478 ap->stats.idle_irq = 1;
4479#endif
4480
4481 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
4482}
4483
4484/**
4485 * ata_host_add - Attach low-level ATA driver to system
4486 * @ent: Information provided by low-level driver
4487 * @host_set: Collections of ports to which we add
4488 * @port_no: Port number associated with this host
4489 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004490 * Attach low-level ATA driver to system.
4491 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004493 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494 *
4495 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004496 * New ata_port on success, for NULL on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497 */
4498
Jeff Garzik057ace52005-10-22 14:27:05 -04004499static struct ata_port * ata_host_add(const struct ata_probe_ent *ent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 struct ata_host_set *host_set,
4501 unsigned int port_no)
4502{
4503 struct Scsi_Host *host;
4504 struct ata_port *ap;
4505 int rc;
4506
4507 DPRINTK("ENTER\n");
4508 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
4509 if (!host)
4510 return NULL;
4511
4512 ap = (struct ata_port *) &host->hostdata[0];
4513
4514 ata_host_init(ap, host, host_set, ent, port_no);
4515
4516 rc = ap->ops->port_start(ap);
4517 if (rc)
4518 goto err_out;
4519
4520 return ap;
4521
4522err_out:
4523 scsi_host_put(host);
4524 return NULL;
4525}
4526
4527/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004528 * ata_device_add - Register hardware device with ATA and SCSI layers
4529 * @ent: Probe information describing hardware device to be registered
4530 *
4531 * This function processes the information provided in the probe
4532 * information struct @ent, allocates the necessary ATA and SCSI
4533 * host information structures, initializes them, and registers
4534 * everything with requisite kernel subsystems.
4535 *
4536 * This function requests irqs, probes the ATA bus, and probes
4537 * the SCSI bus.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538 *
4539 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004540 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541 *
4542 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004543 * Number of ports registered. Zero on error (no ports registered).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544 */
4545
Jeff Garzik057ace52005-10-22 14:27:05 -04004546int ata_device_add(const struct ata_probe_ent *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547{
4548 unsigned int count = 0, i;
4549 struct device *dev = ent->dev;
4550 struct ata_host_set *host_set;
4551
4552 DPRINTK("ENTER\n");
4553 /* alloc a container for our list of ATA ports (buses) */
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004554 host_set = kzalloc(sizeof(struct ata_host_set) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
4556 if (!host_set)
4557 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558 spin_lock_init(&host_set->lock);
4559
4560 host_set->dev = dev;
4561 host_set->n_ports = ent->n_ports;
4562 host_set->irq = ent->irq;
4563 host_set->mmio_base = ent->mmio_base;
4564 host_set->private_data = ent->private_data;
4565 host_set->ops = ent->port_ops;
4566
4567 /* register each port bound to this device */
4568 for (i = 0; i < ent->n_ports; i++) {
4569 struct ata_port *ap;
4570 unsigned long xfer_mode_mask;
4571
4572 ap = ata_host_add(ent, host_set, i);
4573 if (!ap)
4574 goto err_out;
4575
4576 host_set->ports[i] = ap;
4577 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
4578 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
4579 (ap->pio_mask << ATA_SHIFT_PIO);
4580
4581 /* print per-port info to dmesg */
4582 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
4583 "bmdma 0x%lX irq %lu\n",
4584 ap->id,
4585 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
4586 ata_mode_string(xfer_mode_mask),
4587 ap->ioaddr.cmd_addr,
4588 ap->ioaddr.ctl_addr,
4589 ap->ioaddr.bmdma_addr,
4590 ent->irq);
4591
4592 ata_chk_status(ap);
4593 host_set->ops->irq_clear(ap);
4594 count++;
4595 }
4596
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004597 if (!count)
4598 goto err_free_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599
4600 /* obtain irq, that is shared between channels */
4601 if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
4602 DRV_NAME, host_set))
4603 goto err_out;
4604
4605 /* perform each probe synchronously */
4606 DPRINTK("probe begin\n");
4607 for (i = 0; i < count; i++) {
4608 struct ata_port *ap;
4609 int rc;
4610
4611 ap = host_set->ports[i];
4612
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004613 DPRINTK("ata%u: bus probe begin\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614 rc = ata_bus_probe(ap);
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004615 DPRINTK("ata%u: bus probe end\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616
4617 if (rc) {
4618 /* FIXME: do something useful here?
4619 * Current libata behavior will
4620 * tear down everything when
4621 * the module is removed
4622 * or the h/w is unplugged.
4623 */
4624 }
4625
4626 rc = scsi_add_host(ap->host, dev);
4627 if (rc) {
4628 printk(KERN_ERR "ata%u: scsi_add_host failed\n",
4629 ap->id);
4630 /* FIXME: do something useful here */
4631 /* FIXME: handle unconditional calls to
4632 * scsi_scan_host and ata_host_remove, below,
4633 * at the very least
4634 */
4635 }
4636 }
4637
4638 /* probes are done, now scan each port's disk(s) */
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004639 DPRINTK("host probe begin\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640 for (i = 0; i < count; i++) {
4641 struct ata_port *ap = host_set->ports[i];
4642
Jeff Garzik644dd0c2005-10-03 15:55:19 -04004643 ata_scsi_scan_host(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004644 }
4645
4646 dev_set_drvdata(dev, host_set);
4647
4648 VPRINTK("EXIT, returning %u\n", ent->n_ports);
4649 return ent->n_ports; /* success */
4650
4651err_out:
4652 for (i = 0; i < count; i++) {
4653 ata_host_remove(host_set->ports[i], 1);
4654 scsi_host_put(host_set->ports[i]->host);
4655 }
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004656err_free_ret:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657 kfree(host_set);
4658 VPRINTK("EXIT, returning 0\n");
4659 return 0;
4660}
4661
4662/**
Alan Cox17b14452005-09-15 15:44:00 +01004663 * ata_host_set_remove - PCI layer callback for device removal
4664 * @host_set: ATA host set that was removed
4665 *
4666 * Unregister all objects associated with this host set. Free those
4667 * objects.
4668 *
4669 * LOCKING:
4670 * Inherited from calling layer (may sleep).
4671 */
4672
Alan Cox17b14452005-09-15 15:44:00 +01004673void ata_host_set_remove(struct ata_host_set *host_set)
4674{
4675 struct ata_port *ap;
4676 unsigned int i;
4677
4678 for (i = 0; i < host_set->n_ports; i++) {
4679 ap = host_set->ports[i];
4680 scsi_remove_host(ap->host);
4681 }
4682
4683 free_irq(host_set->irq, host_set);
4684
4685 for (i = 0; i < host_set->n_ports; i++) {
4686 ap = host_set->ports[i];
4687
4688 ata_scsi_release(ap->host);
4689
4690 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
4691 struct ata_ioports *ioaddr = &ap->ioaddr;
4692
4693 if (ioaddr->cmd_addr == 0x1f0)
4694 release_region(0x1f0, 8);
4695 else if (ioaddr->cmd_addr == 0x170)
4696 release_region(0x170, 8);
4697 }
4698
4699 scsi_host_put(ap->host);
4700 }
4701
4702 if (host_set->ops->host_stop)
4703 host_set->ops->host_stop(host_set);
4704
4705 kfree(host_set);
4706}
4707
4708/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709 * ata_scsi_release - SCSI layer callback hook for host unload
4710 * @host: libata host to be unloaded
4711 *
4712 * Performs all duties necessary to shut down a libata port...
4713 * Kill port kthread, disable port, and release resources.
4714 *
4715 * LOCKING:
4716 * Inherited from SCSI layer.
4717 *
4718 * RETURNS:
4719 * One.
4720 */
4721
4722int ata_scsi_release(struct Scsi_Host *host)
4723{
4724 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
4725
4726 DPRINTK("ENTER\n");
4727
4728 ap->ops->port_disable(ap);
4729 ata_host_remove(ap, 0);
4730
4731 DPRINTK("EXIT\n");
4732 return 1;
4733}
4734
4735/**
4736 * ata_std_ports - initialize ioaddr with standard port offsets.
4737 * @ioaddr: IO address structure to be initialized
Edward Falk0baab862005-06-02 18:17:13 -04004738 *
4739 * Utility function which initializes data_addr, error_addr,
4740 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
4741 * device_addr, status_addr, and command_addr to standard offsets
4742 * relative to cmd_addr.
4743 *
4744 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745 */
Edward Falk0baab862005-06-02 18:17:13 -04004746
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747void ata_std_ports(struct ata_ioports *ioaddr)
4748{
4749 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
4750 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
4751 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
4752 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
4753 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
4754 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
4755 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
4756 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
4757 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
4758 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
4759}
4760
Edward Falk0baab862005-06-02 18:17:13 -04004761
Jeff Garzik374b1872005-08-30 05:42:52 -04004762#ifdef CONFIG_PCI
4763
4764void ata_pci_host_stop (struct ata_host_set *host_set)
4765{
4766 struct pci_dev *pdev = to_pci_dev(host_set->dev);
4767
4768 pci_iounmap(pdev, host_set->mmio_base);
4769}
4770
Edward Falk0baab862005-06-02 18:17:13 -04004771/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772 * ata_pci_remove_one - PCI layer callback for device removal
4773 * @pdev: PCI device that was removed
4774 *
4775 * PCI layer indicates to libata via this hook that
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004776 * hot-unplug or module unload event has occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777 * Handle this by unregistering all objects associated
4778 * with this PCI device. Free those objects. Then finally
4779 * release PCI resources and disable device.
4780 *
4781 * LOCKING:
4782 * Inherited from PCI layer (may sleep).
4783 */
4784
4785void ata_pci_remove_one (struct pci_dev *pdev)
4786{
4787 struct device *dev = pci_dev_to_dev(pdev);
4788 struct ata_host_set *host_set = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789
Alan Cox17b14452005-09-15 15:44:00 +01004790 ata_host_set_remove(host_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791 pci_release_regions(pdev);
4792 pci_disable_device(pdev);
4793 dev_set_drvdata(dev, NULL);
4794}
4795
4796/* move to PCI subsystem */
Jeff Garzik057ace52005-10-22 14:27:05 -04004797int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798{
4799 unsigned long tmp = 0;
4800
4801 switch (bits->width) {
4802 case 1: {
4803 u8 tmp8 = 0;
4804 pci_read_config_byte(pdev, bits->reg, &tmp8);
4805 tmp = tmp8;
4806 break;
4807 }
4808 case 2: {
4809 u16 tmp16 = 0;
4810 pci_read_config_word(pdev, bits->reg, &tmp16);
4811 tmp = tmp16;
4812 break;
4813 }
4814 case 4: {
4815 u32 tmp32 = 0;
4816 pci_read_config_dword(pdev, bits->reg, &tmp32);
4817 tmp = tmp32;
4818 break;
4819 }
4820
4821 default:
4822 return -EINVAL;
4823 }
4824
4825 tmp &= bits->mask;
4826
4827 return (tmp == bits->val) ? 1 : 0;
4828}
Jens Axboe9b847542006-01-06 09:28:07 +01004829
4830int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t state)
4831{
4832 pci_save_state(pdev);
4833 pci_disable_device(pdev);
4834 pci_set_power_state(pdev, PCI_D3hot);
4835 return 0;
4836}
4837
4838int ata_pci_device_resume(struct pci_dev *pdev)
4839{
4840 pci_set_power_state(pdev, PCI_D0);
4841 pci_restore_state(pdev);
4842 pci_enable_device(pdev);
4843 pci_set_master(pdev);
4844 return 0;
4845}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846#endif /* CONFIG_PCI */
4847
4848
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849static int __init ata_init(void)
4850{
4851 ata_wq = create_workqueue("ata");
4852 if (!ata_wq)
4853 return -ENOMEM;
4854
4855 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
4856 return 0;
4857}
4858
4859static void __exit ata_exit(void)
4860{
4861 destroy_workqueue(ata_wq);
4862}
4863
4864module_init(ata_init);
4865module_exit(ata_exit);
4866
Jeff Garzik67846b32005-10-05 02:58:32 -04004867static unsigned long ratelimit_time;
4868static spinlock_t ata_ratelimit_lock = SPIN_LOCK_UNLOCKED;
4869
4870int ata_ratelimit(void)
4871{
4872 int rc;
4873 unsigned long flags;
4874
4875 spin_lock_irqsave(&ata_ratelimit_lock, flags);
4876
4877 if (time_after(jiffies, ratelimit_time)) {
4878 rc = 1;
4879 ratelimit_time = jiffies + (HZ/5);
4880 } else
4881 rc = 0;
4882
4883 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
4884
4885 return rc;
4886}
4887
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888/*
4889 * libata is essentially a library of internal helper functions for
4890 * low-level ATA host controller drivers. As such, the API/ABI is
4891 * likely to change as new drivers are added and updated.
4892 * Do not depend on ABI/API stability.
4893 */
4894
4895EXPORT_SYMBOL_GPL(ata_std_bios_param);
4896EXPORT_SYMBOL_GPL(ata_std_ports);
4897EXPORT_SYMBOL_GPL(ata_device_add);
Alan Cox17b14452005-09-15 15:44:00 +01004898EXPORT_SYMBOL_GPL(ata_host_set_remove);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004899EXPORT_SYMBOL_GPL(ata_sg_init);
4900EXPORT_SYMBOL_GPL(ata_sg_init_one);
Tejun Heo76014422006-02-11 15:13:49 +09004901EXPORT_SYMBOL_GPL(__ata_qc_complete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004902EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
4903EXPORT_SYMBOL_GPL(ata_eng_timeout);
4904EXPORT_SYMBOL_GPL(ata_tf_load);
4905EXPORT_SYMBOL_GPL(ata_tf_read);
4906EXPORT_SYMBOL_GPL(ata_noop_dev_select);
4907EXPORT_SYMBOL_GPL(ata_std_dev_select);
4908EXPORT_SYMBOL_GPL(ata_tf_to_fis);
4909EXPORT_SYMBOL_GPL(ata_tf_from_fis);
4910EXPORT_SYMBOL_GPL(ata_check_status);
4911EXPORT_SYMBOL_GPL(ata_altstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912EXPORT_SYMBOL_GPL(ata_exec_command);
4913EXPORT_SYMBOL_GPL(ata_port_start);
4914EXPORT_SYMBOL_GPL(ata_port_stop);
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04004915EXPORT_SYMBOL_GPL(ata_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916EXPORT_SYMBOL_GPL(ata_interrupt);
4917EXPORT_SYMBOL_GPL(ata_qc_prep);
4918EXPORT_SYMBOL_GPL(ata_bmdma_setup);
4919EXPORT_SYMBOL_GPL(ata_bmdma_start);
4920EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
4921EXPORT_SYMBOL_GPL(ata_bmdma_status);
4922EXPORT_SYMBOL_GPL(ata_bmdma_stop);
4923EXPORT_SYMBOL_GPL(ata_port_probe);
4924EXPORT_SYMBOL_GPL(sata_phy_reset);
4925EXPORT_SYMBOL_GPL(__sata_phy_reset);
4926EXPORT_SYMBOL_GPL(ata_bus_reset);
Tejun Heo8a19ac82006-02-02 18:20:00 +09004927EXPORT_SYMBOL_GPL(ata_std_probeinit);
Tejun Heoc2bd5802006-01-24 17:05:22 +09004928EXPORT_SYMBOL_GPL(ata_std_softreset);
4929EXPORT_SYMBOL_GPL(sata_std_hardreset);
4930EXPORT_SYMBOL_GPL(ata_std_postreset);
4931EXPORT_SYMBOL_GPL(ata_std_probe_reset);
Tejun Heoa62c0fc2006-01-24 17:05:22 +09004932EXPORT_SYMBOL_GPL(ata_drive_probe_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933EXPORT_SYMBOL_GPL(ata_port_disable);
Jeff Garzik67846b32005-10-05 02:58:32 -04004934EXPORT_SYMBOL_GPL(ata_ratelimit);
Tejun Heo6f8b9952006-01-24 17:05:21 +09004935EXPORT_SYMBOL_GPL(ata_busy_sleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
4937EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
Tejun Heof29841e2006-02-10 15:10:48 +09004938EXPORT_SYMBOL_GPL(ata_scsi_timed_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939EXPORT_SYMBOL_GPL(ata_scsi_error);
4940EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
4941EXPORT_SYMBOL_GPL(ata_scsi_release);
4942EXPORT_SYMBOL_GPL(ata_host_intr);
4943EXPORT_SYMBOL_GPL(ata_dev_classify);
Tejun Heo6a62a042006-02-13 10:02:46 +09004944EXPORT_SYMBOL_GPL(ata_id_string);
4945EXPORT_SYMBOL_GPL(ata_id_c_string);
Brad Campbell6f2f3812005-05-12 15:07:47 -04004946EXPORT_SYMBOL_GPL(ata_dev_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947EXPORT_SYMBOL_GPL(ata_scsi_simulate);
Tejun Heoa72ec4c2006-01-23 13:09:37 +09004948EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
4949EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950
Alan Cox1bc4ccf2006-01-09 17:18:14 +00004951EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
Alan Cox452503f2005-10-21 19:01:32 -04004952EXPORT_SYMBOL_GPL(ata_timing_compute);
4953EXPORT_SYMBOL_GPL(ata_timing_merge);
4954
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955#ifdef CONFIG_PCI
4956EXPORT_SYMBOL_GPL(pci_test_config_bits);
Jeff Garzik374b1872005-08-30 05:42:52 -04004957EXPORT_SYMBOL_GPL(ata_pci_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
4959EXPORT_SYMBOL_GPL(ata_pci_init_one);
4960EXPORT_SYMBOL_GPL(ata_pci_remove_one);
Jens Axboe9b847542006-01-06 09:28:07 +01004961EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
4962EXPORT_SYMBOL_GPL(ata_pci_device_resume);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004963#endif /* CONFIG_PCI */
Jens Axboe9b847542006-01-06 09:28:07 +01004964
4965EXPORT_SYMBOL_GPL(ata_device_suspend);
4966EXPORT_SYMBOL_GPL(ata_device_resume);
4967EXPORT_SYMBOL_GPL(ata_scsi_device_suspend);
4968EXPORT_SYMBOL_GPL(ata_scsi_device_resume);