blob: b666f51da7ed740c6fd694fc7b765c7f1ff8d2fa [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 Garzikfda0efc2007-01-31 07:43:15 -050062
Tejun Heod7bb4cc2006-05-31 18:27:46 +090063/* debounce timing parameters in msecs { interval, duration, timeout } */
Tejun Heoe9c83912006-07-03 16:07:26 +090064const unsigned long sata_deb_timing_normal[] = { 5, 100, 2000 };
65const unsigned long sata_deb_timing_hotplug[] = { 25, 500, 2000 };
66const unsigned long sata_deb_timing_long[] = { 100, 2000, 5000 };
Tejun Heod7bb4cc2006-05-31 18:27:46 +090067
Tejun Heo3373efd2006-05-15 20:57:53 +090068static unsigned int ata_dev_init_params(struct ata_device *dev,
69 u16 heads, u16 sectors);
70static unsigned int ata_dev_set_xfermode(struct ata_device *dev);
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -040071static unsigned int ata_dev_set_AN(struct ata_device *dev, u8 enable);
Tejun Heo3373efd2006-05-15 20:57:53 +090072static void ata_dev_xfermask(struct ata_device *dev);
Tejun Heo75683fe2007-07-05 13:31:27 +090073static unsigned long ata_dev_blacklisted(const struct ata_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Tejun Heof3187192007-04-17 23:44:07 +090075unsigned int ata_print_id = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076static struct workqueue_struct *ata_wq;
77
Tejun Heo453b07a2006-05-31 18:27:42 +090078struct workqueue_struct *ata_aux_wq;
79
Jeff Garzik418dc1f2006-03-11 20:50:08 -050080int atapi_enabled = 1;
Jeff Garzik1623c812005-08-30 03:37:42 -040081module_param(atapi_enabled, int, 0444);
82MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)");
83
Albert Lee95de7192006-04-04 10:57:18 +080084int atapi_dmadir = 0;
85module_param(atapi_dmadir, int, 0444);
86MODULE_PARM_DESC(atapi_dmadir, "Enable ATAPI DMADIR bridge support (0=off, 1=on)");
87
Mark Lordbaf4fdf2007-08-08 01:08:45 +090088int atapi_passthru16 = 1;
89module_param(atapi_passthru16, int, 0444);
90MODULE_PARM_DESC(atapi_passthru16, "Enable ATA_16 passthru for ATAPI devices; on by default (0=off, 1=on)");
91
Jeff Garzikc3c013a2006-02-27 22:31:19 -050092int libata_fua = 0;
93module_param_named(fua, libata_fua, int, 0444);
94MODULE_PARM_DESC(fua, "FUA support (0=off, 1=on)");
95
Alan Cox1e999732007-04-11 00:23:13 +010096static int ata_ignore_hpa = 0;
97module_param_named(ignore_hpa, ata_ignore_hpa, int, 0644);
98MODULE_PARM_DESC(ignore_hpa, "Ignore HPA limit (0=keep BIOS limits, 1=ignore limits, using full disk)");
99
Andrew Mortona8601e52006-06-25 01:36:52 -0700100static int ata_probe_timeout = ATA_TMOUT_INTERNAL / HZ;
101module_param(ata_probe_timeout, int, 0444);
102MODULE_PARM_DESC(ata_probe_timeout, "Set ATA probing timeout (seconds)");
103
Jeff Garzikd7d0dad2007-03-28 01:57:37 -0400104int libata_noacpi = 1;
105module_param_named(noacpi, libata_noacpi, int, 0444);
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700106MODULE_PARM_DESC(noacpi, "Disables the use of ACPI in suspend/resume when set");
107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108MODULE_AUTHOR("Jeff Garzik");
109MODULE_DESCRIPTION("Library module for ATA devices");
110MODULE_LICENSE("GPL");
111MODULE_VERSION(DRV_VERSION);
112
Edward Falk0baab862005-06-02 18:17:13 -0400113
114/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
116 * @tf: Taskfile to convert
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 * @pmp: Port multiplier port
Tejun Heo99771262007-07-16 14:29:38 +0900118 * @is_cmd: This FIS is for command
119 * @fis: Buffer into which data will output
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 *
121 * Converts a standard ATA taskfile to a Serial ATA
122 * FIS structure (Register - Host to Device).
123 *
124 * LOCKING:
125 * Inherited from caller.
126 */
Tejun Heo99771262007-07-16 14:29:38 +0900127void ata_tf_to_fis(const struct ata_taskfile *tf, u8 pmp, int is_cmd, u8 *fis)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
Tejun Heo99771262007-07-16 14:29:38 +0900129 fis[0] = 0x27; /* Register - Host to Device FIS */
130 fis[1] = pmp & 0xf; /* Port multiplier number*/
131 if (is_cmd)
132 fis[1] |= (1 << 7); /* bit 7 indicates Command FIS */
133
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 fis[2] = tf->command;
135 fis[3] = tf->feature;
136
137 fis[4] = tf->lbal;
138 fis[5] = tf->lbam;
139 fis[6] = tf->lbah;
140 fis[7] = tf->device;
141
142 fis[8] = tf->hob_lbal;
143 fis[9] = tf->hob_lbam;
144 fis[10] = tf->hob_lbah;
145 fis[11] = tf->hob_feature;
146
147 fis[12] = tf->nsect;
148 fis[13] = tf->hob_nsect;
149 fis[14] = 0;
150 fis[15] = tf->ctl;
151
152 fis[16] = 0;
153 fis[17] = 0;
154 fis[18] = 0;
155 fis[19] = 0;
156}
157
158/**
159 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
160 * @fis: Buffer from which data will be input
161 * @tf: Taskfile to output
162 *
Mark Lorde12a1be2005-11-12 18:55:45 -0500163 * Converts a serial ATA FIS structure to a standard ATA taskfile.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 *
165 * LOCKING:
166 * Inherited from caller.
167 */
168
Jeff Garzik057ace52005-10-22 14:27:05 -0400169void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
171 tf->command = fis[2]; /* status */
172 tf->feature = fis[3]; /* error */
173
174 tf->lbal = fis[4];
175 tf->lbam = fis[5];
176 tf->lbah = fis[6];
177 tf->device = fis[7];
178
179 tf->hob_lbal = fis[8];
180 tf->hob_lbam = fis[9];
181 tf->hob_lbah = fis[10];
182
183 tf->nsect = fis[12];
184 tf->hob_nsect = fis[13];
185}
186
Albert Lee8cbd6df2005-10-12 15:06:27 +0800187static const u8 ata_rw_cmds[] = {
188 /* pio multi */
189 ATA_CMD_READ_MULTI,
190 ATA_CMD_WRITE_MULTI,
191 ATA_CMD_READ_MULTI_EXT,
192 ATA_CMD_WRITE_MULTI_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100193 0,
194 0,
195 0,
196 ATA_CMD_WRITE_MULTI_FUA_EXT,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800197 /* pio */
198 ATA_CMD_PIO_READ,
199 ATA_CMD_PIO_WRITE,
200 ATA_CMD_PIO_READ_EXT,
201 ATA_CMD_PIO_WRITE_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100202 0,
203 0,
204 0,
205 0,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800206 /* dma */
207 ATA_CMD_READ,
208 ATA_CMD_WRITE,
209 ATA_CMD_READ_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100210 ATA_CMD_WRITE_EXT,
211 0,
212 0,
213 0,
214 ATA_CMD_WRITE_FUA_EXT
Albert Lee8cbd6df2005-10-12 15:06:27 +0800215};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217/**
Albert Lee8cbd6df2005-10-12 15:06:27 +0800218 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
Tejun Heobd056d72006-11-14 22:47:10 +0900219 * @tf: command to examine and configure
220 * @dev: device tf belongs to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 *
Jeff Garzik2e9edbf2006-03-24 09:56:57 -0500222 * Examine the device configuration and tf->flags to calculate
Albert Lee8cbd6df2005-10-12 15:06:27 +0800223 * the proper read/write commands and protocol to use.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 *
225 * LOCKING:
226 * caller.
227 */
Tejun Heobd056d72006-11-14 22:47:10 +0900228static int ata_rwcmd_protocol(struct ata_taskfile *tf, struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100230 u8 cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100232 int index, fua, lba48, write;
Jeff Garzik2e9edbf2006-03-24 09:56:57 -0500233
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100234 fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800235 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
236 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Albert Lee8cbd6df2005-10-12 15:06:27 +0800238 if (dev->flags & ATA_DFLAG_PIO) {
239 tf->protocol = ATA_PROT_PIO;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100240 index = dev->multi_count ? 0 : 8;
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900241 } else if (lba48 && (dev->link->ap->flags & ATA_FLAG_PIO_LBA48)) {
Alan Cox8d238e02006-01-17 20:50:31 +0000242 /* Unable to use DMA due to host limitation */
243 tf->protocol = ATA_PROT_PIO;
Albert Lee0565c262006-02-13 18:55:25 +0800244 index = dev->multi_count ? 0 : 8;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800245 } else {
246 tf->protocol = ATA_PROT_DMA;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100247 index = 16;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100250 cmd = ata_rw_cmds[index + fua + lba48 + write];
251 if (cmd) {
252 tf->command = cmd;
253 return 0;
254 }
255 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256}
257
Tejun Heocb95d562006-03-06 04:31:56 +0900258/**
Tejun Heo35b649f2006-11-14 22:37:35 +0900259 * ata_tf_read_block - Read block address from ATA taskfile
260 * @tf: ATA taskfile of interest
261 * @dev: ATA device @tf belongs to
262 *
263 * LOCKING:
264 * None.
265 *
266 * Read block address from @tf. This function can handle all
267 * three address formats - LBA, LBA48 and CHS. tf->protocol and
268 * flags select the address format to use.
269 *
270 * RETURNS:
271 * Block address read from @tf.
272 */
273u64 ata_tf_read_block(struct ata_taskfile *tf, struct ata_device *dev)
274{
275 u64 block = 0;
276
277 if (tf->flags & ATA_TFLAG_LBA) {
278 if (tf->flags & ATA_TFLAG_LBA48) {
279 block |= (u64)tf->hob_lbah << 40;
280 block |= (u64)tf->hob_lbam << 32;
281 block |= tf->hob_lbal << 24;
282 } else
283 block |= (tf->device & 0xf) << 24;
284
285 block |= tf->lbah << 16;
286 block |= tf->lbam << 8;
287 block |= tf->lbal;
288 } else {
289 u32 cyl, head, sect;
290
291 cyl = tf->lbam | (tf->lbah << 8);
292 head = tf->device & 0xf;
293 sect = tf->lbal;
294
295 block = (cyl * dev->heads + head) * dev->sectors + sect;
296 }
297
298 return block;
299}
300
301/**
Tejun Heobd056d72006-11-14 22:47:10 +0900302 * ata_build_rw_tf - Build ATA taskfile for given read/write request
303 * @tf: Target ATA taskfile
304 * @dev: ATA device @tf belongs to
305 * @block: Block address
306 * @n_block: Number of blocks
307 * @tf_flags: RW/FUA etc...
308 * @tag: tag
309 *
310 * LOCKING:
311 * None.
312 *
313 * Build ATA taskfile @tf for read/write request described by
314 * @block, @n_block, @tf_flags and @tag on @dev.
315 *
316 * RETURNS:
317 *
318 * 0 on success, -ERANGE if the request is too large for @dev,
319 * -EINVAL if the request is invalid.
320 */
321int ata_build_rw_tf(struct ata_taskfile *tf, struct ata_device *dev,
322 u64 block, u32 n_block, unsigned int tf_flags,
323 unsigned int tag)
324{
325 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
326 tf->flags |= tf_flags;
327
Tejun Heo6d1245b2007-02-20 23:20:27 +0900328 if (ata_ncq_enabled(dev) && likely(tag != ATA_TAG_INTERNAL)) {
Tejun Heobd056d72006-11-14 22:47:10 +0900329 /* yay, NCQ */
330 if (!lba_48_ok(block, n_block))
331 return -ERANGE;
332
333 tf->protocol = ATA_PROT_NCQ;
334 tf->flags |= ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
335
336 if (tf->flags & ATA_TFLAG_WRITE)
337 tf->command = ATA_CMD_FPDMA_WRITE;
338 else
339 tf->command = ATA_CMD_FPDMA_READ;
340
341 tf->nsect = tag << 3;
342 tf->hob_feature = (n_block >> 8) & 0xff;
343 tf->feature = n_block & 0xff;
344
345 tf->hob_lbah = (block >> 40) & 0xff;
346 tf->hob_lbam = (block >> 32) & 0xff;
347 tf->hob_lbal = (block >> 24) & 0xff;
348 tf->lbah = (block >> 16) & 0xff;
349 tf->lbam = (block >> 8) & 0xff;
350 tf->lbal = block & 0xff;
351
352 tf->device = 1 << 6;
353 if (tf->flags & ATA_TFLAG_FUA)
354 tf->device |= 1 << 7;
355 } else if (dev->flags & ATA_DFLAG_LBA) {
356 tf->flags |= ATA_TFLAG_LBA;
357
358 if (lba_28_ok(block, n_block)) {
359 /* use LBA28 */
360 tf->device |= (block >> 24) & 0xf;
361 } else if (lba_48_ok(block, n_block)) {
362 if (!(dev->flags & ATA_DFLAG_LBA48))
363 return -ERANGE;
364
365 /* use LBA48 */
366 tf->flags |= ATA_TFLAG_LBA48;
367
368 tf->hob_nsect = (n_block >> 8) & 0xff;
369
370 tf->hob_lbah = (block >> 40) & 0xff;
371 tf->hob_lbam = (block >> 32) & 0xff;
372 tf->hob_lbal = (block >> 24) & 0xff;
373 } else
374 /* request too large even for LBA48 */
375 return -ERANGE;
376
377 if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
378 return -EINVAL;
379
380 tf->nsect = n_block & 0xff;
381
382 tf->lbah = (block >> 16) & 0xff;
383 tf->lbam = (block >> 8) & 0xff;
384 tf->lbal = block & 0xff;
385
386 tf->device |= ATA_LBA;
387 } else {
388 /* CHS */
389 u32 sect, head, cyl, track;
390
391 /* The request -may- be too large for CHS addressing. */
392 if (!lba_28_ok(block, n_block))
393 return -ERANGE;
394
395 if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
396 return -EINVAL;
397
398 /* Convert LBA to CHS */
399 track = (u32)block / dev->sectors;
400 cyl = track / dev->heads;
401 head = track % dev->heads;
402 sect = (u32)block % dev->sectors + 1;
403
404 DPRINTK("block %u track %u cyl %u head %u sect %u\n",
405 (u32)block, track, cyl, head, sect);
406
407 /* Check whether the converted CHS can fit.
408 Cylinder: 0-65535
409 Head: 0-15
410 Sector: 1-255*/
411 if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
412 return -ERANGE;
413
414 tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
415 tf->lbal = sect;
416 tf->lbam = cyl;
417 tf->lbah = cyl >> 8;
418 tf->device |= head;
419 }
420
421 return 0;
422}
423
424/**
Tejun Heocb95d562006-03-06 04:31:56 +0900425 * ata_pack_xfermask - Pack pio, mwdma and udma masks into xfer_mask
426 * @pio_mask: pio_mask
427 * @mwdma_mask: mwdma_mask
428 * @udma_mask: udma_mask
429 *
430 * Pack @pio_mask, @mwdma_mask and @udma_mask into a single
431 * unsigned int xfer_mask.
432 *
433 * LOCKING:
434 * None.
435 *
436 * RETURNS:
437 * Packed xfer_mask.
438 */
439static unsigned int ata_pack_xfermask(unsigned int pio_mask,
440 unsigned int mwdma_mask,
441 unsigned int udma_mask)
442{
443 return ((pio_mask << ATA_SHIFT_PIO) & ATA_MASK_PIO) |
444 ((mwdma_mask << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA) |
445 ((udma_mask << ATA_SHIFT_UDMA) & ATA_MASK_UDMA);
446}
447
Tejun Heoc0489e42006-03-24 14:07:49 +0900448/**
449 * ata_unpack_xfermask - Unpack xfer_mask into pio, mwdma and udma masks
450 * @xfer_mask: xfer_mask to unpack
451 * @pio_mask: resulting pio_mask
452 * @mwdma_mask: resulting mwdma_mask
453 * @udma_mask: resulting udma_mask
454 *
455 * Unpack @xfer_mask into @pio_mask, @mwdma_mask and @udma_mask.
456 * Any NULL distination masks will be ignored.
457 */
458static void ata_unpack_xfermask(unsigned int xfer_mask,
459 unsigned int *pio_mask,
460 unsigned int *mwdma_mask,
461 unsigned int *udma_mask)
462{
463 if (pio_mask)
464 *pio_mask = (xfer_mask & ATA_MASK_PIO) >> ATA_SHIFT_PIO;
465 if (mwdma_mask)
466 *mwdma_mask = (xfer_mask & ATA_MASK_MWDMA) >> ATA_SHIFT_MWDMA;
467 if (udma_mask)
468 *udma_mask = (xfer_mask & ATA_MASK_UDMA) >> ATA_SHIFT_UDMA;
469}
470
Tejun Heocb95d562006-03-06 04:31:56 +0900471static const struct ata_xfer_ent {
Tejun Heobe9a50c2006-03-31 22:48:52 +0900472 int shift, bits;
Tejun Heocb95d562006-03-06 04:31:56 +0900473 u8 base;
474} ata_xfer_tbl[] = {
475 { ATA_SHIFT_PIO, ATA_BITS_PIO, XFER_PIO_0 },
476 { ATA_SHIFT_MWDMA, ATA_BITS_MWDMA, XFER_MW_DMA_0 },
477 { ATA_SHIFT_UDMA, ATA_BITS_UDMA, XFER_UDMA_0 },
478 { -1, },
479};
480
481/**
482 * ata_xfer_mask2mode - Find matching XFER_* for the given xfer_mask
483 * @xfer_mask: xfer_mask of interest
484 *
485 * Return matching XFER_* value for @xfer_mask. Only the highest
486 * bit of @xfer_mask is considered.
487 *
488 * LOCKING:
489 * None.
490 *
491 * RETURNS:
492 * Matching XFER_* value, 0 if no match found.
493 */
494static u8 ata_xfer_mask2mode(unsigned int xfer_mask)
495{
496 int highbit = fls(xfer_mask) - 1;
497 const struct ata_xfer_ent *ent;
498
499 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
500 if (highbit >= ent->shift && highbit < ent->shift + ent->bits)
501 return ent->base + highbit - ent->shift;
502 return 0;
503}
504
505/**
506 * ata_xfer_mode2mask - Find matching xfer_mask for XFER_*
507 * @xfer_mode: XFER_* of interest
508 *
509 * Return matching xfer_mask for @xfer_mode.
510 *
511 * LOCKING:
512 * None.
513 *
514 * RETURNS:
515 * Matching xfer_mask, 0 if no match found.
516 */
517static unsigned int ata_xfer_mode2mask(u8 xfer_mode)
518{
519 const struct ata_xfer_ent *ent;
520
521 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
522 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
523 return 1 << (ent->shift + xfer_mode - ent->base);
524 return 0;
525}
526
527/**
528 * ata_xfer_mode2shift - Find matching xfer_shift for XFER_*
529 * @xfer_mode: XFER_* of interest
530 *
531 * Return matching xfer_shift for @xfer_mode.
532 *
533 * LOCKING:
534 * None.
535 *
536 * RETURNS:
537 * Matching xfer_shift, -1 if no match found.
538 */
539static int ata_xfer_mode2shift(unsigned int xfer_mode)
540{
541 const struct ata_xfer_ent *ent;
542
543 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
544 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
545 return ent->shift;
546 return -1;
547}
548
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549/**
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900550 * ata_mode_string - convert xfer_mask to string
551 * @xfer_mask: mask of bits supported; only highest bit counts.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 *
553 * Determine string which represents the highest speed
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900554 * (highest bit in @modemask).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 *
556 * LOCKING:
557 * None.
558 *
559 * RETURNS:
560 * Constant C string representing highest speed listed in
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900561 * @mode_mask, or the constant C string "<n/a>".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 */
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900563static const char *ata_mode_string(unsigned int xfer_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
Tejun Heo75f554b2006-03-06 04:31:57 +0900565 static const char * const xfer_mode_str[] = {
566 "PIO0",
567 "PIO1",
568 "PIO2",
569 "PIO3",
570 "PIO4",
Alan Coxb352e572006-08-10 18:52:12 +0100571 "PIO5",
572 "PIO6",
Tejun Heo75f554b2006-03-06 04:31:57 +0900573 "MWDMA0",
574 "MWDMA1",
575 "MWDMA2",
Alan Coxb352e572006-08-10 18:52:12 +0100576 "MWDMA3",
577 "MWDMA4",
Tejun Heo75f554b2006-03-06 04:31:57 +0900578 "UDMA/16",
579 "UDMA/25",
580 "UDMA/33",
581 "UDMA/44",
582 "UDMA/66",
583 "UDMA/100",
584 "UDMA/133",
585 "UDMA7",
586 };
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900587 int highbit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900589 highbit = fls(xfer_mask) - 1;
590 if (highbit >= 0 && highbit < ARRAY_SIZE(xfer_mode_str))
591 return xfer_mode_str[highbit];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 return "<n/a>";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593}
594
Tejun Heo4c360c82006-04-01 01:38:17 +0900595static const char *sata_spd_string(unsigned int spd)
596{
597 static const char * const spd_str[] = {
598 "1.5 Gbps",
599 "3.0 Gbps",
600 };
601
602 if (spd == 0 || (spd - 1) >= ARRAY_SIZE(spd_str))
603 return "<unknown>";
604 return spd_str[spd - 1];
605}
606
Tejun Heo3373efd2006-05-15 20:57:53 +0900607void ata_dev_disable(struct ata_device *dev)
Tejun Heo0b8efb02006-03-24 15:25:31 +0900608{
Tejun Heo09d7f9b2007-06-25 23:34:02 +0900609 if (ata_dev_enabled(dev)) {
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900610 if (ata_msg_drv(dev->link->ap))
Tejun Heo09d7f9b2007-06-25 23:34:02 +0900611 ata_dev_printk(dev, KERN_WARNING, "disabled\n");
Tejun Heo4ae72a12007-02-02 16:22:30 +0900612 ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO0 |
613 ATA_DNXFER_QUIET);
Tejun Heo0b8efb02006-03-24 15:25:31 +0900614 dev->class++;
615 }
616}
617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 * ata_devchk - PATA device presence detection
620 * @ap: ATA channel to examine
621 * @device: Device to examine (starting at zero)
622 *
Tejun Heo0d5ff562007-02-01 15:06:36 +0900623 * This technique was originally described in
624 * Hale Landis's ATADRVR (www.ata-atapi.com), and
625 * later found its way into the ATA/ATAPI spec.
626 *
627 * Write a pattern to the ATA shadow registers,
628 * and if a device is present, it will respond by
629 * correctly storing and echoing back the
630 * ATA shadow register contents.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 *
632 * LOCKING:
633 * caller.
634 */
635
Tejun Heo0d5ff562007-02-01 15:06:36 +0900636static unsigned int ata_devchk(struct ata_port *ap, unsigned int device)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637{
Tejun Heo0d5ff562007-02-01 15:06:36 +0900638 struct ata_ioports *ioaddr = &ap->ioaddr;
639 u8 nsect, lbal;
640
641 ap->ops->dev_select(ap, device);
642
643 iowrite8(0x55, ioaddr->nsect_addr);
644 iowrite8(0xaa, ioaddr->lbal_addr);
645
646 iowrite8(0xaa, ioaddr->nsect_addr);
647 iowrite8(0x55, ioaddr->lbal_addr);
648
649 iowrite8(0x55, ioaddr->nsect_addr);
650 iowrite8(0xaa, ioaddr->lbal_addr);
651
652 nsect = ioread8(ioaddr->nsect_addr);
653 lbal = ioread8(ioaddr->lbal_addr);
654
655 if ((nsect == 0x55) && (lbal == 0xaa))
656 return 1; /* we found a device */
657
658 return 0; /* nothing found */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659}
660
661/**
662 * ata_dev_classify - determine device type based on ATA-spec signature
663 * @tf: ATA taskfile register set for device to be identified
664 *
665 * Determine from taskfile register contents whether a device is
666 * ATA or ATAPI, as per "Signature and persistence" section
667 * of ATA/PI spec (volume 1, sect 5.14).
668 *
669 * LOCKING:
670 * None.
671 *
672 * RETURNS:
673 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
674 * the event of failure.
675 */
676
Jeff Garzik057ace52005-10-22 14:27:05 -0400677unsigned int ata_dev_classify(const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678{
679 /* Apple's open source Darwin code hints that some devices only
680 * put a proper signature into the LBA mid/high registers,
681 * So, we only check those. It's sufficient for uniqueness.
682 */
683
684 if (((tf->lbam == 0) && (tf->lbah == 0)) ||
685 ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
686 DPRINTK("found ATA device by sig\n");
687 return ATA_DEV_ATA;
688 }
689
690 if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
691 ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
692 DPRINTK("found ATAPI device by sig\n");
693 return ATA_DEV_ATAPI;
694 }
695
696 DPRINTK("unknown device\n");
697 return ATA_DEV_UNKNOWN;
698}
699
700/**
701 * ata_dev_try_classify - Parse returned ATA device signature
Tejun Heo3f198592007-09-02 23:23:57 +0900702 * @dev: ATA device to classify (starting at zero)
703 * @present: device seems present
Tejun Heob4dc7622006-01-24 17:05:22 +0900704 * @r_err: Value of error register on completion
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 *
706 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
707 * an ATA/ATAPI-defined set of values is placed in the ATA
708 * shadow registers, indicating the results of device detection
709 * and diagnostics.
710 *
711 * Select the ATA device, and read the values from the ATA shadow
712 * registers. Then parse according to the Error register value,
713 * and the spec-defined values examined by ata_dev_classify().
714 *
715 * LOCKING:
716 * caller.
Tejun Heob4dc7622006-01-24 17:05:22 +0900717 *
718 * RETURNS:
719 * Device type - %ATA_DEV_ATA, %ATA_DEV_ATAPI or %ATA_DEV_NONE.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 */
Tejun Heo3f198592007-09-02 23:23:57 +0900721unsigned int ata_dev_try_classify(struct ata_device *dev, int present,
722 u8 *r_err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
Tejun Heo3f198592007-09-02 23:23:57 +0900724 struct ata_port *ap = dev->link->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 struct ata_taskfile tf;
726 unsigned int class;
727 u8 err;
728
Tejun Heo3f198592007-09-02 23:23:57 +0900729 ap->ops->dev_select(ap, dev->devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
731 memset(&tf, 0, sizeof(tf));
732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 ap->ops->tf_read(ap, &tf);
Jeff Garzik0169e282005-10-29 21:25:10 -0400734 err = tf.feature;
Tejun Heob4dc7622006-01-24 17:05:22 +0900735 if (r_err)
736 *r_err = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Alan Cox93590852006-09-12 16:55:12 +0100738 /* see if device passed diags: if master then continue and warn later */
Tejun Heo3f198592007-09-02 23:23:57 +0900739 if (err == 0 && dev->devno == 0)
Alan Cox93590852006-09-12 16:55:12 +0100740 /* diagnostic fail : do nothing _YET_ */
Tejun Heo3f198592007-09-02 23:23:57 +0900741 dev->horkage |= ATA_HORKAGE_DIAGNOSTIC;
Alan Cox93590852006-09-12 16:55:12 +0100742 else if (err == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 /* do nothing */ ;
Tejun Heo3f198592007-09-02 23:23:57 +0900744 else if ((dev->devno == 0) && (err == 0x81))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 /* do nothing */ ;
746 else
Tejun Heob4dc7622006-01-24 17:05:22 +0900747 return ATA_DEV_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
Tejun Heob4dc7622006-01-24 17:05:22 +0900749 /* determine if device is ATA or ATAPI */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 class = ata_dev_classify(&tf);
Tejun Heob4dc7622006-01-24 17:05:22 +0900751
Tejun Heod7fbee02007-09-02 23:24:48 +0900752 if (class == ATA_DEV_UNKNOWN) {
753 /* If the device failed diagnostic, it's likely to
754 * have reported incorrect device signature too.
755 * Assume ATA device if the device seems present but
756 * device signature is invalid with diagnostic
757 * failure.
758 */
759 if (present && (dev->horkage & ATA_HORKAGE_DIAGNOSTIC))
760 class = ATA_DEV_ATA;
761 else
762 class = ATA_DEV_NONE;
763 } else if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
764 class = ATA_DEV_NONE;
765
Tejun Heob4dc7622006-01-24 17:05:22 +0900766 return class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767}
768
769/**
Tejun Heo6a62a042006-02-13 10:02:46 +0900770 * ata_id_string - Convert IDENTIFY DEVICE page into string
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 * @id: IDENTIFY DEVICE results we will examine
772 * @s: string into which data is output
773 * @ofs: offset into identify device page
774 * @len: length of string to return. must be an even number.
775 *
776 * The strings in the IDENTIFY DEVICE page are broken up into
777 * 16-bit chunks. Run through the string, and output each
778 * 8-bit chunk linearly, regardless of platform.
779 *
780 * LOCKING:
781 * caller.
782 */
783
Tejun Heo6a62a042006-02-13 10:02:46 +0900784void ata_id_string(const u16 *id, unsigned char *s,
785 unsigned int ofs, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
787 unsigned int c;
788
789 while (len > 0) {
790 c = id[ofs] >> 8;
791 *s = c;
792 s++;
793
794 c = id[ofs] & 0xff;
795 *s = c;
796 s++;
797
798 ofs++;
799 len -= 2;
800 }
801}
802
Tejun Heo0e949ff2006-02-12 22:47:04 +0900803/**
Tejun Heo6a62a042006-02-13 10:02:46 +0900804 * ata_id_c_string - Convert IDENTIFY DEVICE page into C string
Tejun Heo0e949ff2006-02-12 22:47:04 +0900805 * @id: IDENTIFY DEVICE results we will examine
806 * @s: string into which data is output
807 * @ofs: offset into identify device page
808 * @len: length of string to return. must be an odd number.
809 *
Tejun Heo6a62a042006-02-13 10:02:46 +0900810 * This function is identical to ata_id_string except that it
Tejun Heo0e949ff2006-02-12 22:47:04 +0900811 * trims trailing spaces and terminates the resulting string with
812 * null. @len must be actual maximum length (even number) + 1.
813 *
814 * LOCKING:
815 * caller.
816 */
Tejun Heo6a62a042006-02-13 10:02:46 +0900817void ata_id_c_string(const u16 *id, unsigned char *s,
818 unsigned int ofs, unsigned int len)
Tejun Heo0e949ff2006-02-12 22:47:04 +0900819{
820 unsigned char *p;
821
822 WARN_ON(!(len & 1));
823
Tejun Heo6a62a042006-02-13 10:02:46 +0900824 ata_id_string(id, s, ofs, len - 1);
Tejun Heo0e949ff2006-02-12 22:47:04 +0900825
826 p = s + strnlen(s, len - 1);
827 while (p > s && p[-1] == ' ')
828 p--;
829 *p = '\0';
830}
Edward Falk0baab862005-06-02 18:17:13 -0400831
Tejun Heodb6f8752007-09-03 12:31:58 +0900832static u64 ata_id_n_sectors(const u16 *id)
833{
834 if (ata_id_has_lba(id)) {
835 if (ata_id_has_lba48(id))
836 return ata_id_u64(id, 100);
837 else
838 return ata_id_u32(id, 60);
839 } else {
840 if (ata_id_current_chs_valid(id))
841 return ata_id_u32(id, 57);
842 else
843 return id[1] * id[3] * id[6];
844 }
845}
846
Alan Cox1e999732007-04-11 00:23:13 +0100847static u64 ata_tf_to_lba48(struct ata_taskfile *tf)
848{
849 u64 sectors = 0;
850
851 sectors |= ((u64)(tf->hob_lbah & 0xff)) << 40;
852 sectors |= ((u64)(tf->hob_lbam & 0xff)) << 32;
853 sectors |= (tf->hob_lbal & 0xff) << 24;
854 sectors |= (tf->lbah & 0xff) << 16;
855 sectors |= (tf->lbam & 0xff) << 8;
856 sectors |= (tf->lbal & 0xff);
857
858 return ++sectors;
859}
860
861static u64 ata_tf_to_lba(struct ata_taskfile *tf)
862{
863 u64 sectors = 0;
864
865 sectors |= (tf->device & 0x0f) << 24;
866 sectors |= (tf->lbah & 0xff) << 16;
867 sectors |= (tf->lbam & 0xff) << 8;
868 sectors |= (tf->lbal & 0xff);
869
870 return ++sectors;
871}
872
873/**
Tejun Heoc728a912007-09-03 12:32:30 +0900874 * ata_read_native_max_address - Read native max address
875 * @dev: target device
876 * @max_sectors: out parameter for the result native max address
Alan Cox1e999732007-04-11 00:23:13 +0100877 *
Tejun Heoc728a912007-09-03 12:32:30 +0900878 * Perform an LBA48 or LBA28 native size query upon the device in
879 * question.
880 *
881 * RETURNS:
882 * 0 on success, -EACCES if command is aborted by the drive.
883 * -EIO on other errors.
Alan Cox1e999732007-04-11 00:23:13 +0100884 */
Tejun Heoc728a912007-09-03 12:32:30 +0900885static int ata_read_native_max_address(struct ata_device *dev, u64 *max_sectors)
Alan Cox1e999732007-04-11 00:23:13 +0100886{
Tejun Heoc728a912007-09-03 12:32:30 +0900887 unsigned int err_mask;
Alan Cox1e999732007-04-11 00:23:13 +0100888 struct ata_taskfile tf;
Tejun Heoc728a912007-09-03 12:32:30 +0900889 int lba48 = ata_id_has_lba48(dev->id);
Alan Cox1e999732007-04-11 00:23:13 +0100890
891 ata_tf_init(dev, &tf);
892
Tejun Heoc728a912007-09-03 12:32:30 +0900893 /* always clear all address registers */
Alan Cox1e999732007-04-11 00:23:13 +0100894 tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
Tejun Heoc728a912007-09-03 12:32:30 +0900895
896 if (lba48) {
897 tf.command = ATA_CMD_READ_NATIVE_MAX_EXT;
898 tf.flags |= ATA_TFLAG_LBA48;
899 } else
900 tf.command = ATA_CMD_READ_NATIVE_MAX;
901
Alan Cox1e999732007-04-11 00:23:13 +0100902 tf.protocol |= ATA_PROT_NODATA;
Tejun Heoc728a912007-09-03 12:32:30 +0900903 tf.device |= ATA_LBA;
Alan Cox1e999732007-04-11 00:23:13 +0100904
Tejun Heoc728a912007-09-03 12:32:30 +0900905 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
906 if (err_mask) {
907 ata_dev_printk(dev, KERN_WARNING, "failed to read native "
908 "max address (err_mask=0x%x)\n", err_mask);
909 if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
910 return -EACCES;
911 return -EIO;
912 }
Alan Cox1e999732007-04-11 00:23:13 +0100913
Tejun Heoc728a912007-09-03 12:32:30 +0900914 if (lba48)
915 *max_sectors = ata_tf_to_lba48(&tf);
916 else
917 *max_sectors = ata_tf_to_lba(&tf);
918
919 return 0;
Alan Cox1e999732007-04-11 00:23:13 +0100920}
921
922/**
Tejun Heoc728a912007-09-03 12:32:30 +0900923 * ata_set_max_sectors - Set max sectors
924 * @dev: target device
Randy Dunlap6b38d1d2007-05-01 17:35:55 -0700925 * @new_sectors: new max sectors value to set for the device
Alan Cox1e999732007-04-11 00:23:13 +0100926 *
Tejun Heoc728a912007-09-03 12:32:30 +0900927 * Set max sectors of @dev to @new_sectors.
928 *
929 * RETURNS:
930 * 0 on success, -EACCES if command is aborted or denied (due to
931 * previous non-volatile SET_MAX) by the drive. -EIO on other
932 * errors.
Alan Cox1e999732007-04-11 00:23:13 +0100933 */
Tejun Heo05027ad2007-09-03 12:32:57 +0900934static int ata_set_max_sectors(struct ata_device *dev, u64 new_sectors)
Alan Cox1e999732007-04-11 00:23:13 +0100935{
Tejun Heoc728a912007-09-03 12:32:30 +0900936 unsigned int err_mask;
Alan Cox1e999732007-04-11 00:23:13 +0100937 struct ata_taskfile tf;
Tejun Heoc728a912007-09-03 12:32:30 +0900938 int lba48 = ata_id_has_lba48(dev->id);
Alan Cox1e999732007-04-11 00:23:13 +0100939
940 new_sectors--;
941
942 ata_tf_init(dev, &tf);
943
Alan Cox1e999732007-04-11 00:23:13 +0100944 tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
Tejun Heoc728a912007-09-03 12:32:30 +0900945
946 if (lba48) {
947 tf.command = ATA_CMD_SET_MAX_EXT;
948 tf.flags |= ATA_TFLAG_LBA48;
949
950 tf.hob_lbal = (new_sectors >> 24) & 0xff;
951 tf.hob_lbam = (new_sectors >> 32) & 0xff;
952 tf.hob_lbah = (new_sectors >> 40) & 0xff;
953 } else
954 tf.command = ATA_CMD_SET_MAX;
955
Alan Cox1e999732007-04-11 00:23:13 +0100956 tf.protocol |= ATA_PROT_NODATA;
Tejun Heoc728a912007-09-03 12:32:30 +0900957 tf.device |= ATA_LBA;
Alan Cox1e999732007-04-11 00:23:13 +0100958
959 tf.lbal = (new_sectors >> 0) & 0xff;
960 tf.lbam = (new_sectors >> 8) & 0xff;
961 tf.lbah = (new_sectors >> 16) & 0xff;
Alan Cox1e999732007-04-11 00:23:13 +0100962
Tejun Heoc728a912007-09-03 12:32:30 +0900963 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
964 if (err_mask) {
965 ata_dev_printk(dev, KERN_WARNING, "failed to set "
966 "max address (err_mask=0x%x)\n", err_mask);
967 if (err_mask == AC_ERR_DEV &&
968 (tf.feature & (ATA_ABORTED | ATA_IDNF)))
969 return -EACCES;
970 return -EIO;
971 }
Alan Cox1e999732007-04-11 00:23:13 +0100972
Tejun Heoc728a912007-09-03 12:32:30 +0900973 return 0;
Alan Cox1e999732007-04-11 00:23:13 +0100974}
975
976/**
977 * ata_hpa_resize - Resize a device with an HPA set
978 * @dev: Device to resize
979 *
980 * Read the size of an LBA28 or LBA48 disk with HPA features and resize
981 * it if required to the full size of the media. The caller must check
982 * the drive has the HPA feature set enabled.
Tejun Heo05027ad2007-09-03 12:32:57 +0900983 *
984 * RETURNS:
985 * 0 on success, -errno on failure.
Alan Cox1e999732007-04-11 00:23:13 +0100986 */
Tejun Heo05027ad2007-09-03 12:32:57 +0900987static int ata_hpa_resize(struct ata_device *dev)
Alan Cox1e999732007-04-11 00:23:13 +0100988{
Tejun Heo05027ad2007-09-03 12:32:57 +0900989 struct ata_eh_context *ehc = &dev->link->eh_context;
990 int print_info = ehc->i.flags & ATA_EHI_PRINTINFO;
991 u64 sectors = ata_id_n_sectors(dev->id);
992 u64 native_sectors;
Tejun Heoc728a912007-09-03 12:32:30 +0900993 int rc;
Jeff Garzika617c092007-05-21 20:14:23 -0400994
Tejun Heo05027ad2007-09-03 12:32:57 +0900995 /* do we need to do it? */
996 if (dev->class != ATA_DEV_ATA ||
997 !ata_id_has_lba(dev->id) || !ata_id_hpa_enabled(dev->id) ||
998 (dev->horkage & ATA_HORKAGE_BROKEN_HPA))
Tejun Heoc728a912007-09-03 12:32:30 +0900999 return 0;
Alan Cox1e999732007-04-11 00:23:13 +01001000
Tejun Heo05027ad2007-09-03 12:32:57 +09001001 /* read native max address */
1002 rc = ata_read_native_max_address(dev, &native_sectors);
1003 if (rc) {
1004 /* If HPA isn't going to be unlocked, skip HPA
1005 * resizing from the next try.
1006 */
1007 if (!ata_ignore_hpa) {
1008 ata_dev_printk(dev, KERN_WARNING, "HPA support seems "
1009 "broken, will skip HPA handling\n");
1010 dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
Alan Cox1e999732007-04-11 00:23:13 +01001011
Tejun Heo05027ad2007-09-03 12:32:57 +09001012 /* we can continue if device aborted the command */
1013 if (rc == -EACCES)
1014 rc = 0;
Alan Cox1e999732007-04-11 00:23:13 +01001015 }
Tejun Heo37301a52007-06-25 20:45:54 +09001016
Tejun Heo05027ad2007-09-03 12:32:57 +09001017 return rc;
1018 }
1019
1020 /* nothing to do? */
1021 if (native_sectors <= sectors || !ata_ignore_hpa) {
1022 if (!print_info || native_sectors == sectors)
1023 return 0;
1024
1025 if (native_sectors > sectors)
1026 ata_dev_printk(dev, KERN_INFO,
1027 "HPA detected: current %llu, native %llu\n",
1028 (unsigned long long)sectors,
1029 (unsigned long long)native_sectors);
1030 else if (native_sectors < sectors)
1031 ata_dev_printk(dev, KERN_WARNING,
1032 "native sectors (%llu) is smaller than "
1033 "sectors (%llu)\n",
1034 (unsigned long long)native_sectors,
1035 (unsigned long long)sectors);
1036 return 0;
1037 }
1038
1039 /* let's unlock HPA */
1040 rc = ata_set_max_sectors(dev, native_sectors);
1041 if (rc == -EACCES) {
1042 /* if device aborted the command, skip HPA resizing */
1043 ata_dev_printk(dev, KERN_WARNING, "device aborted resize "
1044 "(%llu -> %llu), skipping HPA handling\n",
1045 (unsigned long long)sectors,
1046 (unsigned long long)native_sectors);
1047 dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
1048 return 0;
1049 } else if (rc)
1050 return rc;
1051
1052 /* re-read IDENTIFY data */
1053 rc = ata_dev_reread_id(dev, 0);
1054 if (rc) {
1055 ata_dev_printk(dev, KERN_ERR, "failed to re-read IDENTIFY "
1056 "data after HPA resizing\n");
1057 return rc;
1058 }
1059
1060 if (print_info) {
1061 u64 new_sectors = ata_id_n_sectors(dev->id);
1062 ata_dev_printk(dev, KERN_INFO,
1063 "HPA unlocked: %llu -> %llu, native %llu\n",
1064 (unsigned long long)sectors,
1065 (unsigned long long)new_sectors,
1066 (unsigned long long)native_sectors);
1067 }
1068
1069 return 0;
Alan Cox1e999732007-04-11 00:23:13 +01001070}
1071
Edward Falk0baab862005-06-02 18:17:13 -04001072/**
Alan10305f02007-02-20 18:01:59 +00001073 * ata_id_to_dma_mode - Identify DMA mode from id block
1074 * @dev: device to identify
Randy Dunlapcc261262007-03-16 19:55:47 -07001075 * @unknown: mode to assume if we cannot tell
Alan10305f02007-02-20 18:01:59 +00001076 *
1077 * Set up the timing values for the device based upon the identify
1078 * reported values for the DMA mode. This function is used by drivers
1079 * which rely upon firmware configured modes, but wish to report the
1080 * mode correctly when possible.
1081 *
1082 * In addition we emit similarly formatted messages to the default
1083 * ata_dev_set_mode handler, in order to provide consistency of
1084 * presentation.
1085 */
1086
1087void ata_id_to_dma_mode(struct ata_device *dev, u8 unknown)
1088{
1089 unsigned int mask;
1090 u8 mode;
1091
1092 /* Pack the DMA modes */
1093 mask = ((dev->id[63] >> 8) << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA;
1094 if (dev->id[53] & 0x04)
1095 mask |= ((dev->id[88] >> 8) << ATA_SHIFT_UDMA) & ATA_MASK_UDMA;
1096
1097 /* Select the mode in use */
1098 mode = ata_xfer_mask2mode(mask);
1099
1100 if (mode != 0) {
1101 ata_dev_printk(dev, KERN_INFO, "configured for %s\n",
1102 ata_mode_string(mask));
1103 } else {
1104 /* SWDMA perhaps ? */
1105 mode = unknown;
1106 ata_dev_printk(dev, KERN_INFO, "configured for DMA\n");
1107 }
1108
1109 /* Configure the device reporting */
1110 dev->xfer_mode = mode;
1111 dev->xfer_shift = ata_xfer_mode2shift(mode);
1112}
1113
1114/**
Edward Falk0baab862005-06-02 18:17:13 -04001115 * ata_noop_dev_select - Select device 0/1 on ATA bus
1116 * @ap: ATA channel to manipulate
1117 * @device: ATA device (numbered from zero) to select
1118 *
1119 * This function performs no actual function.
1120 *
1121 * May be used as the dev_select() entry in ata_port_operations.
1122 *
1123 * LOCKING:
1124 * caller.
1125 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
1127{
1128}
1129
Edward Falk0baab862005-06-02 18:17:13 -04001130
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131/**
1132 * ata_std_dev_select - Select device 0/1 on ATA bus
1133 * @ap: ATA channel to manipulate
1134 * @device: ATA device (numbered from zero) to select
1135 *
1136 * Use the method defined in the ATA specification to
1137 * make either device 0, or device 1, active on the
Edward Falk0baab862005-06-02 18:17:13 -04001138 * ATA channel. Works with both PIO and MMIO.
1139 *
1140 * May be used as the dev_select() entry in ata_port_operations.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 *
1142 * LOCKING:
1143 * caller.
1144 */
1145
1146void ata_std_dev_select (struct ata_port *ap, unsigned int device)
1147{
1148 u8 tmp;
1149
1150 if (device == 0)
1151 tmp = ATA_DEVICE_OBS;
1152 else
1153 tmp = ATA_DEVICE_OBS | ATA_DEV1;
1154
Tejun Heo0d5ff562007-02-01 15:06:36 +09001155 iowrite8(tmp, ap->ioaddr.device_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 ata_pause(ap); /* needed; also flushes, for mmio */
1157}
1158
1159/**
1160 * ata_dev_select - Select device 0/1 on ATA bus
1161 * @ap: ATA channel to manipulate
1162 * @device: ATA device (numbered from zero) to select
1163 * @wait: non-zero to wait for Status register BSY bit to clear
1164 * @can_sleep: non-zero if context allows sleeping
1165 *
1166 * Use the method defined in the ATA specification to
1167 * make either device 0, or device 1, active on the
1168 * ATA channel.
1169 *
1170 * This is a high-level version of ata_std_dev_select(),
1171 * which additionally provides the services of inserting
1172 * the proper pauses and status polling, where needed.
1173 *
1174 * LOCKING:
1175 * caller.
1176 */
1177
1178void ata_dev_select(struct ata_port *ap, unsigned int device,
1179 unsigned int wait, unsigned int can_sleep)
1180{
Tejun Heo88574552006-06-25 20:00:35 +09001181 if (ata_msg_probe(ap))
Tejun Heo44877b42007-02-21 01:06:51 +09001182 ata_port_printk(ap, KERN_INFO, "ata_dev_select: ENTER, "
1183 "device %u, wait %u\n", device, wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
1185 if (wait)
1186 ata_wait_idle(ap);
1187
1188 ap->ops->dev_select(ap, device);
1189
1190 if (wait) {
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001191 if (can_sleep && ap->link.device[device].class == ATA_DEV_ATAPI)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 msleep(150);
1193 ata_wait_idle(ap);
1194 }
1195}
1196
1197/**
1198 * ata_dump_id - IDENTIFY DEVICE info debugging output
Tejun Heo0bd33002006-02-12 22:47:05 +09001199 * @id: IDENTIFY DEVICE page to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 *
Tejun Heo0bd33002006-02-12 22:47:05 +09001201 * Dump selected 16-bit words from the given IDENTIFY DEVICE
1202 * page.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 *
1204 * LOCKING:
1205 * caller.
1206 */
1207
Tejun Heo0bd33002006-02-12 22:47:05 +09001208static inline void ata_dump_id(const u16 *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209{
1210 DPRINTK("49==0x%04x "
1211 "53==0x%04x "
1212 "63==0x%04x "
1213 "64==0x%04x "
1214 "75==0x%04x \n",
Tejun Heo0bd33002006-02-12 22:47:05 +09001215 id[49],
1216 id[53],
1217 id[63],
1218 id[64],
1219 id[75]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 DPRINTK("80==0x%04x "
1221 "81==0x%04x "
1222 "82==0x%04x "
1223 "83==0x%04x "
1224 "84==0x%04x \n",
Tejun Heo0bd33002006-02-12 22:47:05 +09001225 id[80],
1226 id[81],
1227 id[82],
1228 id[83],
1229 id[84]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 DPRINTK("88==0x%04x "
1231 "93==0x%04x\n",
Tejun Heo0bd33002006-02-12 22:47:05 +09001232 id[88],
1233 id[93]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234}
1235
Tejun Heocb95d562006-03-06 04:31:56 +09001236/**
1237 * ata_id_xfermask - Compute xfermask from the given IDENTIFY data
1238 * @id: IDENTIFY data to compute xfer mask from
1239 *
1240 * Compute the xfermask for this device. This is not as trivial
1241 * as it seems if we must consider early devices correctly.
1242 *
1243 * FIXME: pre IDE drive timing (do we care ?).
1244 *
1245 * LOCKING:
1246 * None.
1247 *
1248 * RETURNS:
1249 * Computed xfermask
1250 */
1251static unsigned int ata_id_xfermask(const u16 *id)
1252{
1253 unsigned int pio_mask, mwdma_mask, udma_mask;
1254
1255 /* Usual case. Word 53 indicates word 64 is valid */
1256 if (id[ATA_ID_FIELD_VALID] & (1 << 1)) {
1257 pio_mask = id[ATA_ID_PIO_MODES] & 0x03;
1258 pio_mask <<= 3;
1259 pio_mask |= 0x7;
1260 } else {
1261 /* If word 64 isn't valid then Word 51 high byte holds
1262 * the PIO timing number for the maximum. Turn it into
1263 * a mask.
1264 */
Lennert Buytenhek7a0f1c82007-01-29 13:28:47 +01001265 u8 mode = (id[ATA_ID_OLD_PIO_MODES] >> 8) & 0xFF;
Alan Cox46767aeb2006-09-29 18:26:47 +01001266 if (mode < 5) /* Valid PIO range */
1267 pio_mask = (2 << mode) - 1;
1268 else
1269 pio_mask = 1;
Tejun Heocb95d562006-03-06 04:31:56 +09001270
1271 /* But wait.. there's more. Design your standards by
1272 * committee and you too can get a free iordy field to
1273 * process. However its the speeds not the modes that
1274 * are supported... Note drivers using the timing API
1275 * will get this right anyway
1276 */
1277 }
1278
1279 mwdma_mask = id[ATA_ID_MWDMA_MODES] & 0x07;
Tejun Heofb21f0d2006-03-12 12:34:35 +09001280
Alan Coxb352e572006-08-10 18:52:12 +01001281 if (ata_id_is_cfa(id)) {
1282 /*
1283 * Process compact flash extended modes
1284 */
1285 int pio = id[163] & 0x7;
1286 int dma = (id[163] >> 3) & 7;
1287
1288 if (pio)
1289 pio_mask |= (1 << 5);
1290 if (pio > 1)
1291 pio_mask |= (1 << 6);
1292 if (dma)
1293 mwdma_mask |= (1 << 3);
1294 if (dma > 1)
1295 mwdma_mask |= (1 << 4);
1296 }
1297
Tejun Heofb21f0d2006-03-12 12:34:35 +09001298 udma_mask = 0;
1299 if (id[ATA_ID_FIELD_VALID] & (1 << 2))
1300 udma_mask = id[ATA_ID_UDMA_MODES] & 0xff;
Tejun Heocb95d562006-03-06 04:31:56 +09001301
1302 return ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
1303}
1304
Tejun Heo86e45b62006-03-05 15:29:09 +09001305/**
1306 * ata_port_queue_task - Queue port_task
1307 * @ap: The ata_port to queue port_task for
Randy Dunlape2a7f772006-05-18 10:50:18 -07001308 * @fn: workqueue function to be scheduled
David Howells65f27f32006-11-22 14:55:48 +00001309 * @data: data for @fn to use
Randy Dunlape2a7f772006-05-18 10:50:18 -07001310 * @delay: delay time for workqueue function
Tejun Heo86e45b62006-03-05 15:29:09 +09001311 *
1312 * Schedule @fn(@data) for execution after @delay jiffies using
1313 * port_task. There is one port_task per port and it's the
1314 * user(low level driver)'s responsibility to make sure that only
1315 * one task is active at any given time.
1316 *
1317 * libata core layer takes care of synchronization between
1318 * port_task and EH. ata_port_queue_task() may be ignored for EH
1319 * synchronization.
1320 *
1321 * LOCKING:
1322 * Inherited from caller.
1323 */
David Howells65f27f32006-11-22 14:55:48 +00001324void ata_port_queue_task(struct ata_port *ap, work_func_t fn, void *data,
Tejun Heo86e45b62006-03-05 15:29:09 +09001325 unsigned long delay)
1326{
David Howells65f27f32006-11-22 14:55:48 +00001327 PREPARE_DELAYED_WORK(&ap->port_task, fn);
1328 ap->port_task_data = data;
Tejun Heo86e45b62006-03-05 15:29:09 +09001329
Oleg Nesterov45a66c12007-07-09 11:46:13 -07001330 /* may fail if ata_port_flush_task() in progress */
1331 queue_delayed_work(ata_wq, &ap->port_task, delay);
Tejun Heo86e45b62006-03-05 15:29:09 +09001332}
1333
1334/**
1335 * ata_port_flush_task - Flush port_task
1336 * @ap: The ata_port to flush port_task for
1337 *
1338 * After this function completes, port_task is guranteed not to
1339 * be running or scheduled.
1340 *
1341 * LOCKING:
1342 * Kernel thread context (may sleep)
1343 */
1344void ata_port_flush_task(struct ata_port *ap)
1345{
Tejun Heo86e45b62006-03-05 15:29:09 +09001346 DPRINTK("ENTER\n");
1347
Oleg Nesterov45a66c12007-07-09 11:46:13 -07001348 cancel_rearming_delayed_work(&ap->port_task);
Tejun Heo86e45b62006-03-05 15:29:09 +09001349
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001350 if (ata_msg_ctl(ap))
1351 ata_port_printk(ap, KERN_DEBUG, "%s: EXIT\n", __FUNCTION__);
Tejun Heo86e45b62006-03-05 15:29:09 +09001352}
1353
Adrian Bunk7102d232007-01-04 00:09:36 +01001354static void ata_qc_complete_internal(struct ata_queued_cmd *qc)
Tejun Heoa2a7a662005-12-13 14:48:31 +09001355{
Tejun Heo77853bf2006-01-23 13:09:36 +09001356 struct completion *waiting = qc->private_data;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001357
Tejun Heoa2a7a662005-12-13 14:48:31 +09001358 complete(waiting);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001359}
1360
1361/**
Tejun Heo24326972006-11-14 22:47:09 +09001362 * ata_exec_internal_sg - execute libata internal command
Tejun Heoa2a7a662005-12-13 14:48:31 +09001363 * @dev: Device to which the command is sent
1364 * @tf: Taskfile registers for the command and the result
Tejun Heod69cf372006-04-02 18:51:53 +09001365 * @cdb: CDB for packet command
Tejun Heoa2a7a662005-12-13 14:48:31 +09001366 * @dma_dir: Data tranfer direction of the command
Tejun Heo24326972006-11-14 22:47:09 +09001367 * @sg: sg list for the data buffer of the command
1368 * @n_elem: Number of sg entries
Tejun Heoa2a7a662005-12-13 14:48:31 +09001369 *
1370 * Executes libata internal command with timeout. @tf contains
1371 * command on entry and result on return. Timeout and error
1372 * conditions are reported via return value. No recovery action
1373 * is taken after a command times out. It's caller's duty to
1374 * clean up after timeout.
1375 *
1376 * LOCKING:
1377 * None. Should be called with kernel context, might sleep.
Tejun Heo551e8882006-06-12 14:09:49 +09001378 *
1379 * RETURNS:
1380 * Zero on success, AC_ERR_* mask on failure
Tejun Heoa2a7a662005-12-13 14:48:31 +09001381 */
Tejun Heo24326972006-11-14 22:47:09 +09001382unsigned ata_exec_internal_sg(struct ata_device *dev,
1383 struct ata_taskfile *tf, const u8 *cdb,
1384 int dma_dir, struct scatterlist *sg,
1385 unsigned int n_elem)
Tejun Heoa2a7a662005-12-13 14:48:31 +09001386{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001387 struct ata_link *link = dev->link;
1388 struct ata_port *ap = link->ap;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001389 u8 command = tf->command;
1390 struct ata_queued_cmd *qc;
Tejun Heo2ab7db12006-05-15 20:58:02 +09001391 unsigned int tag, preempted_tag;
Tejun Heodedaf2b2006-05-15 21:03:43 +09001392 u32 preempted_sactive, preempted_qc_active;
Ingo Molnar60be6b92006-07-03 00:25:26 -07001393 DECLARE_COMPLETION_ONSTACK(wait);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001394 unsigned long flags;
Tejun Heo77853bf2006-01-23 13:09:36 +09001395 unsigned int err_mask;
Tejun Heod95a7172006-05-15 20:58:14 +09001396 int rc;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001397
Jeff Garzikba6a1302006-06-22 23:46:10 -04001398 spin_lock_irqsave(ap->lock, flags);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001399
Tejun Heoe3180492006-05-15 20:58:09 +09001400 /* no internal command while frozen */
Tejun Heob51e9e52006-06-29 01:29:30 +09001401 if (ap->pflags & ATA_PFLAG_FROZEN) {
Jeff Garzikba6a1302006-06-22 23:46:10 -04001402 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +09001403 return AC_ERR_SYSTEM;
1404 }
1405
Tejun Heo2ab7db12006-05-15 20:58:02 +09001406 /* initialize internal qc */
Tejun Heoa2a7a662005-12-13 14:48:31 +09001407
Tejun Heo2ab7db12006-05-15 20:58:02 +09001408 /* XXX: Tag 0 is used for drivers with legacy EH as some
1409 * drivers choke if any other tag is given. This breaks
1410 * ata_tag_internal() test for those drivers. Don't use new
1411 * EH stuff without converting to it.
1412 */
1413 if (ap->ops->error_handler)
1414 tag = ATA_TAG_INTERNAL;
1415 else
1416 tag = 0;
1417
Tejun Heo6cec4a32006-05-15 21:03:41 +09001418 if (test_and_set_bit(tag, &ap->qc_allocated))
Tejun Heo2ab7db12006-05-15 20:58:02 +09001419 BUG();
Tejun Heof69499f2006-05-15 20:58:03 +09001420 qc = __ata_qc_from_tag(ap, tag);
Tejun Heo2ab7db12006-05-15 20:58:02 +09001421
1422 qc->tag = tag;
1423 qc->scsicmd = NULL;
1424 qc->ap = ap;
1425 qc->dev = dev;
1426 ata_qc_reinit(qc);
1427
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001428 preempted_tag = link->active_tag;
1429 preempted_sactive = link->sactive;
Tejun Heodedaf2b2006-05-15 21:03:43 +09001430 preempted_qc_active = ap->qc_active;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001431 link->active_tag = ATA_TAG_POISON;
1432 link->sactive = 0;
Tejun Heodedaf2b2006-05-15 21:03:43 +09001433 ap->qc_active = 0;
Tejun Heo2ab7db12006-05-15 20:58:02 +09001434
1435 /* prepare & issue qc */
Tejun Heoa2a7a662005-12-13 14:48:31 +09001436 qc->tf = *tf;
Tejun Heod69cf372006-04-02 18:51:53 +09001437 if (cdb)
1438 memcpy(qc->cdb, cdb, ATAPI_CDB_LEN);
Tejun Heoe61e0672006-05-15 20:57:40 +09001439 qc->flags |= ATA_QCFLAG_RESULT_TF;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001440 qc->dma_dir = dma_dir;
1441 if (dma_dir != DMA_NONE) {
Tejun Heo24326972006-11-14 22:47:09 +09001442 unsigned int i, buflen = 0;
1443
1444 for (i = 0; i < n_elem; i++)
1445 buflen += sg[i].length;
1446
1447 ata_sg_init(qc, sg, n_elem);
Brian King49c80422007-01-30 11:32:26 -06001448 qc->nbytes = buflen;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001449 }
1450
Tejun Heo77853bf2006-01-23 13:09:36 +09001451 qc->private_data = &wait;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001452 qc->complete_fn = ata_qc_complete_internal;
1453
Tejun Heo8e0e6942006-03-31 20:41:11 +09001454 ata_qc_issue(qc);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001455
Jeff Garzikba6a1302006-06-22 23:46:10 -04001456 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001457
Andrew Mortona8601e52006-06-25 01:36:52 -07001458 rc = wait_for_completion_timeout(&wait, ata_probe_timeout);
Albert Lee41ade502006-03-14 11:19:04 +08001459
Tejun Heod95a7172006-05-15 20:58:14 +09001460 ata_port_flush_task(ap);
1461
1462 if (!rc) {
Jeff Garzikba6a1302006-06-22 23:46:10 -04001463 spin_lock_irqsave(ap->lock, flags);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001464
1465 /* We're racing with irq here. If we lose, the
1466 * following test prevents us from completing the qc
Tejun Heod95a7172006-05-15 20:58:14 +09001467 * twice. If we win, the port is frozen and will be
1468 * cleaned up by ->post_internal_cmd().
Tejun Heoa2a7a662005-12-13 14:48:31 +09001469 */
Tejun Heo77853bf2006-01-23 13:09:36 +09001470 if (qc->flags & ATA_QCFLAG_ACTIVE) {
Tejun Heod95a7172006-05-15 20:58:14 +09001471 qc->err_mask |= AC_ERR_TIMEOUT;
1472
1473 if (ap->ops->error_handler)
1474 ata_port_freeze(ap);
1475 else
1476 ata_qc_complete(qc);
Tejun Heof15a1da2006-05-15 20:57:56 +09001477
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001478 if (ata_msg_warn(ap))
1479 ata_dev_printk(dev, KERN_WARNING,
Tejun Heo88574552006-06-25 20:00:35 +09001480 "qc timeout (cmd 0x%x)\n", command);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001481 }
1482
Jeff Garzikba6a1302006-06-22 23:46:10 -04001483 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001484 }
1485
Tejun Heod95a7172006-05-15 20:58:14 +09001486 /* do post_internal_cmd */
1487 if (ap->ops->post_internal_cmd)
1488 ap->ops->post_internal_cmd(qc);
1489
Tejun Heoa51d6442007-03-20 15:24:11 +09001490 /* perform minimal error analysis */
1491 if (qc->flags & ATA_QCFLAG_FAILED) {
1492 if (qc->result_tf.command & (ATA_ERR | ATA_DF))
1493 qc->err_mask |= AC_ERR_DEV;
1494
1495 if (!qc->err_mask)
1496 qc->err_mask |= AC_ERR_OTHER;
1497
1498 if (qc->err_mask & ~AC_ERR_OTHER)
1499 qc->err_mask &= ~AC_ERR_OTHER;
Tejun Heod95a7172006-05-15 20:58:14 +09001500 }
1501
Tejun Heo15869302006-05-15 20:57:33 +09001502 /* finish up */
Jeff Garzikba6a1302006-06-22 23:46:10 -04001503 spin_lock_irqsave(ap->lock, flags);
Tejun Heo15869302006-05-15 20:57:33 +09001504
Tejun Heoe61e0672006-05-15 20:57:40 +09001505 *tf = qc->result_tf;
Tejun Heo77853bf2006-01-23 13:09:36 +09001506 err_mask = qc->err_mask;
1507
1508 ata_qc_free(qc);
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001509 link->active_tag = preempted_tag;
1510 link->sactive = preempted_sactive;
Tejun Heodedaf2b2006-05-15 21:03:43 +09001511 ap->qc_active = preempted_qc_active;
Tejun Heo77853bf2006-01-23 13:09:36 +09001512
Tejun Heo1f7dd3e92006-03-24 15:25:30 +09001513 /* XXX - Some LLDDs (sata_mv) disable port on command failure.
1514 * Until those drivers are fixed, we detect the condition
1515 * here, fail the command with AC_ERR_SYSTEM and reenable the
1516 * port.
1517 *
1518 * Note that this doesn't change any behavior as internal
1519 * command failure results in disabling the device in the
1520 * higher layer for LLDDs without new reset/EH callbacks.
1521 *
1522 * Kill the following code as soon as those drivers are fixed.
1523 */
Tejun Heo198e0fe2006-04-02 18:51:52 +09001524 if (ap->flags & ATA_FLAG_DISABLED) {
Tejun Heo1f7dd3e92006-03-24 15:25:30 +09001525 err_mask |= AC_ERR_SYSTEM;
1526 ata_port_probe(ap);
1527 }
1528
Jeff Garzikba6a1302006-06-22 23:46:10 -04001529 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo15869302006-05-15 20:57:33 +09001530
Tejun Heo77853bf2006-01-23 13:09:36 +09001531 return err_mask;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001532}
1533
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534/**
Tejun Heo33480a02006-12-12 02:15:31 +09001535 * ata_exec_internal - execute libata internal command
Tejun Heo24326972006-11-14 22:47:09 +09001536 * @dev: Device to which the command is sent
1537 * @tf: Taskfile registers for the command and the result
1538 * @cdb: CDB for packet command
1539 * @dma_dir: Data tranfer direction of the command
1540 * @buf: Data buffer of the command
1541 * @buflen: Length of data buffer
1542 *
1543 * Wrapper around ata_exec_internal_sg() which takes simple
1544 * buffer instead of sg list.
1545 *
1546 * LOCKING:
1547 * None. Should be called with kernel context, might sleep.
1548 *
1549 * RETURNS:
1550 * Zero on success, AC_ERR_* mask on failure
1551 */
1552unsigned ata_exec_internal(struct ata_device *dev,
1553 struct ata_taskfile *tf, const u8 *cdb,
1554 int dma_dir, void *buf, unsigned int buflen)
1555{
Tejun Heo33480a02006-12-12 02:15:31 +09001556 struct scatterlist *psg = NULL, sg;
1557 unsigned int n_elem = 0;
Tejun Heo24326972006-11-14 22:47:09 +09001558
Tejun Heo33480a02006-12-12 02:15:31 +09001559 if (dma_dir != DMA_NONE) {
1560 WARN_ON(!buf);
1561 sg_init_one(&sg, buf, buflen);
1562 psg = &sg;
1563 n_elem++;
1564 }
Tejun Heo24326972006-11-14 22:47:09 +09001565
Tejun Heo33480a02006-12-12 02:15:31 +09001566 return ata_exec_internal_sg(dev, tf, cdb, dma_dir, psg, n_elem);
Tejun Heo24326972006-11-14 22:47:09 +09001567}
1568
1569/**
Tejun Heo977e6b92006-06-24 20:30:19 +09001570 * ata_do_simple_cmd - execute simple internal command
1571 * @dev: Device to which the command is sent
1572 * @cmd: Opcode to execute
1573 *
1574 * Execute a 'simple' command, that only consists of the opcode
1575 * 'cmd' itself, without filling any other registers
1576 *
1577 * LOCKING:
1578 * Kernel thread context (may sleep).
1579 *
1580 * RETURNS:
1581 * Zero on success, AC_ERR_* mask on failure
Tejun Heoe58eb582006-06-24 20:30:19 +09001582 */
Tejun Heo77b08fb2006-06-24 20:30:19 +09001583unsigned int ata_do_simple_cmd(struct ata_device *dev, u8 cmd)
Tejun Heoe58eb582006-06-24 20:30:19 +09001584{
1585 struct ata_taskfile tf;
Tejun Heoe58eb582006-06-24 20:30:19 +09001586
1587 ata_tf_init(dev, &tf);
1588
1589 tf.command = cmd;
1590 tf.flags |= ATA_TFLAG_DEVICE;
1591 tf.protocol = ATA_PROT_NODATA;
1592
Tejun Heo977e6b92006-06-24 20:30:19 +09001593 return ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
Tejun Heoe58eb582006-06-24 20:30:19 +09001594}
1595
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596/**
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001597 * ata_pio_need_iordy - check if iordy needed
1598 * @adev: ATA device
1599 *
1600 * Check if the current speed of the device requires IORDY. Used
1601 * by various controllers for chip configuration.
1602 */
Jeff Garzika617c092007-05-21 20:14:23 -04001603
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001604unsigned int ata_pio_need_iordy(const struct ata_device *adev)
1605{
Alan Cox432729f2007-03-08 23:22:59 +00001606 /* Controller doesn't support IORDY. Probably a pointless check
1607 as the caller should know this */
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001608 if (adev->link->ap->flags & ATA_FLAG_NO_IORDY)
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001609 return 0;
Alan Cox432729f2007-03-08 23:22:59 +00001610 /* PIO3 and higher it is mandatory */
1611 if (adev->pio_mode > XFER_PIO_2)
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001612 return 1;
Alan Cox432729f2007-03-08 23:22:59 +00001613 /* We turn it on when possible */
1614 if (ata_id_has_iordy(adev->id))
1615 return 1;
1616 return 0;
1617}
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001618
Alan Cox432729f2007-03-08 23:22:59 +00001619/**
1620 * ata_pio_mask_no_iordy - Return the non IORDY mask
1621 * @adev: ATA device
1622 *
1623 * Compute the highest mode possible if we are not using iordy. Return
1624 * -1 if no iordy mode is available.
1625 */
Jeff Garzika617c092007-05-21 20:14:23 -04001626
Alan Cox432729f2007-03-08 23:22:59 +00001627static u32 ata_pio_mask_no_iordy(const struct ata_device *adev)
1628{
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001629 /* If we have no drive specific rule, then PIO 2 is non IORDY */
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001630 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
Alan Cox432729f2007-03-08 23:22:59 +00001631 u16 pio = adev->id[ATA_ID_EIDE_PIO];
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001632 /* Is the speed faster than the drive allows non IORDY ? */
1633 if (pio) {
1634 /* This is cycle times not frequency - watch the logic! */
1635 if (pio > 240) /* PIO2 is 240nS per cycle */
Alan Cox432729f2007-03-08 23:22:59 +00001636 return 3 << ATA_SHIFT_PIO;
1637 return 7 << ATA_SHIFT_PIO;
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001638 }
1639 }
Alan Cox432729f2007-03-08 23:22:59 +00001640 return 3 << ATA_SHIFT_PIO;
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001641}
1642
1643/**
Tejun Heo49016ac2006-02-21 02:12:11 +09001644 * ata_dev_read_id - Read ID data from the specified device
Tejun Heo49016ac2006-02-21 02:12:11 +09001645 * @dev: target device
1646 * @p_class: pointer to class of the target device (may be changed)
Tejun Heobff04642006-11-10 18:08:10 +09001647 * @flags: ATA_READID_* flags
Tejun Heofe635c72006-05-15 20:57:35 +09001648 * @id: buffer to read IDENTIFY data into
Tejun Heo49016ac2006-02-21 02:12:11 +09001649 *
1650 * Read ID data from the specified device. ATA_CMD_ID_ATA is
1651 * performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
Tejun Heoaec5c3c2006-03-25 01:33:34 +09001652 * devices. This function also issues ATA_CMD_INIT_DEV_PARAMS
1653 * for pre-ATA4 drives.
Tejun Heo49016ac2006-02-21 02:12:11 +09001654 *
Alan Cox50a99012007-08-08 14:27:00 +01001655 * FIXME: ATA_CMD_ID_ATA is optional for early drives and right
1656 * now we abort if we hit that case.
1657 *
Tejun Heo49016ac2006-02-21 02:12:11 +09001658 * LOCKING:
1659 * Kernel thread context (may sleep)
1660 *
1661 * RETURNS:
1662 * 0 on success, -errno otherwise.
1663 */
Tejun Heoa9beec92006-05-31 18:27:44 +09001664int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
Tejun Heobff04642006-11-10 18:08:10 +09001665 unsigned int flags, u16 *id)
Tejun Heo49016ac2006-02-21 02:12:11 +09001666{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001667 struct ata_port *ap = dev->link->ap;
Tejun Heo49016ac2006-02-21 02:12:11 +09001668 unsigned int class = *p_class;
Tejun Heo49016ac2006-02-21 02:12:11 +09001669 struct ata_taskfile tf;
1670 unsigned int err_mask = 0;
1671 const char *reason;
Tejun Heo54936f82007-05-11 14:35:29 +02001672 int may_fallback = 1, tried_spinup = 0;
Tejun Heo49016ac2006-02-21 02:12:11 +09001673 int rc;
1674
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001675 if (ata_msg_ctl(ap))
Tejun Heo44877b42007-02-21 01:06:51 +09001676 ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER\n", __FUNCTION__);
Tejun Heo49016ac2006-02-21 02:12:11 +09001677
Tejun Heo49016ac2006-02-21 02:12:11 +09001678 ata_dev_select(ap, dev->devno, 1, 1); /* select device 0/1 */
Tejun Heo49016ac2006-02-21 02:12:11 +09001679 retry:
Tejun Heo3373efd2006-05-15 20:57:53 +09001680 ata_tf_init(dev, &tf);
Tejun Heo49016ac2006-02-21 02:12:11 +09001681
1682 switch (class) {
1683 case ATA_DEV_ATA:
1684 tf.command = ATA_CMD_ID_ATA;
1685 break;
1686 case ATA_DEV_ATAPI:
1687 tf.command = ATA_CMD_ID_ATAPI;
1688 break;
1689 default:
1690 rc = -ENODEV;
1691 reason = "unsupported class";
1692 goto err_out;
1693 }
1694
1695 tf.protocol = ATA_PROT_PIO;
Tejun Heo81afe892007-02-07 12:37:41 -08001696
1697 /* Some devices choke if TF registers contain garbage. Make
1698 * sure those are properly initialized.
1699 */
1700 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1701
1702 /* Device presence detection is unreliable on some
1703 * controllers. Always poll IDENTIFY if available.
1704 */
1705 tf.flags |= ATA_TFLAG_POLLING;
Tejun Heo49016ac2006-02-21 02:12:11 +09001706
Tejun Heo3373efd2006-05-15 20:57:53 +09001707 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
Tejun Heo49016ac2006-02-21 02:12:11 +09001708 id, sizeof(id[0]) * ATA_ID_WORDS);
Tejun Heo49016ac2006-02-21 02:12:11 +09001709 if (err_mask) {
Tejun Heo800b3992006-12-03 21:34:13 +09001710 if (err_mask & AC_ERR_NODEV_HINT) {
Tejun Heo55a8e2c2006-11-10 18:08:10 +09001711 DPRINTK("ata%u.%d: NODEV after polling detection\n",
Tejun Heo44877b42007-02-21 01:06:51 +09001712 ap->print_id, dev->devno);
Tejun Heo55a8e2c2006-11-10 18:08:10 +09001713 return -ENOENT;
1714 }
1715
Tejun Heo54936f82007-05-11 14:35:29 +02001716 /* Device or controller might have reported the wrong
1717 * device class. Give a shot at the other IDENTIFY if
1718 * the current one is aborted by the device.
1719 */
1720 if (may_fallback &&
1721 (err_mask == AC_ERR_DEV) && (tf.feature & ATA_ABORTED)) {
1722 may_fallback = 0;
1723
1724 if (class == ATA_DEV_ATA)
1725 class = ATA_DEV_ATAPI;
1726 else
1727 class = ATA_DEV_ATA;
1728 goto retry;
1729 }
1730
Tejun Heo49016ac2006-02-21 02:12:11 +09001731 rc = -EIO;
1732 reason = "I/O error";
Tejun Heo49016ac2006-02-21 02:12:11 +09001733 goto err_out;
1734 }
1735
Tejun Heo54936f82007-05-11 14:35:29 +02001736 /* Falling back doesn't make sense if ID data was read
1737 * successfully at least once.
1738 */
1739 may_fallback = 0;
1740
Tejun Heo49016ac2006-02-21 02:12:11 +09001741 swap_buf_le16(id, ATA_ID_WORDS);
1742
Tejun Heo49016ac2006-02-21 02:12:11 +09001743 /* sanity check */
Tejun Heoa4f57492006-09-12 20:35:49 -07001744 rc = -EINVAL;
Alan Cox60700682007-06-07 16:13:55 +01001745 reason = "device reports invalid type";
Tejun Heoa4f57492006-09-12 20:35:49 -07001746
1747 if (class == ATA_DEV_ATA) {
1748 if (!ata_id_is_ata(id) && !ata_id_is_cfa(id))
1749 goto err_out;
1750 } else {
1751 if (ata_id_is_ata(id))
1752 goto err_out;
Tejun Heo49016ac2006-02-21 02:12:11 +09001753 }
1754
Mark Lord169439c2007-04-17 18:26:07 -04001755 if (!tried_spinup && (id[2] == 0x37c8 || id[2] == 0x738c)) {
1756 tried_spinup = 1;
1757 /*
1758 * Drive powered-up in standby mode, and requires a specific
1759 * SET_FEATURES spin-up subcommand before it will accept
1760 * anything other than the original IDENTIFY command.
1761 */
1762 ata_tf_init(dev, &tf);
1763 tf.command = ATA_CMD_SET_FEATURES;
1764 tf.feature = SETFEATURES_SPINUP;
1765 tf.protocol = ATA_PROT_NODATA;
1766 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1767 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
Ryan Powerfb0582f92007-08-10 13:59:35 -07001768 if (err_mask && id[2] != 0x738c) {
Mark Lord169439c2007-04-17 18:26:07 -04001769 rc = -EIO;
1770 reason = "SPINUP failed";
1771 goto err_out;
1772 }
1773 /*
1774 * If the drive initially returned incomplete IDENTIFY info,
1775 * we now must reissue the IDENTIFY command.
1776 */
1777 if (id[2] == 0x37c8)
1778 goto retry;
1779 }
1780
Tejun Heobff04642006-11-10 18:08:10 +09001781 if ((flags & ATA_READID_POSTRESET) && class == ATA_DEV_ATA) {
Tejun Heo49016ac2006-02-21 02:12:11 +09001782 /*
1783 * The exact sequence expected by certain pre-ATA4 drives is:
1784 * SRST RESET
Alan Cox50a99012007-08-08 14:27:00 +01001785 * IDENTIFY (optional in early ATA)
1786 * INITIALIZE DEVICE PARAMETERS (later IDE and ATA)
Tejun Heo49016ac2006-02-21 02:12:11 +09001787 * anything else..
1788 * Some drives were very specific about that exact sequence.
Alan Cox50a99012007-08-08 14:27:00 +01001789 *
1790 * Note that ATA4 says lba is mandatory so the second check
1791 * shoud never trigger.
Tejun Heo49016ac2006-02-21 02:12:11 +09001792 */
1793 if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
Tejun Heo3373efd2006-05-15 20:57:53 +09001794 err_mask = ata_dev_init_params(dev, id[3], id[6]);
Tejun Heo49016ac2006-02-21 02:12:11 +09001795 if (err_mask) {
1796 rc = -EIO;
1797 reason = "INIT_DEV_PARAMS failed";
1798 goto err_out;
1799 }
1800
1801 /* current CHS translation info (id[53-58]) might be
1802 * changed. reread the identify device info.
1803 */
Tejun Heobff04642006-11-10 18:08:10 +09001804 flags &= ~ATA_READID_POSTRESET;
Tejun Heo49016ac2006-02-21 02:12:11 +09001805 goto retry;
1806 }
1807 }
1808
1809 *p_class = class;
Tejun Heofe635c72006-05-15 20:57:35 +09001810
Tejun Heo49016ac2006-02-21 02:12:11 +09001811 return 0;
1812
1813 err_out:
Tejun Heo88574552006-06-25 20:00:35 +09001814 if (ata_msg_warn(ap))
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001815 ata_dev_printk(dev, KERN_WARNING, "failed to IDENTIFY "
Tejun Heo88574552006-06-25 20:00:35 +09001816 "(%s, err_mask=0x%x)\n", reason, err_mask);
Tejun Heo49016ac2006-02-21 02:12:11 +09001817 return rc;
1818}
1819
Tejun Heo3373efd2006-05-15 20:57:53 +09001820static inline u8 ata_dev_knobble(struct ata_device *dev)
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09001821{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001822 struct ata_port *ap = dev->link->ap;
1823 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09001824}
1825
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001826static void ata_dev_config_ncq(struct ata_device *dev,
1827 char *desc, size_t desc_sz)
1828{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001829 struct ata_port *ap = dev->link->ap;
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001830 int hdepth = 0, ddepth = ata_id_queue_depth(dev->id);
1831
1832 if (!ata_id_has_ncq(dev->id)) {
1833 desc[0] = '\0';
1834 return;
1835 }
Tejun Heo75683fe2007-07-05 13:31:27 +09001836 if (dev->horkage & ATA_HORKAGE_NONCQ) {
Alan Cox6919a0a2006-10-27 19:08:46 -07001837 snprintf(desc, desc_sz, "NCQ (not used)");
1838 return;
1839 }
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001840 if (ap->flags & ATA_FLAG_NCQ) {
Jeff Garzikcca39742006-08-24 03:19:22 -04001841 hdepth = min(ap->scsi_host->can_queue, ATA_MAX_QUEUE - 1);
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001842 dev->flags |= ATA_DFLAG_NCQ;
1843 }
1844
1845 if (hdepth >= ddepth)
1846 snprintf(desc, desc_sz, "NCQ (depth %d)", ddepth);
1847 else
1848 snprintf(desc, desc_sz, "NCQ (depth %d/%d)", hdepth, ddepth);
1849}
1850
Tejun Heo49016ac2006-02-21 02:12:11 +09001851/**
Tejun Heoffeae412006-03-01 16:09:35 +09001852 * ata_dev_configure - Configure the specified ATA/ATAPI device
Tejun Heoffeae412006-03-01 16:09:35 +09001853 * @dev: Target device to configure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 *
Tejun Heoffeae412006-03-01 16:09:35 +09001855 * Configure @dev according to @dev->id. Generic and low-level
1856 * driver specific fixups are also applied.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 *
1858 * LOCKING:
Tejun Heoffeae412006-03-01 16:09:35 +09001859 * Kernel thread context (may sleep)
1860 *
1861 * RETURNS:
1862 * 0 on success, -errno otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 */
Tejun Heoefdaedc2006-11-01 18:38:52 +09001864int ata_dev_configure(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001866 struct ata_port *ap = dev->link->ap;
1867 struct ata_eh_context *ehc = &dev->link->eh_context;
Tejun Heo67465442007-05-15 03:28:16 +09001868 int print_info = ehc->i.flags & ATA_EHI_PRINTINFO;
Tejun Heo1148c3a2006-03-13 19:48:04 +09001869 const u16 *id = dev->id;
Tejun Heoff8854b2006-03-06 04:31:56 +09001870 unsigned int xfer_mask;
Alan Coxb352e572006-08-10 18:52:12 +01001871 char revbuf[7]; /* XYZ-99\0 */
Eric D. Mudama3f64f562007-01-30 23:00:40 -07001872 char fwrevbuf[ATA_ID_FW_REV_LEN+1];
1873 char modelbuf[ATA_ID_PROD_LEN+1];
Brian Kinge6d902a2006-06-28 08:30:31 -05001874 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001876 if (!ata_dev_enabled(dev) && ata_msg_info(ap)) {
Tejun Heo44877b42007-02-21 01:06:51 +09001877 ata_dev_printk(dev, KERN_INFO, "%s: ENTER/EXIT -- nodev\n",
1878 __FUNCTION__);
Tejun Heoffeae412006-03-01 16:09:35 +09001879 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 }
1881
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001882 if (ata_msg_probe(ap))
Tejun Heo44877b42007-02-21 01:06:51 +09001883 ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
Tejun Heo75683fe2007-07-05 13:31:27 +09001885 /* set horkage */
1886 dev->horkage |= ata_dev_blacklisted(dev);
1887
Tejun Heo67465442007-05-15 03:28:16 +09001888 /* let ACPI work its magic */
1889 rc = ata_acpi_on_devcfg(dev);
1890 if (rc)
1891 return rc;
Kristen Carlson Accardi08573a82006-11-10 16:14:47 -08001892
Tejun Heo05027ad2007-09-03 12:32:57 +09001893 /* massage HPA, do it early as it might change IDENTIFY data */
1894 rc = ata_hpa_resize(dev);
1895 if (rc)
1896 return rc;
1897
Tejun Heoc39f5eb2006-03-13 19:51:19 +09001898 /* print device capabilities */
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001899 if (ata_msg_probe(ap))
Tejun Heo88574552006-06-25 20:00:35 +09001900 ata_dev_printk(dev, KERN_DEBUG,
1901 "%s: cfg 49:%04x 82:%04x 83:%04x 84:%04x "
1902 "85:%04x 86:%04x 87:%04x 88:%04x\n",
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001903 __FUNCTION__,
Tejun Heof15a1da2006-05-15 20:57:56 +09001904 id[49], id[82], id[83], id[84],
1905 id[85], id[86], id[87], id[88]);
Tejun Heoc39f5eb2006-03-13 19:51:19 +09001906
Tejun Heo208a9932006-03-05 17:55:58 +09001907 /* initialize to-be-configured parameters */
Tejun Heoea1dd4e2006-04-02 18:51:53 +09001908 dev->flags &= ~ATA_DFLAG_CFG_MASK;
Tejun Heo208a9932006-03-05 17:55:58 +09001909 dev->max_sectors = 0;
1910 dev->cdb_len = 0;
1911 dev->n_sectors = 0;
1912 dev->cylinders = 0;
1913 dev->heads = 0;
1914 dev->sectors = 0;
1915
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 /*
1917 * common ATA, ATAPI feature tests
1918 */
1919
Tejun Heoff8854b2006-03-06 04:31:56 +09001920 /* find max transfer mode; for printk only */
Tejun Heo1148c3a2006-03-13 19:48:04 +09001921 xfer_mask = ata_id_xfermask(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001923 if (ata_msg_probe(ap))
1924 ata_dump_id(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
Albert Leeef143d52007-06-05 13:01:33 +08001926 /* SCSI only uses 4-char revisions, dump full 8 chars from ATA */
1927 ata_id_c_string(dev->id, fwrevbuf, ATA_ID_FW_REV,
1928 sizeof(fwrevbuf));
1929
1930 ata_id_c_string(dev->id, modelbuf, ATA_ID_PROD,
1931 sizeof(modelbuf));
1932
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 /* ATA-specific feature tests */
1934 if (dev->class == ATA_DEV_ATA) {
Alan Coxb352e572006-08-10 18:52:12 +01001935 if (ata_id_is_cfa(id)) {
1936 if (id[162] & 1) /* CPRM may make this media unusable */
Tejun Heo44877b42007-02-21 01:06:51 +09001937 ata_dev_printk(dev, KERN_WARNING,
1938 "supports DRM functions and may "
1939 "not be fully accessable.\n");
Alan Coxb352e572006-08-10 18:52:12 +01001940 snprintf(revbuf, 7, "CFA");
1941 }
1942 else
1943 snprintf(revbuf, 7, "ATA-%d", ata_id_major_version(id));
1944
Tejun Heo1148c3a2006-03-13 19:48:04 +09001945 dev->n_sectors = ata_id_n_sectors(id);
Tejun Heo29407402006-02-12 22:47:04 +09001946
Eric D. Mudama3f64f562007-01-30 23:00:40 -07001947 if (dev->id[59] & 0x100)
1948 dev->multi_count = dev->id[59] & 0xff;
1949
Tejun Heo1148c3a2006-03-13 19:48:04 +09001950 if (ata_id_has_lba(id)) {
Tejun Heo4c2d7212006-03-05 17:55:58 +09001951 const char *lba_desc;
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001952 char ncq_desc[20];
Albert Lee8bf62ece2005-05-12 15:29:42 -04001953
Tejun Heo4c2d7212006-03-05 17:55:58 +09001954 lba_desc = "LBA";
1955 dev->flags |= ATA_DFLAG_LBA;
Tejun Heo1148c3a2006-03-13 19:48:04 +09001956 if (ata_id_has_lba48(id)) {
Albert Lee8bf62ece2005-05-12 15:29:42 -04001957 dev->flags |= ATA_DFLAG_LBA48;
Tejun Heo4c2d7212006-03-05 17:55:58 +09001958 lba_desc = "LBA48";
Tejun Heo6fc49ad2006-11-11 20:10:45 +09001959
1960 if (dev->n_sectors >= (1UL << 28) &&
1961 ata_id_has_flush_ext(id))
1962 dev->flags |= ATA_DFLAG_FLUSH_EXT;
Tejun Heo4c2d7212006-03-05 17:55:58 +09001963 }
Albert Lee8bf62ece2005-05-12 15:29:42 -04001964
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001965 /* config NCQ */
1966 ata_dev_config_ncq(dev, ncq_desc, sizeof(ncq_desc));
1967
Albert Lee8bf62ece2005-05-12 15:29:42 -04001968 /* print device info to dmesg */
Eric D. Mudama3f64f562007-01-30 23:00:40 -07001969 if (ata_msg_drv(ap) && print_info) {
1970 ata_dev_printk(dev, KERN_INFO,
1971 "%s: %s, %s, max %s\n",
1972 revbuf, modelbuf, fwrevbuf,
1973 ata_mode_string(xfer_mask));
1974 ata_dev_printk(dev, KERN_INFO,
1975 "%Lu sectors, multi %u: %s %s\n",
Tejun Heof15a1da2006-05-15 20:57:56 +09001976 (unsigned long long)dev->n_sectors,
Eric D. Mudama3f64f562007-01-30 23:00:40 -07001977 dev->multi_count, lba_desc, ncq_desc);
1978 }
Tejun Heoffeae412006-03-01 16:09:35 +09001979 } else {
Albert Lee8bf62ece2005-05-12 15:29:42 -04001980 /* CHS */
1981
1982 /* Default translation */
Tejun Heo1148c3a2006-03-13 19:48:04 +09001983 dev->cylinders = id[1];
1984 dev->heads = id[3];
1985 dev->sectors = id[6];
Albert Lee8bf62ece2005-05-12 15:29:42 -04001986
Tejun Heo1148c3a2006-03-13 19:48:04 +09001987 if (ata_id_current_chs_valid(id)) {
Albert Lee8bf62ece2005-05-12 15:29:42 -04001988 /* Current CHS translation is valid. */
Tejun Heo1148c3a2006-03-13 19:48:04 +09001989 dev->cylinders = id[54];
1990 dev->heads = id[55];
1991 dev->sectors = id[56];
Albert Lee8bf62ece2005-05-12 15:29:42 -04001992 }
1993
1994 /* print device info to dmesg */
Eric D. Mudama3f64f562007-01-30 23:00:40 -07001995 if (ata_msg_drv(ap) && print_info) {
Tejun Heo88574552006-06-25 20:00:35 +09001996 ata_dev_printk(dev, KERN_INFO,
Eric D. Mudama3f64f562007-01-30 23:00:40 -07001997 "%s: %s, %s, max %s\n",
1998 revbuf, modelbuf, fwrevbuf,
1999 ata_mode_string(xfer_mask));
Jeff Garzika84471f2007-02-26 05:51:33 -05002000 ata_dev_printk(dev, KERN_INFO,
Eric D. Mudama3f64f562007-01-30 23:00:40 -07002001 "%Lu sectors, multi %u, CHS %u/%u/%u\n",
2002 (unsigned long long)dev->n_sectors,
2003 dev->multi_count, dev->cylinders,
2004 dev->heads, dev->sectors);
2005 }
Albert Lee07f6f7d2005-11-01 19:33:20 +08002006 }
2007
Tejun Heo6e7846e2006-02-12 23:32:58 +09002008 dev->cdb_len = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 }
2010
2011 /* ATAPI-specific feature tests */
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05002012 else if (dev->class == ATA_DEV_ATAPI) {
Tejun Heo854c73a2007-09-23 13:14:11 +09002013 const char *cdb_intr_string = "";
2014 const char *atapi_an_string = "";
Albert Lee08a556d2006-03-31 13:29:04 +08002015
Tejun Heo1148c3a2006-03-13 19:48:04 +09002016 rc = atapi_cdb_len(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002018 if (ata_msg_warn(ap))
Tejun Heo88574552006-06-25 20:00:35 +09002019 ata_dev_printk(dev, KERN_WARNING,
2020 "unsupported CDB len\n");
Tejun Heoffeae412006-03-01 16:09:35 +09002021 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 goto err_out_nosup;
2023 }
Tejun Heo6e7846e2006-02-12 23:32:58 +09002024 dev->cdb_len = (unsigned int) rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04002026 /*
2027 * check to see if this ATAPI device supports
2028 * Asynchronous Notification
2029 */
Tejun Heo854c73a2007-09-23 13:14:11 +09002030 if ((ap->flags & ATA_FLAG_AN) && ata_id_has_atapi_AN(id)) {
2031 unsigned int err_mask;
2032
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04002033 /* issue SET feature command to turn this on */
Tejun Heo854c73a2007-09-23 13:14:11 +09002034 err_mask = ata_dev_set_AN(dev, SETFEATURES_SATA_ENABLE);
2035 if (err_mask)
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04002036 ata_dev_printk(dev, KERN_ERR,
Tejun Heo854c73a2007-09-23 13:14:11 +09002037 "failed to enable ATAPI AN "
2038 "(err_mask=0x%x)\n", err_mask);
2039 else {
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04002040 dev->flags |= ATA_DFLAG_AN;
Tejun Heo854c73a2007-09-23 13:14:11 +09002041 atapi_an_string = ", ATAPI AN";
2042 }
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04002043 }
2044
Albert Lee08a556d2006-03-31 13:29:04 +08002045 if (ata_id_cdb_intr(dev->id)) {
Albert Lee312f7da2005-09-27 17:38:03 +08002046 dev->flags |= ATA_DFLAG_CDB_INTR;
Albert Lee08a556d2006-03-31 13:29:04 +08002047 cdb_intr_string = ", CDB intr";
2048 }
Albert Lee312f7da2005-09-27 17:38:03 +08002049
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 /* print device info to dmesg */
Borislav Petkov5afc8142006-06-27 14:51:25 +02002051 if (ata_msg_drv(ap) && print_info)
Albert Leeef143d52007-06-05 13:01:33 +08002052 ata_dev_printk(dev, KERN_INFO,
Tejun Heo854c73a2007-09-23 13:14:11 +09002053 "ATAPI: %s, %s, max %s%s%s\n",
Albert Leeef143d52007-06-05 13:01:33 +08002054 modelbuf, fwrevbuf,
Tejun Heo12436c32006-05-15 20:59:15 +09002055 ata_mode_string(xfer_mask),
Tejun Heo854c73a2007-09-23 13:14:11 +09002056 cdb_intr_string, atapi_an_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 }
2058
Tejun Heo914ed352006-11-01 18:39:55 +09002059 /* determine max_sectors */
2060 dev->max_sectors = ATA_MAX_SECTORS;
2061 if (dev->flags & ATA_DFLAG_LBA48)
2062 dev->max_sectors = ATA_MAX_SECTORS_LBA48;
2063
Alan Cox93590852006-09-12 16:55:12 +01002064 if (dev->horkage & ATA_HORKAGE_DIAGNOSTIC) {
2065 /* Let the user know. We don't want to disallow opens for
2066 rescue purposes, or in case the vendor is just a blithering
2067 idiot */
2068 if (print_info) {
2069 ata_dev_printk(dev, KERN_WARNING,
2070"Drive reports diagnostics failure. This may indicate a drive\n");
2071 ata_dev_printk(dev, KERN_WARNING,
2072"fault or invalid emulation. Contact drive vendor for information.\n");
2073 }
2074 }
2075
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09002076 /* limit bridge transfers to udma5, 200 sectors */
Tejun Heo3373efd2006-05-15 20:57:53 +09002077 if (ata_dev_knobble(dev)) {
Borislav Petkov5afc8142006-06-27 14:51:25 +02002078 if (ata_msg_drv(ap) && print_info)
Tejun Heof15a1da2006-05-15 20:57:56 +09002079 ata_dev_printk(dev, KERN_INFO,
2080 "applying bridge limits\n");
Tejun Heo5a529132006-03-24 14:07:50 +09002081 dev->udma_mask &= ATA_UDMA5;
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09002082 dev->max_sectors = ATA_MAX_SECTORS;
2083 }
2084
Tejun Heo75683fe2007-07-05 13:31:27 +09002085 if (dev->horkage & ATA_HORKAGE_MAX_SEC_128)
Tejun Heo03ec52d2007-04-12 13:38:11 +09002086 dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_128,
2087 dev->max_sectors);
Albert Lee18d6e9d2007-04-02 11:34:15 +08002088
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09002089 if (ap->ops->dev_config)
Alancd0d3bb2007-03-02 00:56:15 +00002090 ap->ops->dev_config(dev);
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09002091
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002092 if (ata_msg_probe(ap))
2093 ata_dev_printk(dev, KERN_DEBUG, "%s: EXIT, drv_stat = 0x%x\n",
2094 __FUNCTION__, ata_chk_status(ap));
Tejun Heoffeae412006-03-01 16:09:35 +09002095 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
2097err_out_nosup:
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002098 if (ata_msg_probe(ap))
Tejun Heo88574552006-06-25 20:00:35 +09002099 ata_dev_printk(dev, KERN_DEBUG,
2100 "%s: EXIT, err\n", __FUNCTION__);
Tejun Heoffeae412006-03-01 16:09:35 +09002101 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102}
2103
2104/**
Alan Cox2e41e8e2007-03-08 23:19:19 +00002105 * ata_cable_40wire - return 40 wire cable type
Alan Coxbe0d18d2007-03-06 02:37:56 -08002106 * @ap: port
2107 *
Alan Cox2e41e8e2007-03-08 23:19:19 +00002108 * Helper method for drivers which want to hardwire 40 wire cable
Alan Coxbe0d18d2007-03-06 02:37:56 -08002109 * detection.
2110 */
2111
2112int ata_cable_40wire(struct ata_port *ap)
2113{
2114 return ATA_CBL_PATA40;
2115}
2116
2117/**
Alan Cox2e41e8e2007-03-08 23:19:19 +00002118 * ata_cable_80wire - return 80 wire cable type
Alan Coxbe0d18d2007-03-06 02:37:56 -08002119 * @ap: port
2120 *
Alan Cox2e41e8e2007-03-08 23:19:19 +00002121 * Helper method for drivers which want to hardwire 80 wire cable
Alan Coxbe0d18d2007-03-06 02:37:56 -08002122 * detection.
2123 */
2124
2125int ata_cable_80wire(struct ata_port *ap)
2126{
2127 return ATA_CBL_PATA80;
2128}
2129
2130/**
2131 * ata_cable_unknown - return unknown PATA cable.
2132 * @ap: port
2133 *
2134 * Helper method for drivers which have no PATA cable detection.
2135 */
2136
2137int ata_cable_unknown(struct ata_port *ap)
2138{
2139 return ATA_CBL_PATA_UNK;
2140}
2141
2142/**
2143 * ata_cable_sata - return SATA cable type
2144 * @ap: port
2145 *
2146 * Helper method for drivers which have SATA cables
2147 */
2148
2149int ata_cable_sata(struct ata_port *ap)
2150{
2151 return ATA_CBL_SATA;
2152}
2153
2154/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 * ata_bus_probe - Reset and probe ATA bus
2156 * @ap: Bus to probe
2157 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04002158 * Master ATA bus probing function. Initiates a hardware-dependent
2159 * bus reset, then attempts to identify any devices found on
2160 * the bus.
2161 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002163 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 *
2165 * RETURNS:
Tejun Heo96072e62006-04-01 01:38:17 +09002166 * Zero on success, negative errno otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 */
2168
Brian King80289162006-08-07 14:27:31 -05002169int ata_bus_probe(struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170{
Tejun Heo28ca5c52006-03-01 16:09:36 +09002171 unsigned int classes[ATA_MAX_DEVICES];
Tejun Heo14d2bac2006-04-02 17:54:46 +09002172 int tries[ATA_MAX_DEVICES];
Tejun Heof58229f2007-08-06 18:36:23 +09002173 int rc;
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002174 struct ata_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175
Tejun Heo28ca5c52006-03-01 16:09:36 +09002176 ata_port_probe(ap);
2177
Tejun Heof58229f2007-08-06 18:36:23 +09002178 ata_link_for_each_dev(dev, &ap->link)
2179 tries[dev->devno] = ATA_PROBE_MAX_TRIES;
Tejun Heo14d2bac2006-04-02 17:54:46 +09002180
2181 retry:
Tejun Heo20444702006-03-13 01:57:01 +09002182 /* reset and determine device classes */
Tejun Heo52783c52006-05-31 18:28:22 +09002183 ap->ops->phy_reset(ap);
Tejun Heo2061a472006-03-12 00:57:39 +09002184
Tejun Heof58229f2007-08-06 18:36:23 +09002185 ata_link_for_each_dev(dev, &ap->link) {
Tejun Heo52783c52006-05-31 18:28:22 +09002186 if (!(ap->flags & ATA_FLAG_DISABLED) &&
2187 dev->class != ATA_DEV_UNKNOWN)
2188 classes[dev->devno] = dev->class;
2189 else
2190 classes[dev->devno] = ATA_DEV_NONE;
Tejun Heo20444702006-03-13 01:57:01 +09002191
Tejun Heo52783c52006-05-31 18:28:22 +09002192 dev->class = ATA_DEV_UNKNOWN;
Tejun Heo28ca5c52006-03-01 16:09:36 +09002193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194
Tejun Heo52783c52006-05-31 18:28:22 +09002195 ata_port_probe(ap);
Tejun Heo20444702006-03-13 01:57:01 +09002196
Alan Coxb6079ca2006-05-22 16:52:06 +01002197 /* after the reset the device state is PIO 0 and the controller
2198 state is undefined. Record the mode */
2199
Tejun Heof58229f2007-08-06 18:36:23 +09002200 ata_link_for_each_dev(dev, &ap->link)
2201 dev->pio_mode = XFER_PIO_0;
Alan Coxb6079ca2006-05-22 16:52:06 +01002202
Jeff Garzikf31f0cc2007-03-02 17:47:28 -05002203 /* read IDENTIFY page and configure devices. We have to do the identify
2204 specific sequence bass-ackwards so that PDIAG- is released by
2205 the slave device */
2206
Tejun Heof58229f2007-08-06 18:36:23 +09002207 ata_link_for_each_dev(dev, &ap->link) {
2208 if (tries[dev->devno])
2209 dev->class = classes[dev->devno];
Tejun Heo14d2bac2006-04-02 17:54:46 +09002210
Tejun Heoe1211e32006-04-01 01:38:18 +09002211 if (!ata_dev_enabled(dev))
Tejun Heoffeae412006-03-01 16:09:35 +09002212 continue;
2213
Tejun Heobff04642006-11-10 18:08:10 +09002214 rc = ata_dev_read_id(dev, &dev->class, ATA_READID_POSTRESET,
2215 dev->id);
Tejun Heo14d2bac2006-04-02 17:54:46 +09002216 if (rc)
2217 goto fail;
Jeff Garzikf31f0cc2007-03-02 17:47:28 -05002218 }
2219
Alan Coxbe0d18d2007-03-06 02:37:56 -08002220 /* Now ask for the cable type as PDIAG- should have been released */
2221 if (ap->ops->cable_detect)
2222 ap->cbl = ap->ops->cable_detect(ap);
2223
Alan Cox614fe292007-08-22 23:22:45 +01002224 /* We may have SATA bridge glue hiding here irrespective of the
2225 reported cable types and sensed types */
2226 ata_link_for_each_dev(dev, &ap->link) {
2227 if (!ata_dev_enabled(dev))
2228 continue;
2229 /* SATA drives indicate we have a bridge. We don't know which
2230 end of the link the bridge is which is a problem */
2231 if (ata_id_is_sata(dev->id))
2232 ap->cbl = ATA_CBL_SATA;
2233 }
2234
Jeff Garzikf31f0cc2007-03-02 17:47:28 -05002235 /* After the identify sequence we can now set up the devices. We do
2236 this in the normal order so that the user doesn't get confused */
2237
Tejun Heof58229f2007-08-06 18:36:23 +09002238 ata_link_for_each_dev(dev, &ap->link) {
Jeff Garzikf31f0cc2007-03-02 17:47:28 -05002239 if (!ata_dev_enabled(dev))
2240 continue;
Tejun Heoffeae412006-03-01 16:09:35 +09002241
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002242 ap->link.eh_context.i.flags |= ATA_EHI_PRINTINFO;
Tejun Heoefdaedc2006-11-01 18:38:52 +09002243 rc = ata_dev_configure(dev);
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002244 ap->link.eh_context.i.flags &= ~ATA_EHI_PRINTINFO;
Tejun Heo14d2bac2006-04-02 17:54:46 +09002245 if (rc)
2246 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 }
2248
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002249 /* configure transfer mode */
Tejun Heo02607312007-08-06 18:36:23 +09002250 rc = ata_set_mode(&ap->link, &dev);
Tejun Heo4ae72a12007-02-02 16:22:30 +09002251 if (rc)
Tejun Heo51713d32006-04-11 22:26:29 +09002252 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253
Tejun Heof58229f2007-08-06 18:36:23 +09002254 ata_link_for_each_dev(dev, &ap->link)
2255 if (ata_dev_enabled(dev))
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002256 return 0;
Alan Coxe35a9e02006-03-27 18:46:37 +01002257
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002258 /* no device present, disable port */
2259 ata_port_disable(ap);
Tejun Heo96072e62006-04-01 01:38:17 +09002260 return -ENODEV;
Tejun Heo14d2bac2006-04-02 17:54:46 +09002261
2262 fail:
Tejun Heo4ae72a12007-02-02 16:22:30 +09002263 tries[dev->devno]--;
2264
Tejun Heo14d2bac2006-04-02 17:54:46 +09002265 switch (rc) {
2266 case -EINVAL:
Tejun Heo4ae72a12007-02-02 16:22:30 +09002267 /* eeek, something went very wrong, give up */
Tejun Heo14d2bac2006-04-02 17:54:46 +09002268 tries[dev->devno] = 0;
2269 break;
Tejun Heo4ae72a12007-02-02 16:22:30 +09002270
2271 case -ENODEV:
2272 /* give it just one more chance */
2273 tries[dev->devno] = min(tries[dev->devno], 1);
Tejun Heo14d2bac2006-04-02 17:54:46 +09002274 case -EIO:
Tejun Heo4ae72a12007-02-02 16:22:30 +09002275 if (tries[dev->devno] == 1) {
2276 /* This is the last chance, better to slow
2277 * down than lose it.
2278 */
Tejun Heo936fd732007-08-06 18:36:23 +09002279 sata_down_spd_limit(&ap->link);
Tejun Heo4ae72a12007-02-02 16:22:30 +09002280 ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
2281 }
Tejun Heo14d2bac2006-04-02 17:54:46 +09002282 }
2283
Tejun Heo4ae72a12007-02-02 16:22:30 +09002284 if (!tries[dev->devno])
Tejun Heo3373efd2006-05-15 20:57:53 +09002285 ata_dev_disable(dev);
Tejun Heoec573752006-04-11 22:26:29 +09002286
Tejun Heo14d2bac2006-04-02 17:54:46 +09002287 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288}
2289
2290/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002291 * ata_port_probe - Mark port as enabled
2292 * @ap: Port for which we indicate enablement
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04002294 * Modify @ap data structure such that the system
2295 * thinks that the entire port is enabled.
2296 *
Jeff Garzikcca39742006-08-24 03:19:22 -04002297 * LOCKING: host lock, or some other form of
Jeff Garzik0cba6322005-05-30 19:49:12 -04002298 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 */
2300
2301void ata_port_probe(struct ata_port *ap)
2302{
Tejun Heo198e0fe2006-04-02 18:51:52 +09002303 ap->flags &= ~ATA_FLAG_DISABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304}
2305
2306/**
Tejun Heo3be680b2005-12-19 22:35:02 +09002307 * sata_print_link_status - Print SATA link status
Tejun Heo936fd732007-08-06 18:36:23 +09002308 * @link: SATA link to printk link status about
Tejun Heo3be680b2005-12-19 22:35:02 +09002309 *
2310 * This function prints link speed and status of a SATA link.
2311 *
2312 * LOCKING:
2313 * None.
2314 */
Tejun Heo936fd732007-08-06 18:36:23 +09002315void sata_print_link_status(struct ata_link *link)
Tejun Heo3be680b2005-12-19 22:35:02 +09002316{
Tejun Heo6d5f9732006-04-03 00:09:41 +09002317 u32 sstatus, scontrol, tmp;
Tejun Heo3be680b2005-12-19 22:35:02 +09002318
Tejun Heo936fd732007-08-06 18:36:23 +09002319 if (sata_scr_read(link, SCR_STATUS, &sstatus))
Tejun Heo3be680b2005-12-19 22:35:02 +09002320 return;
Tejun Heo936fd732007-08-06 18:36:23 +09002321 sata_scr_read(link, SCR_CONTROL, &scontrol);
Tejun Heo3be680b2005-12-19 22:35:02 +09002322
Tejun Heo936fd732007-08-06 18:36:23 +09002323 if (ata_link_online(link)) {
Tejun Heo3be680b2005-12-19 22:35:02 +09002324 tmp = (sstatus >> 4) & 0xf;
Tejun Heo936fd732007-08-06 18:36:23 +09002325 ata_link_printk(link, KERN_INFO,
Tejun Heof15a1da2006-05-15 20:57:56 +09002326 "SATA link up %s (SStatus %X SControl %X)\n",
2327 sata_spd_string(tmp), sstatus, scontrol);
Tejun Heo3be680b2005-12-19 22:35:02 +09002328 } else {
Tejun Heo936fd732007-08-06 18:36:23 +09002329 ata_link_printk(link, KERN_INFO,
Tejun Heof15a1da2006-05-15 20:57:56 +09002330 "SATA link down (SStatus %X SControl %X)\n",
2331 sstatus, scontrol);
Tejun Heo3be680b2005-12-19 22:35:02 +09002332 }
2333}
2334
2335/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04002336 * __sata_phy_reset - Wake/reset a low-level SATA PHY
2337 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002339 * This function issues commands to standard SATA Sxxx
2340 * PHY registers, to wake up the phy (and device), and
2341 * clear any reset condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 *
2343 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002344 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 *
2346 */
2347void __sata_phy_reset(struct ata_port *ap)
2348{
Tejun Heo936fd732007-08-06 18:36:23 +09002349 struct ata_link *link = &ap->link;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 unsigned long timeout = jiffies + (HZ * 5);
Tejun Heo936fd732007-08-06 18:36:23 +09002351 u32 sstatus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
2353 if (ap->flags & ATA_FLAG_SATA_RESET) {
Brett Russcdcca89e2005-03-28 15:10:27 -05002354 /* issue phy wake/reset */
Tejun Heo936fd732007-08-06 18:36:23 +09002355 sata_scr_write_flush(link, SCR_CONTROL, 0x301);
Tejun Heo62ba2842005-06-26 23:27:19 +09002356 /* Couldn't find anything in SATA I/II specs, but
2357 * AHCI-1.1 10.4.2 says at least 1 ms. */
2358 mdelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 }
Tejun Heo81952c52006-05-15 20:57:47 +09002360 /* phy wake/clear reset */
Tejun Heo936fd732007-08-06 18:36:23 +09002361 sata_scr_write_flush(link, SCR_CONTROL, 0x300);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362
2363 /* wait for phy to become ready, if necessary */
2364 do {
2365 msleep(200);
Tejun Heo936fd732007-08-06 18:36:23 +09002366 sata_scr_read(link, SCR_STATUS, &sstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 if ((sstatus & 0xf) != 1)
2368 break;
2369 } while (time_before(jiffies, timeout));
2370
Tejun Heo3be680b2005-12-19 22:35:02 +09002371 /* print link status */
Tejun Heo936fd732007-08-06 18:36:23 +09002372 sata_print_link_status(link);
Jeff Garzik656563e2005-11-20 03:36:45 -05002373
Tejun Heo3be680b2005-12-19 22:35:02 +09002374 /* TODO: phy layer with polling, timeouts, etc. */
Tejun Heo936fd732007-08-06 18:36:23 +09002375 if (!ata_link_offline(link))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002376 ata_port_probe(ap);
Tejun Heo3be680b2005-12-19 22:35:02 +09002377 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378 ata_port_disable(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379
Tejun Heo198e0fe2006-04-02 18:51:52 +09002380 if (ap->flags & ATA_FLAG_DISABLED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 return;
2382
2383 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
2384 ata_port_disable(ap);
2385 return;
2386 }
2387
2388 ap->cbl = ATA_CBL_SATA;
2389}
2390
2391/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04002392 * sata_phy_reset - Reset SATA bus.
2393 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002395 * This function resets the SATA bus, and then probes
2396 * the bus for devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397 *
2398 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002399 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 *
2401 */
2402void sata_phy_reset(struct ata_port *ap)
2403{
2404 __sata_phy_reset(ap);
Tejun Heo198e0fe2006-04-02 18:51:52 +09002405 if (ap->flags & ATA_FLAG_DISABLED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 return;
2407 ata_bus_reset(ap);
2408}
2409
2410/**
Alan Coxebdfca62006-03-23 15:38:34 +00002411 * ata_dev_pair - return other device on cable
Alan Coxebdfca62006-03-23 15:38:34 +00002412 * @adev: device
2413 *
2414 * Obtain the other device on the same cable, or if none is
2415 * present NULL is returned
2416 */
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002417
Tejun Heo3373efd2006-05-15 20:57:53 +09002418struct ata_device *ata_dev_pair(struct ata_device *adev)
Alan Coxebdfca62006-03-23 15:38:34 +00002419{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002420 struct ata_link *link = adev->link;
2421 struct ata_device *pair = &link->device[1 - adev->devno];
Tejun Heoe1211e32006-04-01 01:38:18 +09002422 if (!ata_dev_enabled(pair))
Alan Coxebdfca62006-03-23 15:38:34 +00002423 return NULL;
2424 return pair;
2425}
2426
2427/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04002428 * ata_port_disable - Disable port.
2429 * @ap: Port to be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002431 * Modify @ap data structure such that the system
2432 * thinks that the entire port is disabled, and should
2433 * never attempt to probe or communicate with devices
2434 * on this port.
2435 *
Jeff Garzikcca39742006-08-24 03:19:22 -04002436 * LOCKING: host lock, or some other form of
Jeff Garzik780a87f2005-05-30 15:41:05 -04002437 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 */
2439
2440void ata_port_disable(struct ata_port *ap)
2441{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002442 ap->link.device[0].class = ATA_DEV_NONE;
2443 ap->link.device[1].class = ATA_DEV_NONE;
Tejun Heo198e0fe2006-04-02 18:51:52 +09002444 ap->flags |= ATA_FLAG_DISABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002445}
2446
Tejun Heo1c3fae42006-04-02 20:53:28 +09002447/**
Tejun Heo3c567b72006-05-15 20:57:23 +09002448 * sata_down_spd_limit - adjust SATA spd limit downward
Tejun Heo936fd732007-08-06 18:36:23 +09002449 * @link: Link to adjust SATA spd limit for
Tejun Heo1c3fae42006-04-02 20:53:28 +09002450 *
Tejun Heo936fd732007-08-06 18:36:23 +09002451 * Adjust SATA spd limit of @link downward. Note that this
Tejun Heo1c3fae42006-04-02 20:53:28 +09002452 * function only adjusts the limit. The change must be applied
Tejun Heo3c567b72006-05-15 20:57:23 +09002453 * using sata_set_spd().
Tejun Heo1c3fae42006-04-02 20:53:28 +09002454 *
2455 * LOCKING:
2456 * Inherited from caller.
2457 *
2458 * RETURNS:
2459 * 0 on success, negative errno on failure
2460 */
Tejun Heo936fd732007-08-06 18:36:23 +09002461int sata_down_spd_limit(struct ata_link *link)
Tejun Heo1c3fae42006-04-02 20:53:28 +09002462{
Tejun Heo81952c52006-05-15 20:57:47 +09002463 u32 sstatus, spd, mask;
2464 int rc, highbit;
Tejun Heo1c3fae42006-04-02 20:53:28 +09002465
Tejun Heo936fd732007-08-06 18:36:23 +09002466 if (!sata_scr_valid(link))
Tejun Heo008a7892007-07-16 14:29:40 +09002467 return -EOPNOTSUPP;
2468
2469 /* If SCR can be read, use it to determine the current SPD.
Tejun Heo936fd732007-08-06 18:36:23 +09002470 * If not, use cached value in link->sata_spd.
Tejun Heo008a7892007-07-16 14:29:40 +09002471 */
Tejun Heo936fd732007-08-06 18:36:23 +09002472 rc = sata_scr_read(link, SCR_STATUS, &sstatus);
Tejun Heo008a7892007-07-16 14:29:40 +09002473 if (rc == 0)
2474 spd = (sstatus >> 4) & 0xf;
2475 else
Tejun Heo936fd732007-08-06 18:36:23 +09002476 spd = link->sata_spd;
Tejun Heo1c3fae42006-04-02 20:53:28 +09002477
Tejun Heo936fd732007-08-06 18:36:23 +09002478 mask = link->sata_spd_limit;
Tejun Heo1c3fae42006-04-02 20:53:28 +09002479 if (mask <= 1)
2480 return -EINVAL;
Tejun Heo008a7892007-07-16 14:29:40 +09002481
2482 /* unconditionally mask off the highest bit */
Tejun Heo1c3fae42006-04-02 20:53:28 +09002483 highbit = fls(mask) - 1;
2484 mask &= ~(1 << highbit);
2485
Tejun Heo008a7892007-07-16 14:29:40 +09002486 /* Mask off all speeds higher than or equal to the current
2487 * one. Force 1.5Gbps if current SPD is not available.
2488 */
2489 if (spd > 1)
2490 mask &= (1 << (spd - 1)) - 1;
2491 else
2492 mask &= 1;
2493
2494 /* were we already at the bottom? */
Tejun Heo1c3fae42006-04-02 20:53:28 +09002495 if (!mask)
2496 return -EINVAL;
2497
Tejun Heo936fd732007-08-06 18:36:23 +09002498 link->sata_spd_limit = mask;
Tejun Heo1c3fae42006-04-02 20:53:28 +09002499
Tejun Heo936fd732007-08-06 18:36:23 +09002500 ata_link_printk(link, KERN_WARNING, "limiting SATA link speed to %s\n",
Tejun Heof15a1da2006-05-15 20:57:56 +09002501 sata_spd_string(fls(mask)));
Tejun Heo1c3fae42006-04-02 20:53:28 +09002502
2503 return 0;
2504}
2505
Tejun Heo936fd732007-08-06 18:36:23 +09002506static int __sata_set_spd_needed(struct ata_link *link, u32 *scontrol)
Tejun Heo1c3fae42006-04-02 20:53:28 +09002507{
2508 u32 spd, limit;
2509
Tejun Heo936fd732007-08-06 18:36:23 +09002510 if (link->sata_spd_limit == UINT_MAX)
Tejun Heo1c3fae42006-04-02 20:53:28 +09002511 limit = 0;
2512 else
Tejun Heo936fd732007-08-06 18:36:23 +09002513 limit = fls(link->sata_spd_limit);
Tejun Heo1c3fae42006-04-02 20:53:28 +09002514
2515 spd = (*scontrol >> 4) & 0xf;
2516 *scontrol = (*scontrol & ~0xf0) | ((limit & 0xf) << 4);
2517
2518 return spd != limit;
2519}
2520
2521/**
Tejun Heo3c567b72006-05-15 20:57:23 +09002522 * sata_set_spd_needed - is SATA spd configuration needed
Tejun Heo936fd732007-08-06 18:36:23 +09002523 * @link: Link in question
Tejun Heo1c3fae42006-04-02 20:53:28 +09002524 *
2525 * Test whether the spd limit in SControl matches
Tejun Heo936fd732007-08-06 18:36:23 +09002526 * @link->sata_spd_limit. This function is used to determine
Tejun Heo1c3fae42006-04-02 20:53:28 +09002527 * whether hardreset is necessary to apply SATA spd
2528 * configuration.
2529 *
2530 * LOCKING:
2531 * Inherited from caller.
2532 *
2533 * RETURNS:
2534 * 1 if SATA spd configuration is needed, 0 otherwise.
2535 */
Tejun Heo936fd732007-08-06 18:36:23 +09002536int sata_set_spd_needed(struct ata_link *link)
Tejun Heo1c3fae42006-04-02 20:53:28 +09002537{
2538 u32 scontrol;
2539
Tejun Heo936fd732007-08-06 18:36:23 +09002540 if (sata_scr_read(link, SCR_CONTROL, &scontrol))
Tejun Heo1c3fae42006-04-02 20:53:28 +09002541 return 0;
2542
Tejun Heo936fd732007-08-06 18:36:23 +09002543 return __sata_set_spd_needed(link, &scontrol);
Tejun Heo1c3fae42006-04-02 20:53:28 +09002544}
2545
2546/**
Tejun Heo3c567b72006-05-15 20:57:23 +09002547 * sata_set_spd - set SATA spd according to spd limit
Tejun Heo936fd732007-08-06 18:36:23 +09002548 * @link: Link to set SATA spd for
Tejun Heo1c3fae42006-04-02 20:53:28 +09002549 *
Tejun Heo936fd732007-08-06 18:36:23 +09002550 * Set SATA spd of @link according to sata_spd_limit.
Tejun Heo1c3fae42006-04-02 20:53:28 +09002551 *
2552 * LOCKING:
2553 * Inherited from caller.
2554 *
2555 * RETURNS:
2556 * 0 if spd doesn't need to be changed, 1 if spd has been
Tejun Heo81952c52006-05-15 20:57:47 +09002557 * changed. Negative errno if SCR registers are inaccessible.
Tejun Heo1c3fae42006-04-02 20:53:28 +09002558 */
Tejun Heo936fd732007-08-06 18:36:23 +09002559int sata_set_spd(struct ata_link *link)
Tejun Heo1c3fae42006-04-02 20:53:28 +09002560{
2561 u32 scontrol;
Tejun Heo81952c52006-05-15 20:57:47 +09002562 int rc;
Tejun Heo1c3fae42006-04-02 20:53:28 +09002563
Tejun Heo936fd732007-08-06 18:36:23 +09002564 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
Tejun Heo81952c52006-05-15 20:57:47 +09002565 return rc;
Tejun Heo1c3fae42006-04-02 20:53:28 +09002566
Tejun Heo936fd732007-08-06 18:36:23 +09002567 if (!__sata_set_spd_needed(link, &scontrol))
Tejun Heo1c3fae42006-04-02 20:53:28 +09002568 return 0;
2569
Tejun Heo936fd732007-08-06 18:36:23 +09002570 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
Tejun Heo81952c52006-05-15 20:57:47 +09002571 return rc;
2572
Tejun Heo1c3fae42006-04-02 20:53:28 +09002573 return 1;
2574}
2575
Alan Cox452503f2005-10-21 19:01:32 -04002576/*
2577 * This mode timing computation functionality is ported over from
2578 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
2579 */
2580/*
Alan Coxb352e572006-08-10 18:52:12 +01002581 * PIO 0-4, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
Alan Cox452503f2005-10-21 19:01:32 -04002582 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
Alan Coxb352e572006-08-10 18:52:12 +01002583 * for UDMA6, which is currently supported only by Maxtor drives.
2584 *
2585 * For PIO 5/6 MWDMA 3/4 see the CFA specification 3.0.
Alan Cox452503f2005-10-21 19:01:32 -04002586 */
2587
2588static const struct ata_timing ata_timing[] = {
2589
2590 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
2591 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
2592 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
2593 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
2594
Alan Coxb352e572006-08-10 18:52:12 +01002595 { XFER_MW_DMA_4, 25, 0, 0, 0, 55, 20, 80, 0 },
2596 { XFER_MW_DMA_3, 25, 0, 0, 0, 65, 25, 100, 0 },
Alan Cox452503f2005-10-21 19:01:32 -04002597 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
2598 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
2599 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
2600
2601/* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002602
Alan Cox452503f2005-10-21 19:01:32 -04002603 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
2604 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
2605 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002606
Alan Cox452503f2005-10-21 19:01:32 -04002607 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
2608 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
2609 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
2610
Alan Coxb352e572006-08-10 18:52:12 +01002611 { XFER_PIO_6, 10, 55, 20, 80, 55, 20, 80, 0 },
2612 { XFER_PIO_5, 15, 65, 25, 100, 65, 25, 100, 0 },
Alan Cox452503f2005-10-21 19:01:32 -04002613 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
2614 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
2615
2616 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
2617 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
2618 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
2619
2620/* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
2621
2622 { 0xFF }
2623};
2624
2625#define ENOUGH(v,unit) (((v)-1)/(unit)+1)
2626#define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
2627
2628static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
2629{
2630 q->setup = EZ(t->setup * 1000, T);
2631 q->act8b = EZ(t->act8b * 1000, T);
2632 q->rec8b = EZ(t->rec8b * 1000, T);
2633 q->cyc8b = EZ(t->cyc8b * 1000, T);
2634 q->active = EZ(t->active * 1000, T);
2635 q->recover = EZ(t->recover * 1000, T);
2636 q->cycle = EZ(t->cycle * 1000, T);
2637 q->udma = EZ(t->udma * 1000, UT);
2638}
2639
2640void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
2641 struct ata_timing *m, unsigned int what)
2642{
2643 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
2644 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
2645 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
2646 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
2647 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
2648 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
2649 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
2650 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
2651}
2652
2653static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
2654{
2655 const struct ata_timing *t;
2656
2657 for (t = ata_timing; t->mode != speed; t++)
Alan Cox91190752005-10-26 12:17:46 -04002658 if (t->mode == 0xFF)
Alan Cox452503f2005-10-21 19:01:32 -04002659 return NULL;
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002660 return t;
Alan Cox452503f2005-10-21 19:01:32 -04002661}
2662
2663int ata_timing_compute(struct ata_device *adev, unsigned short speed,
2664 struct ata_timing *t, int T, int UT)
2665{
2666 const struct ata_timing *s;
2667 struct ata_timing p;
2668
2669 /*
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002670 * Find the mode.
Albert Lee75b1f2f2005-11-16 17:06:18 +08002671 */
Alan Cox452503f2005-10-21 19:01:32 -04002672
2673 if (!(s = ata_timing_find_mode(speed)))
2674 return -EINVAL;
2675
Albert Lee75b1f2f2005-11-16 17:06:18 +08002676 memcpy(t, s, sizeof(*s));
2677
Alan Cox452503f2005-10-21 19:01:32 -04002678 /*
2679 * If the drive is an EIDE drive, it can tell us it needs extended
2680 * PIO/MW_DMA cycle timing.
2681 */
2682
2683 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
2684 memset(&p, 0, sizeof(p));
2685 if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
2686 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
2687 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
2688 } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
2689 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
2690 }
2691 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
2692 }
2693
2694 /*
2695 * Convert the timing to bus clock counts.
2696 */
2697
Albert Lee75b1f2f2005-11-16 17:06:18 +08002698 ata_timing_quantize(t, t, T, UT);
Alan Cox452503f2005-10-21 19:01:32 -04002699
2700 /*
Randy Dunlapc893a3a2006-01-28 13:15:32 -05002701 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
2702 * S.M.A.R.T * and some other commands. We have to ensure that the
2703 * DMA cycle timing is slower/equal than the fastest PIO timing.
Alan Cox452503f2005-10-21 19:01:32 -04002704 */
2705
Alanfd3367af2006-12-07 12:41:18 +00002706 if (speed > XFER_PIO_6) {
Alan Cox452503f2005-10-21 19:01:32 -04002707 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
2708 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
2709 }
2710
2711 /*
Randy Dunlapc893a3a2006-01-28 13:15:32 -05002712 * Lengthen active & recovery time so that cycle time is correct.
Alan Cox452503f2005-10-21 19:01:32 -04002713 */
2714
2715 if (t->act8b + t->rec8b < t->cyc8b) {
2716 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
2717 t->rec8b = t->cyc8b - t->act8b;
2718 }
2719
2720 if (t->active + t->recover < t->cycle) {
2721 t->active += (t->cycle - (t->active + t->recover)) / 2;
2722 t->recover = t->cycle - t->active;
2723 }
Jeff Garzika617c092007-05-21 20:14:23 -04002724
Alan Cox4f701d12007-04-23 11:55:36 +01002725 /* In a few cases quantisation may produce enough errors to
2726 leave t->cycle too low for the sum of active and recovery
2727 if so we must correct this */
2728 if (t->active + t->recover > t->cycle)
2729 t->cycle = t->active + t->recover;
Alan Cox452503f2005-10-21 19:01:32 -04002730
2731 return 0;
2732}
2733
Tejun Heocf176e12006-04-02 17:54:46 +09002734/**
2735 * ata_down_xfermask_limit - adjust dev xfer masks downward
Tejun Heocf176e12006-04-02 17:54:46 +09002736 * @dev: Device to adjust xfer masks
Tejun Heo458337d2007-02-02 16:22:30 +09002737 * @sel: ATA_DNXFER_* selector
Tejun Heocf176e12006-04-02 17:54:46 +09002738 *
2739 * Adjust xfer masks of @dev downward. Note that this function
2740 * does not apply the change. Invoking ata_set_mode() afterwards
2741 * will apply the limit.
2742 *
2743 * LOCKING:
2744 * Inherited from caller.
2745 *
2746 * RETURNS:
2747 * 0 on success, negative errno on failure
2748 */
Tejun Heo458337d2007-02-02 16:22:30 +09002749int ata_down_xfermask_limit(struct ata_device *dev, unsigned int sel)
Tejun Heocf176e12006-04-02 17:54:46 +09002750{
Tejun Heo458337d2007-02-02 16:22:30 +09002751 char buf[32];
2752 unsigned int orig_mask, xfer_mask;
2753 unsigned int pio_mask, mwdma_mask, udma_mask;
2754 int quiet, highbit;
Tejun Heocf176e12006-04-02 17:54:46 +09002755
Tejun Heo458337d2007-02-02 16:22:30 +09002756 quiet = !!(sel & ATA_DNXFER_QUIET);
2757 sel &= ~ATA_DNXFER_QUIET;
Tejun Heocf176e12006-04-02 17:54:46 +09002758
Tejun Heo458337d2007-02-02 16:22:30 +09002759 xfer_mask = orig_mask = ata_pack_xfermask(dev->pio_mask,
2760 dev->mwdma_mask,
2761 dev->udma_mask);
2762 ata_unpack_xfermask(xfer_mask, &pio_mask, &mwdma_mask, &udma_mask);
Tejun Heocf176e12006-04-02 17:54:46 +09002763
Tejun Heo458337d2007-02-02 16:22:30 +09002764 switch (sel) {
2765 case ATA_DNXFER_PIO:
2766 highbit = fls(pio_mask) - 1;
2767 pio_mask &= ~(1 << highbit);
2768 break;
2769
2770 case ATA_DNXFER_DMA:
2771 if (udma_mask) {
2772 highbit = fls(udma_mask) - 1;
2773 udma_mask &= ~(1 << highbit);
2774 if (!udma_mask)
2775 return -ENOENT;
2776 } else if (mwdma_mask) {
2777 highbit = fls(mwdma_mask) - 1;
2778 mwdma_mask &= ~(1 << highbit);
2779 if (!mwdma_mask)
2780 return -ENOENT;
2781 }
2782 break;
2783
2784 case ATA_DNXFER_40C:
2785 udma_mask &= ATA_UDMA_MASK_40C;
2786 break;
2787
2788 case ATA_DNXFER_FORCE_PIO0:
2789 pio_mask &= 1;
2790 case ATA_DNXFER_FORCE_PIO:
2791 mwdma_mask = 0;
2792 udma_mask = 0;
2793 break;
2794
Tejun Heo458337d2007-02-02 16:22:30 +09002795 default:
2796 BUG();
2797 }
2798
2799 xfer_mask &= ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
2800
2801 if (!(xfer_mask & ATA_MASK_PIO) || xfer_mask == orig_mask)
2802 return -ENOENT;
2803
2804 if (!quiet) {
2805 if (xfer_mask & (ATA_MASK_MWDMA | ATA_MASK_UDMA))
2806 snprintf(buf, sizeof(buf), "%s:%s",
2807 ata_mode_string(xfer_mask),
2808 ata_mode_string(xfer_mask & ATA_MASK_PIO));
2809 else
2810 snprintf(buf, sizeof(buf), "%s",
2811 ata_mode_string(xfer_mask));
2812
2813 ata_dev_printk(dev, KERN_WARNING,
2814 "limiting speed to %s\n", buf);
2815 }
Tejun Heocf176e12006-04-02 17:54:46 +09002816
2817 ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask,
2818 &dev->udma_mask);
2819
Tejun Heocf176e12006-04-02 17:54:46 +09002820 return 0;
Tejun Heocf176e12006-04-02 17:54:46 +09002821}
2822
Tejun Heo3373efd2006-05-15 20:57:53 +09002823static int ata_dev_set_mode(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002824{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002825 struct ata_eh_context *ehc = &dev->link->eh_context;
Tejun Heo83206a22006-03-24 15:25:31 +09002826 unsigned int err_mask;
2827 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828
Tejun Heoe8384602006-04-02 18:51:53 +09002829 dev->flags &= ~ATA_DFLAG_PIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 if (dev->xfer_shift == ATA_SHIFT_PIO)
2831 dev->flags |= ATA_DFLAG_PIO;
2832
Tejun Heo3373efd2006-05-15 20:57:53 +09002833 err_mask = ata_dev_set_xfermode(dev);
Alan11750a42007-02-05 16:28:30 +00002834 /* Old CFA may refuse this command, which is just fine */
2835 if (dev->xfer_shift == ATA_SHIFT_PIO && ata_id_is_cfa(dev->id))
2836 err_mask &= ~AC_ERR_DEV;
Alan Cox0bc2a792007-07-31 14:01:48 +01002837 /* Some very old devices and some bad newer ones fail any kind of
2838 SET_XFERMODE request but support PIO0-2 timings and no IORDY */
2839 if (dev->xfer_shift == ATA_SHIFT_PIO && !ata_id_has_iordy(dev->id) &&
2840 dev->pio_mode <= XFER_PIO_2)
2841 err_mask &= ~AC_ERR_DEV;
Tejun Heo83206a22006-03-24 15:25:31 +09002842 if (err_mask) {
Tejun Heof15a1da2006-05-15 20:57:56 +09002843 ata_dev_printk(dev, KERN_ERR, "failed to set xfermode "
2844 "(err_mask=0x%x)\n", err_mask);
Tejun Heo83206a22006-03-24 15:25:31 +09002845 return -EIO;
2846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847
Tejun Heobaa1e782006-11-01 18:39:27 +09002848 ehc->i.flags |= ATA_EHI_POST_SETMODE;
Tejun Heo422c9da2007-09-23 13:14:12 +09002849 rc = ata_dev_revalidate(dev, ATA_DEV_UNKNOWN, 0);
Tejun Heobaa1e782006-11-01 18:39:27 +09002850 ehc->i.flags &= ~ATA_EHI_POST_SETMODE;
Tejun Heo5eb45c02006-04-02 18:51:52 +09002851 if (rc)
Tejun Heo83206a22006-03-24 15:25:31 +09002852 return rc;
Tejun Heo48a8a142006-03-05 17:55:58 +09002853
Tejun Heo23e71c32006-03-06 04:31:57 +09002854 DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n",
2855 dev->xfer_shift, (int)dev->xfer_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856
Tejun Heof15a1da2006-05-15 20:57:56 +09002857 ata_dev_printk(dev, KERN_INFO, "configured for %s\n",
2858 ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)));
Tejun Heo83206a22006-03-24 15:25:31 +09002859 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002860}
2861
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862/**
Alan04351822007-03-06 02:37:52 -08002863 * ata_do_set_mode - Program timings and issue SET FEATURES - XFER
Tejun Heo02607312007-08-06 18:36:23 +09002864 * @link: link on which timings will be programmed
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002865 * @r_failed_dev: out paramter for failed device
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 *
Alan04351822007-03-06 02:37:52 -08002867 * Standard implementation of the function used to tune and set
2868 * ATA device disk transfer mode (PIO3, UDMA6, etc.). If
2869 * ata_dev_set_mode() fails, pointer to the failing device is
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002870 * returned in @r_failed_dev.
Jeff Garzik780a87f2005-05-30 15:41:05 -04002871 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002873 * PCI/etc. bus probe sem.
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002874 *
2875 * RETURNS:
2876 * 0 on success, negative errno otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 */
Alan04351822007-03-06 02:37:52 -08002878
Tejun Heo02607312007-08-06 18:36:23 +09002879int ata_do_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880{
Tejun Heo02607312007-08-06 18:36:23 +09002881 struct ata_port *ap = link->ap;
Tejun Heoe8e06192006-04-01 01:38:18 +09002882 struct ata_device *dev;
Tejun Heof58229f2007-08-06 18:36:23 +09002883 int rc = 0, used_dma = 0, found = 0;
Tejun Heo3adcebb2006-05-15 20:57:37 +09002884
Tejun Heoa6d5a512006-03-06 04:31:57 +09002885 /* step 1: calculate xfer_mask */
Tejun Heof58229f2007-08-06 18:36:23 +09002886 ata_link_for_each_dev(dev, link) {
Tejun Heoacf356b2006-03-24 14:07:50 +09002887 unsigned int pio_mask, dma_mask;
Tejun Heoa6d5a512006-03-06 04:31:57 +09002888
Tejun Heoe1211e32006-04-01 01:38:18 +09002889 if (!ata_dev_enabled(dev))
Tejun Heoa6d5a512006-03-06 04:31:57 +09002890 continue;
2891
Tejun Heo3373efd2006-05-15 20:57:53 +09002892 ata_dev_xfermask(dev);
Tejun Heoa6d5a512006-03-06 04:31:57 +09002893
Tejun Heoacf356b2006-03-24 14:07:50 +09002894 pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0);
2895 dma_mask = ata_pack_xfermask(0, dev->mwdma_mask, dev->udma_mask);
2896 dev->pio_mode = ata_xfer_mask2mode(pio_mask);
2897 dev->dma_mode = ata_xfer_mask2mode(dma_mask);
Alan Cox5444a6f2006-03-27 18:58:20 +01002898
Tejun Heo4f659772006-04-01 01:38:18 +09002899 found = 1;
Alan Cox5444a6f2006-03-27 18:58:20 +01002900 if (dev->dma_mode)
2901 used_dma = 1;
Tejun Heoa6d5a512006-03-06 04:31:57 +09002902 }
Tejun Heo4f659772006-04-01 01:38:18 +09002903 if (!found)
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002904 goto out;
Tejun Heoa6d5a512006-03-06 04:31:57 +09002905
2906 /* step 2: always set host PIO timings */
Tejun Heof58229f2007-08-06 18:36:23 +09002907 ata_link_for_each_dev(dev, link) {
Tejun Heoe8e06192006-04-01 01:38:18 +09002908 if (!ata_dev_enabled(dev))
2909 continue;
2910
2911 if (!dev->pio_mode) {
Tejun Heof15a1da2006-05-15 20:57:56 +09002912 ata_dev_printk(dev, KERN_WARNING, "no PIO support\n");
Tejun Heoe8e06192006-04-01 01:38:18 +09002913 rc = -EINVAL;
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002914 goto out;
Tejun Heoe8e06192006-04-01 01:38:18 +09002915 }
2916
2917 dev->xfer_mode = dev->pio_mode;
2918 dev->xfer_shift = ATA_SHIFT_PIO;
2919 if (ap->ops->set_piomode)
2920 ap->ops->set_piomode(ap, dev);
2921 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922
Tejun Heoa6d5a512006-03-06 04:31:57 +09002923 /* step 3: set host DMA timings */
Tejun Heof58229f2007-08-06 18:36:23 +09002924 ata_link_for_each_dev(dev, link) {
Tejun Heoe8e06192006-04-01 01:38:18 +09002925 if (!ata_dev_enabled(dev) || !dev->dma_mode)
2926 continue;
2927
2928 dev->xfer_mode = dev->dma_mode;
2929 dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
2930 if (ap->ops->set_dmamode)
2931 ap->ops->set_dmamode(ap, dev);
2932 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933
2934 /* step 4: update devices' xfer mode */
Tejun Heof58229f2007-08-06 18:36:23 +09002935 ata_link_for_each_dev(dev, link) {
Alan18d90de2007-01-24 11:42:38 +00002936 /* don't update suspended devices' xfer mode */
Tejun Heo9666f402007-05-04 21:27:47 +02002937 if (!ata_dev_enabled(dev))
Tejun Heo83206a22006-03-24 15:25:31 +09002938 continue;
2939
Tejun Heo3373efd2006-05-15 20:57:53 +09002940 rc = ata_dev_set_mode(dev);
Tejun Heo5bbc53f2006-04-01 01:38:17 +09002941 if (rc)
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002942 goto out;
Tejun Heo83206a22006-03-24 15:25:31 +09002943 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944
Tejun Heoe8e06192006-04-01 01:38:18 +09002945 /* Record simplex status. If we selected DMA then the other
2946 * host channels are not permitted to do so.
Alan Cox5444a6f2006-03-27 18:58:20 +01002947 */
Jeff Garzikcca39742006-08-24 03:19:22 -04002948 if (used_dma && (ap->host->flags & ATA_HOST_SIMPLEX))
Alan032af1c2007-03-01 17:36:46 +00002949 ap->host->simplex_claimed = ap;
Alan Cox5444a6f2006-03-27 18:58:20 +01002950
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002951 out:
2952 if (rc)
2953 *r_failed_dev = dev;
2954 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955}
2956
2957/**
Alan04351822007-03-06 02:37:52 -08002958 * ata_set_mode - Program timings and issue SET FEATURES - XFER
Tejun Heo02607312007-08-06 18:36:23 +09002959 * @link: link on which timings will be programmed
Alan04351822007-03-06 02:37:52 -08002960 * @r_failed_dev: out paramter for failed device
2961 *
2962 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.). If
2963 * ata_set_mode() fails, pointer to the failing device is
2964 * returned in @r_failed_dev.
2965 *
2966 * LOCKING:
2967 * PCI/etc. bus probe sem.
2968 *
2969 * RETURNS:
2970 * 0 on success, negative errno otherwise
2971 */
Tejun Heo02607312007-08-06 18:36:23 +09002972int ata_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
Alan04351822007-03-06 02:37:52 -08002973{
Tejun Heo02607312007-08-06 18:36:23 +09002974 struct ata_port *ap = link->ap;
2975
Alan04351822007-03-06 02:37:52 -08002976 /* has private set_mode? */
2977 if (ap->ops->set_mode)
Tejun Heo02607312007-08-06 18:36:23 +09002978 return ap->ops->set_mode(link, r_failed_dev);
2979 return ata_do_set_mode(link, r_failed_dev);
Alan04351822007-03-06 02:37:52 -08002980}
2981
2982/**
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002983 * ata_tf_to_host - issue ATA taskfile to host controller
2984 * @ap: port to which command is being issued
2985 * @tf: ATA taskfile register set
2986 *
2987 * Issues ATA taskfile register set to ATA host controller,
2988 * with proper synchronization with interrupt handler and
2989 * other threads.
2990 *
2991 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002992 * spin_lock_irqsave(host lock)
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002993 */
2994
2995static inline void ata_tf_to_host(struct ata_port *ap,
2996 const struct ata_taskfile *tf)
2997{
2998 ap->ops->tf_load(ap, tf);
2999 ap->ops->exec_command(ap, tf);
3000}
3001
3002/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 * ata_busy_sleep - sleep until BSY clears, or timeout
3004 * @ap: port containing status register to be polled
3005 * @tmout_pat: impatience timeout
3006 * @tmout: overall timeout
3007 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04003008 * Sleep until ATA Status register bit BSY clears,
3009 * or a timeout occurs.
3010 *
Tejun Heod1adc1b2006-10-09 18:32:15 +09003011 * LOCKING:
3012 * Kernel thread context (may sleep).
3013 *
3014 * RETURNS:
3015 * 0 on success, -errno otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 */
Tejun Heod1adc1b2006-10-09 18:32:15 +09003017int ata_busy_sleep(struct ata_port *ap,
3018 unsigned long tmout_pat, unsigned long tmout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019{
3020 unsigned long timer_start, timeout;
3021 u8 status;
3022
3023 status = ata_busy_wait(ap, ATA_BUSY, 300);
3024 timer_start = jiffies;
3025 timeout = timer_start + tmout_pat;
Tejun Heod1adc1b2006-10-09 18:32:15 +09003026 while (status != 0xff && (status & ATA_BUSY) &&
3027 time_before(jiffies, timeout)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 msleep(50);
3029 status = ata_busy_wait(ap, ATA_BUSY, 3);
3030 }
3031
Tejun Heod1adc1b2006-10-09 18:32:15 +09003032 if (status != 0xff && (status & ATA_BUSY))
Tejun Heof15a1da2006-05-15 20:57:56 +09003033 ata_port_printk(ap, KERN_WARNING,
Jeff Garzik35aa7a42006-09-28 06:50:24 -04003034 "port is slow to respond, please be patient "
3035 "(Status 0x%x)\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036
3037 timeout = timer_start + tmout;
Tejun Heod1adc1b2006-10-09 18:32:15 +09003038 while (status != 0xff && (status & ATA_BUSY) &&
3039 time_before(jiffies, timeout)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 msleep(50);
3041 status = ata_chk_status(ap);
3042 }
3043
Tejun Heod1adc1b2006-10-09 18:32:15 +09003044 if (status == 0xff)
3045 return -ENODEV;
3046
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 if (status & ATA_BUSY) {
Tejun Heof15a1da2006-05-15 20:57:56 +09003048 ata_port_printk(ap, KERN_ERR, "port failed to respond "
Jeff Garzik35aa7a42006-09-28 06:50:24 -04003049 "(%lu secs, Status 0x%x)\n",
3050 tmout / HZ, status);
Tejun Heod1adc1b2006-10-09 18:32:15 +09003051 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 }
3053
3054 return 0;
3055}
3056
Tejun Heod4b2bab2007-02-02 16:50:52 +09003057/**
3058 * ata_wait_ready - sleep until BSY clears, or timeout
3059 * @ap: port containing status register to be polled
3060 * @deadline: deadline jiffies for the operation
3061 *
3062 * Sleep until ATA Status register bit BSY clears, or timeout
3063 * occurs.
3064 *
3065 * LOCKING:
3066 * Kernel thread context (may sleep).
3067 *
3068 * RETURNS:
3069 * 0 on success, -errno otherwise.
3070 */
3071int ata_wait_ready(struct ata_port *ap, unsigned long deadline)
3072{
3073 unsigned long start = jiffies;
3074 int warned = 0;
3075
3076 while (1) {
3077 u8 status = ata_chk_status(ap);
3078 unsigned long now = jiffies;
3079
3080 if (!(status & ATA_BUSY))
3081 return 0;
Tejun Heo936fd732007-08-06 18:36:23 +09003082 if (!ata_link_online(&ap->link) && status == 0xff)
Tejun Heod4b2bab2007-02-02 16:50:52 +09003083 return -ENODEV;
3084 if (time_after(now, deadline))
3085 return -EBUSY;
3086
3087 if (!warned && time_after(now, start + 5 * HZ) &&
3088 (deadline - now > 3 * HZ)) {
3089 ata_port_printk(ap, KERN_WARNING,
3090 "port is slow to respond, please be patient "
3091 "(Status 0x%x)\n", status);
3092 warned = 1;
3093 }
3094
3095 msleep(50);
3096 }
3097}
3098
3099static int ata_bus_post_reset(struct ata_port *ap, unsigned int devmask,
3100 unsigned long deadline)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101{
3102 struct ata_ioports *ioaddr = &ap->ioaddr;
3103 unsigned int dev0 = devmask & (1 << 0);
3104 unsigned int dev1 = devmask & (1 << 1);
Tejun Heo9b893912007-02-02 16:50:52 +09003105 int rc, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106
3107 /* if device 0 was found in ata_devchk, wait for its
3108 * BSY bit to clear
3109 */
Tejun Heod4b2bab2007-02-02 16:50:52 +09003110 if (dev0) {
3111 rc = ata_wait_ready(ap, deadline);
Tejun Heo9b893912007-02-02 16:50:52 +09003112 if (rc) {
3113 if (rc != -ENODEV)
3114 return rc;
3115 ret = rc;
3116 }
Tejun Heod4b2bab2007-02-02 16:50:52 +09003117 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118
Tejun Heoe141d992007-06-10 14:26:20 +09003119 /* if device 1 was found in ata_devchk, wait for register
3120 * access briefly, then wait for BSY to clear.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 */
Tejun Heoe141d992007-06-10 14:26:20 +09003122 if (dev1) {
3123 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124
3125 ap->ops->dev_select(ap, 1);
Tejun Heoe141d992007-06-10 14:26:20 +09003126
3127 /* Wait for register access. Some ATAPI devices fail
3128 * to set nsect/lbal after reset, so don't waste too
3129 * much time on it. We're gonna wait for !BSY anyway.
3130 */
3131 for (i = 0; i < 2; i++) {
3132 u8 nsect, lbal;
3133
3134 nsect = ioread8(ioaddr->nsect_addr);
3135 lbal = ioread8(ioaddr->lbal_addr);
3136 if ((nsect == 1) && (lbal == 1))
3137 break;
3138 msleep(50); /* give drive a breather */
3139 }
3140
Tejun Heod4b2bab2007-02-02 16:50:52 +09003141 rc = ata_wait_ready(ap, deadline);
Tejun Heo9b893912007-02-02 16:50:52 +09003142 if (rc) {
3143 if (rc != -ENODEV)
3144 return rc;
3145 ret = rc;
3146 }
Tejun Heod4b2bab2007-02-02 16:50:52 +09003147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148
3149 /* is all this really necessary? */
3150 ap->ops->dev_select(ap, 0);
3151 if (dev1)
3152 ap->ops->dev_select(ap, 1);
3153 if (dev0)
3154 ap->ops->dev_select(ap, 0);
Tejun Heod4b2bab2007-02-02 16:50:52 +09003155
Tejun Heo9b893912007-02-02 16:50:52 +09003156 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157}
3158
Tejun Heod4b2bab2007-02-02 16:50:52 +09003159static int ata_bus_softreset(struct ata_port *ap, unsigned int devmask,
3160 unsigned long deadline)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161{
3162 struct ata_ioports *ioaddr = &ap->ioaddr;
3163
Tejun Heo44877b42007-02-21 01:06:51 +09003164 DPRINTK("ata%u: bus reset via SRST\n", ap->print_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165
3166 /* software reset. causes dev0 to be selected */
Tejun Heo0d5ff562007-02-01 15:06:36 +09003167 iowrite8(ap->ctl, ioaddr->ctl_addr);
3168 udelay(20); /* FIXME: flush */
3169 iowrite8(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
3170 udelay(20); /* FIXME: flush */
3171 iowrite8(ap->ctl, ioaddr->ctl_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172
3173 /* spec mandates ">= 2ms" before checking status.
3174 * We wait 150ms, because that was the magic delay used for
3175 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
3176 * between when the ATA command register is written, and then
3177 * status is checked. Because waiting for "a while" before
3178 * checking status is fine, post SRST, we perform this magic
3179 * delay here as well.
Alan Cox09c7ad72006-03-22 15:52:40 +00003180 *
3181 * Old drivers/ide uses the 2mS rule and then waits for ready
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 */
3183 msleep(150);
3184
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05003185 /* Before we perform post reset processing we want to see if
Tejun Heo298a41c2006-03-25 02:58:13 +09003186 * the bus shows 0xFF because the odd clown forgets the D7
3187 * pulldown resistor.
3188 */
Tejun Heod1adc1b2006-10-09 18:32:15 +09003189 if (ata_check_status(ap) == 0xFF)
Tejun Heo9b893912007-02-02 16:50:52 +09003190 return -ENODEV;
Alan Cox09c7ad72006-03-22 15:52:40 +00003191
Tejun Heod4b2bab2007-02-02 16:50:52 +09003192 return ata_bus_post_reset(ap, devmask, deadline);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193}
3194
3195/**
3196 * ata_bus_reset - reset host port and associated ATA channel
3197 * @ap: port to reset
3198 *
3199 * This is typically the first time we actually start issuing
3200 * commands to the ATA channel. We wait for BSY to clear, then
3201 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
3202 * result. Determine what devices, if any, are on the channel
3203 * by looking at the device 0/1 error register. Look at the signature
3204 * stored in each device's taskfile registers, to determine if
3205 * the device is ATA or ATAPI.
3206 *
3207 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003208 * PCI/etc. bus probe sem.
Jeff Garzikcca39742006-08-24 03:19:22 -04003209 * Obtains host lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210 *
3211 * SIDE EFFECTS:
Tejun Heo198e0fe2006-04-02 18:51:52 +09003212 * Sets ATA_FLAG_DISABLED if bus reset fails.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 */
3214
3215void ata_bus_reset(struct ata_port *ap)
3216{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09003217 struct ata_device *device = ap->link.device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 struct ata_ioports *ioaddr = &ap->ioaddr;
3219 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
3220 u8 err;
Tejun Heoaec5c3c2006-03-25 01:33:34 +09003221 unsigned int dev0, dev1 = 0, devmask = 0;
Tejun Heo9b893912007-02-02 16:50:52 +09003222 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003223
Tejun Heo44877b42007-02-21 01:06:51 +09003224 DPRINTK("ENTER, host %u, port %u\n", ap->print_id, ap->port_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225
3226 /* determine if device 0/1 are present */
3227 if (ap->flags & ATA_FLAG_SATA_RESET)
3228 dev0 = 1;
3229 else {
3230 dev0 = ata_devchk(ap, 0);
3231 if (slave_possible)
3232 dev1 = ata_devchk(ap, 1);
3233 }
3234
3235 if (dev0)
3236 devmask |= (1 << 0);
3237 if (dev1)
3238 devmask |= (1 << 1);
3239
3240 /* select device 0 again */
3241 ap->ops->dev_select(ap, 0);
3242
3243 /* issue bus reset */
Tejun Heo9b893912007-02-02 16:50:52 +09003244 if (ap->flags & ATA_FLAG_SRST) {
3245 rc = ata_bus_softreset(ap, devmask, jiffies + 40 * HZ);
3246 if (rc && rc != -ENODEV)
Tejun Heoaec5c3c2006-03-25 01:33:34 +09003247 goto err_out;
Tejun Heo9b893912007-02-02 16:50:52 +09003248 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249
3250 /*
3251 * determine by signature whether we have ATA or ATAPI devices
3252 */
Tejun Heo3f198592007-09-02 23:23:57 +09003253 device[0].class = ata_dev_try_classify(&device[0], dev0, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003254 if ((slave_possible) && (err != 0x81))
Tejun Heo3f198592007-09-02 23:23:57 +09003255 device[1].class = ata_dev_try_classify(&device[1], dev1, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257 /* is double-select really necessary? */
Tejun Heo9af5c9c2007-08-06 18:36:22 +09003258 if (device[1].class != ATA_DEV_NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003259 ap->ops->dev_select(ap, 1);
Tejun Heo9af5c9c2007-08-06 18:36:22 +09003260 if (device[0].class != ATA_DEV_NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261 ap->ops->dev_select(ap, 0);
3262
3263 /* if no devices were detected, disable this port */
Tejun Heo9af5c9c2007-08-06 18:36:22 +09003264 if ((device[0].class == ATA_DEV_NONE) &&
3265 (device[1].class == ATA_DEV_NONE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 goto err_out;
3267
3268 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
3269 /* set up device control for ATA_FLAG_SATA_RESET */
Tejun Heo0d5ff562007-02-01 15:06:36 +09003270 iowrite8(ap->ctl, ioaddr->ctl_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 }
3272
3273 DPRINTK("EXIT\n");
3274 return;
3275
3276err_out:
Tejun Heof15a1da2006-05-15 20:57:56 +09003277 ata_port_printk(ap, KERN_ERR, "disabling port\n");
Jeff Garzikac8869d2007-08-16 03:17:03 -04003278 ata_port_disable(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279
3280 DPRINTK("EXIT\n");
3281}
3282
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003283/**
Tejun Heo936fd732007-08-06 18:36:23 +09003284 * sata_link_debounce - debounce SATA phy status
3285 * @link: ATA link to debounce SATA phy status for
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003286 * @params: timing parameters { interval, duratinon, timeout } in msec
Tejun Heod4b2bab2007-02-02 16:50:52 +09003287 * @deadline: deadline jiffies for the operation
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003288 *
Tejun Heo936fd732007-08-06 18:36:23 +09003289* Make sure SStatus of @link reaches stable state, determined by
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003290 * holding the same value where DET is not 1 for @duration polled
3291 * every @interval, before @timeout. Timeout constraints the
Tejun Heod4b2bab2007-02-02 16:50:52 +09003292 * beginning of the stable state. Because DET gets stuck at 1 on
3293 * some controllers after hot unplugging, this functions waits
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003294 * until timeout then returns 0 if DET is stable at 1.
3295 *
Tejun Heod4b2bab2007-02-02 16:50:52 +09003296 * @timeout is further limited by @deadline. The sooner of the
3297 * two is used.
3298 *
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003299 * LOCKING:
3300 * Kernel thread context (may sleep)
3301 *
3302 * RETURNS:
3303 * 0 on success, -errno on failure.
3304 */
Tejun Heo936fd732007-08-06 18:36:23 +09003305int sata_link_debounce(struct ata_link *link, const unsigned long *params,
3306 unsigned long deadline)
Tejun Heo7a7921e2006-02-02 18:20:00 +09003307{
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003308 unsigned long interval_msec = params[0];
Tejun Heod4b2bab2007-02-02 16:50:52 +09003309 unsigned long duration = msecs_to_jiffies(params[1]);
3310 unsigned long last_jiffies, t;
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003311 u32 last, cur;
3312 int rc;
3313
Tejun Heod4b2bab2007-02-02 16:50:52 +09003314 t = jiffies + msecs_to_jiffies(params[2]);
3315 if (time_before(t, deadline))
3316 deadline = t;
3317
Tejun Heo936fd732007-08-06 18:36:23 +09003318 if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003319 return rc;
3320 cur &= 0xf;
3321
3322 last = cur;
3323 last_jiffies = jiffies;
3324
3325 while (1) {
3326 msleep(interval_msec);
Tejun Heo936fd732007-08-06 18:36:23 +09003327 if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003328 return rc;
3329 cur &= 0xf;
3330
3331 /* DET stable? */
3332 if (cur == last) {
Tejun Heod4b2bab2007-02-02 16:50:52 +09003333 if (cur == 1 && time_before(jiffies, deadline))
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003334 continue;
3335 if (time_after(jiffies, last_jiffies + duration))
3336 return 0;
3337 continue;
3338 }
3339
3340 /* unstable, start over */
3341 last = cur;
3342 last_jiffies = jiffies;
3343
Tejun Heof1545152007-07-16 14:29:40 +09003344 /* Check deadline. If debouncing failed, return
3345 * -EPIPE to tell upper layer to lower link speed.
3346 */
Tejun Heod4b2bab2007-02-02 16:50:52 +09003347 if (time_after(jiffies, deadline))
Tejun Heof1545152007-07-16 14:29:40 +09003348 return -EPIPE;
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003349 }
3350}
3351
3352/**
Tejun Heo936fd732007-08-06 18:36:23 +09003353 * sata_link_resume - resume SATA link
3354 * @link: ATA link to resume SATA
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003355 * @params: timing parameters { interval, duratinon, timeout } in msec
Tejun Heod4b2bab2007-02-02 16:50:52 +09003356 * @deadline: deadline jiffies for the operation
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003357 *
Tejun Heo936fd732007-08-06 18:36:23 +09003358 * Resume SATA phy @link and debounce it.
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003359 *
3360 * LOCKING:
3361 * Kernel thread context (may sleep)
3362 *
3363 * RETURNS:
3364 * 0 on success, -errno on failure.
3365 */
Tejun Heo936fd732007-08-06 18:36:23 +09003366int sata_link_resume(struct ata_link *link, const unsigned long *params,
3367 unsigned long deadline)
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003368{
3369 u32 scontrol;
Tejun Heo81952c52006-05-15 20:57:47 +09003370 int rc;
Tejun Heo7a7921e2006-02-02 18:20:00 +09003371
Tejun Heo936fd732007-08-06 18:36:23 +09003372 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
Tejun Heo81952c52006-05-15 20:57:47 +09003373 return rc;
3374
Tejun Heo852ee162006-04-01 01:38:18 +09003375 scontrol = (scontrol & 0x0f0) | 0x300;
Tejun Heo81952c52006-05-15 20:57:47 +09003376
Tejun Heo936fd732007-08-06 18:36:23 +09003377 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
Tejun Heo81952c52006-05-15 20:57:47 +09003378 return rc;
Tejun Heo7a7921e2006-02-02 18:20:00 +09003379
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003380 /* Some PHYs react badly if SStatus is pounded immediately
3381 * after resuming. Delay 200ms before debouncing.
3382 */
3383 msleep(200);
Tejun Heo7a7921e2006-02-02 18:20:00 +09003384
Tejun Heo936fd732007-08-06 18:36:23 +09003385 return sata_link_debounce(link, params, deadline);
Tejun Heo7a7921e2006-02-02 18:20:00 +09003386}
3387
Tejun Heof5914a42006-05-31 18:27:48 +09003388/**
3389 * ata_std_prereset - prepare for reset
Tejun Heocc0680a2007-08-06 18:36:23 +09003390 * @link: ATA link to be reset
Tejun Heod4b2bab2007-02-02 16:50:52 +09003391 * @deadline: deadline jiffies for the operation
Tejun Heof5914a42006-05-31 18:27:48 +09003392 *
Tejun Heocc0680a2007-08-06 18:36:23 +09003393 * @link is about to be reset. Initialize it. Failure from
Tejun Heob8cffc62007-02-02 16:50:52 +09003394 * prereset makes libata abort whole reset sequence and give up
3395 * that port, so prereset should be best-effort. It does its
3396 * best to prepare for reset sequence but if things go wrong, it
3397 * should just whine, not fail.
Tejun Heof5914a42006-05-31 18:27:48 +09003398 *
3399 * LOCKING:
3400 * Kernel thread context (may sleep)
3401 *
3402 * RETURNS:
3403 * 0 on success, -errno otherwise.
3404 */
Tejun Heocc0680a2007-08-06 18:36:23 +09003405int ata_std_prereset(struct ata_link *link, unsigned long deadline)
Tejun Heof5914a42006-05-31 18:27:48 +09003406{
Tejun Heocc0680a2007-08-06 18:36:23 +09003407 struct ata_port *ap = link->ap;
Tejun Heo936fd732007-08-06 18:36:23 +09003408 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heoe9c83912006-07-03 16:07:26 +09003409 const unsigned long *timing = sata_ehc_deb_timing(ehc);
Tejun Heof5914a42006-05-31 18:27:48 +09003410 int rc;
3411
Tejun Heo31daabd2007-02-02 16:50:52 +09003412 /* handle link resume */
Tejun Heo28324302006-07-03 16:07:26 +09003413 if ((ehc->i.flags & ATA_EHI_RESUME_LINK) &&
Tejun Heo0c887582007-08-06 18:36:23 +09003414 (link->flags & ATA_LFLAG_HRST_TO_RESUME))
Tejun Heo28324302006-07-03 16:07:26 +09003415 ehc->i.action |= ATA_EH_HARDRESET;
3416
Tejun Heof5914a42006-05-31 18:27:48 +09003417 /* if we're about to do hardreset, nothing more to do */
3418 if (ehc->i.action & ATA_EH_HARDRESET)
3419 return 0;
3420
Tejun Heo936fd732007-08-06 18:36:23 +09003421 /* if SATA, resume link */
Tejun Heoa16abc02007-05-21 18:33:47 +02003422 if (ap->flags & ATA_FLAG_SATA) {
Tejun Heo936fd732007-08-06 18:36:23 +09003423 rc = sata_link_resume(link, timing, deadline);
Tejun Heob8cffc62007-02-02 16:50:52 +09003424 /* whine about phy resume failure but proceed */
3425 if (rc && rc != -EOPNOTSUPP)
Tejun Heocc0680a2007-08-06 18:36:23 +09003426 ata_link_printk(link, KERN_WARNING, "failed to resume "
Tejun Heof5914a42006-05-31 18:27:48 +09003427 "link for reset (errno=%d)\n", rc);
Tejun Heof5914a42006-05-31 18:27:48 +09003428 }
3429
3430 /* Wait for !BSY if the controller can wait for the first D2H
3431 * Reg FIS and we don't know that no device is attached.
3432 */
Tejun Heo0c887582007-08-06 18:36:23 +09003433 if (!(link->flags & ATA_LFLAG_SKIP_D2H_BSY) && !ata_link_offline(link)) {
Tejun Heob8cffc62007-02-02 16:50:52 +09003434 rc = ata_wait_ready(ap, deadline);
Tejun Heo6dffaf62007-05-23 11:58:52 +02003435 if (rc && rc != -ENODEV) {
Tejun Heocc0680a2007-08-06 18:36:23 +09003436 ata_link_printk(link, KERN_WARNING, "device not ready "
Tejun Heob8cffc62007-02-02 16:50:52 +09003437 "(errno=%d), forcing hardreset\n", rc);
3438 ehc->i.action |= ATA_EH_HARDRESET;
3439 }
3440 }
Tejun Heof5914a42006-05-31 18:27:48 +09003441
3442 return 0;
3443}
3444
Tejun Heoc2bd5802006-01-24 17:05:22 +09003445/**
3446 * ata_std_softreset - reset host port via ATA SRST
Tejun Heocc0680a2007-08-06 18:36:23 +09003447 * @link: ATA link to reset
Tejun Heoc2bd5802006-01-24 17:05:22 +09003448 * @classes: resulting classes of attached devices
Tejun Heod4b2bab2007-02-02 16:50:52 +09003449 * @deadline: deadline jiffies for the operation
Tejun Heoc2bd5802006-01-24 17:05:22 +09003450 *
Tejun Heo52783c52006-05-31 18:28:22 +09003451 * Reset host port using ATA SRST.
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 ata_std_softreset(struct ata_link *link, unsigned int *classes,
Tejun Heod4b2bab2007-02-02 16:50:52 +09003460 unsigned long deadline)
Tejun Heoc2bd5802006-01-24 17:05:22 +09003461{
Tejun Heocc0680a2007-08-06 18:36:23 +09003462 struct ata_port *ap = link->ap;
Tejun Heoc2bd5802006-01-24 17:05:22 +09003463 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
Tejun Heod4b2bab2007-02-02 16:50:52 +09003464 unsigned int devmask = 0;
3465 int rc;
Tejun Heoc2bd5802006-01-24 17:05:22 +09003466 u8 err;
3467
3468 DPRINTK("ENTER\n");
3469
Tejun Heo936fd732007-08-06 18:36:23 +09003470 if (ata_link_offline(link)) {
Tejun Heo3a397462006-02-10 23:58:48 +09003471 classes[0] = ATA_DEV_NONE;
3472 goto out;
3473 }
3474
Tejun Heoc2bd5802006-01-24 17:05:22 +09003475 /* determine if device 0/1 are present */
3476 if (ata_devchk(ap, 0))
3477 devmask |= (1 << 0);
3478 if (slave_possible && ata_devchk(ap, 1))
3479 devmask |= (1 << 1);
3480
Tejun Heoc2bd5802006-01-24 17:05:22 +09003481 /* select device 0 again */
3482 ap->ops->dev_select(ap, 0);
3483
3484 /* issue bus reset */
3485 DPRINTK("about to softreset, devmask=%x\n", devmask);
Tejun Heod4b2bab2007-02-02 16:50:52 +09003486 rc = ata_bus_softreset(ap, devmask, deadline);
Tejun Heo9b893912007-02-02 16:50:52 +09003487 /* if link is occupied, -ENODEV too is an error */
Tejun Heo936fd732007-08-06 18:36:23 +09003488 if (rc && (rc != -ENODEV || sata_scr_valid(link))) {
Tejun Heocc0680a2007-08-06 18:36:23 +09003489 ata_link_printk(link, KERN_ERR, "SRST failed (errno=%d)\n", rc);
Tejun Heod4b2bab2007-02-02 16:50:52 +09003490 return rc;
Tejun Heoc2bd5802006-01-24 17:05:22 +09003491 }
3492
3493 /* determine by signature whether we have ATA or ATAPI devices */
Tejun Heo3f198592007-09-02 23:23:57 +09003494 classes[0] = ata_dev_try_classify(&link->device[0],
3495 devmask & (1 << 0), &err);
Tejun Heoc2bd5802006-01-24 17:05:22 +09003496 if (slave_possible && err != 0x81)
Tejun Heo3f198592007-09-02 23:23:57 +09003497 classes[1] = ata_dev_try_classify(&link->device[1],
3498 devmask & (1 << 1), &err);
Tejun Heoc2bd5802006-01-24 17:05:22 +09003499
Tejun Heo3a397462006-02-10 23:58:48 +09003500 out:
Tejun Heoc2bd5802006-01-24 17:05:22 +09003501 DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]);
3502 return 0;
3503}
3504
3505/**
Tejun Heocc0680a2007-08-06 18:36:23 +09003506 * sata_link_hardreset - reset link via SATA phy reset
3507 * @link: link to reset
Tejun Heob6103f62006-11-01 17:59:53 +09003508 * @timing: timing parameters { interval, duratinon, timeout } in msec
Tejun Heod4b2bab2007-02-02 16:50:52 +09003509 * @deadline: deadline jiffies for the operation
Tejun Heoc2bd5802006-01-24 17:05:22 +09003510 *
Tejun Heocc0680a2007-08-06 18:36:23 +09003511 * SATA phy-reset @link using DET bits of SControl register.
Tejun Heoc2bd5802006-01-24 17:05:22 +09003512 *
3513 * LOCKING:
3514 * Kernel thread context (may sleep)
3515 *
3516 * RETURNS:
3517 * 0 on success, -errno otherwise.
3518 */
Tejun Heocc0680a2007-08-06 18:36:23 +09003519int sata_link_hardreset(struct ata_link *link, const unsigned long *timing,
Tejun Heod4b2bab2007-02-02 16:50:52 +09003520 unsigned long deadline)
Tejun Heoc2bd5802006-01-24 17:05:22 +09003521{
Tejun Heo852ee162006-04-01 01:38:18 +09003522 u32 scontrol;
Tejun Heo81952c52006-05-15 20:57:47 +09003523 int rc;
Tejun Heo852ee162006-04-01 01:38:18 +09003524
Tejun Heoc2bd5802006-01-24 17:05:22 +09003525 DPRINTK("ENTER\n");
3526
Tejun Heo936fd732007-08-06 18:36:23 +09003527 if (sata_set_spd_needed(link)) {
Tejun Heo1c3fae42006-04-02 20:53:28 +09003528 /* SATA spec says nothing about how to reconfigure
3529 * spd. To be on the safe side, turn off phy during
3530 * reconfiguration. This works for at least ICH7 AHCI
3531 * and Sil3124.
3532 */
Tejun Heo936fd732007-08-06 18:36:23 +09003533 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
Tejun Heob6103f62006-11-01 17:59:53 +09003534 goto out;
Tejun Heo81952c52006-05-15 20:57:47 +09003535
Martin Hicksa34b6fc2006-07-05 15:06:13 -04003536 scontrol = (scontrol & 0x0f0) | 0x304;
Tejun Heo81952c52006-05-15 20:57:47 +09003537
Tejun Heo936fd732007-08-06 18:36:23 +09003538 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
Tejun Heob6103f62006-11-01 17:59:53 +09003539 goto out;
Tejun Heo1c3fae42006-04-02 20:53:28 +09003540
Tejun Heo936fd732007-08-06 18:36:23 +09003541 sata_set_spd(link);
Tejun Heo1c3fae42006-04-02 20:53:28 +09003542 }
3543
3544 /* issue phy wake/reset */
Tejun Heo936fd732007-08-06 18:36:23 +09003545 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
Tejun Heob6103f62006-11-01 17:59:53 +09003546 goto out;
Tejun Heo81952c52006-05-15 20:57:47 +09003547
Tejun Heo852ee162006-04-01 01:38:18 +09003548 scontrol = (scontrol & 0x0f0) | 0x301;
Tejun Heo81952c52006-05-15 20:57:47 +09003549
Tejun Heo936fd732007-08-06 18:36:23 +09003550 if ((rc = sata_scr_write_flush(link, SCR_CONTROL, scontrol)))
Tejun Heob6103f62006-11-01 17:59:53 +09003551 goto out;
Tejun Heoc2bd5802006-01-24 17:05:22 +09003552
Tejun Heo1c3fae42006-04-02 20:53:28 +09003553 /* Couldn't find anything in SATA I/II specs, but AHCI-1.1
Tejun Heoc2bd5802006-01-24 17:05:22 +09003554 * 10.4.2 says at least 1 ms.
3555 */
3556 msleep(1);
3557
Tejun Heo936fd732007-08-06 18:36:23 +09003558 /* bring link back */
3559 rc = sata_link_resume(link, timing, deadline);
Tejun Heob6103f62006-11-01 17:59:53 +09003560 out:
3561 DPRINTK("EXIT, rc=%d\n", rc);
3562 return rc;
3563}
3564
3565/**
3566 * sata_std_hardreset - reset host port via SATA phy reset
Tejun Heocc0680a2007-08-06 18:36:23 +09003567 * @link: link to reset
Tejun Heob6103f62006-11-01 17:59:53 +09003568 * @class: resulting class of attached device
Tejun Heod4b2bab2007-02-02 16:50:52 +09003569 * @deadline: deadline jiffies for the operation
Tejun Heob6103f62006-11-01 17:59:53 +09003570 *
3571 * SATA phy-reset host port using DET bits of SControl register,
3572 * wait for !BSY and classify the attached device.
3573 *
3574 * LOCKING:
3575 * Kernel thread context (may sleep)
3576 *
3577 * RETURNS:
3578 * 0 on success, -errno otherwise.
3579 */
Tejun Heocc0680a2007-08-06 18:36:23 +09003580int sata_std_hardreset(struct ata_link *link, unsigned int *class,
Tejun Heod4b2bab2007-02-02 16:50:52 +09003581 unsigned long deadline)
Tejun Heob6103f62006-11-01 17:59:53 +09003582{
Tejun Heocc0680a2007-08-06 18:36:23 +09003583 struct ata_port *ap = link->ap;
Tejun Heo936fd732007-08-06 18:36:23 +09003584 const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
Tejun Heob6103f62006-11-01 17:59:53 +09003585 int rc;
3586
3587 DPRINTK("ENTER\n");
3588
3589 /* do hardreset */
Tejun Heocc0680a2007-08-06 18:36:23 +09003590 rc = sata_link_hardreset(link, timing, deadline);
Tejun Heob6103f62006-11-01 17:59:53 +09003591 if (rc) {
Tejun Heocc0680a2007-08-06 18:36:23 +09003592 ata_link_printk(link, KERN_ERR,
Tejun Heob6103f62006-11-01 17:59:53 +09003593 "COMRESET failed (errno=%d)\n", rc);
3594 return rc;
3595 }
Tejun Heoc2bd5802006-01-24 17:05:22 +09003596
Tejun Heoc2bd5802006-01-24 17:05:22 +09003597 /* TODO: phy layer with polling, timeouts, etc. */
Tejun Heo936fd732007-08-06 18:36:23 +09003598 if (ata_link_offline(link)) {
Tejun Heoc2bd5802006-01-24 17:05:22 +09003599 *class = ATA_DEV_NONE;
3600 DPRINTK("EXIT, link offline\n");
3601 return 0;
3602 }
3603
Tejun Heo34fee222007-02-02 15:29:27 +09003604 /* wait a while before checking status, see SRST for more info */
3605 msleep(150);
3606
Tejun Heod4b2bab2007-02-02 16:50:52 +09003607 rc = ata_wait_ready(ap, deadline);
Tejun Heo9b893912007-02-02 16:50:52 +09003608 /* link occupied, -ENODEV too is an error */
3609 if (rc) {
Tejun Heocc0680a2007-08-06 18:36:23 +09003610 ata_link_printk(link, KERN_ERR,
Tejun Heod4b2bab2007-02-02 16:50:52 +09003611 "COMRESET failed (errno=%d)\n", rc);
3612 return rc;
Tejun Heoc2bd5802006-01-24 17:05:22 +09003613 }
3614
Tejun Heo3a397462006-02-10 23:58:48 +09003615 ap->ops->dev_select(ap, 0); /* probably unnecessary */
3616
Tejun Heo3f198592007-09-02 23:23:57 +09003617 *class = ata_dev_try_classify(link->device, 1, NULL);
Tejun Heoc2bd5802006-01-24 17:05:22 +09003618
3619 DPRINTK("EXIT, class=%u\n", *class);
3620 return 0;
3621}
3622
3623/**
3624 * ata_std_postreset - standard postreset callback
Tejun Heocc0680a2007-08-06 18:36:23 +09003625 * @link: the target ata_link
Tejun Heoc2bd5802006-01-24 17:05:22 +09003626 * @classes: classes of attached devices
3627 *
3628 * This function is invoked after a successful reset. Note that
3629 * the device might have been reset more than once using
3630 * different reset methods before postreset is invoked.
Tejun Heoc2bd5802006-01-24 17:05:22 +09003631 *
Tejun Heoc2bd5802006-01-24 17:05:22 +09003632 * LOCKING:
3633 * Kernel thread context (may sleep)
3634 */
Tejun Heocc0680a2007-08-06 18:36:23 +09003635void ata_std_postreset(struct ata_link *link, unsigned int *classes)
Tejun Heoc2bd5802006-01-24 17:05:22 +09003636{
Tejun Heocc0680a2007-08-06 18:36:23 +09003637 struct ata_port *ap = link->ap;
Tejun Heodc2b3512006-05-15 20:58:00 +09003638 u32 serror;
3639
Tejun Heoc2bd5802006-01-24 17:05:22 +09003640 DPRINTK("ENTER\n");
3641
Tejun Heoc2bd5802006-01-24 17:05:22 +09003642 /* print link status */
Tejun Heo936fd732007-08-06 18:36:23 +09003643 sata_print_link_status(link);
Tejun Heoc2bd5802006-01-24 17:05:22 +09003644
Tejun Heodc2b3512006-05-15 20:58:00 +09003645 /* clear SError */
Tejun Heo936fd732007-08-06 18:36:23 +09003646 if (sata_scr_read(link, SCR_ERROR, &serror) == 0)
3647 sata_scr_write(link, SCR_ERROR, serror);
Tejun Heodc2b3512006-05-15 20:58:00 +09003648
Tejun Heoc2bd5802006-01-24 17:05:22 +09003649 /* is double-select really necessary? */
3650 if (classes[0] != ATA_DEV_NONE)
3651 ap->ops->dev_select(ap, 1);
3652 if (classes[1] != ATA_DEV_NONE)
3653 ap->ops->dev_select(ap, 0);
3654
Tejun Heo3a397462006-02-10 23:58:48 +09003655 /* bail out if no device is present */
3656 if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
3657 DPRINTK("EXIT, no device\n");
3658 return;
3659 }
3660
3661 /* set up device control */
Tejun Heo0d5ff562007-02-01 15:06:36 +09003662 if (ap->ioaddr.ctl_addr)
3663 iowrite8(ap->ctl, ap->ioaddr.ctl_addr);
Tejun Heoc2bd5802006-01-24 17:05:22 +09003664
3665 DPRINTK("EXIT\n");
3666}
3667
Tejun Heoa62c0fc2006-01-24 17:05:22 +09003668/**
Tejun Heo623a3122006-03-05 17:55:58 +09003669 * ata_dev_same_device - Determine whether new ID matches configured device
Tejun Heo623a3122006-03-05 17:55:58 +09003670 * @dev: device to compare against
3671 * @new_class: class of the new device
3672 * @new_id: IDENTIFY page of the new device
3673 *
3674 * Compare @new_class and @new_id against @dev and determine
3675 * whether @dev is the device indicated by @new_class and
3676 * @new_id.
3677 *
3678 * LOCKING:
3679 * None.
3680 *
3681 * RETURNS:
3682 * 1 if @dev matches @new_class and @new_id, 0 otherwise.
3683 */
Tejun Heo3373efd2006-05-15 20:57:53 +09003684static int ata_dev_same_device(struct ata_device *dev, unsigned int new_class,
3685 const u16 *new_id)
Tejun Heo623a3122006-03-05 17:55:58 +09003686{
3687 const u16 *old_id = dev->id;
Tejun Heoa0cf7332007-01-02 20:18:49 +09003688 unsigned char model[2][ATA_ID_PROD_LEN + 1];
3689 unsigned char serial[2][ATA_ID_SERNO_LEN + 1];
Tejun Heo623a3122006-03-05 17:55:58 +09003690
3691 if (dev->class != new_class) {
Tejun Heof15a1da2006-05-15 20:57:56 +09003692 ata_dev_printk(dev, KERN_INFO, "class mismatch %d != %d\n",
3693 dev->class, new_class);
Tejun Heo623a3122006-03-05 17:55:58 +09003694 return 0;
3695 }
3696
Tejun Heoa0cf7332007-01-02 20:18:49 +09003697 ata_id_c_string(old_id, model[0], ATA_ID_PROD, sizeof(model[0]));
3698 ata_id_c_string(new_id, model[1], ATA_ID_PROD, sizeof(model[1]));
3699 ata_id_c_string(old_id, serial[0], ATA_ID_SERNO, sizeof(serial[0]));
3700 ata_id_c_string(new_id, serial[1], ATA_ID_SERNO, sizeof(serial[1]));
Tejun Heo623a3122006-03-05 17:55:58 +09003701
3702 if (strcmp(model[0], model[1])) {
Tejun Heof15a1da2006-05-15 20:57:56 +09003703 ata_dev_printk(dev, KERN_INFO, "model number mismatch "
3704 "'%s' != '%s'\n", model[0], model[1]);
Tejun Heo623a3122006-03-05 17:55:58 +09003705 return 0;
3706 }
3707
3708 if (strcmp(serial[0], serial[1])) {
Tejun Heof15a1da2006-05-15 20:57:56 +09003709 ata_dev_printk(dev, KERN_INFO, "serial number mismatch "
3710 "'%s' != '%s'\n", serial[0], serial[1]);
Tejun Heo623a3122006-03-05 17:55:58 +09003711 return 0;
3712 }
3713
Tejun Heo623a3122006-03-05 17:55:58 +09003714 return 1;
3715}
3716
3717/**
Tejun Heofe309112007-05-15 03:28:15 +09003718 * ata_dev_reread_id - Re-read IDENTIFY data
Henrik Kretzschmar3fae4502007-06-19 10:10:50 +02003719 * @dev: target ATA device
Tejun Heobff04642006-11-10 18:08:10 +09003720 * @readid_flags: read ID flags
Tejun Heo623a3122006-03-05 17:55:58 +09003721 *
3722 * Re-read IDENTIFY page and make sure @dev is still attached to
3723 * the port.
3724 *
3725 * LOCKING:
3726 * Kernel thread context (may sleep)
3727 *
3728 * RETURNS:
3729 * 0 on success, negative errno otherwise
3730 */
Tejun Heofe309112007-05-15 03:28:15 +09003731int ata_dev_reread_id(struct ata_device *dev, unsigned int readid_flags)
Tejun Heo623a3122006-03-05 17:55:58 +09003732{
Tejun Heo5eb45c02006-04-02 18:51:52 +09003733 unsigned int class = dev->class;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09003734 u16 *id = (void *)dev->link->ap->sector_buf;
Tejun Heo623a3122006-03-05 17:55:58 +09003735 int rc;
3736
Tejun Heofe635c72006-05-15 20:57:35 +09003737 /* read ID data */
Tejun Heobff04642006-11-10 18:08:10 +09003738 rc = ata_dev_read_id(dev, &class, readid_flags, id);
Tejun Heo623a3122006-03-05 17:55:58 +09003739 if (rc)
Tejun Heofe309112007-05-15 03:28:15 +09003740 return rc;
Tejun Heo623a3122006-03-05 17:55:58 +09003741
3742 /* is the device still there? */
Tejun Heofe309112007-05-15 03:28:15 +09003743 if (!ata_dev_same_device(dev, class, id))
3744 return -ENODEV;
Tejun Heo623a3122006-03-05 17:55:58 +09003745
Tejun Heofe635c72006-05-15 20:57:35 +09003746 memcpy(dev->id, id, sizeof(id[0]) * ATA_ID_WORDS);
Tejun Heofe309112007-05-15 03:28:15 +09003747 return 0;
3748}
3749
3750/**
3751 * ata_dev_revalidate - Revalidate ATA device
3752 * @dev: device to revalidate
Tejun Heo422c9da2007-09-23 13:14:12 +09003753 * @new_class: new class code
Tejun Heofe309112007-05-15 03:28:15 +09003754 * @readid_flags: read ID flags
3755 *
3756 * Re-read IDENTIFY page, make sure @dev is still attached to the
3757 * port and reconfigure it according to the new IDENTIFY page.
3758 *
3759 * LOCKING:
3760 * Kernel thread context (may sleep)
3761 *
3762 * RETURNS:
3763 * 0 on success, negative errno otherwise
3764 */
Tejun Heo422c9da2007-09-23 13:14:12 +09003765int ata_dev_revalidate(struct ata_device *dev, unsigned int new_class,
3766 unsigned int readid_flags)
Tejun Heofe309112007-05-15 03:28:15 +09003767{
Tejun Heo6ddcd3b2007-05-15 03:28:15 +09003768 u64 n_sectors = dev->n_sectors;
Tejun Heofe309112007-05-15 03:28:15 +09003769 int rc;
3770
3771 if (!ata_dev_enabled(dev))
3772 return -ENODEV;
3773
Tejun Heo422c9da2007-09-23 13:14:12 +09003774 /* fail early if !ATA && !ATAPI to avoid issuing [P]IDENTIFY to PMP */
3775 if (ata_class_enabled(new_class) &&
3776 new_class != ATA_DEV_ATA && new_class != ATA_DEV_ATAPI) {
3777 ata_dev_printk(dev, KERN_INFO, "class mismatch %u != %u\n",
3778 dev->class, new_class);
3779 rc = -ENODEV;
3780 goto fail;
3781 }
3782
Tejun Heofe309112007-05-15 03:28:15 +09003783 /* re-read ID */
3784 rc = ata_dev_reread_id(dev, readid_flags);
3785 if (rc)
3786 goto fail;
Tejun Heo623a3122006-03-05 17:55:58 +09003787
3788 /* configure device according to the new ID */
Tejun Heoefdaedc2006-11-01 18:38:52 +09003789 rc = ata_dev_configure(dev);
Tejun Heo6ddcd3b2007-05-15 03:28:15 +09003790 if (rc)
3791 goto fail;
3792
3793 /* verify n_sectors hasn't changed */
Tejun Heob54eebd2007-08-17 18:46:51 +09003794 if (dev->class == ATA_DEV_ATA && n_sectors &&
3795 dev->n_sectors != n_sectors) {
Tejun Heo6ddcd3b2007-05-15 03:28:15 +09003796 ata_dev_printk(dev, KERN_INFO, "n_sectors mismatch "
3797 "%llu != %llu\n",
3798 (unsigned long long)n_sectors,
3799 (unsigned long long)dev->n_sectors);
Tejun Heo8270bec2007-08-16 03:02:22 +09003800
3801 /* restore original n_sectors */
3802 dev->n_sectors = n_sectors;
3803
Tejun Heo6ddcd3b2007-05-15 03:28:15 +09003804 rc = -ENODEV;
3805 goto fail;
3806 }
3807
3808 return 0;
Tejun Heo623a3122006-03-05 17:55:58 +09003809
3810 fail:
Tejun Heof15a1da2006-05-15 20:57:56 +09003811 ata_dev_printk(dev, KERN_ERR, "revalidation failed (errno=%d)\n", rc);
Tejun Heo623a3122006-03-05 17:55:58 +09003812 return rc;
3813}
3814
Alan Cox6919a0a2006-10-27 19:08:46 -07003815struct ata_blacklist_entry {
3816 const char *model_num;
3817 const char *model_rev;
3818 unsigned long horkage;
3819};
3820
3821static const struct ata_blacklist_entry ata_device_blacklist [] = {
3822 /* Devices with DMA related problems under Linux */
3823 { "WDC AC11000H", NULL, ATA_HORKAGE_NODMA },
3824 { "WDC AC22100H", NULL, ATA_HORKAGE_NODMA },
3825 { "WDC AC32500H", NULL, ATA_HORKAGE_NODMA },
3826 { "WDC AC33100H", NULL, ATA_HORKAGE_NODMA },
3827 { "WDC AC31600H", NULL, ATA_HORKAGE_NODMA },
3828 { "WDC AC32100H", "24.09P07", ATA_HORKAGE_NODMA },
3829 { "WDC AC23200L", "21.10N21", ATA_HORKAGE_NODMA },
3830 { "Compaq CRD-8241B", NULL, ATA_HORKAGE_NODMA },
3831 { "CRD-8400B", NULL, ATA_HORKAGE_NODMA },
3832 { "CRD-8480B", NULL, ATA_HORKAGE_NODMA },
3833 { "CRD-8482B", NULL, ATA_HORKAGE_NODMA },
3834 { "CRD-84", NULL, ATA_HORKAGE_NODMA },
3835 { "SanDisk SDP3B", NULL, ATA_HORKAGE_NODMA },
3836 { "SanDisk SDP3B-64", NULL, ATA_HORKAGE_NODMA },
3837 { "SANYO CD-ROM CRD", NULL, ATA_HORKAGE_NODMA },
3838 { "HITACHI CDR-8", NULL, ATA_HORKAGE_NODMA },
3839 { "HITACHI CDR-8335", NULL, ATA_HORKAGE_NODMA },
3840 { "HITACHI CDR-8435", NULL, ATA_HORKAGE_NODMA },
3841 { "Toshiba CD-ROM XM-6202B", NULL, ATA_HORKAGE_NODMA },
3842 { "TOSHIBA CD-ROM XM-1702BC", NULL, ATA_HORKAGE_NODMA },
3843 { "CD-532E-A", NULL, ATA_HORKAGE_NODMA },
3844 { "E-IDE CD-ROM CR-840",NULL, ATA_HORKAGE_NODMA },
3845 { "CD-ROM Drive/F5A", NULL, ATA_HORKAGE_NODMA },
3846 { "WPI CDD-820", NULL, ATA_HORKAGE_NODMA },
3847 { "SAMSUNG CD-ROM SC-148C", NULL, ATA_HORKAGE_NODMA },
3848 { "SAMSUNG CD-ROM SC", NULL, ATA_HORKAGE_NODMA },
Alan Cox6919a0a2006-10-27 19:08:46 -07003849 { "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,ATA_HORKAGE_NODMA },
3850 { "_NEC DV5800A", NULL, ATA_HORKAGE_NODMA },
3851 { "SAMSUNG CD-ROM SN-124","N001", ATA_HORKAGE_NODMA },
Dave Jones39f19882007-05-21 14:31:03 -04003852 { "Seagate STT20000A", NULL, ATA_HORKAGE_NODMA },
Tejun Heo5acd50f2007-06-10 14:52:36 +09003853 { "IOMEGA ZIP 250 ATAPI", NULL, ATA_HORKAGE_NODMA }, /* temporary fix */
Tejun Heo39ce7122007-07-05 12:31:31 +09003854 { "IOMEGA ZIP 250 ATAPI Floppy",
3855 NULL, ATA_HORKAGE_NODMA },
Alan Cox6919a0a2006-10-27 19:08:46 -07003856
Albert Lee18d6e9d2007-04-02 11:34:15 +08003857 /* Weird ATAPI devices */
Tejun Heo40a1d532007-06-27 02:49:38 +09003858 { "TORiSAN DVD-ROM DRD-N216", NULL, ATA_HORKAGE_MAX_SEC_128 },
Albert Lee18d6e9d2007-04-02 11:34:15 +08003859
Alan Cox6919a0a2006-10-27 19:08:46 -07003860 /* Devices we expect to fail diagnostics */
3861
3862 /* Devices where NCQ should be avoided */
3863 /* NCQ is slow */
3864 { "WDC WD740ADFD-00", NULL, ATA_HORKAGE_NONCQ },
Tejun Heo09125ea2007-02-28 15:21:23 +09003865 /* http://thread.gmane.org/gmane.linux.ide/14907 */
3866 { "FUJITSU MHT2060BH", NULL, ATA_HORKAGE_NONCQ },
Paul Rolland7acfaf32007-03-26 21:43:44 -08003867 /* NCQ is broken */
Jeff Garzik539cc7c2007-09-20 16:31:47 -04003868 { "Maxtor *", "BANC*", ATA_HORKAGE_NONCQ },
Alan Cox0e3dbc02007-09-20 15:22:47 +01003869 { "Maxtor 7V300F0", "VA111630", ATA_HORKAGE_NONCQ },
Prarit Bhargava2f8d90a2007-07-10 18:13:28 -04003870 { "HITACHI HDS7250SASUN500G 0621KTAWSD", "K2AOAJ0AHITACHI",
Jeff Garzik539cc7c2007-09-20 16:31:47 -04003871 ATA_HORKAGE_NONCQ },
3872
Robert Hancock36e337d2007-04-02 22:05:29 -06003873 /* Blacklist entries taken from Silicon Image 3124/3132
3874 Windows driver .inf file - also several Linux problem reports */
3875 { "HTS541060G9SA00", "MB3OC60D", ATA_HORKAGE_NONCQ, },
3876 { "HTS541080G9SA00", "MB4OC60D", ATA_HORKAGE_NONCQ, },
3877 { "HTS541010G9SA00", "MBZOC60D", ATA_HORKAGE_NONCQ, },
Tejun Heobd9c5a32007-06-08 22:20:59 +09003878 /* Drives which do spurious command completion */
3879 { "HTS541680J9SA00", "SB2IC7EP", ATA_HORKAGE_NONCQ, },
Tejun Heo2f8fceb2007-06-18 14:38:41 +09003880 { "HTS541612J9SA00", "SBDIC7JP", ATA_HORKAGE_NONCQ, },
Tejun Heoe14cbfa2007-06-25 11:28:59 +09003881 { "Hitachi HTS541616J9SA00", "SB4OC70P", ATA_HORKAGE_NONCQ, },
Tejun Heo2f8fceb2007-06-18 14:38:41 +09003882 { "WDC WD740ADFD-00NLR1", NULL, ATA_HORKAGE_NONCQ, },
Tejun Heoa520f262007-07-10 16:16:18 +09003883 { "FUJITSU MHV2080BH", "00840028", ATA_HORKAGE_NONCQ, },
Tejun Heo3fb65892007-07-20 12:49:38 +09003884 { "ST9160821AS", "3.CLF", ATA_HORKAGE_NONCQ, },
Alan Cox0e3dbc02007-09-20 15:22:47 +01003885 { "ST3160812AS", "3.AD", ATA_HORKAGE_NONCQ, },
Tejun Heo5d6aca82007-07-28 16:25:25 +09003886 { "SAMSUNG HD401LJ", "ZZ100-15", ATA_HORKAGE_NONCQ, },
Alan Cox6919a0a2006-10-27 19:08:46 -07003887
Tejun Heo16c55b02007-08-29 11:58:33 +09003888 /* devices which puke on READ_NATIVE_MAX */
3889 { "HDS724040KLSA80", "KFAOA20N", ATA_HORKAGE_BROKEN_HPA, },
3890 { "WDC WD3200JD-00KLB0", "WD-WCAMR1130137", ATA_HORKAGE_BROKEN_HPA },
3891 { "WDC WD2500JD-00HBB0", "WD-WMAL71490727", ATA_HORKAGE_BROKEN_HPA },
3892 { "MAXTOR 6L080L4", "A93.0500", ATA_HORKAGE_BROKEN_HPA },
Alan Cox6919a0a2006-10-27 19:08:46 -07003893
3894 /* End Marker */
3895 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896};
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05003897
Jeff Garzik539cc7c2007-09-20 16:31:47 -04003898int strn_pattern_cmp(const char *patt, const char *name, int wildchar)
3899{
3900 const char *p;
3901 int len;
3902
3903 /*
3904 * check for trailing wildcard: *\0
3905 */
3906 p = strchr(patt, wildchar);
3907 if (p && ((*(p + 1)) == 0))
3908 len = p - patt;
3909 else
3910 len = strlen(name);
3911
3912 return strncmp(patt, name, len);
3913}
3914
Tejun Heo75683fe2007-07-05 13:31:27 +09003915static unsigned long ata_dev_blacklisted(const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916{
Tejun Heo8bfa79f2007-01-02 20:19:40 +09003917 unsigned char model_num[ATA_ID_PROD_LEN + 1];
3918 unsigned char model_rev[ATA_ID_FW_REV_LEN + 1];
Alan Cox6919a0a2006-10-27 19:08:46 -07003919 const struct ata_blacklist_entry *ad = ata_device_blacklist;
Albert Lee3a778272006-06-22 13:00:25 +08003920
Tejun Heo8bfa79f2007-01-02 20:19:40 +09003921 ata_id_c_string(dev->id, model_num, ATA_ID_PROD, sizeof(model_num));
3922 ata_id_c_string(dev->id, model_rev, ATA_ID_FW_REV, sizeof(model_rev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003923
Alan Cox6919a0a2006-10-27 19:08:46 -07003924 while (ad->model_num) {
Jeff Garzik539cc7c2007-09-20 16:31:47 -04003925 if (!strn_pattern_cmp(ad->model_num, model_num, '*')) {
Alan Cox6919a0a2006-10-27 19:08:46 -07003926 if (ad->model_rev == NULL)
3927 return ad->horkage;
Jeff Garzik539cc7c2007-09-20 16:31:47 -04003928 if (!strn_pattern_cmp(ad->model_rev, model_rev, '*'))
Alan Cox6919a0a2006-10-27 19:08:46 -07003929 return ad->horkage;
Alan Coxf4b15fe2006-03-22 15:54:04 +00003930 }
Alan Cox6919a0a2006-10-27 19:08:46 -07003931 ad++;
Alan Coxf4b15fe2006-03-22 15:54:04 +00003932 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933 return 0;
3934}
3935
Alan Cox6919a0a2006-10-27 19:08:46 -07003936static int ata_dma_blacklisted(const struct ata_device *dev)
3937{
3938 /* We don't support polling DMA.
3939 * DMA blacklist those ATAPI devices with CDB-intr (and use PIO)
3940 * if the LLDD handles only interrupts in the HSM_ST_LAST state.
3941 */
Tejun Heo9af5c9c2007-08-06 18:36:22 +09003942 if ((dev->link->ap->flags & ATA_FLAG_PIO_POLLING) &&
Alan Cox6919a0a2006-10-27 19:08:46 -07003943 (dev->flags & ATA_DFLAG_CDB_INTR))
3944 return 1;
Tejun Heo75683fe2007-07-05 13:31:27 +09003945 return (dev->horkage & ATA_HORKAGE_NODMA) ? 1 : 0;
Alan Cox6919a0a2006-10-27 19:08:46 -07003946}
3947
Tejun Heoa6d5a512006-03-06 04:31:57 +09003948/**
3949 * ata_dev_xfermask - Compute supported xfermask of the given device
Tejun Heoa6d5a512006-03-06 04:31:57 +09003950 * @dev: Device to compute xfermask for
3951 *
Tejun Heoacf356b2006-03-24 14:07:50 +09003952 * Compute supported xfermask of @dev and store it in
3953 * dev->*_mask. This function is responsible for applying all
3954 * known limits including host controller limits, device
3955 * blacklist, etc...
Tejun Heoa6d5a512006-03-06 04:31:57 +09003956 *
3957 * LOCKING:
3958 * None.
Tejun Heoa6d5a512006-03-06 04:31:57 +09003959 */
Tejun Heo3373efd2006-05-15 20:57:53 +09003960static void ata_dev_xfermask(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003961{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09003962 struct ata_link *link = dev->link;
3963 struct ata_port *ap = link->ap;
Jeff Garzikcca39742006-08-24 03:19:22 -04003964 struct ata_host *host = ap->host;
Tejun Heoa6d5a512006-03-06 04:31:57 +09003965 unsigned long xfer_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966
Tejun Heo37deecb2006-08-10 16:59:07 +09003967 /* controller modes available */
Tejun Heo565083e2006-04-02 17:54:47 +09003968 xfer_mask = ata_pack_xfermask(ap->pio_mask,
3969 ap->mwdma_mask, ap->udma_mask);
3970
Robert Hancock8343f882007-03-06 02:37:51 -08003971 /* drive modes available */
Tejun Heo37deecb2006-08-10 16:59:07 +09003972 xfer_mask &= ata_pack_xfermask(dev->pio_mask,
3973 dev->mwdma_mask, dev->udma_mask);
3974 xfer_mask &= ata_id_xfermask(dev->id);
Tejun Heo565083e2006-04-02 17:54:47 +09003975
Alan Coxb352e572006-08-10 18:52:12 +01003976 /*
3977 * CFA Advanced TrueIDE timings are not allowed on a shared
3978 * cable
3979 */
3980 if (ata_dev_pair(dev)) {
3981 /* No PIO5 or PIO6 */
3982 xfer_mask &= ~(0x03 << (ATA_SHIFT_PIO + 5));
3983 /* No MWDMA3 or MWDMA 4 */
3984 xfer_mask &= ~(0x03 << (ATA_SHIFT_MWDMA + 3));
3985 }
3986
Tejun Heo37deecb2006-08-10 16:59:07 +09003987 if (ata_dma_blacklisted(dev)) {
3988 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
Tejun Heof15a1da2006-05-15 20:57:56 +09003989 ata_dev_printk(dev, KERN_WARNING,
3990 "device is on DMA blacklist, disabling DMA\n");
Tejun Heo37deecb2006-08-10 16:59:07 +09003991 }
Tejun Heoa6d5a512006-03-06 04:31:57 +09003992
Petr Vandrovec14d66ab2007-03-08 10:12:12 +01003993 if ((host->flags & ATA_HOST_SIMPLEX) &&
3994 host->simplex_claimed && host->simplex_claimed != ap) {
Tejun Heo37deecb2006-08-10 16:59:07 +09003995 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
3996 ata_dev_printk(dev, KERN_WARNING, "simplex DMA is claimed by "
3997 "other device, disabling DMA\n");
Alan Cox5444a6f2006-03-27 18:58:20 +01003998 }
Tejun Heo565083e2006-04-02 17:54:47 +09003999
Jeff Garzike4246752007-03-09 09:56:46 -05004000 if (ap->flags & ATA_FLAG_NO_IORDY)
4001 xfer_mask &= ata_pio_mask_no_iordy(dev);
4002
Alan Cox5444a6f2006-03-27 18:58:20 +01004003 if (ap->ops->mode_filter)
Alan Coxa76b62c2007-03-09 09:34:07 -05004004 xfer_mask = ap->ops->mode_filter(dev, xfer_mask);
Alan Cox5444a6f2006-03-27 18:58:20 +01004005
Robert Hancock8343f882007-03-06 02:37:51 -08004006 /* Apply cable rule here. Don't apply it early because when
4007 * we handle hot plug the cable type can itself change.
4008 * Check this last so that we know if the transfer rate was
4009 * solely limited by the cable.
4010 * Unknown or 80 wire cables reported host side are checked
4011 * drive side as well. Cases where we know a 40wire cable
4012 * is used safely for 80 are not checked here.
4013 */
4014 if (xfer_mask & (0xF8 << ATA_SHIFT_UDMA))
4015 /* UDMA/44 or higher would be available */
4016 if((ap->cbl == ATA_CBL_PATA40) ||
4017 (ata_drive_40wire(dev->id) &&
4018 (ap->cbl == ATA_CBL_PATA_UNK ||
4019 ap->cbl == ATA_CBL_PATA80))) {
4020 ata_dev_printk(dev, KERN_WARNING,
4021 "limited to UDMA/33 due to 40-wire cable\n");
4022 xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
4023 }
4024
Tejun Heo565083e2006-04-02 17:54:47 +09004025 ata_unpack_xfermask(xfer_mask, &dev->pio_mask,
4026 &dev->mwdma_mask, &dev->udma_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027}
4028
Linus Torvalds1da177e2005-04-16 15:20:36 -07004029/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004030 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
Linus Torvalds1da177e2005-04-16 15:20:36 -07004031 * @dev: Device to which command will be sent
4032 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04004033 * Issue SET FEATURES - XFER MODE command to device @dev
4034 * on port @ap.
4035 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004036 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004037 * PCI/etc. bus probe sem.
Tejun Heo83206a22006-03-24 15:25:31 +09004038 *
4039 * RETURNS:
4040 * 0 on success, AC_ERR_* mask otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041 */
4042
Tejun Heo3373efd2006-05-15 20:57:53 +09004043static unsigned int ata_dev_set_xfermode(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044{
Tejun Heoa0123702005-12-13 14:49:31 +09004045 struct ata_taskfile tf;
Tejun Heo83206a22006-03-24 15:25:31 +09004046 unsigned int err_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047
4048 /* set up set-features taskfile */
4049 DPRINTK("set features - xfer mode\n");
4050
Tejun Heo464cf172007-05-27 15:10:40 +02004051 /* Some controllers and ATAPI devices show flaky interrupt
4052 * behavior after setting xfer mode. Use polling instead.
4053 */
Tejun Heo3373efd2006-05-15 20:57:53 +09004054 ata_tf_init(dev, &tf);
Tejun Heoa0123702005-12-13 14:49:31 +09004055 tf.command = ATA_CMD_SET_FEATURES;
4056 tf.feature = SETFEATURES_XFER;
Tejun Heo464cf172007-05-27 15:10:40 +02004057 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_POLLING;
Tejun Heoa0123702005-12-13 14:49:31 +09004058 tf.protocol = ATA_PROT_NODATA;
4059 tf.nsect = dev->xfer_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060
Tejun Heo3373efd2006-05-15 20:57:53 +09004061 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062
Tejun Heo83206a22006-03-24 15:25:31 +09004063 DPRINTK("EXIT, err_mask=%x\n", err_mask);
4064 return err_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065}
4066
4067/**
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04004068 * ata_dev_set_AN - Issue SET FEATURES - SATA FEATURES
4069 * @dev: Device to which command will be sent
4070 * @enable: Whether to enable or disable the feature
4071 *
4072 * Issue SET FEATURES - SATA FEATURES command to device @dev
4073 * on port @ap with sector count set to indicate Asynchronous
4074 * Notification feature
4075 *
4076 * LOCKING:
4077 * PCI/etc. bus probe sem.
4078 *
4079 * RETURNS:
4080 * 0 on success, AC_ERR_* mask otherwise.
4081 */
4082static unsigned int ata_dev_set_AN(struct ata_device *dev, u8 enable)
4083{
4084 struct ata_taskfile tf;
4085 unsigned int err_mask;
4086
4087 /* set up set-features taskfile */
4088 DPRINTK("set features - SATA features\n");
4089
4090 ata_tf_init(dev, &tf);
4091 tf.command = ATA_CMD_SET_FEATURES;
4092 tf.feature = enable;
4093 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
4094 tf.protocol = ATA_PROT_NODATA;
4095 tf.nsect = SATA_AN;
4096
4097 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
4098
4099 DPRINTK("EXIT, err_mask=%x\n", err_mask);
4100 return err_mask;
4101}
4102
4103/**
Albert Lee8bf62ece2005-05-12 15:29:42 -04004104 * ata_dev_init_params - Issue INIT DEV PARAMS command
Albert Lee8bf62ece2005-05-12 15:29:42 -04004105 * @dev: Device to which command will be sent
Randy Dunlape2a7f772006-05-18 10:50:18 -07004106 * @heads: Number of heads (taskfile parameter)
4107 * @sectors: Number of sectors (taskfile parameter)
Albert Lee8bf62ece2005-05-12 15:29:42 -04004108 *
4109 * LOCKING:
Tejun Heo6aff8f12006-02-15 18:24:09 +09004110 * Kernel thread context (may sleep)
4111 *
4112 * RETURNS:
4113 * 0 on success, AC_ERR_* mask otherwise.
Albert Lee8bf62ece2005-05-12 15:29:42 -04004114 */
Tejun Heo3373efd2006-05-15 20:57:53 +09004115static unsigned int ata_dev_init_params(struct ata_device *dev,
4116 u16 heads, u16 sectors)
Albert Lee8bf62ece2005-05-12 15:29:42 -04004117{
Tejun Heoa0123702005-12-13 14:49:31 +09004118 struct ata_taskfile tf;
Tejun Heo6aff8f12006-02-15 18:24:09 +09004119 unsigned int err_mask;
Albert Lee8bf62ece2005-05-12 15:29:42 -04004120
4121 /* Number of sectors per track 1-255. Number of heads 1-16 */
4122 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
Albert Lee00b6f5e2006-03-27 16:39:18 +08004123 return AC_ERR_INVALID;
Albert Lee8bf62ece2005-05-12 15:29:42 -04004124
4125 /* set up init dev params taskfile */
4126 DPRINTK("init dev params \n");
4127
Tejun Heo3373efd2006-05-15 20:57:53 +09004128 ata_tf_init(dev, &tf);
Tejun Heoa0123702005-12-13 14:49:31 +09004129 tf.command = ATA_CMD_INIT_DEV_PARAMS;
4130 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
4131 tf.protocol = ATA_PROT_NODATA;
4132 tf.nsect = sectors;
4133 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
Albert Lee8bf62ece2005-05-12 15:29:42 -04004134
Tejun Heo3373efd2006-05-15 20:57:53 +09004135 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
Alan Cox18b24662007-08-08 14:28:49 +01004136 /* A clean abort indicates an original or just out of spec drive
4137 and we should continue as we issue the setup based on the
4138 drive reported working geometry */
4139 if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
4140 err_mask = 0;
Albert Lee8bf62ece2005-05-12 15:29:42 -04004141
Tejun Heo6aff8f12006-02-15 18:24:09 +09004142 DPRINTK("EXIT, err_mask=%x\n", err_mask);
4143 return err_mask;
Albert Lee8bf62ece2005-05-12 15:29:42 -04004144}
4145
4146/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004147 * ata_sg_clean - Unmap DMA memory associated with command
4148 * @qc: Command containing DMA memory to be released
4149 *
4150 * Unmap all mapped DMA memory associated with this command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151 *
4152 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004153 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154 */
Tejun Heo70e6ad02006-11-14 22:47:10 +09004155void ata_sg_clean(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156{
4157 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004158 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004160 void *pad_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161
Tejun Heoa46314742006-02-11 19:11:13 +09004162 WARN_ON(!(qc->flags & ATA_QCFLAG_DMAMAP));
4163 WARN_ON(sg == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164
4165 if (qc->flags & ATA_QCFLAG_SINGLE)
Jeff Garzikf1318832006-02-20 16:55:56 -05004166 WARN_ON(qc->n_elem > 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05004168 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004170 /* if we padded the buffer out to 32-bit bound, and data
4171 * xfer direction is from-device, we must copy from the
4172 * pad buffer back into the supplied buffer
4173 */
4174 if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
4175 pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
4176
4177 if (qc->flags & ATA_QCFLAG_SG) {
Jeff Garzike1410f22005-11-14 14:06:26 -05004178 if (qc->n_elem)
Brian King2f1f6102006-03-23 17:30:15 -06004179 dma_unmap_sg(ap->dev, sg, qc->n_elem, dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004180 /* restore last sg */
4181 sg[qc->orig_n_elem - 1].length += qc->pad_len;
4182 if (pad_buf) {
4183 struct scatterlist *psg = &qc->pad_sgent;
4184 void *addr = kmap_atomic(psg->page, KM_IRQ0);
4185 memcpy(addr + psg->offset, pad_buf, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05004186 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004187 }
4188 } else {
Tejun Heo2e242fa2006-02-20 23:48:38 +09004189 if (qc->n_elem)
Brian King2f1f6102006-03-23 17:30:15 -06004190 dma_unmap_single(ap->dev,
Jeff Garzike1410f22005-11-14 14:06:26 -05004191 sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
4192 dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004193 /* restore sg */
4194 sg->length += qc->pad_len;
4195 if (pad_buf)
4196 memcpy(qc->buf_virt + sg->length - qc->pad_len,
4197 pad_buf, qc->pad_len);
4198 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199
4200 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004201 qc->__sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202}
4203
4204/**
4205 * ata_fill_sg - Fill PCI IDE PRD table
4206 * @qc: Metadata associated with taskfile to be transferred
4207 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04004208 * Fill PCI IDE PRD (scatter-gather) table with segments
4209 * associated with the current disk command.
4210 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004212 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213 *
4214 */
4215static void ata_fill_sg(struct ata_queued_cmd *qc)
4216{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004218 struct scatterlist *sg;
4219 unsigned int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004220
Tejun Heoa46314742006-02-11 19:11:13 +09004221 WARN_ON(qc->__sg == NULL);
Jeff Garzikf1318832006-02-20 16:55:56 -05004222 WARN_ON(qc->n_elem == 0 && qc->pad_len == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223
4224 idx = 0;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004225 ata_for_each_sg(sg, qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004226 u32 addr, offset;
4227 u32 sg_len, len;
4228
4229 /* determine if physical DMA addr spans 64K boundary.
4230 * Note h/w doesn't support 64-bit, so we unconditionally
4231 * truncate dma_addr_t to u32.
4232 */
4233 addr = (u32) sg_dma_address(sg);
4234 sg_len = sg_dma_len(sg);
4235
4236 while (sg_len) {
4237 offset = addr & 0xffff;
4238 len = sg_len;
4239 if ((offset + sg_len) > 0x10000)
4240 len = 0x10000 - offset;
4241
4242 ap->prd[idx].addr = cpu_to_le32(addr);
4243 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
4244 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
4245
4246 idx++;
4247 sg_len -= len;
4248 addr += len;
4249 }
4250 }
4251
4252 if (idx)
4253 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
4254}
Tejun Heob9a41972007-06-27 02:48:43 +09004255
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256/**
Alan Coxd26fc952007-07-06 19:13:52 -04004257 * ata_fill_sg_dumb - Fill PCI IDE PRD table
4258 * @qc: Metadata associated with taskfile to be transferred
4259 *
4260 * Fill PCI IDE PRD (scatter-gather) table with segments
4261 * associated with the current disk command. Perform the fill
4262 * so that we avoid writing any length 64K records for
4263 * controllers that don't follow the spec.
4264 *
4265 * LOCKING:
4266 * spin_lock_irqsave(host lock)
4267 *
4268 */
4269static void ata_fill_sg_dumb(struct ata_queued_cmd *qc)
4270{
4271 struct ata_port *ap = qc->ap;
4272 struct scatterlist *sg;
4273 unsigned int idx;
4274
4275 WARN_ON(qc->__sg == NULL);
4276 WARN_ON(qc->n_elem == 0 && qc->pad_len == 0);
4277
4278 idx = 0;
4279 ata_for_each_sg(sg, qc) {
4280 u32 addr, offset;
4281 u32 sg_len, len, blen;
4282
4283 /* determine if physical DMA addr spans 64K boundary.
4284 * Note h/w doesn't support 64-bit, so we unconditionally
4285 * truncate dma_addr_t to u32.
4286 */
4287 addr = (u32) sg_dma_address(sg);
4288 sg_len = sg_dma_len(sg);
4289
4290 while (sg_len) {
4291 offset = addr & 0xffff;
4292 len = sg_len;
4293 if ((offset + sg_len) > 0x10000)
4294 len = 0x10000 - offset;
4295
4296 blen = len & 0xffff;
4297 ap->prd[idx].addr = cpu_to_le32(addr);
4298 if (blen == 0) {
4299 /* Some PATA chipsets like the CS5530 can't
4300 cope with 0x0000 meaning 64K as the spec says */
4301 ap->prd[idx].flags_len = cpu_to_le32(0x8000);
4302 blen = 0x8000;
4303 ap->prd[++idx].addr = cpu_to_le32(addr + 0x8000);
4304 }
4305 ap->prd[idx].flags_len = cpu_to_le32(blen);
4306 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
4307
4308 idx++;
4309 sg_len -= len;
4310 addr += len;
4311 }
4312 }
4313
4314 if (idx)
4315 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
4316}
4317
4318/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
4320 * @qc: Metadata associated with taskfile to check
4321 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04004322 * Allow low-level driver to filter ATA PACKET commands, returning
4323 * a status indicating whether or not it is OK to use DMA for the
4324 * supplied PACKET command.
4325 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004327 * spin_lock_irqsave(host lock)
Jeff Garzik0cba6322005-05-30 19:49:12 -04004328 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329 * RETURNS: 0 when ATAPI DMA can be used
4330 * nonzero otherwise
4331 */
4332int ata_check_atapi_dma(struct ata_queued_cmd *qc)
4333{
4334 struct ata_port *ap = qc->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335
Tejun Heob9a41972007-06-27 02:48:43 +09004336 /* Don't allow DMA if it isn't multiple of 16 bytes. Quite a
4337 * few ATAPI devices choke on such DMA requests.
4338 */
4339 if (unlikely(qc->nbytes & 15))
4340 return 1;
Albert Lee6f23a312007-04-02 11:39:25 +08004341
Linus Torvalds1da177e2005-04-16 15:20:36 -07004342 if (ap->ops->check_atapi_dma)
Tejun Heob9a41972007-06-27 02:48:43 +09004343 return ap->ops->check_atapi_dma(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004344
Tejun Heob9a41972007-06-27 02:48:43 +09004345 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346}
Tejun Heob9a41972007-06-27 02:48:43 +09004347
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348/**
Tejun Heo31cc23b2007-09-23 13:14:12 +09004349 * ata_std_qc_defer - Check whether a qc needs to be deferred
4350 * @qc: ATA command in question
4351 *
4352 * Non-NCQ commands cannot run with any other command, NCQ or
4353 * not. As upper layer only knows the queue depth, we are
4354 * responsible for maintaining exclusion. This function checks
4355 * whether a new command @qc can be issued.
4356 *
4357 * LOCKING:
4358 * spin_lock_irqsave(host lock)
4359 *
4360 * RETURNS:
4361 * ATA_DEFER_* if deferring is needed, 0 otherwise.
4362 */
4363int ata_std_qc_defer(struct ata_queued_cmd *qc)
4364{
4365 struct ata_link *link = qc->dev->link;
4366
4367 if (qc->tf.protocol == ATA_PROT_NCQ) {
4368 if (!ata_tag_valid(link->active_tag))
4369 return 0;
4370 } else {
4371 if (!ata_tag_valid(link->active_tag) && !link->sactive)
4372 return 0;
4373 }
4374
4375 return ATA_DEFER_LINK;
4376}
4377
4378/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379 * ata_qc_prep - Prepare taskfile for submission
4380 * @qc: Metadata associated with taskfile to be prepared
4381 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04004382 * Prepare ATA taskfile for submission.
4383 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004385 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004386 */
4387void ata_qc_prep(struct ata_queued_cmd *qc)
4388{
4389 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
4390 return;
4391
4392 ata_fill_sg(qc);
4393}
4394
Alan Coxd26fc952007-07-06 19:13:52 -04004395/**
4396 * ata_dumb_qc_prep - Prepare taskfile for submission
4397 * @qc: Metadata associated with taskfile to be prepared
4398 *
4399 * Prepare ATA taskfile for submission.
4400 *
4401 * LOCKING:
4402 * spin_lock_irqsave(host lock)
4403 */
4404void ata_dumb_qc_prep(struct ata_queued_cmd *qc)
4405{
4406 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
4407 return;
4408
4409 ata_fill_sg_dumb(qc);
4410}
4411
Brian Kinge46834c2006-03-17 17:04:03 -06004412void ata_noop_qc_prep(struct ata_queued_cmd *qc) { }
4413
Jeff Garzik0cba6322005-05-30 19:49:12 -04004414/**
4415 * ata_sg_init_one - Associate command with memory buffer
4416 * @qc: Command to be associated
4417 * @buf: Memory buffer
4418 * @buflen: Length of memory buffer, in bytes.
4419 *
4420 * Initialize the data-related elements of queued_cmd @qc
4421 * to point to a single memory buffer, @buf of byte length @buflen.
4422 *
4423 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004424 * spin_lock_irqsave(host lock)
Jeff Garzik0cba6322005-05-30 19:49:12 -04004425 */
4426
Linus Torvalds1da177e2005-04-16 15:20:36 -07004427void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
4428{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429 qc->flags |= ATA_QCFLAG_SINGLE;
4430
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004431 qc->__sg = &qc->sgent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004432 qc->n_elem = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004433 qc->orig_n_elem = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434 qc->buf_virt = buf;
Brian King233277c2006-06-07 11:25:31 -05004435 qc->nbytes = buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004436
Tejun Heo61c05962006-11-14 22:35:43 +09004437 sg_init_one(&qc->sgent, buf, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004438}
4439
Jeff Garzik0cba6322005-05-30 19:49:12 -04004440/**
4441 * ata_sg_init - Associate command with scatter-gather table.
4442 * @qc: Command to be associated
4443 * @sg: Scatter-gather table.
4444 * @n_elem: Number of elements in s/g table.
4445 *
4446 * Initialize the data-related elements of queued_cmd @qc
4447 * to point to a scatter-gather table @sg, containing @n_elem
4448 * elements.
4449 *
4450 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004451 * spin_lock_irqsave(host lock)
Jeff Garzik0cba6322005-05-30 19:49:12 -04004452 */
4453
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
4455 unsigned int n_elem)
4456{
4457 qc->flags |= ATA_QCFLAG_SG;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004458 qc->__sg = sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004459 qc->n_elem = n_elem;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004460 qc->orig_n_elem = n_elem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461}
4462
4463/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004464 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
4465 * @qc: Command with memory buffer to be mapped.
4466 *
4467 * DMA-map the memory buffer associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004468 *
4469 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004470 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471 *
4472 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004473 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474 */
4475
4476static int ata_sg_setup_one(struct ata_queued_cmd *qc)
4477{
4478 struct ata_port *ap = qc->ap;
4479 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004480 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481 dma_addr_t dma_address;
Tejun Heo2e242fa2006-02-20 23:48:38 +09004482 int trim_sg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004484 /* we must lengthen transfers to end on a 32-bit boundary */
4485 qc->pad_len = sg->length & 3;
4486 if (qc->pad_len) {
4487 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
4488 struct scatterlist *psg = &qc->pad_sgent;
4489
Tejun Heoa46314742006-02-11 19:11:13 +09004490 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004491
4492 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
4493
4494 if (qc->tf.flags & ATA_TFLAG_WRITE)
4495 memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
4496 qc->pad_len);
4497
4498 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
4499 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
4500 /* trim sg */
4501 sg->length -= qc->pad_len;
Tejun Heo2e242fa2006-02-20 23:48:38 +09004502 if (sg->length == 0)
4503 trim_sg = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004504
4505 DPRINTK("padding done, sg->length=%u pad_len=%u\n",
4506 sg->length, qc->pad_len);
4507 }
4508
Tejun Heo2e242fa2006-02-20 23:48:38 +09004509 if (trim_sg) {
4510 qc->n_elem--;
Jeff Garzike1410f22005-11-14 14:06:26 -05004511 goto skip_map;
4512 }
4513
Brian King2f1f6102006-03-23 17:30:15 -06004514 dma_address = dma_map_single(ap->dev, qc->buf_virt,
Albert Lee32529e02005-05-26 03:49:42 -04004515 sg->length, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05004516 if (dma_mapping_error(dma_address)) {
4517 /* restore sg */
4518 sg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05004520 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521
4522 sg_dma_address(sg) = dma_address;
Albert Lee32529e02005-05-26 03:49:42 -04004523 sg_dma_len(sg) = sg->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524
Tejun Heo2e242fa2006-02-20 23:48:38 +09004525skip_map:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
4527 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
4528
4529 return 0;
4530}
4531
4532/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004533 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
4534 * @qc: Command with scatter-gather table to be mapped.
4535 *
4536 * DMA-map the scatter-gather table associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537 *
4538 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004539 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540 *
4541 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004542 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543 *
4544 */
4545
4546static int ata_sg_setup(struct ata_queued_cmd *qc)
4547{
4548 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004549 struct scatterlist *sg = qc->__sg;
4550 struct scatterlist *lsg = &sg[qc->n_elem - 1];
Jeff Garzike1410f22005-11-14 14:06:26 -05004551 int n_elem, pre_n_elem, dir, trim_sg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552
Tejun Heo44877b42007-02-21 01:06:51 +09004553 VPRINTK("ENTER, ata%u\n", ap->print_id);
Tejun Heoa46314742006-02-11 19:11:13 +09004554 WARN_ON(!(qc->flags & ATA_QCFLAG_SG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004556 /* we must lengthen transfers to end on a 32-bit boundary */
4557 qc->pad_len = lsg->length & 3;
4558 if (qc->pad_len) {
4559 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
4560 struct scatterlist *psg = &qc->pad_sgent;
4561 unsigned int offset;
4562
Tejun Heoa46314742006-02-11 19:11:13 +09004563 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004564
4565 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
4566
4567 /*
4568 * psg->page/offset are used to copy to-be-written
4569 * data in this function or read data in ata_sg_clean.
4570 */
4571 offset = lsg->offset + lsg->length - qc->pad_len;
4572 psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
4573 psg->offset = offset_in_page(offset);
4574
4575 if (qc->tf.flags & ATA_TFLAG_WRITE) {
4576 void *addr = kmap_atomic(psg->page, KM_IRQ0);
4577 memcpy(pad_buf, addr + psg->offset, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05004578 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004579 }
4580
4581 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
4582 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
4583 /* trim last sg */
4584 lsg->length -= qc->pad_len;
Jeff Garzike1410f22005-11-14 14:06:26 -05004585 if (lsg->length == 0)
4586 trim_sg = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004587
4588 DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
4589 qc->n_elem - 1, lsg->length, qc->pad_len);
4590 }
4591
Jeff Garzike1410f22005-11-14 14:06:26 -05004592 pre_n_elem = qc->n_elem;
4593 if (trim_sg && pre_n_elem)
4594 pre_n_elem--;
4595
4596 if (!pre_n_elem) {
4597 n_elem = 0;
4598 goto skip_map;
4599 }
4600
Linus Torvalds1da177e2005-04-16 15:20:36 -07004601 dir = qc->dma_dir;
Brian King2f1f6102006-03-23 17:30:15 -06004602 n_elem = dma_map_sg(ap->dev, sg, pre_n_elem, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05004603 if (n_elem < 1) {
4604 /* restore last sg */
4605 lsg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05004607 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608
4609 DPRINTK("%d sg elements mapped\n", n_elem);
4610
Jeff Garzike1410f22005-11-14 14:06:26 -05004611skip_map:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612 qc->n_elem = n_elem;
4613
4614 return 0;
4615}
4616
4617/**
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004618 * swap_buf_le16 - swap halves of 16-bit words in place
Edward Falk0baab862005-06-02 18:17:13 -04004619 * @buf: Buffer to swap
4620 * @buf_words: Number of 16-bit words in buffer.
4621 *
4622 * Swap halves of 16-bit words if needed to convert from
4623 * little-endian byte order to native cpu byte order, or
4624 * vice-versa.
4625 *
4626 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004627 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04004628 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629void swap_buf_le16(u16 *buf, unsigned int buf_words)
4630{
4631#ifdef __BIG_ENDIAN
4632 unsigned int i;
4633
4634 for (i = 0; i < buf_words; i++)
4635 buf[i] = le16_to_cpu(buf[i]);
4636#endif /* __BIG_ENDIAN */
4637}
4638
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004639/**
Tejun Heo0d5ff562007-02-01 15:06:36 +09004640 * ata_data_xfer - Transfer data by PIO
Alan Coxa6b2c5d2006-05-22 16:59:59 +01004641 * @adev: device to target
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004642 * @buf: data buffer
4643 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04004644 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004645 *
4646 * Transfer data from/to the device data register by PIO.
4647 *
4648 * LOCKING:
4649 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004650 */
Tejun Heo0d5ff562007-02-01 15:06:36 +09004651void ata_data_xfer(struct ata_device *adev, unsigned char *buf,
4652 unsigned int buflen, int write_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09004654 struct ata_port *ap = adev->link->ap;
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004655 unsigned int words = buflen >> 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004657 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658 if (write_data)
Tejun Heo0d5ff562007-02-01 15:06:36 +09004659 iowrite16_rep(ap->ioaddr.data_addr, buf, words);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660 else
Tejun Heo0d5ff562007-02-01 15:06:36 +09004661 ioread16_rep(ap->ioaddr.data_addr, buf, words);
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004662
4663 /* Transfer trailing 1 byte, if any. */
4664 if (unlikely(buflen & 0x01)) {
4665 u16 align_buf[1] = { 0 };
4666 unsigned char *trailing_buf = buf + buflen - 1;
4667
4668 if (write_data) {
4669 memcpy(align_buf, trailing_buf, 1);
Tejun Heo0d5ff562007-02-01 15:06:36 +09004670 iowrite16(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004671 } else {
Tejun Heo0d5ff562007-02-01 15:06:36 +09004672 align_buf[0] = cpu_to_le16(ioread16(ap->ioaddr.data_addr));
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004673 memcpy(trailing_buf, align_buf, 1);
4674 }
4675 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004676}
4677
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004678/**
Tejun Heo0d5ff562007-02-01 15:06:36 +09004679 * ata_data_xfer_noirq - Transfer data by PIO
Alan Cox75e99582006-05-24 14:14:41 +01004680 * @adev: device to target
4681 * @buf: data buffer
4682 * @buflen: buffer length
4683 * @write_data: read/write
4684 *
Tejun Heo88574552006-06-25 20:00:35 +09004685 * Transfer data from/to the device data register by PIO. Do the
Alan Cox75e99582006-05-24 14:14:41 +01004686 * transfer with interrupts disabled.
4687 *
4688 * LOCKING:
4689 * Inherited from caller.
4690 */
Tejun Heo0d5ff562007-02-01 15:06:36 +09004691void ata_data_xfer_noirq(struct ata_device *adev, unsigned char *buf,
4692 unsigned int buflen, int write_data)
Alan Cox75e99582006-05-24 14:14:41 +01004693{
4694 unsigned long flags;
4695 local_irq_save(flags);
Tejun Heo0d5ff562007-02-01 15:06:36 +09004696 ata_data_xfer(adev, buf, buflen, write_data);
Alan Cox75e99582006-05-24 14:14:41 +01004697 local_irq_restore(flags);
4698}
4699
4700
4701/**
Mark Lord5a5dbd12007-03-16 10:22:26 -04004702 * ata_pio_sector - Transfer a sector of data.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004703 * @qc: Command on going
4704 *
Mark Lord5a5dbd12007-03-16 10:22:26 -04004705 * Transfer qc->sect_size bytes of data from/to the ATA device.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004706 *
4707 * LOCKING:
4708 * Inherited from caller.
4709 */
4710
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711static void ata_pio_sector(struct ata_queued_cmd *qc)
4712{
4713 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004714 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715 struct ata_port *ap = qc->ap;
4716 struct page *page;
4717 unsigned int offset;
4718 unsigned char *buf;
4719
Mark Lord5a5dbd12007-03-16 10:22:26 -04004720 if (qc->curbytes == qc->nbytes - qc->sect_size)
Albert Lee14be71f2005-09-27 17:36:35 +08004721 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722
4723 page = sg[qc->cursg].page;
Tejun Heo726f0782007-01-03 17:30:39 +09004724 offset = sg[qc->cursg].offset + qc->cursg_ofs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725
4726 /* get the current page and offset */
4727 page = nth_page(page, (offset >> PAGE_SHIFT));
4728 offset %= PAGE_SIZE;
4729
Albert Lee7282aa42005-10-09 09:46:07 -04004730 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
4731
Albert Lee91b8b312005-10-09 09:48:44 -04004732 if (PageHighMem(page)) {
4733 unsigned long flags;
Albert Lee7282aa42005-10-09 09:46:07 -04004734
Alan Coxa6b2c5d2006-05-22 16:59:59 +01004735 /* FIXME: use a bounce buffer */
Albert Lee91b8b312005-10-09 09:48:44 -04004736 local_irq_save(flags);
4737 buf = kmap_atomic(page, KM_IRQ0);
Albert Lee083958d2005-10-09 09:47:31 -04004738
Albert Lee91b8b312005-10-09 09:48:44 -04004739 /* do the actual data transfer */
Mark Lord5a5dbd12007-03-16 10:22:26 -04004740 ap->ops->data_xfer(qc->dev, buf + offset, qc->sect_size, do_write);
Albert Lee91b8b312005-10-09 09:48:44 -04004741
4742 kunmap_atomic(buf, KM_IRQ0);
4743 local_irq_restore(flags);
4744 } else {
4745 buf = page_address(page);
Mark Lord5a5dbd12007-03-16 10:22:26 -04004746 ap->ops->data_xfer(qc->dev, buf + offset, qc->sect_size, do_write);
Albert Lee91b8b312005-10-09 09:48:44 -04004747 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748
Mark Lord5a5dbd12007-03-16 10:22:26 -04004749 qc->curbytes += qc->sect_size;
4750 qc->cursg_ofs += qc->sect_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751
Tejun Heo726f0782007-01-03 17:30:39 +09004752 if (qc->cursg_ofs == (&sg[qc->cursg])->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753 qc->cursg++;
4754 qc->cursg_ofs = 0;
4755 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004758/**
Mark Lord5a5dbd12007-03-16 10:22:26 -04004759 * ata_pio_sectors - Transfer one or many sectors.
Albert Lee07f6f7d2005-11-01 19:33:20 +08004760 * @qc: Command on going
4761 *
Mark Lord5a5dbd12007-03-16 10:22:26 -04004762 * Transfer one or many sectors of data from/to the
Albert Lee07f6f7d2005-11-01 19:33:20 +08004763 * ATA device for the DRQ request.
4764 *
4765 * LOCKING:
4766 * Inherited from caller.
4767 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768
Albert Lee07f6f7d2005-11-01 19:33:20 +08004769static void ata_pio_sectors(struct ata_queued_cmd *qc)
4770{
4771 if (is_multi_taskfile(&qc->tf)) {
4772 /* READ/WRITE MULTIPLE */
4773 unsigned int nsect;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774
Jeff Garzik587005d2006-02-11 18:17:32 -05004775 WARN_ON(qc->dev->multi_count == 0);
Albert Lee07f6f7d2005-11-01 19:33:20 +08004776
Mark Lord5a5dbd12007-03-16 10:22:26 -04004777 nsect = min((qc->nbytes - qc->curbytes) / qc->sect_size,
Tejun Heo726f0782007-01-03 17:30:39 +09004778 qc->dev->multi_count);
Albert Lee07f6f7d2005-11-01 19:33:20 +08004779 while (nsect--)
4780 ata_pio_sector(qc);
4781 } else
4782 ata_pio_sector(qc);
Albert Lee4cc980b2007-08-15 03:19:45 -04004783
4784 ata_altstatus(qc->ap); /* flush */
Albert Lee07f6f7d2005-11-01 19:33:20 +08004785}
4786
4787/**
Albert Leec71c1852005-10-04 06:03:45 -04004788 * atapi_send_cdb - Write CDB bytes to hardware
4789 * @ap: Port to which ATAPI device is attached.
4790 * @qc: Taskfile currently active
4791 *
4792 * When device has indicated its readiness to accept
4793 * a CDB, this function is called. Send the CDB.
4794 *
4795 * LOCKING:
4796 * caller.
4797 */
4798
4799static void atapi_send_cdb(struct ata_port *ap, struct ata_queued_cmd *qc)
4800{
4801 /* send SCSI cdb */
4802 DPRINTK("send cdb\n");
Jeff Garzikdb024d52006-02-13 00:23:57 -05004803 WARN_ON(qc->dev->cdb_len < 12);
Albert Leec71c1852005-10-04 06:03:45 -04004804
Alan Coxa6b2c5d2006-05-22 16:59:59 +01004805 ap->ops->data_xfer(qc->dev, qc->cdb, qc->dev->cdb_len, 1);
Albert Leec71c1852005-10-04 06:03:45 -04004806 ata_altstatus(ap); /* flush */
4807
4808 switch (qc->tf.protocol) {
4809 case ATA_PROT_ATAPI:
4810 ap->hsm_task_state = HSM_ST;
4811 break;
4812 case ATA_PROT_ATAPI_NODATA:
4813 ap->hsm_task_state = HSM_ST_LAST;
4814 break;
4815 case ATA_PROT_ATAPI_DMA:
4816 ap->hsm_task_state = HSM_ST_LAST;
4817 /* initiate bmdma */
4818 ap->ops->bmdma_start(qc);
4819 break;
4820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821}
4822
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004823/**
4824 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
4825 * @qc: Command on going
4826 * @bytes: number of bytes
4827 *
4828 * Transfer Transfer data from/to the ATAPI device.
4829 *
4830 * LOCKING:
4831 * Inherited from caller.
4832 *
4833 */
4834
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
4836{
4837 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004838 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839 struct ata_port *ap = qc->ap;
4840 struct page *page;
4841 unsigned char *buf;
4842 unsigned int offset, count;
4843
Albert Lee563a6e12005-08-12 14:17:50 +08004844 if (qc->curbytes + bytes >= qc->nbytes)
Albert Lee14be71f2005-09-27 17:36:35 +08004845 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846
4847next_sg:
Albert Lee563a6e12005-08-12 14:17:50 +08004848 if (unlikely(qc->cursg >= qc->n_elem)) {
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04004849 /*
Albert Lee563a6e12005-08-12 14:17:50 +08004850 * The end of qc->sg is reached and the device expects
4851 * more data to transfer. In order not to overrun qc->sg
4852 * and fulfill length specified in the byte count register,
4853 * - for read case, discard trailing data from the device
4854 * - for write case, padding zero data to the device
4855 */
4856 u16 pad_buf[1] = { 0 };
4857 unsigned int words = bytes >> 1;
4858 unsigned int i;
4859
4860 if (words) /* warning if bytes > 1 */
Tejun Heof15a1da2006-05-15 20:57:56 +09004861 ata_dev_printk(qc->dev, KERN_WARNING,
4862 "%u bytes trailing data\n", bytes);
Albert Lee563a6e12005-08-12 14:17:50 +08004863
4864 for (i = 0; i < words; i++)
Alan Coxa6b2c5d2006-05-22 16:59:59 +01004865 ap->ops->data_xfer(qc->dev, (unsigned char*)pad_buf, 2, do_write);
Albert Lee563a6e12005-08-12 14:17:50 +08004866
Albert Lee14be71f2005-09-27 17:36:35 +08004867 ap->hsm_task_state = HSM_ST_LAST;
Albert Lee563a6e12005-08-12 14:17:50 +08004868 return;
4869 }
4870
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004871 sg = &qc->__sg[qc->cursg];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004872
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873 page = sg->page;
4874 offset = sg->offset + qc->cursg_ofs;
4875
4876 /* get the current page and offset */
4877 page = nth_page(page, (offset >> PAGE_SHIFT));
4878 offset %= PAGE_SIZE;
4879
Albert Lee6952df02005-06-06 15:56:03 +08004880 /* don't overrun current sg */
Albert Lee32529e02005-05-26 03:49:42 -04004881 count = min(sg->length - qc->cursg_ofs, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882
4883 /* don't cross page boundaries */
4884 count = min(count, (unsigned int)PAGE_SIZE - offset);
4885
Albert Lee7282aa42005-10-09 09:46:07 -04004886 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
4887
Albert Lee91b8b312005-10-09 09:48:44 -04004888 if (PageHighMem(page)) {
4889 unsigned long flags;
Albert Lee7282aa42005-10-09 09:46:07 -04004890
Alan Coxa6b2c5d2006-05-22 16:59:59 +01004891 /* FIXME: use bounce buffer */
Albert Lee91b8b312005-10-09 09:48:44 -04004892 local_irq_save(flags);
4893 buf = kmap_atomic(page, KM_IRQ0);
Albert Lee083958d2005-10-09 09:47:31 -04004894
Albert Lee91b8b312005-10-09 09:48:44 -04004895 /* do the actual data transfer */
Alan Coxa6b2c5d2006-05-22 16:59:59 +01004896 ap->ops->data_xfer(qc->dev, buf + offset, count, do_write);
Albert Lee91b8b312005-10-09 09:48:44 -04004897
4898 kunmap_atomic(buf, KM_IRQ0);
4899 local_irq_restore(flags);
4900 } else {
4901 buf = page_address(page);
Alan Coxa6b2c5d2006-05-22 16:59:59 +01004902 ap->ops->data_xfer(qc->dev, buf + offset, count, do_write);
Albert Lee91b8b312005-10-09 09:48:44 -04004903 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004904
4905 bytes -= count;
4906 qc->curbytes += count;
4907 qc->cursg_ofs += count;
4908
Albert Lee32529e02005-05-26 03:49:42 -04004909 if (qc->cursg_ofs == sg->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910 qc->cursg++;
4911 qc->cursg_ofs = 0;
4912 }
4913
Albert Lee563a6e12005-08-12 14:17:50 +08004914 if (bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915 goto next_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916}
4917
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004918/**
4919 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
4920 * @qc: Command on going
4921 *
4922 * Transfer Transfer data from/to the ATAPI device.
4923 *
4924 * LOCKING:
4925 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004926 */
4927
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928static void atapi_pio_bytes(struct ata_queued_cmd *qc)
4929{
4930 struct ata_port *ap = qc->ap;
4931 struct ata_device *dev = qc->dev;
4932 unsigned int ireason, bc_lo, bc_hi, bytes;
4933 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
4934
Albert Leeeec4c3f2006-05-18 17:51:10 +08004935 /* Abuse qc->result_tf for temp storage of intermediate TF
4936 * here to save some kernel stack usage.
4937 * For normal completion, qc->result_tf is not relevant. For
4938 * error, qc->result_tf is later overwritten by ata_qc_complete().
4939 * So, the correctness of qc->result_tf is not affected.
4940 */
4941 ap->ops->tf_read(ap, &qc->result_tf);
4942 ireason = qc->result_tf.nsect;
4943 bc_lo = qc->result_tf.lbam;
4944 bc_hi = qc->result_tf.lbah;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004945 bytes = (bc_hi << 8) | bc_lo;
4946
4947 /* shall be cleared to zero, indicating xfer of data */
4948 if (ireason & (1 << 0))
4949 goto err_out;
4950
4951 /* make sure transfer direction matches expected */
4952 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
4953 if (do_write != i_write)
4954 goto err_out;
4955
Tejun Heo44877b42007-02-21 01:06:51 +09004956 VPRINTK("ata%u: xfering %d bytes\n", ap->print_id, bytes);
Albert Lee312f7da2005-09-27 17:38:03 +08004957
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958 __atapi_pio_bytes(qc, bytes);
Albert Lee4cc980b2007-08-15 03:19:45 -04004959 ata_altstatus(ap); /* flush */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004960
4961 return;
4962
4963err_out:
Tejun Heof15a1da2006-05-15 20:57:56 +09004964 ata_dev_printk(dev, KERN_INFO, "ATAPI check failed\n");
Tejun Heo11a56d22006-01-23 13:09:36 +09004965 qc->err_mask |= AC_ERR_HSM;
Albert Lee14be71f2005-09-27 17:36:35 +08004966 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004967}
4968
4969/**
Albert Leec234fb02006-03-25 17:58:38 +08004970 * ata_hsm_ok_in_wq - Check if the qc can be handled in the workqueue.
4971 * @ap: the target ata_port
4972 * @qc: qc on going
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973 *
Albert Leec234fb02006-03-25 17:58:38 +08004974 * RETURNS:
4975 * 1 if ok in workqueue, 0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976 */
Albert Leec234fb02006-03-25 17:58:38 +08004977
4978static inline int ata_hsm_ok_in_wq(struct ata_port *ap, struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979{
Albert Leec234fb02006-03-25 17:58:38 +08004980 if (qc->tf.flags & ATA_TFLAG_POLLING)
4981 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982
Albert Leec234fb02006-03-25 17:58:38 +08004983 if (ap->hsm_task_state == HSM_ST_FIRST) {
4984 if (qc->tf.protocol == ATA_PROT_PIO &&
4985 (qc->tf.flags & ATA_TFLAG_WRITE))
4986 return 1;
4987
4988 if (is_atapi_taskfile(&qc->tf) &&
4989 !(qc->dev->flags & ATA_DFLAG_CDB_INTR))
4990 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004991 }
4992
Albert Leec234fb02006-03-25 17:58:38 +08004993 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994}
4995
Albert Leec234fb02006-03-25 17:58:38 +08004996/**
Tejun Heoc17ea202006-05-15 20:59:29 +09004997 * ata_hsm_qc_complete - finish a qc running on standard HSM
4998 * @qc: Command to complete
4999 * @in_wq: 1 if called from workqueue, 0 otherwise
5000 *
5001 * Finish @qc which is running on standard HSM.
5002 *
5003 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04005004 * If @in_wq is zero, spin_lock_irqsave(host lock).
Tejun Heoc17ea202006-05-15 20:59:29 +09005005 * Otherwise, none on entry and grabs host lock.
5006 */
5007static void ata_hsm_qc_complete(struct ata_queued_cmd *qc, int in_wq)
5008{
5009 struct ata_port *ap = qc->ap;
5010 unsigned long flags;
5011
5012 if (ap->ops->error_handler) {
5013 if (in_wq) {
Jeff Garzikba6a1302006-06-22 23:46:10 -04005014 spin_lock_irqsave(ap->lock, flags);
Tejun Heoc17ea202006-05-15 20:59:29 +09005015
Jeff Garzikcca39742006-08-24 03:19:22 -04005016 /* EH might have kicked in while host lock is
5017 * released.
Tejun Heoc17ea202006-05-15 20:59:29 +09005018 */
5019 qc = ata_qc_from_tag(ap, qc->tag);
5020 if (qc) {
5021 if (likely(!(qc->err_mask & AC_ERR_HSM))) {
Akira Iguchi83625002007-01-26 16:27:32 +09005022 ap->ops->irq_on(ap);
Tejun Heoc17ea202006-05-15 20:59:29 +09005023 ata_qc_complete(qc);
5024 } else
5025 ata_port_freeze(ap);
5026 }
5027
Jeff Garzikba6a1302006-06-22 23:46:10 -04005028 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoc17ea202006-05-15 20:59:29 +09005029 } else {
5030 if (likely(!(qc->err_mask & AC_ERR_HSM)))
5031 ata_qc_complete(qc);
5032 else
5033 ata_port_freeze(ap);
5034 }
5035 } else {
5036 if (in_wq) {
Jeff Garzikba6a1302006-06-22 23:46:10 -04005037 spin_lock_irqsave(ap->lock, flags);
Akira Iguchi83625002007-01-26 16:27:32 +09005038 ap->ops->irq_on(ap);
Tejun Heoc17ea202006-05-15 20:59:29 +09005039 ata_qc_complete(qc);
Jeff Garzikba6a1302006-06-22 23:46:10 -04005040 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoc17ea202006-05-15 20:59:29 +09005041 } else
5042 ata_qc_complete(qc);
5043 }
5044}
5045
5046/**
Albert Leebb5cb292006-03-25 17:48:02 +08005047 * ata_hsm_move - move the HSM to the next state.
5048 * @ap: the target ata_port
5049 * @qc: qc on going
5050 * @status: current device status
5051 * @in_wq: 1 if called from workqueue, 0 otherwise
5052 *
5053 * RETURNS:
5054 * 1 when poll next status needed, 0 otherwise.
5055 */
Tejun Heo9a1004d2006-05-31 18:27:52 +09005056int ata_hsm_move(struct ata_port *ap, struct ata_queued_cmd *qc,
5057 u8 status, int in_wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005058{
Albert Leebb5cb292006-03-25 17:48:02 +08005059 unsigned long flags = 0;
5060 int poll_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061
Albert Lee6912ccd2006-03-25 17:45:49 +08005062 WARN_ON((qc->flags & ATA_QCFLAG_ACTIVE) == 0);
Albert Lee0565c262006-02-13 18:55:25 +08005063
Albert Leebb5cb292006-03-25 17:48:02 +08005064 /* Make sure ata_qc_issue_prot() does not throw things
5065 * like DMA polling into the workqueue. Notice that
5066 * in_wq is not equivalent to (qc->tf.flags & ATA_TFLAG_POLLING).
Albert Lee1c848982005-12-05 15:40:15 +08005067 */
Albert Leec234fb02006-03-25 17:58:38 +08005068 WARN_ON(in_wq != ata_hsm_ok_in_wq(ap, qc));
Albert Lee1c848982005-12-05 15:40:15 +08005069
Albert Leee2cec772006-03-25 17:43:49 +08005070fsm_start:
Albert Lee999bb6f2006-03-25 18:07:48 +08005071 DPRINTK("ata%u: protocol %d task_state %d (dev_stat 0x%X)\n",
Tejun Heo44877b42007-02-21 01:06:51 +09005072 ap->print_id, qc->tf.protocol, ap->hsm_task_state, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073
Albert Leee2cec772006-03-25 17:43:49 +08005074 switch (ap->hsm_task_state) {
5075 case HSM_ST_FIRST:
Albert Leebb5cb292006-03-25 17:48:02 +08005076 /* Send first data block or PACKET CDB */
5077
5078 /* If polling, we will stay in the work queue after
5079 * sending the data. Otherwise, interrupt handler
5080 * takes over after sending the data.
5081 */
5082 poll_next = (qc->tf.flags & ATA_TFLAG_POLLING);
5083
Albert Leee2cec772006-03-25 17:43:49 +08005084 /* check device status */
Albert Lee3655d1d2006-05-19 11:43:04 +08005085 if (unlikely((status & ATA_DRQ) == 0)) {
5086 /* handle BSY=0, DRQ=0 as error */
5087 if (likely(status & (ATA_ERR | ATA_DF)))
5088 /* device stops HSM for abort/error */
5089 qc->err_mask |= AC_ERR_DEV;
5090 else
5091 /* HSM violation. Let EH handle this */
5092 qc->err_mask |= AC_ERR_HSM;
5093
Albert Leee2cec772006-03-25 17:43:49 +08005094 ap->hsm_task_state = HSM_ST_ERR;
5095 goto fsm_start;
5096 }
5097
Albert Lee71601952006-03-25 18:11:12 +08005098 /* Device should not ask for data transfer (DRQ=1)
5099 * when it finds something wrong.
Albert Leeeee6c322006-04-01 17:38:43 +08005100 * We ignore DRQ here and stop the HSM by
5101 * changing hsm_task_state to HSM_ST_ERR and
5102 * let the EH abort the command or reset the device.
Albert Lee71601952006-03-25 18:11:12 +08005103 */
5104 if (unlikely(status & (ATA_ERR | ATA_DF))) {
Tejun Heo44877b42007-02-21 01:06:51 +09005105 ata_port_printk(ap, KERN_WARNING, "DRQ=1 with device "
5106 "error, dev_stat 0x%X\n", status);
Albert Lee3655d1d2006-05-19 11:43:04 +08005107 qc->err_mask |= AC_ERR_HSM;
Albert Leeeee6c322006-04-01 17:38:43 +08005108 ap->hsm_task_state = HSM_ST_ERR;
5109 goto fsm_start;
Albert Lee71601952006-03-25 18:11:12 +08005110 }
5111
Albert Leebb5cb292006-03-25 17:48:02 +08005112 /* Send the CDB (atapi) or the first data block (ata pio out).
5113 * During the state transition, interrupt handler shouldn't
5114 * be invoked before the data transfer is complete and
5115 * hsm_task_state is changed. Hence, the following locking.
5116 */
5117 if (in_wq)
Jeff Garzikba6a1302006-06-22 23:46:10 -04005118 spin_lock_irqsave(ap->lock, flags);
Albert Leee2cec772006-03-25 17:43:49 +08005119
Albert Leebb5cb292006-03-25 17:48:02 +08005120 if (qc->tf.protocol == ATA_PROT_PIO) {
5121 /* PIO data out protocol.
5122 * send first data block.
5123 */
5124
5125 /* ata_pio_sectors() might change the state
5126 * to HSM_ST_LAST. so, the state is changed here
5127 * before ata_pio_sectors().
5128 */
5129 ap->hsm_task_state = HSM_ST;
5130 ata_pio_sectors(qc);
Albert Leebb5cb292006-03-25 17:48:02 +08005131 } else
5132 /* send CDB */
5133 atapi_send_cdb(ap, qc);
5134
5135 if (in_wq)
Jeff Garzikba6a1302006-06-22 23:46:10 -04005136 spin_unlock_irqrestore(ap->lock, flags);
Albert Leebb5cb292006-03-25 17:48:02 +08005137
5138 /* if polling, ata_pio_task() handles the rest.
5139 * otherwise, interrupt handler takes over from here.
5140 */
Albert Leee2cec772006-03-25 17:43:49 +08005141 break;
5142
5143 case HSM_ST:
5144 /* complete command or read/write the data register */
5145 if (qc->tf.protocol == ATA_PROT_ATAPI) {
5146 /* ATAPI PIO protocol */
5147 if ((status & ATA_DRQ) == 0) {
Albert Lee3655d1d2006-05-19 11:43:04 +08005148 /* No more data to transfer or device error.
5149 * Device error will be tagged in HSM_ST_LAST.
5150 */
Albert Leee2cec772006-03-25 17:43:49 +08005151 ap->hsm_task_state = HSM_ST_LAST;
5152 goto fsm_start;
5153 }
5154
Albert Lee71601952006-03-25 18:11:12 +08005155 /* Device should not ask for data transfer (DRQ=1)
5156 * when it finds something wrong.
Albert Leeeee6c322006-04-01 17:38:43 +08005157 * We ignore DRQ here and stop the HSM by
5158 * changing hsm_task_state to HSM_ST_ERR and
5159 * let the EH abort the command or reset the device.
Albert Lee71601952006-03-25 18:11:12 +08005160 */
5161 if (unlikely(status & (ATA_ERR | ATA_DF))) {
Tejun Heo44877b42007-02-21 01:06:51 +09005162 ata_port_printk(ap, KERN_WARNING, "DRQ=1 with "
5163 "device error, dev_stat 0x%X\n",
5164 status);
Albert Lee3655d1d2006-05-19 11:43:04 +08005165 qc->err_mask |= AC_ERR_HSM;
Albert Leeeee6c322006-04-01 17:38:43 +08005166 ap->hsm_task_state = HSM_ST_ERR;
5167 goto fsm_start;
Albert Lee71601952006-03-25 18:11:12 +08005168 }
5169
Albert Leee2cec772006-03-25 17:43:49 +08005170 atapi_pio_bytes(qc);
5171
5172 if (unlikely(ap->hsm_task_state == HSM_ST_ERR))
5173 /* bad ireason reported by device */
5174 goto fsm_start;
5175
5176 } else {
5177 /* ATA PIO protocol */
5178 if (unlikely((status & ATA_DRQ) == 0)) {
5179 /* handle BSY=0, DRQ=0 as error */
Albert Lee3655d1d2006-05-19 11:43:04 +08005180 if (likely(status & (ATA_ERR | ATA_DF)))
5181 /* device stops HSM for abort/error */
5182 qc->err_mask |= AC_ERR_DEV;
5183 else
Tejun Heo55a8e2c2006-11-10 18:08:10 +09005184 /* HSM violation. Let EH handle this.
5185 * Phantom devices also trigger this
5186 * condition. Mark hint.
5187 */
5188 qc->err_mask |= AC_ERR_HSM |
5189 AC_ERR_NODEV_HINT;
Albert Lee3655d1d2006-05-19 11:43:04 +08005190
Albert Leee2cec772006-03-25 17:43:49 +08005191 ap->hsm_task_state = HSM_ST_ERR;
5192 goto fsm_start;
5193 }
5194
Albert Leeeee6c322006-04-01 17:38:43 +08005195 /* For PIO reads, some devices may ask for
5196 * data transfer (DRQ=1) alone with ERR=1.
5197 * We respect DRQ here and transfer one
5198 * block of junk data before changing the
5199 * hsm_task_state to HSM_ST_ERR.
5200 *
5201 * For PIO writes, ERR=1 DRQ=1 doesn't make
5202 * sense since the data block has been
5203 * transferred to the device.
Albert Lee71601952006-03-25 18:11:12 +08005204 */
5205 if (unlikely(status & (ATA_ERR | ATA_DF))) {
Albert Lee71601952006-03-25 18:11:12 +08005206 /* data might be corrputed */
5207 qc->err_mask |= AC_ERR_DEV;
Albert Leeeee6c322006-04-01 17:38:43 +08005208
5209 if (!(qc->tf.flags & ATA_TFLAG_WRITE)) {
5210 ata_pio_sectors(qc);
Albert Leeeee6c322006-04-01 17:38:43 +08005211 status = ata_wait_idle(ap);
5212 }
5213
Albert Lee3655d1d2006-05-19 11:43:04 +08005214 if (status & (ATA_BUSY | ATA_DRQ))
5215 qc->err_mask |= AC_ERR_HSM;
5216
Albert Leeeee6c322006-04-01 17:38:43 +08005217 /* ata_pio_sectors() might change the
5218 * state to HSM_ST_LAST. so, the state
5219 * is changed after ata_pio_sectors().
5220 */
5221 ap->hsm_task_state = HSM_ST_ERR;
5222 goto fsm_start;
Albert Lee71601952006-03-25 18:11:12 +08005223 }
5224
Albert Leee2cec772006-03-25 17:43:49 +08005225 ata_pio_sectors(qc);
5226
5227 if (ap->hsm_task_state == HSM_ST_LAST &&
5228 (!(qc->tf.flags & ATA_TFLAG_WRITE))) {
5229 /* all data read */
Albert Lee52a32202006-03-25 18:18:15 +08005230 status = ata_wait_idle(ap);
Albert Leee2cec772006-03-25 17:43:49 +08005231 goto fsm_start;
5232 }
5233 }
5234
Albert Leebb5cb292006-03-25 17:48:02 +08005235 poll_next = 1;
Albert Leee2cec772006-03-25 17:43:49 +08005236 break;
5237
5238 case HSM_ST_LAST:
Albert Lee6912ccd2006-03-25 17:45:49 +08005239 if (unlikely(!ata_ok(status))) {
5240 qc->err_mask |= __ac_err_mask(status);
Albert Leee2cec772006-03-25 17:43:49 +08005241 ap->hsm_task_state = HSM_ST_ERR;
5242 goto fsm_start;
5243 }
5244
5245 /* no more data to transfer */
Albert Lee4332a772006-04-03 17:43:24 +08005246 DPRINTK("ata%u: dev %u command complete, drv_stat 0x%x\n",
Tejun Heo44877b42007-02-21 01:06:51 +09005247 ap->print_id, qc->dev->devno, status);
Albert Leee2cec772006-03-25 17:43:49 +08005248
Albert Lee6912ccd2006-03-25 17:45:49 +08005249 WARN_ON(qc->err_mask);
5250
Albert Leee2cec772006-03-25 17:43:49 +08005251 ap->hsm_task_state = HSM_ST_IDLE;
5252
5253 /* complete taskfile transaction */
Tejun Heoc17ea202006-05-15 20:59:29 +09005254 ata_hsm_qc_complete(qc, in_wq);
Albert Leebb5cb292006-03-25 17:48:02 +08005255
5256 poll_next = 0;
Albert Leee2cec772006-03-25 17:43:49 +08005257 break;
5258
5259 case HSM_ST_ERR:
Albert Leee2cec772006-03-25 17:43:49 +08005260 /* make sure qc->err_mask is available to
5261 * know what's wrong and recover
5262 */
5263 WARN_ON(qc->err_mask == 0);
5264
5265 ap->hsm_task_state = HSM_ST_IDLE;
Albert Leebb5cb292006-03-25 17:48:02 +08005266
Albert Lee999bb6f2006-03-25 18:07:48 +08005267 /* complete taskfile transaction */
Tejun Heoc17ea202006-05-15 20:59:29 +09005268 ata_hsm_qc_complete(qc, in_wq);
Albert Leebb5cb292006-03-25 17:48:02 +08005269
5270 poll_next = 0;
Albert Leee2cec772006-03-25 17:43:49 +08005271 break;
5272 default:
Albert Leebb5cb292006-03-25 17:48:02 +08005273 poll_next = 0;
Albert Lee6912ccd2006-03-25 17:45:49 +08005274 BUG();
Albert Leee2cec772006-03-25 17:43:49 +08005275 }
5276
Albert Leebb5cb292006-03-25 17:48:02 +08005277 return poll_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005278}
5279
David Howells65f27f32006-11-22 14:55:48 +00005280static void ata_pio_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005281{
David Howells65f27f32006-11-22 14:55:48 +00005282 struct ata_port *ap =
5283 container_of(work, struct ata_port, port_task.work);
5284 struct ata_queued_cmd *qc = ap->port_task_data;
Albert Leea1af3732006-03-25 17:50:15 +08005285 u8 status;
5286 int poll_next;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04005287
5288fsm_start:
Albert Leea1af3732006-03-25 17:50:15 +08005289 WARN_ON(ap->hsm_task_state == HSM_ST_IDLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005290
Albert Leea1af3732006-03-25 17:50:15 +08005291 /*
5292 * This is purely heuristic. This is a fast path.
5293 * Sometimes when we enter, BSY will be cleared in
5294 * a chk-status or two. If not, the drive is probably seeking
5295 * or something. Snooze for a couple msecs, then
5296 * chk-status again. If still busy, queue delayed work.
5297 */
5298 status = ata_busy_wait(ap, ATA_BUSY, 5);
5299 if (status & ATA_BUSY) {
5300 msleep(2);
5301 status = ata_busy_wait(ap, ATA_BUSY, 10);
5302 if (status & ATA_BUSY) {
Albert Lee31ce6da2006-04-03 18:31:44 +08005303 ata_port_queue_task(ap, ata_pio_task, qc, ATA_SHORT_PAUSE);
Albert Leea1af3732006-03-25 17:50:15 +08005304 return;
5305 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005306 }
5307
Albert Leea1af3732006-03-25 17:50:15 +08005308 /* move the HSM */
5309 poll_next = ata_hsm_move(ap, qc, status, 1);
5310
5311 /* another command or interrupt handler
5312 * may be running at this point.
5313 */
5314 if (poll_next)
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04005315 goto fsm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005316}
5317
Linus Torvalds1da177e2005-04-16 15:20:36 -07005318/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07005319 * ata_qc_new - Request an available ATA command, for queueing
5320 * @ap: Port associated with device @dev
5321 * @dev: Device from whom we request an available command structure
5322 *
5323 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04005324 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005325 */
5326
5327static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
5328{
5329 struct ata_queued_cmd *qc = NULL;
5330 unsigned int i;
5331
Tejun Heoe3180492006-05-15 20:58:09 +09005332 /* no command while frozen */
Tejun Heob51e9e52006-06-29 01:29:30 +09005333 if (unlikely(ap->pflags & ATA_PFLAG_FROZEN))
Tejun Heoe3180492006-05-15 20:58:09 +09005334 return NULL;
5335
Tejun Heo2ab7db12006-05-15 20:58:02 +09005336 /* the last tag is reserved for internal command. */
5337 for (i = 0; i < ATA_MAX_QUEUE - 1; i++)
Tejun Heo6cec4a32006-05-15 21:03:41 +09005338 if (!test_and_set_bit(i, &ap->qc_allocated)) {
Tejun Heof69499f2006-05-15 20:58:03 +09005339 qc = __ata_qc_from_tag(ap, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340 break;
5341 }
5342
5343 if (qc)
5344 qc->tag = i;
5345
5346 return qc;
5347}
5348
5349/**
5350 * ata_qc_new_init - Request an available ATA command, and initialize it
Linus Torvalds1da177e2005-04-16 15:20:36 -07005351 * @dev: Device from whom we request an available command structure
5352 *
5353 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04005354 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005355 */
5356
Tejun Heo3373efd2006-05-15 20:57:53 +09005357struct ata_queued_cmd *ata_qc_new_init(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005358{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005359 struct ata_port *ap = dev->link->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005360 struct ata_queued_cmd *qc;
5361
5362 qc = ata_qc_new(ap);
5363 if (qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364 qc->scsicmd = NULL;
5365 qc->ap = ap;
5366 qc->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005367
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05005368 ata_qc_reinit(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005369 }
5370
5371 return qc;
5372}
5373
Linus Torvalds1da177e2005-04-16 15:20:36 -07005374/**
5375 * ata_qc_free - free unused ata_queued_cmd
5376 * @qc: Command to complete
5377 *
5378 * Designed to free unused ata_queued_cmd object
5379 * in case something prevents using it.
5380 *
5381 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04005382 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005383 */
5384void ata_qc_free(struct ata_queued_cmd *qc)
5385{
Tejun Heo4ba946e2006-01-23 13:09:36 +09005386 struct ata_port *ap = qc->ap;
5387 unsigned int tag;
5388
Tejun Heoa46314742006-02-11 19:11:13 +09005389 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005390
Tejun Heo4ba946e2006-01-23 13:09:36 +09005391 qc->flags = 0;
5392 tag = qc->tag;
5393 if (likely(ata_tag_valid(tag))) {
Tejun Heo4ba946e2006-01-23 13:09:36 +09005394 qc->tag = ATA_TAG_POISON;
Tejun Heo6cec4a32006-05-15 21:03:41 +09005395 clear_bit(tag, &ap->qc_allocated);
Tejun Heo4ba946e2006-01-23 13:09:36 +09005396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005397}
5398
Tejun Heo76014422006-02-11 15:13:49 +09005399void __ata_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400{
Tejun Heodedaf2b2006-05-15 21:03:43 +09005401 struct ata_port *ap = qc->ap;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005402 struct ata_link *link = qc->dev->link;
Tejun Heodedaf2b2006-05-15 21:03:43 +09005403
Tejun Heoa46314742006-02-11 19:11:13 +09005404 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
5405 WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005406
5407 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
5408 ata_sg_clean(qc);
5409
Tejun Heo7401abf2006-05-15 20:57:32 +09005410 /* command should be marked inactive atomically with qc completion */
Tejun Heodedaf2b2006-05-15 21:03:43 +09005411 if (qc->tf.protocol == ATA_PROT_NCQ)
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005412 link->sactive &= ~(1 << qc->tag);
Tejun Heodedaf2b2006-05-15 21:03:43 +09005413 else
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005414 link->active_tag = ATA_TAG_POISON;
Tejun Heo7401abf2006-05-15 20:57:32 +09005415
Albert Lee3f3791d2005-08-16 14:25:38 +08005416 /* atapi: mark qc as inactive to prevent the interrupt handler
5417 * from completing the command twice later, before the error handler
5418 * is called. (when rc != 0 and atapi request sense is needed)
5419 */
5420 qc->flags &= ~ATA_QCFLAG_ACTIVE;
Tejun Heodedaf2b2006-05-15 21:03:43 +09005421 ap->qc_active &= ~(1 << qc->tag);
Albert Lee3f3791d2005-08-16 14:25:38 +08005422
Linus Torvalds1da177e2005-04-16 15:20:36 -07005423 /* call completion callback */
Tejun Heo77853bf2006-01-23 13:09:36 +09005424 qc->complete_fn(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425}
5426
Tejun Heo39599a52006-11-14 22:37:35 +09005427static void fill_result_tf(struct ata_queued_cmd *qc)
5428{
5429 struct ata_port *ap = qc->ap;
5430
Tejun Heo39599a52006-11-14 22:37:35 +09005431 qc->result_tf.flags = qc->tf.flags;
Mark Lord4742d542007-04-02 16:20:35 -04005432 ap->ops->tf_read(ap, &qc->result_tf);
Tejun Heo39599a52006-11-14 22:37:35 +09005433}
5434
Tejun Heof686bcb2006-05-15 20:58:05 +09005435/**
5436 * ata_qc_complete - Complete an active ATA command
5437 * @qc: Command to complete
5438 * @err_mask: ATA Status register contents
5439 *
5440 * Indicate to the mid and upper layers that an ATA
5441 * command has completed, with either an ok or not-ok status.
5442 *
5443 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04005444 * spin_lock_irqsave(host lock)
Tejun Heof686bcb2006-05-15 20:58:05 +09005445 */
5446void ata_qc_complete(struct ata_queued_cmd *qc)
5447{
5448 struct ata_port *ap = qc->ap;
5449
5450 /* XXX: New EH and old EH use different mechanisms to
5451 * synchronize EH with regular execution path.
5452 *
5453 * In new EH, a failed qc is marked with ATA_QCFLAG_FAILED.
5454 * Normal execution path is responsible for not accessing a
5455 * failed qc. libata core enforces the rule by returning NULL
5456 * from ata_qc_from_tag() for failed qcs.
5457 *
5458 * Old EH depends on ata_qc_complete() nullifying completion
5459 * requests if ATA_QCFLAG_EH_SCHEDULED is set. Old EH does
5460 * not synchronize with interrupt handler. Only PIO task is
5461 * taken care of.
5462 */
5463 if (ap->ops->error_handler) {
Tejun Heob51e9e52006-06-29 01:29:30 +09005464 WARN_ON(ap->pflags & ATA_PFLAG_FROZEN);
Tejun Heof686bcb2006-05-15 20:58:05 +09005465
5466 if (unlikely(qc->err_mask))
5467 qc->flags |= ATA_QCFLAG_FAILED;
5468
5469 if (unlikely(qc->flags & ATA_QCFLAG_FAILED)) {
5470 if (!ata_tag_internal(qc->tag)) {
5471 /* always fill result TF for failed qc */
Tejun Heo39599a52006-11-14 22:37:35 +09005472 fill_result_tf(qc);
Tejun Heof686bcb2006-05-15 20:58:05 +09005473 ata_qc_schedule_eh(qc);
5474 return;
5475 }
5476 }
5477
5478 /* read result TF if requested */
5479 if (qc->flags & ATA_QCFLAG_RESULT_TF)
Tejun Heo39599a52006-11-14 22:37:35 +09005480 fill_result_tf(qc);
Tejun Heof686bcb2006-05-15 20:58:05 +09005481
5482 __ata_qc_complete(qc);
5483 } else {
5484 if (qc->flags & ATA_QCFLAG_EH_SCHEDULED)
5485 return;
5486
5487 /* read result TF if failed or requested */
5488 if (qc->err_mask || qc->flags & ATA_QCFLAG_RESULT_TF)
Tejun Heo39599a52006-11-14 22:37:35 +09005489 fill_result_tf(qc);
Tejun Heof686bcb2006-05-15 20:58:05 +09005490
5491 __ata_qc_complete(qc);
5492 }
5493}
5494
Tejun Heodedaf2b2006-05-15 21:03:43 +09005495/**
5496 * ata_qc_complete_multiple - Complete multiple qcs successfully
5497 * @ap: port in question
5498 * @qc_active: new qc_active mask
5499 * @finish_qc: LLDD callback invoked before completing a qc
5500 *
5501 * Complete in-flight commands. This functions is meant to be
5502 * called from low-level driver's interrupt routine to complete
5503 * requests normally. ap->qc_active and @qc_active is compared
5504 * and commands are completed accordingly.
5505 *
5506 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04005507 * spin_lock_irqsave(host lock)
Tejun Heodedaf2b2006-05-15 21:03:43 +09005508 *
5509 * RETURNS:
5510 * Number of completed commands on success, -errno otherwise.
5511 */
5512int ata_qc_complete_multiple(struct ata_port *ap, u32 qc_active,
5513 void (*finish_qc)(struct ata_queued_cmd *))
5514{
5515 int nr_done = 0;
5516 u32 done_mask;
5517 int i;
5518
5519 done_mask = ap->qc_active ^ qc_active;
5520
5521 if (unlikely(done_mask & qc_active)) {
5522 ata_port_printk(ap, KERN_ERR, "illegal qc_active transition "
5523 "(%08x->%08x)\n", ap->qc_active, qc_active);
5524 return -EINVAL;
5525 }
5526
5527 for (i = 0; i < ATA_MAX_QUEUE; i++) {
5528 struct ata_queued_cmd *qc;
5529
5530 if (!(done_mask & (1 << i)))
5531 continue;
5532
5533 if ((qc = ata_qc_from_tag(ap, i))) {
5534 if (finish_qc)
5535 finish_qc(qc);
5536 ata_qc_complete(qc);
5537 nr_done++;
5538 }
5539 }
5540
5541 return nr_done;
5542}
5543
Linus Torvalds1da177e2005-04-16 15:20:36 -07005544static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
5545{
5546 struct ata_port *ap = qc->ap;
5547
5548 switch (qc->tf.protocol) {
Tejun Heo3dc1d882006-05-15 21:03:45 +09005549 case ATA_PROT_NCQ:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005550 case ATA_PROT_DMA:
5551 case ATA_PROT_ATAPI_DMA:
5552 return 1;
5553
5554 case ATA_PROT_ATAPI:
5555 case ATA_PROT_PIO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005556 if (ap->flags & ATA_FLAG_PIO_DMA)
5557 return 1;
5558
5559 /* fall through */
5560
5561 default:
5562 return 0;
5563 }
5564
5565 /* never reached */
5566}
5567
5568/**
5569 * ata_qc_issue - issue taskfile to device
5570 * @qc: command to issue to device
5571 *
5572 * Prepare an ATA command to submission to device.
5573 * This includes mapping the data into a DMA-able
5574 * area, filling in the S/G table, and finally
5575 * writing the taskfile to hardware, starting the command.
5576 *
5577 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04005578 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005579 */
Tejun Heo8e0e6942006-03-31 20:41:11 +09005580void ata_qc_issue(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005581{
5582 struct ata_port *ap = qc->ap;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005583 struct ata_link *link = qc->dev->link;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005584
Tejun Heodedaf2b2006-05-15 21:03:43 +09005585 /* Make sure only one non-NCQ command is outstanding. The
5586 * check is skipped for old EH because it reuses active qc to
5587 * request ATAPI sense.
5588 */
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005589 WARN_ON(ap->ops->error_handler && ata_tag_valid(link->active_tag));
Tejun Heodedaf2b2006-05-15 21:03:43 +09005590
5591 if (qc->tf.protocol == ATA_PROT_NCQ) {
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005592 WARN_ON(link->sactive & (1 << qc->tag));
5593 link->sactive |= 1 << qc->tag;
Tejun Heodedaf2b2006-05-15 21:03:43 +09005594 } else {
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005595 WARN_ON(link->sactive);
5596 link->active_tag = qc->tag;
Tejun Heodedaf2b2006-05-15 21:03:43 +09005597 }
5598
Tejun Heoe4a70e72006-03-31 20:36:47 +09005599 qc->flags |= ATA_QCFLAG_ACTIVE;
Tejun Heodedaf2b2006-05-15 21:03:43 +09005600 ap->qc_active |= 1 << qc->tag;
Tejun Heoe4a70e72006-03-31 20:36:47 +09005601
Linus Torvalds1da177e2005-04-16 15:20:36 -07005602 if (ata_should_dma_map(qc)) {
5603 if (qc->flags & ATA_QCFLAG_SG) {
5604 if (ata_sg_setup(qc))
Tejun Heo8e436af2006-01-23 13:09:36 +09005605 goto sg_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005606 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
5607 if (ata_sg_setup_one(qc))
Tejun Heo8e436af2006-01-23 13:09:36 +09005608 goto sg_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005609 }
5610 } else {
5611 qc->flags &= ~ATA_QCFLAG_DMAMAP;
5612 }
5613
5614 ap->ops->qc_prep(qc);
5615
Tejun Heo8e0e6942006-03-31 20:41:11 +09005616 qc->err_mask |= ap->ops->qc_issue(qc);
5617 if (unlikely(qc->err_mask))
5618 goto err;
5619 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005620
Tejun Heo8e436af2006-01-23 13:09:36 +09005621sg_err:
5622 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Tejun Heo8e0e6942006-03-31 20:41:11 +09005623 qc->err_mask |= AC_ERR_SYSTEM;
5624err:
5625 ata_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005626}
5627
5628/**
5629 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
5630 * @qc: command to issue to device
5631 *
5632 * Using various libata functions and hooks, this function
5633 * starts an ATA command. ATA commands are grouped into
5634 * classes called "protocols", and issuing each type of protocol
5635 * is slightly different.
5636 *
Edward Falk0baab862005-06-02 18:17:13 -04005637 * May be used as the qc_issue() entry in ata_port_operations.
5638 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005639 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04005640 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005641 *
5642 * RETURNS:
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09005643 * Zero on success, AC_ERR_* mask on failure
Linus Torvalds1da177e2005-04-16 15:20:36 -07005644 */
5645
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09005646unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005647{
5648 struct ata_port *ap = qc->ap;
5649
Albert Leee50362e2005-09-27 17:39:50 +08005650 /* Use polling pio if the LLD doesn't handle
5651 * interrupt driven pio and atapi CDB interrupt.
5652 */
5653 if (ap->flags & ATA_FLAG_PIO_POLLING) {
5654 switch (qc->tf.protocol) {
5655 case ATA_PROT_PIO:
Albert Leee3472cb2006-12-07 11:37:58 +08005656 case ATA_PROT_NODATA:
Albert Leee50362e2005-09-27 17:39:50 +08005657 case ATA_PROT_ATAPI:
5658 case ATA_PROT_ATAPI_NODATA:
5659 qc->tf.flags |= ATA_TFLAG_POLLING;
5660 break;
5661 case ATA_PROT_ATAPI_DMA:
5662 if (qc->dev->flags & ATA_DFLAG_CDB_INTR)
Albert Lee3a778272006-06-22 13:00:25 +08005663 /* see ata_dma_blacklisted() */
Albert Leee50362e2005-09-27 17:39:50 +08005664 BUG();
5665 break;
5666 default:
5667 break;
5668 }
5669 }
5670
Albert Lee312f7da2005-09-27 17:38:03 +08005671 /* select the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005672 ata_dev_select(ap, qc->dev->devno, 1, 0);
5673
Albert Lee312f7da2005-09-27 17:38:03 +08005674 /* start the command */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005675 switch (qc->tf.protocol) {
5676 case ATA_PROT_NODATA:
Albert Lee312f7da2005-09-27 17:38:03 +08005677 if (qc->tf.flags & ATA_TFLAG_POLLING)
5678 ata_qc_set_polling(qc);
5679
Jeff Garzike5338252005-10-30 21:37:17 -05005680 ata_tf_to_host(ap, &qc->tf);
Albert Lee312f7da2005-09-27 17:38:03 +08005681 ap->hsm_task_state = HSM_ST_LAST;
5682
5683 if (qc->tf.flags & ATA_TFLAG_POLLING)
Albert Lee31ce6da2006-04-03 18:31:44 +08005684 ata_port_queue_task(ap, ata_pio_task, qc, 0);
Albert Lee312f7da2005-09-27 17:38:03 +08005685
Linus Torvalds1da177e2005-04-16 15:20:36 -07005686 break;
5687
5688 case ATA_PROT_DMA:
Jeff Garzik587005d2006-02-11 18:17:32 -05005689 WARN_ON(qc->tf.flags & ATA_TFLAG_POLLING);
Albert Lee312f7da2005-09-27 17:38:03 +08005690
Linus Torvalds1da177e2005-04-16 15:20:36 -07005691 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
5692 ap->ops->bmdma_setup(qc); /* set up bmdma */
5693 ap->ops->bmdma_start(qc); /* initiate bmdma */
Albert Lee312f7da2005-09-27 17:38:03 +08005694 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005695 break;
5696
Albert Lee312f7da2005-09-27 17:38:03 +08005697 case ATA_PROT_PIO:
5698 if (qc->tf.flags & ATA_TFLAG_POLLING)
5699 ata_qc_set_polling(qc);
5700
Jeff Garzike5338252005-10-30 21:37:17 -05005701 ata_tf_to_host(ap, &qc->tf);
Albert Lee312f7da2005-09-27 17:38:03 +08005702
Albert Lee54f00382005-09-30 19:14:19 +08005703 if (qc->tf.flags & ATA_TFLAG_WRITE) {
5704 /* PIO data out protocol */
5705 ap->hsm_task_state = HSM_ST_FIRST;
Albert Lee31ce6da2006-04-03 18:31:44 +08005706 ata_port_queue_task(ap, ata_pio_task, qc, 0);
Albert Lee54f00382005-09-30 19:14:19 +08005707
5708 /* always send first data block using
Albert Leee27486d2005-11-01 19:24:49 +08005709 * the ata_pio_task() codepath.
Albert Lee54f00382005-09-30 19:14:19 +08005710 */
Albert Lee312f7da2005-09-27 17:38:03 +08005711 } else {
Albert Lee54f00382005-09-30 19:14:19 +08005712 /* PIO data in protocol */
5713 ap->hsm_task_state = HSM_ST;
Albert Lee312f7da2005-09-27 17:38:03 +08005714
Albert Lee54f00382005-09-30 19:14:19 +08005715 if (qc->tf.flags & ATA_TFLAG_POLLING)
Albert Lee31ce6da2006-04-03 18:31:44 +08005716 ata_port_queue_task(ap, ata_pio_task, qc, 0);
Albert Lee312f7da2005-09-27 17:38:03 +08005717
Albert Lee54f00382005-09-30 19:14:19 +08005718 /* if polling, ata_pio_task() handles the rest.
5719 * otherwise, interrupt handler takes over from here.
5720 */
Albert Lee312f7da2005-09-27 17:38:03 +08005721 }
5722
Linus Torvalds1da177e2005-04-16 15:20:36 -07005723 break;
5724
5725 case ATA_PROT_ATAPI:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005726 case ATA_PROT_ATAPI_NODATA:
Albert Lee312f7da2005-09-27 17:38:03 +08005727 if (qc->tf.flags & ATA_TFLAG_POLLING)
5728 ata_qc_set_polling(qc);
5729
Jeff Garzike5338252005-10-30 21:37:17 -05005730 ata_tf_to_host(ap, &qc->tf);
Jeff Garzikf6ef65e2006-01-27 02:45:00 -05005731
Albert Lee312f7da2005-09-27 17:38:03 +08005732 ap->hsm_task_state = HSM_ST_FIRST;
5733
5734 /* send cdb by polling if no cdb interrupt */
5735 if ((!(qc->dev->flags & ATA_DFLAG_CDB_INTR)) ||
5736 (qc->tf.flags & ATA_TFLAG_POLLING))
Albert Lee31ce6da2006-04-03 18:31:44 +08005737 ata_port_queue_task(ap, ata_pio_task, qc, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005738 break;
5739
5740 case ATA_PROT_ATAPI_DMA:
Jeff Garzik587005d2006-02-11 18:17:32 -05005741 WARN_ON(qc->tf.flags & ATA_TFLAG_POLLING);
Albert Lee312f7da2005-09-27 17:38:03 +08005742
Linus Torvalds1da177e2005-04-16 15:20:36 -07005743 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
5744 ap->ops->bmdma_setup(qc); /* set up bmdma */
Albert Lee312f7da2005-09-27 17:38:03 +08005745 ap->hsm_task_state = HSM_ST_FIRST;
5746
5747 /* send cdb by polling if no cdb interrupt */
5748 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
Albert Lee31ce6da2006-04-03 18:31:44 +08005749 ata_port_queue_task(ap, ata_pio_task, qc, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005750 break;
5751
5752 default:
5753 WARN_ON(1);
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09005754 return AC_ERR_SYSTEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005755 }
5756
5757 return 0;
5758}
5759
5760/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07005761 * ata_host_intr - Handle host interrupt for given (port, task)
5762 * @ap: Port on which interrupt arrived (possibly...)
5763 * @qc: Taskfile currently active in engine
5764 *
5765 * Handle host interrupt for given queued command. Currently,
5766 * only DMA interrupts are handled. All other commands are
5767 * handled via polling with interrupts disabled (nIEN bit).
5768 *
5769 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04005770 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005771 *
5772 * RETURNS:
5773 * One if interrupt was handled, zero if not (shared irq).
5774 */
5775
5776inline unsigned int ata_host_intr (struct ata_port *ap,
5777 struct ata_queued_cmd *qc)
5778{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005779 struct ata_eh_info *ehi = &ap->link.eh_info;
Albert Lee312f7da2005-09-27 17:38:03 +08005780 u8 status, host_stat = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005781
Albert Lee312f7da2005-09-27 17:38:03 +08005782 VPRINTK("ata%u: protocol %d task_state %d\n",
Tejun Heo44877b42007-02-21 01:06:51 +09005783 ap->print_id, qc->tf.protocol, ap->hsm_task_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005784
Albert Lee312f7da2005-09-27 17:38:03 +08005785 /* Check whether we are expecting interrupt in this state */
5786 switch (ap->hsm_task_state) {
5787 case HSM_ST_FIRST:
Albert Lee6912ccd2006-03-25 17:45:49 +08005788 /* Some pre-ATAPI-4 devices assert INTRQ
5789 * at this state when ready to receive CDB.
5790 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005791
Albert Lee312f7da2005-09-27 17:38:03 +08005792 /* Check the ATA_DFLAG_CDB_INTR flag is enough here.
5793 * The flag was turned on only for atapi devices.
5794 * No need to check is_atapi_taskfile(&qc->tf) again.
5795 */
5796 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005797 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005798 break;
Albert Lee312f7da2005-09-27 17:38:03 +08005799 case HSM_ST_LAST:
5800 if (qc->tf.protocol == ATA_PROT_DMA ||
5801 qc->tf.protocol == ATA_PROT_ATAPI_DMA) {
5802 /* check status of DMA engine */
5803 host_stat = ap->ops->bmdma_status(ap);
Tejun Heo44877b42007-02-21 01:06:51 +09005804 VPRINTK("ata%u: host_stat 0x%X\n",
5805 ap->print_id, host_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005806
Albert Lee312f7da2005-09-27 17:38:03 +08005807 /* if it's not our irq... */
5808 if (!(host_stat & ATA_DMA_INTR))
5809 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005810
Albert Lee312f7da2005-09-27 17:38:03 +08005811 /* before we do anything else, clear DMA-Start bit */
5812 ap->ops->bmdma_stop(qc);
Albert Leea4f16612005-12-26 16:40:53 +08005813
5814 if (unlikely(host_stat & ATA_DMA_ERR)) {
5815 /* error when transfering data to/from memory */
5816 qc->err_mask |= AC_ERR_HOST_BUS;
5817 ap->hsm_task_state = HSM_ST_ERR;
5818 }
Albert Lee312f7da2005-09-27 17:38:03 +08005819 }
5820 break;
5821 case HSM_ST:
5822 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005823 default:
5824 goto idle_irq;
5825 }
5826
Albert Lee312f7da2005-09-27 17:38:03 +08005827 /* check altstatus */
5828 status = ata_altstatus(ap);
5829 if (status & ATA_BUSY)
5830 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005831
Albert Lee312f7da2005-09-27 17:38:03 +08005832 /* check main status, clearing INTRQ */
5833 status = ata_chk_status(ap);
5834 if (unlikely(status & ATA_BUSY))
5835 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005836
Albert Lee312f7da2005-09-27 17:38:03 +08005837 /* ack bmdma irq events */
5838 ap->ops->irq_clear(ap);
5839
Albert Leebb5cb292006-03-25 17:48:02 +08005840 ata_hsm_move(ap, qc, status, 0);
Tejun Heoea547632006-11-17 12:06:21 +09005841
5842 if (unlikely(qc->err_mask) && (qc->tf.protocol == ATA_PROT_DMA ||
5843 qc->tf.protocol == ATA_PROT_ATAPI_DMA))
5844 ata_ehi_push_desc(ehi, "BMDMA stat 0x%x", host_stat);
5845
Linus Torvalds1da177e2005-04-16 15:20:36 -07005846 return 1; /* irq handled */
5847
5848idle_irq:
5849 ap->stats.idle_irq++;
5850
5851#ifdef ATA_IRQ_TRAP
5852 if ((ap->stats.idle_irq % 1000) == 0) {
Jeff Garzik6d32d302007-08-15 05:38:46 -04005853 ata_chk_status(ap);
5854 ap->ops->irq_clear(ap);
Tejun Heof15a1da2006-05-15 20:57:56 +09005855 ata_port_printk(ap, KERN_WARNING, "irq trap\n");
Alan Cox23cfce82006-03-21 16:06:53 +00005856 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005857 }
5858#endif
5859 return 0; /* irq not handled */
5860}
5861
5862/**
5863 * ata_interrupt - Default ATA host interrupt handler
Jeff Garzik0cba6322005-05-30 19:49:12 -04005864 * @irq: irq line (unused)
Jeff Garzikcca39742006-08-24 03:19:22 -04005865 * @dev_instance: pointer to our ata_host information structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07005866 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04005867 * Default interrupt handler for PCI IDE devices. Calls
5868 * ata_host_intr() for each port that is not disabled.
5869 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005870 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04005871 * Obtains host lock during operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005872 *
5873 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04005874 * IRQ_NONE or IRQ_HANDLED.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005875 */
5876
David Howells7d12e782006-10-05 14:55:46 +01005877irqreturn_t ata_interrupt (int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005878{
Jeff Garzikcca39742006-08-24 03:19:22 -04005879 struct ata_host *host = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005880 unsigned int i;
5881 unsigned int handled = 0;
5882 unsigned long flags;
5883
5884 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
Jeff Garzikcca39742006-08-24 03:19:22 -04005885 spin_lock_irqsave(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005886
Jeff Garzikcca39742006-08-24 03:19:22 -04005887 for (i = 0; i < host->n_ports; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005888 struct ata_port *ap;
5889
Jeff Garzikcca39742006-08-24 03:19:22 -04005890 ap = host->ports[i];
Tejun Heoc1389502005-08-22 14:59:24 +09005891 if (ap &&
Jeff Garzik029f5462006-04-02 10:30:40 -04005892 !(ap->flags & ATA_FLAG_DISABLED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005893 struct ata_queued_cmd *qc;
5894
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005895 qc = ata_qc_from_tag(ap, ap->link.active_tag);
Albert Lee312f7da2005-09-27 17:38:03 +08005896 if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING)) &&
Albert Lee21b1ed72005-04-29 17:34:59 +08005897 (qc->flags & ATA_QCFLAG_ACTIVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005898 handled |= ata_host_intr(ap, qc);
5899 }
5900 }
5901
Jeff Garzikcca39742006-08-24 03:19:22 -04005902 spin_unlock_irqrestore(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005903
5904 return IRQ_RETVAL(handled);
5905}
5906
Tejun Heo34bf2172006-05-15 20:57:46 +09005907/**
5908 * sata_scr_valid - test whether SCRs are accessible
Tejun Heo936fd732007-08-06 18:36:23 +09005909 * @link: ATA link to test SCR accessibility for
Tejun Heo34bf2172006-05-15 20:57:46 +09005910 *
Tejun Heo936fd732007-08-06 18:36:23 +09005911 * Test whether SCRs are accessible for @link.
Tejun Heo34bf2172006-05-15 20:57:46 +09005912 *
5913 * LOCKING:
5914 * None.
5915 *
5916 * RETURNS:
5917 * 1 if SCRs are accessible, 0 otherwise.
5918 */
Tejun Heo936fd732007-08-06 18:36:23 +09005919int sata_scr_valid(struct ata_link *link)
Tejun Heo34bf2172006-05-15 20:57:46 +09005920{
Tejun Heo936fd732007-08-06 18:36:23 +09005921 struct ata_port *ap = link->ap;
5922
Tejun Heoa16abc02007-05-21 18:33:47 +02005923 return (ap->flags & ATA_FLAG_SATA) && ap->ops->scr_read;
Tejun Heo34bf2172006-05-15 20:57:46 +09005924}
5925
5926/**
5927 * sata_scr_read - read SCR register of the specified port
Tejun Heo936fd732007-08-06 18:36:23 +09005928 * @link: ATA link to read SCR for
Tejun Heo34bf2172006-05-15 20:57:46 +09005929 * @reg: SCR to read
5930 * @val: Place to store read value
5931 *
Tejun Heo936fd732007-08-06 18:36:23 +09005932 * Read SCR register @reg of @link into *@val. This function is
Tejun Heo34bf2172006-05-15 20:57:46 +09005933 * guaranteed to succeed if the cable type of the port is SATA
5934 * and the port implements ->scr_read.
5935 *
5936 * LOCKING:
5937 * None.
5938 *
5939 * RETURNS:
5940 * 0 on success, negative errno on failure.
5941 */
Tejun Heo936fd732007-08-06 18:36:23 +09005942int sata_scr_read(struct ata_link *link, int reg, u32 *val)
Tejun Heo34bf2172006-05-15 20:57:46 +09005943{
Tejun Heo936fd732007-08-06 18:36:23 +09005944 struct ata_port *ap = link->ap;
5945
5946 if (sata_scr_valid(link))
Tejun Heoda3dbb12007-07-16 14:29:40 +09005947 return ap->ops->scr_read(ap, reg, val);
Tejun Heo34bf2172006-05-15 20:57:46 +09005948 return -EOPNOTSUPP;
5949}
5950
5951/**
5952 * sata_scr_write - write SCR register of the specified port
Tejun Heo936fd732007-08-06 18:36:23 +09005953 * @link: ATA link to write SCR for
Tejun Heo34bf2172006-05-15 20:57:46 +09005954 * @reg: SCR to write
5955 * @val: value to write
5956 *
Tejun Heo936fd732007-08-06 18:36:23 +09005957 * Write @val to SCR register @reg of @link. This function is
Tejun Heo34bf2172006-05-15 20:57:46 +09005958 * guaranteed to succeed if the cable type of the port is SATA
5959 * and the port implements ->scr_read.
5960 *
5961 * LOCKING:
5962 * None.
5963 *
5964 * RETURNS:
5965 * 0 on success, negative errno on failure.
5966 */
Tejun Heo936fd732007-08-06 18:36:23 +09005967int sata_scr_write(struct ata_link *link, int reg, u32 val)
Tejun Heo34bf2172006-05-15 20:57:46 +09005968{
Tejun Heo936fd732007-08-06 18:36:23 +09005969 struct ata_port *ap = link->ap;
5970
5971 if (sata_scr_valid(link))
Tejun Heoda3dbb12007-07-16 14:29:40 +09005972 return ap->ops->scr_write(ap, reg, val);
Tejun Heo34bf2172006-05-15 20:57:46 +09005973 return -EOPNOTSUPP;
5974}
5975
5976/**
5977 * sata_scr_write_flush - write SCR register of the specified port and flush
Tejun Heo936fd732007-08-06 18:36:23 +09005978 * @link: ATA link to write SCR for
Tejun Heo34bf2172006-05-15 20:57:46 +09005979 * @reg: SCR to write
5980 * @val: value to write
5981 *
5982 * This function is identical to sata_scr_write() except that this
5983 * function performs flush after writing to the register.
5984 *
5985 * LOCKING:
5986 * None.
5987 *
5988 * RETURNS:
5989 * 0 on success, negative errno on failure.
5990 */
Tejun Heo936fd732007-08-06 18:36:23 +09005991int sata_scr_write_flush(struct ata_link *link, int reg, u32 val)
Tejun Heo34bf2172006-05-15 20:57:46 +09005992{
Tejun Heo936fd732007-08-06 18:36:23 +09005993 struct ata_port *ap = link->ap;
Tejun Heoda3dbb12007-07-16 14:29:40 +09005994 int rc;
5995
Tejun Heo936fd732007-08-06 18:36:23 +09005996 if (sata_scr_valid(link)) {
Tejun Heoda3dbb12007-07-16 14:29:40 +09005997 rc = ap->ops->scr_write(ap, reg, val);
5998 if (rc == 0)
5999 rc = ap->ops->scr_read(ap, reg, &val);
6000 return rc;
Tejun Heo34bf2172006-05-15 20:57:46 +09006001 }
6002 return -EOPNOTSUPP;
6003}
6004
6005/**
Tejun Heo936fd732007-08-06 18:36:23 +09006006 * ata_link_online - test whether the given link is online
6007 * @link: ATA link to test
Tejun Heo34bf2172006-05-15 20:57:46 +09006008 *
Tejun Heo936fd732007-08-06 18:36:23 +09006009 * Test whether @link is online. Note that this function returns
6010 * 0 if online status of @link cannot be obtained, so
6011 * ata_link_online(link) != !ata_link_offline(link).
Tejun Heo34bf2172006-05-15 20:57:46 +09006012 *
6013 * LOCKING:
6014 * None.
6015 *
6016 * RETURNS:
6017 * 1 if the port online status is available and online.
6018 */
Tejun Heo936fd732007-08-06 18:36:23 +09006019int ata_link_online(struct ata_link *link)
Tejun Heo34bf2172006-05-15 20:57:46 +09006020{
6021 u32 sstatus;
6022
Tejun Heo936fd732007-08-06 18:36:23 +09006023 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
6024 (sstatus & 0xf) == 0x3)
Tejun Heo34bf2172006-05-15 20:57:46 +09006025 return 1;
6026 return 0;
6027}
6028
6029/**
Tejun Heo936fd732007-08-06 18:36:23 +09006030 * ata_link_offline - test whether the given link is offline
6031 * @link: ATA link to test
Tejun Heo34bf2172006-05-15 20:57:46 +09006032 *
Tejun Heo936fd732007-08-06 18:36:23 +09006033 * Test whether @link is offline. Note that this function
6034 * returns 0 if offline status of @link cannot be obtained, so
6035 * ata_link_online(link) != !ata_link_offline(link).
Tejun Heo34bf2172006-05-15 20:57:46 +09006036 *
6037 * LOCKING:
6038 * None.
6039 *
6040 * RETURNS:
6041 * 1 if the port offline status is available and offline.
6042 */
Tejun Heo936fd732007-08-06 18:36:23 +09006043int ata_link_offline(struct ata_link *link)
Tejun Heo34bf2172006-05-15 20:57:46 +09006044{
6045 u32 sstatus;
6046
Tejun Heo936fd732007-08-06 18:36:23 +09006047 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
6048 (sstatus & 0xf) != 0x3)
Tejun Heo34bf2172006-05-15 20:57:46 +09006049 return 1;
6050 return 0;
6051}
Edward Falk0baab862005-06-02 18:17:13 -04006052
Tejun Heo77b08fb2006-06-24 20:30:19 +09006053int ata_flush_cache(struct ata_device *dev)
Jens Axboe9b847542006-01-06 09:28:07 +01006054{
Tejun Heo977e6b92006-06-24 20:30:19 +09006055 unsigned int err_mask;
Jens Axboe9b847542006-01-06 09:28:07 +01006056 u8 cmd;
6057
6058 if (!ata_try_flush_cache(dev))
6059 return 0;
6060
Tejun Heo6fc49ad2006-11-11 20:10:45 +09006061 if (dev->flags & ATA_DFLAG_FLUSH_EXT)
Jens Axboe9b847542006-01-06 09:28:07 +01006062 cmd = ATA_CMD_FLUSH_EXT;
6063 else
6064 cmd = ATA_CMD_FLUSH;
6065
Alan Cox4f343372007-08-08 14:30:31 +01006066 /* This is wrong. On a failed flush we get back the LBA of the lost
6067 sector and we should (assuming it wasn't aborted as unknown) issue
6068 a further flush command to continue the writeback until it
6069 does not error */
Tejun Heo977e6b92006-06-24 20:30:19 +09006070 err_mask = ata_do_simple_cmd(dev, cmd);
6071 if (err_mask) {
6072 ata_dev_printk(dev, KERN_ERR, "failed to flush cache\n");
6073 return -EIO;
6074 }
6075
6076 return 0;
Jens Axboe9b847542006-01-06 09:28:07 +01006077}
6078
Tejun Heo6ffa01d2007-03-02 17:32:47 +09006079#ifdef CONFIG_PM
Jeff Garzikcca39742006-08-24 03:19:22 -04006080static int ata_host_request_pm(struct ata_host *host, pm_message_t mesg,
6081 unsigned int action, unsigned int ehi_flags,
6082 int wait)
Tejun Heo500530f2006-07-03 16:07:27 +09006083{
6084 unsigned long flags;
6085 int i, rc;
6086
Jeff Garzikcca39742006-08-24 03:19:22 -04006087 for (i = 0; i < host->n_ports; i++) {
6088 struct ata_port *ap = host->ports[i];
Tejun Heoe3667eb2007-08-06 18:36:24 +09006089 struct ata_link *link;
Tejun Heo500530f2006-07-03 16:07:27 +09006090
6091 /* Previous resume operation might still be in
6092 * progress. Wait for PM_PENDING to clear.
6093 */
6094 if (ap->pflags & ATA_PFLAG_PM_PENDING) {
6095 ata_port_wait_eh(ap);
6096 WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
6097 }
6098
6099 /* request PM ops to EH */
6100 spin_lock_irqsave(ap->lock, flags);
6101
6102 ap->pm_mesg = mesg;
6103 if (wait) {
6104 rc = 0;
6105 ap->pm_result = &rc;
6106 }
6107
6108 ap->pflags |= ATA_PFLAG_PM_PENDING;
Tejun Heoe3667eb2007-08-06 18:36:24 +09006109 __ata_port_for_each_link(link, ap) {
6110 link->eh_info.action |= action;
6111 link->eh_info.flags |= ehi_flags;
6112 }
Tejun Heo500530f2006-07-03 16:07:27 +09006113
6114 ata_port_schedule_eh(ap);
6115
6116 spin_unlock_irqrestore(ap->lock, flags);
6117
6118 /* wait and check result */
6119 if (wait) {
6120 ata_port_wait_eh(ap);
6121 WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
6122 if (rc)
6123 return rc;
6124 }
6125 }
6126
6127 return 0;
6128}
6129
6130/**
Jeff Garzikcca39742006-08-24 03:19:22 -04006131 * ata_host_suspend - suspend host
6132 * @host: host to suspend
Tejun Heo500530f2006-07-03 16:07:27 +09006133 * @mesg: PM message
6134 *
Jeff Garzikcca39742006-08-24 03:19:22 -04006135 * Suspend @host. Actual operation is performed by EH. This
Tejun Heo500530f2006-07-03 16:07:27 +09006136 * function requests EH to perform PM operations and waits for EH
6137 * to finish.
6138 *
6139 * LOCKING:
6140 * Kernel thread context (may sleep).
6141 *
6142 * RETURNS:
6143 * 0 on success, -errno on failure.
6144 */
Jeff Garzikcca39742006-08-24 03:19:22 -04006145int ata_host_suspend(struct ata_host *host, pm_message_t mesg)
Tejun Heo500530f2006-07-03 16:07:27 +09006146{
Tejun Heo9666f402007-05-04 21:27:47 +02006147 int rc;
Tejun Heo500530f2006-07-03 16:07:27 +09006148
Jeff Garzikcca39742006-08-24 03:19:22 -04006149 rc = ata_host_request_pm(host, mesg, 0, ATA_EHI_QUIET, 1);
Tejun Heo9666f402007-05-04 21:27:47 +02006150 if (rc == 0)
6151 host->dev->power.power_state = mesg;
Tejun Heo500530f2006-07-03 16:07:27 +09006152 return rc;
6153}
6154
6155/**
Jeff Garzikcca39742006-08-24 03:19:22 -04006156 * ata_host_resume - resume host
6157 * @host: host to resume
Tejun Heo500530f2006-07-03 16:07:27 +09006158 *
Jeff Garzikcca39742006-08-24 03:19:22 -04006159 * Resume @host. Actual operation is performed by EH. This
Tejun Heo500530f2006-07-03 16:07:27 +09006160 * function requests EH to perform PM operations and returns.
6161 * Note that all resume operations are performed parallely.
6162 *
6163 * LOCKING:
6164 * Kernel thread context (may sleep).
6165 */
Jeff Garzikcca39742006-08-24 03:19:22 -04006166void ata_host_resume(struct ata_host *host)
Tejun Heo500530f2006-07-03 16:07:27 +09006167{
Jeff Garzikcca39742006-08-24 03:19:22 -04006168 ata_host_request_pm(host, PMSG_ON, ATA_EH_SOFTRESET,
6169 ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET, 0);
6170 host->dev->power.power_state = PMSG_ON;
Tejun Heo500530f2006-07-03 16:07:27 +09006171}
Tejun Heo6ffa01d2007-03-02 17:32:47 +09006172#endif
Tejun Heo500530f2006-07-03 16:07:27 +09006173
Randy Dunlapc893a3a2006-01-28 13:15:32 -05006174/**
6175 * ata_port_start - Set port up for dma.
6176 * @ap: Port to initialize
6177 *
6178 * Called just after data structures for each port are
6179 * initialized. Allocates space for PRD table.
6180 *
6181 * May be used as the port_start() entry in ata_port_operations.
6182 *
6183 * LOCKING:
6184 * Inherited from caller.
6185 */
Tejun Heof0d36ef2007-01-20 16:00:28 +09006186int ata_port_start(struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006187{
Brian King2f1f6102006-03-23 17:30:15 -06006188 struct device *dev = ap->dev;
Jeff Garzik6037d6b2005-11-04 22:08:00 -05006189 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006190
Tejun Heof0d36ef2007-01-20 16:00:28 +09006191 ap->prd = dmam_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma,
6192 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006193 if (!ap->prd)
6194 return -ENOMEM;
6195
Jeff Garzik6037d6b2005-11-04 22:08:00 -05006196 rc = ata_pad_alloc(ap, dev);
Tejun Heof0d36ef2007-01-20 16:00:28 +09006197 if (rc)
Jeff Garzik6037d6b2005-11-04 22:08:00 -05006198 return rc;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04006199
Tejun Heof0d36ef2007-01-20 16:00:28 +09006200 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd,
6201 (unsigned long long)ap->prd_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006202 return 0;
6203}
6204
Edward Falk0baab862005-06-02 18:17:13 -04006205/**
Tejun Heo3ef3b432006-05-31 18:27:30 +09006206 * ata_dev_init - Initialize an ata_device structure
6207 * @dev: Device structure to initialize
6208 *
6209 * Initialize @dev in preparation for probing.
6210 *
6211 * LOCKING:
6212 * Inherited from caller.
6213 */
6214void ata_dev_init(struct ata_device *dev)
6215{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09006216 struct ata_link *link = dev->link;
6217 struct ata_port *ap = link->ap;
Tejun Heo72fa4b72006-05-31 18:27:32 +09006218 unsigned long flags;
Tejun Heo3ef3b432006-05-31 18:27:30 +09006219
Tejun Heo5a04bf42006-05-31 18:27:38 +09006220 /* SATA spd limit is bound to the first device */
Tejun Heo9af5c9c2007-08-06 18:36:22 +09006221 link->sata_spd_limit = link->hw_sata_spd_limit;
6222 link->sata_spd = 0;
Tejun Heo5a04bf42006-05-31 18:27:38 +09006223
Tejun Heo72fa4b72006-05-31 18:27:32 +09006224 /* High bits of dev->flags are used to record warm plug
6225 * requests which occur asynchronously. Synchronize using
Jeff Garzikcca39742006-08-24 03:19:22 -04006226 * host lock.
Tejun Heo72fa4b72006-05-31 18:27:32 +09006227 */
Jeff Garzikba6a1302006-06-22 23:46:10 -04006228 spin_lock_irqsave(ap->lock, flags);
Tejun Heo72fa4b72006-05-31 18:27:32 +09006229 dev->flags &= ~ATA_DFLAG_INIT_MASK;
Tejun Heo3dcc3232007-09-03 12:20:11 +09006230 dev->horkage = 0;
Jeff Garzikba6a1302006-06-22 23:46:10 -04006231 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo72fa4b72006-05-31 18:27:32 +09006232
6233 memset((void *)dev + ATA_DEVICE_CLEAR_OFFSET, 0,
6234 sizeof(*dev) - ATA_DEVICE_CLEAR_OFFSET);
Tejun Heo3ef3b432006-05-31 18:27:30 +09006235 dev->pio_mask = UINT_MAX;
6236 dev->mwdma_mask = UINT_MAX;
6237 dev->udma_mask = UINT_MAX;
6238}
6239
6240/**
Tejun Heo4fb37a22007-08-06 18:36:23 +09006241 * ata_link_init - Initialize an ata_link structure
6242 * @ap: ATA port link is attached to
6243 * @link: Link structure to initialize
Tejun Heo89898052007-08-06 18:36:23 +09006244 * @pmp: Port multiplier port number
Tejun Heo4fb37a22007-08-06 18:36:23 +09006245 *
6246 * Initialize @link.
6247 *
6248 * LOCKING:
6249 * Kernel thread context (may sleep)
6250 */
Tejun Heofb7fd612007-09-23 13:14:12 +09006251void ata_link_init(struct ata_port *ap, struct ata_link *link, int pmp)
Tejun Heo4fb37a22007-08-06 18:36:23 +09006252{
6253 int i;
6254
6255 /* clear everything except for devices */
6256 memset(link, 0, offsetof(struct ata_link, device[0]));
6257
6258 link->ap = ap;
Tejun Heo89898052007-08-06 18:36:23 +09006259 link->pmp = pmp;
Tejun Heo4fb37a22007-08-06 18:36:23 +09006260 link->active_tag = ATA_TAG_POISON;
6261 link->hw_sata_spd_limit = UINT_MAX;
6262
6263 /* can't use iterator, ap isn't initialized yet */
6264 for (i = 0; i < ATA_MAX_DEVICES; i++) {
6265 struct ata_device *dev = &link->device[i];
6266
6267 dev->link = link;
6268 dev->devno = dev - link->device;
6269 ata_dev_init(dev);
6270 }
6271}
6272
6273/**
6274 * sata_link_init_spd - Initialize link->sata_spd_limit
6275 * @link: Link to configure sata_spd_limit for
6276 *
6277 * Initialize @link->[hw_]sata_spd_limit to the currently
6278 * configured value.
6279 *
6280 * LOCKING:
6281 * Kernel thread context (may sleep).
6282 *
6283 * RETURNS:
6284 * 0 on success, -errno on failure.
6285 */
Tejun Heofb7fd612007-09-23 13:14:12 +09006286int sata_link_init_spd(struct ata_link *link)
Tejun Heo4fb37a22007-08-06 18:36:23 +09006287{
6288 u32 scontrol, spd;
6289 int rc;
6290
6291 rc = sata_scr_read(link, SCR_CONTROL, &scontrol);
6292 if (rc)
6293 return rc;
6294
6295 spd = (scontrol >> 4) & 0xf;
6296 if (spd)
6297 link->hw_sata_spd_limit &= (1 << spd) - 1;
6298
6299 link->sata_spd_limit = link->hw_sata_spd_limit;
6300
6301 return 0;
6302}
6303
6304/**
Tejun Heof3187192007-04-17 23:44:07 +09006305 * ata_port_alloc - allocate and initialize basic ATA port resources
6306 * @host: ATA host this allocated port belongs to
Linus Torvalds1da177e2005-04-16 15:20:36 -07006307 *
Tejun Heof3187192007-04-17 23:44:07 +09006308 * Allocate and initialize basic ATA port resources.
6309 *
6310 * RETURNS:
6311 * Allocate ATA port on success, NULL on failure.
Jeff Garzik0cba6322005-05-30 19:49:12 -04006312 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006313 * LOCKING:
Tejun Heof3187192007-04-17 23:44:07 +09006314 * Inherited from calling layer (may sleep).
Linus Torvalds1da177e2005-04-16 15:20:36 -07006315 */
Tejun Heof3187192007-04-17 23:44:07 +09006316struct ata_port *ata_port_alloc(struct ata_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006317{
Tejun Heof3187192007-04-17 23:44:07 +09006318 struct ata_port *ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006319
Tejun Heof3187192007-04-17 23:44:07 +09006320 DPRINTK("ENTER\n");
6321
6322 ap = kzalloc(sizeof(*ap), GFP_KERNEL);
6323 if (!ap)
6324 return NULL;
6325
Tejun Heof4d6d002007-05-01 11:50:15 +02006326 ap->pflags |= ATA_PFLAG_INITIALIZING;
Jeff Garzikcca39742006-08-24 03:19:22 -04006327 ap->lock = &host->lock;
Tejun Heo198e0fe2006-04-02 18:51:52 +09006328 ap->flags = ATA_FLAG_DISABLED;
Tejun Heof3187192007-04-17 23:44:07 +09006329 ap->print_id = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006330 ap->ctl = ATA_DEVCTL_OBS;
Jeff Garzikcca39742006-08-24 03:19:22 -04006331 ap->host = host;
Tejun Heof3187192007-04-17 23:44:07 +09006332 ap->dev = host->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006333 ap->last_ctl = 0xFF;
Borislav Petkovbd5d8252006-06-11 23:17:01 -04006334
6335#if defined(ATA_VERBOSE_DEBUG)
6336 /* turn on all debugging levels */
6337 ap->msg_enable = 0x00FF;
6338#elif defined(ATA_DEBUG)
6339 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 +09006340#else
Borislav Petkov0dd4b212006-06-23 02:29:08 -04006341 ap->msg_enable = ATA_MSG_DRV | ATA_MSG_ERR | ATA_MSG_WARN;
Borislav Petkovbd5d8252006-06-11 23:17:01 -04006342#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006343
David Howells65f27f32006-11-22 14:55:48 +00006344 INIT_DELAYED_WORK(&ap->port_task, NULL);
6345 INIT_DELAYED_WORK(&ap->hotplug_task, ata_scsi_hotplug);
6346 INIT_WORK(&ap->scsi_rescan_task, ata_scsi_dev_rescan);
Tejun Heoa72ec4c2006-01-23 13:09:37 +09006347 INIT_LIST_HEAD(&ap->eh_done_q);
Tejun Heoc6cf9e92006-05-31 18:27:27 +09006348 init_waitqueue_head(&ap->eh_wait_q);
Tejun Heo5ddf24c2007-07-16 14:29:41 +09006349 init_timer_deferrable(&ap->fastdrain_timer);
6350 ap->fastdrain_timer.function = ata_eh_fastdrain_timerfn;
6351 ap->fastdrain_timer.data = (unsigned long)ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006352
Tejun Heo838df622006-05-15 20:57:44 +09006353 ap->cbl = ATA_CBL_NONE;
Tejun Heo838df622006-05-15 20:57:44 +09006354
Tejun Heo89898052007-08-06 18:36:23 +09006355 ata_link_init(ap, &ap->link, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006356
6357#ifdef ATA_IRQ_TRAP
6358 ap->stats.unhandled_irq = 1;
6359 ap->stats.idle_irq = 1;
6360#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006361 return ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006362}
6363
Tejun Heof0d36ef2007-01-20 16:00:28 +09006364static void ata_host_release(struct device *gendev, void *res)
6365{
6366 struct ata_host *host = dev_get_drvdata(gendev);
6367 int i;
6368
6369 for (i = 0; i < host->n_ports; i++) {
6370 struct ata_port *ap = host->ports[i];
6371
Tejun Heoecef7252007-04-17 23:44:06 +09006372 if (!ap)
6373 continue;
6374
6375 if ((host->flags & ATA_HOST_STARTED) && ap->ops->port_stop)
Tejun Heof0d36ef2007-01-20 16:00:28 +09006376 ap->ops->port_stop(ap);
Tejun Heof0d36ef2007-01-20 16:00:28 +09006377 }
6378
Tejun Heoecef7252007-04-17 23:44:06 +09006379 if ((host->flags & ATA_HOST_STARTED) && host->ops->host_stop)
Tejun Heof0d36ef2007-01-20 16:00:28 +09006380 host->ops->host_stop(host);
Tejun Heo1aa56cc2007-02-27 22:33:21 +09006381
Tejun Heo1aa506e2007-03-09 19:36:12 +09006382 for (i = 0; i < host->n_ports; i++) {
6383 struct ata_port *ap = host->ports[i];
6384
Tejun Heo49114872007-04-17 23:44:06 +09006385 if (!ap)
6386 continue;
6387
6388 if (ap->scsi_host)
Tejun Heo1aa506e2007-03-09 19:36:12 +09006389 scsi_host_put(ap->scsi_host);
6390
Tejun Heo49114872007-04-17 23:44:06 +09006391 kfree(ap);
Tejun Heo1aa506e2007-03-09 19:36:12 +09006392 host->ports[i] = NULL;
6393 }
6394
Tejun Heo1aa56cc2007-02-27 22:33:21 +09006395 dev_set_drvdata(gendev, NULL);
Tejun Heof0d36ef2007-01-20 16:00:28 +09006396}
6397
Linus Torvalds1da177e2005-04-16 15:20:36 -07006398/**
Tejun Heof3187192007-04-17 23:44:07 +09006399 * ata_host_alloc - allocate and init basic ATA host resources
6400 * @dev: generic device this host is associated with
6401 * @max_ports: maximum number of ATA ports associated with this host
6402 *
6403 * Allocate and initialize basic ATA host resources. LLD calls
6404 * this function to allocate a host, initializes it fully and
6405 * attaches it using ata_host_register().
6406 *
6407 * @max_ports ports are allocated and host->n_ports is
6408 * initialized to @max_ports. The caller is allowed to decrease
6409 * host->n_ports before calling ata_host_register(). The unused
6410 * ports will be automatically freed on registration.
6411 *
6412 * RETURNS:
6413 * Allocate ATA host on success, NULL on failure.
6414 *
6415 * LOCKING:
6416 * Inherited from calling layer (may sleep).
6417 */
6418struct ata_host *ata_host_alloc(struct device *dev, int max_ports)
6419{
6420 struct ata_host *host;
6421 size_t sz;
6422 int i;
6423
6424 DPRINTK("ENTER\n");
6425
6426 if (!devres_open_group(dev, NULL, GFP_KERNEL))
6427 return NULL;
6428
6429 /* alloc a container for our list of ATA ports (buses) */
6430 sz = sizeof(struct ata_host) + (max_ports + 1) * sizeof(void *);
6431 /* alloc a container for our list of ATA ports (buses) */
6432 host = devres_alloc(ata_host_release, sz, GFP_KERNEL);
6433 if (!host)
6434 goto err_out;
6435
6436 devres_add(dev, host);
6437 dev_set_drvdata(dev, host);
6438
6439 spin_lock_init(&host->lock);
6440 host->dev = dev;
6441 host->n_ports = max_ports;
6442
6443 /* allocate ports bound to this host */
6444 for (i = 0; i < max_ports; i++) {
6445 struct ata_port *ap;
6446
6447 ap = ata_port_alloc(host);
6448 if (!ap)
6449 goto err_out;
6450
6451 ap->port_no = i;
6452 host->ports[i] = ap;
6453 }
6454
6455 devres_remove_group(dev, NULL);
6456 return host;
6457
6458 err_out:
6459 devres_release_group(dev, NULL);
6460 return NULL;
6461}
6462
6463/**
Tejun Heof5cda252007-04-17 23:44:07 +09006464 * ata_host_alloc_pinfo - alloc host and init with port_info array
6465 * @dev: generic device this host is associated with
6466 * @ppi: array of ATA port_info to initialize host with
6467 * @n_ports: number of ATA ports attached to this host
6468 *
6469 * Allocate ATA host and initialize with info from @ppi. If NULL
6470 * terminated, @ppi may contain fewer entries than @n_ports. The
6471 * last entry will be used for the remaining ports.
6472 *
6473 * RETURNS:
6474 * Allocate ATA host on success, NULL on failure.
6475 *
6476 * LOCKING:
6477 * Inherited from calling layer (may sleep).
6478 */
6479struct ata_host *ata_host_alloc_pinfo(struct device *dev,
6480 const struct ata_port_info * const * ppi,
6481 int n_ports)
6482{
6483 const struct ata_port_info *pi;
6484 struct ata_host *host;
6485 int i, j;
6486
6487 host = ata_host_alloc(dev, n_ports);
6488 if (!host)
6489 return NULL;
6490
6491 for (i = 0, j = 0, pi = NULL; i < host->n_ports; i++) {
6492 struct ata_port *ap = host->ports[i];
6493
6494 if (ppi[j])
6495 pi = ppi[j++];
6496
6497 ap->pio_mask = pi->pio_mask;
6498 ap->mwdma_mask = pi->mwdma_mask;
6499 ap->udma_mask = pi->udma_mask;
6500 ap->flags |= pi->flags;
Tejun Heo0c887582007-08-06 18:36:23 +09006501 ap->link.flags |= pi->link_flags;
Tejun Heof5cda252007-04-17 23:44:07 +09006502 ap->ops = pi->port_ops;
6503
6504 if (!host->ops && (pi->port_ops != &ata_dummy_port_ops))
6505 host->ops = pi->port_ops;
6506 if (!host->private_data && pi->private_data)
6507 host->private_data = pi->private_data;
6508 }
6509
6510 return host;
6511}
6512
6513/**
Tejun Heoecef7252007-04-17 23:44:06 +09006514 * ata_host_start - start and freeze ports of an ATA host
6515 * @host: ATA host to start ports for
6516 *
6517 * Start and then freeze ports of @host. Started status is
6518 * recorded in host->flags, so this function can be called
6519 * multiple times. Ports are guaranteed to get started only
Tejun Heof3187192007-04-17 23:44:07 +09006520 * once. If host->ops isn't initialized yet, its set to the
6521 * first non-dummy port ops.
Tejun Heoecef7252007-04-17 23:44:06 +09006522 *
6523 * LOCKING:
6524 * Inherited from calling layer (may sleep).
6525 *
6526 * RETURNS:
6527 * 0 if all ports are started successfully, -errno otherwise.
6528 */
6529int ata_host_start(struct ata_host *host)
6530{
6531 int i, rc;
6532
6533 if (host->flags & ATA_HOST_STARTED)
6534 return 0;
6535
6536 for (i = 0; i < host->n_ports; i++) {
6537 struct ata_port *ap = host->ports[i];
6538
Tejun Heof3187192007-04-17 23:44:07 +09006539 if (!host->ops && !ata_port_is_dummy(ap))
6540 host->ops = ap->ops;
6541
Tejun Heoecef7252007-04-17 23:44:06 +09006542 if (ap->ops->port_start) {
6543 rc = ap->ops->port_start(ap);
6544 if (rc) {
6545 ata_port_printk(ap, KERN_ERR, "failed to "
6546 "start port (errno=%d)\n", rc);
6547 goto err_out;
6548 }
6549 }
6550
6551 ata_eh_freeze_port(ap);
6552 }
6553
6554 host->flags |= ATA_HOST_STARTED;
6555 return 0;
6556
6557 err_out:
6558 while (--i >= 0) {
6559 struct ata_port *ap = host->ports[i];
6560
6561 if (ap->ops->port_stop)
6562 ap->ops->port_stop(ap);
6563 }
6564 return rc;
6565}
6566
6567/**
Jeff Garzikcca39742006-08-24 03:19:22 -04006568 * ata_sas_host_init - Initialize a host struct
6569 * @host: host to initialize
6570 * @dev: device host is attached to
6571 * @flags: host flags
6572 * @ops: port_ops
Brian Kingb03732f2006-08-07 14:27:10 -05006573 *
6574 * LOCKING:
6575 * PCI/etc. bus probe sem.
6576 *
6577 */
Tejun Heof3187192007-04-17 23:44:07 +09006578/* KILLME - the only user left is ipr */
Jeff Garzikcca39742006-08-24 03:19:22 -04006579void ata_host_init(struct ata_host *host, struct device *dev,
6580 unsigned long flags, const struct ata_port_operations *ops)
Brian Kingb03732f2006-08-07 14:27:10 -05006581{
Jeff Garzikcca39742006-08-24 03:19:22 -04006582 spin_lock_init(&host->lock);
6583 host->dev = dev;
6584 host->flags = flags;
6585 host->ops = ops;
Brian Kingb03732f2006-08-07 14:27:10 -05006586}
6587
6588/**
Tejun Heof3187192007-04-17 23:44:07 +09006589 * ata_host_register - register initialized ATA host
6590 * @host: ATA host to register
6591 * @sht: template for SCSI host
Jeff Garzik0cba6322005-05-30 19:49:12 -04006592 *
Tejun Heof3187192007-04-17 23:44:07 +09006593 * Register initialized ATA host. @host is allocated using
6594 * ata_host_alloc() and fully initialized by LLD. This function
6595 * starts ports, registers @host with ATA and SCSI layers and
6596 * probe registered devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006597 *
6598 * LOCKING:
Tejun Heof3187192007-04-17 23:44:07 +09006599 * Inherited from calling layer (may sleep).
Linus Torvalds1da177e2005-04-16 15:20:36 -07006600 *
6601 * RETURNS:
Tejun Heof3187192007-04-17 23:44:07 +09006602 * 0 on success, -errno otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006603 */
Tejun Heof3187192007-04-17 23:44:07 +09006604int ata_host_register(struct ata_host *host, struct scsi_host_template *sht)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006605{
Tejun Heof3187192007-04-17 23:44:07 +09006606 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006607
Tejun Heof3187192007-04-17 23:44:07 +09006608 /* host must have been started */
6609 if (!(host->flags & ATA_HOST_STARTED)) {
6610 dev_printk(KERN_ERR, host->dev,
6611 "BUG: trying to register unstarted host\n");
6612 WARN_ON(1);
6613 return -EINVAL;
Alan Cox02f076a2006-09-26 17:35:32 +01006614 }
Tejun Heof0d36ef2007-01-20 16:00:28 +09006615
Tejun Heof3187192007-04-17 23:44:07 +09006616 /* Blow away unused ports. This happens when LLD can't
6617 * determine the exact number of ports to allocate at
6618 * allocation time.
6619 */
6620 for (i = host->n_ports; host->ports[i]; i++)
6621 kfree(host->ports[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006622
Tejun Heof3187192007-04-17 23:44:07 +09006623 /* give ports names and add SCSI hosts */
6624 for (i = 0; i < host->n_ports; i++)
6625 host->ports[i]->print_id = ata_print_id++;
Tejun Heof0d36ef2007-01-20 16:00:28 +09006626
Tejun Heof3187192007-04-17 23:44:07 +09006627 rc = ata_scsi_add_hosts(host, sht);
Tejun Heoecef7252007-04-17 23:44:06 +09006628 if (rc)
Tejun Heof3187192007-04-17 23:44:07 +09006629 return rc;
Tejun Heoecef7252007-04-17 23:44:06 +09006630
Tejun Heofafbae82007-05-15 03:28:16 +09006631 /* associate with ACPI nodes */
6632 ata_acpi_associate(host);
6633
Tejun Heof3187192007-04-17 23:44:07 +09006634 /* set cable, sata_spd_limit and report */
Jeff Garzikcca39742006-08-24 03:19:22 -04006635 for (i = 0; i < host->n_ports; i++) {
6636 struct ata_port *ap = host->ports[i];
Tejun Heof3187192007-04-17 23:44:07 +09006637 unsigned long xfer_mask;
6638
6639 /* set SATA cable type if still unset */
6640 if (ap->cbl == ATA_CBL_NONE && (ap->flags & ATA_FLAG_SATA))
6641 ap->cbl = ATA_CBL_SATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006642
Tejun Heo5a04bf42006-05-31 18:27:38 +09006643 /* init sata_spd_limit to the current value */
Tejun Heo4fb37a22007-08-06 18:36:23 +09006644 sata_link_init_spd(&ap->link);
Tejun Heo5a04bf42006-05-31 18:27:38 +09006645
Tejun Heocbcdd872007-08-18 13:14:55 +09006646 /* print per-port info to dmesg */
Tejun Heof3187192007-04-17 23:44:07 +09006647 xfer_mask = ata_pack_xfermask(ap->pio_mask, ap->mwdma_mask,
6648 ap->udma_mask);
6649
Tejun Heof3187192007-04-17 23:44:07 +09006650 if (!ata_port_is_dummy(ap))
Tejun Heocbcdd872007-08-18 13:14:55 +09006651 ata_port_printk(ap, KERN_INFO,
6652 "%cATA max %s %s\n",
Tejun Heoa16abc02007-05-21 18:33:47 +02006653 (ap->flags & ATA_FLAG_SATA) ? 'S' : 'P',
Tejun Heof3187192007-04-17 23:44:07 +09006654 ata_mode_string(xfer_mask),
Tejun Heocbcdd872007-08-18 13:14:55 +09006655 ap->link.eh_info.desc);
Tejun Heof3187192007-04-17 23:44:07 +09006656 else
6657 ata_port_printk(ap, KERN_INFO, "DUMMY\n");
6658 }
6659
6660 /* perform each probe synchronously */
6661 DPRINTK("probe begin\n");
6662 for (i = 0; i < host->n_ports; i++) {
6663 struct ata_port *ap = host->ports[i];
6664 int rc;
6665
6666 /* probe */
Tejun Heo52783c52006-05-31 18:28:22 +09006667 if (ap->ops->error_handler) {
Tejun Heo9af5c9c2007-08-06 18:36:22 +09006668 struct ata_eh_info *ehi = &ap->link.eh_info;
Tejun Heo3e706392006-05-31 18:28:11 +09006669 unsigned long flags;
6670
6671 ata_port_probe(ap);
6672
6673 /* kick EH for boot probing */
Jeff Garzikba6a1302006-06-22 23:46:10 -04006674 spin_lock_irqsave(ap->lock, flags);
Tejun Heo3e706392006-05-31 18:28:11 +09006675
Tejun Heof58229f2007-08-06 18:36:23 +09006676 ehi->probe_mask =
6677 (1 << ata_link_max_devices(&ap->link)) - 1;
Tejun Heo1cdaf532006-07-03 16:07:26 +09006678 ehi->action |= ATA_EH_SOFTRESET;
6679 ehi->flags |= ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET;
Tejun Heo3e706392006-05-31 18:28:11 +09006680
Tejun Heof4d6d002007-05-01 11:50:15 +02006681 ap->pflags &= ~ATA_PFLAG_INITIALIZING;
Tejun Heob51e9e52006-06-29 01:29:30 +09006682 ap->pflags |= ATA_PFLAG_LOADING;
Tejun Heo3e706392006-05-31 18:28:11 +09006683 ata_port_schedule_eh(ap);
6684
Jeff Garzikba6a1302006-06-22 23:46:10 -04006685 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo3e706392006-05-31 18:28:11 +09006686
6687 /* wait for EH to finish */
6688 ata_port_wait_eh(ap);
6689 } else {
Tejun Heo44877b42007-02-21 01:06:51 +09006690 DPRINTK("ata%u: bus probe begin\n", ap->print_id);
Tejun Heo3e706392006-05-31 18:28:11 +09006691 rc = ata_bus_probe(ap);
Tejun Heo44877b42007-02-21 01:06:51 +09006692 DPRINTK("ata%u: bus probe end\n", ap->print_id);
Tejun Heo3e706392006-05-31 18:28:11 +09006693
6694 if (rc) {
6695 /* FIXME: do something useful here?
6696 * Current libata behavior will
6697 * tear down everything when
6698 * the module is removed
6699 * or the h/w is unplugged.
6700 */
6701 }
6702 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006703 }
6704
6705 /* probes are done, now scan each port's disk(s) */
Randy Dunlapc893a3a2006-01-28 13:15:32 -05006706 DPRINTK("host probe begin\n");
Jeff Garzikcca39742006-08-24 03:19:22 -04006707 for (i = 0; i < host->n_ports; i++) {
6708 struct ata_port *ap = host->ports[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006709
Tejun Heo1ae46312007-07-16 14:29:40 +09006710 ata_scsi_scan_host(ap, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006711 }
6712
Tejun Heof3187192007-04-17 23:44:07 +09006713 return 0;
6714}
6715
6716/**
Tejun Heof5cda252007-04-17 23:44:07 +09006717 * ata_host_activate - start host, request IRQ and register it
6718 * @host: target ATA host
6719 * @irq: IRQ to request
6720 * @irq_handler: irq_handler used when requesting IRQ
6721 * @irq_flags: irq_flags used when requesting IRQ
6722 * @sht: scsi_host_template to use when registering the host
6723 *
6724 * After allocating an ATA host and initializing it, most libata
6725 * LLDs perform three steps to activate the host - start host,
6726 * request IRQ and register it. This helper takes necessasry
6727 * arguments and performs the three steps in one go.
6728 *
6729 * LOCKING:
6730 * Inherited from calling layer (may sleep).
6731 *
6732 * RETURNS:
6733 * 0 on success, -errno otherwise.
6734 */
6735int ata_host_activate(struct ata_host *host, int irq,
6736 irq_handler_t irq_handler, unsigned long irq_flags,
6737 struct scsi_host_template *sht)
6738{
Tejun Heocbcdd872007-08-18 13:14:55 +09006739 int i, rc;
Tejun Heof5cda252007-04-17 23:44:07 +09006740
6741 rc = ata_host_start(host);
6742 if (rc)
6743 return rc;
6744
6745 rc = devm_request_irq(host->dev, irq, irq_handler, irq_flags,
6746 dev_driver_string(host->dev), host);
6747 if (rc)
6748 return rc;
6749
Tejun Heocbcdd872007-08-18 13:14:55 +09006750 for (i = 0; i < host->n_ports; i++)
6751 ata_port_desc(host->ports[i], "irq %d", irq);
Tejun Heo40318262007-07-03 01:38:47 +09006752
Tejun Heof5cda252007-04-17 23:44:07 +09006753 rc = ata_host_register(host, sht);
6754 /* if failed, just free the IRQ and leave ports alone */
6755 if (rc)
6756 devm_free_irq(host->dev, irq, host);
6757
6758 return rc;
6759}
6760
6761/**
Tejun Heo720ba122006-05-31 18:28:13 +09006762 * ata_port_detach - Detach ATA port in prepration of device removal
6763 * @ap: ATA port to be detached
6764 *
6765 * Detach all ATA devices and the associated SCSI devices of @ap;
6766 * then, remove the associated SCSI host. @ap is guaranteed to
6767 * be quiescent on return from this function.
6768 *
6769 * LOCKING:
6770 * Kernel thread context (may sleep).
6771 */
6772void ata_port_detach(struct ata_port *ap)
6773{
6774 unsigned long flags;
Tejun Heo41bda9c2007-08-06 18:36:24 +09006775 struct ata_link *link;
Tejun Heof58229f2007-08-06 18:36:23 +09006776 struct ata_device *dev;
Tejun Heo720ba122006-05-31 18:28:13 +09006777
6778 if (!ap->ops->error_handler)
Tejun Heoc3cf30a2006-08-05 03:59:11 +09006779 goto skip_eh;
Tejun Heo720ba122006-05-31 18:28:13 +09006780
6781 /* tell EH we're leaving & flush EH */
Jeff Garzikba6a1302006-06-22 23:46:10 -04006782 spin_lock_irqsave(ap->lock, flags);
Tejun Heob51e9e52006-06-29 01:29:30 +09006783 ap->pflags |= ATA_PFLAG_UNLOADING;
Jeff Garzikba6a1302006-06-22 23:46:10 -04006784 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo720ba122006-05-31 18:28:13 +09006785
6786 ata_port_wait_eh(ap);
6787
6788 /* EH is now guaranteed to see UNLOADING, so no new device
6789 * will be attached. Disable all existing devices.
6790 */
Jeff Garzikba6a1302006-06-22 23:46:10 -04006791 spin_lock_irqsave(ap->lock, flags);
Tejun Heo720ba122006-05-31 18:28:13 +09006792
Tejun Heo41bda9c2007-08-06 18:36:24 +09006793 ata_port_for_each_link(link, ap) {
6794 ata_link_for_each_dev(dev, link)
6795 ata_dev_disable(dev);
6796 }
Tejun Heo720ba122006-05-31 18:28:13 +09006797
Jeff Garzikba6a1302006-06-22 23:46:10 -04006798 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo720ba122006-05-31 18:28:13 +09006799
6800 /* Final freeze & EH. All in-flight commands are aborted. EH
6801 * will be skipped and retrials will be terminated with bad
6802 * target.
6803 */
Jeff Garzikba6a1302006-06-22 23:46:10 -04006804 spin_lock_irqsave(ap->lock, flags);
Tejun Heo720ba122006-05-31 18:28:13 +09006805 ata_port_freeze(ap); /* won't be thawed */
Jeff Garzikba6a1302006-06-22 23:46:10 -04006806 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo720ba122006-05-31 18:28:13 +09006807
6808 ata_port_wait_eh(ap);
Oleg Nesterov45a66c12007-07-09 11:46:13 -07006809 cancel_rearming_delayed_work(&ap->hotplug_task);
Tejun Heo720ba122006-05-31 18:28:13 +09006810
Tejun Heoc3cf30a2006-08-05 03:59:11 +09006811 skip_eh:
Tejun Heo720ba122006-05-31 18:28:13 +09006812 /* remove the associated SCSI host */
Jeff Garzikcca39742006-08-24 03:19:22 -04006813 scsi_remove_host(ap->scsi_host);
Tejun Heo720ba122006-05-31 18:28:13 +09006814}
6815
6816/**
Tejun Heo0529c152007-01-20 16:00:26 +09006817 * ata_host_detach - Detach all ports of an ATA host
6818 * @host: Host to detach
6819 *
6820 * Detach all ports of @host.
6821 *
6822 * LOCKING:
6823 * Kernel thread context (may sleep).
6824 */
6825void ata_host_detach(struct ata_host *host)
6826{
6827 int i;
6828
6829 for (i = 0; i < host->n_ports; i++)
6830 ata_port_detach(host->ports[i]);
6831}
6832
Linus Torvalds1da177e2005-04-16 15:20:36 -07006833/**
6834 * ata_std_ports - initialize ioaddr with standard port offsets.
6835 * @ioaddr: IO address structure to be initialized
Edward Falk0baab862005-06-02 18:17:13 -04006836 *
6837 * Utility function which initializes data_addr, error_addr,
6838 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
6839 * device_addr, status_addr, and command_addr to standard offsets
6840 * relative to cmd_addr.
6841 *
6842 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006843 */
Edward Falk0baab862005-06-02 18:17:13 -04006844
Linus Torvalds1da177e2005-04-16 15:20:36 -07006845void ata_std_ports(struct ata_ioports *ioaddr)
6846{
6847 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
6848 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
6849 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
6850 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
6851 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
6852 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
6853 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
6854 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
6855 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
6856 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
6857}
6858
Edward Falk0baab862005-06-02 18:17:13 -04006859
Jeff Garzik374b1872005-08-30 05:42:52 -04006860#ifdef CONFIG_PCI
6861
Edward Falk0baab862005-06-02 18:17:13 -04006862/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07006863 * ata_pci_remove_one - PCI layer callback for device removal
6864 * @pdev: PCI device that was removed
6865 *
Tejun Heob878ca52007-01-20 16:00:28 +09006866 * PCI layer indicates to libata via this hook that hot-unplug or
6867 * module unload event has occurred. Detach all ports. Resource
6868 * release is handled via devres.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006869 *
6870 * LOCKING:
6871 * Inherited from PCI layer (may sleep).
6872 */
Tejun Heof0d36ef2007-01-20 16:00:28 +09006873void ata_pci_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006874{
6875 struct device *dev = pci_dev_to_dev(pdev);
Jeff Garzikcca39742006-08-24 03:19:22 -04006876 struct ata_host *host = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006877
Tejun Heob878ca52007-01-20 16:00:28 +09006878 ata_host_detach(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006879}
6880
6881/* move to PCI subsystem */
Jeff Garzik057ace52005-10-22 14:27:05 -04006882int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006883{
6884 unsigned long tmp = 0;
6885
6886 switch (bits->width) {
6887 case 1: {
6888 u8 tmp8 = 0;
6889 pci_read_config_byte(pdev, bits->reg, &tmp8);
6890 tmp = tmp8;
6891 break;
6892 }
6893 case 2: {
6894 u16 tmp16 = 0;
6895 pci_read_config_word(pdev, bits->reg, &tmp16);
6896 tmp = tmp16;
6897 break;
6898 }
6899 case 4: {
6900 u32 tmp32 = 0;
6901 pci_read_config_dword(pdev, bits->reg, &tmp32);
6902 tmp = tmp32;
6903 break;
6904 }
6905
6906 default:
6907 return -EINVAL;
6908 }
6909
6910 tmp &= bits->mask;
6911
6912 return (tmp == bits->val) ? 1 : 0;
6913}
Jens Axboe9b847542006-01-06 09:28:07 +01006914
Tejun Heo6ffa01d2007-03-02 17:32:47 +09006915#ifdef CONFIG_PM
Tejun Heo3c5100c2006-07-26 16:58:33 +09006916void ata_pci_device_do_suspend(struct pci_dev *pdev, pm_message_t mesg)
Jens Axboe9b847542006-01-06 09:28:07 +01006917{
6918 pci_save_state(pdev);
Tejun Heo4c90d972007-02-20 18:14:48 +09006919 pci_disable_device(pdev);
Tejun Heo500530f2006-07-03 16:07:27 +09006920
Tejun Heo4c90d972007-02-20 18:14:48 +09006921 if (mesg.event == PM_EVENT_SUSPEND)
Tejun Heo500530f2006-07-03 16:07:27 +09006922 pci_set_power_state(pdev, PCI_D3hot);
Jens Axboe9b847542006-01-06 09:28:07 +01006923}
6924
Tejun Heo553c4aa2006-12-26 19:39:50 +09006925int ata_pci_device_do_resume(struct pci_dev *pdev)
Jens Axboe9b847542006-01-06 09:28:07 +01006926{
Tejun Heo553c4aa2006-12-26 19:39:50 +09006927 int rc;
6928
Jens Axboe9b847542006-01-06 09:28:07 +01006929 pci_set_power_state(pdev, PCI_D0);
6930 pci_restore_state(pdev);
Tejun Heo553c4aa2006-12-26 19:39:50 +09006931
Tejun Heob878ca52007-01-20 16:00:28 +09006932 rc = pcim_enable_device(pdev);
Tejun Heo553c4aa2006-12-26 19:39:50 +09006933 if (rc) {
6934 dev_printk(KERN_ERR, &pdev->dev,
6935 "failed to enable device after resume (%d)\n", rc);
6936 return rc;
6937 }
6938
Jens Axboe9b847542006-01-06 09:28:07 +01006939 pci_set_master(pdev);
Tejun Heo553c4aa2006-12-26 19:39:50 +09006940 return 0;
Tejun Heo500530f2006-07-03 16:07:27 +09006941}
6942
Tejun Heo3c5100c2006-07-26 16:58:33 +09006943int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg)
Tejun Heo500530f2006-07-03 16:07:27 +09006944{
Jeff Garzikcca39742006-08-24 03:19:22 -04006945 struct ata_host *host = dev_get_drvdata(&pdev->dev);
Tejun Heo500530f2006-07-03 16:07:27 +09006946 int rc = 0;
6947
Jeff Garzikcca39742006-08-24 03:19:22 -04006948 rc = ata_host_suspend(host, mesg);
Tejun Heo500530f2006-07-03 16:07:27 +09006949 if (rc)
6950 return rc;
6951
Tejun Heo3c5100c2006-07-26 16:58:33 +09006952 ata_pci_device_do_suspend(pdev, mesg);
Tejun Heo500530f2006-07-03 16:07:27 +09006953
6954 return 0;
6955}
6956
6957int ata_pci_device_resume(struct pci_dev *pdev)
6958{
Jeff Garzikcca39742006-08-24 03:19:22 -04006959 struct ata_host *host = dev_get_drvdata(&pdev->dev);
Tejun Heo553c4aa2006-12-26 19:39:50 +09006960 int rc;
Tejun Heo500530f2006-07-03 16:07:27 +09006961
Tejun Heo553c4aa2006-12-26 19:39:50 +09006962 rc = ata_pci_device_do_resume(pdev);
6963 if (rc == 0)
6964 ata_host_resume(host);
6965 return rc;
Jens Axboe9b847542006-01-06 09:28:07 +01006966}
Tejun Heo6ffa01d2007-03-02 17:32:47 +09006967#endif /* CONFIG_PM */
6968
Linus Torvalds1da177e2005-04-16 15:20:36 -07006969#endif /* CONFIG_PCI */
6970
6971
Linus Torvalds1da177e2005-04-16 15:20:36 -07006972static int __init ata_init(void)
6973{
Andrew Mortona8601e52006-06-25 01:36:52 -07006974 ata_probe_timeout *= HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006975 ata_wq = create_workqueue("ata");
6976 if (!ata_wq)
6977 return -ENOMEM;
6978
Tejun Heo453b07a2006-05-31 18:27:42 +09006979 ata_aux_wq = create_singlethread_workqueue("ata_aux");
6980 if (!ata_aux_wq) {
6981 destroy_workqueue(ata_wq);
6982 return -ENOMEM;
6983 }
6984
Linus Torvalds1da177e2005-04-16 15:20:36 -07006985 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
6986 return 0;
6987}
6988
6989static void __exit ata_exit(void)
6990{
6991 destroy_workqueue(ata_wq);
Tejun Heo453b07a2006-05-31 18:27:42 +09006992 destroy_workqueue(ata_aux_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006993}
6994
Brian Kinga4625082006-11-13 16:32:36 -06006995subsys_initcall(ata_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006996module_exit(ata_exit);
6997
Jeff Garzik67846b32005-10-05 02:58:32 -04006998static unsigned long ratelimit_time;
Ingo Molnar34af9462006-06-27 02:53:55 -07006999static DEFINE_SPINLOCK(ata_ratelimit_lock);
Jeff Garzik67846b32005-10-05 02:58:32 -04007000
7001int ata_ratelimit(void)
7002{
7003 int rc;
7004 unsigned long flags;
7005
7006 spin_lock_irqsave(&ata_ratelimit_lock, flags);
7007
7008 if (time_after(jiffies, ratelimit_time)) {
7009 rc = 1;
7010 ratelimit_time = jiffies + (HZ/5);
7011 } else
7012 rc = 0;
7013
7014 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
7015
7016 return rc;
7017}
7018
Tejun Heoc22daff2006-04-11 22:22:29 +09007019/**
7020 * ata_wait_register - wait until register value changes
7021 * @reg: IO-mapped register
7022 * @mask: Mask to apply to read register value
7023 * @val: Wait condition
7024 * @interval_msec: polling interval in milliseconds
7025 * @timeout_msec: timeout in milliseconds
7026 *
7027 * Waiting for some bits of register to change is a common
7028 * operation for ATA controllers. This function reads 32bit LE
7029 * IO-mapped register @reg and tests for the following condition.
7030 *
7031 * (*@reg & mask) != val
7032 *
7033 * If the condition is met, it returns; otherwise, the process is
7034 * repeated after @interval_msec until timeout.
7035 *
7036 * LOCKING:
7037 * Kernel thread context (may sleep)
7038 *
7039 * RETURNS:
7040 * The final register value.
7041 */
7042u32 ata_wait_register(void __iomem *reg, u32 mask, u32 val,
7043 unsigned long interval_msec,
7044 unsigned long timeout_msec)
7045{
7046 unsigned long timeout;
7047 u32 tmp;
7048
7049 tmp = ioread32(reg);
7050
7051 /* Calculate timeout _after_ the first read to make sure
7052 * preceding writes reach the controller before starting to
7053 * eat away the timeout.
7054 */
7055 timeout = jiffies + (timeout_msec * HZ) / 1000;
7056
7057 while ((tmp & mask) == val && time_before(jiffies, timeout)) {
7058 msleep(interval_msec);
7059 tmp = ioread32(reg);
7060 }
7061
7062 return tmp;
7063}
7064
Linus Torvalds1da177e2005-04-16 15:20:36 -07007065/*
Tejun Heodd5b06c2006-08-10 16:59:12 +09007066 * Dummy port_ops
7067 */
7068static void ata_dummy_noret(struct ata_port *ap) { }
7069static int ata_dummy_ret0(struct ata_port *ap) { return 0; }
7070static void ata_dummy_qc_noret(struct ata_queued_cmd *qc) { }
7071
7072static u8 ata_dummy_check_status(struct ata_port *ap)
7073{
7074 return ATA_DRDY;
7075}
7076
7077static unsigned int ata_dummy_qc_issue(struct ata_queued_cmd *qc)
7078{
7079 return AC_ERR_SYSTEM;
7080}
7081
7082const struct ata_port_operations ata_dummy_port_ops = {
Tejun Heodd5b06c2006-08-10 16:59:12 +09007083 .check_status = ata_dummy_check_status,
7084 .check_altstatus = ata_dummy_check_status,
7085 .dev_select = ata_noop_dev_select,
7086 .qc_prep = ata_noop_qc_prep,
7087 .qc_issue = ata_dummy_qc_issue,
7088 .freeze = ata_dummy_noret,
7089 .thaw = ata_dummy_noret,
7090 .error_handler = ata_dummy_noret,
7091 .post_internal_cmd = ata_dummy_qc_noret,
7092 .irq_clear = ata_dummy_noret,
7093 .port_start = ata_dummy_ret0,
7094 .port_stop = ata_dummy_noret,
7095};
7096
Tejun Heo21b0ad42007-04-17 23:44:07 +09007097const struct ata_port_info ata_dummy_port_info = {
7098 .port_ops = &ata_dummy_port_ops,
7099};
7100
Tejun Heodd5b06c2006-08-10 16:59:12 +09007101/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07007102 * libata is essentially a library of internal helper functions for
7103 * low-level ATA host controller drivers. As such, the API/ABI is
7104 * likely to change as new drivers are added and updated.
7105 * Do not depend on ABI/API stability.
7106 */
7107
Tejun Heoe9c83912006-07-03 16:07:26 +09007108EXPORT_SYMBOL_GPL(sata_deb_timing_normal);
7109EXPORT_SYMBOL_GPL(sata_deb_timing_hotplug);
7110EXPORT_SYMBOL_GPL(sata_deb_timing_long);
Tejun Heodd5b06c2006-08-10 16:59:12 +09007111EXPORT_SYMBOL_GPL(ata_dummy_port_ops);
Tejun Heo21b0ad42007-04-17 23:44:07 +09007112EXPORT_SYMBOL_GPL(ata_dummy_port_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007113EXPORT_SYMBOL_GPL(ata_std_bios_param);
7114EXPORT_SYMBOL_GPL(ata_std_ports);
Jeff Garzikcca39742006-08-24 03:19:22 -04007115EXPORT_SYMBOL_GPL(ata_host_init);
Tejun Heof3187192007-04-17 23:44:07 +09007116EXPORT_SYMBOL_GPL(ata_host_alloc);
Tejun Heof5cda252007-04-17 23:44:07 +09007117EXPORT_SYMBOL_GPL(ata_host_alloc_pinfo);
Tejun Heoecef7252007-04-17 23:44:06 +09007118EXPORT_SYMBOL_GPL(ata_host_start);
Tejun Heof3187192007-04-17 23:44:07 +09007119EXPORT_SYMBOL_GPL(ata_host_register);
Tejun Heof5cda252007-04-17 23:44:07 +09007120EXPORT_SYMBOL_GPL(ata_host_activate);
Tejun Heo0529c152007-01-20 16:00:26 +09007121EXPORT_SYMBOL_GPL(ata_host_detach);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007122EXPORT_SYMBOL_GPL(ata_sg_init);
7123EXPORT_SYMBOL_GPL(ata_sg_init_one);
Tejun Heo9a1004d2006-05-31 18:27:52 +09007124EXPORT_SYMBOL_GPL(ata_hsm_move);
Tejun Heof686bcb2006-05-15 20:58:05 +09007125EXPORT_SYMBOL_GPL(ata_qc_complete);
Tejun Heodedaf2b2006-05-15 21:03:43 +09007126EXPORT_SYMBOL_GPL(ata_qc_complete_multiple);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007127EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007128EXPORT_SYMBOL_GPL(ata_tf_load);
7129EXPORT_SYMBOL_GPL(ata_tf_read);
7130EXPORT_SYMBOL_GPL(ata_noop_dev_select);
7131EXPORT_SYMBOL_GPL(ata_std_dev_select);
Jeff Garzik43727fb2007-02-25 16:50:52 -05007132EXPORT_SYMBOL_GPL(sata_print_link_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007133EXPORT_SYMBOL_GPL(ata_tf_to_fis);
7134EXPORT_SYMBOL_GPL(ata_tf_from_fis);
7135EXPORT_SYMBOL_GPL(ata_check_status);
7136EXPORT_SYMBOL_GPL(ata_altstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007137EXPORT_SYMBOL_GPL(ata_exec_command);
7138EXPORT_SYMBOL_GPL(ata_port_start);
Alan Coxd92e74d2007-06-07 16:19:15 +01007139EXPORT_SYMBOL_GPL(ata_sff_port_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007140EXPORT_SYMBOL_GPL(ata_interrupt);
Alan04351822007-03-06 02:37:52 -08007141EXPORT_SYMBOL_GPL(ata_do_set_mode);
Tejun Heo0d5ff562007-02-01 15:06:36 +09007142EXPORT_SYMBOL_GPL(ata_data_xfer);
7143EXPORT_SYMBOL_GPL(ata_data_xfer_noirq);
Tejun Heo31cc23b2007-09-23 13:14:12 +09007144EXPORT_SYMBOL_GPL(ata_std_qc_defer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007145EXPORT_SYMBOL_GPL(ata_qc_prep);
Alan Coxd26fc952007-07-06 19:13:52 -04007146EXPORT_SYMBOL_GPL(ata_dumb_qc_prep);
Brian Kinge46834c2006-03-17 17:04:03 -06007147EXPORT_SYMBOL_GPL(ata_noop_qc_prep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007148EXPORT_SYMBOL_GPL(ata_bmdma_setup);
7149EXPORT_SYMBOL_GPL(ata_bmdma_start);
7150EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
7151EXPORT_SYMBOL_GPL(ata_bmdma_status);
7152EXPORT_SYMBOL_GPL(ata_bmdma_stop);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09007153EXPORT_SYMBOL_GPL(ata_bmdma_freeze);
7154EXPORT_SYMBOL_GPL(ata_bmdma_thaw);
7155EXPORT_SYMBOL_GPL(ata_bmdma_drive_eh);
7156EXPORT_SYMBOL_GPL(ata_bmdma_error_handler);
7157EXPORT_SYMBOL_GPL(ata_bmdma_post_internal_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007158EXPORT_SYMBOL_GPL(ata_port_probe);
Alan10305f02007-02-20 18:01:59 +00007159EXPORT_SYMBOL_GPL(ata_dev_disable);
Tejun Heo3c567b72006-05-15 20:57:23 +09007160EXPORT_SYMBOL_GPL(sata_set_spd);
Tejun Heo936fd732007-08-06 18:36:23 +09007161EXPORT_SYMBOL_GPL(sata_link_debounce);
7162EXPORT_SYMBOL_GPL(sata_link_resume);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007163EXPORT_SYMBOL_GPL(sata_phy_reset);
7164EXPORT_SYMBOL_GPL(__sata_phy_reset);
7165EXPORT_SYMBOL_GPL(ata_bus_reset);
Tejun Heof5914a42006-05-31 18:27:48 +09007166EXPORT_SYMBOL_GPL(ata_std_prereset);
Tejun Heoc2bd5802006-01-24 17:05:22 +09007167EXPORT_SYMBOL_GPL(ata_std_softreset);
Tejun Heocc0680a2007-08-06 18:36:23 +09007168EXPORT_SYMBOL_GPL(sata_link_hardreset);
Tejun Heoc2bd5802006-01-24 17:05:22 +09007169EXPORT_SYMBOL_GPL(sata_std_hardreset);
7170EXPORT_SYMBOL_GPL(ata_std_postreset);
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05007171EXPORT_SYMBOL_GPL(ata_dev_classify);
7172EXPORT_SYMBOL_GPL(ata_dev_pair);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007173EXPORT_SYMBOL_GPL(ata_port_disable);
Jeff Garzik67846b32005-10-05 02:58:32 -04007174EXPORT_SYMBOL_GPL(ata_ratelimit);
Tejun Heoc22daff2006-04-11 22:22:29 +09007175EXPORT_SYMBOL_GPL(ata_wait_register);
Tejun Heo6f8b9952006-01-24 17:05:21 +09007176EXPORT_SYMBOL_GPL(ata_busy_sleep);
Tejun Heod4b2bab2007-02-02 16:50:52 +09007177EXPORT_SYMBOL_GPL(ata_wait_ready);
Tejun Heo86e45b62006-03-05 15:29:09 +09007178EXPORT_SYMBOL_GPL(ata_port_queue_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007179EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
7180EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007181EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
Tejun Heo83c47bc2006-05-31 18:28:07 +09007182EXPORT_SYMBOL_GPL(ata_scsi_slave_destroy);
Tejun Heoa6e6ce82006-05-15 21:03:48 +09007183EXPORT_SYMBOL_GPL(ata_scsi_change_queue_depth);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007184EXPORT_SYMBOL_GPL(ata_host_intr);
Tejun Heo34bf2172006-05-15 20:57:46 +09007185EXPORT_SYMBOL_GPL(sata_scr_valid);
7186EXPORT_SYMBOL_GPL(sata_scr_read);
7187EXPORT_SYMBOL_GPL(sata_scr_write);
7188EXPORT_SYMBOL_GPL(sata_scr_write_flush);
Tejun Heo936fd732007-08-06 18:36:23 +09007189EXPORT_SYMBOL_GPL(ata_link_online);
7190EXPORT_SYMBOL_GPL(ata_link_offline);
Tejun Heo6ffa01d2007-03-02 17:32:47 +09007191#ifdef CONFIG_PM
Jeff Garzikcca39742006-08-24 03:19:22 -04007192EXPORT_SYMBOL_GPL(ata_host_suspend);
7193EXPORT_SYMBOL_GPL(ata_host_resume);
Tejun Heo6ffa01d2007-03-02 17:32:47 +09007194#endif /* CONFIG_PM */
Tejun Heo6a62a042006-02-13 10:02:46 +09007195EXPORT_SYMBOL_GPL(ata_id_string);
7196EXPORT_SYMBOL_GPL(ata_id_c_string);
Alan10305f02007-02-20 18:01:59 +00007197EXPORT_SYMBOL_GPL(ata_id_to_dma_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007198EXPORT_SYMBOL_GPL(ata_scsi_simulate);
7199
Alan Cox1bc4ccf2006-01-09 17:18:14 +00007200EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
Alan Cox452503f2005-10-21 19:01:32 -04007201EXPORT_SYMBOL_GPL(ata_timing_compute);
7202EXPORT_SYMBOL_GPL(ata_timing_merge);
7203
Linus Torvalds1da177e2005-04-16 15:20:36 -07007204#ifdef CONFIG_PCI
7205EXPORT_SYMBOL_GPL(pci_test_config_bits);
Tejun Heod583bc12007-07-04 18:02:07 +09007206EXPORT_SYMBOL_GPL(ata_pci_init_sff_host);
Tejun Heo1626aeb2007-05-04 12:43:58 +02007207EXPORT_SYMBOL_GPL(ata_pci_init_bmdma);
Tejun Heod583bc12007-07-04 18:02:07 +09007208EXPORT_SYMBOL_GPL(ata_pci_prepare_sff_host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007209EXPORT_SYMBOL_GPL(ata_pci_init_one);
7210EXPORT_SYMBOL_GPL(ata_pci_remove_one);
Tejun Heo6ffa01d2007-03-02 17:32:47 +09007211#ifdef CONFIG_PM
Tejun Heo500530f2006-07-03 16:07:27 +09007212EXPORT_SYMBOL_GPL(ata_pci_device_do_suspend);
7213EXPORT_SYMBOL_GPL(ata_pci_device_do_resume);
Jens Axboe9b847542006-01-06 09:28:07 +01007214EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
7215EXPORT_SYMBOL_GPL(ata_pci_device_resume);
Tejun Heo6ffa01d2007-03-02 17:32:47 +09007216#endif /* CONFIG_PM */
Alan Cox67951ad2006-03-22 15:55:54 +00007217EXPORT_SYMBOL_GPL(ata_pci_default_filter);
7218EXPORT_SYMBOL_GPL(ata_pci_clear_simplex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007219#endif /* CONFIG_PCI */
Jens Axboe9b847542006-01-06 09:28:07 +01007220
Tejun Heob64bbc32007-07-16 14:29:39 +09007221EXPORT_SYMBOL_GPL(__ata_ehi_push_desc);
7222EXPORT_SYMBOL_GPL(ata_ehi_push_desc);
7223EXPORT_SYMBOL_GPL(ata_ehi_clear_desc);
Tejun Heocbcdd872007-08-18 13:14:55 +09007224EXPORT_SYMBOL_GPL(ata_port_desc);
7225#ifdef CONFIG_PCI
7226EXPORT_SYMBOL_GPL(ata_port_pbar_desc);
7227#endif /* CONFIG_PCI */
Tejun Heoece1d632006-04-02 18:51:53 +09007228EXPORT_SYMBOL_GPL(ata_eng_timeout);
Tejun Heo7b70fc02006-05-15 20:58:07 +09007229EXPORT_SYMBOL_GPL(ata_port_schedule_eh);
Tejun Heodbd82612007-08-06 18:36:23 +09007230EXPORT_SYMBOL_GPL(ata_link_abort);
Tejun Heo7b70fc02006-05-15 20:58:07 +09007231EXPORT_SYMBOL_GPL(ata_port_abort);
Tejun Heoe3180492006-05-15 20:58:09 +09007232EXPORT_SYMBOL_GPL(ata_port_freeze);
7233EXPORT_SYMBOL_GPL(ata_eh_freeze_port);
7234EXPORT_SYMBOL_GPL(ata_eh_thaw_port);
Tejun Heoece1d632006-04-02 18:51:53 +09007235EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
7236EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
Tejun Heo022bdb02006-05-15 20:58:22 +09007237EXPORT_SYMBOL_GPL(ata_do_eh);
Akira Iguchi83625002007-01-26 16:27:32 +09007238EXPORT_SYMBOL_GPL(ata_irq_on);
Akira Iguchia619f981b2007-01-26 16:28:18 +09007239EXPORT_SYMBOL_GPL(ata_dev_try_classify);
Alan Coxbe0d18d2007-03-06 02:37:56 -08007240
7241EXPORT_SYMBOL_GPL(ata_cable_40wire);
7242EXPORT_SYMBOL_GPL(ata_cable_80wire);
7243EXPORT_SYMBOL_GPL(ata_cable_unknown);
7244EXPORT_SYMBOL_GPL(ata_cable_sata);