blob: 8b08e7bdd24d59727bf3d7e3a94266a27e22e0d5 [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;
Tejun Heoda917d62007-09-23 13:14:12 +09001393 int preempted_nr_active_links;
Ingo Molnar60be6b92006-07-03 00:25:26 -07001394 DECLARE_COMPLETION_ONSTACK(wait);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001395 unsigned long flags;
Tejun Heo77853bf2006-01-23 13:09:36 +09001396 unsigned int err_mask;
Tejun Heod95a7172006-05-15 20:58:14 +09001397 int rc;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001398
Jeff Garzikba6a1302006-06-22 23:46:10 -04001399 spin_lock_irqsave(ap->lock, flags);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001400
Tejun Heoe3180492006-05-15 20:58:09 +09001401 /* no internal command while frozen */
Tejun Heob51e9e52006-06-29 01:29:30 +09001402 if (ap->pflags & ATA_PFLAG_FROZEN) {
Jeff Garzikba6a1302006-06-22 23:46:10 -04001403 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +09001404 return AC_ERR_SYSTEM;
1405 }
1406
Tejun Heo2ab7db12006-05-15 20:58:02 +09001407 /* initialize internal qc */
Tejun Heoa2a7a662005-12-13 14:48:31 +09001408
Tejun Heo2ab7db12006-05-15 20:58:02 +09001409 /* XXX: Tag 0 is used for drivers with legacy EH as some
1410 * drivers choke if any other tag is given. This breaks
1411 * ata_tag_internal() test for those drivers. Don't use new
1412 * EH stuff without converting to it.
1413 */
1414 if (ap->ops->error_handler)
1415 tag = ATA_TAG_INTERNAL;
1416 else
1417 tag = 0;
1418
Tejun Heo6cec4a32006-05-15 21:03:41 +09001419 if (test_and_set_bit(tag, &ap->qc_allocated))
Tejun Heo2ab7db12006-05-15 20:58:02 +09001420 BUG();
Tejun Heof69499f2006-05-15 20:58:03 +09001421 qc = __ata_qc_from_tag(ap, tag);
Tejun Heo2ab7db12006-05-15 20:58:02 +09001422
1423 qc->tag = tag;
1424 qc->scsicmd = NULL;
1425 qc->ap = ap;
1426 qc->dev = dev;
1427 ata_qc_reinit(qc);
1428
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001429 preempted_tag = link->active_tag;
1430 preempted_sactive = link->sactive;
Tejun Heodedaf2b2006-05-15 21:03:43 +09001431 preempted_qc_active = ap->qc_active;
Tejun Heoda917d62007-09-23 13:14:12 +09001432 preempted_nr_active_links = ap->nr_active_links;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001433 link->active_tag = ATA_TAG_POISON;
1434 link->sactive = 0;
Tejun Heodedaf2b2006-05-15 21:03:43 +09001435 ap->qc_active = 0;
Tejun Heoda917d62007-09-23 13:14:12 +09001436 ap->nr_active_links = 0;
Tejun Heo2ab7db12006-05-15 20:58:02 +09001437
1438 /* prepare & issue qc */
Tejun Heoa2a7a662005-12-13 14:48:31 +09001439 qc->tf = *tf;
Tejun Heod69cf372006-04-02 18:51:53 +09001440 if (cdb)
1441 memcpy(qc->cdb, cdb, ATAPI_CDB_LEN);
Tejun Heoe61e0672006-05-15 20:57:40 +09001442 qc->flags |= ATA_QCFLAG_RESULT_TF;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001443 qc->dma_dir = dma_dir;
1444 if (dma_dir != DMA_NONE) {
Tejun Heo24326972006-11-14 22:47:09 +09001445 unsigned int i, buflen = 0;
1446
1447 for (i = 0; i < n_elem; i++)
1448 buflen += sg[i].length;
1449
1450 ata_sg_init(qc, sg, n_elem);
Brian King49c80422007-01-30 11:32:26 -06001451 qc->nbytes = buflen;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001452 }
1453
Tejun Heo77853bf2006-01-23 13:09:36 +09001454 qc->private_data = &wait;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001455 qc->complete_fn = ata_qc_complete_internal;
1456
Tejun Heo8e0e6942006-03-31 20:41:11 +09001457 ata_qc_issue(qc);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001458
Jeff Garzikba6a1302006-06-22 23:46:10 -04001459 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001460
Andrew Mortona8601e52006-06-25 01:36:52 -07001461 rc = wait_for_completion_timeout(&wait, ata_probe_timeout);
Albert Lee41ade502006-03-14 11:19:04 +08001462
Tejun Heod95a7172006-05-15 20:58:14 +09001463 ata_port_flush_task(ap);
1464
1465 if (!rc) {
Jeff Garzikba6a1302006-06-22 23:46:10 -04001466 spin_lock_irqsave(ap->lock, flags);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001467
1468 /* We're racing with irq here. If we lose, the
1469 * following test prevents us from completing the qc
Tejun Heod95a7172006-05-15 20:58:14 +09001470 * twice. If we win, the port is frozen and will be
1471 * cleaned up by ->post_internal_cmd().
Tejun Heoa2a7a662005-12-13 14:48:31 +09001472 */
Tejun Heo77853bf2006-01-23 13:09:36 +09001473 if (qc->flags & ATA_QCFLAG_ACTIVE) {
Tejun Heod95a7172006-05-15 20:58:14 +09001474 qc->err_mask |= AC_ERR_TIMEOUT;
1475
1476 if (ap->ops->error_handler)
1477 ata_port_freeze(ap);
1478 else
1479 ata_qc_complete(qc);
Tejun Heof15a1da2006-05-15 20:57:56 +09001480
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001481 if (ata_msg_warn(ap))
1482 ata_dev_printk(dev, KERN_WARNING,
Tejun Heo88574552006-06-25 20:00:35 +09001483 "qc timeout (cmd 0x%x)\n", command);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001484 }
1485
Jeff Garzikba6a1302006-06-22 23:46:10 -04001486 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001487 }
1488
Tejun Heod95a7172006-05-15 20:58:14 +09001489 /* do post_internal_cmd */
1490 if (ap->ops->post_internal_cmd)
1491 ap->ops->post_internal_cmd(qc);
1492
Tejun Heoa51d6442007-03-20 15:24:11 +09001493 /* perform minimal error analysis */
1494 if (qc->flags & ATA_QCFLAG_FAILED) {
1495 if (qc->result_tf.command & (ATA_ERR | ATA_DF))
1496 qc->err_mask |= AC_ERR_DEV;
1497
1498 if (!qc->err_mask)
1499 qc->err_mask |= AC_ERR_OTHER;
1500
1501 if (qc->err_mask & ~AC_ERR_OTHER)
1502 qc->err_mask &= ~AC_ERR_OTHER;
Tejun Heod95a7172006-05-15 20:58:14 +09001503 }
1504
Tejun Heo15869302006-05-15 20:57:33 +09001505 /* finish up */
Jeff Garzikba6a1302006-06-22 23:46:10 -04001506 spin_lock_irqsave(ap->lock, flags);
Tejun Heo15869302006-05-15 20:57:33 +09001507
Tejun Heoe61e0672006-05-15 20:57:40 +09001508 *tf = qc->result_tf;
Tejun Heo77853bf2006-01-23 13:09:36 +09001509 err_mask = qc->err_mask;
1510
1511 ata_qc_free(qc);
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001512 link->active_tag = preempted_tag;
1513 link->sactive = preempted_sactive;
Tejun Heodedaf2b2006-05-15 21:03:43 +09001514 ap->qc_active = preempted_qc_active;
Tejun Heoda917d62007-09-23 13:14:12 +09001515 ap->nr_active_links = preempted_nr_active_links;
Tejun Heo77853bf2006-01-23 13:09:36 +09001516
Tejun Heo1f7dd3e92006-03-24 15:25:30 +09001517 /* XXX - Some LLDDs (sata_mv) disable port on command failure.
1518 * Until those drivers are fixed, we detect the condition
1519 * here, fail the command with AC_ERR_SYSTEM and reenable the
1520 * port.
1521 *
1522 * Note that this doesn't change any behavior as internal
1523 * command failure results in disabling the device in the
1524 * higher layer for LLDDs without new reset/EH callbacks.
1525 *
1526 * Kill the following code as soon as those drivers are fixed.
1527 */
Tejun Heo198e0fe2006-04-02 18:51:52 +09001528 if (ap->flags & ATA_FLAG_DISABLED) {
Tejun Heo1f7dd3e92006-03-24 15:25:30 +09001529 err_mask |= AC_ERR_SYSTEM;
1530 ata_port_probe(ap);
1531 }
1532
Jeff Garzikba6a1302006-06-22 23:46:10 -04001533 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo15869302006-05-15 20:57:33 +09001534
Tejun Heo77853bf2006-01-23 13:09:36 +09001535 return err_mask;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001536}
1537
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538/**
Tejun Heo33480a02006-12-12 02:15:31 +09001539 * ata_exec_internal - execute libata internal command
Tejun Heo24326972006-11-14 22:47:09 +09001540 * @dev: Device to which the command is sent
1541 * @tf: Taskfile registers for the command and the result
1542 * @cdb: CDB for packet command
1543 * @dma_dir: Data tranfer direction of the command
1544 * @buf: Data buffer of the command
1545 * @buflen: Length of data buffer
1546 *
1547 * Wrapper around ata_exec_internal_sg() which takes simple
1548 * buffer instead of sg list.
1549 *
1550 * LOCKING:
1551 * None. Should be called with kernel context, might sleep.
1552 *
1553 * RETURNS:
1554 * Zero on success, AC_ERR_* mask on failure
1555 */
1556unsigned ata_exec_internal(struct ata_device *dev,
1557 struct ata_taskfile *tf, const u8 *cdb,
1558 int dma_dir, void *buf, unsigned int buflen)
1559{
Tejun Heo33480a02006-12-12 02:15:31 +09001560 struct scatterlist *psg = NULL, sg;
1561 unsigned int n_elem = 0;
Tejun Heo24326972006-11-14 22:47:09 +09001562
Tejun Heo33480a02006-12-12 02:15:31 +09001563 if (dma_dir != DMA_NONE) {
1564 WARN_ON(!buf);
1565 sg_init_one(&sg, buf, buflen);
1566 psg = &sg;
1567 n_elem++;
1568 }
Tejun Heo24326972006-11-14 22:47:09 +09001569
Tejun Heo33480a02006-12-12 02:15:31 +09001570 return ata_exec_internal_sg(dev, tf, cdb, dma_dir, psg, n_elem);
Tejun Heo24326972006-11-14 22:47:09 +09001571}
1572
1573/**
Tejun Heo977e6b92006-06-24 20:30:19 +09001574 * ata_do_simple_cmd - execute simple internal command
1575 * @dev: Device to which the command is sent
1576 * @cmd: Opcode to execute
1577 *
1578 * Execute a 'simple' command, that only consists of the opcode
1579 * 'cmd' itself, without filling any other registers
1580 *
1581 * LOCKING:
1582 * Kernel thread context (may sleep).
1583 *
1584 * RETURNS:
1585 * Zero on success, AC_ERR_* mask on failure
Tejun Heoe58eb582006-06-24 20:30:19 +09001586 */
Tejun Heo77b08fb2006-06-24 20:30:19 +09001587unsigned int ata_do_simple_cmd(struct ata_device *dev, u8 cmd)
Tejun Heoe58eb582006-06-24 20:30:19 +09001588{
1589 struct ata_taskfile tf;
Tejun Heoe58eb582006-06-24 20:30:19 +09001590
1591 ata_tf_init(dev, &tf);
1592
1593 tf.command = cmd;
1594 tf.flags |= ATA_TFLAG_DEVICE;
1595 tf.protocol = ATA_PROT_NODATA;
1596
Tejun Heo977e6b92006-06-24 20:30:19 +09001597 return ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
Tejun Heoe58eb582006-06-24 20:30:19 +09001598}
1599
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600/**
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001601 * ata_pio_need_iordy - check if iordy needed
1602 * @adev: ATA device
1603 *
1604 * Check if the current speed of the device requires IORDY. Used
1605 * by various controllers for chip configuration.
1606 */
Jeff Garzika617c092007-05-21 20:14:23 -04001607
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001608unsigned int ata_pio_need_iordy(const struct ata_device *adev)
1609{
Alan Cox432729f2007-03-08 23:22:59 +00001610 /* Controller doesn't support IORDY. Probably a pointless check
1611 as the caller should know this */
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001612 if (adev->link->ap->flags & ATA_FLAG_NO_IORDY)
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001613 return 0;
Alan Cox432729f2007-03-08 23:22:59 +00001614 /* PIO3 and higher it is mandatory */
1615 if (adev->pio_mode > XFER_PIO_2)
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001616 return 1;
Alan Cox432729f2007-03-08 23:22:59 +00001617 /* We turn it on when possible */
1618 if (ata_id_has_iordy(adev->id))
1619 return 1;
1620 return 0;
1621}
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001622
Alan Cox432729f2007-03-08 23:22:59 +00001623/**
1624 * ata_pio_mask_no_iordy - Return the non IORDY mask
1625 * @adev: ATA device
1626 *
1627 * Compute the highest mode possible if we are not using iordy. Return
1628 * -1 if no iordy mode is available.
1629 */
Jeff Garzika617c092007-05-21 20:14:23 -04001630
Alan Cox432729f2007-03-08 23:22:59 +00001631static u32 ata_pio_mask_no_iordy(const struct ata_device *adev)
1632{
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001633 /* If we have no drive specific rule, then PIO 2 is non IORDY */
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001634 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
Alan Cox432729f2007-03-08 23:22:59 +00001635 u16 pio = adev->id[ATA_ID_EIDE_PIO];
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001636 /* Is the speed faster than the drive allows non IORDY ? */
1637 if (pio) {
1638 /* This is cycle times not frequency - watch the logic! */
1639 if (pio > 240) /* PIO2 is 240nS per cycle */
Alan Cox432729f2007-03-08 23:22:59 +00001640 return 3 << ATA_SHIFT_PIO;
1641 return 7 << ATA_SHIFT_PIO;
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001642 }
1643 }
Alan Cox432729f2007-03-08 23:22:59 +00001644 return 3 << ATA_SHIFT_PIO;
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001645}
1646
1647/**
Tejun Heo49016ac2006-02-21 02:12:11 +09001648 * ata_dev_read_id - Read ID data from the specified device
Tejun Heo49016ac2006-02-21 02:12:11 +09001649 * @dev: target device
1650 * @p_class: pointer to class of the target device (may be changed)
Tejun Heobff04642006-11-10 18:08:10 +09001651 * @flags: ATA_READID_* flags
Tejun Heofe635c72006-05-15 20:57:35 +09001652 * @id: buffer to read IDENTIFY data into
Tejun Heo49016ac2006-02-21 02:12:11 +09001653 *
1654 * Read ID data from the specified device. ATA_CMD_ID_ATA is
1655 * performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
Tejun Heoaec5c3c2006-03-25 01:33:34 +09001656 * devices. This function also issues ATA_CMD_INIT_DEV_PARAMS
1657 * for pre-ATA4 drives.
Tejun Heo49016ac2006-02-21 02:12:11 +09001658 *
Alan Cox50a99012007-08-08 14:27:00 +01001659 * FIXME: ATA_CMD_ID_ATA is optional for early drives and right
1660 * now we abort if we hit that case.
1661 *
Tejun Heo49016ac2006-02-21 02:12:11 +09001662 * LOCKING:
1663 * Kernel thread context (may sleep)
1664 *
1665 * RETURNS:
1666 * 0 on success, -errno otherwise.
1667 */
Tejun Heoa9beec92006-05-31 18:27:44 +09001668int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
Tejun Heobff04642006-11-10 18:08:10 +09001669 unsigned int flags, u16 *id)
Tejun Heo49016ac2006-02-21 02:12:11 +09001670{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001671 struct ata_port *ap = dev->link->ap;
Tejun Heo49016ac2006-02-21 02:12:11 +09001672 unsigned int class = *p_class;
Tejun Heo49016ac2006-02-21 02:12:11 +09001673 struct ata_taskfile tf;
1674 unsigned int err_mask = 0;
1675 const char *reason;
Tejun Heo54936f82007-05-11 14:35:29 +02001676 int may_fallback = 1, tried_spinup = 0;
Tejun Heo49016ac2006-02-21 02:12:11 +09001677 int rc;
1678
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001679 if (ata_msg_ctl(ap))
Tejun Heo44877b42007-02-21 01:06:51 +09001680 ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER\n", __FUNCTION__);
Tejun Heo49016ac2006-02-21 02:12:11 +09001681
Tejun Heo49016ac2006-02-21 02:12:11 +09001682 ata_dev_select(ap, dev->devno, 1, 1); /* select device 0/1 */
Tejun Heo49016ac2006-02-21 02:12:11 +09001683 retry:
Tejun Heo3373efd2006-05-15 20:57:53 +09001684 ata_tf_init(dev, &tf);
Tejun Heo49016ac2006-02-21 02:12:11 +09001685
1686 switch (class) {
1687 case ATA_DEV_ATA:
1688 tf.command = ATA_CMD_ID_ATA;
1689 break;
1690 case ATA_DEV_ATAPI:
1691 tf.command = ATA_CMD_ID_ATAPI;
1692 break;
1693 default:
1694 rc = -ENODEV;
1695 reason = "unsupported class";
1696 goto err_out;
1697 }
1698
1699 tf.protocol = ATA_PROT_PIO;
Tejun Heo81afe892007-02-07 12:37:41 -08001700
1701 /* Some devices choke if TF registers contain garbage. Make
1702 * sure those are properly initialized.
1703 */
1704 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1705
1706 /* Device presence detection is unreliable on some
1707 * controllers. Always poll IDENTIFY if available.
1708 */
1709 tf.flags |= ATA_TFLAG_POLLING;
Tejun Heo49016ac2006-02-21 02:12:11 +09001710
Tejun Heo3373efd2006-05-15 20:57:53 +09001711 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
Tejun Heo49016ac2006-02-21 02:12:11 +09001712 id, sizeof(id[0]) * ATA_ID_WORDS);
Tejun Heo49016ac2006-02-21 02:12:11 +09001713 if (err_mask) {
Tejun Heo800b3992006-12-03 21:34:13 +09001714 if (err_mask & AC_ERR_NODEV_HINT) {
Tejun Heo55a8e2c2006-11-10 18:08:10 +09001715 DPRINTK("ata%u.%d: NODEV after polling detection\n",
Tejun Heo44877b42007-02-21 01:06:51 +09001716 ap->print_id, dev->devno);
Tejun Heo55a8e2c2006-11-10 18:08:10 +09001717 return -ENOENT;
1718 }
1719
Tejun Heo54936f82007-05-11 14:35:29 +02001720 /* Device or controller might have reported the wrong
1721 * device class. Give a shot at the other IDENTIFY if
1722 * the current one is aborted by the device.
1723 */
1724 if (may_fallback &&
1725 (err_mask == AC_ERR_DEV) && (tf.feature & ATA_ABORTED)) {
1726 may_fallback = 0;
1727
1728 if (class == ATA_DEV_ATA)
1729 class = ATA_DEV_ATAPI;
1730 else
1731 class = ATA_DEV_ATA;
1732 goto retry;
1733 }
1734
Tejun Heo49016ac2006-02-21 02:12:11 +09001735 rc = -EIO;
1736 reason = "I/O error";
Tejun Heo49016ac2006-02-21 02:12:11 +09001737 goto err_out;
1738 }
1739
Tejun Heo54936f82007-05-11 14:35:29 +02001740 /* Falling back doesn't make sense if ID data was read
1741 * successfully at least once.
1742 */
1743 may_fallback = 0;
1744
Tejun Heo49016ac2006-02-21 02:12:11 +09001745 swap_buf_le16(id, ATA_ID_WORDS);
1746
Tejun Heo49016ac2006-02-21 02:12:11 +09001747 /* sanity check */
Tejun Heoa4f57492006-09-12 20:35:49 -07001748 rc = -EINVAL;
Alan Cox60700682007-06-07 16:13:55 +01001749 reason = "device reports invalid type";
Tejun Heoa4f57492006-09-12 20:35:49 -07001750
1751 if (class == ATA_DEV_ATA) {
1752 if (!ata_id_is_ata(id) && !ata_id_is_cfa(id))
1753 goto err_out;
1754 } else {
1755 if (ata_id_is_ata(id))
1756 goto err_out;
Tejun Heo49016ac2006-02-21 02:12:11 +09001757 }
1758
Mark Lord169439c2007-04-17 18:26:07 -04001759 if (!tried_spinup && (id[2] == 0x37c8 || id[2] == 0x738c)) {
1760 tried_spinup = 1;
1761 /*
1762 * Drive powered-up in standby mode, and requires a specific
1763 * SET_FEATURES spin-up subcommand before it will accept
1764 * anything other than the original IDENTIFY command.
1765 */
1766 ata_tf_init(dev, &tf);
1767 tf.command = ATA_CMD_SET_FEATURES;
1768 tf.feature = SETFEATURES_SPINUP;
1769 tf.protocol = ATA_PROT_NODATA;
1770 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1771 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
Ryan Powerfb0582f92007-08-10 13:59:35 -07001772 if (err_mask && id[2] != 0x738c) {
Mark Lord169439c2007-04-17 18:26:07 -04001773 rc = -EIO;
1774 reason = "SPINUP failed";
1775 goto err_out;
1776 }
1777 /*
1778 * If the drive initially returned incomplete IDENTIFY info,
1779 * we now must reissue the IDENTIFY command.
1780 */
1781 if (id[2] == 0x37c8)
1782 goto retry;
1783 }
1784
Tejun Heobff04642006-11-10 18:08:10 +09001785 if ((flags & ATA_READID_POSTRESET) && class == ATA_DEV_ATA) {
Tejun Heo49016ac2006-02-21 02:12:11 +09001786 /*
1787 * The exact sequence expected by certain pre-ATA4 drives is:
1788 * SRST RESET
Alan Cox50a99012007-08-08 14:27:00 +01001789 * IDENTIFY (optional in early ATA)
1790 * INITIALIZE DEVICE PARAMETERS (later IDE and ATA)
Tejun Heo49016ac2006-02-21 02:12:11 +09001791 * anything else..
1792 * Some drives were very specific about that exact sequence.
Alan Cox50a99012007-08-08 14:27:00 +01001793 *
1794 * Note that ATA4 says lba is mandatory so the second check
1795 * shoud never trigger.
Tejun Heo49016ac2006-02-21 02:12:11 +09001796 */
1797 if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
Tejun Heo3373efd2006-05-15 20:57:53 +09001798 err_mask = ata_dev_init_params(dev, id[3], id[6]);
Tejun Heo49016ac2006-02-21 02:12:11 +09001799 if (err_mask) {
1800 rc = -EIO;
1801 reason = "INIT_DEV_PARAMS failed";
1802 goto err_out;
1803 }
1804
1805 /* current CHS translation info (id[53-58]) might be
1806 * changed. reread the identify device info.
1807 */
Tejun Heobff04642006-11-10 18:08:10 +09001808 flags &= ~ATA_READID_POSTRESET;
Tejun Heo49016ac2006-02-21 02:12:11 +09001809 goto retry;
1810 }
1811 }
1812
1813 *p_class = class;
Tejun Heofe635c72006-05-15 20:57:35 +09001814
Tejun Heo49016ac2006-02-21 02:12:11 +09001815 return 0;
1816
1817 err_out:
Tejun Heo88574552006-06-25 20:00:35 +09001818 if (ata_msg_warn(ap))
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001819 ata_dev_printk(dev, KERN_WARNING, "failed to IDENTIFY "
Tejun Heo88574552006-06-25 20:00:35 +09001820 "(%s, err_mask=0x%x)\n", reason, err_mask);
Tejun Heo49016ac2006-02-21 02:12:11 +09001821 return rc;
1822}
1823
Tejun Heo3373efd2006-05-15 20:57:53 +09001824static inline u8 ata_dev_knobble(struct ata_device *dev)
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09001825{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001826 struct ata_port *ap = dev->link->ap;
1827 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09001828}
1829
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001830static void ata_dev_config_ncq(struct ata_device *dev,
1831 char *desc, size_t desc_sz)
1832{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001833 struct ata_port *ap = dev->link->ap;
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001834 int hdepth = 0, ddepth = ata_id_queue_depth(dev->id);
1835
1836 if (!ata_id_has_ncq(dev->id)) {
1837 desc[0] = '\0';
1838 return;
1839 }
Tejun Heo75683fe2007-07-05 13:31:27 +09001840 if (dev->horkage & ATA_HORKAGE_NONCQ) {
Alan Cox6919a0a2006-10-27 19:08:46 -07001841 snprintf(desc, desc_sz, "NCQ (not used)");
1842 return;
1843 }
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001844 if (ap->flags & ATA_FLAG_NCQ) {
Jeff Garzikcca39742006-08-24 03:19:22 -04001845 hdepth = min(ap->scsi_host->can_queue, ATA_MAX_QUEUE - 1);
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001846 dev->flags |= ATA_DFLAG_NCQ;
1847 }
1848
1849 if (hdepth >= ddepth)
1850 snprintf(desc, desc_sz, "NCQ (depth %d)", ddepth);
1851 else
1852 snprintf(desc, desc_sz, "NCQ (depth %d/%d)", hdepth, ddepth);
1853}
1854
Tejun Heo49016ac2006-02-21 02:12:11 +09001855/**
Tejun Heoffeae412006-03-01 16:09:35 +09001856 * ata_dev_configure - Configure the specified ATA/ATAPI device
Tejun Heoffeae412006-03-01 16:09:35 +09001857 * @dev: Target device to configure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 *
Tejun Heoffeae412006-03-01 16:09:35 +09001859 * Configure @dev according to @dev->id. Generic and low-level
1860 * driver specific fixups are also applied.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 *
1862 * LOCKING:
Tejun Heoffeae412006-03-01 16:09:35 +09001863 * Kernel thread context (may sleep)
1864 *
1865 * RETURNS:
1866 * 0 on success, -errno otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 */
Tejun Heoefdaedc2006-11-01 18:38:52 +09001868int ata_dev_configure(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001870 struct ata_port *ap = dev->link->ap;
1871 struct ata_eh_context *ehc = &dev->link->eh_context;
Tejun Heo67465442007-05-15 03:28:16 +09001872 int print_info = ehc->i.flags & ATA_EHI_PRINTINFO;
Tejun Heo1148c3a2006-03-13 19:48:04 +09001873 const u16 *id = dev->id;
Tejun Heoff8854b2006-03-06 04:31:56 +09001874 unsigned int xfer_mask;
Alan Coxb352e572006-08-10 18:52:12 +01001875 char revbuf[7]; /* XYZ-99\0 */
Eric D. Mudama3f64f562007-01-30 23:00:40 -07001876 char fwrevbuf[ATA_ID_FW_REV_LEN+1];
1877 char modelbuf[ATA_ID_PROD_LEN+1];
Brian Kinge6d902a2006-06-28 08:30:31 -05001878 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001880 if (!ata_dev_enabled(dev) && ata_msg_info(ap)) {
Tejun Heo44877b42007-02-21 01:06:51 +09001881 ata_dev_printk(dev, KERN_INFO, "%s: ENTER/EXIT -- nodev\n",
1882 __FUNCTION__);
Tejun Heoffeae412006-03-01 16:09:35 +09001883 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 }
1885
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001886 if (ata_msg_probe(ap))
Tejun Heo44877b42007-02-21 01:06:51 +09001887 ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888
Tejun Heo75683fe2007-07-05 13:31:27 +09001889 /* set horkage */
1890 dev->horkage |= ata_dev_blacklisted(dev);
1891
Tejun Heo67465442007-05-15 03:28:16 +09001892 /* let ACPI work its magic */
1893 rc = ata_acpi_on_devcfg(dev);
1894 if (rc)
1895 return rc;
Kristen Carlson Accardi08573a82006-11-10 16:14:47 -08001896
Tejun Heo05027ad2007-09-03 12:32:57 +09001897 /* massage HPA, do it early as it might change IDENTIFY data */
1898 rc = ata_hpa_resize(dev);
1899 if (rc)
1900 return rc;
1901
Tejun Heoc39f5eb2006-03-13 19:51:19 +09001902 /* print device capabilities */
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001903 if (ata_msg_probe(ap))
Tejun Heo88574552006-06-25 20:00:35 +09001904 ata_dev_printk(dev, KERN_DEBUG,
1905 "%s: cfg 49:%04x 82:%04x 83:%04x 84:%04x "
1906 "85:%04x 86:%04x 87:%04x 88:%04x\n",
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001907 __FUNCTION__,
Tejun Heof15a1da2006-05-15 20:57:56 +09001908 id[49], id[82], id[83], id[84],
1909 id[85], id[86], id[87], id[88]);
Tejun Heoc39f5eb2006-03-13 19:51:19 +09001910
Tejun Heo208a9932006-03-05 17:55:58 +09001911 /* initialize to-be-configured parameters */
Tejun Heoea1dd4e2006-04-02 18:51:53 +09001912 dev->flags &= ~ATA_DFLAG_CFG_MASK;
Tejun Heo208a9932006-03-05 17:55:58 +09001913 dev->max_sectors = 0;
1914 dev->cdb_len = 0;
1915 dev->n_sectors = 0;
1916 dev->cylinders = 0;
1917 dev->heads = 0;
1918 dev->sectors = 0;
1919
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 /*
1921 * common ATA, ATAPI feature tests
1922 */
1923
Tejun Heoff8854b2006-03-06 04:31:56 +09001924 /* find max transfer mode; for printk only */
Tejun Heo1148c3a2006-03-13 19:48:04 +09001925 xfer_mask = ata_id_xfermask(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001927 if (ata_msg_probe(ap))
1928 ata_dump_id(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
Albert Leeef143d52007-06-05 13:01:33 +08001930 /* SCSI only uses 4-char revisions, dump full 8 chars from ATA */
1931 ata_id_c_string(dev->id, fwrevbuf, ATA_ID_FW_REV,
1932 sizeof(fwrevbuf));
1933
1934 ata_id_c_string(dev->id, modelbuf, ATA_ID_PROD,
1935 sizeof(modelbuf));
1936
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 /* ATA-specific feature tests */
1938 if (dev->class == ATA_DEV_ATA) {
Alan Coxb352e572006-08-10 18:52:12 +01001939 if (ata_id_is_cfa(id)) {
1940 if (id[162] & 1) /* CPRM may make this media unusable */
Tejun Heo44877b42007-02-21 01:06:51 +09001941 ata_dev_printk(dev, KERN_WARNING,
1942 "supports DRM functions and may "
1943 "not be fully accessable.\n");
Alan Coxb352e572006-08-10 18:52:12 +01001944 snprintf(revbuf, 7, "CFA");
1945 }
1946 else
1947 snprintf(revbuf, 7, "ATA-%d", ata_id_major_version(id));
1948
Tejun Heo1148c3a2006-03-13 19:48:04 +09001949 dev->n_sectors = ata_id_n_sectors(id);
Tejun Heo29407402006-02-12 22:47:04 +09001950
Eric D. Mudama3f64f562007-01-30 23:00:40 -07001951 if (dev->id[59] & 0x100)
1952 dev->multi_count = dev->id[59] & 0xff;
1953
Tejun Heo1148c3a2006-03-13 19:48:04 +09001954 if (ata_id_has_lba(id)) {
Tejun Heo4c2d7212006-03-05 17:55:58 +09001955 const char *lba_desc;
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001956 char ncq_desc[20];
Albert Lee8bf62ece2005-05-12 15:29:42 -04001957
Tejun Heo4c2d7212006-03-05 17:55:58 +09001958 lba_desc = "LBA";
1959 dev->flags |= ATA_DFLAG_LBA;
Tejun Heo1148c3a2006-03-13 19:48:04 +09001960 if (ata_id_has_lba48(id)) {
Albert Lee8bf62ece2005-05-12 15:29:42 -04001961 dev->flags |= ATA_DFLAG_LBA48;
Tejun Heo4c2d7212006-03-05 17:55:58 +09001962 lba_desc = "LBA48";
Tejun Heo6fc49ad2006-11-11 20:10:45 +09001963
1964 if (dev->n_sectors >= (1UL << 28) &&
1965 ata_id_has_flush_ext(id))
1966 dev->flags |= ATA_DFLAG_FLUSH_EXT;
Tejun Heo4c2d7212006-03-05 17:55:58 +09001967 }
Albert Lee8bf62ece2005-05-12 15:29:42 -04001968
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001969 /* config NCQ */
1970 ata_dev_config_ncq(dev, ncq_desc, sizeof(ncq_desc));
1971
Albert Lee8bf62ece2005-05-12 15:29:42 -04001972 /* print device info to dmesg */
Eric D. Mudama3f64f562007-01-30 23:00:40 -07001973 if (ata_msg_drv(ap) && print_info) {
1974 ata_dev_printk(dev, KERN_INFO,
1975 "%s: %s, %s, max %s\n",
1976 revbuf, modelbuf, fwrevbuf,
1977 ata_mode_string(xfer_mask));
1978 ata_dev_printk(dev, KERN_INFO,
1979 "%Lu sectors, multi %u: %s %s\n",
Tejun Heof15a1da2006-05-15 20:57:56 +09001980 (unsigned long long)dev->n_sectors,
Eric D. Mudama3f64f562007-01-30 23:00:40 -07001981 dev->multi_count, lba_desc, ncq_desc);
1982 }
Tejun Heoffeae412006-03-01 16:09:35 +09001983 } else {
Albert Lee8bf62ece2005-05-12 15:29:42 -04001984 /* CHS */
1985
1986 /* Default translation */
Tejun Heo1148c3a2006-03-13 19:48:04 +09001987 dev->cylinders = id[1];
1988 dev->heads = id[3];
1989 dev->sectors = id[6];
Albert Lee8bf62ece2005-05-12 15:29:42 -04001990
Tejun Heo1148c3a2006-03-13 19:48:04 +09001991 if (ata_id_current_chs_valid(id)) {
Albert Lee8bf62ece2005-05-12 15:29:42 -04001992 /* Current CHS translation is valid. */
Tejun Heo1148c3a2006-03-13 19:48:04 +09001993 dev->cylinders = id[54];
1994 dev->heads = id[55];
1995 dev->sectors = id[56];
Albert Lee8bf62ece2005-05-12 15:29:42 -04001996 }
1997
1998 /* print device info to dmesg */
Eric D. Mudama3f64f562007-01-30 23:00:40 -07001999 if (ata_msg_drv(ap) && print_info) {
Tejun Heo88574552006-06-25 20:00:35 +09002000 ata_dev_printk(dev, KERN_INFO,
Eric D. Mudama3f64f562007-01-30 23:00:40 -07002001 "%s: %s, %s, max %s\n",
2002 revbuf, modelbuf, fwrevbuf,
2003 ata_mode_string(xfer_mask));
Jeff Garzika84471f2007-02-26 05:51:33 -05002004 ata_dev_printk(dev, KERN_INFO,
Eric D. Mudama3f64f562007-01-30 23:00:40 -07002005 "%Lu sectors, multi %u, CHS %u/%u/%u\n",
2006 (unsigned long long)dev->n_sectors,
2007 dev->multi_count, dev->cylinders,
2008 dev->heads, dev->sectors);
2009 }
Albert Lee07f6f7d2005-11-01 19:33:20 +08002010 }
2011
Tejun Heo6e7846e2006-02-12 23:32:58 +09002012 dev->cdb_len = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 }
2014
2015 /* ATAPI-specific feature tests */
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05002016 else if (dev->class == ATA_DEV_ATAPI) {
Tejun Heo854c73a2007-09-23 13:14:11 +09002017 const char *cdb_intr_string = "";
2018 const char *atapi_an_string = "";
Tejun Heo7d77b242007-09-23 13:14:13 +09002019 u32 sntf;
Albert Lee08a556d2006-03-31 13:29:04 +08002020
Tejun Heo1148c3a2006-03-13 19:48:04 +09002021 rc = atapi_cdb_len(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002023 if (ata_msg_warn(ap))
Tejun Heo88574552006-06-25 20:00:35 +09002024 ata_dev_printk(dev, KERN_WARNING,
2025 "unsupported CDB len\n");
Tejun Heoffeae412006-03-01 16:09:35 +09002026 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 goto err_out_nosup;
2028 }
Tejun Heo6e7846e2006-02-12 23:32:58 +09002029 dev->cdb_len = (unsigned int) rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030
Tejun Heo7d77b242007-09-23 13:14:13 +09002031 /* Enable ATAPI AN if both the host and device have
2032 * the support. If PMP is attached, SNTF is required
2033 * to enable ATAPI AN to discern between PHY status
2034 * changed notifications and ATAPI ANs.
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04002035 */
Tejun Heo7d77b242007-09-23 13:14:13 +09002036 if ((ap->flags & ATA_FLAG_AN) && ata_id_has_atapi_AN(id) &&
2037 (!ap->nr_pmp_links ||
2038 sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf) == 0)) {
Tejun Heo854c73a2007-09-23 13:14:11 +09002039 unsigned int err_mask;
2040
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04002041 /* issue SET feature command to turn this on */
Tejun Heo854c73a2007-09-23 13:14:11 +09002042 err_mask = ata_dev_set_AN(dev, SETFEATURES_SATA_ENABLE);
2043 if (err_mask)
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04002044 ata_dev_printk(dev, KERN_ERR,
Tejun Heo854c73a2007-09-23 13:14:11 +09002045 "failed to enable ATAPI AN "
2046 "(err_mask=0x%x)\n", err_mask);
2047 else {
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04002048 dev->flags |= ATA_DFLAG_AN;
Tejun Heo854c73a2007-09-23 13:14:11 +09002049 atapi_an_string = ", ATAPI AN";
2050 }
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04002051 }
2052
Albert Lee08a556d2006-03-31 13:29:04 +08002053 if (ata_id_cdb_intr(dev->id)) {
Albert Lee312f7da2005-09-27 17:38:03 +08002054 dev->flags |= ATA_DFLAG_CDB_INTR;
Albert Lee08a556d2006-03-31 13:29:04 +08002055 cdb_intr_string = ", CDB intr";
2056 }
Albert Lee312f7da2005-09-27 17:38:03 +08002057
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 /* print device info to dmesg */
Borislav Petkov5afc8142006-06-27 14:51:25 +02002059 if (ata_msg_drv(ap) && print_info)
Albert Leeef143d52007-06-05 13:01:33 +08002060 ata_dev_printk(dev, KERN_INFO,
Tejun Heo854c73a2007-09-23 13:14:11 +09002061 "ATAPI: %s, %s, max %s%s%s\n",
Albert Leeef143d52007-06-05 13:01:33 +08002062 modelbuf, fwrevbuf,
Tejun Heo12436c32006-05-15 20:59:15 +09002063 ata_mode_string(xfer_mask),
Tejun Heo854c73a2007-09-23 13:14:11 +09002064 cdb_intr_string, atapi_an_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 }
2066
Tejun Heo914ed352006-11-01 18:39:55 +09002067 /* determine max_sectors */
2068 dev->max_sectors = ATA_MAX_SECTORS;
2069 if (dev->flags & ATA_DFLAG_LBA48)
2070 dev->max_sectors = ATA_MAX_SECTORS_LBA48;
2071
Alan Cox93590852006-09-12 16:55:12 +01002072 if (dev->horkage & ATA_HORKAGE_DIAGNOSTIC) {
2073 /* Let the user know. We don't want to disallow opens for
2074 rescue purposes, or in case the vendor is just a blithering
2075 idiot */
2076 if (print_info) {
2077 ata_dev_printk(dev, KERN_WARNING,
2078"Drive reports diagnostics failure. This may indicate a drive\n");
2079 ata_dev_printk(dev, KERN_WARNING,
2080"fault or invalid emulation. Contact drive vendor for information.\n");
2081 }
2082 }
2083
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09002084 /* limit bridge transfers to udma5, 200 sectors */
Tejun Heo3373efd2006-05-15 20:57:53 +09002085 if (ata_dev_knobble(dev)) {
Borislav Petkov5afc8142006-06-27 14:51:25 +02002086 if (ata_msg_drv(ap) && print_info)
Tejun Heof15a1da2006-05-15 20:57:56 +09002087 ata_dev_printk(dev, KERN_INFO,
2088 "applying bridge limits\n");
Tejun Heo5a529132006-03-24 14:07:50 +09002089 dev->udma_mask &= ATA_UDMA5;
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09002090 dev->max_sectors = ATA_MAX_SECTORS;
2091 }
2092
Tejun Heo75683fe2007-07-05 13:31:27 +09002093 if (dev->horkage & ATA_HORKAGE_MAX_SEC_128)
Tejun Heo03ec52d2007-04-12 13:38:11 +09002094 dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_128,
2095 dev->max_sectors);
Albert Lee18d6e9d2007-04-02 11:34:15 +08002096
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09002097 if (ap->ops->dev_config)
Alancd0d3bb2007-03-02 00:56:15 +00002098 ap->ops->dev_config(dev);
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09002099
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002100 if (ata_msg_probe(ap))
2101 ata_dev_printk(dev, KERN_DEBUG, "%s: EXIT, drv_stat = 0x%x\n",
2102 __FUNCTION__, ata_chk_status(ap));
Tejun Heoffeae412006-03-01 16:09:35 +09002103 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
2105err_out_nosup:
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002106 if (ata_msg_probe(ap))
Tejun Heo88574552006-06-25 20:00:35 +09002107 ata_dev_printk(dev, KERN_DEBUG,
2108 "%s: EXIT, err\n", __FUNCTION__);
Tejun Heoffeae412006-03-01 16:09:35 +09002109 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110}
2111
2112/**
Alan Cox2e41e8e2007-03-08 23:19:19 +00002113 * ata_cable_40wire - return 40 wire cable type
Alan Coxbe0d18d2007-03-06 02:37:56 -08002114 * @ap: port
2115 *
Alan Cox2e41e8e2007-03-08 23:19:19 +00002116 * Helper method for drivers which want to hardwire 40 wire cable
Alan Coxbe0d18d2007-03-06 02:37:56 -08002117 * detection.
2118 */
2119
2120int ata_cable_40wire(struct ata_port *ap)
2121{
2122 return ATA_CBL_PATA40;
2123}
2124
2125/**
Alan Cox2e41e8e2007-03-08 23:19:19 +00002126 * ata_cable_80wire - return 80 wire cable type
Alan Coxbe0d18d2007-03-06 02:37:56 -08002127 * @ap: port
2128 *
Alan Cox2e41e8e2007-03-08 23:19:19 +00002129 * Helper method for drivers which want to hardwire 80 wire cable
Alan Coxbe0d18d2007-03-06 02:37:56 -08002130 * detection.
2131 */
2132
2133int ata_cable_80wire(struct ata_port *ap)
2134{
2135 return ATA_CBL_PATA80;
2136}
2137
2138/**
2139 * ata_cable_unknown - return unknown PATA cable.
2140 * @ap: port
2141 *
2142 * Helper method for drivers which have no PATA cable detection.
2143 */
2144
2145int ata_cable_unknown(struct ata_port *ap)
2146{
2147 return ATA_CBL_PATA_UNK;
2148}
2149
2150/**
2151 * ata_cable_sata - return SATA cable type
2152 * @ap: port
2153 *
2154 * Helper method for drivers which have SATA cables
2155 */
2156
2157int ata_cable_sata(struct ata_port *ap)
2158{
2159 return ATA_CBL_SATA;
2160}
2161
2162/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 * ata_bus_probe - Reset and probe ATA bus
2164 * @ap: Bus to probe
2165 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04002166 * Master ATA bus probing function. Initiates a hardware-dependent
2167 * bus reset, then attempts to identify any devices found on
2168 * the bus.
2169 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002171 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 *
2173 * RETURNS:
Tejun Heo96072e62006-04-01 01:38:17 +09002174 * Zero on success, negative errno otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 */
2176
Brian King80289162006-08-07 14:27:31 -05002177int ata_bus_probe(struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178{
Tejun Heo28ca5c52006-03-01 16:09:36 +09002179 unsigned int classes[ATA_MAX_DEVICES];
Tejun Heo14d2bac2006-04-02 17:54:46 +09002180 int tries[ATA_MAX_DEVICES];
Tejun Heof58229f2007-08-06 18:36:23 +09002181 int rc;
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002182 struct ata_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
Tejun Heo28ca5c52006-03-01 16:09:36 +09002184 ata_port_probe(ap);
2185
Tejun Heof58229f2007-08-06 18:36:23 +09002186 ata_link_for_each_dev(dev, &ap->link)
2187 tries[dev->devno] = ATA_PROBE_MAX_TRIES;
Tejun Heo14d2bac2006-04-02 17:54:46 +09002188
2189 retry:
Tejun Heo20444702006-03-13 01:57:01 +09002190 /* reset and determine device classes */
Tejun Heo52783c52006-05-31 18:28:22 +09002191 ap->ops->phy_reset(ap);
Tejun Heo2061a472006-03-12 00:57:39 +09002192
Tejun Heof58229f2007-08-06 18:36:23 +09002193 ata_link_for_each_dev(dev, &ap->link) {
Tejun Heo52783c52006-05-31 18:28:22 +09002194 if (!(ap->flags & ATA_FLAG_DISABLED) &&
2195 dev->class != ATA_DEV_UNKNOWN)
2196 classes[dev->devno] = dev->class;
2197 else
2198 classes[dev->devno] = ATA_DEV_NONE;
Tejun Heo20444702006-03-13 01:57:01 +09002199
Tejun Heo52783c52006-05-31 18:28:22 +09002200 dev->class = ATA_DEV_UNKNOWN;
Tejun Heo28ca5c52006-03-01 16:09:36 +09002201 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202
Tejun Heo52783c52006-05-31 18:28:22 +09002203 ata_port_probe(ap);
Tejun Heo20444702006-03-13 01:57:01 +09002204
Alan Coxb6079ca2006-05-22 16:52:06 +01002205 /* after the reset the device state is PIO 0 and the controller
2206 state is undefined. Record the mode */
2207
Tejun Heof58229f2007-08-06 18:36:23 +09002208 ata_link_for_each_dev(dev, &ap->link)
2209 dev->pio_mode = XFER_PIO_0;
Alan Coxb6079ca2006-05-22 16:52:06 +01002210
Jeff Garzikf31f0cc2007-03-02 17:47:28 -05002211 /* read IDENTIFY page and configure devices. We have to do the identify
2212 specific sequence bass-ackwards so that PDIAG- is released by
2213 the slave device */
2214
Tejun Heof58229f2007-08-06 18:36:23 +09002215 ata_link_for_each_dev(dev, &ap->link) {
2216 if (tries[dev->devno])
2217 dev->class = classes[dev->devno];
Tejun Heo14d2bac2006-04-02 17:54:46 +09002218
Tejun Heoe1211e32006-04-01 01:38:18 +09002219 if (!ata_dev_enabled(dev))
Tejun Heoffeae412006-03-01 16:09:35 +09002220 continue;
2221
Tejun Heobff04642006-11-10 18:08:10 +09002222 rc = ata_dev_read_id(dev, &dev->class, ATA_READID_POSTRESET,
2223 dev->id);
Tejun Heo14d2bac2006-04-02 17:54:46 +09002224 if (rc)
2225 goto fail;
Jeff Garzikf31f0cc2007-03-02 17:47:28 -05002226 }
2227
Alan Coxbe0d18d2007-03-06 02:37:56 -08002228 /* Now ask for the cable type as PDIAG- should have been released */
2229 if (ap->ops->cable_detect)
2230 ap->cbl = ap->ops->cable_detect(ap);
2231
Alan Cox614fe292007-08-22 23:22:45 +01002232 /* We may have SATA bridge glue hiding here irrespective of the
2233 reported cable types and sensed types */
2234 ata_link_for_each_dev(dev, &ap->link) {
2235 if (!ata_dev_enabled(dev))
2236 continue;
2237 /* SATA drives indicate we have a bridge. We don't know which
2238 end of the link the bridge is which is a problem */
2239 if (ata_id_is_sata(dev->id))
2240 ap->cbl = ATA_CBL_SATA;
2241 }
2242
Jeff Garzikf31f0cc2007-03-02 17:47:28 -05002243 /* After the identify sequence we can now set up the devices. We do
2244 this in the normal order so that the user doesn't get confused */
2245
Tejun Heof58229f2007-08-06 18:36:23 +09002246 ata_link_for_each_dev(dev, &ap->link) {
Jeff Garzikf31f0cc2007-03-02 17:47:28 -05002247 if (!ata_dev_enabled(dev))
2248 continue;
Tejun Heoffeae412006-03-01 16:09:35 +09002249
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002250 ap->link.eh_context.i.flags |= ATA_EHI_PRINTINFO;
Tejun Heoefdaedc2006-11-01 18:38:52 +09002251 rc = ata_dev_configure(dev);
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002252 ap->link.eh_context.i.flags &= ~ATA_EHI_PRINTINFO;
Tejun Heo14d2bac2006-04-02 17:54:46 +09002253 if (rc)
2254 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 }
2256
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002257 /* configure transfer mode */
Tejun Heo02607312007-08-06 18:36:23 +09002258 rc = ata_set_mode(&ap->link, &dev);
Tejun Heo4ae72a12007-02-02 16:22:30 +09002259 if (rc)
Tejun Heo51713d32006-04-11 22:26:29 +09002260 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261
Tejun Heof58229f2007-08-06 18:36:23 +09002262 ata_link_for_each_dev(dev, &ap->link)
2263 if (ata_dev_enabled(dev))
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002264 return 0;
Alan Coxe35a9e02006-03-27 18:46:37 +01002265
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002266 /* no device present, disable port */
2267 ata_port_disable(ap);
Tejun Heo96072e62006-04-01 01:38:17 +09002268 return -ENODEV;
Tejun Heo14d2bac2006-04-02 17:54:46 +09002269
2270 fail:
Tejun Heo4ae72a12007-02-02 16:22:30 +09002271 tries[dev->devno]--;
2272
Tejun Heo14d2bac2006-04-02 17:54:46 +09002273 switch (rc) {
2274 case -EINVAL:
Tejun Heo4ae72a12007-02-02 16:22:30 +09002275 /* eeek, something went very wrong, give up */
Tejun Heo14d2bac2006-04-02 17:54:46 +09002276 tries[dev->devno] = 0;
2277 break;
Tejun Heo4ae72a12007-02-02 16:22:30 +09002278
2279 case -ENODEV:
2280 /* give it just one more chance */
2281 tries[dev->devno] = min(tries[dev->devno], 1);
Tejun Heo14d2bac2006-04-02 17:54:46 +09002282 case -EIO:
Tejun Heo4ae72a12007-02-02 16:22:30 +09002283 if (tries[dev->devno] == 1) {
2284 /* This is the last chance, better to slow
2285 * down than lose it.
2286 */
Tejun Heo936fd732007-08-06 18:36:23 +09002287 sata_down_spd_limit(&ap->link);
Tejun Heo4ae72a12007-02-02 16:22:30 +09002288 ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
2289 }
Tejun Heo14d2bac2006-04-02 17:54:46 +09002290 }
2291
Tejun Heo4ae72a12007-02-02 16:22:30 +09002292 if (!tries[dev->devno])
Tejun Heo3373efd2006-05-15 20:57:53 +09002293 ata_dev_disable(dev);
Tejun Heoec573752006-04-11 22:26:29 +09002294
Tejun Heo14d2bac2006-04-02 17:54:46 +09002295 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296}
2297
2298/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002299 * ata_port_probe - Mark port as enabled
2300 * @ap: Port for which we indicate enablement
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04002302 * Modify @ap data structure such that the system
2303 * thinks that the entire port is enabled.
2304 *
Jeff Garzikcca39742006-08-24 03:19:22 -04002305 * LOCKING: host lock, or some other form of
Jeff Garzik0cba6322005-05-30 19:49:12 -04002306 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 */
2308
2309void ata_port_probe(struct ata_port *ap)
2310{
Tejun Heo198e0fe2006-04-02 18:51:52 +09002311 ap->flags &= ~ATA_FLAG_DISABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312}
2313
2314/**
Tejun Heo3be680b2005-12-19 22:35:02 +09002315 * sata_print_link_status - Print SATA link status
Tejun Heo936fd732007-08-06 18:36:23 +09002316 * @link: SATA link to printk link status about
Tejun Heo3be680b2005-12-19 22:35:02 +09002317 *
2318 * This function prints link speed and status of a SATA link.
2319 *
2320 * LOCKING:
2321 * None.
2322 */
Tejun Heo936fd732007-08-06 18:36:23 +09002323void sata_print_link_status(struct ata_link *link)
Tejun Heo3be680b2005-12-19 22:35:02 +09002324{
Tejun Heo6d5f9732006-04-03 00:09:41 +09002325 u32 sstatus, scontrol, tmp;
Tejun Heo3be680b2005-12-19 22:35:02 +09002326
Tejun Heo936fd732007-08-06 18:36:23 +09002327 if (sata_scr_read(link, SCR_STATUS, &sstatus))
Tejun Heo3be680b2005-12-19 22:35:02 +09002328 return;
Tejun Heo936fd732007-08-06 18:36:23 +09002329 sata_scr_read(link, SCR_CONTROL, &scontrol);
Tejun Heo3be680b2005-12-19 22:35:02 +09002330
Tejun Heo936fd732007-08-06 18:36:23 +09002331 if (ata_link_online(link)) {
Tejun Heo3be680b2005-12-19 22:35:02 +09002332 tmp = (sstatus >> 4) & 0xf;
Tejun Heo936fd732007-08-06 18:36:23 +09002333 ata_link_printk(link, KERN_INFO,
Tejun Heof15a1da2006-05-15 20:57:56 +09002334 "SATA link up %s (SStatus %X SControl %X)\n",
2335 sata_spd_string(tmp), sstatus, scontrol);
Tejun Heo3be680b2005-12-19 22:35:02 +09002336 } else {
Tejun Heo936fd732007-08-06 18:36:23 +09002337 ata_link_printk(link, KERN_INFO,
Tejun Heof15a1da2006-05-15 20:57:56 +09002338 "SATA link down (SStatus %X SControl %X)\n",
2339 sstatus, scontrol);
Tejun Heo3be680b2005-12-19 22:35:02 +09002340 }
2341}
2342
2343/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04002344 * __sata_phy_reset - Wake/reset a low-level SATA PHY
2345 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002347 * This function issues commands to standard SATA Sxxx
2348 * PHY registers, to wake up the phy (and device), and
2349 * clear any reset condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 *
2351 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002352 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 *
2354 */
2355void __sata_phy_reset(struct ata_port *ap)
2356{
Tejun Heo936fd732007-08-06 18:36:23 +09002357 struct ata_link *link = &ap->link;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 unsigned long timeout = jiffies + (HZ * 5);
Tejun Heo936fd732007-08-06 18:36:23 +09002359 u32 sstatus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360
2361 if (ap->flags & ATA_FLAG_SATA_RESET) {
Brett Russcdcca89e2005-03-28 15:10:27 -05002362 /* issue phy wake/reset */
Tejun Heo936fd732007-08-06 18:36:23 +09002363 sata_scr_write_flush(link, SCR_CONTROL, 0x301);
Tejun Heo62ba2842005-06-26 23:27:19 +09002364 /* Couldn't find anything in SATA I/II specs, but
2365 * AHCI-1.1 10.4.2 says at least 1 ms. */
2366 mdelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 }
Tejun Heo81952c52006-05-15 20:57:47 +09002368 /* phy wake/clear reset */
Tejun Heo936fd732007-08-06 18:36:23 +09002369 sata_scr_write_flush(link, SCR_CONTROL, 0x300);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370
2371 /* wait for phy to become ready, if necessary */
2372 do {
2373 msleep(200);
Tejun Heo936fd732007-08-06 18:36:23 +09002374 sata_scr_read(link, SCR_STATUS, &sstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002375 if ((sstatus & 0xf) != 1)
2376 break;
2377 } while (time_before(jiffies, timeout));
2378
Tejun Heo3be680b2005-12-19 22:35:02 +09002379 /* print link status */
Tejun Heo936fd732007-08-06 18:36:23 +09002380 sata_print_link_status(link);
Jeff Garzik656563e2005-11-20 03:36:45 -05002381
Tejun Heo3be680b2005-12-19 22:35:02 +09002382 /* TODO: phy layer with polling, timeouts, etc. */
Tejun Heo936fd732007-08-06 18:36:23 +09002383 if (!ata_link_offline(link))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 ata_port_probe(ap);
Tejun Heo3be680b2005-12-19 22:35:02 +09002385 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386 ata_port_disable(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387
Tejun Heo198e0fe2006-04-02 18:51:52 +09002388 if (ap->flags & ATA_FLAG_DISABLED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 return;
2390
2391 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
2392 ata_port_disable(ap);
2393 return;
2394 }
2395
2396 ap->cbl = ATA_CBL_SATA;
2397}
2398
2399/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04002400 * sata_phy_reset - Reset SATA bus.
2401 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002403 * This function resets the SATA bus, and then probes
2404 * the bus for devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 *
2406 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002407 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 *
2409 */
2410void sata_phy_reset(struct ata_port *ap)
2411{
2412 __sata_phy_reset(ap);
Tejun Heo198e0fe2006-04-02 18:51:52 +09002413 if (ap->flags & ATA_FLAG_DISABLED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 return;
2415 ata_bus_reset(ap);
2416}
2417
2418/**
Alan Coxebdfca6e2006-03-23 15:38:34 +00002419 * ata_dev_pair - return other device on cable
Alan Coxebdfca6e2006-03-23 15:38:34 +00002420 * @adev: device
2421 *
2422 * Obtain the other device on the same cable, or if none is
2423 * present NULL is returned
2424 */
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002425
Tejun Heo3373efd2006-05-15 20:57:53 +09002426struct ata_device *ata_dev_pair(struct ata_device *adev)
Alan Coxebdfca6e2006-03-23 15:38:34 +00002427{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002428 struct ata_link *link = adev->link;
2429 struct ata_device *pair = &link->device[1 - adev->devno];
Tejun Heoe1211e32006-04-01 01:38:18 +09002430 if (!ata_dev_enabled(pair))
Alan Coxebdfca6e2006-03-23 15:38:34 +00002431 return NULL;
2432 return pair;
2433}
2434
2435/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04002436 * ata_port_disable - Disable port.
2437 * @ap: Port to be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002439 * Modify @ap data structure such that the system
2440 * thinks that the entire port is disabled, and should
2441 * never attempt to probe or communicate with devices
2442 * on this port.
2443 *
Jeff Garzikcca39742006-08-24 03:19:22 -04002444 * LOCKING: host lock, or some other form of
Jeff Garzik780a87f2005-05-30 15:41:05 -04002445 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 */
2447
2448void ata_port_disable(struct ata_port *ap)
2449{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002450 ap->link.device[0].class = ATA_DEV_NONE;
2451 ap->link.device[1].class = ATA_DEV_NONE;
Tejun Heo198e0fe2006-04-02 18:51:52 +09002452 ap->flags |= ATA_FLAG_DISABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453}
2454
Tejun Heo1c3fae42006-04-02 20:53:28 +09002455/**
Tejun Heo3c567b72006-05-15 20:57:23 +09002456 * sata_down_spd_limit - adjust SATA spd limit downward
Tejun Heo936fd732007-08-06 18:36:23 +09002457 * @link: Link to adjust SATA spd limit for
Tejun Heo1c3fae42006-04-02 20:53:28 +09002458 *
Tejun Heo936fd732007-08-06 18:36:23 +09002459 * Adjust SATA spd limit of @link downward. Note that this
Tejun Heo1c3fae42006-04-02 20:53:28 +09002460 * function only adjusts the limit. The change must be applied
Tejun Heo3c567b72006-05-15 20:57:23 +09002461 * using sata_set_spd().
Tejun Heo1c3fae42006-04-02 20:53:28 +09002462 *
2463 * LOCKING:
2464 * Inherited from caller.
2465 *
2466 * RETURNS:
2467 * 0 on success, negative errno on failure
2468 */
Tejun Heo936fd732007-08-06 18:36:23 +09002469int sata_down_spd_limit(struct ata_link *link)
Tejun Heo1c3fae42006-04-02 20:53:28 +09002470{
Tejun Heo81952c52006-05-15 20:57:47 +09002471 u32 sstatus, spd, mask;
2472 int rc, highbit;
Tejun Heo1c3fae42006-04-02 20:53:28 +09002473
Tejun Heo936fd732007-08-06 18:36:23 +09002474 if (!sata_scr_valid(link))
Tejun Heo008a7892007-07-16 14:29:40 +09002475 return -EOPNOTSUPP;
2476
2477 /* If SCR can be read, use it to determine the current SPD.
Tejun Heo936fd732007-08-06 18:36:23 +09002478 * If not, use cached value in link->sata_spd.
Tejun Heo008a7892007-07-16 14:29:40 +09002479 */
Tejun Heo936fd732007-08-06 18:36:23 +09002480 rc = sata_scr_read(link, SCR_STATUS, &sstatus);
Tejun Heo008a7892007-07-16 14:29:40 +09002481 if (rc == 0)
2482 spd = (sstatus >> 4) & 0xf;
2483 else
Tejun Heo936fd732007-08-06 18:36:23 +09002484 spd = link->sata_spd;
Tejun Heo1c3fae42006-04-02 20:53:28 +09002485
Tejun Heo936fd732007-08-06 18:36:23 +09002486 mask = link->sata_spd_limit;
Tejun Heo1c3fae42006-04-02 20:53:28 +09002487 if (mask <= 1)
2488 return -EINVAL;
Tejun Heo008a7892007-07-16 14:29:40 +09002489
2490 /* unconditionally mask off the highest bit */
Tejun Heo1c3fae42006-04-02 20:53:28 +09002491 highbit = fls(mask) - 1;
2492 mask &= ~(1 << highbit);
2493
Tejun Heo008a7892007-07-16 14:29:40 +09002494 /* Mask off all speeds higher than or equal to the current
2495 * one. Force 1.5Gbps if current SPD is not available.
2496 */
2497 if (spd > 1)
2498 mask &= (1 << (spd - 1)) - 1;
2499 else
2500 mask &= 1;
2501
2502 /* were we already at the bottom? */
Tejun Heo1c3fae42006-04-02 20:53:28 +09002503 if (!mask)
2504 return -EINVAL;
2505
Tejun Heo936fd732007-08-06 18:36:23 +09002506 link->sata_spd_limit = mask;
Tejun Heo1c3fae42006-04-02 20:53:28 +09002507
Tejun Heo936fd732007-08-06 18:36:23 +09002508 ata_link_printk(link, KERN_WARNING, "limiting SATA link speed to %s\n",
Tejun Heof15a1da2006-05-15 20:57:56 +09002509 sata_spd_string(fls(mask)));
Tejun Heo1c3fae42006-04-02 20:53:28 +09002510
2511 return 0;
2512}
2513
Tejun Heo936fd732007-08-06 18:36:23 +09002514static int __sata_set_spd_needed(struct ata_link *link, u32 *scontrol)
Tejun Heo1c3fae42006-04-02 20:53:28 +09002515{
2516 u32 spd, limit;
2517
Tejun Heo936fd732007-08-06 18:36:23 +09002518 if (link->sata_spd_limit == UINT_MAX)
Tejun Heo1c3fae42006-04-02 20:53:28 +09002519 limit = 0;
2520 else
Tejun Heo936fd732007-08-06 18:36:23 +09002521 limit = fls(link->sata_spd_limit);
Tejun Heo1c3fae42006-04-02 20:53:28 +09002522
2523 spd = (*scontrol >> 4) & 0xf;
2524 *scontrol = (*scontrol & ~0xf0) | ((limit & 0xf) << 4);
2525
2526 return spd != limit;
2527}
2528
2529/**
Tejun Heo3c567b72006-05-15 20:57:23 +09002530 * sata_set_spd_needed - is SATA spd configuration needed
Tejun Heo936fd732007-08-06 18:36:23 +09002531 * @link: Link in question
Tejun Heo1c3fae42006-04-02 20:53:28 +09002532 *
2533 * Test whether the spd limit in SControl matches
Tejun Heo936fd732007-08-06 18:36:23 +09002534 * @link->sata_spd_limit. This function is used to determine
Tejun Heo1c3fae42006-04-02 20:53:28 +09002535 * whether hardreset is necessary to apply SATA spd
2536 * configuration.
2537 *
2538 * LOCKING:
2539 * Inherited from caller.
2540 *
2541 * RETURNS:
2542 * 1 if SATA spd configuration is needed, 0 otherwise.
2543 */
Tejun Heo936fd732007-08-06 18:36:23 +09002544int sata_set_spd_needed(struct ata_link *link)
Tejun Heo1c3fae42006-04-02 20:53:28 +09002545{
2546 u32 scontrol;
2547
Tejun Heo936fd732007-08-06 18:36:23 +09002548 if (sata_scr_read(link, SCR_CONTROL, &scontrol))
Tejun Heo1c3fae42006-04-02 20:53:28 +09002549 return 0;
2550
Tejun Heo936fd732007-08-06 18:36:23 +09002551 return __sata_set_spd_needed(link, &scontrol);
Tejun Heo1c3fae42006-04-02 20:53:28 +09002552}
2553
2554/**
Tejun Heo3c567b72006-05-15 20:57:23 +09002555 * sata_set_spd - set SATA spd according to spd limit
Tejun Heo936fd732007-08-06 18:36:23 +09002556 * @link: Link to set SATA spd for
Tejun Heo1c3fae42006-04-02 20:53:28 +09002557 *
Tejun Heo936fd732007-08-06 18:36:23 +09002558 * Set SATA spd of @link according to sata_spd_limit.
Tejun Heo1c3fae42006-04-02 20:53:28 +09002559 *
2560 * LOCKING:
2561 * Inherited from caller.
2562 *
2563 * RETURNS:
2564 * 0 if spd doesn't need to be changed, 1 if spd has been
Tejun Heo81952c52006-05-15 20:57:47 +09002565 * changed. Negative errno if SCR registers are inaccessible.
Tejun Heo1c3fae42006-04-02 20:53:28 +09002566 */
Tejun Heo936fd732007-08-06 18:36:23 +09002567int sata_set_spd(struct ata_link *link)
Tejun Heo1c3fae42006-04-02 20:53:28 +09002568{
2569 u32 scontrol;
Tejun Heo81952c52006-05-15 20:57:47 +09002570 int rc;
Tejun Heo1c3fae42006-04-02 20:53:28 +09002571
Tejun Heo936fd732007-08-06 18:36:23 +09002572 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
Tejun Heo81952c52006-05-15 20:57:47 +09002573 return rc;
Tejun Heo1c3fae42006-04-02 20:53:28 +09002574
Tejun Heo936fd732007-08-06 18:36:23 +09002575 if (!__sata_set_spd_needed(link, &scontrol))
Tejun Heo1c3fae42006-04-02 20:53:28 +09002576 return 0;
2577
Tejun Heo936fd732007-08-06 18:36:23 +09002578 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
Tejun Heo81952c52006-05-15 20:57:47 +09002579 return rc;
2580
Tejun Heo1c3fae42006-04-02 20:53:28 +09002581 return 1;
2582}
2583
Alan Cox452503f2005-10-21 19:01:32 -04002584/*
2585 * This mode timing computation functionality is ported over from
2586 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
2587 */
2588/*
Alan Coxb352e572006-08-10 18:52:12 +01002589 * PIO 0-4, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
Alan Cox452503f2005-10-21 19:01:32 -04002590 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
Alan Coxb352e572006-08-10 18:52:12 +01002591 * for UDMA6, which is currently supported only by Maxtor drives.
2592 *
2593 * For PIO 5/6 MWDMA 3/4 see the CFA specification 3.0.
Alan Cox452503f2005-10-21 19:01:32 -04002594 */
2595
2596static const struct ata_timing ata_timing[] = {
2597
2598 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
2599 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
2600 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
2601 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
2602
Alan Coxb352e572006-08-10 18:52:12 +01002603 { XFER_MW_DMA_4, 25, 0, 0, 0, 55, 20, 80, 0 },
2604 { XFER_MW_DMA_3, 25, 0, 0, 0, 65, 25, 100, 0 },
Alan Cox452503f2005-10-21 19:01:32 -04002605 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
2606 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
2607 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
2608
2609/* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002610
Alan Cox452503f2005-10-21 19:01:32 -04002611 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
2612 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
2613 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002614
Alan Cox452503f2005-10-21 19:01:32 -04002615 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
2616 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
2617 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
2618
Alan Coxb352e572006-08-10 18:52:12 +01002619 { XFER_PIO_6, 10, 55, 20, 80, 55, 20, 80, 0 },
2620 { XFER_PIO_5, 15, 65, 25, 100, 65, 25, 100, 0 },
Alan Cox452503f2005-10-21 19:01:32 -04002621 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
2622 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
2623
2624 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
2625 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
2626 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
2627
2628/* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
2629
2630 { 0xFF }
2631};
2632
2633#define ENOUGH(v,unit) (((v)-1)/(unit)+1)
2634#define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
2635
2636static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
2637{
2638 q->setup = EZ(t->setup * 1000, T);
2639 q->act8b = EZ(t->act8b * 1000, T);
2640 q->rec8b = EZ(t->rec8b * 1000, T);
2641 q->cyc8b = EZ(t->cyc8b * 1000, T);
2642 q->active = EZ(t->active * 1000, T);
2643 q->recover = EZ(t->recover * 1000, T);
2644 q->cycle = EZ(t->cycle * 1000, T);
2645 q->udma = EZ(t->udma * 1000, UT);
2646}
2647
2648void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
2649 struct ata_timing *m, unsigned int what)
2650{
2651 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
2652 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
2653 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
2654 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
2655 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
2656 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
2657 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
2658 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
2659}
2660
2661static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
2662{
2663 const struct ata_timing *t;
2664
2665 for (t = ata_timing; t->mode != speed; t++)
Alan Cox91190752005-10-26 12:17:46 -04002666 if (t->mode == 0xFF)
Alan Cox452503f2005-10-21 19:01:32 -04002667 return NULL;
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002668 return t;
Alan Cox452503f2005-10-21 19:01:32 -04002669}
2670
2671int ata_timing_compute(struct ata_device *adev, unsigned short speed,
2672 struct ata_timing *t, int T, int UT)
2673{
2674 const struct ata_timing *s;
2675 struct ata_timing p;
2676
2677 /*
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002678 * Find the mode.
Albert Lee75b1f2f2005-11-16 17:06:18 +08002679 */
Alan Cox452503f2005-10-21 19:01:32 -04002680
2681 if (!(s = ata_timing_find_mode(speed)))
2682 return -EINVAL;
2683
Albert Lee75b1f2f2005-11-16 17:06:18 +08002684 memcpy(t, s, sizeof(*s));
2685
Alan Cox452503f2005-10-21 19:01:32 -04002686 /*
2687 * If the drive is an EIDE drive, it can tell us it needs extended
2688 * PIO/MW_DMA cycle timing.
2689 */
2690
2691 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
2692 memset(&p, 0, sizeof(p));
2693 if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
2694 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
2695 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
2696 } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
2697 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
2698 }
2699 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
2700 }
2701
2702 /*
2703 * Convert the timing to bus clock counts.
2704 */
2705
Albert Lee75b1f2f2005-11-16 17:06:18 +08002706 ata_timing_quantize(t, t, T, UT);
Alan Cox452503f2005-10-21 19:01:32 -04002707
2708 /*
Randy Dunlapc893a3a2006-01-28 13:15:32 -05002709 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
2710 * S.M.A.R.T * and some other commands. We have to ensure that the
2711 * DMA cycle timing is slower/equal than the fastest PIO timing.
Alan Cox452503f2005-10-21 19:01:32 -04002712 */
2713
Alanfd3367af2006-12-07 12:41:18 +00002714 if (speed > XFER_PIO_6) {
Alan Cox452503f2005-10-21 19:01:32 -04002715 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
2716 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
2717 }
2718
2719 /*
Randy Dunlapc893a3a2006-01-28 13:15:32 -05002720 * Lengthen active & recovery time so that cycle time is correct.
Alan Cox452503f2005-10-21 19:01:32 -04002721 */
2722
2723 if (t->act8b + t->rec8b < t->cyc8b) {
2724 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
2725 t->rec8b = t->cyc8b - t->act8b;
2726 }
2727
2728 if (t->active + t->recover < t->cycle) {
2729 t->active += (t->cycle - (t->active + t->recover)) / 2;
2730 t->recover = t->cycle - t->active;
2731 }
Jeff Garzika617c092007-05-21 20:14:23 -04002732
Alan Cox4f701d12007-04-23 11:55:36 +01002733 /* In a few cases quantisation may produce enough errors to
2734 leave t->cycle too low for the sum of active and recovery
2735 if so we must correct this */
2736 if (t->active + t->recover > t->cycle)
2737 t->cycle = t->active + t->recover;
Alan Cox452503f2005-10-21 19:01:32 -04002738
2739 return 0;
2740}
2741
Tejun Heocf176e12006-04-02 17:54:46 +09002742/**
2743 * ata_down_xfermask_limit - adjust dev xfer masks downward
Tejun Heocf176e12006-04-02 17:54:46 +09002744 * @dev: Device to adjust xfer masks
Tejun Heo458337d2007-02-02 16:22:30 +09002745 * @sel: ATA_DNXFER_* selector
Tejun Heocf176e12006-04-02 17:54:46 +09002746 *
2747 * Adjust xfer masks of @dev downward. Note that this function
2748 * does not apply the change. Invoking ata_set_mode() afterwards
2749 * will apply the limit.
2750 *
2751 * LOCKING:
2752 * Inherited from caller.
2753 *
2754 * RETURNS:
2755 * 0 on success, negative errno on failure
2756 */
Tejun Heo458337d2007-02-02 16:22:30 +09002757int ata_down_xfermask_limit(struct ata_device *dev, unsigned int sel)
Tejun Heocf176e12006-04-02 17:54:46 +09002758{
Tejun Heo458337d2007-02-02 16:22:30 +09002759 char buf[32];
2760 unsigned int orig_mask, xfer_mask;
2761 unsigned int pio_mask, mwdma_mask, udma_mask;
2762 int quiet, highbit;
Tejun Heocf176e12006-04-02 17:54:46 +09002763
Tejun Heo458337d2007-02-02 16:22:30 +09002764 quiet = !!(sel & ATA_DNXFER_QUIET);
2765 sel &= ~ATA_DNXFER_QUIET;
Tejun Heocf176e12006-04-02 17:54:46 +09002766
Tejun Heo458337d2007-02-02 16:22:30 +09002767 xfer_mask = orig_mask = ata_pack_xfermask(dev->pio_mask,
2768 dev->mwdma_mask,
2769 dev->udma_mask);
2770 ata_unpack_xfermask(xfer_mask, &pio_mask, &mwdma_mask, &udma_mask);
Tejun Heocf176e12006-04-02 17:54:46 +09002771
Tejun Heo458337d2007-02-02 16:22:30 +09002772 switch (sel) {
2773 case ATA_DNXFER_PIO:
2774 highbit = fls(pio_mask) - 1;
2775 pio_mask &= ~(1 << highbit);
2776 break;
2777
2778 case ATA_DNXFER_DMA:
2779 if (udma_mask) {
2780 highbit = fls(udma_mask) - 1;
2781 udma_mask &= ~(1 << highbit);
2782 if (!udma_mask)
2783 return -ENOENT;
2784 } else if (mwdma_mask) {
2785 highbit = fls(mwdma_mask) - 1;
2786 mwdma_mask &= ~(1 << highbit);
2787 if (!mwdma_mask)
2788 return -ENOENT;
2789 }
2790 break;
2791
2792 case ATA_DNXFER_40C:
2793 udma_mask &= ATA_UDMA_MASK_40C;
2794 break;
2795
2796 case ATA_DNXFER_FORCE_PIO0:
2797 pio_mask &= 1;
2798 case ATA_DNXFER_FORCE_PIO:
2799 mwdma_mask = 0;
2800 udma_mask = 0;
2801 break;
2802
Tejun Heo458337d2007-02-02 16:22:30 +09002803 default:
2804 BUG();
2805 }
2806
2807 xfer_mask &= ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
2808
2809 if (!(xfer_mask & ATA_MASK_PIO) || xfer_mask == orig_mask)
2810 return -ENOENT;
2811
2812 if (!quiet) {
2813 if (xfer_mask & (ATA_MASK_MWDMA | ATA_MASK_UDMA))
2814 snprintf(buf, sizeof(buf), "%s:%s",
2815 ata_mode_string(xfer_mask),
2816 ata_mode_string(xfer_mask & ATA_MASK_PIO));
2817 else
2818 snprintf(buf, sizeof(buf), "%s",
2819 ata_mode_string(xfer_mask));
2820
2821 ata_dev_printk(dev, KERN_WARNING,
2822 "limiting speed to %s\n", buf);
2823 }
Tejun Heocf176e12006-04-02 17:54:46 +09002824
2825 ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask,
2826 &dev->udma_mask);
2827
Tejun Heocf176e12006-04-02 17:54:46 +09002828 return 0;
Tejun Heocf176e12006-04-02 17:54:46 +09002829}
2830
Tejun Heo3373efd2006-05-15 20:57:53 +09002831static int ata_dev_set_mode(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002833 struct ata_eh_context *ehc = &dev->link->eh_context;
Tejun Heo83206a22006-03-24 15:25:31 +09002834 unsigned int err_mask;
2835 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836
Tejun Heoe8384602006-04-02 18:51:53 +09002837 dev->flags &= ~ATA_DFLAG_PIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838 if (dev->xfer_shift == ATA_SHIFT_PIO)
2839 dev->flags |= ATA_DFLAG_PIO;
2840
Tejun Heo3373efd2006-05-15 20:57:53 +09002841 err_mask = ata_dev_set_xfermode(dev);
Alan11750a42007-02-05 16:28:30 +00002842 /* Old CFA may refuse this command, which is just fine */
2843 if (dev->xfer_shift == ATA_SHIFT_PIO && ata_id_is_cfa(dev->id))
2844 err_mask &= ~AC_ERR_DEV;
Alan Cox0bc2a792007-07-31 14:01:48 +01002845 /* Some very old devices and some bad newer ones fail any kind of
2846 SET_XFERMODE request but support PIO0-2 timings and no IORDY */
2847 if (dev->xfer_shift == ATA_SHIFT_PIO && !ata_id_has_iordy(dev->id) &&
2848 dev->pio_mode <= XFER_PIO_2)
2849 err_mask &= ~AC_ERR_DEV;
Tejun Heo83206a22006-03-24 15:25:31 +09002850 if (err_mask) {
Tejun Heof15a1da2006-05-15 20:57:56 +09002851 ata_dev_printk(dev, KERN_ERR, "failed to set xfermode "
2852 "(err_mask=0x%x)\n", err_mask);
Tejun Heo83206a22006-03-24 15:25:31 +09002853 return -EIO;
2854 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855
Tejun Heobaa1e782006-11-01 18:39:27 +09002856 ehc->i.flags |= ATA_EHI_POST_SETMODE;
Tejun Heo422c9da2007-09-23 13:14:12 +09002857 rc = ata_dev_revalidate(dev, ATA_DEV_UNKNOWN, 0);
Tejun Heobaa1e782006-11-01 18:39:27 +09002858 ehc->i.flags &= ~ATA_EHI_POST_SETMODE;
Tejun Heo5eb45c02006-04-02 18:51:52 +09002859 if (rc)
Tejun Heo83206a22006-03-24 15:25:31 +09002860 return rc;
Tejun Heo48a8a142006-03-05 17:55:58 +09002861
Tejun Heo23e71c32006-03-06 04:31:57 +09002862 DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n",
2863 dev->xfer_shift, (int)dev->xfer_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864
Tejun Heof15a1da2006-05-15 20:57:56 +09002865 ata_dev_printk(dev, KERN_INFO, "configured for %s\n",
2866 ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)));
Tejun Heo83206a22006-03-24 15:25:31 +09002867 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868}
2869
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870/**
Alan04351822007-03-06 02:37:52 -08002871 * ata_do_set_mode - Program timings and issue SET FEATURES - XFER
Tejun Heo02607312007-08-06 18:36:23 +09002872 * @link: link on which timings will be programmed
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002873 * @r_failed_dev: out paramter for failed device
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 *
Alan04351822007-03-06 02:37:52 -08002875 * Standard implementation of the function used to tune and set
2876 * ATA device disk transfer mode (PIO3, UDMA6, etc.). If
2877 * ata_dev_set_mode() fails, pointer to the failing device is
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002878 * returned in @r_failed_dev.
Jeff Garzik780a87f2005-05-30 15:41:05 -04002879 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002880 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002881 * PCI/etc. bus probe sem.
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002882 *
2883 * RETURNS:
2884 * 0 on success, negative errno otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 */
Alan04351822007-03-06 02:37:52 -08002886
Tejun Heo02607312007-08-06 18:36:23 +09002887int ata_do_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888{
Tejun Heo02607312007-08-06 18:36:23 +09002889 struct ata_port *ap = link->ap;
Tejun Heoe8e06192006-04-01 01:38:18 +09002890 struct ata_device *dev;
Tejun Heof58229f2007-08-06 18:36:23 +09002891 int rc = 0, used_dma = 0, found = 0;
Tejun Heo3adcebb2006-05-15 20:57:37 +09002892
Tejun Heoa6d5a512006-03-06 04:31:57 +09002893 /* step 1: calculate xfer_mask */
Tejun Heof58229f2007-08-06 18:36:23 +09002894 ata_link_for_each_dev(dev, link) {
Tejun Heoacf356b2006-03-24 14:07:50 +09002895 unsigned int pio_mask, dma_mask;
Tejun Heoa6d5a512006-03-06 04:31:57 +09002896
Tejun Heoe1211e32006-04-01 01:38:18 +09002897 if (!ata_dev_enabled(dev))
Tejun Heoa6d5a512006-03-06 04:31:57 +09002898 continue;
2899
Tejun Heo3373efd2006-05-15 20:57:53 +09002900 ata_dev_xfermask(dev);
Tejun Heoa6d5a512006-03-06 04:31:57 +09002901
Tejun Heoacf356b2006-03-24 14:07:50 +09002902 pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0);
2903 dma_mask = ata_pack_xfermask(0, dev->mwdma_mask, dev->udma_mask);
2904 dev->pio_mode = ata_xfer_mask2mode(pio_mask);
2905 dev->dma_mode = ata_xfer_mask2mode(dma_mask);
Alan Cox5444a6f2006-03-27 18:58:20 +01002906
Tejun Heo4f659772006-04-01 01:38:18 +09002907 found = 1;
Alan Cox5444a6f2006-03-27 18:58:20 +01002908 if (dev->dma_mode)
2909 used_dma = 1;
Tejun Heoa6d5a512006-03-06 04:31:57 +09002910 }
Tejun Heo4f659772006-04-01 01:38:18 +09002911 if (!found)
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002912 goto out;
Tejun Heoa6d5a512006-03-06 04:31:57 +09002913
2914 /* step 2: always set host PIO timings */
Tejun Heof58229f2007-08-06 18:36:23 +09002915 ata_link_for_each_dev(dev, link) {
Tejun Heoe8e06192006-04-01 01:38:18 +09002916 if (!ata_dev_enabled(dev))
2917 continue;
2918
2919 if (!dev->pio_mode) {
Tejun Heof15a1da2006-05-15 20:57:56 +09002920 ata_dev_printk(dev, KERN_WARNING, "no PIO support\n");
Tejun Heoe8e06192006-04-01 01:38:18 +09002921 rc = -EINVAL;
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002922 goto out;
Tejun Heoe8e06192006-04-01 01:38:18 +09002923 }
2924
2925 dev->xfer_mode = dev->pio_mode;
2926 dev->xfer_shift = ATA_SHIFT_PIO;
2927 if (ap->ops->set_piomode)
2928 ap->ops->set_piomode(ap, dev);
2929 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930
Tejun Heoa6d5a512006-03-06 04:31:57 +09002931 /* step 3: set host DMA timings */
Tejun Heof58229f2007-08-06 18:36:23 +09002932 ata_link_for_each_dev(dev, link) {
Tejun Heoe8e06192006-04-01 01:38:18 +09002933 if (!ata_dev_enabled(dev) || !dev->dma_mode)
2934 continue;
2935
2936 dev->xfer_mode = dev->dma_mode;
2937 dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
2938 if (ap->ops->set_dmamode)
2939 ap->ops->set_dmamode(ap, dev);
2940 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941
2942 /* step 4: update devices' xfer mode */
Tejun Heof58229f2007-08-06 18:36:23 +09002943 ata_link_for_each_dev(dev, link) {
Alan18d90de2007-01-24 11:42:38 +00002944 /* don't update suspended devices' xfer mode */
Tejun Heo9666f402007-05-04 21:27:47 +02002945 if (!ata_dev_enabled(dev))
Tejun Heo83206a22006-03-24 15:25:31 +09002946 continue;
2947
Tejun Heo3373efd2006-05-15 20:57:53 +09002948 rc = ata_dev_set_mode(dev);
Tejun Heo5bbc53f2006-04-01 01:38:17 +09002949 if (rc)
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002950 goto out;
Tejun Heo83206a22006-03-24 15:25:31 +09002951 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952
Tejun Heoe8e06192006-04-01 01:38:18 +09002953 /* Record simplex status. If we selected DMA then the other
2954 * host channels are not permitted to do so.
Alan Cox5444a6f2006-03-27 18:58:20 +01002955 */
Jeff Garzikcca39742006-08-24 03:19:22 -04002956 if (used_dma && (ap->host->flags & ATA_HOST_SIMPLEX))
Alan032af1c2007-03-01 17:36:46 +00002957 ap->host->simplex_claimed = ap;
Alan Cox5444a6f2006-03-27 18:58:20 +01002958
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002959 out:
2960 if (rc)
2961 *r_failed_dev = dev;
2962 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963}
2964
2965/**
Alan04351822007-03-06 02:37:52 -08002966 * ata_set_mode - Program timings and issue SET FEATURES - XFER
Tejun Heo02607312007-08-06 18:36:23 +09002967 * @link: link on which timings will be programmed
Alan04351822007-03-06 02:37:52 -08002968 * @r_failed_dev: out paramter for failed device
2969 *
2970 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.). If
2971 * ata_set_mode() fails, pointer to the failing device is
2972 * returned in @r_failed_dev.
2973 *
2974 * LOCKING:
2975 * PCI/etc. bus probe sem.
2976 *
2977 * RETURNS:
2978 * 0 on success, negative errno otherwise
2979 */
Tejun Heo02607312007-08-06 18:36:23 +09002980int ata_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
Alan04351822007-03-06 02:37:52 -08002981{
Tejun Heo02607312007-08-06 18:36:23 +09002982 struct ata_port *ap = link->ap;
2983
Alan04351822007-03-06 02:37:52 -08002984 /* has private set_mode? */
2985 if (ap->ops->set_mode)
Tejun Heo02607312007-08-06 18:36:23 +09002986 return ap->ops->set_mode(link, r_failed_dev);
2987 return ata_do_set_mode(link, r_failed_dev);
Alan04351822007-03-06 02:37:52 -08002988}
2989
2990/**
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002991 * ata_tf_to_host - issue ATA taskfile to host controller
2992 * @ap: port to which command is being issued
2993 * @tf: ATA taskfile register set
2994 *
2995 * Issues ATA taskfile register set to ATA host controller,
2996 * with proper synchronization with interrupt handler and
2997 * other threads.
2998 *
2999 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04003000 * spin_lock_irqsave(host lock)
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05003001 */
3002
3003static inline void ata_tf_to_host(struct ata_port *ap,
3004 const struct ata_taskfile *tf)
3005{
3006 ap->ops->tf_load(ap, tf);
3007 ap->ops->exec_command(ap, tf);
3008}
3009
3010/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 * ata_busy_sleep - sleep until BSY clears, or timeout
3012 * @ap: port containing status register to be polled
3013 * @tmout_pat: impatience timeout
3014 * @tmout: overall timeout
3015 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04003016 * Sleep until ATA Status register bit BSY clears,
3017 * or a timeout occurs.
3018 *
Tejun Heod1adc1b2006-10-09 18:32:15 +09003019 * LOCKING:
3020 * Kernel thread context (may sleep).
3021 *
3022 * RETURNS:
3023 * 0 on success, -errno otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 */
Tejun Heod1adc1b2006-10-09 18:32:15 +09003025int ata_busy_sleep(struct ata_port *ap,
3026 unsigned long tmout_pat, unsigned long tmout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027{
3028 unsigned long timer_start, timeout;
3029 u8 status;
3030
3031 status = ata_busy_wait(ap, ATA_BUSY, 300);
3032 timer_start = jiffies;
3033 timeout = timer_start + tmout_pat;
Tejun Heod1adc1b2006-10-09 18:32:15 +09003034 while (status != 0xff && (status & ATA_BUSY) &&
3035 time_before(jiffies, timeout)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 msleep(50);
3037 status = ata_busy_wait(ap, ATA_BUSY, 3);
3038 }
3039
Tejun Heod1adc1b2006-10-09 18:32:15 +09003040 if (status != 0xff && (status & ATA_BUSY))
Tejun Heof15a1da2006-05-15 20:57:56 +09003041 ata_port_printk(ap, KERN_WARNING,
Jeff Garzik35aa7a42006-09-28 06:50:24 -04003042 "port is slow to respond, please be patient "
3043 "(Status 0x%x)\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044
3045 timeout = timer_start + tmout;
Tejun Heod1adc1b2006-10-09 18:32:15 +09003046 while (status != 0xff && (status & ATA_BUSY) &&
3047 time_before(jiffies, timeout)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 msleep(50);
3049 status = ata_chk_status(ap);
3050 }
3051
Tejun Heod1adc1b2006-10-09 18:32:15 +09003052 if (status == 0xff)
3053 return -ENODEV;
3054
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 if (status & ATA_BUSY) {
Tejun Heof15a1da2006-05-15 20:57:56 +09003056 ata_port_printk(ap, KERN_ERR, "port failed to respond "
Jeff Garzik35aa7a42006-09-28 06:50:24 -04003057 "(%lu secs, Status 0x%x)\n",
3058 tmout / HZ, status);
Tejun Heod1adc1b2006-10-09 18:32:15 +09003059 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 }
3061
3062 return 0;
3063}
3064
Tejun Heod4b2bab2007-02-02 16:50:52 +09003065/**
3066 * ata_wait_ready - sleep until BSY clears, or timeout
3067 * @ap: port containing status register to be polled
3068 * @deadline: deadline jiffies for the operation
3069 *
3070 * Sleep until ATA Status register bit BSY clears, or timeout
3071 * occurs.
3072 *
3073 * LOCKING:
3074 * Kernel thread context (may sleep).
3075 *
3076 * RETURNS:
3077 * 0 on success, -errno otherwise.
3078 */
3079int ata_wait_ready(struct ata_port *ap, unsigned long deadline)
3080{
3081 unsigned long start = jiffies;
3082 int warned = 0;
3083
3084 while (1) {
3085 u8 status = ata_chk_status(ap);
3086 unsigned long now = jiffies;
3087
3088 if (!(status & ATA_BUSY))
3089 return 0;
Tejun Heo936fd732007-08-06 18:36:23 +09003090 if (!ata_link_online(&ap->link) && status == 0xff)
Tejun Heod4b2bab2007-02-02 16:50:52 +09003091 return -ENODEV;
3092 if (time_after(now, deadline))
3093 return -EBUSY;
3094
3095 if (!warned && time_after(now, start + 5 * HZ) &&
3096 (deadline - now > 3 * HZ)) {
3097 ata_port_printk(ap, KERN_WARNING,
3098 "port is slow to respond, please be patient "
3099 "(Status 0x%x)\n", status);
3100 warned = 1;
3101 }
3102
3103 msleep(50);
3104 }
3105}
3106
3107static int ata_bus_post_reset(struct ata_port *ap, unsigned int devmask,
3108 unsigned long deadline)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003109{
3110 struct ata_ioports *ioaddr = &ap->ioaddr;
3111 unsigned int dev0 = devmask & (1 << 0);
3112 unsigned int dev1 = devmask & (1 << 1);
Tejun Heo9b893912007-02-02 16:50:52 +09003113 int rc, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003114
3115 /* if device 0 was found in ata_devchk, wait for its
3116 * BSY bit to clear
3117 */
Tejun Heod4b2bab2007-02-02 16:50:52 +09003118 if (dev0) {
3119 rc = ata_wait_ready(ap, deadline);
Tejun Heo9b893912007-02-02 16:50:52 +09003120 if (rc) {
3121 if (rc != -ENODEV)
3122 return rc;
3123 ret = rc;
3124 }
Tejun Heod4b2bab2007-02-02 16:50:52 +09003125 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126
Tejun Heoe141d992007-06-10 14:26:20 +09003127 /* if device 1 was found in ata_devchk, wait for register
3128 * access briefly, then wait for BSY to clear.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 */
Tejun Heoe141d992007-06-10 14:26:20 +09003130 if (dev1) {
3131 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132
3133 ap->ops->dev_select(ap, 1);
Tejun Heoe141d992007-06-10 14:26:20 +09003134
3135 /* Wait for register access. Some ATAPI devices fail
3136 * to set nsect/lbal after reset, so don't waste too
3137 * much time on it. We're gonna wait for !BSY anyway.
3138 */
3139 for (i = 0; i < 2; i++) {
3140 u8 nsect, lbal;
3141
3142 nsect = ioread8(ioaddr->nsect_addr);
3143 lbal = ioread8(ioaddr->lbal_addr);
3144 if ((nsect == 1) && (lbal == 1))
3145 break;
3146 msleep(50); /* give drive a breather */
3147 }
3148
Tejun Heod4b2bab2007-02-02 16:50:52 +09003149 rc = ata_wait_ready(ap, deadline);
Tejun Heo9b893912007-02-02 16:50:52 +09003150 if (rc) {
3151 if (rc != -ENODEV)
3152 return rc;
3153 ret = rc;
3154 }
Tejun Heod4b2bab2007-02-02 16:50:52 +09003155 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156
3157 /* is all this really necessary? */
3158 ap->ops->dev_select(ap, 0);
3159 if (dev1)
3160 ap->ops->dev_select(ap, 1);
3161 if (dev0)
3162 ap->ops->dev_select(ap, 0);
Tejun Heod4b2bab2007-02-02 16:50:52 +09003163
Tejun Heo9b893912007-02-02 16:50:52 +09003164 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165}
3166
Tejun Heod4b2bab2007-02-02 16:50:52 +09003167static int ata_bus_softreset(struct ata_port *ap, unsigned int devmask,
3168 unsigned long deadline)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169{
3170 struct ata_ioports *ioaddr = &ap->ioaddr;
3171
Tejun Heo44877b42007-02-21 01:06:51 +09003172 DPRINTK("ata%u: bus reset via SRST\n", ap->print_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173
3174 /* software reset. causes dev0 to be selected */
Tejun Heo0d5ff562007-02-01 15:06:36 +09003175 iowrite8(ap->ctl, ioaddr->ctl_addr);
3176 udelay(20); /* FIXME: flush */
3177 iowrite8(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
3178 udelay(20); /* FIXME: flush */
3179 iowrite8(ap->ctl, ioaddr->ctl_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180
3181 /* spec mandates ">= 2ms" before checking status.
3182 * We wait 150ms, because that was the magic delay used for
3183 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
3184 * between when the ATA command register is written, and then
3185 * status is checked. Because waiting for "a while" before
3186 * checking status is fine, post SRST, we perform this magic
3187 * delay here as well.
Alan Cox09c7ad72006-03-22 15:52:40 +00003188 *
3189 * Old drivers/ide uses the 2mS rule and then waits for ready
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 */
3191 msleep(150);
3192
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05003193 /* Before we perform post reset processing we want to see if
Tejun Heo298a41c2006-03-25 02:58:13 +09003194 * the bus shows 0xFF because the odd clown forgets the D7
3195 * pulldown resistor.
3196 */
Tejun Heod1adc1b2006-10-09 18:32:15 +09003197 if (ata_check_status(ap) == 0xFF)
Tejun Heo9b893912007-02-02 16:50:52 +09003198 return -ENODEV;
Alan Cox09c7ad72006-03-22 15:52:40 +00003199
Tejun Heod4b2bab2007-02-02 16:50:52 +09003200 return ata_bus_post_reset(ap, devmask, deadline);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201}
3202
3203/**
3204 * ata_bus_reset - reset host port and associated ATA channel
3205 * @ap: port to reset
3206 *
3207 * This is typically the first time we actually start issuing
3208 * commands to the ATA channel. We wait for BSY to clear, then
3209 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
3210 * result. Determine what devices, if any, are on the channel
3211 * by looking at the device 0/1 error register. Look at the signature
3212 * stored in each device's taskfile registers, to determine if
3213 * the device is ATA or ATAPI.
3214 *
3215 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003216 * PCI/etc. bus probe sem.
Jeff Garzikcca39742006-08-24 03:19:22 -04003217 * Obtains host lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 *
3219 * SIDE EFFECTS:
Tejun Heo198e0fe2006-04-02 18:51:52 +09003220 * Sets ATA_FLAG_DISABLED if bus reset fails.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 */
3222
3223void ata_bus_reset(struct ata_port *ap)
3224{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09003225 struct ata_device *device = ap->link.device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 struct ata_ioports *ioaddr = &ap->ioaddr;
3227 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
3228 u8 err;
Tejun Heoaec5c3c2006-03-25 01:33:34 +09003229 unsigned int dev0, dev1 = 0, devmask = 0;
Tejun Heo9b893912007-02-02 16:50:52 +09003230 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231
Tejun Heo44877b42007-02-21 01:06:51 +09003232 DPRINTK("ENTER, host %u, port %u\n", ap->print_id, ap->port_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233
3234 /* determine if device 0/1 are present */
3235 if (ap->flags & ATA_FLAG_SATA_RESET)
3236 dev0 = 1;
3237 else {
3238 dev0 = ata_devchk(ap, 0);
3239 if (slave_possible)
3240 dev1 = ata_devchk(ap, 1);
3241 }
3242
3243 if (dev0)
3244 devmask |= (1 << 0);
3245 if (dev1)
3246 devmask |= (1 << 1);
3247
3248 /* select device 0 again */
3249 ap->ops->dev_select(ap, 0);
3250
3251 /* issue bus reset */
Tejun Heo9b893912007-02-02 16:50:52 +09003252 if (ap->flags & ATA_FLAG_SRST) {
3253 rc = ata_bus_softreset(ap, devmask, jiffies + 40 * HZ);
3254 if (rc && rc != -ENODEV)
Tejun Heoaec5c3c2006-03-25 01:33:34 +09003255 goto err_out;
Tejun Heo9b893912007-02-02 16:50:52 +09003256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003257
3258 /*
3259 * determine by signature whether we have ATA or ATAPI devices
3260 */
Tejun Heo3f198592007-09-02 23:23:57 +09003261 device[0].class = ata_dev_try_classify(&device[0], dev0, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 if ((slave_possible) && (err != 0x81))
Tejun Heo3f198592007-09-02 23:23:57 +09003263 device[1].class = ata_dev_try_classify(&device[1], dev1, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 /* is double-select really necessary? */
Tejun Heo9af5c9c2007-08-06 18:36:22 +09003266 if (device[1].class != ATA_DEV_NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 ap->ops->dev_select(ap, 1);
Tejun Heo9af5c9c2007-08-06 18:36:22 +09003268 if (device[0].class != ATA_DEV_NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 ap->ops->dev_select(ap, 0);
3270
3271 /* if no devices were detected, disable this port */
Tejun Heo9af5c9c2007-08-06 18:36:22 +09003272 if ((device[0].class == ATA_DEV_NONE) &&
3273 (device[1].class == ATA_DEV_NONE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003274 goto err_out;
3275
3276 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
3277 /* set up device control for ATA_FLAG_SATA_RESET */
Tejun Heo0d5ff562007-02-01 15:06:36 +09003278 iowrite8(ap->ctl, ioaddr->ctl_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 }
3280
3281 DPRINTK("EXIT\n");
3282 return;
3283
3284err_out:
Tejun Heof15a1da2006-05-15 20:57:56 +09003285 ata_port_printk(ap, KERN_ERR, "disabling port\n");
Jeff Garzikac8869d2007-08-16 03:17:03 -04003286 ata_port_disable(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003287
3288 DPRINTK("EXIT\n");
3289}
3290
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003291/**
Tejun Heo936fd732007-08-06 18:36:23 +09003292 * sata_link_debounce - debounce SATA phy status
3293 * @link: ATA link to debounce SATA phy status for
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003294 * @params: timing parameters { interval, duratinon, timeout } in msec
Tejun Heod4b2bab2007-02-02 16:50:52 +09003295 * @deadline: deadline jiffies for the operation
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003296 *
Tejun Heo936fd732007-08-06 18:36:23 +09003297* Make sure SStatus of @link reaches stable state, determined by
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003298 * holding the same value where DET is not 1 for @duration polled
3299 * every @interval, before @timeout. Timeout constraints the
Tejun Heod4b2bab2007-02-02 16:50:52 +09003300 * beginning of the stable state. Because DET gets stuck at 1 on
3301 * some controllers after hot unplugging, this functions waits
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003302 * until timeout then returns 0 if DET is stable at 1.
3303 *
Tejun Heod4b2bab2007-02-02 16:50:52 +09003304 * @timeout is further limited by @deadline. The sooner of the
3305 * two is used.
3306 *
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003307 * LOCKING:
3308 * Kernel thread context (may sleep)
3309 *
3310 * RETURNS:
3311 * 0 on success, -errno on failure.
3312 */
Tejun Heo936fd732007-08-06 18:36:23 +09003313int sata_link_debounce(struct ata_link *link, const unsigned long *params,
3314 unsigned long deadline)
Tejun Heo7a7921e2006-02-02 18:20:00 +09003315{
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003316 unsigned long interval_msec = params[0];
Tejun Heod4b2bab2007-02-02 16:50:52 +09003317 unsigned long duration = msecs_to_jiffies(params[1]);
3318 unsigned long last_jiffies, t;
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003319 u32 last, cur;
3320 int rc;
3321
Tejun Heod4b2bab2007-02-02 16:50:52 +09003322 t = jiffies + msecs_to_jiffies(params[2]);
3323 if (time_before(t, deadline))
3324 deadline = t;
3325
Tejun Heo936fd732007-08-06 18:36:23 +09003326 if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003327 return rc;
3328 cur &= 0xf;
3329
3330 last = cur;
3331 last_jiffies = jiffies;
3332
3333 while (1) {
3334 msleep(interval_msec);
Tejun Heo936fd732007-08-06 18:36:23 +09003335 if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003336 return rc;
3337 cur &= 0xf;
3338
3339 /* DET stable? */
3340 if (cur == last) {
Tejun Heod4b2bab2007-02-02 16:50:52 +09003341 if (cur == 1 && time_before(jiffies, deadline))
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003342 continue;
3343 if (time_after(jiffies, last_jiffies + duration))
3344 return 0;
3345 continue;
3346 }
3347
3348 /* unstable, start over */
3349 last = cur;
3350 last_jiffies = jiffies;
3351
Tejun Heof1545152007-07-16 14:29:40 +09003352 /* Check deadline. If debouncing failed, return
3353 * -EPIPE to tell upper layer to lower link speed.
3354 */
Tejun Heod4b2bab2007-02-02 16:50:52 +09003355 if (time_after(jiffies, deadline))
Tejun Heof1545152007-07-16 14:29:40 +09003356 return -EPIPE;
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003357 }
3358}
3359
3360/**
Tejun Heo936fd732007-08-06 18:36:23 +09003361 * sata_link_resume - resume SATA link
3362 * @link: ATA link to resume SATA
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003363 * @params: timing parameters { interval, duratinon, timeout } in msec
Tejun Heod4b2bab2007-02-02 16:50:52 +09003364 * @deadline: deadline jiffies for the operation
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003365 *
Tejun Heo936fd732007-08-06 18:36:23 +09003366 * Resume SATA phy @link and debounce it.
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003367 *
3368 * LOCKING:
3369 * Kernel thread context (may sleep)
3370 *
3371 * RETURNS:
3372 * 0 on success, -errno on failure.
3373 */
Tejun Heo936fd732007-08-06 18:36:23 +09003374int sata_link_resume(struct ata_link *link, const unsigned long *params,
3375 unsigned long deadline)
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003376{
3377 u32 scontrol;
Tejun Heo81952c52006-05-15 20:57:47 +09003378 int rc;
Tejun Heo7a7921e2006-02-02 18:20:00 +09003379
Tejun Heo936fd732007-08-06 18:36:23 +09003380 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
Tejun Heo81952c52006-05-15 20:57:47 +09003381 return rc;
3382
Tejun Heo852ee162006-04-01 01:38:18 +09003383 scontrol = (scontrol & 0x0f0) | 0x300;
Tejun Heo81952c52006-05-15 20:57:47 +09003384
Tejun Heo936fd732007-08-06 18:36:23 +09003385 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
Tejun Heo81952c52006-05-15 20:57:47 +09003386 return rc;
Tejun Heo7a7921e2006-02-02 18:20:00 +09003387
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003388 /* Some PHYs react badly if SStatus is pounded immediately
3389 * after resuming. Delay 200ms before debouncing.
3390 */
3391 msleep(200);
Tejun Heo7a7921e2006-02-02 18:20:00 +09003392
Tejun Heo936fd732007-08-06 18:36:23 +09003393 return sata_link_debounce(link, params, deadline);
Tejun Heo7a7921e2006-02-02 18:20:00 +09003394}
3395
Tejun Heof5914a42006-05-31 18:27:48 +09003396/**
3397 * ata_std_prereset - prepare for reset
Tejun Heocc0680a2007-08-06 18:36:23 +09003398 * @link: ATA link to be reset
Tejun Heod4b2bab2007-02-02 16:50:52 +09003399 * @deadline: deadline jiffies for the operation
Tejun Heof5914a42006-05-31 18:27:48 +09003400 *
Tejun Heocc0680a2007-08-06 18:36:23 +09003401 * @link is about to be reset. Initialize it. Failure from
Tejun Heob8cffc62007-02-02 16:50:52 +09003402 * prereset makes libata abort whole reset sequence and give up
3403 * that port, so prereset should be best-effort. It does its
3404 * best to prepare for reset sequence but if things go wrong, it
3405 * should just whine, not fail.
Tejun Heof5914a42006-05-31 18:27:48 +09003406 *
3407 * LOCKING:
3408 * Kernel thread context (may sleep)
3409 *
3410 * RETURNS:
3411 * 0 on success, -errno otherwise.
3412 */
Tejun Heocc0680a2007-08-06 18:36:23 +09003413int ata_std_prereset(struct ata_link *link, unsigned long deadline)
Tejun Heof5914a42006-05-31 18:27:48 +09003414{
Tejun Heocc0680a2007-08-06 18:36:23 +09003415 struct ata_port *ap = link->ap;
Tejun Heo936fd732007-08-06 18:36:23 +09003416 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heoe9c83912006-07-03 16:07:26 +09003417 const unsigned long *timing = sata_ehc_deb_timing(ehc);
Tejun Heof5914a42006-05-31 18:27:48 +09003418 int rc;
3419
Tejun Heo31daabd2007-02-02 16:50:52 +09003420 /* handle link resume */
Tejun Heo28324302006-07-03 16:07:26 +09003421 if ((ehc->i.flags & ATA_EHI_RESUME_LINK) &&
Tejun Heo0c887582007-08-06 18:36:23 +09003422 (link->flags & ATA_LFLAG_HRST_TO_RESUME))
Tejun Heo28324302006-07-03 16:07:26 +09003423 ehc->i.action |= ATA_EH_HARDRESET;
3424
Tejun Heof5914a42006-05-31 18:27:48 +09003425 /* if we're about to do hardreset, nothing more to do */
3426 if (ehc->i.action & ATA_EH_HARDRESET)
3427 return 0;
3428
Tejun Heo936fd732007-08-06 18:36:23 +09003429 /* if SATA, resume link */
Tejun Heoa16abc02007-05-21 18:33:47 +02003430 if (ap->flags & ATA_FLAG_SATA) {
Tejun Heo936fd732007-08-06 18:36:23 +09003431 rc = sata_link_resume(link, timing, deadline);
Tejun Heob8cffc62007-02-02 16:50:52 +09003432 /* whine about phy resume failure but proceed */
3433 if (rc && rc != -EOPNOTSUPP)
Tejun Heocc0680a2007-08-06 18:36:23 +09003434 ata_link_printk(link, KERN_WARNING, "failed to resume "
Tejun Heof5914a42006-05-31 18:27:48 +09003435 "link for reset (errno=%d)\n", rc);
Tejun Heof5914a42006-05-31 18:27:48 +09003436 }
3437
3438 /* Wait for !BSY if the controller can wait for the first D2H
3439 * Reg FIS and we don't know that no device is attached.
3440 */
Tejun Heo0c887582007-08-06 18:36:23 +09003441 if (!(link->flags & ATA_LFLAG_SKIP_D2H_BSY) && !ata_link_offline(link)) {
Tejun Heob8cffc62007-02-02 16:50:52 +09003442 rc = ata_wait_ready(ap, deadline);
Tejun Heo6dffaf62007-05-23 11:58:52 +02003443 if (rc && rc != -ENODEV) {
Tejun Heocc0680a2007-08-06 18:36:23 +09003444 ata_link_printk(link, KERN_WARNING, "device not ready "
Tejun Heob8cffc62007-02-02 16:50:52 +09003445 "(errno=%d), forcing hardreset\n", rc);
3446 ehc->i.action |= ATA_EH_HARDRESET;
3447 }
3448 }
Tejun Heof5914a42006-05-31 18:27:48 +09003449
3450 return 0;
3451}
3452
Tejun Heoc2bd5802006-01-24 17:05:22 +09003453/**
3454 * ata_std_softreset - reset host port via ATA SRST
Tejun Heocc0680a2007-08-06 18:36:23 +09003455 * @link: ATA link to reset
Tejun Heoc2bd5802006-01-24 17:05:22 +09003456 * @classes: resulting classes of attached devices
Tejun Heod4b2bab2007-02-02 16:50:52 +09003457 * @deadline: deadline jiffies for the operation
Tejun Heoc2bd5802006-01-24 17:05:22 +09003458 *
Tejun Heo52783c52006-05-31 18:28:22 +09003459 * Reset host port using ATA SRST.
Tejun Heoc2bd5802006-01-24 17:05:22 +09003460 *
3461 * LOCKING:
3462 * Kernel thread context (may sleep)
3463 *
3464 * RETURNS:
3465 * 0 on success, -errno otherwise.
3466 */
Tejun Heocc0680a2007-08-06 18:36:23 +09003467int ata_std_softreset(struct ata_link *link, unsigned int *classes,
Tejun Heod4b2bab2007-02-02 16:50:52 +09003468 unsigned long deadline)
Tejun Heoc2bd5802006-01-24 17:05:22 +09003469{
Tejun Heocc0680a2007-08-06 18:36:23 +09003470 struct ata_port *ap = link->ap;
Tejun Heoc2bd5802006-01-24 17:05:22 +09003471 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
Tejun Heod4b2bab2007-02-02 16:50:52 +09003472 unsigned int devmask = 0;
3473 int rc;
Tejun Heoc2bd5802006-01-24 17:05:22 +09003474 u8 err;
3475
3476 DPRINTK("ENTER\n");
3477
Tejun Heo936fd732007-08-06 18:36:23 +09003478 if (ata_link_offline(link)) {
Tejun Heo3a397462006-02-10 23:58:48 +09003479 classes[0] = ATA_DEV_NONE;
3480 goto out;
3481 }
3482
Tejun Heoc2bd5802006-01-24 17:05:22 +09003483 /* determine if device 0/1 are present */
3484 if (ata_devchk(ap, 0))
3485 devmask |= (1 << 0);
3486 if (slave_possible && ata_devchk(ap, 1))
3487 devmask |= (1 << 1);
3488
Tejun Heoc2bd5802006-01-24 17:05:22 +09003489 /* select device 0 again */
3490 ap->ops->dev_select(ap, 0);
3491
3492 /* issue bus reset */
3493 DPRINTK("about to softreset, devmask=%x\n", devmask);
Tejun Heod4b2bab2007-02-02 16:50:52 +09003494 rc = ata_bus_softreset(ap, devmask, deadline);
Tejun Heo9b893912007-02-02 16:50:52 +09003495 /* if link is occupied, -ENODEV too is an error */
Tejun Heo936fd732007-08-06 18:36:23 +09003496 if (rc && (rc != -ENODEV || sata_scr_valid(link))) {
Tejun Heocc0680a2007-08-06 18:36:23 +09003497 ata_link_printk(link, KERN_ERR, "SRST failed (errno=%d)\n", rc);
Tejun Heod4b2bab2007-02-02 16:50:52 +09003498 return rc;
Tejun Heoc2bd5802006-01-24 17:05:22 +09003499 }
3500
3501 /* determine by signature whether we have ATA or ATAPI devices */
Tejun Heo3f198592007-09-02 23:23:57 +09003502 classes[0] = ata_dev_try_classify(&link->device[0],
3503 devmask & (1 << 0), &err);
Tejun Heoc2bd5802006-01-24 17:05:22 +09003504 if (slave_possible && err != 0x81)
Tejun Heo3f198592007-09-02 23:23:57 +09003505 classes[1] = ata_dev_try_classify(&link->device[1],
3506 devmask & (1 << 1), &err);
Tejun Heoc2bd5802006-01-24 17:05:22 +09003507
Tejun Heo3a397462006-02-10 23:58:48 +09003508 out:
Tejun Heoc2bd5802006-01-24 17:05:22 +09003509 DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]);
3510 return 0;
3511}
3512
3513/**
Tejun Heocc0680a2007-08-06 18:36:23 +09003514 * sata_link_hardreset - reset link via SATA phy reset
3515 * @link: link to reset
Tejun Heob6103f62006-11-01 17:59:53 +09003516 * @timing: timing parameters { interval, duratinon, timeout } in msec
Tejun Heod4b2bab2007-02-02 16:50:52 +09003517 * @deadline: deadline jiffies for the operation
Tejun Heoc2bd5802006-01-24 17:05:22 +09003518 *
Tejun Heocc0680a2007-08-06 18:36:23 +09003519 * SATA phy-reset @link using DET bits of SControl register.
Tejun Heoc2bd5802006-01-24 17:05:22 +09003520 *
3521 * LOCKING:
3522 * Kernel thread context (may sleep)
3523 *
3524 * RETURNS:
3525 * 0 on success, -errno otherwise.
3526 */
Tejun Heocc0680a2007-08-06 18:36:23 +09003527int sata_link_hardreset(struct ata_link *link, const unsigned long *timing,
Tejun Heod4b2bab2007-02-02 16:50:52 +09003528 unsigned long deadline)
Tejun Heoc2bd5802006-01-24 17:05:22 +09003529{
Tejun Heo852ee162006-04-01 01:38:18 +09003530 u32 scontrol;
Tejun Heo81952c52006-05-15 20:57:47 +09003531 int rc;
Tejun Heo852ee162006-04-01 01:38:18 +09003532
Tejun Heoc2bd5802006-01-24 17:05:22 +09003533 DPRINTK("ENTER\n");
3534
Tejun Heo936fd732007-08-06 18:36:23 +09003535 if (sata_set_spd_needed(link)) {
Tejun Heo1c3fae42006-04-02 20:53:28 +09003536 /* SATA spec says nothing about how to reconfigure
3537 * spd. To be on the safe side, turn off phy during
3538 * reconfiguration. This works for at least ICH7 AHCI
3539 * and Sil3124.
3540 */
Tejun Heo936fd732007-08-06 18:36:23 +09003541 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
Tejun Heob6103f62006-11-01 17:59:53 +09003542 goto out;
Tejun Heo81952c52006-05-15 20:57:47 +09003543
Martin Hicksa34b6fc2006-07-05 15:06:13 -04003544 scontrol = (scontrol & 0x0f0) | 0x304;
Tejun Heo81952c52006-05-15 20:57:47 +09003545
Tejun Heo936fd732007-08-06 18:36:23 +09003546 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
Tejun Heob6103f62006-11-01 17:59:53 +09003547 goto out;
Tejun Heo1c3fae42006-04-02 20:53:28 +09003548
Tejun Heo936fd732007-08-06 18:36:23 +09003549 sata_set_spd(link);
Tejun Heo1c3fae42006-04-02 20:53:28 +09003550 }
3551
3552 /* issue phy wake/reset */
Tejun Heo936fd732007-08-06 18:36:23 +09003553 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
Tejun Heob6103f62006-11-01 17:59:53 +09003554 goto out;
Tejun Heo81952c52006-05-15 20:57:47 +09003555
Tejun Heo852ee162006-04-01 01:38:18 +09003556 scontrol = (scontrol & 0x0f0) | 0x301;
Tejun Heo81952c52006-05-15 20:57:47 +09003557
Tejun Heo936fd732007-08-06 18:36:23 +09003558 if ((rc = sata_scr_write_flush(link, SCR_CONTROL, scontrol)))
Tejun Heob6103f62006-11-01 17:59:53 +09003559 goto out;
Tejun Heoc2bd5802006-01-24 17:05:22 +09003560
Tejun Heo1c3fae42006-04-02 20:53:28 +09003561 /* Couldn't find anything in SATA I/II specs, but AHCI-1.1
Tejun Heoc2bd5802006-01-24 17:05:22 +09003562 * 10.4.2 says at least 1 ms.
3563 */
3564 msleep(1);
3565
Tejun Heo936fd732007-08-06 18:36:23 +09003566 /* bring link back */
3567 rc = sata_link_resume(link, timing, deadline);
Tejun Heob6103f62006-11-01 17:59:53 +09003568 out:
3569 DPRINTK("EXIT, rc=%d\n", rc);
3570 return rc;
3571}
3572
3573/**
3574 * sata_std_hardreset - reset host port via SATA phy reset
Tejun Heocc0680a2007-08-06 18:36:23 +09003575 * @link: link to reset
Tejun Heob6103f62006-11-01 17:59:53 +09003576 * @class: resulting class of attached device
Tejun Heod4b2bab2007-02-02 16:50:52 +09003577 * @deadline: deadline jiffies for the operation
Tejun Heob6103f62006-11-01 17:59:53 +09003578 *
3579 * SATA phy-reset host port using DET bits of SControl register,
3580 * wait for !BSY and classify the attached device.
3581 *
3582 * LOCKING:
3583 * Kernel thread context (may sleep)
3584 *
3585 * RETURNS:
3586 * 0 on success, -errno otherwise.
3587 */
Tejun Heocc0680a2007-08-06 18:36:23 +09003588int sata_std_hardreset(struct ata_link *link, unsigned int *class,
Tejun Heod4b2bab2007-02-02 16:50:52 +09003589 unsigned long deadline)
Tejun Heob6103f62006-11-01 17:59:53 +09003590{
Tejun Heocc0680a2007-08-06 18:36:23 +09003591 struct ata_port *ap = link->ap;
Tejun Heo936fd732007-08-06 18:36:23 +09003592 const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
Tejun Heob6103f62006-11-01 17:59:53 +09003593 int rc;
3594
3595 DPRINTK("ENTER\n");
3596
3597 /* do hardreset */
Tejun Heocc0680a2007-08-06 18:36:23 +09003598 rc = sata_link_hardreset(link, timing, deadline);
Tejun Heob6103f62006-11-01 17:59:53 +09003599 if (rc) {
Tejun Heocc0680a2007-08-06 18:36:23 +09003600 ata_link_printk(link, KERN_ERR,
Tejun Heob6103f62006-11-01 17:59:53 +09003601 "COMRESET failed (errno=%d)\n", rc);
3602 return rc;
3603 }
Tejun Heoc2bd5802006-01-24 17:05:22 +09003604
Tejun Heoc2bd5802006-01-24 17:05:22 +09003605 /* TODO: phy layer with polling, timeouts, etc. */
Tejun Heo936fd732007-08-06 18:36:23 +09003606 if (ata_link_offline(link)) {
Tejun Heoc2bd5802006-01-24 17:05:22 +09003607 *class = ATA_DEV_NONE;
3608 DPRINTK("EXIT, link offline\n");
3609 return 0;
3610 }
3611
Tejun Heo34fee222007-02-02 15:29:27 +09003612 /* wait a while before checking status, see SRST for more info */
3613 msleep(150);
3614
Tejun Heod4b2bab2007-02-02 16:50:52 +09003615 rc = ata_wait_ready(ap, deadline);
Tejun Heo9b893912007-02-02 16:50:52 +09003616 /* link occupied, -ENODEV too is an error */
3617 if (rc) {
Tejun Heocc0680a2007-08-06 18:36:23 +09003618 ata_link_printk(link, KERN_ERR,
Tejun Heod4b2bab2007-02-02 16:50:52 +09003619 "COMRESET failed (errno=%d)\n", rc);
3620 return rc;
Tejun Heoc2bd5802006-01-24 17:05:22 +09003621 }
3622
Tejun Heo3a397462006-02-10 23:58:48 +09003623 ap->ops->dev_select(ap, 0); /* probably unnecessary */
3624
Tejun Heo3f198592007-09-02 23:23:57 +09003625 *class = ata_dev_try_classify(link->device, 1, NULL);
Tejun Heoc2bd5802006-01-24 17:05:22 +09003626
3627 DPRINTK("EXIT, class=%u\n", *class);
3628 return 0;
3629}
3630
3631/**
3632 * ata_std_postreset - standard postreset callback
Tejun Heocc0680a2007-08-06 18:36:23 +09003633 * @link: the target ata_link
Tejun Heoc2bd5802006-01-24 17:05:22 +09003634 * @classes: classes of attached devices
3635 *
3636 * This function is invoked after a successful reset. Note that
3637 * the device might have been reset more than once using
3638 * different reset methods before postreset is invoked.
Tejun Heoc2bd5802006-01-24 17:05:22 +09003639 *
Tejun Heoc2bd5802006-01-24 17:05:22 +09003640 * LOCKING:
3641 * Kernel thread context (may sleep)
3642 */
Tejun Heocc0680a2007-08-06 18:36:23 +09003643void ata_std_postreset(struct ata_link *link, unsigned int *classes)
Tejun Heoc2bd5802006-01-24 17:05:22 +09003644{
Tejun Heocc0680a2007-08-06 18:36:23 +09003645 struct ata_port *ap = link->ap;
Tejun Heodc2b3512006-05-15 20:58:00 +09003646 u32 serror;
3647
Tejun Heoc2bd5802006-01-24 17:05:22 +09003648 DPRINTK("ENTER\n");
3649
Tejun Heoc2bd5802006-01-24 17:05:22 +09003650 /* print link status */
Tejun Heo936fd732007-08-06 18:36:23 +09003651 sata_print_link_status(link);
Tejun Heoc2bd5802006-01-24 17:05:22 +09003652
Tejun Heodc2b3512006-05-15 20:58:00 +09003653 /* clear SError */
Tejun Heo936fd732007-08-06 18:36:23 +09003654 if (sata_scr_read(link, SCR_ERROR, &serror) == 0)
3655 sata_scr_write(link, SCR_ERROR, serror);
Tejun Heodc2b3512006-05-15 20:58:00 +09003656
Tejun Heoc2bd5802006-01-24 17:05:22 +09003657 /* is double-select really necessary? */
3658 if (classes[0] != ATA_DEV_NONE)
3659 ap->ops->dev_select(ap, 1);
3660 if (classes[1] != ATA_DEV_NONE)
3661 ap->ops->dev_select(ap, 0);
3662
Tejun Heo3a397462006-02-10 23:58:48 +09003663 /* bail out if no device is present */
3664 if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
3665 DPRINTK("EXIT, no device\n");
3666 return;
3667 }
3668
3669 /* set up device control */
Tejun Heo0d5ff562007-02-01 15:06:36 +09003670 if (ap->ioaddr.ctl_addr)
3671 iowrite8(ap->ctl, ap->ioaddr.ctl_addr);
Tejun Heoc2bd5802006-01-24 17:05:22 +09003672
3673 DPRINTK("EXIT\n");
3674}
3675
Tejun Heoa62c0fc2006-01-24 17:05:22 +09003676/**
Tejun Heo623a3122006-03-05 17:55:58 +09003677 * ata_dev_same_device - Determine whether new ID matches configured device
Tejun Heo623a3122006-03-05 17:55:58 +09003678 * @dev: device to compare against
3679 * @new_class: class of the new device
3680 * @new_id: IDENTIFY page of the new device
3681 *
3682 * Compare @new_class and @new_id against @dev and determine
3683 * whether @dev is the device indicated by @new_class and
3684 * @new_id.
3685 *
3686 * LOCKING:
3687 * None.
3688 *
3689 * RETURNS:
3690 * 1 if @dev matches @new_class and @new_id, 0 otherwise.
3691 */
Tejun Heo3373efd2006-05-15 20:57:53 +09003692static int ata_dev_same_device(struct ata_device *dev, unsigned int new_class,
3693 const u16 *new_id)
Tejun Heo623a3122006-03-05 17:55:58 +09003694{
3695 const u16 *old_id = dev->id;
Tejun Heoa0cf7332007-01-02 20:18:49 +09003696 unsigned char model[2][ATA_ID_PROD_LEN + 1];
3697 unsigned char serial[2][ATA_ID_SERNO_LEN + 1];
Tejun Heo623a3122006-03-05 17:55:58 +09003698
3699 if (dev->class != new_class) {
Tejun Heof15a1da2006-05-15 20:57:56 +09003700 ata_dev_printk(dev, KERN_INFO, "class mismatch %d != %d\n",
3701 dev->class, new_class);
Tejun Heo623a3122006-03-05 17:55:58 +09003702 return 0;
3703 }
3704
Tejun Heoa0cf7332007-01-02 20:18:49 +09003705 ata_id_c_string(old_id, model[0], ATA_ID_PROD, sizeof(model[0]));
3706 ata_id_c_string(new_id, model[1], ATA_ID_PROD, sizeof(model[1]));
3707 ata_id_c_string(old_id, serial[0], ATA_ID_SERNO, sizeof(serial[0]));
3708 ata_id_c_string(new_id, serial[1], ATA_ID_SERNO, sizeof(serial[1]));
Tejun Heo623a3122006-03-05 17:55:58 +09003709
3710 if (strcmp(model[0], model[1])) {
Tejun Heof15a1da2006-05-15 20:57:56 +09003711 ata_dev_printk(dev, KERN_INFO, "model number mismatch "
3712 "'%s' != '%s'\n", model[0], model[1]);
Tejun Heo623a3122006-03-05 17:55:58 +09003713 return 0;
3714 }
3715
3716 if (strcmp(serial[0], serial[1])) {
Tejun Heof15a1da2006-05-15 20:57:56 +09003717 ata_dev_printk(dev, KERN_INFO, "serial number mismatch "
3718 "'%s' != '%s'\n", serial[0], serial[1]);
Tejun Heo623a3122006-03-05 17:55:58 +09003719 return 0;
3720 }
3721
Tejun Heo623a3122006-03-05 17:55:58 +09003722 return 1;
3723}
3724
3725/**
Tejun Heofe309112007-05-15 03:28:15 +09003726 * ata_dev_reread_id - Re-read IDENTIFY data
Henrik Kretzschmar3fae4502007-06-19 10:10:50 +02003727 * @dev: target ATA device
Tejun Heobff04642006-11-10 18:08:10 +09003728 * @readid_flags: read ID flags
Tejun Heo623a3122006-03-05 17:55:58 +09003729 *
3730 * Re-read IDENTIFY page and make sure @dev is still attached to
3731 * the port.
3732 *
3733 * LOCKING:
3734 * Kernel thread context (may sleep)
3735 *
3736 * RETURNS:
3737 * 0 on success, negative errno otherwise
3738 */
Tejun Heofe309112007-05-15 03:28:15 +09003739int ata_dev_reread_id(struct ata_device *dev, unsigned int readid_flags)
Tejun Heo623a3122006-03-05 17:55:58 +09003740{
Tejun Heo5eb45c02006-04-02 18:51:52 +09003741 unsigned int class = dev->class;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09003742 u16 *id = (void *)dev->link->ap->sector_buf;
Tejun Heo623a3122006-03-05 17:55:58 +09003743 int rc;
3744
Tejun Heofe635c72006-05-15 20:57:35 +09003745 /* read ID data */
Tejun Heobff04642006-11-10 18:08:10 +09003746 rc = ata_dev_read_id(dev, &class, readid_flags, id);
Tejun Heo623a3122006-03-05 17:55:58 +09003747 if (rc)
Tejun Heofe309112007-05-15 03:28:15 +09003748 return rc;
Tejun Heo623a3122006-03-05 17:55:58 +09003749
3750 /* is the device still there? */
Tejun Heofe309112007-05-15 03:28:15 +09003751 if (!ata_dev_same_device(dev, class, id))
3752 return -ENODEV;
Tejun Heo623a3122006-03-05 17:55:58 +09003753
Tejun Heofe635c72006-05-15 20:57:35 +09003754 memcpy(dev->id, id, sizeof(id[0]) * ATA_ID_WORDS);
Tejun Heofe309112007-05-15 03:28:15 +09003755 return 0;
3756}
3757
3758/**
3759 * ata_dev_revalidate - Revalidate ATA device
3760 * @dev: device to revalidate
Tejun Heo422c9da2007-09-23 13:14:12 +09003761 * @new_class: new class code
Tejun Heofe309112007-05-15 03:28:15 +09003762 * @readid_flags: read ID flags
3763 *
3764 * Re-read IDENTIFY page, make sure @dev is still attached to the
3765 * port and reconfigure it according to the new IDENTIFY page.
3766 *
3767 * LOCKING:
3768 * Kernel thread context (may sleep)
3769 *
3770 * RETURNS:
3771 * 0 on success, negative errno otherwise
3772 */
Tejun Heo422c9da2007-09-23 13:14:12 +09003773int ata_dev_revalidate(struct ata_device *dev, unsigned int new_class,
3774 unsigned int readid_flags)
Tejun Heofe309112007-05-15 03:28:15 +09003775{
Tejun Heo6ddcd3b2007-05-15 03:28:15 +09003776 u64 n_sectors = dev->n_sectors;
Tejun Heofe309112007-05-15 03:28:15 +09003777 int rc;
3778
3779 if (!ata_dev_enabled(dev))
3780 return -ENODEV;
3781
Tejun Heo422c9da2007-09-23 13:14:12 +09003782 /* fail early if !ATA && !ATAPI to avoid issuing [P]IDENTIFY to PMP */
3783 if (ata_class_enabled(new_class) &&
3784 new_class != ATA_DEV_ATA && new_class != ATA_DEV_ATAPI) {
3785 ata_dev_printk(dev, KERN_INFO, "class mismatch %u != %u\n",
3786 dev->class, new_class);
3787 rc = -ENODEV;
3788 goto fail;
3789 }
3790
Tejun Heofe309112007-05-15 03:28:15 +09003791 /* re-read ID */
3792 rc = ata_dev_reread_id(dev, readid_flags);
3793 if (rc)
3794 goto fail;
Tejun Heo623a3122006-03-05 17:55:58 +09003795
3796 /* configure device according to the new ID */
Tejun Heoefdaedc2006-11-01 18:38:52 +09003797 rc = ata_dev_configure(dev);
Tejun Heo6ddcd3b2007-05-15 03:28:15 +09003798 if (rc)
3799 goto fail;
3800
3801 /* verify n_sectors hasn't changed */
Tejun Heob54eebd2007-08-17 18:46:51 +09003802 if (dev->class == ATA_DEV_ATA && n_sectors &&
3803 dev->n_sectors != n_sectors) {
Tejun Heo6ddcd3b2007-05-15 03:28:15 +09003804 ata_dev_printk(dev, KERN_INFO, "n_sectors mismatch "
3805 "%llu != %llu\n",
3806 (unsigned long long)n_sectors,
3807 (unsigned long long)dev->n_sectors);
Tejun Heo8270bec2007-08-16 03:02:22 +09003808
3809 /* restore original n_sectors */
3810 dev->n_sectors = n_sectors;
3811
Tejun Heo6ddcd3b2007-05-15 03:28:15 +09003812 rc = -ENODEV;
3813 goto fail;
3814 }
3815
3816 return 0;
Tejun Heo623a3122006-03-05 17:55:58 +09003817
3818 fail:
Tejun Heof15a1da2006-05-15 20:57:56 +09003819 ata_dev_printk(dev, KERN_ERR, "revalidation failed (errno=%d)\n", rc);
Tejun Heo623a3122006-03-05 17:55:58 +09003820 return rc;
3821}
3822
Alan Cox6919a0a2006-10-27 19:08:46 -07003823struct ata_blacklist_entry {
3824 const char *model_num;
3825 const char *model_rev;
3826 unsigned long horkage;
3827};
3828
3829static const struct ata_blacklist_entry ata_device_blacklist [] = {
3830 /* Devices with DMA related problems under Linux */
3831 { "WDC AC11000H", NULL, ATA_HORKAGE_NODMA },
3832 { "WDC AC22100H", NULL, ATA_HORKAGE_NODMA },
3833 { "WDC AC32500H", NULL, ATA_HORKAGE_NODMA },
3834 { "WDC AC33100H", NULL, ATA_HORKAGE_NODMA },
3835 { "WDC AC31600H", NULL, ATA_HORKAGE_NODMA },
3836 { "WDC AC32100H", "24.09P07", ATA_HORKAGE_NODMA },
3837 { "WDC AC23200L", "21.10N21", ATA_HORKAGE_NODMA },
3838 { "Compaq CRD-8241B", NULL, ATA_HORKAGE_NODMA },
3839 { "CRD-8400B", NULL, ATA_HORKAGE_NODMA },
3840 { "CRD-8480B", NULL, ATA_HORKAGE_NODMA },
3841 { "CRD-8482B", NULL, ATA_HORKAGE_NODMA },
3842 { "CRD-84", NULL, ATA_HORKAGE_NODMA },
3843 { "SanDisk SDP3B", NULL, ATA_HORKAGE_NODMA },
3844 { "SanDisk SDP3B-64", NULL, ATA_HORKAGE_NODMA },
3845 { "SANYO CD-ROM CRD", NULL, ATA_HORKAGE_NODMA },
3846 { "HITACHI CDR-8", NULL, ATA_HORKAGE_NODMA },
3847 { "HITACHI CDR-8335", NULL, ATA_HORKAGE_NODMA },
3848 { "HITACHI CDR-8435", NULL, ATA_HORKAGE_NODMA },
3849 { "Toshiba CD-ROM XM-6202B", NULL, ATA_HORKAGE_NODMA },
3850 { "TOSHIBA CD-ROM XM-1702BC", NULL, ATA_HORKAGE_NODMA },
3851 { "CD-532E-A", NULL, ATA_HORKAGE_NODMA },
3852 { "E-IDE CD-ROM CR-840",NULL, ATA_HORKAGE_NODMA },
3853 { "CD-ROM Drive/F5A", NULL, ATA_HORKAGE_NODMA },
3854 { "WPI CDD-820", NULL, ATA_HORKAGE_NODMA },
3855 { "SAMSUNG CD-ROM SC-148C", NULL, ATA_HORKAGE_NODMA },
3856 { "SAMSUNG CD-ROM SC", NULL, ATA_HORKAGE_NODMA },
Alan Cox6919a0a2006-10-27 19:08:46 -07003857 { "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,ATA_HORKAGE_NODMA },
3858 { "_NEC DV5800A", NULL, ATA_HORKAGE_NODMA },
3859 { "SAMSUNG CD-ROM SN-124","N001", ATA_HORKAGE_NODMA },
Dave Jones39f19882007-05-21 14:31:03 -04003860 { "Seagate STT20000A", NULL, ATA_HORKAGE_NODMA },
Tejun Heo5acd50f2007-06-10 14:52:36 +09003861 { "IOMEGA ZIP 250 ATAPI", NULL, ATA_HORKAGE_NODMA }, /* temporary fix */
Tejun Heo39ce7122007-07-05 12:31:31 +09003862 { "IOMEGA ZIP 250 ATAPI Floppy",
3863 NULL, ATA_HORKAGE_NODMA },
Alan Cox6919a0a2006-10-27 19:08:46 -07003864
Albert Lee18d6e9d2007-04-02 11:34:15 +08003865 /* Weird ATAPI devices */
Tejun Heo40a1d532007-06-27 02:49:38 +09003866 { "TORiSAN DVD-ROM DRD-N216", NULL, ATA_HORKAGE_MAX_SEC_128 },
Albert Lee18d6e9d2007-04-02 11:34:15 +08003867
Alan Cox6919a0a2006-10-27 19:08:46 -07003868 /* Devices we expect to fail diagnostics */
3869
3870 /* Devices where NCQ should be avoided */
3871 /* NCQ is slow */
3872 { "WDC WD740ADFD-00", NULL, ATA_HORKAGE_NONCQ },
Tejun Heo09125ea2007-02-28 15:21:23 +09003873 /* http://thread.gmane.org/gmane.linux.ide/14907 */
3874 { "FUJITSU MHT2060BH", NULL, ATA_HORKAGE_NONCQ },
Paul Rolland7acfaf32007-03-26 21:43:44 -08003875 /* NCQ is broken */
Jeff Garzik539cc7c2007-09-20 16:31:47 -04003876 { "Maxtor *", "BANC*", ATA_HORKAGE_NONCQ },
Alan Cox0e3dbc02007-09-20 15:22:47 +01003877 { "Maxtor 7V300F0", "VA111630", ATA_HORKAGE_NONCQ },
Prarit Bhargava2f8d90a2007-07-10 18:13:28 -04003878 { "HITACHI HDS7250SASUN500G 0621KTAWSD", "K2AOAJ0AHITACHI",
Jeff Garzik539cc7c2007-09-20 16:31:47 -04003879 ATA_HORKAGE_NONCQ },
3880
Robert Hancock36e337d2007-04-02 22:05:29 -06003881 /* Blacklist entries taken from Silicon Image 3124/3132
3882 Windows driver .inf file - also several Linux problem reports */
3883 { "HTS541060G9SA00", "MB3OC60D", ATA_HORKAGE_NONCQ, },
3884 { "HTS541080G9SA00", "MB4OC60D", ATA_HORKAGE_NONCQ, },
3885 { "HTS541010G9SA00", "MBZOC60D", ATA_HORKAGE_NONCQ, },
Tejun Heobd9c5a32007-06-08 22:20:59 +09003886 /* Drives which do spurious command completion */
3887 { "HTS541680J9SA00", "SB2IC7EP", ATA_HORKAGE_NONCQ, },
Tejun Heo2f8fceb2007-06-18 14:38:41 +09003888 { "HTS541612J9SA00", "SBDIC7JP", ATA_HORKAGE_NONCQ, },
Tejun Heoe14cbfa2007-06-25 11:28:59 +09003889 { "Hitachi HTS541616J9SA00", "SB4OC70P", ATA_HORKAGE_NONCQ, },
Tejun Heo2f8fceb2007-06-18 14:38:41 +09003890 { "WDC WD740ADFD-00NLR1", NULL, ATA_HORKAGE_NONCQ, },
Tejun Heoa520f262007-07-10 16:16:18 +09003891 { "FUJITSU MHV2080BH", "00840028", ATA_HORKAGE_NONCQ, },
Tejun Heo3fb65892007-07-20 12:49:38 +09003892 { "ST9160821AS", "3.CLF", ATA_HORKAGE_NONCQ, },
Alan Cox0e3dbc02007-09-20 15:22:47 +01003893 { "ST3160812AS", "3.AD", ATA_HORKAGE_NONCQ, },
Tejun Heo5d6aca82007-07-28 16:25:25 +09003894 { "SAMSUNG HD401LJ", "ZZ100-15", ATA_HORKAGE_NONCQ, },
Alan Cox6919a0a2006-10-27 19:08:46 -07003895
Tejun Heo16c55b02007-08-29 11:58:33 +09003896 /* devices which puke on READ_NATIVE_MAX */
3897 { "HDS724040KLSA80", "KFAOA20N", ATA_HORKAGE_BROKEN_HPA, },
3898 { "WDC WD3200JD-00KLB0", "WD-WCAMR1130137", ATA_HORKAGE_BROKEN_HPA },
3899 { "WDC WD2500JD-00HBB0", "WD-WMAL71490727", ATA_HORKAGE_BROKEN_HPA },
3900 { "MAXTOR 6L080L4", "A93.0500", ATA_HORKAGE_BROKEN_HPA },
Alan Cox6919a0a2006-10-27 19:08:46 -07003901
3902 /* End Marker */
3903 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904};
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05003905
Jeff Garzik539cc7c2007-09-20 16:31:47 -04003906int strn_pattern_cmp(const char *patt, const char *name, int wildchar)
3907{
3908 const char *p;
3909 int len;
3910
3911 /*
3912 * check for trailing wildcard: *\0
3913 */
3914 p = strchr(patt, wildchar);
3915 if (p && ((*(p + 1)) == 0))
3916 len = p - patt;
3917 else
3918 len = strlen(name);
3919
3920 return strncmp(patt, name, len);
3921}
3922
Tejun Heo75683fe2007-07-05 13:31:27 +09003923static unsigned long ata_dev_blacklisted(const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924{
Tejun Heo8bfa79f2007-01-02 20:19:40 +09003925 unsigned char model_num[ATA_ID_PROD_LEN + 1];
3926 unsigned char model_rev[ATA_ID_FW_REV_LEN + 1];
Alan Cox6919a0a2006-10-27 19:08:46 -07003927 const struct ata_blacklist_entry *ad = ata_device_blacklist;
Albert Lee3a778272006-06-22 13:00:25 +08003928
Tejun Heo8bfa79f2007-01-02 20:19:40 +09003929 ata_id_c_string(dev->id, model_num, ATA_ID_PROD, sizeof(model_num));
3930 ata_id_c_string(dev->id, model_rev, ATA_ID_FW_REV, sizeof(model_rev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003931
Alan Cox6919a0a2006-10-27 19:08:46 -07003932 while (ad->model_num) {
Jeff Garzik539cc7c2007-09-20 16:31:47 -04003933 if (!strn_pattern_cmp(ad->model_num, model_num, '*')) {
Alan Cox6919a0a2006-10-27 19:08:46 -07003934 if (ad->model_rev == NULL)
3935 return ad->horkage;
Jeff Garzik539cc7c2007-09-20 16:31:47 -04003936 if (!strn_pattern_cmp(ad->model_rev, model_rev, '*'))
Alan Cox6919a0a2006-10-27 19:08:46 -07003937 return ad->horkage;
Alan Coxf4b15fe2006-03-22 15:54:04 +00003938 }
Alan Cox6919a0a2006-10-27 19:08:46 -07003939 ad++;
Alan Coxf4b15fe2006-03-22 15:54:04 +00003940 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941 return 0;
3942}
3943
Alan Cox6919a0a2006-10-27 19:08:46 -07003944static int ata_dma_blacklisted(const struct ata_device *dev)
3945{
3946 /* We don't support polling DMA.
3947 * DMA blacklist those ATAPI devices with CDB-intr (and use PIO)
3948 * if the LLDD handles only interrupts in the HSM_ST_LAST state.
3949 */
Tejun Heo9af5c9c2007-08-06 18:36:22 +09003950 if ((dev->link->ap->flags & ATA_FLAG_PIO_POLLING) &&
Alan Cox6919a0a2006-10-27 19:08:46 -07003951 (dev->flags & ATA_DFLAG_CDB_INTR))
3952 return 1;
Tejun Heo75683fe2007-07-05 13:31:27 +09003953 return (dev->horkage & ATA_HORKAGE_NODMA) ? 1 : 0;
Alan Cox6919a0a2006-10-27 19:08:46 -07003954}
3955
Tejun Heoa6d5a512006-03-06 04:31:57 +09003956/**
3957 * ata_dev_xfermask - Compute supported xfermask of the given device
Tejun Heoa6d5a512006-03-06 04:31:57 +09003958 * @dev: Device to compute xfermask for
3959 *
Tejun Heoacf356b2006-03-24 14:07:50 +09003960 * Compute supported xfermask of @dev and store it in
3961 * dev->*_mask. This function is responsible for applying all
3962 * known limits including host controller limits, device
3963 * blacklist, etc...
Tejun Heoa6d5a512006-03-06 04:31:57 +09003964 *
3965 * LOCKING:
3966 * None.
Tejun Heoa6d5a512006-03-06 04:31:57 +09003967 */
Tejun Heo3373efd2006-05-15 20:57:53 +09003968static void ata_dev_xfermask(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09003970 struct ata_link *link = dev->link;
3971 struct ata_port *ap = link->ap;
Jeff Garzikcca39742006-08-24 03:19:22 -04003972 struct ata_host *host = ap->host;
Tejun Heoa6d5a512006-03-06 04:31:57 +09003973 unsigned long xfer_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974
Tejun Heo37deecb2006-08-10 16:59:07 +09003975 /* controller modes available */
Tejun Heo565083e2006-04-02 17:54:47 +09003976 xfer_mask = ata_pack_xfermask(ap->pio_mask,
3977 ap->mwdma_mask, ap->udma_mask);
3978
Robert Hancock8343f882007-03-06 02:37:51 -08003979 /* drive modes available */
Tejun Heo37deecb2006-08-10 16:59:07 +09003980 xfer_mask &= ata_pack_xfermask(dev->pio_mask,
3981 dev->mwdma_mask, dev->udma_mask);
3982 xfer_mask &= ata_id_xfermask(dev->id);
Tejun Heo565083e2006-04-02 17:54:47 +09003983
Alan Coxb352e572006-08-10 18:52:12 +01003984 /*
3985 * CFA Advanced TrueIDE timings are not allowed on a shared
3986 * cable
3987 */
3988 if (ata_dev_pair(dev)) {
3989 /* No PIO5 or PIO6 */
3990 xfer_mask &= ~(0x03 << (ATA_SHIFT_PIO + 5));
3991 /* No MWDMA3 or MWDMA 4 */
3992 xfer_mask &= ~(0x03 << (ATA_SHIFT_MWDMA + 3));
3993 }
3994
Tejun Heo37deecb2006-08-10 16:59:07 +09003995 if (ata_dma_blacklisted(dev)) {
3996 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
Tejun Heof15a1da2006-05-15 20:57:56 +09003997 ata_dev_printk(dev, KERN_WARNING,
3998 "device is on DMA blacklist, disabling DMA\n");
Tejun Heo37deecb2006-08-10 16:59:07 +09003999 }
Tejun Heoa6d5a512006-03-06 04:31:57 +09004000
Petr Vandrovec14d66ab2007-03-08 10:12:12 +01004001 if ((host->flags & ATA_HOST_SIMPLEX) &&
4002 host->simplex_claimed && host->simplex_claimed != ap) {
Tejun Heo37deecb2006-08-10 16:59:07 +09004003 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
4004 ata_dev_printk(dev, KERN_WARNING, "simplex DMA is claimed by "
4005 "other device, disabling DMA\n");
Alan Cox5444a6f2006-03-27 18:58:20 +01004006 }
Tejun Heo565083e2006-04-02 17:54:47 +09004007
Jeff Garzike4246752007-03-09 09:56:46 -05004008 if (ap->flags & ATA_FLAG_NO_IORDY)
4009 xfer_mask &= ata_pio_mask_no_iordy(dev);
4010
Alan Cox5444a6f2006-03-27 18:58:20 +01004011 if (ap->ops->mode_filter)
Alan Coxa76b62ca2007-03-09 09:34:07 -05004012 xfer_mask = ap->ops->mode_filter(dev, xfer_mask);
Alan Cox5444a6f2006-03-27 18:58:20 +01004013
Robert Hancock8343f882007-03-06 02:37:51 -08004014 /* Apply cable rule here. Don't apply it early because when
4015 * we handle hot plug the cable type can itself change.
4016 * Check this last so that we know if the transfer rate was
4017 * solely limited by the cable.
4018 * Unknown or 80 wire cables reported host side are checked
4019 * drive side as well. Cases where we know a 40wire cable
4020 * is used safely for 80 are not checked here.
4021 */
4022 if (xfer_mask & (0xF8 << ATA_SHIFT_UDMA))
4023 /* UDMA/44 or higher would be available */
4024 if((ap->cbl == ATA_CBL_PATA40) ||
4025 (ata_drive_40wire(dev->id) &&
4026 (ap->cbl == ATA_CBL_PATA_UNK ||
4027 ap->cbl == ATA_CBL_PATA80))) {
4028 ata_dev_printk(dev, KERN_WARNING,
4029 "limited to UDMA/33 due to 40-wire cable\n");
4030 xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
4031 }
4032
Tejun Heo565083e2006-04-02 17:54:47 +09004033 ata_unpack_xfermask(xfer_mask, &dev->pio_mask,
4034 &dev->mwdma_mask, &dev->udma_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035}
4036
Linus Torvalds1da177e2005-04-16 15:20:36 -07004037/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039 * @dev: Device to which command will be sent
4040 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04004041 * Issue SET FEATURES - XFER MODE command to device @dev
4042 * on port @ap.
4043 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004044 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004045 * PCI/etc. bus probe sem.
Tejun Heo83206a22006-03-24 15:25:31 +09004046 *
4047 * RETURNS:
4048 * 0 on success, AC_ERR_* mask otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049 */
4050
Tejun Heo3373efd2006-05-15 20:57:53 +09004051static unsigned int ata_dev_set_xfermode(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004052{
Tejun Heoa0123702005-12-13 14:49:31 +09004053 struct ata_taskfile tf;
Tejun Heo83206a22006-03-24 15:25:31 +09004054 unsigned int err_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055
4056 /* set up set-features taskfile */
4057 DPRINTK("set features - xfer mode\n");
4058
Tejun Heo464cf172007-05-27 15:10:40 +02004059 /* Some controllers and ATAPI devices show flaky interrupt
4060 * behavior after setting xfer mode. Use polling instead.
4061 */
Tejun Heo3373efd2006-05-15 20:57:53 +09004062 ata_tf_init(dev, &tf);
Tejun Heoa0123702005-12-13 14:49:31 +09004063 tf.command = ATA_CMD_SET_FEATURES;
4064 tf.feature = SETFEATURES_XFER;
Tejun Heo464cf172007-05-27 15:10:40 +02004065 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_POLLING;
Tejun Heoa0123702005-12-13 14:49:31 +09004066 tf.protocol = ATA_PROT_NODATA;
4067 tf.nsect = dev->xfer_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068
Tejun Heo3373efd2006-05-15 20:57:53 +09004069 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070
Tejun Heo83206a22006-03-24 15:25:31 +09004071 DPRINTK("EXIT, err_mask=%x\n", err_mask);
4072 return err_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073}
4074
4075/**
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04004076 * ata_dev_set_AN - Issue SET FEATURES - SATA FEATURES
4077 * @dev: Device to which command will be sent
4078 * @enable: Whether to enable or disable the feature
4079 *
4080 * Issue SET FEATURES - SATA FEATURES command to device @dev
4081 * on port @ap with sector count set to indicate Asynchronous
4082 * Notification feature
4083 *
4084 * LOCKING:
4085 * PCI/etc. bus probe sem.
4086 *
4087 * RETURNS:
4088 * 0 on success, AC_ERR_* mask otherwise.
4089 */
4090static unsigned int ata_dev_set_AN(struct ata_device *dev, u8 enable)
4091{
4092 struct ata_taskfile tf;
4093 unsigned int err_mask;
4094
4095 /* set up set-features taskfile */
4096 DPRINTK("set features - SATA features\n");
4097
4098 ata_tf_init(dev, &tf);
4099 tf.command = ATA_CMD_SET_FEATURES;
4100 tf.feature = enable;
4101 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
4102 tf.protocol = ATA_PROT_NODATA;
4103 tf.nsect = SATA_AN;
4104
4105 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
4106
4107 DPRINTK("EXIT, err_mask=%x\n", err_mask);
4108 return err_mask;
4109}
4110
4111/**
Albert Lee8bf62ece2005-05-12 15:29:42 -04004112 * ata_dev_init_params - Issue INIT DEV PARAMS command
Albert Lee8bf62ece2005-05-12 15:29:42 -04004113 * @dev: Device to which command will be sent
Randy Dunlape2a7f772006-05-18 10:50:18 -07004114 * @heads: Number of heads (taskfile parameter)
4115 * @sectors: Number of sectors (taskfile parameter)
Albert Lee8bf62ece2005-05-12 15:29:42 -04004116 *
4117 * LOCKING:
Tejun Heo6aff8f12006-02-15 18:24:09 +09004118 * Kernel thread context (may sleep)
4119 *
4120 * RETURNS:
4121 * 0 on success, AC_ERR_* mask otherwise.
Albert Lee8bf62ece2005-05-12 15:29:42 -04004122 */
Tejun Heo3373efd2006-05-15 20:57:53 +09004123static unsigned int ata_dev_init_params(struct ata_device *dev,
4124 u16 heads, u16 sectors)
Albert Lee8bf62ece2005-05-12 15:29:42 -04004125{
Tejun Heoa0123702005-12-13 14:49:31 +09004126 struct ata_taskfile tf;
Tejun Heo6aff8f12006-02-15 18:24:09 +09004127 unsigned int err_mask;
Albert Lee8bf62ece2005-05-12 15:29:42 -04004128
4129 /* Number of sectors per track 1-255. Number of heads 1-16 */
4130 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
Albert Lee00b6f5e2006-03-27 16:39:18 +08004131 return AC_ERR_INVALID;
Albert Lee8bf62ece2005-05-12 15:29:42 -04004132
4133 /* set up init dev params taskfile */
4134 DPRINTK("init dev params \n");
4135
Tejun Heo3373efd2006-05-15 20:57:53 +09004136 ata_tf_init(dev, &tf);
Tejun Heoa0123702005-12-13 14:49:31 +09004137 tf.command = ATA_CMD_INIT_DEV_PARAMS;
4138 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
4139 tf.protocol = ATA_PROT_NODATA;
4140 tf.nsect = sectors;
4141 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
Albert Lee8bf62ece2005-05-12 15:29:42 -04004142
Tejun Heo3373efd2006-05-15 20:57:53 +09004143 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
Alan Cox18b24662007-08-08 14:28:49 +01004144 /* A clean abort indicates an original or just out of spec drive
4145 and we should continue as we issue the setup based on the
4146 drive reported working geometry */
4147 if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
4148 err_mask = 0;
Albert Lee8bf62ece2005-05-12 15:29:42 -04004149
Tejun Heo6aff8f12006-02-15 18:24:09 +09004150 DPRINTK("EXIT, err_mask=%x\n", err_mask);
4151 return err_mask;
Albert Lee8bf62ece2005-05-12 15:29:42 -04004152}
4153
4154/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004155 * ata_sg_clean - Unmap DMA memory associated with command
4156 * @qc: Command containing DMA memory to be released
4157 *
4158 * Unmap all mapped DMA memory associated with this command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159 *
4160 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004161 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162 */
Tejun Heo70e6ad02006-11-14 22:47:10 +09004163void ata_sg_clean(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164{
4165 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004166 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004168 void *pad_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169
Tejun Heoa46314742006-02-11 19:11:13 +09004170 WARN_ON(!(qc->flags & ATA_QCFLAG_DMAMAP));
4171 WARN_ON(sg == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172
4173 if (qc->flags & ATA_QCFLAG_SINGLE)
Jeff Garzikf1318832006-02-20 16:55:56 -05004174 WARN_ON(qc->n_elem > 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05004176 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004178 /* if we padded the buffer out to 32-bit bound, and data
4179 * xfer direction is from-device, we must copy from the
4180 * pad buffer back into the supplied buffer
4181 */
4182 if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
4183 pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
4184
4185 if (qc->flags & ATA_QCFLAG_SG) {
Jeff Garzike1410f22005-11-14 14:06:26 -05004186 if (qc->n_elem)
Brian King2f1f6102006-03-23 17:30:15 -06004187 dma_unmap_sg(ap->dev, sg, qc->n_elem, dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004188 /* restore last sg */
4189 sg[qc->orig_n_elem - 1].length += qc->pad_len;
4190 if (pad_buf) {
4191 struct scatterlist *psg = &qc->pad_sgent;
4192 void *addr = kmap_atomic(psg->page, KM_IRQ0);
4193 memcpy(addr + psg->offset, pad_buf, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05004194 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004195 }
4196 } else {
Tejun Heo2e242fa2006-02-20 23:48:38 +09004197 if (qc->n_elem)
Brian King2f1f6102006-03-23 17:30:15 -06004198 dma_unmap_single(ap->dev,
Jeff Garzike1410f22005-11-14 14:06:26 -05004199 sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
4200 dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004201 /* restore sg */
4202 sg->length += qc->pad_len;
4203 if (pad_buf)
4204 memcpy(qc->buf_virt + sg->length - qc->pad_len,
4205 pad_buf, qc->pad_len);
4206 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004207
4208 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004209 qc->__sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210}
4211
4212/**
4213 * ata_fill_sg - Fill PCI IDE PRD table
4214 * @qc: Metadata associated with taskfile to be transferred
4215 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04004216 * Fill PCI IDE PRD (scatter-gather) table with segments
4217 * associated with the current disk command.
4218 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004220 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221 *
4222 */
4223static void ata_fill_sg(struct ata_queued_cmd *qc)
4224{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004226 struct scatterlist *sg;
4227 unsigned int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004228
Tejun Heoa46314742006-02-11 19:11:13 +09004229 WARN_ON(qc->__sg == NULL);
Jeff Garzikf1318832006-02-20 16:55:56 -05004230 WARN_ON(qc->n_elem == 0 && qc->pad_len == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231
4232 idx = 0;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004233 ata_for_each_sg(sg, qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234 u32 addr, offset;
4235 u32 sg_len, len;
4236
4237 /* determine if physical DMA addr spans 64K boundary.
4238 * Note h/w doesn't support 64-bit, so we unconditionally
4239 * truncate dma_addr_t to u32.
4240 */
4241 addr = (u32) sg_dma_address(sg);
4242 sg_len = sg_dma_len(sg);
4243
4244 while (sg_len) {
4245 offset = addr & 0xffff;
4246 len = sg_len;
4247 if ((offset + sg_len) > 0x10000)
4248 len = 0x10000 - offset;
4249
4250 ap->prd[idx].addr = cpu_to_le32(addr);
4251 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
4252 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
4253
4254 idx++;
4255 sg_len -= len;
4256 addr += len;
4257 }
4258 }
4259
4260 if (idx)
4261 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
4262}
Tejun Heob9a41972007-06-27 02:48:43 +09004263
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264/**
Alan Coxd26fc952007-07-06 19:13:52 -04004265 * ata_fill_sg_dumb - Fill PCI IDE PRD table
4266 * @qc: Metadata associated with taskfile to be transferred
4267 *
4268 * Fill PCI IDE PRD (scatter-gather) table with segments
4269 * associated with the current disk command. Perform the fill
4270 * so that we avoid writing any length 64K records for
4271 * controllers that don't follow the spec.
4272 *
4273 * LOCKING:
4274 * spin_lock_irqsave(host lock)
4275 *
4276 */
4277static void ata_fill_sg_dumb(struct ata_queued_cmd *qc)
4278{
4279 struct ata_port *ap = qc->ap;
4280 struct scatterlist *sg;
4281 unsigned int idx;
4282
4283 WARN_ON(qc->__sg == NULL);
4284 WARN_ON(qc->n_elem == 0 && qc->pad_len == 0);
4285
4286 idx = 0;
4287 ata_for_each_sg(sg, qc) {
4288 u32 addr, offset;
4289 u32 sg_len, len, blen;
4290
4291 /* determine if physical DMA addr spans 64K boundary.
4292 * Note h/w doesn't support 64-bit, so we unconditionally
4293 * truncate dma_addr_t to u32.
4294 */
4295 addr = (u32) sg_dma_address(sg);
4296 sg_len = sg_dma_len(sg);
4297
4298 while (sg_len) {
4299 offset = addr & 0xffff;
4300 len = sg_len;
4301 if ((offset + sg_len) > 0x10000)
4302 len = 0x10000 - offset;
4303
4304 blen = len & 0xffff;
4305 ap->prd[idx].addr = cpu_to_le32(addr);
4306 if (blen == 0) {
4307 /* Some PATA chipsets like the CS5530 can't
4308 cope with 0x0000 meaning 64K as the spec says */
4309 ap->prd[idx].flags_len = cpu_to_le32(0x8000);
4310 blen = 0x8000;
4311 ap->prd[++idx].addr = cpu_to_le32(addr + 0x8000);
4312 }
4313 ap->prd[idx].flags_len = cpu_to_le32(blen);
4314 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
4315
4316 idx++;
4317 sg_len -= len;
4318 addr += len;
4319 }
4320 }
4321
4322 if (idx)
4323 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
4324}
4325
4326/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
4328 * @qc: Metadata associated with taskfile to check
4329 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04004330 * Allow low-level driver to filter ATA PACKET commands, returning
4331 * a status indicating whether or not it is OK to use DMA for the
4332 * supplied PACKET command.
4333 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004335 * spin_lock_irqsave(host lock)
Jeff Garzik0cba6322005-05-30 19:49:12 -04004336 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337 * RETURNS: 0 when ATAPI DMA can be used
4338 * nonzero otherwise
4339 */
4340int ata_check_atapi_dma(struct ata_queued_cmd *qc)
4341{
4342 struct ata_port *ap = qc->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343
Tejun Heob9a41972007-06-27 02:48:43 +09004344 /* Don't allow DMA if it isn't multiple of 16 bytes. Quite a
4345 * few ATAPI devices choke on such DMA requests.
4346 */
4347 if (unlikely(qc->nbytes & 15))
4348 return 1;
Albert Lee6f23a312007-04-02 11:39:25 +08004349
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350 if (ap->ops->check_atapi_dma)
Tejun Heob9a41972007-06-27 02:48:43 +09004351 return ap->ops->check_atapi_dma(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352
Tejun Heob9a41972007-06-27 02:48:43 +09004353 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354}
Tejun Heob9a41972007-06-27 02:48:43 +09004355
Linus Torvalds1da177e2005-04-16 15:20:36 -07004356/**
Tejun Heo31cc23b2007-09-23 13:14:12 +09004357 * ata_std_qc_defer - Check whether a qc needs to be deferred
4358 * @qc: ATA command in question
4359 *
4360 * Non-NCQ commands cannot run with any other command, NCQ or
4361 * not. As upper layer only knows the queue depth, we are
4362 * responsible for maintaining exclusion. This function checks
4363 * whether a new command @qc can be issued.
4364 *
4365 * LOCKING:
4366 * spin_lock_irqsave(host lock)
4367 *
4368 * RETURNS:
4369 * ATA_DEFER_* if deferring is needed, 0 otherwise.
4370 */
4371int ata_std_qc_defer(struct ata_queued_cmd *qc)
4372{
4373 struct ata_link *link = qc->dev->link;
4374
4375 if (qc->tf.protocol == ATA_PROT_NCQ) {
4376 if (!ata_tag_valid(link->active_tag))
4377 return 0;
4378 } else {
4379 if (!ata_tag_valid(link->active_tag) && !link->sactive)
4380 return 0;
4381 }
4382
4383 return ATA_DEFER_LINK;
4384}
4385
4386/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004387 * ata_qc_prep - Prepare taskfile for submission
4388 * @qc: Metadata associated with taskfile to be prepared
4389 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04004390 * Prepare ATA taskfile for submission.
4391 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004392 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004393 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394 */
4395void ata_qc_prep(struct ata_queued_cmd *qc)
4396{
4397 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
4398 return;
4399
4400 ata_fill_sg(qc);
4401}
4402
Alan Coxd26fc952007-07-06 19:13:52 -04004403/**
4404 * ata_dumb_qc_prep - Prepare taskfile for submission
4405 * @qc: Metadata associated with taskfile to be prepared
4406 *
4407 * Prepare ATA taskfile for submission.
4408 *
4409 * LOCKING:
4410 * spin_lock_irqsave(host lock)
4411 */
4412void ata_dumb_qc_prep(struct ata_queued_cmd *qc)
4413{
4414 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
4415 return;
4416
4417 ata_fill_sg_dumb(qc);
4418}
4419
Brian Kinge46834c2006-03-17 17:04:03 -06004420void ata_noop_qc_prep(struct ata_queued_cmd *qc) { }
4421
Jeff Garzik0cba6322005-05-30 19:49:12 -04004422/**
4423 * ata_sg_init_one - Associate command with memory buffer
4424 * @qc: Command to be associated
4425 * @buf: Memory buffer
4426 * @buflen: Length of memory buffer, in bytes.
4427 *
4428 * Initialize the data-related elements of queued_cmd @qc
4429 * to point to a single memory buffer, @buf of byte length @buflen.
4430 *
4431 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004432 * spin_lock_irqsave(host lock)
Jeff Garzik0cba6322005-05-30 19:49:12 -04004433 */
4434
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
4436{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004437 qc->flags |= ATA_QCFLAG_SINGLE;
4438
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004439 qc->__sg = &qc->sgent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440 qc->n_elem = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004441 qc->orig_n_elem = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442 qc->buf_virt = buf;
Brian King233277c2006-06-07 11:25:31 -05004443 qc->nbytes = buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444
Tejun Heo61c05962006-11-14 22:35:43 +09004445 sg_init_one(&qc->sgent, buf, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446}
4447
Jeff Garzik0cba6322005-05-30 19:49:12 -04004448/**
4449 * ata_sg_init - Associate command with scatter-gather table.
4450 * @qc: Command to be associated
4451 * @sg: Scatter-gather table.
4452 * @n_elem: Number of elements in s/g table.
4453 *
4454 * Initialize the data-related elements of queued_cmd @qc
4455 * to point to a scatter-gather table @sg, containing @n_elem
4456 * elements.
4457 *
4458 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004459 * spin_lock_irqsave(host lock)
Jeff Garzik0cba6322005-05-30 19:49:12 -04004460 */
4461
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
4463 unsigned int n_elem)
4464{
4465 qc->flags |= ATA_QCFLAG_SG;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004466 qc->__sg = sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467 qc->n_elem = n_elem;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004468 qc->orig_n_elem = n_elem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469}
4470
4471/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004472 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
4473 * @qc: Command with memory buffer to be mapped.
4474 *
4475 * DMA-map the memory buffer associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476 *
4477 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004478 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479 *
4480 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004481 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482 */
4483
4484static int ata_sg_setup_one(struct ata_queued_cmd *qc)
4485{
4486 struct ata_port *ap = qc->ap;
4487 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004488 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489 dma_addr_t dma_address;
Tejun Heo2e242fa2006-02-20 23:48:38 +09004490 int trim_sg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004492 /* we must lengthen transfers to end on a 32-bit boundary */
4493 qc->pad_len = sg->length & 3;
4494 if (qc->pad_len) {
4495 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
4496 struct scatterlist *psg = &qc->pad_sgent;
4497
Tejun Heoa46314742006-02-11 19:11:13 +09004498 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004499
4500 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
4501
4502 if (qc->tf.flags & ATA_TFLAG_WRITE)
4503 memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
4504 qc->pad_len);
4505
4506 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
4507 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
4508 /* trim sg */
4509 sg->length -= qc->pad_len;
Tejun Heo2e242fa2006-02-20 23:48:38 +09004510 if (sg->length == 0)
4511 trim_sg = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004512
4513 DPRINTK("padding done, sg->length=%u pad_len=%u\n",
4514 sg->length, qc->pad_len);
4515 }
4516
Tejun Heo2e242fa2006-02-20 23:48:38 +09004517 if (trim_sg) {
4518 qc->n_elem--;
Jeff Garzike1410f22005-11-14 14:06:26 -05004519 goto skip_map;
4520 }
4521
Brian King2f1f6102006-03-23 17:30:15 -06004522 dma_address = dma_map_single(ap->dev, qc->buf_virt,
Albert Lee32529e02005-05-26 03:49:42 -04004523 sg->length, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05004524 if (dma_mapping_error(dma_address)) {
4525 /* restore sg */
4526 sg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05004528 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529
4530 sg_dma_address(sg) = dma_address;
Albert Lee32529e02005-05-26 03:49:42 -04004531 sg_dma_len(sg) = sg->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532
Tejun Heo2e242fa2006-02-20 23:48:38 +09004533skip_map:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
4535 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
4536
4537 return 0;
4538}
4539
4540/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004541 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
4542 * @qc: Command with scatter-gather table to be mapped.
4543 *
4544 * DMA-map the scatter-gather table associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545 *
4546 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004547 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548 *
4549 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004550 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551 *
4552 */
4553
4554static int ata_sg_setup(struct ata_queued_cmd *qc)
4555{
4556 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004557 struct scatterlist *sg = qc->__sg;
4558 struct scatterlist *lsg = &sg[qc->n_elem - 1];
Jeff Garzike1410f22005-11-14 14:06:26 -05004559 int n_elem, pre_n_elem, dir, trim_sg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560
Tejun Heo44877b42007-02-21 01:06:51 +09004561 VPRINTK("ENTER, ata%u\n", ap->print_id);
Tejun Heoa46314742006-02-11 19:11:13 +09004562 WARN_ON(!(qc->flags & ATA_QCFLAG_SG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004564 /* we must lengthen transfers to end on a 32-bit boundary */
4565 qc->pad_len = lsg->length & 3;
4566 if (qc->pad_len) {
4567 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
4568 struct scatterlist *psg = &qc->pad_sgent;
4569 unsigned int offset;
4570
Tejun Heoa46314742006-02-11 19:11:13 +09004571 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004572
4573 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
4574
4575 /*
4576 * psg->page/offset are used to copy to-be-written
4577 * data in this function or read data in ata_sg_clean.
4578 */
4579 offset = lsg->offset + lsg->length - qc->pad_len;
4580 psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
4581 psg->offset = offset_in_page(offset);
4582
4583 if (qc->tf.flags & ATA_TFLAG_WRITE) {
4584 void *addr = kmap_atomic(psg->page, KM_IRQ0);
4585 memcpy(pad_buf, addr + psg->offset, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05004586 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004587 }
4588
4589 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
4590 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
4591 /* trim last sg */
4592 lsg->length -= qc->pad_len;
Jeff Garzike1410f22005-11-14 14:06:26 -05004593 if (lsg->length == 0)
4594 trim_sg = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004595
4596 DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
4597 qc->n_elem - 1, lsg->length, qc->pad_len);
4598 }
4599
Jeff Garzike1410f22005-11-14 14:06:26 -05004600 pre_n_elem = qc->n_elem;
4601 if (trim_sg && pre_n_elem)
4602 pre_n_elem--;
4603
4604 if (!pre_n_elem) {
4605 n_elem = 0;
4606 goto skip_map;
4607 }
4608
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609 dir = qc->dma_dir;
Brian King2f1f6102006-03-23 17:30:15 -06004610 n_elem = dma_map_sg(ap->dev, sg, pre_n_elem, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05004611 if (n_elem < 1) {
4612 /* restore last sg */
4613 lsg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05004615 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616
4617 DPRINTK("%d sg elements mapped\n", n_elem);
4618
Jeff Garzike1410f22005-11-14 14:06:26 -05004619skip_map:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620 qc->n_elem = n_elem;
4621
4622 return 0;
4623}
4624
4625/**
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004626 * swap_buf_le16 - swap halves of 16-bit words in place
Edward Falk0baab862005-06-02 18:17:13 -04004627 * @buf: Buffer to swap
4628 * @buf_words: Number of 16-bit words in buffer.
4629 *
4630 * Swap halves of 16-bit words if needed to convert from
4631 * little-endian byte order to native cpu byte order, or
4632 * vice-versa.
4633 *
4634 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004635 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04004636 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637void swap_buf_le16(u16 *buf, unsigned int buf_words)
4638{
4639#ifdef __BIG_ENDIAN
4640 unsigned int i;
4641
4642 for (i = 0; i < buf_words; i++)
4643 buf[i] = le16_to_cpu(buf[i]);
4644#endif /* __BIG_ENDIAN */
4645}
4646
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004647/**
Tejun Heo0d5ff562007-02-01 15:06:36 +09004648 * ata_data_xfer - Transfer data by PIO
Alan Coxa6b2c5d2006-05-22 16:59:59 +01004649 * @adev: device to target
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004650 * @buf: data buffer
4651 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04004652 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004653 *
4654 * Transfer data from/to the device data register by PIO.
4655 *
4656 * LOCKING:
4657 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004658 */
Tejun Heo0d5ff562007-02-01 15:06:36 +09004659void ata_data_xfer(struct ata_device *adev, unsigned char *buf,
4660 unsigned int buflen, int write_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09004662 struct ata_port *ap = adev->link->ap;
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004663 unsigned int words = buflen >> 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004665 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666 if (write_data)
Tejun Heo0d5ff562007-02-01 15:06:36 +09004667 iowrite16_rep(ap->ioaddr.data_addr, buf, words);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668 else
Tejun Heo0d5ff562007-02-01 15:06:36 +09004669 ioread16_rep(ap->ioaddr.data_addr, buf, words);
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004670
4671 /* Transfer trailing 1 byte, if any. */
4672 if (unlikely(buflen & 0x01)) {
4673 u16 align_buf[1] = { 0 };
4674 unsigned char *trailing_buf = buf + buflen - 1;
4675
4676 if (write_data) {
4677 memcpy(align_buf, trailing_buf, 1);
Tejun Heo0d5ff562007-02-01 15:06:36 +09004678 iowrite16(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004679 } else {
Tejun Heo0d5ff562007-02-01 15:06:36 +09004680 align_buf[0] = cpu_to_le16(ioread16(ap->ioaddr.data_addr));
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004681 memcpy(trailing_buf, align_buf, 1);
4682 }
4683 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684}
4685
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004686/**
Tejun Heo0d5ff562007-02-01 15:06:36 +09004687 * ata_data_xfer_noirq - Transfer data by PIO
Alan Cox75e99582006-05-24 14:14:41 +01004688 * @adev: device to target
4689 * @buf: data buffer
4690 * @buflen: buffer length
4691 * @write_data: read/write
4692 *
Tejun Heo88574552006-06-25 20:00:35 +09004693 * Transfer data from/to the device data register by PIO. Do the
Alan Cox75e99582006-05-24 14:14:41 +01004694 * transfer with interrupts disabled.
4695 *
4696 * LOCKING:
4697 * Inherited from caller.
4698 */
Tejun Heo0d5ff562007-02-01 15:06:36 +09004699void ata_data_xfer_noirq(struct ata_device *adev, unsigned char *buf,
4700 unsigned int buflen, int write_data)
Alan Cox75e99582006-05-24 14:14:41 +01004701{
4702 unsigned long flags;
4703 local_irq_save(flags);
Tejun Heo0d5ff562007-02-01 15:06:36 +09004704 ata_data_xfer(adev, buf, buflen, write_data);
Alan Cox75e99582006-05-24 14:14:41 +01004705 local_irq_restore(flags);
4706}
4707
4708
4709/**
Mark Lord5a5dbd12007-03-16 10:22:26 -04004710 * ata_pio_sector - Transfer a sector of data.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004711 * @qc: Command on going
4712 *
Mark Lord5a5dbd12007-03-16 10:22:26 -04004713 * Transfer qc->sect_size bytes of data from/to the ATA device.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004714 *
4715 * LOCKING:
4716 * Inherited from caller.
4717 */
4718
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719static void ata_pio_sector(struct ata_queued_cmd *qc)
4720{
4721 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004722 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723 struct ata_port *ap = qc->ap;
4724 struct page *page;
4725 unsigned int offset;
4726 unsigned char *buf;
4727
Mark Lord5a5dbd12007-03-16 10:22:26 -04004728 if (qc->curbytes == qc->nbytes - qc->sect_size)
Albert Lee14be71f2005-09-27 17:36:35 +08004729 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730
4731 page = sg[qc->cursg].page;
Tejun Heo726f0782007-01-03 17:30:39 +09004732 offset = sg[qc->cursg].offset + qc->cursg_ofs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733
4734 /* get the current page and offset */
4735 page = nth_page(page, (offset >> PAGE_SHIFT));
4736 offset %= PAGE_SIZE;
4737
Albert Lee7282aa42005-10-09 09:46:07 -04004738 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
4739
Albert Lee91b8b312005-10-09 09:48:44 -04004740 if (PageHighMem(page)) {
4741 unsigned long flags;
Albert Lee7282aa42005-10-09 09:46:07 -04004742
Alan Coxa6b2c5d2006-05-22 16:59:59 +01004743 /* FIXME: use a bounce buffer */
Albert Lee91b8b312005-10-09 09:48:44 -04004744 local_irq_save(flags);
4745 buf = kmap_atomic(page, KM_IRQ0);
Albert Lee083958d2005-10-09 09:47:31 -04004746
Albert Lee91b8b312005-10-09 09:48:44 -04004747 /* do the actual data transfer */
Mark Lord5a5dbd12007-03-16 10:22:26 -04004748 ap->ops->data_xfer(qc->dev, buf + offset, qc->sect_size, do_write);
Albert Lee91b8b312005-10-09 09:48:44 -04004749
4750 kunmap_atomic(buf, KM_IRQ0);
4751 local_irq_restore(flags);
4752 } else {
4753 buf = page_address(page);
Mark Lord5a5dbd12007-03-16 10:22:26 -04004754 ap->ops->data_xfer(qc->dev, buf + offset, qc->sect_size, do_write);
Albert Lee91b8b312005-10-09 09:48:44 -04004755 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756
Mark Lord5a5dbd12007-03-16 10:22:26 -04004757 qc->curbytes += qc->sect_size;
4758 qc->cursg_ofs += qc->sect_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759
Tejun Heo726f0782007-01-03 17:30:39 +09004760 if (qc->cursg_ofs == (&sg[qc->cursg])->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761 qc->cursg++;
4762 qc->cursg_ofs = 0;
4763 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004766/**
Mark Lord5a5dbd12007-03-16 10:22:26 -04004767 * ata_pio_sectors - Transfer one or many sectors.
Albert Lee07f6f7d2005-11-01 19:33:20 +08004768 * @qc: Command on going
4769 *
Mark Lord5a5dbd12007-03-16 10:22:26 -04004770 * Transfer one or many sectors of data from/to the
Albert Lee07f6f7d2005-11-01 19:33:20 +08004771 * ATA device for the DRQ request.
4772 *
4773 * LOCKING:
4774 * Inherited from caller.
4775 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776
Albert Lee07f6f7d2005-11-01 19:33:20 +08004777static void ata_pio_sectors(struct ata_queued_cmd *qc)
4778{
4779 if (is_multi_taskfile(&qc->tf)) {
4780 /* READ/WRITE MULTIPLE */
4781 unsigned int nsect;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782
Jeff Garzik587005d2006-02-11 18:17:32 -05004783 WARN_ON(qc->dev->multi_count == 0);
Albert Lee07f6f7d2005-11-01 19:33:20 +08004784
Mark Lord5a5dbd12007-03-16 10:22:26 -04004785 nsect = min((qc->nbytes - qc->curbytes) / qc->sect_size,
Tejun Heo726f0782007-01-03 17:30:39 +09004786 qc->dev->multi_count);
Albert Lee07f6f7d2005-11-01 19:33:20 +08004787 while (nsect--)
4788 ata_pio_sector(qc);
4789 } else
4790 ata_pio_sector(qc);
Albert Lee4cc980b2007-08-15 03:19:45 -04004791
4792 ata_altstatus(qc->ap); /* flush */
Albert Lee07f6f7d2005-11-01 19:33:20 +08004793}
4794
4795/**
Albert Leec71c1852005-10-04 06:03:45 -04004796 * atapi_send_cdb - Write CDB bytes to hardware
4797 * @ap: Port to which ATAPI device is attached.
4798 * @qc: Taskfile currently active
4799 *
4800 * When device has indicated its readiness to accept
4801 * a CDB, this function is called. Send the CDB.
4802 *
4803 * LOCKING:
4804 * caller.
4805 */
4806
4807static void atapi_send_cdb(struct ata_port *ap, struct ata_queued_cmd *qc)
4808{
4809 /* send SCSI cdb */
4810 DPRINTK("send cdb\n");
Jeff Garzikdb024d52006-02-13 00:23:57 -05004811 WARN_ON(qc->dev->cdb_len < 12);
Albert Leec71c1852005-10-04 06:03:45 -04004812
Alan Coxa6b2c5d2006-05-22 16:59:59 +01004813 ap->ops->data_xfer(qc->dev, qc->cdb, qc->dev->cdb_len, 1);
Albert Leec71c1852005-10-04 06:03:45 -04004814 ata_altstatus(ap); /* flush */
4815
4816 switch (qc->tf.protocol) {
4817 case ATA_PROT_ATAPI:
4818 ap->hsm_task_state = HSM_ST;
4819 break;
4820 case ATA_PROT_ATAPI_NODATA:
4821 ap->hsm_task_state = HSM_ST_LAST;
4822 break;
4823 case ATA_PROT_ATAPI_DMA:
4824 ap->hsm_task_state = HSM_ST_LAST;
4825 /* initiate bmdma */
4826 ap->ops->bmdma_start(qc);
4827 break;
4828 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829}
4830
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004831/**
4832 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
4833 * @qc: Command on going
4834 * @bytes: number of bytes
4835 *
4836 * Transfer Transfer data from/to the ATAPI device.
4837 *
4838 * LOCKING:
4839 * Inherited from caller.
4840 *
4841 */
4842
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
4844{
4845 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004846 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847 struct ata_port *ap = qc->ap;
4848 struct page *page;
4849 unsigned char *buf;
4850 unsigned int offset, count;
4851
Albert Lee563a6e12005-08-12 14:17:50 +08004852 if (qc->curbytes + bytes >= qc->nbytes)
Albert Lee14be71f2005-09-27 17:36:35 +08004853 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854
4855next_sg:
Albert Lee563a6e12005-08-12 14:17:50 +08004856 if (unlikely(qc->cursg >= qc->n_elem)) {
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04004857 /*
Albert Lee563a6e12005-08-12 14:17:50 +08004858 * The end of qc->sg is reached and the device expects
4859 * more data to transfer. In order not to overrun qc->sg
4860 * and fulfill length specified in the byte count register,
4861 * - for read case, discard trailing data from the device
4862 * - for write case, padding zero data to the device
4863 */
4864 u16 pad_buf[1] = { 0 };
4865 unsigned int words = bytes >> 1;
4866 unsigned int i;
4867
4868 if (words) /* warning if bytes > 1 */
Tejun Heof15a1da2006-05-15 20:57:56 +09004869 ata_dev_printk(qc->dev, KERN_WARNING,
4870 "%u bytes trailing data\n", bytes);
Albert Lee563a6e12005-08-12 14:17:50 +08004871
4872 for (i = 0; i < words; i++)
Alan Coxa6b2c5d2006-05-22 16:59:59 +01004873 ap->ops->data_xfer(qc->dev, (unsigned char*)pad_buf, 2, do_write);
Albert Lee563a6e12005-08-12 14:17:50 +08004874
Albert Lee14be71f2005-09-27 17:36:35 +08004875 ap->hsm_task_state = HSM_ST_LAST;
Albert Lee563a6e12005-08-12 14:17:50 +08004876 return;
4877 }
4878
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004879 sg = &qc->__sg[qc->cursg];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004880
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881 page = sg->page;
4882 offset = sg->offset + qc->cursg_ofs;
4883
4884 /* get the current page and offset */
4885 page = nth_page(page, (offset >> PAGE_SHIFT));
4886 offset %= PAGE_SIZE;
4887
Albert Lee6952df02005-06-06 15:56:03 +08004888 /* don't overrun current sg */
Albert Lee32529e02005-05-26 03:49:42 -04004889 count = min(sg->length - qc->cursg_ofs, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890
4891 /* don't cross page boundaries */
4892 count = min(count, (unsigned int)PAGE_SIZE - offset);
4893
Albert Lee7282aa42005-10-09 09:46:07 -04004894 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
4895
Albert Lee91b8b312005-10-09 09:48:44 -04004896 if (PageHighMem(page)) {
4897 unsigned long flags;
Albert Lee7282aa42005-10-09 09:46:07 -04004898
Alan Coxa6b2c5d2006-05-22 16:59:59 +01004899 /* FIXME: use bounce buffer */
Albert Lee91b8b312005-10-09 09:48:44 -04004900 local_irq_save(flags);
4901 buf = kmap_atomic(page, KM_IRQ0);
Albert Lee083958d2005-10-09 09:47:31 -04004902
Albert Lee91b8b312005-10-09 09:48:44 -04004903 /* do the actual data transfer */
Alan Coxa6b2c5d2006-05-22 16:59:59 +01004904 ap->ops->data_xfer(qc->dev, buf + offset, count, do_write);
Albert Lee91b8b312005-10-09 09:48:44 -04004905
4906 kunmap_atomic(buf, KM_IRQ0);
4907 local_irq_restore(flags);
4908 } else {
4909 buf = page_address(page);
Alan Coxa6b2c5d2006-05-22 16:59:59 +01004910 ap->ops->data_xfer(qc->dev, buf + offset, count, do_write);
Albert Lee91b8b312005-10-09 09:48:44 -04004911 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912
4913 bytes -= count;
4914 qc->curbytes += count;
4915 qc->cursg_ofs += count;
4916
Albert Lee32529e02005-05-26 03:49:42 -04004917 if (qc->cursg_ofs == sg->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004918 qc->cursg++;
4919 qc->cursg_ofs = 0;
4920 }
4921
Albert Lee563a6e12005-08-12 14:17:50 +08004922 if (bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923 goto next_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924}
4925
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004926/**
4927 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
4928 * @qc: Command on going
4929 *
4930 * Transfer Transfer data from/to the ATAPI device.
4931 *
4932 * LOCKING:
4933 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004934 */
4935
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936static void atapi_pio_bytes(struct ata_queued_cmd *qc)
4937{
4938 struct ata_port *ap = qc->ap;
4939 struct ata_device *dev = qc->dev;
4940 unsigned int ireason, bc_lo, bc_hi, bytes;
4941 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
4942
Albert Leeeec4c3f2006-05-18 17:51:10 +08004943 /* Abuse qc->result_tf for temp storage of intermediate TF
4944 * here to save some kernel stack usage.
4945 * For normal completion, qc->result_tf is not relevant. For
4946 * error, qc->result_tf is later overwritten by ata_qc_complete().
4947 * So, the correctness of qc->result_tf is not affected.
4948 */
4949 ap->ops->tf_read(ap, &qc->result_tf);
4950 ireason = qc->result_tf.nsect;
4951 bc_lo = qc->result_tf.lbam;
4952 bc_hi = qc->result_tf.lbah;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953 bytes = (bc_hi << 8) | bc_lo;
4954
4955 /* shall be cleared to zero, indicating xfer of data */
4956 if (ireason & (1 << 0))
4957 goto err_out;
4958
4959 /* make sure transfer direction matches expected */
4960 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
4961 if (do_write != i_write)
4962 goto err_out;
4963
Tejun Heo44877b42007-02-21 01:06:51 +09004964 VPRINTK("ata%u: xfering %d bytes\n", ap->print_id, bytes);
Albert Lee312f7da2005-09-27 17:38:03 +08004965
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966 __atapi_pio_bytes(qc, bytes);
Albert Lee4cc980b2007-08-15 03:19:45 -04004967 ata_altstatus(ap); /* flush */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004968
4969 return;
4970
4971err_out:
Tejun Heof15a1da2006-05-15 20:57:56 +09004972 ata_dev_printk(dev, KERN_INFO, "ATAPI check failed\n");
Tejun Heo11a56d22006-01-23 13:09:36 +09004973 qc->err_mask |= AC_ERR_HSM;
Albert Lee14be71f2005-09-27 17:36:35 +08004974 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975}
4976
4977/**
Albert Leec234fb02006-03-25 17:58:38 +08004978 * ata_hsm_ok_in_wq - Check if the qc can be handled in the workqueue.
4979 * @ap: the target ata_port
4980 * @qc: qc on going
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981 *
Albert Leec234fb02006-03-25 17:58:38 +08004982 * RETURNS:
4983 * 1 if ok in workqueue, 0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004984 */
Albert Leec234fb02006-03-25 17:58:38 +08004985
4986static inline int ata_hsm_ok_in_wq(struct ata_port *ap, struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987{
Albert Leec234fb02006-03-25 17:58:38 +08004988 if (qc->tf.flags & ATA_TFLAG_POLLING)
4989 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990
Albert Leec234fb02006-03-25 17:58:38 +08004991 if (ap->hsm_task_state == HSM_ST_FIRST) {
4992 if (qc->tf.protocol == ATA_PROT_PIO &&
4993 (qc->tf.flags & ATA_TFLAG_WRITE))
4994 return 1;
4995
4996 if (is_atapi_taskfile(&qc->tf) &&
4997 !(qc->dev->flags & ATA_DFLAG_CDB_INTR))
4998 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999 }
5000
Albert Leec234fb02006-03-25 17:58:38 +08005001 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002}
5003
Albert Leec234fb02006-03-25 17:58:38 +08005004/**
Tejun Heoc17ea202006-05-15 20:59:29 +09005005 * ata_hsm_qc_complete - finish a qc running on standard HSM
5006 * @qc: Command to complete
5007 * @in_wq: 1 if called from workqueue, 0 otherwise
5008 *
5009 * Finish @qc which is running on standard HSM.
5010 *
5011 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04005012 * If @in_wq is zero, spin_lock_irqsave(host lock).
Tejun Heoc17ea202006-05-15 20:59:29 +09005013 * Otherwise, none on entry and grabs host lock.
5014 */
5015static void ata_hsm_qc_complete(struct ata_queued_cmd *qc, int in_wq)
5016{
5017 struct ata_port *ap = qc->ap;
5018 unsigned long flags;
5019
5020 if (ap->ops->error_handler) {
5021 if (in_wq) {
Jeff Garzikba6a1302006-06-22 23:46:10 -04005022 spin_lock_irqsave(ap->lock, flags);
Tejun Heoc17ea202006-05-15 20:59:29 +09005023
Jeff Garzikcca39742006-08-24 03:19:22 -04005024 /* EH might have kicked in while host lock is
5025 * released.
Tejun Heoc17ea202006-05-15 20:59:29 +09005026 */
5027 qc = ata_qc_from_tag(ap, qc->tag);
5028 if (qc) {
5029 if (likely(!(qc->err_mask & AC_ERR_HSM))) {
Akira Iguchi83625002007-01-26 16:27:32 +09005030 ap->ops->irq_on(ap);
Tejun Heoc17ea202006-05-15 20:59:29 +09005031 ata_qc_complete(qc);
5032 } else
5033 ata_port_freeze(ap);
5034 }
5035
Jeff Garzikba6a1302006-06-22 23:46:10 -04005036 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoc17ea202006-05-15 20:59:29 +09005037 } else {
5038 if (likely(!(qc->err_mask & AC_ERR_HSM)))
5039 ata_qc_complete(qc);
5040 else
5041 ata_port_freeze(ap);
5042 }
5043 } else {
5044 if (in_wq) {
Jeff Garzikba6a1302006-06-22 23:46:10 -04005045 spin_lock_irqsave(ap->lock, flags);
Akira Iguchi83625002007-01-26 16:27:32 +09005046 ap->ops->irq_on(ap);
Tejun Heoc17ea202006-05-15 20:59:29 +09005047 ata_qc_complete(qc);
Jeff Garzikba6a1302006-06-22 23:46:10 -04005048 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoc17ea202006-05-15 20:59:29 +09005049 } else
5050 ata_qc_complete(qc);
5051 }
5052}
5053
5054/**
Albert Leebb5cb292006-03-25 17:48:02 +08005055 * ata_hsm_move - move the HSM to the next state.
5056 * @ap: the target ata_port
5057 * @qc: qc on going
5058 * @status: current device status
5059 * @in_wq: 1 if called from workqueue, 0 otherwise
5060 *
5061 * RETURNS:
5062 * 1 when poll next status needed, 0 otherwise.
5063 */
Tejun Heo9a1004d2006-05-31 18:27:52 +09005064int ata_hsm_move(struct ata_port *ap, struct ata_queued_cmd *qc,
5065 u8 status, int in_wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066{
Albert Leebb5cb292006-03-25 17:48:02 +08005067 unsigned long flags = 0;
5068 int poll_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069
Albert Lee6912ccd2006-03-25 17:45:49 +08005070 WARN_ON((qc->flags & ATA_QCFLAG_ACTIVE) == 0);
Albert Lee0565c262006-02-13 18:55:25 +08005071
Albert Leebb5cb292006-03-25 17:48:02 +08005072 /* Make sure ata_qc_issue_prot() does not throw things
5073 * like DMA polling into the workqueue. Notice that
5074 * in_wq is not equivalent to (qc->tf.flags & ATA_TFLAG_POLLING).
Albert Lee1c848982005-12-05 15:40:15 +08005075 */
Albert Leec234fb02006-03-25 17:58:38 +08005076 WARN_ON(in_wq != ata_hsm_ok_in_wq(ap, qc));
Albert Lee1c848982005-12-05 15:40:15 +08005077
Albert Leee2cec772006-03-25 17:43:49 +08005078fsm_start:
Albert Lee999bb6f2006-03-25 18:07:48 +08005079 DPRINTK("ata%u: protocol %d task_state %d (dev_stat 0x%X)\n",
Tejun Heo44877b42007-02-21 01:06:51 +09005080 ap->print_id, qc->tf.protocol, ap->hsm_task_state, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081
Albert Leee2cec772006-03-25 17:43:49 +08005082 switch (ap->hsm_task_state) {
5083 case HSM_ST_FIRST:
Albert Leebb5cb292006-03-25 17:48:02 +08005084 /* Send first data block or PACKET CDB */
5085
5086 /* If polling, we will stay in the work queue after
5087 * sending the data. Otherwise, interrupt handler
5088 * takes over after sending the data.
5089 */
5090 poll_next = (qc->tf.flags & ATA_TFLAG_POLLING);
5091
Albert Leee2cec772006-03-25 17:43:49 +08005092 /* check device status */
Albert Lee3655d1d2006-05-19 11:43:04 +08005093 if (unlikely((status & ATA_DRQ) == 0)) {
5094 /* handle BSY=0, DRQ=0 as error */
5095 if (likely(status & (ATA_ERR | ATA_DF)))
5096 /* device stops HSM for abort/error */
5097 qc->err_mask |= AC_ERR_DEV;
5098 else
5099 /* HSM violation. Let EH handle this */
5100 qc->err_mask |= AC_ERR_HSM;
5101
Albert Leee2cec772006-03-25 17:43:49 +08005102 ap->hsm_task_state = HSM_ST_ERR;
5103 goto fsm_start;
5104 }
5105
Albert Lee71601952006-03-25 18:11:12 +08005106 /* Device should not ask for data transfer (DRQ=1)
5107 * when it finds something wrong.
Albert Leeeee6c322006-04-01 17:38:43 +08005108 * We ignore DRQ here and stop the HSM by
5109 * changing hsm_task_state to HSM_ST_ERR and
5110 * let the EH abort the command or reset the device.
Albert Lee71601952006-03-25 18:11:12 +08005111 */
5112 if (unlikely(status & (ATA_ERR | ATA_DF))) {
Tejun Heo44877b42007-02-21 01:06:51 +09005113 ata_port_printk(ap, KERN_WARNING, "DRQ=1 with device "
5114 "error, dev_stat 0x%X\n", status);
Albert Lee3655d1d2006-05-19 11:43:04 +08005115 qc->err_mask |= AC_ERR_HSM;
Albert Leeeee6c322006-04-01 17:38:43 +08005116 ap->hsm_task_state = HSM_ST_ERR;
5117 goto fsm_start;
Albert Lee71601952006-03-25 18:11:12 +08005118 }
5119
Albert Leebb5cb292006-03-25 17:48:02 +08005120 /* Send the CDB (atapi) or the first data block (ata pio out).
5121 * During the state transition, interrupt handler shouldn't
5122 * be invoked before the data transfer is complete and
5123 * hsm_task_state is changed. Hence, the following locking.
5124 */
5125 if (in_wq)
Jeff Garzikba6a1302006-06-22 23:46:10 -04005126 spin_lock_irqsave(ap->lock, flags);
Albert Leee2cec772006-03-25 17:43:49 +08005127
Albert Leebb5cb292006-03-25 17:48:02 +08005128 if (qc->tf.protocol == ATA_PROT_PIO) {
5129 /* PIO data out protocol.
5130 * send first data block.
5131 */
5132
5133 /* ata_pio_sectors() might change the state
5134 * to HSM_ST_LAST. so, the state is changed here
5135 * before ata_pio_sectors().
5136 */
5137 ap->hsm_task_state = HSM_ST;
5138 ata_pio_sectors(qc);
Albert Leebb5cb292006-03-25 17:48:02 +08005139 } else
5140 /* send CDB */
5141 atapi_send_cdb(ap, qc);
5142
5143 if (in_wq)
Jeff Garzikba6a1302006-06-22 23:46:10 -04005144 spin_unlock_irqrestore(ap->lock, flags);
Albert Leebb5cb292006-03-25 17:48:02 +08005145
5146 /* if polling, ata_pio_task() handles the rest.
5147 * otherwise, interrupt handler takes over from here.
5148 */
Albert Leee2cec772006-03-25 17:43:49 +08005149 break;
5150
5151 case HSM_ST:
5152 /* complete command or read/write the data register */
5153 if (qc->tf.protocol == ATA_PROT_ATAPI) {
5154 /* ATAPI PIO protocol */
5155 if ((status & ATA_DRQ) == 0) {
Albert Lee3655d1d2006-05-19 11:43:04 +08005156 /* No more data to transfer or device error.
5157 * Device error will be tagged in HSM_ST_LAST.
5158 */
Albert Leee2cec772006-03-25 17:43:49 +08005159 ap->hsm_task_state = HSM_ST_LAST;
5160 goto fsm_start;
5161 }
5162
Albert Lee71601952006-03-25 18:11:12 +08005163 /* Device should not ask for data transfer (DRQ=1)
5164 * when it finds something wrong.
Albert Leeeee6c322006-04-01 17:38:43 +08005165 * We ignore DRQ here and stop the HSM by
5166 * changing hsm_task_state to HSM_ST_ERR and
5167 * let the EH abort the command or reset the device.
Albert Lee71601952006-03-25 18:11:12 +08005168 */
5169 if (unlikely(status & (ATA_ERR | ATA_DF))) {
Tejun Heo44877b42007-02-21 01:06:51 +09005170 ata_port_printk(ap, KERN_WARNING, "DRQ=1 with "
5171 "device error, dev_stat 0x%X\n",
5172 status);
Albert Lee3655d1d2006-05-19 11:43:04 +08005173 qc->err_mask |= AC_ERR_HSM;
Albert Leeeee6c322006-04-01 17:38:43 +08005174 ap->hsm_task_state = HSM_ST_ERR;
5175 goto fsm_start;
Albert Lee71601952006-03-25 18:11:12 +08005176 }
5177
Albert Leee2cec772006-03-25 17:43:49 +08005178 atapi_pio_bytes(qc);
5179
5180 if (unlikely(ap->hsm_task_state == HSM_ST_ERR))
5181 /* bad ireason reported by device */
5182 goto fsm_start;
5183
5184 } else {
5185 /* ATA PIO protocol */
5186 if (unlikely((status & ATA_DRQ) == 0)) {
5187 /* handle BSY=0, DRQ=0 as error */
Albert Lee3655d1d2006-05-19 11:43:04 +08005188 if (likely(status & (ATA_ERR | ATA_DF)))
5189 /* device stops HSM for abort/error */
5190 qc->err_mask |= AC_ERR_DEV;
5191 else
Tejun Heo55a8e2c2006-11-10 18:08:10 +09005192 /* HSM violation. Let EH handle this.
5193 * Phantom devices also trigger this
5194 * condition. Mark hint.
5195 */
5196 qc->err_mask |= AC_ERR_HSM |
5197 AC_ERR_NODEV_HINT;
Albert Lee3655d1d2006-05-19 11:43:04 +08005198
Albert Leee2cec772006-03-25 17:43:49 +08005199 ap->hsm_task_state = HSM_ST_ERR;
5200 goto fsm_start;
5201 }
5202
Albert Leeeee6c322006-04-01 17:38:43 +08005203 /* For PIO reads, some devices may ask for
5204 * data transfer (DRQ=1) alone with ERR=1.
5205 * We respect DRQ here and transfer one
5206 * block of junk data before changing the
5207 * hsm_task_state to HSM_ST_ERR.
5208 *
5209 * For PIO writes, ERR=1 DRQ=1 doesn't make
5210 * sense since the data block has been
5211 * transferred to the device.
Albert Lee71601952006-03-25 18:11:12 +08005212 */
5213 if (unlikely(status & (ATA_ERR | ATA_DF))) {
Albert Lee71601952006-03-25 18:11:12 +08005214 /* data might be corrputed */
5215 qc->err_mask |= AC_ERR_DEV;
Albert Leeeee6c322006-04-01 17:38:43 +08005216
5217 if (!(qc->tf.flags & ATA_TFLAG_WRITE)) {
5218 ata_pio_sectors(qc);
Albert Leeeee6c322006-04-01 17:38:43 +08005219 status = ata_wait_idle(ap);
5220 }
5221
Albert Lee3655d1d2006-05-19 11:43:04 +08005222 if (status & (ATA_BUSY | ATA_DRQ))
5223 qc->err_mask |= AC_ERR_HSM;
5224
Albert Leeeee6c322006-04-01 17:38:43 +08005225 /* ata_pio_sectors() might change the
5226 * state to HSM_ST_LAST. so, the state
5227 * is changed after ata_pio_sectors().
5228 */
5229 ap->hsm_task_state = HSM_ST_ERR;
5230 goto fsm_start;
Albert Lee71601952006-03-25 18:11:12 +08005231 }
5232
Albert Leee2cec772006-03-25 17:43:49 +08005233 ata_pio_sectors(qc);
5234
5235 if (ap->hsm_task_state == HSM_ST_LAST &&
5236 (!(qc->tf.flags & ATA_TFLAG_WRITE))) {
5237 /* all data read */
Albert Lee52a32202006-03-25 18:18:15 +08005238 status = ata_wait_idle(ap);
Albert Leee2cec772006-03-25 17:43:49 +08005239 goto fsm_start;
5240 }
5241 }
5242
Albert Leebb5cb292006-03-25 17:48:02 +08005243 poll_next = 1;
Albert Leee2cec772006-03-25 17:43:49 +08005244 break;
5245
5246 case HSM_ST_LAST:
Albert Lee6912ccd2006-03-25 17:45:49 +08005247 if (unlikely(!ata_ok(status))) {
5248 qc->err_mask |= __ac_err_mask(status);
Albert Leee2cec772006-03-25 17:43:49 +08005249 ap->hsm_task_state = HSM_ST_ERR;
5250 goto fsm_start;
5251 }
5252
5253 /* no more data to transfer */
Albert Lee4332a772006-04-03 17:43:24 +08005254 DPRINTK("ata%u: dev %u command complete, drv_stat 0x%x\n",
Tejun Heo44877b42007-02-21 01:06:51 +09005255 ap->print_id, qc->dev->devno, status);
Albert Leee2cec772006-03-25 17:43:49 +08005256
Albert Lee6912ccd2006-03-25 17:45:49 +08005257 WARN_ON(qc->err_mask);
5258
Albert Leee2cec772006-03-25 17:43:49 +08005259 ap->hsm_task_state = HSM_ST_IDLE;
5260
5261 /* complete taskfile transaction */
Tejun Heoc17ea202006-05-15 20:59:29 +09005262 ata_hsm_qc_complete(qc, in_wq);
Albert Leebb5cb292006-03-25 17:48:02 +08005263
5264 poll_next = 0;
Albert Leee2cec772006-03-25 17:43:49 +08005265 break;
5266
5267 case HSM_ST_ERR:
Albert Leee2cec772006-03-25 17:43:49 +08005268 /* make sure qc->err_mask is available to
5269 * know what's wrong and recover
5270 */
5271 WARN_ON(qc->err_mask == 0);
5272
5273 ap->hsm_task_state = HSM_ST_IDLE;
Albert Leebb5cb292006-03-25 17:48:02 +08005274
Albert Lee999bb6f2006-03-25 18:07:48 +08005275 /* complete taskfile transaction */
Tejun Heoc17ea202006-05-15 20:59:29 +09005276 ata_hsm_qc_complete(qc, in_wq);
Albert Leebb5cb292006-03-25 17:48:02 +08005277
5278 poll_next = 0;
Albert Leee2cec772006-03-25 17:43:49 +08005279 break;
5280 default:
Albert Leebb5cb292006-03-25 17:48:02 +08005281 poll_next = 0;
Albert Lee6912ccd2006-03-25 17:45:49 +08005282 BUG();
Albert Leee2cec772006-03-25 17:43:49 +08005283 }
5284
Albert Leebb5cb292006-03-25 17:48:02 +08005285 return poll_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005286}
5287
David Howells65f27f32006-11-22 14:55:48 +00005288static void ata_pio_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005289{
David Howells65f27f32006-11-22 14:55:48 +00005290 struct ata_port *ap =
5291 container_of(work, struct ata_port, port_task.work);
5292 struct ata_queued_cmd *qc = ap->port_task_data;
Albert Leea1af3732006-03-25 17:50:15 +08005293 u8 status;
5294 int poll_next;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04005295
5296fsm_start:
Albert Leea1af3732006-03-25 17:50:15 +08005297 WARN_ON(ap->hsm_task_state == HSM_ST_IDLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005298
Albert Leea1af3732006-03-25 17:50:15 +08005299 /*
5300 * This is purely heuristic. This is a fast path.
5301 * Sometimes when we enter, BSY will be cleared in
5302 * a chk-status or two. If not, the drive is probably seeking
5303 * or something. Snooze for a couple msecs, then
5304 * chk-status again. If still busy, queue delayed work.
5305 */
5306 status = ata_busy_wait(ap, ATA_BUSY, 5);
5307 if (status & ATA_BUSY) {
5308 msleep(2);
5309 status = ata_busy_wait(ap, ATA_BUSY, 10);
5310 if (status & ATA_BUSY) {
Albert Lee31ce6da2006-04-03 18:31:44 +08005311 ata_port_queue_task(ap, ata_pio_task, qc, ATA_SHORT_PAUSE);
Albert Leea1af3732006-03-25 17:50:15 +08005312 return;
5313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005314 }
5315
Albert Leea1af3732006-03-25 17:50:15 +08005316 /* move the HSM */
5317 poll_next = ata_hsm_move(ap, qc, status, 1);
5318
5319 /* another command or interrupt handler
5320 * may be running at this point.
5321 */
5322 if (poll_next)
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04005323 goto fsm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005324}
5325
Linus Torvalds1da177e2005-04-16 15:20:36 -07005326/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07005327 * ata_qc_new - Request an available ATA command, for queueing
5328 * @ap: Port associated with device @dev
5329 * @dev: Device from whom we request an available command structure
5330 *
5331 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04005332 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005333 */
5334
5335static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
5336{
5337 struct ata_queued_cmd *qc = NULL;
5338 unsigned int i;
5339
Tejun Heoe3180492006-05-15 20:58:09 +09005340 /* no command while frozen */
Tejun Heob51e9e52006-06-29 01:29:30 +09005341 if (unlikely(ap->pflags & ATA_PFLAG_FROZEN))
Tejun Heoe3180492006-05-15 20:58:09 +09005342 return NULL;
5343
Tejun Heo2ab7db12006-05-15 20:58:02 +09005344 /* the last tag is reserved for internal command. */
5345 for (i = 0; i < ATA_MAX_QUEUE - 1; i++)
Tejun Heo6cec4a32006-05-15 21:03:41 +09005346 if (!test_and_set_bit(i, &ap->qc_allocated)) {
Tejun Heof69499f2006-05-15 20:58:03 +09005347 qc = __ata_qc_from_tag(ap, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005348 break;
5349 }
5350
5351 if (qc)
5352 qc->tag = i;
5353
5354 return qc;
5355}
5356
5357/**
5358 * ata_qc_new_init - Request an available ATA command, and initialize it
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359 * @dev: Device from whom we request an available command structure
5360 *
5361 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04005362 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363 */
5364
Tejun Heo3373efd2006-05-15 20:57:53 +09005365struct ata_queued_cmd *ata_qc_new_init(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005366{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005367 struct ata_port *ap = dev->link->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005368 struct ata_queued_cmd *qc;
5369
5370 qc = ata_qc_new(ap);
5371 if (qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005372 qc->scsicmd = NULL;
5373 qc->ap = ap;
5374 qc->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005375
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05005376 ata_qc_reinit(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005377 }
5378
5379 return qc;
5380}
5381
Linus Torvalds1da177e2005-04-16 15:20:36 -07005382/**
5383 * ata_qc_free - free unused ata_queued_cmd
5384 * @qc: Command to complete
5385 *
5386 * Designed to free unused ata_queued_cmd object
5387 * in case something prevents using it.
5388 *
5389 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04005390 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005391 */
5392void ata_qc_free(struct ata_queued_cmd *qc)
5393{
Tejun Heo4ba946e2006-01-23 13:09:36 +09005394 struct ata_port *ap = qc->ap;
5395 unsigned int tag;
5396
Tejun Heoa46314742006-02-11 19:11:13 +09005397 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005398
Tejun Heo4ba946e2006-01-23 13:09:36 +09005399 qc->flags = 0;
5400 tag = qc->tag;
5401 if (likely(ata_tag_valid(tag))) {
Tejun Heo4ba946e2006-01-23 13:09:36 +09005402 qc->tag = ATA_TAG_POISON;
Tejun Heo6cec4a32006-05-15 21:03:41 +09005403 clear_bit(tag, &ap->qc_allocated);
Tejun Heo4ba946e2006-01-23 13:09:36 +09005404 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405}
5406
Tejun Heo76014422006-02-11 15:13:49 +09005407void __ata_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408{
Tejun Heodedaf2b2006-05-15 21:03:43 +09005409 struct ata_port *ap = qc->ap;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005410 struct ata_link *link = qc->dev->link;
Tejun Heodedaf2b2006-05-15 21:03:43 +09005411
Tejun Heoa46314742006-02-11 19:11:13 +09005412 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
5413 WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005414
5415 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
5416 ata_sg_clean(qc);
5417
Tejun Heo7401abf2006-05-15 20:57:32 +09005418 /* command should be marked inactive atomically with qc completion */
Tejun Heoda917d62007-09-23 13:14:12 +09005419 if (qc->tf.protocol == ATA_PROT_NCQ) {
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005420 link->sactive &= ~(1 << qc->tag);
Tejun Heoda917d62007-09-23 13:14:12 +09005421 if (!link->sactive)
5422 ap->nr_active_links--;
5423 } else {
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005424 link->active_tag = ATA_TAG_POISON;
Tejun Heoda917d62007-09-23 13:14:12 +09005425 ap->nr_active_links--;
5426 }
5427
5428 /* clear exclusive status */
5429 if (unlikely(qc->flags & ATA_QCFLAG_CLEAR_EXCL &&
5430 ap->excl_link == link))
5431 ap->excl_link = NULL;
Tejun Heo7401abf2006-05-15 20:57:32 +09005432
Albert Lee3f3791d2005-08-16 14:25:38 +08005433 /* atapi: mark qc as inactive to prevent the interrupt handler
5434 * from completing the command twice later, before the error handler
5435 * is called. (when rc != 0 and atapi request sense is needed)
5436 */
5437 qc->flags &= ~ATA_QCFLAG_ACTIVE;
Tejun Heodedaf2b2006-05-15 21:03:43 +09005438 ap->qc_active &= ~(1 << qc->tag);
Albert Lee3f3791d2005-08-16 14:25:38 +08005439
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440 /* call completion callback */
Tejun Heo77853bf2006-01-23 13:09:36 +09005441 qc->complete_fn(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005442}
5443
Tejun Heo39599a52006-11-14 22:37:35 +09005444static void fill_result_tf(struct ata_queued_cmd *qc)
5445{
5446 struct ata_port *ap = qc->ap;
5447
Tejun Heo39599a52006-11-14 22:37:35 +09005448 qc->result_tf.flags = qc->tf.flags;
Mark Lord4742d542007-04-02 16:20:35 -04005449 ap->ops->tf_read(ap, &qc->result_tf);
Tejun Heo39599a52006-11-14 22:37:35 +09005450}
5451
Tejun Heof686bcb2006-05-15 20:58:05 +09005452/**
5453 * ata_qc_complete - Complete an active ATA command
5454 * @qc: Command to complete
5455 * @err_mask: ATA Status register contents
5456 *
5457 * Indicate to the mid and upper layers that an ATA
5458 * command has completed, with either an ok or not-ok status.
5459 *
5460 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04005461 * spin_lock_irqsave(host lock)
Tejun Heof686bcb2006-05-15 20:58:05 +09005462 */
5463void ata_qc_complete(struct ata_queued_cmd *qc)
5464{
5465 struct ata_port *ap = qc->ap;
5466
5467 /* XXX: New EH and old EH use different mechanisms to
5468 * synchronize EH with regular execution path.
5469 *
5470 * In new EH, a failed qc is marked with ATA_QCFLAG_FAILED.
5471 * Normal execution path is responsible for not accessing a
5472 * failed qc. libata core enforces the rule by returning NULL
5473 * from ata_qc_from_tag() for failed qcs.
5474 *
5475 * Old EH depends on ata_qc_complete() nullifying completion
5476 * requests if ATA_QCFLAG_EH_SCHEDULED is set. Old EH does
5477 * not synchronize with interrupt handler. Only PIO task is
5478 * taken care of.
5479 */
5480 if (ap->ops->error_handler) {
Tejun Heob51e9e52006-06-29 01:29:30 +09005481 WARN_ON(ap->pflags & ATA_PFLAG_FROZEN);
Tejun Heof686bcb2006-05-15 20:58:05 +09005482
5483 if (unlikely(qc->err_mask))
5484 qc->flags |= ATA_QCFLAG_FAILED;
5485
5486 if (unlikely(qc->flags & ATA_QCFLAG_FAILED)) {
5487 if (!ata_tag_internal(qc->tag)) {
5488 /* always fill result TF for failed qc */
Tejun Heo39599a52006-11-14 22:37:35 +09005489 fill_result_tf(qc);
Tejun Heof686bcb2006-05-15 20:58:05 +09005490 ata_qc_schedule_eh(qc);
5491 return;
5492 }
5493 }
5494
5495 /* read result TF if requested */
5496 if (qc->flags & ATA_QCFLAG_RESULT_TF)
Tejun Heo39599a52006-11-14 22:37:35 +09005497 fill_result_tf(qc);
Tejun Heof686bcb2006-05-15 20:58:05 +09005498
5499 __ata_qc_complete(qc);
5500 } else {
5501 if (qc->flags & ATA_QCFLAG_EH_SCHEDULED)
5502 return;
5503
5504 /* read result TF if failed or requested */
5505 if (qc->err_mask || qc->flags & ATA_QCFLAG_RESULT_TF)
Tejun Heo39599a52006-11-14 22:37:35 +09005506 fill_result_tf(qc);
Tejun Heof686bcb2006-05-15 20:58:05 +09005507
5508 __ata_qc_complete(qc);
5509 }
5510}
5511
Tejun Heodedaf2b2006-05-15 21:03:43 +09005512/**
5513 * ata_qc_complete_multiple - Complete multiple qcs successfully
5514 * @ap: port in question
5515 * @qc_active: new qc_active mask
5516 * @finish_qc: LLDD callback invoked before completing a qc
5517 *
5518 * Complete in-flight commands. This functions is meant to be
5519 * called from low-level driver's interrupt routine to complete
5520 * requests normally. ap->qc_active and @qc_active is compared
5521 * and commands are completed accordingly.
5522 *
5523 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04005524 * spin_lock_irqsave(host lock)
Tejun Heodedaf2b2006-05-15 21:03:43 +09005525 *
5526 * RETURNS:
5527 * Number of completed commands on success, -errno otherwise.
5528 */
5529int ata_qc_complete_multiple(struct ata_port *ap, u32 qc_active,
5530 void (*finish_qc)(struct ata_queued_cmd *))
5531{
5532 int nr_done = 0;
5533 u32 done_mask;
5534 int i;
5535
5536 done_mask = ap->qc_active ^ qc_active;
5537
5538 if (unlikely(done_mask & qc_active)) {
5539 ata_port_printk(ap, KERN_ERR, "illegal qc_active transition "
5540 "(%08x->%08x)\n", ap->qc_active, qc_active);
5541 return -EINVAL;
5542 }
5543
5544 for (i = 0; i < ATA_MAX_QUEUE; i++) {
5545 struct ata_queued_cmd *qc;
5546
5547 if (!(done_mask & (1 << i)))
5548 continue;
5549
5550 if ((qc = ata_qc_from_tag(ap, i))) {
5551 if (finish_qc)
5552 finish_qc(qc);
5553 ata_qc_complete(qc);
5554 nr_done++;
5555 }
5556 }
5557
5558 return nr_done;
5559}
5560
Linus Torvalds1da177e2005-04-16 15:20:36 -07005561static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
5562{
5563 struct ata_port *ap = qc->ap;
5564
5565 switch (qc->tf.protocol) {
Tejun Heo3dc1d882006-05-15 21:03:45 +09005566 case ATA_PROT_NCQ:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005567 case ATA_PROT_DMA:
5568 case ATA_PROT_ATAPI_DMA:
5569 return 1;
5570
5571 case ATA_PROT_ATAPI:
5572 case ATA_PROT_PIO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005573 if (ap->flags & ATA_FLAG_PIO_DMA)
5574 return 1;
5575
5576 /* fall through */
5577
5578 default:
5579 return 0;
5580 }
5581
5582 /* never reached */
5583}
5584
5585/**
5586 * ata_qc_issue - issue taskfile to device
5587 * @qc: command to issue to device
5588 *
5589 * Prepare an ATA command to submission to device.
5590 * This includes mapping the data into a DMA-able
5591 * area, filling in the S/G table, and finally
5592 * writing the taskfile to hardware, starting the command.
5593 *
5594 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04005595 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005596 */
Tejun Heo8e0e6942006-03-31 20:41:11 +09005597void ata_qc_issue(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005598{
5599 struct ata_port *ap = qc->ap;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005600 struct ata_link *link = qc->dev->link;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005601
Tejun Heodedaf2b2006-05-15 21:03:43 +09005602 /* Make sure only one non-NCQ command is outstanding. The
5603 * check is skipped for old EH because it reuses active qc to
5604 * request ATAPI sense.
5605 */
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005606 WARN_ON(ap->ops->error_handler && ata_tag_valid(link->active_tag));
Tejun Heodedaf2b2006-05-15 21:03:43 +09005607
5608 if (qc->tf.protocol == ATA_PROT_NCQ) {
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005609 WARN_ON(link->sactive & (1 << qc->tag));
Tejun Heoda917d62007-09-23 13:14:12 +09005610
5611 if (!link->sactive)
5612 ap->nr_active_links++;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005613 link->sactive |= 1 << qc->tag;
Tejun Heodedaf2b2006-05-15 21:03:43 +09005614 } else {
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005615 WARN_ON(link->sactive);
Tejun Heoda917d62007-09-23 13:14:12 +09005616
5617 ap->nr_active_links++;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005618 link->active_tag = qc->tag;
Tejun Heodedaf2b2006-05-15 21:03:43 +09005619 }
5620
Tejun Heoe4a70e72006-03-31 20:36:47 +09005621 qc->flags |= ATA_QCFLAG_ACTIVE;
Tejun Heodedaf2b2006-05-15 21:03:43 +09005622 ap->qc_active |= 1 << qc->tag;
Tejun Heoe4a70e72006-03-31 20:36:47 +09005623
Linus Torvalds1da177e2005-04-16 15:20:36 -07005624 if (ata_should_dma_map(qc)) {
5625 if (qc->flags & ATA_QCFLAG_SG) {
5626 if (ata_sg_setup(qc))
Tejun Heo8e436af2006-01-23 13:09:36 +09005627 goto sg_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005628 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
5629 if (ata_sg_setup_one(qc))
Tejun Heo8e436af2006-01-23 13:09:36 +09005630 goto sg_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005631 }
5632 } else {
5633 qc->flags &= ~ATA_QCFLAG_DMAMAP;
5634 }
5635
5636 ap->ops->qc_prep(qc);
5637
Tejun Heo8e0e6942006-03-31 20:41:11 +09005638 qc->err_mask |= ap->ops->qc_issue(qc);
5639 if (unlikely(qc->err_mask))
5640 goto err;
5641 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642
Tejun Heo8e436af2006-01-23 13:09:36 +09005643sg_err:
5644 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Tejun Heo8e0e6942006-03-31 20:41:11 +09005645 qc->err_mask |= AC_ERR_SYSTEM;
5646err:
5647 ata_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005648}
5649
5650/**
5651 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
5652 * @qc: command to issue to device
5653 *
5654 * Using various libata functions and hooks, this function
5655 * starts an ATA command. ATA commands are grouped into
5656 * classes called "protocols", and issuing each type of protocol
5657 * is slightly different.
5658 *
Edward Falk0baab862005-06-02 18:17:13 -04005659 * May be used as the qc_issue() entry in ata_port_operations.
5660 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005661 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04005662 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005663 *
5664 * RETURNS:
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09005665 * Zero on success, AC_ERR_* mask on failure
Linus Torvalds1da177e2005-04-16 15:20:36 -07005666 */
5667
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09005668unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005669{
5670 struct ata_port *ap = qc->ap;
5671
Albert Leee50362e2005-09-27 17:39:50 +08005672 /* Use polling pio if the LLD doesn't handle
5673 * interrupt driven pio and atapi CDB interrupt.
5674 */
5675 if (ap->flags & ATA_FLAG_PIO_POLLING) {
5676 switch (qc->tf.protocol) {
5677 case ATA_PROT_PIO:
Albert Leee3472cb2006-12-07 11:37:58 +08005678 case ATA_PROT_NODATA:
Albert Leee50362e2005-09-27 17:39:50 +08005679 case ATA_PROT_ATAPI:
5680 case ATA_PROT_ATAPI_NODATA:
5681 qc->tf.flags |= ATA_TFLAG_POLLING;
5682 break;
5683 case ATA_PROT_ATAPI_DMA:
5684 if (qc->dev->flags & ATA_DFLAG_CDB_INTR)
Albert Lee3a778272006-06-22 13:00:25 +08005685 /* see ata_dma_blacklisted() */
Albert Leee50362e2005-09-27 17:39:50 +08005686 BUG();
5687 break;
5688 default:
5689 break;
5690 }
5691 }
5692
Albert Lee312f7da2005-09-27 17:38:03 +08005693 /* select the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005694 ata_dev_select(ap, qc->dev->devno, 1, 0);
5695
Albert Lee312f7da2005-09-27 17:38:03 +08005696 /* start the command */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005697 switch (qc->tf.protocol) {
5698 case ATA_PROT_NODATA:
Albert Lee312f7da2005-09-27 17:38:03 +08005699 if (qc->tf.flags & ATA_TFLAG_POLLING)
5700 ata_qc_set_polling(qc);
5701
Jeff Garzike5338252005-10-30 21:37:17 -05005702 ata_tf_to_host(ap, &qc->tf);
Albert Lee312f7da2005-09-27 17:38:03 +08005703 ap->hsm_task_state = HSM_ST_LAST;
5704
5705 if (qc->tf.flags & ATA_TFLAG_POLLING)
Albert Lee31ce6da2006-04-03 18:31:44 +08005706 ata_port_queue_task(ap, ata_pio_task, qc, 0);
Albert Lee312f7da2005-09-27 17:38:03 +08005707
Linus Torvalds1da177e2005-04-16 15:20:36 -07005708 break;
5709
5710 case ATA_PROT_DMA:
Jeff Garzik587005d2006-02-11 18:17:32 -05005711 WARN_ON(qc->tf.flags & ATA_TFLAG_POLLING);
Albert Lee312f7da2005-09-27 17:38:03 +08005712
Linus Torvalds1da177e2005-04-16 15:20:36 -07005713 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
5714 ap->ops->bmdma_setup(qc); /* set up bmdma */
5715 ap->ops->bmdma_start(qc); /* initiate bmdma */
Albert Lee312f7da2005-09-27 17:38:03 +08005716 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717 break;
5718
Albert Lee312f7da2005-09-27 17:38:03 +08005719 case ATA_PROT_PIO:
5720 if (qc->tf.flags & ATA_TFLAG_POLLING)
5721 ata_qc_set_polling(qc);
5722
Jeff Garzike5338252005-10-30 21:37:17 -05005723 ata_tf_to_host(ap, &qc->tf);
Albert Lee312f7da2005-09-27 17:38:03 +08005724
Albert Lee54f00382005-09-30 19:14:19 +08005725 if (qc->tf.flags & ATA_TFLAG_WRITE) {
5726 /* PIO data out protocol */
5727 ap->hsm_task_state = HSM_ST_FIRST;
Albert Lee31ce6da2006-04-03 18:31:44 +08005728 ata_port_queue_task(ap, ata_pio_task, qc, 0);
Albert Lee54f00382005-09-30 19:14:19 +08005729
5730 /* always send first data block using
Albert Leee27486d2005-11-01 19:24:49 +08005731 * the ata_pio_task() codepath.
Albert Lee54f00382005-09-30 19:14:19 +08005732 */
Albert Lee312f7da2005-09-27 17:38:03 +08005733 } else {
Albert Lee54f00382005-09-30 19:14:19 +08005734 /* PIO data in protocol */
5735 ap->hsm_task_state = HSM_ST;
Albert Lee312f7da2005-09-27 17:38:03 +08005736
Albert Lee54f00382005-09-30 19:14:19 +08005737 if (qc->tf.flags & ATA_TFLAG_POLLING)
Albert Lee31ce6da2006-04-03 18:31:44 +08005738 ata_port_queue_task(ap, ata_pio_task, qc, 0);
Albert Lee312f7da2005-09-27 17:38:03 +08005739
Albert Lee54f00382005-09-30 19:14:19 +08005740 /* if polling, ata_pio_task() handles the rest.
5741 * otherwise, interrupt handler takes over from here.
5742 */
Albert Lee312f7da2005-09-27 17:38:03 +08005743 }
5744
Linus Torvalds1da177e2005-04-16 15:20:36 -07005745 break;
5746
5747 case ATA_PROT_ATAPI:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005748 case ATA_PROT_ATAPI_NODATA:
Albert Lee312f7da2005-09-27 17:38:03 +08005749 if (qc->tf.flags & ATA_TFLAG_POLLING)
5750 ata_qc_set_polling(qc);
5751
Jeff Garzike5338252005-10-30 21:37:17 -05005752 ata_tf_to_host(ap, &qc->tf);
Jeff Garzikf6ef65e2006-01-27 02:45:00 -05005753
Albert Lee312f7da2005-09-27 17:38:03 +08005754 ap->hsm_task_state = HSM_ST_FIRST;
5755
5756 /* send cdb by polling if no cdb interrupt */
5757 if ((!(qc->dev->flags & ATA_DFLAG_CDB_INTR)) ||
5758 (qc->tf.flags & ATA_TFLAG_POLLING))
Albert Lee31ce6da2006-04-03 18:31:44 +08005759 ata_port_queue_task(ap, ata_pio_task, qc, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005760 break;
5761
5762 case ATA_PROT_ATAPI_DMA:
Jeff Garzik587005d2006-02-11 18:17:32 -05005763 WARN_ON(qc->tf.flags & ATA_TFLAG_POLLING);
Albert Lee312f7da2005-09-27 17:38:03 +08005764
Linus Torvalds1da177e2005-04-16 15:20:36 -07005765 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
5766 ap->ops->bmdma_setup(qc); /* set up bmdma */
Albert Lee312f7da2005-09-27 17:38:03 +08005767 ap->hsm_task_state = HSM_ST_FIRST;
5768
5769 /* send cdb by polling if no cdb interrupt */
5770 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
Albert Lee31ce6da2006-04-03 18:31:44 +08005771 ata_port_queue_task(ap, ata_pio_task, qc, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005772 break;
5773
5774 default:
5775 WARN_ON(1);
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09005776 return AC_ERR_SYSTEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005777 }
5778
5779 return 0;
5780}
5781
5782/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07005783 * ata_host_intr - Handle host interrupt for given (port, task)
5784 * @ap: Port on which interrupt arrived (possibly...)
5785 * @qc: Taskfile currently active in engine
5786 *
5787 * Handle host interrupt for given queued command. Currently,
5788 * only DMA interrupts are handled. All other commands are
5789 * handled via polling with interrupts disabled (nIEN bit).
5790 *
5791 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04005792 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005793 *
5794 * RETURNS:
5795 * One if interrupt was handled, zero if not (shared irq).
5796 */
5797
5798inline unsigned int ata_host_intr (struct ata_port *ap,
5799 struct ata_queued_cmd *qc)
5800{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005801 struct ata_eh_info *ehi = &ap->link.eh_info;
Albert Lee312f7da2005-09-27 17:38:03 +08005802 u8 status, host_stat = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005803
Albert Lee312f7da2005-09-27 17:38:03 +08005804 VPRINTK("ata%u: protocol %d task_state %d\n",
Tejun Heo44877b42007-02-21 01:06:51 +09005805 ap->print_id, qc->tf.protocol, ap->hsm_task_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005806
Albert Lee312f7da2005-09-27 17:38:03 +08005807 /* Check whether we are expecting interrupt in this state */
5808 switch (ap->hsm_task_state) {
5809 case HSM_ST_FIRST:
Albert Lee6912ccd2006-03-25 17:45:49 +08005810 /* Some pre-ATAPI-4 devices assert INTRQ
5811 * at this state when ready to receive CDB.
5812 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005813
Albert Lee312f7da2005-09-27 17:38:03 +08005814 /* Check the ATA_DFLAG_CDB_INTR flag is enough here.
5815 * The flag was turned on only for atapi devices.
5816 * No need to check is_atapi_taskfile(&qc->tf) again.
5817 */
5818 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005819 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005820 break;
Albert Lee312f7da2005-09-27 17:38:03 +08005821 case HSM_ST_LAST:
5822 if (qc->tf.protocol == ATA_PROT_DMA ||
5823 qc->tf.protocol == ATA_PROT_ATAPI_DMA) {
5824 /* check status of DMA engine */
5825 host_stat = ap->ops->bmdma_status(ap);
Tejun Heo44877b42007-02-21 01:06:51 +09005826 VPRINTK("ata%u: host_stat 0x%X\n",
5827 ap->print_id, host_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005828
Albert Lee312f7da2005-09-27 17:38:03 +08005829 /* if it's not our irq... */
5830 if (!(host_stat & ATA_DMA_INTR))
5831 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005832
Albert Lee312f7da2005-09-27 17:38:03 +08005833 /* before we do anything else, clear DMA-Start bit */
5834 ap->ops->bmdma_stop(qc);
Albert Leea4f16612005-12-26 16:40:53 +08005835
5836 if (unlikely(host_stat & ATA_DMA_ERR)) {
5837 /* error when transfering data to/from memory */
5838 qc->err_mask |= AC_ERR_HOST_BUS;
5839 ap->hsm_task_state = HSM_ST_ERR;
5840 }
Albert Lee312f7da2005-09-27 17:38:03 +08005841 }
5842 break;
5843 case HSM_ST:
5844 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005845 default:
5846 goto idle_irq;
5847 }
5848
Albert Lee312f7da2005-09-27 17:38:03 +08005849 /* check altstatus */
5850 status = ata_altstatus(ap);
5851 if (status & ATA_BUSY)
5852 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005853
Albert Lee312f7da2005-09-27 17:38:03 +08005854 /* check main status, clearing INTRQ */
5855 status = ata_chk_status(ap);
5856 if (unlikely(status & ATA_BUSY))
5857 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005858
Albert Lee312f7da2005-09-27 17:38:03 +08005859 /* ack bmdma irq events */
5860 ap->ops->irq_clear(ap);
5861
Albert Leebb5cb292006-03-25 17:48:02 +08005862 ata_hsm_move(ap, qc, status, 0);
Tejun Heoea547632006-11-17 12:06:21 +09005863
5864 if (unlikely(qc->err_mask) && (qc->tf.protocol == ATA_PROT_DMA ||
5865 qc->tf.protocol == ATA_PROT_ATAPI_DMA))
5866 ata_ehi_push_desc(ehi, "BMDMA stat 0x%x", host_stat);
5867
Linus Torvalds1da177e2005-04-16 15:20:36 -07005868 return 1; /* irq handled */
5869
5870idle_irq:
5871 ap->stats.idle_irq++;
5872
5873#ifdef ATA_IRQ_TRAP
5874 if ((ap->stats.idle_irq % 1000) == 0) {
Jeff Garzik6d32d302007-08-15 05:38:46 -04005875 ata_chk_status(ap);
5876 ap->ops->irq_clear(ap);
Tejun Heof15a1da2006-05-15 20:57:56 +09005877 ata_port_printk(ap, KERN_WARNING, "irq trap\n");
Alan Cox23cfce82006-03-21 16:06:53 +00005878 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005879 }
5880#endif
5881 return 0; /* irq not handled */
5882}
5883
5884/**
5885 * ata_interrupt - Default ATA host interrupt handler
Jeff Garzik0cba6322005-05-30 19:49:12 -04005886 * @irq: irq line (unused)
Jeff Garzikcca39742006-08-24 03:19:22 -04005887 * @dev_instance: pointer to our ata_host information structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07005888 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04005889 * Default interrupt handler for PCI IDE devices. Calls
5890 * ata_host_intr() for each port that is not disabled.
5891 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005892 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04005893 * Obtains host lock during operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005894 *
5895 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04005896 * IRQ_NONE or IRQ_HANDLED.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005897 */
5898
David Howells7d12e782006-10-05 14:55:46 +01005899irqreturn_t ata_interrupt (int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005900{
Jeff Garzikcca39742006-08-24 03:19:22 -04005901 struct ata_host *host = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005902 unsigned int i;
5903 unsigned int handled = 0;
5904 unsigned long flags;
5905
5906 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
Jeff Garzikcca39742006-08-24 03:19:22 -04005907 spin_lock_irqsave(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005908
Jeff Garzikcca39742006-08-24 03:19:22 -04005909 for (i = 0; i < host->n_ports; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005910 struct ata_port *ap;
5911
Jeff Garzikcca39742006-08-24 03:19:22 -04005912 ap = host->ports[i];
Tejun Heoc1389502005-08-22 14:59:24 +09005913 if (ap &&
Jeff Garzik029f5462006-04-02 10:30:40 -04005914 !(ap->flags & ATA_FLAG_DISABLED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005915 struct ata_queued_cmd *qc;
5916
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005917 qc = ata_qc_from_tag(ap, ap->link.active_tag);
Albert Lee312f7da2005-09-27 17:38:03 +08005918 if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING)) &&
Albert Lee21b1ed72005-04-29 17:34:59 +08005919 (qc->flags & ATA_QCFLAG_ACTIVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005920 handled |= ata_host_intr(ap, qc);
5921 }
5922 }
5923
Jeff Garzikcca39742006-08-24 03:19:22 -04005924 spin_unlock_irqrestore(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005925
5926 return IRQ_RETVAL(handled);
5927}
5928
Tejun Heo34bf2172006-05-15 20:57:46 +09005929/**
5930 * sata_scr_valid - test whether SCRs are accessible
Tejun Heo936fd732007-08-06 18:36:23 +09005931 * @link: ATA link to test SCR accessibility for
Tejun Heo34bf2172006-05-15 20:57:46 +09005932 *
Tejun Heo936fd732007-08-06 18:36:23 +09005933 * Test whether SCRs are accessible for @link.
Tejun Heo34bf2172006-05-15 20:57:46 +09005934 *
5935 * LOCKING:
5936 * None.
5937 *
5938 * RETURNS:
5939 * 1 if SCRs are accessible, 0 otherwise.
5940 */
Tejun Heo936fd732007-08-06 18:36:23 +09005941int sata_scr_valid(struct ata_link *link)
Tejun Heo34bf2172006-05-15 20:57:46 +09005942{
Tejun Heo936fd732007-08-06 18:36:23 +09005943 struct ata_port *ap = link->ap;
5944
Tejun Heoa16abc02007-05-21 18:33:47 +02005945 return (ap->flags & ATA_FLAG_SATA) && ap->ops->scr_read;
Tejun Heo34bf2172006-05-15 20:57:46 +09005946}
5947
5948/**
5949 * sata_scr_read - read SCR register of the specified port
Tejun Heo936fd732007-08-06 18:36:23 +09005950 * @link: ATA link to read SCR for
Tejun Heo34bf2172006-05-15 20:57:46 +09005951 * @reg: SCR to read
5952 * @val: Place to store read value
5953 *
Tejun Heo936fd732007-08-06 18:36:23 +09005954 * Read SCR register @reg of @link into *@val. This function is
Tejun Heo34bf2172006-05-15 20:57:46 +09005955 * guaranteed to succeed if the cable type of the port is SATA
5956 * and the port implements ->scr_read.
5957 *
5958 * LOCKING:
5959 * None.
5960 *
5961 * RETURNS:
5962 * 0 on success, negative errno on failure.
5963 */
Tejun Heo936fd732007-08-06 18:36:23 +09005964int sata_scr_read(struct ata_link *link, int reg, u32 *val)
Tejun Heo34bf2172006-05-15 20:57:46 +09005965{
Tejun Heo936fd732007-08-06 18:36:23 +09005966 struct ata_port *ap = link->ap;
5967
5968 if (sata_scr_valid(link))
Tejun Heoda3dbb12007-07-16 14:29:40 +09005969 return ap->ops->scr_read(ap, reg, val);
Tejun Heo34bf2172006-05-15 20:57:46 +09005970 return -EOPNOTSUPP;
5971}
5972
5973/**
5974 * sata_scr_write - write SCR register of the specified port
Tejun Heo936fd732007-08-06 18:36:23 +09005975 * @link: ATA link to write SCR for
Tejun Heo34bf2172006-05-15 20:57:46 +09005976 * @reg: SCR to write
5977 * @val: value to write
5978 *
Tejun Heo936fd732007-08-06 18:36:23 +09005979 * Write @val to SCR register @reg of @link. This function is
Tejun Heo34bf2172006-05-15 20:57:46 +09005980 * guaranteed to succeed if the cable type of the port is SATA
5981 * and the port implements ->scr_read.
5982 *
5983 * LOCKING:
5984 * None.
5985 *
5986 * RETURNS:
5987 * 0 on success, negative errno on failure.
5988 */
Tejun Heo936fd732007-08-06 18:36:23 +09005989int sata_scr_write(struct ata_link *link, int reg, u32 val)
Tejun Heo34bf2172006-05-15 20:57:46 +09005990{
Tejun Heo936fd732007-08-06 18:36:23 +09005991 struct ata_port *ap = link->ap;
5992
5993 if (sata_scr_valid(link))
Tejun Heoda3dbb12007-07-16 14:29:40 +09005994 return ap->ops->scr_write(ap, reg, val);
Tejun Heo34bf2172006-05-15 20:57:46 +09005995 return -EOPNOTSUPP;
5996}
5997
5998/**
5999 * sata_scr_write_flush - write SCR register of the specified port and flush
Tejun Heo936fd732007-08-06 18:36:23 +09006000 * @link: ATA link to write SCR for
Tejun Heo34bf2172006-05-15 20:57:46 +09006001 * @reg: SCR to write
6002 * @val: value to write
6003 *
6004 * This function is identical to sata_scr_write() except that this
6005 * function performs flush after writing to the register.
6006 *
6007 * LOCKING:
6008 * None.
6009 *
6010 * RETURNS:
6011 * 0 on success, negative errno on failure.
6012 */
Tejun Heo936fd732007-08-06 18:36:23 +09006013int sata_scr_write_flush(struct ata_link *link, int reg, u32 val)
Tejun Heo34bf2172006-05-15 20:57:46 +09006014{
Tejun Heo936fd732007-08-06 18:36:23 +09006015 struct ata_port *ap = link->ap;
Tejun Heoda3dbb12007-07-16 14:29:40 +09006016 int rc;
6017
Tejun Heo936fd732007-08-06 18:36:23 +09006018 if (sata_scr_valid(link)) {
Tejun Heoda3dbb12007-07-16 14:29:40 +09006019 rc = ap->ops->scr_write(ap, reg, val);
6020 if (rc == 0)
6021 rc = ap->ops->scr_read(ap, reg, &val);
6022 return rc;
Tejun Heo34bf2172006-05-15 20:57:46 +09006023 }
6024 return -EOPNOTSUPP;
6025}
6026
6027/**
Tejun Heo936fd732007-08-06 18:36:23 +09006028 * ata_link_online - test whether the given link is online
6029 * @link: ATA link to test
Tejun Heo34bf2172006-05-15 20:57:46 +09006030 *
Tejun Heo936fd732007-08-06 18:36:23 +09006031 * Test whether @link is online. Note that this function returns
6032 * 0 if online status of @link cannot be obtained, so
6033 * ata_link_online(link) != !ata_link_offline(link).
Tejun Heo34bf2172006-05-15 20:57:46 +09006034 *
6035 * LOCKING:
6036 * None.
6037 *
6038 * RETURNS:
6039 * 1 if the port online status is available and online.
6040 */
Tejun Heo936fd732007-08-06 18:36:23 +09006041int ata_link_online(struct ata_link *link)
Tejun Heo34bf2172006-05-15 20:57:46 +09006042{
6043 u32 sstatus;
6044
Tejun Heo936fd732007-08-06 18:36:23 +09006045 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
6046 (sstatus & 0xf) == 0x3)
Tejun Heo34bf2172006-05-15 20:57:46 +09006047 return 1;
6048 return 0;
6049}
6050
6051/**
Tejun Heo936fd732007-08-06 18:36:23 +09006052 * ata_link_offline - test whether the given link is offline
6053 * @link: ATA link to test
Tejun Heo34bf2172006-05-15 20:57:46 +09006054 *
Tejun Heo936fd732007-08-06 18:36:23 +09006055 * Test whether @link is offline. Note that this function
6056 * returns 0 if offline status of @link cannot be obtained, so
6057 * ata_link_online(link) != !ata_link_offline(link).
Tejun Heo34bf2172006-05-15 20:57:46 +09006058 *
6059 * LOCKING:
6060 * None.
6061 *
6062 * RETURNS:
6063 * 1 if the port offline status is available and offline.
6064 */
Tejun Heo936fd732007-08-06 18:36:23 +09006065int ata_link_offline(struct ata_link *link)
Tejun Heo34bf2172006-05-15 20:57:46 +09006066{
6067 u32 sstatus;
6068
Tejun Heo936fd732007-08-06 18:36:23 +09006069 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
6070 (sstatus & 0xf) != 0x3)
Tejun Heo34bf2172006-05-15 20:57:46 +09006071 return 1;
6072 return 0;
6073}
Edward Falk0baab862005-06-02 18:17:13 -04006074
Tejun Heo77b08fb2006-06-24 20:30:19 +09006075int ata_flush_cache(struct ata_device *dev)
Jens Axboe9b847542006-01-06 09:28:07 +01006076{
Tejun Heo977e6b92006-06-24 20:30:19 +09006077 unsigned int err_mask;
Jens Axboe9b847542006-01-06 09:28:07 +01006078 u8 cmd;
6079
6080 if (!ata_try_flush_cache(dev))
6081 return 0;
6082
Tejun Heo6fc49ad2006-11-11 20:10:45 +09006083 if (dev->flags & ATA_DFLAG_FLUSH_EXT)
Jens Axboe9b847542006-01-06 09:28:07 +01006084 cmd = ATA_CMD_FLUSH_EXT;
6085 else
6086 cmd = ATA_CMD_FLUSH;
6087
Alan Cox4f343372007-08-08 14:30:31 +01006088 /* This is wrong. On a failed flush we get back the LBA of the lost
6089 sector and we should (assuming it wasn't aborted as unknown) issue
6090 a further flush command to continue the writeback until it
6091 does not error */
Tejun Heo977e6b92006-06-24 20:30:19 +09006092 err_mask = ata_do_simple_cmd(dev, cmd);
6093 if (err_mask) {
6094 ata_dev_printk(dev, KERN_ERR, "failed to flush cache\n");
6095 return -EIO;
6096 }
6097
6098 return 0;
Jens Axboe9b847542006-01-06 09:28:07 +01006099}
6100
Tejun Heo6ffa01d2007-03-02 17:32:47 +09006101#ifdef CONFIG_PM
Jeff Garzikcca39742006-08-24 03:19:22 -04006102static int ata_host_request_pm(struct ata_host *host, pm_message_t mesg,
6103 unsigned int action, unsigned int ehi_flags,
6104 int wait)
Tejun Heo500530f2006-07-03 16:07:27 +09006105{
6106 unsigned long flags;
6107 int i, rc;
6108
Jeff Garzikcca39742006-08-24 03:19:22 -04006109 for (i = 0; i < host->n_ports; i++) {
6110 struct ata_port *ap = host->ports[i];
Tejun Heoe3667eb2007-08-06 18:36:24 +09006111 struct ata_link *link;
Tejun Heo500530f2006-07-03 16:07:27 +09006112
6113 /* Previous resume operation might still be in
6114 * progress. Wait for PM_PENDING to clear.
6115 */
6116 if (ap->pflags & ATA_PFLAG_PM_PENDING) {
6117 ata_port_wait_eh(ap);
6118 WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
6119 }
6120
6121 /* request PM ops to EH */
6122 spin_lock_irqsave(ap->lock, flags);
6123
6124 ap->pm_mesg = mesg;
6125 if (wait) {
6126 rc = 0;
6127 ap->pm_result = &rc;
6128 }
6129
6130 ap->pflags |= ATA_PFLAG_PM_PENDING;
Tejun Heoe3667eb2007-08-06 18:36:24 +09006131 __ata_port_for_each_link(link, ap) {
6132 link->eh_info.action |= action;
6133 link->eh_info.flags |= ehi_flags;
6134 }
Tejun Heo500530f2006-07-03 16:07:27 +09006135
6136 ata_port_schedule_eh(ap);
6137
6138 spin_unlock_irqrestore(ap->lock, flags);
6139
6140 /* wait and check result */
6141 if (wait) {
6142 ata_port_wait_eh(ap);
6143 WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
6144 if (rc)
6145 return rc;
6146 }
6147 }
6148
6149 return 0;
6150}
6151
6152/**
Jeff Garzikcca39742006-08-24 03:19:22 -04006153 * ata_host_suspend - suspend host
6154 * @host: host to suspend
Tejun Heo500530f2006-07-03 16:07:27 +09006155 * @mesg: PM message
6156 *
Jeff Garzikcca39742006-08-24 03:19:22 -04006157 * Suspend @host. Actual operation is performed by EH. This
Tejun Heo500530f2006-07-03 16:07:27 +09006158 * function requests EH to perform PM operations and waits for EH
6159 * to finish.
6160 *
6161 * LOCKING:
6162 * Kernel thread context (may sleep).
6163 *
6164 * RETURNS:
6165 * 0 on success, -errno on failure.
6166 */
Jeff Garzikcca39742006-08-24 03:19:22 -04006167int ata_host_suspend(struct ata_host *host, pm_message_t mesg)
Tejun Heo500530f2006-07-03 16:07:27 +09006168{
Tejun Heo9666f402007-05-04 21:27:47 +02006169 int rc;
Tejun Heo500530f2006-07-03 16:07:27 +09006170
Jeff Garzikcca39742006-08-24 03:19:22 -04006171 rc = ata_host_request_pm(host, mesg, 0, ATA_EHI_QUIET, 1);
Tejun Heo9666f402007-05-04 21:27:47 +02006172 if (rc == 0)
6173 host->dev->power.power_state = mesg;
Tejun Heo500530f2006-07-03 16:07:27 +09006174 return rc;
6175}
6176
6177/**
Jeff Garzikcca39742006-08-24 03:19:22 -04006178 * ata_host_resume - resume host
6179 * @host: host to resume
Tejun Heo500530f2006-07-03 16:07:27 +09006180 *
Jeff Garzikcca39742006-08-24 03:19:22 -04006181 * Resume @host. Actual operation is performed by EH. This
Tejun Heo500530f2006-07-03 16:07:27 +09006182 * function requests EH to perform PM operations and returns.
6183 * Note that all resume operations are performed parallely.
6184 *
6185 * LOCKING:
6186 * Kernel thread context (may sleep).
6187 */
Jeff Garzikcca39742006-08-24 03:19:22 -04006188void ata_host_resume(struct ata_host *host)
Tejun Heo500530f2006-07-03 16:07:27 +09006189{
Jeff Garzikcca39742006-08-24 03:19:22 -04006190 ata_host_request_pm(host, PMSG_ON, ATA_EH_SOFTRESET,
6191 ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET, 0);
6192 host->dev->power.power_state = PMSG_ON;
Tejun Heo500530f2006-07-03 16:07:27 +09006193}
Tejun Heo6ffa01d2007-03-02 17:32:47 +09006194#endif
Tejun Heo500530f2006-07-03 16:07:27 +09006195
Randy Dunlapc893a3a2006-01-28 13:15:32 -05006196/**
6197 * ata_port_start - Set port up for dma.
6198 * @ap: Port to initialize
6199 *
6200 * Called just after data structures for each port are
6201 * initialized. Allocates space for PRD table.
6202 *
6203 * May be used as the port_start() entry in ata_port_operations.
6204 *
6205 * LOCKING:
6206 * Inherited from caller.
6207 */
Tejun Heof0d36ef2007-01-20 16:00:28 +09006208int ata_port_start(struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006209{
Brian King2f1f6102006-03-23 17:30:15 -06006210 struct device *dev = ap->dev;
Jeff Garzik6037d6b2005-11-04 22:08:00 -05006211 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006212
Tejun Heof0d36ef2007-01-20 16:00:28 +09006213 ap->prd = dmam_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma,
6214 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006215 if (!ap->prd)
6216 return -ENOMEM;
6217
Jeff Garzik6037d6b2005-11-04 22:08:00 -05006218 rc = ata_pad_alloc(ap, dev);
Tejun Heof0d36ef2007-01-20 16:00:28 +09006219 if (rc)
Jeff Garzik6037d6b2005-11-04 22:08:00 -05006220 return rc;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04006221
Tejun Heof0d36ef2007-01-20 16:00:28 +09006222 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd,
6223 (unsigned long long)ap->prd_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006224 return 0;
6225}
6226
Edward Falk0baab862005-06-02 18:17:13 -04006227/**
Tejun Heo3ef3b432006-05-31 18:27:30 +09006228 * ata_dev_init - Initialize an ata_device structure
6229 * @dev: Device structure to initialize
6230 *
6231 * Initialize @dev in preparation for probing.
6232 *
6233 * LOCKING:
6234 * Inherited from caller.
6235 */
6236void ata_dev_init(struct ata_device *dev)
6237{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09006238 struct ata_link *link = dev->link;
6239 struct ata_port *ap = link->ap;
Tejun Heo72fa4b72006-05-31 18:27:32 +09006240 unsigned long flags;
Tejun Heo3ef3b432006-05-31 18:27:30 +09006241
Tejun Heo5a04bf42006-05-31 18:27:38 +09006242 /* SATA spd limit is bound to the first device */
Tejun Heo9af5c9c2007-08-06 18:36:22 +09006243 link->sata_spd_limit = link->hw_sata_spd_limit;
6244 link->sata_spd = 0;
Tejun Heo5a04bf42006-05-31 18:27:38 +09006245
Tejun Heo72fa4b72006-05-31 18:27:32 +09006246 /* High bits of dev->flags are used to record warm plug
6247 * requests which occur asynchronously. Synchronize using
Jeff Garzikcca39742006-08-24 03:19:22 -04006248 * host lock.
Tejun Heo72fa4b72006-05-31 18:27:32 +09006249 */
Jeff Garzikba6a1302006-06-22 23:46:10 -04006250 spin_lock_irqsave(ap->lock, flags);
Tejun Heo72fa4b72006-05-31 18:27:32 +09006251 dev->flags &= ~ATA_DFLAG_INIT_MASK;
Tejun Heo3dcc3232007-09-03 12:20:11 +09006252 dev->horkage = 0;
Jeff Garzikba6a1302006-06-22 23:46:10 -04006253 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo72fa4b72006-05-31 18:27:32 +09006254
6255 memset((void *)dev + ATA_DEVICE_CLEAR_OFFSET, 0,
6256 sizeof(*dev) - ATA_DEVICE_CLEAR_OFFSET);
Tejun Heo3ef3b432006-05-31 18:27:30 +09006257 dev->pio_mask = UINT_MAX;
6258 dev->mwdma_mask = UINT_MAX;
6259 dev->udma_mask = UINT_MAX;
6260}
6261
6262/**
Tejun Heo4fb37a22007-08-06 18:36:23 +09006263 * ata_link_init - Initialize an ata_link structure
6264 * @ap: ATA port link is attached to
6265 * @link: Link structure to initialize
Tejun Heo89898052007-08-06 18:36:23 +09006266 * @pmp: Port multiplier port number
Tejun Heo4fb37a22007-08-06 18:36:23 +09006267 *
6268 * Initialize @link.
6269 *
6270 * LOCKING:
6271 * Kernel thread context (may sleep)
6272 */
Tejun Heofb7fd612007-09-23 13:14:12 +09006273void ata_link_init(struct ata_port *ap, struct ata_link *link, int pmp)
Tejun Heo4fb37a22007-08-06 18:36:23 +09006274{
6275 int i;
6276
6277 /* clear everything except for devices */
6278 memset(link, 0, offsetof(struct ata_link, device[0]));
6279
6280 link->ap = ap;
Tejun Heo89898052007-08-06 18:36:23 +09006281 link->pmp = pmp;
Tejun Heo4fb37a22007-08-06 18:36:23 +09006282 link->active_tag = ATA_TAG_POISON;
6283 link->hw_sata_spd_limit = UINT_MAX;
6284
6285 /* can't use iterator, ap isn't initialized yet */
6286 for (i = 0; i < ATA_MAX_DEVICES; i++) {
6287 struct ata_device *dev = &link->device[i];
6288
6289 dev->link = link;
6290 dev->devno = dev - link->device;
6291 ata_dev_init(dev);
6292 }
6293}
6294
6295/**
6296 * sata_link_init_spd - Initialize link->sata_spd_limit
6297 * @link: Link to configure sata_spd_limit for
6298 *
6299 * Initialize @link->[hw_]sata_spd_limit to the currently
6300 * configured value.
6301 *
6302 * LOCKING:
6303 * Kernel thread context (may sleep).
6304 *
6305 * RETURNS:
6306 * 0 on success, -errno on failure.
6307 */
Tejun Heofb7fd612007-09-23 13:14:12 +09006308int sata_link_init_spd(struct ata_link *link)
Tejun Heo4fb37a22007-08-06 18:36:23 +09006309{
6310 u32 scontrol, spd;
6311 int rc;
6312
6313 rc = sata_scr_read(link, SCR_CONTROL, &scontrol);
6314 if (rc)
6315 return rc;
6316
6317 spd = (scontrol >> 4) & 0xf;
6318 if (spd)
6319 link->hw_sata_spd_limit &= (1 << spd) - 1;
6320
6321 link->sata_spd_limit = link->hw_sata_spd_limit;
6322
6323 return 0;
6324}
6325
6326/**
Tejun Heof3187192007-04-17 23:44:07 +09006327 * ata_port_alloc - allocate and initialize basic ATA port resources
6328 * @host: ATA host this allocated port belongs to
Linus Torvalds1da177e2005-04-16 15:20:36 -07006329 *
Tejun Heof3187192007-04-17 23:44:07 +09006330 * Allocate and initialize basic ATA port resources.
6331 *
6332 * RETURNS:
6333 * Allocate ATA port on success, NULL on failure.
Jeff Garzik0cba6322005-05-30 19:49:12 -04006334 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006335 * LOCKING:
Tejun Heof3187192007-04-17 23:44:07 +09006336 * Inherited from calling layer (may sleep).
Linus Torvalds1da177e2005-04-16 15:20:36 -07006337 */
Tejun Heof3187192007-04-17 23:44:07 +09006338struct ata_port *ata_port_alloc(struct ata_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006339{
Tejun Heof3187192007-04-17 23:44:07 +09006340 struct ata_port *ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006341
Tejun Heof3187192007-04-17 23:44:07 +09006342 DPRINTK("ENTER\n");
6343
6344 ap = kzalloc(sizeof(*ap), GFP_KERNEL);
6345 if (!ap)
6346 return NULL;
6347
Tejun Heof4d6d002007-05-01 11:50:15 +02006348 ap->pflags |= ATA_PFLAG_INITIALIZING;
Jeff Garzikcca39742006-08-24 03:19:22 -04006349 ap->lock = &host->lock;
Tejun Heo198e0fe2006-04-02 18:51:52 +09006350 ap->flags = ATA_FLAG_DISABLED;
Tejun Heof3187192007-04-17 23:44:07 +09006351 ap->print_id = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006352 ap->ctl = ATA_DEVCTL_OBS;
Jeff Garzikcca39742006-08-24 03:19:22 -04006353 ap->host = host;
Tejun Heof3187192007-04-17 23:44:07 +09006354 ap->dev = host->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006355 ap->last_ctl = 0xFF;
Borislav Petkovbd5d8252006-06-11 23:17:01 -04006356
6357#if defined(ATA_VERBOSE_DEBUG)
6358 /* turn on all debugging levels */
6359 ap->msg_enable = 0x00FF;
6360#elif defined(ATA_DEBUG)
6361 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 +09006362#else
Borislav Petkov0dd4b212006-06-23 02:29:08 -04006363 ap->msg_enable = ATA_MSG_DRV | ATA_MSG_ERR | ATA_MSG_WARN;
Borislav Petkovbd5d8252006-06-11 23:17:01 -04006364#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006365
David Howells65f27f32006-11-22 14:55:48 +00006366 INIT_DELAYED_WORK(&ap->port_task, NULL);
6367 INIT_DELAYED_WORK(&ap->hotplug_task, ata_scsi_hotplug);
6368 INIT_WORK(&ap->scsi_rescan_task, ata_scsi_dev_rescan);
Tejun Heoa72ec4c2006-01-23 13:09:37 +09006369 INIT_LIST_HEAD(&ap->eh_done_q);
Tejun Heoc6cf9e92006-05-31 18:27:27 +09006370 init_waitqueue_head(&ap->eh_wait_q);
Tejun Heo5ddf24c2007-07-16 14:29:41 +09006371 init_timer_deferrable(&ap->fastdrain_timer);
6372 ap->fastdrain_timer.function = ata_eh_fastdrain_timerfn;
6373 ap->fastdrain_timer.data = (unsigned long)ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006374
Tejun Heo838df622006-05-15 20:57:44 +09006375 ap->cbl = ATA_CBL_NONE;
Tejun Heo838df622006-05-15 20:57:44 +09006376
Tejun Heo89898052007-08-06 18:36:23 +09006377 ata_link_init(ap, &ap->link, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006378
6379#ifdef ATA_IRQ_TRAP
6380 ap->stats.unhandled_irq = 1;
6381 ap->stats.idle_irq = 1;
6382#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006383 return ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006384}
6385
Tejun Heof0d36ef2007-01-20 16:00:28 +09006386static void ata_host_release(struct device *gendev, void *res)
6387{
6388 struct ata_host *host = dev_get_drvdata(gendev);
6389 int i;
6390
6391 for (i = 0; i < host->n_ports; i++) {
6392 struct ata_port *ap = host->ports[i];
6393
Tejun Heoecef7252007-04-17 23:44:06 +09006394 if (!ap)
6395 continue;
6396
6397 if ((host->flags & ATA_HOST_STARTED) && ap->ops->port_stop)
Tejun Heof0d36ef2007-01-20 16:00:28 +09006398 ap->ops->port_stop(ap);
Tejun Heof0d36ef2007-01-20 16:00:28 +09006399 }
6400
Tejun Heoecef7252007-04-17 23:44:06 +09006401 if ((host->flags & ATA_HOST_STARTED) && host->ops->host_stop)
Tejun Heof0d36ef2007-01-20 16:00:28 +09006402 host->ops->host_stop(host);
Tejun Heo1aa56cc2007-02-27 22:33:21 +09006403
Tejun Heo1aa506e42007-03-09 19:36:12 +09006404 for (i = 0; i < host->n_ports; i++) {
6405 struct ata_port *ap = host->ports[i];
6406
Tejun Heo49114872007-04-17 23:44:06 +09006407 if (!ap)
6408 continue;
6409
6410 if (ap->scsi_host)
Tejun Heo1aa506e42007-03-09 19:36:12 +09006411 scsi_host_put(ap->scsi_host);
6412
Tejun Heo49114872007-04-17 23:44:06 +09006413 kfree(ap);
Tejun Heo1aa506e42007-03-09 19:36:12 +09006414 host->ports[i] = NULL;
6415 }
6416
Tejun Heo1aa56cc2007-02-27 22:33:21 +09006417 dev_set_drvdata(gendev, NULL);
Tejun Heof0d36ef2007-01-20 16:00:28 +09006418}
6419
Linus Torvalds1da177e2005-04-16 15:20:36 -07006420/**
Tejun Heof3187192007-04-17 23:44:07 +09006421 * ata_host_alloc - allocate and init basic ATA host resources
6422 * @dev: generic device this host is associated with
6423 * @max_ports: maximum number of ATA ports associated with this host
6424 *
6425 * Allocate and initialize basic ATA host resources. LLD calls
6426 * this function to allocate a host, initializes it fully and
6427 * attaches it using ata_host_register().
6428 *
6429 * @max_ports ports are allocated and host->n_ports is
6430 * initialized to @max_ports. The caller is allowed to decrease
6431 * host->n_ports before calling ata_host_register(). The unused
6432 * ports will be automatically freed on registration.
6433 *
6434 * RETURNS:
6435 * Allocate ATA host on success, NULL on failure.
6436 *
6437 * LOCKING:
6438 * Inherited from calling layer (may sleep).
6439 */
6440struct ata_host *ata_host_alloc(struct device *dev, int max_ports)
6441{
6442 struct ata_host *host;
6443 size_t sz;
6444 int i;
6445
6446 DPRINTK("ENTER\n");
6447
6448 if (!devres_open_group(dev, NULL, GFP_KERNEL))
6449 return NULL;
6450
6451 /* alloc a container for our list of ATA ports (buses) */
6452 sz = sizeof(struct ata_host) + (max_ports + 1) * sizeof(void *);
6453 /* alloc a container for our list of ATA ports (buses) */
6454 host = devres_alloc(ata_host_release, sz, GFP_KERNEL);
6455 if (!host)
6456 goto err_out;
6457
6458 devres_add(dev, host);
6459 dev_set_drvdata(dev, host);
6460
6461 spin_lock_init(&host->lock);
6462 host->dev = dev;
6463 host->n_ports = max_ports;
6464
6465 /* allocate ports bound to this host */
6466 for (i = 0; i < max_ports; i++) {
6467 struct ata_port *ap;
6468
6469 ap = ata_port_alloc(host);
6470 if (!ap)
6471 goto err_out;
6472
6473 ap->port_no = i;
6474 host->ports[i] = ap;
6475 }
6476
6477 devres_remove_group(dev, NULL);
6478 return host;
6479
6480 err_out:
6481 devres_release_group(dev, NULL);
6482 return NULL;
6483}
6484
6485/**
Tejun Heof5cda252007-04-17 23:44:07 +09006486 * ata_host_alloc_pinfo - alloc host and init with port_info array
6487 * @dev: generic device this host is associated with
6488 * @ppi: array of ATA port_info to initialize host with
6489 * @n_ports: number of ATA ports attached to this host
6490 *
6491 * Allocate ATA host and initialize with info from @ppi. If NULL
6492 * terminated, @ppi may contain fewer entries than @n_ports. The
6493 * last entry will be used for the remaining ports.
6494 *
6495 * RETURNS:
6496 * Allocate ATA host on success, NULL on failure.
6497 *
6498 * LOCKING:
6499 * Inherited from calling layer (may sleep).
6500 */
6501struct ata_host *ata_host_alloc_pinfo(struct device *dev,
6502 const struct ata_port_info * const * ppi,
6503 int n_ports)
6504{
6505 const struct ata_port_info *pi;
6506 struct ata_host *host;
6507 int i, j;
6508
6509 host = ata_host_alloc(dev, n_ports);
6510 if (!host)
6511 return NULL;
6512
6513 for (i = 0, j = 0, pi = NULL; i < host->n_ports; i++) {
6514 struct ata_port *ap = host->ports[i];
6515
6516 if (ppi[j])
6517 pi = ppi[j++];
6518
6519 ap->pio_mask = pi->pio_mask;
6520 ap->mwdma_mask = pi->mwdma_mask;
6521 ap->udma_mask = pi->udma_mask;
6522 ap->flags |= pi->flags;
Tejun Heo0c887582007-08-06 18:36:23 +09006523 ap->link.flags |= pi->link_flags;
Tejun Heof5cda252007-04-17 23:44:07 +09006524 ap->ops = pi->port_ops;
6525
6526 if (!host->ops && (pi->port_ops != &ata_dummy_port_ops))
6527 host->ops = pi->port_ops;
6528 if (!host->private_data && pi->private_data)
6529 host->private_data = pi->private_data;
6530 }
6531
6532 return host;
6533}
6534
6535/**
Tejun Heoecef7252007-04-17 23:44:06 +09006536 * ata_host_start - start and freeze ports of an ATA host
6537 * @host: ATA host to start ports for
6538 *
6539 * Start and then freeze ports of @host. Started status is
6540 * recorded in host->flags, so this function can be called
6541 * multiple times. Ports are guaranteed to get started only
Tejun Heof3187192007-04-17 23:44:07 +09006542 * once. If host->ops isn't initialized yet, its set to the
6543 * first non-dummy port ops.
Tejun Heoecef7252007-04-17 23:44:06 +09006544 *
6545 * LOCKING:
6546 * Inherited from calling layer (may sleep).
6547 *
6548 * RETURNS:
6549 * 0 if all ports are started successfully, -errno otherwise.
6550 */
6551int ata_host_start(struct ata_host *host)
6552{
6553 int i, rc;
6554
6555 if (host->flags & ATA_HOST_STARTED)
6556 return 0;
6557
6558 for (i = 0; i < host->n_ports; i++) {
6559 struct ata_port *ap = host->ports[i];
6560
Tejun Heof3187192007-04-17 23:44:07 +09006561 if (!host->ops && !ata_port_is_dummy(ap))
6562 host->ops = ap->ops;
6563
Tejun Heoecef7252007-04-17 23:44:06 +09006564 if (ap->ops->port_start) {
6565 rc = ap->ops->port_start(ap);
6566 if (rc) {
6567 ata_port_printk(ap, KERN_ERR, "failed to "
6568 "start port (errno=%d)\n", rc);
6569 goto err_out;
6570 }
6571 }
6572
6573 ata_eh_freeze_port(ap);
6574 }
6575
6576 host->flags |= ATA_HOST_STARTED;
6577 return 0;
6578
6579 err_out:
6580 while (--i >= 0) {
6581 struct ata_port *ap = host->ports[i];
6582
6583 if (ap->ops->port_stop)
6584 ap->ops->port_stop(ap);
6585 }
6586 return rc;
6587}
6588
6589/**
Jeff Garzikcca39742006-08-24 03:19:22 -04006590 * ata_sas_host_init - Initialize a host struct
6591 * @host: host to initialize
6592 * @dev: device host is attached to
6593 * @flags: host flags
6594 * @ops: port_ops
Brian Kingb03732f2006-08-07 14:27:10 -05006595 *
6596 * LOCKING:
6597 * PCI/etc. bus probe sem.
6598 *
6599 */
Tejun Heof3187192007-04-17 23:44:07 +09006600/* KILLME - the only user left is ipr */
Jeff Garzikcca39742006-08-24 03:19:22 -04006601void ata_host_init(struct ata_host *host, struct device *dev,
6602 unsigned long flags, const struct ata_port_operations *ops)
Brian Kingb03732f2006-08-07 14:27:10 -05006603{
Jeff Garzikcca39742006-08-24 03:19:22 -04006604 spin_lock_init(&host->lock);
6605 host->dev = dev;
6606 host->flags = flags;
6607 host->ops = ops;
Brian Kingb03732f2006-08-07 14:27:10 -05006608}
6609
6610/**
Tejun Heof3187192007-04-17 23:44:07 +09006611 * ata_host_register - register initialized ATA host
6612 * @host: ATA host to register
6613 * @sht: template for SCSI host
Jeff Garzik0cba6322005-05-30 19:49:12 -04006614 *
Tejun Heof3187192007-04-17 23:44:07 +09006615 * Register initialized ATA host. @host is allocated using
6616 * ata_host_alloc() and fully initialized by LLD. This function
6617 * starts ports, registers @host with ATA and SCSI layers and
6618 * probe registered devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006619 *
6620 * LOCKING:
Tejun Heof3187192007-04-17 23:44:07 +09006621 * Inherited from calling layer (may sleep).
Linus Torvalds1da177e2005-04-16 15:20:36 -07006622 *
6623 * RETURNS:
Tejun Heof3187192007-04-17 23:44:07 +09006624 * 0 on success, -errno otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006625 */
Tejun Heof3187192007-04-17 23:44:07 +09006626int ata_host_register(struct ata_host *host, struct scsi_host_template *sht)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006627{
Tejun Heof3187192007-04-17 23:44:07 +09006628 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006629
Tejun Heof3187192007-04-17 23:44:07 +09006630 /* host must have been started */
6631 if (!(host->flags & ATA_HOST_STARTED)) {
6632 dev_printk(KERN_ERR, host->dev,
6633 "BUG: trying to register unstarted host\n");
6634 WARN_ON(1);
6635 return -EINVAL;
Alan Cox02f076a2006-09-26 17:35:32 +01006636 }
Tejun Heof0d36ef2007-01-20 16:00:28 +09006637
Tejun Heof3187192007-04-17 23:44:07 +09006638 /* Blow away unused ports. This happens when LLD can't
6639 * determine the exact number of ports to allocate at
6640 * allocation time.
6641 */
6642 for (i = host->n_ports; host->ports[i]; i++)
6643 kfree(host->ports[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006644
Tejun Heof3187192007-04-17 23:44:07 +09006645 /* give ports names and add SCSI hosts */
6646 for (i = 0; i < host->n_ports; i++)
6647 host->ports[i]->print_id = ata_print_id++;
Tejun Heof0d36ef2007-01-20 16:00:28 +09006648
Tejun Heof3187192007-04-17 23:44:07 +09006649 rc = ata_scsi_add_hosts(host, sht);
Tejun Heoecef7252007-04-17 23:44:06 +09006650 if (rc)
Tejun Heof3187192007-04-17 23:44:07 +09006651 return rc;
Tejun Heoecef7252007-04-17 23:44:06 +09006652
Tejun Heofafbae82007-05-15 03:28:16 +09006653 /* associate with ACPI nodes */
6654 ata_acpi_associate(host);
6655
Tejun Heof3187192007-04-17 23:44:07 +09006656 /* set cable, sata_spd_limit and report */
Jeff Garzikcca39742006-08-24 03:19:22 -04006657 for (i = 0; i < host->n_ports; i++) {
6658 struct ata_port *ap = host->ports[i];
Tejun Heof3187192007-04-17 23:44:07 +09006659 unsigned long xfer_mask;
6660
6661 /* set SATA cable type if still unset */
6662 if (ap->cbl == ATA_CBL_NONE && (ap->flags & ATA_FLAG_SATA))
6663 ap->cbl = ATA_CBL_SATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006664
Tejun Heo5a04bf42006-05-31 18:27:38 +09006665 /* init sata_spd_limit to the current value */
Tejun Heo4fb37a22007-08-06 18:36:23 +09006666 sata_link_init_spd(&ap->link);
Tejun Heo5a04bf42006-05-31 18:27:38 +09006667
Tejun Heocbcdd872007-08-18 13:14:55 +09006668 /* print per-port info to dmesg */
Tejun Heof3187192007-04-17 23:44:07 +09006669 xfer_mask = ata_pack_xfermask(ap->pio_mask, ap->mwdma_mask,
6670 ap->udma_mask);
6671
Tejun Heof3187192007-04-17 23:44:07 +09006672 if (!ata_port_is_dummy(ap))
Tejun Heocbcdd872007-08-18 13:14:55 +09006673 ata_port_printk(ap, KERN_INFO,
6674 "%cATA max %s %s\n",
Tejun Heoa16abc02007-05-21 18:33:47 +02006675 (ap->flags & ATA_FLAG_SATA) ? 'S' : 'P',
Tejun Heof3187192007-04-17 23:44:07 +09006676 ata_mode_string(xfer_mask),
Tejun Heocbcdd872007-08-18 13:14:55 +09006677 ap->link.eh_info.desc);
Tejun Heof3187192007-04-17 23:44:07 +09006678 else
6679 ata_port_printk(ap, KERN_INFO, "DUMMY\n");
6680 }
6681
6682 /* perform each probe synchronously */
6683 DPRINTK("probe begin\n");
6684 for (i = 0; i < host->n_ports; i++) {
6685 struct ata_port *ap = host->ports[i];
6686 int rc;
6687
6688 /* probe */
Tejun Heo52783c52006-05-31 18:28:22 +09006689 if (ap->ops->error_handler) {
Tejun Heo9af5c9c2007-08-06 18:36:22 +09006690 struct ata_eh_info *ehi = &ap->link.eh_info;
Tejun Heo3e706392006-05-31 18:28:11 +09006691 unsigned long flags;
6692
6693 ata_port_probe(ap);
6694
6695 /* kick EH for boot probing */
Jeff Garzikba6a1302006-06-22 23:46:10 -04006696 spin_lock_irqsave(ap->lock, flags);
Tejun Heo3e706392006-05-31 18:28:11 +09006697
Tejun Heof58229f2007-08-06 18:36:23 +09006698 ehi->probe_mask =
6699 (1 << ata_link_max_devices(&ap->link)) - 1;
Tejun Heo1cdaf532006-07-03 16:07:26 +09006700 ehi->action |= ATA_EH_SOFTRESET;
6701 ehi->flags |= ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET;
Tejun Heo3e706392006-05-31 18:28:11 +09006702
Tejun Heof4d6d002007-05-01 11:50:15 +02006703 ap->pflags &= ~ATA_PFLAG_INITIALIZING;
Tejun Heob51e9e52006-06-29 01:29:30 +09006704 ap->pflags |= ATA_PFLAG_LOADING;
Tejun Heo3e706392006-05-31 18:28:11 +09006705 ata_port_schedule_eh(ap);
6706
Jeff Garzikba6a1302006-06-22 23:46:10 -04006707 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo3e706392006-05-31 18:28:11 +09006708
6709 /* wait for EH to finish */
6710 ata_port_wait_eh(ap);
6711 } else {
Tejun Heo44877b42007-02-21 01:06:51 +09006712 DPRINTK("ata%u: bus probe begin\n", ap->print_id);
Tejun Heo3e706392006-05-31 18:28:11 +09006713 rc = ata_bus_probe(ap);
Tejun Heo44877b42007-02-21 01:06:51 +09006714 DPRINTK("ata%u: bus probe end\n", ap->print_id);
Tejun Heo3e706392006-05-31 18:28:11 +09006715
6716 if (rc) {
6717 /* FIXME: do something useful here?
6718 * Current libata behavior will
6719 * tear down everything when
6720 * the module is removed
6721 * or the h/w is unplugged.
6722 */
6723 }
6724 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006725 }
6726
6727 /* probes are done, now scan each port's disk(s) */
Randy Dunlapc893a3a2006-01-28 13:15:32 -05006728 DPRINTK("host probe begin\n");
Jeff Garzikcca39742006-08-24 03:19:22 -04006729 for (i = 0; i < host->n_ports; i++) {
6730 struct ata_port *ap = host->ports[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006731
Tejun Heo1ae46312007-07-16 14:29:40 +09006732 ata_scsi_scan_host(ap, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006733 }
6734
Tejun Heof3187192007-04-17 23:44:07 +09006735 return 0;
6736}
6737
6738/**
Tejun Heof5cda252007-04-17 23:44:07 +09006739 * ata_host_activate - start host, request IRQ and register it
6740 * @host: target ATA host
6741 * @irq: IRQ to request
6742 * @irq_handler: irq_handler used when requesting IRQ
6743 * @irq_flags: irq_flags used when requesting IRQ
6744 * @sht: scsi_host_template to use when registering the host
6745 *
6746 * After allocating an ATA host and initializing it, most libata
6747 * LLDs perform three steps to activate the host - start host,
6748 * request IRQ and register it. This helper takes necessasry
6749 * arguments and performs the three steps in one go.
6750 *
6751 * LOCKING:
6752 * Inherited from calling layer (may sleep).
6753 *
6754 * RETURNS:
6755 * 0 on success, -errno otherwise.
6756 */
6757int ata_host_activate(struct ata_host *host, int irq,
6758 irq_handler_t irq_handler, unsigned long irq_flags,
6759 struct scsi_host_template *sht)
6760{
Tejun Heocbcdd872007-08-18 13:14:55 +09006761 int i, rc;
Tejun Heof5cda252007-04-17 23:44:07 +09006762
6763 rc = ata_host_start(host);
6764 if (rc)
6765 return rc;
6766
6767 rc = devm_request_irq(host->dev, irq, irq_handler, irq_flags,
6768 dev_driver_string(host->dev), host);
6769 if (rc)
6770 return rc;
6771
Tejun Heocbcdd872007-08-18 13:14:55 +09006772 for (i = 0; i < host->n_ports; i++)
6773 ata_port_desc(host->ports[i], "irq %d", irq);
Tejun Heo40318262007-07-03 01:38:47 +09006774
Tejun Heof5cda252007-04-17 23:44:07 +09006775 rc = ata_host_register(host, sht);
6776 /* if failed, just free the IRQ and leave ports alone */
6777 if (rc)
6778 devm_free_irq(host->dev, irq, host);
6779
6780 return rc;
6781}
6782
6783/**
Tejun Heo720ba122006-05-31 18:28:13 +09006784 * ata_port_detach - Detach ATA port in prepration of device removal
6785 * @ap: ATA port to be detached
6786 *
6787 * Detach all ATA devices and the associated SCSI devices of @ap;
6788 * then, remove the associated SCSI host. @ap is guaranteed to
6789 * be quiescent on return from this function.
6790 *
6791 * LOCKING:
6792 * Kernel thread context (may sleep).
6793 */
6794void ata_port_detach(struct ata_port *ap)
6795{
6796 unsigned long flags;
Tejun Heo41bda9c2007-08-06 18:36:24 +09006797 struct ata_link *link;
Tejun Heof58229f2007-08-06 18:36:23 +09006798 struct ata_device *dev;
Tejun Heo720ba122006-05-31 18:28:13 +09006799
6800 if (!ap->ops->error_handler)
Tejun Heoc3cf30a2006-08-05 03:59:11 +09006801 goto skip_eh;
Tejun Heo720ba122006-05-31 18:28:13 +09006802
6803 /* tell EH we're leaving & flush EH */
Jeff Garzikba6a1302006-06-22 23:46:10 -04006804 spin_lock_irqsave(ap->lock, flags);
Tejun Heob51e9e52006-06-29 01:29:30 +09006805 ap->pflags |= ATA_PFLAG_UNLOADING;
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);
6809
6810 /* EH is now guaranteed to see UNLOADING, so no new device
6811 * will be attached. Disable all existing devices.
6812 */
Jeff Garzikba6a1302006-06-22 23:46:10 -04006813 spin_lock_irqsave(ap->lock, flags);
Tejun Heo720ba122006-05-31 18:28:13 +09006814
Tejun Heo41bda9c2007-08-06 18:36:24 +09006815 ata_port_for_each_link(link, ap) {
6816 ata_link_for_each_dev(dev, link)
6817 ata_dev_disable(dev);
6818 }
Tejun Heo720ba122006-05-31 18:28:13 +09006819
Jeff Garzikba6a1302006-06-22 23:46:10 -04006820 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo720ba122006-05-31 18:28:13 +09006821
6822 /* Final freeze & EH. All in-flight commands are aborted. EH
6823 * will be skipped and retrials will be terminated with bad
6824 * target.
6825 */
Jeff Garzikba6a1302006-06-22 23:46:10 -04006826 spin_lock_irqsave(ap->lock, flags);
Tejun Heo720ba122006-05-31 18:28:13 +09006827 ata_port_freeze(ap); /* won't be thawed */
Jeff Garzikba6a1302006-06-22 23:46:10 -04006828 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo720ba122006-05-31 18:28:13 +09006829
6830 ata_port_wait_eh(ap);
Oleg Nesterov45a66c12007-07-09 11:46:13 -07006831 cancel_rearming_delayed_work(&ap->hotplug_task);
Tejun Heo720ba122006-05-31 18:28:13 +09006832
Tejun Heoc3cf30a2006-08-05 03:59:11 +09006833 skip_eh:
Tejun Heo720ba122006-05-31 18:28:13 +09006834 /* remove the associated SCSI host */
Jeff Garzikcca39742006-08-24 03:19:22 -04006835 scsi_remove_host(ap->scsi_host);
Tejun Heo720ba122006-05-31 18:28:13 +09006836}
6837
6838/**
Tejun Heo0529c1592007-01-20 16:00:26 +09006839 * ata_host_detach - Detach all ports of an ATA host
6840 * @host: Host to detach
6841 *
6842 * Detach all ports of @host.
6843 *
6844 * LOCKING:
6845 * Kernel thread context (may sleep).
6846 */
6847void ata_host_detach(struct ata_host *host)
6848{
6849 int i;
6850
6851 for (i = 0; i < host->n_ports; i++)
6852 ata_port_detach(host->ports[i]);
6853}
6854
Linus Torvalds1da177e2005-04-16 15:20:36 -07006855/**
6856 * ata_std_ports - initialize ioaddr with standard port offsets.
6857 * @ioaddr: IO address structure to be initialized
Edward Falk0baab862005-06-02 18:17:13 -04006858 *
6859 * Utility function which initializes data_addr, error_addr,
6860 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
6861 * device_addr, status_addr, and command_addr to standard offsets
6862 * relative to cmd_addr.
6863 *
6864 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006865 */
Edward Falk0baab862005-06-02 18:17:13 -04006866
Linus Torvalds1da177e2005-04-16 15:20:36 -07006867void ata_std_ports(struct ata_ioports *ioaddr)
6868{
6869 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
6870 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
6871 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
6872 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
6873 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
6874 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
6875 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
6876 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
6877 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
6878 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
6879}
6880
Edward Falk0baab862005-06-02 18:17:13 -04006881
Jeff Garzik374b1872005-08-30 05:42:52 -04006882#ifdef CONFIG_PCI
6883
Edward Falk0baab862005-06-02 18:17:13 -04006884/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07006885 * ata_pci_remove_one - PCI layer callback for device removal
6886 * @pdev: PCI device that was removed
6887 *
Tejun Heob878ca52007-01-20 16:00:28 +09006888 * PCI layer indicates to libata via this hook that hot-unplug or
6889 * module unload event has occurred. Detach all ports. Resource
6890 * release is handled via devres.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006891 *
6892 * LOCKING:
6893 * Inherited from PCI layer (may sleep).
6894 */
Tejun Heof0d36ef2007-01-20 16:00:28 +09006895void ata_pci_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006896{
6897 struct device *dev = pci_dev_to_dev(pdev);
Jeff Garzikcca39742006-08-24 03:19:22 -04006898 struct ata_host *host = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006899
Tejun Heob878ca52007-01-20 16:00:28 +09006900 ata_host_detach(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006901}
6902
6903/* move to PCI subsystem */
Jeff Garzik057ace52005-10-22 14:27:05 -04006904int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006905{
6906 unsigned long tmp = 0;
6907
6908 switch (bits->width) {
6909 case 1: {
6910 u8 tmp8 = 0;
6911 pci_read_config_byte(pdev, bits->reg, &tmp8);
6912 tmp = tmp8;
6913 break;
6914 }
6915 case 2: {
6916 u16 tmp16 = 0;
6917 pci_read_config_word(pdev, bits->reg, &tmp16);
6918 tmp = tmp16;
6919 break;
6920 }
6921 case 4: {
6922 u32 tmp32 = 0;
6923 pci_read_config_dword(pdev, bits->reg, &tmp32);
6924 tmp = tmp32;
6925 break;
6926 }
6927
6928 default:
6929 return -EINVAL;
6930 }
6931
6932 tmp &= bits->mask;
6933
6934 return (tmp == bits->val) ? 1 : 0;
6935}
Jens Axboe9b847542006-01-06 09:28:07 +01006936
Tejun Heo6ffa01d2007-03-02 17:32:47 +09006937#ifdef CONFIG_PM
Tejun Heo3c5100c2006-07-26 16:58:33 +09006938void ata_pci_device_do_suspend(struct pci_dev *pdev, pm_message_t mesg)
Jens Axboe9b847542006-01-06 09:28:07 +01006939{
6940 pci_save_state(pdev);
Tejun Heo4c90d972007-02-20 18:14:48 +09006941 pci_disable_device(pdev);
Tejun Heo500530f2006-07-03 16:07:27 +09006942
Tejun Heo4c90d972007-02-20 18:14:48 +09006943 if (mesg.event == PM_EVENT_SUSPEND)
Tejun Heo500530f2006-07-03 16:07:27 +09006944 pci_set_power_state(pdev, PCI_D3hot);
Jens Axboe9b847542006-01-06 09:28:07 +01006945}
6946
Tejun Heo553c4aa2006-12-26 19:39:50 +09006947int ata_pci_device_do_resume(struct pci_dev *pdev)
Jens Axboe9b847542006-01-06 09:28:07 +01006948{
Tejun Heo553c4aa2006-12-26 19:39:50 +09006949 int rc;
6950
Jens Axboe9b847542006-01-06 09:28:07 +01006951 pci_set_power_state(pdev, PCI_D0);
6952 pci_restore_state(pdev);
Tejun Heo553c4aa2006-12-26 19:39:50 +09006953
Tejun Heob878ca52007-01-20 16:00:28 +09006954 rc = pcim_enable_device(pdev);
Tejun Heo553c4aa2006-12-26 19:39:50 +09006955 if (rc) {
6956 dev_printk(KERN_ERR, &pdev->dev,
6957 "failed to enable device after resume (%d)\n", rc);
6958 return rc;
6959 }
6960
Jens Axboe9b847542006-01-06 09:28:07 +01006961 pci_set_master(pdev);
Tejun Heo553c4aa2006-12-26 19:39:50 +09006962 return 0;
Tejun Heo500530f2006-07-03 16:07:27 +09006963}
6964
Tejun Heo3c5100c2006-07-26 16:58:33 +09006965int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg)
Tejun Heo500530f2006-07-03 16:07:27 +09006966{
Jeff Garzikcca39742006-08-24 03:19:22 -04006967 struct ata_host *host = dev_get_drvdata(&pdev->dev);
Tejun Heo500530f2006-07-03 16:07:27 +09006968 int rc = 0;
6969
Jeff Garzikcca39742006-08-24 03:19:22 -04006970 rc = ata_host_suspend(host, mesg);
Tejun Heo500530f2006-07-03 16:07:27 +09006971 if (rc)
6972 return rc;
6973
Tejun Heo3c5100c2006-07-26 16:58:33 +09006974 ata_pci_device_do_suspend(pdev, mesg);
Tejun Heo500530f2006-07-03 16:07:27 +09006975
6976 return 0;
6977}
6978
6979int ata_pci_device_resume(struct pci_dev *pdev)
6980{
Jeff Garzikcca39742006-08-24 03:19:22 -04006981 struct ata_host *host = dev_get_drvdata(&pdev->dev);
Tejun Heo553c4aa2006-12-26 19:39:50 +09006982 int rc;
Tejun Heo500530f2006-07-03 16:07:27 +09006983
Tejun Heo553c4aa2006-12-26 19:39:50 +09006984 rc = ata_pci_device_do_resume(pdev);
6985 if (rc == 0)
6986 ata_host_resume(host);
6987 return rc;
Jens Axboe9b847542006-01-06 09:28:07 +01006988}
Tejun Heo6ffa01d2007-03-02 17:32:47 +09006989#endif /* CONFIG_PM */
6990
Linus Torvalds1da177e2005-04-16 15:20:36 -07006991#endif /* CONFIG_PCI */
6992
6993
Linus Torvalds1da177e2005-04-16 15:20:36 -07006994static int __init ata_init(void)
6995{
Andrew Mortona8601e52006-06-25 01:36:52 -07006996 ata_probe_timeout *= HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006997 ata_wq = create_workqueue("ata");
6998 if (!ata_wq)
6999 return -ENOMEM;
7000
Tejun Heo453b07a2006-05-31 18:27:42 +09007001 ata_aux_wq = create_singlethread_workqueue("ata_aux");
7002 if (!ata_aux_wq) {
7003 destroy_workqueue(ata_wq);
7004 return -ENOMEM;
7005 }
7006
Linus Torvalds1da177e2005-04-16 15:20:36 -07007007 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
7008 return 0;
7009}
7010
7011static void __exit ata_exit(void)
7012{
7013 destroy_workqueue(ata_wq);
Tejun Heo453b07a2006-05-31 18:27:42 +09007014 destroy_workqueue(ata_aux_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007015}
7016
Brian Kinga4625082006-11-13 16:32:36 -06007017subsys_initcall(ata_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007018module_exit(ata_exit);
7019
Jeff Garzik67846b32005-10-05 02:58:32 -04007020static unsigned long ratelimit_time;
Ingo Molnar34af9462006-06-27 02:53:55 -07007021static DEFINE_SPINLOCK(ata_ratelimit_lock);
Jeff Garzik67846b32005-10-05 02:58:32 -04007022
7023int ata_ratelimit(void)
7024{
7025 int rc;
7026 unsigned long flags;
7027
7028 spin_lock_irqsave(&ata_ratelimit_lock, flags);
7029
7030 if (time_after(jiffies, ratelimit_time)) {
7031 rc = 1;
7032 ratelimit_time = jiffies + (HZ/5);
7033 } else
7034 rc = 0;
7035
7036 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
7037
7038 return rc;
7039}
7040
Tejun Heoc22daff2006-04-11 22:22:29 +09007041/**
7042 * ata_wait_register - wait until register value changes
7043 * @reg: IO-mapped register
7044 * @mask: Mask to apply to read register value
7045 * @val: Wait condition
7046 * @interval_msec: polling interval in milliseconds
7047 * @timeout_msec: timeout in milliseconds
7048 *
7049 * Waiting for some bits of register to change is a common
7050 * operation for ATA controllers. This function reads 32bit LE
7051 * IO-mapped register @reg and tests for the following condition.
7052 *
7053 * (*@reg & mask) != val
7054 *
7055 * If the condition is met, it returns; otherwise, the process is
7056 * repeated after @interval_msec until timeout.
7057 *
7058 * LOCKING:
7059 * Kernel thread context (may sleep)
7060 *
7061 * RETURNS:
7062 * The final register value.
7063 */
7064u32 ata_wait_register(void __iomem *reg, u32 mask, u32 val,
7065 unsigned long interval_msec,
7066 unsigned long timeout_msec)
7067{
7068 unsigned long timeout;
7069 u32 tmp;
7070
7071 tmp = ioread32(reg);
7072
7073 /* Calculate timeout _after_ the first read to make sure
7074 * preceding writes reach the controller before starting to
7075 * eat away the timeout.
7076 */
7077 timeout = jiffies + (timeout_msec * HZ) / 1000;
7078
7079 while ((tmp & mask) == val && time_before(jiffies, timeout)) {
7080 msleep(interval_msec);
7081 tmp = ioread32(reg);
7082 }
7083
7084 return tmp;
7085}
7086
Linus Torvalds1da177e2005-04-16 15:20:36 -07007087/*
Tejun Heodd5b06c2006-08-10 16:59:12 +09007088 * Dummy port_ops
7089 */
7090static void ata_dummy_noret(struct ata_port *ap) { }
7091static int ata_dummy_ret0(struct ata_port *ap) { return 0; }
7092static void ata_dummy_qc_noret(struct ata_queued_cmd *qc) { }
7093
7094static u8 ata_dummy_check_status(struct ata_port *ap)
7095{
7096 return ATA_DRDY;
7097}
7098
7099static unsigned int ata_dummy_qc_issue(struct ata_queued_cmd *qc)
7100{
7101 return AC_ERR_SYSTEM;
7102}
7103
7104const struct ata_port_operations ata_dummy_port_ops = {
Tejun Heodd5b06c2006-08-10 16:59:12 +09007105 .check_status = ata_dummy_check_status,
7106 .check_altstatus = ata_dummy_check_status,
7107 .dev_select = ata_noop_dev_select,
7108 .qc_prep = ata_noop_qc_prep,
7109 .qc_issue = ata_dummy_qc_issue,
7110 .freeze = ata_dummy_noret,
7111 .thaw = ata_dummy_noret,
7112 .error_handler = ata_dummy_noret,
7113 .post_internal_cmd = ata_dummy_qc_noret,
7114 .irq_clear = ata_dummy_noret,
7115 .port_start = ata_dummy_ret0,
7116 .port_stop = ata_dummy_noret,
7117};
7118
Tejun Heo21b0ad42007-04-17 23:44:07 +09007119const struct ata_port_info ata_dummy_port_info = {
7120 .port_ops = &ata_dummy_port_ops,
7121};
7122
Tejun Heodd5b06c2006-08-10 16:59:12 +09007123/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07007124 * libata is essentially a library of internal helper functions for
7125 * low-level ATA host controller drivers. As such, the API/ABI is
7126 * likely to change as new drivers are added and updated.
7127 * Do not depend on ABI/API stability.
7128 */
7129
Tejun Heoe9c83912006-07-03 16:07:26 +09007130EXPORT_SYMBOL_GPL(sata_deb_timing_normal);
7131EXPORT_SYMBOL_GPL(sata_deb_timing_hotplug);
7132EXPORT_SYMBOL_GPL(sata_deb_timing_long);
Tejun Heodd5b06c2006-08-10 16:59:12 +09007133EXPORT_SYMBOL_GPL(ata_dummy_port_ops);
Tejun Heo21b0ad42007-04-17 23:44:07 +09007134EXPORT_SYMBOL_GPL(ata_dummy_port_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007135EXPORT_SYMBOL_GPL(ata_std_bios_param);
7136EXPORT_SYMBOL_GPL(ata_std_ports);
Jeff Garzikcca39742006-08-24 03:19:22 -04007137EXPORT_SYMBOL_GPL(ata_host_init);
Tejun Heof3187192007-04-17 23:44:07 +09007138EXPORT_SYMBOL_GPL(ata_host_alloc);
Tejun Heof5cda252007-04-17 23:44:07 +09007139EXPORT_SYMBOL_GPL(ata_host_alloc_pinfo);
Tejun Heoecef7252007-04-17 23:44:06 +09007140EXPORT_SYMBOL_GPL(ata_host_start);
Tejun Heof3187192007-04-17 23:44:07 +09007141EXPORT_SYMBOL_GPL(ata_host_register);
Tejun Heof5cda252007-04-17 23:44:07 +09007142EXPORT_SYMBOL_GPL(ata_host_activate);
Tejun Heo0529c1592007-01-20 16:00:26 +09007143EXPORT_SYMBOL_GPL(ata_host_detach);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007144EXPORT_SYMBOL_GPL(ata_sg_init);
7145EXPORT_SYMBOL_GPL(ata_sg_init_one);
Tejun Heo9a1004d2006-05-31 18:27:52 +09007146EXPORT_SYMBOL_GPL(ata_hsm_move);
Tejun Heof686bcb2006-05-15 20:58:05 +09007147EXPORT_SYMBOL_GPL(ata_qc_complete);
Tejun Heodedaf2b2006-05-15 21:03:43 +09007148EXPORT_SYMBOL_GPL(ata_qc_complete_multiple);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007149EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007150EXPORT_SYMBOL_GPL(ata_tf_load);
7151EXPORT_SYMBOL_GPL(ata_tf_read);
7152EXPORT_SYMBOL_GPL(ata_noop_dev_select);
7153EXPORT_SYMBOL_GPL(ata_std_dev_select);
Jeff Garzik43727fb2007-02-25 16:50:52 -05007154EXPORT_SYMBOL_GPL(sata_print_link_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007155EXPORT_SYMBOL_GPL(ata_tf_to_fis);
7156EXPORT_SYMBOL_GPL(ata_tf_from_fis);
7157EXPORT_SYMBOL_GPL(ata_check_status);
7158EXPORT_SYMBOL_GPL(ata_altstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007159EXPORT_SYMBOL_GPL(ata_exec_command);
7160EXPORT_SYMBOL_GPL(ata_port_start);
Alan Coxd92e74d2007-06-07 16:19:15 +01007161EXPORT_SYMBOL_GPL(ata_sff_port_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007162EXPORT_SYMBOL_GPL(ata_interrupt);
Alan04351822007-03-06 02:37:52 -08007163EXPORT_SYMBOL_GPL(ata_do_set_mode);
Tejun Heo0d5ff562007-02-01 15:06:36 +09007164EXPORT_SYMBOL_GPL(ata_data_xfer);
7165EXPORT_SYMBOL_GPL(ata_data_xfer_noirq);
Tejun Heo31cc23b2007-09-23 13:14:12 +09007166EXPORT_SYMBOL_GPL(ata_std_qc_defer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007167EXPORT_SYMBOL_GPL(ata_qc_prep);
Alan Coxd26fc952007-07-06 19:13:52 -04007168EXPORT_SYMBOL_GPL(ata_dumb_qc_prep);
Brian Kinge46834c2006-03-17 17:04:03 -06007169EXPORT_SYMBOL_GPL(ata_noop_qc_prep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007170EXPORT_SYMBOL_GPL(ata_bmdma_setup);
7171EXPORT_SYMBOL_GPL(ata_bmdma_start);
7172EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
7173EXPORT_SYMBOL_GPL(ata_bmdma_status);
7174EXPORT_SYMBOL_GPL(ata_bmdma_stop);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09007175EXPORT_SYMBOL_GPL(ata_bmdma_freeze);
7176EXPORT_SYMBOL_GPL(ata_bmdma_thaw);
7177EXPORT_SYMBOL_GPL(ata_bmdma_drive_eh);
7178EXPORT_SYMBOL_GPL(ata_bmdma_error_handler);
7179EXPORT_SYMBOL_GPL(ata_bmdma_post_internal_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007180EXPORT_SYMBOL_GPL(ata_port_probe);
Alan10305f02007-02-20 18:01:59 +00007181EXPORT_SYMBOL_GPL(ata_dev_disable);
Tejun Heo3c567b72006-05-15 20:57:23 +09007182EXPORT_SYMBOL_GPL(sata_set_spd);
Tejun Heo936fd732007-08-06 18:36:23 +09007183EXPORT_SYMBOL_GPL(sata_link_debounce);
7184EXPORT_SYMBOL_GPL(sata_link_resume);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007185EXPORT_SYMBOL_GPL(sata_phy_reset);
7186EXPORT_SYMBOL_GPL(__sata_phy_reset);
7187EXPORT_SYMBOL_GPL(ata_bus_reset);
Tejun Heof5914a42006-05-31 18:27:48 +09007188EXPORT_SYMBOL_GPL(ata_std_prereset);
Tejun Heoc2bd5802006-01-24 17:05:22 +09007189EXPORT_SYMBOL_GPL(ata_std_softreset);
Tejun Heocc0680a2007-08-06 18:36:23 +09007190EXPORT_SYMBOL_GPL(sata_link_hardreset);
Tejun Heoc2bd5802006-01-24 17:05:22 +09007191EXPORT_SYMBOL_GPL(sata_std_hardreset);
7192EXPORT_SYMBOL_GPL(ata_std_postreset);
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05007193EXPORT_SYMBOL_GPL(ata_dev_classify);
7194EXPORT_SYMBOL_GPL(ata_dev_pair);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007195EXPORT_SYMBOL_GPL(ata_port_disable);
Jeff Garzik67846b32005-10-05 02:58:32 -04007196EXPORT_SYMBOL_GPL(ata_ratelimit);
Tejun Heoc22daff2006-04-11 22:22:29 +09007197EXPORT_SYMBOL_GPL(ata_wait_register);
Tejun Heo6f8b9952006-01-24 17:05:21 +09007198EXPORT_SYMBOL_GPL(ata_busy_sleep);
Tejun Heod4b2bab2007-02-02 16:50:52 +09007199EXPORT_SYMBOL_GPL(ata_wait_ready);
Tejun Heo86e45b62006-03-05 15:29:09 +09007200EXPORT_SYMBOL_GPL(ata_port_queue_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007201EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
7202EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007203EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
Tejun Heo83c47bc2006-05-31 18:28:07 +09007204EXPORT_SYMBOL_GPL(ata_scsi_slave_destroy);
Tejun Heoa6e6ce82006-05-15 21:03:48 +09007205EXPORT_SYMBOL_GPL(ata_scsi_change_queue_depth);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007206EXPORT_SYMBOL_GPL(ata_host_intr);
Tejun Heo34bf2172006-05-15 20:57:46 +09007207EXPORT_SYMBOL_GPL(sata_scr_valid);
7208EXPORT_SYMBOL_GPL(sata_scr_read);
7209EXPORT_SYMBOL_GPL(sata_scr_write);
7210EXPORT_SYMBOL_GPL(sata_scr_write_flush);
Tejun Heo936fd732007-08-06 18:36:23 +09007211EXPORT_SYMBOL_GPL(ata_link_online);
7212EXPORT_SYMBOL_GPL(ata_link_offline);
Tejun Heo6ffa01d2007-03-02 17:32:47 +09007213#ifdef CONFIG_PM
Jeff Garzikcca39742006-08-24 03:19:22 -04007214EXPORT_SYMBOL_GPL(ata_host_suspend);
7215EXPORT_SYMBOL_GPL(ata_host_resume);
Tejun Heo6ffa01d2007-03-02 17:32:47 +09007216#endif /* CONFIG_PM */
Tejun Heo6a62a042006-02-13 10:02:46 +09007217EXPORT_SYMBOL_GPL(ata_id_string);
7218EXPORT_SYMBOL_GPL(ata_id_c_string);
Alan10305f02007-02-20 18:01:59 +00007219EXPORT_SYMBOL_GPL(ata_id_to_dma_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007220EXPORT_SYMBOL_GPL(ata_scsi_simulate);
7221
Alan Cox1bc4ccf2006-01-09 17:18:14 +00007222EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
Alan Cox452503f2005-10-21 19:01:32 -04007223EXPORT_SYMBOL_GPL(ata_timing_compute);
7224EXPORT_SYMBOL_GPL(ata_timing_merge);
7225
Linus Torvalds1da177e2005-04-16 15:20:36 -07007226#ifdef CONFIG_PCI
7227EXPORT_SYMBOL_GPL(pci_test_config_bits);
Tejun Heod583bc12007-07-04 18:02:07 +09007228EXPORT_SYMBOL_GPL(ata_pci_init_sff_host);
Tejun Heo1626aeb2007-05-04 12:43:58 +02007229EXPORT_SYMBOL_GPL(ata_pci_init_bmdma);
Tejun Heod583bc12007-07-04 18:02:07 +09007230EXPORT_SYMBOL_GPL(ata_pci_prepare_sff_host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007231EXPORT_SYMBOL_GPL(ata_pci_init_one);
7232EXPORT_SYMBOL_GPL(ata_pci_remove_one);
Tejun Heo6ffa01d2007-03-02 17:32:47 +09007233#ifdef CONFIG_PM
Tejun Heo500530f2006-07-03 16:07:27 +09007234EXPORT_SYMBOL_GPL(ata_pci_device_do_suspend);
7235EXPORT_SYMBOL_GPL(ata_pci_device_do_resume);
Jens Axboe9b847542006-01-06 09:28:07 +01007236EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
7237EXPORT_SYMBOL_GPL(ata_pci_device_resume);
Tejun Heo6ffa01d2007-03-02 17:32:47 +09007238#endif /* CONFIG_PM */
Alan Cox67951ad2006-03-22 15:55:54 +00007239EXPORT_SYMBOL_GPL(ata_pci_default_filter);
7240EXPORT_SYMBOL_GPL(ata_pci_clear_simplex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007241#endif /* CONFIG_PCI */
Jens Axboe9b847542006-01-06 09:28:07 +01007242
Tejun Heob64bbc32007-07-16 14:29:39 +09007243EXPORT_SYMBOL_GPL(__ata_ehi_push_desc);
7244EXPORT_SYMBOL_GPL(ata_ehi_push_desc);
7245EXPORT_SYMBOL_GPL(ata_ehi_clear_desc);
Tejun Heocbcdd872007-08-18 13:14:55 +09007246EXPORT_SYMBOL_GPL(ata_port_desc);
7247#ifdef CONFIG_PCI
7248EXPORT_SYMBOL_GPL(ata_port_pbar_desc);
7249#endif /* CONFIG_PCI */
Tejun Heoece1d632006-04-02 18:51:53 +09007250EXPORT_SYMBOL_GPL(ata_eng_timeout);
Tejun Heo7b70fc02006-05-15 20:58:07 +09007251EXPORT_SYMBOL_GPL(ata_port_schedule_eh);
Tejun Heodbd82612007-08-06 18:36:23 +09007252EXPORT_SYMBOL_GPL(ata_link_abort);
Tejun Heo7b70fc02006-05-15 20:58:07 +09007253EXPORT_SYMBOL_GPL(ata_port_abort);
Tejun Heoe3180492006-05-15 20:58:09 +09007254EXPORT_SYMBOL_GPL(ata_port_freeze);
Tejun Heo7d77b242007-09-23 13:14:13 +09007255EXPORT_SYMBOL_GPL(sata_async_notification);
Tejun Heoe3180492006-05-15 20:58:09 +09007256EXPORT_SYMBOL_GPL(ata_eh_freeze_port);
7257EXPORT_SYMBOL_GPL(ata_eh_thaw_port);
Tejun Heoece1d632006-04-02 18:51:53 +09007258EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
7259EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
Tejun Heo022bdb02006-05-15 20:58:22 +09007260EXPORT_SYMBOL_GPL(ata_do_eh);
Akira Iguchi83625002007-01-26 16:27:32 +09007261EXPORT_SYMBOL_GPL(ata_irq_on);
Akira Iguchia619f981b2007-01-26 16:28:18 +09007262EXPORT_SYMBOL_GPL(ata_dev_try_classify);
Alan Coxbe0d18d2007-03-06 02:37:56 -08007263
7264EXPORT_SYMBOL_GPL(ata_cable_40wire);
7265EXPORT_SYMBOL_GPL(ata_cable_80wire);
7266EXPORT_SYMBOL_GPL(ata_cable_unknown);
7267EXPORT_SYMBOL_GPL(ata_cable_sata);