blob: 63488673765f1c871a15d07e7e005aa618f7eeb0 [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);
Tejun Heo1c3fae42006-04-02 20:53:28 +090068static int ata_down_sata_spd_limit(struct ata_port *ap);
Tejun Heoe82cbdb2006-04-01 01:38:18 +090069static int ata_set_mode(struct ata_port *ap, struct ata_device **r_failed_dev);
Tejun Heo83206a22006-03-24 15:25:31 +090070static unsigned int ata_dev_set_xfermode(struct ata_port *ap,
71 struct ata_device *dev);
Tejun Heoacf356b2006-03-24 14:07:50 +090072static void ata_dev_xfermask(struct ata_port *ap, struct ata_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
74static unsigned int ata_unique_id = 1;
75static struct workqueue_struct *ata_wq;
76
Jeff Garzik418dc1f2006-03-11 20:50:08 -050077int atapi_enabled = 1;
Jeff Garzik1623c812005-08-30 03:37:42 -040078module_param(atapi_enabled, int, 0444);
79MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)");
80
Jeff Garzikc3c013a2006-02-27 22:31:19 -050081int libata_fua = 0;
82module_param_named(fua, libata_fua, int, 0444);
83MODULE_PARM_DESC(fua, "FUA support (0=off, 1=on)");
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085MODULE_AUTHOR("Jeff Garzik");
86MODULE_DESCRIPTION("Library module for ATA devices");
87MODULE_LICENSE("GPL");
88MODULE_VERSION(DRV_VERSION);
89
Edward Falk0baab862005-06-02 18:17:13 -040090
91/**
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
93 * @tf: Taskfile to convert
94 * @fis: Buffer into which data will output
95 * @pmp: Port multiplier port
96 *
97 * Converts a standard ATA taskfile to a Serial ATA
98 * FIS structure (Register - Host to Device).
99 *
100 * LOCKING:
101 * Inherited from caller.
102 */
103
Jeff Garzik057ace52005-10-22 14:27:05 -0400104void ata_tf_to_fis(const struct ata_taskfile *tf, u8 *fis, u8 pmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
106 fis[0] = 0x27; /* Register - Host to Device FIS */
107 fis[1] = (pmp & 0xf) | (1 << 7); /* Port multiplier number,
108 bit 7 indicates Command FIS */
109 fis[2] = tf->command;
110 fis[3] = tf->feature;
111
112 fis[4] = tf->lbal;
113 fis[5] = tf->lbam;
114 fis[6] = tf->lbah;
115 fis[7] = tf->device;
116
117 fis[8] = tf->hob_lbal;
118 fis[9] = tf->hob_lbam;
119 fis[10] = tf->hob_lbah;
120 fis[11] = tf->hob_feature;
121
122 fis[12] = tf->nsect;
123 fis[13] = tf->hob_nsect;
124 fis[14] = 0;
125 fis[15] = tf->ctl;
126
127 fis[16] = 0;
128 fis[17] = 0;
129 fis[18] = 0;
130 fis[19] = 0;
131}
132
133/**
134 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
135 * @fis: Buffer from which data will be input
136 * @tf: Taskfile to output
137 *
Mark Lorde12a1be2005-11-12 18:55:45 -0500138 * Converts a serial ATA FIS structure to a standard ATA taskfile.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 *
140 * LOCKING:
141 * Inherited from caller.
142 */
143
Jeff Garzik057ace52005-10-22 14:27:05 -0400144void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
146 tf->command = fis[2]; /* status */
147 tf->feature = fis[3]; /* error */
148
149 tf->lbal = fis[4];
150 tf->lbam = fis[5];
151 tf->lbah = fis[6];
152 tf->device = fis[7];
153
154 tf->hob_lbal = fis[8];
155 tf->hob_lbam = fis[9];
156 tf->hob_lbah = fis[10];
157
158 tf->nsect = fis[12];
159 tf->hob_nsect = fis[13];
160}
161
Albert Lee8cbd6df2005-10-12 15:06:27 +0800162static const u8 ata_rw_cmds[] = {
163 /* pio multi */
164 ATA_CMD_READ_MULTI,
165 ATA_CMD_WRITE_MULTI,
166 ATA_CMD_READ_MULTI_EXT,
167 ATA_CMD_WRITE_MULTI_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100168 0,
169 0,
170 0,
171 ATA_CMD_WRITE_MULTI_FUA_EXT,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800172 /* pio */
173 ATA_CMD_PIO_READ,
174 ATA_CMD_PIO_WRITE,
175 ATA_CMD_PIO_READ_EXT,
176 ATA_CMD_PIO_WRITE_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100177 0,
178 0,
179 0,
180 0,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800181 /* dma */
182 ATA_CMD_READ,
183 ATA_CMD_WRITE,
184 ATA_CMD_READ_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100185 ATA_CMD_WRITE_EXT,
186 0,
187 0,
188 0,
189 ATA_CMD_WRITE_FUA_EXT
Albert Lee8cbd6df2005-10-12 15:06:27 +0800190};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191
192/**
Albert Lee8cbd6df2005-10-12 15:06:27 +0800193 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
194 * @qc: command to examine and configure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 *
Jeff Garzik2e9edbf2006-03-24 09:56:57 -0500196 * Examine the device configuration and tf->flags to calculate
Albert Lee8cbd6df2005-10-12 15:06:27 +0800197 * the proper read/write commands and protocol to use.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 *
199 * LOCKING:
200 * caller.
201 */
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100202int ata_rwcmd_protocol(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
Albert Lee8cbd6df2005-10-12 15:06:27 +0800204 struct ata_taskfile *tf = &qc->tf;
205 struct ata_device *dev = qc->dev;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100206 u8 cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100208 int index, fua, lba48, write;
Jeff Garzik2e9edbf2006-03-24 09:56:57 -0500209
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100210 fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800211 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
212 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Albert Lee8cbd6df2005-10-12 15:06:27 +0800214 if (dev->flags & ATA_DFLAG_PIO) {
215 tf->protocol = ATA_PROT_PIO;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100216 index = dev->multi_count ? 0 : 8;
Alan Cox8d238e02006-01-17 20:50:31 +0000217 } else if (lba48 && (qc->ap->flags & ATA_FLAG_PIO_LBA48)) {
218 /* Unable to use DMA due to host limitation */
219 tf->protocol = ATA_PROT_PIO;
Albert Lee0565c262006-02-13 18:55:25 +0800220 index = dev->multi_count ? 0 : 8;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800221 } else {
222 tf->protocol = ATA_PROT_DMA;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100223 index = 16;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100226 cmd = ata_rw_cmds[index + fua + lba48 + write];
227 if (cmd) {
228 tf->command = cmd;
229 return 0;
230 }
231 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}
233
Tejun Heocb95d562006-03-06 04:31:56 +0900234/**
235 * ata_pack_xfermask - Pack pio, mwdma and udma masks into xfer_mask
236 * @pio_mask: pio_mask
237 * @mwdma_mask: mwdma_mask
238 * @udma_mask: udma_mask
239 *
240 * Pack @pio_mask, @mwdma_mask and @udma_mask into a single
241 * unsigned int xfer_mask.
242 *
243 * LOCKING:
244 * None.
245 *
246 * RETURNS:
247 * Packed xfer_mask.
248 */
249static unsigned int ata_pack_xfermask(unsigned int pio_mask,
250 unsigned int mwdma_mask,
251 unsigned int udma_mask)
252{
253 return ((pio_mask << ATA_SHIFT_PIO) & ATA_MASK_PIO) |
254 ((mwdma_mask << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA) |
255 ((udma_mask << ATA_SHIFT_UDMA) & ATA_MASK_UDMA);
256}
257
Tejun Heoc0489e42006-03-24 14:07:49 +0900258/**
259 * ata_unpack_xfermask - Unpack xfer_mask into pio, mwdma and udma masks
260 * @xfer_mask: xfer_mask to unpack
261 * @pio_mask: resulting pio_mask
262 * @mwdma_mask: resulting mwdma_mask
263 * @udma_mask: resulting udma_mask
264 *
265 * Unpack @xfer_mask into @pio_mask, @mwdma_mask and @udma_mask.
266 * Any NULL distination masks will be ignored.
267 */
268static void ata_unpack_xfermask(unsigned int xfer_mask,
269 unsigned int *pio_mask,
270 unsigned int *mwdma_mask,
271 unsigned int *udma_mask)
272{
273 if (pio_mask)
274 *pio_mask = (xfer_mask & ATA_MASK_PIO) >> ATA_SHIFT_PIO;
275 if (mwdma_mask)
276 *mwdma_mask = (xfer_mask & ATA_MASK_MWDMA) >> ATA_SHIFT_MWDMA;
277 if (udma_mask)
278 *udma_mask = (xfer_mask & ATA_MASK_UDMA) >> ATA_SHIFT_UDMA;
279}
280
Tejun Heocb95d562006-03-06 04:31:56 +0900281static const struct ata_xfer_ent {
Tejun Heobe9a50c82006-03-31 22:48:52 +0900282 int shift, bits;
Tejun Heocb95d562006-03-06 04:31:56 +0900283 u8 base;
284} ata_xfer_tbl[] = {
285 { ATA_SHIFT_PIO, ATA_BITS_PIO, XFER_PIO_0 },
286 { ATA_SHIFT_MWDMA, ATA_BITS_MWDMA, XFER_MW_DMA_0 },
287 { ATA_SHIFT_UDMA, ATA_BITS_UDMA, XFER_UDMA_0 },
288 { -1, },
289};
290
291/**
292 * ata_xfer_mask2mode - Find matching XFER_* for the given xfer_mask
293 * @xfer_mask: xfer_mask of interest
294 *
295 * Return matching XFER_* value for @xfer_mask. Only the highest
296 * bit of @xfer_mask is considered.
297 *
298 * LOCKING:
299 * None.
300 *
301 * RETURNS:
302 * Matching XFER_* value, 0 if no match found.
303 */
304static u8 ata_xfer_mask2mode(unsigned int xfer_mask)
305{
306 int highbit = fls(xfer_mask) - 1;
307 const struct ata_xfer_ent *ent;
308
309 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
310 if (highbit >= ent->shift && highbit < ent->shift + ent->bits)
311 return ent->base + highbit - ent->shift;
312 return 0;
313}
314
315/**
316 * ata_xfer_mode2mask - Find matching xfer_mask for XFER_*
317 * @xfer_mode: XFER_* of interest
318 *
319 * Return matching xfer_mask for @xfer_mode.
320 *
321 * LOCKING:
322 * None.
323 *
324 * RETURNS:
325 * Matching xfer_mask, 0 if no match found.
326 */
327static unsigned int ata_xfer_mode2mask(u8 xfer_mode)
328{
329 const struct ata_xfer_ent *ent;
330
331 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
332 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
333 return 1 << (ent->shift + xfer_mode - ent->base);
334 return 0;
335}
336
337/**
338 * ata_xfer_mode2shift - Find matching xfer_shift for XFER_*
339 * @xfer_mode: XFER_* of interest
340 *
341 * Return matching xfer_shift for @xfer_mode.
342 *
343 * LOCKING:
344 * None.
345 *
346 * RETURNS:
347 * Matching xfer_shift, -1 if no match found.
348 */
349static int ata_xfer_mode2shift(unsigned int xfer_mode)
350{
351 const struct ata_xfer_ent *ent;
352
353 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
354 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
355 return ent->shift;
356 return -1;
357}
358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359/**
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900360 * ata_mode_string - convert xfer_mask to string
361 * @xfer_mask: mask of bits supported; only highest bit counts.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 *
363 * Determine string which represents the highest speed
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900364 * (highest bit in @modemask).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 *
366 * LOCKING:
367 * None.
368 *
369 * RETURNS:
370 * Constant C string representing highest speed listed in
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900371 * @mode_mask, or the constant C string "<n/a>".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 */
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900373static const char *ata_mode_string(unsigned int xfer_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
Tejun Heo75f554b2006-03-06 04:31:57 +0900375 static const char * const xfer_mode_str[] = {
376 "PIO0",
377 "PIO1",
378 "PIO2",
379 "PIO3",
380 "PIO4",
381 "MWDMA0",
382 "MWDMA1",
383 "MWDMA2",
384 "UDMA/16",
385 "UDMA/25",
386 "UDMA/33",
387 "UDMA/44",
388 "UDMA/66",
389 "UDMA/100",
390 "UDMA/133",
391 "UDMA7",
392 };
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900393 int highbit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900395 highbit = fls(xfer_mask) - 1;
396 if (highbit >= 0 && highbit < ARRAY_SIZE(xfer_mode_str))
397 return xfer_mode_str[highbit];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 return "<n/a>";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399}
400
Tejun Heo4c360c82006-04-01 01:38:17 +0900401static const char *sata_spd_string(unsigned int spd)
402{
403 static const char * const spd_str[] = {
404 "1.5 Gbps",
405 "3.0 Gbps",
406 };
407
408 if (spd == 0 || (spd - 1) >= ARRAY_SIZE(spd_str))
409 return "<unknown>";
410 return spd_str[spd - 1];
411}
412
Tejun Heo0b8efb02006-03-24 15:25:31 +0900413static void ata_dev_disable(struct ata_port *ap, struct ata_device *dev)
414{
Tejun Heoe1211e32006-04-01 01:38:18 +0900415 if (ata_dev_enabled(dev)) {
Tejun Heo0b8efb02006-03-24 15:25:31 +0900416 printk(KERN_WARNING "ata%u: dev %u disabled\n",
417 ap->id, dev->devno);
418 dev->class++;
419 }
420}
421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422/**
423 * ata_pio_devchk - PATA device presence detection
424 * @ap: ATA channel to examine
425 * @device: Device to examine (starting at zero)
426 *
427 * This technique was originally described in
428 * Hale Landis's ATADRVR (www.ata-atapi.com), and
429 * later found its way into the ATA/ATAPI spec.
430 *
431 * Write a pattern to the ATA shadow registers,
432 * and if a device is present, it will respond by
433 * correctly storing and echoing back the
434 * ATA shadow register contents.
435 *
436 * LOCKING:
437 * caller.
438 */
439
440static unsigned int ata_pio_devchk(struct ata_port *ap,
441 unsigned int device)
442{
443 struct ata_ioports *ioaddr = &ap->ioaddr;
444 u8 nsect, lbal;
445
446 ap->ops->dev_select(ap, device);
447
448 outb(0x55, ioaddr->nsect_addr);
449 outb(0xaa, ioaddr->lbal_addr);
450
451 outb(0xaa, ioaddr->nsect_addr);
452 outb(0x55, ioaddr->lbal_addr);
453
454 outb(0x55, ioaddr->nsect_addr);
455 outb(0xaa, ioaddr->lbal_addr);
456
457 nsect = inb(ioaddr->nsect_addr);
458 lbal = inb(ioaddr->lbal_addr);
459
460 if ((nsect == 0x55) && (lbal == 0xaa))
461 return 1; /* we found a device */
462
463 return 0; /* nothing found */
464}
465
466/**
467 * ata_mmio_devchk - PATA device presence detection
468 * @ap: ATA channel to examine
469 * @device: Device to examine (starting at zero)
470 *
471 * This technique was originally described in
472 * Hale Landis's ATADRVR (www.ata-atapi.com), and
473 * later found its way into the ATA/ATAPI spec.
474 *
475 * Write a pattern to the ATA shadow registers,
476 * and if a device is present, it will respond by
477 * correctly storing and echoing back the
478 * ATA shadow register contents.
479 *
480 * LOCKING:
481 * caller.
482 */
483
484static unsigned int ata_mmio_devchk(struct ata_port *ap,
485 unsigned int device)
486{
487 struct ata_ioports *ioaddr = &ap->ioaddr;
488 u8 nsect, lbal;
489
490 ap->ops->dev_select(ap, device);
491
492 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
493 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
494
495 writeb(0xaa, (void __iomem *) ioaddr->nsect_addr);
496 writeb(0x55, (void __iomem *) ioaddr->lbal_addr);
497
498 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
499 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
500
501 nsect = readb((void __iomem *) ioaddr->nsect_addr);
502 lbal = readb((void __iomem *) ioaddr->lbal_addr);
503
504 if ((nsect == 0x55) && (lbal == 0xaa))
505 return 1; /* we found a device */
506
507 return 0; /* nothing found */
508}
509
510/**
511 * ata_devchk - PATA device presence detection
512 * @ap: ATA channel to examine
513 * @device: Device to examine (starting at zero)
514 *
515 * Dispatch ATA device presence detection, depending
516 * on whether we are using PIO or MMIO to talk to the
517 * ATA shadow registers.
518 *
519 * LOCKING:
520 * caller.
521 */
522
523static unsigned int ata_devchk(struct ata_port *ap,
524 unsigned int device)
525{
526 if (ap->flags & ATA_FLAG_MMIO)
527 return ata_mmio_devchk(ap, device);
528 return ata_pio_devchk(ap, device);
529}
530
531/**
532 * ata_dev_classify - determine device type based on ATA-spec signature
533 * @tf: ATA taskfile register set for device to be identified
534 *
535 * Determine from taskfile register contents whether a device is
536 * ATA or ATAPI, as per "Signature and persistence" section
537 * of ATA/PI spec (volume 1, sect 5.14).
538 *
539 * LOCKING:
540 * None.
541 *
542 * RETURNS:
543 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
544 * the event of failure.
545 */
546
Jeff Garzik057ace52005-10-22 14:27:05 -0400547unsigned int ata_dev_classify(const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548{
549 /* Apple's open source Darwin code hints that some devices only
550 * put a proper signature into the LBA mid/high registers,
551 * So, we only check those. It's sufficient for uniqueness.
552 */
553
554 if (((tf->lbam == 0) && (tf->lbah == 0)) ||
555 ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
556 DPRINTK("found ATA device by sig\n");
557 return ATA_DEV_ATA;
558 }
559
560 if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
561 ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
562 DPRINTK("found ATAPI device by sig\n");
563 return ATA_DEV_ATAPI;
564 }
565
566 DPRINTK("unknown device\n");
567 return ATA_DEV_UNKNOWN;
568}
569
570/**
571 * ata_dev_try_classify - Parse returned ATA device signature
572 * @ap: ATA channel to examine
573 * @device: Device to examine (starting at zero)
Tejun Heob4dc7622006-01-24 17:05:22 +0900574 * @r_err: Value of error register on completion
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 *
576 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
577 * an ATA/ATAPI-defined set of values is placed in the ATA
578 * shadow registers, indicating the results of device detection
579 * and diagnostics.
580 *
581 * Select the ATA device, and read the values from the ATA shadow
582 * registers. Then parse according to the Error register value,
583 * and the spec-defined values examined by ata_dev_classify().
584 *
585 * LOCKING:
586 * caller.
Tejun Heob4dc7622006-01-24 17:05:22 +0900587 *
588 * RETURNS:
589 * Device type - %ATA_DEV_ATA, %ATA_DEV_ATAPI or %ATA_DEV_NONE.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 */
591
Tejun Heob4dc7622006-01-24 17:05:22 +0900592static unsigned int
593ata_dev_try_classify(struct ata_port *ap, unsigned int device, u8 *r_err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 struct ata_taskfile tf;
596 unsigned int class;
597 u8 err;
598
599 ap->ops->dev_select(ap, device);
600
601 memset(&tf, 0, sizeof(tf));
602
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 ap->ops->tf_read(ap, &tf);
Jeff Garzik0169e282005-10-29 21:25:10 -0400604 err = tf.feature;
Tejun Heob4dc7622006-01-24 17:05:22 +0900605 if (r_err)
606 *r_err = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
608 /* see if device passed diags */
609 if (err == 1)
610 /* do nothing */ ;
611 else if ((device == 0) && (err == 0x81))
612 /* do nothing */ ;
613 else
Tejun Heob4dc7622006-01-24 17:05:22 +0900614 return ATA_DEV_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
Tejun Heob4dc7622006-01-24 17:05:22 +0900616 /* determine if device is ATA or ATAPI */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 class = ata_dev_classify(&tf);
Tejun Heob4dc7622006-01-24 17:05:22 +0900618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 if (class == ATA_DEV_UNKNOWN)
Tejun Heob4dc7622006-01-24 17:05:22 +0900620 return ATA_DEV_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
Tejun Heob4dc7622006-01-24 17:05:22 +0900622 return ATA_DEV_NONE;
623 return class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624}
625
626/**
Tejun Heo6a62a042006-02-13 10:02:46 +0900627 * ata_id_string - Convert IDENTIFY DEVICE page into string
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 * @id: IDENTIFY DEVICE results we will examine
629 * @s: string into which data is output
630 * @ofs: offset into identify device page
631 * @len: length of string to return. must be an even number.
632 *
633 * The strings in the IDENTIFY DEVICE page are broken up into
634 * 16-bit chunks. Run through the string, and output each
635 * 8-bit chunk linearly, regardless of platform.
636 *
637 * LOCKING:
638 * caller.
639 */
640
Tejun Heo6a62a042006-02-13 10:02:46 +0900641void ata_id_string(const u16 *id, unsigned char *s,
642 unsigned int ofs, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643{
644 unsigned int c;
645
646 while (len > 0) {
647 c = id[ofs] >> 8;
648 *s = c;
649 s++;
650
651 c = id[ofs] & 0xff;
652 *s = c;
653 s++;
654
655 ofs++;
656 len -= 2;
657 }
658}
659
Tejun Heo0e949ff2006-02-12 22:47:04 +0900660/**
Tejun Heo6a62a042006-02-13 10:02:46 +0900661 * ata_id_c_string - Convert IDENTIFY DEVICE page into C string
Tejun Heo0e949ff2006-02-12 22:47:04 +0900662 * @id: IDENTIFY DEVICE results we will examine
663 * @s: string into which data is output
664 * @ofs: offset into identify device page
665 * @len: length of string to return. must be an odd number.
666 *
Tejun Heo6a62a042006-02-13 10:02:46 +0900667 * This function is identical to ata_id_string except that it
Tejun Heo0e949ff2006-02-12 22:47:04 +0900668 * trims trailing spaces and terminates the resulting string with
669 * null. @len must be actual maximum length (even number) + 1.
670 *
671 * LOCKING:
672 * caller.
673 */
Tejun Heo6a62a042006-02-13 10:02:46 +0900674void ata_id_c_string(const u16 *id, unsigned char *s,
675 unsigned int ofs, unsigned int len)
Tejun Heo0e949ff2006-02-12 22:47:04 +0900676{
677 unsigned char *p;
678
679 WARN_ON(!(len & 1));
680
Tejun Heo6a62a042006-02-13 10:02:46 +0900681 ata_id_string(id, s, ofs, len - 1);
Tejun Heo0e949ff2006-02-12 22:47:04 +0900682
683 p = s + strnlen(s, len - 1);
684 while (p > s && p[-1] == ' ')
685 p--;
686 *p = '\0';
687}
Edward Falk0baab862005-06-02 18:17:13 -0400688
Tejun Heo29407402006-02-12 22:47:04 +0900689static u64 ata_id_n_sectors(const u16 *id)
690{
691 if (ata_id_has_lba(id)) {
692 if (ata_id_has_lba48(id))
693 return ata_id_u64(id, 100);
694 else
695 return ata_id_u32(id, 60);
696 } else {
697 if (ata_id_current_chs_valid(id))
698 return ata_id_u32(id, 57);
699 else
700 return id[1] * id[3] * id[6];
701 }
702}
703
Edward Falk0baab862005-06-02 18:17:13 -0400704/**
705 * ata_noop_dev_select - Select device 0/1 on ATA bus
706 * @ap: ATA channel to manipulate
707 * @device: ATA device (numbered from zero) to select
708 *
709 * This function performs no actual function.
710 *
711 * May be used as the dev_select() entry in ata_port_operations.
712 *
713 * LOCKING:
714 * caller.
715 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
717{
718}
719
Edward Falk0baab862005-06-02 18:17:13 -0400720
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721/**
722 * ata_std_dev_select - Select device 0/1 on ATA bus
723 * @ap: ATA channel to manipulate
724 * @device: ATA device (numbered from zero) to select
725 *
726 * Use the method defined in the ATA specification to
727 * make either device 0, or device 1, active on the
Edward Falk0baab862005-06-02 18:17:13 -0400728 * ATA channel. Works with both PIO and MMIO.
729 *
730 * May be used as the dev_select() entry in ata_port_operations.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 *
732 * LOCKING:
733 * caller.
734 */
735
736void ata_std_dev_select (struct ata_port *ap, unsigned int device)
737{
738 u8 tmp;
739
740 if (device == 0)
741 tmp = ATA_DEVICE_OBS;
742 else
743 tmp = ATA_DEVICE_OBS | ATA_DEV1;
744
745 if (ap->flags & ATA_FLAG_MMIO) {
746 writeb(tmp, (void __iomem *) ap->ioaddr.device_addr);
747 } else {
748 outb(tmp, ap->ioaddr.device_addr);
749 }
750 ata_pause(ap); /* needed; also flushes, for mmio */
751}
752
753/**
754 * ata_dev_select - Select device 0/1 on ATA bus
755 * @ap: ATA channel to manipulate
756 * @device: ATA device (numbered from zero) to select
757 * @wait: non-zero to wait for Status register BSY bit to clear
758 * @can_sleep: non-zero if context allows sleeping
759 *
760 * Use the method defined in the ATA specification to
761 * make either device 0, or device 1, active on the
762 * ATA channel.
763 *
764 * This is a high-level version of ata_std_dev_select(),
765 * which additionally provides the services of inserting
766 * the proper pauses and status polling, where needed.
767 *
768 * LOCKING:
769 * caller.
770 */
771
772void ata_dev_select(struct ata_port *ap, unsigned int device,
773 unsigned int wait, unsigned int can_sleep)
774{
775 VPRINTK("ENTER, ata%u: device %u, wait %u\n",
776 ap->id, device, wait);
777
778 if (wait)
779 ata_wait_idle(ap);
780
781 ap->ops->dev_select(ap, device);
782
783 if (wait) {
784 if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
785 msleep(150);
786 ata_wait_idle(ap);
787 }
788}
789
790/**
791 * ata_dump_id - IDENTIFY DEVICE info debugging output
Tejun Heo0bd33002006-02-12 22:47:05 +0900792 * @id: IDENTIFY DEVICE page to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 *
Tejun Heo0bd33002006-02-12 22:47:05 +0900794 * Dump selected 16-bit words from the given IDENTIFY DEVICE
795 * page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 *
797 * LOCKING:
798 * caller.
799 */
800
Tejun Heo0bd33002006-02-12 22:47:05 +0900801static inline void ata_dump_id(const u16 *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802{
803 DPRINTK("49==0x%04x "
804 "53==0x%04x "
805 "63==0x%04x "
806 "64==0x%04x "
807 "75==0x%04x \n",
Tejun Heo0bd33002006-02-12 22:47:05 +0900808 id[49],
809 id[53],
810 id[63],
811 id[64],
812 id[75]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 DPRINTK("80==0x%04x "
814 "81==0x%04x "
815 "82==0x%04x "
816 "83==0x%04x "
817 "84==0x%04x \n",
Tejun Heo0bd33002006-02-12 22:47:05 +0900818 id[80],
819 id[81],
820 id[82],
821 id[83],
822 id[84]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 DPRINTK("88==0x%04x "
824 "93==0x%04x\n",
Tejun Heo0bd33002006-02-12 22:47:05 +0900825 id[88],
826 id[93]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827}
828
Tejun Heocb95d562006-03-06 04:31:56 +0900829/**
830 * ata_id_xfermask - Compute xfermask from the given IDENTIFY data
831 * @id: IDENTIFY data to compute xfer mask from
832 *
833 * Compute the xfermask for this device. This is not as trivial
834 * as it seems if we must consider early devices correctly.
835 *
836 * FIXME: pre IDE drive timing (do we care ?).
837 *
838 * LOCKING:
839 * None.
840 *
841 * RETURNS:
842 * Computed xfermask
843 */
844static unsigned int ata_id_xfermask(const u16 *id)
845{
846 unsigned int pio_mask, mwdma_mask, udma_mask;
847
848 /* Usual case. Word 53 indicates word 64 is valid */
849 if (id[ATA_ID_FIELD_VALID] & (1 << 1)) {
850 pio_mask = id[ATA_ID_PIO_MODES] & 0x03;
851 pio_mask <<= 3;
852 pio_mask |= 0x7;
853 } else {
854 /* If word 64 isn't valid then Word 51 high byte holds
855 * the PIO timing number for the maximum. Turn it into
856 * a mask.
857 */
858 pio_mask = (2 << (id[ATA_ID_OLD_PIO_MODES] & 0xFF)) - 1 ;
859
860 /* But wait.. there's more. Design your standards by
861 * committee and you too can get a free iordy field to
862 * process. However its the speeds not the modes that
863 * are supported... Note drivers using the timing API
864 * will get this right anyway
865 */
866 }
867
868 mwdma_mask = id[ATA_ID_MWDMA_MODES] & 0x07;
Tejun Heofb21f0d2006-03-12 12:34:35 +0900869
870 udma_mask = 0;
871 if (id[ATA_ID_FIELD_VALID] & (1 << 2))
872 udma_mask = id[ATA_ID_UDMA_MODES] & 0xff;
Tejun Heocb95d562006-03-06 04:31:56 +0900873
874 return ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
875}
876
Tejun Heo86e45b62006-03-05 15:29:09 +0900877/**
878 * ata_port_queue_task - Queue port_task
879 * @ap: The ata_port to queue port_task for
880 *
881 * Schedule @fn(@data) for execution after @delay jiffies using
882 * port_task. There is one port_task per port and it's the
883 * user(low level driver)'s responsibility to make sure that only
884 * one task is active at any given time.
885 *
886 * libata core layer takes care of synchronization between
887 * port_task and EH. ata_port_queue_task() may be ignored for EH
888 * synchronization.
889 *
890 * LOCKING:
891 * Inherited from caller.
892 */
893void ata_port_queue_task(struct ata_port *ap, void (*fn)(void *), void *data,
894 unsigned long delay)
895{
896 int rc;
897
Tejun Heo2e755f62006-03-05 15:29:09 +0900898 if (ap->flags & ATA_FLAG_FLUSH_PORT_TASK)
Tejun Heo86e45b62006-03-05 15:29:09 +0900899 return;
900
901 PREPARE_WORK(&ap->port_task, fn, data);
902
903 if (!delay)
904 rc = queue_work(ata_wq, &ap->port_task);
905 else
906 rc = queue_delayed_work(ata_wq, &ap->port_task, delay);
907
908 /* rc == 0 means that another user is using port task */
909 WARN_ON(rc == 0);
910}
911
912/**
913 * ata_port_flush_task - Flush port_task
914 * @ap: The ata_port to flush port_task for
915 *
916 * After this function completes, port_task is guranteed not to
917 * be running or scheduled.
918 *
919 * LOCKING:
920 * Kernel thread context (may sleep)
921 */
922void ata_port_flush_task(struct ata_port *ap)
923{
924 unsigned long flags;
925
926 DPRINTK("ENTER\n");
927
928 spin_lock_irqsave(&ap->host_set->lock, flags);
Tejun Heo2e755f62006-03-05 15:29:09 +0900929 ap->flags |= ATA_FLAG_FLUSH_PORT_TASK;
Tejun Heo86e45b62006-03-05 15:29:09 +0900930 spin_unlock_irqrestore(&ap->host_set->lock, flags);
931
932 DPRINTK("flush #1\n");
933 flush_workqueue(ata_wq);
934
935 /*
936 * At this point, if a task is running, it's guaranteed to see
937 * the FLUSH flag; thus, it will never queue pio tasks again.
938 * Cancel and flush.
939 */
940 if (!cancel_delayed_work(&ap->port_task)) {
941 DPRINTK("flush #2\n");
942 flush_workqueue(ata_wq);
943 }
944
945 spin_lock_irqsave(&ap->host_set->lock, flags);
Tejun Heo2e755f62006-03-05 15:29:09 +0900946 ap->flags &= ~ATA_FLAG_FLUSH_PORT_TASK;
Tejun Heo86e45b62006-03-05 15:29:09 +0900947 spin_unlock_irqrestore(&ap->host_set->lock, flags);
948
949 DPRINTK("EXIT\n");
950}
951
Tejun Heo77853bf2006-01-23 13:09:36 +0900952void ata_qc_complete_internal(struct ata_queued_cmd *qc)
Tejun Heoa2a7a662005-12-13 14:48:31 +0900953{
Tejun Heo77853bf2006-01-23 13:09:36 +0900954 struct completion *waiting = qc->private_data;
Tejun Heoa2a7a662005-12-13 14:48:31 +0900955
Tejun Heo77853bf2006-01-23 13:09:36 +0900956 qc->ap->ops->tf_read(qc->ap, &qc->tf);
Tejun Heoa2a7a662005-12-13 14:48:31 +0900957 complete(waiting);
Tejun Heoa2a7a662005-12-13 14:48:31 +0900958}
959
960/**
961 * ata_exec_internal - execute libata internal command
962 * @ap: Port to which the command is sent
963 * @dev: Device to which the command is sent
964 * @tf: Taskfile registers for the command and the result
965 * @dma_dir: Data tranfer direction of the command
966 * @buf: Data buffer of the command
967 * @buflen: Length of data buffer
968 *
969 * Executes libata internal command with timeout. @tf contains
970 * command on entry and result on return. Timeout and error
971 * conditions are reported via return value. No recovery action
972 * is taken after a command times out. It's caller's duty to
973 * clean up after timeout.
974 *
975 * LOCKING:
976 * None. Should be called with kernel context, might sleep.
977 */
978
979static unsigned
980ata_exec_internal(struct ata_port *ap, struct ata_device *dev,
981 struct ata_taskfile *tf,
982 int dma_dir, void *buf, unsigned int buflen)
983{
984 u8 command = tf->command;
985 struct ata_queued_cmd *qc;
986 DECLARE_COMPLETION(wait);
987 unsigned long flags;
Tejun Heo77853bf2006-01-23 13:09:36 +0900988 unsigned int err_mask;
Tejun Heoa2a7a662005-12-13 14:48:31 +0900989
990 spin_lock_irqsave(&ap->host_set->lock, flags);
991
992 qc = ata_qc_new_init(ap, dev);
993 BUG_ON(qc == NULL);
994
995 qc->tf = *tf;
996 qc->dma_dir = dma_dir;
997 if (dma_dir != DMA_NONE) {
998 ata_sg_init_one(qc, buf, buflen);
999 qc->nsect = buflen / ATA_SECT_SIZE;
1000 }
1001
Tejun Heo77853bf2006-01-23 13:09:36 +09001002 qc->private_data = &wait;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001003 qc->complete_fn = ata_qc_complete_internal;
1004
Tejun Heo8e0e6942006-03-31 20:41:11 +09001005 ata_qc_issue(qc);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001006
1007 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1008
1009 if (!wait_for_completion_timeout(&wait, ATA_TMOUT_INTERNAL)) {
Albert Lee41ade502006-03-14 11:19:04 +08001010 ata_port_flush_task(ap);
1011
Tejun Heoa2a7a662005-12-13 14:48:31 +09001012 spin_lock_irqsave(&ap->host_set->lock, flags);
1013
1014 /* We're racing with irq here. If we lose, the
1015 * following test prevents us from completing the qc
1016 * again. If completion irq occurs after here but
1017 * before the caller cleans up, it will result in a
1018 * spurious interrupt. We can live with that.
1019 */
Tejun Heo77853bf2006-01-23 13:09:36 +09001020 if (qc->flags & ATA_QCFLAG_ACTIVE) {
Tejun Heo11a56d22006-01-23 13:09:36 +09001021 qc->err_mask = AC_ERR_TIMEOUT;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001022 ata_qc_complete(qc);
1023 printk(KERN_WARNING "ata%u: qc timeout (cmd 0x%x)\n",
1024 ap->id, command);
1025 }
1026
1027 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1028 }
1029
Tejun Heo77853bf2006-01-23 13:09:36 +09001030 *tf = qc->tf;
1031 err_mask = qc->err_mask;
1032
1033 ata_qc_free(qc);
1034
Tejun Heo1f7dd3e2006-03-24 15:25:30 +09001035 /* XXX - Some LLDDs (sata_mv) disable port on command failure.
1036 * Until those drivers are fixed, we detect the condition
1037 * here, fail the command with AC_ERR_SYSTEM and reenable the
1038 * port.
1039 *
1040 * Note that this doesn't change any behavior as internal
1041 * command failure results in disabling the device in the
1042 * higher layer for LLDDs without new reset/EH callbacks.
1043 *
1044 * Kill the following code as soon as those drivers are fixed.
1045 */
1046 if (ap->flags & ATA_FLAG_PORT_DISABLED) {
1047 err_mask |= AC_ERR_SYSTEM;
1048 ata_port_probe(ap);
1049 }
1050
Tejun Heo77853bf2006-01-23 13:09:36 +09001051 return err_mask;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001052}
1053
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054/**
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001055 * ata_pio_need_iordy - check if iordy needed
1056 * @adev: ATA device
1057 *
1058 * Check if the current speed of the device requires IORDY. Used
1059 * by various controllers for chip configuration.
1060 */
1061
1062unsigned int ata_pio_need_iordy(const struct ata_device *adev)
1063{
1064 int pio;
1065 int speed = adev->pio_mode - XFER_PIO_0;
1066
1067 if (speed < 2)
1068 return 0;
1069 if (speed > 2)
1070 return 1;
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001071
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001072 /* If we have no drive specific rule, then PIO 2 is non IORDY */
1073
1074 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
1075 pio = adev->id[ATA_ID_EIDE_PIO];
1076 /* Is the speed faster than the drive allows non IORDY ? */
1077 if (pio) {
1078 /* This is cycle times not frequency - watch the logic! */
1079 if (pio > 240) /* PIO2 is 240nS per cycle */
1080 return 1;
1081 return 0;
1082 }
1083 }
1084 return 0;
1085}
1086
1087/**
Tejun Heo49016ac2006-02-21 02:12:11 +09001088 * ata_dev_read_id - Read ID data from the specified device
1089 * @ap: port on which target device resides
1090 * @dev: target device
1091 * @p_class: pointer to class of the target device (may be changed)
1092 * @post_reset: is this read ID post-reset?
Tejun Heod9572b12006-03-01 16:09:35 +09001093 * @p_id: read IDENTIFY page (newly allocated)
Tejun Heo49016ac2006-02-21 02:12:11 +09001094 *
1095 * Read ID data from the specified device. ATA_CMD_ID_ATA is
1096 * performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
Tejun Heoaec5c3c2006-03-25 01:33:34 +09001097 * devices. This function also issues ATA_CMD_INIT_DEV_PARAMS
1098 * for pre-ATA4 drives.
Tejun Heo49016ac2006-02-21 02:12:11 +09001099 *
1100 * LOCKING:
1101 * Kernel thread context (may sleep)
1102 *
1103 * RETURNS:
1104 * 0 on success, -errno otherwise.
1105 */
1106static int ata_dev_read_id(struct ata_port *ap, struct ata_device *dev,
Tejun Heod9572b12006-03-01 16:09:35 +09001107 unsigned int *p_class, int post_reset, u16 **p_id)
Tejun Heo49016ac2006-02-21 02:12:11 +09001108{
1109 unsigned int class = *p_class;
Tejun Heo49016ac2006-02-21 02:12:11 +09001110 struct ata_taskfile tf;
1111 unsigned int err_mask = 0;
Tejun Heod9572b12006-03-01 16:09:35 +09001112 u16 *id;
Tejun Heo49016ac2006-02-21 02:12:11 +09001113 const char *reason;
1114 int rc;
1115
1116 DPRINTK("ENTER, host %u, dev %u\n", ap->id, dev->devno);
1117
Tejun Heo49016ac2006-02-21 02:12:11 +09001118 ata_dev_select(ap, dev->devno, 1, 1); /* select device 0/1 */
1119
Tejun Heod9572b12006-03-01 16:09:35 +09001120 id = kmalloc(sizeof(id[0]) * ATA_ID_WORDS, GFP_KERNEL);
1121 if (id == NULL) {
1122 rc = -ENOMEM;
1123 reason = "out of memory";
1124 goto err_out;
1125 }
1126
Tejun Heo49016ac2006-02-21 02:12:11 +09001127 retry:
1128 ata_tf_init(ap, &tf, dev->devno);
1129
1130 switch (class) {
1131 case ATA_DEV_ATA:
1132 tf.command = ATA_CMD_ID_ATA;
1133 break;
1134 case ATA_DEV_ATAPI:
1135 tf.command = ATA_CMD_ID_ATAPI;
1136 break;
1137 default:
1138 rc = -ENODEV;
1139 reason = "unsupported class";
1140 goto err_out;
1141 }
1142
1143 tf.protocol = ATA_PROT_PIO;
1144
1145 err_mask = ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE,
1146 id, sizeof(id[0]) * ATA_ID_WORDS);
Tejun Heo49016ac2006-02-21 02:12:11 +09001147 if (err_mask) {
1148 rc = -EIO;
1149 reason = "I/O error";
Tejun Heo49016ac2006-02-21 02:12:11 +09001150 goto err_out;
1151 }
1152
1153 swap_buf_le16(id, ATA_ID_WORDS);
1154
Tejun Heo49016ac2006-02-21 02:12:11 +09001155 /* sanity check */
Alan Cox692785e2006-03-27 18:49:19 +01001156 if ((class == ATA_DEV_ATA) != (ata_id_is_ata(id) | ata_id_is_cfa(id))) {
Tejun Heo49016ac2006-02-21 02:12:11 +09001157 rc = -EINVAL;
1158 reason = "device reports illegal type";
1159 goto err_out;
1160 }
1161
1162 if (post_reset && class == ATA_DEV_ATA) {
1163 /*
1164 * The exact sequence expected by certain pre-ATA4 drives is:
1165 * SRST RESET
1166 * IDENTIFY
1167 * INITIALIZE DEVICE PARAMETERS
1168 * anything else..
1169 * Some drives were very specific about that exact sequence.
1170 */
1171 if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
Albert Lee00b6f5e2006-03-27 16:39:18 +08001172 err_mask = ata_dev_init_params(ap, dev, id[3], id[6]);
Tejun Heo49016ac2006-02-21 02:12:11 +09001173 if (err_mask) {
1174 rc = -EIO;
1175 reason = "INIT_DEV_PARAMS failed";
1176 goto err_out;
1177 }
1178
1179 /* current CHS translation info (id[53-58]) might be
1180 * changed. reread the identify device info.
1181 */
1182 post_reset = 0;
1183 goto retry;
1184 }
1185 }
1186
1187 *p_class = class;
Tejun Heod9572b12006-03-01 16:09:35 +09001188 *p_id = id;
Tejun Heo49016ac2006-02-21 02:12:11 +09001189 return 0;
1190
1191 err_out:
1192 printk(KERN_WARNING "ata%u: dev %u failed to IDENTIFY (%s)\n",
1193 ap->id, dev->devno, reason);
Tejun Heod9572b12006-03-01 16:09:35 +09001194 kfree(id);
Tejun Heo49016ac2006-02-21 02:12:11 +09001195 return rc;
1196}
1197
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09001198static inline u8 ata_dev_knobble(const struct ata_port *ap,
1199 struct ata_device *dev)
1200{
1201 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
1202}
1203
Tejun Heo49016ac2006-02-21 02:12:11 +09001204/**
Tejun Heoffeae412006-03-01 16:09:35 +09001205 * ata_dev_configure - Configure the specified ATA/ATAPI device
1206 * @ap: Port on which target device resides
1207 * @dev: Target device to configure
Tejun Heo4c2d7212006-03-05 17:55:58 +09001208 * @print_info: Enable device info printout
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 *
Tejun Heoffeae412006-03-01 16:09:35 +09001210 * Configure @dev according to @dev->id. Generic and low-level
1211 * driver specific fixups are also applied.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 *
1213 * LOCKING:
Tejun Heoffeae412006-03-01 16:09:35 +09001214 * Kernel thread context (may sleep)
1215 *
1216 * RETURNS:
1217 * 0 on success, -errno otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 */
Tejun Heo4c2d7212006-03-05 17:55:58 +09001219static int ata_dev_configure(struct ata_port *ap, struct ata_device *dev,
1220 int print_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221{
Tejun Heo1148c3a2006-03-13 19:48:04 +09001222 const u16 *id = dev->id;
Tejun Heoff8854b2006-03-06 04:31:56 +09001223 unsigned int xfer_mask;
Tejun Heo6e7846e2006-02-12 23:32:58 +09001224 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
Tejun Heoe1211e32006-04-01 01:38:18 +09001226 if (!ata_dev_enabled(dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n",
Tejun Heoffeae412006-03-01 16:09:35 +09001228 ap->id, dev->devno);
1229 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 }
1231
Tejun Heoffeae412006-03-01 16:09:35 +09001232 DPRINTK("ENTER, host %u, dev %u\n", ap->id, dev->devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
Tejun Heoc39f5eb2006-03-13 19:51:19 +09001234 /* print device capabilities */
1235 if (print_info)
1236 printk(KERN_DEBUG "ata%u: dev %u cfg 49:%04x 82:%04x 83:%04x "
1237 "84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n",
1238 ap->id, dev->devno, id[49], id[82], id[83],
1239 id[84], id[85], id[86], id[87], id[88]);
1240
Tejun Heo208a9932006-03-05 17:55:58 +09001241 /* initialize to-be-configured parameters */
1242 dev->flags = 0;
1243 dev->max_sectors = 0;
1244 dev->cdb_len = 0;
1245 dev->n_sectors = 0;
1246 dev->cylinders = 0;
1247 dev->heads = 0;
1248 dev->sectors = 0;
1249
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 /*
1251 * common ATA, ATAPI feature tests
1252 */
1253
Tejun Heoff8854b2006-03-06 04:31:56 +09001254 /* find max transfer mode; for printk only */
Tejun Heo1148c3a2006-03-13 19:48:04 +09001255 xfer_mask = ata_id_xfermask(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
Tejun Heo1148c3a2006-03-13 19:48:04 +09001257 ata_dump_id(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258
1259 /* ATA-specific feature tests */
1260 if (dev->class == ATA_DEV_ATA) {
Tejun Heo1148c3a2006-03-13 19:48:04 +09001261 dev->n_sectors = ata_id_n_sectors(id);
Tejun Heo29407402006-02-12 22:47:04 +09001262
Tejun Heo1148c3a2006-03-13 19:48:04 +09001263 if (ata_id_has_lba(id)) {
Tejun Heo4c2d7212006-03-05 17:55:58 +09001264 const char *lba_desc;
Albert Lee8bf62ec2005-05-12 15:29:42 -04001265
Tejun Heo4c2d7212006-03-05 17:55:58 +09001266 lba_desc = "LBA";
1267 dev->flags |= ATA_DFLAG_LBA;
Tejun Heo1148c3a2006-03-13 19:48:04 +09001268 if (ata_id_has_lba48(id)) {
Albert Lee8bf62ec2005-05-12 15:29:42 -04001269 dev->flags |= ATA_DFLAG_LBA48;
Tejun Heo4c2d7212006-03-05 17:55:58 +09001270 lba_desc = "LBA48";
1271 }
Albert Lee8bf62ec2005-05-12 15:29:42 -04001272
1273 /* print device info to dmesg */
Tejun Heo4c2d7212006-03-05 17:55:58 +09001274 if (print_info)
1275 printk(KERN_INFO "ata%u: dev %u ATA-%d, "
1276 "max %s, %Lu sectors: %s\n",
1277 ap->id, dev->devno,
Tejun Heo1148c3a2006-03-13 19:48:04 +09001278 ata_id_major_version(id),
Tejun Heoff8854b2006-03-06 04:31:56 +09001279 ata_mode_string(xfer_mask),
Tejun Heo4c2d7212006-03-05 17:55:58 +09001280 (unsigned long long)dev->n_sectors,
1281 lba_desc);
Tejun Heoffeae412006-03-01 16:09:35 +09001282 } else {
Albert Lee8bf62ec2005-05-12 15:29:42 -04001283 /* CHS */
1284
1285 /* Default translation */
Tejun Heo1148c3a2006-03-13 19:48:04 +09001286 dev->cylinders = id[1];
1287 dev->heads = id[3];
1288 dev->sectors = id[6];
Albert Lee8bf62ec2005-05-12 15:29:42 -04001289
Tejun Heo1148c3a2006-03-13 19:48:04 +09001290 if (ata_id_current_chs_valid(id)) {
Albert Lee8bf62ec2005-05-12 15:29:42 -04001291 /* Current CHS translation is valid. */
Tejun Heo1148c3a2006-03-13 19:48:04 +09001292 dev->cylinders = id[54];
1293 dev->heads = id[55];
1294 dev->sectors = id[56];
Albert Lee8bf62ec2005-05-12 15:29:42 -04001295 }
1296
1297 /* print device info to dmesg */
Tejun Heo4c2d7212006-03-05 17:55:58 +09001298 if (print_info)
1299 printk(KERN_INFO "ata%u: dev %u ATA-%d, "
1300 "max %s, %Lu sectors: CHS %u/%u/%u\n",
1301 ap->id, dev->devno,
Tejun Heo1148c3a2006-03-13 19:48:04 +09001302 ata_id_major_version(id),
Tejun Heoff8854b2006-03-06 04:31:56 +09001303 ata_mode_string(xfer_mask),
Tejun Heo4c2d7212006-03-05 17:55:58 +09001304 (unsigned long long)dev->n_sectors,
1305 dev->cylinders, dev->heads, dev->sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 }
1307
Tejun Heo6e7846e2006-02-12 23:32:58 +09001308 dev->cdb_len = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 }
1310
1311 /* ATAPI-specific feature tests */
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05001312 else if (dev->class == ATA_DEV_ATAPI) {
Tejun Heo1148c3a2006-03-13 19:48:04 +09001313 rc = atapi_cdb_len(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
1315 printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id);
Tejun Heoffeae412006-03-01 16:09:35 +09001316 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 goto err_out_nosup;
1318 }
Tejun Heo6e7846e2006-02-12 23:32:58 +09001319 dev->cdb_len = (unsigned int) rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
1321 /* print device info to dmesg */
Tejun Heo4c2d7212006-03-05 17:55:58 +09001322 if (print_info)
1323 printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n",
Tejun Heoff8854b2006-03-06 04:31:56 +09001324 ap->id, dev->devno, ata_mode_string(xfer_mask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 }
1326
Tejun Heo6e7846e2006-02-12 23:32:58 +09001327 ap->host->max_cmd_len = 0;
1328 for (i = 0; i < ATA_MAX_DEVICES; i++)
1329 ap->host->max_cmd_len = max_t(unsigned int,
1330 ap->host->max_cmd_len,
1331 ap->device[i].cdb_len);
1332
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09001333 /* limit bridge transfers to udma5, 200 sectors */
1334 if (ata_dev_knobble(ap, dev)) {
Tejun Heo4c2d7212006-03-05 17:55:58 +09001335 if (print_info)
1336 printk(KERN_INFO "ata%u(%u): applying bridge limits\n",
1337 ap->id, dev->devno);
Tejun Heo5a529132006-03-24 14:07:50 +09001338 dev->udma_mask &= ATA_UDMA5;
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09001339 dev->max_sectors = ATA_MAX_SECTORS;
1340 }
1341
1342 if (ap->ops->dev_config)
1343 ap->ops->dev_config(ap, dev);
1344
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap));
Tejun Heoffeae412006-03-01 16:09:35 +09001346 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
1348err_out_nosup:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 DPRINTK("EXIT, err\n");
Tejun Heoffeae412006-03-01 16:09:35 +09001350 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351}
1352
1353/**
1354 * ata_bus_probe - Reset and probe ATA bus
1355 * @ap: Bus to probe
1356 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001357 * Master ATA bus probing function. Initiates a hardware-dependent
1358 * bus reset, then attempts to identify any devices found on
1359 * the bus.
1360 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001362 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 *
1364 * RETURNS:
Tejun Heo96072e62006-04-01 01:38:17 +09001365 * Zero on success, negative errno otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 */
1367
1368static int ata_bus_probe(struct ata_port *ap)
1369{
Tejun Heo28ca5c52006-03-01 16:09:36 +09001370 unsigned int classes[ATA_MAX_DEVICES];
Tejun Heo96072e62006-04-01 01:38:17 +09001371 int i, rc, found = 0;
Tejun Heoe82cbdb2006-04-01 01:38:18 +09001372 struct ata_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373
Tejun Heo28ca5c52006-03-01 16:09:36 +09001374 ata_port_probe(ap);
1375
Tejun Heo20444702006-03-13 01:57:01 +09001376 /* reset and determine device classes */
1377 for (i = 0; i < ATA_MAX_DEVICES; i++)
1378 classes[i] = ATA_DEV_UNKNOWN;
Tejun Heo2061a472006-03-12 00:57:39 +09001379
Tejun Heo20444702006-03-13 01:57:01 +09001380 if (ap->ops->probe_reset) {
Tejun Heoc19ba8a2006-01-24 17:05:22 +09001381 rc = ap->ops->probe_reset(ap, classes);
Tejun Heo28ca5c52006-03-01 16:09:36 +09001382 if (rc) {
1383 printk("ata%u: reset failed (errno=%d)\n", ap->id, rc);
1384 return rc;
Tejun Heoc19ba8a2006-01-24 17:05:22 +09001385 }
Tejun Heo28ca5c52006-03-01 16:09:36 +09001386 } else {
Tejun Heoc19ba8a2006-01-24 17:05:22 +09001387 ap->ops->phy_reset(ap);
1388
Tejun Heo20444702006-03-13 01:57:01 +09001389 if (!(ap->flags & ATA_FLAG_PORT_DISABLED))
1390 for (i = 0; i < ATA_MAX_DEVICES; i++)
Tejun Heo28ca5c52006-03-01 16:09:36 +09001391 classes[i] = ap->device[i].class;
Tejun Heo20444702006-03-13 01:57:01 +09001392
Tejun Heo28ca5c52006-03-01 16:09:36 +09001393 ata_port_probe(ap);
1394 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
Tejun Heo20444702006-03-13 01:57:01 +09001396 for (i = 0; i < ATA_MAX_DEVICES; i++)
1397 if (classes[i] == ATA_DEV_UNKNOWN)
1398 classes[i] = ATA_DEV_NONE;
1399
Tejun Heo28ca5c52006-03-01 16:09:36 +09001400 /* read IDENTIFY page and configure devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 for (i = 0; i < ATA_MAX_DEVICES; i++) {
Tejun Heoe82cbdb2006-04-01 01:38:18 +09001402 dev = &ap->device[i];
Tejun Heo28ca5c52006-03-01 16:09:36 +09001403 dev->class = classes[i];
1404
Tejun Heoe1211e32006-04-01 01:38:18 +09001405 if (!ata_dev_enabled(dev))
Tejun Heoffeae412006-03-01 16:09:35 +09001406 continue;
1407
1408 WARN_ON(dev->id != NULL);
1409 if (ata_dev_read_id(ap, dev, &dev->class, 1, &dev->id)) {
1410 dev->class = ATA_DEV_NONE;
1411 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 }
Tejun Heoffeae412006-03-01 16:09:35 +09001413
Tejun Heo4c2d7212006-03-05 17:55:58 +09001414 if (ata_dev_configure(ap, dev, 1)) {
Tejun Heofcef9782006-03-24 15:25:31 +09001415 ata_dev_disable(ap, dev);
Tejun Heoffeae412006-03-01 16:09:35 +09001416 continue;
1417 }
1418
Tejun Heoffeae412006-03-01 16:09:35 +09001419 found = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 }
1421
Tejun Heoe82cbdb2006-04-01 01:38:18 +09001422 /* configure transfer mode */
1423 if (ap->ops->set_mode) {
1424 /* FIXME: make ->set_mode handle no device case and
1425 * return error code and failing device on failure as
1426 * ata_set_mode() does.
1427 */
1428 if (found)
1429 ap->ops->set_mode(ap);
1430 rc = 0;
1431 } else {
1432 while (ata_set_mode(ap, &dev))
1433 ata_dev_disable(ap, dev);
1434 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
Tejun Heoe82cbdb2006-04-01 01:38:18 +09001436 for (i = 0; i < ATA_MAX_DEVICES; i++)
1437 if (ata_dev_enabled(&ap->device[i]))
1438 return 0;
Alan Coxe35a9e02006-03-27 18:46:37 +01001439
Tejun Heoe82cbdb2006-04-01 01:38:18 +09001440 /* no device present, disable port */
1441 ata_port_disable(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 ap->ops->port_disable(ap);
Tejun Heo96072e62006-04-01 01:38:17 +09001443 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444}
1445
1446/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001447 * ata_port_probe - Mark port as enabled
1448 * @ap: Port for which we indicate enablement
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001450 * Modify @ap data structure such that the system
1451 * thinks that the entire port is enabled.
1452 *
1453 * LOCKING: host_set lock, or some other form of
1454 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001455 */
1456
1457void ata_port_probe(struct ata_port *ap)
1458{
1459 ap->flags &= ~ATA_FLAG_PORT_DISABLED;
1460}
1461
1462/**
Tejun Heo3be680b2005-12-19 22:35:02 +09001463 * sata_print_link_status - Print SATA link status
1464 * @ap: SATA port to printk link status about
1465 *
1466 * This function prints link speed and status of a SATA link.
1467 *
1468 * LOCKING:
1469 * None.
1470 */
1471static void sata_print_link_status(struct ata_port *ap)
1472{
1473 u32 sstatus, tmp;
Tejun Heo3be680b2005-12-19 22:35:02 +09001474
1475 if (!ap->ops->scr_read)
1476 return;
1477
1478 sstatus = scr_read(ap, SCR_STATUS);
1479
1480 if (sata_dev_present(ap)) {
1481 tmp = (sstatus >> 4) & 0xf;
Tejun Heo4c360c82006-04-01 01:38:17 +09001482 printk(KERN_INFO "ata%u: SATA link up %s (SStatus %X)\n",
1483 ap->id, sata_spd_string(tmp), sstatus);
Tejun Heo3be680b2005-12-19 22:35:02 +09001484 } else {
1485 printk(KERN_INFO "ata%u: SATA link down (SStatus %X)\n",
1486 ap->id, sstatus);
1487 }
1488}
1489
1490/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001491 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1492 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001494 * This function issues commands to standard SATA Sxxx
1495 * PHY registers, to wake up the phy (and device), and
1496 * clear any reset condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 *
1498 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001499 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 *
1501 */
1502void __sata_phy_reset(struct ata_port *ap)
1503{
1504 u32 sstatus;
1505 unsigned long timeout = jiffies + (HZ * 5);
1506
1507 if (ap->flags & ATA_FLAG_SATA_RESET) {
Brett Russcdcca892005-03-28 15:10:27 -05001508 /* issue phy wake/reset */
1509 scr_write_flush(ap, SCR_CONTROL, 0x301);
Tejun Heo62ba2842005-06-26 23:27:19 +09001510 /* Couldn't find anything in SATA I/II specs, but
1511 * AHCI-1.1 10.4.2 says at least 1 ms. */
1512 mdelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 }
Brett Russcdcca892005-03-28 15:10:27 -05001514 scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515
1516 /* wait for phy to become ready, if necessary */
1517 do {
1518 msleep(200);
1519 sstatus = scr_read(ap, SCR_STATUS);
1520 if ((sstatus & 0xf) != 1)
1521 break;
1522 } while (time_before(jiffies, timeout));
1523
Tejun Heo3be680b2005-12-19 22:35:02 +09001524 /* print link status */
1525 sata_print_link_status(ap);
Jeff Garzik656563e2005-11-20 03:36:45 -05001526
Tejun Heo3be680b2005-12-19 22:35:02 +09001527 /* TODO: phy layer with polling, timeouts, etc. */
1528 if (sata_dev_present(ap))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 ata_port_probe(ap);
Tejun Heo3be680b2005-12-19 22:35:02 +09001530 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 ata_port_disable(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
1533 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1534 return;
1535
1536 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1537 ata_port_disable(ap);
1538 return;
1539 }
1540
1541 ap->cbl = ATA_CBL_SATA;
1542}
1543
1544/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001545 * sata_phy_reset - Reset SATA bus.
1546 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001548 * This function resets the SATA bus, and then probes
1549 * the bus for devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 *
1551 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001552 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 *
1554 */
1555void sata_phy_reset(struct ata_port *ap)
1556{
1557 __sata_phy_reset(ap);
1558 if (ap->flags & ATA_FLAG_PORT_DISABLED)
1559 return;
1560 ata_bus_reset(ap);
1561}
1562
1563/**
Alan Coxebdfca62006-03-23 15:38:34 +00001564 * ata_dev_pair - return other device on cable
1565 * @ap: port
1566 * @adev: device
1567 *
1568 * Obtain the other device on the same cable, or if none is
1569 * present NULL is returned
1570 */
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001571
Alan Coxebdfca62006-03-23 15:38:34 +00001572struct ata_device *ata_dev_pair(struct ata_port *ap, struct ata_device *adev)
1573{
1574 struct ata_device *pair = &ap->device[1 - adev->devno];
Tejun Heoe1211e32006-04-01 01:38:18 +09001575 if (!ata_dev_enabled(pair))
Alan Coxebdfca62006-03-23 15:38:34 +00001576 return NULL;
1577 return pair;
1578}
1579
1580/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001581 * ata_port_disable - Disable port.
1582 * @ap: Port to be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001584 * Modify @ap data structure such that the system
1585 * thinks that the entire port is disabled, and should
1586 * never attempt to probe or communicate with devices
1587 * on this port.
1588 *
1589 * LOCKING: host_set lock, or some other form of
1590 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 */
1592
1593void ata_port_disable(struct ata_port *ap)
1594{
1595 ap->device[0].class = ATA_DEV_NONE;
1596 ap->device[1].class = ATA_DEV_NONE;
1597 ap->flags |= ATA_FLAG_PORT_DISABLED;
1598}
1599
Tejun Heo1c3fae42006-04-02 20:53:28 +09001600/**
1601 * ata_down_sata_spd_limit - adjust SATA spd limit downward
1602 * @ap: Port to adjust SATA spd limit for
1603 *
1604 * Adjust SATA spd limit of @ap downward. Note that this
1605 * function only adjusts the limit. The change must be applied
1606 * using ata_set_sata_spd().
1607 *
1608 * LOCKING:
1609 * Inherited from caller.
1610 *
1611 * RETURNS:
1612 * 0 on success, negative errno on failure
1613 */
1614static int ata_down_sata_spd_limit(struct ata_port *ap)
1615{
1616 u32 spd, mask;
1617 int highbit;
1618
1619 if (ap->cbl != ATA_CBL_SATA || !ap->ops->scr_read)
1620 return -EOPNOTSUPP;
1621
1622 mask = ap->sata_spd_limit;
1623 if (mask <= 1)
1624 return -EINVAL;
1625 highbit = fls(mask) - 1;
1626 mask &= ~(1 << highbit);
1627
1628 spd = (scr_read(ap, SCR_STATUS) >> 4) & 0xf;
1629 if (spd <= 1)
1630 return -EINVAL;
1631 spd--;
1632 mask &= (1 << spd) - 1;
1633 if (!mask)
1634 return -EINVAL;
1635
1636 ap->sata_spd_limit = mask;
1637
1638 printk(KERN_WARNING "ata%u: limiting SATA link speed to %s\n",
1639 ap->id, sata_spd_string(fls(mask)));
1640
1641 return 0;
1642}
1643
1644static int __ata_set_sata_spd_needed(struct ata_port *ap, u32 *scontrol)
1645{
1646 u32 spd, limit;
1647
1648 if (ap->sata_spd_limit == UINT_MAX)
1649 limit = 0;
1650 else
1651 limit = fls(ap->sata_spd_limit);
1652
1653 spd = (*scontrol >> 4) & 0xf;
1654 *scontrol = (*scontrol & ~0xf0) | ((limit & 0xf) << 4);
1655
1656 return spd != limit;
1657}
1658
1659/**
1660 * ata_set_sata_spd_needed - is SATA spd configuration needed
1661 * @ap: Port in question
1662 *
1663 * Test whether the spd limit in SControl matches
1664 * @ap->sata_spd_limit. This function is used to determine
1665 * whether hardreset is necessary to apply SATA spd
1666 * configuration.
1667 *
1668 * LOCKING:
1669 * Inherited from caller.
1670 *
1671 * RETURNS:
1672 * 1 if SATA spd configuration is needed, 0 otherwise.
1673 */
1674static int ata_set_sata_spd_needed(struct ata_port *ap)
1675{
1676 u32 scontrol;
1677
1678 if (ap->cbl != ATA_CBL_SATA || !ap->ops->scr_read)
1679 return 0;
1680
1681 scontrol = scr_read(ap, SCR_CONTROL);
1682
1683 return __ata_set_sata_spd_needed(ap, &scontrol);
1684}
1685
1686/**
1687 * ata_set_sata_spd - set SATA spd according to spd limit
1688 * @ap: Port to set SATA spd for
1689 *
1690 * Set SATA spd of @ap according to sata_spd_limit.
1691 *
1692 * LOCKING:
1693 * Inherited from caller.
1694 *
1695 * RETURNS:
1696 * 0 if spd doesn't need to be changed, 1 if spd has been
1697 * changed. -EOPNOTSUPP if SCR registers are inaccessible.
1698 */
1699static int ata_set_sata_spd(struct ata_port *ap)
1700{
1701 u32 scontrol;
1702
1703 if (ap->cbl != ATA_CBL_SATA || !ap->ops->scr_read)
1704 return -EOPNOTSUPP;
1705
1706 scontrol = scr_read(ap, SCR_CONTROL);
1707 if (!__ata_set_sata_spd_needed(ap, &scontrol))
1708 return 0;
1709
1710 scr_write(ap, SCR_CONTROL, scontrol);
1711 return 1;
1712}
1713
Alan Cox452503f2005-10-21 19:01:32 -04001714/*
1715 * This mode timing computation functionality is ported over from
1716 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
1717 */
1718/*
1719 * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
1720 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
1721 * for PIO 5, which is a nonstandard extension and UDMA6, which
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001722 * is currently supported only by Maxtor drives.
Alan Cox452503f2005-10-21 19:01:32 -04001723 */
1724
1725static const struct ata_timing ata_timing[] = {
1726
1727 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
1728 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
1729 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
1730 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
1731
1732 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
1733 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
1734 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
1735
1736/* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001737
Alan Cox452503f2005-10-21 19:01:32 -04001738 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
1739 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
1740 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001741
Alan Cox452503f2005-10-21 19:01:32 -04001742 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
1743 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
1744 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
1745
1746/* { XFER_PIO_5, 20, 50, 30, 100, 50, 30, 100, 0 }, */
1747 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
1748 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
1749
1750 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
1751 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
1752 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
1753
1754/* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
1755
1756 { 0xFF }
1757};
1758
1759#define ENOUGH(v,unit) (((v)-1)/(unit)+1)
1760#define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
1761
1762static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
1763{
1764 q->setup = EZ(t->setup * 1000, T);
1765 q->act8b = EZ(t->act8b * 1000, T);
1766 q->rec8b = EZ(t->rec8b * 1000, T);
1767 q->cyc8b = EZ(t->cyc8b * 1000, T);
1768 q->active = EZ(t->active * 1000, T);
1769 q->recover = EZ(t->recover * 1000, T);
1770 q->cycle = EZ(t->cycle * 1000, T);
1771 q->udma = EZ(t->udma * 1000, UT);
1772}
1773
1774void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
1775 struct ata_timing *m, unsigned int what)
1776{
1777 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
1778 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
1779 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
1780 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
1781 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
1782 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
1783 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
1784 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
1785}
1786
1787static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
1788{
1789 const struct ata_timing *t;
1790
1791 for (t = ata_timing; t->mode != speed; t++)
Alan Cox91190752005-10-26 12:17:46 -04001792 if (t->mode == 0xFF)
Alan Cox452503f2005-10-21 19:01:32 -04001793 return NULL;
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001794 return t;
Alan Cox452503f2005-10-21 19:01:32 -04001795}
1796
1797int ata_timing_compute(struct ata_device *adev, unsigned short speed,
1798 struct ata_timing *t, int T, int UT)
1799{
1800 const struct ata_timing *s;
1801 struct ata_timing p;
1802
1803 /*
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001804 * Find the mode.
Albert Lee75b1f2f2005-11-16 17:06:18 +08001805 */
Alan Cox452503f2005-10-21 19:01:32 -04001806
1807 if (!(s = ata_timing_find_mode(speed)))
1808 return -EINVAL;
1809
Albert Lee75b1f2f2005-11-16 17:06:18 +08001810 memcpy(t, s, sizeof(*s));
1811
Alan Cox452503f2005-10-21 19:01:32 -04001812 /*
1813 * If the drive is an EIDE drive, it can tell us it needs extended
1814 * PIO/MW_DMA cycle timing.
1815 */
1816
1817 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
1818 memset(&p, 0, sizeof(p));
1819 if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
1820 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
1821 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
1822 } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
1823 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
1824 }
1825 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
1826 }
1827
1828 /*
1829 * Convert the timing to bus clock counts.
1830 */
1831
Albert Lee75b1f2f2005-11-16 17:06:18 +08001832 ata_timing_quantize(t, t, T, UT);
Alan Cox452503f2005-10-21 19:01:32 -04001833
1834 /*
Randy Dunlapc893a3a2006-01-28 13:15:32 -05001835 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
1836 * S.M.A.R.T * and some other commands. We have to ensure that the
1837 * DMA cycle timing is slower/equal than the fastest PIO timing.
Alan Cox452503f2005-10-21 19:01:32 -04001838 */
1839
1840 if (speed > XFER_PIO_4) {
1841 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
1842 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
1843 }
1844
1845 /*
Randy Dunlapc893a3a2006-01-28 13:15:32 -05001846 * Lengthen active & recovery time so that cycle time is correct.
Alan Cox452503f2005-10-21 19:01:32 -04001847 */
1848
1849 if (t->act8b + t->rec8b < t->cyc8b) {
1850 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
1851 t->rec8b = t->cyc8b - t->act8b;
1852 }
1853
1854 if (t->active + t->recover < t->cycle) {
1855 t->active += (t->cycle - (t->active + t->recover)) / 2;
1856 t->recover = t->cycle - t->active;
1857 }
1858
1859 return 0;
1860}
1861
Tejun Heo83206a22006-03-24 15:25:31 +09001862static int ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863{
Tejun Heo83206a22006-03-24 15:25:31 +09001864 unsigned int err_mask;
1865 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
1867 if (dev->xfer_shift == ATA_SHIFT_PIO)
1868 dev->flags |= ATA_DFLAG_PIO;
1869
Tejun Heo83206a22006-03-24 15:25:31 +09001870 err_mask = ata_dev_set_xfermode(ap, dev);
1871 if (err_mask) {
1872 printk(KERN_ERR
1873 "ata%u: failed to set xfermode (err_mask=0x%x)\n",
1874 ap->id, err_mask);
1875 return -EIO;
1876 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877
Tejun Heo83206a22006-03-24 15:25:31 +09001878 rc = ata_dev_revalidate(ap, dev, 0);
1879 if (rc) {
1880 printk(KERN_ERR
1881 "ata%u: failed to revalidate after set xfermode\n",
1882 ap->id);
1883 return rc;
Tejun Heo48a8a142006-03-05 17:55:58 +09001884 }
1885
Tejun Heo23e71c32006-03-06 04:31:57 +09001886 DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n",
1887 dev->xfer_shift, (int)dev->xfer_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888
1889 printk(KERN_INFO "ata%u: dev %u configured for %s\n",
Tejun Heo23e71c32006-03-06 04:31:57 +09001890 ap->id, dev->devno,
1891 ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)));
Tejun Heo83206a22006-03-24 15:25:31 +09001892 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893}
1894
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895/**
1896 * ata_set_mode - Program timings and issue SET FEATURES - XFER
1897 * @ap: port on which timings will be programmed
Tejun Heoe82cbdb2006-04-01 01:38:18 +09001898 * @r_failed_dev: out paramter for failed device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 *
Tejun Heoe82cbdb2006-04-01 01:38:18 +09001900 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.). If
1901 * ata_set_mode() fails, pointer to the failing device is
1902 * returned in @r_failed_dev.
Jeff Garzik780a87f2005-05-30 15:41:05 -04001903 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001905 * PCI/etc. bus probe sem.
Tejun Heoe82cbdb2006-04-01 01:38:18 +09001906 *
1907 * RETURNS:
1908 * 0 on success, negative errno otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 */
Tejun Heoe82cbdb2006-04-01 01:38:18 +09001910static int ata_set_mode(struct ata_port *ap, struct ata_device **r_failed_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911{
Tejun Heoe8e06192006-04-01 01:38:18 +09001912 struct ata_device *dev;
Tejun Heoe82cbdb2006-04-01 01:38:18 +09001913 int i, rc = 0, used_dma = 0, found = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914
Tejun Heoa6d5a512006-03-06 04:31:57 +09001915 /* step 1: calculate xfer_mask */
1916 for (i = 0; i < ATA_MAX_DEVICES; i++) {
Tejun Heoacf356b2006-03-24 14:07:50 +09001917 unsigned int pio_mask, dma_mask;
Tejun Heoa6d5a512006-03-06 04:31:57 +09001918
Tejun Heoe8e06192006-04-01 01:38:18 +09001919 dev = &ap->device[i];
1920
Tejun Heoe1211e32006-04-01 01:38:18 +09001921 if (!ata_dev_enabled(dev))
Tejun Heoa6d5a512006-03-06 04:31:57 +09001922 continue;
1923
Tejun Heoacf356b2006-03-24 14:07:50 +09001924 ata_dev_xfermask(ap, dev);
Tejun Heoa6d5a512006-03-06 04:31:57 +09001925
Tejun Heoacf356b2006-03-24 14:07:50 +09001926 pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0);
1927 dma_mask = ata_pack_xfermask(0, dev->mwdma_mask, dev->udma_mask);
1928 dev->pio_mode = ata_xfer_mask2mode(pio_mask);
1929 dev->dma_mode = ata_xfer_mask2mode(dma_mask);
Alan Cox5444a6f2006-03-27 18:58:20 +01001930
Tejun Heo4f659772006-04-01 01:38:18 +09001931 found = 1;
Alan Cox5444a6f2006-03-27 18:58:20 +01001932 if (dev->dma_mode)
1933 used_dma = 1;
Tejun Heoa6d5a512006-03-06 04:31:57 +09001934 }
Tejun Heo4f659772006-04-01 01:38:18 +09001935 if (!found)
Tejun Heoe82cbdb2006-04-01 01:38:18 +09001936 goto out;
Tejun Heoa6d5a512006-03-06 04:31:57 +09001937
1938 /* step 2: always set host PIO timings */
Tejun Heoe8e06192006-04-01 01:38:18 +09001939 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1940 dev = &ap->device[i];
1941 if (!ata_dev_enabled(dev))
1942 continue;
1943
1944 if (!dev->pio_mode) {
1945 printk(KERN_WARNING "ata%u: dev %u no PIO support\n",
1946 ap->id, dev->devno);
1947 rc = -EINVAL;
Tejun Heoe82cbdb2006-04-01 01:38:18 +09001948 goto out;
Tejun Heoe8e06192006-04-01 01:38:18 +09001949 }
1950
1951 dev->xfer_mode = dev->pio_mode;
1952 dev->xfer_shift = ATA_SHIFT_PIO;
1953 if (ap->ops->set_piomode)
1954 ap->ops->set_piomode(ap, dev);
1955 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956
Tejun Heoa6d5a512006-03-06 04:31:57 +09001957 /* step 3: set host DMA timings */
Tejun Heoe8e06192006-04-01 01:38:18 +09001958 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1959 dev = &ap->device[i];
1960
1961 if (!ata_dev_enabled(dev) || !dev->dma_mode)
1962 continue;
1963
1964 dev->xfer_mode = dev->dma_mode;
1965 dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
1966 if (ap->ops->set_dmamode)
1967 ap->ops->set_dmamode(ap, dev);
1968 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
1970 /* step 4: update devices' xfer mode */
Tejun Heo83206a22006-03-24 15:25:31 +09001971 for (i = 0; i < ATA_MAX_DEVICES; i++) {
Tejun Heoe8e06192006-04-01 01:38:18 +09001972 dev = &ap->device[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973
Tejun Heoe1211e32006-04-01 01:38:18 +09001974 if (!ata_dev_enabled(dev))
Tejun Heo83206a22006-03-24 15:25:31 +09001975 continue;
1976
Tejun Heo5bbc53f2006-04-01 01:38:17 +09001977 rc = ata_dev_set_mode(ap, dev);
1978 if (rc)
Tejun Heoe82cbdb2006-04-01 01:38:18 +09001979 goto out;
Tejun Heo83206a22006-03-24 15:25:31 +09001980 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
Tejun Heoe8e06192006-04-01 01:38:18 +09001982 /* Record simplex status. If we selected DMA then the other
1983 * host channels are not permitted to do so.
Alan Cox5444a6f2006-03-27 18:58:20 +01001984 */
Alan Cox5444a6f2006-03-27 18:58:20 +01001985 if (used_dma && (ap->host_set->flags & ATA_HOST_SIMPLEX))
1986 ap->host_set->simplex_claimed = 1;
1987
Tejun Heoe8e06192006-04-01 01:38:18 +09001988 /* step5: chip specific finalisation */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 if (ap->ops->post_set_mode)
1990 ap->ops->post_set_mode(ap);
1991
Tejun Heoe82cbdb2006-04-01 01:38:18 +09001992 out:
1993 if (rc)
1994 *r_failed_dev = dev;
1995 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996}
1997
1998/**
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05001999 * ata_tf_to_host - issue ATA taskfile to host controller
2000 * @ap: port to which command is being issued
2001 * @tf: ATA taskfile register set
2002 *
2003 * Issues ATA taskfile register set to ATA host controller,
2004 * with proper synchronization with interrupt handler and
2005 * other threads.
2006 *
2007 * LOCKING:
2008 * spin_lock_irqsave(host_set lock)
2009 */
2010
2011static inline void ata_tf_to_host(struct ata_port *ap,
2012 const struct ata_taskfile *tf)
2013{
2014 ap->ops->tf_load(ap, tf);
2015 ap->ops->exec_command(ap, tf);
2016}
2017
2018/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 * ata_busy_sleep - sleep until BSY clears, or timeout
2020 * @ap: port containing status register to be polled
2021 * @tmout_pat: impatience timeout
2022 * @tmout: overall timeout
2023 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002024 * Sleep until ATA Status register bit BSY clears,
2025 * or a timeout occurs.
2026 *
2027 * LOCKING: None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 */
2029
Tejun Heo6f8b9952006-01-24 17:05:21 +09002030unsigned int ata_busy_sleep (struct ata_port *ap,
2031 unsigned long tmout_pat, unsigned long tmout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032{
2033 unsigned long timer_start, timeout;
2034 u8 status;
2035
2036 status = ata_busy_wait(ap, ATA_BUSY, 300);
2037 timer_start = jiffies;
2038 timeout = timer_start + tmout_pat;
2039 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
2040 msleep(50);
2041 status = ata_busy_wait(ap, ATA_BUSY, 3);
2042 }
2043
2044 if (status & ATA_BUSY)
2045 printk(KERN_WARNING "ata%u is slow to respond, "
2046 "please be patient\n", ap->id);
2047
2048 timeout = timer_start + tmout;
2049 while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) {
2050 msleep(50);
2051 status = ata_chk_status(ap);
2052 }
2053
2054 if (status & ATA_BUSY) {
2055 printk(KERN_ERR "ata%u failed to respond (%lu secs)\n",
2056 ap->id, tmout / HZ);
2057 return 1;
2058 }
2059
2060 return 0;
2061}
2062
2063static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
2064{
2065 struct ata_ioports *ioaddr = &ap->ioaddr;
2066 unsigned int dev0 = devmask & (1 << 0);
2067 unsigned int dev1 = devmask & (1 << 1);
2068 unsigned long timeout;
2069
2070 /* if device 0 was found in ata_devchk, wait for its
2071 * BSY bit to clear
2072 */
2073 if (dev0)
2074 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2075
2076 /* if device 1 was found in ata_devchk, wait for
2077 * register access, then wait for BSY to clear
2078 */
2079 timeout = jiffies + ATA_TMOUT_BOOT;
2080 while (dev1) {
2081 u8 nsect, lbal;
2082
2083 ap->ops->dev_select(ap, 1);
2084 if (ap->flags & ATA_FLAG_MMIO) {
2085 nsect = readb((void __iomem *) ioaddr->nsect_addr);
2086 lbal = readb((void __iomem *) ioaddr->lbal_addr);
2087 } else {
2088 nsect = inb(ioaddr->nsect_addr);
2089 lbal = inb(ioaddr->lbal_addr);
2090 }
2091 if ((nsect == 1) && (lbal == 1))
2092 break;
2093 if (time_after(jiffies, timeout)) {
2094 dev1 = 0;
2095 break;
2096 }
2097 msleep(50); /* give drive a breather */
2098 }
2099 if (dev1)
2100 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2101
2102 /* is all this really necessary? */
2103 ap->ops->dev_select(ap, 0);
2104 if (dev1)
2105 ap->ops->dev_select(ap, 1);
2106 if (dev0)
2107 ap->ops->dev_select(ap, 0);
2108}
2109
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110static unsigned int ata_bus_softreset(struct ata_port *ap,
2111 unsigned int devmask)
2112{
2113 struct ata_ioports *ioaddr = &ap->ioaddr;
2114
2115 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
2116
2117 /* software reset. causes dev0 to be selected */
2118 if (ap->flags & ATA_FLAG_MMIO) {
2119 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2120 udelay(20); /* FIXME: flush */
2121 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
2122 udelay(20); /* FIXME: flush */
2123 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2124 } else {
2125 outb(ap->ctl, ioaddr->ctl_addr);
2126 udelay(10);
2127 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
2128 udelay(10);
2129 outb(ap->ctl, ioaddr->ctl_addr);
2130 }
2131
2132 /* spec mandates ">= 2ms" before checking status.
2133 * We wait 150ms, because that was the magic delay used for
2134 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
2135 * between when the ATA command register is written, and then
2136 * status is checked. Because waiting for "a while" before
2137 * checking status is fine, post SRST, we perform this magic
2138 * delay here as well.
Alan Cox09c7ad72006-03-22 15:52:40 +00002139 *
2140 * Old drivers/ide uses the 2mS rule and then waits for ready
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 */
2142 msleep(150);
2143
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002144 /* Before we perform post reset processing we want to see if
Tejun Heo298a41c2006-03-25 02:58:13 +09002145 * the bus shows 0xFF because the odd clown forgets the D7
2146 * pulldown resistor.
2147 */
Alan Cox09c7ad72006-03-22 15:52:40 +00002148 if (ata_check_status(ap) == 0xFF)
Tejun Heo298a41c2006-03-25 02:58:13 +09002149 return AC_ERR_OTHER;
Alan Cox09c7ad72006-03-22 15:52:40 +00002150
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 ata_bus_post_reset(ap, devmask);
2152
2153 return 0;
2154}
2155
2156/**
2157 * ata_bus_reset - reset host port and associated ATA channel
2158 * @ap: port to reset
2159 *
2160 * This is typically the first time we actually start issuing
2161 * commands to the ATA channel. We wait for BSY to clear, then
2162 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
2163 * result. Determine what devices, if any, are on the channel
2164 * by looking at the device 0/1 error register. Look at the signature
2165 * stored in each device's taskfile registers, to determine if
2166 * the device is ATA or ATAPI.
2167 *
2168 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002169 * PCI/etc. bus probe sem.
2170 * Obtains host_set lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 *
2172 * SIDE EFFECTS:
2173 * Sets ATA_FLAG_PORT_DISABLED if bus reset fails.
2174 */
2175
2176void ata_bus_reset(struct ata_port *ap)
2177{
2178 struct ata_ioports *ioaddr = &ap->ioaddr;
2179 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2180 u8 err;
Tejun Heoaec5c3c2006-03-25 01:33:34 +09002181 unsigned int dev0, dev1 = 0, devmask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182
2183 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
2184
2185 /* determine if device 0/1 are present */
2186 if (ap->flags & ATA_FLAG_SATA_RESET)
2187 dev0 = 1;
2188 else {
2189 dev0 = ata_devchk(ap, 0);
2190 if (slave_possible)
2191 dev1 = ata_devchk(ap, 1);
2192 }
2193
2194 if (dev0)
2195 devmask |= (1 << 0);
2196 if (dev1)
2197 devmask |= (1 << 1);
2198
2199 /* select device 0 again */
2200 ap->ops->dev_select(ap, 0);
2201
2202 /* issue bus reset */
2203 if (ap->flags & ATA_FLAG_SRST)
Tejun Heoaec5c3c2006-03-25 01:33:34 +09002204 if (ata_bus_softreset(ap, devmask))
2205 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206
2207 /*
2208 * determine by signature whether we have ATA or ATAPI devices
2209 */
Tejun Heob4dc7622006-01-24 17:05:22 +09002210 ap->device[0].class = ata_dev_try_classify(ap, 0, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 if ((slave_possible) && (err != 0x81))
Tejun Heob4dc7622006-01-24 17:05:22 +09002212 ap->device[1].class = ata_dev_try_classify(ap, 1, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213
2214 /* re-enable interrupts */
2215 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2216 ata_irq_on(ap);
2217
2218 /* is double-select really necessary? */
2219 if (ap->device[1].class != ATA_DEV_NONE)
2220 ap->ops->dev_select(ap, 1);
2221 if (ap->device[0].class != ATA_DEV_NONE)
2222 ap->ops->dev_select(ap, 0);
2223
2224 /* if no devices were detected, disable this port */
2225 if ((ap->device[0].class == ATA_DEV_NONE) &&
2226 (ap->device[1].class == ATA_DEV_NONE))
2227 goto err_out;
2228
2229 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
2230 /* set up device control for ATA_FLAG_SATA_RESET */
2231 if (ap->flags & ATA_FLAG_MMIO)
2232 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2233 else
2234 outb(ap->ctl, ioaddr->ctl_addr);
2235 }
2236
2237 DPRINTK("EXIT\n");
2238 return;
2239
2240err_out:
2241 printk(KERN_ERR "ata%u: disabling port\n", ap->id);
2242 ap->ops->port_disable(ap);
2243
2244 DPRINTK("EXIT\n");
2245}
2246
Tejun Heo7a7921e2006-02-02 18:20:00 +09002247static int sata_phy_resume(struct ata_port *ap)
2248{
2249 unsigned long timeout = jiffies + (HZ * 5);
Tejun Heo852ee162006-04-01 01:38:18 +09002250 u32 scontrol, sstatus;
Tejun Heo7a7921e2006-02-02 18:20:00 +09002251
Tejun Heo852ee162006-04-01 01:38:18 +09002252 scontrol = scr_read(ap, SCR_CONTROL);
2253 scontrol = (scontrol & 0x0f0) | 0x300;
2254 scr_write_flush(ap, SCR_CONTROL, scontrol);
Tejun Heo7a7921e2006-02-02 18:20:00 +09002255
2256 /* Wait for phy to become ready, if necessary. */
2257 do {
2258 msleep(200);
2259 sstatus = scr_read(ap, SCR_STATUS);
2260 if ((sstatus & 0xf) != 1)
2261 return 0;
2262 } while (time_before(jiffies, timeout));
2263
2264 return -1;
2265}
2266
Tejun Heoc2bd5802006-01-24 17:05:22 +09002267/**
Tejun Heo8a19ac82006-02-02 18:20:00 +09002268 * ata_std_probeinit - initialize probing
2269 * @ap: port to be probed
2270 *
2271 * @ap is about to be probed. Initialize it. This function is
2272 * to be used as standard callback for ata_drive_probe_reset().
Tejun Heo3a397462006-02-10 23:58:48 +09002273 *
2274 * NOTE!!! Do not use this function as probeinit if a low level
2275 * driver implements only hardreset. Just pass NULL as probeinit
2276 * in that case. Using this function is probably okay but doing
2277 * so makes reset sequence different from the original
2278 * ->phy_reset implementation and Jeff nervous. :-P
Tejun Heo8a19ac82006-02-02 18:20:00 +09002279 */
Alan Cox17efc5f2006-03-27 19:01:32 +01002280void ata_std_probeinit(struct ata_port *ap)
Tejun Heo8a19ac82006-02-02 18:20:00 +09002281{
Alan Cox17efc5f2006-03-27 19:01:32 +01002282 if ((ap->flags & ATA_FLAG_SATA) && ap->ops->scr_read) {
Tejun Heo1c3fae42006-04-02 20:53:28 +09002283 u32 spd;
2284
Tejun Heo8a19ac82006-02-02 18:20:00 +09002285 sata_phy_resume(ap);
Tejun Heo1c3fae42006-04-02 20:53:28 +09002286
2287 spd = (scr_read(ap, SCR_CONTROL) & 0xf0) >> 4;
2288 if (spd)
2289 ap->sata_spd_limit &= (1 << spd) - 1;
2290
Tejun Heo3a397462006-02-10 23:58:48 +09002291 if (sata_dev_present(ap))
2292 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2293 }
Tejun Heo8a19ac82006-02-02 18:20:00 +09002294}
2295
2296/**
Tejun Heoc2bd5802006-01-24 17:05:22 +09002297 * ata_std_softreset - reset host port via ATA SRST
2298 * @ap: port to reset
2299 * @verbose: fail verbosely
2300 * @classes: resulting classes of attached devices
2301 *
2302 * Reset host port using ATA SRST. This function is to be used
2303 * as standard callback for ata_drive_*_reset() functions.
2304 *
2305 * LOCKING:
2306 * Kernel thread context (may sleep)
2307 *
2308 * RETURNS:
2309 * 0 on success, -errno otherwise.
2310 */
2311int ata_std_softreset(struct ata_port *ap, int verbose, unsigned int *classes)
2312{
2313 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2314 unsigned int devmask = 0, err_mask;
2315 u8 err;
2316
2317 DPRINTK("ENTER\n");
2318
Tejun Heo3a397462006-02-10 23:58:48 +09002319 if (ap->ops->scr_read && !sata_dev_present(ap)) {
2320 classes[0] = ATA_DEV_NONE;
2321 goto out;
2322 }
2323
Tejun Heoc2bd5802006-01-24 17:05:22 +09002324 /* determine if device 0/1 are present */
2325 if (ata_devchk(ap, 0))
2326 devmask |= (1 << 0);
2327 if (slave_possible && ata_devchk(ap, 1))
2328 devmask |= (1 << 1);
2329
Tejun Heoc2bd5802006-01-24 17:05:22 +09002330 /* select device 0 again */
2331 ap->ops->dev_select(ap, 0);
2332
2333 /* issue bus reset */
2334 DPRINTK("about to softreset, devmask=%x\n", devmask);
2335 err_mask = ata_bus_softreset(ap, devmask);
2336 if (err_mask) {
2337 if (verbose)
2338 printk(KERN_ERR "ata%u: SRST failed (err_mask=0x%x)\n",
2339 ap->id, err_mask);
2340 else
2341 DPRINTK("EXIT, softreset failed (err_mask=0x%x)\n",
2342 err_mask);
2343 return -EIO;
2344 }
2345
2346 /* determine by signature whether we have ATA or ATAPI devices */
2347 classes[0] = ata_dev_try_classify(ap, 0, &err);
2348 if (slave_possible && err != 0x81)
2349 classes[1] = ata_dev_try_classify(ap, 1, &err);
2350
Tejun Heo3a397462006-02-10 23:58:48 +09002351 out:
Tejun Heoc2bd5802006-01-24 17:05:22 +09002352 DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]);
2353 return 0;
2354}
2355
2356/**
2357 * sata_std_hardreset - reset host port via SATA phy reset
2358 * @ap: port to reset
2359 * @verbose: fail verbosely
2360 * @class: resulting class of attached device
2361 *
2362 * SATA phy-reset host port using DET bits of SControl register.
2363 * This function is to be used as standard callback for
2364 * ata_drive_*_reset().
2365 *
2366 * LOCKING:
2367 * Kernel thread context (may sleep)
2368 *
2369 * RETURNS:
2370 * 0 on success, -errno otherwise.
2371 */
2372int sata_std_hardreset(struct ata_port *ap, int verbose, unsigned int *class)
2373{
Tejun Heo852ee162006-04-01 01:38:18 +09002374 u32 scontrol;
2375
Tejun Heoc2bd5802006-01-24 17:05:22 +09002376 DPRINTK("ENTER\n");
2377
Tejun Heo1c3fae42006-04-02 20:53:28 +09002378 if (ata_set_sata_spd_needed(ap)) {
2379 /* SATA spec says nothing about how to reconfigure
2380 * spd. To be on the safe side, turn off phy during
2381 * reconfiguration. This works for at least ICH7 AHCI
2382 * and Sil3124.
2383 */
2384 scontrol = scr_read(ap, SCR_CONTROL);
2385 scontrol = (scontrol & 0x0f0) | 0x302;
2386 scr_write_flush(ap, SCR_CONTROL, scontrol);
2387
2388 ata_set_sata_spd(ap);
2389 }
2390
2391 /* issue phy wake/reset */
Tejun Heo852ee162006-04-01 01:38:18 +09002392 scontrol = scr_read(ap, SCR_CONTROL);
2393 scontrol = (scontrol & 0x0f0) | 0x301;
2394 scr_write_flush(ap, SCR_CONTROL, scontrol);
Tejun Heoc2bd5802006-01-24 17:05:22 +09002395
Tejun Heo1c3fae42006-04-02 20:53:28 +09002396 /* Couldn't find anything in SATA I/II specs, but AHCI-1.1
Tejun Heoc2bd5802006-01-24 17:05:22 +09002397 * 10.4.2 says at least 1 ms.
2398 */
2399 msleep(1);
2400
Tejun Heo1c3fae42006-04-02 20:53:28 +09002401 /* bring phy back */
Tejun Heo7a7921e2006-02-02 18:20:00 +09002402 sata_phy_resume(ap);
Tejun Heoc2bd5802006-01-24 17:05:22 +09002403
Tejun Heoc2bd5802006-01-24 17:05:22 +09002404 /* TODO: phy layer with polling, timeouts, etc. */
2405 if (!sata_dev_present(ap)) {
2406 *class = ATA_DEV_NONE;
2407 DPRINTK("EXIT, link offline\n");
2408 return 0;
2409 }
2410
2411 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
2412 if (verbose)
2413 printk(KERN_ERR "ata%u: COMRESET failed "
2414 "(device not ready)\n", ap->id);
2415 else
2416 DPRINTK("EXIT, device not ready\n");
2417 return -EIO;
2418 }
2419
Tejun Heo3a397462006-02-10 23:58:48 +09002420 ap->ops->dev_select(ap, 0); /* probably unnecessary */
2421
Tejun Heoc2bd5802006-01-24 17:05:22 +09002422 *class = ata_dev_try_classify(ap, 0, NULL);
2423
2424 DPRINTK("EXIT, class=%u\n", *class);
2425 return 0;
2426}
2427
2428/**
2429 * ata_std_postreset - standard postreset callback
2430 * @ap: the target ata_port
2431 * @classes: classes of attached devices
2432 *
2433 * This function is invoked after a successful reset. Note that
2434 * the device might have been reset more than once using
2435 * different reset methods before postreset is invoked.
Tejun Heoc2bd5802006-01-24 17:05:22 +09002436 *
2437 * This function is to be used as standard callback for
2438 * ata_drive_*_reset().
2439 *
2440 * LOCKING:
2441 * Kernel thread context (may sleep)
2442 */
2443void ata_std_postreset(struct ata_port *ap, unsigned int *classes)
2444{
2445 DPRINTK("ENTER\n");
2446
Tejun Heo56497bd2006-02-15 15:01:42 +09002447 /* set cable type if it isn't already set */
Tejun Heoc2bd5802006-01-24 17:05:22 +09002448 if (ap->cbl == ATA_CBL_NONE && ap->flags & ATA_FLAG_SATA)
2449 ap->cbl = ATA_CBL_SATA;
2450
2451 /* print link status */
2452 if (ap->cbl == ATA_CBL_SATA)
2453 sata_print_link_status(ap);
2454
Tejun Heo3a397462006-02-10 23:58:48 +09002455 /* re-enable interrupts */
2456 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2457 ata_irq_on(ap);
Tejun Heoc2bd5802006-01-24 17:05:22 +09002458
2459 /* is double-select really necessary? */
2460 if (classes[0] != ATA_DEV_NONE)
2461 ap->ops->dev_select(ap, 1);
2462 if (classes[1] != ATA_DEV_NONE)
2463 ap->ops->dev_select(ap, 0);
2464
Tejun Heo3a397462006-02-10 23:58:48 +09002465 /* bail out if no device is present */
2466 if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
2467 DPRINTK("EXIT, no device\n");
2468 return;
2469 }
2470
2471 /* set up device control */
2472 if (ap->ioaddr.ctl_addr) {
2473 if (ap->flags & ATA_FLAG_MMIO)
2474 writeb(ap->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
2475 else
2476 outb(ap->ctl, ap->ioaddr.ctl_addr);
2477 }
Tejun Heoc2bd5802006-01-24 17:05:22 +09002478
2479 DPRINTK("EXIT\n");
2480}
2481
2482/**
2483 * ata_std_probe_reset - standard probe reset method
2484 * @ap: prot to perform probe-reset
2485 * @classes: resulting classes of attached devices
2486 *
2487 * The stock off-the-shelf ->probe_reset method.
2488 *
2489 * LOCKING:
2490 * Kernel thread context (may sleep)
2491 *
2492 * RETURNS:
2493 * 0 on success, -errno otherwise.
2494 */
2495int ata_std_probe_reset(struct ata_port *ap, unsigned int *classes)
2496{
2497 ata_reset_fn_t hardreset;
2498
2499 hardreset = NULL;
Tejun Heob911fc32006-02-02 18:20:00 +09002500 if (ap->flags & ATA_FLAG_SATA && ap->ops->scr_read)
Tejun Heoc2bd5802006-01-24 17:05:22 +09002501 hardreset = sata_std_hardreset;
2502
Tejun Heo8a19ac82006-02-02 18:20:00 +09002503 return ata_drive_probe_reset(ap, ata_std_probeinit,
Tejun Heo7944ea92006-02-02 18:20:00 +09002504 ata_std_softreset, hardreset,
Tejun Heoc2bd5802006-01-24 17:05:22 +09002505 ata_std_postreset, classes);
2506}
2507
Tejun Heo9974e7c2006-04-01 01:38:17 +09002508static int ata_do_reset(struct ata_port *ap,
2509 ata_reset_fn_t reset, ata_postreset_fn_t postreset,
2510 int verbose, unsigned int *classes)
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002511{
2512 int i, rc;
2513
2514 for (i = 0; i < ATA_MAX_DEVICES; i++)
2515 classes[i] = ATA_DEV_UNKNOWN;
2516
Tejun Heo9974e7c2006-04-01 01:38:17 +09002517 rc = reset(ap, verbose, classes);
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002518 if (rc)
2519 return rc;
2520
2521 /* If any class isn't ATA_DEV_UNKNOWN, consider classification
2522 * is complete and convert all ATA_DEV_UNKNOWN to
2523 * ATA_DEV_NONE.
2524 */
2525 for (i = 0; i < ATA_MAX_DEVICES; i++)
2526 if (classes[i] != ATA_DEV_UNKNOWN)
2527 break;
2528
2529 if (i < ATA_MAX_DEVICES)
2530 for (i = 0; i < ATA_MAX_DEVICES; i++)
2531 if (classes[i] == ATA_DEV_UNKNOWN)
2532 classes[i] = ATA_DEV_NONE;
2533
2534 if (postreset)
2535 postreset(ap, classes);
2536
Tejun Heo9974e7c2006-04-01 01:38:17 +09002537 return 0;
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002538}
2539
2540/**
2541 * ata_drive_probe_reset - Perform probe reset with given methods
2542 * @ap: port to reset
Tejun Heo7944ea92006-02-02 18:20:00 +09002543 * @probeinit: probeinit method (can be NULL)
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002544 * @softreset: softreset method (can be NULL)
2545 * @hardreset: hardreset method (can be NULL)
2546 * @postreset: postreset method (can be NULL)
2547 * @classes: resulting classes of attached devices
2548 *
2549 * Reset the specified port and classify attached devices using
2550 * given methods. This function prefers softreset but tries all
2551 * possible reset sequences to reset and classify devices. This
2552 * function is intended to be used for constructing ->probe_reset
2553 * callback by low level drivers.
2554 *
2555 * Reset methods should follow the following rules.
2556 *
2557 * - Return 0 on sucess, -errno on failure.
2558 * - If classification is supported, fill classes[] with
2559 * recognized class codes.
2560 * - If classification is not supported, leave classes[] alone.
2561 * - If verbose is non-zero, print error message on failure;
2562 * otherwise, shut up.
2563 *
2564 * LOCKING:
2565 * Kernel thread context (may sleep)
2566 *
2567 * RETURNS:
2568 * 0 on success, -EINVAL if no reset method is avaliable, -ENODEV
2569 * if classification fails, and any error code from reset
2570 * methods.
2571 */
Tejun Heo7944ea92006-02-02 18:20:00 +09002572int ata_drive_probe_reset(struct ata_port *ap, ata_probeinit_fn_t probeinit,
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002573 ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
2574 ata_postreset_fn_t postreset, unsigned int *classes)
2575{
2576 int rc = -EINVAL;
2577
Tejun Heo7944ea92006-02-02 18:20:00 +09002578 if (probeinit)
2579 probeinit(ap);
2580
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002581 if (softreset) {
Tejun Heo9974e7c2006-04-01 01:38:17 +09002582 rc = ata_do_reset(ap, softreset, postreset, 0, classes);
2583 if (rc == 0 && classes[0] != ATA_DEV_UNKNOWN)
2584 goto done;
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002585 }
2586
2587 if (!hardreset)
Tejun Heo9974e7c2006-04-01 01:38:17 +09002588 goto done;
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002589
Tejun Heo9974e7c2006-04-01 01:38:17 +09002590 rc = ata_do_reset(ap, hardreset, postreset, 0, classes);
2591 if (rc || classes[0] != ATA_DEV_UNKNOWN)
2592 goto done;
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002593
2594 if (softreset)
Tejun Heo9974e7c2006-04-01 01:38:17 +09002595 rc = ata_do_reset(ap, softreset, postreset, 0, classes);
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002596
Tejun Heo9974e7c2006-04-01 01:38:17 +09002597 done:
2598 if (rc == 0 && classes[0] == ATA_DEV_UNKNOWN)
2599 rc = -ENODEV;
Tejun Heoa62c0fc2006-01-24 17:05:22 +09002600 return rc;
2601}
2602
Tejun Heo623a3122006-03-05 17:55:58 +09002603/**
2604 * ata_dev_same_device - Determine whether new ID matches configured device
2605 * @ap: port on which the device to compare against resides
2606 * @dev: device to compare against
2607 * @new_class: class of the new device
2608 * @new_id: IDENTIFY page of the new device
2609 *
2610 * Compare @new_class and @new_id against @dev and determine
2611 * whether @dev is the device indicated by @new_class and
2612 * @new_id.
2613 *
2614 * LOCKING:
2615 * None.
2616 *
2617 * RETURNS:
2618 * 1 if @dev matches @new_class and @new_id, 0 otherwise.
2619 */
2620static int ata_dev_same_device(struct ata_port *ap, struct ata_device *dev,
2621 unsigned int new_class, const u16 *new_id)
2622{
2623 const u16 *old_id = dev->id;
2624 unsigned char model[2][41], serial[2][21];
2625 u64 new_n_sectors;
2626
2627 if (dev->class != new_class) {
2628 printk(KERN_INFO
2629 "ata%u: dev %u class mismatch %d != %d\n",
2630 ap->id, dev->devno, dev->class, new_class);
2631 return 0;
2632 }
2633
2634 ata_id_c_string(old_id, model[0], ATA_ID_PROD_OFS, sizeof(model[0]));
2635 ata_id_c_string(new_id, model[1], ATA_ID_PROD_OFS, sizeof(model[1]));
2636 ata_id_c_string(old_id, serial[0], ATA_ID_SERNO_OFS, sizeof(serial[0]));
2637 ata_id_c_string(new_id, serial[1], ATA_ID_SERNO_OFS, sizeof(serial[1]));
2638 new_n_sectors = ata_id_n_sectors(new_id);
2639
2640 if (strcmp(model[0], model[1])) {
2641 printk(KERN_INFO
2642 "ata%u: dev %u model number mismatch '%s' != '%s'\n",
2643 ap->id, dev->devno, model[0], model[1]);
2644 return 0;
2645 }
2646
2647 if (strcmp(serial[0], serial[1])) {
2648 printk(KERN_INFO
2649 "ata%u: dev %u serial number mismatch '%s' != '%s'\n",
2650 ap->id, dev->devno, serial[0], serial[1]);
2651 return 0;
2652 }
2653
2654 if (dev->class == ATA_DEV_ATA && dev->n_sectors != new_n_sectors) {
2655 printk(KERN_INFO
2656 "ata%u: dev %u n_sectors mismatch %llu != %llu\n",
2657 ap->id, dev->devno, (unsigned long long)dev->n_sectors,
2658 (unsigned long long)new_n_sectors);
2659 return 0;
2660 }
2661
2662 return 1;
2663}
2664
2665/**
2666 * ata_dev_revalidate - Revalidate ATA device
2667 * @ap: port on which the device to revalidate resides
2668 * @dev: device to revalidate
2669 * @post_reset: is this revalidation after reset?
2670 *
2671 * Re-read IDENTIFY page and make sure @dev is still attached to
2672 * the port.
2673 *
2674 * LOCKING:
2675 * Kernel thread context (may sleep)
2676 *
2677 * RETURNS:
2678 * 0 on success, negative errno otherwise
2679 */
2680int ata_dev_revalidate(struct ata_port *ap, struct ata_device *dev,
2681 int post_reset)
2682{
2683 unsigned int class;
2684 u16 *id;
2685 int rc;
2686
Tejun Heoe1211e32006-04-01 01:38:18 +09002687 if (!ata_dev_enabled(dev))
Tejun Heo623a3122006-03-05 17:55:58 +09002688 return -ENODEV;
2689
2690 class = dev->class;
2691 id = NULL;
2692
2693 /* allocate & read ID data */
2694 rc = ata_dev_read_id(ap, dev, &class, post_reset, &id);
2695 if (rc)
2696 goto fail;
2697
2698 /* is the device still there? */
2699 if (!ata_dev_same_device(ap, dev, class, id)) {
2700 rc = -ENODEV;
2701 goto fail;
2702 }
2703
2704 kfree(dev->id);
2705 dev->id = id;
2706
2707 /* configure device according to the new ID */
2708 return ata_dev_configure(ap, dev, 0);
2709
2710 fail:
2711 printk(KERN_ERR "ata%u: dev %u revalidation failed (errno=%d)\n",
2712 ap->id, dev->devno, rc);
2713 kfree(id);
2714 return rc;
2715}
2716
Arjan van de Ven98ac62d2005-11-28 10:06:23 +01002717static const char * const ata_dma_blacklist [] = {
Alan Coxf4b15fe2006-03-22 15:54:04 +00002718 "WDC AC11000H", NULL,
2719 "WDC AC22100H", NULL,
2720 "WDC AC32500H", NULL,
2721 "WDC AC33100H", NULL,
2722 "WDC AC31600H", NULL,
2723 "WDC AC32100H", "24.09P07",
2724 "WDC AC23200L", "21.10N21",
2725 "Compaq CRD-8241B", NULL,
2726 "CRD-8400B", NULL,
2727 "CRD-8480B", NULL,
2728 "CRD-8482B", NULL,
2729 "CRD-84", NULL,
2730 "SanDisk SDP3B", NULL,
2731 "SanDisk SDP3B-64", NULL,
2732 "SANYO CD-ROM CRD", NULL,
2733 "HITACHI CDR-8", NULL,
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002734 "HITACHI CDR-8335", NULL,
Alan Coxf4b15fe2006-03-22 15:54:04 +00002735 "HITACHI CDR-8435", NULL,
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002736 "Toshiba CD-ROM XM-6202B", NULL,
2737 "TOSHIBA CD-ROM XM-1702BC", NULL,
2738 "CD-532E-A", NULL,
2739 "E-IDE CD-ROM CR-840", NULL,
2740 "CD-ROM Drive/F5A", NULL,
2741 "WPI CDD-820", NULL,
Alan Coxf4b15fe2006-03-22 15:54:04 +00002742 "SAMSUNG CD-ROM SC-148C", NULL,
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002743 "SAMSUNG CD-ROM SC", NULL,
Alan Coxf4b15fe2006-03-22 15:54:04 +00002744 "SanDisk SDP3B-64", NULL,
2745 "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,
2746 "_NEC DV5800A", NULL,
2747 "SAMSUNG CD-ROM SN-124", "N001"
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748};
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002749
Alan Coxf4b15fe2006-03-22 15:54:04 +00002750static int ata_strim(char *s, size_t len)
2751{
2752 len = strnlen(s, len);
2753
2754 /* ATAPI specifies that empty space is blank-filled; remove blanks */
2755 while ((len > 0) && (s[len - 1] == ' ')) {
2756 len--;
2757 s[len] = 0;
2758 }
2759 return len;
2760}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761
Jeff Garzik057ace52005-10-22 14:27:05 -04002762static int ata_dma_blacklisted(const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763{
Alan Coxf4b15fe2006-03-22 15:54:04 +00002764 unsigned char model_num[40];
2765 unsigned char model_rev[16];
2766 unsigned int nlen, rlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 int i;
2768
Alan Coxf4b15fe2006-03-22 15:54:04 +00002769 ata_id_string(dev->id, model_num, ATA_ID_PROD_OFS,
2770 sizeof(model_num));
2771 ata_id_string(dev->id, model_rev, ATA_ID_FW_REV_OFS,
2772 sizeof(model_rev));
2773 nlen = ata_strim(model_num, sizeof(model_num));
2774 rlen = ata_strim(model_rev, sizeof(model_rev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775
Alan Coxf4b15fe2006-03-22 15:54:04 +00002776 for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i += 2) {
2777 if (!strncmp(ata_dma_blacklist[i], model_num, nlen)) {
2778 if (ata_dma_blacklist[i+1] == NULL)
2779 return 1;
2780 if (!strncmp(ata_dma_blacklist[i], model_rev, rlen))
2781 return 1;
2782 }
2783 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002784 return 0;
2785}
2786
Tejun Heoa6d5a512006-03-06 04:31:57 +09002787/**
2788 * ata_dev_xfermask - Compute supported xfermask of the given device
2789 * @ap: Port on which the device to compute xfermask for resides
2790 * @dev: Device to compute xfermask for
2791 *
Tejun Heoacf356b2006-03-24 14:07:50 +09002792 * Compute supported xfermask of @dev and store it in
2793 * dev->*_mask. This function is responsible for applying all
2794 * known limits including host controller limits, device
2795 * blacklist, etc...
Tejun Heoa6d5a512006-03-06 04:31:57 +09002796 *
Tejun Heo600511e2006-03-25 11:14:07 +09002797 * FIXME: The current implementation limits all transfer modes to
2798 * the fastest of the lowested device on the port. This is not
Tejun Heo05c8e0a2006-03-25 14:28:57 +09002799 * required on most controllers.
Tejun Heo600511e2006-03-25 11:14:07 +09002800 *
Tejun Heoa6d5a512006-03-06 04:31:57 +09002801 * LOCKING:
2802 * None.
Tejun Heoa6d5a512006-03-06 04:31:57 +09002803 */
Tejun Heoacf356b2006-03-24 14:07:50 +09002804static void ata_dev_xfermask(struct ata_port *ap, struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805{
Alan Cox5444a6f2006-03-27 18:58:20 +01002806 struct ata_host_set *hs = ap->host_set;
Tejun Heoa6d5a512006-03-06 04:31:57 +09002807 unsigned long xfer_mask;
2808 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809
Tejun Heoa6d5a512006-03-06 04:31:57 +09002810 xfer_mask = ata_pack_xfermask(ap->pio_mask, ap->mwdma_mask,
2811 ap->udma_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812
Alan Cox5444a6f2006-03-27 18:58:20 +01002813 /* FIXME: Use port-wide xfermask for now */
Tejun Heoa6d5a512006-03-06 04:31:57 +09002814 for (i = 0; i < ATA_MAX_DEVICES; i++) {
2815 struct ata_device *d = &ap->device[i];
Tejun Heoe1211e32006-04-01 01:38:18 +09002816 if (!ata_dev_enabled(d))
Tejun Heoa6d5a512006-03-06 04:31:57 +09002817 continue;
Tejun Heoacf356b2006-03-24 14:07:50 +09002818 xfer_mask &= ata_pack_xfermask(d->pio_mask, d->mwdma_mask,
2819 d->udma_mask);
Tejun Heoa6d5a512006-03-06 04:31:57 +09002820 xfer_mask &= ata_id_xfermask(d->id);
2821 if (ata_dma_blacklisted(d))
2822 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
Alan Cox5444a6f2006-03-27 18:58:20 +01002823 /* Apply cable rule here. Don't apply it early because when
2824 we handle hot plug the cable type can itself change */
2825 if (ap->cbl == ATA_CBL_PATA40)
2826 xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 }
2828
Tejun Heoa6d5a512006-03-06 04:31:57 +09002829 if (ata_dma_blacklisted(dev))
2830 printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, "
2831 "disabling DMA\n", ap->id, dev->devno);
2832
Alan Cox5444a6f2006-03-27 18:58:20 +01002833 if (hs->flags & ATA_HOST_SIMPLEX) {
2834 if (hs->simplex_claimed)
2835 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
2836 }
2837 if (ap->ops->mode_filter)
2838 xfer_mask = ap->ops->mode_filter(ap, dev, xfer_mask);
2839
Tejun Heoacf356b2006-03-24 14:07:50 +09002840 ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask,
2841 &dev->udma_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842}
2843
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002845 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
2846 * @ap: Port associated with device @dev
2847 * @dev: Device to which command will be sent
2848 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002849 * Issue SET FEATURES - XFER MODE command to device @dev
2850 * on port @ap.
2851 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002852 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002853 * PCI/etc. bus probe sem.
Tejun Heo83206a22006-03-24 15:25:31 +09002854 *
2855 * RETURNS:
2856 * 0 on success, AC_ERR_* mask otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 */
2858
Tejun Heo83206a22006-03-24 15:25:31 +09002859static unsigned int ata_dev_set_xfermode(struct ata_port *ap,
2860 struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861{
Tejun Heoa0123702005-12-13 14:49:31 +09002862 struct ata_taskfile tf;
Tejun Heo83206a22006-03-24 15:25:31 +09002863 unsigned int err_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864
2865 /* set up set-features taskfile */
2866 DPRINTK("set features - xfer mode\n");
2867
Tejun Heoa0123702005-12-13 14:49:31 +09002868 ata_tf_init(ap, &tf, dev->devno);
2869 tf.command = ATA_CMD_SET_FEATURES;
2870 tf.feature = SETFEATURES_XFER;
2871 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2872 tf.protocol = ATA_PROT_NODATA;
2873 tf.nsect = dev->xfer_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874
Tejun Heo83206a22006-03-24 15:25:31 +09002875 err_mask = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876
Tejun Heo83206a22006-03-24 15:25:31 +09002877 DPRINTK("EXIT, err_mask=%x\n", err_mask);
2878 return err_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879}
2880
2881/**
Albert Lee8bf62ec2005-05-12 15:29:42 -04002882 * ata_dev_init_params - Issue INIT DEV PARAMS command
2883 * @ap: Port associated with device @dev
2884 * @dev: Device to which command will be sent
2885 *
2886 * LOCKING:
Tejun Heo6aff8f12006-02-15 18:24:09 +09002887 * Kernel thread context (may sleep)
2888 *
2889 * RETURNS:
2890 * 0 on success, AC_ERR_* mask otherwise.
Albert Lee8bf62ec2005-05-12 15:29:42 -04002891 */
2892
Tejun Heo6aff8f12006-02-15 18:24:09 +09002893static unsigned int ata_dev_init_params(struct ata_port *ap,
Albert Lee00b6f5e2006-03-27 16:39:18 +08002894 struct ata_device *dev,
2895 u16 heads,
2896 u16 sectors)
Albert Lee8bf62ec2005-05-12 15:29:42 -04002897{
Tejun Heoa0123702005-12-13 14:49:31 +09002898 struct ata_taskfile tf;
Tejun Heo6aff8f12006-02-15 18:24:09 +09002899 unsigned int err_mask;
Albert Lee8bf62ec2005-05-12 15:29:42 -04002900
2901 /* Number of sectors per track 1-255. Number of heads 1-16 */
2902 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
Albert Lee00b6f5e2006-03-27 16:39:18 +08002903 return AC_ERR_INVALID;
Albert Lee8bf62ec2005-05-12 15:29:42 -04002904
2905 /* set up init dev params taskfile */
2906 DPRINTK("init dev params \n");
2907
Tejun Heoa0123702005-12-13 14:49:31 +09002908 ata_tf_init(ap, &tf, dev->devno);
2909 tf.command = ATA_CMD_INIT_DEV_PARAMS;
2910 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2911 tf.protocol = ATA_PROT_NODATA;
2912 tf.nsect = sectors;
2913 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
Albert Lee8bf62ec2005-05-12 15:29:42 -04002914
Tejun Heo6aff8f12006-02-15 18:24:09 +09002915 err_mask = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
Albert Lee8bf62ec2005-05-12 15:29:42 -04002916
Tejun Heo6aff8f12006-02-15 18:24:09 +09002917 DPRINTK("EXIT, err_mask=%x\n", err_mask);
2918 return err_mask;
Albert Lee8bf62ec2005-05-12 15:29:42 -04002919}
2920
2921/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002922 * ata_sg_clean - Unmap DMA memory associated with command
2923 * @qc: Command containing DMA memory to be released
2924 *
2925 * Unmap all mapped DMA memory associated with this command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 *
2927 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002928 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929 */
2930
2931static void ata_sg_clean(struct ata_queued_cmd *qc)
2932{
2933 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002934 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002936 void *pad_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937
Tejun Heoa4631472006-02-11 19:11:13 +09002938 WARN_ON(!(qc->flags & ATA_QCFLAG_DMAMAP));
2939 WARN_ON(sg == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940
2941 if (qc->flags & ATA_QCFLAG_SINGLE)
Jeff Garzikf1318832006-02-20 16:55:56 -05002942 WARN_ON(qc->n_elem > 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05002944 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002946 /* if we padded the buffer out to 32-bit bound, and data
2947 * xfer direction is from-device, we must copy from the
2948 * pad buffer back into the supplied buffer
2949 */
2950 if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
2951 pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
2952
2953 if (qc->flags & ATA_QCFLAG_SG) {
Jeff Garzike1410f22005-11-14 14:06:26 -05002954 if (qc->n_elem)
Brian King2f1f6102006-03-23 17:30:15 -06002955 dma_unmap_sg(ap->dev, sg, qc->n_elem, dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002956 /* restore last sg */
2957 sg[qc->orig_n_elem - 1].length += qc->pad_len;
2958 if (pad_buf) {
2959 struct scatterlist *psg = &qc->pad_sgent;
2960 void *addr = kmap_atomic(psg->page, KM_IRQ0);
2961 memcpy(addr + psg->offset, pad_buf, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05002962 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002963 }
2964 } else {
Tejun Heo2e242fa2006-02-20 23:48:38 +09002965 if (qc->n_elem)
Brian King2f1f6102006-03-23 17:30:15 -06002966 dma_unmap_single(ap->dev,
Jeff Garzike1410f22005-11-14 14:06:26 -05002967 sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
2968 dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002969 /* restore sg */
2970 sg->length += qc->pad_len;
2971 if (pad_buf)
2972 memcpy(qc->buf_virt + sg->length - qc->pad_len,
2973 pad_buf, qc->pad_len);
2974 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975
2976 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002977 qc->__sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978}
2979
2980/**
2981 * ata_fill_sg - Fill PCI IDE PRD table
2982 * @qc: Metadata associated with taskfile to be transferred
2983 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002984 * Fill PCI IDE PRD (scatter-gather) table with segments
2985 * associated with the current disk command.
2986 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002987 * LOCKING:
Jeff Garzik780a87f2005-05-30 15:41:05 -04002988 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 *
2990 */
2991static void ata_fill_sg(struct ata_queued_cmd *qc)
2992{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002994 struct scatterlist *sg;
2995 unsigned int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996
Tejun Heoa4631472006-02-11 19:11:13 +09002997 WARN_ON(qc->__sg == NULL);
Jeff Garzikf1318832006-02-20 16:55:56 -05002998 WARN_ON(qc->n_elem == 0 && qc->pad_len == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999
3000 idx = 0;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003001 ata_for_each_sg(sg, qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 u32 addr, offset;
3003 u32 sg_len, len;
3004
3005 /* determine if physical DMA addr spans 64K boundary.
3006 * Note h/w doesn't support 64-bit, so we unconditionally
3007 * truncate dma_addr_t to u32.
3008 */
3009 addr = (u32) sg_dma_address(sg);
3010 sg_len = sg_dma_len(sg);
3011
3012 while (sg_len) {
3013 offset = addr & 0xffff;
3014 len = sg_len;
3015 if ((offset + sg_len) > 0x10000)
3016 len = 0x10000 - offset;
3017
3018 ap->prd[idx].addr = cpu_to_le32(addr);
3019 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
3020 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
3021
3022 idx++;
3023 sg_len -= len;
3024 addr += len;
3025 }
3026 }
3027
3028 if (idx)
3029 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
3030}
3031/**
3032 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
3033 * @qc: Metadata associated with taskfile to check
3034 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04003035 * Allow low-level driver to filter ATA PACKET commands, returning
3036 * a status indicating whether or not it is OK to use DMA for the
3037 * supplied PACKET command.
3038 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003040 * spin_lock_irqsave(host_set lock)
3041 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 * RETURNS: 0 when ATAPI DMA can be used
3043 * nonzero otherwise
3044 */
3045int ata_check_atapi_dma(struct ata_queued_cmd *qc)
3046{
3047 struct ata_port *ap = qc->ap;
3048 int rc = 0; /* Assume ATAPI DMA is OK by default */
3049
3050 if (ap->ops->check_atapi_dma)
3051 rc = ap->ops->check_atapi_dma(qc);
3052
3053 return rc;
3054}
3055/**
3056 * ata_qc_prep - Prepare taskfile for submission
3057 * @qc: Metadata associated with taskfile to be prepared
3058 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04003059 * Prepare ATA taskfile for submission.
3060 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061 * LOCKING:
3062 * spin_lock_irqsave(host_set lock)
3063 */
3064void ata_qc_prep(struct ata_queued_cmd *qc)
3065{
3066 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
3067 return;
3068
3069 ata_fill_sg(qc);
3070}
3071
Brian Kinge46834c2006-03-17 17:04:03 -06003072void ata_noop_qc_prep(struct ata_queued_cmd *qc) { }
3073
Jeff Garzik0cba6322005-05-30 19:49:12 -04003074/**
3075 * ata_sg_init_one - Associate command with memory buffer
3076 * @qc: Command to be associated
3077 * @buf: Memory buffer
3078 * @buflen: Length of memory buffer, in bytes.
3079 *
3080 * Initialize the data-related elements of queued_cmd @qc
3081 * to point to a single memory buffer, @buf of byte length @buflen.
3082 *
3083 * LOCKING:
3084 * spin_lock_irqsave(host_set lock)
3085 */
3086
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
3088{
3089 struct scatterlist *sg;
3090
3091 qc->flags |= ATA_QCFLAG_SINGLE;
3092
3093 memset(&qc->sgent, 0, sizeof(qc->sgent));
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003094 qc->__sg = &qc->sgent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 qc->n_elem = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003096 qc->orig_n_elem = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 qc->buf_virt = buf;
3098
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003099 sg = qc->__sg;
Jeff Garzikf0612bb2005-10-30 01:58:18 -05003100 sg_init_one(sg, buf, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101}
3102
Jeff Garzik0cba6322005-05-30 19:49:12 -04003103/**
3104 * ata_sg_init - Associate command with scatter-gather table.
3105 * @qc: Command to be associated
3106 * @sg: Scatter-gather table.
3107 * @n_elem: Number of elements in s/g table.
3108 *
3109 * Initialize the data-related elements of queued_cmd @qc
3110 * to point to a scatter-gather table @sg, containing @n_elem
3111 * elements.
3112 *
3113 * LOCKING:
3114 * spin_lock_irqsave(host_set lock)
3115 */
3116
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
3118 unsigned int n_elem)
3119{
3120 qc->flags |= ATA_QCFLAG_SG;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003121 qc->__sg = sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 qc->n_elem = n_elem;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003123 qc->orig_n_elem = n_elem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124}
3125
3126/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04003127 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
3128 * @qc: Command with memory buffer to be mapped.
3129 *
3130 * DMA-map the memory buffer associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 *
3132 * LOCKING:
3133 * spin_lock_irqsave(host_set lock)
3134 *
3135 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003136 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 */
3138
3139static int ata_sg_setup_one(struct ata_queued_cmd *qc)
3140{
3141 struct ata_port *ap = qc->ap;
3142 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003143 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144 dma_addr_t dma_address;
Tejun Heo2e242fa2006-02-20 23:48:38 +09003145 int trim_sg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003147 /* we must lengthen transfers to end on a 32-bit boundary */
3148 qc->pad_len = sg->length & 3;
3149 if (qc->pad_len) {
3150 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
3151 struct scatterlist *psg = &qc->pad_sgent;
3152
Tejun Heoa4631472006-02-11 19:11:13 +09003153 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003154
3155 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
3156
3157 if (qc->tf.flags & ATA_TFLAG_WRITE)
3158 memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
3159 qc->pad_len);
3160
3161 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
3162 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
3163 /* trim sg */
3164 sg->length -= qc->pad_len;
Tejun Heo2e242fa2006-02-20 23:48:38 +09003165 if (sg->length == 0)
3166 trim_sg = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003167
3168 DPRINTK("padding done, sg->length=%u pad_len=%u\n",
3169 sg->length, qc->pad_len);
3170 }
3171
Tejun Heo2e242fa2006-02-20 23:48:38 +09003172 if (trim_sg) {
3173 qc->n_elem--;
Jeff Garzike1410f22005-11-14 14:06:26 -05003174 goto skip_map;
3175 }
3176
Brian King2f1f6102006-03-23 17:30:15 -06003177 dma_address = dma_map_single(ap->dev, qc->buf_virt,
Albert Lee32529e02005-05-26 03:49:42 -04003178 sg->length, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05003179 if (dma_mapping_error(dma_address)) {
3180 /* restore sg */
3181 sg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05003183 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184
3185 sg_dma_address(sg) = dma_address;
Albert Lee32529e02005-05-26 03:49:42 -04003186 sg_dma_len(sg) = sg->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187
Tejun Heo2e242fa2006-02-20 23:48:38 +09003188skip_map:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
3190 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3191
3192 return 0;
3193}
3194
3195/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04003196 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
3197 * @qc: Command with scatter-gather table to be mapped.
3198 *
3199 * DMA-map the scatter-gather table associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 *
3201 * LOCKING:
3202 * spin_lock_irqsave(host_set lock)
3203 *
3204 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003205 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 *
3207 */
3208
3209static int ata_sg_setup(struct ata_queued_cmd *qc)
3210{
3211 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003212 struct scatterlist *sg = qc->__sg;
3213 struct scatterlist *lsg = &sg[qc->n_elem - 1];
Jeff Garzike1410f22005-11-14 14:06:26 -05003214 int n_elem, pre_n_elem, dir, trim_sg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215
3216 VPRINTK("ENTER, ata%u\n", ap->id);
Tejun Heoa4631472006-02-11 19:11:13 +09003217 WARN_ON(!(qc->flags & ATA_QCFLAG_SG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003219 /* we must lengthen transfers to end on a 32-bit boundary */
3220 qc->pad_len = lsg->length & 3;
3221 if (qc->pad_len) {
3222 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
3223 struct scatterlist *psg = &qc->pad_sgent;
3224 unsigned int offset;
3225
Tejun Heoa4631472006-02-11 19:11:13 +09003226 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003227
3228 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
3229
3230 /*
3231 * psg->page/offset are used to copy to-be-written
3232 * data in this function or read data in ata_sg_clean.
3233 */
3234 offset = lsg->offset + lsg->length - qc->pad_len;
3235 psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
3236 psg->offset = offset_in_page(offset);
3237
3238 if (qc->tf.flags & ATA_TFLAG_WRITE) {
3239 void *addr = kmap_atomic(psg->page, KM_IRQ0);
3240 memcpy(pad_buf, addr + psg->offset, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05003241 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003242 }
3243
3244 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
3245 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
3246 /* trim last sg */
3247 lsg->length -= qc->pad_len;
Jeff Garzike1410f22005-11-14 14:06:26 -05003248 if (lsg->length == 0)
3249 trim_sg = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003250
3251 DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
3252 qc->n_elem - 1, lsg->length, qc->pad_len);
3253 }
3254
Jeff Garzike1410f22005-11-14 14:06:26 -05003255 pre_n_elem = qc->n_elem;
3256 if (trim_sg && pre_n_elem)
3257 pre_n_elem--;
3258
3259 if (!pre_n_elem) {
3260 n_elem = 0;
3261 goto skip_map;
3262 }
3263
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264 dir = qc->dma_dir;
Brian King2f1f6102006-03-23 17:30:15 -06003265 n_elem = dma_map_sg(ap->dev, sg, pre_n_elem, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05003266 if (n_elem < 1) {
3267 /* restore last sg */
3268 lsg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05003270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271
3272 DPRINTK("%d sg elements mapped\n", n_elem);
3273
Jeff Garzike1410f22005-11-14 14:06:26 -05003274skip_map:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003275 qc->n_elem = n_elem;
3276
3277 return 0;
3278}
3279
3280/**
Tejun Heo40e8c822005-08-22 17:12:45 +09003281 * ata_poll_qc_complete - turn irq back on and finish qc
3282 * @qc: Command to complete
Randy Dunlap8e8b77d2005-11-01 21:29:27 -08003283 * @err_mask: ATA status register content
Tejun Heo40e8c822005-08-22 17:12:45 +09003284 *
3285 * LOCKING:
3286 * None. (grabs host lock)
3287 */
3288
Albert Leea22e2eb2005-12-05 15:38:02 +08003289void ata_poll_qc_complete(struct ata_queued_cmd *qc)
Tejun Heo40e8c822005-08-22 17:12:45 +09003290{
3291 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003292 unsigned long flags;
Tejun Heo40e8c822005-08-22 17:12:45 +09003293
Jeff Garzikb8f61532005-08-25 22:01:20 -04003294 spin_lock_irqsave(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09003295 ap->flags &= ~ATA_FLAG_NOINTR;
3296 ata_irq_on(ap);
Albert Leea22e2eb2005-12-05 15:38:02 +08003297 ata_qc_complete(qc);
Jeff Garzikb8f61532005-08-25 22:01:20 -04003298 spin_unlock_irqrestore(&ap->host_set->lock, flags);
Tejun Heo40e8c822005-08-22 17:12:45 +09003299}
3300
3301/**
Randy Dunlapc893a3a2006-01-28 13:15:32 -05003302 * ata_pio_poll - poll using PIO, depending on current state
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003303 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 *
3305 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003306 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003307 *
3308 * RETURNS:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003309 * timeout value to use
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310 */
3311
3312static unsigned long ata_pio_poll(struct ata_port *ap)
3313{
Albert Leec14b8332005-12-05 15:36:08 +08003314 struct ata_queued_cmd *qc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 u8 status;
Albert Lee14be71f2005-09-27 17:36:35 +08003316 unsigned int poll_state = HSM_ST_UNKNOWN;
3317 unsigned int reg_state = HSM_ST_UNKNOWN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318
Albert Leec14b8332005-12-05 15:36:08 +08003319 qc = ata_qc_from_tag(ap, ap->active_tag);
Tejun Heoa4631472006-02-11 19:11:13 +09003320 WARN_ON(qc == NULL);
Albert Leec14b8332005-12-05 15:36:08 +08003321
Albert Lee14be71f2005-09-27 17:36:35 +08003322 switch (ap->hsm_task_state) {
3323 case HSM_ST:
3324 case HSM_ST_POLL:
3325 poll_state = HSM_ST_POLL;
3326 reg_state = HSM_ST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327 break;
Albert Lee14be71f2005-09-27 17:36:35 +08003328 case HSM_ST_LAST:
3329 case HSM_ST_LAST_POLL:
3330 poll_state = HSM_ST_LAST_POLL;
3331 reg_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003332 break;
3333 default:
3334 BUG();
3335 break;
3336 }
3337
3338 status = ata_chk_status(ap);
3339 if (status & ATA_BUSY) {
3340 if (time_after(jiffies, ap->pio_task_timeout)) {
Tejun Heo11a56d22006-01-23 13:09:36 +09003341 qc->err_mask |= AC_ERR_TIMEOUT;
Albert Lee7c398332005-11-09 13:03:30 +08003342 ap->hsm_task_state = HSM_ST_TMOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 return 0;
3344 }
Albert Lee14be71f2005-09-27 17:36:35 +08003345 ap->hsm_task_state = poll_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346 return ATA_SHORT_PAUSE;
3347 }
3348
Albert Lee14be71f2005-09-27 17:36:35 +08003349 ap->hsm_task_state = reg_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350 return 0;
3351}
3352
3353/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003354 * ata_pio_complete - check if drive is busy or idle
3355 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356 *
3357 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003358 * None. (executing in kernel thread context)
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003359 *
3360 * RETURNS:
3361 * Non-zero if qc completed, zero otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362 */
3363
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003364static int ata_pio_complete (struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365{
3366 struct ata_queued_cmd *qc;
3367 u8 drv_stat;
3368
3369 /*
Alan Cox31433ea2005-08-26 15:56:47 +01003370 * This is purely heuristic. This is a fast path. Sometimes when
3371 * we enter, BSY will be cleared in a chk-status or two. If not,
3372 * the drive is probably seeking or something. Snooze for a couple
3373 * msecs, then chk-status again. If still busy, fall back to
Albert Lee14be71f2005-09-27 17:36:35 +08003374 * HSM_ST_POLL state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 */
Albert Leefe79e682005-12-06 11:34:59 +08003376 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
3377 if (drv_stat & ATA_BUSY) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378 msleep(2);
Albert Leefe79e682005-12-06 11:34:59 +08003379 drv_stat = ata_busy_wait(ap, ATA_BUSY, 10);
3380 if (drv_stat & ATA_BUSY) {
Albert Lee14be71f2005-09-27 17:36:35 +08003381 ap->hsm_task_state = HSM_ST_LAST_POLL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003383 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384 }
3385 }
3386
Albert Leec14b8332005-12-05 15:36:08 +08003387 qc = ata_qc_from_tag(ap, ap->active_tag);
Tejun Heoa4631472006-02-11 19:11:13 +09003388 WARN_ON(qc == NULL);
Albert Leec14b8332005-12-05 15:36:08 +08003389
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 drv_stat = ata_wait_idle(ap);
3391 if (!ata_ok(drv_stat)) {
Albert Lee1c848982005-12-05 15:40:15 +08003392 qc->err_mask |= __ac_err_mask(drv_stat);
Albert Lee14be71f2005-09-27 17:36:35 +08003393 ap->hsm_task_state = HSM_ST_ERR;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003394 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 }
3396
Albert Lee14be71f2005-09-27 17:36:35 +08003397 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398
Tejun Heoa4631472006-02-11 19:11:13 +09003399 WARN_ON(qc->err_mask);
Albert Leea22e2eb2005-12-05 15:38:02 +08003400 ata_poll_qc_complete(qc);
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003401
3402 /* another command may start at this point */
3403
3404 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003405}
3406
Edward Falk0baab862005-06-02 18:17:13 -04003407
3408/**
Randy Dunlapc893a3a2006-01-28 13:15:32 -05003409 * swap_buf_le16 - swap halves of 16-bit words in place
Edward Falk0baab862005-06-02 18:17:13 -04003410 * @buf: Buffer to swap
3411 * @buf_words: Number of 16-bit words in buffer.
3412 *
3413 * Swap halves of 16-bit words if needed to convert from
3414 * little-endian byte order to native cpu byte order, or
3415 * vice-versa.
3416 *
3417 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003418 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04003419 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003420void swap_buf_le16(u16 *buf, unsigned int buf_words)
3421{
3422#ifdef __BIG_ENDIAN
3423 unsigned int i;
3424
3425 for (i = 0; i < buf_words; i++)
3426 buf[i] = le16_to_cpu(buf[i]);
3427#endif /* __BIG_ENDIAN */
3428}
3429
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003430/**
3431 * ata_mmio_data_xfer - Transfer data by MMIO
3432 * @ap: port to read/write
3433 * @buf: data buffer
3434 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04003435 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003436 *
3437 * Transfer data from/to the device data register by MMIO.
3438 *
3439 * LOCKING:
3440 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003441 */
3442
Linus Torvalds1da177e2005-04-16 15:20:36 -07003443static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf,
3444 unsigned int buflen, int write_data)
3445{
3446 unsigned int i;
3447 unsigned int words = buflen >> 1;
3448 u16 *buf16 = (u16 *) buf;
3449 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
3450
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003451 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003452 if (write_data) {
3453 for (i = 0; i < words; i++)
3454 writew(le16_to_cpu(buf16[i]), mmio);
3455 } else {
3456 for (i = 0; i < words; i++)
3457 buf16[i] = cpu_to_le16(readw(mmio));
3458 }
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003459
3460 /* Transfer trailing 1 byte, if any. */
3461 if (unlikely(buflen & 0x01)) {
3462 u16 align_buf[1] = { 0 };
3463 unsigned char *trailing_buf = buf + buflen - 1;
3464
3465 if (write_data) {
3466 memcpy(align_buf, trailing_buf, 1);
3467 writew(le16_to_cpu(align_buf[0]), mmio);
3468 } else {
3469 align_buf[0] = cpu_to_le16(readw(mmio));
3470 memcpy(trailing_buf, align_buf, 1);
3471 }
3472 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003473}
3474
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003475/**
3476 * ata_pio_data_xfer - Transfer data by PIO
3477 * @ap: port to read/write
3478 * @buf: data buffer
3479 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04003480 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003481 *
3482 * Transfer data from/to the device data register by PIO.
3483 *
3484 * LOCKING:
3485 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003486 */
3487
Linus Torvalds1da177e2005-04-16 15:20:36 -07003488static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf,
3489 unsigned int buflen, int write_data)
3490{
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003491 unsigned int words = buflen >> 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003493 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 if (write_data)
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003495 outsw(ap->ioaddr.data_addr, buf, words);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003496 else
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003497 insw(ap->ioaddr.data_addr, buf, words);
3498
3499 /* Transfer trailing 1 byte, if any. */
3500 if (unlikely(buflen & 0x01)) {
3501 u16 align_buf[1] = { 0 };
3502 unsigned char *trailing_buf = buf + buflen - 1;
3503
3504 if (write_data) {
3505 memcpy(align_buf, trailing_buf, 1);
3506 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
3507 } else {
3508 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
3509 memcpy(trailing_buf, align_buf, 1);
3510 }
3511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512}
3513
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003514/**
3515 * ata_data_xfer - Transfer data from/to the data register.
3516 * @ap: port to read/write
3517 * @buf: data buffer
3518 * @buflen: buffer length
3519 * @do_write: read/write
3520 *
3521 * Transfer data from/to the device data register.
3522 *
3523 * LOCKING:
3524 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003525 */
3526
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527static void ata_data_xfer(struct ata_port *ap, unsigned char *buf,
3528 unsigned int buflen, int do_write)
3529{
Alan Coxa1bd9e62006-01-17 20:53:50 +00003530 /* Make the crap hardware pay the costs not the good stuff */
3531 if (unlikely(ap->flags & ATA_FLAG_IRQ_MASK)) {
3532 unsigned long flags;
3533 local_irq_save(flags);
3534 if (ap->flags & ATA_FLAG_MMIO)
3535 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3536 else
3537 ata_pio_data_xfer(ap, buf, buflen, do_write);
3538 local_irq_restore(flags);
3539 } else {
3540 if (ap->flags & ATA_FLAG_MMIO)
3541 ata_mmio_data_xfer(ap, buf, buflen, do_write);
3542 else
3543 ata_pio_data_xfer(ap, buf, buflen, do_write);
3544 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003545}
3546
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003547/**
3548 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
3549 * @qc: Command on going
3550 *
3551 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
3552 *
3553 * LOCKING:
3554 * Inherited from caller.
3555 */
3556
Linus Torvalds1da177e2005-04-16 15:20:36 -07003557static void ata_pio_sector(struct ata_queued_cmd *qc)
3558{
3559 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003560 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561 struct ata_port *ap = qc->ap;
3562 struct page *page;
3563 unsigned int offset;
3564 unsigned char *buf;
3565
3566 if (qc->cursect == (qc->nsect - 1))
Albert Lee14be71f2005-09-27 17:36:35 +08003567 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003568
3569 page = sg[qc->cursg].page;
3570 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
3571
3572 /* get the current page and offset */
3573 page = nth_page(page, (offset >> PAGE_SHIFT));
3574 offset %= PAGE_SIZE;
3575
3576 buf = kmap(page) + offset;
3577
3578 qc->cursect++;
3579 qc->cursg_ofs++;
3580
Albert Lee32529e02005-05-26 03:49:42 -04003581 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582 qc->cursg++;
3583 qc->cursg_ofs = 0;
3584 }
3585
3586 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3587
3588 /* do the actual data transfer */
3589 do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
3590 ata_data_xfer(ap, buf, ATA_SECT_SIZE, do_write);
3591
3592 kunmap(page);
3593}
3594
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003595/**
3596 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
3597 * @qc: Command on going
3598 * @bytes: number of bytes
3599 *
3600 * Transfer Transfer data from/to the ATAPI device.
3601 *
3602 * LOCKING:
3603 * Inherited from caller.
3604 *
3605 */
3606
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
3608{
3609 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003610 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003611 struct ata_port *ap = qc->ap;
3612 struct page *page;
3613 unsigned char *buf;
3614 unsigned int offset, count;
3615
Albert Lee563a6e12005-08-12 14:17:50 +08003616 if (qc->curbytes + bytes >= qc->nbytes)
Albert Lee14be71f2005-09-27 17:36:35 +08003617 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618
3619next_sg:
Albert Lee563a6e12005-08-12 14:17:50 +08003620 if (unlikely(qc->cursg >= qc->n_elem)) {
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003621 /*
Albert Lee563a6e12005-08-12 14:17:50 +08003622 * The end of qc->sg is reached and the device expects
3623 * more data to transfer. In order not to overrun qc->sg
3624 * and fulfill length specified in the byte count register,
3625 * - for read case, discard trailing data from the device
3626 * - for write case, padding zero data to the device
3627 */
3628 u16 pad_buf[1] = { 0 };
3629 unsigned int words = bytes >> 1;
3630 unsigned int i;
3631
3632 if (words) /* warning if bytes > 1 */
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003633 printk(KERN_WARNING "ata%u: %u bytes trailing data\n",
Albert Lee563a6e12005-08-12 14:17:50 +08003634 ap->id, bytes);
3635
3636 for (i = 0; i < words; i++)
3637 ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write);
3638
Albert Lee14be71f2005-09-27 17:36:35 +08003639 ap->hsm_task_state = HSM_ST_LAST;
Albert Lee563a6e12005-08-12 14:17:50 +08003640 return;
3641 }
3642
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003643 sg = &qc->__sg[qc->cursg];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 page = sg->page;
3646 offset = sg->offset + qc->cursg_ofs;
3647
3648 /* get the current page and offset */
3649 page = nth_page(page, (offset >> PAGE_SHIFT));
3650 offset %= PAGE_SIZE;
3651
Albert Lee6952df02005-06-06 15:56:03 +08003652 /* don't overrun current sg */
Albert Lee32529e02005-05-26 03:49:42 -04003653 count = min(sg->length - qc->cursg_ofs, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654
3655 /* don't cross page boundaries */
3656 count = min(count, (unsigned int)PAGE_SIZE - offset);
3657
3658 buf = kmap(page) + offset;
3659
3660 bytes -= count;
3661 qc->curbytes += count;
3662 qc->cursg_ofs += count;
3663
Albert Lee32529e02005-05-26 03:49:42 -04003664 if (qc->cursg_ofs == sg->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665 qc->cursg++;
3666 qc->cursg_ofs = 0;
3667 }
3668
3669 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3670
3671 /* do the actual data transfer */
3672 ata_data_xfer(ap, buf, count, do_write);
3673
3674 kunmap(page);
3675
Albert Lee563a6e12005-08-12 14:17:50 +08003676 if (bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003677 goto next_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003678}
3679
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003680/**
3681 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
3682 * @qc: Command on going
3683 *
3684 * Transfer Transfer data from/to the ATAPI device.
3685 *
3686 * LOCKING:
3687 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003688 */
3689
Linus Torvalds1da177e2005-04-16 15:20:36 -07003690static void atapi_pio_bytes(struct ata_queued_cmd *qc)
3691{
3692 struct ata_port *ap = qc->ap;
3693 struct ata_device *dev = qc->dev;
3694 unsigned int ireason, bc_lo, bc_hi, bytes;
3695 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
3696
3697 ap->ops->tf_read(ap, &qc->tf);
3698 ireason = qc->tf.nsect;
3699 bc_lo = qc->tf.lbam;
3700 bc_hi = qc->tf.lbah;
3701 bytes = (bc_hi << 8) | bc_lo;
3702
3703 /* shall be cleared to zero, indicating xfer of data */
3704 if (ireason & (1 << 0))
3705 goto err_out;
3706
3707 /* make sure transfer direction matches expected */
3708 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
3709 if (do_write != i_write)
3710 goto err_out;
3711
3712 __atapi_pio_bytes(qc, bytes);
3713
3714 return;
3715
3716err_out:
3717 printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n",
3718 ap->id, dev->devno);
Tejun Heo11a56d22006-01-23 13:09:36 +09003719 qc->err_mask |= AC_ERR_HSM;
Albert Lee14be71f2005-09-27 17:36:35 +08003720 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721}
3722
3723/**
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003724 * ata_pio_block - start PIO on a block
3725 * @ap: the target ata_port
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726 *
3727 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003728 * None. (executing in kernel thread context)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003729 */
3730
3731static void ata_pio_block(struct ata_port *ap)
3732{
3733 struct ata_queued_cmd *qc;
3734 u8 status;
3735
3736 /*
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003737 * This is purely heuristic. This is a fast path.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003738 * Sometimes when we enter, BSY will be cleared in
3739 * a chk-status or two. If not, the drive is probably seeking
3740 * or something. Snooze for a couple msecs, then
3741 * chk-status again. If still busy, fall back to
Albert Lee14be71f2005-09-27 17:36:35 +08003742 * HSM_ST_POLL state.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003743 */
3744 status = ata_busy_wait(ap, ATA_BUSY, 5);
3745 if (status & ATA_BUSY) {
3746 msleep(2);
3747 status = ata_busy_wait(ap, ATA_BUSY, 10);
3748 if (status & ATA_BUSY) {
Albert Lee14be71f2005-09-27 17:36:35 +08003749 ap->hsm_task_state = HSM_ST_POLL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750 ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO;
3751 return;
3752 }
3753 }
3754
3755 qc = ata_qc_from_tag(ap, ap->active_tag);
Tejun Heoa4631472006-02-11 19:11:13 +09003756 WARN_ON(qc == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757
Albert Leefe79e682005-12-06 11:34:59 +08003758 /* check error */
3759 if (status & (ATA_ERR | ATA_DF)) {
3760 qc->err_mask |= AC_ERR_DEV;
3761 ap->hsm_task_state = HSM_ST_ERR;
3762 return;
3763 }
3764
3765 /* transfer data if any */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766 if (is_atapi_taskfile(&qc->tf)) {
Albert Leefe79e682005-12-06 11:34:59 +08003767 /* DRQ=0 means no more data to transfer */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003768 if ((status & ATA_DRQ) == 0) {
Albert Lee14be71f2005-09-27 17:36:35 +08003769 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770 return;
3771 }
3772
3773 atapi_pio_bytes(qc);
3774 } else {
3775 /* handle BSY=0, DRQ=0 as error */
3776 if ((status & ATA_DRQ) == 0) {
Tejun Heo11a56d22006-01-23 13:09:36 +09003777 qc->err_mask |= AC_ERR_HSM;
Albert Lee14be71f2005-09-27 17:36:35 +08003778 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003779 return;
3780 }
3781
3782 ata_pio_sector(qc);
3783 }
3784}
3785
3786static void ata_pio_error(struct ata_port *ap)
3787{
3788 struct ata_queued_cmd *qc;
Jeff Garzika7dac442005-10-30 04:44:42 -05003789
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790 qc = ata_qc_from_tag(ap, ap->active_tag);
Tejun Heoa4631472006-02-11 19:11:13 +09003791 WARN_ON(qc == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003792
Albert Lee0565c262006-02-13 18:55:25 +08003793 if (qc->tf.command != ATA_CMD_PACKET)
3794 printk(KERN_WARNING "ata%u: PIO error\n", ap->id);
3795
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05003796 /* make sure qc->err_mask is available to
Albert Lee1c848982005-12-05 15:40:15 +08003797 * know what's wrong and recover
3798 */
Tejun Heoa4631472006-02-11 19:11:13 +09003799 WARN_ON(qc->err_mask == 0);
Albert Lee1c848982005-12-05 15:40:15 +08003800
Albert Lee14be71f2005-09-27 17:36:35 +08003801 ap->hsm_task_state = HSM_ST_IDLE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802
Albert Leea22e2eb2005-12-05 15:38:02 +08003803 ata_poll_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804}
3805
3806static void ata_pio_task(void *_data)
3807{
3808 struct ata_port *ap = _data;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003809 unsigned long timeout;
3810 int qc_completed;
3811
3812fsm_start:
3813 timeout = 0;
3814 qc_completed = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815
Albert Lee14be71f2005-09-27 17:36:35 +08003816 switch (ap->hsm_task_state) {
3817 case HSM_ST_IDLE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003818 return;
3819
Albert Lee14be71f2005-09-27 17:36:35 +08003820 case HSM_ST:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821 ata_pio_block(ap);
3822 break;
3823
Albert Lee14be71f2005-09-27 17:36:35 +08003824 case HSM_ST_LAST:
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003825 qc_completed = ata_pio_complete(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826 break;
3827
Albert Lee14be71f2005-09-27 17:36:35 +08003828 case HSM_ST_POLL:
3829 case HSM_ST_LAST_POLL:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830 timeout = ata_pio_poll(ap);
3831 break;
3832
Albert Lee14be71f2005-09-27 17:36:35 +08003833 case HSM_ST_TMOUT:
3834 case HSM_ST_ERR:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835 ata_pio_error(ap);
3836 return;
3837 }
3838
3839 if (timeout)
Tejun Heo8061f5f2006-03-05 15:29:09 +09003840 ata_port_queue_task(ap, ata_pio_task, ap, timeout);
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04003841 else if (!qc_completed)
3842 goto fsm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003843}
3844
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845/**
Tejun Heo8061f5f2006-03-05 15:29:09 +09003846 * atapi_packet_task - Write CDB bytes to hardware
3847 * @_data: Port to which ATAPI device is attached.
3848 *
3849 * When device has indicated its readiness to accept
3850 * a CDB, this function is called. Send the CDB.
3851 * If DMA is to be performed, exit immediately.
3852 * Otherwise, we are in polling mode, so poll
3853 * status under operation succeeds or fails.
3854 *
3855 * LOCKING:
3856 * Kernel thread context (may sleep)
3857 */
3858
3859static void atapi_packet_task(void *_data)
3860{
3861 struct ata_port *ap = _data;
3862 struct ata_queued_cmd *qc;
3863 u8 status;
3864
3865 qc = ata_qc_from_tag(ap, ap->active_tag);
3866 WARN_ON(qc == NULL);
3867 WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
3868
3869 /* sleep-wait for BSY to clear */
3870 DPRINTK("busy wait\n");
3871 if (ata_busy_sleep(ap, ATA_TMOUT_CDB_QUICK, ATA_TMOUT_CDB)) {
3872 qc->err_mask |= AC_ERR_TIMEOUT;
3873 goto err_out;
3874 }
3875
3876 /* make sure DRQ is set */
3877 status = ata_chk_status(ap);
3878 if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ) {
3879 qc->err_mask |= AC_ERR_HSM;
3880 goto err_out;
3881 }
3882
3883 /* send SCSI cdb */
3884 DPRINTK("send cdb\n");
3885 WARN_ON(qc->dev->cdb_len < 12);
3886
3887 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA ||
3888 qc->tf.protocol == ATA_PROT_ATAPI_NODATA) {
3889 unsigned long flags;
3890
3891 /* Once we're done issuing command and kicking bmdma,
3892 * irq handler takes over. To not lose irq, we need
3893 * to clear NOINTR flag before sending cdb, but
3894 * interrupt handler shouldn't be invoked before we're
3895 * finished. Hence, the following locking.
3896 */
3897 spin_lock_irqsave(&ap->host_set->lock, flags);
3898 ap->flags &= ~ATA_FLAG_NOINTR;
3899 ata_data_xfer(ap, qc->cdb, qc->dev->cdb_len, 1);
3900 if (qc->tf.protocol == ATA_PROT_ATAPI_DMA)
3901 ap->ops->bmdma_start(qc); /* initiate bmdma */
3902 spin_unlock_irqrestore(&ap->host_set->lock, flags);
3903 } else {
3904 ata_data_xfer(ap, qc->cdb, qc->dev->cdb_len, 1);
3905
3906 /* PIO commands are handled by polling */
3907 ap->hsm_task_state = HSM_ST;
3908 ata_port_queue_task(ap, ata_pio_task, ap, 0);
3909 }
3910
3911 return;
3912
3913err_out:
3914 ata_poll_qc_complete(qc);
3915}
3916
3917/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918 * ata_qc_timeout - Handle timeout of queued command
3919 * @qc: Command that timed out
3920 *
3921 * Some part of the kernel (currently, only the SCSI layer)
3922 * has noticed that the active command on port @ap has not
3923 * completed after a specified length of time. Handle this
3924 * condition by disabling DMA (if necessary) and completing
3925 * transactions, with error if necessary.
3926 *
3927 * This also handles the case of the "lost interrupt", where
3928 * for some reason (possibly hardware bug, possibly driver bug)
3929 * an interrupt was not delivered to the driver, even though the
3930 * transaction completed successfully.
3931 *
3932 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003933 * Inherited from SCSI layer (none, can sleep)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934 */
3935
3936static void ata_qc_timeout(struct ata_queued_cmd *qc)
3937{
3938 struct ata_port *ap = qc->ap;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003939 struct ata_host_set *host_set = ap->host_set;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940 u8 host_stat = 0, drv_stat;
Jeff Garzikb8f61532005-08-25 22:01:20 -04003941 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942
3943 DPRINTK("ENTER\n");
3944
Tejun Heoc18d06f2006-02-02 00:56:10 +09003945 ap->hsm_task_state = HSM_ST_IDLE;
3946
Jeff Garzikb8f61532005-08-25 22:01:20 -04003947 spin_lock_irqsave(&host_set->lock, flags);
3948
Linus Torvalds1da177e2005-04-16 15:20:36 -07003949 switch (qc->tf.protocol) {
3950
3951 case ATA_PROT_DMA:
3952 case ATA_PROT_ATAPI_DMA:
3953 host_stat = ap->ops->bmdma_status(ap);
3954
3955 /* before we do anything else, clear DMA-Start bit */
Alan Coxb73fc892005-08-26 16:03:19 +01003956 ap->ops->bmdma_stop(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957
3958 /* fall through */
3959
3960 default:
3961 ata_altstatus(ap);
3962 drv_stat = ata_chk_status(ap);
3963
3964 /* ack bmdma irq events */
3965 ap->ops->irq_clear(ap);
3966
3967 printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n",
3968 ap->id, qc->tf.command, drv_stat, host_stat);
3969
3970 /* complete taskfile transaction */
Albert Leea22e2eb2005-12-05 15:38:02 +08003971 qc->err_mask |= ac_err_mask(drv_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003972 break;
3973 }
Jeff Garzikb8f61532005-08-25 22:01:20 -04003974
3975 spin_unlock_irqrestore(&host_set->lock, flags);
3976
Tejun Heoa72ec4c2006-01-23 13:09:37 +09003977 ata_eh_qc_complete(qc);
3978
Linus Torvalds1da177e2005-04-16 15:20:36 -07003979 DPRINTK("EXIT\n");
3980}
3981
3982/**
3983 * ata_eng_timeout - Handle timeout of queued command
3984 * @ap: Port on which timed-out command is active
3985 *
3986 * Some part of the kernel (currently, only the SCSI layer)
3987 * has noticed that the active command on port @ap has not
3988 * completed after a specified length of time. Handle this
3989 * condition by disabling DMA (if necessary) and completing
3990 * transactions, with error if necessary.
3991 *
3992 * This also handles the case of the "lost interrupt", where
3993 * for some reason (possibly hardware bug, possibly driver bug)
3994 * an interrupt was not delivered to the driver, even though the
3995 * transaction completed successfully.
3996 *
3997 * LOCKING:
3998 * Inherited from SCSI layer (none, can sleep)
3999 */
4000
4001void ata_eng_timeout(struct ata_port *ap)
4002{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004003 DPRINTK("ENTER\n");
4004
Tejun Heof6379022006-02-10 15:10:48 +09004005 ata_qc_timeout(ata_qc_from_tag(ap, ap->active_tag));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007 DPRINTK("EXIT\n");
4008}
4009
4010/**
4011 * ata_qc_new - Request an available ATA command, for queueing
4012 * @ap: Port associated with device @dev
4013 * @dev: Device from whom we request an available command structure
4014 *
4015 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004016 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017 */
4018
4019static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
4020{
4021 struct ata_queued_cmd *qc = NULL;
4022 unsigned int i;
4023
4024 for (i = 0; i < ATA_MAX_QUEUE; i++)
4025 if (!test_and_set_bit(i, &ap->qactive)) {
4026 qc = ata_qc_from_tag(ap, i);
4027 break;
4028 }
4029
4030 if (qc)
4031 qc->tag = i;
4032
4033 return qc;
4034}
4035
4036/**
4037 * ata_qc_new_init - Request an available ATA command, and initialize it
4038 * @ap: Port associated with device @dev
4039 * @dev: Device from whom we request an available command structure
4040 *
4041 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004042 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004043 */
4044
4045struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap,
4046 struct ata_device *dev)
4047{
4048 struct ata_queued_cmd *qc;
4049
4050 qc = ata_qc_new(ap);
4051 if (qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052 qc->scsicmd = NULL;
4053 qc->ap = ap;
4054 qc->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05004056 ata_qc_reinit(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057 }
4058
4059 return qc;
4060}
4061
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062/**
4063 * ata_qc_free - free unused ata_queued_cmd
4064 * @qc: Command to complete
4065 *
4066 * Designed to free unused ata_queued_cmd object
4067 * in case something prevents using it.
4068 *
4069 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004070 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071 */
4072void ata_qc_free(struct ata_queued_cmd *qc)
4073{
Tejun Heo4ba946e2006-01-23 13:09:36 +09004074 struct ata_port *ap = qc->ap;
4075 unsigned int tag;
4076
Tejun Heoa4631472006-02-11 19:11:13 +09004077 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078
Tejun Heo4ba946e2006-01-23 13:09:36 +09004079 qc->flags = 0;
4080 tag = qc->tag;
4081 if (likely(ata_tag_valid(tag))) {
4082 if (tag == ap->active_tag)
4083 ap->active_tag = ATA_TAG_POISON;
4084 qc->tag = ATA_TAG_POISON;
4085 clear_bit(tag, &ap->qactive);
4086 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087}
4088
Tejun Heo76014422006-02-11 15:13:49 +09004089void __ata_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090{
Tejun Heoa4631472006-02-11 19:11:13 +09004091 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
4092 WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093
4094 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
4095 ata_sg_clean(qc);
4096
Albert Lee3f3791d2005-08-16 14:25:38 +08004097 /* atapi: mark qc as inactive to prevent the interrupt handler
4098 * from completing the command twice later, before the error handler
4099 * is called. (when rc != 0 and atapi request sense is needed)
4100 */
4101 qc->flags &= ~ATA_QCFLAG_ACTIVE;
4102
Linus Torvalds1da177e2005-04-16 15:20:36 -07004103 /* call completion callback */
Tejun Heo77853bf2006-01-23 13:09:36 +09004104 qc->complete_fn(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004105}
4106
4107static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
4108{
4109 struct ata_port *ap = qc->ap;
4110
4111 switch (qc->tf.protocol) {
4112 case ATA_PROT_DMA:
4113 case ATA_PROT_ATAPI_DMA:
4114 return 1;
4115
4116 case ATA_PROT_ATAPI:
4117 case ATA_PROT_PIO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118 if (ap->flags & ATA_FLAG_PIO_DMA)
4119 return 1;
4120
4121 /* fall through */
4122
4123 default:
4124 return 0;
4125 }
4126
4127 /* never reached */
4128}
4129
4130/**
4131 * ata_qc_issue - issue taskfile to device
4132 * @qc: command to issue to device
4133 *
4134 * Prepare an ATA command to submission to device.
4135 * This includes mapping the data into a DMA-able
4136 * area, filling in the S/G table, and finally
4137 * writing the taskfile to hardware, starting the command.
4138 *
4139 * LOCKING:
4140 * spin_lock_irqsave(host_set lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004141 */
Tejun Heo8e0e6942006-03-31 20:41:11 +09004142void ata_qc_issue(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143{
4144 struct ata_port *ap = qc->ap;
4145
Tejun Heoe4a70e72006-03-31 20:36:47 +09004146 qc->ap->active_tag = qc->tag;
4147 qc->flags |= ATA_QCFLAG_ACTIVE;
4148
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149 if (ata_should_dma_map(qc)) {
4150 if (qc->flags & ATA_QCFLAG_SG) {
4151 if (ata_sg_setup(qc))
Tejun Heo8e436af2006-01-23 13:09:36 +09004152 goto sg_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
4154 if (ata_sg_setup_one(qc))
Tejun Heo8e436af2006-01-23 13:09:36 +09004155 goto sg_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156 }
4157 } else {
4158 qc->flags &= ~ATA_QCFLAG_DMAMAP;
4159 }
4160
4161 ap->ops->qc_prep(qc);
4162
Tejun Heo8e0e6942006-03-31 20:41:11 +09004163 qc->err_mask |= ap->ops->qc_issue(qc);
4164 if (unlikely(qc->err_mask))
4165 goto err;
4166 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167
Tejun Heo8e436af2006-01-23 13:09:36 +09004168sg_err:
4169 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Tejun Heo8e0e6942006-03-31 20:41:11 +09004170 qc->err_mask |= AC_ERR_SYSTEM;
4171err:
4172 ata_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173}
4174
4175/**
4176 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
4177 * @qc: command to issue to device
4178 *
4179 * Using various libata functions and hooks, this function
4180 * starts an ATA command. ATA commands are grouped into
4181 * classes called "protocols", and issuing each type of protocol
4182 * is slightly different.
4183 *
Edward Falk0baab862005-06-02 18:17:13 -04004184 * May be used as the qc_issue() entry in ata_port_operations.
4185 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186 * LOCKING:
4187 * spin_lock_irqsave(host_set lock)
4188 *
4189 * RETURNS:
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09004190 * Zero on success, AC_ERR_* mask on failure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191 */
4192
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09004193unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194{
4195 struct ata_port *ap = qc->ap;
4196
4197 ata_dev_select(ap, qc->dev->devno, 1, 0);
4198
4199 switch (qc->tf.protocol) {
4200 case ATA_PROT_NODATA:
Jeff Garzike5338252005-10-30 21:37:17 -05004201 ata_tf_to_host(ap, &qc->tf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202 break;
4203
4204 case ATA_PROT_DMA:
4205 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
4206 ap->ops->bmdma_setup(qc); /* set up bmdma */
4207 ap->ops->bmdma_start(qc); /* initiate bmdma */
4208 break;
4209
4210 case ATA_PROT_PIO: /* load tf registers, initiate polling pio */
4211 ata_qc_set_polling(qc);
Jeff Garzike5338252005-10-30 21:37:17 -05004212 ata_tf_to_host(ap, &qc->tf);
Albert Lee14be71f2005-09-27 17:36:35 +08004213 ap->hsm_task_state = HSM_ST;
Tejun Heo8061f5f2006-03-05 15:29:09 +09004214 ata_port_queue_task(ap, ata_pio_task, ap, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215 break;
4216
4217 case ATA_PROT_ATAPI:
4218 ata_qc_set_polling(qc);
Jeff Garzike5338252005-10-30 21:37:17 -05004219 ata_tf_to_host(ap, &qc->tf);
Tejun Heo8061f5f2006-03-05 15:29:09 +09004220 ata_port_queue_task(ap, atapi_packet_task, ap, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221 break;
4222
4223 case ATA_PROT_ATAPI_NODATA:
Tejun Heoc1389502005-08-22 14:59:24 +09004224 ap->flags |= ATA_FLAG_NOINTR;
Jeff Garzike5338252005-10-30 21:37:17 -05004225 ata_tf_to_host(ap, &qc->tf);
Tejun Heo8061f5f2006-03-05 15:29:09 +09004226 ata_port_queue_task(ap, atapi_packet_task, ap, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227 break;
4228
4229 case ATA_PROT_ATAPI_DMA:
Tejun Heoc1389502005-08-22 14:59:24 +09004230 ap->flags |= ATA_FLAG_NOINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
4232 ap->ops->bmdma_setup(qc); /* set up bmdma */
Tejun Heo8061f5f2006-03-05 15:29:09 +09004233 ata_port_queue_task(ap, atapi_packet_task, ap, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234 break;
4235
4236 default:
4237 WARN_ON(1);
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09004238 return AC_ERR_SYSTEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239 }
4240
4241 return 0;
4242}
4243
4244/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245 * ata_host_intr - Handle host interrupt for given (port, task)
4246 * @ap: Port on which interrupt arrived (possibly...)
4247 * @qc: Taskfile currently active in engine
4248 *
4249 * Handle host interrupt for given queued command. Currently,
4250 * only DMA interrupts are handled. All other commands are
4251 * handled via polling with interrupts disabled (nIEN bit).
4252 *
4253 * LOCKING:
4254 * spin_lock_irqsave(host_set lock)
4255 *
4256 * RETURNS:
4257 * One if interrupt was handled, zero if not (shared irq).
4258 */
4259
4260inline unsigned int ata_host_intr (struct ata_port *ap,
4261 struct ata_queued_cmd *qc)
4262{
4263 u8 status, host_stat;
4264
4265 switch (qc->tf.protocol) {
4266
4267 case ATA_PROT_DMA:
4268 case ATA_PROT_ATAPI_DMA:
4269 case ATA_PROT_ATAPI:
4270 /* check status of DMA engine */
4271 host_stat = ap->ops->bmdma_status(ap);
4272 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
4273
4274 /* if it's not our irq... */
4275 if (!(host_stat & ATA_DMA_INTR))
4276 goto idle_irq;
4277
4278 /* before we do anything else, clear DMA-Start bit */
Alan Coxb73fc892005-08-26 16:03:19 +01004279 ap->ops->bmdma_stop(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004280
4281 /* fall through */
4282
4283 case ATA_PROT_ATAPI_NODATA:
4284 case ATA_PROT_NODATA:
4285 /* check altstatus */
4286 status = ata_altstatus(ap);
4287 if (status & ATA_BUSY)
4288 goto idle_irq;
4289
4290 /* check main status, clearing INTRQ */
4291 status = ata_chk_status(ap);
4292 if (unlikely(status & ATA_BUSY))
4293 goto idle_irq;
4294 DPRINTK("ata%u: protocol %d (dev_stat 0x%X)\n",
4295 ap->id, qc->tf.protocol, status);
4296
4297 /* ack bmdma irq events */
4298 ap->ops->irq_clear(ap);
4299
4300 /* complete taskfile transaction */
Albert Leea22e2eb2005-12-05 15:38:02 +08004301 qc->err_mask |= ac_err_mask(status);
4302 ata_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303 break;
4304
4305 default:
4306 goto idle_irq;
4307 }
4308
4309 return 1; /* irq handled */
4310
4311idle_irq:
4312 ap->stats.idle_irq++;
4313
4314#ifdef ATA_IRQ_TRAP
4315 if ((ap->stats.idle_irq % 1000) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316 ata_irq_ack(ap, 0); /* debug trap */
4317 printk(KERN_WARNING "ata%d: irq trap\n", ap->id);
Alan Cox23cfce82006-03-21 16:06:53 +00004318 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319 }
4320#endif
4321 return 0; /* irq not handled */
4322}
4323
4324/**
4325 * ata_interrupt - Default ATA host interrupt handler
Jeff Garzik0cba6322005-05-30 19:49:12 -04004326 * @irq: irq line (unused)
4327 * @dev_instance: pointer to our ata_host_set information structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328 * @regs: unused
4329 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004330 * Default interrupt handler for PCI IDE devices. Calls
4331 * ata_host_intr() for each port that is not disabled.
4332 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004334 * Obtains host_set lock during operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 *
4336 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004337 * IRQ_NONE or IRQ_HANDLED.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338 */
4339
4340irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs)
4341{
4342 struct ata_host_set *host_set = dev_instance;
4343 unsigned int i;
4344 unsigned int handled = 0;
4345 unsigned long flags;
4346
4347 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
4348 spin_lock_irqsave(&host_set->lock, flags);
4349
4350 for (i = 0; i < host_set->n_ports; i++) {
4351 struct ata_port *ap;
4352
4353 ap = host_set->ports[i];
Tejun Heoc1389502005-08-22 14:59:24 +09004354 if (ap &&
4355 !(ap->flags & (ATA_FLAG_PORT_DISABLED | ATA_FLAG_NOINTR))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356 struct ata_queued_cmd *qc;
4357
4358 qc = ata_qc_from_tag(ap, ap->active_tag);
Albert Lee21b1ed72005-04-29 17:34:59 +08004359 if (qc && (!(qc->tf.ctl & ATA_NIEN)) &&
4360 (qc->flags & ATA_QCFLAG_ACTIVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361 handled |= ata_host_intr(ap, qc);
4362 }
4363 }
4364
4365 spin_unlock_irqrestore(&host_set->lock, flags);
4366
4367 return IRQ_RETVAL(handled);
4368}
4369
Edward Falk0baab862005-06-02 18:17:13 -04004370
Jens Axboe9b847542006-01-06 09:28:07 +01004371/*
4372 * Execute a 'simple' command, that only consists of the opcode 'cmd' itself,
4373 * without filling any other registers
4374 */
4375static int ata_do_simple_cmd(struct ata_port *ap, struct ata_device *dev,
4376 u8 cmd)
4377{
4378 struct ata_taskfile tf;
4379 int err;
4380
4381 ata_tf_init(ap, &tf, dev->devno);
4382
4383 tf.command = cmd;
4384 tf.flags |= ATA_TFLAG_DEVICE;
4385 tf.protocol = ATA_PROT_NODATA;
4386
4387 err = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0);
4388 if (err)
4389 printk(KERN_ERR "%s: ata command failed: %d\n",
4390 __FUNCTION__, err);
4391
4392 return err;
4393}
4394
4395static int ata_flush_cache(struct ata_port *ap, struct ata_device *dev)
4396{
4397 u8 cmd;
4398
4399 if (!ata_try_flush_cache(dev))
4400 return 0;
4401
4402 if (ata_id_has_flush_ext(dev->id))
4403 cmd = ATA_CMD_FLUSH_EXT;
4404 else
4405 cmd = ATA_CMD_FLUSH;
4406
4407 return ata_do_simple_cmd(ap, dev, cmd);
4408}
4409
4410static int ata_standby_drive(struct ata_port *ap, struct ata_device *dev)
4411{
4412 return ata_do_simple_cmd(ap, dev, ATA_CMD_STANDBYNOW1);
4413}
4414
4415static int ata_start_drive(struct ata_port *ap, struct ata_device *dev)
4416{
4417 return ata_do_simple_cmd(ap, dev, ATA_CMD_IDLEIMMEDIATE);
4418}
4419
4420/**
4421 * ata_device_resume - wakeup a previously suspended devices
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004422 * @ap: port the device is connected to
4423 * @dev: the device to resume
Jens Axboe9b847542006-01-06 09:28:07 +01004424 *
4425 * Kick the drive back into action, by sending it an idle immediate
4426 * command and making sure its transfer mode matches between drive
4427 * and host.
4428 *
4429 */
4430int ata_device_resume(struct ata_port *ap, struct ata_device *dev)
4431{
4432 if (ap->flags & ATA_FLAG_SUSPENDED) {
Tejun Heoe82cbdb2006-04-01 01:38:18 +09004433 struct ata_device *failed_dev;
Jens Axboe9b847542006-01-06 09:28:07 +01004434 ap->flags &= ~ATA_FLAG_SUSPENDED;
Tejun Heoe82cbdb2006-04-01 01:38:18 +09004435 while (ata_set_mode(ap, &failed_dev))
4436 ata_dev_disable(ap, failed_dev);
Jens Axboe9b847542006-01-06 09:28:07 +01004437 }
Tejun Heoe1211e32006-04-01 01:38:18 +09004438 if (!ata_dev_enabled(dev))
Jens Axboe9b847542006-01-06 09:28:07 +01004439 return 0;
4440 if (dev->class == ATA_DEV_ATA)
4441 ata_start_drive(ap, dev);
4442
4443 return 0;
4444}
4445
4446/**
4447 * ata_device_suspend - prepare a device for suspend
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004448 * @ap: port the device is connected to
4449 * @dev: the device to suspend
Jens Axboe9b847542006-01-06 09:28:07 +01004450 *
4451 * Flush the cache on the drive, if appropriate, then issue a
4452 * standbynow command.
Jens Axboe9b847542006-01-06 09:28:07 +01004453 */
Nigel Cunningham082776e2006-03-23 23:22:16 +10004454int ata_device_suspend(struct ata_port *ap, struct ata_device *dev, pm_message_t state)
Jens Axboe9b847542006-01-06 09:28:07 +01004455{
Tejun Heoe1211e32006-04-01 01:38:18 +09004456 if (!ata_dev_enabled(dev))
Jens Axboe9b847542006-01-06 09:28:07 +01004457 return 0;
4458 if (dev->class == ATA_DEV_ATA)
4459 ata_flush_cache(ap, dev);
4460
Nigel Cunningham082776e2006-03-23 23:22:16 +10004461 if (state.event != PM_EVENT_FREEZE)
4462 ata_standby_drive(ap, dev);
Jens Axboe9b847542006-01-06 09:28:07 +01004463 ap->flags |= ATA_FLAG_SUSPENDED;
4464 return 0;
4465}
4466
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004467/**
4468 * ata_port_start - Set port up for dma.
4469 * @ap: Port to initialize
4470 *
4471 * Called just after data structures for each port are
4472 * initialized. Allocates space for PRD table.
4473 *
4474 * May be used as the port_start() entry in ata_port_operations.
4475 *
4476 * LOCKING:
4477 * Inherited from caller.
4478 */
4479
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480int ata_port_start (struct ata_port *ap)
4481{
Brian King2f1f6102006-03-23 17:30:15 -06004482 struct device *dev = ap->dev;
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004483 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484
4485 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
4486 if (!ap->prd)
4487 return -ENOMEM;
4488
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004489 rc = ata_pad_alloc(ap, dev);
4490 if (rc) {
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004491 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004492 return rc;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004493 }
4494
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
4496
4497 return 0;
4498}
4499
Edward Falk0baab862005-06-02 18:17:13 -04004500
4501/**
4502 * ata_port_stop - Undo ata_port_start()
4503 * @ap: Port to shut down
4504 *
4505 * Frees the PRD table.
4506 *
4507 * May be used as the port_stop() entry in ata_port_operations.
4508 *
4509 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004510 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04004511 */
4512
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513void ata_port_stop (struct ata_port *ap)
4514{
Brian King2f1f6102006-03-23 17:30:15 -06004515 struct device *dev = ap->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516
4517 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05004518 ata_pad_free(ap, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519}
4520
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04004521void ata_host_stop (struct ata_host_set *host_set)
4522{
4523 if (host_set->mmio_base)
4524 iounmap(host_set->mmio_base);
4525}
4526
4527
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528/**
4529 * ata_host_remove - Unregister SCSI host structure with upper layers
4530 * @ap: Port to unregister
4531 * @do_unregister: 1 if we fully unregister, 0 to just stop the port
4532 *
4533 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004534 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535 */
4536
4537static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister)
4538{
4539 struct Scsi_Host *sh = ap->host;
4540
4541 DPRINTK("ENTER\n");
4542
4543 if (do_unregister)
4544 scsi_remove_host(sh);
4545
4546 ap->ops->port_stop(ap);
4547}
4548
4549/**
4550 * ata_host_init - Initialize an ata_port structure
4551 * @ap: Structure to initialize
4552 * @host: associated SCSI mid-layer structure
4553 * @host_set: Collection of hosts to which @ap belongs
4554 * @ent: Probe information provided by low-level driver
4555 * @port_no: Port number associated with this ata_port
4556 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004557 * Initialize a new ata_port structure, and its associated
4558 * scsi_host.
4559 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004561 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562 */
4563
4564static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host,
4565 struct ata_host_set *host_set,
Jeff Garzik057ace52005-10-22 14:27:05 -04004566 const struct ata_probe_ent *ent, unsigned int port_no)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567{
4568 unsigned int i;
4569
4570 host->max_id = 16;
4571 host->max_lun = 1;
4572 host->max_channel = 1;
4573 host->unique_id = ata_unique_id++;
4574 host->max_cmd_len = 12;
Christoph Hellwig12413192005-06-11 01:05:01 +02004575
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576 ap->flags = ATA_FLAG_PORT_DISABLED;
4577 ap->id = host->unique_id;
4578 ap->host = host;
4579 ap->ctl = ATA_DEVCTL_OBS;
4580 ap->host_set = host_set;
Brian King2f1f6102006-03-23 17:30:15 -06004581 ap->dev = ent->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004582 ap->port_no = port_no;
4583 ap->hard_port_no =
4584 ent->legacy_mode ? ent->hard_port_no : port_no;
4585 ap->pio_mask = ent->pio_mask;
4586 ap->mwdma_mask = ent->mwdma_mask;
4587 ap->udma_mask = ent->udma_mask;
4588 ap->flags |= ent->host_flags;
4589 ap->ops = ent->port_ops;
4590 ap->cbl = ATA_CBL_NONE;
Tejun Heo1c3fae42006-04-02 20:53:28 +09004591 ap->sata_spd_limit = UINT_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592 ap->active_tag = ATA_TAG_POISON;
4593 ap->last_ctl = 0xFF;
4594
Tejun Heo86e45b62006-03-05 15:29:09 +09004595 INIT_WORK(&ap->port_task, NULL, NULL);
Tejun Heoa72ec4c2006-01-23 13:09:37 +09004596 INIT_LIST_HEAD(&ap->eh_done_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597
Tejun Heoacf356b2006-03-24 14:07:50 +09004598 for (i = 0; i < ATA_MAX_DEVICES; i++) {
4599 struct ata_device *dev = &ap->device[i];
4600 dev->devno = i;
4601 dev->pio_mask = UINT_MAX;
4602 dev->mwdma_mask = UINT_MAX;
4603 dev->udma_mask = UINT_MAX;
4604 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605
4606#ifdef ATA_IRQ_TRAP
4607 ap->stats.unhandled_irq = 1;
4608 ap->stats.idle_irq = 1;
4609#endif
4610
4611 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
4612}
4613
4614/**
4615 * ata_host_add - Attach low-level ATA driver to system
4616 * @ent: Information provided by low-level driver
4617 * @host_set: Collections of ports to which we add
4618 * @port_no: Port number associated with this host
4619 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04004620 * Attach low-level ATA driver to system.
4621 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004623 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624 *
4625 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004626 * New ata_port on success, for NULL on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627 */
4628
Jeff Garzik057ace52005-10-22 14:27:05 -04004629static struct ata_port * ata_host_add(const struct ata_probe_ent *ent,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630 struct ata_host_set *host_set,
4631 unsigned int port_no)
4632{
4633 struct Scsi_Host *host;
4634 struct ata_port *ap;
4635 int rc;
4636
4637 DPRINTK("ENTER\n");
Tejun Heoaec5c3c2006-03-25 01:33:34 +09004638
4639 if (!ent->port_ops->probe_reset &&
4640 !(ent->host_flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST))) {
4641 printk(KERN_ERR "ata%u: no reset mechanism available\n",
4642 port_no);
4643 return NULL;
4644 }
4645
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646 host = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
4647 if (!host)
4648 return NULL;
4649
Tejun Heo30afc842006-03-18 18:40:14 +09004650 host->transportt = &ata_scsi_transport_template;
4651
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652 ap = (struct ata_port *) &host->hostdata[0];
4653
4654 ata_host_init(ap, host, host_set, ent, port_no);
4655
4656 rc = ap->ops->port_start(ap);
4657 if (rc)
4658 goto err_out;
4659
4660 return ap;
4661
4662err_out:
4663 scsi_host_put(host);
4664 return NULL;
4665}
4666
4667/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004668 * ata_device_add - Register hardware device with ATA and SCSI layers
4669 * @ent: Probe information describing hardware device to be registered
4670 *
4671 * This function processes the information provided in the probe
4672 * information struct @ent, allocates the necessary ATA and SCSI
4673 * host information structures, initializes them, and registers
4674 * everything with requisite kernel subsystems.
4675 *
4676 * This function requests irqs, probes the ATA bus, and probes
4677 * the SCSI bus.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678 *
4679 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004680 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681 *
4682 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004683 * Number of ports registered. Zero on error (no ports registered).
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 */
4685
Jeff Garzik057ace52005-10-22 14:27:05 -04004686int ata_device_add(const struct ata_probe_ent *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687{
4688 unsigned int count = 0, i;
4689 struct device *dev = ent->dev;
4690 struct ata_host_set *host_set;
4691
4692 DPRINTK("ENTER\n");
4693 /* alloc a container for our list of ATA ports (buses) */
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004694 host_set = kzalloc(sizeof(struct ata_host_set) +
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
4696 if (!host_set)
4697 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004698 spin_lock_init(&host_set->lock);
4699
4700 host_set->dev = dev;
4701 host_set->n_ports = ent->n_ports;
4702 host_set->irq = ent->irq;
4703 host_set->mmio_base = ent->mmio_base;
4704 host_set->private_data = ent->private_data;
4705 host_set->ops = ent->port_ops;
Alan Cox5444a6f2006-03-27 18:58:20 +01004706 host_set->flags = ent->host_set_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707
4708 /* register each port bound to this device */
4709 for (i = 0; i < ent->n_ports; i++) {
4710 struct ata_port *ap;
4711 unsigned long xfer_mode_mask;
4712
4713 ap = ata_host_add(ent, host_set, i);
4714 if (!ap)
4715 goto err_out;
4716
4717 host_set->ports[i] = ap;
4718 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
4719 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
4720 (ap->pio_mask << ATA_SHIFT_PIO);
4721
4722 /* print per-port info to dmesg */
4723 printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX "
4724 "bmdma 0x%lX irq %lu\n",
4725 ap->id,
4726 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
4727 ata_mode_string(xfer_mode_mask),
4728 ap->ioaddr.cmd_addr,
4729 ap->ioaddr.ctl_addr,
4730 ap->ioaddr.bmdma_addr,
4731 ent->irq);
4732
4733 ata_chk_status(ap);
4734 host_set->ops->irq_clear(ap);
4735 count++;
4736 }
4737
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004738 if (!count)
4739 goto err_free_ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740
4741 /* obtain irq, that is shared between channels */
4742 if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
4743 DRV_NAME, host_set))
4744 goto err_out;
4745
4746 /* perform each probe synchronously */
4747 DPRINTK("probe begin\n");
4748 for (i = 0; i < count; i++) {
4749 struct ata_port *ap;
4750 int rc;
4751
4752 ap = host_set->ports[i];
4753
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004754 DPRINTK("ata%u: bus probe begin\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755 rc = ata_bus_probe(ap);
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004756 DPRINTK("ata%u: bus probe end\n", ap->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757
4758 if (rc) {
4759 /* FIXME: do something useful here?
4760 * Current libata behavior will
4761 * tear down everything when
4762 * the module is removed
4763 * or the h/w is unplugged.
4764 */
4765 }
4766
4767 rc = scsi_add_host(ap->host, dev);
4768 if (rc) {
4769 printk(KERN_ERR "ata%u: scsi_add_host failed\n",
4770 ap->id);
4771 /* FIXME: do something useful here */
4772 /* FIXME: handle unconditional calls to
4773 * scsi_scan_host and ata_host_remove, below,
4774 * at the very least
4775 */
4776 }
4777 }
4778
4779 /* probes are done, now scan each port's disk(s) */
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004780 DPRINTK("host probe begin\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781 for (i = 0; i < count; i++) {
4782 struct ata_port *ap = host_set->ports[i];
4783
Jeff Garzik644dd0c2005-10-03 15:55:19 -04004784 ata_scsi_scan_host(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785 }
4786
4787 dev_set_drvdata(dev, host_set);
4788
4789 VPRINTK("EXIT, returning %u\n", ent->n_ports);
4790 return ent->n_ports; /* success */
4791
4792err_out:
4793 for (i = 0; i < count; i++) {
4794 ata_host_remove(host_set->ports[i], 1);
4795 scsi_host_put(host_set->ports[i]->host);
4796 }
Randy Dunlap57f3bda2005-10-28 20:37:23 -07004797err_free_ret:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798 kfree(host_set);
4799 VPRINTK("EXIT, returning 0\n");
4800 return 0;
4801}
4802
4803/**
Alan Cox17b14452005-09-15 15:44:00 +01004804 * ata_host_set_remove - PCI layer callback for device removal
4805 * @host_set: ATA host set that was removed
4806 *
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05004807 * Unregister all objects associated with this host set. Free those
Alan Cox17b14452005-09-15 15:44:00 +01004808 * objects.
4809 *
4810 * LOCKING:
4811 * Inherited from calling layer (may sleep).
4812 */
4813
Alan Cox17b14452005-09-15 15:44:00 +01004814void ata_host_set_remove(struct ata_host_set *host_set)
4815{
4816 struct ata_port *ap;
4817 unsigned int i;
4818
4819 for (i = 0; i < host_set->n_ports; i++) {
4820 ap = host_set->ports[i];
4821 scsi_remove_host(ap->host);
4822 }
4823
4824 free_irq(host_set->irq, host_set);
4825
4826 for (i = 0; i < host_set->n_ports; i++) {
4827 ap = host_set->ports[i];
4828
4829 ata_scsi_release(ap->host);
4830
4831 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
4832 struct ata_ioports *ioaddr = &ap->ioaddr;
4833
4834 if (ioaddr->cmd_addr == 0x1f0)
4835 release_region(0x1f0, 8);
4836 else if (ioaddr->cmd_addr == 0x170)
4837 release_region(0x170, 8);
4838 }
4839
4840 scsi_host_put(ap->host);
4841 }
4842
4843 if (host_set->ops->host_stop)
4844 host_set->ops->host_stop(host_set);
4845
4846 kfree(host_set);
4847}
4848
4849/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850 * ata_scsi_release - SCSI layer callback hook for host unload
4851 * @host: libata host to be unloaded
4852 *
4853 * Performs all duties necessary to shut down a libata port...
4854 * Kill port kthread, disable port, and release resources.
4855 *
4856 * LOCKING:
4857 * Inherited from SCSI layer.
4858 *
4859 * RETURNS:
4860 * One.
4861 */
4862
4863int ata_scsi_release(struct Scsi_Host *host)
4864{
4865 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
Tejun Heod9572b12006-03-01 16:09:35 +09004866 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004867
4868 DPRINTK("ENTER\n");
4869
4870 ap->ops->port_disable(ap);
4871 ata_host_remove(ap, 0);
Tejun Heod9572b12006-03-01 16:09:35 +09004872 for (i = 0; i < ATA_MAX_DEVICES; i++)
4873 kfree(ap->device[i].id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874
4875 DPRINTK("EXIT\n");
4876 return 1;
4877}
4878
4879/**
4880 * ata_std_ports - initialize ioaddr with standard port offsets.
4881 * @ioaddr: IO address structure to be initialized
Edward Falk0baab862005-06-02 18:17:13 -04004882 *
4883 * Utility function which initializes data_addr, error_addr,
4884 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
4885 * device_addr, status_addr, and command_addr to standard offsets
4886 * relative to cmd_addr.
4887 *
4888 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004889 */
Edward Falk0baab862005-06-02 18:17:13 -04004890
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891void ata_std_ports(struct ata_ioports *ioaddr)
4892{
4893 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
4894 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
4895 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
4896 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
4897 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
4898 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
4899 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
4900 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
4901 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
4902 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
4903}
4904
Edward Falk0baab862005-06-02 18:17:13 -04004905
Jeff Garzik374b1872005-08-30 05:42:52 -04004906#ifdef CONFIG_PCI
4907
4908void ata_pci_host_stop (struct ata_host_set *host_set)
4909{
4910 struct pci_dev *pdev = to_pci_dev(host_set->dev);
4911
4912 pci_iounmap(pdev, host_set->mmio_base);
4913}
4914
Edward Falk0baab862005-06-02 18:17:13 -04004915/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916 * ata_pci_remove_one - PCI layer callback for device removal
4917 * @pdev: PCI device that was removed
4918 *
4919 * PCI layer indicates to libata via this hook that
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004920 * hot-unplug or module unload event has occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921 * Handle this by unregistering all objects associated
4922 * with this PCI device. Free those objects. Then finally
4923 * release PCI resources and disable device.
4924 *
4925 * LOCKING:
4926 * Inherited from PCI layer (may sleep).
4927 */
4928
4929void ata_pci_remove_one (struct pci_dev *pdev)
4930{
4931 struct device *dev = pci_dev_to_dev(pdev);
4932 struct ata_host_set *host_set = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933
Alan Cox17b14452005-09-15 15:44:00 +01004934 ata_host_set_remove(host_set);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935 pci_release_regions(pdev);
4936 pci_disable_device(pdev);
4937 dev_set_drvdata(dev, NULL);
4938}
4939
4940/* move to PCI subsystem */
Jeff Garzik057ace52005-10-22 14:27:05 -04004941int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942{
4943 unsigned long tmp = 0;
4944
4945 switch (bits->width) {
4946 case 1: {
4947 u8 tmp8 = 0;
4948 pci_read_config_byte(pdev, bits->reg, &tmp8);
4949 tmp = tmp8;
4950 break;
4951 }
4952 case 2: {
4953 u16 tmp16 = 0;
4954 pci_read_config_word(pdev, bits->reg, &tmp16);
4955 tmp = tmp16;
4956 break;
4957 }
4958 case 4: {
4959 u32 tmp32 = 0;
4960 pci_read_config_dword(pdev, bits->reg, &tmp32);
4961 tmp = tmp32;
4962 break;
4963 }
4964
4965 default:
4966 return -EINVAL;
4967 }
4968
4969 tmp &= bits->mask;
4970
4971 return (tmp == bits->val) ? 1 : 0;
4972}
Jens Axboe9b847542006-01-06 09:28:07 +01004973
4974int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t state)
4975{
4976 pci_save_state(pdev);
4977 pci_disable_device(pdev);
4978 pci_set_power_state(pdev, PCI_D3hot);
4979 return 0;
4980}
4981
4982int ata_pci_device_resume(struct pci_dev *pdev)
4983{
4984 pci_set_power_state(pdev, PCI_D0);
4985 pci_restore_state(pdev);
4986 pci_enable_device(pdev);
4987 pci_set_master(pdev);
4988 return 0;
4989}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990#endif /* CONFIG_PCI */
4991
4992
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993static int __init ata_init(void)
4994{
4995 ata_wq = create_workqueue("ata");
4996 if (!ata_wq)
4997 return -ENOMEM;
4998
4999 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
5000 return 0;
5001}
5002
5003static void __exit ata_exit(void)
5004{
5005 destroy_workqueue(ata_wq);
5006}
5007
5008module_init(ata_init);
5009module_exit(ata_exit);
5010
Jeff Garzik67846b32005-10-05 02:58:32 -04005011static unsigned long ratelimit_time;
5012static spinlock_t ata_ratelimit_lock = SPIN_LOCK_UNLOCKED;
5013
5014int ata_ratelimit(void)
5015{
5016 int rc;
5017 unsigned long flags;
5018
5019 spin_lock_irqsave(&ata_ratelimit_lock, flags);
5020
5021 if (time_after(jiffies, ratelimit_time)) {
5022 rc = 1;
5023 ratelimit_time = jiffies + (HZ/5);
5024 } else
5025 rc = 0;
5026
5027 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
5028
5029 return rc;
5030}
5031
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032/*
5033 * libata is essentially a library of internal helper functions for
5034 * low-level ATA host controller drivers. As such, the API/ABI is
5035 * likely to change as new drivers are added and updated.
5036 * Do not depend on ABI/API stability.
5037 */
5038
5039EXPORT_SYMBOL_GPL(ata_std_bios_param);
5040EXPORT_SYMBOL_GPL(ata_std_ports);
5041EXPORT_SYMBOL_GPL(ata_device_add);
Alan Cox17b14452005-09-15 15:44:00 +01005042EXPORT_SYMBOL_GPL(ata_host_set_remove);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043EXPORT_SYMBOL_GPL(ata_sg_init);
5044EXPORT_SYMBOL_GPL(ata_sg_init_one);
Tejun Heo76014422006-02-11 15:13:49 +09005045EXPORT_SYMBOL_GPL(__ata_qc_complete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005046EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
5047EXPORT_SYMBOL_GPL(ata_eng_timeout);
5048EXPORT_SYMBOL_GPL(ata_tf_load);
5049EXPORT_SYMBOL_GPL(ata_tf_read);
5050EXPORT_SYMBOL_GPL(ata_noop_dev_select);
5051EXPORT_SYMBOL_GPL(ata_std_dev_select);
5052EXPORT_SYMBOL_GPL(ata_tf_to_fis);
5053EXPORT_SYMBOL_GPL(ata_tf_from_fis);
5054EXPORT_SYMBOL_GPL(ata_check_status);
5055EXPORT_SYMBOL_GPL(ata_altstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005056EXPORT_SYMBOL_GPL(ata_exec_command);
5057EXPORT_SYMBOL_GPL(ata_port_start);
5058EXPORT_SYMBOL_GPL(ata_port_stop);
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04005059EXPORT_SYMBOL_GPL(ata_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060EXPORT_SYMBOL_GPL(ata_interrupt);
5061EXPORT_SYMBOL_GPL(ata_qc_prep);
Brian Kinge46834c2006-03-17 17:04:03 -06005062EXPORT_SYMBOL_GPL(ata_noop_qc_prep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063EXPORT_SYMBOL_GPL(ata_bmdma_setup);
5064EXPORT_SYMBOL_GPL(ata_bmdma_start);
5065EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
5066EXPORT_SYMBOL_GPL(ata_bmdma_status);
5067EXPORT_SYMBOL_GPL(ata_bmdma_stop);
5068EXPORT_SYMBOL_GPL(ata_port_probe);
5069EXPORT_SYMBOL_GPL(sata_phy_reset);
5070EXPORT_SYMBOL_GPL(__sata_phy_reset);
5071EXPORT_SYMBOL_GPL(ata_bus_reset);
Tejun Heo8a19ac82006-02-02 18:20:00 +09005072EXPORT_SYMBOL_GPL(ata_std_probeinit);
Tejun Heoc2bd5802006-01-24 17:05:22 +09005073EXPORT_SYMBOL_GPL(ata_std_softreset);
5074EXPORT_SYMBOL_GPL(sata_std_hardreset);
5075EXPORT_SYMBOL_GPL(ata_std_postreset);
5076EXPORT_SYMBOL_GPL(ata_std_probe_reset);
Tejun Heoa62c0fc2006-01-24 17:05:22 +09005077EXPORT_SYMBOL_GPL(ata_drive_probe_reset);
Tejun Heo623a3122006-03-05 17:55:58 +09005078EXPORT_SYMBOL_GPL(ata_dev_revalidate);
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05005079EXPORT_SYMBOL_GPL(ata_dev_classify);
5080EXPORT_SYMBOL_GPL(ata_dev_pair);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081EXPORT_SYMBOL_GPL(ata_port_disable);
Jeff Garzik67846b32005-10-05 02:58:32 -04005082EXPORT_SYMBOL_GPL(ata_ratelimit);
Tejun Heo6f8b9952006-01-24 17:05:21 +09005083EXPORT_SYMBOL_GPL(ata_busy_sleep);
Tejun Heo86e45b62006-03-05 15:29:09 +09005084EXPORT_SYMBOL_GPL(ata_port_queue_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005085EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
5086EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
5087EXPORT_SYMBOL_GPL(ata_scsi_error);
5088EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
5089EXPORT_SYMBOL_GPL(ata_scsi_release);
5090EXPORT_SYMBOL_GPL(ata_host_intr);
Tejun Heo6a62a042006-02-13 10:02:46 +09005091EXPORT_SYMBOL_GPL(ata_id_string);
5092EXPORT_SYMBOL_GPL(ata_id_c_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093EXPORT_SYMBOL_GPL(ata_scsi_simulate);
Tejun Heoa72ec4c2006-01-23 13:09:37 +09005094EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
5095EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096
Alan Cox1bc4ccf2006-01-09 17:18:14 +00005097EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
Alan Cox452503f2005-10-21 19:01:32 -04005098EXPORT_SYMBOL_GPL(ata_timing_compute);
5099EXPORT_SYMBOL_GPL(ata_timing_merge);
5100
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101#ifdef CONFIG_PCI
5102EXPORT_SYMBOL_GPL(pci_test_config_bits);
Jeff Garzik374b1872005-08-30 05:42:52 -04005103EXPORT_SYMBOL_GPL(ata_pci_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005104EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
5105EXPORT_SYMBOL_GPL(ata_pci_init_one);
5106EXPORT_SYMBOL_GPL(ata_pci_remove_one);
Jens Axboe9b847542006-01-06 09:28:07 +01005107EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
5108EXPORT_SYMBOL_GPL(ata_pci_device_resume);
Alan Cox67951ad2006-03-22 15:55:54 +00005109EXPORT_SYMBOL_GPL(ata_pci_default_filter);
5110EXPORT_SYMBOL_GPL(ata_pci_clear_simplex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005111#endif /* CONFIG_PCI */
Jens Axboe9b847542006-01-06 09:28:07 +01005112
5113EXPORT_SYMBOL_GPL(ata_device_suspend);
5114EXPORT_SYMBOL_GPL(ata_device_resume);
5115EXPORT_SYMBOL_GPL(ata_scsi_device_suspend);
5116EXPORT_SYMBOL_GPL(ata_scsi_device_resume);