blob: d694b20e81bf86a0c05285a168653433e55bfc67 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Jeff Garzikaf36d7f2005-08-28 20:18:39 -04002 * libata-core.c - helper library for ATA
3 *
4 * Maintained by: Jeff Garzik <jgarzik@pobox.com>
5 * Please ALWAYS copy linux-ide@vger.kernel.org
6 * on emails.
7 *
8 * Copyright 2003-2004 Red Hat, Inc. All rights reserved.
9 * Copyright 2003-2004 Jeff Garzik
10 *
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2, or (at your option)
15 * any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; see the file COPYING. If not, write to
24 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
25 *
26 *
27 * libata documentation is available via 'make {ps|pdf}docs',
28 * as Documentation/DocBook/libata.*
29 *
30 * Hardware documentation available from http://www.t13.org/ and
31 * http://www.sata-io.org/
32 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 */
34
35#include <linux/config.h>
36#include <linux/kernel.h>
37#include <linux/module.h>
38#include <linux/pci.h>
39#include <linux/init.h>
40#include <linux/list.h>
41#include <linux/mm.h>
42#include <linux/highmem.h>
43#include <linux/spinlock.h>
44#include <linux/blkdev.h>
45#include <linux/delay.h>
46#include <linux/timer.h>
47#include <linux/interrupt.h>
48#include <linux/completion.h>
49#include <linux/suspend.h>
50#include <linux/workqueue.h>
Jeff Garzik67846b32005-10-05 02:58:32 -040051#include <linux/jiffies.h>
David Hardeman378f0582005-09-17 17:55:31 +100052#include <linux/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#include <scsi/scsi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include "scsi_priv.h"
Jeff Garzik193515d2005-11-07 00:59:37 -050055#include <scsi/scsi_cmnd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <scsi/scsi_host.h>
57#include <linux/libata.h>
58#include <asm/io.h>
59#include <asm/semaphore.h>
60#include <asm/byteorder.h>
61
62#include "libata.h"
63
Albert Lee59a10b12005-10-12 15:09:42 +080064static void ata_dev_reread_id(struct ata_port *ap, struct ata_device *dev);
Albert Lee8bf62ec2005-05-12 15:29:42 -040065static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066static void ata_set_mode(struct ata_port *ap);
67static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev);
Jeff Garzik057ace52005-10-22 14:27:05 -040068static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069static int fgb(u32 bitmap);
Jeff Garzik057ace52005-10-22 14:27:05 -040070static int ata_choose_xfer_mode(const struct ata_port *ap,
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 u8 *xfer_mode_out,
72 unsigned int *xfer_shift_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74static unsigned int ata_unique_id = 1;
75static struct workqueue_struct *ata_wq;
76
Jeff Garzik1623c812005-08-30 03:37:42 -040077int atapi_enabled = 0;
78module_param(atapi_enabled, int, 0444);
79MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)");
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081MODULE_AUTHOR("Jeff Garzik");
82MODULE_DESCRIPTION("Library module for ATA devices");
83MODULE_LICENSE("GPL");
84MODULE_VERSION(DRV_VERSION);
85
Edward Falk0baab862005-06-02 18:17:13 -040086
87/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
89 * @tf: Taskfile to convert
90 * @fis: Buffer into which data will output
91 * @pmp: Port multiplier port
92 *
93 * Converts a standard ATA taskfile to a Serial ATA
94 * FIS structure (Register - Host to Device).
95 *
96 * LOCKING:
97 * Inherited from caller.
98 */
99
Jeff Garzik057ace52005-10-22 14:27:05 -0400100void ata_tf_to_fis(const struct ata_taskfile *tf, u8 *fis, u8 pmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101{
102 fis[0] = 0x27; /* Register - Host to Device FIS */
103 fis[1] = (pmp & 0xf) | (1 << 7); /* Port multiplier number,
104 bit 7 indicates Command FIS */
105 fis[2] = tf->command;
106 fis[3] = tf->feature;
107
108 fis[4] = tf->lbal;
109 fis[5] = tf->lbam;
110 fis[6] = tf->lbah;
111 fis[7] = tf->device;
112
113 fis[8] = tf->hob_lbal;
114 fis[9] = tf->hob_lbam;
115 fis[10] = tf->hob_lbah;
116 fis[11] = tf->hob_feature;
117
118 fis[12] = tf->nsect;
119 fis[13] = tf->hob_nsect;
120 fis[14] = 0;
121 fis[15] = tf->ctl;
122
123 fis[16] = 0;
124 fis[17] = 0;
125 fis[18] = 0;
126 fis[19] = 0;
127}
128
129/**
130 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
131 * @fis: Buffer from which data will be input
132 * @tf: Taskfile to output
133 *
Mark Lorde12a1be2005-11-12 18:55:45 -0500134 * Converts a serial ATA FIS structure to a standard ATA taskfile.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 *
136 * LOCKING:
137 * Inherited from caller.
138 */
139
Jeff Garzik057ace52005-10-22 14:27:05 -0400140void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
142 tf->command = fis[2]; /* status */
143 tf->feature = fis[3]; /* error */
144
145 tf->lbal = fis[4];
146 tf->lbam = fis[5];
147 tf->lbah = fis[6];
148 tf->device = fis[7];
149
150 tf->hob_lbal = fis[8];
151 tf->hob_lbam = fis[9];
152 tf->hob_lbah = fis[10];
153
154 tf->nsect = fis[12];
155 tf->hob_nsect = fis[13];
156}
157
Albert Lee8cbd6df2005-10-12 15:06:27 +0800158static const u8 ata_rw_cmds[] = {
159 /* pio multi */
160 ATA_CMD_READ_MULTI,
161 ATA_CMD_WRITE_MULTI,
162 ATA_CMD_READ_MULTI_EXT,
163 ATA_CMD_WRITE_MULTI_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100164 0,
165 0,
166 0,
167 ATA_CMD_WRITE_MULTI_FUA_EXT,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800168 /* pio */
169 ATA_CMD_PIO_READ,
170 ATA_CMD_PIO_WRITE,
171 ATA_CMD_PIO_READ_EXT,
172 ATA_CMD_PIO_WRITE_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100173 0,
174 0,
175 0,
176 0,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800177 /* dma */
178 ATA_CMD_READ,
179 ATA_CMD_WRITE,
180 ATA_CMD_READ_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100181 ATA_CMD_WRITE_EXT,
182 0,
183 0,
184 0,
185 ATA_CMD_WRITE_FUA_EXT
Albert Lee8cbd6df2005-10-12 15:06:27 +0800186};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188/**
Albert Lee8cbd6df2005-10-12 15:06:27 +0800189 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
190 * @qc: command to examine and configure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 *
Albert Lee8cbd6df2005-10-12 15:06:27 +0800192 * Examine the device configuration and tf->flags to calculate
193 * the proper read/write commands and protocol to use.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 *
195 * LOCKING:
196 * caller.
197 */
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100198int ata_rwcmd_protocol(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
Albert Lee8cbd6df2005-10-12 15:06:27 +0800200 struct ata_taskfile *tf = &qc->tf;
201 struct ata_device *dev = qc->dev;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100202 u8 cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100204 int index, fua, lba48, write;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800205
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100206 fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800207 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
208 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209
Albert Lee8cbd6df2005-10-12 15:06:27 +0800210 if (dev->flags & ATA_DFLAG_PIO) {
211 tf->protocol = ATA_PROT_PIO;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100212 index = dev->multi_count ? 0 : 8;
Alan Cox8d238e02006-01-17 20:50:31 +0000213 } else if (lba48 && (qc->ap->flags & ATA_FLAG_PIO_LBA48)) {
214 /* Unable to use DMA due to host limitation */
215 tf->protocol = ATA_PROT_PIO;
Albert Lee0565c262006-02-13 18:55:25 +0800216 index = dev->multi_count ? 0 : 8;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800217 } else {
218 tf->protocol = ATA_PROT_DMA;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100219 index = 16;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800220 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100222 cmd = ata_rw_cmds[index + fua + lba48 + write];
223 if (cmd) {
224 tf->command = cmd;
225 return 0;
226 }
227 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228}
229
Arjan van de Ven98ac62d2005-11-28 10:06:23 +0100230static const char * const xfer_mode_str[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 "UDMA/16",
232 "UDMA/25",
233 "UDMA/33",
234 "UDMA/44",
235 "UDMA/66",
236 "UDMA/100",
237 "UDMA/133",
238 "UDMA7",
239 "MWDMA0",
240 "MWDMA1",
241 "MWDMA2",
242 "PIO0",
243 "PIO1",
244 "PIO2",
245 "PIO3",
246 "PIO4",
247};
248
249/**
250 * ata_udma_string - convert UDMA bit offset to string
251 * @mask: mask of bits supported; only highest bit counts.
252 *
253 * Determine string which represents the highest speed
254 * (highest bit in @udma_mask).
255 *
256 * LOCKING:
257 * None.
258 *
259 * RETURNS:
260 * Constant C string representing highest speed listed in
261 * @udma_mask, or the constant C string "<n/a>".
262 */
263
264static const char *ata_mode_string(unsigned int mask)
265{
266 int i;
267
268 for (i = 7; i >= 0; i--)
269 if (mask & (1 << i))
270 goto out;
271 for (i = ATA_SHIFT_MWDMA + 2; i >= ATA_SHIFT_MWDMA; i--)
272 if (mask & (1 << i))
273 goto out;
274 for (i = ATA_SHIFT_PIO + 4; i >= ATA_SHIFT_PIO; i--)
275 if (mask & (1 << i))
276 goto out;
277
278 return "<n/a>";
279
280out:
281 return xfer_mode_str[i];
282}
283
284/**
285 * ata_pio_devchk - PATA device presence detection
286 * @ap: ATA channel to examine
287 * @device: Device to examine (starting at zero)
288 *
289 * This technique was originally described in
290 * Hale Landis's ATADRVR (www.ata-atapi.com), and
291 * later found its way into the ATA/ATAPI spec.
292 *
293 * Write a pattern to the ATA shadow registers,
294 * and if a device is present, it will respond by
295 * correctly storing and echoing back the
296 * ATA shadow register contents.
297 *
298 * LOCKING:
299 * caller.
300 */
301
302static unsigned int ata_pio_devchk(struct ata_port *ap,
303 unsigned int device)
304{
305 struct ata_ioports *ioaddr = &ap->ioaddr;
306 u8 nsect, lbal;
307
308 ap->ops->dev_select(ap, device);
309
310 outb(0x55, ioaddr->nsect_addr);
311 outb(0xaa, ioaddr->lbal_addr);
312
313 outb(0xaa, ioaddr->nsect_addr);
314 outb(0x55, ioaddr->lbal_addr);
315
316 outb(0x55, ioaddr->nsect_addr);
317 outb(0xaa, ioaddr->lbal_addr);
318
319 nsect = inb(ioaddr->nsect_addr);
320 lbal = inb(ioaddr->lbal_addr);
321
322 if ((nsect == 0x55) && (lbal == 0xaa))
323 return 1; /* we found a device */
324
325 return 0; /* nothing found */
326}
327
328/**
329 * ata_mmio_devchk - PATA device presence detection
330 * @ap: ATA channel to examine
331 * @device: Device to examine (starting at zero)
332 *
333 * This technique was originally described in
334 * Hale Landis's ATADRVR (www.ata-atapi.com), and
335 * later found its way into the ATA/ATAPI spec.
336 *
337 * Write a pattern to the ATA shadow registers,
338 * and if a device is present, it will respond by
339 * correctly storing and echoing back the
340 * ATA shadow register contents.
341 *
342 * LOCKING:
343 * caller.
344 */
345
346static unsigned int ata_mmio_devchk(struct ata_port *ap,
347 unsigned int device)
348{
349 struct ata_ioports *ioaddr = &ap->ioaddr;
350 u8 nsect, lbal;
351
352 ap->ops->dev_select(ap, device);
353
354 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
355 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
356
357 writeb(0xaa, (void __iomem *) ioaddr->nsect_addr);
358 writeb(0x55, (void __iomem *) ioaddr->lbal_addr);
359
360 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
361 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
362
363 nsect = readb((void __iomem *) ioaddr->nsect_addr);
364 lbal = readb((void __iomem *) ioaddr->lbal_addr);
365
366 if ((nsect == 0x55) && (lbal == 0xaa))
367 return 1; /* we found a device */
368
369 return 0; /* nothing found */
370}
371
372/**
373 * ata_devchk - PATA device presence detection
374 * @ap: ATA channel to examine
375 * @device: Device to examine (starting at zero)
376 *
377 * Dispatch ATA device presence detection, depending
378 * on whether we are using PIO or MMIO to talk to the
379 * ATA shadow registers.
380 *
381 * LOCKING:
382 * caller.
383 */
384
385static unsigned int ata_devchk(struct ata_port *ap,
386 unsigned int device)
387{
388 if (ap->flags & ATA_FLAG_MMIO)
389 return ata_mmio_devchk(ap, device);
390 return ata_pio_devchk(ap, device);
391}
392
393/**
394 * ata_dev_classify - determine device type based on ATA-spec signature
395 * @tf: ATA taskfile register set for device to be identified
396 *
397 * Determine from taskfile register contents whether a device is
398 * ATA or ATAPI, as per "Signature and persistence" section
399 * of ATA/PI spec (volume 1, sect 5.14).
400 *
401 * LOCKING:
402 * None.
403 *
404 * RETURNS:
405 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
406 * the event of failure.
407 */
408
Jeff Garzik057ace52005-10-22 14:27:05 -0400409unsigned int ata_dev_classify(const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410{
411 /* Apple's open source Darwin code hints that some devices only
412 * put a proper signature into the LBA mid/high registers,
413 * So, we only check those. It's sufficient for uniqueness.
414 */
415
416 if (((tf->lbam == 0) && (tf->lbah == 0)) ||
417 ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
418 DPRINTK("found ATA device by sig\n");
419 return ATA_DEV_ATA;
420 }
421
422 if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
423 ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
424 DPRINTK("found ATAPI device by sig\n");
425 return ATA_DEV_ATAPI;
426 }
427
428 DPRINTK("unknown device\n");
429 return ATA_DEV_UNKNOWN;
430}
431
432/**
433 * ata_dev_try_classify - Parse returned ATA device signature
434 * @ap: ATA channel to examine
435 * @device: Device to examine (starting at zero)
Tejun Heob4dc7622006-01-24 17:05:22 +0900436 * @r_err: Value of error register on completion
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 *
438 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
439 * an ATA/ATAPI-defined set of values is placed in the ATA
440 * shadow registers, indicating the results of device detection
441 * and diagnostics.
442 *
443 * Select the ATA device, and read the values from the ATA shadow
444 * registers. Then parse according to the Error register value,
445 * and the spec-defined values examined by ata_dev_classify().
446 *
447 * LOCKING:
448 * caller.
Tejun Heob4dc7622006-01-24 17:05:22 +0900449 *
450 * RETURNS:
451 * Device type - %ATA_DEV_ATA, %ATA_DEV_ATAPI or %ATA_DEV_NONE.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 */
453
Tejun Heob4dc7622006-01-24 17:05:22 +0900454static unsigned int
455ata_dev_try_classify(struct ata_port *ap, unsigned int device, u8 *r_err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 struct ata_taskfile tf;
458 unsigned int class;
459 u8 err;
460
461 ap->ops->dev_select(ap, device);
462
463 memset(&tf, 0, sizeof(tf));
464
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 ap->ops->tf_read(ap, &tf);
Jeff Garzik0169e282005-10-29 21:25:10 -0400466 err = tf.feature;
Tejun Heob4dc7622006-01-24 17:05:22 +0900467 if (r_err)
468 *r_err = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
470 /* see if device passed diags */
471 if (err == 1)
472 /* do nothing */ ;
473 else if ((device == 0) && (err == 0x81))
474 /* do nothing */ ;
475 else
Tejun Heob4dc7622006-01-24 17:05:22 +0900476 return ATA_DEV_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
Tejun Heob4dc7622006-01-24 17:05:22 +0900478 /* determine if device is ATA or ATAPI */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 class = ata_dev_classify(&tf);
Tejun Heob4dc7622006-01-24 17:05:22 +0900480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 if (class == ATA_DEV_UNKNOWN)
Tejun Heob4dc7622006-01-24 17:05:22 +0900482 return ATA_DEV_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
Tejun Heob4dc7622006-01-24 17:05:22 +0900484 return ATA_DEV_NONE;
485 return class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486}
487
488/**
Tejun Heo6a62a042006-02-13 10:02:46 +0900489 * ata_id_string - Convert IDENTIFY DEVICE page into string
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 * @id: IDENTIFY DEVICE results we will examine
491 * @s: string into which data is output
492 * @ofs: offset into identify device page
493 * @len: length of string to return. must be an even number.
494 *
495 * The strings in the IDENTIFY DEVICE page are broken up into
496 * 16-bit chunks. Run through the string, and output each
497 * 8-bit chunk linearly, regardless of platform.
498 *
499 * LOCKING:
500 * caller.
501 */
502
Tejun Heo6a62a042006-02-13 10:02:46 +0900503void ata_id_string(const u16 *id, unsigned char *s,
504 unsigned int ofs, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
506 unsigned int c;
507
508 while (len > 0) {
509 c = id[ofs] >> 8;
510 *s = c;
511 s++;
512
513 c = id[ofs] & 0xff;
514 *s = c;
515 s++;
516
517 ofs++;
518 len -= 2;
519 }
520}
521
Tejun Heo0e949ff2006-02-12 22:47:04 +0900522/**
Tejun Heo6a62a042006-02-13 10:02:46 +0900523 * ata_id_c_string - Convert IDENTIFY DEVICE page into C string
Tejun Heo0e949ff2006-02-12 22:47:04 +0900524 * @id: IDENTIFY DEVICE results we will examine
525 * @s: string into which data is output
526 * @ofs: offset into identify device page
527 * @len: length of string to return. must be an odd number.
528 *
Tejun Heo6a62a042006-02-13 10:02:46 +0900529 * This function is identical to ata_id_string except that it
Tejun Heo0e949ff2006-02-12 22:47:04 +0900530 * trims trailing spaces and terminates the resulting string with
531 * null. @len must be actual maximum length (even number) + 1.
532 *
533 * LOCKING:
534 * caller.
535 */
Tejun Heo6a62a042006-02-13 10:02:46 +0900536void ata_id_c_string(const u16 *id, unsigned char *s,
537 unsigned int ofs, unsigned int len)
Tejun Heo0e949ff2006-02-12 22:47:04 +0900538{
539 unsigned char *p;
540
541 WARN_ON(!(len & 1));
542
Tejun Heo6a62a042006-02-13 10:02:46 +0900543 ata_id_string(id, s, ofs, len - 1);
Tejun Heo0e949ff2006-02-12 22:47:04 +0900544
545 p = s + strnlen(s, len - 1);
546 while (p > s && p[-1] == ' ')
547 p--;
548 *p = '\0';
549}
Edward Falk0baab862005-06-02 18:17:13 -0400550
Tejun Heo29407402006-02-12 22:47:04 +0900551static u64 ata_id_n_sectors(const u16 *id)
552{
553 if (ata_id_has_lba(id)) {
554 if (ata_id_has_lba48(id))
555 return ata_id_u64(id, 100);
556 else
557 return ata_id_u32(id, 60);
558 } else {
559 if (ata_id_current_chs_valid(id))
560 return ata_id_u32(id, 57);
561 else
562 return id[1] * id[3] * id[6];
563 }
564}
565
Edward Falk0baab862005-06-02 18:17:13 -0400566/**
567 * ata_noop_dev_select - Select device 0/1 on ATA bus
568 * @ap: ATA channel to manipulate
569 * @device: ATA device (numbered from zero) to select
570 *
571 * This function performs no actual function.
572 *
573 * May be used as the dev_select() entry in ata_port_operations.
574 *
575 * LOCKING:
576 * caller.
577 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
579{
580}
581
Edward Falk0baab862005-06-02 18:17:13 -0400582
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583/**
584 * ata_std_dev_select - Select device 0/1 on ATA bus
585 * @ap: ATA channel to manipulate
586 * @device: ATA device (numbered from zero) to select
587 *
588 * Use the method defined in the ATA specification to
589 * make either device 0, or device 1, active on the
Edward Falk0baab862005-06-02 18:17:13 -0400590 * ATA channel. Works with both PIO and MMIO.
591 *
592 * May be used as the dev_select() entry in ata_port_operations.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 *
594 * LOCKING:
595 * caller.
596 */
597
598void ata_std_dev_select (struct ata_port *ap, unsigned int device)
599{
600 u8 tmp;
601
602 if (device == 0)
603 tmp = ATA_DEVICE_OBS;
604 else
605 tmp = ATA_DEVICE_OBS | ATA_DEV1;
606
607 if (ap->flags & ATA_FLAG_MMIO) {
608 writeb(tmp, (void __iomem *) ap->ioaddr.device_addr);
609 } else {
610 outb(tmp, ap->ioaddr.device_addr);
611 }
612 ata_pause(ap); /* needed; also flushes, for mmio */
613}
614
615/**
616 * ata_dev_select - Select device 0/1 on ATA bus
617 * @ap: ATA channel to manipulate
618 * @device: ATA device (numbered from zero) to select
619 * @wait: non-zero to wait for Status register BSY bit to clear
620 * @can_sleep: non-zero if context allows sleeping
621 *
622 * Use the method defined in the ATA specification to
623 * make either device 0, or device 1, active on the
624 * ATA channel.
625 *
626 * This is a high-level version of ata_std_dev_select(),
627 * which additionally provides the services of inserting
628 * the proper pauses and status polling, where needed.
629 *
630 * LOCKING:
631 * caller.
632 */
633
634void ata_dev_select(struct ata_port *ap, unsigned int device,
635 unsigned int wait, unsigned int can_sleep)
636{
637 VPRINTK("ENTER, ata%u: device %u, wait %u\n",
638 ap->id, device, wait);
639
640 if (wait)
641 ata_wait_idle(ap);
642
643 ap->ops->dev_select(ap, device);
644
645 if (wait) {
646 if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
647 msleep(150);
648 ata_wait_idle(ap);
649 }
650}
651
652/**
653 * ata_dump_id - IDENTIFY DEVICE info debugging output
Tejun Heo0bd33002006-02-12 22:47:05 +0900654 * @id: IDENTIFY DEVICE page to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 *
Tejun Heo0bd33002006-02-12 22:47:05 +0900656 * Dump selected 16-bit words from the given IDENTIFY DEVICE
657 * page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 *
659 * LOCKING:
660 * caller.
661 */
662
Tejun Heo0bd33002006-02-12 22:47:05 +0900663static inline void ata_dump_id(const u16 *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664{
665 DPRINTK("49==0x%04x "
666 "53==0x%04x "
667 "63==0x%04x "
668 "64==0x%04x "
669 "75==0x%04x \n",
Tejun Heo0bd33002006-02-12 22:47:05 +0900670 id[49],
671 id[53],
672 id[63],
673 id[64],
674 id[75]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 DPRINTK("80==0x%04x "
676 "81==0x%04x "
677 "82==0x%04x "
678 "83==0x%04x "
679 "84==0x%04x \n",
Tejun Heo0bd33002006-02-12 22:47:05 +0900680 id[80],
681 id[81],
682 id[82],
683 id[83],
684 id[84]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 DPRINTK("88==0x%04x "
686 "93==0x%04x\n",
Tejun Heo0bd33002006-02-12 22:47:05 +0900687 id[88],
688 id[93]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689}
690
Alan Cox11e29e22005-10-21 18:46:32 -0400691/*
692 * Compute the PIO modes available for this device. This is not as
693 * trivial as it seems if we must consider early devices correctly.
694 *
695 * FIXME: pre IDE drive timing (do we care ?).
696 */
697
Jeff Garzik057ace52005-10-22 14:27:05 -0400698static unsigned int ata_pio_modes(const struct ata_device *adev)
Alan Cox11e29e22005-10-21 18:46:32 -0400699{
700 u16 modes;
701
Alan Coxffa29452006-01-09 17:14:40 +0000702 /* Usual case. Word 53 indicates word 64 is valid */
703 if (adev->id[ATA_ID_FIELD_VALID] & (1 << 1)) {
Alan Cox11e29e22005-10-21 18:46:32 -0400704 modes = adev->id[ATA_ID_PIO_MODES] & 0x03;
705 modes <<= 3;
706 modes |= 0x7;
707 return modes;
708 }
709
Alan Coxffa29452006-01-09 17:14:40 +0000710 /* If word 64 isn't valid then Word 51 high byte holds the PIO timing
711 number for the maximum. Turn it into a mask and return it */
712 modes = (2 << ((adev->id[ATA_ID_OLD_PIO_MODES] >> 8) & 0xFF)) - 1 ;
Alan Cox11e29e22005-10-21 18:46:32 -0400713 return modes;
Alan Coxffa29452006-01-09 17:14:40 +0000714 /* But wait.. there's more. Design your standards by committee and
715 you too can get a free iordy field to process. However its the
716 speeds not the modes that are supported... Note drivers using the
717 timing API will get this right anyway */
Alan Cox11e29e22005-10-21 18:46:32 -0400718}
719
Tejun Heo95064372006-01-23 13:09:37 +0900720static inline void
721ata_queue_packet_task(struct ata_port *ap)
722{
Tejun Heoc18d06f2006-02-02 00:56:10 +0900723 if (!(ap->flags & ATA_FLAG_FLUSH_PIO_TASK))
724 queue_work(ata_wq, &ap->packet_task);
Tejun Heo95064372006-01-23 13:09:37 +0900725}
726
727static inline void
728ata_queue_pio_task(struct ata_port *ap)
729{
Tejun Heoc18d06f2006-02-02 00:56:10 +0900730 if (!(ap->flags & ATA_FLAG_FLUSH_PIO_TASK))
731 queue_work(ata_wq, &ap->pio_task);
Tejun Heo95064372006-01-23 13:09:37 +0900732}
733
734static inline void
735ata_queue_delayed_pio_task(struct ata_port *ap, unsigned long delay)
736{
Tejun Heoc18d06f2006-02-02 00:56:10 +0900737 if (!(ap->flags & ATA_FLAG_FLUSH_PIO_TASK))
738 queue_delayed_work(ata_wq, &ap->pio_task, delay);
739}
740
741/**
742 * ata_flush_pio_tasks - Flush pio_task and packet_task
743 * @ap: the target ata_port
744 *
745 * After this function completes, pio_task and packet_task are
746 * guranteed not to be running or scheduled.
747 *
748 * LOCKING:
749 * Kernel thread context (may sleep)
750 */
751
752static void ata_flush_pio_tasks(struct ata_port *ap)
753{
754 int tmp = 0;
755 unsigned long flags;
756
757 DPRINTK("ENTER\n");
758
759 spin_lock_irqsave(&ap->host_set->lock, flags);
760 ap->flags |= ATA_FLAG_FLUSH_PIO_TASK;
761 spin_unlock_irqrestore(&ap->host_set->lock, flags);
762
763 DPRINTK("flush #1\n");
764 flush_workqueue(ata_wq);
765
766 /*
767 * At this point, if a task is running, it's guaranteed to see
768 * the FLUSH flag; thus, it will never queue pio tasks again.
769 * Cancel and flush.
770 */
771 tmp |= cancel_delayed_work(&ap->pio_task);
772 tmp |= cancel_delayed_work(&ap->packet_task);
773 if (!tmp) {
774 DPRINTK("flush #2\n");
775 flush_workqueue(ata_wq);
776 }
777
778 spin_lock_irqsave(&ap->host_set->lock, flags);
779 ap->flags &= ~ATA_FLAG_FLUSH_PIO_TASK;
780 spin_unlock_irqrestore(&ap->host_set->lock, flags);
781
782 DPRINTK("EXIT\n");
Tejun Heo95064372006-01-23 13:09:37 +0900783}
784
Tejun Heo77853bf2006-01-23 13:09:36 +0900785void ata_qc_complete_internal(struct ata_queued_cmd *qc)
Tejun Heoa2a7a662005-12-13 14:48:31 +0900786{
Tejun Heo77853bf2006-01-23 13:09:36 +0900787 struct completion *waiting = qc->private_data;
Tejun Heoa2a7a662005-12-13 14:48:31 +0900788
Tejun Heo77853bf2006-01-23 13:09:36 +0900789 qc->ap->ops->tf_read(qc->ap, &qc->tf);
Tejun Heoa2a7a662005-12-13 14:48:31 +0900790 complete(waiting);
Tejun Heoa2a7a662005-12-13 14:48:31 +0900791}
792
793/**
794 * ata_exec_internal - execute libata internal command
795 * @ap: Port to which the command is sent
796 * @dev: Device to which the command is sent
797 * @tf: Taskfile registers for the command and the result
798 * @dma_dir: Data tranfer direction of the command
799 * @buf: Data buffer of the command
800 * @buflen: Length of data buffer
801 *
802 * Executes libata internal command with timeout. @tf contains
803 * command on entry and result on return. Timeout and error
804 * conditions are reported via return value. No recovery action
805 * is taken after a command times out. It's caller's duty to
806 * clean up after timeout.
807 *
808 * LOCKING:
809 * None. Should be called with kernel context, might sleep.
810 */
811
812static unsigned
813ata_exec_internal(struct ata_port *ap, struct ata_device *dev,
814 struct ata_taskfile *tf,
815 int dma_dir, void *buf, unsigned int buflen)
816{
817 u8 command = tf->command;
818 struct ata_queued_cmd *qc;
819 DECLARE_COMPLETION(wait);
820 unsigned long flags;
Tejun Heo77853bf2006-01-23 13:09:36 +0900821 unsigned int err_mask;
Tejun Heoa2a7a662005-12-13 14:48:31 +0900822
823 spin_lock_irqsave(&ap->host_set->lock, flags);
824
825 qc = ata_qc_new_init(ap, dev);
826 BUG_ON(qc == NULL);
827
828 qc->tf = *tf;
829 qc->dma_dir = dma_dir;
830 if (dma_dir != DMA_NONE) {
831 ata_sg_init_one(qc, buf, buflen);
832 qc->nsect = buflen / ATA_SECT_SIZE;
833 }
834
Tejun Heo77853bf2006-01-23 13:09:36 +0900835 qc->private_data = &wait;
Tejun Heoa2a7a662005-12-13 14:48:31 +0900836 qc->complete_fn = ata_qc_complete_internal;
837
Tejun Heo9a3d9eb2006-01-23 13:09:36 +0900838 qc->err_mask = ata_qc_issue(qc);
839 if (qc->err_mask)
Tejun Heo8e436af2006-01-23 13:09:36 +0900840 ata_qc_complete(qc);
Tejun Heoa2a7a662005-12-13 14:48:31 +0900841
842 spin_unlock_irqrestore(&ap->host_set->lock, flags);
843
844 if (!wait_for_completion_timeout(&wait, ATA_TMOUT_INTERNAL)) {
845 spin_lock_irqsave(&ap->host_set->lock, flags);
846
847 /* We're racing with irq here. If we lose, the
848 * following test prevents us from completing the qc
849 * again. If completion irq occurs after here but
850 * before the caller cleans up, it will result in a
851 * spurious interrupt. We can live with that.
852 */
Tejun Heo77853bf2006-01-23 13:09:36 +0900853 if (qc->flags & ATA_QCFLAG_ACTIVE) {
Tejun Heo11a56d22006-01-23 13:09:36 +0900854 qc->err_mask = AC_ERR_TIMEOUT;
Tejun Heoa2a7a662005-12-13 14:48:31 +0900855 ata_qc_complete(qc);
856 printk(KERN_WARNING "ata%u: qc timeout (cmd 0x%x)\n",
857 ap->id, command);
858 }
859
860 spin_unlock_irqrestore(&ap->host_set->lock, flags);
861 }
862
Tejun Heo77853bf2006-01-23 13:09:36 +0900863 *tf = qc->tf;
864 err_mask = qc->err_mask;
865
866 ata_qc_free(qc);
867
868 return err_mask;
Tejun Heoa2a7a662005-12-13 14:48:31 +0900869}
870
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871/**
Alan Cox1bc4ccf2006-01-09 17:18:14 +0000872 * ata_pio_need_iordy - check if iordy needed
873 * @adev: ATA device
874 *
875 * Check if the current speed of the device requires IORDY. Used
876 * by various controllers for chip configuration.
877 */
878
879unsigned int ata_pio_need_iordy(const struct ata_device *adev)
880{
881 int pio;
882 int speed = adev->pio_mode - XFER_PIO_0;
883
884 if (speed < 2)
885 return 0;
886 if (speed > 2)
887 return 1;
888
889 /* If we have no drive specific rule, then PIO 2 is non IORDY */
890
891 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
892 pio = adev->id[ATA_ID_EIDE_PIO];
893 /* Is the speed faster than the drive allows non IORDY ? */
894 if (pio) {
895 /* This is cycle times not frequency - watch the logic! */
896 if (pio > 240) /* PIO2 is 240nS per cycle */
897 return 1;
898 return 0;
899 }
900 }
901 return 0;
902}
903
904/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 * ata_dev_identify - obtain IDENTIFY x DEVICE page
906 * @ap: port on which device we wish to probe resides
907 * @device: device bus address, starting at zero
908 *
909 * Following bus reset, we issue the IDENTIFY [PACKET] DEVICE
910 * command, and read back the 512-byte device information page.
911 * The device information page is fed to us via the standard
912 * PIO-IN protocol, but we hand-code it here. (TODO: investigate
913 * using standard PIO-IN paths)
914 *
915 * After reading the device information page, we use several
916 * bits of information from it to initialize data structures
917 * that will be used during the lifetime of the ata_device.
918 * Other data from the info page is used to disqualify certain
919 * older ATA devices we do not wish to support.
920 *
921 * LOCKING:
922 * Inherited from caller. Some functions called by this function
923 * obtain the host_set lock.
924 */
925
926static void ata_dev_identify(struct ata_port *ap, unsigned int device)
927{
928 struct ata_device *dev = &ap->device[device];
Albert Lee8bf62ec2005-05-12 15:29:42 -0400929 unsigned int major_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 unsigned long xfer_modes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 unsigned int using_edd;
Tejun Heoa0123702005-12-13 14:49:31 +0900932 struct ata_taskfile tf;
933 unsigned int err_mask;
Tejun Heo6e7846e2006-02-12 23:32:58 +0900934 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935
936 if (!ata_dev_present(dev)) {
937 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
938 ap->id, device);
939 return;
940 }
941
Tejun Heo61eb0662006-02-12 23:22:37 +0900942 if (ap->ops->probe_reset ||
943 ap->flags & (ATA_FLAG_SRST | ATA_FLAG_SATA_RESET))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 using_edd = 0;
945 else
946 using_edd = 1;
947
948 DPRINTK("ENTER, host %u, dev %u\n", ap->id, device);
949
Tejun Heoa4631472006-02-11 19:11:13 +0900950 WARN_ON(dev->class != ATA_DEV_ATA && dev->class != ATA_DEV_ATAPI &&
951 dev->class != ATA_DEV_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
953 ata_dev_select(ap, device, 1, 1); /* select device 0/1 */
954
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955retry:
Tejun Heoa0123702005-12-13 14:49:31 +0900956 ata_tf_init(ap, &tf, device);
957
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 if (dev->class == ATA_DEV_ATA) {
Tejun Heoa0123702005-12-13 14:49:31 +0900959 tf.command = ATA_CMD_ID_ATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 DPRINTK("do ATA identify\n");
961 } else {
Tejun Heoa0123702005-12-13 14:49:31 +0900962 tf.command = ATA_CMD_ID_ATAPI;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 DPRINTK("do ATAPI identify\n");
964 }
965
Tejun Heoa0123702005-12-13 14:49:31 +0900966 tf.protocol = ATA_PROT_PIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
Tejun Heoa0123702005-12-13 14:49:31 +0900968 err_mask = ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE,
969 dev->id, sizeof(dev->id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Tejun Heoa0123702005-12-13 14:49:31 +0900971 if (err_mask) {
972 if (err_mask & ~AC_ERR_DEV)
973 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 /*
976 * arg! EDD works for all test cases, but seems to return
977 * the ATA signature for some ATAPI devices. Until the
978 * reason for this is found and fixed, we fix up the mess
979 * here. If IDENTIFY DEVICE returns command aborted
980 * (as ATAPI devices do), then we issue an
981 * IDENTIFY PACKET DEVICE.
982 *
983 * ATA software reset (SRST, the default) does not appear
984 * to have this problem.
985 */
Albert Lee7c398332005-11-09 13:03:30 +0800986 if ((using_edd) && (dev->class == ATA_DEV_ATA)) {
Tejun Heoa0123702005-12-13 14:49:31 +0900987 u8 err = tf.feature;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 if (err & ATA_ABORTED) {
989 dev->class = ATA_DEV_ATAPI;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 goto retry;
991 }
992 }
993 goto err_out;
994 }
995
996 swap_buf_le16(dev->id, ATA_ID_WORDS);
997
998 /* print device capabilities */
999 printk(KERN_DEBUG "ata%u: dev %u cfg "
1000 "49:%04x 82:%04x 83:%04x 84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1001 ap->id, device, dev->id[49],
1002 dev->id[82], dev->id[83], dev->id[84],
1003 dev->id[85], dev->id[86], dev->id[87],
1004 dev->id[88]);
1005
1006 /*
1007 * common ATA, ATAPI feature tests
1008 */
1009
Albert Lee8bf62ec2005-05-12 15:29:42 -04001010 /* we require DMA support (bits 8 of word 49) */
1011 if (!ata_id_has_dma(dev->id)) {
1012 printk(KERN_DEBUG "ata%u: no dma\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 goto err_out_nosup;
1014 }
1015
1016 /* quick-n-dirty find max transfer mode; for printk only */
1017 xfer_modes = dev->id[ATA_ID_UDMA_MODES];
1018 if (!xfer_modes)
1019 xfer_modes = (dev->id[ATA_ID_MWDMA_MODES]) << ATA_SHIFT_MWDMA;
Alan Cox11e29e22005-10-21 18:46:32 -04001020 if (!xfer_modes)
1021 xfer_modes = ata_pio_modes(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
Tejun Heo0bd33002006-02-12 22:47:05 +09001023 ata_dump_id(dev->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
1025 /* ATA-specific feature tests */
1026 if (dev->class == ATA_DEV_ATA) {
Tejun Heo29407402006-02-12 22:47:04 +09001027 dev->n_sectors = ata_id_n_sectors(dev->id);
1028
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 if (!ata_id_is_ata(dev->id)) /* sanity check */
1030 goto err_out_nosup;
1031
Albert Lee8bf62ec2005-05-12 15:29:42 -04001032 /* get major version */
Tejun Heo3d2ca912006-02-12 22:47:04 +09001033 major_version = ata_id_major_version(dev->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
Albert Lee8bf62ec2005-05-12 15:29:42 -04001035 /*
1036 * The exact sequence expected by certain pre-ATA4 drives is:
1037 * SRST RESET
1038 * IDENTIFY
1039 * INITIALIZE DEVICE PARAMETERS
1040 * anything else..
1041 * Some drives were very specific about that exact sequence.
1042 */
Albert Lee59a10b12005-10-12 15:09:42 +08001043 if (major_version < 4 || (!ata_id_has_lba(dev->id))) {
Albert Lee8bf62ec2005-05-12 15:29:42 -04001044 ata_dev_init_params(ap, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045
Albert Lee59a10b12005-10-12 15:09:42 +08001046 /* current CHS translation info (id[53-58]) might be
1047 * changed. reread the identify device info.
1048 */
1049 ata_dev_reread_id(ap, dev);
1050 }
1051
Albert Lee8bf62ec2005-05-12 15:29:42 -04001052 if (ata_id_has_lba(dev->id)) {
1053 dev->flags |= ATA_DFLAG_LBA;
1054
Tejun Heo29407402006-02-12 22:47:04 +09001055 if (ata_id_has_lba48(dev->id))
Albert Lee8bf62ec2005-05-12 15:29:42 -04001056 dev->flags |= ATA_DFLAG_LBA48;
Albert Lee8bf62ec2005-05-12 15:29:42 -04001057
1058 /* print device info to dmesg */
1059 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors:%s\n",
1060 ap->id, device,
1061 major_version,
1062 ata_mode_string(xfer_modes),
1063 (unsigned long long)dev->n_sectors,
1064 dev->flags & ATA_DFLAG_LBA48 ? " LBA48" : " LBA");
1065 } else {
1066 /* CHS */
1067
1068 /* Default translation */
1069 dev->cylinders = dev->id[1];
1070 dev->heads = dev->id[3];
1071 dev->sectors = dev->id[6];
Albert Lee8bf62ec2005-05-12 15:29:42 -04001072
1073 if (ata_id_current_chs_valid(dev->id)) {
1074 /* Current CHS translation is valid. */
1075 dev->cylinders = dev->id[54];
1076 dev->heads = dev->id[55];
1077 dev->sectors = dev->id[56];
Albert Lee8bf62ec2005-05-12 15:29:42 -04001078 }
1079
1080 /* print device info to dmesg */
1081 printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors: CHS %d/%d/%d\n",
1082 ap->id, device,
1083 major_version,
1084 ata_mode_string(xfer_modes),
1085 (unsigned long long)dev->n_sectors,
1086 (int)dev->cylinders, (int)dev->heads, (int)dev->sectors);
1087
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 }
1089
Tejun Heo6e7846e2006-02-12 23:32:58 +09001090 dev->cdb_len = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 }
1092
1093 /* ATAPI-specific feature tests */
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05001094 else if (dev->class == ATA_DEV_ATAPI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 if (ata_id_is_ata(dev->id)) /* sanity check */
1096 goto err_out_nosup;
1097
1098 rc = atapi_cdb_len(dev->id);
1099 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
1100 printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
1101 goto err_out_nosup;
1102 }
Tejun Heo6e7846e2006-02-12 23:32:58 +09001103 dev->cdb_len = (unsigned int) rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
1105 /* print device info to dmesg */
1106 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
1107 ap->id, device,
1108 ata_mode_string(xfer_modes));
1109 }
1110
Tejun Heo6e7846e2006-02-12 23:32:58 +09001111 ap->host->max_cmd_len = 0;
1112 for (i = 0; i < ATA_MAX_DEVICES; i++)
1113 ap->host->max_cmd_len = max_t(unsigned int,
1114 ap->host->max_cmd_len,
1115 ap->device[i].cdb_len);
1116
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
1118 return;
1119
1120err_out_nosup:
1121 printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n",
1122 ap->id, device);
1123err_out:
1124 dev->class++; /* converts ATA_DEV_xxx into ATA_DEV_xxx_UNSUP */
1125 DPRINTK("EXIT, err\n");
1126}
1127
Brad Campbell6f2f3812005-05-12 15:07:47 -04001128
Tejun Heo8eabd022006-02-12 23:32:58 +09001129static inline u8 ata_dev_knobble(const struct ata_port *ap,
1130 struct ata_device *dev)
Brad Campbell6f2f3812005-05-12 15:07:47 -04001131{
Tejun Heo8eabd022006-02-12 23:32:58 +09001132 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
Brad Campbell6f2f3812005-05-12 15:07:47 -04001133}
1134
1135/**
Randy Dunlapc893a3a2006-01-28 13:15:32 -05001136 * ata_dev_config - Run device specific handlers & check for SATA->PATA bridges
1137 * @ap: Bus
1138 * @i: Device
Brad Campbell6f2f3812005-05-12 15:07:47 -04001139 *
Randy Dunlapc893a3a2006-01-28 13:15:32 -05001140 * LOCKING:
Brad Campbell6f2f3812005-05-12 15:07:47 -04001141 */
Jeff Garzik8a60a072005-07-31 13:13:24 -04001142
Brad Campbell6f2f3812005-05-12 15:07:47 -04001143void ata_dev_config(struct ata_port *ap, unsigned int i)
1144{
1145 /* limit bridge transfers to udma5, 200 sectors */
Tejun Heo8eabd022006-02-12 23:32:58 +09001146 if (ata_dev_knobble(ap, &ap->device[i])) {
Brad Campbell6f2f3812005-05-12 15:07:47 -04001147 printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
Tejun Heo8eabd022006-02-12 23:32:58 +09001148 ap->id, i);
Brad Campbell6f2f3812005-05-12 15:07:47 -04001149 ap->udma_mask &= ATA_UDMA5;
Tejun Heob00eec12006-02-12 23:32:59 +09001150 ap->device[i].max_sectors = ATA_MAX_SECTORS;
Brad Campbell6f2f3812005-05-12 15:07:47 -04001151 }
1152
1153 if (ap->ops->dev_config)
1154 ap->ops->dev_config(ap, &ap->device[i]);
1155}
1156
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157/**
1158 * ata_bus_probe - Reset and probe ATA bus
1159 * @ap: Bus to probe
1160 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001161 * Master ATA bus probing function. Initiates a hardware-dependent
1162 * bus reset, then attempts to identify any devices found on
1163 * the bus.
1164 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001166 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 *
1168 * RETURNS:
1169 * Zero on success, non-zero on error.
1170 */
1171
1172static int ata_bus_probe(struct ata_port *ap)
1173{
1174 unsigned int i, found = 0;
1175
Tejun Heoc19ba8a2006-01-24 17:05:22 +09001176 if (ap->ops->probe_reset) {
1177 unsigned int classes[ATA_MAX_DEVICES];
1178 int rc;
1179
1180 ata_port_probe(ap);
1181
1182 rc = ap->ops->probe_reset(ap, classes);
1183 if (rc == 0) {
Tejun Heo06ab7822006-02-12 15:01:49 +09001184 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1185 if (classes[i] == ATA_DEV_UNKNOWN)
1186 classes[i] = ATA_DEV_NONE;
Tejun Heoc19ba8a2006-01-24 17:05:22 +09001187 ap->device[i].class = classes[i];
Tejun Heo06ab7822006-02-12 15:01:49 +09001188 }
Tejun Heoc19ba8a2006-01-24 17:05:22 +09001189 } else {
1190 printk(KERN_ERR "ata%u: probe reset failed, "
1191 "disabling port\n", ap->id);
1192 ata_port_disable(ap);
1193 }
1194 } else
1195 ap->ops->phy_reset(ap);
1196
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1198 goto err_out;
1199
1200 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1201 ata_dev_identify(ap, i);
1202 if (ata_dev_present(&ap->device[i])) {
1203 found = 1;
Brad Campbell6f2f3812005-05-12 15:07:47 -04001204 ata_dev_config(ap,i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 }
1206 }
1207
1208 if ((!found) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1209 goto err_out_disable;
1210
1211 ata_set_mode(ap);
1212 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1213 goto err_out_disable;
1214
1215 return 0;
1216
1217err_out_disable:
1218 ap->ops->port_disable(ap);
1219err_out:
1220 return -1;
1221}
1222
1223/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001224 * ata_port_probe - Mark port as enabled
1225 * @ap: Port for which we indicate enablement
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001227 * Modify @ap data structure such that the system
1228 * thinks that the entire port is enabled.
1229 *
1230 * LOCKING: host_set lock, or some other form of
1231 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 */
1233
1234void ata_port_probe(struct ata_port *ap)
1235{
1236 ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1237}
1238
1239/**
Tejun Heo3be680b2005-12-19 22:35:02 +09001240 * sata_print_link_status - Print SATA link status
1241 * @ap: SATA port to printk link status about
1242 *
1243 * This function prints link speed and status of a SATA link.
1244 *
1245 * LOCKING:
1246 * None.
1247 */
1248static void sata_print_link_status(struct ata_port *ap)
1249{
1250 u32 sstatus, tmp;
1251 const char *speed;
1252
1253 if (!ap->ops->scr_read)
1254 return;
1255
1256 sstatus = scr_read(ap, SCR_STATUS);
1257
1258 if (sata_dev_present(ap)) {
1259 tmp = (sstatus >> 4) & 0xf;
1260 if (tmp & (1 << 0))
1261 speed = "1.5";
1262 else if (tmp & (1 << 1))
1263 speed = "3.0";
1264 else
1265 speed = "<unknown>";
1266 printk(KERN_INFO "ata%u: SATA link up %s Gbps (SStatus %X)\n",
1267 ap->id, speed, sstatus);
1268 } else {
1269 printk(KERN_INFO "ata%u: SATA link down (SStatus %X)\n",
1270 ap->id, sstatus);
1271 }
1272}
1273
1274/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001275 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1276 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001278 * This function issues commands to standard SATA Sxxx
1279 * PHY registers, to wake up the phy (and device), and
1280 * clear any reset condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 *
1282 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001283 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 *
1285 */
1286void __sata_phy_reset(struct ata_port *ap)
1287{
1288 u32 sstatus;
1289 unsigned long timeout = jiffies + (HZ * 5);
1290
1291 if (ap->flags & ATA_FLAG_SATA_RESET) {
Brett Russcdcca892005-03-28 15:10:27 -05001292 /* issue phy wake/reset */
1293 scr_write_flush(ap, SCR_CONTROL, 0x301);
Tejun Heo62ba2842005-06-26 23:27:19 +09001294 /* Couldn't find anything in SATA I/II specs, but
1295 * AHCI-1.1 10.4.2 says at least 1 ms. */
1296 mdelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 }
Brett Russcdcca892005-03-28 15:10:27 -05001298 scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
1300 /* wait for phy to become ready, if necessary */
1301 do {
1302 msleep(200);
1303 sstatus = scr_read(ap, SCR_STATUS);
1304 if ((sstatus & 0xf) != 1)
1305 break;
1306 } while (time_before(jiffies, timeout));
1307
Tejun Heo3be680b2005-12-19 22:35:02 +09001308 /* print link status */
1309 sata_print_link_status(ap);
Jeff Garzik656563e2005-11-20 03:36:45 -05001310
Tejun Heo3be680b2005-12-19 22:35:02 +09001311 /* TODO: phy layer with polling, timeouts, etc. */
1312 if (sata_dev_present(ap))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 ata_port_probe(ap);
Tejun Heo3be680b2005-12-19 22:35:02 +09001314 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 ata_port_disable(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
1317 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1318 return;
1319
1320 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1321 ata_port_disable(ap);
1322 return;
1323 }
1324
1325 ap->cbl = ATA_CBL_SATA;
1326}
1327
1328/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001329 * sata_phy_reset - Reset SATA bus.
1330 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001332 * This function resets the SATA bus, and then probes
1333 * the bus for devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 *
1335 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001336 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 *
1338 */
1339void sata_phy_reset(struct ata_port *ap)
1340{
1341 __sata_phy_reset(ap);
1342 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1343 return;
1344 ata_bus_reset(ap);
1345}
1346
1347/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001348 * ata_port_disable - Disable port.
1349 * @ap: Port to be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001351 * Modify @ap data structure such that the system
1352 * thinks that the entire port is disabled, and should
1353 * never attempt to probe or communicate with devices
1354 * on this port.
1355 *
1356 * LOCKING: host_set lock, or some other form of
1357 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 */
1359
1360void ata_port_disable(struct ata_port *ap)
1361{
1362 ap->device[0].class = ATA_DEV_NONE;
1363 ap->device[1].class = ATA_DEV_NONE;
1364 ap->flags |= ATA_FLAG_PORT_DISABLED;
1365}
1366
Alan Cox452503f2005-10-21 19:01:32 -04001367/*
1368 * This mode timing computation functionality is ported over from
1369 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
1370 */
1371/*
1372 * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
1373 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
1374 * for PIO 5, which is a nonstandard extension and UDMA6, which
1375 * is currently supported only by Maxtor drives.
1376 */
1377
1378static const struct ata_timing ata_timing[] = {
1379
1380 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
1381 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
1382 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
1383 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
1384
1385 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
1386 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
1387 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
1388
1389/* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
1390
1391 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
1392 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
1393 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
1394
1395 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
1396 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
1397 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
1398
1399/* { XFER_PIO_5, 20, 50, 30, 100, 50, 30, 100, 0 }, */
1400 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
1401 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
1402
1403 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
1404 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
1405 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
1406
1407/* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
1408
1409 { 0xFF }
1410};
1411
1412#define ENOUGH(v,unit) (((v)-1)/(unit)+1)
1413#define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
1414
1415static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
1416{
1417 q->setup = EZ(t->setup * 1000, T);
1418 q->act8b = EZ(t->act8b * 1000, T);
1419 q->rec8b = EZ(t->rec8b * 1000, T);
1420 q->cyc8b = EZ(t->cyc8b * 1000, T);
1421 q->active = EZ(t->active * 1000, T);
1422 q->recover = EZ(t->recover * 1000, T);
1423 q->cycle = EZ(t->cycle * 1000, T);
1424 q->udma = EZ(t->udma * 1000, UT);
1425}
1426
1427void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
1428 struct ata_timing *m, unsigned int what)
1429{
1430 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
1431 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
1432 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
1433 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
1434 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
1435 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
1436 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
1437 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
1438}
1439
1440static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
1441{
1442 const struct ata_timing *t;
1443
1444 for (t = ata_timing; t->mode != speed; t++)
Alan Cox91190752005-10-26 12:17:46 -04001445 if (t->mode == 0xFF)
Alan Cox452503f2005-10-21 19:01:32 -04001446 return NULL;
1447 return t;
1448}
1449
1450int ata_timing_compute(struct ata_device *adev, unsigned short speed,
1451 struct ata_timing *t, int T, int UT)
1452{
1453 const struct ata_timing *s;
1454 struct ata_timing p;
1455
1456 /*
1457 * Find the mode.
Albert Lee75b1f2f2005-11-16 17:06:18 +08001458 */
Alan Cox452503f2005-10-21 19:01:32 -04001459
1460 if (!(s = ata_timing_find_mode(speed)))
1461 return -EINVAL;
1462
Albert Lee75b1f2f2005-11-16 17:06:18 +08001463 memcpy(t, s, sizeof(*s));
1464
Alan Cox452503f2005-10-21 19:01:32 -04001465 /*
1466 * If the drive is an EIDE drive, it can tell us it needs extended
1467 * PIO/MW_DMA cycle timing.
1468 */
1469
1470 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
1471 memset(&p, 0, sizeof(p));
1472 if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
1473 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
1474 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
1475 } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
1476 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
1477 }
1478 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
1479 }
1480
1481 /*
1482 * Convert the timing to bus clock counts.
1483 */
1484
Albert Lee75b1f2f2005-11-16 17:06:18 +08001485 ata_timing_quantize(t, t, T, UT);
Alan Cox452503f2005-10-21 19:01:32 -04001486
1487 /*
Randy Dunlapc893a3a2006-01-28 13:15:32 -05001488 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
1489 * S.M.A.R.T * and some other commands. We have to ensure that the
1490 * DMA cycle timing is slower/equal than the fastest PIO timing.
Alan Cox452503f2005-10-21 19:01:32 -04001491 */
1492
1493 if (speed > XFER_PIO_4) {
1494 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
1495 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
1496 }
1497
1498 /*
Randy Dunlapc893a3a2006-01-28 13:15:32 -05001499 * Lengthen active & recovery time so that cycle time is correct.
Alan Cox452503f2005-10-21 19:01:32 -04001500 */
1501
1502 if (t->act8b + t->rec8b < t->cyc8b) {
1503 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
1504 t->rec8b = t->cyc8b - t->act8b;
1505 }
1506
1507 if (t->active + t->recover < t->cycle) {
1508 t->active += (t->cycle - (t->active + t->recover)) / 2;
1509 t->recover = t->cycle - t->active;
1510 }
1511
1512 return 0;
1513}
1514
Jeff Garzik057ace52005-10-22 14:27:05 -04001515static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001516 unsigned int shift;
1517 u8 base;
1518} xfer_mode_classes[] = {
1519 { ATA_SHIFT_UDMA, XFER_UDMA_0 },
1520 { ATA_SHIFT_MWDMA, XFER_MW_DMA_0 },
1521 { ATA_SHIFT_PIO, XFER_PIO_0 },
1522};
1523
Arjan van de Ven858119e2006-01-14 13:20:43 -08001524static u8 base_from_shift(unsigned int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525{
1526 int i;
1527
1528 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++)
1529 if (xfer_mode_classes[i].shift == shift)
1530 return xfer_mode_classes[i].base;
1531
1532 return 0xff;
1533}
1534
1535static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
1536{
1537 int ofs, idx;
1538 u8 base;
1539
1540 if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED))
1541 return;
1542
1543 if (dev->xfer_shift == ATA_SHIFT_PIO)
1544 dev->flags |= ATA_DFLAG_PIO;
1545
1546 ata_dev_set_xfermode(ap, dev);
1547
1548 base = base_from_shift(dev->xfer_shift);
1549 ofs = dev->xfer_mode - base;
1550 idx = ofs + dev->xfer_shift;
1551 WARN_ON(idx >= ARRAY_SIZE(xfer_mode_str));
1552
1553 DPRINTK("idx=%d xfer_shift=%u, xfer_mode=0x%x, base=0x%x, offset=%d\n",
1554 idx, dev->xfer_shift, (int)dev->xfer_mode, (int)base, ofs);
1555
1556 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
1557 ap->id, dev->devno, xfer_mode_str[idx]);
1558}
1559
1560static int ata_host_set_pio(struct ata_port *ap)
1561{
1562 unsigned int mask;
1563 int x, i;
1564 u8 base, xfer_mode;
1565
1566 mask = ata_get_mode_mask(ap, ATA_SHIFT_PIO);
1567 x = fgb(mask);
1568 if (x < 0) {
1569 printk(KERN_WARNING "ata%u: no PIO support\n", ap->id);
1570 return -1;
1571 }
1572
1573 base = base_from_shift(ATA_SHIFT_PIO);
1574 xfer_mode = base + x;
1575
1576 DPRINTK("base 0x%x xfer_mode 0x%x mask 0x%x x %d\n",
1577 (int)base, (int)xfer_mode, mask, x);
1578
1579 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1580 struct ata_device *dev = &ap->device[i];
1581 if (ata_dev_present(dev)) {
1582 dev->pio_mode = xfer_mode;
1583 dev->xfer_mode = xfer_mode;
1584 dev->xfer_shift = ATA_SHIFT_PIO;
1585 if (ap->ops->set_piomode)
1586 ap->ops->set_piomode(ap, dev);
1587 }
1588 }
1589
1590 return 0;
1591}
1592
1593static void ata_host_set_dma(struct ata_port *ap, u8 xfer_mode,
1594 unsigned int xfer_shift)
1595{
1596 int i;
1597
1598 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1599 struct ata_device *dev = &ap->device[i];
1600 if (ata_dev_present(dev)) {
1601 dev->dma_mode = xfer_mode;
1602 dev->xfer_mode = xfer_mode;
1603 dev->xfer_shift = xfer_shift;
1604 if (ap->ops->set_dmamode)
1605 ap->ops->set_dmamode(ap, dev);
1606 }
1607 }
1608}
1609
1610/**
1611 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1612 * @ap: port on which timings will be programmed
1613 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001614 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1615 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001617 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 */
1619static void ata_set_mode(struct ata_port *ap)
1620{
Albert Lee8cbd6df2005-10-12 15:06:27 +08001621 unsigned int xfer_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 u8 xfer_mode;
1623 int rc;
1624
1625 /* step 1: always set host PIO timings */
1626 rc = ata_host_set_pio(ap);
1627 if (rc)
1628 goto err_out;
1629
1630 /* step 2: choose the best data xfer mode */
1631 xfer_mode = xfer_shift = 0;
1632 rc = ata_choose_xfer_mode(ap, &xfer_mode, &xfer_shift);
1633 if (rc)
1634 goto err_out;
1635
1636 /* step 3: if that xfer mode isn't PIO, set host DMA timings */
1637 if (xfer_shift != ATA_SHIFT_PIO)
1638 ata_host_set_dma(ap, xfer_mode, xfer_shift);
1639
1640 /* step 4: update devices' xfer mode */
1641 ata_dev_set_mode(ap, &ap->device[0]);
1642 ata_dev_set_mode(ap, &ap->device[1]);
1643
1644 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1645 return;
1646
1647 if (ap->ops->post_set_mode)
1648 ap->ops->post_set_mode(ap);
1649
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 return;
1651
1652err_out:
1653 ata_port_disable(ap);
1654}
1655
1656/**
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05001657 * ata_tf_to_host - issue ATA taskfile to host controller
1658 * @ap: port to which command is being issued
1659 * @tf: ATA taskfile register set
1660 *
1661 * Issues ATA taskfile register set to ATA host controller,
1662 * with proper synchronization with interrupt handler and
1663 * other threads.
1664 *
1665 * LOCKING:
1666 * spin_lock_irqsave(host_set lock)
1667 */
1668
1669static inline void ata_tf_to_host(struct ata_port *ap,
1670 const struct ata_taskfile *tf)
1671{
1672 ap->ops->tf_load(ap, tf);
1673 ap->ops->exec_command(ap, tf);
1674}
1675
1676/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 * ata_busy_sleep - sleep until BSY clears, or timeout
1678 * @ap: port containing status register to be polled
1679 * @tmout_pat: impatience timeout
1680 * @tmout: overall timeout
1681 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001682 * Sleep until ATA Status register bit BSY clears,
1683 * or a timeout occurs.
1684 *
1685 * LOCKING: None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 */
1687
Tejun Heo6f8b9952006-01-24 17:05:21 +09001688unsigned int ata_busy_sleep (struct ata_port *ap,
1689 unsigned long tmout_pat, unsigned long tmout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690{
1691 unsigned long timer_start, timeout;
1692 u8 status;
1693
1694 status = ata_busy_wait(ap, ATA_BUSY, 300);
1695 timer_start = jiffies;
1696 timeout = timer_start + tmout_pat;
1697 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1698 msleep(50);
1699 status = ata_busy_wait(ap, ATA_BUSY, 3);
1700 }
1701
1702 if (status & ATA_BUSY)
1703 printk(KERN_WARNING "ata%u is slow to respond, "
1704 "please be patient\n", ap->id);
1705
1706 timeout = timer_start + tmout;
1707 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1708 msleep(50);
1709 status = ata_chk_status(ap);
1710 }
1711
1712 if (status & ATA_BUSY) {
1713 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
1714 ap->id, tmout / HZ);
1715 return 1;
1716 }
1717
1718 return 0;
1719}
1720
1721static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
1722{
1723 struct ata_ioports *ioaddr = &ap->ioaddr;
1724 unsigned int dev0 = devmask & (1 << 0);
1725 unsigned int dev1 = devmask & (1 << 1);
1726 unsigned long timeout;
1727
1728 /* if device 0 was found in ata_devchk, wait for its
1729 * BSY bit to clear
1730 */
1731 if (dev0)
1732 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1733
1734 /* if device 1 was found in ata_devchk, wait for
1735 * register access, then wait for BSY to clear
1736 */
1737 timeout = jiffies + ATA_TMOUT_BOOT;
1738 while (dev1) {
1739 u8 nsect, lbal;
1740
1741 ap->ops->dev_select(ap, 1);
1742 if (ap->flags & ATA_FLAG_MMIO) {
1743 nsect = readb((void __iomem *) ioaddr->nsect_addr);
1744 lbal = readb((void __iomem *) ioaddr->lbal_addr);
1745 } else {
1746 nsect = inb(ioaddr->nsect_addr);
1747 lbal = inb(ioaddr->lbal_addr);
1748 }
1749 if ((nsect == 1) && (lbal == 1))
1750 break;
1751 if (time_after(jiffies, timeout)) {
1752 dev1 = 0;
1753 break;
1754 }
1755 msleep(50); /* give drive a breather */
1756 }
1757 if (dev1)
1758 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1759
1760 /* is all this really necessary? */
1761 ap->ops->dev_select(ap, 0);
1762 if (dev1)
1763 ap->ops->dev_select(ap, 1);
1764 if (dev0)
1765 ap->ops->dev_select(ap, 0);
1766}
1767
1768/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001769 * ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command.
1770 * @ap: Port to reset and probe
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001772 * Use the EXECUTE DEVICE DIAGNOSTIC command to reset and
1773 * probe the bus. Not often used these days.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 *
1775 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001776 * PCI/etc. bus probe sem.
Jeff Garzike5338252005-10-30 21:37:17 -05001777 * Obtains host_set lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 *
1779 */
1780
1781static unsigned int ata_bus_edd(struct ata_port *ap)
1782{
1783 struct ata_taskfile tf;
Jeff Garzike5338252005-10-30 21:37:17 -05001784 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785
1786 /* set up execute-device-diag (bus reset) taskfile */
1787 /* also, take interrupts to a known state (disabled) */
1788 DPRINTK("execute-device-diag\n");
1789 ata_tf_init(ap, &tf, 0);
1790 tf.ctl |= ATA_NIEN;
1791 tf.command = ATA_CMD_EDD;
1792 tf.protocol = ATA_PROT_NODATA;
1793
1794 /* do bus reset */
Jeff Garzike5338252005-10-30 21:37:17 -05001795 spin_lock_irqsave(&ap->host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 ata_tf_to_host(ap, &tf);
Jeff Garzike5338252005-10-30 21:37:17 -05001797 spin_unlock_irqrestore(&ap->host_set->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798
1799 /* spec says at least 2ms. but who knows with those
1800 * crazy ATAPI devices...
1801 */
1802 msleep(150);
1803
1804 return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1805}
1806
1807static unsigned int ata_bus_softreset(struct ata_port *ap,
1808 unsigned int devmask)
1809{
1810 struct ata_ioports *ioaddr = &ap->ioaddr;
1811
1812 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
1813
1814 /* software reset. causes dev0 to be selected */
1815 if (ap->flags & ATA_FLAG_MMIO) {
1816 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1817 udelay(20); /* FIXME: flush */
1818 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
1819 udelay(20); /* FIXME: flush */
1820 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1821 } else {
1822 outb(ap->ctl, ioaddr->ctl_addr);
1823 udelay(10);
1824 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
1825 udelay(10);
1826 outb(ap->ctl, ioaddr->ctl_addr);
1827 }
1828
1829 /* spec mandates ">= 2ms" before checking status.
1830 * We wait 150ms, because that was the magic delay used for
1831 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
1832 * between when the ATA command register is written, and then
1833 * status is checked. Because waiting for "a while" before
1834 * checking status is fine, post SRST, we perform this magic
1835 * delay here as well.
1836 */
1837 msleep(150);
1838
1839 ata_bus_post_reset(ap, devmask);
1840
1841 return 0;
1842}
1843
1844/**
1845 * ata_bus_reset - reset host port and associated ATA channel
1846 * @ap: port to reset
1847 *
1848 * This is typically the first time we actually start issuing
1849 * commands to the ATA channel. We wait for BSY to clear, then
1850 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
1851 * result. Determine what devices, if any, are on the channel
1852 * by looking at the device 0/1 error register. Look at the signature
1853 * stored in each device's taskfile registers, to determine if
1854 * the device is ATA or ATAPI.
1855 *
1856 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001857 * PCI/etc. bus probe sem.
1858 * Obtains host_set lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 *
1860 * SIDE EFFECTS:
1861 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
1862 */
1863
1864void ata_bus_reset(struct ata_port *ap)
1865{
1866 struct ata_ioports *ioaddr = &ap->ioaddr;
1867 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
1868 u8 err;
1869 unsigned int dev0, dev1 = 0, rc = 0, devmask = 0;
1870
1871 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
1872
1873 /* determine if device 0/1 are present */
1874 if (ap->flags & ATA_FLAG_SATA_RESET)
1875 dev0 = 1;
1876 else {
1877 dev0 = ata_devchk(ap, 0);
1878 if (slave_possible)
1879 dev1 = ata_devchk(ap, 1);
1880 }
1881
1882 if (dev0)
1883 devmask |= (1 << 0);
1884 if (dev1)
1885 devmask |= (1 << 1);
1886
1887 /* select device 0 again */
1888 ap->ops->dev_select(ap, 0);
1889
1890 /* issue bus reset */
1891 if (ap->flags & ATA_FLAG_SRST)
1892 rc = ata_bus_softreset(ap, devmask);
1893 else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) {
1894 /* set up device control */
1895 if (ap->flags & ATA_FLAG_MMIO)
1896 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1897 else
1898 outb(ap->ctl, ioaddr->ctl_addr);
1899 rc = ata_bus_edd(ap);
1900 }
1901
1902 if (rc)
1903 goto err_out;
1904
1905 /*
1906 * determine by signature whether we have ATA or ATAPI devices
1907 */
Tejun Heob4dc7622006-01-24 17:05:22 +09001908 ap->device[0].class = ata_dev_try_classify(ap, 0, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 if ((slave_possible) && (err != 0x81))
Tejun Heob4dc7622006-01-24 17:05:22 +09001910 ap->device[1].class = ata_dev_try_classify(ap, 1, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911
1912 /* re-enable interrupts */
1913 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
1914 ata_irq_on(ap);
1915
1916 /* is double-select really necessary? */
1917 if (ap->device[1].class != ATA_DEV_NONE)
1918 ap->ops->dev_select(ap, 1);
1919 if (ap->device[0].class != ATA_DEV_NONE)
1920 ap->ops->dev_select(ap, 0);
1921
1922 /* if no devices were detected, disable this port */
1923 if ((ap->device[0].class == ATA_DEV_NONE) &&
1924 (ap->device[1].class == ATA_DEV_NONE))
1925 goto err_out;
1926
1927 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
1928 /* set up device control for ATA_FLAG_SATA_RESET */
1929 if (ap->flags & ATA_FLAG_MMIO)
1930 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
1931 else
1932 outb(ap->ctl, ioaddr->ctl_addr);
1933 }
1934
1935 DPRINTK("EXIT\n");
1936 return;
1937
1938err_out:
1939 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
1940 ap->ops->port_disable(ap);
1941
1942 DPRINTK("EXIT\n");
1943}
1944
Tejun Heo7a7921e2006-02-02 18:20:00 +09001945static int sata_phy_resume(struct ata_port *ap)
1946{
1947 unsigned long timeout = jiffies + (HZ * 5);
1948 u32 sstatus;
1949
1950 scr_write_flush(ap, SCR_CONTROL, 0x300);
1951
1952 /* Wait for phy to become ready, if necessary. */
1953 do {
1954 msleep(200);
1955 sstatus = scr_read(ap, SCR_STATUS);
1956 if ((sstatus & 0xf) != 1)
1957 return 0;
1958 } while (time_before(jiffies, timeout));
1959
1960 return -1;
1961}
1962
Tejun Heoc2bd5802006-01-24 17:05:22 +09001963/**
Tejun Heo8a19ac82006-02-02 18:20:00 +09001964 * ata_std_probeinit - initialize probing
1965 * @ap: port to be probed
1966 *
1967 * @ap is about to be probed. Initialize it. This function is
1968 * to be used as standard callback for ata_drive_probe_reset().
Tejun Heo3a397462006-02-10 23:58:48 +09001969 *
1970 * NOTE!!! Do not use this function as probeinit if a low level
1971 * driver implements only hardreset. Just pass NULL as probeinit
1972 * in that case. Using this function is probably okay but doing
1973 * so makes reset sequence different from the original
1974 * ->phy_reset implementation and Jeff nervous. :-P
Tejun Heo8a19ac82006-02-02 18:20:00 +09001975 */
1976extern void ata_std_probeinit(struct ata_port *ap)
1977{
Tejun Heo3a397462006-02-10 23:58:48 +09001978 if (ap->flags & ATA_FLAG_SATA && ap->ops->scr_read) {
Tejun Heo8a19ac82006-02-02 18:20:00 +09001979 sata_phy_resume(ap);
Tejun Heo3a397462006-02-10 23:58:48 +09001980 if (sata_dev_present(ap))
1981 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1982 }
Tejun Heo8a19ac82006-02-02 18:20:00 +09001983}
1984
1985/**
Tejun Heoc2bd5802006-01-24 17:05:22 +09001986 * ata_std_softreset - reset host port via ATA SRST
1987 * @ap: port to reset
1988 * @verbose: fail verbosely
1989 * @classes: resulting classes of attached devices
1990 *
1991 * Reset host port using ATA SRST. This function is to be used
1992 * as standard callback for ata_drive_*_reset() functions.
1993 *
1994 * LOCKING:
1995 * Kernel thread context (may sleep)
1996 *
1997 * RETURNS:
1998 * 0 on success, -errno otherwise.
1999 */
2000int ata_std_softreset(struct ata_port *ap, int verbose, unsigned int *classes)
2001{
2002 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2003 unsigned int devmask = 0, err_mask;
2004 u8 err;
2005
2006 DPRINTK("ENTER\n");
2007
Tejun Heo3a397462006-02-10 23:58:48 +09002008 if (ap->ops->scr_read && !sata_dev_present(ap)) {
2009 classes[0] = ATA_DEV_NONE;
2010 goto out;
2011 }
2012
Tejun Heoc2bd5802006-01-24 17:05:22 +09002013 /* determine if device 0/1 are present */
2014 if (ata_devchk(ap, 0))
2015 devmask |= (1 << 0);
2016 if (slave_possible && ata_devchk(ap, 1))
2017 devmask |= (1 << 1);
2018
Tejun Heoc2bd5802006-01-24 17:05:22 +09002019 /* select device 0 again */
2020 ap->ops->dev_select(ap, 0);
2021
2022 /* issue bus reset */
2023 DPRINTK("about to softreset, devmask=%x\n", devmask);
2024 err_mask = ata_bus_softreset(ap, devmask);
2025 if (err_mask) {
2026 if (verbose)
2027 printk(KERN_ERR "ata%u: SRST failed (err_mask=0x%x)\n",
2028 ap->id, err_mask);
2029 else
2030 DPRINTK("EXIT, softreset failed (err_mask=0x%x)\n",
2031 err_mask);
2032 return -EIO;
2033 }
2034
2035 /* determine by signature whether we have ATA or ATAPI devices */
2036 classes[0] = ata_dev_try_classify(ap, 0, &err);
2037 if (slave_possible && err != 0x81)
2038 classes[1] = ata_dev_try_classify(ap, 1, &err);
2039
Tejun Heo3a397462006-02-10 23:58:48 +09002040 out:
Tejun Heoc2bd5802006-01-24 17:05:22 +09002041 DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]);
2042 return 0;
2043}
2044
2045/**
2046 * sata_std_hardreset - reset host port via SATA phy reset
2047 * @ap: port to reset
2048 * @verbose: fail verbosely
2049 * @class: resulting class of attached device
2050 *
2051 * SATA phy-reset host port using DET bits of SControl register.
2052 * This function is to be used as standard callback for
2053 * ata_drive_*_reset().
2054 *
2055 * LOCKING:
2056 * Kernel thread context (may sleep)
2057 *
2058 * RETURNS:
2059 * 0 on success, -errno otherwise.
2060 */
2061int sata_std_hardreset(struct ata_port *ap, int verbose, unsigned int *class)
2062{
Tejun Heoc2bd5802006-01-24 17:05:22 +09002063 DPRINTK("ENTER\n");
2064
2065 /* Issue phy wake/reset */
2066 scr_write_flush(ap, SCR_CONTROL, 0x301);
2067
2068 /*
2069 * Couldn't find anything in SATA I/II specs, but AHCI-1.1
2070 * 10.4.2 says at least 1 ms.
2071 */
2072 msleep(1);
2073
Tejun Heo7a7921e2006-02-02 18:20:00 +09002074 /* Bring phy back */
2075 sata_phy_resume(ap);
Tejun Heoc2bd5802006-01-24 17:05:22 +09002076
Tejun Heoc2bd5802006-01-24 17:05:22 +09002077 /* TODO: phy layer with polling, timeouts, etc. */
2078 if (!sata_dev_present(ap)) {
2079 *class = ATA_DEV_NONE;
2080 DPRINTK("EXIT, link offline\n");
2081 return 0;
2082 }
2083
2084 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
2085 if (verbose)
2086 printk(KERN_ERR "ata%u: COMRESET failed "
2087 "(device not ready)\n", ap->id);
2088 else
2089 DPRINTK("EXIT, device not ready\n");
2090 return -EIO;
2091 }
2092
Tejun Heo3a397462006-02-10 23:58:48 +09002093 ap->ops->dev_select(ap, 0); /* probably unnecessary */
2094
Tejun Heoc2bd5802006-01-24 17:05:22 +09002095 *class = ata_dev_try_classify(ap, 0, NULL);
2096
2097 DPRINTK("EXIT, class=%u\n", *class);
2098 return 0;
2099}
2100
2101/**
2102 * ata_std_postreset - standard postreset callback
2103 * @ap: the target ata_port
2104 * @classes: classes of attached devices
2105 *
2106 * This function is invoked after a successful reset. Note that
2107 * the device might have been reset more than once using
2108 * different reset methods before postreset is invoked.
2109 * postreset is also reponsible for setting cable type.
2110 *
2111 * This function is to be used as standard callback for
2112 * ata_drive_*_reset().
2113 *
2114 * LOCKING:
2115 * Kernel thread context (may sleep)
2116 */
2117void ata_std_postreset(struct ata_port *ap, unsigned int *classes)
2118{
2119 DPRINTK("ENTER\n");
2120
2121 /* set cable type */
2122 if (ap->cbl == ATA_CBL_NONE && ap->flags & ATA_FLAG_SATA)
2123 ap->cbl = ATA_CBL_SATA;
2124
2125 /* print link status */
2126 if (ap->cbl == ATA_CBL_SATA)
2127 sata_print_link_status(ap);
2128
Tejun Heo3a397462006-02-10 23:58:48 +09002129 /* re-enable interrupts */
2130 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2131 ata_irq_on(ap);
Tejun Heoc2bd5802006-01-24 17:05:22 +09002132
2133 /* is double-select really necessary? */
2134 if (classes[0] != ATA_DEV_NONE)
2135 ap->ops->dev_select(ap, 1);
2136 if (classes[1] != ATA_DEV_NONE)
2137 ap->ops->dev_select(ap, 0);
2138
Tejun Heo3a397462006-02-10 23:58:48 +09002139 /* bail out if no device is present */
2140 if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
2141 DPRINTK("EXIT, no device\n");
2142 return;
2143 }
2144
2145 /* set up device control */
2146 if (ap->ioaddr.ctl_addr) {
2147 if (ap->flags & ATA_FLAG_MMIO)
2148 writeb(ap->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
2149 else
2150 outb(ap->ctl, ap->ioaddr.ctl_addr);
2151 }
Tejun Heoc2bd5802006-01-24 17:05:22 +09002152
2153 DPRINTK("EXIT\n");
2154}
2155
2156/**
2157 * ata_std_probe_reset - standard probe reset method
2158 * @ap: prot to perform probe-reset
2159 * @classes: resulting classes of attached devices
2160 *
2161 * The stock off-the-shelf ->probe_reset method.
2162 *
2163 * LOCKING:
2164 * Kernel thread context (may sleep)
2165 *
2166 * RETURNS:
2167 * 0 on success, -errno otherwise.
2168 */
2169int ata_std_probe_reset(struct ata_port *ap, unsigned int *classes)
2170{
2171 ata_reset_fn_t hardreset;
2172
2173 hardreset = NULL;
Tejun Heob911fc32006-02-02 18:20:00 +09002174 if (ap->flags & ATA_FLAG_SATA && ap->ops->scr_read)
Tejun Heoc2bd5802006-01-24 17:05:22 +09002175 hardreset = sata_std_hardreset;
2176
Tejun Heo8a19ac82006-02-02 18:20:00 +09002177 return ata_drive_probe_reset(ap, ata_std_probeinit,
Tejun Heo7944ea92006-02-02 18:20:00 +09002178 ata_std_softreset, hardreset,
Tejun Heoc2bd5802006-01-24 17:05:22 +09002179 ata_std_postreset, classes);
2180}
2181
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002182static int do_probe_reset(struct ata_port *ap, ata_reset_fn_t reset,
2183 ata_postreset_fn_t postreset,
2184 unsigned int *classes)
2185{
2186 int i, rc;
2187
2188 for (i = 0; i < ATA_MAX_DEVICES; i++)
2189 classes[i] = ATA_DEV_UNKNOWN;
2190
2191 rc = reset(ap, 0, classes);
2192 if (rc)
2193 return rc;
2194
2195 /* If any class isn't ATA_DEV_UNKNOWN, consider classification
2196 * is complete and convert all ATA_DEV_UNKNOWN to
2197 * ATA_DEV_NONE.
2198 */
2199 for (i = 0; i < ATA_MAX_DEVICES; i++)
2200 if (classes[i] != ATA_DEV_UNKNOWN)
2201 break;
2202
2203 if (i < ATA_MAX_DEVICES)
2204 for (i = 0; i < ATA_MAX_DEVICES; i++)
2205 if (classes[i] == ATA_DEV_UNKNOWN)
2206 classes[i] = ATA_DEV_NONE;
2207
2208 if (postreset)
2209 postreset(ap, classes);
2210
2211 return classes[0] != ATA_DEV_UNKNOWN ? 0 : -ENODEV;
2212}
2213
2214/**
2215 * ata_drive_probe_reset - Perform probe reset with given methods
2216 * @ap: port to reset
Tejun Heo7944ea92006-02-02 18:20:00 +09002217 * @probeinit: probeinit method (can be NULL)
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002218 * @softreset: softreset method (can be NULL)
2219 * @hardreset: hardreset method (can be NULL)
2220 * @postreset: postreset method (can be NULL)
2221 * @classes: resulting classes of attached devices
2222 *
2223 * Reset the specified port and classify attached devices using
2224 * given methods. This function prefers softreset but tries all
2225 * possible reset sequences to reset and classify devices. This
2226 * function is intended to be used for constructing ->probe_reset
2227 * callback by low level drivers.
2228 *
2229 * Reset methods should follow the following rules.
2230 *
2231 * - Return 0 on sucess, -errno on failure.
2232 * - If classification is supported, fill classes[] with
2233 * recognized class codes.
2234 * - If classification is not supported, leave classes[] alone.
2235 * - If verbose is non-zero, print error message on failure;
2236 * otherwise, shut up.
2237 *
2238 * LOCKING:
2239 * Kernel thread context (may sleep)
2240 *
2241 * RETURNS:
2242 * 0 on success, -EINVAL if no reset method is avaliable, -ENODEV
2243 * if classification fails, and any error code from reset
2244 * methods.
2245 */
Tejun Heo7944ea92006-02-02 18:20:00 +09002246int ata_drive_probe_reset(struct ata_port *ap, ata_probeinit_fn_t probeinit,
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002247 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2248 ata_postreset_fn_t postreset, unsigned int *classes)
2249{
2250 int rc = -EINVAL;
2251
Tejun Heo7944ea92006-02-02 18:20:00 +09002252 if (probeinit)
2253 probeinit(ap);
2254
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002255 if (softreset) {
2256 rc = do_probe_reset(ap, softreset, postreset, classes);
2257 if (rc == 0)
2258 return 0;
2259 }
2260
2261 if (!hardreset)
2262 return rc;
2263
2264 rc = do_probe_reset(ap, hardreset, postreset, classes);
2265 if (rc == 0 || rc != -ENODEV)
2266 return rc;
2267
2268 if (softreset)
2269 rc = do_probe_reset(ap, softreset, postreset, classes);
2270
2271 return rc;
2272}
2273
Jeff Garzik057ace52005-10-22 14:27:05 -04002274static void ata_pr_blacklisted(const struct ata_port *ap,
2275 const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276{
2277 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, disabling DMA\n",
2278 ap->id, dev->devno);
2279}
2280
Arjan van de Ven98ac62d2005-11-28 10:06:23 +01002281static const char * const ata_dma_blacklist [] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 "WDC AC11000H",
2283 "WDC AC22100H",
2284 "WDC AC32500H",
2285 "WDC AC33100H",
2286 "WDC AC31600H",
2287 "WDC AC32100H",
2288 "WDC AC23200L",
2289 "Compaq CRD-8241B",
2290 "CRD-8400B",
2291 "CRD-8480B",
2292 "CRD-8482B",
2293 "CRD-84",
2294 "SanDisk SDP3B",
2295 "SanDisk SDP3B-64",
2296 "SANYO CD-ROM CRD",
2297 "HITACHI CDR-8",
2298 "HITACHI CDR-8335",
2299 "HITACHI CDR-8435",
2300 "Toshiba CD-ROM XM-6202B",
Jeff Garzike9222562005-06-28 00:03:37 -04002301 "TOSHIBA CD-ROM XM-1702BC",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 "CD-532E-A",
2303 "E-IDE CD-ROM CR-840",
2304 "CD-ROM Drive/F5A",
2305 "WPI CDD-820",
2306 "SAMSUNG CD-ROM SC-148C",
2307 "SAMSUNG CD-ROM SC",
2308 "SanDisk SDP3B-64",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 "ATAPI CD-ROM DRIVE 40X MAXIMUM",
2310 "_NEC DV5800A",
2311};
2312
Jeff Garzik057ace52005-10-22 14:27:05 -04002313static int ata_dma_blacklisted(const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314{
Tejun Heo2e026712006-02-12 22:47:04 +09002315 unsigned char model_num[41];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 int i;
2317
Tejun Heo6a62a042006-02-13 10:02:46 +09002318 ata_id_c_string(dev->id, model_num, ATA_ID_PROD_OFS, sizeof(model_num));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319
2320 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i++)
Tejun Heo2e026712006-02-12 22:47:04 +09002321 if (!strcmp(ata_dma_blacklist[i], model_num))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 return 1;
2323
2324 return 0;
2325}
2326
Jeff Garzik057ace52005-10-22 14:27:05 -04002327static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328{
Jeff Garzik057ace52005-10-22 14:27:05 -04002329 const struct ata_device *master, *slave;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 unsigned int mask;
2331
2332 master = &ap->device[0];
2333 slave = &ap->device[1];
2334
Tejun Heoa4631472006-02-11 19:11:13 +09002335 WARN_ON(!ata_dev_present(master) && !ata_dev_present(slave));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336
2337 if (shift == ATA_SHIFT_UDMA) {
2338 mask = ap->udma_mask;
2339 if (ata_dev_present(master)) {
2340 mask &= (master->id[ATA_ID_UDMA_MODES] & 0xff);
Jeff Garzik057ace52005-10-22 14:27:05 -04002341 if (ata_dma_blacklisted(master)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 mask = 0;
2343 ata_pr_blacklisted(ap, master);
2344 }
2345 }
2346 if (ata_dev_present(slave)) {
2347 mask &= (slave->id[ATA_ID_UDMA_MODES] & 0xff);
Jeff Garzik057ace52005-10-22 14:27:05 -04002348 if (ata_dma_blacklisted(slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 mask = 0;
2350 ata_pr_blacklisted(ap, slave);
2351 }
2352 }
2353 }
2354 else if (shift == ATA_SHIFT_MWDMA) {
2355 mask = ap->mwdma_mask;
2356 if (ata_dev_present(master)) {
2357 mask &= (master->id[ATA_ID_MWDMA_MODES] & 0x07);
Jeff Garzik057ace52005-10-22 14:27:05 -04002358 if (ata_dma_blacklisted(master)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 mask = 0;
2360 ata_pr_blacklisted(ap, master);
2361 }
2362 }
2363 if (ata_dev_present(slave)) {
2364 mask &= (slave->id[ATA_ID_MWDMA_MODES] & 0x07);
Jeff Garzik057ace52005-10-22 14:27:05 -04002365 if (ata_dma_blacklisted(slave)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 mask = 0;
2367 ata_pr_blacklisted(ap, slave);
2368 }
2369 }
2370 }
2371 else if (shift == ATA_SHIFT_PIO) {
2372 mask = ap->pio_mask;
2373 if (ata_dev_present(master)) {
2374 /* spec doesn't return explicit support for
2375 * PIO0-2, so we fake it
2376 */
2377 u16 tmp_mode = master->id[ATA_ID_PIO_MODES] & 0x03;
2378 tmp_mode <<= 3;
2379 tmp_mode |= 0x7;
2380 mask &= tmp_mode;
2381 }
2382 if (ata_dev_present(slave)) {
2383 /* spec doesn't return explicit support for
2384 * PIO0-2, so we fake it
2385 */
2386 u16 tmp_mode = slave->id[ATA_ID_PIO_MODES] & 0x03;
2387 tmp_mode <<= 3;
2388 tmp_mode |= 0x7;
2389 mask &= tmp_mode;
2390 }
2391 }
2392 else {
2393 mask = 0xffffffff; /* shut up compiler warning */
2394 BUG();
2395 }
2396
2397 return mask;
2398}
2399
2400/* find greatest bit */
2401static int fgb(u32 bitmap)
2402{
2403 unsigned int i;
2404 int x = -1;
2405
2406 for (i = 0; i < 32; i++)
2407 if (bitmap & (1 << i))
2408 x = i;
2409
2410 return x;
2411}
2412
2413/**
2414 * ata_choose_xfer_mode - attempt to find best transfer mode
2415 * @ap: Port for which an xfer mode will be selected
2416 * @xfer_mode_out: (output) SET FEATURES - XFER MODE code
2417 * @xfer_shift_out: (output) bit shift that selects this mode
2418 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04002419 * Based on host and device capabilities, determine the
2420 * maximum transfer mode that is amenable to all.
2421 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002423 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424 *
2425 * RETURNS:
2426 * Zero on success, negative on error.
2427 */
2428
Jeff Garzik057ace52005-10-22 14:27:05 -04002429static int ata_choose_xfer_mode(const struct ata_port *ap,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 u8 *xfer_mode_out,
2431 unsigned int *xfer_shift_out)
2432{
2433 unsigned int mask, shift;
2434 int x, i;
2435
2436 for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++) {
2437 shift = xfer_mode_classes[i].shift;
2438 mask = ata_get_mode_mask(ap, shift);
2439
2440 x = fgb(mask);
2441 if (x >= 0) {
2442 *xfer_mode_out = xfer_mode_classes[i].base + x;
2443 *xfer_shift_out = shift;
2444 return 0;
2445 }
2446 }
2447
2448 return -1;
2449}
2450
2451/**
2452 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2453 * @ap: Port associated with device @dev
2454 * @dev: Device to which command will be sent
2455 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002456 * Issue SET FEATURES - XFER MODE command to device @dev
2457 * on port @ap.
2458 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002459 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002460 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 */
2462
2463static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev)
2464{
Tejun Heoa0123702005-12-13 14:49:31 +09002465 struct ata_taskfile tf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466
2467 /* set up set-features taskfile */
2468 DPRINTK("set features - xfer mode\n");
2469
Tejun Heoa0123702005-12-13 14:49:31 +09002470 ata_tf_init(ap, &tf, dev->devno);
2471 tf.command = ATA_CMD_SET_FEATURES;
2472 tf.feature = SETFEATURES_XFER;
2473 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2474 tf.protocol = ATA_PROT_NODATA;
2475 tf.nsect = dev->xfer_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476
Tejun Heoa0123702005-12-13 14:49:31 +09002477 if (ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0)) {
2478 printk(KERN_ERR "ata%u: failed to set xfermode, disabled\n",
2479 ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 ata_port_disable(ap);
Tejun Heoa0123702005-12-13 14:49:31 +09002481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482
2483 DPRINTK("EXIT\n");
2484}
2485
2486/**
Albert Lee59a10b12005-10-12 15:09:42 +08002487 * ata_dev_reread_id - Reread the device identify device info
2488 * @ap: port where the device is
2489 * @dev: device to reread the identify device info
2490 *
2491 * LOCKING:
2492 */
2493
2494static void ata_dev_reread_id(struct ata_port *ap, struct ata_device *dev)
2495{
Tejun Heoa0123702005-12-13 14:49:31 +09002496 struct ata_taskfile tf;
Albert Lee59a10b12005-10-12 15:09:42 +08002497
Tejun Heoa0123702005-12-13 14:49:31 +09002498 ata_tf_init(ap, &tf, dev->devno);
Albert Lee59a10b12005-10-12 15:09:42 +08002499
2500 if (dev->class == ATA_DEV_ATA) {
Tejun Heoa0123702005-12-13 14:49:31 +09002501 tf.command = ATA_CMD_ID_ATA;
Albert Lee59a10b12005-10-12 15:09:42 +08002502 DPRINTK("do ATA identify\n");
2503 } else {
Tejun Heoa0123702005-12-13 14:49:31 +09002504 tf.command = ATA_CMD_ID_ATAPI;
Albert Lee59a10b12005-10-12 15:09:42 +08002505 DPRINTK("do ATAPI identify\n");
2506 }
2507
Tejun Heoa0123702005-12-13 14:49:31 +09002508 tf.flags |= ATA_TFLAG_DEVICE;
2509 tf.protocol = ATA_PROT_PIO;
Albert Lee59a10b12005-10-12 15:09:42 +08002510
Tejun Heoa0123702005-12-13 14:49:31 +09002511 if (ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE,
2512 dev->id, sizeof(dev->id)))
Albert Lee59a10b12005-10-12 15:09:42 +08002513 goto err_out;
2514
Albert Lee59a10b12005-10-12 15:09:42 +08002515 swap_buf_le16(dev->id, ATA_ID_WORDS);
2516
Tejun Heo0bd33002006-02-12 22:47:05 +09002517 ata_dump_id(dev->id);
Albert Lee59a10b12005-10-12 15:09:42 +08002518
2519 DPRINTK("EXIT\n");
2520
2521 return;
2522err_out:
Tejun Heoa0123702005-12-13 14:49:31 +09002523 printk(KERN_ERR "ata%u: failed to reread ID, disabled\n", ap->id);
Albert Lee59a10b12005-10-12 15:09:42 +08002524 ata_port_disable(ap);
2525}
2526
2527/**
Albert Lee8bf62ec2005-05-12 15:29:42 -04002528 * ata_dev_init_params - Issue INIT DEV PARAMS command
2529 * @ap: Port associated with device @dev
2530 * @dev: Device to which command will be sent
2531 *
2532 * LOCKING:
2533 */
2534
2535static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev)
2536{
Tejun Heoa0123702005-12-13 14:49:31 +09002537 struct ata_taskfile tf;
Albert Lee8bf62ec2005-05-12 15:29:42 -04002538 u16 sectors = dev->id[6];
2539 u16 heads = dev->id[3];
2540
2541 /* Number of sectors per track 1-255. Number of heads 1-16 */
2542 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
2543 return;
2544
2545 /* set up init dev params taskfile */
2546 DPRINTK("init dev params \n");
2547
Tejun Heoa0123702005-12-13 14:49:31 +09002548 ata_tf_init(ap, &tf, dev->devno);
2549 tf.command = ATA_CMD_INIT_DEV_PARAMS;
2550 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2551 tf.protocol = ATA_PROT_NODATA;
2552 tf.nsect = sectors;
2553 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
Albert Lee8bf62ec2005-05-12 15:29:42 -04002554
Tejun Heoa0123702005-12-13 14:49:31 +09002555 if (ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0)) {
2556 printk(KERN_ERR "ata%u: failed to init parameters, disabled\n",
2557 ap->id);
Albert Lee8bf62ec2005-05-12 15:29:42 -04002558 ata_port_disable(ap);
Tejun Heoa0123702005-12-13 14:49:31 +09002559 }
Albert Lee8bf62ec2005-05-12 15:29:42 -04002560
2561 DPRINTK("EXIT\n");
2562}
2563
2564/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002565 * ata_sg_clean - Unmap DMA memory associated with command
2566 * @qc: Command containing DMA memory to be released
2567 *
2568 * Unmap all mapped DMA memory associated with this command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002569 *
2570 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002571 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 */
2573
2574static void ata_sg_clean(struct ata_queued_cmd *qc)
2575{
2576 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002577 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002579 void *pad_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580
Tejun Heoa4631472006-02-11 19:11:13 +09002581 WARN_ON(!(qc->flags & ATA_QCFLAG_DMAMAP));
2582 WARN_ON(sg == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583
2584 if (qc->flags & ATA_QCFLAG_SINGLE)
Tejun Heoa4631472006-02-11 19:11:13 +09002585 WARN_ON(qc->n_elem != 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05002587 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002589 /* if we padded the buffer out to 32-bit bound, and data
2590 * xfer direction is from-device, we must copy from the
2591 * pad buffer back into the supplied buffer
2592 */
2593 if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
2594 pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2595
2596 if (qc->flags & ATA_QCFLAG_SG) {
Jeff Garzike1410f22005-11-14 14:06:26 -05002597 if (qc->n_elem)
2598 dma_unmap_sg(ap->host_set->dev, sg, qc->n_elem, dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002599 /* restore last sg */
2600 sg[qc->orig_n_elem - 1].length += qc->pad_len;
2601 if (pad_buf) {
2602 struct scatterlist *psg = &qc->pad_sgent;
2603 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2604 memcpy(addr + psg->offset, pad_buf, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05002605 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002606 }
2607 } else {
Jeff Garzike1410f22005-11-14 14:06:26 -05002608 if (sg_dma_len(&sg[0]) > 0)
2609 dma_unmap_single(ap->host_set->dev,
2610 sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
2611 dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002612 /* restore sg */
2613 sg->length += qc->pad_len;
2614 if (pad_buf)
2615 memcpy(qc->buf_virt + sg->length - qc->pad_len,
2616 pad_buf, qc->pad_len);
2617 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618
2619 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002620 qc->__sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621}
2622
2623/**
2624 * ata_fill_sg - Fill PCI IDE PRD table
2625 * @qc: Metadata associated with taskfile to be transferred
2626 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002627 * Fill PCI IDE PRD (scatter-gather) table with segments
2628 * associated with the current disk command.
2629 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 * LOCKING:
Jeff Garzik780a87f2005-05-30 15:41:05 -04002631 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 *
2633 */
2634static void ata_fill_sg(struct ata_queued_cmd *qc)
2635{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002637 struct scatterlist *sg;
2638 unsigned int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639
Tejun Heoa4631472006-02-11 19:11:13 +09002640 WARN_ON(qc->__sg == NULL);
2641 WARN_ON(qc->n_elem == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642
2643 idx = 0;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002644 ata_for_each_sg(sg, qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645 u32 addr, offset;
2646 u32 sg_len, len;
2647
2648 /* determine if physical DMA addr spans 64K boundary.
2649 * Note h/w doesn't support 64-bit, so we unconditionally
2650 * truncate dma_addr_t to u32.
2651 */
2652 addr = (u32) sg_dma_address(sg);
2653 sg_len = sg_dma_len(sg);
2654
2655 while (sg_len) {
2656 offset = addr & 0xffff;
2657 len = sg_len;
2658 if ((offset + sg_len) > 0x10000)
2659 len = 0x10000 - offset;
2660
2661 ap->prd[idx].addr = cpu_to_le32(addr);
2662 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
2663 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
2664
2665 idx++;
2666 sg_len -= len;
2667 addr += len;
2668 }
2669 }
2670
2671 if (idx)
2672 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2673}
2674/**
2675 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2676 * @qc: Metadata associated with taskfile to check
2677 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002678 * Allow low-level driver to filter ATA PACKET commands, returning
2679 * a status indicating whether or not it is OK to use DMA for the
2680 * supplied PACKET command.
2681 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002683 * spin_lock_irqsave(host_set lock)
2684 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685 * RETURNS: 0 when ATAPI DMA can be used
2686 * nonzero otherwise
2687 */
2688int ata_check_atapi_dma(struct ata_queued_cmd *qc)
2689{
2690 struct ata_port *ap = qc->ap;
2691 int rc = 0; /* Assume ATAPI DMA is OK by default */
2692
2693 if (ap->ops->check_atapi_dma)
2694 rc = ap->ops->check_atapi_dma(qc);
2695
2696 return rc;
2697}
2698/**
2699 * ata_qc_prep - Prepare taskfile for submission
2700 * @qc: Metadata associated with taskfile to be prepared
2701 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002702 * Prepare ATA taskfile for submission.
2703 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 * LOCKING:
2705 * spin_lock_irqsave(host_set lock)
2706 */
2707void ata_qc_prep(struct ata_queued_cmd *qc)
2708{
2709 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2710 return;
2711
2712 ata_fill_sg(qc);
2713}
2714
Jeff Garzik0cba6322005-05-30 19:49:12 -04002715/**
2716 * ata_sg_init_one - Associate command with memory buffer
2717 * @qc: Command to be associated
2718 * @buf: Memory buffer
2719 * @buflen: Length of memory buffer, in bytes.
2720 *
2721 * Initialize the data-related elements of queued_cmd @qc
2722 * to point to a single memory buffer, @buf of byte length @buflen.
2723 *
2724 * LOCKING:
2725 * spin_lock_irqsave(host_set lock)
2726 */
2727
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
2729{
2730 struct scatterlist *sg;
2731
2732 qc->flags |= ATA_QCFLAG_SINGLE;
2733
2734 memset(&qc->sgent, 0, sizeof(qc->sgent));
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002735 qc->__sg = &qc->sgent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 qc->n_elem = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002737 qc->orig_n_elem = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738 qc->buf_virt = buf;
2739
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002740 sg = qc->__sg;
Jeff Garzikf0612bb2005-10-30 01:58:18 -05002741 sg_init_one(sg, buf, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742}
2743
Jeff Garzik0cba6322005-05-30 19:49:12 -04002744/**
2745 * ata_sg_init - Associate command with scatter-gather table.
2746 * @qc: Command to be associated
2747 * @sg: Scatter-gather table.
2748 * @n_elem: Number of elements in s/g table.
2749 *
2750 * Initialize the data-related elements of queued_cmd @qc
2751 * to point to a scatter-gather table @sg, containing @n_elem
2752 * elements.
2753 *
2754 * LOCKING:
2755 * spin_lock_irqsave(host_set lock)
2756 */
2757
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
2759 unsigned int n_elem)
2760{
2761 qc->flags |= ATA_QCFLAG_SG;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002762 qc->__sg = sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 qc->n_elem = n_elem;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002764 qc->orig_n_elem = n_elem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765}
2766
2767/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002768 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
2769 * @qc: Command with memory buffer to be mapped.
2770 *
2771 * DMA-map the memory buffer associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 *
2773 * LOCKING:
2774 * spin_lock_irqsave(host_set lock)
2775 *
2776 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002777 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778 */
2779
2780static int ata_sg_setup_one(struct ata_queued_cmd *qc)
2781{
2782 struct ata_port *ap = qc->ap;
2783 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002784 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785 dma_addr_t dma_address;
2786
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002787 /* we must lengthen transfers to end on a 32-bit boundary */
2788 qc->pad_len = sg->length & 3;
2789 if (qc->pad_len) {
2790 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2791 struct scatterlist *psg = &qc->pad_sgent;
2792
Tejun Heoa4631472006-02-11 19:11:13 +09002793 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002794
2795 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2796
2797 if (qc->tf.flags & ATA_TFLAG_WRITE)
2798 memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
2799 qc->pad_len);
2800
2801 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2802 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2803 /* trim sg */
2804 sg->length -= qc->pad_len;
2805
2806 DPRINTK("padding done, sg->length=%u pad_len=%u\n",
2807 sg->length, qc->pad_len);
2808 }
2809
Jeff Garzike1410f22005-11-14 14:06:26 -05002810 if (!sg->length) {
2811 sg_dma_address(sg) = 0;
2812 goto skip_map;
2813 }
2814
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815 dma_address = dma_map_single(ap->host_set->dev, qc->buf_virt,
Albert Lee32529e02005-05-26 03:49:42 -04002816 sg->length, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05002817 if (dma_mapping_error(dma_address)) {
2818 /* restore sg */
2819 sg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05002821 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822
2823 sg_dma_address(sg) = dma_address;
Jeff Garzike1410f22005-11-14 14:06:26 -05002824skip_map:
Albert Lee32529e02005-05-26 03:49:42 -04002825 sg_dma_len(sg) = sg->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826
2827 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
2828 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
2829
2830 return 0;
2831}
2832
2833/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002834 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
2835 * @qc: Command with scatter-gather table to be mapped.
2836 *
2837 * DMA-map the scatter-gather table associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 *
2839 * LOCKING:
2840 * spin_lock_irqsave(host_set lock)
2841 *
2842 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002843 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 *
2845 */
2846
2847static int ata_sg_setup(struct ata_queued_cmd *qc)
2848{
2849 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002850 struct scatterlist *sg = qc->__sg;
2851 struct scatterlist *lsg = &sg[qc->n_elem - 1];
Jeff Garzike1410f22005-11-14 14:06:26 -05002852 int n_elem, pre_n_elem, dir, trim_sg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853
2854 VPRINTK("ENTER, ata%u\n", ap->id);
Tejun Heoa4631472006-02-11 19:11:13 +09002855 WARN_ON(!(qc->flags & ATA_QCFLAG_SG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002857 /* we must lengthen transfers to end on a 32-bit boundary */
2858 qc->pad_len = lsg->length & 3;
2859 if (qc->pad_len) {
2860 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2861 struct scatterlist *psg = &qc->pad_sgent;
2862 unsigned int offset;
2863
Tejun Heoa4631472006-02-11 19:11:13 +09002864 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002865
2866 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
2867
2868 /*
2869 * psg->page/offset are used to copy to-be-written
2870 * data in this function or read data in ata_sg_clean.
2871 */
2872 offset = lsg->offset + lsg->length - qc->pad_len;
2873 psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
2874 psg->offset = offset_in_page(offset);
2875
2876 if (qc->tf.flags & ATA_TFLAG_WRITE) {
2877 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2878 memcpy(pad_buf, addr + psg->offset, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05002879 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002880 }
2881
2882 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
2883 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
2884 /* trim last sg */
2885 lsg->length -= qc->pad_len;
Jeff Garzike1410f22005-11-14 14:06:26 -05002886 if (lsg->length == 0)
2887 trim_sg = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002888
2889 DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
2890 qc->n_elem - 1, lsg->length, qc->pad_len);
2891 }
2892
Jeff Garzike1410f22005-11-14 14:06:26 -05002893 pre_n_elem = qc->n_elem;
2894 if (trim_sg && pre_n_elem)
2895 pre_n_elem--;
2896
2897 if (!pre_n_elem) {
2898 n_elem = 0;
2899 goto skip_map;
2900 }
2901
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 dir = qc->dma_dir;
Jeff Garzike1410f22005-11-14 14:06:26 -05002903 n_elem = dma_map_sg(ap->host_set->dev, sg, pre_n_elem, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05002904 if (n_elem < 1) {
2905 /* restore last sg */
2906 lsg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05002908 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909
2910 DPRINTK("%d sg elements mapped\n", n_elem);
2911
Jeff Garzike1410f22005-11-14 14:06:26 -05002912skip_map:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 qc->n_elem = n_elem;
2914
2915 return 0;
2916}
2917
2918/**
Tejun Heo40e8c822005-08-22 17:12:45 +09002919 * ata_poll_qc_complete - turn irq back on and finish qc
2920 * @qc: Command to complete
Randy Dunlap8e8b77d2005-11-01 21:29:27 -08002921 * @err_mask: ATA status register content
Tejun Heo40e8c822005-08-22 17:12:45 +09002922 *
2923 * LOCKING:
2924 * None. (grabs host lock)
2925 */
2926
Albert Leea22e2eb2005-12-05 15:38:02 +08002927void ata_poll_qc_complete(struct ata_queued_cmd *qc)
Tejun Heo40e8c822005-08-22 17:12:45 +09002928{
2929 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04002930 unsigned long flags;
Tejun Heo40e8c822005-08-22 17:12:45 +09002931
Jeff Garzikb8f61532005-08-25 22:01:20 -04002932 spin_lock_irqsave(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09002933 ap->flags &= ~ATA_FLAG_NOINTR;
2934 ata_irq_on(ap);
Albert Leea22e2eb2005-12-05 15:38:02 +08002935 ata_qc_complete(qc);
Jeff Garzikb8f61532005-08-25 22:01:20 -04002936 spin_unlock_irqrestore(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09002937}
2938
2939/**
Randy Dunlapc893a3a2006-01-28 13:15:32 -05002940 * ata_pio_poll - poll using PIO, depending on current state
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002941 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07002942 *
2943 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002944 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 *
2946 * RETURNS:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002947 * timeout value to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 */
2949
2950static unsigned long ata_pio_poll(struct ata_port *ap)
2951{
Albert Leec14b8332005-12-05 15:36:08 +08002952 struct ata_queued_cmd *qc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 u8 status;
Albert Lee14be71f2005-09-27 17:36:35 +08002954 unsigned int poll_state = HSM_ST_UNKNOWN;
2955 unsigned int reg_state = HSM_ST_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956
Albert Leec14b8332005-12-05 15:36:08 +08002957 qc = ata_qc_from_tag(ap, ap->active_tag);
Tejun Heoa4631472006-02-11 19:11:13 +09002958 WARN_ON(qc == NULL);
Albert Leec14b8332005-12-05 15:36:08 +08002959
Albert Lee14be71f2005-09-27 17:36:35 +08002960 switch (ap->hsm_task_state) {
2961 case HSM_ST:
2962 case HSM_ST_POLL:
2963 poll_state = HSM_ST_POLL;
2964 reg_state = HSM_ST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 break;
Albert Lee14be71f2005-09-27 17:36:35 +08002966 case HSM_ST_LAST:
2967 case HSM_ST_LAST_POLL:
2968 poll_state = HSM_ST_LAST_POLL;
2969 reg_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 break;
2971 default:
2972 BUG();
2973 break;
2974 }
2975
2976 status = ata_chk_status(ap);
2977 if (status & ATA_BUSY) {
2978 if (time_after(jiffies, ap->pio_task_timeout)) {
Tejun Heo11a56d22006-01-23 13:09:36 +09002979 qc->err_mask |= AC_ERR_TIMEOUT;
Albert Lee7c398332005-11-09 13:03:30 +08002980 ap->hsm_task_state = HSM_ST_TMOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 return 0;
2982 }
Albert Lee14be71f2005-09-27 17:36:35 +08002983 ap->hsm_task_state = poll_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 return ATA_SHORT_PAUSE;
2985 }
2986
Albert Lee14be71f2005-09-27 17:36:35 +08002987 ap->hsm_task_state = reg_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 return 0;
2989}
2990
2991/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04002992 * ata_pio_complete - check if drive is busy or idle
2993 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 *
2995 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002996 * None. (executing in kernel thread context)
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04002997 *
2998 * RETURNS:
2999 * Non-zero if qc completed, zero otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 */
3001
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003002static int ata_pio_complete (struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003{
3004 struct ata_queued_cmd *qc;
3005 u8 drv_stat;
3006
3007 /*
Alan Cox31433ea2005-08-26 15:56:47 +01003008 * This is purely heuristic. This is a fast path. Sometimes when
3009 * we enter, BSY will be cleared in a chk-status or two. If not,
3010 * the drive is probably seeking or something. Snooze for a couple
3011 * msecs, then chk-status again. If still busy, fall back to
Albert Lee14be71f2005-09-27 17:36:35 +08003012 * HSM_ST_POLL state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 */
Albert Leefe79e682005-12-06 11:34:59 +08003014 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
3015 if (drv_stat & ATA_BUSY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 msleep(2);
Albert Leefe79e682005-12-06 11:34:59 +08003017 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
3018 if (drv_stat & ATA_BUSY) {
Albert Lee14be71f2005-09-27 17:36:35 +08003019 ap->hsm_task_state = HSM_ST_LAST_POLL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003021 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 }
3023 }
3024
Albert Leec14b8332005-12-05 15:36:08 +08003025 qc = ata_qc_from_tag(ap, ap->active_tag);
Tejun Heoa4631472006-02-11 19:11:13 +09003026 WARN_ON(qc == NULL);
Albert Leec14b8332005-12-05 15:36:08 +08003027
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 drv_stat = ata_wait_idle(ap);
3029 if (!ata_ok(drv_stat)) {
Albert Lee1c848982005-12-05 15:40:15 +08003030 qc->err_mask |= __ac_err_mask(drv_stat);
Albert Lee14be71f2005-09-27 17:36:35 +08003031 ap->hsm_task_state = HSM_ST_ERR;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003032 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033 }
3034
Albert Lee14be71f2005-09-27 17:36:35 +08003035 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036
Tejun Heoa4631472006-02-11 19:11:13 +09003037 WARN_ON(qc->err_mask);
Albert Leea22e2eb2005-12-05 15:38:02 +08003038 ata_poll_qc_complete(qc);
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003039
3040 /* another command may start at this point */
3041
3042 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043}
3044
Edward Falk0baab862005-06-02 18:17:13 -04003045
3046/**
Randy Dunlapc893a3a2006-01-28 13:15:32 -05003047 * swap_buf_le16 - swap halves of 16-bit words in place
Edward Falk0baab862005-06-02 18:17:13 -04003048 * @buf: Buffer to swap
3049 * @buf_words: Number of 16-bit words in buffer.
3050 *
3051 * Swap halves of 16-bit words if needed to convert from
3052 * little-endian byte order to native cpu byte order, or
3053 * vice-versa.
3054 *
3055 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003056 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04003057 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058void swap_buf_le16(u16 *buf, unsigned int buf_words)
3059{
3060#ifdef __BIG_ENDIAN
3061 unsigned int i;
3062
3063 for (i = 0; i < buf_words; i++)
3064 buf[i] = le16_to_cpu(buf[i]);
3065#endif /* __BIG_ENDIAN */
3066}
3067
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003068/**
3069 * ata_mmio_data_xfer - Transfer data by MMIO
3070 * @ap: port to read/write
3071 * @buf: data buffer
3072 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04003073 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003074 *
3075 * Transfer data from/to the device data register by MMIO.
3076 *
3077 * LOCKING:
3078 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003079 */
3080
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
3082 unsigned int buflen, int write_data)
3083{
3084 unsigned int i;
3085 unsigned int words = buflen >> 1;
3086 u16 *buf16 = (u16 *) buf;
3087 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
3088
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003089 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 if (write_data) {
3091 for (i = 0; i < words; i++)
3092 writew(le16_to_cpu(buf16[i]), mmio);
3093 } else {
3094 for (i = 0; i < words; i++)
3095 buf16[i] = cpu_to_le16(readw(mmio));
3096 }
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003097
3098 /* Transfer trailing 1 byte, if any. */
3099 if (unlikely(buflen & 0x01)) {
3100 u16 align_buf[1] = { 0 };
3101 unsigned char *trailing_buf = buf + buflen - 1;
3102
3103 if (write_data) {
3104 memcpy(align_buf, trailing_buf, 1);
3105 writew(le16_to_cpu(align_buf[0]), mmio);
3106 } else {
3107 align_buf[0] = cpu_to_le16(readw(mmio));
3108 memcpy(trailing_buf, align_buf, 1);
3109 }
3110 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111}
3112
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003113/**
3114 * ata_pio_data_xfer - Transfer data by PIO
3115 * @ap: port to read/write
3116 * @buf: data buffer
3117 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04003118 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003119 *
3120 * Transfer data from/to the device data register by PIO.
3121 *
3122 * LOCKING:
3123 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003124 */
3125
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
3127 unsigned int buflen, int write_data)
3128{
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003129 unsigned int words = buflen >> 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003131 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132 if (write_data)
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003133 outsw(ap->ioaddr.data_addr, buf, words);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 else
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003135 insw(ap->ioaddr.data_addr, buf, words);
3136
3137 /* Transfer trailing 1 byte, if any. */
3138 if (unlikely(buflen & 0x01)) {
3139 u16 align_buf[1] = { 0 };
3140 unsigned char *trailing_buf = buf + buflen - 1;
3141
3142 if (write_data) {
3143 memcpy(align_buf, trailing_buf, 1);
3144 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
3145 } else {
3146 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
3147 memcpy(trailing_buf, align_buf, 1);
3148 }
3149 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150}
3151
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003152/**
3153 * ata_data_xfer - Transfer data from/to the data register.
3154 * @ap: port to read/write
3155 * @buf: data buffer
3156 * @buflen: buffer length
3157 * @do_write: read/write
3158 *
3159 * Transfer data from/to the device data register.
3160 *
3161 * LOCKING:
3162 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003163 */
3164
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
3166 unsigned int buflen, int do_write)
3167{
Alan Coxa1bd9e62006-01-17 20:53:50 +00003168 /* Make the crap hardware pay the costs not the good stuff */
3169 if (unlikely(ap->flags & ATA_FLAG_IRQ_MASK)) {
3170 unsigned long flags;
3171 local_irq_save(flags);
3172 if (ap->flags & ATA_FLAG_MMIO)
3173 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3174 else
3175 ata_pio_data_xfer(ap, buf, buflen, do_write);
3176 local_irq_restore(flags);
3177 } else {
3178 if (ap->flags & ATA_FLAG_MMIO)
3179 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3180 else
3181 ata_pio_data_xfer(ap, buf, buflen, do_write);
3182 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183}
3184
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003185/**
3186 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
3187 * @qc: Command on going
3188 *
3189 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
3190 *
3191 * LOCKING:
3192 * Inherited from caller.
3193 */
3194
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195static void ata_pio_sector(struct ata_queued_cmd *qc)
3196{
3197 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003198 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 struct ata_port *ap = qc->ap;
3200 struct page *page;
3201 unsigned int offset;
3202 unsigned char *buf;
3203
3204 if (qc->cursect == (qc->nsect - 1))
Albert Lee14be71f2005-09-27 17:36:35 +08003205 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206
3207 page = sg[qc->cursg].page;
3208 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
3209
3210 /* get the current page and offset */
3211 page = nth_page(page, (offset >> PAGE_SHIFT));
3212 offset %= PAGE_SIZE;
3213
3214 buf = kmap(page) + offset;
3215
3216 qc->cursect++;
3217 qc->cursg_ofs++;
3218
Albert Lee32529e02005-05-26 03:49:42 -04003219 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 qc->cursg++;
3221 qc->cursg_ofs = 0;
3222 }
3223
3224 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3225
3226 /* do the actual data transfer */
3227 do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3228 ata_data_xfer(ap, buf, ATA_SECT_SIZE, do_write);
3229
3230 kunmap(page);
3231}
3232
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003233/**
3234 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
3235 * @qc: Command on going
3236 * @bytes: number of bytes
3237 *
3238 * Transfer Transfer data from/to the ATAPI device.
3239 *
3240 * LOCKING:
3241 * Inherited from caller.
3242 *
3243 */
3244
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
3246{
3247 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003248 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249 struct ata_port *ap = qc->ap;
3250 struct page *page;
3251 unsigned char *buf;
3252 unsigned int offset, count;
3253
Albert Lee563a6e12005-08-12 14:17:50 +08003254 if (qc->curbytes + bytes >= qc->nbytes)
Albert Lee14be71f2005-09-27 17:36:35 +08003255 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256
3257next_sg:
Albert Lee563a6e12005-08-12 14:17:50 +08003258 if (unlikely(qc->cursg >= qc->n_elem)) {
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003259 /*
Albert Lee563a6e12005-08-12 14:17:50 +08003260 * The end of qc->sg is reached and the device expects
3261 * more data to transfer. In order not to overrun qc->sg
3262 * and fulfill length specified in the byte count register,
3263 * - for read case, discard trailing data from the device
3264 * - for write case, padding zero data to the device
3265 */
3266 u16 pad_buf[1] = { 0 };
3267 unsigned int words = bytes >> 1;
3268 unsigned int i;
3269
3270 if (words) /* warning if bytes > 1 */
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003271 printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
Albert Lee563a6e12005-08-12 14:17:50 +08003272 ap->id, bytes);
3273
3274 for (i = 0; i < words; i++)
3275 ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
3276
Albert Lee14be71f2005-09-27 17:36:35 +08003277 ap->hsm_task_state = HSM_ST_LAST;
Albert Lee563a6e12005-08-12 14:17:50 +08003278 return;
3279 }
3280
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003281 sg = &qc->__sg[qc->cursg];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003282
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 page = sg->page;
3284 offset = sg->offset + qc->cursg_ofs;
3285
3286 /* get the current page and offset */
3287 page = nth_page(page, (offset >> PAGE_SHIFT));
3288 offset %= PAGE_SIZE;
3289
Albert Lee6952df02005-06-06 15:56:03 +08003290 /* don't overrun current sg */
Albert Lee32529e02005-05-26 03:49:42 -04003291 count = min(sg->length - qc->cursg_ofs, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003292
3293 /* don't cross page boundaries */
3294 count = min(count, (unsigned int)PAGE_SIZE - offset);
3295
3296 buf = kmap(page) + offset;
3297
3298 bytes -= count;
3299 qc->curbytes += count;
3300 qc->cursg_ofs += count;
3301
Albert Lee32529e02005-05-26 03:49:42 -04003302 if (qc->cursg_ofs == sg->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 qc->cursg++;
3304 qc->cursg_ofs = 0;
3305 }
3306
3307 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3308
3309 /* do the actual data transfer */
3310 ata_data_xfer(ap, buf, count, do_write);
3311
3312 kunmap(page);
3313
Albert Lee563a6e12005-08-12 14:17:50 +08003314 if (bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 goto next_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316}
3317
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003318/**
3319 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
3320 * @qc: Command on going
3321 *
3322 * Transfer Transfer data from/to the ATAPI device.
3323 *
3324 * LOCKING:
3325 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003326 */
3327
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328static void atapi_pio_bytes(struct ata_queued_cmd *qc)
3329{
3330 struct ata_port *ap = qc->ap;
3331 struct ata_device *dev = qc->dev;
3332 unsigned int ireason, bc_lo, bc_hi, bytes;
3333 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
3334
3335 ap->ops->tf_read(ap, &qc->tf);
3336 ireason = qc->tf.nsect;
3337 bc_lo = qc->tf.lbam;
3338 bc_hi = qc->tf.lbah;
3339 bytes = (bc_hi << 8) | bc_lo;
3340
3341 /* shall be cleared to zero, indicating xfer of data */
3342 if (ireason & (1 << 0))
3343 goto err_out;
3344
3345 /* make sure transfer direction matches expected */
3346 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
3347 if (do_write != i_write)
3348 goto err_out;
3349
3350 __atapi_pio_bytes(qc, bytes);
3351
3352 return;
3353
3354err_out:
3355 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
3356 ap->id, dev->devno);
Tejun Heo11a56d22006-01-23 13:09:36 +09003357 qc->err_mask |= AC_ERR_HSM;
Albert Lee14be71f2005-09-27 17:36:35 +08003358 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359}
3360
3361/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003362 * ata_pio_block - start PIO on a block
3363 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364 *
3365 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003366 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 */
3368
3369static void ata_pio_block(struct ata_port *ap)
3370{
3371 struct ata_queued_cmd *qc;
3372 u8 status;
3373
3374 /*
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003375 * This is purely heuristic. This is a fast path.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003376 * Sometimes when we enter, BSY will be cleared in
3377 * a chk-status or two. If not, the drive is probably seeking
3378 * or something. Snooze for a couple msecs, then
3379 * chk-status again. If still busy, fall back to
Albert Lee14be71f2005-09-27 17:36:35 +08003380 * HSM_ST_POLL state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 */
3382 status = ata_busy_wait(ap, ATA_BUSY, 5);
3383 if (status & ATA_BUSY) {
3384 msleep(2);
3385 status = ata_busy_wait(ap, ATA_BUSY, 10);
3386 if (status & ATA_BUSY) {
Albert Lee14be71f2005-09-27 17:36:35 +08003387 ap->hsm_task_state = HSM_ST_POLL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
3389 return;
3390 }
3391 }
3392
3393 qc = ata_qc_from_tag(ap, ap->active_tag);
Tejun Heoa4631472006-02-11 19:11:13 +09003394 WARN_ON(qc == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395
Albert Leefe79e682005-12-06 11:34:59 +08003396 /* check error */
3397 if (status & (ATA_ERR | ATA_DF)) {
3398 qc->err_mask |= AC_ERR_DEV;
3399 ap->hsm_task_state = HSM_ST_ERR;
3400 return;
3401 }
3402
3403 /* transfer data if any */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003404 if (is_atapi_taskfile(&qc->tf)) {
Albert Leefe79e682005-12-06 11:34:59 +08003405 /* DRQ=0 means no more data to transfer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 if ((status & ATA_DRQ) == 0) {
Albert Lee14be71f2005-09-27 17:36:35 +08003407 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003408 return;
3409 }
3410
3411 atapi_pio_bytes(qc);
3412 } else {
3413 /* handle BSY=0, DRQ=0 as error */
3414 if ((status & ATA_DRQ) == 0) {
Tejun Heo11a56d22006-01-23 13:09:36 +09003415 qc->err_mask |= AC_ERR_HSM;
Albert Lee14be71f2005-09-27 17:36:35 +08003416 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003417 return;
3418 }
3419
3420 ata_pio_sector(qc);
3421 }
3422}
3423
3424static void ata_pio_error(struct ata_port *ap)
3425{
3426 struct ata_queued_cmd *qc;
Jeff Garzika7dac442005-10-30 04:44:42 -05003427
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428 qc = ata_qc_from_tag(ap, ap->active_tag);
Tejun Heoa4631472006-02-11 19:11:13 +09003429 WARN_ON(qc == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430
Albert Lee0565c262006-02-13 18:55:25 +08003431 if (qc->tf.command != ATA_CMD_PACKET)
3432 printk(KERN_WARNING "ata%u: PIO error\n", ap->id);
3433
Albert Lee1c848982005-12-05 15:40:15 +08003434 /* make sure qc->err_mask is available to
3435 * know what's wrong and recover
3436 */
Tejun Heoa4631472006-02-11 19:11:13 +09003437 WARN_ON(qc->err_mask == 0);
Albert Lee1c848982005-12-05 15:40:15 +08003438
Albert Lee14be71f2005-09-27 17:36:35 +08003439 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440
Albert Leea22e2eb2005-12-05 15:38:02 +08003441 ata_poll_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442}
3443
3444static void ata_pio_task(void *_data)
3445{
3446 struct ata_port *ap = _data;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003447 unsigned long timeout;
3448 int qc_completed;
3449
3450fsm_start:
3451 timeout = 0;
3452 qc_completed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453
Albert Lee14be71f2005-09-27 17:36:35 +08003454 switch (ap->hsm_task_state) {
3455 case HSM_ST_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003456 return;
3457
Albert Lee14be71f2005-09-27 17:36:35 +08003458 case HSM_ST:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003459 ata_pio_block(ap);
3460 break;
3461
Albert Lee14be71f2005-09-27 17:36:35 +08003462 case HSM_ST_LAST:
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003463 qc_completed = ata_pio_complete(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 break;
3465
Albert Lee14be71f2005-09-27 17:36:35 +08003466 case HSM_ST_POLL:
3467 case HSM_ST_LAST_POLL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468 timeout = ata_pio_poll(ap);
3469 break;
3470
Albert Lee14be71f2005-09-27 17:36:35 +08003471 case HSM_ST_TMOUT:
3472 case HSM_ST_ERR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473 ata_pio_error(ap);
3474 return;
3475 }
3476
3477 if (timeout)
Tejun Heo95064372006-01-23 13:09:37 +09003478 ata_queue_delayed_pio_task(ap, timeout);
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003479 else if (!qc_completed)
3480 goto fsm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481}
3482
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483/**
3484 * ata_qc_timeout - Handle timeout of queued command
3485 * @qc: Command that timed out
3486 *
3487 * Some part of the kernel (currently, only the SCSI layer)
3488 * has noticed that the active command on port @ap has not
3489 * completed after a specified length of time. Handle this
3490 * condition by disabling DMA (if necessary) and completing
3491 * transactions, with error if necessary.
3492 *
3493 * This also handles the case of the "lost interrupt", where
3494 * for some reason (possibly hardware bug, possibly driver bug)
3495 * an interrupt was not delivered to the driver, even though the
3496 * transaction completed successfully.
3497 *
3498 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003499 * Inherited from SCSI layer (none, can sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 */
3501
3502static void ata_qc_timeout(struct ata_queued_cmd *qc)
3503{
3504 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003505 struct ata_host_set *host_set = ap->host_set;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003506 u8 host_stat = 0, drv_stat;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003507 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003508
3509 DPRINTK("ENTER\n");
3510
Tejun Heoc18d06f2006-02-02 00:56:10 +09003511 ata_flush_pio_tasks(ap);
3512 ap->hsm_task_state = HSM_ST_IDLE;
3513
Jeff Garzikb8f61532005-08-25 22:01:20 -04003514 spin_lock_irqsave(&host_set->lock, flags);
3515
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 switch (qc->tf.protocol) {
3517
3518 case ATA_PROT_DMA:
3519 case ATA_PROT_ATAPI_DMA:
3520 host_stat = ap->ops->bmdma_status(ap);
3521
3522 /* before we do anything else, clear DMA-Start bit */
Alan Coxb73fc892005-08-26 16:03:19 +01003523 ap->ops->bmdma_stop(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003524
3525 /* fall through */
3526
3527 default:
3528 ata_altstatus(ap);
3529 drv_stat = ata_chk_status(ap);
3530
3531 /* ack bmdma irq events */
3532 ap->ops->irq_clear(ap);
3533
3534 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
3535 ap->id, qc->tf.command, drv_stat, host_stat);
3536
3537 /* complete taskfile transaction */
Albert Leea22e2eb2005-12-05 15:38:02 +08003538 qc->err_mask |= ac_err_mask(drv_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539 break;
3540 }
Jeff Garzikb8f61532005-08-25 22:01:20 -04003541
3542 spin_unlock_irqrestore(&host_set->lock, flags);
3543
Tejun Heoa72ec4c2006-01-23 13:09:37 +09003544 ata_eh_qc_complete(qc);
3545
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546 DPRINTK("EXIT\n");
3547}
3548
3549/**
3550 * ata_eng_timeout - Handle timeout of queued command
3551 * @ap: Port on which timed-out command is active
3552 *
3553 * Some part of the kernel (currently, only the SCSI layer)
3554 * has noticed that the active command on port @ap has not
3555 * completed after a specified length of time. Handle this
3556 * condition by disabling DMA (if necessary) and completing
3557 * transactions, with error if necessary.
3558 *
3559 * This also handles the case of the "lost interrupt", where
3560 * for some reason (possibly hardware bug, possibly driver bug)
3561 * an interrupt was not delivered to the driver, even though the
3562 * transaction completed successfully.
3563 *
3564 * LOCKING:
3565 * Inherited from SCSI layer (none, can sleep)
3566 */
3567
3568void ata_eng_timeout(struct ata_port *ap)
3569{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570 DPRINTK("ENTER\n");
3571
Tejun Heof6379022006-02-10 15:10:48 +09003572 ata_qc_timeout(ata_qc_from_tag(ap, ap->active_tag));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003573
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574 DPRINTK("EXIT\n");
3575}
3576
3577/**
3578 * ata_qc_new - Request an available ATA command, for queueing
3579 * @ap: Port associated with device @dev
3580 * @dev: Device from whom we request an available command structure
3581 *
3582 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003583 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 */
3585
3586static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
3587{
3588 struct ata_queued_cmd *qc = NULL;
3589 unsigned int i;
3590
3591 for (i = 0; i < ATA_MAX_QUEUE; i++)
3592 if (!test_and_set_bit(i, &ap->qactive)) {
3593 qc = ata_qc_from_tag(ap, i);
3594 break;
3595 }
3596
3597 if (qc)
3598 qc->tag = i;
3599
3600 return qc;
3601}
3602
3603/**
3604 * ata_qc_new_init - Request an available ATA command, and initialize it
3605 * @ap: Port associated with device @dev
3606 * @dev: Device from whom we request an available command structure
3607 *
3608 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003609 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003610 */
3611
3612struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
3613 struct ata_device *dev)
3614{
3615 struct ata_queued_cmd *qc;
3616
3617 qc = ata_qc_new(ap);
3618 if (qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 qc->scsicmd = NULL;
3620 qc->ap = ap;
3621 qc->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003622
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05003623 ata_qc_reinit(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 }
3625
3626 return qc;
3627}
3628
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629/**
3630 * ata_qc_free - free unused ata_queued_cmd
3631 * @qc: Command to complete
3632 *
3633 * Designed to free unused ata_queued_cmd object
3634 * in case something prevents using it.
3635 *
3636 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003637 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003638 */
3639void ata_qc_free(struct ata_queued_cmd *qc)
3640{
Tejun Heo4ba946e2006-01-23 13:09:36 +09003641 struct ata_port *ap = qc->ap;
3642 unsigned int tag;
3643
Tejun Heoa4631472006-02-11 19:11:13 +09003644 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645
Tejun Heo4ba946e2006-01-23 13:09:36 +09003646 qc->flags = 0;
3647 tag = qc->tag;
3648 if (likely(ata_tag_valid(tag))) {
3649 if (tag == ap->active_tag)
3650 ap->active_tag = ATA_TAG_POISON;
3651 qc->tag = ATA_TAG_POISON;
3652 clear_bit(tag, &ap->qactive);
3653 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654}
3655
Tejun Heo76014422006-02-11 15:13:49 +09003656void __ata_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003657{
Tejun Heoa4631472006-02-11 19:11:13 +09003658 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
3659 WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660
3661 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
3662 ata_sg_clean(qc);
3663
Albert Lee3f3791d2005-08-16 14:25:38 +08003664 /* atapi: mark qc as inactive to prevent the interrupt handler
3665 * from completing the command twice later, before the error handler
3666 * is called. (when rc != 0 and atapi request sense is needed)
3667 */
3668 qc->flags &= ~ATA_QCFLAG_ACTIVE;
3669
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670 /* call completion callback */
Tejun Heo77853bf2006-01-23 13:09:36 +09003671 qc->complete_fn(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003672}
3673
3674static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
3675{
3676 struct ata_port *ap = qc->ap;
3677
3678 switch (qc->tf.protocol) {
3679 case ATA_PROT_DMA:
3680 case ATA_PROT_ATAPI_DMA:
3681 return 1;
3682
3683 case ATA_PROT_ATAPI:
3684 case ATA_PROT_PIO:
3685 case ATA_PROT_PIO_MULT:
3686 if (ap->flags & ATA_FLAG_PIO_DMA)
3687 return 1;
3688
3689 /* fall through */
3690
3691 default:
3692 return 0;
3693 }
3694
3695 /* never reached */
3696}
3697
3698/**
3699 * ata_qc_issue - issue taskfile to device
3700 * @qc: command to issue to device
3701 *
3702 * Prepare an ATA command to submission to device.
3703 * This includes mapping the data into a DMA-able
3704 * area, filling in the S/G table, and finally
3705 * writing the taskfile to hardware, starting the command.
3706 *
3707 * LOCKING:
3708 * spin_lock_irqsave(host_set lock)
3709 *
3710 * RETURNS:
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003711 * Zero on success, AC_ERR_* mask on failure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003712 */
3713
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003714unsigned int ata_qc_issue(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715{
3716 struct ata_port *ap = qc->ap;
3717
3718 if (ata_should_dma_map(qc)) {
3719 if (qc->flags & ATA_QCFLAG_SG) {
3720 if (ata_sg_setup(qc))
Tejun Heo8e436af2006-01-23 13:09:36 +09003721 goto sg_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003722 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
3723 if (ata_sg_setup_one(qc))
Tejun Heo8e436af2006-01-23 13:09:36 +09003724 goto sg_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003725 }
3726 } else {
3727 qc->flags &= ~ATA_QCFLAG_DMAMAP;
3728 }
3729
3730 ap->ops->qc_prep(qc);
3731
3732 qc->ap->active_tag = qc->tag;
3733 qc->flags |= ATA_QCFLAG_ACTIVE;
3734
3735 return ap->ops->qc_issue(qc);
3736
Tejun Heo8e436af2006-01-23 13:09:36 +09003737sg_err:
3738 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003739 return AC_ERR_SYSTEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003740}
3741
Edward Falk0baab862005-06-02 18:17:13 -04003742
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743/**
3744 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
3745 * @qc: command to issue to device
3746 *
3747 * Using various libata functions and hooks, this function
3748 * starts an ATA command. ATA commands are grouped into
3749 * classes called "protocols", and issuing each type of protocol
3750 * is slightly different.
3751 *
Edward Falk0baab862005-06-02 18:17:13 -04003752 * May be used as the qc_issue() entry in ata_port_operations.
3753 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754 * LOCKING:
3755 * spin_lock_irqsave(host_set lock)
3756 *
3757 * RETURNS:
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003758 * Zero on success, AC_ERR_* mask on failure
Linus Torvalds1da177e2005-04-16 15:20:36 -07003759 */
3760
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003761unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762{
3763 struct ata_port *ap = qc->ap;
3764
3765 ata_dev_select(ap, qc->dev->devno, 1, 0);
3766
3767 switch (qc->tf.protocol) {
3768 case ATA_PROT_NODATA:
Jeff Garzike5338252005-10-30 21:37:17 -05003769 ata_tf_to_host(ap, &qc->tf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770 break;
3771
3772 case ATA_PROT_DMA:
3773 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3774 ap->ops->bmdma_setup(qc); /* set up bmdma */
3775 ap->ops->bmdma_start(qc); /* initiate bmdma */
3776 break;
3777
3778 case ATA_PROT_PIO: /* load tf registers, initiate polling pio */
3779 ata_qc_set_polling(qc);
Jeff Garzike5338252005-10-30 21:37:17 -05003780 ata_tf_to_host(ap, &qc->tf);
Albert Lee14be71f2005-09-27 17:36:35 +08003781 ap->hsm_task_state = HSM_ST;
Tejun Heo95064372006-01-23 13:09:37 +09003782 ata_queue_pio_task(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003783 break;
3784
3785 case ATA_PROT_ATAPI:
3786 ata_qc_set_polling(qc);
Jeff Garzike5338252005-10-30 21:37:17 -05003787 ata_tf_to_host(ap, &qc->tf);
Tejun Heo95064372006-01-23 13:09:37 +09003788 ata_queue_packet_task(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 break;
3790
3791 case ATA_PROT_ATAPI_NODATA:
Tejun Heoc1389502005-08-22 14:59:24 +09003792 ap->flags |= ATA_FLAG_NOINTR;
Jeff Garzike5338252005-10-30 21:37:17 -05003793 ata_tf_to_host(ap, &qc->tf);
Tejun Heo95064372006-01-23 13:09:37 +09003794 ata_queue_packet_task(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003795 break;
3796
3797 case ATA_PROT_ATAPI_DMA:
Tejun Heoc1389502005-08-22 14:59:24 +09003798 ap->flags |= ATA_FLAG_NOINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003799 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
3800 ap->ops->bmdma_setup(qc); /* set up bmdma */
Tejun Heo95064372006-01-23 13:09:37 +09003801 ata_queue_packet_task(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802 break;
3803
3804 default:
3805 WARN_ON(1);
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09003806 return AC_ERR_SYSTEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 }
3808
3809 return 0;
3810}
3811
3812/**
Edward Falk0baab862005-06-02 18:17:13 -04003813 * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814 * @qc: Info associated with this ATA transaction.
3815 *
3816 * LOCKING:
3817 * spin_lock_irqsave(host_set lock)
3818 */
3819
3820static void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc)
3821{
3822 struct ata_port *ap = qc->ap;
3823 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3824 u8 dmactl;
3825 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3826
3827 /* load PRD table addr. */
3828 mb(); /* make sure PRD table writes are visible to controller */
3829 writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS);
3830
3831 /* specify data direction, triple-check start bit is clear */
3832 dmactl = readb(mmio + ATA_DMA_CMD);
3833 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3834 if (!rw)
3835 dmactl |= ATA_DMA_WR;
3836 writeb(dmactl, mmio + ATA_DMA_CMD);
3837
3838 /* issue r/w command */
3839 ap->ops->exec_command(ap, &qc->tf);
3840}
3841
3842/**
Alan Coxb73fc892005-08-26 16:03:19 +01003843 * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction
Linus Torvalds1da177e2005-04-16 15:20:36 -07003844 * @qc: Info associated with this ATA transaction.
3845 *
3846 * LOCKING:
3847 * spin_lock_irqsave(host_set lock)
3848 */
3849
3850static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc)
3851{
3852 struct ata_port *ap = qc->ap;
3853 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
3854 u8 dmactl;
3855
3856 /* start host DMA transaction */
3857 dmactl = readb(mmio + ATA_DMA_CMD);
3858 writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD);
3859
3860 /* Strictly, one may wish to issue a readb() here, to
3861 * flush the mmio write. However, control also passes
3862 * to the hardware at this point, and it will interrupt
3863 * us when we are to resume control. So, in effect,
3864 * we don't care when the mmio write flushes.
3865 * Further, a read of the DMA status register _immediately_
3866 * following the write may not be what certain flaky hardware
3867 * is expected, so I think it is best to not add a readb()
3868 * without first all the MMIO ATA cards/mobos.
3869 * Or maybe I'm just being paranoid.
3870 */
3871}
3872
3873/**
3874 * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO)
3875 * @qc: Info associated with this ATA transaction.
3876 *
3877 * LOCKING:
3878 * spin_lock_irqsave(host_set lock)
3879 */
3880
3881static void ata_bmdma_setup_pio (struct ata_queued_cmd *qc)
3882{
3883 struct ata_port *ap = qc->ap;
3884 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
3885 u8 dmactl;
3886
3887 /* load PRD table addr. */
3888 outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS);
3889
3890 /* specify data direction, triple-check start bit is clear */
3891 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3892 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START);
3893 if (!rw)
3894 dmactl |= ATA_DMA_WR;
3895 outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3896
3897 /* issue r/w command */
3898 ap->ops->exec_command(ap, &qc->tf);
3899}
3900
3901/**
3902 * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO)
3903 * @qc: Info associated with this ATA transaction.
3904 *
3905 * LOCKING:
3906 * spin_lock_irqsave(host_set lock)
3907 */
3908
3909static void ata_bmdma_start_pio (struct ata_queued_cmd *qc)
3910{
3911 struct ata_port *ap = qc->ap;
3912 u8 dmactl;
3913
3914 /* start host DMA transaction */
3915 dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3916 outb(dmactl | ATA_DMA_START,
3917 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
3918}
3919
Edward Falk0baab862005-06-02 18:17:13 -04003920
3921/**
3922 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
3923 * @qc: Info associated with this ATA transaction.
3924 *
3925 * Writes the ATA_DMA_START flag to the DMA command register.
3926 *
3927 * May be used as the bmdma_start() entry in ata_port_operations.
3928 *
3929 * LOCKING:
3930 * spin_lock_irqsave(host_set lock)
3931 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932void ata_bmdma_start(struct ata_queued_cmd *qc)
3933{
3934 if (qc->ap->flags & ATA_FLAG_MMIO)
3935 ata_bmdma_start_mmio(qc);
3936 else
3937 ata_bmdma_start_pio(qc);
3938}
3939
Edward Falk0baab862005-06-02 18:17:13 -04003940
3941/**
3942 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
3943 * @qc: Info associated with this ATA transaction.
3944 *
3945 * Writes address of PRD table to device's PRD Table Address
3946 * register, sets the DMA control register, and calls
3947 * ops->exec_command() to start the transfer.
3948 *
3949 * May be used as the bmdma_setup() entry in ata_port_operations.
3950 *
3951 * LOCKING:
3952 * spin_lock_irqsave(host_set lock)
3953 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003954void ata_bmdma_setup(struct ata_queued_cmd *qc)
3955{
3956 if (qc->ap->flags & ATA_FLAG_MMIO)
3957 ata_bmdma_setup_mmio(qc);
3958 else
3959 ata_bmdma_setup_pio(qc);
3960}
3961
Edward Falk0baab862005-06-02 18:17:13 -04003962
3963/**
3964 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
Jeff Garzikdecc6d02005-06-02 18:42:33 -04003965 * @ap: Port associated with this ATA transaction.
Edward Falk0baab862005-06-02 18:17:13 -04003966 *
3967 * Clear interrupt and error flags in DMA status register.
3968 *
3969 * May be used as the irq_clear() entry in ata_port_operations.
3970 *
3971 * LOCKING:
3972 * spin_lock_irqsave(host_set lock)
3973 */
3974
Linus Torvalds1da177e2005-04-16 15:20:36 -07003975void ata_bmdma_irq_clear(struct ata_port *ap)
3976{
3977 if (ap->flags & ATA_FLAG_MMIO) {
3978 void __iomem *mmio = ((void __iomem *) ap->ioaddr.bmdma_addr) + ATA_DMA_STATUS;
3979 writeb(readb(mmio), mmio);
3980 } else {
3981 unsigned long addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS;
3982 outb(inb(addr), addr);
3983 }
3984
3985}
3986
Edward Falk0baab862005-06-02 18:17:13 -04003987
3988/**
3989 * ata_bmdma_status - Read PCI IDE BMDMA status
Jeff Garzikdecc6d02005-06-02 18:42:33 -04003990 * @ap: Port associated with this ATA transaction.
Edward Falk0baab862005-06-02 18:17:13 -04003991 *
3992 * Read and return BMDMA status register.
3993 *
3994 * May be used as the bmdma_status() entry in ata_port_operations.
3995 *
3996 * LOCKING:
3997 * spin_lock_irqsave(host_set lock)
3998 */
3999
Linus Torvalds1da177e2005-04-16 15:20:36 -07004000u8 ata_bmdma_status(struct ata_port *ap)
4001{
4002 u8 host_stat;
4003 if (ap->flags & ATA_FLAG_MMIO) {
4004 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4005 host_stat = readb(mmio + ATA_DMA_STATUS);
4006 } else
Albert Leeee500aa2005-09-27 17:34:38 +08004007 host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004008 return host_stat;
4009}
4010
Edward Falk0baab862005-06-02 18:17:13 -04004011
4012/**
4013 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
Alan Coxb73fc892005-08-26 16:03:19 +01004014 * @qc: Command we are ending DMA for
Edward Falk0baab862005-06-02 18:17:13 -04004015 *
4016 * Clears the ATA_DMA_START flag in the dma control register
4017 *
4018 * May be used as the bmdma_stop() entry in ata_port_operations.
4019 *
4020 * LOCKING:
4021 * spin_lock_irqsave(host_set lock)
4022 */
4023
Alan Coxb73fc892005-08-26 16:03:19 +01004024void ata_bmdma_stop(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025{
Alan Coxb73fc892005-08-26 16:03:19 +01004026 struct ata_port *ap = qc->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027 if (ap->flags & ATA_FLAG_MMIO) {
4028 void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr;
4029
4030 /* clear start/stop bit */
4031 writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START,
4032 mmio + ATA_DMA_CMD);
4033 } else {
4034 /* clear start/stop bit */
4035 outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START,
4036 ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
4037 }
4038
4039 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
4040 ata_altstatus(ap); /* dummy read */
4041}
4042
4043/**
4044 * ata_host_intr - Handle host interrupt for given (port, task)
4045 * @ap: Port on which interrupt arrived (possibly...)
4046 * @qc: Taskfile currently active in engine
4047 *
4048 * Handle host interrupt for given queued command. Currently,
4049 * only DMA interrupts are handled. All other commands are
4050 * handled via polling with interrupts disabled (nIEN bit).
4051 *
4052 * LOCKING:
4053 * spin_lock_irqsave(host_set lock)
4054 *
4055 * RETURNS:
4056 * One if interrupt was handled, zero if not (shared irq).
4057 */
4058
4059inline unsigned int ata_host_intr (struct ata_port *ap,
4060 struct ata_queued_cmd *qc)
4061{
4062 u8 status, host_stat;
4063
4064 switch (qc->tf.protocol) {
4065
4066 case ATA_PROT_DMA:
4067 case ATA_PROT_ATAPI_DMA:
4068 case ATA_PROT_ATAPI:
4069 /* check status of DMA engine */
4070 host_stat = ap->ops->bmdma_status(ap);
4071 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
4072
4073 /* if it's not our irq... */
4074 if (!(host_stat & ATA_DMA_INTR))
4075 goto idle_irq;
4076
4077 /* before we do anything else, clear DMA-Start bit */
Alan Coxb73fc892005-08-26 16:03:19 +01004078 ap->ops->bmdma_stop(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079
4080 /* fall through */
4081
4082 case ATA_PROT_ATAPI_NODATA:
4083 case ATA_PROT_NODATA:
4084 /* check altstatus */
4085 status = ata_altstatus(ap);
4086 if (status & ATA_BUSY)
4087 goto idle_irq;
4088
4089 /* check main status, clearing INTRQ */
4090 status = ata_chk_status(ap);
4091 if (unlikely(status & ATA_BUSY))
4092 goto idle_irq;
4093 DPRINTK("ata%u: protocol %d (dev_stat 0x%X)\n",
4094 ap->id, qc->tf.protocol, status);
4095
4096 /* ack bmdma irq events */
4097 ap->ops->irq_clear(ap);
4098
4099 /* complete taskfile transaction */
Albert Leea22e2eb2005-12-05 15:38:02 +08004100 qc->err_mask |= ac_err_mask(status);
4101 ata_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004102 break;
4103
4104 default:
4105 goto idle_irq;
4106 }
4107
4108 return 1; /* irq handled */
4109
4110idle_irq:
4111 ap->stats.idle_irq++;
4112
4113#ifdef ATA_IRQ_TRAP
4114 if ((ap->stats.idle_irq % 1000) == 0) {
4115 handled = 1;
4116 ata_irq_ack(ap, 0); /* debug trap */
4117 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
4118 }
4119#endif
4120 return 0; /* irq not handled */
4121}
4122
4123/**
4124 * ata_interrupt - Default ATA host interrupt handler
Jeff Garzik0cba6322005-05-30 19:49:12 -04004125 * @irq: irq line (unused)
4126 * @dev_instance: pointer to our ata_host_set information structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127 * @regs: unused
4128 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004129 * Default interrupt handler for PCI IDE devices. Calls
4130 * ata_host_intr() for each port that is not disabled.
4131 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004133 * Obtains host_set lock during operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134 *
4135 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004136 * IRQ_NONE or IRQ_HANDLED.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004137 */
4138
4139irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
4140{
4141 struct ata_host_set *host_set = dev_instance;
4142 unsigned int i;
4143 unsigned int handled = 0;
4144 unsigned long flags;
4145
4146 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
4147 spin_lock_irqsave(&host_set->lock, flags);
4148
4149 for (i = 0; i < host_set->n_ports; i++) {
4150 struct ata_port *ap;
4151
4152 ap = host_set->ports[i];
Tejun Heoc1389502005-08-22 14:59:24 +09004153 if (ap &&
4154 !(ap->flags & (ATA_FLAG_PORT_DISABLED | ATA_FLAG_NOINTR))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 struct ata_queued_cmd *qc;
4156
4157 qc = ata_qc_from_tag(ap, ap->active_tag);
Albert Lee21b1ed72005-04-29 17:34:59 +08004158 if (qc && (!(qc->tf.ctl & ATA_NIEN)) &&
4159 (qc->flags & ATA_QCFLAG_ACTIVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 handled |= ata_host_intr(ap, qc);
4161 }
4162 }
4163
4164 spin_unlock_irqrestore(&host_set->lock, flags);
4165
4166 return IRQ_RETVAL(handled);
4167}
4168
4169/**
4170 * atapi_packet_task - Write CDB bytes to hardware
4171 * @_data: Port to which ATAPI device is attached.
4172 *
4173 * When device has indicated its readiness to accept
4174 * a CDB, this function is called. Send the CDB.
4175 * If DMA is to be performed, exit immediately.
4176 * Otherwise, we are in polling mode, so poll
4177 * status under operation succeeds or fails.
4178 *
4179 * LOCKING:
4180 * Kernel thread context (may sleep)
4181 */
4182
4183static void atapi_packet_task(void *_data)
4184{
4185 struct ata_port *ap = _data;
4186 struct ata_queued_cmd *qc;
4187 u8 status;
4188
4189 qc = ata_qc_from_tag(ap, ap->active_tag);
Tejun Heoa4631472006-02-11 19:11:13 +09004190 WARN_ON(qc == NULL);
4191 WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192
4193 /* sleep-wait for BSY to clear */
4194 DPRINTK("busy wait\n");
Albert Leed8fe4522005-12-05 15:42:17 +08004195 if (ata_busy_sleep(ap, ATA_TMOUT_CDB_QUICK, ATA_TMOUT_CDB)) {
Tejun Heo11a56d22006-01-23 13:09:36 +09004196 qc->err_mask |= AC_ERR_TIMEOUT;
Albert Leed8fe4522005-12-05 15:42:17 +08004197 goto err_out;
4198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199
4200 /* make sure DRQ is set */
4201 status = ata_chk_status(ap);
Albert Leed8fe4522005-12-05 15:42:17 +08004202 if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ) {
Tejun Heo11a56d22006-01-23 13:09:36 +09004203 qc->err_mask |= AC_ERR_HSM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204 goto err_out;
Albert Leed8fe4522005-12-05 15:42:17 +08004205 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206
4207 /* send SCSI cdb */
4208 DPRINTK("send cdb\n");
Tejun Heo6e7846e2006-02-12 23:32:58 +09004209 WARN_ON(qc->dev->cdb_len < 12);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210
Tejun Heoc1389502005-08-22 14:59:24 +09004211 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA ||
4212 qc->tf.protocol == ATA_PROT_ATAPI_NODATA) {
4213 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214
Tejun Heoc1389502005-08-22 14:59:24 +09004215 /* Once we're done issuing command and kicking bmdma,
4216 * irq handler takes over. To not lose irq, we need
4217 * to clear NOINTR flag before sending cdb, but
4218 * interrupt handler shouldn't be invoked before we're
4219 * finished. Hence, the following locking.
4220 */
4221 spin_lock_irqsave(&ap->host_set->lock, flags);
4222 ap->flags &= ~ATA_FLAG_NOINTR;
Tejun Heo6e7846e2006-02-12 23:32:58 +09004223 ata_data_xfer(ap, qc->cdb, qc->dev->cdb_len, 1);
Tejun Heoc1389502005-08-22 14:59:24 +09004224 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA)
4225 ap->ops->bmdma_start(qc); /* initiate bmdma */
4226 spin_unlock_irqrestore(&ap->host_set->lock, flags);
4227 } else {
Tejun Heo6e7846e2006-02-12 23:32:58 +09004228 ata_data_xfer(ap, qc->cdb, qc->dev->cdb_len, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229
Tejun Heoc1389502005-08-22 14:59:24 +09004230 /* PIO commands are handled by polling */
Albert Lee14be71f2005-09-27 17:36:35 +08004231 ap->hsm_task_state = HSM_ST;
Tejun Heo95064372006-01-23 13:09:37 +09004232 ata_queue_pio_task(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233 }
4234
4235 return;
4236
4237err_out:
Albert Leea22e2eb2005-12-05 15:38:02 +08004238 ata_poll_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239}
4240
Edward Falk0baab862005-06-02 18:17:13 -04004241
Jens Axboe9b847542006-01-06 09:28:07 +01004242/*
4243 * Execute a 'simple' command, that only consists of the opcode 'cmd' itself,
4244 * without filling any other registers
4245 */
4246static int ata_do_simple_cmd(struct ata_port *ap, struct ata_device *dev,
4247 u8 cmd)
4248{
4249 struct ata_taskfile tf;
4250 int err;
4251
4252 ata_tf_init(ap, &tf, dev->devno);
4253
4254 tf.command = cmd;
4255 tf.flags |= ATA_TFLAG_DEVICE;
4256 tf.protocol = ATA_PROT_NODATA;
4257
4258 err = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
4259 if (err)
4260 printk(KERN_ERR "%s: ata command failed: %d\n",
4261 __FUNCTION__, err);
4262
4263 return err;
4264}
4265
4266static int ata_flush_cache(struct ata_port *ap, struct ata_device *dev)
4267{
4268 u8 cmd;
4269
4270 if (!ata_try_flush_cache(dev))
4271 return 0;
4272
4273 if (ata_id_has_flush_ext(dev->id))
4274 cmd = ATA_CMD_FLUSH_EXT;
4275 else
4276 cmd = ATA_CMD_FLUSH;
4277
4278 return ata_do_simple_cmd(ap, dev, cmd);
4279}
4280
4281static int ata_standby_drive(struct ata_port *ap, struct ata_device *dev)
4282{
4283 return ata_do_simple_cmd(ap, dev, ATA_CMD_STANDBYNOW1);
4284}
4285
4286static int ata_start_drive(struct ata_port *ap, struct ata_device *dev)
4287{
4288 return ata_do_simple_cmd(ap, dev, ATA_CMD_IDLEIMMEDIATE);
4289}
4290
4291/**
4292 * ata_device_resume - wakeup a previously suspended devices
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004293 * @ap: port the device is connected to
4294 * @dev: the device to resume
Jens Axboe9b847542006-01-06 09:28:07 +01004295 *
4296 * Kick the drive back into action, by sending it an idle immediate
4297 * command and making sure its transfer mode matches between drive
4298 * and host.
4299 *
4300 */
4301int ata_device_resume(struct ata_port *ap, struct ata_device *dev)
4302{
4303 if (ap->flags & ATA_FLAG_SUSPENDED) {
4304 ap->flags &= ~ATA_FLAG_SUSPENDED;
4305 ata_set_mode(ap);
4306 }
4307 if (!ata_dev_present(dev))
4308 return 0;
4309 if (dev->class == ATA_DEV_ATA)
4310 ata_start_drive(ap, dev);
4311
4312 return 0;
4313}
4314
4315/**
4316 * ata_device_suspend - prepare a device for suspend
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004317 * @ap: port the device is connected to
4318 * @dev: the device to suspend
Jens Axboe9b847542006-01-06 09:28:07 +01004319 *
4320 * Flush the cache on the drive, if appropriate, then issue a
4321 * standbynow command.
Jens Axboe9b847542006-01-06 09:28:07 +01004322 */
4323int ata_device_suspend(struct ata_port *ap, struct ata_device *dev)
4324{
4325 if (!ata_dev_present(dev))
4326 return 0;
4327 if (dev->class == ATA_DEV_ATA)
4328 ata_flush_cache(ap, dev);
4329
4330 ata_standby_drive(ap, dev);
4331 ap->flags |= ATA_FLAG_SUSPENDED;
4332 return 0;
4333}
4334
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004335/**
4336 * ata_port_start - Set port up for dma.
4337 * @ap: Port to initialize
4338 *
4339 * Called just after data structures for each port are
4340 * initialized. Allocates space for PRD table.
4341 *
4342 * May be used as the port_start() entry in ata_port_operations.
4343 *
4344 * LOCKING:
4345 * Inherited from caller.
4346 */
4347
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348int ata_port_start (struct ata_port *ap)
4349{
4350 struct device *dev = ap->host_set->dev;
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004351 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352
4353 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
4354 if (!ap->prd)
4355 return -ENOMEM;
4356
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004357 rc = ata_pad_alloc(ap, dev);
4358 if (rc) {
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004359 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004360 return rc;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004361 }
4362
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
4364
4365 return 0;
4366}
4367
Edward Falk0baab862005-06-02 18:17:13 -04004368
4369/**
4370 * ata_port_stop - Undo ata_port_start()
4371 * @ap: Port to shut down
4372 *
4373 * Frees the PRD table.
4374 *
4375 * May be used as the port_stop() entry in ata_port_operations.
4376 *
4377 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004378 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04004379 */
4380
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381void ata_port_stop (struct ata_port *ap)
4382{
4383 struct device *dev = ap->host_set->dev;
4384
4385 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004386 ata_pad_free(ap, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387}
4388
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04004389void ata_host_stop (struct ata_host_set *host_set)
4390{
4391 if (host_set->mmio_base)
4392 iounmap(host_set->mmio_base);
4393}
4394
4395
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396/**
4397 * ata_host_remove - Unregister SCSI host structure with upper layers
4398 * @ap: Port to unregister
4399 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
4400 *
4401 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004402 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403 */
4404
4405static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
4406{
4407 struct Scsi_Host *sh = ap->host;
4408
4409 DPRINTK("ENTER\n");
4410
4411 if (do_unregister)
4412 scsi_remove_host(sh);
4413
4414 ap->ops->port_stop(ap);
4415}
4416
4417/**
4418 * ata_host_init - Initialize an ata_port structure
4419 * @ap: Structure to initialize
4420 * @host: associated SCSI mid-layer structure
4421 * @host_set: Collection of hosts to which @ap belongs
4422 * @ent: Probe information provided by low-level driver
4423 * @port_no: Port number associated with this ata_port
4424 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004425 * Initialize a new ata_port structure, and its associated
4426 * scsi_host.
4427 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004429 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430 */
4431
4432static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
4433 struct ata_host_set *host_set,
Jeff Garzik057ace52005-10-22 14:27:05 -04004434 const struct ata_probe_ent *ent, unsigned int port_no)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435{
4436 unsigned int i;
4437
4438 host->max_id = 16;
4439 host->max_lun = 1;
4440 host->max_channel = 1;
4441 host->unique_id = ata_unique_id++;
4442 host->max_cmd_len = 12;
Christoph Hellwig12413192005-06-11 01:05:01 +02004443
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444 ap->flags = ATA_FLAG_PORT_DISABLED;
4445 ap->id = host->unique_id;
4446 ap->host = host;
4447 ap->ctl = ATA_DEVCTL_OBS;
4448 ap->host_set = host_set;
4449 ap->port_no = port_no;
4450 ap->hard_port_no =
4451 ent->legacy_mode ? ent->hard_port_no : port_no;
4452 ap->pio_mask = ent->pio_mask;
4453 ap->mwdma_mask = ent->mwdma_mask;
4454 ap->udma_mask = ent->udma_mask;
4455 ap->flags |= ent->host_flags;
4456 ap->ops = ent->port_ops;
4457 ap->cbl = ATA_CBL_NONE;
4458 ap->active_tag = ATA_TAG_POISON;
4459 ap->last_ctl = 0xFF;
4460
4461 INIT_WORK(&ap->packet_task, atapi_packet_task, ap);
4462 INIT_WORK(&ap->pio_task, ata_pio_task, ap);
Tejun Heoa72ec4c2006-01-23 13:09:37 +09004463 INIT_LIST_HEAD(&ap->eh_done_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464
4465 for (i = 0; i < ATA_MAX_DEVICES; i++)
4466 ap->device[i].devno = i;
4467
4468#ifdef ATA_IRQ_TRAP
4469 ap->stats.unhandled_irq = 1;
4470 ap->stats.idle_irq = 1;
4471#endif
4472
4473 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
4474}
4475
4476/**
4477 * ata_host_add - Attach low-level ATA driver to system
4478 * @ent: Information provided by low-level driver
4479 * @host_set: Collections of ports to which we add
4480 * @port_no: Port number associated with this host
4481 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004482 * Attach low-level ATA driver to system.
4483 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004485 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486 *
4487 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004488 * New ata_port on success, for NULL on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489 */
4490
Jeff Garzik057ace52005-10-22 14:27:05 -04004491static struct ata_port * ata_host_add(const struct ata_probe_ent *ent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492 struct ata_host_set *host_set,
4493 unsigned int port_no)
4494{
4495 struct Scsi_Host *host;
4496 struct ata_port *ap;
4497 int rc;
4498
4499 DPRINTK("ENTER\n");
4500 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
4501 if (!host)
4502 return NULL;
4503
4504 ap = (struct ata_port *) &host->hostdata[0];
4505
4506 ata_host_init(ap, host, host_set, ent, port_no);
4507
4508 rc = ap->ops->port_start(ap);
4509 if (rc)
4510 goto err_out;
4511
4512 return ap;
4513
4514err_out:
4515 scsi_host_put(host);
4516 return NULL;
4517}
4518
4519/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004520 * ata_device_add - Register hardware device with ATA and SCSI layers
4521 * @ent: Probe information describing hardware device to be registered
4522 *
4523 * This function processes the information provided in the probe
4524 * information struct @ent, allocates the necessary ATA and SCSI
4525 * host information structures, initializes them, and registers
4526 * everything with requisite kernel subsystems.
4527 *
4528 * This function requests irqs, probes the ATA bus, and probes
4529 * the SCSI bus.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530 *
4531 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004532 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533 *
4534 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004535 * Number of ports registered. Zero on error (no ports registered).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536 */
4537
Jeff Garzik057ace52005-10-22 14:27:05 -04004538int ata_device_add(const struct ata_probe_ent *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539{
4540 unsigned int count = 0, i;
4541 struct device *dev = ent->dev;
4542 struct ata_host_set *host_set;
4543
4544 DPRINTK("ENTER\n");
4545 /* alloc a container for our list of ATA ports (buses) */
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004546 host_set = kzalloc(sizeof(struct ata_host_set) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
4548 if (!host_set)
4549 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004550 spin_lock_init(&host_set->lock);
4551
4552 host_set->dev = dev;
4553 host_set->n_ports = ent->n_ports;
4554 host_set->irq = ent->irq;
4555 host_set->mmio_base = ent->mmio_base;
4556 host_set->private_data = ent->private_data;
4557 host_set->ops = ent->port_ops;
4558
4559 /* register each port bound to this device */
4560 for (i = 0; i < ent->n_ports; i++) {
4561 struct ata_port *ap;
4562 unsigned long xfer_mode_mask;
4563
4564 ap = ata_host_add(ent, host_set, i);
4565 if (!ap)
4566 goto err_out;
4567
4568 host_set->ports[i] = ap;
4569 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
4570 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
4571 (ap->pio_mask << ATA_SHIFT_PIO);
4572
4573 /* print per-port info to dmesg */
4574 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
4575 "bmdma 0x%lX irq %lu\n",
4576 ap->id,
4577 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
4578 ata_mode_string(xfer_mode_mask),
4579 ap->ioaddr.cmd_addr,
4580 ap->ioaddr.ctl_addr,
4581 ap->ioaddr.bmdma_addr,
4582 ent->irq);
4583
4584 ata_chk_status(ap);
4585 host_set->ops->irq_clear(ap);
4586 count++;
4587 }
4588
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004589 if (!count)
4590 goto err_free_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591
4592 /* obtain irq, that is shared between channels */
4593 if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
4594 DRV_NAME, host_set))
4595 goto err_out;
4596
4597 /* perform each probe synchronously */
4598 DPRINTK("probe begin\n");
4599 for (i = 0; i < count; i++) {
4600 struct ata_port *ap;
4601 int rc;
4602
4603 ap = host_set->ports[i];
4604
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004605 DPRINTK("ata%u: bus probe begin\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606 rc = ata_bus_probe(ap);
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004607 DPRINTK("ata%u: bus probe end\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608
4609 if (rc) {
4610 /* FIXME: do something useful here?
4611 * Current libata behavior will
4612 * tear down everything when
4613 * the module is removed
4614 * or the h/w is unplugged.
4615 */
4616 }
4617
4618 rc = scsi_add_host(ap->host, dev);
4619 if (rc) {
4620 printk(KERN_ERR "ata%u: scsi_add_host failed\n",
4621 ap->id);
4622 /* FIXME: do something useful here */
4623 /* FIXME: handle unconditional calls to
4624 * scsi_scan_host and ata_host_remove, below,
4625 * at the very least
4626 */
4627 }
4628 }
4629
4630 /* probes are done, now scan each port's disk(s) */
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004631 DPRINTK("host probe begin\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004632 for (i = 0; i < count; i++) {
4633 struct ata_port *ap = host_set->ports[i];
4634
Jeff Garzik644dd0c2005-10-03 15:55:19 -04004635 ata_scsi_scan_host(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636 }
4637
4638 dev_set_drvdata(dev, host_set);
4639
4640 VPRINTK("EXIT, returning %u\n", ent->n_ports);
4641 return ent->n_ports; /* success */
4642
4643err_out:
4644 for (i = 0; i < count; i++) {
4645 ata_host_remove(host_set->ports[i], 1);
4646 scsi_host_put(host_set->ports[i]->host);
4647 }
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004648err_free_ret:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649 kfree(host_set);
4650 VPRINTK("EXIT, returning 0\n");
4651 return 0;
4652}
4653
4654/**
Alan Cox17b14452005-09-15 15:44:00 +01004655 * ata_host_set_remove - PCI layer callback for device removal
4656 * @host_set: ATA host set that was removed
4657 *
4658 * Unregister all objects associated with this host set. Free those
4659 * objects.
4660 *
4661 * LOCKING:
4662 * Inherited from calling layer (may sleep).
4663 */
4664
Alan Cox17b14452005-09-15 15:44:00 +01004665void ata_host_set_remove(struct ata_host_set *host_set)
4666{
4667 struct ata_port *ap;
4668 unsigned int i;
4669
4670 for (i = 0; i < host_set->n_ports; i++) {
4671 ap = host_set->ports[i];
4672 scsi_remove_host(ap->host);
4673 }
4674
4675 free_irq(host_set->irq, host_set);
4676
4677 for (i = 0; i < host_set->n_ports; i++) {
4678 ap = host_set->ports[i];
4679
4680 ata_scsi_release(ap->host);
4681
4682 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
4683 struct ata_ioports *ioaddr = &ap->ioaddr;
4684
4685 if (ioaddr->cmd_addr == 0x1f0)
4686 release_region(0x1f0, 8);
4687 else if (ioaddr->cmd_addr == 0x170)
4688 release_region(0x170, 8);
4689 }
4690
4691 scsi_host_put(ap->host);
4692 }
4693
4694 if (host_set->ops->host_stop)
4695 host_set->ops->host_stop(host_set);
4696
4697 kfree(host_set);
4698}
4699
4700/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004701 * ata_scsi_release - SCSI layer callback hook for host unload
4702 * @host: libata host to be unloaded
4703 *
4704 * Performs all duties necessary to shut down a libata port...
4705 * Kill port kthread, disable port, and release resources.
4706 *
4707 * LOCKING:
4708 * Inherited from SCSI layer.
4709 *
4710 * RETURNS:
4711 * One.
4712 */
4713
4714int ata_scsi_release(struct Scsi_Host *host)
4715{
4716 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
4717
4718 DPRINTK("ENTER\n");
4719
4720 ap->ops->port_disable(ap);
4721 ata_host_remove(ap, 0);
4722
4723 DPRINTK("EXIT\n");
4724 return 1;
4725}
4726
4727/**
4728 * ata_std_ports - initialize ioaddr with standard port offsets.
4729 * @ioaddr: IO address structure to be initialized
Edward Falk0baab862005-06-02 18:17:13 -04004730 *
4731 * Utility function which initializes data_addr, error_addr,
4732 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
4733 * device_addr, status_addr, and command_addr to standard offsets
4734 * relative to cmd_addr.
4735 *
4736 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737 */
Edward Falk0baab862005-06-02 18:17:13 -04004738
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739void ata_std_ports(struct ata_ioports *ioaddr)
4740{
4741 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
4742 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
4743 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
4744 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
4745 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
4746 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
4747 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
4748 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
4749 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
4750 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
4751}
4752
Edward Falk0baab862005-06-02 18:17:13 -04004753
Jeff Garzik374b1872005-08-30 05:42:52 -04004754#ifdef CONFIG_PCI
4755
4756void ata_pci_host_stop (struct ata_host_set *host_set)
4757{
4758 struct pci_dev *pdev = to_pci_dev(host_set->dev);
4759
4760 pci_iounmap(pdev, host_set->mmio_base);
4761}
4762
Edward Falk0baab862005-06-02 18:17:13 -04004763/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764 * ata_pci_remove_one - PCI layer callback for device removal
4765 * @pdev: PCI device that was removed
4766 *
4767 * PCI layer indicates to libata via this hook that
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004768 * hot-unplug or module unload event has occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004769 * Handle this by unregistering all objects associated
4770 * with this PCI device. Free those objects. Then finally
4771 * release PCI resources and disable device.
4772 *
4773 * LOCKING:
4774 * Inherited from PCI layer (may sleep).
4775 */
4776
4777void ata_pci_remove_one (struct pci_dev *pdev)
4778{
4779 struct device *dev = pci_dev_to_dev(pdev);
4780 struct ata_host_set *host_set = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781
Alan Cox17b14452005-09-15 15:44:00 +01004782 ata_host_set_remove(host_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783 pci_release_regions(pdev);
4784 pci_disable_device(pdev);
4785 dev_set_drvdata(dev, NULL);
4786}
4787
4788/* move to PCI subsystem */
Jeff Garzik057ace52005-10-22 14:27:05 -04004789int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790{
4791 unsigned long tmp = 0;
4792
4793 switch (bits->width) {
4794 case 1: {
4795 u8 tmp8 = 0;
4796 pci_read_config_byte(pdev, bits->reg, &tmp8);
4797 tmp = tmp8;
4798 break;
4799 }
4800 case 2: {
4801 u16 tmp16 = 0;
4802 pci_read_config_word(pdev, bits->reg, &tmp16);
4803 tmp = tmp16;
4804 break;
4805 }
4806 case 4: {
4807 u32 tmp32 = 0;
4808 pci_read_config_dword(pdev, bits->reg, &tmp32);
4809 tmp = tmp32;
4810 break;
4811 }
4812
4813 default:
4814 return -EINVAL;
4815 }
4816
4817 tmp &= bits->mask;
4818
4819 return (tmp == bits->val) ? 1 : 0;
4820}
Jens Axboe9b847542006-01-06 09:28:07 +01004821
4822int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t state)
4823{
4824 pci_save_state(pdev);
4825 pci_disable_device(pdev);
4826 pci_set_power_state(pdev, PCI_D3hot);
4827 return 0;
4828}
4829
4830int ata_pci_device_resume(struct pci_dev *pdev)
4831{
4832 pci_set_power_state(pdev, PCI_D0);
4833 pci_restore_state(pdev);
4834 pci_enable_device(pdev);
4835 pci_set_master(pdev);
4836 return 0;
4837}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838#endif /* CONFIG_PCI */
4839
4840
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841static int __init ata_init(void)
4842{
4843 ata_wq = create_workqueue("ata");
4844 if (!ata_wq)
4845 return -ENOMEM;
4846
4847 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
4848 return 0;
4849}
4850
4851static void __exit ata_exit(void)
4852{
4853 destroy_workqueue(ata_wq);
4854}
4855
4856module_init(ata_init);
4857module_exit(ata_exit);
4858
Jeff Garzik67846b32005-10-05 02:58:32 -04004859static unsigned long ratelimit_time;
4860static spinlock_t ata_ratelimit_lock = SPIN_LOCK_UNLOCKED;
4861
4862int ata_ratelimit(void)
4863{
4864 int rc;
4865 unsigned long flags;
4866
4867 spin_lock_irqsave(&ata_ratelimit_lock, flags);
4868
4869 if (time_after(jiffies, ratelimit_time)) {
4870 rc = 1;
4871 ratelimit_time = jiffies + (HZ/5);
4872 } else
4873 rc = 0;
4874
4875 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
4876
4877 return rc;
4878}
4879
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880/*
4881 * libata is essentially a library of internal helper functions for
4882 * low-level ATA host controller drivers. As such, the API/ABI is
4883 * likely to change as new drivers are added and updated.
4884 * Do not depend on ABI/API stability.
4885 */
4886
4887EXPORT_SYMBOL_GPL(ata_std_bios_param);
4888EXPORT_SYMBOL_GPL(ata_std_ports);
4889EXPORT_SYMBOL_GPL(ata_device_add);
Alan Cox17b14452005-09-15 15:44:00 +01004890EXPORT_SYMBOL_GPL(ata_host_set_remove);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891EXPORT_SYMBOL_GPL(ata_sg_init);
4892EXPORT_SYMBOL_GPL(ata_sg_init_one);
Tejun Heo76014422006-02-11 15:13:49 +09004893EXPORT_SYMBOL_GPL(__ata_qc_complete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
4895EXPORT_SYMBOL_GPL(ata_eng_timeout);
4896EXPORT_SYMBOL_GPL(ata_tf_load);
4897EXPORT_SYMBOL_GPL(ata_tf_read);
4898EXPORT_SYMBOL_GPL(ata_noop_dev_select);
4899EXPORT_SYMBOL_GPL(ata_std_dev_select);
4900EXPORT_SYMBOL_GPL(ata_tf_to_fis);
4901EXPORT_SYMBOL_GPL(ata_tf_from_fis);
4902EXPORT_SYMBOL_GPL(ata_check_status);
4903EXPORT_SYMBOL_GPL(ata_altstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904EXPORT_SYMBOL_GPL(ata_exec_command);
4905EXPORT_SYMBOL_GPL(ata_port_start);
4906EXPORT_SYMBOL_GPL(ata_port_stop);
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04004907EXPORT_SYMBOL_GPL(ata_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908EXPORT_SYMBOL_GPL(ata_interrupt);
4909EXPORT_SYMBOL_GPL(ata_qc_prep);
4910EXPORT_SYMBOL_GPL(ata_bmdma_setup);
4911EXPORT_SYMBOL_GPL(ata_bmdma_start);
4912EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
4913EXPORT_SYMBOL_GPL(ata_bmdma_status);
4914EXPORT_SYMBOL_GPL(ata_bmdma_stop);
4915EXPORT_SYMBOL_GPL(ata_port_probe);
4916EXPORT_SYMBOL_GPL(sata_phy_reset);
4917EXPORT_SYMBOL_GPL(__sata_phy_reset);
4918EXPORT_SYMBOL_GPL(ata_bus_reset);
Tejun Heo8a19ac82006-02-02 18:20:00 +09004919EXPORT_SYMBOL_GPL(ata_std_probeinit);
Tejun Heoc2bd5802006-01-24 17:05:22 +09004920EXPORT_SYMBOL_GPL(ata_std_softreset);
4921EXPORT_SYMBOL_GPL(sata_std_hardreset);
4922EXPORT_SYMBOL_GPL(ata_std_postreset);
4923EXPORT_SYMBOL_GPL(ata_std_probe_reset);
Tejun Heoa62c0fc2006-01-24 17:05:22 +09004924EXPORT_SYMBOL_GPL(ata_drive_probe_reset);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925EXPORT_SYMBOL_GPL(ata_port_disable);
Jeff Garzik67846b32005-10-05 02:58:32 -04004926EXPORT_SYMBOL_GPL(ata_ratelimit);
Tejun Heo6f8b9952006-01-24 17:05:21 +09004927EXPORT_SYMBOL_GPL(ata_busy_sleep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
4929EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
Tejun Heof29841e2006-02-10 15:10:48 +09004930EXPORT_SYMBOL_GPL(ata_scsi_timed_out);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004931EXPORT_SYMBOL_GPL(ata_scsi_error);
4932EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
4933EXPORT_SYMBOL_GPL(ata_scsi_release);
4934EXPORT_SYMBOL_GPL(ata_host_intr);
4935EXPORT_SYMBOL_GPL(ata_dev_classify);
Tejun Heo6a62a042006-02-13 10:02:46 +09004936EXPORT_SYMBOL_GPL(ata_id_string);
4937EXPORT_SYMBOL_GPL(ata_id_c_string);
Brad Campbell6f2f3812005-05-12 15:07:47 -04004938EXPORT_SYMBOL_GPL(ata_dev_config);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004939EXPORT_SYMBOL_GPL(ata_scsi_simulate);
Tejun Heoa72ec4c2006-01-23 13:09:37 +09004940EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
4941EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942
Alan Cox1bc4ccf2006-01-09 17:18:14 +00004943EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
Alan Cox452503f2005-10-21 19:01:32 -04004944EXPORT_SYMBOL_GPL(ata_timing_compute);
4945EXPORT_SYMBOL_GPL(ata_timing_merge);
4946
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947#ifdef CONFIG_PCI
4948EXPORT_SYMBOL_GPL(pci_test_config_bits);
Jeff Garzik374b1872005-08-30 05:42:52 -04004949EXPORT_SYMBOL_GPL(ata_pci_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
4951EXPORT_SYMBOL_GPL(ata_pci_init_one);
4952EXPORT_SYMBOL_GPL(ata_pci_remove_one);
Jens Axboe9b847542006-01-06 09:28:07 +01004953EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
4954EXPORT_SYMBOL_GPL(ata_pci_device_resume);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955#endif /* CONFIG_PCI */
Jens Axboe9b847542006-01-06 09:28:07 +01004956
4957EXPORT_SYMBOL_GPL(ata_device_suspend);
4958EXPORT_SYMBOL_GPL(ata_device_resume);
4959EXPORT_SYMBOL_GPL(ata_scsi_device_suspend);
4960EXPORT_SYMBOL_GPL(ata_scsi_device_resume);