blob: deda68446b437c15ff7fffb88794eaf417e3e2ea [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/kernel.h>
36#include <linux/module.h>
37#include <linux/pci.h>
38#include <linux/init.h>
39#include <linux/list.h>
40#include <linux/mm.h>
41#include <linux/highmem.h>
42#include <linux/spinlock.h>
43#include <linux/blkdev.h>
44#include <linux/delay.h>
45#include <linux/timer.h>
46#include <linux/interrupt.h>
47#include <linux/completion.h>
48#include <linux/suspend.h>
49#include <linux/workqueue.h>
Jeff Garzik67846b32005-10-05 02:58:32 -040050#include <linux/jiffies.h>
David Hardeman378f0582005-09-17 17:55:31 +100051#include <linux/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <scsi/scsi.h>
Jeff Garzik193515d2005-11-07 00:59:37 -050053#include <scsi/scsi_cmnd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <scsi/scsi_host.h>
55#include <linux/libata.h>
56#include <asm/io.h>
57#include <asm/semaphore.h>
58#include <asm/byteorder.h>
59
60#include "libata.h"
61
Jeff Garzik8bc3fc42007-05-21 20:26:38 -040062#define DRV_VERSION "2.21" /* must be exactly four chars */
Jeff Garzikfda0efc2007-01-31 07:43:15 -050063
64
Tejun Heod7bb4cc2006-05-31 18:27:46 +090065/* debounce timing parameters in msecs { interval, duration, timeout } */
Tejun Heoe9c83912006-07-03 16:07:26 +090066const unsigned long sata_deb_timing_normal[] = { 5, 100, 2000 };
67const unsigned long sata_deb_timing_hotplug[] = { 25, 500, 2000 };
68const unsigned long sata_deb_timing_long[] = { 100, 2000, 5000 };
Tejun Heod7bb4cc2006-05-31 18:27:46 +090069
Tejun Heo3373efd2006-05-15 20:57:53 +090070static unsigned int ata_dev_init_params(struct ata_device *dev,
71 u16 heads, u16 sectors);
72static unsigned int ata_dev_set_xfermode(struct ata_device *dev);
73static void ata_dev_xfermask(struct ata_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Tejun Heof3187192007-04-17 23:44:07 +090075unsigned int ata_print_id = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076static struct workqueue_struct *ata_wq;
77
Tejun Heo453b07a2006-05-31 18:27:42 +090078struct workqueue_struct *ata_aux_wq;
79
Jeff Garzik418dc1f2006-03-11 20:50:08 -050080int atapi_enabled = 1;
Jeff Garzik1623c812005-08-30 03:37:42 -040081module_param(atapi_enabled, int, 0444);
82MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)");
83
Albert Lee95de7192006-04-04 10:57:18 +080084int atapi_dmadir = 0;
85module_param(atapi_dmadir, int, 0444);
86MODULE_PARM_DESC(atapi_dmadir, "Enable ATAPI DMADIR bridge support (0=off, 1=on)");
87
Jeff Garzikc3c013a2006-02-27 22:31:19 -050088int libata_fua = 0;
89module_param_named(fua, libata_fua, int, 0444);
90MODULE_PARM_DESC(fua, "FUA support (0=off, 1=on)");
91
Alan Cox1e999732007-04-11 00:23:13 +010092static int ata_ignore_hpa = 0;
93module_param_named(ignore_hpa, ata_ignore_hpa, int, 0644);
94MODULE_PARM_DESC(ignore_hpa, "Ignore HPA limit (0=keep BIOS limits, 1=ignore limits, using full disk)");
95
Andrew Mortona8601e52006-06-25 01:36:52 -070096static int ata_probe_timeout = ATA_TMOUT_INTERNAL / HZ;
97module_param(ata_probe_timeout, int, 0444);
98MODULE_PARM_DESC(ata_probe_timeout, "Set ATA probing timeout (seconds)");
99
Jeff Garzikd7d0dad2007-03-28 01:57:37 -0400100int libata_noacpi = 1;
101module_param_named(noacpi, libata_noacpi, int, 0444);
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700102MODULE_PARM_DESC(noacpi, "Disables the use of ACPI in suspend/resume when set");
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104MODULE_AUTHOR("Jeff Garzik");
105MODULE_DESCRIPTION("Library module for ATA devices");
106MODULE_LICENSE("GPL");
107MODULE_VERSION(DRV_VERSION);
108
Edward Falk0baab862005-06-02 18:17:13 -0400109
110/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
112 * @tf: Taskfile to convert
113 * @fis: Buffer into which data will output
114 * @pmp: Port multiplier port
115 *
116 * Converts a standard ATA taskfile to a Serial ATA
117 * FIS structure (Register - Host to Device).
118 *
119 * LOCKING:
120 * Inherited from caller.
121 */
122
Jeff Garzik057ace52005-10-22 14:27:05 -0400123void ata_tf_to_fis(const struct ata_taskfile *tf, u8 *fis, u8 pmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
125 fis[0] = 0x27; /* Register - Host to Device FIS */
126 fis[1] = (pmp & 0xf) | (1 << 7); /* Port multiplier number,
127 bit 7 indicates Command FIS */
128 fis[2] = tf->command;
129 fis[3] = tf->feature;
130
131 fis[4] = tf->lbal;
132 fis[5] = tf->lbam;
133 fis[6] = tf->lbah;
134 fis[7] = tf->device;
135
136 fis[8] = tf->hob_lbal;
137 fis[9] = tf->hob_lbam;
138 fis[10] = tf->hob_lbah;
139 fis[11] = tf->hob_feature;
140
141 fis[12] = tf->nsect;
142 fis[13] = tf->hob_nsect;
143 fis[14] = 0;
144 fis[15] = tf->ctl;
145
146 fis[16] = 0;
147 fis[17] = 0;
148 fis[18] = 0;
149 fis[19] = 0;
150}
151
152/**
153 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
154 * @fis: Buffer from which data will be input
155 * @tf: Taskfile to output
156 *
Mark Lorde12a1be2005-11-12 18:55:45 -0500157 * Converts a serial ATA FIS structure to a standard ATA taskfile.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 *
159 * LOCKING:
160 * Inherited from caller.
161 */
162
Jeff Garzik057ace52005-10-22 14:27:05 -0400163void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
165 tf->command = fis[2]; /* status */
166 tf->feature = fis[3]; /* error */
167
168 tf->lbal = fis[4];
169 tf->lbam = fis[5];
170 tf->lbah = fis[6];
171 tf->device = fis[7];
172
173 tf->hob_lbal = fis[8];
174 tf->hob_lbam = fis[9];
175 tf->hob_lbah = fis[10];
176
177 tf->nsect = fis[12];
178 tf->hob_nsect = fis[13];
179}
180
Albert Lee8cbd6df2005-10-12 15:06:27 +0800181static const u8 ata_rw_cmds[] = {
182 /* pio multi */
183 ATA_CMD_READ_MULTI,
184 ATA_CMD_WRITE_MULTI,
185 ATA_CMD_READ_MULTI_EXT,
186 ATA_CMD_WRITE_MULTI_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100187 0,
188 0,
189 0,
190 ATA_CMD_WRITE_MULTI_FUA_EXT,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800191 /* pio */
192 ATA_CMD_PIO_READ,
193 ATA_CMD_PIO_WRITE,
194 ATA_CMD_PIO_READ_EXT,
195 ATA_CMD_PIO_WRITE_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100196 0,
197 0,
198 0,
199 0,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800200 /* dma */
201 ATA_CMD_READ,
202 ATA_CMD_WRITE,
203 ATA_CMD_READ_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100204 ATA_CMD_WRITE_EXT,
205 0,
206 0,
207 0,
208 ATA_CMD_WRITE_FUA_EXT
Albert Lee8cbd6df2005-10-12 15:06:27 +0800209};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211/**
Albert Lee8cbd6df2005-10-12 15:06:27 +0800212 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
Tejun Heobd056d72006-11-14 22:47:10 +0900213 * @tf: command to examine and configure
214 * @dev: device tf belongs to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 *
Jeff Garzik2e9edbf2006-03-24 09:56:57 -0500216 * Examine the device configuration and tf->flags to calculate
Albert Lee8cbd6df2005-10-12 15:06:27 +0800217 * the proper read/write commands and protocol to use.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 *
219 * LOCKING:
220 * caller.
221 */
Tejun Heobd056d72006-11-14 22:47:10 +0900222static int ata_rwcmd_protocol(struct ata_taskfile *tf, struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100224 u8 cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100226 int index, fua, lba48, write;
Jeff Garzik2e9edbf2006-03-24 09:56:57 -0500227
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100228 fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800229 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
230 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Albert Lee8cbd6df2005-10-12 15:06:27 +0800232 if (dev->flags & ATA_DFLAG_PIO) {
233 tf->protocol = ATA_PROT_PIO;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100234 index = dev->multi_count ? 0 : 8;
Tejun Heobd056d72006-11-14 22:47:10 +0900235 } else if (lba48 && (dev->ap->flags & ATA_FLAG_PIO_LBA48)) {
Alan Cox8d238e02006-01-17 20:50:31 +0000236 /* Unable to use DMA due to host limitation */
237 tf->protocol = ATA_PROT_PIO;
Albert Lee0565c262006-02-13 18:55:25 +0800238 index = dev->multi_count ? 0 : 8;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800239 } else {
240 tf->protocol = ATA_PROT_DMA;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100241 index = 16;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100244 cmd = ata_rw_cmds[index + fua + lba48 + write];
245 if (cmd) {
246 tf->command = cmd;
247 return 0;
248 }
249 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250}
251
Tejun Heocb95d562006-03-06 04:31:56 +0900252/**
Tejun Heo35b649f2006-11-14 22:37:35 +0900253 * ata_tf_read_block - Read block address from ATA taskfile
254 * @tf: ATA taskfile of interest
255 * @dev: ATA device @tf belongs to
256 *
257 * LOCKING:
258 * None.
259 *
260 * Read block address from @tf. This function can handle all
261 * three address formats - LBA, LBA48 and CHS. tf->protocol and
262 * flags select the address format to use.
263 *
264 * RETURNS:
265 * Block address read from @tf.
266 */
267u64 ata_tf_read_block(struct ata_taskfile *tf, struct ata_device *dev)
268{
269 u64 block = 0;
270
271 if (tf->flags & ATA_TFLAG_LBA) {
272 if (tf->flags & ATA_TFLAG_LBA48) {
273 block |= (u64)tf->hob_lbah << 40;
274 block |= (u64)tf->hob_lbam << 32;
275 block |= tf->hob_lbal << 24;
276 } else
277 block |= (tf->device & 0xf) << 24;
278
279 block |= tf->lbah << 16;
280 block |= tf->lbam << 8;
281 block |= tf->lbal;
282 } else {
283 u32 cyl, head, sect;
284
285 cyl = tf->lbam | (tf->lbah << 8);
286 head = tf->device & 0xf;
287 sect = tf->lbal;
288
289 block = (cyl * dev->heads + head) * dev->sectors + sect;
290 }
291
292 return block;
293}
294
295/**
Tejun Heobd056d72006-11-14 22:47:10 +0900296 * ata_build_rw_tf - Build ATA taskfile for given read/write request
297 * @tf: Target ATA taskfile
298 * @dev: ATA device @tf belongs to
299 * @block: Block address
300 * @n_block: Number of blocks
301 * @tf_flags: RW/FUA etc...
302 * @tag: tag
303 *
304 * LOCKING:
305 * None.
306 *
307 * Build ATA taskfile @tf for read/write request described by
308 * @block, @n_block, @tf_flags and @tag on @dev.
309 *
310 * RETURNS:
311 *
312 * 0 on success, -ERANGE if the request is too large for @dev,
313 * -EINVAL if the request is invalid.
314 */
315int ata_build_rw_tf(struct ata_taskfile *tf, struct ata_device *dev,
316 u64 block, u32 n_block, unsigned int tf_flags,
317 unsigned int tag)
318{
319 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
320 tf->flags |= tf_flags;
321
Tejun Heo6d1245b2007-02-20 23:20:27 +0900322 if (ata_ncq_enabled(dev) && likely(tag != ATA_TAG_INTERNAL)) {
Tejun Heobd056d72006-11-14 22:47:10 +0900323 /* yay, NCQ */
324 if (!lba_48_ok(block, n_block))
325 return -ERANGE;
326
327 tf->protocol = ATA_PROT_NCQ;
328 tf->flags |= ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
329
330 if (tf->flags & ATA_TFLAG_WRITE)
331 tf->command = ATA_CMD_FPDMA_WRITE;
332 else
333 tf->command = ATA_CMD_FPDMA_READ;
334
335 tf->nsect = tag << 3;
336 tf->hob_feature = (n_block >> 8) & 0xff;
337 tf->feature = n_block & 0xff;
338
339 tf->hob_lbah = (block >> 40) & 0xff;
340 tf->hob_lbam = (block >> 32) & 0xff;
341 tf->hob_lbal = (block >> 24) & 0xff;
342 tf->lbah = (block >> 16) & 0xff;
343 tf->lbam = (block >> 8) & 0xff;
344 tf->lbal = block & 0xff;
345
346 tf->device = 1 << 6;
347 if (tf->flags & ATA_TFLAG_FUA)
348 tf->device |= 1 << 7;
349 } else if (dev->flags & ATA_DFLAG_LBA) {
350 tf->flags |= ATA_TFLAG_LBA;
351
352 if (lba_28_ok(block, n_block)) {
353 /* use LBA28 */
354 tf->device |= (block >> 24) & 0xf;
355 } else if (lba_48_ok(block, n_block)) {
356 if (!(dev->flags & ATA_DFLAG_LBA48))
357 return -ERANGE;
358
359 /* use LBA48 */
360 tf->flags |= ATA_TFLAG_LBA48;
361
362 tf->hob_nsect = (n_block >> 8) & 0xff;
363
364 tf->hob_lbah = (block >> 40) & 0xff;
365 tf->hob_lbam = (block >> 32) & 0xff;
366 tf->hob_lbal = (block >> 24) & 0xff;
367 } else
368 /* request too large even for LBA48 */
369 return -ERANGE;
370
371 if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
372 return -EINVAL;
373
374 tf->nsect = n_block & 0xff;
375
376 tf->lbah = (block >> 16) & 0xff;
377 tf->lbam = (block >> 8) & 0xff;
378 tf->lbal = block & 0xff;
379
380 tf->device |= ATA_LBA;
381 } else {
382 /* CHS */
383 u32 sect, head, cyl, track;
384
385 /* The request -may- be too large for CHS addressing. */
386 if (!lba_28_ok(block, n_block))
387 return -ERANGE;
388
389 if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
390 return -EINVAL;
391
392 /* Convert LBA to CHS */
393 track = (u32)block / dev->sectors;
394 cyl = track / dev->heads;
395 head = track % dev->heads;
396 sect = (u32)block % dev->sectors + 1;
397
398 DPRINTK("block %u track %u cyl %u head %u sect %u\n",
399 (u32)block, track, cyl, head, sect);
400
401 /* Check whether the converted CHS can fit.
402 Cylinder: 0-65535
403 Head: 0-15
404 Sector: 1-255*/
405 if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
406 return -ERANGE;
407
408 tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
409 tf->lbal = sect;
410 tf->lbam = cyl;
411 tf->lbah = cyl >> 8;
412 tf->device |= head;
413 }
414
415 return 0;
416}
417
418/**
Tejun Heocb95d562006-03-06 04:31:56 +0900419 * ata_pack_xfermask - Pack pio, mwdma and udma masks into xfer_mask
420 * @pio_mask: pio_mask
421 * @mwdma_mask: mwdma_mask
422 * @udma_mask: udma_mask
423 *
424 * Pack @pio_mask, @mwdma_mask and @udma_mask into a single
425 * unsigned int xfer_mask.
426 *
427 * LOCKING:
428 * None.
429 *
430 * RETURNS:
431 * Packed xfer_mask.
432 */
433static unsigned int ata_pack_xfermask(unsigned int pio_mask,
434 unsigned int mwdma_mask,
435 unsigned int udma_mask)
436{
437 return ((pio_mask << ATA_SHIFT_PIO) & ATA_MASK_PIO) |
438 ((mwdma_mask << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA) |
439 ((udma_mask << ATA_SHIFT_UDMA) & ATA_MASK_UDMA);
440}
441
Tejun Heoc0489e42006-03-24 14:07:49 +0900442/**
443 * ata_unpack_xfermask - Unpack xfer_mask into pio, mwdma and udma masks
444 * @xfer_mask: xfer_mask to unpack
445 * @pio_mask: resulting pio_mask
446 * @mwdma_mask: resulting mwdma_mask
447 * @udma_mask: resulting udma_mask
448 *
449 * Unpack @xfer_mask into @pio_mask, @mwdma_mask and @udma_mask.
450 * Any NULL distination masks will be ignored.
451 */
452static void ata_unpack_xfermask(unsigned int xfer_mask,
453 unsigned int *pio_mask,
454 unsigned int *mwdma_mask,
455 unsigned int *udma_mask)
456{
457 if (pio_mask)
458 *pio_mask = (xfer_mask & ATA_MASK_PIO) >> ATA_SHIFT_PIO;
459 if (mwdma_mask)
460 *mwdma_mask = (xfer_mask & ATA_MASK_MWDMA) >> ATA_SHIFT_MWDMA;
461 if (udma_mask)
462 *udma_mask = (xfer_mask & ATA_MASK_UDMA) >> ATA_SHIFT_UDMA;
463}
464
Tejun Heocb95d562006-03-06 04:31:56 +0900465static const struct ata_xfer_ent {
Tejun Heobe9a50c2006-03-31 22:48:52 +0900466 int shift, bits;
Tejun Heocb95d562006-03-06 04:31:56 +0900467 u8 base;
468} ata_xfer_tbl[] = {
469 { ATA_SHIFT_PIO, ATA_BITS_PIO, XFER_PIO_0 },
470 { ATA_SHIFT_MWDMA, ATA_BITS_MWDMA, XFER_MW_DMA_0 },
471 { ATA_SHIFT_UDMA, ATA_BITS_UDMA, XFER_UDMA_0 },
472 { -1, },
473};
474
475/**
476 * ata_xfer_mask2mode - Find matching XFER_* for the given xfer_mask
477 * @xfer_mask: xfer_mask of interest
478 *
479 * Return matching XFER_* value for @xfer_mask. Only the highest
480 * bit of @xfer_mask is considered.
481 *
482 * LOCKING:
483 * None.
484 *
485 * RETURNS:
486 * Matching XFER_* value, 0 if no match found.
487 */
488static u8 ata_xfer_mask2mode(unsigned int xfer_mask)
489{
490 int highbit = fls(xfer_mask) - 1;
491 const struct ata_xfer_ent *ent;
492
493 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
494 if (highbit >= ent->shift && highbit < ent->shift + ent->bits)
495 return ent->base + highbit - ent->shift;
496 return 0;
497}
498
499/**
500 * ata_xfer_mode2mask - Find matching xfer_mask for XFER_*
501 * @xfer_mode: XFER_* of interest
502 *
503 * Return matching xfer_mask for @xfer_mode.
504 *
505 * LOCKING:
506 * None.
507 *
508 * RETURNS:
509 * Matching xfer_mask, 0 if no match found.
510 */
511static unsigned int ata_xfer_mode2mask(u8 xfer_mode)
512{
513 const struct ata_xfer_ent *ent;
514
515 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
516 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
517 return 1 << (ent->shift + xfer_mode - ent->base);
518 return 0;
519}
520
521/**
522 * ata_xfer_mode2shift - Find matching xfer_shift for XFER_*
523 * @xfer_mode: XFER_* of interest
524 *
525 * Return matching xfer_shift for @xfer_mode.
526 *
527 * LOCKING:
528 * None.
529 *
530 * RETURNS:
531 * Matching xfer_shift, -1 if no match found.
532 */
533static int ata_xfer_mode2shift(unsigned int xfer_mode)
534{
535 const struct ata_xfer_ent *ent;
536
537 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
538 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
539 return ent->shift;
540 return -1;
541}
542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543/**
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900544 * ata_mode_string - convert xfer_mask to string
545 * @xfer_mask: mask of bits supported; only highest bit counts.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 *
547 * Determine string which represents the highest speed
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900548 * (highest bit in @modemask).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 *
550 * LOCKING:
551 * None.
552 *
553 * RETURNS:
554 * Constant C string representing highest speed listed in
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900555 * @mode_mask, or the constant C string "<n/a>".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 */
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900557static const char *ata_mode_string(unsigned int xfer_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
Tejun Heo75f554b2006-03-06 04:31:57 +0900559 static const char * const xfer_mode_str[] = {
560 "PIO0",
561 "PIO1",
562 "PIO2",
563 "PIO3",
564 "PIO4",
Alan Coxb352e572006-08-10 18:52:12 +0100565 "PIO5",
566 "PIO6",
Tejun Heo75f554b2006-03-06 04:31:57 +0900567 "MWDMA0",
568 "MWDMA1",
569 "MWDMA2",
Alan Coxb352e572006-08-10 18:52:12 +0100570 "MWDMA3",
571 "MWDMA4",
Tejun Heo75f554b2006-03-06 04:31:57 +0900572 "UDMA/16",
573 "UDMA/25",
574 "UDMA/33",
575 "UDMA/44",
576 "UDMA/66",
577 "UDMA/100",
578 "UDMA/133",
579 "UDMA7",
580 };
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900581 int highbit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900583 highbit = fls(xfer_mask) - 1;
584 if (highbit >= 0 && highbit < ARRAY_SIZE(xfer_mode_str))
585 return xfer_mode_str[highbit];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 return "<n/a>";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587}
588
Tejun Heo4c360c82006-04-01 01:38:17 +0900589static const char *sata_spd_string(unsigned int spd)
590{
591 static const char * const spd_str[] = {
592 "1.5 Gbps",
593 "3.0 Gbps",
594 };
595
596 if (spd == 0 || (spd - 1) >= ARRAY_SIZE(spd_str))
597 return "<unknown>";
598 return spd_str[spd - 1];
599}
600
Tejun Heo3373efd2006-05-15 20:57:53 +0900601void ata_dev_disable(struct ata_device *dev)
Tejun Heo0b8efb02006-03-24 15:25:31 +0900602{
Borislav Petkov0dd4b212006-06-23 02:29:08 -0400603 if (ata_dev_enabled(dev) && ata_msg_drv(dev->ap)) {
Tejun Heof15a1da2006-05-15 20:57:56 +0900604 ata_dev_printk(dev, KERN_WARNING, "disabled\n");
Tejun Heo4ae72a12007-02-02 16:22:30 +0900605 ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO0 |
606 ATA_DNXFER_QUIET);
Tejun Heo0b8efb02006-03-24 15:25:31 +0900607 dev->class++;
608 }
609}
610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 * ata_devchk - PATA device presence detection
613 * @ap: ATA channel to examine
614 * @device: Device to examine (starting at zero)
615 *
Tejun Heo0d5ff562007-02-01 15:06:36 +0900616 * This technique was originally described in
617 * Hale Landis's ATADRVR (www.ata-atapi.com), and
618 * later found its way into the ATA/ATAPI spec.
619 *
620 * Write a pattern to the ATA shadow registers,
621 * and if a device is present, it will respond by
622 * correctly storing and echoing back the
623 * ATA shadow register contents.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 *
625 * LOCKING:
626 * caller.
627 */
628
Tejun Heo0d5ff562007-02-01 15:06:36 +0900629static unsigned int ata_devchk(struct ata_port *ap, unsigned int device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
Tejun Heo0d5ff562007-02-01 15:06:36 +0900631 struct ata_ioports *ioaddr = &ap->ioaddr;
632 u8 nsect, lbal;
633
634 ap->ops->dev_select(ap, device);
635
636 iowrite8(0x55, ioaddr->nsect_addr);
637 iowrite8(0xaa, ioaddr->lbal_addr);
638
639 iowrite8(0xaa, ioaddr->nsect_addr);
640 iowrite8(0x55, ioaddr->lbal_addr);
641
642 iowrite8(0x55, ioaddr->nsect_addr);
643 iowrite8(0xaa, ioaddr->lbal_addr);
644
645 nsect = ioread8(ioaddr->nsect_addr);
646 lbal = ioread8(ioaddr->lbal_addr);
647
648 if ((nsect == 0x55) && (lbal == 0xaa))
649 return 1; /* we found a device */
650
651 return 0; /* nothing found */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652}
653
654/**
655 * ata_dev_classify - determine device type based on ATA-spec signature
656 * @tf: ATA taskfile register set for device to be identified
657 *
658 * Determine from taskfile register contents whether a device is
659 * ATA or ATAPI, as per "Signature and persistence" section
660 * of ATA/PI spec (volume 1, sect 5.14).
661 *
662 * LOCKING:
663 * None.
664 *
665 * RETURNS:
666 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
667 * the event of failure.
668 */
669
Jeff Garzik057ace52005-10-22 14:27:05 -0400670unsigned int ata_dev_classify(const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
672 /* Apple's open source Darwin code hints that some devices only
673 * put a proper signature into the LBA mid/high registers,
674 * So, we only check those. It's sufficient for uniqueness.
675 */
676
677 if (((tf->lbam == 0) && (tf->lbah == 0)) ||
678 ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
679 DPRINTK("found ATA device by sig\n");
680 return ATA_DEV_ATA;
681 }
682
683 if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
684 ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
685 DPRINTK("found ATAPI device by sig\n");
686 return ATA_DEV_ATAPI;
687 }
688
689 DPRINTK("unknown device\n");
690 return ATA_DEV_UNKNOWN;
691}
692
693/**
694 * ata_dev_try_classify - Parse returned ATA device signature
695 * @ap: ATA channel to examine
696 * @device: Device to examine (starting at zero)
Tejun Heob4dc7622006-01-24 17:05:22 +0900697 * @r_err: Value of error register on completion
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 *
699 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
700 * an ATA/ATAPI-defined set of values is placed in the ATA
701 * shadow registers, indicating the results of device detection
702 * and diagnostics.
703 *
704 * Select the ATA device, and read the values from the ATA shadow
705 * registers. Then parse according to the Error register value,
706 * and the spec-defined values examined by ata_dev_classify().
707 *
708 * LOCKING:
709 * caller.
Tejun Heob4dc7622006-01-24 17:05:22 +0900710 *
711 * RETURNS:
712 * Device type - %ATA_DEV_ATA, %ATA_DEV_ATAPI or %ATA_DEV_NONE.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 */
714
Akira Iguchia619f981b2007-01-26 16:28:18 +0900715unsigned int
Tejun Heob4dc7622006-01-24 17:05:22 +0900716ata_dev_try_classify(struct ata_port *ap, unsigned int device, u8 *r_err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 struct ata_taskfile tf;
719 unsigned int class;
720 u8 err;
721
722 ap->ops->dev_select(ap, device);
723
724 memset(&tf, 0, sizeof(tf));
725
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 ap->ops->tf_read(ap, &tf);
Jeff Garzik0169e282005-10-29 21:25:10 -0400727 err = tf.feature;
Tejun Heob4dc7622006-01-24 17:05:22 +0900728 if (r_err)
729 *r_err = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Alan Cox93590852006-09-12 16:55:12 +0100731 /* see if device passed diags: if master then continue and warn later */
732 if (err == 0 && device == 0)
733 /* diagnostic fail : do nothing _YET_ */
734 ap->device[device].horkage |= ATA_HORKAGE_DIAGNOSTIC;
735 else if (err == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 /* do nothing */ ;
737 else if ((device == 0) && (err == 0x81))
738 /* do nothing */ ;
739 else
Tejun Heob4dc7622006-01-24 17:05:22 +0900740 return ATA_DEV_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Tejun Heob4dc7622006-01-24 17:05:22 +0900742 /* determine if device is ATA or ATAPI */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 class = ata_dev_classify(&tf);
Tejun Heob4dc7622006-01-24 17:05:22 +0900744
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 if (class == ATA_DEV_UNKNOWN)
Tejun Heob4dc7622006-01-24 17:05:22 +0900746 return ATA_DEV_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
Tejun Heob4dc7622006-01-24 17:05:22 +0900748 return ATA_DEV_NONE;
749 return class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750}
751
752/**
Tejun Heo6a62a042006-02-13 10:02:46 +0900753 * ata_id_string - Convert IDENTIFY DEVICE page into string
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 * @id: IDENTIFY DEVICE results we will examine
755 * @s: string into which data is output
756 * @ofs: offset into identify device page
757 * @len: length of string to return. must be an even number.
758 *
759 * The strings in the IDENTIFY DEVICE page are broken up into
760 * 16-bit chunks. Run through the string, and output each
761 * 8-bit chunk linearly, regardless of platform.
762 *
763 * LOCKING:
764 * caller.
765 */
766
Tejun Heo6a62a042006-02-13 10:02:46 +0900767void ata_id_string(const u16 *id, unsigned char *s,
768 unsigned int ofs, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769{
770 unsigned int c;
771
772 while (len > 0) {
773 c = id[ofs] >> 8;
774 *s = c;
775 s++;
776
777 c = id[ofs] & 0xff;
778 *s = c;
779 s++;
780
781 ofs++;
782 len -= 2;
783 }
784}
785
Tejun Heo0e949ff2006-02-12 22:47:04 +0900786/**
Tejun Heo6a62a042006-02-13 10:02:46 +0900787 * ata_id_c_string - Convert IDENTIFY DEVICE page into C string
Tejun Heo0e949ff2006-02-12 22:47:04 +0900788 * @id: IDENTIFY DEVICE results we will examine
789 * @s: string into which data is output
790 * @ofs: offset into identify device page
791 * @len: length of string to return. must be an odd number.
792 *
Tejun Heo6a62a042006-02-13 10:02:46 +0900793 * This function is identical to ata_id_string except that it
Tejun Heo0e949ff2006-02-12 22:47:04 +0900794 * trims trailing spaces and terminates the resulting string with
795 * null. @len must be actual maximum length (even number) + 1.
796 *
797 * LOCKING:
798 * caller.
799 */
Tejun Heo6a62a042006-02-13 10:02:46 +0900800void ata_id_c_string(const u16 *id, unsigned char *s,
801 unsigned int ofs, unsigned int len)
Tejun Heo0e949ff2006-02-12 22:47:04 +0900802{
803 unsigned char *p;
804
805 WARN_ON(!(len & 1));
806
Tejun Heo6a62a042006-02-13 10:02:46 +0900807 ata_id_string(id, s, ofs, len - 1);
Tejun Heo0e949ff2006-02-12 22:47:04 +0900808
809 p = s + strnlen(s, len - 1);
810 while (p > s && p[-1] == ' ')
811 p--;
812 *p = '\0';
813}
Edward Falk0baab862005-06-02 18:17:13 -0400814
Alan Cox1e999732007-04-11 00:23:13 +0100815static u64 ata_tf_to_lba48(struct ata_taskfile *tf)
816{
817 u64 sectors = 0;
818
819 sectors |= ((u64)(tf->hob_lbah & 0xff)) << 40;
820 sectors |= ((u64)(tf->hob_lbam & 0xff)) << 32;
821 sectors |= (tf->hob_lbal & 0xff) << 24;
822 sectors |= (tf->lbah & 0xff) << 16;
823 sectors |= (tf->lbam & 0xff) << 8;
824 sectors |= (tf->lbal & 0xff);
825
826 return ++sectors;
827}
828
829static u64 ata_tf_to_lba(struct ata_taskfile *tf)
830{
831 u64 sectors = 0;
832
833 sectors |= (tf->device & 0x0f) << 24;
834 sectors |= (tf->lbah & 0xff) << 16;
835 sectors |= (tf->lbam & 0xff) << 8;
836 sectors |= (tf->lbal & 0xff);
837
838 return ++sectors;
839}
840
841/**
842 * ata_read_native_max_address_ext - LBA48 native max query
843 * @dev: Device to query
844 *
845 * Perform an LBA48 size query upon the device in question. Return the
846 * actual LBA48 size or zero if the command fails.
847 */
848
849static u64 ata_read_native_max_address_ext(struct ata_device *dev)
850{
851 unsigned int err;
852 struct ata_taskfile tf;
853
854 ata_tf_init(dev, &tf);
855
856 tf.command = ATA_CMD_READ_NATIVE_MAX_EXT;
857 tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_LBA48 | ATA_TFLAG_ISADDR;
858 tf.protocol |= ATA_PROT_NODATA;
859 tf.device |= 0x40;
860
861 err = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
862 if (err)
863 return 0;
864
865 return ata_tf_to_lba48(&tf);
866}
867
868/**
869 * ata_read_native_max_address - LBA28 native max query
870 * @dev: Device to query
871 *
872 * Performa an LBA28 size query upon the device in question. Return the
873 * actual LBA28 size or zero if the command fails.
874 */
875
876static u64 ata_read_native_max_address(struct ata_device *dev)
877{
878 unsigned int err;
879 struct ata_taskfile tf;
880
881 ata_tf_init(dev, &tf);
882
883 tf.command = ATA_CMD_READ_NATIVE_MAX;
884 tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
885 tf.protocol |= ATA_PROT_NODATA;
886 tf.device |= 0x40;
887
888 err = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
889 if (err)
890 return 0;
891
892 return ata_tf_to_lba(&tf);
893}
894
895/**
896 * ata_set_native_max_address_ext - LBA48 native max set
897 * @dev: Device to query
Randy Dunlap6b38d1d2007-05-01 17:35:55 -0700898 * @new_sectors: new max sectors value to set for the device
Alan Cox1e999732007-04-11 00:23:13 +0100899 *
900 * Perform an LBA48 size set max upon the device in question. Return the
901 * actual LBA48 size or zero if the command fails.
902 */
903
904static u64 ata_set_native_max_address_ext(struct ata_device *dev, u64 new_sectors)
905{
906 unsigned int err;
907 struct ata_taskfile tf;
908
909 new_sectors--;
910
911 ata_tf_init(dev, &tf);
912
913 tf.command = ATA_CMD_SET_MAX_EXT;
914 tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_LBA48 | ATA_TFLAG_ISADDR;
915 tf.protocol |= ATA_PROT_NODATA;
916 tf.device |= 0x40;
917
918 tf.lbal = (new_sectors >> 0) & 0xff;
919 tf.lbam = (new_sectors >> 8) & 0xff;
920 tf.lbah = (new_sectors >> 16) & 0xff;
921
922 tf.hob_lbal = (new_sectors >> 24) & 0xff;
923 tf.hob_lbam = (new_sectors >> 32) & 0xff;
924 tf.hob_lbah = (new_sectors >> 40) & 0xff;
925
926 err = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
927 if (err)
928 return 0;
929
930 return ata_tf_to_lba48(&tf);
931}
932
933/**
934 * ata_set_native_max_address - LBA28 native max set
935 * @dev: Device to query
Randy Dunlap6b38d1d2007-05-01 17:35:55 -0700936 * @new_sectors: new max sectors value to set for the device
Alan Cox1e999732007-04-11 00:23:13 +0100937 *
938 * Perform an LBA28 size set max upon the device in question. Return the
939 * actual LBA28 size or zero if the command fails.
940 */
941
942static u64 ata_set_native_max_address(struct ata_device *dev, u64 new_sectors)
943{
944 unsigned int err;
945 struct ata_taskfile tf;
946
947 new_sectors--;
948
949 ata_tf_init(dev, &tf);
950
951 tf.command = ATA_CMD_SET_MAX;
952 tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
953 tf.protocol |= ATA_PROT_NODATA;
954
955 tf.lbal = (new_sectors >> 0) & 0xff;
956 tf.lbam = (new_sectors >> 8) & 0xff;
957 tf.lbah = (new_sectors >> 16) & 0xff;
958 tf.device |= ((new_sectors >> 24) & 0x0f) | 0x40;
959
960 err = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
961 if (err)
962 return 0;
963
964 return ata_tf_to_lba(&tf);
965}
966
967/**
968 * ata_hpa_resize - Resize a device with an HPA set
969 * @dev: Device to resize
970 *
971 * Read the size of an LBA28 or LBA48 disk with HPA features and resize
972 * it if required to the full size of the media. The caller must check
973 * the drive has the HPA feature set enabled.
974 */
975
976static u64 ata_hpa_resize(struct ata_device *dev)
977{
978 u64 sectors = dev->n_sectors;
979 u64 hpa_sectors;
Jeff Garzika617c092007-05-21 20:14:23 -0400980
Alan Cox1e999732007-04-11 00:23:13 +0100981 if (ata_id_has_lba48(dev->id))
982 hpa_sectors = ata_read_native_max_address_ext(dev);
983 else
984 hpa_sectors = ata_read_native_max_address(dev);
985
Alan Cox1e999732007-04-11 00:23:13 +0100986 if (hpa_sectors > sectors) {
987 ata_dev_printk(dev, KERN_INFO,
988 "Host Protected Area detected:\n"
989 "\tcurrent size: %lld sectors\n"
990 "\tnative size: %lld sectors\n",
Andrew Mortonbd1d5ec2007-04-26 00:19:21 -0700991 (long long)sectors, (long long)hpa_sectors);
Alan Cox1e999732007-04-11 00:23:13 +0100992
993 if (ata_ignore_hpa) {
994 if (ata_id_has_lba48(dev->id))
995 hpa_sectors = ata_set_native_max_address_ext(dev, hpa_sectors);
996 else
Andrew Mortonbd1d5ec2007-04-26 00:19:21 -0700997 hpa_sectors = ata_set_native_max_address(dev,
998 hpa_sectors);
Alan Cox1e999732007-04-11 00:23:13 +0100999
1000 if (hpa_sectors) {
Andrew Mortonbd1d5ec2007-04-26 00:19:21 -07001001 ata_dev_printk(dev, KERN_INFO, "native size "
1002 "increased to %lld sectors\n",
1003 (long long)hpa_sectors);
Alan Cox1e999732007-04-11 00:23:13 +01001004 return hpa_sectors;
1005 }
1006 }
Tejun Heo37301a52007-06-25 20:45:54 +09001007 } else if (hpa_sectors < sectors)
1008 ata_dev_printk(dev, KERN_WARNING, "%s 1: hpa sectors (%lld) "
1009 "is smaller than sectors (%lld)\n", __FUNCTION__,
1010 (long long)hpa_sectors, (long long)sectors);
1011
Alan Cox1e999732007-04-11 00:23:13 +01001012 return sectors;
1013}
1014
Tejun Heo29407402006-02-12 22:47:04 +09001015static u64 ata_id_n_sectors(const u16 *id)
1016{
1017 if (ata_id_has_lba(id)) {
1018 if (ata_id_has_lba48(id))
1019 return ata_id_u64(id, 100);
1020 else
1021 return ata_id_u32(id, 60);
1022 } else {
1023 if (ata_id_current_chs_valid(id))
1024 return ata_id_u32(id, 57);
1025 else
1026 return id[1] * id[3] * id[6];
1027 }
1028}
1029
Edward Falk0baab862005-06-02 18:17:13 -04001030/**
Alan10305f02007-02-20 18:01:59 +00001031 * ata_id_to_dma_mode - Identify DMA mode from id block
1032 * @dev: device to identify
Randy Dunlapcc261262007-03-16 19:55:47 -07001033 * @unknown: mode to assume if we cannot tell
Alan10305f02007-02-20 18:01:59 +00001034 *
1035 * Set up the timing values for the device based upon the identify
1036 * reported values for the DMA mode. This function is used by drivers
1037 * which rely upon firmware configured modes, but wish to report the
1038 * mode correctly when possible.
1039 *
1040 * In addition we emit similarly formatted messages to the default
1041 * ata_dev_set_mode handler, in order to provide consistency of
1042 * presentation.
1043 */
1044
1045void ata_id_to_dma_mode(struct ata_device *dev, u8 unknown)
1046{
1047 unsigned int mask;
1048 u8 mode;
1049
1050 /* Pack the DMA modes */
1051 mask = ((dev->id[63] >> 8) << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA;
1052 if (dev->id[53] & 0x04)
1053 mask |= ((dev->id[88] >> 8) << ATA_SHIFT_UDMA) & ATA_MASK_UDMA;
1054
1055 /* Select the mode in use */
1056 mode = ata_xfer_mask2mode(mask);
1057
1058 if (mode != 0) {
1059 ata_dev_printk(dev, KERN_INFO, "configured for %s\n",
1060 ata_mode_string(mask));
1061 } else {
1062 /* SWDMA perhaps ? */
1063 mode = unknown;
1064 ata_dev_printk(dev, KERN_INFO, "configured for DMA\n");
1065 }
1066
1067 /* Configure the device reporting */
1068 dev->xfer_mode = mode;
1069 dev->xfer_shift = ata_xfer_mode2shift(mode);
1070}
1071
1072/**
Edward Falk0baab862005-06-02 18:17:13 -04001073 * ata_noop_dev_select - Select device 0/1 on ATA bus
1074 * @ap: ATA channel to manipulate
1075 * @device: ATA device (numbered from zero) to select
1076 *
1077 * This function performs no actual function.
1078 *
1079 * May be used as the dev_select() entry in ata_port_operations.
1080 *
1081 * LOCKING:
1082 * caller.
1083 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
1085{
1086}
1087
Edward Falk0baab862005-06-02 18:17:13 -04001088
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089/**
1090 * ata_std_dev_select - Select device 0/1 on ATA bus
1091 * @ap: ATA channel to manipulate
1092 * @device: ATA device (numbered from zero) to select
1093 *
1094 * Use the method defined in the ATA specification to
1095 * make either device 0, or device 1, active on the
Edward Falk0baab862005-06-02 18:17:13 -04001096 * ATA channel. Works with both PIO and MMIO.
1097 *
1098 * May be used as the dev_select() entry in ata_port_operations.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 *
1100 * LOCKING:
1101 * caller.
1102 */
1103
1104void ata_std_dev_select (struct ata_port *ap, unsigned int device)
1105{
1106 u8 tmp;
1107
1108 if (device == 0)
1109 tmp = ATA_DEVICE_OBS;
1110 else
1111 tmp = ATA_DEVICE_OBS | ATA_DEV1;
1112
Tejun Heo0d5ff562007-02-01 15:06:36 +09001113 iowrite8(tmp, ap->ioaddr.device_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 ata_pause(ap); /* needed; also flushes, for mmio */
1115}
1116
1117/**
1118 * ata_dev_select - Select device 0/1 on ATA bus
1119 * @ap: ATA channel to manipulate
1120 * @device: ATA device (numbered from zero) to select
1121 * @wait: non-zero to wait for Status register BSY bit to clear
1122 * @can_sleep: non-zero if context allows sleeping
1123 *
1124 * Use the method defined in the ATA specification to
1125 * make either device 0, or device 1, active on the
1126 * ATA channel.
1127 *
1128 * This is a high-level version of ata_std_dev_select(),
1129 * which additionally provides the services of inserting
1130 * the proper pauses and status polling, where needed.
1131 *
1132 * LOCKING:
1133 * caller.
1134 */
1135
1136void ata_dev_select(struct ata_port *ap, unsigned int device,
1137 unsigned int wait, unsigned int can_sleep)
1138{
Tejun Heo88574552006-06-25 20:00:35 +09001139 if (ata_msg_probe(ap))
Tejun Heo44877b42007-02-21 01:06:51 +09001140 ata_port_printk(ap, KERN_INFO, "ata_dev_select: ENTER, "
1141 "device %u, wait %u\n", device, wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142
1143 if (wait)
1144 ata_wait_idle(ap);
1145
1146 ap->ops->dev_select(ap, device);
1147
1148 if (wait) {
1149 if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
1150 msleep(150);
1151 ata_wait_idle(ap);
1152 }
1153}
1154
1155/**
1156 * ata_dump_id - IDENTIFY DEVICE info debugging output
Tejun Heo0bd33002006-02-12 22:47:05 +09001157 * @id: IDENTIFY DEVICE page to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 *
Tejun Heo0bd33002006-02-12 22:47:05 +09001159 * Dump selected 16-bit words from the given IDENTIFY DEVICE
1160 * page.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 *
1162 * LOCKING:
1163 * caller.
1164 */
1165
Tejun Heo0bd33002006-02-12 22:47:05 +09001166static inline void ata_dump_id(const u16 *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167{
1168 DPRINTK("49==0x%04x "
1169 "53==0x%04x "
1170 "63==0x%04x "
1171 "64==0x%04x "
1172 "75==0x%04x \n",
Tejun Heo0bd33002006-02-12 22:47:05 +09001173 id[49],
1174 id[53],
1175 id[63],
1176 id[64],
1177 id[75]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 DPRINTK("80==0x%04x "
1179 "81==0x%04x "
1180 "82==0x%04x "
1181 "83==0x%04x "
1182 "84==0x%04x \n",
Tejun Heo0bd33002006-02-12 22:47:05 +09001183 id[80],
1184 id[81],
1185 id[82],
1186 id[83],
1187 id[84]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 DPRINTK("88==0x%04x "
1189 "93==0x%04x\n",
Tejun Heo0bd33002006-02-12 22:47:05 +09001190 id[88],
1191 id[93]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192}
1193
Tejun Heocb95d562006-03-06 04:31:56 +09001194/**
1195 * ata_id_xfermask - Compute xfermask from the given IDENTIFY data
1196 * @id: IDENTIFY data to compute xfer mask from
1197 *
1198 * Compute the xfermask for this device. This is not as trivial
1199 * as it seems if we must consider early devices correctly.
1200 *
1201 * FIXME: pre IDE drive timing (do we care ?).
1202 *
1203 * LOCKING:
1204 * None.
1205 *
1206 * RETURNS:
1207 * Computed xfermask
1208 */
1209static unsigned int ata_id_xfermask(const u16 *id)
1210{
1211 unsigned int pio_mask, mwdma_mask, udma_mask;
1212
1213 /* Usual case. Word 53 indicates word 64 is valid */
1214 if (id[ATA_ID_FIELD_VALID] & (1 << 1)) {
1215 pio_mask = id[ATA_ID_PIO_MODES] & 0x03;
1216 pio_mask <<= 3;
1217 pio_mask |= 0x7;
1218 } else {
1219 /* If word 64 isn't valid then Word 51 high byte holds
1220 * the PIO timing number for the maximum. Turn it into
1221 * a mask.
1222 */
Lennert Buytenhek7a0f1c82007-01-29 13:28:47 +01001223 u8 mode = (id[ATA_ID_OLD_PIO_MODES] >> 8) & 0xFF;
Alan Cox46767aeb2006-09-29 18:26:47 +01001224 if (mode < 5) /* Valid PIO range */
1225 pio_mask = (2 << mode) - 1;
1226 else
1227 pio_mask = 1;
Tejun Heocb95d562006-03-06 04:31:56 +09001228
1229 /* But wait.. there's more. Design your standards by
1230 * committee and you too can get a free iordy field to
1231 * process. However its the speeds not the modes that
1232 * are supported... Note drivers using the timing API
1233 * will get this right anyway
1234 */
1235 }
1236
1237 mwdma_mask = id[ATA_ID_MWDMA_MODES] & 0x07;
Tejun Heofb21f0d2006-03-12 12:34:35 +09001238
Alan Coxb352e572006-08-10 18:52:12 +01001239 if (ata_id_is_cfa(id)) {
1240 /*
1241 * Process compact flash extended modes
1242 */
1243 int pio = id[163] & 0x7;
1244 int dma = (id[163] >> 3) & 7;
1245
1246 if (pio)
1247 pio_mask |= (1 << 5);
1248 if (pio > 1)
1249 pio_mask |= (1 << 6);
1250 if (dma)
1251 mwdma_mask |= (1 << 3);
1252 if (dma > 1)
1253 mwdma_mask |= (1 << 4);
1254 }
1255
Tejun Heofb21f0d2006-03-12 12:34:35 +09001256 udma_mask = 0;
1257 if (id[ATA_ID_FIELD_VALID] & (1 << 2))
1258 udma_mask = id[ATA_ID_UDMA_MODES] & 0xff;
Tejun Heocb95d562006-03-06 04:31:56 +09001259
1260 return ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
1261}
1262
Tejun Heo86e45b62006-03-05 15:29:09 +09001263/**
1264 * ata_port_queue_task - Queue port_task
1265 * @ap: The ata_port to queue port_task for
Randy Dunlape2a7f772006-05-18 10:50:18 -07001266 * @fn: workqueue function to be scheduled
David Howells65f27f32006-11-22 14:55:48 +00001267 * @data: data for @fn to use
Randy Dunlape2a7f772006-05-18 10:50:18 -07001268 * @delay: delay time for workqueue function
Tejun Heo86e45b62006-03-05 15:29:09 +09001269 *
1270 * Schedule @fn(@data) for execution after @delay jiffies using
1271 * port_task. There is one port_task per port and it's the
1272 * user(low level driver)'s responsibility to make sure that only
1273 * one task is active at any given time.
1274 *
1275 * libata core layer takes care of synchronization between
1276 * port_task and EH. ata_port_queue_task() may be ignored for EH
1277 * synchronization.
1278 *
1279 * LOCKING:
1280 * Inherited from caller.
1281 */
David Howells65f27f32006-11-22 14:55:48 +00001282void ata_port_queue_task(struct ata_port *ap, work_func_t fn, void *data,
Tejun Heo86e45b62006-03-05 15:29:09 +09001283 unsigned long delay)
1284{
1285 int rc;
1286
Tejun Heob51e9e52006-06-29 01:29:30 +09001287 if (ap->pflags & ATA_PFLAG_FLUSH_PORT_TASK)
Tejun Heo86e45b62006-03-05 15:29:09 +09001288 return;
1289
David Howells65f27f32006-11-22 14:55:48 +00001290 PREPARE_DELAYED_WORK(&ap->port_task, fn);
1291 ap->port_task_data = data;
Tejun Heo86e45b62006-03-05 15:29:09 +09001292
David Howells52bad642006-11-22 14:54:01 +00001293 rc = queue_delayed_work(ata_wq, &ap->port_task, delay);
Tejun Heo86e45b62006-03-05 15:29:09 +09001294
1295 /* rc == 0 means that another user is using port task */
1296 WARN_ON(rc == 0);
1297}
1298
1299/**
1300 * ata_port_flush_task - Flush port_task
1301 * @ap: The ata_port to flush port_task for
1302 *
1303 * After this function completes, port_task is guranteed not to
1304 * be running or scheduled.
1305 *
1306 * LOCKING:
1307 * Kernel thread context (may sleep)
1308 */
1309void ata_port_flush_task(struct ata_port *ap)
1310{
1311 unsigned long flags;
1312
1313 DPRINTK("ENTER\n");
1314
Jeff Garzikba6a1302006-06-22 23:46:10 -04001315 spin_lock_irqsave(ap->lock, flags);
Tejun Heob51e9e52006-06-29 01:29:30 +09001316 ap->pflags |= ATA_PFLAG_FLUSH_PORT_TASK;
Jeff Garzikba6a1302006-06-22 23:46:10 -04001317 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo86e45b62006-03-05 15:29:09 +09001318
1319 DPRINTK("flush #1\n");
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07001320 cancel_work_sync(&ap->port_task.work); /* akpm: seems unneeded */
Tejun Heo86e45b62006-03-05 15:29:09 +09001321
1322 /*
1323 * At this point, if a task is running, it's guaranteed to see
1324 * the FLUSH flag; thus, it will never queue pio tasks again.
1325 * Cancel and flush.
1326 */
1327 if (!cancel_delayed_work(&ap->port_task)) {
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001328 if (ata_msg_ctl(ap))
Tejun Heo88574552006-06-25 20:00:35 +09001329 ata_port_printk(ap, KERN_DEBUG, "%s: flush #2\n",
1330 __FUNCTION__);
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07001331 cancel_work_sync(&ap->port_task.work);
Tejun Heo86e45b62006-03-05 15:29:09 +09001332 }
1333
Jeff Garzikba6a1302006-06-22 23:46:10 -04001334 spin_lock_irqsave(ap->lock, flags);
Tejun Heob51e9e52006-06-29 01:29:30 +09001335 ap->pflags &= ~ATA_PFLAG_FLUSH_PORT_TASK;
Jeff Garzikba6a1302006-06-22 23:46:10 -04001336 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo86e45b62006-03-05 15:29:09 +09001337
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001338 if (ata_msg_ctl(ap))
1339 ata_port_printk(ap, KERN_DEBUG, "%s: EXIT\n", __FUNCTION__);
Tejun Heo86e45b62006-03-05 15:29:09 +09001340}
1341
Adrian Bunk7102d232007-01-04 00:09:36 +01001342static void ata_qc_complete_internal(struct ata_queued_cmd *qc)
Tejun Heoa2a7a662005-12-13 14:48:31 +09001343{
Tejun Heo77853bf2006-01-23 13:09:36 +09001344 struct completion *waiting = qc->private_data;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001345
Tejun Heoa2a7a662005-12-13 14:48:31 +09001346 complete(waiting);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001347}
1348
1349/**
Tejun Heo24326972006-11-14 22:47:09 +09001350 * ata_exec_internal_sg - execute libata internal command
Tejun Heoa2a7a662005-12-13 14:48:31 +09001351 * @dev: Device to which the command is sent
1352 * @tf: Taskfile registers for the command and the result
Tejun Heod69cf372006-04-02 18:51:53 +09001353 * @cdb: CDB for packet command
Tejun Heoa2a7a662005-12-13 14:48:31 +09001354 * @dma_dir: Data tranfer direction of the command
Tejun Heo24326972006-11-14 22:47:09 +09001355 * @sg: sg list for the data buffer of the command
1356 * @n_elem: Number of sg entries
Tejun Heoa2a7a662005-12-13 14:48:31 +09001357 *
1358 * Executes libata internal command with timeout. @tf contains
1359 * command on entry and result on return. Timeout and error
1360 * conditions are reported via return value. No recovery action
1361 * is taken after a command times out. It's caller's duty to
1362 * clean up after timeout.
1363 *
1364 * LOCKING:
1365 * None. Should be called with kernel context, might sleep.
Tejun Heo551e8882006-06-12 14:09:49 +09001366 *
1367 * RETURNS:
1368 * Zero on success, AC_ERR_* mask on failure
Tejun Heoa2a7a662005-12-13 14:48:31 +09001369 */
Tejun Heo24326972006-11-14 22:47:09 +09001370unsigned ata_exec_internal_sg(struct ata_device *dev,
1371 struct ata_taskfile *tf, const u8 *cdb,
1372 int dma_dir, struct scatterlist *sg,
1373 unsigned int n_elem)
Tejun Heoa2a7a662005-12-13 14:48:31 +09001374{
Tejun Heo3373efd2006-05-15 20:57:53 +09001375 struct ata_port *ap = dev->ap;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001376 u8 command = tf->command;
1377 struct ata_queued_cmd *qc;
Tejun Heo2ab7db12006-05-15 20:58:02 +09001378 unsigned int tag, preempted_tag;
Tejun Heodedaf2b2006-05-15 21:03:43 +09001379 u32 preempted_sactive, preempted_qc_active;
Ingo Molnar60be6b92006-07-03 00:25:26 -07001380 DECLARE_COMPLETION_ONSTACK(wait);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001381 unsigned long flags;
Tejun Heo77853bf2006-01-23 13:09:36 +09001382 unsigned int err_mask;
Tejun Heod95a7172006-05-15 20:58:14 +09001383 int rc;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001384
Jeff Garzikba6a1302006-06-22 23:46:10 -04001385 spin_lock_irqsave(ap->lock, flags);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001386
Tejun Heoe3180492006-05-15 20:58:09 +09001387 /* no internal command while frozen */
Tejun Heob51e9e52006-06-29 01:29:30 +09001388 if (ap->pflags & ATA_PFLAG_FROZEN) {
Jeff Garzikba6a1302006-06-22 23:46:10 -04001389 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +09001390 return AC_ERR_SYSTEM;
1391 }
1392
Tejun Heo2ab7db12006-05-15 20:58:02 +09001393 /* initialize internal qc */
Tejun Heoa2a7a662005-12-13 14:48:31 +09001394
Tejun Heo2ab7db12006-05-15 20:58:02 +09001395 /* XXX: Tag 0 is used for drivers with legacy EH as some
1396 * drivers choke if any other tag is given. This breaks
1397 * ata_tag_internal() test for those drivers. Don't use new
1398 * EH stuff without converting to it.
1399 */
1400 if (ap->ops->error_handler)
1401 tag = ATA_TAG_INTERNAL;
1402 else
1403 tag = 0;
1404
Tejun Heo6cec4a32006-05-15 21:03:41 +09001405 if (test_and_set_bit(tag, &ap->qc_allocated))
Tejun Heo2ab7db12006-05-15 20:58:02 +09001406 BUG();
Tejun Heof69499f2006-05-15 20:58:03 +09001407 qc = __ata_qc_from_tag(ap, tag);
Tejun Heo2ab7db12006-05-15 20:58:02 +09001408
1409 qc->tag = tag;
1410 qc->scsicmd = NULL;
1411 qc->ap = ap;
1412 qc->dev = dev;
1413 ata_qc_reinit(qc);
1414
1415 preempted_tag = ap->active_tag;
Tejun Heodedaf2b2006-05-15 21:03:43 +09001416 preempted_sactive = ap->sactive;
1417 preempted_qc_active = ap->qc_active;
Tejun Heo2ab7db12006-05-15 20:58:02 +09001418 ap->active_tag = ATA_TAG_POISON;
Tejun Heodedaf2b2006-05-15 21:03:43 +09001419 ap->sactive = 0;
1420 ap->qc_active = 0;
Tejun Heo2ab7db12006-05-15 20:58:02 +09001421
1422 /* prepare & issue qc */
Tejun Heoa2a7a662005-12-13 14:48:31 +09001423 qc->tf = *tf;
Tejun Heod69cf372006-04-02 18:51:53 +09001424 if (cdb)
1425 memcpy(qc->cdb, cdb, ATAPI_CDB_LEN);
Tejun Heoe61e0672006-05-15 20:57:40 +09001426 qc->flags |= ATA_QCFLAG_RESULT_TF;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001427 qc->dma_dir = dma_dir;
1428 if (dma_dir != DMA_NONE) {
Tejun Heo24326972006-11-14 22:47:09 +09001429 unsigned int i, buflen = 0;
1430
1431 for (i = 0; i < n_elem; i++)
1432 buflen += sg[i].length;
1433
1434 ata_sg_init(qc, sg, n_elem);
Brian King49c80422007-01-30 11:32:26 -06001435 qc->nbytes = buflen;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001436 }
1437
Tejun Heo77853bf2006-01-23 13:09:36 +09001438 qc->private_data = &wait;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001439 qc->complete_fn = ata_qc_complete_internal;
1440
Tejun Heo8e0e6942006-03-31 20:41:11 +09001441 ata_qc_issue(qc);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001442
Jeff Garzikba6a1302006-06-22 23:46:10 -04001443 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001444
Andrew Mortona8601e52006-06-25 01:36:52 -07001445 rc = wait_for_completion_timeout(&wait, ata_probe_timeout);
Albert Lee41ade502006-03-14 11:19:04 +08001446
Tejun Heod95a7172006-05-15 20:58:14 +09001447 ata_port_flush_task(ap);
1448
1449 if (!rc) {
Jeff Garzikba6a1302006-06-22 23:46:10 -04001450 spin_lock_irqsave(ap->lock, flags);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001451
1452 /* We're racing with irq here. If we lose, the
1453 * following test prevents us from completing the qc
Tejun Heod95a7172006-05-15 20:58:14 +09001454 * twice. If we win, the port is frozen and will be
1455 * cleaned up by ->post_internal_cmd().
Tejun Heoa2a7a662005-12-13 14:48:31 +09001456 */
Tejun Heo77853bf2006-01-23 13:09:36 +09001457 if (qc->flags & ATA_QCFLAG_ACTIVE) {
Tejun Heod95a7172006-05-15 20:58:14 +09001458 qc->err_mask |= AC_ERR_TIMEOUT;
1459
1460 if (ap->ops->error_handler)
1461 ata_port_freeze(ap);
1462 else
1463 ata_qc_complete(qc);
Tejun Heof15a1da2006-05-15 20:57:56 +09001464
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001465 if (ata_msg_warn(ap))
1466 ata_dev_printk(dev, KERN_WARNING,
Tejun Heo88574552006-06-25 20:00:35 +09001467 "qc timeout (cmd 0x%x)\n", command);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001468 }
1469
Jeff Garzikba6a1302006-06-22 23:46:10 -04001470 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001471 }
1472
Tejun Heod95a7172006-05-15 20:58:14 +09001473 /* do post_internal_cmd */
1474 if (ap->ops->post_internal_cmd)
1475 ap->ops->post_internal_cmd(qc);
1476
Tejun Heoa51d6442007-03-20 15:24:11 +09001477 /* perform minimal error analysis */
1478 if (qc->flags & ATA_QCFLAG_FAILED) {
1479 if (qc->result_tf.command & (ATA_ERR | ATA_DF))
1480 qc->err_mask |= AC_ERR_DEV;
1481
1482 if (!qc->err_mask)
1483 qc->err_mask |= AC_ERR_OTHER;
1484
1485 if (qc->err_mask & ~AC_ERR_OTHER)
1486 qc->err_mask &= ~AC_ERR_OTHER;
Tejun Heod95a7172006-05-15 20:58:14 +09001487 }
1488
Tejun Heo15869302006-05-15 20:57:33 +09001489 /* finish up */
Jeff Garzikba6a1302006-06-22 23:46:10 -04001490 spin_lock_irqsave(ap->lock, flags);
Tejun Heo15869302006-05-15 20:57:33 +09001491
Tejun Heoe61e0672006-05-15 20:57:40 +09001492 *tf = qc->result_tf;
Tejun Heo77853bf2006-01-23 13:09:36 +09001493 err_mask = qc->err_mask;
1494
1495 ata_qc_free(qc);
Tejun Heo2ab7db12006-05-15 20:58:02 +09001496 ap->active_tag = preempted_tag;
Tejun Heodedaf2b2006-05-15 21:03:43 +09001497 ap->sactive = preempted_sactive;
1498 ap->qc_active = preempted_qc_active;
Tejun Heo77853bf2006-01-23 13:09:36 +09001499
Tejun Heo1f7dd3e92006-03-24 15:25:30 +09001500 /* XXX - Some LLDDs (sata_mv) disable port on command failure.
1501 * Until those drivers are fixed, we detect the condition
1502 * here, fail the command with AC_ERR_SYSTEM and reenable the
1503 * port.
1504 *
1505 * Note that this doesn't change any behavior as internal
1506 * command failure results in disabling the device in the
1507 * higher layer for LLDDs without new reset/EH callbacks.
1508 *
1509 * Kill the following code as soon as those drivers are fixed.
1510 */
Tejun Heo198e0fe2006-04-02 18:51:52 +09001511 if (ap->flags & ATA_FLAG_DISABLED) {
Tejun Heo1f7dd3e92006-03-24 15:25:30 +09001512 err_mask |= AC_ERR_SYSTEM;
1513 ata_port_probe(ap);
1514 }
1515
Jeff Garzikba6a1302006-06-22 23:46:10 -04001516 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo15869302006-05-15 20:57:33 +09001517
Tejun Heo77853bf2006-01-23 13:09:36 +09001518 return err_mask;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001519}
1520
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521/**
Tejun Heo33480a02006-12-12 02:15:31 +09001522 * ata_exec_internal - execute libata internal command
Tejun Heo24326972006-11-14 22:47:09 +09001523 * @dev: Device to which the command is sent
1524 * @tf: Taskfile registers for the command and the result
1525 * @cdb: CDB for packet command
1526 * @dma_dir: Data tranfer direction of the command
1527 * @buf: Data buffer of the command
1528 * @buflen: Length of data buffer
1529 *
1530 * Wrapper around ata_exec_internal_sg() which takes simple
1531 * buffer instead of sg list.
1532 *
1533 * LOCKING:
1534 * None. Should be called with kernel context, might sleep.
1535 *
1536 * RETURNS:
1537 * Zero on success, AC_ERR_* mask on failure
1538 */
1539unsigned ata_exec_internal(struct ata_device *dev,
1540 struct ata_taskfile *tf, const u8 *cdb,
1541 int dma_dir, void *buf, unsigned int buflen)
1542{
Tejun Heo33480a02006-12-12 02:15:31 +09001543 struct scatterlist *psg = NULL, sg;
1544 unsigned int n_elem = 0;
Tejun Heo24326972006-11-14 22:47:09 +09001545
Tejun Heo33480a02006-12-12 02:15:31 +09001546 if (dma_dir != DMA_NONE) {
1547 WARN_ON(!buf);
1548 sg_init_one(&sg, buf, buflen);
1549 psg = &sg;
1550 n_elem++;
1551 }
Tejun Heo24326972006-11-14 22:47:09 +09001552
Tejun Heo33480a02006-12-12 02:15:31 +09001553 return ata_exec_internal_sg(dev, tf, cdb, dma_dir, psg, n_elem);
Tejun Heo24326972006-11-14 22:47:09 +09001554}
1555
1556/**
Tejun Heo977e6b92006-06-24 20:30:19 +09001557 * ata_do_simple_cmd - execute simple internal command
1558 * @dev: Device to which the command is sent
1559 * @cmd: Opcode to execute
1560 *
1561 * Execute a 'simple' command, that only consists of the opcode
1562 * 'cmd' itself, without filling any other registers
1563 *
1564 * LOCKING:
1565 * Kernel thread context (may sleep).
1566 *
1567 * RETURNS:
1568 * Zero on success, AC_ERR_* mask on failure
Tejun Heoe58eb582006-06-24 20:30:19 +09001569 */
Tejun Heo77b08fb2006-06-24 20:30:19 +09001570unsigned int ata_do_simple_cmd(struct ata_device *dev, u8 cmd)
Tejun Heoe58eb582006-06-24 20:30:19 +09001571{
1572 struct ata_taskfile tf;
Tejun Heoe58eb582006-06-24 20:30:19 +09001573
1574 ata_tf_init(dev, &tf);
1575
1576 tf.command = cmd;
1577 tf.flags |= ATA_TFLAG_DEVICE;
1578 tf.protocol = ATA_PROT_NODATA;
1579
Tejun Heo977e6b92006-06-24 20:30:19 +09001580 return ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
Tejun Heoe58eb582006-06-24 20:30:19 +09001581}
1582
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583/**
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001584 * ata_pio_need_iordy - check if iordy needed
1585 * @adev: ATA device
1586 *
1587 * Check if the current speed of the device requires IORDY. Used
1588 * by various controllers for chip configuration.
1589 */
Jeff Garzika617c092007-05-21 20:14:23 -04001590
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001591unsigned int ata_pio_need_iordy(const struct ata_device *adev)
1592{
Alan Cox432729f2007-03-08 23:22:59 +00001593 /* Controller doesn't support IORDY. Probably a pointless check
1594 as the caller should know this */
1595 if (adev->ap->flags & ATA_FLAG_NO_IORDY)
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001596 return 0;
Alan Cox432729f2007-03-08 23:22:59 +00001597 /* PIO3 and higher it is mandatory */
1598 if (adev->pio_mode > XFER_PIO_2)
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001599 return 1;
Alan Cox432729f2007-03-08 23:22:59 +00001600 /* We turn it on when possible */
1601 if (ata_id_has_iordy(adev->id))
1602 return 1;
1603 return 0;
1604}
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001605
Alan Cox432729f2007-03-08 23:22:59 +00001606/**
1607 * ata_pio_mask_no_iordy - Return the non IORDY mask
1608 * @adev: ATA device
1609 *
1610 * Compute the highest mode possible if we are not using iordy. Return
1611 * -1 if no iordy mode is available.
1612 */
Jeff Garzika617c092007-05-21 20:14:23 -04001613
Alan Cox432729f2007-03-08 23:22:59 +00001614static u32 ata_pio_mask_no_iordy(const struct ata_device *adev)
1615{
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001616 /* If we have no drive specific rule, then PIO 2 is non IORDY */
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001617 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
Alan Cox432729f2007-03-08 23:22:59 +00001618 u16 pio = adev->id[ATA_ID_EIDE_PIO];
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001619 /* Is the speed faster than the drive allows non IORDY ? */
1620 if (pio) {
1621 /* This is cycle times not frequency - watch the logic! */
1622 if (pio > 240) /* PIO2 is 240nS per cycle */
Alan Cox432729f2007-03-08 23:22:59 +00001623 return 3 << ATA_SHIFT_PIO;
1624 return 7 << ATA_SHIFT_PIO;
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001625 }
1626 }
Alan Cox432729f2007-03-08 23:22:59 +00001627 return 3 << ATA_SHIFT_PIO;
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001628}
1629
1630/**
Tejun Heo49016ac2006-02-21 02:12:11 +09001631 * ata_dev_read_id - Read ID data from the specified device
Tejun Heo49016ac2006-02-21 02:12:11 +09001632 * @dev: target device
1633 * @p_class: pointer to class of the target device (may be changed)
Tejun Heobff04642006-11-10 18:08:10 +09001634 * @flags: ATA_READID_* flags
Tejun Heofe635c72006-05-15 20:57:35 +09001635 * @id: buffer to read IDENTIFY data into
Tejun Heo49016ac2006-02-21 02:12:11 +09001636 *
1637 * Read ID data from the specified device. ATA_CMD_ID_ATA is
1638 * performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
Tejun Heoaec5c3c2006-03-25 01:33:34 +09001639 * devices. This function also issues ATA_CMD_INIT_DEV_PARAMS
1640 * for pre-ATA4 drives.
Tejun Heo49016ac2006-02-21 02:12:11 +09001641 *
1642 * LOCKING:
1643 * Kernel thread context (may sleep)
1644 *
1645 * RETURNS:
1646 * 0 on success, -errno otherwise.
1647 */
Tejun Heoa9beec92006-05-31 18:27:44 +09001648int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
Tejun Heobff04642006-11-10 18:08:10 +09001649 unsigned int flags, u16 *id)
Tejun Heo49016ac2006-02-21 02:12:11 +09001650{
Tejun Heo3373efd2006-05-15 20:57:53 +09001651 struct ata_port *ap = dev->ap;
Tejun Heo49016ac2006-02-21 02:12:11 +09001652 unsigned int class = *p_class;
Tejun Heo49016ac2006-02-21 02:12:11 +09001653 struct ata_taskfile tf;
1654 unsigned int err_mask = 0;
1655 const char *reason;
Tejun Heo54936f82007-05-11 14:35:29 +02001656 int may_fallback = 1, tried_spinup = 0;
Tejun Heo49016ac2006-02-21 02:12:11 +09001657 int rc;
1658
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001659 if (ata_msg_ctl(ap))
Tejun Heo44877b42007-02-21 01:06:51 +09001660 ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER\n", __FUNCTION__);
Tejun Heo49016ac2006-02-21 02:12:11 +09001661
Tejun Heo49016ac2006-02-21 02:12:11 +09001662 ata_dev_select(ap, dev->devno, 1, 1); /* select device 0/1 */
Tejun Heo49016ac2006-02-21 02:12:11 +09001663 retry:
Tejun Heo3373efd2006-05-15 20:57:53 +09001664 ata_tf_init(dev, &tf);
Tejun Heo49016ac2006-02-21 02:12:11 +09001665
1666 switch (class) {
1667 case ATA_DEV_ATA:
1668 tf.command = ATA_CMD_ID_ATA;
1669 break;
1670 case ATA_DEV_ATAPI:
1671 tf.command = ATA_CMD_ID_ATAPI;
1672 break;
1673 default:
1674 rc = -ENODEV;
1675 reason = "unsupported class";
1676 goto err_out;
1677 }
1678
1679 tf.protocol = ATA_PROT_PIO;
Tejun Heo81afe892007-02-07 12:37:41 -08001680
1681 /* Some devices choke if TF registers contain garbage. Make
1682 * sure those are properly initialized.
1683 */
1684 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1685
1686 /* Device presence detection is unreliable on some
1687 * controllers. Always poll IDENTIFY if available.
1688 */
1689 tf.flags |= ATA_TFLAG_POLLING;
Tejun Heo49016ac2006-02-21 02:12:11 +09001690
Tejun Heo3373efd2006-05-15 20:57:53 +09001691 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
Tejun Heo49016ac2006-02-21 02:12:11 +09001692 id, sizeof(id[0]) * ATA_ID_WORDS);
Tejun Heo49016ac2006-02-21 02:12:11 +09001693 if (err_mask) {
Tejun Heo800b3992006-12-03 21:34:13 +09001694 if (err_mask & AC_ERR_NODEV_HINT) {
Tejun Heo55a8e2c2006-11-10 18:08:10 +09001695 DPRINTK("ata%u.%d: NODEV after polling detection\n",
Tejun Heo44877b42007-02-21 01:06:51 +09001696 ap->print_id, dev->devno);
Tejun Heo55a8e2c2006-11-10 18:08:10 +09001697 return -ENOENT;
1698 }
1699
Tejun Heo54936f82007-05-11 14:35:29 +02001700 /* Device or controller might have reported the wrong
1701 * device class. Give a shot at the other IDENTIFY if
1702 * the current one is aborted by the device.
1703 */
1704 if (may_fallback &&
1705 (err_mask == AC_ERR_DEV) && (tf.feature & ATA_ABORTED)) {
1706 may_fallback = 0;
1707
1708 if (class == ATA_DEV_ATA)
1709 class = ATA_DEV_ATAPI;
1710 else
1711 class = ATA_DEV_ATA;
1712 goto retry;
1713 }
1714
Tejun Heo49016ac2006-02-21 02:12:11 +09001715 rc = -EIO;
1716 reason = "I/O error";
Tejun Heo49016ac2006-02-21 02:12:11 +09001717 goto err_out;
1718 }
1719
Tejun Heo54936f82007-05-11 14:35:29 +02001720 /* Falling back doesn't make sense if ID data was read
1721 * successfully at least once.
1722 */
1723 may_fallback = 0;
1724
Tejun Heo49016ac2006-02-21 02:12:11 +09001725 swap_buf_le16(id, ATA_ID_WORDS);
1726
Tejun Heo49016ac2006-02-21 02:12:11 +09001727 /* sanity check */
Tejun Heoa4f57492006-09-12 20:35:49 -07001728 rc = -EINVAL;
Alan Cox60700682007-06-07 16:13:55 +01001729 reason = "device reports invalid type";
Tejun Heoa4f57492006-09-12 20:35:49 -07001730
1731 if (class == ATA_DEV_ATA) {
1732 if (!ata_id_is_ata(id) && !ata_id_is_cfa(id))
1733 goto err_out;
1734 } else {
1735 if (ata_id_is_ata(id))
1736 goto err_out;
Tejun Heo49016ac2006-02-21 02:12:11 +09001737 }
1738
Mark Lord169439c2007-04-17 18:26:07 -04001739 if (!tried_spinup && (id[2] == 0x37c8 || id[2] == 0x738c)) {
1740 tried_spinup = 1;
1741 /*
1742 * Drive powered-up in standby mode, and requires a specific
1743 * SET_FEATURES spin-up subcommand before it will accept
1744 * anything other than the original IDENTIFY command.
1745 */
1746 ata_tf_init(dev, &tf);
1747 tf.command = ATA_CMD_SET_FEATURES;
1748 tf.feature = SETFEATURES_SPINUP;
1749 tf.protocol = ATA_PROT_NODATA;
1750 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1751 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
1752 if (err_mask) {
1753 rc = -EIO;
1754 reason = "SPINUP failed";
1755 goto err_out;
1756 }
1757 /*
1758 * If the drive initially returned incomplete IDENTIFY info,
1759 * we now must reissue the IDENTIFY command.
1760 */
1761 if (id[2] == 0x37c8)
1762 goto retry;
1763 }
1764
Tejun Heobff04642006-11-10 18:08:10 +09001765 if ((flags & ATA_READID_POSTRESET) && class == ATA_DEV_ATA) {
Tejun Heo49016ac2006-02-21 02:12:11 +09001766 /*
1767 * The exact sequence expected by certain pre-ATA4 drives is:
1768 * SRST RESET
1769 * IDENTIFY
1770 * INITIALIZE DEVICE PARAMETERS
1771 * anything else..
1772 * Some drives were very specific about that exact sequence.
1773 */
1774 if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
Tejun Heo3373efd2006-05-15 20:57:53 +09001775 err_mask = ata_dev_init_params(dev, id[3], id[6]);
Tejun Heo49016ac2006-02-21 02:12:11 +09001776 if (err_mask) {
1777 rc = -EIO;
1778 reason = "INIT_DEV_PARAMS failed";
1779 goto err_out;
1780 }
1781
1782 /* current CHS translation info (id[53-58]) might be
1783 * changed. reread the identify device info.
1784 */
Tejun Heobff04642006-11-10 18:08:10 +09001785 flags &= ~ATA_READID_POSTRESET;
Tejun Heo49016ac2006-02-21 02:12:11 +09001786 goto retry;
1787 }
1788 }
1789
1790 *p_class = class;
Tejun Heofe635c72006-05-15 20:57:35 +09001791
Tejun Heo49016ac2006-02-21 02:12:11 +09001792 return 0;
1793
1794 err_out:
Tejun Heo88574552006-06-25 20:00:35 +09001795 if (ata_msg_warn(ap))
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001796 ata_dev_printk(dev, KERN_WARNING, "failed to IDENTIFY "
Tejun Heo88574552006-06-25 20:00:35 +09001797 "(%s, err_mask=0x%x)\n", reason, err_mask);
Tejun Heo49016ac2006-02-21 02:12:11 +09001798 return rc;
1799}
1800
Tejun Heo3373efd2006-05-15 20:57:53 +09001801static inline u8 ata_dev_knobble(struct ata_device *dev)
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09001802{
Tejun Heo3373efd2006-05-15 20:57:53 +09001803 return ((dev->ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09001804}
1805
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001806static void ata_dev_config_ncq(struct ata_device *dev,
1807 char *desc, size_t desc_sz)
1808{
1809 struct ata_port *ap = dev->ap;
1810 int hdepth = 0, ddepth = ata_id_queue_depth(dev->id);
1811
1812 if (!ata_id_has_ncq(dev->id)) {
1813 desc[0] = '\0';
1814 return;
1815 }
Alan Cox6919a0a2006-10-27 19:08:46 -07001816 if (ata_device_blacklisted(dev) & ATA_HORKAGE_NONCQ) {
1817 snprintf(desc, desc_sz, "NCQ (not used)");
1818 return;
1819 }
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001820 if (ap->flags & ATA_FLAG_NCQ) {
Jeff Garzikcca39742006-08-24 03:19:22 -04001821 hdepth = min(ap->scsi_host->can_queue, ATA_MAX_QUEUE - 1);
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001822 dev->flags |= ATA_DFLAG_NCQ;
1823 }
1824
1825 if (hdepth >= ddepth)
1826 snprintf(desc, desc_sz, "NCQ (depth %d)", ddepth);
1827 else
1828 snprintf(desc, desc_sz, "NCQ (depth %d/%d)", hdepth, ddepth);
1829}
1830
Tejun Heo49016ac2006-02-21 02:12:11 +09001831/**
Tejun Heoffeae412006-03-01 16:09:35 +09001832 * ata_dev_configure - Configure the specified ATA/ATAPI device
Tejun Heoffeae412006-03-01 16:09:35 +09001833 * @dev: Target device to configure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 *
Tejun Heoffeae412006-03-01 16:09:35 +09001835 * Configure @dev according to @dev->id. Generic and low-level
1836 * driver specific fixups are also applied.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 *
1838 * LOCKING:
Tejun Heoffeae412006-03-01 16:09:35 +09001839 * Kernel thread context (may sleep)
1840 *
1841 * RETURNS:
1842 * 0 on success, -errno otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 */
Tejun Heoefdaedc2006-11-01 18:38:52 +09001844int ata_dev_configure(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845{
Tejun Heo3373efd2006-05-15 20:57:53 +09001846 struct ata_port *ap = dev->ap;
Tejun Heoefdaedc2006-11-01 18:38:52 +09001847 int print_info = ap->eh_context.i.flags & ATA_EHI_PRINTINFO;
Tejun Heo1148c3a2006-03-13 19:48:04 +09001848 const u16 *id = dev->id;
Tejun Heoff8854b2006-03-06 04:31:56 +09001849 unsigned int xfer_mask;
Alan Coxb352e572006-08-10 18:52:12 +01001850 char revbuf[7]; /* XYZ-99\0 */
Eric D. Mudama3f64f562007-01-30 23:00:40 -07001851 char fwrevbuf[ATA_ID_FW_REV_LEN+1];
1852 char modelbuf[ATA_ID_PROD_LEN+1];
Brian Kinge6d902a2006-06-28 08:30:31 -05001853 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001855 if (!ata_dev_enabled(dev) && ata_msg_info(ap)) {
Tejun Heo44877b42007-02-21 01:06:51 +09001856 ata_dev_printk(dev, KERN_INFO, "%s: ENTER/EXIT -- nodev\n",
1857 __FUNCTION__);
Tejun Heoffeae412006-03-01 16:09:35 +09001858 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 }
1860
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001861 if (ata_msg_probe(ap))
Tejun Heo44877b42007-02-21 01:06:51 +09001862 ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863
Kristen Carlson Accardi08573a82006-11-10 16:14:47 -08001864 /* set _SDD */
Tejun Heo3a32a8e2007-05-05 23:50:38 +09001865 rc = ata_acpi_push_id(dev);
Kristen Carlson Accardi08573a82006-11-10 16:14:47 -08001866 if (rc) {
1867 ata_dev_printk(dev, KERN_WARNING, "failed to set _SDD(%d)\n",
1868 rc);
1869 }
1870
1871 /* retrieve and execute the ATA task file of _GTF */
1872 ata_acpi_exec_tfs(ap);
1873
Tejun Heoc39f5eb2006-03-13 19:51:19 +09001874 /* print device capabilities */
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001875 if (ata_msg_probe(ap))
Tejun Heo88574552006-06-25 20:00:35 +09001876 ata_dev_printk(dev, KERN_DEBUG,
1877 "%s: cfg 49:%04x 82:%04x 83:%04x 84:%04x "
1878 "85:%04x 86:%04x 87:%04x 88:%04x\n",
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001879 __FUNCTION__,
Tejun Heof15a1da2006-05-15 20:57:56 +09001880 id[49], id[82], id[83], id[84],
1881 id[85], id[86], id[87], id[88]);
Tejun Heoc39f5eb2006-03-13 19:51:19 +09001882
Tejun Heo208a9932006-03-05 17:55:58 +09001883 /* initialize to-be-configured parameters */
Tejun Heoea1dd4e2006-04-02 18:51:53 +09001884 dev->flags &= ~ATA_DFLAG_CFG_MASK;
Tejun Heo208a9932006-03-05 17:55:58 +09001885 dev->max_sectors = 0;
1886 dev->cdb_len = 0;
1887 dev->n_sectors = 0;
1888 dev->cylinders = 0;
1889 dev->heads = 0;
1890 dev->sectors = 0;
1891
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 /*
1893 * common ATA, ATAPI feature tests
1894 */
1895
Tejun Heoff8854b2006-03-06 04:31:56 +09001896 /* find max transfer mode; for printk only */
Tejun Heo1148c3a2006-03-13 19:48:04 +09001897 xfer_mask = ata_id_xfermask(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001899 if (ata_msg_probe(ap))
1900 ata_dump_id(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901
Albert Leeef143d52007-06-05 13:01:33 +08001902 /* SCSI only uses 4-char revisions, dump full 8 chars from ATA */
1903 ata_id_c_string(dev->id, fwrevbuf, ATA_ID_FW_REV,
1904 sizeof(fwrevbuf));
1905
1906 ata_id_c_string(dev->id, modelbuf, ATA_ID_PROD,
1907 sizeof(modelbuf));
1908
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 /* ATA-specific feature tests */
1910 if (dev->class == ATA_DEV_ATA) {
Alan Coxb352e572006-08-10 18:52:12 +01001911 if (ata_id_is_cfa(id)) {
1912 if (id[162] & 1) /* CPRM may make this media unusable */
Tejun Heo44877b42007-02-21 01:06:51 +09001913 ata_dev_printk(dev, KERN_WARNING,
1914 "supports DRM functions and may "
1915 "not be fully accessable.\n");
Alan Coxb352e572006-08-10 18:52:12 +01001916 snprintf(revbuf, 7, "CFA");
1917 }
1918 else
1919 snprintf(revbuf, 7, "ATA-%d", ata_id_major_version(id));
1920
Tejun Heo1148c3a2006-03-13 19:48:04 +09001921 dev->n_sectors = ata_id_n_sectors(id);
Tejun Heo29407402006-02-12 22:47:04 +09001922
Eric D. Mudama3f64f562007-01-30 23:00:40 -07001923 if (dev->id[59] & 0x100)
1924 dev->multi_count = dev->id[59] & 0xff;
1925
Tejun Heo1148c3a2006-03-13 19:48:04 +09001926 if (ata_id_has_lba(id)) {
Tejun Heo4c2d7212006-03-05 17:55:58 +09001927 const char *lba_desc;
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001928 char ncq_desc[20];
Albert Lee8bf62ece2005-05-12 15:29:42 -04001929
Tejun Heo4c2d7212006-03-05 17:55:58 +09001930 lba_desc = "LBA";
1931 dev->flags |= ATA_DFLAG_LBA;
Tejun Heo1148c3a2006-03-13 19:48:04 +09001932 if (ata_id_has_lba48(id)) {
Albert Lee8bf62ece2005-05-12 15:29:42 -04001933 dev->flags |= ATA_DFLAG_LBA48;
Tejun Heo4c2d7212006-03-05 17:55:58 +09001934 lba_desc = "LBA48";
Tejun Heo6fc49ad2006-11-11 20:10:45 +09001935
1936 if (dev->n_sectors >= (1UL << 28) &&
1937 ata_id_has_flush_ext(id))
1938 dev->flags |= ATA_DFLAG_FLUSH_EXT;
Tejun Heo4c2d7212006-03-05 17:55:58 +09001939 }
Albert Lee8bf62ece2005-05-12 15:29:42 -04001940
Alan Cox1e999732007-04-11 00:23:13 +01001941 if (ata_id_hpa_enabled(dev->id))
1942 dev->n_sectors = ata_hpa_resize(dev);
1943
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001944 /* config NCQ */
1945 ata_dev_config_ncq(dev, ncq_desc, sizeof(ncq_desc));
1946
Albert Lee8bf62ece2005-05-12 15:29:42 -04001947 /* print device info to dmesg */
Eric D. Mudama3f64f562007-01-30 23:00:40 -07001948 if (ata_msg_drv(ap) && print_info) {
1949 ata_dev_printk(dev, KERN_INFO,
1950 "%s: %s, %s, max %s\n",
1951 revbuf, modelbuf, fwrevbuf,
1952 ata_mode_string(xfer_mask));
1953 ata_dev_printk(dev, KERN_INFO,
1954 "%Lu sectors, multi %u: %s %s\n",
Tejun Heof15a1da2006-05-15 20:57:56 +09001955 (unsigned long long)dev->n_sectors,
Eric D. Mudama3f64f562007-01-30 23:00:40 -07001956 dev->multi_count, lba_desc, ncq_desc);
1957 }
Tejun Heoffeae412006-03-01 16:09:35 +09001958 } else {
Albert Lee8bf62ece2005-05-12 15:29:42 -04001959 /* CHS */
1960
1961 /* Default translation */
Tejun Heo1148c3a2006-03-13 19:48:04 +09001962 dev->cylinders = id[1];
1963 dev->heads = id[3];
1964 dev->sectors = id[6];
Albert Lee8bf62ece2005-05-12 15:29:42 -04001965
Tejun Heo1148c3a2006-03-13 19:48:04 +09001966 if (ata_id_current_chs_valid(id)) {
Albert Lee8bf62ece2005-05-12 15:29:42 -04001967 /* Current CHS translation is valid. */
Tejun Heo1148c3a2006-03-13 19:48:04 +09001968 dev->cylinders = id[54];
1969 dev->heads = id[55];
1970 dev->sectors = id[56];
Albert Lee8bf62ece2005-05-12 15:29:42 -04001971 }
1972
1973 /* print device info to dmesg */
Eric D. Mudama3f64f562007-01-30 23:00:40 -07001974 if (ata_msg_drv(ap) && print_info) {
Tejun Heo88574552006-06-25 20:00:35 +09001975 ata_dev_printk(dev, KERN_INFO,
Eric D. Mudama3f64f562007-01-30 23:00:40 -07001976 "%s: %s, %s, max %s\n",
1977 revbuf, modelbuf, fwrevbuf,
1978 ata_mode_string(xfer_mask));
Jeff Garzika84471f2007-02-26 05:51:33 -05001979 ata_dev_printk(dev, KERN_INFO,
Eric D. Mudama3f64f562007-01-30 23:00:40 -07001980 "%Lu sectors, multi %u, CHS %u/%u/%u\n",
1981 (unsigned long long)dev->n_sectors,
1982 dev->multi_count, dev->cylinders,
1983 dev->heads, dev->sectors);
1984 }
Albert Lee07f6f7d2005-11-01 19:33:20 +08001985 }
1986
Tejun Heo6e7846e2006-02-12 23:32:58 +09001987 dev->cdb_len = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 }
1989
1990 /* ATAPI-specific feature tests */
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05001991 else if (dev->class == ATA_DEV_ATAPI) {
Albert Lee08a556d2006-03-31 13:29:04 +08001992 char *cdb_intr_string = "";
1993
Tejun Heo1148c3a2006-03-13 19:48:04 +09001994 rc = atapi_cdb_len(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001996 if (ata_msg_warn(ap))
Tejun Heo88574552006-06-25 20:00:35 +09001997 ata_dev_printk(dev, KERN_WARNING,
1998 "unsupported CDB len\n");
Tejun Heoffeae412006-03-01 16:09:35 +09001999 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 goto err_out_nosup;
2001 }
Tejun Heo6e7846e2006-02-12 23:32:58 +09002002 dev->cdb_len = (unsigned int) rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003
Albert Lee08a556d2006-03-31 13:29:04 +08002004 if (ata_id_cdb_intr(dev->id)) {
Albert Lee312f7da2005-09-27 17:38:03 +08002005 dev->flags |= ATA_DFLAG_CDB_INTR;
Albert Lee08a556d2006-03-31 13:29:04 +08002006 cdb_intr_string = ", CDB intr";
2007 }
Albert Lee312f7da2005-09-27 17:38:03 +08002008
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 /* print device info to dmesg */
Borislav Petkov5afc8142006-06-27 14:51:25 +02002010 if (ata_msg_drv(ap) && print_info)
Albert Leeef143d52007-06-05 13:01:33 +08002011 ata_dev_printk(dev, KERN_INFO,
2012 "ATAPI: %s, %s, max %s%s\n",
2013 modelbuf, fwrevbuf,
Tejun Heo12436c32006-05-15 20:59:15 +09002014 ata_mode_string(xfer_mask),
2015 cdb_intr_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 }
2017
Tejun Heo914ed352006-11-01 18:39:55 +09002018 /* determine max_sectors */
2019 dev->max_sectors = ATA_MAX_SECTORS;
2020 if (dev->flags & ATA_DFLAG_LBA48)
2021 dev->max_sectors = ATA_MAX_SECTORS_LBA48;
2022
Alan Cox93590852006-09-12 16:55:12 +01002023 if (dev->horkage & ATA_HORKAGE_DIAGNOSTIC) {
2024 /* Let the user know. We don't want to disallow opens for
2025 rescue purposes, or in case the vendor is just a blithering
2026 idiot */
2027 if (print_info) {
2028 ata_dev_printk(dev, KERN_WARNING,
2029"Drive reports diagnostics failure. This may indicate a drive\n");
2030 ata_dev_printk(dev, KERN_WARNING,
2031"fault or invalid emulation. Contact drive vendor for information.\n");
2032 }
2033 }
2034
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09002035 /* limit bridge transfers to udma5, 200 sectors */
Tejun Heo3373efd2006-05-15 20:57:53 +09002036 if (ata_dev_knobble(dev)) {
Borislav Petkov5afc8142006-06-27 14:51:25 +02002037 if (ata_msg_drv(ap) && print_info)
Tejun Heof15a1da2006-05-15 20:57:56 +09002038 ata_dev_printk(dev, KERN_INFO,
2039 "applying bridge limits\n");
Tejun Heo5a529132006-03-24 14:07:50 +09002040 dev->udma_mask &= ATA_UDMA5;
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09002041 dev->max_sectors = ATA_MAX_SECTORS;
2042 }
2043
Albert Lee18d6e9d2007-04-02 11:34:15 +08002044 if (ata_device_blacklisted(dev) & ATA_HORKAGE_MAX_SEC_128)
Tejun Heo03ec52d2007-04-12 13:38:11 +09002045 dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_128,
2046 dev->max_sectors);
Albert Lee18d6e9d2007-04-02 11:34:15 +08002047
Albert Lee6f23a312007-04-02 11:39:25 +08002048 /* limit ATAPI DMA to R/W commands only */
2049 if (ata_device_blacklisted(dev) & ATA_HORKAGE_DMA_RW_ONLY)
2050 dev->horkage |= ATA_HORKAGE_DMA_RW_ONLY;
2051
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09002052 if (ap->ops->dev_config)
Alancd0d3bb2007-03-02 00:56:15 +00002053 ap->ops->dev_config(dev);
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09002054
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002055 if (ata_msg_probe(ap))
2056 ata_dev_printk(dev, KERN_DEBUG, "%s: EXIT, drv_stat = 0x%x\n",
2057 __FUNCTION__, ata_chk_status(ap));
Tejun Heoffeae412006-03-01 16:09:35 +09002058 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
2060err_out_nosup:
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002061 if (ata_msg_probe(ap))
Tejun Heo88574552006-06-25 20:00:35 +09002062 ata_dev_printk(dev, KERN_DEBUG,
2063 "%s: EXIT, err\n", __FUNCTION__);
Tejun Heoffeae412006-03-01 16:09:35 +09002064 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065}
2066
2067/**
Alan Cox2e41e8e2007-03-08 23:19:19 +00002068 * ata_cable_40wire - return 40 wire cable type
Alan Coxbe0d18d2007-03-06 02:37:56 -08002069 * @ap: port
2070 *
Alan Cox2e41e8e2007-03-08 23:19:19 +00002071 * Helper method for drivers which want to hardwire 40 wire cable
Alan Coxbe0d18d2007-03-06 02:37:56 -08002072 * detection.
2073 */
2074
2075int ata_cable_40wire(struct ata_port *ap)
2076{
2077 return ATA_CBL_PATA40;
2078}
2079
2080/**
Alan Cox2e41e8e2007-03-08 23:19:19 +00002081 * ata_cable_80wire - return 80 wire cable type
Alan Coxbe0d18d2007-03-06 02:37:56 -08002082 * @ap: port
2083 *
Alan Cox2e41e8e2007-03-08 23:19:19 +00002084 * Helper method for drivers which want to hardwire 80 wire cable
Alan Coxbe0d18d2007-03-06 02:37:56 -08002085 * detection.
2086 */
2087
2088int ata_cable_80wire(struct ata_port *ap)
2089{
2090 return ATA_CBL_PATA80;
2091}
2092
2093/**
2094 * ata_cable_unknown - return unknown PATA cable.
2095 * @ap: port
2096 *
2097 * Helper method for drivers which have no PATA cable detection.
2098 */
2099
2100int ata_cable_unknown(struct ata_port *ap)
2101{
2102 return ATA_CBL_PATA_UNK;
2103}
2104
2105/**
2106 * ata_cable_sata - return SATA cable type
2107 * @ap: port
2108 *
2109 * Helper method for drivers which have SATA cables
2110 */
2111
2112int ata_cable_sata(struct ata_port *ap)
2113{
2114 return ATA_CBL_SATA;
2115}
2116
2117/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118 * ata_bus_probe - Reset and probe ATA bus
2119 * @ap: Bus to probe
2120 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04002121 * Master ATA bus probing function. Initiates a hardware-dependent
2122 * bus reset, then attempts to identify any devices found on
2123 * the bus.
2124 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002126 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 *
2128 * RETURNS:
Tejun Heo96072e62006-04-01 01:38:17 +09002129 * Zero on success, negative errno otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 */
2131
Brian King80289162006-08-07 14:27:31 -05002132int ata_bus_probe(struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133{
Tejun Heo28ca5c52006-03-01 16:09:36 +09002134 unsigned int classes[ATA_MAX_DEVICES];
Tejun Heo14d2bac2006-04-02 17:54:46 +09002135 int tries[ATA_MAX_DEVICES];
Tejun Heo4ae72a12007-02-02 16:22:30 +09002136 int i, rc;
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002137 struct ata_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138
Tejun Heo28ca5c52006-03-01 16:09:36 +09002139 ata_port_probe(ap);
2140
Tejun Heo14d2bac2006-04-02 17:54:46 +09002141 for (i = 0; i < ATA_MAX_DEVICES; i++)
2142 tries[i] = ATA_PROBE_MAX_TRIES;
2143
2144 retry:
Tejun Heo20444702006-03-13 01:57:01 +09002145 /* reset and determine device classes */
Tejun Heo52783c52006-05-31 18:28:22 +09002146 ap->ops->phy_reset(ap);
Tejun Heo2061a472006-03-12 00:57:39 +09002147
Tejun Heo52783c52006-05-31 18:28:22 +09002148 for (i = 0; i < ATA_MAX_DEVICES; i++) {
2149 dev = &ap->device[i];
Tejun Heoc19ba8a2006-01-24 17:05:22 +09002150
Tejun Heo52783c52006-05-31 18:28:22 +09002151 if (!(ap->flags & ATA_FLAG_DISABLED) &&
2152 dev->class != ATA_DEV_UNKNOWN)
2153 classes[dev->devno] = dev->class;
2154 else
2155 classes[dev->devno] = ATA_DEV_NONE;
Tejun Heo20444702006-03-13 01:57:01 +09002156
Tejun Heo52783c52006-05-31 18:28:22 +09002157 dev->class = ATA_DEV_UNKNOWN;
Tejun Heo28ca5c52006-03-01 16:09:36 +09002158 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159
Tejun Heo52783c52006-05-31 18:28:22 +09002160 ata_port_probe(ap);
Tejun Heo20444702006-03-13 01:57:01 +09002161
Alan Coxb6079ca2006-05-22 16:52:06 +01002162 /* after the reset the device state is PIO 0 and the controller
2163 state is undefined. Record the mode */
2164
2165 for (i = 0; i < ATA_MAX_DEVICES; i++)
2166 ap->device[i].pio_mode = XFER_PIO_0;
2167
Jeff Garzikf31f0cc2007-03-02 17:47:28 -05002168 /* read IDENTIFY page and configure devices. We have to do the identify
2169 specific sequence bass-ackwards so that PDIAG- is released by
2170 the slave device */
2171
2172 for (i = ATA_MAX_DEVICES - 1; i >= 0; i--) {
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002173 dev = &ap->device[i];
Tejun Heo28ca5c52006-03-01 16:09:36 +09002174
Tejun Heoec573752006-04-11 22:26:29 +09002175 if (tries[i])
2176 dev->class = classes[i];
Tejun Heo14d2bac2006-04-02 17:54:46 +09002177
Tejun Heoe1211e32006-04-01 01:38:18 +09002178 if (!ata_dev_enabled(dev))
Tejun Heoffeae412006-03-01 16:09:35 +09002179 continue;
2180
Tejun Heobff04642006-11-10 18:08:10 +09002181 rc = ata_dev_read_id(dev, &dev->class, ATA_READID_POSTRESET,
2182 dev->id);
Tejun Heo14d2bac2006-04-02 17:54:46 +09002183 if (rc)
2184 goto fail;
Jeff Garzikf31f0cc2007-03-02 17:47:28 -05002185 }
2186
Alan Coxbe0d18d2007-03-06 02:37:56 -08002187 /* Now ask for the cable type as PDIAG- should have been released */
2188 if (ap->ops->cable_detect)
2189 ap->cbl = ap->ops->cable_detect(ap);
2190
Jeff Garzikf31f0cc2007-03-02 17:47:28 -05002191 /* After the identify sequence we can now set up the devices. We do
2192 this in the normal order so that the user doesn't get confused */
2193
2194 for(i = 0; i < ATA_MAX_DEVICES; i++) {
2195 dev = &ap->device[i];
2196 if (!ata_dev_enabled(dev))
2197 continue;
Tejun Heoffeae412006-03-01 16:09:35 +09002198
Tejun Heoefdaedc2006-11-01 18:38:52 +09002199 ap->eh_context.i.flags |= ATA_EHI_PRINTINFO;
2200 rc = ata_dev_configure(dev);
2201 ap->eh_context.i.flags &= ~ATA_EHI_PRINTINFO;
Tejun Heo14d2bac2006-04-02 17:54:46 +09002202 if (rc)
2203 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 }
2205
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002206 /* configure transfer mode */
Tejun Heo3adcebb2006-05-15 20:57:37 +09002207 rc = ata_set_mode(ap, &dev);
Tejun Heo4ae72a12007-02-02 16:22:30 +09002208 if (rc)
Tejun Heo51713d32006-04-11 22:26:29 +09002209 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002211 for (i = 0; i < ATA_MAX_DEVICES; i++)
2212 if (ata_dev_enabled(&ap->device[i]))
2213 return 0;
Alan Coxe35a9e02006-03-27 18:46:37 +01002214
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002215 /* no device present, disable port */
2216 ata_port_disable(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 ap->ops->port_disable(ap);
Tejun Heo96072e62006-04-01 01:38:17 +09002218 return -ENODEV;
Tejun Heo14d2bac2006-04-02 17:54:46 +09002219
2220 fail:
Tejun Heo4ae72a12007-02-02 16:22:30 +09002221 tries[dev->devno]--;
2222
Tejun Heo14d2bac2006-04-02 17:54:46 +09002223 switch (rc) {
2224 case -EINVAL:
Tejun Heo4ae72a12007-02-02 16:22:30 +09002225 /* eeek, something went very wrong, give up */
Tejun Heo14d2bac2006-04-02 17:54:46 +09002226 tries[dev->devno] = 0;
2227 break;
Tejun Heo4ae72a12007-02-02 16:22:30 +09002228
2229 case -ENODEV:
2230 /* give it just one more chance */
2231 tries[dev->devno] = min(tries[dev->devno], 1);
Tejun Heo14d2bac2006-04-02 17:54:46 +09002232 case -EIO:
Tejun Heo4ae72a12007-02-02 16:22:30 +09002233 if (tries[dev->devno] == 1) {
2234 /* This is the last chance, better to slow
2235 * down than lose it.
2236 */
2237 sata_down_spd_limit(ap);
2238 ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
2239 }
Tejun Heo14d2bac2006-04-02 17:54:46 +09002240 }
2241
Tejun Heo4ae72a12007-02-02 16:22:30 +09002242 if (!tries[dev->devno])
Tejun Heo3373efd2006-05-15 20:57:53 +09002243 ata_dev_disable(dev);
Tejun Heoec573752006-04-11 22:26:29 +09002244
Tejun Heo14d2bac2006-04-02 17:54:46 +09002245 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246}
2247
2248/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002249 * ata_port_probe - Mark port as enabled
2250 * @ap: Port for which we indicate enablement
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04002252 * Modify @ap data structure such that the system
2253 * thinks that the entire port is enabled.
2254 *
Jeff Garzikcca39742006-08-24 03:19:22 -04002255 * LOCKING: host lock, or some other form of
Jeff Garzik0cba6322005-05-30 19:49:12 -04002256 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 */
2258
2259void ata_port_probe(struct ata_port *ap)
2260{
Tejun Heo198e0fe2006-04-02 18:51:52 +09002261 ap->flags &= ~ATA_FLAG_DISABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262}
2263
2264/**
Tejun Heo3be680b2005-12-19 22:35:02 +09002265 * sata_print_link_status - Print SATA link status
2266 * @ap: SATA port to printk link status about
2267 *
2268 * This function prints link speed and status of a SATA link.
2269 *
2270 * LOCKING:
2271 * None.
2272 */
Jeff Garzik43727fb2007-02-25 16:50:52 -05002273void sata_print_link_status(struct ata_port *ap)
Tejun Heo3be680b2005-12-19 22:35:02 +09002274{
Tejun Heo6d5f9732006-04-03 00:09:41 +09002275 u32 sstatus, scontrol, tmp;
Tejun Heo3be680b2005-12-19 22:35:02 +09002276
Tejun Heo81952c52006-05-15 20:57:47 +09002277 if (sata_scr_read(ap, SCR_STATUS, &sstatus))
Tejun Heo3be680b2005-12-19 22:35:02 +09002278 return;
Tejun Heo81952c52006-05-15 20:57:47 +09002279 sata_scr_read(ap, SCR_CONTROL, &scontrol);
Tejun Heo3be680b2005-12-19 22:35:02 +09002280
Tejun Heo81952c52006-05-15 20:57:47 +09002281 if (ata_port_online(ap)) {
Tejun Heo3be680b2005-12-19 22:35:02 +09002282 tmp = (sstatus >> 4) & 0xf;
Tejun Heof15a1da2006-05-15 20:57:56 +09002283 ata_port_printk(ap, KERN_INFO,
2284 "SATA link up %s (SStatus %X SControl %X)\n",
2285 sata_spd_string(tmp), sstatus, scontrol);
Tejun Heo3be680b2005-12-19 22:35:02 +09002286 } else {
Tejun Heof15a1da2006-05-15 20:57:56 +09002287 ata_port_printk(ap, KERN_INFO,
2288 "SATA link down (SStatus %X SControl %X)\n",
2289 sstatus, scontrol);
Tejun Heo3be680b2005-12-19 22:35:02 +09002290 }
2291}
2292
2293/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04002294 * __sata_phy_reset - Wake/reset a low-level SATA PHY
2295 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002297 * This function issues commands to standard SATA Sxxx
2298 * PHY registers, to wake up the phy (and device), and
2299 * clear any reset condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 *
2301 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002302 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 *
2304 */
2305void __sata_phy_reset(struct ata_port *ap)
2306{
2307 u32 sstatus;
2308 unsigned long timeout = jiffies + (HZ * 5);
2309
2310 if (ap->flags & ATA_FLAG_SATA_RESET) {
Brett Russcdcca89e2005-03-28 15:10:27 -05002311 /* issue phy wake/reset */
Tejun Heo81952c52006-05-15 20:57:47 +09002312 sata_scr_write_flush(ap, SCR_CONTROL, 0x301);
Tejun Heo62ba2842005-06-26 23:27:19 +09002313 /* Couldn't find anything in SATA I/II specs, but
2314 * AHCI-1.1 10.4.2 says at least 1 ms. */
2315 mdelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316 }
Tejun Heo81952c52006-05-15 20:57:47 +09002317 /* phy wake/clear reset */
2318 sata_scr_write_flush(ap, SCR_CONTROL, 0x300);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002319
2320 /* wait for phy to become ready, if necessary */
2321 do {
2322 msleep(200);
Tejun Heo81952c52006-05-15 20:57:47 +09002323 sata_scr_read(ap, SCR_STATUS, &sstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324 if ((sstatus & 0xf) != 1)
2325 break;
2326 } while (time_before(jiffies, timeout));
2327
Tejun Heo3be680b2005-12-19 22:35:02 +09002328 /* print link status */
2329 sata_print_link_status(ap);
Jeff Garzik656563e2005-11-20 03:36:45 -05002330
Tejun Heo3be680b2005-12-19 22:35:02 +09002331 /* TODO: phy layer with polling, timeouts, etc. */
Tejun Heo81952c52006-05-15 20:57:47 +09002332 if (!ata_port_offline(ap))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 ata_port_probe(ap);
Tejun Heo3be680b2005-12-19 22:35:02 +09002334 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 ata_port_disable(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336
Tejun Heo198e0fe2006-04-02 18:51:52 +09002337 if (ap->flags & ATA_FLAG_DISABLED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 return;
2339
2340 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
2341 ata_port_disable(ap);
2342 return;
2343 }
2344
2345 ap->cbl = ATA_CBL_SATA;
2346}
2347
2348/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04002349 * sata_phy_reset - Reset SATA bus.
2350 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002352 * This function resets the SATA bus, and then probes
2353 * the bus for devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 *
2355 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002356 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 *
2358 */
2359void sata_phy_reset(struct ata_port *ap)
2360{
2361 __sata_phy_reset(ap);
Tejun Heo198e0fe2006-04-02 18:51:52 +09002362 if (ap->flags & ATA_FLAG_DISABLED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 return;
2364 ata_bus_reset(ap);
2365}
2366
2367/**
Alan Coxebdfca6e2006-03-23 15:38:34 +00002368 * ata_dev_pair - return other device on cable
Alan Coxebdfca6e2006-03-23 15:38:34 +00002369 * @adev: device
2370 *
2371 * Obtain the other device on the same cable, or if none is
2372 * present NULL is returned
2373 */
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002374
Tejun Heo3373efd2006-05-15 20:57:53 +09002375struct ata_device *ata_dev_pair(struct ata_device *adev)
Alan Coxebdfca6e2006-03-23 15:38:34 +00002376{
Tejun Heo3373efd2006-05-15 20:57:53 +09002377 struct ata_port *ap = adev->ap;
Alan Coxebdfca6e2006-03-23 15:38:34 +00002378 struct ata_device *pair = &ap->device[1 - adev->devno];
Tejun Heoe1211e32006-04-01 01:38:18 +09002379 if (!ata_dev_enabled(pair))
Alan Coxebdfca6e2006-03-23 15:38:34 +00002380 return NULL;
2381 return pair;
2382}
2383
2384/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04002385 * ata_port_disable - Disable port.
2386 * @ap: Port to be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002388 * Modify @ap data structure such that the system
2389 * thinks that the entire port is disabled, and should
2390 * never attempt to probe or communicate with devices
2391 * on this port.
2392 *
Jeff Garzikcca39742006-08-24 03:19:22 -04002393 * LOCKING: host lock, or some other form of
Jeff Garzik780a87f2005-05-30 15:41:05 -04002394 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 */
2396
2397void ata_port_disable(struct ata_port *ap)
2398{
2399 ap->device[0].class = ATA_DEV_NONE;
2400 ap->device[1].class = ATA_DEV_NONE;
Tejun Heo198e0fe2006-04-02 18:51:52 +09002401 ap->flags |= ATA_FLAG_DISABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402}
2403
Tejun Heo1c3fae42006-04-02 20:53:28 +09002404/**
Tejun Heo3c567b72006-05-15 20:57:23 +09002405 * sata_down_spd_limit - adjust SATA spd limit downward
Tejun Heo1c3fae42006-04-02 20:53:28 +09002406 * @ap: Port to adjust SATA spd limit for
2407 *
2408 * Adjust SATA spd limit of @ap downward. Note that this
2409 * function only adjusts the limit. The change must be applied
Tejun Heo3c567b72006-05-15 20:57:23 +09002410 * using sata_set_spd().
Tejun Heo1c3fae42006-04-02 20:53:28 +09002411 *
2412 * LOCKING:
2413 * Inherited from caller.
2414 *
2415 * RETURNS:
2416 * 0 on success, negative errno on failure
2417 */
Tejun Heo3c567b72006-05-15 20:57:23 +09002418int sata_down_spd_limit(struct ata_port *ap)
Tejun Heo1c3fae42006-04-02 20:53:28 +09002419{
Tejun Heo81952c52006-05-15 20:57:47 +09002420 u32 sstatus, spd, mask;
2421 int rc, highbit;
Tejun Heo1c3fae42006-04-02 20:53:28 +09002422
Tejun Heo81952c52006-05-15 20:57:47 +09002423 rc = sata_scr_read(ap, SCR_STATUS, &sstatus);
2424 if (rc)
2425 return rc;
Tejun Heo1c3fae42006-04-02 20:53:28 +09002426
2427 mask = ap->sata_spd_limit;
2428 if (mask <= 1)
2429 return -EINVAL;
2430 highbit = fls(mask) - 1;
2431 mask &= ~(1 << highbit);
2432
Tejun Heo81952c52006-05-15 20:57:47 +09002433 spd = (sstatus >> 4) & 0xf;
Tejun Heo1c3fae42006-04-02 20:53:28 +09002434 if (spd <= 1)
2435 return -EINVAL;
2436 spd--;
2437 mask &= (1 << spd) - 1;
2438 if (!mask)
2439 return -EINVAL;
2440
2441 ap->sata_spd_limit = mask;
2442
Tejun Heof15a1da2006-05-15 20:57:56 +09002443 ata_port_printk(ap, KERN_WARNING, "limiting SATA link speed to %s\n",
2444 sata_spd_string(fls(mask)));
Tejun Heo1c3fae42006-04-02 20:53:28 +09002445
2446 return 0;
2447}
2448
Tejun Heo3c567b72006-05-15 20:57:23 +09002449static int __sata_set_spd_needed(struct ata_port *ap, u32 *scontrol)
Tejun Heo1c3fae42006-04-02 20:53:28 +09002450{
2451 u32 spd, limit;
2452
2453 if (ap->sata_spd_limit == UINT_MAX)
2454 limit = 0;
2455 else
2456 limit = fls(ap->sata_spd_limit);
2457
2458 spd = (*scontrol >> 4) & 0xf;
2459 *scontrol = (*scontrol & ~0xf0) | ((limit & 0xf) << 4);
2460
2461 return spd != limit;
2462}
2463
2464/**
Tejun Heo3c567b72006-05-15 20:57:23 +09002465 * sata_set_spd_needed - is SATA spd configuration needed
Tejun Heo1c3fae42006-04-02 20:53:28 +09002466 * @ap: Port in question
2467 *
2468 * Test whether the spd limit in SControl matches
2469 * @ap->sata_spd_limit. This function is used to determine
2470 * whether hardreset is necessary to apply SATA spd
2471 * configuration.
2472 *
2473 * LOCKING:
2474 * Inherited from caller.
2475 *
2476 * RETURNS:
2477 * 1 if SATA spd configuration is needed, 0 otherwise.
2478 */
Tejun Heo3c567b72006-05-15 20:57:23 +09002479int sata_set_spd_needed(struct ata_port *ap)
Tejun Heo1c3fae42006-04-02 20:53:28 +09002480{
2481 u32 scontrol;
2482
Tejun Heo81952c52006-05-15 20:57:47 +09002483 if (sata_scr_read(ap, SCR_CONTROL, &scontrol))
Tejun Heo1c3fae42006-04-02 20:53:28 +09002484 return 0;
2485
Tejun Heo3c567b72006-05-15 20:57:23 +09002486 return __sata_set_spd_needed(ap, &scontrol);
Tejun Heo1c3fae42006-04-02 20:53:28 +09002487}
2488
2489/**
Tejun Heo3c567b72006-05-15 20:57:23 +09002490 * sata_set_spd - set SATA spd according to spd limit
Tejun Heo1c3fae42006-04-02 20:53:28 +09002491 * @ap: Port to set SATA spd for
2492 *
2493 * Set SATA spd of @ap according to sata_spd_limit.
2494 *
2495 * LOCKING:
2496 * Inherited from caller.
2497 *
2498 * RETURNS:
2499 * 0 if spd doesn't need to be changed, 1 if spd has been
Tejun Heo81952c52006-05-15 20:57:47 +09002500 * changed. Negative errno if SCR registers are inaccessible.
Tejun Heo1c3fae42006-04-02 20:53:28 +09002501 */
Tejun Heo3c567b72006-05-15 20:57:23 +09002502int sata_set_spd(struct ata_port *ap)
Tejun Heo1c3fae42006-04-02 20:53:28 +09002503{
2504 u32 scontrol;
Tejun Heo81952c52006-05-15 20:57:47 +09002505 int rc;
Tejun Heo1c3fae42006-04-02 20:53:28 +09002506
Tejun Heo81952c52006-05-15 20:57:47 +09002507 if ((rc = sata_scr_read(ap, SCR_CONTROL, &scontrol)))
2508 return rc;
Tejun Heo1c3fae42006-04-02 20:53:28 +09002509
Tejun Heo3c567b72006-05-15 20:57:23 +09002510 if (!__sata_set_spd_needed(ap, &scontrol))
Tejun Heo1c3fae42006-04-02 20:53:28 +09002511 return 0;
2512
Tejun Heo81952c52006-05-15 20:57:47 +09002513 if ((rc = sata_scr_write(ap, SCR_CONTROL, scontrol)))
2514 return rc;
2515
Tejun Heo1c3fae42006-04-02 20:53:28 +09002516 return 1;
2517}
2518
Alan Cox452503f2005-10-21 19:01:32 -04002519/*
2520 * This mode timing computation functionality is ported over from
2521 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
2522 */
2523/*
Alan Coxb352e572006-08-10 18:52:12 +01002524 * PIO 0-4, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
Alan Cox452503f2005-10-21 19:01:32 -04002525 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
Alan Coxb352e572006-08-10 18:52:12 +01002526 * for UDMA6, which is currently supported only by Maxtor drives.
2527 *
2528 * For PIO 5/6 MWDMA 3/4 see the CFA specification 3.0.
Alan Cox452503f2005-10-21 19:01:32 -04002529 */
2530
2531static const struct ata_timing ata_timing[] = {
2532
2533 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
2534 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
2535 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
2536 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
2537
Alan Coxb352e572006-08-10 18:52:12 +01002538 { XFER_MW_DMA_4, 25, 0, 0, 0, 55, 20, 80, 0 },
2539 { XFER_MW_DMA_3, 25, 0, 0, 0, 65, 25, 100, 0 },
Alan Cox452503f2005-10-21 19:01:32 -04002540 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
2541 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
2542 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
2543
2544/* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002545
Alan Cox452503f2005-10-21 19:01:32 -04002546 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
2547 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
2548 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002549
Alan Cox452503f2005-10-21 19:01:32 -04002550 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
2551 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
2552 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
2553
Alan Coxb352e572006-08-10 18:52:12 +01002554 { XFER_PIO_6, 10, 55, 20, 80, 55, 20, 80, 0 },
2555 { XFER_PIO_5, 15, 65, 25, 100, 65, 25, 100, 0 },
Alan Cox452503f2005-10-21 19:01:32 -04002556 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
2557 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
2558
2559 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
2560 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
2561 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
2562
2563/* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
2564
2565 { 0xFF }
2566};
2567
2568#define ENOUGH(v,unit) (((v)-1)/(unit)+1)
2569#define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
2570
2571static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
2572{
2573 q->setup = EZ(t->setup * 1000, T);
2574 q->act8b = EZ(t->act8b * 1000, T);
2575 q->rec8b = EZ(t->rec8b * 1000, T);
2576 q->cyc8b = EZ(t->cyc8b * 1000, T);
2577 q->active = EZ(t->active * 1000, T);
2578 q->recover = EZ(t->recover * 1000, T);
2579 q->cycle = EZ(t->cycle * 1000, T);
2580 q->udma = EZ(t->udma * 1000, UT);
2581}
2582
2583void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
2584 struct ata_timing *m, unsigned int what)
2585{
2586 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
2587 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
2588 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
2589 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
2590 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
2591 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
2592 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
2593 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
2594}
2595
2596static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
2597{
2598 const struct ata_timing *t;
2599
2600 for (t = ata_timing; t->mode != speed; t++)
Alan Cox91190752005-10-26 12:17:46 -04002601 if (t->mode == 0xFF)
Alan Cox452503f2005-10-21 19:01:32 -04002602 return NULL;
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002603 return t;
Alan Cox452503f2005-10-21 19:01:32 -04002604}
2605
2606int ata_timing_compute(struct ata_device *adev, unsigned short speed,
2607 struct ata_timing *t, int T, int UT)
2608{
2609 const struct ata_timing *s;
2610 struct ata_timing p;
2611
2612 /*
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002613 * Find the mode.
Albert Lee75b1f2f2005-11-16 17:06:18 +08002614 */
Alan Cox452503f2005-10-21 19:01:32 -04002615
2616 if (!(s = ata_timing_find_mode(speed)))
2617 return -EINVAL;
2618
Albert Lee75b1f2f2005-11-16 17:06:18 +08002619 memcpy(t, s, sizeof(*s));
2620
Alan Cox452503f2005-10-21 19:01:32 -04002621 /*
2622 * If the drive is an EIDE drive, it can tell us it needs extended
2623 * PIO/MW_DMA cycle timing.
2624 */
2625
2626 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
2627 memset(&p, 0, sizeof(p));
2628 if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
2629 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
2630 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
2631 } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
2632 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
2633 }
2634 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
2635 }
2636
2637 /*
2638 * Convert the timing to bus clock counts.
2639 */
2640
Albert Lee75b1f2f2005-11-16 17:06:18 +08002641 ata_timing_quantize(t, t, T, UT);
Alan Cox452503f2005-10-21 19:01:32 -04002642
2643 /*
Randy Dunlapc893a3a2006-01-28 13:15:32 -05002644 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
2645 * S.M.A.R.T * and some other commands. We have to ensure that the
2646 * DMA cycle timing is slower/equal than the fastest PIO timing.
Alan Cox452503f2005-10-21 19:01:32 -04002647 */
2648
Alanfd3367af2006-12-07 12:41:18 +00002649 if (speed > XFER_PIO_6) {
Alan Cox452503f2005-10-21 19:01:32 -04002650 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
2651 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
2652 }
2653
2654 /*
Randy Dunlapc893a3a2006-01-28 13:15:32 -05002655 * Lengthen active & recovery time so that cycle time is correct.
Alan Cox452503f2005-10-21 19:01:32 -04002656 */
2657
2658 if (t->act8b + t->rec8b < t->cyc8b) {
2659 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
2660 t->rec8b = t->cyc8b - t->act8b;
2661 }
2662
2663 if (t->active + t->recover < t->cycle) {
2664 t->active += (t->cycle - (t->active + t->recover)) / 2;
2665 t->recover = t->cycle - t->active;
2666 }
Jeff Garzika617c092007-05-21 20:14:23 -04002667
Alan Cox4f701d12007-04-23 11:55:36 +01002668 /* In a few cases quantisation may produce enough errors to
2669 leave t->cycle too low for the sum of active and recovery
2670 if so we must correct this */
2671 if (t->active + t->recover > t->cycle)
2672 t->cycle = t->active + t->recover;
Alan Cox452503f2005-10-21 19:01:32 -04002673
2674 return 0;
2675}
2676
Tejun Heocf176e12006-04-02 17:54:46 +09002677/**
2678 * ata_down_xfermask_limit - adjust dev xfer masks downward
Tejun Heocf176e12006-04-02 17:54:46 +09002679 * @dev: Device to adjust xfer masks
Tejun Heo458337d2007-02-02 16:22:30 +09002680 * @sel: ATA_DNXFER_* selector
Tejun Heocf176e12006-04-02 17:54:46 +09002681 *
2682 * Adjust xfer masks of @dev downward. Note that this function
2683 * does not apply the change. Invoking ata_set_mode() afterwards
2684 * will apply the limit.
2685 *
2686 * LOCKING:
2687 * Inherited from caller.
2688 *
2689 * RETURNS:
2690 * 0 on success, negative errno on failure
2691 */
Tejun Heo458337d2007-02-02 16:22:30 +09002692int ata_down_xfermask_limit(struct ata_device *dev, unsigned int sel)
Tejun Heocf176e12006-04-02 17:54:46 +09002693{
Tejun Heo458337d2007-02-02 16:22:30 +09002694 char buf[32];
2695 unsigned int orig_mask, xfer_mask;
2696 unsigned int pio_mask, mwdma_mask, udma_mask;
2697 int quiet, highbit;
Tejun Heocf176e12006-04-02 17:54:46 +09002698
Tejun Heo458337d2007-02-02 16:22:30 +09002699 quiet = !!(sel & ATA_DNXFER_QUIET);
2700 sel &= ~ATA_DNXFER_QUIET;
Tejun Heocf176e12006-04-02 17:54:46 +09002701
Tejun Heo458337d2007-02-02 16:22:30 +09002702 xfer_mask = orig_mask = ata_pack_xfermask(dev->pio_mask,
2703 dev->mwdma_mask,
2704 dev->udma_mask);
2705 ata_unpack_xfermask(xfer_mask, &pio_mask, &mwdma_mask, &udma_mask);
Tejun Heocf176e12006-04-02 17:54:46 +09002706
Tejun Heo458337d2007-02-02 16:22:30 +09002707 switch (sel) {
2708 case ATA_DNXFER_PIO:
2709 highbit = fls(pio_mask) - 1;
2710 pio_mask &= ~(1 << highbit);
2711 break;
2712
2713 case ATA_DNXFER_DMA:
2714 if (udma_mask) {
2715 highbit = fls(udma_mask) - 1;
2716 udma_mask &= ~(1 << highbit);
2717 if (!udma_mask)
2718 return -ENOENT;
2719 } else if (mwdma_mask) {
2720 highbit = fls(mwdma_mask) - 1;
2721 mwdma_mask &= ~(1 << highbit);
2722 if (!mwdma_mask)
2723 return -ENOENT;
2724 }
2725 break;
2726
2727 case ATA_DNXFER_40C:
2728 udma_mask &= ATA_UDMA_MASK_40C;
2729 break;
2730
2731 case ATA_DNXFER_FORCE_PIO0:
2732 pio_mask &= 1;
2733 case ATA_DNXFER_FORCE_PIO:
2734 mwdma_mask = 0;
2735 udma_mask = 0;
2736 break;
2737
Tejun Heo458337d2007-02-02 16:22:30 +09002738 default:
2739 BUG();
2740 }
2741
2742 xfer_mask &= ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
2743
2744 if (!(xfer_mask & ATA_MASK_PIO) || xfer_mask == orig_mask)
2745 return -ENOENT;
2746
2747 if (!quiet) {
2748 if (xfer_mask & (ATA_MASK_MWDMA | ATA_MASK_UDMA))
2749 snprintf(buf, sizeof(buf), "%s:%s",
2750 ata_mode_string(xfer_mask),
2751 ata_mode_string(xfer_mask & ATA_MASK_PIO));
2752 else
2753 snprintf(buf, sizeof(buf), "%s",
2754 ata_mode_string(xfer_mask));
2755
2756 ata_dev_printk(dev, KERN_WARNING,
2757 "limiting speed to %s\n", buf);
2758 }
Tejun Heocf176e12006-04-02 17:54:46 +09002759
2760 ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask,
2761 &dev->udma_mask);
2762
Tejun Heocf176e12006-04-02 17:54:46 +09002763 return 0;
Tejun Heocf176e12006-04-02 17:54:46 +09002764}
2765
Tejun Heo3373efd2006-05-15 20:57:53 +09002766static int ata_dev_set_mode(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767{
Tejun Heobaa1e782006-11-01 18:39:27 +09002768 struct ata_eh_context *ehc = &dev->ap->eh_context;
Tejun Heo83206a22006-03-24 15:25:31 +09002769 unsigned int err_mask;
2770 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771
Tejun Heoe8384602006-04-02 18:51:53 +09002772 dev->flags &= ~ATA_DFLAG_PIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 if (dev->xfer_shift == ATA_SHIFT_PIO)
2774 dev->flags |= ATA_DFLAG_PIO;
2775
Tejun Heo3373efd2006-05-15 20:57:53 +09002776 err_mask = ata_dev_set_xfermode(dev);
Alan11750a42007-02-05 16:28:30 +00002777 /* Old CFA may refuse this command, which is just fine */
2778 if (dev->xfer_shift == ATA_SHIFT_PIO && ata_id_is_cfa(dev->id))
2779 err_mask &= ~AC_ERR_DEV;
2780
Tejun Heo83206a22006-03-24 15:25:31 +09002781 if (err_mask) {
Tejun Heof15a1da2006-05-15 20:57:56 +09002782 ata_dev_printk(dev, KERN_ERR, "failed to set xfermode "
2783 "(err_mask=0x%x)\n", err_mask);
Tejun Heo83206a22006-03-24 15:25:31 +09002784 return -EIO;
2785 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786
Tejun Heobaa1e782006-11-01 18:39:27 +09002787 ehc->i.flags |= ATA_EHI_POST_SETMODE;
Tejun Heo3373efd2006-05-15 20:57:53 +09002788 rc = ata_dev_revalidate(dev, 0);
Tejun Heobaa1e782006-11-01 18:39:27 +09002789 ehc->i.flags &= ~ATA_EHI_POST_SETMODE;
Tejun Heo5eb45c02006-04-02 18:51:52 +09002790 if (rc)
Tejun Heo83206a22006-03-24 15:25:31 +09002791 return rc;
Tejun Heo48a8a142006-03-05 17:55:58 +09002792
Tejun Heo23e71c32006-03-06 04:31:57 +09002793 DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n",
2794 dev->xfer_shift, (int)dev->xfer_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795
Tejun Heof15a1da2006-05-15 20:57:56 +09002796 ata_dev_printk(dev, KERN_INFO, "configured for %s\n",
2797 ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)));
Tejun Heo83206a22006-03-24 15:25:31 +09002798 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799}
2800
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801/**
Alan04351822007-03-06 02:37:52 -08002802 * ata_do_set_mode - Program timings and issue SET FEATURES - XFER
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 * @ap: port on which timings will be programmed
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002804 * @r_failed_dev: out paramter for failed device
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 *
Alan04351822007-03-06 02:37:52 -08002806 * Standard implementation of the function used to tune and set
2807 * ATA device disk transfer mode (PIO3, UDMA6, etc.). If
2808 * ata_dev_set_mode() fails, pointer to the failing device is
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002809 * returned in @r_failed_dev.
Jeff Garzik780a87f2005-05-30 15:41:05 -04002810 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002812 * PCI/etc. bus probe sem.
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002813 *
2814 * RETURNS:
2815 * 0 on success, negative errno otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 */
Alan04351822007-03-06 02:37:52 -08002817
2818int ata_do_set_mode(struct ata_port *ap, struct ata_device **r_failed_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819{
Tejun Heoe8e06192006-04-01 01:38:18 +09002820 struct ata_device *dev;
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002821 int i, rc = 0, used_dma = 0, found = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822
Tejun Heo3adcebb2006-05-15 20:57:37 +09002823
Tejun Heoa6d5a512006-03-06 04:31:57 +09002824 /* step 1: calculate xfer_mask */
2825 for (i = 0; i < ATA_MAX_DEVICES; i++) {
Tejun Heoacf356b2006-03-24 14:07:50 +09002826 unsigned int pio_mask, dma_mask;
Tejun Heoa6d5a512006-03-06 04:31:57 +09002827
Tejun Heoe8e06192006-04-01 01:38:18 +09002828 dev = &ap->device[i];
2829
Tejun Heoe1211e32006-04-01 01:38:18 +09002830 if (!ata_dev_enabled(dev))
Tejun Heoa6d5a512006-03-06 04:31:57 +09002831 continue;
2832
Tejun Heo3373efd2006-05-15 20:57:53 +09002833 ata_dev_xfermask(dev);
Tejun Heoa6d5a512006-03-06 04:31:57 +09002834
Tejun Heoacf356b2006-03-24 14:07:50 +09002835 pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0);
2836 dma_mask = ata_pack_xfermask(0, dev->mwdma_mask, dev->udma_mask);
2837 dev->pio_mode = ata_xfer_mask2mode(pio_mask);
2838 dev->dma_mode = ata_xfer_mask2mode(dma_mask);
Alan Cox5444a6f2006-03-27 18:58:20 +01002839
Tejun Heo4f659772006-04-01 01:38:18 +09002840 found = 1;
Alan Cox5444a6f2006-03-27 18:58:20 +01002841 if (dev->dma_mode)
2842 used_dma = 1;
Tejun Heoa6d5a512006-03-06 04:31:57 +09002843 }
Tejun Heo4f659772006-04-01 01:38:18 +09002844 if (!found)
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002845 goto out;
Tejun Heoa6d5a512006-03-06 04:31:57 +09002846
2847 /* step 2: always set host PIO timings */
Tejun Heoe8e06192006-04-01 01:38:18 +09002848 for (i = 0; i < ATA_MAX_DEVICES; i++) {
2849 dev = &ap->device[i];
2850 if (!ata_dev_enabled(dev))
2851 continue;
2852
2853 if (!dev->pio_mode) {
Tejun Heof15a1da2006-05-15 20:57:56 +09002854 ata_dev_printk(dev, KERN_WARNING, "no PIO support\n");
Tejun Heoe8e06192006-04-01 01:38:18 +09002855 rc = -EINVAL;
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002856 goto out;
Tejun Heoe8e06192006-04-01 01:38:18 +09002857 }
2858
2859 dev->xfer_mode = dev->pio_mode;
2860 dev->xfer_shift = ATA_SHIFT_PIO;
2861 if (ap->ops->set_piomode)
2862 ap->ops->set_piomode(ap, dev);
2863 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864
Tejun Heoa6d5a512006-03-06 04:31:57 +09002865 /* step 3: set host DMA timings */
Tejun Heoe8e06192006-04-01 01:38:18 +09002866 for (i = 0; i < ATA_MAX_DEVICES; i++) {
2867 dev = &ap->device[i];
2868
2869 if (!ata_dev_enabled(dev) || !dev->dma_mode)
2870 continue;
2871
2872 dev->xfer_mode = dev->dma_mode;
2873 dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
2874 if (ap->ops->set_dmamode)
2875 ap->ops->set_dmamode(ap, dev);
2876 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877
2878 /* step 4: update devices' xfer mode */
Tejun Heo83206a22006-03-24 15:25:31 +09002879 for (i = 0; i < ATA_MAX_DEVICES; i++) {
Tejun Heoe8e06192006-04-01 01:38:18 +09002880 dev = &ap->device[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881
Alan18d90de2007-01-24 11:42:38 +00002882 /* don't update suspended devices' xfer mode */
Tejun Heo9666f402007-05-04 21:27:47 +02002883 if (!ata_dev_enabled(dev))
Tejun Heo83206a22006-03-24 15:25:31 +09002884 continue;
2885
Tejun Heo3373efd2006-05-15 20:57:53 +09002886 rc = ata_dev_set_mode(dev);
Tejun Heo5bbc53f2006-04-01 01:38:17 +09002887 if (rc)
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002888 goto out;
Tejun Heo83206a22006-03-24 15:25:31 +09002889 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890
Tejun Heoe8e06192006-04-01 01:38:18 +09002891 /* Record simplex status. If we selected DMA then the other
2892 * host channels are not permitted to do so.
Alan Cox5444a6f2006-03-27 18:58:20 +01002893 */
Jeff Garzikcca39742006-08-24 03:19:22 -04002894 if (used_dma && (ap->host->flags & ATA_HOST_SIMPLEX))
Alan032af1c2007-03-01 17:36:46 +00002895 ap->host->simplex_claimed = ap;
Alan Cox5444a6f2006-03-27 18:58:20 +01002896
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002897 out:
2898 if (rc)
2899 *r_failed_dev = dev;
2900 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901}
2902
2903/**
Alan04351822007-03-06 02:37:52 -08002904 * ata_set_mode - Program timings and issue SET FEATURES - XFER
2905 * @ap: port on which timings will be programmed
2906 * @r_failed_dev: out paramter for failed device
2907 *
2908 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.). If
2909 * ata_set_mode() fails, pointer to the failing device is
2910 * returned in @r_failed_dev.
2911 *
2912 * LOCKING:
2913 * PCI/etc. bus probe sem.
2914 *
2915 * RETURNS:
2916 * 0 on success, negative errno otherwise
2917 */
2918int ata_set_mode(struct ata_port *ap, struct ata_device **r_failed_dev)
2919{
2920 /* has private set_mode? */
2921 if (ap->ops->set_mode)
2922 return ap->ops->set_mode(ap, r_failed_dev);
2923 return ata_do_set_mode(ap, r_failed_dev);
2924}
2925
2926/**
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002927 * ata_tf_to_host - issue ATA taskfile to host controller
2928 * @ap: port to which command is being issued
2929 * @tf: ATA taskfile register set
2930 *
2931 * Issues ATA taskfile register set to ATA host controller,
2932 * with proper synchronization with interrupt handler and
2933 * other threads.
2934 *
2935 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002936 * spin_lock_irqsave(host lock)
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002937 */
2938
2939static inline void ata_tf_to_host(struct ata_port *ap,
2940 const struct ata_taskfile *tf)
2941{
2942 ap->ops->tf_load(ap, tf);
2943 ap->ops->exec_command(ap, tf);
2944}
2945
2946/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 * ata_busy_sleep - sleep until BSY clears, or timeout
2948 * @ap: port containing status register to be polled
2949 * @tmout_pat: impatience timeout
2950 * @tmout: overall timeout
2951 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002952 * Sleep until ATA Status register bit BSY clears,
2953 * or a timeout occurs.
2954 *
Tejun Heod1adc1b2006-10-09 18:32:15 +09002955 * LOCKING:
2956 * Kernel thread context (may sleep).
2957 *
2958 * RETURNS:
2959 * 0 on success, -errno otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 */
Tejun Heod1adc1b2006-10-09 18:32:15 +09002961int ata_busy_sleep(struct ata_port *ap,
2962 unsigned long tmout_pat, unsigned long tmout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963{
2964 unsigned long timer_start, timeout;
2965 u8 status;
2966
2967 status = ata_busy_wait(ap, ATA_BUSY, 300);
2968 timer_start = jiffies;
2969 timeout = timer_start + tmout_pat;
Tejun Heod1adc1b2006-10-09 18:32:15 +09002970 while (status != 0xff && (status & ATA_BUSY) &&
2971 time_before(jiffies, timeout)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 msleep(50);
2973 status = ata_busy_wait(ap, ATA_BUSY, 3);
2974 }
2975
Tejun Heod1adc1b2006-10-09 18:32:15 +09002976 if (status != 0xff && (status & ATA_BUSY))
Tejun Heof15a1da2006-05-15 20:57:56 +09002977 ata_port_printk(ap, KERN_WARNING,
Jeff Garzik35aa7a42006-09-28 06:50:24 -04002978 "port is slow to respond, please be patient "
2979 "(Status 0x%x)\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980
2981 timeout = timer_start + tmout;
Tejun Heod1adc1b2006-10-09 18:32:15 +09002982 while (status != 0xff && (status & ATA_BUSY) &&
2983 time_before(jiffies, timeout)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 msleep(50);
2985 status = ata_chk_status(ap);
2986 }
2987
Tejun Heod1adc1b2006-10-09 18:32:15 +09002988 if (status == 0xff)
2989 return -ENODEV;
2990
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 if (status & ATA_BUSY) {
Tejun Heof15a1da2006-05-15 20:57:56 +09002992 ata_port_printk(ap, KERN_ERR, "port failed to respond "
Jeff Garzik35aa7a42006-09-28 06:50:24 -04002993 "(%lu secs, Status 0x%x)\n",
2994 tmout / HZ, status);
Tejun Heod1adc1b2006-10-09 18:32:15 +09002995 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 }
2997
2998 return 0;
2999}
3000
Tejun Heod4b2bab2007-02-02 16:50:52 +09003001/**
3002 * ata_wait_ready - sleep until BSY clears, or timeout
3003 * @ap: port containing status register to be polled
3004 * @deadline: deadline jiffies for the operation
3005 *
3006 * Sleep until ATA Status register bit BSY clears, or timeout
3007 * occurs.
3008 *
3009 * LOCKING:
3010 * Kernel thread context (may sleep).
3011 *
3012 * RETURNS:
3013 * 0 on success, -errno otherwise.
3014 */
3015int ata_wait_ready(struct ata_port *ap, unsigned long deadline)
3016{
3017 unsigned long start = jiffies;
3018 int warned = 0;
3019
3020 while (1) {
3021 u8 status = ata_chk_status(ap);
3022 unsigned long now = jiffies;
3023
3024 if (!(status & ATA_BUSY))
3025 return 0;
Tejun Heofd7fe702007-05-23 11:22:15 +02003026 if (!ata_port_online(ap) && status == 0xff)
Tejun Heod4b2bab2007-02-02 16:50:52 +09003027 return -ENODEV;
3028 if (time_after(now, deadline))
3029 return -EBUSY;
3030
3031 if (!warned && time_after(now, start + 5 * HZ) &&
3032 (deadline - now > 3 * HZ)) {
3033 ata_port_printk(ap, KERN_WARNING,
3034 "port is slow to respond, please be patient "
3035 "(Status 0x%x)\n", status);
3036 warned = 1;
3037 }
3038
3039 msleep(50);
3040 }
3041}
3042
3043static int ata_bus_post_reset(struct ata_port *ap, unsigned int devmask,
3044 unsigned long deadline)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045{
3046 struct ata_ioports *ioaddr = &ap->ioaddr;
3047 unsigned int dev0 = devmask & (1 << 0);
3048 unsigned int dev1 = devmask & (1 << 1);
Tejun Heo9b893912007-02-02 16:50:52 +09003049 int rc, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050
3051 /* if device 0 was found in ata_devchk, wait for its
3052 * BSY bit to clear
3053 */
Tejun Heod4b2bab2007-02-02 16:50:52 +09003054 if (dev0) {
3055 rc = ata_wait_ready(ap, deadline);
Tejun Heo9b893912007-02-02 16:50:52 +09003056 if (rc) {
3057 if (rc != -ENODEV)
3058 return rc;
3059 ret = rc;
3060 }
Tejun Heod4b2bab2007-02-02 16:50:52 +09003061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062
Tejun Heoe141d992007-06-10 14:26:20 +09003063 /* if device 1 was found in ata_devchk, wait for register
3064 * access briefly, then wait for BSY to clear.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 */
Tejun Heoe141d992007-06-10 14:26:20 +09003066 if (dev1) {
3067 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068
3069 ap->ops->dev_select(ap, 1);
Tejun Heoe141d992007-06-10 14:26:20 +09003070
3071 /* Wait for register access. Some ATAPI devices fail
3072 * to set nsect/lbal after reset, so don't waste too
3073 * much time on it. We're gonna wait for !BSY anyway.
3074 */
3075 for (i = 0; i < 2; i++) {
3076 u8 nsect, lbal;
3077
3078 nsect = ioread8(ioaddr->nsect_addr);
3079 lbal = ioread8(ioaddr->lbal_addr);
3080 if ((nsect == 1) && (lbal == 1))
3081 break;
3082 msleep(50); /* give drive a breather */
3083 }
3084
Tejun Heod4b2bab2007-02-02 16:50:52 +09003085 rc = ata_wait_ready(ap, deadline);
Tejun Heo9b893912007-02-02 16:50:52 +09003086 if (rc) {
3087 if (rc != -ENODEV)
3088 return rc;
3089 ret = rc;
3090 }
Tejun Heod4b2bab2007-02-02 16:50:52 +09003091 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092
3093 /* is all this really necessary? */
3094 ap->ops->dev_select(ap, 0);
3095 if (dev1)
3096 ap->ops->dev_select(ap, 1);
3097 if (dev0)
3098 ap->ops->dev_select(ap, 0);
Tejun Heod4b2bab2007-02-02 16:50:52 +09003099
Tejun Heo9b893912007-02-02 16:50:52 +09003100 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101}
3102
Tejun Heod4b2bab2007-02-02 16:50:52 +09003103static int ata_bus_softreset(struct ata_port *ap, unsigned int devmask,
3104 unsigned long deadline)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105{
3106 struct ata_ioports *ioaddr = &ap->ioaddr;
3107
Tejun Heo44877b42007-02-21 01:06:51 +09003108 DPRINTK("ata%u: bus reset via SRST\n", ap->print_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109
3110 /* software reset. causes dev0 to be selected */
Tejun Heo0d5ff562007-02-01 15:06:36 +09003111 iowrite8(ap->ctl, ioaddr->ctl_addr);
3112 udelay(20); /* FIXME: flush */
3113 iowrite8(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
3114 udelay(20); /* FIXME: flush */
3115 iowrite8(ap->ctl, ioaddr->ctl_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116
3117 /* spec mandates ">= 2ms" before checking status.
3118 * We wait 150ms, because that was the magic delay used for
3119 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
3120 * between when the ATA command register is written, and then
3121 * status is checked. Because waiting for "a while" before
3122 * checking status is fine, post SRST, we perform this magic
3123 * delay here as well.
Alan Cox09c7ad72006-03-22 15:52:40 +00003124 *
3125 * Old drivers/ide uses the 2mS rule and then waits for ready
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 */
3127 msleep(150);
3128
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05003129 /* Before we perform post reset processing we want to see if
Tejun Heo298a41c2006-03-25 02:58:13 +09003130 * the bus shows 0xFF because the odd clown forgets the D7
3131 * pulldown resistor.
3132 */
Tejun Heod1adc1b2006-10-09 18:32:15 +09003133 if (ata_check_status(ap) == 0xFF)
Tejun Heo9b893912007-02-02 16:50:52 +09003134 return -ENODEV;
Alan Cox09c7ad72006-03-22 15:52:40 +00003135
Tejun Heod4b2bab2007-02-02 16:50:52 +09003136 return ata_bus_post_reset(ap, devmask, deadline);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137}
3138
3139/**
3140 * ata_bus_reset - reset host port and associated ATA channel
3141 * @ap: port to reset
3142 *
3143 * This is typically the first time we actually start issuing
3144 * commands to the ATA channel. We wait for BSY to clear, then
3145 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
3146 * result. Determine what devices, if any, are on the channel
3147 * by looking at the device 0/1 error register. Look at the signature
3148 * stored in each device's taskfile registers, to determine if
3149 * the device is ATA or ATAPI.
3150 *
3151 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003152 * PCI/etc. bus probe sem.
Jeff Garzikcca39742006-08-24 03:19:22 -04003153 * Obtains host lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 *
3155 * SIDE EFFECTS:
Tejun Heo198e0fe2006-04-02 18:51:52 +09003156 * Sets ATA_FLAG_DISABLED if bus reset fails.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 */
3158
3159void ata_bus_reset(struct ata_port *ap)
3160{
3161 struct ata_ioports *ioaddr = &ap->ioaddr;
3162 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
3163 u8 err;
Tejun Heoaec5c3c2006-03-25 01:33:34 +09003164 unsigned int dev0, dev1 = 0, devmask = 0;
Tejun Heo9b893912007-02-02 16:50:52 +09003165 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166
Tejun Heo44877b42007-02-21 01:06:51 +09003167 DPRINTK("ENTER, host %u, port %u\n", ap->print_id, ap->port_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003168
3169 /* determine if device 0/1 are present */
3170 if (ap->flags & ATA_FLAG_SATA_RESET)
3171 dev0 = 1;
3172 else {
3173 dev0 = ata_devchk(ap, 0);
3174 if (slave_possible)
3175 dev1 = ata_devchk(ap, 1);
3176 }
3177
3178 if (dev0)
3179 devmask |= (1 << 0);
3180 if (dev1)
3181 devmask |= (1 << 1);
3182
3183 /* select device 0 again */
3184 ap->ops->dev_select(ap, 0);
3185
3186 /* issue bus reset */
Tejun Heo9b893912007-02-02 16:50:52 +09003187 if (ap->flags & ATA_FLAG_SRST) {
3188 rc = ata_bus_softreset(ap, devmask, jiffies + 40 * HZ);
3189 if (rc && rc != -ENODEV)
Tejun Heoaec5c3c2006-03-25 01:33:34 +09003190 goto err_out;
Tejun Heo9b893912007-02-02 16:50:52 +09003191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192
3193 /*
3194 * determine by signature whether we have ATA or ATAPI devices
3195 */
Tejun Heob4dc7622006-01-24 17:05:22 +09003196 ap->device[0].class = ata_dev_try_classify(ap, 0, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 if ((slave_possible) && (err != 0x81))
Tejun Heob4dc7622006-01-24 17:05:22 +09003198 ap->device[1].class = ata_dev_try_classify(ap, 1, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199
3200 /* re-enable interrupts */
Akira Iguchi83625002007-01-26 16:27:32 +09003201 ap->ops->irq_on(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202
3203 /* is double-select really necessary? */
3204 if (ap->device[1].class != ATA_DEV_NONE)
3205 ap->ops->dev_select(ap, 1);
3206 if (ap->device[0].class != ATA_DEV_NONE)
3207 ap->ops->dev_select(ap, 0);
3208
3209 /* if no devices were detected, disable this port */
3210 if ((ap->device[0].class == ATA_DEV_NONE) &&
3211 (ap->device[1].class == ATA_DEV_NONE))
3212 goto err_out;
3213
3214 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
3215 /* set up device control for ATA_FLAG_SATA_RESET */
Tejun Heo0d5ff562007-02-01 15:06:36 +09003216 iowrite8(ap->ctl, ioaddr->ctl_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 }
3218
3219 DPRINTK("EXIT\n");
3220 return;
3221
3222err_out:
Tejun Heof15a1da2006-05-15 20:57:56 +09003223 ata_port_printk(ap, KERN_ERR, "disabling port\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003224 ap->ops->port_disable(ap);
3225
3226 DPRINTK("EXIT\n");
3227}
3228
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003229/**
3230 * sata_phy_debounce - debounce SATA phy status
3231 * @ap: ATA port to debounce SATA phy status for
3232 * @params: timing parameters { interval, duratinon, timeout } in msec
Tejun Heod4b2bab2007-02-02 16:50:52 +09003233 * @deadline: deadline jiffies for the operation
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003234 *
3235 * Make sure SStatus of @ap reaches stable state, determined by
3236 * holding the same value where DET is not 1 for @duration polled
3237 * every @interval, before @timeout. Timeout constraints the
Tejun Heod4b2bab2007-02-02 16:50:52 +09003238 * beginning of the stable state. Because DET gets stuck at 1 on
3239 * some controllers after hot unplugging, this functions waits
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003240 * until timeout then returns 0 if DET is stable at 1.
3241 *
Tejun Heod4b2bab2007-02-02 16:50:52 +09003242 * @timeout is further limited by @deadline. The sooner of the
3243 * two is used.
3244 *
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003245 * LOCKING:
3246 * Kernel thread context (may sleep)
3247 *
3248 * RETURNS:
3249 * 0 on success, -errno on failure.
3250 */
Tejun Heod4b2bab2007-02-02 16:50:52 +09003251int sata_phy_debounce(struct ata_port *ap, const unsigned long *params,
3252 unsigned long deadline)
Tejun Heo7a7921e2006-02-02 18:20:00 +09003253{
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003254 unsigned long interval_msec = params[0];
Tejun Heod4b2bab2007-02-02 16:50:52 +09003255 unsigned long duration = msecs_to_jiffies(params[1]);
3256 unsigned long last_jiffies, t;
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003257 u32 last, cur;
3258 int rc;
3259
Tejun Heod4b2bab2007-02-02 16:50:52 +09003260 t = jiffies + msecs_to_jiffies(params[2]);
3261 if (time_before(t, deadline))
3262 deadline = t;
3263
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003264 if ((rc = sata_scr_read(ap, SCR_STATUS, &cur)))
3265 return rc;
3266 cur &= 0xf;
3267
3268 last = cur;
3269 last_jiffies = jiffies;
3270
3271 while (1) {
3272 msleep(interval_msec);
3273 if ((rc = sata_scr_read(ap, SCR_STATUS, &cur)))
3274 return rc;
3275 cur &= 0xf;
3276
3277 /* DET stable? */
3278 if (cur == last) {
Tejun Heod4b2bab2007-02-02 16:50:52 +09003279 if (cur == 1 && time_before(jiffies, deadline))
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003280 continue;
3281 if (time_after(jiffies, last_jiffies + duration))
3282 return 0;
3283 continue;
3284 }
3285
3286 /* unstable, start over */
3287 last = cur;
3288 last_jiffies = jiffies;
3289
Tejun Heod4b2bab2007-02-02 16:50:52 +09003290 /* check deadline */
3291 if (time_after(jiffies, deadline))
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003292 return -EBUSY;
3293 }
3294}
3295
3296/**
3297 * sata_phy_resume - resume SATA phy
3298 * @ap: ATA port to resume SATA phy for
3299 * @params: timing parameters { interval, duratinon, timeout } in msec
Tejun Heod4b2bab2007-02-02 16:50:52 +09003300 * @deadline: deadline jiffies for the operation
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003301 *
3302 * Resume SATA phy of @ap and debounce it.
3303 *
3304 * LOCKING:
3305 * Kernel thread context (may sleep)
3306 *
3307 * RETURNS:
3308 * 0 on success, -errno on failure.
3309 */
Tejun Heod4b2bab2007-02-02 16:50:52 +09003310int sata_phy_resume(struct ata_port *ap, const unsigned long *params,
3311 unsigned long deadline)
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003312{
3313 u32 scontrol;
Tejun Heo81952c52006-05-15 20:57:47 +09003314 int rc;
Tejun Heo7a7921e2006-02-02 18:20:00 +09003315
Tejun Heo81952c52006-05-15 20:57:47 +09003316 if ((rc = sata_scr_read(ap, SCR_CONTROL, &scontrol)))
3317 return rc;
3318
Tejun Heo852ee162006-04-01 01:38:18 +09003319 scontrol = (scontrol & 0x0f0) | 0x300;
Tejun Heo81952c52006-05-15 20:57:47 +09003320
3321 if ((rc = sata_scr_write(ap, SCR_CONTROL, scontrol)))
3322 return rc;
Tejun Heo7a7921e2006-02-02 18:20:00 +09003323
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003324 /* Some PHYs react badly if SStatus is pounded immediately
3325 * after resuming. Delay 200ms before debouncing.
3326 */
3327 msleep(200);
Tejun Heo7a7921e2006-02-02 18:20:00 +09003328
Tejun Heod4b2bab2007-02-02 16:50:52 +09003329 return sata_phy_debounce(ap, params, deadline);
Tejun Heo7a7921e2006-02-02 18:20:00 +09003330}
3331
Tejun Heof5914a42006-05-31 18:27:48 +09003332/**
3333 * ata_std_prereset - prepare for reset
3334 * @ap: ATA port to be reset
Tejun Heod4b2bab2007-02-02 16:50:52 +09003335 * @deadline: deadline jiffies for the operation
Tejun Heof5914a42006-05-31 18:27:48 +09003336 *
Tejun Heob8cffc62007-02-02 16:50:52 +09003337 * @ap is about to be reset. Initialize it. Failure from
3338 * prereset makes libata abort whole reset sequence and give up
3339 * that port, so prereset should be best-effort. It does its
3340 * best to prepare for reset sequence but if things go wrong, it
3341 * should just whine, not fail.
Tejun Heof5914a42006-05-31 18:27:48 +09003342 *
3343 * LOCKING:
3344 * Kernel thread context (may sleep)
3345 *
3346 * RETURNS:
3347 * 0 on success, -errno otherwise.
3348 */
Tejun Heod4b2bab2007-02-02 16:50:52 +09003349int ata_std_prereset(struct ata_port *ap, unsigned long deadline)
Tejun Heof5914a42006-05-31 18:27:48 +09003350{
3351 struct ata_eh_context *ehc = &ap->eh_context;
Tejun Heoe9c83912006-07-03 16:07:26 +09003352 const unsigned long *timing = sata_ehc_deb_timing(ehc);
Tejun Heof5914a42006-05-31 18:27:48 +09003353 int rc;
3354
Tejun Heo31daabd2007-02-02 16:50:52 +09003355 /* handle link resume */
Tejun Heo28324302006-07-03 16:07:26 +09003356 if ((ehc->i.flags & ATA_EHI_RESUME_LINK) &&
3357 (ap->flags & ATA_FLAG_HRST_TO_RESUME))
3358 ehc->i.action |= ATA_EH_HARDRESET;
3359
Tejun Heof5914a42006-05-31 18:27:48 +09003360 /* if we're about to do hardreset, nothing more to do */
3361 if (ehc->i.action & ATA_EH_HARDRESET)
3362 return 0;
3363
3364 /* if SATA, resume phy */
3365 if (ap->cbl == ATA_CBL_SATA) {
Tejun Heod4b2bab2007-02-02 16:50:52 +09003366 rc = sata_phy_resume(ap, timing, deadline);
Tejun Heob8cffc62007-02-02 16:50:52 +09003367 /* whine about phy resume failure but proceed */
3368 if (rc && rc != -EOPNOTSUPP)
Tejun Heof5914a42006-05-31 18:27:48 +09003369 ata_port_printk(ap, KERN_WARNING, "failed to resume "
3370 "link for reset (errno=%d)\n", rc);
Tejun Heof5914a42006-05-31 18:27:48 +09003371 }
3372
3373 /* Wait for !BSY if the controller can wait for the first D2H
3374 * Reg FIS and we don't know that no device is attached.
3375 */
Tejun Heob8cffc62007-02-02 16:50:52 +09003376 if (!(ap->flags & ATA_FLAG_SKIP_D2H_BSY) && !ata_port_offline(ap)) {
3377 rc = ata_wait_ready(ap, deadline);
Tejun Heo6dffaf62007-05-23 11:58:52 +02003378 if (rc && rc != -ENODEV) {
Tejun Heob8cffc62007-02-02 16:50:52 +09003379 ata_port_printk(ap, KERN_WARNING, "device not ready "
3380 "(errno=%d), forcing hardreset\n", rc);
3381 ehc->i.action |= ATA_EH_HARDRESET;
3382 }
3383 }
Tejun Heof5914a42006-05-31 18:27:48 +09003384
3385 return 0;
3386}
3387
Tejun Heoc2bd5802006-01-24 17:05:22 +09003388/**
3389 * ata_std_softreset - reset host port via ATA SRST
3390 * @ap: port to reset
Tejun Heoc2bd5802006-01-24 17:05:22 +09003391 * @classes: resulting classes of attached devices
Tejun Heod4b2bab2007-02-02 16:50:52 +09003392 * @deadline: deadline jiffies for the operation
Tejun Heoc2bd5802006-01-24 17:05:22 +09003393 *
Tejun Heo52783c52006-05-31 18:28:22 +09003394 * Reset host port using ATA SRST.
Tejun Heoc2bd5802006-01-24 17:05:22 +09003395 *
3396 * LOCKING:
3397 * Kernel thread context (may sleep)
3398 *
3399 * RETURNS:
3400 * 0 on success, -errno otherwise.
3401 */
Tejun Heod4b2bab2007-02-02 16:50:52 +09003402int ata_std_softreset(struct ata_port *ap, unsigned int *classes,
3403 unsigned long deadline)
Tejun Heoc2bd5802006-01-24 17:05:22 +09003404{
3405 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
Tejun Heod4b2bab2007-02-02 16:50:52 +09003406 unsigned int devmask = 0;
3407 int rc;
Tejun Heoc2bd5802006-01-24 17:05:22 +09003408 u8 err;
3409
3410 DPRINTK("ENTER\n");
3411
Tejun Heo81952c52006-05-15 20:57:47 +09003412 if (ata_port_offline(ap)) {
Tejun Heo3a397462006-02-10 23:58:48 +09003413 classes[0] = ATA_DEV_NONE;
3414 goto out;
3415 }
3416
Tejun Heoc2bd5802006-01-24 17:05:22 +09003417 /* determine if device 0/1 are present */
3418 if (ata_devchk(ap, 0))
3419 devmask |= (1 << 0);
3420 if (slave_possible && ata_devchk(ap, 1))
3421 devmask |= (1 << 1);
3422
Tejun Heoc2bd5802006-01-24 17:05:22 +09003423 /* select device 0 again */
3424 ap->ops->dev_select(ap, 0);
3425
3426 /* issue bus reset */
3427 DPRINTK("about to softreset, devmask=%x\n", devmask);
Tejun Heod4b2bab2007-02-02 16:50:52 +09003428 rc = ata_bus_softreset(ap, devmask, deadline);
Tejun Heo9b893912007-02-02 16:50:52 +09003429 /* if link is occupied, -ENODEV too is an error */
3430 if (rc && (rc != -ENODEV || sata_scr_valid(ap))) {
Tejun Heod4b2bab2007-02-02 16:50:52 +09003431 ata_port_printk(ap, KERN_ERR, "SRST failed (errno=%d)\n", rc);
3432 return rc;
Tejun Heoc2bd5802006-01-24 17:05:22 +09003433 }
3434
3435 /* determine by signature whether we have ATA or ATAPI devices */
3436 classes[0] = ata_dev_try_classify(ap, 0, &err);
3437 if (slave_possible && err != 0x81)
3438 classes[1] = ata_dev_try_classify(ap, 1, &err);
3439
Tejun Heo3a397462006-02-10 23:58:48 +09003440 out:
Tejun Heoc2bd5802006-01-24 17:05:22 +09003441 DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]);
3442 return 0;
3443}
3444
3445/**
Tejun Heob6103f62006-11-01 17:59:53 +09003446 * sata_port_hardreset - reset port via SATA phy reset
Tejun Heoc2bd5802006-01-24 17:05:22 +09003447 * @ap: port to reset
Tejun Heob6103f62006-11-01 17:59:53 +09003448 * @timing: timing parameters { interval, duratinon, timeout } in msec
Tejun Heod4b2bab2007-02-02 16:50:52 +09003449 * @deadline: deadline jiffies for the operation
Tejun Heoc2bd5802006-01-24 17:05:22 +09003450 *
3451 * SATA phy-reset host port using DET bits of SControl register.
Tejun Heoc2bd5802006-01-24 17:05:22 +09003452 *
3453 * LOCKING:
3454 * Kernel thread context (may sleep)
3455 *
3456 * RETURNS:
3457 * 0 on success, -errno otherwise.
3458 */
Tejun Heod4b2bab2007-02-02 16:50:52 +09003459int sata_port_hardreset(struct ata_port *ap, const unsigned long *timing,
3460 unsigned long deadline)
Tejun Heoc2bd5802006-01-24 17:05:22 +09003461{
Tejun Heo852ee162006-04-01 01:38:18 +09003462 u32 scontrol;
Tejun Heo81952c52006-05-15 20:57:47 +09003463 int rc;
Tejun Heo852ee162006-04-01 01:38:18 +09003464
Tejun Heoc2bd5802006-01-24 17:05:22 +09003465 DPRINTK("ENTER\n");
3466
Tejun Heo3c567b72006-05-15 20:57:23 +09003467 if (sata_set_spd_needed(ap)) {
Tejun Heo1c3fae42006-04-02 20:53:28 +09003468 /* SATA spec says nothing about how to reconfigure
3469 * spd. To be on the safe side, turn off phy during
3470 * reconfiguration. This works for at least ICH7 AHCI
3471 * and Sil3124.
3472 */
Tejun Heo81952c52006-05-15 20:57:47 +09003473 if ((rc = sata_scr_read(ap, SCR_CONTROL, &scontrol)))
Tejun Heob6103f62006-11-01 17:59:53 +09003474 goto out;
Tejun Heo81952c52006-05-15 20:57:47 +09003475
Martin Hicksa34b6fc2006-07-05 15:06:13 -04003476 scontrol = (scontrol & 0x0f0) | 0x304;
Tejun Heo81952c52006-05-15 20:57:47 +09003477
3478 if ((rc = sata_scr_write(ap, SCR_CONTROL, scontrol)))
Tejun Heob6103f62006-11-01 17:59:53 +09003479 goto out;
Tejun Heo1c3fae42006-04-02 20:53:28 +09003480
Tejun Heo3c567b72006-05-15 20:57:23 +09003481 sata_set_spd(ap);
Tejun Heo1c3fae42006-04-02 20:53:28 +09003482 }
3483
3484 /* issue phy wake/reset */
Tejun Heo81952c52006-05-15 20:57:47 +09003485 if ((rc = sata_scr_read(ap, SCR_CONTROL, &scontrol)))
Tejun Heob6103f62006-11-01 17:59:53 +09003486 goto out;
Tejun Heo81952c52006-05-15 20:57:47 +09003487
Tejun Heo852ee162006-04-01 01:38:18 +09003488 scontrol = (scontrol & 0x0f0) | 0x301;
Tejun Heo81952c52006-05-15 20:57:47 +09003489
3490 if ((rc = sata_scr_write_flush(ap, SCR_CONTROL, scontrol)))
Tejun Heob6103f62006-11-01 17:59:53 +09003491 goto out;
Tejun Heoc2bd5802006-01-24 17:05:22 +09003492
Tejun Heo1c3fae42006-04-02 20:53:28 +09003493 /* Couldn't find anything in SATA I/II specs, but AHCI-1.1
Tejun Heoc2bd5802006-01-24 17:05:22 +09003494 * 10.4.2 says at least 1 ms.
3495 */
3496 msleep(1);
3497
Tejun Heo1c3fae42006-04-02 20:53:28 +09003498 /* bring phy back */
Tejun Heod4b2bab2007-02-02 16:50:52 +09003499 rc = sata_phy_resume(ap, timing, deadline);
Tejun Heob6103f62006-11-01 17:59:53 +09003500 out:
3501 DPRINTK("EXIT, rc=%d\n", rc);
3502 return rc;
3503}
3504
3505/**
3506 * sata_std_hardreset - reset host port via SATA phy reset
3507 * @ap: port to reset
3508 * @class: resulting class of attached device
Tejun Heod4b2bab2007-02-02 16:50:52 +09003509 * @deadline: deadline jiffies for the operation
Tejun Heob6103f62006-11-01 17:59:53 +09003510 *
3511 * SATA phy-reset host port using DET bits of SControl register,
3512 * wait for !BSY and classify the attached device.
3513 *
3514 * LOCKING:
3515 * Kernel thread context (may sleep)
3516 *
3517 * RETURNS:
3518 * 0 on success, -errno otherwise.
3519 */
Tejun Heod4b2bab2007-02-02 16:50:52 +09003520int sata_std_hardreset(struct ata_port *ap, unsigned int *class,
3521 unsigned long deadline)
Tejun Heob6103f62006-11-01 17:59:53 +09003522{
3523 const unsigned long *timing = sata_ehc_deb_timing(&ap->eh_context);
3524 int rc;
3525
3526 DPRINTK("ENTER\n");
3527
3528 /* do hardreset */
Tejun Heod4b2bab2007-02-02 16:50:52 +09003529 rc = sata_port_hardreset(ap, timing, deadline);
Tejun Heob6103f62006-11-01 17:59:53 +09003530 if (rc) {
3531 ata_port_printk(ap, KERN_ERR,
3532 "COMRESET failed (errno=%d)\n", rc);
3533 return rc;
3534 }
Tejun Heoc2bd5802006-01-24 17:05:22 +09003535
Tejun Heoc2bd5802006-01-24 17:05:22 +09003536 /* TODO: phy layer with polling, timeouts, etc. */
Tejun Heo81952c52006-05-15 20:57:47 +09003537 if (ata_port_offline(ap)) {
Tejun Heoc2bd5802006-01-24 17:05:22 +09003538 *class = ATA_DEV_NONE;
3539 DPRINTK("EXIT, link offline\n");
3540 return 0;
3541 }
3542
Tejun Heo34fee222007-02-02 15:29:27 +09003543 /* wait a while before checking status, see SRST for more info */
3544 msleep(150);
3545
Tejun Heod4b2bab2007-02-02 16:50:52 +09003546 rc = ata_wait_ready(ap, deadline);
Tejun Heo9b893912007-02-02 16:50:52 +09003547 /* link occupied, -ENODEV too is an error */
3548 if (rc) {
Tejun Heof15a1da2006-05-15 20:57:56 +09003549 ata_port_printk(ap, KERN_ERR,
Tejun Heod4b2bab2007-02-02 16:50:52 +09003550 "COMRESET failed (errno=%d)\n", rc);
3551 return rc;
Tejun Heoc2bd5802006-01-24 17:05:22 +09003552 }
3553
Tejun Heo3a397462006-02-10 23:58:48 +09003554 ap->ops->dev_select(ap, 0); /* probably unnecessary */
3555
Tejun Heoc2bd5802006-01-24 17:05:22 +09003556 *class = ata_dev_try_classify(ap, 0, NULL);
3557
3558 DPRINTK("EXIT, class=%u\n", *class);
3559 return 0;
3560}
3561
3562/**
3563 * ata_std_postreset - standard postreset callback
3564 * @ap: the target ata_port
3565 * @classes: classes of attached devices
3566 *
3567 * This function is invoked after a successful reset. Note that
3568 * the device might have been reset more than once using
3569 * different reset methods before postreset is invoked.
Tejun Heoc2bd5802006-01-24 17:05:22 +09003570 *
Tejun Heoc2bd5802006-01-24 17:05:22 +09003571 * LOCKING:
3572 * Kernel thread context (may sleep)
3573 */
3574void ata_std_postreset(struct ata_port *ap, unsigned int *classes)
3575{
Tejun Heodc2b3512006-05-15 20:58:00 +09003576 u32 serror;
3577
Tejun Heoc2bd5802006-01-24 17:05:22 +09003578 DPRINTK("ENTER\n");
3579
Tejun Heoc2bd5802006-01-24 17:05:22 +09003580 /* print link status */
Tejun Heo81952c52006-05-15 20:57:47 +09003581 sata_print_link_status(ap);
Tejun Heoc2bd5802006-01-24 17:05:22 +09003582
Tejun Heodc2b3512006-05-15 20:58:00 +09003583 /* clear SError */
3584 if (sata_scr_read(ap, SCR_ERROR, &serror) == 0)
3585 sata_scr_write(ap, SCR_ERROR, serror);
3586
Tejun Heo3a397462006-02-10 23:58:48 +09003587 /* re-enable interrupts */
Akira Iguchi83625002007-01-26 16:27:32 +09003588 if (!ap->ops->error_handler)
3589 ap->ops->irq_on(ap);
Tejun Heoc2bd5802006-01-24 17:05:22 +09003590
3591 /* is double-select really necessary? */
3592 if (classes[0] != ATA_DEV_NONE)
3593 ap->ops->dev_select(ap, 1);
3594 if (classes[1] != ATA_DEV_NONE)
3595 ap->ops->dev_select(ap, 0);
3596
Tejun Heo3a397462006-02-10 23:58:48 +09003597 /* bail out if no device is present */
3598 if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
3599 DPRINTK("EXIT, no device\n");
3600 return;
3601 }
3602
3603 /* set up device control */
Tejun Heo0d5ff562007-02-01 15:06:36 +09003604 if (ap->ioaddr.ctl_addr)
3605 iowrite8(ap->ctl, ap->ioaddr.ctl_addr);
Tejun Heoc2bd5802006-01-24 17:05:22 +09003606
3607 DPRINTK("EXIT\n");
3608}
3609
Tejun Heoa62c0fc2006-01-24 17:05:22 +09003610/**
Tejun Heo623a3122006-03-05 17:55:58 +09003611 * ata_dev_same_device - Determine whether new ID matches configured device
Tejun Heo623a3122006-03-05 17:55:58 +09003612 * @dev: device to compare against
3613 * @new_class: class of the new device
3614 * @new_id: IDENTIFY page of the new device
3615 *
3616 * Compare @new_class and @new_id against @dev and determine
3617 * whether @dev is the device indicated by @new_class and
3618 * @new_id.
3619 *
3620 * LOCKING:
3621 * None.
3622 *
3623 * RETURNS:
3624 * 1 if @dev matches @new_class and @new_id, 0 otherwise.
3625 */
Tejun Heo3373efd2006-05-15 20:57:53 +09003626static int ata_dev_same_device(struct ata_device *dev, unsigned int new_class,
3627 const u16 *new_id)
Tejun Heo623a3122006-03-05 17:55:58 +09003628{
3629 const u16 *old_id = dev->id;
Tejun Heoa0cf7332007-01-02 20:18:49 +09003630 unsigned char model[2][ATA_ID_PROD_LEN + 1];
3631 unsigned char serial[2][ATA_ID_SERNO_LEN + 1];
Tejun Heo623a3122006-03-05 17:55:58 +09003632
3633 if (dev->class != new_class) {
Tejun Heof15a1da2006-05-15 20:57:56 +09003634 ata_dev_printk(dev, KERN_INFO, "class mismatch %d != %d\n",
3635 dev->class, new_class);
Tejun Heo623a3122006-03-05 17:55:58 +09003636 return 0;
3637 }
3638
Tejun Heoa0cf7332007-01-02 20:18:49 +09003639 ata_id_c_string(old_id, model[0], ATA_ID_PROD, sizeof(model[0]));
3640 ata_id_c_string(new_id, model[1], ATA_ID_PROD, sizeof(model[1]));
3641 ata_id_c_string(old_id, serial[0], ATA_ID_SERNO, sizeof(serial[0]));
3642 ata_id_c_string(new_id, serial[1], ATA_ID_SERNO, sizeof(serial[1]));
Tejun Heo623a3122006-03-05 17:55:58 +09003643
3644 if (strcmp(model[0], model[1])) {
Tejun Heof15a1da2006-05-15 20:57:56 +09003645 ata_dev_printk(dev, KERN_INFO, "model number mismatch "
3646 "'%s' != '%s'\n", model[0], model[1]);
Tejun Heo623a3122006-03-05 17:55:58 +09003647 return 0;
3648 }
3649
3650 if (strcmp(serial[0], serial[1])) {
Tejun Heof15a1da2006-05-15 20:57:56 +09003651 ata_dev_printk(dev, KERN_INFO, "serial number mismatch "
3652 "'%s' != '%s'\n", serial[0], serial[1]);
Tejun Heo623a3122006-03-05 17:55:58 +09003653 return 0;
3654 }
3655
Tejun Heo623a3122006-03-05 17:55:58 +09003656 return 1;
3657}
3658
3659/**
Tejun Heofe309112007-05-15 03:28:15 +09003660 * ata_dev_reread_id - Re-read IDENTIFY data
Henrik Kretzschmar3fae4502007-06-19 10:10:50 +02003661 * @dev: target ATA device
Tejun Heobff04642006-11-10 18:08:10 +09003662 * @readid_flags: read ID flags
Tejun Heo623a3122006-03-05 17:55:58 +09003663 *
3664 * Re-read IDENTIFY page and make sure @dev is still attached to
3665 * the port.
3666 *
3667 * LOCKING:
3668 * Kernel thread context (may sleep)
3669 *
3670 * RETURNS:
3671 * 0 on success, negative errno otherwise
3672 */
Tejun Heofe309112007-05-15 03:28:15 +09003673int ata_dev_reread_id(struct ata_device *dev, unsigned int readid_flags)
Tejun Heo623a3122006-03-05 17:55:58 +09003674{
Tejun Heo5eb45c02006-04-02 18:51:52 +09003675 unsigned int class = dev->class;
Tejun Heof15a1da2006-05-15 20:57:56 +09003676 u16 *id = (void *)dev->ap->sector_buf;
Tejun Heo623a3122006-03-05 17:55:58 +09003677 int rc;
3678
Tejun Heofe635c72006-05-15 20:57:35 +09003679 /* read ID data */
Tejun Heobff04642006-11-10 18:08:10 +09003680 rc = ata_dev_read_id(dev, &class, readid_flags, id);
Tejun Heo623a3122006-03-05 17:55:58 +09003681 if (rc)
Tejun Heofe309112007-05-15 03:28:15 +09003682 return rc;
Tejun Heo623a3122006-03-05 17:55:58 +09003683
3684 /* is the device still there? */
Tejun Heofe309112007-05-15 03:28:15 +09003685 if (!ata_dev_same_device(dev, class, id))
3686 return -ENODEV;
Tejun Heo623a3122006-03-05 17:55:58 +09003687
Tejun Heofe635c72006-05-15 20:57:35 +09003688 memcpy(dev->id, id, sizeof(id[0]) * ATA_ID_WORDS);
Tejun Heofe309112007-05-15 03:28:15 +09003689 return 0;
3690}
3691
3692/**
3693 * ata_dev_revalidate - Revalidate ATA device
3694 * @dev: device to revalidate
3695 * @readid_flags: read ID flags
3696 *
3697 * Re-read IDENTIFY page, make sure @dev is still attached to the
3698 * port and reconfigure it according to the new IDENTIFY page.
3699 *
3700 * LOCKING:
3701 * Kernel thread context (may sleep)
3702 *
3703 * RETURNS:
3704 * 0 on success, negative errno otherwise
3705 */
3706int ata_dev_revalidate(struct ata_device *dev, unsigned int readid_flags)
3707{
Tejun Heo6ddcd3b2007-05-15 03:28:15 +09003708 u64 n_sectors = dev->n_sectors;
Tejun Heofe309112007-05-15 03:28:15 +09003709 int rc;
3710
3711 if (!ata_dev_enabled(dev))
3712 return -ENODEV;
3713
3714 /* re-read ID */
3715 rc = ata_dev_reread_id(dev, readid_flags);
3716 if (rc)
3717 goto fail;
Tejun Heo623a3122006-03-05 17:55:58 +09003718
3719 /* configure device according to the new ID */
Tejun Heoefdaedc2006-11-01 18:38:52 +09003720 rc = ata_dev_configure(dev);
Tejun Heo6ddcd3b2007-05-15 03:28:15 +09003721 if (rc)
3722 goto fail;
3723
3724 /* verify n_sectors hasn't changed */
3725 if (dev->class == ATA_DEV_ATA && dev->n_sectors != n_sectors) {
3726 ata_dev_printk(dev, KERN_INFO, "n_sectors mismatch "
3727 "%llu != %llu\n",
3728 (unsigned long long)n_sectors,
3729 (unsigned long long)dev->n_sectors);
3730 rc = -ENODEV;
3731 goto fail;
3732 }
3733
3734 return 0;
Tejun Heo623a3122006-03-05 17:55:58 +09003735
3736 fail:
Tejun Heof15a1da2006-05-15 20:57:56 +09003737 ata_dev_printk(dev, KERN_ERR, "revalidation failed (errno=%d)\n", rc);
Tejun Heo623a3122006-03-05 17:55:58 +09003738 return rc;
3739}
3740
Alan Cox6919a0a2006-10-27 19:08:46 -07003741struct ata_blacklist_entry {
3742 const char *model_num;
3743 const char *model_rev;
3744 unsigned long horkage;
3745};
3746
3747static const struct ata_blacklist_entry ata_device_blacklist [] = {
3748 /* Devices with DMA related problems under Linux */
3749 { "WDC AC11000H", NULL, ATA_HORKAGE_NODMA },
3750 { "WDC AC22100H", NULL, ATA_HORKAGE_NODMA },
3751 { "WDC AC32500H", NULL, ATA_HORKAGE_NODMA },
3752 { "WDC AC33100H", NULL, ATA_HORKAGE_NODMA },
3753 { "WDC AC31600H", NULL, ATA_HORKAGE_NODMA },
3754 { "WDC AC32100H", "24.09P07", ATA_HORKAGE_NODMA },
3755 { "WDC AC23200L", "21.10N21", ATA_HORKAGE_NODMA },
3756 { "Compaq CRD-8241B", NULL, ATA_HORKAGE_NODMA },
3757 { "CRD-8400B", NULL, ATA_HORKAGE_NODMA },
3758 { "CRD-8480B", NULL, ATA_HORKAGE_NODMA },
3759 { "CRD-8482B", NULL, ATA_HORKAGE_NODMA },
3760 { "CRD-84", NULL, ATA_HORKAGE_NODMA },
3761 { "SanDisk SDP3B", NULL, ATA_HORKAGE_NODMA },
3762 { "SanDisk SDP3B-64", NULL, ATA_HORKAGE_NODMA },
3763 { "SANYO CD-ROM CRD", NULL, ATA_HORKAGE_NODMA },
3764 { "HITACHI CDR-8", NULL, ATA_HORKAGE_NODMA },
3765 { "HITACHI CDR-8335", NULL, ATA_HORKAGE_NODMA },
3766 { "HITACHI CDR-8435", NULL, ATA_HORKAGE_NODMA },
3767 { "Toshiba CD-ROM XM-6202B", NULL, ATA_HORKAGE_NODMA },
3768 { "TOSHIBA CD-ROM XM-1702BC", NULL, ATA_HORKAGE_NODMA },
3769 { "CD-532E-A", NULL, ATA_HORKAGE_NODMA },
3770 { "E-IDE CD-ROM CR-840",NULL, ATA_HORKAGE_NODMA },
3771 { "CD-ROM Drive/F5A", NULL, ATA_HORKAGE_NODMA },
3772 { "WPI CDD-820", NULL, ATA_HORKAGE_NODMA },
3773 { "SAMSUNG CD-ROM SC-148C", NULL, ATA_HORKAGE_NODMA },
3774 { "SAMSUNG CD-ROM SC", NULL, ATA_HORKAGE_NODMA },
Alan Cox6919a0a2006-10-27 19:08:46 -07003775 { "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,ATA_HORKAGE_NODMA },
3776 { "_NEC DV5800A", NULL, ATA_HORKAGE_NODMA },
3777 { "SAMSUNG CD-ROM SN-124","N001", ATA_HORKAGE_NODMA },
Dave Jones39f19882007-05-21 14:31:03 -04003778 { "Seagate STT20000A", NULL, ATA_HORKAGE_NODMA },
Tejun Heo5acd50f2007-06-10 14:52:36 +09003779 { "IOMEGA ZIP 250 ATAPI", NULL, ATA_HORKAGE_NODMA }, /* temporary fix */
Alan Cox6919a0a2006-10-27 19:08:46 -07003780
Albert Lee18d6e9d2007-04-02 11:34:15 +08003781 /* Weird ATAPI devices */
Albert Lee6f23a312007-04-02 11:39:25 +08003782 { "TORiSAN DVD-ROM DRD-N216", NULL, ATA_HORKAGE_MAX_SEC_128 |
3783 ATA_HORKAGE_DMA_RW_ONLY },
Albert Lee18d6e9d2007-04-02 11:34:15 +08003784
Alan Cox6919a0a2006-10-27 19:08:46 -07003785 /* Devices we expect to fail diagnostics */
3786
3787 /* Devices where NCQ should be avoided */
3788 /* NCQ is slow */
3789 { "WDC WD740ADFD-00", NULL, ATA_HORKAGE_NONCQ },
Tejun Heo09125ea2007-02-28 15:21:23 +09003790 /* http://thread.gmane.org/gmane.linux.ide/14907 */
3791 { "FUJITSU MHT2060BH", NULL, ATA_HORKAGE_NONCQ },
Paul Rolland7acfaf32007-03-26 21:43:44 -08003792 /* NCQ is broken */
3793 { "Maxtor 6L250S0", "BANC1G10", ATA_HORKAGE_NONCQ },
Jeff Garzik471e44b2007-05-28 09:00:05 -04003794 { "Maxtor 6B200M0", "BANC1B10", ATA_HORKAGE_NONCQ },
Jens Axboe96442922007-03-30 09:27:58 +02003795 /* NCQ hard hangs device under heavier load, needs hard power cycle */
3796 { "Maxtor 6B250S0", "BANC1B70", ATA_HORKAGE_NONCQ },
Robert Hancock36e337d2007-04-02 22:05:29 -06003797 /* Blacklist entries taken from Silicon Image 3124/3132
3798 Windows driver .inf file - also several Linux problem reports */
3799 { "HTS541060G9SA00", "MB3OC60D", ATA_HORKAGE_NONCQ, },
3800 { "HTS541080G9SA00", "MB4OC60D", ATA_HORKAGE_NONCQ, },
3801 { "HTS541010G9SA00", "MBZOC60D", ATA_HORKAGE_NONCQ, },
Tejun Heobd9c5a32007-06-08 22:20:59 +09003802 /* Drives which do spurious command completion */
3803 { "HTS541680J9SA00", "SB2IC7EP", ATA_HORKAGE_NONCQ, },
Tejun Heo2f8fceb2007-06-18 14:38:41 +09003804 { "HTS541612J9SA00", "SBDIC7JP", ATA_HORKAGE_NONCQ, },
3805 { "WDC WD740ADFD-00NLR1", NULL, ATA_HORKAGE_NONCQ, },
Alan Cox6919a0a2006-10-27 19:08:46 -07003806
3807 /* Devices with NCQ limits */
3808
3809 /* End Marker */
3810 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811};
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05003812
Alan Cox6919a0a2006-10-27 19:08:46 -07003813unsigned long ata_device_blacklisted(const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814{
Tejun Heo8bfa79f2007-01-02 20:19:40 +09003815 unsigned char model_num[ATA_ID_PROD_LEN + 1];
3816 unsigned char model_rev[ATA_ID_FW_REV_LEN + 1];
Alan Cox6919a0a2006-10-27 19:08:46 -07003817 const struct ata_blacklist_entry *ad = ata_device_blacklist;
Albert Lee3a778272006-06-22 13:00:25 +08003818
Tejun Heo8bfa79f2007-01-02 20:19:40 +09003819 ata_id_c_string(dev->id, model_num, ATA_ID_PROD, sizeof(model_num));
3820 ata_id_c_string(dev->id, model_rev, ATA_ID_FW_REV, sizeof(model_rev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821
Alan Cox6919a0a2006-10-27 19:08:46 -07003822 while (ad->model_num) {
Tejun Heo8bfa79f2007-01-02 20:19:40 +09003823 if (!strcmp(ad->model_num, model_num)) {
Alan Cox6919a0a2006-10-27 19:08:46 -07003824 if (ad->model_rev == NULL)
3825 return ad->horkage;
Tejun Heo8bfa79f2007-01-02 20:19:40 +09003826 if (!strcmp(ad->model_rev, model_rev))
Alan Cox6919a0a2006-10-27 19:08:46 -07003827 return ad->horkage;
Alan Coxf4b15fe2006-03-22 15:54:04 +00003828 }
Alan Cox6919a0a2006-10-27 19:08:46 -07003829 ad++;
Alan Coxf4b15fe2006-03-22 15:54:04 +00003830 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831 return 0;
3832}
3833
Alan Cox6919a0a2006-10-27 19:08:46 -07003834static int ata_dma_blacklisted(const struct ata_device *dev)
3835{
3836 /* We don't support polling DMA.
3837 * DMA blacklist those ATAPI devices with CDB-intr (and use PIO)
3838 * if the LLDD handles only interrupts in the HSM_ST_LAST state.
3839 */
3840 if ((dev->ap->flags & ATA_FLAG_PIO_POLLING) &&
3841 (dev->flags & ATA_DFLAG_CDB_INTR))
3842 return 1;
3843 return (ata_device_blacklisted(dev) & ATA_HORKAGE_NODMA) ? 1 : 0;
3844}
3845
Tejun Heoa6d5a512006-03-06 04:31:57 +09003846/**
3847 * ata_dev_xfermask - Compute supported xfermask of the given device
Tejun Heoa6d5a512006-03-06 04:31:57 +09003848 * @dev: Device to compute xfermask for
3849 *
Tejun Heoacf356b2006-03-24 14:07:50 +09003850 * Compute supported xfermask of @dev and store it in
3851 * dev->*_mask. This function is responsible for applying all
3852 * known limits including host controller limits, device
3853 * blacklist, etc...
Tejun Heoa6d5a512006-03-06 04:31:57 +09003854 *
3855 * LOCKING:
3856 * None.
Tejun Heoa6d5a512006-03-06 04:31:57 +09003857 */
Tejun Heo3373efd2006-05-15 20:57:53 +09003858static void ata_dev_xfermask(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859{
Tejun Heo3373efd2006-05-15 20:57:53 +09003860 struct ata_port *ap = dev->ap;
Jeff Garzikcca39742006-08-24 03:19:22 -04003861 struct ata_host *host = ap->host;
Tejun Heoa6d5a512006-03-06 04:31:57 +09003862 unsigned long xfer_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863
Tejun Heo37deecb2006-08-10 16:59:07 +09003864 /* controller modes available */
Tejun Heo565083e2006-04-02 17:54:47 +09003865 xfer_mask = ata_pack_xfermask(ap->pio_mask,
3866 ap->mwdma_mask, ap->udma_mask);
3867
Robert Hancock8343f882007-03-06 02:37:51 -08003868 /* drive modes available */
Tejun Heo37deecb2006-08-10 16:59:07 +09003869 xfer_mask &= ata_pack_xfermask(dev->pio_mask,
3870 dev->mwdma_mask, dev->udma_mask);
3871 xfer_mask &= ata_id_xfermask(dev->id);
Tejun Heo565083e2006-04-02 17:54:47 +09003872
Alan Coxb352e572006-08-10 18:52:12 +01003873 /*
3874 * CFA Advanced TrueIDE timings are not allowed on a shared
3875 * cable
3876 */
3877 if (ata_dev_pair(dev)) {
3878 /* No PIO5 or PIO6 */
3879 xfer_mask &= ~(0x03 << (ATA_SHIFT_PIO + 5));
3880 /* No MWDMA3 or MWDMA 4 */
3881 xfer_mask &= ~(0x03 << (ATA_SHIFT_MWDMA + 3));
3882 }
3883
Tejun Heo37deecb2006-08-10 16:59:07 +09003884 if (ata_dma_blacklisted(dev)) {
3885 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
Tejun Heof15a1da2006-05-15 20:57:56 +09003886 ata_dev_printk(dev, KERN_WARNING,
3887 "device is on DMA blacklist, disabling DMA\n");
Tejun Heo37deecb2006-08-10 16:59:07 +09003888 }
Tejun Heoa6d5a512006-03-06 04:31:57 +09003889
Petr Vandrovec14d66ab2007-03-08 10:12:12 +01003890 if ((host->flags & ATA_HOST_SIMPLEX) &&
3891 host->simplex_claimed && host->simplex_claimed != ap) {
Tejun Heo37deecb2006-08-10 16:59:07 +09003892 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
3893 ata_dev_printk(dev, KERN_WARNING, "simplex DMA is claimed by "
3894 "other device, disabling DMA\n");
Alan Cox5444a6f2006-03-27 18:58:20 +01003895 }
Tejun Heo565083e2006-04-02 17:54:47 +09003896
Jeff Garzike4246752007-03-09 09:56:46 -05003897 if (ap->flags & ATA_FLAG_NO_IORDY)
3898 xfer_mask &= ata_pio_mask_no_iordy(dev);
3899
Alan Cox5444a6f2006-03-27 18:58:20 +01003900 if (ap->ops->mode_filter)
Alan Coxa76b62ca2007-03-09 09:34:07 -05003901 xfer_mask = ap->ops->mode_filter(dev, xfer_mask);
Alan Cox5444a6f2006-03-27 18:58:20 +01003902
Robert Hancock8343f882007-03-06 02:37:51 -08003903 /* Apply cable rule here. Don't apply it early because when
3904 * we handle hot plug the cable type can itself change.
3905 * Check this last so that we know if the transfer rate was
3906 * solely limited by the cable.
3907 * Unknown or 80 wire cables reported host side are checked
3908 * drive side as well. Cases where we know a 40wire cable
3909 * is used safely for 80 are not checked here.
3910 */
3911 if (xfer_mask & (0xF8 << ATA_SHIFT_UDMA))
3912 /* UDMA/44 or higher would be available */
3913 if((ap->cbl == ATA_CBL_PATA40) ||
3914 (ata_drive_40wire(dev->id) &&
3915 (ap->cbl == ATA_CBL_PATA_UNK ||
3916 ap->cbl == ATA_CBL_PATA80))) {
3917 ata_dev_printk(dev, KERN_WARNING,
3918 "limited to UDMA/33 due to 40-wire cable\n");
3919 xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
3920 }
3921
Tejun Heo565083e2006-04-02 17:54:47 +09003922 ata_unpack_xfermask(xfer_mask, &dev->pio_mask,
3923 &dev->mwdma_mask, &dev->udma_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924}
3925
Linus Torvalds1da177e2005-04-16 15:20:36 -07003926/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003927 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928 * @dev: Device to which command will be sent
3929 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04003930 * Issue SET FEATURES - XFER MODE command to device @dev
3931 * on port @ap.
3932 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003934 * PCI/etc. bus probe sem.
Tejun Heo83206a22006-03-24 15:25:31 +09003935 *
3936 * RETURNS:
3937 * 0 on success, AC_ERR_* mask otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938 */
3939
Tejun Heo3373efd2006-05-15 20:57:53 +09003940static unsigned int ata_dev_set_xfermode(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941{
Tejun Heoa0123702005-12-13 14:49:31 +09003942 struct ata_taskfile tf;
Tejun Heo83206a22006-03-24 15:25:31 +09003943 unsigned int err_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003944
3945 /* set up set-features taskfile */
3946 DPRINTK("set features - xfer mode\n");
3947
Tejun Heo464cf172007-05-27 15:10:40 +02003948 /* Some controllers and ATAPI devices show flaky interrupt
3949 * behavior after setting xfer mode. Use polling instead.
3950 */
Tejun Heo3373efd2006-05-15 20:57:53 +09003951 ata_tf_init(dev, &tf);
Tejun Heoa0123702005-12-13 14:49:31 +09003952 tf.command = ATA_CMD_SET_FEATURES;
3953 tf.feature = SETFEATURES_XFER;
Tejun Heo464cf172007-05-27 15:10:40 +02003954 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_POLLING;
Tejun Heoa0123702005-12-13 14:49:31 +09003955 tf.protocol = ATA_PROT_NODATA;
3956 tf.nsect = dev->xfer_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003957
Tejun Heo3373efd2006-05-15 20:57:53 +09003958 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959
Tejun Heo83206a22006-03-24 15:25:31 +09003960 DPRINTK("EXIT, err_mask=%x\n", err_mask);
3961 return err_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962}
3963
3964/**
Albert Lee8bf62ece2005-05-12 15:29:42 -04003965 * ata_dev_init_params - Issue INIT DEV PARAMS command
Albert Lee8bf62ece2005-05-12 15:29:42 -04003966 * @dev: Device to which command will be sent
Randy Dunlape2a7f772006-05-18 10:50:18 -07003967 * @heads: Number of heads (taskfile parameter)
3968 * @sectors: Number of sectors (taskfile parameter)
Albert Lee8bf62ece2005-05-12 15:29:42 -04003969 *
3970 * LOCKING:
Tejun Heo6aff8f12006-02-15 18:24:09 +09003971 * Kernel thread context (may sleep)
3972 *
3973 * RETURNS:
3974 * 0 on success, AC_ERR_* mask otherwise.
Albert Lee8bf62ece2005-05-12 15:29:42 -04003975 */
Tejun Heo3373efd2006-05-15 20:57:53 +09003976static unsigned int ata_dev_init_params(struct ata_device *dev,
3977 u16 heads, u16 sectors)
Albert Lee8bf62ece2005-05-12 15:29:42 -04003978{
Tejun Heoa0123702005-12-13 14:49:31 +09003979 struct ata_taskfile tf;
Tejun Heo6aff8f12006-02-15 18:24:09 +09003980 unsigned int err_mask;
Albert Lee8bf62ece2005-05-12 15:29:42 -04003981
3982 /* Number of sectors per track 1-255. Number of heads 1-16 */
3983 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
Albert Lee00b6f5e2006-03-27 16:39:18 +08003984 return AC_ERR_INVALID;
Albert Lee8bf62ece2005-05-12 15:29:42 -04003985
3986 /* set up init dev params taskfile */
3987 DPRINTK("init dev params \n");
3988
Tejun Heo3373efd2006-05-15 20:57:53 +09003989 ata_tf_init(dev, &tf);
Tejun Heoa0123702005-12-13 14:49:31 +09003990 tf.command = ATA_CMD_INIT_DEV_PARAMS;
3991 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
3992 tf.protocol = ATA_PROT_NODATA;
3993 tf.nsect = sectors;
3994 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
Albert Lee8bf62ece2005-05-12 15:29:42 -04003995
Tejun Heo3373efd2006-05-15 20:57:53 +09003996 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
Albert Lee8bf62ece2005-05-12 15:29:42 -04003997
Tejun Heo6aff8f12006-02-15 18:24:09 +09003998 DPRINTK("EXIT, err_mask=%x\n", err_mask);
3999 return err_mask;
Albert Lee8bf62ece2005-05-12 15:29:42 -04004000}
4001
4002/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004003 * ata_sg_clean - Unmap DMA memory associated with command
4004 * @qc: Command containing DMA memory to be released
4005 *
4006 * Unmap all mapped DMA memory associated with this command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004007 *
4008 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004009 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004010 */
Tejun Heo70e6ad02006-11-14 22:47:10 +09004011void ata_sg_clean(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004012{
4013 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004014 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004015 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004016 void *pad_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004017
Tejun Heoa46314742006-02-11 19:11:13 +09004018 WARN_ON(!(qc->flags & ATA_QCFLAG_DMAMAP));
4019 WARN_ON(sg == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004020
4021 if (qc->flags & ATA_QCFLAG_SINGLE)
Jeff Garzikf1318832006-02-20 16:55:56 -05004022 WARN_ON(qc->n_elem > 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05004024 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004026 /* if we padded the buffer out to 32-bit bound, and data
4027 * xfer direction is from-device, we must copy from the
4028 * pad buffer back into the supplied buffer
4029 */
4030 if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
4031 pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
4032
4033 if (qc->flags & ATA_QCFLAG_SG) {
Jeff Garzike1410f22005-11-14 14:06:26 -05004034 if (qc->n_elem)
Brian King2f1f6102006-03-23 17:30:15 -06004035 dma_unmap_sg(ap->dev, sg, qc->n_elem, dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004036 /* restore last sg */
4037 sg[qc->orig_n_elem - 1].length += qc->pad_len;
4038 if (pad_buf) {
4039 struct scatterlist *psg = &qc->pad_sgent;
4040 void *addr = kmap_atomic(psg->page, KM_IRQ0);
4041 memcpy(addr + psg->offset, pad_buf, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05004042 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004043 }
4044 } else {
Tejun Heo2e242fa2006-02-20 23:48:38 +09004045 if (qc->n_elem)
Brian King2f1f6102006-03-23 17:30:15 -06004046 dma_unmap_single(ap->dev,
Jeff Garzike1410f22005-11-14 14:06:26 -05004047 sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
4048 dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004049 /* restore sg */
4050 sg->length += qc->pad_len;
4051 if (pad_buf)
4052 memcpy(qc->buf_virt + sg->length - qc->pad_len,
4053 pad_buf, qc->pad_len);
4054 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055
4056 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004057 qc->__sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058}
4059
4060/**
4061 * ata_fill_sg - Fill PCI IDE PRD table
4062 * @qc: Metadata associated with taskfile to be transferred
4063 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04004064 * Fill PCI IDE PRD (scatter-gather) table with segments
4065 * associated with the current disk command.
4066 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004068 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069 *
4070 */
4071static void ata_fill_sg(struct ata_queued_cmd *qc)
4072{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004074 struct scatterlist *sg;
4075 unsigned int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076
Tejun Heoa46314742006-02-11 19:11:13 +09004077 WARN_ON(qc->__sg == NULL);
Jeff Garzikf1318832006-02-20 16:55:56 -05004078 WARN_ON(qc->n_elem == 0 && qc->pad_len == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079
4080 idx = 0;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004081 ata_for_each_sg(sg, qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082 u32 addr, offset;
4083 u32 sg_len, len;
4084
4085 /* determine if physical DMA addr spans 64K boundary.
4086 * Note h/w doesn't support 64-bit, so we unconditionally
4087 * truncate dma_addr_t to u32.
4088 */
4089 addr = (u32) sg_dma_address(sg);
4090 sg_len = sg_dma_len(sg);
4091
4092 while (sg_len) {
4093 offset = addr & 0xffff;
4094 len = sg_len;
4095 if ((offset + sg_len) > 0x10000)
4096 len = 0x10000 - offset;
4097
4098 ap->prd[idx].addr = cpu_to_le32(addr);
4099 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
4100 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
4101
4102 idx++;
4103 sg_len -= len;
4104 addr += len;
4105 }
4106 }
4107
4108 if (idx)
4109 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
4110}
4111/**
4112 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
4113 * @qc: Metadata associated with taskfile to check
4114 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04004115 * Allow low-level driver to filter ATA PACKET commands, returning
4116 * a status indicating whether or not it is OK to use DMA for the
4117 * supplied PACKET command.
4118 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004120 * spin_lock_irqsave(host lock)
Jeff Garzik0cba6322005-05-30 19:49:12 -04004121 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004122 * RETURNS: 0 when ATAPI DMA can be used
4123 * nonzero otherwise
4124 */
4125int ata_check_atapi_dma(struct ata_queued_cmd *qc)
4126{
4127 struct ata_port *ap = qc->ap;
4128 int rc = 0; /* Assume ATAPI DMA is OK by default */
4129
Albert Lee6f23a312007-04-02 11:39:25 +08004130 /* some drives can only do ATAPI DMA on read/write */
4131 if (unlikely(qc->dev->horkage & ATA_HORKAGE_DMA_RW_ONLY)) {
4132 struct scsi_cmnd *cmd = qc->scsicmd;
4133 u8 *scsicmd = cmd->cmnd;
4134
4135 switch (scsicmd[0]) {
4136 case READ_10:
4137 case WRITE_10:
4138 case READ_12:
4139 case WRITE_12:
4140 case READ_6:
4141 case WRITE_6:
4142 /* atapi dma maybe ok */
4143 break;
4144 default:
4145 /* turn off atapi dma */
4146 return 1;
4147 }
4148 }
4149
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150 if (ap->ops->check_atapi_dma)
4151 rc = ap->ops->check_atapi_dma(qc);
4152
4153 return rc;
4154}
4155/**
4156 * ata_qc_prep - Prepare taskfile for submission
4157 * @qc: Metadata associated with taskfile to be prepared
4158 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04004159 * Prepare ATA taskfile for submission.
4160 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004162 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163 */
4164void ata_qc_prep(struct ata_queued_cmd *qc)
4165{
4166 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
4167 return;
4168
4169 ata_fill_sg(qc);
4170}
4171
Brian Kinge46834c2006-03-17 17:04:03 -06004172void ata_noop_qc_prep(struct ata_queued_cmd *qc) { }
4173
Jeff Garzik0cba6322005-05-30 19:49:12 -04004174/**
4175 * ata_sg_init_one - Associate command with memory buffer
4176 * @qc: Command to be associated
4177 * @buf: Memory buffer
4178 * @buflen: Length of memory buffer, in bytes.
4179 *
4180 * Initialize the data-related elements of queued_cmd @qc
4181 * to point to a single memory buffer, @buf of byte length @buflen.
4182 *
4183 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004184 * spin_lock_irqsave(host lock)
Jeff Garzik0cba6322005-05-30 19:49:12 -04004185 */
4186
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
4188{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189 qc->flags |= ATA_QCFLAG_SINGLE;
4190
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004191 qc->__sg = &qc->sgent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192 qc->n_elem = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004193 qc->orig_n_elem = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194 qc->buf_virt = buf;
Brian King233277c2006-06-07 11:25:31 -05004195 qc->nbytes = buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196
Tejun Heo61c05962006-11-14 22:35:43 +09004197 sg_init_one(&qc->sgent, buf, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198}
4199
Jeff Garzik0cba6322005-05-30 19:49:12 -04004200/**
4201 * ata_sg_init - Associate command with scatter-gather table.
4202 * @qc: Command to be associated
4203 * @sg: Scatter-gather table.
4204 * @n_elem: Number of elements in s/g table.
4205 *
4206 * Initialize the data-related elements of queued_cmd @qc
4207 * to point to a scatter-gather table @sg, containing @n_elem
4208 * elements.
4209 *
4210 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004211 * spin_lock_irqsave(host lock)
Jeff Garzik0cba6322005-05-30 19:49:12 -04004212 */
4213
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
4215 unsigned int n_elem)
4216{
4217 qc->flags |= ATA_QCFLAG_SG;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004218 qc->__sg = sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219 qc->n_elem = n_elem;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004220 qc->orig_n_elem = n_elem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221}
4222
4223/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004224 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
4225 * @qc: Command with memory buffer to be mapped.
4226 *
4227 * DMA-map the memory buffer associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228 *
4229 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004230 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231 *
4232 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004233 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234 */
4235
4236static int ata_sg_setup_one(struct ata_queued_cmd *qc)
4237{
4238 struct ata_port *ap = qc->ap;
4239 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004240 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241 dma_addr_t dma_address;
Tejun Heo2e242fa2006-02-20 23:48:38 +09004242 int trim_sg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004244 /* we must lengthen transfers to end on a 32-bit boundary */
4245 qc->pad_len = sg->length & 3;
4246 if (qc->pad_len) {
4247 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
4248 struct scatterlist *psg = &qc->pad_sgent;
4249
Tejun Heoa46314742006-02-11 19:11:13 +09004250 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004251
4252 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
4253
4254 if (qc->tf.flags & ATA_TFLAG_WRITE)
4255 memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
4256 qc->pad_len);
4257
4258 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
4259 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
4260 /* trim sg */
4261 sg->length -= qc->pad_len;
Tejun Heo2e242fa2006-02-20 23:48:38 +09004262 if (sg->length == 0)
4263 trim_sg = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004264
4265 DPRINTK("padding done, sg->length=%u pad_len=%u\n",
4266 sg->length, qc->pad_len);
4267 }
4268
Tejun Heo2e242fa2006-02-20 23:48:38 +09004269 if (trim_sg) {
4270 qc->n_elem--;
Jeff Garzike1410f22005-11-14 14:06:26 -05004271 goto skip_map;
4272 }
4273
Brian King2f1f6102006-03-23 17:30:15 -06004274 dma_address = dma_map_single(ap->dev, qc->buf_virt,
Albert Lee32529e02005-05-26 03:49:42 -04004275 sg->length, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05004276 if (dma_mapping_error(dma_address)) {
4277 /* restore sg */
4278 sg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004279 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05004280 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281
4282 sg_dma_address(sg) = dma_address;
Albert Lee32529e02005-05-26 03:49:42 -04004283 sg_dma_len(sg) = sg->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004284
Tejun Heo2e242fa2006-02-20 23:48:38 +09004285skip_map:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
4287 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
4288
4289 return 0;
4290}
4291
4292/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004293 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
4294 * @qc: Command with scatter-gather table to be mapped.
4295 *
4296 * DMA-map the scatter-gather table associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297 *
4298 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004299 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 *
4301 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004302 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303 *
4304 */
4305
4306static int ata_sg_setup(struct ata_queued_cmd *qc)
4307{
4308 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004309 struct scatterlist *sg = qc->__sg;
4310 struct scatterlist *lsg = &sg[qc->n_elem - 1];
Jeff Garzike1410f22005-11-14 14:06:26 -05004311 int n_elem, pre_n_elem, dir, trim_sg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312
Tejun Heo44877b42007-02-21 01:06:51 +09004313 VPRINTK("ENTER, ata%u\n", ap->print_id);
Tejun Heoa46314742006-02-11 19:11:13 +09004314 WARN_ON(!(qc->flags & ATA_QCFLAG_SG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004315
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004316 /* we must lengthen transfers to end on a 32-bit boundary */
4317 qc->pad_len = lsg->length & 3;
4318 if (qc->pad_len) {
4319 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
4320 struct scatterlist *psg = &qc->pad_sgent;
4321 unsigned int offset;
4322
Tejun Heoa46314742006-02-11 19:11:13 +09004323 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004324
4325 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
4326
4327 /*
4328 * psg->page/offset are used to copy to-be-written
4329 * data in this function or read data in ata_sg_clean.
4330 */
4331 offset = lsg->offset + lsg->length - qc->pad_len;
4332 psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
4333 psg->offset = offset_in_page(offset);
4334
4335 if (qc->tf.flags & ATA_TFLAG_WRITE) {
4336 void *addr = kmap_atomic(psg->page, KM_IRQ0);
4337 memcpy(pad_buf, addr + psg->offset, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05004338 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004339 }
4340
4341 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
4342 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
4343 /* trim last sg */
4344 lsg->length -= qc->pad_len;
Jeff Garzike1410f22005-11-14 14:06:26 -05004345 if (lsg->length == 0)
4346 trim_sg = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004347
4348 DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
4349 qc->n_elem - 1, lsg->length, qc->pad_len);
4350 }
4351
Jeff Garzike1410f22005-11-14 14:06:26 -05004352 pre_n_elem = qc->n_elem;
4353 if (trim_sg && pre_n_elem)
4354 pre_n_elem--;
4355
4356 if (!pre_n_elem) {
4357 n_elem = 0;
4358 goto skip_map;
4359 }
4360
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361 dir = qc->dma_dir;
Brian King2f1f6102006-03-23 17:30:15 -06004362 n_elem = dma_map_sg(ap->dev, sg, pre_n_elem, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05004363 if (n_elem < 1) {
4364 /* restore last sg */
4365 lsg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05004367 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368
4369 DPRINTK("%d sg elements mapped\n", n_elem);
4370
Jeff Garzike1410f22005-11-14 14:06:26 -05004371skip_map:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372 qc->n_elem = n_elem;
4373
4374 return 0;
4375}
4376
4377/**
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004378 * swap_buf_le16 - swap halves of 16-bit words in place
Edward Falk0baab862005-06-02 18:17:13 -04004379 * @buf: Buffer to swap
4380 * @buf_words: Number of 16-bit words in buffer.
4381 *
4382 * Swap halves of 16-bit words if needed to convert from
4383 * little-endian byte order to native cpu byte order, or
4384 * vice-versa.
4385 *
4386 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004387 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04004388 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004389void swap_buf_le16(u16 *buf, unsigned int buf_words)
4390{
4391#ifdef __BIG_ENDIAN
4392 unsigned int i;
4393
4394 for (i = 0; i < buf_words; i++)
4395 buf[i] = le16_to_cpu(buf[i]);
4396#endif /* __BIG_ENDIAN */
4397}
4398
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004399/**
Tejun Heo0d5ff562007-02-01 15:06:36 +09004400 * ata_data_xfer - Transfer data by PIO
Alan Coxa6b2c5d2006-05-22 16:59:59 +01004401 * @adev: device to target
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004402 * @buf: data buffer
4403 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04004404 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004405 *
4406 * Transfer data from/to the device data register by PIO.
4407 *
4408 * LOCKING:
4409 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004410 */
Tejun Heo0d5ff562007-02-01 15:06:36 +09004411void ata_data_xfer(struct ata_device *adev, unsigned char *buf,
4412 unsigned int buflen, int write_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413{
Alan Coxa6b2c5d2006-05-22 16:59:59 +01004414 struct ata_port *ap = adev->ap;
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004415 unsigned int words = buflen >> 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004416
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004417 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418 if (write_data)
Tejun Heo0d5ff562007-02-01 15:06:36 +09004419 iowrite16_rep(ap->ioaddr.data_addr, buf, words);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420 else
Tejun Heo0d5ff562007-02-01 15:06:36 +09004421 ioread16_rep(ap->ioaddr.data_addr, buf, words);
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004422
4423 /* Transfer trailing 1 byte, if any. */
4424 if (unlikely(buflen & 0x01)) {
4425 u16 align_buf[1] = { 0 };
4426 unsigned char *trailing_buf = buf + buflen - 1;
4427
4428 if (write_data) {
4429 memcpy(align_buf, trailing_buf, 1);
Tejun Heo0d5ff562007-02-01 15:06:36 +09004430 iowrite16(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004431 } else {
Tejun Heo0d5ff562007-02-01 15:06:36 +09004432 align_buf[0] = cpu_to_le16(ioread16(ap->ioaddr.data_addr));
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004433 memcpy(trailing_buf, align_buf, 1);
4434 }
4435 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436}
4437
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004438/**
Tejun Heo0d5ff562007-02-01 15:06:36 +09004439 * ata_data_xfer_noirq - Transfer data by PIO
Alan Cox75e99582006-05-24 14:14:41 +01004440 * @adev: device to target
4441 * @buf: data buffer
4442 * @buflen: buffer length
4443 * @write_data: read/write
4444 *
Tejun Heo88574552006-06-25 20:00:35 +09004445 * Transfer data from/to the device data register by PIO. Do the
Alan Cox75e99582006-05-24 14:14:41 +01004446 * transfer with interrupts disabled.
4447 *
4448 * LOCKING:
4449 * Inherited from caller.
4450 */
Tejun Heo0d5ff562007-02-01 15:06:36 +09004451void ata_data_xfer_noirq(struct ata_device *adev, unsigned char *buf,
4452 unsigned int buflen, int write_data)
Alan Cox75e99582006-05-24 14:14:41 +01004453{
4454 unsigned long flags;
4455 local_irq_save(flags);
Tejun Heo0d5ff562007-02-01 15:06:36 +09004456 ata_data_xfer(adev, buf, buflen, write_data);
Alan Cox75e99582006-05-24 14:14:41 +01004457 local_irq_restore(flags);
4458}
4459
4460
4461/**
Mark Lord5a5dbd12007-03-16 10:22:26 -04004462 * ata_pio_sector - Transfer a sector of data.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004463 * @qc: Command on going
4464 *
Mark Lord5a5dbd12007-03-16 10:22:26 -04004465 * Transfer qc->sect_size bytes of data from/to the ATA device.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004466 *
4467 * LOCKING:
4468 * Inherited from caller.
4469 */
4470
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471static void ata_pio_sector(struct ata_queued_cmd *qc)
4472{
4473 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004474 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475 struct ata_port *ap = qc->ap;
4476 struct page *page;
4477 unsigned int offset;
4478 unsigned char *buf;
4479
Mark Lord5a5dbd12007-03-16 10:22:26 -04004480 if (qc->curbytes == qc->nbytes - qc->sect_size)
Albert Lee14be71f2005-09-27 17:36:35 +08004481 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482
4483 page = sg[qc->cursg].page;
Tejun Heo726f0782007-01-03 17:30:39 +09004484 offset = sg[qc->cursg].offset + qc->cursg_ofs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485
4486 /* get the current page and offset */
4487 page = nth_page(page, (offset >> PAGE_SHIFT));
4488 offset %= PAGE_SIZE;
4489
Albert Lee7282aa42005-10-09 09:46:07 -04004490 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
4491
Albert Lee91b8b312005-10-09 09:48:44 -04004492 if (PageHighMem(page)) {
4493 unsigned long flags;
Albert Lee7282aa42005-10-09 09:46:07 -04004494
Alan Coxa6b2c5d2006-05-22 16:59:59 +01004495 /* FIXME: use a bounce buffer */
Albert Lee91b8b312005-10-09 09:48:44 -04004496 local_irq_save(flags);
4497 buf = kmap_atomic(page, KM_IRQ0);
Albert Lee083958d2005-10-09 09:47:31 -04004498
Albert Lee91b8b312005-10-09 09:48:44 -04004499 /* do the actual data transfer */
Mark Lord5a5dbd12007-03-16 10:22:26 -04004500 ap->ops->data_xfer(qc->dev, buf + offset, qc->sect_size, do_write);
Albert Lee91b8b312005-10-09 09:48:44 -04004501
4502 kunmap_atomic(buf, KM_IRQ0);
4503 local_irq_restore(flags);
4504 } else {
4505 buf = page_address(page);
Mark Lord5a5dbd12007-03-16 10:22:26 -04004506 ap->ops->data_xfer(qc->dev, buf + offset, qc->sect_size, do_write);
Albert Lee91b8b312005-10-09 09:48:44 -04004507 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508
Mark Lord5a5dbd12007-03-16 10:22:26 -04004509 qc->curbytes += qc->sect_size;
4510 qc->cursg_ofs += qc->sect_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511
Tejun Heo726f0782007-01-03 17:30:39 +09004512 if (qc->cursg_ofs == (&sg[qc->cursg])->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513 qc->cursg++;
4514 qc->cursg_ofs = 0;
4515 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004518/**
Mark Lord5a5dbd12007-03-16 10:22:26 -04004519 * ata_pio_sectors - Transfer one or many sectors.
Albert Lee07f6f7d2005-11-01 19:33:20 +08004520 * @qc: Command on going
4521 *
Mark Lord5a5dbd12007-03-16 10:22:26 -04004522 * Transfer one or many sectors of data from/to the
Albert Lee07f6f7d2005-11-01 19:33:20 +08004523 * ATA device for the DRQ request.
4524 *
4525 * LOCKING:
4526 * Inherited from caller.
4527 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528
Albert Lee07f6f7d2005-11-01 19:33:20 +08004529static void ata_pio_sectors(struct ata_queued_cmd *qc)
4530{
4531 if (is_multi_taskfile(&qc->tf)) {
4532 /* READ/WRITE MULTIPLE */
4533 unsigned int nsect;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534
Jeff Garzik587005d2006-02-11 18:17:32 -05004535 WARN_ON(qc->dev->multi_count == 0);
Albert Lee07f6f7d2005-11-01 19:33:20 +08004536
Mark Lord5a5dbd12007-03-16 10:22:26 -04004537 nsect = min((qc->nbytes - qc->curbytes) / qc->sect_size,
Tejun Heo726f0782007-01-03 17:30:39 +09004538 qc->dev->multi_count);
Albert Lee07f6f7d2005-11-01 19:33:20 +08004539 while (nsect--)
4540 ata_pio_sector(qc);
4541 } else
4542 ata_pio_sector(qc);
4543}
4544
4545/**
Albert Leec71c1852005-10-04 06:03:45 -04004546 * atapi_send_cdb - Write CDB bytes to hardware
4547 * @ap: Port to which ATAPI device is attached.
4548 * @qc: Taskfile currently active
4549 *
4550 * When device has indicated its readiness to accept
4551 * a CDB, this function is called. Send the CDB.
4552 *
4553 * LOCKING:
4554 * caller.
4555 */
4556
4557static void atapi_send_cdb(struct ata_port *ap, struct ata_queued_cmd *qc)
4558{
4559 /* send SCSI cdb */
4560 DPRINTK("send cdb\n");
Jeff Garzikdb024d52006-02-13 00:23:57 -05004561 WARN_ON(qc->dev->cdb_len < 12);
Albert Leec71c1852005-10-04 06:03:45 -04004562
Alan Coxa6b2c5d2006-05-22 16:59:59 +01004563 ap->ops->data_xfer(qc->dev, qc->cdb, qc->dev->cdb_len, 1);
Albert Leec71c1852005-10-04 06:03:45 -04004564 ata_altstatus(ap); /* flush */
4565
4566 switch (qc->tf.protocol) {
4567 case ATA_PROT_ATAPI:
4568 ap->hsm_task_state = HSM_ST;
4569 break;
4570 case ATA_PROT_ATAPI_NODATA:
4571 ap->hsm_task_state = HSM_ST_LAST;
4572 break;
4573 case ATA_PROT_ATAPI_DMA:
4574 ap->hsm_task_state = HSM_ST_LAST;
4575 /* initiate bmdma */
4576 ap->ops->bmdma_start(qc);
4577 break;
4578 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579}
4580
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004581/**
4582 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
4583 * @qc: Command on going
4584 * @bytes: number of bytes
4585 *
4586 * Transfer Transfer data from/to the ATAPI device.
4587 *
4588 * LOCKING:
4589 * Inherited from caller.
4590 *
4591 */
4592
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
4594{
4595 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004596 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597 struct ata_port *ap = qc->ap;
4598 struct page *page;
4599 unsigned char *buf;
4600 unsigned int offset, count;
4601
Albert Lee563a6e12005-08-12 14:17:50 +08004602 if (qc->curbytes + bytes >= qc->nbytes)
Albert Lee14be71f2005-09-27 17:36:35 +08004603 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604
4605next_sg:
Albert Lee563a6e12005-08-12 14:17:50 +08004606 if (unlikely(qc->cursg >= qc->n_elem)) {
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04004607 /*
Albert Lee563a6e12005-08-12 14:17:50 +08004608 * The end of qc->sg is reached and the device expects
4609 * more data to transfer. In order not to overrun qc->sg
4610 * and fulfill length specified in the byte count register,
4611 * - for read case, discard trailing data from the device
4612 * - for write case, padding zero data to the device
4613 */
4614 u16 pad_buf[1] = { 0 };
4615 unsigned int words = bytes >> 1;
4616 unsigned int i;
4617
4618 if (words) /* warning if bytes > 1 */
Tejun Heof15a1da2006-05-15 20:57:56 +09004619 ata_dev_printk(qc->dev, KERN_WARNING,
4620 "%u bytes trailing data\n", bytes);
Albert Lee563a6e12005-08-12 14:17:50 +08004621
4622 for (i = 0; i < words; i++)
Alan Coxa6b2c5d2006-05-22 16:59:59 +01004623 ap->ops->data_xfer(qc->dev, (unsigned char*)pad_buf, 2, do_write);
Albert Lee563a6e12005-08-12 14:17:50 +08004624
Albert Lee14be71f2005-09-27 17:36:35 +08004625 ap->hsm_task_state = HSM_ST_LAST;
Albert Lee563a6e12005-08-12 14:17:50 +08004626 return;
4627 }
4628
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004629 sg = &qc->__sg[qc->cursg];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631 page = sg->page;
4632 offset = sg->offset + qc->cursg_ofs;
4633
4634 /* get the current page and offset */
4635 page = nth_page(page, (offset >> PAGE_SHIFT));
4636 offset %= PAGE_SIZE;
4637
Albert Lee6952df02005-06-06 15:56:03 +08004638 /* don't overrun current sg */
Albert Lee32529e02005-05-26 03:49:42 -04004639 count = min(sg->length - qc->cursg_ofs, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640
4641 /* don't cross page boundaries */
4642 count = min(count, (unsigned int)PAGE_SIZE - offset);
4643
Albert Lee7282aa42005-10-09 09:46:07 -04004644 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
4645
Albert Lee91b8b312005-10-09 09:48:44 -04004646 if (PageHighMem(page)) {
4647 unsigned long flags;
Albert Lee7282aa42005-10-09 09:46:07 -04004648
Alan Coxa6b2c5d2006-05-22 16:59:59 +01004649 /* FIXME: use bounce buffer */
Albert Lee91b8b312005-10-09 09:48:44 -04004650 local_irq_save(flags);
4651 buf = kmap_atomic(page, KM_IRQ0);
Albert Lee083958d2005-10-09 09:47:31 -04004652
Albert Lee91b8b312005-10-09 09:48:44 -04004653 /* do the actual data transfer */
Alan Coxa6b2c5d2006-05-22 16:59:59 +01004654 ap->ops->data_xfer(qc->dev, buf + offset, count, do_write);
Albert Lee91b8b312005-10-09 09:48:44 -04004655
4656 kunmap_atomic(buf, KM_IRQ0);
4657 local_irq_restore(flags);
4658 } else {
4659 buf = page_address(page);
Alan Coxa6b2c5d2006-05-22 16:59:59 +01004660 ap->ops->data_xfer(qc->dev, buf + offset, count, do_write);
Albert Lee91b8b312005-10-09 09:48:44 -04004661 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662
4663 bytes -= count;
4664 qc->curbytes += count;
4665 qc->cursg_ofs += count;
4666
Albert Lee32529e02005-05-26 03:49:42 -04004667 if (qc->cursg_ofs == sg->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668 qc->cursg++;
4669 qc->cursg_ofs = 0;
4670 }
4671
Albert Lee563a6e12005-08-12 14:17:50 +08004672 if (bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673 goto next_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674}
4675
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004676/**
4677 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
4678 * @qc: Command on going
4679 *
4680 * Transfer Transfer data from/to the ATAPI device.
4681 *
4682 * LOCKING:
4683 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004684 */
4685
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686static void atapi_pio_bytes(struct ata_queued_cmd *qc)
4687{
4688 struct ata_port *ap = qc->ap;
4689 struct ata_device *dev = qc->dev;
4690 unsigned int ireason, bc_lo, bc_hi, bytes;
4691 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
4692
Albert Leeeec4c3f2006-05-18 17:51:10 +08004693 /* Abuse qc->result_tf for temp storage of intermediate TF
4694 * here to save some kernel stack usage.
4695 * For normal completion, qc->result_tf is not relevant. For
4696 * error, qc->result_tf is later overwritten by ata_qc_complete().
4697 * So, the correctness of qc->result_tf is not affected.
4698 */
4699 ap->ops->tf_read(ap, &qc->result_tf);
4700 ireason = qc->result_tf.nsect;
4701 bc_lo = qc->result_tf.lbam;
4702 bc_hi = qc->result_tf.lbah;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703 bytes = (bc_hi << 8) | bc_lo;
4704
4705 /* shall be cleared to zero, indicating xfer of data */
4706 if (ireason & (1 << 0))
4707 goto err_out;
4708
4709 /* make sure transfer direction matches expected */
4710 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
4711 if (do_write != i_write)
4712 goto err_out;
4713
Tejun Heo44877b42007-02-21 01:06:51 +09004714 VPRINTK("ata%u: xfering %d bytes\n", ap->print_id, bytes);
Albert Lee312f7da2005-09-27 17:38:03 +08004715
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716 __atapi_pio_bytes(qc, bytes);
4717
4718 return;
4719
4720err_out:
Tejun Heof15a1da2006-05-15 20:57:56 +09004721 ata_dev_printk(dev, KERN_INFO, "ATAPI check failed\n");
Tejun Heo11a56d22006-01-23 13:09:36 +09004722 qc->err_mask |= AC_ERR_HSM;
Albert Lee14be71f2005-09-27 17:36:35 +08004723 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724}
4725
4726/**
Albert Leec234fb02006-03-25 17:58:38 +08004727 * ata_hsm_ok_in_wq - Check if the qc can be handled in the workqueue.
4728 * @ap: the target ata_port
4729 * @qc: qc on going
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730 *
Albert Leec234fb02006-03-25 17:58:38 +08004731 * RETURNS:
4732 * 1 if ok in workqueue, 0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733 */
Albert Leec234fb02006-03-25 17:58:38 +08004734
4735static inline int ata_hsm_ok_in_wq(struct ata_port *ap, struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736{
Albert Leec234fb02006-03-25 17:58:38 +08004737 if (qc->tf.flags & ATA_TFLAG_POLLING)
4738 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739
Albert Leec234fb02006-03-25 17:58:38 +08004740 if (ap->hsm_task_state == HSM_ST_FIRST) {
4741 if (qc->tf.protocol == ATA_PROT_PIO &&
4742 (qc->tf.flags & ATA_TFLAG_WRITE))
4743 return 1;
4744
4745 if (is_atapi_taskfile(&qc->tf) &&
4746 !(qc->dev->flags & ATA_DFLAG_CDB_INTR))
4747 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748 }
4749
Albert Leec234fb02006-03-25 17:58:38 +08004750 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751}
4752
Albert Leec234fb02006-03-25 17:58:38 +08004753/**
Tejun Heoc17ea202006-05-15 20:59:29 +09004754 * ata_hsm_qc_complete - finish a qc running on standard HSM
4755 * @qc: Command to complete
4756 * @in_wq: 1 if called from workqueue, 0 otherwise
4757 *
4758 * Finish @qc which is running on standard HSM.
4759 *
4760 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004761 * If @in_wq is zero, spin_lock_irqsave(host lock).
Tejun Heoc17ea202006-05-15 20:59:29 +09004762 * Otherwise, none on entry and grabs host lock.
4763 */
4764static void ata_hsm_qc_complete(struct ata_queued_cmd *qc, int in_wq)
4765{
4766 struct ata_port *ap = qc->ap;
4767 unsigned long flags;
4768
4769 if (ap->ops->error_handler) {
4770 if (in_wq) {
Jeff Garzikba6a1302006-06-22 23:46:10 -04004771 spin_lock_irqsave(ap->lock, flags);
Tejun Heoc17ea202006-05-15 20:59:29 +09004772
Jeff Garzikcca39742006-08-24 03:19:22 -04004773 /* EH might have kicked in while host lock is
4774 * released.
Tejun Heoc17ea202006-05-15 20:59:29 +09004775 */
4776 qc = ata_qc_from_tag(ap, qc->tag);
4777 if (qc) {
4778 if (likely(!(qc->err_mask & AC_ERR_HSM))) {
Akira Iguchi83625002007-01-26 16:27:32 +09004779 ap->ops->irq_on(ap);
Tejun Heoc17ea202006-05-15 20:59:29 +09004780 ata_qc_complete(qc);
4781 } else
4782 ata_port_freeze(ap);
4783 }
4784
Jeff Garzikba6a1302006-06-22 23:46:10 -04004785 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoc17ea202006-05-15 20:59:29 +09004786 } else {
4787 if (likely(!(qc->err_mask & AC_ERR_HSM)))
4788 ata_qc_complete(qc);
4789 else
4790 ata_port_freeze(ap);
4791 }
4792 } else {
4793 if (in_wq) {
Jeff Garzikba6a1302006-06-22 23:46:10 -04004794 spin_lock_irqsave(ap->lock, flags);
Akira Iguchi83625002007-01-26 16:27:32 +09004795 ap->ops->irq_on(ap);
Tejun Heoc17ea202006-05-15 20:59:29 +09004796 ata_qc_complete(qc);
Jeff Garzikba6a1302006-06-22 23:46:10 -04004797 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoc17ea202006-05-15 20:59:29 +09004798 } else
4799 ata_qc_complete(qc);
4800 }
Jeff Garzikc81e29b2006-05-24 01:49:12 -04004801
4802 ata_altstatus(ap); /* flush */
Tejun Heoc17ea202006-05-15 20:59:29 +09004803}
4804
4805/**
Albert Leebb5cb292006-03-25 17:48:02 +08004806 * ata_hsm_move - move the HSM to the next state.
4807 * @ap: the target ata_port
4808 * @qc: qc on going
4809 * @status: current device status
4810 * @in_wq: 1 if called from workqueue, 0 otherwise
4811 *
4812 * RETURNS:
4813 * 1 when poll next status needed, 0 otherwise.
4814 */
Tejun Heo9a1004d2006-05-31 18:27:52 +09004815int ata_hsm_move(struct ata_port *ap, struct ata_queued_cmd *qc,
4816 u8 status, int in_wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817{
Albert Leebb5cb292006-03-25 17:48:02 +08004818 unsigned long flags = 0;
4819 int poll_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004820
Albert Lee6912ccd2006-03-25 17:45:49 +08004821 WARN_ON((qc->flags & ATA_QCFLAG_ACTIVE) == 0);
Albert Lee0565c262006-02-13 18:55:25 +08004822
Albert Leebb5cb292006-03-25 17:48:02 +08004823 /* Make sure ata_qc_issue_prot() does not throw things
4824 * like DMA polling into the workqueue. Notice that
4825 * in_wq is not equivalent to (qc->tf.flags & ATA_TFLAG_POLLING).
Albert Lee1c848982005-12-05 15:40:15 +08004826 */
Albert Leec234fb02006-03-25 17:58:38 +08004827 WARN_ON(in_wq != ata_hsm_ok_in_wq(ap, qc));
Albert Lee1c848982005-12-05 15:40:15 +08004828
Albert Leee2cec772006-03-25 17:43:49 +08004829fsm_start:
Albert Lee999bb6f2006-03-25 18:07:48 +08004830 DPRINTK("ata%u: protocol %d task_state %d (dev_stat 0x%X)\n",
Tejun Heo44877b42007-02-21 01:06:51 +09004831 ap->print_id, qc->tf.protocol, ap->hsm_task_state, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832
Albert Leee2cec772006-03-25 17:43:49 +08004833 switch (ap->hsm_task_state) {
4834 case HSM_ST_FIRST:
Albert Leebb5cb292006-03-25 17:48:02 +08004835 /* Send first data block or PACKET CDB */
4836
4837 /* If polling, we will stay in the work queue after
4838 * sending the data. Otherwise, interrupt handler
4839 * takes over after sending the data.
4840 */
4841 poll_next = (qc->tf.flags & ATA_TFLAG_POLLING);
4842
Albert Leee2cec772006-03-25 17:43:49 +08004843 /* check device status */
Albert Lee3655d1d2006-05-19 11:43:04 +08004844 if (unlikely((status & ATA_DRQ) == 0)) {
4845 /* handle BSY=0, DRQ=0 as error */
4846 if (likely(status & (ATA_ERR | ATA_DF)))
4847 /* device stops HSM for abort/error */
4848 qc->err_mask |= AC_ERR_DEV;
4849 else
4850 /* HSM violation. Let EH handle this */
4851 qc->err_mask |= AC_ERR_HSM;
4852
Albert Leee2cec772006-03-25 17:43:49 +08004853 ap->hsm_task_state = HSM_ST_ERR;
4854 goto fsm_start;
4855 }
4856
Albert Lee71601952006-03-25 18:11:12 +08004857 /* Device should not ask for data transfer (DRQ=1)
4858 * when it finds something wrong.
Albert Leeeee6c322006-04-01 17:38:43 +08004859 * We ignore DRQ here and stop the HSM by
4860 * changing hsm_task_state to HSM_ST_ERR and
4861 * let the EH abort the command or reset the device.
Albert Lee71601952006-03-25 18:11:12 +08004862 */
4863 if (unlikely(status & (ATA_ERR | ATA_DF))) {
Tejun Heo44877b42007-02-21 01:06:51 +09004864 ata_port_printk(ap, KERN_WARNING, "DRQ=1 with device "
4865 "error, dev_stat 0x%X\n", status);
Albert Lee3655d1d2006-05-19 11:43:04 +08004866 qc->err_mask |= AC_ERR_HSM;
Albert Leeeee6c322006-04-01 17:38:43 +08004867 ap->hsm_task_state = HSM_ST_ERR;
4868 goto fsm_start;
Albert Lee71601952006-03-25 18:11:12 +08004869 }
4870
Albert Leebb5cb292006-03-25 17:48:02 +08004871 /* Send the CDB (atapi) or the first data block (ata pio out).
4872 * During the state transition, interrupt handler shouldn't
4873 * be invoked before the data transfer is complete and
4874 * hsm_task_state is changed. Hence, the following locking.
4875 */
4876 if (in_wq)
Jeff Garzikba6a1302006-06-22 23:46:10 -04004877 spin_lock_irqsave(ap->lock, flags);
Albert Leee2cec772006-03-25 17:43:49 +08004878
Albert Leebb5cb292006-03-25 17:48:02 +08004879 if (qc->tf.protocol == ATA_PROT_PIO) {
4880 /* PIO data out protocol.
4881 * send first data block.
4882 */
4883
4884 /* ata_pio_sectors() might change the state
4885 * to HSM_ST_LAST. so, the state is changed here
4886 * before ata_pio_sectors().
4887 */
4888 ap->hsm_task_state = HSM_ST;
4889 ata_pio_sectors(qc);
4890 ata_altstatus(ap); /* flush */
4891 } else
4892 /* send CDB */
4893 atapi_send_cdb(ap, qc);
4894
4895 if (in_wq)
Jeff Garzikba6a1302006-06-22 23:46:10 -04004896 spin_unlock_irqrestore(ap->lock, flags);
Albert Leebb5cb292006-03-25 17:48:02 +08004897
4898 /* if polling, ata_pio_task() handles the rest.
4899 * otherwise, interrupt handler takes over from here.
4900 */
Albert Leee2cec772006-03-25 17:43:49 +08004901 break;
4902
4903 case HSM_ST:
4904 /* complete command or read/write the data register */
4905 if (qc->tf.protocol == ATA_PROT_ATAPI) {
4906 /* ATAPI PIO protocol */
4907 if ((status & ATA_DRQ) == 0) {
Albert Lee3655d1d2006-05-19 11:43:04 +08004908 /* No more data to transfer or device error.
4909 * Device error will be tagged in HSM_ST_LAST.
4910 */
Albert Leee2cec772006-03-25 17:43:49 +08004911 ap->hsm_task_state = HSM_ST_LAST;
4912 goto fsm_start;
4913 }
4914
Albert Lee71601952006-03-25 18:11:12 +08004915 /* Device should not ask for data transfer (DRQ=1)
4916 * when it finds something wrong.
Albert Leeeee6c322006-04-01 17:38:43 +08004917 * We ignore DRQ here and stop the HSM by
4918 * changing hsm_task_state to HSM_ST_ERR and
4919 * let the EH abort the command or reset the device.
Albert Lee71601952006-03-25 18:11:12 +08004920 */
4921 if (unlikely(status & (ATA_ERR | ATA_DF))) {
Tejun Heo44877b42007-02-21 01:06:51 +09004922 ata_port_printk(ap, KERN_WARNING, "DRQ=1 with "
4923 "device error, dev_stat 0x%X\n",
4924 status);
Albert Lee3655d1d2006-05-19 11:43:04 +08004925 qc->err_mask |= AC_ERR_HSM;
Albert Leeeee6c322006-04-01 17:38:43 +08004926 ap->hsm_task_state = HSM_ST_ERR;
4927 goto fsm_start;
Albert Lee71601952006-03-25 18:11:12 +08004928 }
4929
Albert Leee2cec772006-03-25 17:43:49 +08004930 atapi_pio_bytes(qc);
4931
4932 if (unlikely(ap->hsm_task_state == HSM_ST_ERR))
4933 /* bad ireason reported by device */
4934 goto fsm_start;
4935
4936 } else {
4937 /* ATA PIO protocol */
4938 if (unlikely((status & ATA_DRQ) == 0)) {
4939 /* handle BSY=0, DRQ=0 as error */
Albert Lee3655d1d2006-05-19 11:43:04 +08004940 if (likely(status & (ATA_ERR | ATA_DF)))
4941 /* device stops HSM for abort/error */
4942 qc->err_mask |= AC_ERR_DEV;
4943 else
Tejun Heo55a8e2c2006-11-10 18:08:10 +09004944 /* HSM violation. Let EH handle this.
4945 * Phantom devices also trigger this
4946 * condition. Mark hint.
4947 */
4948 qc->err_mask |= AC_ERR_HSM |
4949 AC_ERR_NODEV_HINT;
Albert Lee3655d1d2006-05-19 11:43:04 +08004950
Albert Leee2cec772006-03-25 17:43:49 +08004951 ap->hsm_task_state = HSM_ST_ERR;
4952 goto fsm_start;
4953 }
4954
Albert Leeeee6c322006-04-01 17:38:43 +08004955 /* For PIO reads, some devices may ask for
4956 * data transfer (DRQ=1) alone with ERR=1.
4957 * We respect DRQ here and transfer one
4958 * block of junk data before changing the
4959 * hsm_task_state to HSM_ST_ERR.
4960 *
4961 * For PIO writes, ERR=1 DRQ=1 doesn't make
4962 * sense since the data block has been
4963 * transferred to the device.
Albert Lee71601952006-03-25 18:11:12 +08004964 */
4965 if (unlikely(status & (ATA_ERR | ATA_DF))) {
Albert Lee71601952006-03-25 18:11:12 +08004966 /* data might be corrputed */
4967 qc->err_mask |= AC_ERR_DEV;
Albert Leeeee6c322006-04-01 17:38:43 +08004968
4969 if (!(qc->tf.flags & ATA_TFLAG_WRITE)) {
4970 ata_pio_sectors(qc);
4971 ata_altstatus(ap);
4972 status = ata_wait_idle(ap);
4973 }
4974
Albert Lee3655d1d2006-05-19 11:43:04 +08004975 if (status & (ATA_BUSY | ATA_DRQ))
4976 qc->err_mask |= AC_ERR_HSM;
4977
Albert Leeeee6c322006-04-01 17:38:43 +08004978 /* ata_pio_sectors() might change the
4979 * state to HSM_ST_LAST. so, the state
4980 * is changed after ata_pio_sectors().
4981 */
4982 ap->hsm_task_state = HSM_ST_ERR;
4983 goto fsm_start;
Albert Lee71601952006-03-25 18:11:12 +08004984 }
4985
Albert Leee2cec772006-03-25 17:43:49 +08004986 ata_pio_sectors(qc);
4987
4988 if (ap->hsm_task_state == HSM_ST_LAST &&
4989 (!(qc->tf.flags & ATA_TFLAG_WRITE))) {
4990 /* all data read */
4991 ata_altstatus(ap);
Albert Lee52a32202006-03-25 18:18:15 +08004992 status = ata_wait_idle(ap);
Albert Leee2cec772006-03-25 17:43:49 +08004993 goto fsm_start;
4994 }
4995 }
4996
4997 ata_altstatus(ap); /* flush */
Albert Leebb5cb292006-03-25 17:48:02 +08004998 poll_next = 1;
Albert Leee2cec772006-03-25 17:43:49 +08004999 break;
5000
5001 case HSM_ST_LAST:
Albert Lee6912ccd2006-03-25 17:45:49 +08005002 if (unlikely(!ata_ok(status))) {
5003 qc->err_mask |= __ac_err_mask(status);
Albert Leee2cec772006-03-25 17:43:49 +08005004 ap->hsm_task_state = HSM_ST_ERR;
5005 goto fsm_start;
5006 }
5007
5008 /* no more data to transfer */
Albert Lee4332a772006-04-03 17:43:24 +08005009 DPRINTK("ata%u: dev %u command complete, drv_stat 0x%x\n",
Tejun Heo44877b42007-02-21 01:06:51 +09005010 ap->print_id, qc->dev->devno, status);
Albert Leee2cec772006-03-25 17:43:49 +08005011
Albert Lee6912ccd2006-03-25 17:45:49 +08005012 WARN_ON(qc->err_mask);
5013
Albert Leee2cec772006-03-25 17:43:49 +08005014 ap->hsm_task_state = HSM_ST_IDLE;
5015
5016 /* complete taskfile transaction */
Tejun Heoc17ea202006-05-15 20:59:29 +09005017 ata_hsm_qc_complete(qc, in_wq);
Albert Leebb5cb292006-03-25 17:48:02 +08005018
5019 poll_next = 0;
Albert Leee2cec772006-03-25 17:43:49 +08005020 break;
5021
5022 case HSM_ST_ERR:
Albert Leee2cec772006-03-25 17:43:49 +08005023 /* make sure qc->err_mask is available to
5024 * know what's wrong and recover
5025 */
5026 WARN_ON(qc->err_mask == 0);
5027
5028 ap->hsm_task_state = HSM_ST_IDLE;
Albert Leebb5cb292006-03-25 17:48:02 +08005029
Albert Lee999bb6f2006-03-25 18:07:48 +08005030 /* complete taskfile transaction */
Tejun Heoc17ea202006-05-15 20:59:29 +09005031 ata_hsm_qc_complete(qc, in_wq);
Albert Leebb5cb292006-03-25 17:48:02 +08005032
5033 poll_next = 0;
Albert Leee2cec772006-03-25 17:43:49 +08005034 break;
5035 default:
Albert Leebb5cb292006-03-25 17:48:02 +08005036 poll_next = 0;
Albert Lee6912ccd2006-03-25 17:45:49 +08005037 BUG();
Albert Leee2cec772006-03-25 17:43:49 +08005038 }
5039
Albert Leebb5cb292006-03-25 17:48:02 +08005040 return poll_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041}
5042
David Howells65f27f32006-11-22 14:55:48 +00005043static void ata_pio_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044{
David Howells65f27f32006-11-22 14:55:48 +00005045 struct ata_port *ap =
5046 container_of(work, struct ata_port, port_task.work);
5047 struct ata_queued_cmd *qc = ap->port_task_data;
Albert Leea1af3732006-03-25 17:50:15 +08005048 u8 status;
5049 int poll_next;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04005050
5051fsm_start:
Albert Leea1af3732006-03-25 17:50:15 +08005052 WARN_ON(ap->hsm_task_state == HSM_ST_IDLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005053
Albert Leea1af3732006-03-25 17:50:15 +08005054 /*
5055 * This is purely heuristic. This is a fast path.
5056 * Sometimes when we enter, BSY will be cleared in
5057 * a chk-status or two. If not, the drive is probably seeking
5058 * or something. Snooze for a couple msecs, then
5059 * chk-status again. If still busy, queue delayed work.
5060 */
5061 status = ata_busy_wait(ap, ATA_BUSY, 5);
5062 if (status & ATA_BUSY) {
5063 msleep(2);
5064 status = ata_busy_wait(ap, ATA_BUSY, 10);
5065 if (status & ATA_BUSY) {
Albert Lee31ce6da2006-04-03 18:31:44 +08005066 ata_port_queue_task(ap, ata_pio_task, qc, ATA_SHORT_PAUSE);
Albert Leea1af3732006-03-25 17:50:15 +08005067 return;
5068 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069 }
5070
Albert Leea1af3732006-03-25 17:50:15 +08005071 /* move the HSM */
5072 poll_next = ata_hsm_move(ap, qc, status, 1);
5073
5074 /* another command or interrupt handler
5075 * may be running at this point.
5076 */
5077 if (poll_next)
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04005078 goto fsm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005079}
5080
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082 * ata_qc_new - Request an available ATA command, for queueing
5083 * @ap: Port associated with device @dev
5084 * @dev: Device from whom we request an available command structure
5085 *
5086 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04005087 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088 */
5089
5090static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
5091{
5092 struct ata_queued_cmd *qc = NULL;
5093 unsigned int i;
5094
Tejun Heoe3180492006-05-15 20:58:09 +09005095 /* no command while frozen */
Tejun Heob51e9e52006-06-29 01:29:30 +09005096 if (unlikely(ap->pflags & ATA_PFLAG_FROZEN))
Tejun Heoe3180492006-05-15 20:58:09 +09005097 return NULL;
5098
Tejun Heo2ab7db12006-05-15 20:58:02 +09005099 /* the last tag is reserved for internal command. */
5100 for (i = 0; i < ATA_MAX_QUEUE - 1; i++)
Tejun Heo6cec4a32006-05-15 21:03:41 +09005101 if (!test_and_set_bit(i, &ap->qc_allocated)) {
Tejun Heof69499f2006-05-15 20:58:03 +09005102 qc = __ata_qc_from_tag(ap, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103 break;
5104 }
5105
5106 if (qc)
5107 qc->tag = i;
5108
5109 return qc;
5110}
5111
5112/**
5113 * ata_qc_new_init - Request an available ATA command, and initialize it
Linus Torvalds1da177e2005-04-16 15:20:36 -07005114 * @dev: Device from whom we request an available command structure
5115 *
5116 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04005117 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118 */
5119
Tejun Heo3373efd2006-05-15 20:57:53 +09005120struct ata_queued_cmd *ata_qc_new_init(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005121{
Tejun Heo3373efd2006-05-15 20:57:53 +09005122 struct ata_port *ap = dev->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123 struct ata_queued_cmd *qc;
5124
5125 qc = ata_qc_new(ap);
5126 if (qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127 qc->scsicmd = NULL;
5128 qc->ap = ap;
5129 qc->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005130
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05005131 ata_qc_reinit(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005132 }
5133
5134 return qc;
5135}
5136
Linus Torvalds1da177e2005-04-16 15:20:36 -07005137/**
5138 * ata_qc_free - free unused ata_queued_cmd
5139 * @qc: Command to complete
5140 *
5141 * Designed to free unused ata_queued_cmd object
5142 * in case something prevents using it.
5143 *
5144 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04005145 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005146 */
5147void ata_qc_free(struct ata_queued_cmd *qc)
5148{
Tejun Heo4ba946e2006-01-23 13:09:36 +09005149 struct ata_port *ap = qc->ap;
5150 unsigned int tag;
5151
Tejun Heoa46314742006-02-11 19:11:13 +09005152 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153
Tejun Heo4ba946e2006-01-23 13:09:36 +09005154 qc->flags = 0;
5155 tag = qc->tag;
5156 if (likely(ata_tag_valid(tag))) {
Tejun Heo4ba946e2006-01-23 13:09:36 +09005157 qc->tag = ATA_TAG_POISON;
Tejun Heo6cec4a32006-05-15 21:03:41 +09005158 clear_bit(tag, &ap->qc_allocated);
Tejun Heo4ba946e2006-01-23 13:09:36 +09005159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005160}
5161
Tejun Heo76014422006-02-11 15:13:49 +09005162void __ata_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163{
Tejun Heodedaf2b2006-05-15 21:03:43 +09005164 struct ata_port *ap = qc->ap;
5165
Tejun Heoa46314742006-02-11 19:11:13 +09005166 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
5167 WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005168
5169 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
5170 ata_sg_clean(qc);
5171
Tejun Heo7401abf2006-05-15 20:57:32 +09005172 /* command should be marked inactive atomically with qc completion */
Tejun Heodedaf2b2006-05-15 21:03:43 +09005173 if (qc->tf.protocol == ATA_PROT_NCQ)
5174 ap->sactive &= ~(1 << qc->tag);
5175 else
5176 ap->active_tag = ATA_TAG_POISON;
Tejun Heo7401abf2006-05-15 20:57:32 +09005177
Albert Lee3f3791d2005-08-16 14:25:38 +08005178 /* atapi: mark qc as inactive to prevent the interrupt handler
5179 * from completing the command twice later, before the error handler
5180 * is called. (when rc != 0 and atapi request sense is needed)
5181 */
5182 qc->flags &= ~ATA_QCFLAG_ACTIVE;
Tejun Heodedaf2b2006-05-15 21:03:43 +09005183 ap->qc_active &= ~(1 << qc->tag);
Albert Lee3f3791d2005-08-16 14:25:38 +08005184
Linus Torvalds1da177e2005-04-16 15:20:36 -07005185 /* call completion callback */
Tejun Heo77853bf2006-01-23 13:09:36 +09005186 qc->complete_fn(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005187}
5188
Tejun Heo39599a52006-11-14 22:37:35 +09005189static void fill_result_tf(struct ata_queued_cmd *qc)
5190{
5191 struct ata_port *ap = qc->ap;
5192
Tejun Heo39599a52006-11-14 22:37:35 +09005193 qc->result_tf.flags = qc->tf.flags;
Mark Lord4742d542007-04-02 16:20:35 -04005194 ap->ops->tf_read(ap, &qc->result_tf);
Tejun Heo39599a52006-11-14 22:37:35 +09005195}
5196
Tejun Heof686bcb2006-05-15 20:58:05 +09005197/**
5198 * ata_qc_complete - Complete an active ATA command
5199 * @qc: Command to complete
5200 * @err_mask: ATA Status register contents
5201 *
5202 * Indicate to the mid and upper layers that an ATA
5203 * command has completed, with either an ok or not-ok status.
5204 *
5205 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04005206 * spin_lock_irqsave(host lock)
Tejun Heof686bcb2006-05-15 20:58:05 +09005207 */
5208void ata_qc_complete(struct ata_queued_cmd *qc)
5209{
5210 struct ata_port *ap = qc->ap;
5211
5212 /* XXX: New EH and old EH use different mechanisms to
5213 * synchronize EH with regular execution path.
5214 *
5215 * In new EH, a failed qc is marked with ATA_QCFLAG_FAILED.
5216 * Normal execution path is responsible for not accessing a
5217 * failed qc. libata core enforces the rule by returning NULL
5218 * from ata_qc_from_tag() for failed qcs.
5219 *
5220 * Old EH depends on ata_qc_complete() nullifying completion
5221 * requests if ATA_QCFLAG_EH_SCHEDULED is set. Old EH does
5222 * not synchronize with interrupt handler. Only PIO task is
5223 * taken care of.
5224 */
5225 if (ap->ops->error_handler) {
Tejun Heob51e9e52006-06-29 01:29:30 +09005226 WARN_ON(ap->pflags & ATA_PFLAG_FROZEN);
Tejun Heof686bcb2006-05-15 20:58:05 +09005227
5228 if (unlikely(qc->err_mask))
5229 qc->flags |= ATA_QCFLAG_FAILED;
5230
5231 if (unlikely(qc->flags & ATA_QCFLAG_FAILED)) {
5232 if (!ata_tag_internal(qc->tag)) {
5233 /* always fill result TF for failed qc */
Tejun Heo39599a52006-11-14 22:37:35 +09005234 fill_result_tf(qc);
Tejun Heof686bcb2006-05-15 20:58:05 +09005235 ata_qc_schedule_eh(qc);
5236 return;
5237 }
5238 }
5239
5240 /* read result TF if requested */
5241 if (qc->flags & ATA_QCFLAG_RESULT_TF)
Tejun Heo39599a52006-11-14 22:37:35 +09005242 fill_result_tf(qc);
Tejun Heof686bcb2006-05-15 20:58:05 +09005243
5244 __ata_qc_complete(qc);
5245 } else {
5246 if (qc->flags & ATA_QCFLAG_EH_SCHEDULED)
5247 return;
5248
5249 /* read result TF if failed or requested */
5250 if (qc->err_mask || qc->flags & ATA_QCFLAG_RESULT_TF)
Tejun Heo39599a52006-11-14 22:37:35 +09005251 fill_result_tf(qc);
Tejun Heof686bcb2006-05-15 20:58:05 +09005252
5253 __ata_qc_complete(qc);
5254 }
5255}
5256
Tejun Heodedaf2b2006-05-15 21:03:43 +09005257/**
5258 * ata_qc_complete_multiple - Complete multiple qcs successfully
5259 * @ap: port in question
5260 * @qc_active: new qc_active mask
5261 * @finish_qc: LLDD callback invoked before completing a qc
5262 *
5263 * Complete in-flight commands. This functions is meant to be
5264 * called from low-level driver's interrupt routine to complete
5265 * requests normally. ap->qc_active and @qc_active is compared
5266 * and commands are completed accordingly.
5267 *
5268 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04005269 * spin_lock_irqsave(host lock)
Tejun Heodedaf2b2006-05-15 21:03:43 +09005270 *
5271 * RETURNS:
5272 * Number of completed commands on success, -errno otherwise.
5273 */
5274int ata_qc_complete_multiple(struct ata_port *ap, u32 qc_active,
5275 void (*finish_qc)(struct ata_queued_cmd *))
5276{
5277 int nr_done = 0;
5278 u32 done_mask;
5279 int i;
5280
5281 done_mask = ap->qc_active ^ qc_active;
5282
5283 if (unlikely(done_mask & qc_active)) {
5284 ata_port_printk(ap, KERN_ERR, "illegal qc_active transition "
5285 "(%08x->%08x)\n", ap->qc_active, qc_active);
5286 return -EINVAL;
5287 }
5288
5289 for (i = 0; i < ATA_MAX_QUEUE; i++) {
5290 struct ata_queued_cmd *qc;
5291
5292 if (!(done_mask & (1 << i)))
5293 continue;
5294
5295 if ((qc = ata_qc_from_tag(ap, i))) {
5296 if (finish_qc)
5297 finish_qc(qc);
5298 ata_qc_complete(qc);
5299 nr_done++;
5300 }
5301 }
5302
5303 return nr_done;
5304}
5305
Linus Torvalds1da177e2005-04-16 15:20:36 -07005306static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
5307{
5308 struct ata_port *ap = qc->ap;
5309
5310 switch (qc->tf.protocol) {
Tejun Heo3dc1d882006-05-15 21:03:45 +09005311 case ATA_PROT_NCQ:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005312 case ATA_PROT_DMA:
5313 case ATA_PROT_ATAPI_DMA:
5314 return 1;
5315
5316 case ATA_PROT_ATAPI:
5317 case ATA_PROT_PIO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005318 if (ap->flags & ATA_FLAG_PIO_DMA)
5319 return 1;
5320
5321 /* fall through */
5322
5323 default:
5324 return 0;
5325 }
5326
5327 /* never reached */
5328}
5329
5330/**
5331 * ata_qc_issue - issue taskfile to device
5332 * @qc: command to issue to device
5333 *
5334 * Prepare an ATA command to submission to device.
5335 * This includes mapping the data into a DMA-able
5336 * area, filling in the S/G table, and finally
5337 * writing the taskfile to hardware, starting the command.
5338 *
5339 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04005340 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341 */
Tejun Heo8e0e6942006-03-31 20:41:11 +09005342void ata_qc_issue(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005343{
5344 struct ata_port *ap = qc->ap;
5345
Tejun Heodedaf2b2006-05-15 21:03:43 +09005346 /* Make sure only one non-NCQ command is outstanding. The
5347 * check is skipped for old EH because it reuses active qc to
5348 * request ATAPI sense.
5349 */
5350 WARN_ON(ap->ops->error_handler && ata_tag_valid(ap->active_tag));
5351
5352 if (qc->tf.protocol == ATA_PROT_NCQ) {
5353 WARN_ON(ap->sactive & (1 << qc->tag));
5354 ap->sactive |= 1 << qc->tag;
5355 } else {
5356 WARN_ON(ap->sactive);
5357 ap->active_tag = qc->tag;
5358 }
5359
Tejun Heoe4a70e72006-03-31 20:36:47 +09005360 qc->flags |= ATA_QCFLAG_ACTIVE;
Tejun Heodedaf2b2006-05-15 21:03:43 +09005361 ap->qc_active |= 1 << qc->tag;
Tejun Heoe4a70e72006-03-31 20:36:47 +09005362
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363 if (ata_should_dma_map(qc)) {
5364 if (qc->flags & ATA_QCFLAG_SG) {
5365 if (ata_sg_setup(qc))
Tejun Heo8e436af2006-01-23 13:09:36 +09005366 goto sg_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005367 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
5368 if (ata_sg_setup_one(qc))
Tejun Heo8e436af2006-01-23 13:09:36 +09005369 goto sg_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370 }
5371 } else {
5372 qc->flags &= ~ATA_QCFLAG_DMAMAP;
5373 }
5374
5375 ap->ops->qc_prep(qc);
5376
Tejun Heo8e0e6942006-03-31 20:41:11 +09005377 qc->err_mask |= ap->ops->qc_issue(qc);
5378 if (unlikely(qc->err_mask))
5379 goto err;
5380 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381
Tejun Heo8e436af2006-01-23 13:09:36 +09005382sg_err:
5383 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Tejun Heo8e0e6942006-03-31 20:41:11 +09005384 qc->err_mask |= AC_ERR_SYSTEM;
5385err:
5386 ata_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005387}
5388
5389/**
5390 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
5391 * @qc: command to issue to device
5392 *
5393 * Using various libata functions and hooks, this function
5394 * starts an ATA command. ATA commands are grouped into
5395 * classes called "protocols", and issuing each type of protocol
5396 * is slightly different.
5397 *
Edward Falk0baab862005-06-02 18:17:13 -04005398 * May be used as the qc_issue() entry in ata_port_operations.
5399 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04005401 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005402 *
5403 * RETURNS:
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09005404 * Zero on success, AC_ERR_* mask on failure
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405 */
5406
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09005407unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408{
5409 struct ata_port *ap = qc->ap;
5410
Albert Leee50362e2005-09-27 17:39:50 +08005411 /* Use polling pio if the LLD doesn't handle
5412 * interrupt driven pio and atapi CDB interrupt.
5413 */
5414 if (ap->flags & ATA_FLAG_PIO_POLLING) {
5415 switch (qc->tf.protocol) {
5416 case ATA_PROT_PIO:
Albert Leee3472cb2006-12-07 11:37:58 +08005417 case ATA_PROT_NODATA:
Albert Leee50362e2005-09-27 17:39:50 +08005418 case ATA_PROT_ATAPI:
5419 case ATA_PROT_ATAPI_NODATA:
5420 qc->tf.flags |= ATA_TFLAG_POLLING;
5421 break;
5422 case ATA_PROT_ATAPI_DMA:
5423 if (qc->dev->flags & ATA_DFLAG_CDB_INTR)
Albert Lee3a778272006-06-22 13:00:25 +08005424 /* see ata_dma_blacklisted() */
Albert Leee50362e2005-09-27 17:39:50 +08005425 BUG();
5426 break;
5427 default:
5428 break;
5429 }
5430 }
5431
Albert Lee312f7da2005-09-27 17:38:03 +08005432 /* select the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005433 ata_dev_select(ap, qc->dev->devno, 1, 0);
5434
Albert Lee312f7da2005-09-27 17:38:03 +08005435 /* start the command */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005436 switch (qc->tf.protocol) {
5437 case ATA_PROT_NODATA:
Albert Lee312f7da2005-09-27 17:38:03 +08005438 if (qc->tf.flags & ATA_TFLAG_POLLING)
5439 ata_qc_set_polling(qc);
5440
Jeff Garzike5338252005-10-30 21:37:17 -05005441 ata_tf_to_host(ap, &qc->tf);
Albert Lee312f7da2005-09-27 17:38:03 +08005442 ap->hsm_task_state = HSM_ST_LAST;
5443
5444 if (qc->tf.flags & ATA_TFLAG_POLLING)
Albert Lee31ce6da2006-04-03 18:31:44 +08005445 ata_port_queue_task(ap, ata_pio_task, qc, 0);
Albert Lee312f7da2005-09-27 17:38:03 +08005446
Linus Torvalds1da177e2005-04-16 15:20:36 -07005447 break;
5448
5449 case ATA_PROT_DMA:
Jeff Garzik587005d2006-02-11 18:17:32 -05005450 WARN_ON(qc->tf.flags & ATA_TFLAG_POLLING);
Albert Lee312f7da2005-09-27 17:38:03 +08005451
Linus Torvalds1da177e2005-04-16 15:20:36 -07005452 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
5453 ap->ops->bmdma_setup(qc); /* set up bmdma */
5454 ap->ops->bmdma_start(qc); /* initiate bmdma */
Albert Lee312f7da2005-09-27 17:38:03 +08005455 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005456 break;
5457
Albert Lee312f7da2005-09-27 17:38:03 +08005458 case ATA_PROT_PIO:
5459 if (qc->tf.flags & ATA_TFLAG_POLLING)
5460 ata_qc_set_polling(qc);
5461
Jeff Garzike5338252005-10-30 21:37:17 -05005462 ata_tf_to_host(ap, &qc->tf);
Albert Lee312f7da2005-09-27 17:38:03 +08005463
Albert Lee54f00382005-09-30 19:14:19 +08005464 if (qc->tf.flags & ATA_TFLAG_WRITE) {
5465 /* PIO data out protocol */
5466 ap->hsm_task_state = HSM_ST_FIRST;
Albert Lee31ce6da2006-04-03 18:31:44 +08005467 ata_port_queue_task(ap, ata_pio_task, qc, 0);
Albert Lee54f00382005-09-30 19:14:19 +08005468
5469 /* always send first data block using
Albert Leee27486d2005-11-01 19:24:49 +08005470 * the ata_pio_task() codepath.
Albert Lee54f00382005-09-30 19:14:19 +08005471 */
Albert Lee312f7da2005-09-27 17:38:03 +08005472 } else {
Albert Lee54f00382005-09-30 19:14:19 +08005473 /* PIO data in protocol */
5474 ap->hsm_task_state = HSM_ST;
Albert Lee312f7da2005-09-27 17:38:03 +08005475
Albert Lee54f00382005-09-30 19:14:19 +08005476 if (qc->tf.flags & ATA_TFLAG_POLLING)
Albert Lee31ce6da2006-04-03 18:31:44 +08005477 ata_port_queue_task(ap, ata_pio_task, qc, 0);
Albert Lee312f7da2005-09-27 17:38:03 +08005478
Albert Lee54f00382005-09-30 19:14:19 +08005479 /* if polling, ata_pio_task() handles the rest.
5480 * otherwise, interrupt handler takes over from here.
5481 */
Albert Lee312f7da2005-09-27 17:38:03 +08005482 }
5483
Linus Torvalds1da177e2005-04-16 15:20:36 -07005484 break;
5485
5486 case ATA_PROT_ATAPI:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487 case ATA_PROT_ATAPI_NODATA:
Albert Lee312f7da2005-09-27 17:38:03 +08005488 if (qc->tf.flags & ATA_TFLAG_POLLING)
5489 ata_qc_set_polling(qc);
5490
Jeff Garzike5338252005-10-30 21:37:17 -05005491 ata_tf_to_host(ap, &qc->tf);
Jeff Garzikf6ef65e2006-01-27 02:45:00 -05005492
Albert Lee312f7da2005-09-27 17:38:03 +08005493 ap->hsm_task_state = HSM_ST_FIRST;
5494
5495 /* send cdb by polling if no cdb interrupt */
5496 if ((!(qc->dev->flags & ATA_DFLAG_CDB_INTR)) ||
5497 (qc->tf.flags & ATA_TFLAG_POLLING))
Albert Lee31ce6da2006-04-03 18:31:44 +08005498 ata_port_queue_task(ap, ata_pio_task, qc, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005499 break;
5500
5501 case ATA_PROT_ATAPI_DMA:
Jeff Garzik587005d2006-02-11 18:17:32 -05005502 WARN_ON(qc->tf.flags & ATA_TFLAG_POLLING);
Albert Lee312f7da2005-09-27 17:38:03 +08005503
Linus Torvalds1da177e2005-04-16 15:20:36 -07005504 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
5505 ap->ops->bmdma_setup(qc); /* set up bmdma */
Albert Lee312f7da2005-09-27 17:38:03 +08005506 ap->hsm_task_state = HSM_ST_FIRST;
5507
5508 /* send cdb by polling if no cdb interrupt */
5509 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
Albert Lee31ce6da2006-04-03 18:31:44 +08005510 ata_port_queue_task(ap, ata_pio_task, qc, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005511 break;
5512
5513 default:
5514 WARN_ON(1);
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09005515 return AC_ERR_SYSTEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005516 }
5517
5518 return 0;
5519}
5520
5521/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07005522 * ata_host_intr - Handle host interrupt for given (port, task)
5523 * @ap: Port on which interrupt arrived (possibly...)
5524 * @qc: Taskfile currently active in engine
5525 *
5526 * Handle host interrupt for given queued command. Currently,
5527 * only DMA interrupts are handled. All other commands are
5528 * handled via polling with interrupts disabled (nIEN bit).
5529 *
5530 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04005531 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005532 *
5533 * RETURNS:
5534 * One if interrupt was handled, zero if not (shared irq).
5535 */
5536
5537inline unsigned int ata_host_intr (struct ata_port *ap,
5538 struct ata_queued_cmd *qc)
5539{
Tejun Heoea547632006-11-17 12:06:21 +09005540 struct ata_eh_info *ehi = &ap->eh_info;
Albert Lee312f7da2005-09-27 17:38:03 +08005541 u8 status, host_stat = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005542
Albert Lee312f7da2005-09-27 17:38:03 +08005543 VPRINTK("ata%u: protocol %d task_state %d\n",
Tejun Heo44877b42007-02-21 01:06:51 +09005544 ap->print_id, qc->tf.protocol, ap->hsm_task_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005545
Albert Lee312f7da2005-09-27 17:38:03 +08005546 /* Check whether we are expecting interrupt in this state */
5547 switch (ap->hsm_task_state) {
5548 case HSM_ST_FIRST:
Albert Lee6912ccd2006-03-25 17:45:49 +08005549 /* Some pre-ATAPI-4 devices assert INTRQ
5550 * at this state when ready to receive CDB.
5551 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005552
Albert Lee312f7da2005-09-27 17:38:03 +08005553 /* Check the ATA_DFLAG_CDB_INTR flag is enough here.
5554 * The flag was turned on only for atapi devices.
5555 * No need to check is_atapi_taskfile(&qc->tf) again.
5556 */
5557 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005558 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005559 break;
Albert Lee312f7da2005-09-27 17:38:03 +08005560 case HSM_ST_LAST:
5561 if (qc->tf.protocol == ATA_PROT_DMA ||
5562 qc->tf.protocol == ATA_PROT_ATAPI_DMA) {
5563 /* check status of DMA engine */
5564 host_stat = ap->ops->bmdma_status(ap);
Tejun Heo44877b42007-02-21 01:06:51 +09005565 VPRINTK("ata%u: host_stat 0x%X\n",
5566 ap->print_id, host_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005567
Albert Lee312f7da2005-09-27 17:38:03 +08005568 /* if it's not our irq... */
5569 if (!(host_stat & ATA_DMA_INTR))
5570 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005571
Albert Lee312f7da2005-09-27 17:38:03 +08005572 /* before we do anything else, clear DMA-Start bit */
5573 ap->ops->bmdma_stop(qc);
Albert Leea4f16612005-12-26 16:40:53 +08005574
5575 if (unlikely(host_stat & ATA_DMA_ERR)) {
5576 /* error when transfering data to/from memory */
5577 qc->err_mask |= AC_ERR_HOST_BUS;
5578 ap->hsm_task_state = HSM_ST_ERR;
5579 }
Albert Lee312f7da2005-09-27 17:38:03 +08005580 }
5581 break;
5582 case HSM_ST:
5583 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005584 default:
5585 goto idle_irq;
5586 }
5587
Albert Lee312f7da2005-09-27 17:38:03 +08005588 /* check altstatus */
5589 status = ata_altstatus(ap);
5590 if (status & ATA_BUSY)
5591 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005592
Albert Lee312f7da2005-09-27 17:38:03 +08005593 /* check main status, clearing INTRQ */
5594 status = ata_chk_status(ap);
5595 if (unlikely(status & ATA_BUSY))
5596 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005597
Albert Lee312f7da2005-09-27 17:38:03 +08005598 /* ack bmdma irq events */
5599 ap->ops->irq_clear(ap);
5600
Albert Leebb5cb292006-03-25 17:48:02 +08005601 ata_hsm_move(ap, qc, status, 0);
Tejun Heoea547632006-11-17 12:06:21 +09005602
5603 if (unlikely(qc->err_mask) && (qc->tf.protocol == ATA_PROT_DMA ||
5604 qc->tf.protocol == ATA_PROT_ATAPI_DMA))
5605 ata_ehi_push_desc(ehi, "BMDMA stat 0x%x", host_stat);
5606
Linus Torvalds1da177e2005-04-16 15:20:36 -07005607 return 1; /* irq handled */
5608
5609idle_irq:
5610 ap->stats.idle_irq++;
5611
5612#ifdef ATA_IRQ_TRAP
5613 if ((ap->stats.idle_irq % 1000) == 0) {
Akira Iguchi83625002007-01-26 16:27:32 +09005614 ap->ops->irq_ack(ap, 0); /* debug trap */
Tejun Heof15a1da2006-05-15 20:57:56 +09005615 ata_port_printk(ap, KERN_WARNING, "irq trap\n");
Alan Cox23cfce82006-03-21 16:06:53 +00005616 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617 }
5618#endif
5619 return 0; /* irq not handled */
5620}
5621
5622/**
5623 * ata_interrupt - Default ATA host interrupt handler
Jeff Garzik0cba6322005-05-30 19:49:12 -04005624 * @irq: irq line (unused)
Jeff Garzikcca39742006-08-24 03:19:22 -04005625 * @dev_instance: pointer to our ata_host information structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07005626 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04005627 * Default interrupt handler for PCI IDE devices. Calls
5628 * ata_host_intr() for each port that is not disabled.
5629 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005630 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04005631 * Obtains host lock during operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005632 *
5633 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04005634 * IRQ_NONE or IRQ_HANDLED.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635 */
5636
David Howells7d12e782006-10-05 14:55:46 +01005637irqreturn_t ata_interrupt (int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005638{
Jeff Garzikcca39742006-08-24 03:19:22 -04005639 struct ata_host *host = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005640 unsigned int i;
5641 unsigned int handled = 0;
5642 unsigned long flags;
5643
5644 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
Jeff Garzikcca39742006-08-24 03:19:22 -04005645 spin_lock_irqsave(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005646
Jeff Garzikcca39742006-08-24 03:19:22 -04005647 for (i = 0; i < host->n_ports; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005648 struct ata_port *ap;
5649
Jeff Garzikcca39742006-08-24 03:19:22 -04005650 ap = host->ports[i];
Tejun Heoc1389502005-08-22 14:59:24 +09005651 if (ap &&
Jeff Garzik029f5462006-04-02 10:30:40 -04005652 !(ap->flags & ATA_FLAG_DISABLED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005653 struct ata_queued_cmd *qc;
5654
5655 qc = ata_qc_from_tag(ap, ap->active_tag);
Albert Lee312f7da2005-09-27 17:38:03 +08005656 if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING)) &&
Albert Lee21b1ed72005-04-29 17:34:59 +08005657 (qc->flags & ATA_QCFLAG_ACTIVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005658 handled |= ata_host_intr(ap, qc);
5659 }
5660 }
5661
Jeff Garzikcca39742006-08-24 03:19:22 -04005662 spin_unlock_irqrestore(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005663
5664 return IRQ_RETVAL(handled);
5665}
5666
Tejun Heo34bf2172006-05-15 20:57:46 +09005667/**
5668 * sata_scr_valid - test whether SCRs are accessible
5669 * @ap: ATA port to test SCR accessibility for
5670 *
5671 * Test whether SCRs are accessible for @ap.
5672 *
5673 * LOCKING:
5674 * None.
5675 *
5676 * RETURNS:
5677 * 1 if SCRs are accessible, 0 otherwise.
5678 */
5679int sata_scr_valid(struct ata_port *ap)
5680{
5681 return ap->cbl == ATA_CBL_SATA && ap->ops->scr_read;
5682}
5683
5684/**
5685 * sata_scr_read - read SCR register of the specified port
5686 * @ap: ATA port to read SCR for
5687 * @reg: SCR to read
5688 * @val: Place to store read value
5689 *
5690 * Read SCR register @reg of @ap into *@val. This function is
5691 * guaranteed to succeed if the cable type of the port is SATA
5692 * and the port implements ->scr_read.
5693 *
5694 * LOCKING:
5695 * None.
5696 *
5697 * RETURNS:
5698 * 0 on success, negative errno on failure.
5699 */
5700int sata_scr_read(struct ata_port *ap, int reg, u32 *val)
5701{
5702 if (sata_scr_valid(ap)) {
5703 *val = ap->ops->scr_read(ap, reg);
5704 return 0;
5705 }
5706 return -EOPNOTSUPP;
5707}
5708
5709/**
5710 * sata_scr_write - write SCR register of the specified port
5711 * @ap: ATA port to write SCR for
5712 * @reg: SCR to write
5713 * @val: value to write
5714 *
5715 * Write @val to SCR register @reg of @ap. This function is
5716 * guaranteed to succeed if the cable type of the port is SATA
5717 * and the port implements ->scr_read.
5718 *
5719 * LOCKING:
5720 * None.
5721 *
5722 * RETURNS:
5723 * 0 on success, negative errno on failure.
5724 */
5725int sata_scr_write(struct ata_port *ap, int reg, u32 val)
5726{
5727 if (sata_scr_valid(ap)) {
5728 ap->ops->scr_write(ap, reg, val);
5729 return 0;
5730 }
5731 return -EOPNOTSUPP;
5732}
5733
5734/**
5735 * sata_scr_write_flush - write SCR register of the specified port and flush
5736 * @ap: ATA port to write SCR for
5737 * @reg: SCR to write
5738 * @val: value to write
5739 *
5740 * This function is identical to sata_scr_write() except that this
5741 * function performs flush after writing to the register.
5742 *
5743 * LOCKING:
5744 * None.
5745 *
5746 * RETURNS:
5747 * 0 on success, negative errno on failure.
5748 */
5749int sata_scr_write_flush(struct ata_port *ap, int reg, u32 val)
5750{
5751 if (sata_scr_valid(ap)) {
5752 ap->ops->scr_write(ap, reg, val);
5753 ap->ops->scr_read(ap, reg);
5754 return 0;
5755 }
5756 return -EOPNOTSUPP;
5757}
5758
5759/**
5760 * ata_port_online - test whether the given port is online
5761 * @ap: ATA port to test
5762 *
5763 * Test whether @ap is online. Note that this function returns 0
5764 * if online status of @ap cannot be obtained, so
5765 * ata_port_online(ap) != !ata_port_offline(ap).
5766 *
5767 * LOCKING:
5768 * None.
5769 *
5770 * RETURNS:
5771 * 1 if the port online status is available and online.
5772 */
5773int ata_port_online(struct ata_port *ap)
5774{
5775 u32 sstatus;
5776
5777 if (!sata_scr_read(ap, SCR_STATUS, &sstatus) && (sstatus & 0xf) == 0x3)
5778 return 1;
5779 return 0;
5780}
5781
5782/**
5783 * ata_port_offline - test whether the given port is offline
5784 * @ap: ATA port to test
5785 *
5786 * Test whether @ap is offline. Note that this function returns
5787 * 0 if offline status of @ap cannot be obtained, so
5788 * ata_port_online(ap) != !ata_port_offline(ap).
5789 *
5790 * LOCKING:
5791 * None.
5792 *
5793 * RETURNS:
5794 * 1 if the port offline status is available and offline.
5795 */
5796int ata_port_offline(struct ata_port *ap)
5797{
5798 u32 sstatus;
5799
5800 if (!sata_scr_read(ap, SCR_STATUS, &sstatus) && (sstatus & 0xf) != 0x3)
5801 return 1;
5802 return 0;
5803}
Edward Falk0baab862005-06-02 18:17:13 -04005804
Tejun Heo77b08fb2006-06-24 20:30:19 +09005805int ata_flush_cache(struct ata_device *dev)
Jens Axboe9b847542006-01-06 09:28:07 +01005806{
Tejun Heo977e6b92006-06-24 20:30:19 +09005807 unsigned int err_mask;
Jens Axboe9b847542006-01-06 09:28:07 +01005808 u8 cmd;
5809
5810 if (!ata_try_flush_cache(dev))
5811 return 0;
5812
Tejun Heo6fc49ad2006-11-11 20:10:45 +09005813 if (dev->flags & ATA_DFLAG_FLUSH_EXT)
Jens Axboe9b847542006-01-06 09:28:07 +01005814 cmd = ATA_CMD_FLUSH_EXT;
5815 else
5816 cmd = ATA_CMD_FLUSH;
5817
Tejun Heo977e6b92006-06-24 20:30:19 +09005818 err_mask = ata_do_simple_cmd(dev, cmd);
5819 if (err_mask) {
5820 ata_dev_printk(dev, KERN_ERR, "failed to flush cache\n");
5821 return -EIO;
5822 }
5823
5824 return 0;
Jens Axboe9b847542006-01-06 09:28:07 +01005825}
5826
Tejun Heo6ffa01d2007-03-02 17:32:47 +09005827#ifdef CONFIG_PM
Jeff Garzikcca39742006-08-24 03:19:22 -04005828static int ata_host_request_pm(struct ata_host *host, pm_message_t mesg,
5829 unsigned int action, unsigned int ehi_flags,
5830 int wait)
Tejun Heo500530f2006-07-03 16:07:27 +09005831{
5832 unsigned long flags;
5833 int i, rc;
5834
Jeff Garzikcca39742006-08-24 03:19:22 -04005835 for (i = 0; i < host->n_ports; i++) {
5836 struct ata_port *ap = host->ports[i];
Tejun Heo500530f2006-07-03 16:07:27 +09005837
5838 /* Previous resume operation might still be in
5839 * progress. Wait for PM_PENDING to clear.
5840 */
5841 if (ap->pflags & ATA_PFLAG_PM_PENDING) {
5842 ata_port_wait_eh(ap);
5843 WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
5844 }
5845
5846 /* request PM ops to EH */
5847 spin_lock_irqsave(ap->lock, flags);
5848
5849 ap->pm_mesg = mesg;
5850 if (wait) {
5851 rc = 0;
5852 ap->pm_result = &rc;
5853 }
5854
5855 ap->pflags |= ATA_PFLAG_PM_PENDING;
5856 ap->eh_info.action |= action;
5857 ap->eh_info.flags |= ehi_flags;
5858
5859 ata_port_schedule_eh(ap);
5860
5861 spin_unlock_irqrestore(ap->lock, flags);
5862
5863 /* wait and check result */
5864 if (wait) {
5865 ata_port_wait_eh(ap);
5866 WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
5867 if (rc)
5868 return rc;
5869 }
5870 }
5871
5872 return 0;
5873}
5874
5875/**
Jeff Garzikcca39742006-08-24 03:19:22 -04005876 * ata_host_suspend - suspend host
5877 * @host: host to suspend
Tejun Heo500530f2006-07-03 16:07:27 +09005878 * @mesg: PM message
5879 *
Jeff Garzikcca39742006-08-24 03:19:22 -04005880 * Suspend @host. Actual operation is performed by EH. This
Tejun Heo500530f2006-07-03 16:07:27 +09005881 * function requests EH to perform PM operations and waits for EH
5882 * to finish.
5883 *
5884 * LOCKING:
5885 * Kernel thread context (may sleep).
5886 *
5887 * RETURNS:
5888 * 0 on success, -errno on failure.
5889 */
Jeff Garzikcca39742006-08-24 03:19:22 -04005890int ata_host_suspend(struct ata_host *host, pm_message_t mesg)
Tejun Heo500530f2006-07-03 16:07:27 +09005891{
Tejun Heo9666f402007-05-04 21:27:47 +02005892 int rc;
Tejun Heo500530f2006-07-03 16:07:27 +09005893
Jeff Garzikcca39742006-08-24 03:19:22 -04005894 rc = ata_host_request_pm(host, mesg, 0, ATA_EHI_QUIET, 1);
Tejun Heo9666f402007-05-04 21:27:47 +02005895 if (rc == 0)
5896 host->dev->power.power_state = mesg;
Tejun Heo500530f2006-07-03 16:07:27 +09005897 return rc;
5898}
5899
5900/**
Jeff Garzikcca39742006-08-24 03:19:22 -04005901 * ata_host_resume - resume host
5902 * @host: host to resume
Tejun Heo500530f2006-07-03 16:07:27 +09005903 *
Jeff Garzikcca39742006-08-24 03:19:22 -04005904 * Resume @host. Actual operation is performed by EH. This
Tejun Heo500530f2006-07-03 16:07:27 +09005905 * function requests EH to perform PM operations and returns.
5906 * Note that all resume operations are performed parallely.
5907 *
5908 * LOCKING:
5909 * Kernel thread context (may sleep).
5910 */
Jeff Garzikcca39742006-08-24 03:19:22 -04005911void ata_host_resume(struct ata_host *host)
Tejun Heo500530f2006-07-03 16:07:27 +09005912{
Jeff Garzikcca39742006-08-24 03:19:22 -04005913 ata_host_request_pm(host, PMSG_ON, ATA_EH_SOFTRESET,
5914 ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET, 0);
5915 host->dev->power.power_state = PMSG_ON;
Tejun Heo500530f2006-07-03 16:07:27 +09005916}
Tejun Heo6ffa01d2007-03-02 17:32:47 +09005917#endif
Tejun Heo500530f2006-07-03 16:07:27 +09005918
Randy Dunlapc893a3a2006-01-28 13:15:32 -05005919/**
5920 * ata_port_start - Set port up for dma.
5921 * @ap: Port to initialize
5922 *
5923 * Called just after data structures for each port are
5924 * initialized. Allocates space for PRD table.
5925 *
5926 * May be used as the port_start() entry in ata_port_operations.
5927 *
5928 * LOCKING:
5929 * Inherited from caller.
5930 */
Tejun Heof0d36ef2007-01-20 16:00:28 +09005931int ata_port_start(struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005932{
Brian King2f1f6102006-03-23 17:30:15 -06005933 struct device *dev = ap->dev;
Jeff Garzik6037d6b2005-11-04 22:08:00 -05005934 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005935
Tejun Heof0d36ef2007-01-20 16:00:28 +09005936 ap->prd = dmam_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma,
5937 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005938 if (!ap->prd)
5939 return -ENOMEM;
5940
Jeff Garzik6037d6b2005-11-04 22:08:00 -05005941 rc = ata_pad_alloc(ap, dev);
Tejun Heof0d36ef2007-01-20 16:00:28 +09005942 if (rc)
Jeff Garzik6037d6b2005-11-04 22:08:00 -05005943 return rc;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04005944
Tejun Heof0d36ef2007-01-20 16:00:28 +09005945 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd,
5946 (unsigned long long)ap->prd_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005947 return 0;
5948}
5949
Edward Falk0baab862005-06-02 18:17:13 -04005950/**
Tejun Heo3ef3b432006-05-31 18:27:30 +09005951 * ata_dev_init - Initialize an ata_device structure
5952 * @dev: Device structure to initialize
5953 *
5954 * Initialize @dev in preparation for probing.
5955 *
5956 * LOCKING:
5957 * Inherited from caller.
5958 */
5959void ata_dev_init(struct ata_device *dev)
5960{
5961 struct ata_port *ap = dev->ap;
Tejun Heo72fa4b72006-05-31 18:27:32 +09005962 unsigned long flags;
Tejun Heo3ef3b432006-05-31 18:27:30 +09005963
Tejun Heo5a04bf42006-05-31 18:27:38 +09005964 /* SATA spd limit is bound to the first device */
5965 ap->sata_spd_limit = ap->hw_sata_spd_limit;
5966
Tejun Heo72fa4b72006-05-31 18:27:32 +09005967 /* High bits of dev->flags are used to record warm plug
5968 * requests which occur asynchronously. Synchronize using
Jeff Garzikcca39742006-08-24 03:19:22 -04005969 * host lock.
Tejun Heo72fa4b72006-05-31 18:27:32 +09005970 */
Jeff Garzikba6a1302006-06-22 23:46:10 -04005971 spin_lock_irqsave(ap->lock, flags);
Tejun Heo72fa4b72006-05-31 18:27:32 +09005972 dev->flags &= ~ATA_DFLAG_INIT_MASK;
Jeff Garzikba6a1302006-06-22 23:46:10 -04005973 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo72fa4b72006-05-31 18:27:32 +09005974
5975 memset((void *)dev + ATA_DEVICE_CLEAR_OFFSET, 0,
5976 sizeof(*dev) - ATA_DEVICE_CLEAR_OFFSET);
Tejun Heo3ef3b432006-05-31 18:27:30 +09005977 dev->pio_mask = UINT_MAX;
5978 dev->mwdma_mask = UINT_MAX;
5979 dev->udma_mask = UINT_MAX;
5980}
5981
5982/**
Tejun Heof3187192007-04-17 23:44:07 +09005983 * ata_port_alloc - allocate and initialize basic ATA port resources
5984 * @host: ATA host this allocated port belongs to
Linus Torvalds1da177e2005-04-16 15:20:36 -07005985 *
Tejun Heof3187192007-04-17 23:44:07 +09005986 * Allocate and initialize basic ATA port resources.
5987 *
5988 * RETURNS:
5989 * Allocate ATA port on success, NULL on failure.
Jeff Garzik0cba6322005-05-30 19:49:12 -04005990 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005991 * LOCKING:
Tejun Heof3187192007-04-17 23:44:07 +09005992 * Inherited from calling layer (may sleep).
Linus Torvalds1da177e2005-04-16 15:20:36 -07005993 */
Tejun Heof3187192007-04-17 23:44:07 +09005994struct ata_port *ata_port_alloc(struct ata_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005995{
Tejun Heof3187192007-04-17 23:44:07 +09005996 struct ata_port *ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005997 unsigned int i;
5998
Tejun Heof3187192007-04-17 23:44:07 +09005999 DPRINTK("ENTER\n");
6000
6001 ap = kzalloc(sizeof(*ap), GFP_KERNEL);
6002 if (!ap)
6003 return NULL;
6004
Tejun Heof4d6d002007-05-01 11:50:15 +02006005 ap->pflags |= ATA_PFLAG_INITIALIZING;
Jeff Garzikcca39742006-08-24 03:19:22 -04006006 ap->lock = &host->lock;
Tejun Heo198e0fe2006-04-02 18:51:52 +09006007 ap->flags = ATA_FLAG_DISABLED;
Tejun Heof3187192007-04-17 23:44:07 +09006008 ap->print_id = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006009 ap->ctl = ATA_DEVCTL_OBS;
Jeff Garzikcca39742006-08-24 03:19:22 -04006010 ap->host = host;
Tejun Heof3187192007-04-17 23:44:07 +09006011 ap->dev = host->dev;
6012
Tejun Heo5a04bf42006-05-31 18:27:38 +09006013 ap->hw_sata_spd_limit = UINT_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006014 ap->active_tag = ATA_TAG_POISON;
6015 ap->last_ctl = 0xFF;
Borislav Petkovbd5d8252006-06-11 23:17:01 -04006016
6017#if defined(ATA_VERBOSE_DEBUG)
6018 /* turn on all debugging levels */
6019 ap->msg_enable = 0x00FF;
6020#elif defined(ATA_DEBUG)
6021 ap->msg_enable = ATA_MSG_DRV | ATA_MSG_INFO | ATA_MSG_CTL | ATA_MSG_WARN | ATA_MSG_ERR;
Tejun Heo88574552006-06-25 20:00:35 +09006022#else
Borislav Petkov0dd4b212006-06-23 02:29:08 -04006023 ap->msg_enable = ATA_MSG_DRV | ATA_MSG_ERR | ATA_MSG_WARN;
Borislav Petkovbd5d8252006-06-11 23:17:01 -04006024#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006025
David Howells65f27f32006-11-22 14:55:48 +00006026 INIT_DELAYED_WORK(&ap->port_task, NULL);
6027 INIT_DELAYED_WORK(&ap->hotplug_task, ata_scsi_hotplug);
6028 INIT_WORK(&ap->scsi_rescan_task, ata_scsi_dev_rescan);
Tejun Heoa72ec4c2006-01-23 13:09:37 +09006029 INIT_LIST_HEAD(&ap->eh_done_q);
Tejun Heoc6cf9e92006-05-31 18:27:27 +09006030 init_waitqueue_head(&ap->eh_wait_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006031
Tejun Heo838df622006-05-15 20:57:44 +09006032 ap->cbl = ATA_CBL_NONE;
Tejun Heo838df622006-05-15 20:57:44 +09006033
Tejun Heoacf356b2006-03-24 14:07:50 +09006034 for (i = 0; i < ATA_MAX_DEVICES; i++) {
6035 struct ata_device *dev = &ap->device[i];
Tejun Heo38d87232006-05-15 20:57:51 +09006036 dev->ap = ap;
Tejun Heo72fa4b72006-05-31 18:27:32 +09006037 dev->devno = i;
Tejun Heo3ef3b432006-05-31 18:27:30 +09006038 ata_dev_init(dev);
Tejun Heoacf356b2006-03-24 14:07:50 +09006039 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006040
6041#ifdef ATA_IRQ_TRAP
6042 ap->stats.unhandled_irq = 1;
6043 ap->stats.idle_irq = 1;
6044#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006045 return ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006046}
6047
Tejun Heof0d36ef2007-01-20 16:00:28 +09006048static void ata_host_release(struct device *gendev, void *res)
6049{
6050 struct ata_host *host = dev_get_drvdata(gendev);
6051 int i;
6052
6053 for (i = 0; i < host->n_ports; i++) {
6054 struct ata_port *ap = host->ports[i];
6055
Tejun Heoecef7252007-04-17 23:44:06 +09006056 if (!ap)
6057 continue;
6058
6059 if ((host->flags & ATA_HOST_STARTED) && ap->ops->port_stop)
Tejun Heof0d36ef2007-01-20 16:00:28 +09006060 ap->ops->port_stop(ap);
Tejun Heof0d36ef2007-01-20 16:00:28 +09006061 }
6062
Tejun Heoecef7252007-04-17 23:44:06 +09006063 if ((host->flags & ATA_HOST_STARTED) && host->ops->host_stop)
Tejun Heof0d36ef2007-01-20 16:00:28 +09006064 host->ops->host_stop(host);
Tejun Heo1aa56cc2007-02-27 22:33:21 +09006065
Tejun Heo1aa506e42007-03-09 19:36:12 +09006066 for (i = 0; i < host->n_ports; i++) {
6067 struct ata_port *ap = host->ports[i];
6068
Tejun Heo49114872007-04-17 23:44:06 +09006069 if (!ap)
6070 continue;
6071
6072 if (ap->scsi_host)
Tejun Heo1aa506e42007-03-09 19:36:12 +09006073 scsi_host_put(ap->scsi_host);
6074
Tejun Heo49114872007-04-17 23:44:06 +09006075 kfree(ap);
Tejun Heo1aa506e42007-03-09 19:36:12 +09006076 host->ports[i] = NULL;
6077 }
6078
Tejun Heo1aa56cc2007-02-27 22:33:21 +09006079 dev_set_drvdata(gendev, NULL);
Tejun Heof0d36ef2007-01-20 16:00:28 +09006080}
6081
Linus Torvalds1da177e2005-04-16 15:20:36 -07006082/**
Tejun Heof3187192007-04-17 23:44:07 +09006083 * ata_host_alloc - allocate and init basic ATA host resources
6084 * @dev: generic device this host is associated with
6085 * @max_ports: maximum number of ATA ports associated with this host
6086 *
6087 * Allocate and initialize basic ATA host resources. LLD calls
6088 * this function to allocate a host, initializes it fully and
6089 * attaches it using ata_host_register().
6090 *
6091 * @max_ports ports are allocated and host->n_ports is
6092 * initialized to @max_ports. The caller is allowed to decrease
6093 * host->n_ports before calling ata_host_register(). The unused
6094 * ports will be automatically freed on registration.
6095 *
6096 * RETURNS:
6097 * Allocate ATA host on success, NULL on failure.
6098 *
6099 * LOCKING:
6100 * Inherited from calling layer (may sleep).
6101 */
6102struct ata_host *ata_host_alloc(struct device *dev, int max_ports)
6103{
6104 struct ata_host *host;
6105 size_t sz;
6106 int i;
6107
6108 DPRINTK("ENTER\n");
6109
6110 if (!devres_open_group(dev, NULL, GFP_KERNEL))
6111 return NULL;
6112
6113 /* alloc a container for our list of ATA ports (buses) */
6114 sz = sizeof(struct ata_host) + (max_ports + 1) * sizeof(void *);
6115 /* alloc a container for our list of ATA ports (buses) */
6116 host = devres_alloc(ata_host_release, sz, GFP_KERNEL);
6117 if (!host)
6118 goto err_out;
6119
6120 devres_add(dev, host);
6121 dev_set_drvdata(dev, host);
6122
6123 spin_lock_init(&host->lock);
6124 host->dev = dev;
6125 host->n_ports = max_ports;
6126
6127 /* allocate ports bound to this host */
6128 for (i = 0; i < max_ports; i++) {
6129 struct ata_port *ap;
6130
6131 ap = ata_port_alloc(host);
6132 if (!ap)
6133 goto err_out;
6134
6135 ap->port_no = i;
6136 host->ports[i] = ap;
6137 }
6138
6139 devres_remove_group(dev, NULL);
6140 return host;
6141
6142 err_out:
6143 devres_release_group(dev, NULL);
6144 return NULL;
6145}
6146
6147/**
Tejun Heof5cda252007-04-17 23:44:07 +09006148 * ata_host_alloc_pinfo - alloc host and init with port_info array
6149 * @dev: generic device this host is associated with
6150 * @ppi: array of ATA port_info to initialize host with
6151 * @n_ports: number of ATA ports attached to this host
6152 *
6153 * Allocate ATA host and initialize with info from @ppi. If NULL
6154 * terminated, @ppi may contain fewer entries than @n_ports. The
6155 * last entry will be used for the remaining ports.
6156 *
6157 * RETURNS:
6158 * Allocate ATA host on success, NULL on failure.
6159 *
6160 * LOCKING:
6161 * Inherited from calling layer (may sleep).
6162 */
6163struct ata_host *ata_host_alloc_pinfo(struct device *dev,
6164 const struct ata_port_info * const * ppi,
6165 int n_ports)
6166{
6167 const struct ata_port_info *pi;
6168 struct ata_host *host;
6169 int i, j;
6170
6171 host = ata_host_alloc(dev, n_ports);
6172 if (!host)
6173 return NULL;
6174
6175 for (i = 0, j = 0, pi = NULL; i < host->n_ports; i++) {
6176 struct ata_port *ap = host->ports[i];
6177
6178 if (ppi[j])
6179 pi = ppi[j++];
6180
6181 ap->pio_mask = pi->pio_mask;
6182 ap->mwdma_mask = pi->mwdma_mask;
6183 ap->udma_mask = pi->udma_mask;
6184 ap->flags |= pi->flags;
6185 ap->ops = pi->port_ops;
6186
6187 if (!host->ops && (pi->port_ops != &ata_dummy_port_ops))
6188 host->ops = pi->port_ops;
6189 if (!host->private_data && pi->private_data)
6190 host->private_data = pi->private_data;
6191 }
6192
6193 return host;
6194}
6195
6196/**
Tejun Heoecef7252007-04-17 23:44:06 +09006197 * ata_host_start - start and freeze ports of an ATA host
6198 * @host: ATA host to start ports for
6199 *
6200 * Start and then freeze ports of @host. Started status is
6201 * recorded in host->flags, so this function can be called
6202 * multiple times. Ports are guaranteed to get started only
Tejun Heof3187192007-04-17 23:44:07 +09006203 * once. If host->ops isn't initialized yet, its set to the
6204 * first non-dummy port ops.
Tejun Heoecef7252007-04-17 23:44:06 +09006205 *
6206 * LOCKING:
6207 * Inherited from calling layer (may sleep).
6208 *
6209 * RETURNS:
6210 * 0 if all ports are started successfully, -errno otherwise.
6211 */
6212int ata_host_start(struct ata_host *host)
6213{
6214 int i, rc;
6215
6216 if (host->flags & ATA_HOST_STARTED)
6217 return 0;
6218
6219 for (i = 0; i < host->n_ports; i++) {
6220 struct ata_port *ap = host->ports[i];
6221
Tejun Heof3187192007-04-17 23:44:07 +09006222 if (!host->ops && !ata_port_is_dummy(ap))
6223 host->ops = ap->ops;
6224
Tejun Heoecef7252007-04-17 23:44:06 +09006225 if (ap->ops->port_start) {
6226 rc = ap->ops->port_start(ap);
6227 if (rc) {
6228 ata_port_printk(ap, KERN_ERR, "failed to "
6229 "start port (errno=%d)\n", rc);
6230 goto err_out;
6231 }
6232 }
6233
6234 ata_eh_freeze_port(ap);
6235 }
6236
6237 host->flags |= ATA_HOST_STARTED;
6238 return 0;
6239
6240 err_out:
6241 while (--i >= 0) {
6242 struct ata_port *ap = host->ports[i];
6243
6244 if (ap->ops->port_stop)
6245 ap->ops->port_stop(ap);
6246 }
6247 return rc;
6248}
6249
6250/**
Jeff Garzikcca39742006-08-24 03:19:22 -04006251 * ata_sas_host_init - Initialize a host struct
6252 * @host: host to initialize
6253 * @dev: device host is attached to
6254 * @flags: host flags
6255 * @ops: port_ops
Brian Kingb03732f2006-08-07 14:27:10 -05006256 *
6257 * LOCKING:
6258 * PCI/etc. bus probe sem.
6259 *
6260 */
Tejun Heof3187192007-04-17 23:44:07 +09006261/* KILLME - the only user left is ipr */
Jeff Garzikcca39742006-08-24 03:19:22 -04006262void ata_host_init(struct ata_host *host, struct device *dev,
6263 unsigned long flags, const struct ata_port_operations *ops)
Brian Kingb03732f2006-08-07 14:27:10 -05006264{
Jeff Garzikcca39742006-08-24 03:19:22 -04006265 spin_lock_init(&host->lock);
6266 host->dev = dev;
6267 host->flags = flags;
6268 host->ops = ops;
Brian Kingb03732f2006-08-07 14:27:10 -05006269}
6270
6271/**
Tejun Heof3187192007-04-17 23:44:07 +09006272 * ata_host_register - register initialized ATA host
6273 * @host: ATA host to register
6274 * @sht: template for SCSI host
Jeff Garzik0cba6322005-05-30 19:49:12 -04006275 *
Tejun Heof3187192007-04-17 23:44:07 +09006276 * Register initialized ATA host. @host is allocated using
6277 * ata_host_alloc() and fully initialized by LLD. This function
6278 * starts ports, registers @host with ATA and SCSI layers and
6279 * probe registered devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006280 *
6281 * LOCKING:
Tejun Heof3187192007-04-17 23:44:07 +09006282 * Inherited from calling layer (may sleep).
Linus Torvalds1da177e2005-04-16 15:20:36 -07006283 *
6284 * RETURNS:
Tejun Heof3187192007-04-17 23:44:07 +09006285 * 0 on success, -errno otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006286 */
Tejun Heof3187192007-04-17 23:44:07 +09006287int ata_host_register(struct ata_host *host, struct scsi_host_template *sht)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006288{
Tejun Heof3187192007-04-17 23:44:07 +09006289 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006290
Tejun Heof3187192007-04-17 23:44:07 +09006291 /* host must have been started */
6292 if (!(host->flags & ATA_HOST_STARTED)) {
6293 dev_printk(KERN_ERR, host->dev,
6294 "BUG: trying to register unstarted host\n");
6295 WARN_ON(1);
6296 return -EINVAL;
Alan Cox02f076a2006-09-26 17:35:32 +01006297 }
Tejun Heof0d36ef2007-01-20 16:00:28 +09006298
Tejun Heof3187192007-04-17 23:44:07 +09006299 /* Blow away unused ports. This happens when LLD can't
6300 * determine the exact number of ports to allocate at
6301 * allocation time.
6302 */
6303 for (i = host->n_ports; host->ports[i]; i++)
6304 kfree(host->ports[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006305
Tejun Heof3187192007-04-17 23:44:07 +09006306 /* give ports names and add SCSI hosts */
6307 for (i = 0; i < host->n_ports; i++)
6308 host->ports[i]->print_id = ata_print_id++;
Tejun Heof0d36ef2007-01-20 16:00:28 +09006309
Tejun Heof3187192007-04-17 23:44:07 +09006310 rc = ata_scsi_add_hosts(host, sht);
Tejun Heoecef7252007-04-17 23:44:06 +09006311 if (rc)
Tejun Heof3187192007-04-17 23:44:07 +09006312 return rc;
Tejun Heoecef7252007-04-17 23:44:06 +09006313
Tejun Heof3187192007-04-17 23:44:07 +09006314 /* set cable, sata_spd_limit and report */
Jeff Garzikcca39742006-08-24 03:19:22 -04006315 for (i = 0; i < host->n_ports; i++) {
6316 struct ata_port *ap = host->ports[i];
Tejun Heof3187192007-04-17 23:44:07 +09006317 int irq_line;
Tejun Heo5a04bf42006-05-31 18:27:38 +09006318 u32 scontrol;
Tejun Heof3187192007-04-17 23:44:07 +09006319 unsigned long xfer_mask;
6320
6321 /* set SATA cable type if still unset */
6322 if (ap->cbl == ATA_CBL_NONE && (ap->flags & ATA_FLAG_SATA))
6323 ap->cbl = ATA_CBL_SATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006324
Tejun Heo5a04bf42006-05-31 18:27:38 +09006325 /* init sata_spd_limit to the current value */
6326 if (sata_scr_read(ap, SCR_CONTROL, &scontrol) == 0) {
6327 int spd = (scontrol >> 4) & 0xf;
Tejun Heoafe3cc52007-06-06 16:35:55 +09006328 if (spd)
6329 ap->hw_sata_spd_limit &= (1 << spd) - 1;
Tejun Heo5a04bf42006-05-31 18:27:38 +09006330 }
6331 ap->sata_spd_limit = ap->hw_sata_spd_limit;
6332
Tejun Heof3187192007-04-17 23:44:07 +09006333 /* report the secondary IRQ for second channel legacy */
6334 irq_line = host->irq;
6335 if (i == 1 && host->irq2)
6336 irq_line = host->irq2;
Tejun Heo3e706392006-05-31 18:28:11 +09006337
Tejun Heof3187192007-04-17 23:44:07 +09006338 xfer_mask = ata_pack_xfermask(ap->pio_mask, ap->mwdma_mask,
6339 ap->udma_mask);
6340
6341 /* print per-port info to dmesg */
6342 if (!ata_port_is_dummy(ap))
6343 ata_port_printk(ap, KERN_INFO, "%cATA max %s cmd 0x%p "
6344 "ctl 0x%p bmdma 0x%p irq %d\n",
6345 ap->cbl == ATA_CBL_SATA ? 'S' : 'P',
6346 ata_mode_string(xfer_mask),
6347 ap->ioaddr.cmd_addr,
6348 ap->ioaddr.ctl_addr,
6349 ap->ioaddr.bmdma_addr,
6350 irq_line);
6351 else
6352 ata_port_printk(ap, KERN_INFO, "DUMMY\n");
6353 }
6354
6355 /* perform each probe synchronously */
6356 DPRINTK("probe begin\n");
6357 for (i = 0; i < host->n_ports; i++) {
6358 struct ata_port *ap = host->ports[i];
6359 int rc;
6360
6361 /* probe */
Tejun Heo52783c52006-05-31 18:28:22 +09006362 if (ap->ops->error_handler) {
Tejun Heo1cdaf532006-07-03 16:07:26 +09006363 struct ata_eh_info *ehi = &ap->eh_info;
Tejun Heo3e706392006-05-31 18:28:11 +09006364 unsigned long flags;
6365
6366 ata_port_probe(ap);
6367
6368 /* kick EH for boot probing */
Jeff Garzikba6a1302006-06-22 23:46:10 -04006369 spin_lock_irqsave(ap->lock, flags);
Tejun Heo3e706392006-05-31 18:28:11 +09006370
Tejun Heo1cdaf532006-07-03 16:07:26 +09006371 ehi->probe_mask = (1 << ATA_MAX_DEVICES) - 1;
6372 ehi->action |= ATA_EH_SOFTRESET;
6373 ehi->flags |= ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET;
Tejun Heo3e706392006-05-31 18:28:11 +09006374
Tejun Heof4d6d002007-05-01 11:50:15 +02006375 ap->pflags &= ~ATA_PFLAG_INITIALIZING;
Tejun Heob51e9e52006-06-29 01:29:30 +09006376 ap->pflags |= ATA_PFLAG_LOADING;
Tejun Heo3e706392006-05-31 18:28:11 +09006377 ata_port_schedule_eh(ap);
6378
Jeff Garzikba6a1302006-06-22 23:46:10 -04006379 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo3e706392006-05-31 18:28:11 +09006380
6381 /* wait for EH to finish */
6382 ata_port_wait_eh(ap);
6383 } else {
Tejun Heo44877b42007-02-21 01:06:51 +09006384 DPRINTK("ata%u: bus probe begin\n", ap->print_id);
Tejun Heo3e706392006-05-31 18:28:11 +09006385 rc = ata_bus_probe(ap);
Tejun Heo44877b42007-02-21 01:06:51 +09006386 DPRINTK("ata%u: bus probe end\n", ap->print_id);
Tejun Heo3e706392006-05-31 18:28:11 +09006387
6388 if (rc) {
6389 /* FIXME: do something useful here?
6390 * Current libata behavior will
6391 * tear down everything when
6392 * the module is removed
6393 * or the h/w is unplugged.
6394 */
6395 }
6396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006397 }
6398
6399 /* probes are done, now scan each port's disk(s) */
Randy Dunlapc893a3a2006-01-28 13:15:32 -05006400 DPRINTK("host probe begin\n");
Jeff Garzikcca39742006-08-24 03:19:22 -04006401 for (i = 0; i < host->n_ports; i++) {
6402 struct ata_port *ap = host->ports[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006403
Jeff Garzik644dd0c2005-10-03 15:55:19 -04006404 ata_scsi_scan_host(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006405 }
6406
Tejun Heof3187192007-04-17 23:44:07 +09006407 return 0;
6408}
6409
6410/**
Tejun Heof5cda252007-04-17 23:44:07 +09006411 * ata_host_activate - start host, request IRQ and register it
6412 * @host: target ATA host
6413 * @irq: IRQ to request
6414 * @irq_handler: irq_handler used when requesting IRQ
6415 * @irq_flags: irq_flags used when requesting IRQ
6416 * @sht: scsi_host_template to use when registering the host
6417 *
6418 * After allocating an ATA host and initializing it, most libata
6419 * LLDs perform three steps to activate the host - start host,
6420 * request IRQ and register it. This helper takes necessasry
6421 * arguments and performs the three steps in one go.
6422 *
6423 * LOCKING:
6424 * Inherited from calling layer (may sleep).
6425 *
6426 * RETURNS:
6427 * 0 on success, -errno otherwise.
6428 */
6429int ata_host_activate(struct ata_host *host, int irq,
6430 irq_handler_t irq_handler, unsigned long irq_flags,
6431 struct scsi_host_template *sht)
6432{
6433 int rc;
6434
6435 rc = ata_host_start(host);
6436 if (rc)
6437 return rc;
6438
6439 rc = devm_request_irq(host->dev, irq, irq_handler, irq_flags,
6440 dev_driver_string(host->dev), host);
6441 if (rc)
6442 return rc;
6443
6444 rc = ata_host_register(host, sht);
6445 /* if failed, just free the IRQ and leave ports alone */
6446 if (rc)
6447 devm_free_irq(host->dev, irq, host);
6448
Olof Johansson22888422007-06-03 18:35:10 -05006449 /* Used to print device info at probe */
6450 host->irq = irq;
6451
Tejun Heof5cda252007-04-17 23:44:07 +09006452 return rc;
6453}
6454
6455/**
Tejun Heo720ba122006-05-31 18:28:13 +09006456 * ata_port_detach - Detach ATA port in prepration of device removal
6457 * @ap: ATA port to be detached
6458 *
6459 * Detach all ATA devices and the associated SCSI devices of @ap;
6460 * then, remove the associated SCSI host. @ap is guaranteed to
6461 * be quiescent on return from this function.
6462 *
6463 * LOCKING:
6464 * Kernel thread context (may sleep).
6465 */
6466void ata_port_detach(struct ata_port *ap)
6467{
6468 unsigned long flags;
6469 int i;
6470
6471 if (!ap->ops->error_handler)
Tejun Heoc3cf30a2006-08-05 03:59:11 +09006472 goto skip_eh;
Tejun Heo720ba122006-05-31 18:28:13 +09006473
6474 /* tell EH we're leaving & flush EH */
Jeff Garzikba6a1302006-06-22 23:46:10 -04006475 spin_lock_irqsave(ap->lock, flags);
Tejun Heob51e9e52006-06-29 01:29:30 +09006476 ap->pflags |= ATA_PFLAG_UNLOADING;
Jeff Garzikba6a1302006-06-22 23:46:10 -04006477 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo720ba122006-05-31 18:28:13 +09006478
6479 ata_port_wait_eh(ap);
6480
6481 /* EH is now guaranteed to see UNLOADING, so no new device
6482 * will be attached. Disable all existing devices.
6483 */
Jeff Garzikba6a1302006-06-22 23:46:10 -04006484 spin_lock_irqsave(ap->lock, flags);
Tejun Heo720ba122006-05-31 18:28:13 +09006485
6486 for (i = 0; i < ATA_MAX_DEVICES; i++)
6487 ata_dev_disable(&ap->device[i]);
6488
Jeff Garzikba6a1302006-06-22 23:46:10 -04006489 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo720ba122006-05-31 18:28:13 +09006490
6491 /* Final freeze & EH. All in-flight commands are aborted. EH
6492 * will be skipped and retrials will be terminated with bad
6493 * target.
6494 */
Jeff Garzikba6a1302006-06-22 23:46:10 -04006495 spin_lock_irqsave(ap->lock, flags);
Tejun Heo720ba122006-05-31 18:28:13 +09006496 ata_port_freeze(ap); /* won't be thawed */
Jeff Garzikba6a1302006-06-22 23:46:10 -04006497 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo720ba122006-05-31 18:28:13 +09006498
6499 ata_port_wait_eh(ap);
6500
6501 /* Flush hotplug task. The sequence is similar to
6502 * ata_port_flush_task().
6503 */
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07006504 cancel_work_sync(&ap->hotplug_task.work); /* akpm: why? */
Tejun Heo720ba122006-05-31 18:28:13 +09006505 cancel_delayed_work(&ap->hotplug_task);
Oleg Nesterov28e53bd2007-05-09 02:34:22 -07006506 cancel_work_sync(&ap->hotplug_task.work);
Tejun Heo720ba122006-05-31 18:28:13 +09006507
Tejun Heoc3cf30a2006-08-05 03:59:11 +09006508 skip_eh:
Tejun Heo720ba122006-05-31 18:28:13 +09006509 /* remove the associated SCSI host */
Jeff Garzikcca39742006-08-24 03:19:22 -04006510 scsi_remove_host(ap->scsi_host);
Tejun Heo720ba122006-05-31 18:28:13 +09006511}
6512
6513/**
Tejun Heo0529c1592007-01-20 16:00:26 +09006514 * ata_host_detach - Detach all ports of an ATA host
6515 * @host: Host to detach
6516 *
6517 * Detach all ports of @host.
6518 *
6519 * LOCKING:
6520 * Kernel thread context (may sleep).
6521 */
6522void ata_host_detach(struct ata_host *host)
6523{
6524 int i;
6525
6526 for (i = 0; i < host->n_ports; i++)
6527 ata_port_detach(host->ports[i]);
6528}
6529
Linus Torvalds1da177e2005-04-16 15:20:36 -07006530/**
6531 * ata_std_ports - initialize ioaddr with standard port offsets.
6532 * @ioaddr: IO address structure to be initialized
Edward Falk0baab862005-06-02 18:17:13 -04006533 *
6534 * Utility function which initializes data_addr, error_addr,
6535 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
6536 * device_addr, status_addr, and command_addr to standard offsets
6537 * relative to cmd_addr.
6538 *
6539 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006540 */
Edward Falk0baab862005-06-02 18:17:13 -04006541
Linus Torvalds1da177e2005-04-16 15:20:36 -07006542void ata_std_ports(struct ata_ioports *ioaddr)
6543{
6544 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
6545 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
6546 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
6547 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
6548 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
6549 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
6550 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
6551 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
6552 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
6553 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
6554}
6555
Edward Falk0baab862005-06-02 18:17:13 -04006556
Jeff Garzik374b1872005-08-30 05:42:52 -04006557#ifdef CONFIG_PCI
6558
Edward Falk0baab862005-06-02 18:17:13 -04006559/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07006560 * ata_pci_remove_one - PCI layer callback for device removal
6561 * @pdev: PCI device that was removed
6562 *
Tejun Heob878ca52007-01-20 16:00:28 +09006563 * PCI layer indicates to libata via this hook that hot-unplug or
6564 * module unload event has occurred. Detach all ports. Resource
6565 * release is handled via devres.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006566 *
6567 * LOCKING:
6568 * Inherited from PCI layer (may sleep).
6569 */
Tejun Heof0d36ef2007-01-20 16:00:28 +09006570void ata_pci_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006571{
6572 struct device *dev = pci_dev_to_dev(pdev);
Jeff Garzikcca39742006-08-24 03:19:22 -04006573 struct ata_host *host = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006574
Tejun Heob878ca52007-01-20 16:00:28 +09006575 ata_host_detach(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006576}
6577
6578/* move to PCI subsystem */
Jeff Garzik057ace52005-10-22 14:27:05 -04006579int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006580{
6581 unsigned long tmp = 0;
6582
6583 switch (bits->width) {
6584 case 1: {
6585 u8 tmp8 = 0;
6586 pci_read_config_byte(pdev, bits->reg, &tmp8);
6587 tmp = tmp8;
6588 break;
6589 }
6590 case 2: {
6591 u16 tmp16 = 0;
6592 pci_read_config_word(pdev, bits->reg, &tmp16);
6593 tmp = tmp16;
6594 break;
6595 }
6596 case 4: {
6597 u32 tmp32 = 0;
6598 pci_read_config_dword(pdev, bits->reg, &tmp32);
6599 tmp = tmp32;
6600 break;
6601 }
6602
6603 default:
6604 return -EINVAL;
6605 }
6606
6607 tmp &= bits->mask;
6608
6609 return (tmp == bits->val) ? 1 : 0;
6610}
Jens Axboe9b847542006-01-06 09:28:07 +01006611
Tejun Heo6ffa01d2007-03-02 17:32:47 +09006612#ifdef CONFIG_PM
Tejun Heo3c5100c2006-07-26 16:58:33 +09006613void ata_pci_device_do_suspend(struct pci_dev *pdev, pm_message_t mesg)
Jens Axboe9b847542006-01-06 09:28:07 +01006614{
6615 pci_save_state(pdev);
Tejun Heo4c90d972007-02-20 18:14:48 +09006616 pci_disable_device(pdev);
Tejun Heo500530f2006-07-03 16:07:27 +09006617
Tejun Heo4c90d972007-02-20 18:14:48 +09006618 if (mesg.event == PM_EVENT_SUSPEND)
Tejun Heo500530f2006-07-03 16:07:27 +09006619 pci_set_power_state(pdev, PCI_D3hot);
Jens Axboe9b847542006-01-06 09:28:07 +01006620}
6621
Tejun Heo553c4aa2006-12-26 19:39:50 +09006622int ata_pci_device_do_resume(struct pci_dev *pdev)
Jens Axboe9b847542006-01-06 09:28:07 +01006623{
Tejun Heo553c4aa2006-12-26 19:39:50 +09006624 int rc;
6625
Jens Axboe9b847542006-01-06 09:28:07 +01006626 pci_set_power_state(pdev, PCI_D0);
6627 pci_restore_state(pdev);
Tejun Heo553c4aa2006-12-26 19:39:50 +09006628
Tejun Heob878ca52007-01-20 16:00:28 +09006629 rc = pcim_enable_device(pdev);
Tejun Heo553c4aa2006-12-26 19:39:50 +09006630 if (rc) {
6631 dev_printk(KERN_ERR, &pdev->dev,
6632 "failed to enable device after resume (%d)\n", rc);
6633 return rc;
6634 }
6635
Jens Axboe9b847542006-01-06 09:28:07 +01006636 pci_set_master(pdev);
Tejun Heo553c4aa2006-12-26 19:39:50 +09006637 return 0;
Tejun Heo500530f2006-07-03 16:07:27 +09006638}
6639
Tejun Heo3c5100c2006-07-26 16:58:33 +09006640int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg)
Tejun Heo500530f2006-07-03 16:07:27 +09006641{
Jeff Garzikcca39742006-08-24 03:19:22 -04006642 struct ata_host *host = dev_get_drvdata(&pdev->dev);
Tejun Heo500530f2006-07-03 16:07:27 +09006643 int rc = 0;
6644
Jeff Garzikcca39742006-08-24 03:19:22 -04006645 rc = ata_host_suspend(host, mesg);
Tejun Heo500530f2006-07-03 16:07:27 +09006646 if (rc)
6647 return rc;
6648
Tejun Heo3c5100c2006-07-26 16:58:33 +09006649 ata_pci_device_do_suspend(pdev, mesg);
Tejun Heo500530f2006-07-03 16:07:27 +09006650
6651 return 0;
6652}
6653
6654int ata_pci_device_resume(struct pci_dev *pdev)
6655{
Jeff Garzikcca39742006-08-24 03:19:22 -04006656 struct ata_host *host = dev_get_drvdata(&pdev->dev);
Tejun Heo553c4aa2006-12-26 19:39:50 +09006657 int rc;
Tejun Heo500530f2006-07-03 16:07:27 +09006658
Tejun Heo553c4aa2006-12-26 19:39:50 +09006659 rc = ata_pci_device_do_resume(pdev);
6660 if (rc == 0)
6661 ata_host_resume(host);
6662 return rc;
Jens Axboe9b847542006-01-06 09:28:07 +01006663}
Tejun Heo6ffa01d2007-03-02 17:32:47 +09006664#endif /* CONFIG_PM */
6665
Linus Torvalds1da177e2005-04-16 15:20:36 -07006666#endif /* CONFIG_PCI */
6667
6668
Linus Torvalds1da177e2005-04-16 15:20:36 -07006669static int __init ata_init(void)
6670{
Andrew Mortona8601e52006-06-25 01:36:52 -07006671 ata_probe_timeout *= HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006672 ata_wq = create_workqueue("ata");
6673 if (!ata_wq)
6674 return -ENOMEM;
6675
Tejun Heo453b07a2006-05-31 18:27:42 +09006676 ata_aux_wq = create_singlethread_workqueue("ata_aux");
6677 if (!ata_aux_wq) {
6678 destroy_workqueue(ata_wq);
6679 return -ENOMEM;
6680 }
6681
Linus Torvalds1da177e2005-04-16 15:20:36 -07006682 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
6683 return 0;
6684}
6685
6686static void __exit ata_exit(void)
6687{
6688 destroy_workqueue(ata_wq);
Tejun Heo453b07a2006-05-31 18:27:42 +09006689 destroy_workqueue(ata_aux_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006690}
6691
Brian Kinga4625082006-11-13 16:32:36 -06006692subsys_initcall(ata_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006693module_exit(ata_exit);
6694
Jeff Garzik67846b32005-10-05 02:58:32 -04006695static unsigned long ratelimit_time;
Ingo Molnar34af9462006-06-27 02:53:55 -07006696static DEFINE_SPINLOCK(ata_ratelimit_lock);
Jeff Garzik67846b32005-10-05 02:58:32 -04006697
6698int ata_ratelimit(void)
6699{
6700 int rc;
6701 unsigned long flags;
6702
6703 spin_lock_irqsave(&ata_ratelimit_lock, flags);
6704
6705 if (time_after(jiffies, ratelimit_time)) {
6706 rc = 1;
6707 ratelimit_time = jiffies + (HZ/5);
6708 } else
6709 rc = 0;
6710
6711 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
6712
6713 return rc;
6714}
6715
Tejun Heoc22daff2006-04-11 22:22:29 +09006716/**
6717 * ata_wait_register - wait until register value changes
6718 * @reg: IO-mapped register
6719 * @mask: Mask to apply to read register value
6720 * @val: Wait condition
6721 * @interval_msec: polling interval in milliseconds
6722 * @timeout_msec: timeout in milliseconds
6723 *
6724 * Waiting for some bits of register to change is a common
6725 * operation for ATA controllers. This function reads 32bit LE
6726 * IO-mapped register @reg and tests for the following condition.
6727 *
6728 * (*@reg & mask) != val
6729 *
6730 * If the condition is met, it returns; otherwise, the process is
6731 * repeated after @interval_msec until timeout.
6732 *
6733 * LOCKING:
6734 * Kernel thread context (may sleep)
6735 *
6736 * RETURNS:
6737 * The final register value.
6738 */
6739u32 ata_wait_register(void __iomem *reg, u32 mask, u32 val,
6740 unsigned long interval_msec,
6741 unsigned long timeout_msec)
6742{
6743 unsigned long timeout;
6744 u32 tmp;
6745
6746 tmp = ioread32(reg);
6747
6748 /* Calculate timeout _after_ the first read to make sure
6749 * preceding writes reach the controller before starting to
6750 * eat away the timeout.
6751 */
6752 timeout = jiffies + (timeout_msec * HZ) / 1000;
6753
6754 while ((tmp & mask) == val && time_before(jiffies, timeout)) {
6755 msleep(interval_msec);
6756 tmp = ioread32(reg);
6757 }
6758
6759 return tmp;
6760}
6761
Linus Torvalds1da177e2005-04-16 15:20:36 -07006762/*
Tejun Heodd5b06c2006-08-10 16:59:12 +09006763 * Dummy port_ops
6764 */
6765static void ata_dummy_noret(struct ata_port *ap) { }
6766static int ata_dummy_ret0(struct ata_port *ap) { return 0; }
6767static void ata_dummy_qc_noret(struct ata_queued_cmd *qc) { }
6768
6769static u8 ata_dummy_check_status(struct ata_port *ap)
6770{
6771 return ATA_DRDY;
6772}
6773
6774static unsigned int ata_dummy_qc_issue(struct ata_queued_cmd *qc)
6775{
6776 return AC_ERR_SYSTEM;
6777}
6778
6779const struct ata_port_operations ata_dummy_port_ops = {
6780 .port_disable = ata_port_disable,
6781 .check_status = ata_dummy_check_status,
6782 .check_altstatus = ata_dummy_check_status,
6783 .dev_select = ata_noop_dev_select,
6784 .qc_prep = ata_noop_qc_prep,
6785 .qc_issue = ata_dummy_qc_issue,
6786 .freeze = ata_dummy_noret,
6787 .thaw = ata_dummy_noret,
6788 .error_handler = ata_dummy_noret,
6789 .post_internal_cmd = ata_dummy_qc_noret,
6790 .irq_clear = ata_dummy_noret,
6791 .port_start = ata_dummy_ret0,
6792 .port_stop = ata_dummy_noret,
6793};
6794
Tejun Heo21b0ad42007-04-17 23:44:07 +09006795const struct ata_port_info ata_dummy_port_info = {
6796 .port_ops = &ata_dummy_port_ops,
6797};
6798
Tejun Heodd5b06c2006-08-10 16:59:12 +09006799/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006800 * libata is essentially a library of internal helper functions for
6801 * low-level ATA host controller drivers. As such, the API/ABI is
6802 * likely to change as new drivers are added and updated.
6803 * Do not depend on ABI/API stability.
6804 */
6805
Tejun Heoe9c83912006-07-03 16:07:26 +09006806EXPORT_SYMBOL_GPL(sata_deb_timing_normal);
6807EXPORT_SYMBOL_GPL(sata_deb_timing_hotplug);
6808EXPORT_SYMBOL_GPL(sata_deb_timing_long);
Tejun Heodd5b06c2006-08-10 16:59:12 +09006809EXPORT_SYMBOL_GPL(ata_dummy_port_ops);
Tejun Heo21b0ad42007-04-17 23:44:07 +09006810EXPORT_SYMBOL_GPL(ata_dummy_port_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006811EXPORT_SYMBOL_GPL(ata_std_bios_param);
6812EXPORT_SYMBOL_GPL(ata_std_ports);
Jeff Garzikcca39742006-08-24 03:19:22 -04006813EXPORT_SYMBOL_GPL(ata_host_init);
Tejun Heof3187192007-04-17 23:44:07 +09006814EXPORT_SYMBOL_GPL(ata_host_alloc);
Tejun Heof5cda252007-04-17 23:44:07 +09006815EXPORT_SYMBOL_GPL(ata_host_alloc_pinfo);
Tejun Heoecef7252007-04-17 23:44:06 +09006816EXPORT_SYMBOL_GPL(ata_host_start);
Tejun Heof3187192007-04-17 23:44:07 +09006817EXPORT_SYMBOL_GPL(ata_host_register);
Tejun Heof5cda252007-04-17 23:44:07 +09006818EXPORT_SYMBOL_GPL(ata_host_activate);
Tejun Heo0529c1592007-01-20 16:00:26 +09006819EXPORT_SYMBOL_GPL(ata_host_detach);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006820EXPORT_SYMBOL_GPL(ata_sg_init);
6821EXPORT_SYMBOL_GPL(ata_sg_init_one);
Tejun Heo9a1004d2006-05-31 18:27:52 +09006822EXPORT_SYMBOL_GPL(ata_hsm_move);
Tejun Heof686bcb2006-05-15 20:58:05 +09006823EXPORT_SYMBOL_GPL(ata_qc_complete);
Tejun Heodedaf2b2006-05-15 21:03:43 +09006824EXPORT_SYMBOL_GPL(ata_qc_complete_multiple);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006825EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006826EXPORT_SYMBOL_GPL(ata_tf_load);
6827EXPORT_SYMBOL_GPL(ata_tf_read);
6828EXPORT_SYMBOL_GPL(ata_noop_dev_select);
6829EXPORT_SYMBOL_GPL(ata_std_dev_select);
Jeff Garzik43727fb2007-02-25 16:50:52 -05006830EXPORT_SYMBOL_GPL(sata_print_link_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006831EXPORT_SYMBOL_GPL(ata_tf_to_fis);
6832EXPORT_SYMBOL_GPL(ata_tf_from_fis);
6833EXPORT_SYMBOL_GPL(ata_check_status);
6834EXPORT_SYMBOL_GPL(ata_altstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006835EXPORT_SYMBOL_GPL(ata_exec_command);
6836EXPORT_SYMBOL_GPL(ata_port_start);
Alan Coxd92e74d2007-06-07 16:19:15 +01006837EXPORT_SYMBOL_GPL(ata_sff_port_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006838EXPORT_SYMBOL_GPL(ata_interrupt);
Alan04351822007-03-06 02:37:52 -08006839EXPORT_SYMBOL_GPL(ata_do_set_mode);
Tejun Heo0d5ff562007-02-01 15:06:36 +09006840EXPORT_SYMBOL_GPL(ata_data_xfer);
6841EXPORT_SYMBOL_GPL(ata_data_xfer_noirq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006842EXPORT_SYMBOL_GPL(ata_qc_prep);
Brian Kinge46834c2006-03-17 17:04:03 -06006843EXPORT_SYMBOL_GPL(ata_noop_qc_prep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006844EXPORT_SYMBOL_GPL(ata_bmdma_setup);
6845EXPORT_SYMBOL_GPL(ata_bmdma_start);
6846EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
6847EXPORT_SYMBOL_GPL(ata_bmdma_status);
6848EXPORT_SYMBOL_GPL(ata_bmdma_stop);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09006849EXPORT_SYMBOL_GPL(ata_bmdma_freeze);
6850EXPORT_SYMBOL_GPL(ata_bmdma_thaw);
6851EXPORT_SYMBOL_GPL(ata_bmdma_drive_eh);
6852EXPORT_SYMBOL_GPL(ata_bmdma_error_handler);
6853EXPORT_SYMBOL_GPL(ata_bmdma_post_internal_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006854EXPORT_SYMBOL_GPL(ata_port_probe);
Alan10305f02007-02-20 18:01:59 +00006855EXPORT_SYMBOL_GPL(ata_dev_disable);
Tejun Heo3c567b72006-05-15 20:57:23 +09006856EXPORT_SYMBOL_GPL(sata_set_spd);
Tejun Heod7bb4cc2006-05-31 18:27:46 +09006857EXPORT_SYMBOL_GPL(sata_phy_debounce);
6858EXPORT_SYMBOL_GPL(sata_phy_resume);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006859EXPORT_SYMBOL_GPL(sata_phy_reset);
6860EXPORT_SYMBOL_GPL(__sata_phy_reset);
6861EXPORT_SYMBOL_GPL(ata_bus_reset);
Tejun Heof5914a42006-05-31 18:27:48 +09006862EXPORT_SYMBOL_GPL(ata_std_prereset);
Tejun Heoc2bd5802006-01-24 17:05:22 +09006863EXPORT_SYMBOL_GPL(ata_std_softreset);
Tejun Heob6103f62006-11-01 17:59:53 +09006864EXPORT_SYMBOL_GPL(sata_port_hardreset);
Tejun Heoc2bd5802006-01-24 17:05:22 +09006865EXPORT_SYMBOL_GPL(sata_std_hardreset);
6866EXPORT_SYMBOL_GPL(ata_std_postreset);
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05006867EXPORT_SYMBOL_GPL(ata_dev_classify);
6868EXPORT_SYMBOL_GPL(ata_dev_pair);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006869EXPORT_SYMBOL_GPL(ata_port_disable);
Jeff Garzik67846b32005-10-05 02:58:32 -04006870EXPORT_SYMBOL_GPL(ata_ratelimit);
Tejun Heoc22daff2006-04-11 22:22:29 +09006871EXPORT_SYMBOL_GPL(ata_wait_register);
Tejun Heo6f8b9952006-01-24 17:05:21 +09006872EXPORT_SYMBOL_GPL(ata_busy_sleep);
Tejun Heod4b2bab2007-02-02 16:50:52 +09006873EXPORT_SYMBOL_GPL(ata_wait_ready);
Tejun Heo86e45b62006-03-05 15:29:09 +09006874EXPORT_SYMBOL_GPL(ata_port_queue_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006875EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
6876EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006877EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
Tejun Heo83c47bc2006-05-31 18:28:07 +09006878EXPORT_SYMBOL_GPL(ata_scsi_slave_destroy);
Tejun Heoa6e6ce82006-05-15 21:03:48 +09006879EXPORT_SYMBOL_GPL(ata_scsi_change_queue_depth);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006880EXPORT_SYMBOL_GPL(ata_host_intr);
Tejun Heo34bf2172006-05-15 20:57:46 +09006881EXPORT_SYMBOL_GPL(sata_scr_valid);
6882EXPORT_SYMBOL_GPL(sata_scr_read);
6883EXPORT_SYMBOL_GPL(sata_scr_write);
6884EXPORT_SYMBOL_GPL(sata_scr_write_flush);
6885EXPORT_SYMBOL_GPL(ata_port_online);
6886EXPORT_SYMBOL_GPL(ata_port_offline);
Tejun Heo6ffa01d2007-03-02 17:32:47 +09006887#ifdef CONFIG_PM
Jeff Garzikcca39742006-08-24 03:19:22 -04006888EXPORT_SYMBOL_GPL(ata_host_suspend);
6889EXPORT_SYMBOL_GPL(ata_host_resume);
Tejun Heo6ffa01d2007-03-02 17:32:47 +09006890#endif /* CONFIG_PM */
Tejun Heo6a62a042006-02-13 10:02:46 +09006891EXPORT_SYMBOL_GPL(ata_id_string);
6892EXPORT_SYMBOL_GPL(ata_id_c_string);
Alan10305f02007-02-20 18:01:59 +00006893EXPORT_SYMBOL_GPL(ata_id_to_dma_mode);
Alan Cox6919a0a2006-10-27 19:08:46 -07006894EXPORT_SYMBOL_GPL(ata_device_blacklisted);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006895EXPORT_SYMBOL_GPL(ata_scsi_simulate);
6896
Alan Cox1bc4ccf2006-01-09 17:18:14 +00006897EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
Alan Cox452503f2005-10-21 19:01:32 -04006898EXPORT_SYMBOL_GPL(ata_timing_compute);
6899EXPORT_SYMBOL_GPL(ata_timing_merge);
6900
Linus Torvalds1da177e2005-04-16 15:20:36 -07006901#ifdef CONFIG_PCI
6902EXPORT_SYMBOL_GPL(pci_test_config_bits);
Tejun Heod491b272007-04-17 23:44:07 +09006903EXPORT_SYMBOL_GPL(ata_pci_init_native_host);
Tejun Heo1626aeb2007-05-04 12:43:58 +02006904EXPORT_SYMBOL_GPL(ata_pci_init_bmdma);
Tejun Heo21b0ad42007-04-17 23:44:07 +09006905EXPORT_SYMBOL_GPL(ata_pci_prepare_native_host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006906EXPORT_SYMBOL_GPL(ata_pci_init_one);
6907EXPORT_SYMBOL_GPL(ata_pci_remove_one);
Tejun Heo6ffa01d2007-03-02 17:32:47 +09006908#ifdef CONFIG_PM
Tejun Heo500530f2006-07-03 16:07:27 +09006909EXPORT_SYMBOL_GPL(ata_pci_device_do_suspend);
6910EXPORT_SYMBOL_GPL(ata_pci_device_do_resume);
Jens Axboe9b847542006-01-06 09:28:07 +01006911EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
6912EXPORT_SYMBOL_GPL(ata_pci_device_resume);
Tejun Heo6ffa01d2007-03-02 17:32:47 +09006913#endif /* CONFIG_PM */
Alan Cox67951ad2006-03-22 15:55:54 +00006914EXPORT_SYMBOL_GPL(ata_pci_default_filter);
6915EXPORT_SYMBOL_GPL(ata_pci_clear_simplex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006916#endif /* CONFIG_PCI */
Jens Axboe9b847542006-01-06 09:28:07 +01006917
Tejun Heoece1d632006-04-02 18:51:53 +09006918EXPORT_SYMBOL_GPL(ata_eng_timeout);
Tejun Heo7b70fc02006-05-15 20:58:07 +09006919EXPORT_SYMBOL_GPL(ata_port_schedule_eh);
6920EXPORT_SYMBOL_GPL(ata_port_abort);
Tejun Heoe3180492006-05-15 20:58:09 +09006921EXPORT_SYMBOL_GPL(ata_port_freeze);
6922EXPORT_SYMBOL_GPL(ata_eh_freeze_port);
6923EXPORT_SYMBOL_GPL(ata_eh_thaw_port);
Tejun Heoece1d632006-04-02 18:51:53 +09006924EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
6925EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
Tejun Heo022bdb02006-05-15 20:58:22 +09006926EXPORT_SYMBOL_GPL(ata_do_eh);
Akira Iguchi83625002007-01-26 16:27:32 +09006927EXPORT_SYMBOL_GPL(ata_irq_on);
6928EXPORT_SYMBOL_GPL(ata_dummy_irq_on);
6929EXPORT_SYMBOL_GPL(ata_irq_ack);
6930EXPORT_SYMBOL_GPL(ata_dummy_irq_ack);
Akira Iguchia619f981b2007-01-26 16:28:18 +09006931EXPORT_SYMBOL_GPL(ata_dev_try_classify);
Alan Coxbe0d18d2007-03-06 02:37:56 -08006932
6933EXPORT_SYMBOL_GPL(ata_cable_40wire);
6934EXPORT_SYMBOL_GPL(ata_cable_80wire);
6935EXPORT_SYMBOL_GPL(ata_cable_unknown);
6936EXPORT_SYMBOL_GPL(ata_cable_sata);