blob: a14187e32d0aad3f31500490e0db67eed60131a1 [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
Tejun Heo6aff8f12006-02-15 18:24:09 +090064static unsigned int ata_dev_init_params(struct ata_port *ap,
Albert Lee00b6f5e2006-03-27 16:39:18 +080065 struct ata_device *dev,
66 u16 heads,
67 u16 sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068static void ata_set_mode(struct ata_port *ap);
Tejun Heo83206a22006-03-24 15:25:31 +090069static unsigned int ata_dev_set_xfermode(struct ata_port *ap,
70 struct ata_device *dev);
Tejun Heoacf356b2006-03-24 14:07:50 +090071static void ata_dev_xfermask(struct ata_port *ap, struct ata_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
73static unsigned int ata_unique_id = 1;
74static struct workqueue_struct *ata_wq;
75
Jeff Garzik418dc1f2006-03-11 20:50:08 -050076int atapi_enabled = 1;
Jeff Garzik1623c812005-08-30 03:37:42 -040077module_param(atapi_enabled, int, 0444);
78MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)");
79
Jeff Garzikc3c013a2006-02-27 22:31:19 -050080int libata_fua = 0;
81module_param_named(fua, libata_fua, int, 0444);
82MODULE_PARM_DESC(fua, "FUA support (0=off, 1=on)");
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084MODULE_AUTHOR("Jeff Garzik");
85MODULE_DESCRIPTION("Library module for ATA devices");
86MODULE_LICENSE("GPL");
87MODULE_VERSION(DRV_VERSION);
88
Edward Falk0baab862005-06-02 18:17:13 -040089
90/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
92 * @tf: Taskfile to convert
93 * @fis: Buffer into which data will output
94 * @pmp: Port multiplier port
95 *
96 * Converts a standard ATA taskfile to a Serial ATA
97 * FIS structure (Register - Host to Device).
98 *
99 * LOCKING:
100 * Inherited from caller.
101 */
102
Jeff Garzik057ace52005-10-22 14:27:05 -0400103void ata_tf_to_fis(const struct ata_taskfile *tf, u8 *fis, u8 pmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
105 fis[0] = 0x27; /* Register - Host to Device FIS */
106 fis[1] = (pmp & 0xf) | (1 << 7); /* Port multiplier number,
107 bit 7 indicates Command FIS */
108 fis[2] = tf->command;
109 fis[3] = tf->feature;
110
111 fis[4] = tf->lbal;
112 fis[5] = tf->lbam;
113 fis[6] = tf->lbah;
114 fis[7] = tf->device;
115
116 fis[8] = tf->hob_lbal;
117 fis[9] = tf->hob_lbam;
118 fis[10] = tf->hob_lbah;
119 fis[11] = tf->hob_feature;
120
121 fis[12] = tf->nsect;
122 fis[13] = tf->hob_nsect;
123 fis[14] = 0;
124 fis[15] = tf->ctl;
125
126 fis[16] = 0;
127 fis[17] = 0;
128 fis[18] = 0;
129 fis[19] = 0;
130}
131
132/**
133 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
134 * @fis: Buffer from which data will be input
135 * @tf: Taskfile to output
136 *
Mark Lorde12a1be2005-11-12 18:55:45 -0500137 * Converts a serial ATA FIS structure to a standard ATA taskfile.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 *
139 * LOCKING:
140 * Inherited from caller.
141 */
142
Jeff Garzik057ace52005-10-22 14:27:05 -0400143void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
145 tf->command = fis[2]; /* status */
146 tf->feature = fis[3]; /* error */
147
148 tf->lbal = fis[4];
149 tf->lbam = fis[5];
150 tf->lbah = fis[6];
151 tf->device = fis[7];
152
153 tf->hob_lbal = fis[8];
154 tf->hob_lbam = fis[9];
155 tf->hob_lbah = fis[10];
156
157 tf->nsect = fis[12];
158 tf->hob_nsect = fis[13];
159}
160
Albert Lee8cbd6df2005-10-12 15:06:27 +0800161static const u8 ata_rw_cmds[] = {
162 /* pio multi */
163 ATA_CMD_READ_MULTI,
164 ATA_CMD_WRITE_MULTI,
165 ATA_CMD_READ_MULTI_EXT,
166 ATA_CMD_WRITE_MULTI_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100167 0,
168 0,
169 0,
170 ATA_CMD_WRITE_MULTI_FUA_EXT,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800171 /* pio */
172 ATA_CMD_PIO_READ,
173 ATA_CMD_PIO_WRITE,
174 ATA_CMD_PIO_READ_EXT,
175 ATA_CMD_PIO_WRITE_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100176 0,
177 0,
178 0,
179 0,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800180 /* dma */
181 ATA_CMD_READ,
182 ATA_CMD_WRITE,
183 ATA_CMD_READ_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100184 ATA_CMD_WRITE_EXT,
185 0,
186 0,
187 0,
188 ATA_CMD_WRITE_FUA_EXT
Albert Lee8cbd6df2005-10-12 15:06:27 +0800189};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191/**
Albert Lee8cbd6df2005-10-12 15:06:27 +0800192 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
193 * @qc: command to examine and configure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 *
Jeff Garzik2e9edbf2006-03-24 09:56:57 -0500195 * Examine the device configuration and tf->flags to calculate
Albert Lee8cbd6df2005-10-12 15:06:27 +0800196 * the proper read/write commands and protocol to use.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 *
198 * LOCKING:
199 * caller.
200 */
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100201int ata_rwcmd_protocol(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
Albert Lee8cbd6df2005-10-12 15:06:27 +0800203 struct ata_taskfile *tf = &qc->tf;
204 struct ata_device *dev = qc->dev;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100205 u8 cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100207 int index, fua, lba48, write;
Jeff Garzik2e9edbf2006-03-24 09:56:57 -0500208
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100209 fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800210 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
211 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Albert Lee8cbd6df2005-10-12 15:06:27 +0800213 if (dev->flags & ATA_DFLAG_PIO) {
214 tf->protocol = ATA_PROT_PIO;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100215 index = dev->multi_count ? 0 : 8;
Alan Cox8d238e02006-01-17 20:50:31 +0000216 } else if (lba48 && (qc->ap->flags & ATA_FLAG_PIO_LBA48)) {
217 /* Unable to use DMA due to host limitation */
218 tf->protocol = ATA_PROT_PIO;
Albert Leeaef9d532006-02-08 16:37:43 +0800219 index = dev->multi_count ? 0 : 8;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800220 } else {
221 tf->protocol = ATA_PROT_DMA;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100222 index = 16;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800223 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100225 cmd = ata_rw_cmds[index + fua + lba48 + write];
226 if (cmd) {
227 tf->command = cmd;
228 return 0;
229 }
230 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231}
232
Tejun Heocb95d562006-03-06 04:31:56 +0900233/**
234 * ata_pack_xfermask - Pack pio, mwdma and udma masks into xfer_mask
235 * @pio_mask: pio_mask
236 * @mwdma_mask: mwdma_mask
237 * @udma_mask: udma_mask
238 *
239 * Pack @pio_mask, @mwdma_mask and @udma_mask into a single
240 * unsigned int xfer_mask.
241 *
242 * LOCKING:
243 * None.
244 *
245 * RETURNS:
246 * Packed xfer_mask.
247 */
248static unsigned int ata_pack_xfermask(unsigned int pio_mask,
249 unsigned int mwdma_mask,
250 unsigned int udma_mask)
251{
252 return ((pio_mask << ATA_SHIFT_PIO) & ATA_MASK_PIO) |
253 ((mwdma_mask << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA) |
254 ((udma_mask << ATA_SHIFT_UDMA) & ATA_MASK_UDMA);
255}
256
Tejun Heoc0489e42006-03-24 14:07:49 +0900257/**
258 * ata_unpack_xfermask - Unpack xfer_mask into pio, mwdma and udma masks
259 * @xfer_mask: xfer_mask to unpack
260 * @pio_mask: resulting pio_mask
261 * @mwdma_mask: resulting mwdma_mask
262 * @udma_mask: resulting udma_mask
263 *
264 * Unpack @xfer_mask into @pio_mask, @mwdma_mask and @udma_mask.
265 * Any NULL distination masks will be ignored.
266 */
267static void ata_unpack_xfermask(unsigned int xfer_mask,
268 unsigned int *pio_mask,
269 unsigned int *mwdma_mask,
270 unsigned int *udma_mask)
271{
272 if (pio_mask)
273 *pio_mask = (xfer_mask & ATA_MASK_PIO) >> ATA_SHIFT_PIO;
274 if (mwdma_mask)
275 *mwdma_mask = (xfer_mask & ATA_MASK_MWDMA) >> ATA_SHIFT_MWDMA;
276 if (udma_mask)
277 *udma_mask = (xfer_mask & ATA_MASK_UDMA) >> ATA_SHIFT_UDMA;
278}
279
Tejun Heocb95d562006-03-06 04:31:56 +0900280static const struct ata_xfer_ent {
281 unsigned int shift, bits;
282 u8 base;
283} ata_xfer_tbl[] = {
284 { ATA_SHIFT_PIO, ATA_BITS_PIO, XFER_PIO_0 },
285 { ATA_SHIFT_MWDMA, ATA_BITS_MWDMA, XFER_MW_DMA_0 },
286 { ATA_SHIFT_UDMA, ATA_BITS_UDMA, XFER_UDMA_0 },
287 { -1, },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288};
289
290/**
Tejun Heocb95d562006-03-06 04:31:56 +0900291 * ata_xfer_mask2mode - Find matching XFER_* for the given xfer_mask
292 * @xfer_mask: xfer_mask of interest
293 *
294 * Return matching XFER_* value for @xfer_mask. Only the highest
295 * bit of @xfer_mask is considered.
296 *
297 * LOCKING:
298 * None.
299 *
300 * RETURNS:
301 * Matching XFER_* value, 0 if no match found.
302 */
303static u8 ata_xfer_mask2mode(unsigned int xfer_mask)
304{
305 int highbit = fls(xfer_mask) - 1;
306 const struct ata_xfer_ent *ent;
307
308 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
309 if (highbit >= ent->shift && highbit < ent->shift + ent->bits)
310 return ent->base + highbit - ent->shift;
311 return 0;
312}
313
314/**
315 * ata_xfer_mode2mask - Find matching xfer_mask for XFER_*
316 * @xfer_mode: XFER_* of interest
317 *
318 * Return matching xfer_mask for @xfer_mode.
319 *
320 * LOCKING:
321 * None.
322 *
323 * RETURNS:
324 * Matching xfer_mask, 0 if no match found.
325 */
326static unsigned int ata_xfer_mode2mask(u8 xfer_mode)
327{
328 const struct ata_xfer_ent *ent;
329
330 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
331 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
332 return 1 << (ent->shift + xfer_mode - ent->base);
333 return 0;
334}
335
336/**
337 * ata_xfer_mode2shift - Find matching xfer_shift for XFER_*
338 * @xfer_mode: XFER_* of interest
339 *
340 * Return matching xfer_shift for @xfer_mode.
341 *
342 * LOCKING:
343 * None.
344 *
345 * RETURNS:
346 * Matching xfer_shift, -1 if no match found.
347 */
348static int ata_xfer_mode2shift(unsigned int xfer_mode)
349{
350 const struct ata_xfer_ent *ent;
351
352 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
353 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
354 return ent->shift;
355 return -1;
356}
357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358/**
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900359 * ata_mode_string - convert xfer_mask to string
360 * @xfer_mask: mask of bits supported; only highest bit counts.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 *
362 * Determine string which represents the highest speed
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900363 * (highest bit in @modemask).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 *
365 * LOCKING:
366 * None.
367 *
368 * RETURNS:
369 * Constant C string representing highest speed listed in
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900370 * @mode_mask, or the constant C string "<n/a>".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 */
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900372static const char *ata_mode_string(unsigned int xfer_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
Tejun Heo75f554b2006-03-06 04:31:57 +0900374 static const char * const xfer_mode_str[] = {
375 "PIO0",
376 "PIO1",
377 "PIO2",
378 "PIO3",
379 "PIO4",
380 "MWDMA0",
381 "MWDMA1",
382 "MWDMA2",
383 "UDMA/16",
384 "UDMA/25",
385 "UDMA/33",
386 "UDMA/44",
387 "UDMA/66",
388 "UDMA/100",
389 "UDMA/133",
390 "UDMA7",
391 };
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900392 int highbit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900394 highbit = fls(xfer_mask) - 1;
395 if (highbit >= 0 && highbit < ARRAY_SIZE(xfer_mode_str))
396 return xfer_mode_str[highbit];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 return "<n/a>";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398}
399
Tejun Heo0b8efb02006-03-24 15:25:31 +0900400static void ata_dev_disable(struct ata_port *ap, struct ata_device *dev)
401{
402 if (ata_dev_present(dev)) {
403 printk(KERN_WARNING "ata%u: dev %u disabled\n",
404 ap->id, dev->devno);
405 dev->class++;
406 }
407}
408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409/**
410 * ata_pio_devchk - PATA device presence detection
411 * @ap: ATA channel to examine
412 * @device: Device to examine (starting at zero)
413 *
414 * This technique was originally described in
415 * Hale Landis's ATADRVR (www.ata-atapi.com), and
416 * later found its way into the ATA/ATAPI spec.
417 *
418 * Write a pattern to the ATA shadow registers,
419 * and if a device is present, it will respond by
420 * correctly storing and echoing back the
421 * ATA shadow register contents.
422 *
423 * LOCKING:
424 * caller.
425 */
426
427static unsigned int ata_pio_devchk(struct ata_port *ap,
428 unsigned int device)
429{
430 struct ata_ioports *ioaddr = &ap->ioaddr;
431 u8 nsect, lbal;
432
433 ap->ops->dev_select(ap, device);
434
435 outb(0x55, ioaddr->nsect_addr);
436 outb(0xaa, ioaddr->lbal_addr);
437
438 outb(0xaa, ioaddr->nsect_addr);
439 outb(0x55, ioaddr->lbal_addr);
440
441 outb(0x55, ioaddr->nsect_addr);
442 outb(0xaa, ioaddr->lbal_addr);
443
444 nsect = inb(ioaddr->nsect_addr);
445 lbal = inb(ioaddr->lbal_addr);
446
447 if ((nsect == 0x55) && (lbal == 0xaa))
448 return 1; /* we found a device */
449
450 return 0; /* nothing found */
451}
452
453/**
454 * ata_mmio_devchk - PATA device presence detection
455 * @ap: ATA channel to examine
456 * @device: Device to examine (starting at zero)
457 *
458 * This technique was originally described in
459 * Hale Landis's ATADRVR (www.ata-atapi.com), and
460 * later found its way into the ATA/ATAPI spec.
461 *
462 * Write a pattern to the ATA shadow registers,
463 * and if a device is present, it will respond by
464 * correctly storing and echoing back the
465 * ATA shadow register contents.
466 *
467 * LOCKING:
468 * caller.
469 */
470
471static unsigned int ata_mmio_devchk(struct ata_port *ap,
472 unsigned int device)
473{
474 struct ata_ioports *ioaddr = &ap->ioaddr;
475 u8 nsect, lbal;
476
477 ap->ops->dev_select(ap, device);
478
479 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
480 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
481
482 writeb(0xaa, (void __iomem *) ioaddr->nsect_addr);
483 writeb(0x55, (void __iomem *) ioaddr->lbal_addr);
484
485 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
486 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
487
488 nsect = readb((void __iomem *) ioaddr->nsect_addr);
489 lbal = readb((void __iomem *) ioaddr->lbal_addr);
490
491 if ((nsect == 0x55) && (lbal == 0xaa))
492 return 1; /* we found a device */
493
494 return 0; /* nothing found */
495}
496
497/**
498 * ata_devchk - PATA device presence detection
499 * @ap: ATA channel to examine
500 * @device: Device to examine (starting at zero)
501 *
502 * Dispatch ATA device presence detection, depending
503 * on whether we are using PIO or MMIO to talk to the
504 * ATA shadow registers.
505 *
506 * LOCKING:
507 * caller.
508 */
509
510static unsigned int ata_devchk(struct ata_port *ap,
511 unsigned int device)
512{
513 if (ap->flags & ATA_FLAG_MMIO)
514 return ata_mmio_devchk(ap, device);
515 return ata_pio_devchk(ap, device);
516}
517
518/**
519 * ata_dev_classify - determine device type based on ATA-spec signature
520 * @tf: ATA taskfile register set for device to be identified
521 *
522 * Determine from taskfile register contents whether a device is
523 * ATA or ATAPI, as per "Signature and persistence" section
524 * of ATA/PI spec (volume 1, sect 5.14).
525 *
526 * LOCKING:
527 * None.
528 *
529 * RETURNS:
530 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
531 * the event of failure.
532 */
533
Jeff Garzik057ace52005-10-22 14:27:05 -0400534unsigned int ata_dev_classify(const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535{
536 /* Apple's open source Darwin code hints that some devices only
537 * put a proper signature into the LBA mid/high registers,
538 * So, we only check those. It's sufficient for uniqueness.
539 */
540
541 if (((tf->lbam == 0) && (tf->lbah == 0)) ||
542 ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
543 DPRINTK("found ATA device by sig\n");
544 return ATA_DEV_ATA;
545 }
546
547 if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
548 ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
549 DPRINTK("found ATAPI device by sig\n");
550 return ATA_DEV_ATAPI;
551 }
552
553 DPRINTK("unknown device\n");
554 return ATA_DEV_UNKNOWN;
555}
556
557/**
558 * ata_dev_try_classify - Parse returned ATA device signature
559 * @ap: ATA channel to examine
560 * @device: Device to examine (starting at zero)
Tejun Heob4dc7622006-01-24 17:05:22 +0900561 * @r_err: Value of error register on completion
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 *
563 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
564 * an ATA/ATAPI-defined set of values is placed in the ATA
565 * shadow registers, indicating the results of device detection
566 * and diagnostics.
567 *
568 * Select the ATA device, and read the values from the ATA shadow
569 * registers. Then parse according to the Error register value,
570 * and the spec-defined values examined by ata_dev_classify().
571 *
572 * LOCKING:
573 * caller.
Tejun Heob4dc7622006-01-24 17:05:22 +0900574 *
575 * RETURNS:
576 * Device type - %ATA_DEV_ATA, %ATA_DEV_ATAPI or %ATA_DEV_NONE.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 */
578
Tejun Heob4dc7622006-01-24 17:05:22 +0900579static unsigned int
580ata_dev_try_classify(struct ata_port *ap, unsigned int device, u8 *r_err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 struct ata_taskfile tf;
583 unsigned int class;
584 u8 err;
585
586 ap->ops->dev_select(ap, device);
587
588 memset(&tf, 0, sizeof(tf));
589
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 ap->ops->tf_read(ap, &tf);
Jeff Garzik0169e282005-10-29 21:25:10 -0400591 err = tf.feature;
Tejun Heob4dc7622006-01-24 17:05:22 +0900592 if (r_err)
593 *r_err = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
595 /* see if device passed diags */
596 if (err == 1)
597 /* do nothing */ ;
598 else if ((device == 0) && (err == 0x81))
599 /* do nothing */ ;
600 else
Tejun Heob4dc7622006-01-24 17:05:22 +0900601 return ATA_DEV_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
Tejun Heob4dc7622006-01-24 17:05:22 +0900603 /* determine if device is ATA or ATAPI */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 class = ata_dev_classify(&tf);
Tejun Heob4dc7622006-01-24 17:05:22 +0900605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 if (class == ATA_DEV_UNKNOWN)
Tejun Heob4dc7622006-01-24 17:05:22 +0900607 return ATA_DEV_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
Tejun Heob4dc7622006-01-24 17:05:22 +0900609 return ATA_DEV_NONE;
610 return class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611}
612
613/**
Tejun Heo6a62a042006-02-13 10:02:46 +0900614 * ata_id_string - Convert IDENTIFY DEVICE page into string
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615 * @id: IDENTIFY DEVICE results we will examine
616 * @s: string into which data is output
617 * @ofs: offset into identify device page
618 * @len: length of string to return. must be an even number.
619 *
620 * The strings in the IDENTIFY DEVICE page are broken up into
621 * 16-bit chunks. Run through the string, and output each
622 * 8-bit chunk linearly, regardless of platform.
623 *
624 * LOCKING:
625 * caller.
626 */
627
Tejun Heo6a62a042006-02-13 10:02:46 +0900628void ata_id_string(const u16 *id, unsigned char *s,
629 unsigned int ofs, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
631 unsigned int c;
632
633 while (len > 0) {
634 c = id[ofs] >> 8;
635 *s = c;
636 s++;
637
638 c = id[ofs] & 0xff;
639 *s = c;
640 s++;
641
642 ofs++;
643 len -= 2;
644 }
645}
646
Tejun Heo0e949ff2006-02-12 22:47:04 +0900647/**
Tejun Heo6a62a042006-02-13 10:02:46 +0900648 * ata_id_c_string - Convert IDENTIFY DEVICE page into C string
Tejun Heo0e949ff2006-02-12 22:47:04 +0900649 * @id: IDENTIFY DEVICE results we will examine
650 * @s: string into which data is output
651 * @ofs: offset into identify device page
652 * @len: length of string to return. must be an odd number.
653 *
Tejun Heo6a62a042006-02-13 10:02:46 +0900654 * This function is identical to ata_id_string except that it
Tejun Heo0e949ff2006-02-12 22:47:04 +0900655 * trims trailing spaces and terminates the resulting string with
656 * null. @len must be actual maximum length (even number) + 1.
657 *
658 * LOCKING:
659 * caller.
660 */
Tejun Heo6a62a042006-02-13 10:02:46 +0900661void ata_id_c_string(const u16 *id, unsigned char *s,
662 unsigned int ofs, unsigned int len)
Tejun Heo0e949ff2006-02-12 22:47:04 +0900663{
664 unsigned char *p;
665
666 WARN_ON(!(len & 1));
667
Tejun Heo6a62a042006-02-13 10:02:46 +0900668 ata_id_string(id, s, ofs, len - 1);
Tejun Heo0e949ff2006-02-12 22:47:04 +0900669
670 p = s + strnlen(s, len - 1);
671 while (p > s && p[-1] == ' ')
672 p--;
673 *p = '\0';
674}
Edward Falk0baab862005-06-02 18:17:13 -0400675
Tejun Heo29407402006-02-12 22:47:04 +0900676static u64 ata_id_n_sectors(const u16 *id)
677{
678 if (ata_id_has_lba(id)) {
679 if (ata_id_has_lba48(id))
680 return ata_id_u64(id, 100);
681 else
682 return ata_id_u32(id, 60);
683 } else {
684 if (ata_id_current_chs_valid(id))
685 return ata_id_u32(id, 57);
686 else
687 return id[1] * id[3] * id[6];
688 }
689}
Edward Falk0baab862005-06-02 18:17:13 -0400690
691/**
692 * ata_noop_dev_select - Select device 0/1 on ATA bus
693 * @ap: ATA channel to manipulate
694 * @device: ATA device (numbered from zero) to select
695 *
696 * This function performs no actual function.
697 *
698 * May be used as the dev_select() entry in ata_port_operations.
699 *
700 * LOCKING:
701 * caller.
702 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
704{
705}
706
Edward Falk0baab862005-06-02 18:17:13 -0400707
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708/**
709 * ata_std_dev_select - Select device 0/1 on ATA bus
710 * @ap: ATA channel to manipulate
711 * @device: ATA device (numbered from zero) to select
712 *
713 * Use the method defined in the ATA specification to
714 * make either device 0, or device 1, active on the
Edward Falk0baab862005-06-02 18:17:13 -0400715 * ATA channel. Works with both PIO and MMIO.
716 *
717 * May be used as the dev_select() entry in ata_port_operations.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 *
719 * LOCKING:
720 * caller.
721 */
722
723void ata_std_dev_select (struct ata_port *ap, unsigned int device)
724{
725 u8 tmp;
726
727 if (device == 0)
728 tmp = ATA_DEVICE_OBS;
729 else
730 tmp = ATA_DEVICE_OBS | ATA_DEV1;
731
732 if (ap->flags & ATA_FLAG_MMIO) {
733 writeb(tmp, (void __iomem *) ap->ioaddr.device_addr);
734 } else {
735 outb(tmp, ap->ioaddr.device_addr);
736 }
737 ata_pause(ap); /* needed; also flushes, for mmio */
738}
739
740/**
741 * ata_dev_select - Select device 0/1 on ATA bus
742 * @ap: ATA channel to manipulate
743 * @device: ATA device (numbered from zero) to select
744 * @wait: non-zero to wait for Status register BSY bit to clear
745 * @can_sleep: non-zero if context allows sleeping
746 *
747 * Use the method defined in the ATA specification to
748 * make either device 0, or device 1, active on the
749 * ATA channel.
750 *
751 * This is a high-level version of ata_std_dev_select(),
752 * which additionally provides the services of inserting
753 * the proper pauses and status polling, where needed.
754 *
755 * LOCKING:
756 * caller.
757 */
758
759void ata_dev_select(struct ata_port *ap, unsigned int device,
760 unsigned int wait, unsigned int can_sleep)
761{
762 VPRINTK("ENTER, ata%u: device %u, wait %u\n",
763 ap->id, device, wait);
764
765 if (wait)
766 ata_wait_idle(ap);
767
768 ap->ops->dev_select(ap, device);
769
770 if (wait) {
771 if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
772 msleep(150);
773 ata_wait_idle(ap);
774 }
775}
776
777/**
778 * ata_dump_id - IDENTIFY DEVICE info debugging output
Tejun Heo0bd33002006-02-12 22:47:05 +0900779 * @id: IDENTIFY DEVICE page to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 *
Tejun Heo0bd33002006-02-12 22:47:05 +0900781 * Dump selected 16-bit words from the given IDENTIFY DEVICE
782 * page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 *
784 * LOCKING:
785 * caller.
786 */
787
Tejun Heo0bd33002006-02-12 22:47:05 +0900788static inline void ata_dump_id(const u16 *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789{
790 DPRINTK("49==0x%04x "
791 "53==0x%04x "
792 "63==0x%04x "
793 "64==0x%04x "
794 "75==0x%04x \n",
Tejun Heo0bd33002006-02-12 22:47:05 +0900795 id[49],
796 id[53],
797 id[63],
798 id[64],
799 id[75]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 DPRINTK("80==0x%04x "
801 "81==0x%04x "
802 "82==0x%04x "
803 "83==0x%04x "
804 "84==0x%04x \n",
Tejun Heo0bd33002006-02-12 22:47:05 +0900805 id[80],
806 id[81],
807 id[82],
808 id[83],
809 id[84]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 DPRINTK("88==0x%04x "
811 "93==0x%04x\n",
Tejun Heo0bd33002006-02-12 22:47:05 +0900812 id[88],
813 id[93]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814}
815
Tejun Heocb95d562006-03-06 04:31:56 +0900816/**
817 * ata_id_xfermask - Compute xfermask from the given IDENTIFY data
818 * @id: IDENTIFY data to compute xfer mask from
Alan Cox11e29e22005-10-21 18:46:32 -0400819 *
Tejun Heocb95d562006-03-06 04:31:56 +0900820 * Compute the xfermask for this device. This is not as trivial
821 * as it seems if we must consider early devices correctly.
822 *
823 * FIXME: pre IDE drive timing (do we care ?).
824 *
825 * LOCKING:
826 * None.
827 *
828 * RETURNS:
829 * Computed xfermask
Alan Cox11e29e22005-10-21 18:46:32 -0400830 */
Tejun Heocb95d562006-03-06 04:31:56 +0900831static unsigned int ata_id_xfermask(const u16 *id)
Alan Cox11e29e22005-10-21 18:46:32 -0400832{
Tejun Heocb95d562006-03-06 04:31:56 +0900833 unsigned int pio_mask, mwdma_mask, udma_mask;
Alan Cox11e29e22005-10-21 18:46:32 -0400834
Alan Coxffa29452006-01-09 17:14:40 +0000835 /* Usual case. Word 53 indicates word 64 is valid */
Tejun Heocb95d562006-03-06 04:31:56 +0900836 if (id[ATA_ID_FIELD_VALID] & (1 << 1)) {
837 pio_mask = id[ATA_ID_PIO_MODES] & 0x03;
838 pio_mask <<= 3;
839 pio_mask |= 0x7;
840 } else {
841 /* If word 64 isn't valid then Word 51 high byte holds
842 * the PIO timing number for the maximum. Turn it into
843 * a mask.
844 */
845 pio_mask = (2 << (id[ATA_ID_OLD_PIO_MODES] & 0xFF)) - 1 ;
846
847 /* But wait.. there's more. Design your standards by
848 * committee and you too can get a free iordy field to
849 * process. However its the speeds not the modes that
850 * are supported... Note drivers using the timing API
851 * will get this right anyway
852 */
Alan Cox11e29e22005-10-21 18:46:32 -0400853 }
854
Tejun Heocb95d562006-03-06 04:31:56 +0900855 mwdma_mask = id[ATA_ID_MWDMA_MODES] & 0x07;
Tejun Heofb21f0d2006-03-12 12:34:35 +0900856
857 udma_mask = 0;
858 if (id[ATA_ID_FIELD_VALID] & (1 << 2))
859 udma_mask = id[ATA_ID_UDMA_MODES] & 0xff;
Alan Cox11e29e22005-10-21 18:46:32 -0400860
Tejun Heocb95d562006-03-06 04:31:56 +0900861 return ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
Tejun Heoc18d06f2006-02-02 00:56:10 +0900862}
863
864/**
Tejun Heo86e45b62006-03-05 15:29:09 +0900865 * ata_port_queue_task - Queue port_task
866 * @ap: The ata_port to queue port_task for
Tejun Heoc18d06f2006-02-02 00:56:10 +0900867 *
Tejun Heo86e45b62006-03-05 15:29:09 +0900868 * Schedule @fn(@data) for execution after @delay jiffies using
869 * port_task. There is one port_task per port and it's the
870 * user(low level driver)'s responsibility to make sure that only
871 * one task is active at any given time.
872 *
873 * libata core layer takes care of synchronization between
874 * port_task and EH. ata_port_queue_task() may be ignored for EH
875 * synchronization.
876 *
877 * LOCKING:
878 * Inherited from caller.
879 */
880void ata_port_queue_task(struct ata_port *ap, void (*fn)(void *), void *data,
881 unsigned long delay)
882{
883 int rc;
884
Tejun Heo2e755f62006-03-05 15:29:09 +0900885 if (ap->flags & ATA_FLAG_FLUSH_PORT_TASK)
Tejun Heo86e45b62006-03-05 15:29:09 +0900886 return;
887
888 PREPARE_WORK(&ap->port_task, fn, data);
889
890 if (!delay)
891 rc = queue_work(ata_wq, &ap->port_task);
892 else
893 rc = queue_delayed_work(ata_wq, &ap->port_task, delay);
894
895 /* rc == 0 means that another user is using port task */
896 WARN_ON(rc == 0);
897}
898
899/**
900 * ata_port_flush_task - Flush port_task
901 * @ap: The ata_port to flush port_task for
902 *
903 * After this function completes, port_task is guranteed not to
904 * be running or scheduled.
Tejun Heoc18d06f2006-02-02 00:56:10 +0900905 *
906 * LOCKING:
907 * Kernel thread context (may sleep)
908 */
Tejun Heo86e45b62006-03-05 15:29:09 +0900909void ata_port_flush_task(struct ata_port *ap)
Tejun Heoc18d06f2006-02-02 00:56:10 +0900910{
Tejun Heoc18d06f2006-02-02 00:56:10 +0900911 unsigned long flags;
912
913 DPRINTK("ENTER\n");
914
915 spin_lock_irqsave(&ap->host_set->lock, flags);
Tejun Heo2e755f62006-03-05 15:29:09 +0900916 ap->flags |= ATA_FLAG_FLUSH_PORT_TASK;
Tejun Heoc18d06f2006-02-02 00:56:10 +0900917 spin_unlock_irqrestore(&ap->host_set->lock, flags);
918
919 DPRINTK("flush #1\n");
920 flush_workqueue(ata_wq);
921
922 /*
923 * At this point, if a task is running, it's guaranteed to see
924 * the FLUSH flag; thus, it will never queue pio tasks again.
925 * Cancel and flush.
926 */
Tejun Heo86e45b62006-03-05 15:29:09 +0900927 if (!cancel_delayed_work(&ap->port_task)) {
Tejun Heoc18d06f2006-02-02 00:56:10 +0900928 DPRINTK("flush #2\n");
929 flush_workqueue(ata_wq);
930 }
931
932 spin_lock_irqsave(&ap->host_set->lock, flags);
Tejun Heo2e755f62006-03-05 15:29:09 +0900933 ap->flags &= ~ATA_FLAG_FLUSH_PORT_TASK;
Tejun Heoc18d06f2006-02-02 00:56:10 +0900934 spin_unlock_irqrestore(&ap->host_set->lock, flags);
935
936 DPRINTK("EXIT\n");
Tejun Heo95064372006-01-23 13:09:37 +0900937}
938
Tejun Heo77853bf2006-01-23 13:09:36 +0900939void ata_qc_complete_internal(struct ata_queued_cmd *qc)
Jeff Garzik64f043d2005-11-17 10:50:01 -0500940{
Tejun Heo77853bf2006-01-23 13:09:36 +0900941 struct completion *waiting = qc->private_data;
Jeff Garzik64f043d2005-11-17 10:50:01 -0500942
Tejun Heo77853bf2006-01-23 13:09:36 +0900943 qc->ap->ops->tf_read(qc->ap, &qc->tf);
Tejun Heoa2a7a662005-12-13 14:48:31 +0900944 complete(waiting);
Tejun Heoa2a7a662005-12-13 14:48:31 +0900945}
Jeff Garzik64f043d2005-11-17 10:50:01 -0500946
Tejun Heoa2a7a662005-12-13 14:48:31 +0900947/**
948 * ata_exec_internal - execute libata internal command
949 * @ap: Port to which the command is sent
950 * @dev: Device to which the command is sent
951 * @tf: Taskfile registers for the command and the result
952 * @dma_dir: Data tranfer direction of the command
953 * @buf: Data buffer of the command
954 * @buflen: Length of data buffer
955 *
956 * Executes libata internal command with timeout. @tf contains
957 * command on entry and result on return. Timeout and error
958 * conditions are reported via return value. No recovery action
959 * is taken after a command times out. It's caller's duty to
960 * clean up after timeout.
961 *
962 * LOCKING:
963 * None. Should be called with kernel context, might sleep.
964 */
965
966static unsigned
967ata_exec_internal(struct ata_port *ap, struct ata_device *dev,
968 struct ata_taskfile *tf,
969 int dma_dir, void *buf, unsigned int buflen)
970{
971 u8 command = tf->command;
972 struct ata_queued_cmd *qc;
973 DECLARE_COMPLETION(wait);
974 unsigned long flags;
Tejun Heo77853bf2006-01-23 13:09:36 +0900975 unsigned int err_mask;
Tejun Heoa2a7a662005-12-13 14:48:31 +0900976
977 spin_lock_irqsave(&ap->host_set->lock, flags);
978
979 qc = ata_qc_new_init(ap, dev);
980 BUG_ON(qc == NULL);
981
982 qc->tf = *tf;
983 qc->dma_dir = dma_dir;
984 if (dma_dir != DMA_NONE) {
985 ata_sg_init_one(qc, buf, buflen);
986 qc->nsect = buflen / ATA_SECT_SIZE;
Jeff Garzik64f043d2005-11-17 10:50:01 -0500987 }
988
Tejun Heo77853bf2006-01-23 13:09:36 +0900989 qc->private_data = &wait;
Tejun Heoa2a7a662005-12-13 14:48:31 +0900990 qc->complete_fn = ata_qc_complete_internal;
991
Tejun Heo9a3d9eb2006-01-23 13:09:36 +0900992 qc->err_mask = ata_qc_issue(qc);
993 if (qc->err_mask)
Tejun Heo8e436af2006-01-23 13:09:36 +0900994 ata_qc_complete(qc);
Tejun Heoa2a7a662005-12-13 14:48:31 +0900995
996 spin_unlock_irqrestore(&ap->host_set->lock, flags);
997
998 if (!wait_for_completion_timeout(&wait, ATA_TMOUT_INTERNAL)) {
Albert Lee41ade502006-03-14 11:19:04 +0800999 ata_port_flush_task(ap);
1000
Tejun Heoa2a7a662005-12-13 14:48:31 +09001001 spin_lock_irqsave(&ap->host_set->lock, flags);
1002
1003 /* We're racing with irq here. If we lose, the
1004 * following test prevents us from completing the qc
1005 * again. If completion irq occurs after here but
1006 * before the caller cleans up, it will result in a
1007 * spurious interrupt. We can live with that.
1008 */
Tejun Heo77853bf2006-01-23 13:09:36 +09001009 if (qc->flags & ATA_QCFLAG_ACTIVE) {
Tejun Heo11a56d22006-01-23 13:09:36 +09001010 qc->err_mask = AC_ERR_TIMEOUT;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001011 ata_qc_complete(qc);
1012 printk(KERN_WARNING "ata%u: qc timeout (cmd 0x%x)\n",
1013 ap->id, command);
1014 }
1015
1016 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1017 }
1018
Tejun Heo77853bf2006-01-23 13:09:36 +09001019 *tf = qc->tf;
1020 err_mask = qc->err_mask;
1021
1022 ata_qc_free(qc);
1023
Tejun Heo1f7dd3e2006-03-24 15:25:30 +09001024 /* XXX - Some LLDDs (sata_mv) disable port on command failure.
1025 * Until those drivers are fixed, we detect the condition
1026 * here, fail the command with AC_ERR_SYSTEM and reenable the
1027 * port.
1028 *
1029 * Note that this doesn't change any behavior as internal
1030 * command failure results in disabling the device in the
1031 * higher layer for LLDDs without new reset/EH callbacks.
1032 *
1033 * Kill the following code as soon as those drivers are fixed.
1034 */
1035 if (ap->flags & ATA_FLAG_PORT_DISABLED) {
1036 err_mask |= AC_ERR_SYSTEM;
1037 ata_port_probe(ap);
1038 }
1039
Tejun Heo77853bf2006-01-23 13:09:36 +09001040 return err_mask;
Jeff Garzik64f043d2005-11-17 10:50:01 -05001041}
1042
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043/**
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001044 * ata_pio_need_iordy - check if iordy needed
1045 * @adev: ATA device
1046 *
1047 * Check if the current speed of the device requires IORDY. Used
1048 * by various controllers for chip configuration.
1049 */
1050
1051unsigned int ata_pio_need_iordy(const struct ata_device *adev)
1052{
1053 int pio;
1054 int speed = adev->pio_mode - XFER_PIO_0;
1055
1056 if (speed < 2)
1057 return 0;
1058 if (speed > 2)
1059 return 1;
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001060
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001061 /* If we have no drive specific rule, then PIO 2 is non IORDY */
1062
1063 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
1064 pio = adev->id[ATA_ID_EIDE_PIO];
1065 /* Is the speed faster than the drive allows non IORDY ? */
1066 if (pio) {
1067 /* This is cycle times not frequency - watch the logic! */
1068 if (pio > 240) /* PIO2 is 240nS per cycle */
1069 return 1;
1070 return 0;
1071 }
1072 }
1073 return 0;
1074}
1075
1076/**
Tejun Heo49016ac2006-02-21 02:12:11 +09001077 * ata_dev_read_id - Read ID data from the specified device
1078 * @ap: port on which target device resides
1079 * @dev: target device
1080 * @p_class: pointer to class of the target device (may be changed)
1081 * @post_reset: is this read ID post-reset?
Tejun Heod9572b12006-03-01 16:09:35 +09001082 * @p_id: read IDENTIFY page (newly allocated)
Tejun Heo49016ac2006-02-21 02:12:11 +09001083 *
1084 * Read ID data from the specified device. ATA_CMD_ID_ATA is
1085 * performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
Tejun Heoaec5c3c2006-03-25 01:33:34 +09001086 * devices. This function also issues ATA_CMD_INIT_DEV_PARAMS
1087 * for pre-ATA4 drives.
Tejun Heo49016ac2006-02-21 02:12:11 +09001088 *
1089 * LOCKING:
1090 * Kernel thread context (may sleep)
1091 *
1092 * RETURNS:
1093 * 0 on success, -errno otherwise.
1094 */
1095static int ata_dev_read_id(struct ata_port *ap, struct ata_device *dev,
Tejun Heod9572b12006-03-01 16:09:35 +09001096 unsigned int *p_class, int post_reset, u16 **p_id)
Tejun Heo49016ac2006-02-21 02:12:11 +09001097{
1098 unsigned int class = *p_class;
Tejun Heo49016ac2006-02-21 02:12:11 +09001099 struct ata_taskfile tf;
1100 unsigned int err_mask = 0;
Tejun Heod9572b12006-03-01 16:09:35 +09001101 u16 *id;
Tejun Heo49016ac2006-02-21 02:12:11 +09001102 const char *reason;
1103 int rc;
1104
1105 DPRINTK("ENTER, host %u, dev %u\n", ap->id, dev->devno);
1106
Tejun Heo49016ac2006-02-21 02:12:11 +09001107 ata_dev_select(ap, dev->devno, 1, 1); /* select device 0/1 */
1108
Tejun Heod9572b12006-03-01 16:09:35 +09001109 id = kmalloc(sizeof(id[0]) * ATA_ID_WORDS, GFP_KERNEL);
1110 if (id == NULL) {
1111 rc = -ENOMEM;
1112 reason = "out of memory";
1113 goto err_out;
1114 }
1115
Tejun Heo49016ac2006-02-21 02:12:11 +09001116 retry:
1117 ata_tf_init(ap, &tf, dev->devno);
1118
1119 switch (class) {
1120 case ATA_DEV_ATA:
1121 tf.command = ATA_CMD_ID_ATA;
1122 break;
1123 case ATA_DEV_ATAPI:
1124 tf.command = ATA_CMD_ID_ATAPI;
1125 break;
1126 default:
1127 rc = -ENODEV;
1128 reason = "unsupported class";
1129 goto err_out;
1130 }
1131
1132 tf.protocol = ATA_PROT_PIO;
1133
1134 err_mask = ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE,
1135 id, sizeof(id[0]) * ATA_ID_WORDS);
Tejun Heo49016ac2006-02-21 02:12:11 +09001136 if (err_mask) {
1137 rc = -EIO;
1138 reason = "I/O error";
Tejun Heo49016ac2006-02-21 02:12:11 +09001139 goto err_out;
1140 }
1141
1142 swap_buf_le16(id, ATA_ID_WORDS);
1143
Tejun Heo49016ac2006-02-21 02:12:11 +09001144 /* sanity check */
Alan Cox692785e2006-03-27 18:49:19 +01001145 if ((class == ATA_DEV_ATA) != (ata_id_is_ata(id) | ata_id_is_cfa(id))) {
Tejun Heo49016ac2006-02-21 02:12:11 +09001146 rc = -EINVAL;
1147 reason = "device reports illegal type";
1148 goto err_out;
1149 }
1150
1151 if (post_reset && class == ATA_DEV_ATA) {
1152 /*
1153 * The exact sequence expected by certain pre-ATA4 drives is:
1154 * SRST RESET
1155 * IDENTIFY
1156 * INITIALIZE DEVICE PARAMETERS
1157 * anything else..
1158 * Some drives were very specific about that exact sequence.
1159 */
1160 if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
Albert Lee00b6f5e2006-03-27 16:39:18 +08001161 err_mask = ata_dev_init_params(ap, dev, id[3], id[6]);
Tejun Heo49016ac2006-02-21 02:12:11 +09001162 if (err_mask) {
1163 rc = -EIO;
1164 reason = "INIT_DEV_PARAMS failed";
1165 goto err_out;
1166 }
1167
1168 /* current CHS translation info (id[53-58]) might be
1169 * changed. reread the identify device info.
1170 */
1171 post_reset = 0;
1172 goto retry;
1173 }
1174 }
1175
1176 *p_class = class;
Tejun Heod9572b12006-03-01 16:09:35 +09001177 *p_id = id;
Tejun Heo49016ac2006-02-21 02:12:11 +09001178 return 0;
1179
1180 err_out:
1181 printk(KERN_WARNING "ata%u: dev %u failed to IDENTIFY (%s)\n",
1182 ap->id, dev->devno, reason);
Tejun Heod9572b12006-03-01 16:09:35 +09001183 kfree(id);
Tejun Heo49016ac2006-02-21 02:12:11 +09001184 return rc;
1185}
1186
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09001187static inline u8 ata_dev_knobble(const struct ata_port *ap,
1188 struct ata_device *dev)
1189{
1190 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
1191}
1192
Tejun Heo49016ac2006-02-21 02:12:11 +09001193/**
Tejun Heoffeae412006-03-01 16:09:35 +09001194 * ata_dev_configure - Configure the specified ATA/ATAPI device
1195 * @ap: Port on which target device resides
1196 * @dev: Target device to configure
Tejun Heo4c2d7212006-03-05 17:55:58 +09001197 * @print_info: Enable device info printout
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 *
Tejun Heoffeae412006-03-01 16:09:35 +09001199 * Configure @dev according to @dev->id. Generic and low-level
1200 * driver specific fixups are also applied.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 *
1202 * LOCKING:
Tejun Heoffeae412006-03-01 16:09:35 +09001203 * Kernel thread context (may sleep)
1204 *
1205 * RETURNS:
1206 * 0 on success, -errno otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 */
Tejun Heo4c2d7212006-03-05 17:55:58 +09001208static int ata_dev_configure(struct ata_port *ap, struct ata_device *dev,
1209 int print_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210{
Tejun Heo1148c3a2006-03-13 19:48:04 +09001211 const u16 *id = dev->id;
Tejun Heoff8854b2006-03-06 04:31:56 +09001212 unsigned int xfer_mask;
Tejun Heo6e7846e2006-02-12 23:32:58 +09001213 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
1215 if (!ata_dev_present(dev)) {
1216 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
Tejun Heoffeae412006-03-01 16:09:35 +09001217 ap->id, dev->devno);
1218 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 }
1220
Tejun Heoffeae412006-03-01 16:09:35 +09001221 DPRINTK("ENTER, host %u, dev %u\n", ap->id, dev->devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
Tejun Heoc39f5eb2006-03-13 19:51:19 +09001223 /* print device capabilities */
1224 if (print_info)
1225 printk(KERN_DEBUG "ata%u: dev %u cfg 49:%04x 82:%04x 83:%04x "
1226 "84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1227 ap->id, dev->devno, id[49], id[82], id[83],
1228 id[84], id[85], id[86], id[87], id[88]);
1229
Tejun Heo208a9932006-03-05 17:55:58 +09001230 /* initialize to-be-configured parameters */
1231 dev->flags = 0;
1232 dev->max_sectors = 0;
1233 dev->cdb_len = 0;
1234 dev->n_sectors = 0;
1235 dev->cylinders = 0;
1236 dev->heads = 0;
1237 dev->sectors = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
1239 /*
1240 * common ATA, ATAPI feature tests
1241 */
1242
Tejun Heoff8854b2006-03-06 04:31:56 +09001243 /* find max transfer mode; for printk only */
Tejun Heo1148c3a2006-03-13 19:48:04 +09001244 xfer_mask = ata_id_xfermask(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
Tejun Heo1148c3a2006-03-13 19:48:04 +09001246 ata_dump_id(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
1248 /* ATA-specific feature tests */
1249 if (dev->class == ATA_DEV_ATA) {
Tejun Heo1148c3a2006-03-13 19:48:04 +09001250 dev->n_sectors = ata_id_n_sectors(id);
Tejun Heo29407402006-02-12 22:47:04 +09001251
Tejun Heo1148c3a2006-03-13 19:48:04 +09001252 if (ata_id_has_lba(id)) {
Tejun Heo4c2d7212006-03-05 17:55:58 +09001253 const char *lba_desc;
Albert Lee8bf62ec2005-05-12 15:29:42 -04001254
Tejun Heo4c2d7212006-03-05 17:55:58 +09001255 lba_desc = "LBA";
1256 dev->flags |= ATA_DFLAG_LBA;
Tejun Heo1148c3a2006-03-13 19:48:04 +09001257 if (ata_id_has_lba48(id)) {
Albert Lee8bf62ec2005-05-12 15:29:42 -04001258 dev->flags |= ATA_DFLAG_LBA48;
Tejun Heo4c2d7212006-03-05 17:55:58 +09001259 lba_desc = "LBA48";
1260 }
Albert Lee8bf62ec2005-05-12 15:29:42 -04001261
1262 /* print device info to dmesg */
Tejun Heo4c2d7212006-03-05 17:55:58 +09001263 if (print_info)
1264 printk(KERN_INFO "ata%u: dev %u ATA-%d, "
1265 "max %s, %Lu sectors: %s\n",
1266 ap->id, dev->devno,
Tejun Heo1148c3a2006-03-13 19:48:04 +09001267 ata_id_major_version(id),
Tejun Heoff8854b2006-03-06 04:31:56 +09001268 ata_mode_string(xfer_mask),
Tejun Heo4c2d7212006-03-05 17:55:58 +09001269 (unsigned long long)dev->n_sectors,
1270 lba_desc);
Tejun Heoffeae412006-03-01 16:09:35 +09001271 } else {
Albert Lee8bf62ec2005-05-12 15:29:42 -04001272 /* CHS */
1273
1274 /* Default translation */
Tejun Heo1148c3a2006-03-13 19:48:04 +09001275 dev->cylinders = id[1];
1276 dev->heads = id[3];
1277 dev->sectors = id[6];
Albert Lee8bf62ec2005-05-12 15:29:42 -04001278
Tejun Heo1148c3a2006-03-13 19:48:04 +09001279 if (ata_id_current_chs_valid(id)) {
Albert Lee8bf62ec2005-05-12 15:29:42 -04001280 /* Current CHS translation is valid. */
Tejun Heo1148c3a2006-03-13 19:48:04 +09001281 dev->cylinders = id[54];
1282 dev->heads = id[55];
1283 dev->sectors = id[56];
Albert Lee8bf62ec2005-05-12 15:29:42 -04001284 }
1285
1286 /* print device info to dmesg */
Tejun Heo4c2d7212006-03-05 17:55:58 +09001287 if (print_info)
1288 printk(KERN_INFO "ata%u: dev %u ATA-%d, "
1289 "max %s, %Lu sectors: CHS %u/%u/%u\n",
1290 ap->id, dev->devno,
Tejun Heo1148c3a2006-03-13 19:48:04 +09001291 ata_id_major_version(id),
Tejun Heoff8854b2006-03-06 04:31:56 +09001292 ata_mode_string(xfer_mask),
Tejun Heo4c2d7212006-03-05 17:55:58 +09001293 (unsigned long long)dev->n_sectors,
1294 dev->cylinders, dev->heads, dev->sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 }
1296
Albert Lee07f6f7d2005-11-01 19:33:20 +08001297 if (dev->id[59] & 0x100) {
1298 dev->multi_count = dev->id[59] & 0xff;
1299 DPRINTK("ata%u: dev %u multi count %u\n",
Albert Lee999bb6f2006-03-25 18:07:48 +08001300 ap->id, dev->devno, dev->multi_count);
Albert Lee07f6f7d2005-11-01 19:33:20 +08001301 }
1302
Albert Lee13ee4622006-03-25 17:39:34 +08001303 dev->cdb_len = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 }
1305
1306 /* ATAPI-specific feature tests */
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05001307 else if (dev->class == ATA_DEV_ATAPI) {
Tejun Heo1148c3a2006-03-13 19:48:04 +09001308 rc = atapi_cdb_len(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
1310 printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
Tejun Heoffeae412006-03-01 16:09:35 +09001311 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 goto err_out_nosup;
1313 }
Tejun Heo6e7846e2006-02-12 23:32:58 +09001314 dev->cdb_len = (unsigned int) rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
Albert Lee312f7da2005-09-27 17:38:03 +08001316 if (ata_id_cdb_intr(dev->id))
1317 dev->flags |= ATA_DFLAG_CDB_INTR;
1318
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 /* print device info to dmesg */
Tejun Heo4c2d7212006-03-05 17:55:58 +09001320 if (print_info)
1321 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
Tejun Heoff8854b2006-03-06 04:31:56 +09001322 ap->id, dev->devno, ata_mode_string(xfer_mask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 }
1324
Tejun Heo6e7846e2006-02-12 23:32:58 +09001325 ap->host->max_cmd_len = 0;
1326 for (i = 0; i < ATA_MAX_DEVICES; i++)
1327 ap->host->max_cmd_len = max_t(unsigned int,
1328 ap->host->max_cmd_len,
1329 ap->device[i].cdb_len);
1330
Brad Campbell6f2f3812005-05-12 15:07:47 -04001331 /* limit bridge transfers to udma5, 200 sectors */
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09001332 if (ata_dev_knobble(ap, dev)) {
Tejun Heo4c2d7212006-03-05 17:55:58 +09001333 if (print_info)
1334 printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
1335 ap->id, dev->devno);
Tejun Heo5a529132006-03-24 14:07:50 +09001336 dev->udma_mask &= ATA_UDMA5;
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09001337 dev->max_sectors = ATA_MAX_SECTORS;
Brad Campbell6f2f3812005-05-12 15:07:47 -04001338 }
1339
1340 if (ap->ops->dev_config)
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09001341 ap->ops->dev_config(ap, dev);
1342
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
Tejun Heoffeae412006-03-01 16:09:35 +09001344 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
1346err_out_nosup:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 DPRINTK("EXIT, err\n");
Tejun Heoffeae412006-03-01 16:09:35 +09001348 return rc;
Brad Campbell6f2f3812005-05-12 15:07:47 -04001349}
1350
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351/**
1352 * ata_bus_probe - Reset and probe ATA bus
1353 * @ap: Bus to probe
1354 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001355 * Master ATA bus probing function. Initiates a hardware-dependent
1356 * bus reset, then attempts to identify any devices found on
1357 * the bus.
1358 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001360 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 *
1362 * RETURNS:
1363 * Zero on success, non-zero on error.
1364 */
1365
1366static int ata_bus_probe(struct ata_port *ap)
1367{
Tejun Heo28ca5c52006-03-01 16:09:36 +09001368 unsigned int classes[ATA_MAX_DEVICES];
1369 unsigned int i, rc, found = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370
Tejun Heo28ca5c52006-03-01 16:09:36 +09001371 ata_port_probe(ap);
1372
Tejun Heo20444702006-03-13 01:57:01 +09001373 /* reset and determine device classes */
1374 for (i = 0; i < ATA_MAX_DEVICES; i++)
1375 classes[i] = ATA_DEV_UNKNOWN;
Tejun Heoc19ba8a2006-01-24 17:05:22 +09001376
Tejun Heo20444702006-03-13 01:57:01 +09001377 if (ap->ops->probe_reset) {
Tejun Heoc19ba8a2006-01-24 17:05:22 +09001378 rc = ap->ops->probe_reset(ap, classes);
Tejun Heo28ca5c52006-03-01 16:09:36 +09001379 if (rc) {
1380 printk("ata%u: reset failed (errno=%d)\n", ap->id, rc);
1381 return rc;
Tejun Heoc19ba8a2006-01-24 17:05:22 +09001382 }
Tejun Heo28ca5c52006-03-01 16:09:36 +09001383 } else {
Tejun Heoc19ba8a2006-01-24 17:05:22 +09001384 ap->ops->phy_reset(ap);
1385
Tejun Heo20444702006-03-13 01:57:01 +09001386 if (!(ap->flags & ATA_FLAG_PORT_DISABLED))
1387 for (i = 0; i < ATA_MAX_DEVICES; i++)
Tejun Heo28ca5c52006-03-01 16:09:36 +09001388 classes[i] = ap->device[i].class;
Tejun Heo20444702006-03-13 01:57:01 +09001389
Tejun Heo28ca5c52006-03-01 16:09:36 +09001390 ata_port_probe(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 }
1392
Tejun Heo20444702006-03-13 01:57:01 +09001393 for (i = 0; i < ATA_MAX_DEVICES; i++)
1394 if (classes[i] == ATA_DEV_UNKNOWN)
1395 classes[i] = ATA_DEV_NONE;
1396
Tejun Heo28ca5c52006-03-01 16:09:36 +09001397 /* read IDENTIFY page and configure devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001398 for (i = 0; i < ATA_MAX_DEVICES; i++) {
Tejun Heoffeae412006-03-01 16:09:35 +09001399 struct ata_device *dev = &ap->device[i];
1400
Tejun Heo28ca5c52006-03-01 16:09:36 +09001401 dev->class = classes[i];
1402
Tejun Heoffeae412006-03-01 16:09:35 +09001403 if (!ata_dev_present(dev))
1404 continue;
1405
1406 WARN_ON(dev->id != NULL);
1407 if (ata_dev_read_id(ap, dev, &dev->class, 1, &dev->id)) {
1408 dev->class = ATA_DEV_NONE;
1409 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 }
Tejun Heoffeae412006-03-01 16:09:35 +09001411
Tejun Heo4c2d7212006-03-05 17:55:58 +09001412 if (ata_dev_configure(ap, dev, 1)) {
Tejun Heofcef9782006-03-24 15:25:31 +09001413 ata_dev_disable(ap, dev);
Tejun Heoffeae412006-03-01 16:09:35 +09001414 continue;
1415 }
1416
Tejun Heoffeae412006-03-01 16:09:35 +09001417 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 }
1419
Tejun Heo28ca5c52006-03-01 16:09:36 +09001420 if (!found)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 goto err_out_disable;
1422
Alan Coxe35a9e02006-03-27 18:46:37 +01001423 if (ap->ops->set_mode)
1424 ap->ops->set_mode(ap);
1425 else
1426 ata_set_mode(ap);
1427
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1429 goto err_out_disable;
1430
1431 return 0;
1432
1433err_out_disable:
1434 ap->ops->port_disable(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 return -1;
1436}
1437
1438/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001439 * ata_port_probe - Mark port as enabled
1440 * @ap: Port for which we indicate enablement
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001442 * Modify @ap data structure such that the system
1443 * thinks that the entire port is enabled.
1444 *
1445 * LOCKING: host_set lock, or some other form of
1446 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 */
1448
1449void ata_port_probe(struct ata_port *ap)
1450{
1451 ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1452}
1453
1454/**
Tejun Heo3be680b2005-12-19 22:35:02 +09001455 * sata_print_link_status - Print SATA link status
1456 * @ap: SATA port to printk link status about
1457 *
1458 * This function prints link speed and status of a SATA link.
1459 *
1460 * LOCKING:
1461 * None.
1462 */
1463static void sata_print_link_status(struct ata_port *ap)
1464{
1465 u32 sstatus, tmp;
1466 const char *speed;
1467
1468 if (!ap->ops->scr_read)
1469 return;
1470
1471 sstatus = scr_read(ap, SCR_STATUS);
1472
1473 if (sata_dev_present(ap)) {
1474 tmp = (sstatus >> 4) & 0xf;
1475 if (tmp & (1 << 0))
1476 speed = "1.5";
1477 else if (tmp & (1 << 1))
1478 speed = "3.0";
1479 else
1480 speed = "<unknown>";
1481 printk(KERN_INFO "ata%u: SATA link up %s Gbps (SStatus %X)\n",
1482 ap->id, speed, sstatus);
1483 } else {
1484 printk(KERN_INFO "ata%u: SATA link down (SStatus %X)\n",
1485 ap->id, sstatus);
1486 }
1487}
1488
1489/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001490 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1491 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001493 * This function issues commands to standard SATA Sxxx
1494 * PHY registers, to wake up the phy (and device), and
1495 * clear any reset condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496 *
1497 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001498 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 *
1500 */
1501void __sata_phy_reset(struct ata_port *ap)
1502{
1503 u32 sstatus;
1504 unsigned long timeout = jiffies + (HZ * 5);
1505
1506 if (ap->flags & ATA_FLAG_SATA_RESET) {
Brett Russcdcca892005-03-28 15:10:27 -05001507 /* issue phy wake/reset */
1508 scr_write_flush(ap, SCR_CONTROL, 0x301);
Tejun Heo62ba2842005-06-26 23:27:19 +09001509 /* Couldn't find anything in SATA I/II specs, but
1510 * AHCI-1.1 10.4.2 says at least 1 ms. */
1511 mdelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 }
Brett Russcdcca892005-03-28 15:10:27 -05001513 scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
1515 /* wait for phy to become ready, if necessary */
1516 do {
1517 msleep(200);
1518 sstatus = scr_read(ap, SCR_STATUS);
1519 if ((sstatus & 0xf) != 1)
1520 break;
1521 } while (time_before(jiffies, timeout));
1522
Tejun Heo3be680b2005-12-19 22:35:02 +09001523 /* print link status */
1524 sata_print_link_status(ap);
Jeff Garzik656563e2005-11-20 03:36:45 -05001525
Tejun Heo3be680b2005-12-19 22:35:02 +09001526 /* TODO: phy layer with polling, timeouts, etc. */
1527 if (sata_dev_present(ap))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 ata_port_probe(ap);
Tejun Heo3be680b2005-12-19 22:35:02 +09001529 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 ata_port_disable(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531
1532 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1533 return;
1534
1535 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1536 ata_port_disable(ap);
1537 return;
1538 }
1539
1540 ap->cbl = ATA_CBL_SATA;
1541}
1542
1543/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001544 * sata_phy_reset - Reset SATA bus.
1545 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001547 * This function resets the SATA bus, and then probes
1548 * the bus for devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 *
1550 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001551 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 *
1553 */
1554void sata_phy_reset(struct ata_port *ap)
1555{
1556 __sata_phy_reset(ap);
1557 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1558 return;
1559 ata_bus_reset(ap);
1560}
1561
1562/**
Alan Coxebdfca62006-03-23 15:38:34 +00001563 * ata_dev_pair - return other device on cable
1564 * @ap: port
1565 * @adev: device
1566 *
1567 * Obtain the other device on the same cable, or if none is
1568 * present NULL is returned
1569 */
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001570
Alan Coxebdfca62006-03-23 15:38:34 +00001571struct ata_device *ata_dev_pair(struct ata_port *ap, struct ata_device *adev)
1572{
1573 struct ata_device *pair = &ap->device[1 - adev->devno];
1574 if (!ata_dev_present(pair))
1575 return NULL;
1576 return pair;
1577}
1578
1579/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001580 * ata_port_disable - Disable port.
1581 * @ap: Port to be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001583 * Modify @ap data structure such that the system
1584 * thinks that the entire port is disabled, and should
1585 * never attempt to probe or communicate with devices
1586 * on this port.
1587 *
1588 * LOCKING: host_set lock, or some other form of
1589 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 */
1591
1592void ata_port_disable(struct ata_port *ap)
1593{
1594 ap->device[0].class = ATA_DEV_NONE;
1595 ap->device[1].class = ATA_DEV_NONE;
1596 ap->flags |= ATA_FLAG_PORT_DISABLED;
1597}
1598
Alan Cox452503f2005-10-21 19:01:32 -04001599/*
1600 * This mode timing computation functionality is ported over from
1601 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
1602 */
1603/*
1604 * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
1605 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
1606 * for PIO 5, which is a nonstandard extension and UDMA6, which
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001607 * is currently supported only by Maxtor drives.
Alan Cox452503f2005-10-21 19:01:32 -04001608 */
1609
1610static const struct ata_timing ata_timing[] = {
1611
1612 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
1613 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
1614 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
1615 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
1616
1617 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
1618 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
1619 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
1620
1621/* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001622
Alan Cox452503f2005-10-21 19:01:32 -04001623 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
1624 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
1625 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001626
Alan Cox452503f2005-10-21 19:01:32 -04001627 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
1628 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
1629 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
1630
1631/* { XFER_PIO_5, 20, 50, 30, 100, 50, 30, 100, 0 }, */
1632 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
1633 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
1634
1635 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
1636 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
1637 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
1638
1639/* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
1640
1641 { 0xFF }
1642};
1643
1644#define ENOUGH(v,unit) (((v)-1)/(unit)+1)
1645#define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
1646
1647static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
1648{
1649 q->setup = EZ(t->setup * 1000, T);
1650 q->act8b = EZ(t->act8b * 1000, T);
1651 q->rec8b = EZ(t->rec8b * 1000, T);
1652 q->cyc8b = EZ(t->cyc8b * 1000, T);
1653 q->active = EZ(t->active * 1000, T);
1654 q->recover = EZ(t->recover * 1000, T);
1655 q->cycle = EZ(t->cycle * 1000, T);
1656 q->udma = EZ(t->udma * 1000, UT);
1657}
1658
1659void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
1660 struct ata_timing *m, unsigned int what)
1661{
1662 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
1663 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
1664 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
1665 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
1666 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
1667 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
1668 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
1669 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
1670}
1671
1672static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
1673{
1674 const struct ata_timing *t;
1675
1676 for (t = ata_timing; t->mode != speed; t++)
Alan Cox91190752005-10-26 12:17:46 -04001677 if (t->mode == 0xFF)
Alan Cox452503f2005-10-21 19:01:32 -04001678 return NULL;
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001679 return t;
Alan Cox452503f2005-10-21 19:01:32 -04001680}
1681
1682int ata_timing_compute(struct ata_device *adev, unsigned short speed,
1683 struct ata_timing *t, int T, int UT)
1684{
1685 const struct ata_timing *s;
1686 struct ata_timing p;
1687
1688 /*
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001689 * Find the mode.
Albert Lee75b1f2f2005-11-16 17:06:18 +08001690 */
Alan Cox452503f2005-10-21 19:01:32 -04001691
1692 if (!(s = ata_timing_find_mode(speed)))
1693 return -EINVAL;
1694
Albert Lee75b1f2f2005-11-16 17:06:18 +08001695 memcpy(t, s, sizeof(*s));
1696
Alan Cox452503f2005-10-21 19:01:32 -04001697 /*
1698 * If the drive is an EIDE drive, it can tell us it needs extended
1699 * PIO/MW_DMA cycle timing.
1700 */
1701
1702 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
1703 memset(&p, 0, sizeof(p));
1704 if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
1705 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
1706 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
1707 } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
1708 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
1709 }
1710 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
1711 }
1712
1713 /*
1714 * Convert the timing to bus clock counts.
1715 */
1716
Albert Lee75b1f2f2005-11-16 17:06:18 +08001717 ata_timing_quantize(t, t, T, UT);
Alan Cox452503f2005-10-21 19:01:32 -04001718
1719 /*
Randy Dunlapc893a3a2006-01-28 13:15:32 -05001720 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
1721 * S.M.A.R.T * and some other commands. We have to ensure that the
1722 * DMA cycle timing is slower/equal than the fastest PIO timing.
Alan Cox452503f2005-10-21 19:01:32 -04001723 */
1724
1725 if (speed > XFER_PIO_4) {
1726 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
1727 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
1728 }
1729
1730 /*
Randy Dunlapc893a3a2006-01-28 13:15:32 -05001731 * Lengthen active & recovery time so that cycle time is correct.
Alan Cox452503f2005-10-21 19:01:32 -04001732 */
1733
1734 if (t->act8b + t->rec8b < t->cyc8b) {
1735 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
1736 t->rec8b = t->cyc8b - t->act8b;
1737 }
1738
1739 if (t->active + t->recover < t->cycle) {
1740 t->active += (t->cycle - (t->active + t->recover)) / 2;
1741 t->recover = t->cycle - t->active;
1742 }
1743
1744 return 0;
1745}
1746
Tejun Heo83206a22006-03-24 15:25:31 +09001747static int ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748{
Tejun Heo83206a22006-03-24 15:25:31 +09001749 unsigned int err_mask;
1750 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
1752 if (dev->xfer_shift == ATA_SHIFT_PIO)
1753 dev->flags |= ATA_DFLAG_PIO;
1754
Tejun Heo83206a22006-03-24 15:25:31 +09001755 err_mask = ata_dev_set_xfermode(ap, dev);
1756 if (err_mask) {
1757 printk(KERN_ERR
1758 "ata%u: failed to set xfermode (err_mask=0x%x)\n",
1759 ap->id, err_mask);
1760 return -EIO;
1761 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
Tejun Heo83206a22006-03-24 15:25:31 +09001763 rc = ata_dev_revalidate(ap, dev, 0);
1764 if (rc) {
1765 printk(KERN_ERR
1766 "ata%u: failed to revalidate after set xfermode\n",
1767 ap->id);
1768 return rc;
Tejun Heo48a8a142006-03-05 17:55:58 +09001769 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
Tejun Heo23e71c32006-03-06 04:31:57 +09001771 DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n",
1772 dev->xfer_shift, (int)dev->xfer_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773
1774 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
Tejun Heo23e71c32006-03-06 04:31:57 +09001775 ap->id, dev->devno,
1776 ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)));
Tejun Heo83206a22006-03-24 15:25:31 +09001777 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778}
1779
1780static int ata_host_set_pio(struct ata_port *ap)
1781{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 int i;
1783
1784 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1785 struct ata_device *dev = &ap->device[i];
Tejun Heoa6d5a512006-03-06 04:31:57 +09001786
1787 if (!ata_dev_present(dev))
1788 continue;
1789
1790 if (!dev->pio_mode) {
Alan Cox88f93a32006-03-21 16:04:12 +00001791 printk(KERN_WARNING "ata%u: no PIO support for device %d.\n", ap->id, i);
Tejun Heoa6d5a512006-03-06 04:31:57 +09001792 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 }
Tejun Heoa6d5a512006-03-06 04:31:57 +09001794
1795 dev->xfer_mode = dev->pio_mode;
1796 dev->xfer_shift = ATA_SHIFT_PIO;
1797 if (ap->ops->set_piomode)
1798 ap->ops->set_piomode(ap, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 }
1800
1801 return 0;
1802}
1803
Tejun Heoa6d5a512006-03-06 04:31:57 +09001804static void ata_host_set_dma(struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805{
1806 int i;
1807
1808 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1809 struct ata_device *dev = &ap->device[i];
Tejun Heoa6d5a512006-03-06 04:31:57 +09001810
1811 if (!ata_dev_present(dev) || !dev->dma_mode)
1812 continue;
1813
1814 dev->xfer_mode = dev->dma_mode;
1815 dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
1816 if (ap->ops->set_dmamode)
1817 ap->ops->set_dmamode(ap, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 }
1819}
1820
1821/**
1822 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1823 * @ap: port on which timings will be programmed
1824 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001825 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.).
1826 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001828 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 */
1830static void ata_set_mode(struct ata_port *ap)
1831{
Alan Cox5444a6f2006-03-27 18:58:20 +01001832 int i, rc, used_dma = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833
Tejun Heoa6d5a512006-03-06 04:31:57 +09001834 /* step 1: calculate xfer_mask */
1835 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1836 struct ata_device *dev = &ap->device[i];
Tejun Heoacf356b2006-03-24 14:07:50 +09001837 unsigned int pio_mask, dma_mask;
Tejun Heoa6d5a512006-03-06 04:31:57 +09001838
1839 if (!ata_dev_present(dev))
1840 continue;
1841
Tejun Heoacf356b2006-03-24 14:07:50 +09001842 ata_dev_xfermask(ap, dev);
Tejun Heoa6d5a512006-03-06 04:31:57 +09001843
Tejun Heoacf356b2006-03-24 14:07:50 +09001844 /* TODO: let LLDD filter dev->*_mask here */
1845
1846 pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0);
1847 dma_mask = ata_pack_xfermask(0, dev->mwdma_mask, dev->udma_mask);
1848 dev->pio_mode = ata_xfer_mask2mode(pio_mask);
1849 dev->dma_mode = ata_xfer_mask2mode(dma_mask);
Alan Cox5444a6f2006-03-27 18:58:20 +01001850
1851 if (dev->dma_mode)
1852 used_dma = 1;
Tejun Heoa6d5a512006-03-06 04:31:57 +09001853 }
1854
1855 /* step 2: always set host PIO timings */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 rc = ata_host_set_pio(ap);
1857 if (rc)
1858 goto err_out;
1859
Tejun Heoa6d5a512006-03-06 04:31:57 +09001860 /* step 3: set host DMA timings */
1861 ata_host_set_dma(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
1863 /* step 4: update devices' xfer mode */
Tejun Heo83206a22006-03-24 15:25:31 +09001864 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1865 struct ata_device *dev = &ap->device[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
Tejun Heo83206a22006-03-24 15:25:31 +09001867 if (!ata_dev_present(dev))
1868 continue;
1869
1870 if (ata_dev_set_mode(ap, dev))
1871 goto err_out;
1872 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873
Alan Cox5444a6f2006-03-27 18:58:20 +01001874 /*
1875 * Record simplex status. If we selected DMA then the other
1876 * host channels are not permitted to do so.
1877 */
1878
1879 if (used_dma && (ap->host_set->flags & ATA_HOST_SIMPLEX))
1880 ap->host_set->simplex_claimed = 1;
1881
1882 /*
1883 * Chip specific finalisation
1884 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 if (ap->ops->post_set_mode)
1886 ap->ops->post_set_mode(ap);
1887
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 return;
1889
1890err_out:
1891 ata_port_disable(ap);
1892}
1893
1894/**
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05001895 * ata_tf_to_host - issue ATA taskfile to host controller
1896 * @ap: port to which command is being issued
1897 * @tf: ATA taskfile register set
1898 *
1899 * Issues ATA taskfile register set to ATA host controller,
1900 * with proper synchronization with interrupt handler and
1901 * other threads.
1902 *
1903 * LOCKING:
1904 * spin_lock_irqsave(host_set lock)
1905 */
1906
1907static inline void ata_tf_to_host(struct ata_port *ap,
1908 const struct ata_taskfile *tf)
1909{
1910 ap->ops->tf_load(ap, tf);
1911 ap->ops->exec_command(ap, tf);
1912}
1913
1914/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 * ata_busy_sleep - sleep until BSY clears, or timeout
1916 * @ap: port containing status register to be polled
1917 * @tmout_pat: impatience timeout
1918 * @tmout: overall timeout
1919 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001920 * Sleep until ATA Status register bit BSY clears,
1921 * or a timeout occurs.
1922 *
1923 * LOCKING: None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 */
1925
Tejun Heo6f8b9952006-01-24 17:05:21 +09001926unsigned int ata_busy_sleep (struct ata_port *ap,
1927 unsigned long tmout_pat, unsigned long tmout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928{
1929 unsigned long timer_start, timeout;
1930 u8 status;
1931
1932 status = ata_busy_wait(ap, ATA_BUSY, 300);
1933 timer_start = jiffies;
1934 timeout = timer_start + tmout_pat;
1935 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1936 msleep(50);
1937 status = ata_busy_wait(ap, ATA_BUSY, 3);
1938 }
1939
1940 if (status & ATA_BUSY)
1941 printk(KERN_WARNING "ata%u is slow to respond, "
1942 "please be patient\n", ap->id);
1943
1944 timeout = timer_start + tmout;
1945 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
1946 msleep(50);
1947 status = ata_chk_status(ap);
1948 }
1949
1950 if (status & ATA_BUSY) {
1951 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
1952 ap->id, tmout / HZ);
1953 return 1;
1954 }
1955
1956 return 0;
1957}
1958
1959static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
1960{
1961 struct ata_ioports *ioaddr = &ap->ioaddr;
1962 unsigned int dev0 = devmask & (1 << 0);
1963 unsigned int dev1 = devmask & (1 << 1);
1964 unsigned long timeout;
1965
1966 /* if device 0 was found in ata_devchk, wait for its
1967 * BSY bit to clear
1968 */
1969 if (dev0)
1970 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1971
1972 /* if device 1 was found in ata_devchk, wait for
1973 * register access, then wait for BSY to clear
1974 */
1975 timeout = jiffies + ATA_TMOUT_BOOT;
1976 while (dev1) {
1977 u8 nsect, lbal;
1978
1979 ap->ops->dev_select(ap, 1);
1980 if (ap->flags & ATA_FLAG_MMIO) {
1981 nsect = readb((void __iomem *) ioaddr->nsect_addr);
1982 lbal = readb((void __iomem *) ioaddr->lbal_addr);
1983 } else {
1984 nsect = inb(ioaddr->nsect_addr);
1985 lbal = inb(ioaddr->lbal_addr);
1986 }
1987 if ((nsect == 1) && (lbal == 1))
1988 break;
1989 if (time_after(jiffies, timeout)) {
1990 dev1 = 0;
1991 break;
1992 }
1993 msleep(50); /* give drive a breather */
1994 }
1995 if (dev1)
1996 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
1997
1998 /* is all this really necessary? */
1999 ap->ops->dev_select(ap, 0);
2000 if (dev1)
2001 ap->ops->dev_select(ap, 1);
2002 if (dev0)
2003 ap->ops->dev_select(ap, 0);
2004}
2005
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006static unsigned int ata_bus_softreset(struct ata_port *ap,
2007 unsigned int devmask)
2008{
2009 struct ata_ioports *ioaddr = &ap->ioaddr;
2010
2011 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
2012
2013 /* software reset. causes dev0 to be selected */
2014 if (ap->flags & ATA_FLAG_MMIO) {
2015 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2016 udelay(20); /* FIXME: flush */
2017 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
2018 udelay(20); /* FIXME: flush */
2019 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2020 } else {
2021 outb(ap->ctl, ioaddr->ctl_addr);
2022 udelay(10);
2023 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
2024 udelay(10);
2025 outb(ap->ctl, ioaddr->ctl_addr);
2026 }
2027
2028 /* spec mandates ">= 2ms" before checking status.
2029 * We wait 150ms, because that was the magic delay used for
2030 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
2031 * between when the ATA command register is written, and then
2032 * status is checked. Because waiting for "a while" before
2033 * checking status is fine, post SRST, we perform this magic
2034 * delay here as well.
Alan Cox09c7ad72006-03-22 15:52:40 +00002035 *
2036 * Old drivers/ide uses the 2mS rule and then waits for ready
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 */
2038 msleep(150);
2039
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002040 /* Before we perform post reset processing we want to see if
Tejun Heo298a41c2006-03-25 02:58:13 +09002041 * the bus shows 0xFF because the odd clown forgets the D7
2042 * pulldown resistor.
2043 */
Alan Cox09c7ad72006-03-22 15:52:40 +00002044 if (ata_check_status(ap) == 0xFF)
Tejun Heo298a41c2006-03-25 02:58:13 +09002045 return AC_ERR_OTHER;
Alan Cox09c7ad72006-03-22 15:52:40 +00002046
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 ata_bus_post_reset(ap, devmask);
2048
2049 return 0;
2050}
2051
2052/**
2053 * ata_bus_reset - reset host port and associated ATA channel
2054 * @ap: port to reset
2055 *
2056 * This is typically the first time we actually start issuing
2057 * commands to the ATA channel. We wait for BSY to clear, then
2058 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
2059 * result. Determine what devices, if any, are on the channel
2060 * by looking at the device 0/1 error register. Look at the signature
2061 * stored in each device's taskfile registers, to determine if
2062 * the device is ATA or ATAPI.
2063 *
2064 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002065 * PCI/etc. bus probe sem.
2066 * Obtains host_set lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 *
2068 * SIDE EFFECTS:
2069 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
2070 */
2071
2072void ata_bus_reset(struct ata_port *ap)
2073{
2074 struct ata_ioports *ioaddr = &ap->ioaddr;
2075 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2076 u8 err;
Tejun Heoaec5c3c2006-03-25 01:33:34 +09002077 unsigned int dev0, dev1 = 0, devmask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078
2079 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
2080
2081 /* determine if device 0/1 are present */
2082 if (ap->flags & ATA_FLAG_SATA_RESET)
2083 dev0 = 1;
2084 else {
2085 dev0 = ata_devchk(ap, 0);
2086 if (slave_possible)
2087 dev1 = ata_devchk(ap, 1);
2088 }
2089
2090 if (dev0)
2091 devmask |= (1 << 0);
2092 if (dev1)
2093 devmask |= (1 << 1);
2094
2095 /* select device 0 again */
2096 ap->ops->dev_select(ap, 0);
2097
2098 /* issue bus reset */
2099 if (ap->flags & ATA_FLAG_SRST)
Tejun Heoaec5c3c2006-03-25 01:33:34 +09002100 if (ata_bus_softreset(ap, devmask))
2101 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102
2103 /*
2104 * determine by signature whether we have ATA or ATAPI devices
2105 */
Tejun Heob4dc7622006-01-24 17:05:22 +09002106 ap->device[0].class = ata_dev_try_classify(ap, 0, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 if ((slave_possible) && (err != 0x81))
Tejun Heob4dc7622006-01-24 17:05:22 +09002108 ap->device[1].class = ata_dev_try_classify(ap, 1, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109
2110 /* re-enable interrupts */
2111 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2112 ata_irq_on(ap);
2113
2114 /* is double-select really necessary? */
2115 if (ap->device[1].class != ATA_DEV_NONE)
2116 ap->ops->dev_select(ap, 1);
2117 if (ap->device[0].class != ATA_DEV_NONE)
2118 ap->ops->dev_select(ap, 0);
2119
2120 /* if no devices were detected, disable this port */
2121 if ((ap->device[0].class == ATA_DEV_NONE) &&
2122 (ap->device[1].class == ATA_DEV_NONE))
2123 goto err_out;
2124
2125 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
2126 /* set up device control for ATA_FLAG_SATA_RESET */
2127 if (ap->flags & ATA_FLAG_MMIO)
2128 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2129 else
2130 outb(ap->ctl, ioaddr->ctl_addr);
2131 }
2132
2133 DPRINTK("EXIT\n");
2134 return;
2135
2136err_out:
2137 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
2138 ap->ops->port_disable(ap);
2139
2140 DPRINTK("EXIT\n");
2141}
2142
Tejun Heo7a7921e2006-02-02 18:20:00 +09002143static int sata_phy_resume(struct ata_port *ap)
2144{
2145 unsigned long timeout = jiffies + (HZ * 5);
2146 u32 sstatus;
2147
2148 scr_write_flush(ap, SCR_CONTROL, 0x300);
2149
2150 /* Wait for phy to become ready, if necessary. */
2151 do {
2152 msleep(200);
2153 sstatus = scr_read(ap, SCR_STATUS);
2154 if ((sstatus & 0xf) != 1)
2155 return 0;
2156 } while (time_before(jiffies, timeout));
2157
2158 return -1;
2159}
2160
Tejun Heoc2bd5802006-01-24 17:05:22 +09002161/**
Tejun Heo8a19ac82006-02-02 18:20:00 +09002162 * ata_std_probeinit - initialize probing
2163 * @ap: port to be probed
2164 *
2165 * @ap is about to be probed. Initialize it. This function is
2166 * to be used as standard callback for ata_drive_probe_reset().
Tejun Heo3a397462006-02-10 23:58:48 +09002167 *
2168 * NOTE!!! Do not use this function as probeinit if a low level
2169 * driver implements only hardreset. Just pass NULL as probeinit
2170 * in that case. Using this function is probably okay but doing
2171 * so makes reset sequence different from the original
2172 * ->phy_reset implementation and Jeff nervous. :-P
Tejun Heo8a19ac82006-02-02 18:20:00 +09002173 */
Alan Cox17efc5f2006-03-27 19:01:32 +01002174void ata_std_probeinit(struct ata_port *ap)
Tejun Heo8a19ac82006-02-02 18:20:00 +09002175{
Alan Cox17efc5f2006-03-27 19:01:32 +01002176 if ((ap->flags & ATA_FLAG_SATA) && ap->ops->scr_read) {
Tejun Heo8a19ac82006-02-02 18:20:00 +09002177 sata_phy_resume(ap);
Tejun Heo3a397462006-02-10 23:58:48 +09002178 if (sata_dev_present(ap))
2179 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2180 }
Tejun Heo8a19ac82006-02-02 18:20:00 +09002181}
2182
2183/**
Tejun Heoc2bd5802006-01-24 17:05:22 +09002184 * ata_std_softreset - reset host port via ATA SRST
2185 * @ap: port to reset
2186 * @verbose: fail verbosely
2187 * @classes: resulting classes of attached devices
2188 *
2189 * Reset host port using ATA SRST. This function is to be used
2190 * as standard callback for ata_drive_*_reset() functions.
2191 *
2192 * LOCKING:
2193 * Kernel thread context (may sleep)
2194 *
2195 * RETURNS:
2196 * 0 on success, -errno otherwise.
2197 */
2198int ata_std_softreset(struct ata_port *ap, int verbose, unsigned int *classes)
2199{
2200 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2201 unsigned int devmask = 0, err_mask;
2202 u8 err;
2203
2204 DPRINTK("ENTER\n");
2205
Tejun Heo3a397462006-02-10 23:58:48 +09002206 if (ap->ops->scr_read && !sata_dev_present(ap)) {
2207 classes[0] = ATA_DEV_NONE;
2208 goto out;
2209 }
2210
Tejun Heoc2bd5802006-01-24 17:05:22 +09002211 /* determine if device 0/1 are present */
2212 if (ata_devchk(ap, 0))
2213 devmask |= (1 << 0);
2214 if (slave_possible && ata_devchk(ap, 1))
2215 devmask |= (1 << 1);
2216
Tejun Heoc2bd5802006-01-24 17:05:22 +09002217 /* select device 0 again */
2218 ap->ops->dev_select(ap, 0);
2219
2220 /* issue bus reset */
2221 DPRINTK("about to softreset, devmask=%x\n", devmask);
2222 err_mask = ata_bus_softreset(ap, devmask);
2223 if (err_mask) {
2224 if (verbose)
2225 printk(KERN_ERR "ata%u: SRST failed (err_mask=0x%x)\n",
2226 ap->id, err_mask);
2227 else
2228 DPRINTK("EXIT, softreset failed (err_mask=0x%x)\n",
2229 err_mask);
2230 return -EIO;
2231 }
2232
2233 /* determine by signature whether we have ATA or ATAPI devices */
2234 classes[0] = ata_dev_try_classify(ap, 0, &err);
2235 if (slave_possible && err != 0x81)
2236 classes[1] = ata_dev_try_classify(ap, 1, &err);
2237
Tejun Heo3a397462006-02-10 23:58:48 +09002238 out:
Tejun Heoc2bd5802006-01-24 17:05:22 +09002239 DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]);
2240 return 0;
2241}
2242
2243/**
2244 * sata_std_hardreset - reset host port via SATA phy reset
2245 * @ap: port to reset
2246 * @verbose: fail verbosely
2247 * @class: resulting class of attached device
2248 *
2249 * SATA phy-reset host port using DET bits of SControl register.
2250 * This function is to be used as standard callback for
2251 * ata_drive_*_reset().
2252 *
2253 * LOCKING:
2254 * Kernel thread context (may sleep)
2255 *
2256 * RETURNS:
2257 * 0 on success, -errno otherwise.
2258 */
2259int sata_std_hardreset(struct ata_port *ap, int verbose, unsigned int *class)
2260{
Tejun Heoc2bd5802006-01-24 17:05:22 +09002261 DPRINTK("ENTER\n");
2262
2263 /* Issue phy wake/reset */
2264 scr_write_flush(ap, SCR_CONTROL, 0x301);
2265
2266 /*
2267 * Couldn't find anything in SATA I/II specs, but AHCI-1.1
2268 * 10.4.2 says at least 1 ms.
2269 */
2270 msleep(1);
2271
Tejun Heo7a7921e2006-02-02 18:20:00 +09002272 /* Bring phy back */
2273 sata_phy_resume(ap);
Tejun Heoc2bd5802006-01-24 17:05:22 +09002274
Tejun Heoc2bd5802006-01-24 17:05:22 +09002275 /* TODO: phy layer with polling, timeouts, etc. */
2276 if (!sata_dev_present(ap)) {
2277 *class = ATA_DEV_NONE;
2278 DPRINTK("EXIT, link offline\n");
2279 return 0;
2280 }
2281
2282 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
2283 if (verbose)
2284 printk(KERN_ERR "ata%u: COMRESET failed "
2285 "(device not ready)\n", ap->id);
2286 else
2287 DPRINTK("EXIT, device not ready\n");
2288 return -EIO;
2289 }
2290
Tejun Heo3a397462006-02-10 23:58:48 +09002291 ap->ops->dev_select(ap, 0); /* probably unnecessary */
2292
Tejun Heoc2bd5802006-01-24 17:05:22 +09002293 *class = ata_dev_try_classify(ap, 0, NULL);
2294
2295 DPRINTK("EXIT, class=%u\n", *class);
2296 return 0;
2297}
2298
2299/**
2300 * ata_std_postreset - standard postreset callback
2301 * @ap: the target ata_port
2302 * @classes: classes of attached devices
2303 *
2304 * This function is invoked after a successful reset. Note that
2305 * the device might have been reset more than once using
2306 * different reset methods before postreset is invoked.
Tejun Heoc2bd5802006-01-24 17:05:22 +09002307 *
2308 * This function is to be used as standard callback for
2309 * ata_drive_*_reset().
2310 *
2311 * LOCKING:
2312 * Kernel thread context (may sleep)
2313 */
2314void ata_std_postreset(struct ata_port *ap, unsigned int *classes)
2315{
2316 DPRINTK("ENTER\n");
2317
Tejun Heo56497bd2006-02-15 15:01:42 +09002318 /* set cable type if it isn't already set */
Tejun Heoc2bd5802006-01-24 17:05:22 +09002319 if (ap->cbl == ATA_CBL_NONE && ap->flags & ATA_FLAG_SATA)
2320 ap->cbl = ATA_CBL_SATA;
2321
2322 /* print link status */
2323 if (ap->cbl == ATA_CBL_SATA)
2324 sata_print_link_status(ap);
2325
Tejun Heo3a397462006-02-10 23:58:48 +09002326 /* re-enable interrupts */
2327 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2328 ata_irq_on(ap);
Tejun Heoc2bd5802006-01-24 17:05:22 +09002329
2330 /* is double-select really necessary? */
2331 if (classes[0] != ATA_DEV_NONE)
2332 ap->ops->dev_select(ap, 1);
2333 if (classes[1] != ATA_DEV_NONE)
2334 ap->ops->dev_select(ap, 0);
2335
Tejun Heo3a397462006-02-10 23:58:48 +09002336 /* bail out if no device is present */
2337 if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
2338 DPRINTK("EXIT, no device\n");
2339 return;
2340 }
2341
2342 /* set up device control */
2343 if (ap->ioaddr.ctl_addr) {
2344 if (ap->flags & ATA_FLAG_MMIO)
2345 writeb(ap->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
2346 else
2347 outb(ap->ctl, ap->ioaddr.ctl_addr);
2348 }
Tejun Heoc2bd5802006-01-24 17:05:22 +09002349
2350 DPRINTK("EXIT\n");
2351}
2352
2353/**
2354 * ata_std_probe_reset - standard probe reset method
2355 * @ap: prot to perform probe-reset
2356 * @classes: resulting classes of attached devices
2357 *
2358 * The stock off-the-shelf ->probe_reset method.
2359 *
2360 * LOCKING:
2361 * Kernel thread context (may sleep)
2362 *
2363 * RETURNS:
2364 * 0 on success, -errno otherwise.
2365 */
2366int ata_std_probe_reset(struct ata_port *ap, unsigned int *classes)
2367{
2368 ata_reset_fn_t hardreset;
2369
2370 hardreset = NULL;
Tejun Heob911fc32006-02-02 18:20:00 +09002371 if (ap->flags & ATA_FLAG_SATA && ap->ops->scr_read)
Tejun Heoc2bd5802006-01-24 17:05:22 +09002372 hardreset = sata_std_hardreset;
2373
Tejun Heo8a19ac82006-02-02 18:20:00 +09002374 return ata_drive_probe_reset(ap, ata_std_probeinit,
Tejun Heo7944ea92006-02-02 18:20:00 +09002375 ata_std_softreset, hardreset,
Tejun Heoc2bd5802006-01-24 17:05:22 +09002376 ata_std_postreset, classes);
2377}
2378
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002379static int do_probe_reset(struct ata_port *ap, ata_reset_fn_t reset,
2380 ata_postreset_fn_t postreset,
2381 unsigned int *classes)
2382{
2383 int i, rc;
2384
2385 for (i = 0; i < ATA_MAX_DEVICES; i++)
2386 classes[i] = ATA_DEV_UNKNOWN;
2387
2388 rc = reset(ap, 0, classes);
2389 if (rc)
2390 return rc;
2391
2392 /* If any class isn't ATA_DEV_UNKNOWN, consider classification
2393 * is complete and convert all ATA_DEV_UNKNOWN to
2394 * ATA_DEV_NONE.
2395 */
2396 for (i = 0; i < ATA_MAX_DEVICES; i++)
2397 if (classes[i] != ATA_DEV_UNKNOWN)
2398 break;
2399
2400 if (i < ATA_MAX_DEVICES)
2401 for (i = 0; i < ATA_MAX_DEVICES; i++)
2402 if (classes[i] == ATA_DEV_UNKNOWN)
2403 classes[i] = ATA_DEV_NONE;
2404
2405 if (postreset)
2406 postreset(ap, classes);
2407
2408 return classes[0] != ATA_DEV_UNKNOWN ? 0 : -ENODEV;
2409}
2410
2411/**
2412 * ata_drive_probe_reset - Perform probe reset with given methods
2413 * @ap: port to reset
Tejun Heo7944ea92006-02-02 18:20:00 +09002414 * @probeinit: probeinit method (can be NULL)
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002415 * @softreset: softreset method (can be NULL)
2416 * @hardreset: hardreset method (can be NULL)
2417 * @postreset: postreset method (can be NULL)
2418 * @classes: resulting classes of attached devices
2419 *
2420 * Reset the specified port and classify attached devices using
2421 * given methods. This function prefers softreset but tries all
2422 * possible reset sequences to reset and classify devices. This
2423 * function is intended to be used for constructing ->probe_reset
2424 * callback by low level drivers.
2425 *
2426 * Reset methods should follow the following rules.
2427 *
2428 * - Return 0 on sucess, -errno on failure.
2429 * - If classification is supported, fill classes[] with
2430 * recognized class codes.
2431 * - If classification is not supported, leave classes[] alone.
2432 * - If verbose is non-zero, print error message on failure;
2433 * otherwise, shut up.
2434 *
2435 * LOCKING:
2436 * Kernel thread context (may sleep)
2437 *
2438 * RETURNS:
2439 * 0 on success, -EINVAL if no reset method is avaliable, -ENODEV
2440 * if classification fails, and any error code from reset
2441 * methods.
2442 */
Tejun Heo7944ea92006-02-02 18:20:00 +09002443int ata_drive_probe_reset(struct ata_port *ap, ata_probeinit_fn_t probeinit,
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002444 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2445 ata_postreset_fn_t postreset, unsigned int *classes)
2446{
2447 int rc = -EINVAL;
2448
Tejun Heo7944ea92006-02-02 18:20:00 +09002449 if (probeinit)
2450 probeinit(ap);
2451
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002452 if (softreset) {
2453 rc = do_probe_reset(ap, softreset, postreset, classes);
2454 if (rc == 0)
2455 return 0;
2456 }
2457
2458 if (!hardreset)
2459 return rc;
2460
2461 rc = do_probe_reset(ap, hardreset, postreset, classes);
2462 if (rc == 0 || rc != -ENODEV)
2463 return rc;
2464
2465 if (softreset)
2466 rc = do_probe_reset(ap, softreset, postreset, classes);
2467
2468 return rc;
2469}
2470
Tejun Heo623a3122006-03-05 17:55:58 +09002471/**
2472 * ata_dev_same_device - Determine whether new ID matches configured device
2473 * @ap: port on which the device to compare against resides
2474 * @dev: device to compare against
2475 * @new_class: class of the new device
2476 * @new_id: IDENTIFY page of the new device
2477 *
2478 * Compare @new_class and @new_id against @dev and determine
2479 * whether @dev is the device indicated by @new_class and
2480 * @new_id.
2481 *
2482 * LOCKING:
2483 * None.
2484 *
2485 * RETURNS:
2486 * 1 if @dev matches @new_class and @new_id, 0 otherwise.
2487 */
2488static int ata_dev_same_device(struct ata_port *ap, struct ata_device *dev,
2489 unsigned int new_class, const u16 *new_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490{
Tejun Heo623a3122006-03-05 17:55:58 +09002491 const u16 *old_id = dev->id;
2492 unsigned char model[2][41], serial[2][21];
2493 u64 new_n_sectors;
2494
2495 if (dev->class != new_class) {
2496 printk(KERN_INFO
2497 "ata%u: dev %u class mismatch %d != %d\n",
2498 ap->id, dev->devno, dev->class, new_class);
2499 return 0;
2500 }
2501
2502 ata_id_c_string(old_id, model[0], ATA_ID_PROD_OFS, sizeof(model[0]));
2503 ata_id_c_string(new_id, model[1], ATA_ID_PROD_OFS, sizeof(model[1]));
2504 ata_id_c_string(old_id, serial[0], ATA_ID_SERNO_OFS, sizeof(serial[0]));
2505 ata_id_c_string(new_id, serial[1], ATA_ID_SERNO_OFS, sizeof(serial[1]));
2506 new_n_sectors = ata_id_n_sectors(new_id);
2507
2508 if (strcmp(model[0], model[1])) {
2509 printk(KERN_INFO
2510 "ata%u: dev %u model number mismatch '%s' != '%s'\n",
2511 ap->id, dev->devno, model[0], model[1]);
2512 return 0;
2513 }
2514
2515 if (strcmp(serial[0], serial[1])) {
2516 printk(KERN_INFO
2517 "ata%u: dev %u serial number mismatch '%s' != '%s'\n",
2518 ap->id, dev->devno, serial[0], serial[1]);
2519 return 0;
2520 }
2521
2522 if (dev->class == ATA_DEV_ATA && dev->n_sectors != new_n_sectors) {
2523 printk(KERN_INFO
2524 "ata%u: dev %u n_sectors mismatch %llu != %llu\n",
2525 ap->id, dev->devno, (unsigned long long)dev->n_sectors,
2526 (unsigned long long)new_n_sectors);
2527 return 0;
2528 }
2529
2530 return 1;
2531}
2532
2533/**
2534 * ata_dev_revalidate - Revalidate ATA device
2535 * @ap: port on which the device to revalidate resides
2536 * @dev: device to revalidate
2537 * @post_reset: is this revalidation after reset?
2538 *
2539 * Re-read IDENTIFY page and make sure @dev is still attached to
2540 * the port.
2541 *
2542 * LOCKING:
2543 * Kernel thread context (may sleep)
2544 *
2545 * RETURNS:
2546 * 0 on success, negative errno otherwise
2547 */
2548int ata_dev_revalidate(struct ata_port *ap, struct ata_device *dev,
2549 int post_reset)
2550{
2551 unsigned int class;
2552 u16 *id;
2553 int rc;
2554
2555 if (!ata_dev_present(dev))
2556 return -ENODEV;
2557
2558 class = dev->class;
2559 id = NULL;
2560
2561 /* allocate & read ID data */
2562 rc = ata_dev_read_id(ap, dev, &class, post_reset, &id);
2563 if (rc)
2564 goto fail;
2565
2566 /* is the device still there? */
2567 if (!ata_dev_same_device(ap, dev, class, id)) {
2568 rc = -ENODEV;
2569 goto fail;
2570 }
2571
2572 kfree(dev->id);
2573 dev->id = id;
2574
2575 /* configure device according to the new ID */
2576 return ata_dev_configure(ap, dev, 0);
2577
2578 fail:
2579 printk(KERN_ERR "ata%u: dev %u revalidation failed (errno=%d)\n",
2580 ap->id, dev->devno, rc);
2581 kfree(id);
2582 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583}
2584
Arjan van de Ven98ac62d2005-11-28 10:06:23 +01002585static const char * const ata_dma_blacklist [] = {
Alan Coxf4b15fe2006-03-22 15:54:04 +00002586 "WDC AC11000H", NULL,
2587 "WDC AC22100H", NULL,
2588 "WDC AC32500H", NULL,
2589 "WDC AC33100H", NULL,
2590 "WDC AC31600H", NULL,
2591 "WDC AC32100H", "24.09P07",
2592 "WDC AC23200L", "21.10N21",
2593 "Compaq CRD-8241B", NULL,
2594 "CRD-8400B", NULL,
2595 "CRD-8480B", NULL,
2596 "CRD-8482B", NULL,
2597 "CRD-84", NULL,
2598 "SanDisk SDP3B", NULL,
2599 "SanDisk SDP3B-64", NULL,
2600 "SANYO CD-ROM CRD", NULL,
2601 "HITACHI CDR-8", NULL,
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002602 "HITACHI CDR-8335", NULL,
Alan Coxf4b15fe2006-03-22 15:54:04 +00002603 "HITACHI CDR-8435", NULL,
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002604 "Toshiba CD-ROM XM-6202B", NULL,
2605 "TOSHIBA CD-ROM XM-1702BC", NULL,
2606 "CD-532E-A", NULL,
2607 "E-IDE CD-ROM CR-840", NULL,
2608 "CD-ROM Drive/F5A", NULL,
2609 "WPI CDD-820", NULL,
Alan Coxf4b15fe2006-03-22 15:54:04 +00002610 "SAMSUNG CD-ROM SC-148C", NULL,
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002611 "SAMSUNG CD-ROM SC", NULL,
Alan Coxf4b15fe2006-03-22 15:54:04 +00002612 "SanDisk SDP3B-64", NULL,
2613 "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,
2614 "_NEC DV5800A", NULL,
2615 "SAMSUNG CD-ROM SN-124", "N001"
Linus Torvalds1da177e2005-04-16 15:20:36 -07002616};
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002617
Alan Coxf4b15fe2006-03-22 15:54:04 +00002618static int ata_strim(char *s, size_t len)
2619{
2620 len = strnlen(s, len);
2621
2622 /* ATAPI specifies that empty space is blank-filled; remove blanks */
2623 while ((len > 0) && (s[len - 1] == ' ')) {
2624 len--;
2625 s[len] = 0;
2626 }
2627 return len;
2628}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629
Jeff Garzik057ace52005-10-22 14:27:05 -04002630static int ata_dma_blacklisted(const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631{
Alan Coxf4b15fe2006-03-22 15:54:04 +00002632 unsigned char model_num[40];
2633 unsigned char model_rev[16];
2634 unsigned int nlen, rlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 int i;
2636
Alan Coxf4b15fe2006-03-22 15:54:04 +00002637 ata_id_string(dev->id, model_num, ATA_ID_PROD_OFS,
2638 sizeof(model_num));
2639 ata_id_string(dev->id, model_rev, ATA_ID_FW_REV_OFS,
2640 sizeof(model_rev));
2641 nlen = ata_strim(model_num, sizeof(model_num));
2642 rlen = ata_strim(model_rev, sizeof(model_rev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643
Alan Coxf4b15fe2006-03-22 15:54:04 +00002644 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i += 2) {
2645 if (!strncmp(ata_dma_blacklist[i], model_num, nlen)) {
2646 if (ata_dma_blacklist[i+1] == NULL)
2647 return 1;
2648 if (!strncmp(ata_dma_blacklist[i], model_rev, rlen))
2649 return 1;
2650 }
2651 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002652 return 0;
2653}
2654
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655/**
Tejun Heoa6d5a512006-03-06 04:31:57 +09002656 * ata_dev_xfermask - Compute supported xfermask of the given device
2657 * @ap: Port on which the device to compute xfermask for resides
2658 * @dev: Device to compute xfermask for
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659 *
Tejun Heoacf356b2006-03-24 14:07:50 +09002660 * Compute supported xfermask of @dev and store it in
2661 * dev->*_mask. This function is responsible for applying all
2662 * known limits including host controller limits, device
2663 * blacklist, etc...
Jeff Garzik0cba6322005-05-30 19:49:12 -04002664 *
Tejun Heo600511e2006-03-25 11:14:07 +09002665 * FIXME: The current implementation limits all transfer modes to
2666 * the fastest of the lowested device on the port. This is not
Tejun Heo05c8e0a2006-03-25 14:28:57 +09002667 * required on most controllers.
Tejun Heo600511e2006-03-25 11:14:07 +09002668 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 * LOCKING:
Tejun Heoa6d5a512006-03-06 04:31:57 +09002670 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 */
Tejun Heoacf356b2006-03-24 14:07:50 +09002672static void ata_dev_xfermask(struct ata_port *ap, struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673{
Alan Cox5444a6f2006-03-27 18:58:20 +01002674 struct ata_host_set *hs = ap->host_set;
Tejun Heoa6d5a512006-03-06 04:31:57 +09002675 unsigned long xfer_mask;
2676 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677
Tejun Heoa6d5a512006-03-06 04:31:57 +09002678 xfer_mask = ata_pack_xfermask(ap->pio_mask, ap->mwdma_mask,
2679 ap->udma_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680
Alan Cox5444a6f2006-03-27 18:58:20 +01002681 /* FIXME: Use port-wide xfermask for now */
Tejun Heoa6d5a512006-03-06 04:31:57 +09002682 for (i = 0; i < ATA_MAX_DEVICES; i++) {
2683 struct ata_device *d = &ap->device[i];
2684 if (!ata_dev_present(d))
2685 continue;
Tejun Heoacf356b2006-03-24 14:07:50 +09002686 xfer_mask &= ata_pack_xfermask(d->pio_mask, d->mwdma_mask,
2687 d->udma_mask);
Tejun Heoa6d5a512006-03-06 04:31:57 +09002688 xfer_mask &= ata_id_xfermask(d->id);
2689 if (ata_dma_blacklisted(d))
2690 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
Alan Cox5444a6f2006-03-27 18:58:20 +01002691 /* Apply cable rule here. Don't apply it early because when
2692 we handle hot plug the cable type can itself change */
2693 if (ap->cbl == ATA_CBL_PATA40)
2694 xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695 }
2696
Tejun Heoa6d5a512006-03-06 04:31:57 +09002697 if (ata_dma_blacklisted(dev))
2698 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, "
2699 "disabling DMA\n", ap->id, dev->devno);
2700
Alan Cox5444a6f2006-03-27 18:58:20 +01002701 if (hs->flags & ATA_HOST_SIMPLEX) {
2702 if (hs->simplex_claimed)
2703 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
2704 }
2705 if (ap->ops->mode_filter)
2706 xfer_mask = ap->ops->mode_filter(ap, dev, xfer_mask);
2707
Tejun Heoacf356b2006-03-24 14:07:50 +09002708 ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask,
2709 &dev->udma_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710}
2711
2712/**
2713 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2714 * @ap: Port associated with device @dev
2715 * @dev: Device to which command will be sent
2716 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002717 * Issue SET FEATURES - XFER MODE command to device @dev
2718 * on port @ap.
2719 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002721 * PCI/etc. bus probe sem.
Tejun Heo83206a22006-03-24 15:25:31 +09002722 *
2723 * RETURNS:
2724 * 0 on success, AC_ERR_* mask otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 */
2726
Tejun Heo83206a22006-03-24 15:25:31 +09002727static unsigned int ata_dev_set_xfermode(struct ata_port *ap,
2728 struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729{
Tejun Heoa0123702005-12-13 14:49:31 +09002730 struct ata_taskfile tf;
Tejun Heo83206a22006-03-24 15:25:31 +09002731 unsigned int err_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732
2733 /* set up set-features taskfile */
2734 DPRINTK("set features - xfer mode\n");
2735
Tejun Heoa0123702005-12-13 14:49:31 +09002736 ata_tf_init(ap, &tf, dev->devno);
2737 tf.command = ATA_CMD_SET_FEATURES;
2738 tf.feature = SETFEATURES_XFER;
2739 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2740 tf.protocol = ATA_PROT_NODATA;
2741 tf.nsect = dev->xfer_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742
Tejun Heo83206a22006-03-24 15:25:31 +09002743 err_mask = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744
Tejun Heo83206a22006-03-24 15:25:31 +09002745 DPRINTK("EXIT, err_mask=%x\n", err_mask);
2746 return err_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747}
2748
2749/**
Albert Lee8bf62ec2005-05-12 15:29:42 -04002750 * ata_dev_init_params - Issue INIT DEV PARAMS command
2751 * @ap: Port associated with device @dev
2752 * @dev: Device to which command will be sent
2753 *
2754 * LOCKING:
Tejun Heo6aff8f12006-02-15 18:24:09 +09002755 * Kernel thread context (may sleep)
2756 *
2757 * RETURNS:
2758 * 0 on success, AC_ERR_* mask otherwise.
Albert Lee8bf62ec2005-05-12 15:29:42 -04002759 */
2760
Tejun Heo6aff8f12006-02-15 18:24:09 +09002761static unsigned int ata_dev_init_params(struct ata_port *ap,
Albert Lee00b6f5e2006-03-27 16:39:18 +08002762 struct ata_device *dev,
2763 u16 heads,
2764 u16 sectors)
Albert Lee8bf62ec2005-05-12 15:29:42 -04002765{
Tejun Heoa0123702005-12-13 14:49:31 +09002766 struct ata_taskfile tf;
Tejun Heo6aff8f12006-02-15 18:24:09 +09002767 unsigned int err_mask;
Albert Lee8bf62ec2005-05-12 15:29:42 -04002768
2769 /* Number of sectors per track 1-255. Number of heads 1-16 */
2770 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
Albert Lee00b6f5e2006-03-27 16:39:18 +08002771 return AC_ERR_INVALID;
Albert Lee8bf62ec2005-05-12 15:29:42 -04002772
2773 /* set up init dev params taskfile */
2774 DPRINTK("init dev params \n");
2775
Tejun Heoa0123702005-12-13 14:49:31 +09002776 ata_tf_init(ap, &tf, dev->devno);
2777 tf.command = ATA_CMD_INIT_DEV_PARAMS;
2778 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2779 tf.protocol = ATA_PROT_NODATA;
2780 tf.nsect = sectors;
2781 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
Albert Lee8bf62ec2005-05-12 15:29:42 -04002782
Tejun Heo6aff8f12006-02-15 18:24:09 +09002783 err_mask = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
Albert Lee8bf62ec2005-05-12 15:29:42 -04002784
Tejun Heo6aff8f12006-02-15 18:24:09 +09002785 DPRINTK("EXIT, err_mask=%x\n", err_mask);
2786 return err_mask;
Albert Lee8bf62ec2005-05-12 15:29:42 -04002787}
2788
2789/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002790 * ata_sg_clean - Unmap DMA memory associated with command
2791 * @qc: Command containing DMA memory to be released
2792 *
2793 * Unmap all mapped DMA memory associated with this command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 *
2795 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002796 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 */
2798
2799static void ata_sg_clean(struct ata_queued_cmd *qc)
2800{
2801 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002802 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002804 void *pad_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805
Tejun Heoa4631472006-02-11 19:11:13 +09002806 WARN_ON(!(qc->flags & ATA_QCFLAG_DMAMAP));
2807 WARN_ON(sg == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808
2809 if (qc->flags & ATA_QCFLAG_SINGLE)
Jeff Garzikf1318832006-02-20 16:55:56 -05002810 WARN_ON(qc->n_elem > 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05002812 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002814 /* if we padded the buffer out to 32-bit bound, and data
2815 * xfer direction is from-device, we must copy from the
2816 * pad buffer back into the supplied buffer
2817 */
2818 if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
2819 pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2820
2821 if (qc->flags & ATA_QCFLAG_SG) {
Jeff Garzike1410f22005-11-14 14:06:26 -05002822 if (qc->n_elem)
Brian King2f1f6102006-03-23 17:30:15 -06002823 dma_unmap_sg(ap->dev, sg, qc->n_elem, dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002824 /* restore last sg */
2825 sg[qc->orig_n_elem - 1].length += qc->pad_len;
2826 if (pad_buf) {
2827 struct scatterlist *psg = &qc->pad_sgent;
2828 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2829 memcpy(addr + psg->offset, pad_buf, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05002830 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002831 }
2832 } else {
Tejun Heo2e242fa2006-02-20 23:48:38 +09002833 if (qc->n_elem)
Brian King2f1f6102006-03-23 17:30:15 -06002834 dma_unmap_single(ap->dev,
Jeff Garzike1410f22005-11-14 14:06:26 -05002835 sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
2836 dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002837 /* restore sg */
2838 sg->length += qc->pad_len;
2839 if (pad_buf)
2840 memcpy(qc->buf_virt + sg->length - qc->pad_len,
2841 pad_buf, qc->pad_len);
2842 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843
2844 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002845 qc->__sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846}
2847
2848/**
2849 * ata_fill_sg - Fill PCI IDE PRD table
2850 * @qc: Metadata associated with taskfile to be transferred
2851 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002852 * Fill PCI IDE PRD (scatter-gather) table with segments
2853 * associated with the current disk command.
2854 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855 * LOCKING:
Jeff Garzik780a87f2005-05-30 15:41:05 -04002856 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 *
2858 */
2859static void ata_fill_sg(struct ata_queued_cmd *qc)
2860{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002862 struct scatterlist *sg;
2863 unsigned int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864
Tejun Heoa4631472006-02-11 19:11:13 +09002865 WARN_ON(qc->__sg == NULL);
Jeff Garzikf1318832006-02-20 16:55:56 -05002866 WARN_ON(qc->n_elem == 0 && qc->pad_len == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002867
2868 idx = 0;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002869 ata_for_each_sg(sg, qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 u32 addr, offset;
2871 u32 sg_len, len;
2872
2873 /* determine if physical DMA addr spans 64K boundary.
2874 * Note h/w doesn't support 64-bit, so we unconditionally
2875 * truncate dma_addr_t to u32.
2876 */
2877 addr = (u32) sg_dma_address(sg);
2878 sg_len = sg_dma_len(sg);
2879
2880 while (sg_len) {
2881 offset = addr & 0xffff;
2882 len = sg_len;
2883 if ((offset + sg_len) > 0x10000)
2884 len = 0x10000 - offset;
2885
2886 ap->prd[idx].addr = cpu_to_le32(addr);
2887 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
2888 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
2889
2890 idx++;
2891 sg_len -= len;
2892 addr += len;
2893 }
2894 }
2895
2896 if (idx)
2897 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
2898}
2899/**
2900 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
2901 * @qc: Metadata associated with taskfile to check
2902 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002903 * Allow low-level driver to filter ATA PACKET commands, returning
2904 * a status indicating whether or not it is OK to use DMA for the
2905 * supplied PACKET command.
2906 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002908 * spin_lock_irqsave(host_set lock)
2909 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 * RETURNS: 0 when ATAPI DMA can be used
2911 * nonzero otherwise
2912 */
2913int ata_check_atapi_dma(struct ata_queued_cmd *qc)
2914{
2915 struct ata_port *ap = qc->ap;
2916 int rc = 0; /* Assume ATAPI DMA is OK by default */
2917
2918 if (ap->ops->check_atapi_dma)
2919 rc = ap->ops->check_atapi_dma(qc);
2920
Albert Leec2bbc552006-03-25 17:56:55 +08002921 /* We don't support polling DMA.
2922 * Use PIO if the LLDD handles only interrupts in
2923 * the HSM_ST_LAST state and the ATAPI device
2924 * generates CDB interrupts.
2925 */
2926 if ((ap->flags & ATA_FLAG_PIO_POLLING) &&
2927 (qc->dev->flags & ATA_DFLAG_CDB_INTR))
2928 rc = 1;
2929
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 return rc;
2931}
2932/**
2933 * ata_qc_prep - Prepare taskfile for submission
2934 * @qc: Metadata associated with taskfile to be prepared
2935 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002936 * Prepare ATA taskfile for submission.
2937 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 * LOCKING:
2939 * spin_lock_irqsave(host_set lock)
2940 */
2941void ata_qc_prep(struct ata_queued_cmd *qc)
2942{
2943 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2944 return;
2945
2946 ata_fill_sg(qc);
2947}
2948
Brian Kinge46834c2006-03-17 17:04:03 -06002949void ata_noop_qc_prep(struct ata_queued_cmd *qc) { }
2950
Jeff Garzik0cba6322005-05-30 19:49:12 -04002951/**
2952 * ata_sg_init_one - Associate command with memory buffer
2953 * @qc: Command to be associated
2954 * @buf: Memory buffer
2955 * @buflen: Length of memory buffer, in bytes.
2956 *
2957 * Initialize the data-related elements of queued_cmd @qc
2958 * to point to a single memory buffer, @buf of byte length @buflen.
2959 *
2960 * LOCKING:
2961 * spin_lock_irqsave(host_set lock)
2962 */
2963
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
2965{
2966 struct scatterlist *sg;
2967
2968 qc->flags |= ATA_QCFLAG_SINGLE;
2969
2970 memset(&qc->sgent, 0, sizeof(qc->sgent));
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002971 qc->__sg = &qc->sgent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 qc->n_elem = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002973 qc->orig_n_elem = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 qc->buf_virt = buf;
2975
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002976 sg = qc->__sg;
Jeff Garzikf0612bb2005-10-30 01:58:18 -05002977 sg_init_one(sg, buf, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978}
2979
Jeff Garzik0cba6322005-05-30 19:49:12 -04002980/**
2981 * ata_sg_init - Associate command with scatter-gather table.
2982 * @qc: Command to be associated
2983 * @sg: Scatter-gather table.
2984 * @n_elem: Number of elements in s/g table.
2985 *
2986 * Initialize the data-related elements of queued_cmd @qc
2987 * to point to a scatter-gather table @sg, containing @n_elem
2988 * elements.
2989 *
2990 * LOCKING:
2991 * spin_lock_irqsave(host_set lock)
2992 */
2993
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
2995 unsigned int n_elem)
2996{
2997 qc->flags |= ATA_QCFLAG_SG;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002998 qc->__sg = sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 qc->n_elem = n_elem;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003000 qc->orig_n_elem = n_elem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001}
3002
3003/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04003004 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
3005 * @qc: Command with memory buffer to be mapped.
3006 *
3007 * DMA-map the memory buffer associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008 *
3009 * LOCKING:
3010 * spin_lock_irqsave(host_set lock)
3011 *
3012 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003013 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 */
3015
3016static int ata_sg_setup_one(struct ata_queued_cmd *qc)
3017{
3018 struct ata_port *ap = qc->ap;
3019 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003020 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021 dma_addr_t dma_address;
Tejun Heo2e242fa2006-02-20 23:48:38 +09003022 int trim_sg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003024 /* we must lengthen transfers to end on a 32-bit boundary */
3025 qc->pad_len = sg->length & 3;
3026 if (qc->pad_len) {
3027 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
3028 struct scatterlist *psg = &qc->pad_sgent;
3029
Tejun Heoa4631472006-02-11 19:11:13 +09003030 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003031
3032 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
3033
3034 if (qc->tf.flags & ATA_TFLAG_WRITE)
3035 memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
3036 qc->pad_len);
3037
3038 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
3039 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
3040 /* trim sg */
3041 sg->length -= qc->pad_len;
Tejun Heo2e242fa2006-02-20 23:48:38 +09003042 if (sg->length == 0)
3043 trim_sg = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003044
3045 DPRINTK("padding done, sg->length=%u pad_len=%u\n",
3046 sg->length, qc->pad_len);
3047 }
3048
Tejun Heo2e242fa2006-02-20 23:48:38 +09003049 if (trim_sg) {
3050 qc->n_elem--;
Jeff Garzike1410f22005-11-14 14:06:26 -05003051 goto skip_map;
3052 }
3053
Brian King2f1f6102006-03-23 17:30:15 -06003054 dma_address = dma_map_single(ap->dev, qc->buf_virt,
Albert Lee32529e02005-05-26 03:49:42 -04003055 sg->length, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05003056 if (dma_mapping_error(dma_address)) {
3057 /* restore sg */
3058 sg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05003060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061
3062 sg_dma_address(sg) = dma_address;
Albert Lee32529e02005-05-26 03:49:42 -04003063 sg_dma_len(sg) = sg->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064
Tejun Heo2e242fa2006-02-20 23:48:38 +09003065skip_map:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
3067 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3068
3069 return 0;
3070}
3071
3072/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04003073 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
3074 * @qc: Command with scatter-gather table to be mapped.
3075 *
3076 * DMA-map the scatter-gather table associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 *
3078 * LOCKING:
3079 * spin_lock_irqsave(host_set lock)
3080 *
3081 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003082 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 *
3084 */
3085
3086static int ata_sg_setup(struct ata_queued_cmd *qc)
3087{
3088 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003089 struct scatterlist *sg = qc->__sg;
3090 struct scatterlist *lsg = &sg[qc->n_elem - 1];
Jeff Garzike1410f22005-11-14 14:06:26 -05003091 int n_elem, pre_n_elem, dir, trim_sg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092
3093 VPRINTK("ENTER, ata%u\n", ap->id);
Tejun Heoa4631472006-02-11 19:11:13 +09003094 WARN_ON(!(qc->flags & ATA_QCFLAG_SG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003096 /* we must lengthen transfers to end on a 32-bit boundary */
3097 qc->pad_len = lsg->length & 3;
3098 if (qc->pad_len) {
3099 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
3100 struct scatterlist *psg = &qc->pad_sgent;
3101 unsigned int offset;
3102
Tejun Heoa4631472006-02-11 19:11:13 +09003103 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003104
3105 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
3106
3107 /*
3108 * psg->page/offset are used to copy to-be-written
3109 * data in this function or read data in ata_sg_clean.
3110 */
3111 offset = lsg->offset + lsg->length - qc->pad_len;
3112 psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
3113 psg->offset = offset_in_page(offset);
3114
3115 if (qc->tf.flags & ATA_TFLAG_WRITE) {
3116 void *addr = kmap_atomic(psg->page, KM_IRQ0);
3117 memcpy(pad_buf, addr + psg->offset, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05003118 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003119 }
3120
3121 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
3122 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
3123 /* trim last sg */
3124 lsg->length -= qc->pad_len;
Jeff Garzike1410f22005-11-14 14:06:26 -05003125 if (lsg->length == 0)
3126 trim_sg = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003127
3128 DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
3129 qc->n_elem - 1, lsg->length, qc->pad_len);
3130 }
3131
Jeff Garzike1410f22005-11-14 14:06:26 -05003132 pre_n_elem = qc->n_elem;
3133 if (trim_sg && pre_n_elem)
3134 pre_n_elem--;
3135
3136 if (!pre_n_elem) {
3137 n_elem = 0;
3138 goto skip_map;
3139 }
3140
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 dir = qc->dma_dir;
Brian King2f1f6102006-03-23 17:30:15 -06003142 n_elem = dma_map_sg(ap->dev, sg, pre_n_elem, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05003143 if (n_elem < 1) {
3144 /* restore last sg */
3145 lsg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05003147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148
3149 DPRINTK("%d sg elements mapped\n", n_elem);
3150
Jeff Garzike1410f22005-11-14 14:06:26 -05003151skip_map:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 qc->n_elem = n_elem;
3153
3154 return 0;
3155}
3156
3157/**
Tejun Heo40e8c822005-08-22 17:12:45 +09003158 * ata_poll_qc_complete - turn irq back on and finish qc
3159 * @qc: Command to complete
Randy Dunlap8e8b77d2005-11-01 21:29:27 -08003160 * @err_mask: ATA status register content
Tejun Heo40e8c822005-08-22 17:12:45 +09003161 *
3162 * LOCKING:
3163 * None. (grabs host lock)
3164 */
3165
Albert Leea22e2eb2005-12-05 15:38:02 +08003166void ata_poll_qc_complete(struct ata_queued_cmd *qc)
Tejun Heo40e8c822005-08-22 17:12:45 +09003167{
3168 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003169 unsigned long flags;
Tejun Heo40e8c822005-08-22 17:12:45 +09003170
Jeff Garzikb8f61532005-08-25 22:01:20 -04003171 spin_lock_irqsave(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09003172 ata_irq_on(ap);
Albert Leea22e2eb2005-12-05 15:38:02 +08003173 ata_qc_complete(qc);
Jeff Garzikb8f61532005-08-25 22:01:20 -04003174 spin_unlock_irqrestore(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09003175}
3176
3177/**
Randy Dunlapc893a3a2006-01-28 13:15:32 -05003178 * swap_buf_le16 - swap halves of 16-bit words in place
Edward Falk0baab862005-06-02 18:17:13 -04003179 * @buf: Buffer to swap
3180 * @buf_words: Number of 16-bit words in buffer.
3181 *
3182 * Swap halves of 16-bit words if needed to convert from
3183 * little-endian byte order to native cpu byte order, or
3184 * vice-versa.
3185 *
3186 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003187 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04003188 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189void swap_buf_le16(u16 *buf, unsigned int buf_words)
3190{
3191#ifdef __BIG_ENDIAN
3192 unsigned int i;
3193
3194 for (i = 0; i < buf_words; i++)
3195 buf[i] = le16_to_cpu(buf[i]);
3196#endif /* __BIG_ENDIAN */
3197}
3198
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003199/**
3200 * ata_mmio_data_xfer - Transfer data by MMIO
3201 * @ap: port to read/write
3202 * @buf: data buffer
3203 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04003204 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003205 *
3206 * Transfer data from/to the device data register by MMIO.
3207 *
3208 * LOCKING:
3209 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003210 */
3211
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
3213 unsigned int buflen, int write_data)
3214{
3215 unsigned int i;
3216 unsigned int words = buflen >> 1;
3217 u16 *buf16 = (u16 *) buf;
3218 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
3219
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003220 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 if (write_data) {
3222 for (i = 0; i < words; i++)
3223 writew(le16_to_cpu(buf16[i]), mmio);
3224 } else {
3225 for (i = 0; i < words; i++)
3226 buf16[i] = cpu_to_le16(readw(mmio));
3227 }
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003228
3229 /* Transfer trailing 1 byte, if any. */
3230 if (unlikely(buflen & 0x01)) {
3231 u16 align_buf[1] = { 0 };
3232 unsigned char *trailing_buf = buf + buflen - 1;
3233
3234 if (write_data) {
3235 memcpy(align_buf, trailing_buf, 1);
3236 writew(le16_to_cpu(align_buf[0]), mmio);
3237 } else {
3238 align_buf[0] = cpu_to_le16(readw(mmio));
3239 memcpy(trailing_buf, align_buf, 1);
3240 }
3241 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242}
3243
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003244/**
3245 * ata_pio_data_xfer - Transfer data by PIO
3246 * @ap: port to read/write
3247 * @buf: data buffer
3248 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04003249 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003250 *
3251 * Transfer data from/to the device data register by PIO.
3252 *
3253 * LOCKING:
3254 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003255 */
3256
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
3258 unsigned int buflen, int write_data)
3259{
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003260 unsigned int words = buflen >> 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003262 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 if (write_data)
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003264 outsw(ap->ioaddr.data_addr, buf, words);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 else
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003266 insw(ap->ioaddr.data_addr, buf, words);
3267
3268 /* Transfer trailing 1 byte, if any. */
3269 if (unlikely(buflen & 0x01)) {
3270 u16 align_buf[1] = { 0 };
3271 unsigned char *trailing_buf = buf + buflen - 1;
3272
3273 if (write_data) {
3274 memcpy(align_buf, trailing_buf, 1);
3275 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
3276 } else {
3277 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
3278 memcpy(trailing_buf, align_buf, 1);
3279 }
3280 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281}
3282
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003283/**
3284 * ata_data_xfer - Transfer data from/to the data register.
3285 * @ap: port to read/write
3286 * @buf: data buffer
3287 * @buflen: buffer length
3288 * @do_write: read/write
3289 *
3290 * Transfer data from/to the device data register.
3291 *
3292 * LOCKING:
3293 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003294 */
3295
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
3297 unsigned int buflen, int do_write)
3298{
Alan Coxa1bd9e62006-01-17 20:53:50 +00003299 /* Make the crap hardware pay the costs not the good stuff */
3300 if (unlikely(ap->flags & ATA_FLAG_IRQ_MASK)) {
3301 unsigned long flags;
3302 local_irq_save(flags);
3303 if (ap->flags & ATA_FLAG_MMIO)
3304 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3305 else
3306 ata_pio_data_xfer(ap, buf, buflen, do_write);
3307 local_irq_restore(flags);
3308 } else {
3309 if (ap->flags & ATA_FLAG_MMIO)
3310 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3311 else
3312 ata_pio_data_xfer(ap, buf, buflen, do_write);
3313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314}
3315
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003316/**
3317 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
3318 * @qc: Command on going
3319 *
3320 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
3321 *
3322 * LOCKING:
3323 * Inherited from caller.
3324 */
3325
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326static void ata_pio_sector(struct ata_queued_cmd *qc)
3327{
3328 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003329 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 struct ata_port *ap = qc->ap;
3331 struct page *page;
3332 unsigned int offset;
3333 unsigned char *buf;
3334
3335 if (qc->cursect == (qc->nsect - 1))
Albert Lee14be71f2005-09-27 17:36:35 +08003336 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003337
3338 page = sg[qc->cursg].page;
3339 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
3340
3341 /* get the current page and offset */
3342 page = nth_page(page, (offset >> PAGE_SHIFT));
3343 offset %= PAGE_SIZE;
3344
Albert Lee7282aa42005-10-09 09:46:07 -04003345 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3346
Albert Lee91b8b312005-10-09 09:48:44 -04003347 if (PageHighMem(page)) {
3348 unsigned long flags;
Albert Lee7282aa42005-10-09 09:46:07 -04003349
Albert Lee91b8b312005-10-09 09:48:44 -04003350 local_irq_save(flags);
3351 buf = kmap_atomic(page, KM_IRQ0);
Albert Lee083958d2005-10-09 09:47:31 -04003352
Albert Lee91b8b312005-10-09 09:48:44 -04003353 /* do the actual data transfer */
3354 ata_data_xfer(ap, buf + offset, ATA_SECT_SIZE, do_write);
3355
3356 kunmap_atomic(buf, KM_IRQ0);
3357 local_irq_restore(flags);
3358 } else {
3359 buf = page_address(page);
3360 ata_data_xfer(ap, buf + offset, ATA_SECT_SIZE, do_write);
3361 }
Albert Lee7282aa42005-10-09 09:46:07 -04003362
Linus Torvalds1da177e2005-04-16 15:20:36 -07003363 qc->cursect++;
3364 qc->cursg_ofs++;
3365
Albert Lee32529e02005-05-26 03:49:42 -04003366 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 qc->cursg++;
3368 qc->cursg_ofs = 0;
3369 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370}
3371
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003372/**
Albert Lee07f6f7d2005-11-01 19:33:20 +08003373 * ata_pio_sectors - Transfer one or many 512-byte sectors.
3374 * @qc: Command on going
3375 *
3376 * Transfer one or many ATA_SECT_SIZE of data from/to the
3377 * ATA device for the DRQ request.
3378 *
3379 * LOCKING:
3380 * Inherited from caller.
3381 */
3382
3383static void ata_pio_sectors(struct ata_queued_cmd *qc)
3384{
3385 if (is_multi_taskfile(&qc->tf)) {
3386 /* READ/WRITE MULTIPLE */
3387 unsigned int nsect;
3388
Jeff Garzik587005d2006-02-11 18:17:32 -05003389 WARN_ON(qc->dev->multi_count == 0);
Albert Lee07f6f7d2005-11-01 19:33:20 +08003390
3391 nsect = min(qc->nsect - qc->cursect, qc->dev->multi_count);
3392 while (nsect--)
3393 ata_pio_sector(qc);
3394 } else
3395 ata_pio_sector(qc);
3396}
3397
3398/**
Albert Leec71c1852005-10-04 06:03:45 -04003399 * atapi_send_cdb - Write CDB bytes to hardware
3400 * @ap: Port to which ATAPI device is attached.
3401 * @qc: Taskfile currently active
3402 *
3403 * When device has indicated its readiness to accept
3404 * a CDB, this function is called. Send the CDB.
3405 *
3406 * LOCKING:
3407 * caller.
3408 */
3409
3410static void atapi_send_cdb(struct ata_port *ap, struct ata_queued_cmd *qc)
3411{
3412 /* send SCSI cdb */
3413 DPRINTK("send cdb\n");
Jeff Garzikdb024d52006-02-13 00:23:57 -05003414 WARN_ON(qc->dev->cdb_len < 12);
Albert Leec71c1852005-10-04 06:03:45 -04003415
Jeff Garzikdb024d52006-02-13 00:23:57 -05003416 ata_data_xfer(ap, qc->cdb, qc->dev->cdb_len, 1);
Albert Leec71c1852005-10-04 06:03:45 -04003417 ata_altstatus(ap); /* flush */
3418
3419 switch (qc->tf.protocol) {
3420 case ATA_PROT_ATAPI:
3421 ap->hsm_task_state = HSM_ST;
3422 break;
3423 case ATA_PROT_ATAPI_NODATA:
3424 ap->hsm_task_state = HSM_ST_LAST;
3425 break;
3426 case ATA_PROT_ATAPI_DMA:
3427 ap->hsm_task_state = HSM_ST_LAST;
3428 /* initiate bmdma */
3429 ap->ops->bmdma_start(qc);
3430 break;
3431 }
3432}
3433
3434/**
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003435 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
3436 * @qc: Command on going
3437 * @bytes: number of bytes
3438 *
3439 * Transfer Transfer data from/to the ATAPI device.
3440 *
3441 * LOCKING:
3442 * Inherited from caller.
3443 *
3444 */
3445
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
3447{
3448 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003449 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 struct ata_port *ap = qc->ap;
3451 struct page *page;
3452 unsigned char *buf;
3453 unsigned int offset, count;
3454
Albert Lee563a6e12005-08-12 14:17:50 +08003455 if (qc->curbytes + bytes >= qc->nbytes)
Albert Lee14be71f2005-09-27 17:36:35 +08003456 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003457
3458next_sg:
Albert Lee563a6e12005-08-12 14:17:50 +08003459 if (unlikely(qc->cursg >= qc->n_elem)) {
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003460 /*
Albert Lee563a6e12005-08-12 14:17:50 +08003461 * The end of qc->sg is reached and the device expects
3462 * more data to transfer. In order not to overrun qc->sg
3463 * and fulfill length specified in the byte count register,
3464 * - for read case, discard trailing data from the device
3465 * - for write case, padding zero data to the device
3466 */
3467 u16 pad_buf[1] = { 0 };
3468 unsigned int words = bytes >> 1;
3469 unsigned int i;
3470
3471 if (words) /* warning if bytes > 1 */
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003472 printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
Albert Lee563a6e12005-08-12 14:17:50 +08003473 ap->id, bytes);
3474
3475 for (i = 0; i < words; i++)
3476 ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
3477
Albert Lee14be71f2005-09-27 17:36:35 +08003478 ap->hsm_task_state = HSM_ST_LAST;
Albert Lee563a6e12005-08-12 14:17:50 +08003479 return;
3480 }
3481
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003482 sg = &qc->__sg[qc->cursg];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484 page = sg->page;
3485 offset = sg->offset + qc->cursg_ofs;
3486
3487 /* get the current page and offset */
3488 page = nth_page(page, (offset >> PAGE_SHIFT));
3489 offset %= PAGE_SIZE;
3490
Albert Lee6952df02005-06-06 15:56:03 +08003491 /* don't overrun current sg */
Albert Lee32529e02005-05-26 03:49:42 -04003492 count = min(sg->length - qc->cursg_ofs, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493
3494 /* don't cross page boundaries */
3495 count = min(count, (unsigned int)PAGE_SIZE - offset);
3496
Albert Lee7282aa42005-10-09 09:46:07 -04003497 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3498
Albert Lee91b8b312005-10-09 09:48:44 -04003499 if (PageHighMem(page)) {
3500 unsigned long flags;
Albert Lee7282aa42005-10-09 09:46:07 -04003501
Albert Lee91b8b312005-10-09 09:48:44 -04003502 local_irq_save(flags);
3503 buf = kmap_atomic(page, KM_IRQ0);
Albert Lee083958d2005-10-09 09:47:31 -04003504
Albert Lee91b8b312005-10-09 09:48:44 -04003505 /* do the actual data transfer */
3506 ata_data_xfer(ap, buf + offset, count, do_write);
3507
3508 kunmap_atomic(buf, KM_IRQ0);
3509 local_irq_restore(flags);
3510 } else {
3511 buf = page_address(page);
3512 ata_data_xfer(ap, buf + offset, count, do_write);
3513 }
Albert Lee7282aa42005-10-09 09:46:07 -04003514
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515 bytes -= count;
3516 qc->curbytes += count;
3517 qc->cursg_ofs += count;
3518
Albert Lee32529e02005-05-26 03:49:42 -04003519 if (qc->cursg_ofs == sg->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003520 qc->cursg++;
3521 qc->cursg_ofs = 0;
3522 }
3523
Albert Lee563a6e12005-08-12 14:17:50 +08003524 if (bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003525 goto next_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003526}
3527
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003528/**
3529 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
3530 * @qc: Command on going
3531 *
3532 * Transfer Transfer data from/to the ATAPI device.
3533 *
3534 * LOCKING:
3535 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003536 */
3537
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538static void atapi_pio_bytes(struct ata_queued_cmd *qc)
3539{
3540 struct ata_port *ap = qc->ap;
3541 struct ata_device *dev = qc->dev;
3542 unsigned int ireason, bc_lo, bc_hi, bytes;
3543 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
3544
3545 ap->ops->tf_read(ap, &qc->tf);
3546 ireason = qc->tf.nsect;
3547 bc_lo = qc->tf.lbam;
3548 bc_hi = qc->tf.lbah;
3549 bytes = (bc_hi << 8) | bc_lo;
3550
3551 /* shall be cleared to zero, indicating xfer of data */
3552 if (ireason & (1 << 0))
3553 goto err_out;
3554
3555 /* make sure transfer direction matches expected */
3556 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
3557 if (do_write != i_write)
3558 goto err_out;
3559
Albert Lee312f7da2005-09-27 17:38:03 +08003560 VPRINTK("ata%u: xfering %d bytes\n", ap->id, bytes);
3561
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562 __atapi_pio_bytes(qc, bytes);
3563
3564 return;
3565
3566err_out:
3567 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
3568 ap->id, dev->devno);
Tejun Heo11a56d22006-01-23 13:09:36 +09003569 qc->err_mask |= AC_ERR_HSM;
Albert Lee14be71f2005-09-27 17:36:35 +08003570 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571}
3572
3573/**
Albert Leec234fb02006-03-25 17:58:38 +08003574 * ata_hsm_ok_in_wq - Check if the qc can be handled in the workqueue.
3575 * @ap: the target ata_port
3576 * @qc: qc on going
3577 *
3578 * RETURNS:
3579 * 1 if ok in workqueue, 0 otherwise.
3580 */
3581
3582static inline int ata_hsm_ok_in_wq(struct ata_port *ap, struct ata_queued_cmd *qc)
3583{
3584 if (qc->tf.flags & ATA_TFLAG_POLLING)
3585 return 1;
3586
3587 if (ap->hsm_task_state == HSM_ST_FIRST) {
3588 if (qc->tf.protocol == ATA_PROT_PIO &&
3589 (qc->tf.flags & ATA_TFLAG_WRITE))
3590 return 1;
3591
3592 if (is_atapi_taskfile(&qc->tf) &&
3593 !(qc->dev->flags & ATA_DFLAG_CDB_INTR))
3594 return 1;
3595 }
3596
3597 return 0;
3598}
3599
3600/**
Albert Leebb5cb292006-03-25 17:48:02 +08003601 * ata_hsm_move - move the HSM to the next state.
3602 * @ap: the target ata_port
3603 * @qc: qc on going
3604 * @status: current device status
3605 * @in_wq: 1 if called from workqueue, 0 otherwise
3606 *
3607 * RETURNS:
3608 * 1 when poll next status needed, 0 otherwise.
3609 */
3610
3611static int ata_hsm_move(struct ata_port *ap, struct ata_queued_cmd *qc,
3612 u8 status, int in_wq)
Albert Leee2cec772006-03-25 17:43:49 +08003613{
Albert Leebb5cb292006-03-25 17:48:02 +08003614 unsigned long flags = 0;
3615 int poll_next;
3616
Albert Lee6912ccd2006-03-25 17:45:49 +08003617 WARN_ON((qc->flags & ATA_QCFLAG_ACTIVE) == 0);
3618
Albert Leebb5cb292006-03-25 17:48:02 +08003619 /* Make sure ata_qc_issue_prot() does not throw things
3620 * like DMA polling into the workqueue. Notice that
3621 * in_wq is not equivalent to (qc->tf.flags & ATA_TFLAG_POLLING).
3622 */
Albert Leec234fb02006-03-25 17:58:38 +08003623 WARN_ON(in_wq != ata_hsm_ok_in_wq(ap, qc));
Albert Leebb5cb292006-03-25 17:48:02 +08003624
Albert Leee2cec772006-03-25 17:43:49 +08003625fsm_start:
Albert Lee999bb6f2006-03-25 18:07:48 +08003626 DPRINTK("ata%u: protocol %d task_state %d (dev_stat 0x%X)\n",
3627 ap->id, qc->tf.protocol, ap->hsm_task_state, status);
3628
Albert Leee2cec772006-03-25 17:43:49 +08003629 switch (ap->hsm_task_state) {
3630 case HSM_ST_FIRST:
Albert Leebb5cb292006-03-25 17:48:02 +08003631 /* Send first data block or PACKET CDB */
3632
3633 /* If polling, we will stay in the work queue after
3634 * sending the data. Otherwise, interrupt handler
3635 * takes over after sending the data.
3636 */
3637 poll_next = (qc->tf.flags & ATA_TFLAG_POLLING);
3638
Albert Leee2cec772006-03-25 17:43:49 +08003639 /* check device status */
3640 if (unlikely((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ)) {
3641 /* Wrong status. Let EH handle this */
3642 qc->err_mask |= AC_ERR_HSM;
3643 ap->hsm_task_state = HSM_ST_ERR;
3644 goto fsm_start;
3645 }
3646
Albert Lee71601952006-03-25 18:11:12 +08003647 /* Device should not ask for data transfer (DRQ=1)
3648 * when it finds something wrong.
3649 * Anyway, we respect DRQ here and let HSM go on
3650 * without changing hsm_task_state to HSM_ST_ERR.
3651 */
3652 if (unlikely(status & (ATA_ERR | ATA_DF))) {
3653 printk(KERN_WARNING "ata%d: DRQ=1 with device error, dev_stat 0x%X\n",
3654 ap->id, status);
3655 qc->err_mask |= AC_ERR_DEV;
3656 }
3657
Albert Leebb5cb292006-03-25 17:48:02 +08003658 /* Send the CDB (atapi) or the first data block (ata pio out).
3659 * During the state transition, interrupt handler shouldn't
3660 * be invoked before the data transfer is complete and
3661 * hsm_task_state is changed. Hence, the following locking.
3662 */
3663 if (in_wq)
3664 spin_lock_irqsave(&ap->host_set->lock, flags);
Albert Leee2cec772006-03-25 17:43:49 +08003665
Albert Leebb5cb292006-03-25 17:48:02 +08003666 if (qc->tf.protocol == ATA_PROT_PIO) {
3667 /* PIO data out protocol.
3668 * send first data block.
3669 */
3670
3671 /* ata_pio_sectors() might change the state
3672 * to HSM_ST_LAST. so, the state is changed here
3673 * before ata_pio_sectors().
3674 */
3675 ap->hsm_task_state = HSM_ST;
3676 ata_pio_sectors(qc);
3677 ata_altstatus(ap); /* flush */
3678 } else
3679 /* send CDB */
3680 atapi_send_cdb(ap, qc);
3681
3682 if (in_wq)
3683 spin_unlock_irqrestore(&ap->host_set->lock, flags);
3684
3685 /* if polling, ata_pio_task() handles the rest.
3686 * otherwise, interrupt handler takes over from here.
3687 */
Albert Leee2cec772006-03-25 17:43:49 +08003688 break;
3689
3690 case HSM_ST:
3691 /* complete command or read/write the data register */
3692 if (qc->tf.protocol == ATA_PROT_ATAPI) {
3693 /* ATAPI PIO protocol */
3694 if ((status & ATA_DRQ) == 0) {
3695 /* no more data to transfer */
3696 ap->hsm_task_state = HSM_ST_LAST;
3697 goto fsm_start;
3698 }
3699
Albert Lee71601952006-03-25 18:11:12 +08003700 /* Device should not ask for data transfer (DRQ=1)
3701 * when it finds something wrong.
3702 * Anyway, we respect DRQ here and let HSM go on
3703 * without changing hsm_task_state to HSM_ST_ERR.
3704 */
3705 if (unlikely(status & (ATA_ERR | ATA_DF))) {
3706 printk(KERN_WARNING "ata%d: DRQ=1 with device error, dev_stat 0x%X\n",
3707 ap->id, status);
3708 qc->err_mask |= AC_ERR_DEV;
3709 }
3710
Albert Leee2cec772006-03-25 17:43:49 +08003711 atapi_pio_bytes(qc);
3712
3713 if (unlikely(ap->hsm_task_state == HSM_ST_ERR))
3714 /* bad ireason reported by device */
3715 goto fsm_start;
3716
3717 } else {
3718 /* ATA PIO protocol */
3719 if (unlikely((status & ATA_DRQ) == 0)) {
3720 /* handle BSY=0, DRQ=0 as error */
3721 qc->err_mask |= AC_ERR_HSM;
3722 ap->hsm_task_state = HSM_ST_ERR;
3723 goto fsm_start;
3724 }
3725
Albert Lee71601952006-03-25 18:11:12 +08003726 /* Some devices may ask for data transfer (DRQ=1)
3727 * alone with ERR=1 for PIO reads.
3728 * We respect DRQ here and let HSM go on without
3729 * changing hsm_task_state to HSM_ST_ERR.
3730 */
3731 if (unlikely(status & (ATA_ERR | ATA_DF))) {
3732 /* For writes, ERR=1 DRQ=1 doesn't make
3733 * sense since the data block has been
3734 * transferred to the device.
3735 */
3736 WARN_ON(qc->tf.flags & ATA_TFLAG_WRITE);
3737
3738 /* data might be corrputed */
3739 qc->err_mask |= AC_ERR_DEV;
3740 }
3741
Albert Leee2cec772006-03-25 17:43:49 +08003742 ata_pio_sectors(qc);
3743
3744 if (ap->hsm_task_state == HSM_ST_LAST &&
3745 (!(qc->tf.flags & ATA_TFLAG_WRITE))) {
3746 /* all data read */
3747 ata_altstatus(ap);
Albert Lee52a32202006-03-25 18:18:15 +08003748 status = ata_wait_idle(ap);
Albert Leee2cec772006-03-25 17:43:49 +08003749 goto fsm_start;
3750 }
3751 }
3752
3753 ata_altstatus(ap); /* flush */
Albert Leebb5cb292006-03-25 17:48:02 +08003754 poll_next = 1;
Albert Leee2cec772006-03-25 17:43:49 +08003755 break;
3756
3757 case HSM_ST_LAST:
Albert Lee6912ccd2006-03-25 17:45:49 +08003758 if (unlikely(!ata_ok(status))) {
3759 qc->err_mask |= __ac_err_mask(status);
Albert Leee2cec772006-03-25 17:43:49 +08003760 ap->hsm_task_state = HSM_ST_ERR;
3761 goto fsm_start;
3762 }
3763
3764 /* no more data to transfer */
3765 DPRINTK("ata%u: command complete, drv_stat 0x%x\n",
3766 ap->id, status);
3767
Albert Lee6912ccd2006-03-25 17:45:49 +08003768 WARN_ON(qc->err_mask);
3769
Albert Leee2cec772006-03-25 17:43:49 +08003770 ap->hsm_task_state = HSM_ST_IDLE;
3771
3772 /* complete taskfile transaction */
Albert Leebb5cb292006-03-25 17:48:02 +08003773 if (in_wq)
3774 ata_poll_qc_complete(qc);
3775 else
3776 ata_qc_complete(qc);
3777
3778 poll_next = 0;
Albert Leee2cec772006-03-25 17:43:49 +08003779 break;
3780
3781 case HSM_ST_ERR:
3782 if (qc->tf.command != ATA_CMD_PACKET)
Albert Lee6912ccd2006-03-25 17:45:49 +08003783 printk(KERN_ERR "ata%u: command error, drv_stat 0x%x\n",
3784 ap->id, status);
Albert Leee2cec772006-03-25 17:43:49 +08003785
3786 /* make sure qc->err_mask is available to
3787 * know what's wrong and recover
3788 */
3789 WARN_ON(qc->err_mask == 0);
3790
3791 ap->hsm_task_state = HSM_ST_IDLE;
Albert Leebb5cb292006-03-25 17:48:02 +08003792
Albert Lee999bb6f2006-03-25 18:07:48 +08003793 /* complete taskfile transaction */
Albert Leebb5cb292006-03-25 17:48:02 +08003794 if (in_wq)
3795 ata_poll_qc_complete(qc);
3796 else
3797 ata_qc_complete(qc);
3798
3799 poll_next = 0;
Albert Leee2cec772006-03-25 17:43:49 +08003800 break;
3801 default:
Albert Leebb5cb292006-03-25 17:48:02 +08003802 poll_next = 0;
Albert Lee6912ccd2006-03-25 17:45:49 +08003803 BUG();
Albert Leee2cec772006-03-25 17:43:49 +08003804 }
3805
Albert Leebb5cb292006-03-25 17:48:02 +08003806 return poll_next;
Albert Leee2cec772006-03-25 17:43:49 +08003807}
3808
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809static void ata_pio_task(void *_data)
3810{
3811 struct ata_port *ap = _data;
Albert Leea1af3732006-03-25 17:50:15 +08003812 struct ata_queued_cmd *qc;
3813 u8 status;
3814 int poll_next;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003815
3816fsm_start:
Albert Leea1af3732006-03-25 17:50:15 +08003817 WARN_ON(ap->hsm_task_state == HSM_ST_IDLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818
Albert Leea1af3732006-03-25 17:50:15 +08003819 qc = ata_qc_from_tag(ap, ap->active_tag);
3820 WARN_ON(qc == NULL);
Albert Leee27486d2005-11-01 19:24:49 +08003821
Albert Leea1af3732006-03-25 17:50:15 +08003822 /*
3823 * This is purely heuristic. This is a fast path.
3824 * Sometimes when we enter, BSY will be cleared in
3825 * a chk-status or two. If not, the drive is probably seeking
3826 * or something. Snooze for a couple msecs, then
3827 * chk-status again. If still busy, queue delayed work.
3828 */
3829 status = ata_busy_wait(ap, ATA_BUSY, 5);
3830 if (status & ATA_BUSY) {
3831 msleep(2);
3832 status = ata_busy_wait(ap, ATA_BUSY, 10);
3833 if (status & ATA_BUSY) {
3834 ata_port_queue_task(ap, ata_pio_task, ap, ATA_SHORT_PAUSE);
3835 return;
3836 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837 }
3838
Albert Leea1af3732006-03-25 17:50:15 +08003839 /* move the HSM */
3840 poll_next = ata_hsm_move(ap, qc, status, 1);
3841
3842 /* another command or interrupt handler
3843 * may be running at this point.
3844 */
3845 if (poll_next)
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003846 goto fsm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003847}
3848
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849/**
3850 * ata_qc_timeout - Handle timeout of queued command
3851 * @qc: Command that timed out
3852 *
3853 * Some part of the kernel (currently, only the SCSI layer)
3854 * has noticed that the active command on port @ap has not
3855 * completed after a specified length of time. Handle this
3856 * condition by disabling DMA (if necessary) and completing
3857 * transactions, with error if necessary.
3858 *
3859 * This also handles the case of the "lost interrupt", where
3860 * for some reason (possibly hardware bug, possibly driver bug)
3861 * an interrupt was not delivered to the driver, even though the
3862 * transaction completed successfully.
3863 *
3864 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003865 * Inherited from SCSI layer (none, can sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003866 */
3867
3868static void ata_qc_timeout(struct ata_queued_cmd *qc)
3869{
3870 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003871 struct ata_host_set *host_set = ap->host_set;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872 u8 host_stat = 0, drv_stat;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003873 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874
3875 DPRINTK("ENTER\n");
3876
Tejun Heoc18d06f2006-02-02 00:56:10 +09003877 ap->hsm_task_state = HSM_ST_IDLE;
3878
Jeff Garzikb8f61532005-08-25 22:01:20 -04003879 spin_lock_irqsave(&host_set->lock, flags);
3880
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881 switch (qc->tf.protocol) {
3882
3883 case ATA_PROT_DMA:
3884 case ATA_PROT_ATAPI_DMA:
3885 host_stat = ap->ops->bmdma_status(ap);
3886
3887 /* before we do anything else, clear DMA-Start bit */
Alan Coxb73fc892005-08-26 16:03:19 +01003888 ap->ops->bmdma_stop(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003889
3890 /* fall through */
3891
3892 default:
3893 ata_altstatus(ap);
3894 drv_stat = ata_chk_status(ap);
3895
3896 /* ack bmdma irq events */
3897 ap->ops->irq_clear(ap);
3898
3899 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
3900 ap->id, qc->tf.command, drv_stat, host_stat);
3901
Albert Lee312f7da2005-09-27 17:38:03 +08003902 ap->hsm_task_state = HSM_ST_IDLE;
3903
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904 /* complete taskfile transaction */
Albert Lee555a8962006-02-08 16:50:29 +08003905 qc->err_mask |= AC_ERR_TIMEOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906 break;
3907 }
Jeff Garzikb8f61532005-08-25 22:01:20 -04003908
3909 spin_unlock_irqrestore(&host_set->lock, flags);
3910
Tejun Heoa72ec4c2006-01-23 13:09:37 +09003911 ata_eh_qc_complete(qc);
3912
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913 DPRINTK("EXIT\n");
3914}
3915
3916/**
3917 * ata_eng_timeout - Handle timeout of queued command
3918 * @ap: Port on which timed-out command is active
3919 *
3920 * Some part of the kernel (currently, only the SCSI layer)
3921 * has noticed that the active command on port @ap has not
3922 * completed after a specified length of time. Handle this
3923 * condition by disabling DMA (if necessary) and completing
3924 * transactions, with error if necessary.
3925 *
3926 * This also handles the case of the "lost interrupt", where
3927 * for some reason (possibly hardware bug, possibly driver bug)
3928 * an interrupt was not delivered to the driver, even though the
3929 * transaction completed successfully.
3930 *
3931 * LOCKING:
3932 * Inherited from SCSI layer (none, can sleep)
3933 */
3934
3935void ata_eng_timeout(struct ata_port *ap)
3936{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003937 DPRINTK("ENTER\n");
3938
Tejun Heof6379022006-02-10 15:10:48 +09003939 ata_qc_timeout(ata_qc_from_tag(ap, ap->active_tag));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941 DPRINTK("EXIT\n");
3942}
3943
3944/**
3945 * ata_qc_new - Request an available ATA command, for queueing
3946 * @ap: Port associated with device @dev
3947 * @dev: Device from whom we request an available command structure
3948 *
3949 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003950 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003951 */
3952
3953static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
3954{
3955 struct ata_queued_cmd *qc = NULL;
3956 unsigned int i;
3957
3958 for (i = 0; i < ATA_MAX_QUEUE; i++)
3959 if (!test_and_set_bit(i, &ap->qactive)) {
3960 qc = ata_qc_from_tag(ap, i);
3961 break;
3962 }
3963
3964 if (qc)
3965 qc->tag = i;
3966
3967 return qc;
3968}
3969
3970/**
3971 * ata_qc_new_init - Request an available ATA command, and initialize it
3972 * @ap: Port associated with device @dev
3973 * @dev: Device from whom we request an available command structure
3974 *
3975 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003976 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977 */
3978
3979struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
3980 struct ata_device *dev)
3981{
3982 struct ata_queued_cmd *qc;
3983
3984 qc = ata_qc_new(ap);
3985 if (qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986 qc->scsicmd = NULL;
3987 qc->ap = ap;
3988 qc->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05003990 ata_qc_reinit(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991 }
3992
3993 return qc;
3994}
3995
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996/**
3997 * ata_qc_free - free unused ata_queued_cmd
3998 * @qc: Command to complete
3999 *
4000 * Designed to free unused ata_queued_cmd object
4001 * in case something prevents using it.
4002 *
4003 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004004 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004005 */
4006void ata_qc_free(struct ata_queued_cmd *qc)
4007{
Tejun Heo4ba946e2006-01-23 13:09:36 +09004008 struct ata_port *ap = qc->ap;
4009 unsigned int tag;
4010
Tejun Heoa4631472006-02-11 19:11:13 +09004011 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012
Tejun Heo4ba946e2006-01-23 13:09:36 +09004013 qc->flags = 0;
4014 tag = qc->tag;
4015 if (likely(ata_tag_valid(tag))) {
4016 if (tag == ap->active_tag)
4017 ap->active_tag = ATA_TAG_POISON;
4018 qc->tag = ATA_TAG_POISON;
4019 clear_bit(tag, &ap->qactive);
4020 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004021}
4022
Tejun Heo76014422006-02-11 15:13:49 +09004023void __ata_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004024{
Tejun Heoa4631472006-02-11 19:11:13 +09004025 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
4026 WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027
4028 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
4029 ata_sg_clean(qc);
4030
Albert Lee3f3791d2005-08-16 14:25:38 +08004031 /* atapi: mark qc as inactive to prevent the interrupt handler
4032 * from completing the command twice later, before the error handler
4033 * is called. (when rc != 0 and atapi request sense is needed)
4034 */
4035 qc->flags &= ~ATA_QCFLAG_ACTIVE;
4036
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037 /* call completion callback */
Tejun Heo77853bf2006-01-23 13:09:36 +09004038 qc->complete_fn(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039}
4040
4041static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
4042{
4043 struct ata_port *ap = qc->ap;
4044
4045 switch (qc->tf.protocol) {
4046 case ATA_PROT_DMA:
4047 case ATA_PROT_ATAPI_DMA:
4048 return 1;
4049
4050 case ATA_PROT_ATAPI:
4051 case ATA_PROT_PIO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052 if (ap->flags & ATA_FLAG_PIO_DMA)
4053 return 1;
4054
4055 /* fall through */
4056
4057 default:
4058 return 0;
4059 }
4060
4061 /* never reached */
4062}
4063
4064/**
4065 * ata_qc_issue - issue taskfile to device
4066 * @qc: command to issue to device
4067 *
4068 * Prepare an ATA command to submission to device.
4069 * This includes mapping the data into a DMA-able
4070 * area, filling in the S/G table, and finally
4071 * writing the taskfile to hardware, starting the command.
4072 *
4073 * LOCKING:
4074 * spin_lock_irqsave(host_set lock)
4075 *
4076 * RETURNS:
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09004077 * Zero on success, AC_ERR_* mask on failure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078 */
4079
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09004080unsigned int ata_qc_issue(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081{
4082 struct ata_port *ap = qc->ap;
4083
4084 if (ata_should_dma_map(qc)) {
4085 if (qc->flags & ATA_QCFLAG_SG) {
4086 if (ata_sg_setup(qc))
Tejun Heo8e436af2006-01-23 13:09:36 +09004087 goto sg_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
4089 if (ata_sg_setup_one(qc))
Tejun Heo8e436af2006-01-23 13:09:36 +09004090 goto sg_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091 }
4092 } else {
4093 qc->flags &= ~ATA_QCFLAG_DMAMAP;
4094 }
4095
4096 ap->ops->qc_prep(qc);
4097
4098 qc->ap->active_tag = qc->tag;
4099 qc->flags |= ATA_QCFLAG_ACTIVE;
4100
4101 return ap->ops->qc_issue(qc);
4102
Tejun Heo8e436af2006-01-23 13:09:36 +09004103sg_err:
4104 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09004105 return AC_ERR_SYSTEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106}
4107
Edward Falk0baab862005-06-02 18:17:13 -04004108
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109/**
4110 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
4111 * @qc: command to issue to device
4112 *
4113 * Using various libata functions and hooks, this function
4114 * starts an ATA command. ATA commands are grouped into
4115 * classes called "protocols", and issuing each type of protocol
4116 * is slightly different.
4117 *
Edward Falk0baab862005-06-02 18:17:13 -04004118 * May be used as the qc_issue() entry in ata_port_operations.
4119 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120 * LOCKING:
4121 * spin_lock_irqsave(host_set lock)
4122 *
4123 * RETURNS:
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09004124 * Zero on success, AC_ERR_* mask on failure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125 */
4126
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09004127unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128{
4129 struct ata_port *ap = qc->ap;
4130
Albert Leee50362e2005-09-27 17:39:50 +08004131 /* Use polling pio if the LLD doesn't handle
4132 * interrupt driven pio and atapi CDB interrupt.
4133 */
4134 if (ap->flags & ATA_FLAG_PIO_POLLING) {
4135 switch (qc->tf.protocol) {
4136 case ATA_PROT_PIO:
4137 case ATA_PROT_ATAPI:
4138 case ATA_PROT_ATAPI_NODATA:
4139 qc->tf.flags |= ATA_TFLAG_POLLING;
4140 break;
4141 case ATA_PROT_ATAPI_DMA:
4142 if (qc->dev->flags & ATA_DFLAG_CDB_INTR)
Albert Leec2bbc552006-03-25 17:56:55 +08004143 /* see ata_check_atapi_dma() */
Albert Leee50362e2005-09-27 17:39:50 +08004144 BUG();
4145 break;
4146 default:
4147 break;
4148 }
4149 }
4150
Albert Lee312f7da2005-09-27 17:38:03 +08004151 /* select the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152 ata_dev_select(ap, qc->dev->devno, 1, 0);
4153
Albert Lee312f7da2005-09-27 17:38:03 +08004154 /* start the command */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 switch (qc->tf.protocol) {
4156 case ATA_PROT_NODATA:
Albert Lee312f7da2005-09-27 17:38:03 +08004157 if (qc->tf.flags & ATA_TFLAG_POLLING)
4158 ata_qc_set_polling(qc);
4159
Jeff Garzike5338252005-10-30 21:37:17 -05004160 ata_tf_to_host(ap, &qc->tf);
Albert Lee312f7da2005-09-27 17:38:03 +08004161 ap->hsm_task_state = HSM_ST_LAST;
4162
4163 if (qc->tf.flags & ATA_TFLAG_POLLING)
Jeff Garzik46e202e2006-03-11 19:25:47 -05004164 ata_port_queue_task(ap, ata_pio_task, ap, 0);
Albert Lee312f7da2005-09-27 17:38:03 +08004165
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166 break;
4167
4168 case ATA_PROT_DMA:
Jeff Garzik587005d2006-02-11 18:17:32 -05004169 WARN_ON(qc->tf.flags & ATA_TFLAG_POLLING);
Albert Lee312f7da2005-09-27 17:38:03 +08004170
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
4172 ap->ops->bmdma_setup(qc); /* set up bmdma */
4173 ap->ops->bmdma_start(qc); /* initiate bmdma */
Albert Lee312f7da2005-09-27 17:38:03 +08004174 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175 break;
4176
Albert Lee312f7da2005-09-27 17:38:03 +08004177 case ATA_PROT_PIO:
4178 if (qc->tf.flags & ATA_TFLAG_POLLING)
4179 ata_qc_set_polling(qc);
4180
Jeff Garzike5338252005-10-30 21:37:17 -05004181 ata_tf_to_host(ap, &qc->tf);
Albert Lee312f7da2005-09-27 17:38:03 +08004182
Albert Lee54f00382005-09-30 19:14:19 +08004183 if (qc->tf.flags & ATA_TFLAG_WRITE) {
4184 /* PIO data out protocol */
4185 ap->hsm_task_state = HSM_ST_FIRST;
Jeff Garzikce1e7a22006-03-11 19:21:17 -05004186 ata_port_queue_task(ap, ata_pio_task, ap, 0);
Albert Lee54f00382005-09-30 19:14:19 +08004187
4188 /* always send first data block using
Albert Leee27486d2005-11-01 19:24:49 +08004189 * the ata_pio_task() codepath.
Albert Lee54f00382005-09-30 19:14:19 +08004190 */
Albert Lee312f7da2005-09-27 17:38:03 +08004191 } else {
Albert Lee54f00382005-09-30 19:14:19 +08004192 /* PIO data in protocol */
4193 ap->hsm_task_state = HSM_ST;
Albert Lee312f7da2005-09-27 17:38:03 +08004194
Albert Lee54f00382005-09-30 19:14:19 +08004195 if (qc->tf.flags & ATA_TFLAG_POLLING)
Jeff Garzikce1e7a22006-03-11 19:21:17 -05004196 ata_port_queue_task(ap, ata_pio_task, ap, 0);
Albert Lee312f7da2005-09-27 17:38:03 +08004197
Albert Lee54f00382005-09-30 19:14:19 +08004198 /* if polling, ata_pio_task() handles the rest.
4199 * otherwise, interrupt handler takes over from here.
4200 */
Albert Lee312f7da2005-09-27 17:38:03 +08004201 }
4202
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203 break;
4204
4205 case ATA_PROT_ATAPI:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206 case ATA_PROT_ATAPI_NODATA:
Albert Lee312f7da2005-09-27 17:38:03 +08004207 if (qc->tf.flags & ATA_TFLAG_POLLING)
4208 ata_qc_set_polling(qc);
4209
Jeff Garzike5338252005-10-30 21:37:17 -05004210 ata_tf_to_host(ap, &qc->tf);
Jeff Garzikf6ef65e2006-01-27 02:45:00 -05004211
Albert Lee312f7da2005-09-27 17:38:03 +08004212 ap->hsm_task_state = HSM_ST_FIRST;
4213
4214 /* send cdb by polling if no cdb interrupt */
4215 if ((!(qc->dev->flags & ATA_DFLAG_CDB_INTR)) ||
4216 (qc->tf.flags & ATA_TFLAG_POLLING))
Albert Lee13ee4622006-03-25 17:39:34 +08004217 ata_port_queue_task(ap, ata_pio_task, ap, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218 break;
4219
4220 case ATA_PROT_ATAPI_DMA:
Jeff Garzik587005d2006-02-11 18:17:32 -05004221 WARN_ON(qc->tf.flags & ATA_TFLAG_POLLING);
Albert Lee312f7da2005-09-27 17:38:03 +08004222
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
4224 ap->ops->bmdma_setup(qc); /* set up bmdma */
Albert Lee312f7da2005-09-27 17:38:03 +08004225 ap->hsm_task_state = HSM_ST_FIRST;
4226
4227 /* send cdb by polling if no cdb interrupt */
4228 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
Albert Lee13ee4622006-03-25 17:39:34 +08004229 ata_port_queue_task(ap, ata_pio_task, ap, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230 break;
4231
4232 default:
4233 WARN_ON(1);
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09004234 return AC_ERR_SYSTEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235 }
4236
4237 return 0;
4238}
4239
4240/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241 * ata_host_intr - Handle host interrupt for given (port, task)
4242 * @ap: Port on which interrupt arrived (possibly...)
4243 * @qc: Taskfile currently active in engine
4244 *
4245 * Handle host interrupt for given queued command. Currently,
4246 * only DMA interrupts are handled. All other commands are
4247 * handled via polling with interrupts disabled (nIEN bit).
4248 *
4249 * LOCKING:
4250 * spin_lock_irqsave(host_set lock)
4251 *
4252 * RETURNS:
4253 * One if interrupt was handled, zero if not (shared irq).
4254 */
4255
4256inline unsigned int ata_host_intr (struct ata_port *ap,
4257 struct ata_queued_cmd *qc)
4258{
Albert Lee312f7da2005-09-27 17:38:03 +08004259 u8 status, host_stat = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260
Albert Lee312f7da2005-09-27 17:38:03 +08004261 VPRINTK("ata%u: protocol %d task_state %d\n",
4262 ap->id, qc->tf.protocol, ap->hsm_task_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263
Albert Lee312f7da2005-09-27 17:38:03 +08004264 /* Check whether we are expecting interrupt in this state */
4265 switch (ap->hsm_task_state) {
4266 case HSM_ST_FIRST:
Albert Lee6912ccd2006-03-25 17:45:49 +08004267 /* Some pre-ATAPI-4 devices assert INTRQ
4268 * at this state when ready to receive CDB.
4269 */
4270
Albert Lee312f7da2005-09-27 17:38:03 +08004271 /* Check the ATA_DFLAG_CDB_INTR flag is enough here.
4272 * The flag was turned on only for atapi devices.
4273 * No need to check is_atapi_taskfile(&qc->tf) again.
4274 */
4275 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004276 goto idle_irq;
Albert Lee312f7da2005-09-27 17:38:03 +08004277 break;
4278 case HSM_ST_LAST:
4279 if (qc->tf.protocol == ATA_PROT_DMA ||
4280 qc->tf.protocol == ATA_PROT_ATAPI_DMA) {
4281 /* check status of DMA engine */
4282 host_stat = ap->ops->bmdma_status(ap);
4283 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284
Albert Lee312f7da2005-09-27 17:38:03 +08004285 /* if it's not our irq... */
4286 if (!(host_stat & ATA_DMA_INTR))
4287 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288
Albert Lee312f7da2005-09-27 17:38:03 +08004289 /* before we do anything else, clear DMA-Start bit */
4290 ap->ops->bmdma_stop(qc);
Albert Leea4f16612005-12-26 16:40:53 +08004291
4292 if (unlikely(host_stat & ATA_DMA_ERR)) {
4293 /* error when transfering data to/from memory */
4294 qc->err_mask |= AC_ERR_HOST_BUS;
4295 ap->hsm_task_state = HSM_ST_ERR;
4296 }
Albert Lee312f7da2005-09-27 17:38:03 +08004297 }
4298 break;
4299 case HSM_ST:
4300 break;
4301 default:
4302 goto idle_irq;
4303 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304
Albert Lee312f7da2005-09-27 17:38:03 +08004305 /* check altstatus */
4306 status = ata_altstatus(ap);
4307 if (status & ATA_BUSY)
4308 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309
Albert Lee312f7da2005-09-27 17:38:03 +08004310 /* check main status, clearing INTRQ */
4311 status = ata_chk_status(ap);
4312 if (unlikely(status & ATA_BUSY))
4313 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314
Albert Lee312f7da2005-09-27 17:38:03 +08004315 /* ack bmdma irq events */
4316 ap->ops->irq_clear(ap);
4317
Albert Leebb5cb292006-03-25 17:48:02 +08004318 ata_hsm_move(ap, qc, status, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319 return 1; /* irq handled */
4320
4321idle_irq:
4322 ap->stats.idle_irq++;
4323
4324#ifdef ATA_IRQ_TRAP
4325 if ((ap->stats.idle_irq % 1000) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326 ata_irq_ack(ap, 0); /* debug trap */
4327 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
Alan Cox23cfce82006-03-21 16:06:53 +00004328 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329 }
4330#endif
4331 return 0; /* irq not handled */
4332}
4333
4334/**
4335 * ata_interrupt - Default ATA host interrupt handler
Jeff Garzik0cba6322005-05-30 19:49:12 -04004336 * @irq: irq line (unused)
4337 * @dev_instance: pointer to our ata_host_set information structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338 * @regs: unused
4339 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004340 * Default interrupt handler for PCI IDE devices. Calls
4341 * ata_host_intr() for each port that is not disabled.
4342 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004344 * Obtains host_set lock during operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345 *
4346 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004347 * IRQ_NONE or IRQ_HANDLED.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348 */
4349
4350irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
4351{
4352 struct ata_host_set *host_set = dev_instance;
4353 unsigned int i;
4354 unsigned int handled = 0;
4355 unsigned long flags;
4356
4357 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
4358 spin_lock_irqsave(&host_set->lock, flags);
4359
4360 for (i = 0; i < host_set->n_ports; i++) {
4361 struct ata_port *ap;
4362
4363 ap = host_set->ports[i];
Tejun Heoc1389502005-08-22 14:59:24 +09004364 if (ap &&
Albert Lee312f7da2005-09-27 17:38:03 +08004365 !(ap->flags & ATA_FLAG_PORT_DISABLED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366 struct ata_queued_cmd *qc;
4367
4368 qc = ata_qc_from_tag(ap, ap->active_tag);
Albert Lee312f7da2005-09-27 17:38:03 +08004369 if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING)) &&
Albert Lee21b1ed72005-04-29 17:34:59 +08004370 (qc->flags & ATA_QCFLAG_ACTIVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371 handled |= ata_host_intr(ap, qc);
4372 }
4373 }
4374
4375 spin_unlock_irqrestore(&host_set->lock, flags);
4376
4377 return IRQ_RETVAL(handled);
4378}
4379
Edward Falk0baab862005-06-02 18:17:13 -04004380
Jens Axboe9b847542006-01-06 09:28:07 +01004381/*
4382 * Execute a 'simple' command, that only consists of the opcode 'cmd' itself,
4383 * without filling any other registers
4384 */
4385static int ata_do_simple_cmd(struct ata_port *ap, struct ata_device *dev,
4386 u8 cmd)
4387{
4388 struct ata_taskfile tf;
4389 int err;
4390
4391 ata_tf_init(ap, &tf, dev->devno);
4392
4393 tf.command = cmd;
4394 tf.flags |= ATA_TFLAG_DEVICE;
4395 tf.protocol = ATA_PROT_NODATA;
4396
4397 err = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
4398 if (err)
4399 printk(KERN_ERR "%s: ata command failed: %d\n",
4400 __FUNCTION__, err);
4401
4402 return err;
4403}
4404
4405static int ata_flush_cache(struct ata_port *ap, struct ata_device *dev)
4406{
4407 u8 cmd;
4408
4409 if (!ata_try_flush_cache(dev))
4410 return 0;
4411
4412 if (ata_id_has_flush_ext(dev->id))
4413 cmd = ATA_CMD_FLUSH_EXT;
4414 else
4415 cmd = ATA_CMD_FLUSH;
4416
4417 return ata_do_simple_cmd(ap, dev, cmd);
4418}
4419
4420static int ata_standby_drive(struct ata_port *ap, struct ata_device *dev)
4421{
4422 return ata_do_simple_cmd(ap, dev, ATA_CMD_STANDBYNOW1);
4423}
4424
4425static int ata_start_drive(struct ata_port *ap, struct ata_device *dev)
4426{
4427 return ata_do_simple_cmd(ap, dev, ATA_CMD_IDLEIMMEDIATE);
4428}
4429
4430/**
4431 * ata_device_resume - wakeup a previously suspended devices
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004432 * @ap: port the device is connected to
4433 * @dev: the device to resume
Jens Axboe9b847542006-01-06 09:28:07 +01004434 *
4435 * Kick the drive back into action, by sending it an idle immediate
4436 * command and making sure its transfer mode matches between drive
4437 * and host.
4438 *
4439 */
4440int ata_device_resume(struct ata_port *ap, struct ata_device *dev)
4441{
4442 if (ap->flags & ATA_FLAG_SUSPENDED) {
4443 ap->flags &= ~ATA_FLAG_SUSPENDED;
4444 ata_set_mode(ap);
4445 }
4446 if (!ata_dev_present(dev))
4447 return 0;
4448 if (dev->class == ATA_DEV_ATA)
4449 ata_start_drive(ap, dev);
4450
4451 return 0;
4452}
4453
4454/**
4455 * ata_device_suspend - prepare a device for suspend
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004456 * @ap: port the device is connected to
4457 * @dev: the device to suspend
Jens Axboe9b847542006-01-06 09:28:07 +01004458 *
4459 * Flush the cache on the drive, if appropriate, then issue a
4460 * standbynow command.
Jens Axboe9b847542006-01-06 09:28:07 +01004461 */
Nigel Cunningham082776e2006-03-23 23:22:16 +10004462int ata_device_suspend(struct ata_port *ap, struct ata_device *dev, pm_message_t state)
Jens Axboe9b847542006-01-06 09:28:07 +01004463{
4464 if (!ata_dev_present(dev))
4465 return 0;
4466 if (dev->class == ATA_DEV_ATA)
4467 ata_flush_cache(ap, dev);
4468
Nigel Cunningham082776e2006-03-23 23:22:16 +10004469 if (state.event != PM_EVENT_FREEZE)
4470 ata_standby_drive(ap, dev);
Jens Axboe9b847542006-01-06 09:28:07 +01004471 ap->flags |= ATA_FLAG_SUSPENDED;
4472 return 0;
4473}
4474
Albert Lee332b5a52006-02-08 16:51:34 +08004475/**
4476 * ata_port_start - Set port up for dma.
4477 * @ap: Port to initialize
4478 *
4479 * Called just after data structures for each port are
4480 * initialized. Allocates space for PRD table.
4481 *
4482 * May be used as the port_start() entry in ata_port_operations.
4483 *
4484 * LOCKING:
4485 * Inherited from caller.
4486 */
4487
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488int ata_port_start (struct ata_port *ap)
4489{
Brian King2f1f6102006-03-23 17:30:15 -06004490 struct device *dev = ap->dev;
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004491 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492
4493 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
4494 if (!ap->prd)
4495 return -ENOMEM;
4496
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004497 rc = ata_pad_alloc(ap, dev);
4498 if (rc) {
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004499 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004500 return rc;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004501 }
4502
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
4504
4505 return 0;
4506}
4507
Edward Falk0baab862005-06-02 18:17:13 -04004508
4509/**
4510 * ata_port_stop - Undo ata_port_start()
4511 * @ap: Port to shut down
4512 *
4513 * Frees the PRD table.
4514 *
4515 * May be used as the port_stop() entry in ata_port_operations.
4516 *
4517 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004518 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04004519 */
4520
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521void ata_port_stop (struct ata_port *ap)
4522{
Brian King2f1f6102006-03-23 17:30:15 -06004523 struct device *dev = ap->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524
4525 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004526 ata_pad_free(ap, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527}
4528
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04004529void ata_host_stop (struct ata_host_set *host_set)
4530{
4531 if (host_set->mmio_base)
4532 iounmap(host_set->mmio_base);
4533}
4534
4535
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536/**
4537 * ata_host_remove - Unregister SCSI host structure with upper layers
4538 * @ap: Port to unregister
4539 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
4540 *
4541 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004542 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543 */
4544
4545static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
4546{
4547 struct Scsi_Host *sh = ap->host;
4548
4549 DPRINTK("ENTER\n");
4550
4551 if (do_unregister)
4552 scsi_remove_host(sh);
4553
4554 ap->ops->port_stop(ap);
4555}
4556
4557/**
4558 * ata_host_init - Initialize an ata_port structure
4559 * @ap: Structure to initialize
4560 * @host: associated SCSI mid-layer structure
4561 * @host_set: Collection of hosts to which @ap belongs
4562 * @ent: Probe information provided by low-level driver
4563 * @port_no: Port number associated with this ata_port
4564 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004565 * Initialize a new ata_port structure, and its associated
4566 * scsi_host.
4567 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004569 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570 */
4571
4572static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
4573 struct ata_host_set *host_set,
Jeff Garzik057ace52005-10-22 14:27:05 -04004574 const struct ata_probe_ent *ent, unsigned int port_no)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575{
4576 unsigned int i;
4577
4578 host->max_id = 16;
4579 host->max_lun = 1;
4580 host->max_channel = 1;
4581 host->unique_id = ata_unique_id++;
4582 host->max_cmd_len = 12;
Christoph Hellwig12413192005-06-11 01:05:01 +02004583
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584 ap->flags = ATA_FLAG_PORT_DISABLED;
4585 ap->id = host->unique_id;
4586 ap->host = host;
4587 ap->ctl = ATA_DEVCTL_OBS;
4588 ap->host_set = host_set;
Brian King2f1f6102006-03-23 17:30:15 -06004589 ap->dev = ent->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590 ap->port_no = port_no;
4591 ap->hard_port_no =
4592 ent->legacy_mode ? ent->hard_port_no : port_no;
4593 ap->pio_mask = ent->pio_mask;
4594 ap->mwdma_mask = ent->mwdma_mask;
4595 ap->udma_mask = ent->udma_mask;
4596 ap->flags |= ent->host_flags;
4597 ap->ops = ent->port_ops;
4598 ap->cbl = ATA_CBL_NONE;
4599 ap->active_tag = ATA_TAG_POISON;
4600 ap->last_ctl = 0xFF;
4601
Tejun Heo86e45b62006-03-05 15:29:09 +09004602 INIT_WORK(&ap->port_task, NULL, NULL);
Tejun Heoa72ec4c2006-01-23 13:09:37 +09004603 INIT_LIST_HEAD(&ap->eh_done_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604
Tejun Heoacf356b2006-03-24 14:07:50 +09004605 for (i = 0; i < ATA_MAX_DEVICES; i++) {
4606 struct ata_device *dev = &ap->device[i];
4607 dev->devno = i;
4608 dev->pio_mask = UINT_MAX;
4609 dev->mwdma_mask = UINT_MAX;
4610 dev->udma_mask = UINT_MAX;
4611 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612
4613#ifdef ATA_IRQ_TRAP
4614 ap->stats.unhandled_irq = 1;
4615 ap->stats.idle_irq = 1;
4616#endif
4617
4618 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
4619}
4620
4621/**
4622 * ata_host_add - Attach low-level ATA driver to system
4623 * @ent: Information provided by low-level driver
4624 * @host_set: Collections of ports to which we add
4625 * @port_no: Port number associated with this host
4626 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004627 * Attach low-level ATA driver to system.
4628 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004630 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631 *
4632 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004633 * New ata_port on success, for NULL on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634 */
4635
Jeff Garzik057ace52005-10-22 14:27:05 -04004636static struct ata_port * ata_host_add(const struct ata_probe_ent *ent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637 struct ata_host_set *host_set,
4638 unsigned int port_no)
4639{
4640 struct Scsi_Host *host;
4641 struct ata_port *ap;
4642 int rc;
4643
4644 DPRINTK("ENTER\n");
Tejun Heoaec5c3c2006-03-25 01:33:34 +09004645
4646 if (!ent->port_ops->probe_reset &&
4647 !(ent->host_flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST))) {
4648 printk(KERN_ERR "ata%u: no reset mechanism available\n",
4649 port_no);
4650 return NULL;
4651 }
4652
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
4654 if (!host)
4655 return NULL;
4656
Tejun Heo30afc842006-03-18 18:40:14 +09004657 host->transportt = &ata_scsi_transport_template;
4658
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659 ap = (struct ata_port *) &host->hostdata[0];
4660
4661 ata_host_init(ap, host, host_set, ent, port_no);
4662
4663 rc = ap->ops->port_start(ap);
4664 if (rc)
4665 goto err_out;
4666
4667 return ap;
4668
4669err_out:
4670 scsi_host_put(host);
4671 return NULL;
4672}
4673
4674/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004675 * ata_device_add - Register hardware device with ATA and SCSI layers
4676 * @ent: Probe information describing hardware device to be registered
4677 *
4678 * This function processes the information provided in the probe
4679 * information struct @ent, allocates the necessary ATA and SCSI
4680 * host information structures, initializes them, and registers
4681 * everything with requisite kernel subsystems.
4682 *
4683 * This function requests irqs, probes the ATA bus, and probes
4684 * the SCSI bus.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685 *
4686 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004687 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688 *
4689 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004690 * Number of ports registered. Zero on error (no ports registered).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691 */
4692
Jeff Garzik057ace52005-10-22 14:27:05 -04004693int ata_device_add(const struct ata_probe_ent *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694{
4695 unsigned int count = 0, i;
4696 struct device *dev = ent->dev;
4697 struct ata_host_set *host_set;
4698
4699 DPRINTK("ENTER\n");
4700 /* alloc a container for our list of ATA ports (buses) */
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004701 host_set = kzalloc(sizeof(struct ata_host_set) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004702 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
4703 if (!host_set)
4704 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705 spin_lock_init(&host_set->lock);
4706
4707 host_set->dev = dev;
4708 host_set->n_ports = ent->n_ports;
4709 host_set->irq = ent->irq;
4710 host_set->mmio_base = ent->mmio_base;
4711 host_set->private_data = ent->private_data;
4712 host_set->ops = ent->port_ops;
Alan Cox5444a6f2006-03-27 18:58:20 +01004713 host_set->flags = ent->host_set_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714
4715 /* register each port bound to this device */
4716 for (i = 0; i < ent->n_ports; i++) {
4717 struct ata_port *ap;
4718 unsigned long xfer_mode_mask;
4719
4720 ap = ata_host_add(ent, host_set, i);
4721 if (!ap)
4722 goto err_out;
4723
4724 host_set->ports[i] = ap;
4725 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
4726 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
4727 (ap->pio_mask << ATA_SHIFT_PIO);
4728
4729 /* print per-port info to dmesg */
4730 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
4731 "bmdma 0x%lX irq %lu\n",
4732 ap->id,
4733 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
4734 ata_mode_string(xfer_mode_mask),
4735 ap->ioaddr.cmd_addr,
4736 ap->ioaddr.ctl_addr,
4737 ap->ioaddr.bmdma_addr,
4738 ent->irq);
4739
4740 ata_chk_status(ap);
4741 host_set->ops->irq_clear(ap);
4742 count++;
4743 }
4744
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004745 if (!count)
4746 goto err_free_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747
4748 /* obtain irq, that is shared between channels */
4749 if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
4750 DRV_NAME, host_set))
4751 goto err_out;
4752
4753 /* perform each probe synchronously */
4754 DPRINTK("probe begin\n");
4755 for (i = 0; i < count; i++) {
4756 struct ata_port *ap;
4757 int rc;
4758
4759 ap = host_set->ports[i];
4760
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004761 DPRINTK("ata%u: bus probe begin\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004762 rc = ata_bus_probe(ap);
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004763 DPRINTK("ata%u: bus probe end\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764
4765 if (rc) {
4766 /* FIXME: do something useful here?
4767 * Current libata behavior will
4768 * tear down everything when
4769 * the module is removed
4770 * or the h/w is unplugged.
4771 */
4772 }
4773
4774 rc = scsi_add_host(ap->host, dev);
4775 if (rc) {
4776 printk(KERN_ERR "ata%u: scsi_add_host failed\n",
4777 ap->id);
4778 /* FIXME: do something useful here */
4779 /* FIXME: handle unconditional calls to
4780 * scsi_scan_host and ata_host_remove, below,
4781 * at the very least
4782 */
4783 }
4784 }
4785
4786 /* probes are done, now scan each port's disk(s) */
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004787 DPRINTK("host probe begin\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788 for (i = 0; i < count; i++) {
4789 struct ata_port *ap = host_set->ports[i];
4790
Jeff Garzik644dd0c2005-10-03 15:55:19 -04004791 ata_scsi_scan_host(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792 }
4793
4794 dev_set_drvdata(dev, host_set);
4795
4796 VPRINTK("EXIT, returning %u\n", ent->n_ports);
4797 return ent->n_ports; /* success */
4798
4799err_out:
4800 for (i = 0; i < count; i++) {
4801 ata_host_remove(host_set->ports[i], 1);
4802 scsi_host_put(host_set->ports[i]->host);
4803 }
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004804err_free_ret:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805 kfree(host_set);
4806 VPRINTK("EXIT, returning 0\n");
4807 return 0;
4808}
4809
4810/**
Alan Cox17b14452005-09-15 15:44:00 +01004811 * ata_host_set_remove - PCI layer callback for device removal
4812 * @host_set: ATA host set that was removed
4813 *
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05004814 * Unregister all objects associated with this host set. Free those
Alan Cox17b14452005-09-15 15:44:00 +01004815 * objects.
4816 *
4817 * LOCKING:
4818 * Inherited from calling layer (may sleep).
4819 */
4820
Alan Cox17b14452005-09-15 15:44:00 +01004821void ata_host_set_remove(struct ata_host_set *host_set)
4822{
4823 struct ata_port *ap;
4824 unsigned int i;
4825
4826 for (i = 0; i < host_set->n_ports; i++) {
4827 ap = host_set->ports[i];
4828 scsi_remove_host(ap->host);
4829 }
4830
4831 free_irq(host_set->irq, host_set);
4832
4833 for (i = 0; i < host_set->n_ports; i++) {
4834 ap = host_set->ports[i];
4835
4836 ata_scsi_release(ap->host);
4837
4838 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
4839 struct ata_ioports *ioaddr = &ap->ioaddr;
4840
4841 if (ioaddr->cmd_addr == 0x1f0)
4842 release_region(0x1f0, 8);
4843 else if (ioaddr->cmd_addr == 0x170)
4844 release_region(0x170, 8);
4845 }
4846
4847 scsi_host_put(ap->host);
4848 }
4849
4850 if (host_set->ops->host_stop)
4851 host_set->ops->host_stop(host_set);
4852
4853 kfree(host_set);
4854}
4855
4856/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857 * ata_scsi_release - SCSI layer callback hook for host unload
4858 * @host: libata host to be unloaded
4859 *
4860 * Performs all duties necessary to shut down a libata port...
4861 * Kill port kthread, disable port, and release resources.
4862 *
4863 * LOCKING:
4864 * Inherited from SCSI layer.
4865 *
4866 * RETURNS:
4867 * One.
4868 */
4869
4870int ata_scsi_release(struct Scsi_Host *host)
4871{
4872 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
Tejun Heod9572b12006-03-01 16:09:35 +09004873 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874
4875 DPRINTK("ENTER\n");
4876
4877 ap->ops->port_disable(ap);
4878 ata_host_remove(ap, 0);
Tejun Heod9572b12006-03-01 16:09:35 +09004879 for (i = 0; i < ATA_MAX_DEVICES; i++)
4880 kfree(ap->device[i].id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881
4882 DPRINTK("EXIT\n");
4883 return 1;
4884}
4885
4886/**
4887 * ata_std_ports - initialize ioaddr with standard port offsets.
4888 * @ioaddr: IO address structure to be initialized
Edward Falk0baab862005-06-02 18:17:13 -04004889 *
4890 * Utility function which initializes data_addr, error_addr,
4891 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
4892 * device_addr, status_addr, and command_addr to standard offsets
4893 * relative to cmd_addr.
4894 *
4895 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896 */
Edward Falk0baab862005-06-02 18:17:13 -04004897
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898void ata_std_ports(struct ata_ioports *ioaddr)
4899{
4900 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
4901 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
4902 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
4903 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
4904 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
4905 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
4906 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
4907 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
4908 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
4909 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
4910}
4911
Edward Falk0baab862005-06-02 18:17:13 -04004912
Jeff Garzik374b1872005-08-30 05:42:52 -04004913#ifdef CONFIG_PCI
4914
4915void ata_pci_host_stop (struct ata_host_set *host_set)
4916{
4917 struct pci_dev *pdev = to_pci_dev(host_set->dev);
4918
4919 pci_iounmap(pdev, host_set->mmio_base);
4920}
4921
Edward Falk0baab862005-06-02 18:17:13 -04004922/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923 * ata_pci_remove_one - PCI layer callback for device removal
4924 * @pdev: PCI device that was removed
4925 *
4926 * PCI layer indicates to libata via this hook that
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004927 * hot-unplug or module unload event has occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928 * Handle this by unregistering all objects associated
4929 * with this PCI device. Free those objects. Then finally
4930 * release PCI resources and disable device.
4931 *
4932 * LOCKING:
4933 * Inherited from PCI layer (may sleep).
4934 */
4935
4936void ata_pci_remove_one (struct pci_dev *pdev)
4937{
4938 struct device *dev = pci_dev_to_dev(pdev);
4939 struct ata_host_set *host_set = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940
Alan Cox17b14452005-09-15 15:44:00 +01004941 ata_host_set_remove(host_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942 pci_release_regions(pdev);
4943 pci_disable_device(pdev);
4944 dev_set_drvdata(dev, NULL);
4945}
4946
4947/* move to PCI subsystem */
Jeff Garzik057ace52005-10-22 14:27:05 -04004948int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949{
4950 unsigned long tmp = 0;
4951
4952 switch (bits->width) {
4953 case 1: {
4954 u8 tmp8 = 0;
4955 pci_read_config_byte(pdev, bits->reg, &tmp8);
4956 tmp = tmp8;
4957 break;
4958 }
4959 case 2: {
4960 u16 tmp16 = 0;
4961 pci_read_config_word(pdev, bits->reg, &tmp16);
4962 tmp = tmp16;
4963 break;
4964 }
4965 case 4: {
4966 u32 tmp32 = 0;
4967 pci_read_config_dword(pdev, bits->reg, &tmp32);
4968 tmp = tmp32;
4969 break;
4970 }
4971
4972 default:
4973 return -EINVAL;
4974 }
4975
4976 tmp &= bits->mask;
4977
4978 return (tmp == bits->val) ? 1 : 0;
4979}
Jens Axboe9b847542006-01-06 09:28:07 +01004980
4981int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t state)
4982{
4983 pci_save_state(pdev);
4984 pci_disable_device(pdev);
4985 pci_set_power_state(pdev, PCI_D3hot);
4986 return 0;
4987}
4988
4989int ata_pci_device_resume(struct pci_dev *pdev)
4990{
4991 pci_set_power_state(pdev, PCI_D0);
4992 pci_restore_state(pdev);
4993 pci_enable_device(pdev);
4994 pci_set_master(pdev);
4995 return 0;
4996}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997#endif /* CONFIG_PCI */
4998
4999
Linus Torvalds1da177e2005-04-16 15:20:36 -07005000static int __init ata_init(void)
5001{
5002 ata_wq = create_workqueue("ata");
5003 if (!ata_wq)
5004 return -ENOMEM;
5005
5006 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
5007 return 0;
5008}
5009
5010static void __exit ata_exit(void)
5011{
5012 destroy_workqueue(ata_wq);
5013}
5014
5015module_init(ata_init);
5016module_exit(ata_exit);
5017
Jeff Garzik67846b32005-10-05 02:58:32 -04005018static unsigned long ratelimit_time;
5019static spinlock_t ata_ratelimit_lock = SPIN_LOCK_UNLOCKED;
5020
5021int ata_ratelimit(void)
5022{
5023 int rc;
5024 unsigned long flags;
5025
5026 spin_lock_irqsave(&ata_ratelimit_lock, flags);
5027
5028 if (time_after(jiffies, ratelimit_time)) {
5029 rc = 1;
5030 ratelimit_time = jiffies + (HZ/5);
5031 } else
5032 rc = 0;
5033
5034 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
5035
5036 return rc;
5037}
5038
Linus Torvalds1da177e2005-04-16 15:20:36 -07005039/*
5040 * libata is essentially a library of internal helper functions for
5041 * low-level ATA host controller drivers. As such, the API/ABI is
5042 * likely to change as new drivers are added and updated.
5043 * Do not depend on ABI/API stability.
5044 */
5045
5046EXPORT_SYMBOL_GPL(ata_std_bios_param);
5047EXPORT_SYMBOL_GPL(ata_std_ports);
5048EXPORT_SYMBOL_GPL(ata_device_add);
Alan Cox17b14452005-09-15 15:44:00 +01005049EXPORT_SYMBOL_GPL(ata_host_set_remove);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005050EXPORT_SYMBOL_GPL(ata_sg_init);
5051EXPORT_SYMBOL_GPL(ata_sg_init_one);
Tejun Heo76014422006-02-11 15:13:49 +09005052EXPORT_SYMBOL_GPL(__ata_qc_complete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
5054EXPORT_SYMBOL_GPL(ata_eng_timeout);
5055EXPORT_SYMBOL_GPL(ata_tf_load);
5056EXPORT_SYMBOL_GPL(ata_tf_read);
5057EXPORT_SYMBOL_GPL(ata_noop_dev_select);
5058EXPORT_SYMBOL_GPL(ata_std_dev_select);
5059EXPORT_SYMBOL_GPL(ata_tf_to_fis);
5060EXPORT_SYMBOL_GPL(ata_tf_from_fis);
5061EXPORT_SYMBOL_GPL(ata_check_status);
5062EXPORT_SYMBOL_GPL(ata_altstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063EXPORT_SYMBOL_GPL(ata_exec_command);
5064EXPORT_SYMBOL_GPL(ata_port_start);
5065EXPORT_SYMBOL_GPL(ata_port_stop);
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04005066EXPORT_SYMBOL_GPL(ata_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067EXPORT_SYMBOL_GPL(ata_interrupt);
5068EXPORT_SYMBOL_GPL(ata_qc_prep);
Brian Kinge46834c2006-03-17 17:04:03 -06005069EXPORT_SYMBOL_GPL(ata_noop_qc_prep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070EXPORT_SYMBOL_GPL(ata_bmdma_setup);
5071EXPORT_SYMBOL_GPL(ata_bmdma_start);
5072EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
5073EXPORT_SYMBOL_GPL(ata_bmdma_status);
5074EXPORT_SYMBOL_GPL(ata_bmdma_stop);
5075EXPORT_SYMBOL_GPL(ata_port_probe);
5076EXPORT_SYMBOL_GPL(sata_phy_reset);
5077EXPORT_SYMBOL_GPL(__sata_phy_reset);
5078EXPORT_SYMBOL_GPL(ata_bus_reset);
Tejun Heo8a19ac82006-02-02 18:20:00 +09005079EXPORT_SYMBOL_GPL(ata_std_probeinit);
Tejun Heoc2bd5802006-01-24 17:05:22 +09005080EXPORT_SYMBOL_GPL(ata_std_softreset);
5081EXPORT_SYMBOL_GPL(sata_std_hardreset);
5082EXPORT_SYMBOL_GPL(ata_std_postreset);
5083EXPORT_SYMBOL_GPL(ata_std_probe_reset);
Tejun Heoa62c0fc2006-01-24 17:05:22 +09005084EXPORT_SYMBOL_GPL(ata_drive_probe_reset);
Tejun Heo623a3122006-03-05 17:55:58 +09005085EXPORT_SYMBOL_GPL(ata_dev_revalidate);
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05005086EXPORT_SYMBOL_GPL(ata_dev_classify);
5087EXPORT_SYMBOL_GPL(ata_dev_pair);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088EXPORT_SYMBOL_GPL(ata_port_disable);
Jeff Garzik67846b32005-10-05 02:58:32 -04005089EXPORT_SYMBOL_GPL(ata_ratelimit);
Tejun Heo6f8b9952006-01-24 17:05:21 +09005090EXPORT_SYMBOL_GPL(ata_busy_sleep);
Tejun Heo86e45b62006-03-05 15:29:09 +09005091EXPORT_SYMBOL_GPL(ata_port_queue_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
5093EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
5094EXPORT_SYMBOL_GPL(ata_scsi_error);
5095EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
5096EXPORT_SYMBOL_GPL(ata_scsi_release);
5097EXPORT_SYMBOL_GPL(ata_host_intr);
Tejun Heo6a62a042006-02-13 10:02:46 +09005098EXPORT_SYMBOL_GPL(ata_id_string);
5099EXPORT_SYMBOL_GPL(ata_id_c_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100EXPORT_SYMBOL_GPL(ata_scsi_simulate);
Tejun Heoa72ec4c2006-01-23 13:09:37 +09005101EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
5102EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103
Alan Cox1bc4ccf2006-01-09 17:18:14 +00005104EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
Alan Cox452503f2005-10-21 19:01:32 -04005105EXPORT_SYMBOL_GPL(ata_timing_compute);
5106EXPORT_SYMBOL_GPL(ata_timing_merge);
5107
Linus Torvalds1da177e2005-04-16 15:20:36 -07005108#ifdef CONFIG_PCI
5109EXPORT_SYMBOL_GPL(pci_test_config_bits);
Jeff Garzik374b1872005-08-30 05:42:52 -04005110EXPORT_SYMBOL_GPL(ata_pci_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
5112EXPORT_SYMBOL_GPL(ata_pci_init_one);
5113EXPORT_SYMBOL_GPL(ata_pci_remove_one);
Jens Axboe9b847542006-01-06 09:28:07 +01005114EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
5115EXPORT_SYMBOL_GPL(ata_pci_device_resume);
Alan Cox67951ad2006-03-22 15:55:54 +00005116EXPORT_SYMBOL_GPL(ata_pci_default_filter);
5117EXPORT_SYMBOL_GPL(ata_pci_clear_simplex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118#endif /* CONFIG_PCI */
Jens Axboe9b847542006-01-06 09:28:07 +01005119
5120EXPORT_SYMBOL_GPL(ata_device_suspend);
5121EXPORT_SYMBOL_GPL(ata_device_resume);
5122EXPORT_SYMBOL_GPL(ata_scsi_device_suspend);
5123EXPORT_SYMBOL_GPL(ata_scsi_device_resume);