blob: c7614bdc0cb9c7583ce5707e476f186c7dcddf37 [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);
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -040073static unsigned int ata_dev_set_AN(struct ata_device *dev, u8 enable);
Tejun Heo3373efd2006-05-15 20:57:53 +090074static void ata_dev_xfermask(struct ata_device *dev);
Tejun Heo75683fe2007-07-05 13:31:27 +090075static unsigned long ata_dev_blacklisted(const struct ata_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070076
Tejun Heof3187192007-04-17 23:44:07 +090077unsigned int ata_print_id = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078static struct workqueue_struct *ata_wq;
79
Tejun Heo453b07a2006-05-31 18:27:42 +090080struct workqueue_struct *ata_aux_wq;
81
Jeff Garzik418dc1f2006-03-11 20:50:08 -050082int atapi_enabled = 1;
Jeff Garzik1623c812005-08-30 03:37:42 -040083module_param(atapi_enabled, int, 0444);
84MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)");
85
Albert Lee95de7192006-04-04 10:57:18 +080086int atapi_dmadir = 0;
87module_param(atapi_dmadir, int, 0444);
88MODULE_PARM_DESC(atapi_dmadir, "Enable ATAPI DMADIR bridge support (0=off, 1=on)");
89
Mark Lordbaf4fdf2007-08-08 01:08:45 +090090int atapi_passthru16 = 1;
91module_param(atapi_passthru16, int, 0444);
92MODULE_PARM_DESC(atapi_passthru16, "Enable ATA_16 passthru for ATAPI devices; on by default (0=off, 1=on)");
93
Jeff Garzikc3c013a2006-02-27 22:31:19 -050094int libata_fua = 0;
95module_param_named(fua, libata_fua, int, 0444);
96MODULE_PARM_DESC(fua, "FUA support (0=off, 1=on)");
97
Alan Cox1e999732007-04-11 00:23:13 +010098static int ata_ignore_hpa = 0;
99module_param_named(ignore_hpa, ata_ignore_hpa, int, 0644);
100MODULE_PARM_DESC(ignore_hpa, "Ignore HPA limit (0=keep BIOS limits, 1=ignore limits, using full disk)");
101
Andrew Mortona8601e52006-06-25 01:36:52 -0700102static int ata_probe_timeout = ATA_TMOUT_INTERNAL / HZ;
103module_param(ata_probe_timeout, int, 0444);
104MODULE_PARM_DESC(ata_probe_timeout, "Set ATA probing timeout (seconds)");
105
Jeff Garzikd7d0dad2007-03-28 01:57:37 -0400106int libata_noacpi = 1;
107module_param_named(noacpi, libata_noacpi, int, 0444);
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700108MODULE_PARM_DESC(noacpi, "Disables the use of ACPI in suspend/resume when set");
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110MODULE_AUTHOR("Jeff Garzik");
111MODULE_DESCRIPTION("Library module for ATA devices");
112MODULE_LICENSE("GPL");
113MODULE_VERSION(DRV_VERSION);
114
Edward Falk0baab862005-06-02 18:17:13 -0400115
116/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
118 * @tf: Taskfile to convert
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 * @pmp: Port multiplier port
Tejun Heo99771262007-07-16 14:29:38 +0900120 * @is_cmd: This FIS is for command
121 * @fis: Buffer into which data will output
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 *
123 * Converts a standard ATA taskfile to a Serial ATA
124 * FIS structure (Register - Host to Device).
125 *
126 * LOCKING:
127 * Inherited from caller.
128 */
Tejun Heo99771262007-07-16 14:29:38 +0900129void ata_tf_to_fis(const struct ata_taskfile *tf, u8 pmp, int is_cmd, u8 *fis)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
Tejun Heo99771262007-07-16 14:29:38 +0900131 fis[0] = 0x27; /* Register - Host to Device FIS */
132 fis[1] = pmp & 0xf; /* Port multiplier number*/
133 if (is_cmd)
134 fis[1] |= (1 << 7); /* bit 7 indicates Command FIS */
135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 fis[2] = tf->command;
137 fis[3] = tf->feature;
138
139 fis[4] = tf->lbal;
140 fis[5] = tf->lbam;
141 fis[6] = tf->lbah;
142 fis[7] = tf->device;
143
144 fis[8] = tf->hob_lbal;
145 fis[9] = tf->hob_lbam;
146 fis[10] = tf->hob_lbah;
147 fis[11] = tf->hob_feature;
148
149 fis[12] = tf->nsect;
150 fis[13] = tf->hob_nsect;
151 fis[14] = 0;
152 fis[15] = tf->ctl;
153
154 fis[16] = 0;
155 fis[17] = 0;
156 fis[18] = 0;
157 fis[19] = 0;
158}
159
160/**
161 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
162 * @fis: Buffer from which data will be input
163 * @tf: Taskfile to output
164 *
Mark Lorde12a1be2005-11-12 18:55:45 -0500165 * Converts a serial ATA FIS structure to a standard ATA taskfile.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 *
167 * LOCKING:
168 * Inherited from caller.
169 */
170
Jeff Garzik057ace52005-10-22 14:27:05 -0400171void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
173 tf->command = fis[2]; /* status */
174 tf->feature = fis[3]; /* error */
175
176 tf->lbal = fis[4];
177 tf->lbam = fis[5];
178 tf->lbah = fis[6];
179 tf->device = fis[7];
180
181 tf->hob_lbal = fis[8];
182 tf->hob_lbam = fis[9];
183 tf->hob_lbah = fis[10];
184
185 tf->nsect = fis[12];
186 tf->hob_nsect = fis[13];
187}
188
Albert Lee8cbd6df2005-10-12 15:06:27 +0800189static const u8 ata_rw_cmds[] = {
190 /* pio multi */
191 ATA_CMD_READ_MULTI,
192 ATA_CMD_WRITE_MULTI,
193 ATA_CMD_READ_MULTI_EXT,
194 ATA_CMD_WRITE_MULTI_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100195 0,
196 0,
197 0,
198 ATA_CMD_WRITE_MULTI_FUA_EXT,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800199 /* pio */
200 ATA_CMD_PIO_READ,
201 ATA_CMD_PIO_WRITE,
202 ATA_CMD_PIO_READ_EXT,
203 ATA_CMD_PIO_WRITE_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100204 0,
205 0,
206 0,
207 0,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800208 /* dma */
209 ATA_CMD_READ,
210 ATA_CMD_WRITE,
211 ATA_CMD_READ_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100212 ATA_CMD_WRITE_EXT,
213 0,
214 0,
215 0,
216 ATA_CMD_WRITE_FUA_EXT
Albert Lee8cbd6df2005-10-12 15:06:27 +0800217};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
219/**
Albert Lee8cbd6df2005-10-12 15:06:27 +0800220 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
Tejun Heobd056d72006-11-14 22:47:10 +0900221 * @tf: command to examine and configure
222 * @dev: device tf belongs to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 *
Jeff Garzik2e9edbf2006-03-24 09:56:57 -0500224 * Examine the device configuration and tf->flags to calculate
Albert Lee8cbd6df2005-10-12 15:06:27 +0800225 * the proper read/write commands and protocol to use.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 *
227 * LOCKING:
228 * caller.
229 */
Tejun Heobd056d72006-11-14 22:47:10 +0900230static int ata_rwcmd_protocol(struct ata_taskfile *tf, struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231{
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100232 u8 cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100234 int index, fua, lba48, write;
Jeff Garzik2e9edbf2006-03-24 09:56:57 -0500235
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100236 fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800237 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
238 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
Albert Lee8cbd6df2005-10-12 15:06:27 +0800240 if (dev->flags & ATA_DFLAG_PIO) {
241 tf->protocol = ATA_PROT_PIO;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100242 index = dev->multi_count ? 0 : 8;
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900243 } else if (lba48 && (dev->link->ap->flags & ATA_FLAG_PIO_LBA48)) {
Alan Cox8d238e02006-01-17 20:50:31 +0000244 /* Unable to use DMA due to host limitation */
245 tf->protocol = ATA_PROT_PIO;
Albert Lee0565c262006-02-13 18:55:25 +0800246 index = dev->multi_count ? 0 : 8;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800247 } else {
248 tf->protocol = ATA_PROT_DMA;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100249 index = 16;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800250 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100252 cmd = ata_rw_cmds[index + fua + lba48 + write];
253 if (cmd) {
254 tf->command = cmd;
255 return 0;
256 }
257 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258}
259
Tejun Heocb95d562006-03-06 04:31:56 +0900260/**
Tejun Heo35b649f2006-11-14 22:37:35 +0900261 * ata_tf_read_block - Read block address from ATA taskfile
262 * @tf: ATA taskfile of interest
263 * @dev: ATA device @tf belongs to
264 *
265 * LOCKING:
266 * None.
267 *
268 * Read block address from @tf. This function can handle all
269 * three address formats - LBA, LBA48 and CHS. tf->protocol and
270 * flags select the address format to use.
271 *
272 * RETURNS:
273 * Block address read from @tf.
274 */
275u64 ata_tf_read_block(struct ata_taskfile *tf, struct ata_device *dev)
276{
277 u64 block = 0;
278
279 if (tf->flags & ATA_TFLAG_LBA) {
280 if (tf->flags & ATA_TFLAG_LBA48) {
281 block |= (u64)tf->hob_lbah << 40;
282 block |= (u64)tf->hob_lbam << 32;
283 block |= tf->hob_lbal << 24;
284 } else
285 block |= (tf->device & 0xf) << 24;
286
287 block |= tf->lbah << 16;
288 block |= tf->lbam << 8;
289 block |= tf->lbal;
290 } else {
291 u32 cyl, head, sect;
292
293 cyl = tf->lbam | (tf->lbah << 8);
294 head = tf->device & 0xf;
295 sect = tf->lbal;
296
297 block = (cyl * dev->heads + head) * dev->sectors + sect;
298 }
299
300 return block;
301}
302
303/**
Tejun Heobd056d72006-11-14 22:47:10 +0900304 * ata_build_rw_tf - Build ATA taskfile for given read/write request
305 * @tf: Target ATA taskfile
306 * @dev: ATA device @tf belongs to
307 * @block: Block address
308 * @n_block: Number of blocks
309 * @tf_flags: RW/FUA etc...
310 * @tag: tag
311 *
312 * LOCKING:
313 * None.
314 *
315 * Build ATA taskfile @tf for read/write request described by
316 * @block, @n_block, @tf_flags and @tag on @dev.
317 *
318 * RETURNS:
319 *
320 * 0 on success, -ERANGE if the request is too large for @dev,
321 * -EINVAL if the request is invalid.
322 */
323int ata_build_rw_tf(struct ata_taskfile *tf, struct ata_device *dev,
324 u64 block, u32 n_block, unsigned int tf_flags,
325 unsigned int tag)
326{
327 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
328 tf->flags |= tf_flags;
329
Tejun Heo6d1245b2007-02-20 23:20:27 +0900330 if (ata_ncq_enabled(dev) && likely(tag != ATA_TAG_INTERNAL)) {
Tejun Heobd056d72006-11-14 22:47:10 +0900331 /* yay, NCQ */
332 if (!lba_48_ok(block, n_block))
333 return -ERANGE;
334
335 tf->protocol = ATA_PROT_NCQ;
336 tf->flags |= ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
337
338 if (tf->flags & ATA_TFLAG_WRITE)
339 tf->command = ATA_CMD_FPDMA_WRITE;
340 else
341 tf->command = ATA_CMD_FPDMA_READ;
342
343 tf->nsect = tag << 3;
344 tf->hob_feature = (n_block >> 8) & 0xff;
345 tf->feature = n_block & 0xff;
346
347 tf->hob_lbah = (block >> 40) & 0xff;
348 tf->hob_lbam = (block >> 32) & 0xff;
349 tf->hob_lbal = (block >> 24) & 0xff;
350 tf->lbah = (block >> 16) & 0xff;
351 tf->lbam = (block >> 8) & 0xff;
352 tf->lbal = block & 0xff;
353
354 tf->device = 1 << 6;
355 if (tf->flags & ATA_TFLAG_FUA)
356 tf->device |= 1 << 7;
357 } else if (dev->flags & ATA_DFLAG_LBA) {
358 tf->flags |= ATA_TFLAG_LBA;
359
360 if (lba_28_ok(block, n_block)) {
361 /* use LBA28 */
362 tf->device |= (block >> 24) & 0xf;
363 } else if (lba_48_ok(block, n_block)) {
364 if (!(dev->flags & ATA_DFLAG_LBA48))
365 return -ERANGE;
366
367 /* use LBA48 */
368 tf->flags |= ATA_TFLAG_LBA48;
369
370 tf->hob_nsect = (n_block >> 8) & 0xff;
371
372 tf->hob_lbah = (block >> 40) & 0xff;
373 tf->hob_lbam = (block >> 32) & 0xff;
374 tf->hob_lbal = (block >> 24) & 0xff;
375 } else
376 /* request too large even for LBA48 */
377 return -ERANGE;
378
379 if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
380 return -EINVAL;
381
382 tf->nsect = n_block & 0xff;
383
384 tf->lbah = (block >> 16) & 0xff;
385 tf->lbam = (block >> 8) & 0xff;
386 tf->lbal = block & 0xff;
387
388 tf->device |= ATA_LBA;
389 } else {
390 /* CHS */
391 u32 sect, head, cyl, track;
392
393 /* The request -may- be too large for CHS addressing. */
394 if (!lba_28_ok(block, n_block))
395 return -ERANGE;
396
397 if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
398 return -EINVAL;
399
400 /* Convert LBA to CHS */
401 track = (u32)block / dev->sectors;
402 cyl = track / dev->heads;
403 head = track % dev->heads;
404 sect = (u32)block % dev->sectors + 1;
405
406 DPRINTK("block %u track %u cyl %u head %u sect %u\n",
407 (u32)block, track, cyl, head, sect);
408
409 /* Check whether the converted CHS can fit.
410 Cylinder: 0-65535
411 Head: 0-15
412 Sector: 1-255*/
413 if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
414 return -ERANGE;
415
416 tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
417 tf->lbal = sect;
418 tf->lbam = cyl;
419 tf->lbah = cyl >> 8;
420 tf->device |= head;
421 }
422
423 return 0;
424}
425
426/**
Tejun Heocb95d562006-03-06 04:31:56 +0900427 * ata_pack_xfermask - Pack pio, mwdma and udma masks into xfer_mask
428 * @pio_mask: pio_mask
429 * @mwdma_mask: mwdma_mask
430 * @udma_mask: udma_mask
431 *
432 * Pack @pio_mask, @mwdma_mask and @udma_mask into a single
433 * unsigned int xfer_mask.
434 *
435 * LOCKING:
436 * None.
437 *
438 * RETURNS:
439 * Packed xfer_mask.
440 */
441static unsigned int ata_pack_xfermask(unsigned int pio_mask,
442 unsigned int mwdma_mask,
443 unsigned int udma_mask)
444{
445 return ((pio_mask << ATA_SHIFT_PIO) & ATA_MASK_PIO) |
446 ((mwdma_mask << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA) |
447 ((udma_mask << ATA_SHIFT_UDMA) & ATA_MASK_UDMA);
448}
449
Tejun Heoc0489e42006-03-24 14:07:49 +0900450/**
451 * ata_unpack_xfermask - Unpack xfer_mask into pio, mwdma and udma masks
452 * @xfer_mask: xfer_mask to unpack
453 * @pio_mask: resulting pio_mask
454 * @mwdma_mask: resulting mwdma_mask
455 * @udma_mask: resulting udma_mask
456 *
457 * Unpack @xfer_mask into @pio_mask, @mwdma_mask and @udma_mask.
458 * Any NULL distination masks will be ignored.
459 */
460static void ata_unpack_xfermask(unsigned int xfer_mask,
461 unsigned int *pio_mask,
462 unsigned int *mwdma_mask,
463 unsigned int *udma_mask)
464{
465 if (pio_mask)
466 *pio_mask = (xfer_mask & ATA_MASK_PIO) >> ATA_SHIFT_PIO;
467 if (mwdma_mask)
468 *mwdma_mask = (xfer_mask & ATA_MASK_MWDMA) >> ATA_SHIFT_MWDMA;
469 if (udma_mask)
470 *udma_mask = (xfer_mask & ATA_MASK_UDMA) >> ATA_SHIFT_UDMA;
471}
472
Tejun Heocb95d562006-03-06 04:31:56 +0900473static const struct ata_xfer_ent {
Tejun Heobe9a50c2006-03-31 22:48:52 +0900474 int shift, bits;
Tejun Heocb95d562006-03-06 04:31:56 +0900475 u8 base;
476} ata_xfer_tbl[] = {
477 { ATA_SHIFT_PIO, ATA_BITS_PIO, XFER_PIO_0 },
478 { ATA_SHIFT_MWDMA, ATA_BITS_MWDMA, XFER_MW_DMA_0 },
479 { ATA_SHIFT_UDMA, ATA_BITS_UDMA, XFER_UDMA_0 },
480 { -1, },
481};
482
483/**
484 * ata_xfer_mask2mode - Find matching XFER_* for the given xfer_mask
485 * @xfer_mask: xfer_mask of interest
486 *
487 * Return matching XFER_* value for @xfer_mask. Only the highest
488 * bit of @xfer_mask is considered.
489 *
490 * LOCKING:
491 * None.
492 *
493 * RETURNS:
494 * Matching XFER_* value, 0 if no match found.
495 */
496static u8 ata_xfer_mask2mode(unsigned int xfer_mask)
497{
498 int highbit = fls(xfer_mask) - 1;
499 const struct ata_xfer_ent *ent;
500
501 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
502 if (highbit >= ent->shift && highbit < ent->shift + ent->bits)
503 return ent->base + highbit - ent->shift;
504 return 0;
505}
506
507/**
508 * ata_xfer_mode2mask - Find matching xfer_mask for XFER_*
509 * @xfer_mode: XFER_* of interest
510 *
511 * Return matching xfer_mask for @xfer_mode.
512 *
513 * LOCKING:
514 * None.
515 *
516 * RETURNS:
517 * Matching xfer_mask, 0 if no match found.
518 */
519static unsigned int ata_xfer_mode2mask(u8 xfer_mode)
520{
521 const struct ata_xfer_ent *ent;
522
523 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
524 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
525 return 1 << (ent->shift + xfer_mode - ent->base);
526 return 0;
527}
528
529/**
530 * ata_xfer_mode2shift - Find matching xfer_shift for XFER_*
531 * @xfer_mode: XFER_* of interest
532 *
533 * Return matching xfer_shift for @xfer_mode.
534 *
535 * LOCKING:
536 * None.
537 *
538 * RETURNS:
539 * Matching xfer_shift, -1 if no match found.
540 */
541static int ata_xfer_mode2shift(unsigned int xfer_mode)
542{
543 const struct ata_xfer_ent *ent;
544
545 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
546 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
547 return ent->shift;
548 return -1;
549}
550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551/**
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900552 * ata_mode_string - convert xfer_mask to string
553 * @xfer_mask: mask of bits supported; only highest bit counts.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 *
555 * Determine string which represents the highest speed
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900556 * (highest bit in @modemask).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 *
558 * LOCKING:
559 * None.
560 *
561 * RETURNS:
562 * Constant C string representing highest speed listed in
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900563 * @mode_mask, or the constant C string "<n/a>".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 */
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900565static const char *ata_mode_string(unsigned int xfer_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
Tejun Heo75f554b2006-03-06 04:31:57 +0900567 static const char * const xfer_mode_str[] = {
568 "PIO0",
569 "PIO1",
570 "PIO2",
571 "PIO3",
572 "PIO4",
Alan Coxb352e572006-08-10 18:52:12 +0100573 "PIO5",
574 "PIO6",
Tejun Heo75f554b2006-03-06 04:31:57 +0900575 "MWDMA0",
576 "MWDMA1",
577 "MWDMA2",
Alan Coxb352e572006-08-10 18:52:12 +0100578 "MWDMA3",
579 "MWDMA4",
Tejun Heo75f554b2006-03-06 04:31:57 +0900580 "UDMA/16",
581 "UDMA/25",
582 "UDMA/33",
583 "UDMA/44",
584 "UDMA/66",
585 "UDMA/100",
586 "UDMA/133",
587 "UDMA7",
588 };
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900589 int highbit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900591 highbit = fls(xfer_mask) - 1;
592 if (highbit >= 0 && highbit < ARRAY_SIZE(xfer_mode_str))
593 return xfer_mode_str[highbit];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 return "<n/a>";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595}
596
Tejun Heo4c360c82006-04-01 01:38:17 +0900597static const char *sata_spd_string(unsigned int spd)
598{
599 static const char * const spd_str[] = {
600 "1.5 Gbps",
601 "3.0 Gbps",
602 };
603
604 if (spd == 0 || (spd - 1) >= ARRAY_SIZE(spd_str))
605 return "<unknown>";
606 return spd_str[spd - 1];
607}
608
Tejun Heo3373efd2006-05-15 20:57:53 +0900609void ata_dev_disable(struct ata_device *dev)
Tejun Heo0b8efb02006-03-24 15:25:31 +0900610{
Tejun Heo09d7f9b2007-06-25 23:34:02 +0900611 if (ata_dev_enabled(dev)) {
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900612 if (ata_msg_drv(dev->link->ap))
Tejun Heo09d7f9b2007-06-25 23:34:02 +0900613 ata_dev_printk(dev, KERN_WARNING, "disabled\n");
Tejun Heo4ae72a12007-02-02 16:22:30 +0900614 ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO0 |
615 ATA_DNXFER_QUIET);
Tejun Heo0b8efb02006-03-24 15:25:31 +0900616 dev->class++;
617 }
618}
619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 * ata_devchk - PATA device presence detection
622 * @ap: ATA channel to examine
623 * @device: Device to examine (starting at zero)
624 *
Tejun Heo0d5ff562007-02-01 15:06:36 +0900625 * This technique was originally described in
626 * Hale Landis's ATADRVR (www.ata-atapi.com), and
627 * later found its way into the ATA/ATAPI spec.
628 *
629 * Write a pattern to the ATA shadow registers,
630 * and if a device is present, it will respond by
631 * correctly storing and echoing back the
632 * ATA shadow register contents.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 *
634 * LOCKING:
635 * caller.
636 */
637
Tejun Heo0d5ff562007-02-01 15:06:36 +0900638static unsigned int ata_devchk(struct ata_port *ap, unsigned int device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639{
Tejun Heo0d5ff562007-02-01 15:06:36 +0900640 struct ata_ioports *ioaddr = &ap->ioaddr;
641 u8 nsect, lbal;
642
643 ap->ops->dev_select(ap, device);
644
645 iowrite8(0x55, ioaddr->nsect_addr);
646 iowrite8(0xaa, ioaddr->lbal_addr);
647
648 iowrite8(0xaa, ioaddr->nsect_addr);
649 iowrite8(0x55, ioaddr->lbal_addr);
650
651 iowrite8(0x55, ioaddr->nsect_addr);
652 iowrite8(0xaa, ioaddr->lbal_addr);
653
654 nsect = ioread8(ioaddr->nsect_addr);
655 lbal = ioread8(ioaddr->lbal_addr);
656
657 if ((nsect == 0x55) && (lbal == 0xaa))
658 return 1; /* we found a device */
659
660 return 0; /* nothing found */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661}
662
663/**
664 * ata_dev_classify - determine device type based on ATA-spec signature
665 * @tf: ATA taskfile register set for device to be identified
666 *
667 * Determine from taskfile register contents whether a device is
668 * ATA or ATAPI, as per "Signature and persistence" section
669 * of ATA/PI spec (volume 1, sect 5.14).
670 *
671 * LOCKING:
672 * None.
673 *
674 * RETURNS:
675 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
676 * the event of failure.
677 */
678
Jeff Garzik057ace52005-10-22 14:27:05 -0400679unsigned int ata_dev_classify(const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680{
681 /* Apple's open source Darwin code hints that some devices only
682 * put a proper signature into the LBA mid/high registers,
683 * So, we only check those. It's sufficient for uniqueness.
684 */
685
686 if (((tf->lbam == 0) && (tf->lbah == 0)) ||
687 ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
688 DPRINTK("found ATA device by sig\n");
689 return ATA_DEV_ATA;
690 }
691
692 if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
693 ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
694 DPRINTK("found ATAPI device by sig\n");
695 return ATA_DEV_ATAPI;
696 }
697
698 DPRINTK("unknown device\n");
699 return ATA_DEV_UNKNOWN;
700}
701
702/**
703 * ata_dev_try_classify - Parse returned ATA device signature
704 * @ap: ATA channel to examine
705 * @device: Device to examine (starting at zero)
Tejun Heob4dc7622006-01-24 17:05:22 +0900706 * @r_err: Value of error register on completion
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 *
708 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
709 * an ATA/ATAPI-defined set of values is placed in the ATA
710 * shadow registers, indicating the results of device detection
711 * and diagnostics.
712 *
713 * Select the ATA device, and read the values from the ATA shadow
714 * registers. Then parse according to the Error register value,
715 * and the spec-defined values examined by ata_dev_classify().
716 *
717 * LOCKING:
718 * caller.
Tejun Heob4dc7622006-01-24 17:05:22 +0900719 *
720 * RETURNS:
721 * Device type - %ATA_DEV_ATA, %ATA_DEV_ATAPI or %ATA_DEV_NONE.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 */
723
Akira Iguchia619f981b2007-01-26 16:28:18 +0900724unsigned int
Tejun Heob4dc7622006-01-24 17:05:22 +0900725ata_dev_try_classify(struct ata_port *ap, unsigned int device, u8 *r_err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 struct ata_taskfile tf;
728 unsigned int class;
729 u8 err;
730
731 ap->ops->dev_select(ap, device);
732
733 memset(&tf, 0, sizeof(tf));
734
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 ap->ops->tf_read(ap, &tf);
Jeff Garzik0169e282005-10-29 21:25:10 -0400736 err = tf.feature;
Tejun Heob4dc7622006-01-24 17:05:22 +0900737 if (r_err)
738 *r_err = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
Alan Cox93590852006-09-12 16:55:12 +0100740 /* see if device passed diags: if master then continue and warn later */
741 if (err == 0 && device == 0)
742 /* diagnostic fail : do nothing _YET_ */
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900743 ap->link.device[device].horkage |= ATA_HORKAGE_DIAGNOSTIC;
Alan Cox93590852006-09-12 16:55:12 +0100744 else if (err == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 /* do nothing */ ;
746 else if ((device == 0) && (err == 0x81))
747 /* do nothing */ ;
748 else
Tejun Heob4dc7622006-01-24 17:05:22 +0900749 return ATA_DEV_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Tejun Heob4dc7622006-01-24 17:05:22 +0900751 /* determine if device is ATA or ATAPI */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 class = ata_dev_classify(&tf);
Tejun Heob4dc7622006-01-24 17:05:22 +0900753
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 if (class == ATA_DEV_UNKNOWN)
Tejun Heob4dc7622006-01-24 17:05:22 +0900755 return ATA_DEV_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
Tejun Heob4dc7622006-01-24 17:05:22 +0900757 return ATA_DEV_NONE;
758 return class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759}
760
761/**
Tejun Heo6a62a042006-02-13 10:02:46 +0900762 * ata_id_string - Convert IDENTIFY DEVICE page into string
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 * @id: IDENTIFY DEVICE results we will examine
764 * @s: string into which data is output
765 * @ofs: offset into identify device page
766 * @len: length of string to return. must be an even number.
767 *
768 * The strings in the IDENTIFY DEVICE page are broken up into
769 * 16-bit chunks. Run through the string, and output each
770 * 8-bit chunk linearly, regardless of platform.
771 *
772 * LOCKING:
773 * caller.
774 */
775
Tejun Heo6a62a042006-02-13 10:02:46 +0900776void ata_id_string(const u16 *id, unsigned char *s,
777 unsigned int ofs, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
779 unsigned int c;
780
781 while (len > 0) {
782 c = id[ofs] >> 8;
783 *s = c;
784 s++;
785
786 c = id[ofs] & 0xff;
787 *s = c;
788 s++;
789
790 ofs++;
791 len -= 2;
792 }
793}
794
Tejun Heo0e949ff2006-02-12 22:47:04 +0900795/**
Tejun Heo6a62a042006-02-13 10:02:46 +0900796 * ata_id_c_string - Convert IDENTIFY DEVICE page into C string
Tejun Heo0e949ff2006-02-12 22:47:04 +0900797 * @id: IDENTIFY DEVICE results we will examine
798 * @s: string into which data is output
799 * @ofs: offset into identify device page
800 * @len: length of string to return. must be an odd number.
801 *
Tejun Heo6a62a042006-02-13 10:02:46 +0900802 * This function is identical to ata_id_string except that it
Tejun Heo0e949ff2006-02-12 22:47:04 +0900803 * trims trailing spaces and terminates the resulting string with
804 * null. @len must be actual maximum length (even number) + 1.
805 *
806 * LOCKING:
807 * caller.
808 */
Tejun Heo6a62a042006-02-13 10:02:46 +0900809void ata_id_c_string(const u16 *id, unsigned char *s,
810 unsigned int ofs, unsigned int len)
Tejun Heo0e949ff2006-02-12 22:47:04 +0900811{
812 unsigned char *p;
813
814 WARN_ON(!(len & 1));
815
Tejun Heo6a62a042006-02-13 10:02:46 +0900816 ata_id_string(id, s, ofs, len - 1);
Tejun Heo0e949ff2006-02-12 22:47:04 +0900817
818 p = s + strnlen(s, len - 1);
819 while (p > s && p[-1] == ' ')
820 p--;
821 *p = '\0';
822}
Edward Falk0baab862005-06-02 18:17:13 -0400823
Tejun Heodb6f8752007-09-03 12:31:58 +0900824static u64 ata_id_n_sectors(const u16 *id)
825{
826 if (ata_id_has_lba(id)) {
827 if (ata_id_has_lba48(id))
828 return ata_id_u64(id, 100);
829 else
830 return ata_id_u32(id, 60);
831 } else {
832 if (ata_id_current_chs_valid(id))
833 return ata_id_u32(id, 57);
834 else
835 return id[1] * id[3] * id[6];
836 }
837}
838
Alan Cox1e999732007-04-11 00:23:13 +0100839static u64 ata_tf_to_lba48(struct ata_taskfile *tf)
840{
841 u64 sectors = 0;
842
843 sectors |= ((u64)(tf->hob_lbah & 0xff)) << 40;
844 sectors |= ((u64)(tf->hob_lbam & 0xff)) << 32;
845 sectors |= (tf->hob_lbal & 0xff) << 24;
846 sectors |= (tf->lbah & 0xff) << 16;
847 sectors |= (tf->lbam & 0xff) << 8;
848 sectors |= (tf->lbal & 0xff);
849
850 return ++sectors;
851}
852
853static u64 ata_tf_to_lba(struct ata_taskfile *tf)
854{
855 u64 sectors = 0;
856
857 sectors |= (tf->device & 0x0f) << 24;
858 sectors |= (tf->lbah & 0xff) << 16;
859 sectors |= (tf->lbam & 0xff) << 8;
860 sectors |= (tf->lbal & 0xff);
861
862 return ++sectors;
863}
864
865/**
Tejun Heoc728a912007-09-03 12:32:30 +0900866 * ata_read_native_max_address - Read native max address
867 * @dev: target device
868 * @max_sectors: out parameter for the result native max address
Alan Cox1e999732007-04-11 00:23:13 +0100869 *
Tejun Heoc728a912007-09-03 12:32:30 +0900870 * Perform an LBA48 or LBA28 native size query upon the device in
871 * question.
872 *
873 * RETURNS:
874 * 0 on success, -EACCES if command is aborted by the drive.
875 * -EIO on other errors.
Alan Cox1e999732007-04-11 00:23:13 +0100876 */
Tejun Heoc728a912007-09-03 12:32:30 +0900877static int ata_read_native_max_address(struct ata_device *dev, u64 *max_sectors)
Alan Cox1e999732007-04-11 00:23:13 +0100878{
Tejun Heoc728a912007-09-03 12:32:30 +0900879 unsigned int err_mask;
Alan Cox1e999732007-04-11 00:23:13 +0100880 struct ata_taskfile tf;
Tejun Heoc728a912007-09-03 12:32:30 +0900881 int lba48 = ata_id_has_lba48(dev->id);
Alan Cox1e999732007-04-11 00:23:13 +0100882
883 ata_tf_init(dev, &tf);
884
Tejun Heoc728a912007-09-03 12:32:30 +0900885 /* always clear all address registers */
Alan Cox1e999732007-04-11 00:23:13 +0100886 tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
Tejun Heoc728a912007-09-03 12:32:30 +0900887
888 if (lba48) {
889 tf.command = ATA_CMD_READ_NATIVE_MAX_EXT;
890 tf.flags |= ATA_TFLAG_LBA48;
891 } else
892 tf.command = ATA_CMD_READ_NATIVE_MAX;
893
Alan Cox1e999732007-04-11 00:23:13 +0100894 tf.protocol |= ATA_PROT_NODATA;
Tejun Heoc728a912007-09-03 12:32:30 +0900895 tf.device |= ATA_LBA;
Alan Cox1e999732007-04-11 00:23:13 +0100896
Tejun Heoc728a912007-09-03 12:32:30 +0900897 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
898 if (err_mask) {
899 ata_dev_printk(dev, KERN_WARNING, "failed to read native "
900 "max address (err_mask=0x%x)\n", err_mask);
901 if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
902 return -EACCES;
903 return -EIO;
904 }
Alan Cox1e999732007-04-11 00:23:13 +0100905
Tejun Heoc728a912007-09-03 12:32:30 +0900906 if (lba48)
907 *max_sectors = ata_tf_to_lba48(&tf);
908 else
909 *max_sectors = ata_tf_to_lba(&tf);
910
911 return 0;
Alan Cox1e999732007-04-11 00:23:13 +0100912}
913
914/**
Tejun Heoc728a912007-09-03 12:32:30 +0900915 * ata_set_max_sectors - Set max sectors
916 * @dev: target device
Randy Dunlap6b38d1d2007-05-01 17:35:55 -0700917 * @new_sectors: new max sectors value to set for the device
Tejun Heoc728a912007-09-03 12:32:30 +0900918 * @res_sectors: result max sectors
Alan Cox1e999732007-04-11 00:23:13 +0100919 *
Tejun Heoc728a912007-09-03 12:32:30 +0900920 * Set max sectors of @dev to @new_sectors.
921 *
922 * RETURNS:
923 * 0 on success, -EACCES if command is aborted or denied (due to
924 * previous non-volatile SET_MAX) by the drive. -EIO on other
925 * errors.
Alan Cox1e999732007-04-11 00:23:13 +0100926 */
Tejun Heoc728a912007-09-03 12:32:30 +0900927static int ata_set_max_sectors(struct ata_device *dev, u64 new_sectors,
928 u64 *res_sectors)
Alan Cox1e999732007-04-11 00:23:13 +0100929{
Tejun Heoc728a912007-09-03 12:32:30 +0900930 unsigned int err_mask;
Alan Cox1e999732007-04-11 00:23:13 +0100931 struct ata_taskfile tf;
Tejun Heoc728a912007-09-03 12:32:30 +0900932 int lba48 = ata_id_has_lba48(dev->id);
Alan Cox1e999732007-04-11 00:23:13 +0100933
934 new_sectors--;
935
936 ata_tf_init(dev, &tf);
937
Alan Cox1e999732007-04-11 00:23:13 +0100938 tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
Tejun Heoc728a912007-09-03 12:32:30 +0900939
940 if (lba48) {
941 tf.command = ATA_CMD_SET_MAX_EXT;
942 tf.flags |= ATA_TFLAG_LBA48;
943
944 tf.hob_lbal = (new_sectors >> 24) & 0xff;
945 tf.hob_lbam = (new_sectors >> 32) & 0xff;
946 tf.hob_lbah = (new_sectors >> 40) & 0xff;
947 } else
948 tf.command = ATA_CMD_SET_MAX;
949
Alan Cox1e999732007-04-11 00:23:13 +0100950 tf.protocol |= ATA_PROT_NODATA;
Tejun Heoc728a912007-09-03 12:32:30 +0900951 tf.device |= ATA_LBA;
Alan Cox1e999732007-04-11 00:23:13 +0100952
953 tf.lbal = (new_sectors >> 0) & 0xff;
954 tf.lbam = (new_sectors >> 8) & 0xff;
955 tf.lbah = (new_sectors >> 16) & 0xff;
Alan Cox1e999732007-04-11 00:23:13 +0100956
Tejun Heoc728a912007-09-03 12:32:30 +0900957 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
958 if (err_mask) {
959 ata_dev_printk(dev, KERN_WARNING, "failed to set "
960 "max address (err_mask=0x%x)\n", err_mask);
961 if (err_mask == AC_ERR_DEV &&
962 (tf.feature & (ATA_ABORTED | ATA_IDNF)))
963 return -EACCES;
964 return -EIO;
965 }
Alan Cox1e999732007-04-11 00:23:13 +0100966
Tejun Heoc728a912007-09-03 12:32:30 +0900967 if (lba48)
968 *res_sectors = ata_tf_to_lba48(&tf);
969 else
970 *res_sectors = ata_tf_to_lba(&tf);
971
972 return 0;
Alan Cox1e999732007-04-11 00:23:13 +0100973}
974
975/**
976 * ata_hpa_resize - Resize a device with an HPA set
977 * @dev: Device to resize
978 *
979 * Read the size of an LBA28 or LBA48 disk with HPA features and resize
980 * it if required to the full size of the media. The caller must check
981 * the drive has the HPA feature set enabled.
982 */
983
984static u64 ata_hpa_resize(struct ata_device *dev)
985{
986 u64 sectors = dev->n_sectors;
987 u64 hpa_sectors;
Tejun Heoc728a912007-09-03 12:32:30 +0900988 int rc;
Jeff Garzika617c092007-05-21 20:14:23 -0400989
Tejun Heoc728a912007-09-03 12:32:30 +0900990 rc = ata_read_native_max_address(dev, &hpa_sectors);
991 if (rc)
992 return 0;
Alan Cox1e999732007-04-11 00:23:13 +0100993
Alan Cox1e999732007-04-11 00:23:13 +0100994 if (hpa_sectors > sectors) {
995 ata_dev_printk(dev, KERN_INFO,
996 "Host Protected Area detected:\n"
997 "\tcurrent size: %lld sectors\n"
998 "\tnative size: %lld sectors\n",
Andrew Mortonbd1d5ec2007-04-26 00:19:21 -0700999 (long long)sectors, (long long)hpa_sectors);
Alan Cox1e999732007-04-11 00:23:13 +01001000
1001 if (ata_ignore_hpa) {
Tejun Heoc728a912007-09-03 12:32:30 +09001002 rc = ata_set_max_sectors(dev, hpa_sectors, &hpa_sectors);
Alan Cox1e999732007-04-11 00:23:13 +01001003
Tejun Heoc728a912007-09-03 12:32:30 +09001004 if (rc == 0) {
Andrew Mortonbd1d5ec2007-04-26 00:19:21 -07001005 ata_dev_printk(dev, KERN_INFO, "native size "
1006 "increased to %lld sectors\n",
1007 (long long)hpa_sectors);
Alan Cox1e999732007-04-11 00:23:13 +01001008 return hpa_sectors;
1009 }
1010 }
Tejun Heo37301a52007-06-25 20:45:54 +09001011 } else if (hpa_sectors < sectors)
1012 ata_dev_printk(dev, KERN_WARNING, "%s 1: hpa sectors (%lld) "
1013 "is smaller than sectors (%lld)\n", __FUNCTION__,
1014 (long long)hpa_sectors, (long long)sectors);
1015
Alan Cox1e999732007-04-11 00:23:13 +01001016 return sectors;
1017}
1018
Edward Falk0baab862005-06-02 18:17:13 -04001019/**
Alan10305f02007-02-20 18:01:59 +00001020 * ata_id_to_dma_mode - Identify DMA mode from id block
1021 * @dev: device to identify
Randy Dunlapcc261262007-03-16 19:55:47 -07001022 * @unknown: mode to assume if we cannot tell
Alan10305f02007-02-20 18:01:59 +00001023 *
1024 * Set up the timing values for the device based upon the identify
1025 * reported values for the DMA mode. This function is used by drivers
1026 * which rely upon firmware configured modes, but wish to report the
1027 * mode correctly when possible.
1028 *
1029 * In addition we emit similarly formatted messages to the default
1030 * ata_dev_set_mode handler, in order to provide consistency of
1031 * presentation.
1032 */
1033
1034void ata_id_to_dma_mode(struct ata_device *dev, u8 unknown)
1035{
1036 unsigned int mask;
1037 u8 mode;
1038
1039 /* Pack the DMA modes */
1040 mask = ((dev->id[63] >> 8) << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA;
1041 if (dev->id[53] & 0x04)
1042 mask |= ((dev->id[88] >> 8) << ATA_SHIFT_UDMA) & ATA_MASK_UDMA;
1043
1044 /* Select the mode in use */
1045 mode = ata_xfer_mask2mode(mask);
1046
1047 if (mode != 0) {
1048 ata_dev_printk(dev, KERN_INFO, "configured for %s\n",
1049 ata_mode_string(mask));
1050 } else {
1051 /* SWDMA perhaps ? */
1052 mode = unknown;
1053 ata_dev_printk(dev, KERN_INFO, "configured for DMA\n");
1054 }
1055
1056 /* Configure the device reporting */
1057 dev->xfer_mode = mode;
1058 dev->xfer_shift = ata_xfer_mode2shift(mode);
1059}
1060
1061/**
Edward Falk0baab862005-06-02 18:17:13 -04001062 * ata_noop_dev_select - Select device 0/1 on ATA bus
1063 * @ap: ATA channel to manipulate
1064 * @device: ATA device (numbered from zero) to select
1065 *
1066 * This function performs no actual function.
1067 *
1068 * May be used as the dev_select() entry in ata_port_operations.
1069 *
1070 * LOCKING:
1071 * caller.
1072 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
1074{
1075}
1076
Edward Falk0baab862005-06-02 18:17:13 -04001077
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078/**
1079 * ata_std_dev_select - Select device 0/1 on ATA bus
1080 * @ap: ATA channel to manipulate
1081 * @device: ATA device (numbered from zero) to select
1082 *
1083 * Use the method defined in the ATA specification to
1084 * make either device 0, or device 1, active on the
Edward Falk0baab862005-06-02 18:17:13 -04001085 * ATA channel. Works with both PIO and MMIO.
1086 *
1087 * May be used as the dev_select() entry in ata_port_operations.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 *
1089 * LOCKING:
1090 * caller.
1091 */
1092
1093void ata_std_dev_select (struct ata_port *ap, unsigned int device)
1094{
1095 u8 tmp;
1096
1097 if (device == 0)
1098 tmp = ATA_DEVICE_OBS;
1099 else
1100 tmp = ATA_DEVICE_OBS | ATA_DEV1;
1101
Tejun Heo0d5ff562007-02-01 15:06:36 +09001102 iowrite8(tmp, ap->ioaddr.device_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 ata_pause(ap); /* needed; also flushes, for mmio */
1104}
1105
1106/**
1107 * ata_dev_select - Select device 0/1 on ATA bus
1108 * @ap: ATA channel to manipulate
1109 * @device: ATA device (numbered from zero) to select
1110 * @wait: non-zero to wait for Status register BSY bit to clear
1111 * @can_sleep: non-zero if context allows sleeping
1112 *
1113 * Use the method defined in the ATA specification to
1114 * make either device 0, or device 1, active on the
1115 * ATA channel.
1116 *
1117 * This is a high-level version of ata_std_dev_select(),
1118 * which additionally provides the services of inserting
1119 * the proper pauses and status polling, where needed.
1120 *
1121 * LOCKING:
1122 * caller.
1123 */
1124
1125void ata_dev_select(struct ata_port *ap, unsigned int device,
1126 unsigned int wait, unsigned int can_sleep)
1127{
Tejun Heo88574552006-06-25 20:00:35 +09001128 if (ata_msg_probe(ap))
Tejun Heo44877b42007-02-21 01:06:51 +09001129 ata_port_printk(ap, KERN_INFO, "ata_dev_select: ENTER, "
1130 "device %u, wait %u\n", device, wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
1132 if (wait)
1133 ata_wait_idle(ap);
1134
1135 ap->ops->dev_select(ap, device);
1136
1137 if (wait) {
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001138 if (can_sleep && ap->link.device[device].class == ATA_DEV_ATAPI)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 msleep(150);
1140 ata_wait_idle(ap);
1141 }
1142}
1143
1144/**
1145 * ata_dump_id - IDENTIFY DEVICE info debugging output
Tejun Heo0bd33002006-02-12 22:47:05 +09001146 * @id: IDENTIFY DEVICE page to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 *
Tejun Heo0bd33002006-02-12 22:47:05 +09001148 * Dump selected 16-bit words from the given IDENTIFY DEVICE
1149 * page.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 *
1151 * LOCKING:
1152 * caller.
1153 */
1154
Tejun Heo0bd33002006-02-12 22:47:05 +09001155static inline void ata_dump_id(const u16 *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156{
1157 DPRINTK("49==0x%04x "
1158 "53==0x%04x "
1159 "63==0x%04x "
1160 "64==0x%04x "
1161 "75==0x%04x \n",
Tejun Heo0bd33002006-02-12 22:47:05 +09001162 id[49],
1163 id[53],
1164 id[63],
1165 id[64],
1166 id[75]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 DPRINTK("80==0x%04x "
1168 "81==0x%04x "
1169 "82==0x%04x "
1170 "83==0x%04x "
1171 "84==0x%04x \n",
Tejun Heo0bd33002006-02-12 22:47:05 +09001172 id[80],
1173 id[81],
1174 id[82],
1175 id[83],
1176 id[84]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 DPRINTK("88==0x%04x "
1178 "93==0x%04x\n",
Tejun Heo0bd33002006-02-12 22:47:05 +09001179 id[88],
1180 id[93]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181}
1182
Tejun Heocb95d562006-03-06 04:31:56 +09001183/**
1184 * ata_id_xfermask - Compute xfermask from the given IDENTIFY data
1185 * @id: IDENTIFY data to compute xfer mask from
1186 *
1187 * Compute the xfermask for this device. This is not as trivial
1188 * as it seems if we must consider early devices correctly.
1189 *
1190 * FIXME: pre IDE drive timing (do we care ?).
1191 *
1192 * LOCKING:
1193 * None.
1194 *
1195 * RETURNS:
1196 * Computed xfermask
1197 */
1198static unsigned int ata_id_xfermask(const u16 *id)
1199{
1200 unsigned int pio_mask, mwdma_mask, udma_mask;
1201
1202 /* Usual case. Word 53 indicates word 64 is valid */
1203 if (id[ATA_ID_FIELD_VALID] & (1 << 1)) {
1204 pio_mask = id[ATA_ID_PIO_MODES] & 0x03;
1205 pio_mask <<= 3;
1206 pio_mask |= 0x7;
1207 } else {
1208 /* If word 64 isn't valid then Word 51 high byte holds
1209 * the PIO timing number for the maximum. Turn it into
1210 * a mask.
1211 */
Lennert Buytenhek7a0f1c82007-01-29 13:28:47 +01001212 u8 mode = (id[ATA_ID_OLD_PIO_MODES] >> 8) & 0xFF;
Alan Cox46767aeb2006-09-29 18:26:47 +01001213 if (mode < 5) /* Valid PIO range */
1214 pio_mask = (2 << mode) - 1;
1215 else
1216 pio_mask = 1;
Tejun Heocb95d562006-03-06 04:31:56 +09001217
1218 /* But wait.. there's more. Design your standards by
1219 * committee and you too can get a free iordy field to
1220 * process. However its the speeds not the modes that
1221 * are supported... Note drivers using the timing API
1222 * will get this right anyway
1223 */
1224 }
1225
1226 mwdma_mask = id[ATA_ID_MWDMA_MODES] & 0x07;
Tejun Heofb21f0d2006-03-12 12:34:35 +09001227
Alan Coxb352e572006-08-10 18:52:12 +01001228 if (ata_id_is_cfa(id)) {
1229 /*
1230 * Process compact flash extended modes
1231 */
1232 int pio = id[163] & 0x7;
1233 int dma = (id[163] >> 3) & 7;
1234
1235 if (pio)
1236 pio_mask |= (1 << 5);
1237 if (pio > 1)
1238 pio_mask |= (1 << 6);
1239 if (dma)
1240 mwdma_mask |= (1 << 3);
1241 if (dma > 1)
1242 mwdma_mask |= (1 << 4);
1243 }
1244
Tejun Heofb21f0d2006-03-12 12:34:35 +09001245 udma_mask = 0;
1246 if (id[ATA_ID_FIELD_VALID] & (1 << 2))
1247 udma_mask = id[ATA_ID_UDMA_MODES] & 0xff;
Tejun Heocb95d562006-03-06 04:31:56 +09001248
1249 return ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
1250}
1251
Tejun Heo86e45b62006-03-05 15:29:09 +09001252/**
1253 * ata_port_queue_task - Queue port_task
1254 * @ap: The ata_port to queue port_task for
Randy Dunlape2a7f772006-05-18 10:50:18 -07001255 * @fn: workqueue function to be scheduled
David Howells65f27f32006-11-22 14:55:48 +00001256 * @data: data for @fn to use
Randy Dunlape2a7f772006-05-18 10:50:18 -07001257 * @delay: delay time for workqueue function
Tejun Heo86e45b62006-03-05 15:29:09 +09001258 *
1259 * Schedule @fn(@data) for execution after @delay jiffies using
1260 * port_task. There is one port_task per port and it's the
1261 * user(low level driver)'s responsibility to make sure that only
1262 * one task is active at any given time.
1263 *
1264 * libata core layer takes care of synchronization between
1265 * port_task and EH. ata_port_queue_task() may be ignored for EH
1266 * synchronization.
1267 *
1268 * LOCKING:
1269 * Inherited from caller.
1270 */
David Howells65f27f32006-11-22 14:55:48 +00001271void ata_port_queue_task(struct ata_port *ap, work_func_t fn, void *data,
Tejun Heo86e45b62006-03-05 15:29:09 +09001272 unsigned long delay)
1273{
David Howells65f27f32006-11-22 14:55:48 +00001274 PREPARE_DELAYED_WORK(&ap->port_task, fn);
1275 ap->port_task_data = data;
Tejun Heo86e45b62006-03-05 15:29:09 +09001276
Oleg Nesterov45a66c12007-07-09 11:46:13 -07001277 /* may fail if ata_port_flush_task() in progress */
1278 queue_delayed_work(ata_wq, &ap->port_task, delay);
Tejun Heo86e45b62006-03-05 15:29:09 +09001279}
1280
1281/**
1282 * ata_port_flush_task - Flush port_task
1283 * @ap: The ata_port to flush port_task for
1284 *
1285 * After this function completes, port_task is guranteed not to
1286 * be running or scheduled.
1287 *
1288 * LOCKING:
1289 * Kernel thread context (may sleep)
1290 */
1291void ata_port_flush_task(struct ata_port *ap)
1292{
Tejun Heo86e45b62006-03-05 15:29:09 +09001293 DPRINTK("ENTER\n");
1294
Oleg Nesterov45a66c12007-07-09 11:46:13 -07001295 cancel_rearming_delayed_work(&ap->port_task);
Tejun Heo86e45b62006-03-05 15:29:09 +09001296
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001297 if (ata_msg_ctl(ap))
1298 ata_port_printk(ap, KERN_DEBUG, "%s: EXIT\n", __FUNCTION__);
Tejun Heo86e45b62006-03-05 15:29:09 +09001299}
1300
Adrian Bunk7102d232007-01-04 00:09:36 +01001301static void ata_qc_complete_internal(struct ata_queued_cmd *qc)
Tejun Heoa2a7a662005-12-13 14:48:31 +09001302{
Tejun Heo77853bf2006-01-23 13:09:36 +09001303 struct completion *waiting = qc->private_data;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001304
Tejun Heoa2a7a662005-12-13 14:48:31 +09001305 complete(waiting);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001306}
1307
1308/**
Tejun Heo24326972006-11-14 22:47:09 +09001309 * ata_exec_internal_sg - execute libata internal command
Tejun Heoa2a7a662005-12-13 14:48:31 +09001310 * @dev: Device to which the command is sent
1311 * @tf: Taskfile registers for the command and the result
Tejun Heod69cf372006-04-02 18:51:53 +09001312 * @cdb: CDB for packet command
Tejun Heoa2a7a662005-12-13 14:48:31 +09001313 * @dma_dir: Data tranfer direction of the command
Tejun Heo24326972006-11-14 22:47:09 +09001314 * @sg: sg list for the data buffer of the command
1315 * @n_elem: Number of sg entries
Tejun Heoa2a7a662005-12-13 14:48:31 +09001316 *
1317 * Executes libata internal command with timeout. @tf contains
1318 * command on entry and result on return. Timeout and error
1319 * conditions are reported via return value. No recovery action
1320 * is taken after a command times out. It's caller's duty to
1321 * clean up after timeout.
1322 *
1323 * LOCKING:
1324 * None. Should be called with kernel context, might sleep.
Tejun Heo551e8882006-06-12 14:09:49 +09001325 *
1326 * RETURNS:
1327 * Zero on success, AC_ERR_* mask on failure
Tejun Heoa2a7a662005-12-13 14:48:31 +09001328 */
Tejun Heo24326972006-11-14 22:47:09 +09001329unsigned ata_exec_internal_sg(struct ata_device *dev,
1330 struct ata_taskfile *tf, const u8 *cdb,
1331 int dma_dir, struct scatterlist *sg,
1332 unsigned int n_elem)
Tejun Heoa2a7a662005-12-13 14:48:31 +09001333{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001334 struct ata_link *link = dev->link;
1335 struct ata_port *ap = link->ap;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001336 u8 command = tf->command;
1337 struct ata_queued_cmd *qc;
Tejun Heo2ab7db12006-05-15 20:58:02 +09001338 unsigned int tag, preempted_tag;
Tejun Heodedaf2b2006-05-15 21:03:43 +09001339 u32 preempted_sactive, preempted_qc_active;
Ingo Molnar60be6b92006-07-03 00:25:26 -07001340 DECLARE_COMPLETION_ONSTACK(wait);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001341 unsigned long flags;
Tejun Heo77853bf2006-01-23 13:09:36 +09001342 unsigned int err_mask;
Tejun Heod95a7172006-05-15 20:58:14 +09001343 int rc;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001344
Jeff Garzikba6a1302006-06-22 23:46:10 -04001345 spin_lock_irqsave(ap->lock, flags);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001346
Tejun Heoe3180492006-05-15 20:58:09 +09001347 /* no internal command while frozen */
Tejun Heob51e9e52006-06-29 01:29:30 +09001348 if (ap->pflags & ATA_PFLAG_FROZEN) {
Jeff Garzikba6a1302006-06-22 23:46:10 -04001349 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +09001350 return AC_ERR_SYSTEM;
1351 }
1352
Tejun Heo2ab7db12006-05-15 20:58:02 +09001353 /* initialize internal qc */
Tejun Heoa2a7a662005-12-13 14:48:31 +09001354
Tejun Heo2ab7db12006-05-15 20:58:02 +09001355 /* XXX: Tag 0 is used for drivers with legacy EH as some
1356 * drivers choke if any other tag is given. This breaks
1357 * ata_tag_internal() test for those drivers. Don't use new
1358 * EH stuff without converting to it.
1359 */
1360 if (ap->ops->error_handler)
1361 tag = ATA_TAG_INTERNAL;
1362 else
1363 tag = 0;
1364
Tejun Heo6cec4a32006-05-15 21:03:41 +09001365 if (test_and_set_bit(tag, &ap->qc_allocated))
Tejun Heo2ab7db12006-05-15 20:58:02 +09001366 BUG();
Tejun Heof69499f2006-05-15 20:58:03 +09001367 qc = __ata_qc_from_tag(ap, tag);
Tejun Heo2ab7db12006-05-15 20:58:02 +09001368
1369 qc->tag = tag;
1370 qc->scsicmd = NULL;
1371 qc->ap = ap;
1372 qc->dev = dev;
1373 ata_qc_reinit(qc);
1374
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001375 preempted_tag = link->active_tag;
1376 preempted_sactive = link->sactive;
Tejun Heodedaf2b2006-05-15 21:03:43 +09001377 preempted_qc_active = ap->qc_active;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001378 link->active_tag = ATA_TAG_POISON;
1379 link->sactive = 0;
Tejun Heodedaf2b2006-05-15 21:03:43 +09001380 ap->qc_active = 0;
Tejun Heo2ab7db12006-05-15 20:58:02 +09001381
1382 /* prepare & issue qc */
Tejun Heoa2a7a662005-12-13 14:48:31 +09001383 qc->tf = *tf;
Tejun Heod69cf372006-04-02 18:51:53 +09001384 if (cdb)
1385 memcpy(qc->cdb, cdb, ATAPI_CDB_LEN);
Tejun Heoe61e0672006-05-15 20:57:40 +09001386 qc->flags |= ATA_QCFLAG_RESULT_TF;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001387 qc->dma_dir = dma_dir;
1388 if (dma_dir != DMA_NONE) {
Tejun Heo24326972006-11-14 22:47:09 +09001389 unsigned int i, buflen = 0;
1390
1391 for (i = 0; i < n_elem; i++)
1392 buflen += sg[i].length;
1393
1394 ata_sg_init(qc, sg, n_elem);
Brian King49c80422007-01-30 11:32:26 -06001395 qc->nbytes = buflen;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001396 }
1397
Tejun Heo77853bf2006-01-23 13:09:36 +09001398 qc->private_data = &wait;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001399 qc->complete_fn = ata_qc_complete_internal;
1400
Tejun Heo8e0e6942006-03-31 20:41:11 +09001401 ata_qc_issue(qc);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001402
Jeff Garzikba6a1302006-06-22 23:46:10 -04001403 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001404
Andrew Mortona8601e52006-06-25 01:36:52 -07001405 rc = wait_for_completion_timeout(&wait, ata_probe_timeout);
Albert Lee41ade502006-03-14 11:19:04 +08001406
Tejun Heod95a7172006-05-15 20:58:14 +09001407 ata_port_flush_task(ap);
1408
1409 if (!rc) {
Jeff Garzikba6a1302006-06-22 23:46:10 -04001410 spin_lock_irqsave(ap->lock, flags);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001411
1412 /* We're racing with irq here. If we lose, the
1413 * following test prevents us from completing the qc
Tejun Heod95a7172006-05-15 20:58:14 +09001414 * twice. If we win, the port is frozen and will be
1415 * cleaned up by ->post_internal_cmd().
Tejun Heoa2a7a662005-12-13 14:48:31 +09001416 */
Tejun Heo77853bf2006-01-23 13:09:36 +09001417 if (qc->flags & ATA_QCFLAG_ACTIVE) {
Tejun Heod95a7172006-05-15 20:58:14 +09001418 qc->err_mask |= AC_ERR_TIMEOUT;
1419
1420 if (ap->ops->error_handler)
1421 ata_port_freeze(ap);
1422 else
1423 ata_qc_complete(qc);
Tejun Heof15a1da2006-05-15 20:57:56 +09001424
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001425 if (ata_msg_warn(ap))
1426 ata_dev_printk(dev, KERN_WARNING,
Tejun Heo88574552006-06-25 20:00:35 +09001427 "qc timeout (cmd 0x%x)\n", command);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001428 }
1429
Jeff Garzikba6a1302006-06-22 23:46:10 -04001430 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001431 }
1432
Tejun Heod95a7172006-05-15 20:58:14 +09001433 /* do post_internal_cmd */
1434 if (ap->ops->post_internal_cmd)
1435 ap->ops->post_internal_cmd(qc);
1436
Tejun Heoa51d6442007-03-20 15:24:11 +09001437 /* perform minimal error analysis */
1438 if (qc->flags & ATA_QCFLAG_FAILED) {
1439 if (qc->result_tf.command & (ATA_ERR | ATA_DF))
1440 qc->err_mask |= AC_ERR_DEV;
1441
1442 if (!qc->err_mask)
1443 qc->err_mask |= AC_ERR_OTHER;
1444
1445 if (qc->err_mask & ~AC_ERR_OTHER)
1446 qc->err_mask &= ~AC_ERR_OTHER;
Tejun Heod95a7172006-05-15 20:58:14 +09001447 }
1448
Tejun Heo15869302006-05-15 20:57:33 +09001449 /* finish up */
Jeff Garzikba6a1302006-06-22 23:46:10 -04001450 spin_lock_irqsave(ap->lock, flags);
Tejun Heo15869302006-05-15 20:57:33 +09001451
Tejun Heoe61e0672006-05-15 20:57:40 +09001452 *tf = qc->result_tf;
Tejun Heo77853bf2006-01-23 13:09:36 +09001453 err_mask = qc->err_mask;
1454
1455 ata_qc_free(qc);
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001456 link->active_tag = preempted_tag;
1457 link->sactive = preempted_sactive;
Tejun Heodedaf2b2006-05-15 21:03:43 +09001458 ap->qc_active = preempted_qc_active;
Tejun Heo77853bf2006-01-23 13:09:36 +09001459
Tejun Heo1f7dd3e92006-03-24 15:25:30 +09001460 /* XXX - Some LLDDs (sata_mv) disable port on command failure.
1461 * Until those drivers are fixed, we detect the condition
1462 * here, fail the command with AC_ERR_SYSTEM and reenable the
1463 * port.
1464 *
1465 * Note that this doesn't change any behavior as internal
1466 * command failure results in disabling the device in the
1467 * higher layer for LLDDs without new reset/EH callbacks.
1468 *
1469 * Kill the following code as soon as those drivers are fixed.
1470 */
Tejun Heo198e0fe2006-04-02 18:51:52 +09001471 if (ap->flags & ATA_FLAG_DISABLED) {
Tejun Heo1f7dd3e92006-03-24 15:25:30 +09001472 err_mask |= AC_ERR_SYSTEM;
1473 ata_port_probe(ap);
1474 }
1475
Jeff Garzikba6a1302006-06-22 23:46:10 -04001476 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo15869302006-05-15 20:57:33 +09001477
Tejun Heo77853bf2006-01-23 13:09:36 +09001478 return err_mask;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001479}
1480
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481/**
Tejun Heo33480a02006-12-12 02:15:31 +09001482 * ata_exec_internal - execute libata internal command
Tejun Heo24326972006-11-14 22:47:09 +09001483 * @dev: Device to which the command is sent
1484 * @tf: Taskfile registers for the command and the result
1485 * @cdb: CDB for packet command
1486 * @dma_dir: Data tranfer direction of the command
1487 * @buf: Data buffer of the command
1488 * @buflen: Length of data buffer
1489 *
1490 * Wrapper around ata_exec_internal_sg() which takes simple
1491 * buffer instead of sg list.
1492 *
1493 * LOCKING:
1494 * None. Should be called with kernel context, might sleep.
1495 *
1496 * RETURNS:
1497 * Zero on success, AC_ERR_* mask on failure
1498 */
1499unsigned ata_exec_internal(struct ata_device *dev,
1500 struct ata_taskfile *tf, const u8 *cdb,
1501 int dma_dir, void *buf, unsigned int buflen)
1502{
Tejun Heo33480a02006-12-12 02:15:31 +09001503 struct scatterlist *psg = NULL, sg;
1504 unsigned int n_elem = 0;
Tejun Heo24326972006-11-14 22:47:09 +09001505
Tejun Heo33480a02006-12-12 02:15:31 +09001506 if (dma_dir != DMA_NONE) {
1507 WARN_ON(!buf);
1508 sg_init_one(&sg, buf, buflen);
1509 psg = &sg;
1510 n_elem++;
1511 }
Tejun Heo24326972006-11-14 22:47:09 +09001512
Tejun Heo33480a02006-12-12 02:15:31 +09001513 return ata_exec_internal_sg(dev, tf, cdb, dma_dir, psg, n_elem);
Tejun Heo24326972006-11-14 22:47:09 +09001514}
1515
1516/**
Tejun Heo977e6b92006-06-24 20:30:19 +09001517 * ata_do_simple_cmd - execute simple internal command
1518 * @dev: Device to which the command is sent
1519 * @cmd: Opcode to execute
1520 *
1521 * Execute a 'simple' command, that only consists of the opcode
1522 * 'cmd' itself, without filling any other registers
1523 *
1524 * LOCKING:
1525 * Kernel thread context (may sleep).
1526 *
1527 * RETURNS:
1528 * Zero on success, AC_ERR_* mask on failure
Tejun Heoe58eb582006-06-24 20:30:19 +09001529 */
Tejun Heo77b08fb2006-06-24 20:30:19 +09001530unsigned int ata_do_simple_cmd(struct ata_device *dev, u8 cmd)
Tejun Heoe58eb582006-06-24 20:30:19 +09001531{
1532 struct ata_taskfile tf;
Tejun Heoe58eb582006-06-24 20:30:19 +09001533
1534 ata_tf_init(dev, &tf);
1535
1536 tf.command = cmd;
1537 tf.flags |= ATA_TFLAG_DEVICE;
1538 tf.protocol = ATA_PROT_NODATA;
1539
Tejun Heo977e6b92006-06-24 20:30:19 +09001540 return ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
Tejun Heoe58eb582006-06-24 20:30:19 +09001541}
1542
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543/**
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001544 * ata_pio_need_iordy - check if iordy needed
1545 * @adev: ATA device
1546 *
1547 * Check if the current speed of the device requires IORDY. Used
1548 * by various controllers for chip configuration.
1549 */
Jeff Garzika617c092007-05-21 20:14:23 -04001550
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001551unsigned int ata_pio_need_iordy(const struct ata_device *adev)
1552{
Alan Cox432729f2007-03-08 23:22:59 +00001553 /* Controller doesn't support IORDY. Probably a pointless check
1554 as the caller should know this */
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001555 if (adev->link->ap->flags & ATA_FLAG_NO_IORDY)
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001556 return 0;
Alan Cox432729f2007-03-08 23:22:59 +00001557 /* PIO3 and higher it is mandatory */
1558 if (adev->pio_mode > XFER_PIO_2)
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001559 return 1;
Alan Cox432729f2007-03-08 23:22:59 +00001560 /* We turn it on when possible */
1561 if (ata_id_has_iordy(adev->id))
1562 return 1;
1563 return 0;
1564}
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001565
Alan Cox432729f2007-03-08 23:22:59 +00001566/**
1567 * ata_pio_mask_no_iordy - Return the non IORDY mask
1568 * @adev: ATA device
1569 *
1570 * Compute the highest mode possible if we are not using iordy. Return
1571 * -1 if no iordy mode is available.
1572 */
Jeff Garzika617c092007-05-21 20:14:23 -04001573
Alan Cox432729f2007-03-08 23:22:59 +00001574static u32 ata_pio_mask_no_iordy(const struct ata_device *adev)
1575{
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001576 /* If we have no drive specific rule, then PIO 2 is non IORDY */
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001577 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
Alan Cox432729f2007-03-08 23:22:59 +00001578 u16 pio = adev->id[ATA_ID_EIDE_PIO];
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001579 /* Is the speed faster than the drive allows non IORDY ? */
1580 if (pio) {
1581 /* This is cycle times not frequency - watch the logic! */
1582 if (pio > 240) /* PIO2 is 240nS per cycle */
Alan Cox432729f2007-03-08 23:22:59 +00001583 return 3 << ATA_SHIFT_PIO;
1584 return 7 << ATA_SHIFT_PIO;
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001585 }
1586 }
Alan Cox432729f2007-03-08 23:22:59 +00001587 return 3 << ATA_SHIFT_PIO;
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001588}
1589
1590/**
Tejun Heo49016ac2006-02-21 02:12:11 +09001591 * ata_dev_read_id - Read ID data from the specified device
Tejun Heo49016ac2006-02-21 02:12:11 +09001592 * @dev: target device
1593 * @p_class: pointer to class of the target device (may be changed)
Tejun Heobff04642006-11-10 18:08:10 +09001594 * @flags: ATA_READID_* flags
Tejun Heofe635c72006-05-15 20:57:35 +09001595 * @id: buffer to read IDENTIFY data into
Tejun Heo49016ac2006-02-21 02:12:11 +09001596 *
1597 * Read ID data from the specified device. ATA_CMD_ID_ATA is
1598 * performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
Tejun Heoaec5c3c2006-03-25 01:33:34 +09001599 * devices. This function also issues ATA_CMD_INIT_DEV_PARAMS
1600 * for pre-ATA4 drives.
Tejun Heo49016ac2006-02-21 02:12:11 +09001601 *
Alan Cox50a99012007-08-08 14:27:00 +01001602 * FIXME: ATA_CMD_ID_ATA is optional for early drives and right
1603 * now we abort if we hit that case.
1604 *
Tejun Heo49016ac2006-02-21 02:12:11 +09001605 * LOCKING:
1606 * Kernel thread context (may sleep)
1607 *
1608 * RETURNS:
1609 * 0 on success, -errno otherwise.
1610 */
Tejun Heoa9beec92006-05-31 18:27:44 +09001611int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
Tejun Heobff04642006-11-10 18:08:10 +09001612 unsigned int flags, u16 *id)
Tejun Heo49016ac2006-02-21 02:12:11 +09001613{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001614 struct ata_port *ap = dev->link->ap;
Tejun Heo49016ac2006-02-21 02:12:11 +09001615 unsigned int class = *p_class;
Tejun Heo49016ac2006-02-21 02:12:11 +09001616 struct ata_taskfile tf;
1617 unsigned int err_mask = 0;
1618 const char *reason;
Tejun Heo54936f82007-05-11 14:35:29 +02001619 int may_fallback = 1, tried_spinup = 0;
Tejun Heo49016ac2006-02-21 02:12:11 +09001620 int rc;
1621
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001622 if (ata_msg_ctl(ap))
Tejun Heo44877b42007-02-21 01:06:51 +09001623 ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER\n", __FUNCTION__);
Tejun Heo49016ac2006-02-21 02:12:11 +09001624
Tejun Heo49016ac2006-02-21 02:12:11 +09001625 ata_dev_select(ap, dev->devno, 1, 1); /* select device 0/1 */
Tejun Heo49016ac2006-02-21 02:12:11 +09001626 retry:
Tejun Heo3373efd2006-05-15 20:57:53 +09001627 ata_tf_init(dev, &tf);
Tejun Heo49016ac2006-02-21 02:12:11 +09001628
1629 switch (class) {
1630 case ATA_DEV_ATA:
1631 tf.command = ATA_CMD_ID_ATA;
1632 break;
1633 case ATA_DEV_ATAPI:
1634 tf.command = ATA_CMD_ID_ATAPI;
1635 break;
1636 default:
1637 rc = -ENODEV;
1638 reason = "unsupported class";
1639 goto err_out;
1640 }
1641
1642 tf.protocol = ATA_PROT_PIO;
Tejun Heo81afe892007-02-07 12:37:41 -08001643
1644 /* Some devices choke if TF registers contain garbage. Make
1645 * sure those are properly initialized.
1646 */
1647 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1648
1649 /* Device presence detection is unreliable on some
1650 * controllers. Always poll IDENTIFY if available.
1651 */
1652 tf.flags |= ATA_TFLAG_POLLING;
Tejun Heo49016ac2006-02-21 02:12:11 +09001653
Tejun Heo3373efd2006-05-15 20:57:53 +09001654 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
Tejun Heo49016ac2006-02-21 02:12:11 +09001655 id, sizeof(id[0]) * ATA_ID_WORDS);
Tejun Heo49016ac2006-02-21 02:12:11 +09001656 if (err_mask) {
Tejun Heo800b3992006-12-03 21:34:13 +09001657 if (err_mask & AC_ERR_NODEV_HINT) {
Tejun Heo55a8e2c2006-11-10 18:08:10 +09001658 DPRINTK("ata%u.%d: NODEV after polling detection\n",
Tejun Heo44877b42007-02-21 01:06:51 +09001659 ap->print_id, dev->devno);
Tejun Heo55a8e2c2006-11-10 18:08:10 +09001660 return -ENOENT;
1661 }
1662
Tejun Heo54936f82007-05-11 14:35:29 +02001663 /* Device or controller might have reported the wrong
1664 * device class. Give a shot at the other IDENTIFY if
1665 * the current one is aborted by the device.
1666 */
1667 if (may_fallback &&
1668 (err_mask == AC_ERR_DEV) && (tf.feature & ATA_ABORTED)) {
1669 may_fallback = 0;
1670
1671 if (class == ATA_DEV_ATA)
1672 class = ATA_DEV_ATAPI;
1673 else
1674 class = ATA_DEV_ATA;
1675 goto retry;
1676 }
1677
Tejun Heo49016ac2006-02-21 02:12:11 +09001678 rc = -EIO;
1679 reason = "I/O error";
Tejun Heo49016ac2006-02-21 02:12:11 +09001680 goto err_out;
1681 }
1682
Tejun Heo54936f82007-05-11 14:35:29 +02001683 /* Falling back doesn't make sense if ID data was read
1684 * successfully at least once.
1685 */
1686 may_fallback = 0;
1687
Tejun Heo49016ac2006-02-21 02:12:11 +09001688 swap_buf_le16(id, ATA_ID_WORDS);
1689
Tejun Heo49016ac2006-02-21 02:12:11 +09001690 /* sanity check */
Tejun Heoa4f57492006-09-12 20:35:49 -07001691 rc = -EINVAL;
Alan Cox60700682007-06-07 16:13:55 +01001692 reason = "device reports invalid type";
Tejun Heoa4f57492006-09-12 20:35:49 -07001693
1694 if (class == ATA_DEV_ATA) {
1695 if (!ata_id_is_ata(id) && !ata_id_is_cfa(id))
1696 goto err_out;
1697 } else {
1698 if (ata_id_is_ata(id))
1699 goto err_out;
Tejun Heo49016ac2006-02-21 02:12:11 +09001700 }
1701
Mark Lord169439c2007-04-17 18:26:07 -04001702 if (!tried_spinup && (id[2] == 0x37c8 || id[2] == 0x738c)) {
1703 tried_spinup = 1;
1704 /*
1705 * Drive powered-up in standby mode, and requires a specific
1706 * SET_FEATURES spin-up subcommand before it will accept
1707 * anything other than the original IDENTIFY command.
1708 */
1709 ata_tf_init(dev, &tf);
1710 tf.command = ATA_CMD_SET_FEATURES;
1711 tf.feature = SETFEATURES_SPINUP;
1712 tf.protocol = ATA_PROT_NODATA;
1713 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1714 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
Ryan Powerfb0582f92007-08-10 13:59:35 -07001715 if (err_mask && id[2] != 0x738c) {
Mark Lord169439c2007-04-17 18:26:07 -04001716 rc = -EIO;
1717 reason = "SPINUP failed";
1718 goto err_out;
1719 }
1720 /*
1721 * If the drive initially returned incomplete IDENTIFY info,
1722 * we now must reissue the IDENTIFY command.
1723 */
1724 if (id[2] == 0x37c8)
1725 goto retry;
1726 }
1727
Tejun Heobff04642006-11-10 18:08:10 +09001728 if ((flags & ATA_READID_POSTRESET) && class == ATA_DEV_ATA) {
Tejun Heo49016ac2006-02-21 02:12:11 +09001729 /*
1730 * The exact sequence expected by certain pre-ATA4 drives is:
1731 * SRST RESET
Alan Cox50a99012007-08-08 14:27:00 +01001732 * IDENTIFY (optional in early ATA)
1733 * INITIALIZE DEVICE PARAMETERS (later IDE and ATA)
Tejun Heo49016ac2006-02-21 02:12:11 +09001734 * anything else..
1735 * Some drives were very specific about that exact sequence.
Alan Cox50a99012007-08-08 14:27:00 +01001736 *
1737 * Note that ATA4 says lba is mandatory so the second check
1738 * shoud never trigger.
Tejun Heo49016ac2006-02-21 02:12:11 +09001739 */
1740 if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
Tejun Heo3373efd2006-05-15 20:57:53 +09001741 err_mask = ata_dev_init_params(dev, id[3], id[6]);
Tejun Heo49016ac2006-02-21 02:12:11 +09001742 if (err_mask) {
1743 rc = -EIO;
1744 reason = "INIT_DEV_PARAMS failed";
1745 goto err_out;
1746 }
1747
1748 /* current CHS translation info (id[53-58]) might be
1749 * changed. reread the identify device info.
1750 */
Tejun Heobff04642006-11-10 18:08:10 +09001751 flags &= ~ATA_READID_POSTRESET;
Tejun Heo49016ac2006-02-21 02:12:11 +09001752 goto retry;
1753 }
1754 }
1755
1756 *p_class = class;
Tejun Heofe635c72006-05-15 20:57:35 +09001757
Tejun Heo49016ac2006-02-21 02:12:11 +09001758 return 0;
1759
1760 err_out:
Tejun Heo88574552006-06-25 20:00:35 +09001761 if (ata_msg_warn(ap))
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001762 ata_dev_printk(dev, KERN_WARNING, "failed to IDENTIFY "
Tejun Heo88574552006-06-25 20:00:35 +09001763 "(%s, err_mask=0x%x)\n", reason, err_mask);
Tejun Heo49016ac2006-02-21 02:12:11 +09001764 return rc;
1765}
1766
Tejun Heo3373efd2006-05-15 20:57:53 +09001767static inline u8 ata_dev_knobble(struct ata_device *dev)
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09001768{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001769 struct ata_port *ap = dev->link->ap;
1770 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09001771}
1772
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001773static void ata_dev_config_ncq(struct ata_device *dev,
1774 char *desc, size_t desc_sz)
1775{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001776 struct ata_port *ap = dev->link->ap;
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001777 int hdepth = 0, ddepth = ata_id_queue_depth(dev->id);
1778
1779 if (!ata_id_has_ncq(dev->id)) {
1780 desc[0] = '\0';
1781 return;
1782 }
Tejun Heo75683fe2007-07-05 13:31:27 +09001783 if (dev->horkage & ATA_HORKAGE_NONCQ) {
Alan Cox6919a0a2006-10-27 19:08:46 -07001784 snprintf(desc, desc_sz, "NCQ (not used)");
1785 return;
1786 }
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001787 if (ap->flags & ATA_FLAG_NCQ) {
Jeff Garzikcca39742006-08-24 03:19:22 -04001788 hdepth = min(ap->scsi_host->can_queue, ATA_MAX_QUEUE - 1);
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001789 dev->flags |= ATA_DFLAG_NCQ;
1790 }
1791
1792 if (hdepth >= ddepth)
1793 snprintf(desc, desc_sz, "NCQ (depth %d)", ddepth);
1794 else
1795 snprintf(desc, desc_sz, "NCQ (depth %d/%d)", hdepth, ddepth);
1796}
1797
Tejun Heo49016ac2006-02-21 02:12:11 +09001798/**
Tejun Heoffeae412006-03-01 16:09:35 +09001799 * ata_dev_configure - Configure the specified ATA/ATAPI device
Tejun Heoffeae412006-03-01 16:09:35 +09001800 * @dev: Target device to configure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 *
Tejun Heoffeae412006-03-01 16:09:35 +09001802 * Configure @dev according to @dev->id. Generic and low-level
1803 * driver specific fixups are also applied.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 *
1805 * LOCKING:
Tejun Heoffeae412006-03-01 16:09:35 +09001806 * Kernel thread context (may sleep)
1807 *
1808 * RETURNS:
1809 * 0 on success, -errno otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 */
Tejun Heoefdaedc2006-11-01 18:38:52 +09001811int ata_dev_configure(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001813 struct ata_port *ap = dev->link->ap;
1814 struct ata_eh_context *ehc = &dev->link->eh_context;
Tejun Heo67465442007-05-15 03:28:16 +09001815 int print_info = ehc->i.flags & ATA_EHI_PRINTINFO;
Tejun Heo1148c3a2006-03-13 19:48:04 +09001816 const u16 *id = dev->id;
Tejun Heoff8854b2006-03-06 04:31:56 +09001817 unsigned int xfer_mask;
Alan Coxb352e572006-08-10 18:52:12 +01001818 char revbuf[7]; /* XYZ-99\0 */
Eric D. Mudama3f64f562007-01-30 23:00:40 -07001819 char fwrevbuf[ATA_ID_FW_REV_LEN+1];
1820 char modelbuf[ATA_ID_PROD_LEN+1];
Brian Kinge6d902a2006-06-28 08:30:31 -05001821 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001823 if (!ata_dev_enabled(dev) && ata_msg_info(ap)) {
Tejun Heo44877b42007-02-21 01:06:51 +09001824 ata_dev_printk(dev, KERN_INFO, "%s: ENTER/EXIT -- nodev\n",
1825 __FUNCTION__);
Tejun Heoffeae412006-03-01 16:09:35 +09001826 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 }
1828
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001829 if (ata_msg_probe(ap))
Tejun Heo44877b42007-02-21 01:06:51 +09001830 ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
Tejun Heo75683fe2007-07-05 13:31:27 +09001832 /* set horkage */
1833 dev->horkage |= ata_dev_blacklisted(dev);
1834
Tejun Heo67465442007-05-15 03:28:16 +09001835 /* let ACPI work its magic */
1836 rc = ata_acpi_on_devcfg(dev);
1837 if (rc)
1838 return rc;
Kristen Carlson Accardi08573a82006-11-10 16:14:47 -08001839
Tejun Heoc39f5eb2006-03-13 19:51:19 +09001840 /* print device capabilities */
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001841 if (ata_msg_probe(ap))
Tejun Heo88574552006-06-25 20:00:35 +09001842 ata_dev_printk(dev, KERN_DEBUG,
1843 "%s: cfg 49:%04x 82:%04x 83:%04x 84:%04x "
1844 "85:%04x 86:%04x 87:%04x 88:%04x\n",
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001845 __FUNCTION__,
Tejun Heof15a1da2006-05-15 20:57:56 +09001846 id[49], id[82], id[83], id[84],
1847 id[85], id[86], id[87], id[88]);
Tejun Heoc39f5eb2006-03-13 19:51:19 +09001848
Tejun Heo208a9932006-03-05 17:55:58 +09001849 /* initialize to-be-configured parameters */
Tejun Heoea1dd4e2006-04-02 18:51:53 +09001850 dev->flags &= ~ATA_DFLAG_CFG_MASK;
Tejun Heo208a9932006-03-05 17:55:58 +09001851 dev->max_sectors = 0;
1852 dev->cdb_len = 0;
1853 dev->n_sectors = 0;
1854 dev->cylinders = 0;
1855 dev->heads = 0;
1856 dev->sectors = 0;
1857
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 /*
1859 * common ATA, ATAPI feature tests
1860 */
1861
Tejun Heoff8854b2006-03-06 04:31:56 +09001862 /* find max transfer mode; for printk only */
Tejun Heo1148c3a2006-03-13 19:48:04 +09001863 xfer_mask = ata_id_xfermask(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001865 if (ata_msg_probe(ap))
1866 ata_dump_id(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
Albert Leeef143d52007-06-05 13:01:33 +08001868 /* SCSI only uses 4-char revisions, dump full 8 chars from ATA */
1869 ata_id_c_string(dev->id, fwrevbuf, ATA_ID_FW_REV,
1870 sizeof(fwrevbuf));
1871
1872 ata_id_c_string(dev->id, modelbuf, ATA_ID_PROD,
1873 sizeof(modelbuf));
1874
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 /* ATA-specific feature tests */
1876 if (dev->class == ATA_DEV_ATA) {
Alan Coxb352e572006-08-10 18:52:12 +01001877 if (ata_id_is_cfa(id)) {
1878 if (id[162] & 1) /* CPRM may make this media unusable */
Tejun Heo44877b42007-02-21 01:06:51 +09001879 ata_dev_printk(dev, KERN_WARNING,
1880 "supports DRM functions and may "
1881 "not be fully accessable.\n");
Alan Coxb352e572006-08-10 18:52:12 +01001882 snprintf(revbuf, 7, "CFA");
1883 }
1884 else
1885 snprintf(revbuf, 7, "ATA-%d", ata_id_major_version(id));
1886
Tejun Heo1148c3a2006-03-13 19:48:04 +09001887 dev->n_sectors = ata_id_n_sectors(id);
Tejun Heo29407402006-02-12 22:47:04 +09001888
Eric D. Mudama3f64f562007-01-30 23:00:40 -07001889 if (dev->id[59] & 0x100)
1890 dev->multi_count = dev->id[59] & 0xff;
1891
Tejun Heo1148c3a2006-03-13 19:48:04 +09001892 if (ata_id_has_lba(id)) {
Tejun Heo4c2d7212006-03-05 17:55:58 +09001893 const char *lba_desc;
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001894 char ncq_desc[20];
Albert Lee8bf62ece2005-05-12 15:29:42 -04001895
Tejun Heo4c2d7212006-03-05 17:55:58 +09001896 lba_desc = "LBA";
1897 dev->flags |= ATA_DFLAG_LBA;
Tejun Heo1148c3a2006-03-13 19:48:04 +09001898 if (ata_id_has_lba48(id)) {
Albert Lee8bf62ece2005-05-12 15:29:42 -04001899 dev->flags |= ATA_DFLAG_LBA48;
Tejun Heo4c2d7212006-03-05 17:55:58 +09001900 lba_desc = "LBA48";
Tejun Heo6fc49ad2006-11-11 20:10:45 +09001901
1902 if (dev->n_sectors >= (1UL << 28) &&
1903 ata_id_has_flush_ext(id))
1904 dev->flags |= ATA_DFLAG_FLUSH_EXT;
Tejun Heo4c2d7212006-03-05 17:55:58 +09001905 }
Albert Lee8bf62ece2005-05-12 15:29:42 -04001906
Tejun Heo16c55b02007-08-29 11:58:33 +09001907 if (!(dev->horkage & ATA_HORKAGE_BROKEN_HPA) &&
1908 ata_id_hpa_enabled(dev->id))
1909 dev->n_sectors = ata_hpa_resize(dev);
Alan Cox1e999732007-04-11 00:23:13 +01001910
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001911 /* config NCQ */
1912 ata_dev_config_ncq(dev, ncq_desc, sizeof(ncq_desc));
1913
Albert Lee8bf62ece2005-05-12 15:29:42 -04001914 /* print device info to dmesg */
Eric D. Mudama3f64f562007-01-30 23:00:40 -07001915 if (ata_msg_drv(ap) && print_info) {
1916 ata_dev_printk(dev, KERN_INFO,
1917 "%s: %s, %s, max %s\n",
1918 revbuf, modelbuf, fwrevbuf,
1919 ata_mode_string(xfer_mask));
1920 ata_dev_printk(dev, KERN_INFO,
1921 "%Lu sectors, multi %u: %s %s\n",
Tejun Heof15a1da2006-05-15 20:57:56 +09001922 (unsigned long long)dev->n_sectors,
Eric D. Mudama3f64f562007-01-30 23:00:40 -07001923 dev->multi_count, lba_desc, ncq_desc);
1924 }
Tejun Heoffeae412006-03-01 16:09:35 +09001925 } else {
Albert Lee8bf62ece2005-05-12 15:29:42 -04001926 /* CHS */
1927
1928 /* Default translation */
Tejun Heo1148c3a2006-03-13 19:48:04 +09001929 dev->cylinders = id[1];
1930 dev->heads = id[3];
1931 dev->sectors = id[6];
Albert Lee8bf62ece2005-05-12 15:29:42 -04001932
Tejun Heo1148c3a2006-03-13 19:48:04 +09001933 if (ata_id_current_chs_valid(id)) {
Albert Lee8bf62ece2005-05-12 15:29:42 -04001934 /* Current CHS translation is valid. */
Tejun Heo1148c3a2006-03-13 19:48:04 +09001935 dev->cylinders = id[54];
1936 dev->heads = id[55];
1937 dev->sectors = id[56];
Albert Lee8bf62ece2005-05-12 15:29:42 -04001938 }
1939
1940 /* print device info to dmesg */
Eric D. Mudama3f64f562007-01-30 23:00:40 -07001941 if (ata_msg_drv(ap) && print_info) {
Tejun Heo88574552006-06-25 20:00:35 +09001942 ata_dev_printk(dev, KERN_INFO,
Eric D. Mudama3f64f562007-01-30 23:00:40 -07001943 "%s: %s, %s, max %s\n",
1944 revbuf, modelbuf, fwrevbuf,
1945 ata_mode_string(xfer_mask));
Jeff Garzika84471f2007-02-26 05:51:33 -05001946 ata_dev_printk(dev, KERN_INFO,
Eric D. Mudama3f64f562007-01-30 23:00:40 -07001947 "%Lu sectors, multi %u, CHS %u/%u/%u\n",
1948 (unsigned long long)dev->n_sectors,
1949 dev->multi_count, dev->cylinders,
1950 dev->heads, dev->sectors);
1951 }
Albert Lee07f6f7d2005-11-01 19:33:20 +08001952 }
1953
Tejun Heo6e7846e2006-02-12 23:32:58 +09001954 dev->cdb_len = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 }
1956
1957 /* ATAPI-specific feature tests */
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05001958 else if (dev->class == ATA_DEV_ATAPI) {
Albert Lee08a556d2006-03-31 13:29:04 +08001959 char *cdb_intr_string = "";
1960
Tejun Heo1148c3a2006-03-13 19:48:04 +09001961 rc = atapi_cdb_len(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001963 if (ata_msg_warn(ap))
Tejun Heo88574552006-06-25 20:00:35 +09001964 ata_dev_printk(dev, KERN_WARNING,
1965 "unsupported CDB len\n");
Tejun Heoffeae412006-03-01 16:09:35 +09001966 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 goto err_out_nosup;
1968 }
Tejun Heo6e7846e2006-02-12 23:32:58 +09001969 dev->cdb_len = (unsigned int) rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04001971 /*
1972 * check to see if this ATAPI device supports
1973 * Asynchronous Notification
1974 */
1975 if ((ap->flags & ATA_FLAG_AN) && ata_id_has_AN(id)) {
1976 int err;
1977 /* issue SET feature command to turn this on */
1978 err = ata_dev_set_AN(dev, SETFEATURES_SATA_ENABLE);
1979 if (err)
1980 ata_dev_printk(dev, KERN_ERR,
1981 "unable to set AN, err %x\n",
1982 err);
1983 else
1984 dev->flags |= ATA_DFLAG_AN;
1985 }
1986
Albert Lee08a556d2006-03-31 13:29:04 +08001987 if (ata_id_cdb_intr(dev->id)) {
Albert Lee312f7da2005-09-27 17:38:03 +08001988 dev->flags |= ATA_DFLAG_CDB_INTR;
Albert Lee08a556d2006-03-31 13:29:04 +08001989 cdb_intr_string = ", CDB intr";
1990 }
Albert Lee312f7da2005-09-27 17:38:03 +08001991
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 /* print device info to dmesg */
Borislav Petkov5afc8142006-06-27 14:51:25 +02001993 if (ata_msg_drv(ap) && print_info)
Albert Leeef143d52007-06-05 13:01:33 +08001994 ata_dev_printk(dev, KERN_INFO,
1995 "ATAPI: %s, %s, max %s%s\n",
1996 modelbuf, fwrevbuf,
Tejun Heo12436c32006-05-15 20:59:15 +09001997 ata_mode_string(xfer_mask),
1998 cdb_intr_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 }
2000
Tejun Heo914ed352006-11-01 18:39:55 +09002001 /* determine max_sectors */
2002 dev->max_sectors = ATA_MAX_SECTORS;
2003 if (dev->flags & ATA_DFLAG_LBA48)
2004 dev->max_sectors = ATA_MAX_SECTORS_LBA48;
2005
Alan Cox93590852006-09-12 16:55:12 +01002006 if (dev->horkage & ATA_HORKAGE_DIAGNOSTIC) {
2007 /* Let the user know. We don't want to disallow opens for
2008 rescue purposes, or in case the vendor is just a blithering
2009 idiot */
2010 if (print_info) {
2011 ata_dev_printk(dev, KERN_WARNING,
2012"Drive reports diagnostics failure. This may indicate a drive\n");
2013 ata_dev_printk(dev, KERN_WARNING,
2014"fault or invalid emulation. Contact drive vendor for information.\n");
2015 }
2016 }
2017
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09002018 /* limit bridge transfers to udma5, 200 sectors */
Tejun Heo3373efd2006-05-15 20:57:53 +09002019 if (ata_dev_knobble(dev)) {
Borislav Petkov5afc8142006-06-27 14:51:25 +02002020 if (ata_msg_drv(ap) && print_info)
Tejun Heof15a1da2006-05-15 20:57:56 +09002021 ata_dev_printk(dev, KERN_INFO,
2022 "applying bridge limits\n");
Tejun Heo5a529132006-03-24 14:07:50 +09002023 dev->udma_mask &= ATA_UDMA5;
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09002024 dev->max_sectors = ATA_MAX_SECTORS;
2025 }
2026
Tejun Heo75683fe2007-07-05 13:31:27 +09002027 if (dev->horkage & ATA_HORKAGE_MAX_SEC_128)
Tejun Heo03ec52d2007-04-12 13:38:11 +09002028 dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_128,
2029 dev->max_sectors);
Albert Lee18d6e9d2007-04-02 11:34:15 +08002030
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09002031 if (ap->ops->dev_config)
Alancd0d3bb2007-03-02 00:56:15 +00002032 ap->ops->dev_config(dev);
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09002033
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002034 if (ata_msg_probe(ap))
2035 ata_dev_printk(dev, KERN_DEBUG, "%s: EXIT, drv_stat = 0x%x\n",
2036 __FUNCTION__, ata_chk_status(ap));
Tejun Heoffeae412006-03-01 16:09:35 +09002037 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038
2039err_out_nosup:
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002040 if (ata_msg_probe(ap))
Tejun Heo88574552006-06-25 20:00:35 +09002041 ata_dev_printk(dev, KERN_DEBUG,
2042 "%s: EXIT, err\n", __FUNCTION__);
Tejun Heoffeae412006-03-01 16:09:35 +09002043 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044}
2045
2046/**
Alan Cox2e41e8e2007-03-08 23:19:19 +00002047 * ata_cable_40wire - return 40 wire cable type
Alan Coxbe0d18d2007-03-06 02:37:56 -08002048 * @ap: port
2049 *
Alan Cox2e41e8e2007-03-08 23:19:19 +00002050 * Helper method for drivers which want to hardwire 40 wire cable
Alan Coxbe0d18d2007-03-06 02:37:56 -08002051 * detection.
2052 */
2053
2054int ata_cable_40wire(struct ata_port *ap)
2055{
2056 return ATA_CBL_PATA40;
2057}
2058
2059/**
Alan Cox2e41e8e2007-03-08 23:19:19 +00002060 * ata_cable_80wire - return 80 wire cable type
Alan Coxbe0d18d2007-03-06 02:37:56 -08002061 * @ap: port
2062 *
Alan Cox2e41e8e2007-03-08 23:19:19 +00002063 * Helper method for drivers which want to hardwire 80 wire cable
Alan Coxbe0d18d2007-03-06 02:37:56 -08002064 * detection.
2065 */
2066
2067int ata_cable_80wire(struct ata_port *ap)
2068{
2069 return ATA_CBL_PATA80;
2070}
2071
2072/**
2073 * ata_cable_unknown - return unknown PATA cable.
2074 * @ap: port
2075 *
2076 * Helper method for drivers which have no PATA cable detection.
2077 */
2078
2079int ata_cable_unknown(struct ata_port *ap)
2080{
2081 return ATA_CBL_PATA_UNK;
2082}
2083
2084/**
2085 * ata_cable_sata - return SATA cable type
2086 * @ap: port
2087 *
2088 * Helper method for drivers which have SATA cables
2089 */
2090
2091int ata_cable_sata(struct ata_port *ap)
2092{
2093 return ATA_CBL_SATA;
2094}
2095
2096/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 * ata_bus_probe - Reset and probe ATA bus
2098 * @ap: Bus to probe
2099 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04002100 * Master ATA bus probing function. Initiates a hardware-dependent
2101 * bus reset, then attempts to identify any devices found on
2102 * the bus.
2103 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002105 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 *
2107 * RETURNS:
Tejun Heo96072e62006-04-01 01:38:17 +09002108 * Zero on success, negative errno otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 */
2110
Brian King80289162006-08-07 14:27:31 -05002111int ata_bus_probe(struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112{
Tejun Heo28ca5c52006-03-01 16:09:36 +09002113 unsigned int classes[ATA_MAX_DEVICES];
Tejun Heo14d2bac2006-04-02 17:54:46 +09002114 int tries[ATA_MAX_DEVICES];
Tejun Heof58229f2007-08-06 18:36:23 +09002115 int rc;
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002116 struct ata_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117
Tejun Heo28ca5c52006-03-01 16:09:36 +09002118 ata_port_probe(ap);
2119
Tejun Heof58229f2007-08-06 18:36:23 +09002120 ata_link_for_each_dev(dev, &ap->link)
2121 tries[dev->devno] = ATA_PROBE_MAX_TRIES;
Tejun Heo14d2bac2006-04-02 17:54:46 +09002122
2123 retry:
Tejun Heo20444702006-03-13 01:57:01 +09002124 /* reset and determine device classes */
Tejun Heo52783c52006-05-31 18:28:22 +09002125 ap->ops->phy_reset(ap);
Tejun Heo2061a472006-03-12 00:57:39 +09002126
Tejun Heof58229f2007-08-06 18:36:23 +09002127 ata_link_for_each_dev(dev, &ap->link) {
Tejun Heo52783c52006-05-31 18:28:22 +09002128 if (!(ap->flags & ATA_FLAG_DISABLED) &&
2129 dev->class != ATA_DEV_UNKNOWN)
2130 classes[dev->devno] = dev->class;
2131 else
2132 classes[dev->devno] = ATA_DEV_NONE;
Tejun Heo20444702006-03-13 01:57:01 +09002133
Tejun Heo52783c52006-05-31 18:28:22 +09002134 dev->class = ATA_DEV_UNKNOWN;
Tejun Heo28ca5c52006-03-01 16:09:36 +09002135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136
Tejun Heo52783c52006-05-31 18:28:22 +09002137 ata_port_probe(ap);
Tejun Heo20444702006-03-13 01:57:01 +09002138
Alan Coxb6079ca2006-05-22 16:52:06 +01002139 /* after the reset the device state is PIO 0 and the controller
2140 state is undefined. Record the mode */
2141
Tejun Heof58229f2007-08-06 18:36:23 +09002142 ata_link_for_each_dev(dev, &ap->link)
2143 dev->pio_mode = XFER_PIO_0;
Alan Coxb6079ca2006-05-22 16:52:06 +01002144
Jeff Garzikf31f0cc2007-03-02 17:47:28 -05002145 /* read IDENTIFY page and configure devices. We have to do the identify
2146 specific sequence bass-ackwards so that PDIAG- is released by
2147 the slave device */
2148
Tejun Heof58229f2007-08-06 18:36:23 +09002149 ata_link_for_each_dev(dev, &ap->link) {
2150 if (tries[dev->devno])
2151 dev->class = classes[dev->devno];
Tejun Heo14d2bac2006-04-02 17:54:46 +09002152
Tejun Heoe1211e32006-04-01 01:38:18 +09002153 if (!ata_dev_enabled(dev))
Tejun Heoffeae412006-03-01 16:09:35 +09002154 continue;
2155
Tejun Heobff04642006-11-10 18:08:10 +09002156 rc = ata_dev_read_id(dev, &dev->class, ATA_READID_POSTRESET,
2157 dev->id);
Tejun Heo14d2bac2006-04-02 17:54:46 +09002158 if (rc)
2159 goto fail;
Jeff Garzikf31f0cc2007-03-02 17:47:28 -05002160 }
2161
Alan Coxbe0d18d2007-03-06 02:37:56 -08002162 /* Now ask for the cable type as PDIAG- should have been released */
2163 if (ap->ops->cable_detect)
2164 ap->cbl = ap->ops->cable_detect(ap);
2165
Alan Cox614fe292007-08-22 23:22:45 +01002166 /* We may have SATA bridge glue hiding here irrespective of the
2167 reported cable types and sensed types */
2168 ata_link_for_each_dev(dev, &ap->link) {
2169 if (!ata_dev_enabled(dev))
2170 continue;
2171 /* SATA drives indicate we have a bridge. We don't know which
2172 end of the link the bridge is which is a problem */
2173 if (ata_id_is_sata(dev->id))
2174 ap->cbl = ATA_CBL_SATA;
2175 }
2176
Jeff Garzikf31f0cc2007-03-02 17:47:28 -05002177 /* After the identify sequence we can now set up the devices. We do
2178 this in the normal order so that the user doesn't get confused */
2179
Tejun Heof58229f2007-08-06 18:36:23 +09002180 ata_link_for_each_dev(dev, &ap->link) {
Jeff Garzikf31f0cc2007-03-02 17:47:28 -05002181 if (!ata_dev_enabled(dev))
2182 continue;
Tejun Heoffeae412006-03-01 16:09:35 +09002183
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002184 ap->link.eh_context.i.flags |= ATA_EHI_PRINTINFO;
Tejun Heoefdaedc2006-11-01 18:38:52 +09002185 rc = ata_dev_configure(dev);
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002186 ap->link.eh_context.i.flags &= ~ATA_EHI_PRINTINFO;
Tejun Heo14d2bac2006-04-02 17:54:46 +09002187 if (rc)
2188 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 }
2190
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002191 /* configure transfer mode */
Tejun Heo02607312007-08-06 18:36:23 +09002192 rc = ata_set_mode(&ap->link, &dev);
Tejun Heo4ae72a12007-02-02 16:22:30 +09002193 if (rc)
Tejun Heo51713d32006-04-11 22:26:29 +09002194 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195
Tejun Heof58229f2007-08-06 18:36:23 +09002196 ata_link_for_each_dev(dev, &ap->link)
2197 if (ata_dev_enabled(dev))
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002198 return 0;
Alan Coxe35a9e02006-03-27 18:46:37 +01002199
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002200 /* no device present, disable port */
2201 ata_port_disable(ap);
Tejun Heo96072e62006-04-01 01:38:17 +09002202 return -ENODEV;
Tejun Heo14d2bac2006-04-02 17:54:46 +09002203
2204 fail:
Tejun Heo4ae72a12007-02-02 16:22:30 +09002205 tries[dev->devno]--;
2206
Tejun Heo14d2bac2006-04-02 17:54:46 +09002207 switch (rc) {
2208 case -EINVAL:
Tejun Heo4ae72a12007-02-02 16:22:30 +09002209 /* eeek, something went very wrong, give up */
Tejun Heo14d2bac2006-04-02 17:54:46 +09002210 tries[dev->devno] = 0;
2211 break;
Tejun Heo4ae72a12007-02-02 16:22:30 +09002212
2213 case -ENODEV:
2214 /* give it just one more chance */
2215 tries[dev->devno] = min(tries[dev->devno], 1);
Tejun Heo14d2bac2006-04-02 17:54:46 +09002216 case -EIO:
Tejun Heo4ae72a12007-02-02 16:22:30 +09002217 if (tries[dev->devno] == 1) {
2218 /* This is the last chance, better to slow
2219 * down than lose it.
2220 */
Tejun Heo936fd732007-08-06 18:36:23 +09002221 sata_down_spd_limit(&ap->link);
Tejun Heo4ae72a12007-02-02 16:22:30 +09002222 ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
2223 }
Tejun Heo14d2bac2006-04-02 17:54:46 +09002224 }
2225
Tejun Heo4ae72a12007-02-02 16:22:30 +09002226 if (!tries[dev->devno])
Tejun Heo3373efd2006-05-15 20:57:53 +09002227 ata_dev_disable(dev);
Tejun Heoec573752006-04-11 22:26:29 +09002228
Tejun Heo14d2bac2006-04-02 17:54:46 +09002229 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230}
2231
2232/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002233 * ata_port_probe - Mark port as enabled
2234 * @ap: Port for which we indicate enablement
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04002236 * Modify @ap data structure such that the system
2237 * thinks that the entire port is enabled.
2238 *
Jeff Garzikcca39742006-08-24 03:19:22 -04002239 * LOCKING: host lock, or some other form of
Jeff Garzik0cba6322005-05-30 19:49:12 -04002240 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 */
2242
2243void ata_port_probe(struct ata_port *ap)
2244{
Tejun Heo198e0fe2006-04-02 18:51:52 +09002245 ap->flags &= ~ATA_FLAG_DISABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246}
2247
2248/**
Tejun Heo3be680b2005-12-19 22:35:02 +09002249 * sata_print_link_status - Print SATA link status
Tejun Heo936fd732007-08-06 18:36:23 +09002250 * @link: SATA link to printk link status about
Tejun Heo3be680b2005-12-19 22:35:02 +09002251 *
2252 * This function prints link speed and status of a SATA link.
2253 *
2254 * LOCKING:
2255 * None.
2256 */
Tejun Heo936fd732007-08-06 18:36:23 +09002257void sata_print_link_status(struct ata_link *link)
Tejun Heo3be680b2005-12-19 22:35:02 +09002258{
Tejun Heo6d5f9732006-04-03 00:09:41 +09002259 u32 sstatus, scontrol, tmp;
Tejun Heo3be680b2005-12-19 22:35:02 +09002260
Tejun Heo936fd732007-08-06 18:36:23 +09002261 if (sata_scr_read(link, SCR_STATUS, &sstatus))
Tejun Heo3be680b2005-12-19 22:35:02 +09002262 return;
Tejun Heo936fd732007-08-06 18:36:23 +09002263 sata_scr_read(link, SCR_CONTROL, &scontrol);
Tejun Heo3be680b2005-12-19 22:35:02 +09002264
Tejun Heo936fd732007-08-06 18:36:23 +09002265 if (ata_link_online(link)) {
Tejun Heo3be680b2005-12-19 22:35:02 +09002266 tmp = (sstatus >> 4) & 0xf;
Tejun Heo936fd732007-08-06 18:36:23 +09002267 ata_link_printk(link, KERN_INFO,
Tejun Heof15a1da2006-05-15 20:57:56 +09002268 "SATA link up %s (SStatus %X SControl %X)\n",
2269 sata_spd_string(tmp), sstatus, scontrol);
Tejun Heo3be680b2005-12-19 22:35:02 +09002270 } else {
Tejun Heo936fd732007-08-06 18:36:23 +09002271 ata_link_printk(link, KERN_INFO,
Tejun Heof15a1da2006-05-15 20:57:56 +09002272 "SATA link down (SStatus %X SControl %X)\n",
2273 sstatus, scontrol);
Tejun Heo3be680b2005-12-19 22:35:02 +09002274 }
2275}
2276
2277/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04002278 * __sata_phy_reset - Wake/reset a low-level SATA PHY
2279 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002281 * This function issues commands to standard SATA Sxxx
2282 * PHY registers, to wake up the phy (and device), and
2283 * clear any reset condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 *
2285 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002286 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 *
2288 */
2289void __sata_phy_reset(struct ata_port *ap)
2290{
Tejun Heo936fd732007-08-06 18:36:23 +09002291 struct ata_link *link = &ap->link;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 unsigned long timeout = jiffies + (HZ * 5);
Tejun Heo936fd732007-08-06 18:36:23 +09002293 u32 sstatus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294
2295 if (ap->flags & ATA_FLAG_SATA_RESET) {
Brett Russcdcca89e2005-03-28 15:10:27 -05002296 /* issue phy wake/reset */
Tejun Heo936fd732007-08-06 18:36:23 +09002297 sata_scr_write_flush(link, SCR_CONTROL, 0x301);
Tejun Heo62ba2842005-06-26 23:27:19 +09002298 /* Couldn't find anything in SATA I/II specs, but
2299 * AHCI-1.1 10.4.2 says at least 1 ms. */
2300 mdelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 }
Tejun Heo81952c52006-05-15 20:57:47 +09002302 /* phy wake/clear reset */
Tejun Heo936fd732007-08-06 18:36:23 +09002303 sata_scr_write_flush(link, SCR_CONTROL, 0x300);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304
2305 /* wait for phy to become ready, if necessary */
2306 do {
2307 msleep(200);
Tejun Heo936fd732007-08-06 18:36:23 +09002308 sata_scr_read(link, SCR_STATUS, &sstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 if ((sstatus & 0xf) != 1)
2310 break;
2311 } while (time_before(jiffies, timeout));
2312
Tejun Heo3be680b2005-12-19 22:35:02 +09002313 /* print link status */
Tejun Heo936fd732007-08-06 18:36:23 +09002314 sata_print_link_status(link);
Jeff Garzik656563e2005-11-20 03:36:45 -05002315
Tejun Heo3be680b2005-12-19 22:35:02 +09002316 /* TODO: phy layer with polling, timeouts, etc. */
Tejun Heo936fd732007-08-06 18:36:23 +09002317 if (!ata_link_offline(link))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 ata_port_probe(ap);
Tejun Heo3be680b2005-12-19 22:35:02 +09002319 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 ata_port_disable(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321
Tejun Heo198e0fe2006-04-02 18:51:52 +09002322 if (ap->flags & ATA_FLAG_DISABLED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 return;
2324
2325 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
2326 ata_port_disable(ap);
2327 return;
2328 }
2329
2330 ap->cbl = ATA_CBL_SATA;
2331}
2332
2333/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04002334 * sata_phy_reset - Reset SATA bus.
2335 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002337 * This function resets the SATA bus, and then probes
2338 * the bus for devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 *
2340 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002341 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 *
2343 */
2344void sata_phy_reset(struct ata_port *ap)
2345{
2346 __sata_phy_reset(ap);
Tejun Heo198e0fe2006-04-02 18:51:52 +09002347 if (ap->flags & ATA_FLAG_DISABLED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 return;
2349 ata_bus_reset(ap);
2350}
2351
2352/**
Alan Coxebdfca62006-03-23 15:38:34 +00002353 * ata_dev_pair - return other device on cable
Alan Coxebdfca62006-03-23 15:38:34 +00002354 * @adev: device
2355 *
2356 * Obtain the other device on the same cable, or if none is
2357 * present NULL is returned
2358 */
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002359
Tejun Heo3373efd2006-05-15 20:57:53 +09002360struct ata_device *ata_dev_pair(struct ata_device *adev)
Alan Coxebdfca62006-03-23 15:38:34 +00002361{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002362 struct ata_link *link = adev->link;
2363 struct ata_device *pair = &link->device[1 - adev->devno];
Tejun Heoe1211e32006-04-01 01:38:18 +09002364 if (!ata_dev_enabled(pair))
Alan Coxebdfca62006-03-23 15:38:34 +00002365 return NULL;
2366 return pair;
2367}
2368
2369/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04002370 * ata_port_disable - Disable port.
2371 * @ap: Port to be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002373 * Modify @ap data structure such that the system
2374 * thinks that the entire port is disabled, and should
2375 * never attempt to probe or communicate with devices
2376 * on this port.
2377 *
Jeff Garzikcca39742006-08-24 03:19:22 -04002378 * LOCKING: host lock, or some other form of
Jeff Garzik780a87f2005-05-30 15:41:05 -04002379 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 */
2381
2382void ata_port_disable(struct ata_port *ap)
2383{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002384 ap->link.device[0].class = ATA_DEV_NONE;
2385 ap->link.device[1].class = ATA_DEV_NONE;
Tejun Heo198e0fe2006-04-02 18:51:52 +09002386 ap->flags |= ATA_FLAG_DISABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387}
2388
Tejun Heo1c3fae42006-04-02 20:53:28 +09002389/**
Tejun Heo3c567b72006-05-15 20:57:23 +09002390 * sata_down_spd_limit - adjust SATA spd limit downward
Tejun Heo936fd732007-08-06 18:36:23 +09002391 * @link: Link to adjust SATA spd limit for
Tejun Heo1c3fae42006-04-02 20:53:28 +09002392 *
Tejun Heo936fd732007-08-06 18:36:23 +09002393 * Adjust SATA spd limit of @link downward. Note that this
Tejun Heo1c3fae42006-04-02 20:53:28 +09002394 * function only adjusts the limit. The change must be applied
Tejun Heo3c567b72006-05-15 20:57:23 +09002395 * using sata_set_spd().
Tejun Heo1c3fae42006-04-02 20:53:28 +09002396 *
2397 * LOCKING:
2398 * Inherited from caller.
2399 *
2400 * RETURNS:
2401 * 0 on success, negative errno on failure
2402 */
Tejun Heo936fd732007-08-06 18:36:23 +09002403int sata_down_spd_limit(struct ata_link *link)
Tejun Heo1c3fae42006-04-02 20:53:28 +09002404{
Tejun Heo81952c52006-05-15 20:57:47 +09002405 u32 sstatus, spd, mask;
2406 int rc, highbit;
Tejun Heo1c3fae42006-04-02 20:53:28 +09002407
Tejun Heo936fd732007-08-06 18:36:23 +09002408 if (!sata_scr_valid(link))
Tejun Heo008a7892007-07-16 14:29:40 +09002409 return -EOPNOTSUPP;
2410
2411 /* If SCR can be read, use it to determine the current SPD.
Tejun Heo936fd732007-08-06 18:36:23 +09002412 * If not, use cached value in link->sata_spd.
Tejun Heo008a7892007-07-16 14:29:40 +09002413 */
Tejun Heo936fd732007-08-06 18:36:23 +09002414 rc = sata_scr_read(link, SCR_STATUS, &sstatus);
Tejun Heo008a7892007-07-16 14:29:40 +09002415 if (rc == 0)
2416 spd = (sstatus >> 4) & 0xf;
2417 else
Tejun Heo936fd732007-08-06 18:36:23 +09002418 spd = link->sata_spd;
Tejun Heo1c3fae42006-04-02 20:53:28 +09002419
Tejun Heo936fd732007-08-06 18:36:23 +09002420 mask = link->sata_spd_limit;
Tejun Heo1c3fae42006-04-02 20:53:28 +09002421 if (mask <= 1)
2422 return -EINVAL;
Tejun Heo008a7892007-07-16 14:29:40 +09002423
2424 /* unconditionally mask off the highest bit */
Tejun Heo1c3fae42006-04-02 20:53:28 +09002425 highbit = fls(mask) - 1;
2426 mask &= ~(1 << highbit);
2427
Tejun Heo008a7892007-07-16 14:29:40 +09002428 /* Mask off all speeds higher than or equal to the current
2429 * one. Force 1.5Gbps if current SPD is not available.
2430 */
2431 if (spd > 1)
2432 mask &= (1 << (spd - 1)) - 1;
2433 else
2434 mask &= 1;
2435
2436 /* were we already at the bottom? */
Tejun Heo1c3fae42006-04-02 20:53:28 +09002437 if (!mask)
2438 return -EINVAL;
2439
Tejun Heo936fd732007-08-06 18:36:23 +09002440 link->sata_spd_limit = mask;
Tejun Heo1c3fae42006-04-02 20:53:28 +09002441
Tejun Heo936fd732007-08-06 18:36:23 +09002442 ata_link_printk(link, KERN_WARNING, "limiting SATA link speed to %s\n",
Tejun Heof15a1da2006-05-15 20:57:56 +09002443 sata_spd_string(fls(mask)));
Tejun Heo1c3fae42006-04-02 20:53:28 +09002444
2445 return 0;
2446}
2447
Tejun Heo936fd732007-08-06 18:36:23 +09002448static int __sata_set_spd_needed(struct ata_link *link, u32 *scontrol)
Tejun Heo1c3fae42006-04-02 20:53:28 +09002449{
2450 u32 spd, limit;
2451
Tejun Heo936fd732007-08-06 18:36:23 +09002452 if (link->sata_spd_limit == UINT_MAX)
Tejun Heo1c3fae42006-04-02 20:53:28 +09002453 limit = 0;
2454 else
Tejun Heo936fd732007-08-06 18:36:23 +09002455 limit = fls(link->sata_spd_limit);
Tejun Heo1c3fae42006-04-02 20:53:28 +09002456
2457 spd = (*scontrol >> 4) & 0xf;
2458 *scontrol = (*scontrol & ~0xf0) | ((limit & 0xf) << 4);
2459
2460 return spd != limit;
2461}
2462
2463/**
Tejun Heo3c567b72006-05-15 20:57:23 +09002464 * sata_set_spd_needed - is SATA spd configuration needed
Tejun Heo936fd732007-08-06 18:36:23 +09002465 * @link: Link in question
Tejun Heo1c3fae42006-04-02 20:53:28 +09002466 *
2467 * Test whether the spd limit in SControl matches
Tejun Heo936fd732007-08-06 18:36:23 +09002468 * @link->sata_spd_limit. This function is used to determine
Tejun Heo1c3fae42006-04-02 20:53:28 +09002469 * whether hardreset is necessary to apply SATA spd
2470 * configuration.
2471 *
2472 * LOCKING:
2473 * Inherited from caller.
2474 *
2475 * RETURNS:
2476 * 1 if SATA spd configuration is needed, 0 otherwise.
2477 */
Tejun Heo936fd732007-08-06 18:36:23 +09002478int sata_set_spd_needed(struct ata_link *link)
Tejun Heo1c3fae42006-04-02 20:53:28 +09002479{
2480 u32 scontrol;
2481
Tejun Heo936fd732007-08-06 18:36:23 +09002482 if (sata_scr_read(link, SCR_CONTROL, &scontrol))
Tejun Heo1c3fae42006-04-02 20:53:28 +09002483 return 0;
2484
Tejun Heo936fd732007-08-06 18:36:23 +09002485 return __sata_set_spd_needed(link, &scontrol);
Tejun Heo1c3fae42006-04-02 20:53:28 +09002486}
2487
2488/**
Tejun Heo3c567b72006-05-15 20:57:23 +09002489 * sata_set_spd - set SATA spd according to spd limit
Tejun Heo936fd732007-08-06 18:36:23 +09002490 * @link: Link to set SATA spd for
Tejun Heo1c3fae42006-04-02 20:53:28 +09002491 *
Tejun Heo936fd732007-08-06 18:36:23 +09002492 * Set SATA spd of @link according to sata_spd_limit.
Tejun Heo1c3fae42006-04-02 20:53:28 +09002493 *
2494 * LOCKING:
2495 * Inherited from caller.
2496 *
2497 * RETURNS:
2498 * 0 if spd doesn't need to be changed, 1 if spd has been
Tejun Heo81952c52006-05-15 20:57:47 +09002499 * changed. Negative errno if SCR registers are inaccessible.
Tejun Heo1c3fae42006-04-02 20:53:28 +09002500 */
Tejun Heo936fd732007-08-06 18:36:23 +09002501int sata_set_spd(struct ata_link *link)
Tejun Heo1c3fae42006-04-02 20:53:28 +09002502{
2503 u32 scontrol;
Tejun Heo81952c52006-05-15 20:57:47 +09002504 int rc;
Tejun Heo1c3fae42006-04-02 20:53:28 +09002505
Tejun Heo936fd732007-08-06 18:36:23 +09002506 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
Tejun Heo81952c52006-05-15 20:57:47 +09002507 return rc;
Tejun Heo1c3fae42006-04-02 20:53:28 +09002508
Tejun Heo936fd732007-08-06 18:36:23 +09002509 if (!__sata_set_spd_needed(link, &scontrol))
Tejun Heo1c3fae42006-04-02 20:53:28 +09002510 return 0;
2511
Tejun Heo936fd732007-08-06 18:36:23 +09002512 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
Tejun Heo81952c52006-05-15 20:57:47 +09002513 return rc;
2514
Tejun Heo1c3fae42006-04-02 20:53:28 +09002515 return 1;
2516}
2517
Alan Cox452503f2005-10-21 19:01:32 -04002518/*
2519 * This mode timing computation functionality is ported over from
2520 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
2521 */
2522/*
Alan Coxb352e572006-08-10 18:52:12 +01002523 * PIO 0-4, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
Alan Cox452503f2005-10-21 19:01:32 -04002524 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
Alan Coxb352e572006-08-10 18:52:12 +01002525 * for UDMA6, which is currently supported only by Maxtor drives.
2526 *
2527 * For PIO 5/6 MWDMA 3/4 see the CFA specification 3.0.
Alan Cox452503f2005-10-21 19:01:32 -04002528 */
2529
2530static const struct ata_timing ata_timing[] = {
2531
2532 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
2533 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
2534 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
2535 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
2536
Alan Coxb352e572006-08-10 18:52:12 +01002537 { XFER_MW_DMA_4, 25, 0, 0, 0, 55, 20, 80, 0 },
2538 { XFER_MW_DMA_3, 25, 0, 0, 0, 65, 25, 100, 0 },
Alan Cox452503f2005-10-21 19:01:32 -04002539 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
2540 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
2541 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
2542
2543/* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002544
Alan Cox452503f2005-10-21 19:01:32 -04002545 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
2546 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
2547 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002548
Alan Cox452503f2005-10-21 19:01:32 -04002549 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
2550 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
2551 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
2552
Alan Coxb352e572006-08-10 18:52:12 +01002553 { XFER_PIO_6, 10, 55, 20, 80, 55, 20, 80, 0 },
2554 { XFER_PIO_5, 15, 65, 25, 100, 65, 25, 100, 0 },
Alan Cox452503f2005-10-21 19:01:32 -04002555 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
2556 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
2557
2558 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
2559 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
2560 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
2561
2562/* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
2563
2564 { 0xFF }
2565};
2566
2567#define ENOUGH(v,unit) (((v)-1)/(unit)+1)
2568#define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
2569
2570static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
2571{
2572 q->setup = EZ(t->setup * 1000, T);
2573 q->act8b = EZ(t->act8b * 1000, T);
2574 q->rec8b = EZ(t->rec8b * 1000, T);
2575 q->cyc8b = EZ(t->cyc8b * 1000, T);
2576 q->active = EZ(t->active * 1000, T);
2577 q->recover = EZ(t->recover * 1000, T);
2578 q->cycle = EZ(t->cycle * 1000, T);
2579 q->udma = EZ(t->udma * 1000, UT);
2580}
2581
2582void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
2583 struct ata_timing *m, unsigned int what)
2584{
2585 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
2586 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
2587 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
2588 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
2589 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
2590 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
2591 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
2592 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
2593}
2594
2595static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
2596{
2597 const struct ata_timing *t;
2598
2599 for (t = ata_timing; t->mode != speed; t++)
Alan Cox91190752005-10-26 12:17:46 -04002600 if (t->mode == 0xFF)
Alan Cox452503f2005-10-21 19:01:32 -04002601 return NULL;
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002602 return t;
Alan Cox452503f2005-10-21 19:01:32 -04002603}
2604
2605int ata_timing_compute(struct ata_device *adev, unsigned short speed,
2606 struct ata_timing *t, int T, int UT)
2607{
2608 const struct ata_timing *s;
2609 struct ata_timing p;
2610
2611 /*
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002612 * Find the mode.
Albert Lee75b1f2f2005-11-16 17:06:18 +08002613 */
Alan Cox452503f2005-10-21 19:01:32 -04002614
2615 if (!(s = ata_timing_find_mode(speed)))
2616 return -EINVAL;
2617
Albert Lee75b1f2f2005-11-16 17:06:18 +08002618 memcpy(t, s, sizeof(*s));
2619
Alan Cox452503f2005-10-21 19:01:32 -04002620 /*
2621 * If the drive is an EIDE drive, it can tell us it needs extended
2622 * PIO/MW_DMA cycle timing.
2623 */
2624
2625 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
2626 memset(&p, 0, sizeof(p));
2627 if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
2628 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
2629 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
2630 } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
2631 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
2632 }
2633 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
2634 }
2635
2636 /*
2637 * Convert the timing to bus clock counts.
2638 */
2639
Albert Lee75b1f2f2005-11-16 17:06:18 +08002640 ata_timing_quantize(t, t, T, UT);
Alan Cox452503f2005-10-21 19:01:32 -04002641
2642 /*
Randy Dunlapc893a3a2006-01-28 13:15:32 -05002643 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
2644 * S.M.A.R.T * and some other commands. We have to ensure that the
2645 * DMA cycle timing is slower/equal than the fastest PIO timing.
Alan Cox452503f2005-10-21 19:01:32 -04002646 */
2647
Alanfd3367af2006-12-07 12:41:18 +00002648 if (speed > XFER_PIO_6) {
Alan Cox452503f2005-10-21 19:01:32 -04002649 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
2650 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
2651 }
2652
2653 /*
Randy Dunlapc893a3a2006-01-28 13:15:32 -05002654 * Lengthen active & recovery time so that cycle time is correct.
Alan Cox452503f2005-10-21 19:01:32 -04002655 */
2656
2657 if (t->act8b + t->rec8b < t->cyc8b) {
2658 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
2659 t->rec8b = t->cyc8b - t->act8b;
2660 }
2661
2662 if (t->active + t->recover < t->cycle) {
2663 t->active += (t->cycle - (t->active + t->recover)) / 2;
2664 t->recover = t->cycle - t->active;
2665 }
Jeff Garzika617c092007-05-21 20:14:23 -04002666
Alan Cox4f701d12007-04-23 11:55:36 +01002667 /* In a few cases quantisation may produce enough errors to
2668 leave t->cycle too low for the sum of active and recovery
2669 if so we must correct this */
2670 if (t->active + t->recover > t->cycle)
2671 t->cycle = t->active + t->recover;
Alan Cox452503f2005-10-21 19:01:32 -04002672
2673 return 0;
2674}
2675
Tejun Heocf176e12006-04-02 17:54:46 +09002676/**
2677 * ata_down_xfermask_limit - adjust dev xfer masks downward
Tejun Heocf176e12006-04-02 17:54:46 +09002678 * @dev: Device to adjust xfer masks
Tejun Heo458337d2007-02-02 16:22:30 +09002679 * @sel: ATA_DNXFER_* selector
Tejun Heocf176e12006-04-02 17:54:46 +09002680 *
2681 * Adjust xfer masks of @dev downward. Note that this function
2682 * does not apply the change. Invoking ata_set_mode() afterwards
2683 * will apply the limit.
2684 *
2685 * LOCKING:
2686 * Inherited from caller.
2687 *
2688 * RETURNS:
2689 * 0 on success, negative errno on failure
2690 */
Tejun Heo458337d2007-02-02 16:22:30 +09002691int ata_down_xfermask_limit(struct ata_device *dev, unsigned int sel)
Tejun Heocf176e12006-04-02 17:54:46 +09002692{
Tejun Heo458337d2007-02-02 16:22:30 +09002693 char buf[32];
2694 unsigned int orig_mask, xfer_mask;
2695 unsigned int pio_mask, mwdma_mask, udma_mask;
2696 int quiet, highbit;
Tejun Heocf176e12006-04-02 17:54:46 +09002697
Tejun Heo458337d2007-02-02 16:22:30 +09002698 quiet = !!(sel & ATA_DNXFER_QUIET);
2699 sel &= ~ATA_DNXFER_QUIET;
Tejun Heocf176e12006-04-02 17:54:46 +09002700
Tejun Heo458337d2007-02-02 16:22:30 +09002701 xfer_mask = orig_mask = ata_pack_xfermask(dev->pio_mask,
2702 dev->mwdma_mask,
2703 dev->udma_mask);
2704 ata_unpack_xfermask(xfer_mask, &pio_mask, &mwdma_mask, &udma_mask);
Tejun Heocf176e12006-04-02 17:54:46 +09002705
Tejun Heo458337d2007-02-02 16:22:30 +09002706 switch (sel) {
2707 case ATA_DNXFER_PIO:
2708 highbit = fls(pio_mask) - 1;
2709 pio_mask &= ~(1 << highbit);
2710 break;
2711
2712 case ATA_DNXFER_DMA:
2713 if (udma_mask) {
2714 highbit = fls(udma_mask) - 1;
2715 udma_mask &= ~(1 << highbit);
2716 if (!udma_mask)
2717 return -ENOENT;
2718 } else if (mwdma_mask) {
2719 highbit = fls(mwdma_mask) - 1;
2720 mwdma_mask &= ~(1 << highbit);
2721 if (!mwdma_mask)
2722 return -ENOENT;
2723 }
2724 break;
2725
2726 case ATA_DNXFER_40C:
2727 udma_mask &= ATA_UDMA_MASK_40C;
2728 break;
2729
2730 case ATA_DNXFER_FORCE_PIO0:
2731 pio_mask &= 1;
2732 case ATA_DNXFER_FORCE_PIO:
2733 mwdma_mask = 0;
2734 udma_mask = 0;
2735 break;
2736
Tejun Heo458337d2007-02-02 16:22:30 +09002737 default:
2738 BUG();
2739 }
2740
2741 xfer_mask &= ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
2742
2743 if (!(xfer_mask & ATA_MASK_PIO) || xfer_mask == orig_mask)
2744 return -ENOENT;
2745
2746 if (!quiet) {
2747 if (xfer_mask & (ATA_MASK_MWDMA | ATA_MASK_UDMA))
2748 snprintf(buf, sizeof(buf), "%s:%s",
2749 ata_mode_string(xfer_mask),
2750 ata_mode_string(xfer_mask & ATA_MASK_PIO));
2751 else
2752 snprintf(buf, sizeof(buf), "%s",
2753 ata_mode_string(xfer_mask));
2754
2755 ata_dev_printk(dev, KERN_WARNING,
2756 "limiting speed to %s\n", buf);
2757 }
Tejun Heocf176e12006-04-02 17:54:46 +09002758
2759 ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask,
2760 &dev->udma_mask);
2761
Tejun Heocf176e12006-04-02 17:54:46 +09002762 return 0;
Tejun Heocf176e12006-04-02 17:54:46 +09002763}
2764
Tejun Heo3373efd2006-05-15 20:57:53 +09002765static int ata_dev_set_mode(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002767 struct ata_eh_context *ehc = &dev->link->eh_context;
Tejun Heo83206a22006-03-24 15:25:31 +09002768 unsigned int err_mask;
2769 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770
Tejun Heoe8384602006-04-02 18:51:53 +09002771 dev->flags &= ~ATA_DFLAG_PIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 if (dev->xfer_shift == ATA_SHIFT_PIO)
2773 dev->flags |= ATA_DFLAG_PIO;
2774
Tejun Heo3373efd2006-05-15 20:57:53 +09002775 err_mask = ata_dev_set_xfermode(dev);
Alan11750a42007-02-05 16:28:30 +00002776 /* Old CFA may refuse this command, which is just fine */
2777 if (dev->xfer_shift == ATA_SHIFT_PIO && ata_id_is_cfa(dev->id))
2778 err_mask &= ~AC_ERR_DEV;
Alan Cox0bc2a792007-07-31 14:01:48 +01002779 /* Some very old devices and some bad newer ones fail any kind of
2780 SET_XFERMODE request but support PIO0-2 timings and no IORDY */
2781 if (dev->xfer_shift == ATA_SHIFT_PIO && !ata_id_has_iordy(dev->id) &&
2782 dev->pio_mode <= XFER_PIO_2)
2783 err_mask &= ~AC_ERR_DEV;
Tejun Heo83206a22006-03-24 15:25:31 +09002784 if (err_mask) {
Tejun Heof15a1da2006-05-15 20:57:56 +09002785 ata_dev_printk(dev, KERN_ERR, "failed to set xfermode "
2786 "(err_mask=0x%x)\n", err_mask);
Tejun Heo83206a22006-03-24 15:25:31 +09002787 return -EIO;
2788 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789
Tejun Heobaa1e782006-11-01 18:39:27 +09002790 ehc->i.flags |= ATA_EHI_POST_SETMODE;
Tejun Heo3373efd2006-05-15 20:57:53 +09002791 rc = ata_dev_revalidate(dev, 0);
Tejun Heobaa1e782006-11-01 18:39:27 +09002792 ehc->i.flags &= ~ATA_EHI_POST_SETMODE;
Tejun Heo5eb45c02006-04-02 18:51:52 +09002793 if (rc)
Tejun Heo83206a22006-03-24 15:25:31 +09002794 return rc;
Tejun Heo48a8a142006-03-05 17:55:58 +09002795
Tejun Heo23e71c32006-03-06 04:31:57 +09002796 DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n",
2797 dev->xfer_shift, (int)dev->xfer_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798
Tejun Heof15a1da2006-05-15 20:57:56 +09002799 ata_dev_printk(dev, KERN_INFO, "configured for %s\n",
2800 ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)));
Tejun Heo83206a22006-03-24 15:25:31 +09002801 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002802}
2803
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804/**
Alan04351822007-03-06 02:37:52 -08002805 * ata_do_set_mode - Program timings and issue SET FEATURES - XFER
Tejun Heo02607312007-08-06 18:36:23 +09002806 * @link: link on which timings will be programmed
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002807 * @r_failed_dev: out paramter for failed device
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808 *
Alan04351822007-03-06 02:37:52 -08002809 * Standard implementation of the function used to tune and set
2810 * ATA device disk transfer mode (PIO3, UDMA6, etc.). If
2811 * ata_dev_set_mode() fails, pointer to the failing device is
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002812 * returned in @r_failed_dev.
Jeff Garzik780a87f2005-05-30 15:41:05 -04002813 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002815 * PCI/etc. bus probe sem.
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002816 *
2817 * RETURNS:
2818 * 0 on success, negative errno otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 */
Alan04351822007-03-06 02:37:52 -08002820
Tejun Heo02607312007-08-06 18:36:23 +09002821int ata_do_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822{
Tejun Heo02607312007-08-06 18:36:23 +09002823 struct ata_port *ap = link->ap;
Tejun Heoe8e06192006-04-01 01:38:18 +09002824 struct ata_device *dev;
Tejun Heof58229f2007-08-06 18:36:23 +09002825 int rc = 0, used_dma = 0, found = 0;
Tejun Heo3adcebb2006-05-15 20:57:37 +09002826
Tejun Heoa6d5a512006-03-06 04:31:57 +09002827 /* step 1: calculate xfer_mask */
Tejun Heof58229f2007-08-06 18:36:23 +09002828 ata_link_for_each_dev(dev, link) {
Tejun Heoacf356b2006-03-24 14:07:50 +09002829 unsigned int pio_mask, dma_mask;
Tejun Heoa6d5a512006-03-06 04:31:57 +09002830
Tejun Heoe1211e32006-04-01 01:38:18 +09002831 if (!ata_dev_enabled(dev))
Tejun Heoa6d5a512006-03-06 04:31:57 +09002832 continue;
2833
Tejun Heo3373efd2006-05-15 20:57:53 +09002834 ata_dev_xfermask(dev);
Tejun Heoa6d5a512006-03-06 04:31:57 +09002835
Tejun Heoacf356b2006-03-24 14:07:50 +09002836 pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0);
2837 dma_mask = ata_pack_xfermask(0, dev->mwdma_mask, dev->udma_mask);
2838 dev->pio_mode = ata_xfer_mask2mode(pio_mask);
2839 dev->dma_mode = ata_xfer_mask2mode(dma_mask);
Alan Cox5444a6f2006-03-27 18:58:20 +01002840
Tejun Heo4f659772006-04-01 01:38:18 +09002841 found = 1;
Alan Cox5444a6f2006-03-27 18:58:20 +01002842 if (dev->dma_mode)
2843 used_dma = 1;
Tejun Heoa6d5a512006-03-06 04:31:57 +09002844 }
Tejun Heo4f659772006-04-01 01:38:18 +09002845 if (!found)
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002846 goto out;
Tejun Heoa6d5a512006-03-06 04:31:57 +09002847
2848 /* step 2: always set host PIO timings */
Tejun Heof58229f2007-08-06 18:36:23 +09002849 ata_link_for_each_dev(dev, link) {
Tejun Heoe8e06192006-04-01 01:38:18 +09002850 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 Heof58229f2007-08-06 18:36:23 +09002866 ata_link_for_each_dev(dev, link) {
Tejun Heoe8e06192006-04-01 01:38:18 +09002867 if (!ata_dev_enabled(dev) || !dev->dma_mode)
2868 continue;
2869
2870 dev->xfer_mode = dev->dma_mode;
2871 dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
2872 if (ap->ops->set_dmamode)
2873 ap->ops->set_dmamode(ap, dev);
2874 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875
2876 /* step 4: update devices' xfer mode */
Tejun Heof58229f2007-08-06 18:36:23 +09002877 ata_link_for_each_dev(dev, link) {
Alan18d90de2007-01-24 11:42:38 +00002878 /* don't update suspended devices' xfer mode */
Tejun Heo9666f402007-05-04 21:27:47 +02002879 if (!ata_dev_enabled(dev))
Tejun Heo83206a22006-03-24 15:25:31 +09002880 continue;
2881
Tejun Heo3373efd2006-05-15 20:57:53 +09002882 rc = ata_dev_set_mode(dev);
Tejun Heo5bbc53f2006-04-01 01:38:17 +09002883 if (rc)
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002884 goto out;
Tejun Heo83206a22006-03-24 15:25:31 +09002885 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002886
Tejun Heoe8e06192006-04-01 01:38:18 +09002887 /* Record simplex status. If we selected DMA then the other
2888 * host channels are not permitted to do so.
Alan Cox5444a6f2006-03-27 18:58:20 +01002889 */
Jeff Garzikcca39742006-08-24 03:19:22 -04002890 if (used_dma && (ap->host->flags & ATA_HOST_SIMPLEX))
Alan032af1c2007-03-01 17:36:46 +00002891 ap->host->simplex_claimed = ap;
Alan Cox5444a6f2006-03-27 18:58:20 +01002892
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002893 out:
2894 if (rc)
2895 *r_failed_dev = dev;
2896 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897}
2898
2899/**
Alan04351822007-03-06 02:37:52 -08002900 * ata_set_mode - Program timings and issue SET FEATURES - XFER
Tejun Heo02607312007-08-06 18:36:23 +09002901 * @link: link on which timings will be programmed
Alan04351822007-03-06 02:37:52 -08002902 * @r_failed_dev: out paramter for failed device
2903 *
2904 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.). If
2905 * ata_set_mode() fails, pointer to the failing device is
2906 * returned in @r_failed_dev.
2907 *
2908 * LOCKING:
2909 * PCI/etc. bus probe sem.
2910 *
2911 * RETURNS:
2912 * 0 on success, negative errno otherwise
2913 */
Tejun Heo02607312007-08-06 18:36:23 +09002914int ata_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
Alan04351822007-03-06 02:37:52 -08002915{
Tejun Heo02607312007-08-06 18:36:23 +09002916 struct ata_port *ap = link->ap;
2917
Alan04351822007-03-06 02:37:52 -08002918 /* has private set_mode? */
2919 if (ap->ops->set_mode)
Tejun Heo02607312007-08-06 18:36:23 +09002920 return ap->ops->set_mode(link, r_failed_dev);
2921 return ata_do_set_mode(link, r_failed_dev);
Alan04351822007-03-06 02:37:52 -08002922}
2923
2924/**
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002925 * ata_tf_to_host - issue ATA taskfile to host controller
2926 * @ap: port to which command is being issued
2927 * @tf: ATA taskfile register set
2928 *
2929 * Issues ATA taskfile register set to ATA host controller,
2930 * with proper synchronization with interrupt handler and
2931 * other threads.
2932 *
2933 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002934 * spin_lock_irqsave(host lock)
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002935 */
2936
2937static inline void ata_tf_to_host(struct ata_port *ap,
2938 const struct ata_taskfile *tf)
2939{
2940 ap->ops->tf_load(ap, tf);
2941 ap->ops->exec_command(ap, tf);
2942}
2943
2944/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 * ata_busy_sleep - sleep until BSY clears, or timeout
2946 * @ap: port containing status register to be polled
2947 * @tmout_pat: impatience timeout
2948 * @tmout: overall timeout
2949 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002950 * Sleep until ATA Status register bit BSY clears,
2951 * or a timeout occurs.
2952 *
Tejun Heod1adc1b2006-10-09 18:32:15 +09002953 * LOCKING:
2954 * Kernel thread context (may sleep).
2955 *
2956 * RETURNS:
2957 * 0 on success, -errno otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958 */
Tejun Heod1adc1b2006-10-09 18:32:15 +09002959int ata_busy_sleep(struct ata_port *ap,
2960 unsigned long tmout_pat, unsigned long tmout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961{
2962 unsigned long timer_start, timeout;
2963 u8 status;
2964
2965 status = ata_busy_wait(ap, ATA_BUSY, 300);
2966 timer_start = jiffies;
2967 timeout = timer_start + tmout_pat;
Tejun Heod1adc1b2006-10-09 18:32:15 +09002968 while (status != 0xff && (status & ATA_BUSY) &&
2969 time_before(jiffies, timeout)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 msleep(50);
2971 status = ata_busy_wait(ap, ATA_BUSY, 3);
2972 }
2973
Tejun Heod1adc1b2006-10-09 18:32:15 +09002974 if (status != 0xff && (status & ATA_BUSY))
Tejun Heof15a1da2006-05-15 20:57:56 +09002975 ata_port_printk(ap, KERN_WARNING,
Jeff Garzik35aa7a42006-09-28 06:50:24 -04002976 "port is slow to respond, please be patient "
2977 "(Status 0x%x)\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978
2979 timeout = timer_start + tmout;
Tejun Heod1adc1b2006-10-09 18:32:15 +09002980 while (status != 0xff && (status & ATA_BUSY) &&
2981 time_before(jiffies, timeout)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 msleep(50);
2983 status = ata_chk_status(ap);
2984 }
2985
Tejun Heod1adc1b2006-10-09 18:32:15 +09002986 if (status == 0xff)
2987 return -ENODEV;
2988
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989 if (status & ATA_BUSY) {
Tejun Heof15a1da2006-05-15 20:57:56 +09002990 ata_port_printk(ap, KERN_ERR, "port failed to respond "
Jeff Garzik35aa7a42006-09-28 06:50:24 -04002991 "(%lu secs, Status 0x%x)\n",
2992 tmout / HZ, status);
Tejun Heod1adc1b2006-10-09 18:32:15 +09002993 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 }
2995
2996 return 0;
2997}
2998
Tejun Heod4b2bab2007-02-02 16:50:52 +09002999/**
3000 * ata_wait_ready - sleep until BSY clears, or timeout
3001 * @ap: port containing status register to be polled
3002 * @deadline: deadline jiffies for the operation
3003 *
3004 * Sleep until ATA Status register bit BSY clears, or timeout
3005 * occurs.
3006 *
3007 * LOCKING:
3008 * Kernel thread context (may sleep).
3009 *
3010 * RETURNS:
3011 * 0 on success, -errno otherwise.
3012 */
3013int ata_wait_ready(struct ata_port *ap, unsigned long deadline)
3014{
3015 unsigned long start = jiffies;
3016 int warned = 0;
3017
3018 while (1) {
3019 u8 status = ata_chk_status(ap);
3020 unsigned long now = jiffies;
3021
3022 if (!(status & ATA_BUSY))
3023 return 0;
Tejun Heo936fd732007-08-06 18:36:23 +09003024 if (!ata_link_online(&ap->link) && status == 0xff)
Tejun Heod4b2bab2007-02-02 16:50:52 +09003025 return -ENODEV;
3026 if (time_after(now, deadline))
3027 return -EBUSY;
3028
3029 if (!warned && time_after(now, start + 5 * HZ) &&
3030 (deadline - now > 3 * HZ)) {
3031 ata_port_printk(ap, KERN_WARNING,
3032 "port is slow to respond, please be patient "
3033 "(Status 0x%x)\n", status);
3034 warned = 1;
3035 }
3036
3037 msleep(50);
3038 }
3039}
3040
3041static int ata_bus_post_reset(struct ata_port *ap, unsigned int devmask,
3042 unsigned long deadline)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003043{
3044 struct ata_ioports *ioaddr = &ap->ioaddr;
3045 unsigned int dev0 = devmask & (1 << 0);
3046 unsigned int dev1 = devmask & (1 << 1);
Tejun Heo9b893912007-02-02 16:50:52 +09003047 int rc, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048
3049 /* if device 0 was found in ata_devchk, wait for its
3050 * BSY bit to clear
3051 */
Tejun Heod4b2bab2007-02-02 16:50:52 +09003052 if (dev0) {
3053 rc = ata_wait_ready(ap, deadline);
Tejun Heo9b893912007-02-02 16:50:52 +09003054 if (rc) {
3055 if (rc != -ENODEV)
3056 return rc;
3057 ret = rc;
3058 }
Tejun Heod4b2bab2007-02-02 16:50:52 +09003059 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060
Tejun Heoe141d992007-06-10 14:26:20 +09003061 /* if device 1 was found in ata_devchk, wait for register
3062 * access briefly, then wait for BSY to clear.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 */
Tejun Heoe141d992007-06-10 14:26:20 +09003064 if (dev1) {
3065 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003066
3067 ap->ops->dev_select(ap, 1);
Tejun Heoe141d992007-06-10 14:26:20 +09003068
3069 /* Wait for register access. Some ATAPI devices fail
3070 * to set nsect/lbal after reset, so don't waste too
3071 * much time on it. We're gonna wait for !BSY anyway.
3072 */
3073 for (i = 0; i < 2; i++) {
3074 u8 nsect, lbal;
3075
3076 nsect = ioread8(ioaddr->nsect_addr);
3077 lbal = ioread8(ioaddr->lbal_addr);
3078 if ((nsect == 1) && (lbal == 1))
3079 break;
3080 msleep(50); /* give drive a breather */
3081 }
3082
Tejun Heod4b2bab2007-02-02 16:50:52 +09003083 rc = ata_wait_ready(ap, deadline);
Tejun Heo9b893912007-02-02 16:50:52 +09003084 if (rc) {
3085 if (rc != -ENODEV)
3086 return rc;
3087 ret = rc;
3088 }
Tejun Heod4b2bab2007-02-02 16:50:52 +09003089 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090
3091 /* is all this really necessary? */
3092 ap->ops->dev_select(ap, 0);
3093 if (dev1)
3094 ap->ops->dev_select(ap, 1);
3095 if (dev0)
3096 ap->ops->dev_select(ap, 0);
Tejun Heod4b2bab2007-02-02 16:50:52 +09003097
Tejun Heo9b893912007-02-02 16:50:52 +09003098 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099}
3100
Tejun Heod4b2bab2007-02-02 16:50:52 +09003101static int ata_bus_softreset(struct ata_port *ap, unsigned int devmask,
3102 unsigned long deadline)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103{
3104 struct ata_ioports *ioaddr = &ap->ioaddr;
3105
Tejun Heo44877b42007-02-21 01:06:51 +09003106 DPRINTK("ata%u: bus reset via SRST\n", ap->print_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107
3108 /* software reset. causes dev0 to be selected */
Tejun Heo0d5ff562007-02-01 15:06:36 +09003109 iowrite8(ap->ctl, ioaddr->ctl_addr);
3110 udelay(20); /* FIXME: flush */
3111 iowrite8(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
3112 udelay(20); /* FIXME: flush */
3113 iowrite8(ap->ctl, ioaddr->ctl_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114
3115 /* spec mandates ">= 2ms" before checking status.
3116 * We wait 150ms, because that was the magic delay used for
3117 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
3118 * between when the ATA command register is written, and then
3119 * status is checked. Because waiting for "a while" before
3120 * checking status is fine, post SRST, we perform this magic
3121 * delay here as well.
Alan Cox09c7ad72006-03-22 15:52:40 +00003122 *
3123 * Old drivers/ide uses the 2mS rule and then waits for ready
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 */
3125 msleep(150);
3126
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05003127 /* Before we perform post reset processing we want to see if
Tejun Heo298a41c2006-03-25 02:58:13 +09003128 * the bus shows 0xFF because the odd clown forgets the D7
3129 * pulldown resistor.
3130 */
Tejun Heod1adc1b2006-10-09 18:32:15 +09003131 if (ata_check_status(ap) == 0xFF)
Tejun Heo9b893912007-02-02 16:50:52 +09003132 return -ENODEV;
Alan Cox09c7ad72006-03-22 15:52:40 +00003133
Tejun Heod4b2bab2007-02-02 16:50:52 +09003134 return ata_bus_post_reset(ap, devmask, deadline);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003135}
3136
3137/**
3138 * ata_bus_reset - reset host port and associated ATA channel
3139 * @ap: port to reset
3140 *
3141 * This is typically the first time we actually start issuing
3142 * commands to the ATA channel. We wait for BSY to clear, then
3143 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
3144 * result. Determine what devices, if any, are on the channel
3145 * by looking at the device 0/1 error register. Look at the signature
3146 * stored in each device's taskfile registers, to determine if
3147 * the device is ATA or ATAPI.
3148 *
3149 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003150 * PCI/etc. bus probe sem.
Jeff Garzikcca39742006-08-24 03:19:22 -04003151 * Obtains host lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152 *
3153 * SIDE EFFECTS:
Tejun Heo198e0fe2006-04-02 18:51:52 +09003154 * Sets ATA_FLAG_DISABLED if bus reset fails.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 */
3156
3157void ata_bus_reset(struct ata_port *ap)
3158{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09003159 struct ata_device *device = ap->link.device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 struct ata_ioports *ioaddr = &ap->ioaddr;
3161 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
3162 u8 err;
Tejun Heoaec5c3c2006-03-25 01:33:34 +09003163 unsigned int dev0, dev1 = 0, devmask = 0;
Tejun Heo9b893912007-02-02 16:50:52 +09003164 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165
Tejun Heo44877b42007-02-21 01:06:51 +09003166 DPRINTK("ENTER, host %u, port %u\n", ap->print_id, ap->port_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167
3168 /* determine if device 0/1 are present */
3169 if (ap->flags & ATA_FLAG_SATA_RESET)
3170 dev0 = 1;
3171 else {
3172 dev0 = ata_devchk(ap, 0);
3173 if (slave_possible)
3174 dev1 = ata_devchk(ap, 1);
3175 }
3176
3177 if (dev0)
3178 devmask |= (1 << 0);
3179 if (dev1)
3180 devmask |= (1 << 1);
3181
3182 /* select device 0 again */
3183 ap->ops->dev_select(ap, 0);
3184
3185 /* issue bus reset */
Tejun Heo9b893912007-02-02 16:50:52 +09003186 if (ap->flags & ATA_FLAG_SRST) {
3187 rc = ata_bus_softreset(ap, devmask, jiffies + 40 * HZ);
3188 if (rc && rc != -ENODEV)
Tejun Heoaec5c3c2006-03-25 01:33:34 +09003189 goto err_out;
Tejun Heo9b893912007-02-02 16:50:52 +09003190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191
3192 /*
3193 * determine by signature whether we have ATA or ATAPI devices
3194 */
Tejun Heo9af5c9c2007-08-06 18:36:22 +09003195 device[0].class = ata_dev_try_classify(ap, 0, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 if ((slave_possible) && (err != 0x81))
Tejun Heo9af5c9c2007-08-06 18:36:22 +09003197 device[1].class = ata_dev_try_classify(ap, 1, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 /* is double-select really necessary? */
Tejun Heo9af5c9c2007-08-06 18:36:22 +09003200 if (device[1].class != ATA_DEV_NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 ap->ops->dev_select(ap, 1);
Tejun Heo9af5c9c2007-08-06 18:36:22 +09003202 if (device[0].class != ATA_DEV_NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203 ap->ops->dev_select(ap, 0);
3204
3205 /* if no devices were detected, disable this port */
Tejun Heo9af5c9c2007-08-06 18:36:22 +09003206 if ((device[0].class == ATA_DEV_NONE) &&
3207 (device[1].class == ATA_DEV_NONE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 goto err_out;
3209
3210 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
3211 /* set up device control for ATA_FLAG_SATA_RESET */
Tejun Heo0d5ff562007-02-01 15:06:36 +09003212 iowrite8(ap->ctl, ioaddr->ctl_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 }
3214
3215 DPRINTK("EXIT\n");
3216 return;
3217
3218err_out:
Tejun Heof15a1da2006-05-15 20:57:56 +09003219 ata_port_printk(ap, KERN_ERR, "disabling port\n");
Jeff Garzikac8869d2007-08-16 03:17:03 -04003220 ata_port_disable(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221
3222 DPRINTK("EXIT\n");
3223}
3224
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003225/**
Tejun Heo936fd732007-08-06 18:36:23 +09003226 * sata_link_debounce - debounce SATA phy status
3227 * @link: ATA link to debounce SATA phy status for
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003228 * @params: timing parameters { interval, duratinon, timeout } in msec
Tejun Heod4b2bab2007-02-02 16:50:52 +09003229 * @deadline: deadline jiffies for the operation
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003230 *
Tejun Heo936fd732007-08-06 18:36:23 +09003231* Make sure SStatus of @link reaches stable state, determined by
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003232 * holding the same value where DET is not 1 for @duration polled
3233 * every @interval, before @timeout. Timeout constraints the
Tejun Heod4b2bab2007-02-02 16:50:52 +09003234 * beginning of the stable state. Because DET gets stuck at 1 on
3235 * some controllers after hot unplugging, this functions waits
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003236 * until timeout then returns 0 if DET is stable at 1.
3237 *
Tejun Heod4b2bab2007-02-02 16:50:52 +09003238 * @timeout is further limited by @deadline. The sooner of the
3239 * two is used.
3240 *
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003241 * LOCKING:
3242 * Kernel thread context (may sleep)
3243 *
3244 * RETURNS:
3245 * 0 on success, -errno on failure.
3246 */
Tejun Heo936fd732007-08-06 18:36:23 +09003247int sata_link_debounce(struct ata_link *link, const unsigned long *params,
3248 unsigned long deadline)
Tejun Heo7a7921e2006-02-02 18:20:00 +09003249{
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003250 unsigned long interval_msec = params[0];
Tejun Heod4b2bab2007-02-02 16:50:52 +09003251 unsigned long duration = msecs_to_jiffies(params[1]);
3252 unsigned long last_jiffies, t;
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003253 u32 last, cur;
3254 int rc;
3255
Tejun Heod4b2bab2007-02-02 16:50:52 +09003256 t = jiffies + msecs_to_jiffies(params[2]);
3257 if (time_before(t, deadline))
3258 deadline = t;
3259
Tejun Heo936fd732007-08-06 18:36:23 +09003260 if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003261 return rc;
3262 cur &= 0xf;
3263
3264 last = cur;
3265 last_jiffies = jiffies;
3266
3267 while (1) {
3268 msleep(interval_msec);
Tejun Heo936fd732007-08-06 18:36:23 +09003269 if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003270 return rc;
3271 cur &= 0xf;
3272
3273 /* DET stable? */
3274 if (cur == last) {
Tejun Heod4b2bab2007-02-02 16:50:52 +09003275 if (cur == 1 && time_before(jiffies, deadline))
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003276 continue;
3277 if (time_after(jiffies, last_jiffies + duration))
3278 return 0;
3279 continue;
3280 }
3281
3282 /* unstable, start over */
3283 last = cur;
3284 last_jiffies = jiffies;
3285
Tejun Heof1545152007-07-16 14:29:40 +09003286 /* Check deadline. If debouncing failed, return
3287 * -EPIPE to tell upper layer to lower link speed.
3288 */
Tejun Heod4b2bab2007-02-02 16:50:52 +09003289 if (time_after(jiffies, deadline))
Tejun Heof1545152007-07-16 14:29:40 +09003290 return -EPIPE;
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003291 }
3292}
3293
3294/**
Tejun Heo936fd732007-08-06 18:36:23 +09003295 * sata_link_resume - resume SATA link
3296 * @link: ATA link to resume SATA
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003297 * @params: timing parameters { interval, duratinon, timeout } in msec
Tejun Heod4b2bab2007-02-02 16:50:52 +09003298 * @deadline: deadline jiffies for the operation
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003299 *
Tejun Heo936fd732007-08-06 18:36:23 +09003300 * Resume SATA phy @link and debounce it.
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003301 *
3302 * LOCKING:
3303 * Kernel thread context (may sleep)
3304 *
3305 * RETURNS:
3306 * 0 on success, -errno on failure.
3307 */
Tejun Heo936fd732007-08-06 18:36:23 +09003308int sata_link_resume(struct ata_link *link, const unsigned long *params,
3309 unsigned long deadline)
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003310{
3311 u32 scontrol;
Tejun Heo81952c52006-05-15 20:57:47 +09003312 int rc;
Tejun Heo7a7921e2006-02-02 18:20:00 +09003313
Tejun Heo936fd732007-08-06 18:36:23 +09003314 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
Tejun Heo81952c52006-05-15 20:57:47 +09003315 return rc;
3316
Tejun Heo852ee162006-04-01 01:38:18 +09003317 scontrol = (scontrol & 0x0f0) | 0x300;
Tejun Heo81952c52006-05-15 20:57:47 +09003318
Tejun Heo936fd732007-08-06 18:36:23 +09003319 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
Tejun Heo81952c52006-05-15 20:57:47 +09003320 return rc;
Tejun Heo7a7921e2006-02-02 18:20:00 +09003321
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003322 /* Some PHYs react badly if SStatus is pounded immediately
3323 * after resuming. Delay 200ms before debouncing.
3324 */
3325 msleep(200);
Tejun Heo7a7921e2006-02-02 18:20:00 +09003326
Tejun Heo936fd732007-08-06 18:36:23 +09003327 return sata_link_debounce(link, params, deadline);
Tejun Heo7a7921e2006-02-02 18:20:00 +09003328}
3329
Tejun Heof5914a42006-05-31 18:27:48 +09003330/**
3331 * ata_std_prereset - prepare for reset
Tejun Heocc0680a2007-08-06 18:36:23 +09003332 * @link: ATA link to be reset
Tejun Heod4b2bab2007-02-02 16:50:52 +09003333 * @deadline: deadline jiffies for the operation
Tejun Heof5914a42006-05-31 18:27:48 +09003334 *
Tejun Heocc0680a2007-08-06 18:36:23 +09003335 * @link is about to be reset. Initialize it. Failure from
Tejun Heob8cffc62007-02-02 16:50:52 +09003336 * prereset makes libata abort whole reset sequence and give up
3337 * that port, so prereset should be best-effort. It does its
3338 * best to prepare for reset sequence but if things go wrong, it
3339 * should just whine, not fail.
Tejun Heof5914a42006-05-31 18:27:48 +09003340 *
3341 * LOCKING:
3342 * Kernel thread context (may sleep)
3343 *
3344 * RETURNS:
3345 * 0 on success, -errno otherwise.
3346 */
Tejun Heocc0680a2007-08-06 18:36:23 +09003347int ata_std_prereset(struct ata_link *link, unsigned long deadline)
Tejun Heof5914a42006-05-31 18:27:48 +09003348{
Tejun Heocc0680a2007-08-06 18:36:23 +09003349 struct ata_port *ap = link->ap;
Tejun Heo936fd732007-08-06 18:36:23 +09003350 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heoe9c83912006-07-03 16:07:26 +09003351 const unsigned long *timing = sata_ehc_deb_timing(ehc);
Tejun Heof5914a42006-05-31 18:27:48 +09003352 int rc;
3353
Tejun Heo31daabd2007-02-02 16:50:52 +09003354 /* handle link resume */
Tejun Heo28324302006-07-03 16:07:26 +09003355 if ((ehc->i.flags & ATA_EHI_RESUME_LINK) &&
Tejun Heo0c887582007-08-06 18:36:23 +09003356 (link->flags & ATA_LFLAG_HRST_TO_RESUME))
Tejun Heo28324302006-07-03 16:07:26 +09003357 ehc->i.action |= ATA_EH_HARDRESET;
3358
Tejun Heof5914a42006-05-31 18:27:48 +09003359 /* if we're about to do hardreset, nothing more to do */
3360 if (ehc->i.action & ATA_EH_HARDRESET)
3361 return 0;
3362
Tejun Heo936fd732007-08-06 18:36:23 +09003363 /* if SATA, resume link */
Tejun Heoa16abc02007-05-21 18:33:47 +02003364 if (ap->flags & ATA_FLAG_SATA) {
Tejun Heo936fd732007-08-06 18:36:23 +09003365 rc = sata_link_resume(link, timing, deadline);
Tejun Heob8cffc62007-02-02 16:50:52 +09003366 /* whine about phy resume failure but proceed */
3367 if (rc && rc != -EOPNOTSUPP)
Tejun Heocc0680a2007-08-06 18:36:23 +09003368 ata_link_printk(link, KERN_WARNING, "failed to resume "
Tejun Heof5914a42006-05-31 18:27:48 +09003369 "link for reset (errno=%d)\n", rc);
Tejun Heof5914a42006-05-31 18:27:48 +09003370 }
3371
3372 /* Wait for !BSY if the controller can wait for the first D2H
3373 * Reg FIS and we don't know that no device is attached.
3374 */
Tejun Heo0c887582007-08-06 18:36:23 +09003375 if (!(link->flags & ATA_LFLAG_SKIP_D2H_BSY) && !ata_link_offline(link)) {
Tejun Heob8cffc62007-02-02 16:50:52 +09003376 rc = ata_wait_ready(ap, deadline);
Tejun Heo6dffaf62007-05-23 11:58:52 +02003377 if (rc && rc != -ENODEV) {
Tejun Heocc0680a2007-08-06 18:36:23 +09003378 ata_link_printk(link, KERN_WARNING, "device not ready "
Tejun Heob8cffc62007-02-02 16:50:52 +09003379 "(errno=%d), forcing hardreset\n", rc);
3380 ehc->i.action |= ATA_EH_HARDRESET;
3381 }
3382 }
Tejun Heof5914a42006-05-31 18:27:48 +09003383
3384 return 0;
3385}
3386
Tejun Heoc2bd5802006-01-24 17:05:22 +09003387/**
3388 * ata_std_softreset - reset host port via ATA SRST
Tejun Heocc0680a2007-08-06 18:36:23 +09003389 * @link: ATA link to reset
Tejun Heoc2bd5802006-01-24 17:05:22 +09003390 * @classes: resulting classes of attached devices
Tejun Heod4b2bab2007-02-02 16:50:52 +09003391 * @deadline: deadline jiffies for the operation
Tejun Heoc2bd5802006-01-24 17:05:22 +09003392 *
Tejun Heo52783c52006-05-31 18:28:22 +09003393 * Reset host port using ATA SRST.
Tejun Heoc2bd5802006-01-24 17:05:22 +09003394 *
3395 * LOCKING:
3396 * Kernel thread context (may sleep)
3397 *
3398 * RETURNS:
3399 * 0 on success, -errno otherwise.
3400 */
Tejun Heocc0680a2007-08-06 18:36:23 +09003401int ata_std_softreset(struct ata_link *link, unsigned int *classes,
Tejun Heod4b2bab2007-02-02 16:50:52 +09003402 unsigned long deadline)
Tejun Heoc2bd5802006-01-24 17:05:22 +09003403{
Tejun Heocc0680a2007-08-06 18:36:23 +09003404 struct ata_port *ap = link->ap;
Tejun Heoc2bd5802006-01-24 17:05:22 +09003405 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 Heo936fd732007-08-06 18:36:23 +09003412 if (ata_link_offline(link)) {
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 */
Tejun Heo936fd732007-08-06 18:36:23 +09003430 if (rc && (rc != -ENODEV || sata_scr_valid(link))) {
Tejun Heocc0680a2007-08-06 18:36:23 +09003431 ata_link_printk(link, KERN_ERR, "SRST failed (errno=%d)\n", rc);
Tejun Heod4b2bab2007-02-02 16:50:52 +09003432 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 Heocc0680a2007-08-06 18:36:23 +09003446 * sata_link_hardreset - reset link via SATA phy reset
3447 * @link: link 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 *
Tejun Heocc0680a2007-08-06 18:36:23 +09003451 * SATA phy-reset @link 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 Heocc0680a2007-08-06 18:36:23 +09003459int sata_link_hardreset(struct ata_link *link, const unsigned long *timing,
Tejun Heod4b2bab2007-02-02 16:50:52 +09003460 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 Heo936fd732007-08-06 18:36:23 +09003467 if (sata_set_spd_needed(link)) {
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 Heo936fd732007-08-06 18:36:23 +09003473 if ((rc = sata_scr_read(link, 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
Tejun Heo936fd732007-08-06 18:36:23 +09003478 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
Tejun Heob6103f62006-11-01 17:59:53 +09003479 goto out;
Tejun Heo1c3fae42006-04-02 20:53:28 +09003480
Tejun Heo936fd732007-08-06 18:36:23 +09003481 sata_set_spd(link);
Tejun Heo1c3fae42006-04-02 20:53:28 +09003482 }
3483
3484 /* issue phy wake/reset */
Tejun Heo936fd732007-08-06 18:36:23 +09003485 if ((rc = sata_scr_read(link, 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
Tejun Heo936fd732007-08-06 18:36:23 +09003490 if ((rc = sata_scr_write_flush(link, 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 Heo936fd732007-08-06 18:36:23 +09003498 /* bring link back */
3499 rc = sata_link_resume(link, 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
Tejun Heocc0680a2007-08-06 18:36:23 +09003507 * @link: link to reset
Tejun Heob6103f62006-11-01 17:59:53 +09003508 * @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 Heocc0680a2007-08-06 18:36:23 +09003520int sata_std_hardreset(struct ata_link *link, unsigned int *class,
Tejun Heod4b2bab2007-02-02 16:50:52 +09003521 unsigned long deadline)
Tejun Heob6103f62006-11-01 17:59:53 +09003522{
Tejun Heocc0680a2007-08-06 18:36:23 +09003523 struct ata_port *ap = link->ap;
Tejun Heo936fd732007-08-06 18:36:23 +09003524 const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
Tejun Heob6103f62006-11-01 17:59:53 +09003525 int rc;
3526
3527 DPRINTK("ENTER\n");
3528
3529 /* do hardreset */
Tejun Heocc0680a2007-08-06 18:36:23 +09003530 rc = sata_link_hardreset(link, timing, deadline);
Tejun Heob6103f62006-11-01 17:59:53 +09003531 if (rc) {
Tejun Heocc0680a2007-08-06 18:36:23 +09003532 ata_link_printk(link, KERN_ERR,
Tejun Heob6103f62006-11-01 17:59:53 +09003533 "COMRESET failed (errno=%d)\n", rc);
3534 return rc;
3535 }
Tejun Heoc2bd5802006-01-24 17:05:22 +09003536
Tejun Heoc2bd5802006-01-24 17:05:22 +09003537 /* TODO: phy layer with polling, timeouts, etc. */
Tejun Heo936fd732007-08-06 18:36:23 +09003538 if (ata_link_offline(link)) {
Tejun Heoc2bd5802006-01-24 17:05:22 +09003539 *class = ATA_DEV_NONE;
3540 DPRINTK("EXIT, link offline\n");
3541 return 0;
3542 }
3543
Tejun Heo34fee222007-02-02 15:29:27 +09003544 /* wait a while before checking status, see SRST for more info */
3545 msleep(150);
3546
Tejun Heod4b2bab2007-02-02 16:50:52 +09003547 rc = ata_wait_ready(ap, deadline);
Tejun Heo9b893912007-02-02 16:50:52 +09003548 /* link occupied, -ENODEV too is an error */
3549 if (rc) {
Tejun Heocc0680a2007-08-06 18:36:23 +09003550 ata_link_printk(link, KERN_ERR,
Tejun Heod4b2bab2007-02-02 16:50:52 +09003551 "COMRESET failed (errno=%d)\n", rc);
3552 return rc;
Tejun Heoc2bd5802006-01-24 17:05:22 +09003553 }
3554
Tejun Heo3a397462006-02-10 23:58:48 +09003555 ap->ops->dev_select(ap, 0); /* probably unnecessary */
3556
Tejun Heoc2bd5802006-01-24 17:05:22 +09003557 *class = ata_dev_try_classify(ap, 0, NULL);
3558
3559 DPRINTK("EXIT, class=%u\n", *class);
3560 return 0;
3561}
3562
3563/**
3564 * ata_std_postreset - standard postreset callback
Tejun Heocc0680a2007-08-06 18:36:23 +09003565 * @link: the target ata_link
Tejun Heoc2bd5802006-01-24 17:05:22 +09003566 * @classes: classes of attached devices
3567 *
3568 * This function is invoked after a successful reset. Note that
3569 * the device might have been reset more than once using
3570 * different reset methods before postreset is invoked.
Tejun Heoc2bd5802006-01-24 17:05:22 +09003571 *
Tejun Heoc2bd5802006-01-24 17:05:22 +09003572 * LOCKING:
3573 * Kernel thread context (may sleep)
3574 */
Tejun Heocc0680a2007-08-06 18:36:23 +09003575void ata_std_postreset(struct ata_link *link, unsigned int *classes)
Tejun Heoc2bd5802006-01-24 17:05:22 +09003576{
Tejun Heocc0680a2007-08-06 18:36:23 +09003577 struct ata_port *ap = link->ap;
Tejun Heodc2b3512006-05-15 20:58:00 +09003578 u32 serror;
3579
Tejun Heoc2bd5802006-01-24 17:05:22 +09003580 DPRINTK("ENTER\n");
3581
Tejun Heoc2bd5802006-01-24 17:05:22 +09003582 /* print link status */
Tejun Heo936fd732007-08-06 18:36:23 +09003583 sata_print_link_status(link);
Tejun Heoc2bd5802006-01-24 17:05:22 +09003584
Tejun Heodc2b3512006-05-15 20:58:00 +09003585 /* clear SError */
Tejun Heo936fd732007-08-06 18:36:23 +09003586 if (sata_scr_read(link, SCR_ERROR, &serror) == 0)
3587 sata_scr_write(link, SCR_ERROR, serror);
Tejun Heodc2b3512006-05-15 20:58:00 +09003588
Tejun Heoc2bd5802006-01-24 17:05:22 +09003589 /* is double-select really necessary? */
3590 if (classes[0] != ATA_DEV_NONE)
3591 ap->ops->dev_select(ap, 1);
3592 if (classes[1] != ATA_DEV_NONE)
3593 ap->ops->dev_select(ap, 0);
3594
Tejun Heo3a397462006-02-10 23:58:48 +09003595 /* bail out if no device is present */
3596 if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
3597 DPRINTK("EXIT, no device\n");
3598 return;
3599 }
3600
3601 /* set up device control */
Tejun Heo0d5ff562007-02-01 15:06:36 +09003602 if (ap->ioaddr.ctl_addr)
3603 iowrite8(ap->ctl, ap->ioaddr.ctl_addr);
Tejun Heoc2bd5802006-01-24 17:05:22 +09003604
3605 DPRINTK("EXIT\n");
3606}
3607
Tejun Heoa62c0fc2006-01-24 17:05:22 +09003608/**
Tejun Heo623a3122006-03-05 17:55:58 +09003609 * ata_dev_same_device - Determine whether new ID matches configured device
Tejun Heo623a3122006-03-05 17:55:58 +09003610 * @dev: device to compare against
3611 * @new_class: class of the new device
3612 * @new_id: IDENTIFY page of the new device
3613 *
3614 * Compare @new_class and @new_id against @dev and determine
3615 * whether @dev is the device indicated by @new_class and
3616 * @new_id.
3617 *
3618 * LOCKING:
3619 * None.
3620 *
3621 * RETURNS:
3622 * 1 if @dev matches @new_class and @new_id, 0 otherwise.
3623 */
Tejun Heo3373efd2006-05-15 20:57:53 +09003624static int ata_dev_same_device(struct ata_device *dev, unsigned int new_class,
3625 const u16 *new_id)
Tejun Heo623a3122006-03-05 17:55:58 +09003626{
3627 const u16 *old_id = dev->id;
Tejun Heoa0cf7332007-01-02 20:18:49 +09003628 unsigned char model[2][ATA_ID_PROD_LEN + 1];
3629 unsigned char serial[2][ATA_ID_SERNO_LEN + 1];
Tejun Heo623a3122006-03-05 17:55:58 +09003630
3631 if (dev->class != new_class) {
Tejun Heof15a1da2006-05-15 20:57:56 +09003632 ata_dev_printk(dev, KERN_INFO, "class mismatch %d != %d\n",
3633 dev->class, new_class);
Tejun Heo623a3122006-03-05 17:55:58 +09003634 return 0;
3635 }
3636
Tejun Heoa0cf7332007-01-02 20:18:49 +09003637 ata_id_c_string(old_id, model[0], ATA_ID_PROD, sizeof(model[0]));
3638 ata_id_c_string(new_id, model[1], ATA_ID_PROD, sizeof(model[1]));
3639 ata_id_c_string(old_id, serial[0], ATA_ID_SERNO, sizeof(serial[0]));
3640 ata_id_c_string(new_id, serial[1], ATA_ID_SERNO, sizeof(serial[1]));
Tejun Heo623a3122006-03-05 17:55:58 +09003641
3642 if (strcmp(model[0], model[1])) {
Tejun Heof15a1da2006-05-15 20:57:56 +09003643 ata_dev_printk(dev, KERN_INFO, "model number mismatch "
3644 "'%s' != '%s'\n", model[0], model[1]);
Tejun Heo623a3122006-03-05 17:55:58 +09003645 return 0;
3646 }
3647
3648 if (strcmp(serial[0], serial[1])) {
Tejun Heof15a1da2006-05-15 20:57:56 +09003649 ata_dev_printk(dev, KERN_INFO, "serial number mismatch "
3650 "'%s' != '%s'\n", serial[0], serial[1]);
Tejun Heo623a3122006-03-05 17:55:58 +09003651 return 0;
3652 }
3653
Tejun Heo623a3122006-03-05 17:55:58 +09003654 return 1;
3655}
3656
3657/**
Tejun Heofe309112007-05-15 03:28:15 +09003658 * ata_dev_reread_id - Re-read IDENTIFY data
Henrik Kretzschmar3fae4502007-06-19 10:10:50 +02003659 * @dev: target ATA device
Tejun Heobff04642006-11-10 18:08:10 +09003660 * @readid_flags: read ID flags
Tejun Heo623a3122006-03-05 17:55:58 +09003661 *
3662 * Re-read IDENTIFY page and make sure @dev is still attached to
3663 * the port.
3664 *
3665 * LOCKING:
3666 * Kernel thread context (may sleep)
3667 *
3668 * RETURNS:
3669 * 0 on success, negative errno otherwise
3670 */
Tejun Heofe309112007-05-15 03:28:15 +09003671int ata_dev_reread_id(struct ata_device *dev, unsigned int readid_flags)
Tejun Heo623a3122006-03-05 17:55:58 +09003672{
Tejun Heo5eb45c02006-04-02 18:51:52 +09003673 unsigned int class = dev->class;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09003674 u16 *id = (void *)dev->link->ap->sector_buf;
Tejun Heo623a3122006-03-05 17:55:58 +09003675 int rc;
3676
Tejun Heofe635c72006-05-15 20:57:35 +09003677 /* read ID data */
Tejun Heobff04642006-11-10 18:08:10 +09003678 rc = ata_dev_read_id(dev, &class, readid_flags, id);
Tejun Heo623a3122006-03-05 17:55:58 +09003679 if (rc)
Tejun Heofe309112007-05-15 03:28:15 +09003680 return rc;
Tejun Heo623a3122006-03-05 17:55:58 +09003681
3682 /* is the device still there? */
Tejun Heofe309112007-05-15 03:28:15 +09003683 if (!ata_dev_same_device(dev, class, id))
3684 return -ENODEV;
Tejun Heo623a3122006-03-05 17:55:58 +09003685
Tejun Heofe635c72006-05-15 20:57:35 +09003686 memcpy(dev->id, id, sizeof(id[0]) * ATA_ID_WORDS);
Tejun Heofe309112007-05-15 03:28:15 +09003687 return 0;
3688}
3689
3690/**
3691 * ata_dev_revalidate - Revalidate ATA device
3692 * @dev: device to revalidate
3693 * @readid_flags: read ID flags
3694 *
3695 * Re-read IDENTIFY page, make sure @dev is still attached to the
3696 * port and reconfigure it according to the new IDENTIFY page.
3697 *
3698 * LOCKING:
3699 * Kernel thread context (may sleep)
3700 *
3701 * RETURNS:
3702 * 0 on success, negative errno otherwise
3703 */
3704int ata_dev_revalidate(struct ata_device *dev, unsigned int readid_flags)
3705{
Tejun Heo6ddcd3b2007-05-15 03:28:15 +09003706 u64 n_sectors = dev->n_sectors;
Tejun Heofe309112007-05-15 03:28:15 +09003707 int rc;
3708
3709 if (!ata_dev_enabled(dev))
3710 return -ENODEV;
3711
3712 /* re-read ID */
3713 rc = ata_dev_reread_id(dev, readid_flags);
3714 if (rc)
3715 goto fail;
Tejun Heo623a3122006-03-05 17:55:58 +09003716
3717 /* configure device according to the new ID */
Tejun Heoefdaedc2006-11-01 18:38:52 +09003718 rc = ata_dev_configure(dev);
Tejun Heo6ddcd3b2007-05-15 03:28:15 +09003719 if (rc)
3720 goto fail;
3721
3722 /* verify n_sectors hasn't changed */
Tejun Heob54eebd2007-08-17 18:46:51 +09003723 if (dev->class == ATA_DEV_ATA && n_sectors &&
3724 dev->n_sectors != n_sectors) {
Tejun Heo6ddcd3b2007-05-15 03:28:15 +09003725 ata_dev_printk(dev, KERN_INFO, "n_sectors mismatch "
3726 "%llu != %llu\n",
3727 (unsigned long long)n_sectors,
3728 (unsigned long long)dev->n_sectors);
Tejun Heo8270bec2007-08-16 03:02:22 +09003729
3730 /* restore original n_sectors */
3731 dev->n_sectors = n_sectors;
3732
Tejun Heo6ddcd3b2007-05-15 03:28:15 +09003733 rc = -ENODEV;
3734 goto fail;
3735 }
3736
3737 return 0;
Tejun Heo623a3122006-03-05 17:55:58 +09003738
3739 fail:
Tejun Heof15a1da2006-05-15 20:57:56 +09003740 ata_dev_printk(dev, KERN_ERR, "revalidation failed (errno=%d)\n", rc);
Tejun Heo623a3122006-03-05 17:55:58 +09003741 return rc;
3742}
3743
Alan Cox6919a0a2006-10-27 19:08:46 -07003744struct ata_blacklist_entry {
3745 const char *model_num;
3746 const char *model_rev;
3747 unsigned long horkage;
3748};
3749
3750static const struct ata_blacklist_entry ata_device_blacklist [] = {
3751 /* Devices with DMA related problems under Linux */
3752 { "WDC AC11000H", NULL, ATA_HORKAGE_NODMA },
3753 { "WDC AC22100H", NULL, ATA_HORKAGE_NODMA },
3754 { "WDC AC32500H", NULL, ATA_HORKAGE_NODMA },
3755 { "WDC AC33100H", NULL, ATA_HORKAGE_NODMA },
3756 { "WDC AC31600H", NULL, ATA_HORKAGE_NODMA },
3757 { "WDC AC32100H", "24.09P07", ATA_HORKAGE_NODMA },
3758 { "WDC AC23200L", "21.10N21", ATA_HORKAGE_NODMA },
3759 { "Compaq CRD-8241B", NULL, ATA_HORKAGE_NODMA },
3760 { "CRD-8400B", NULL, ATA_HORKAGE_NODMA },
3761 { "CRD-8480B", NULL, ATA_HORKAGE_NODMA },
3762 { "CRD-8482B", NULL, ATA_HORKAGE_NODMA },
3763 { "CRD-84", NULL, ATA_HORKAGE_NODMA },
3764 { "SanDisk SDP3B", NULL, ATA_HORKAGE_NODMA },
3765 { "SanDisk SDP3B-64", NULL, ATA_HORKAGE_NODMA },
3766 { "SANYO CD-ROM CRD", NULL, ATA_HORKAGE_NODMA },
3767 { "HITACHI CDR-8", NULL, ATA_HORKAGE_NODMA },
3768 { "HITACHI CDR-8335", NULL, ATA_HORKAGE_NODMA },
3769 { "HITACHI CDR-8435", NULL, ATA_HORKAGE_NODMA },
3770 { "Toshiba CD-ROM XM-6202B", NULL, ATA_HORKAGE_NODMA },
3771 { "TOSHIBA CD-ROM XM-1702BC", NULL, ATA_HORKAGE_NODMA },
3772 { "CD-532E-A", NULL, ATA_HORKAGE_NODMA },
3773 { "E-IDE CD-ROM CR-840",NULL, ATA_HORKAGE_NODMA },
3774 { "CD-ROM Drive/F5A", NULL, ATA_HORKAGE_NODMA },
3775 { "WPI CDD-820", NULL, ATA_HORKAGE_NODMA },
3776 { "SAMSUNG CD-ROM SC-148C", NULL, ATA_HORKAGE_NODMA },
3777 { "SAMSUNG CD-ROM SC", NULL, ATA_HORKAGE_NODMA },
Alan Cox6919a0a2006-10-27 19:08:46 -07003778 { "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,ATA_HORKAGE_NODMA },
3779 { "_NEC DV5800A", NULL, ATA_HORKAGE_NODMA },
3780 { "SAMSUNG CD-ROM SN-124","N001", ATA_HORKAGE_NODMA },
Dave Jones39f19882007-05-21 14:31:03 -04003781 { "Seagate STT20000A", NULL, ATA_HORKAGE_NODMA },
Tejun Heo5acd50f2007-06-10 14:52:36 +09003782 { "IOMEGA ZIP 250 ATAPI", NULL, ATA_HORKAGE_NODMA }, /* temporary fix */
Tejun Heo39ce7122007-07-05 12:31:31 +09003783 { "IOMEGA ZIP 250 ATAPI Floppy",
3784 NULL, ATA_HORKAGE_NODMA },
Alan Cox6919a0a2006-10-27 19:08:46 -07003785
Albert Lee18d6e9d2007-04-02 11:34:15 +08003786 /* Weird ATAPI devices */
Tejun Heo40a1d532007-06-27 02:49:38 +09003787 { "TORiSAN DVD-ROM DRD-N216", NULL, ATA_HORKAGE_MAX_SEC_128 },
Albert Lee18d6e9d2007-04-02 11:34:15 +08003788
Alan Cox6919a0a2006-10-27 19:08:46 -07003789 /* Devices we expect to fail diagnostics */
3790
3791 /* Devices where NCQ should be avoided */
3792 /* NCQ is slow */
3793 { "WDC WD740ADFD-00", NULL, ATA_HORKAGE_NONCQ },
Tejun Heo09125ea2007-02-28 15:21:23 +09003794 /* http://thread.gmane.org/gmane.linux.ide/14907 */
3795 { "FUJITSU MHT2060BH", NULL, ATA_HORKAGE_NONCQ },
Paul Rolland7acfaf32007-03-26 21:43:44 -08003796 /* NCQ is broken */
Jeff Garzik539cc7c2007-09-20 16:31:47 -04003797 { "Maxtor *", "BANC*", ATA_HORKAGE_NONCQ },
Alan Cox0e3dbc02007-09-20 15:22:47 +01003798 { "Maxtor 7V300F0", "VA111630", ATA_HORKAGE_NONCQ },
Prarit Bhargava2f8d90a2007-07-10 18:13:28 -04003799 { "HITACHI HDS7250SASUN500G 0621KTAWSD", "K2AOAJ0AHITACHI",
Jeff Garzik539cc7c2007-09-20 16:31:47 -04003800 ATA_HORKAGE_NONCQ },
3801
Robert Hancock36e337d2007-04-02 22:05:29 -06003802 /* Blacklist entries taken from Silicon Image 3124/3132
3803 Windows driver .inf file - also several Linux problem reports */
3804 { "HTS541060G9SA00", "MB3OC60D", ATA_HORKAGE_NONCQ, },
3805 { "HTS541080G9SA00", "MB4OC60D", ATA_HORKAGE_NONCQ, },
3806 { "HTS541010G9SA00", "MBZOC60D", ATA_HORKAGE_NONCQ, },
Tejun Heobd9c5a32007-06-08 22:20:59 +09003807 /* Drives which do spurious command completion */
3808 { "HTS541680J9SA00", "SB2IC7EP", ATA_HORKAGE_NONCQ, },
Tejun Heo2f8fceb2007-06-18 14:38:41 +09003809 { "HTS541612J9SA00", "SBDIC7JP", ATA_HORKAGE_NONCQ, },
Tejun Heoe14cbfa2007-06-25 11:28:59 +09003810 { "Hitachi HTS541616J9SA00", "SB4OC70P", ATA_HORKAGE_NONCQ, },
Tejun Heo2f8fceb2007-06-18 14:38:41 +09003811 { "WDC WD740ADFD-00NLR1", NULL, ATA_HORKAGE_NONCQ, },
Tejun Heoa520f262007-07-10 16:16:18 +09003812 { "FUJITSU MHV2080BH", "00840028", ATA_HORKAGE_NONCQ, },
Tejun Heo3fb65892007-07-20 12:49:38 +09003813 { "ST9160821AS", "3.CLF", ATA_HORKAGE_NONCQ, },
Alan Cox0e3dbc02007-09-20 15:22:47 +01003814 { "ST3160812AS", "3.AD", ATA_HORKAGE_NONCQ, },
Tejun Heo5d6aca82007-07-28 16:25:25 +09003815 { "SAMSUNG HD401LJ", "ZZ100-15", ATA_HORKAGE_NONCQ, },
Alan Cox6919a0a2006-10-27 19:08:46 -07003816
Tejun Heo16c55b02007-08-29 11:58:33 +09003817 /* devices which puke on READ_NATIVE_MAX */
3818 { "HDS724040KLSA80", "KFAOA20N", ATA_HORKAGE_BROKEN_HPA, },
3819 { "WDC WD3200JD-00KLB0", "WD-WCAMR1130137", ATA_HORKAGE_BROKEN_HPA },
3820 { "WDC WD2500JD-00HBB0", "WD-WMAL71490727", ATA_HORKAGE_BROKEN_HPA },
3821 { "MAXTOR 6L080L4", "A93.0500", ATA_HORKAGE_BROKEN_HPA },
Alan Cox6919a0a2006-10-27 19:08:46 -07003822
3823 /* End Marker */
3824 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003825};
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05003826
Jeff Garzik539cc7c2007-09-20 16:31:47 -04003827int strn_pattern_cmp(const char *patt, const char *name, int wildchar)
3828{
3829 const char *p;
3830 int len;
3831
3832 /*
3833 * check for trailing wildcard: *\0
3834 */
3835 p = strchr(patt, wildchar);
3836 if (p && ((*(p + 1)) == 0))
3837 len = p - patt;
3838 else
3839 len = strlen(name);
3840
3841 return strncmp(patt, name, len);
3842}
3843
Tejun Heo75683fe2007-07-05 13:31:27 +09003844static unsigned long ata_dev_blacklisted(const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003845{
Tejun Heo8bfa79f2007-01-02 20:19:40 +09003846 unsigned char model_num[ATA_ID_PROD_LEN + 1];
3847 unsigned char model_rev[ATA_ID_FW_REV_LEN + 1];
Alan Cox6919a0a2006-10-27 19:08:46 -07003848 const struct ata_blacklist_entry *ad = ata_device_blacklist;
Albert Lee3a778272006-06-22 13:00:25 +08003849
Tejun Heo8bfa79f2007-01-02 20:19:40 +09003850 ata_id_c_string(dev->id, model_num, ATA_ID_PROD, sizeof(model_num));
3851 ata_id_c_string(dev->id, model_rev, ATA_ID_FW_REV, sizeof(model_rev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003852
Alan Cox6919a0a2006-10-27 19:08:46 -07003853 while (ad->model_num) {
Jeff Garzik539cc7c2007-09-20 16:31:47 -04003854 if (!strn_pattern_cmp(ad->model_num, model_num, '*')) {
Alan Cox6919a0a2006-10-27 19:08:46 -07003855 if (ad->model_rev == NULL)
3856 return ad->horkage;
Jeff Garzik539cc7c2007-09-20 16:31:47 -04003857 if (!strn_pattern_cmp(ad->model_rev, model_rev, '*'))
Alan Cox6919a0a2006-10-27 19:08:46 -07003858 return ad->horkage;
Alan Coxf4b15fe2006-03-22 15:54:04 +00003859 }
Alan Cox6919a0a2006-10-27 19:08:46 -07003860 ad++;
Alan Coxf4b15fe2006-03-22 15:54:04 +00003861 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003862 return 0;
3863}
3864
Alan Cox6919a0a2006-10-27 19:08:46 -07003865static int ata_dma_blacklisted(const struct ata_device *dev)
3866{
3867 /* We don't support polling DMA.
3868 * DMA blacklist those ATAPI devices with CDB-intr (and use PIO)
3869 * if the LLDD handles only interrupts in the HSM_ST_LAST state.
3870 */
Tejun Heo9af5c9c2007-08-06 18:36:22 +09003871 if ((dev->link->ap->flags & ATA_FLAG_PIO_POLLING) &&
Alan Cox6919a0a2006-10-27 19:08:46 -07003872 (dev->flags & ATA_DFLAG_CDB_INTR))
3873 return 1;
Tejun Heo75683fe2007-07-05 13:31:27 +09003874 return (dev->horkage & ATA_HORKAGE_NODMA) ? 1 : 0;
Alan Cox6919a0a2006-10-27 19:08:46 -07003875}
3876
Tejun Heoa6d5a512006-03-06 04:31:57 +09003877/**
3878 * ata_dev_xfermask - Compute supported xfermask of the given device
Tejun Heoa6d5a512006-03-06 04:31:57 +09003879 * @dev: Device to compute xfermask for
3880 *
Tejun Heoacf356b2006-03-24 14:07:50 +09003881 * Compute supported xfermask of @dev and store it in
3882 * dev->*_mask. This function is responsible for applying all
3883 * known limits including host controller limits, device
3884 * blacklist, etc...
Tejun Heoa6d5a512006-03-06 04:31:57 +09003885 *
3886 * LOCKING:
3887 * None.
Tejun Heoa6d5a512006-03-06 04:31:57 +09003888 */
Tejun Heo3373efd2006-05-15 20:57:53 +09003889static void ata_dev_xfermask(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09003891 struct ata_link *link = dev->link;
3892 struct ata_port *ap = link->ap;
Jeff Garzikcca39742006-08-24 03:19:22 -04003893 struct ata_host *host = ap->host;
Tejun Heoa6d5a512006-03-06 04:31:57 +09003894 unsigned long xfer_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003895
Tejun Heo37deecb2006-08-10 16:59:07 +09003896 /* controller modes available */
Tejun Heo565083e2006-04-02 17:54:47 +09003897 xfer_mask = ata_pack_xfermask(ap->pio_mask,
3898 ap->mwdma_mask, ap->udma_mask);
3899
Robert Hancock8343f882007-03-06 02:37:51 -08003900 /* drive modes available */
Tejun Heo37deecb2006-08-10 16:59:07 +09003901 xfer_mask &= ata_pack_xfermask(dev->pio_mask,
3902 dev->mwdma_mask, dev->udma_mask);
3903 xfer_mask &= ata_id_xfermask(dev->id);
Tejun Heo565083e2006-04-02 17:54:47 +09003904
Alan Coxb352e572006-08-10 18:52:12 +01003905 /*
3906 * CFA Advanced TrueIDE timings are not allowed on a shared
3907 * cable
3908 */
3909 if (ata_dev_pair(dev)) {
3910 /* No PIO5 or PIO6 */
3911 xfer_mask &= ~(0x03 << (ATA_SHIFT_PIO + 5));
3912 /* No MWDMA3 or MWDMA 4 */
3913 xfer_mask &= ~(0x03 << (ATA_SHIFT_MWDMA + 3));
3914 }
3915
Tejun Heo37deecb2006-08-10 16:59:07 +09003916 if (ata_dma_blacklisted(dev)) {
3917 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
Tejun Heof15a1da2006-05-15 20:57:56 +09003918 ata_dev_printk(dev, KERN_WARNING,
3919 "device is on DMA blacklist, disabling DMA\n");
Tejun Heo37deecb2006-08-10 16:59:07 +09003920 }
Tejun Heoa6d5a512006-03-06 04:31:57 +09003921
Petr Vandrovec14d66ab2007-03-08 10:12:12 +01003922 if ((host->flags & ATA_HOST_SIMPLEX) &&
3923 host->simplex_claimed && host->simplex_claimed != ap) {
Tejun Heo37deecb2006-08-10 16:59:07 +09003924 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
3925 ata_dev_printk(dev, KERN_WARNING, "simplex DMA is claimed by "
3926 "other device, disabling DMA\n");
Alan Cox5444a6f2006-03-27 18:58:20 +01003927 }
Tejun Heo565083e2006-04-02 17:54:47 +09003928
Jeff Garzike4246752007-03-09 09:56:46 -05003929 if (ap->flags & ATA_FLAG_NO_IORDY)
3930 xfer_mask &= ata_pio_mask_no_iordy(dev);
3931
Alan Cox5444a6f2006-03-27 18:58:20 +01003932 if (ap->ops->mode_filter)
Alan Coxa76b62c2007-03-09 09:34:07 -05003933 xfer_mask = ap->ops->mode_filter(dev, xfer_mask);
Alan Cox5444a6f2006-03-27 18:58:20 +01003934
Robert Hancock8343f882007-03-06 02:37:51 -08003935 /* Apply cable rule here. Don't apply it early because when
3936 * we handle hot plug the cable type can itself change.
3937 * Check this last so that we know if the transfer rate was
3938 * solely limited by the cable.
3939 * Unknown or 80 wire cables reported host side are checked
3940 * drive side as well. Cases where we know a 40wire cable
3941 * is used safely for 80 are not checked here.
3942 */
3943 if (xfer_mask & (0xF8 << ATA_SHIFT_UDMA))
3944 /* UDMA/44 or higher would be available */
3945 if((ap->cbl == ATA_CBL_PATA40) ||
3946 (ata_drive_40wire(dev->id) &&
3947 (ap->cbl == ATA_CBL_PATA_UNK ||
3948 ap->cbl == ATA_CBL_PATA80))) {
3949 ata_dev_printk(dev, KERN_WARNING,
3950 "limited to UDMA/33 due to 40-wire cable\n");
3951 xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
3952 }
3953
Tejun Heo565083e2006-04-02 17:54:47 +09003954 ata_unpack_xfermask(xfer_mask, &dev->pio_mask,
3955 &dev->mwdma_mask, &dev->udma_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003956}
3957
Linus Torvalds1da177e2005-04-16 15:20:36 -07003958/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003959 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960 * @dev: Device to which command will be sent
3961 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04003962 * Issue SET FEATURES - XFER MODE command to device @dev
3963 * on port @ap.
3964 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003966 * PCI/etc. bus probe sem.
Tejun Heo83206a22006-03-24 15:25:31 +09003967 *
3968 * RETURNS:
3969 * 0 on success, AC_ERR_* mask otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970 */
3971
Tejun Heo3373efd2006-05-15 20:57:53 +09003972static unsigned int ata_dev_set_xfermode(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003973{
Tejun Heoa0123702005-12-13 14:49:31 +09003974 struct ata_taskfile tf;
Tejun Heo83206a22006-03-24 15:25:31 +09003975 unsigned int err_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003976
3977 /* set up set-features taskfile */
3978 DPRINTK("set features - xfer mode\n");
3979
Tejun Heo464cf172007-05-27 15:10:40 +02003980 /* Some controllers and ATAPI devices show flaky interrupt
3981 * behavior after setting xfer mode. Use polling instead.
3982 */
Tejun Heo3373efd2006-05-15 20:57:53 +09003983 ata_tf_init(dev, &tf);
Tejun Heoa0123702005-12-13 14:49:31 +09003984 tf.command = ATA_CMD_SET_FEATURES;
3985 tf.feature = SETFEATURES_XFER;
Tejun Heo464cf172007-05-27 15:10:40 +02003986 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_POLLING;
Tejun Heoa0123702005-12-13 14:49:31 +09003987 tf.protocol = ATA_PROT_NODATA;
3988 tf.nsect = dev->xfer_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003989
Tejun Heo3373efd2006-05-15 20:57:53 +09003990 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003991
Tejun Heo83206a22006-03-24 15:25:31 +09003992 DPRINTK("EXIT, err_mask=%x\n", err_mask);
3993 return err_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994}
3995
3996/**
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04003997 * ata_dev_set_AN - Issue SET FEATURES - SATA FEATURES
3998 * @dev: Device to which command will be sent
3999 * @enable: Whether to enable or disable the feature
4000 *
4001 * Issue SET FEATURES - SATA FEATURES command to device @dev
4002 * on port @ap with sector count set to indicate Asynchronous
4003 * Notification feature
4004 *
4005 * LOCKING:
4006 * PCI/etc. bus probe sem.
4007 *
4008 * RETURNS:
4009 * 0 on success, AC_ERR_* mask otherwise.
4010 */
4011static unsigned int ata_dev_set_AN(struct ata_device *dev, u8 enable)
4012{
4013 struct ata_taskfile tf;
4014 unsigned int err_mask;
4015
4016 /* set up set-features taskfile */
4017 DPRINTK("set features - SATA features\n");
4018
4019 ata_tf_init(dev, &tf);
4020 tf.command = ATA_CMD_SET_FEATURES;
4021 tf.feature = enable;
4022 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
4023 tf.protocol = ATA_PROT_NODATA;
4024 tf.nsect = SATA_AN;
4025
4026 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
4027
4028 DPRINTK("EXIT, err_mask=%x\n", err_mask);
4029 return err_mask;
4030}
4031
4032/**
Albert Lee8bf62ece2005-05-12 15:29:42 -04004033 * ata_dev_init_params - Issue INIT DEV PARAMS command
Albert Lee8bf62ece2005-05-12 15:29:42 -04004034 * @dev: Device to which command will be sent
Randy Dunlape2a7f772006-05-18 10:50:18 -07004035 * @heads: Number of heads (taskfile parameter)
4036 * @sectors: Number of sectors (taskfile parameter)
Albert Lee8bf62ece2005-05-12 15:29:42 -04004037 *
4038 * LOCKING:
Tejun Heo6aff8f12006-02-15 18:24:09 +09004039 * Kernel thread context (may sleep)
4040 *
4041 * RETURNS:
4042 * 0 on success, AC_ERR_* mask otherwise.
Albert Lee8bf62ece2005-05-12 15:29:42 -04004043 */
Tejun Heo3373efd2006-05-15 20:57:53 +09004044static unsigned int ata_dev_init_params(struct ata_device *dev,
4045 u16 heads, u16 sectors)
Albert Lee8bf62ece2005-05-12 15:29:42 -04004046{
Tejun Heoa0123702005-12-13 14:49:31 +09004047 struct ata_taskfile tf;
Tejun Heo6aff8f12006-02-15 18:24:09 +09004048 unsigned int err_mask;
Albert Lee8bf62ece2005-05-12 15:29:42 -04004049
4050 /* Number of sectors per track 1-255. Number of heads 1-16 */
4051 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
Albert Lee00b6f5e2006-03-27 16:39:18 +08004052 return AC_ERR_INVALID;
Albert Lee8bf62ece2005-05-12 15:29:42 -04004053
4054 /* set up init dev params taskfile */
4055 DPRINTK("init dev params \n");
4056
Tejun Heo3373efd2006-05-15 20:57:53 +09004057 ata_tf_init(dev, &tf);
Tejun Heoa0123702005-12-13 14:49:31 +09004058 tf.command = ATA_CMD_INIT_DEV_PARAMS;
4059 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
4060 tf.protocol = ATA_PROT_NODATA;
4061 tf.nsect = sectors;
4062 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
Albert Lee8bf62ece2005-05-12 15:29:42 -04004063
Tejun Heo3373efd2006-05-15 20:57:53 +09004064 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
Alan Cox18b24662007-08-08 14:28:49 +01004065 /* A clean abort indicates an original or just out of spec drive
4066 and we should continue as we issue the setup based on the
4067 drive reported working geometry */
4068 if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
4069 err_mask = 0;
Albert Lee8bf62ece2005-05-12 15:29:42 -04004070
Tejun Heo6aff8f12006-02-15 18:24:09 +09004071 DPRINTK("EXIT, err_mask=%x\n", err_mask);
4072 return err_mask;
Albert Lee8bf62ece2005-05-12 15:29:42 -04004073}
4074
4075/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004076 * ata_sg_clean - Unmap DMA memory associated with command
4077 * @qc: Command containing DMA memory to be released
4078 *
4079 * Unmap all mapped DMA memory associated with this command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080 *
4081 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004082 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004083 */
Tejun Heo70e6ad02006-11-14 22:47:10 +09004084void ata_sg_clean(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004085{
4086 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004087 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004088 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004089 void *pad_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004090
Tejun Heoa46314742006-02-11 19:11:13 +09004091 WARN_ON(!(qc->flags & ATA_QCFLAG_DMAMAP));
4092 WARN_ON(sg == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093
4094 if (qc->flags & ATA_QCFLAG_SINGLE)
Jeff Garzikf1318832006-02-20 16:55:56 -05004095 WARN_ON(qc->n_elem > 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05004097 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004099 /* if we padded the buffer out to 32-bit bound, and data
4100 * xfer direction is from-device, we must copy from the
4101 * pad buffer back into the supplied buffer
4102 */
4103 if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
4104 pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
4105
4106 if (qc->flags & ATA_QCFLAG_SG) {
Jeff Garzike1410f22005-11-14 14:06:26 -05004107 if (qc->n_elem)
Brian King2f1f6102006-03-23 17:30:15 -06004108 dma_unmap_sg(ap->dev, sg, qc->n_elem, dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004109 /* restore last sg */
4110 sg[qc->orig_n_elem - 1].length += qc->pad_len;
4111 if (pad_buf) {
4112 struct scatterlist *psg = &qc->pad_sgent;
4113 void *addr = kmap_atomic(psg->page, KM_IRQ0);
4114 memcpy(addr + psg->offset, pad_buf, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05004115 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004116 }
4117 } else {
Tejun Heo2e242fa2006-02-20 23:48:38 +09004118 if (qc->n_elem)
Brian King2f1f6102006-03-23 17:30:15 -06004119 dma_unmap_single(ap->dev,
Jeff Garzike1410f22005-11-14 14:06:26 -05004120 sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
4121 dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004122 /* restore sg */
4123 sg->length += qc->pad_len;
4124 if (pad_buf)
4125 memcpy(qc->buf_virt + sg->length - qc->pad_len,
4126 pad_buf, qc->pad_len);
4127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128
4129 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004130 qc->__sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131}
4132
4133/**
4134 * ata_fill_sg - Fill PCI IDE PRD table
4135 * @qc: Metadata associated with taskfile to be transferred
4136 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04004137 * Fill PCI IDE PRD (scatter-gather) table with segments
4138 * associated with the current disk command.
4139 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004141 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142 *
4143 */
4144static void ata_fill_sg(struct ata_queued_cmd *qc)
4145{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004147 struct scatterlist *sg;
4148 unsigned int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149
Tejun Heoa46314742006-02-11 19:11:13 +09004150 WARN_ON(qc->__sg == NULL);
Jeff Garzikf1318832006-02-20 16:55:56 -05004151 WARN_ON(qc->n_elem == 0 && qc->pad_len == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004152
4153 idx = 0;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004154 ata_for_each_sg(sg, qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 u32 addr, offset;
4156 u32 sg_len, len;
4157
4158 /* determine if physical DMA addr spans 64K boundary.
4159 * Note h/w doesn't support 64-bit, so we unconditionally
4160 * truncate dma_addr_t to u32.
4161 */
4162 addr = (u32) sg_dma_address(sg);
4163 sg_len = sg_dma_len(sg);
4164
4165 while (sg_len) {
4166 offset = addr & 0xffff;
4167 len = sg_len;
4168 if ((offset + sg_len) > 0x10000)
4169 len = 0x10000 - offset;
4170
4171 ap->prd[idx].addr = cpu_to_le32(addr);
4172 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
4173 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
4174
4175 idx++;
4176 sg_len -= len;
4177 addr += len;
4178 }
4179 }
4180
4181 if (idx)
4182 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
4183}
Tejun Heob9a41972007-06-27 02:48:43 +09004184
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185/**
Alan Coxd26fc952007-07-06 19:13:52 -04004186 * ata_fill_sg_dumb - Fill PCI IDE PRD table
4187 * @qc: Metadata associated with taskfile to be transferred
4188 *
4189 * Fill PCI IDE PRD (scatter-gather) table with segments
4190 * associated with the current disk command. Perform the fill
4191 * so that we avoid writing any length 64K records for
4192 * controllers that don't follow the spec.
4193 *
4194 * LOCKING:
4195 * spin_lock_irqsave(host lock)
4196 *
4197 */
4198static void ata_fill_sg_dumb(struct ata_queued_cmd *qc)
4199{
4200 struct ata_port *ap = qc->ap;
4201 struct scatterlist *sg;
4202 unsigned int idx;
4203
4204 WARN_ON(qc->__sg == NULL);
4205 WARN_ON(qc->n_elem == 0 && qc->pad_len == 0);
4206
4207 idx = 0;
4208 ata_for_each_sg(sg, qc) {
4209 u32 addr, offset;
4210 u32 sg_len, len, blen;
4211
4212 /* determine if physical DMA addr spans 64K boundary.
4213 * Note h/w doesn't support 64-bit, so we unconditionally
4214 * truncate dma_addr_t to u32.
4215 */
4216 addr = (u32) sg_dma_address(sg);
4217 sg_len = sg_dma_len(sg);
4218
4219 while (sg_len) {
4220 offset = addr & 0xffff;
4221 len = sg_len;
4222 if ((offset + sg_len) > 0x10000)
4223 len = 0x10000 - offset;
4224
4225 blen = len & 0xffff;
4226 ap->prd[idx].addr = cpu_to_le32(addr);
4227 if (blen == 0) {
4228 /* Some PATA chipsets like the CS5530 can't
4229 cope with 0x0000 meaning 64K as the spec says */
4230 ap->prd[idx].flags_len = cpu_to_le32(0x8000);
4231 blen = 0x8000;
4232 ap->prd[++idx].addr = cpu_to_le32(addr + 0x8000);
4233 }
4234 ap->prd[idx].flags_len = cpu_to_le32(blen);
4235 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
4236
4237 idx++;
4238 sg_len -= len;
4239 addr += len;
4240 }
4241 }
4242
4243 if (idx)
4244 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
4245}
4246
4247/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
4249 * @qc: Metadata associated with taskfile to check
4250 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04004251 * Allow low-level driver to filter ATA PACKET commands, returning
4252 * a status indicating whether or not it is OK to use DMA for the
4253 * supplied PACKET command.
4254 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004256 * spin_lock_irqsave(host lock)
Jeff Garzik0cba6322005-05-30 19:49:12 -04004257 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258 * RETURNS: 0 when ATAPI DMA can be used
4259 * nonzero otherwise
4260 */
4261int ata_check_atapi_dma(struct ata_queued_cmd *qc)
4262{
4263 struct ata_port *ap = qc->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264
Tejun Heob9a41972007-06-27 02:48:43 +09004265 /* Don't allow DMA if it isn't multiple of 16 bytes. Quite a
4266 * few ATAPI devices choke on such DMA requests.
4267 */
4268 if (unlikely(qc->nbytes & 15))
4269 return 1;
Albert Lee6f23a312007-04-02 11:39:25 +08004270
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271 if (ap->ops->check_atapi_dma)
Tejun Heob9a41972007-06-27 02:48:43 +09004272 return ap->ops->check_atapi_dma(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273
Tejun Heob9a41972007-06-27 02:48:43 +09004274 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275}
Tejun Heob9a41972007-06-27 02:48:43 +09004276
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277/**
4278 * ata_qc_prep - Prepare taskfile for submission
4279 * @qc: Metadata associated with taskfile to be prepared
4280 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04004281 * Prepare ATA taskfile for submission.
4282 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004284 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285 */
4286void ata_qc_prep(struct ata_queued_cmd *qc)
4287{
4288 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
4289 return;
4290
4291 ata_fill_sg(qc);
4292}
4293
Alan Coxd26fc952007-07-06 19:13:52 -04004294/**
4295 * ata_dumb_qc_prep - Prepare taskfile for submission
4296 * @qc: Metadata associated with taskfile to be prepared
4297 *
4298 * Prepare ATA taskfile for submission.
4299 *
4300 * LOCKING:
4301 * spin_lock_irqsave(host lock)
4302 */
4303void ata_dumb_qc_prep(struct ata_queued_cmd *qc)
4304{
4305 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
4306 return;
4307
4308 ata_fill_sg_dumb(qc);
4309}
4310
Brian Kinge46834c2006-03-17 17:04:03 -06004311void ata_noop_qc_prep(struct ata_queued_cmd *qc) { }
4312
Jeff Garzik0cba6322005-05-30 19:49:12 -04004313/**
4314 * ata_sg_init_one - Associate command with memory buffer
4315 * @qc: Command to be associated
4316 * @buf: Memory buffer
4317 * @buflen: Length of memory buffer, in bytes.
4318 *
4319 * Initialize the data-related elements of queued_cmd @qc
4320 * to point to a single memory buffer, @buf of byte length @buflen.
4321 *
4322 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004323 * spin_lock_irqsave(host lock)
Jeff Garzik0cba6322005-05-30 19:49:12 -04004324 */
4325
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
4327{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328 qc->flags |= ATA_QCFLAG_SINGLE;
4329
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004330 qc->__sg = &qc->sgent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 qc->n_elem = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004332 qc->orig_n_elem = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004333 qc->buf_virt = buf;
Brian King233277c2006-06-07 11:25:31 -05004334 qc->nbytes = buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335
Tejun Heo61c05962006-11-14 22:35:43 +09004336 sg_init_one(&qc->sgent, buf, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337}
4338
Jeff Garzik0cba6322005-05-30 19:49:12 -04004339/**
4340 * ata_sg_init - Associate command with scatter-gather table.
4341 * @qc: Command to be associated
4342 * @sg: Scatter-gather table.
4343 * @n_elem: Number of elements in s/g table.
4344 *
4345 * Initialize the data-related elements of queued_cmd @qc
4346 * to point to a scatter-gather table @sg, containing @n_elem
4347 * elements.
4348 *
4349 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004350 * spin_lock_irqsave(host lock)
Jeff Garzik0cba6322005-05-30 19:49:12 -04004351 */
4352
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
4354 unsigned int n_elem)
4355{
4356 qc->flags |= ATA_QCFLAG_SG;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004357 qc->__sg = sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358 qc->n_elem = n_elem;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004359 qc->orig_n_elem = n_elem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360}
4361
4362/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004363 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
4364 * @qc: Command with memory buffer to be mapped.
4365 *
4366 * DMA-map the memory buffer associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367 *
4368 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004369 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004370 *
4371 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004372 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004373 */
4374
4375static int ata_sg_setup_one(struct ata_queued_cmd *qc)
4376{
4377 struct ata_port *ap = qc->ap;
4378 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004379 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380 dma_addr_t dma_address;
Tejun Heo2e242fa2006-02-20 23:48:38 +09004381 int trim_sg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004382
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004383 /* we must lengthen transfers to end on a 32-bit boundary */
4384 qc->pad_len = sg->length & 3;
4385 if (qc->pad_len) {
4386 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
4387 struct scatterlist *psg = &qc->pad_sgent;
4388
Tejun Heoa46314742006-02-11 19:11:13 +09004389 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004390
4391 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
4392
4393 if (qc->tf.flags & ATA_TFLAG_WRITE)
4394 memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
4395 qc->pad_len);
4396
4397 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
4398 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
4399 /* trim sg */
4400 sg->length -= qc->pad_len;
Tejun Heo2e242fa2006-02-20 23:48:38 +09004401 if (sg->length == 0)
4402 trim_sg = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004403
4404 DPRINTK("padding done, sg->length=%u pad_len=%u\n",
4405 sg->length, qc->pad_len);
4406 }
4407
Tejun Heo2e242fa2006-02-20 23:48:38 +09004408 if (trim_sg) {
4409 qc->n_elem--;
Jeff Garzike1410f22005-11-14 14:06:26 -05004410 goto skip_map;
4411 }
4412
Brian King2f1f6102006-03-23 17:30:15 -06004413 dma_address = dma_map_single(ap->dev, qc->buf_virt,
Albert Lee32529e02005-05-26 03:49:42 -04004414 sg->length, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05004415 if (dma_mapping_error(dma_address)) {
4416 /* restore sg */
4417 sg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004418 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05004419 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004420
4421 sg_dma_address(sg) = dma_address;
Albert Lee32529e02005-05-26 03:49:42 -04004422 sg_dma_len(sg) = sg->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004423
Tejun Heo2e242fa2006-02-20 23:48:38 +09004424skip_map:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004425 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
4426 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
4427
4428 return 0;
4429}
4430
4431/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004432 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
4433 * @qc: Command with scatter-gather table to be mapped.
4434 *
4435 * DMA-map the scatter-gather table associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436 *
4437 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004438 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004439 *
4440 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004441 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442 *
4443 */
4444
4445static int ata_sg_setup(struct ata_queued_cmd *qc)
4446{
4447 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004448 struct scatterlist *sg = qc->__sg;
4449 struct scatterlist *lsg = &sg[qc->n_elem - 1];
Jeff Garzike1410f22005-11-14 14:06:26 -05004450 int n_elem, pre_n_elem, dir, trim_sg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451
Tejun Heo44877b42007-02-21 01:06:51 +09004452 VPRINTK("ENTER, ata%u\n", ap->print_id);
Tejun Heoa46314742006-02-11 19:11:13 +09004453 WARN_ON(!(qc->flags & ATA_QCFLAG_SG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004455 /* we must lengthen transfers to end on a 32-bit boundary */
4456 qc->pad_len = lsg->length & 3;
4457 if (qc->pad_len) {
4458 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
4459 struct scatterlist *psg = &qc->pad_sgent;
4460 unsigned int offset;
4461
Tejun Heoa46314742006-02-11 19:11:13 +09004462 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004463
4464 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
4465
4466 /*
4467 * psg->page/offset are used to copy to-be-written
4468 * data in this function or read data in ata_sg_clean.
4469 */
4470 offset = lsg->offset + lsg->length - qc->pad_len;
4471 psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
4472 psg->offset = offset_in_page(offset);
4473
4474 if (qc->tf.flags & ATA_TFLAG_WRITE) {
4475 void *addr = kmap_atomic(psg->page, KM_IRQ0);
4476 memcpy(pad_buf, addr + psg->offset, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05004477 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004478 }
4479
4480 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
4481 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
4482 /* trim last sg */
4483 lsg->length -= qc->pad_len;
Jeff Garzike1410f22005-11-14 14:06:26 -05004484 if (lsg->length == 0)
4485 trim_sg = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004486
4487 DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
4488 qc->n_elem - 1, lsg->length, qc->pad_len);
4489 }
4490
Jeff Garzike1410f22005-11-14 14:06:26 -05004491 pre_n_elem = qc->n_elem;
4492 if (trim_sg && pre_n_elem)
4493 pre_n_elem--;
4494
4495 if (!pre_n_elem) {
4496 n_elem = 0;
4497 goto skip_map;
4498 }
4499
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 dir = qc->dma_dir;
Brian King2f1f6102006-03-23 17:30:15 -06004501 n_elem = dma_map_sg(ap->dev, sg, pre_n_elem, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05004502 if (n_elem < 1) {
4503 /* restore last sg */
4504 lsg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05004506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507
4508 DPRINTK("%d sg elements mapped\n", n_elem);
4509
Jeff Garzike1410f22005-11-14 14:06:26 -05004510skip_map:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511 qc->n_elem = n_elem;
4512
4513 return 0;
4514}
4515
4516/**
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004517 * swap_buf_le16 - swap halves of 16-bit words in place
Edward Falk0baab862005-06-02 18:17:13 -04004518 * @buf: Buffer to swap
4519 * @buf_words: Number of 16-bit words in buffer.
4520 *
4521 * Swap halves of 16-bit words if needed to convert from
4522 * little-endian byte order to native cpu byte order, or
4523 * vice-versa.
4524 *
4525 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004526 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04004527 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528void swap_buf_le16(u16 *buf, unsigned int buf_words)
4529{
4530#ifdef __BIG_ENDIAN
4531 unsigned int i;
4532
4533 for (i = 0; i < buf_words; i++)
4534 buf[i] = le16_to_cpu(buf[i]);
4535#endif /* __BIG_ENDIAN */
4536}
4537
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004538/**
Tejun Heo0d5ff562007-02-01 15:06:36 +09004539 * ata_data_xfer - Transfer data by PIO
Alan Coxa6b2c5d2006-05-22 16:59:59 +01004540 * @adev: device to target
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004541 * @buf: data buffer
4542 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04004543 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004544 *
4545 * Transfer data from/to the device data register by PIO.
4546 *
4547 * LOCKING:
4548 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004549 */
Tejun Heo0d5ff562007-02-01 15:06:36 +09004550void ata_data_xfer(struct ata_device *adev, unsigned char *buf,
4551 unsigned int buflen, int write_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09004553 struct ata_port *ap = adev->link->ap;
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004554 unsigned int words = buflen >> 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004556 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557 if (write_data)
Tejun Heo0d5ff562007-02-01 15:06:36 +09004558 iowrite16_rep(ap->ioaddr.data_addr, buf, words);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559 else
Tejun Heo0d5ff562007-02-01 15:06:36 +09004560 ioread16_rep(ap->ioaddr.data_addr, buf, words);
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004561
4562 /* Transfer trailing 1 byte, if any. */
4563 if (unlikely(buflen & 0x01)) {
4564 u16 align_buf[1] = { 0 };
4565 unsigned char *trailing_buf = buf + buflen - 1;
4566
4567 if (write_data) {
4568 memcpy(align_buf, trailing_buf, 1);
Tejun Heo0d5ff562007-02-01 15:06:36 +09004569 iowrite16(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004570 } else {
Tejun Heo0d5ff562007-02-01 15:06:36 +09004571 align_buf[0] = cpu_to_le16(ioread16(ap->ioaddr.data_addr));
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004572 memcpy(trailing_buf, align_buf, 1);
4573 }
4574 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575}
4576
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004577/**
Tejun Heo0d5ff562007-02-01 15:06:36 +09004578 * ata_data_xfer_noirq - Transfer data by PIO
Alan Cox75e99582006-05-24 14:14:41 +01004579 * @adev: device to target
4580 * @buf: data buffer
4581 * @buflen: buffer length
4582 * @write_data: read/write
4583 *
Tejun Heo88574552006-06-25 20:00:35 +09004584 * Transfer data from/to the device data register by PIO. Do the
Alan Cox75e99582006-05-24 14:14:41 +01004585 * transfer with interrupts disabled.
4586 *
4587 * LOCKING:
4588 * Inherited from caller.
4589 */
Tejun Heo0d5ff562007-02-01 15:06:36 +09004590void ata_data_xfer_noirq(struct ata_device *adev, unsigned char *buf,
4591 unsigned int buflen, int write_data)
Alan Cox75e99582006-05-24 14:14:41 +01004592{
4593 unsigned long flags;
4594 local_irq_save(flags);
Tejun Heo0d5ff562007-02-01 15:06:36 +09004595 ata_data_xfer(adev, buf, buflen, write_data);
Alan Cox75e99582006-05-24 14:14:41 +01004596 local_irq_restore(flags);
4597}
4598
4599
4600/**
Mark Lord5a5dbd12007-03-16 10:22:26 -04004601 * ata_pio_sector - Transfer a sector of data.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004602 * @qc: Command on going
4603 *
Mark Lord5a5dbd12007-03-16 10:22:26 -04004604 * Transfer qc->sect_size bytes of data from/to the ATA device.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004605 *
4606 * LOCKING:
4607 * Inherited from caller.
4608 */
4609
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610static void ata_pio_sector(struct ata_queued_cmd *qc)
4611{
4612 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004613 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614 struct ata_port *ap = qc->ap;
4615 struct page *page;
4616 unsigned int offset;
4617 unsigned char *buf;
4618
Mark Lord5a5dbd12007-03-16 10:22:26 -04004619 if (qc->curbytes == qc->nbytes - qc->sect_size)
Albert Lee14be71f2005-09-27 17:36:35 +08004620 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621
4622 page = sg[qc->cursg].page;
Tejun Heo726f0782007-01-03 17:30:39 +09004623 offset = sg[qc->cursg].offset + qc->cursg_ofs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624
4625 /* get the current page and offset */
4626 page = nth_page(page, (offset >> PAGE_SHIFT));
4627 offset %= PAGE_SIZE;
4628
Albert Lee7282aa42005-10-09 09:46:07 -04004629 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
4630
Albert Lee91b8b312005-10-09 09:48:44 -04004631 if (PageHighMem(page)) {
4632 unsigned long flags;
Albert Lee7282aa42005-10-09 09:46:07 -04004633
Alan Coxa6b2c5d2006-05-22 16:59:59 +01004634 /* FIXME: use a bounce buffer */
Albert Lee91b8b312005-10-09 09:48:44 -04004635 local_irq_save(flags);
4636 buf = kmap_atomic(page, KM_IRQ0);
Albert Lee083958d2005-10-09 09:47:31 -04004637
Albert Lee91b8b312005-10-09 09:48:44 -04004638 /* do the actual data transfer */
Mark Lord5a5dbd12007-03-16 10:22:26 -04004639 ap->ops->data_xfer(qc->dev, buf + offset, qc->sect_size, do_write);
Albert Lee91b8b312005-10-09 09:48:44 -04004640
4641 kunmap_atomic(buf, KM_IRQ0);
4642 local_irq_restore(flags);
4643 } else {
4644 buf = page_address(page);
Mark Lord5a5dbd12007-03-16 10:22:26 -04004645 ap->ops->data_xfer(qc->dev, buf + offset, qc->sect_size, do_write);
Albert Lee91b8b312005-10-09 09:48:44 -04004646 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647
Mark Lord5a5dbd12007-03-16 10:22:26 -04004648 qc->curbytes += qc->sect_size;
4649 qc->cursg_ofs += qc->sect_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650
Tejun Heo726f0782007-01-03 17:30:39 +09004651 if (qc->cursg_ofs == (&sg[qc->cursg])->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652 qc->cursg++;
4653 qc->cursg_ofs = 0;
4654 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004657/**
Mark Lord5a5dbd12007-03-16 10:22:26 -04004658 * ata_pio_sectors - Transfer one or many sectors.
Albert Lee07f6f7d2005-11-01 19:33:20 +08004659 * @qc: Command on going
4660 *
Mark Lord5a5dbd12007-03-16 10:22:26 -04004661 * Transfer one or many sectors of data from/to the
Albert Lee07f6f7d2005-11-01 19:33:20 +08004662 * ATA device for the DRQ request.
4663 *
4664 * LOCKING:
4665 * Inherited from caller.
4666 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667
Albert Lee07f6f7d2005-11-01 19:33:20 +08004668static void ata_pio_sectors(struct ata_queued_cmd *qc)
4669{
4670 if (is_multi_taskfile(&qc->tf)) {
4671 /* READ/WRITE MULTIPLE */
4672 unsigned int nsect;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004673
Jeff Garzik587005d2006-02-11 18:17:32 -05004674 WARN_ON(qc->dev->multi_count == 0);
Albert Lee07f6f7d2005-11-01 19:33:20 +08004675
Mark Lord5a5dbd12007-03-16 10:22:26 -04004676 nsect = min((qc->nbytes - qc->curbytes) / qc->sect_size,
Tejun Heo726f0782007-01-03 17:30:39 +09004677 qc->dev->multi_count);
Albert Lee07f6f7d2005-11-01 19:33:20 +08004678 while (nsect--)
4679 ata_pio_sector(qc);
4680 } else
4681 ata_pio_sector(qc);
Albert Lee4cc980b2007-08-15 03:19:45 -04004682
4683 ata_altstatus(qc->ap); /* flush */
Albert Lee07f6f7d2005-11-01 19:33:20 +08004684}
4685
4686/**
Albert Leec71c1852005-10-04 06:03:45 -04004687 * atapi_send_cdb - Write CDB bytes to hardware
4688 * @ap: Port to which ATAPI device is attached.
4689 * @qc: Taskfile currently active
4690 *
4691 * When device has indicated its readiness to accept
4692 * a CDB, this function is called. Send the CDB.
4693 *
4694 * LOCKING:
4695 * caller.
4696 */
4697
4698static void atapi_send_cdb(struct ata_port *ap, struct ata_queued_cmd *qc)
4699{
4700 /* send SCSI cdb */
4701 DPRINTK("send cdb\n");
Jeff Garzikdb024d52006-02-13 00:23:57 -05004702 WARN_ON(qc->dev->cdb_len < 12);
Albert Leec71c1852005-10-04 06:03:45 -04004703
Alan Coxa6b2c5d2006-05-22 16:59:59 +01004704 ap->ops->data_xfer(qc->dev, qc->cdb, qc->dev->cdb_len, 1);
Albert Leec71c1852005-10-04 06:03:45 -04004705 ata_altstatus(ap); /* flush */
4706
4707 switch (qc->tf.protocol) {
4708 case ATA_PROT_ATAPI:
4709 ap->hsm_task_state = HSM_ST;
4710 break;
4711 case ATA_PROT_ATAPI_NODATA:
4712 ap->hsm_task_state = HSM_ST_LAST;
4713 break;
4714 case ATA_PROT_ATAPI_DMA:
4715 ap->hsm_task_state = HSM_ST_LAST;
4716 /* initiate bmdma */
4717 ap->ops->bmdma_start(qc);
4718 break;
4719 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720}
4721
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004722/**
4723 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
4724 * @qc: Command on going
4725 * @bytes: number of bytes
4726 *
4727 * Transfer Transfer data from/to the ATAPI device.
4728 *
4729 * LOCKING:
4730 * Inherited from caller.
4731 *
4732 */
4733
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
4735{
4736 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004737 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004738 struct ata_port *ap = qc->ap;
4739 struct page *page;
4740 unsigned char *buf;
4741 unsigned int offset, count;
4742
Albert Lee563a6e12005-08-12 14:17:50 +08004743 if (qc->curbytes + bytes >= qc->nbytes)
Albert Lee14be71f2005-09-27 17:36:35 +08004744 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745
4746next_sg:
Albert Lee563a6e12005-08-12 14:17:50 +08004747 if (unlikely(qc->cursg >= qc->n_elem)) {
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04004748 /*
Albert Lee563a6e12005-08-12 14:17:50 +08004749 * The end of qc->sg is reached and the device expects
4750 * more data to transfer. In order not to overrun qc->sg
4751 * and fulfill length specified in the byte count register,
4752 * - for read case, discard trailing data from the device
4753 * - for write case, padding zero data to the device
4754 */
4755 u16 pad_buf[1] = { 0 };
4756 unsigned int words = bytes >> 1;
4757 unsigned int i;
4758
4759 if (words) /* warning if bytes > 1 */
Tejun Heof15a1da2006-05-15 20:57:56 +09004760 ata_dev_printk(qc->dev, KERN_WARNING,
4761 "%u bytes trailing data\n", bytes);
Albert Lee563a6e12005-08-12 14:17:50 +08004762
4763 for (i = 0; i < words; i++)
Alan Coxa6b2c5d2006-05-22 16:59:59 +01004764 ap->ops->data_xfer(qc->dev, (unsigned char*)pad_buf, 2, do_write);
Albert Lee563a6e12005-08-12 14:17:50 +08004765
Albert Lee14be71f2005-09-27 17:36:35 +08004766 ap->hsm_task_state = HSM_ST_LAST;
Albert Lee563a6e12005-08-12 14:17:50 +08004767 return;
4768 }
4769
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004770 sg = &qc->__sg[qc->cursg];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772 page = sg->page;
4773 offset = sg->offset + qc->cursg_ofs;
4774
4775 /* get the current page and offset */
4776 page = nth_page(page, (offset >> PAGE_SHIFT));
4777 offset %= PAGE_SIZE;
4778
Albert Lee6952df02005-06-06 15:56:03 +08004779 /* don't overrun current sg */
Albert Lee32529e02005-05-26 03:49:42 -04004780 count = min(sg->length - qc->cursg_ofs, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781
4782 /* don't cross page boundaries */
4783 count = min(count, (unsigned int)PAGE_SIZE - offset);
4784
Albert Lee7282aa42005-10-09 09:46:07 -04004785 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
4786
Albert Lee91b8b312005-10-09 09:48:44 -04004787 if (PageHighMem(page)) {
4788 unsigned long flags;
Albert Lee7282aa42005-10-09 09:46:07 -04004789
Alan Coxa6b2c5d2006-05-22 16:59:59 +01004790 /* FIXME: use bounce buffer */
Albert Lee91b8b312005-10-09 09:48:44 -04004791 local_irq_save(flags);
4792 buf = kmap_atomic(page, KM_IRQ0);
Albert Lee083958d2005-10-09 09:47:31 -04004793
Albert Lee91b8b312005-10-09 09:48:44 -04004794 /* do the actual data transfer */
Alan Coxa6b2c5d2006-05-22 16:59:59 +01004795 ap->ops->data_xfer(qc->dev, buf + offset, count, do_write);
Albert Lee91b8b312005-10-09 09:48:44 -04004796
4797 kunmap_atomic(buf, KM_IRQ0);
4798 local_irq_restore(flags);
4799 } else {
4800 buf = page_address(page);
Alan Coxa6b2c5d2006-05-22 16:59:59 +01004801 ap->ops->data_xfer(qc->dev, buf + offset, count, do_write);
Albert Lee91b8b312005-10-09 09:48:44 -04004802 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004803
4804 bytes -= count;
4805 qc->curbytes += count;
4806 qc->cursg_ofs += count;
4807
Albert Lee32529e02005-05-26 03:49:42 -04004808 if (qc->cursg_ofs == sg->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809 qc->cursg++;
4810 qc->cursg_ofs = 0;
4811 }
4812
Albert Lee563a6e12005-08-12 14:17:50 +08004813 if (bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814 goto next_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815}
4816
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004817/**
4818 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
4819 * @qc: Command on going
4820 *
4821 * Transfer Transfer data from/to the ATAPI device.
4822 *
4823 * LOCKING:
4824 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004825 */
4826
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827static void atapi_pio_bytes(struct ata_queued_cmd *qc)
4828{
4829 struct ata_port *ap = qc->ap;
4830 struct ata_device *dev = qc->dev;
4831 unsigned int ireason, bc_lo, bc_hi, bytes;
4832 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
4833
Albert Leeeec4c3f2006-05-18 17:51:10 +08004834 /* Abuse qc->result_tf for temp storage of intermediate TF
4835 * here to save some kernel stack usage.
4836 * For normal completion, qc->result_tf is not relevant. For
4837 * error, qc->result_tf is later overwritten by ata_qc_complete().
4838 * So, the correctness of qc->result_tf is not affected.
4839 */
4840 ap->ops->tf_read(ap, &qc->result_tf);
4841 ireason = qc->result_tf.nsect;
4842 bc_lo = qc->result_tf.lbam;
4843 bc_hi = qc->result_tf.lbah;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844 bytes = (bc_hi << 8) | bc_lo;
4845
4846 /* shall be cleared to zero, indicating xfer of data */
4847 if (ireason & (1 << 0))
4848 goto err_out;
4849
4850 /* make sure transfer direction matches expected */
4851 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
4852 if (do_write != i_write)
4853 goto err_out;
4854
Tejun Heo44877b42007-02-21 01:06:51 +09004855 VPRINTK("ata%u: xfering %d bytes\n", ap->print_id, bytes);
Albert Lee312f7da2005-09-27 17:38:03 +08004856
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857 __atapi_pio_bytes(qc, bytes);
Albert Lee4cc980b2007-08-15 03:19:45 -04004858 ata_altstatus(ap); /* flush */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859
4860 return;
4861
4862err_out:
Tejun Heof15a1da2006-05-15 20:57:56 +09004863 ata_dev_printk(dev, KERN_INFO, "ATAPI check failed\n");
Tejun Heo11a56d22006-01-23 13:09:36 +09004864 qc->err_mask |= AC_ERR_HSM;
Albert Lee14be71f2005-09-27 17:36:35 +08004865 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866}
4867
4868/**
Albert Leec234fb02006-03-25 17:58:38 +08004869 * ata_hsm_ok_in_wq - Check if the qc can be handled in the workqueue.
4870 * @ap: the target ata_port
4871 * @qc: qc on going
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872 *
Albert Leec234fb02006-03-25 17:58:38 +08004873 * RETURNS:
4874 * 1 if ok in workqueue, 0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875 */
Albert Leec234fb02006-03-25 17:58:38 +08004876
4877static inline int ata_hsm_ok_in_wq(struct ata_port *ap, struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878{
Albert Leec234fb02006-03-25 17:58:38 +08004879 if (qc->tf.flags & ATA_TFLAG_POLLING)
4880 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881
Albert Leec234fb02006-03-25 17:58:38 +08004882 if (ap->hsm_task_state == HSM_ST_FIRST) {
4883 if (qc->tf.protocol == ATA_PROT_PIO &&
4884 (qc->tf.flags & ATA_TFLAG_WRITE))
4885 return 1;
4886
4887 if (is_atapi_taskfile(&qc->tf) &&
4888 !(qc->dev->flags & ATA_DFLAG_CDB_INTR))
4889 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890 }
4891
Albert Leec234fb02006-03-25 17:58:38 +08004892 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893}
4894
Albert Leec234fb02006-03-25 17:58:38 +08004895/**
Tejun Heoc17ea202006-05-15 20:59:29 +09004896 * ata_hsm_qc_complete - finish a qc running on standard HSM
4897 * @qc: Command to complete
4898 * @in_wq: 1 if called from workqueue, 0 otherwise
4899 *
4900 * Finish @qc which is running on standard HSM.
4901 *
4902 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004903 * If @in_wq is zero, spin_lock_irqsave(host lock).
Tejun Heoc17ea202006-05-15 20:59:29 +09004904 * Otherwise, none on entry and grabs host lock.
4905 */
4906static void ata_hsm_qc_complete(struct ata_queued_cmd *qc, int in_wq)
4907{
4908 struct ata_port *ap = qc->ap;
4909 unsigned long flags;
4910
4911 if (ap->ops->error_handler) {
4912 if (in_wq) {
Jeff Garzikba6a1302006-06-22 23:46:10 -04004913 spin_lock_irqsave(ap->lock, flags);
Tejun Heoc17ea202006-05-15 20:59:29 +09004914
Jeff Garzikcca39742006-08-24 03:19:22 -04004915 /* EH might have kicked in while host lock is
4916 * released.
Tejun Heoc17ea202006-05-15 20:59:29 +09004917 */
4918 qc = ata_qc_from_tag(ap, qc->tag);
4919 if (qc) {
4920 if (likely(!(qc->err_mask & AC_ERR_HSM))) {
Akira Iguchi83625002007-01-26 16:27:32 +09004921 ap->ops->irq_on(ap);
Tejun Heoc17ea202006-05-15 20:59:29 +09004922 ata_qc_complete(qc);
4923 } else
4924 ata_port_freeze(ap);
4925 }
4926
Jeff Garzikba6a1302006-06-22 23:46:10 -04004927 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoc17ea202006-05-15 20:59:29 +09004928 } else {
4929 if (likely(!(qc->err_mask & AC_ERR_HSM)))
4930 ata_qc_complete(qc);
4931 else
4932 ata_port_freeze(ap);
4933 }
4934 } else {
4935 if (in_wq) {
Jeff Garzikba6a1302006-06-22 23:46:10 -04004936 spin_lock_irqsave(ap->lock, flags);
Akira Iguchi83625002007-01-26 16:27:32 +09004937 ap->ops->irq_on(ap);
Tejun Heoc17ea202006-05-15 20:59:29 +09004938 ata_qc_complete(qc);
Jeff Garzikba6a1302006-06-22 23:46:10 -04004939 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoc17ea202006-05-15 20:59:29 +09004940 } else
4941 ata_qc_complete(qc);
4942 }
4943}
4944
4945/**
Albert Leebb5cb292006-03-25 17:48:02 +08004946 * ata_hsm_move - move the HSM to the next state.
4947 * @ap: the target ata_port
4948 * @qc: qc on going
4949 * @status: current device status
4950 * @in_wq: 1 if called from workqueue, 0 otherwise
4951 *
4952 * RETURNS:
4953 * 1 when poll next status needed, 0 otherwise.
4954 */
Tejun Heo9a1004d2006-05-31 18:27:52 +09004955int ata_hsm_move(struct ata_port *ap, struct ata_queued_cmd *qc,
4956 u8 status, int in_wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957{
Albert Leebb5cb292006-03-25 17:48:02 +08004958 unsigned long flags = 0;
4959 int poll_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960
Albert Lee6912ccd2006-03-25 17:45:49 +08004961 WARN_ON((qc->flags & ATA_QCFLAG_ACTIVE) == 0);
Albert Lee0565c262006-02-13 18:55:25 +08004962
Albert Leebb5cb292006-03-25 17:48:02 +08004963 /* Make sure ata_qc_issue_prot() does not throw things
4964 * like DMA polling into the workqueue. Notice that
4965 * in_wq is not equivalent to (qc->tf.flags & ATA_TFLAG_POLLING).
Albert Lee1c848982005-12-05 15:40:15 +08004966 */
Albert Leec234fb02006-03-25 17:58:38 +08004967 WARN_ON(in_wq != ata_hsm_ok_in_wq(ap, qc));
Albert Lee1c848982005-12-05 15:40:15 +08004968
Albert Leee2cec772006-03-25 17:43:49 +08004969fsm_start:
Albert Lee999bb6f2006-03-25 18:07:48 +08004970 DPRINTK("ata%u: protocol %d task_state %d (dev_stat 0x%X)\n",
Tejun Heo44877b42007-02-21 01:06:51 +09004971 ap->print_id, qc->tf.protocol, ap->hsm_task_state, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972
Albert Leee2cec772006-03-25 17:43:49 +08004973 switch (ap->hsm_task_state) {
4974 case HSM_ST_FIRST:
Albert Leebb5cb292006-03-25 17:48:02 +08004975 /* Send first data block or PACKET CDB */
4976
4977 /* If polling, we will stay in the work queue after
4978 * sending the data. Otherwise, interrupt handler
4979 * takes over after sending the data.
4980 */
4981 poll_next = (qc->tf.flags & ATA_TFLAG_POLLING);
4982
Albert Leee2cec772006-03-25 17:43:49 +08004983 /* check device status */
Albert Lee3655d1d2006-05-19 11:43:04 +08004984 if (unlikely((status & ATA_DRQ) == 0)) {
4985 /* handle BSY=0, DRQ=0 as error */
4986 if (likely(status & (ATA_ERR | ATA_DF)))
4987 /* device stops HSM for abort/error */
4988 qc->err_mask |= AC_ERR_DEV;
4989 else
4990 /* HSM violation. Let EH handle this */
4991 qc->err_mask |= AC_ERR_HSM;
4992
Albert Leee2cec772006-03-25 17:43:49 +08004993 ap->hsm_task_state = HSM_ST_ERR;
4994 goto fsm_start;
4995 }
4996
Albert Lee71601952006-03-25 18:11:12 +08004997 /* Device should not ask for data transfer (DRQ=1)
4998 * when it finds something wrong.
Albert Leeeee6c322006-04-01 17:38:43 +08004999 * We ignore DRQ here and stop the HSM by
5000 * changing hsm_task_state to HSM_ST_ERR and
5001 * let the EH abort the command or reset the device.
Albert Lee71601952006-03-25 18:11:12 +08005002 */
5003 if (unlikely(status & (ATA_ERR | ATA_DF))) {
Tejun Heo44877b42007-02-21 01:06:51 +09005004 ata_port_printk(ap, KERN_WARNING, "DRQ=1 with device "
5005 "error, dev_stat 0x%X\n", status);
Albert Lee3655d1d2006-05-19 11:43:04 +08005006 qc->err_mask |= AC_ERR_HSM;
Albert Leeeee6c322006-04-01 17:38:43 +08005007 ap->hsm_task_state = HSM_ST_ERR;
5008 goto fsm_start;
Albert Lee71601952006-03-25 18:11:12 +08005009 }
5010
Albert Leebb5cb292006-03-25 17:48:02 +08005011 /* Send the CDB (atapi) or the first data block (ata pio out).
5012 * During the state transition, interrupt handler shouldn't
5013 * be invoked before the data transfer is complete and
5014 * hsm_task_state is changed. Hence, the following locking.
5015 */
5016 if (in_wq)
Jeff Garzikba6a1302006-06-22 23:46:10 -04005017 spin_lock_irqsave(ap->lock, flags);
Albert Leee2cec772006-03-25 17:43:49 +08005018
Albert Leebb5cb292006-03-25 17:48:02 +08005019 if (qc->tf.protocol == ATA_PROT_PIO) {
5020 /* PIO data out protocol.
5021 * send first data block.
5022 */
5023
5024 /* ata_pio_sectors() might change the state
5025 * to HSM_ST_LAST. so, the state is changed here
5026 * before ata_pio_sectors().
5027 */
5028 ap->hsm_task_state = HSM_ST;
5029 ata_pio_sectors(qc);
Albert Leebb5cb292006-03-25 17:48:02 +08005030 } else
5031 /* send CDB */
5032 atapi_send_cdb(ap, qc);
5033
5034 if (in_wq)
Jeff Garzikba6a1302006-06-22 23:46:10 -04005035 spin_unlock_irqrestore(ap->lock, flags);
Albert Leebb5cb292006-03-25 17:48:02 +08005036
5037 /* if polling, ata_pio_task() handles the rest.
5038 * otherwise, interrupt handler takes over from here.
5039 */
Albert Leee2cec772006-03-25 17:43:49 +08005040 break;
5041
5042 case HSM_ST:
5043 /* complete command or read/write the data register */
5044 if (qc->tf.protocol == ATA_PROT_ATAPI) {
5045 /* ATAPI PIO protocol */
5046 if ((status & ATA_DRQ) == 0) {
Albert Lee3655d1d2006-05-19 11:43:04 +08005047 /* No more data to transfer or device error.
5048 * Device error will be tagged in HSM_ST_LAST.
5049 */
Albert Leee2cec772006-03-25 17:43:49 +08005050 ap->hsm_task_state = HSM_ST_LAST;
5051 goto fsm_start;
5052 }
5053
Albert Lee71601952006-03-25 18:11:12 +08005054 /* Device should not ask for data transfer (DRQ=1)
5055 * when it finds something wrong.
Albert Leeeee6c322006-04-01 17:38:43 +08005056 * We ignore DRQ here and stop the HSM by
5057 * changing hsm_task_state to HSM_ST_ERR and
5058 * let the EH abort the command or reset the device.
Albert Lee71601952006-03-25 18:11:12 +08005059 */
5060 if (unlikely(status & (ATA_ERR | ATA_DF))) {
Tejun Heo44877b42007-02-21 01:06:51 +09005061 ata_port_printk(ap, KERN_WARNING, "DRQ=1 with "
5062 "device error, dev_stat 0x%X\n",
5063 status);
Albert Lee3655d1d2006-05-19 11:43:04 +08005064 qc->err_mask |= AC_ERR_HSM;
Albert Leeeee6c322006-04-01 17:38:43 +08005065 ap->hsm_task_state = HSM_ST_ERR;
5066 goto fsm_start;
Albert Lee71601952006-03-25 18:11:12 +08005067 }
5068
Albert Leee2cec772006-03-25 17:43:49 +08005069 atapi_pio_bytes(qc);
5070
5071 if (unlikely(ap->hsm_task_state == HSM_ST_ERR))
5072 /* bad ireason reported by device */
5073 goto fsm_start;
5074
5075 } else {
5076 /* ATA PIO protocol */
5077 if (unlikely((status & ATA_DRQ) == 0)) {
5078 /* handle BSY=0, DRQ=0 as error */
Albert Lee3655d1d2006-05-19 11:43:04 +08005079 if (likely(status & (ATA_ERR | ATA_DF)))
5080 /* device stops HSM for abort/error */
5081 qc->err_mask |= AC_ERR_DEV;
5082 else
Tejun Heo55a8e2c2006-11-10 18:08:10 +09005083 /* HSM violation. Let EH handle this.
5084 * Phantom devices also trigger this
5085 * condition. Mark hint.
5086 */
5087 qc->err_mask |= AC_ERR_HSM |
5088 AC_ERR_NODEV_HINT;
Albert Lee3655d1d2006-05-19 11:43:04 +08005089
Albert Leee2cec772006-03-25 17:43:49 +08005090 ap->hsm_task_state = HSM_ST_ERR;
5091 goto fsm_start;
5092 }
5093
Albert Leeeee6c322006-04-01 17:38:43 +08005094 /* For PIO reads, some devices may ask for
5095 * data transfer (DRQ=1) alone with ERR=1.
5096 * We respect DRQ here and transfer one
5097 * block of junk data before changing the
5098 * hsm_task_state to HSM_ST_ERR.
5099 *
5100 * For PIO writes, ERR=1 DRQ=1 doesn't make
5101 * sense since the data block has been
5102 * transferred to the device.
Albert Lee71601952006-03-25 18:11:12 +08005103 */
5104 if (unlikely(status & (ATA_ERR | ATA_DF))) {
Albert Lee71601952006-03-25 18:11:12 +08005105 /* data might be corrputed */
5106 qc->err_mask |= AC_ERR_DEV;
Albert Leeeee6c322006-04-01 17:38:43 +08005107
5108 if (!(qc->tf.flags & ATA_TFLAG_WRITE)) {
5109 ata_pio_sectors(qc);
Albert Leeeee6c322006-04-01 17:38:43 +08005110 status = ata_wait_idle(ap);
5111 }
5112
Albert Lee3655d1d2006-05-19 11:43:04 +08005113 if (status & (ATA_BUSY | ATA_DRQ))
5114 qc->err_mask |= AC_ERR_HSM;
5115
Albert Leeeee6c322006-04-01 17:38:43 +08005116 /* ata_pio_sectors() might change the
5117 * state to HSM_ST_LAST. so, the state
5118 * is changed after ata_pio_sectors().
5119 */
5120 ap->hsm_task_state = HSM_ST_ERR;
5121 goto fsm_start;
Albert Lee71601952006-03-25 18:11:12 +08005122 }
5123
Albert Leee2cec772006-03-25 17:43:49 +08005124 ata_pio_sectors(qc);
5125
5126 if (ap->hsm_task_state == HSM_ST_LAST &&
5127 (!(qc->tf.flags & ATA_TFLAG_WRITE))) {
5128 /* all data read */
Albert Lee52a32202006-03-25 18:18:15 +08005129 status = ata_wait_idle(ap);
Albert Leee2cec772006-03-25 17:43:49 +08005130 goto fsm_start;
5131 }
5132 }
5133
Albert Leebb5cb292006-03-25 17:48:02 +08005134 poll_next = 1;
Albert Leee2cec772006-03-25 17:43:49 +08005135 break;
5136
5137 case HSM_ST_LAST:
Albert Lee6912ccd2006-03-25 17:45:49 +08005138 if (unlikely(!ata_ok(status))) {
5139 qc->err_mask |= __ac_err_mask(status);
Albert Leee2cec772006-03-25 17:43:49 +08005140 ap->hsm_task_state = HSM_ST_ERR;
5141 goto fsm_start;
5142 }
5143
5144 /* no more data to transfer */
Albert Lee4332a772006-04-03 17:43:24 +08005145 DPRINTK("ata%u: dev %u command complete, drv_stat 0x%x\n",
Tejun Heo44877b42007-02-21 01:06:51 +09005146 ap->print_id, qc->dev->devno, status);
Albert Leee2cec772006-03-25 17:43:49 +08005147
Albert Lee6912ccd2006-03-25 17:45:49 +08005148 WARN_ON(qc->err_mask);
5149
Albert Leee2cec772006-03-25 17:43:49 +08005150 ap->hsm_task_state = HSM_ST_IDLE;
5151
5152 /* complete taskfile transaction */
Tejun Heoc17ea202006-05-15 20:59:29 +09005153 ata_hsm_qc_complete(qc, in_wq);
Albert Leebb5cb292006-03-25 17:48:02 +08005154
5155 poll_next = 0;
Albert Leee2cec772006-03-25 17:43:49 +08005156 break;
5157
5158 case HSM_ST_ERR:
Albert Leee2cec772006-03-25 17:43:49 +08005159 /* make sure qc->err_mask is available to
5160 * know what's wrong and recover
5161 */
5162 WARN_ON(qc->err_mask == 0);
5163
5164 ap->hsm_task_state = HSM_ST_IDLE;
Albert Leebb5cb292006-03-25 17:48:02 +08005165
Albert Lee999bb6f2006-03-25 18:07:48 +08005166 /* complete taskfile transaction */
Tejun Heoc17ea202006-05-15 20:59:29 +09005167 ata_hsm_qc_complete(qc, in_wq);
Albert Leebb5cb292006-03-25 17:48:02 +08005168
5169 poll_next = 0;
Albert Leee2cec772006-03-25 17:43:49 +08005170 break;
5171 default:
Albert Leebb5cb292006-03-25 17:48:02 +08005172 poll_next = 0;
Albert Lee6912ccd2006-03-25 17:45:49 +08005173 BUG();
Albert Leee2cec772006-03-25 17:43:49 +08005174 }
5175
Albert Leebb5cb292006-03-25 17:48:02 +08005176 return poll_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005177}
5178
David Howells65f27f32006-11-22 14:55:48 +00005179static void ata_pio_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005180{
David Howells65f27f32006-11-22 14:55:48 +00005181 struct ata_port *ap =
5182 container_of(work, struct ata_port, port_task.work);
5183 struct ata_queued_cmd *qc = ap->port_task_data;
Albert Leea1af3732006-03-25 17:50:15 +08005184 u8 status;
5185 int poll_next;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04005186
5187fsm_start:
Albert Leea1af3732006-03-25 17:50:15 +08005188 WARN_ON(ap->hsm_task_state == HSM_ST_IDLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005189
Albert Leea1af3732006-03-25 17:50:15 +08005190 /*
5191 * This is purely heuristic. This is a fast path.
5192 * Sometimes when we enter, BSY will be cleared in
5193 * a chk-status or two. If not, the drive is probably seeking
5194 * or something. Snooze for a couple msecs, then
5195 * chk-status again. If still busy, queue delayed work.
5196 */
5197 status = ata_busy_wait(ap, ATA_BUSY, 5);
5198 if (status & ATA_BUSY) {
5199 msleep(2);
5200 status = ata_busy_wait(ap, ATA_BUSY, 10);
5201 if (status & ATA_BUSY) {
Albert Lee31ce6da2006-04-03 18:31:44 +08005202 ata_port_queue_task(ap, ata_pio_task, qc, ATA_SHORT_PAUSE);
Albert Leea1af3732006-03-25 17:50:15 +08005203 return;
5204 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205 }
5206
Albert Leea1af3732006-03-25 17:50:15 +08005207 /* move the HSM */
5208 poll_next = ata_hsm_move(ap, qc, status, 1);
5209
5210 /* another command or interrupt handler
5211 * may be running at this point.
5212 */
5213 if (poll_next)
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04005214 goto fsm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005215}
5216
Linus Torvalds1da177e2005-04-16 15:20:36 -07005217/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07005218 * ata_qc_new - Request an available ATA command, for queueing
5219 * @ap: Port associated with device @dev
5220 * @dev: Device from whom we request an available command structure
5221 *
5222 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04005223 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005224 */
5225
5226static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
5227{
5228 struct ata_queued_cmd *qc = NULL;
5229 unsigned int i;
5230
Tejun Heoe3180492006-05-15 20:58:09 +09005231 /* no command while frozen */
Tejun Heob51e9e52006-06-29 01:29:30 +09005232 if (unlikely(ap->pflags & ATA_PFLAG_FROZEN))
Tejun Heoe3180492006-05-15 20:58:09 +09005233 return NULL;
5234
Tejun Heo2ab7db12006-05-15 20:58:02 +09005235 /* the last tag is reserved for internal command. */
5236 for (i = 0; i < ATA_MAX_QUEUE - 1; i++)
Tejun Heo6cec4a32006-05-15 21:03:41 +09005237 if (!test_and_set_bit(i, &ap->qc_allocated)) {
Tejun Heof69499f2006-05-15 20:58:03 +09005238 qc = __ata_qc_from_tag(ap, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005239 break;
5240 }
5241
5242 if (qc)
5243 qc->tag = i;
5244
5245 return qc;
5246}
5247
5248/**
5249 * ata_qc_new_init - Request an available ATA command, and initialize it
Linus Torvalds1da177e2005-04-16 15:20:36 -07005250 * @dev: Device from whom we request an available command structure
5251 *
5252 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04005253 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005254 */
5255
Tejun Heo3373efd2006-05-15 20:57:53 +09005256struct ata_queued_cmd *ata_qc_new_init(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005257{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005258 struct ata_port *ap = dev->link->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005259 struct ata_queued_cmd *qc;
5260
5261 qc = ata_qc_new(ap);
5262 if (qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263 qc->scsicmd = NULL;
5264 qc->ap = ap;
5265 qc->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005266
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05005267 ata_qc_reinit(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005268 }
5269
5270 return qc;
5271}
5272
Linus Torvalds1da177e2005-04-16 15:20:36 -07005273/**
5274 * ata_qc_free - free unused ata_queued_cmd
5275 * @qc: Command to complete
5276 *
5277 * Designed to free unused ata_queued_cmd object
5278 * in case something prevents using it.
5279 *
5280 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04005281 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005282 */
5283void ata_qc_free(struct ata_queued_cmd *qc)
5284{
Tejun Heo4ba946e2006-01-23 13:09:36 +09005285 struct ata_port *ap = qc->ap;
5286 unsigned int tag;
5287
Tejun Heoa46314742006-02-11 19:11:13 +09005288 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005289
Tejun Heo4ba946e2006-01-23 13:09:36 +09005290 qc->flags = 0;
5291 tag = qc->tag;
5292 if (likely(ata_tag_valid(tag))) {
Tejun Heo4ba946e2006-01-23 13:09:36 +09005293 qc->tag = ATA_TAG_POISON;
Tejun Heo6cec4a32006-05-15 21:03:41 +09005294 clear_bit(tag, &ap->qc_allocated);
Tejun Heo4ba946e2006-01-23 13:09:36 +09005295 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005296}
5297
Tejun Heo76014422006-02-11 15:13:49 +09005298void __ata_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005299{
Tejun Heodedaf2b2006-05-15 21:03:43 +09005300 struct ata_port *ap = qc->ap;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005301 struct ata_link *link = qc->dev->link;
Tejun Heodedaf2b2006-05-15 21:03:43 +09005302
Tejun Heoa46314742006-02-11 19:11:13 +09005303 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
5304 WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005305
5306 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
5307 ata_sg_clean(qc);
5308
Tejun Heo7401abf2006-05-15 20:57:32 +09005309 /* command should be marked inactive atomically with qc completion */
Tejun Heodedaf2b2006-05-15 21:03:43 +09005310 if (qc->tf.protocol == ATA_PROT_NCQ)
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005311 link->sactive &= ~(1 << qc->tag);
Tejun Heodedaf2b2006-05-15 21:03:43 +09005312 else
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005313 link->active_tag = ATA_TAG_POISON;
Tejun Heo7401abf2006-05-15 20:57:32 +09005314
Albert Lee3f3791d2005-08-16 14:25:38 +08005315 /* atapi: mark qc as inactive to prevent the interrupt handler
5316 * from completing the command twice later, before the error handler
5317 * is called. (when rc != 0 and atapi request sense is needed)
5318 */
5319 qc->flags &= ~ATA_QCFLAG_ACTIVE;
Tejun Heodedaf2b2006-05-15 21:03:43 +09005320 ap->qc_active &= ~(1 << qc->tag);
Albert Lee3f3791d2005-08-16 14:25:38 +08005321
Linus Torvalds1da177e2005-04-16 15:20:36 -07005322 /* call completion callback */
Tejun Heo77853bf2006-01-23 13:09:36 +09005323 qc->complete_fn(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005324}
5325
Tejun Heo39599a52006-11-14 22:37:35 +09005326static void fill_result_tf(struct ata_queued_cmd *qc)
5327{
5328 struct ata_port *ap = qc->ap;
5329
Tejun Heo39599a52006-11-14 22:37:35 +09005330 qc->result_tf.flags = qc->tf.flags;
Mark Lord4742d542007-04-02 16:20:35 -04005331 ap->ops->tf_read(ap, &qc->result_tf);
Tejun Heo39599a52006-11-14 22:37:35 +09005332}
5333
Tejun Heof686bcb2006-05-15 20:58:05 +09005334/**
5335 * ata_qc_complete - Complete an active ATA command
5336 * @qc: Command to complete
5337 * @err_mask: ATA Status register contents
5338 *
5339 * Indicate to the mid and upper layers that an ATA
5340 * command has completed, with either an ok or not-ok status.
5341 *
5342 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04005343 * spin_lock_irqsave(host lock)
Tejun Heof686bcb2006-05-15 20:58:05 +09005344 */
5345void ata_qc_complete(struct ata_queued_cmd *qc)
5346{
5347 struct ata_port *ap = qc->ap;
5348
5349 /* XXX: New EH and old EH use different mechanisms to
5350 * synchronize EH with regular execution path.
5351 *
5352 * In new EH, a failed qc is marked with ATA_QCFLAG_FAILED.
5353 * Normal execution path is responsible for not accessing a
5354 * failed qc. libata core enforces the rule by returning NULL
5355 * from ata_qc_from_tag() for failed qcs.
5356 *
5357 * Old EH depends on ata_qc_complete() nullifying completion
5358 * requests if ATA_QCFLAG_EH_SCHEDULED is set. Old EH does
5359 * not synchronize with interrupt handler. Only PIO task is
5360 * taken care of.
5361 */
5362 if (ap->ops->error_handler) {
Tejun Heob51e9e52006-06-29 01:29:30 +09005363 WARN_ON(ap->pflags & ATA_PFLAG_FROZEN);
Tejun Heof686bcb2006-05-15 20:58:05 +09005364
5365 if (unlikely(qc->err_mask))
5366 qc->flags |= ATA_QCFLAG_FAILED;
5367
5368 if (unlikely(qc->flags & ATA_QCFLAG_FAILED)) {
5369 if (!ata_tag_internal(qc->tag)) {
5370 /* always fill result TF for failed qc */
Tejun Heo39599a52006-11-14 22:37:35 +09005371 fill_result_tf(qc);
Tejun Heof686bcb2006-05-15 20:58:05 +09005372 ata_qc_schedule_eh(qc);
5373 return;
5374 }
5375 }
5376
5377 /* read result TF if requested */
5378 if (qc->flags & ATA_QCFLAG_RESULT_TF)
Tejun Heo39599a52006-11-14 22:37:35 +09005379 fill_result_tf(qc);
Tejun Heof686bcb2006-05-15 20:58:05 +09005380
5381 __ata_qc_complete(qc);
5382 } else {
5383 if (qc->flags & ATA_QCFLAG_EH_SCHEDULED)
5384 return;
5385
5386 /* read result TF if failed or requested */
5387 if (qc->err_mask || qc->flags & ATA_QCFLAG_RESULT_TF)
Tejun Heo39599a52006-11-14 22:37:35 +09005388 fill_result_tf(qc);
Tejun Heof686bcb2006-05-15 20:58:05 +09005389
5390 __ata_qc_complete(qc);
5391 }
5392}
5393
Tejun Heodedaf2b2006-05-15 21:03:43 +09005394/**
5395 * ata_qc_complete_multiple - Complete multiple qcs successfully
5396 * @ap: port in question
5397 * @qc_active: new qc_active mask
5398 * @finish_qc: LLDD callback invoked before completing a qc
5399 *
5400 * Complete in-flight commands. This functions is meant to be
5401 * called from low-level driver's interrupt routine to complete
5402 * requests normally. ap->qc_active and @qc_active is compared
5403 * and commands are completed accordingly.
5404 *
5405 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04005406 * spin_lock_irqsave(host lock)
Tejun Heodedaf2b2006-05-15 21:03:43 +09005407 *
5408 * RETURNS:
5409 * Number of completed commands on success, -errno otherwise.
5410 */
5411int ata_qc_complete_multiple(struct ata_port *ap, u32 qc_active,
5412 void (*finish_qc)(struct ata_queued_cmd *))
5413{
5414 int nr_done = 0;
5415 u32 done_mask;
5416 int i;
5417
5418 done_mask = ap->qc_active ^ qc_active;
5419
5420 if (unlikely(done_mask & qc_active)) {
5421 ata_port_printk(ap, KERN_ERR, "illegal qc_active transition "
5422 "(%08x->%08x)\n", ap->qc_active, qc_active);
5423 return -EINVAL;
5424 }
5425
5426 for (i = 0; i < ATA_MAX_QUEUE; i++) {
5427 struct ata_queued_cmd *qc;
5428
5429 if (!(done_mask & (1 << i)))
5430 continue;
5431
5432 if ((qc = ata_qc_from_tag(ap, i))) {
5433 if (finish_qc)
5434 finish_qc(qc);
5435 ata_qc_complete(qc);
5436 nr_done++;
5437 }
5438 }
5439
5440 return nr_done;
5441}
5442
Linus Torvalds1da177e2005-04-16 15:20:36 -07005443static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
5444{
5445 struct ata_port *ap = qc->ap;
5446
5447 switch (qc->tf.protocol) {
Tejun Heo3dc1d882006-05-15 21:03:45 +09005448 case ATA_PROT_NCQ:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005449 case ATA_PROT_DMA:
5450 case ATA_PROT_ATAPI_DMA:
5451 return 1;
5452
5453 case ATA_PROT_ATAPI:
5454 case ATA_PROT_PIO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005455 if (ap->flags & ATA_FLAG_PIO_DMA)
5456 return 1;
5457
5458 /* fall through */
5459
5460 default:
5461 return 0;
5462 }
5463
5464 /* never reached */
5465}
5466
5467/**
5468 * ata_qc_issue - issue taskfile to device
5469 * @qc: command to issue to device
5470 *
5471 * Prepare an ATA command to submission to device.
5472 * This includes mapping the data into a DMA-able
5473 * area, filling in the S/G table, and finally
5474 * writing the taskfile to hardware, starting the command.
5475 *
5476 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04005477 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005478 */
Tejun Heo8e0e6942006-03-31 20:41:11 +09005479void ata_qc_issue(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005480{
5481 struct ata_port *ap = qc->ap;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005482 struct ata_link *link = qc->dev->link;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483
Tejun Heodedaf2b2006-05-15 21:03:43 +09005484 /* Make sure only one non-NCQ command is outstanding. The
5485 * check is skipped for old EH because it reuses active qc to
5486 * request ATAPI sense.
5487 */
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005488 WARN_ON(ap->ops->error_handler && ata_tag_valid(link->active_tag));
Tejun Heodedaf2b2006-05-15 21:03:43 +09005489
5490 if (qc->tf.protocol == ATA_PROT_NCQ) {
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005491 WARN_ON(link->sactive & (1 << qc->tag));
5492 link->sactive |= 1 << qc->tag;
Tejun Heodedaf2b2006-05-15 21:03:43 +09005493 } else {
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005494 WARN_ON(link->sactive);
5495 link->active_tag = qc->tag;
Tejun Heodedaf2b2006-05-15 21:03:43 +09005496 }
5497
Tejun Heoe4a70e72006-03-31 20:36:47 +09005498 qc->flags |= ATA_QCFLAG_ACTIVE;
Tejun Heodedaf2b2006-05-15 21:03:43 +09005499 ap->qc_active |= 1 << qc->tag;
Tejun Heoe4a70e72006-03-31 20:36:47 +09005500
Linus Torvalds1da177e2005-04-16 15:20:36 -07005501 if (ata_should_dma_map(qc)) {
5502 if (qc->flags & ATA_QCFLAG_SG) {
5503 if (ata_sg_setup(qc))
Tejun Heo8e436af2006-01-23 13:09:36 +09005504 goto sg_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005505 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
5506 if (ata_sg_setup_one(qc))
Tejun Heo8e436af2006-01-23 13:09:36 +09005507 goto sg_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005508 }
5509 } else {
5510 qc->flags &= ~ATA_QCFLAG_DMAMAP;
5511 }
5512
5513 ap->ops->qc_prep(qc);
5514
Tejun Heo8e0e6942006-03-31 20:41:11 +09005515 qc->err_mask |= ap->ops->qc_issue(qc);
5516 if (unlikely(qc->err_mask))
5517 goto err;
5518 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005519
Tejun Heo8e436af2006-01-23 13:09:36 +09005520sg_err:
5521 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Tejun Heo8e0e6942006-03-31 20:41:11 +09005522 qc->err_mask |= AC_ERR_SYSTEM;
5523err:
5524 ata_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005525}
5526
5527/**
5528 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
5529 * @qc: command to issue to device
5530 *
5531 * Using various libata functions and hooks, this function
5532 * starts an ATA command. ATA commands are grouped into
5533 * classes called "protocols", and issuing each type of protocol
5534 * is slightly different.
5535 *
Edward Falk0baab862005-06-02 18:17:13 -04005536 * May be used as the qc_issue() entry in ata_port_operations.
5537 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005538 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04005539 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005540 *
5541 * RETURNS:
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09005542 * Zero on success, AC_ERR_* mask on failure
Linus Torvalds1da177e2005-04-16 15:20:36 -07005543 */
5544
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09005545unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005546{
5547 struct ata_port *ap = qc->ap;
5548
Albert Leee50362e2005-09-27 17:39:50 +08005549 /* Use polling pio if the LLD doesn't handle
5550 * interrupt driven pio and atapi CDB interrupt.
5551 */
5552 if (ap->flags & ATA_FLAG_PIO_POLLING) {
5553 switch (qc->tf.protocol) {
5554 case ATA_PROT_PIO:
Albert Leee3472cb2006-12-07 11:37:58 +08005555 case ATA_PROT_NODATA:
Albert Leee50362e2005-09-27 17:39:50 +08005556 case ATA_PROT_ATAPI:
5557 case ATA_PROT_ATAPI_NODATA:
5558 qc->tf.flags |= ATA_TFLAG_POLLING;
5559 break;
5560 case ATA_PROT_ATAPI_DMA:
5561 if (qc->dev->flags & ATA_DFLAG_CDB_INTR)
Albert Lee3a778272006-06-22 13:00:25 +08005562 /* see ata_dma_blacklisted() */
Albert Leee50362e2005-09-27 17:39:50 +08005563 BUG();
5564 break;
5565 default:
5566 break;
5567 }
5568 }
5569
Albert Lee312f7da2005-09-27 17:38:03 +08005570 /* select the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005571 ata_dev_select(ap, qc->dev->devno, 1, 0);
5572
Albert Lee312f7da2005-09-27 17:38:03 +08005573 /* start the command */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005574 switch (qc->tf.protocol) {
5575 case ATA_PROT_NODATA:
Albert Lee312f7da2005-09-27 17:38:03 +08005576 if (qc->tf.flags & ATA_TFLAG_POLLING)
5577 ata_qc_set_polling(qc);
5578
Jeff Garzike5338252005-10-30 21:37:17 -05005579 ata_tf_to_host(ap, &qc->tf);
Albert Lee312f7da2005-09-27 17:38:03 +08005580 ap->hsm_task_state = HSM_ST_LAST;
5581
5582 if (qc->tf.flags & ATA_TFLAG_POLLING)
Albert Lee31ce6da2006-04-03 18:31:44 +08005583 ata_port_queue_task(ap, ata_pio_task, qc, 0);
Albert Lee312f7da2005-09-27 17:38:03 +08005584
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585 break;
5586
5587 case ATA_PROT_DMA:
Jeff Garzik587005d2006-02-11 18:17:32 -05005588 WARN_ON(qc->tf.flags & ATA_TFLAG_POLLING);
Albert Lee312f7da2005-09-27 17:38:03 +08005589
Linus Torvalds1da177e2005-04-16 15:20:36 -07005590 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
5591 ap->ops->bmdma_setup(qc); /* set up bmdma */
5592 ap->ops->bmdma_start(qc); /* initiate bmdma */
Albert Lee312f7da2005-09-27 17:38:03 +08005593 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005594 break;
5595
Albert Lee312f7da2005-09-27 17:38:03 +08005596 case ATA_PROT_PIO:
5597 if (qc->tf.flags & ATA_TFLAG_POLLING)
5598 ata_qc_set_polling(qc);
5599
Jeff Garzike5338252005-10-30 21:37:17 -05005600 ata_tf_to_host(ap, &qc->tf);
Albert Lee312f7da2005-09-27 17:38:03 +08005601
Albert Lee54f00382005-09-30 19:14:19 +08005602 if (qc->tf.flags & ATA_TFLAG_WRITE) {
5603 /* PIO data out protocol */
5604 ap->hsm_task_state = HSM_ST_FIRST;
Albert Lee31ce6da2006-04-03 18:31:44 +08005605 ata_port_queue_task(ap, ata_pio_task, qc, 0);
Albert Lee54f00382005-09-30 19:14:19 +08005606
5607 /* always send first data block using
Albert Leee27486d2005-11-01 19:24:49 +08005608 * the ata_pio_task() codepath.
Albert Lee54f00382005-09-30 19:14:19 +08005609 */
Albert Lee312f7da2005-09-27 17:38:03 +08005610 } else {
Albert Lee54f00382005-09-30 19:14:19 +08005611 /* PIO data in protocol */
5612 ap->hsm_task_state = HSM_ST;
Albert Lee312f7da2005-09-27 17:38:03 +08005613
Albert Lee54f00382005-09-30 19:14:19 +08005614 if (qc->tf.flags & ATA_TFLAG_POLLING)
Albert Lee31ce6da2006-04-03 18:31:44 +08005615 ata_port_queue_task(ap, ata_pio_task, qc, 0);
Albert Lee312f7da2005-09-27 17:38:03 +08005616
Albert Lee54f00382005-09-30 19:14:19 +08005617 /* if polling, ata_pio_task() handles the rest.
5618 * otherwise, interrupt handler takes over from here.
5619 */
Albert Lee312f7da2005-09-27 17:38:03 +08005620 }
5621
Linus Torvalds1da177e2005-04-16 15:20:36 -07005622 break;
5623
5624 case ATA_PROT_ATAPI:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005625 case ATA_PROT_ATAPI_NODATA:
Albert Lee312f7da2005-09-27 17:38:03 +08005626 if (qc->tf.flags & ATA_TFLAG_POLLING)
5627 ata_qc_set_polling(qc);
5628
Jeff Garzike5338252005-10-30 21:37:17 -05005629 ata_tf_to_host(ap, &qc->tf);
Jeff Garzikf6ef65e2006-01-27 02:45:00 -05005630
Albert Lee312f7da2005-09-27 17:38:03 +08005631 ap->hsm_task_state = HSM_ST_FIRST;
5632
5633 /* send cdb by polling if no cdb interrupt */
5634 if ((!(qc->dev->flags & ATA_DFLAG_CDB_INTR)) ||
5635 (qc->tf.flags & ATA_TFLAG_POLLING))
Albert Lee31ce6da2006-04-03 18:31:44 +08005636 ata_port_queue_task(ap, ata_pio_task, qc, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005637 break;
5638
5639 case ATA_PROT_ATAPI_DMA:
Jeff Garzik587005d2006-02-11 18:17:32 -05005640 WARN_ON(qc->tf.flags & ATA_TFLAG_POLLING);
Albert Lee312f7da2005-09-27 17:38:03 +08005641
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
5643 ap->ops->bmdma_setup(qc); /* set up bmdma */
Albert Lee312f7da2005-09-27 17:38:03 +08005644 ap->hsm_task_state = HSM_ST_FIRST;
5645
5646 /* send cdb by polling if no cdb interrupt */
5647 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
Albert Lee31ce6da2006-04-03 18:31:44 +08005648 ata_port_queue_task(ap, ata_pio_task, qc, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005649 break;
5650
5651 default:
5652 WARN_ON(1);
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09005653 return AC_ERR_SYSTEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005654 }
5655
5656 return 0;
5657}
5658
5659/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07005660 * ata_host_intr - Handle host interrupt for given (port, task)
5661 * @ap: Port on which interrupt arrived (possibly...)
5662 * @qc: Taskfile currently active in engine
5663 *
5664 * Handle host interrupt for given queued command. Currently,
5665 * only DMA interrupts are handled. All other commands are
5666 * handled via polling with interrupts disabled (nIEN bit).
5667 *
5668 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04005669 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005670 *
5671 * RETURNS:
5672 * One if interrupt was handled, zero if not (shared irq).
5673 */
5674
5675inline unsigned int ata_host_intr (struct ata_port *ap,
5676 struct ata_queued_cmd *qc)
5677{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005678 struct ata_eh_info *ehi = &ap->link.eh_info;
Albert Lee312f7da2005-09-27 17:38:03 +08005679 u8 status, host_stat = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680
Albert Lee312f7da2005-09-27 17:38:03 +08005681 VPRINTK("ata%u: protocol %d task_state %d\n",
Tejun Heo44877b42007-02-21 01:06:51 +09005682 ap->print_id, qc->tf.protocol, ap->hsm_task_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005683
Albert Lee312f7da2005-09-27 17:38:03 +08005684 /* Check whether we are expecting interrupt in this state */
5685 switch (ap->hsm_task_state) {
5686 case HSM_ST_FIRST:
Albert Lee6912ccd2006-03-25 17:45:49 +08005687 /* Some pre-ATAPI-4 devices assert INTRQ
5688 * at this state when ready to receive CDB.
5689 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005690
Albert Lee312f7da2005-09-27 17:38:03 +08005691 /* Check the ATA_DFLAG_CDB_INTR flag is enough here.
5692 * The flag was turned on only for atapi devices.
5693 * No need to check is_atapi_taskfile(&qc->tf) again.
5694 */
5695 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005696 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005697 break;
Albert Lee312f7da2005-09-27 17:38:03 +08005698 case HSM_ST_LAST:
5699 if (qc->tf.protocol == ATA_PROT_DMA ||
5700 qc->tf.protocol == ATA_PROT_ATAPI_DMA) {
5701 /* check status of DMA engine */
5702 host_stat = ap->ops->bmdma_status(ap);
Tejun Heo44877b42007-02-21 01:06:51 +09005703 VPRINTK("ata%u: host_stat 0x%X\n",
5704 ap->print_id, host_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005705
Albert Lee312f7da2005-09-27 17:38:03 +08005706 /* if it's not our irq... */
5707 if (!(host_stat & ATA_DMA_INTR))
5708 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709
Albert Lee312f7da2005-09-27 17:38:03 +08005710 /* before we do anything else, clear DMA-Start bit */
5711 ap->ops->bmdma_stop(qc);
Albert Leea4f16612005-12-26 16:40:53 +08005712
5713 if (unlikely(host_stat & ATA_DMA_ERR)) {
5714 /* error when transfering data to/from memory */
5715 qc->err_mask |= AC_ERR_HOST_BUS;
5716 ap->hsm_task_state = HSM_ST_ERR;
5717 }
Albert Lee312f7da2005-09-27 17:38:03 +08005718 }
5719 break;
5720 case HSM_ST:
5721 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005722 default:
5723 goto idle_irq;
5724 }
5725
Albert Lee312f7da2005-09-27 17:38:03 +08005726 /* check altstatus */
5727 status = ata_altstatus(ap);
5728 if (status & ATA_BUSY)
5729 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005730
Albert Lee312f7da2005-09-27 17:38:03 +08005731 /* check main status, clearing INTRQ */
5732 status = ata_chk_status(ap);
5733 if (unlikely(status & ATA_BUSY))
5734 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005735
Albert Lee312f7da2005-09-27 17:38:03 +08005736 /* ack bmdma irq events */
5737 ap->ops->irq_clear(ap);
5738
Albert Leebb5cb292006-03-25 17:48:02 +08005739 ata_hsm_move(ap, qc, status, 0);
Tejun Heoea547632006-11-17 12:06:21 +09005740
5741 if (unlikely(qc->err_mask) && (qc->tf.protocol == ATA_PROT_DMA ||
5742 qc->tf.protocol == ATA_PROT_ATAPI_DMA))
5743 ata_ehi_push_desc(ehi, "BMDMA stat 0x%x", host_stat);
5744
Linus Torvalds1da177e2005-04-16 15:20:36 -07005745 return 1; /* irq handled */
5746
5747idle_irq:
5748 ap->stats.idle_irq++;
5749
5750#ifdef ATA_IRQ_TRAP
5751 if ((ap->stats.idle_irq % 1000) == 0) {
Jeff Garzik6d32d302007-08-15 05:38:46 -04005752 ata_chk_status(ap);
5753 ap->ops->irq_clear(ap);
Tejun Heof15a1da2006-05-15 20:57:56 +09005754 ata_port_printk(ap, KERN_WARNING, "irq trap\n");
Alan Cox23cfce82006-03-21 16:06:53 +00005755 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005756 }
5757#endif
5758 return 0; /* irq not handled */
5759}
5760
5761/**
5762 * ata_interrupt - Default ATA host interrupt handler
Jeff Garzik0cba6322005-05-30 19:49:12 -04005763 * @irq: irq line (unused)
Jeff Garzikcca39742006-08-24 03:19:22 -04005764 * @dev_instance: pointer to our ata_host information structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07005765 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04005766 * Default interrupt handler for PCI IDE devices. Calls
5767 * ata_host_intr() for each port that is not disabled.
5768 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005769 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04005770 * Obtains host lock during operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005771 *
5772 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04005773 * IRQ_NONE or IRQ_HANDLED.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005774 */
5775
David Howells7d12e782006-10-05 14:55:46 +01005776irqreturn_t ata_interrupt (int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005777{
Jeff Garzikcca39742006-08-24 03:19:22 -04005778 struct ata_host *host = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005779 unsigned int i;
5780 unsigned int handled = 0;
5781 unsigned long flags;
5782
5783 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
Jeff Garzikcca39742006-08-24 03:19:22 -04005784 spin_lock_irqsave(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005785
Jeff Garzikcca39742006-08-24 03:19:22 -04005786 for (i = 0; i < host->n_ports; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005787 struct ata_port *ap;
5788
Jeff Garzikcca39742006-08-24 03:19:22 -04005789 ap = host->ports[i];
Tejun Heoc1389502005-08-22 14:59:24 +09005790 if (ap &&
Jeff Garzik029f5462006-04-02 10:30:40 -04005791 !(ap->flags & ATA_FLAG_DISABLED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005792 struct ata_queued_cmd *qc;
5793
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005794 qc = ata_qc_from_tag(ap, ap->link.active_tag);
Albert Lee312f7da2005-09-27 17:38:03 +08005795 if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING)) &&
Albert Lee21b1ed72005-04-29 17:34:59 +08005796 (qc->flags & ATA_QCFLAG_ACTIVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005797 handled |= ata_host_intr(ap, qc);
5798 }
5799 }
5800
Jeff Garzikcca39742006-08-24 03:19:22 -04005801 spin_unlock_irqrestore(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005802
5803 return IRQ_RETVAL(handled);
5804}
5805
Tejun Heo34bf2172006-05-15 20:57:46 +09005806/**
5807 * sata_scr_valid - test whether SCRs are accessible
Tejun Heo936fd732007-08-06 18:36:23 +09005808 * @link: ATA link to test SCR accessibility for
Tejun Heo34bf2172006-05-15 20:57:46 +09005809 *
Tejun Heo936fd732007-08-06 18:36:23 +09005810 * Test whether SCRs are accessible for @link.
Tejun Heo34bf2172006-05-15 20:57:46 +09005811 *
5812 * LOCKING:
5813 * None.
5814 *
5815 * RETURNS:
5816 * 1 if SCRs are accessible, 0 otherwise.
5817 */
Tejun Heo936fd732007-08-06 18:36:23 +09005818int sata_scr_valid(struct ata_link *link)
Tejun Heo34bf2172006-05-15 20:57:46 +09005819{
Tejun Heo936fd732007-08-06 18:36:23 +09005820 struct ata_port *ap = link->ap;
5821
Tejun Heoa16abc02007-05-21 18:33:47 +02005822 return (ap->flags & ATA_FLAG_SATA) && ap->ops->scr_read;
Tejun Heo34bf2172006-05-15 20:57:46 +09005823}
5824
5825/**
5826 * sata_scr_read - read SCR register of the specified port
Tejun Heo936fd732007-08-06 18:36:23 +09005827 * @link: ATA link to read SCR for
Tejun Heo34bf2172006-05-15 20:57:46 +09005828 * @reg: SCR to read
5829 * @val: Place to store read value
5830 *
Tejun Heo936fd732007-08-06 18:36:23 +09005831 * Read SCR register @reg of @link into *@val. This function is
Tejun Heo34bf2172006-05-15 20:57:46 +09005832 * guaranteed to succeed if the cable type of the port is SATA
5833 * and the port implements ->scr_read.
5834 *
5835 * LOCKING:
5836 * None.
5837 *
5838 * RETURNS:
5839 * 0 on success, negative errno on failure.
5840 */
Tejun Heo936fd732007-08-06 18:36:23 +09005841int sata_scr_read(struct ata_link *link, int reg, u32 *val)
Tejun Heo34bf2172006-05-15 20:57:46 +09005842{
Tejun Heo936fd732007-08-06 18:36:23 +09005843 struct ata_port *ap = link->ap;
5844
5845 if (sata_scr_valid(link))
Tejun Heoda3dbb12007-07-16 14:29:40 +09005846 return ap->ops->scr_read(ap, reg, val);
Tejun Heo34bf2172006-05-15 20:57:46 +09005847 return -EOPNOTSUPP;
5848}
5849
5850/**
5851 * sata_scr_write - write SCR register of the specified port
Tejun Heo936fd732007-08-06 18:36:23 +09005852 * @link: ATA link to write SCR for
Tejun Heo34bf2172006-05-15 20:57:46 +09005853 * @reg: SCR to write
5854 * @val: value to write
5855 *
Tejun Heo936fd732007-08-06 18:36:23 +09005856 * Write @val to SCR register @reg of @link. This function is
Tejun Heo34bf2172006-05-15 20:57:46 +09005857 * guaranteed to succeed if the cable type of the port is SATA
5858 * and the port implements ->scr_read.
5859 *
5860 * LOCKING:
5861 * None.
5862 *
5863 * RETURNS:
5864 * 0 on success, negative errno on failure.
5865 */
Tejun Heo936fd732007-08-06 18:36:23 +09005866int sata_scr_write(struct ata_link *link, int reg, u32 val)
Tejun Heo34bf2172006-05-15 20:57:46 +09005867{
Tejun Heo936fd732007-08-06 18:36:23 +09005868 struct ata_port *ap = link->ap;
5869
5870 if (sata_scr_valid(link))
Tejun Heoda3dbb12007-07-16 14:29:40 +09005871 return ap->ops->scr_write(ap, reg, val);
Tejun Heo34bf2172006-05-15 20:57:46 +09005872 return -EOPNOTSUPP;
5873}
5874
5875/**
5876 * sata_scr_write_flush - write SCR register of the specified port and flush
Tejun Heo936fd732007-08-06 18:36:23 +09005877 * @link: ATA link to write SCR for
Tejun Heo34bf2172006-05-15 20:57:46 +09005878 * @reg: SCR to write
5879 * @val: value to write
5880 *
5881 * This function is identical to sata_scr_write() except that this
5882 * function performs flush after writing to the register.
5883 *
5884 * LOCKING:
5885 * None.
5886 *
5887 * RETURNS:
5888 * 0 on success, negative errno on failure.
5889 */
Tejun Heo936fd732007-08-06 18:36:23 +09005890int sata_scr_write_flush(struct ata_link *link, int reg, u32 val)
Tejun Heo34bf2172006-05-15 20:57:46 +09005891{
Tejun Heo936fd732007-08-06 18:36:23 +09005892 struct ata_port *ap = link->ap;
Tejun Heoda3dbb12007-07-16 14:29:40 +09005893 int rc;
5894
Tejun Heo936fd732007-08-06 18:36:23 +09005895 if (sata_scr_valid(link)) {
Tejun Heoda3dbb12007-07-16 14:29:40 +09005896 rc = ap->ops->scr_write(ap, reg, val);
5897 if (rc == 0)
5898 rc = ap->ops->scr_read(ap, reg, &val);
5899 return rc;
Tejun Heo34bf2172006-05-15 20:57:46 +09005900 }
5901 return -EOPNOTSUPP;
5902}
5903
5904/**
Tejun Heo936fd732007-08-06 18:36:23 +09005905 * ata_link_online - test whether the given link is online
5906 * @link: ATA link to test
Tejun Heo34bf2172006-05-15 20:57:46 +09005907 *
Tejun Heo936fd732007-08-06 18:36:23 +09005908 * Test whether @link is online. Note that this function returns
5909 * 0 if online status of @link cannot be obtained, so
5910 * ata_link_online(link) != !ata_link_offline(link).
Tejun Heo34bf2172006-05-15 20:57:46 +09005911 *
5912 * LOCKING:
5913 * None.
5914 *
5915 * RETURNS:
5916 * 1 if the port online status is available and online.
5917 */
Tejun Heo936fd732007-08-06 18:36:23 +09005918int ata_link_online(struct ata_link *link)
Tejun Heo34bf2172006-05-15 20:57:46 +09005919{
5920 u32 sstatus;
5921
Tejun Heo936fd732007-08-06 18:36:23 +09005922 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
5923 (sstatus & 0xf) == 0x3)
Tejun Heo34bf2172006-05-15 20:57:46 +09005924 return 1;
5925 return 0;
5926}
5927
5928/**
Tejun Heo936fd732007-08-06 18:36:23 +09005929 * ata_link_offline - test whether the given link is offline
5930 * @link: ATA link to test
Tejun Heo34bf2172006-05-15 20:57:46 +09005931 *
Tejun Heo936fd732007-08-06 18:36:23 +09005932 * Test whether @link is offline. Note that this function
5933 * returns 0 if offline status of @link cannot be obtained, so
5934 * ata_link_online(link) != !ata_link_offline(link).
Tejun Heo34bf2172006-05-15 20:57:46 +09005935 *
5936 * LOCKING:
5937 * None.
5938 *
5939 * RETURNS:
5940 * 1 if the port offline status is available and offline.
5941 */
Tejun Heo936fd732007-08-06 18:36:23 +09005942int ata_link_offline(struct ata_link *link)
Tejun Heo34bf2172006-05-15 20:57:46 +09005943{
5944 u32 sstatus;
5945
Tejun Heo936fd732007-08-06 18:36:23 +09005946 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
5947 (sstatus & 0xf) != 0x3)
Tejun Heo34bf2172006-05-15 20:57:46 +09005948 return 1;
5949 return 0;
5950}
Edward Falk0baab862005-06-02 18:17:13 -04005951
Tejun Heo77b08fb2006-06-24 20:30:19 +09005952int ata_flush_cache(struct ata_device *dev)
Jens Axboe9b847542006-01-06 09:28:07 +01005953{
Tejun Heo977e6b92006-06-24 20:30:19 +09005954 unsigned int err_mask;
Jens Axboe9b847542006-01-06 09:28:07 +01005955 u8 cmd;
5956
5957 if (!ata_try_flush_cache(dev))
5958 return 0;
5959
Tejun Heo6fc49ad2006-11-11 20:10:45 +09005960 if (dev->flags & ATA_DFLAG_FLUSH_EXT)
Jens Axboe9b847542006-01-06 09:28:07 +01005961 cmd = ATA_CMD_FLUSH_EXT;
5962 else
5963 cmd = ATA_CMD_FLUSH;
5964
Alan Cox4f343372007-08-08 14:30:31 +01005965 /* This is wrong. On a failed flush we get back the LBA of the lost
5966 sector and we should (assuming it wasn't aborted as unknown) issue
5967 a further flush command to continue the writeback until it
5968 does not error */
Tejun Heo977e6b92006-06-24 20:30:19 +09005969 err_mask = ata_do_simple_cmd(dev, cmd);
5970 if (err_mask) {
5971 ata_dev_printk(dev, KERN_ERR, "failed to flush cache\n");
5972 return -EIO;
5973 }
5974
5975 return 0;
Jens Axboe9b847542006-01-06 09:28:07 +01005976}
5977
Tejun Heo6ffa01d2007-03-02 17:32:47 +09005978#ifdef CONFIG_PM
Jeff Garzikcca39742006-08-24 03:19:22 -04005979static int ata_host_request_pm(struct ata_host *host, pm_message_t mesg,
5980 unsigned int action, unsigned int ehi_flags,
5981 int wait)
Tejun Heo500530f2006-07-03 16:07:27 +09005982{
5983 unsigned long flags;
5984 int i, rc;
5985
Jeff Garzikcca39742006-08-24 03:19:22 -04005986 for (i = 0; i < host->n_ports; i++) {
5987 struct ata_port *ap = host->ports[i];
Tejun Heoe3667eb2007-08-06 18:36:24 +09005988 struct ata_link *link;
Tejun Heo500530f2006-07-03 16:07:27 +09005989
5990 /* Previous resume operation might still be in
5991 * progress. Wait for PM_PENDING to clear.
5992 */
5993 if (ap->pflags & ATA_PFLAG_PM_PENDING) {
5994 ata_port_wait_eh(ap);
5995 WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
5996 }
5997
5998 /* request PM ops to EH */
5999 spin_lock_irqsave(ap->lock, flags);
6000
6001 ap->pm_mesg = mesg;
6002 if (wait) {
6003 rc = 0;
6004 ap->pm_result = &rc;
6005 }
6006
6007 ap->pflags |= ATA_PFLAG_PM_PENDING;
Tejun Heoe3667eb2007-08-06 18:36:24 +09006008 __ata_port_for_each_link(link, ap) {
6009 link->eh_info.action |= action;
6010 link->eh_info.flags |= ehi_flags;
6011 }
Tejun Heo500530f2006-07-03 16:07:27 +09006012
6013 ata_port_schedule_eh(ap);
6014
6015 spin_unlock_irqrestore(ap->lock, flags);
6016
6017 /* wait and check result */
6018 if (wait) {
6019 ata_port_wait_eh(ap);
6020 WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
6021 if (rc)
6022 return rc;
6023 }
6024 }
6025
6026 return 0;
6027}
6028
6029/**
Jeff Garzikcca39742006-08-24 03:19:22 -04006030 * ata_host_suspend - suspend host
6031 * @host: host to suspend
Tejun Heo500530f2006-07-03 16:07:27 +09006032 * @mesg: PM message
6033 *
Jeff Garzikcca39742006-08-24 03:19:22 -04006034 * Suspend @host. Actual operation is performed by EH. This
Tejun Heo500530f2006-07-03 16:07:27 +09006035 * function requests EH to perform PM operations and waits for EH
6036 * to finish.
6037 *
6038 * LOCKING:
6039 * Kernel thread context (may sleep).
6040 *
6041 * RETURNS:
6042 * 0 on success, -errno on failure.
6043 */
Jeff Garzikcca39742006-08-24 03:19:22 -04006044int ata_host_suspend(struct ata_host *host, pm_message_t mesg)
Tejun Heo500530f2006-07-03 16:07:27 +09006045{
Tejun Heo9666f402007-05-04 21:27:47 +02006046 int rc;
Tejun Heo500530f2006-07-03 16:07:27 +09006047
Jeff Garzikcca39742006-08-24 03:19:22 -04006048 rc = ata_host_request_pm(host, mesg, 0, ATA_EHI_QUIET, 1);
Tejun Heo9666f402007-05-04 21:27:47 +02006049 if (rc == 0)
6050 host->dev->power.power_state = mesg;
Tejun Heo500530f2006-07-03 16:07:27 +09006051 return rc;
6052}
6053
6054/**
Jeff Garzikcca39742006-08-24 03:19:22 -04006055 * ata_host_resume - resume host
6056 * @host: host to resume
Tejun Heo500530f2006-07-03 16:07:27 +09006057 *
Jeff Garzikcca39742006-08-24 03:19:22 -04006058 * Resume @host. Actual operation is performed by EH. This
Tejun Heo500530f2006-07-03 16:07:27 +09006059 * function requests EH to perform PM operations and returns.
6060 * Note that all resume operations are performed parallely.
6061 *
6062 * LOCKING:
6063 * Kernel thread context (may sleep).
6064 */
Jeff Garzikcca39742006-08-24 03:19:22 -04006065void ata_host_resume(struct ata_host *host)
Tejun Heo500530f2006-07-03 16:07:27 +09006066{
Jeff Garzikcca39742006-08-24 03:19:22 -04006067 ata_host_request_pm(host, PMSG_ON, ATA_EH_SOFTRESET,
6068 ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET, 0);
6069 host->dev->power.power_state = PMSG_ON;
Tejun Heo500530f2006-07-03 16:07:27 +09006070}
Tejun Heo6ffa01d2007-03-02 17:32:47 +09006071#endif
Tejun Heo500530f2006-07-03 16:07:27 +09006072
Randy Dunlapc893a3a2006-01-28 13:15:32 -05006073/**
6074 * ata_port_start - Set port up for dma.
6075 * @ap: Port to initialize
6076 *
6077 * Called just after data structures for each port are
6078 * initialized. Allocates space for PRD table.
6079 *
6080 * May be used as the port_start() entry in ata_port_operations.
6081 *
6082 * LOCKING:
6083 * Inherited from caller.
6084 */
Tejun Heof0d36ef2007-01-20 16:00:28 +09006085int ata_port_start(struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006086{
Brian King2f1f6102006-03-23 17:30:15 -06006087 struct device *dev = ap->dev;
Jeff Garzik6037d6b2005-11-04 22:08:00 -05006088 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006089
Tejun Heof0d36ef2007-01-20 16:00:28 +09006090 ap->prd = dmam_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma,
6091 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006092 if (!ap->prd)
6093 return -ENOMEM;
6094
Jeff Garzik6037d6b2005-11-04 22:08:00 -05006095 rc = ata_pad_alloc(ap, dev);
Tejun Heof0d36ef2007-01-20 16:00:28 +09006096 if (rc)
Jeff Garzik6037d6b2005-11-04 22:08:00 -05006097 return rc;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04006098
Tejun Heof0d36ef2007-01-20 16:00:28 +09006099 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd,
6100 (unsigned long long)ap->prd_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006101 return 0;
6102}
6103
Edward Falk0baab862005-06-02 18:17:13 -04006104/**
Tejun Heo3ef3b432006-05-31 18:27:30 +09006105 * ata_dev_init - Initialize an ata_device structure
6106 * @dev: Device structure to initialize
6107 *
6108 * Initialize @dev in preparation for probing.
6109 *
6110 * LOCKING:
6111 * Inherited from caller.
6112 */
6113void ata_dev_init(struct ata_device *dev)
6114{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09006115 struct ata_link *link = dev->link;
6116 struct ata_port *ap = link->ap;
Tejun Heo72fa4b72006-05-31 18:27:32 +09006117 unsigned long flags;
Tejun Heo3ef3b432006-05-31 18:27:30 +09006118
Tejun Heo5a04bf42006-05-31 18:27:38 +09006119 /* SATA spd limit is bound to the first device */
Tejun Heo9af5c9c2007-08-06 18:36:22 +09006120 link->sata_spd_limit = link->hw_sata_spd_limit;
6121 link->sata_spd = 0;
Tejun Heo5a04bf42006-05-31 18:27:38 +09006122
Tejun Heo72fa4b72006-05-31 18:27:32 +09006123 /* High bits of dev->flags are used to record warm plug
6124 * requests which occur asynchronously. Synchronize using
Jeff Garzikcca39742006-08-24 03:19:22 -04006125 * host lock.
Tejun Heo72fa4b72006-05-31 18:27:32 +09006126 */
Jeff Garzikba6a1302006-06-22 23:46:10 -04006127 spin_lock_irqsave(ap->lock, flags);
Tejun Heo72fa4b72006-05-31 18:27:32 +09006128 dev->flags &= ~ATA_DFLAG_INIT_MASK;
Tejun Heo3dcc3232007-09-03 12:20:11 +09006129 dev->horkage = 0;
Jeff Garzikba6a1302006-06-22 23:46:10 -04006130 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo72fa4b72006-05-31 18:27:32 +09006131
6132 memset((void *)dev + ATA_DEVICE_CLEAR_OFFSET, 0,
6133 sizeof(*dev) - ATA_DEVICE_CLEAR_OFFSET);
Tejun Heo3ef3b432006-05-31 18:27:30 +09006134 dev->pio_mask = UINT_MAX;
6135 dev->mwdma_mask = UINT_MAX;
6136 dev->udma_mask = UINT_MAX;
6137}
6138
6139/**
Tejun Heo4fb37a22007-08-06 18:36:23 +09006140 * ata_link_init - Initialize an ata_link structure
6141 * @ap: ATA port link is attached to
6142 * @link: Link structure to initialize
Tejun Heo89898052007-08-06 18:36:23 +09006143 * @pmp: Port multiplier port number
Tejun Heo4fb37a22007-08-06 18:36:23 +09006144 *
6145 * Initialize @link.
6146 *
6147 * LOCKING:
6148 * Kernel thread context (may sleep)
6149 */
Tejun Heo89898052007-08-06 18:36:23 +09006150static void ata_link_init(struct ata_port *ap, struct ata_link *link, int pmp)
Tejun Heo4fb37a22007-08-06 18:36:23 +09006151{
6152 int i;
6153
6154 /* clear everything except for devices */
6155 memset(link, 0, offsetof(struct ata_link, device[0]));
6156
6157 link->ap = ap;
Tejun Heo89898052007-08-06 18:36:23 +09006158 link->pmp = pmp;
Tejun Heo4fb37a22007-08-06 18:36:23 +09006159 link->active_tag = ATA_TAG_POISON;
6160 link->hw_sata_spd_limit = UINT_MAX;
6161
6162 /* can't use iterator, ap isn't initialized yet */
6163 for (i = 0; i < ATA_MAX_DEVICES; i++) {
6164 struct ata_device *dev = &link->device[i];
6165
6166 dev->link = link;
6167 dev->devno = dev - link->device;
6168 ata_dev_init(dev);
6169 }
6170}
6171
6172/**
6173 * sata_link_init_spd - Initialize link->sata_spd_limit
6174 * @link: Link to configure sata_spd_limit for
6175 *
6176 * Initialize @link->[hw_]sata_spd_limit to the currently
6177 * configured value.
6178 *
6179 * LOCKING:
6180 * Kernel thread context (may sleep).
6181 *
6182 * RETURNS:
6183 * 0 on success, -errno on failure.
6184 */
6185static int sata_link_init_spd(struct ata_link *link)
6186{
6187 u32 scontrol, spd;
6188 int rc;
6189
6190 rc = sata_scr_read(link, SCR_CONTROL, &scontrol);
6191 if (rc)
6192 return rc;
6193
6194 spd = (scontrol >> 4) & 0xf;
6195 if (spd)
6196 link->hw_sata_spd_limit &= (1 << spd) - 1;
6197
6198 link->sata_spd_limit = link->hw_sata_spd_limit;
6199
6200 return 0;
6201}
6202
6203/**
Tejun Heof3187192007-04-17 23:44:07 +09006204 * ata_port_alloc - allocate and initialize basic ATA port resources
6205 * @host: ATA host this allocated port belongs to
Linus Torvalds1da177e2005-04-16 15:20:36 -07006206 *
Tejun Heof3187192007-04-17 23:44:07 +09006207 * Allocate and initialize basic ATA port resources.
6208 *
6209 * RETURNS:
6210 * Allocate ATA port on success, NULL on failure.
Jeff Garzik0cba6322005-05-30 19:49:12 -04006211 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006212 * LOCKING:
Tejun Heof3187192007-04-17 23:44:07 +09006213 * Inherited from calling layer (may sleep).
Linus Torvalds1da177e2005-04-16 15:20:36 -07006214 */
Tejun Heof3187192007-04-17 23:44:07 +09006215struct ata_port *ata_port_alloc(struct ata_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006216{
Tejun Heof3187192007-04-17 23:44:07 +09006217 struct ata_port *ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006218
Tejun Heof3187192007-04-17 23:44:07 +09006219 DPRINTK("ENTER\n");
6220
6221 ap = kzalloc(sizeof(*ap), GFP_KERNEL);
6222 if (!ap)
6223 return NULL;
6224
Tejun Heof4d6d002007-05-01 11:50:15 +02006225 ap->pflags |= ATA_PFLAG_INITIALIZING;
Jeff Garzikcca39742006-08-24 03:19:22 -04006226 ap->lock = &host->lock;
Tejun Heo198e0fe2006-04-02 18:51:52 +09006227 ap->flags = ATA_FLAG_DISABLED;
Tejun Heof3187192007-04-17 23:44:07 +09006228 ap->print_id = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006229 ap->ctl = ATA_DEVCTL_OBS;
Jeff Garzikcca39742006-08-24 03:19:22 -04006230 ap->host = host;
Tejun Heof3187192007-04-17 23:44:07 +09006231 ap->dev = host->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006232 ap->last_ctl = 0xFF;
Borislav Petkovbd5d8252006-06-11 23:17:01 -04006233
6234#if defined(ATA_VERBOSE_DEBUG)
6235 /* turn on all debugging levels */
6236 ap->msg_enable = 0x00FF;
6237#elif defined(ATA_DEBUG)
6238 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 +09006239#else
Borislav Petkov0dd4b212006-06-23 02:29:08 -04006240 ap->msg_enable = ATA_MSG_DRV | ATA_MSG_ERR | ATA_MSG_WARN;
Borislav Petkovbd5d8252006-06-11 23:17:01 -04006241#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006242
David Howells65f27f32006-11-22 14:55:48 +00006243 INIT_DELAYED_WORK(&ap->port_task, NULL);
6244 INIT_DELAYED_WORK(&ap->hotplug_task, ata_scsi_hotplug);
6245 INIT_WORK(&ap->scsi_rescan_task, ata_scsi_dev_rescan);
Tejun Heoa72ec4c2006-01-23 13:09:37 +09006246 INIT_LIST_HEAD(&ap->eh_done_q);
Tejun Heoc6cf9e92006-05-31 18:27:27 +09006247 init_waitqueue_head(&ap->eh_wait_q);
Tejun Heo5ddf24c2007-07-16 14:29:41 +09006248 init_timer_deferrable(&ap->fastdrain_timer);
6249 ap->fastdrain_timer.function = ata_eh_fastdrain_timerfn;
6250 ap->fastdrain_timer.data = (unsigned long)ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006251
Tejun Heo838df622006-05-15 20:57:44 +09006252 ap->cbl = ATA_CBL_NONE;
Tejun Heo838df622006-05-15 20:57:44 +09006253
Tejun Heo89898052007-08-06 18:36:23 +09006254 ata_link_init(ap, &ap->link, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006255
6256#ifdef ATA_IRQ_TRAP
6257 ap->stats.unhandled_irq = 1;
6258 ap->stats.idle_irq = 1;
6259#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006260 return ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006261}
6262
Tejun Heof0d36ef2007-01-20 16:00:28 +09006263static void ata_host_release(struct device *gendev, void *res)
6264{
6265 struct ata_host *host = dev_get_drvdata(gendev);
6266 int i;
6267
6268 for (i = 0; i < host->n_ports; i++) {
6269 struct ata_port *ap = host->ports[i];
6270
Tejun Heoecef7252007-04-17 23:44:06 +09006271 if (!ap)
6272 continue;
6273
6274 if ((host->flags & ATA_HOST_STARTED) && ap->ops->port_stop)
Tejun Heof0d36ef2007-01-20 16:00:28 +09006275 ap->ops->port_stop(ap);
Tejun Heof0d36ef2007-01-20 16:00:28 +09006276 }
6277
Tejun Heoecef7252007-04-17 23:44:06 +09006278 if ((host->flags & ATA_HOST_STARTED) && host->ops->host_stop)
Tejun Heof0d36ef2007-01-20 16:00:28 +09006279 host->ops->host_stop(host);
Tejun Heo1aa56cc2007-02-27 22:33:21 +09006280
Tejun Heo1aa506e2007-03-09 19:36:12 +09006281 for (i = 0; i < host->n_ports; i++) {
6282 struct ata_port *ap = host->ports[i];
6283
Tejun Heo49114872007-04-17 23:44:06 +09006284 if (!ap)
6285 continue;
6286
6287 if (ap->scsi_host)
Tejun Heo1aa506e2007-03-09 19:36:12 +09006288 scsi_host_put(ap->scsi_host);
6289
Tejun Heo49114872007-04-17 23:44:06 +09006290 kfree(ap);
Tejun Heo1aa506e2007-03-09 19:36:12 +09006291 host->ports[i] = NULL;
6292 }
6293
Tejun Heo1aa56cc2007-02-27 22:33:21 +09006294 dev_set_drvdata(gendev, NULL);
Tejun Heof0d36ef2007-01-20 16:00:28 +09006295}
6296
Linus Torvalds1da177e2005-04-16 15:20:36 -07006297/**
Tejun Heof3187192007-04-17 23:44:07 +09006298 * ata_host_alloc - allocate and init basic ATA host resources
6299 * @dev: generic device this host is associated with
6300 * @max_ports: maximum number of ATA ports associated with this host
6301 *
6302 * Allocate and initialize basic ATA host resources. LLD calls
6303 * this function to allocate a host, initializes it fully and
6304 * attaches it using ata_host_register().
6305 *
6306 * @max_ports ports are allocated and host->n_ports is
6307 * initialized to @max_ports. The caller is allowed to decrease
6308 * host->n_ports before calling ata_host_register(). The unused
6309 * ports will be automatically freed on registration.
6310 *
6311 * RETURNS:
6312 * Allocate ATA host on success, NULL on failure.
6313 *
6314 * LOCKING:
6315 * Inherited from calling layer (may sleep).
6316 */
6317struct ata_host *ata_host_alloc(struct device *dev, int max_ports)
6318{
6319 struct ata_host *host;
6320 size_t sz;
6321 int i;
6322
6323 DPRINTK("ENTER\n");
6324
6325 if (!devres_open_group(dev, NULL, GFP_KERNEL))
6326 return NULL;
6327
6328 /* alloc a container for our list of ATA ports (buses) */
6329 sz = sizeof(struct ata_host) + (max_ports + 1) * sizeof(void *);
6330 /* alloc a container for our list of ATA ports (buses) */
6331 host = devres_alloc(ata_host_release, sz, GFP_KERNEL);
6332 if (!host)
6333 goto err_out;
6334
6335 devres_add(dev, host);
6336 dev_set_drvdata(dev, host);
6337
6338 spin_lock_init(&host->lock);
6339 host->dev = dev;
6340 host->n_ports = max_ports;
6341
6342 /* allocate ports bound to this host */
6343 for (i = 0; i < max_ports; i++) {
6344 struct ata_port *ap;
6345
6346 ap = ata_port_alloc(host);
6347 if (!ap)
6348 goto err_out;
6349
6350 ap->port_no = i;
6351 host->ports[i] = ap;
6352 }
6353
6354 devres_remove_group(dev, NULL);
6355 return host;
6356
6357 err_out:
6358 devres_release_group(dev, NULL);
6359 return NULL;
6360}
6361
6362/**
Tejun Heof5cda252007-04-17 23:44:07 +09006363 * ata_host_alloc_pinfo - alloc host and init with port_info array
6364 * @dev: generic device this host is associated with
6365 * @ppi: array of ATA port_info to initialize host with
6366 * @n_ports: number of ATA ports attached to this host
6367 *
6368 * Allocate ATA host and initialize with info from @ppi. If NULL
6369 * terminated, @ppi may contain fewer entries than @n_ports. The
6370 * last entry will be used for the remaining ports.
6371 *
6372 * RETURNS:
6373 * Allocate ATA host on success, NULL on failure.
6374 *
6375 * LOCKING:
6376 * Inherited from calling layer (may sleep).
6377 */
6378struct ata_host *ata_host_alloc_pinfo(struct device *dev,
6379 const struct ata_port_info * const * ppi,
6380 int n_ports)
6381{
6382 const struct ata_port_info *pi;
6383 struct ata_host *host;
6384 int i, j;
6385
6386 host = ata_host_alloc(dev, n_ports);
6387 if (!host)
6388 return NULL;
6389
6390 for (i = 0, j = 0, pi = NULL; i < host->n_ports; i++) {
6391 struct ata_port *ap = host->ports[i];
6392
6393 if (ppi[j])
6394 pi = ppi[j++];
6395
6396 ap->pio_mask = pi->pio_mask;
6397 ap->mwdma_mask = pi->mwdma_mask;
6398 ap->udma_mask = pi->udma_mask;
6399 ap->flags |= pi->flags;
Tejun Heo0c887582007-08-06 18:36:23 +09006400 ap->link.flags |= pi->link_flags;
Tejun Heof5cda252007-04-17 23:44:07 +09006401 ap->ops = pi->port_ops;
6402
6403 if (!host->ops && (pi->port_ops != &ata_dummy_port_ops))
6404 host->ops = pi->port_ops;
6405 if (!host->private_data && pi->private_data)
6406 host->private_data = pi->private_data;
6407 }
6408
6409 return host;
6410}
6411
6412/**
Tejun Heoecef7252007-04-17 23:44:06 +09006413 * ata_host_start - start and freeze ports of an ATA host
6414 * @host: ATA host to start ports for
6415 *
6416 * Start and then freeze ports of @host. Started status is
6417 * recorded in host->flags, so this function can be called
6418 * multiple times. Ports are guaranteed to get started only
Tejun Heof3187192007-04-17 23:44:07 +09006419 * once. If host->ops isn't initialized yet, its set to the
6420 * first non-dummy port ops.
Tejun Heoecef7252007-04-17 23:44:06 +09006421 *
6422 * LOCKING:
6423 * Inherited from calling layer (may sleep).
6424 *
6425 * RETURNS:
6426 * 0 if all ports are started successfully, -errno otherwise.
6427 */
6428int ata_host_start(struct ata_host *host)
6429{
6430 int i, rc;
6431
6432 if (host->flags & ATA_HOST_STARTED)
6433 return 0;
6434
6435 for (i = 0; i < host->n_ports; i++) {
6436 struct ata_port *ap = host->ports[i];
6437
Tejun Heof3187192007-04-17 23:44:07 +09006438 if (!host->ops && !ata_port_is_dummy(ap))
6439 host->ops = ap->ops;
6440
Tejun Heoecef7252007-04-17 23:44:06 +09006441 if (ap->ops->port_start) {
6442 rc = ap->ops->port_start(ap);
6443 if (rc) {
6444 ata_port_printk(ap, KERN_ERR, "failed to "
6445 "start port (errno=%d)\n", rc);
6446 goto err_out;
6447 }
6448 }
6449
6450 ata_eh_freeze_port(ap);
6451 }
6452
6453 host->flags |= ATA_HOST_STARTED;
6454 return 0;
6455
6456 err_out:
6457 while (--i >= 0) {
6458 struct ata_port *ap = host->ports[i];
6459
6460 if (ap->ops->port_stop)
6461 ap->ops->port_stop(ap);
6462 }
6463 return rc;
6464}
6465
6466/**
Jeff Garzikcca39742006-08-24 03:19:22 -04006467 * ata_sas_host_init - Initialize a host struct
6468 * @host: host to initialize
6469 * @dev: device host is attached to
6470 * @flags: host flags
6471 * @ops: port_ops
Brian Kingb03732f2006-08-07 14:27:10 -05006472 *
6473 * LOCKING:
6474 * PCI/etc. bus probe sem.
6475 *
6476 */
Tejun Heof3187192007-04-17 23:44:07 +09006477/* KILLME - the only user left is ipr */
Jeff Garzikcca39742006-08-24 03:19:22 -04006478void ata_host_init(struct ata_host *host, struct device *dev,
6479 unsigned long flags, const struct ata_port_operations *ops)
Brian Kingb03732f2006-08-07 14:27:10 -05006480{
Jeff Garzikcca39742006-08-24 03:19:22 -04006481 spin_lock_init(&host->lock);
6482 host->dev = dev;
6483 host->flags = flags;
6484 host->ops = ops;
Brian Kingb03732f2006-08-07 14:27:10 -05006485}
6486
6487/**
Tejun Heof3187192007-04-17 23:44:07 +09006488 * ata_host_register - register initialized ATA host
6489 * @host: ATA host to register
6490 * @sht: template for SCSI host
Jeff Garzik0cba6322005-05-30 19:49:12 -04006491 *
Tejun Heof3187192007-04-17 23:44:07 +09006492 * Register initialized ATA host. @host is allocated using
6493 * ata_host_alloc() and fully initialized by LLD. This function
6494 * starts ports, registers @host with ATA and SCSI layers and
6495 * probe registered devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006496 *
6497 * LOCKING:
Tejun Heof3187192007-04-17 23:44:07 +09006498 * Inherited from calling layer (may sleep).
Linus Torvalds1da177e2005-04-16 15:20:36 -07006499 *
6500 * RETURNS:
Tejun Heof3187192007-04-17 23:44:07 +09006501 * 0 on success, -errno otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006502 */
Tejun Heof3187192007-04-17 23:44:07 +09006503int ata_host_register(struct ata_host *host, struct scsi_host_template *sht)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006504{
Tejun Heof3187192007-04-17 23:44:07 +09006505 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006506
Tejun Heof3187192007-04-17 23:44:07 +09006507 /* host must have been started */
6508 if (!(host->flags & ATA_HOST_STARTED)) {
6509 dev_printk(KERN_ERR, host->dev,
6510 "BUG: trying to register unstarted host\n");
6511 WARN_ON(1);
6512 return -EINVAL;
Alan Cox02f076a2006-09-26 17:35:32 +01006513 }
Tejun Heof0d36ef2007-01-20 16:00:28 +09006514
Tejun Heof3187192007-04-17 23:44:07 +09006515 /* Blow away unused ports. This happens when LLD can't
6516 * determine the exact number of ports to allocate at
6517 * allocation time.
6518 */
6519 for (i = host->n_ports; host->ports[i]; i++)
6520 kfree(host->ports[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006521
Tejun Heof3187192007-04-17 23:44:07 +09006522 /* give ports names and add SCSI hosts */
6523 for (i = 0; i < host->n_ports; i++)
6524 host->ports[i]->print_id = ata_print_id++;
Tejun Heof0d36ef2007-01-20 16:00:28 +09006525
Tejun Heof3187192007-04-17 23:44:07 +09006526 rc = ata_scsi_add_hosts(host, sht);
Tejun Heoecef7252007-04-17 23:44:06 +09006527 if (rc)
Tejun Heof3187192007-04-17 23:44:07 +09006528 return rc;
Tejun Heoecef7252007-04-17 23:44:06 +09006529
Tejun Heofafbae82007-05-15 03:28:16 +09006530 /* associate with ACPI nodes */
6531 ata_acpi_associate(host);
6532
Tejun Heof3187192007-04-17 23:44:07 +09006533 /* set cable, sata_spd_limit and report */
Jeff Garzikcca39742006-08-24 03:19:22 -04006534 for (i = 0; i < host->n_ports; i++) {
6535 struct ata_port *ap = host->ports[i];
Tejun Heof3187192007-04-17 23:44:07 +09006536 unsigned long xfer_mask;
6537
6538 /* set SATA cable type if still unset */
6539 if (ap->cbl == ATA_CBL_NONE && (ap->flags & ATA_FLAG_SATA))
6540 ap->cbl = ATA_CBL_SATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006541
Tejun Heo5a04bf42006-05-31 18:27:38 +09006542 /* init sata_spd_limit to the current value */
Tejun Heo4fb37a22007-08-06 18:36:23 +09006543 sata_link_init_spd(&ap->link);
Tejun Heo5a04bf42006-05-31 18:27:38 +09006544
Tejun Heocbcdd872007-08-18 13:14:55 +09006545 /* print per-port info to dmesg */
Tejun Heof3187192007-04-17 23:44:07 +09006546 xfer_mask = ata_pack_xfermask(ap->pio_mask, ap->mwdma_mask,
6547 ap->udma_mask);
6548
Tejun Heof3187192007-04-17 23:44:07 +09006549 if (!ata_port_is_dummy(ap))
Tejun Heocbcdd872007-08-18 13:14:55 +09006550 ata_port_printk(ap, KERN_INFO,
6551 "%cATA max %s %s\n",
Tejun Heoa16abc02007-05-21 18:33:47 +02006552 (ap->flags & ATA_FLAG_SATA) ? 'S' : 'P',
Tejun Heof3187192007-04-17 23:44:07 +09006553 ata_mode_string(xfer_mask),
Tejun Heocbcdd872007-08-18 13:14:55 +09006554 ap->link.eh_info.desc);
Tejun Heof3187192007-04-17 23:44:07 +09006555 else
6556 ata_port_printk(ap, KERN_INFO, "DUMMY\n");
6557 }
6558
6559 /* perform each probe synchronously */
6560 DPRINTK("probe begin\n");
6561 for (i = 0; i < host->n_ports; i++) {
6562 struct ata_port *ap = host->ports[i];
6563 int rc;
6564
6565 /* probe */
Tejun Heo52783c52006-05-31 18:28:22 +09006566 if (ap->ops->error_handler) {
Tejun Heo9af5c9c2007-08-06 18:36:22 +09006567 struct ata_eh_info *ehi = &ap->link.eh_info;
Tejun Heo3e706392006-05-31 18:28:11 +09006568 unsigned long flags;
6569
6570 ata_port_probe(ap);
6571
6572 /* kick EH for boot probing */
Jeff Garzikba6a1302006-06-22 23:46:10 -04006573 spin_lock_irqsave(ap->lock, flags);
Tejun Heo3e706392006-05-31 18:28:11 +09006574
Tejun Heof58229f2007-08-06 18:36:23 +09006575 ehi->probe_mask =
6576 (1 << ata_link_max_devices(&ap->link)) - 1;
Tejun Heo1cdaf532006-07-03 16:07:26 +09006577 ehi->action |= ATA_EH_SOFTRESET;
6578 ehi->flags |= ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET;
Tejun Heo3e706392006-05-31 18:28:11 +09006579
Tejun Heof4d6d002007-05-01 11:50:15 +02006580 ap->pflags &= ~ATA_PFLAG_INITIALIZING;
Tejun Heob51e9e52006-06-29 01:29:30 +09006581 ap->pflags |= ATA_PFLAG_LOADING;
Tejun Heo3e706392006-05-31 18:28:11 +09006582 ata_port_schedule_eh(ap);
6583
Jeff Garzikba6a1302006-06-22 23:46:10 -04006584 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo3e706392006-05-31 18:28:11 +09006585
6586 /* wait for EH to finish */
6587 ata_port_wait_eh(ap);
6588 } else {
Tejun Heo44877b42007-02-21 01:06:51 +09006589 DPRINTK("ata%u: bus probe begin\n", ap->print_id);
Tejun Heo3e706392006-05-31 18:28:11 +09006590 rc = ata_bus_probe(ap);
Tejun Heo44877b42007-02-21 01:06:51 +09006591 DPRINTK("ata%u: bus probe end\n", ap->print_id);
Tejun Heo3e706392006-05-31 18:28:11 +09006592
6593 if (rc) {
6594 /* FIXME: do something useful here?
6595 * Current libata behavior will
6596 * tear down everything when
6597 * the module is removed
6598 * or the h/w is unplugged.
6599 */
6600 }
6601 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006602 }
6603
6604 /* probes are done, now scan each port's disk(s) */
Randy Dunlapc893a3a2006-01-28 13:15:32 -05006605 DPRINTK("host probe begin\n");
Jeff Garzikcca39742006-08-24 03:19:22 -04006606 for (i = 0; i < host->n_ports; i++) {
6607 struct ata_port *ap = host->ports[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006608
Tejun Heo1ae46312007-07-16 14:29:40 +09006609 ata_scsi_scan_host(ap, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006610 }
6611
Tejun Heof3187192007-04-17 23:44:07 +09006612 return 0;
6613}
6614
6615/**
Tejun Heof5cda252007-04-17 23:44:07 +09006616 * ata_host_activate - start host, request IRQ and register it
6617 * @host: target ATA host
6618 * @irq: IRQ to request
6619 * @irq_handler: irq_handler used when requesting IRQ
6620 * @irq_flags: irq_flags used when requesting IRQ
6621 * @sht: scsi_host_template to use when registering the host
6622 *
6623 * After allocating an ATA host and initializing it, most libata
6624 * LLDs perform three steps to activate the host - start host,
6625 * request IRQ and register it. This helper takes necessasry
6626 * arguments and performs the three steps in one go.
6627 *
6628 * LOCKING:
6629 * Inherited from calling layer (may sleep).
6630 *
6631 * RETURNS:
6632 * 0 on success, -errno otherwise.
6633 */
6634int ata_host_activate(struct ata_host *host, int irq,
6635 irq_handler_t irq_handler, unsigned long irq_flags,
6636 struct scsi_host_template *sht)
6637{
Tejun Heocbcdd872007-08-18 13:14:55 +09006638 int i, rc;
Tejun Heof5cda252007-04-17 23:44:07 +09006639
6640 rc = ata_host_start(host);
6641 if (rc)
6642 return rc;
6643
6644 rc = devm_request_irq(host->dev, irq, irq_handler, irq_flags,
6645 dev_driver_string(host->dev), host);
6646 if (rc)
6647 return rc;
6648
Tejun Heocbcdd872007-08-18 13:14:55 +09006649 for (i = 0; i < host->n_ports; i++)
6650 ata_port_desc(host->ports[i], "irq %d", irq);
Tejun Heo40318262007-07-03 01:38:47 +09006651
Tejun Heof5cda252007-04-17 23:44:07 +09006652 rc = ata_host_register(host, sht);
6653 /* if failed, just free the IRQ and leave ports alone */
6654 if (rc)
6655 devm_free_irq(host->dev, irq, host);
6656
6657 return rc;
6658}
6659
6660/**
Tejun Heo720ba122006-05-31 18:28:13 +09006661 * ata_port_detach - Detach ATA port in prepration of device removal
6662 * @ap: ATA port to be detached
6663 *
6664 * Detach all ATA devices and the associated SCSI devices of @ap;
6665 * then, remove the associated SCSI host. @ap is guaranteed to
6666 * be quiescent on return from this function.
6667 *
6668 * LOCKING:
6669 * Kernel thread context (may sleep).
6670 */
6671void ata_port_detach(struct ata_port *ap)
6672{
6673 unsigned long flags;
Tejun Heo41bda9c2007-08-06 18:36:24 +09006674 struct ata_link *link;
Tejun Heof58229f2007-08-06 18:36:23 +09006675 struct ata_device *dev;
Tejun Heo720ba122006-05-31 18:28:13 +09006676
6677 if (!ap->ops->error_handler)
Tejun Heoc3cf30a2006-08-05 03:59:11 +09006678 goto skip_eh;
Tejun Heo720ba122006-05-31 18:28:13 +09006679
6680 /* tell EH we're leaving & flush EH */
Jeff Garzikba6a1302006-06-22 23:46:10 -04006681 spin_lock_irqsave(ap->lock, flags);
Tejun Heob51e9e52006-06-29 01:29:30 +09006682 ap->pflags |= ATA_PFLAG_UNLOADING;
Jeff Garzikba6a1302006-06-22 23:46:10 -04006683 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo720ba122006-05-31 18:28:13 +09006684
6685 ata_port_wait_eh(ap);
6686
6687 /* EH is now guaranteed to see UNLOADING, so no new device
6688 * will be attached. Disable all existing devices.
6689 */
Jeff Garzikba6a1302006-06-22 23:46:10 -04006690 spin_lock_irqsave(ap->lock, flags);
Tejun Heo720ba122006-05-31 18:28:13 +09006691
Tejun Heo41bda9c2007-08-06 18:36:24 +09006692 ata_port_for_each_link(link, ap) {
6693 ata_link_for_each_dev(dev, link)
6694 ata_dev_disable(dev);
6695 }
Tejun Heo720ba122006-05-31 18:28:13 +09006696
Jeff Garzikba6a1302006-06-22 23:46:10 -04006697 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo720ba122006-05-31 18:28:13 +09006698
6699 /* Final freeze & EH. All in-flight commands are aborted. EH
6700 * will be skipped and retrials will be terminated with bad
6701 * target.
6702 */
Jeff Garzikba6a1302006-06-22 23:46:10 -04006703 spin_lock_irqsave(ap->lock, flags);
Tejun Heo720ba122006-05-31 18:28:13 +09006704 ata_port_freeze(ap); /* won't be thawed */
Jeff Garzikba6a1302006-06-22 23:46:10 -04006705 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo720ba122006-05-31 18:28:13 +09006706
6707 ata_port_wait_eh(ap);
Oleg Nesterov45a66c12007-07-09 11:46:13 -07006708 cancel_rearming_delayed_work(&ap->hotplug_task);
Tejun Heo720ba122006-05-31 18:28:13 +09006709
Tejun Heoc3cf30a2006-08-05 03:59:11 +09006710 skip_eh:
Tejun Heo720ba122006-05-31 18:28:13 +09006711 /* remove the associated SCSI host */
Jeff Garzikcca39742006-08-24 03:19:22 -04006712 scsi_remove_host(ap->scsi_host);
Tejun Heo720ba122006-05-31 18:28:13 +09006713}
6714
6715/**
Tejun Heo0529c152007-01-20 16:00:26 +09006716 * ata_host_detach - Detach all ports of an ATA host
6717 * @host: Host to detach
6718 *
6719 * Detach all ports of @host.
6720 *
6721 * LOCKING:
6722 * Kernel thread context (may sleep).
6723 */
6724void ata_host_detach(struct ata_host *host)
6725{
6726 int i;
6727
6728 for (i = 0; i < host->n_ports; i++)
6729 ata_port_detach(host->ports[i]);
6730}
6731
Linus Torvalds1da177e2005-04-16 15:20:36 -07006732/**
6733 * ata_std_ports - initialize ioaddr with standard port offsets.
6734 * @ioaddr: IO address structure to be initialized
Edward Falk0baab862005-06-02 18:17:13 -04006735 *
6736 * Utility function which initializes data_addr, error_addr,
6737 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
6738 * device_addr, status_addr, and command_addr to standard offsets
6739 * relative to cmd_addr.
6740 *
6741 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006742 */
Edward Falk0baab862005-06-02 18:17:13 -04006743
Linus Torvalds1da177e2005-04-16 15:20:36 -07006744void ata_std_ports(struct ata_ioports *ioaddr)
6745{
6746 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
6747 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
6748 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
6749 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
6750 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
6751 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
6752 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
6753 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
6754 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
6755 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
6756}
6757
Edward Falk0baab862005-06-02 18:17:13 -04006758
Jeff Garzik374b1872005-08-30 05:42:52 -04006759#ifdef CONFIG_PCI
6760
Edward Falk0baab862005-06-02 18:17:13 -04006761/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07006762 * ata_pci_remove_one - PCI layer callback for device removal
6763 * @pdev: PCI device that was removed
6764 *
Tejun Heob878ca52007-01-20 16:00:28 +09006765 * PCI layer indicates to libata via this hook that hot-unplug or
6766 * module unload event has occurred. Detach all ports. Resource
6767 * release is handled via devres.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006768 *
6769 * LOCKING:
6770 * Inherited from PCI layer (may sleep).
6771 */
Tejun Heof0d36ef2007-01-20 16:00:28 +09006772void ata_pci_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006773{
6774 struct device *dev = pci_dev_to_dev(pdev);
Jeff Garzikcca39742006-08-24 03:19:22 -04006775 struct ata_host *host = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006776
Tejun Heob878ca52007-01-20 16:00:28 +09006777 ata_host_detach(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006778}
6779
6780/* move to PCI subsystem */
Jeff Garzik057ace52005-10-22 14:27:05 -04006781int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006782{
6783 unsigned long tmp = 0;
6784
6785 switch (bits->width) {
6786 case 1: {
6787 u8 tmp8 = 0;
6788 pci_read_config_byte(pdev, bits->reg, &tmp8);
6789 tmp = tmp8;
6790 break;
6791 }
6792 case 2: {
6793 u16 tmp16 = 0;
6794 pci_read_config_word(pdev, bits->reg, &tmp16);
6795 tmp = tmp16;
6796 break;
6797 }
6798 case 4: {
6799 u32 tmp32 = 0;
6800 pci_read_config_dword(pdev, bits->reg, &tmp32);
6801 tmp = tmp32;
6802 break;
6803 }
6804
6805 default:
6806 return -EINVAL;
6807 }
6808
6809 tmp &= bits->mask;
6810
6811 return (tmp == bits->val) ? 1 : 0;
6812}
Jens Axboe9b847542006-01-06 09:28:07 +01006813
Tejun Heo6ffa01d2007-03-02 17:32:47 +09006814#ifdef CONFIG_PM
Tejun Heo3c5100c2006-07-26 16:58:33 +09006815void ata_pci_device_do_suspend(struct pci_dev *pdev, pm_message_t mesg)
Jens Axboe9b847542006-01-06 09:28:07 +01006816{
6817 pci_save_state(pdev);
Tejun Heo4c90d972007-02-20 18:14:48 +09006818 pci_disable_device(pdev);
Tejun Heo500530f2006-07-03 16:07:27 +09006819
Tejun Heo4c90d972007-02-20 18:14:48 +09006820 if (mesg.event == PM_EVENT_SUSPEND)
Tejun Heo500530f2006-07-03 16:07:27 +09006821 pci_set_power_state(pdev, PCI_D3hot);
Jens Axboe9b847542006-01-06 09:28:07 +01006822}
6823
Tejun Heo553c4aa2006-12-26 19:39:50 +09006824int ata_pci_device_do_resume(struct pci_dev *pdev)
Jens Axboe9b847542006-01-06 09:28:07 +01006825{
Tejun Heo553c4aa2006-12-26 19:39:50 +09006826 int rc;
6827
Jens Axboe9b847542006-01-06 09:28:07 +01006828 pci_set_power_state(pdev, PCI_D0);
6829 pci_restore_state(pdev);
Tejun Heo553c4aa2006-12-26 19:39:50 +09006830
Tejun Heob878ca52007-01-20 16:00:28 +09006831 rc = pcim_enable_device(pdev);
Tejun Heo553c4aa2006-12-26 19:39:50 +09006832 if (rc) {
6833 dev_printk(KERN_ERR, &pdev->dev,
6834 "failed to enable device after resume (%d)\n", rc);
6835 return rc;
6836 }
6837
Jens Axboe9b847542006-01-06 09:28:07 +01006838 pci_set_master(pdev);
Tejun Heo553c4aa2006-12-26 19:39:50 +09006839 return 0;
Tejun Heo500530f2006-07-03 16:07:27 +09006840}
6841
Tejun Heo3c5100c2006-07-26 16:58:33 +09006842int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg)
Tejun Heo500530f2006-07-03 16:07:27 +09006843{
Jeff Garzikcca39742006-08-24 03:19:22 -04006844 struct ata_host *host = dev_get_drvdata(&pdev->dev);
Tejun Heo500530f2006-07-03 16:07:27 +09006845 int rc = 0;
6846
Jeff Garzikcca39742006-08-24 03:19:22 -04006847 rc = ata_host_suspend(host, mesg);
Tejun Heo500530f2006-07-03 16:07:27 +09006848 if (rc)
6849 return rc;
6850
Tejun Heo3c5100c2006-07-26 16:58:33 +09006851 ata_pci_device_do_suspend(pdev, mesg);
Tejun Heo500530f2006-07-03 16:07:27 +09006852
6853 return 0;
6854}
6855
6856int ata_pci_device_resume(struct pci_dev *pdev)
6857{
Jeff Garzikcca39742006-08-24 03:19:22 -04006858 struct ata_host *host = dev_get_drvdata(&pdev->dev);
Tejun Heo553c4aa2006-12-26 19:39:50 +09006859 int rc;
Tejun Heo500530f2006-07-03 16:07:27 +09006860
Tejun Heo553c4aa2006-12-26 19:39:50 +09006861 rc = ata_pci_device_do_resume(pdev);
6862 if (rc == 0)
6863 ata_host_resume(host);
6864 return rc;
Jens Axboe9b847542006-01-06 09:28:07 +01006865}
Tejun Heo6ffa01d2007-03-02 17:32:47 +09006866#endif /* CONFIG_PM */
6867
Linus Torvalds1da177e2005-04-16 15:20:36 -07006868#endif /* CONFIG_PCI */
6869
6870
Linus Torvalds1da177e2005-04-16 15:20:36 -07006871static int __init ata_init(void)
6872{
Andrew Mortona8601e52006-06-25 01:36:52 -07006873 ata_probe_timeout *= HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006874 ata_wq = create_workqueue("ata");
6875 if (!ata_wq)
6876 return -ENOMEM;
6877
Tejun Heo453b07a2006-05-31 18:27:42 +09006878 ata_aux_wq = create_singlethread_workqueue("ata_aux");
6879 if (!ata_aux_wq) {
6880 destroy_workqueue(ata_wq);
6881 return -ENOMEM;
6882 }
6883
Linus Torvalds1da177e2005-04-16 15:20:36 -07006884 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
6885 return 0;
6886}
6887
6888static void __exit ata_exit(void)
6889{
6890 destroy_workqueue(ata_wq);
Tejun Heo453b07a2006-05-31 18:27:42 +09006891 destroy_workqueue(ata_aux_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006892}
6893
Brian Kinga4625082006-11-13 16:32:36 -06006894subsys_initcall(ata_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006895module_exit(ata_exit);
6896
Jeff Garzik67846b32005-10-05 02:58:32 -04006897static unsigned long ratelimit_time;
Ingo Molnar34af9462006-06-27 02:53:55 -07006898static DEFINE_SPINLOCK(ata_ratelimit_lock);
Jeff Garzik67846b32005-10-05 02:58:32 -04006899
6900int ata_ratelimit(void)
6901{
6902 int rc;
6903 unsigned long flags;
6904
6905 spin_lock_irqsave(&ata_ratelimit_lock, flags);
6906
6907 if (time_after(jiffies, ratelimit_time)) {
6908 rc = 1;
6909 ratelimit_time = jiffies + (HZ/5);
6910 } else
6911 rc = 0;
6912
6913 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
6914
6915 return rc;
6916}
6917
Tejun Heoc22daff2006-04-11 22:22:29 +09006918/**
6919 * ata_wait_register - wait until register value changes
6920 * @reg: IO-mapped register
6921 * @mask: Mask to apply to read register value
6922 * @val: Wait condition
6923 * @interval_msec: polling interval in milliseconds
6924 * @timeout_msec: timeout in milliseconds
6925 *
6926 * Waiting for some bits of register to change is a common
6927 * operation for ATA controllers. This function reads 32bit LE
6928 * IO-mapped register @reg and tests for the following condition.
6929 *
6930 * (*@reg & mask) != val
6931 *
6932 * If the condition is met, it returns; otherwise, the process is
6933 * repeated after @interval_msec until timeout.
6934 *
6935 * LOCKING:
6936 * Kernel thread context (may sleep)
6937 *
6938 * RETURNS:
6939 * The final register value.
6940 */
6941u32 ata_wait_register(void __iomem *reg, u32 mask, u32 val,
6942 unsigned long interval_msec,
6943 unsigned long timeout_msec)
6944{
6945 unsigned long timeout;
6946 u32 tmp;
6947
6948 tmp = ioread32(reg);
6949
6950 /* Calculate timeout _after_ the first read to make sure
6951 * preceding writes reach the controller before starting to
6952 * eat away the timeout.
6953 */
6954 timeout = jiffies + (timeout_msec * HZ) / 1000;
6955
6956 while ((tmp & mask) == val && time_before(jiffies, timeout)) {
6957 msleep(interval_msec);
6958 tmp = ioread32(reg);
6959 }
6960
6961 return tmp;
6962}
6963
Linus Torvalds1da177e2005-04-16 15:20:36 -07006964/*
Tejun Heodd5b06c2006-08-10 16:59:12 +09006965 * Dummy port_ops
6966 */
6967static void ata_dummy_noret(struct ata_port *ap) { }
6968static int ata_dummy_ret0(struct ata_port *ap) { return 0; }
6969static void ata_dummy_qc_noret(struct ata_queued_cmd *qc) { }
6970
6971static u8 ata_dummy_check_status(struct ata_port *ap)
6972{
6973 return ATA_DRDY;
6974}
6975
6976static unsigned int ata_dummy_qc_issue(struct ata_queued_cmd *qc)
6977{
6978 return AC_ERR_SYSTEM;
6979}
6980
6981const struct ata_port_operations ata_dummy_port_ops = {
Tejun Heodd5b06c2006-08-10 16:59:12 +09006982 .check_status = ata_dummy_check_status,
6983 .check_altstatus = ata_dummy_check_status,
6984 .dev_select = ata_noop_dev_select,
6985 .qc_prep = ata_noop_qc_prep,
6986 .qc_issue = ata_dummy_qc_issue,
6987 .freeze = ata_dummy_noret,
6988 .thaw = ata_dummy_noret,
6989 .error_handler = ata_dummy_noret,
6990 .post_internal_cmd = ata_dummy_qc_noret,
6991 .irq_clear = ata_dummy_noret,
6992 .port_start = ata_dummy_ret0,
6993 .port_stop = ata_dummy_noret,
6994};
6995
Tejun Heo21b0ad42007-04-17 23:44:07 +09006996const struct ata_port_info ata_dummy_port_info = {
6997 .port_ops = &ata_dummy_port_ops,
6998};
6999
Tejun Heodd5b06c2006-08-10 16:59:12 +09007000/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07007001 * libata is essentially a library of internal helper functions for
7002 * low-level ATA host controller drivers. As such, the API/ABI is
7003 * likely to change as new drivers are added and updated.
7004 * Do not depend on ABI/API stability.
7005 */
7006
Tejun Heoe9c83912006-07-03 16:07:26 +09007007EXPORT_SYMBOL_GPL(sata_deb_timing_normal);
7008EXPORT_SYMBOL_GPL(sata_deb_timing_hotplug);
7009EXPORT_SYMBOL_GPL(sata_deb_timing_long);
Tejun Heodd5b06c2006-08-10 16:59:12 +09007010EXPORT_SYMBOL_GPL(ata_dummy_port_ops);
Tejun Heo21b0ad42007-04-17 23:44:07 +09007011EXPORT_SYMBOL_GPL(ata_dummy_port_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007012EXPORT_SYMBOL_GPL(ata_std_bios_param);
7013EXPORT_SYMBOL_GPL(ata_std_ports);
Jeff Garzikcca39742006-08-24 03:19:22 -04007014EXPORT_SYMBOL_GPL(ata_host_init);
Tejun Heof3187192007-04-17 23:44:07 +09007015EXPORT_SYMBOL_GPL(ata_host_alloc);
Tejun Heof5cda252007-04-17 23:44:07 +09007016EXPORT_SYMBOL_GPL(ata_host_alloc_pinfo);
Tejun Heoecef7252007-04-17 23:44:06 +09007017EXPORT_SYMBOL_GPL(ata_host_start);
Tejun Heof3187192007-04-17 23:44:07 +09007018EXPORT_SYMBOL_GPL(ata_host_register);
Tejun Heof5cda252007-04-17 23:44:07 +09007019EXPORT_SYMBOL_GPL(ata_host_activate);
Tejun Heo0529c152007-01-20 16:00:26 +09007020EXPORT_SYMBOL_GPL(ata_host_detach);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007021EXPORT_SYMBOL_GPL(ata_sg_init);
7022EXPORT_SYMBOL_GPL(ata_sg_init_one);
Tejun Heo9a1004d2006-05-31 18:27:52 +09007023EXPORT_SYMBOL_GPL(ata_hsm_move);
Tejun Heof686bcb2006-05-15 20:58:05 +09007024EXPORT_SYMBOL_GPL(ata_qc_complete);
Tejun Heodedaf2b2006-05-15 21:03:43 +09007025EXPORT_SYMBOL_GPL(ata_qc_complete_multiple);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007026EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007027EXPORT_SYMBOL_GPL(ata_tf_load);
7028EXPORT_SYMBOL_GPL(ata_tf_read);
7029EXPORT_SYMBOL_GPL(ata_noop_dev_select);
7030EXPORT_SYMBOL_GPL(ata_std_dev_select);
Jeff Garzik43727fb2007-02-25 16:50:52 -05007031EXPORT_SYMBOL_GPL(sata_print_link_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007032EXPORT_SYMBOL_GPL(ata_tf_to_fis);
7033EXPORT_SYMBOL_GPL(ata_tf_from_fis);
7034EXPORT_SYMBOL_GPL(ata_check_status);
7035EXPORT_SYMBOL_GPL(ata_altstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007036EXPORT_SYMBOL_GPL(ata_exec_command);
7037EXPORT_SYMBOL_GPL(ata_port_start);
Alan Coxd92e74d2007-06-07 16:19:15 +01007038EXPORT_SYMBOL_GPL(ata_sff_port_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007039EXPORT_SYMBOL_GPL(ata_interrupt);
Alan04351822007-03-06 02:37:52 -08007040EXPORT_SYMBOL_GPL(ata_do_set_mode);
Tejun Heo0d5ff562007-02-01 15:06:36 +09007041EXPORT_SYMBOL_GPL(ata_data_xfer);
7042EXPORT_SYMBOL_GPL(ata_data_xfer_noirq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007043EXPORT_SYMBOL_GPL(ata_qc_prep);
Alan Coxd26fc952007-07-06 19:13:52 -04007044EXPORT_SYMBOL_GPL(ata_dumb_qc_prep);
Brian Kinge46834c2006-03-17 17:04:03 -06007045EXPORT_SYMBOL_GPL(ata_noop_qc_prep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007046EXPORT_SYMBOL_GPL(ata_bmdma_setup);
7047EXPORT_SYMBOL_GPL(ata_bmdma_start);
7048EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
7049EXPORT_SYMBOL_GPL(ata_bmdma_status);
7050EXPORT_SYMBOL_GPL(ata_bmdma_stop);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09007051EXPORT_SYMBOL_GPL(ata_bmdma_freeze);
7052EXPORT_SYMBOL_GPL(ata_bmdma_thaw);
7053EXPORT_SYMBOL_GPL(ata_bmdma_drive_eh);
7054EXPORT_SYMBOL_GPL(ata_bmdma_error_handler);
7055EXPORT_SYMBOL_GPL(ata_bmdma_post_internal_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007056EXPORT_SYMBOL_GPL(ata_port_probe);
Alan10305f02007-02-20 18:01:59 +00007057EXPORT_SYMBOL_GPL(ata_dev_disable);
Tejun Heo3c567b72006-05-15 20:57:23 +09007058EXPORT_SYMBOL_GPL(sata_set_spd);
Tejun Heo936fd732007-08-06 18:36:23 +09007059EXPORT_SYMBOL_GPL(sata_link_debounce);
7060EXPORT_SYMBOL_GPL(sata_link_resume);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007061EXPORT_SYMBOL_GPL(sata_phy_reset);
7062EXPORT_SYMBOL_GPL(__sata_phy_reset);
7063EXPORT_SYMBOL_GPL(ata_bus_reset);
Tejun Heof5914a42006-05-31 18:27:48 +09007064EXPORT_SYMBOL_GPL(ata_std_prereset);
Tejun Heoc2bd5802006-01-24 17:05:22 +09007065EXPORT_SYMBOL_GPL(ata_std_softreset);
Tejun Heocc0680a2007-08-06 18:36:23 +09007066EXPORT_SYMBOL_GPL(sata_link_hardreset);
Tejun Heoc2bd5802006-01-24 17:05:22 +09007067EXPORT_SYMBOL_GPL(sata_std_hardreset);
7068EXPORT_SYMBOL_GPL(ata_std_postreset);
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05007069EXPORT_SYMBOL_GPL(ata_dev_classify);
7070EXPORT_SYMBOL_GPL(ata_dev_pair);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007071EXPORT_SYMBOL_GPL(ata_port_disable);
Jeff Garzik67846b32005-10-05 02:58:32 -04007072EXPORT_SYMBOL_GPL(ata_ratelimit);
Tejun Heoc22daff2006-04-11 22:22:29 +09007073EXPORT_SYMBOL_GPL(ata_wait_register);
Tejun Heo6f8b9952006-01-24 17:05:21 +09007074EXPORT_SYMBOL_GPL(ata_busy_sleep);
Tejun Heod4b2bab2007-02-02 16:50:52 +09007075EXPORT_SYMBOL_GPL(ata_wait_ready);
Tejun Heo86e45b62006-03-05 15:29:09 +09007076EXPORT_SYMBOL_GPL(ata_port_queue_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007077EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
7078EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007079EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
Tejun Heo83c47bc2006-05-31 18:28:07 +09007080EXPORT_SYMBOL_GPL(ata_scsi_slave_destroy);
Tejun Heoa6e6ce82006-05-15 21:03:48 +09007081EXPORT_SYMBOL_GPL(ata_scsi_change_queue_depth);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007082EXPORT_SYMBOL_GPL(ata_host_intr);
Tejun Heo34bf2172006-05-15 20:57:46 +09007083EXPORT_SYMBOL_GPL(sata_scr_valid);
7084EXPORT_SYMBOL_GPL(sata_scr_read);
7085EXPORT_SYMBOL_GPL(sata_scr_write);
7086EXPORT_SYMBOL_GPL(sata_scr_write_flush);
Tejun Heo936fd732007-08-06 18:36:23 +09007087EXPORT_SYMBOL_GPL(ata_link_online);
7088EXPORT_SYMBOL_GPL(ata_link_offline);
Tejun Heo6ffa01d2007-03-02 17:32:47 +09007089#ifdef CONFIG_PM
Jeff Garzikcca39742006-08-24 03:19:22 -04007090EXPORT_SYMBOL_GPL(ata_host_suspend);
7091EXPORT_SYMBOL_GPL(ata_host_resume);
Tejun Heo6ffa01d2007-03-02 17:32:47 +09007092#endif /* CONFIG_PM */
Tejun Heo6a62a042006-02-13 10:02:46 +09007093EXPORT_SYMBOL_GPL(ata_id_string);
7094EXPORT_SYMBOL_GPL(ata_id_c_string);
Alan10305f02007-02-20 18:01:59 +00007095EXPORT_SYMBOL_GPL(ata_id_to_dma_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007096EXPORT_SYMBOL_GPL(ata_scsi_simulate);
7097
Alan Cox1bc4ccf2006-01-09 17:18:14 +00007098EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
Alan Cox452503f2005-10-21 19:01:32 -04007099EXPORT_SYMBOL_GPL(ata_timing_compute);
7100EXPORT_SYMBOL_GPL(ata_timing_merge);
7101
Linus Torvalds1da177e2005-04-16 15:20:36 -07007102#ifdef CONFIG_PCI
7103EXPORT_SYMBOL_GPL(pci_test_config_bits);
Tejun Heod583bc12007-07-04 18:02:07 +09007104EXPORT_SYMBOL_GPL(ata_pci_init_sff_host);
Tejun Heo1626aeb2007-05-04 12:43:58 +02007105EXPORT_SYMBOL_GPL(ata_pci_init_bmdma);
Tejun Heod583bc12007-07-04 18:02:07 +09007106EXPORT_SYMBOL_GPL(ata_pci_prepare_sff_host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007107EXPORT_SYMBOL_GPL(ata_pci_init_one);
7108EXPORT_SYMBOL_GPL(ata_pci_remove_one);
Tejun Heo6ffa01d2007-03-02 17:32:47 +09007109#ifdef CONFIG_PM
Tejun Heo500530f2006-07-03 16:07:27 +09007110EXPORT_SYMBOL_GPL(ata_pci_device_do_suspend);
7111EXPORT_SYMBOL_GPL(ata_pci_device_do_resume);
Jens Axboe9b847542006-01-06 09:28:07 +01007112EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
7113EXPORT_SYMBOL_GPL(ata_pci_device_resume);
Tejun Heo6ffa01d2007-03-02 17:32:47 +09007114#endif /* CONFIG_PM */
Alan Cox67951ad2006-03-22 15:55:54 +00007115EXPORT_SYMBOL_GPL(ata_pci_default_filter);
7116EXPORT_SYMBOL_GPL(ata_pci_clear_simplex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007117#endif /* CONFIG_PCI */
Jens Axboe9b847542006-01-06 09:28:07 +01007118
Tejun Heob64bbc32007-07-16 14:29:39 +09007119EXPORT_SYMBOL_GPL(__ata_ehi_push_desc);
7120EXPORT_SYMBOL_GPL(ata_ehi_push_desc);
7121EXPORT_SYMBOL_GPL(ata_ehi_clear_desc);
Tejun Heocbcdd872007-08-18 13:14:55 +09007122EXPORT_SYMBOL_GPL(ata_port_desc);
7123#ifdef CONFIG_PCI
7124EXPORT_SYMBOL_GPL(ata_port_pbar_desc);
7125#endif /* CONFIG_PCI */
Tejun Heoece1d632006-04-02 18:51:53 +09007126EXPORT_SYMBOL_GPL(ata_eng_timeout);
Tejun Heo7b70fc02006-05-15 20:58:07 +09007127EXPORT_SYMBOL_GPL(ata_port_schedule_eh);
Tejun Heodbd82612007-08-06 18:36:23 +09007128EXPORT_SYMBOL_GPL(ata_link_abort);
Tejun Heo7b70fc02006-05-15 20:58:07 +09007129EXPORT_SYMBOL_GPL(ata_port_abort);
Tejun Heoe3180492006-05-15 20:58:09 +09007130EXPORT_SYMBOL_GPL(ata_port_freeze);
7131EXPORT_SYMBOL_GPL(ata_eh_freeze_port);
7132EXPORT_SYMBOL_GPL(ata_eh_thaw_port);
Tejun Heoece1d632006-04-02 18:51:53 +09007133EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
7134EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
Tejun Heo022bdb02006-05-15 20:58:22 +09007135EXPORT_SYMBOL_GPL(ata_do_eh);
Akira Iguchi83625002007-01-26 16:27:32 +09007136EXPORT_SYMBOL_GPL(ata_irq_on);
Akira Iguchia619f981b2007-01-26 16:28:18 +09007137EXPORT_SYMBOL_GPL(ata_dev_try_classify);
Alan Coxbe0d18d2007-03-06 02:37:56 -08007138
7139EXPORT_SYMBOL_GPL(ata_cable_40wire);
7140EXPORT_SYMBOL_GPL(ata_cable_80wire);
7141EXPORT_SYMBOL_GPL(ata_cable_unknown);
7142EXPORT_SYMBOL_GPL(ata_cable_sata);