blob: eb9709864a21abd009c23fa81fc52ed5bccb0618 [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);
Alan Cox93328e12007-09-29 04:06:48 -0400918 if (dev->horkage & ATA_HORKAGE_HPA_SIZE)
919 (*max_sectors)--;
Tejun Heoc728a912007-09-03 12:32:30 +0900920 return 0;
Alan Cox1e999732007-04-11 00:23:13 +0100921}
922
923/**
Tejun Heoc728a912007-09-03 12:32:30 +0900924 * ata_set_max_sectors - Set max sectors
925 * @dev: target device
Randy Dunlap6b38d1d2007-05-01 17:35:55 -0700926 * @new_sectors: new max sectors value to set for the device
Alan Cox1e999732007-04-11 00:23:13 +0100927 *
Tejun Heoc728a912007-09-03 12:32:30 +0900928 * Set max sectors of @dev to @new_sectors.
929 *
930 * RETURNS:
931 * 0 on success, -EACCES if command is aborted or denied (due to
932 * previous non-volatile SET_MAX) by the drive. -EIO on other
933 * errors.
Alan Cox1e999732007-04-11 00:23:13 +0100934 */
Tejun Heo05027ad2007-09-03 12:32:57 +0900935static int ata_set_max_sectors(struct ata_device *dev, u64 new_sectors)
Alan Cox1e999732007-04-11 00:23:13 +0100936{
Tejun Heoc728a912007-09-03 12:32:30 +0900937 unsigned int err_mask;
Alan Cox1e999732007-04-11 00:23:13 +0100938 struct ata_taskfile tf;
Tejun Heoc728a912007-09-03 12:32:30 +0900939 int lba48 = ata_id_has_lba48(dev->id);
Alan Cox1e999732007-04-11 00:23:13 +0100940
941 new_sectors--;
942
943 ata_tf_init(dev, &tf);
944
Alan Cox1e999732007-04-11 00:23:13 +0100945 tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
Tejun Heoc728a912007-09-03 12:32:30 +0900946
947 if (lba48) {
948 tf.command = ATA_CMD_SET_MAX_EXT;
949 tf.flags |= ATA_TFLAG_LBA48;
950
951 tf.hob_lbal = (new_sectors >> 24) & 0xff;
952 tf.hob_lbam = (new_sectors >> 32) & 0xff;
953 tf.hob_lbah = (new_sectors >> 40) & 0xff;
Tejun Heo1e582ba2007-09-21 20:07:14 +0900954 } else {
Tejun Heoc728a912007-09-03 12:32:30 +0900955 tf.command = ATA_CMD_SET_MAX;
956
Tejun Heo1e582ba2007-09-21 20:07:14 +0900957 tf.device |= (new_sectors >> 24) & 0xf;
958 }
959
Alan Cox1e999732007-04-11 00:23:13 +0100960 tf.protocol |= ATA_PROT_NODATA;
Tejun Heoc728a912007-09-03 12:32:30 +0900961 tf.device |= ATA_LBA;
Alan Cox1e999732007-04-11 00:23:13 +0100962
963 tf.lbal = (new_sectors >> 0) & 0xff;
964 tf.lbam = (new_sectors >> 8) & 0xff;
965 tf.lbah = (new_sectors >> 16) & 0xff;
Alan Cox1e999732007-04-11 00:23:13 +0100966
Tejun Heoc728a912007-09-03 12:32:30 +0900967 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
968 if (err_mask) {
969 ata_dev_printk(dev, KERN_WARNING, "failed to set "
970 "max address (err_mask=0x%x)\n", err_mask);
971 if (err_mask == AC_ERR_DEV &&
972 (tf.feature & (ATA_ABORTED | ATA_IDNF)))
973 return -EACCES;
974 return -EIO;
975 }
Alan Cox1e999732007-04-11 00:23:13 +0100976
Tejun Heoc728a912007-09-03 12:32:30 +0900977 return 0;
Alan Cox1e999732007-04-11 00:23:13 +0100978}
979
980/**
981 * ata_hpa_resize - Resize a device with an HPA set
982 * @dev: Device to resize
983 *
984 * Read the size of an LBA28 or LBA48 disk with HPA features and resize
985 * it if required to the full size of the media. The caller must check
986 * the drive has the HPA feature set enabled.
Tejun Heo05027ad2007-09-03 12:32:57 +0900987 *
988 * RETURNS:
989 * 0 on success, -errno on failure.
Alan Cox1e999732007-04-11 00:23:13 +0100990 */
Tejun Heo05027ad2007-09-03 12:32:57 +0900991static int ata_hpa_resize(struct ata_device *dev)
Alan Cox1e999732007-04-11 00:23:13 +0100992{
Tejun Heo05027ad2007-09-03 12:32:57 +0900993 struct ata_eh_context *ehc = &dev->link->eh_context;
994 int print_info = ehc->i.flags & ATA_EHI_PRINTINFO;
995 u64 sectors = ata_id_n_sectors(dev->id);
996 u64 native_sectors;
Tejun Heoc728a912007-09-03 12:32:30 +0900997 int rc;
Jeff Garzika617c092007-05-21 20:14:23 -0400998
Tejun Heo05027ad2007-09-03 12:32:57 +0900999 /* do we need to do it? */
1000 if (dev->class != ATA_DEV_ATA ||
1001 !ata_id_has_lba(dev->id) || !ata_id_hpa_enabled(dev->id) ||
1002 (dev->horkage & ATA_HORKAGE_BROKEN_HPA))
Tejun Heoc728a912007-09-03 12:32:30 +09001003 return 0;
Alan Cox1e999732007-04-11 00:23:13 +01001004
Tejun Heo05027ad2007-09-03 12:32:57 +09001005 /* read native max address */
1006 rc = ata_read_native_max_address(dev, &native_sectors);
1007 if (rc) {
1008 /* If HPA isn't going to be unlocked, skip HPA
1009 * resizing from the next try.
1010 */
1011 if (!ata_ignore_hpa) {
1012 ata_dev_printk(dev, KERN_WARNING, "HPA support seems "
1013 "broken, will skip HPA handling\n");
1014 dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
Alan Cox1e999732007-04-11 00:23:13 +01001015
Tejun Heo05027ad2007-09-03 12:32:57 +09001016 /* we can continue if device aborted the command */
1017 if (rc == -EACCES)
1018 rc = 0;
Alan Cox1e999732007-04-11 00:23:13 +01001019 }
Tejun Heo37301a52007-06-25 20:45:54 +09001020
Tejun Heo05027ad2007-09-03 12:32:57 +09001021 return rc;
1022 }
1023
1024 /* nothing to do? */
1025 if (native_sectors <= sectors || !ata_ignore_hpa) {
1026 if (!print_info || native_sectors == sectors)
1027 return 0;
1028
1029 if (native_sectors > sectors)
1030 ata_dev_printk(dev, KERN_INFO,
1031 "HPA detected: current %llu, native %llu\n",
1032 (unsigned long long)sectors,
1033 (unsigned long long)native_sectors);
1034 else if (native_sectors < sectors)
1035 ata_dev_printk(dev, KERN_WARNING,
1036 "native sectors (%llu) is smaller than "
1037 "sectors (%llu)\n",
1038 (unsigned long long)native_sectors,
1039 (unsigned long long)sectors);
1040 return 0;
1041 }
1042
1043 /* let's unlock HPA */
1044 rc = ata_set_max_sectors(dev, native_sectors);
1045 if (rc == -EACCES) {
1046 /* if device aborted the command, skip HPA resizing */
1047 ata_dev_printk(dev, KERN_WARNING, "device aborted resize "
1048 "(%llu -> %llu), skipping HPA handling\n",
1049 (unsigned long long)sectors,
1050 (unsigned long long)native_sectors);
1051 dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
1052 return 0;
1053 } else if (rc)
1054 return rc;
1055
1056 /* re-read IDENTIFY data */
1057 rc = ata_dev_reread_id(dev, 0);
1058 if (rc) {
1059 ata_dev_printk(dev, KERN_ERR, "failed to re-read IDENTIFY "
1060 "data after HPA resizing\n");
1061 return rc;
1062 }
1063
1064 if (print_info) {
1065 u64 new_sectors = ata_id_n_sectors(dev->id);
1066 ata_dev_printk(dev, KERN_INFO,
1067 "HPA unlocked: %llu -> %llu, native %llu\n",
1068 (unsigned long long)sectors,
1069 (unsigned long long)new_sectors,
1070 (unsigned long long)native_sectors);
1071 }
1072
1073 return 0;
Alan Cox1e999732007-04-11 00:23:13 +01001074}
1075
Edward Falk0baab862005-06-02 18:17:13 -04001076/**
Alan10305f02007-02-20 18:01:59 +00001077 * ata_id_to_dma_mode - Identify DMA mode from id block
1078 * @dev: device to identify
Randy Dunlapcc261262007-03-16 19:55:47 -07001079 * @unknown: mode to assume if we cannot tell
Alan10305f02007-02-20 18:01:59 +00001080 *
1081 * Set up the timing values for the device based upon the identify
1082 * reported values for the DMA mode. This function is used by drivers
1083 * which rely upon firmware configured modes, but wish to report the
1084 * mode correctly when possible.
1085 *
1086 * In addition we emit similarly formatted messages to the default
1087 * ata_dev_set_mode handler, in order to provide consistency of
1088 * presentation.
1089 */
1090
1091void ata_id_to_dma_mode(struct ata_device *dev, u8 unknown)
1092{
1093 unsigned int mask;
1094 u8 mode;
1095
1096 /* Pack the DMA modes */
1097 mask = ((dev->id[63] >> 8) << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA;
1098 if (dev->id[53] & 0x04)
1099 mask |= ((dev->id[88] >> 8) << ATA_SHIFT_UDMA) & ATA_MASK_UDMA;
1100
1101 /* Select the mode in use */
1102 mode = ata_xfer_mask2mode(mask);
1103
1104 if (mode != 0) {
1105 ata_dev_printk(dev, KERN_INFO, "configured for %s\n",
1106 ata_mode_string(mask));
1107 } else {
1108 /* SWDMA perhaps ? */
1109 mode = unknown;
1110 ata_dev_printk(dev, KERN_INFO, "configured for DMA\n");
1111 }
1112
1113 /* Configure the device reporting */
1114 dev->xfer_mode = mode;
1115 dev->xfer_shift = ata_xfer_mode2shift(mode);
1116}
1117
1118/**
Edward Falk0baab862005-06-02 18:17:13 -04001119 * ata_noop_dev_select - Select device 0/1 on ATA bus
1120 * @ap: ATA channel to manipulate
1121 * @device: ATA device (numbered from zero) to select
1122 *
1123 * This function performs no actual function.
1124 *
1125 * May be used as the dev_select() entry in ata_port_operations.
1126 *
1127 * LOCKING:
1128 * caller.
1129 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
1131{
1132}
1133
Edward Falk0baab862005-06-02 18:17:13 -04001134
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135/**
1136 * ata_std_dev_select - Select device 0/1 on ATA bus
1137 * @ap: ATA channel to manipulate
1138 * @device: ATA device (numbered from zero) to select
1139 *
1140 * Use the method defined in the ATA specification to
1141 * make either device 0, or device 1, active on the
Edward Falk0baab862005-06-02 18:17:13 -04001142 * ATA channel. Works with both PIO and MMIO.
1143 *
1144 * May be used as the dev_select() entry in ata_port_operations.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 *
1146 * LOCKING:
1147 * caller.
1148 */
1149
1150void ata_std_dev_select (struct ata_port *ap, unsigned int device)
1151{
1152 u8 tmp;
1153
1154 if (device == 0)
1155 tmp = ATA_DEVICE_OBS;
1156 else
1157 tmp = ATA_DEVICE_OBS | ATA_DEV1;
1158
Tejun Heo0d5ff562007-02-01 15:06:36 +09001159 iowrite8(tmp, ap->ioaddr.device_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 ata_pause(ap); /* needed; also flushes, for mmio */
1161}
1162
1163/**
1164 * ata_dev_select - Select device 0/1 on ATA bus
1165 * @ap: ATA channel to manipulate
1166 * @device: ATA device (numbered from zero) to select
1167 * @wait: non-zero to wait for Status register BSY bit to clear
1168 * @can_sleep: non-zero if context allows sleeping
1169 *
1170 * Use the method defined in the ATA specification to
1171 * make either device 0, or device 1, active on the
1172 * ATA channel.
1173 *
1174 * This is a high-level version of ata_std_dev_select(),
1175 * which additionally provides the services of inserting
1176 * the proper pauses and status polling, where needed.
1177 *
1178 * LOCKING:
1179 * caller.
1180 */
1181
1182void ata_dev_select(struct ata_port *ap, unsigned int device,
1183 unsigned int wait, unsigned int can_sleep)
1184{
Tejun Heo88574552006-06-25 20:00:35 +09001185 if (ata_msg_probe(ap))
Tejun Heo44877b42007-02-21 01:06:51 +09001186 ata_port_printk(ap, KERN_INFO, "ata_dev_select: ENTER, "
1187 "device %u, wait %u\n", device, wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
1189 if (wait)
1190 ata_wait_idle(ap);
1191
1192 ap->ops->dev_select(ap, device);
1193
1194 if (wait) {
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001195 if (can_sleep && ap->link.device[device].class == ATA_DEV_ATAPI)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 msleep(150);
1197 ata_wait_idle(ap);
1198 }
1199}
1200
1201/**
1202 * ata_dump_id - IDENTIFY DEVICE info debugging output
Tejun Heo0bd33002006-02-12 22:47:05 +09001203 * @id: IDENTIFY DEVICE page to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 *
Tejun Heo0bd33002006-02-12 22:47:05 +09001205 * Dump selected 16-bit words from the given IDENTIFY DEVICE
1206 * page.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 *
1208 * LOCKING:
1209 * caller.
1210 */
1211
Tejun Heo0bd33002006-02-12 22:47:05 +09001212static inline void ata_dump_id(const u16 *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213{
1214 DPRINTK("49==0x%04x "
1215 "53==0x%04x "
1216 "63==0x%04x "
1217 "64==0x%04x "
1218 "75==0x%04x \n",
Tejun Heo0bd33002006-02-12 22:47:05 +09001219 id[49],
1220 id[53],
1221 id[63],
1222 id[64],
1223 id[75]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 DPRINTK("80==0x%04x "
1225 "81==0x%04x "
1226 "82==0x%04x "
1227 "83==0x%04x "
1228 "84==0x%04x \n",
Tejun Heo0bd33002006-02-12 22:47:05 +09001229 id[80],
1230 id[81],
1231 id[82],
1232 id[83],
1233 id[84]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 DPRINTK("88==0x%04x "
1235 "93==0x%04x\n",
Tejun Heo0bd33002006-02-12 22:47:05 +09001236 id[88],
1237 id[93]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238}
1239
Tejun Heocb95d562006-03-06 04:31:56 +09001240/**
1241 * ata_id_xfermask - Compute xfermask from the given IDENTIFY data
1242 * @id: IDENTIFY data to compute xfer mask from
1243 *
1244 * Compute the xfermask for this device. This is not as trivial
1245 * as it seems if we must consider early devices correctly.
1246 *
1247 * FIXME: pre IDE drive timing (do we care ?).
1248 *
1249 * LOCKING:
1250 * None.
1251 *
1252 * RETURNS:
1253 * Computed xfermask
1254 */
1255static unsigned int ata_id_xfermask(const u16 *id)
1256{
1257 unsigned int pio_mask, mwdma_mask, udma_mask;
1258
1259 /* Usual case. Word 53 indicates word 64 is valid */
1260 if (id[ATA_ID_FIELD_VALID] & (1 << 1)) {
1261 pio_mask = id[ATA_ID_PIO_MODES] & 0x03;
1262 pio_mask <<= 3;
1263 pio_mask |= 0x7;
1264 } else {
1265 /* If word 64 isn't valid then Word 51 high byte holds
1266 * the PIO timing number for the maximum. Turn it into
1267 * a mask.
1268 */
Lennert Buytenhek7a0f1c82007-01-29 13:28:47 +01001269 u8 mode = (id[ATA_ID_OLD_PIO_MODES] >> 8) & 0xFF;
Alan Cox46767aeb2006-09-29 18:26:47 +01001270 if (mode < 5) /* Valid PIO range */
1271 pio_mask = (2 << mode) - 1;
1272 else
1273 pio_mask = 1;
Tejun Heocb95d562006-03-06 04:31:56 +09001274
1275 /* But wait.. there's more. Design your standards by
1276 * committee and you too can get a free iordy field to
1277 * process. However its the speeds not the modes that
1278 * are supported... Note drivers using the timing API
1279 * will get this right anyway
1280 */
1281 }
1282
1283 mwdma_mask = id[ATA_ID_MWDMA_MODES] & 0x07;
Tejun Heofb21f0d2006-03-12 12:34:35 +09001284
Alan Coxb352e572006-08-10 18:52:12 +01001285 if (ata_id_is_cfa(id)) {
1286 /*
1287 * Process compact flash extended modes
1288 */
1289 int pio = id[163] & 0x7;
1290 int dma = (id[163] >> 3) & 7;
1291
1292 if (pio)
1293 pio_mask |= (1 << 5);
1294 if (pio > 1)
1295 pio_mask |= (1 << 6);
1296 if (dma)
1297 mwdma_mask |= (1 << 3);
1298 if (dma > 1)
1299 mwdma_mask |= (1 << 4);
1300 }
1301
Tejun Heofb21f0d2006-03-12 12:34:35 +09001302 udma_mask = 0;
1303 if (id[ATA_ID_FIELD_VALID] & (1 << 2))
1304 udma_mask = id[ATA_ID_UDMA_MODES] & 0xff;
Tejun Heocb95d562006-03-06 04:31:56 +09001305
1306 return ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
1307}
1308
Tejun Heo86e45b62006-03-05 15:29:09 +09001309/**
1310 * ata_port_queue_task - Queue port_task
1311 * @ap: The ata_port to queue port_task for
Randy Dunlape2a7f772006-05-18 10:50:18 -07001312 * @fn: workqueue function to be scheduled
David Howells65f27f32006-11-22 14:55:48 +00001313 * @data: data for @fn to use
Randy Dunlape2a7f772006-05-18 10:50:18 -07001314 * @delay: delay time for workqueue function
Tejun Heo86e45b62006-03-05 15:29:09 +09001315 *
1316 * Schedule @fn(@data) for execution after @delay jiffies using
1317 * port_task. There is one port_task per port and it's the
1318 * user(low level driver)'s responsibility to make sure that only
1319 * one task is active at any given time.
1320 *
1321 * libata core layer takes care of synchronization between
1322 * port_task and EH. ata_port_queue_task() may be ignored for EH
1323 * synchronization.
1324 *
1325 * LOCKING:
1326 * Inherited from caller.
1327 */
David Howells65f27f32006-11-22 14:55:48 +00001328void ata_port_queue_task(struct ata_port *ap, work_func_t fn, void *data,
Tejun Heo86e45b62006-03-05 15:29:09 +09001329 unsigned long delay)
1330{
David Howells65f27f32006-11-22 14:55:48 +00001331 PREPARE_DELAYED_WORK(&ap->port_task, fn);
1332 ap->port_task_data = data;
Tejun Heo86e45b62006-03-05 15:29:09 +09001333
Oleg Nesterov45a66c12007-07-09 11:46:13 -07001334 /* may fail if ata_port_flush_task() in progress */
1335 queue_delayed_work(ata_wq, &ap->port_task, delay);
Tejun Heo86e45b62006-03-05 15:29:09 +09001336}
1337
1338/**
1339 * ata_port_flush_task - Flush port_task
1340 * @ap: The ata_port to flush port_task for
1341 *
1342 * After this function completes, port_task is guranteed not to
1343 * be running or scheduled.
1344 *
1345 * LOCKING:
1346 * Kernel thread context (may sleep)
1347 */
1348void ata_port_flush_task(struct ata_port *ap)
1349{
Tejun Heo86e45b62006-03-05 15:29:09 +09001350 DPRINTK("ENTER\n");
1351
Oleg Nesterov45a66c12007-07-09 11:46:13 -07001352 cancel_rearming_delayed_work(&ap->port_task);
Tejun Heo86e45b62006-03-05 15:29:09 +09001353
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001354 if (ata_msg_ctl(ap))
1355 ata_port_printk(ap, KERN_DEBUG, "%s: EXIT\n", __FUNCTION__);
Tejun Heo86e45b62006-03-05 15:29:09 +09001356}
1357
Adrian Bunk7102d232007-01-04 00:09:36 +01001358static void ata_qc_complete_internal(struct ata_queued_cmd *qc)
Tejun Heoa2a7a662005-12-13 14:48:31 +09001359{
Tejun Heo77853bf2006-01-23 13:09:36 +09001360 struct completion *waiting = qc->private_data;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001361
Tejun Heoa2a7a662005-12-13 14:48:31 +09001362 complete(waiting);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001363}
1364
1365/**
Tejun Heo24326972006-11-14 22:47:09 +09001366 * ata_exec_internal_sg - execute libata internal command
Tejun Heoa2a7a662005-12-13 14:48:31 +09001367 * @dev: Device to which the command is sent
1368 * @tf: Taskfile registers for the command and the result
Tejun Heod69cf372006-04-02 18:51:53 +09001369 * @cdb: CDB for packet command
Tejun Heoa2a7a662005-12-13 14:48:31 +09001370 * @dma_dir: Data tranfer direction of the command
Tejun Heo24326972006-11-14 22:47:09 +09001371 * @sg: sg list for the data buffer of the command
1372 * @n_elem: Number of sg entries
Tejun Heoa2a7a662005-12-13 14:48:31 +09001373 *
1374 * Executes libata internal command with timeout. @tf contains
1375 * command on entry and result on return. Timeout and error
1376 * conditions are reported via return value. No recovery action
1377 * is taken after a command times out. It's caller's duty to
1378 * clean up after timeout.
1379 *
1380 * LOCKING:
1381 * None. Should be called with kernel context, might sleep.
Tejun Heo551e8882006-06-12 14:09:49 +09001382 *
1383 * RETURNS:
1384 * Zero on success, AC_ERR_* mask on failure
Tejun Heoa2a7a662005-12-13 14:48:31 +09001385 */
Tejun Heo24326972006-11-14 22:47:09 +09001386unsigned ata_exec_internal_sg(struct ata_device *dev,
1387 struct ata_taskfile *tf, const u8 *cdb,
1388 int dma_dir, struct scatterlist *sg,
1389 unsigned int n_elem)
Tejun Heoa2a7a662005-12-13 14:48:31 +09001390{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001391 struct ata_link *link = dev->link;
1392 struct ata_port *ap = link->ap;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001393 u8 command = tf->command;
1394 struct ata_queued_cmd *qc;
Tejun Heo2ab7db12006-05-15 20:58:02 +09001395 unsigned int tag, preempted_tag;
Tejun Heodedaf2b2006-05-15 21:03:43 +09001396 u32 preempted_sactive, preempted_qc_active;
Tejun Heoda917d62007-09-23 13:14:12 +09001397 int preempted_nr_active_links;
Ingo Molnar60be6b92006-07-03 00:25:26 -07001398 DECLARE_COMPLETION_ONSTACK(wait);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001399 unsigned long flags;
Tejun Heo77853bf2006-01-23 13:09:36 +09001400 unsigned int err_mask;
Tejun Heod95a7172006-05-15 20:58:14 +09001401 int rc;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001402
Jeff Garzikba6a1302006-06-22 23:46:10 -04001403 spin_lock_irqsave(ap->lock, flags);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001404
Tejun Heoe3180492006-05-15 20:58:09 +09001405 /* no internal command while frozen */
Tejun Heob51e9e52006-06-29 01:29:30 +09001406 if (ap->pflags & ATA_PFLAG_FROZEN) {
Jeff Garzikba6a1302006-06-22 23:46:10 -04001407 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +09001408 return AC_ERR_SYSTEM;
1409 }
1410
Tejun Heo2ab7db12006-05-15 20:58:02 +09001411 /* initialize internal qc */
Tejun Heoa2a7a662005-12-13 14:48:31 +09001412
Tejun Heo2ab7db12006-05-15 20:58:02 +09001413 /* XXX: Tag 0 is used for drivers with legacy EH as some
1414 * drivers choke if any other tag is given. This breaks
1415 * ata_tag_internal() test for those drivers. Don't use new
1416 * EH stuff without converting to it.
1417 */
1418 if (ap->ops->error_handler)
1419 tag = ATA_TAG_INTERNAL;
1420 else
1421 tag = 0;
1422
Tejun Heo6cec4a32006-05-15 21:03:41 +09001423 if (test_and_set_bit(tag, &ap->qc_allocated))
Tejun Heo2ab7db12006-05-15 20:58:02 +09001424 BUG();
Tejun Heof69499f2006-05-15 20:58:03 +09001425 qc = __ata_qc_from_tag(ap, tag);
Tejun Heo2ab7db12006-05-15 20:58:02 +09001426
1427 qc->tag = tag;
1428 qc->scsicmd = NULL;
1429 qc->ap = ap;
1430 qc->dev = dev;
1431 ata_qc_reinit(qc);
1432
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001433 preempted_tag = link->active_tag;
1434 preempted_sactive = link->sactive;
Tejun Heodedaf2b2006-05-15 21:03:43 +09001435 preempted_qc_active = ap->qc_active;
Tejun Heoda917d62007-09-23 13:14:12 +09001436 preempted_nr_active_links = ap->nr_active_links;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001437 link->active_tag = ATA_TAG_POISON;
1438 link->sactive = 0;
Tejun Heodedaf2b2006-05-15 21:03:43 +09001439 ap->qc_active = 0;
Tejun Heoda917d62007-09-23 13:14:12 +09001440 ap->nr_active_links = 0;
Tejun Heo2ab7db12006-05-15 20:58:02 +09001441
1442 /* prepare & issue qc */
Tejun Heoa2a7a662005-12-13 14:48:31 +09001443 qc->tf = *tf;
Tejun Heod69cf372006-04-02 18:51:53 +09001444 if (cdb)
1445 memcpy(qc->cdb, cdb, ATAPI_CDB_LEN);
Tejun Heoe61e0672006-05-15 20:57:40 +09001446 qc->flags |= ATA_QCFLAG_RESULT_TF;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001447 qc->dma_dir = dma_dir;
1448 if (dma_dir != DMA_NONE) {
Tejun Heo24326972006-11-14 22:47:09 +09001449 unsigned int i, buflen = 0;
1450
1451 for (i = 0; i < n_elem; i++)
1452 buflen += sg[i].length;
1453
1454 ata_sg_init(qc, sg, n_elem);
Brian King49c80422007-01-30 11:32:26 -06001455 qc->nbytes = buflen;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001456 }
1457
Tejun Heo77853bf2006-01-23 13:09:36 +09001458 qc->private_data = &wait;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001459 qc->complete_fn = ata_qc_complete_internal;
1460
Tejun Heo8e0e6942006-03-31 20:41:11 +09001461 ata_qc_issue(qc);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001462
Jeff Garzikba6a1302006-06-22 23:46:10 -04001463 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001464
Andrew Mortona8601e52006-06-25 01:36:52 -07001465 rc = wait_for_completion_timeout(&wait, ata_probe_timeout);
Albert Lee41ade502006-03-14 11:19:04 +08001466
Tejun Heod95a7172006-05-15 20:58:14 +09001467 ata_port_flush_task(ap);
1468
1469 if (!rc) {
Jeff Garzikba6a1302006-06-22 23:46:10 -04001470 spin_lock_irqsave(ap->lock, flags);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001471
1472 /* We're racing with irq here. If we lose, the
1473 * following test prevents us from completing the qc
Tejun Heod95a7172006-05-15 20:58:14 +09001474 * twice. If we win, the port is frozen and will be
1475 * cleaned up by ->post_internal_cmd().
Tejun Heoa2a7a662005-12-13 14:48:31 +09001476 */
Tejun Heo77853bf2006-01-23 13:09:36 +09001477 if (qc->flags & ATA_QCFLAG_ACTIVE) {
Tejun Heod95a7172006-05-15 20:58:14 +09001478 qc->err_mask |= AC_ERR_TIMEOUT;
1479
1480 if (ap->ops->error_handler)
1481 ata_port_freeze(ap);
1482 else
1483 ata_qc_complete(qc);
Tejun Heof15a1da2006-05-15 20:57:56 +09001484
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001485 if (ata_msg_warn(ap))
1486 ata_dev_printk(dev, KERN_WARNING,
Tejun Heo88574552006-06-25 20:00:35 +09001487 "qc timeout (cmd 0x%x)\n", command);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001488 }
1489
Jeff Garzikba6a1302006-06-22 23:46:10 -04001490 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001491 }
1492
Tejun Heod95a7172006-05-15 20:58:14 +09001493 /* do post_internal_cmd */
1494 if (ap->ops->post_internal_cmd)
1495 ap->ops->post_internal_cmd(qc);
1496
Tejun Heoa51d6442007-03-20 15:24:11 +09001497 /* perform minimal error analysis */
1498 if (qc->flags & ATA_QCFLAG_FAILED) {
1499 if (qc->result_tf.command & (ATA_ERR | ATA_DF))
1500 qc->err_mask |= AC_ERR_DEV;
1501
1502 if (!qc->err_mask)
1503 qc->err_mask |= AC_ERR_OTHER;
1504
1505 if (qc->err_mask & ~AC_ERR_OTHER)
1506 qc->err_mask &= ~AC_ERR_OTHER;
Tejun Heod95a7172006-05-15 20:58:14 +09001507 }
1508
Tejun Heo15869302006-05-15 20:57:33 +09001509 /* finish up */
Jeff Garzikba6a1302006-06-22 23:46:10 -04001510 spin_lock_irqsave(ap->lock, flags);
Tejun Heo15869302006-05-15 20:57:33 +09001511
Tejun Heoe61e0672006-05-15 20:57:40 +09001512 *tf = qc->result_tf;
Tejun Heo77853bf2006-01-23 13:09:36 +09001513 err_mask = qc->err_mask;
1514
1515 ata_qc_free(qc);
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001516 link->active_tag = preempted_tag;
1517 link->sactive = preempted_sactive;
Tejun Heodedaf2b2006-05-15 21:03:43 +09001518 ap->qc_active = preempted_qc_active;
Tejun Heoda917d62007-09-23 13:14:12 +09001519 ap->nr_active_links = preempted_nr_active_links;
Tejun Heo77853bf2006-01-23 13:09:36 +09001520
Tejun Heo1f7dd3e92006-03-24 15:25:30 +09001521 /* XXX - Some LLDDs (sata_mv) disable port on command failure.
1522 * Until those drivers are fixed, we detect the condition
1523 * here, fail the command with AC_ERR_SYSTEM and reenable the
1524 * port.
1525 *
1526 * Note that this doesn't change any behavior as internal
1527 * command failure results in disabling the device in the
1528 * higher layer for LLDDs without new reset/EH callbacks.
1529 *
1530 * Kill the following code as soon as those drivers are fixed.
1531 */
Tejun Heo198e0fe2006-04-02 18:51:52 +09001532 if (ap->flags & ATA_FLAG_DISABLED) {
Tejun Heo1f7dd3e92006-03-24 15:25:30 +09001533 err_mask |= AC_ERR_SYSTEM;
1534 ata_port_probe(ap);
1535 }
1536
Jeff Garzikba6a1302006-06-22 23:46:10 -04001537 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo15869302006-05-15 20:57:33 +09001538
Tejun Heo77853bf2006-01-23 13:09:36 +09001539 return err_mask;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001540}
1541
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542/**
Tejun Heo33480a02006-12-12 02:15:31 +09001543 * ata_exec_internal - execute libata internal command
Tejun Heo24326972006-11-14 22:47:09 +09001544 * @dev: Device to which the command is sent
1545 * @tf: Taskfile registers for the command and the result
1546 * @cdb: CDB for packet command
1547 * @dma_dir: Data tranfer direction of the command
1548 * @buf: Data buffer of the command
1549 * @buflen: Length of data buffer
1550 *
1551 * Wrapper around ata_exec_internal_sg() which takes simple
1552 * buffer instead of sg list.
1553 *
1554 * LOCKING:
1555 * None. Should be called with kernel context, might sleep.
1556 *
1557 * RETURNS:
1558 * Zero on success, AC_ERR_* mask on failure
1559 */
1560unsigned ata_exec_internal(struct ata_device *dev,
1561 struct ata_taskfile *tf, const u8 *cdb,
1562 int dma_dir, void *buf, unsigned int buflen)
1563{
Tejun Heo33480a02006-12-12 02:15:31 +09001564 struct scatterlist *psg = NULL, sg;
1565 unsigned int n_elem = 0;
Tejun Heo24326972006-11-14 22:47:09 +09001566
Tejun Heo33480a02006-12-12 02:15:31 +09001567 if (dma_dir != DMA_NONE) {
1568 WARN_ON(!buf);
1569 sg_init_one(&sg, buf, buflen);
1570 psg = &sg;
1571 n_elem++;
1572 }
Tejun Heo24326972006-11-14 22:47:09 +09001573
Tejun Heo33480a02006-12-12 02:15:31 +09001574 return ata_exec_internal_sg(dev, tf, cdb, dma_dir, psg, n_elem);
Tejun Heo24326972006-11-14 22:47:09 +09001575}
1576
1577/**
Tejun Heo977e6b92006-06-24 20:30:19 +09001578 * ata_do_simple_cmd - execute simple internal command
1579 * @dev: Device to which the command is sent
1580 * @cmd: Opcode to execute
1581 *
1582 * Execute a 'simple' command, that only consists of the opcode
1583 * 'cmd' itself, without filling any other registers
1584 *
1585 * LOCKING:
1586 * Kernel thread context (may sleep).
1587 *
1588 * RETURNS:
1589 * Zero on success, AC_ERR_* mask on failure
Tejun Heoe58eb582006-06-24 20:30:19 +09001590 */
Tejun Heo77b08fb2006-06-24 20:30:19 +09001591unsigned int ata_do_simple_cmd(struct ata_device *dev, u8 cmd)
Tejun Heoe58eb582006-06-24 20:30:19 +09001592{
1593 struct ata_taskfile tf;
Tejun Heoe58eb582006-06-24 20:30:19 +09001594
1595 ata_tf_init(dev, &tf);
1596
1597 tf.command = cmd;
1598 tf.flags |= ATA_TFLAG_DEVICE;
1599 tf.protocol = ATA_PROT_NODATA;
1600
Tejun Heo977e6b92006-06-24 20:30:19 +09001601 return ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
Tejun Heoe58eb582006-06-24 20:30:19 +09001602}
1603
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604/**
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001605 * ata_pio_need_iordy - check if iordy needed
1606 * @adev: ATA device
1607 *
1608 * Check if the current speed of the device requires IORDY. Used
1609 * by various controllers for chip configuration.
1610 */
Jeff Garzika617c092007-05-21 20:14:23 -04001611
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001612unsigned int ata_pio_need_iordy(const struct ata_device *adev)
1613{
Alan Cox432729f2007-03-08 23:22:59 +00001614 /* Controller doesn't support IORDY. Probably a pointless check
1615 as the caller should know this */
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001616 if (adev->link->ap->flags & ATA_FLAG_NO_IORDY)
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001617 return 0;
Alan Cox432729f2007-03-08 23:22:59 +00001618 /* PIO3 and higher it is mandatory */
1619 if (adev->pio_mode > XFER_PIO_2)
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001620 return 1;
Alan Cox432729f2007-03-08 23:22:59 +00001621 /* We turn it on when possible */
1622 if (ata_id_has_iordy(adev->id))
1623 return 1;
1624 return 0;
1625}
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001626
Alan Cox432729f2007-03-08 23:22:59 +00001627/**
1628 * ata_pio_mask_no_iordy - Return the non IORDY mask
1629 * @adev: ATA device
1630 *
1631 * Compute the highest mode possible if we are not using iordy. Return
1632 * -1 if no iordy mode is available.
1633 */
Jeff Garzika617c092007-05-21 20:14:23 -04001634
Alan Cox432729f2007-03-08 23:22:59 +00001635static u32 ata_pio_mask_no_iordy(const struct ata_device *adev)
1636{
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001637 /* If we have no drive specific rule, then PIO 2 is non IORDY */
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001638 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
Alan Cox432729f2007-03-08 23:22:59 +00001639 u16 pio = adev->id[ATA_ID_EIDE_PIO];
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001640 /* Is the speed faster than the drive allows non IORDY ? */
1641 if (pio) {
1642 /* This is cycle times not frequency - watch the logic! */
1643 if (pio > 240) /* PIO2 is 240nS per cycle */
Alan Cox432729f2007-03-08 23:22:59 +00001644 return 3 << ATA_SHIFT_PIO;
1645 return 7 << ATA_SHIFT_PIO;
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001646 }
1647 }
Alan Cox432729f2007-03-08 23:22:59 +00001648 return 3 << ATA_SHIFT_PIO;
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001649}
1650
1651/**
Tejun Heo49016ac2006-02-21 02:12:11 +09001652 * ata_dev_read_id - Read ID data from the specified device
Tejun Heo49016ac2006-02-21 02:12:11 +09001653 * @dev: target device
1654 * @p_class: pointer to class of the target device (may be changed)
Tejun Heobff04642006-11-10 18:08:10 +09001655 * @flags: ATA_READID_* flags
Tejun Heofe635c72006-05-15 20:57:35 +09001656 * @id: buffer to read IDENTIFY data into
Tejun Heo49016ac2006-02-21 02:12:11 +09001657 *
1658 * Read ID data from the specified device. ATA_CMD_ID_ATA is
1659 * performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
Tejun Heoaec5c3c2006-03-25 01:33:34 +09001660 * devices. This function also issues ATA_CMD_INIT_DEV_PARAMS
1661 * for pre-ATA4 drives.
Tejun Heo49016ac2006-02-21 02:12:11 +09001662 *
Alan Cox50a99012007-08-08 14:27:00 +01001663 * FIXME: ATA_CMD_ID_ATA is optional for early drives and right
1664 * now we abort if we hit that case.
1665 *
Tejun Heo49016ac2006-02-21 02:12:11 +09001666 * LOCKING:
1667 * Kernel thread context (may sleep)
1668 *
1669 * RETURNS:
1670 * 0 on success, -errno otherwise.
1671 */
Tejun Heoa9beec92006-05-31 18:27:44 +09001672int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
Tejun Heobff04642006-11-10 18:08:10 +09001673 unsigned int flags, u16 *id)
Tejun Heo49016ac2006-02-21 02:12:11 +09001674{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001675 struct ata_port *ap = dev->link->ap;
Tejun Heo49016ac2006-02-21 02:12:11 +09001676 unsigned int class = *p_class;
Tejun Heo49016ac2006-02-21 02:12:11 +09001677 struct ata_taskfile tf;
1678 unsigned int err_mask = 0;
1679 const char *reason;
Tejun Heo54936f82007-05-11 14:35:29 +02001680 int may_fallback = 1, tried_spinup = 0;
Tejun Heo49016ac2006-02-21 02:12:11 +09001681 int rc;
1682
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001683 if (ata_msg_ctl(ap))
Tejun Heo44877b42007-02-21 01:06:51 +09001684 ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER\n", __FUNCTION__);
Tejun Heo49016ac2006-02-21 02:12:11 +09001685
Tejun Heo49016ac2006-02-21 02:12:11 +09001686 ata_dev_select(ap, dev->devno, 1, 1); /* select device 0/1 */
Tejun Heo49016ac2006-02-21 02:12:11 +09001687 retry:
Tejun Heo3373efd2006-05-15 20:57:53 +09001688 ata_tf_init(dev, &tf);
Tejun Heo49016ac2006-02-21 02:12:11 +09001689
1690 switch (class) {
1691 case ATA_DEV_ATA:
1692 tf.command = ATA_CMD_ID_ATA;
1693 break;
1694 case ATA_DEV_ATAPI:
1695 tf.command = ATA_CMD_ID_ATAPI;
1696 break;
1697 default:
1698 rc = -ENODEV;
1699 reason = "unsupported class";
1700 goto err_out;
1701 }
1702
1703 tf.protocol = ATA_PROT_PIO;
Tejun Heo81afe892007-02-07 12:37:41 -08001704
1705 /* Some devices choke if TF registers contain garbage. Make
1706 * sure those are properly initialized.
1707 */
1708 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1709
1710 /* Device presence detection is unreliable on some
1711 * controllers. Always poll IDENTIFY if available.
1712 */
1713 tf.flags |= ATA_TFLAG_POLLING;
Tejun Heo49016ac2006-02-21 02:12:11 +09001714
Tejun Heo3373efd2006-05-15 20:57:53 +09001715 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
Tejun Heo49016ac2006-02-21 02:12:11 +09001716 id, sizeof(id[0]) * ATA_ID_WORDS);
Tejun Heo49016ac2006-02-21 02:12:11 +09001717 if (err_mask) {
Tejun Heo800b3992006-12-03 21:34:13 +09001718 if (err_mask & AC_ERR_NODEV_HINT) {
Tejun Heo55a8e2c2006-11-10 18:08:10 +09001719 DPRINTK("ata%u.%d: NODEV after polling detection\n",
Tejun Heo44877b42007-02-21 01:06:51 +09001720 ap->print_id, dev->devno);
Tejun Heo55a8e2c2006-11-10 18:08:10 +09001721 return -ENOENT;
1722 }
1723
Tejun Heo54936f82007-05-11 14:35:29 +02001724 /* Device or controller might have reported the wrong
1725 * device class. Give a shot at the other IDENTIFY if
1726 * the current one is aborted by the device.
1727 */
1728 if (may_fallback &&
1729 (err_mask == AC_ERR_DEV) && (tf.feature & ATA_ABORTED)) {
1730 may_fallback = 0;
1731
1732 if (class == ATA_DEV_ATA)
1733 class = ATA_DEV_ATAPI;
1734 else
1735 class = ATA_DEV_ATA;
1736 goto retry;
1737 }
1738
Tejun Heo49016ac2006-02-21 02:12:11 +09001739 rc = -EIO;
1740 reason = "I/O error";
Tejun Heo49016ac2006-02-21 02:12:11 +09001741 goto err_out;
1742 }
1743
Tejun Heo54936f82007-05-11 14:35:29 +02001744 /* Falling back doesn't make sense if ID data was read
1745 * successfully at least once.
1746 */
1747 may_fallback = 0;
1748
Tejun Heo49016ac2006-02-21 02:12:11 +09001749 swap_buf_le16(id, ATA_ID_WORDS);
1750
Tejun Heo49016ac2006-02-21 02:12:11 +09001751 /* sanity check */
Tejun Heoa4f57492006-09-12 20:35:49 -07001752 rc = -EINVAL;
Alan Cox60700682007-06-07 16:13:55 +01001753 reason = "device reports invalid type";
Tejun Heoa4f57492006-09-12 20:35:49 -07001754
1755 if (class == ATA_DEV_ATA) {
1756 if (!ata_id_is_ata(id) && !ata_id_is_cfa(id))
1757 goto err_out;
1758 } else {
1759 if (ata_id_is_ata(id))
1760 goto err_out;
Tejun Heo49016ac2006-02-21 02:12:11 +09001761 }
1762
Mark Lord169439c2007-04-17 18:26:07 -04001763 if (!tried_spinup && (id[2] == 0x37c8 || id[2] == 0x738c)) {
1764 tried_spinup = 1;
1765 /*
1766 * Drive powered-up in standby mode, and requires a specific
1767 * SET_FEATURES spin-up subcommand before it will accept
1768 * anything other than the original IDENTIFY command.
1769 */
1770 ata_tf_init(dev, &tf);
1771 tf.command = ATA_CMD_SET_FEATURES;
1772 tf.feature = SETFEATURES_SPINUP;
1773 tf.protocol = ATA_PROT_NODATA;
1774 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1775 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
Ryan Powerfb0582f92007-08-10 13:59:35 -07001776 if (err_mask && id[2] != 0x738c) {
Mark Lord169439c2007-04-17 18:26:07 -04001777 rc = -EIO;
1778 reason = "SPINUP failed";
1779 goto err_out;
1780 }
1781 /*
1782 * If the drive initially returned incomplete IDENTIFY info,
1783 * we now must reissue the IDENTIFY command.
1784 */
1785 if (id[2] == 0x37c8)
1786 goto retry;
1787 }
1788
Tejun Heobff04642006-11-10 18:08:10 +09001789 if ((flags & ATA_READID_POSTRESET) && class == ATA_DEV_ATA) {
Tejun Heo49016ac2006-02-21 02:12:11 +09001790 /*
1791 * The exact sequence expected by certain pre-ATA4 drives is:
1792 * SRST RESET
Alan Cox50a99012007-08-08 14:27:00 +01001793 * IDENTIFY (optional in early ATA)
1794 * INITIALIZE DEVICE PARAMETERS (later IDE and ATA)
Tejun Heo49016ac2006-02-21 02:12:11 +09001795 * anything else..
1796 * Some drives were very specific about that exact sequence.
Alan Cox50a99012007-08-08 14:27:00 +01001797 *
1798 * Note that ATA4 says lba is mandatory so the second check
1799 * shoud never trigger.
Tejun Heo49016ac2006-02-21 02:12:11 +09001800 */
1801 if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
Tejun Heo3373efd2006-05-15 20:57:53 +09001802 err_mask = ata_dev_init_params(dev, id[3], id[6]);
Tejun Heo49016ac2006-02-21 02:12:11 +09001803 if (err_mask) {
1804 rc = -EIO;
1805 reason = "INIT_DEV_PARAMS failed";
1806 goto err_out;
1807 }
1808
1809 /* current CHS translation info (id[53-58]) might be
1810 * changed. reread the identify device info.
1811 */
Tejun Heobff04642006-11-10 18:08:10 +09001812 flags &= ~ATA_READID_POSTRESET;
Tejun Heo49016ac2006-02-21 02:12:11 +09001813 goto retry;
1814 }
1815 }
1816
1817 *p_class = class;
Tejun Heofe635c72006-05-15 20:57:35 +09001818
Tejun Heo49016ac2006-02-21 02:12:11 +09001819 return 0;
1820
1821 err_out:
Tejun Heo88574552006-06-25 20:00:35 +09001822 if (ata_msg_warn(ap))
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001823 ata_dev_printk(dev, KERN_WARNING, "failed to IDENTIFY "
Tejun Heo88574552006-06-25 20:00:35 +09001824 "(%s, err_mask=0x%x)\n", reason, err_mask);
Tejun Heo49016ac2006-02-21 02:12:11 +09001825 return rc;
1826}
1827
Tejun Heo3373efd2006-05-15 20:57:53 +09001828static inline u8 ata_dev_knobble(struct ata_device *dev)
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09001829{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001830 struct ata_port *ap = dev->link->ap;
1831 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09001832}
1833
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001834static void ata_dev_config_ncq(struct ata_device *dev,
1835 char *desc, size_t desc_sz)
1836{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001837 struct ata_port *ap = dev->link->ap;
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001838 int hdepth = 0, ddepth = ata_id_queue_depth(dev->id);
1839
1840 if (!ata_id_has_ncq(dev->id)) {
1841 desc[0] = '\0';
1842 return;
1843 }
Tejun Heo75683fe2007-07-05 13:31:27 +09001844 if (dev->horkage & ATA_HORKAGE_NONCQ) {
Alan Cox6919a0a2006-10-27 19:08:46 -07001845 snprintf(desc, desc_sz, "NCQ (not used)");
1846 return;
1847 }
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001848 if (ap->flags & ATA_FLAG_NCQ) {
Jeff Garzikcca39742006-08-24 03:19:22 -04001849 hdepth = min(ap->scsi_host->can_queue, ATA_MAX_QUEUE - 1);
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001850 dev->flags |= ATA_DFLAG_NCQ;
1851 }
1852
1853 if (hdepth >= ddepth)
1854 snprintf(desc, desc_sz, "NCQ (depth %d)", ddepth);
1855 else
1856 snprintf(desc, desc_sz, "NCQ (depth %d/%d)", hdepth, ddepth);
1857}
1858
Tejun Heo49016ac2006-02-21 02:12:11 +09001859/**
Tejun Heoffeae412006-03-01 16:09:35 +09001860 * ata_dev_configure - Configure the specified ATA/ATAPI device
Tejun Heoffeae412006-03-01 16:09:35 +09001861 * @dev: Target device to configure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 *
Tejun Heoffeae412006-03-01 16:09:35 +09001863 * Configure @dev according to @dev->id. Generic and low-level
1864 * driver specific fixups are also applied.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 *
1866 * LOCKING:
Tejun Heoffeae412006-03-01 16:09:35 +09001867 * Kernel thread context (may sleep)
1868 *
1869 * RETURNS:
1870 * 0 on success, -errno otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 */
Tejun Heoefdaedc2006-11-01 18:38:52 +09001872int ata_dev_configure(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001874 struct ata_port *ap = dev->link->ap;
1875 struct ata_eh_context *ehc = &dev->link->eh_context;
Tejun Heo67465442007-05-15 03:28:16 +09001876 int print_info = ehc->i.flags & ATA_EHI_PRINTINFO;
Tejun Heo1148c3a2006-03-13 19:48:04 +09001877 const u16 *id = dev->id;
Tejun Heoff8854b2006-03-06 04:31:56 +09001878 unsigned int xfer_mask;
Alan Coxb352e572006-08-10 18:52:12 +01001879 char revbuf[7]; /* XYZ-99\0 */
Eric D. Mudama3f64f562007-01-30 23:00:40 -07001880 char fwrevbuf[ATA_ID_FW_REV_LEN+1];
1881 char modelbuf[ATA_ID_PROD_LEN+1];
Brian Kinge6d902a2006-06-28 08:30:31 -05001882 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001884 if (!ata_dev_enabled(dev) && ata_msg_info(ap)) {
Tejun Heo44877b42007-02-21 01:06:51 +09001885 ata_dev_printk(dev, KERN_INFO, "%s: ENTER/EXIT -- nodev\n",
1886 __FUNCTION__);
Tejun Heoffeae412006-03-01 16:09:35 +09001887 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 }
1889
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001890 if (ata_msg_probe(ap))
Tejun Heo44877b42007-02-21 01:06:51 +09001891 ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER\n", __FUNCTION__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892
Tejun Heo75683fe2007-07-05 13:31:27 +09001893 /* set horkage */
1894 dev->horkage |= ata_dev_blacklisted(dev);
1895
Tejun Heo67465442007-05-15 03:28:16 +09001896 /* let ACPI work its magic */
1897 rc = ata_acpi_on_devcfg(dev);
1898 if (rc)
1899 return rc;
Kristen Carlson Accardi08573a82006-11-10 16:14:47 -08001900
Tejun Heo05027ad2007-09-03 12:32:57 +09001901 /* massage HPA, do it early as it might change IDENTIFY data */
1902 rc = ata_hpa_resize(dev);
1903 if (rc)
1904 return rc;
1905
Tejun Heoc39f5eb2006-03-13 19:51:19 +09001906 /* print device capabilities */
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001907 if (ata_msg_probe(ap))
Tejun Heo88574552006-06-25 20:00:35 +09001908 ata_dev_printk(dev, KERN_DEBUG,
1909 "%s: cfg 49:%04x 82:%04x 83:%04x 84:%04x "
1910 "85:%04x 86:%04x 87:%04x 88:%04x\n",
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001911 __FUNCTION__,
Tejun Heof15a1da2006-05-15 20:57:56 +09001912 id[49], id[82], id[83], id[84],
1913 id[85], id[86], id[87], id[88]);
Tejun Heoc39f5eb2006-03-13 19:51:19 +09001914
Tejun Heo208a9932006-03-05 17:55:58 +09001915 /* initialize to-be-configured parameters */
Tejun Heoea1dd4e2006-04-02 18:51:53 +09001916 dev->flags &= ~ATA_DFLAG_CFG_MASK;
Tejun Heo208a9932006-03-05 17:55:58 +09001917 dev->max_sectors = 0;
1918 dev->cdb_len = 0;
1919 dev->n_sectors = 0;
1920 dev->cylinders = 0;
1921 dev->heads = 0;
1922 dev->sectors = 0;
1923
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 /*
1925 * common ATA, ATAPI feature tests
1926 */
1927
Tejun Heoff8854b2006-03-06 04:31:56 +09001928 /* find max transfer mode; for printk only */
Tejun Heo1148c3a2006-03-13 19:48:04 +09001929 xfer_mask = ata_id_xfermask(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001931 if (ata_msg_probe(ap))
1932 ata_dump_id(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
Albert Leeef143d52007-06-05 13:01:33 +08001934 /* SCSI only uses 4-char revisions, dump full 8 chars from ATA */
1935 ata_id_c_string(dev->id, fwrevbuf, ATA_ID_FW_REV,
1936 sizeof(fwrevbuf));
1937
1938 ata_id_c_string(dev->id, modelbuf, ATA_ID_PROD,
1939 sizeof(modelbuf));
1940
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 /* ATA-specific feature tests */
1942 if (dev->class == ATA_DEV_ATA) {
Alan Coxb352e572006-08-10 18:52:12 +01001943 if (ata_id_is_cfa(id)) {
1944 if (id[162] & 1) /* CPRM may make this media unusable */
Tejun Heo44877b42007-02-21 01:06:51 +09001945 ata_dev_printk(dev, KERN_WARNING,
1946 "supports DRM functions and may "
1947 "not be fully accessable.\n");
Alan Coxb352e572006-08-10 18:52:12 +01001948 snprintf(revbuf, 7, "CFA");
1949 }
1950 else
1951 snprintf(revbuf, 7, "ATA-%d", ata_id_major_version(id));
1952
Tejun Heo1148c3a2006-03-13 19:48:04 +09001953 dev->n_sectors = ata_id_n_sectors(id);
Tejun Heo29407402006-02-12 22:47:04 +09001954
Eric D. Mudama3f64f562007-01-30 23:00:40 -07001955 if (dev->id[59] & 0x100)
1956 dev->multi_count = dev->id[59] & 0xff;
1957
Tejun Heo1148c3a2006-03-13 19:48:04 +09001958 if (ata_id_has_lba(id)) {
Tejun Heo4c2d7212006-03-05 17:55:58 +09001959 const char *lba_desc;
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001960 char ncq_desc[20];
Albert Lee8bf62ece2005-05-12 15:29:42 -04001961
Tejun Heo4c2d7212006-03-05 17:55:58 +09001962 lba_desc = "LBA";
1963 dev->flags |= ATA_DFLAG_LBA;
Tejun Heo1148c3a2006-03-13 19:48:04 +09001964 if (ata_id_has_lba48(id)) {
Albert Lee8bf62ece2005-05-12 15:29:42 -04001965 dev->flags |= ATA_DFLAG_LBA48;
Tejun Heo4c2d7212006-03-05 17:55:58 +09001966 lba_desc = "LBA48";
Tejun Heo6fc49ad2006-11-11 20:10:45 +09001967
1968 if (dev->n_sectors >= (1UL << 28) &&
1969 ata_id_has_flush_ext(id))
1970 dev->flags |= ATA_DFLAG_FLUSH_EXT;
Tejun Heo4c2d7212006-03-05 17:55:58 +09001971 }
Albert Lee8bf62ece2005-05-12 15:29:42 -04001972
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001973 /* config NCQ */
1974 ata_dev_config_ncq(dev, ncq_desc, sizeof(ncq_desc));
1975
Albert Lee8bf62ece2005-05-12 15:29:42 -04001976 /* print device info to dmesg */
Eric D. Mudama3f64f562007-01-30 23:00:40 -07001977 if (ata_msg_drv(ap) && print_info) {
1978 ata_dev_printk(dev, KERN_INFO,
1979 "%s: %s, %s, max %s\n",
1980 revbuf, modelbuf, fwrevbuf,
1981 ata_mode_string(xfer_mask));
1982 ata_dev_printk(dev, KERN_INFO,
1983 "%Lu sectors, multi %u: %s %s\n",
Tejun Heof15a1da2006-05-15 20:57:56 +09001984 (unsigned long long)dev->n_sectors,
Eric D. Mudama3f64f562007-01-30 23:00:40 -07001985 dev->multi_count, lba_desc, ncq_desc);
1986 }
Tejun Heoffeae412006-03-01 16:09:35 +09001987 } else {
Albert Lee8bf62ece2005-05-12 15:29:42 -04001988 /* CHS */
1989
1990 /* Default translation */
Tejun Heo1148c3a2006-03-13 19:48:04 +09001991 dev->cylinders = id[1];
1992 dev->heads = id[3];
1993 dev->sectors = id[6];
Albert Lee8bf62ece2005-05-12 15:29:42 -04001994
Tejun Heo1148c3a2006-03-13 19:48:04 +09001995 if (ata_id_current_chs_valid(id)) {
Albert Lee8bf62ece2005-05-12 15:29:42 -04001996 /* Current CHS translation is valid. */
Tejun Heo1148c3a2006-03-13 19:48:04 +09001997 dev->cylinders = id[54];
1998 dev->heads = id[55];
1999 dev->sectors = id[56];
Albert Lee8bf62ece2005-05-12 15:29:42 -04002000 }
2001
2002 /* print device info to dmesg */
Eric D. Mudama3f64f562007-01-30 23:00:40 -07002003 if (ata_msg_drv(ap) && print_info) {
Tejun Heo88574552006-06-25 20:00:35 +09002004 ata_dev_printk(dev, KERN_INFO,
Eric D. Mudama3f64f562007-01-30 23:00:40 -07002005 "%s: %s, %s, max %s\n",
2006 revbuf, modelbuf, fwrevbuf,
2007 ata_mode_string(xfer_mask));
Jeff Garzika84471f2007-02-26 05:51:33 -05002008 ata_dev_printk(dev, KERN_INFO,
Eric D. Mudama3f64f562007-01-30 23:00:40 -07002009 "%Lu sectors, multi %u, CHS %u/%u/%u\n",
2010 (unsigned long long)dev->n_sectors,
2011 dev->multi_count, dev->cylinders,
2012 dev->heads, dev->sectors);
2013 }
Albert Lee07f6f7d2005-11-01 19:33:20 +08002014 }
2015
Tejun Heo6e7846e2006-02-12 23:32:58 +09002016 dev->cdb_len = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 }
2018
2019 /* ATAPI-specific feature tests */
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05002020 else if (dev->class == ATA_DEV_ATAPI) {
Tejun Heo854c73a2007-09-23 13:14:11 +09002021 const char *cdb_intr_string = "";
2022 const char *atapi_an_string = "";
Tejun Heo7d77b242007-09-23 13:14:13 +09002023 u32 sntf;
Albert Lee08a556d2006-03-31 13:29:04 +08002024
Tejun Heo1148c3a2006-03-13 19:48:04 +09002025 rc = atapi_cdb_len(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002027 if (ata_msg_warn(ap))
Tejun Heo88574552006-06-25 20:00:35 +09002028 ata_dev_printk(dev, KERN_WARNING,
2029 "unsupported CDB len\n");
Tejun Heoffeae412006-03-01 16:09:35 +09002030 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 goto err_out_nosup;
2032 }
Tejun Heo6e7846e2006-02-12 23:32:58 +09002033 dev->cdb_len = (unsigned int) rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034
Tejun Heo7d77b242007-09-23 13:14:13 +09002035 /* Enable ATAPI AN if both the host and device have
2036 * the support. If PMP is attached, SNTF is required
2037 * to enable ATAPI AN to discern between PHY status
2038 * changed notifications and ATAPI ANs.
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04002039 */
Tejun Heo7d77b242007-09-23 13:14:13 +09002040 if ((ap->flags & ATA_FLAG_AN) && ata_id_has_atapi_AN(id) &&
2041 (!ap->nr_pmp_links ||
2042 sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf) == 0)) {
Tejun Heo854c73a2007-09-23 13:14:11 +09002043 unsigned int err_mask;
2044
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04002045 /* issue SET feature command to turn this on */
Tejun Heo854c73a2007-09-23 13:14:11 +09002046 err_mask = ata_dev_set_AN(dev, SETFEATURES_SATA_ENABLE);
2047 if (err_mask)
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04002048 ata_dev_printk(dev, KERN_ERR,
Tejun Heo854c73a2007-09-23 13:14:11 +09002049 "failed to enable ATAPI AN "
2050 "(err_mask=0x%x)\n", err_mask);
2051 else {
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04002052 dev->flags |= ATA_DFLAG_AN;
Tejun Heo854c73a2007-09-23 13:14:11 +09002053 atapi_an_string = ", ATAPI AN";
2054 }
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04002055 }
2056
Albert Lee08a556d2006-03-31 13:29:04 +08002057 if (ata_id_cdb_intr(dev->id)) {
Albert Lee312f7da2005-09-27 17:38:03 +08002058 dev->flags |= ATA_DFLAG_CDB_INTR;
Albert Lee08a556d2006-03-31 13:29:04 +08002059 cdb_intr_string = ", CDB intr";
2060 }
Albert Lee312f7da2005-09-27 17:38:03 +08002061
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 /* print device info to dmesg */
Borislav Petkov5afc8142006-06-27 14:51:25 +02002063 if (ata_msg_drv(ap) && print_info)
Albert Leeef143d52007-06-05 13:01:33 +08002064 ata_dev_printk(dev, KERN_INFO,
Tejun Heo854c73a2007-09-23 13:14:11 +09002065 "ATAPI: %s, %s, max %s%s%s\n",
Albert Leeef143d52007-06-05 13:01:33 +08002066 modelbuf, fwrevbuf,
Tejun Heo12436c32006-05-15 20:59:15 +09002067 ata_mode_string(xfer_mask),
Tejun Heo854c73a2007-09-23 13:14:11 +09002068 cdb_intr_string, atapi_an_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 }
2070
Tejun Heo914ed352006-11-01 18:39:55 +09002071 /* determine max_sectors */
2072 dev->max_sectors = ATA_MAX_SECTORS;
2073 if (dev->flags & ATA_DFLAG_LBA48)
2074 dev->max_sectors = ATA_MAX_SECTORS_LBA48;
2075
Alan Cox93590852006-09-12 16:55:12 +01002076 if (dev->horkage & ATA_HORKAGE_DIAGNOSTIC) {
2077 /* Let the user know. We don't want to disallow opens for
2078 rescue purposes, or in case the vendor is just a blithering
2079 idiot */
2080 if (print_info) {
2081 ata_dev_printk(dev, KERN_WARNING,
2082"Drive reports diagnostics failure. This may indicate a drive\n");
2083 ata_dev_printk(dev, KERN_WARNING,
2084"fault or invalid emulation. Contact drive vendor for information.\n");
2085 }
2086 }
2087
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09002088 /* limit bridge transfers to udma5, 200 sectors */
Tejun Heo3373efd2006-05-15 20:57:53 +09002089 if (ata_dev_knobble(dev)) {
Borislav Petkov5afc8142006-06-27 14:51:25 +02002090 if (ata_msg_drv(ap) && print_info)
Tejun Heof15a1da2006-05-15 20:57:56 +09002091 ata_dev_printk(dev, KERN_INFO,
2092 "applying bridge limits\n");
Tejun Heo5a529132006-03-24 14:07:50 +09002093 dev->udma_mask &= ATA_UDMA5;
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09002094 dev->max_sectors = ATA_MAX_SECTORS;
2095 }
2096
Tejun Heo75683fe2007-07-05 13:31:27 +09002097 if (dev->horkage & ATA_HORKAGE_MAX_SEC_128)
Tejun Heo03ec52d2007-04-12 13:38:11 +09002098 dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_128,
2099 dev->max_sectors);
Albert Lee18d6e9d2007-04-02 11:34:15 +08002100
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09002101 if (ap->ops->dev_config)
Alancd0d3bb2007-03-02 00:56:15 +00002102 ap->ops->dev_config(dev);
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09002103
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002104 if (ata_msg_probe(ap))
2105 ata_dev_printk(dev, KERN_DEBUG, "%s: EXIT, drv_stat = 0x%x\n",
2106 __FUNCTION__, ata_chk_status(ap));
Tejun Heoffeae412006-03-01 16:09:35 +09002107 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108
2109err_out_nosup:
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002110 if (ata_msg_probe(ap))
Tejun Heo88574552006-06-25 20:00:35 +09002111 ata_dev_printk(dev, KERN_DEBUG,
2112 "%s: EXIT, err\n", __FUNCTION__);
Tejun Heoffeae412006-03-01 16:09:35 +09002113 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114}
2115
2116/**
Alan Cox2e41e8e2007-03-08 23:19:19 +00002117 * ata_cable_40wire - return 40 wire cable type
Alan Coxbe0d18d2007-03-06 02:37:56 -08002118 * @ap: port
2119 *
Alan Cox2e41e8e2007-03-08 23:19:19 +00002120 * Helper method for drivers which want to hardwire 40 wire cable
Alan Coxbe0d18d2007-03-06 02:37:56 -08002121 * detection.
2122 */
2123
2124int ata_cable_40wire(struct ata_port *ap)
2125{
2126 return ATA_CBL_PATA40;
2127}
2128
2129/**
Alan Cox2e41e8e2007-03-08 23:19:19 +00002130 * ata_cable_80wire - return 80 wire cable type
Alan Coxbe0d18d2007-03-06 02:37:56 -08002131 * @ap: port
2132 *
Alan Cox2e41e8e2007-03-08 23:19:19 +00002133 * Helper method for drivers which want to hardwire 80 wire cable
Alan Coxbe0d18d2007-03-06 02:37:56 -08002134 * detection.
2135 */
2136
2137int ata_cable_80wire(struct ata_port *ap)
2138{
2139 return ATA_CBL_PATA80;
2140}
2141
2142/**
2143 * ata_cable_unknown - return unknown PATA cable.
2144 * @ap: port
2145 *
2146 * Helper method for drivers which have no PATA cable detection.
2147 */
2148
2149int ata_cable_unknown(struct ata_port *ap)
2150{
2151 return ATA_CBL_PATA_UNK;
2152}
2153
2154/**
2155 * ata_cable_sata - return SATA cable type
2156 * @ap: port
2157 *
2158 * Helper method for drivers which have SATA cables
2159 */
2160
2161int ata_cable_sata(struct ata_port *ap)
2162{
2163 return ATA_CBL_SATA;
2164}
2165
2166/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 * ata_bus_probe - Reset and probe ATA bus
2168 * @ap: Bus to probe
2169 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04002170 * Master ATA bus probing function. Initiates a hardware-dependent
2171 * bus reset, then attempts to identify any devices found on
2172 * the bus.
2173 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002175 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 *
2177 * RETURNS:
Tejun Heo96072e62006-04-01 01:38:17 +09002178 * Zero on success, negative errno otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 */
2180
Brian King80289162006-08-07 14:27:31 -05002181int ata_bus_probe(struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182{
Tejun Heo28ca5c52006-03-01 16:09:36 +09002183 unsigned int classes[ATA_MAX_DEVICES];
Tejun Heo14d2bac2006-04-02 17:54:46 +09002184 int tries[ATA_MAX_DEVICES];
Tejun Heof58229f2007-08-06 18:36:23 +09002185 int rc;
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002186 struct ata_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187
Tejun Heo28ca5c52006-03-01 16:09:36 +09002188 ata_port_probe(ap);
2189
Tejun Heof58229f2007-08-06 18:36:23 +09002190 ata_link_for_each_dev(dev, &ap->link)
2191 tries[dev->devno] = ATA_PROBE_MAX_TRIES;
Tejun Heo14d2bac2006-04-02 17:54:46 +09002192
2193 retry:
Tejun Heo20444702006-03-13 01:57:01 +09002194 /* reset and determine device classes */
Tejun Heo52783c52006-05-31 18:28:22 +09002195 ap->ops->phy_reset(ap);
Tejun Heo2061a472006-03-12 00:57:39 +09002196
Tejun Heof58229f2007-08-06 18:36:23 +09002197 ata_link_for_each_dev(dev, &ap->link) {
Tejun Heo52783c52006-05-31 18:28:22 +09002198 if (!(ap->flags & ATA_FLAG_DISABLED) &&
2199 dev->class != ATA_DEV_UNKNOWN)
2200 classes[dev->devno] = dev->class;
2201 else
2202 classes[dev->devno] = ATA_DEV_NONE;
Tejun Heo20444702006-03-13 01:57:01 +09002203
Tejun Heo52783c52006-05-31 18:28:22 +09002204 dev->class = ATA_DEV_UNKNOWN;
Tejun Heo28ca5c52006-03-01 16:09:36 +09002205 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206
Tejun Heo52783c52006-05-31 18:28:22 +09002207 ata_port_probe(ap);
Tejun Heo20444702006-03-13 01:57:01 +09002208
Alan Coxb6079ca2006-05-22 16:52:06 +01002209 /* after the reset the device state is PIO 0 and the controller
2210 state is undefined. Record the mode */
2211
Tejun Heof58229f2007-08-06 18:36:23 +09002212 ata_link_for_each_dev(dev, &ap->link)
2213 dev->pio_mode = XFER_PIO_0;
Alan Coxb6079ca2006-05-22 16:52:06 +01002214
Jeff Garzikf31f0cc2007-03-02 17:47:28 -05002215 /* read IDENTIFY page and configure devices. We have to do the identify
2216 specific sequence bass-ackwards so that PDIAG- is released by
2217 the slave device */
2218
Tejun Heof58229f2007-08-06 18:36:23 +09002219 ata_link_for_each_dev(dev, &ap->link) {
2220 if (tries[dev->devno])
2221 dev->class = classes[dev->devno];
Tejun Heo14d2bac2006-04-02 17:54:46 +09002222
Tejun Heoe1211e32006-04-01 01:38:18 +09002223 if (!ata_dev_enabled(dev))
Tejun Heoffeae412006-03-01 16:09:35 +09002224 continue;
2225
Tejun Heobff04642006-11-10 18:08:10 +09002226 rc = ata_dev_read_id(dev, &dev->class, ATA_READID_POSTRESET,
2227 dev->id);
Tejun Heo14d2bac2006-04-02 17:54:46 +09002228 if (rc)
2229 goto fail;
Jeff Garzikf31f0cc2007-03-02 17:47:28 -05002230 }
2231
Alan Coxbe0d18d2007-03-06 02:37:56 -08002232 /* Now ask for the cable type as PDIAG- should have been released */
2233 if (ap->ops->cable_detect)
2234 ap->cbl = ap->ops->cable_detect(ap);
2235
Alan Cox614fe292007-08-22 23:22:45 +01002236 /* We may have SATA bridge glue hiding here irrespective of the
2237 reported cable types and sensed types */
2238 ata_link_for_each_dev(dev, &ap->link) {
2239 if (!ata_dev_enabled(dev))
2240 continue;
2241 /* SATA drives indicate we have a bridge. We don't know which
2242 end of the link the bridge is which is a problem */
2243 if (ata_id_is_sata(dev->id))
2244 ap->cbl = ATA_CBL_SATA;
2245 }
2246
Jeff Garzikf31f0cc2007-03-02 17:47:28 -05002247 /* After the identify sequence we can now set up the devices. We do
2248 this in the normal order so that the user doesn't get confused */
2249
Tejun Heof58229f2007-08-06 18:36:23 +09002250 ata_link_for_each_dev(dev, &ap->link) {
Jeff Garzikf31f0cc2007-03-02 17:47:28 -05002251 if (!ata_dev_enabled(dev))
2252 continue;
Tejun Heoffeae412006-03-01 16:09:35 +09002253
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002254 ap->link.eh_context.i.flags |= ATA_EHI_PRINTINFO;
Tejun Heoefdaedc2006-11-01 18:38:52 +09002255 rc = ata_dev_configure(dev);
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002256 ap->link.eh_context.i.flags &= ~ATA_EHI_PRINTINFO;
Tejun Heo14d2bac2006-04-02 17:54:46 +09002257 if (rc)
2258 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 }
2260
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002261 /* configure transfer mode */
Tejun Heo02607312007-08-06 18:36:23 +09002262 rc = ata_set_mode(&ap->link, &dev);
Tejun Heo4ae72a12007-02-02 16:22:30 +09002263 if (rc)
Tejun Heo51713d32006-04-11 22:26:29 +09002264 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265
Tejun Heof58229f2007-08-06 18:36:23 +09002266 ata_link_for_each_dev(dev, &ap->link)
2267 if (ata_dev_enabled(dev))
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002268 return 0;
Alan Coxe35a9e02006-03-27 18:46:37 +01002269
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002270 /* no device present, disable port */
2271 ata_port_disable(ap);
Tejun Heo96072e62006-04-01 01:38:17 +09002272 return -ENODEV;
Tejun Heo14d2bac2006-04-02 17:54:46 +09002273
2274 fail:
Tejun Heo4ae72a12007-02-02 16:22:30 +09002275 tries[dev->devno]--;
2276
Tejun Heo14d2bac2006-04-02 17:54:46 +09002277 switch (rc) {
2278 case -EINVAL:
Tejun Heo4ae72a12007-02-02 16:22:30 +09002279 /* eeek, something went very wrong, give up */
Tejun Heo14d2bac2006-04-02 17:54:46 +09002280 tries[dev->devno] = 0;
2281 break;
Tejun Heo4ae72a12007-02-02 16:22:30 +09002282
2283 case -ENODEV:
2284 /* give it just one more chance */
2285 tries[dev->devno] = min(tries[dev->devno], 1);
Tejun Heo14d2bac2006-04-02 17:54:46 +09002286 case -EIO:
Tejun Heo4ae72a12007-02-02 16:22:30 +09002287 if (tries[dev->devno] == 1) {
2288 /* This is the last chance, better to slow
2289 * down than lose it.
2290 */
Tejun Heo936fd732007-08-06 18:36:23 +09002291 sata_down_spd_limit(&ap->link);
Tejun Heo4ae72a12007-02-02 16:22:30 +09002292 ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
2293 }
Tejun Heo14d2bac2006-04-02 17:54:46 +09002294 }
2295
Tejun Heo4ae72a12007-02-02 16:22:30 +09002296 if (!tries[dev->devno])
Tejun Heo3373efd2006-05-15 20:57:53 +09002297 ata_dev_disable(dev);
Tejun Heoec573752006-04-11 22:26:29 +09002298
Tejun Heo14d2bac2006-04-02 17:54:46 +09002299 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300}
2301
2302/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002303 * ata_port_probe - Mark port as enabled
2304 * @ap: Port for which we indicate enablement
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04002306 * Modify @ap data structure such that the system
2307 * thinks that the entire port is enabled.
2308 *
Jeff Garzikcca39742006-08-24 03:19:22 -04002309 * LOCKING: host lock, or some other form of
Jeff Garzik0cba6322005-05-30 19:49:12 -04002310 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 */
2312
2313void ata_port_probe(struct ata_port *ap)
2314{
Tejun Heo198e0fe2006-04-02 18:51:52 +09002315 ap->flags &= ~ATA_FLAG_DISABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002316}
2317
2318/**
Tejun Heo3be680b2005-12-19 22:35:02 +09002319 * sata_print_link_status - Print SATA link status
Tejun Heo936fd732007-08-06 18:36:23 +09002320 * @link: SATA link to printk link status about
Tejun Heo3be680b2005-12-19 22:35:02 +09002321 *
2322 * This function prints link speed and status of a SATA link.
2323 *
2324 * LOCKING:
2325 * None.
2326 */
Tejun Heo936fd732007-08-06 18:36:23 +09002327void sata_print_link_status(struct ata_link *link)
Tejun Heo3be680b2005-12-19 22:35:02 +09002328{
Tejun Heo6d5f9732006-04-03 00:09:41 +09002329 u32 sstatus, scontrol, tmp;
Tejun Heo3be680b2005-12-19 22:35:02 +09002330
Tejun Heo936fd732007-08-06 18:36:23 +09002331 if (sata_scr_read(link, SCR_STATUS, &sstatus))
Tejun Heo3be680b2005-12-19 22:35:02 +09002332 return;
Tejun Heo936fd732007-08-06 18:36:23 +09002333 sata_scr_read(link, SCR_CONTROL, &scontrol);
Tejun Heo3be680b2005-12-19 22:35:02 +09002334
Tejun Heo936fd732007-08-06 18:36:23 +09002335 if (ata_link_online(link)) {
Tejun Heo3be680b2005-12-19 22:35:02 +09002336 tmp = (sstatus >> 4) & 0xf;
Tejun Heo936fd732007-08-06 18:36:23 +09002337 ata_link_printk(link, KERN_INFO,
Tejun Heof15a1da2006-05-15 20:57:56 +09002338 "SATA link up %s (SStatus %X SControl %X)\n",
2339 sata_spd_string(tmp), sstatus, scontrol);
Tejun Heo3be680b2005-12-19 22:35:02 +09002340 } else {
Tejun Heo936fd732007-08-06 18:36:23 +09002341 ata_link_printk(link, KERN_INFO,
Tejun Heof15a1da2006-05-15 20:57:56 +09002342 "SATA link down (SStatus %X SControl %X)\n",
2343 sstatus, scontrol);
Tejun Heo3be680b2005-12-19 22:35:02 +09002344 }
2345}
2346
2347/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04002348 * __sata_phy_reset - Wake/reset a low-level SATA PHY
2349 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002351 * This function issues commands to standard SATA Sxxx
2352 * PHY registers, to wake up the phy (and device), and
2353 * clear any reset condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 *
2355 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002356 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 *
2358 */
2359void __sata_phy_reset(struct ata_port *ap)
2360{
Tejun Heo936fd732007-08-06 18:36:23 +09002361 struct ata_link *link = &ap->link;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 unsigned long timeout = jiffies + (HZ * 5);
Tejun Heo936fd732007-08-06 18:36:23 +09002363 u32 sstatus;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364
2365 if (ap->flags & ATA_FLAG_SATA_RESET) {
Brett Russcdcca89e2005-03-28 15:10:27 -05002366 /* issue phy wake/reset */
Tejun Heo936fd732007-08-06 18:36:23 +09002367 sata_scr_write_flush(link, SCR_CONTROL, 0x301);
Tejun Heo62ba2842005-06-26 23:27:19 +09002368 /* Couldn't find anything in SATA I/II specs, but
2369 * AHCI-1.1 10.4.2 says at least 1 ms. */
2370 mdelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 }
Tejun Heo81952c52006-05-15 20:57:47 +09002372 /* phy wake/clear reset */
Tejun Heo936fd732007-08-06 18:36:23 +09002373 sata_scr_write_flush(link, SCR_CONTROL, 0x300);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374
2375 /* wait for phy to become ready, if necessary */
2376 do {
2377 msleep(200);
Tejun Heo936fd732007-08-06 18:36:23 +09002378 sata_scr_read(link, SCR_STATUS, &sstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 if ((sstatus & 0xf) != 1)
2380 break;
2381 } while (time_before(jiffies, timeout));
2382
Tejun Heo3be680b2005-12-19 22:35:02 +09002383 /* print link status */
Tejun Heo936fd732007-08-06 18:36:23 +09002384 sata_print_link_status(link);
Jeff Garzik656563e2005-11-20 03:36:45 -05002385
Tejun Heo3be680b2005-12-19 22:35:02 +09002386 /* TODO: phy layer with polling, timeouts, etc. */
Tejun Heo936fd732007-08-06 18:36:23 +09002387 if (!ata_link_offline(link))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 ata_port_probe(ap);
Tejun Heo3be680b2005-12-19 22:35:02 +09002389 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 ata_port_disable(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391
Tejun Heo198e0fe2006-04-02 18:51:52 +09002392 if (ap->flags & ATA_FLAG_DISABLED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 return;
2394
2395 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
2396 ata_port_disable(ap);
2397 return;
2398 }
2399
2400 ap->cbl = ATA_CBL_SATA;
2401}
2402
2403/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04002404 * sata_phy_reset - Reset SATA bus.
2405 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002407 * This function resets the SATA bus, and then probes
2408 * the bus for devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 *
2410 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002411 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412 *
2413 */
2414void sata_phy_reset(struct ata_port *ap)
2415{
2416 __sata_phy_reset(ap);
Tejun Heo198e0fe2006-04-02 18:51:52 +09002417 if (ap->flags & ATA_FLAG_DISABLED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 return;
2419 ata_bus_reset(ap);
2420}
2421
2422/**
Alan Coxebdfca62006-03-23 15:38:34 +00002423 * ata_dev_pair - return other device on cable
Alan Coxebdfca62006-03-23 15:38:34 +00002424 * @adev: device
2425 *
2426 * Obtain the other device on the same cable, or if none is
2427 * present NULL is returned
2428 */
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002429
Tejun Heo3373efd2006-05-15 20:57:53 +09002430struct ata_device *ata_dev_pair(struct ata_device *adev)
Alan Coxebdfca62006-03-23 15:38:34 +00002431{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002432 struct ata_link *link = adev->link;
2433 struct ata_device *pair = &link->device[1 - adev->devno];
Tejun Heoe1211e32006-04-01 01:38:18 +09002434 if (!ata_dev_enabled(pair))
Alan Coxebdfca62006-03-23 15:38:34 +00002435 return NULL;
2436 return pair;
2437}
2438
2439/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04002440 * ata_port_disable - Disable port.
2441 * @ap: Port to be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002442 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002443 * Modify @ap data structure such that the system
2444 * thinks that the entire port is disabled, and should
2445 * never attempt to probe or communicate with devices
2446 * on this port.
2447 *
Jeff Garzikcca39742006-08-24 03:19:22 -04002448 * LOCKING: host lock, or some other form of
Jeff Garzik780a87f2005-05-30 15:41:05 -04002449 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 */
2451
2452void ata_port_disable(struct ata_port *ap)
2453{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002454 ap->link.device[0].class = ATA_DEV_NONE;
2455 ap->link.device[1].class = ATA_DEV_NONE;
Tejun Heo198e0fe2006-04-02 18:51:52 +09002456 ap->flags |= ATA_FLAG_DISABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457}
2458
Tejun Heo1c3fae42006-04-02 20:53:28 +09002459/**
Tejun Heo3c567b72006-05-15 20:57:23 +09002460 * sata_down_spd_limit - adjust SATA spd limit downward
Tejun Heo936fd732007-08-06 18:36:23 +09002461 * @link: Link to adjust SATA spd limit for
Tejun Heo1c3fae42006-04-02 20:53:28 +09002462 *
Tejun Heo936fd732007-08-06 18:36:23 +09002463 * Adjust SATA spd limit of @link downward. Note that this
Tejun Heo1c3fae42006-04-02 20:53:28 +09002464 * function only adjusts the limit. The change must be applied
Tejun Heo3c567b72006-05-15 20:57:23 +09002465 * using sata_set_spd().
Tejun Heo1c3fae42006-04-02 20:53:28 +09002466 *
2467 * LOCKING:
2468 * Inherited from caller.
2469 *
2470 * RETURNS:
2471 * 0 on success, negative errno on failure
2472 */
Tejun Heo936fd732007-08-06 18:36:23 +09002473int sata_down_spd_limit(struct ata_link *link)
Tejun Heo1c3fae42006-04-02 20:53:28 +09002474{
Tejun Heo81952c52006-05-15 20:57:47 +09002475 u32 sstatus, spd, mask;
2476 int rc, highbit;
Tejun Heo1c3fae42006-04-02 20:53:28 +09002477
Tejun Heo936fd732007-08-06 18:36:23 +09002478 if (!sata_scr_valid(link))
Tejun Heo008a7892007-07-16 14:29:40 +09002479 return -EOPNOTSUPP;
2480
2481 /* If SCR can be read, use it to determine the current SPD.
Tejun Heo936fd732007-08-06 18:36:23 +09002482 * If not, use cached value in link->sata_spd.
Tejun Heo008a7892007-07-16 14:29:40 +09002483 */
Tejun Heo936fd732007-08-06 18:36:23 +09002484 rc = sata_scr_read(link, SCR_STATUS, &sstatus);
Tejun Heo008a7892007-07-16 14:29:40 +09002485 if (rc == 0)
2486 spd = (sstatus >> 4) & 0xf;
2487 else
Tejun Heo936fd732007-08-06 18:36:23 +09002488 spd = link->sata_spd;
Tejun Heo1c3fae42006-04-02 20:53:28 +09002489
Tejun Heo936fd732007-08-06 18:36:23 +09002490 mask = link->sata_spd_limit;
Tejun Heo1c3fae42006-04-02 20:53:28 +09002491 if (mask <= 1)
2492 return -EINVAL;
Tejun Heo008a7892007-07-16 14:29:40 +09002493
2494 /* unconditionally mask off the highest bit */
Tejun Heo1c3fae42006-04-02 20:53:28 +09002495 highbit = fls(mask) - 1;
2496 mask &= ~(1 << highbit);
2497
Tejun Heo008a7892007-07-16 14:29:40 +09002498 /* Mask off all speeds higher than or equal to the current
2499 * one. Force 1.5Gbps if current SPD is not available.
2500 */
2501 if (spd > 1)
2502 mask &= (1 << (spd - 1)) - 1;
2503 else
2504 mask &= 1;
2505
2506 /* were we already at the bottom? */
Tejun Heo1c3fae42006-04-02 20:53:28 +09002507 if (!mask)
2508 return -EINVAL;
2509
Tejun Heo936fd732007-08-06 18:36:23 +09002510 link->sata_spd_limit = mask;
Tejun Heo1c3fae42006-04-02 20:53:28 +09002511
Tejun Heo936fd732007-08-06 18:36:23 +09002512 ata_link_printk(link, KERN_WARNING, "limiting SATA link speed to %s\n",
Tejun Heof15a1da2006-05-15 20:57:56 +09002513 sata_spd_string(fls(mask)));
Tejun Heo1c3fae42006-04-02 20:53:28 +09002514
2515 return 0;
2516}
2517
Tejun Heo936fd732007-08-06 18:36:23 +09002518static int __sata_set_spd_needed(struct ata_link *link, u32 *scontrol)
Tejun Heo1c3fae42006-04-02 20:53:28 +09002519{
2520 u32 spd, limit;
2521
Tejun Heo936fd732007-08-06 18:36:23 +09002522 if (link->sata_spd_limit == UINT_MAX)
Tejun Heo1c3fae42006-04-02 20:53:28 +09002523 limit = 0;
2524 else
Tejun Heo936fd732007-08-06 18:36:23 +09002525 limit = fls(link->sata_spd_limit);
Tejun Heo1c3fae42006-04-02 20:53:28 +09002526
2527 spd = (*scontrol >> 4) & 0xf;
2528 *scontrol = (*scontrol & ~0xf0) | ((limit & 0xf) << 4);
2529
2530 return spd != limit;
2531}
2532
2533/**
Tejun Heo3c567b72006-05-15 20:57:23 +09002534 * sata_set_spd_needed - is SATA spd configuration needed
Tejun Heo936fd732007-08-06 18:36:23 +09002535 * @link: Link in question
Tejun Heo1c3fae42006-04-02 20:53:28 +09002536 *
2537 * Test whether the spd limit in SControl matches
Tejun Heo936fd732007-08-06 18:36:23 +09002538 * @link->sata_spd_limit. This function is used to determine
Tejun Heo1c3fae42006-04-02 20:53:28 +09002539 * whether hardreset is necessary to apply SATA spd
2540 * configuration.
2541 *
2542 * LOCKING:
2543 * Inherited from caller.
2544 *
2545 * RETURNS:
2546 * 1 if SATA spd configuration is needed, 0 otherwise.
2547 */
Tejun Heo936fd732007-08-06 18:36:23 +09002548int sata_set_spd_needed(struct ata_link *link)
Tejun Heo1c3fae42006-04-02 20:53:28 +09002549{
2550 u32 scontrol;
2551
Tejun Heo936fd732007-08-06 18:36:23 +09002552 if (sata_scr_read(link, SCR_CONTROL, &scontrol))
Tejun Heo1c3fae42006-04-02 20:53:28 +09002553 return 0;
2554
Tejun Heo936fd732007-08-06 18:36:23 +09002555 return __sata_set_spd_needed(link, &scontrol);
Tejun Heo1c3fae42006-04-02 20:53:28 +09002556}
2557
2558/**
Tejun Heo3c567b72006-05-15 20:57:23 +09002559 * sata_set_spd - set SATA spd according to spd limit
Tejun Heo936fd732007-08-06 18:36:23 +09002560 * @link: Link to set SATA spd for
Tejun Heo1c3fae42006-04-02 20:53:28 +09002561 *
Tejun Heo936fd732007-08-06 18:36:23 +09002562 * Set SATA spd of @link according to sata_spd_limit.
Tejun Heo1c3fae42006-04-02 20:53:28 +09002563 *
2564 * LOCKING:
2565 * Inherited from caller.
2566 *
2567 * RETURNS:
2568 * 0 if spd doesn't need to be changed, 1 if spd has been
Tejun Heo81952c52006-05-15 20:57:47 +09002569 * changed. Negative errno if SCR registers are inaccessible.
Tejun Heo1c3fae42006-04-02 20:53:28 +09002570 */
Tejun Heo936fd732007-08-06 18:36:23 +09002571int sata_set_spd(struct ata_link *link)
Tejun Heo1c3fae42006-04-02 20:53:28 +09002572{
2573 u32 scontrol;
Tejun Heo81952c52006-05-15 20:57:47 +09002574 int rc;
Tejun Heo1c3fae42006-04-02 20:53:28 +09002575
Tejun Heo936fd732007-08-06 18:36:23 +09002576 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
Tejun Heo81952c52006-05-15 20:57:47 +09002577 return rc;
Tejun Heo1c3fae42006-04-02 20:53:28 +09002578
Tejun Heo936fd732007-08-06 18:36:23 +09002579 if (!__sata_set_spd_needed(link, &scontrol))
Tejun Heo1c3fae42006-04-02 20:53:28 +09002580 return 0;
2581
Tejun Heo936fd732007-08-06 18:36:23 +09002582 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
Tejun Heo81952c52006-05-15 20:57:47 +09002583 return rc;
2584
Tejun Heo1c3fae42006-04-02 20:53:28 +09002585 return 1;
2586}
2587
Alan Cox452503f2005-10-21 19:01:32 -04002588/*
2589 * This mode timing computation functionality is ported over from
2590 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
2591 */
2592/*
Alan Coxb352e572006-08-10 18:52:12 +01002593 * PIO 0-4, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
Alan Cox452503f2005-10-21 19:01:32 -04002594 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
Alan Coxb352e572006-08-10 18:52:12 +01002595 * for UDMA6, which is currently supported only by Maxtor drives.
2596 *
2597 * For PIO 5/6 MWDMA 3/4 see the CFA specification 3.0.
Alan Cox452503f2005-10-21 19:01:32 -04002598 */
2599
2600static const struct ata_timing ata_timing[] = {
2601
2602 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
2603 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
2604 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
2605 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
2606
Alan Coxb352e572006-08-10 18:52:12 +01002607 { XFER_MW_DMA_4, 25, 0, 0, 0, 55, 20, 80, 0 },
2608 { XFER_MW_DMA_3, 25, 0, 0, 0, 65, 25, 100, 0 },
Alan Cox452503f2005-10-21 19:01:32 -04002609 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
2610 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
2611 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
2612
2613/* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002614
Alan Cox452503f2005-10-21 19:01:32 -04002615 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
2616 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
2617 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002618
Alan Cox452503f2005-10-21 19:01:32 -04002619 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
2620 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
2621 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
2622
Alan Coxb352e572006-08-10 18:52:12 +01002623 { XFER_PIO_6, 10, 55, 20, 80, 55, 20, 80, 0 },
2624 { XFER_PIO_5, 15, 65, 25, 100, 65, 25, 100, 0 },
Alan Cox452503f2005-10-21 19:01:32 -04002625 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
2626 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
2627
2628 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
2629 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
2630 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
2631
2632/* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
2633
2634 { 0xFF }
2635};
2636
2637#define ENOUGH(v,unit) (((v)-1)/(unit)+1)
2638#define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
2639
2640static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
2641{
2642 q->setup = EZ(t->setup * 1000, T);
2643 q->act8b = EZ(t->act8b * 1000, T);
2644 q->rec8b = EZ(t->rec8b * 1000, T);
2645 q->cyc8b = EZ(t->cyc8b * 1000, T);
2646 q->active = EZ(t->active * 1000, T);
2647 q->recover = EZ(t->recover * 1000, T);
2648 q->cycle = EZ(t->cycle * 1000, T);
2649 q->udma = EZ(t->udma * 1000, UT);
2650}
2651
2652void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
2653 struct ata_timing *m, unsigned int what)
2654{
2655 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
2656 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
2657 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
2658 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
2659 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
2660 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
2661 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
2662 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
2663}
2664
2665static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
2666{
2667 const struct ata_timing *t;
2668
2669 for (t = ata_timing; t->mode != speed; t++)
Alan Cox91190752005-10-26 12:17:46 -04002670 if (t->mode == 0xFF)
Alan Cox452503f2005-10-21 19:01:32 -04002671 return NULL;
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002672 return t;
Alan Cox452503f2005-10-21 19:01:32 -04002673}
2674
2675int ata_timing_compute(struct ata_device *adev, unsigned short speed,
2676 struct ata_timing *t, int T, int UT)
2677{
2678 const struct ata_timing *s;
2679 struct ata_timing p;
2680
2681 /*
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002682 * Find the mode.
Albert Lee75b1f2f2005-11-16 17:06:18 +08002683 */
Alan Cox452503f2005-10-21 19:01:32 -04002684
2685 if (!(s = ata_timing_find_mode(speed)))
2686 return -EINVAL;
2687
Albert Lee75b1f2f2005-11-16 17:06:18 +08002688 memcpy(t, s, sizeof(*s));
2689
Alan Cox452503f2005-10-21 19:01:32 -04002690 /*
2691 * If the drive is an EIDE drive, it can tell us it needs extended
2692 * PIO/MW_DMA cycle timing.
2693 */
2694
2695 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
2696 memset(&p, 0, sizeof(p));
2697 if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
2698 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
2699 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
2700 } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
2701 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
2702 }
2703 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
2704 }
2705
2706 /*
2707 * Convert the timing to bus clock counts.
2708 */
2709
Albert Lee75b1f2f2005-11-16 17:06:18 +08002710 ata_timing_quantize(t, t, T, UT);
Alan Cox452503f2005-10-21 19:01:32 -04002711
2712 /*
Randy Dunlapc893a3a2006-01-28 13:15:32 -05002713 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
2714 * S.M.A.R.T * and some other commands. We have to ensure that the
2715 * DMA cycle timing is slower/equal than the fastest PIO timing.
Alan Cox452503f2005-10-21 19:01:32 -04002716 */
2717
Alanfd3367af2006-12-07 12:41:18 +00002718 if (speed > XFER_PIO_6) {
Alan Cox452503f2005-10-21 19:01:32 -04002719 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
2720 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
2721 }
2722
2723 /*
Randy Dunlapc893a3a2006-01-28 13:15:32 -05002724 * Lengthen active & recovery time so that cycle time is correct.
Alan Cox452503f2005-10-21 19:01:32 -04002725 */
2726
2727 if (t->act8b + t->rec8b < t->cyc8b) {
2728 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
2729 t->rec8b = t->cyc8b - t->act8b;
2730 }
2731
2732 if (t->active + t->recover < t->cycle) {
2733 t->active += (t->cycle - (t->active + t->recover)) / 2;
2734 t->recover = t->cycle - t->active;
2735 }
Jeff Garzika617c092007-05-21 20:14:23 -04002736
Alan Cox4f701d12007-04-23 11:55:36 +01002737 /* In a few cases quantisation may produce enough errors to
2738 leave t->cycle too low for the sum of active and recovery
2739 if so we must correct this */
2740 if (t->active + t->recover > t->cycle)
2741 t->cycle = t->active + t->recover;
Alan Cox452503f2005-10-21 19:01:32 -04002742
2743 return 0;
2744}
2745
Tejun Heocf176e12006-04-02 17:54:46 +09002746/**
2747 * ata_down_xfermask_limit - adjust dev xfer masks downward
Tejun Heocf176e12006-04-02 17:54:46 +09002748 * @dev: Device to adjust xfer masks
Tejun Heo458337d2007-02-02 16:22:30 +09002749 * @sel: ATA_DNXFER_* selector
Tejun Heocf176e12006-04-02 17:54:46 +09002750 *
2751 * Adjust xfer masks of @dev downward. Note that this function
2752 * does not apply the change. Invoking ata_set_mode() afterwards
2753 * will apply the limit.
2754 *
2755 * LOCKING:
2756 * Inherited from caller.
2757 *
2758 * RETURNS:
2759 * 0 on success, negative errno on failure
2760 */
Tejun Heo458337d2007-02-02 16:22:30 +09002761int ata_down_xfermask_limit(struct ata_device *dev, unsigned int sel)
Tejun Heocf176e12006-04-02 17:54:46 +09002762{
Tejun Heo458337d2007-02-02 16:22:30 +09002763 char buf[32];
2764 unsigned int orig_mask, xfer_mask;
2765 unsigned int pio_mask, mwdma_mask, udma_mask;
2766 int quiet, highbit;
Tejun Heocf176e12006-04-02 17:54:46 +09002767
Tejun Heo458337d2007-02-02 16:22:30 +09002768 quiet = !!(sel & ATA_DNXFER_QUIET);
2769 sel &= ~ATA_DNXFER_QUIET;
Tejun Heocf176e12006-04-02 17:54:46 +09002770
Tejun Heo458337d2007-02-02 16:22:30 +09002771 xfer_mask = orig_mask = ata_pack_xfermask(dev->pio_mask,
2772 dev->mwdma_mask,
2773 dev->udma_mask);
2774 ata_unpack_xfermask(xfer_mask, &pio_mask, &mwdma_mask, &udma_mask);
Tejun Heocf176e12006-04-02 17:54:46 +09002775
Tejun Heo458337d2007-02-02 16:22:30 +09002776 switch (sel) {
2777 case ATA_DNXFER_PIO:
2778 highbit = fls(pio_mask) - 1;
2779 pio_mask &= ~(1 << highbit);
2780 break;
2781
2782 case ATA_DNXFER_DMA:
2783 if (udma_mask) {
2784 highbit = fls(udma_mask) - 1;
2785 udma_mask &= ~(1 << highbit);
2786 if (!udma_mask)
2787 return -ENOENT;
2788 } else if (mwdma_mask) {
2789 highbit = fls(mwdma_mask) - 1;
2790 mwdma_mask &= ~(1 << highbit);
2791 if (!mwdma_mask)
2792 return -ENOENT;
2793 }
2794 break;
2795
2796 case ATA_DNXFER_40C:
2797 udma_mask &= ATA_UDMA_MASK_40C;
2798 break;
2799
2800 case ATA_DNXFER_FORCE_PIO0:
2801 pio_mask &= 1;
2802 case ATA_DNXFER_FORCE_PIO:
2803 mwdma_mask = 0;
2804 udma_mask = 0;
2805 break;
2806
Tejun Heo458337d2007-02-02 16:22:30 +09002807 default:
2808 BUG();
2809 }
2810
2811 xfer_mask &= ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
2812
2813 if (!(xfer_mask & ATA_MASK_PIO) || xfer_mask == orig_mask)
2814 return -ENOENT;
2815
2816 if (!quiet) {
2817 if (xfer_mask & (ATA_MASK_MWDMA | ATA_MASK_UDMA))
2818 snprintf(buf, sizeof(buf), "%s:%s",
2819 ata_mode_string(xfer_mask),
2820 ata_mode_string(xfer_mask & ATA_MASK_PIO));
2821 else
2822 snprintf(buf, sizeof(buf), "%s",
2823 ata_mode_string(xfer_mask));
2824
2825 ata_dev_printk(dev, KERN_WARNING,
2826 "limiting speed to %s\n", buf);
2827 }
Tejun Heocf176e12006-04-02 17:54:46 +09002828
2829 ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask,
2830 &dev->udma_mask);
2831
Tejun Heocf176e12006-04-02 17:54:46 +09002832 return 0;
Tejun Heocf176e12006-04-02 17:54:46 +09002833}
2834
Tejun Heo3373efd2006-05-15 20:57:53 +09002835static int ata_dev_set_mode(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002837 struct ata_eh_context *ehc = &dev->link->eh_context;
Tejun Heo83206a22006-03-24 15:25:31 +09002838 unsigned int err_mask;
2839 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840
Tejun Heoe8384602006-04-02 18:51:53 +09002841 dev->flags &= ~ATA_DFLAG_PIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002842 if (dev->xfer_shift == ATA_SHIFT_PIO)
2843 dev->flags |= ATA_DFLAG_PIO;
2844
Tejun Heo3373efd2006-05-15 20:57:53 +09002845 err_mask = ata_dev_set_xfermode(dev);
Alan11750a42007-02-05 16:28:30 +00002846 /* Old CFA may refuse this command, which is just fine */
2847 if (dev->xfer_shift == ATA_SHIFT_PIO && ata_id_is_cfa(dev->id))
2848 err_mask &= ~AC_ERR_DEV;
Alan Cox0bc2a792007-07-31 14:01:48 +01002849 /* Some very old devices and some bad newer ones fail any kind of
2850 SET_XFERMODE request but support PIO0-2 timings and no IORDY */
2851 if (dev->xfer_shift == ATA_SHIFT_PIO && !ata_id_has_iordy(dev->id) &&
2852 dev->pio_mode <= XFER_PIO_2)
2853 err_mask &= ~AC_ERR_DEV;
Tejun Heo83206a22006-03-24 15:25:31 +09002854 if (err_mask) {
Tejun Heof15a1da2006-05-15 20:57:56 +09002855 ata_dev_printk(dev, KERN_ERR, "failed to set xfermode "
2856 "(err_mask=0x%x)\n", err_mask);
Tejun Heo83206a22006-03-24 15:25:31 +09002857 return -EIO;
2858 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859
Tejun Heobaa1e782006-11-01 18:39:27 +09002860 ehc->i.flags |= ATA_EHI_POST_SETMODE;
Tejun Heo422c9da2007-09-23 13:14:12 +09002861 rc = ata_dev_revalidate(dev, ATA_DEV_UNKNOWN, 0);
Tejun Heobaa1e782006-11-01 18:39:27 +09002862 ehc->i.flags &= ~ATA_EHI_POST_SETMODE;
Tejun Heo5eb45c02006-04-02 18:51:52 +09002863 if (rc)
Tejun Heo83206a22006-03-24 15:25:31 +09002864 return rc;
Tejun Heo48a8a142006-03-05 17:55:58 +09002865
Tejun Heo23e71c32006-03-06 04:31:57 +09002866 DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n",
2867 dev->xfer_shift, (int)dev->xfer_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868
Tejun Heof15a1da2006-05-15 20:57:56 +09002869 ata_dev_printk(dev, KERN_INFO, "configured for %s\n",
2870 ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)));
Tejun Heo83206a22006-03-24 15:25:31 +09002871 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002872}
2873
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874/**
Alan04351822007-03-06 02:37:52 -08002875 * ata_do_set_mode - Program timings and issue SET FEATURES - XFER
Tejun Heo02607312007-08-06 18:36:23 +09002876 * @link: link on which timings will be programmed
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002877 * @r_failed_dev: out paramter for failed device
Linus Torvalds1da177e2005-04-16 15:20:36 -07002878 *
Alan04351822007-03-06 02:37:52 -08002879 * Standard implementation of the function used to tune and set
2880 * ATA device disk transfer mode (PIO3, UDMA6, etc.). If
2881 * ata_dev_set_mode() fails, pointer to the failing device is
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002882 * returned in @r_failed_dev.
Jeff Garzik780a87f2005-05-30 15:41:05 -04002883 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002885 * PCI/etc. bus probe sem.
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002886 *
2887 * RETURNS:
2888 * 0 on success, negative errno otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 */
Alan04351822007-03-06 02:37:52 -08002890
Tejun Heo02607312007-08-06 18:36:23 +09002891int ata_do_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892{
Tejun Heo02607312007-08-06 18:36:23 +09002893 struct ata_port *ap = link->ap;
Tejun Heoe8e06192006-04-01 01:38:18 +09002894 struct ata_device *dev;
Tejun Heof58229f2007-08-06 18:36:23 +09002895 int rc = 0, used_dma = 0, found = 0;
Tejun Heo3adcebb2006-05-15 20:57:37 +09002896
Tejun Heoa6d5a512006-03-06 04:31:57 +09002897 /* step 1: calculate xfer_mask */
Tejun Heof58229f2007-08-06 18:36:23 +09002898 ata_link_for_each_dev(dev, link) {
Tejun Heoacf356b2006-03-24 14:07:50 +09002899 unsigned int pio_mask, dma_mask;
Tejun Heoa6d5a512006-03-06 04:31:57 +09002900
Tejun Heoe1211e32006-04-01 01:38:18 +09002901 if (!ata_dev_enabled(dev))
Tejun Heoa6d5a512006-03-06 04:31:57 +09002902 continue;
2903
Tejun Heo3373efd2006-05-15 20:57:53 +09002904 ata_dev_xfermask(dev);
Tejun Heoa6d5a512006-03-06 04:31:57 +09002905
Tejun Heoacf356b2006-03-24 14:07:50 +09002906 pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0);
2907 dma_mask = ata_pack_xfermask(0, dev->mwdma_mask, dev->udma_mask);
2908 dev->pio_mode = ata_xfer_mask2mode(pio_mask);
2909 dev->dma_mode = ata_xfer_mask2mode(dma_mask);
Alan Cox5444a6f2006-03-27 18:58:20 +01002910
Tejun Heo4f659772006-04-01 01:38:18 +09002911 found = 1;
Alan Cox5444a6f2006-03-27 18:58:20 +01002912 if (dev->dma_mode)
2913 used_dma = 1;
Tejun Heoa6d5a512006-03-06 04:31:57 +09002914 }
Tejun Heo4f659772006-04-01 01:38:18 +09002915 if (!found)
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002916 goto out;
Tejun Heoa6d5a512006-03-06 04:31:57 +09002917
2918 /* step 2: always set host PIO timings */
Tejun Heof58229f2007-08-06 18:36:23 +09002919 ata_link_for_each_dev(dev, link) {
Tejun Heoe8e06192006-04-01 01:38:18 +09002920 if (!ata_dev_enabled(dev))
2921 continue;
2922
2923 if (!dev->pio_mode) {
Tejun Heof15a1da2006-05-15 20:57:56 +09002924 ata_dev_printk(dev, KERN_WARNING, "no PIO support\n");
Tejun Heoe8e06192006-04-01 01:38:18 +09002925 rc = -EINVAL;
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002926 goto out;
Tejun Heoe8e06192006-04-01 01:38:18 +09002927 }
2928
2929 dev->xfer_mode = dev->pio_mode;
2930 dev->xfer_shift = ATA_SHIFT_PIO;
2931 if (ap->ops->set_piomode)
2932 ap->ops->set_piomode(ap, dev);
2933 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934
Tejun Heoa6d5a512006-03-06 04:31:57 +09002935 /* step 3: set host DMA timings */
Tejun Heof58229f2007-08-06 18:36:23 +09002936 ata_link_for_each_dev(dev, link) {
Tejun Heoe8e06192006-04-01 01:38:18 +09002937 if (!ata_dev_enabled(dev) || !dev->dma_mode)
2938 continue;
2939
2940 dev->xfer_mode = dev->dma_mode;
2941 dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
2942 if (ap->ops->set_dmamode)
2943 ap->ops->set_dmamode(ap, dev);
2944 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945
2946 /* step 4: update devices' xfer mode */
Tejun Heof58229f2007-08-06 18:36:23 +09002947 ata_link_for_each_dev(dev, link) {
Alan18d90de2007-01-24 11:42:38 +00002948 /* don't update suspended devices' xfer mode */
Tejun Heo9666f402007-05-04 21:27:47 +02002949 if (!ata_dev_enabled(dev))
Tejun Heo83206a22006-03-24 15:25:31 +09002950 continue;
2951
Tejun Heo3373efd2006-05-15 20:57:53 +09002952 rc = ata_dev_set_mode(dev);
Tejun Heo5bbc53f2006-04-01 01:38:17 +09002953 if (rc)
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002954 goto out;
Tejun Heo83206a22006-03-24 15:25:31 +09002955 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956
Tejun Heoe8e06192006-04-01 01:38:18 +09002957 /* Record simplex status. If we selected DMA then the other
2958 * host channels are not permitted to do so.
Alan Cox5444a6f2006-03-27 18:58:20 +01002959 */
Jeff Garzikcca39742006-08-24 03:19:22 -04002960 if (used_dma && (ap->host->flags & ATA_HOST_SIMPLEX))
Alan032af1c2007-03-01 17:36:46 +00002961 ap->host->simplex_claimed = ap;
Alan Cox5444a6f2006-03-27 18:58:20 +01002962
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002963 out:
2964 if (rc)
2965 *r_failed_dev = dev;
2966 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967}
2968
2969/**
Alan04351822007-03-06 02:37:52 -08002970 * ata_set_mode - Program timings and issue SET FEATURES - XFER
Tejun Heo02607312007-08-06 18:36:23 +09002971 * @link: link on which timings will be programmed
Alan04351822007-03-06 02:37:52 -08002972 * @r_failed_dev: out paramter for failed device
2973 *
2974 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.). If
2975 * ata_set_mode() fails, pointer to the failing device is
2976 * returned in @r_failed_dev.
2977 *
2978 * LOCKING:
2979 * PCI/etc. bus probe sem.
2980 *
2981 * RETURNS:
2982 * 0 on success, negative errno otherwise
2983 */
Tejun Heo02607312007-08-06 18:36:23 +09002984int ata_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
Alan04351822007-03-06 02:37:52 -08002985{
Tejun Heo02607312007-08-06 18:36:23 +09002986 struct ata_port *ap = link->ap;
2987
Alan04351822007-03-06 02:37:52 -08002988 /* has private set_mode? */
2989 if (ap->ops->set_mode)
Tejun Heo02607312007-08-06 18:36:23 +09002990 return ap->ops->set_mode(link, r_failed_dev);
2991 return ata_do_set_mode(link, r_failed_dev);
Alan04351822007-03-06 02:37:52 -08002992}
2993
2994/**
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002995 * ata_tf_to_host - issue ATA taskfile to host controller
2996 * @ap: port to which command is being issued
2997 * @tf: ATA taskfile register set
2998 *
2999 * Issues ATA taskfile register set to ATA host controller,
3000 * with proper synchronization with interrupt handler and
3001 * other threads.
3002 *
3003 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04003004 * spin_lock_irqsave(host lock)
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05003005 */
3006
3007static inline void ata_tf_to_host(struct ata_port *ap,
3008 const struct ata_taskfile *tf)
3009{
3010 ap->ops->tf_load(ap, tf);
3011 ap->ops->exec_command(ap, tf);
3012}
3013
3014/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 * ata_busy_sleep - sleep until BSY clears, or timeout
3016 * @ap: port containing status register to be polled
3017 * @tmout_pat: impatience timeout
3018 * @tmout: overall timeout
3019 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04003020 * Sleep until ATA Status register bit BSY clears,
3021 * or a timeout occurs.
3022 *
Tejun Heod1adc1b2006-10-09 18:32:15 +09003023 * LOCKING:
3024 * Kernel thread context (may sleep).
3025 *
3026 * RETURNS:
3027 * 0 on success, -errno otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 */
Tejun Heod1adc1b2006-10-09 18:32:15 +09003029int ata_busy_sleep(struct ata_port *ap,
3030 unsigned long tmout_pat, unsigned long tmout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031{
3032 unsigned long timer_start, timeout;
3033 u8 status;
3034
3035 status = ata_busy_wait(ap, ATA_BUSY, 300);
3036 timer_start = jiffies;
3037 timeout = timer_start + tmout_pat;
Tejun Heod1adc1b2006-10-09 18:32:15 +09003038 while (status != 0xff && (status & ATA_BUSY) &&
3039 time_before(jiffies, timeout)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 msleep(50);
3041 status = ata_busy_wait(ap, ATA_BUSY, 3);
3042 }
3043
Tejun Heod1adc1b2006-10-09 18:32:15 +09003044 if (status != 0xff && (status & ATA_BUSY))
Tejun Heof15a1da2006-05-15 20:57:56 +09003045 ata_port_printk(ap, KERN_WARNING,
Jeff Garzik35aa7a42006-09-28 06:50:24 -04003046 "port is slow to respond, please be patient "
3047 "(Status 0x%x)\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048
3049 timeout = timer_start + tmout;
Tejun Heod1adc1b2006-10-09 18:32:15 +09003050 while (status != 0xff && (status & ATA_BUSY) &&
3051 time_before(jiffies, timeout)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 msleep(50);
3053 status = ata_chk_status(ap);
3054 }
3055
Tejun Heod1adc1b2006-10-09 18:32:15 +09003056 if (status == 0xff)
3057 return -ENODEV;
3058
Linus Torvalds1da177e2005-04-16 15:20:36 -07003059 if (status & ATA_BUSY) {
Tejun Heof15a1da2006-05-15 20:57:56 +09003060 ata_port_printk(ap, KERN_ERR, "port failed to respond "
Jeff Garzik35aa7a42006-09-28 06:50:24 -04003061 "(%lu secs, Status 0x%x)\n",
3062 tmout / HZ, status);
Tejun Heod1adc1b2006-10-09 18:32:15 +09003063 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 }
3065
3066 return 0;
3067}
3068
Tejun Heod4b2bab2007-02-02 16:50:52 +09003069/**
3070 * ata_wait_ready - sleep until BSY clears, or timeout
3071 * @ap: port containing status register to be polled
3072 * @deadline: deadline jiffies for the operation
3073 *
3074 * Sleep until ATA Status register bit BSY clears, or timeout
3075 * occurs.
3076 *
3077 * LOCKING:
3078 * Kernel thread context (may sleep).
3079 *
3080 * RETURNS:
3081 * 0 on success, -errno otherwise.
3082 */
3083int ata_wait_ready(struct ata_port *ap, unsigned long deadline)
3084{
3085 unsigned long start = jiffies;
3086 int warned = 0;
3087
3088 while (1) {
3089 u8 status = ata_chk_status(ap);
3090 unsigned long now = jiffies;
3091
3092 if (!(status & ATA_BUSY))
3093 return 0;
Tejun Heo936fd732007-08-06 18:36:23 +09003094 if (!ata_link_online(&ap->link) && status == 0xff)
Tejun Heod4b2bab2007-02-02 16:50:52 +09003095 return -ENODEV;
3096 if (time_after(now, deadline))
3097 return -EBUSY;
3098
3099 if (!warned && time_after(now, start + 5 * HZ) &&
3100 (deadline - now > 3 * HZ)) {
3101 ata_port_printk(ap, KERN_WARNING,
3102 "port is slow to respond, please be patient "
3103 "(Status 0x%x)\n", status);
3104 warned = 1;
3105 }
3106
3107 msleep(50);
3108 }
3109}
3110
3111static int ata_bus_post_reset(struct ata_port *ap, unsigned int devmask,
3112 unsigned long deadline)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113{
3114 struct ata_ioports *ioaddr = &ap->ioaddr;
3115 unsigned int dev0 = devmask & (1 << 0);
3116 unsigned int dev1 = devmask & (1 << 1);
Tejun Heo9b893912007-02-02 16:50:52 +09003117 int rc, ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118
3119 /* if device 0 was found in ata_devchk, wait for its
3120 * BSY bit to clear
3121 */
Tejun Heod4b2bab2007-02-02 16:50:52 +09003122 if (dev0) {
3123 rc = ata_wait_ready(ap, deadline);
Tejun Heo9b893912007-02-02 16:50:52 +09003124 if (rc) {
3125 if (rc != -ENODEV)
3126 return rc;
3127 ret = rc;
3128 }
Tejun Heod4b2bab2007-02-02 16:50:52 +09003129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003130
Tejun Heoe141d992007-06-10 14:26:20 +09003131 /* if device 1 was found in ata_devchk, wait for register
3132 * access briefly, then wait for BSY to clear.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 */
Tejun Heoe141d992007-06-10 14:26:20 +09003134 if (dev1) {
3135 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136
3137 ap->ops->dev_select(ap, 1);
Tejun Heoe141d992007-06-10 14:26:20 +09003138
3139 /* Wait for register access. Some ATAPI devices fail
3140 * to set nsect/lbal after reset, so don't waste too
3141 * much time on it. We're gonna wait for !BSY anyway.
3142 */
3143 for (i = 0; i < 2; i++) {
3144 u8 nsect, lbal;
3145
3146 nsect = ioread8(ioaddr->nsect_addr);
3147 lbal = ioread8(ioaddr->lbal_addr);
3148 if ((nsect == 1) && (lbal == 1))
3149 break;
3150 msleep(50); /* give drive a breather */
3151 }
3152
Tejun Heod4b2bab2007-02-02 16:50:52 +09003153 rc = ata_wait_ready(ap, deadline);
Tejun Heo9b893912007-02-02 16:50:52 +09003154 if (rc) {
3155 if (rc != -ENODEV)
3156 return rc;
3157 ret = rc;
3158 }
Tejun Heod4b2bab2007-02-02 16:50:52 +09003159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160
3161 /* is all this really necessary? */
3162 ap->ops->dev_select(ap, 0);
3163 if (dev1)
3164 ap->ops->dev_select(ap, 1);
3165 if (dev0)
3166 ap->ops->dev_select(ap, 0);
Tejun Heod4b2bab2007-02-02 16:50:52 +09003167
Tejun Heo9b893912007-02-02 16:50:52 +09003168 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169}
3170
Tejun Heod4b2bab2007-02-02 16:50:52 +09003171static int ata_bus_softreset(struct ata_port *ap, unsigned int devmask,
3172 unsigned long deadline)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173{
3174 struct ata_ioports *ioaddr = &ap->ioaddr;
3175
Tejun Heo44877b42007-02-21 01:06:51 +09003176 DPRINTK("ata%u: bus reset via SRST\n", ap->print_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177
3178 /* software reset. causes dev0 to be selected */
Tejun Heo0d5ff562007-02-01 15:06:36 +09003179 iowrite8(ap->ctl, ioaddr->ctl_addr);
3180 udelay(20); /* FIXME: flush */
3181 iowrite8(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
3182 udelay(20); /* FIXME: flush */
3183 iowrite8(ap->ctl, ioaddr->ctl_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184
3185 /* spec mandates ">= 2ms" before checking status.
3186 * We wait 150ms, because that was the magic delay used for
3187 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
3188 * between when the ATA command register is written, and then
3189 * status is checked. Because waiting for "a while" before
3190 * checking status is fine, post SRST, we perform this magic
3191 * delay here as well.
Alan Cox09c7ad72006-03-22 15:52:40 +00003192 *
3193 * Old drivers/ide uses the 2mS rule and then waits for ready
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 */
3195 msleep(150);
3196
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05003197 /* Before we perform post reset processing we want to see if
Tejun Heo298a41c2006-03-25 02:58:13 +09003198 * the bus shows 0xFF because the odd clown forgets the D7
3199 * pulldown resistor.
3200 */
Tejun Heod1adc1b2006-10-09 18:32:15 +09003201 if (ata_check_status(ap) == 0xFF)
Tejun Heo9b893912007-02-02 16:50:52 +09003202 return -ENODEV;
Alan Cox09c7ad72006-03-22 15:52:40 +00003203
Tejun Heod4b2bab2007-02-02 16:50:52 +09003204 return ata_bus_post_reset(ap, devmask, deadline);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205}
3206
3207/**
3208 * ata_bus_reset - reset host port and associated ATA channel
3209 * @ap: port to reset
3210 *
3211 * This is typically the first time we actually start issuing
3212 * commands to the ATA channel. We wait for BSY to clear, then
3213 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
3214 * result. Determine what devices, if any, are on the channel
3215 * by looking at the device 0/1 error register. Look at the signature
3216 * stored in each device's taskfile registers, to determine if
3217 * the device is ATA or ATAPI.
3218 *
3219 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003220 * PCI/etc. bus probe sem.
Jeff Garzikcca39742006-08-24 03:19:22 -04003221 * Obtains host lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 *
3223 * SIDE EFFECTS:
Tejun Heo198e0fe2006-04-02 18:51:52 +09003224 * Sets ATA_FLAG_DISABLED if bus reset fails.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003225 */
3226
3227void ata_bus_reset(struct ata_port *ap)
3228{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09003229 struct ata_device *device = ap->link.device;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 struct ata_ioports *ioaddr = &ap->ioaddr;
3231 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
3232 u8 err;
Tejun Heoaec5c3c2006-03-25 01:33:34 +09003233 unsigned int dev0, dev1 = 0, devmask = 0;
Tejun Heo9b893912007-02-02 16:50:52 +09003234 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235
Tejun Heo44877b42007-02-21 01:06:51 +09003236 DPRINTK("ENTER, host %u, port %u\n", ap->print_id, ap->port_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237
3238 /* determine if device 0/1 are present */
3239 if (ap->flags & ATA_FLAG_SATA_RESET)
3240 dev0 = 1;
3241 else {
3242 dev0 = ata_devchk(ap, 0);
3243 if (slave_possible)
3244 dev1 = ata_devchk(ap, 1);
3245 }
3246
3247 if (dev0)
3248 devmask |= (1 << 0);
3249 if (dev1)
3250 devmask |= (1 << 1);
3251
3252 /* select device 0 again */
3253 ap->ops->dev_select(ap, 0);
3254
3255 /* issue bus reset */
Tejun Heo9b893912007-02-02 16:50:52 +09003256 if (ap->flags & ATA_FLAG_SRST) {
3257 rc = ata_bus_softreset(ap, devmask, jiffies + 40 * HZ);
3258 if (rc && rc != -ENODEV)
Tejun Heoaec5c3c2006-03-25 01:33:34 +09003259 goto err_out;
Tejun Heo9b893912007-02-02 16:50:52 +09003260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261
3262 /*
3263 * determine by signature whether we have ATA or ATAPI devices
3264 */
Tejun Heo3f198592007-09-02 23:23:57 +09003265 device[0].class = ata_dev_try_classify(&device[0], dev0, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 if ((slave_possible) && (err != 0x81))
Tejun Heo3f198592007-09-02 23:23:57 +09003267 device[1].class = ata_dev_try_classify(&device[1], dev1, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 /* is double-select really necessary? */
Tejun Heo9af5c9c2007-08-06 18:36:22 +09003270 if (device[1].class != ATA_DEV_NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 ap->ops->dev_select(ap, 1);
Tejun Heo9af5c9c2007-08-06 18:36:22 +09003272 if (device[0].class != ATA_DEV_NONE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273 ap->ops->dev_select(ap, 0);
3274
3275 /* if no devices were detected, disable this port */
Tejun Heo9af5c9c2007-08-06 18:36:22 +09003276 if ((device[0].class == ATA_DEV_NONE) &&
3277 (device[1].class == ATA_DEV_NONE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278 goto err_out;
3279
3280 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
3281 /* set up device control for ATA_FLAG_SATA_RESET */
Tejun Heo0d5ff562007-02-01 15:06:36 +09003282 iowrite8(ap->ctl, ioaddr->ctl_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003283 }
3284
3285 DPRINTK("EXIT\n");
3286 return;
3287
3288err_out:
Tejun Heof15a1da2006-05-15 20:57:56 +09003289 ata_port_printk(ap, KERN_ERR, "disabling port\n");
Jeff Garzikac8869d2007-08-16 03:17:03 -04003290 ata_port_disable(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003291
3292 DPRINTK("EXIT\n");
3293}
3294
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003295/**
Tejun Heo936fd732007-08-06 18:36:23 +09003296 * sata_link_debounce - debounce SATA phy status
3297 * @link: ATA link to debounce SATA phy status for
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003298 * @params: timing parameters { interval, duratinon, timeout } in msec
Tejun Heod4b2bab2007-02-02 16:50:52 +09003299 * @deadline: deadline jiffies for the operation
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003300 *
Tejun Heo936fd732007-08-06 18:36:23 +09003301* Make sure SStatus of @link reaches stable state, determined by
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003302 * holding the same value where DET is not 1 for @duration polled
3303 * every @interval, before @timeout. Timeout constraints the
Tejun Heod4b2bab2007-02-02 16:50:52 +09003304 * beginning of the stable state. Because DET gets stuck at 1 on
3305 * some controllers after hot unplugging, this functions waits
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003306 * until timeout then returns 0 if DET is stable at 1.
3307 *
Tejun Heod4b2bab2007-02-02 16:50:52 +09003308 * @timeout is further limited by @deadline. The sooner of the
3309 * two is used.
3310 *
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003311 * LOCKING:
3312 * Kernel thread context (may sleep)
3313 *
3314 * RETURNS:
3315 * 0 on success, -errno on failure.
3316 */
Tejun Heo936fd732007-08-06 18:36:23 +09003317int sata_link_debounce(struct ata_link *link, const unsigned long *params,
3318 unsigned long deadline)
Tejun Heo7a7921e2006-02-02 18:20:00 +09003319{
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003320 unsigned long interval_msec = params[0];
Tejun Heod4b2bab2007-02-02 16:50:52 +09003321 unsigned long duration = msecs_to_jiffies(params[1]);
3322 unsigned long last_jiffies, t;
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003323 u32 last, cur;
3324 int rc;
3325
Tejun Heod4b2bab2007-02-02 16:50:52 +09003326 t = jiffies + msecs_to_jiffies(params[2]);
3327 if (time_before(t, deadline))
3328 deadline = t;
3329
Tejun Heo936fd732007-08-06 18:36:23 +09003330 if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003331 return rc;
3332 cur &= 0xf;
3333
3334 last = cur;
3335 last_jiffies = jiffies;
3336
3337 while (1) {
3338 msleep(interval_msec);
Tejun Heo936fd732007-08-06 18:36:23 +09003339 if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003340 return rc;
3341 cur &= 0xf;
3342
3343 /* DET stable? */
3344 if (cur == last) {
Tejun Heod4b2bab2007-02-02 16:50:52 +09003345 if (cur == 1 && time_before(jiffies, deadline))
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003346 continue;
3347 if (time_after(jiffies, last_jiffies + duration))
3348 return 0;
3349 continue;
3350 }
3351
3352 /* unstable, start over */
3353 last = cur;
3354 last_jiffies = jiffies;
3355
Tejun Heof1545152007-07-16 14:29:40 +09003356 /* Check deadline. If debouncing failed, return
3357 * -EPIPE to tell upper layer to lower link speed.
3358 */
Tejun Heod4b2bab2007-02-02 16:50:52 +09003359 if (time_after(jiffies, deadline))
Tejun Heof1545152007-07-16 14:29:40 +09003360 return -EPIPE;
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003361 }
3362}
3363
3364/**
Tejun Heo936fd732007-08-06 18:36:23 +09003365 * sata_link_resume - resume SATA link
3366 * @link: ATA link to resume SATA
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003367 * @params: timing parameters { interval, duratinon, timeout } in msec
Tejun Heod4b2bab2007-02-02 16:50:52 +09003368 * @deadline: deadline jiffies for the operation
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003369 *
Tejun Heo936fd732007-08-06 18:36:23 +09003370 * Resume SATA phy @link and debounce it.
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003371 *
3372 * LOCKING:
3373 * Kernel thread context (may sleep)
3374 *
3375 * RETURNS:
3376 * 0 on success, -errno on failure.
3377 */
Tejun Heo936fd732007-08-06 18:36:23 +09003378int sata_link_resume(struct ata_link *link, const unsigned long *params,
3379 unsigned long deadline)
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003380{
3381 u32 scontrol;
Tejun Heo81952c52006-05-15 20:57:47 +09003382 int rc;
Tejun Heo7a7921e2006-02-02 18:20:00 +09003383
Tejun Heo936fd732007-08-06 18:36:23 +09003384 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
Tejun Heo81952c52006-05-15 20:57:47 +09003385 return rc;
3386
Tejun Heo852ee162006-04-01 01:38:18 +09003387 scontrol = (scontrol & 0x0f0) | 0x300;
Tejun Heo81952c52006-05-15 20:57:47 +09003388
Tejun Heo936fd732007-08-06 18:36:23 +09003389 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
Tejun Heo81952c52006-05-15 20:57:47 +09003390 return rc;
Tejun Heo7a7921e2006-02-02 18:20:00 +09003391
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003392 /* Some PHYs react badly if SStatus is pounded immediately
3393 * after resuming. Delay 200ms before debouncing.
3394 */
3395 msleep(200);
Tejun Heo7a7921e2006-02-02 18:20:00 +09003396
Tejun Heo936fd732007-08-06 18:36:23 +09003397 return sata_link_debounce(link, params, deadline);
Tejun Heo7a7921e2006-02-02 18:20:00 +09003398}
3399
Tejun Heof5914a42006-05-31 18:27:48 +09003400/**
3401 * ata_std_prereset - prepare for reset
Tejun Heocc0680a2007-08-06 18:36:23 +09003402 * @link: ATA link to be reset
Tejun Heod4b2bab2007-02-02 16:50:52 +09003403 * @deadline: deadline jiffies for the operation
Tejun Heof5914a42006-05-31 18:27:48 +09003404 *
Tejun Heocc0680a2007-08-06 18:36:23 +09003405 * @link is about to be reset. Initialize it. Failure from
Tejun Heob8cffc62007-02-02 16:50:52 +09003406 * prereset makes libata abort whole reset sequence and give up
3407 * that port, so prereset should be best-effort. It does its
3408 * best to prepare for reset sequence but if things go wrong, it
3409 * should just whine, not fail.
Tejun Heof5914a42006-05-31 18:27:48 +09003410 *
3411 * LOCKING:
3412 * Kernel thread context (may sleep)
3413 *
3414 * RETURNS:
3415 * 0 on success, -errno otherwise.
3416 */
Tejun Heocc0680a2007-08-06 18:36:23 +09003417int ata_std_prereset(struct ata_link *link, unsigned long deadline)
Tejun Heof5914a42006-05-31 18:27:48 +09003418{
Tejun Heocc0680a2007-08-06 18:36:23 +09003419 struct ata_port *ap = link->ap;
Tejun Heo936fd732007-08-06 18:36:23 +09003420 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heoe9c83912006-07-03 16:07:26 +09003421 const unsigned long *timing = sata_ehc_deb_timing(ehc);
Tejun Heof5914a42006-05-31 18:27:48 +09003422 int rc;
3423
Tejun Heo31daabd2007-02-02 16:50:52 +09003424 /* handle link resume */
Tejun Heo28324302006-07-03 16:07:26 +09003425 if ((ehc->i.flags & ATA_EHI_RESUME_LINK) &&
Tejun Heo0c887582007-08-06 18:36:23 +09003426 (link->flags & ATA_LFLAG_HRST_TO_RESUME))
Tejun Heo28324302006-07-03 16:07:26 +09003427 ehc->i.action |= ATA_EH_HARDRESET;
3428
Tejun Heof5914a42006-05-31 18:27:48 +09003429 /* if we're about to do hardreset, nothing more to do */
3430 if (ehc->i.action & ATA_EH_HARDRESET)
3431 return 0;
3432
Tejun Heo936fd732007-08-06 18:36:23 +09003433 /* if SATA, resume link */
Tejun Heoa16abc02007-05-21 18:33:47 +02003434 if (ap->flags & ATA_FLAG_SATA) {
Tejun Heo936fd732007-08-06 18:36:23 +09003435 rc = sata_link_resume(link, timing, deadline);
Tejun Heob8cffc62007-02-02 16:50:52 +09003436 /* whine about phy resume failure but proceed */
3437 if (rc && rc != -EOPNOTSUPP)
Tejun Heocc0680a2007-08-06 18:36:23 +09003438 ata_link_printk(link, KERN_WARNING, "failed to resume "
Tejun Heof5914a42006-05-31 18:27:48 +09003439 "link for reset (errno=%d)\n", rc);
Tejun Heof5914a42006-05-31 18:27:48 +09003440 }
3441
3442 /* Wait for !BSY if the controller can wait for the first D2H
3443 * Reg FIS and we don't know that no device is attached.
3444 */
Tejun Heo0c887582007-08-06 18:36:23 +09003445 if (!(link->flags & ATA_LFLAG_SKIP_D2H_BSY) && !ata_link_offline(link)) {
Tejun Heob8cffc62007-02-02 16:50:52 +09003446 rc = ata_wait_ready(ap, deadline);
Tejun Heo6dffaf62007-05-23 11:58:52 +02003447 if (rc && rc != -ENODEV) {
Tejun Heocc0680a2007-08-06 18:36:23 +09003448 ata_link_printk(link, KERN_WARNING, "device not ready "
Tejun Heob8cffc62007-02-02 16:50:52 +09003449 "(errno=%d), forcing hardreset\n", rc);
3450 ehc->i.action |= ATA_EH_HARDRESET;
3451 }
3452 }
Tejun Heof5914a42006-05-31 18:27:48 +09003453
3454 return 0;
3455}
3456
Tejun Heoc2bd5802006-01-24 17:05:22 +09003457/**
3458 * ata_std_softreset - reset host port via ATA SRST
Tejun Heocc0680a2007-08-06 18:36:23 +09003459 * @link: ATA link to reset
Tejun Heoc2bd5802006-01-24 17:05:22 +09003460 * @classes: resulting classes of attached devices
Tejun Heod4b2bab2007-02-02 16:50:52 +09003461 * @deadline: deadline jiffies for the operation
Tejun Heoc2bd5802006-01-24 17:05:22 +09003462 *
Tejun Heo52783c52006-05-31 18:28:22 +09003463 * Reset host port using ATA SRST.
Tejun Heoc2bd5802006-01-24 17:05:22 +09003464 *
3465 * LOCKING:
3466 * Kernel thread context (may sleep)
3467 *
3468 * RETURNS:
3469 * 0 on success, -errno otherwise.
3470 */
Tejun Heocc0680a2007-08-06 18:36:23 +09003471int ata_std_softreset(struct ata_link *link, unsigned int *classes,
Tejun Heod4b2bab2007-02-02 16:50:52 +09003472 unsigned long deadline)
Tejun Heoc2bd5802006-01-24 17:05:22 +09003473{
Tejun Heocc0680a2007-08-06 18:36:23 +09003474 struct ata_port *ap = link->ap;
Tejun Heoc2bd5802006-01-24 17:05:22 +09003475 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
Tejun Heod4b2bab2007-02-02 16:50:52 +09003476 unsigned int devmask = 0;
3477 int rc;
Tejun Heoc2bd5802006-01-24 17:05:22 +09003478 u8 err;
3479
3480 DPRINTK("ENTER\n");
3481
Tejun Heo936fd732007-08-06 18:36:23 +09003482 if (ata_link_offline(link)) {
Tejun Heo3a397462006-02-10 23:58:48 +09003483 classes[0] = ATA_DEV_NONE;
3484 goto out;
3485 }
3486
Tejun Heoc2bd5802006-01-24 17:05:22 +09003487 /* determine if device 0/1 are present */
3488 if (ata_devchk(ap, 0))
3489 devmask |= (1 << 0);
3490 if (slave_possible && ata_devchk(ap, 1))
3491 devmask |= (1 << 1);
3492
Tejun Heoc2bd5802006-01-24 17:05:22 +09003493 /* select device 0 again */
3494 ap->ops->dev_select(ap, 0);
3495
3496 /* issue bus reset */
3497 DPRINTK("about to softreset, devmask=%x\n", devmask);
Tejun Heod4b2bab2007-02-02 16:50:52 +09003498 rc = ata_bus_softreset(ap, devmask, deadline);
Tejun Heo9b893912007-02-02 16:50:52 +09003499 /* if link is occupied, -ENODEV too is an error */
Tejun Heo936fd732007-08-06 18:36:23 +09003500 if (rc && (rc != -ENODEV || sata_scr_valid(link))) {
Tejun Heocc0680a2007-08-06 18:36:23 +09003501 ata_link_printk(link, KERN_ERR, "SRST failed (errno=%d)\n", rc);
Tejun Heod4b2bab2007-02-02 16:50:52 +09003502 return rc;
Tejun Heoc2bd5802006-01-24 17:05:22 +09003503 }
3504
3505 /* determine by signature whether we have ATA or ATAPI devices */
Tejun Heo3f198592007-09-02 23:23:57 +09003506 classes[0] = ata_dev_try_classify(&link->device[0],
3507 devmask & (1 << 0), &err);
Tejun Heoc2bd5802006-01-24 17:05:22 +09003508 if (slave_possible && err != 0x81)
Tejun Heo3f198592007-09-02 23:23:57 +09003509 classes[1] = ata_dev_try_classify(&link->device[1],
3510 devmask & (1 << 1), &err);
Tejun Heoc2bd5802006-01-24 17:05:22 +09003511
Tejun Heo3a397462006-02-10 23:58:48 +09003512 out:
Tejun Heoc2bd5802006-01-24 17:05:22 +09003513 DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]);
3514 return 0;
3515}
3516
3517/**
Tejun Heocc0680a2007-08-06 18:36:23 +09003518 * sata_link_hardreset - reset link via SATA phy reset
3519 * @link: link to reset
Tejun Heob6103f62006-11-01 17:59:53 +09003520 * @timing: timing parameters { interval, duratinon, timeout } in msec
Tejun Heod4b2bab2007-02-02 16:50:52 +09003521 * @deadline: deadline jiffies for the operation
Tejun Heoc2bd5802006-01-24 17:05:22 +09003522 *
Tejun Heocc0680a2007-08-06 18:36:23 +09003523 * SATA phy-reset @link using DET bits of SControl register.
Tejun Heoc2bd5802006-01-24 17:05:22 +09003524 *
3525 * LOCKING:
3526 * Kernel thread context (may sleep)
3527 *
3528 * RETURNS:
3529 * 0 on success, -errno otherwise.
3530 */
Tejun Heocc0680a2007-08-06 18:36:23 +09003531int sata_link_hardreset(struct ata_link *link, const unsigned long *timing,
Tejun Heod4b2bab2007-02-02 16:50:52 +09003532 unsigned long deadline)
Tejun Heoc2bd5802006-01-24 17:05:22 +09003533{
Tejun Heo852ee162006-04-01 01:38:18 +09003534 u32 scontrol;
Tejun Heo81952c52006-05-15 20:57:47 +09003535 int rc;
Tejun Heo852ee162006-04-01 01:38:18 +09003536
Tejun Heoc2bd5802006-01-24 17:05:22 +09003537 DPRINTK("ENTER\n");
3538
Tejun Heo936fd732007-08-06 18:36:23 +09003539 if (sata_set_spd_needed(link)) {
Tejun Heo1c3fae42006-04-02 20:53:28 +09003540 /* SATA spec says nothing about how to reconfigure
3541 * spd. To be on the safe side, turn off phy during
3542 * reconfiguration. This works for at least ICH7 AHCI
3543 * and Sil3124.
3544 */
Tejun Heo936fd732007-08-06 18:36:23 +09003545 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
Tejun Heob6103f62006-11-01 17:59:53 +09003546 goto out;
Tejun Heo81952c52006-05-15 20:57:47 +09003547
Martin Hicksa34b6fc2006-07-05 15:06:13 -04003548 scontrol = (scontrol & 0x0f0) | 0x304;
Tejun Heo81952c52006-05-15 20:57:47 +09003549
Tejun Heo936fd732007-08-06 18:36:23 +09003550 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
Tejun Heob6103f62006-11-01 17:59:53 +09003551 goto out;
Tejun Heo1c3fae42006-04-02 20:53:28 +09003552
Tejun Heo936fd732007-08-06 18:36:23 +09003553 sata_set_spd(link);
Tejun Heo1c3fae42006-04-02 20:53:28 +09003554 }
3555
3556 /* issue phy wake/reset */
Tejun Heo936fd732007-08-06 18:36:23 +09003557 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
Tejun Heob6103f62006-11-01 17:59:53 +09003558 goto out;
Tejun Heo81952c52006-05-15 20:57:47 +09003559
Tejun Heo852ee162006-04-01 01:38:18 +09003560 scontrol = (scontrol & 0x0f0) | 0x301;
Tejun Heo81952c52006-05-15 20:57:47 +09003561
Tejun Heo936fd732007-08-06 18:36:23 +09003562 if ((rc = sata_scr_write_flush(link, SCR_CONTROL, scontrol)))
Tejun Heob6103f62006-11-01 17:59:53 +09003563 goto out;
Tejun Heoc2bd5802006-01-24 17:05:22 +09003564
Tejun Heo1c3fae42006-04-02 20:53:28 +09003565 /* Couldn't find anything in SATA I/II specs, but AHCI-1.1
Tejun Heoc2bd5802006-01-24 17:05:22 +09003566 * 10.4.2 says at least 1 ms.
3567 */
3568 msleep(1);
3569
Tejun Heo936fd732007-08-06 18:36:23 +09003570 /* bring link back */
3571 rc = sata_link_resume(link, timing, deadline);
Tejun Heob6103f62006-11-01 17:59:53 +09003572 out:
3573 DPRINTK("EXIT, rc=%d\n", rc);
3574 return rc;
3575}
3576
3577/**
3578 * sata_std_hardreset - reset host port via SATA phy reset
Tejun Heocc0680a2007-08-06 18:36:23 +09003579 * @link: link to reset
Tejun Heob6103f62006-11-01 17:59:53 +09003580 * @class: resulting class of attached device
Tejun Heod4b2bab2007-02-02 16:50:52 +09003581 * @deadline: deadline jiffies for the operation
Tejun Heob6103f62006-11-01 17:59:53 +09003582 *
3583 * SATA phy-reset host port using DET bits of SControl register,
3584 * wait for !BSY and classify the attached device.
3585 *
3586 * LOCKING:
3587 * Kernel thread context (may sleep)
3588 *
3589 * RETURNS:
3590 * 0 on success, -errno otherwise.
3591 */
Tejun Heocc0680a2007-08-06 18:36:23 +09003592int sata_std_hardreset(struct ata_link *link, unsigned int *class,
Tejun Heod4b2bab2007-02-02 16:50:52 +09003593 unsigned long deadline)
Tejun Heob6103f62006-11-01 17:59:53 +09003594{
Tejun Heocc0680a2007-08-06 18:36:23 +09003595 struct ata_port *ap = link->ap;
Tejun Heo936fd732007-08-06 18:36:23 +09003596 const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
Tejun Heob6103f62006-11-01 17:59:53 +09003597 int rc;
3598
3599 DPRINTK("ENTER\n");
3600
3601 /* do hardreset */
Tejun Heocc0680a2007-08-06 18:36:23 +09003602 rc = sata_link_hardreset(link, timing, deadline);
Tejun Heob6103f62006-11-01 17:59:53 +09003603 if (rc) {
Tejun Heocc0680a2007-08-06 18:36:23 +09003604 ata_link_printk(link, KERN_ERR,
Tejun Heob6103f62006-11-01 17:59:53 +09003605 "COMRESET failed (errno=%d)\n", rc);
3606 return rc;
3607 }
Tejun Heoc2bd5802006-01-24 17:05:22 +09003608
Tejun Heoc2bd5802006-01-24 17:05:22 +09003609 /* TODO: phy layer with polling, timeouts, etc. */
Tejun Heo936fd732007-08-06 18:36:23 +09003610 if (ata_link_offline(link)) {
Tejun Heoc2bd5802006-01-24 17:05:22 +09003611 *class = ATA_DEV_NONE;
3612 DPRINTK("EXIT, link offline\n");
3613 return 0;
3614 }
3615
Tejun Heo34fee222007-02-02 15:29:27 +09003616 /* wait a while before checking status, see SRST for more info */
3617 msleep(150);
3618
Tejun Heod4b2bab2007-02-02 16:50:52 +09003619 rc = ata_wait_ready(ap, deadline);
Tejun Heo9b893912007-02-02 16:50:52 +09003620 /* link occupied, -ENODEV too is an error */
3621 if (rc) {
Tejun Heocc0680a2007-08-06 18:36:23 +09003622 ata_link_printk(link, KERN_ERR,
Tejun Heod4b2bab2007-02-02 16:50:52 +09003623 "COMRESET failed (errno=%d)\n", rc);
3624 return rc;
Tejun Heoc2bd5802006-01-24 17:05:22 +09003625 }
3626
Tejun Heo3a397462006-02-10 23:58:48 +09003627 ap->ops->dev_select(ap, 0); /* probably unnecessary */
3628
Tejun Heo3f198592007-09-02 23:23:57 +09003629 *class = ata_dev_try_classify(link->device, 1, NULL);
Tejun Heoc2bd5802006-01-24 17:05:22 +09003630
3631 DPRINTK("EXIT, class=%u\n", *class);
3632 return 0;
3633}
3634
3635/**
3636 * ata_std_postreset - standard postreset callback
Tejun Heocc0680a2007-08-06 18:36:23 +09003637 * @link: the target ata_link
Tejun Heoc2bd5802006-01-24 17:05:22 +09003638 * @classes: classes of attached devices
3639 *
3640 * This function is invoked after a successful reset. Note that
3641 * the device might have been reset more than once using
3642 * different reset methods before postreset is invoked.
Tejun Heoc2bd5802006-01-24 17:05:22 +09003643 *
Tejun Heoc2bd5802006-01-24 17:05:22 +09003644 * LOCKING:
3645 * Kernel thread context (may sleep)
3646 */
Tejun Heocc0680a2007-08-06 18:36:23 +09003647void ata_std_postreset(struct ata_link *link, unsigned int *classes)
Tejun Heoc2bd5802006-01-24 17:05:22 +09003648{
Tejun Heocc0680a2007-08-06 18:36:23 +09003649 struct ata_port *ap = link->ap;
Tejun Heodc2b3512006-05-15 20:58:00 +09003650 u32 serror;
3651
Tejun Heoc2bd5802006-01-24 17:05:22 +09003652 DPRINTK("ENTER\n");
3653
Tejun Heoc2bd5802006-01-24 17:05:22 +09003654 /* print link status */
Tejun Heo936fd732007-08-06 18:36:23 +09003655 sata_print_link_status(link);
Tejun Heoc2bd5802006-01-24 17:05:22 +09003656
Tejun Heodc2b3512006-05-15 20:58:00 +09003657 /* clear SError */
Tejun Heo936fd732007-08-06 18:36:23 +09003658 if (sata_scr_read(link, SCR_ERROR, &serror) == 0)
3659 sata_scr_write(link, SCR_ERROR, serror);
Tejun Heodc2b3512006-05-15 20:58:00 +09003660
Tejun Heoc2bd5802006-01-24 17:05:22 +09003661 /* is double-select really necessary? */
3662 if (classes[0] != ATA_DEV_NONE)
3663 ap->ops->dev_select(ap, 1);
3664 if (classes[1] != ATA_DEV_NONE)
3665 ap->ops->dev_select(ap, 0);
3666
Tejun Heo3a397462006-02-10 23:58:48 +09003667 /* bail out if no device is present */
3668 if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
3669 DPRINTK("EXIT, no device\n");
3670 return;
3671 }
3672
3673 /* set up device control */
Tejun Heo0d5ff562007-02-01 15:06:36 +09003674 if (ap->ioaddr.ctl_addr)
3675 iowrite8(ap->ctl, ap->ioaddr.ctl_addr);
Tejun Heoc2bd5802006-01-24 17:05:22 +09003676
3677 DPRINTK("EXIT\n");
3678}
3679
Tejun Heoa62c0fc2006-01-24 17:05:22 +09003680/**
Tejun Heo623a3122006-03-05 17:55:58 +09003681 * ata_dev_same_device - Determine whether new ID matches configured device
Tejun Heo623a3122006-03-05 17:55:58 +09003682 * @dev: device to compare against
3683 * @new_class: class of the new device
3684 * @new_id: IDENTIFY page of the new device
3685 *
3686 * Compare @new_class and @new_id against @dev and determine
3687 * whether @dev is the device indicated by @new_class and
3688 * @new_id.
3689 *
3690 * LOCKING:
3691 * None.
3692 *
3693 * RETURNS:
3694 * 1 if @dev matches @new_class and @new_id, 0 otherwise.
3695 */
Tejun Heo3373efd2006-05-15 20:57:53 +09003696static int ata_dev_same_device(struct ata_device *dev, unsigned int new_class,
3697 const u16 *new_id)
Tejun Heo623a3122006-03-05 17:55:58 +09003698{
3699 const u16 *old_id = dev->id;
Tejun Heoa0cf7332007-01-02 20:18:49 +09003700 unsigned char model[2][ATA_ID_PROD_LEN + 1];
3701 unsigned char serial[2][ATA_ID_SERNO_LEN + 1];
Tejun Heo623a3122006-03-05 17:55:58 +09003702
3703 if (dev->class != new_class) {
Tejun Heof15a1da2006-05-15 20:57:56 +09003704 ata_dev_printk(dev, KERN_INFO, "class mismatch %d != %d\n",
3705 dev->class, new_class);
Tejun Heo623a3122006-03-05 17:55:58 +09003706 return 0;
3707 }
3708
Tejun Heoa0cf7332007-01-02 20:18:49 +09003709 ata_id_c_string(old_id, model[0], ATA_ID_PROD, sizeof(model[0]));
3710 ata_id_c_string(new_id, model[1], ATA_ID_PROD, sizeof(model[1]));
3711 ata_id_c_string(old_id, serial[0], ATA_ID_SERNO, sizeof(serial[0]));
3712 ata_id_c_string(new_id, serial[1], ATA_ID_SERNO, sizeof(serial[1]));
Tejun Heo623a3122006-03-05 17:55:58 +09003713
3714 if (strcmp(model[0], model[1])) {
Tejun Heof15a1da2006-05-15 20:57:56 +09003715 ata_dev_printk(dev, KERN_INFO, "model number mismatch "
3716 "'%s' != '%s'\n", model[0], model[1]);
Tejun Heo623a3122006-03-05 17:55:58 +09003717 return 0;
3718 }
3719
3720 if (strcmp(serial[0], serial[1])) {
Tejun Heof15a1da2006-05-15 20:57:56 +09003721 ata_dev_printk(dev, KERN_INFO, "serial number mismatch "
3722 "'%s' != '%s'\n", serial[0], serial[1]);
Tejun Heo623a3122006-03-05 17:55:58 +09003723 return 0;
3724 }
3725
Tejun Heo623a3122006-03-05 17:55:58 +09003726 return 1;
3727}
3728
3729/**
Tejun Heofe309112007-05-15 03:28:15 +09003730 * ata_dev_reread_id - Re-read IDENTIFY data
Henrik Kretzschmar3fae4502007-06-19 10:10:50 +02003731 * @dev: target ATA device
Tejun Heobff04642006-11-10 18:08:10 +09003732 * @readid_flags: read ID flags
Tejun Heo623a3122006-03-05 17:55:58 +09003733 *
3734 * Re-read IDENTIFY page and make sure @dev is still attached to
3735 * the port.
3736 *
3737 * LOCKING:
3738 * Kernel thread context (may sleep)
3739 *
3740 * RETURNS:
3741 * 0 on success, negative errno otherwise
3742 */
Tejun Heofe309112007-05-15 03:28:15 +09003743int ata_dev_reread_id(struct ata_device *dev, unsigned int readid_flags)
Tejun Heo623a3122006-03-05 17:55:58 +09003744{
Tejun Heo5eb45c02006-04-02 18:51:52 +09003745 unsigned int class = dev->class;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09003746 u16 *id = (void *)dev->link->ap->sector_buf;
Tejun Heo623a3122006-03-05 17:55:58 +09003747 int rc;
3748
Tejun Heofe635c72006-05-15 20:57:35 +09003749 /* read ID data */
Tejun Heobff04642006-11-10 18:08:10 +09003750 rc = ata_dev_read_id(dev, &class, readid_flags, id);
Tejun Heo623a3122006-03-05 17:55:58 +09003751 if (rc)
Tejun Heofe309112007-05-15 03:28:15 +09003752 return rc;
Tejun Heo623a3122006-03-05 17:55:58 +09003753
3754 /* is the device still there? */
Tejun Heofe309112007-05-15 03:28:15 +09003755 if (!ata_dev_same_device(dev, class, id))
3756 return -ENODEV;
Tejun Heo623a3122006-03-05 17:55:58 +09003757
Tejun Heofe635c72006-05-15 20:57:35 +09003758 memcpy(dev->id, id, sizeof(id[0]) * ATA_ID_WORDS);
Tejun Heofe309112007-05-15 03:28:15 +09003759 return 0;
3760}
3761
3762/**
3763 * ata_dev_revalidate - Revalidate ATA device
3764 * @dev: device to revalidate
Tejun Heo422c9da2007-09-23 13:14:12 +09003765 * @new_class: new class code
Tejun Heofe309112007-05-15 03:28:15 +09003766 * @readid_flags: read ID flags
3767 *
3768 * Re-read IDENTIFY page, make sure @dev is still attached to the
3769 * port and reconfigure it according to the new IDENTIFY page.
3770 *
3771 * LOCKING:
3772 * Kernel thread context (may sleep)
3773 *
3774 * RETURNS:
3775 * 0 on success, negative errno otherwise
3776 */
Tejun Heo422c9da2007-09-23 13:14:12 +09003777int ata_dev_revalidate(struct ata_device *dev, unsigned int new_class,
3778 unsigned int readid_flags)
Tejun Heofe309112007-05-15 03:28:15 +09003779{
Tejun Heo6ddcd3b2007-05-15 03:28:15 +09003780 u64 n_sectors = dev->n_sectors;
Tejun Heofe309112007-05-15 03:28:15 +09003781 int rc;
3782
3783 if (!ata_dev_enabled(dev))
3784 return -ENODEV;
3785
Tejun Heo422c9da2007-09-23 13:14:12 +09003786 /* fail early if !ATA && !ATAPI to avoid issuing [P]IDENTIFY to PMP */
3787 if (ata_class_enabled(new_class) &&
3788 new_class != ATA_DEV_ATA && new_class != ATA_DEV_ATAPI) {
3789 ata_dev_printk(dev, KERN_INFO, "class mismatch %u != %u\n",
3790 dev->class, new_class);
3791 rc = -ENODEV;
3792 goto fail;
3793 }
3794
Tejun Heofe309112007-05-15 03:28:15 +09003795 /* re-read ID */
3796 rc = ata_dev_reread_id(dev, readid_flags);
3797 if (rc)
3798 goto fail;
Tejun Heo623a3122006-03-05 17:55:58 +09003799
3800 /* configure device according to the new ID */
Tejun Heoefdaedc2006-11-01 18:38:52 +09003801 rc = ata_dev_configure(dev);
Tejun Heo6ddcd3b2007-05-15 03:28:15 +09003802 if (rc)
3803 goto fail;
3804
3805 /* verify n_sectors hasn't changed */
Tejun Heob54eebd2007-08-17 18:46:51 +09003806 if (dev->class == ATA_DEV_ATA && n_sectors &&
3807 dev->n_sectors != n_sectors) {
Tejun Heo6ddcd3b2007-05-15 03:28:15 +09003808 ata_dev_printk(dev, KERN_INFO, "n_sectors mismatch "
3809 "%llu != %llu\n",
3810 (unsigned long long)n_sectors,
3811 (unsigned long long)dev->n_sectors);
Tejun Heo8270bec2007-08-16 03:02:22 +09003812
3813 /* restore original n_sectors */
3814 dev->n_sectors = n_sectors;
3815
Tejun Heo6ddcd3b2007-05-15 03:28:15 +09003816 rc = -ENODEV;
3817 goto fail;
3818 }
3819
3820 return 0;
Tejun Heo623a3122006-03-05 17:55:58 +09003821
3822 fail:
Tejun Heof15a1da2006-05-15 20:57:56 +09003823 ata_dev_printk(dev, KERN_ERR, "revalidation failed (errno=%d)\n", rc);
Tejun Heo623a3122006-03-05 17:55:58 +09003824 return rc;
3825}
3826
Alan Cox6919a0a2006-10-27 19:08:46 -07003827struct ata_blacklist_entry {
3828 const char *model_num;
3829 const char *model_rev;
3830 unsigned long horkage;
3831};
3832
3833static const struct ata_blacklist_entry ata_device_blacklist [] = {
3834 /* Devices with DMA related problems under Linux */
3835 { "WDC AC11000H", NULL, ATA_HORKAGE_NODMA },
3836 { "WDC AC22100H", NULL, ATA_HORKAGE_NODMA },
3837 { "WDC AC32500H", NULL, ATA_HORKAGE_NODMA },
3838 { "WDC AC33100H", NULL, ATA_HORKAGE_NODMA },
3839 { "WDC AC31600H", NULL, ATA_HORKAGE_NODMA },
3840 { "WDC AC32100H", "24.09P07", ATA_HORKAGE_NODMA },
3841 { "WDC AC23200L", "21.10N21", ATA_HORKAGE_NODMA },
3842 { "Compaq CRD-8241B", NULL, ATA_HORKAGE_NODMA },
3843 { "CRD-8400B", NULL, ATA_HORKAGE_NODMA },
3844 { "CRD-8480B", NULL, ATA_HORKAGE_NODMA },
3845 { "CRD-8482B", NULL, ATA_HORKAGE_NODMA },
3846 { "CRD-84", NULL, ATA_HORKAGE_NODMA },
3847 { "SanDisk SDP3B", NULL, ATA_HORKAGE_NODMA },
3848 { "SanDisk SDP3B-64", NULL, ATA_HORKAGE_NODMA },
3849 { "SANYO CD-ROM CRD", NULL, ATA_HORKAGE_NODMA },
3850 { "HITACHI CDR-8", NULL, ATA_HORKAGE_NODMA },
3851 { "HITACHI CDR-8335", NULL, ATA_HORKAGE_NODMA },
3852 { "HITACHI CDR-8435", NULL, ATA_HORKAGE_NODMA },
3853 { "Toshiba CD-ROM XM-6202B", NULL, ATA_HORKAGE_NODMA },
3854 { "TOSHIBA CD-ROM XM-1702BC", NULL, ATA_HORKAGE_NODMA },
3855 { "CD-532E-A", NULL, ATA_HORKAGE_NODMA },
3856 { "E-IDE CD-ROM CR-840",NULL, ATA_HORKAGE_NODMA },
3857 { "CD-ROM Drive/F5A", NULL, ATA_HORKAGE_NODMA },
3858 { "WPI CDD-820", NULL, ATA_HORKAGE_NODMA },
3859 { "SAMSUNG CD-ROM SC-148C", NULL, ATA_HORKAGE_NODMA },
3860 { "SAMSUNG CD-ROM SC", NULL, ATA_HORKAGE_NODMA },
Alan Cox6919a0a2006-10-27 19:08:46 -07003861 { "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,ATA_HORKAGE_NODMA },
3862 { "_NEC DV5800A", NULL, ATA_HORKAGE_NODMA },
3863 { "SAMSUNG CD-ROM SN-124","N001", ATA_HORKAGE_NODMA },
Dave Jones39f19882007-05-21 14:31:03 -04003864 { "Seagate STT20000A", NULL, ATA_HORKAGE_NODMA },
Tejun Heo5acd50f2007-06-10 14:52:36 +09003865 { "IOMEGA ZIP 250 ATAPI", NULL, ATA_HORKAGE_NODMA }, /* temporary fix */
Tejun Heo39ce7122007-07-05 12:31:31 +09003866 { "IOMEGA ZIP 250 ATAPI Floppy",
3867 NULL, ATA_HORKAGE_NODMA },
Alan Cox6919a0a2006-10-27 19:08:46 -07003868
Albert Lee18d6e9d2007-04-02 11:34:15 +08003869 /* Weird ATAPI devices */
Tejun Heo40a1d532007-06-27 02:49:38 +09003870 { "TORiSAN DVD-ROM DRD-N216", NULL, ATA_HORKAGE_MAX_SEC_128 },
Albert Lee18d6e9d2007-04-02 11:34:15 +08003871
Alan Cox6919a0a2006-10-27 19:08:46 -07003872 /* Devices we expect to fail diagnostics */
3873
3874 /* Devices where NCQ should be avoided */
3875 /* NCQ is slow */
3876 { "WDC WD740ADFD-00", NULL, ATA_HORKAGE_NONCQ },
Tejun Heo09125ea2007-02-28 15:21:23 +09003877 /* http://thread.gmane.org/gmane.linux.ide/14907 */
3878 { "FUJITSU MHT2060BH", NULL, ATA_HORKAGE_NONCQ },
Paul Rolland7acfaf32007-03-26 21:43:44 -08003879 /* NCQ is broken */
Jeff Garzik539cc7c2007-09-20 16:31:47 -04003880 { "Maxtor *", "BANC*", ATA_HORKAGE_NONCQ },
Alan Cox0e3dbc02007-09-20 15:22:47 +01003881 { "Maxtor 7V300F0", "VA111630", ATA_HORKAGE_NONCQ },
David Milburn0b0a43e2007-09-28 14:29:06 -05003882 { "HITACHI HDS7250SASUN500G*", NULL, ATA_HORKAGE_NONCQ },
3883 { "HITACHI HDS7225SBSUN250G*", NULL, ATA_HORKAGE_NONCQ },
Jeff Garzik539cc7c2007-09-20 16:31:47 -04003884
Robert Hancock36e337d2007-04-02 22:05:29 -06003885 /* Blacklist entries taken from Silicon Image 3124/3132
3886 Windows driver .inf file - also several Linux problem reports */
3887 { "HTS541060G9SA00", "MB3OC60D", ATA_HORKAGE_NONCQ, },
3888 { "HTS541080G9SA00", "MB4OC60D", ATA_HORKAGE_NONCQ, },
3889 { "HTS541010G9SA00", "MBZOC60D", ATA_HORKAGE_NONCQ, },
Tejun Heobd9c5a32007-06-08 22:20:59 +09003890 /* Drives which do spurious command completion */
3891 { "HTS541680J9SA00", "SB2IC7EP", ATA_HORKAGE_NONCQ, },
Tejun Heo2f8fceb2007-06-18 14:38:41 +09003892 { "HTS541612J9SA00", "SBDIC7JP", ATA_HORKAGE_NONCQ, },
Tejun Heoe14cbfa2007-06-25 11:28:59 +09003893 { "Hitachi HTS541616J9SA00", "SB4OC70P", ATA_HORKAGE_NONCQ, },
Tejun Heo2f8fceb2007-06-18 14:38:41 +09003894 { "WDC WD740ADFD-00NLR1", NULL, ATA_HORKAGE_NONCQ, },
Tejun Heo7f567622007-09-28 06:29:23 -07003895 { "WDC WD3200AAJS-00RYA0", "12.01B01", ATA_HORKAGE_NONCQ, },
Tejun Heoa520f262007-07-10 16:16:18 +09003896 { "FUJITSU MHV2080BH", "00840028", ATA_HORKAGE_NONCQ, },
Tejun Heo7f567622007-09-28 06:29:23 -07003897 { "ST9120822AS", "3.CLF", ATA_HORKAGE_NONCQ, },
Tejun Heo3fb65892007-07-20 12:49:38 +09003898 { "ST9160821AS", "3.CLF", ATA_HORKAGE_NONCQ, },
Tejun Heo7f567622007-09-28 06:29:23 -07003899 { "ST3160812AS", "3.ADJ", ATA_HORKAGE_NONCQ, },
3900 { "ST980813AS", "3.ADB", ATA_HORKAGE_NONCQ, },
Tejun Heo5d6aca82007-07-28 16:25:25 +09003901 { "SAMSUNG HD401LJ", "ZZ100-15", ATA_HORKAGE_NONCQ, },
Alan Cox6919a0a2006-10-27 19:08:46 -07003902
Tejun Heo16c55b02007-08-29 11:58:33 +09003903 /* devices which puke on READ_NATIVE_MAX */
3904 { "HDS724040KLSA80", "KFAOA20N", ATA_HORKAGE_BROKEN_HPA, },
3905 { "WDC WD3200JD-00KLB0", "WD-WCAMR1130137", ATA_HORKAGE_BROKEN_HPA },
3906 { "WDC WD2500JD-00HBB0", "WD-WMAL71490727", ATA_HORKAGE_BROKEN_HPA },
3907 { "MAXTOR 6L080L4", "A93.0500", ATA_HORKAGE_BROKEN_HPA },
Alan Cox6919a0a2006-10-27 19:08:46 -07003908
Alan Cox93328e12007-09-29 04:06:48 -04003909 /* Devices which report 1 sector over size HPA */
3910 { "ST340823A", NULL, ATA_HORKAGE_HPA_SIZE, },
3911 { "ST320413A", NULL, ATA_HORKAGE_HPA_SIZE, },
3912
Alan Cox6919a0a2006-10-27 19:08:46 -07003913 /* End Marker */
3914 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003915};
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05003916
Jeff Garzik539cc7c2007-09-20 16:31:47 -04003917int strn_pattern_cmp(const char *patt, const char *name, int wildchar)
3918{
3919 const char *p;
3920 int len;
3921
3922 /*
3923 * check for trailing wildcard: *\0
3924 */
3925 p = strchr(patt, wildchar);
3926 if (p && ((*(p + 1)) == 0))
3927 len = p - patt;
3928 else
3929 len = strlen(name);
3930
3931 return strncmp(patt, name, len);
3932}
3933
Tejun Heo75683fe2007-07-05 13:31:27 +09003934static unsigned long ata_dev_blacklisted(const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003935{
Tejun Heo8bfa79f2007-01-02 20:19:40 +09003936 unsigned char model_num[ATA_ID_PROD_LEN + 1];
3937 unsigned char model_rev[ATA_ID_FW_REV_LEN + 1];
Alan Cox6919a0a2006-10-27 19:08:46 -07003938 const struct ata_blacklist_entry *ad = ata_device_blacklist;
Albert Lee3a778272006-06-22 13:00:25 +08003939
Tejun Heo8bfa79f2007-01-02 20:19:40 +09003940 ata_id_c_string(dev->id, model_num, ATA_ID_PROD, sizeof(model_num));
3941 ata_id_c_string(dev->id, model_rev, ATA_ID_FW_REV, sizeof(model_rev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003942
Alan Cox6919a0a2006-10-27 19:08:46 -07003943 while (ad->model_num) {
Jeff Garzik539cc7c2007-09-20 16:31:47 -04003944 if (!strn_pattern_cmp(ad->model_num, model_num, '*')) {
Alan Cox6919a0a2006-10-27 19:08:46 -07003945 if (ad->model_rev == NULL)
3946 return ad->horkage;
Jeff Garzik539cc7c2007-09-20 16:31:47 -04003947 if (!strn_pattern_cmp(ad->model_rev, model_rev, '*'))
Alan Cox6919a0a2006-10-27 19:08:46 -07003948 return ad->horkage;
Alan Coxf4b15fe2006-03-22 15:54:04 +00003949 }
Alan Cox6919a0a2006-10-27 19:08:46 -07003950 ad++;
Alan Coxf4b15fe2006-03-22 15:54:04 +00003951 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003952 return 0;
3953}
3954
Alan Cox6919a0a2006-10-27 19:08:46 -07003955static int ata_dma_blacklisted(const struct ata_device *dev)
3956{
3957 /* We don't support polling DMA.
3958 * DMA blacklist those ATAPI devices with CDB-intr (and use PIO)
3959 * if the LLDD handles only interrupts in the HSM_ST_LAST state.
3960 */
Tejun Heo9af5c9c2007-08-06 18:36:22 +09003961 if ((dev->link->ap->flags & ATA_FLAG_PIO_POLLING) &&
Alan Cox6919a0a2006-10-27 19:08:46 -07003962 (dev->flags & ATA_DFLAG_CDB_INTR))
3963 return 1;
Tejun Heo75683fe2007-07-05 13:31:27 +09003964 return (dev->horkage & ATA_HORKAGE_NODMA) ? 1 : 0;
Alan Cox6919a0a2006-10-27 19:08:46 -07003965}
3966
Tejun Heoa6d5a512006-03-06 04:31:57 +09003967/**
3968 * ata_dev_xfermask - Compute supported xfermask of the given device
Tejun Heoa6d5a512006-03-06 04:31:57 +09003969 * @dev: Device to compute xfermask for
3970 *
Tejun Heoacf356b2006-03-24 14:07:50 +09003971 * Compute supported xfermask of @dev and store it in
3972 * dev->*_mask. This function is responsible for applying all
3973 * known limits including host controller limits, device
3974 * blacklist, etc...
Tejun Heoa6d5a512006-03-06 04:31:57 +09003975 *
3976 * LOCKING:
3977 * None.
Tejun Heoa6d5a512006-03-06 04:31:57 +09003978 */
Tejun Heo3373efd2006-05-15 20:57:53 +09003979static void ata_dev_xfermask(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09003981 struct ata_link *link = dev->link;
3982 struct ata_port *ap = link->ap;
Jeff Garzikcca39742006-08-24 03:19:22 -04003983 struct ata_host *host = ap->host;
Tejun Heoa6d5a512006-03-06 04:31:57 +09003984 unsigned long xfer_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003985
Tejun Heo37deecb2006-08-10 16:59:07 +09003986 /* controller modes available */
Tejun Heo565083e2006-04-02 17:54:47 +09003987 xfer_mask = ata_pack_xfermask(ap->pio_mask,
3988 ap->mwdma_mask, ap->udma_mask);
3989
Robert Hancock8343f882007-03-06 02:37:51 -08003990 /* drive modes available */
Tejun Heo37deecb2006-08-10 16:59:07 +09003991 xfer_mask &= ata_pack_xfermask(dev->pio_mask,
3992 dev->mwdma_mask, dev->udma_mask);
3993 xfer_mask &= ata_id_xfermask(dev->id);
Tejun Heo565083e2006-04-02 17:54:47 +09003994
Alan Coxb352e572006-08-10 18:52:12 +01003995 /*
3996 * CFA Advanced TrueIDE timings are not allowed on a shared
3997 * cable
3998 */
3999 if (ata_dev_pair(dev)) {
4000 /* No PIO5 or PIO6 */
4001 xfer_mask &= ~(0x03 << (ATA_SHIFT_PIO + 5));
4002 /* No MWDMA3 or MWDMA 4 */
4003 xfer_mask &= ~(0x03 << (ATA_SHIFT_MWDMA + 3));
4004 }
4005
Tejun Heo37deecb2006-08-10 16:59:07 +09004006 if (ata_dma_blacklisted(dev)) {
4007 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
Tejun Heof15a1da2006-05-15 20:57:56 +09004008 ata_dev_printk(dev, KERN_WARNING,
4009 "device is on DMA blacklist, disabling DMA\n");
Tejun Heo37deecb2006-08-10 16:59:07 +09004010 }
Tejun Heoa6d5a512006-03-06 04:31:57 +09004011
Petr Vandrovec14d66ab2007-03-08 10:12:12 +01004012 if ((host->flags & ATA_HOST_SIMPLEX) &&
4013 host->simplex_claimed && host->simplex_claimed != ap) {
Tejun Heo37deecb2006-08-10 16:59:07 +09004014 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
4015 ata_dev_printk(dev, KERN_WARNING, "simplex DMA is claimed by "
4016 "other device, disabling DMA\n");
Alan Cox5444a6f2006-03-27 18:58:20 +01004017 }
Tejun Heo565083e2006-04-02 17:54:47 +09004018
Jeff Garzike4246752007-03-09 09:56:46 -05004019 if (ap->flags & ATA_FLAG_NO_IORDY)
4020 xfer_mask &= ata_pio_mask_no_iordy(dev);
4021
Alan Cox5444a6f2006-03-27 18:58:20 +01004022 if (ap->ops->mode_filter)
Alan Coxa76b62c2007-03-09 09:34:07 -05004023 xfer_mask = ap->ops->mode_filter(dev, xfer_mask);
Alan Cox5444a6f2006-03-27 18:58:20 +01004024
Robert Hancock8343f882007-03-06 02:37:51 -08004025 /* Apply cable rule here. Don't apply it early because when
4026 * we handle hot plug the cable type can itself change.
4027 * Check this last so that we know if the transfer rate was
4028 * solely limited by the cable.
4029 * Unknown or 80 wire cables reported host side are checked
4030 * drive side as well. Cases where we know a 40wire cable
4031 * is used safely for 80 are not checked here.
4032 */
4033 if (xfer_mask & (0xF8 << ATA_SHIFT_UDMA))
4034 /* UDMA/44 or higher would be available */
4035 if((ap->cbl == ATA_CBL_PATA40) ||
4036 (ata_drive_40wire(dev->id) &&
4037 (ap->cbl == ATA_CBL_PATA_UNK ||
4038 ap->cbl == ATA_CBL_PATA80))) {
4039 ata_dev_printk(dev, KERN_WARNING,
4040 "limited to UDMA/33 due to 40-wire cable\n");
4041 xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
4042 }
4043
Tejun Heo565083e2006-04-02 17:54:47 +09004044 ata_unpack_xfermask(xfer_mask, &dev->pio_mask,
4045 &dev->mwdma_mask, &dev->udma_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004046}
4047
Linus Torvalds1da177e2005-04-16 15:20:36 -07004048/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004049 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050 * @dev: Device to which command will be sent
4051 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04004052 * Issue SET FEATURES - XFER MODE command to device @dev
4053 * on port @ap.
4054 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004055 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004056 * PCI/etc. bus probe sem.
Tejun Heo83206a22006-03-24 15:25:31 +09004057 *
4058 * RETURNS:
4059 * 0 on success, AC_ERR_* mask otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060 */
4061
Tejun Heo3373efd2006-05-15 20:57:53 +09004062static unsigned int ata_dev_set_xfermode(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063{
Tejun Heoa0123702005-12-13 14:49:31 +09004064 struct ata_taskfile tf;
Tejun Heo83206a22006-03-24 15:25:31 +09004065 unsigned int err_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004066
4067 /* set up set-features taskfile */
4068 DPRINTK("set features - xfer mode\n");
4069
Tejun Heo464cf172007-05-27 15:10:40 +02004070 /* Some controllers and ATAPI devices show flaky interrupt
4071 * behavior after setting xfer mode. Use polling instead.
4072 */
Tejun Heo3373efd2006-05-15 20:57:53 +09004073 ata_tf_init(dev, &tf);
Tejun Heoa0123702005-12-13 14:49:31 +09004074 tf.command = ATA_CMD_SET_FEATURES;
4075 tf.feature = SETFEATURES_XFER;
Tejun Heo464cf172007-05-27 15:10:40 +02004076 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_POLLING;
Tejun Heoa0123702005-12-13 14:49:31 +09004077 tf.protocol = ATA_PROT_NODATA;
4078 tf.nsect = dev->xfer_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079
Tejun Heo3373efd2006-05-15 20:57:53 +09004080 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081
Tejun Heo83206a22006-03-24 15:25:31 +09004082 DPRINTK("EXIT, err_mask=%x\n", err_mask);
4083 return err_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084}
4085
4086/**
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04004087 * ata_dev_set_AN - Issue SET FEATURES - SATA FEATURES
4088 * @dev: Device to which command will be sent
4089 * @enable: Whether to enable or disable the feature
4090 *
4091 * Issue SET FEATURES - SATA FEATURES command to device @dev
4092 * on port @ap with sector count set to indicate Asynchronous
4093 * Notification feature
4094 *
4095 * LOCKING:
4096 * PCI/etc. bus probe sem.
4097 *
4098 * RETURNS:
4099 * 0 on success, AC_ERR_* mask otherwise.
4100 */
4101static unsigned int ata_dev_set_AN(struct ata_device *dev, u8 enable)
4102{
4103 struct ata_taskfile tf;
4104 unsigned int err_mask;
4105
4106 /* set up set-features taskfile */
4107 DPRINTK("set features - SATA features\n");
4108
4109 ata_tf_init(dev, &tf);
4110 tf.command = ATA_CMD_SET_FEATURES;
4111 tf.feature = enable;
4112 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
4113 tf.protocol = ATA_PROT_NODATA;
4114 tf.nsect = SATA_AN;
4115
4116 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
4117
4118 DPRINTK("EXIT, err_mask=%x\n", err_mask);
4119 return err_mask;
4120}
4121
4122/**
Albert Lee8bf62ece2005-05-12 15:29:42 -04004123 * ata_dev_init_params - Issue INIT DEV PARAMS command
Albert Lee8bf62ece2005-05-12 15:29:42 -04004124 * @dev: Device to which command will be sent
Randy Dunlape2a7f772006-05-18 10:50:18 -07004125 * @heads: Number of heads (taskfile parameter)
4126 * @sectors: Number of sectors (taskfile parameter)
Albert Lee8bf62ece2005-05-12 15:29:42 -04004127 *
4128 * LOCKING:
Tejun Heo6aff8f12006-02-15 18:24:09 +09004129 * Kernel thread context (may sleep)
4130 *
4131 * RETURNS:
4132 * 0 on success, AC_ERR_* mask otherwise.
Albert Lee8bf62ece2005-05-12 15:29:42 -04004133 */
Tejun Heo3373efd2006-05-15 20:57:53 +09004134static unsigned int ata_dev_init_params(struct ata_device *dev,
4135 u16 heads, u16 sectors)
Albert Lee8bf62ece2005-05-12 15:29:42 -04004136{
Tejun Heoa0123702005-12-13 14:49:31 +09004137 struct ata_taskfile tf;
Tejun Heo6aff8f12006-02-15 18:24:09 +09004138 unsigned int err_mask;
Albert Lee8bf62ece2005-05-12 15:29:42 -04004139
4140 /* Number of sectors per track 1-255. Number of heads 1-16 */
4141 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
Albert Lee00b6f5e2006-03-27 16:39:18 +08004142 return AC_ERR_INVALID;
Albert Lee8bf62ece2005-05-12 15:29:42 -04004143
4144 /* set up init dev params taskfile */
4145 DPRINTK("init dev params \n");
4146
Tejun Heo3373efd2006-05-15 20:57:53 +09004147 ata_tf_init(dev, &tf);
Tejun Heoa0123702005-12-13 14:49:31 +09004148 tf.command = ATA_CMD_INIT_DEV_PARAMS;
4149 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
4150 tf.protocol = ATA_PROT_NODATA;
4151 tf.nsect = sectors;
4152 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
Albert Lee8bf62ece2005-05-12 15:29:42 -04004153
Tejun Heo3373efd2006-05-15 20:57:53 +09004154 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
Alan Cox18b24662007-08-08 14:28:49 +01004155 /* A clean abort indicates an original or just out of spec drive
4156 and we should continue as we issue the setup based on the
4157 drive reported working geometry */
4158 if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
4159 err_mask = 0;
Albert Lee8bf62ece2005-05-12 15:29:42 -04004160
Tejun Heo6aff8f12006-02-15 18:24:09 +09004161 DPRINTK("EXIT, err_mask=%x\n", err_mask);
4162 return err_mask;
Albert Lee8bf62ece2005-05-12 15:29:42 -04004163}
4164
4165/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004166 * ata_sg_clean - Unmap DMA memory associated with command
4167 * @qc: Command containing DMA memory to be released
4168 *
4169 * Unmap all mapped DMA memory associated with this command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 *
4171 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004172 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173 */
Tejun Heo70e6ad02006-11-14 22:47:10 +09004174void ata_sg_clean(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175{
4176 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004177 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004179 void *pad_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180
Tejun Heoa46314742006-02-11 19:11:13 +09004181 WARN_ON(!(qc->flags & ATA_QCFLAG_DMAMAP));
4182 WARN_ON(sg == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183
4184 if (qc->flags & ATA_QCFLAG_SINGLE)
Jeff Garzikf1318832006-02-20 16:55:56 -05004185 WARN_ON(qc->n_elem > 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05004187 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004189 /* if we padded the buffer out to 32-bit bound, and data
4190 * xfer direction is from-device, we must copy from the
4191 * pad buffer back into the supplied buffer
4192 */
4193 if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
4194 pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
4195
4196 if (qc->flags & ATA_QCFLAG_SG) {
Jeff Garzike1410f22005-11-14 14:06:26 -05004197 if (qc->n_elem)
Brian King2f1f6102006-03-23 17:30:15 -06004198 dma_unmap_sg(ap->dev, sg, qc->n_elem, dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004199 /* restore last sg */
4200 sg[qc->orig_n_elem - 1].length += qc->pad_len;
4201 if (pad_buf) {
4202 struct scatterlist *psg = &qc->pad_sgent;
4203 void *addr = kmap_atomic(psg->page, KM_IRQ0);
4204 memcpy(addr + psg->offset, pad_buf, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05004205 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004206 }
4207 } else {
Tejun Heo2e242fa2006-02-20 23:48:38 +09004208 if (qc->n_elem)
Brian King2f1f6102006-03-23 17:30:15 -06004209 dma_unmap_single(ap->dev,
Jeff Garzike1410f22005-11-14 14:06:26 -05004210 sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
4211 dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004212 /* restore sg */
4213 sg->length += qc->pad_len;
4214 if (pad_buf)
4215 memcpy(qc->buf_virt + sg->length - qc->pad_len,
4216 pad_buf, qc->pad_len);
4217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218
4219 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004220 qc->__sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221}
4222
4223/**
4224 * ata_fill_sg - Fill PCI IDE PRD table
4225 * @qc: Metadata associated with taskfile to be transferred
4226 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04004227 * Fill PCI IDE PRD (scatter-gather) table with segments
4228 * associated with the current disk command.
4229 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004231 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004232 *
4233 */
4234static void ata_fill_sg(struct ata_queued_cmd *qc)
4235{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004236 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004237 struct scatterlist *sg;
4238 unsigned int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239
Tejun Heoa46314742006-02-11 19:11:13 +09004240 WARN_ON(qc->__sg == NULL);
Jeff Garzikf1318832006-02-20 16:55:56 -05004241 WARN_ON(qc->n_elem == 0 && qc->pad_len == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004242
4243 idx = 0;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004244 ata_for_each_sg(sg, qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245 u32 addr, offset;
4246 u32 sg_len, len;
4247
4248 /* determine if physical DMA addr spans 64K boundary.
4249 * Note h/w doesn't support 64-bit, so we unconditionally
4250 * truncate dma_addr_t to u32.
4251 */
4252 addr = (u32) sg_dma_address(sg);
4253 sg_len = sg_dma_len(sg);
4254
4255 while (sg_len) {
4256 offset = addr & 0xffff;
4257 len = sg_len;
4258 if ((offset + sg_len) > 0x10000)
4259 len = 0x10000 - offset;
4260
4261 ap->prd[idx].addr = cpu_to_le32(addr);
4262 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
4263 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
4264
4265 idx++;
4266 sg_len -= len;
4267 addr += len;
4268 }
4269 }
4270
4271 if (idx)
4272 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
4273}
Tejun Heob9a41972007-06-27 02:48:43 +09004274
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275/**
Alan Coxd26fc952007-07-06 19:13:52 -04004276 * ata_fill_sg_dumb - Fill PCI IDE PRD table
4277 * @qc: Metadata associated with taskfile to be transferred
4278 *
4279 * Fill PCI IDE PRD (scatter-gather) table with segments
4280 * associated with the current disk command. Perform the fill
4281 * so that we avoid writing any length 64K records for
4282 * controllers that don't follow the spec.
4283 *
4284 * LOCKING:
4285 * spin_lock_irqsave(host lock)
4286 *
4287 */
4288static void ata_fill_sg_dumb(struct ata_queued_cmd *qc)
4289{
4290 struct ata_port *ap = qc->ap;
4291 struct scatterlist *sg;
4292 unsigned int idx;
4293
4294 WARN_ON(qc->__sg == NULL);
4295 WARN_ON(qc->n_elem == 0 && qc->pad_len == 0);
4296
4297 idx = 0;
4298 ata_for_each_sg(sg, qc) {
4299 u32 addr, offset;
4300 u32 sg_len, len, blen;
4301
4302 /* determine if physical DMA addr spans 64K boundary.
4303 * Note h/w doesn't support 64-bit, so we unconditionally
4304 * truncate dma_addr_t to u32.
4305 */
4306 addr = (u32) sg_dma_address(sg);
4307 sg_len = sg_dma_len(sg);
4308
4309 while (sg_len) {
4310 offset = addr & 0xffff;
4311 len = sg_len;
4312 if ((offset + sg_len) > 0x10000)
4313 len = 0x10000 - offset;
4314
4315 blen = len & 0xffff;
4316 ap->prd[idx].addr = cpu_to_le32(addr);
4317 if (blen == 0) {
4318 /* Some PATA chipsets like the CS5530 can't
4319 cope with 0x0000 meaning 64K as the spec says */
4320 ap->prd[idx].flags_len = cpu_to_le32(0x8000);
4321 blen = 0x8000;
4322 ap->prd[++idx].addr = cpu_to_le32(addr + 0x8000);
4323 }
4324 ap->prd[idx].flags_len = cpu_to_le32(blen);
4325 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
4326
4327 idx++;
4328 sg_len -= len;
4329 addr += len;
4330 }
4331 }
4332
4333 if (idx)
4334 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
4335}
4336
4337/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
4339 * @qc: Metadata associated with taskfile to check
4340 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04004341 * Allow low-level driver to filter ATA PACKET commands, returning
4342 * a status indicating whether or not it is OK to use DMA for the
4343 * supplied PACKET command.
4344 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004346 * spin_lock_irqsave(host lock)
Jeff Garzik0cba6322005-05-30 19:49:12 -04004347 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348 * RETURNS: 0 when ATAPI DMA can be used
4349 * nonzero otherwise
4350 */
4351int ata_check_atapi_dma(struct ata_queued_cmd *qc)
4352{
4353 struct ata_port *ap = qc->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004354
Tejun Heob9a41972007-06-27 02:48:43 +09004355 /* Don't allow DMA if it isn't multiple of 16 bytes. Quite a
4356 * few ATAPI devices choke on such DMA requests.
4357 */
4358 if (unlikely(qc->nbytes & 15))
4359 return 1;
Albert Lee6f23a312007-04-02 11:39:25 +08004360
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361 if (ap->ops->check_atapi_dma)
Tejun Heob9a41972007-06-27 02:48:43 +09004362 return ap->ops->check_atapi_dma(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363
Tejun Heob9a41972007-06-27 02:48:43 +09004364 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365}
Tejun Heob9a41972007-06-27 02:48:43 +09004366
Linus Torvalds1da177e2005-04-16 15:20:36 -07004367/**
Tejun Heo31cc23b2007-09-23 13:14:12 +09004368 * ata_std_qc_defer - Check whether a qc needs to be deferred
4369 * @qc: ATA command in question
4370 *
4371 * Non-NCQ commands cannot run with any other command, NCQ or
4372 * not. As upper layer only knows the queue depth, we are
4373 * responsible for maintaining exclusion. This function checks
4374 * whether a new command @qc can be issued.
4375 *
4376 * LOCKING:
4377 * spin_lock_irqsave(host lock)
4378 *
4379 * RETURNS:
4380 * ATA_DEFER_* if deferring is needed, 0 otherwise.
4381 */
4382int ata_std_qc_defer(struct ata_queued_cmd *qc)
4383{
4384 struct ata_link *link = qc->dev->link;
4385
4386 if (qc->tf.protocol == ATA_PROT_NCQ) {
4387 if (!ata_tag_valid(link->active_tag))
4388 return 0;
4389 } else {
4390 if (!ata_tag_valid(link->active_tag) && !link->sactive)
4391 return 0;
4392 }
4393
4394 return ATA_DEFER_LINK;
4395}
4396
4397/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398 * ata_qc_prep - Prepare taskfile for submission
4399 * @qc: Metadata associated with taskfile to be prepared
4400 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04004401 * Prepare ATA taskfile for submission.
4402 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004404 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405 */
4406void ata_qc_prep(struct ata_queued_cmd *qc)
4407{
4408 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
4409 return;
4410
4411 ata_fill_sg(qc);
4412}
4413
Alan Coxd26fc952007-07-06 19:13:52 -04004414/**
4415 * ata_dumb_qc_prep - Prepare taskfile for submission
4416 * @qc: Metadata associated with taskfile to be prepared
4417 *
4418 * Prepare ATA taskfile for submission.
4419 *
4420 * LOCKING:
4421 * spin_lock_irqsave(host lock)
4422 */
4423void ata_dumb_qc_prep(struct ata_queued_cmd *qc)
4424{
4425 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
4426 return;
4427
4428 ata_fill_sg_dumb(qc);
4429}
4430
Brian Kinge46834c2006-03-17 17:04:03 -06004431void ata_noop_qc_prep(struct ata_queued_cmd *qc) { }
4432
Jeff Garzik0cba6322005-05-30 19:49:12 -04004433/**
4434 * ata_sg_init_one - Associate command with memory buffer
4435 * @qc: Command to be associated
4436 * @buf: Memory buffer
4437 * @buflen: Length of memory buffer, in bytes.
4438 *
4439 * Initialize the data-related elements of queued_cmd @qc
4440 * to point to a single memory buffer, @buf of byte length @buflen.
4441 *
4442 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004443 * spin_lock_irqsave(host lock)
Jeff Garzik0cba6322005-05-30 19:49:12 -04004444 */
4445
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
4447{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448 qc->flags |= ATA_QCFLAG_SINGLE;
4449
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004450 qc->__sg = &qc->sgent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451 qc->n_elem = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004452 qc->orig_n_elem = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004453 qc->buf_virt = buf;
Brian King233277c2006-06-07 11:25:31 -05004454 qc->nbytes = buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455
Tejun Heo61c05962006-11-14 22:35:43 +09004456 sg_init_one(&qc->sgent, buf, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457}
4458
Jeff Garzik0cba6322005-05-30 19:49:12 -04004459/**
4460 * ata_sg_init - Associate command with scatter-gather table.
4461 * @qc: Command to be associated
4462 * @sg: Scatter-gather table.
4463 * @n_elem: Number of elements in s/g table.
4464 *
4465 * Initialize the data-related elements of queued_cmd @qc
4466 * to point to a scatter-gather table @sg, containing @n_elem
4467 * elements.
4468 *
4469 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004470 * spin_lock_irqsave(host lock)
Jeff Garzik0cba6322005-05-30 19:49:12 -04004471 */
4472
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
4474 unsigned int n_elem)
4475{
4476 qc->flags |= ATA_QCFLAG_SG;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004477 qc->__sg = sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478 qc->n_elem = n_elem;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004479 qc->orig_n_elem = n_elem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480}
4481
4482/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004483 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
4484 * @qc: Command with memory buffer to be mapped.
4485 *
4486 * DMA-map the memory buffer associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487 *
4488 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004489 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490 *
4491 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004492 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493 */
4494
4495static int ata_sg_setup_one(struct ata_queued_cmd *qc)
4496{
4497 struct ata_port *ap = qc->ap;
4498 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004499 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 dma_addr_t dma_address;
Tejun Heo2e242fa2006-02-20 23:48:38 +09004501 int trim_sg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004503 /* we must lengthen transfers to end on a 32-bit boundary */
4504 qc->pad_len = sg->length & 3;
4505 if (qc->pad_len) {
4506 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
4507 struct scatterlist *psg = &qc->pad_sgent;
4508
Tejun Heoa46314742006-02-11 19:11:13 +09004509 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004510
4511 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
4512
4513 if (qc->tf.flags & ATA_TFLAG_WRITE)
4514 memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
4515 qc->pad_len);
4516
4517 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
4518 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
4519 /* trim sg */
4520 sg->length -= qc->pad_len;
Tejun Heo2e242fa2006-02-20 23:48:38 +09004521 if (sg->length == 0)
4522 trim_sg = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004523
4524 DPRINTK("padding done, sg->length=%u pad_len=%u\n",
4525 sg->length, qc->pad_len);
4526 }
4527
Tejun Heo2e242fa2006-02-20 23:48:38 +09004528 if (trim_sg) {
4529 qc->n_elem--;
Jeff Garzike1410f22005-11-14 14:06:26 -05004530 goto skip_map;
4531 }
4532
Brian King2f1f6102006-03-23 17:30:15 -06004533 dma_address = dma_map_single(ap->dev, qc->buf_virt,
Albert Lee32529e02005-05-26 03:49:42 -04004534 sg->length, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05004535 if (dma_mapping_error(dma_address)) {
4536 /* restore sg */
4537 sg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05004539 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540
4541 sg_dma_address(sg) = dma_address;
Albert Lee32529e02005-05-26 03:49:42 -04004542 sg_dma_len(sg) = sg->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543
Tejun Heo2e242fa2006-02-20 23:48:38 +09004544skip_map:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
4546 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
4547
4548 return 0;
4549}
4550
4551/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004552 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
4553 * @qc: Command with scatter-gather table to be mapped.
4554 *
4555 * DMA-map the scatter-gather table associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556 *
4557 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004558 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559 *
4560 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004561 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562 *
4563 */
4564
4565static int ata_sg_setup(struct ata_queued_cmd *qc)
4566{
4567 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004568 struct scatterlist *sg = qc->__sg;
4569 struct scatterlist *lsg = &sg[qc->n_elem - 1];
Jeff Garzike1410f22005-11-14 14:06:26 -05004570 int n_elem, pre_n_elem, dir, trim_sg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571
Tejun Heo44877b42007-02-21 01:06:51 +09004572 VPRINTK("ENTER, ata%u\n", ap->print_id);
Tejun Heoa46314742006-02-11 19:11:13 +09004573 WARN_ON(!(qc->flags & ATA_QCFLAG_SG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004575 /* we must lengthen transfers to end on a 32-bit boundary */
4576 qc->pad_len = lsg->length & 3;
4577 if (qc->pad_len) {
4578 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
4579 struct scatterlist *psg = &qc->pad_sgent;
4580 unsigned int offset;
4581
Tejun Heoa46314742006-02-11 19:11:13 +09004582 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004583
4584 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
4585
4586 /*
4587 * psg->page/offset are used to copy to-be-written
4588 * data in this function or read data in ata_sg_clean.
4589 */
4590 offset = lsg->offset + lsg->length - qc->pad_len;
4591 psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
4592 psg->offset = offset_in_page(offset);
4593
4594 if (qc->tf.flags & ATA_TFLAG_WRITE) {
4595 void *addr = kmap_atomic(psg->page, KM_IRQ0);
4596 memcpy(pad_buf, addr + psg->offset, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05004597 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004598 }
4599
4600 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
4601 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
4602 /* trim last sg */
4603 lsg->length -= qc->pad_len;
Jeff Garzike1410f22005-11-14 14:06:26 -05004604 if (lsg->length == 0)
4605 trim_sg = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004606
4607 DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
4608 qc->n_elem - 1, lsg->length, qc->pad_len);
4609 }
4610
Jeff Garzike1410f22005-11-14 14:06:26 -05004611 pre_n_elem = qc->n_elem;
4612 if (trim_sg && pre_n_elem)
4613 pre_n_elem--;
4614
4615 if (!pre_n_elem) {
4616 n_elem = 0;
4617 goto skip_map;
4618 }
4619
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620 dir = qc->dma_dir;
Brian King2f1f6102006-03-23 17:30:15 -06004621 n_elem = dma_map_sg(ap->dev, sg, pre_n_elem, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05004622 if (n_elem < 1) {
4623 /* restore last sg */
4624 lsg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05004626 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627
4628 DPRINTK("%d sg elements mapped\n", n_elem);
4629
Jeff Garzike1410f22005-11-14 14:06:26 -05004630skip_map:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631 qc->n_elem = n_elem;
4632
4633 return 0;
4634}
4635
4636/**
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004637 * swap_buf_le16 - swap halves of 16-bit words in place
Edward Falk0baab862005-06-02 18:17:13 -04004638 * @buf: Buffer to swap
4639 * @buf_words: Number of 16-bit words in buffer.
4640 *
4641 * Swap halves of 16-bit words if needed to convert from
4642 * little-endian byte order to native cpu byte order, or
4643 * vice-versa.
4644 *
4645 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004646 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04004647 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648void swap_buf_le16(u16 *buf, unsigned int buf_words)
4649{
4650#ifdef __BIG_ENDIAN
4651 unsigned int i;
4652
4653 for (i = 0; i < buf_words; i++)
4654 buf[i] = le16_to_cpu(buf[i]);
4655#endif /* __BIG_ENDIAN */
4656}
4657
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004658/**
Tejun Heo0d5ff562007-02-01 15:06:36 +09004659 * ata_data_xfer - Transfer data by PIO
Alan Coxa6b2c5d2006-05-22 16:59:59 +01004660 * @adev: device to target
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004661 * @buf: data buffer
4662 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04004663 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004664 *
4665 * Transfer data from/to the device data register by PIO.
4666 *
4667 * LOCKING:
4668 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004669 */
Tejun Heo0d5ff562007-02-01 15:06:36 +09004670void ata_data_xfer(struct ata_device *adev, unsigned char *buf,
4671 unsigned int buflen, int write_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09004673 struct ata_port *ap = adev->link->ap;
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004674 unsigned int words = buflen >> 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004675
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004676 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677 if (write_data)
Tejun Heo0d5ff562007-02-01 15:06:36 +09004678 iowrite16_rep(ap->ioaddr.data_addr, buf, words);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004679 else
Tejun Heo0d5ff562007-02-01 15:06:36 +09004680 ioread16_rep(ap->ioaddr.data_addr, buf, words);
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004681
4682 /* Transfer trailing 1 byte, if any. */
4683 if (unlikely(buflen & 0x01)) {
4684 u16 align_buf[1] = { 0 };
4685 unsigned char *trailing_buf = buf + buflen - 1;
4686
4687 if (write_data) {
4688 memcpy(align_buf, trailing_buf, 1);
Tejun Heo0d5ff562007-02-01 15:06:36 +09004689 iowrite16(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004690 } else {
Tejun Heo0d5ff562007-02-01 15:06:36 +09004691 align_buf[0] = cpu_to_le16(ioread16(ap->ioaddr.data_addr));
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004692 memcpy(trailing_buf, align_buf, 1);
4693 }
4694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695}
4696
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004697/**
Tejun Heo0d5ff562007-02-01 15:06:36 +09004698 * ata_data_xfer_noirq - Transfer data by PIO
Alan Cox75e99582006-05-24 14:14:41 +01004699 * @adev: device to target
4700 * @buf: data buffer
4701 * @buflen: buffer length
4702 * @write_data: read/write
4703 *
Tejun Heo88574552006-06-25 20:00:35 +09004704 * Transfer data from/to the device data register by PIO. Do the
Alan Cox75e99582006-05-24 14:14:41 +01004705 * transfer with interrupts disabled.
4706 *
4707 * LOCKING:
4708 * Inherited from caller.
4709 */
Tejun Heo0d5ff562007-02-01 15:06:36 +09004710void ata_data_xfer_noirq(struct ata_device *adev, unsigned char *buf,
4711 unsigned int buflen, int write_data)
Alan Cox75e99582006-05-24 14:14:41 +01004712{
4713 unsigned long flags;
4714 local_irq_save(flags);
Tejun Heo0d5ff562007-02-01 15:06:36 +09004715 ata_data_xfer(adev, buf, buflen, write_data);
Alan Cox75e99582006-05-24 14:14:41 +01004716 local_irq_restore(flags);
4717}
4718
4719
4720/**
Mark Lord5a5dbd12007-03-16 10:22:26 -04004721 * ata_pio_sector - Transfer a sector of data.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004722 * @qc: Command on going
4723 *
Mark Lord5a5dbd12007-03-16 10:22:26 -04004724 * Transfer qc->sect_size bytes of data from/to the ATA device.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004725 *
4726 * LOCKING:
4727 * Inherited from caller.
4728 */
4729
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730static void ata_pio_sector(struct ata_queued_cmd *qc)
4731{
4732 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004733 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734 struct ata_port *ap = qc->ap;
4735 struct page *page;
4736 unsigned int offset;
4737 unsigned char *buf;
4738
Mark Lord5a5dbd12007-03-16 10:22:26 -04004739 if (qc->curbytes == qc->nbytes - qc->sect_size)
Albert Lee14be71f2005-09-27 17:36:35 +08004740 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741
4742 page = sg[qc->cursg].page;
Tejun Heo726f0782007-01-03 17:30:39 +09004743 offset = sg[qc->cursg].offset + qc->cursg_ofs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744
4745 /* get the current page and offset */
4746 page = nth_page(page, (offset >> PAGE_SHIFT));
4747 offset %= PAGE_SIZE;
4748
Albert Lee7282aa42005-10-09 09:46:07 -04004749 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
4750
Albert Lee91b8b312005-10-09 09:48:44 -04004751 if (PageHighMem(page)) {
4752 unsigned long flags;
Albert Lee7282aa42005-10-09 09:46:07 -04004753
Alan Coxa6b2c5d2006-05-22 16:59:59 +01004754 /* FIXME: use a bounce buffer */
Albert Lee91b8b312005-10-09 09:48:44 -04004755 local_irq_save(flags);
4756 buf = kmap_atomic(page, KM_IRQ0);
Albert Lee083958d2005-10-09 09:47:31 -04004757
Albert Lee91b8b312005-10-09 09:48:44 -04004758 /* do the actual data transfer */
Mark Lord5a5dbd12007-03-16 10:22:26 -04004759 ap->ops->data_xfer(qc->dev, buf + offset, qc->sect_size, do_write);
Albert Lee91b8b312005-10-09 09:48:44 -04004760
4761 kunmap_atomic(buf, KM_IRQ0);
4762 local_irq_restore(flags);
4763 } else {
4764 buf = page_address(page);
Mark Lord5a5dbd12007-03-16 10:22:26 -04004765 ap->ops->data_xfer(qc->dev, buf + offset, qc->sect_size, do_write);
Albert Lee91b8b312005-10-09 09:48:44 -04004766 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767
Mark Lord5a5dbd12007-03-16 10:22:26 -04004768 qc->curbytes += qc->sect_size;
4769 qc->cursg_ofs += qc->sect_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770
Tejun Heo726f0782007-01-03 17:30:39 +09004771 if (qc->cursg_ofs == (&sg[qc->cursg])->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004772 qc->cursg++;
4773 qc->cursg_ofs = 0;
4774 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004776
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004777/**
Mark Lord5a5dbd12007-03-16 10:22:26 -04004778 * ata_pio_sectors - Transfer one or many sectors.
Albert Lee07f6f7d2005-11-01 19:33:20 +08004779 * @qc: Command on going
4780 *
Mark Lord5a5dbd12007-03-16 10:22:26 -04004781 * Transfer one or many sectors of data from/to the
Albert Lee07f6f7d2005-11-01 19:33:20 +08004782 * ATA device for the DRQ request.
4783 *
4784 * LOCKING:
4785 * Inherited from caller.
4786 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787
Albert Lee07f6f7d2005-11-01 19:33:20 +08004788static void ata_pio_sectors(struct ata_queued_cmd *qc)
4789{
4790 if (is_multi_taskfile(&qc->tf)) {
4791 /* READ/WRITE MULTIPLE */
4792 unsigned int nsect;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793
Jeff Garzik587005d2006-02-11 18:17:32 -05004794 WARN_ON(qc->dev->multi_count == 0);
Albert Lee07f6f7d2005-11-01 19:33:20 +08004795
Mark Lord5a5dbd12007-03-16 10:22:26 -04004796 nsect = min((qc->nbytes - qc->curbytes) / qc->sect_size,
Tejun Heo726f0782007-01-03 17:30:39 +09004797 qc->dev->multi_count);
Albert Lee07f6f7d2005-11-01 19:33:20 +08004798 while (nsect--)
4799 ata_pio_sector(qc);
4800 } else
4801 ata_pio_sector(qc);
Albert Lee4cc980b2007-08-15 03:19:45 -04004802
4803 ata_altstatus(qc->ap); /* flush */
Albert Lee07f6f7d2005-11-01 19:33:20 +08004804}
4805
4806/**
Albert Leec71c1852005-10-04 06:03:45 -04004807 * atapi_send_cdb - Write CDB bytes to hardware
4808 * @ap: Port to which ATAPI device is attached.
4809 * @qc: Taskfile currently active
4810 *
4811 * When device has indicated its readiness to accept
4812 * a CDB, this function is called. Send the CDB.
4813 *
4814 * LOCKING:
4815 * caller.
4816 */
4817
4818static void atapi_send_cdb(struct ata_port *ap, struct ata_queued_cmd *qc)
4819{
4820 /* send SCSI cdb */
4821 DPRINTK("send cdb\n");
Jeff Garzikdb024d52006-02-13 00:23:57 -05004822 WARN_ON(qc->dev->cdb_len < 12);
Albert Leec71c1852005-10-04 06:03:45 -04004823
Alan Coxa6b2c5d2006-05-22 16:59:59 +01004824 ap->ops->data_xfer(qc->dev, qc->cdb, qc->dev->cdb_len, 1);
Albert Leec71c1852005-10-04 06:03:45 -04004825 ata_altstatus(ap); /* flush */
4826
4827 switch (qc->tf.protocol) {
4828 case ATA_PROT_ATAPI:
4829 ap->hsm_task_state = HSM_ST;
4830 break;
4831 case ATA_PROT_ATAPI_NODATA:
4832 ap->hsm_task_state = HSM_ST_LAST;
4833 break;
4834 case ATA_PROT_ATAPI_DMA:
4835 ap->hsm_task_state = HSM_ST_LAST;
4836 /* initiate bmdma */
4837 ap->ops->bmdma_start(qc);
4838 break;
4839 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004840}
4841
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004842/**
4843 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
4844 * @qc: Command on going
4845 * @bytes: number of bytes
4846 *
4847 * Transfer Transfer data from/to the ATAPI device.
4848 *
4849 * LOCKING:
4850 * Inherited from caller.
4851 *
4852 */
4853
Linus Torvalds1da177e2005-04-16 15:20:36 -07004854static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
4855{
4856 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004857 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004858 struct ata_port *ap = qc->ap;
4859 struct page *page;
4860 unsigned char *buf;
4861 unsigned int offset, count;
4862
Albert Lee563a6e12005-08-12 14:17:50 +08004863 if (qc->curbytes + bytes >= qc->nbytes)
Albert Lee14be71f2005-09-27 17:36:35 +08004864 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865
4866next_sg:
Albert Lee563a6e12005-08-12 14:17:50 +08004867 if (unlikely(qc->cursg >= qc->n_elem)) {
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04004868 /*
Albert Lee563a6e12005-08-12 14:17:50 +08004869 * The end of qc->sg is reached and the device expects
4870 * more data to transfer. In order not to overrun qc->sg
4871 * and fulfill length specified in the byte count register,
4872 * - for read case, discard trailing data from the device
4873 * - for write case, padding zero data to the device
4874 */
4875 u16 pad_buf[1] = { 0 };
4876 unsigned int words = bytes >> 1;
4877 unsigned int i;
4878
4879 if (words) /* warning if bytes > 1 */
Tejun Heof15a1da2006-05-15 20:57:56 +09004880 ata_dev_printk(qc->dev, KERN_WARNING,
4881 "%u bytes trailing data\n", bytes);
Albert Lee563a6e12005-08-12 14:17:50 +08004882
4883 for (i = 0; i < words; i++)
Alan Coxa6b2c5d2006-05-22 16:59:59 +01004884 ap->ops->data_xfer(qc->dev, (unsigned char*)pad_buf, 2, do_write);
Albert Lee563a6e12005-08-12 14:17:50 +08004885
Albert Lee14be71f2005-09-27 17:36:35 +08004886 ap->hsm_task_state = HSM_ST_LAST;
Albert Lee563a6e12005-08-12 14:17:50 +08004887 return;
4888 }
4889
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004890 sg = &qc->__sg[qc->cursg];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891
Linus Torvalds1da177e2005-04-16 15:20:36 -07004892 page = sg->page;
4893 offset = sg->offset + qc->cursg_ofs;
4894
4895 /* get the current page and offset */
4896 page = nth_page(page, (offset >> PAGE_SHIFT));
4897 offset %= PAGE_SIZE;
4898
Albert Lee6952df02005-06-06 15:56:03 +08004899 /* don't overrun current sg */
Albert Lee32529e02005-05-26 03:49:42 -04004900 count = min(sg->length - qc->cursg_ofs, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901
4902 /* don't cross page boundaries */
4903 count = min(count, (unsigned int)PAGE_SIZE - offset);
4904
Albert Lee7282aa42005-10-09 09:46:07 -04004905 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
4906
Albert Lee91b8b312005-10-09 09:48:44 -04004907 if (PageHighMem(page)) {
4908 unsigned long flags;
Albert Lee7282aa42005-10-09 09:46:07 -04004909
Alan Coxa6b2c5d2006-05-22 16:59:59 +01004910 /* FIXME: use bounce buffer */
Albert Lee91b8b312005-10-09 09:48:44 -04004911 local_irq_save(flags);
4912 buf = kmap_atomic(page, KM_IRQ0);
Albert Lee083958d2005-10-09 09:47:31 -04004913
Albert Lee91b8b312005-10-09 09:48:44 -04004914 /* do the actual data transfer */
Alan Coxa6b2c5d2006-05-22 16:59:59 +01004915 ap->ops->data_xfer(qc->dev, buf + offset, count, do_write);
Albert Lee91b8b312005-10-09 09:48:44 -04004916
4917 kunmap_atomic(buf, KM_IRQ0);
4918 local_irq_restore(flags);
4919 } else {
4920 buf = page_address(page);
Alan Coxa6b2c5d2006-05-22 16:59:59 +01004921 ap->ops->data_xfer(qc->dev, buf + offset, count, do_write);
Albert Lee91b8b312005-10-09 09:48:44 -04004922 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004923
4924 bytes -= count;
4925 qc->curbytes += count;
4926 qc->cursg_ofs += count;
4927
Albert Lee32529e02005-05-26 03:49:42 -04004928 if (qc->cursg_ofs == sg->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929 qc->cursg++;
4930 qc->cursg_ofs = 0;
4931 }
4932
Albert Lee563a6e12005-08-12 14:17:50 +08004933 if (bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004934 goto next_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935}
4936
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004937/**
4938 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
4939 * @qc: Command on going
4940 *
4941 * Transfer Transfer data from/to the ATAPI device.
4942 *
4943 * LOCKING:
4944 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004945 */
4946
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947static void atapi_pio_bytes(struct ata_queued_cmd *qc)
4948{
4949 struct ata_port *ap = qc->ap;
4950 struct ata_device *dev = qc->dev;
4951 unsigned int ireason, bc_lo, bc_hi, bytes;
4952 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
4953
Albert Leeeec4c3f2006-05-18 17:51:10 +08004954 /* Abuse qc->result_tf for temp storage of intermediate TF
4955 * here to save some kernel stack usage.
4956 * For normal completion, qc->result_tf is not relevant. For
4957 * error, qc->result_tf is later overwritten by ata_qc_complete().
4958 * So, the correctness of qc->result_tf is not affected.
4959 */
4960 ap->ops->tf_read(ap, &qc->result_tf);
4961 ireason = qc->result_tf.nsect;
4962 bc_lo = qc->result_tf.lbam;
4963 bc_hi = qc->result_tf.lbah;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004964 bytes = (bc_hi << 8) | bc_lo;
4965
4966 /* shall be cleared to zero, indicating xfer of data */
4967 if (ireason & (1 << 0))
4968 goto err_out;
4969
4970 /* make sure transfer direction matches expected */
4971 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
4972 if (do_write != i_write)
4973 goto err_out;
4974
Tejun Heo44877b42007-02-21 01:06:51 +09004975 VPRINTK("ata%u: xfering %d bytes\n", ap->print_id, bytes);
Albert Lee312f7da2005-09-27 17:38:03 +08004976
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977 __atapi_pio_bytes(qc, bytes);
Albert Lee4cc980b2007-08-15 03:19:45 -04004978 ata_altstatus(ap); /* flush */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979
4980 return;
4981
4982err_out:
Tejun Heof15a1da2006-05-15 20:57:56 +09004983 ata_dev_printk(dev, KERN_INFO, "ATAPI check failed\n");
Tejun Heo11a56d22006-01-23 13:09:36 +09004984 qc->err_mask |= AC_ERR_HSM;
Albert Lee14be71f2005-09-27 17:36:35 +08004985 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986}
4987
4988/**
Albert Leec234fb02006-03-25 17:58:38 +08004989 * ata_hsm_ok_in_wq - Check if the qc can be handled in the workqueue.
4990 * @ap: the target ata_port
4991 * @qc: qc on going
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992 *
Albert Leec234fb02006-03-25 17:58:38 +08004993 * RETURNS:
4994 * 1 if ok in workqueue, 0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995 */
Albert Leec234fb02006-03-25 17:58:38 +08004996
4997static inline int ata_hsm_ok_in_wq(struct ata_port *ap, struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998{
Albert Leec234fb02006-03-25 17:58:38 +08004999 if (qc->tf.flags & ATA_TFLAG_POLLING)
5000 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001
Albert Leec234fb02006-03-25 17:58:38 +08005002 if (ap->hsm_task_state == HSM_ST_FIRST) {
5003 if (qc->tf.protocol == ATA_PROT_PIO &&
5004 (qc->tf.flags & ATA_TFLAG_WRITE))
5005 return 1;
5006
5007 if (is_atapi_taskfile(&qc->tf) &&
5008 !(qc->dev->flags & ATA_DFLAG_CDB_INTR))
5009 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010 }
5011
Albert Leec234fb02006-03-25 17:58:38 +08005012 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013}
5014
Albert Leec234fb02006-03-25 17:58:38 +08005015/**
Tejun Heoc17ea202006-05-15 20:59:29 +09005016 * ata_hsm_qc_complete - finish a qc running on standard HSM
5017 * @qc: Command to complete
5018 * @in_wq: 1 if called from workqueue, 0 otherwise
5019 *
5020 * Finish @qc which is running on standard HSM.
5021 *
5022 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04005023 * If @in_wq is zero, spin_lock_irqsave(host lock).
Tejun Heoc17ea202006-05-15 20:59:29 +09005024 * Otherwise, none on entry and grabs host lock.
5025 */
5026static void ata_hsm_qc_complete(struct ata_queued_cmd *qc, int in_wq)
5027{
5028 struct ata_port *ap = qc->ap;
5029 unsigned long flags;
5030
5031 if (ap->ops->error_handler) {
5032 if (in_wq) {
Jeff Garzikba6a1302006-06-22 23:46:10 -04005033 spin_lock_irqsave(ap->lock, flags);
Tejun Heoc17ea202006-05-15 20:59:29 +09005034
Jeff Garzikcca39742006-08-24 03:19:22 -04005035 /* EH might have kicked in while host lock is
5036 * released.
Tejun Heoc17ea202006-05-15 20:59:29 +09005037 */
5038 qc = ata_qc_from_tag(ap, qc->tag);
5039 if (qc) {
5040 if (likely(!(qc->err_mask & AC_ERR_HSM))) {
Akira Iguchi836250062007-01-26 16:27:32 +09005041 ap->ops->irq_on(ap);
Tejun Heoc17ea202006-05-15 20:59:29 +09005042 ata_qc_complete(qc);
5043 } else
5044 ata_port_freeze(ap);
5045 }
5046
Jeff Garzikba6a1302006-06-22 23:46:10 -04005047 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoc17ea202006-05-15 20:59:29 +09005048 } else {
5049 if (likely(!(qc->err_mask & AC_ERR_HSM)))
5050 ata_qc_complete(qc);
5051 else
5052 ata_port_freeze(ap);
5053 }
5054 } else {
5055 if (in_wq) {
Jeff Garzikba6a1302006-06-22 23:46:10 -04005056 spin_lock_irqsave(ap->lock, flags);
Akira Iguchi836250062007-01-26 16:27:32 +09005057 ap->ops->irq_on(ap);
Tejun Heoc17ea202006-05-15 20:59:29 +09005058 ata_qc_complete(qc);
Jeff Garzikba6a1302006-06-22 23:46:10 -04005059 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoc17ea202006-05-15 20:59:29 +09005060 } else
5061 ata_qc_complete(qc);
5062 }
5063}
5064
5065/**
Albert Leebb5cb292006-03-25 17:48:02 +08005066 * ata_hsm_move - move the HSM to the next state.
5067 * @ap: the target ata_port
5068 * @qc: qc on going
5069 * @status: current device status
5070 * @in_wq: 1 if called from workqueue, 0 otherwise
5071 *
5072 * RETURNS:
5073 * 1 when poll next status needed, 0 otherwise.
5074 */
Tejun Heo9a1004d2006-05-31 18:27:52 +09005075int ata_hsm_move(struct ata_port *ap, struct ata_queued_cmd *qc,
5076 u8 status, int in_wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005077{
Albert Leebb5cb292006-03-25 17:48:02 +08005078 unsigned long flags = 0;
5079 int poll_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080
Albert Lee6912ccd2006-03-25 17:45:49 +08005081 WARN_ON((qc->flags & ATA_QCFLAG_ACTIVE) == 0);
Albert Lee0565c262006-02-13 18:55:25 +08005082
Albert Leebb5cb292006-03-25 17:48:02 +08005083 /* Make sure ata_qc_issue_prot() does not throw things
5084 * like DMA polling into the workqueue. Notice that
5085 * in_wq is not equivalent to (qc->tf.flags & ATA_TFLAG_POLLING).
Albert Lee1c848982005-12-05 15:40:15 +08005086 */
Albert Leec234fb02006-03-25 17:58:38 +08005087 WARN_ON(in_wq != ata_hsm_ok_in_wq(ap, qc));
Albert Lee1c848982005-12-05 15:40:15 +08005088
Albert Leee2cec772006-03-25 17:43:49 +08005089fsm_start:
Albert Lee999bb6f2006-03-25 18:07:48 +08005090 DPRINTK("ata%u: protocol %d task_state %d (dev_stat 0x%X)\n",
Tejun Heo44877b42007-02-21 01:06:51 +09005091 ap->print_id, qc->tf.protocol, ap->hsm_task_state, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092
Albert Leee2cec772006-03-25 17:43:49 +08005093 switch (ap->hsm_task_state) {
5094 case HSM_ST_FIRST:
Albert Leebb5cb292006-03-25 17:48:02 +08005095 /* Send first data block or PACKET CDB */
5096
5097 /* If polling, we will stay in the work queue after
5098 * sending the data. Otherwise, interrupt handler
5099 * takes over after sending the data.
5100 */
5101 poll_next = (qc->tf.flags & ATA_TFLAG_POLLING);
5102
Albert Leee2cec772006-03-25 17:43:49 +08005103 /* check device status */
Albert Lee3655d1d2006-05-19 11:43:04 +08005104 if (unlikely((status & ATA_DRQ) == 0)) {
5105 /* handle BSY=0, DRQ=0 as error */
5106 if (likely(status & (ATA_ERR | ATA_DF)))
5107 /* device stops HSM for abort/error */
5108 qc->err_mask |= AC_ERR_DEV;
5109 else
5110 /* HSM violation. Let EH handle this */
5111 qc->err_mask |= AC_ERR_HSM;
5112
Albert Leee2cec772006-03-25 17:43:49 +08005113 ap->hsm_task_state = HSM_ST_ERR;
5114 goto fsm_start;
5115 }
5116
Albert Lee71601952006-03-25 18:11:12 +08005117 /* Device should not ask for data transfer (DRQ=1)
5118 * when it finds something wrong.
Albert Leeeee6c322006-04-01 17:38:43 +08005119 * We ignore DRQ here and stop the HSM by
5120 * changing hsm_task_state to HSM_ST_ERR and
5121 * let the EH abort the command or reset the device.
Albert Lee71601952006-03-25 18:11:12 +08005122 */
5123 if (unlikely(status & (ATA_ERR | ATA_DF))) {
Tejun Heo44877b42007-02-21 01:06:51 +09005124 ata_port_printk(ap, KERN_WARNING, "DRQ=1 with device "
5125 "error, dev_stat 0x%X\n", status);
Albert Lee3655d1d2006-05-19 11:43:04 +08005126 qc->err_mask |= AC_ERR_HSM;
Albert Leeeee6c322006-04-01 17:38:43 +08005127 ap->hsm_task_state = HSM_ST_ERR;
5128 goto fsm_start;
Albert Lee71601952006-03-25 18:11:12 +08005129 }
5130
Albert Leebb5cb292006-03-25 17:48:02 +08005131 /* Send the CDB (atapi) or the first data block (ata pio out).
5132 * During the state transition, interrupt handler shouldn't
5133 * be invoked before the data transfer is complete and
5134 * hsm_task_state is changed. Hence, the following locking.
5135 */
5136 if (in_wq)
Jeff Garzikba6a1302006-06-22 23:46:10 -04005137 spin_lock_irqsave(ap->lock, flags);
Albert Leee2cec772006-03-25 17:43:49 +08005138
Albert Leebb5cb292006-03-25 17:48:02 +08005139 if (qc->tf.protocol == ATA_PROT_PIO) {
5140 /* PIO data out protocol.
5141 * send first data block.
5142 */
5143
5144 /* ata_pio_sectors() might change the state
5145 * to HSM_ST_LAST. so, the state is changed here
5146 * before ata_pio_sectors().
5147 */
5148 ap->hsm_task_state = HSM_ST;
5149 ata_pio_sectors(qc);
Albert Leebb5cb292006-03-25 17:48:02 +08005150 } else
5151 /* send CDB */
5152 atapi_send_cdb(ap, qc);
5153
5154 if (in_wq)
Jeff Garzikba6a1302006-06-22 23:46:10 -04005155 spin_unlock_irqrestore(ap->lock, flags);
Albert Leebb5cb292006-03-25 17:48:02 +08005156
5157 /* if polling, ata_pio_task() handles the rest.
5158 * otherwise, interrupt handler takes over from here.
5159 */
Albert Leee2cec772006-03-25 17:43:49 +08005160 break;
5161
5162 case HSM_ST:
5163 /* complete command or read/write the data register */
5164 if (qc->tf.protocol == ATA_PROT_ATAPI) {
5165 /* ATAPI PIO protocol */
5166 if ((status & ATA_DRQ) == 0) {
Albert Lee3655d1d2006-05-19 11:43:04 +08005167 /* No more data to transfer or device error.
5168 * Device error will be tagged in HSM_ST_LAST.
5169 */
Albert Leee2cec772006-03-25 17:43:49 +08005170 ap->hsm_task_state = HSM_ST_LAST;
5171 goto fsm_start;
5172 }
5173
Albert Lee71601952006-03-25 18:11:12 +08005174 /* Device should not ask for data transfer (DRQ=1)
5175 * when it finds something wrong.
Albert Leeeee6c322006-04-01 17:38:43 +08005176 * We ignore DRQ here and stop the HSM by
5177 * changing hsm_task_state to HSM_ST_ERR and
5178 * let the EH abort the command or reset the device.
Albert Lee71601952006-03-25 18:11:12 +08005179 */
5180 if (unlikely(status & (ATA_ERR | ATA_DF))) {
Tejun Heo44877b42007-02-21 01:06:51 +09005181 ata_port_printk(ap, KERN_WARNING, "DRQ=1 with "
5182 "device error, dev_stat 0x%X\n",
5183 status);
Albert Lee3655d1d2006-05-19 11:43:04 +08005184 qc->err_mask |= AC_ERR_HSM;
Albert Leeeee6c322006-04-01 17:38:43 +08005185 ap->hsm_task_state = HSM_ST_ERR;
5186 goto fsm_start;
Albert Lee71601952006-03-25 18:11:12 +08005187 }
5188
Albert Leee2cec772006-03-25 17:43:49 +08005189 atapi_pio_bytes(qc);
5190
5191 if (unlikely(ap->hsm_task_state == HSM_ST_ERR))
5192 /* bad ireason reported by device */
5193 goto fsm_start;
5194
5195 } else {
5196 /* ATA PIO protocol */
5197 if (unlikely((status & ATA_DRQ) == 0)) {
5198 /* handle BSY=0, DRQ=0 as error */
Albert Lee3655d1d2006-05-19 11:43:04 +08005199 if (likely(status & (ATA_ERR | ATA_DF)))
5200 /* device stops HSM for abort/error */
5201 qc->err_mask |= AC_ERR_DEV;
5202 else
Tejun Heo55a8e2c2006-11-10 18:08:10 +09005203 /* HSM violation. Let EH handle this.
5204 * Phantom devices also trigger this
5205 * condition. Mark hint.
5206 */
5207 qc->err_mask |= AC_ERR_HSM |
5208 AC_ERR_NODEV_HINT;
Albert Lee3655d1d2006-05-19 11:43:04 +08005209
Albert Leee2cec772006-03-25 17:43:49 +08005210 ap->hsm_task_state = HSM_ST_ERR;
5211 goto fsm_start;
5212 }
5213
Albert Leeeee6c322006-04-01 17:38:43 +08005214 /* For PIO reads, some devices may ask for
5215 * data transfer (DRQ=1) alone with ERR=1.
5216 * We respect DRQ here and transfer one
5217 * block of junk data before changing the
5218 * hsm_task_state to HSM_ST_ERR.
5219 *
5220 * For PIO writes, ERR=1 DRQ=1 doesn't make
5221 * sense since the data block has been
5222 * transferred to the device.
Albert Lee71601952006-03-25 18:11:12 +08005223 */
5224 if (unlikely(status & (ATA_ERR | ATA_DF))) {
Albert Lee71601952006-03-25 18:11:12 +08005225 /* data might be corrputed */
5226 qc->err_mask |= AC_ERR_DEV;
Albert Leeeee6c322006-04-01 17:38:43 +08005227
5228 if (!(qc->tf.flags & ATA_TFLAG_WRITE)) {
5229 ata_pio_sectors(qc);
Albert Leeeee6c322006-04-01 17:38:43 +08005230 status = ata_wait_idle(ap);
5231 }
5232
Albert Lee3655d1d2006-05-19 11:43:04 +08005233 if (status & (ATA_BUSY | ATA_DRQ))
5234 qc->err_mask |= AC_ERR_HSM;
5235
Albert Leeeee6c322006-04-01 17:38:43 +08005236 /* ata_pio_sectors() might change the
5237 * state to HSM_ST_LAST. so, the state
5238 * is changed after ata_pio_sectors().
5239 */
5240 ap->hsm_task_state = HSM_ST_ERR;
5241 goto fsm_start;
Albert Lee71601952006-03-25 18:11:12 +08005242 }
5243
Albert Leee2cec772006-03-25 17:43:49 +08005244 ata_pio_sectors(qc);
5245
5246 if (ap->hsm_task_state == HSM_ST_LAST &&
5247 (!(qc->tf.flags & ATA_TFLAG_WRITE))) {
5248 /* all data read */
Albert Lee52a32202006-03-25 18:18:15 +08005249 status = ata_wait_idle(ap);
Albert Leee2cec772006-03-25 17:43:49 +08005250 goto fsm_start;
5251 }
5252 }
5253
Albert Leebb5cb292006-03-25 17:48:02 +08005254 poll_next = 1;
Albert Leee2cec772006-03-25 17:43:49 +08005255 break;
5256
5257 case HSM_ST_LAST:
Albert Lee6912ccd2006-03-25 17:45:49 +08005258 if (unlikely(!ata_ok(status))) {
5259 qc->err_mask |= __ac_err_mask(status);
Albert Leee2cec772006-03-25 17:43:49 +08005260 ap->hsm_task_state = HSM_ST_ERR;
5261 goto fsm_start;
5262 }
5263
5264 /* no more data to transfer */
Albert Lee4332a772006-04-03 17:43:24 +08005265 DPRINTK("ata%u: dev %u command complete, drv_stat 0x%x\n",
Tejun Heo44877b42007-02-21 01:06:51 +09005266 ap->print_id, qc->dev->devno, status);
Albert Leee2cec772006-03-25 17:43:49 +08005267
Albert Lee6912ccd2006-03-25 17:45:49 +08005268 WARN_ON(qc->err_mask);
5269
Albert Leee2cec772006-03-25 17:43:49 +08005270 ap->hsm_task_state = HSM_ST_IDLE;
5271
5272 /* complete taskfile transaction */
Tejun Heoc17ea202006-05-15 20:59:29 +09005273 ata_hsm_qc_complete(qc, in_wq);
Albert Leebb5cb292006-03-25 17:48:02 +08005274
5275 poll_next = 0;
Albert Leee2cec772006-03-25 17:43:49 +08005276 break;
5277
5278 case HSM_ST_ERR:
Albert Leee2cec772006-03-25 17:43:49 +08005279 /* make sure qc->err_mask is available to
5280 * know what's wrong and recover
5281 */
5282 WARN_ON(qc->err_mask == 0);
5283
5284 ap->hsm_task_state = HSM_ST_IDLE;
Albert Leebb5cb292006-03-25 17:48:02 +08005285
Albert Lee999bb6f2006-03-25 18:07:48 +08005286 /* complete taskfile transaction */
Tejun Heoc17ea202006-05-15 20:59:29 +09005287 ata_hsm_qc_complete(qc, in_wq);
Albert Leebb5cb292006-03-25 17:48:02 +08005288
5289 poll_next = 0;
Albert Leee2cec772006-03-25 17:43:49 +08005290 break;
5291 default:
Albert Leebb5cb292006-03-25 17:48:02 +08005292 poll_next = 0;
Albert Lee6912ccd2006-03-25 17:45:49 +08005293 BUG();
Albert Leee2cec772006-03-25 17:43:49 +08005294 }
5295
Albert Leebb5cb292006-03-25 17:48:02 +08005296 return poll_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005297}
5298
David Howells65f27f32006-11-22 14:55:48 +00005299static void ata_pio_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005300{
David Howells65f27f32006-11-22 14:55:48 +00005301 struct ata_port *ap =
5302 container_of(work, struct ata_port, port_task.work);
5303 struct ata_queued_cmd *qc = ap->port_task_data;
Albert Leea1af3732006-03-25 17:50:15 +08005304 u8 status;
5305 int poll_next;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04005306
5307fsm_start:
Albert Leea1af3732006-03-25 17:50:15 +08005308 WARN_ON(ap->hsm_task_state == HSM_ST_IDLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005309
Albert Leea1af3732006-03-25 17:50:15 +08005310 /*
5311 * This is purely heuristic. This is a fast path.
5312 * Sometimes when we enter, BSY will be cleared in
5313 * a chk-status or two. If not, the drive is probably seeking
5314 * or something. Snooze for a couple msecs, then
5315 * chk-status again. If still busy, queue delayed work.
5316 */
5317 status = ata_busy_wait(ap, ATA_BUSY, 5);
5318 if (status & ATA_BUSY) {
5319 msleep(2);
5320 status = ata_busy_wait(ap, ATA_BUSY, 10);
5321 if (status & ATA_BUSY) {
Albert Lee31ce6da2006-04-03 18:31:44 +08005322 ata_port_queue_task(ap, ata_pio_task, qc, ATA_SHORT_PAUSE);
Albert Leea1af3732006-03-25 17:50:15 +08005323 return;
5324 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005325 }
5326
Albert Leea1af3732006-03-25 17:50:15 +08005327 /* move the HSM */
5328 poll_next = ata_hsm_move(ap, qc, status, 1);
5329
5330 /* another command or interrupt handler
5331 * may be running at this point.
5332 */
5333 if (poll_next)
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04005334 goto fsm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005335}
5336
Linus Torvalds1da177e2005-04-16 15:20:36 -07005337/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07005338 * ata_qc_new - Request an available ATA command, for queueing
5339 * @ap: Port associated with device @dev
5340 * @dev: Device from whom we request an available command structure
5341 *
5342 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04005343 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005344 */
5345
5346static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
5347{
5348 struct ata_queued_cmd *qc = NULL;
5349 unsigned int i;
5350
Tejun Heoe3180492006-05-15 20:58:09 +09005351 /* no command while frozen */
Tejun Heob51e9e52006-06-29 01:29:30 +09005352 if (unlikely(ap->pflags & ATA_PFLAG_FROZEN))
Tejun Heoe3180492006-05-15 20:58:09 +09005353 return NULL;
5354
Tejun Heo2ab7db12006-05-15 20:58:02 +09005355 /* the last tag is reserved for internal command. */
5356 for (i = 0; i < ATA_MAX_QUEUE - 1; i++)
Tejun Heo6cec4a32006-05-15 21:03:41 +09005357 if (!test_and_set_bit(i, &ap->qc_allocated)) {
Tejun Heof69499f2006-05-15 20:58:03 +09005358 qc = __ata_qc_from_tag(ap, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005359 break;
5360 }
5361
5362 if (qc)
5363 qc->tag = i;
5364
5365 return qc;
5366}
5367
5368/**
5369 * ata_qc_new_init - Request an available ATA command, and initialize it
Linus Torvalds1da177e2005-04-16 15:20:36 -07005370 * @dev: Device from whom we request an available command structure
5371 *
5372 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04005373 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005374 */
5375
Tejun Heo3373efd2006-05-15 20:57:53 +09005376struct ata_queued_cmd *ata_qc_new_init(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005377{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005378 struct ata_port *ap = dev->link->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005379 struct ata_queued_cmd *qc;
5380
5381 qc = ata_qc_new(ap);
5382 if (qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005383 qc->scsicmd = NULL;
5384 qc->ap = ap;
5385 qc->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005386
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05005387 ata_qc_reinit(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005388 }
5389
5390 return qc;
5391}
5392
Linus Torvalds1da177e2005-04-16 15:20:36 -07005393/**
5394 * ata_qc_free - free unused ata_queued_cmd
5395 * @qc: Command to complete
5396 *
5397 * Designed to free unused ata_queued_cmd object
5398 * in case something prevents using it.
5399 *
5400 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04005401 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005402 */
5403void ata_qc_free(struct ata_queued_cmd *qc)
5404{
Tejun Heo4ba946e2006-01-23 13:09:36 +09005405 struct ata_port *ap = qc->ap;
5406 unsigned int tag;
5407
Tejun Heoa46314742006-02-11 19:11:13 +09005408 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005409
Tejun Heo4ba946e2006-01-23 13:09:36 +09005410 qc->flags = 0;
5411 tag = qc->tag;
5412 if (likely(ata_tag_valid(tag))) {
Tejun Heo4ba946e2006-01-23 13:09:36 +09005413 qc->tag = ATA_TAG_POISON;
Tejun Heo6cec4a32006-05-15 21:03:41 +09005414 clear_bit(tag, &ap->qc_allocated);
Tejun Heo4ba946e2006-01-23 13:09:36 +09005415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005416}
5417
Tejun Heo76014422006-02-11 15:13:49 +09005418void __ata_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005419{
Tejun Heodedaf2b2006-05-15 21:03:43 +09005420 struct ata_port *ap = qc->ap;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005421 struct ata_link *link = qc->dev->link;
Tejun Heodedaf2b2006-05-15 21:03:43 +09005422
Tejun Heoa46314742006-02-11 19:11:13 +09005423 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
5424 WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07005425
5426 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
5427 ata_sg_clean(qc);
5428
Tejun Heo7401abf2006-05-15 20:57:32 +09005429 /* command should be marked inactive atomically with qc completion */
Tejun Heoda917d62007-09-23 13:14:12 +09005430 if (qc->tf.protocol == ATA_PROT_NCQ) {
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005431 link->sactive &= ~(1 << qc->tag);
Tejun Heoda917d62007-09-23 13:14:12 +09005432 if (!link->sactive)
5433 ap->nr_active_links--;
5434 } else {
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005435 link->active_tag = ATA_TAG_POISON;
Tejun Heoda917d62007-09-23 13:14:12 +09005436 ap->nr_active_links--;
5437 }
5438
5439 /* clear exclusive status */
5440 if (unlikely(qc->flags & ATA_QCFLAG_CLEAR_EXCL &&
5441 ap->excl_link == link))
5442 ap->excl_link = NULL;
Tejun Heo7401abf2006-05-15 20:57:32 +09005443
Albert Lee3f3791d2005-08-16 14:25:38 +08005444 /* atapi: mark qc as inactive to prevent the interrupt handler
5445 * from completing the command twice later, before the error handler
5446 * is called. (when rc != 0 and atapi request sense is needed)
5447 */
5448 qc->flags &= ~ATA_QCFLAG_ACTIVE;
Tejun Heodedaf2b2006-05-15 21:03:43 +09005449 ap->qc_active &= ~(1 << qc->tag);
Albert Lee3f3791d2005-08-16 14:25:38 +08005450
Linus Torvalds1da177e2005-04-16 15:20:36 -07005451 /* call completion callback */
Tejun Heo77853bf2006-01-23 13:09:36 +09005452 qc->complete_fn(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005453}
5454
Tejun Heo39599a52006-11-14 22:37:35 +09005455static void fill_result_tf(struct ata_queued_cmd *qc)
5456{
5457 struct ata_port *ap = qc->ap;
5458
Tejun Heo39599a52006-11-14 22:37:35 +09005459 qc->result_tf.flags = qc->tf.flags;
Mark Lord4742d542007-04-02 16:20:35 -04005460 ap->ops->tf_read(ap, &qc->result_tf);
Tejun Heo39599a52006-11-14 22:37:35 +09005461}
5462
Tejun Heof686bcb2006-05-15 20:58:05 +09005463/**
5464 * ata_qc_complete - Complete an active ATA command
5465 * @qc: Command to complete
5466 * @err_mask: ATA Status register contents
5467 *
5468 * Indicate to the mid and upper layers that an ATA
5469 * command has completed, with either an ok or not-ok status.
5470 *
5471 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04005472 * spin_lock_irqsave(host lock)
Tejun Heof686bcb2006-05-15 20:58:05 +09005473 */
5474void ata_qc_complete(struct ata_queued_cmd *qc)
5475{
5476 struct ata_port *ap = qc->ap;
5477
5478 /* XXX: New EH and old EH use different mechanisms to
5479 * synchronize EH with regular execution path.
5480 *
5481 * In new EH, a failed qc is marked with ATA_QCFLAG_FAILED.
5482 * Normal execution path is responsible for not accessing a
5483 * failed qc. libata core enforces the rule by returning NULL
5484 * from ata_qc_from_tag() for failed qcs.
5485 *
5486 * Old EH depends on ata_qc_complete() nullifying completion
5487 * requests if ATA_QCFLAG_EH_SCHEDULED is set. Old EH does
5488 * not synchronize with interrupt handler. Only PIO task is
5489 * taken care of.
5490 */
5491 if (ap->ops->error_handler) {
Tejun Heob51e9e52006-06-29 01:29:30 +09005492 WARN_ON(ap->pflags & ATA_PFLAG_FROZEN);
Tejun Heof686bcb2006-05-15 20:58:05 +09005493
5494 if (unlikely(qc->err_mask))
5495 qc->flags |= ATA_QCFLAG_FAILED;
5496
5497 if (unlikely(qc->flags & ATA_QCFLAG_FAILED)) {
5498 if (!ata_tag_internal(qc->tag)) {
5499 /* always fill result TF for failed qc */
Tejun Heo39599a52006-11-14 22:37:35 +09005500 fill_result_tf(qc);
Tejun Heof686bcb2006-05-15 20:58:05 +09005501 ata_qc_schedule_eh(qc);
5502 return;
5503 }
5504 }
5505
5506 /* read result TF if requested */
5507 if (qc->flags & ATA_QCFLAG_RESULT_TF)
Tejun Heo39599a52006-11-14 22:37:35 +09005508 fill_result_tf(qc);
Tejun Heof686bcb2006-05-15 20:58:05 +09005509
5510 __ata_qc_complete(qc);
5511 } else {
5512 if (qc->flags & ATA_QCFLAG_EH_SCHEDULED)
5513 return;
5514
5515 /* read result TF if failed or requested */
5516 if (qc->err_mask || qc->flags & ATA_QCFLAG_RESULT_TF)
Tejun Heo39599a52006-11-14 22:37:35 +09005517 fill_result_tf(qc);
Tejun Heof686bcb2006-05-15 20:58:05 +09005518
5519 __ata_qc_complete(qc);
5520 }
5521}
5522
Tejun Heodedaf2b2006-05-15 21:03:43 +09005523/**
5524 * ata_qc_complete_multiple - Complete multiple qcs successfully
5525 * @ap: port in question
5526 * @qc_active: new qc_active mask
5527 * @finish_qc: LLDD callback invoked before completing a qc
5528 *
5529 * Complete in-flight commands. This functions is meant to be
5530 * called from low-level driver's interrupt routine to complete
5531 * requests normally. ap->qc_active and @qc_active is compared
5532 * and commands are completed accordingly.
5533 *
5534 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04005535 * spin_lock_irqsave(host lock)
Tejun Heodedaf2b2006-05-15 21:03:43 +09005536 *
5537 * RETURNS:
5538 * Number of completed commands on success, -errno otherwise.
5539 */
5540int ata_qc_complete_multiple(struct ata_port *ap, u32 qc_active,
5541 void (*finish_qc)(struct ata_queued_cmd *))
5542{
5543 int nr_done = 0;
5544 u32 done_mask;
5545 int i;
5546
5547 done_mask = ap->qc_active ^ qc_active;
5548
5549 if (unlikely(done_mask & qc_active)) {
5550 ata_port_printk(ap, KERN_ERR, "illegal qc_active transition "
5551 "(%08x->%08x)\n", ap->qc_active, qc_active);
5552 return -EINVAL;
5553 }
5554
5555 for (i = 0; i < ATA_MAX_QUEUE; i++) {
5556 struct ata_queued_cmd *qc;
5557
5558 if (!(done_mask & (1 << i)))
5559 continue;
5560
5561 if ((qc = ata_qc_from_tag(ap, i))) {
5562 if (finish_qc)
5563 finish_qc(qc);
5564 ata_qc_complete(qc);
5565 nr_done++;
5566 }
5567 }
5568
5569 return nr_done;
5570}
5571
Linus Torvalds1da177e2005-04-16 15:20:36 -07005572static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
5573{
5574 struct ata_port *ap = qc->ap;
5575
5576 switch (qc->tf.protocol) {
Tejun Heo3dc1d882006-05-15 21:03:45 +09005577 case ATA_PROT_NCQ:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005578 case ATA_PROT_DMA:
5579 case ATA_PROT_ATAPI_DMA:
5580 return 1;
5581
5582 case ATA_PROT_ATAPI:
5583 case ATA_PROT_PIO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005584 if (ap->flags & ATA_FLAG_PIO_DMA)
5585 return 1;
5586
5587 /* fall through */
5588
5589 default:
5590 return 0;
5591 }
5592
5593 /* never reached */
5594}
5595
5596/**
5597 * ata_qc_issue - issue taskfile to device
5598 * @qc: command to issue to device
5599 *
5600 * Prepare an ATA command to submission to device.
5601 * This includes mapping the data into a DMA-able
5602 * area, filling in the S/G table, and finally
5603 * writing the taskfile to hardware, starting the command.
5604 *
5605 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04005606 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005607 */
Tejun Heo8e0e6942006-03-31 20:41:11 +09005608void ata_qc_issue(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005609{
5610 struct ata_port *ap = qc->ap;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005611 struct ata_link *link = qc->dev->link;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005612
Tejun Heodedaf2b2006-05-15 21:03:43 +09005613 /* Make sure only one non-NCQ command is outstanding. The
5614 * check is skipped for old EH because it reuses active qc to
5615 * request ATAPI sense.
5616 */
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005617 WARN_ON(ap->ops->error_handler && ata_tag_valid(link->active_tag));
Tejun Heodedaf2b2006-05-15 21:03:43 +09005618
5619 if (qc->tf.protocol == ATA_PROT_NCQ) {
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005620 WARN_ON(link->sactive & (1 << qc->tag));
Tejun Heoda917d62007-09-23 13:14:12 +09005621
5622 if (!link->sactive)
5623 ap->nr_active_links++;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005624 link->sactive |= 1 << qc->tag;
Tejun Heodedaf2b2006-05-15 21:03:43 +09005625 } else {
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005626 WARN_ON(link->sactive);
Tejun Heoda917d62007-09-23 13:14:12 +09005627
5628 ap->nr_active_links++;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005629 link->active_tag = qc->tag;
Tejun Heodedaf2b2006-05-15 21:03:43 +09005630 }
5631
Tejun Heoe4a70e72006-03-31 20:36:47 +09005632 qc->flags |= ATA_QCFLAG_ACTIVE;
Tejun Heodedaf2b2006-05-15 21:03:43 +09005633 ap->qc_active |= 1 << qc->tag;
Tejun Heoe4a70e72006-03-31 20:36:47 +09005634
Linus Torvalds1da177e2005-04-16 15:20:36 -07005635 if (ata_should_dma_map(qc)) {
5636 if (qc->flags & ATA_QCFLAG_SG) {
5637 if (ata_sg_setup(qc))
Tejun Heo8e436af2006-01-23 13:09:36 +09005638 goto sg_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005639 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
5640 if (ata_sg_setup_one(qc))
Tejun Heo8e436af2006-01-23 13:09:36 +09005641 goto sg_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005642 }
5643 } else {
5644 qc->flags &= ~ATA_QCFLAG_DMAMAP;
5645 }
5646
5647 ap->ops->qc_prep(qc);
5648
Tejun Heo8e0e6942006-03-31 20:41:11 +09005649 qc->err_mask |= ap->ops->qc_issue(qc);
5650 if (unlikely(qc->err_mask))
5651 goto err;
5652 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005653
Tejun Heo8e436af2006-01-23 13:09:36 +09005654sg_err:
5655 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Tejun Heo8e0e6942006-03-31 20:41:11 +09005656 qc->err_mask |= AC_ERR_SYSTEM;
5657err:
5658 ata_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005659}
5660
5661/**
5662 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
5663 * @qc: command to issue to device
5664 *
5665 * Using various libata functions and hooks, this function
5666 * starts an ATA command. ATA commands are grouped into
5667 * classes called "protocols", and issuing each type of protocol
5668 * is slightly different.
5669 *
Edward Falk0baab862005-06-02 18:17:13 -04005670 * May be used as the qc_issue() entry in ata_port_operations.
5671 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005672 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04005673 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005674 *
5675 * RETURNS:
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09005676 * Zero on success, AC_ERR_* mask on failure
Linus Torvalds1da177e2005-04-16 15:20:36 -07005677 */
5678
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09005679unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005680{
5681 struct ata_port *ap = qc->ap;
5682
Albert Leee50362e2005-09-27 17:39:50 +08005683 /* Use polling pio if the LLD doesn't handle
5684 * interrupt driven pio and atapi CDB interrupt.
5685 */
5686 if (ap->flags & ATA_FLAG_PIO_POLLING) {
5687 switch (qc->tf.protocol) {
5688 case ATA_PROT_PIO:
Albert Leee3472cb2006-12-07 11:37:58 +08005689 case ATA_PROT_NODATA:
Albert Leee50362e2005-09-27 17:39:50 +08005690 case ATA_PROT_ATAPI:
5691 case ATA_PROT_ATAPI_NODATA:
5692 qc->tf.flags |= ATA_TFLAG_POLLING;
5693 break;
5694 case ATA_PROT_ATAPI_DMA:
5695 if (qc->dev->flags & ATA_DFLAG_CDB_INTR)
Albert Lee3a778272006-06-22 13:00:25 +08005696 /* see ata_dma_blacklisted() */
Albert Leee50362e2005-09-27 17:39:50 +08005697 BUG();
5698 break;
5699 default:
5700 break;
5701 }
5702 }
5703
Albert Lee312f7da2005-09-27 17:38:03 +08005704 /* select the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005705 ata_dev_select(ap, qc->dev->devno, 1, 0);
5706
Albert Lee312f7da2005-09-27 17:38:03 +08005707 /* start the command */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005708 switch (qc->tf.protocol) {
5709 case ATA_PROT_NODATA:
Albert Lee312f7da2005-09-27 17:38:03 +08005710 if (qc->tf.flags & ATA_TFLAG_POLLING)
5711 ata_qc_set_polling(qc);
5712
Jeff Garzike5338252005-10-30 21:37:17 -05005713 ata_tf_to_host(ap, &qc->tf);
Albert Lee312f7da2005-09-27 17:38:03 +08005714 ap->hsm_task_state = HSM_ST_LAST;
5715
5716 if (qc->tf.flags & ATA_TFLAG_POLLING)
Albert Lee31ce6da2006-04-03 18:31:44 +08005717 ata_port_queue_task(ap, ata_pio_task, qc, 0);
Albert Lee312f7da2005-09-27 17:38:03 +08005718
Linus Torvalds1da177e2005-04-16 15:20:36 -07005719 break;
5720
5721 case ATA_PROT_DMA:
Jeff Garzik587005d2006-02-11 18:17:32 -05005722 WARN_ON(qc->tf.flags & ATA_TFLAG_POLLING);
Albert Lee312f7da2005-09-27 17:38:03 +08005723
Linus Torvalds1da177e2005-04-16 15:20:36 -07005724 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
5725 ap->ops->bmdma_setup(qc); /* set up bmdma */
5726 ap->ops->bmdma_start(qc); /* initiate bmdma */
Albert Lee312f7da2005-09-27 17:38:03 +08005727 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005728 break;
5729
Albert Lee312f7da2005-09-27 17:38:03 +08005730 case ATA_PROT_PIO:
5731 if (qc->tf.flags & ATA_TFLAG_POLLING)
5732 ata_qc_set_polling(qc);
5733
Jeff Garzike5338252005-10-30 21:37:17 -05005734 ata_tf_to_host(ap, &qc->tf);
Albert Lee312f7da2005-09-27 17:38:03 +08005735
Albert Lee54f00382005-09-30 19:14:19 +08005736 if (qc->tf.flags & ATA_TFLAG_WRITE) {
5737 /* PIO data out protocol */
5738 ap->hsm_task_state = HSM_ST_FIRST;
Albert Lee31ce6da2006-04-03 18:31:44 +08005739 ata_port_queue_task(ap, ata_pio_task, qc, 0);
Albert Lee54f00382005-09-30 19:14:19 +08005740
5741 /* always send first data block using
Albert Leee27486d2005-11-01 19:24:49 +08005742 * the ata_pio_task() codepath.
Albert Lee54f00382005-09-30 19:14:19 +08005743 */
Albert Lee312f7da2005-09-27 17:38:03 +08005744 } else {
Albert Lee54f00382005-09-30 19:14:19 +08005745 /* PIO data in protocol */
5746 ap->hsm_task_state = HSM_ST;
Albert Lee312f7da2005-09-27 17:38:03 +08005747
Albert Lee54f00382005-09-30 19:14:19 +08005748 if (qc->tf.flags & ATA_TFLAG_POLLING)
Albert Lee31ce6da2006-04-03 18:31:44 +08005749 ata_port_queue_task(ap, ata_pio_task, qc, 0);
Albert Lee312f7da2005-09-27 17:38:03 +08005750
Albert Lee54f00382005-09-30 19:14:19 +08005751 /* if polling, ata_pio_task() handles the rest.
5752 * otherwise, interrupt handler takes over from here.
5753 */
Albert Lee312f7da2005-09-27 17:38:03 +08005754 }
5755
Linus Torvalds1da177e2005-04-16 15:20:36 -07005756 break;
5757
5758 case ATA_PROT_ATAPI:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005759 case ATA_PROT_ATAPI_NODATA:
Albert Lee312f7da2005-09-27 17:38:03 +08005760 if (qc->tf.flags & ATA_TFLAG_POLLING)
5761 ata_qc_set_polling(qc);
5762
Jeff Garzike5338252005-10-30 21:37:17 -05005763 ata_tf_to_host(ap, &qc->tf);
Jeff Garzikf6ef65e2006-01-27 02:45:00 -05005764
Albert Lee312f7da2005-09-27 17:38:03 +08005765 ap->hsm_task_state = HSM_ST_FIRST;
5766
5767 /* send cdb by polling if no cdb interrupt */
5768 if ((!(qc->dev->flags & ATA_DFLAG_CDB_INTR)) ||
5769 (qc->tf.flags & ATA_TFLAG_POLLING))
Albert Lee31ce6da2006-04-03 18:31:44 +08005770 ata_port_queue_task(ap, ata_pio_task, qc, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005771 break;
5772
5773 case ATA_PROT_ATAPI_DMA:
Jeff Garzik587005d2006-02-11 18:17:32 -05005774 WARN_ON(qc->tf.flags & ATA_TFLAG_POLLING);
Albert Lee312f7da2005-09-27 17:38:03 +08005775
Linus Torvalds1da177e2005-04-16 15:20:36 -07005776 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
5777 ap->ops->bmdma_setup(qc); /* set up bmdma */
Albert Lee312f7da2005-09-27 17:38:03 +08005778 ap->hsm_task_state = HSM_ST_FIRST;
5779
5780 /* send cdb by polling if no cdb interrupt */
5781 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
Albert Lee31ce6da2006-04-03 18:31:44 +08005782 ata_port_queue_task(ap, ata_pio_task, qc, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005783 break;
5784
5785 default:
5786 WARN_ON(1);
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09005787 return AC_ERR_SYSTEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005788 }
5789
5790 return 0;
5791}
5792
5793/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07005794 * ata_host_intr - Handle host interrupt for given (port, task)
5795 * @ap: Port on which interrupt arrived (possibly...)
5796 * @qc: Taskfile currently active in engine
5797 *
5798 * Handle host interrupt for given queued command. Currently,
5799 * only DMA interrupts are handled. All other commands are
5800 * handled via polling with interrupts disabled (nIEN bit).
5801 *
5802 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04005803 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005804 *
5805 * RETURNS:
5806 * One if interrupt was handled, zero if not (shared irq).
5807 */
5808
5809inline unsigned int ata_host_intr (struct ata_port *ap,
5810 struct ata_queued_cmd *qc)
5811{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005812 struct ata_eh_info *ehi = &ap->link.eh_info;
Albert Lee312f7da2005-09-27 17:38:03 +08005813 u8 status, host_stat = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005814
Albert Lee312f7da2005-09-27 17:38:03 +08005815 VPRINTK("ata%u: protocol %d task_state %d\n",
Tejun Heo44877b42007-02-21 01:06:51 +09005816 ap->print_id, qc->tf.protocol, ap->hsm_task_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005817
Albert Lee312f7da2005-09-27 17:38:03 +08005818 /* Check whether we are expecting interrupt in this state */
5819 switch (ap->hsm_task_state) {
5820 case HSM_ST_FIRST:
Albert Lee6912ccd2006-03-25 17:45:49 +08005821 /* Some pre-ATAPI-4 devices assert INTRQ
5822 * at this state when ready to receive CDB.
5823 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005824
Albert Lee312f7da2005-09-27 17:38:03 +08005825 /* Check the ATA_DFLAG_CDB_INTR flag is enough here.
5826 * The flag was turned on only for atapi devices.
5827 * No need to check is_atapi_taskfile(&qc->tf) again.
5828 */
5829 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005830 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005831 break;
Albert Lee312f7da2005-09-27 17:38:03 +08005832 case HSM_ST_LAST:
5833 if (qc->tf.protocol == ATA_PROT_DMA ||
5834 qc->tf.protocol == ATA_PROT_ATAPI_DMA) {
5835 /* check status of DMA engine */
5836 host_stat = ap->ops->bmdma_status(ap);
Tejun Heo44877b42007-02-21 01:06:51 +09005837 VPRINTK("ata%u: host_stat 0x%X\n",
5838 ap->print_id, host_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005839
Albert Lee312f7da2005-09-27 17:38:03 +08005840 /* if it's not our irq... */
5841 if (!(host_stat & ATA_DMA_INTR))
5842 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005843
Albert Lee312f7da2005-09-27 17:38:03 +08005844 /* before we do anything else, clear DMA-Start bit */
5845 ap->ops->bmdma_stop(qc);
Albert Leea4f16612005-12-26 16:40:53 +08005846
5847 if (unlikely(host_stat & ATA_DMA_ERR)) {
5848 /* error when transfering data to/from memory */
5849 qc->err_mask |= AC_ERR_HOST_BUS;
5850 ap->hsm_task_state = HSM_ST_ERR;
5851 }
Albert Lee312f7da2005-09-27 17:38:03 +08005852 }
5853 break;
5854 case HSM_ST:
5855 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005856 default:
5857 goto idle_irq;
5858 }
5859
Albert Lee312f7da2005-09-27 17:38:03 +08005860 /* check altstatus */
5861 status = ata_altstatus(ap);
5862 if (status & ATA_BUSY)
5863 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005864
Albert Lee312f7da2005-09-27 17:38:03 +08005865 /* check main status, clearing INTRQ */
5866 status = ata_chk_status(ap);
5867 if (unlikely(status & ATA_BUSY))
5868 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005869
Albert Lee312f7da2005-09-27 17:38:03 +08005870 /* ack bmdma irq events */
5871 ap->ops->irq_clear(ap);
5872
Albert Leebb5cb292006-03-25 17:48:02 +08005873 ata_hsm_move(ap, qc, status, 0);
Tejun Heoea547632006-11-17 12:06:21 +09005874
5875 if (unlikely(qc->err_mask) && (qc->tf.protocol == ATA_PROT_DMA ||
5876 qc->tf.protocol == ATA_PROT_ATAPI_DMA))
5877 ata_ehi_push_desc(ehi, "BMDMA stat 0x%x", host_stat);
5878
Linus Torvalds1da177e2005-04-16 15:20:36 -07005879 return 1; /* irq handled */
5880
5881idle_irq:
5882 ap->stats.idle_irq++;
5883
5884#ifdef ATA_IRQ_TRAP
5885 if ((ap->stats.idle_irq % 1000) == 0) {
Jeff Garzik6d32d302007-08-15 05:38:46 -04005886 ata_chk_status(ap);
5887 ap->ops->irq_clear(ap);
Tejun Heof15a1da2006-05-15 20:57:56 +09005888 ata_port_printk(ap, KERN_WARNING, "irq trap\n");
Alan Cox23cfce82006-03-21 16:06:53 +00005889 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005890 }
5891#endif
5892 return 0; /* irq not handled */
5893}
5894
5895/**
5896 * ata_interrupt - Default ATA host interrupt handler
Jeff Garzik0cba6322005-05-30 19:49:12 -04005897 * @irq: irq line (unused)
Jeff Garzikcca39742006-08-24 03:19:22 -04005898 * @dev_instance: pointer to our ata_host information structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07005899 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04005900 * Default interrupt handler for PCI IDE devices. Calls
5901 * ata_host_intr() for each port that is not disabled.
5902 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005903 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04005904 * Obtains host lock during operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005905 *
5906 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04005907 * IRQ_NONE or IRQ_HANDLED.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005908 */
5909
David Howells7d12e782006-10-05 14:55:46 +01005910irqreturn_t ata_interrupt (int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005911{
Jeff Garzikcca39742006-08-24 03:19:22 -04005912 struct ata_host *host = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005913 unsigned int i;
5914 unsigned int handled = 0;
5915 unsigned long flags;
5916
5917 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
Jeff Garzikcca39742006-08-24 03:19:22 -04005918 spin_lock_irqsave(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005919
Jeff Garzikcca39742006-08-24 03:19:22 -04005920 for (i = 0; i < host->n_ports; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005921 struct ata_port *ap;
5922
Jeff Garzikcca39742006-08-24 03:19:22 -04005923 ap = host->ports[i];
Tejun Heoc1389502005-08-22 14:59:24 +09005924 if (ap &&
Jeff Garzik029f5462006-04-02 10:30:40 -04005925 !(ap->flags & ATA_FLAG_DISABLED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005926 struct ata_queued_cmd *qc;
5927
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005928 qc = ata_qc_from_tag(ap, ap->link.active_tag);
Albert Lee312f7da2005-09-27 17:38:03 +08005929 if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING)) &&
Albert Lee21b1ed72005-04-29 17:34:59 +08005930 (qc->flags & ATA_QCFLAG_ACTIVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005931 handled |= ata_host_intr(ap, qc);
5932 }
5933 }
5934
Jeff Garzikcca39742006-08-24 03:19:22 -04005935 spin_unlock_irqrestore(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005936
5937 return IRQ_RETVAL(handled);
5938}
5939
Tejun Heo34bf2172006-05-15 20:57:46 +09005940/**
5941 * sata_scr_valid - test whether SCRs are accessible
Tejun Heo936fd732007-08-06 18:36:23 +09005942 * @link: ATA link to test SCR accessibility for
Tejun Heo34bf2172006-05-15 20:57:46 +09005943 *
Tejun Heo936fd732007-08-06 18:36:23 +09005944 * Test whether SCRs are accessible for @link.
Tejun Heo34bf2172006-05-15 20:57:46 +09005945 *
5946 * LOCKING:
5947 * None.
5948 *
5949 * RETURNS:
5950 * 1 if SCRs are accessible, 0 otherwise.
5951 */
Tejun Heo936fd732007-08-06 18:36:23 +09005952int sata_scr_valid(struct ata_link *link)
Tejun Heo34bf2172006-05-15 20:57:46 +09005953{
Tejun Heo936fd732007-08-06 18:36:23 +09005954 struct ata_port *ap = link->ap;
5955
Tejun Heoa16abc02007-05-21 18:33:47 +02005956 return (ap->flags & ATA_FLAG_SATA) && ap->ops->scr_read;
Tejun Heo34bf2172006-05-15 20:57:46 +09005957}
5958
5959/**
5960 * sata_scr_read - read SCR register of the specified port
Tejun Heo936fd732007-08-06 18:36:23 +09005961 * @link: ATA link to read SCR for
Tejun Heo34bf2172006-05-15 20:57:46 +09005962 * @reg: SCR to read
5963 * @val: Place to store read value
5964 *
Tejun Heo936fd732007-08-06 18:36:23 +09005965 * Read SCR register @reg of @link into *@val. This function is
Tejun Heo34bf2172006-05-15 20:57:46 +09005966 * guaranteed to succeed if the cable type of the port is SATA
5967 * and the port implements ->scr_read.
5968 *
5969 * LOCKING:
5970 * None.
5971 *
5972 * RETURNS:
5973 * 0 on success, negative errno on failure.
5974 */
Tejun Heo936fd732007-08-06 18:36:23 +09005975int sata_scr_read(struct ata_link *link, int reg, u32 *val)
Tejun Heo34bf2172006-05-15 20:57:46 +09005976{
Tejun Heo936fd732007-08-06 18:36:23 +09005977 struct ata_port *ap = link->ap;
5978
5979 if (sata_scr_valid(link))
Tejun Heoda3dbb12007-07-16 14:29:40 +09005980 return ap->ops->scr_read(ap, reg, val);
Tejun Heo34bf2172006-05-15 20:57:46 +09005981 return -EOPNOTSUPP;
5982}
5983
5984/**
5985 * sata_scr_write - write SCR register of the specified port
Tejun Heo936fd732007-08-06 18:36:23 +09005986 * @link: ATA link to write SCR for
Tejun Heo34bf2172006-05-15 20:57:46 +09005987 * @reg: SCR to write
5988 * @val: value to write
5989 *
Tejun Heo936fd732007-08-06 18:36:23 +09005990 * Write @val to SCR register @reg of @link. This function is
Tejun Heo34bf2172006-05-15 20:57:46 +09005991 * guaranteed to succeed if the cable type of the port is SATA
5992 * and the port implements ->scr_read.
5993 *
5994 * LOCKING:
5995 * None.
5996 *
5997 * RETURNS:
5998 * 0 on success, negative errno on failure.
5999 */
Tejun Heo936fd732007-08-06 18:36:23 +09006000int sata_scr_write(struct ata_link *link, int reg, u32 val)
Tejun Heo34bf2172006-05-15 20:57:46 +09006001{
Tejun Heo936fd732007-08-06 18:36:23 +09006002 struct ata_port *ap = link->ap;
6003
6004 if (sata_scr_valid(link))
Tejun Heoda3dbb12007-07-16 14:29:40 +09006005 return ap->ops->scr_write(ap, reg, val);
Tejun Heo34bf2172006-05-15 20:57:46 +09006006 return -EOPNOTSUPP;
6007}
6008
6009/**
6010 * sata_scr_write_flush - write SCR register of the specified port and flush
Tejun Heo936fd732007-08-06 18:36:23 +09006011 * @link: ATA link to write SCR for
Tejun Heo34bf2172006-05-15 20:57:46 +09006012 * @reg: SCR to write
6013 * @val: value to write
6014 *
6015 * This function is identical to sata_scr_write() except that this
6016 * function performs flush after writing to the register.
6017 *
6018 * LOCKING:
6019 * None.
6020 *
6021 * RETURNS:
6022 * 0 on success, negative errno on failure.
6023 */
Tejun Heo936fd732007-08-06 18:36:23 +09006024int sata_scr_write_flush(struct ata_link *link, int reg, u32 val)
Tejun Heo34bf2172006-05-15 20:57:46 +09006025{
Tejun Heo936fd732007-08-06 18:36:23 +09006026 struct ata_port *ap = link->ap;
Tejun Heoda3dbb12007-07-16 14:29:40 +09006027 int rc;
6028
Tejun Heo936fd732007-08-06 18:36:23 +09006029 if (sata_scr_valid(link)) {
Tejun Heoda3dbb12007-07-16 14:29:40 +09006030 rc = ap->ops->scr_write(ap, reg, val);
6031 if (rc == 0)
6032 rc = ap->ops->scr_read(ap, reg, &val);
6033 return rc;
Tejun Heo34bf2172006-05-15 20:57:46 +09006034 }
6035 return -EOPNOTSUPP;
6036}
6037
6038/**
Tejun Heo936fd732007-08-06 18:36:23 +09006039 * ata_link_online - test whether the given link is online
6040 * @link: ATA link to test
Tejun Heo34bf2172006-05-15 20:57:46 +09006041 *
Tejun Heo936fd732007-08-06 18:36:23 +09006042 * Test whether @link is online. Note that this function returns
6043 * 0 if online status of @link cannot be obtained, so
6044 * ata_link_online(link) != !ata_link_offline(link).
Tejun Heo34bf2172006-05-15 20:57:46 +09006045 *
6046 * LOCKING:
6047 * None.
6048 *
6049 * RETURNS:
6050 * 1 if the port online status is available and online.
6051 */
Tejun Heo936fd732007-08-06 18:36:23 +09006052int ata_link_online(struct ata_link *link)
Tejun Heo34bf2172006-05-15 20:57:46 +09006053{
6054 u32 sstatus;
6055
Tejun Heo936fd732007-08-06 18:36:23 +09006056 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
6057 (sstatus & 0xf) == 0x3)
Tejun Heo34bf2172006-05-15 20:57:46 +09006058 return 1;
6059 return 0;
6060}
6061
6062/**
Tejun Heo936fd732007-08-06 18:36:23 +09006063 * ata_link_offline - test whether the given link is offline
6064 * @link: ATA link to test
Tejun Heo34bf2172006-05-15 20:57:46 +09006065 *
Tejun Heo936fd732007-08-06 18:36:23 +09006066 * Test whether @link is offline. Note that this function
6067 * returns 0 if offline status of @link cannot be obtained, so
6068 * ata_link_online(link) != !ata_link_offline(link).
Tejun Heo34bf2172006-05-15 20:57:46 +09006069 *
6070 * LOCKING:
6071 * None.
6072 *
6073 * RETURNS:
6074 * 1 if the port offline status is available and offline.
6075 */
Tejun Heo936fd732007-08-06 18:36:23 +09006076int ata_link_offline(struct ata_link *link)
Tejun Heo34bf2172006-05-15 20:57:46 +09006077{
6078 u32 sstatus;
6079
Tejun Heo936fd732007-08-06 18:36:23 +09006080 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
6081 (sstatus & 0xf) != 0x3)
Tejun Heo34bf2172006-05-15 20:57:46 +09006082 return 1;
6083 return 0;
6084}
Edward Falk0baab862005-06-02 18:17:13 -04006085
Tejun Heo77b08fb2006-06-24 20:30:19 +09006086int ata_flush_cache(struct ata_device *dev)
Jens Axboe9b847542006-01-06 09:28:07 +01006087{
Tejun Heo977e6b92006-06-24 20:30:19 +09006088 unsigned int err_mask;
Jens Axboe9b847542006-01-06 09:28:07 +01006089 u8 cmd;
6090
6091 if (!ata_try_flush_cache(dev))
6092 return 0;
6093
Tejun Heo6fc49ad2006-11-11 20:10:45 +09006094 if (dev->flags & ATA_DFLAG_FLUSH_EXT)
Jens Axboe9b847542006-01-06 09:28:07 +01006095 cmd = ATA_CMD_FLUSH_EXT;
6096 else
6097 cmd = ATA_CMD_FLUSH;
6098
Alan Cox4f343372007-08-08 14:30:31 +01006099 /* This is wrong. On a failed flush we get back the LBA of the lost
6100 sector and we should (assuming it wasn't aborted as unknown) issue
6101 a further flush command to continue the writeback until it
6102 does not error */
Tejun Heo977e6b92006-06-24 20:30:19 +09006103 err_mask = ata_do_simple_cmd(dev, cmd);
6104 if (err_mask) {
6105 ata_dev_printk(dev, KERN_ERR, "failed to flush cache\n");
6106 return -EIO;
6107 }
6108
6109 return 0;
Jens Axboe9b847542006-01-06 09:28:07 +01006110}
6111
Tejun Heo6ffa01d2007-03-02 17:32:47 +09006112#ifdef CONFIG_PM
Jeff Garzikcca39742006-08-24 03:19:22 -04006113static int ata_host_request_pm(struct ata_host *host, pm_message_t mesg,
6114 unsigned int action, unsigned int ehi_flags,
6115 int wait)
Tejun Heo500530f2006-07-03 16:07:27 +09006116{
6117 unsigned long flags;
6118 int i, rc;
6119
Jeff Garzikcca39742006-08-24 03:19:22 -04006120 for (i = 0; i < host->n_ports; i++) {
6121 struct ata_port *ap = host->ports[i];
Tejun Heoe3667eb2007-08-06 18:36:24 +09006122 struct ata_link *link;
Tejun Heo500530f2006-07-03 16:07:27 +09006123
6124 /* Previous resume operation might still be in
6125 * progress. Wait for PM_PENDING to clear.
6126 */
6127 if (ap->pflags & ATA_PFLAG_PM_PENDING) {
6128 ata_port_wait_eh(ap);
6129 WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
6130 }
6131
6132 /* request PM ops to EH */
6133 spin_lock_irqsave(ap->lock, flags);
6134
6135 ap->pm_mesg = mesg;
6136 if (wait) {
6137 rc = 0;
6138 ap->pm_result = &rc;
6139 }
6140
6141 ap->pflags |= ATA_PFLAG_PM_PENDING;
Tejun Heoe3667eb2007-08-06 18:36:24 +09006142 __ata_port_for_each_link(link, ap) {
6143 link->eh_info.action |= action;
6144 link->eh_info.flags |= ehi_flags;
6145 }
Tejun Heo500530f2006-07-03 16:07:27 +09006146
6147 ata_port_schedule_eh(ap);
6148
6149 spin_unlock_irqrestore(ap->lock, flags);
6150
6151 /* wait and check result */
6152 if (wait) {
6153 ata_port_wait_eh(ap);
6154 WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
6155 if (rc)
6156 return rc;
6157 }
6158 }
6159
6160 return 0;
6161}
6162
6163/**
Jeff Garzikcca39742006-08-24 03:19:22 -04006164 * ata_host_suspend - suspend host
6165 * @host: host to suspend
Tejun Heo500530f2006-07-03 16:07:27 +09006166 * @mesg: PM message
6167 *
Jeff Garzikcca39742006-08-24 03:19:22 -04006168 * Suspend @host. Actual operation is performed by EH. This
Tejun Heo500530f2006-07-03 16:07:27 +09006169 * function requests EH to perform PM operations and waits for EH
6170 * to finish.
6171 *
6172 * LOCKING:
6173 * Kernel thread context (may sleep).
6174 *
6175 * RETURNS:
6176 * 0 on success, -errno on failure.
6177 */
Jeff Garzikcca39742006-08-24 03:19:22 -04006178int ata_host_suspend(struct ata_host *host, pm_message_t mesg)
Tejun Heo500530f2006-07-03 16:07:27 +09006179{
Tejun Heo9666f402007-05-04 21:27:47 +02006180 int rc;
Tejun Heo500530f2006-07-03 16:07:27 +09006181
Jeff Garzikcca39742006-08-24 03:19:22 -04006182 rc = ata_host_request_pm(host, mesg, 0, ATA_EHI_QUIET, 1);
Tejun Heo9666f402007-05-04 21:27:47 +02006183 if (rc == 0)
6184 host->dev->power.power_state = mesg;
Tejun Heo500530f2006-07-03 16:07:27 +09006185 return rc;
6186}
6187
6188/**
Jeff Garzikcca39742006-08-24 03:19:22 -04006189 * ata_host_resume - resume host
6190 * @host: host to resume
Tejun Heo500530f2006-07-03 16:07:27 +09006191 *
Jeff Garzikcca39742006-08-24 03:19:22 -04006192 * Resume @host. Actual operation is performed by EH. This
Tejun Heo500530f2006-07-03 16:07:27 +09006193 * function requests EH to perform PM operations and returns.
6194 * Note that all resume operations are performed parallely.
6195 *
6196 * LOCKING:
6197 * Kernel thread context (may sleep).
6198 */
Jeff Garzikcca39742006-08-24 03:19:22 -04006199void ata_host_resume(struct ata_host *host)
Tejun Heo500530f2006-07-03 16:07:27 +09006200{
Jeff Garzikcca39742006-08-24 03:19:22 -04006201 ata_host_request_pm(host, PMSG_ON, ATA_EH_SOFTRESET,
6202 ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET, 0);
6203 host->dev->power.power_state = PMSG_ON;
Tejun Heo500530f2006-07-03 16:07:27 +09006204}
Tejun Heo6ffa01d2007-03-02 17:32:47 +09006205#endif
Tejun Heo500530f2006-07-03 16:07:27 +09006206
Randy Dunlapc893a3a2006-01-28 13:15:32 -05006207/**
6208 * ata_port_start - Set port up for dma.
6209 * @ap: Port to initialize
6210 *
6211 * Called just after data structures for each port are
6212 * initialized. Allocates space for PRD table.
6213 *
6214 * May be used as the port_start() entry in ata_port_operations.
6215 *
6216 * LOCKING:
6217 * Inherited from caller.
6218 */
Tejun Heof0d36ef2007-01-20 16:00:28 +09006219int ata_port_start(struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006220{
Brian King2f1f6102006-03-23 17:30:15 -06006221 struct device *dev = ap->dev;
Jeff Garzik6037d6b2005-11-04 22:08:00 -05006222 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006223
Tejun Heof0d36ef2007-01-20 16:00:28 +09006224 ap->prd = dmam_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma,
6225 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006226 if (!ap->prd)
6227 return -ENOMEM;
6228
Jeff Garzik6037d6b2005-11-04 22:08:00 -05006229 rc = ata_pad_alloc(ap, dev);
Tejun Heof0d36ef2007-01-20 16:00:28 +09006230 if (rc)
Jeff Garzik6037d6b2005-11-04 22:08:00 -05006231 return rc;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04006232
Tejun Heof0d36ef2007-01-20 16:00:28 +09006233 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd,
6234 (unsigned long long)ap->prd_dma);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006235 return 0;
6236}
6237
Edward Falk0baab862005-06-02 18:17:13 -04006238/**
Tejun Heo3ef3b432006-05-31 18:27:30 +09006239 * ata_dev_init - Initialize an ata_device structure
6240 * @dev: Device structure to initialize
6241 *
6242 * Initialize @dev in preparation for probing.
6243 *
6244 * LOCKING:
6245 * Inherited from caller.
6246 */
6247void ata_dev_init(struct ata_device *dev)
6248{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09006249 struct ata_link *link = dev->link;
6250 struct ata_port *ap = link->ap;
Tejun Heo72fa4b72006-05-31 18:27:32 +09006251 unsigned long flags;
Tejun Heo3ef3b432006-05-31 18:27:30 +09006252
Tejun Heo5a04bf42006-05-31 18:27:38 +09006253 /* SATA spd limit is bound to the first device */
Tejun Heo9af5c9c2007-08-06 18:36:22 +09006254 link->sata_spd_limit = link->hw_sata_spd_limit;
6255 link->sata_spd = 0;
Tejun Heo5a04bf42006-05-31 18:27:38 +09006256
Tejun Heo72fa4b72006-05-31 18:27:32 +09006257 /* High bits of dev->flags are used to record warm plug
6258 * requests which occur asynchronously. Synchronize using
Jeff Garzikcca39742006-08-24 03:19:22 -04006259 * host lock.
Tejun Heo72fa4b72006-05-31 18:27:32 +09006260 */
Jeff Garzikba6a1302006-06-22 23:46:10 -04006261 spin_lock_irqsave(ap->lock, flags);
Tejun Heo72fa4b72006-05-31 18:27:32 +09006262 dev->flags &= ~ATA_DFLAG_INIT_MASK;
Tejun Heo3dcc3232007-09-03 12:20:11 +09006263 dev->horkage = 0;
Jeff Garzikba6a1302006-06-22 23:46:10 -04006264 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo72fa4b72006-05-31 18:27:32 +09006265
6266 memset((void *)dev + ATA_DEVICE_CLEAR_OFFSET, 0,
6267 sizeof(*dev) - ATA_DEVICE_CLEAR_OFFSET);
Tejun Heo3ef3b432006-05-31 18:27:30 +09006268 dev->pio_mask = UINT_MAX;
6269 dev->mwdma_mask = UINT_MAX;
6270 dev->udma_mask = UINT_MAX;
6271}
6272
6273/**
Tejun Heo4fb37a22007-08-06 18:36:23 +09006274 * ata_link_init - Initialize an ata_link structure
6275 * @ap: ATA port link is attached to
6276 * @link: Link structure to initialize
Tejun Heo89898052007-08-06 18:36:23 +09006277 * @pmp: Port multiplier port number
Tejun Heo4fb37a22007-08-06 18:36:23 +09006278 *
6279 * Initialize @link.
6280 *
6281 * LOCKING:
6282 * Kernel thread context (may sleep)
6283 */
Tejun Heofb7fd612007-09-23 13:14:12 +09006284void ata_link_init(struct ata_port *ap, struct ata_link *link, int pmp)
Tejun Heo4fb37a22007-08-06 18:36:23 +09006285{
6286 int i;
6287
6288 /* clear everything except for devices */
6289 memset(link, 0, offsetof(struct ata_link, device[0]));
6290
6291 link->ap = ap;
Tejun Heo89898052007-08-06 18:36:23 +09006292 link->pmp = pmp;
Tejun Heo4fb37a22007-08-06 18:36:23 +09006293 link->active_tag = ATA_TAG_POISON;
6294 link->hw_sata_spd_limit = UINT_MAX;
6295
6296 /* can't use iterator, ap isn't initialized yet */
6297 for (i = 0; i < ATA_MAX_DEVICES; i++) {
6298 struct ata_device *dev = &link->device[i];
6299
6300 dev->link = link;
6301 dev->devno = dev - link->device;
6302 ata_dev_init(dev);
6303 }
6304}
6305
6306/**
6307 * sata_link_init_spd - Initialize link->sata_spd_limit
6308 * @link: Link to configure sata_spd_limit for
6309 *
6310 * Initialize @link->[hw_]sata_spd_limit to the currently
6311 * configured value.
6312 *
6313 * LOCKING:
6314 * Kernel thread context (may sleep).
6315 *
6316 * RETURNS:
6317 * 0 on success, -errno on failure.
6318 */
Tejun Heofb7fd612007-09-23 13:14:12 +09006319int sata_link_init_spd(struct ata_link *link)
Tejun Heo4fb37a22007-08-06 18:36:23 +09006320{
6321 u32 scontrol, spd;
6322 int rc;
6323
6324 rc = sata_scr_read(link, SCR_CONTROL, &scontrol);
6325 if (rc)
6326 return rc;
6327
6328 spd = (scontrol >> 4) & 0xf;
6329 if (spd)
6330 link->hw_sata_spd_limit &= (1 << spd) - 1;
6331
6332 link->sata_spd_limit = link->hw_sata_spd_limit;
6333
6334 return 0;
6335}
6336
6337/**
Tejun Heof3187192007-04-17 23:44:07 +09006338 * ata_port_alloc - allocate and initialize basic ATA port resources
6339 * @host: ATA host this allocated port belongs to
Linus Torvalds1da177e2005-04-16 15:20:36 -07006340 *
Tejun Heof3187192007-04-17 23:44:07 +09006341 * Allocate and initialize basic ATA port resources.
6342 *
6343 * RETURNS:
6344 * Allocate ATA port on success, NULL on failure.
Jeff Garzik0cba6322005-05-30 19:49:12 -04006345 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07006346 * LOCKING:
Tejun Heof3187192007-04-17 23:44:07 +09006347 * Inherited from calling layer (may sleep).
Linus Torvalds1da177e2005-04-16 15:20:36 -07006348 */
Tejun Heof3187192007-04-17 23:44:07 +09006349struct ata_port *ata_port_alloc(struct ata_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006350{
Tejun Heof3187192007-04-17 23:44:07 +09006351 struct ata_port *ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006352
Tejun Heof3187192007-04-17 23:44:07 +09006353 DPRINTK("ENTER\n");
6354
6355 ap = kzalloc(sizeof(*ap), GFP_KERNEL);
6356 if (!ap)
6357 return NULL;
6358
Tejun Heof4d6d002007-05-01 11:50:15 +02006359 ap->pflags |= ATA_PFLAG_INITIALIZING;
Jeff Garzikcca39742006-08-24 03:19:22 -04006360 ap->lock = &host->lock;
Tejun Heo198e0fe2006-04-02 18:51:52 +09006361 ap->flags = ATA_FLAG_DISABLED;
Tejun Heof3187192007-04-17 23:44:07 +09006362 ap->print_id = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006363 ap->ctl = ATA_DEVCTL_OBS;
Jeff Garzikcca39742006-08-24 03:19:22 -04006364 ap->host = host;
Tejun Heof3187192007-04-17 23:44:07 +09006365 ap->dev = host->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006366 ap->last_ctl = 0xFF;
Borislav Petkovbd5d8252006-06-11 23:17:01 -04006367
6368#if defined(ATA_VERBOSE_DEBUG)
6369 /* turn on all debugging levels */
6370 ap->msg_enable = 0x00FF;
6371#elif defined(ATA_DEBUG)
6372 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 +09006373#else
Borislav Petkov0dd4b212006-06-23 02:29:08 -04006374 ap->msg_enable = ATA_MSG_DRV | ATA_MSG_ERR | ATA_MSG_WARN;
Borislav Petkovbd5d8252006-06-11 23:17:01 -04006375#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006376
David Howells65f27f32006-11-22 14:55:48 +00006377 INIT_DELAYED_WORK(&ap->port_task, NULL);
6378 INIT_DELAYED_WORK(&ap->hotplug_task, ata_scsi_hotplug);
6379 INIT_WORK(&ap->scsi_rescan_task, ata_scsi_dev_rescan);
Tejun Heoa72ec4c2006-01-23 13:09:37 +09006380 INIT_LIST_HEAD(&ap->eh_done_q);
Tejun Heoc6cf9e92006-05-31 18:27:27 +09006381 init_waitqueue_head(&ap->eh_wait_q);
Tejun Heo5ddf24c2007-07-16 14:29:41 +09006382 init_timer_deferrable(&ap->fastdrain_timer);
6383 ap->fastdrain_timer.function = ata_eh_fastdrain_timerfn;
6384 ap->fastdrain_timer.data = (unsigned long)ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006385
Tejun Heo838df622006-05-15 20:57:44 +09006386 ap->cbl = ATA_CBL_NONE;
Tejun Heo838df622006-05-15 20:57:44 +09006387
Tejun Heo89898052007-08-06 18:36:23 +09006388 ata_link_init(ap, &ap->link, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006389
6390#ifdef ATA_IRQ_TRAP
6391 ap->stats.unhandled_irq = 1;
6392 ap->stats.idle_irq = 1;
6393#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07006394 return ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006395}
6396
Tejun Heof0d36ef2007-01-20 16:00:28 +09006397static void ata_host_release(struct device *gendev, void *res)
6398{
6399 struct ata_host *host = dev_get_drvdata(gendev);
6400 int i;
6401
6402 for (i = 0; i < host->n_ports; i++) {
6403 struct ata_port *ap = host->ports[i];
6404
Tejun Heoecef7252007-04-17 23:44:06 +09006405 if (!ap)
6406 continue;
6407
6408 if ((host->flags & ATA_HOST_STARTED) && ap->ops->port_stop)
Tejun Heof0d36ef2007-01-20 16:00:28 +09006409 ap->ops->port_stop(ap);
Tejun Heof0d36ef2007-01-20 16:00:28 +09006410 }
6411
Tejun Heoecef7252007-04-17 23:44:06 +09006412 if ((host->flags & ATA_HOST_STARTED) && host->ops->host_stop)
Tejun Heof0d36ef2007-01-20 16:00:28 +09006413 host->ops->host_stop(host);
Tejun Heo1aa56cc2007-02-27 22:33:21 +09006414
Tejun Heo1aa506e2007-03-09 19:36:12 +09006415 for (i = 0; i < host->n_ports; i++) {
6416 struct ata_port *ap = host->ports[i];
6417
Tejun Heo49114872007-04-17 23:44:06 +09006418 if (!ap)
6419 continue;
6420
6421 if (ap->scsi_host)
Tejun Heo1aa506e2007-03-09 19:36:12 +09006422 scsi_host_put(ap->scsi_host);
6423
Tejun Heo49114872007-04-17 23:44:06 +09006424 kfree(ap);
Tejun Heo1aa506e2007-03-09 19:36:12 +09006425 host->ports[i] = NULL;
6426 }
6427
Tejun Heo1aa56cc2007-02-27 22:33:21 +09006428 dev_set_drvdata(gendev, NULL);
Tejun Heof0d36ef2007-01-20 16:00:28 +09006429}
6430
Linus Torvalds1da177e2005-04-16 15:20:36 -07006431/**
Tejun Heof3187192007-04-17 23:44:07 +09006432 * ata_host_alloc - allocate and init basic ATA host resources
6433 * @dev: generic device this host is associated with
6434 * @max_ports: maximum number of ATA ports associated with this host
6435 *
6436 * Allocate and initialize basic ATA host resources. LLD calls
6437 * this function to allocate a host, initializes it fully and
6438 * attaches it using ata_host_register().
6439 *
6440 * @max_ports ports are allocated and host->n_ports is
6441 * initialized to @max_ports. The caller is allowed to decrease
6442 * host->n_ports before calling ata_host_register(). The unused
6443 * ports will be automatically freed on registration.
6444 *
6445 * RETURNS:
6446 * Allocate ATA host on success, NULL on failure.
6447 *
6448 * LOCKING:
6449 * Inherited from calling layer (may sleep).
6450 */
6451struct ata_host *ata_host_alloc(struct device *dev, int max_ports)
6452{
6453 struct ata_host *host;
6454 size_t sz;
6455 int i;
6456
6457 DPRINTK("ENTER\n");
6458
6459 if (!devres_open_group(dev, NULL, GFP_KERNEL))
6460 return NULL;
6461
6462 /* alloc a container for our list of ATA ports (buses) */
6463 sz = sizeof(struct ata_host) + (max_ports + 1) * sizeof(void *);
6464 /* alloc a container for our list of ATA ports (buses) */
6465 host = devres_alloc(ata_host_release, sz, GFP_KERNEL);
6466 if (!host)
6467 goto err_out;
6468
6469 devres_add(dev, host);
6470 dev_set_drvdata(dev, host);
6471
6472 spin_lock_init(&host->lock);
6473 host->dev = dev;
6474 host->n_ports = max_ports;
6475
6476 /* allocate ports bound to this host */
6477 for (i = 0; i < max_ports; i++) {
6478 struct ata_port *ap;
6479
6480 ap = ata_port_alloc(host);
6481 if (!ap)
6482 goto err_out;
6483
6484 ap->port_no = i;
6485 host->ports[i] = ap;
6486 }
6487
6488 devres_remove_group(dev, NULL);
6489 return host;
6490
6491 err_out:
6492 devres_release_group(dev, NULL);
6493 return NULL;
6494}
6495
6496/**
Tejun Heof5cda252007-04-17 23:44:07 +09006497 * ata_host_alloc_pinfo - alloc host and init with port_info array
6498 * @dev: generic device this host is associated with
6499 * @ppi: array of ATA port_info to initialize host with
6500 * @n_ports: number of ATA ports attached to this host
6501 *
6502 * Allocate ATA host and initialize with info from @ppi. If NULL
6503 * terminated, @ppi may contain fewer entries than @n_ports. The
6504 * last entry will be used for the remaining ports.
6505 *
6506 * RETURNS:
6507 * Allocate ATA host on success, NULL on failure.
6508 *
6509 * LOCKING:
6510 * Inherited from calling layer (may sleep).
6511 */
6512struct ata_host *ata_host_alloc_pinfo(struct device *dev,
6513 const struct ata_port_info * const * ppi,
6514 int n_ports)
6515{
6516 const struct ata_port_info *pi;
6517 struct ata_host *host;
6518 int i, j;
6519
6520 host = ata_host_alloc(dev, n_ports);
6521 if (!host)
6522 return NULL;
6523
6524 for (i = 0, j = 0, pi = NULL; i < host->n_ports; i++) {
6525 struct ata_port *ap = host->ports[i];
6526
6527 if (ppi[j])
6528 pi = ppi[j++];
6529
6530 ap->pio_mask = pi->pio_mask;
6531 ap->mwdma_mask = pi->mwdma_mask;
6532 ap->udma_mask = pi->udma_mask;
6533 ap->flags |= pi->flags;
Tejun Heo0c887582007-08-06 18:36:23 +09006534 ap->link.flags |= pi->link_flags;
Tejun Heof5cda252007-04-17 23:44:07 +09006535 ap->ops = pi->port_ops;
6536
6537 if (!host->ops && (pi->port_ops != &ata_dummy_port_ops))
6538 host->ops = pi->port_ops;
6539 if (!host->private_data && pi->private_data)
6540 host->private_data = pi->private_data;
6541 }
6542
6543 return host;
6544}
6545
6546/**
Tejun Heoecef7252007-04-17 23:44:06 +09006547 * ata_host_start - start and freeze ports of an ATA host
6548 * @host: ATA host to start ports for
6549 *
6550 * Start and then freeze ports of @host. Started status is
6551 * recorded in host->flags, so this function can be called
6552 * multiple times. Ports are guaranteed to get started only
Tejun Heof3187192007-04-17 23:44:07 +09006553 * once. If host->ops isn't initialized yet, its set to the
6554 * first non-dummy port ops.
Tejun Heoecef7252007-04-17 23:44:06 +09006555 *
6556 * LOCKING:
6557 * Inherited from calling layer (may sleep).
6558 *
6559 * RETURNS:
6560 * 0 if all ports are started successfully, -errno otherwise.
6561 */
6562int ata_host_start(struct ata_host *host)
6563{
6564 int i, rc;
6565
6566 if (host->flags & ATA_HOST_STARTED)
6567 return 0;
6568
6569 for (i = 0; i < host->n_ports; i++) {
6570 struct ata_port *ap = host->ports[i];
6571
Tejun Heof3187192007-04-17 23:44:07 +09006572 if (!host->ops && !ata_port_is_dummy(ap))
6573 host->ops = ap->ops;
6574
Tejun Heoecef7252007-04-17 23:44:06 +09006575 if (ap->ops->port_start) {
6576 rc = ap->ops->port_start(ap);
6577 if (rc) {
6578 ata_port_printk(ap, KERN_ERR, "failed to "
6579 "start port (errno=%d)\n", rc);
6580 goto err_out;
6581 }
6582 }
6583
6584 ata_eh_freeze_port(ap);
6585 }
6586
6587 host->flags |= ATA_HOST_STARTED;
6588 return 0;
6589
6590 err_out:
6591 while (--i >= 0) {
6592 struct ata_port *ap = host->ports[i];
6593
6594 if (ap->ops->port_stop)
6595 ap->ops->port_stop(ap);
6596 }
6597 return rc;
6598}
6599
6600/**
Jeff Garzikcca39742006-08-24 03:19:22 -04006601 * ata_sas_host_init - Initialize a host struct
6602 * @host: host to initialize
6603 * @dev: device host is attached to
6604 * @flags: host flags
6605 * @ops: port_ops
Brian Kingb03732f2006-08-07 14:27:10 -05006606 *
6607 * LOCKING:
6608 * PCI/etc. bus probe sem.
6609 *
6610 */
Tejun Heof3187192007-04-17 23:44:07 +09006611/* KILLME - the only user left is ipr */
Jeff Garzikcca39742006-08-24 03:19:22 -04006612void ata_host_init(struct ata_host *host, struct device *dev,
6613 unsigned long flags, const struct ata_port_operations *ops)
Brian Kingb03732f2006-08-07 14:27:10 -05006614{
Jeff Garzikcca39742006-08-24 03:19:22 -04006615 spin_lock_init(&host->lock);
6616 host->dev = dev;
6617 host->flags = flags;
6618 host->ops = ops;
Brian Kingb03732f2006-08-07 14:27:10 -05006619}
6620
6621/**
Tejun Heof3187192007-04-17 23:44:07 +09006622 * ata_host_register - register initialized ATA host
6623 * @host: ATA host to register
6624 * @sht: template for SCSI host
Jeff Garzik0cba6322005-05-30 19:49:12 -04006625 *
Tejun Heof3187192007-04-17 23:44:07 +09006626 * Register initialized ATA host. @host is allocated using
6627 * ata_host_alloc() and fully initialized by LLD. This function
6628 * starts ports, registers @host with ATA and SCSI layers and
6629 * probe registered devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006630 *
6631 * LOCKING:
Tejun Heof3187192007-04-17 23:44:07 +09006632 * Inherited from calling layer (may sleep).
Linus Torvalds1da177e2005-04-16 15:20:36 -07006633 *
6634 * RETURNS:
Tejun Heof3187192007-04-17 23:44:07 +09006635 * 0 on success, -errno otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006636 */
Tejun Heof3187192007-04-17 23:44:07 +09006637int ata_host_register(struct ata_host *host, struct scsi_host_template *sht)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006638{
Tejun Heof3187192007-04-17 23:44:07 +09006639 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006640
Tejun Heof3187192007-04-17 23:44:07 +09006641 /* host must have been started */
6642 if (!(host->flags & ATA_HOST_STARTED)) {
6643 dev_printk(KERN_ERR, host->dev,
6644 "BUG: trying to register unstarted host\n");
6645 WARN_ON(1);
6646 return -EINVAL;
Alan Cox02f076a2006-09-26 17:35:32 +01006647 }
Tejun Heof0d36ef2007-01-20 16:00:28 +09006648
Tejun Heof3187192007-04-17 23:44:07 +09006649 /* Blow away unused ports. This happens when LLD can't
6650 * determine the exact number of ports to allocate at
6651 * allocation time.
6652 */
6653 for (i = host->n_ports; host->ports[i]; i++)
6654 kfree(host->ports[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006655
Tejun Heof3187192007-04-17 23:44:07 +09006656 /* give ports names and add SCSI hosts */
6657 for (i = 0; i < host->n_ports; i++)
6658 host->ports[i]->print_id = ata_print_id++;
Tejun Heof0d36ef2007-01-20 16:00:28 +09006659
Tejun Heof3187192007-04-17 23:44:07 +09006660 rc = ata_scsi_add_hosts(host, sht);
Tejun Heoecef7252007-04-17 23:44:06 +09006661 if (rc)
Tejun Heof3187192007-04-17 23:44:07 +09006662 return rc;
Tejun Heoecef7252007-04-17 23:44:06 +09006663
Tejun Heofafbae82007-05-15 03:28:16 +09006664 /* associate with ACPI nodes */
6665 ata_acpi_associate(host);
6666
Tejun Heof3187192007-04-17 23:44:07 +09006667 /* set cable, sata_spd_limit and report */
Jeff Garzikcca39742006-08-24 03:19:22 -04006668 for (i = 0; i < host->n_ports; i++) {
6669 struct ata_port *ap = host->ports[i];
Tejun Heof3187192007-04-17 23:44:07 +09006670 unsigned long xfer_mask;
6671
6672 /* set SATA cable type if still unset */
6673 if (ap->cbl == ATA_CBL_NONE && (ap->flags & ATA_FLAG_SATA))
6674 ap->cbl = ATA_CBL_SATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006675
Tejun Heo5a04bf42006-05-31 18:27:38 +09006676 /* init sata_spd_limit to the current value */
Tejun Heo4fb37a22007-08-06 18:36:23 +09006677 sata_link_init_spd(&ap->link);
Tejun Heo5a04bf42006-05-31 18:27:38 +09006678
Tejun Heocbcdd872007-08-18 13:14:55 +09006679 /* print per-port info to dmesg */
Tejun Heof3187192007-04-17 23:44:07 +09006680 xfer_mask = ata_pack_xfermask(ap->pio_mask, ap->mwdma_mask,
6681 ap->udma_mask);
6682
Tejun Heof3187192007-04-17 23:44:07 +09006683 if (!ata_port_is_dummy(ap))
Tejun Heocbcdd872007-08-18 13:14:55 +09006684 ata_port_printk(ap, KERN_INFO,
6685 "%cATA max %s %s\n",
Tejun Heoa16abc02007-05-21 18:33:47 +02006686 (ap->flags & ATA_FLAG_SATA) ? 'S' : 'P',
Tejun Heof3187192007-04-17 23:44:07 +09006687 ata_mode_string(xfer_mask),
Tejun Heocbcdd872007-08-18 13:14:55 +09006688 ap->link.eh_info.desc);
Tejun Heof3187192007-04-17 23:44:07 +09006689 else
6690 ata_port_printk(ap, KERN_INFO, "DUMMY\n");
6691 }
6692
6693 /* perform each probe synchronously */
6694 DPRINTK("probe begin\n");
6695 for (i = 0; i < host->n_ports; i++) {
6696 struct ata_port *ap = host->ports[i];
6697 int rc;
6698
6699 /* probe */
Tejun Heo52783c52006-05-31 18:28:22 +09006700 if (ap->ops->error_handler) {
Tejun Heo9af5c9c2007-08-06 18:36:22 +09006701 struct ata_eh_info *ehi = &ap->link.eh_info;
Tejun Heo3e706392006-05-31 18:28:11 +09006702 unsigned long flags;
6703
6704 ata_port_probe(ap);
6705
6706 /* kick EH for boot probing */
Jeff Garzikba6a1302006-06-22 23:46:10 -04006707 spin_lock_irqsave(ap->lock, flags);
Tejun Heo3e706392006-05-31 18:28:11 +09006708
Tejun Heof58229f2007-08-06 18:36:23 +09006709 ehi->probe_mask =
6710 (1 << ata_link_max_devices(&ap->link)) - 1;
Tejun Heo1cdaf532006-07-03 16:07:26 +09006711 ehi->action |= ATA_EH_SOFTRESET;
6712 ehi->flags |= ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET;
Tejun Heo3e706392006-05-31 18:28:11 +09006713
Tejun Heof4d6d002007-05-01 11:50:15 +02006714 ap->pflags &= ~ATA_PFLAG_INITIALIZING;
Tejun Heob51e9e52006-06-29 01:29:30 +09006715 ap->pflags |= ATA_PFLAG_LOADING;
Tejun Heo3e706392006-05-31 18:28:11 +09006716 ata_port_schedule_eh(ap);
6717
Jeff Garzikba6a1302006-06-22 23:46:10 -04006718 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo3e706392006-05-31 18:28:11 +09006719
6720 /* wait for EH to finish */
6721 ata_port_wait_eh(ap);
6722 } else {
Tejun Heo44877b42007-02-21 01:06:51 +09006723 DPRINTK("ata%u: bus probe begin\n", ap->print_id);
Tejun Heo3e706392006-05-31 18:28:11 +09006724 rc = ata_bus_probe(ap);
Tejun Heo44877b42007-02-21 01:06:51 +09006725 DPRINTK("ata%u: bus probe end\n", ap->print_id);
Tejun Heo3e706392006-05-31 18:28:11 +09006726
6727 if (rc) {
6728 /* FIXME: do something useful here?
6729 * Current libata behavior will
6730 * tear down everything when
6731 * the module is removed
6732 * or the h/w is unplugged.
6733 */
6734 }
6735 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006736 }
6737
6738 /* probes are done, now scan each port's disk(s) */
Randy Dunlapc893a3a2006-01-28 13:15:32 -05006739 DPRINTK("host probe begin\n");
Jeff Garzikcca39742006-08-24 03:19:22 -04006740 for (i = 0; i < host->n_ports; i++) {
6741 struct ata_port *ap = host->ports[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07006742
Tejun Heo1ae46312007-07-16 14:29:40 +09006743 ata_scsi_scan_host(ap, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006744 }
6745
Tejun Heof3187192007-04-17 23:44:07 +09006746 return 0;
6747}
6748
6749/**
Tejun Heof5cda252007-04-17 23:44:07 +09006750 * ata_host_activate - start host, request IRQ and register it
6751 * @host: target ATA host
6752 * @irq: IRQ to request
6753 * @irq_handler: irq_handler used when requesting IRQ
6754 * @irq_flags: irq_flags used when requesting IRQ
6755 * @sht: scsi_host_template to use when registering the host
6756 *
6757 * After allocating an ATA host and initializing it, most libata
6758 * LLDs perform three steps to activate the host - start host,
6759 * request IRQ and register it. This helper takes necessasry
6760 * arguments and performs the three steps in one go.
6761 *
6762 * LOCKING:
6763 * Inherited from calling layer (may sleep).
6764 *
6765 * RETURNS:
6766 * 0 on success, -errno otherwise.
6767 */
6768int ata_host_activate(struct ata_host *host, int irq,
6769 irq_handler_t irq_handler, unsigned long irq_flags,
6770 struct scsi_host_template *sht)
6771{
Tejun Heocbcdd872007-08-18 13:14:55 +09006772 int i, rc;
Tejun Heof5cda252007-04-17 23:44:07 +09006773
6774 rc = ata_host_start(host);
6775 if (rc)
6776 return rc;
6777
6778 rc = devm_request_irq(host->dev, irq, irq_handler, irq_flags,
6779 dev_driver_string(host->dev), host);
6780 if (rc)
6781 return rc;
6782
Tejun Heocbcdd872007-08-18 13:14:55 +09006783 for (i = 0; i < host->n_ports; i++)
6784 ata_port_desc(host->ports[i], "irq %d", irq);
Tejun Heo40318262007-07-03 01:38:47 +09006785
Tejun Heof5cda252007-04-17 23:44:07 +09006786 rc = ata_host_register(host, sht);
6787 /* if failed, just free the IRQ and leave ports alone */
6788 if (rc)
6789 devm_free_irq(host->dev, irq, host);
6790
6791 return rc;
6792}
6793
6794/**
Tejun Heo720ba122006-05-31 18:28:13 +09006795 * ata_port_detach - Detach ATA port in prepration of device removal
6796 * @ap: ATA port to be detached
6797 *
6798 * Detach all ATA devices and the associated SCSI devices of @ap;
6799 * then, remove the associated SCSI host. @ap is guaranteed to
6800 * be quiescent on return from this function.
6801 *
6802 * LOCKING:
6803 * Kernel thread context (may sleep).
6804 */
6805void ata_port_detach(struct ata_port *ap)
6806{
6807 unsigned long flags;
Tejun Heo41bda9c2007-08-06 18:36:24 +09006808 struct ata_link *link;
Tejun Heof58229f2007-08-06 18:36:23 +09006809 struct ata_device *dev;
Tejun Heo720ba122006-05-31 18:28:13 +09006810
6811 if (!ap->ops->error_handler)
Tejun Heoc3cf30a2006-08-05 03:59:11 +09006812 goto skip_eh;
Tejun Heo720ba122006-05-31 18:28:13 +09006813
6814 /* tell EH we're leaving & flush EH */
Jeff Garzikba6a1302006-06-22 23:46:10 -04006815 spin_lock_irqsave(ap->lock, flags);
Tejun Heob51e9e52006-06-29 01:29:30 +09006816 ap->pflags |= ATA_PFLAG_UNLOADING;
Jeff Garzikba6a1302006-06-22 23:46:10 -04006817 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo720ba122006-05-31 18:28:13 +09006818
6819 ata_port_wait_eh(ap);
6820
6821 /* EH is now guaranteed to see UNLOADING, so no new device
6822 * will be attached. Disable all existing devices.
6823 */
Jeff Garzikba6a1302006-06-22 23:46:10 -04006824 spin_lock_irqsave(ap->lock, flags);
Tejun Heo720ba122006-05-31 18:28:13 +09006825
Tejun Heo41bda9c2007-08-06 18:36:24 +09006826 ata_port_for_each_link(link, ap) {
6827 ata_link_for_each_dev(dev, link)
6828 ata_dev_disable(dev);
6829 }
Tejun Heo720ba122006-05-31 18:28:13 +09006830
Jeff Garzikba6a1302006-06-22 23:46:10 -04006831 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo720ba122006-05-31 18:28:13 +09006832
6833 /* Final freeze & EH. All in-flight commands are aborted. EH
6834 * will be skipped and retrials will be terminated with bad
6835 * target.
6836 */
Jeff Garzikba6a1302006-06-22 23:46:10 -04006837 spin_lock_irqsave(ap->lock, flags);
Tejun Heo720ba122006-05-31 18:28:13 +09006838 ata_port_freeze(ap); /* won't be thawed */
Jeff Garzikba6a1302006-06-22 23:46:10 -04006839 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo720ba122006-05-31 18:28:13 +09006840
6841 ata_port_wait_eh(ap);
Oleg Nesterov45a66c12007-07-09 11:46:13 -07006842 cancel_rearming_delayed_work(&ap->hotplug_task);
Tejun Heo720ba122006-05-31 18:28:13 +09006843
Tejun Heoc3cf30a2006-08-05 03:59:11 +09006844 skip_eh:
Tejun Heo720ba122006-05-31 18:28:13 +09006845 /* remove the associated SCSI host */
Jeff Garzikcca39742006-08-24 03:19:22 -04006846 scsi_remove_host(ap->scsi_host);
Tejun Heo720ba122006-05-31 18:28:13 +09006847}
6848
6849/**
Tejun Heo0529c1592007-01-20 16:00:26 +09006850 * ata_host_detach - Detach all ports of an ATA host
6851 * @host: Host to detach
6852 *
6853 * Detach all ports of @host.
6854 *
6855 * LOCKING:
6856 * Kernel thread context (may sleep).
6857 */
6858void ata_host_detach(struct ata_host *host)
6859{
6860 int i;
6861
6862 for (i = 0; i < host->n_ports; i++)
6863 ata_port_detach(host->ports[i]);
6864}
6865
Linus Torvalds1da177e2005-04-16 15:20:36 -07006866/**
6867 * ata_std_ports - initialize ioaddr with standard port offsets.
6868 * @ioaddr: IO address structure to be initialized
Edward Falk0baab862005-06-02 18:17:13 -04006869 *
6870 * Utility function which initializes data_addr, error_addr,
6871 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
6872 * device_addr, status_addr, and command_addr to standard offsets
6873 * relative to cmd_addr.
6874 *
6875 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006876 */
Edward Falk0baab862005-06-02 18:17:13 -04006877
Linus Torvalds1da177e2005-04-16 15:20:36 -07006878void ata_std_ports(struct ata_ioports *ioaddr)
6879{
6880 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
6881 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
6882 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
6883 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
6884 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
6885 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
6886 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
6887 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
6888 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
6889 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
6890}
6891
Edward Falk0baab862005-06-02 18:17:13 -04006892
Jeff Garzik374b1872005-08-30 05:42:52 -04006893#ifdef CONFIG_PCI
6894
Edward Falk0baab862005-06-02 18:17:13 -04006895/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07006896 * ata_pci_remove_one - PCI layer callback for device removal
6897 * @pdev: PCI device that was removed
6898 *
Tejun Heob878ca52007-01-20 16:00:28 +09006899 * PCI layer indicates to libata via this hook that hot-unplug or
6900 * module unload event has occurred. Detach all ports. Resource
6901 * release is handled via devres.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006902 *
6903 * LOCKING:
6904 * Inherited from PCI layer (may sleep).
6905 */
Tejun Heof0d36ef2007-01-20 16:00:28 +09006906void ata_pci_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006907{
6908 struct device *dev = pci_dev_to_dev(pdev);
Jeff Garzikcca39742006-08-24 03:19:22 -04006909 struct ata_host *host = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006910
Tejun Heob878ca52007-01-20 16:00:28 +09006911 ata_host_detach(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006912}
6913
6914/* move to PCI subsystem */
Jeff Garzik057ace52005-10-22 14:27:05 -04006915int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006916{
6917 unsigned long tmp = 0;
6918
6919 switch (bits->width) {
6920 case 1: {
6921 u8 tmp8 = 0;
6922 pci_read_config_byte(pdev, bits->reg, &tmp8);
6923 tmp = tmp8;
6924 break;
6925 }
6926 case 2: {
6927 u16 tmp16 = 0;
6928 pci_read_config_word(pdev, bits->reg, &tmp16);
6929 tmp = tmp16;
6930 break;
6931 }
6932 case 4: {
6933 u32 tmp32 = 0;
6934 pci_read_config_dword(pdev, bits->reg, &tmp32);
6935 tmp = tmp32;
6936 break;
6937 }
6938
6939 default:
6940 return -EINVAL;
6941 }
6942
6943 tmp &= bits->mask;
6944
6945 return (tmp == bits->val) ? 1 : 0;
6946}
Jens Axboe9b847542006-01-06 09:28:07 +01006947
Tejun Heo6ffa01d2007-03-02 17:32:47 +09006948#ifdef CONFIG_PM
Tejun Heo3c5100c2006-07-26 16:58:33 +09006949void ata_pci_device_do_suspend(struct pci_dev *pdev, pm_message_t mesg)
Jens Axboe9b847542006-01-06 09:28:07 +01006950{
6951 pci_save_state(pdev);
Tejun Heo4c90d972007-02-20 18:14:48 +09006952 pci_disable_device(pdev);
Tejun Heo500530f2006-07-03 16:07:27 +09006953
Tejun Heo4c90d972007-02-20 18:14:48 +09006954 if (mesg.event == PM_EVENT_SUSPEND)
Tejun Heo500530f2006-07-03 16:07:27 +09006955 pci_set_power_state(pdev, PCI_D3hot);
Jens Axboe9b847542006-01-06 09:28:07 +01006956}
6957
Tejun Heo553c4aa2006-12-26 19:39:50 +09006958int ata_pci_device_do_resume(struct pci_dev *pdev)
Jens Axboe9b847542006-01-06 09:28:07 +01006959{
Tejun Heo553c4aa2006-12-26 19:39:50 +09006960 int rc;
6961
Jens Axboe9b847542006-01-06 09:28:07 +01006962 pci_set_power_state(pdev, PCI_D0);
6963 pci_restore_state(pdev);
Tejun Heo553c4aa2006-12-26 19:39:50 +09006964
Tejun Heob878ca52007-01-20 16:00:28 +09006965 rc = pcim_enable_device(pdev);
Tejun Heo553c4aa2006-12-26 19:39:50 +09006966 if (rc) {
6967 dev_printk(KERN_ERR, &pdev->dev,
6968 "failed to enable device after resume (%d)\n", rc);
6969 return rc;
6970 }
6971
Jens Axboe9b847542006-01-06 09:28:07 +01006972 pci_set_master(pdev);
Tejun Heo553c4aa2006-12-26 19:39:50 +09006973 return 0;
Tejun Heo500530f2006-07-03 16:07:27 +09006974}
6975
Tejun Heo3c5100c2006-07-26 16:58:33 +09006976int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg)
Tejun Heo500530f2006-07-03 16:07:27 +09006977{
Jeff Garzikcca39742006-08-24 03:19:22 -04006978 struct ata_host *host = dev_get_drvdata(&pdev->dev);
Tejun Heo500530f2006-07-03 16:07:27 +09006979 int rc = 0;
6980
Jeff Garzikcca39742006-08-24 03:19:22 -04006981 rc = ata_host_suspend(host, mesg);
Tejun Heo500530f2006-07-03 16:07:27 +09006982 if (rc)
6983 return rc;
6984
Tejun Heo3c5100c2006-07-26 16:58:33 +09006985 ata_pci_device_do_suspend(pdev, mesg);
Tejun Heo500530f2006-07-03 16:07:27 +09006986
6987 return 0;
6988}
6989
6990int ata_pci_device_resume(struct pci_dev *pdev)
6991{
Jeff Garzikcca39742006-08-24 03:19:22 -04006992 struct ata_host *host = dev_get_drvdata(&pdev->dev);
Tejun Heo553c4aa2006-12-26 19:39:50 +09006993 int rc;
Tejun Heo500530f2006-07-03 16:07:27 +09006994
Tejun Heo553c4aa2006-12-26 19:39:50 +09006995 rc = ata_pci_device_do_resume(pdev);
6996 if (rc == 0)
6997 ata_host_resume(host);
6998 return rc;
Jens Axboe9b847542006-01-06 09:28:07 +01006999}
Tejun Heo6ffa01d2007-03-02 17:32:47 +09007000#endif /* CONFIG_PM */
7001
Linus Torvalds1da177e2005-04-16 15:20:36 -07007002#endif /* CONFIG_PCI */
7003
7004
Linus Torvalds1da177e2005-04-16 15:20:36 -07007005static int __init ata_init(void)
7006{
Andrew Mortona8601e52006-06-25 01:36:52 -07007007 ata_probe_timeout *= HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007008 ata_wq = create_workqueue("ata");
7009 if (!ata_wq)
7010 return -ENOMEM;
7011
Tejun Heo453b07a2006-05-31 18:27:42 +09007012 ata_aux_wq = create_singlethread_workqueue("ata_aux");
7013 if (!ata_aux_wq) {
7014 destroy_workqueue(ata_wq);
7015 return -ENOMEM;
7016 }
7017
Linus Torvalds1da177e2005-04-16 15:20:36 -07007018 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
7019 return 0;
7020}
7021
7022static void __exit ata_exit(void)
7023{
7024 destroy_workqueue(ata_wq);
Tejun Heo453b07a2006-05-31 18:27:42 +09007025 destroy_workqueue(ata_aux_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007026}
7027
Brian Kinga4625082006-11-13 16:32:36 -06007028subsys_initcall(ata_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007029module_exit(ata_exit);
7030
Jeff Garzik67846b32005-10-05 02:58:32 -04007031static unsigned long ratelimit_time;
Ingo Molnar34af9462006-06-27 02:53:55 -07007032static DEFINE_SPINLOCK(ata_ratelimit_lock);
Jeff Garzik67846b32005-10-05 02:58:32 -04007033
7034int ata_ratelimit(void)
7035{
7036 int rc;
7037 unsigned long flags;
7038
7039 spin_lock_irqsave(&ata_ratelimit_lock, flags);
7040
7041 if (time_after(jiffies, ratelimit_time)) {
7042 rc = 1;
7043 ratelimit_time = jiffies + (HZ/5);
7044 } else
7045 rc = 0;
7046
7047 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
7048
7049 return rc;
7050}
7051
Tejun Heoc22daff2006-04-11 22:22:29 +09007052/**
7053 * ata_wait_register - wait until register value changes
7054 * @reg: IO-mapped register
7055 * @mask: Mask to apply to read register value
7056 * @val: Wait condition
7057 * @interval_msec: polling interval in milliseconds
7058 * @timeout_msec: timeout in milliseconds
7059 *
7060 * Waiting for some bits of register to change is a common
7061 * operation for ATA controllers. This function reads 32bit LE
7062 * IO-mapped register @reg and tests for the following condition.
7063 *
7064 * (*@reg & mask) != val
7065 *
7066 * If the condition is met, it returns; otherwise, the process is
7067 * repeated after @interval_msec until timeout.
7068 *
7069 * LOCKING:
7070 * Kernel thread context (may sleep)
7071 *
7072 * RETURNS:
7073 * The final register value.
7074 */
7075u32 ata_wait_register(void __iomem *reg, u32 mask, u32 val,
7076 unsigned long interval_msec,
7077 unsigned long timeout_msec)
7078{
7079 unsigned long timeout;
7080 u32 tmp;
7081
7082 tmp = ioread32(reg);
7083
7084 /* Calculate timeout _after_ the first read to make sure
7085 * preceding writes reach the controller before starting to
7086 * eat away the timeout.
7087 */
7088 timeout = jiffies + (timeout_msec * HZ) / 1000;
7089
7090 while ((tmp & mask) == val && time_before(jiffies, timeout)) {
7091 msleep(interval_msec);
7092 tmp = ioread32(reg);
7093 }
7094
7095 return tmp;
7096}
7097
Linus Torvalds1da177e2005-04-16 15:20:36 -07007098/*
Tejun Heodd5b06c2006-08-10 16:59:12 +09007099 * Dummy port_ops
7100 */
7101static void ata_dummy_noret(struct ata_port *ap) { }
7102static int ata_dummy_ret0(struct ata_port *ap) { return 0; }
7103static void ata_dummy_qc_noret(struct ata_queued_cmd *qc) { }
7104
7105static u8 ata_dummy_check_status(struct ata_port *ap)
7106{
7107 return ATA_DRDY;
7108}
7109
7110static unsigned int ata_dummy_qc_issue(struct ata_queued_cmd *qc)
7111{
7112 return AC_ERR_SYSTEM;
7113}
7114
7115const struct ata_port_operations ata_dummy_port_ops = {
Tejun Heodd5b06c2006-08-10 16:59:12 +09007116 .check_status = ata_dummy_check_status,
7117 .check_altstatus = ata_dummy_check_status,
7118 .dev_select = ata_noop_dev_select,
7119 .qc_prep = ata_noop_qc_prep,
7120 .qc_issue = ata_dummy_qc_issue,
7121 .freeze = ata_dummy_noret,
7122 .thaw = ata_dummy_noret,
7123 .error_handler = ata_dummy_noret,
7124 .post_internal_cmd = ata_dummy_qc_noret,
7125 .irq_clear = ata_dummy_noret,
7126 .port_start = ata_dummy_ret0,
7127 .port_stop = ata_dummy_noret,
7128};
7129
Tejun Heo21b0ad42007-04-17 23:44:07 +09007130const struct ata_port_info ata_dummy_port_info = {
7131 .port_ops = &ata_dummy_port_ops,
7132};
7133
Tejun Heodd5b06c2006-08-10 16:59:12 +09007134/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07007135 * libata is essentially a library of internal helper functions for
7136 * low-level ATA host controller drivers. As such, the API/ABI is
7137 * likely to change as new drivers are added and updated.
7138 * Do not depend on ABI/API stability.
7139 */
7140
Tejun Heoe9c83912006-07-03 16:07:26 +09007141EXPORT_SYMBOL_GPL(sata_deb_timing_normal);
7142EXPORT_SYMBOL_GPL(sata_deb_timing_hotplug);
7143EXPORT_SYMBOL_GPL(sata_deb_timing_long);
Tejun Heodd5b06c2006-08-10 16:59:12 +09007144EXPORT_SYMBOL_GPL(ata_dummy_port_ops);
Tejun Heo21b0ad42007-04-17 23:44:07 +09007145EXPORT_SYMBOL_GPL(ata_dummy_port_info);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007146EXPORT_SYMBOL_GPL(ata_std_bios_param);
7147EXPORT_SYMBOL_GPL(ata_std_ports);
Jeff Garzikcca39742006-08-24 03:19:22 -04007148EXPORT_SYMBOL_GPL(ata_host_init);
Tejun Heof3187192007-04-17 23:44:07 +09007149EXPORT_SYMBOL_GPL(ata_host_alloc);
Tejun Heof5cda252007-04-17 23:44:07 +09007150EXPORT_SYMBOL_GPL(ata_host_alloc_pinfo);
Tejun Heoecef7252007-04-17 23:44:06 +09007151EXPORT_SYMBOL_GPL(ata_host_start);
Tejun Heof3187192007-04-17 23:44:07 +09007152EXPORT_SYMBOL_GPL(ata_host_register);
Tejun Heof5cda252007-04-17 23:44:07 +09007153EXPORT_SYMBOL_GPL(ata_host_activate);
Tejun Heo0529c1592007-01-20 16:00:26 +09007154EXPORT_SYMBOL_GPL(ata_host_detach);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007155EXPORT_SYMBOL_GPL(ata_sg_init);
7156EXPORT_SYMBOL_GPL(ata_sg_init_one);
Tejun Heo9a1004d2006-05-31 18:27:52 +09007157EXPORT_SYMBOL_GPL(ata_hsm_move);
Tejun Heof686bcb2006-05-15 20:58:05 +09007158EXPORT_SYMBOL_GPL(ata_qc_complete);
Tejun Heodedaf2b2006-05-15 21:03:43 +09007159EXPORT_SYMBOL_GPL(ata_qc_complete_multiple);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007160EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007161EXPORT_SYMBOL_GPL(ata_tf_load);
7162EXPORT_SYMBOL_GPL(ata_tf_read);
7163EXPORT_SYMBOL_GPL(ata_noop_dev_select);
7164EXPORT_SYMBOL_GPL(ata_std_dev_select);
Jeff Garzik43727fb2007-02-25 16:50:52 -05007165EXPORT_SYMBOL_GPL(sata_print_link_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007166EXPORT_SYMBOL_GPL(ata_tf_to_fis);
7167EXPORT_SYMBOL_GPL(ata_tf_from_fis);
7168EXPORT_SYMBOL_GPL(ata_check_status);
7169EXPORT_SYMBOL_GPL(ata_altstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007170EXPORT_SYMBOL_GPL(ata_exec_command);
7171EXPORT_SYMBOL_GPL(ata_port_start);
Alan Coxd92e74d2007-06-07 16:19:15 +01007172EXPORT_SYMBOL_GPL(ata_sff_port_start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007173EXPORT_SYMBOL_GPL(ata_interrupt);
Alan04351822007-03-06 02:37:52 -08007174EXPORT_SYMBOL_GPL(ata_do_set_mode);
Tejun Heo0d5ff562007-02-01 15:06:36 +09007175EXPORT_SYMBOL_GPL(ata_data_xfer);
7176EXPORT_SYMBOL_GPL(ata_data_xfer_noirq);
Tejun Heo31cc23b2007-09-23 13:14:12 +09007177EXPORT_SYMBOL_GPL(ata_std_qc_defer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007178EXPORT_SYMBOL_GPL(ata_qc_prep);
Alan Coxd26fc952007-07-06 19:13:52 -04007179EXPORT_SYMBOL_GPL(ata_dumb_qc_prep);
Brian Kinge46834c2006-03-17 17:04:03 -06007180EXPORT_SYMBOL_GPL(ata_noop_qc_prep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007181EXPORT_SYMBOL_GPL(ata_bmdma_setup);
7182EXPORT_SYMBOL_GPL(ata_bmdma_start);
7183EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
7184EXPORT_SYMBOL_GPL(ata_bmdma_status);
7185EXPORT_SYMBOL_GPL(ata_bmdma_stop);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09007186EXPORT_SYMBOL_GPL(ata_bmdma_freeze);
7187EXPORT_SYMBOL_GPL(ata_bmdma_thaw);
7188EXPORT_SYMBOL_GPL(ata_bmdma_drive_eh);
7189EXPORT_SYMBOL_GPL(ata_bmdma_error_handler);
7190EXPORT_SYMBOL_GPL(ata_bmdma_post_internal_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007191EXPORT_SYMBOL_GPL(ata_port_probe);
Alan10305f02007-02-20 18:01:59 +00007192EXPORT_SYMBOL_GPL(ata_dev_disable);
Tejun Heo3c567b72006-05-15 20:57:23 +09007193EXPORT_SYMBOL_GPL(sata_set_spd);
Tejun Heo936fd732007-08-06 18:36:23 +09007194EXPORT_SYMBOL_GPL(sata_link_debounce);
7195EXPORT_SYMBOL_GPL(sata_link_resume);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007196EXPORT_SYMBOL_GPL(sata_phy_reset);
7197EXPORT_SYMBOL_GPL(__sata_phy_reset);
7198EXPORT_SYMBOL_GPL(ata_bus_reset);
Tejun Heof5914a42006-05-31 18:27:48 +09007199EXPORT_SYMBOL_GPL(ata_std_prereset);
Tejun Heoc2bd5802006-01-24 17:05:22 +09007200EXPORT_SYMBOL_GPL(ata_std_softreset);
Tejun Heocc0680a2007-08-06 18:36:23 +09007201EXPORT_SYMBOL_GPL(sata_link_hardreset);
Tejun Heoc2bd5802006-01-24 17:05:22 +09007202EXPORT_SYMBOL_GPL(sata_std_hardreset);
7203EXPORT_SYMBOL_GPL(ata_std_postreset);
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05007204EXPORT_SYMBOL_GPL(ata_dev_classify);
7205EXPORT_SYMBOL_GPL(ata_dev_pair);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007206EXPORT_SYMBOL_GPL(ata_port_disable);
Jeff Garzik67846b32005-10-05 02:58:32 -04007207EXPORT_SYMBOL_GPL(ata_ratelimit);
Tejun Heoc22daff2006-04-11 22:22:29 +09007208EXPORT_SYMBOL_GPL(ata_wait_register);
Tejun Heo6f8b9952006-01-24 17:05:21 +09007209EXPORT_SYMBOL_GPL(ata_busy_sleep);
Tejun Heod4b2bab2007-02-02 16:50:52 +09007210EXPORT_SYMBOL_GPL(ata_wait_ready);
Tejun Heo86e45b62006-03-05 15:29:09 +09007211EXPORT_SYMBOL_GPL(ata_port_queue_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007212EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
7213EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007214EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
Tejun Heo83c47bc2006-05-31 18:28:07 +09007215EXPORT_SYMBOL_GPL(ata_scsi_slave_destroy);
Tejun Heoa6e6ce82006-05-15 21:03:48 +09007216EXPORT_SYMBOL_GPL(ata_scsi_change_queue_depth);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007217EXPORT_SYMBOL_GPL(ata_host_intr);
Tejun Heo34bf2172006-05-15 20:57:46 +09007218EXPORT_SYMBOL_GPL(sata_scr_valid);
7219EXPORT_SYMBOL_GPL(sata_scr_read);
7220EXPORT_SYMBOL_GPL(sata_scr_write);
7221EXPORT_SYMBOL_GPL(sata_scr_write_flush);
Tejun Heo936fd732007-08-06 18:36:23 +09007222EXPORT_SYMBOL_GPL(ata_link_online);
7223EXPORT_SYMBOL_GPL(ata_link_offline);
Tejun Heo6ffa01d2007-03-02 17:32:47 +09007224#ifdef CONFIG_PM
Jeff Garzikcca39742006-08-24 03:19:22 -04007225EXPORT_SYMBOL_GPL(ata_host_suspend);
7226EXPORT_SYMBOL_GPL(ata_host_resume);
Tejun Heo6ffa01d2007-03-02 17:32:47 +09007227#endif /* CONFIG_PM */
Tejun Heo6a62a042006-02-13 10:02:46 +09007228EXPORT_SYMBOL_GPL(ata_id_string);
7229EXPORT_SYMBOL_GPL(ata_id_c_string);
Alan10305f02007-02-20 18:01:59 +00007230EXPORT_SYMBOL_GPL(ata_id_to_dma_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007231EXPORT_SYMBOL_GPL(ata_scsi_simulate);
7232
Alan Cox1bc4ccf2006-01-09 17:18:14 +00007233EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
Alan Cox452503f2005-10-21 19:01:32 -04007234EXPORT_SYMBOL_GPL(ata_timing_compute);
7235EXPORT_SYMBOL_GPL(ata_timing_merge);
7236
Linus Torvalds1da177e2005-04-16 15:20:36 -07007237#ifdef CONFIG_PCI
7238EXPORT_SYMBOL_GPL(pci_test_config_bits);
Tejun Heod583bc12007-07-04 18:02:07 +09007239EXPORT_SYMBOL_GPL(ata_pci_init_sff_host);
Tejun Heo1626aeb2007-05-04 12:43:58 +02007240EXPORT_SYMBOL_GPL(ata_pci_init_bmdma);
Tejun Heod583bc12007-07-04 18:02:07 +09007241EXPORT_SYMBOL_GPL(ata_pci_prepare_sff_host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007242EXPORT_SYMBOL_GPL(ata_pci_init_one);
7243EXPORT_SYMBOL_GPL(ata_pci_remove_one);
Tejun Heo6ffa01d2007-03-02 17:32:47 +09007244#ifdef CONFIG_PM
Tejun Heo500530f2006-07-03 16:07:27 +09007245EXPORT_SYMBOL_GPL(ata_pci_device_do_suspend);
7246EXPORT_SYMBOL_GPL(ata_pci_device_do_resume);
Jens Axboe9b847542006-01-06 09:28:07 +01007247EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
7248EXPORT_SYMBOL_GPL(ata_pci_device_resume);
Tejun Heo6ffa01d2007-03-02 17:32:47 +09007249#endif /* CONFIG_PM */
Alan Cox67951ad2006-03-22 15:55:54 +00007250EXPORT_SYMBOL_GPL(ata_pci_default_filter);
7251EXPORT_SYMBOL_GPL(ata_pci_clear_simplex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007252#endif /* CONFIG_PCI */
Jens Axboe9b847542006-01-06 09:28:07 +01007253
Tejun Heob64bbc32007-07-16 14:29:39 +09007254EXPORT_SYMBOL_GPL(__ata_ehi_push_desc);
7255EXPORT_SYMBOL_GPL(ata_ehi_push_desc);
7256EXPORT_SYMBOL_GPL(ata_ehi_clear_desc);
Tejun Heocbcdd872007-08-18 13:14:55 +09007257EXPORT_SYMBOL_GPL(ata_port_desc);
7258#ifdef CONFIG_PCI
7259EXPORT_SYMBOL_GPL(ata_port_pbar_desc);
7260#endif /* CONFIG_PCI */
Tejun Heoece1d632006-04-02 18:51:53 +09007261EXPORT_SYMBOL_GPL(ata_eng_timeout);
Tejun Heo7b70fc02006-05-15 20:58:07 +09007262EXPORT_SYMBOL_GPL(ata_port_schedule_eh);
Tejun Heodbd82612007-08-06 18:36:23 +09007263EXPORT_SYMBOL_GPL(ata_link_abort);
Tejun Heo7b70fc02006-05-15 20:58:07 +09007264EXPORT_SYMBOL_GPL(ata_port_abort);
Tejun Heoe3180492006-05-15 20:58:09 +09007265EXPORT_SYMBOL_GPL(ata_port_freeze);
Tejun Heo7d77b242007-09-23 13:14:13 +09007266EXPORT_SYMBOL_GPL(sata_async_notification);
Tejun Heoe3180492006-05-15 20:58:09 +09007267EXPORT_SYMBOL_GPL(ata_eh_freeze_port);
7268EXPORT_SYMBOL_GPL(ata_eh_thaw_port);
Tejun Heoece1d632006-04-02 18:51:53 +09007269EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
7270EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
Tejun Heo022bdb02006-05-15 20:58:22 +09007271EXPORT_SYMBOL_GPL(ata_do_eh);
Akira Iguchi836250062007-01-26 16:27:32 +09007272EXPORT_SYMBOL_GPL(ata_irq_on);
Akira Iguchia619f981b2007-01-26 16:28:18 +09007273EXPORT_SYMBOL_GPL(ata_dev_try_classify);
Alan Coxbe0d18d2007-03-06 02:37:56 -08007274
7275EXPORT_SYMBOL_GPL(ata_cable_40wire);
7276EXPORT_SYMBOL_GPL(ata_cable_80wire);
7277EXPORT_SYMBOL_GPL(ata_cable_unknown);
7278EXPORT_SYMBOL_GPL(ata_cable_sata);