blob: 86f405b4831cc95d8fe2fa8e76580a1e9587223e [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 *
Alan Cox92c52c52007-11-19 14:30:16 +000033 * Standards documents from:
34 * http://www.t13.org (ATA standards, PCI DMA IDE spec)
35 * http://www.t10.org (SCSI MMC - for ATAPI MMC)
36 * http://www.sata-io.org (SATA)
37 * http://www.compactflash.org (CF)
38 * http://www.qic.org (QIC157 - Tape and DSC)
39 * http://www.ce-ata.org (CE-ATA: not supported)
40 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 */
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043#include <linux/kernel.h>
44#include <linux/module.h>
45#include <linux/pci.h>
46#include <linux/init.h>
47#include <linux/list.h>
48#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070049#include <linux/spinlock.h>
50#include <linux/blkdev.h>
51#include <linux/delay.h>
52#include <linux/timer.h>
53#include <linux/interrupt.h>
54#include <linux/completion.h>
55#include <linux/suspend.h>
56#include <linux/workqueue.h>
David Hardeman378f0582005-09-17 17:55:31 +100057#include <linux/scatterlist.h>
Jeff Garzik2dcb4072007-10-19 06:42:56 -040058#include <linux/io.h>
Arjan van de Ven79318052009-01-04 05:32:28 -080059#include <linux/async.h>
Mark Lorde18086d2009-03-19 13:32:21 -040060#include <linux/log2.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090061#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#include <scsi/scsi.h>
Jeff Garzik193515d2005-11-07 00:59:37 -050063#include <scsi/scsi_cmnd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#include <scsi/scsi_host.h>
65#include <linux/libata.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#include <asm/byteorder.h>
Tejun Heo140b5e592007-12-12 12:21:52 +090067#include <linux/cdrom.h>
Akinobu Mita9990b6f2010-04-12 21:11:41 +090068#include <linux/ratelimit.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70#include "libata.h"
71
Jeff Garzikfda0efc2007-01-31 07:43:15 -050072
Tejun Heod7bb4cc2006-05-31 18:27:46 +090073/* debounce timing parameters in msecs { interval, duration, timeout } */
Tejun Heoe9c83912006-07-03 16:07:26 +090074const unsigned long sata_deb_timing_normal[] = { 5, 100, 2000 };
75const unsigned long sata_deb_timing_hotplug[] = { 25, 500, 2000 };
76const unsigned long sata_deb_timing_long[] = { 100, 2000, 5000 };
Tejun Heod7bb4cc2006-05-31 18:27:46 +090077
Tejun Heo029cfd62008-03-25 12:22:49 +090078const struct ata_port_operations ata_base_port_ops = {
Tejun Heo0aa11132008-04-07 22:47:18 +090079 .prereset = ata_std_prereset,
Tejun Heo203c75b2008-04-07 22:47:18 +090080 .postreset = ata_std_postreset,
Tejun Heoa1efdab2008-03-25 12:22:50 +090081 .error_handler = ata_std_error_handler,
Tejun Heo029cfd62008-03-25 12:22:49 +090082};
83
84const struct ata_port_operations sata_port_ops = {
85 .inherits = &ata_base_port_ops,
86
87 .qc_defer = ata_std_qc_defer,
Tejun Heo57c9efd2008-04-07 22:47:19 +090088 .hardreset = sata_std_hardreset,
Tejun Heo029cfd62008-03-25 12:22:49 +090089};
90
Tejun Heo3373efd2006-05-15 20:57:53 +090091static unsigned int ata_dev_init_params(struct ata_device *dev,
92 u16 heads, u16 sectors);
93static unsigned int ata_dev_set_xfermode(struct ata_device *dev);
Jeff Garzik218f3d32007-10-25 00:33:27 -040094static unsigned int ata_dev_set_feature(struct ata_device *dev,
95 u8 enable, u8 feature);
Tejun Heo3373efd2006-05-15 20:57:53 +090096static void ata_dev_xfermask(struct ata_device *dev);
Tejun Heo75683fe2007-07-05 13:31:27 +090097static unsigned long ata_dev_blacklisted(const struct ata_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Tejun Heof3187192007-04-17 23:44:07 +090099unsigned int ata_print_id = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100static struct workqueue_struct *ata_wq;
101
Tejun Heo453b07a2006-05-31 18:27:42 +0900102struct workqueue_struct *ata_aux_wq;
103
Tejun Heo33267322008-02-13 09:15:09 +0900104struct ata_force_param {
105 const char *name;
106 unsigned int cbl;
107 int spd_limit;
108 unsigned long xfer_mask;
109 unsigned int horkage_on;
110 unsigned int horkage_off;
Tejun Heo05944bd2008-08-13 20:19:09 +0900111 unsigned int lflags;
Tejun Heo33267322008-02-13 09:15:09 +0900112};
113
114struct ata_force_ent {
115 int port;
116 int device;
117 struct ata_force_param param;
118};
119
120static struct ata_force_ent *ata_force_tbl;
121static int ata_force_tbl_size;
122
123static char ata_force_param_buf[PAGE_SIZE] __initdata;
Tejun Heo7afb4222008-03-09 20:21:53 +0900124/* param_buf is thrown away after initialization, disallow read */
125module_param_string(force, ata_force_param_buf, sizeof(ata_force_param_buf), 0);
Tejun Heo33267322008-02-13 09:15:09 +0900126MODULE_PARM_DESC(force, "Force ATA configurations including cable type, link speed and transfer mode (see Documentation/kernel-parameters.txt for details)");
127
Tejun Heo2486fa52008-07-31 07:52:40 +0900128static int atapi_enabled = 1;
Jeff Garzik1623c812005-08-30 03:37:42 -0400129module_param(atapi_enabled, int, 0444);
Evgeni Golovad5d8ea2009-06-22 09:46:20 +0200130MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on [default])");
Jeff Garzik1623c812005-08-30 03:37:42 -0400131
Adrian Bunkc5c61bd2008-02-25 02:07:25 +0200132static int atapi_dmadir = 0;
Albert Lee95de7192006-04-04 10:57:18 +0800133module_param(atapi_dmadir, int, 0444);
Evgeni Golovad5d8ea2009-06-22 09:46:20 +0200134MODULE_PARM_DESC(atapi_dmadir, "Enable ATAPI DMADIR bridge support (0=off [default], 1=on)");
Albert Lee95de7192006-04-04 10:57:18 +0800135
Mark Lordbaf4fdf2007-08-08 01:08:45 +0900136int atapi_passthru16 = 1;
137module_param(atapi_passthru16, int, 0444);
Evgeni Golovad5d8ea2009-06-22 09:46:20 +0200138MODULE_PARM_DESC(atapi_passthru16, "Enable ATA_16 passthru for ATAPI devices (0=off, 1=on [default])");
Mark Lordbaf4fdf2007-08-08 01:08:45 +0900139
Jeff Garzikc3c013a2006-02-27 22:31:19 -0500140int libata_fua = 0;
141module_param_named(fua, libata_fua, int, 0444);
Evgeni Golovad5d8ea2009-06-22 09:46:20 +0200142MODULE_PARM_DESC(fua, "FUA support (0=off [default], 1=on)");
Jeff Garzikc3c013a2006-02-27 22:31:19 -0500143
Jeff Garzik2dcb4072007-10-19 06:42:56 -0400144static int ata_ignore_hpa;
Alan Cox1e999732007-04-11 00:23:13 +0100145module_param_named(ignore_hpa, ata_ignore_hpa, int, 0644);
146MODULE_PARM_DESC(ignore_hpa, "Ignore HPA limit (0=keep BIOS limits, 1=ignore limits, using full disk)");
147
Alan Coxb3a70602007-10-02 12:38:26 -0400148static int libata_dma_mask = ATA_DMA_MASK_ATA|ATA_DMA_MASK_ATAPI|ATA_DMA_MASK_CFA;
149module_param_named(dma, libata_dma_mask, int, 0444);
150MODULE_PARM_DESC(dma, "DMA enable/disable (0x1==ATA, 0x2==ATAPI, 0x4==CF)");
151
Tejun Heo87fbc5a2008-05-20 02:17:54 +0900152static int ata_probe_timeout;
Andrew Mortona8601e52006-06-25 01:36:52 -0700153module_param(ata_probe_timeout, int, 0444);
154MODULE_PARM_DESC(ata_probe_timeout, "Set ATA probing timeout (seconds)");
155
Jeff Garzik6ebe9d82007-10-05 16:28:36 -0400156int libata_noacpi = 0;
Jeff Garzikd7d0dad2007-03-28 01:57:37 -0400157module_param_named(noacpi, libata_noacpi, int, 0444);
Evgeni Golovad5d8ea2009-06-22 09:46:20 +0200158MODULE_PARM_DESC(noacpi, "Disable the use of ACPI in probe/suspend/resume (0=off [default], 1=on)");
Kristen Carlson Accardi11ef6972006-09-28 11:29:01 -0700159
Alan Coxae8d4ee2007-11-04 22:05:49 -0500160int libata_allow_tpm = 0;
161module_param_named(allow_tpm, libata_allow_tpm, int, 0444);
Evgeni Golovad5d8ea2009-06-22 09:46:20 +0200162MODULE_PARM_DESC(allow_tpm, "Permit the use of TPM commands (0=off [default], 1=on)");
Alan Coxae8d4ee2007-11-04 22:05:49 -0500163
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164MODULE_AUTHOR("Jeff Garzik");
165MODULE_DESCRIPTION("Library module for ATA devices");
166MODULE_LICENSE("GPL");
167MODULE_VERSION(DRV_VERSION);
168
Edward Falk0baab862005-06-02 18:17:13 -0400169
Tejun Heo9913ff82009-01-29 20:31:31 +0900170static bool ata_sstatus_online(u32 sstatus)
171{
172 return (sstatus & 0xf) == 0x3;
173}
174
Tejun Heo1eca4362008-11-03 20:03:17 +0900175/**
176 * ata_link_next - link iteration helper
177 * @link: the previous link, NULL to start
178 * @ap: ATA port containing links to iterate
179 * @mode: iteration mode, one of ATA_LITER_*
Tejun Heoaadffb62008-07-31 17:02:41 +0900180 *
Tejun Heo1eca4362008-11-03 20:03:17 +0900181 * LOCKING:
182 * Host lock or EH context.
183 *
184 * RETURNS:
185 * Pointer to the next link.
Tejun Heoaadffb62008-07-31 17:02:41 +0900186 */
Tejun Heo1eca4362008-11-03 20:03:17 +0900187struct ata_link *ata_link_next(struct ata_link *link, struct ata_port *ap,
188 enum ata_link_iter_mode mode)
Tejun Heoaadffb62008-07-31 17:02:41 +0900189{
Tejun Heo1eca4362008-11-03 20:03:17 +0900190 BUG_ON(mode != ATA_LITER_EDGE &&
191 mode != ATA_LITER_PMP_FIRST && mode != ATA_LITER_HOST_FIRST);
Tejun Heoaadffb62008-07-31 17:02:41 +0900192
Tejun Heo1eca4362008-11-03 20:03:17 +0900193 /* NULL link indicates start of iteration */
194 if (!link)
195 switch (mode) {
196 case ATA_LITER_EDGE:
197 case ATA_LITER_PMP_FIRST:
198 if (sata_pmp_attached(ap))
199 return ap->pmp_link;
200 /* fall through */
201 case ATA_LITER_HOST_FIRST:
202 return &ap->link;
203 }
204
205 /* we just iterated over the host link, what's next? */
206 if (link == &ap->link)
207 switch (mode) {
208 case ATA_LITER_HOST_FIRST:
209 if (sata_pmp_attached(ap))
210 return ap->pmp_link;
211 /* fall through */
212 case ATA_LITER_PMP_FIRST:
213 if (unlikely(ap->slave_link))
Tejun Heob1c72912008-07-31 17:02:43 +0900214 return ap->slave_link;
Tejun Heo1eca4362008-11-03 20:03:17 +0900215 /* fall through */
216 case ATA_LITER_EDGE:
Tejun Heoaadffb62008-07-31 17:02:41 +0900217 return NULL;
Tejun Heob1c72912008-07-31 17:02:43 +0900218 }
Tejun Heoaadffb62008-07-31 17:02:41 +0900219
Tejun Heob1c72912008-07-31 17:02:43 +0900220 /* slave_link excludes PMP */
221 if (unlikely(link == ap->slave_link))
222 return NULL;
223
Tejun Heo1eca4362008-11-03 20:03:17 +0900224 /* we were over a PMP link */
Tejun Heoaadffb62008-07-31 17:02:41 +0900225 if (++link < ap->pmp_link + ap->nr_pmp_links)
226 return link;
Tejun Heo1eca4362008-11-03 20:03:17 +0900227
228 if (mode == ATA_LITER_PMP_FIRST)
229 return &ap->link;
230
Tejun Heoaadffb62008-07-31 17:02:41 +0900231 return NULL;
232}
233
Edward Falk0baab862005-06-02 18:17:13 -0400234/**
Tejun Heo1eca4362008-11-03 20:03:17 +0900235 * ata_dev_next - device iteration helper
236 * @dev: the previous device, NULL to start
237 * @link: ATA link containing devices to iterate
238 * @mode: iteration mode, one of ATA_DITER_*
239 *
240 * LOCKING:
241 * Host lock or EH context.
242 *
243 * RETURNS:
244 * Pointer to the next device.
245 */
246struct ata_device *ata_dev_next(struct ata_device *dev, struct ata_link *link,
247 enum ata_dev_iter_mode mode)
248{
249 BUG_ON(mode != ATA_DITER_ENABLED && mode != ATA_DITER_ENABLED_REVERSE &&
250 mode != ATA_DITER_ALL && mode != ATA_DITER_ALL_REVERSE);
251
252 /* NULL dev indicates start of iteration */
253 if (!dev)
254 switch (mode) {
255 case ATA_DITER_ENABLED:
256 case ATA_DITER_ALL:
257 dev = link->device;
258 goto check;
259 case ATA_DITER_ENABLED_REVERSE:
260 case ATA_DITER_ALL_REVERSE:
261 dev = link->device + ata_link_max_devices(link) - 1;
262 goto check;
263 }
264
265 next:
266 /* move to the next one */
267 switch (mode) {
268 case ATA_DITER_ENABLED:
269 case ATA_DITER_ALL:
270 if (++dev < link->device + ata_link_max_devices(link))
271 goto check;
272 return NULL;
273 case ATA_DITER_ENABLED_REVERSE:
274 case ATA_DITER_ALL_REVERSE:
275 if (--dev >= link->device)
276 goto check;
277 return NULL;
278 }
279
280 check:
281 if ((mode == ATA_DITER_ENABLED || mode == ATA_DITER_ENABLED_REVERSE) &&
282 !ata_dev_enabled(dev))
283 goto next;
284 return dev;
285}
286
287/**
Tejun Heob1c72912008-07-31 17:02:43 +0900288 * ata_dev_phys_link - find physical link for a device
289 * @dev: ATA device to look up physical link for
290 *
291 * Look up physical link which @dev is attached to. Note that
292 * this is different from @dev->link only when @dev is on slave
293 * link. For all other cases, it's the same as @dev->link.
294 *
295 * LOCKING:
296 * Don't care.
297 *
298 * RETURNS:
299 * Pointer to the found physical link.
300 */
301struct ata_link *ata_dev_phys_link(struct ata_device *dev)
302{
303 struct ata_port *ap = dev->link->ap;
304
305 if (!ap->slave_link)
306 return dev->link;
307 if (!dev->devno)
308 return &ap->link;
309 return ap->slave_link;
310}
311
312/**
Tejun Heo33267322008-02-13 09:15:09 +0900313 * ata_force_cbl - force cable type according to libata.force
Randy Dunlap4cdfa1b2008-02-22 12:21:37 -0800314 * @ap: ATA port of interest
Tejun Heo33267322008-02-13 09:15:09 +0900315 *
316 * Force cable type according to libata.force and whine about it.
317 * The last entry which has matching port number is used, so it
318 * can be specified as part of device force parameters. For
319 * example, both "a:40c,1.00:udma4" and "1.00:40c,udma4" have the
320 * same effect.
321 *
322 * LOCKING:
323 * EH context.
324 */
325void ata_force_cbl(struct ata_port *ap)
326{
327 int i;
328
329 for (i = ata_force_tbl_size - 1; i >= 0; i--) {
330 const struct ata_force_ent *fe = &ata_force_tbl[i];
331
332 if (fe->port != -1 && fe->port != ap->print_id)
333 continue;
334
335 if (fe->param.cbl == ATA_CBL_NONE)
336 continue;
337
338 ap->cbl = fe->param.cbl;
339 ata_port_printk(ap, KERN_NOTICE,
340 "FORCE: cable set to %s\n", fe->param.name);
341 return;
342 }
343}
344
345/**
Tejun Heo05944bd2008-08-13 20:19:09 +0900346 * ata_force_link_limits - force link limits according to libata.force
Tejun Heo33267322008-02-13 09:15:09 +0900347 * @link: ATA link of interest
348 *
Tejun Heo05944bd2008-08-13 20:19:09 +0900349 * Force link flags and SATA spd limit according to libata.force
350 * and whine about it. When only the port part is specified
351 * (e.g. 1:), the limit applies to all links connected to both
352 * the host link and all fan-out ports connected via PMP. If the
353 * device part is specified as 0 (e.g. 1.00:), it specifies the
354 * first fan-out link not the host link. Device number 15 always
Tejun Heob1c72912008-07-31 17:02:43 +0900355 * points to the host link whether PMP is attached or not. If the
356 * controller has slave link, device number 16 points to it.
Tejun Heo33267322008-02-13 09:15:09 +0900357 *
358 * LOCKING:
359 * EH context.
360 */
Tejun Heo05944bd2008-08-13 20:19:09 +0900361static void ata_force_link_limits(struct ata_link *link)
Tejun Heo33267322008-02-13 09:15:09 +0900362{
Tejun Heo05944bd2008-08-13 20:19:09 +0900363 bool did_spd = false;
Tejun Heob1c72912008-07-31 17:02:43 +0900364 int linkno = link->pmp;
365 int i;
Tejun Heo33267322008-02-13 09:15:09 +0900366
367 if (ata_is_host_link(link))
Tejun Heob1c72912008-07-31 17:02:43 +0900368 linkno += 15;
Tejun Heo33267322008-02-13 09:15:09 +0900369
370 for (i = ata_force_tbl_size - 1; i >= 0; i--) {
371 const struct ata_force_ent *fe = &ata_force_tbl[i];
372
373 if (fe->port != -1 && fe->port != link->ap->print_id)
374 continue;
375
376 if (fe->device != -1 && fe->device != linkno)
377 continue;
378
Tejun Heo05944bd2008-08-13 20:19:09 +0900379 /* only honor the first spd limit */
380 if (!did_spd && fe->param.spd_limit) {
381 link->hw_sata_spd_limit = (1 << fe->param.spd_limit) - 1;
382 ata_link_printk(link, KERN_NOTICE,
383 "FORCE: PHY spd limit set to %s\n",
384 fe->param.name);
385 did_spd = true;
386 }
Tejun Heo33267322008-02-13 09:15:09 +0900387
Tejun Heo05944bd2008-08-13 20:19:09 +0900388 /* let lflags stack */
389 if (fe->param.lflags) {
390 link->flags |= fe->param.lflags;
391 ata_link_printk(link, KERN_NOTICE,
392 "FORCE: link flag 0x%x forced -> 0x%x\n",
393 fe->param.lflags, link->flags);
394 }
Tejun Heo33267322008-02-13 09:15:09 +0900395 }
396}
397
398/**
399 * ata_force_xfermask - force xfermask according to libata.force
400 * @dev: ATA device of interest
401 *
402 * Force xfer_mask according to libata.force and whine about it.
403 * For consistency with link selection, device number 15 selects
404 * the first device connected to the host link.
405 *
406 * LOCKING:
407 * EH context.
408 */
409static void ata_force_xfermask(struct ata_device *dev)
410{
411 int devno = dev->link->pmp + dev->devno;
412 int alt_devno = devno;
413 int i;
414
Tejun Heob1c72912008-07-31 17:02:43 +0900415 /* allow n.15/16 for devices attached to host port */
416 if (ata_is_host_link(dev->link))
417 alt_devno += 15;
Tejun Heo33267322008-02-13 09:15:09 +0900418
419 for (i = ata_force_tbl_size - 1; i >= 0; i--) {
420 const struct ata_force_ent *fe = &ata_force_tbl[i];
421 unsigned long pio_mask, mwdma_mask, udma_mask;
422
423 if (fe->port != -1 && fe->port != dev->link->ap->print_id)
424 continue;
425
426 if (fe->device != -1 && fe->device != devno &&
427 fe->device != alt_devno)
428 continue;
429
430 if (!fe->param.xfer_mask)
431 continue;
432
433 ata_unpack_xfermask(fe->param.xfer_mask,
434 &pio_mask, &mwdma_mask, &udma_mask);
435 if (udma_mask)
436 dev->udma_mask = udma_mask;
437 else if (mwdma_mask) {
438 dev->udma_mask = 0;
439 dev->mwdma_mask = mwdma_mask;
440 } else {
441 dev->udma_mask = 0;
442 dev->mwdma_mask = 0;
443 dev->pio_mask = pio_mask;
444 }
445
446 ata_dev_printk(dev, KERN_NOTICE,
447 "FORCE: xfer_mask set to %s\n", fe->param.name);
448 return;
449 }
450}
451
452/**
453 * ata_force_horkage - force horkage according to libata.force
454 * @dev: ATA device of interest
455 *
456 * Force horkage according to libata.force and whine about it.
457 * For consistency with link selection, device number 15 selects
458 * the first device connected to the host link.
459 *
460 * LOCKING:
461 * EH context.
462 */
463static void ata_force_horkage(struct ata_device *dev)
464{
465 int devno = dev->link->pmp + dev->devno;
466 int alt_devno = devno;
467 int i;
468
Tejun Heob1c72912008-07-31 17:02:43 +0900469 /* allow n.15/16 for devices attached to host port */
470 if (ata_is_host_link(dev->link))
471 alt_devno += 15;
Tejun Heo33267322008-02-13 09:15:09 +0900472
473 for (i = 0; i < ata_force_tbl_size; i++) {
474 const struct ata_force_ent *fe = &ata_force_tbl[i];
475
476 if (fe->port != -1 && fe->port != dev->link->ap->print_id)
477 continue;
478
479 if (fe->device != -1 && fe->device != devno &&
480 fe->device != alt_devno)
481 continue;
482
483 if (!(~dev->horkage & fe->param.horkage_on) &&
484 !(dev->horkage & fe->param.horkage_off))
485 continue;
486
487 dev->horkage |= fe->param.horkage_on;
488 dev->horkage &= ~fe->param.horkage_off;
489
490 ata_dev_printk(dev, KERN_NOTICE,
491 "FORCE: horkage modified (%s)\n", fe->param.name);
492 }
493}
494
495/**
Tejun Heo436d34b2008-04-02 17:28:46 +0900496 * atapi_cmd_type - Determine ATAPI command type from SCSI opcode
497 * @opcode: SCSI opcode
498 *
499 * Determine ATAPI command type from @opcode.
500 *
501 * LOCKING:
502 * None.
503 *
504 * RETURNS:
505 * ATAPI_{READ|WRITE|READ_CD|PASS_THRU|MISC}
506 */
507int atapi_cmd_type(u8 opcode)
508{
509 switch (opcode) {
510 case GPCMD_READ_10:
511 case GPCMD_READ_12:
512 return ATAPI_READ;
513
514 case GPCMD_WRITE_10:
515 case GPCMD_WRITE_12:
516 case GPCMD_WRITE_AND_VERIFY_10:
517 return ATAPI_WRITE;
518
519 case GPCMD_READ_CD:
520 case GPCMD_READ_CD_MSF:
521 return ATAPI_READ_CD;
522
Tejun Heoe52dcc42008-04-02 17:35:19 +0900523 case ATA_16:
524 case ATA_12:
525 if (atapi_passthru16)
526 return ATAPI_PASS_THRU;
527 /* fall thru */
Tejun Heo436d34b2008-04-02 17:28:46 +0900528 default:
529 return ATAPI_MISC;
530 }
531}
532
533/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
535 * @tf: Taskfile to convert
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 * @pmp: Port multiplier port
Tejun Heo99771262007-07-16 14:29:38 +0900537 * @is_cmd: This FIS is for command
538 * @fis: Buffer into which data will output
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 *
540 * Converts a standard ATA taskfile to a Serial ATA
541 * FIS structure (Register - Host to Device).
542 *
543 * LOCKING:
544 * Inherited from caller.
545 */
Tejun Heo99771262007-07-16 14:29:38 +0900546void ata_tf_to_fis(const struct ata_taskfile *tf, u8 pmp, int is_cmd, u8 *fis)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547{
Tejun Heo99771262007-07-16 14:29:38 +0900548 fis[0] = 0x27; /* Register - Host to Device FIS */
549 fis[1] = pmp & 0xf; /* Port multiplier number*/
550 if (is_cmd)
551 fis[1] |= (1 << 7); /* bit 7 indicates Command FIS */
552
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 fis[2] = tf->command;
554 fis[3] = tf->feature;
555
556 fis[4] = tf->lbal;
557 fis[5] = tf->lbam;
558 fis[6] = tf->lbah;
559 fis[7] = tf->device;
560
561 fis[8] = tf->hob_lbal;
562 fis[9] = tf->hob_lbam;
563 fis[10] = tf->hob_lbah;
564 fis[11] = tf->hob_feature;
565
566 fis[12] = tf->nsect;
567 fis[13] = tf->hob_nsect;
568 fis[14] = 0;
569 fis[15] = tf->ctl;
570
571 fis[16] = 0;
572 fis[17] = 0;
573 fis[18] = 0;
574 fis[19] = 0;
575}
576
577/**
578 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
579 * @fis: Buffer from which data will be input
580 * @tf: Taskfile to output
581 *
Mark Lorde12a1be2005-11-12 18:55:45 -0500582 * Converts a serial ATA FIS structure to a standard ATA taskfile.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 *
584 * LOCKING:
585 * Inherited from caller.
586 */
587
Jeff Garzik057ace52005-10-22 14:27:05 -0400588void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
590 tf->command = fis[2]; /* status */
591 tf->feature = fis[3]; /* error */
592
593 tf->lbal = fis[4];
594 tf->lbam = fis[5];
595 tf->lbah = fis[6];
596 tf->device = fis[7];
597
598 tf->hob_lbal = fis[8];
599 tf->hob_lbam = fis[9];
600 tf->hob_lbah = fis[10];
601
602 tf->nsect = fis[12];
603 tf->hob_nsect = fis[13];
604}
605
Albert Lee8cbd6df2005-10-12 15:06:27 +0800606static const u8 ata_rw_cmds[] = {
607 /* pio multi */
608 ATA_CMD_READ_MULTI,
609 ATA_CMD_WRITE_MULTI,
610 ATA_CMD_READ_MULTI_EXT,
611 ATA_CMD_WRITE_MULTI_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100612 0,
613 0,
614 0,
615 ATA_CMD_WRITE_MULTI_FUA_EXT,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800616 /* pio */
617 ATA_CMD_PIO_READ,
618 ATA_CMD_PIO_WRITE,
619 ATA_CMD_PIO_READ_EXT,
620 ATA_CMD_PIO_WRITE_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100621 0,
622 0,
623 0,
624 0,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800625 /* dma */
626 ATA_CMD_READ,
627 ATA_CMD_WRITE,
628 ATA_CMD_READ_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100629 ATA_CMD_WRITE_EXT,
630 0,
631 0,
632 0,
633 ATA_CMD_WRITE_FUA_EXT
Albert Lee8cbd6df2005-10-12 15:06:27 +0800634};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
636/**
Albert Lee8cbd6df2005-10-12 15:06:27 +0800637 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
Tejun Heobd056d72006-11-14 22:47:10 +0900638 * @tf: command to examine and configure
639 * @dev: device tf belongs to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 *
Jeff Garzik2e9edbf2006-03-24 09:56:57 -0500641 * Examine the device configuration and tf->flags to calculate
Albert Lee8cbd6df2005-10-12 15:06:27 +0800642 * the proper read/write commands and protocol to use.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 *
644 * LOCKING:
645 * caller.
646 */
Tejun Heobd056d72006-11-14 22:47:10 +0900647static int ata_rwcmd_protocol(struct ata_taskfile *tf, struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100649 u8 cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100651 int index, fua, lba48, write;
Jeff Garzik2e9edbf2006-03-24 09:56:57 -0500652
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100653 fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800654 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
655 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Albert Lee8cbd6df2005-10-12 15:06:27 +0800657 if (dev->flags & ATA_DFLAG_PIO) {
658 tf->protocol = ATA_PROT_PIO;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100659 index = dev->multi_count ? 0 : 8;
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900660 } else if (lba48 && (dev->link->ap->flags & ATA_FLAG_PIO_LBA48)) {
Alan Cox8d238e02006-01-17 20:50:31 +0000661 /* Unable to use DMA due to host limitation */
662 tf->protocol = ATA_PROT_PIO;
Albert Lee0565c262006-02-13 18:55:25 +0800663 index = dev->multi_count ? 0 : 8;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800664 } else {
665 tf->protocol = ATA_PROT_DMA;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100666 index = 16;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800667 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100669 cmd = ata_rw_cmds[index + fua + lba48 + write];
670 if (cmd) {
671 tf->command = cmd;
672 return 0;
673 }
674 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675}
676
Tejun Heocb95d562006-03-06 04:31:56 +0900677/**
Tejun Heo35b649f2006-11-14 22:37:35 +0900678 * ata_tf_read_block - Read block address from ATA taskfile
679 * @tf: ATA taskfile of interest
680 * @dev: ATA device @tf belongs to
681 *
682 * LOCKING:
683 * None.
684 *
685 * Read block address from @tf. This function can handle all
686 * three address formats - LBA, LBA48 and CHS. tf->protocol and
687 * flags select the address format to use.
688 *
689 * RETURNS:
690 * Block address read from @tf.
691 */
692u64 ata_tf_read_block(struct ata_taskfile *tf, struct ata_device *dev)
693{
694 u64 block = 0;
695
696 if (tf->flags & ATA_TFLAG_LBA) {
697 if (tf->flags & ATA_TFLAG_LBA48) {
698 block |= (u64)tf->hob_lbah << 40;
699 block |= (u64)tf->hob_lbam << 32;
Roland Dreier44901a92008-11-04 10:34:48 -0800700 block |= (u64)tf->hob_lbal << 24;
Tejun Heo35b649f2006-11-14 22:37:35 +0900701 } else
702 block |= (tf->device & 0xf) << 24;
703
704 block |= tf->lbah << 16;
705 block |= tf->lbam << 8;
706 block |= tf->lbal;
707 } else {
708 u32 cyl, head, sect;
709
710 cyl = tf->lbam | (tf->lbah << 8);
711 head = tf->device & 0xf;
712 sect = tf->lbal;
713
Tejun Heoac8672e2009-08-16 21:21:21 +0900714 if (!sect) {
715 ata_dev_printk(dev, KERN_WARNING, "device reported "
716 "invalid CHS sector 0\n");
717 sect = 1; /* oh well */
718 }
719
720 block = (cyl * dev->heads + head) * dev->sectors + sect - 1;
Tejun Heo35b649f2006-11-14 22:37:35 +0900721 }
722
723 return block;
724}
725
726/**
Tejun Heobd056d72006-11-14 22:47:10 +0900727 * ata_build_rw_tf - Build ATA taskfile for given read/write request
728 * @tf: Target ATA taskfile
729 * @dev: ATA device @tf belongs to
730 * @block: Block address
731 * @n_block: Number of blocks
732 * @tf_flags: RW/FUA etc...
733 * @tag: tag
734 *
735 * LOCKING:
736 * None.
737 *
738 * Build ATA taskfile @tf for read/write request described by
739 * @block, @n_block, @tf_flags and @tag on @dev.
740 *
741 * RETURNS:
742 *
743 * 0 on success, -ERANGE if the request is too large for @dev,
744 * -EINVAL if the request is invalid.
745 */
746int ata_build_rw_tf(struct ata_taskfile *tf, struct ata_device *dev,
747 u64 block, u32 n_block, unsigned int tf_flags,
748 unsigned int tag)
749{
750 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
751 tf->flags |= tf_flags;
752
Tejun Heo6d1245b2007-02-20 23:20:27 +0900753 if (ata_ncq_enabled(dev) && likely(tag != ATA_TAG_INTERNAL)) {
Tejun Heobd056d72006-11-14 22:47:10 +0900754 /* yay, NCQ */
755 if (!lba_48_ok(block, n_block))
756 return -ERANGE;
757
758 tf->protocol = ATA_PROT_NCQ;
759 tf->flags |= ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
760
761 if (tf->flags & ATA_TFLAG_WRITE)
762 tf->command = ATA_CMD_FPDMA_WRITE;
763 else
764 tf->command = ATA_CMD_FPDMA_READ;
765
766 tf->nsect = tag << 3;
767 tf->hob_feature = (n_block >> 8) & 0xff;
768 tf->feature = n_block & 0xff;
769
770 tf->hob_lbah = (block >> 40) & 0xff;
771 tf->hob_lbam = (block >> 32) & 0xff;
772 tf->hob_lbal = (block >> 24) & 0xff;
773 tf->lbah = (block >> 16) & 0xff;
774 tf->lbam = (block >> 8) & 0xff;
775 tf->lbal = block & 0xff;
776
777 tf->device = 1 << 6;
778 if (tf->flags & ATA_TFLAG_FUA)
779 tf->device |= 1 << 7;
780 } else if (dev->flags & ATA_DFLAG_LBA) {
781 tf->flags |= ATA_TFLAG_LBA;
782
783 if (lba_28_ok(block, n_block)) {
784 /* use LBA28 */
785 tf->device |= (block >> 24) & 0xf;
786 } else if (lba_48_ok(block, n_block)) {
787 if (!(dev->flags & ATA_DFLAG_LBA48))
788 return -ERANGE;
789
790 /* use LBA48 */
791 tf->flags |= ATA_TFLAG_LBA48;
792
793 tf->hob_nsect = (n_block >> 8) & 0xff;
794
795 tf->hob_lbah = (block >> 40) & 0xff;
796 tf->hob_lbam = (block >> 32) & 0xff;
797 tf->hob_lbal = (block >> 24) & 0xff;
798 } else
799 /* request too large even for LBA48 */
800 return -ERANGE;
801
802 if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
803 return -EINVAL;
804
805 tf->nsect = n_block & 0xff;
806
807 tf->lbah = (block >> 16) & 0xff;
808 tf->lbam = (block >> 8) & 0xff;
809 tf->lbal = block & 0xff;
810
811 tf->device |= ATA_LBA;
812 } else {
813 /* CHS */
814 u32 sect, head, cyl, track;
815
816 /* The request -may- be too large for CHS addressing. */
817 if (!lba_28_ok(block, n_block))
818 return -ERANGE;
819
820 if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
821 return -EINVAL;
822
823 /* Convert LBA to CHS */
824 track = (u32)block / dev->sectors;
825 cyl = track / dev->heads;
826 head = track % dev->heads;
827 sect = (u32)block % dev->sectors + 1;
828
829 DPRINTK("block %u track %u cyl %u head %u sect %u\n",
830 (u32)block, track, cyl, head, sect);
831
832 /* Check whether the converted CHS can fit.
833 Cylinder: 0-65535
834 Head: 0-15
835 Sector: 1-255*/
836 if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
837 return -ERANGE;
838
839 tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
840 tf->lbal = sect;
841 tf->lbam = cyl;
842 tf->lbah = cyl >> 8;
843 tf->device |= head;
844 }
845
846 return 0;
847}
848
849/**
Tejun Heocb95d562006-03-06 04:31:56 +0900850 * ata_pack_xfermask - Pack pio, mwdma and udma masks into xfer_mask
851 * @pio_mask: pio_mask
852 * @mwdma_mask: mwdma_mask
853 * @udma_mask: udma_mask
854 *
855 * Pack @pio_mask, @mwdma_mask and @udma_mask into a single
856 * unsigned int xfer_mask.
857 *
858 * LOCKING:
859 * None.
860 *
861 * RETURNS:
862 * Packed xfer_mask.
863 */
Tejun Heo7dc951a2007-11-27 19:43:42 +0900864unsigned long ata_pack_xfermask(unsigned long pio_mask,
865 unsigned long mwdma_mask,
866 unsigned long udma_mask)
Tejun Heocb95d562006-03-06 04:31:56 +0900867{
868 return ((pio_mask << ATA_SHIFT_PIO) & ATA_MASK_PIO) |
869 ((mwdma_mask << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA) |
870 ((udma_mask << ATA_SHIFT_UDMA) & ATA_MASK_UDMA);
871}
872
Tejun Heoc0489e42006-03-24 14:07:49 +0900873/**
874 * ata_unpack_xfermask - Unpack xfer_mask into pio, mwdma and udma masks
875 * @xfer_mask: xfer_mask to unpack
876 * @pio_mask: resulting pio_mask
877 * @mwdma_mask: resulting mwdma_mask
878 * @udma_mask: resulting udma_mask
879 *
880 * Unpack @xfer_mask into @pio_mask, @mwdma_mask and @udma_mask.
881 * Any NULL distination masks will be ignored.
882 */
Tejun Heo7dc951a2007-11-27 19:43:42 +0900883void ata_unpack_xfermask(unsigned long xfer_mask, unsigned long *pio_mask,
884 unsigned long *mwdma_mask, unsigned long *udma_mask)
Tejun Heoc0489e42006-03-24 14:07:49 +0900885{
886 if (pio_mask)
887 *pio_mask = (xfer_mask & ATA_MASK_PIO) >> ATA_SHIFT_PIO;
888 if (mwdma_mask)
889 *mwdma_mask = (xfer_mask & ATA_MASK_MWDMA) >> ATA_SHIFT_MWDMA;
890 if (udma_mask)
891 *udma_mask = (xfer_mask & ATA_MASK_UDMA) >> ATA_SHIFT_UDMA;
892}
893
Tejun Heocb95d562006-03-06 04:31:56 +0900894static const struct ata_xfer_ent {
Tejun Heobe9a50c2006-03-31 22:48:52 +0900895 int shift, bits;
Tejun Heocb95d562006-03-06 04:31:56 +0900896 u8 base;
897} ata_xfer_tbl[] = {
Tejun Heo70cd0712007-11-27 19:43:40 +0900898 { ATA_SHIFT_PIO, ATA_NR_PIO_MODES, XFER_PIO_0 },
899 { ATA_SHIFT_MWDMA, ATA_NR_MWDMA_MODES, XFER_MW_DMA_0 },
900 { ATA_SHIFT_UDMA, ATA_NR_UDMA_MODES, XFER_UDMA_0 },
Tejun Heocb95d562006-03-06 04:31:56 +0900901 { -1, },
902};
903
904/**
905 * ata_xfer_mask2mode - Find matching XFER_* for the given xfer_mask
906 * @xfer_mask: xfer_mask of interest
907 *
908 * Return matching XFER_* value for @xfer_mask. Only the highest
909 * bit of @xfer_mask is considered.
910 *
911 * LOCKING:
912 * None.
913 *
914 * RETURNS:
Tejun Heo70cd0712007-11-27 19:43:40 +0900915 * Matching XFER_* value, 0xff if no match found.
Tejun Heocb95d562006-03-06 04:31:56 +0900916 */
Tejun Heo7dc951a2007-11-27 19:43:42 +0900917u8 ata_xfer_mask2mode(unsigned long xfer_mask)
Tejun Heocb95d562006-03-06 04:31:56 +0900918{
919 int highbit = fls(xfer_mask) - 1;
920 const struct ata_xfer_ent *ent;
921
922 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
923 if (highbit >= ent->shift && highbit < ent->shift + ent->bits)
924 return ent->base + highbit - ent->shift;
Tejun Heo70cd0712007-11-27 19:43:40 +0900925 return 0xff;
Tejun Heocb95d562006-03-06 04:31:56 +0900926}
927
928/**
929 * ata_xfer_mode2mask - Find matching xfer_mask for XFER_*
930 * @xfer_mode: XFER_* of interest
931 *
932 * Return matching xfer_mask for @xfer_mode.
933 *
934 * LOCKING:
935 * None.
936 *
937 * RETURNS:
938 * Matching xfer_mask, 0 if no match found.
939 */
Tejun Heo7dc951a2007-11-27 19:43:42 +0900940unsigned long ata_xfer_mode2mask(u8 xfer_mode)
Tejun Heocb95d562006-03-06 04:31:56 +0900941{
942 const struct ata_xfer_ent *ent;
943
944 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
945 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
Tejun Heo70cd0712007-11-27 19:43:40 +0900946 return ((2 << (ent->shift + xfer_mode - ent->base)) - 1)
947 & ~((1 << ent->shift) - 1);
Tejun Heocb95d562006-03-06 04:31:56 +0900948 return 0;
949}
950
951/**
952 * ata_xfer_mode2shift - Find matching xfer_shift for XFER_*
953 * @xfer_mode: XFER_* of interest
954 *
955 * Return matching xfer_shift for @xfer_mode.
956 *
957 * LOCKING:
958 * None.
959 *
960 * RETURNS:
961 * Matching xfer_shift, -1 if no match found.
962 */
Tejun Heo7dc951a2007-11-27 19:43:42 +0900963int ata_xfer_mode2shift(unsigned long xfer_mode)
Tejun Heocb95d562006-03-06 04:31:56 +0900964{
965 const struct ata_xfer_ent *ent;
966
967 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
968 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
969 return ent->shift;
970 return -1;
971}
972
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973/**
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900974 * ata_mode_string - convert xfer_mask to string
975 * @xfer_mask: mask of bits supported; only highest bit counts.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 *
977 * Determine string which represents the highest speed
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900978 * (highest bit in @modemask).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 *
980 * LOCKING:
981 * None.
982 *
983 * RETURNS:
984 * Constant C string representing highest speed listed in
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900985 * @mode_mask, or the constant C string "<n/a>".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 */
Tejun Heo7dc951a2007-11-27 19:43:42 +0900987const char *ata_mode_string(unsigned long xfer_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988{
Tejun Heo75f554b2006-03-06 04:31:57 +0900989 static const char * const xfer_mode_str[] = {
990 "PIO0",
991 "PIO1",
992 "PIO2",
993 "PIO3",
994 "PIO4",
Alan Coxb352e572006-08-10 18:52:12 +0100995 "PIO5",
996 "PIO6",
Tejun Heo75f554b2006-03-06 04:31:57 +0900997 "MWDMA0",
998 "MWDMA1",
999 "MWDMA2",
Alan Coxb352e572006-08-10 18:52:12 +01001000 "MWDMA3",
1001 "MWDMA4",
Tejun Heo75f554b2006-03-06 04:31:57 +09001002 "UDMA/16",
1003 "UDMA/25",
1004 "UDMA/33",
1005 "UDMA/44",
1006 "UDMA/66",
1007 "UDMA/100",
1008 "UDMA/133",
1009 "UDMA7",
1010 };
Tejun Heo1da7b0d2006-03-06 04:31:56 +09001011 int highbit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
Tejun Heo1da7b0d2006-03-06 04:31:56 +09001013 highbit = fls(xfer_mask) - 1;
1014 if (highbit >= 0 && highbit < ARRAY_SIZE(xfer_mode_str))
1015 return xfer_mode_str[highbit];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 return "<n/a>";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017}
1018
Tejun Heo4c360c82006-04-01 01:38:17 +09001019static const char *sata_spd_string(unsigned int spd)
1020{
1021 static const char * const spd_str[] = {
1022 "1.5 Gbps",
1023 "3.0 Gbps",
Shane Huang8522ee22008-12-30 11:00:37 +08001024 "6.0 Gbps",
Tejun Heo4c360c82006-04-01 01:38:17 +09001025 };
1026
1027 if (spd == 0 || (spd - 1) >= ARRAY_SIZE(spd_str))
1028 return "<unknown>";
1029 return spd_str[spd - 1];
1030}
1031
Kristen Carlson Accardica773292007-10-25 00:58:59 -04001032static int ata_dev_set_dipm(struct ata_device *dev, enum link_pm policy)
1033{
1034 struct ata_link *link = dev->link;
1035 struct ata_port *ap = link->ap;
1036 u32 scontrol;
1037 unsigned int err_mask;
1038 int rc;
1039
1040 /*
1041 * disallow DIPM for drivers which haven't set
1042 * ATA_FLAG_IPM. This is because when DIPM is enabled,
1043 * phy ready will be set in the interrupt status on
1044 * state changes, which will cause some drivers to
1045 * think there are errors - additionally drivers will
1046 * need to disable hot plug.
1047 */
1048 if (!(ap->flags & ATA_FLAG_IPM) || !ata_dev_enabled(dev)) {
1049 ap->pm_policy = NOT_AVAILABLE;
1050 return -EINVAL;
1051 }
1052
1053 /*
1054 * For DIPM, we will only enable it for the
1055 * min_power setting.
1056 *
1057 * Why? Because Disks are too stupid to know that
1058 * If the host rejects a request to go to SLUMBER
1059 * they should retry at PARTIAL, and instead it
1060 * just would give up. So, for medium_power to
1061 * work at all, we need to only allow HIPM.
1062 */
1063 rc = sata_scr_read(link, SCR_CONTROL, &scontrol);
1064 if (rc)
1065 return rc;
1066
1067 switch (policy) {
1068 case MIN_POWER:
1069 /* no restrictions on IPM transitions */
1070 scontrol &= ~(0x3 << 8);
1071 rc = sata_scr_write(link, SCR_CONTROL, scontrol);
1072 if (rc)
1073 return rc;
1074
1075 /* enable DIPM */
1076 if (dev->flags & ATA_DFLAG_DIPM)
1077 err_mask = ata_dev_set_feature(dev,
1078 SETFEATURES_SATA_ENABLE, SATA_DIPM);
1079 break;
1080 case MEDIUM_POWER:
1081 /* allow IPM to PARTIAL */
1082 scontrol &= ~(0x1 << 8);
1083 scontrol |= (0x2 << 8);
1084 rc = sata_scr_write(link, SCR_CONTROL, scontrol);
1085 if (rc)
1086 return rc;
1087
Kristen Carlson Accardif5456b62007-11-02 16:37:08 -07001088 /*
1089 * we don't have to disable DIPM since IPM flags
1090 * disallow transitions to SLUMBER, which effectively
1091 * disable DIPM if it does not support PARTIAL
1092 */
Kristen Carlson Accardica773292007-10-25 00:58:59 -04001093 break;
1094 case NOT_AVAILABLE:
1095 case MAX_PERFORMANCE:
1096 /* disable all IPM transitions */
1097 scontrol |= (0x3 << 8);
1098 rc = sata_scr_write(link, SCR_CONTROL, scontrol);
1099 if (rc)
1100 return rc;
1101
Kristen Carlson Accardif5456b62007-11-02 16:37:08 -07001102 /*
1103 * we don't have to disable DIPM since IPM flags
1104 * disallow all transitions which effectively
1105 * disable DIPM anyway.
1106 */
Kristen Carlson Accardica773292007-10-25 00:58:59 -04001107 break;
1108 }
1109
1110 /* FIXME: handle SET FEATURES failure */
1111 (void) err_mask;
1112
1113 return 0;
1114}
1115
1116/**
1117 * ata_dev_enable_pm - enable SATA interface power management
Stephen Hemminger48166fd2007-10-31 10:00:27 -07001118 * @dev: device to enable power management
1119 * @policy: the link power management policy
Kristen Carlson Accardica773292007-10-25 00:58:59 -04001120 *
1121 * Enable SATA Interface power management. This will enable
1122 * Device Interface Power Management (DIPM) for min_power
1123 * policy, and then call driver specific callbacks for
1124 * enabling Host Initiated Power management.
1125 *
1126 * Locking: Caller.
1127 * Returns: -EINVAL if IPM is not supported, 0 otherwise.
1128 */
1129void ata_dev_enable_pm(struct ata_device *dev, enum link_pm policy)
1130{
1131 int rc = 0;
1132 struct ata_port *ap = dev->link->ap;
1133
1134 /* set HIPM first, then DIPM */
1135 if (ap->ops->enable_pm)
1136 rc = ap->ops->enable_pm(ap, policy);
1137 if (rc)
1138 goto enable_pm_out;
1139 rc = ata_dev_set_dipm(dev, policy);
1140
1141enable_pm_out:
1142 if (rc)
1143 ap->pm_policy = MAX_PERFORMANCE;
1144 else
1145 ap->pm_policy = policy;
1146 return /* rc */; /* hopefully we can use 'rc' eventually */
1147}
1148
Stephen Rothwell1992a5e2007-10-31 14:53:32 +11001149#ifdef CONFIG_PM
Kristen Carlson Accardica773292007-10-25 00:58:59 -04001150/**
1151 * ata_dev_disable_pm - disable SATA interface power management
Stephen Hemminger48166fd2007-10-31 10:00:27 -07001152 * @dev: device to disable power management
Kristen Carlson Accardica773292007-10-25 00:58:59 -04001153 *
1154 * Disable SATA Interface power management. This will disable
1155 * Device Interface Power Management (DIPM) without changing
1156 * policy, call driver specific callbacks for disabling Host
1157 * Initiated Power management.
1158 *
1159 * Locking: Caller.
1160 * Returns: void
1161 */
1162static void ata_dev_disable_pm(struct ata_device *dev)
1163{
1164 struct ata_port *ap = dev->link->ap;
1165
1166 ata_dev_set_dipm(dev, MAX_PERFORMANCE);
1167 if (ap->ops->disable_pm)
1168 ap->ops->disable_pm(ap);
1169}
Stephen Rothwell1992a5e2007-10-31 14:53:32 +11001170#endif /* CONFIG_PM */
Kristen Carlson Accardica773292007-10-25 00:58:59 -04001171
1172void ata_lpm_schedule(struct ata_port *ap, enum link_pm policy)
1173{
1174 ap->pm_policy = policy;
Tejun Heo3ec25eb2008-03-27 18:37:14 +09001175 ap->link.eh_info.action |= ATA_EH_LPM;
Kristen Carlson Accardica773292007-10-25 00:58:59 -04001176 ap->link.eh_info.flags |= ATA_EHI_NO_AUTOPSY;
1177 ata_port_schedule_eh(ap);
1178}
1179
Stephen Rothwell1992a5e2007-10-31 14:53:32 +11001180#ifdef CONFIG_PM
Kristen Carlson Accardica773292007-10-25 00:58:59 -04001181static void ata_lpm_enable(struct ata_host *host)
1182{
1183 struct ata_link *link;
1184 struct ata_port *ap;
1185 struct ata_device *dev;
1186 int i;
1187
1188 for (i = 0; i < host->n_ports; i++) {
1189 ap = host->ports[i];
Tejun Heo1eca4362008-11-03 20:03:17 +09001190 ata_for_each_link(link, ap, EDGE) {
1191 ata_for_each_dev(dev, link, ALL)
Kristen Carlson Accardica773292007-10-25 00:58:59 -04001192 ata_dev_disable_pm(dev);
1193 }
1194 }
1195}
1196
1197static void ata_lpm_disable(struct ata_host *host)
1198{
1199 int i;
1200
1201 for (i = 0; i < host->n_ports; i++) {
1202 struct ata_port *ap = host->ports[i];
1203 ata_lpm_schedule(ap, ap->pm_policy);
1204 }
1205}
Stephen Rothwell1992a5e2007-10-31 14:53:32 +11001206#endif /* CONFIG_PM */
Kristen Carlson Accardica773292007-10-25 00:58:59 -04001207
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208/**
1209 * ata_dev_classify - determine device type based on ATA-spec signature
1210 * @tf: ATA taskfile register set for device to be identified
1211 *
1212 * Determine from taskfile register contents whether a device is
1213 * ATA or ATAPI, as per "Signature and persistence" section
1214 * of ATA/PI spec (volume 1, sect 5.14).
1215 *
1216 * LOCKING:
1217 * None.
1218 *
1219 * RETURNS:
Tejun Heo633273a2007-09-23 13:19:54 +09001220 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, %ATA_DEV_PMP or
1221 * %ATA_DEV_UNKNOWN the event of failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 */
Jeff Garzik057ace52005-10-22 14:27:05 -04001223unsigned int ata_dev_classify(const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224{
1225 /* Apple's open source Darwin code hints that some devices only
1226 * put a proper signature into the LBA mid/high registers,
1227 * So, we only check those. It's sufficient for uniqueness.
Tejun Heo633273a2007-09-23 13:19:54 +09001228 *
1229 * ATA/ATAPI-7 (d1532v1r1: Feb. 19, 2003) specified separate
1230 * signatures for ATA and ATAPI devices attached on SerialATA,
1231 * 0x3c/0xc3 and 0x69/0x96 respectively. However, SerialATA
1232 * spec has never mentioned about using different signatures
1233 * for ATA/ATAPI devices. Then, Serial ATA II: Port
1234 * Multiplier specification began to use 0x69/0x96 to identify
1235 * port multpliers and 0x3c/0xc3 to identify SEMB device.
1236 * ATA/ATAPI-7 dropped descriptions about 0x3c/0xc3 and
1237 * 0x69/0x96 shortly and described them as reserved for
1238 * SerialATA.
1239 *
1240 * We follow the current spec and consider that 0x69/0x96
1241 * identifies a port multiplier and 0x3c/0xc3 a SEMB device.
Tejun Heo79b42ba2009-04-15 06:21:10 +09001242 * Unfortunately, WDC WD1600JS-62MHB5 (a hard drive) reports
1243 * SEMB signature. This is worked around in
1244 * ata_dev_read_id().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 */
Tejun Heo633273a2007-09-23 13:19:54 +09001246 if ((tf->lbam == 0) && (tf->lbah == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 DPRINTK("found ATA device by sig\n");
1248 return ATA_DEV_ATA;
1249 }
1250
Tejun Heo633273a2007-09-23 13:19:54 +09001251 if ((tf->lbam == 0x14) && (tf->lbah == 0xeb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 DPRINTK("found ATAPI device by sig\n");
1253 return ATA_DEV_ATAPI;
1254 }
1255
Tejun Heo633273a2007-09-23 13:19:54 +09001256 if ((tf->lbam == 0x69) && (tf->lbah == 0x96)) {
1257 DPRINTK("found PMP device by sig\n");
1258 return ATA_DEV_PMP;
1259 }
1260
1261 if ((tf->lbam == 0x3c) && (tf->lbah == 0xc3)) {
Tejun Heo79b42ba2009-04-15 06:21:10 +09001262 DPRINTK("found SEMB device by sig (could be ATA device)\n");
1263 return ATA_DEV_SEMB;
Tejun Heo633273a2007-09-23 13:19:54 +09001264 }
1265
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 DPRINTK("unknown device\n");
1267 return ATA_DEV_UNKNOWN;
1268}
1269
1270/**
Tejun Heo6a62a042006-02-13 10:02:46 +09001271 * ata_id_string - Convert IDENTIFY DEVICE page into string
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 * @id: IDENTIFY DEVICE results we will examine
1273 * @s: string into which data is output
1274 * @ofs: offset into identify device page
1275 * @len: length of string to return. must be an even number.
1276 *
1277 * The strings in the IDENTIFY DEVICE page are broken up into
1278 * 16-bit chunks. Run through the string, and output each
1279 * 8-bit chunk linearly, regardless of platform.
1280 *
1281 * LOCKING:
1282 * caller.
1283 */
1284
Tejun Heo6a62a042006-02-13 10:02:46 +09001285void ata_id_string(const u16 *id, unsigned char *s,
1286 unsigned int ofs, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287{
1288 unsigned int c;
1289
Alan Cox963e4972008-07-24 17:16:06 +01001290 BUG_ON(len & 1);
1291
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 while (len > 0) {
1293 c = id[ofs] >> 8;
1294 *s = c;
1295 s++;
1296
1297 c = id[ofs] & 0xff;
1298 *s = c;
1299 s++;
1300
1301 ofs++;
1302 len -= 2;
1303 }
1304}
1305
Tejun Heo0e949ff2006-02-12 22:47:04 +09001306/**
Tejun Heo6a62a042006-02-13 10:02:46 +09001307 * ata_id_c_string - Convert IDENTIFY DEVICE page into C string
Tejun Heo0e949ff2006-02-12 22:47:04 +09001308 * @id: IDENTIFY DEVICE results we will examine
1309 * @s: string into which data is output
1310 * @ofs: offset into identify device page
1311 * @len: length of string to return. must be an odd number.
1312 *
Tejun Heo6a62a042006-02-13 10:02:46 +09001313 * This function is identical to ata_id_string except that it
Tejun Heo0e949ff2006-02-12 22:47:04 +09001314 * trims trailing spaces and terminates the resulting string with
1315 * null. @len must be actual maximum length (even number) + 1.
1316 *
1317 * LOCKING:
1318 * caller.
1319 */
Tejun Heo6a62a042006-02-13 10:02:46 +09001320void ata_id_c_string(const u16 *id, unsigned char *s,
1321 unsigned int ofs, unsigned int len)
Tejun Heo0e949ff2006-02-12 22:47:04 +09001322{
1323 unsigned char *p;
1324
Tejun Heo6a62a042006-02-13 10:02:46 +09001325 ata_id_string(id, s, ofs, len - 1);
Tejun Heo0e949ff2006-02-12 22:47:04 +09001326
1327 p = s + strnlen(s, len - 1);
1328 while (p > s && p[-1] == ' ')
1329 p--;
1330 *p = '\0';
1331}
Edward Falk0baab862005-06-02 18:17:13 -04001332
Tejun Heodb6f8752007-09-03 12:31:58 +09001333static u64 ata_id_n_sectors(const u16 *id)
1334{
1335 if (ata_id_has_lba(id)) {
1336 if (ata_id_has_lba48(id))
Robert Hancock968e5942009-02-16 20:15:08 -06001337 return ata_id_u64(id, ATA_ID_LBA_CAPACITY_2);
Tejun Heodb6f8752007-09-03 12:31:58 +09001338 else
Robert Hancock968e5942009-02-16 20:15:08 -06001339 return ata_id_u32(id, ATA_ID_LBA_CAPACITY);
Tejun Heodb6f8752007-09-03 12:31:58 +09001340 } else {
1341 if (ata_id_current_chs_valid(id))
Robert Hancock968e5942009-02-16 20:15:08 -06001342 return id[ATA_ID_CUR_CYLS] * id[ATA_ID_CUR_HEADS] *
1343 id[ATA_ID_CUR_SECTORS];
Tejun Heodb6f8752007-09-03 12:31:58 +09001344 else
Robert Hancock968e5942009-02-16 20:15:08 -06001345 return id[ATA_ID_CYLS] * id[ATA_ID_HEADS] *
1346 id[ATA_ID_SECTORS];
Tejun Heodb6f8752007-09-03 12:31:58 +09001347 }
1348}
1349
Tejun Heoa5987e02008-03-27 19:14:23 +09001350u64 ata_tf_to_lba48(const struct ata_taskfile *tf)
Alan Cox1e999732007-04-11 00:23:13 +01001351{
1352 u64 sectors = 0;
1353
1354 sectors |= ((u64)(tf->hob_lbah & 0xff)) << 40;
1355 sectors |= ((u64)(tf->hob_lbam & 0xff)) << 32;
Roland Dreierba14a9c2008-10-28 16:52:20 -07001356 sectors |= ((u64)(tf->hob_lbal & 0xff)) << 24;
Alan Cox1e999732007-04-11 00:23:13 +01001357 sectors |= (tf->lbah & 0xff) << 16;
1358 sectors |= (tf->lbam & 0xff) << 8;
1359 sectors |= (tf->lbal & 0xff);
1360
Tejun Heoa5987e02008-03-27 19:14:23 +09001361 return sectors;
Alan Cox1e999732007-04-11 00:23:13 +01001362}
1363
Tejun Heoa5987e02008-03-27 19:14:23 +09001364u64 ata_tf_to_lba(const struct ata_taskfile *tf)
Alan Cox1e999732007-04-11 00:23:13 +01001365{
1366 u64 sectors = 0;
1367
1368 sectors |= (tf->device & 0x0f) << 24;
1369 sectors |= (tf->lbah & 0xff) << 16;
1370 sectors |= (tf->lbam & 0xff) << 8;
1371 sectors |= (tf->lbal & 0xff);
1372
Tejun Heoa5987e02008-03-27 19:14:23 +09001373 return sectors;
Alan Cox1e999732007-04-11 00:23:13 +01001374}
1375
1376/**
Tejun Heoc728a912007-09-03 12:32:30 +09001377 * ata_read_native_max_address - Read native max address
1378 * @dev: target device
1379 * @max_sectors: out parameter for the result native max address
Alan Cox1e999732007-04-11 00:23:13 +01001380 *
Tejun Heoc728a912007-09-03 12:32:30 +09001381 * Perform an LBA48 or LBA28 native size query upon the device in
1382 * question.
1383 *
1384 * RETURNS:
1385 * 0 on success, -EACCES if command is aborted by the drive.
1386 * -EIO on other errors.
Alan Cox1e999732007-04-11 00:23:13 +01001387 */
Tejun Heoc728a912007-09-03 12:32:30 +09001388static int ata_read_native_max_address(struct ata_device *dev, u64 *max_sectors)
Alan Cox1e999732007-04-11 00:23:13 +01001389{
Tejun Heoc728a912007-09-03 12:32:30 +09001390 unsigned int err_mask;
Alan Cox1e999732007-04-11 00:23:13 +01001391 struct ata_taskfile tf;
Tejun Heoc728a912007-09-03 12:32:30 +09001392 int lba48 = ata_id_has_lba48(dev->id);
Alan Cox1e999732007-04-11 00:23:13 +01001393
1394 ata_tf_init(dev, &tf);
1395
Tejun Heoc728a912007-09-03 12:32:30 +09001396 /* always clear all address registers */
Alan Cox1e999732007-04-11 00:23:13 +01001397 tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
Tejun Heoc728a912007-09-03 12:32:30 +09001398
1399 if (lba48) {
1400 tf.command = ATA_CMD_READ_NATIVE_MAX_EXT;
1401 tf.flags |= ATA_TFLAG_LBA48;
1402 } else
1403 tf.command = ATA_CMD_READ_NATIVE_MAX;
1404
Alan Cox1e999732007-04-11 00:23:13 +01001405 tf.protocol |= ATA_PROT_NODATA;
Tejun Heoc728a912007-09-03 12:32:30 +09001406 tf.device |= ATA_LBA;
Alan Cox1e999732007-04-11 00:23:13 +01001407
Tejun Heo2b789102007-10-09 15:05:44 +09001408 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
Tejun Heoc728a912007-09-03 12:32:30 +09001409 if (err_mask) {
1410 ata_dev_printk(dev, KERN_WARNING, "failed to read native "
1411 "max address (err_mask=0x%x)\n", err_mask);
1412 if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
1413 return -EACCES;
1414 return -EIO;
1415 }
Alan Cox1e999732007-04-11 00:23:13 +01001416
Tejun Heoc728a912007-09-03 12:32:30 +09001417 if (lba48)
Tejun Heoa5987e02008-03-27 19:14:23 +09001418 *max_sectors = ata_tf_to_lba48(&tf) + 1;
Tejun Heoc728a912007-09-03 12:32:30 +09001419 else
Tejun Heoa5987e02008-03-27 19:14:23 +09001420 *max_sectors = ata_tf_to_lba(&tf) + 1;
Jeff Garzik2dcb4072007-10-19 06:42:56 -04001421 if (dev->horkage & ATA_HORKAGE_HPA_SIZE)
Alan Cox93328e12007-09-29 04:06:48 -04001422 (*max_sectors)--;
Tejun Heoc728a912007-09-03 12:32:30 +09001423 return 0;
Alan Cox1e999732007-04-11 00:23:13 +01001424}
1425
1426/**
Tejun Heoc728a912007-09-03 12:32:30 +09001427 * ata_set_max_sectors - Set max sectors
1428 * @dev: target device
Randy Dunlap6b38d1d2007-05-01 17:35:55 -07001429 * @new_sectors: new max sectors value to set for the device
Alan Cox1e999732007-04-11 00:23:13 +01001430 *
Tejun Heoc728a912007-09-03 12:32:30 +09001431 * Set max sectors of @dev to @new_sectors.
1432 *
1433 * RETURNS:
1434 * 0 on success, -EACCES if command is aborted or denied (due to
1435 * previous non-volatile SET_MAX) by the drive. -EIO on other
1436 * errors.
Alan Cox1e999732007-04-11 00:23:13 +01001437 */
Tejun Heo05027ad2007-09-03 12:32:57 +09001438static int ata_set_max_sectors(struct ata_device *dev, u64 new_sectors)
Alan Cox1e999732007-04-11 00:23:13 +01001439{
Tejun Heoc728a912007-09-03 12:32:30 +09001440 unsigned int err_mask;
Alan Cox1e999732007-04-11 00:23:13 +01001441 struct ata_taskfile tf;
Tejun Heoc728a912007-09-03 12:32:30 +09001442 int lba48 = ata_id_has_lba48(dev->id);
Alan Cox1e999732007-04-11 00:23:13 +01001443
1444 new_sectors--;
1445
1446 ata_tf_init(dev, &tf);
1447
Alan Cox1e999732007-04-11 00:23:13 +01001448 tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
Tejun Heoc728a912007-09-03 12:32:30 +09001449
1450 if (lba48) {
1451 tf.command = ATA_CMD_SET_MAX_EXT;
1452 tf.flags |= ATA_TFLAG_LBA48;
1453
1454 tf.hob_lbal = (new_sectors >> 24) & 0xff;
1455 tf.hob_lbam = (new_sectors >> 32) & 0xff;
1456 tf.hob_lbah = (new_sectors >> 40) & 0xff;
Tejun Heo1e582ba2007-09-21 20:07:14 +09001457 } else {
Tejun Heoc728a912007-09-03 12:32:30 +09001458 tf.command = ATA_CMD_SET_MAX;
1459
Tejun Heo1e582ba2007-09-21 20:07:14 +09001460 tf.device |= (new_sectors >> 24) & 0xf;
1461 }
1462
Alan Cox1e999732007-04-11 00:23:13 +01001463 tf.protocol |= ATA_PROT_NODATA;
Tejun Heoc728a912007-09-03 12:32:30 +09001464 tf.device |= ATA_LBA;
Alan Cox1e999732007-04-11 00:23:13 +01001465
1466 tf.lbal = (new_sectors >> 0) & 0xff;
1467 tf.lbam = (new_sectors >> 8) & 0xff;
1468 tf.lbah = (new_sectors >> 16) & 0xff;
Alan Cox1e999732007-04-11 00:23:13 +01001469
Tejun Heo2b789102007-10-09 15:05:44 +09001470 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
Tejun Heoc728a912007-09-03 12:32:30 +09001471 if (err_mask) {
1472 ata_dev_printk(dev, KERN_WARNING, "failed to set "
1473 "max address (err_mask=0x%x)\n", err_mask);
1474 if (err_mask == AC_ERR_DEV &&
1475 (tf.feature & (ATA_ABORTED | ATA_IDNF)))
1476 return -EACCES;
1477 return -EIO;
1478 }
Alan Cox1e999732007-04-11 00:23:13 +01001479
Tejun Heoc728a912007-09-03 12:32:30 +09001480 return 0;
Alan Cox1e999732007-04-11 00:23:13 +01001481}
1482
1483/**
1484 * ata_hpa_resize - Resize a device with an HPA set
1485 * @dev: Device to resize
1486 *
1487 * Read the size of an LBA28 or LBA48 disk with HPA features and resize
1488 * it if required to the full size of the media. The caller must check
1489 * the drive has the HPA feature set enabled.
Tejun Heo05027ad2007-09-03 12:32:57 +09001490 *
1491 * RETURNS:
1492 * 0 on success, -errno on failure.
Alan Cox1e999732007-04-11 00:23:13 +01001493 */
Tejun Heo05027ad2007-09-03 12:32:57 +09001494static int ata_hpa_resize(struct ata_device *dev)
Alan Cox1e999732007-04-11 00:23:13 +01001495{
Tejun Heo05027ad2007-09-03 12:32:57 +09001496 struct ata_eh_context *ehc = &dev->link->eh_context;
1497 int print_info = ehc->i.flags & ATA_EHI_PRINTINFO;
Tejun Heo445d2112010-04-05 10:33:13 +09001498 bool unlock_hpa = ata_ignore_hpa || dev->flags & ATA_DFLAG_UNLOCK_HPA;
Tejun Heo05027ad2007-09-03 12:32:57 +09001499 u64 sectors = ata_id_n_sectors(dev->id);
1500 u64 native_sectors;
Tejun Heoc728a912007-09-03 12:32:30 +09001501 int rc;
Jeff Garzika617c092007-05-21 20:14:23 -04001502
Tejun Heo05027ad2007-09-03 12:32:57 +09001503 /* do we need to do it? */
1504 if (dev->class != ATA_DEV_ATA ||
1505 !ata_id_has_lba(dev->id) || !ata_id_hpa_enabled(dev->id) ||
1506 (dev->horkage & ATA_HORKAGE_BROKEN_HPA))
Tejun Heoc728a912007-09-03 12:32:30 +09001507 return 0;
Alan Cox1e999732007-04-11 00:23:13 +01001508
Tejun Heo05027ad2007-09-03 12:32:57 +09001509 /* read native max address */
1510 rc = ata_read_native_max_address(dev, &native_sectors);
1511 if (rc) {
Tejun Heodda7aba2008-03-23 21:05:15 +09001512 /* If device aborted the command or HPA isn't going to
1513 * be unlocked, skip HPA resizing.
Tejun Heo05027ad2007-09-03 12:32:57 +09001514 */
Tejun Heo445d2112010-04-05 10:33:13 +09001515 if (rc == -EACCES || !unlock_hpa) {
Tejun Heo05027ad2007-09-03 12:32:57 +09001516 ata_dev_printk(dev, KERN_WARNING, "HPA support seems "
Tejun Heodda7aba2008-03-23 21:05:15 +09001517 "broken, skipping HPA handling\n");
Tejun Heo05027ad2007-09-03 12:32:57 +09001518 dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
Alan Cox1e999732007-04-11 00:23:13 +01001519
Tejun Heo05027ad2007-09-03 12:32:57 +09001520 /* we can continue if device aborted the command */
1521 if (rc == -EACCES)
1522 rc = 0;
Alan Cox1e999732007-04-11 00:23:13 +01001523 }
Tejun Heo37301a52007-06-25 20:45:54 +09001524
Tejun Heo05027ad2007-09-03 12:32:57 +09001525 return rc;
1526 }
Tejun Heo5920dad2009-07-15 17:11:41 +09001527 dev->n_native_sectors = native_sectors;
Tejun Heo05027ad2007-09-03 12:32:57 +09001528
1529 /* nothing to do? */
Tejun Heo445d2112010-04-05 10:33:13 +09001530 if (native_sectors <= sectors || !unlock_hpa) {
Tejun Heo05027ad2007-09-03 12:32:57 +09001531 if (!print_info || native_sectors == sectors)
1532 return 0;
1533
1534 if (native_sectors > sectors)
1535 ata_dev_printk(dev, KERN_INFO,
1536 "HPA detected: current %llu, native %llu\n",
1537 (unsigned long long)sectors,
1538 (unsigned long long)native_sectors);
1539 else if (native_sectors < sectors)
1540 ata_dev_printk(dev, KERN_WARNING,
1541 "native sectors (%llu) is smaller than "
1542 "sectors (%llu)\n",
1543 (unsigned long long)native_sectors,
1544 (unsigned long long)sectors);
1545 return 0;
1546 }
1547
1548 /* let's unlock HPA */
1549 rc = ata_set_max_sectors(dev, native_sectors);
1550 if (rc == -EACCES) {
1551 /* if device aborted the command, skip HPA resizing */
1552 ata_dev_printk(dev, KERN_WARNING, "device aborted resize "
1553 "(%llu -> %llu), skipping HPA handling\n",
1554 (unsigned long long)sectors,
1555 (unsigned long long)native_sectors);
1556 dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
1557 return 0;
1558 } else if (rc)
1559 return rc;
1560
1561 /* re-read IDENTIFY data */
1562 rc = ata_dev_reread_id(dev, 0);
1563 if (rc) {
1564 ata_dev_printk(dev, KERN_ERR, "failed to re-read IDENTIFY "
1565 "data after HPA resizing\n");
1566 return rc;
1567 }
1568
1569 if (print_info) {
1570 u64 new_sectors = ata_id_n_sectors(dev->id);
1571 ata_dev_printk(dev, KERN_INFO,
1572 "HPA unlocked: %llu -> %llu, native %llu\n",
1573 (unsigned long long)sectors,
1574 (unsigned long long)new_sectors,
1575 (unsigned long long)native_sectors);
1576 }
1577
1578 return 0;
Alan Cox1e999732007-04-11 00:23:13 +01001579}
1580
Edward Falk0baab862005-06-02 18:17:13 -04001581/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 * ata_dump_id - IDENTIFY DEVICE info debugging output
Tejun Heo0bd33002006-02-12 22:47:05 +09001583 * @id: IDENTIFY DEVICE page to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 *
Tejun Heo0bd33002006-02-12 22:47:05 +09001585 * Dump selected 16-bit words from the given IDENTIFY DEVICE
1586 * page.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 *
1588 * LOCKING:
1589 * caller.
1590 */
1591
Tejun Heo0bd33002006-02-12 22:47:05 +09001592static inline void ata_dump_id(const u16 *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593{
1594 DPRINTK("49==0x%04x "
1595 "53==0x%04x "
1596 "63==0x%04x "
1597 "64==0x%04x "
1598 "75==0x%04x \n",
Tejun Heo0bd33002006-02-12 22:47:05 +09001599 id[49],
1600 id[53],
1601 id[63],
1602 id[64],
1603 id[75]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 DPRINTK("80==0x%04x "
1605 "81==0x%04x "
1606 "82==0x%04x "
1607 "83==0x%04x "
1608 "84==0x%04x \n",
Tejun Heo0bd33002006-02-12 22:47:05 +09001609 id[80],
1610 id[81],
1611 id[82],
1612 id[83],
1613 id[84]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 DPRINTK("88==0x%04x "
1615 "93==0x%04x\n",
Tejun Heo0bd33002006-02-12 22:47:05 +09001616 id[88],
1617 id[93]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618}
1619
Tejun Heocb95d562006-03-06 04:31:56 +09001620/**
1621 * ata_id_xfermask - Compute xfermask from the given IDENTIFY data
1622 * @id: IDENTIFY data to compute xfer mask from
1623 *
1624 * Compute the xfermask for this device. This is not as trivial
1625 * as it seems if we must consider early devices correctly.
1626 *
1627 * FIXME: pre IDE drive timing (do we care ?).
1628 *
1629 * LOCKING:
1630 * None.
1631 *
1632 * RETURNS:
1633 * Computed xfermask
1634 */
Tejun Heo7dc951a2007-11-27 19:43:42 +09001635unsigned long ata_id_xfermask(const u16 *id)
Tejun Heocb95d562006-03-06 04:31:56 +09001636{
Tejun Heo7dc951a2007-11-27 19:43:42 +09001637 unsigned long pio_mask, mwdma_mask, udma_mask;
Tejun Heocb95d562006-03-06 04:31:56 +09001638
1639 /* Usual case. Word 53 indicates word 64 is valid */
1640 if (id[ATA_ID_FIELD_VALID] & (1 << 1)) {
1641 pio_mask = id[ATA_ID_PIO_MODES] & 0x03;
1642 pio_mask <<= 3;
1643 pio_mask |= 0x7;
1644 } else {
1645 /* If word 64 isn't valid then Word 51 high byte holds
1646 * the PIO timing number for the maximum. Turn it into
1647 * a mask.
1648 */
Lennert Buytenhek7a0f1c82007-01-29 13:28:47 +01001649 u8 mode = (id[ATA_ID_OLD_PIO_MODES] >> 8) & 0xFF;
Alan Cox46767aeb2006-09-29 18:26:47 +01001650 if (mode < 5) /* Valid PIO range */
Jeff Garzik2dcb4072007-10-19 06:42:56 -04001651 pio_mask = (2 << mode) - 1;
Alan Cox46767aeb2006-09-29 18:26:47 +01001652 else
1653 pio_mask = 1;
Tejun Heocb95d562006-03-06 04:31:56 +09001654
1655 /* But wait.. there's more. Design your standards by
1656 * committee and you too can get a free iordy field to
1657 * process. However its the speeds not the modes that
1658 * are supported... Note drivers using the timing API
1659 * will get this right anyway
1660 */
1661 }
1662
1663 mwdma_mask = id[ATA_ID_MWDMA_MODES] & 0x07;
Tejun Heofb21f0d2006-03-12 12:34:35 +09001664
Alan Coxb352e572006-08-10 18:52:12 +01001665 if (ata_id_is_cfa(id)) {
1666 /*
1667 * Process compact flash extended modes
1668 */
Sergei Shtylyov62afe5d2009-04-13 20:50:00 +04001669 int pio = (id[ATA_ID_CFA_MODES] >> 0) & 0x7;
1670 int dma = (id[ATA_ID_CFA_MODES] >> 3) & 0x7;
Alan Coxb352e572006-08-10 18:52:12 +01001671
1672 if (pio)
1673 pio_mask |= (1 << 5);
1674 if (pio > 1)
1675 pio_mask |= (1 << 6);
1676 if (dma)
1677 mwdma_mask |= (1 << 3);
1678 if (dma > 1)
1679 mwdma_mask |= (1 << 4);
1680 }
1681
Tejun Heofb21f0d2006-03-12 12:34:35 +09001682 udma_mask = 0;
1683 if (id[ATA_ID_FIELD_VALID] & (1 << 2))
1684 udma_mask = id[ATA_ID_UDMA_MODES] & 0xff;
Tejun Heocb95d562006-03-06 04:31:56 +09001685
1686 return ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
1687}
1688
Tejun Heo86e45b62006-03-05 15:29:09 +09001689/**
Jeff Garzik442eacc2007-12-19 04:25:10 -05001690 * ata_pio_queue_task - Queue port_task
Tejun Heo86e45b62006-03-05 15:29:09 +09001691 * @ap: The ata_port to queue port_task for
David Howells65f27f32006-11-22 14:55:48 +00001692 * @data: data for @fn to use
Tejun Heo341c2c92008-05-20 02:17:51 +09001693 * @delay: delay time in msecs for workqueue function
Tejun Heo86e45b62006-03-05 15:29:09 +09001694 *
1695 * Schedule @fn(@data) for execution after @delay jiffies using
1696 * port_task. There is one port_task per port and it's the
1697 * user(low level driver)'s responsibility to make sure that only
1698 * one task is active at any given time.
1699 *
1700 * libata core layer takes care of synchronization between
Jeff Garzik442eacc2007-12-19 04:25:10 -05001701 * port_task and EH. ata_pio_queue_task() may be ignored for EH
Tejun Heo86e45b62006-03-05 15:29:09 +09001702 * synchronization.
1703 *
1704 * LOCKING:
1705 * Inherited from caller.
1706 */
Tejun Heo624d5c52008-03-25 22:16:41 +09001707void ata_pio_queue_task(struct ata_port *ap, void *data, unsigned long delay)
Tejun Heo86e45b62006-03-05 15:29:09 +09001708{
David Howells65f27f32006-11-22 14:55:48 +00001709 ap->port_task_data = data;
Tejun Heo86e45b62006-03-05 15:29:09 +09001710
Oleg Nesterov45a66c12007-07-09 11:46:13 -07001711 /* may fail if ata_port_flush_task() in progress */
Tejun Heo341c2c92008-05-20 02:17:51 +09001712 queue_delayed_work(ata_wq, &ap->port_task, msecs_to_jiffies(delay));
Tejun Heo86e45b62006-03-05 15:29:09 +09001713}
1714
1715/**
1716 * ata_port_flush_task - Flush port_task
1717 * @ap: The ata_port to flush port_task for
1718 *
1719 * After this function completes, port_task is guranteed not to
1720 * be running or scheduled.
1721 *
1722 * LOCKING:
1723 * Kernel thread context (may sleep)
1724 */
1725void ata_port_flush_task(struct ata_port *ap)
1726{
Tejun Heo86e45b62006-03-05 15:29:09 +09001727 DPRINTK("ENTER\n");
1728
Oleg Nesterov45a66c12007-07-09 11:46:13 -07001729 cancel_rearming_delayed_work(&ap->port_task);
Tejun Heo86e45b62006-03-05 15:29:09 +09001730
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001731 if (ata_msg_ctl(ap))
Harvey Harrison7f5e4e82008-03-05 18:24:52 -08001732 ata_port_printk(ap, KERN_DEBUG, "%s: EXIT\n", __func__);
Tejun Heo86e45b62006-03-05 15:29:09 +09001733}
1734
Adrian Bunk7102d232007-01-04 00:09:36 +01001735static void ata_qc_complete_internal(struct ata_queued_cmd *qc)
Tejun Heoa2a7a662005-12-13 14:48:31 +09001736{
Tejun Heo77853bf2006-01-23 13:09:36 +09001737 struct completion *waiting = qc->private_data;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001738
Tejun Heoa2a7a662005-12-13 14:48:31 +09001739 complete(waiting);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001740}
1741
1742/**
Tejun Heo24326972006-11-14 22:47:09 +09001743 * ata_exec_internal_sg - execute libata internal command
Tejun Heoa2a7a662005-12-13 14:48:31 +09001744 * @dev: Device to which the command is sent
1745 * @tf: Taskfile registers for the command and the result
Tejun Heod69cf372006-04-02 18:51:53 +09001746 * @cdb: CDB for packet command
Tejun Heoa2a7a662005-12-13 14:48:31 +09001747 * @dma_dir: Data tranfer direction of the command
Randy Dunlap5c1ad8b2007-10-18 14:12:26 -07001748 * @sgl: sg list for the data buffer of the command
Tejun Heo24326972006-11-14 22:47:09 +09001749 * @n_elem: Number of sg entries
Tejun Heo2b789102007-10-09 15:05:44 +09001750 * @timeout: Timeout in msecs (0 for default)
Tejun Heoa2a7a662005-12-13 14:48:31 +09001751 *
1752 * Executes libata internal command with timeout. @tf contains
1753 * command on entry and result on return. Timeout and error
1754 * conditions are reported via return value. No recovery action
1755 * is taken after a command times out. It's caller's duty to
1756 * clean up after timeout.
1757 *
1758 * LOCKING:
1759 * None. Should be called with kernel context, might sleep.
Tejun Heo551e8882006-06-12 14:09:49 +09001760 *
1761 * RETURNS:
1762 * Zero on success, AC_ERR_* mask on failure
Tejun Heoa2a7a662005-12-13 14:48:31 +09001763 */
Tejun Heo24326972006-11-14 22:47:09 +09001764unsigned ata_exec_internal_sg(struct ata_device *dev,
1765 struct ata_taskfile *tf, const u8 *cdb,
Jens Axboe87260212007-10-16 11:14:12 +02001766 int dma_dir, struct scatterlist *sgl,
Tejun Heo2b789102007-10-09 15:05:44 +09001767 unsigned int n_elem, unsigned long timeout)
Tejun Heoa2a7a662005-12-13 14:48:31 +09001768{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001769 struct ata_link *link = dev->link;
1770 struct ata_port *ap = link->ap;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001771 u8 command = tf->command;
Tejun Heo87fbc5a2008-05-20 02:17:54 +09001772 int auto_timeout = 0;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001773 struct ata_queued_cmd *qc;
Tejun Heo2ab7db12006-05-15 20:58:02 +09001774 unsigned int tag, preempted_tag;
Tejun Heodedaf2b2006-05-15 21:03:43 +09001775 u32 preempted_sactive, preempted_qc_active;
Tejun Heoda917d62007-09-23 13:14:12 +09001776 int preempted_nr_active_links;
Ingo Molnar60be6b92006-07-03 00:25:26 -07001777 DECLARE_COMPLETION_ONSTACK(wait);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001778 unsigned long flags;
Tejun Heo77853bf2006-01-23 13:09:36 +09001779 unsigned int err_mask;
Tejun Heod95a7172006-05-15 20:58:14 +09001780 int rc;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001781
Jeff Garzikba6a1302006-06-22 23:46:10 -04001782 spin_lock_irqsave(ap->lock, flags);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001783
Tejun Heoe3180492006-05-15 20:58:09 +09001784 /* no internal command while frozen */
Tejun Heob51e9e52006-06-29 01:29:30 +09001785 if (ap->pflags & ATA_PFLAG_FROZEN) {
Jeff Garzikba6a1302006-06-22 23:46:10 -04001786 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +09001787 return AC_ERR_SYSTEM;
1788 }
1789
Tejun Heo2ab7db12006-05-15 20:58:02 +09001790 /* initialize internal qc */
Tejun Heoa2a7a662005-12-13 14:48:31 +09001791
Tejun Heo2ab7db12006-05-15 20:58:02 +09001792 /* XXX: Tag 0 is used for drivers with legacy EH as some
1793 * drivers choke if any other tag is given. This breaks
1794 * ata_tag_internal() test for those drivers. Don't use new
1795 * EH stuff without converting to it.
1796 */
1797 if (ap->ops->error_handler)
1798 tag = ATA_TAG_INTERNAL;
1799 else
1800 tag = 0;
1801
Tejun Heo8a8bc222008-11-10 14:48:21 +09001802 if (test_and_set_bit(tag, &ap->qc_allocated))
1803 BUG();
Tejun Heof69499f2006-05-15 20:58:03 +09001804 qc = __ata_qc_from_tag(ap, tag);
Tejun Heo2ab7db12006-05-15 20:58:02 +09001805
1806 qc->tag = tag;
1807 qc->scsicmd = NULL;
1808 qc->ap = ap;
1809 qc->dev = dev;
1810 ata_qc_reinit(qc);
1811
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001812 preempted_tag = link->active_tag;
1813 preempted_sactive = link->sactive;
Tejun Heodedaf2b2006-05-15 21:03:43 +09001814 preempted_qc_active = ap->qc_active;
Tejun Heoda917d62007-09-23 13:14:12 +09001815 preempted_nr_active_links = ap->nr_active_links;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001816 link->active_tag = ATA_TAG_POISON;
1817 link->sactive = 0;
Tejun Heodedaf2b2006-05-15 21:03:43 +09001818 ap->qc_active = 0;
Tejun Heoda917d62007-09-23 13:14:12 +09001819 ap->nr_active_links = 0;
Tejun Heo2ab7db12006-05-15 20:58:02 +09001820
1821 /* prepare & issue qc */
Tejun Heoa2a7a662005-12-13 14:48:31 +09001822 qc->tf = *tf;
Tejun Heod69cf372006-04-02 18:51:53 +09001823 if (cdb)
1824 memcpy(qc->cdb, cdb, ATAPI_CDB_LEN);
Tejun Heoe61e0672006-05-15 20:57:40 +09001825 qc->flags |= ATA_QCFLAG_RESULT_TF;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001826 qc->dma_dir = dma_dir;
1827 if (dma_dir != DMA_NONE) {
Tejun Heo24326972006-11-14 22:47:09 +09001828 unsigned int i, buflen = 0;
Jens Axboe87260212007-10-16 11:14:12 +02001829 struct scatterlist *sg;
Tejun Heo24326972006-11-14 22:47:09 +09001830
Jens Axboe87260212007-10-16 11:14:12 +02001831 for_each_sg(sgl, sg, n_elem, i)
1832 buflen += sg->length;
Tejun Heo24326972006-11-14 22:47:09 +09001833
Jens Axboe87260212007-10-16 11:14:12 +02001834 ata_sg_init(qc, sgl, n_elem);
Brian King49c80422007-01-30 11:32:26 -06001835 qc->nbytes = buflen;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001836 }
1837
Tejun Heo77853bf2006-01-23 13:09:36 +09001838 qc->private_data = &wait;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001839 qc->complete_fn = ata_qc_complete_internal;
1840
Tejun Heo8e0e6942006-03-31 20:41:11 +09001841 ata_qc_issue(qc);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001842
Jeff Garzikba6a1302006-06-22 23:46:10 -04001843 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001844
Tejun Heo87fbc5a2008-05-20 02:17:54 +09001845 if (!timeout) {
1846 if (ata_probe_timeout)
1847 timeout = ata_probe_timeout * 1000;
1848 else {
1849 timeout = ata_internal_cmd_timeout(dev, command);
1850 auto_timeout = 1;
1851 }
1852 }
Tejun Heo2b789102007-10-09 15:05:44 +09001853
1854 rc = wait_for_completion_timeout(&wait, msecs_to_jiffies(timeout));
Albert Lee41ade502006-03-14 11:19:04 +08001855
Tejun Heod95a7172006-05-15 20:58:14 +09001856 ata_port_flush_task(ap);
1857
1858 if (!rc) {
Jeff Garzikba6a1302006-06-22 23:46:10 -04001859 spin_lock_irqsave(ap->lock, flags);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001860
1861 /* We're racing with irq here. If we lose, the
1862 * following test prevents us from completing the qc
Tejun Heod95a7172006-05-15 20:58:14 +09001863 * twice. If we win, the port is frozen and will be
1864 * cleaned up by ->post_internal_cmd().
Tejun Heoa2a7a662005-12-13 14:48:31 +09001865 */
Tejun Heo77853bf2006-01-23 13:09:36 +09001866 if (qc->flags & ATA_QCFLAG_ACTIVE) {
Tejun Heod95a7172006-05-15 20:58:14 +09001867 qc->err_mask |= AC_ERR_TIMEOUT;
1868
1869 if (ap->ops->error_handler)
1870 ata_port_freeze(ap);
1871 else
1872 ata_qc_complete(qc);
Tejun Heof15a1da2006-05-15 20:57:56 +09001873
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001874 if (ata_msg_warn(ap))
1875 ata_dev_printk(dev, KERN_WARNING,
Tejun Heo88574552006-06-25 20:00:35 +09001876 "qc timeout (cmd 0x%x)\n", command);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001877 }
1878
Jeff Garzikba6a1302006-06-22 23:46:10 -04001879 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001880 }
1881
Tejun Heod95a7172006-05-15 20:58:14 +09001882 /* do post_internal_cmd */
1883 if (ap->ops->post_internal_cmd)
1884 ap->ops->post_internal_cmd(qc);
1885
Tejun Heoa51d6442007-03-20 15:24:11 +09001886 /* perform minimal error analysis */
1887 if (qc->flags & ATA_QCFLAG_FAILED) {
1888 if (qc->result_tf.command & (ATA_ERR | ATA_DF))
1889 qc->err_mask |= AC_ERR_DEV;
1890
1891 if (!qc->err_mask)
1892 qc->err_mask |= AC_ERR_OTHER;
1893
1894 if (qc->err_mask & ~AC_ERR_OTHER)
1895 qc->err_mask &= ~AC_ERR_OTHER;
Tejun Heod95a7172006-05-15 20:58:14 +09001896 }
1897
Tejun Heo15869302006-05-15 20:57:33 +09001898 /* finish up */
Jeff Garzikba6a1302006-06-22 23:46:10 -04001899 spin_lock_irqsave(ap->lock, flags);
Tejun Heo15869302006-05-15 20:57:33 +09001900
Tejun Heoe61e0672006-05-15 20:57:40 +09001901 *tf = qc->result_tf;
Tejun Heo77853bf2006-01-23 13:09:36 +09001902 err_mask = qc->err_mask;
1903
1904 ata_qc_free(qc);
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001905 link->active_tag = preempted_tag;
1906 link->sactive = preempted_sactive;
Tejun Heodedaf2b2006-05-15 21:03:43 +09001907 ap->qc_active = preempted_qc_active;
Tejun Heoda917d62007-09-23 13:14:12 +09001908 ap->nr_active_links = preempted_nr_active_links;
Tejun Heo77853bf2006-01-23 13:09:36 +09001909
Tejun Heo1f7dd3e92006-03-24 15:25:30 +09001910 /* XXX - Some LLDDs (sata_mv) disable port on command failure.
1911 * Until those drivers are fixed, we detect the condition
1912 * here, fail the command with AC_ERR_SYSTEM and reenable the
1913 * port.
1914 *
1915 * Note that this doesn't change any behavior as internal
1916 * command failure results in disabling the device in the
1917 * higher layer for LLDDs without new reset/EH callbacks.
1918 *
1919 * Kill the following code as soon as those drivers are fixed.
1920 */
Tejun Heo198e0fe2006-04-02 18:51:52 +09001921 if (ap->flags & ATA_FLAG_DISABLED) {
Tejun Heo1f7dd3e92006-03-24 15:25:30 +09001922 err_mask |= AC_ERR_SYSTEM;
1923 ata_port_probe(ap);
1924 }
1925
Jeff Garzikba6a1302006-06-22 23:46:10 -04001926 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo15869302006-05-15 20:57:33 +09001927
Tejun Heo87fbc5a2008-05-20 02:17:54 +09001928 if ((err_mask & AC_ERR_TIMEOUT) && auto_timeout)
1929 ata_internal_cmd_timed_out(dev, command);
1930
Tejun Heo77853bf2006-01-23 13:09:36 +09001931 return err_mask;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001932}
1933
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934/**
Tejun Heo33480a02006-12-12 02:15:31 +09001935 * ata_exec_internal - execute libata internal command
Tejun Heo24326972006-11-14 22:47:09 +09001936 * @dev: Device to which the command is sent
1937 * @tf: Taskfile registers for the command and the result
1938 * @cdb: CDB for packet command
1939 * @dma_dir: Data tranfer direction of the command
1940 * @buf: Data buffer of the command
1941 * @buflen: Length of data buffer
Tejun Heo2b789102007-10-09 15:05:44 +09001942 * @timeout: Timeout in msecs (0 for default)
Tejun Heo24326972006-11-14 22:47:09 +09001943 *
1944 * Wrapper around ata_exec_internal_sg() which takes simple
1945 * buffer instead of sg list.
1946 *
1947 * LOCKING:
1948 * None. Should be called with kernel context, might sleep.
1949 *
1950 * RETURNS:
1951 * Zero on success, AC_ERR_* mask on failure
1952 */
1953unsigned ata_exec_internal(struct ata_device *dev,
1954 struct ata_taskfile *tf, const u8 *cdb,
Tejun Heo2b789102007-10-09 15:05:44 +09001955 int dma_dir, void *buf, unsigned int buflen,
1956 unsigned long timeout)
Tejun Heo24326972006-11-14 22:47:09 +09001957{
Tejun Heo33480a02006-12-12 02:15:31 +09001958 struct scatterlist *psg = NULL, sg;
1959 unsigned int n_elem = 0;
Tejun Heo24326972006-11-14 22:47:09 +09001960
Tejun Heo33480a02006-12-12 02:15:31 +09001961 if (dma_dir != DMA_NONE) {
1962 WARN_ON(!buf);
1963 sg_init_one(&sg, buf, buflen);
1964 psg = &sg;
1965 n_elem++;
1966 }
Tejun Heo24326972006-11-14 22:47:09 +09001967
Tejun Heo2b789102007-10-09 15:05:44 +09001968 return ata_exec_internal_sg(dev, tf, cdb, dma_dir, psg, n_elem,
1969 timeout);
Tejun Heo24326972006-11-14 22:47:09 +09001970}
1971
1972/**
Tejun Heo977e6b92006-06-24 20:30:19 +09001973 * ata_do_simple_cmd - execute simple internal command
1974 * @dev: Device to which the command is sent
1975 * @cmd: Opcode to execute
1976 *
1977 * Execute a 'simple' command, that only consists of the opcode
1978 * 'cmd' itself, without filling any other registers
1979 *
1980 * LOCKING:
1981 * Kernel thread context (may sleep).
1982 *
1983 * RETURNS:
1984 * Zero on success, AC_ERR_* mask on failure
Tejun Heoe58eb582006-06-24 20:30:19 +09001985 */
Tejun Heo77b08fb2006-06-24 20:30:19 +09001986unsigned int ata_do_simple_cmd(struct ata_device *dev, u8 cmd)
Tejun Heoe58eb582006-06-24 20:30:19 +09001987{
1988 struct ata_taskfile tf;
Tejun Heoe58eb582006-06-24 20:30:19 +09001989
1990 ata_tf_init(dev, &tf);
1991
1992 tf.command = cmd;
1993 tf.flags |= ATA_TFLAG_DEVICE;
1994 tf.protocol = ATA_PROT_NODATA;
1995
Tejun Heo2b789102007-10-09 15:05:44 +09001996 return ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
Tejun Heoe58eb582006-06-24 20:30:19 +09001997}
1998
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999/**
Alan Cox1bc4ccf2006-01-09 17:18:14 +00002000 * ata_pio_need_iordy - check if iordy needed
2001 * @adev: ATA device
2002 *
2003 * Check if the current speed of the device requires IORDY. Used
2004 * by various controllers for chip configuration.
2005 */
Alan Cox1bc4ccf2006-01-09 17:18:14 +00002006unsigned int ata_pio_need_iordy(const struct ata_device *adev)
2007{
Tejun Heo0d9e6652009-06-11 11:04:45 +09002008 /* Don't set IORDY if we're preparing for reset. IORDY may
2009 * lead to controller lock up on certain controllers if the
2010 * port is not occupied. See bko#11703 for details.
2011 */
2012 if (adev->link->ap->pflags & ATA_PFLAG_RESETTING)
2013 return 0;
2014 /* Controller doesn't support IORDY. Probably a pointless
2015 * check as the caller should know this.
2016 */
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002017 if (adev->link->ap->flags & ATA_FLAG_NO_IORDY)
Alan Cox1bc4ccf2006-01-09 17:18:14 +00002018 return 0;
David Daney5c18c4d2008-12-10 15:39:12 -08002019 /* CF spec. r4.1 Table 22 says no iordy on PIO5 and PIO6. */
2020 if (ata_id_is_cfa(adev->id)
2021 && (adev->pio_mode == XFER_PIO_5 || adev->pio_mode == XFER_PIO_6))
2022 return 0;
Alan Cox432729f2007-03-08 23:22:59 +00002023 /* PIO3 and higher it is mandatory */
2024 if (adev->pio_mode > XFER_PIO_2)
Alan Cox1bc4ccf2006-01-09 17:18:14 +00002025 return 1;
Alan Cox432729f2007-03-08 23:22:59 +00002026 /* We turn it on when possible */
2027 if (ata_id_has_iordy(adev->id))
2028 return 1;
2029 return 0;
2030}
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002031
Alan Cox432729f2007-03-08 23:22:59 +00002032/**
2033 * ata_pio_mask_no_iordy - Return the non IORDY mask
2034 * @adev: ATA device
2035 *
2036 * Compute the highest mode possible if we are not using iordy. Return
2037 * -1 if no iordy mode is available.
2038 */
Alan Cox432729f2007-03-08 23:22:59 +00002039static u32 ata_pio_mask_no_iordy(const struct ata_device *adev)
2040{
Alan Cox1bc4ccf2006-01-09 17:18:14 +00002041 /* If we have no drive specific rule, then PIO 2 is non IORDY */
Alan Cox1bc4ccf2006-01-09 17:18:14 +00002042 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
Alan Cox432729f2007-03-08 23:22:59 +00002043 u16 pio = adev->id[ATA_ID_EIDE_PIO];
Alan Cox1bc4ccf2006-01-09 17:18:14 +00002044 /* Is the speed faster than the drive allows non IORDY ? */
2045 if (pio) {
2046 /* This is cycle times not frequency - watch the logic! */
2047 if (pio > 240) /* PIO2 is 240nS per cycle */
Alan Cox432729f2007-03-08 23:22:59 +00002048 return 3 << ATA_SHIFT_PIO;
2049 return 7 << ATA_SHIFT_PIO;
Alan Cox1bc4ccf2006-01-09 17:18:14 +00002050 }
2051 }
Alan Cox432729f2007-03-08 23:22:59 +00002052 return 3 << ATA_SHIFT_PIO;
Alan Cox1bc4ccf2006-01-09 17:18:14 +00002053}
2054
2055/**
Alan Cox963e4972008-07-24 17:16:06 +01002056 * ata_do_dev_read_id - default ID read method
2057 * @dev: device
2058 * @tf: proposed taskfile
2059 * @id: data buffer
2060 *
2061 * Issue the identify taskfile and hand back the buffer containing
2062 * identify data. For some RAID controllers and for pre ATA devices
2063 * this function is wrapped or replaced by the driver
2064 */
2065unsigned int ata_do_dev_read_id(struct ata_device *dev,
2066 struct ata_taskfile *tf, u16 *id)
2067{
2068 return ata_exec_internal(dev, tf, NULL, DMA_FROM_DEVICE,
2069 id, sizeof(id[0]) * ATA_ID_WORDS, 0);
2070}
2071
2072/**
Tejun Heo49016ac2006-02-21 02:12:11 +09002073 * ata_dev_read_id - Read ID data from the specified device
Tejun Heo49016ac2006-02-21 02:12:11 +09002074 * @dev: target device
2075 * @p_class: pointer to class of the target device (may be changed)
Tejun Heobff04642006-11-10 18:08:10 +09002076 * @flags: ATA_READID_* flags
Tejun Heofe635c72006-05-15 20:57:35 +09002077 * @id: buffer to read IDENTIFY data into
Tejun Heo49016ac2006-02-21 02:12:11 +09002078 *
2079 * Read ID data from the specified device. ATA_CMD_ID_ATA is
2080 * performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
Tejun Heoaec5c3c2006-03-25 01:33:34 +09002081 * devices. This function also issues ATA_CMD_INIT_DEV_PARAMS
2082 * for pre-ATA4 drives.
Tejun Heo49016ac2006-02-21 02:12:11 +09002083 *
Alan Cox50a99012007-08-08 14:27:00 +01002084 * FIXME: ATA_CMD_ID_ATA is optional for early drives and right
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002085 * now we abort if we hit that case.
Alan Cox50a99012007-08-08 14:27:00 +01002086 *
Tejun Heo49016ac2006-02-21 02:12:11 +09002087 * LOCKING:
2088 * Kernel thread context (may sleep)
2089 *
2090 * RETURNS:
2091 * 0 on success, -errno otherwise.
2092 */
Tejun Heoa9beec92006-05-31 18:27:44 +09002093int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
Tejun Heobff04642006-11-10 18:08:10 +09002094 unsigned int flags, u16 *id)
Tejun Heo49016ac2006-02-21 02:12:11 +09002095{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002096 struct ata_port *ap = dev->link->ap;
Tejun Heo49016ac2006-02-21 02:12:11 +09002097 unsigned int class = *p_class;
Tejun Heo49016ac2006-02-21 02:12:11 +09002098 struct ata_taskfile tf;
2099 unsigned int err_mask = 0;
2100 const char *reason;
Tejun Heo79b42ba2009-04-15 06:21:10 +09002101 bool is_semb = class == ATA_DEV_SEMB;
Tejun Heo54936f82007-05-11 14:35:29 +02002102 int may_fallback = 1, tried_spinup = 0;
Tejun Heo49016ac2006-02-21 02:12:11 +09002103 int rc;
2104
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002105 if (ata_msg_ctl(ap))
Harvey Harrison7f5e4e82008-03-05 18:24:52 -08002106 ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER\n", __func__);
Tejun Heo49016ac2006-02-21 02:12:11 +09002107
Alan Cox963e4972008-07-24 17:16:06 +01002108retry:
Tejun Heo3373efd2006-05-15 20:57:53 +09002109 ata_tf_init(dev, &tf);
Tejun Heo49016ac2006-02-21 02:12:11 +09002110
2111 switch (class) {
Tejun Heo79b42ba2009-04-15 06:21:10 +09002112 case ATA_DEV_SEMB:
2113 class = ATA_DEV_ATA; /* some hard drives report SEMB sig */
Tejun Heo49016ac2006-02-21 02:12:11 +09002114 case ATA_DEV_ATA:
2115 tf.command = ATA_CMD_ID_ATA;
2116 break;
2117 case ATA_DEV_ATAPI:
2118 tf.command = ATA_CMD_ID_ATAPI;
2119 break;
2120 default:
2121 rc = -ENODEV;
2122 reason = "unsupported class";
2123 goto err_out;
2124 }
2125
2126 tf.protocol = ATA_PROT_PIO;
Tejun Heo81afe892007-02-07 12:37:41 -08002127
2128 /* Some devices choke if TF registers contain garbage. Make
2129 * sure those are properly initialized.
2130 */
2131 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2132
2133 /* Device presence detection is unreliable on some
2134 * controllers. Always poll IDENTIFY if available.
2135 */
2136 tf.flags |= ATA_TFLAG_POLLING;
Tejun Heo49016ac2006-02-21 02:12:11 +09002137
Alan Cox963e4972008-07-24 17:16:06 +01002138 if (ap->ops->read_id)
2139 err_mask = ap->ops->read_id(dev, &tf, id);
2140 else
2141 err_mask = ata_do_dev_read_id(dev, &tf, id);
2142
Tejun Heo49016ac2006-02-21 02:12:11 +09002143 if (err_mask) {
Tejun Heo800b3992006-12-03 21:34:13 +09002144 if (err_mask & AC_ERR_NODEV_HINT) {
Tejun Heo1ffc1512008-03-23 15:16:53 +09002145 ata_dev_printk(dev, KERN_DEBUG,
2146 "NODEV after polling detection\n");
Tejun Heo55a8e2c2006-11-10 18:08:10 +09002147 return -ENOENT;
2148 }
2149
Tejun Heo79b42ba2009-04-15 06:21:10 +09002150 if (is_semb) {
2151 ata_dev_printk(dev, KERN_INFO, "IDENTIFY failed on "
2152 "device w/ SEMB sig, disabled\n");
2153 /* SEMB is not supported yet */
2154 *p_class = ATA_DEV_SEMB_UNSUP;
2155 return 0;
2156 }
2157
Tejun Heo1ffc1512008-03-23 15:16:53 +09002158 if ((err_mask == AC_ERR_DEV) && (tf.feature & ATA_ABORTED)) {
2159 /* Device or controller might have reported
2160 * the wrong device class. Give a shot at the
2161 * other IDENTIFY if the current one is
2162 * aborted by the device.
2163 */
2164 if (may_fallback) {
2165 may_fallback = 0;
Tejun Heo54936f82007-05-11 14:35:29 +02002166
Tejun Heo1ffc1512008-03-23 15:16:53 +09002167 if (class == ATA_DEV_ATA)
2168 class = ATA_DEV_ATAPI;
2169 else
2170 class = ATA_DEV_ATA;
2171 goto retry;
2172 }
2173
2174 /* Control reaches here iff the device aborted
2175 * both flavors of IDENTIFYs which happens
2176 * sometimes with phantom devices.
2177 */
2178 ata_dev_printk(dev, KERN_DEBUG,
2179 "both IDENTIFYs aborted, assuming NODEV\n");
2180 return -ENOENT;
Tejun Heo54936f82007-05-11 14:35:29 +02002181 }
2182
Tejun Heo49016ac2006-02-21 02:12:11 +09002183 rc = -EIO;
2184 reason = "I/O error";
Tejun Heo49016ac2006-02-21 02:12:11 +09002185 goto err_out;
2186 }
2187
Tejun Heo54936f82007-05-11 14:35:29 +02002188 /* Falling back doesn't make sense if ID data was read
2189 * successfully at least once.
2190 */
2191 may_fallback = 0;
2192
Tejun Heo49016ac2006-02-21 02:12:11 +09002193 swap_buf_le16(id, ATA_ID_WORDS);
2194
Tejun Heo49016ac2006-02-21 02:12:11 +09002195 /* sanity check */
Tejun Heoa4f57492006-09-12 20:35:49 -07002196 rc = -EINVAL;
Alan Cox60700682007-06-07 16:13:55 +01002197 reason = "device reports invalid type";
Tejun Heoa4f57492006-09-12 20:35:49 -07002198
2199 if (class == ATA_DEV_ATA) {
2200 if (!ata_id_is_ata(id) && !ata_id_is_cfa(id))
2201 goto err_out;
2202 } else {
2203 if (ata_id_is_ata(id))
2204 goto err_out;
Tejun Heo49016ac2006-02-21 02:12:11 +09002205 }
2206
Mark Lord169439c2007-04-17 18:26:07 -04002207 if (!tried_spinup && (id[2] == 0x37c8 || id[2] == 0x738c)) {
2208 tried_spinup = 1;
2209 /*
2210 * Drive powered-up in standby mode, and requires a specific
2211 * SET_FEATURES spin-up subcommand before it will accept
2212 * anything other than the original IDENTIFY command.
2213 */
Jeff Garzik218f3d32007-10-25 00:33:27 -04002214 err_mask = ata_dev_set_feature(dev, SETFEATURES_SPINUP, 0);
Ryan Powerfb0582f92007-08-10 13:59:35 -07002215 if (err_mask && id[2] != 0x738c) {
Mark Lord169439c2007-04-17 18:26:07 -04002216 rc = -EIO;
2217 reason = "SPINUP failed";
2218 goto err_out;
2219 }
2220 /*
2221 * If the drive initially returned incomplete IDENTIFY info,
2222 * we now must reissue the IDENTIFY command.
2223 */
2224 if (id[2] == 0x37c8)
2225 goto retry;
2226 }
2227
Tejun Heobff04642006-11-10 18:08:10 +09002228 if ((flags & ATA_READID_POSTRESET) && class == ATA_DEV_ATA) {
Tejun Heo49016ac2006-02-21 02:12:11 +09002229 /*
2230 * The exact sequence expected by certain pre-ATA4 drives is:
2231 * SRST RESET
Alan Cox50a99012007-08-08 14:27:00 +01002232 * IDENTIFY (optional in early ATA)
2233 * INITIALIZE DEVICE PARAMETERS (later IDE and ATA)
Tejun Heo49016ac2006-02-21 02:12:11 +09002234 * anything else..
2235 * Some drives were very specific about that exact sequence.
Alan Cox50a99012007-08-08 14:27:00 +01002236 *
2237 * Note that ATA4 says lba is mandatory so the second check
Adam Buchbinderc9404c92009-12-18 15:40:42 -05002238 * should never trigger.
Tejun Heo49016ac2006-02-21 02:12:11 +09002239 */
2240 if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
Tejun Heo3373efd2006-05-15 20:57:53 +09002241 err_mask = ata_dev_init_params(dev, id[3], id[6]);
Tejun Heo49016ac2006-02-21 02:12:11 +09002242 if (err_mask) {
2243 rc = -EIO;
2244 reason = "INIT_DEV_PARAMS failed";
2245 goto err_out;
2246 }
2247
2248 /* current CHS translation info (id[53-58]) might be
2249 * changed. reread the identify device info.
2250 */
Tejun Heobff04642006-11-10 18:08:10 +09002251 flags &= ~ATA_READID_POSTRESET;
Tejun Heo49016ac2006-02-21 02:12:11 +09002252 goto retry;
2253 }
2254 }
2255
2256 *p_class = class;
Tejun Heofe635c72006-05-15 20:57:35 +09002257
Tejun Heo49016ac2006-02-21 02:12:11 +09002258 return 0;
2259
2260 err_out:
Tejun Heo88574552006-06-25 20:00:35 +09002261 if (ata_msg_warn(ap))
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002262 ata_dev_printk(dev, KERN_WARNING, "failed to IDENTIFY "
Tejun Heo88574552006-06-25 20:00:35 +09002263 "(%s, err_mask=0x%x)\n", reason, err_mask);
Tejun Heo49016ac2006-02-21 02:12:11 +09002264 return rc;
2265}
2266
Tejun Heo90627122009-01-29 20:31:36 +09002267static int ata_do_link_spd_horkage(struct ata_device *dev)
2268{
2269 struct ata_link *plink = ata_dev_phys_link(dev);
2270 u32 target, target_limit;
2271
2272 if (!sata_scr_valid(plink))
2273 return 0;
2274
2275 if (dev->horkage & ATA_HORKAGE_1_5_GBPS)
2276 target = 1;
2277 else
2278 return 0;
2279
2280 target_limit = (1 << target) - 1;
2281
2282 /* if already on stricter limit, no need to push further */
2283 if (plink->sata_spd_limit <= target_limit)
2284 return 0;
2285
2286 plink->sata_spd_limit = target_limit;
2287
2288 /* Request another EH round by returning -EAGAIN if link is
2289 * going faster than the target speed. Forward progress is
2290 * guaranteed by setting sata_spd_limit to target_limit above.
2291 */
2292 if (plink->sata_spd > target) {
2293 ata_dev_printk(dev, KERN_INFO,
2294 "applying link speed limit horkage to %s\n",
2295 sata_spd_string(target));
2296 return -EAGAIN;
2297 }
2298 return 0;
2299}
2300
Tejun Heo3373efd2006-05-15 20:57:53 +09002301static inline u8 ata_dev_knobble(struct ata_device *dev)
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09002302{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002303 struct ata_port *ap = dev->link->ap;
Jens Axboe9ce8e302008-08-27 15:23:18 +02002304
2305 if (ata_dev_blacklisted(dev) & ATA_HORKAGE_BRIDGE_OK)
2306 return 0;
2307
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002308 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09002309}
2310
Shaohua Li388539f2009-07-27 09:24:35 +08002311static int ata_dev_config_ncq(struct ata_device *dev,
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002312 char *desc, size_t desc_sz)
2313{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002314 struct ata_port *ap = dev->link->ap;
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002315 int hdepth = 0, ddepth = ata_id_queue_depth(dev->id);
Shaohua Li388539f2009-07-27 09:24:35 +08002316 unsigned int err_mask;
2317 char *aa_desc = "";
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002318
2319 if (!ata_id_has_ncq(dev->id)) {
2320 desc[0] = '\0';
Shaohua Li388539f2009-07-27 09:24:35 +08002321 return 0;
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002322 }
Tejun Heo75683fe2007-07-05 13:31:27 +09002323 if (dev->horkage & ATA_HORKAGE_NONCQ) {
Alan Cox6919a0a2006-10-27 19:08:46 -07002324 snprintf(desc, desc_sz, "NCQ (not used)");
Shaohua Li388539f2009-07-27 09:24:35 +08002325 return 0;
Alan Cox6919a0a2006-10-27 19:08:46 -07002326 }
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002327 if (ap->flags & ATA_FLAG_NCQ) {
Jeff Garzikcca39742006-08-24 03:19:22 -04002328 hdepth = min(ap->scsi_host->can_queue, ATA_MAX_QUEUE - 1);
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002329 dev->flags |= ATA_DFLAG_NCQ;
2330 }
2331
Shaohua Li388539f2009-07-27 09:24:35 +08002332 if (!(dev->horkage & ATA_HORKAGE_BROKEN_FPDMA_AA) &&
2333 (ap->flags & ATA_FLAG_FPDMA_AA) &&
2334 ata_id_has_fpdma_aa(dev->id)) {
2335 err_mask = ata_dev_set_feature(dev, SETFEATURES_SATA_ENABLE,
2336 SATA_FPDMA_AA);
2337 if (err_mask) {
2338 ata_dev_printk(dev, KERN_ERR, "failed to enable AA"
2339 "(error_mask=0x%x)\n", err_mask);
2340 if (err_mask != AC_ERR_DEV) {
2341 dev->horkage |= ATA_HORKAGE_BROKEN_FPDMA_AA;
2342 return -EIO;
2343 }
2344 } else
2345 aa_desc = ", AA";
2346 }
2347
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002348 if (hdepth >= ddepth)
Shaohua Li388539f2009-07-27 09:24:35 +08002349 snprintf(desc, desc_sz, "NCQ (depth %d)%s", ddepth, aa_desc);
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002350 else
Shaohua Li388539f2009-07-27 09:24:35 +08002351 snprintf(desc, desc_sz, "NCQ (depth %d/%d)%s", hdepth,
2352 ddepth, aa_desc);
2353 return 0;
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002354}
2355
Tejun Heo49016ac2006-02-21 02:12:11 +09002356/**
Tejun Heoffeae412006-03-01 16:09:35 +09002357 * ata_dev_configure - Configure the specified ATA/ATAPI device
Tejun Heoffeae412006-03-01 16:09:35 +09002358 * @dev: Target device to configure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 *
Tejun Heoffeae412006-03-01 16:09:35 +09002360 * Configure @dev according to @dev->id. Generic and low-level
2361 * driver specific fixups are also applied.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 *
2363 * LOCKING:
Tejun Heoffeae412006-03-01 16:09:35 +09002364 * Kernel thread context (may sleep)
2365 *
2366 * RETURNS:
2367 * 0 on success, -errno otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 */
Tejun Heoefdaedc2006-11-01 18:38:52 +09002369int ata_dev_configure(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002371 struct ata_port *ap = dev->link->ap;
2372 struct ata_eh_context *ehc = &dev->link->eh_context;
Tejun Heo67465442007-05-15 03:28:16 +09002373 int print_info = ehc->i.flags & ATA_EHI_PRINTINFO;
Tejun Heo1148c3a2006-03-13 19:48:04 +09002374 const u16 *id = dev->id;
Tejun Heo7dc951a2007-11-27 19:43:42 +09002375 unsigned long xfer_mask;
Alan Coxb352e572006-08-10 18:52:12 +01002376 char revbuf[7]; /* XYZ-99\0 */
Eric D. Mudama3f64f562007-01-30 23:00:40 -07002377 char fwrevbuf[ATA_ID_FW_REV_LEN+1];
2378 char modelbuf[ATA_ID_PROD_LEN+1];
Brian Kinge6d902a2006-06-28 08:30:31 -05002379 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002381 if (!ata_dev_enabled(dev) && ata_msg_info(ap)) {
Tejun Heo44877b42007-02-21 01:06:51 +09002382 ata_dev_printk(dev, KERN_INFO, "%s: ENTER/EXIT -- nodev\n",
Harvey Harrison7f5e4e82008-03-05 18:24:52 -08002383 __func__);
Tejun Heoffeae412006-03-01 16:09:35 +09002384 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002385 }
2386
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002387 if (ata_msg_probe(ap))
Harvey Harrison7f5e4e82008-03-05 18:24:52 -08002388 ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389
Tejun Heo75683fe2007-07-05 13:31:27 +09002390 /* set horkage */
2391 dev->horkage |= ata_dev_blacklisted(dev);
Tejun Heo33267322008-02-13 09:15:09 +09002392 ata_force_horkage(dev);
Tejun Heo75683fe2007-07-05 13:31:27 +09002393
Tejun Heo50af2fa2008-05-19 01:15:14 +09002394 if (dev->horkage & ATA_HORKAGE_DISABLE) {
2395 ata_dev_printk(dev, KERN_INFO,
2396 "unsupported device, disabling\n");
2397 ata_dev_disable(dev);
2398 return 0;
2399 }
2400
Tejun Heo2486fa52008-07-31 07:52:40 +09002401 if ((!atapi_enabled || (ap->flags & ATA_FLAG_NO_ATAPI)) &&
2402 dev->class == ATA_DEV_ATAPI) {
2403 ata_dev_printk(dev, KERN_WARNING,
2404 "WARNING: ATAPI is %s, device ignored.\n",
2405 atapi_enabled ? "not supported with this driver"
2406 : "disabled");
2407 ata_dev_disable(dev);
2408 return 0;
2409 }
2410
Tejun Heo90627122009-01-29 20:31:36 +09002411 rc = ata_do_link_spd_horkage(dev);
2412 if (rc)
2413 return rc;
2414
Tejun Heo67465442007-05-15 03:28:16 +09002415 /* let ACPI work its magic */
2416 rc = ata_acpi_on_devcfg(dev);
2417 if (rc)
2418 return rc;
Kristen Carlson Accardi08573a82006-11-10 16:14:47 -08002419
Tejun Heo05027ad2007-09-03 12:32:57 +09002420 /* massage HPA, do it early as it might change IDENTIFY data */
2421 rc = ata_hpa_resize(dev);
2422 if (rc)
2423 return rc;
2424
Tejun Heoc39f5eb2006-03-13 19:51:19 +09002425 /* print device capabilities */
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002426 if (ata_msg_probe(ap))
Tejun Heo88574552006-06-25 20:00:35 +09002427 ata_dev_printk(dev, KERN_DEBUG,
2428 "%s: cfg 49:%04x 82:%04x 83:%04x 84:%04x "
2429 "85:%04x 86:%04x 87:%04x 88:%04x\n",
Harvey Harrison7f5e4e82008-03-05 18:24:52 -08002430 __func__,
Tejun Heof15a1da2006-05-15 20:57:56 +09002431 id[49], id[82], id[83], id[84],
2432 id[85], id[86], id[87], id[88]);
Tejun Heoc39f5eb2006-03-13 19:51:19 +09002433
Tejun Heo208a9932006-03-05 17:55:58 +09002434 /* initialize to-be-configured parameters */
Tejun Heoea1dd4e2006-04-02 18:51:53 +09002435 dev->flags &= ~ATA_DFLAG_CFG_MASK;
Tejun Heo208a9932006-03-05 17:55:58 +09002436 dev->max_sectors = 0;
2437 dev->cdb_len = 0;
2438 dev->n_sectors = 0;
2439 dev->cylinders = 0;
2440 dev->heads = 0;
2441 dev->sectors = 0;
Mark Lorde18086d2009-03-19 13:32:21 -04002442 dev->multi_count = 0;
Tejun Heo208a9932006-03-05 17:55:58 +09002443
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444 /*
2445 * common ATA, ATAPI feature tests
2446 */
2447
Tejun Heoff8854b2006-03-06 04:31:56 +09002448 /* find max transfer mode; for printk only */
Tejun Heo1148c3a2006-03-13 19:48:04 +09002449 xfer_mask = ata_id_xfermask(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002451 if (ata_msg_probe(ap))
2452 ata_dump_id(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453
Albert Leeef143d52007-06-05 13:01:33 +08002454 /* SCSI only uses 4-char revisions, dump full 8 chars from ATA */
2455 ata_id_c_string(dev->id, fwrevbuf, ATA_ID_FW_REV,
2456 sizeof(fwrevbuf));
2457
2458 ata_id_c_string(dev->id, modelbuf, ATA_ID_PROD,
2459 sizeof(modelbuf));
2460
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461 /* ATA-specific feature tests */
2462 if (dev->class == ATA_DEV_ATA) {
Alan Coxb352e572006-08-10 18:52:12 +01002463 if (ata_id_is_cfa(id)) {
Sergei Shtylyov62afe5d2009-04-13 20:50:00 +04002464 /* CPRM may make this media unusable */
2465 if (id[ATA_ID_CFA_KEY_MGMT] & 1)
Tejun Heo44877b42007-02-21 01:06:51 +09002466 ata_dev_printk(dev, KERN_WARNING,
2467 "supports DRM functions and may "
2468 "not be fully accessable.\n");
Alan Coxb352e572006-08-10 18:52:12 +01002469 snprintf(revbuf, 7, "CFA");
Alan Coxae8d4ee2007-11-04 22:05:49 -05002470 } else {
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002471 snprintf(revbuf, 7, "ATA-%d", ata_id_major_version(id));
Alan Coxae8d4ee2007-11-04 22:05:49 -05002472 /* Warn the user if the device has TPM extensions */
2473 if (ata_id_has_tpm(id))
2474 ata_dev_printk(dev, KERN_WARNING,
2475 "supports DRM functions and may "
2476 "not be fully accessable.\n");
2477 }
Alan Coxb352e572006-08-10 18:52:12 +01002478
Tejun Heo1148c3a2006-03-13 19:48:04 +09002479 dev->n_sectors = ata_id_n_sectors(id);
Tejun Heo29407402006-02-12 22:47:04 +09002480
Mark Lorde18086d2009-03-19 13:32:21 -04002481 /* get current R/W Multiple count setting */
2482 if ((dev->id[47] >> 8) == 0x80 && (dev->id[59] & 0x100)) {
2483 unsigned int max = dev->id[47] & 0xff;
2484 unsigned int cnt = dev->id[59] & 0xff;
2485 /* only recognize/allow powers of two here */
2486 if (is_power_of_2(max) && is_power_of_2(cnt))
2487 if (cnt <= max)
2488 dev->multi_count = cnt;
2489 }
Eric D. Mudama3f64f562007-01-30 23:00:40 -07002490
Tejun Heo1148c3a2006-03-13 19:48:04 +09002491 if (ata_id_has_lba(id)) {
Tejun Heo4c2d7212006-03-05 17:55:58 +09002492 const char *lba_desc;
Shaohua Li388539f2009-07-27 09:24:35 +08002493 char ncq_desc[24];
Albert Lee8bf62ece2005-05-12 15:29:42 -04002494
Tejun Heo4c2d7212006-03-05 17:55:58 +09002495 lba_desc = "LBA";
2496 dev->flags |= ATA_DFLAG_LBA;
Tejun Heo1148c3a2006-03-13 19:48:04 +09002497 if (ata_id_has_lba48(id)) {
Albert Lee8bf62ece2005-05-12 15:29:42 -04002498 dev->flags |= ATA_DFLAG_LBA48;
Tejun Heo4c2d7212006-03-05 17:55:58 +09002499 lba_desc = "LBA48";
Tejun Heo6fc49ad2006-11-11 20:10:45 +09002500
2501 if (dev->n_sectors >= (1UL << 28) &&
2502 ata_id_has_flush_ext(id))
2503 dev->flags |= ATA_DFLAG_FLUSH_EXT;
Tejun Heo4c2d7212006-03-05 17:55:58 +09002504 }
Albert Lee8bf62ece2005-05-12 15:29:42 -04002505
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002506 /* config NCQ */
Shaohua Li388539f2009-07-27 09:24:35 +08002507 rc = ata_dev_config_ncq(dev, ncq_desc, sizeof(ncq_desc));
2508 if (rc)
2509 return rc;
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002510
Albert Lee8bf62ece2005-05-12 15:29:42 -04002511 /* print device info to dmesg */
Eric D. Mudama3f64f562007-01-30 23:00:40 -07002512 if (ata_msg_drv(ap) && print_info) {
2513 ata_dev_printk(dev, KERN_INFO,
2514 "%s: %s, %s, max %s\n",
2515 revbuf, modelbuf, fwrevbuf,
2516 ata_mode_string(xfer_mask));
2517 ata_dev_printk(dev, KERN_INFO,
2518 "%Lu sectors, multi %u: %s %s\n",
Tejun Heof15a1da2006-05-15 20:57:56 +09002519 (unsigned long long)dev->n_sectors,
Eric D. Mudama3f64f562007-01-30 23:00:40 -07002520 dev->multi_count, lba_desc, ncq_desc);
2521 }
Tejun Heoffeae412006-03-01 16:09:35 +09002522 } else {
Albert Lee8bf62ece2005-05-12 15:29:42 -04002523 /* CHS */
2524
2525 /* Default translation */
Tejun Heo1148c3a2006-03-13 19:48:04 +09002526 dev->cylinders = id[1];
2527 dev->heads = id[3];
2528 dev->sectors = id[6];
Albert Lee8bf62ece2005-05-12 15:29:42 -04002529
Tejun Heo1148c3a2006-03-13 19:48:04 +09002530 if (ata_id_current_chs_valid(id)) {
Albert Lee8bf62ece2005-05-12 15:29:42 -04002531 /* Current CHS translation is valid. */
Tejun Heo1148c3a2006-03-13 19:48:04 +09002532 dev->cylinders = id[54];
2533 dev->heads = id[55];
2534 dev->sectors = id[56];
Albert Lee8bf62ece2005-05-12 15:29:42 -04002535 }
2536
2537 /* print device info to dmesg */
Eric D. Mudama3f64f562007-01-30 23:00:40 -07002538 if (ata_msg_drv(ap) && print_info) {
Tejun Heo88574552006-06-25 20:00:35 +09002539 ata_dev_printk(dev, KERN_INFO,
Eric D. Mudama3f64f562007-01-30 23:00:40 -07002540 "%s: %s, %s, max %s\n",
2541 revbuf, modelbuf, fwrevbuf,
2542 ata_mode_string(xfer_mask));
Jeff Garzika84471f2007-02-26 05:51:33 -05002543 ata_dev_printk(dev, KERN_INFO,
Eric D. Mudama3f64f562007-01-30 23:00:40 -07002544 "%Lu sectors, multi %u, CHS %u/%u/%u\n",
2545 (unsigned long long)dev->n_sectors,
2546 dev->multi_count, dev->cylinders,
2547 dev->heads, dev->sectors);
2548 }
Albert Lee07f6f7d2005-11-01 19:33:20 +08002549 }
2550
Tejun Heo6e7846e2006-02-12 23:32:58 +09002551 dev->cdb_len = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 }
2553
2554 /* ATAPI-specific feature tests */
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05002555 else if (dev->class == ATA_DEV_ATAPI) {
Tejun Heo854c73a2007-09-23 13:14:11 +09002556 const char *cdb_intr_string = "";
2557 const char *atapi_an_string = "";
Tejun Heo91163002008-02-21 13:25:50 +09002558 const char *dma_dir_string = "";
Tejun Heo7d77b242007-09-23 13:14:13 +09002559 u32 sntf;
Albert Lee08a556d2006-03-31 13:29:04 +08002560
Tejun Heo1148c3a2006-03-13 19:48:04 +09002561 rc = atapi_cdb_len(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002563 if (ata_msg_warn(ap))
Tejun Heo88574552006-06-25 20:00:35 +09002564 ata_dev_printk(dev, KERN_WARNING,
2565 "unsupported CDB len\n");
Tejun Heoffeae412006-03-01 16:09:35 +09002566 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 goto err_out_nosup;
2568 }
Tejun Heo6e7846e2006-02-12 23:32:58 +09002569 dev->cdb_len = (unsigned int) rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002570
Tejun Heo7d77b242007-09-23 13:14:13 +09002571 /* Enable ATAPI AN if both the host and device have
2572 * the support. If PMP is attached, SNTF is required
2573 * to enable ATAPI AN to discern between PHY status
2574 * changed notifications and ATAPI ANs.
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04002575 */
Tejun Heo7d77b242007-09-23 13:14:13 +09002576 if ((ap->flags & ATA_FLAG_AN) && ata_id_has_atapi_AN(id) &&
Tejun Heo071f44b2008-04-07 22:47:22 +09002577 (!sata_pmp_attached(ap) ||
Tejun Heo7d77b242007-09-23 13:14:13 +09002578 sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf) == 0)) {
Tejun Heo854c73a2007-09-23 13:14:11 +09002579 unsigned int err_mask;
2580
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04002581 /* issue SET feature command to turn this on */
Jeff Garzik218f3d32007-10-25 00:33:27 -04002582 err_mask = ata_dev_set_feature(dev,
2583 SETFEATURES_SATA_ENABLE, SATA_AN);
Tejun Heo854c73a2007-09-23 13:14:11 +09002584 if (err_mask)
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04002585 ata_dev_printk(dev, KERN_ERR,
Tejun Heo854c73a2007-09-23 13:14:11 +09002586 "failed to enable ATAPI AN "
2587 "(err_mask=0x%x)\n", err_mask);
2588 else {
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04002589 dev->flags |= ATA_DFLAG_AN;
Tejun Heo854c73a2007-09-23 13:14:11 +09002590 atapi_an_string = ", ATAPI AN";
2591 }
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04002592 }
2593
Albert Lee08a556d2006-03-31 13:29:04 +08002594 if (ata_id_cdb_intr(dev->id)) {
Albert Lee312f7da2005-09-27 17:38:03 +08002595 dev->flags |= ATA_DFLAG_CDB_INTR;
Albert Lee08a556d2006-03-31 13:29:04 +08002596 cdb_intr_string = ", CDB intr";
2597 }
Albert Lee312f7da2005-09-27 17:38:03 +08002598
Tejun Heo91163002008-02-21 13:25:50 +09002599 if (atapi_dmadir || atapi_id_dmadir(dev->id)) {
2600 dev->flags |= ATA_DFLAG_DMADIR;
2601 dma_dir_string = ", DMADIR";
2602 }
2603
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 /* print device info to dmesg */
Borislav Petkov5afc8142006-06-27 14:51:25 +02002605 if (ata_msg_drv(ap) && print_info)
Albert Leeef143d52007-06-05 13:01:33 +08002606 ata_dev_printk(dev, KERN_INFO,
Tejun Heo91163002008-02-21 13:25:50 +09002607 "ATAPI: %s, %s, max %s%s%s%s\n",
Albert Leeef143d52007-06-05 13:01:33 +08002608 modelbuf, fwrevbuf,
Tejun Heo12436c32006-05-15 20:59:15 +09002609 ata_mode_string(xfer_mask),
Tejun Heo91163002008-02-21 13:25:50 +09002610 cdb_intr_string, atapi_an_string,
2611 dma_dir_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 }
2613
Tejun Heo914ed352006-11-01 18:39:55 +09002614 /* determine max_sectors */
2615 dev->max_sectors = ATA_MAX_SECTORS;
2616 if (dev->flags & ATA_DFLAG_LBA48)
2617 dev->max_sectors = ATA_MAX_SECTORS_LBA48;
2618
Kristen Carlson Accardica773292007-10-25 00:58:59 -04002619 if (!(dev->horkage & ATA_HORKAGE_IPM)) {
2620 if (ata_id_has_hipm(dev->id))
2621 dev->flags |= ATA_DFLAG_HIPM;
2622 if (ata_id_has_dipm(dev->id))
2623 dev->flags |= ATA_DFLAG_DIPM;
2624 }
2625
Alan Coxc5038fc2007-10-25 14:21:16 +01002626 /* Limit PATA drive on SATA cable bridge transfers to udma5,
2627 200 sectors */
Tejun Heo3373efd2006-05-15 20:57:53 +09002628 if (ata_dev_knobble(dev)) {
Borislav Petkov5afc8142006-06-27 14:51:25 +02002629 if (ata_msg_drv(ap) && print_info)
Tejun Heof15a1da2006-05-15 20:57:56 +09002630 ata_dev_printk(dev, KERN_INFO,
2631 "applying bridge limits\n");
Tejun Heo5a529132006-03-24 14:07:50 +09002632 dev->udma_mask &= ATA_UDMA5;
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09002633 dev->max_sectors = ATA_MAX_SECTORS;
2634 }
2635
Tony Battersbyf8d8e572007-10-30 11:44:35 -04002636 if ((dev->class == ATA_DEV_ATAPI) &&
Albert Leef442cd82007-11-15 10:35:47 +09002637 (atapi_command_packet_set(id) == TYPE_TAPE)) {
Tony Battersbyf8d8e572007-10-30 11:44:35 -04002638 dev->max_sectors = ATA_MAX_SECTORS_TAPE;
Albert Leef442cd82007-11-15 10:35:47 +09002639 dev->horkage |= ATA_HORKAGE_STUCK_ERR;
2640 }
Tony Battersbyf8d8e572007-10-30 11:44:35 -04002641
Tejun Heo75683fe2007-07-05 13:31:27 +09002642 if (dev->horkage & ATA_HORKAGE_MAX_SEC_128)
Tejun Heo03ec52d2007-04-12 13:38:11 +09002643 dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_128,
2644 dev->max_sectors);
Albert Lee18d6e9d2007-04-02 11:34:15 +08002645
Kristen Carlson Accardica773292007-10-25 00:58:59 -04002646 if (ata_dev_blacklisted(dev) & ATA_HORKAGE_IPM) {
2647 dev->horkage |= ATA_HORKAGE_IPM;
2648
2649 /* reset link pm_policy for this port to no pm */
2650 ap->pm_policy = MAX_PERFORMANCE;
2651 }
2652
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09002653 if (ap->ops->dev_config)
Alancd0d3bb2007-03-02 00:56:15 +00002654 ap->ops->dev_config(dev);
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09002655
Alan Coxc5038fc2007-10-25 14:21:16 +01002656 if (dev->horkage & ATA_HORKAGE_DIAGNOSTIC) {
2657 /* Let the user know. We don't want to disallow opens for
2658 rescue purposes, or in case the vendor is just a blithering
2659 idiot. Do this after the dev_config call as some controllers
2660 with buggy firmware may want to avoid reporting false device
2661 bugs */
2662
2663 if (print_info) {
2664 ata_dev_printk(dev, KERN_WARNING,
2665"Drive reports diagnostics failure. This may indicate a drive\n");
2666 ata_dev_printk(dev, KERN_WARNING,
2667"fault or invalid emulation. Contact drive vendor for information.\n");
2668 }
2669 }
2670
Tejun Heoac70a962008-11-27 13:36:48 +09002671 if ((dev->horkage & ATA_HORKAGE_FIRMWARE_WARN) && print_info) {
2672 ata_dev_printk(dev, KERN_WARNING, "WARNING: device requires "
2673 "firmware update to be fully functional.\n");
2674 ata_dev_printk(dev, KERN_WARNING, " contact the vendor "
2675 "or visit http://ata.wiki.kernel.org.\n");
2676 }
2677
Tejun Heoffeae412006-03-01 16:09:35 +09002678 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679
2680err_out_nosup:
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002681 if (ata_msg_probe(ap))
Tejun Heo88574552006-06-25 20:00:35 +09002682 ata_dev_printk(dev, KERN_DEBUG,
Harvey Harrison7f5e4e82008-03-05 18:24:52 -08002683 "%s: EXIT, err\n", __func__);
Tejun Heoffeae412006-03-01 16:09:35 +09002684 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685}
2686
2687/**
Alan Cox2e41e8e2007-03-08 23:19:19 +00002688 * ata_cable_40wire - return 40 wire cable type
Alan Coxbe0d18d2007-03-06 02:37:56 -08002689 * @ap: port
2690 *
Alan Cox2e41e8e2007-03-08 23:19:19 +00002691 * Helper method for drivers which want to hardwire 40 wire cable
Alan Coxbe0d18d2007-03-06 02:37:56 -08002692 * detection.
2693 */
2694
2695int ata_cable_40wire(struct ata_port *ap)
2696{
2697 return ATA_CBL_PATA40;
2698}
2699
2700/**
Alan Cox2e41e8e2007-03-08 23:19:19 +00002701 * ata_cable_80wire - return 80 wire cable type
Alan Coxbe0d18d2007-03-06 02:37:56 -08002702 * @ap: port
2703 *
Alan Cox2e41e8e2007-03-08 23:19:19 +00002704 * Helper method for drivers which want to hardwire 80 wire cable
Alan Coxbe0d18d2007-03-06 02:37:56 -08002705 * detection.
2706 */
2707
2708int ata_cable_80wire(struct ata_port *ap)
2709{
2710 return ATA_CBL_PATA80;
2711}
2712
2713/**
2714 * ata_cable_unknown - return unknown PATA cable.
2715 * @ap: port
2716 *
2717 * Helper method for drivers which have no PATA cable detection.
2718 */
2719
2720int ata_cable_unknown(struct ata_port *ap)
2721{
2722 return ATA_CBL_PATA_UNK;
2723}
2724
2725/**
Tejun Heoc88f90c2007-11-27 19:43:48 +09002726 * ata_cable_ignore - return ignored PATA cable.
2727 * @ap: port
2728 *
2729 * Helper method for drivers which don't use cable type to limit
2730 * transfer mode.
2731 */
2732int ata_cable_ignore(struct ata_port *ap)
2733{
2734 return ATA_CBL_PATA_IGN;
2735}
2736
2737/**
Alan Coxbe0d18d2007-03-06 02:37:56 -08002738 * ata_cable_sata - return SATA cable type
2739 * @ap: port
2740 *
2741 * Helper method for drivers which have SATA cables
2742 */
2743
2744int ata_cable_sata(struct ata_port *ap)
2745{
2746 return ATA_CBL_SATA;
2747}
2748
2749/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 * ata_bus_probe - Reset and probe ATA bus
2751 * @ap: Bus to probe
2752 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04002753 * Master ATA bus probing function. Initiates a hardware-dependent
2754 * bus reset, then attempts to identify any devices found on
2755 * the bus.
2756 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002757 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002758 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 *
2760 * RETURNS:
Tejun Heo96072e62006-04-01 01:38:17 +09002761 * Zero on success, negative errno otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002762 */
2763
Brian King80289162006-08-07 14:27:31 -05002764int ata_bus_probe(struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765{
Tejun Heo28ca5c52006-03-01 16:09:36 +09002766 unsigned int classes[ATA_MAX_DEVICES];
Tejun Heo14d2bac2006-04-02 17:54:46 +09002767 int tries[ATA_MAX_DEVICES];
Tejun Heof58229f2007-08-06 18:36:23 +09002768 int rc;
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002769 struct ata_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770
Tejun Heo28ca5c52006-03-01 16:09:36 +09002771 ata_port_probe(ap);
2772
Tejun Heo1eca4362008-11-03 20:03:17 +09002773 ata_for_each_dev(dev, &ap->link, ALL)
Tejun Heof58229f2007-08-06 18:36:23 +09002774 tries[dev->devno] = ATA_PROBE_MAX_TRIES;
Tejun Heo14d2bac2006-04-02 17:54:46 +09002775
2776 retry:
Tejun Heo1eca4362008-11-03 20:03:17 +09002777 ata_for_each_dev(dev, &ap->link, ALL) {
Tejun Heocdeab112007-10-29 16:41:09 +09002778 /* If we issue an SRST then an ATA drive (not ATAPI)
2779 * may change configuration and be in PIO0 timing. If
2780 * we do a hard reset (or are coming from power on)
2781 * this is true for ATA or ATAPI. Until we've set a
2782 * suitable controller mode we should not touch the
2783 * bus as we may be talking too fast.
2784 */
2785 dev->pio_mode = XFER_PIO_0;
2786
2787 /* If the controller has a pio mode setup function
2788 * then use it to set the chipset to rights. Don't
2789 * touch the DMA setup as that will be dealt with when
2790 * configuring devices.
2791 */
2792 if (ap->ops->set_piomode)
2793 ap->ops->set_piomode(ap, dev);
2794 }
2795
Tejun Heo20444702006-03-13 01:57:01 +09002796 /* reset and determine device classes */
Tejun Heo52783c52006-05-31 18:28:22 +09002797 ap->ops->phy_reset(ap);
Tejun Heo2061a472006-03-12 00:57:39 +09002798
Tejun Heo1eca4362008-11-03 20:03:17 +09002799 ata_for_each_dev(dev, &ap->link, ALL) {
Tejun Heo52783c52006-05-31 18:28:22 +09002800 if (!(ap->flags & ATA_FLAG_DISABLED) &&
2801 dev->class != ATA_DEV_UNKNOWN)
2802 classes[dev->devno] = dev->class;
2803 else
2804 classes[dev->devno] = ATA_DEV_NONE;
Tejun Heo20444702006-03-13 01:57:01 +09002805
Tejun Heo52783c52006-05-31 18:28:22 +09002806 dev->class = ATA_DEV_UNKNOWN;
Tejun Heo28ca5c52006-03-01 16:09:36 +09002807 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002808
Tejun Heo52783c52006-05-31 18:28:22 +09002809 ata_port_probe(ap);
Tejun Heo20444702006-03-13 01:57:01 +09002810
Jeff Garzikf31f0cc2007-03-02 17:47:28 -05002811 /* read IDENTIFY page and configure devices. We have to do the identify
2812 specific sequence bass-ackwards so that PDIAG- is released by
2813 the slave device */
2814
Tejun Heo1eca4362008-11-03 20:03:17 +09002815 ata_for_each_dev(dev, &ap->link, ALL_REVERSE) {
Tejun Heof58229f2007-08-06 18:36:23 +09002816 if (tries[dev->devno])
2817 dev->class = classes[dev->devno];
Tejun Heo14d2bac2006-04-02 17:54:46 +09002818
Tejun Heoe1211e32006-04-01 01:38:18 +09002819 if (!ata_dev_enabled(dev))
Tejun Heoffeae412006-03-01 16:09:35 +09002820 continue;
2821
Tejun Heobff04642006-11-10 18:08:10 +09002822 rc = ata_dev_read_id(dev, &dev->class, ATA_READID_POSTRESET,
2823 dev->id);
Tejun Heo14d2bac2006-04-02 17:54:46 +09002824 if (rc)
2825 goto fail;
Jeff Garzikf31f0cc2007-03-02 17:47:28 -05002826 }
2827
Alan Coxbe0d18d2007-03-06 02:37:56 -08002828 /* Now ask for the cable type as PDIAG- should have been released */
2829 if (ap->ops->cable_detect)
2830 ap->cbl = ap->ops->cable_detect(ap);
2831
Tejun Heo1eca4362008-11-03 20:03:17 +09002832 /* We may have SATA bridge glue hiding here irrespective of
2833 * the reported cable types and sensed types. When SATA
2834 * drives indicate we have a bridge, we don't know which end
2835 * of the link the bridge is which is a problem.
2836 */
2837 ata_for_each_dev(dev, &ap->link, ENABLED)
Alan Cox614fe292007-08-22 23:22:45 +01002838 if (ata_id_is_sata(dev->id))
2839 ap->cbl = ATA_CBL_SATA;
Alan Cox614fe292007-08-22 23:22:45 +01002840
Jeff Garzikf31f0cc2007-03-02 17:47:28 -05002841 /* After the identify sequence we can now set up the devices. We do
2842 this in the normal order so that the user doesn't get confused */
2843
Tejun Heo1eca4362008-11-03 20:03:17 +09002844 ata_for_each_dev(dev, &ap->link, ENABLED) {
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002845 ap->link.eh_context.i.flags |= ATA_EHI_PRINTINFO;
Tejun Heoefdaedc2006-11-01 18:38:52 +09002846 rc = ata_dev_configure(dev);
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002847 ap->link.eh_context.i.flags &= ~ATA_EHI_PRINTINFO;
Tejun Heo14d2bac2006-04-02 17:54:46 +09002848 if (rc)
2849 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850 }
2851
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002852 /* configure transfer mode */
Tejun Heo02607312007-08-06 18:36:23 +09002853 rc = ata_set_mode(&ap->link, &dev);
Tejun Heo4ae72a12007-02-02 16:22:30 +09002854 if (rc)
Tejun Heo51713d32006-04-11 22:26:29 +09002855 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856
Tejun Heo1eca4362008-11-03 20:03:17 +09002857 ata_for_each_dev(dev, &ap->link, ENABLED)
2858 return 0;
Alan Coxe35a9e02006-03-27 18:46:37 +01002859
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002860 /* no device present, disable port */
2861 ata_port_disable(ap);
Tejun Heo96072e62006-04-01 01:38:17 +09002862 return -ENODEV;
Tejun Heo14d2bac2006-04-02 17:54:46 +09002863
2864 fail:
Tejun Heo4ae72a12007-02-02 16:22:30 +09002865 tries[dev->devno]--;
2866
Tejun Heo14d2bac2006-04-02 17:54:46 +09002867 switch (rc) {
2868 case -EINVAL:
Tejun Heo4ae72a12007-02-02 16:22:30 +09002869 /* eeek, something went very wrong, give up */
Tejun Heo14d2bac2006-04-02 17:54:46 +09002870 tries[dev->devno] = 0;
2871 break;
Tejun Heo4ae72a12007-02-02 16:22:30 +09002872
2873 case -ENODEV:
2874 /* give it just one more chance */
2875 tries[dev->devno] = min(tries[dev->devno], 1);
Tejun Heo14d2bac2006-04-02 17:54:46 +09002876 case -EIO:
Tejun Heo4ae72a12007-02-02 16:22:30 +09002877 if (tries[dev->devno] == 1) {
2878 /* This is the last chance, better to slow
2879 * down than lose it.
2880 */
Tejun Heoa07d4992009-01-29 20:31:33 +09002881 sata_down_spd_limit(&ap->link, 0);
Tejun Heo4ae72a12007-02-02 16:22:30 +09002882 ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
2883 }
Tejun Heo14d2bac2006-04-02 17:54:46 +09002884 }
2885
Tejun Heo4ae72a12007-02-02 16:22:30 +09002886 if (!tries[dev->devno])
Tejun Heo3373efd2006-05-15 20:57:53 +09002887 ata_dev_disable(dev);
Tejun Heoec573752006-04-11 22:26:29 +09002888
Tejun Heo14d2bac2006-04-02 17:54:46 +09002889 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890}
2891
2892/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002893 * ata_port_probe - Mark port as enabled
2894 * @ap: Port for which we indicate enablement
Linus Torvalds1da177e2005-04-16 15:20:36 -07002895 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04002896 * Modify @ap data structure such that the system
2897 * thinks that the entire port is enabled.
2898 *
Jeff Garzikcca39742006-08-24 03:19:22 -04002899 * LOCKING: host lock, or some other form of
Jeff Garzik0cba6322005-05-30 19:49:12 -04002900 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 */
2902
2903void ata_port_probe(struct ata_port *ap)
2904{
Tejun Heo198e0fe2006-04-02 18:51:52 +09002905 ap->flags &= ~ATA_FLAG_DISABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906}
2907
2908/**
Tejun Heo3be680b2005-12-19 22:35:02 +09002909 * sata_print_link_status - Print SATA link status
Tejun Heo936fd732007-08-06 18:36:23 +09002910 * @link: SATA link to printk link status about
Tejun Heo3be680b2005-12-19 22:35:02 +09002911 *
2912 * This function prints link speed and status of a SATA link.
2913 *
2914 * LOCKING:
2915 * None.
2916 */
Adrian Bunk6bdb4fc2008-04-21 11:51:11 +03002917static void sata_print_link_status(struct ata_link *link)
Tejun Heo3be680b2005-12-19 22:35:02 +09002918{
Tejun Heo6d5f9732006-04-03 00:09:41 +09002919 u32 sstatus, scontrol, tmp;
Tejun Heo3be680b2005-12-19 22:35:02 +09002920
Tejun Heo936fd732007-08-06 18:36:23 +09002921 if (sata_scr_read(link, SCR_STATUS, &sstatus))
Tejun Heo3be680b2005-12-19 22:35:02 +09002922 return;
Tejun Heo936fd732007-08-06 18:36:23 +09002923 sata_scr_read(link, SCR_CONTROL, &scontrol);
Tejun Heo3be680b2005-12-19 22:35:02 +09002924
Tejun Heob1c72912008-07-31 17:02:43 +09002925 if (ata_phys_link_online(link)) {
Tejun Heo3be680b2005-12-19 22:35:02 +09002926 tmp = (sstatus >> 4) & 0xf;
Tejun Heo936fd732007-08-06 18:36:23 +09002927 ata_link_printk(link, KERN_INFO,
Tejun Heof15a1da2006-05-15 20:57:56 +09002928 "SATA link up %s (SStatus %X SControl %X)\n",
2929 sata_spd_string(tmp), sstatus, scontrol);
Tejun Heo3be680b2005-12-19 22:35:02 +09002930 } else {
Tejun Heo936fd732007-08-06 18:36:23 +09002931 ata_link_printk(link, KERN_INFO,
Tejun Heof15a1da2006-05-15 20:57:56 +09002932 "SATA link down (SStatus %X SControl %X)\n",
2933 sstatus, scontrol);
Tejun Heo3be680b2005-12-19 22:35:02 +09002934 }
2935}
2936
2937/**
Alan Coxebdfca62006-03-23 15:38:34 +00002938 * ata_dev_pair - return other device on cable
Alan Coxebdfca62006-03-23 15:38:34 +00002939 * @adev: device
2940 *
2941 * Obtain the other device on the same cable, or if none is
2942 * present NULL is returned
2943 */
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002944
Tejun Heo3373efd2006-05-15 20:57:53 +09002945struct ata_device *ata_dev_pair(struct ata_device *adev)
Alan Coxebdfca62006-03-23 15:38:34 +00002946{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002947 struct ata_link *link = adev->link;
2948 struct ata_device *pair = &link->device[1 - adev->devno];
Tejun Heoe1211e32006-04-01 01:38:18 +09002949 if (!ata_dev_enabled(pair))
Alan Coxebdfca62006-03-23 15:38:34 +00002950 return NULL;
2951 return pair;
2952}
2953
2954/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04002955 * ata_port_disable - Disable port.
2956 * @ap: Port to be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002958 * Modify @ap data structure such that the system
2959 * thinks that the entire port is disabled, and should
2960 * never attempt to probe or communicate with devices
2961 * on this port.
2962 *
Jeff Garzikcca39742006-08-24 03:19:22 -04002963 * LOCKING: host lock, or some other form of
Jeff Garzik780a87f2005-05-30 15:41:05 -04002964 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 */
2966
2967void ata_port_disable(struct ata_port *ap)
2968{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002969 ap->link.device[0].class = ATA_DEV_NONE;
2970 ap->link.device[1].class = ATA_DEV_NONE;
Tejun Heo198e0fe2006-04-02 18:51:52 +09002971 ap->flags |= ATA_FLAG_DISABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972}
2973
Tejun Heo1c3fae42006-04-02 20:53:28 +09002974/**
Tejun Heo3c567b72006-05-15 20:57:23 +09002975 * sata_down_spd_limit - adjust SATA spd limit downward
Tejun Heo936fd732007-08-06 18:36:23 +09002976 * @link: Link to adjust SATA spd limit for
Tejun Heoa07d4992009-01-29 20:31:33 +09002977 * @spd_limit: Additional limit
Tejun Heo1c3fae42006-04-02 20:53:28 +09002978 *
Tejun Heo936fd732007-08-06 18:36:23 +09002979 * Adjust SATA spd limit of @link downward. Note that this
Tejun Heo1c3fae42006-04-02 20:53:28 +09002980 * function only adjusts the limit. The change must be applied
Tejun Heo3c567b72006-05-15 20:57:23 +09002981 * using sata_set_spd().
Tejun Heo1c3fae42006-04-02 20:53:28 +09002982 *
Tejun Heoa07d4992009-01-29 20:31:33 +09002983 * If @spd_limit is non-zero, the speed is limited to equal to or
2984 * lower than @spd_limit if such speed is supported. If
2985 * @spd_limit is slower than any supported speed, only the lowest
2986 * supported speed is allowed.
2987 *
Tejun Heo1c3fae42006-04-02 20:53:28 +09002988 * LOCKING:
2989 * Inherited from caller.
2990 *
2991 * RETURNS:
2992 * 0 on success, negative errno on failure
2993 */
Tejun Heoa07d4992009-01-29 20:31:33 +09002994int sata_down_spd_limit(struct ata_link *link, u32 spd_limit)
Tejun Heo1c3fae42006-04-02 20:53:28 +09002995{
Tejun Heo81952c52006-05-15 20:57:47 +09002996 u32 sstatus, spd, mask;
Tejun Heoa07d4992009-01-29 20:31:33 +09002997 int rc, bit;
Tejun Heo1c3fae42006-04-02 20:53:28 +09002998
Tejun Heo936fd732007-08-06 18:36:23 +09002999 if (!sata_scr_valid(link))
Tejun Heo008a7892007-07-16 14:29:40 +09003000 return -EOPNOTSUPP;
3001
3002 /* If SCR can be read, use it to determine the current SPD.
Tejun Heo936fd732007-08-06 18:36:23 +09003003 * If not, use cached value in link->sata_spd.
Tejun Heo008a7892007-07-16 14:29:40 +09003004 */
Tejun Heo936fd732007-08-06 18:36:23 +09003005 rc = sata_scr_read(link, SCR_STATUS, &sstatus);
Tejun Heo9913ff82009-01-29 20:31:31 +09003006 if (rc == 0 && ata_sstatus_online(sstatus))
Tejun Heo008a7892007-07-16 14:29:40 +09003007 spd = (sstatus >> 4) & 0xf;
3008 else
Tejun Heo936fd732007-08-06 18:36:23 +09003009 spd = link->sata_spd;
Tejun Heo1c3fae42006-04-02 20:53:28 +09003010
Tejun Heo936fd732007-08-06 18:36:23 +09003011 mask = link->sata_spd_limit;
Tejun Heo1c3fae42006-04-02 20:53:28 +09003012 if (mask <= 1)
3013 return -EINVAL;
Tejun Heo008a7892007-07-16 14:29:40 +09003014
3015 /* unconditionally mask off the highest bit */
Tejun Heoa07d4992009-01-29 20:31:33 +09003016 bit = fls(mask) - 1;
3017 mask &= ~(1 << bit);
Tejun Heo1c3fae42006-04-02 20:53:28 +09003018
Tejun Heo008a7892007-07-16 14:29:40 +09003019 /* Mask off all speeds higher than or equal to the current
3020 * one. Force 1.5Gbps if current SPD is not available.
3021 */
3022 if (spd > 1)
3023 mask &= (1 << (spd - 1)) - 1;
3024 else
3025 mask &= 1;
3026
3027 /* were we already at the bottom? */
Tejun Heo1c3fae42006-04-02 20:53:28 +09003028 if (!mask)
3029 return -EINVAL;
3030
Tejun Heoa07d4992009-01-29 20:31:33 +09003031 if (spd_limit) {
3032 if (mask & ((1 << spd_limit) - 1))
3033 mask &= (1 << spd_limit) - 1;
3034 else {
3035 bit = ffs(mask) - 1;
3036 mask = 1 << bit;
3037 }
3038 }
3039
Tejun Heo936fd732007-08-06 18:36:23 +09003040 link->sata_spd_limit = mask;
Tejun Heo1c3fae42006-04-02 20:53:28 +09003041
Tejun Heo936fd732007-08-06 18:36:23 +09003042 ata_link_printk(link, KERN_WARNING, "limiting SATA link speed to %s\n",
Tejun Heof15a1da2006-05-15 20:57:56 +09003043 sata_spd_string(fls(mask)));
Tejun Heo1c3fae42006-04-02 20:53:28 +09003044
3045 return 0;
3046}
3047
Tejun Heo936fd732007-08-06 18:36:23 +09003048static int __sata_set_spd_needed(struct ata_link *link, u32 *scontrol)
Tejun Heo1c3fae42006-04-02 20:53:28 +09003049{
Tejun Heo52702222007-10-31 10:17:07 +09003050 struct ata_link *host_link = &link->ap->link;
3051 u32 limit, target, spd;
Tejun Heo1c3fae42006-04-02 20:53:28 +09003052
Tejun Heo52702222007-10-31 10:17:07 +09003053 limit = link->sata_spd_limit;
3054
3055 /* Don't configure downstream link faster than upstream link.
3056 * It doesn't speed up anything and some PMPs choke on such
3057 * configuration.
3058 */
3059 if (!ata_is_host_link(link) && host_link->sata_spd)
3060 limit &= (1 << host_link->sata_spd) - 1;
3061
3062 if (limit == UINT_MAX)
3063 target = 0;
Tejun Heo1c3fae42006-04-02 20:53:28 +09003064 else
Tejun Heo52702222007-10-31 10:17:07 +09003065 target = fls(limit);
Tejun Heo1c3fae42006-04-02 20:53:28 +09003066
3067 spd = (*scontrol >> 4) & 0xf;
Tejun Heo52702222007-10-31 10:17:07 +09003068 *scontrol = (*scontrol & ~0xf0) | ((target & 0xf) << 4);
Tejun Heo1c3fae42006-04-02 20:53:28 +09003069
Tejun Heo52702222007-10-31 10:17:07 +09003070 return spd != target;
Tejun Heo1c3fae42006-04-02 20:53:28 +09003071}
3072
3073/**
Tejun Heo3c567b72006-05-15 20:57:23 +09003074 * sata_set_spd_needed - is SATA spd configuration needed
Tejun Heo936fd732007-08-06 18:36:23 +09003075 * @link: Link in question
Tejun Heo1c3fae42006-04-02 20:53:28 +09003076 *
3077 * Test whether the spd limit in SControl matches
Tejun Heo936fd732007-08-06 18:36:23 +09003078 * @link->sata_spd_limit. This function is used to determine
Tejun Heo1c3fae42006-04-02 20:53:28 +09003079 * whether hardreset is necessary to apply SATA spd
3080 * configuration.
3081 *
3082 * LOCKING:
3083 * Inherited from caller.
3084 *
3085 * RETURNS:
3086 * 1 if SATA spd configuration is needed, 0 otherwise.
3087 */
Adrian Bunk1dc55e82008-04-21 11:51:17 +03003088static int sata_set_spd_needed(struct ata_link *link)
Tejun Heo1c3fae42006-04-02 20:53:28 +09003089{
3090 u32 scontrol;
3091
Tejun Heo936fd732007-08-06 18:36:23 +09003092 if (sata_scr_read(link, SCR_CONTROL, &scontrol))
Tejun Heodb64bcf2007-10-31 10:17:06 +09003093 return 1;
Tejun Heo1c3fae42006-04-02 20:53:28 +09003094
Tejun Heo936fd732007-08-06 18:36:23 +09003095 return __sata_set_spd_needed(link, &scontrol);
Tejun Heo1c3fae42006-04-02 20:53:28 +09003096}
3097
3098/**
Tejun Heo3c567b72006-05-15 20:57:23 +09003099 * sata_set_spd - set SATA spd according to spd limit
Tejun Heo936fd732007-08-06 18:36:23 +09003100 * @link: Link to set SATA spd for
Tejun Heo1c3fae42006-04-02 20:53:28 +09003101 *
Tejun Heo936fd732007-08-06 18:36:23 +09003102 * Set SATA spd of @link according to sata_spd_limit.
Tejun Heo1c3fae42006-04-02 20:53:28 +09003103 *
3104 * LOCKING:
3105 * Inherited from caller.
3106 *
3107 * RETURNS:
3108 * 0 if spd doesn't need to be changed, 1 if spd has been
Tejun Heo81952c52006-05-15 20:57:47 +09003109 * changed. Negative errno if SCR registers are inaccessible.
Tejun Heo1c3fae42006-04-02 20:53:28 +09003110 */
Tejun Heo936fd732007-08-06 18:36:23 +09003111int sata_set_spd(struct ata_link *link)
Tejun Heo1c3fae42006-04-02 20:53:28 +09003112{
3113 u32 scontrol;
Tejun Heo81952c52006-05-15 20:57:47 +09003114 int rc;
Tejun Heo1c3fae42006-04-02 20:53:28 +09003115
Tejun Heo936fd732007-08-06 18:36:23 +09003116 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
Tejun Heo81952c52006-05-15 20:57:47 +09003117 return rc;
Tejun Heo1c3fae42006-04-02 20:53:28 +09003118
Tejun Heo936fd732007-08-06 18:36:23 +09003119 if (!__sata_set_spd_needed(link, &scontrol))
Tejun Heo1c3fae42006-04-02 20:53:28 +09003120 return 0;
3121
Tejun Heo936fd732007-08-06 18:36:23 +09003122 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
Tejun Heo81952c52006-05-15 20:57:47 +09003123 return rc;
3124
Tejun Heo1c3fae42006-04-02 20:53:28 +09003125 return 1;
3126}
3127
Alan Cox452503f2005-10-21 19:01:32 -04003128/*
3129 * This mode timing computation functionality is ported over from
3130 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
3131 */
3132/*
Alan Coxb352e572006-08-10 18:52:12 +01003133 * PIO 0-4, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
Alan Cox452503f2005-10-21 19:01:32 -04003134 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
Alan Coxb352e572006-08-10 18:52:12 +01003135 * for UDMA6, which is currently supported only by Maxtor drives.
3136 *
3137 * For PIO 5/6 MWDMA 3/4 see the CFA specification 3.0.
Alan Cox452503f2005-10-21 19:01:32 -04003138 */
3139
3140static const struct ata_timing ata_timing[] = {
David Daney3ada9c12009-01-15 17:45:31 -08003141/* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 0, 960, 0 }, */
3142 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 0, 600, 0 },
3143 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 0, 383, 0 },
3144 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 0, 240, 0 },
3145 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 0, 180, 0 },
3146 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 0, 120, 0 },
3147 { XFER_PIO_5, 15, 65, 25, 100, 65, 25, 0, 100, 0 },
3148 { XFER_PIO_6, 10, 55, 20, 80, 55, 20, 0, 80, 0 },
Alan Cox452503f2005-10-21 19:01:32 -04003149
David Daney3ada9c12009-01-15 17:45:31 -08003150 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 50, 960, 0 },
3151 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 30, 480, 0 },
3152 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 20, 240, 0 },
Alan Cox452503f2005-10-21 19:01:32 -04003153
David Daney3ada9c12009-01-15 17:45:31 -08003154 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 20, 480, 0 },
3155 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 5, 150, 0 },
3156 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 5, 120, 0 },
3157 { XFER_MW_DMA_3, 25, 0, 0, 0, 65, 25, 5, 100, 0 },
3158 { XFER_MW_DMA_4, 25, 0, 0, 0, 55, 20, 5, 80, 0 },
Alan Cox452503f2005-10-21 19:01:32 -04003159
David Daney3ada9c12009-01-15 17:45:31 -08003160/* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 0, 150 }, */
3161 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 0, 120 },
3162 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 0, 80 },
3163 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 0, 60 },
3164 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 0, 45 },
3165 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 0, 30 },
3166 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 0, 20 },
3167 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 0, 15 },
Alan Cox452503f2005-10-21 19:01:32 -04003168
3169 { 0xFF }
3170};
3171
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003172#define ENOUGH(v, unit) (((v)-1)/(unit)+1)
3173#define EZ(v, unit) ((v)?ENOUGH(v, unit):0)
Alan Cox452503f2005-10-21 19:01:32 -04003174
3175static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
3176{
David Daney3ada9c12009-01-15 17:45:31 -08003177 q->setup = EZ(t->setup * 1000, T);
3178 q->act8b = EZ(t->act8b * 1000, T);
3179 q->rec8b = EZ(t->rec8b * 1000, T);
3180 q->cyc8b = EZ(t->cyc8b * 1000, T);
3181 q->active = EZ(t->active * 1000, T);
3182 q->recover = EZ(t->recover * 1000, T);
3183 q->dmack_hold = EZ(t->dmack_hold * 1000, T);
3184 q->cycle = EZ(t->cycle * 1000, T);
3185 q->udma = EZ(t->udma * 1000, UT);
Alan Cox452503f2005-10-21 19:01:32 -04003186}
3187
3188void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
3189 struct ata_timing *m, unsigned int what)
3190{
3191 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
3192 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
3193 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
3194 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
3195 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
3196 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
David Daney3ada9c12009-01-15 17:45:31 -08003197 if (what & ATA_TIMING_DMACK_HOLD) m->dmack_hold = max(a->dmack_hold, b->dmack_hold);
Alan Cox452503f2005-10-21 19:01:32 -04003198 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
3199 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
3200}
3201
Tejun Heo63573572007-11-27 19:43:39 +09003202const struct ata_timing *ata_timing_find_mode(u8 xfer_mode)
Alan Cox452503f2005-10-21 19:01:32 -04003203{
Tejun Heo70cd0712007-11-27 19:43:40 +09003204 const struct ata_timing *t = ata_timing;
Alan Cox452503f2005-10-21 19:01:32 -04003205
Tejun Heo70cd0712007-11-27 19:43:40 +09003206 while (xfer_mode > t->mode)
3207 t++;
3208
3209 if (xfer_mode == t->mode)
3210 return t;
3211 return NULL;
Alan Cox452503f2005-10-21 19:01:32 -04003212}
3213
3214int ata_timing_compute(struct ata_device *adev, unsigned short speed,
3215 struct ata_timing *t, int T, int UT)
3216{
Bartlomiej Zolnierkiewicz9e8808a2010-01-18 18:13:57 +01003217 const u16 *id = adev->id;
Alan Cox452503f2005-10-21 19:01:32 -04003218 const struct ata_timing *s;
3219 struct ata_timing p;
3220
3221 /*
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05003222 * Find the mode.
Albert Lee75b1f2f2005-11-16 17:06:18 +08003223 */
Alan Cox452503f2005-10-21 19:01:32 -04003224
3225 if (!(s = ata_timing_find_mode(speed)))
3226 return -EINVAL;
3227
Albert Lee75b1f2f2005-11-16 17:06:18 +08003228 memcpy(t, s, sizeof(*s));
3229
Alan Cox452503f2005-10-21 19:01:32 -04003230 /*
3231 * If the drive is an EIDE drive, it can tell us it needs extended
3232 * PIO/MW_DMA cycle timing.
3233 */
3234
Bartlomiej Zolnierkiewicz9e8808a2010-01-18 18:13:57 +01003235 if (id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
Alan Cox452503f2005-10-21 19:01:32 -04003236 memset(&p, 0, sizeof(p));
Bartlomiej Zolnierkiewicz9e8808a2010-01-18 18:13:57 +01003237
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003238 if (speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
Bartlomiej Zolnierkiewicz9e8808a2010-01-18 18:13:57 +01003239 if (speed <= XFER_PIO_2)
3240 p.cycle = p.cyc8b = id[ATA_ID_EIDE_PIO];
3241 else if ((speed <= XFER_PIO_4) ||
3242 (speed == XFER_PIO_5 && !ata_id_is_cfa(id)))
3243 p.cycle = p.cyc8b = id[ATA_ID_EIDE_PIO_IORDY];
3244 } else if (speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2)
3245 p.cycle = id[ATA_ID_EIDE_DMA_MIN];
3246
Alan Cox452503f2005-10-21 19:01:32 -04003247 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
3248 }
3249
3250 /*
3251 * Convert the timing to bus clock counts.
3252 */
3253
Albert Lee75b1f2f2005-11-16 17:06:18 +08003254 ata_timing_quantize(t, t, T, UT);
Alan Cox452503f2005-10-21 19:01:32 -04003255
3256 /*
Randy Dunlapc893a3a2006-01-28 13:15:32 -05003257 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
3258 * S.M.A.R.T * and some other commands. We have to ensure that the
3259 * DMA cycle timing is slower/equal than the fastest PIO timing.
Alan Cox452503f2005-10-21 19:01:32 -04003260 */
3261
Alanfd3367af2006-12-07 12:41:18 +00003262 if (speed > XFER_PIO_6) {
Alan Cox452503f2005-10-21 19:01:32 -04003263 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
3264 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
3265 }
3266
3267 /*
Randy Dunlapc893a3a2006-01-28 13:15:32 -05003268 * Lengthen active & recovery time so that cycle time is correct.
Alan Cox452503f2005-10-21 19:01:32 -04003269 */
3270
3271 if (t->act8b + t->rec8b < t->cyc8b) {
3272 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
3273 t->rec8b = t->cyc8b - t->act8b;
3274 }
3275
3276 if (t->active + t->recover < t->cycle) {
3277 t->active += (t->cycle - (t->active + t->recover)) / 2;
3278 t->recover = t->cycle - t->active;
3279 }
Jeff Garzika617c092007-05-21 20:14:23 -04003280
Alan Cox4f701d12007-04-23 11:55:36 +01003281 /* In a few cases quantisation may produce enough errors to
3282 leave t->cycle too low for the sum of active and recovery
3283 if so we must correct this */
3284 if (t->active + t->recover > t->cycle)
3285 t->cycle = t->active + t->recover;
Alan Cox452503f2005-10-21 19:01:32 -04003286
3287 return 0;
3288}
3289
Tejun Heocf176e12006-04-02 17:54:46 +09003290/**
Tejun Heoa0f79b92007-12-18 16:33:05 +09003291 * ata_timing_cycle2mode - find xfer mode for the specified cycle duration
3292 * @xfer_shift: ATA_SHIFT_* value for transfer type to examine.
3293 * @cycle: cycle duration in ns
3294 *
3295 * Return matching xfer mode for @cycle. The returned mode is of
3296 * the transfer type specified by @xfer_shift. If @cycle is too
3297 * slow for @xfer_shift, 0xff is returned. If @cycle is faster
3298 * than the fastest known mode, the fasted mode is returned.
3299 *
3300 * LOCKING:
3301 * None.
3302 *
3303 * RETURNS:
3304 * Matching xfer_mode, 0xff if no match found.
3305 */
3306u8 ata_timing_cycle2mode(unsigned int xfer_shift, int cycle)
3307{
3308 u8 base_mode = 0xff, last_mode = 0xff;
3309 const struct ata_xfer_ent *ent;
3310 const struct ata_timing *t;
3311
3312 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
3313 if (ent->shift == xfer_shift)
3314 base_mode = ent->base;
3315
3316 for (t = ata_timing_find_mode(base_mode);
3317 t && ata_xfer_mode2shift(t->mode) == xfer_shift; t++) {
3318 unsigned short this_cycle;
3319
3320 switch (xfer_shift) {
3321 case ATA_SHIFT_PIO:
3322 case ATA_SHIFT_MWDMA:
3323 this_cycle = t->cycle;
3324 break;
3325 case ATA_SHIFT_UDMA:
3326 this_cycle = t->udma;
3327 break;
3328 default:
3329 return 0xff;
3330 }
3331
3332 if (cycle > this_cycle)
3333 break;
3334
3335 last_mode = t->mode;
3336 }
3337
3338 return last_mode;
3339}
3340
3341/**
Tejun Heocf176e12006-04-02 17:54:46 +09003342 * ata_down_xfermask_limit - adjust dev xfer masks downward
Tejun Heocf176e12006-04-02 17:54:46 +09003343 * @dev: Device to adjust xfer masks
Tejun Heo458337d2007-02-02 16:22:30 +09003344 * @sel: ATA_DNXFER_* selector
Tejun Heocf176e12006-04-02 17:54:46 +09003345 *
3346 * Adjust xfer masks of @dev downward. Note that this function
3347 * does not apply the change. Invoking ata_set_mode() afterwards
3348 * will apply the limit.
3349 *
3350 * LOCKING:
3351 * Inherited from caller.
3352 *
3353 * RETURNS:
3354 * 0 on success, negative errno on failure
3355 */
Tejun Heo458337d2007-02-02 16:22:30 +09003356int ata_down_xfermask_limit(struct ata_device *dev, unsigned int sel)
Tejun Heocf176e12006-04-02 17:54:46 +09003357{
Tejun Heo458337d2007-02-02 16:22:30 +09003358 char buf[32];
Tejun Heo7dc951a2007-11-27 19:43:42 +09003359 unsigned long orig_mask, xfer_mask;
3360 unsigned long pio_mask, mwdma_mask, udma_mask;
Tejun Heo458337d2007-02-02 16:22:30 +09003361 int quiet, highbit;
Tejun Heocf176e12006-04-02 17:54:46 +09003362
Tejun Heo458337d2007-02-02 16:22:30 +09003363 quiet = !!(sel & ATA_DNXFER_QUIET);
3364 sel &= ~ATA_DNXFER_QUIET;
Tejun Heocf176e12006-04-02 17:54:46 +09003365
Tejun Heo458337d2007-02-02 16:22:30 +09003366 xfer_mask = orig_mask = ata_pack_xfermask(dev->pio_mask,
3367 dev->mwdma_mask,
3368 dev->udma_mask);
3369 ata_unpack_xfermask(xfer_mask, &pio_mask, &mwdma_mask, &udma_mask);
Tejun Heocf176e12006-04-02 17:54:46 +09003370
Tejun Heo458337d2007-02-02 16:22:30 +09003371 switch (sel) {
3372 case ATA_DNXFER_PIO:
3373 highbit = fls(pio_mask) - 1;
3374 pio_mask &= ~(1 << highbit);
3375 break;
3376
3377 case ATA_DNXFER_DMA:
3378 if (udma_mask) {
3379 highbit = fls(udma_mask) - 1;
3380 udma_mask &= ~(1 << highbit);
3381 if (!udma_mask)
3382 return -ENOENT;
3383 } else if (mwdma_mask) {
3384 highbit = fls(mwdma_mask) - 1;
3385 mwdma_mask &= ~(1 << highbit);
3386 if (!mwdma_mask)
3387 return -ENOENT;
3388 }
3389 break;
3390
3391 case ATA_DNXFER_40C:
3392 udma_mask &= ATA_UDMA_MASK_40C;
3393 break;
3394
3395 case ATA_DNXFER_FORCE_PIO0:
3396 pio_mask &= 1;
3397 case ATA_DNXFER_FORCE_PIO:
3398 mwdma_mask = 0;
3399 udma_mask = 0;
3400 break;
3401
Tejun Heo458337d2007-02-02 16:22:30 +09003402 default:
3403 BUG();
3404 }
3405
3406 xfer_mask &= ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
3407
3408 if (!(xfer_mask & ATA_MASK_PIO) || xfer_mask == orig_mask)
3409 return -ENOENT;
3410
3411 if (!quiet) {
3412 if (xfer_mask & (ATA_MASK_MWDMA | ATA_MASK_UDMA))
3413 snprintf(buf, sizeof(buf), "%s:%s",
3414 ata_mode_string(xfer_mask),
3415 ata_mode_string(xfer_mask & ATA_MASK_PIO));
3416 else
3417 snprintf(buf, sizeof(buf), "%s",
3418 ata_mode_string(xfer_mask));
3419
3420 ata_dev_printk(dev, KERN_WARNING,
3421 "limiting speed to %s\n", buf);
3422 }
Tejun Heocf176e12006-04-02 17:54:46 +09003423
3424 ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask,
3425 &dev->udma_mask);
3426
Tejun Heocf176e12006-04-02 17:54:46 +09003427 return 0;
Tejun Heocf176e12006-04-02 17:54:46 +09003428}
3429
Tejun Heo3373efd2006-05-15 20:57:53 +09003430static int ata_dev_set_mode(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003431{
Tejun Heod0cb43b2009-07-09 09:27:50 +09003432 struct ata_port *ap = dev->link->ap;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09003433 struct ata_eh_context *ehc = &dev->link->eh_context;
Tejun Heod0cb43b2009-07-09 09:27:50 +09003434 const bool nosetxfer = dev->horkage & ATA_HORKAGE_NOSETXFER;
Tejun Heo4055dee2008-02-07 10:34:08 +09003435 const char *dev_err_whine = "";
3436 int ign_dev_err = 0;
Tejun Heod0cb43b2009-07-09 09:27:50 +09003437 unsigned int err_mask = 0;
Tejun Heo83206a22006-03-24 15:25:31 +09003438 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439
Tejun Heoe8384602006-04-02 18:51:53 +09003440 dev->flags &= ~ATA_DFLAG_PIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 if (dev->xfer_shift == ATA_SHIFT_PIO)
3442 dev->flags |= ATA_DFLAG_PIO;
3443
Tejun Heod0cb43b2009-07-09 09:27:50 +09003444 if (nosetxfer && ap->flags & ATA_FLAG_SATA && ata_id_is_sata(dev->id))
3445 dev_err_whine = " (SET_XFERMODE skipped)";
3446 else {
3447 if (nosetxfer)
3448 ata_dev_printk(dev, KERN_WARNING,
3449 "NOSETXFER but PATA detected - can't "
3450 "skip SETXFER, might malfunction\n");
3451 err_mask = ata_dev_set_xfermode(dev);
3452 }
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003453
Tejun Heo4055dee2008-02-07 10:34:08 +09003454 if (err_mask & ~AC_ERR_DEV)
3455 goto fail;
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003456
Tejun Heo4055dee2008-02-07 10:34:08 +09003457 /* revalidate */
Tejun Heobaa1e782006-11-01 18:39:27 +09003458 ehc->i.flags |= ATA_EHI_POST_SETMODE;
Tejun Heo422c9da2007-09-23 13:14:12 +09003459 rc = ata_dev_revalidate(dev, ATA_DEV_UNKNOWN, 0);
Tejun Heobaa1e782006-11-01 18:39:27 +09003460 ehc->i.flags &= ~ATA_EHI_POST_SETMODE;
Tejun Heo5eb45c02006-04-02 18:51:52 +09003461 if (rc)
Tejun Heo83206a22006-03-24 15:25:31 +09003462 return rc;
Tejun Heo48a8a142006-03-05 17:55:58 +09003463
Alan Coxb93fda12008-04-08 16:36:44 +01003464 if (dev->xfer_shift == ATA_SHIFT_PIO) {
3465 /* Old CFA may refuse this command, which is just fine */
3466 if (ata_id_is_cfa(dev->id))
3467 ign_dev_err = 1;
3468 /* Catch several broken garbage emulations plus some pre
3469 ATA devices */
3470 if (ata_id_major_version(dev->id) == 0 &&
3471 dev->pio_mode <= XFER_PIO_2)
3472 ign_dev_err = 1;
3473 /* Some very old devices and some bad newer ones fail
3474 any kind of SET_XFERMODE request but support PIO0-2
3475 timings and no IORDY */
3476 if (!ata_id_has_iordy(dev->id) && dev->pio_mode <= XFER_PIO_2)
3477 ign_dev_err = 1;
3478 }
Tejun Heo4055dee2008-02-07 10:34:08 +09003479 /* Early MWDMA devices do DMA but don't allow DMA mode setting.
3480 Don't fail an MWDMA0 set IFF the device indicates it is in MWDMA0 */
3481 if (dev->xfer_shift == ATA_SHIFT_MWDMA &&
3482 dev->dma_mode == XFER_MW_DMA_0 &&
3483 (dev->id[63] >> 8) & 1)
3484 ign_dev_err = 1;
3485
3486 /* if the device is actually configured correctly, ignore dev err */
3487 if (dev->xfer_mode == ata_xfer_mask2mode(ata_id_xfermask(dev->id)))
3488 ign_dev_err = 1;
3489
3490 if (err_mask & AC_ERR_DEV) {
3491 if (!ign_dev_err)
3492 goto fail;
3493 else
3494 dev_err_whine = " (device error ignored)";
3495 }
3496
Tejun Heo23e71c32006-03-06 04:31:57 +09003497 DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n",
3498 dev->xfer_shift, (int)dev->xfer_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499
Tejun Heo4055dee2008-02-07 10:34:08 +09003500 ata_dev_printk(dev, KERN_INFO, "configured for %s%s\n",
3501 ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)),
3502 dev_err_whine);
3503
Tejun Heo83206a22006-03-24 15:25:31 +09003504 return 0;
Tejun Heo4055dee2008-02-07 10:34:08 +09003505
3506 fail:
3507 ata_dev_printk(dev, KERN_ERR, "failed to set xfermode "
3508 "(err_mask=0x%x)\n", err_mask);
3509 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003510}
3511
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512/**
Alan04351822007-03-06 02:37:52 -08003513 * ata_do_set_mode - Program timings and issue SET FEATURES - XFER
Tejun Heo02607312007-08-06 18:36:23 +09003514 * @link: link on which timings will be programmed
Joe Perches1967b7f2008-02-03 17:08:11 +02003515 * @r_failed_dev: out parameter for failed device
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 *
Alan04351822007-03-06 02:37:52 -08003517 * Standard implementation of the function used to tune and set
3518 * ATA device disk transfer mode (PIO3, UDMA6, etc.). If
3519 * ata_dev_set_mode() fails, pointer to the failing device is
Tejun Heoe82cbdb2006-04-01 01:38:18 +09003520 * returned in @r_failed_dev.
Jeff Garzik780a87f2005-05-30 15:41:05 -04003521 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003522 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003523 * PCI/etc. bus probe sem.
Tejun Heoe82cbdb2006-04-01 01:38:18 +09003524 *
3525 * RETURNS:
3526 * 0 on success, negative errno otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -07003527 */
Alan04351822007-03-06 02:37:52 -08003528
Tejun Heo02607312007-08-06 18:36:23 +09003529int ata_do_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003530{
Tejun Heo02607312007-08-06 18:36:23 +09003531 struct ata_port *ap = link->ap;
Tejun Heoe8e06192006-04-01 01:38:18 +09003532 struct ata_device *dev;
Tejun Heof58229f2007-08-06 18:36:23 +09003533 int rc = 0, used_dma = 0, found = 0;
Tejun Heo3adcebb2006-05-15 20:57:37 +09003534
Tejun Heoa6d5a512006-03-06 04:31:57 +09003535 /* step 1: calculate xfer_mask */
Tejun Heo1eca4362008-11-03 20:03:17 +09003536 ata_for_each_dev(dev, link, ENABLED) {
Tejun Heo7dc951a2007-11-27 19:43:42 +09003537 unsigned long pio_mask, dma_mask;
Alan Coxb3a70602007-10-02 12:38:26 -04003538 unsigned int mode_mask;
Tejun Heoa6d5a512006-03-06 04:31:57 +09003539
Alan Coxb3a70602007-10-02 12:38:26 -04003540 mode_mask = ATA_DMA_MASK_ATA;
3541 if (dev->class == ATA_DEV_ATAPI)
3542 mode_mask = ATA_DMA_MASK_ATAPI;
3543 else if (ata_id_is_cfa(dev->id))
3544 mode_mask = ATA_DMA_MASK_CFA;
3545
Tejun Heo3373efd2006-05-15 20:57:53 +09003546 ata_dev_xfermask(dev);
Tejun Heo33267322008-02-13 09:15:09 +09003547 ata_force_xfermask(dev);
Tejun Heoa6d5a512006-03-06 04:31:57 +09003548
Tejun Heoacf356b2006-03-24 14:07:50 +09003549 pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0);
3550 dma_mask = ata_pack_xfermask(0, dev->mwdma_mask, dev->udma_mask);
Alan Coxb3a70602007-10-02 12:38:26 -04003551
3552 if (libata_dma_mask & mode_mask)
3553 dma_mask = ata_pack_xfermask(0, dev->mwdma_mask, dev->udma_mask);
3554 else
3555 dma_mask = 0;
3556
Tejun Heoacf356b2006-03-24 14:07:50 +09003557 dev->pio_mode = ata_xfer_mask2mode(pio_mask);
3558 dev->dma_mode = ata_xfer_mask2mode(dma_mask);
Alan Cox5444a6f2006-03-27 18:58:20 +01003559
Tejun Heo4f659772006-04-01 01:38:18 +09003560 found = 1;
Alan Coxb15b3eb2008-08-01 09:18:34 +01003561 if (ata_dma_enabled(dev))
Alan Cox5444a6f2006-03-27 18:58:20 +01003562 used_dma = 1;
Tejun Heoa6d5a512006-03-06 04:31:57 +09003563 }
Tejun Heo4f659772006-04-01 01:38:18 +09003564 if (!found)
Tejun Heoe82cbdb2006-04-01 01:38:18 +09003565 goto out;
Tejun Heoa6d5a512006-03-06 04:31:57 +09003566
3567 /* step 2: always set host PIO timings */
Tejun Heo1eca4362008-11-03 20:03:17 +09003568 ata_for_each_dev(dev, link, ENABLED) {
Tejun Heo70cd0712007-11-27 19:43:40 +09003569 if (dev->pio_mode == 0xff) {
Tejun Heof15a1da2006-05-15 20:57:56 +09003570 ata_dev_printk(dev, KERN_WARNING, "no PIO support\n");
Tejun Heoe8e06192006-04-01 01:38:18 +09003571 rc = -EINVAL;
Tejun Heoe82cbdb2006-04-01 01:38:18 +09003572 goto out;
Tejun Heoe8e06192006-04-01 01:38:18 +09003573 }
3574
3575 dev->xfer_mode = dev->pio_mode;
3576 dev->xfer_shift = ATA_SHIFT_PIO;
3577 if (ap->ops->set_piomode)
3578 ap->ops->set_piomode(ap, dev);
3579 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580
Tejun Heoa6d5a512006-03-06 04:31:57 +09003581 /* step 3: set host DMA timings */
Tejun Heo1eca4362008-11-03 20:03:17 +09003582 ata_for_each_dev(dev, link, ENABLED) {
3583 if (!ata_dma_enabled(dev))
Tejun Heoe8e06192006-04-01 01:38:18 +09003584 continue;
3585
3586 dev->xfer_mode = dev->dma_mode;
3587 dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
3588 if (ap->ops->set_dmamode)
3589 ap->ops->set_dmamode(ap, dev);
3590 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591
3592 /* step 4: update devices' xfer mode */
Tejun Heo1eca4362008-11-03 20:03:17 +09003593 ata_for_each_dev(dev, link, ENABLED) {
Tejun Heo3373efd2006-05-15 20:57:53 +09003594 rc = ata_dev_set_mode(dev);
Tejun Heo5bbc53f2006-04-01 01:38:17 +09003595 if (rc)
Tejun Heoe82cbdb2006-04-01 01:38:18 +09003596 goto out;
Tejun Heo83206a22006-03-24 15:25:31 +09003597 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598
Tejun Heoe8e06192006-04-01 01:38:18 +09003599 /* Record simplex status. If we selected DMA then the other
3600 * host channels are not permitted to do so.
Alan Cox5444a6f2006-03-27 18:58:20 +01003601 */
Jeff Garzikcca39742006-08-24 03:19:22 -04003602 if (used_dma && (ap->host->flags & ATA_HOST_SIMPLEX))
Alan032af1c2007-03-01 17:36:46 +00003603 ap->host->simplex_claimed = ap;
Alan Cox5444a6f2006-03-27 18:58:20 +01003604
Tejun Heoe82cbdb2006-04-01 01:38:18 +09003605 out:
3606 if (rc)
3607 *r_failed_dev = dev;
3608 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003609}
3610
3611/**
Tejun Heoaa2731a2008-04-07 22:47:19 +09003612 * ata_wait_ready - wait for link to become ready
3613 * @link: link to be waited on
3614 * @deadline: deadline jiffies for the operation
3615 * @check_ready: callback to check link readiness
3616 *
3617 * Wait for @link to become ready. @check_ready should return
3618 * positive number if @link is ready, 0 if it isn't, -ENODEV if
3619 * link doesn't seem to be occupied, other errno for other error
3620 * conditions.
3621 *
3622 * Transient -ENODEV conditions are allowed for
3623 * ATA_TMOUT_FF_WAIT.
3624 *
3625 * LOCKING:
3626 * EH context.
3627 *
3628 * RETURNS:
3629 * 0 if @linke is ready before @deadline; otherwise, -errno.
3630 */
3631int ata_wait_ready(struct ata_link *link, unsigned long deadline,
3632 int (*check_ready)(struct ata_link *link))
3633{
3634 unsigned long start = jiffies;
Tejun Heob48d58f2010-04-09 19:46:38 +09003635 unsigned long nodev_deadline;
Tejun Heoaa2731a2008-04-07 22:47:19 +09003636 int warned = 0;
3637
Tejun Heob48d58f2010-04-09 19:46:38 +09003638 /* choose which 0xff timeout to use, read comment in libata.h */
3639 if (link->ap->host->flags & ATA_HOST_PARALLEL_SCAN)
3640 nodev_deadline = ata_deadline(start, ATA_TMOUT_FF_WAIT_LONG);
3641 else
3642 nodev_deadline = ata_deadline(start, ATA_TMOUT_FF_WAIT);
3643
Tejun Heob1c72912008-07-31 17:02:43 +09003644 /* Slave readiness can't be tested separately from master. On
3645 * M/S emulation configuration, this function should be called
3646 * only on the master and it will handle both master and slave.
3647 */
3648 WARN_ON(link == link->ap->slave_link);
3649
Tejun Heoaa2731a2008-04-07 22:47:19 +09003650 if (time_after(nodev_deadline, deadline))
3651 nodev_deadline = deadline;
3652
3653 while (1) {
3654 unsigned long now = jiffies;
3655 int ready, tmp;
3656
3657 ready = tmp = check_ready(link);
3658 if (ready > 0)
3659 return 0;
3660
Tejun Heob48d58f2010-04-09 19:46:38 +09003661 /*
3662 * -ENODEV could be transient. Ignore -ENODEV if link
Tejun Heoaa2731a2008-04-07 22:47:19 +09003663 * is online. Also, some SATA devices take a long
Tejun Heob48d58f2010-04-09 19:46:38 +09003664 * time to clear 0xff after reset. Wait for
3665 * ATA_TMOUT_FF_WAIT[_LONG] on -ENODEV if link isn't
3666 * offline.
Tejun Heoaa2731a2008-04-07 22:47:19 +09003667 *
3668 * Note that some PATA controllers (pata_ali) explode
3669 * if status register is read more than once when
3670 * there's no device attached.
3671 */
3672 if (ready == -ENODEV) {
3673 if (ata_link_online(link))
3674 ready = 0;
3675 else if ((link->ap->flags & ATA_FLAG_SATA) &&
3676 !ata_link_offline(link) &&
3677 time_before(now, nodev_deadline))
3678 ready = 0;
3679 }
3680
3681 if (ready)
3682 return ready;
3683 if (time_after(now, deadline))
3684 return -EBUSY;
3685
3686 if (!warned && time_after(now, start + 5 * HZ) &&
3687 (deadline - now > 3 * HZ)) {
3688 ata_link_printk(link, KERN_WARNING,
3689 "link is slow to respond, please be patient "
3690 "(ready=%d)\n", tmp);
3691 warned = 1;
3692 }
3693
3694 msleep(50);
3695 }
3696}
3697
3698/**
3699 * ata_wait_after_reset - wait for link to become ready after reset
3700 * @link: link to be waited on
3701 * @deadline: deadline jiffies for the operation
3702 * @check_ready: callback to check link readiness
3703 *
3704 * Wait for @link to become ready after reset.
3705 *
3706 * LOCKING:
3707 * EH context.
3708 *
3709 * RETURNS:
3710 * 0 if @linke is ready before @deadline; otherwise, -errno.
3711 */
Harvey Harrison2b4221b2008-04-24 18:37:34 -07003712int ata_wait_after_reset(struct ata_link *link, unsigned long deadline,
Tejun Heoaa2731a2008-04-07 22:47:19 +09003713 int (*check_ready)(struct ata_link *link))
3714{
Tejun Heo341c2c92008-05-20 02:17:51 +09003715 msleep(ATA_WAIT_AFTER_RESET);
Tejun Heoaa2731a2008-04-07 22:47:19 +09003716
3717 return ata_wait_ready(link, deadline, check_ready);
3718}
3719
3720/**
Tejun Heo936fd732007-08-06 18:36:23 +09003721 * sata_link_debounce - debounce SATA phy status
3722 * @link: ATA link to debounce SATA phy status for
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003723 * @params: timing parameters { interval, duratinon, timeout } in msec
Tejun Heod4b2bab2007-02-02 16:50:52 +09003724 * @deadline: deadline jiffies for the operation
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003725 *
Tejun Heo936fd732007-08-06 18:36:23 +09003726* Make sure SStatus of @link reaches stable state, determined by
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003727 * holding the same value where DET is not 1 for @duration polled
3728 * every @interval, before @timeout. Timeout constraints the
Tejun Heod4b2bab2007-02-02 16:50:52 +09003729 * beginning of the stable state. Because DET gets stuck at 1 on
3730 * some controllers after hot unplugging, this functions waits
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003731 * until timeout then returns 0 if DET is stable at 1.
3732 *
Tejun Heod4b2bab2007-02-02 16:50:52 +09003733 * @timeout is further limited by @deadline. The sooner of the
3734 * two is used.
3735 *
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003736 * LOCKING:
3737 * Kernel thread context (may sleep)
3738 *
3739 * RETURNS:
3740 * 0 on success, -errno on failure.
3741 */
Tejun Heo936fd732007-08-06 18:36:23 +09003742int sata_link_debounce(struct ata_link *link, const unsigned long *params,
3743 unsigned long deadline)
Tejun Heo7a7921e2006-02-02 18:20:00 +09003744{
Tejun Heo341c2c92008-05-20 02:17:51 +09003745 unsigned long interval = params[0];
3746 unsigned long duration = params[1];
Tejun Heod4b2bab2007-02-02 16:50:52 +09003747 unsigned long last_jiffies, t;
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003748 u32 last, cur;
3749 int rc;
3750
Tejun Heo341c2c92008-05-20 02:17:51 +09003751 t = ata_deadline(jiffies, params[2]);
Tejun Heod4b2bab2007-02-02 16:50:52 +09003752 if (time_before(t, deadline))
3753 deadline = t;
3754
Tejun Heo936fd732007-08-06 18:36:23 +09003755 if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003756 return rc;
3757 cur &= 0xf;
3758
3759 last = cur;
3760 last_jiffies = jiffies;
3761
3762 while (1) {
Tejun Heo341c2c92008-05-20 02:17:51 +09003763 msleep(interval);
Tejun Heo936fd732007-08-06 18:36:23 +09003764 if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003765 return rc;
3766 cur &= 0xf;
3767
3768 /* DET stable? */
3769 if (cur == last) {
Tejun Heod4b2bab2007-02-02 16:50:52 +09003770 if (cur == 1 && time_before(jiffies, deadline))
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003771 continue;
Tejun Heo341c2c92008-05-20 02:17:51 +09003772 if (time_after(jiffies,
3773 ata_deadline(last_jiffies, duration)))
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003774 return 0;
3775 continue;
3776 }
3777
3778 /* unstable, start over */
3779 last = cur;
3780 last_jiffies = jiffies;
3781
Tejun Heof1545152007-07-16 14:29:40 +09003782 /* Check deadline. If debouncing failed, return
3783 * -EPIPE to tell upper layer to lower link speed.
3784 */
Tejun Heod4b2bab2007-02-02 16:50:52 +09003785 if (time_after(jiffies, deadline))
Tejun Heof1545152007-07-16 14:29:40 +09003786 return -EPIPE;
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003787 }
3788}
3789
3790/**
Tejun Heo936fd732007-08-06 18:36:23 +09003791 * sata_link_resume - resume SATA link
3792 * @link: ATA link to resume SATA
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003793 * @params: timing parameters { interval, duratinon, timeout } in msec
Tejun Heod4b2bab2007-02-02 16:50:52 +09003794 * @deadline: deadline jiffies for the operation
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003795 *
Tejun Heo936fd732007-08-06 18:36:23 +09003796 * Resume SATA phy @link and debounce it.
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003797 *
3798 * LOCKING:
3799 * Kernel thread context (may sleep)
3800 *
3801 * RETURNS:
3802 * 0 on success, -errno on failure.
3803 */
Tejun Heo936fd732007-08-06 18:36:23 +09003804int sata_link_resume(struct ata_link *link, const unsigned long *params,
3805 unsigned long deadline)
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003806{
Tejun Heo5040ab62010-01-11 11:14:44 +09003807 int tries = ATA_LINK_RESUME_TRIES;
Tejun Heoac371982008-04-07 22:47:19 +09003808 u32 scontrol, serror;
Tejun Heo81952c52006-05-15 20:57:47 +09003809 int rc;
Tejun Heo7a7921e2006-02-02 18:20:00 +09003810
Tejun Heo936fd732007-08-06 18:36:23 +09003811 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
Tejun Heo81952c52006-05-15 20:57:47 +09003812 return rc;
3813
Tejun Heo5040ab62010-01-11 11:14:44 +09003814 /*
3815 * Writes to SControl sometimes get ignored under certain
3816 * controllers (ata_piix SIDPR). Make sure DET actually is
3817 * cleared.
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003818 */
Tejun Heo5040ab62010-01-11 11:14:44 +09003819 do {
3820 scontrol = (scontrol & 0x0f0) | 0x300;
3821 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
3822 return rc;
3823 /*
3824 * Some PHYs react badly if SStatus is pounded
3825 * immediately after resuming. Delay 200ms before
3826 * debouncing.
3827 */
3828 msleep(200);
3829
3830 /* is SControl restored correctly? */
3831 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
3832 return rc;
3833 } while ((scontrol & 0xf0f) != 0x300 && --tries);
3834
3835 if ((scontrol & 0xf0f) != 0x300) {
3836 ata_link_printk(link, KERN_ERR,
3837 "failed to resume link (SControl %X)\n",
3838 scontrol);
3839 return 0;
3840 }
3841
3842 if (tries < ATA_LINK_RESUME_TRIES)
3843 ata_link_printk(link, KERN_WARNING,
3844 "link resume succeeded after %d retries\n",
3845 ATA_LINK_RESUME_TRIES - tries);
Tejun Heo7a7921e2006-02-02 18:20:00 +09003846
Tejun Heoac371982008-04-07 22:47:19 +09003847 if ((rc = sata_link_debounce(link, params, deadline)))
3848 return rc;
3849
Tejun Heof0465192008-05-19 01:15:08 +09003850 /* clear SError, some PHYs require this even for SRST to work */
Tejun Heoac371982008-04-07 22:47:19 +09003851 if (!(rc = sata_scr_read(link, SCR_ERROR, &serror)))
3852 rc = sata_scr_write(link, SCR_ERROR, serror);
Tejun Heoac371982008-04-07 22:47:19 +09003853
Tejun Heof0465192008-05-19 01:15:08 +09003854 return rc != -EINVAL ? rc : 0;
Tejun Heo7a7921e2006-02-02 18:20:00 +09003855}
3856
Tejun Heof5914a42006-05-31 18:27:48 +09003857/**
Tejun Heo0aa11132008-04-07 22:47:18 +09003858 * ata_std_prereset - prepare for reset
Tejun Heocc0680a2007-08-06 18:36:23 +09003859 * @link: ATA link to be reset
Tejun Heod4b2bab2007-02-02 16:50:52 +09003860 * @deadline: deadline jiffies for the operation
Tejun Heof5914a42006-05-31 18:27:48 +09003861 *
Tejun Heocc0680a2007-08-06 18:36:23 +09003862 * @link is about to be reset. Initialize it. Failure from
Tejun Heob8cffc62007-02-02 16:50:52 +09003863 * prereset makes libata abort whole reset sequence and give up
3864 * that port, so prereset should be best-effort. It does its
3865 * best to prepare for reset sequence but if things go wrong, it
3866 * should just whine, not fail.
Tejun Heof5914a42006-05-31 18:27:48 +09003867 *
3868 * LOCKING:
3869 * Kernel thread context (may sleep)
3870 *
3871 * RETURNS:
3872 * 0 on success, -errno otherwise.
3873 */
Tejun Heo0aa11132008-04-07 22:47:18 +09003874int ata_std_prereset(struct ata_link *link, unsigned long deadline)
Tejun Heof5914a42006-05-31 18:27:48 +09003875{
Tejun Heocc0680a2007-08-06 18:36:23 +09003876 struct ata_port *ap = link->ap;
Tejun Heo936fd732007-08-06 18:36:23 +09003877 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heoe9c83912006-07-03 16:07:26 +09003878 const unsigned long *timing = sata_ehc_deb_timing(ehc);
Tejun Heof5914a42006-05-31 18:27:48 +09003879 int rc;
3880
Tejun Heof5914a42006-05-31 18:27:48 +09003881 /* if we're about to do hardreset, nothing more to do */
3882 if (ehc->i.action & ATA_EH_HARDRESET)
3883 return 0;
3884
Tejun Heo936fd732007-08-06 18:36:23 +09003885 /* if SATA, resume link */
Tejun Heoa16abc02007-05-21 18:33:47 +02003886 if (ap->flags & ATA_FLAG_SATA) {
Tejun Heo936fd732007-08-06 18:36:23 +09003887 rc = sata_link_resume(link, timing, deadline);
Tejun Heob8cffc62007-02-02 16:50:52 +09003888 /* whine about phy resume failure but proceed */
3889 if (rc && rc != -EOPNOTSUPP)
Tejun Heocc0680a2007-08-06 18:36:23 +09003890 ata_link_printk(link, KERN_WARNING, "failed to resume "
Tejun Heof5914a42006-05-31 18:27:48 +09003891 "link for reset (errno=%d)\n", rc);
Tejun Heof5914a42006-05-31 18:27:48 +09003892 }
3893
Tejun Heo45db2f62008-04-08 01:46:56 +09003894 /* no point in trying softreset on offline link */
Tejun Heob1c72912008-07-31 17:02:43 +09003895 if (ata_phys_link_offline(link))
Tejun Heo45db2f62008-04-08 01:46:56 +09003896 ehc->i.action &= ~ATA_EH_SOFTRESET;
3897
Tejun Heof5914a42006-05-31 18:27:48 +09003898 return 0;
3899}
3900
Tejun Heoc2bd5802006-01-24 17:05:22 +09003901/**
Tejun Heocc0680a2007-08-06 18:36:23 +09003902 * sata_link_hardreset - reset link via SATA phy reset
3903 * @link: link to reset
Tejun Heob6103f62006-11-01 17:59:53 +09003904 * @timing: timing parameters { interval, duratinon, timeout } in msec
Tejun Heod4b2bab2007-02-02 16:50:52 +09003905 * @deadline: deadline jiffies for the operation
Tejun Heo9dadd452008-04-07 22:47:19 +09003906 * @online: optional out parameter indicating link onlineness
3907 * @check_ready: optional callback to check link readiness
Tejun Heoc2bd5802006-01-24 17:05:22 +09003908 *
Tejun Heocc0680a2007-08-06 18:36:23 +09003909 * SATA phy-reset @link using DET bits of SControl register.
Tejun Heo9dadd452008-04-07 22:47:19 +09003910 * After hardreset, link readiness is waited upon using
3911 * ata_wait_ready() if @check_ready is specified. LLDs are
3912 * allowed to not specify @check_ready and wait itself after this
3913 * function returns. Device classification is LLD's
3914 * responsibility.
3915 *
3916 * *@online is set to one iff reset succeeded and @link is online
3917 * after reset.
Tejun Heoc2bd5802006-01-24 17:05:22 +09003918 *
3919 * LOCKING:
3920 * Kernel thread context (may sleep)
3921 *
3922 * RETURNS:
3923 * 0 on success, -errno otherwise.
3924 */
Tejun Heocc0680a2007-08-06 18:36:23 +09003925int sata_link_hardreset(struct ata_link *link, const unsigned long *timing,
Tejun Heo9dadd452008-04-07 22:47:19 +09003926 unsigned long deadline,
3927 bool *online, int (*check_ready)(struct ata_link *))
Tejun Heoc2bd5802006-01-24 17:05:22 +09003928{
Tejun Heo852ee162006-04-01 01:38:18 +09003929 u32 scontrol;
Tejun Heo81952c52006-05-15 20:57:47 +09003930 int rc;
Tejun Heo852ee162006-04-01 01:38:18 +09003931
Tejun Heoc2bd5802006-01-24 17:05:22 +09003932 DPRINTK("ENTER\n");
3933
Tejun Heo9dadd452008-04-07 22:47:19 +09003934 if (online)
3935 *online = false;
3936
Tejun Heo936fd732007-08-06 18:36:23 +09003937 if (sata_set_spd_needed(link)) {
Tejun Heo1c3fae42006-04-02 20:53:28 +09003938 /* SATA spec says nothing about how to reconfigure
3939 * spd. To be on the safe side, turn off phy during
3940 * reconfiguration. This works for at least ICH7 AHCI
3941 * and Sil3124.
3942 */
Tejun Heo936fd732007-08-06 18:36:23 +09003943 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
Tejun Heob6103f62006-11-01 17:59:53 +09003944 goto out;
Tejun Heo81952c52006-05-15 20:57:47 +09003945
Martin Hicksa34b6fc2006-07-05 15:06:13 -04003946 scontrol = (scontrol & 0x0f0) | 0x304;
Tejun Heo81952c52006-05-15 20:57:47 +09003947
Tejun Heo936fd732007-08-06 18:36:23 +09003948 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
Tejun Heob6103f62006-11-01 17:59:53 +09003949 goto out;
Tejun Heo1c3fae42006-04-02 20:53:28 +09003950
Tejun Heo936fd732007-08-06 18:36:23 +09003951 sata_set_spd(link);
Tejun Heo1c3fae42006-04-02 20:53:28 +09003952 }
3953
3954 /* issue phy wake/reset */
Tejun Heo936fd732007-08-06 18:36:23 +09003955 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
Tejun Heob6103f62006-11-01 17:59:53 +09003956 goto out;
Tejun Heo81952c52006-05-15 20:57:47 +09003957
Tejun Heo852ee162006-04-01 01:38:18 +09003958 scontrol = (scontrol & 0x0f0) | 0x301;
Tejun Heo81952c52006-05-15 20:57:47 +09003959
Tejun Heo936fd732007-08-06 18:36:23 +09003960 if ((rc = sata_scr_write_flush(link, SCR_CONTROL, scontrol)))
Tejun Heob6103f62006-11-01 17:59:53 +09003961 goto out;
Tejun Heoc2bd5802006-01-24 17:05:22 +09003962
Tejun Heo1c3fae42006-04-02 20:53:28 +09003963 /* Couldn't find anything in SATA I/II specs, but AHCI-1.1
Tejun Heoc2bd5802006-01-24 17:05:22 +09003964 * 10.4.2 says at least 1 ms.
3965 */
3966 msleep(1);
3967
Tejun Heo936fd732007-08-06 18:36:23 +09003968 /* bring link back */
3969 rc = sata_link_resume(link, timing, deadline);
Tejun Heo9dadd452008-04-07 22:47:19 +09003970 if (rc)
3971 goto out;
3972 /* if link is offline nothing more to do */
Tejun Heob1c72912008-07-31 17:02:43 +09003973 if (ata_phys_link_offline(link))
Tejun Heo9dadd452008-04-07 22:47:19 +09003974 goto out;
3975
3976 /* Link is online. From this point, -ENODEV too is an error. */
3977 if (online)
3978 *online = true;
3979
Tejun Heo071f44b2008-04-07 22:47:22 +09003980 if (sata_pmp_supported(link->ap) && ata_is_host_link(link)) {
Tejun Heo9dadd452008-04-07 22:47:19 +09003981 /* If PMP is supported, we have to do follow-up SRST.
3982 * Some PMPs don't send D2H Reg FIS after hardreset if
3983 * the first port is empty. Wait only for
3984 * ATA_TMOUT_PMP_SRST_WAIT.
3985 */
3986 if (check_ready) {
3987 unsigned long pmp_deadline;
3988
Tejun Heo341c2c92008-05-20 02:17:51 +09003989 pmp_deadline = ata_deadline(jiffies,
3990 ATA_TMOUT_PMP_SRST_WAIT);
Tejun Heo9dadd452008-04-07 22:47:19 +09003991 if (time_after(pmp_deadline, deadline))
3992 pmp_deadline = deadline;
3993 ata_wait_ready(link, pmp_deadline, check_ready);
3994 }
3995 rc = -EAGAIN;
3996 goto out;
3997 }
3998
3999 rc = 0;
4000 if (check_ready)
4001 rc = ata_wait_ready(link, deadline, check_ready);
Tejun Heob6103f62006-11-01 17:59:53 +09004002 out:
Tejun Heo0cbf0712008-05-19 01:15:05 +09004003 if (rc && rc != -EAGAIN) {
4004 /* online is set iff link is online && reset succeeded */
4005 if (online)
4006 *online = false;
Tejun Heo9dadd452008-04-07 22:47:19 +09004007 ata_link_printk(link, KERN_ERR,
4008 "COMRESET failed (errno=%d)\n", rc);
Tejun Heo0cbf0712008-05-19 01:15:05 +09004009 }
Tejun Heob6103f62006-11-01 17:59:53 +09004010 DPRINTK("EXIT, rc=%d\n", rc);
4011 return rc;
4012}
4013
4014/**
Tejun Heo57c9efd2008-04-07 22:47:19 +09004015 * sata_std_hardreset - COMRESET w/o waiting or classification
4016 * @link: link to reset
4017 * @class: resulting class of attached device
4018 * @deadline: deadline jiffies for the operation
4019 *
4020 * Standard SATA COMRESET w/o waiting or classification.
4021 *
4022 * LOCKING:
4023 * Kernel thread context (may sleep)
4024 *
4025 * RETURNS:
4026 * 0 if link offline, -EAGAIN if link online, -errno on errors.
4027 */
4028int sata_std_hardreset(struct ata_link *link, unsigned int *class,
4029 unsigned long deadline)
4030{
4031 const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
4032 bool online;
4033 int rc;
4034
4035 /* do hardreset */
4036 rc = sata_link_hardreset(link, timing, deadline, &online, NULL);
Tejun Heo57c9efd2008-04-07 22:47:19 +09004037 return online ? -EAGAIN : rc;
4038}
4039
4040/**
Tejun Heo203c75b2008-04-07 22:47:18 +09004041 * ata_std_postreset - standard postreset callback
Tejun Heocc0680a2007-08-06 18:36:23 +09004042 * @link: the target ata_link
Tejun Heoc2bd5802006-01-24 17:05:22 +09004043 * @classes: classes of attached devices
4044 *
4045 * This function is invoked after a successful reset. Note that
4046 * the device might have been reset more than once using
4047 * different reset methods before postreset is invoked.
Tejun Heoc2bd5802006-01-24 17:05:22 +09004048 *
Tejun Heoc2bd5802006-01-24 17:05:22 +09004049 * LOCKING:
4050 * Kernel thread context (may sleep)
4051 */
Tejun Heo203c75b2008-04-07 22:47:18 +09004052void ata_std_postreset(struct ata_link *link, unsigned int *classes)
Tejun Heoc2bd5802006-01-24 17:05:22 +09004053{
Tejun Heof0465192008-05-19 01:15:08 +09004054 u32 serror;
4055
Tejun Heoc2bd5802006-01-24 17:05:22 +09004056 DPRINTK("ENTER\n");
4057
Tejun Heof0465192008-05-19 01:15:08 +09004058 /* reset complete, clear SError */
4059 if (!sata_scr_read(link, SCR_ERROR, &serror))
4060 sata_scr_write(link, SCR_ERROR, serror);
4061
Tejun Heoc2bd5802006-01-24 17:05:22 +09004062 /* print link status */
Tejun Heo936fd732007-08-06 18:36:23 +09004063 sata_print_link_status(link);
Tejun Heoc2bd5802006-01-24 17:05:22 +09004064
Tejun Heoc2bd5802006-01-24 17:05:22 +09004065 DPRINTK("EXIT\n");
4066}
4067
Tejun Heoa62c0fc2006-01-24 17:05:22 +09004068/**
Tejun Heo623a3122006-03-05 17:55:58 +09004069 * ata_dev_same_device - Determine whether new ID matches configured device
Tejun Heo623a3122006-03-05 17:55:58 +09004070 * @dev: device to compare against
4071 * @new_class: class of the new device
4072 * @new_id: IDENTIFY page of the new device
4073 *
4074 * Compare @new_class and @new_id against @dev and determine
4075 * whether @dev is the device indicated by @new_class and
4076 * @new_id.
4077 *
4078 * LOCKING:
4079 * None.
4080 *
4081 * RETURNS:
4082 * 1 if @dev matches @new_class and @new_id, 0 otherwise.
4083 */
Tejun Heo3373efd2006-05-15 20:57:53 +09004084static int ata_dev_same_device(struct ata_device *dev, unsigned int new_class,
4085 const u16 *new_id)
Tejun Heo623a3122006-03-05 17:55:58 +09004086{
4087 const u16 *old_id = dev->id;
Tejun Heoa0cf7332007-01-02 20:18:49 +09004088 unsigned char model[2][ATA_ID_PROD_LEN + 1];
4089 unsigned char serial[2][ATA_ID_SERNO_LEN + 1];
Tejun Heo623a3122006-03-05 17:55:58 +09004090
4091 if (dev->class != new_class) {
Tejun Heof15a1da2006-05-15 20:57:56 +09004092 ata_dev_printk(dev, KERN_INFO, "class mismatch %d != %d\n",
4093 dev->class, new_class);
Tejun Heo623a3122006-03-05 17:55:58 +09004094 return 0;
4095 }
4096
Tejun Heoa0cf7332007-01-02 20:18:49 +09004097 ata_id_c_string(old_id, model[0], ATA_ID_PROD, sizeof(model[0]));
4098 ata_id_c_string(new_id, model[1], ATA_ID_PROD, sizeof(model[1]));
4099 ata_id_c_string(old_id, serial[0], ATA_ID_SERNO, sizeof(serial[0]));
4100 ata_id_c_string(new_id, serial[1], ATA_ID_SERNO, sizeof(serial[1]));
Tejun Heo623a3122006-03-05 17:55:58 +09004101
4102 if (strcmp(model[0], model[1])) {
Tejun Heof15a1da2006-05-15 20:57:56 +09004103 ata_dev_printk(dev, KERN_INFO, "model number mismatch "
4104 "'%s' != '%s'\n", model[0], model[1]);
Tejun Heo623a3122006-03-05 17:55:58 +09004105 return 0;
4106 }
4107
4108 if (strcmp(serial[0], serial[1])) {
Tejun Heof15a1da2006-05-15 20:57:56 +09004109 ata_dev_printk(dev, KERN_INFO, "serial number mismatch "
4110 "'%s' != '%s'\n", serial[0], serial[1]);
Tejun Heo623a3122006-03-05 17:55:58 +09004111 return 0;
4112 }
4113
Tejun Heo623a3122006-03-05 17:55:58 +09004114 return 1;
4115}
4116
4117/**
Tejun Heofe309112007-05-15 03:28:15 +09004118 * ata_dev_reread_id - Re-read IDENTIFY data
Henrik Kretzschmar3fae4502007-06-19 10:10:50 +02004119 * @dev: target ATA device
Tejun Heobff04642006-11-10 18:08:10 +09004120 * @readid_flags: read ID flags
Tejun Heo623a3122006-03-05 17:55:58 +09004121 *
4122 * Re-read IDENTIFY page and make sure @dev is still attached to
4123 * the port.
4124 *
4125 * LOCKING:
4126 * Kernel thread context (may sleep)
4127 *
4128 * RETURNS:
4129 * 0 on success, negative errno otherwise
4130 */
Tejun Heofe309112007-05-15 03:28:15 +09004131int ata_dev_reread_id(struct ata_device *dev, unsigned int readid_flags)
Tejun Heo623a3122006-03-05 17:55:58 +09004132{
Tejun Heo5eb45c02006-04-02 18:51:52 +09004133 unsigned int class = dev->class;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09004134 u16 *id = (void *)dev->link->ap->sector_buf;
Tejun Heo623a3122006-03-05 17:55:58 +09004135 int rc;
4136
Tejun Heofe635c72006-05-15 20:57:35 +09004137 /* read ID data */
Tejun Heobff04642006-11-10 18:08:10 +09004138 rc = ata_dev_read_id(dev, &class, readid_flags, id);
Tejun Heo623a3122006-03-05 17:55:58 +09004139 if (rc)
Tejun Heofe309112007-05-15 03:28:15 +09004140 return rc;
Tejun Heo623a3122006-03-05 17:55:58 +09004141
4142 /* is the device still there? */
Tejun Heofe309112007-05-15 03:28:15 +09004143 if (!ata_dev_same_device(dev, class, id))
4144 return -ENODEV;
Tejun Heo623a3122006-03-05 17:55:58 +09004145
Tejun Heofe635c72006-05-15 20:57:35 +09004146 memcpy(dev->id, id, sizeof(id[0]) * ATA_ID_WORDS);
Tejun Heofe309112007-05-15 03:28:15 +09004147 return 0;
4148}
4149
4150/**
4151 * ata_dev_revalidate - Revalidate ATA device
4152 * @dev: device to revalidate
Tejun Heo422c9da2007-09-23 13:14:12 +09004153 * @new_class: new class code
Tejun Heofe309112007-05-15 03:28:15 +09004154 * @readid_flags: read ID flags
4155 *
4156 * Re-read IDENTIFY page, make sure @dev is still attached to the
4157 * port and reconfigure it according to the new IDENTIFY page.
4158 *
4159 * LOCKING:
4160 * Kernel thread context (may sleep)
4161 *
4162 * RETURNS:
4163 * 0 on success, negative errno otherwise
4164 */
Tejun Heo422c9da2007-09-23 13:14:12 +09004165int ata_dev_revalidate(struct ata_device *dev, unsigned int new_class,
4166 unsigned int readid_flags)
Tejun Heofe309112007-05-15 03:28:15 +09004167{
Tejun Heo6ddcd3b2007-05-15 03:28:15 +09004168 u64 n_sectors = dev->n_sectors;
Tejun Heo5920dad2009-07-15 17:11:41 +09004169 u64 n_native_sectors = dev->n_native_sectors;
Tejun Heofe309112007-05-15 03:28:15 +09004170 int rc;
4171
4172 if (!ata_dev_enabled(dev))
4173 return -ENODEV;
4174
Tejun Heo422c9da2007-09-23 13:14:12 +09004175 /* fail early if !ATA && !ATAPI to avoid issuing [P]IDENTIFY to PMP */
4176 if (ata_class_enabled(new_class) &&
Borislav Petkovf0d06132009-04-26 16:33:34 +02004177 new_class != ATA_DEV_ATA &&
4178 new_class != ATA_DEV_ATAPI &&
4179 new_class != ATA_DEV_SEMB) {
Tejun Heo422c9da2007-09-23 13:14:12 +09004180 ata_dev_printk(dev, KERN_INFO, "class mismatch %u != %u\n",
4181 dev->class, new_class);
4182 rc = -ENODEV;
4183 goto fail;
4184 }
4185
Tejun Heofe309112007-05-15 03:28:15 +09004186 /* re-read ID */
4187 rc = ata_dev_reread_id(dev, readid_flags);
4188 if (rc)
4189 goto fail;
Tejun Heo623a3122006-03-05 17:55:58 +09004190
4191 /* configure device according to the new ID */
Tejun Heoefdaedc2006-11-01 18:38:52 +09004192 rc = ata_dev_configure(dev);
Tejun Heo6ddcd3b2007-05-15 03:28:15 +09004193 if (rc)
4194 goto fail;
4195
4196 /* verify n_sectors hasn't changed */
Tejun Heo445d2112010-04-05 10:33:13 +09004197 if (dev->class != ATA_DEV_ATA || !n_sectors ||
4198 dev->n_sectors == n_sectors)
4199 return 0;
4200
4201 /* n_sectors has changed */
4202 ata_dev_printk(dev, KERN_WARNING, "n_sectors mismatch %llu != %llu\n",
4203 (unsigned long long)n_sectors,
4204 (unsigned long long)dev->n_sectors);
4205
4206 /*
4207 * Something could have caused HPA to be unlocked
4208 * involuntarily. If n_native_sectors hasn't changed and the
4209 * new size matches it, keep the device.
4210 */
4211 if (dev->n_native_sectors == n_native_sectors &&
4212 dev->n_sectors > n_sectors && dev->n_sectors == n_native_sectors) {
4213 ata_dev_printk(dev, KERN_WARNING,
4214 "new n_sectors matches native, probably "
4215 "late HPA unlock, continuing\n");
4216 /* keep using the old n_sectors */
4217 dev->n_sectors = n_sectors;
4218 return 0;
Tejun Heo6ddcd3b2007-05-15 03:28:15 +09004219 }
4220
Tejun Heo445d2112010-04-05 10:33:13 +09004221 /*
4222 * Some BIOSes boot w/o HPA but resume w/ HPA locked. Try
4223 * unlocking HPA in those cases.
4224 *
4225 * https://bugzilla.kernel.org/show_bug.cgi?id=15396
4226 */
4227 if (dev->n_native_sectors == n_native_sectors &&
4228 dev->n_sectors < n_sectors && n_sectors == n_native_sectors &&
4229 !(dev->horkage & ATA_HORKAGE_BROKEN_HPA)) {
4230 ata_dev_printk(dev, KERN_WARNING,
4231 "old n_sectors matches native, probably "
4232 "late HPA lock, will try to unlock HPA\n");
4233 /* try unlocking HPA */
4234 dev->flags |= ATA_DFLAG_UNLOCK_HPA;
4235 rc = -EIO;
4236 } else
4237 rc = -ENODEV;
Tejun Heo623a3122006-03-05 17:55:58 +09004238
Tejun Heo445d2112010-04-05 10:33:13 +09004239 /* restore original n_[native_]sectors and fail */
4240 dev->n_native_sectors = n_native_sectors;
4241 dev->n_sectors = n_sectors;
Tejun Heo623a3122006-03-05 17:55:58 +09004242 fail:
Tejun Heof15a1da2006-05-15 20:57:56 +09004243 ata_dev_printk(dev, KERN_ERR, "revalidation failed (errno=%d)\n", rc);
Tejun Heo623a3122006-03-05 17:55:58 +09004244 return rc;
4245}
4246
Alan Cox6919a0a2006-10-27 19:08:46 -07004247struct ata_blacklist_entry {
4248 const char *model_num;
4249 const char *model_rev;
4250 unsigned long horkage;
4251};
4252
4253static const struct ata_blacklist_entry ata_device_blacklist [] = {
4254 /* Devices with DMA related problems under Linux */
4255 { "WDC AC11000H", NULL, ATA_HORKAGE_NODMA },
4256 { "WDC AC22100H", NULL, ATA_HORKAGE_NODMA },
4257 { "WDC AC32500H", NULL, ATA_HORKAGE_NODMA },
4258 { "WDC AC33100H", NULL, ATA_HORKAGE_NODMA },
4259 { "WDC AC31600H", NULL, ATA_HORKAGE_NODMA },
4260 { "WDC AC32100H", "24.09P07", ATA_HORKAGE_NODMA },
4261 { "WDC AC23200L", "21.10N21", ATA_HORKAGE_NODMA },
4262 { "Compaq CRD-8241B", NULL, ATA_HORKAGE_NODMA },
4263 { "CRD-8400B", NULL, ATA_HORKAGE_NODMA },
4264 { "CRD-8480B", NULL, ATA_HORKAGE_NODMA },
4265 { "CRD-8482B", NULL, ATA_HORKAGE_NODMA },
4266 { "CRD-84", NULL, ATA_HORKAGE_NODMA },
4267 { "SanDisk SDP3B", NULL, ATA_HORKAGE_NODMA },
4268 { "SanDisk SDP3B-64", NULL, ATA_HORKAGE_NODMA },
4269 { "SANYO CD-ROM CRD", NULL, ATA_HORKAGE_NODMA },
4270 { "HITACHI CDR-8", NULL, ATA_HORKAGE_NODMA },
4271 { "HITACHI CDR-8335", NULL, ATA_HORKAGE_NODMA },
4272 { "HITACHI CDR-8435", NULL, ATA_HORKAGE_NODMA },
4273 { "Toshiba CD-ROM XM-6202B", NULL, ATA_HORKAGE_NODMA },
4274 { "TOSHIBA CD-ROM XM-1702BC", NULL, ATA_HORKAGE_NODMA },
4275 { "CD-532E-A", NULL, ATA_HORKAGE_NODMA },
4276 { "E-IDE CD-ROM CR-840",NULL, ATA_HORKAGE_NODMA },
4277 { "CD-ROM Drive/F5A", NULL, ATA_HORKAGE_NODMA },
4278 { "WPI CDD-820", NULL, ATA_HORKAGE_NODMA },
4279 { "SAMSUNG CD-ROM SC-148C", NULL, ATA_HORKAGE_NODMA },
4280 { "SAMSUNG CD-ROM SC", NULL, ATA_HORKAGE_NODMA },
Alan Cox6919a0a2006-10-27 19:08:46 -07004281 { "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,ATA_HORKAGE_NODMA },
4282 { "_NEC DV5800A", NULL, ATA_HORKAGE_NODMA },
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004283 { "SAMSUNG CD-ROM SN-124", "N001", ATA_HORKAGE_NODMA },
Dave Jones39f19882007-05-21 14:31:03 -04004284 { "Seagate STT20000A", NULL, ATA_HORKAGE_NODMA },
Tejun Heo3af9a772007-09-23 13:19:54 +09004285 /* Odd clown on sil3726/4726 PMPs */
Tejun Heo50af2fa2008-05-19 01:15:14 +09004286 { "Config Disk", NULL, ATA_HORKAGE_DISABLE },
Alan Cox6919a0a2006-10-27 19:08:46 -07004287
Albert Lee18d6e9d2007-04-02 11:34:15 +08004288 /* Weird ATAPI devices */
Tejun Heo40a1d532007-06-27 02:49:38 +09004289 { "TORiSAN DVD-ROM DRD-N216", NULL, ATA_HORKAGE_MAX_SEC_128 },
Tejun Heo6a87e422008-11-03 19:01:09 +09004290 { "QUANTUM DAT DAT72-000", NULL, ATA_HORKAGE_ATAPI_MOD16_DMA },
Albert Lee18d6e9d2007-04-02 11:34:15 +08004291
Alan Cox6919a0a2006-10-27 19:08:46 -07004292 /* Devices we expect to fail diagnostics */
4293
4294 /* Devices where NCQ should be avoided */
4295 /* NCQ is slow */
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004296 { "WDC WD740ADFD-00", NULL, ATA_HORKAGE_NONCQ },
Tejun Heo459ad682007-12-07 12:46:23 +09004297 { "WDC WD740ADFD-00NLR1", NULL, ATA_HORKAGE_NONCQ, },
Tejun Heo09125ea2007-02-28 15:21:23 +09004298 /* http://thread.gmane.org/gmane.linux.ide/14907 */
4299 { "FUJITSU MHT2060BH", NULL, ATA_HORKAGE_NONCQ },
Paul Rolland7acfaf32007-03-26 21:43:44 -08004300 /* NCQ is broken */
Jeff Garzik539cc7c2007-09-20 16:31:47 -04004301 { "Maxtor *", "BANC*", ATA_HORKAGE_NONCQ },
Alan Cox0e3dbc02007-09-20 15:22:47 +01004302 { "Maxtor 7V300F0", "VA111630", ATA_HORKAGE_NONCQ },
Paolo Ornatida6f0ec2007-10-04 11:06:56 +09004303 { "ST380817AS", "3.42", ATA_HORKAGE_NONCQ },
Tejun Heoe41bd3e2007-12-09 19:45:39 +09004304 { "ST3160023AS", "3.42", ATA_HORKAGE_NONCQ },
Lubomir Bulej5ccfca92008-12-22 11:35:22 +01004305 { "OCZ CORE_SSD", "02.10104", ATA_HORKAGE_NONCQ },
Jeff Garzik539cc7c2007-09-20 16:31:47 -04004306
Tejun Heoac70a962008-11-27 13:36:48 +09004307 /* Seagate NCQ + FLUSH CACHE firmware bug */
Tejun Heod10d4912008-12-11 13:42:42 +09004308 { "ST31500341AS", "SD15", ATA_HORKAGE_NONCQ |
Tejun Heoac70a962008-11-27 13:36:48 +09004309 ATA_HORKAGE_FIRMWARE_WARN },
Tejun Heod10d4912008-12-11 13:42:42 +09004310 { "ST31500341AS", "SD16", ATA_HORKAGE_NONCQ |
Tejun Heoac70a962008-11-27 13:36:48 +09004311 ATA_HORKAGE_FIRMWARE_WARN },
Tejun Heod10d4912008-12-11 13:42:42 +09004312 { "ST31500341AS", "SD17", ATA_HORKAGE_NONCQ |
Tejun Heoac70a962008-11-27 13:36:48 +09004313 ATA_HORKAGE_FIRMWARE_WARN },
Tejun Heod10d4912008-12-11 13:42:42 +09004314 { "ST31500341AS", "SD18", ATA_HORKAGE_NONCQ |
Tejun Heoac70a962008-11-27 13:36:48 +09004315 ATA_HORKAGE_FIRMWARE_WARN },
Tejun Heod10d4912008-12-11 13:42:42 +09004316 { "ST31500341AS", "SD19", ATA_HORKAGE_NONCQ |
Tejun Heoac70a962008-11-27 13:36:48 +09004317 ATA_HORKAGE_FIRMWARE_WARN },
Tejun Heod10d4912008-12-11 13:42:42 +09004318
4319 { "ST31000333AS", "SD15", ATA_HORKAGE_NONCQ |
4320 ATA_HORKAGE_FIRMWARE_WARN },
4321 { "ST31000333AS", "SD16", ATA_HORKAGE_NONCQ |
4322 ATA_HORKAGE_FIRMWARE_WARN },
4323 { "ST31000333AS", "SD17", ATA_HORKAGE_NONCQ |
4324 ATA_HORKAGE_FIRMWARE_WARN },
4325 { "ST31000333AS", "SD18", ATA_HORKAGE_NONCQ |
4326 ATA_HORKAGE_FIRMWARE_WARN },
4327 { "ST31000333AS", "SD19", ATA_HORKAGE_NONCQ |
4328 ATA_HORKAGE_FIRMWARE_WARN },
4329
4330 { "ST3640623AS", "SD15", ATA_HORKAGE_NONCQ |
4331 ATA_HORKAGE_FIRMWARE_WARN },
4332 { "ST3640623AS", "SD16", ATA_HORKAGE_NONCQ |
4333 ATA_HORKAGE_FIRMWARE_WARN },
4334 { "ST3640623AS", "SD17", ATA_HORKAGE_NONCQ |
4335 ATA_HORKAGE_FIRMWARE_WARN },
4336 { "ST3640623AS", "SD18", ATA_HORKAGE_NONCQ |
4337 ATA_HORKAGE_FIRMWARE_WARN },
4338 { "ST3640623AS", "SD19", ATA_HORKAGE_NONCQ |
4339 ATA_HORKAGE_FIRMWARE_WARN },
4340
4341 { "ST3640323AS", "SD15", ATA_HORKAGE_NONCQ |
4342 ATA_HORKAGE_FIRMWARE_WARN },
4343 { "ST3640323AS", "SD16", ATA_HORKAGE_NONCQ |
4344 ATA_HORKAGE_FIRMWARE_WARN },
4345 { "ST3640323AS", "SD17", ATA_HORKAGE_NONCQ |
4346 ATA_HORKAGE_FIRMWARE_WARN },
4347 { "ST3640323AS", "SD18", ATA_HORKAGE_NONCQ |
4348 ATA_HORKAGE_FIRMWARE_WARN },
4349 { "ST3640323AS", "SD19", ATA_HORKAGE_NONCQ |
4350 ATA_HORKAGE_FIRMWARE_WARN },
4351
4352 { "ST3320813AS", "SD15", ATA_HORKAGE_NONCQ |
4353 ATA_HORKAGE_FIRMWARE_WARN },
4354 { "ST3320813AS", "SD16", ATA_HORKAGE_NONCQ |
4355 ATA_HORKAGE_FIRMWARE_WARN },
4356 { "ST3320813AS", "SD17", ATA_HORKAGE_NONCQ |
4357 ATA_HORKAGE_FIRMWARE_WARN },
4358 { "ST3320813AS", "SD18", ATA_HORKAGE_NONCQ |
4359 ATA_HORKAGE_FIRMWARE_WARN },
4360 { "ST3320813AS", "SD19", ATA_HORKAGE_NONCQ |
4361 ATA_HORKAGE_FIRMWARE_WARN },
4362
4363 { "ST3320613AS", "SD15", ATA_HORKAGE_NONCQ |
4364 ATA_HORKAGE_FIRMWARE_WARN },
4365 { "ST3320613AS", "SD16", ATA_HORKAGE_NONCQ |
4366 ATA_HORKAGE_FIRMWARE_WARN },
4367 { "ST3320613AS", "SD17", ATA_HORKAGE_NONCQ |
4368 ATA_HORKAGE_FIRMWARE_WARN },
4369 { "ST3320613AS", "SD18", ATA_HORKAGE_NONCQ |
4370 ATA_HORKAGE_FIRMWARE_WARN },
4371 { "ST3320613AS", "SD19", ATA_HORKAGE_NONCQ |
Tejun Heoac70a962008-11-27 13:36:48 +09004372 ATA_HORKAGE_FIRMWARE_WARN },
4373
Robert Hancock36e337d2007-04-02 22:05:29 -06004374 /* Blacklist entries taken from Silicon Image 3124/3132
4375 Windows driver .inf file - also several Linux problem reports */
4376 { "HTS541060G9SA00", "MB3OC60D", ATA_HORKAGE_NONCQ, },
4377 { "HTS541080G9SA00", "MB4OC60D", ATA_HORKAGE_NONCQ, },
4378 { "HTS541010G9SA00", "MBZOC60D", ATA_HORKAGE_NONCQ, },
Alan Cox6919a0a2006-10-27 19:08:46 -07004379
Tejun Heo68b0ddb2010-04-05 10:51:26 +09004380 /* https://bugzilla.kernel.org/show_bug.cgi?id=15573 */
4381 { "C300-CTFDDAC128MAG", "0001", ATA_HORKAGE_NONCQ, },
4382
Tejun Heo16c55b02007-08-29 11:58:33 +09004383 /* devices which puke on READ_NATIVE_MAX */
4384 { "HDS724040KLSA80", "KFAOA20N", ATA_HORKAGE_BROKEN_HPA, },
4385 { "WDC WD3200JD-00KLB0", "WD-WCAMR1130137", ATA_HORKAGE_BROKEN_HPA },
4386 { "WDC WD2500JD-00HBB0", "WD-WMAL71490727", ATA_HORKAGE_BROKEN_HPA },
4387 { "MAXTOR 6L080L4", "A93.0500", ATA_HORKAGE_BROKEN_HPA },
Alan Cox6919a0a2006-10-27 19:08:46 -07004388
Tejun Heo78313872009-08-07 01:59:15 +09004389 /* this one allows HPA unlocking but fails IOs on the area */
4390 { "OCZ-VERTEX", "1.30", ATA_HORKAGE_BROKEN_HPA },
4391
Alan Cox93328e12007-09-29 04:06:48 -04004392 /* Devices which report 1 sector over size HPA */
4393 { "ST340823A", NULL, ATA_HORKAGE_HPA_SIZE, },
4394 { "ST320413A", NULL, ATA_HORKAGE_HPA_SIZE, },
Mikko Rapelib152fcd2008-02-19 01:41:25 +01004395 { "ST310211A", NULL, ATA_HORKAGE_HPA_SIZE, },
Alan Cox93328e12007-09-29 04:06:48 -04004396
Alan Cox6bbfd532007-11-05 22:58:58 +00004397 /* Devices which get the IVB wrong */
4398 { "QUANTUM FIREBALLlct10 05", "A03.0900", ATA_HORKAGE_IVB, },
Alan Coxa79067e2008-04-29 14:08:36 +01004399 /* Maybe we should just blacklist TSSTcorp... */
4400 { "TSSTcorp CDDVDW SH-S202H", "SB00", ATA_HORKAGE_IVB, },
4401 { "TSSTcorp CDDVDW SH-S202H", "SB01", ATA_HORKAGE_IVB, },
Alan Cox6bbfd532007-11-05 22:58:58 +00004402 { "TSSTcorp CDDVDW SH-S202J", "SB00", ATA_HORKAGE_IVB, },
Peter Missele9f33402007-11-27 18:04:42 +01004403 { "TSSTcorp CDDVDW SH-S202J", "SB01", ATA_HORKAGE_IVB, },
4404 { "TSSTcorp CDDVDW SH-S202N", "SB00", ATA_HORKAGE_IVB, },
4405 { "TSSTcorp CDDVDW SH-S202N", "SB01", ATA_HORKAGE_IVB, },
Alan Cox6bbfd532007-11-05 22:58:58 +00004406
Jens Axboe9ce8e302008-08-27 15:23:18 +02004407 /* Devices that do not need bridging limits applied */
4408 { "MTRON MSP-SATA*", NULL, ATA_HORKAGE_BRIDGE_OK, },
4409
Tejun Heo90627122009-01-29 20:31:36 +09004410 /* Devices which aren't very happy with higher link speeds */
4411 { "WD My Book", NULL, ATA_HORKAGE_1_5_GBPS, },
4412
Tejun Heod0cb43b2009-07-09 09:27:50 +09004413 /*
4414 * Devices which choke on SETXFER. Applies only if both the
4415 * device and controller are SATA.
4416 */
4417 { "PIONEER DVD-RW DVRTD08", "1.00", ATA_HORKAGE_NOSETXFER },
4418
Alan Cox6919a0a2006-10-27 19:08:46 -07004419 /* End Marker */
4420 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421};
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05004422
Adrian Bunk741b7762007-10-24 18:23:06 +02004423static int strn_pattern_cmp(const char *patt, const char *name, int wildchar)
Jeff Garzik539cc7c2007-09-20 16:31:47 -04004424{
4425 const char *p;
4426 int len;
4427
4428 /*
4429 * check for trailing wildcard: *\0
4430 */
4431 p = strchr(patt, wildchar);
4432 if (p && ((*(p + 1)) == 0))
4433 len = p - patt;
Andrew Paprocki317b50b2007-10-15 15:43:12 -04004434 else {
Jeff Garzik539cc7c2007-09-20 16:31:47 -04004435 len = strlen(name);
Andrew Paprocki317b50b2007-10-15 15:43:12 -04004436 if (!len) {
4437 if (!*patt)
4438 return 0;
4439 return -1;
4440 }
4441 }
Jeff Garzik539cc7c2007-09-20 16:31:47 -04004442
4443 return strncmp(patt, name, len);
4444}
4445
Tejun Heo75683fe2007-07-05 13:31:27 +09004446static unsigned long ata_dev_blacklisted(const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447{
Tejun Heo8bfa79f2007-01-02 20:19:40 +09004448 unsigned char model_num[ATA_ID_PROD_LEN + 1];
4449 unsigned char model_rev[ATA_ID_FW_REV_LEN + 1];
Alan Cox6919a0a2006-10-27 19:08:46 -07004450 const struct ata_blacklist_entry *ad = ata_device_blacklist;
Albert Lee3a778272006-06-22 13:00:25 +08004451
Tejun Heo8bfa79f2007-01-02 20:19:40 +09004452 ata_id_c_string(dev->id, model_num, ATA_ID_PROD, sizeof(model_num));
4453 ata_id_c_string(dev->id, model_rev, ATA_ID_FW_REV, sizeof(model_rev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454
Alan Cox6919a0a2006-10-27 19:08:46 -07004455 while (ad->model_num) {
Jeff Garzik539cc7c2007-09-20 16:31:47 -04004456 if (!strn_pattern_cmp(ad->model_num, model_num, '*')) {
Alan Cox6919a0a2006-10-27 19:08:46 -07004457 if (ad->model_rev == NULL)
4458 return ad->horkage;
Jeff Garzik539cc7c2007-09-20 16:31:47 -04004459 if (!strn_pattern_cmp(ad->model_rev, model_rev, '*'))
Alan Cox6919a0a2006-10-27 19:08:46 -07004460 return ad->horkage;
Alan Coxf4b15fe2006-03-22 15:54:04 +00004461 }
Alan Cox6919a0a2006-10-27 19:08:46 -07004462 ad++;
Alan Coxf4b15fe2006-03-22 15:54:04 +00004463 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464 return 0;
4465}
4466
Alan Cox6919a0a2006-10-27 19:08:46 -07004467static int ata_dma_blacklisted(const struct ata_device *dev)
4468{
4469 /* We don't support polling DMA.
4470 * DMA blacklist those ATAPI devices with CDB-intr (and use PIO)
4471 * if the LLDD handles only interrupts in the HSM_ST_LAST state.
4472 */
Tejun Heo9af5c9c2007-08-06 18:36:22 +09004473 if ((dev->link->ap->flags & ATA_FLAG_PIO_POLLING) &&
Alan Cox6919a0a2006-10-27 19:08:46 -07004474 (dev->flags & ATA_DFLAG_CDB_INTR))
4475 return 1;
Tejun Heo75683fe2007-07-05 13:31:27 +09004476 return (dev->horkage & ATA_HORKAGE_NODMA) ? 1 : 0;
Alan Cox6919a0a2006-10-27 19:08:46 -07004477}
4478
Tejun Heoa6d5a512006-03-06 04:31:57 +09004479/**
Alan Cox6bbfd532007-11-05 22:58:58 +00004480 * ata_is_40wire - check drive side detection
4481 * @dev: device
4482 *
4483 * Perform drive side detection decoding, allowing for device vendors
4484 * who can't follow the documentation.
4485 */
4486
4487static int ata_is_40wire(struct ata_device *dev)
4488{
4489 if (dev->horkage & ATA_HORKAGE_IVB)
4490 return ata_drive_40wire_relaxed(dev->id);
4491 return ata_drive_40wire(dev->id);
4492}
4493
4494/**
Alan Cox15a55512008-03-28 14:33:46 -07004495 * cable_is_40wire - 40/80/SATA decider
4496 * @ap: port to consider
4497 *
4498 * This function encapsulates the policy for speed management
4499 * in one place. At the moment we don't cache the result but
4500 * there is a good case for setting ap->cbl to the result when
4501 * we are called with unknown cables (and figuring out if it
4502 * impacts hotplug at all).
4503 *
4504 * Return 1 if the cable appears to be 40 wire.
4505 */
4506
4507static int cable_is_40wire(struct ata_port *ap)
4508{
4509 struct ata_link *link;
4510 struct ata_device *dev;
4511
Tejun Heo4a9c7b32008-10-27 19:59:23 +09004512 /* If the controller thinks we are 40 wire, we are. */
Alan Cox15a55512008-03-28 14:33:46 -07004513 if (ap->cbl == ATA_CBL_PATA40)
4514 return 1;
Tejun Heo4a9c7b32008-10-27 19:59:23 +09004515
4516 /* If the controller thinks we are 80 wire, we are. */
Alan Cox15a55512008-03-28 14:33:46 -07004517 if (ap->cbl == ATA_CBL_PATA80 || ap->cbl == ATA_CBL_SATA)
4518 return 0;
Tejun Heo4a9c7b32008-10-27 19:59:23 +09004519
4520 /* If the system is known to be 40 wire short cable (eg
4521 * laptop), then we allow 80 wire modes even if the drive
4522 * isn't sure.
4523 */
Alan Coxf7920682008-04-08 16:37:50 +01004524 if (ap->cbl == ATA_CBL_PATA40_SHORT)
4525 return 0;
Alan Cox15a55512008-03-28 14:33:46 -07004526
Tejun Heo4a9c7b32008-10-27 19:59:23 +09004527 /* If the controller doesn't know, we scan.
4528 *
4529 * Note: We look for all 40 wire detects at this point. Any
4530 * 80 wire detect is taken to be 80 wire cable because
4531 * - in many setups only the one drive (slave if present) will
4532 * give a valid detect
4533 * - if you have a non detect capable drive you don't want it
4534 * to colour the choice
4535 */
Tejun Heo1eca4362008-11-03 20:03:17 +09004536 ata_for_each_link(link, ap, EDGE) {
4537 ata_for_each_dev(dev, link, ENABLED) {
4538 if (!ata_is_40wire(dev))
Alan Cox15a55512008-03-28 14:33:46 -07004539 return 0;
4540 }
4541 }
4542 return 1;
4543}
4544
4545/**
Tejun Heoa6d5a512006-03-06 04:31:57 +09004546 * ata_dev_xfermask - Compute supported xfermask of the given device
Tejun Heoa6d5a512006-03-06 04:31:57 +09004547 * @dev: Device to compute xfermask for
4548 *
Tejun Heoacf356b2006-03-24 14:07:50 +09004549 * Compute supported xfermask of @dev and store it in
4550 * dev->*_mask. This function is responsible for applying all
4551 * known limits including host controller limits, device
4552 * blacklist, etc...
Tejun Heoa6d5a512006-03-06 04:31:57 +09004553 *
4554 * LOCKING:
4555 * None.
Tejun Heoa6d5a512006-03-06 04:31:57 +09004556 */
Tejun Heo3373efd2006-05-15 20:57:53 +09004557static void ata_dev_xfermask(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09004559 struct ata_link *link = dev->link;
4560 struct ata_port *ap = link->ap;
Jeff Garzikcca39742006-08-24 03:19:22 -04004561 struct ata_host *host = ap->host;
Tejun Heoa6d5a512006-03-06 04:31:57 +09004562 unsigned long xfer_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563
Tejun Heo37deecb2006-08-10 16:59:07 +09004564 /* controller modes available */
Tejun Heo565083e2006-04-02 17:54:47 +09004565 xfer_mask = ata_pack_xfermask(ap->pio_mask,
4566 ap->mwdma_mask, ap->udma_mask);
4567
Robert Hancock8343f882007-03-06 02:37:51 -08004568 /* drive modes available */
Tejun Heo37deecb2006-08-10 16:59:07 +09004569 xfer_mask &= ata_pack_xfermask(dev->pio_mask,
4570 dev->mwdma_mask, dev->udma_mask);
4571 xfer_mask &= ata_id_xfermask(dev->id);
Tejun Heo565083e2006-04-02 17:54:47 +09004572
Alan Coxb352e572006-08-10 18:52:12 +01004573 /*
4574 * CFA Advanced TrueIDE timings are not allowed on a shared
4575 * cable
4576 */
4577 if (ata_dev_pair(dev)) {
4578 /* No PIO5 or PIO6 */
4579 xfer_mask &= ~(0x03 << (ATA_SHIFT_PIO + 5));
4580 /* No MWDMA3 or MWDMA 4 */
4581 xfer_mask &= ~(0x03 << (ATA_SHIFT_MWDMA + 3));
4582 }
4583
Tejun Heo37deecb2006-08-10 16:59:07 +09004584 if (ata_dma_blacklisted(dev)) {
4585 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
Tejun Heof15a1da2006-05-15 20:57:56 +09004586 ata_dev_printk(dev, KERN_WARNING,
4587 "device is on DMA blacklist, disabling DMA\n");
Tejun Heo37deecb2006-08-10 16:59:07 +09004588 }
Tejun Heoa6d5a512006-03-06 04:31:57 +09004589
Petr Vandrovec14d66ab2007-03-08 10:12:12 +01004590 if ((host->flags & ATA_HOST_SIMPLEX) &&
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004591 host->simplex_claimed && host->simplex_claimed != ap) {
Tejun Heo37deecb2006-08-10 16:59:07 +09004592 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
4593 ata_dev_printk(dev, KERN_WARNING, "simplex DMA is claimed by "
4594 "other device, disabling DMA\n");
Alan Cox5444a6f2006-03-27 18:58:20 +01004595 }
Tejun Heo565083e2006-04-02 17:54:47 +09004596
Jeff Garzike4246752007-03-09 09:56:46 -05004597 if (ap->flags & ATA_FLAG_NO_IORDY)
4598 xfer_mask &= ata_pio_mask_no_iordy(dev);
4599
Alan Cox5444a6f2006-03-27 18:58:20 +01004600 if (ap->ops->mode_filter)
Alan Coxa76b62c2007-03-09 09:34:07 -05004601 xfer_mask = ap->ops->mode_filter(dev, xfer_mask);
Alan Cox5444a6f2006-03-27 18:58:20 +01004602
Robert Hancock8343f882007-03-06 02:37:51 -08004603 /* Apply cable rule here. Don't apply it early because when
4604 * we handle hot plug the cable type can itself change.
4605 * Check this last so that we know if the transfer rate was
4606 * solely limited by the cable.
4607 * Unknown or 80 wire cables reported host side are checked
4608 * drive side as well. Cases where we know a 40wire cable
4609 * is used safely for 80 are not checked here.
4610 */
4611 if (xfer_mask & (0xF8 << ATA_SHIFT_UDMA))
4612 /* UDMA/44 or higher would be available */
Alan Cox15a55512008-03-28 14:33:46 -07004613 if (cable_is_40wire(ap)) {
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004614 ata_dev_printk(dev, KERN_WARNING,
Robert Hancock8343f882007-03-06 02:37:51 -08004615 "limited to UDMA/33 due to 40-wire cable\n");
4616 xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
4617 }
4618
Tejun Heo565083e2006-04-02 17:54:47 +09004619 ata_unpack_xfermask(xfer_mask, &dev->pio_mask,
4620 &dev->mwdma_mask, &dev->udma_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621}
4622
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625 * @dev: Device to which command will be sent
4626 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04004627 * Issue SET FEATURES - XFER MODE command to device @dev
4628 * on port @ap.
4629 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004630 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004631 * PCI/etc. bus probe sem.
Tejun Heo83206a22006-03-24 15:25:31 +09004632 *
4633 * RETURNS:
4634 * 0 on success, AC_ERR_* mask otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004635 */
4636
Tejun Heo3373efd2006-05-15 20:57:53 +09004637static unsigned int ata_dev_set_xfermode(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638{
Tejun Heoa0123702005-12-13 14:49:31 +09004639 struct ata_taskfile tf;
Tejun Heo83206a22006-03-24 15:25:31 +09004640 unsigned int err_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641
4642 /* set up set-features taskfile */
4643 DPRINTK("set features - xfer mode\n");
4644
Tejun Heo464cf172007-05-27 15:10:40 +02004645 /* Some controllers and ATAPI devices show flaky interrupt
4646 * behavior after setting xfer mode. Use polling instead.
4647 */
Tejun Heo3373efd2006-05-15 20:57:53 +09004648 ata_tf_init(dev, &tf);
Tejun Heoa0123702005-12-13 14:49:31 +09004649 tf.command = ATA_CMD_SET_FEATURES;
4650 tf.feature = SETFEATURES_XFER;
Tejun Heo464cf172007-05-27 15:10:40 +02004651 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_POLLING;
Tejun Heoa0123702005-12-13 14:49:31 +09004652 tf.protocol = ATA_PROT_NODATA;
Alan Coxb9f8ab22007-11-19 14:33:11 +00004653 /* If we are using IORDY we must send the mode setting command */
Jeff Garzik11b7bec2007-11-23 21:12:14 -05004654 if (ata_pio_need_iordy(dev))
4655 tf.nsect = dev->xfer_mode;
Alan Coxb9f8ab22007-11-19 14:33:11 +00004656 /* If the device has IORDY and the controller does not - turn it off */
4657 else if (ata_id_has_iordy(dev->id))
Jeff Garzik11b7bec2007-11-23 21:12:14 -05004658 tf.nsect = 0x01;
Alan Coxb9f8ab22007-11-19 14:33:11 +00004659 else /* In the ancient relic department - skip all of this */
4660 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661
Tejun Heo2b789102007-10-09 15:05:44 +09004662 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663
Tejun Heo83206a22006-03-24 15:25:31 +09004664 DPRINTK("EXIT, err_mask=%x\n", err_mask);
4665 return err_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667/**
Jeff Garzik218f3d32007-10-25 00:33:27 -04004668 * ata_dev_set_feature - Issue SET FEATURES - SATA FEATURES
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04004669 * @dev: Device to which command will be sent
4670 * @enable: Whether to enable or disable the feature
Jeff Garzik218f3d32007-10-25 00:33:27 -04004671 * @feature: The sector count represents the feature to set
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04004672 *
4673 * Issue SET FEATURES - SATA FEATURES command to device @dev
Jeff Garzik218f3d32007-10-25 00:33:27 -04004674 * on port @ap with sector count
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04004675 *
4676 * LOCKING:
4677 * PCI/etc. bus probe sem.
4678 *
4679 * RETURNS:
4680 * 0 on success, AC_ERR_* mask otherwise.
4681 */
Jeff Garzik218f3d32007-10-25 00:33:27 -04004682static unsigned int ata_dev_set_feature(struct ata_device *dev, u8 enable,
4683 u8 feature)
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04004684{
4685 struct ata_taskfile tf;
4686 unsigned int err_mask;
4687
4688 /* set up set-features taskfile */
4689 DPRINTK("set features - SATA features\n");
4690
4691 ata_tf_init(dev, &tf);
4692 tf.command = ATA_CMD_SET_FEATURES;
4693 tf.feature = enable;
4694 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
4695 tf.protocol = ATA_PROT_NODATA;
Jeff Garzik218f3d32007-10-25 00:33:27 -04004696 tf.nsect = feature;
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04004697
Tejun Heo2b789102007-10-09 15:05:44 +09004698 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04004699
4700 DPRINTK("EXIT, err_mask=%x\n", err_mask);
4701 return err_mask;
4702}
4703
4704/**
Albert Lee8bf62ece2005-05-12 15:29:42 -04004705 * ata_dev_init_params - Issue INIT DEV PARAMS command
Albert Lee8bf62ece2005-05-12 15:29:42 -04004706 * @dev: Device to which command will be sent
Randy Dunlape2a7f772006-05-18 10:50:18 -07004707 * @heads: Number of heads (taskfile parameter)
4708 * @sectors: Number of sectors (taskfile parameter)
Albert Lee8bf62ece2005-05-12 15:29:42 -04004709 *
4710 * LOCKING:
Tejun Heo6aff8f12006-02-15 18:24:09 +09004711 * Kernel thread context (may sleep)
4712 *
4713 * RETURNS:
4714 * 0 on success, AC_ERR_* mask otherwise.
Albert Lee8bf62ece2005-05-12 15:29:42 -04004715 */
Tejun Heo3373efd2006-05-15 20:57:53 +09004716static unsigned int ata_dev_init_params(struct ata_device *dev,
4717 u16 heads, u16 sectors)
Albert Lee8bf62ece2005-05-12 15:29:42 -04004718{
Tejun Heoa0123702005-12-13 14:49:31 +09004719 struct ata_taskfile tf;
Tejun Heo6aff8f12006-02-15 18:24:09 +09004720 unsigned int err_mask;
Albert Lee8bf62ece2005-05-12 15:29:42 -04004721
4722 /* Number of sectors per track 1-255. Number of heads 1-16 */
4723 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
Albert Lee00b6f5e2006-03-27 16:39:18 +08004724 return AC_ERR_INVALID;
Albert Lee8bf62ece2005-05-12 15:29:42 -04004725
4726 /* set up init dev params taskfile */
4727 DPRINTK("init dev params \n");
4728
Tejun Heo3373efd2006-05-15 20:57:53 +09004729 ata_tf_init(dev, &tf);
Tejun Heoa0123702005-12-13 14:49:31 +09004730 tf.command = ATA_CMD_INIT_DEV_PARAMS;
4731 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
4732 tf.protocol = ATA_PROT_NODATA;
4733 tf.nsect = sectors;
4734 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
Albert Lee8bf62ece2005-05-12 15:29:42 -04004735
Tejun Heo2b789102007-10-09 15:05:44 +09004736 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
Alan Cox18b24662007-08-08 14:28:49 +01004737 /* A clean abort indicates an original or just out of spec drive
4738 and we should continue as we issue the setup based on the
4739 drive reported working geometry */
4740 if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
4741 err_mask = 0;
Albert Lee8bf62ece2005-05-12 15:29:42 -04004742
Tejun Heo6aff8f12006-02-15 18:24:09 +09004743 DPRINTK("EXIT, err_mask=%x\n", err_mask);
4744 return err_mask;
Albert Lee8bf62ece2005-05-12 15:29:42 -04004745}
4746
4747/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004748 * ata_sg_clean - Unmap DMA memory associated with command
4749 * @qc: Command containing DMA memory to be released
4750 *
4751 * Unmap all mapped DMA memory associated with this command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752 *
4753 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004754 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755 */
Tejun Heo70e6ad02006-11-14 22:47:10 +09004756void ata_sg_clean(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004757{
4758 struct ata_port *ap = qc->ap;
Tejun Heoff2aeb12007-12-05 16:43:11 +09004759 struct scatterlist *sg = qc->sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760 int dir = qc->dma_dir;
4761
Tejun Heoefcb3cf2009-01-09 19:19:14 +09004762 WARN_ON_ONCE(sg == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763
James Bottomleydde20202008-02-19 11:36:56 +01004764 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004765
James Bottomleydde20202008-02-19 11:36:56 +01004766 if (qc->n_elem)
FUJITA Tomonori58256272009-02-27 17:35:43 +09004767 dma_unmap_sg(ap->dev, sg, qc->orig_n_elem, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768
4769 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Tejun Heoff2aeb12007-12-05 16:43:11 +09004770 qc->sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771}
4772
4773/**
Tejun Heo5895ef92008-06-17 12:36:26 +09004774 * atapi_check_dma - Check whether ATAPI DMA can be supported
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775 * @qc: Metadata associated with taskfile to check
4776 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04004777 * Allow low-level driver to filter ATA PACKET commands, returning
4778 * a status indicating whether or not it is OK to use DMA for the
4779 * supplied PACKET command.
4780 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004781 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004782 * spin_lock_irqsave(host lock)
Jeff Garzik0cba6322005-05-30 19:49:12 -04004783 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004784 * RETURNS: 0 when ATAPI DMA can be used
4785 * nonzero otherwise
4786 */
Tejun Heo5895ef92008-06-17 12:36:26 +09004787int atapi_check_dma(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004788{
4789 struct ata_port *ap = qc->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004790
Tejun Heob9a41972007-06-27 02:48:43 +09004791 /* Don't allow DMA if it isn't multiple of 16 bytes. Quite a
4792 * few ATAPI devices choke on such DMA requests.
4793 */
Tejun Heo6a87e422008-11-03 19:01:09 +09004794 if (!(qc->dev->horkage & ATA_HORKAGE_ATAPI_MOD16_DMA) &&
4795 unlikely(qc->nbytes & 15))
Tejun Heob9a41972007-06-27 02:48:43 +09004796 return 1;
Albert Lee6f23a312007-04-02 11:39:25 +08004797
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798 if (ap->ops->check_atapi_dma)
Tejun Heob9a41972007-06-27 02:48:43 +09004799 return ap->ops->check_atapi_dma(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800
Tejun Heob9a41972007-06-27 02:48:43 +09004801 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004802}
Tejun Heob9a41972007-06-27 02:48:43 +09004803
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804/**
Tejun Heo31cc23b2007-09-23 13:14:12 +09004805 * ata_std_qc_defer - Check whether a qc needs to be deferred
4806 * @qc: ATA command in question
4807 *
4808 * Non-NCQ commands cannot run with any other command, NCQ or
4809 * not. As upper layer only knows the queue depth, we are
4810 * responsible for maintaining exclusion. This function checks
4811 * whether a new command @qc can be issued.
4812 *
4813 * LOCKING:
4814 * spin_lock_irqsave(host lock)
4815 *
4816 * RETURNS:
4817 * ATA_DEFER_* if deferring is needed, 0 otherwise.
4818 */
4819int ata_std_qc_defer(struct ata_queued_cmd *qc)
4820{
4821 struct ata_link *link = qc->dev->link;
4822
4823 if (qc->tf.protocol == ATA_PROT_NCQ) {
4824 if (!ata_tag_valid(link->active_tag))
4825 return 0;
4826 } else {
4827 if (!ata_tag_valid(link->active_tag) && !link->sactive)
4828 return 0;
4829 }
4830
4831 return ATA_DEFER_LINK;
4832}
4833
Brian Kinge46834c2006-03-17 17:04:03 -06004834void ata_noop_qc_prep(struct ata_queued_cmd *qc) { }
4835
Jeff Garzik0cba6322005-05-30 19:49:12 -04004836/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004837 * ata_sg_init - Associate command with scatter-gather table.
4838 * @qc: Command to be associated
4839 * @sg: Scatter-gather table.
4840 * @n_elem: Number of elements in s/g table.
4841 *
4842 * Initialize the data-related elements of queued_cmd @qc
4843 * to point to a scatter-gather table @sg, containing @n_elem
4844 * elements.
4845 *
4846 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004847 * spin_lock_irqsave(host lock)
Jeff Garzik0cba6322005-05-30 19:49:12 -04004848 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
4850 unsigned int n_elem)
4851{
Tejun Heoff2aeb12007-12-05 16:43:11 +09004852 qc->sg = sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853 qc->n_elem = n_elem;
Tejun Heoff2aeb12007-12-05 16:43:11 +09004854 qc->cursg = qc->sg;
4855}
4856
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004858 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
4859 * @qc: Command with scatter-gather table to be mapped.
4860 *
4861 * DMA-map the scatter-gather table associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862 *
4863 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004864 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865 *
4866 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004867 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868 *
4869 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870static int ata_sg_setup(struct ata_queued_cmd *qc)
4871{
4872 struct ata_port *ap = qc->ap;
James Bottomleydde20202008-02-19 11:36:56 +01004873 unsigned int n_elem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874
Tejun Heo44877b42007-02-21 01:06:51 +09004875 VPRINTK("ENTER, ata%u\n", ap->print_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876
James Bottomleydde20202008-02-19 11:36:56 +01004877 n_elem = dma_map_sg(ap->dev, qc->sg, qc->n_elem, qc->dma_dir);
4878 if (n_elem < 1)
4879 return -1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004880
James Bottomleydde20202008-02-19 11:36:56 +01004881 DPRINTK("%d sg elements mapped\n", n_elem);
FUJITA Tomonori58256272009-02-27 17:35:43 +09004882 qc->orig_n_elem = qc->n_elem;
James Bottomleydde20202008-02-19 11:36:56 +01004883 qc->n_elem = n_elem;
Tejun Heof92a2632007-12-05 16:43:10 +09004884 qc->flags |= ATA_QCFLAG_DMAMAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885
4886 return 0;
4887}
4888
4889/**
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004890 * swap_buf_le16 - swap halves of 16-bit words in place
Edward Falk0baab862005-06-02 18:17:13 -04004891 * @buf: Buffer to swap
4892 * @buf_words: Number of 16-bit words in buffer.
4893 *
4894 * Swap halves of 16-bit words if needed to convert from
4895 * little-endian byte order to native cpu byte order, or
4896 * vice-versa.
4897 *
4898 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004899 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04004900 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901void swap_buf_le16(u16 *buf, unsigned int buf_words)
4902{
4903#ifdef __BIG_ENDIAN
4904 unsigned int i;
4905
4906 for (i = 0; i < buf_words; i++)
4907 buf[i] = le16_to_cpu(buf[i]);
4908#endif /* __BIG_ENDIAN */
4909}
4910
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004911/**
Tejun Heo8a8bc222008-11-10 14:48:21 +09004912 * ata_qc_new - Request an available ATA command, for queueing
Randy Dunlap5eb66fe2009-01-20 16:28:59 -08004913 * @ap: target port
Tejun Heo8a8bc222008-11-10 14:48:21 +09004914 *
4915 * LOCKING:
4916 * None.
4917 */
4918
4919static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
4920{
4921 struct ata_queued_cmd *qc = NULL;
4922 unsigned int i;
4923
4924 /* no command while frozen */
4925 if (unlikely(ap->pflags & ATA_PFLAG_FROZEN))
4926 return NULL;
4927
4928 /* the last tag is reserved for internal command. */
4929 for (i = 0; i < ATA_MAX_QUEUE - 1; i++)
4930 if (!test_and_set_bit(i, &ap->qc_allocated)) {
4931 qc = __ata_qc_from_tag(ap, i);
4932 break;
4933 }
4934
4935 if (qc)
4936 qc->tag = i;
4937
4938 return qc;
4939}
4940
4941/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004942 * ata_qc_new_init - Request an available ATA command, and initialize it
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943 * @dev: Device from whom we request an available command structure
4944 *
4945 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004946 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947 */
4948
Tejun Heo8a8bc222008-11-10 14:48:21 +09004949struct ata_queued_cmd *ata_qc_new_init(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09004951 struct ata_port *ap = dev->link->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952 struct ata_queued_cmd *qc;
4953
Tejun Heo8a8bc222008-11-10 14:48:21 +09004954 qc = ata_qc_new(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004955 if (qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956 qc->scsicmd = NULL;
4957 qc->ap = ap;
4958 qc->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05004960 ata_qc_reinit(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961 }
4962
4963 return qc;
4964}
4965
Tejun Heo8a8bc222008-11-10 14:48:21 +09004966/**
4967 * ata_qc_free - free unused ata_queued_cmd
4968 * @qc: Command to complete
4969 *
4970 * Designed to free unused ata_queued_cmd object
4971 * in case something prevents using it.
4972 *
4973 * LOCKING:
4974 * spin_lock_irqsave(host lock)
4975 */
4976void ata_qc_free(struct ata_queued_cmd *qc)
4977{
Julia Lawalla1104012009-10-17 08:41:47 +02004978 struct ata_port *ap;
Tejun Heo8a8bc222008-11-10 14:48:21 +09004979 unsigned int tag;
4980
Tejun Heoefcb3cf2009-01-09 19:19:14 +09004981 WARN_ON_ONCE(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
Julia Lawalla1104012009-10-17 08:41:47 +02004982 ap = qc->ap;
Tejun Heo8a8bc222008-11-10 14:48:21 +09004983
4984 qc->flags = 0;
4985 tag = qc->tag;
4986 if (likely(ata_tag_valid(tag))) {
4987 qc->tag = ATA_TAG_POISON;
4988 clear_bit(tag, &ap->qc_allocated);
4989 }
4990}
4991
Tejun Heo76014422006-02-11 15:13:49 +09004992void __ata_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993{
Julia Lawalla1104012009-10-17 08:41:47 +02004994 struct ata_port *ap;
4995 struct ata_link *link;
Tejun Heodedaf2b2006-05-15 21:03:43 +09004996
Tejun Heoefcb3cf2009-01-09 19:19:14 +09004997 WARN_ON_ONCE(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
4998 WARN_ON_ONCE(!(qc->flags & ATA_QCFLAG_ACTIVE));
Julia Lawalla1104012009-10-17 08:41:47 +02004999 ap = qc->ap;
5000 link = qc->dev->link;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001
5002 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
5003 ata_sg_clean(qc);
5004
Tejun Heo7401abf2006-05-15 20:57:32 +09005005 /* command should be marked inactive atomically with qc completion */
Tejun Heoda917d62007-09-23 13:14:12 +09005006 if (qc->tf.protocol == ATA_PROT_NCQ) {
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005007 link->sactive &= ~(1 << qc->tag);
Tejun Heoda917d62007-09-23 13:14:12 +09005008 if (!link->sactive)
5009 ap->nr_active_links--;
5010 } else {
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005011 link->active_tag = ATA_TAG_POISON;
Tejun Heoda917d62007-09-23 13:14:12 +09005012 ap->nr_active_links--;
5013 }
5014
5015 /* clear exclusive status */
5016 if (unlikely(qc->flags & ATA_QCFLAG_CLEAR_EXCL &&
5017 ap->excl_link == link))
5018 ap->excl_link = NULL;
Tejun Heo7401abf2006-05-15 20:57:32 +09005019
Albert Lee3f3791d2005-08-16 14:25:38 +08005020 /* atapi: mark qc as inactive to prevent the interrupt handler
5021 * from completing the command twice later, before the error handler
5022 * is called. (when rc != 0 and atapi request sense is needed)
5023 */
5024 qc->flags &= ~ATA_QCFLAG_ACTIVE;
Tejun Heodedaf2b2006-05-15 21:03:43 +09005025 ap->qc_active &= ~(1 << qc->tag);
Albert Lee3f3791d2005-08-16 14:25:38 +08005026
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027 /* call completion callback */
Tejun Heo77853bf2006-01-23 13:09:36 +09005028 qc->complete_fn(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005029}
5030
Tejun Heo39599a52006-11-14 22:37:35 +09005031static void fill_result_tf(struct ata_queued_cmd *qc)
5032{
5033 struct ata_port *ap = qc->ap;
5034
Tejun Heo39599a52006-11-14 22:37:35 +09005035 qc->result_tf.flags = qc->tf.flags;
Tejun Heo22183bf2008-04-07 22:47:20 +09005036 ap->ops->qc_fill_rtf(qc);
Tejun Heo39599a52006-11-14 22:37:35 +09005037}
5038
Tejun Heo00115e02007-11-27 19:28:58 +09005039static void ata_verify_xfer(struct ata_queued_cmd *qc)
5040{
5041 struct ata_device *dev = qc->dev;
5042
5043 if (ata_tag_internal(qc->tag))
5044 return;
5045
5046 if (ata_is_nodata(qc->tf.protocol))
5047 return;
5048
5049 if ((dev->mwdma_mask || dev->udma_mask) && ata_is_pio(qc->tf.protocol))
5050 return;
5051
5052 dev->flags &= ~ATA_DFLAG_DUBIOUS_XFER;
5053}
5054
Tejun Heof686bcb2006-05-15 20:58:05 +09005055/**
5056 * ata_qc_complete - Complete an active ATA command
5057 * @qc: Command to complete
Tejun Heof686bcb2006-05-15 20:58:05 +09005058 *
5059 * Indicate to the mid and upper layers that an ATA
5060 * command has completed, with either an ok or not-ok status.
5061 *
5062 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04005063 * spin_lock_irqsave(host lock)
Tejun Heof686bcb2006-05-15 20:58:05 +09005064 */
5065void ata_qc_complete(struct ata_queued_cmd *qc)
5066{
5067 struct ata_port *ap = qc->ap;
5068
5069 /* XXX: New EH and old EH use different mechanisms to
5070 * synchronize EH with regular execution path.
5071 *
5072 * In new EH, a failed qc is marked with ATA_QCFLAG_FAILED.
5073 * Normal execution path is responsible for not accessing a
5074 * failed qc. libata core enforces the rule by returning NULL
5075 * from ata_qc_from_tag() for failed qcs.
5076 *
5077 * Old EH depends on ata_qc_complete() nullifying completion
5078 * requests if ATA_QCFLAG_EH_SCHEDULED is set. Old EH does
5079 * not synchronize with interrupt handler. Only PIO task is
5080 * taken care of.
5081 */
5082 if (ap->ops->error_handler) {
Tejun Heo4dbfa392007-10-25 18:22:44 +09005083 struct ata_device *dev = qc->dev;
5084 struct ata_eh_info *ehi = &dev->link->eh_info;
5085
Tejun Heof686bcb2006-05-15 20:58:05 +09005086 if (unlikely(qc->err_mask))
5087 qc->flags |= ATA_QCFLAG_FAILED;
5088
5089 if (unlikely(qc->flags & ATA_QCFLAG_FAILED)) {
Tejun Heof4b31db92009-10-16 13:00:51 +09005090 /* always fill result TF for failed qc */
5091 fill_result_tf(qc);
5092
5093 if (!ata_tag_internal(qc->tag))
Tejun Heof686bcb2006-05-15 20:58:05 +09005094 ata_qc_schedule_eh(qc);
Tejun Heof4b31db92009-10-16 13:00:51 +09005095 else
5096 __ata_qc_complete(qc);
5097 return;
Tejun Heof686bcb2006-05-15 20:58:05 +09005098 }
5099
Tejun Heo4dc738e2009-09-17 18:45:27 +09005100 WARN_ON_ONCE(ap->pflags & ATA_PFLAG_FROZEN);
5101
Tejun Heof686bcb2006-05-15 20:58:05 +09005102 /* read result TF if requested */
5103 if (qc->flags & ATA_QCFLAG_RESULT_TF)
Tejun Heo39599a52006-11-14 22:37:35 +09005104 fill_result_tf(qc);
Tejun Heof686bcb2006-05-15 20:58:05 +09005105
Tejun Heo4dbfa392007-10-25 18:22:44 +09005106 /* Some commands need post-processing after successful
5107 * completion.
5108 */
5109 switch (qc->tf.command) {
5110 case ATA_CMD_SET_FEATURES:
5111 if (qc->tf.feature != SETFEATURES_WC_ON &&
5112 qc->tf.feature != SETFEATURES_WC_OFF)
5113 break;
5114 /* fall through */
5115 case ATA_CMD_INIT_DEV_PARAMS: /* CHS translation changed */
5116 case ATA_CMD_SET_MULTI: /* multi_count changed */
5117 /* revalidate device */
5118 ehi->dev_action[dev->devno] |= ATA_EH_REVALIDATE;
5119 ata_port_schedule_eh(ap);
5120 break;
Tejun Heo054a5fb2007-10-25 18:30:36 +09005121
5122 case ATA_CMD_SLEEP:
5123 dev->flags |= ATA_DFLAG_SLEEPING;
5124 break;
Tejun Heo4dbfa392007-10-25 18:22:44 +09005125 }
5126
Tejun Heo00115e02007-11-27 19:28:58 +09005127 if (unlikely(dev->flags & ATA_DFLAG_DUBIOUS_XFER))
5128 ata_verify_xfer(qc);
5129
Tejun Heof686bcb2006-05-15 20:58:05 +09005130 __ata_qc_complete(qc);
5131 } else {
5132 if (qc->flags & ATA_QCFLAG_EH_SCHEDULED)
5133 return;
5134
5135 /* read result TF if failed or requested */
5136 if (qc->err_mask || qc->flags & ATA_QCFLAG_RESULT_TF)
Tejun Heo39599a52006-11-14 22:37:35 +09005137 fill_result_tf(qc);
Tejun Heof686bcb2006-05-15 20:58:05 +09005138
5139 __ata_qc_complete(qc);
5140 }
5141}
5142
Tejun Heodedaf2b2006-05-15 21:03:43 +09005143/**
5144 * ata_qc_complete_multiple - Complete multiple qcs successfully
5145 * @ap: port in question
5146 * @qc_active: new qc_active mask
Tejun Heodedaf2b2006-05-15 21:03:43 +09005147 *
5148 * Complete in-flight commands. This functions is meant to be
5149 * called from low-level driver's interrupt routine to complete
5150 * requests normally. ap->qc_active and @qc_active is compared
5151 * and commands are completed accordingly.
5152 *
5153 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04005154 * spin_lock_irqsave(host lock)
Tejun Heodedaf2b2006-05-15 21:03:43 +09005155 *
5156 * RETURNS:
5157 * Number of completed commands on success, -errno otherwise.
5158 */
Tejun Heo79f97da2008-04-07 22:47:20 +09005159int ata_qc_complete_multiple(struct ata_port *ap, u32 qc_active)
Tejun Heodedaf2b2006-05-15 21:03:43 +09005160{
5161 int nr_done = 0;
5162 u32 done_mask;
Tejun Heodedaf2b2006-05-15 21:03:43 +09005163
5164 done_mask = ap->qc_active ^ qc_active;
5165
5166 if (unlikely(done_mask & qc_active)) {
5167 ata_port_printk(ap, KERN_ERR, "illegal qc_active transition "
5168 "(%08x->%08x)\n", ap->qc_active, qc_active);
5169 return -EINVAL;
5170 }
5171
Jens Axboe43768182009-05-20 09:44:39 +02005172 while (done_mask) {
Tejun Heodedaf2b2006-05-15 21:03:43 +09005173 struct ata_queued_cmd *qc;
Jens Axboe43768182009-05-20 09:44:39 +02005174 unsigned int tag = __ffs(done_mask);
Tejun Heodedaf2b2006-05-15 21:03:43 +09005175
Jens Axboe43768182009-05-20 09:44:39 +02005176 qc = ata_qc_from_tag(ap, tag);
5177 if (qc) {
Tejun Heodedaf2b2006-05-15 21:03:43 +09005178 ata_qc_complete(qc);
5179 nr_done++;
5180 }
Jens Axboe43768182009-05-20 09:44:39 +02005181 done_mask &= ~(1 << tag);
Tejun Heodedaf2b2006-05-15 21:03:43 +09005182 }
5183
5184 return nr_done;
5185}
5186
Linus Torvalds1da177e2005-04-16 15:20:36 -07005187/**
5188 * ata_qc_issue - issue taskfile to device
5189 * @qc: command to issue to device
5190 *
5191 * Prepare an ATA command to submission to device.
5192 * This includes mapping the data into a DMA-able
5193 * area, filling in the S/G table, and finally
5194 * writing the taskfile to hardware, starting the command.
5195 *
5196 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04005197 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005198 */
Tejun Heo8e0e6942006-03-31 20:41:11 +09005199void ata_qc_issue(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005200{
5201 struct ata_port *ap = qc->ap;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005202 struct ata_link *link = qc->dev->link;
Tejun Heo405e66b2007-11-27 19:28:53 +09005203 u8 prot = qc->tf.protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005204
Tejun Heodedaf2b2006-05-15 21:03:43 +09005205 /* Make sure only one non-NCQ command is outstanding. The
5206 * check is skipped for old EH because it reuses active qc to
5207 * request ATAPI sense.
5208 */
Tejun Heoefcb3cf2009-01-09 19:19:14 +09005209 WARN_ON_ONCE(ap->ops->error_handler && ata_tag_valid(link->active_tag));
Tejun Heodedaf2b2006-05-15 21:03:43 +09005210
Tejun Heo1973a022007-12-05 10:36:13 +09005211 if (ata_is_ncq(prot)) {
Tejun Heoefcb3cf2009-01-09 19:19:14 +09005212 WARN_ON_ONCE(link->sactive & (1 << qc->tag));
Tejun Heoda917d62007-09-23 13:14:12 +09005213
5214 if (!link->sactive)
5215 ap->nr_active_links++;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005216 link->sactive |= 1 << qc->tag;
Tejun Heodedaf2b2006-05-15 21:03:43 +09005217 } else {
Tejun Heoefcb3cf2009-01-09 19:19:14 +09005218 WARN_ON_ONCE(link->sactive);
Tejun Heoda917d62007-09-23 13:14:12 +09005219
5220 ap->nr_active_links++;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005221 link->active_tag = qc->tag;
Tejun Heodedaf2b2006-05-15 21:03:43 +09005222 }
5223
Tejun Heoe4a70e72006-03-31 20:36:47 +09005224 qc->flags |= ATA_QCFLAG_ACTIVE;
Tejun Heodedaf2b2006-05-15 21:03:43 +09005225 ap->qc_active |= 1 << qc->tag;
Tejun Heoe4a70e72006-03-31 20:36:47 +09005226
Tejun Heof92a2632007-12-05 16:43:10 +09005227 /* We guarantee to LLDs that they will have at least one
5228 * non-zero sg if the command is a data command.
5229 */
Tejun Heoff2aeb12007-12-05 16:43:11 +09005230 BUG_ON(ata_is_data(prot) && (!qc->sg || !qc->n_elem || !qc->nbytes));
Tejun Heof92a2632007-12-05 16:43:10 +09005231
Tejun Heo405e66b2007-11-27 19:28:53 +09005232 if (ata_is_dma(prot) || (ata_is_pio(prot) &&
Tejun Heof92a2632007-12-05 16:43:10 +09005233 (ap->flags & ATA_FLAG_PIO_DMA)))
Tejun Heo001102d2007-12-05 16:43:09 +09005234 if (ata_sg_setup(qc))
5235 goto sg_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005236
Tejun Heocf480622008-01-24 00:05:14 +09005237 /* if device is sleeping, schedule reset and abort the link */
Tejun Heo054a5fb2007-10-25 18:30:36 +09005238 if (unlikely(qc->dev->flags & ATA_DFLAG_SLEEPING)) {
Tejun Heocf480622008-01-24 00:05:14 +09005239 link->eh_info.action |= ATA_EH_RESET;
Tejun Heo054a5fb2007-10-25 18:30:36 +09005240 ata_ehi_push_desc(&link->eh_info, "waking up from sleep");
5241 ata_link_abort(link);
5242 return;
5243 }
5244
Linus Torvalds1da177e2005-04-16 15:20:36 -07005245 ap->ops->qc_prep(qc);
5246
Tejun Heo8e0e6942006-03-31 20:41:11 +09005247 qc->err_mask |= ap->ops->qc_issue(qc);
5248 if (unlikely(qc->err_mask))
5249 goto err;
5250 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005251
Tejun Heo8e436af2006-01-23 13:09:36 +09005252sg_err:
Tejun Heo8e0e6942006-03-31 20:41:11 +09005253 qc->err_mask |= AC_ERR_SYSTEM;
5254err:
5255 ata_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005256}
5257
5258/**
Tejun Heo34bf2172006-05-15 20:57:46 +09005259 * sata_scr_valid - test whether SCRs are accessible
Tejun Heo936fd732007-08-06 18:36:23 +09005260 * @link: ATA link to test SCR accessibility for
Tejun Heo34bf2172006-05-15 20:57:46 +09005261 *
Tejun Heo936fd732007-08-06 18:36:23 +09005262 * Test whether SCRs are accessible for @link.
Tejun Heo34bf2172006-05-15 20:57:46 +09005263 *
5264 * LOCKING:
5265 * None.
5266 *
5267 * RETURNS:
5268 * 1 if SCRs are accessible, 0 otherwise.
5269 */
Tejun Heo936fd732007-08-06 18:36:23 +09005270int sata_scr_valid(struct ata_link *link)
Tejun Heo34bf2172006-05-15 20:57:46 +09005271{
Tejun Heo936fd732007-08-06 18:36:23 +09005272 struct ata_port *ap = link->ap;
5273
Tejun Heoa16abc02007-05-21 18:33:47 +02005274 return (ap->flags & ATA_FLAG_SATA) && ap->ops->scr_read;
Tejun Heo34bf2172006-05-15 20:57:46 +09005275}
5276
5277/**
5278 * sata_scr_read - read SCR register of the specified port
Tejun Heo936fd732007-08-06 18:36:23 +09005279 * @link: ATA link to read SCR for
Tejun Heo34bf2172006-05-15 20:57:46 +09005280 * @reg: SCR to read
5281 * @val: Place to store read value
5282 *
Tejun Heo936fd732007-08-06 18:36:23 +09005283 * Read SCR register @reg of @link into *@val. This function is
Tejun Heo633273a2007-09-23 13:19:54 +09005284 * guaranteed to succeed if @link is ap->link, the cable type of
5285 * the port is SATA and the port implements ->scr_read.
Tejun Heo34bf2172006-05-15 20:57:46 +09005286 *
5287 * LOCKING:
Tejun Heo633273a2007-09-23 13:19:54 +09005288 * None if @link is ap->link. Kernel thread context otherwise.
Tejun Heo34bf2172006-05-15 20:57:46 +09005289 *
5290 * RETURNS:
5291 * 0 on success, negative errno on failure.
5292 */
Tejun Heo936fd732007-08-06 18:36:23 +09005293int sata_scr_read(struct ata_link *link, int reg, u32 *val)
Tejun Heo34bf2172006-05-15 20:57:46 +09005294{
Tejun Heo633273a2007-09-23 13:19:54 +09005295 if (ata_is_host_link(link)) {
Tejun Heo633273a2007-09-23 13:19:54 +09005296 if (sata_scr_valid(link))
Tejun Heo82ef04f2008-07-31 17:02:40 +09005297 return link->ap->ops->scr_read(link, reg, val);
Tejun Heo633273a2007-09-23 13:19:54 +09005298 return -EOPNOTSUPP;
5299 }
5300
5301 return sata_pmp_scr_read(link, reg, val);
Tejun Heo34bf2172006-05-15 20:57:46 +09005302}
5303
5304/**
5305 * sata_scr_write - write SCR register of the specified port
Tejun Heo936fd732007-08-06 18:36:23 +09005306 * @link: ATA link to write SCR for
Tejun Heo34bf2172006-05-15 20:57:46 +09005307 * @reg: SCR to write
5308 * @val: value to write
5309 *
Tejun Heo936fd732007-08-06 18:36:23 +09005310 * Write @val to SCR register @reg of @link. This function is
Tejun Heo633273a2007-09-23 13:19:54 +09005311 * guaranteed to succeed if @link is ap->link, the cable type of
5312 * the port is SATA and the port implements ->scr_read.
Tejun Heo34bf2172006-05-15 20:57:46 +09005313 *
5314 * LOCKING:
Tejun Heo633273a2007-09-23 13:19:54 +09005315 * None if @link is ap->link. Kernel thread context otherwise.
Tejun Heo34bf2172006-05-15 20:57:46 +09005316 *
5317 * RETURNS:
5318 * 0 on success, negative errno on failure.
5319 */
Tejun Heo936fd732007-08-06 18:36:23 +09005320int sata_scr_write(struct ata_link *link, int reg, u32 val)
Tejun Heo34bf2172006-05-15 20:57:46 +09005321{
Tejun Heo633273a2007-09-23 13:19:54 +09005322 if (ata_is_host_link(link)) {
Tejun Heo633273a2007-09-23 13:19:54 +09005323 if (sata_scr_valid(link))
Tejun Heo82ef04f2008-07-31 17:02:40 +09005324 return link->ap->ops->scr_write(link, reg, val);
Tejun Heo633273a2007-09-23 13:19:54 +09005325 return -EOPNOTSUPP;
5326 }
5327
5328 return sata_pmp_scr_write(link, reg, val);
Tejun Heo34bf2172006-05-15 20:57:46 +09005329}
5330
5331/**
5332 * sata_scr_write_flush - write SCR register of the specified port and flush
Tejun Heo936fd732007-08-06 18:36:23 +09005333 * @link: ATA link to write SCR for
Tejun Heo34bf2172006-05-15 20:57:46 +09005334 * @reg: SCR to write
5335 * @val: value to write
5336 *
5337 * This function is identical to sata_scr_write() except that this
5338 * function performs flush after writing to the register.
5339 *
5340 * LOCKING:
Tejun Heo633273a2007-09-23 13:19:54 +09005341 * None if @link is ap->link. Kernel thread context otherwise.
Tejun Heo34bf2172006-05-15 20:57:46 +09005342 *
5343 * RETURNS:
5344 * 0 on success, negative errno on failure.
5345 */
Tejun Heo936fd732007-08-06 18:36:23 +09005346int sata_scr_write_flush(struct ata_link *link, int reg, u32 val)
Tejun Heo34bf2172006-05-15 20:57:46 +09005347{
Tejun Heo633273a2007-09-23 13:19:54 +09005348 if (ata_is_host_link(link)) {
Tejun Heo633273a2007-09-23 13:19:54 +09005349 int rc;
Tejun Heoda3dbb12007-07-16 14:29:40 +09005350
Tejun Heo633273a2007-09-23 13:19:54 +09005351 if (sata_scr_valid(link)) {
Tejun Heo82ef04f2008-07-31 17:02:40 +09005352 rc = link->ap->ops->scr_write(link, reg, val);
Tejun Heo633273a2007-09-23 13:19:54 +09005353 if (rc == 0)
Tejun Heo82ef04f2008-07-31 17:02:40 +09005354 rc = link->ap->ops->scr_read(link, reg, &val);
Tejun Heo633273a2007-09-23 13:19:54 +09005355 return rc;
5356 }
5357 return -EOPNOTSUPP;
Tejun Heo34bf2172006-05-15 20:57:46 +09005358 }
Tejun Heo633273a2007-09-23 13:19:54 +09005359
5360 return sata_pmp_scr_write(link, reg, val);
Tejun Heo34bf2172006-05-15 20:57:46 +09005361}
5362
5363/**
Tejun Heob1c72912008-07-31 17:02:43 +09005364 * ata_phys_link_online - test whether the given link is online
Tejun Heo936fd732007-08-06 18:36:23 +09005365 * @link: ATA link to test
Tejun Heo34bf2172006-05-15 20:57:46 +09005366 *
Tejun Heo936fd732007-08-06 18:36:23 +09005367 * Test whether @link is online. Note that this function returns
5368 * 0 if online status of @link cannot be obtained, so
5369 * ata_link_online(link) != !ata_link_offline(link).
Tejun Heo34bf2172006-05-15 20:57:46 +09005370 *
5371 * LOCKING:
5372 * None.
5373 *
5374 * RETURNS:
Tejun Heob5b3fa32008-07-31 17:02:42 +09005375 * True if the port online status is available and online.
Tejun Heo34bf2172006-05-15 20:57:46 +09005376 */
Tejun Heob1c72912008-07-31 17:02:43 +09005377bool ata_phys_link_online(struct ata_link *link)
Tejun Heo34bf2172006-05-15 20:57:46 +09005378{
5379 u32 sstatus;
5380
Tejun Heo936fd732007-08-06 18:36:23 +09005381 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
Tejun Heo9913ff82009-01-29 20:31:31 +09005382 ata_sstatus_online(sstatus))
Tejun Heob5b3fa32008-07-31 17:02:42 +09005383 return true;
5384 return false;
Tejun Heo34bf2172006-05-15 20:57:46 +09005385}
5386
5387/**
Tejun Heob1c72912008-07-31 17:02:43 +09005388 * ata_phys_link_offline - test whether the given link is offline
Tejun Heo936fd732007-08-06 18:36:23 +09005389 * @link: ATA link to test
Tejun Heo34bf2172006-05-15 20:57:46 +09005390 *
Tejun Heo936fd732007-08-06 18:36:23 +09005391 * Test whether @link is offline. Note that this function
5392 * returns 0 if offline status of @link cannot be obtained, so
5393 * ata_link_online(link) != !ata_link_offline(link).
Tejun Heo34bf2172006-05-15 20:57:46 +09005394 *
5395 * LOCKING:
5396 * None.
5397 *
5398 * RETURNS:
Tejun Heob5b3fa32008-07-31 17:02:42 +09005399 * True if the port offline status is available and offline.
Tejun Heo34bf2172006-05-15 20:57:46 +09005400 */
Tejun Heob1c72912008-07-31 17:02:43 +09005401bool ata_phys_link_offline(struct ata_link *link)
Tejun Heo34bf2172006-05-15 20:57:46 +09005402{
5403 u32 sstatus;
5404
Tejun Heo936fd732007-08-06 18:36:23 +09005405 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
Tejun Heo9913ff82009-01-29 20:31:31 +09005406 !ata_sstatus_online(sstatus))
Tejun Heob5b3fa32008-07-31 17:02:42 +09005407 return true;
5408 return false;
Tejun Heo34bf2172006-05-15 20:57:46 +09005409}
Edward Falk0baab862005-06-02 18:17:13 -04005410
Tejun Heob1c72912008-07-31 17:02:43 +09005411/**
5412 * ata_link_online - test whether the given link is online
5413 * @link: ATA link to test
5414 *
5415 * Test whether @link is online. This is identical to
5416 * ata_phys_link_online() when there's no slave link. When
5417 * there's a slave link, this function should only be called on
5418 * the master link and will return true if any of M/S links is
5419 * online.
5420 *
5421 * LOCKING:
5422 * None.
5423 *
5424 * RETURNS:
5425 * True if the port online status is available and online.
5426 */
5427bool ata_link_online(struct ata_link *link)
5428{
5429 struct ata_link *slave = link->ap->slave_link;
5430
5431 WARN_ON(link == slave); /* shouldn't be called on slave link */
5432
5433 return ata_phys_link_online(link) ||
5434 (slave && ata_phys_link_online(slave));
5435}
5436
5437/**
5438 * ata_link_offline - test whether the given link is offline
5439 * @link: ATA link to test
5440 *
5441 * Test whether @link is offline. This is identical to
5442 * ata_phys_link_offline() when there's no slave link. When
5443 * there's a slave link, this function should only be called on
5444 * the master link and will return true if both M/S links are
5445 * offline.
5446 *
5447 * LOCKING:
5448 * None.
5449 *
5450 * RETURNS:
5451 * True if the port offline status is available and offline.
5452 */
5453bool ata_link_offline(struct ata_link *link)
5454{
5455 struct ata_link *slave = link->ap->slave_link;
5456
5457 WARN_ON(link == slave); /* shouldn't be called on slave link */
5458
5459 return ata_phys_link_offline(link) &&
5460 (!slave || ata_phys_link_offline(slave));
5461}
5462
Tejun Heo6ffa01d2007-03-02 17:32:47 +09005463#ifdef CONFIG_PM
Jeff Garzikcca39742006-08-24 03:19:22 -04005464static int ata_host_request_pm(struct ata_host *host, pm_message_t mesg,
5465 unsigned int action, unsigned int ehi_flags,
5466 int wait)
Tejun Heo500530f2006-07-03 16:07:27 +09005467{
5468 unsigned long flags;
5469 int i, rc;
5470
Jeff Garzikcca39742006-08-24 03:19:22 -04005471 for (i = 0; i < host->n_ports; i++) {
5472 struct ata_port *ap = host->ports[i];
Tejun Heoe3667eb2007-08-06 18:36:24 +09005473 struct ata_link *link;
Tejun Heo500530f2006-07-03 16:07:27 +09005474
5475 /* Previous resume operation might still be in
5476 * progress. Wait for PM_PENDING to clear.
5477 */
5478 if (ap->pflags & ATA_PFLAG_PM_PENDING) {
5479 ata_port_wait_eh(ap);
5480 WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
5481 }
5482
5483 /* request PM ops to EH */
5484 spin_lock_irqsave(ap->lock, flags);
5485
5486 ap->pm_mesg = mesg;
5487 if (wait) {
5488 rc = 0;
5489 ap->pm_result = &rc;
5490 }
5491
5492 ap->pflags |= ATA_PFLAG_PM_PENDING;
Tejun Heo1eca4362008-11-03 20:03:17 +09005493 ata_for_each_link(link, ap, HOST_FIRST) {
Tejun Heoe3667eb2007-08-06 18:36:24 +09005494 link->eh_info.action |= action;
5495 link->eh_info.flags |= ehi_flags;
5496 }
Tejun Heo500530f2006-07-03 16:07:27 +09005497
5498 ata_port_schedule_eh(ap);
5499
5500 spin_unlock_irqrestore(ap->lock, flags);
5501
5502 /* wait and check result */
5503 if (wait) {
5504 ata_port_wait_eh(ap);
5505 WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
5506 if (rc)
5507 return rc;
5508 }
5509 }
5510
5511 return 0;
5512}
5513
5514/**
Jeff Garzikcca39742006-08-24 03:19:22 -04005515 * ata_host_suspend - suspend host
5516 * @host: host to suspend
Tejun Heo500530f2006-07-03 16:07:27 +09005517 * @mesg: PM message
5518 *
Jeff Garzikcca39742006-08-24 03:19:22 -04005519 * Suspend @host. Actual operation is performed by EH. This
Tejun Heo500530f2006-07-03 16:07:27 +09005520 * function requests EH to perform PM operations and waits for EH
5521 * to finish.
5522 *
5523 * LOCKING:
5524 * Kernel thread context (may sleep).
5525 *
5526 * RETURNS:
5527 * 0 on success, -errno on failure.
5528 */
Jeff Garzikcca39742006-08-24 03:19:22 -04005529int ata_host_suspend(struct ata_host *host, pm_message_t mesg)
Tejun Heo500530f2006-07-03 16:07:27 +09005530{
Tejun Heo9666f402007-05-04 21:27:47 +02005531 int rc;
Tejun Heo500530f2006-07-03 16:07:27 +09005532
Kristen Carlson Accardica773292007-10-25 00:58:59 -04005533 /*
5534 * disable link pm on all ports before requesting
5535 * any pm activity
5536 */
5537 ata_lpm_enable(host);
5538
Jeff Garzikcca39742006-08-24 03:19:22 -04005539 rc = ata_host_request_pm(host, mesg, 0, ATA_EHI_QUIET, 1);
Jeff Garzik72ad6ec2008-02-25 17:31:10 -05005540 if (rc == 0)
5541 host->dev->power.power_state = mesg;
Tejun Heo500530f2006-07-03 16:07:27 +09005542 return rc;
5543}
5544
5545/**
Jeff Garzikcca39742006-08-24 03:19:22 -04005546 * ata_host_resume - resume host
5547 * @host: host to resume
Tejun Heo500530f2006-07-03 16:07:27 +09005548 *
Jeff Garzikcca39742006-08-24 03:19:22 -04005549 * Resume @host. Actual operation is performed by EH. This
Tejun Heo500530f2006-07-03 16:07:27 +09005550 * function requests EH to perform PM operations and returns.
5551 * Note that all resume operations are performed parallely.
5552 *
5553 * LOCKING:
5554 * Kernel thread context (may sleep).
5555 */
Jeff Garzikcca39742006-08-24 03:19:22 -04005556void ata_host_resume(struct ata_host *host)
Tejun Heo500530f2006-07-03 16:07:27 +09005557{
Tejun Heocf480622008-01-24 00:05:14 +09005558 ata_host_request_pm(host, PMSG_ON, ATA_EH_RESET,
Jeff Garzikcca39742006-08-24 03:19:22 -04005559 ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET, 0);
Jeff Garzik72ad6ec2008-02-25 17:31:10 -05005560 host->dev->power.power_state = PMSG_ON;
Kristen Carlson Accardica773292007-10-25 00:58:59 -04005561
5562 /* reenable link pm */
5563 ata_lpm_disable(host);
Tejun Heo500530f2006-07-03 16:07:27 +09005564}
Tejun Heo6ffa01d2007-03-02 17:32:47 +09005565#endif
Tejun Heo500530f2006-07-03 16:07:27 +09005566
Randy Dunlapc893a3a2006-01-28 13:15:32 -05005567/**
5568 * ata_port_start - Set port up for dma.
5569 * @ap: Port to initialize
5570 *
5571 * Called just after data structures for each port are
5572 * initialized. Allocates space for PRD table.
5573 *
5574 * May be used as the port_start() entry in ata_port_operations.
5575 *
5576 * LOCKING:
5577 * Inherited from caller.
5578 */
Tejun Heof0d36ef2007-01-20 16:00:28 +09005579int ata_port_start(struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005580{
Brian King2f1f6102006-03-23 17:30:15 -06005581 struct device *dev = ap->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005582
Tejun Heof0d36ef2007-01-20 16:00:28 +09005583 ap->prd = dmam_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma,
5584 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005585 if (!ap->prd)
5586 return -ENOMEM;
5587
Linus Torvalds1da177e2005-04-16 15:20:36 -07005588 return 0;
5589}
5590
Edward Falk0baab862005-06-02 18:17:13 -04005591/**
Tejun Heo3ef3b432006-05-31 18:27:30 +09005592 * ata_dev_init - Initialize an ata_device structure
5593 * @dev: Device structure to initialize
5594 *
5595 * Initialize @dev in preparation for probing.
5596 *
5597 * LOCKING:
5598 * Inherited from caller.
5599 */
5600void ata_dev_init(struct ata_device *dev)
5601{
Tejun Heob1c72912008-07-31 17:02:43 +09005602 struct ata_link *link = ata_dev_phys_link(dev);
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005603 struct ata_port *ap = link->ap;
Tejun Heo72fa4b72006-05-31 18:27:32 +09005604 unsigned long flags;
Tejun Heo3ef3b432006-05-31 18:27:30 +09005605
Tejun Heob1c72912008-07-31 17:02:43 +09005606 /* SATA spd limit is bound to the attached device, reset together */
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005607 link->sata_spd_limit = link->hw_sata_spd_limit;
5608 link->sata_spd = 0;
Tejun Heo5a04bf42006-05-31 18:27:38 +09005609
Tejun Heo72fa4b72006-05-31 18:27:32 +09005610 /* High bits of dev->flags are used to record warm plug
5611 * requests which occur asynchronously. Synchronize using
Jeff Garzikcca39742006-08-24 03:19:22 -04005612 * host lock.
Tejun Heo72fa4b72006-05-31 18:27:32 +09005613 */
Jeff Garzikba6a1302006-06-22 23:46:10 -04005614 spin_lock_irqsave(ap->lock, flags);
Tejun Heo72fa4b72006-05-31 18:27:32 +09005615 dev->flags &= ~ATA_DFLAG_INIT_MASK;
Tejun Heo3dcc3232007-09-03 12:20:11 +09005616 dev->horkage = 0;
Jeff Garzikba6a1302006-06-22 23:46:10 -04005617 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo72fa4b72006-05-31 18:27:32 +09005618
Tejun Heo99cf6102009-01-29 20:31:32 +09005619 memset((void *)dev + ATA_DEVICE_CLEAR_BEGIN, 0,
5620 ATA_DEVICE_CLEAR_END - ATA_DEVICE_CLEAR_BEGIN);
Tejun Heo3ef3b432006-05-31 18:27:30 +09005621 dev->pio_mask = UINT_MAX;
5622 dev->mwdma_mask = UINT_MAX;
5623 dev->udma_mask = UINT_MAX;
5624}
5625
5626/**
Tejun Heo4fb37a22007-08-06 18:36:23 +09005627 * ata_link_init - Initialize an ata_link structure
5628 * @ap: ATA port link is attached to
5629 * @link: Link structure to initialize
Tejun Heo89898052007-08-06 18:36:23 +09005630 * @pmp: Port multiplier port number
Tejun Heo4fb37a22007-08-06 18:36:23 +09005631 *
5632 * Initialize @link.
5633 *
5634 * LOCKING:
5635 * Kernel thread context (may sleep)
5636 */
Tejun Heofb7fd612007-09-23 13:14:12 +09005637void ata_link_init(struct ata_port *ap, struct ata_link *link, int pmp)
Tejun Heo4fb37a22007-08-06 18:36:23 +09005638{
5639 int i;
5640
5641 /* clear everything except for devices */
5642 memset(link, 0, offsetof(struct ata_link, device[0]));
5643
5644 link->ap = ap;
Tejun Heo89898052007-08-06 18:36:23 +09005645 link->pmp = pmp;
Tejun Heo4fb37a22007-08-06 18:36:23 +09005646 link->active_tag = ATA_TAG_POISON;
5647 link->hw_sata_spd_limit = UINT_MAX;
5648
5649 /* can't use iterator, ap isn't initialized yet */
5650 for (i = 0; i < ATA_MAX_DEVICES; i++) {
5651 struct ata_device *dev = &link->device[i];
5652
5653 dev->link = link;
5654 dev->devno = dev - link->device;
Tejun Heo110f66d2009-09-16 04:17:28 +09005655#ifdef CONFIG_ATA_ACPI
5656 dev->gtf_filter = ata_acpi_gtf_filter;
5657#endif
Tejun Heo4fb37a22007-08-06 18:36:23 +09005658 ata_dev_init(dev);
5659 }
5660}
5661
5662/**
5663 * sata_link_init_spd - Initialize link->sata_spd_limit
5664 * @link: Link to configure sata_spd_limit for
5665 *
5666 * Initialize @link->[hw_]sata_spd_limit to the currently
5667 * configured value.
5668 *
5669 * LOCKING:
5670 * Kernel thread context (may sleep).
5671 *
5672 * RETURNS:
5673 * 0 on success, -errno on failure.
5674 */
Tejun Heofb7fd612007-09-23 13:14:12 +09005675int sata_link_init_spd(struct ata_link *link)
Tejun Heo4fb37a22007-08-06 18:36:23 +09005676{
Tejun Heo33267322008-02-13 09:15:09 +09005677 u8 spd;
Tejun Heo4fb37a22007-08-06 18:36:23 +09005678 int rc;
5679
Tejun Heod127ea72008-07-31 16:09:34 +09005680 rc = sata_scr_read(link, SCR_CONTROL, &link->saved_scontrol);
Tejun Heo4fb37a22007-08-06 18:36:23 +09005681 if (rc)
5682 return rc;
5683
Tejun Heod127ea72008-07-31 16:09:34 +09005684 spd = (link->saved_scontrol >> 4) & 0xf;
Tejun Heo4fb37a22007-08-06 18:36:23 +09005685 if (spd)
5686 link->hw_sata_spd_limit &= (1 << spd) - 1;
5687
Tejun Heo05944bd2008-08-13 20:19:09 +09005688 ata_force_link_limits(link);
Tejun Heo33267322008-02-13 09:15:09 +09005689
Tejun Heo4fb37a22007-08-06 18:36:23 +09005690 link->sata_spd_limit = link->hw_sata_spd_limit;
5691
5692 return 0;
5693}
5694
5695/**
Tejun Heof3187192007-04-17 23:44:07 +09005696 * ata_port_alloc - allocate and initialize basic ATA port resources
5697 * @host: ATA host this allocated port belongs to
Linus Torvalds1da177e2005-04-16 15:20:36 -07005698 *
Tejun Heof3187192007-04-17 23:44:07 +09005699 * Allocate and initialize basic ATA port resources.
5700 *
5701 * RETURNS:
5702 * Allocate ATA port on success, NULL on failure.
Jeff Garzik0cba6322005-05-30 19:49:12 -04005703 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704 * LOCKING:
Tejun Heof3187192007-04-17 23:44:07 +09005705 * Inherited from calling layer (may sleep).
Linus Torvalds1da177e2005-04-16 15:20:36 -07005706 */
Tejun Heof3187192007-04-17 23:44:07 +09005707struct ata_port *ata_port_alloc(struct ata_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005708{
Tejun Heof3187192007-04-17 23:44:07 +09005709 struct ata_port *ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005710
Tejun Heof3187192007-04-17 23:44:07 +09005711 DPRINTK("ENTER\n");
5712
5713 ap = kzalloc(sizeof(*ap), GFP_KERNEL);
5714 if (!ap)
5715 return NULL;
5716
Tejun Heof4d6d002007-05-01 11:50:15 +02005717 ap->pflags |= ATA_PFLAG_INITIALIZING;
Jeff Garzikcca39742006-08-24 03:19:22 -04005718 ap->lock = &host->lock;
Tejun Heo198e0fe2006-04-02 18:51:52 +09005719 ap->flags = ATA_FLAG_DISABLED;
Tejun Heof3187192007-04-17 23:44:07 +09005720 ap->print_id = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005721 ap->ctl = ATA_DEVCTL_OBS;
Jeff Garzikcca39742006-08-24 03:19:22 -04005722 ap->host = host;
Tejun Heof3187192007-04-17 23:44:07 +09005723 ap->dev = host->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005724 ap->last_ctl = 0xFF;
Borislav Petkovbd5d8252006-06-11 23:17:01 -04005725
5726#if defined(ATA_VERBOSE_DEBUG)
5727 /* turn on all debugging levels */
5728 ap->msg_enable = 0x00FF;
5729#elif defined(ATA_DEBUG)
5730 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 +09005731#else
Borislav Petkov0dd4b212006-06-23 02:29:08 -04005732 ap->msg_enable = ATA_MSG_DRV | ATA_MSG_ERR | ATA_MSG_WARN;
Borislav Petkovbd5d8252006-06-11 23:17:01 -04005733#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005734
Tejun Heo127102a2008-04-07 22:47:21 +09005735#ifdef CONFIG_ATA_SFF
Jeff Garzik442eacc2007-12-19 04:25:10 -05005736 INIT_DELAYED_WORK(&ap->port_task, ata_pio_task);
Tejun Heof667fdb2008-10-20 13:11:56 +09005737#else
5738 INIT_DELAYED_WORK(&ap->port_task, NULL);
Tejun Heo127102a2008-04-07 22:47:21 +09005739#endif
David Howells65f27f32006-11-22 14:55:48 +00005740 INIT_DELAYED_WORK(&ap->hotplug_task, ata_scsi_hotplug);
5741 INIT_WORK(&ap->scsi_rescan_task, ata_scsi_dev_rescan);
Tejun Heoa72ec4c2006-01-23 13:09:37 +09005742 INIT_LIST_HEAD(&ap->eh_done_q);
Tejun Heoc6cf9e92006-05-31 18:27:27 +09005743 init_waitqueue_head(&ap->eh_wait_q);
Elias Oltmanns45fabbb2008-09-21 11:54:08 +02005744 init_completion(&ap->park_req_pending);
Tejun Heo5ddf24c2007-07-16 14:29:41 +09005745 init_timer_deferrable(&ap->fastdrain_timer);
5746 ap->fastdrain_timer.function = ata_eh_fastdrain_timerfn;
5747 ap->fastdrain_timer.data = (unsigned long)ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005748
Tejun Heo838df622006-05-15 20:57:44 +09005749 ap->cbl = ATA_CBL_NONE;
Tejun Heo838df622006-05-15 20:57:44 +09005750
Tejun Heo89898052007-08-06 18:36:23 +09005751 ata_link_init(ap, &ap->link, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005752
5753#ifdef ATA_IRQ_TRAP
5754 ap->stats.unhandled_irq = 1;
5755 ap->stats.idle_irq = 1;
5756#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005757 return ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005758}
5759
Tejun Heof0d36ef2007-01-20 16:00:28 +09005760static void ata_host_release(struct device *gendev, void *res)
5761{
5762 struct ata_host *host = dev_get_drvdata(gendev);
5763 int i;
5764
5765 for (i = 0; i < host->n_ports; i++) {
5766 struct ata_port *ap = host->ports[i];
5767
Tejun Heoecef7252007-04-17 23:44:06 +09005768 if (!ap)
5769 continue;
5770
Tejun Heo49114872007-04-17 23:44:06 +09005771 if (ap->scsi_host)
Tejun Heo1aa506e2007-03-09 19:36:12 +09005772 scsi_host_put(ap->scsi_host);
5773
Tejun Heo633273a2007-09-23 13:19:54 +09005774 kfree(ap->pmp_link);
Tejun Heob1c72912008-07-31 17:02:43 +09005775 kfree(ap->slave_link);
Tejun Heo49114872007-04-17 23:44:06 +09005776 kfree(ap);
Tejun Heo1aa506e2007-03-09 19:36:12 +09005777 host->ports[i] = NULL;
5778 }
5779
Tejun Heo1aa56cc2007-02-27 22:33:21 +09005780 dev_set_drvdata(gendev, NULL);
Tejun Heof0d36ef2007-01-20 16:00:28 +09005781}
5782
Linus Torvalds1da177e2005-04-16 15:20:36 -07005783/**
Tejun Heof3187192007-04-17 23:44:07 +09005784 * ata_host_alloc - allocate and init basic ATA host resources
5785 * @dev: generic device this host is associated with
5786 * @max_ports: maximum number of ATA ports associated with this host
5787 *
5788 * Allocate and initialize basic ATA host resources. LLD calls
5789 * this function to allocate a host, initializes it fully and
5790 * attaches it using ata_host_register().
5791 *
5792 * @max_ports ports are allocated and host->n_ports is
5793 * initialized to @max_ports. The caller is allowed to decrease
5794 * host->n_ports before calling ata_host_register(). The unused
5795 * ports will be automatically freed on registration.
5796 *
5797 * RETURNS:
5798 * Allocate ATA host on success, NULL on failure.
5799 *
5800 * LOCKING:
5801 * Inherited from calling layer (may sleep).
5802 */
5803struct ata_host *ata_host_alloc(struct device *dev, int max_ports)
5804{
5805 struct ata_host *host;
5806 size_t sz;
5807 int i;
5808
5809 DPRINTK("ENTER\n");
5810
5811 if (!devres_open_group(dev, NULL, GFP_KERNEL))
5812 return NULL;
5813
5814 /* alloc a container for our list of ATA ports (buses) */
5815 sz = sizeof(struct ata_host) + (max_ports + 1) * sizeof(void *);
5816 /* alloc a container for our list of ATA ports (buses) */
5817 host = devres_alloc(ata_host_release, sz, GFP_KERNEL);
5818 if (!host)
5819 goto err_out;
5820
5821 devres_add(dev, host);
5822 dev_set_drvdata(dev, host);
5823
5824 spin_lock_init(&host->lock);
5825 host->dev = dev;
5826 host->n_ports = max_ports;
5827
5828 /* allocate ports bound to this host */
5829 for (i = 0; i < max_ports; i++) {
5830 struct ata_port *ap;
5831
5832 ap = ata_port_alloc(host);
5833 if (!ap)
5834 goto err_out;
5835
5836 ap->port_no = i;
5837 host->ports[i] = ap;
5838 }
5839
5840 devres_remove_group(dev, NULL);
5841 return host;
5842
5843 err_out:
5844 devres_release_group(dev, NULL);
5845 return NULL;
5846}
5847
5848/**
Tejun Heof5cda252007-04-17 23:44:07 +09005849 * ata_host_alloc_pinfo - alloc host and init with port_info array
5850 * @dev: generic device this host is associated with
5851 * @ppi: array of ATA port_info to initialize host with
5852 * @n_ports: number of ATA ports attached to this host
5853 *
5854 * Allocate ATA host and initialize with info from @ppi. If NULL
5855 * terminated, @ppi may contain fewer entries than @n_ports. The
5856 * last entry will be used for the remaining ports.
5857 *
5858 * RETURNS:
5859 * Allocate ATA host on success, NULL on failure.
5860 *
5861 * LOCKING:
5862 * Inherited from calling layer (may sleep).
5863 */
5864struct ata_host *ata_host_alloc_pinfo(struct device *dev,
5865 const struct ata_port_info * const * ppi,
5866 int n_ports)
5867{
5868 const struct ata_port_info *pi;
5869 struct ata_host *host;
5870 int i, j;
5871
5872 host = ata_host_alloc(dev, n_ports);
5873 if (!host)
5874 return NULL;
5875
5876 for (i = 0, j = 0, pi = NULL; i < host->n_ports; i++) {
5877 struct ata_port *ap = host->ports[i];
5878
5879 if (ppi[j])
5880 pi = ppi[j++];
5881
5882 ap->pio_mask = pi->pio_mask;
5883 ap->mwdma_mask = pi->mwdma_mask;
5884 ap->udma_mask = pi->udma_mask;
5885 ap->flags |= pi->flags;
Tejun Heo0c887582007-08-06 18:36:23 +09005886 ap->link.flags |= pi->link_flags;
Tejun Heof5cda252007-04-17 23:44:07 +09005887 ap->ops = pi->port_ops;
5888
5889 if (!host->ops && (pi->port_ops != &ata_dummy_port_ops))
5890 host->ops = pi->port_ops;
Tejun Heof5cda252007-04-17 23:44:07 +09005891 }
5892
5893 return host;
5894}
5895
Tejun Heob1c72912008-07-31 17:02:43 +09005896/**
5897 * ata_slave_link_init - initialize slave link
5898 * @ap: port to initialize slave link for
5899 *
5900 * Create and initialize slave link for @ap. This enables slave
5901 * link handling on the port.
5902 *
5903 * In libata, a port contains links and a link contains devices.
5904 * There is single host link but if a PMP is attached to it,
5905 * there can be multiple fan-out links. On SATA, there's usually
5906 * a single device connected to a link but PATA and SATA
5907 * controllers emulating TF based interface can have two - master
5908 * and slave.
5909 *
5910 * However, there are a few controllers which don't fit into this
5911 * abstraction too well - SATA controllers which emulate TF
5912 * interface with both master and slave devices but also have
5913 * separate SCR register sets for each device. These controllers
5914 * need separate links for physical link handling
5915 * (e.g. onlineness, link speed) but should be treated like a
5916 * traditional M/S controller for everything else (e.g. command
5917 * issue, softreset).
5918 *
5919 * slave_link is libata's way of handling this class of
5920 * controllers without impacting core layer too much. For
5921 * anything other than physical link handling, the default host
5922 * link is used for both master and slave. For physical link
5923 * handling, separate @ap->slave_link is used. All dirty details
5924 * are implemented inside libata core layer. From LLD's POV, the
5925 * only difference is that prereset, hardreset and postreset are
5926 * called once more for the slave link, so the reset sequence
5927 * looks like the following.
5928 *
5929 * prereset(M) -> prereset(S) -> hardreset(M) -> hardreset(S) ->
5930 * softreset(M) -> postreset(M) -> postreset(S)
5931 *
5932 * Note that softreset is called only for the master. Softreset
5933 * resets both M/S by definition, so SRST on master should handle
5934 * both (the standard method will work just fine).
5935 *
5936 * LOCKING:
5937 * Should be called before host is registered.
5938 *
5939 * RETURNS:
5940 * 0 on success, -errno on failure.
5941 */
5942int ata_slave_link_init(struct ata_port *ap)
5943{
5944 struct ata_link *link;
5945
5946 WARN_ON(ap->slave_link);
5947 WARN_ON(ap->flags & ATA_FLAG_PMP);
5948
5949 link = kzalloc(sizeof(*link), GFP_KERNEL);
5950 if (!link)
5951 return -ENOMEM;
5952
5953 ata_link_init(ap, link, 1);
5954 ap->slave_link = link;
5955 return 0;
5956}
5957
Tejun Heo32ebbc02007-11-08 13:09:00 +09005958static void ata_host_stop(struct device *gendev, void *res)
5959{
5960 struct ata_host *host = dev_get_drvdata(gendev);
5961 int i;
5962
5963 WARN_ON(!(host->flags & ATA_HOST_STARTED));
5964
5965 for (i = 0; i < host->n_ports; i++) {
5966 struct ata_port *ap = host->ports[i];
5967
5968 if (ap->ops->port_stop)
5969 ap->ops->port_stop(ap);
5970 }
5971
5972 if (host->ops->host_stop)
5973 host->ops->host_stop(host);
5974}
5975
Tejun Heof5cda252007-04-17 23:44:07 +09005976/**
Tejun Heo029cfd62008-03-25 12:22:49 +09005977 * ata_finalize_port_ops - finalize ata_port_operations
5978 * @ops: ata_port_operations to finalize
5979 *
5980 * An ata_port_operations can inherit from another ops and that
5981 * ops can again inherit from another. This can go on as many
5982 * times as necessary as long as there is no loop in the
5983 * inheritance chain.
5984 *
5985 * Ops tables are finalized when the host is started. NULL or
5986 * unspecified entries are inherited from the closet ancestor
5987 * which has the method and the entry is populated with it.
5988 * After finalization, the ops table directly points to all the
5989 * methods and ->inherits is no longer necessary and cleared.
5990 *
5991 * Using ATA_OP_NULL, inheriting ops can force a method to NULL.
5992 *
5993 * LOCKING:
5994 * None.
5995 */
5996static void ata_finalize_port_ops(struct ata_port_operations *ops)
5997{
Pradeep Singh Rautela2da67652008-05-29 23:28:14 +05305998 static DEFINE_SPINLOCK(lock);
Tejun Heo029cfd62008-03-25 12:22:49 +09005999 const struct ata_port_operations *cur;
6000 void **begin = (void **)ops;
6001 void **end = (void **)&ops->inherits;
6002 void **pp;
6003
6004 if (!ops || !ops->inherits)
6005 return;
6006
6007 spin_lock(&lock);
6008
6009 for (cur = ops->inherits; cur; cur = cur->inherits) {
6010 void **inherit = (void **)cur;
6011
6012 for (pp = begin; pp < end; pp++, inherit++)
6013 if (!*pp)
6014 *pp = *inherit;
6015 }
6016
6017 for (pp = begin; pp < end; pp++)
6018 if (IS_ERR(*pp))
6019 *pp = NULL;
6020
6021 ops->inherits = NULL;
6022
6023 spin_unlock(&lock);
6024}
6025
6026/**
Tejun Heoecef7252007-04-17 23:44:06 +09006027 * ata_host_start - start and freeze ports of an ATA host
6028 * @host: ATA host to start ports for
6029 *
6030 * Start and then freeze ports of @host. Started status is
6031 * recorded in host->flags, so this function can be called
6032 * multiple times. Ports are guaranteed to get started only
Tejun Heof3187192007-04-17 23:44:07 +09006033 * once. If host->ops isn't initialized yet, its set to the
6034 * first non-dummy port ops.
Tejun Heoecef7252007-04-17 23:44:06 +09006035 *
6036 * LOCKING:
6037 * Inherited from calling layer (may sleep).
6038 *
6039 * RETURNS:
6040 * 0 if all ports are started successfully, -errno otherwise.
6041 */
6042int ata_host_start(struct ata_host *host)
6043{
Tejun Heo32ebbc02007-11-08 13:09:00 +09006044 int have_stop = 0;
6045 void *start_dr = NULL;
Tejun Heoecef7252007-04-17 23:44:06 +09006046 int i, rc;
6047
6048 if (host->flags & ATA_HOST_STARTED)
6049 return 0;
6050
Tejun Heo029cfd62008-03-25 12:22:49 +09006051 ata_finalize_port_ops(host->ops);
6052
Tejun Heoecef7252007-04-17 23:44:06 +09006053 for (i = 0; i < host->n_ports; i++) {
6054 struct ata_port *ap = host->ports[i];
6055
Tejun Heo029cfd62008-03-25 12:22:49 +09006056 ata_finalize_port_ops(ap->ops);
6057
Tejun Heof3187192007-04-17 23:44:07 +09006058 if (!host->ops && !ata_port_is_dummy(ap))
6059 host->ops = ap->ops;
6060
Tejun Heo32ebbc02007-11-08 13:09:00 +09006061 if (ap->ops->port_stop)
6062 have_stop = 1;
6063 }
6064
6065 if (host->ops->host_stop)
6066 have_stop = 1;
6067
6068 if (have_stop) {
6069 start_dr = devres_alloc(ata_host_stop, 0, GFP_KERNEL);
6070 if (!start_dr)
6071 return -ENOMEM;
6072 }
6073
6074 for (i = 0; i < host->n_ports; i++) {
6075 struct ata_port *ap = host->ports[i];
6076
Tejun Heoecef7252007-04-17 23:44:06 +09006077 if (ap->ops->port_start) {
6078 rc = ap->ops->port_start(ap);
6079 if (rc) {
Alan Cox0f9fe9b2007-11-30 15:23:16 +00006080 if (rc != -ENODEV)
Andrew Morton0f757742008-01-10 14:33:09 -08006081 dev_printk(KERN_ERR, host->dev,
6082 "failed to start port %d "
6083 "(errno=%d)\n", i, rc);
Tejun Heoecef7252007-04-17 23:44:06 +09006084 goto err_out;
6085 }
6086 }
Tejun Heoecef7252007-04-17 23:44:06 +09006087 ata_eh_freeze_port(ap);
6088 }
6089
Tejun Heo32ebbc02007-11-08 13:09:00 +09006090 if (start_dr)
6091 devres_add(host->dev, start_dr);
Tejun Heoecef7252007-04-17 23:44:06 +09006092 host->flags |= ATA_HOST_STARTED;
6093 return 0;
6094
6095 err_out:
6096 while (--i >= 0) {
6097 struct ata_port *ap = host->ports[i];
6098
6099 if (ap->ops->port_stop)
6100 ap->ops->port_stop(ap);
6101 }
Tejun Heo32ebbc02007-11-08 13:09:00 +09006102 devres_free(start_dr);
Tejun Heoecef7252007-04-17 23:44:06 +09006103 return rc;
6104}
6105
6106/**
Jeff Garzikcca39742006-08-24 03:19:22 -04006107 * ata_sas_host_init - Initialize a host struct
6108 * @host: host to initialize
6109 * @dev: device host is attached to
6110 * @flags: host flags
6111 * @ops: port_ops
Brian Kingb03732f2006-08-07 14:27:10 -05006112 *
6113 * LOCKING:
6114 * PCI/etc. bus probe sem.
6115 *
6116 */
Tejun Heof3187192007-04-17 23:44:07 +09006117/* KILLME - the only user left is ipr */
Jeff Garzikcca39742006-08-24 03:19:22 -04006118void ata_host_init(struct ata_host *host, struct device *dev,
Tejun Heo029cfd62008-03-25 12:22:49 +09006119 unsigned long flags, struct ata_port_operations *ops)
Brian Kingb03732f2006-08-07 14:27:10 -05006120{
Jeff Garzikcca39742006-08-24 03:19:22 -04006121 spin_lock_init(&host->lock);
6122 host->dev = dev;
6123 host->flags = flags;
6124 host->ops = ops;
Brian Kingb03732f2006-08-07 14:27:10 -05006125}
6126
Arjan van de Ven79318052009-01-04 05:32:28 -08006127
6128static void async_port_probe(void *data, async_cookie_t cookie)
6129{
6130 int rc;
6131 struct ata_port *ap = data;
Arjan van de Ven886ad092009-01-09 15:54:07 -08006132
6133 /*
6134 * If we're not allowed to scan this host in parallel,
6135 * we need to wait until all previous scans have completed
6136 * before going further.
Arjan van de Venfa853a42009-01-10 10:18:44 -08006137 * Jeff Garzik says this is only within a controller, so we
6138 * don't need to wait for port 0, only for later ports.
Arjan van de Ven886ad092009-01-09 15:54:07 -08006139 */
Arjan van de Venfa853a42009-01-10 10:18:44 -08006140 if (!(ap->host->flags & ATA_HOST_PARALLEL_SCAN) && ap->port_no != 0)
Arjan van de Ven886ad092009-01-09 15:54:07 -08006141 async_synchronize_cookie(cookie);
6142
Arjan van de Ven79318052009-01-04 05:32:28 -08006143 /* probe */
6144 if (ap->ops->error_handler) {
6145 struct ata_eh_info *ehi = &ap->link.eh_info;
6146 unsigned long flags;
6147
6148 ata_port_probe(ap);
6149
6150 /* kick EH for boot probing */
6151 spin_lock_irqsave(ap->lock, flags);
6152
6153 ehi->probe_mask |= ATA_ALL_DEVICES;
6154 ehi->action |= ATA_EH_RESET | ATA_EH_LPM;
6155 ehi->flags |= ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET;
6156
6157 ap->pflags &= ~ATA_PFLAG_INITIALIZING;
6158 ap->pflags |= ATA_PFLAG_LOADING;
6159 ata_port_schedule_eh(ap);
6160
6161 spin_unlock_irqrestore(ap->lock, flags);
6162
6163 /* wait for EH to finish */
6164 ata_port_wait_eh(ap);
6165 } else {
6166 DPRINTK("ata%u: bus probe begin\n", ap->print_id);
6167 rc = ata_bus_probe(ap);
6168 DPRINTK("ata%u: bus probe end\n", ap->print_id);
6169
6170 if (rc) {
6171 /* FIXME: do something useful here?
6172 * Current libata behavior will
6173 * tear down everything when
6174 * the module is removed
6175 * or the h/w is unplugged.
6176 */
6177 }
6178 }
Arjan van de Venf29d3b22009-01-05 15:07:07 -08006179
6180 /* in order to keep device order, we need to synchronize at this point */
6181 async_synchronize_cookie(cookie);
6182
6183 ata_scsi_scan_host(ap, 1);
6184
Arjan van de Ven79318052009-01-04 05:32:28 -08006185}
Brian Kingb03732f2006-08-07 14:27:10 -05006186/**
Tejun Heof3187192007-04-17 23:44:07 +09006187 * ata_host_register - register initialized ATA host
6188 * @host: ATA host to register
6189 * @sht: template for SCSI host
Jeff Garzik0cba6322005-05-30 19:49:12 -04006190 *
Tejun Heof3187192007-04-17 23:44:07 +09006191 * Register initialized ATA host. @host is allocated using
6192 * ata_host_alloc() and fully initialized by LLD. This function
6193 * starts ports, registers @host with ATA and SCSI layers and
6194 * probe registered devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006195 *
6196 * LOCKING:
Tejun Heof3187192007-04-17 23:44:07 +09006197 * Inherited from calling layer (may sleep).
Linus Torvalds1da177e2005-04-16 15:20:36 -07006198 *
6199 * RETURNS:
Tejun Heof3187192007-04-17 23:44:07 +09006200 * 0 on success, -errno otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006201 */
Tejun Heof3187192007-04-17 23:44:07 +09006202int ata_host_register(struct ata_host *host, struct scsi_host_template *sht)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006203{
Tejun Heof3187192007-04-17 23:44:07 +09006204 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006205
Tejun Heof3187192007-04-17 23:44:07 +09006206 /* host must have been started */
6207 if (!(host->flags & ATA_HOST_STARTED)) {
6208 dev_printk(KERN_ERR, host->dev,
6209 "BUG: trying to register unstarted host\n");
6210 WARN_ON(1);
6211 return -EINVAL;
Alan Cox02f076a2006-09-26 17:35:32 +01006212 }
Tejun Heof0d36ef2007-01-20 16:00:28 +09006213
Tejun Heof3187192007-04-17 23:44:07 +09006214 /* Blow away unused ports. This happens when LLD can't
6215 * determine the exact number of ports to allocate at
6216 * allocation time.
6217 */
6218 for (i = host->n_ports; host->ports[i]; i++)
6219 kfree(host->ports[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006220
Tejun Heof3187192007-04-17 23:44:07 +09006221 /* give ports names and add SCSI hosts */
6222 for (i = 0; i < host->n_ports; i++)
6223 host->ports[i]->print_id = ata_print_id++;
Tejun Heof0d36ef2007-01-20 16:00:28 +09006224
Tejun Heof3187192007-04-17 23:44:07 +09006225 rc = ata_scsi_add_hosts(host, sht);
Tejun Heoecef7252007-04-17 23:44:06 +09006226 if (rc)
Tejun Heof3187192007-04-17 23:44:07 +09006227 return rc;
Tejun Heoecef7252007-04-17 23:44:06 +09006228
Tejun Heofafbae82007-05-15 03:28:16 +09006229 /* associate with ACPI nodes */
6230 ata_acpi_associate(host);
6231
Tejun Heof3187192007-04-17 23:44:07 +09006232 /* set cable, sata_spd_limit and report */
Jeff Garzikcca39742006-08-24 03:19:22 -04006233 for (i = 0; i < host->n_ports; i++) {
6234 struct ata_port *ap = host->ports[i];
Tejun Heof3187192007-04-17 23:44:07 +09006235 unsigned long xfer_mask;
6236
6237 /* set SATA cable type if still unset */
6238 if (ap->cbl == ATA_CBL_NONE && (ap->flags & ATA_FLAG_SATA))
6239 ap->cbl = ATA_CBL_SATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006240
Tejun Heo5a04bf42006-05-31 18:27:38 +09006241 /* init sata_spd_limit to the current value */
Tejun Heo4fb37a22007-08-06 18:36:23 +09006242 sata_link_init_spd(&ap->link);
Tejun Heob1c72912008-07-31 17:02:43 +09006243 if (ap->slave_link)
6244 sata_link_init_spd(ap->slave_link);
Tejun Heo5a04bf42006-05-31 18:27:38 +09006245
Tejun Heocbcdd872007-08-18 13:14:55 +09006246 /* print per-port info to dmesg */
Tejun Heof3187192007-04-17 23:44:07 +09006247 xfer_mask = ata_pack_xfermask(ap->pio_mask, ap->mwdma_mask,
6248 ap->udma_mask);
6249
Tejun Heoabf6e8e2007-10-09 14:57:25 +09006250 if (!ata_port_is_dummy(ap)) {
Tejun Heocbcdd872007-08-18 13:14:55 +09006251 ata_port_printk(ap, KERN_INFO,
6252 "%cATA max %s %s\n",
Tejun Heoa16abc02007-05-21 18:33:47 +02006253 (ap->flags & ATA_FLAG_SATA) ? 'S' : 'P',
Tejun Heof3187192007-04-17 23:44:07 +09006254 ata_mode_string(xfer_mask),
Tejun Heocbcdd872007-08-18 13:14:55 +09006255 ap->link.eh_info.desc);
Tejun Heoabf6e8e2007-10-09 14:57:25 +09006256 ata_ehi_clear_desc(&ap->link.eh_info);
6257 } else
Tejun Heof3187192007-04-17 23:44:07 +09006258 ata_port_printk(ap, KERN_INFO, "DUMMY\n");
6259 }
6260
Vegard Nossumf6005352009-04-08 18:19:39 +02006261 /* perform each probe asynchronously */
Tejun Heof3187192007-04-17 23:44:07 +09006262 for (i = 0; i < host->n_ports; i++) {
6263 struct ata_port *ap = host->ports[i];
Arjan van de Ven79318052009-01-04 05:32:28 -08006264 async_schedule(async_port_probe, ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006266
Tejun Heof3187192007-04-17 23:44:07 +09006267 return 0;
6268}
6269
6270/**
Tejun Heof5cda252007-04-17 23:44:07 +09006271 * ata_host_activate - start host, request IRQ and register it
6272 * @host: target ATA host
6273 * @irq: IRQ to request
6274 * @irq_handler: irq_handler used when requesting IRQ
6275 * @irq_flags: irq_flags used when requesting IRQ
6276 * @sht: scsi_host_template to use when registering the host
6277 *
6278 * After allocating an ATA host and initializing it, most libata
6279 * LLDs perform three steps to activate the host - start host,
6280 * request IRQ and register it. This helper takes necessasry
6281 * arguments and performs the three steps in one go.
6282 *
Paul Mundt3d46b2e2007-11-08 11:14:56 +09006283 * An invalid IRQ skips the IRQ registration and expects the host to
6284 * have set polling mode on the port. In this case, @irq_handler
6285 * should be NULL.
6286 *
Tejun Heof5cda252007-04-17 23:44:07 +09006287 * LOCKING:
6288 * Inherited from calling layer (may sleep).
6289 *
6290 * RETURNS:
6291 * 0 on success, -errno otherwise.
6292 */
6293int ata_host_activate(struct ata_host *host, int irq,
6294 irq_handler_t irq_handler, unsigned long irq_flags,
6295 struct scsi_host_template *sht)
6296{
Tejun Heocbcdd872007-08-18 13:14:55 +09006297 int i, rc;
Tejun Heof5cda252007-04-17 23:44:07 +09006298
6299 rc = ata_host_start(host);
6300 if (rc)
6301 return rc;
6302
Paul Mundt3d46b2e2007-11-08 11:14:56 +09006303 /* Special case for polling mode */
6304 if (!irq) {
6305 WARN_ON(irq_handler);
6306 return ata_host_register(host, sht);
6307 }
6308
Tejun Heof5cda252007-04-17 23:44:07 +09006309 rc = devm_request_irq(host->dev, irq, irq_handler, irq_flags,
6310 dev_driver_string(host->dev), host);
6311 if (rc)
6312 return rc;
6313
Tejun Heocbcdd872007-08-18 13:14:55 +09006314 for (i = 0; i < host->n_ports; i++)
6315 ata_port_desc(host->ports[i], "irq %d", irq);
Tejun Heo40318262007-07-03 01:38:47 +09006316
Tejun Heof5cda252007-04-17 23:44:07 +09006317 rc = ata_host_register(host, sht);
6318 /* if failed, just free the IRQ and leave ports alone */
6319 if (rc)
6320 devm_free_irq(host->dev, irq, host);
6321
6322 return rc;
6323}
6324
6325/**
Tejun Heo720ba122006-05-31 18:28:13 +09006326 * ata_port_detach - Detach ATA port in prepration of device removal
6327 * @ap: ATA port to be detached
6328 *
6329 * Detach all ATA devices and the associated SCSI devices of @ap;
6330 * then, remove the associated SCSI host. @ap is guaranteed to
6331 * be quiescent on return from this function.
6332 *
6333 * LOCKING:
6334 * Kernel thread context (may sleep).
6335 */
Adrian Bunk741b7762007-10-24 18:23:06 +02006336static void ata_port_detach(struct ata_port *ap)
Tejun Heo720ba122006-05-31 18:28:13 +09006337{
6338 unsigned long flags;
Tejun Heo720ba122006-05-31 18:28:13 +09006339
6340 if (!ap->ops->error_handler)
Tejun Heoc3cf30a2006-08-05 03:59:11 +09006341 goto skip_eh;
Tejun Heo720ba122006-05-31 18:28:13 +09006342
6343 /* tell EH we're leaving & flush EH */
Jeff Garzikba6a1302006-06-22 23:46:10 -04006344 spin_lock_irqsave(ap->lock, flags);
Tejun Heob51e9e52006-06-29 01:29:30 +09006345 ap->pflags |= ATA_PFLAG_UNLOADING;
Tejun Heoece180d2008-11-03 20:04:37 +09006346 ata_port_schedule_eh(ap);
Jeff Garzikba6a1302006-06-22 23:46:10 -04006347 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo720ba122006-05-31 18:28:13 +09006348
Tejun Heoece180d2008-11-03 20:04:37 +09006349 /* wait till EH commits suicide */
Tejun Heo720ba122006-05-31 18:28:13 +09006350 ata_port_wait_eh(ap);
6351
Tejun Heoece180d2008-11-03 20:04:37 +09006352 /* it better be dead now */
6353 WARN_ON(!(ap->pflags & ATA_PFLAG_UNLOADED));
Tejun Heo720ba122006-05-31 18:28:13 +09006354
Oleg Nesterov45a66c12007-07-09 11:46:13 -07006355 cancel_rearming_delayed_work(&ap->hotplug_task);
Tejun Heo720ba122006-05-31 18:28:13 +09006356
Tejun Heoc3cf30a2006-08-05 03:59:11 +09006357 skip_eh:
Tejun Heo720ba122006-05-31 18:28:13 +09006358 /* remove the associated SCSI host */
Jeff Garzikcca39742006-08-24 03:19:22 -04006359 scsi_remove_host(ap->scsi_host);
Tejun Heo720ba122006-05-31 18:28:13 +09006360}
6361
6362/**
Tejun Heo0529c1592007-01-20 16:00:26 +09006363 * ata_host_detach - Detach all ports of an ATA host
6364 * @host: Host to detach
6365 *
6366 * Detach all ports of @host.
6367 *
6368 * LOCKING:
6369 * Kernel thread context (may sleep).
6370 */
6371void ata_host_detach(struct ata_host *host)
6372{
6373 int i;
6374
6375 for (i = 0; i < host->n_ports; i++)
6376 ata_port_detach(host->ports[i]);
Tejun Heo562f0c22007-12-15 15:05:01 +09006377
6378 /* the host is dead now, dissociate ACPI */
6379 ata_acpi_dissociate(host);
Tejun Heo0529c1592007-01-20 16:00:26 +09006380}
6381
Jeff Garzik374b1872005-08-30 05:42:52 -04006382#ifdef CONFIG_PCI
6383
Edward Falk0baab862005-06-02 18:17:13 -04006384/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07006385 * ata_pci_remove_one - PCI layer callback for device removal
6386 * @pdev: PCI device that was removed
6387 *
Tejun Heob878ca52007-01-20 16:00:28 +09006388 * PCI layer indicates to libata via this hook that hot-unplug or
6389 * module unload event has occurred. Detach all ports. Resource
6390 * release is handled via devres.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006391 *
6392 * LOCKING:
6393 * Inherited from PCI layer (may sleep).
6394 */
Tejun Heof0d36ef2007-01-20 16:00:28 +09006395void ata_pci_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006396{
Jeff Garzik28555682007-10-11 17:12:35 -04006397 struct device *dev = &pdev->dev;
Jeff Garzikcca39742006-08-24 03:19:22 -04006398 struct ata_host *host = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006399
Tejun Heob878ca52007-01-20 16:00:28 +09006400 ata_host_detach(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006401}
6402
6403/* move to PCI subsystem */
Jeff Garzik057ace52005-10-22 14:27:05 -04006404int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006405{
6406 unsigned long tmp = 0;
6407
6408 switch (bits->width) {
6409 case 1: {
6410 u8 tmp8 = 0;
6411 pci_read_config_byte(pdev, bits->reg, &tmp8);
6412 tmp = tmp8;
6413 break;
6414 }
6415 case 2: {
6416 u16 tmp16 = 0;
6417 pci_read_config_word(pdev, bits->reg, &tmp16);
6418 tmp = tmp16;
6419 break;
6420 }
6421 case 4: {
6422 u32 tmp32 = 0;
6423 pci_read_config_dword(pdev, bits->reg, &tmp32);
6424 tmp = tmp32;
6425 break;
6426 }
6427
6428 default:
6429 return -EINVAL;
6430 }
6431
6432 tmp &= bits->mask;
6433
6434 return (tmp == bits->val) ? 1 : 0;
6435}
Jens Axboe9b847542006-01-06 09:28:07 +01006436
Tejun Heo6ffa01d2007-03-02 17:32:47 +09006437#ifdef CONFIG_PM
Tejun Heo3c5100c2006-07-26 16:58:33 +09006438void ata_pci_device_do_suspend(struct pci_dev *pdev, pm_message_t mesg)
Jens Axboe9b847542006-01-06 09:28:07 +01006439{
6440 pci_save_state(pdev);
Tejun Heo4c90d972007-02-20 18:14:48 +09006441 pci_disable_device(pdev);
Tejun Heo500530f2006-07-03 16:07:27 +09006442
Rafael J. Wysocki3a2d5b72008-02-23 19:13:25 +01006443 if (mesg.event & PM_EVENT_SLEEP)
Tejun Heo500530f2006-07-03 16:07:27 +09006444 pci_set_power_state(pdev, PCI_D3hot);
Jens Axboe9b847542006-01-06 09:28:07 +01006445}
6446
Tejun Heo553c4aa2006-12-26 19:39:50 +09006447int ata_pci_device_do_resume(struct pci_dev *pdev)
Jens Axboe9b847542006-01-06 09:28:07 +01006448{
Tejun Heo553c4aa2006-12-26 19:39:50 +09006449 int rc;
6450
Jens Axboe9b847542006-01-06 09:28:07 +01006451 pci_set_power_state(pdev, PCI_D0);
6452 pci_restore_state(pdev);
Tejun Heo553c4aa2006-12-26 19:39:50 +09006453
Tejun Heob878ca52007-01-20 16:00:28 +09006454 rc = pcim_enable_device(pdev);
Tejun Heo553c4aa2006-12-26 19:39:50 +09006455 if (rc) {
6456 dev_printk(KERN_ERR, &pdev->dev,
6457 "failed to enable device after resume (%d)\n", rc);
6458 return rc;
6459 }
6460
Jens Axboe9b847542006-01-06 09:28:07 +01006461 pci_set_master(pdev);
Tejun Heo553c4aa2006-12-26 19:39:50 +09006462 return 0;
Tejun Heo500530f2006-07-03 16:07:27 +09006463}
6464
Tejun Heo3c5100c2006-07-26 16:58:33 +09006465int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg)
Tejun Heo500530f2006-07-03 16:07:27 +09006466{
Jeff Garzikcca39742006-08-24 03:19:22 -04006467 struct ata_host *host = dev_get_drvdata(&pdev->dev);
Tejun Heo500530f2006-07-03 16:07:27 +09006468 int rc = 0;
6469
Jeff Garzikcca39742006-08-24 03:19:22 -04006470 rc = ata_host_suspend(host, mesg);
Tejun Heo500530f2006-07-03 16:07:27 +09006471 if (rc)
6472 return rc;
6473
Tejun Heo3c5100c2006-07-26 16:58:33 +09006474 ata_pci_device_do_suspend(pdev, mesg);
Tejun Heo500530f2006-07-03 16:07:27 +09006475
6476 return 0;
6477}
6478
6479int ata_pci_device_resume(struct pci_dev *pdev)
6480{
Jeff Garzikcca39742006-08-24 03:19:22 -04006481 struct ata_host *host = dev_get_drvdata(&pdev->dev);
Tejun Heo553c4aa2006-12-26 19:39:50 +09006482 int rc;
Tejun Heo500530f2006-07-03 16:07:27 +09006483
Tejun Heo553c4aa2006-12-26 19:39:50 +09006484 rc = ata_pci_device_do_resume(pdev);
6485 if (rc == 0)
6486 ata_host_resume(host);
6487 return rc;
Jens Axboe9b847542006-01-06 09:28:07 +01006488}
Tejun Heo6ffa01d2007-03-02 17:32:47 +09006489#endif /* CONFIG_PM */
6490
Linus Torvalds1da177e2005-04-16 15:20:36 -07006491#endif /* CONFIG_PCI */
6492
Tejun Heo33267322008-02-13 09:15:09 +09006493static int __init ata_parse_force_one(char **cur,
6494 struct ata_force_ent *force_ent,
6495 const char **reason)
6496{
6497 /* FIXME: Currently, there's no way to tag init const data and
6498 * using __initdata causes build failure on some versions of
6499 * gcc. Once __initdataconst is implemented, add const to the
6500 * following structure.
6501 */
6502 static struct ata_force_param force_tbl[] __initdata = {
6503 { "40c", .cbl = ATA_CBL_PATA40 },
6504 { "80c", .cbl = ATA_CBL_PATA80 },
6505 { "short40c", .cbl = ATA_CBL_PATA40_SHORT },
6506 { "unk", .cbl = ATA_CBL_PATA_UNK },
6507 { "ign", .cbl = ATA_CBL_PATA_IGN },
6508 { "sata", .cbl = ATA_CBL_SATA },
6509 { "1.5Gbps", .spd_limit = 1 },
6510 { "3.0Gbps", .spd_limit = 2 },
6511 { "noncq", .horkage_on = ATA_HORKAGE_NONCQ },
6512 { "ncq", .horkage_off = ATA_HORKAGE_NONCQ },
6513 { "pio0", .xfer_mask = 1 << (ATA_SHIFT_PIO + 0) },
6514 { "pio1", .xfer_mask = 1 << (ATA_SHIFT_PIO + 1) },
6515 { "pio2", .xfer_mask = 1 << (ATA_SHIFT_PIO + 2) },
6516 { "pio3", .xfer_mask = 1 << (ATA_SHIFT_PIO + 3) },
6517 { "pio4", .xfer_mask = 1 << (ATA_SHIFT_PIO + 4) },
6518 { "pio5", .xfer_mask = 1 << (ATA_SHIFT_PIO + 5) },
6519 { "pio6", .xfer_mask = 1 << (ATA_SHIFT_PIO + 6) },
6520 { "mwdma0", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 0) },
6521 { "mwdma1", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 1) },
6522 { "mwdma2", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 2) },
6523 { "mwdma3", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 3) },
6524 { "mwdma4", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 4) },
6525 { "udma0", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 0) },
6526 { "udma16", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 0) },
6527 { "udma/16", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 0) },
6528 { "udma1", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 1) },
6529 { "udma25", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 1) },
6530 { "udma/25", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 1) },
6531 { "udma2", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 2) },
6532 { "udma33", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 2) },
6533 { "udma/33", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 2) },
6534 { "udma3", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 3) },
6535 { "udma44", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 3) },
6536 { "udma/44", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 3) },
6537 { "udma4", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 4) },
6538 { "udma66", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 4) },
6539 { "udma/66", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 4) },
6540 { "udma5", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 5) },
6541 { "udma100", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 5) },
6542 { "udma/100", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 5) },
6543 { "udma6", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 6) },
6544 { "udma133", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 6) },
6545 { "udma/133", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 6) },
6546 { "udma7", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 7) },
Tejun Heo05944bd2008-08-13 20:19:09 +09006547 { "nohrst", .lflags = ATA_LFLAG_NO_HRST },
6548 { "nosrst", .lflags = ATA_LFLAG_NO_SRST },
6549 { "norst", .lflags = ATA_LFLAG_NO_HRST | ATA_LFLAG_NO_SRST },
Tejun Heo33267322008-02-13 09:15:09 +09006550 };
6551 char *start = *cur, *p = *cur;
6552 char *id, *val, *endp;
6553 const struct ata_force_param *match_fp = NULL;
6554 int nr_matches = 0, i;
6555
6556 /* find where this param ends and update *cur */
6557 while (*p != '\0' && *p != ',')
6558 p++;
6559
6560 if (*p == '\0')
6561 *cur = p;
6562 else
6563 *cur = p + 1;
6564
6565 *p = '\0';
6566
6567 /* parse */
6568 p = strchr(start, ':');
6569 if (!p) {
6570 val = strstrip(start);
6571 goto parse_val;
6572 }
6573 *p = '\0';
6574
6575 id = strstrip(start);
6576 val = strstrip(p + 1);
6577
6578 /* parse id */
6579 p = strchr(id, '.');
6580 if (p) {
6581 *p++ = '\0';
6582 force_ent->device = simple_strtoul(p, &endp, 10);
6583 if (p == endp || *endp != '\0') {
6584 *reason = "invalid device";
6585 return -EINVAL;
6586 }
6587 }
6588
6589 force_ent->port = simple_strtoul(id, &endp, 10);
6590 if (p == endp || *endp != '\0') {
6591 *reason = "invalid port/link";
6592 return -EINVAL;
6593 }
6594
6595 parse_val:
6596 /* parse val, allow shortcuts so that both 1.5 and 1.5Gbps work */
6597 for (i = 0; i < ARRAY_SIZE(force_tbl); i++) {
6598 const struct ata_force_param *fp = &force_tbl[i];
6599
6600 if (strncasecmp(val, fp->name, strlen(val)))
6601 continue;
6602
6603 nr_matches++;
6604 match_fp = fp;
6605
6606 if (strcasecmp(val, fp->name) == 0) {
6607 nr_matches = 1;
6608 break;
6609 }
6610 }
6611
6612 if (!nr_matches) {
6613 *reason = "unknown value";
6614 return -EINVAL;
6615 }
6616 if (nr_matches > 1) {
6617 *reason = "ambigious value";
6618 return -EINVAL;
6619 }
6620
6621 force_ent->param = *match_fp;
6622
6623 return 0;
6624}
6625
6626static void __init ata_parse_force_param(void)
6627{
6628 int idx = 0, size = 1;
6629 int last_port = -1, last_device = -1;
6630 char *p, *cur, *next;
6631
6632 /* calculate maximum number of params and allocate force_tbl */
6633 for (p = ata_force_param_buf; *p; p++)
6634 if (*p == ',')
6635 size++;
6636
6637 ata_force_tbl = kzalloc(sizeof(ata_force_tbl[0]) * size, GFP_KERNEL);
6638 if (!ata_force_tbl) {
6639 printk(KERN_WARNING "ata: failed to extend force table, "
6640 "libata.force ignored\n");
6641 return;
6642 }
6643
6644 /* parse and populate the table */
6645 for (cur = ata_force_param_buf; *cur != '\0'; cur = next) {
6646 const char *reason = "";
6647 struct ata_force_ent te = { .port = -1, .device = -1 };
6648
6649 next = cur;
6650 if (ata_parse_force_one(&next, &te, &reason)) {
6651 printk(KERN_WARNING "ata: failed to parse force "
6652 "parameter \"%s\" (%s)\n",
6653 cur, reason);
6654 continue;
6655 }
6656
6657 if (te.port == -1) {
6658 te.port = last_port;
6659 te.device = last_device;
6660 }
6661
6662 ata_force_tbl[idx++] = te;
6663
6664 last_port = te.port;
6665 last_device = te.device;
6666 }
6667
6668 ata_force_tbl_size = idx;
6669}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006670
Linus Torvalds1da177e2005-04-16 15:20:36 -07006671static int __init ata_init(void)
6672{
Tejun Heo33267322008-02-13 09:15:09 +09006673 ata_parse_force_param();
6674
Bartlomiej Zolnierkiewicz9cd13bd2009-11-26 17:11:16 +01006675 /*
6676 * FIXME: In UP case, there is only one workqueue thread and if you
6677 * have more than one PIO device, latency is bloody awful, with
6678 * occasional multi-second "hiccups" as one PIO device waits for
6679 * another. It's an ugly wart that users DO occasionally complain
6680 * about; luckily most users have at most one PIO polled device.
6681 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006682 ata_wq = create_workqueue("ata");
6683 if (!ata_wq)
Elias Oltmanns49ea3b02008-07-30 12:32:39 -07006684 goto free_force_tbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006685
Tejun Heo453b07a2006-05-31 18:27:42 +09006686 ata_aux_wq = create_singlethread_workqueue("ata_aux");
Elias Oltmanns49ea3b02008-07-30 12:32:39 -07006687 if (!ata_aux_wq)
6688 goto free_wq;
Tejun Heo453b07a2006-05-31 18:27:42 +09006689
Linus Torvalds1da177e2005-04-16 15:20:36 -07006690 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
6691 return 0;
Elias Oltmanns49ea3b02008-07-30 12:32:39 -07006692
6693free_wq:
6694 destroy_workqueue(ata_wq);
6695free_force_tbl:
6696 kfree(ata_force_tbl);
6697 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006698}
6699
6700static void __exit ata_exit(void)
6701{
Tejun Heo33267322008-02-13 09:15:09 +09006702 kfree(ata_force_tbl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006703 destroy_workqueue(ata_wq);
Tejun Heo453b07a2006-05-31 18:27:42 +09006704 destroy_workqueue(ata_aux_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006705}
6706
Brian Kinga4625082006-11-13 16:32:36 -06006707subsys_initcall(ata_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006708module_exit(ata_exit);
6709
Akinobu Mita9990b6f2010-04-12 21:11:41 +09006710static DEFINE_RATELIMIT_STATE(ratelimit, HZ / 5, 1);
Jeff Garzik67846b32005-10-05 02:58:32 -04006711
6712int ata_ratelimit(void)
6713{
Akinobu Mita9990b6f2010-04-12 21:11:41 +09006714 return __ratelimit(&ratelimit);
Jeff Garzik67846b32005-10-05 02:58:32 -04006715}
6716
Tejun Heoc22daff2006-04-11 22:22:29 +09006717/**
6718 * ata_wait_register - wait until register value changes
6719 * @reg: IO-mapped register
6720 * @mask: Mask to apply to read register value
6721 * @val: Wait condition
Tejun Heo341c2c92008-05-20 02:17:51 +09006722 * @interval: polling interval in milliseconds
6723 * @timeout: timeout in milliseconds
Tejun Heoc22daff2006-04-11 22:22:29 +09006724 *
6725 * Waiting for some bits of register to change is a common
6726 * operation for ATA controllers. This function reads 32bit LE
6727 * IO-mapped register @reg and tests for the following condition.
6728 *
6729 * (*@reg & mask) != val
6730 *
6731 * If the condition is met, it returns; otherwise, the process is
6732 * repeated after @interval_msec until timeout.
6733 *
6734 * LOCKING:
6735 * Kernel thread context (may sleep)
6736 *
6737 * RETURNS:
6738 * The final register value.
6739 */
6740u32 ata_wait_register(void __iomem *reg, u32 mask, u32 val,
Tejun Heo341c2c92008-05-20 02:17:51 +09006741 unsigned long interval, unsigned long timeout)
Tejun Heoc22daff2006-04-11 22:22:29 +09006742{
Tejun Heo341c2c92008-05-20 02:17:51 +09006743 unsigned long deadline;
Tejun Heoc22daff2006-04-11 22:22:29 +09006744 u32 tmp;
6745
6746 tmp = ioread32(reg);
6747
6748 /* Calculate timeout _after_ the first read to make sure
6749 * preceding writes reach the controller before starting to
6750 * eat away the timeout.
6751 */
Tejun Heo341c2c92008-05-20 02:17:51 +09006752 deadline = ata_deadline(jiffies, timeout);
Tejun Heoc22daff2006-04-11 22:22:29 +09006753
Tejun Heo341c2c92008-05-20 02:17:51 +09006754 while ((tmp & mask) == val && time_before(jiffies, deadline)) {
6755 msleep(interval);
Tejun Heoc22daff2006-04-11 22:22:29 +09006756 tmp = ioread32(reg);
6757 }
6758
6759 return tmp;
6760}
6761
Linus Torvalds1da177e2005-04-16 15:20:36 -07006762/*
Tejun Heodd5b06c2006-08-10 16:59:12 +09006763 * Dummy port_ops
6764 */
Tejun Heodd5b06c2006-08-10 16:59:12 +09006765static unsigned int ata_dummy_qc_issue(struct ata_queued_cmd *qc)
6766{
6767 return AC_ERR_SYSTEM;
6768}
6769
Tejun Heo182d7bb2008-04-07 22:47:21 +09006770static void ata_dummy_error_handler(struct ata_port *ap)
6771{
6772 /* truly dummy */
6773}
6774
Tejun Heo029cfd62008-03-25 12:22:49 +09006775struct ata_port_operations ata_dummy_port_ops = {
Tejun Heodd5b06c2006-08-10 16:59:12 +09006776 .qc_prep = ata_noop_qc_prep,
6777 .qc_issue = ata_dummy_qc_issue,
Tejun Heo182d7bb2008-04-07 22:47:21 +09006778 .error_handler = ata_dummy_error_handler,
Tejun Heodd5b06c2006-08-10 16:59:12 +09006779};
6780
Tejun Heo21b0ad42007-04-17 23:44:07 +09006781const struct ata_port_info ata_dummy_port_info = {
6782 .port_ops = &ata_dummy_port_ops,
6783};
6784
Tejun Heodd5b06c2006-08-10 16:59:12 +09006785/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006786 * libata is essentially a library of internal helper functions for
6787 * low-level ATA host controller drivers. As such, the API/ABI is
6788 * likely to change as new drivers are added and updated.
6789 * Do not depend on ABI/API stability.
6790 */
Tejun Heoe9c83912006-07-03 16:07:26 +09006791EXPORT_SYMBOL_GPL(sata_deb_timing_normal);
6792EXPORT_SYMBOL_GPL(sata_deb_timing_hotplug);
6793EXPORT_SYMBOL_GPL(sata_deb_timing_long);
Tejun Heo029cfd62008-03-25 12:22:49 +09006794EXPORT_SYMBOL_GPL(ata_base_port_ops);
6795EXPORT_SYMBOL_GPL(sata_port_ops);
Tejun Heodd5b06c2006-08-10 16:59:12 +09006796EXPORT_SYMBOL_GPL(ata_dummy_port_ops);
Tejun Heo21b0ad42007-04-17 23:44:07 +09006797EXPORT_SYMBOL_GPL(ata_dummy_port_info);
Tejun Heo1eca4362008-11-03 20:03:17 +09006798EXPORT_SYMBOL_GPL(ata_link_next);
6799EXPORT_SYMBOL_GPL(ata_dev_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006800EXPORT_SYMBOL_GPL(ata_std_bios_param);
Jeff Garzikcca39742006-08-24 03:19:22 -04006801EXPORT_SYMBOL_GPL(ata_host_init);
Tejun Heof3187192007-04-17 23:44:07 +09006802EXPORT_SYMBOL_GPL(ata_host_alloc);
Tejun Heof5cda252007-04-17 23:44:07 +09006803EXPORT_SYMBOL_GPL(ata_host_alloc_pinfo);
Tejun Heob1c72912008-07-31 17:02:43 +09006804EXPORT_SYMBOL_GPL(ata_slave_link_init);
Tejun Heoecef7252007-04-17 23:44:06 +09006805EXPORT_SYMBOL_GPL(ata_host_start);
Tejun Heof3187192007-04-17 23:44:07 +09006806EXPORT_SYMBOL_GPL(ata_host_register);
Tejun Heof5cda252007-04-17 23:44:07 +09006807EXPORT_SYMBOL_GPL(ata_host_activate);
Tejun Heo0529c1592007-01-20 16:00:26 +09006808EXPORT_SYMBOL_GPL(ata_host_detach);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006809EXPORT_SYMBOL_GPL(ata_sg_init);
Tejun Heof686bcb2006-05-15 20:58:05 +09006810EXPORT_SYMBOL_GPL(ata_qc_complete);
Tejun Heodedaf2b2006-05-15 21:03:43 +09006811EXPORT_SYMBOL_GPL(ata_qc_complete_multiple);
Tejun Heo436d34b2008-04-02 17:28:46 +09006812EXPORT_SYMBOL_GPL(atapi_cmd_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006813EXPORT_SYMBOL_GPL(ata_tf_to_fis);
6814EXPORT_SYMBOL_GPL(ata_tf_from_fis);
Tejun Heo63573572007-11-27 19:43:39 +09006815EXPORT_SYMBOL_GPL(ata_pack_xfermask);
6816EXPORT_SYMBOL_GPL(ata_unpack_xfermask);
6817EXPORT_SYMBOL_GPL(ata_xfer_mask2mode);
6818EXPORT_SYMBOL_GPL(ata_xfer_mode2mask);
6819EXPORT_SYMBOL_GPL(ata_xfer_mode2shift);
6820EXPORT_SYMBOL_GPL(ata_mode_string);
6821EXPORT_SYMBOL_GPL(ata_id_xfermask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006822EXPORT_SYMBOL_GPL(ata_port_start);
Alan04351822007-03-06 02:37:52 -08006823EXPORT_SYMBOL_GPL(ata_do_set_mode);
Tejun Heo31cc23b2007-09-23 13:14:12 +09006824EXPORT_SYMBOL_GPL(ata_std_qc_defer);
Brian Kinge46834c2006-03-17 17:04:03 -06006825EXPORT_SYMBOL_GPL(ata_noop_qc_prep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006826EXPORT_SYMBOL_GPL(ata_port_probe);
Alan10305f02007-02-20 18:01:59 +00006827EXPORT_SYMBOL_GPL(ata_dev_disable);
Tejun Heo3c567b72006-05-15 20:57:23 +09006828EXPORT_SYMBOL_GPL(sata_set_spd);
Tejun Heoaa2731a2008-04-07 22:47:19 +09006829EXPORT_SYMBOL_GPL(ata_wait_after_reset);
Tejun Heo936fd732007-08-06 18:36:23 +09006830EXPORT_SYMBOL_GPL(sata_link_debounce);
6831EXPORT_SYMBOL_GPL(sata_link_resume);
Tejun Heo0aa11132008-04-07 22:47:18 +09006832EXPORT_SYMBOL_GPL(ata_std_prereset);
Tejun Heocc0680a2007-08-06 18:36:23 +09006833EXPORT_SYMBOL_GPL(sata_link_hardreset);
Tejun Heo57c9efd2008-04-07 22:47:19 +09006834EXPORT_SYMBOL_GPL(sata_std_hardreset);
Tejun Heo203c75b2008-04-07 22:47:18 +09006835EXPORT_SYMBOL_GPL(ata_std_postreset);
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05006836EXPORT_SYMBOL_GPL(ata_dev_classify);
6837EXPORT_SYMBOL_GPL(ata_dev_pair);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006838EXPORT_SYMBOL_GPL(ata_port_disable);
Jeff Garzik67846b32005-10-05 02:58:32 -04006839EXPORT_SYMBOL_GPL(ata_ratelimit);
Tejun Heoc22daff2006-04-11 22:22:29 +09006840EXPORT_SYMBOL_GPL(ata_wait_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006841EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006842EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
Tejun Heo83c47bc2006-05-31 18:28:07 +09006843EXPORT_SYMBOL_GPL(ata_scsi_slave_destroy);
Tejun Heoa6e6ce82006-05-15 21:03:48 +09006844EXPORT_SYMBOL_GPL(ata_scsi_change_queue_depth);
Tejun Heo34bf2172006-05-15 20:57:46 +09006845EXPORT_SYMBOL_GPL(sata_scr_valid);
6846EXPORT_SYMBOL_GPL(sata_scr_read);
6847EXPORT_SYMBOL_GPL(sata_scr_write);
6848EXPORT_SYMBOL_GPL(sata_scr_write_flush);
Tejun Heo936fd732007-08-06 18:36:23 +09006849EXPORT_SYMBOL_GPL(ata_link_online);
6850EXPORT_SYMBOL_GPL(ata_link_offline);
Tejun Heo6ffa01d2007-03-02 17:32:47 +09006851#ifdef CONFIG_PM
Jeff Garzikcca39742006-08-24 03:19:22 -04006852EXPORT_SYMBOL_GPL(ata_host_suspend);
6853EXPORT_SYMBOL_GPL(ata_host_resume);
Tejun Heo6ffa01d2007-03-02 17:32:47 +09006854#endif /* CONFIG_PM */
Tejun Heo6a62a042006-02-13 10:02:46 +09006855EXPORT_SYMBOL_GPL(ata_id_string);
6856EXPORT_SYMBOL_GPL(ata_id_c_string);
Alan Cox963e4972008-07-24 17:16:06 +01006857EXPORT_SYMBOL_GPL(ata_do_dev_read_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006858EXPORT_SYMBOL_GPL(ata_scsi_simulate);
6859
Mark Lord1a660162009-02-25 15:18:32 -05006860EXPORT_SYMBOL_GPL(ata_pio_queue_task);
Alan Cox1bc4ccf2006-01-09 17:18:14 +00006861EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
Tejun Heo63573572007-11-27 19:43:39 +09006862EXPORT_SYMBOL_GPL(ata_timing_find_mode);
Alan Cox452503f2005-10-21 19:01:32 -04006863EXPORT_SYMBOL_GPL(ata_timing_compute);
6864EXPORT_SYMBOL_GPL(ata_timing_merge);
Tejun Heoa0f79b92007-12-18 16:33:05 +09006865EXPORT_SYMBOL_GPL(ata_timing_cycle2mode);
Alan Cox452503f2005-10-21 19:01:32 -04006866
Linus Torvalds1da177e2005-04-16 15:20:36 -07006867#ifdef CONFIG_PCI
6868EXPORT_SYMBOL_GPL(pci_test_config_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006869EXPORT_SYMBOL_GPL(ata_pci_remove_one);
Tejun Heo6ffa01d2007-03-02 17:32:47 +09006870#ifdef CONFIG_PM
Tejun Heo500530f2006-07-03 16:07:27 +09006871EXPORT_SYMBOL_GPL(ata_pci_device_do_suspend);
6872EXPORT_SYMBOL_GPL(ata_pci_device_do_resume);
Jens Axboe9b847542006-01-06 09:28:07 +01006873EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
6874EXPORT_SYMBOL_GPL(ata_pci_device_resume);
Tejun Heo6ffa01d2007-03-02 17:32:47 +09006875#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006876#endif /* CONFIG_PCI */
Jens Axboe9b847542006-01-06 09:28:07 +01006877
Tejun Heob64bbc32007-07-16 14:29:39 +09006878EXPORT_SYMBOL_GPL(__ata_ehi_push_desc);
6879EXPORT_SYMBOL_GPL(ata_ehi_push_desc);
6880EXPORT_SYMBOL_GPL(ata_ehi_clear_desc);
Tejun Heocbcdd872007-08-18 13:14:55 +09006881EXPORT_SYMBOL_GPL(ata_port_desc);
6882#ifdef CONFIG_PCI
6883EXPORT_SYMBOL_GPL(ata_port_pbar_desc);
6884#endif /* CONFIG_PCI */
Tejun Heo7b70fc02006-05-15 20:58:07 +09006885EXPORT_SYMBOL_GPL(ata_port_schedule_eh);
Tejun Heodbd82612007-08-06 18:36:23 +09006886EXPORT_SYMBOL_GPL(ata_link_abort);
Tejun Heo7b70fc02006-05-15 20:58:07 +09006887EXPORT_SYMBOL_GPL(ata_port_abort);
Tejun Heoe3180492006-05-15 20:58:09 +09006888EXPORT_SYMBOL_GPL(ata_port_freeze);
Tejun Heo7d77b242007-09-23 13:14:13 +09006889EXPORT_SYMBOL_GPL(sata_async_notification);
Tejun Heoe3180492006-05-15 20:58:09 +09006890EXPORT_SYMBOL_GPL(ata_eh_freeze_port);
6891EXPORT_SYMBOL_GPL(ata_eh_thaw_port);
Tejun Heoece1d632006-04-02 18:51:53 +09006892EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
6893EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
Mark Lord10acf3b2008-05-02 02:14:53 -04006894EXPORT_SYMBOL_GPL(ata_eh_analyze_ncq_error);
Tejun Heo022bdb02006-05-15 20:58:22 +09006895EXPORT_SYMBOL_GPL(ata_do_eh);
Tejun Heoa1efdab2008-03-25 12:22:50 +09006896EXPORT_SYMBOL_GPL(ata_std_error_handler);
Alan Coxbe0d18d2007-03-06 02:37:56 -08006897
6898EXPORT_SYMBOL_GPL(ata_cable_40wire);
6899EXPORT_SYMBOL_GPL(ata_cable_80wire);
6900EXPORT_SYMBOL_GPL(ata_cable_unknown);
Tejun Heoc88f90c2007-11-27 19:43:48 +09006901EXPORT_SYMBOL_GPL(ata_cable_ignore);
Alan Coxbe0d18d2007-03-06 02:37:56 -08006902EXPORT_SYMBOL_GPL(ata_cable_sata);