blob: b8024451234c10922d9aee295752cbad824701b3 [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 Heo140b5e52007-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"
Gwendal Grignoud9027472010-05-25 12:31:38 -070071#include "libata-transport.h"
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);
94static void ata_dev_xfermask(struct ata_device *dev);
Tejun Heo75683fe2007-07-05 13:31:27 +090095static unsigned long ata_dev_blacklisted(const struct ata_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Tejun Heof3187192007-04-17 23:44:07 +090097unsigned int ata_print_id = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Tejun Heo33267322008-02-13 09:15:09 +090099struct ata_force_param {
100 const char *name;
101 unsigned int cbl;
102 int spd_limit;
103 unsigned long xfer_mask;
104 unsigned int horkage_on;
105 unsigned int horkage_off;
Tejun Heo05944bd2008-08-13 20:19:09 +0900106 unsigned int lflags;
Tejun Heo33267322008-02-13 09:15:09 +0900107};
108
109struct ata_force_ent {
110 int port;
111 int device;
112 struct ata_force_param param;
113};
114
115static struct ata_force_ent *ata_force_tbl;
116static int ata_force_tbl_size;
117
118static char ata_force_param_buf[PAGE_SIZE] __initdata;
Tejun Heo7afb4222008-03-09 20:21:53 +0900119/* param_buf is thrown away after initialization, disallow read */
120module_param_string(force, ata_force_param_buf, sizeof(ata_force_param_buf), 0);
Tejun Heo33267322008-02-13 09:15:09 +0900121MODULE_PARM_DESC(force, "Force ATA configurations including cable type, link speed and transfer mode (see Documentation/kernel-parameters.txt for details)");
122
Tejun Heo2486fa52008-07-31 07:52:40 +0900123static int atapi_enabled = 1;
Jeff Garzik1623c812005-08-30 03:37:42 -0400124module_param(atapi_enabled, int, 0444);
Evgeni Golovad5d8ea2009-06-22 09:46:20 +0200125MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on [default])");
Jeff Garzik1623c812005-08-30 03:37:42 -0400126
Adrian Bunkc5c61bd2008-02-25 02:07:25 +0200127static int atapi_dmadir = 0;
Albert Lee95de7192006-04-04 10:57:18 +0800128module_param(atapi_dmadir, int, 0444);
Evgeni Golovad5d8ea2009-06-22 09:46:20 +0200129MODULE_PARM_DESC(atapi_dmadir, "Enable ATAPI DMADIR bridge support (0=off [default], 1=on)");
Albert Lee95de7192006-04-04 10:57:18 +0800130
Mark Lordbaf4fdf2007-08-08 01:08:45 +0900131int atapi_passthru16 = 1;
132module_param(atapi_passthru16, int, 0444);
Evgeni Golovad5d8ea2009-06-22 09:46:20 +0200133MODULE_PARM_DESC(atapi_passthru16, "Enable ATA_16 passthru for ATAPI devices (0=off, 1=on [default])");
Mark Lordbaf4fdf2007-08-08 01:08:45 +0900134
Jeff Garzikc3c013a2006-02-27 22:31:19 -0500135int libata_fua = 0;
136module_param_named(fua, libata_fua, int, 0444);
Evgeni Golovad5d8ea2009-06-22 09:46:20 +0200137MODULE_PARM_DESC(fua, "FUA support (0=off [default], 1=on)");
Jeff Garzikc3c013a2006-02-27 22:31:19 -0500138
Jeff Garzik2dcb4072007-10-19 06:42:56 -0400139static int ata_ignore_hpa;
Alan Cox1e999732007-04-11 00:23:13 +0100140module_param_named(ignore_hpa, ata_ignore_hpa, int, 0644);
141MODULE_PARM_DESC(ignore_hpa, "Ignore HPA limit (0=keep BIOS limits, 1=ignore limits, using full disk)");
142
Alan Coxb3a70602007-10-02 12:38:26 -0400143static int libata_dma_mask = ATA_DMA_MASK_ATA|ATA_DMA_MASK_ATAPI|ATA_DMA_MASK_CFA;
144module_param_named(dma, libata_dma_mask, int, 0444);
145MODULE_PARM_DESC(dma, "DMA enable/disable (0x1==ATA, 0x2==ATAPI, 0x4==CF)");
146
Tejun Heo87fbc5a2008-05-20 02:17:54 +0900147static int ata_probe_timeout;
Andrew Mortona8601e52006-06-25 01:36:52 -0700148module_param(ata_probe_timeout, int, 0444);
149MODULE_PARM_DESC(ata_probe_timeout, "Set ATA probing timeout (seconds)");
150
Jeff Garzik6ebe9d82007-10-05 16:28:36 -0400151int libata_noacpi = 0;
Jeff Garzikd7d0dad2007-03-28 01:57:37 -0400152module_param_named(noacpi, libata_noacpi, int, 0444);
Evgeni Golovad5d8ea2009-06-22 09:46:20 +0200153MODULE_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 -0700154
Alan Coxae8d4ee2007-11-04 22:05:49 -0500155int libata_allow_tpm = 0;
156module_param_named(allow_tpm, libata_allow_tpm, int, 0444);
Evgeni Golovad5d8ea2009-06-22 09:46:20 +0200157MODULE_PARM_DESC(allow_tpm, "Permit the use of TPM commands (0=off [default], 1=on)");
Alan Coxae8d4ee2007-11-04 22:05:49 -0500158
Tejun Heoe7ecd432010-05-19 15:38:58 +0200159static int atapi_an;
160module_param(atapi_an, int, 0444);
161MODULE_PARM_DESC(atapi_an, "Enable ATAPI AN media presence notification (0=0ff [default], 1=on)");
162
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163MODULE_AUTHOR("Jeff Garzik");
164MODULE_DESCRIPTION("Library module for ATA devices");
165MODULE_LICENSE("GPL");
166MODULE_VERSION(DRV_VERSION);
167
Edward Falk0baab862005-06-02 18:17:13 -0400168
Tejun Heo9913ff82009-01-29 20:31:31 +0900169static bool ata_sstatus_online(u32 sstatus)
170{
171 return (sstatus & 0xf) == 0x3;
172}
173
Tejun Heo1eca4362008-11-03 20:03:17 +0900174/**
175 * ata_link_next - link iteration helper
176 * @link: the previous link, NULL to start
177 * @ap: ATA port containing links to iterate
178 * @mode: iteration mode, one of ATA_LITER_*
Tejun Heoaadffb62008-07-31 17:02:41 +0900179 *
Tejun Heo1eca4362008-11-03 20:03:17 +0900180 * LOCKING:
181 * Host lock or EH context.
182 *
183 * RETURNS:
184 * Pointer to the next link.
Tejun Heoaadffb62008-07-31 17:02:41 +0900185 */
Tejun Heo1eca4362008-11-03 20:03:17 +0900186struct ata_link *ata_link_next(struct ata_link *link, struct ata_port *ap,
187 enum ata_link_iter_mode mode)
Tejun Heoaadffb62008-07-31 17:02:41 +0900188{
Tejun Heo1eca4362008-11-03 20:03:17 +0900189 BUG_ON(mode != ATA_LITER_EDGE &&
190 mode != ATA_LITER_PMP_FIRST && mode != ATA_LITER_HOST_FIRST);
Tejun Heoaadffb62008-07-31 17:02:41 +0900191
Tejun Heo1eca4362008-11-03 20:03:17 +0900192 /* NULL link indicates start of iteration */
193 if (!link)
194 switch (mode) {
195 case ATA_LITER_EDGE:
196 case ATA_LITER_PMP_FIRST:
197 if (sata_pmp_attached(ap))
198 return ap->pmp_link;
199 /* fall through */
200 case ATA_LITER_HOST_FIRST:
201 return &ap->link;
202 }
203
204 /* we just iterated over the host link, what's next? */
205 if (link == &ap->link)
206 switch (mode) {
207 case ATA_LITER_HOST_FIRST:
208 if (sata_pmp_attached(ap))
209 return ap->pmp_link;
210 /* fall through */
211 case ATA_LITER_PMP_FIRST:
212 if (unlikely(ap->slave_link))
Tejun Heob1c72912008-07-31 17:02:43 +0900213 return ap->slave_link;
Tejun Heo1eca4362008-11-03 20:03:17 +0900214 /* fall through */
215 case ATA_LITER_EDGE:
Tejun Heoaadffb62008-07-31 17:02:41 +0900216 return NULL;
Tejun Heob1c72912008-07-31 17:02:43 +0900217 }
Tejun Heoaadffb62008-07-31 17:02:41 +0900218
Tejun Heob1c72912008-07-31 17:02:43 +0900219 /* slave_link excludes PMP */
220 if (unlikely(link == ap->slave_link))
221 return NULL;
222
Tejun Heo1eca4362008-11-03 20:03:17 +0900223 /* we were over a PMP link */
Tejun Heoaadffb62008-07-31 17:02:41 +0900224 if (++link < ap->pmp_link + ap->nr_pmp_links)
225 return link;
Tejun Heo1eca4362008-11-03 20:03:17 +0900226
227 if (mode == ATA_LITER_PMP_FIRST)
228 return &ap->link;
229
Tejun Heoaadffb62008-07-31 17:02:41 +0900230 return NULL;
231}
232
Edward Falk0baab862005-06-02 18:17:13 -0400233/**
Tejun Heo1eca4362008-11-03 20:03:17 +0900234 * ata_dev_next - device iteration helper
235 * @dev: the previous device, NULL to start
236 * @link: ATA link containing devices to iterate
237 * @mode: iteration mode, one of ATA_DITER_*
238 *
239 * LOCKING:
240 * Host lock or EH context.
241 *
242 * RETURNS:
243 * Pointer to the next device.
244 */
245struct ata_device *ata_dev_next(struct ata_device *dev, struct ata_link *link,
246 enum ata_dev_iter_mode mode)
247{
248 BUG_ON(mode != ATA_DITER_ENABLED && mode != ATA_DITER_ENABLED_REVERSE &&
249 mode != ATA_DITER_ALL && mode != ATA_DITER_ALL_REVERSE);
250
251 /* NULL dev indicates start of iteration */
252 if (!dev)
253 switch (mode) {
254 case ATA_DITER_ENABLED:
255 case ATA_DITER_ALL:
256 dev = link->device;
257 goto check;
258 case ATA_DITER_ENABLED_REVERSE:
259 case ATA_DITER_ALL_REVERSE:
260 dev = link->device + ata_link_max_devices(link) - 1;
261 goto check;
262 }
263
264 next:
265 /* move to the next one */
266 switch (mode) {
267 case ATA_DITER_ENABLED:
268 case ATA_DITER_ALL:
269 if (++dev < link->device + ata_link_max_devices(link))
270 goto check;
271 return NULL;
272 case ATA_DITER_ENABLED_REVERSE:
273 case ATA_DITER_ALL_REVERSE:
274 if (--dev >= link->device)
275 goto check;
276 return NULL;
277 }
278
279 check:
280 if ((mode == ATA_DITER_ENABLED || mode == ATA_DITER_ENABLED_REVERSE) &&
281 !ata_dev_enabled(dev))
282 goto next;
283 return dev;
284}
285
286/**
Tejun Heob1c72912008-07-31 17:02:43 +0900287 * ata_dev_phys_link - find physical link for a device
288 * @dev: ATA device to look up physical link for
289 *
290 * Look up physical link which @dev is attached to. Note that
291 * this is different from @dev->link only when @dev is on slave
292 * link. For all other cases, it's the same as @dev->link.
293 *
294 * LOCKING:
295 * Don't care.
296 *
297 * RETURNS:
298 * Pointer to the found physical link.
299 */
300struct ata_link *ata_dev_phys_link(struct ata_device *dev)
301{
302 struct ata_port *ap = dev->link->ap;
303
304 if (!ap->slave_link)
305 return dev->link;
306 if (!dev->devno)
307 return &ap->link;
308 return ap->slave_link;
309}
310
311/**
Tejun Heo33267322008-02-13 09:15:09 +0900312 * ata_force_cbl - force cable type according to libata.force
Randy Dunlap4cdfa1b2008-02-22 12:21:37 -0800313 * @ap: ATA port of interest
Tejun Heo33267322008-02-13 09:15:09 +0900314 *
315 * Force cable type according to libata.force and whine about it.
316 * The last entry which has matching port number is used, so it
317 * can be specified as part of device force parameters. For
318 * example, both "a:40c,1.00:udma4" and "1.00:40c,udma4" have the
319 * same effect.
320 *
321 * LOCKING:
322 * EH context.
323 */
324void ata_force_cbl(struct ata_port *ap)
325{
326 int i;
327
328 for (i = ata_force_tbl_size - 1; i >= 0; i--) {
329 const struct ata_force_ent *fe = &ata_force_tbl[i];
330
331 if (fe->port != -1 && fe->port != ap->print_id)
332 continue;
333
334 if (fe->param.cbl == ATA_CBL_NONE)
335 continue;
336
337 ap->cbl = fe->param.cbl;
338 ata_port_printk(ap, KERN_NOTICE,
339 "FORCE: cable set to %s\n", fe->param.name);
340 return;
341 }
342}
343
344/**
Tejun Heo05944bd2008-08-13 20:19:09 +0900345 * ata_force_link_limits - force link limits according to libata.force
Tejun Heo33267322008-02-13 09:15:09 +0900346 * @link: ATA link of interest
347 *
Tejun Heo05944bd2008-08-13 20:19:09 +0900348 * Force link flags and SATA spd limit according to libata.force
349 * and whine about it. When only the port part is specified
350 * (e.g. 1:), the limit applies to all links connected to both
351 * the host link and all fan-out ports connected via PMP. If the
352 * device part is specified as 0 (e.g. 1.00:), it specifies the
353 * first fan-out link not the host link. Device number 15 always
Tejun Heob1c72912008-07-31 17:02:43 +0900354 * points to the host link whether PMP is attached or not. If the
355 * controller has slave link, device number 16 points to it.
Tejun Heo33267322008-02-13 09:15:09 +0900356 *
357 * LOCKING:
358 * EH context.
359 */
Tejun Heo05944bd2008-08-13 20:19:09 +0900360static void ata_force_link_limits(struct ata_link *link)
Tejun Heo33267322008-02-13 09:15:09 +0900361{
Tejun Heo05944bd2008-08-13 20:19:09 +0900362 bool did_spd = false;
Tejun Heob1c72912008-07-31 17:02:43 +0900363 int linkno = link->pmp;
364 int i;
Tejun Heo33267322008-02-13 09:15:09 +0900365
366 if (ata_is_host_link(link))
Tejun Heob1c72912008-07-31 17:02:43 +0900367 linkno += 15;
Tejun Heo33267322008-02-13 09:15:09 +0900368
369 for (i = ata_force_tbl_size - 1; i >= 0; i--) {
370 const struct ata_force_ent *fe = &ata_force_tbl[i];
371
372 if (fe->port != -1 && fe->port != link->ap->print_id)
373 continue;
374
375 if (fe->device != -1 && fe->device != linkno)
376 continue;
377
Tejun Heo05944bd2008-08-13 20:19:09 +0900378 /* only honor the first spd limit */
379 if (!did_spd && fe->param.spd_limit) {
380 link->hw_sata_spd_limit = (1 << fe->param.spd_limit) - 1;
381 ata_link_printk(link, KERN_NOTICE,
382 "FORCE: PHY spd limit set to %s\n",
383 fe->param.name);
384 did_spd = true;
385 }
Tejun Heo33267322008-02-13 09:15:09 +0900386
Tejun Heo05944bd2008-08-13 20:19:09 +0900387 /* let lflags stack */
388 if (fe->param.lflags) {
389 link->flags |= fe->param.lflags;
390 ata_link_printk(link, KERN_NOTICE,
391 "FORCE: link flag 0x%x forced -> 0x%x\n",
392 fe->param.lflags, link->flags);
393 }
Tejun Heo33267322008-02-13 09:15:09 +0900394 }
395}
396
397/**
398 * ata_force_xfermask - force xfermask according to libata.force
399 * @dev: ATA device of interest
400 *
401 * Force xfer_mask according to libata.force and whine about it.
402 * For consistency with link selection, device number 15 selects
403 * the first device connected to the host link.
404 *
405 * LOCKING:
406 * EH context.
407 */
408static void ata_force_xfermask(struct ata_device *dev)
409{
410 int devno = dev->link->pmp + dev->devno;
411 int alt_devno = devno;
412 int i;
413
Tejun Heob1c72912008-07-31 17:02:43 +0900414 /* allow n.15/16 for devices attached to host port */
415 if (ata_is_host_link(dev->link))
416 alt_devno += 15;
Tejun Heo33267322008-02-13 09:15:09 +0900417
418 for (i = ata_force_tbl_size - 1; i >= 0; i--) {
419 const struct ata_force_ent *fe = &ata_force_tbl[i];
420 unsigned long pio_mask, mwdma_mask, udma_mask;
421
422 if (fe->port != -1 && fe->port != dev->link->ap->print_id)
423 continue;
424
425 if (fe->device != -1 && fe->device != devno &&
426 fe->device != alt_devno)
427 continue;
428
429 if (!fe->param.xfer_mask)
430 continue;
431
432 ata_unpack_xfermask(fe->param.xfer_mask,
433 &pio_mask, &mwdma_mask, &udma_mask);
434 if (udma_mask)
435 dev->udma_mask = udma_mask;
436 else if (mwdma_mask) {
437 dev->udma_mask = 0;
438 dev->mwdma_mask = mwdma_mask;
439 } else {
440 dev->udma_mask = 0;
441 dev->mwdma_mask = 0;
442 dev->pio_mask = pio_mask;
443 }
444
445 ata_dev_printk(dev, KERN_NOTICE,
446 "FORCE: xfer_mask set to %s\n", fe->param.name);
447 return;
448 }
449}
450
451/**
452 * ata_force_horkage - force horkage according to libata.force
453 * @dev: ATA device of interest
454 *
455 * Force horkage according to libata.force and whine about it.
456 * For consistency with link selection, device number 15 selects
457 * the first device connected to the host link.
458 *
459 * LOCKING:
460 * EH context.
461 */
462static void ata_force_horkage(struct ata_device *dev)
463{
464 int devno = dev->link->pmp + dev->devno;
465 int alt_devno = devno;
466 int i;
467
Tejun Heob1c72912008-07-31 17:02:43 +0900468 /* allow n.15/16 for devices attached to host port */
469 if (ata_is_host_link(dev->link))
470 alt_devno += 15;
Tejun Heo33267322008-02-13 09:15:09 +0900471
472 for (i = 0; i < ata_force_tbl_size; i++) {
473 const struct ata_force_ent *fe = &ata_force_tbl[i];
474
475 if (fe->port != -1 && fe->port != dev->link->ap->print_id)
476 continue;
477
478 if (fe->device != -1 && fe->device != devno &&
479 fe->device != alt_devno)
480 continue;
481
482 if (!(~dev->horkage & fe->param.horkage_on) &&
483 !(dev->horkage & fe->param.horkage_off))
484 continue;
485
486 dev->horkage |= fe->param.horkage_on;
487 dev->horkage &= ~fe->param.horkage_off;
488
489 ata_dev_printk(dev, KERN_NOTICE,
490 "FORCE: horkage modified (%s)\n", fe->param.name);
491 }
492}
493
494/**
Tejun Heo436d34b2008-04-02 17:28:46 +0900495 * atapi_cmd_type - Determine ATAPI command type from SCSI opcode
496 * @opcode: SCSI opcode
497 *
498 * Determine ATAPI command type from @opcode.
499 *
500 * LOCKING:
501 * None.
502 *
503 * RETURNS:
504 * ATAPI_{READ|WRITE|READ_CD|PASS_THRU|MISC}
505 */
506int atapi_cmd_type(u8 opcode)
507{
508 switch (opcode) {
509 case GPCMD_READ_10:
510 case GPCMD_READ_12:
511 return ATAPI_READ;
512
513 case GPCMD_WRITE_10:
514 case GPCMD_WRITE_12:
515 case GPCMD_WRITE_AND_VERIFY_10:
516 return ATAPI_WRITE;
517
518 case GPCMD_READ_CD:
519 case GPCMD_READ_CD_MSF:
520 return ATAPI_READ_CD;
521
Tejun Heoe52dcc42008-04-02 17:35:19 +0900522 case ATA_16:
523 case ATA_12:
524 if (atapi_passthru16)
525 return ATAPI_PASS_THRU;
526 /* fall thru */
Tejun Heo436d34b2008-04-02 17:28:46 +0900527 default:
528 return ATAPI_MISC;
529 }
530}
531
532/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
534 * @tf: Taskfile to convert
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 * @pmp: Port multiplier port
Tejun Heo99771262007-07-16 14:29:38 +0900536 * @is_cmd: This FIS is for command
537 * @fis: Buffer into which data will output
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 *
539 * Converts a standard ATA taskfile to a Serial ATA
540 * FIS structure (Register - Host to Device).
541 *
542 * LOCKING:
543 * Inherited from caller.
544 */
Tejun Heo99771262007-07-16 14:29:38 +0900545void ata_tf_to_fis(const struct ata_taskfile *tf, u8 pmp, int is_cmd, u8 *fis)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546{
Tejun Heo99771262007-07-16 14:29:38 +0900547 fis[0] = 0x27; /* Register - Host to Device FIS */
548 fis[1] = pmp & 0xf; /* Port multiplier number*/
549 if (is_cmd)
550 fis[1] |= (1 << 7); /* bit 7 indicates Command FIS */
551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 fis[2] = tf->command;
553 fis[3] = tf->feature;
554
555 fis[4] = tf->lbal;
556 fis[5] = tf->lbam;
557 fis[6] = tf->lbah;
558 fis[7] = tf->device;
559
560 fis[8] = tf->hob_lbal;
561 fis[9] = tf->hob_lbam;
562 fis[10] = tf->hob_lbah;
563 fis[11] = tf->hob_feature;
564
565 fis[12] = tf->nsect;
566 fis[13] = tf->hob_nsect;
567 fis[14] = 0;
568 fis[15] = tf->ctl;
569
570 fis[16] = 0;
571 fis[17] = 0;
572 fis[18] = 0;
573 fis[19] = 0;
574}
575
576/**
577 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
578 * @fis: Buffer from which data will be input
579 * @tf: Taskfile to output
580 *
Mark Lorde12a1be2005-11-12 18:55:45 -0500581 * Converts a serial ATA FIS structure to a standard ATA taskfile.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 *
583 * LOCKING:
584 * Inherited from caller.
585 */
586
Jeff Garzik057ace52005-10-22 14:27:05 -0400587void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588{
589 tf->command = fis[2]; /* status */
590 tf->feature = fis[3]; /* error */
591
592 tf->lbal = fis[4];
593 tf->lbam = fis[5];
594 tf->lbah = fis[6];
595 tf->device = fis[7];
596
597 tf->hob_lbal = fis[8];
598 tf->hob_lbam = fis[9];
599 tf->hob_lbah = fis[10];
600
601 tf->nsect = fis[12];
602 tf->hob_nsect = fis[13];
603}
604
Albert Lee8cbd6df2005-10-12 15:06:27 +0800605static const u8 ata_rw_cmds[] = {
606 /* pio multi */
607 ATA_CMD_READ_MULTI,
608 ATA_CMD_WRITE_MULTI,
609 ATA_CMD_READ_MULTI_EXT,
610 ATA_CMD_WRITE_MULTI_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100611 0,
612 0,
613 0,
614 ATA_CMD_WRITE_MULTI_FUA_EXT,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800615 /* pio */
616 ATA_CMD_PIO_READ,
617 ATA_CMD_PIO_WRITE,
618 ATA_CMD_PIO_READ_EXT,
619 ATA_CMD_PIO_WRITE_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100620 0,
621 0,
622 0,
623 0,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800624 /* dma */
625 ATA_CMD_READ,
626 ATA_CMD_WRITE,
627 ATA_CMD_READ_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100628 ATA_CMD_WRITE_EXT,
629 0,
630 0,
631 0,
632 ATA_CMD_WRITE_FUA_EXT
Albert Lee8cbd6df2005-10-12 15:06:27 +0800633};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634
635/**
Albert Lee8cbd6df2005-10-12 15:06:27 +0800636 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
Tejun Heobd056d72006-11-14 22:47:10 +0900637 * @tf: command to examine and configure
638 * @dev: device tf belongs to
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 *
Jeff Garzik2e9edbf2006-03-24 09:56:57 -0500640 * Examine the device configuration and tf->flags to calculate
Albert Lee8cbd6df2005-10-12 15:06:27 +0800641 * the proper read/write commands and protocol to use.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 *
643 * LOCKING:
644 * caller.
645 */
Tejun Heobd056d72006-11-14 22:47:10 +0900646static int ata_rwcmd_protocol(struct ata_taskfile *tf, struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100648 u8 cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100650 int index, fua, lba48, write;
Jeff Garzik2e9edbf2006-03-24 09:56:57 -0500651
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100652 fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800653 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
654 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
Albert Lee8cbd6df2005-10-12 15:06:27 +0800656 if (dev->flags & ATA_DFLAG_PIO) {
657 tf->protocol = ATA_PROT_PIO;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100658 index = dev->multi_count ? 0 : 8;
Tejun Heo9af5c9c2007-08-06 18:36:22 +0900659 } else if (lba48 && (dev->link->ap->flags & ATA_FLAG_PIO_LBA48)) {
Alan Cox8d238e02006-01-17 20:50:31 +0000660 /* Unable to use DMA due to host limitation */
661 tf->protocol = ATA_PROT_PIO;
Albert Lee0565c262006-02-13 18:55:25 +0800662 index = dev->multi_count ? 0 : 8;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800663 } else {
664 tf->protocol = ATA_PROT_DMA;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100665 index = 16;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800666 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100668 cmd = ata_rw_cmds[index + fua + lba48 + write];
669 if (cmd) {
670 tf->command = cmd;
671 return 0;
672 }
673 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674}
675
Tejun Heocb95d562006-03-06 04:31:56 +0900676/**
Tejun Heo35b649f2006-11-14 22:37:35 +0900677 * ata_tf_read_block - Read block address from ATA taskfile
678 * @tf: ATA taskfile of interest
679 * @dev: ATA device @tf belongs to
680 *
681 * LOCKING:
682 * None.
683 *
684 * Read block address from @tf. This function can handle all
685 * three address formats - LBA, LBA48 and CHS. tf->protocol and
686 * flags select the address format to use.
687 *
688 * RETURNS:
689 * Block address read from @tf.
690 */
691u64 ata_tf_read_block(struct ata_taskfile *tf, struct ata_device *dev)
692{
693 u64 block = 0;
694
695 if (tf->flags & ATA_TFLAG_LBA) {
696 if (tf->flags & ATA_TFLAG_LBA48) {
697 block |= (u64)tf->hob_lbah << 40;
698 block |= (u64)tf->hob_lbam << 32;
Roland Dreier44901a92008-11-04 10:34:48 -0800699 block |= (u64)tf->hob_lbal << 24;
Tejun Heo35b649f2006-11-14 22:37:35 +0900700 } else
701 block |= (tf->device & 0xf) << 24;
702
703 block |= tf->lbah << 16;
704 block |= tf->lbam << 8;
705 block |= tf->lbal;
706 } else {
707 u32 cyl, head, sect;
708
709 cyl = tf->lbam | (tf->lbah << 8);
710 head = tf->device & 0xf;
711 sect = tf->lbal;
712
Tejun Heoac8672e2009-08-16 21:21:21 +0900713 if (!sect) {
714 ata_dev_printk(dev, KERN_WARNING, "device reported "
715 "invalid CHS sector 0\n");
716 sect = 1; /* oh well */
717 }
718
719 block = (cyl * dev->heads + head) * dev->sectors + sect - 1;
Tejun Heo35b649f2006-11-14 22:37:35 +0900720 }
721
722 return block;
723}
724
725/**
Tejun Heobd056d72006-11-14 22:47:10 +0900726 * ata_build_rw_tf - Build ATA taskfile for given read/write request
727 * @tf: Target ATA taskfile
728 * @dev: ATA device @tf belongs to
729 * @block: Block address
730 * @n_block: Number of blocks
731 * @tf_flags: RW/FUA etc...
732 * @tag: tag
733 *
734 * LOCKING:
735 * None.
736 *
737 * Build ATA taskfile @tf for read/write request described by
738 * @block, @n_block, @tf_flags and @tag on @dev.
739 *
740 * RETURNS:
741 *
742 * 0 on success, -ERANGE if the request is too large for @dev,
743 * -EINVAL if the request is invalid.
744 */
745int ata_build_rw_tf(struct ata_taskfile *tf, struct ata_device *dev,
746 u64 block, u32 n_block, unsigned int tf_flags,
747 unsigned int tag)
748{
749 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
750 tf->flags |= tf_flags;
751
Tejun Heo6d1245b2007-02-20 23:20:27 +0900752 if (ata_ncq_enabled(dev) && likely(tag != ATA_TAG_INTERNAL)) {
Tejun Heobd056d72006-11-14 22:47:10 +0900753 /* yay, NCQ */
754 if (!lba_48_ok(block, n_block))
755 return -ERANGE;
756
757 tf->protocol = ATA_PROT_NCQ;
758 tf->flags |= ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
759
760 if (tf->flags & ATA_TFLAG_WRITE)
761 tf->command = ATA_CMD_FPDMA_WRITE;
762 else
763 tf->command = ATA_CMD_FPDMA_READ;
764
765 tf->nsect = tag << 3;
766 tf->hob_feature = (n_block >> 8) & 0xff;
767 tf->feature = n_block & 0xff;
768
769 tf->hob_lbah = (block >> 40) & 0xff;
770 tf->hob_lbam = (block >> 32) & 0xff;
771 tf->hob_lbal = (block >> 24) & 0xff;
772 tf->lbah = (block >> 16) & 0xff;
773 tf->lbam = (block >> 8) & 0xff;
774 tf->lbal = block & 0xff;
775
776 tf->device = 1 << 6;
777 if (tf->flags & ATA_TFLAG_FUA)
778 tf->device |= 1 << 7;
779 } else if (dev->flags & ATA_DFLAG_LBA) {
780 tf->flags |= ATA_TFLAG_LBA;
781
782 if (lba_28_ok(block, n_block)) {
783 /* use LBA28 */
784 tf->device |= (block >> 24) & 0xf;
785 } else if (lba_48_ok(block, n_block)) {
786 if (!(dev->flags & ATA_DFLAG_LBA48))
787 return -ERANGE;
788
789 /* use LBA48 */
790 tf->flags |= ATA_TFLAG_LBA48;
791
792 tf->hob_nsect = (n_block >> 8) & 0xff;
793
794 tf->hob_lbah = (block >> 40) & 0xff;
795 tf->hob_lbam = (block >> 32) & 0xff;
796 tf->hob_lbal = (block >> 24) & 0xff;
797 } else
798 /* request too large even for LBA48 */
799 return -ERANGE;
800
801 if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
802 return -EINVAL;
803
804 tf->nsect = n_block & 0xff;
805
806 tf->lbah = (block >> 16) & 0xff;
807 tf->lbam = (block >> 8) & 0xff;
808 tf->lbal = block & 0xff;
809
810 tf->device |= ATA_LBA;
811 } else {
812 /* CHS */
813 u32 sect, head, cyl, track;
814
815 /* The request -may- be too large for CHS addressing. */
816 if (!lba_28_ok(block, n_block))
817 return -ERANGE;
818
819 if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
820 return -EINVAL;
821
822 /* Convert LBA to CHS */
823 track = (u32)block / dev->sectors;
824 cyl = track / dev->heads;
825 head = track % dev->heads;
826 sect = (u32)block % dev->sectors + 1;
827
828 DPRINTK("block %u track %u cyl %u head %u sect %u\n",
829 (u32)block, track, cyl, head, sect);
830
831 /* Check whether the converted CHS can fit.
832 Cylinder: 0-65535
833 Head: 0-15
834 Sector: 1-255*/
835 if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
836 return -ERANGE;
837
838 tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
839 tf->lbal = sect;
840 tf->lbam = cyl;
841 tf->lbah = cyl >> 8;
842 tf->device |= head;
843 }
844
845 return 0;
846}
847
848/**
Tejun Heocb95d562006-03-06 04:31:56 +0900849 * ata_pack_xfermask - Pack pio, mwdma and udma masks into xfer_mask
850 * @pio_mask: pio_mask
851 * @mwdma_mask: mwdma_mask
852 * @udma_mask: udma_mask
853 *
854 * Pack @pio_mask, @mwdma_mask and @udma_mask into a single
855 * unsigned int xfer_mask.
856 *
857 * LOCKING:
858 * None.
859 *
860 * RETURNS:
861 * Packed xfer_mask.
862 */
Tejun Heo7dc951a2007-11-27 19:43:42 +0900863unsigned long ata_pack_xfermask(unsigned long pio_mask,
864 unsigned long mwdma_mask,
865 unsigned long udma_mask)
Tejun Heocb95d562006-03-06 04:31:56 +0900866{
867 return ((pio_mask << ATA_SHIFT_PIO) & ATA_MASK_PIO) |
868 ((mwdma_mask << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA) |
869 ((udma_mask << ATA_SHIFT_UDMA) & ATA_MASK_UDMA);
870}
871
Tejun Heoc0489e42006-03-24 14:07:49 +0900872/**
873 * ata_unpack_xfermask - Unpack xfer_mask into pio, mwdma and udma masks
874 * @xfer_mask: xfer_mask to unpack
875 * @pio_mask: resulting pio_mask
876 * @mwdma_mask: resulting mwdma_mask
877 * @udma_mask: resulting udma_mask
878 *
879 * Unpack @xfer_mask into @pio_mask, @mwdma_mask and @udma_mask.
880 * Any NULL distination masks will be ignored.
881 */
Tejun Heo7dc951a2007-11-27 19:43:42 +0900882void ata_unpack_xfermask(unsigned long xfer_mask, unsigned long *pio_mask,
883 unsigned long *mwdma_mask, unsigned long *udma_mask)
Tejun Heoc0489e42006-03-24 14:07:49 +0900884{
885 if (pio_mask)
886 *pio_mask = (xfer_mask & ATA_MASK_PIO) >> ATA_SHIFT_PIO;
887 if (mwdma_mask)
888 *mwdma_mask = (xfer_mask & ATA_MASK_MWDMA) >> ATA_SHIFT_MWDMA;
889 if (udma_mask)
890 *udma_mask = (xfer_mask & ATA_MASK_UDMA) >> ATA_SHIFT_UDMA;
891}
892
Tejun Heocb95d562006-03-06 04:31:56 +0900893static const struct ata_xfer_ent {
Tejun Heobe9a50c2006-03-31 22:48:52 +0900894 int shift, bits;
Tejun Heocb95d562006-03-06 04:31:56 +0900895 u8 base;
896} ata_xfer_tbl[] = {
Tejun Heo70cd0712007-11-27 19:43:40 +0900897 { ATA_SHIFT_PIO, ATA_NR_PIO_MODES, XFER_PIO_0 },
898 { ATA_SHIFT_MWDMA, ATA_NR_MWDMA_MODES, XFER_MW_DMA_0 },
899 { ATA_SHIFT_UDMA, ATA_NR_UDMA_MODES, XFER_UDMA_0 },
Tejun Heocb95d562006-03-06 04:31:56 +0900900 { -1, },
901};
902
903/**
904 * ata_xfer_mask2mode - Find matching XFER_* for the given xfer_mask
905 * @xfer_mask: xfer_mask of interest
906 *
907 * Return matching XFER_* value for @xfer_mask. Only the highest
908 * bit of @xfer_mask is considered.
909 *
910 * LOCKING:
911 * None.
912 *
913 * RETURNS:
Tejun Heo70cd0712007-11-27 19:43:40 +0900914 * Matching XFER_* value, 0xff if no match found.
Tejun Heocb95d562006-03-06 04:31:56 +0900915 */
Tejun Heo7dc951a2007-11-27 19:43:42 +0900916u8 ata_xfer_mask2mode(unsigned long xfer_mask)
Tejun Heocb95d562006-03-06 04:31:56 +0900917{
918 int highbit = fls(xfer_mask) - 1;
919 const struct ata_xfer_ent *ent;
920
921 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
922 if (highbit >= ent->shift && highbit < ent->shift + ent->bits)
923 return ent->base + highbit - ent->shift;
Tejun Heo70cd0712007-11-27 19:43:40 +0900924 return 0xff;
Tejun Heocb95d562006-03-06 04:31:56 +0900925}
926
927/**
928 * ata_xfer_mode2mask - Find matching xfer_mask for XFER_*
929 * @xfer_mode: XFER_* of interest
930 *
931 * Return matching xfer_mask for @xfer_mode.
932 *
933 * LOCKING:
934 * None.
935 *
936 * RETURNS:
937 * Matching xfer_mask, 0 if no match found.
938 */
Tejun Heo7dc951a2007-11-27 19:43:42 +0900939unsigned long ata_xfer_mode2mask(u8 xfer_mode)
Tejun Heocb95d562006-03-06 04:31:56 +0900940{
941 const struct ata_xfer_ent *ent;
942
943 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
944 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
Tejun Heo70cd0712007-11-27 19:43:40 +0900945 return ((2 << (ent->shift + xfer_mode - ent->base)) - 1)
946 & ~((1 << ent->shift) - 1);
Tejun Heocb95d562006-03-06 04:31:56 +0900947 return 0;
948}
949
950/**
951 * ata_xfer_mode2shift - Find matching xfer_shift for XFER_*
952 * @xfer_mode: XFER_* of interest
953 *
954 * Return matching xfer_shift for @xfer_mode.
955 *
956 * LOCKING:
957 * None.
958 *
959 * RETURNS:
960 * Matching xfer_shift, -1 if no match found.
961 */
Tejun Heo7dc951a2007-11-27 19:43:42 +0900962int ata_xfer_mode2shift(unsigned long xfer_mode)
Tejun Heocb95d562006-03-06 04:31:56 +0900963{
964 const struct ata_xfer_ent *ent;
965
966 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
967 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
968 return ent->shift;
969 return -1;
970}
971
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972/**
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900973 * ata_mode_string - convert xfer_mask to string
974 * @xfer_mask: mask of bits supported; only highest bit counts.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 *
976 * Determine string which represents the highest speed
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900977 * (highest bit in @modemask).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 *
979 * LOCKING:
980 * None.
981 *
982 * RETURNS:
983 * Constant C string representing highest speed listed in
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900984 * @mode_mask, or the constant C string "<n/a>".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 */
Tejun Heo7dc951a2007-11-27 19:43:42 +0900986const char *ata_mode_string(unsigned long xfer_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987{
Tejun Heo75f554b2006-03-06 04:31:57 +0900988 static const char * const xfer_mode_str[] = {
989 "PIO0",
990 "PIO1",
991 "PIO2",
992 "PIO3",
993 "PIO4",
Alan Coxb352e572006-08-10 18:52:12 +0100994 "PIO5",
995 "PIO6",
Tejun Heo75f554b2006-03-06 04:31:57 +0900996 "MWDMA0",
997 "MWDMA1",
998 "MWDMA2",
Alan Coxb352e572006-08-10 18:52:12 +0100999 "MWDMA3",
1000 "MWDMA4",
Tejun Heo75f554b2006-03-06 04:31:57 +09001001 "UDMA/16",
1002 "UDMA/25",
1003 "UDMA/33",
1004 "UDMA/44",
1005 "UDMA/66",
1006 "UDMA/100",
1007 "UDMA/133",
1008 "UDMA7",
1009 };
Tejun Heo1da7b0d2006-03-06 04:31:56 +09001010 int highbit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
Tejun Heo1da7b0d2006-03-06 04:31:56 +09001012 highbit = fls(xfer_mask) - 1;
1013 if (highbit >= 0 && highbit < ARRAY_SIZE(xfer_mode_str))
1014 return xfer_mode_str[highbit];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 return "<n/a>";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016}
1017
Gwendal Grignoud9027472010-05-25 12:31:38 -07001018const char *sata_spd_string(unsigned int spd)
Tejun Heo4c360c82006-04-01 01:38:17 +09001019{
1020 static const char * const spd_str[] = {
1021 "1.5 Gbps",
1022 "3.0 Gbps",
Shane Huang8522ee22008-12-30 11:00:37 +08001023 "6.0 Gbps",
Tejun Heo4c360c82006-04-01 01:38:17 +09001024 };
1025
1026 if (spd == 0 || (spd - 1) >= ARRAY_SIZE(spd_str))
1027 return "<unknown>";
1028 return spd_str[spd - 1];
1029}
1030
Tejun Heoc93b2632010-09-01 17:50:04 +02001031static int ata_dev_set_dipm(struct ata_device *dev, enum ata_lpm_policy policy)
Kristen Carlson Accardica773292007-10-25 00:58:59 -04001032{
1033 struct ata_link *link = dev->link;
1034 struct ata_port *ap = link->ap;
1035 u32 scontrol;
1036 unsigned int err_mask;
1037 int rc;
1038
1039 /*
1040 * disallow DIPM for drivers which haven't set
Tejun Heoc93b2632010-09-01 17:50:04 +02001041 * ATA_FLAG_LPM. This is because when DIPM is enabled,
Kristen Carlson Accardica773292007-10-25 00:58:59 -04001042 * phy ready will be set in the interrupt status on
1043 * state changes, which will cause some drivers to
1044 * think there are errors - additionally drivers will
1045 * need to disable hot plug.
1046 */
Tejun Heoc93b2632010-09-01 17:50:04 +02001047 if (!(ap->flags & ATA_FLAG_LPM) || !ata_dev_enabled(dev)) {
1048 ap->lpm_policy = ATA_LPM_UNKNOWN;
Kristen Carlson Accardica773292007-10-25 00:58:59 -04001049 return -EINVAL;
1050 }
1051
1052 /*
1053 * For DIPM, we will only enable it for the
1054 * min_power setting.
1055 *
1056 * Why? Because Disks are too stupid to know that
1057 * If the host rejects a request to go to SLUMBER
1058 * they should retry at PARTIAL, and instead it
1059 * just would give up. So, for medium_power to
1060 * work at all, we need to only allow HIPM.
1061 */
1062 rc = sata_scr_read(link, SCR_CONTROL, &scontrol);
1063 if (rc)
1064 return rc;
1065
1066 switch (policy) {
Tejun Heoc93b2632010-09-01 17:50:04 +02001067 case ATA_LPM_MIN_POWER:
1068 /* no restrictions on LPM transitions */
Kristen Carlson Accardica773292007-10-25 00:58:59 -04001069 scontrol &= ~(0x3 << 8);
1070 rc = sata_scr_write(link, SCR_CONTROL, scontrol);
1071 if (rc)
1072 return rc;
1073
1074 /* enable DIPM */
1075 if (dev->flags & ATA_DFLAG_DIPM)
1076 err_mask = ata_dev_set_feature(dev,
1077 SETFEATURES_SATA_ENABLE, SATA_DIPM);
1078 break;
Tejun Heoc93b2632010-09-01 17:50:04 +02001079 case ATA_LPM_MED_POWER:
1080 /* allow LPM to PARTIAL */
Kristen Carlson Accardica773292007-10-25 00:58:59 -04001081 scontrol &= ~(0x1 << 8);
1082 scontrol |= (0x2 << 8);
1083 rc = sata_scr_write(link, SCR_CONTROL, scontrol);
1084 if (rc)
1085 return rc;
1086
Kristen Carlson Accardif5456b62007-11-02 16:37:08 -07001087 /*
Tejun Heoc93b2632010-09-01 17:50:04 +02001088 * we don't have to disable DIPM since LPM flags
Kristen Carlson Accardif5456b62007-11-02 16:37:08 -07001089 * disallow transitions to SLUMBER, which effectively
1090 * disable DIPM if it does not support PARTIAL
1091 */
Kristen Carlson Accardica773292007-10-25 00:58:59 -04001092 break;
Tejun Heoc93b2632010-09-01 17:50:04 +02001093 case ATA_LPM_UNKNOWN:
1094 case ATA_LPM_MAX_POWER:
1095 /* disable all LPM transitions */
Kristen Carlson Accardica773292007-10-25 00:58:59 -04001096 scontrol |= (0x3 << 8);
1097 rc = sata_scr_write(link, SCR_CONTROL, scontrol);
1098 if (rc)
1099 return rc;
1100
Kristen Carlson Accardif5456b62007-11-02 16:37:08 -07001101 /*
Tejun Heoc93b2632010-09-01 17:50:04 +02001102 * we don't have to disable DIPM since LPM flags
Kristen Carlson Accardif5456b62007-11-02 16:37:08 -07001103 * disallow all transitions which effectively
1104 * disable DIPM anyway.
1105 */
Kristen Carlson Accardica773292007-10-25 00:58:59 -04001106 break;
1107 }
1108
1109 /* FIXME: handle SET FEATURES failure */
1110 (void) err_mask;
1111
1112 return 0;
1113}
1114
1115/**
1116 * ata_dev_enable_pm - enable SATA interface power management
Stephen Hemminger48166fd2007-10-31 10:00:27 -07001117 * @dev: device to enable power management
1118 * @policy: the link power management policy
Kristen Carlson Accardica773292007-10-25 00:58:59 -04001119 *
1120 * Enable SATA Interface power management. This will enable
1121 * Device Interface Power Management (DIPM) for min_power
1122 * policy, and then call driver specific callbacks for
1123 * enabling Host Initiated Power management.
1124 *
1125 * Locking: Caller.
Tejun Heoc93b2632010-09-01 17:50:04 +02001126 * Returns: -EINVAL if LPM is not supported, 0 otherwise.
Kristen Carlson Accardica773292007-10-25 00:58:59 -04001127 */
Tejun Heoc93b2632010-09-01 17:50:04 +02001128void ata_dev_enable_pm(struct ata_device *dev, enum ata_lpm_policy policy)
Kristen Carlson Accardica773292007-10-25 00:58:59 -04001129{
1130 int rc = 0;
1131 struct ata_port *ap = dev->link->ap;
1132
1133 /* set HIPM first, then DIPM */
1134 if (ap->ops->enable_pm)
1135 rc = ap->ops->enable_pm(ap, policy);
1136 if (rc)
1137 goto enable_pm_out;
1138 rc = ata_dev_set_dipm(dev, policy);
1139
1140enable_pm_out:
1141 if (rc)
Tejun Heoc93b2632010-09-01 17:50:04 +02001142 ap->lpm_policy = ATA_LPM_MAX_POWER;
Kristen Carlson Accardica773292007-10-25 00:58:59 -04001143 else
Tejun Heoc93b2632010-09-01 17:50:04 +02001144 ap->lpm_policy = policy;
Kristen Carlson Accardica773292007-10-25 00:58:59 -04001145 return /* rc */; /* hopefully we can use 'rc' eventually */
1146}
1147
Stephen Rothwell1992a5e2007-10-31 14:53:32 +11001148#ifdef CONFIG_PM
Kristen Carlson Accardica773292007-10-25 00:58:59 -04001149/**
1150 * ata_dev_disable_pm - disable SATA interface power management
Stephen Hemminger48166fd2007-10-31 10:00:27 -07001151 * @dev: device to disable power management
Kristen Carlson Accardica773292007-10-25 00:58:59 -04001152 *
1153 * Disable SATA Interface power management. This will disable
1154 * Device Interface Power Management (DIPM) without changing
1155 * policy, call driver specific callbacks for disabling Host
1156 * Initiated Power management.
1157 *
1158 * Locking: Caller.
1159 * Returns: void
1160 */
1161static void ata_dev_disable_pm(struct ata_device *dev)
1162{
1163 struct ata_port *ap = dev->link->ap;
1164
Tejun Heoc93b2632010-09-01 17:50:04 +02001165 ata_dev_set_dipm(dev, ATA_LPM_MAX_POWER);
Kristen Carlson Accardica773292007-10-25 00:58:59 -04001166 if (ap->ops->disable_pm)
1167 ap->ops->disable_pm(ap);
1168}
Stephen Rothwell1992a5e2007-10-31 14:53:32 +11001169#endif /* CONFIG_PM */
Kristen Carlson Accardica773292007-10-25 00:58:59 -04001170
Tejun Heoc93b2632010-09-01 17:50:04 +02001171void ata_lpm_schedule(struct ata_port *ap, enum ata_lpm_policy policy)
Kristen Carlson Accardica773292007-10-25 00:58:59 -04001172{
Tejun Heoc93b2632010-09-01 17:50:04 +02001173 ap->lpm_policy = policy;
Tejun Heo3ec25eb2008-03-27 18:37:14 +09001174 ap->link.eh_info.action |= ATA_EH_LPM;
Kristen Carlson Accardica773292007-10-25 00:58:59 -04001175 ap->link.eh_info.flags |= ATA_EHI_NO_AUTOPSY;
1176 ata_port_schedule_eh(ap);
1177}
1178
Stephen Rothwell1992a5e2007-10-31 14:53:32 +11001179#ifdef CONFIG_PM
Kristen Carlson Accardica773292007-10-25 00:58:59 -04001180static void ata_lpm_enable(struct ata_host *host)
1181{
1182 struct ata_link *link;
1183 struct ata_port *ap;
1184 struct ata_device *dev;
1185 int i;
1186
1187 for (i = 0; i < host->n_ports; i++) {
1188 ap = host->ports[i];
Tejun Heo1eca4362008-11-03 20:03:17 +09001189 ata_for_each_link(link, ap, EDGE) {
1190 ata_for_each_dev(dev, link, ALL)
Kristen Carlson Accardica773292007-10-25 00:58:59 -04001191 ata_dev_disable_pm(dev);
1192 }
1193 }
1194}
1195
1196static void ata_lpm_disable(struct ata_host *host)
1197{
1198 int i;
1199
1200 for (i = 0; i < host->n_ports; i++) {
1201 struct ata_port *ap = host->ports[i];
Tejun Heoc93b2632010-09-01 17:50:04 +02001202 ata_lpm_schedule(ap, ap->lpm_policy);
Kristen Carlson Accardica773292007-10-25 00:58:59 -04001203 }
1204}
Stephen Rothwell1992a5e2007-10-31 14:53:32 +11001205#endif /* CONFIG_PM */
Kristen Carlson Accardica773292007-10-25 00:58:59 -04001206
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207/**
1208 * ata_dev_classify - determine device type based on ATA-spec signature
1209 * @tf: ATA taskfile register set for device to be identified
1210 *
1211 * Determine from taskfile register contents whether a device is
1212 * ATA or ATAPI, as per "Signature and persistence" section
1213 * of ATA/PI spec (volume 1, sect 5.14).
1214 *
1215 * LOCKING:
1216 * None.
1217 *
1218 * RETURNS:
Tejun Heo633273a32007-09-23 13:19:54 +09001219 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, %ATA_DEV_PMP or
1220 * %ATA_DEV_UNKNOWN the event of failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 */
Jeff Garzik057ace52005-10-22 14:27:05 -04001222unsigned int ata_dev_classify(const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223{
1224 /* Apple's open source Darwin code hints that some devices only
1225 * put a proper signature into the LBA mid/high registers,
1226 * So, we only check those. It's sufficient for uniqueness.
Tejun Heo633273a32007-09-23 13:19:54 +09001227 *
1228 * ATA/ATAPI-7 (d1532v1r1: Feb. 19, 2003) specified separate
1229 * signatures for ATA and ATAPI devices attached on SerialATA,
1230 * 0x3c/0xc3 and 0x69/0x96 respectively. However, SerialATA
1231 * spec has never mentioned about using different signatures
1232 * for ATA/ATAPI devices. Then, Serial ATA II: Port
1233 * Multiplier specification began to use 0x69/0x96 to identify
1234 * port multpliers and 0x3c/0xc3 to identify SEMB device.
1235 * ATA/ATAPI-7 dropped descriptions about 0x3c/0xc3 and
1236 * 0x69/0x96 shortly and described them as reserved for
1237 * SerialATA.
1238 *
1239 * We follow the current spec and consider that 0x69/0x96
1240 * identifies a port multiplier and 0x3c/0xc3 a SEMB device.
Tejun Heo79b42ba2009-04-15 06:21:10 +09001241 * Unfortunately, WDC WD1600JS-62MHB5 (a hard drive) reports
1242 * SEMB signature. This is worked around in
1243 * ata_dev_read_id().
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 */
Tejun Heo633273a32007-09-23 13:19:54 +09001245 if ((tf->lbam == 0) && (tf->lbah == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 DPRINTK("found ATA device by sig\n");
1247 return ATA_DEV_ATA;
1248 }
1249
Tejun Heo633273a32007-09-23 13:19:54 +09001250 if ((tf->lbam == 0x14) && (tf->lbah == 0xeb)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 DPRINTK("found ATAPI device by sig\n");
1252 return ATA_DEV_ATAPI;
1253 }
1254
Tejun Heo633273a32007-09-23 13:19:54 +09001255 if ((tf->lbam == 0x69) && (tf->lbah == 0x96)) {
1256 DPRINTK("found PMP device by sig\n");
1257 return ATA_DEV_PMP;
1258 }
1259
1260 if ((tf->lbam == 0x3c) && (tf->lbah == 0xc3)) {
Tejun Heo79b42ba2009-04-15 06:21:10 +09001261 DPRINTK("found SEMB device by sig (could be ATA device)\n");
1262 return ATA_DEV_SEMB;
Tejun Heo633273a32007-09-23 13:19:54 +09001263 }
1264
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 DPRINTK("unknown device\n");
1266 return ATA_DEV_UNKNOWN;
1267}
1268
1269/**
Tejun Heo6a62a042006-02-13 10:02:46 +09001270 * ata_id_string - Convert IDENTIFY DEVICE page into string
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 * @id: IDENTIFY DEVICE results we will examine
1272 * @s: string into which data is output
1273 * @ofs: offset into identify device page
1274 * @len: length of string to return. must be an even number.
1275 *
1276 * The strings in the IDENTIFY DEVICE page are broken up into
1277 * 16-bit chunks. Run through the string, and output each
1278 * 8-bit chunk linearly, regardless of platform.
1279 *
1280 * LOCKING:
1281 * caller.
1282 */
1283
Tejun Heo6a62a042006-02-13 10:02:46 +09001284void ata_id_string(const u16 *id, unsigned char *s,
1285 unsigned int ofs, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286{
1287 unsigned int c;
1288
Alan Cox963e4972008-07-24 17:16:06 +01001289 BUG_ON(len & 1);
1290
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 while (len > 0) {
1292 c = id[ofs] >> 8;
1293 *s = c;
1294 s++;
1295
1296 c = id[ofs] & 0xff;
1297 *s = c;
1298 s++;
1299
1300 ofs++;
1301 len -= 2;
1302 }
1303}
1304
Tejun Heo0e949ff2006-02-12 22:47:04 +09001305/**
Tejun Heo6a62a042006-02-13 10:02:46 +09001306 * ata_id_c_string - Convert IDENTIFY DEVICE page into C string
Tejun Heo0e949ff2006-02-12 22:47:04 +09001307 * @id: IDENTIFY DEVICE results we will examine
1308 * @s: string into which data is output
1309 * @ofs: offset into identify device page
1310 * @len: length of string to return. must be an odd number.
1311 *
Tejun Heo6a62a042006-02-13 10:02:46 +09001312 * This function is identical to ata_id_string except that it
Tejun Heo0e949ff2006-02-12 22:47:04 +09001313 * trims trailing spaces and terminates the resulting string with
1314 * null. @len must be actual maximum length (even number) + 1.
1315 *
1316 * LOCKING:
1317 * caller.
1318 */
Tejun Heo6a62a042006-02-13 10:02:46 +09001319void ata_id_c_string(const u16 *id, unsigned char *s,
1320 unsigned int ofs, unsigned int len)
Tejun Heo0e949ff2006-02-12 22:47:04 +09001321{
1322 unsigned char *p;
1323
Tejun Heo6a62a042006-02-13 10:02:46 +09001324 ata_id_string(id, s, ofs, len - 1);
Tejun Heo0e949ff2006-02-12 22:47:04 +09001325
1326 p = s + strnlen(s, len - 1);
1327 while (p > s && p[-1] == ' ')
1328 p--;
1329 *p = '\0';
1330}
Edward Falk0baab862005-06-02 18:17:13 -04001331
Tejun Heodb6f8752007-09-03 12:31:58 +09001332static u64 ata_id_n_sectors(const u16 *id)
1333{
1334 if (ata_id_has_lba(id)) {
1335 if (ata_id_has_lba48(id))
Robert Hancock968e5942009-02-16 20:15:08 -06001336 return ata_id_u64(id, ATA_ID_LBA_CAPACITY_2);
Tejun Heodb6f8752007-09-03 12:31:58 +09001337 else
Robert Hancock968e5942009-02-16 20:15:08 -06001338 return ata_id_u32(id, ATA_ID_LBA_CAPACITY);
Tejun Heodb6f8752007-09-03 12:31:58 +09001339 } else {
1340 if (ata_id_current_chs_valid(id))
Robert Hancock968e5942009-02-16 20:15:08 -06001341 return id[ATA_ID_CUR_CYLS] * id[ATA_ID_CUR_HEADS] *
1342 id[ATA_ID_CUR_SECTORS];
Tejun Heodb6f8752007-09-03 12:31:58 +09001343 else
Robert Hancock968e5942009-02-16 20:15:08 -06001344 return id[ATA_ID_CYLS] * id[ATA_ID_HEADS] *
1345 id[ATA_ID_SECTORS];
Tejun Heodb6f8752007-09-03 12:31:58 +09001346 }
1347}
1348
Tejun Heoa5987e02008-03-27 19:14:23 +09001349u64 ata_tf_to_lba48(const struct ata_taskfile *tf)
Alan Cox1e999732007-04-11 00:23:13 +01001350{
1351 u64 sectors = 0;
1352
1353 sectors |= ((u64)(tf->hob_lbah & 0xff)) << 40;
1354 sectors |= ((u64)(tf->hob_lbam & 0xff)) << 32;
Roland Dreierba14a9c2008-10-28 16:52:20 -07001355 sectors |= ((u64)(tf->hob_lbal & 0xff)) << 24;
Alan Cox1e999732007-04-11 00:23:13 +01001356 sectors |= (tf->lbah & 0xff) << 16;
1357 sectors |= (tf->lbam & 0xff) << 8;
1358 sectors |= (tf->lbal & 0xff);
1359
Tejun Heoa5987e02008-03-27 19:14:23 +09001360 return sectors;
Alan Cox1e999732007-04-11 00:23:13 +01001361}
1362
Tejun Heoa5987e02008-03-27 19:14:23 +09001363u64 ata_tf_to_lba(const struct ata_taskfile *tf)
Alan Cox1e999732007-04-11 00:23:13 +01001364{
1365 u64 sectors = 0;
1366
1367 sectors |= (tf->device & 0x0f) << 24;
1368 sectors |= (tf->lbah & 0xff) << 16;
1369 sectors |= (tf->lbam & 0xff) << 8;
1370 sectors |= (tf->lbal & 0xff);
1371
Tejun Heoa5987e02008-03-27 19:14:23 +09001372 return sectors;
Alan Cox1e999732007-04-11 00:23:13 +01001373}
1374
1375/**
Tejun Heoc728a912007-09-03 12:32:30 +09001376 * ata_read_native_max_address - Read native max address
1377 * @dev: target device
1378 * @max_sectors: out parameter for the result native max address
Alan Cox1e999732007-04-11 00:23:13 +01001379 *
Tejun Heoc728a912007-09-03 12:32:30 +09001380 * Perform an LBA48 or LBA28 native size query upon the device in
1381 * question.
1382 *
1383 * RETURNS:
1384 * 0 on success, -EACCES if command is aborted by the drive.
1385 * -EIO on other errors.
Alan Cox1e999732007-04-11 00:23:13 +01001386 */
Tejun Heoc728a912007-09-03 12:32:30 +09001387static int ata_read_native_max_address(struct ata_device *dev, u64 *max_sectors)
Alan Cox1e999732007-04-11 00:23:13 +01001388{
Tejun Heoc728a912007-09-03 12:32:30 +09001389 unsigned int err_mask;
Alan Cox1e999732007-04-11 00:23:13 +01001390 struct ata_taskfile tf;
Tejun Heoc728a912007-09-03 12:32:30 +09001391 int lba48 = ata_id_has_lba48(dev->id);
Alan Cox1e999732007-04-11 00:23:13 +01001392
1393 ata_tf_init(dev, &tf);
1394
Tejun Heoc728a912007-09-03 12:32:30 +09001395 /* always clear all address registers */
Alan Cox1e999732007-04-11 00:23:13 +01001396 tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
Tejun Heoc728a912007-09-03 12:32:30 +09001397
1398 if (lba48) {
1399 tf.command = ATA_CMD_READ_NATIVE_MAX_EXT;
1400 tf.flags |= ATA_TFLAG_LBA48;
1401 } else
1402 tf.command = ATA_CMD_READ_NATIVE_MAX;
1403
Alan Cox1e999732007-04-11 00:23:13 +01001404 tf.protocol |= ATA_PROT_NODATA;
Tejun Heoc728a912007-09-03 12:32:30 +09001405 tf.device |= ATA_LBA;
Alan Cox1e999732007-04-11 00:23:13 +01001406
Tejun Heo2b789102007-10-09 15:05:44 +09001407 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
Tejun Heoc728a912007-09-03 12:32:30 +09001408 if (err_mask) {
1409 ata_dev_printk(dev, KERN_WARNING, "failed to read native "
1410 "max address (err_mask=0x%x)\n", err_mask);
1411 if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
1412 return -EACCES;
1413 return -EIO;
1414 }
Alan Cox1e999732007-04-11 00:23:13 +01001415
Tejun Heoc728a912007-09-03 12:32:30 +09001416 if (lba48)
Tejun Heoa5987e02008-03-27 19:14:23 +09001417 *max_sectors = ata_tf_to_lba48(&tf) + 1;
Tejun Heoc728a912007-09-03 12:32:30 +09001418 else
Tejun Heoa5987e02008-03-27 19:14:23 +09001419 *max_sectors = ata_tf_to_lba(&tf) + 1;
Jeff Garzik2dcb4072007-10-19 06:42:56 -04001420 if (dev->horkage & ATA_HORKAGE_HPA_SIZE)
Alan Cox93328e12007-09-29 04:06:48 -04001421 (*max_sectors)--;
Tejun Heoc728a912007-09-03 12:32:30 +09001422 return 0;
Alan Cox1e999732007-04-11 00:23:13 +01001423}
1424
1425/**
Tejun Heoc728a912007-09-03 12:32:30 +09001426 * ata_set_max_sectors - Set max sectors
1427 * @dev: target device
Randy Dunlap6b38d1d2007-05-01 17:35:55 -07001428 * @new_sectors: new max sectors value to set for the device
Alan Cox1e999732007-04-11 00:23:13 +01001429 *
Tejun Heoc728a912007-09-03 12:32:30 +09001430 * Set max sectors of @dev to @new_sectors.
1431 *
1432 * RETURNS:
1433 * 0 on success, -EACCES if command is aborted or denied (due to
1434 * previous non-volatile SET_MAX) by the drive. -EIO on other
1435 * errors.
Alan Cox1e999732007-04-11 00:23:13 +01001436 */
Tejun Heo05027ad2007-09-03 12:32:57 +09001437static int ata_set_max_sectors(struct ata_device *dev, u64 new_sectors)
Alan Cox1e999732007-04-11 00:23:13 +01001438{
Tejun Heoc728a912007-09-03 12:32:30 +09001439 unsigned int err_mask;
Alan Cox1e999732007-04-11 00:23:13 +01001440 struct ata_taskfile tf;
Tejun Heoc728a912007-09-03 12:32:30 +09001441 int lba48 = ata_id_has_lba48(dev->id);
Alan Cox1e999732007-04-11 00:23:13 +01001442
1443 new_sectors--;
1444
1445 ata_tf_init(dev, &tf);
1446
Alan Cox1e999732007-04-11 00:23:13 +01001447 tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
Tejun Heoc728a912007-09-03 12:32:30 +09001448
1449 if (lba48) {
1450 tf.command = ATA_CMD_SET_MAX_EXT;
1451 tf.flags |= ATA_TFLAG_LBA48;
1452
1453 tf.hob_lbal = (new_sectors >> 24) & 0xff;
1454 tf.hob_lbam = (new_sectors >> 32) & 0xff;
1455 tf.hob_lbah = (new_sectors >> 40) & 0xff;
Tejun Heo1e582ba2007-09-21 20:07:14 +09001456 } else {
Tejun Heoc728a912007-09-03 12:32:30 +09001457 tf.command = ATA_CMD_SET_MAX;
1458
Tejun Heo1e582ba2007-09-21 20:07:14 +09001459 tf.device |= (new_sectors >> 24) & 0xf;
1460 }
1461
Alan Cox1e999732007-04-11 00:23:13 +01001462 tf.protocol |= ATA_PROT_NODATA;
Tejun Heoc728a912007-09-03 12:32:30 +09001463 tf.device |= ATA_LBA;
Alan Cox1e999732007-04-11 00:23:13 +01001464
1465 tf.lbal = (new_sectors >> 0) & 0xff;
1466 tf.lbam = (new_sectors >> 8) & 0xff;
1467 tf.lbah = (new_sectors >> 16) & 0xff;
Alan Cox1e999732007-04-11 00:23:13 +01001468
Tejun Heo2b789102007-10-09 15:05:44 +09001469 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
Tejun Heoc728a912007-09-03 12:32:30 +09001470 if (err_mask) {
1471 ata_dev_printk(dev, KERN_WARNING, "failed to set "
1472 "max address (err_mask=0x%x)\n", err_mask);
1473 if (err_mask == AC_ERR_DEV &&
1474 (tf.feature & (ATA_ABORTED | ATA_IDNF)))
1475 return -EACCES;
1476 return -EIO;
1477 }
Alan Cox1e999732007-04-11 00:23:13 +01001478
Tejun Heoc728a912007-09-03 12:32:30 +09001479 return 0;
Alan Cox1e999732007-04-11 00:23:13 +01001480}
1481
1482/**
1483 * ata_hpa_resize - Resize a device with an HPA set
1484 * @dev: Device to resize
1485 *
1486 * Read the size of an LBA28 or LBA48 disk with HPA features and resize
1487 * it if required to the full size of the media. The caller must check
1488 * the drive has the HPA feature set enabled.
Tejun Heo05027ad2007-09-03 12:32:57 +09001489 *
1490 * RETURNS:
1491 * 0 on success, -errno on failure.
Alan Cox1e999732007-04-11 00:23:13 +01001492 */
Tejun Heo05027ad2007-09-03 12:32:57 +09001493static int ata_hpa_resize(struct ata_device *dev)
Alan Cox1e999732007-04-11 00:23:13 +01001494{
Tejun Heo05027ad2007-09-03 12:32:57 +09001495 struct ata_eh_context *ehc = &dev->link->eh_context;
1496 int print_info = ehc->i.flags & ATA_EHI_PRINTINFO;
Tejun Heo445d2112010-04-05 10:33:13 +09001497 bool unlock_hpa = ata_ignore_hpa || dev->flags & ATA_DFLAG_UNLOCK_HPA;
Tejun Heo05027ad2007-09-03 12:32:57 +09001498 u64 sectors = ata_id_n_sectors(dev->id);
1499 u64 native_sectors;
Tejun Heoc728a912007-09-03 12:32:30 +09001500 int rc;
Jeff Garzika617c092007-05-21 20:14:23 -04001501
Tejun Heo05027ad2007-09-03 12:32:57 +09001502 /* do we need to do it? */
1503 if (dev->class != ATA_DEV_ATA ||
1504 !ata_id_has_lba(dev->id) || !ata_id_hpa_enabled(dev->id) ||
1505 (dev->horkage & ATA_HORKAGE_BROKEN_HPA))
Tejun Heoc728a912007-09-03 12:32:30 +09001506 return 0;
Alan Cox1e999732007-04-11 00:23:13 +01001507
Tejun Heo05027ad2007-09-03 12:32:57 +09001508 /* read native max address */
1509 rc = ata_read_native_max_address(dev, &native_sectors);
1510 if (rc) {
Tejun Heodda7aba2008-03-23 21:05:15 +09001511 /* If device aborted the command or HPA isn't going to
1512 * be unlocked, skip HPA resizing.
Tejun Heo05027ad2007-09-03 12:32:57 +09001513 */
Tejun Heo445d2112010-04-05 10:33:13 +09001514 if (rc == -EACCES || !unlock_hpa) {
Tejun Heo05027ad2007-09-03 12:32:57 +09001515 ata_dev_printk(dev, KERN_WARNING, "HPA support seems "
Tejun Heodda7aba2008-03-23 21:05:15 +09001516 "broken, skipping HPA handling\n");
Tejun Heo05027ad2007-09-03 12:32:57 +09001517 dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
Alan Cox1e999732007-04-11 00:23:13 +01001518
Tejun Heo05027ad2007-09-03 12:32:57 +09001519 /* we can continue if device aborted the command */
1520 if (rc == -EACCES)
1521 rc = 0;
Alan Cox1e999732007-04-11 00:23:13 +01001522 }
Tejun Heo37301a52007-06-25 20:45:54 +09001523
Tejun Heo05027ad2007-09-03 12:32:57 +09001524 return rc;
1525 }
Tejun Heo5920dad2009-07-15 17:11:41 +09001526 dev->n_native_sectors = native_sectors;
Tejun Heo05027ad2007-09-03 12:32:57 +09001527
1528 /* nothing to do? */
Tejun Heo445d2112010-04-05 10:33:13 +09001529 if (native_sectors <= sectors || !unlock_hpa) {
Tejun Heo05027ad2007-09-03 12:32:57 +09001530 if (!print_info || native_sectors == sectors)
1531 return 0;
1532
1533 if (native_sectors > sectors)
1534 ata_dev_printk(dev, KERN_INFO,
1535 "HPA detected: current %llu, native %llu\n",
1536 (unsigned long long)sectors,
1537 (unsigned long long)native_sectors);
1538 else if (native_sectors < sectors)
1539 ata_dev_printk(dev, KERN_WARNING,
1540 "native sectors (%llu) is smaller than "
1541 "sectors (%llu)\n",
1542 (unsigned long long)native_sectors,
1543 (unsigned long long)sectors);
1544 return 0;
1545 }
1546
1547 /* let's unlock HPA */
1548 rc = ata_set_max_sectors(dev, native_sectors);
1549 if (rc == -EACCES) {
1550 /* if device aborted the command, skip HPA resizing */
1551 ata_dev_printk(dev, KERN_WARNING, "device aborted resize "
1552 "(%llu -> %llu), skipping HPA handling\n",
1553 (unsigned long long)sectors,
1554 (unsigned long long)native_sectors);
1555 dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
1556 return 0;
1557 } else if (rc)
1558 return rc;
1559
1560 /* re-read IDENTIFY data */
1561 rc = ata_dev_reread_id(dev, 0);
1562 if (rc) {
1563 ata_dev_printk(dev, KERN_ERR, "failed to re-read IDENTIFY "
1564 "data after HPA resizing\n");
1565 return rc;
1566 }
1567
1568 if (print_info) {
1569 u64 new_sectors = ata_id_n_sectors(dev->id);
1570 ata_dev_printk(dev, KERN_INFO,
1571 "HPA unlocked: %llu -> %llu, native %llu\n",
1572 (unsigned long long)sectors,
1573 (unsigned long long)new_sectors,
1574 (unsigned long long)native_sectors);
1575 }
1576
1577 return 0;
Alan Cox1e999732007-04-11 00:23:13 +01001578}
1579
Edward Falk0baab862005-06-02 18:17:13 -04001580/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 * ata_dump_id - IDENTIFY DEVICE info debugging output
Tejun Heo0bd33002006-02-12 22:47:05 +09001582 * @id: IDENTIFY DEVICE page to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583 *
Tejun Heo0bd33002006-02-12 22:47:05 +09001584 * Dump selected 16-bit words from the given IDENTIFY DEVICE
1585 * page.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 *
1587 * LOCKING:
1588 * caller.
1589 */
1590
Tejun Heo0bd33002006-02-12 22:47:05 +09001591static inline void ata_dump_id(const u16 *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592{
1593 DPRINTK("49==0x%04x "
1594 "53==0x%04x "
1595 "63==0x%04x "
1596 "64==0x%04x "
1597 "75==0x%04x \n",
Tejun Heo0bd33002006-02-12 22:47:05 +09001598 id[49],
1599 id[53],
1600 id[63],
1601 id[64],
1602 id[75]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 DPRINTK("80==0x%04x "
1604 "81==0x%04x "
1605 "82==0x%04x "
1606 "83==0x%04x "
1607 "84==0x%04x \n",
Tejun Heo0bd33002006-02-12 22:47:05 +09001608 id[80],
1609 id[81],
1610 id[82],
1611 id[83],
1612 id[84]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 DPRINTK("88==0x%04x "
1614 "93==0x%04x\n",
Tejun Heo0bd33002006-02-12 22:47:05 +09001615 id[88],
1616 id[93]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617}
1618
Tejun Heocb95d562006-03-06 04:31:56 +09001619/**
1620 * ata_id_xfermask - Compute xfermask from the given IDENTIFY data
1621 * @id: IDENTIFY data to compute xfer mask from
1622 *
1623 * Compute the xfermask for this device. This is not as trivial
1624 * as it seems if we must consider early devices correctly.
1625 *
1626 * FIXME: pre IDE drive timing (do we care ?).
1627 *
1628 * LOCKING:
1629 * None.
1630 *
1631 * RETURNS:
1632 * Computed xfermask
1633 */
Tejun Heo7dc951a2007-11-27 19:43:42 +09001634unsigned long ata_id_xfermask(const u16 *id)
Tejun Heocb95d562006-03-06 04:31:56 +09001635{
Tejun Heo7dc951a2007-11-27 19:43:42 +09001636 unsigned long pio_mask, mwdma_mask, udma_mask;
Tejun Heocb95d562006-03-06 04:31:56 +09001637
1638 /* Usual case. Word 53 indicates word 64 is valid */
1639 if (id[ATA_ID_FIELD_VALID] & (1 << 1)) {
1640 pio_mask = id[ATA_ID_PIO_MODES] & 0x03;
1641 pio_mask <<= 3;
1642 pio_mask |= 0x7;
1643 } else {
1644 /* If word 64 isn't valid then Word 51 high byte holds
1645 * the PIO timing number for the maximum. Turn it into
1646 * a mask.
1647 */
Lennert Buytenhek7a0f1c82007-01-29 13:28:47 +01001648 u8 mode = (id[ATA_ID_OLD_PIO_MODES] >> 8) & 0xFF;
Alan Cox46767aeb2006-09-29 18:26:47 +01001649 if (mode < 5) /* Valid PIO range */
Jeff Garzik2dcb4072007-10-19 06:42:56 -04001650 pio_mask = (2 << mode) - 1;
Alan Cox46767aeb2006-09-29 18:26:47 +01001651 else
1652 pio_mask = 1;
Tejun Heocb95d562006-03-06 04:31:56 +09001653
1654 /* But wait.. there's more. Design your standards by
1655 * committee and you too can get a free iordy field to
1656 * process. However its the speeds not the modes that
1657 * are supported... Note drivers using the timing API
1658 * will get this right anyway
1659 */
1660 }
1661
1662 mwdma_mask = id[ATA_ID_MWDMA_MODES] & 0x07;
Tejun Heofb21f0d2006-03-12 12:34:35 +09001663
Alan Coxb352e572006-08-10 18:52:12 +01001664 if (ata_id_is_cfa(id)) {
1665 /*
1666 * Process compact flash extended modes
1667 */
Sergei Shtylyov62afe5d2009-04-13 20:50:00 +04001668 int pio = (id[ATA_ID_CFA_MODES] >> 0) & 0x7;
1669 int dma = (id[ATA_ID_CFA_MODES] >> 3) & 0x7;
Alan Coxb352e572006-08-10 18:52:12 +01001670
1671 if (pio)
1672 pio_mask |= (1 << 5);
1673 if (pio > 1)
1674 pio_mask |= (1 << 6);
1675 if (dma)
1676 mwdma_mask |= (1 << 3);
1677 if (dma > 1)
1678 mwdma_mask |= (1 << 4);
1679 }
1680
Tejun Heofb21f0d2006-03-12 12:34:35 +09001681 udma_mask = 0;
1682 if (id[ATA_ID_FIELD_VALID] & (1 << 2))
1683 udma_mask = id[ATA_ID_UDMA_MODES] & 0xff;
Tejun Heocb95d562006-03-06 04:31:56 +09001684
1685 return ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
1686}
1687
Adrian Bunk7102d232007-01-04 00:09:36 +01001688static void ata_qc_complete_internal(struct ata_queued_cmd *qc)
Tejun Heoa2a7a662005-12-13 14:48:31 +09001689{
Tejun Heo77853bf2006-01-23 13:09:36 +09001690 struct completion *waiting = qc->private_data;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001691
Tejun Heoa2a7a662005-12-13 14:48:31 +09001692 complete(waiting);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001693}
1694
1695/**
Tejun Heo24326972006-11-14 22:47:09 +09001696 * ata_exec_internal_sg - execute libata internal command
Tejun Heoa2a7a662005-12-13 14:48:31 +09001697 * @dev: Device to which the command is sent
1698 * @tf: Taskfile registers for the command and the result
Tejun Heod69cf372006-04-02 18:51:53 +09001699 * @cdb: CDB for packet command
Tejun Heoa2a7a662005-12-13 14:48:31 +09001700 * @dma_dir: Data tranfer direction of the command
Randy Dunlap5c1ad8b2007-10-18 14:12:26 -07001701 * @sgl: sg list for the data buffer of the command
Tejun Heo24326972006-11-14 22:47:09 +09001702 * @n_elem: Number of sg entries
Tejun Heo2b789102007-10-09 15:05:44 +09001703 * @timeout: Timeout in msecs (0 for default)
Tejun Heoa2a7a662005-12-13 14:48:31 +09001704 *
1705 * Executes libata internal command with timeout. @tf contains
1706 * command on entry and result on return. Timeout and error
1707 * conditions are reported via return value. No recovery action
1708 * is taken after a command times out. It's caller's duty to
1709 * clean up after timeout.
1710 *
1711 * LOCKING:
1712 * None. Should be called with kernel context, might sleep.
Tejun Heo551e8882006-06-12 14:09:49 +09001713 *
1714 * RETURNS:
1715 * Zero on success, AC_ERR_* mask on failure
Tejun Heoa2a7a662005-12-13 14:48:31 +09001716 */
Tejun Heo24326972006-11-14 22:47:09 +09001717unsigned ata_exec_internal_sg(struct ata_device *dev,
1718 struct ata_taskfile *tf, const u8 *cdb,
Jens Axboe87260212007-10-16 11:14:12 +02001719 int dma_dir, struct scatterlist *sgl,
Tejun Heo2b789102007-10-09 15:05:44 +09001720 unsigned int n_elem, unsigned long timeout)
Tejun Heoa2a7a662005-12-13 14:48:31 +09001721{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001722 struct ata_link *link = dev->link;
1723 struct ata_port *ap = link->ap;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001724 u8 command = tf->command;
Tejun Heo87fbc5a2008-05-20 02:17:54 +09001725 int auto_timeout = 0;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001726 struct ata_queued_cmd *qc;
Tejun Heo2ab7db12006-05-15 20:58:02 +09001727 unsigned int tag, preempted_tag;
Tejun Heodedaf2b2006-05-15 21:03:43 +09001728 u32 preempted_sactive, preempted_qc_active;
Tejun Heoda917d62007-09-23 13:14:12 +09001729 int preempted_nr_active_links;
Ingo Molnar60be6b92006-07-03 00:25:26 -07001730 DECLARE_COMPLETION_ONSTACK(wait);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001731 unsigned long flags;
Tejun Heo77853bf2006-01-23 13:09:36 +09001732 unsigned int err_mask;
Tejun Heod95a7172006-05-15 20:58:14 +09001733 int rc;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001734
Jeff Garzikba6a1302006-06-22 23:46:10 -04001735 spin_lock_irqsave(ap->lock, flags);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001736
Tejun Heoe3180492006-05-15 20:58:09 +09001737 /* no internal command while frozen */
Tejun Heob51e9e52006-06-29 01:29:30 +09001738 if (ap->pflags & ATA_PFLAG_FROZEN) {
Jeff Garzikba6a1302006-06-22 23:46:10 -04001739 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +09001740 return AC_ERR_SYSTEM;
1741 }
1742
Tejun Heo2ab7db12006-05-15 20:58:02 +09001743 /* initialize internal qc */
Tejun Heoa2a7a662005-12-13 14:48:31 +09001744
Tejun Heo2ab7db12006-05-15 20:58:02 +09001745 /* XXX: Tag 0 is used for drivers with legacy EH as some
1746 * drivers choke if any other tag is given. This breaks
1747 * ata_tag_internal() test for those drivers. Don't use new
1748 * EH stuff without converting to it.
1749 */
1750 if (ap->ops->error_handler)
1751 tag = ATA_TAG_INTERNAL;
1752 else
1753 tag = 0;
1754
Tejun Heo8a8bc222008-11-10 14:48:21 +09001755 if (test_and_set_bit(tag, &ap->qc_allocated))
1756 BUG();
Tejun Heof69499f2006-05-15 20:58:03 +09001757 qc = __ata_qc_from_tag(ap, tag);
Tejun Heo2ab7db12006-05-15 20:58:02 +09001758
1759 qc->tag = tag;
1760 qc->scsicmd = NULL;
1761 qc->ap = ap;
1762 qc->dev = dev;
1763 ata_qc_reinit(qc);
1764
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001765 preempted_tag = link->active_tag;
1766 preempted_sactive = link->sactive;
Tejun Heodedaf2b2006-05-15 21:03:43 +09001767 preempted_qc_active = ap->qc_active;
Tejun Heoda917d62007-09-23 13:14:12 +09001768 preempted_nr_active_links = ap->nr_active_links;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001769 link->active_tag = ATA_TAG_POISON;
1770 link->sactive = 0;
Tejun Heodedaf2b2006-05-15 21:03:43 +09001771 ap->qc_active = 0;
Tejun Heoda917d62007-09-23 13:14:12 +09001772 ap->nr_active_links = 0;
Tejun Heo2ab7db12006-05-15 20:58:02 +09001773
1774 /* prepare & issue qc */
Tejun Heoa2a7a662005-12-13 14:48:31 +09001775 qc->tf = *tf;
Tejun Heod69cf372006-04-02 18:51:53 +09001776 if (cdb)
1777 memcpy(qc->cdb, cdb, ATAPI_CDB_LEN);
Tejun Heoe61e0672006-05-15 20:57:40 +09001778 qc->flags |= ATA_QCFLAG_RESULT_TF;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001779 qc->dma_dir = dma_dir;
1780 if (dma_dir != DMA_NONE) {
Tejun Heo24326972006-11-14 22:47:09 +09001781 unsigned int i, buflen = 0;
Jens Axboe87260212007-10-16 11:14:12 +02001782 struct scatterlist *sg;
Tejun Heo24326972006-11-14 22:47:09 +09001783
Jens Axboe87260212007-10-16 11:14:12 +02001784 for_each_sg(sgl, sg, n_elem, i)
1785 buflen += sg->length;
Tejun Heo24326972006-11-14 22:47:09 +09001786
Jens Axboe87260212007-10-16 11:14:12 +02001787 ata_sg_init(qc, sgl, n_elem);
Brian King49c80422007-01-30 11:32:26 -06001788 qc->nbytes = buflen;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001789 }
1790
Tejun Heo77853bf2006-01-23 13:09:36 +09001791 qc->private_data = &wait;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001792 qc->complete_fn = ata_qc_complete_internal;
1793
Tejun Heo8e0e6942006-03-31 20:41:11 +09001794 ata_qc_issue(qc);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001795
Jeff Garzikba6a1302006-06-22 23:46:10 -04001796 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001797
Tejun Heo87fbc5a2008-05-20 02:17:54 +09001798 if (!timeout) {
1799 if (ata_probe_timeout)
1800 timeout = ata_probe_timeout * 1000;
1801 else {
1802 timeout = ata_internal_cmd_timeout(dev, command);
1803 auto_timeout = 1;
1804 }
1805 }
Tejun Heo2b789102007-10-09 15:05:44 +09001806
1807 rc = wait_for_completion_timeout(&wait, msecs_to_jiffies(timeout));
Albert Lee41ade502006-03-14 11:19:04 +08001808
Tejun Heoc4291372010-05-10 21:41:38 +02001809 ata_sff_flush_pio_task(ap);
Tejun Heod95a7172006-05-15 20:58:14 +09001810
1811 if (!rc) {
Jeff Garzikba6a1302006-06-22 23:46:10 -04001812 spin_lock_irqsave(ap->lock, flags);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001813
1814 /* We're racing with irq here. If we lose, the
1815 * following test prevents us from completing the qc
Tejun Heod95a7172006-05-15 20:58:14 +09001816 * twice. If we win, the port is frozen and will be
1817 * cleaned up by ->post_internal_cmd().
Tejun Heoa2a7a662005-12-13 14:48:31 +09001818 */
Tejun Heo77853bf2006-01-23 13:09:36 +09001819 if (qc->flags & ATA_QCFLAG_ACTIVE) {
Tejun Heod95a7172006-05-15 20:58:14 +09001820 qc->err_mask |= AC_ERR_TIMEOUT;
1821
1822 if (ap->ops->error_handler)
1823 ata_port_freeze(ap);
1824 else
1825 ata_qc_complete(qc);
Tejun Heof15a1da2006-05-15 20:57:56 +09001826
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001827 if (ata_msg_warn(ap))
1828 ata_dev_printk(dev, KERN_WARNING,
Tejun Heo88574552006-06-25 20:00:35 +09001829 "qc timeout (cmd 0x%x)\n", command);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001830 }
1831
Jeff Garzikba6a1302006-06-22 23:46:10 -04001832 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001833 }
1834
Tejun Heod95a7172006-05-15 20:58:14 +09001835 /* do post_internal_cmd */
1836 if (ap->ops->post_internal_cmd)
1837 ap->ops->post_internal_cmd(qc);
1838
Tejun Heoa51d6442007-03-20 15:24:11 +09001839 /* perform minimal error analysis */
1840 if (qc->flags & ATA_QCFLAG_FAILED) {
1841 if (qc->result_tf.command & (ATA_ERR | ATA_DF))
1842 qc->err_mask |= AC_ERR_DEV;
1843
1844 if (!qc->err_mask)
1845 qc->err_mask |= AC_ERR_OTHER;
1846
1847 if (qc->err_mask & ~AC_ERR_OTHER)
1848 qc->err_mask &= ~AC_ERR_OTHER;
Tejun Heod95a7172006-05-15 20:58:14 +09001849 }
1850
Tejun Heo15869302006-05-15 20:57:33 +09001851 /* finish up */
Jeff Garzikba6a1302006-06-22 23:46:10 -04001852 spin_lock_irqsave(ap->lock, flags);
Tejun Heo15869302006-05-15 20:57:33 +09001853
Tejun Heoe61e0672006-05-15 20:57:40 +09001854 *tf = qc->result_tf;
Tejun Heo77853bf2006-01-23 13:09:36 +09001855 err_mask = qc->err_mask;
1856
1857 ata_qc_free(qc);
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001858 link->active_tag = preempted_tag;
1859 link->sactive = preempted_sactive;
Tejun Heodedaf2b2006-05-15 21:03:43 +09001860 ap->qc_active = preempted_qc_active;
Tejun Heoda917d62007-09-23 13:14:12 +09001861 ap->nr_active_links = preempted_nr_active_links;
Tejun Heo77853bf2006-01-23 13:09:36 +09001862
Jeff Garzikba6a1302006-06-22 23:46:10 -04001863 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo15869302006-05-15 20:57:33 +09001864
Tejun Heo87fbc5a2008-05-20 02:17:54 +09001865 if ((err_mask & AC_ERR_TIMEOUT) && auto_timeout)
1866 ata_internal_cmd_timed_out(dev, command);
1867
Tejun Heo77853bf2006-01-23 13:09:36 +09001868 return err_mask;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001869}
1870
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871/**
Tejun Heo33480a02006-12-12 02:15:31 +09001872 * ata_exec_internal - execute libata internal command
Tejun Heo24326972006-11-14 22:47:09 +09001873 * @dev: Device to which the command is sent
1874 * @tf: Taskfile registers for the command and the result
1875 * @cdb: CDB for packet command
1876 * @dma_dir: Data tranfer direction of the command
1877 * @buf: Data buffer of the command
1878 * @buflen: Length of data buffer
Tejun Heo2b789102007-10-09 15:05:44 +09001879 * @timeout: Timeout in msecs (0 for default)
Tejun Heo24326972006-11-14 22:47:09 +09001880 *
1881 * Wrapper around ata_exec_internal_sg() which takes simple
1882 * buffer instead of sg list.
1883 *
1884 * LOCKING:
1885 * None. Should be called with kernel context, might sleep.
1886 *
1887 * RETURNS:
1888 * Zero on success, AC_ERR_* mask on failure
1889 */
1890unsigned ata_exec_internal(struct ata_device *dev,
1891 struct ata_taskfile *tf, const u8 *cdb,
Tejun Heo2b789102007-10-09 15:05:44 +09001892 int dma_dir, void *buf, unsigned int buflen,
1893 unsigned long timeout)
Tejun Heo24326972006-11-14 22:47:09 +09001894{
Tejun Heo33480a02006-12-12 02:15:31 +09001895 struct scatterlist *psg = NULL, sg;
1896 unsigned int n_elem = 0;
Tejun Heo24326972006-11-14 22:47:09 +09001897
Tejun Heo33480a02006-12-12 02:15:31 +09001898 if (dma_dir != DMA_NONE) {
1899 WARN_ON(!buf);
1900 sg_init_one(&sg, buf, buflen);
1901 psg = &sg;
1902 n_elem++;
1903 }
Tejun Heo24326972006-11-14 22:47:09 +09001904
Tejun Heo2b789102007-10-09 15:05:44 +09001905 return ata_exec_internal_sg(dev, tf, cdb, dma_dir, psg, n_elem,
1906 timeout);
Tejun Heo24326972006-11-14 22:47:09 +09001907}
1908
1909/**
Tejun Heo977e6b92006-06-24 20:30:19 +09001910 * ata_do_simple_cmd - execute simple internal command
1911 * @dev: Device to which the command is sent
1912 * @cmd: Opcode to execute
1913 *
1914 * Execute a 'simple' command, that only consists of the opcode
1915 * 'cmd' itself, without filling any other registers
1916 *
1917 * LOCKING:
1918 * Kernel thread context (may sleep).
1919 *
1920 * RETURNS:
1921 * Zero on success, AC_ERR_* mask on failure
Tejun Heoe58eb582006-06-24 20:30:19 +09001922 */
Tejun Heo77b08fb2006-06-24 20:30:19 +09001923unsigned int ata_do_simple_cmd(struct ata_device *dev, u8 cmd)
Tejun Heoe58eb582006-06-24 20:30:19 +09001924{
1925 struct ata_taskfile tf;
Tejun Heoe58eb582006-06-24 20:30:19 +09001926
1927 ata_tf_init(dev, &tf);
1928
1929 tf.command = cmd;
1930 tf.flags |= ATA_TFLAG_DEVICE;
1931 tf.protocol = ATA_PROT_NODATA;
1932
Tejun Heo2b789102007-10-09 15:05:44 +09001933 return ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
Tejun Heoe58eb582006-06-24 20:30:19 +09001934}
1935
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936/**
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001937 * ata_pio_need_iordy - check if iordy needed
1938 * @adev: ATA device
1939 *
1940 * Check if the current speed of the device requires IORDY. Used
1941 * by various controllers for chip configuration.
1942 */
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001943unsigned int ata_pio_need_iordy(const struct ata_device *adev)
1944{
Tejun Heo0d9e6652009-06-11 11:04:45 +09001945 /* Don't set IORDY if we're preparing for reset. IORDY may
1946 * lead to controller lock up on certain controllers if the
1947 * port is not occupied. See bko#11703 for details.
1948 */
1949 if (adev->link->ap->pflags & ATA_PFLAG_RESETTING)
1950 return 0;
1951 /* Controller doesn't support IORDY. Probably a pointless
1952 * check as the caller should know this.
1953 */
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001954 if (adev->link->ap->flags & ATA_FLAG_NO_IORDY)
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001955 return 0;
David Daney5c18c4d2008-12-10 15:39:12 -08001956 /* CF spec. r4.1 Table 22 says no iordy on PIO5 and PIO6. */
1957 if (ata_id_is_cfa(adev->id)
1958 && (adev->pio_mode == XFER_PIO_5 || adev->pio_mode == XFER_PIO_6))
1959 return 0;
Alan Cox432729f2007-03-08 23:22:59 +00001960 /* PIO3 and higher it is mandatory */
1961 if (adev->pio_mode > XFER_PIO_2)
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001962 return 1;
Alan Cox432729f2007-03-08 23:22:59 +00001963 /* We turn it on when possible */
1964 if (ata_id_has_iordy(adev->id))
1965 return 1;
1966 return 0;
1967}
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001968
Alan Cox432729f2007-03-08 23:22:59 +00001969/**
1970 * ata_pio_mask_no_iordy - Return the non IORDY mask
1971 * @adev: ATA device
1972 *
1973 * Compute the highest mode possible if we are not using iordy. Return
1974 * -1 if no iordy mode is available.
1975 */
Alan Cox432729f2007-03-08 23:22:59 +00001976static u32 ata_pio_mask_no_iordy(const struct ata_device *adev)
1977{
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001978 /* If we have no drive specific rule, then PIO 2 is non IORDY */
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001979 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
Alan Cox432729f2007-03-08 23:22:59 +00001980 u16 pio = adev->id[ATA_ID_EIDE_PIO];
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001981 /* Is the speed faster than the drive allows non IORDY ? */
1982 if (pio) {
1983 /* This is cycle times not frequency - watch the logic! */
1984 if (pio > 240) /* PIO2 is 240nS per cycle */
Alan Cox432729f2007-03-08 23:22:59 +00001985 return 3 << ATA_SHIFT_PIO;
1986 return 7 << ATA_SHIFT_PIO;
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001987 }
1988 }
Alan Cox432729f2007-03-08 23:22:59 +00001989 return 3 << ATA_SHIFT_PIO;
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001990}
1991
1992/**
Alan Cox963e4972008-07-24 17:16:06 +01001993 * ata_do_dev_read_id - default ID read method
1994 * @dev: device
1995 * @tf: proposed taskfile
1996 * @id: data buffer
1997 *
1998 * Issue the identify taskfile and hand back the buffer containing
1999 * identify data. For some RAID controllers and for pre ATA devices
2000 * this function is wrapped or replaced by the driver
2001 */
2002unsigned int ata_do_dev_read_id(struct ata_device *dev,
2003 struct ata_taskfile *tf, u16 *id)
2004{
2005 return ata_exec_internal(dev, tf, NULL, DMA_FROM_DEVICE,
2006 id, sizeof(id[0]) * ATA_ID_WORDS, 0);
2007}
2008
2009/**
Tejun Heo49016ac2006-02-21 02:12:11 +09002010 * ata_dev_read_id - Read ID data from the specified device
Tejun Heo49016ac2006-02-21 02:12:11 +09002011 * @dev: target device
2012 * @p_class: pointer to class of the target device (may be changed)
Tejun Heobff04642006-11-10 18:08:10 +09002013 * @flags: ATA_READID_* flags
Tejun Heofe635c72006-05-15 20:57:35 +09002014 * @id: buffer to read IDENTIFY data into
Tejun Heo49016ac2006-02-21 02:12:11 +09002015 *
2016 * Read ID data from the specified device. ATA_CMD_ID_ATA is
2017 * performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
Tejun Heoaec5c3c2006-03-25 01:33:34 +09002018 * devices. This function also issues ATA_CMD_INIT_DEV_PARAMS
2019 * for pre-ATA4 drives.
Tejun Heo49016ac2006-02-21 02:12:11 +09002020 *
Alan Cox50a99012007-08-08 14:27:00 +01002021 * FIXME: ATA_CMD_ID_ATA is optional for early drives and right
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002022 * now we abort if we hit that case.
Alan Cox50a99012007-08-08 14:27:00 +01002023 *
Tejun Heo49016ac2006-02-21 02:12:11 +09002024 * LOCKING:
2025 * Kernel thread context (may sleep)
2026 *
2027 * RETURNS:
2028 * 0 on success, -errno otherwise.
2029 */
Tejun Heoa9beec92006-05-31 18:27:44 +09002030int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
Tejun Heobff04642006-11-10 18:08:10 +09002031 unsigned int flags, u16 *id)
Tejun Heo49016ac2006-02-21 02:12:11 +09002032{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002033 struct ata_port *ap = dev->link->ap;
Tejun Heo49016ac2006-02-21 02:12:11 +09002034 unsigned int class = *p_class;
Tejun Heo49016ac2006-02-21 02:12:11 +09002035 struct ata_taskfile tf;
2036 unsigned int err_mask = 0;
2037 const char *reason;
Tejun Heo79b42ba2009-04-15 06:21:10 +09002038 bool is_semb = class == ATA_DEV_SEMB;
Tejun Heo54936f82007-05-11 14:35:29 +02002039 int may_fallback = 1, tried_spinup = 0;
Tejun Heo49016ac2006-02-21 02:12:11 +09002040 int rc;
2041
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002042 if (ata_msg_ctl(ap))
Harvey Harrison7f5e4e82008-03-05 18:24:52 -08002043 ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER\n", __func__);
Tejun Heo49016ac2006-02-21 02:12:11 +09002044
Alan Cox963e4972008-07-24 17:16:06 +01002045retry:
Tejun Heo3373efd2006-05-15 20:57:53 +09002046 ata_tf_init(dev, &tf);
Tejun Heo49016ac2006-02-21 02:12:11 +09002047
2048 switch (class) {
Tejun Heo79b42ba2009-04-15 06:21:10 +09002049 case ATA_DEV_SEMB:
2050 class = ATA_DEV_ATA; /* some hard drives report SEMB sig */
Tejun Heo49016ac2006-02-21 02:12:11 +09002051 case ATA_DEV_ATA:
2052 tf.command = ATA_CMD_ID_ATA;
2053 break;
2054 case ATA_DEV_ATAPI:
2055 tf.command = ATA_CMD_ID_ATAPI;
2056 break;
2057 default:
2058 rc = -ENODEV;
2059 reason = "unsupported class";
2060 goto err_out;
2061 }
2062
2063 tf.protocol = ATA_PROT_PIO;
Tejun Heo81afe892007-02-07 12:37:41 -08002064
2065 /* Some devices choke if TF registers contain garbage. Make
2066 * sure those are properly initialized.
2067 */
2068 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2069
2070 /* Device presence detection is unreliable on some
2071 * controllers. Always poll IDENTIFY if available.
2072 */
2073 tf.flags |= ATA_TFLAG_POLLING;
Tejun Heo49016ac2006-02-21 02:12:11 +09002074
Alan Cox963e4972008-07-24 17:16:06 +01002075 if (ap->ops->read_id)
2076 err_mask = ap->ops->read_id(dev, &tf, id);
2077 else
2078 err_mask = ata_do_dev_read_id(dev, &tf, id);
2079
Tejun Heo49016ac2006-02-21 02:12:11 +09002080 if (err_mask) {
Tejun Heo800b3992006-12-03 21:34:13 +09002081 if (err_mask & AC_ERR_NODEV_HINT) {
Tejun Heo1ffc1512008-03-23 15:16:53 +09002082 ata_dev_printk(dev, KERN_DEBUG,
2083 "NODEV after polling detection\n");
Tejun Heo55a8e2c2006-11-10 18:08:10 +09002084 return -ENOENT;
2085 }
2086
Tejun Heo79b42ba2009-04-15 06:21:10 +09002087 if (is_semb) {
2088 ata_dev_printk(dev, KERN_INFO, "IDENTIFY failed on "
2089 "device w/ SEMB sig, disabled\n");
2090 /* SEMB is not supported yet */
2091 *p_class = ATA_DEV_SEMB_UNSUP;
2092 return 0;
2093 }
2094
Tejun Heo1ffc1512008-03-23 15:16:53 +09002095 if ((err_mask == AC_ERR_DEV) && (tf.feature & ATA_ABORTED)) {
2096 /* Device or controller might have reported
2097 * the wrong device class. Give a shot at the
2098 * other IDENTIFY if the current one is
2099 * aborted by the device.
2100 */
2101 if (may_fallback) {
2102 may_fallback = 0;
Tejun Heo54936f82007-05-11 14:35:29 +02002103
Tejun Heo1ffc1512008-03-23 15:16:53 +09002104 if (class == ATA_DEV_ATA)
2105 class = ATA_DEV_ATAPI;
2106 else
2107 class = ATA_DEV_ATA;
2108 goto retry;
2109 }
2110
2111 /* Control reaches here iff the device aborted
2112 * both flavors of IDENTIFYs which happens
2113 * sometimes with phantom devices.
2114 */
2115 ata_dev_printk(dev, KERN_DEBUG,
2116 "both IDENTIFYs aborted, assuming NODEV\n");
2117 return -ENOENT;
Tejun Heo54936f82007-05-11 14:35:29 +02002118 }
2119
Tejun Heo49016ac2006-02-21 02:12:11 +09002120 rc = -EIO;
2121 reason = "I/O error";
Tejun Heo49016ac2006-02-21 02:12:11 +09002122 goto err_out;
2123 }
2124
Tejun Heo43c9c592010-05-23 12:59:11 +02002125 if (dev->horkage & ATA_HORKAGE_DUMP_ID) {
2126 ata_dev_printk(dev, KERN_DEBUG, "dumping IDENTIFY data, "
2127 "class=%d may_fallback=%d tried_spinup=%d\n",
2128 class, may_fallback, tried_spinup);
2129 print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET,
2130 16, 2, id, ATA_ID_WORDS * sizeof(*id), true);
2131 }
2132
Tejun Heo54936f82007-05-11 14:35:29 +02002133 /* Falling back doesn't make sense if ID data was read
2134 * successfully at least once.
2135 */
2136 may_fallback = 0;
2137
Tejun Heo49016ac2006-02-21 02:12:11 +09002138 swap_buf_le16(id, ATA_ID_WORDS);
2139
Tejun Heo49016ac2006-02-21 02:12:11 +09002140 /* sanity check */
Tejun Heoa4f57492006-09-12 20:35:49 -07002141 rc = -EINVAL;
Alan Cox60700682007-06-07 16:13:55 +01002142 reason = "device reports invalid type";
Tejun Heoa4f57492006-09-12 20:35:49 -07002143
2144 if (class == ATA_DEV_ATA) {
2145 if (!ata_id_is_ata(id) && !ata_id_is_cfa(id))
2146 goto err_out;
2147 } else {
2148 if (ata_id_is_ata(id))
2149 goto err_out;
Tejun Heo49016ac2006-02-21 02:12:11 +09002150 }
2151
Mark Lord169439c2007-04-17 18:26:07 -04002152 if (!tried_spinup && (id[2] == 0x37c8 || id[2] == 0x738c)) {
2153 tried_spinup = 1;
2154 /*
2155 * Drive powered-up in standby mode, and requires a specific
2156 * SET_FEATURES spin-up subcommand before it will accept
2157 * anything other than the original IDENTIFY command.
2158 */
Jeff Garzik218f3d32007-10-25 00:33:27 -04002159 err_mask = ata_dev_set_feature(dev, SETFEATURES_SPINUP, 0);
Ryan Powerfb0582f92007-08-10 13:59:35 -07002160 if (err_mask && id[2] != 0x738c) {
Mark Lord169439c2007-04-17 18:26:07 -04002161 rc = -EIO;
2162 reason = "SPINUP failed";
2163 goto err_out;
2164 }
2165 /*
2166 * If the drive initially returned incomplete IDENTIFY info,
2167 * we now must reissue the IDENTIFY command.
2168 */
2169 if (id[2] == 0x37c8)
2170 goto retry;
2171 }
2172
Tejun Heobff04642006-11-10 18:08:10 +09002173 if ((flags & ATA_READID_POSTRESET) && class == ATA_DEV_ATA) {
Tejun Heo49016ac2006-02-21 02:12:11 +09002174 /*
2175 * The exact sequence expected by certain pre-ATA4 drives is:
2176 * SRST RESET
Alan Cox50a99012007-08-08 14:27:00 +01002177 * IDENTIFY (optional in early ATA)
2178 * INITIALIZE DEVICE PARAMETERS (later IDE and ATA)
Tejun Heo49016ac2006-02-21 02:12:11 +09002179 * anything else..
2180 * Some drives were very specific about that exact sequence.
Alan Cox50a99012007-08-08 14:27:00 +01002181 *
2182 * Note that ATA4 says lba is mandatory so the second check
Adam Buchbinderc9404c92009-12-18 15:40:42 -05002183 * should never trigger.
Tejun Heo49016ac2006-02-21 02:12:11 +09002184 */
2185 if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
Tejun Heo3373efd2006-05-15 20:57:53 +09002186 err_mask = ata_dev_init_params(dev, id[3], id[6]);
Tejun Heo49016ac2006-02-21 02:12:11 +09002187 if (err_mask) {
2188 rc = -EIO;
2189 reason = "INIT_DEV_PARAMS failed";
2190 goto err_out;
2191 }
2192
2193 /* current CHS translation info (id[53-58]) might be
2194 * changed. reread the identify device info.
2195 */
Tejun Heobff04642006-11-10 18:08:10 +09002196 flags &= ~ATA_READID_POSTRESET;
Tejun Heo49016ac2006-02-21 02:12:11 +09002197 goto retry;
2198 }
2199 }
2200
2201 *p_class = class;
Tejun Heofe635c72006-05-15 20:57:35 +09002202
Tejun Heo49016ac2006-02-21 02:12:11 +09002203 return 0;
2204
2205 err_out:
Tejun Heo88574552006-06-25 20:00:35 +09002206 if (ata_msg_warn(ap))
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002207 ata_dev_printk(dev, KERN_WARNING, "failed to IDENTIFY "
Tejun Heo88574552006-06-25 20:00:35 +09002208 "(%s, err_mask=0x%x)\n", reason, err_mask);
Tejun Heo49016ac2006-02-21 02:12:11 +09002209 return rc;
2210}
2211
Tejun Heo90627122009-01-29 20:31:36 +09002212static int ata_do_link_spd_horkage(struct ata_device *dev)
2213{
2214 struct ata_link *plink = ata_dev_phys_link(dev);
2215 u32 target, target_limit;
2216
2217 if (!sata_scr_valid(plink))
2218 return 0;
2219
2220 if (dev->horkage & ATA_HORKAGE_1_5_GBPS)
2221 target = 1;
2222 else
2223 return 0;
2224
2225 target_limit = (1 << target) - 1;
2226
2227 /* if already on stricter limit, no need to push further */
2228 if (plink->sata_spd_limit <= target_limit)
2229 return 0;
2230
2231 plink->sata_spd_limit = target_limit;
2232
2233 /* Request another EH round by returning -EAGAIN if link is
2234 * going faster than the target speed. Forward progress is
2235 * guaranteed by setting sata_spd_limit to target_limit above.
2236 */
2237 if (plink->sata_spd > target) {
2238 ata_dev_printk(dev, KERN_INFO,
2239 "applying link speed limit horkage to %s\n",
2240 sata_spd_string(target));
2241 return -EAGAIN;
2242 }
2243 return 0;
2244}
2245
Tejun Heo3373efd2006-05-15 20:57:53 +09002246static inline u8 ata_dev_knobble(struct ata_device *dev)
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09002247{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002248 struct ata_port *ap = dev->link->ap;
Jens Axboe9ce8e302008-08-27 15:23:18 +02002249
2250 if (ata_dev_blacklisted(dev) & ATA_HORKAGE_BRIDGE_OK)
2251 return 0;
2252
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002253 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09002254}
2255
Shaohua Li388539f2009-07-27 09:24:35 +08002256static int ata_dev_config_ncq(struct ata_device *dev,
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002257 char *desc, size_t desc_sz)
2258{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002259 struct ata_port *ap = dev->link->ap;
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002260 int hdepth = 0, ddepth = ata_id_queue_depth(dev->id);
Shaohua Li388539f2009-07-27 09:24:35 +08002261 unsigned int err_mask;
2262 char *aa_desc = "";
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002263
2264 if (!ata_id_has_ncq(dev->id)) {
2265 desc[0] = '\0';
Shaohua Li388539f2009-07-27 09:24:35 +08002266 return 0;
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002267 }
Tejun Heo75683fe2007-07-05 13:31:27 +09002268 if (dev->horkage & ATA_HORKAGE_NONCQ) {
Alan Cox6919a0a2006-10-27 19:08:46 -07002269 snprintf(desc, desc_sz, "NCQ (not used)");
Shaohua Li388539f2009-07-27 09:24:35 +08002270 return 0;
Alan Cox6919a0a2006-10-27 19:08:46 -07002271 }
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002272 if (ap->flags & ATA_FLAG_NCQ) {
Jeff Garzikcca39742006-08-24 03:19:22 -04002273 hdepth = min(ap->scsi_host->can_queue, ATA_MAX_QUEUE - 1);
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002274 dev->flags |= ATA_DFLAG_NCQ;
2275 }
2276
Shaohua Li388539f2009-07-27 09:24:35 +08002277 if (!(dev->horkage & ATA_HORKAGE_BROKEN_FPDMA_AA) &&
2278 (ap->flags & ATA_FLAG_FPDMA_AA) &&
2279 ata_id_has_fpdma_aa(dev->id)) {
2280 err_mask = ata_dev_set_feature(dev, SETFEATURES_SATA_ENABLE,
2281 SATA_FPDMA_AA);
2282 if (err_mask) {
2283 ata_dev_printk(dev, KERN_ERR, "failed to enable AA"
2284 "(error_mask=0x%x)\n", err_mask);
2285 if (err_mask != AC_ERR_DEV) {
2286 dev->horkage |= ATA_HORKAGE_BROKEN_FPDMA_AA;
2287 return -EIO;
2288 }
2289 } else
2290 aa_desc = ", AA";
2291 }
2292
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002293 if (hdepth >= ddepth)
Shaohua Li388539f2009-07-27 09:24:35 +08002294 snprintf(desc, desc_sz, "NCQ (depth %d)%s", ddepth, aa_desc);
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002295 else
Shaohua Li388539f2009-07-27 09:24:35 +08002296 snprintf(desc, desc_sz, "NCQ (depth %d/%d)%s", hdepth,
2297 ddepth, aa_desc);
2298 return 0;
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002299}
2300
Tejun Heo49016ac2006-02-21 02:12:11 +09002301/**
Tejun Heoffeae412006-03-01 16:09:35 +09002302 * ata_dev_configure - Configure the specified ATA/ATAPI device
Tejun Heoffeae412006-03-01 16:09:35 +09002303 * @dev: Target device to configure
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 *
Tejun Heoffeae412006-03-01 16:09:35 +09002305 * Configure @dev according to @dev->id. Generic and low-level
2306 * driver specific fixups are also applied.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 *
2308 * LOCKING:
Tejun Heoffeae412006-03-01 16:09:35 +09002309 * Kernel thread context (may sleep)
2310 *
2311 * RETURNS:
2312 * 0 on success, -errno otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 */
Tejun Heoefdaedc2006-11-01 18:38:52 +09002314int ata_dev_configure(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002316 struct ata_port *ap = dev->link->ap;
2317 struct ata_eh_context *ehc = &dev->link->eh_context;
Tejun Heo67465442007-05-15 03:28:16 +09002318 int print_info = ehc->i.flags & ATA_EHI_PRINTINFO;
Tejun Heo1148c3a2006-03-13 19:48:04 +09002319 const u16 *id = dev->id;
Tejun Heo7dc951a2007-11-27 19:43:42 +09002320 unsigned long xfer_mask;
Alan Coxb352e572006-08-10 18:52:12 +01002321 char revbuf[7]; /* XYZ-99\0 */
Eric D. Mudama3f64f562007-01-30 23:00:40 -07002322 char fwrevbuf[ATA_ID_FW_REV_LEN+1];
2323 char modelbuf[ATA_ID_PROD_LEN+1];
Brian Kinge6d902a2006-06-28 08:30:31 -05002324 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002326 if (!ata_dev_enabled(dev) && ata_msg_info(ap)) {
Tejun Heo44877b42007-02-21 01:06:51 +09002327 ata_dev_printk(dev, KERN_INFO, "%s: ENTER/EXIT -- nodev\n",
Harvey Harrison7f5e4e82008-03-05 18:24:52 -08002328 __func__);
Tejun Heoffeae412006-03-01 16:09:35 +09002329 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 }
2331
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002332 if (ata_msg_probe(ap))
Harvey Harrison7f5e4e82008-03-05 18:24:52 -08002333 ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER\n", __func__);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334
Tejun Heo75683fe2007-07-05 13:31:27 +09002335 /* set horkage */
2336 dev->horkage |= ata_dev_blacklisted(dev);
Tejun Heo33267322008-02-13 09:15:09 +09002337 ata_force_horkage(dev);
Tejun Heo75683fe2007-07-05 13:31:27 +09002338
Tejun Heo50af2fa2008-05-19 01:15:14 +09002339 if (dev->horkage & ATA_HORKAGE_DISABLE) {
2340 ata_dev_printk(dev, KERN_INFO,
2341 "unsupported device, disabling\n");
2342 ata_dev_disable(dev);
2343 return 0;
2344 }
2345
Tejun Heo2486fa52008-07-31 07:52:40 +09002346 if ((!atapi_enabled || (ap->flags & ATA_FLAG_NO_ATAPI)) &&
2347 dev->class == ATA_DEV_ATAPI) {
2348 ata_dev_printk(dev, KERN_WARNING,
2349 "WARNING: ATAPI is %s, device ignored.\n",
2350 atapi_enabled ? "not supported with this driver"
2351 : "disabled");
2352 ata_dev_disable(dev);
2353 return 0;
2354 }
2355
Tejun Heo90627122009-01-29 20:31:36 +09002356 rc = ata_do_link_spd_horkage(dev);
2357 if (rc)
2358 return rc;
2359
Tejun Heo67465442007-05-15 03:28:16 +09002360 /* let ACPI work its magic */
2361 rc = ata_acpi_on_devcfg(dev);
2362 if (rc)
2363 return rc;
Kristen Carlson Accardi08573a82006-11-10 16:14:47 -08002364
Tejun Heo05027ad2007-09-03 12:32:57 +09002365 /* massage HPA, do it early as it might change IDENTIFY data */
2366 rc = ata_hpa_resize(dev);
2367 if (rc)
2368 return rc;
2369
Tejun Heoc39f5eb2006-03-13 19:51:19 +09002370 /* print device capabilities */
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002371 if (ata_msg_probe(ap))
Tejun Heo88574552006-06-25 20:00:35 +09002372 ata_dev_printk(dev, KERN_DEBUG,
2373 "%s: cfg 49:%04x 82:%04x 83:%04x 84:%04x "
2374 "85:%04x 86:%04x 87:%04x 88:%04x\n",
Harvey Harrison7f5e4e82008-03-05 18:24:52 -08002375 __func__,
Tejun Heof15a1da2006-05-15 20:57:56 +09002376 id[49], id[82], id[83], id[84],
2377 id[85], id[86], id[87], id[88]);
Tejun Heoc39f5eb2006-03-13 19:51:19 +09002378
Tejun Heo208a9932006-03-05 17:55:58 +09002379 /* initialize to-be-configured parameters */
Tejun Heoea1dd4e2006-04-02 18:51:53 +09002380 dev->flags &= ~ATA_DFLAG_CFG_MASK;
Tejun Heo208a9932006-03-05 17:55:58 +09002381 dev->max_sectors = 0;
2382 dev->cdb_len = 0;
2383 dev->n_sectors = 0;
2384 dev->cylinders = 0;
2385 dev->heads = 0;
2386 dev->sectors = 0;
Mark Lorde18086d2009-03-19 13:32:21 -04002387 dev->multi_count = 0;
Tejun Heo208a9932006-03-05 17:55:58 +09002388
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 /*
2390 * common ATA, ATAPI feature tests
2391 */
2392
Tejun Heoff8854b2006-03-06 04:31:56 +09002393 /* find max transfer mode; for printk only */
Tejun Heo1148c3a2006-03-13 19:48:04 +09002394 xfer_mask = ata_id_xfermask(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002396 if (ata_msg_probe(ap))
2397 ata_dump_id(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398
Albert Leeef143d52007-06-05 13:01:33 +08002399 /* SCSI only uses 4-char revisions, dump full 8 chars from ATA */
2400 ata_id_c_string(dev->id, fwrevbuf, ATA_ID_FW_REV,
2401 sizeof(fwrevbuf));
2402
2403 ata_id_c_string(dev->id, modelbuf, ATA_ID_PROD,
2404 sizeof(modelbuf));
2405
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406 /* ATA-specific feature tests */
2407 if (dev->class == ATA_DEV_ATA) {
Alan Coxb352e572006-08-10 18:52:12 +01002408 if (ata_id_is_cfa(id)) {
Sergei Shtylyov62afe5d2009-04-13 20:50:00 +04002409 /* CPRM may make this media unusable */
2410 if (id[ATA_ID_CFA_KEY_MGMT] & 1)
Tejun Heo44877b42007-02-21 01:06:51 +09002411 ata_dev_printk(dev, KERN_WARNING,
2412 "supports DRM functions and may "
2413 "not be fully accessable.\n");
Alan Coxb352e572006-08-10 18:52:12 +01002414 snprintf(revbuf, 7, "CFA");
Alan Coxae8d4ee2007-11-04 22:05:49 -05002415 } else {
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002416 snprintf(revbuf, 7, "ATA-%d", ata_id_major_version(id));
Alan Coxae8d4ee2007-11-04 22:05:49 -05002417 /* Warn the user if the device has TPM extensions */
2418 if (ata_id_has_tpm(id))
2419 ata_dev_printk(dev, KERN_WARNING,
2420 "supports DRM functions and may "
2421 "not be fully accessable.\n");
2422 }
Alan Coxb352e572006-08-10 18:52:12 +01002423
Tejun Heo1148c3a2006-03-13 19:48:04 +09002424 dev->n_sectors = ata_id_n_sectors(id);
Tejun Heo29407402006-02-12 22:47:04 +09002425
Mark Lorde18086d2009-03-19 13:32:21 -04002426 /* get current R/W Multiple count setting */
2427 if ((dev->id[47] >> 8) == 0x80 && (dev->id[59] & 0x100)) {
2428 unsigned int max = dev->id[47] & 0xff;
2429 unsigned int cnt = dev->id[59] & 0xff;
2430 /* only recognize/allow powers of two here */
2431 if (is_power_of_2(max) && is_power_of_2(cnt))
2432 if (cnt <= max)
2433 dev->multi_count = cnt;
2434 }
Eric D. Mudama3f64f562007-01-30 23:00:40 -07002435
Tejun Heo1148c3a2006-03-13 19:48:04 +09002436 if (ata_id_has_lba(id)) {
Tejun Heo4c2d7212006-03-05 17:55:58 +09002437 const char *lba_desc;
Shaohua Li388539f2009-07-27 09:24:35 +08002438 char ncq_desc[24];
Albert Lee8bf62ece2005-05-12 15:29:42 -04002439
Tejun Heo4c2d7212006-03-05 17:55:58 +09002440 lba_desc = "LBA";
2441 dev->flags |= ATA_DFLAG_LBA;
Tejun Heo1148c3a2006-03-13 19:48:04 +09002442 if (ata_id_has_lba48(id)) {
Albert Lee8bf62ece2005-05-12 15:29:42 -04002443 dev->flags |= ATA_DFLAG_LBA48;
Tejun Heo4c2d7212006-03-05 17:55:58 +09002444 lba_desc = "LBA48";
Tejun Heo6fc49ad2006-11-11 20:10:45 +09002445
2446 if (dev->n_sectors >= (1UL << 28) &&
2447 ata_id_has_flush_ext(id))
2448 dev->flags |= ATA_DFLAG_FLUSH_EXT;
Tejun Heo4c2d7212006-03-05 17:55:58 +09002449 }
Albert Lee8bf62ece2005-05-12 15:29:42 -04002450
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002451 /* config NCQ */
Shaohua Li388539f2009-07-27 09:24:35 +08002452 rc = ata_dev_config_ncq(dev, ncq_desc, sizeof(ncq_desc));
2453 if (rc)
2454 return rc;
Tejun Heoa6e6ce82006-05-15 21:03:48 +09002455
Albert Lee8bf62ece2005-05-12 15:29:42 -04002456 /* print device info to dmesg */
Eric D. Mudama3f64f562007-01-30 23:00:40 -07002457 if (ata_msg_drv(ap) && print_info) {
2458 ata_dev_printk(dev, KERN_INFO,
2459 "%s: %s, %s, max %s\n",
2460 revbuf, modelbuf, fwrevbuf,
2461 ata_mode_string(xfer_mask));
2462 ata_dev_printk(dev, KERN_INFO,
2463 "%Lu sectors, multi %u: %s %s\n",
Tejun Heof15a1da2006-05-15 20:57:56 +09002464 (unsigned long long)dev->n_sectors,
Eric D. Mudama3f64f562007-01-30 23:00:40 -07002465 dev->multi_count, lba_desc, ncq_desc);
2466 }
Tejun Heoffeae412006-03-01 16:09:35 +09002467 } else {
Albert Lee8bf62ece2005-05-12 15:29:42 -04002468 /* CHS */
2469
2470 /* Default translation */
Tejun Heo1148c3a2006-03-13 19:48:04 +09002471 dev->cylinders = id[1];
2472 dev->heads = id[3];
2473 dev->sectors = id[6];
Albert Lee8bf62ece2005-05-12 15:29:42 -04002474
Tejun Heo1148c3a2006-03-13 19:48:04 +09002475 if (ata_id_current_chs_valid(id)) {
Albert Lee8bf62ece2005-05-12 15:29:42 -04002476 /* Current CHS translation is valid. */
Tejun Heo1148c3a2006-03-13 19:48:04 +09002477 dev->cylinders = id[54];
2478 dev->heads = id[55];
2479 dev->sectors = id[56];
Albert Lee8bf62ece2005-05-12 15:29:42 -04002480 }
2481
2482 /* print device info to dmesg */
Eric D. Mudama3f64f562007-01-30 23:00:40 -07002483 if (ata_msg_drv(ap) && print_info) {
Tejun Heo88574552006-06-25 20:00:35 +09002484 ata_dev_printk(dev, KERN_INFO,
Eric D. Mudama3f64f562007-01-30 23:00:40 -07002485 "%s: %s, %s, max %s\n",
2486 revbuf, modelbuf, fwrevbuf,
2487 ata_mode_string(xfer_mask));
Jeff Garzika84471f2007-02-26 05:51:33 -05002488 ata_dev_printk(dev, KERN_INFO,
Eric D. Mudama3f64f562007-01-30 23:00:40 -07002489 "%Lu sectors, multi %u, CHS %u/%u/%u\n",
2490 (unsigned long long)dev->n_sectors,
2491 dev->multi_count, dev->cylinders,
2492 dev->heads, dev->sectors);
2493 }
Albert Lee07f6f7d2005-11-01 19:33:20 +08002494 }
2495
Tejun Heo6e7846e2006-02-12 23:32:58 +09002496 dev->cdb_len = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497 }
2498
2499 /* ATAPI-specific feature tests */
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05002500 else if (dev->class == ATA_DEV_ATAPI) {
Tejun Heo854c73a2007-09-23 13:14:11 +09002501 const char *cdb_intr_string = "";
2502 const char *atapi_an_string = "";
Tejun Heo91163002008-02-21 13:25:50 +09002503 const char *dma_dir_string = "";
Tejun Heo7d77b242007-09-23 13:14:13 +09002504 u32 sntf;
Albert Lee08a556d2006-03-31 13:29:04 +08002505
Tejun Heo1148c3a2006-03-13 19:48:04 +09002506 rc = atapi_cdb_len(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002508 if (ata_msg_warn(ap))
Tejun Heo88574552006-06-25 20:00:35 +09002509 ata_dev_printk(dev, KERN_WARNING,
2510 "unsupported CDB len\n");
Tejun Heoffeae412006-03-01 16:09:35 +09002511 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 goto err_out_nosup;
2513 }
Tejun Heo6e7846e2006-02-12 23:32:58 +09002514 dev->cdb_len = (unsigned int) rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515
Tejun Heo7d77b242007-09-23 13:14:13 +09002516 /* Enable ATAPI AN if both the host and device have
2517 * the support. If PMP is attached, SNTF is required
2518 * to enable ATAPI AN to discern between PHY status
2519 * changed notifications and ATAPI ANs.
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04002520 */
Tejun Heoe7ecd432010-05-19 15:38:58 +02002521 if (atapi_an &&
2522 (ap->flags & ATA_FLAG_AN) && ata_id_has_atapi_AN(id) &&
Tejun Heo071f44b2008-04-07 22:47:22 +09002523 (!sata_pmp_attached(ap) ||
Tejun Heo7d77b242007-09-23 13:14:13 +09002524 sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf) == 0)) {
Tejun Heo854c73a2007-09-23 13:14:11 +09002525 unsigned int err_mask;
2526
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04002527 /* issue SET feature command to turn this on */
Jeff Garzik218f3d32007-10-25 00:33:27 -04002528 err_mask = ata_dev_set_feature(dev,
2529 SETFEATURES_SATA_ENABLE, SATA_AN);
Tejun Heo854c73a2007-09-23 13:14:11 +09002530 if (err_mask)
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04002531 ata_dev_printk(dev, KERN_ERR,
Tejun Heo854c73a2007-09-23 13:14:11 +09002532 "failed to enable ATAPI AN "
2533 "(err_mask=0x%x)\n", err_mask);
2534 else {
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04002535 dev->flags |= ATA_DFLAG_AN;
Tejun Heo854c73a2007-09-23 13:14:11 +09002536 atapi_an_string = ", ATAPI AN";
2537 }
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04002538 }
2539
Albert Lee08a556d2006-03-31 13:29:04 +08002540 if (ata_id_cdb_intr(dev->id)) {
Albert Lee312f7da2005-09-27 17:38:03 +08002541 dev->flags |= ATA_DFLAG_CDB_INTR;
Albert Lee08a556d2006-03-31 13:29:04 +08002542 cdb_intr_string = ", CDB intr";
2543 }
Albert Lee312f7da2005-09-27 17:38:03 +08002544
Tejun Heo91163002008-02-21 13:25:50 +09002545 if (atapi_dmadir || atapi_id_dmadir(dev->id)) {
2546 dev->flags |= ATA_DFLAG_DMADIR;
2547 dma_dir_string = ", DMADIR";
2548 }
2549
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 /* print device info to dmesg */
Borislav Petkov5afc8142006-06-27 14:51:25 +02002551 if (ata_msg_drv(ap) && print_info)
Albert Leeef143d52007-06-05 13:01:33 +08002552 ata_dev_printk(dev, KERN_INFO,
Tejun Heo91163002008-02-21 13:25:50 +09002553 "ATAPI: %s, %s, max %s%s%s%s\n",
Albert Leeef143d52007-06-05 13:01:33 +08002554 modelbuf, fwrevbuf,
Tejun Heo12436c32006-05-15 20:59:15 +09002555 ata_mode_string(xfer_mask),
Tejun Heo91163002008-02-21 13:25:50 +09002556 cdb_intr_string, atapi_an_string,
2557 dma_dir_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002558 }
2559
Tejun Heo914ed352006-11-01 18:39:55 +09002560 /* determine max_sectors */
2561 dev->max_sectors = ATA_MAX_SECTORS;
2562 if (dev->flags & ATA_DFLAG_LBA48)
2563 dev->max_sectors = ATA_MAX_SECTORS_LBA48;
2564
Tejun Heoc93b2632010-09-01 17:50:04 +02002565 if (!(dev->horkage & ATA_HORKAGE_LPM)) {
Kristen Carlson Accardica773292007-10-25 00:58:59 -04002566 if (ata_id_has_hipm(dev->id))
2567 dev->flags |= ATA_DFLAG_HIPM;
2568 if (ata_id_has_dipm(dev->id))
2569 dev->flags |= ATA_DFLAG_DIPM;
2570 }
2571
Alan Coxc5038fc2007-10-25 14:21:16 +01002572 /* Limit PATA drive on SATA cable bridge transfers to udma5,
2573 200 sectors */
Tejun Heo3373efd2006-05-15 20:57:53 +09002574 if (ata_dev_knobble(dev)) {
Borislav Petkov5afc8142006-06-27 14:51:25 +02002575 if (ata_msg_drv(ap) && print_info)
Tejun Heof15a1da2006-05-15 20:57:56 +09002576 ata_dev_printk(dev, KERN_INFO,
2577 "applying bridge limits\n");
Tejun Heo5a529132006-03-24 14:07:50 +09002578 dev->udma_mask &= ATA_UDMA5;
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09002579 dev->max_sectors = ATA_MAX_SECTORS;
2580 }
2581
Tony Battersbyf8d8e572007-10-30 11:44:35 -04002582 if ((dev->class == ATA_DEV_ATAPI) &&
Albert Leef442cd82007-11-15 10:35:47 +09002583 (atapi_command_packet_set(id) == TYPE_TAPE)) {
Tony Battersbyf8d8e572007-10-30 11:44:35 -04002584 dev->max_sectors = ATA_MAX_SECTORS_TAPE;
Albert Leef442cd82007-11-15 10:35:47 +09002585 dev->horkage |= ATA_HORKAGE_STUCK_ERR;
2586 }
Tony Battersbyf8d8e572007-10-30 11:44:35 -04002587
Tejun Heo75683fe2007-07-05 13:31:27 +09002588 if (dev->horkage & ATA_HORKAGE_MAX_SEC_128)
Tejun Heo03ec52d2007-04-12 13:38:11 +09002589 dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_128,
2590 dev->max_sectors);
Albert Lee18d6e9d2007-04-02 11:34:15 +08002591
Tejun Heoc93b2632010-09-01 17:50:04 +02002592 if (ata_dev_blacklisted(dev) & ATA_HORKAGE_LPM) {
2593 dev->horkage |= ATA_HORKAGE_LPM;
Kristen Carlson Accardica773292007-10-25 00:58:59 -04002594
2595 /* reset link pm_policy for this port to no pm */
Tejun Heoc93b2632010-09-01 17:50:04 +02002596 ap->lpm_policy = ATA_LPM_MAX_POWER;
Kristen Carlson Accardica773292007-10-25 00:58:59 -04002597 }
2598
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09002599 if (ap->ops->dev_config)
Alancd0d3bb2007-03-02 00:56:15 +00002600 ap->ops->dev_config(dev);
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09002601
Alan Coxc5038fc2007-10-25 14:21:16 +01002602 if (dev->horkage & ATA_HORKAGE_DIAGNOSTIC) {
2603 /* Let the user know. We don't want to disallow opens for
2604 rescue purposes, or in case the vendor is just a blithering
2605 idiot. Do this after the dev_config call as some controllers
2606 with buggy firmware may want to avoid reporting false device
2607 bugs */
2608
2609 if (print_info) {
2610 ata_dev_printk(dev, KERN_WARNING,
2611"Drive reports diagnostics failure. This may indicate a drive\n");
2612 ata_dev_printk(dev, KERN_WARNING,
2613"fault or invalid emulation. Contact drive vendor for information.\n");
2614 }
2615 }
2616
Tejun Heoac70a962008-11-27 13:36:48 +09002617 if ((dev->horkage & ATA_HORKAGE_FIRMWARE_WARN) && print_info) {
2618 ata_dev_printk(dev, KERN_WARNING, "WARNING: device requires "
2619 "firmware update to be fully functional.\n");
2620 ata_dev_printk(dev, KERN_WARNING, " contact the vendor "
2621 "or visit http://ata.wiki.kernel.org.\n");
2622 }
2623
Tejun Heoffeae412006-03-01 16:09:35 +09002624 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625
2626err_out_nosup:
Borislav Petkov0dd4b212006-06-23 02:29:08 -04002627 if (ata_msg_probe(ap))
Tejun Heo88574552006-06-25 20:00:35 +09002628 ata_dev_printk(dev, KERN_DEBUG,
Harvey Harrison7f5e4e82008-03-05 18:24:52 -08002629 "%s: EXIT, err\n", __func__);
Tejun Heoffeae412006-03-01 16:09:35 +09002630 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631}
2632
2633/**
Alan Cox2e41e8e2007-03-08 23:19:19 +00002634 * ata_cable_40wire - return 40 wire cable type
Alan Coxbe0d18d2007-03-06 02:37:56 -08002635 * @ap: port
2636 *
Alan Cox2e41e8e2007-03-08 23:19:19 +00002637 * Helper method for drivers which want to hardwire 40 wire cable
Alan Coxbe0d18d2007-03-06 02:37:56 -08002638 * detection.
2639 */
2640
2641int ata_cable_40wire(struct ata_port *ap)
2642{
2643 return ATA_CBL_PATA40;
2644}
2645
2646/**
Alan Cox2e41e8e2007-03-08 23:19:19 +00002647 * ata_cable_80wire - return 80 wire cable type
Alan Coxbe0d18d2007-03-06 02:37:56 -08002648 * @ap: port
2649 *
Alan Cox2e41e8e2007-03-08 23:19:19 +00002650 * Helper method for drivers which want to hardwire 80 wire cable
Alan Coxbe0d18d2007-03-06 02:37:56 -08002651 * detection.
2652 */
2653
2654int ata_cable_80wire(struct ata_port *ap)
2655{
2656 return ATA_CBL_PATA80;
2657}
2658
2659/**
2660 * ata_cable_unknown - return unknown PATA cable.
2661 * @ap: port
2662 *
2663 * Helper method for drivers which have no PATA cable detection.
2664 */
2665
2666int ata_cable_unknown(struct ata_port *ap)
2667{
2668 return ATA_CBL_PATA_UNK;
2669}
2670
2671/**
Tejun Heoc88f90c2007-11-27 19:43:48 +09002672 * ata_cable_ignore - return ignored PATA cable.
2673 * @ap: port
2674 *
2675 * Helper method for drivers which don't use cable type to limit
2676 * transfer mode.
2677 */
2678int ata_cable_ignore(struct ata_port *ap)
2679{
2680 return ATA_CBL_PATA_IGN;
2681}
2682
2683/**
Alan Coxbe0d18d2007-03-06 02:37:56 -08002684 * ata_cable_sata - return SATA cable type
2685 * @ap: port
2686 *
2687 * Helper method for drivers which have SATA cables
2688 */
2689
2690int ata_cable_sata(struct ata_port *ap)
2691{
2692 return ATA_CBL_SATA;
2693}
2694
2695/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 * ata_bus_probe - Reset and probe ATA bus
2697 * @ap: Bus to probe
2698 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04002699 * Master ATA bus probing function. Initiates a hardware-dependent
2700 * bus reset, then attempts to identify any devices found on
2701 * the bus.
2702 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002704 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002705 *
2706 * RETURNS:
Tejun Heo96072e62006-04-01 01:38:17 +09002707 * Zero on success, negative errno otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708 */
2709
Brian King80289162006-08-07 14:27:31 -05002710int ata_bus_probe(struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711{
Tejun Heo28ca5c52006-03-01 16:09:36 +09002712 unsigned int classes[ATA_MAX_DEVICES];
Tejun Heo14d2bac2006-04-02 17:54:46 +09002713 int tries[ATA_MAX_DEVICES];
Tejun Heof58229f2007-08-06 18:36:23 +09002714 int rc;
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002715 struct ata_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716
Tejun Heo1eca4362008-11-03 20:03:17 +09002717 ata_for_each_dev(dev, &ap->link, ALL)
Tejun Heof58229f2007-08-06 18:36:23 +09002718 tries[dev->devno] = ATA_PROBE_MAX_TRIES;
Tejun Heo14d2bac2006-04-02 17:54:46 +09002719
2720 retry:
Tejun Heo1eca4362008-11-03 20:03:17 +09002721 ata_for_each_dev(dev, &ap->link, ALL) {
Tejun Heocdeab112007-10-29 16:41:09 +09002722 /* If we issue an SRST then an ATA drive (not ATAPI)
2723 * may change configuration and be in PIO0 timing. If
2724 * we do a hard reset (or are coming from power on)
2725 * this is true for ATA or ATAPI. Until we've set a
2726 * suitable controller mode we should not touch the
2727 * bus as we may be talking too fast.
2728 */
2729 dev->pio_mode = XFER_PIO_0;
2730
2731 /* If the controller has a pio mode setup function
2732 * then use it to set the chipset to rights. Don't
2733 * touch the DMA setup as that will be dealt with when
2734 * configuring devices.
2735 */
2736 if (ap->ops->set_piomode)
2737 ap->ops->set_piomode(ap, dev);
2738 }
2739
Tejun Heo20444702006-03-13 01:57:01 +09002740 /* reset and determine device classes */
Tejun Heo52783c52006-05-31 18:28:22 +09002741 ap->ops->phy_reset(ap);
Tejun Heo2061a472006-03-12 00:57:39 +09002742
Tejun Heo1eca4362008-11-03 20:03:17 +09002743 ata_for_each_dev(dev, &ap->link, ALL) {
Tejun Heo3e4ec342010-05-10 21:41:30 +02002744 if (dev->class != ATA_DEV_UNKNOWN)
Tejun Heo52783c52006-05-31 18:28:22 +09002745 classes[dev->devno] = dev->class;
2746 else
2747 classes[dev->devno] = ATA_DEV_NONE;
Tejun Heo20444702006-03-13 01:57:01 +09002748
Tejun Heo52783c52006-05-31 18:28:22 +09002749 dev->class = ATA_DEV_UNKNOWN;
Tejun Heo28ca5c52006-03-01 16:09:36 +09002750 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751
Jeff Garzikf31f0cc2007-03-02 17:47:28 -05002752 /* read IDENTIFY page and configure devices. We have to do the identify
2753 specific sequence bass-ackwards so that PDIAG- is released by
2754 the slave device */
2755
Tejun Heo1eca4362008-11-03 20:03:17 +09002756 ata_for_each_dev(dev, &ap->link, ALL_REVERSE) {
Tejun Heof58229f2007-08-06 18:36:23 +09002757 if (tries[dev->devno])
2758 dev->class = classes[dev->devno];
Tejun Heo14d2bac2006-04-02 17:54:46 +09002759
Tejun Heoe1211e32006-04-01 01:38:18 +09002760 if (!ata_dev_enabled(dev))
Tejun Heoffeae412006-03-01 16:09:35 +09002761 continue;
2762
Tejun Heobff04642006-11-10 18:08:10 +09002763 rc = ata_dev_read_id(dev, &dev->class, ATA_READID_POSTRESET,
2764 dev->id);
Tejun Heo14d2bac2006-04-02 17:54:46 +09002765 if (rc)
2766 goto fail;
Jeff Garzikf31f0cc2007-03-02 17:47:28 -05002767 }
2768
Alan Coxbe0d18d2007-03-06 02:37:56 -08002769 /* Now ask for the cable type as PDIAG- should have been released */
2770 if (ap->ops->cable_detect)
2771 ap->cbl = ap->ops->cable_detect(ap);
2772
Tejun Heo1eca4362008-11-03 20:03:17 +09002773 /* We may have SATA bridge glue hiding here irrespective of
2774 * the reported cable types and sensed types. When SATA
2775 * drives indicate we have a bridge, we don't know which end
2776 * of the link the bridge is which is a problem.
2777 */
2778 ata_for_each_dev(dev, &ap->link, ENABLED)
Alan Cox614fe292007-08-22 23:22:45 +01002779 if (ata_id_is_sata(dev->id))
2780 ap->cbl = ATA_CBL_SATA;
Alan Cox614fe292007-08-22 23:22:45 +01002781
Jeff Garzikf31f0cc2007-03-02 17:47:28 -05002782 /* After the identify sequence we can now set up the devices. We do
2783 this in the normal order so that the user doesn't get confused */
2784
Tejun Heo1eca4362008-11-03 20:03:17 +09002785 ata_for_each_dev(dev, &ap->link, ENABLED) {
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002786 ap->link.eh_context.i.flags |= ATA_EHI_PRINTINFO;
Tejun Heoefdaedc2006-11-01 18:38:52 +09002787 rc = ata_dev_configure(dev);
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002788 ap->link.eh_context.i.flags &= ~ATA_EHI_PRINTINFO;
Tejun Heo14d2bac2006-04-02 17:54:46 +09002789 if (rc)
2790 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 }
2792
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002793 /* configure transfer mode */
Tejun Heo02607312007-08-06 18:36:23 +09002794 rc = ata_set_mode(&ap->link, &dev);
Tejun Heo4ae72a12007-02-02 16:22:30 +09002795 if (rc)
Tejun Heo51713d32006-04-11 22:26:29 +09002796 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797
Tejun Heo1eca4362008-11-03 20:03:17 +09002798 ata_for_each_dev(dev, &ap->link, ENABLED)
2799 return 0;
Alan Coxe35a9e02006-03-27 18:46:37 +01002800
Tejun Heo96072e62006-04-01 01:38:17 +09002801 return -ENODEV;
Tejun Heo14d2bac2006-04-02 17:54:46 +09002802
2803 fail:
Tejun Heo4ae72a12007-02-02 16:22:30 +09002804 tries[dev->devno]--;
2805
Tejun Heo14d2bac2006-04-02 17:54:46 +09002806 switch (rc) {
2807 case -EINVAL:
Tejun Heo4ae72a12007-02-02 16:22:30 +09002808 /* eeek, something went very wrong, give up */
Tejun Heo14d2bac2006-04-02 17:54:46 +09002809 tries[dev->devno] = 0;
2810 break;
Tejun Heo4ae72a12007-02-02 16:22:30 +09002811
2812 case -ENODEV:
2813 /* give it just one more chance */
2814 tries[dev->devno] = min(tries[dev->devno], 1);
Tejun Heo14d2bac2006-04-02 17:54:46 +09002815 case -EIO:
Tejun Heo4ae72a12007-02-02 16:22:30 +09002816 if (tries[dev->devno] == 1) {
2817 /* This is the last chance, better to slow
2818 * down than lose it.
2819 */
Tejun Heoa07d4992009-01-29 20:31:33 +09002820 sata_down_spd_limit(&ap->link, 0);
Tejun Heo4ae72a12007-02-02 16:22:30 +09002821 ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
2822 }
Tejun Heo14d2bac2006-04-02 17:54:46 +09002823 }
2824
Tejun Heo4ae72a12007-02-02 16:22:30 +09002825 if (!tries[dev->devno])
Tejun Heo3373efd2006-05-15 20:57:53 +09002826 ata_dev_disable(dev);
Tejun Heoec573752006-04-11 22:26:29 +09002827
Tejun Heo14d2bac2006-04-02 17:54:46 +09002828 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829}
2830
2831/**
Tejun Heo3be680b2005-12-19 22:35:02 +09002832 * sata_print_link_status - Print SATA link status
Tejun Heo936fd732007-08-06 18:36:23 +09002833 * @link: SATA link to printk link status about
Tejun Heo3be680b2005-12-19 22:35:02 +09002834 *
2835 * This function prints link speed and status of a SATA link.
2836 *
2837 * LOCKING:
2838 * None.
2839 */
Adrian Bunk6bdb4fc2008-04-21 11:51:11 +03002840static void sata_print_link_status(struct ata_link *link)
Tejun Heo3be680b2005-12-19 22:35:02 +09002841{
Tejun Heo6d5f9732006-04-03 00:09:41 +09002842 u32 sstatus, scontrol, tmp;
Tejun Heo3be680b2005-12-19 22:35:02 +09002843
Tejun Heo936fd732007-08-06 18:36:23 +09002844 if (sata_scr_read(link, SCR_STATUS, &sstatus))
Tejun Heo3be680b2005-12-19 22:35:02 +09002845 return;
Tejun Heo936fd732007-08-06 18:36:23 +09002846 sata_scr_read(link, SCR_CONTROL, &scontrol);
Tejun Heo3be680b2005-12-19 22:35:02 +09002847
Tejun Heob1c72912008-07-31 17:02:43 +09002848 if (ata_phys_link_online(link)) {
Tejun Heo3be680b2005-12-19 22:35:02 +09002849 tmp = (sstatus >> 4) & 0xf;
Tejun Heo936fd732007-08-06 18:36:23 +09002850 ata_link_printk(link, KERN_INFO,
Tejun Heof15a1da2006-05-15 20:57:56 +09002851 "SATA link up %s (SStatus %X SControl %X)\n",
2852 sata_spd_string(tmp), sstatus, scontrol);
Tejun Heo3be680b2005-12-19 22:35:02 +09002853 } else {
Tejun Heo936fd732007-08-06 18:36:23 +09002854 ata_link_printk(link, KERN_INFO,
Tejun Heof15a1da2006-05-15 20:57:56 +09002855 "SATA link down (SStatus %X SControl %X)\n",
2856 sstatus, scontrol);
Tejun Heo3be680b2005-12-19 22:35:02 +09002857 }
2858}
2859
2860/**
Alan Coxebdfca62006-03-23 15:38:34 +00002861 * ata_dev_pair - return other device on cable
Alan Coxebdfca62006-03-23 15:38:34 +00002862 * @adev: device
2863 *
2864 * Obtain the other device on the same cable, or if none is
2865 * present NULL is returned
2866 */
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002867
Tejun Heo3373efd2006-05-15 20:57:53 +09002868struct ata_device *ata_dev_pair(struct ata_device *adev)
Alan Coxebdfca62006-03-23 15:38:34 +00002869{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002870 struct ata_link *link = adev->link;
2871 struct ata_device *pair = &link->device[1 - adev->devno];
Tejun Heoe1211e32006-04-01 01:38:18 +09002872 if (!ata_dev_enabled(pair))
Alan Coxebdfca62006-03-23 15:38:34 +00002873 return NULL;
2874 return pair;
2875}
2876
2877/**
Tejun Heo3c567b72006-05-15 20:57:23 +09002878 * sata_down_spd_limit - adjust SATA spd limit downward
Tejun Heo936fd732007-08-06 18:36:23 +09002879 * @link: Link to adjust SATA spd limit for
Tejun Heoa07d4992009-01-29 20:31:33 +09002880 * @spd_limit: Additional limit
Tejun Heo1c3fae42006-04-02 20:53:28 +09002881 *
Tejun Heo936fd732007-08-06 18:36:23 +09002882 * Adjust SATA spd limit of @link downward. Note that this
Tejun Heo1c3fae42006-04-02 20:53:28 +09002883 * function only adjusts the limit. The change must be applied
Tejun Heo3c567b72006-05-15 20:57:23 +09002884 * using sata_set_spd().
Tejun Heo1c3fae42006-04-02 20:53:28 +09002885 *
Tejun Heoa07d4992009-01-29 20:31:33 +09002886 * If @spd_limit is non-zero, the speed is limited to equal to or
2887 * lower than @spd_limit if such speed is supported. If
2888 * @spd_limit is slower than any supported speed, only the lowest
2889 * supported speed is allowed.
2890 *
Tejun Heo1c3fae42006-04-02 20:53:28 +09002891 * LOCKING:
2892 * Inherited from caller.
2893 *
2894 * RETURNS:
2895 * 0 on success, negative errno on failure
2896 */
Tejun Heoa07d4992009-01-29 20:31:33 +09002897int sata_down_spd_limit(struct ata_link *link, u32 spd_limit)
Tejun Heo1c3fae42006-04-02 20:53:28 +09002898{
Tejun Heo81952c52006-05-15 20:57:47 +09002899 u32 sstatus, spd, mask;
Tejun Heoa07d4992009-01-29 20:31:33 +09002900 int rc, bit;
Tejun Heo1c3fae42006-04-02 20:53:28 +09002901
Tejun Heo936fd732007-08-06 18:36:23 +09002902 if (!sata_scr_valid(link))
Tejun Heo008a7892007-07-16 14:29:40 +09002903 return -EOPNOTSUPP;
2904
2905 /* If SCR can be read, use it to determine the current SPD.
Tejun Heo936fd732007-08-06 18:36:23 +09002906 * If not, use cached value in link->sata_spd.
Tejun Heo008a7892007-07-16 14:29:40 +09002907 */
Tejun Heo936fd732007-08-06 18:36:23 +09002908 rc = sata_scr_read(link, SCR_STATUS, &sstatus);
Tejun Heo9913ff82009-01-29 20:31:31 +09002909 if (rc == 0 && ata_sstatus_online(sstatus))
Tejun Heo008a7892007-07-16 14:29:40 +09002910 spd = (sstatus >> 4) & 0xf;
2911 else
Tejun Heo936fd732007-08-06 18:36:23 +09002912 spd = link->sata_spd;
Tejun Heo1c3fae42006-04-02 20:53:28 +09002913
Tejun Heo936fd732007-08-06 18:36:23 +09002914 mask = link->sata_spd_limit;
Tejun Heo1c3fae42006-04-02 20:53:28 +09002915 if (mask <= 1)
2916 return -EINVAL;
Tejun Heo008a7892007-07-16 14:29:40 +09002917
2918 /* unconditionally mask off the highest bit */
Tejun Heoa07d4992009-01-29 20:31:33 +09002919 bit = fls(mask) - 1;
2920 mask &= ~(1 << bit);
Tejun Heo1c3fae42006-04-02 20:53:28 +09002921
Tejun Heo008a7892007-07-16 14:29:40 +09002922 /* Mask off all speeds higher than or equal to the current
2923 * one. Force 1.5Gbps if current SPD is not available.
2924 */
2925 if (spd > 1)
2926 mask &= (1 << (spd - 1)) - 1;
2927 else
2928 mask &= 1;
2929
2930 /* were we already at the bottom? */
Tejun Heo1c3fae42006-04-02 20:53:28 +09002931 if (!mask)
2932 return -EINVAL;
2933
Tejun Heoa07d4992009-01-29 20:31:33 +09002934 if (spd_limit) {
2935 if (mask & ((1 << spd_limit) - 1))
2936 mask &= (1 << spd_limit) - 1;
2937 else {
2938 bit = ffs(mask) - 1;
2939 mask = 1 << bit;
2940 }
2941 }
2942
Tejun Heo936fd732007-08-06 18:36:23 +09002943 link->sata_spd_limit = mask;
Tejun Heo1c3fae42006-04-02 20:53:28 +09002944
Tejun Heo936fd732007-08-06 18:36:23 +09002945 ata_link_printk(link, KERN_WARNING, "limiting SATA link speed to %s\n",
Tejun Heof15a1da2006-05-15 20:57:56 +09002946 sata_spd_string(fls(mask)));
Tejun Heo1c3fae42006-04-02 20:53:28 +09002947
2948 return 0;
2949}
2950
Tejun Heo936fd732007-08-06 18:36:23 +09002951static int __sata_set_spd_needed(struct ata_link *link, u32 *scontrol)
Tejun Heo1c3fae42006-04-02 20:53:28 +09002952{
Tejun Heo52702222007-10-31 10:17:07 +09002953 struct ata_link *host_link = &link->ap->link;
2954 u32 limit, target, spd;
Tejun Heo1c3fae42006-04-02 20:53:28 +09002955
Tejun Heo52702222007-10-31 10:17:07 +09002956 limit = link->sata_spd_limit;
2957
2958 /* Don't configure downstream link faster than upstream link.
2959 * It doesn't speed up anything and some PMPs choke on such
2960 * configuration.
2961 */
2962 if (!ata_is_host_link(link) && host_link->sata_spd)
2963 limit &= (1 << host_link->sata_spd) - 1;
2964
2965 if (limit == UINT_MAX)
2966 target = 0;
Tejun Heo1c3fae42006-04-02 20:53:28 +09002967 else
Tejun Heo52702222007-10-31 10:17:07 +09002968 target = fls(limit);
Tejun Heo1c3fae42006-04-02 20:53:28 +09002969
2970 spd = (*scontrol >> 4) & 0xf;
Tejun Heo52702222007-10-31 10:17:07 +09002971 *scontrol = (*scontrol & ~0xf0) | ((target & 0xf) << 4);
Tejun Heo1c3fae42006-04-02 20:53:28 +09002972
Tejun Heo52702222007-10-31 10:17:07 +09002973 return spd != target;
Tejun Heo1c3fae42006-04-02 20:53:28 +09002974}
2975
2976/**
Tejun Heo3c567b72006-05-15 20:57:23 +09002977 * sata_set_spd_needed - is SATA spd configuration needed
Tejun Heo936fd732007-08-06 18:36:23 +09002978 * @link: Link in question
Tejun Heo1c3fae42006-04-02 20:53:28 +09002979 *
2980 * Test whether the spd limit in SControl matches
Tejun Heo936fd732007-08-06 18:36:23 +09002981 * @link->sata_spd_limit. This function is used to determine
Tejun Heo1c3fae42006-04-02 20:53:28 +09002982 * whether hardreset is necessary to apply SATA spd
2983 * configuration.
2984 *
2985 * LOCKING:
2986 * Inherited from caller.
2987 *
2988 * RETURNS:
2989 * 1 if SATA spd configuration is needed, 0 otherwise.
2990 */
Adrian Bunk1dc55e82008-04-21 11:51:17 +03002991static int sata_set_spd_needed(struct ata_link *link)
Tejun Heo1c3fae42006-04-02 20:53:28 +09002992{
2993 u32 scontrol;
2994
Tejun Heo936fd732007-08-06 18:36:23 +09002995 if (sata_scr_read(link, SCR_CONTROL, &scontrol))
Tejun Heodb64bcf2007-10-31 10:17:06 +09002996 return 1;
Tejun Heo1c3fae42006-04-02 20:53:28 +09002997
Tejun Heo936fd732007-08-06 18:36:23 +09002998 return __sata_set_spd_needed(link, &scontrol);
Tejun Heo1c3fae42006-04-02 20:53:28 +09002999}
3000
3001/**
Tejun Heo3c567b72006-05-15 20:57:23 +09003002 * sata_set_spd - set SATA spd according to spd limit
Tejun Heo936fd732007-08-06 18:36:23 +09003003 * @link: Link to set SATA spd for
Tejun Heo1c3fae42006-04-02 20:53:28 +09003004 *
Tejun Heo936fd732007-08-06 18:36:23 +09003005 * Set SATA spd of @link according to sata_spd_limit.
Tejun Heo1c3fae42006-04-02 20:53:28 +09003006 *
3007 * LOCKING:
3008 * Inherited from caller.
3009 *
3010 * RETURNS:
3011 * 0 if spd doesn't need to be changed, 1 if spd has been
Tejun Heo81952c52006-05-15 20:57:47 +09003012 * changed. Negative errno if SCR registers are inaccessible.
Tejun Heo1c3fae42006-04-02 20:53:28 +09003013 */
Tejun Heo936fd732007-08-06 18:36:23 +09003014int sata_set_spd(struct ata_link *link)
Tejun Heo1c3fae42006-04-02 20:53:28 +09003015{
3016 u32 scontrol;
Tejun Heo81952c52006-05-15 20:57:47 +09003017 int rc;
Tejun Heo1c3fae42006-04-02 20:53:28 +09003018
Tejun Heo936fd732007-08-06 18:36:23 +09003019 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
Tejun Heo81952c52006-05-15 20:57:47 +09003020 return rc;
Tejun Heo1c3fae42006-04-02 20:53:28 +09003021
Tejun Heo936fd732007-08-06 18:36:23 +09003022 if (!__sata_set_spd_needed(link, &scontrol))
Tejun Heo1c3fae42006-04-02 20:53:28 +09003023 return 0;
3024
Tejun Heo936fd732007-08-06 18:36:23 +09003025 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
Tejun Heo81952c52006-05-15 20:57:47 +09003026 return rc;
3027
Tejun Heo1c3fae42006-04-02 20:53:28 +09003028 return 1;
3029}
3030
Alan Cox452503f2005-10-21 19:01:32 -04003031/*
3032 * This mode timing computation functionality is ported over from
3033 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
3034 */
3035/*
Alan Coxb352e572006-08-10 18:52:12 +01003036 * PIO 0-4, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
Alan Cox452503f2005-10-21 19:01:32 -04003037 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
Alan Coxb352e572006-08-10 18:52:12 +01003038 * for UDMA6, which is currently supported only by Maxtor drives.
3039 *
3040 * For PIO 5/6 MWDMA 3/4 see the CFA specification 3.0.
Alan Cox452503f2005-10-21 19:01:32 -04003041 */
3042
3043static const struct ata_timing ata_timing[] = {
David Daney3ada9c12009-01-15 17:45:31 -08003044/* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 0, 960, 0 }, */
3045 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 0, 600, 0 },
3046 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 0, 383, 0 },
3047 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 0, 240, 0 },
3048 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 0, 180, 0 },
3049 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 0, 120, 0 },
3050 { XFER_PIO_5, 15, 65, 25, 100, 65, 25, 0, 100, 0 },
3051 { XFER_PIO_6, 10, 55, 20, 80, 55, 20, 0, 80, 0 },
Alan Cox452503f2005-10-21 19:01:32 -04003052
David Daney3ada9c12009-01-15 17:45:31 -08003053 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 50, 960, 0 },
3054 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 30, 480, 0 },
3055 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 20, 240, 0 },
Alan Cox452503f2005-10-21 19:01:32 -04003056
David Daney3ada9c12009-01-15 17:45:31 -08003057 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 20, 480, 0 },
3058 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 5, 150, 0 },
3059 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 5, 120, 0 },
3060 { XFER_MW_DMA_3, 25, 0, 0, 0, 65, 25, 5, 100, 0 },
3061 { XFER_MW_DMA_4, 25, 0, 0, 0, 55, 20, 5, 80, 0 },
Alan Cox452503f2005-10-21 19:01:32 -04003062
David Daney3ada9c12009-01-15 17:45:31 -08003063/* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 0, 150 }, */
3064 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 0, 120 },
3065 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 0, 80 },
3066 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 0, 60 },
3067 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 0, 45 },
3068 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 0, 30 },
3069 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 0, 20 },
3070 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 0, 15 },
Alan Cox452503f2005-10-21 19:01:32 -04003071
3072 { 0xFF }
3073};
3074
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003075#define ENOUGH(v, unit) (((v)-1)/(unit)+1)
3076#define EZ(v, unit) ((v)?ENOUGH(v, unit):0)
Alan Cox452503f2005-10-21 19:01:32 -04003077
3078static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
3079{
David Daney3ada9c12009-01-15 17:45:31 -08003080 q->setup = EZ(t->setup * 1000, T);
3081 q->act8b = EZ(t->act8b * 1000, T);
3082 q->rec8b = EZ(t->rec8b * 1000, T);
3083 q->cyc8b = EZ(t->cyc8b * 1000, T);
3084 q->active = EZ(t->active * 1000, T);
3085 q->recover = EZ(t->recover * 1000, T);
3086 q->dmack_hold = EZ(t->dmack_hold * 1000, T);
3087 q->cycle = EZ(t->cycle * 1000, T);
3088 q->udma = EZ(t->udma * 1000, UT);
Alan Cox452503f2005-10-21 19:01:32 -04003089}
3090
3091void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
3092 struct ata_timing *m, unsigned int what)
3093{
3094 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
3095 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
3096 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
3097 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
3098 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
3099 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
David Daney3ada9c12009-01-15 17:45:31 -08003100 if (what & ATA_TIMING_DMACK_HOLD) m->dmack_hold = max(a->dmack_hold, b->dmack_hold);
Alan Cox452503f2005-10-21 19:01:32 -04003101 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
3102 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
3103}
3104
Tejun Heo63573572007-11-27 19:43:39 +09003105const struct ata_timing *ata_timing_find_mode(u8 xfer_mode)
Alan Cox452503f2005-10-21 19:01:32 -04003106{
Tejun Heo70cd0712007-11-27 19:43:40 +09003107 const struct ata_timing *t = ata_timing;
Alan Cox452503f2005-10-21 19:01:32 -04003108
Tejun Heo70cd0712007-11-27 19:43:40 +09003109 while (xfer_mode > t->mode)
3110 t++;
3111
3112 if (xfer_mode == t->mode)
3113 return t;
3114 return NULL;
Alan Cox452503f2005-10-21 19:01:32 -04003115}
3116
3117int ata_timing_compute(struct ata_device *adev, unsigned short speed,
3118 struct ata_timing *t, int T, int UT)
3119{
Bartlomiej Zolnierkiewicz9e8808a2010-01-18 18:13:57 +01003120 const u16 *id = adev->id;
Alan Cox452503f2005-10-21 19:01:32 -04003121 const struct ata_timing *s;
3122 struct ata_timing p;
3123
3124 /*
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05003125 * Find the mode.
Albert Lee75b1f2f2005-11-16 17:06:18 +08003126 */
Alan Cox452503f2005-10-21 19:01:32 -04003127
3128 if (!(s = ata_timing_find_mode(speed)))
3129 return -EINVAL;
3130
Albert Lee75b1f2f2005-11-16 17:06:18 +08003131 memcpy(t, s, sizeof(*s));
3132
Alan Cox452503f2005-10-21 19:01:32 -04003133 /*
3134 * If the drive is an EIDE drive, it can tell us it needs extended
3135 * PIO/MW_DMA cycle timing.
3136 */
3137
Bartlomiej Zolnierkiewicz9e8808a2010-01-18 18:13:57 +01003138 if (id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
Alan Cox452503f2005-10-21 19:01:32 -04003139 memset(&p, 0, sizeof(p));
Bartlomiej Zolnierkiewicz9e8808a2010-01-18 18:13:57 +01003140
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003141 if (speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
Bartlomiej Zolnierkiewicz9e8808a2010-01-18 18:13:57 +01003142 if (speed <= XFER_PIO_2)
3143 p.cycle = p.cyc8b = id[ATA_ID_EIDE_PIO];
3144 else if ((speed <= XFER_PIO_4) ||
3145 (speed == XFER_PIO_5 && !ata_id_is_cfa(id)))
3146 p.cycle = p.cyc8b = id[ATA_ID_EIDE_PIO_IORDY];
3147 } else if (speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2)
3148 p.cycle = id[ATA_ID_EIDE_DMA_MIN];
3149
Alan Cox452503f2005-10-21 19:01:32 -04003150 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
3151 }
3152
3153 /*
3154 * Convert the timing to bus clock counts.
3155 */
3156
Albert Lee75b1f2f2005-11-16 17:06:18 +08003157 ata_timing_quantize(t, t, T, UT);
Alan Cox452503f2005-10-21 19:01:32 -04003158
3159 /*
Randy Dunlapc893a3a2006-01-28 13:15:32 -05003160 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
3161 * S.M.A.R.T * and some other commands. We have to ensure that the
3162 * DMA cycle timing is slower/equal than the fastest PIO timing.
Alan Cox452503f2005-10-21 19:01:32 -04003163 */
3164
Alanfd3367af2006-12-07 12:41:18 +00003165 if (speed > XFER_PIO_6) {
Alan Cox452503f2005-10-21 19:01:32 -04003166 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
3167 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
3168 }
3169
3170 /*
Randy Dunlapc893a3a2006-01-28 13:15:32 -05003171 * Lengthen active & recovery time so that cycle time is correct.
Alan Cox452503f2005-10-21 19:01:32 -04003172 */
3173
3174 if (t->act8b + t->rec8b < t->cyc8b) {
3175 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
3176 t->rec8b = t->cyc8b - t->act8b;
3177 }
3178
3179 if (t->active + t->recover < t->cycle) {
3180 t->active += (t->cycle - (t->active + t->recover)) / 2;
3181 t->recover = t->cycle - t->active;
3182 }
Jeff Garzika617c092007-05-21 20:14:23 -04003183
Alan Cox4f701d12007-04-23 11:55:36 +01003184 /* In a few cases quantisation may produce enough errors to
3185 leave t->cycle too low for the sum of active and recovery
3186 if so we must correct this */
3187 if (t->active + t->recover > t->cycle)
3188 t->cycle = t->active + t->recover;
Alan Cox452503f2005-10-21 19:01:32 -04003189
3190 return 0;
3191}
3192
Tejun Heocf176e12006-04-02 17:54:46 +09003193/**
Tejun Heoa0f79b92007-12-18 16:33:05 +09003194 * ata_timing_cycle2mode - find xfer mode for the specified cycle duration
3195 * @xfer_shift: ATA_SHIFT_* value for transfer type to examine.
3196 * @cycle: cycle duration in ns
3197 *
3198 * Return matching xfer mode for @cycle. The returned mode is of
3199 * the transfer type specified by @xfer_shift. If @cycle is too
3200 * slow for @xfer_shift, 0xff is returned. If @cycle is faster
3201 * than the fastest known mode, the fasted mode is returned.
3202 *
3203 * LOCKING:
3204 * None.
3205 *
3206 * RETURNS:
3207 * Matching xfer_mode, 0xff if no match found.
3208 */
3209u8 ata_timing_cycle2mode(unsigned int xfer_shift, int cycle)
3210{
3211 u8 base_mode = 0xff, last_mode = 0xff;
3212 const struct ata_xfer_ent *ent;
3213 const struct ata_timing *t;
3214
3215 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
3216 if (ent->shift == xfer_shift)
3217 base_mode = ent->base;
3218
3219 for (t = ata_timing_find_mode(base_mode);
3220 t && ata_xfer_mode2shift(t->mode) == xfer_shift; t++) {
3221 unsigned short this_cycle;
3222
3223 switch (xfer_shift) {
3224 case ATA_SHIFT_PIO:
3225 case ATA_SHIFT_MWDMA:
3226 this_cycle = t->cycle;
3227 break;
3228 case ATA_SHIFT_UDMA:
3229 this_cycle = t->udma;
3230 break;
3231 default:
3232 return 0xff;
3233 }
3234
3235 if (cycle > this_cycle)
3236 break;
3237
3238 last_mode = t->mode;
3239 }
3240
3241 return last_mode;
3242}
3243
3244/**
Tejun Heocf176e12006-04-02 17:54:46 +09003245 * ata_down_xfermask_limit - adjust dev xfer masks downward
Tejun Heocf176e12006-04-02 17:54:46 +09003246 * @dev: Device to adjust xfer masks
Tejun Heo458337d2007-02-02 16:22:30 +09003247 * @sel: ATA_DNXFER_* selector
Tejun Heocf176e12006-04-02 17:54:46 +09003248 *
3249 * Adjust xfer masks of @dev downward. Note that this function
3250 * does not apply the change. Invoking ata_set_mode() afterwards
3251 * will apply the limit.
3252 *
3253 * LOCKING:
3254 * Inherited from caller.
3255 *
3256 * RETURNS:
3257 * 0 on success, negative errno on failure
3258 */
Tejun Heo458337d2007-02-02 16:22:30 +09003259int ata_down_xfermask_limit(struct ata_device *dev, unsigned int sel)
Tejun Heocf176e12006-04-02 17:54:46 +09003260{
Tejun Heo458337d2007-02-02 16:22:30 +09003261 char buf[32];
Tejun Heo7dc951a2007-11-27 19:43:42 +09003262 unsigned long orig_mask, xfer_mask;
3263 unsigned long pio_mask, mwdma_mask, udma_mask;
Tejun Heo458337d2007-02-02 16:22:30 +09003264 int quiet, highbit;
Tejun Heocf176e12006-04-02 17:54:46 +09003265
Tejun Heo458337d2007-02-02 16:22:30 +09003266 quiet = !!(sel & ATA_DNXFER_QUIET);
3267 sel &= ~ATA_DNXFER_QUIET;
Tejun Heocf176e12006-04-02 17:54:46 +09003268
Tejun Heo458337d2007-02-02 16:22:30 +09003269 xfer_mask = orig_mask = ata_pack_xfermask(dev->pio_mask,
3270 dev->mwdma_mask,
3271 dev->udma_mask);
3272 ata_unpack_xfermask(xfer_mask, &pio_mask, &mwdma_mask, &udma_mask);
Tejun Heocf176e12006-04-02 17:54:46 +09003273
Tejun Heo458337d2007-02-02 16:22:30 +09003274 switch (sel) {
3275 case ATA_DNXFER_PIO:
3276 highbit = fls(pio_mask) - 1;
3277 pio_mask &= ~(1 << highbit);
3278 break;
3279
3280 case ATA_DNXFER_DMA:
3281 if (udma_mask) {
3282 highbit = fls(udma_mask) - 1;
3283 udma_mask &= ~(1 << highbit);
3284 if (!udma_mask)
3285 return -ENOENT;
3286 } else if (mwdma_mask) {
3287 highbit = fls(mwdma_mask) - 1;
3288 mwdma_mask &= ~(1 << highbit);
3289 if (!mwdma_mask)
3290 return -ENOENT;
3291 }
3292 break;
3293
3294 case ATA_DNXFER_40C:
3295 udma_mask &= ATA_UDMA_MASK_40C;
3296 break;
3297
3298 case ATA_DNXFER_FORCE_PIO0:
3299 pio_mask &= 1;
3300 case ATA_DNXFER_FORCE_PIO:
3301 mwdma_mask = 0;
3302 udma_mask = 0;
3303 break;
3304
Tejun Heo458337d2007-02-02 16:22:30 +09003305 default:
3306 BUG();
3307 }
3308
3309 xfer_mask &= ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
3310
3311 if (!(xfer_mask & ATA_MASK_PIO) || xfer_mask == orig_mask)
3312 return -ENOENT;
3313
3314 if (!quiet) {
3315 if (xfer_mask & (ATA_MASK_MWDMA | ATA_MASK_UDMA))
3316 snprintf(buf, sizeof(buf), "%s:%s",
3317 ata_mode_string(xfer_mask),
3318 ata_mode_string(xfer_mask & ATA_MASK_PIO));
3319 else
3320 snprintf(buf, sizeof(buf), "%s",
3321 ata_mode_string(xfer_mask));
3322
3323 ata_dev_printk(dev, KERN_WARNING,
3324 "limiting speed to %s\n", buf);
3325 }
Tejun Heocf176e12006-04-02 17:54:46 +09003326
3327 ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask,
3328 &dev->udma_mask);
3329
Tejun Heocf176e12006-04-02 17:54:46 +09003330 return 0;
Tejun Heocf176e12006-04-02 17:54:46 +09003331}
3332
Tejun Heo3373efd2006-05-15 20:57:53 +09003333static int ata_dev_set_mode(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003334{
Tejun Heod0cb43b2009-07-09 09:27:50 +09003335 struct ata_port *ap = dev->link->ap;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09003336 struct ata_eh_context *ehc = &dev->link->eh_context;
Tejun Heod0cb43b2009-07-09 09:27:50 +09003337 const bool nosetxfer = dev->horkage & ATA_HORKAGE_NOSETXFER;
Tejun Heo4055dee2008-02-07 10:34:08 +09003338 const char *dev_err_whine = "";
3339 int ign_dev_err = 0;
Tejun Heod0cb43b2009-07-09 09:27:50 +09003340 unsigned int err_mask = 0;
Tejun Heo83206a22006-03-24 15:25:31 +09003341 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342
Tejun Heoe8384602006-04-02 18:51:53 +09003343 dev->flags &= ~ATA_DFLAG_PIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 if (dev->xfer_shift == ATA_SHIFT_PIO)
3345 dev->flags |= ATA_DFLAG_PIO;
3346
Tejun Heod0cb43b2009-07-09 09:27:50 +09003347 if (nosetxfer && ap->flags & ATA_FLAG_SATA && ata_id_is_sata(dev->id))
3348 dev_err_whine = " (SET_XFERMODE skipped)";
3349 else {
3350 if (nosetxfer)
3351 ata_dev_printk(dev, KERN_WARNING,
3352 "NOSETXFER but PATA detected - can't "
3353 "skip SETXFER, might malfunction\n");
3354 err_mask = ata_dev_set_xfermode(dev);
3355 }
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003356
Tejun Heo4055dee2008-02-07 10:34:08 +09003357 if (err_mask & ~AC_ERR_DEV)
3358 goto fail;
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003359
Tejun Heo4055dee2008-02-07 10:34:08 +09003360 /* revalidate */
Tejun Heobaa1e782006-11-01 18:39:27 +09003361 ehc->i.flags |= ATA_EHI_POST_SETMODE;
Tejun Heo422c9da2007-09-23 13:14:12 +09003362 rc = ata_dev_revalidate(dev, ATA_DEV_UNKNOWN, 0);
Tejun Heobaa1e782006-11-01 18:39:27 +09003363 ehc->i.flags &= ~ATA_EHI_POST_SETMODE;
Tejun Heo5eb45c02006-04-02 18:51:52 +09003364 if (rc)
Tejun Heo83206a22006-03-24 15:25:31 +09003365 return rc;
Tejun Heo48a8a142006-03-05 17:55:58 +09003366
Alan Coxb93fda12008-04-08 16:36:44 +01003367 if (dev->xfer_shift == ATA_SHIFT_PIO) {
3368 /* Old CFA may refuse this command, which is just fine */
3369 if (ata_id_is_cfa(dev->id))
3370 ign_dev_err = 1;
3371 /* Catch several broken garbage emulations plus some pre
3372 ATA devices */
3373 if (ata_id_major_version(dev->id) == 0 &&
3374 dev->pio_mode <= XFER_PIO_2)
3375 ign_dev_err = 1;
3376 /* Some very old devices and some bad newer ones fail
3377 any kind of SET_XFERMODE request but support PIO0-2
3378 timings and no IORDY */
3379 if (!ata_id_has_iordy(dev->id) && dev->pio_mode <= XFER_PIO_2)
3380 ign_dev_err = 1;
3381 }
Tejun Heo4055dee2008-02-07 10:34:08 +09003382 /* Early MWDMA devices do DMA but don't allow DMA mode setting.
3383 Don't fail an MWDMA0 set IFF the device indicates it is in MWDMA0 */
3384 if (dev->xfer_shift == ATA_SHIFT_MWDMA &&
3385 dev->dma_mode == XFER_MW_DMA_0 &&
3386 (dev->id[63] >> 8) & 1)
3387 ign_dev_err = 1;
3388
3389 /* if the device is actually configured correctly, ignore dev err */
3390 if (dev->xfer_mode == ata_xfer_mask2mode(ata_id_xfermask(dev->id)))
3391 ign_dev_err = 1;
3392
3393 if (err_mask & AC_ERR_DEV) {
3394 if (!ign_dev_err)
3395 goto fail;
3396 else
3397 dev_err_whine = " (device error ignored)";
3398 }
3399
Tejun Heo23e71c32006-03-06 04:31:57 +09003400 DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n",
3401 dev->xfer_shift, (int)dev->xfer_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402
Tejun Heo4055dee2008-02-07 10:34:08 +09003403 ata_dev_printk(dev, KERN_INFO, "configured for %s%s\n",
3404 ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)),
3405 dev_err_whine);
3406
Tejun Heo83206a22006-03-24 15:25:31 +09003407 return 0;
Tejun Heo4055dee2008-02-07 10:34:08 +09003408
3409 fail:
3410 ata_dev_printk(dev, KERN_ERR, "failed to set xfermode "
3411 "(err_mask=0x%x)\n", err_mask);
3412 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003413}
3414
Linus Torvalds1da177e2005-04-16 15:20:36 -07003415/**
Alan04351822007-03-06 02:37:52 -08003416 * ata_do_set_mode - Program timings and issue SET FEATURES - XFER
Tejun Heo02607312007-08-06 18:36:23 +09003417 * @link: link on which timings will be programmed
Joe Perches1967b7f2008-02-03 17:08:11 +02003418 * @r_failed_dev: out parameter for failed device
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419 *
Alan04351822007-03-06 02:37:52 -08003420 * Standard implementation of the function used to tune and set
3421 * ATA device disk transfer mode (PIO3, UDMA6, etc.). If
3422 * ata_dev_set_mode() fails, pointer to the failing device is
Tejun Heoe82cbdb2006-04-01 01:38:18 +09003423 * returned in @r_failed_dev.
Jeff Garzik780a87f2005-05-30 15:41:05 -04003424 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003425 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003426 * PCI/etc. bus probe sem.
Tejun Heoe82cbdb2006-04-01 01:38:18 +09003427 *
3428 * RETURNS:
3429 * 0 on success, negative errno otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430 */
Alan04351822007-03-06 02:37:52 -08003431
Tejun Heo02607312007-08-06 18:36:23 +09003432int ata_do_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003433{
Tejun Heo02607312007-08-06 18:36:23 +09003434 struct ata_port *ap = link->ap;
Tejun Heoe8e06192006-04-01 01:38:18 +09003435 struct ata_device *dev;
Tejun Heof58229f2007-08-06 18:36:23 +09003436 int rc = 0, used_dma = 0, found = 0;
Tejun Heo3adcebb2006-05-15 20:57:37 +09003437
Tejun Heoa6d5a512006-03-06 04:31:57 +09003438 /* step 1: calculate xfer_mask */
Tejun Heo1eca4362008-11-03 20:03:17 +09003439 ata_for_each_dev(dev, link, ENABLED) {
Tejun Heo7dc951a2007-11-27 19:43:42 +09003440 unsigned long pio_mask, dma_mask;
Alan Coxb3a70602007-10-02 12:38:26 -04003441 unsigned int mode_mask;
Tejun Heoa6d5a512006-03-06 04:31:57 +09003442
Alan Coxb3a70602007-10-02 12:38:26 -04003443 mode_mask = ATA_DMA_MASK_ATA;
3444 if (dev->class == ATA_DEV_ATAPI)
3445 mode_mask = ATA_DMA_MASK_ATAPI;
3446 else if (ata_id_is_cfa(dev->id))
3447 mode_mask = ATA_DMA_MASK_CFA;
3448
Tejun Heo3373efd2006-05-15 20:57:53 +09003449 ata_dev_xfermask(dev);
Tejun Heo33267322008-02-13 09:15:09 +09003450 ata_force_xfermask(dev);
Tejun Heoa6d5a512006-03-06 04:31:57 +09003451
Tejun Heoacf356b2006-03-24 14:07:50 +09003452 pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0);
3453 dma_mask = ata_pack_xfermask(0, dev->mwdma_mask, dev->udma_mask);
Alan Coxb3a70602007-10-02 12:38:26 -04003454
3455 if (libata_dma_mask & mode_mask)
3456 dma_mask = ata_pack_xfermask(0, dev->mwdma_mask, dev->udma_mask);
3457 else
3458 dma_mask = 0;
3459
Tejun Heoacf356b2006-03-24 14:07:50 +09003460 dev->pio_mode = ata_xfer_mask2mode(pio_mask);
3461 dev->dma_mode = ata_xfer_mask2mode(dma_mask);
Alan Cox5444a6f2006-03-27 18:58:20 +01003462
Tejun Heo4f659772006-04-01 01:38:18 +09003463 found = 1;
Alan Coxb15b3eb2008-08-01 09:18:34 +01003464 if (ata_dma_enabled(dev))
Alan Cox5444a6f2006-03-27 18:58:20 +01003465 used_dma = 1;
Tejun Heoa6d5a512006-03-06 04:31:57 +09003466 }
Tejun Heo4f659772006-04-01 01:38:18 +09003467 if (!found)
Tejun Heoe82cbdb2006-04-01 01:38:18 +09003468 goto out;
Tejun Heoa6d5a512006-03-06 04:31:57 +09003469
3470 /* step 2: always set host PIO timings */
Tejun Heo1eca4362008-11-03 20:03:17 +09003471 ata_for_each_dev(dev, link, ENABLED) {
Tejun Heo70cd0712007-11-27 19:43:40 +09003472 if (dev->pio_mode == 0xff) {
Tejun Heof15a1da2006-05-15 20:57:56 +09003473 ata_dev_printk(dev, KERN_WARNING, "no PIO support\n");
Tejun Heoe8e06192006-04-01 01:38:18 +09003474 rc = -EINVAL;
Tejun Heoe82cbdb2006-04-01 01:38:18 +09003475 goto out;
Tejun Heoe8e06192006-04-01 01:38:18 +09003476 }
3477
3478 dev->xfer_mode = dev->pio_mode;
3479 dev->xfer_shift = ATA_SHIFT_PIO;
3480 if (ap->ops->set_piomode)
3481 ap->ops->set_piomode(ap, dev);
3482 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003483
Tejun Heoa6d5a512006-03-06 04:31:57 +09003484 /* step 3: set host DMA timings */
Tejun Heo1eca4362008-11-03 20:03:17 +09003485 ata_for_each_dev(dev, link, ENABLED) {
3486 if (!ata_dma_enabled(dev))
Tejun Heoe8e06192006-04-01 01:38:18 +09003487 continue;
3488
3489 dev->xfer_mode = dev->dma_mode;
3490 dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
3491 if (ap->ops->set_dmamode)
3492 ap->ops->set_dmamode(ap, dev);
3493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494
3495 /* step 4: update devices' xfer mode */
Tejun Heo1eca4362008-11-03 20:03:17 +09003496 ata_for_each_dev(dev, link, ENABLED) {
Tejun Heo3373efd2006-05-15 20:57:53 +09003497 rc = ata_dev_set_mode(dev);
Tejun Heo5bbc53f2006-04-01 01:38:17 +09003498 if (rc)
Tejun Heoe82cbdb2006-04-01 01:38:18 +09003499 goto out;
Tejun Heo83206a22006-03-24 15:25:31 +09003500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003501
Tejun Heoe8e06192006-04-01 01:38:18 +09003502 /* Record simplex status. If we selected DMA then the other
3503 * host channels are not permitted to do so.
Alan Cox5444a6f2006-03-27 18:58:20 +01003504 */
Jeff Garzikcca39742006-08-24 03:19:22 -04003505 if (used_dma && (ap->host->flags & ATA_HOST_SIMPLEX))
Alan032af1c2007-03-01 17:36:46 +00003506 ap->host->simplex_claimed = ap;
Alan Cox5444a6f2006-03-27 18:58:20 +01003507
Tejun Heoe82cbdb2006-04-01 01:38:18 +09003508 out:
3509 if (rc)
3510 *r_failed_dev = dev;
3511 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512}
3513
3514/**
Tejun Heoaa2731a2008-04-07 22:47:19 +09003515 * ata_wait_ready - wait for link to become ready
3516 * @link: link to be waited on
3517 * @deadline: deadline jiffies for the operation
3518 * @check_ready: callback to check link readiness
3519 *
3520 * Wait for @link to become ready. @check_ready should return
3521 * positive number if @link is ready, 0 if it isn't, -ENODEV if
3522 * link doesn't seem to be occupied, other errno for other error
3523 * conditions.
3524 *
3525 * Transient -ENODEV conditions are allowed for
3526 * ATA_TMOUT_FF_WAIT.
3527 *
3528 * LOCKING:
3529 * EH context.
3530 *
3531 * RETURNS:
3532 * 0 if @linke is ready before @deadline; otherwise, -errno.
3533 */
3534int ata_wait_ready(struct ata_link *link, unsigned long deadline,
3535 int (*check_ready)(struct ata_link *link))
3536{
3537 unsigned long start = jiffies;
Tejun Heob48d58f2010-04-09 19:46:38 +09003538 unsigned long nodev_deadline;
Tejun Heoaa2731a2008-04-07 22:47:19 +09003539 int warned = 0;
3540
Tejun Heob48d58f2010-04-09 19:46:38 +09003541 /* choose which 0xff timeout to use, read comment in libata.h */
3542 if (link->ap->host->flags & ATA_HOST_PARALLEL_SCAN)
3543 nodev_deadline = ata_deadline(start, ATA_TMOUT_FF_WAIT_LONG);
3544 else
3545 nodev_deadline = ata_deadline(start, ATA_TMOUT_FF_WAIT);
3546
Tejun Heob1c72912008-07-31 17:02:43 +09003547 /* Slave readiness can't be tested separately from master. On
3548 * M/S emulation configuration, this function should be called
3549 * only on the master and it will handle both master and slave.
3550 */
3551 WARN_ON(link == link->ap->slave_link);
3552
Tejun Heoaa2731a2008-04-07 22:47:19 +09003553 if (time_after(nodev_deadline, deadline))
3554 nodev_deadline = deadline;
3555
3556 while (1) {
3557 unsigned long now = jiffies;
3558 int ready, tmp;
3559
3560 ready = tmp = check_ready(link);
3561 if (ready > 0)
3562 return 0;
3563
Tejun Heob48d58f2010-04-09 19:46:38 +09003564 /*
3565 * -ENODEV could be transient. Ignore -ENODEV if link
Tejun Heoaa2731a2008-04-07 22:47:19 +09003566 * is online. Also, some SATA devices take a long
Tejun Heob48d58f2010-04-09 19:46:38 +09003567 * time to clear 0xff after reset. Wait for
3568 * ATA_TMOUT_FF_WAIT[_LONG] on -ENODEV if link isn't
3569 * offline.
Tejun Heoaa2731a2008-04-07 22:47:19 +09003570 *
3571 * Note that some PATA controllers (pata_ali) explode
3572 * if status register is read more than once when
3573 * there's no device attached.
3574 */
3575 if (ready == -ENODEV) {
3576 if (ata_link_online(link))
3577 ready = 0;
3578 else if ((link->ap->flags & ATA_FLAG_SATA) &&
3579 !ata_link_offline(link) &&
3580 time_before(now, nodev_deadline))
3581 ready = 0;
3582 }
3583
3584 if (ready)
3585 return ready;
3586 if (time_after(now, deadline))
3587 return -EBUSY;
3588
3589 if (!warned && time_after(now, start + 5 * HZ) &&
3590 (deadline - now > 3 * HZ)) {
3591 ata_link_printk(link, KERN_WARNING,
3592 "link is slow to respond, please be patient "
3593 "(ready=%d)\n", tmp);
3594 warned = 1;
3595 }
3596
3597 msleep(50);
3598 }
3599}
3600
3601/**
3602 * ata_wait_after_reset - wait for link to become ready after reset
3603 * @link: link to be waited on
3604 * @deadline: deadline jiffies for the operation
3605 * @check_ready: callback to check link readiness
3606 *
3607 * Wait for @link to become ready after reset.
3608 *
3609 * LOCKING:
3610 * EH context.
3611 *
3612 * RETURNS:
3613 * 0 if @linke is ready before @deadline; otherwise, -errno.
3614 */
Harvey Harrison2b4221b2008-04-24 18:37:34 -07003615int ata_wait_after_reset(struct ata_link *link, unsigned long deadline,
Tejun Heoaa2731a2008-04-07 22:47:19 +09003616 int (*check_ready)(struct ata_link *link))
3617{
Tejun Heo341c2c92008-05-20 02:17:51 +09003618 msleep(ATA_WAIT_AFTER_RESET);
Tejun Heoaa2731a2008-04-07 22:47:19 +09003619
3620 return ata_wait_ready(link, deadline, check_ready);
3621}
3622
3623/**
Tejun Heo936fd732007-08-06 18:36:23 +09003624 * sata_link_debounce - debounce SATA phy status
3625 * @link: ATA link to debounce SATA phy status for
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003626 * @params: timing parameters { interval, duratinon, timeout } in msec
Tejun Heod4b2bab2007-02-02 16:50:52 +09003627 * @deadline: deadline jiffies for the operation
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003628 *
Tejun Heo1152b262010-09-01 17:50:05 +02003629 * Make sure SStatus of @link reaches stable state, determined by
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003630 * holding the same value where DET is not 1 for @duration polled
3631 * every @interval, before @timeout. Timeout constraints the
Tejun Heod4b2bab2007-02-02 16:50:52 +09003632 * beginning of the stable state. Because DET gets stuck at 1 on
3633 * some controllers after hot unplugging, this functions waits
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003634 * until timeout then returns 0 if DET is stable at 1.
3635 *
Tejun Heod4b2bab2007-02-02 16:50:52 +09003636 * @timeout is further limited by @deadline. The sooner of the
3637 * two is used.
3638 *
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003639 * LOCKING:
3640 * Kernel thread context (may sleep)
3641 *
3642 * RETURNS:
3643 * 0 on success, -errno on failure.
3644 */
Tejun Heo936fd732007-08-06 18:36:23 +09003645int sata_link_debounce(struct ata_link *link, const unsigned long *params,
3646 unsigned long deadline)
Tejun Heo7a7921e2006-02-02 18:20:00 +09003647{
Tejun Heo341c2c92008-05-20 02:17:51 +09003648 unsigned long interval = params[0];
3649 unsigned long duration = params[1];
Tejun Heod4b2bab2007-02-02 16:50:52 +09003650 unsigned long last_jiffies, t;
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003651 u32 last, cur;
3652 int rc;
3653
Tejun Heo341c2c92008-05-20 02:17:51 +09003654 t = ata_deadline(jiffies, params[2]);
Tejun Heod4b2bab2007-02-02 16:50:52 +09003655 if (time_before(t, deadline))
3656 deadline = t;
3657
Tejun Heo936fd732007-08-06 18:36:23 +09003658 if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003659 return rc;
3660 cur &= 0xf;
3661
3662 last = cur;
3663 last_jiffies = jiffies;
3664
3665 while (1) {
Tejun Heo341c2c92008-05-20 02:17:51 +09003666 msleep(interval);
Tejun Heo936fd732007-08-06 18:36:23 +09003667 if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003668 return rc;
3669 cur &= 0xf;
3670
3671 /* DET stable? */
3672 if (cur == last) {
Tejun Heod4b2bab2007-02-02 16:50:52 +09003673 if (cur == 1 && time_before(jiffies, deadline))
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003674 continue;
Tejun Heo341c2c92008-05-20 02:17:51 +09003675 if (time_after(jiffies,
3676 ata_deadline(last_jiffies, duration)))
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003677 return 0;
3678 continue;
3679 }
3680
3681 /* unstable, start over */
3682 last = cur;
3683 last_jiffies = jiffies;
3684
Tejun Heof1545152007-07-16 14:29:40 +09003685 /* Check deadline. If debouncing failed, return
3686 * -EPIPE to tell upper layer to lower link speed.
3687 */
Tejun Heod4b2bab2007-02-02 16:50:52 +09003688 if (time_after(jiffies, deadline))
Tejun Heof1545152007-07-16 14:29:40 +09003689 return -EPIPE;
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003690 }
3691}
3692
3693/**
Tejun Heo936fd732007-08-06 18:36:23 +09003694 * sata_link_resume - resume SATA link
3695 * @link: ATA link to resume SATA
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003696 * @params: timing parameters { interval, duratinon, timeout } in msec
Tejun Heod4b2bab2007-02-02 16:50:52 +09003697 * @deadline: deadline jiffies for the operation
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003698 *
Tejun Heo936fd732007-08-06 18:36:23 +09003699 * Resume SATA phy @link and debounce it.
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003700 *
3701 * LOCKING:
3702 * Kernel thread context (may sleep)
3703 *
3704 * RETURNS:
3705 * 0 on success, -errno on failure.
3706 */
Tejun Heo936fd732007-08-06 18:36:23 +09003707int sata_link_resume(struct ata_link *link, const unsigned long *params,
3708 unsigned long deadline)
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003709{
Tejun Heo5040ab62010-01-11 11:14:44 +09003710 int tries = ATA_LINK_RESUME_TRIES;
Tejun Heoac371982008-04-07 22:47:19 +09003711 u32 scontrol, serror;
Tejun Heo81952c52006-05-15 20:57:47 +09003712 int rc;
Tejun Heo7a7921e2006-02-02 18:20:00 +09003713
Tejun Heo936fd732007-08-06 18:36:23 +09003714 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
Tejun Heo81952c52006-05-15 20:57:47 +09003715 return rc;
3716
Tejun Heo5040ab62010-01-11 11:14:44 +09003717 /*
3718 * Writes to SControl sometimes get ignored under certain
3719 * controllers (ata_piix SIDPR). Make sure DET actually is
3720 * cleared.
Tejun Heod7bb4cc2006-05-31 18:27:46 +09003721 */
Tejun Heo5040ab62010-01-11 11:14:44 +09003722 do {
3723 scontrol = (scontrol & 0x0f0) | 0x300;
3724 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
3725 return rc;
3726 /*
3727 * Some PHYs react badly if SStatus is pounded
3728 * immediately after resuming. Delay 200ms before
3729 * debouncing.
3730 */
3731 msleep(200);
3732
3733 /* is SControl restored correctly? */
3734 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
3735 return rc;
3736 } while ((scontrol & 0xf0f) != 0x300 && --tries);
3737
3738 if ((scontrol & 0xf0f) != 0x300) {
3739 ata_link_printk(link, KERN_ERR,
3740 "failed to resume link (SControl %X)\n",
3741 scontrol);
3742 return 0;
3743 }
3744
3745 if (tries < ATA_LINK_RESUME_TRIES)
3746 ata_link_printk(link, KERN_WARNING,
3747 "link resume succeeded after %d retries\n",
3748 ATA_LINK_RESUME_TRIES - tries);
Tejun Heo7a7921e2006-02-02 18:20:00 +09003749
Tejun Heoac371982008-04-07 22:47:19 +09003750 if ((rc = sata_link_debounce(link, params, deadline)))
3751 return rc;
3752
Tejun Heof0465192008-05-19 01:15:08 +09003753 /* clear SError, some PHYs require this even for SRST to work */
Tejun Heoac371982008-04-07 22:47:19 +09003754 if (!(rc = sata_scr_read(link, SCR_ERROR, &serror)))
3755 rc = sata_scr_write(link, SCR_ERROR, serror);
Tejun Heoac371982008-04-07 22:47:19 +09003756
Tejun Heof0465192008-05-19 01:15:08 +09003757 return rc != -EINVAL ? rc : 0;
Tejun Heo7a7921e2006-02-02 18:20:00 +09003758}
3759
Tejun Heof5914a42006-05-31 18:27:48 +09003760/**
Tejun Heo1152b262010-09-01 17:50:05 +02003761 * sata_link_scr_lpm - manipulate SControl IPM and SPM fields
3762 * @link: ATA link to manipulate SControl for
3763 * @policy: LPM policy to configure
3764 * @spm_wakeup: initiate LPM transition to active state
3765 *
3766 * Manipulate the IPM field of the SControl register of @link
3767 * according to @policy. If @policy is ATA_LPM_MAX_POWER and
3768 * @spm_wakeup is %true, the SPM field is manipulated to wake up
3769 * the link. This function also clears PHYRDY_CHG before
3770 * returning.
3771 *
3772 * LOCKING:
3773 * EH context.
3774 *
3775 * RETURNS:
3776 * 0 on succes, -errno otherwise.
3777 */
3778int sata_link_scr_lpm(struct ata_link *link, enum ata_lpm_policy policy,
3779 bool spm_wakeup)
3780{
3781 struct ata_eh_context *ehc = &link->eh_context;
3782 bool woken_up = false;
3783 u32 scontrol;
3784 int rc;
3785
3786 rc = sata_scr_read(link, SCR_CONTROL, &scontrol);
3787 if (rc)
3788 return rc;
3789
3790 switch (policy) {
3791 case ATA_LPM_MAX_POWER:
3792 /* disable all LPM transitions */
3793 scontrol |= (0x3 << 8);
3794 /* initiate transition to active state */
3795 if (spm_wakeup) {
3796 scontrol |= (0x4 << 12);
3797 woken_up = true;
3798 }
3799 break;
3800 case ATA_LPM_MED_POWER:
3801 /* allow LPM to PARTIAL */
3802 scontrol &= ~(0x1 << 8);
3803 scontrol |= (0x2 << 8);
3804 break;
3805 case ATA_LPM_MIN_POWER:
3806 /* no restrictions on LPM transitions */
3807 scontrol &= ~(0x3 << 8);
3808 break;
3809 default:
3810 WARN_ON(1);
3811 }
3812
3813 rc = sata_scr_write(link, SCR_CONTROL, scontrol);
3814 if (rc)
3815 return rc;
3816
3817 /* give the link time to transit out of LPM state */
3818 if (woken_up)
3819 msleep(10);
3820
3821 /* clear PHYRDY_CHG from SError */
3822 ehc->i.serror &= ~SERR_PHYRDY_CHG;
3823 return sata_scr_write(link, SCR_ERROR, SERR_PHYRDY_CHG);
3824}
3825
3826/**
Tejun Heo0aa11132008-04-07 22:47:18 +09003827 * ata_std_prereset - prepare for reset
Tejun Heocc0680a2007-08-06 18:36:23 +09003828 * @link: ATA link to be reset
Tejun Heod4b2bab2007-02-02 16:50:52 +09003829 * @deadline: deadline jiffies for the operation
Tejun Heof5914a42006-05-31 18:27:48 +09003830 *
Tejun Heocc0680a2007-08-06 18:36:23 +09003831 * @link is about to be reset. Initialize it. Failure from
Tejun Heob8cffc62007-02-02 16:50:52 +09003832 * prereset makes libata abort whole reset sequence and give up
3833 * that port, so prereset should be best-effort. It does its
3834 * best to prepare for reset sequence but if things go wrong, it
3835 * should just whine, not fail.
Tejun Heof5914a42006-05-31 18:27:48 +09003836 *
3837 * LOCKING:
3838 * Kernel thread context (may sleep)
3839 *
3840 * RETURNS:
3841 * 0 on success, -errno otherwise.
3842 */
Tejun Heo0aa11132008-04-07 22:47:18 +09003843int ata_std_prereset(struct ata_link *link, unsigned long deadline)
Tejun Heof5914a42006-05-31 18:27:48 +09003844{
Tejun Heocc0680a2007-08-06 18:36:23 +09003845 struct ata_port *ap = link->ap;
Tejun Heo936fd732007-08-06 18:36:23 +09003846 struct ata_eh_context *ehc = &link->eh_context;
Tejun Heoe9c83912006-07-03 16:07:26 +09003847 const unsigned long *timing = sata_ehc_deb_timing(ehc);
Tejun Heof5914a42006-05-31 18:27:48 +09003848 int rc;
3849
Tejun Heof5914a42006-05-31 18:27:48 +09003850 /* if we're about to do hardreset, nothing more to do */
3851 if (ehc->i.action & ATA_EH_HARDRESET)
3852 return 0;
3853
Tejun Heo936fd732007-08-06 18:36:23 +09003854 /* if SATA, resume link */
Tejun Heoa16abc02007-05-21 18:33:47 +02003855 if (ap->flags & ATA_FLAG_SATA) {
Tejun Heo936fd732007-08-06 18:36:23 +09003856 rc = sata_link_resume(link, timing, deadline);
Tejun Heob8cffc62007-02-02 16:50:52 +09003857 /* whine about phy resume failure but proceed */
3858 if (rc && rc != -EOPNOTSUPP)
Tejun Heocc0680a2007-08-06 18:36:23 +09003859 ata_link_printk(link, KERN_WARNING, "failed to resume "
Tejun Heof5914a42006-05-31 18:27:48 +09003860 "link for reset (errno=%d)\n", rc);
Tejun Heof5914a42006-05-31 18:27:48 +09003861 }
3862
Tejun Heo45db2f62008-04-08 01:46:56 +09003863 /* no point in trying softreset on offline link */
Tejun Heob1c72912008-07-31 17:02:43 +09003864 if (ata_phys_link_offline(link))
Tejun Heo45db2f62008-04-08 01:46:56 +09003865 ehc->i.action &= ~ATA_EH_SOFTRESET;
3866
Tejun Heof5914a42006-05-31 18:27:48 +09003867 return 0;
3868}
3869
Tejun Heoc2bd5802006-01-24 17:05:22 +09003870/**
Tejun Heocc0680a2007-08-06 18:36:23 +09003871 * sata_link_hardreset - reset link via SATA phy reset
3872 * @link: link to reset
Tejun Heob6103f62006-11-01 17:59:53 +09003873 * @timing: timing parameters { interval, duratinon, timeout } in msec
Tejun Heod4b2bab2007-02-02 16:50:52 +09003874 * @deadline: deadline jiffies for the operation
Tejun Heo9dadd452008-04-07 22:47:19 +09003875 * @online: optional out parameter indicating link onlineness
3876 * @check_ready: optional callback to check link readiness
Tejun Heoc2bd5802006-01-24 17:05:22 +09003877 *
Tejun Heocc0680a2007-08-06 18:36:23 +09003878 * SATA phy-reset @link using DET bits of SControl register.
Tejun Heo9dadd452008-04-07 22:47:19 +09003879 * After hardreset, link readiness is waited upon using
3880 * ata_wait_ready() if @check_ready is specified. LLDs are
3881 * allowed to not specify @check_ready and wait itself after this
3882 * function returns. Device classification is LLD's
3883 * responsibility.
3884 *
3885 * *@online is set to one iff reset succeeded and @link is online
3886 * after reset.
Tejun Heoc2bd5802006-01-24 17:05:22 +09003887 *
3888 * LOCKING:
3889 * Kernel thread context (may sleep)
3890 *
3891 * RETURNS:
3892 * 0 on success, -errno otherwise.
3893 */
Tejun Heocc0680a2007-08-06 18:36:23 +09003894int sata_link_hardreset(struct ata_link *link, const unsigned long *timing,
Tejun Heo9dadd452008-04-07 22:47:19 +09003895 unsigned long deadline,
3896 bool *online, int (*check_ready)(struct ata_link *))
Tejun Heoc2bd5802006-01-24 17:05:22 +09003897{
Tejun Heo852ee162006-04-01 01:38:18 +09003898 u32 scontrol;
Tejun Heo81952c52006-05-15 20:57:47 +09003899 int rc;
Tejun Heo852ee162006-04-01 01:38:18 +09003900
Tejun Heoc2bd5802006-01-24 17:05:22 +09003901 DPRINTK("ENTER\n");
3902
Tejun Heo9dadd452008-04-07 22:47:19 +09003903 if (online)
3904 *online = false;
3905
Tejun Heo936fd732007-08-06 18:36:23 +09003906 if (sata_set_spd_needed(link)) {
Tejun Heo1c3fae42006-04-02 20:53:28 +09003907 /* SATA spec says nothing about how to reconfigure
3908 * spd. To be on the safe side, turn off phy during
3909 * reconfiguration. This works for at least ICH7 AHCI
3910 * and Sil3124.
3911 */
Tejun Heo936fd732007-08-06 18:36:23 +09003912 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
Tejun Heob6103f62006-11-01 17:59:53 +09003913 goto out;
Tejun Heo81952c52006-05-15 20:57:47 +09003914
Martin Hicksa34b6fc2006-07-05 15:06:13 -04003915 scontrol = (scontrol & 0x0f0) | 0x304;
Tejun Heo81952c52006-05-15 20:57:47 +09003916
Tejun Heo936fd732007-08-06 18:36:23 +09003917 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
Tejun Heob6103f62006-11-01 17:59:53 +09003918 goto out;
Tejun Heo1c3fae42006-04-02 20:53:28 +09003919
Tejun Heo936fd732007-08-06 18:36:23 +09003920 sata_set_spd(link);
Tejun Heo1c3fae42006-04-02 20:53:28 +09003921 }
3922
3923 /* issue phy wake/reset */
Tejun Heo936fd732007-08-06 18:36:23 +09003924 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
Tejun Heob6103f62006-11-01 17:59:53 +09003925 goto out;
Tejun Heo81952c52006-05-15 20:57:47 +09003926
Tejun Heo852ee162006-04-01 01:38:18 +09003927 scontrol = (scontrol & 0x0f0) | 0x301;
Tejun Heo81952c52006-05-15 20:57:47 +09003928
Tejun Heo936fd732007-08-06 18:36:23 +09003929 if ((rc = sata_scr_write_flush(link, SCR_CONTROL, scontrol)))
Tejun Heob6103f62006-11-01 17:59:53 +09003930 goto out;
Tejun Heoc2bd5802006-01-24 17:05:22 +09003931
Tejun Heo1c3fae42006-04-02 20:53:28 +09003932 /* Couldn't find anything in SATA I/II specs, but AHCI-1.1
Tejun Heoc2bd5802006-01-24 17:05:22 +09003933 * 10.4.2 says at least 1 ms.
3934 */
3935 msleep(1);
3936
Tejun Heo936fd732007-08-06 18:36:23 +09003937 /* bring link back */
3938 rc = sata_link_resume(link, timing, deadline);
Tejun Heo9dadd452008-04-07 22:47:19 +09003939 if (rc)
3940 goto out;
3941 /* if link is offline nothing more to do */
Tejun Heob1c72912008-07-31 17:02:43 +09003942 if (ata_phys_link_offline(link))
Tejun Heo9dadd452008-04-07 22:47:19 +09003943 goto out;
3944
3945 /* Link is online. From this point, -ENODEV too is an error. */
3946 if (online)
3947 *online = true;
3948
Tejun Heo071f44b2008-04-07 22:47:22 +09003949 if (sata_pmp_supported(link->ap) && ata_is_host_link(link)) {
Tejun Heo9dadd452008-04-07 22:47:19 +09003950 /* If PMP is supported, we have to do follow-up SRST.
3951 * Some PMPs don't send D2H Reg FIS after hardreset if
3952 * the first port is empty. Wait only for
3953 * ATA_TMOUT_PMP_SRST_WAIT.
3954 */
3955 if (check_ready) {
3956 unsigned long pmp_deadline;
3957
Tejun Heo341c2c92008-05-20 02:17:51 +09003958 pmp_deadline = ata_deadline(jiffies,
3959 ATA_TMOUT_PMP_SRST_WAIT);
Tejun Heo9dadd452008-04-07 22:47:19 +09003960 if (time_after(pmp_deadline, deadline))
3961 pmp_deadline = deadline;
3962 ata_wait_ready(link, pmp_deadline, check_ready);
3963 }
3964 rc = -EAGAIN;
3965 goto out;
3966 }
3967
3968 rc = 0;
3969 if (check_ready)
3970 rc = ata_wait_ready(link, deadline, check_ready);
Tejun Heob6103f62006-11-01 17:59:53 +09003971 out:
Tejun Heo0cbf0712008-05-19 01:15:05 +09003972 if (rc && rc != -EAGAIN) {
3973 /* online is set iff link is online && reset succeeded */
3974 if (online)
3975 *online = false;
Tejun Heo9dadd452008-04-07 22:47:19 +09003976 ata_link_printk(link, KERN_ERR,
3977 "COMRESET failed (errno=%d)\n", rc);
Tejun Heo0cbf0712008-05-19 01:15:05 +09003978 }
Tejun Heob6103f62006-11-01 17:59:53 +09003979 DPRINTK("EXIT, rc=%d\n", rc);
3980 return rc;
3981}
3982
3983/**
Tejun Heo57c9efd2008-04-07 22:47:19 +09003984 * sata_std_hardreset - COMRESET w/o waiting or classification
3985 * @link: link to reset
3986 * @class: resulting class of attached device
3987 * @deadline: deadline jiffies for the operation
3988 *
3989 * Standard SATA COMRESET w/o waiting or classification.
3990 *
3991 * LOCKING:
3992 * Kernel thread context (may sleep)
3993 *
3994 * RETURNS:
3995 * 0 if link offline, -EAGAIN if link online, -errno on errors.
3996 */
3997int sata_std_hardreset(struct ata_link *link, unsigned int *class,
3998 unsigned long deadline)
3999{
4000 const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
4001 bool online;
4002 int rc;
4003
4004 /* do hardreset */
4005 rc = sata_link_hardreset(link, timing, deadline, &online, NULL);
Tejun Heo57c9efd2008-04-07 22:47:19 +09004006 return online ? -EAGAIN : rc;
4007}
4008
4009/**
Tejun Heo203c75b2008-04-07 22:47:18 +09004010 * ata_std_postreset - standard postreset callback
Tejun Heocc0680a2007-08-06 18:36:23 +09004011 * @link: the target ata_link
Tejun Heoc2bd5802006-01-24 17:05:22 +09004012 * @classes: classes of attached devices
4013 *
4014 * This function is invoked after a successful reset. Note that
4015 * the device might have been reset more than once using
4016 * different reset methods before postreset is invoked.
Tejun Heoc2bd5802006-01-24 17:05:22 +09004017 *
Tejun Heoc2bd5802006-01-24 17:05:22 +09004018 * LOCKING:
4019 * Kernel thread context (may sleep)
4020 */
Tejun Heo203c75b2008-04-07 22:47:18 +09004021void ata_std_postreset(struct ata_link *link, unsigned int *classes)
Tejun Heoc2bd5802006-01-24 17:05:22 +09004022{
Tejun Heof0465192008-05-19 01:15:08 +09004023 u32 serror;
4024
Tejun Heoc2bd5802006-01-24 17:05:22 +09004025 DPRINTK("ENTER\n");
4026
Tejun Heof0465192008-05-19 01:15:08 +09004027 /* reset complete, clear SError */
4028 if (!sata_scr_read(link, SCR_ERROR, &serror))
4029 sata_scr_write(link, SCR_ERROR, serror);
4030
Tejun Heoc2bd5802006-01-24 17:05:22 +09004031 /* print link status */
Tejun Heo936fd732007-08-06 18:36:23 +09004032 sata_print_link_status(link);
Tejun Heoc2bd5802006-01-24 17:05:22 +09004033
Tejun Heoc2bd5802006-01-24 17:05:22 +09004034 DPRINTK("EXIT\n");
4035}
4036
Tejun Heoa62c0fc2006-01-24 17:05:22 +09004037/**
Tejun Heo623a3122006-03-05 17:55:58 +09004038 * ata_dev_same_device - Determine whether new ID matches configured device
Tejun Heo623a3122006-03-05 17:55:58 +09004039 * @dev: device to compare against
4040 * @new_class: class of the new device
4041 * @new_id: IDENTIFY page of the new device
4042 *
4043 * Compare @new_class and @new_id against @dev and determine
4044 * whether @dev is the device indicated by @new_class and
4045 * @new_id.
4046 *
4047 * LOCKING:
4048 * None.
4049 *
4050 * RETURNS:
4051 * 1 if @dev matches @new_class and @new_id, 0 otherwise.
4052 */
Tejun Heo3373efd2006-05-15 20:57:53 +09004053static int ata_dev_same_device(struct ata_device *dev, unsigned int new_class,
4054 const u16 *new_id)
Tejun Heo623a3122006-03-05 17:55:58 +09004055{
4056 const u16 *old_id = dev->id;
Tejun Heoa0cf7332007-01-02 20:18:49 +09004057 unsigned char model[2][ATA_ID_PROD_LEN + 1];
4058 unsigned char serial[2][ATA_ID_SERNO_LEN + 1];
Tejun Heo623a3122006-03-05 17:55:58 +09004059
4060 if (dev->class != new_class) {
Tejun Heof15a1da2006-05-15 20:57:56 +09004061 ata_dev_printk(dev, KERN_INFO, "class mismatch %d != %d\n",
4062 dev->class, new_class);
Tejun Heo623a3122006-03-05 17:55:58 +09004063 return 0;
4064 }
4065
Tejun Heoa0cf7332007-01-02 20:18:49 +09004066 ata_id_c_string(old_id, model[0], ATA_ID_PROD, sizeof(model[0]));
4067 ata_id_c_string(new_id, model[1], ATA_ID_PROD, sizeof(model[1]));
4068 ata_id_c_string(old_id, serial[0], ATA_ID_SERNO, sizeof(serial[0]));
4069 ata_id_c_string(new_id, serial[1], ATA_ID_SERNO, sizeof(serial[1]));
Tejun Heo623a3122006-03-05 17:55:58 +09004070
4071 if (strcmp(model[0], model[1])) {
Tejun Heof15a1da2006-05-15 20:57:56 +09004072 ata_dev_printk(dev, KERN_INFO, "model number mismatch "
4073 "'%s' != '%s'\n", model[0], model[1]);
Tejun Heo623a3122006-03-05 17:55:58 +09004074 return 0;
4075 }
4076
4077 if (strcmp(serial[0], serial[1])) {
Tejun Heof15a1da2006-05-15 20:57:56 +09004078 ata_dev_printk(dev, KERN_INFO, "serial number mismatch "
4079 "'%s' != '%s'\n", serial[0], serial[1]);
Tejun Heo623a3122006-03-05 17:55:58 +09004080 return 0;
4081 }
4082
Tejun Heo623a3122006-03-05 17:55:58 +09004083 return 1;
4084}
4085
4086/**
Tejun Heofe309112007-05-15 03:28:15 +09004087 * ata_dev_reread_id - Re-read IDENTIFY data
Henrik Kretzschmar3fae4502007-06-19 10:10:50 +02004088 * @dev: target ATA device
Tejun Heobff04642006-11-10 18:08:10 +09004089 * @readid_flags: read ID flags
Tejun Heo623a3122006-03-05 17:55:58 +09004090 *
4091 * Re-read IDENTIFY page and make sure @dev is still attached to
4092 * the port.
4093 *
4094 * LOCKING:
4095 * Kernel thread context (may sleep)
4096 *
4097 * RETURNS:
4098 * 0 on success, negative errno otherwise
4099 */
Tejun Heofe309112007-05-15 03:28:15 +09004100int ata_dev_reread_id(struct ata_device *dev, unsigned int readid_flags)
Tejun Heo623a3122006-03-05 17:55:58 +09004101{
Tejun Heo5eb45c02006-04-02 18:51:52 +09004102 unsigned int class = dev->class;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09004103 u16 *id = (void *)dev->link->ap->sector_buf;
Tejun Heo623a3122006-03-05 17:55:58 +09004104 int rc;
4105
Tejun Heofe635c72006-05-15 20:57:35 +09004106 /* read ID data */
Tejun Heobff04642006-11-10 18:08:10 +09004107 rc = ata_dev_read_id(dev, &class, readid_flags, id);
Tejun Heo623a3122006-03-05 17:55:58 +09004108 if (rc)
Tejun Heofe309112007-05-15 03:28:15 +09004109 return rc;
Tejun Heo623a3122006-03-05 17:55:58 +09004110
4111 /* is the device still there? */
Tejun Heofe309112007-05-15 03:28:15 +09004112 if (!ata_dev_same_device(dev, class, id))
4113 return -ENODEV;
Tejun Heo623a3122006-03-05 17:55:58 +09004114
Tejun Heofe635c72006-05-15 20:57:35 +09004115 memcpy(dev->id, id, sizeof(id[0]) * ATA_ID_WORDS);
Tejun Heofe309112007-05-15 03:28:15 +09004116 return 0;
4117}
4118
4119/**
4120 * ata_dev_revalidate - Revalidate ATA device
4121 * @dev: device to revalidate
Tejun Heo422c9da2007-09-23 13:14:12 +09004122 * @new_class: new class code
Tejun Heofe309112007-05-15 03:28:15 +09004123 * @readid_flags: read ID flags
4124 *
4125 * Re-read IDENTIFY page, make sure @dev is still attached to the
4126 * port and reconfigure it according to the new IDENTIFY page.
4127 *
4128 * LOCKING:
4129 * Kernel thread context (may sleep)
4130 *
4131 * RETURNS:
4132 * 0 on success, negative errno otherwise
4133 */
Tejun Heo422c9da2007-09-23 13:14:12 +09004134int ata_dev_revalidate(struct ata_device *dev, unsigned int new_class,
4135 unsigned int readid_flags)
Tejun Heofe309112007-05-15 03:28:15 +09004136{
Tejun Heo6ddcd3b2007-05-15 03:28:15 +09004137 u64 n_sectors = dev->n_sectors;
Tejun Heo5920dad2009-07-15 17:11:41 +09004138 u64 n_native_sectors = dev->n_native_sectors;
Tejun Heofe309112007-05-15 03:28:15 +09004139 int rc;
4140
4141 if (!ata_dev_enabled(dev))
4142 return -ENODEV;
4143
Tejun Heo422c9da2007-09-23 13:14:12 +09004144 /* fail early if !ATA && !ATAPI to avoid issuing [P]IDENTIFY to PMP */
4145 if (ata_class_enabled(new_class) &&
Borislav Petkovf0d06132009-04-26 16:33:34 +02004146 new_class != ATA_DEV_ATA &&
4147 new_class != ATA_DEV_ATAPI &&
4148 new_class != ATA_DEV_SEMB) {
Tejun Heo422c9da2007-09-23 13:14:12 +09004149 ata_dev_printk(dev, KERN_INFO, "class mismatch %u != %u\n",
4150 dev->class, new_class);
4151 rc = -ENODEV;
4152 goto fail;
4153 }
4154
Tejun Heofe309112007-05-15 03:28:15 +09004155 /* re-read ID */
4156 rc = ata_dev_reread_id(dev, readid_flags);
4157 if (rc)
4158 goto fail;
Tejun Heo623a3122006-03-05 17:55:58 +09004159
4160 /* configure device according to the new ID */
Tejun Heoefdaedc2006-11-01 18:38:52 +09004161 rc = ata_dev_configure(dev);
Tejun Heo6ddcd3b2007-05-15 03:28:15 +09004162 if (rc)
4163 goto fail;
4164
4165 /* verify n_sectors hasn't changed */
Tejun Heo445d2112010-04-05 10:33:13 +09004166 if (dev->class != ATA_DEV_ATA || !n_sectors ||
4167 dev->n_sectors == n_sectors)
4168 return 0;
4169
4170 /* n_sectors has changed */
4171 ata_dev_printk(dev, KERN_WARNING, "n_sectors mismatch %llu != %llu\n",
4172 (unsigned long long)n_sectors,
4173 (unsigned long long)dev->n_sectors);
4174
4175 /*
4176 * Something could have caused HPA to be unlocked
4177 * involuntarily. If n_native_sectors hasn't changed and the
4178 * new size matches it, keep the device.
4179 */
4180 if (dev->n_native_sectors == n_native_sectors &&
4181 dev->n_sectors > n_sectors && dev->n_sectors == n_native_sectors) {
4182 ata_dev_printk(dev, KERN_WARNING,
4183 "new n_sectors matches native, probably "
Tejun Heo68939ce2010-05-15 20:09:33 +02004184 "late HPA unlock, n_sectors updated\n");
4185 /* use the larger n_sectors */
Tejun Heo445d2112010-04-05 10:33:13 +09004186 return 0;
Tejun Heo6ddcd3b2007-05-15 03:28:15 +09004187 }
4188
Tejun Heo445d2112010-04-05 10:33:13 +09004189 /*
4190 * Some BIOSes boot w/o HPA but resume w/ HPA locked. Try
4191 * unlocking HPA in those cases.
4192 *
4193 * https://bugzilla.kernel.org/show_bug.cgi?id=15396
4194 */
4195 if (dev->n_native_sectors == n_native_sectors &&
4196 dev->n_sectors < n_sectors && n_sectors == n_native_sectors &&
4197 !(dev->horkage & ATA_HORKAGE_BROKEN_HPA)) {
4198 ata_dev_printk(dev, KERN_WARNING,
4199 "old n_sectors matches native, probably "
4200 "late HPA lock, will try to unlock HPA\n");
4201 /* try unlocking HPA */
4202 dev->flags |= ATA_DFLAG_UNLOCK_HPA;
4203 rc = -EIO;
4204 } else
4205 rc = -ENODEV;
Tejun Heo623a3122006-03-05 17:55:58 +09004206
Tejun Heo445d2112010-04-05 10:33:13 +09004207 /* restore original n_[native_]sectors and fail */
4208 dev->n_native_sectors = n_native_sectors;
4209 dev->n_sectors = n_sectors;
Tejun Heo623a3122006-03-05 17:55:58 +09004210 fail:
Tejun Heof15a1da2006-05-15 20:57:56 +09004211 ata_dev_printk(dev, KERN_ERR, "revalidation failed (errno=%d)\n", rc);
Tejun Heo623a3122006-03-05 17:55:58 +09004212 return rc;
4213}
4214
Alan Cox6919a0a2006-10-27 19:08:46 -07004215struct ata_blacklist_entry {
4216 const char *model_num;
4217 const char *model_rev;
4218 unsigned long horkage;
4219};
4220
4221static const struct ata_blacklist_entry ata_device_blacklist [] = {
4222 /* Devices with DMA related problems under Linux */
4223 { "WDC AC11000H", NULL, ATA_HORKAGE_NODMA },
4224 { "WDC AC22100H", NULL, ATA_HORKAGE_NODMA },
4225 { "WDC AC32500H", NULL, ATA_HORKAGE_NODMA },
4226 { "WDC AC33100H", NULL, ATA_HORKAGE_NODMA },
4227 { "WDC AC31600H", NULL, ATA_HORKAGE_NODMA },
4228 { "WDC AC32100H", "24.09P07", ATA_HORKAGE_NODMA },
4229 { "WDC AC23200L", "21.10N21", ATA_HORKAGE_NODMA },
4230 { "Compaq CRD-8241B", NULL, ATA_HORKAGE_NODMA },
4231 { "CRD-8400B", NULL, ATA_HORKAGE_NODMA },
Mark Lord7da4c932010-07-01 18:18:12 -04004232 { "CRD-848[02]B", NULL, ATA_HORKAGE_NODMA },
Alan Cox6919a0a2006-10-27 19:08:46 -07004233 { "CRD-84", NULL, ATA_HORKAGE_NODMA },
4234 { "SanDisk SDP3B", NULL, ATA_HORKAGE_NODMA },
4235 { "SanDisk SDP3B-64", NULL, ATA_HORKAGE_NODMA },
4236 { "SANYO CD-ROM CRD", NULL, ATA_HORKAGE_NODMA },
4237 { "HITACHI CDR-8", NULL, ATA_HORKAGE_NODMA },
Mark Lord7da4c932010-07-01 18:18:12 -04004238 { "HITACHI CDR-8[34]35",NULL, ATA_HORKAGE_NODMA },
Alan Cox6919a0a2006-10-27 19:08:46 -07004239 { "Toshiba CD-ROM XM-6202B", NULL, ATA_HORKAGE_NODMA },
4240 { "TOSHIBA CD-ROM XM-1702BC", NULL, ATA_HORKAGE_NODMA },
4241 { "CD-532E-A", NULL, ATA_HORKAGE_NODMA },
4242 { "E-IDE CD-ROM CR-840",NULL, ATA_HORKAGE_NODMA },
4243 { "CD-ROM Drive/F5A", NULL, ATA_HORKAGE_NODMA },
4244 { "WPI CDD-820", NULL, ATA_HORKAGE_NODMA },
4245 { "SAMSUNG CD-ROM SC-148C", NULL, ATA_HORKAGE_NODMA },
4246 { "SAMSUNG CD-ROM SC", NULL, ATA_HORKAGE_NODMA },
Alan Cox6919a0a2006-10-27 19:08:46 -07004247 { "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,ATA_HORKAGE_NODMA },
4248 { "_NEC DV5800A", NULL, ATA_HORKAGE_NODMA },
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004249 { "SAMSUNG CD-ROM SN-124", "N001", ATA_HORKAGE_NODMA },
Dave Jones39f19882007-05-21 14:31:03 -04004250 { "Seagate STT20000A", NULL, ATA_HORKAGE_NODMA },
Tejun Heo3af9a772007-09-23 13:19:54 +09004251 /* Odd clown on sil3726/4726 PMPs */
Tejun Heo50af2fa2008-05-19 01:15:14 +09004252 { "Config Disk", NULL, ATA_HORKAGE_DISABLE },
Alan Cox6919a0a2006-10-27 19:08:46 -07004253
Albert Lee18d6e9d2007-04-02 11:34:15 +08004254 /* Weird ATAPI devices */
Tejun Heo40a1d532007-06-27 02:49:38 +09004255 { "TORiSAN DVD-ROM DRD-N216", NULL, ATA_HORKAGE_MAX_SEC_128 },
Tejun Heo6a87e422008-11-03 19:01:09 +09004256 { "QUANTUM DAT DAT72-000", NULL, ATA_HORKAGE_ATAPI_MOD16_DMA },
Albert Lee18d6e9d2007-04-02 11:34:15 +08004257
Alan Cox6919a0a2006-10-27 19:08:46 -07004258 /* Devices we expect to fail diagnostics */
4259
4260 /* Devices where NCQ should be avoided */
4261 /* NCQ is slow */
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004262 { "WDC WD740ADFD-00", NULL, ATA_HORKAGE_NONCQ },
Tejun Heo459ad682007-12-07 12:46:23 +09004263 { "WDC WD740ADFD-00NLR1", NULL, ATA_HORKAGE_NONCQ, },
Tejun Heo09125ea2007-02-28 15:21:23 +09004264 /* http://thread.gmane.org/gmane.linux.ide/14907 */
4265 { "FUJITSU MHT2060BH", NULL, ATA_HORKAGE_NONCQ },
Paul Rolland7acfaf32007-03-26 21:43:44 -08004266 /* NCQ is broken */
Jeff Garzik539cc7c2007-09-20 16:31:47 -04004267 { "Maxtor *", "BANC*", ATA_HORKAGE_NONCQ },
Alan Cox0e3dbc02007-09-20 15:22:47 +01004268 { "Maxtor 7V300F0", "VA111630", ATA_HORKAGE_NONCQ },
Paolo Ornatida6f0ec2007-10-04 11:06:56 +09004269 { "ST380817AS", "3.42", ATA_HORKAGE_NONCQ },
Tejun Heoe41bd3e2007-12-09 19:45:39 +09004270 { "ST3160023AS", "3.42", ATA_HORKAGE_NONCQ },
Lubomir Bulej5ccfca92008-12-22 11:35:22 +01004271 { "OCZ CORE_SSD", "02.10104", ATA_HORKAGE_NONCQ },
Jeff Garzik539cc7c2007-09-20 16:31:47 -04004272
Tejun Heoac70a962008-11-27 13:36:48 +09004273 /* Seagate NCQ + FLUSH CACHE firmware bug */
Mark Lord4d1f9082010-07-05 18:53:37 -04004274 { "ST31500341AS", "SD1[5-9]", ATA_HORKAGE_NONCQ |
Tejun Heoac70a962008-11-27 13:36:48 +09004275 ATA_HORKAGE_FIRMWARE_WARN },
Tejun Heod10d4912008-12-11 13:42:42 +09004276
Mark Lord4d1f9082010-07-05 18:53:37 -04004277 { "ST31000333AS", "SD1[5-9]", ATA_HORKAGE_NONCQ |
Tejun Heod10d4912008-12-11 13:42:42 +09004278 ATA_HORKAGE_FIRMWARE_WARN },
4279
Mark Lord4d1f9082010-07-05 18:53:37 -04004280 { "ST3640[36]23AS", "SD1[5-9]", ATA_HORKAGE_NONCQ |
Tejun Heod10d4912008-12-11 13:42:42 +09004281 ATA_HORKAGE_FIRMWARE_WARN },
4282
Mark Lord4d1f9082010-07-05 18:53:37 -04004283 { "ST3320[68]13AS", "SD1[5-9]", ATA_HORKAGE_NONCQ |
Tejun Heoac70a962008-11-27 13:36:48 +09004284 ATA_HORKAGE_FIRMWARE_WARN },
4285
Robert Hancock36e337d2007-04-02 22:05:29 -06004286 /* Blacklist entries taken from Silicon Image 3124/3132
4287 Windows driver .inf file - also several Linux problem reports */
4288 { "HTS541060G9SA00", "MB3OC60D", ATA_HORKAGE_NONCQ, },
4289 { "HTS541080G9SA00", "MB4OC60D", ATA_HORKAGE_NONCQ, },
4290 { "HTS541010G9SA00", "MBZOC60D", ATA_HORKAGE_NONCQ, },
Alan Cox6919a0a2006-10-27 19:08:46 -07004291
Tejun Heo68b0ddb2010-04-05 10:51:26 +09004292 /* https://bugzilla.kernel.org/show_bug.cgi?id=15573 */
4293 { "C300-CTFDDAC128MAG", "0001", ATA_HORKAGE_NONCQ, },
4294
Tejun Heo16c55b02007-08-29 11:58:33 +09004295 /* devices which puke on READ_NATIVE_MAX */
4296 { "HDS724040KLSA80", "KFAOA20N", ATA_HORKAGE_BROKEN_HPA, },
4297 { "WDC WD3200JD-00KLB0", "WD-WCAMR1130137", ATA_HORKAGE_BROKEN_HPA },
4298 { "WDC WD2500JD-00HBB0", "WD-WMAL71490727", ATA_HORKAGE_BROKEN_HPA },
4299 { "MAXTOR 6L080L4", "A93.0500", ATA_HORKAGE_BROKEN_HPA },
Alan Cox6919a0a2006-10-27 19:08:46 -07004300
Tejun Heo78313872009-08-07 01:59:15 +09004301 /* this one allows HPA unlocking but fails IOs on the area */
4302 { "OCZ-VERTEX", "1.30", ATA_HORKAGE_BROKEN_HPA },
4303
Alan Cox93328e12007-09-29 04:06:48 -04004304 /* Devices which report 1 sector over size HPA */
4305 { "ST340823A", NULL, ATA_HORKAGE_HPA_SIZE, },
4306 { "ST320413A", NULL, ATA_HORKAGE_HPA_SIZE, },
Mikko Rapelib152fcd2008-02-19 01:41:25 +01004307 { "ST310211A", NULL, ATA_HORKAGE_HPA_SIZE, },
Alan Cox93328e12007-09-29 04:06:48 -04004308
Alan Cox6bbfd532007-11-05 22:58:58 +00004309 /* Devices which get the IVB wrong */
4310 { "QUANTUM FIREBALLlct10 05", "A03.0900", ATA_HORKAGE_IVB, },
Alan Coxa79067e2008-04-29 14:08:36 +01004311 /* Maybe we should just blacklist TSSTcorp... */
Mark Lord7da4c932010-07-01 18:18:12 -04004312 { "TSSTcorp CDDVDW SH-S202[HJN]", "SB0[01]", ATA_HORKAGE_IVB, },
Alan Cox6bbfd532007-11-05 22:58:58 +00004313
Jens Axboe9ce8e302008-08-27 15:23:18 +02004314 /* Devices that do not need bridging limits applied */
4315 { "MTRON MSP-SATA*", NULL, ATA_HORKAGE_BRIDGE_OK, },
4316
Tejun Heo90627122009-01-29 20:31:36 +09004317 /* Devices which aren't very happy with higher link speeds */
4318 { "WD My Book", NULL, ATA_HORKAGE_1_5_GBPS, },
4319
Tejun Heod0cb43b2009-07-09 09:27:50 +09004320 /*
4321 * Devices which choke on SETXFER. Applies only if both the
4322 * device and controller are SATA.
4323 */
4324 { "PIONEER DVD-RW DVRTD08", "1.00", ATA_HORKAGE_NOSETXFER },
4325
Alan Cox6919a0a2006-10-27 19:08:46 -07004326 /* End Marker */
4327 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004328};
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05004329
Mark Lordbce036c2010-07-01 18:16:14 -04004330/**
4331 * glob_match - match a text string against a glob-style pattern
4332 * @text: the string to be examined
4333 * @pattern: the glob-style pattern to be matched against
4334 *
4335 * Either/both of text and pattern can be empty strings.
4336 *
4337 * Match text against a glob-style pattern, with wildcards and simple sets:
4338 *
4339 * ? matches any single character.
4340 * * matches any run of characters.
4341 * [xyz] matches a single character from the set: x, y, or z.
Mark Lord2f9e4d12010-07-05 10:25:45 -04004342 * [a-d] matches a single character from the range: a, b, c, or d.
4343 * [a-d0-9] matches a single character from either range.
Mark Lordbce036c2010-07-01 18:16:14 -04004344 *
Mark Lord2f9e4d12010-07-05 10:25:45 -04004345 * The special characters ?, [, -, or *, can be matched using a set, eg. [*]
4346 * Behaviour with malformed patterns is undefined, though generally reasonable.
Mark Lordbce036c2010-07-01 18:16:14 -04004347 *
Randy Dunlap3d2be542010-08-09 16:28:30 -07004348 * Sample patterns: "SD1?", "SD1[0-5]", "*R0", "SD*1?[012]*xx"
Mark Lordbce036c2010-07-01 18:16:14 -04004349 *
4350 * This function uses one level of recursion per '*' in pattern.
4351 * Since it calls _nothing_ else, and has _no_ explicit local variables,
4352 * this will not cause stack problems for any reasonable use here.
4353 *
4354 * RETURNS:
4355 * 0 on match, 1 otherwise.
4356 */
4357static int glob_match (const char *text, const char *pattern)
Jeff Garzik539cc7c2007-09-20 16:31:47 -04004358{
Mark Lordbce036c2010-07-01 18:16:14 -04004359 do {
4360 /* Match single character or a '?' wildcard */
4361 if (*text == *pattern || *pattern == '?') {
4362 if (!*pattern++)
4363 return 0; /* End of both strings: match */
4364 } else {
4365 /* Match single char against a '[' bracketed ']' pattern set */
4366 if (!*text || *pattern != '[')
4367 break; /* Not a pattern set */
Mark Lord2f9e4d12010-07-05 10:25:45 -04004368 while (*++pattern && *pattern != ']' && *text != *pattern) {
4369 if (*pattern == '-' && *(pattern - 1) != '[')
4370 if (*text > *(pattern - 1) && *text < *(pattern + 1)) {
4371 ++pattern;
4372 break;
4373 }
4374 }
Mark Lordbce036c2010-07-01 18:16:14 -04004375 if (!*pattern || *pattern == ']')
4376 return 1; /* No match */
4377 while (*pattern && *pattern++ != ']');
4378 }
4379 } while (*++text && *pattern);
Jeff Garzik539cc7c2007-09-20 16:31:47 -04004380
Mark Lordbce036c2010-07-01 18:16:14 -04004381 /* Match any run of chars against a '*' wildcard */
4382 if (*pattern == '*') {
4383 if (!*++pattern)
4384 return 0; /* Match: avoid recursion at end of pattern */
4385 /* Loop to handle additional pattern chars after the wildcard */
4386 while (*text) {
4387 if (glob_match(text, pattern) == 0)
4388 return 0; /* Remainder matched */
4389 ++text; /* Absorb (match) this char and try again */
Andrew Paprocki317b50b2007-10-15 15:43:12 -04004390 }
4391 }
Mark Lordbce036c2010-07-01 18:16:14 -04004392 if (!*text && !*pattern)
4393 return 0; /* End of both strings: match */
4394 return 1; /* No match */
Jeff Garzik539cc7c2007-09-20 16:31:47 -04004395}
Mark Lordbce036c2010-07-01 18:16:14 -04004396
Tejun Heo75683fe2007-07-05 13:31:27 +09004397static unsigned long ata_dev_blacklisted(const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398{
Tejun Heo8bfa79f2007-01-02 20:19:40 +09004399 unsigned char model_num[ATA_ID_PROD_LEN + 1];
4400 unsigned char model_rev[ATA_ID_FW_REV_LEN + 1];
Alan Cox6919a0a2006-10-27 19:08:46 -07004401 const struct ata_blacklist_entry *ad = ata_device_blacklist;
Albert Lee3a778272006-06-22 13:00:25 +08004402
Tejun Heo8bfa79f2007-01-02 20:19:40 +09004403 ata_id_c_string(dev->id, model_num, ATA_ID_PROD, sizeof(model_num));
4404 ata_id_c_string(dev->id, model_rev, ATA_ID_FW_REV, sizeof(model_rev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004405
Alan Cox6919a0a2006-10-27 19:08:46 -07004406 while (ad->model_num) {
Mark Lordbce036c2010-07-01 18:16:14 -04004407 if (!glob_match(model_num, ad->model_num)) {
Alan Cox6919a0a2006-10-27 19:08:46 -07004408 if (ad->model_rev == NULL)
4409 return ad->horkage;
Mark Lordbce036c2010-07-01 18:16:14 -04004410 if (!glob_match(model_rev, ad->model_rev))
Alan Cox6919a0a2006-10-27 19:08:46 -07004411 return ad->horkage;
Alan Coxf4b15fe2006-03-22 15:54:04 +00004412 }
Alan Cox6919a0a2006-10-27 19:08:46 -07004413 ad++;
Alan Coxf4b15fe2006-03-22 15:54:04 +00004414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004415 return 0;
4416}
4417
Alan Cox6919a0a2006-10-27 19:08:46 -07004418static int ata_dma_blacklisted(const struct ata_device *dev)
4419{
4420 /* We don't support polling DMA.
4421 * DMA blacklist those ATAPI devices with CDB-intr (and use PIO)
4422 * if the LLDD handles only interrupts in the HSM_ST_LAST state.
4423 */
Tejun Heo9af5c9c2007-08-06 18:36:22 +09004424 if ((dev->link->ap->flags & ATA_FLAG_PIO_POLLING) &&
Alan Cox6919a0a2006-10-27 19:08:46 -07004425 (dev->flags & ATA_DFLAG_CDB_INTR))
4426 return 1;
Tejun Heo75683fe2007-07-05 13:31:27 +09004427 return (dev->horkage & ATA_HORKAGE_NODMA) ? 1 : 0;
Alan Cox6919a0a2006-10-27 19:08:46 -07004428}
4429
Tejun Heoa6d5a512006-03-06 04:31:57 +09004430/**
Alan Cox6bbfd532007-11-05 22:58:58 +00004431 * ata_is_40wire - check drive side detection
4432 * @dev: device
4433 *
4434 * Perform drive side detection decoding, allowing for device vendors
4435 * who can't follow the documentation.
4436 */
4437
4438static int ata_is_40wire(struct ata_device *dev)
4439{
4440 if (dev->horkage & ATA_HORKAGE_IVB)
4441 return ata_drive_40wire_relaxed(dev->id);
4442 return ata_drive_40wire(dev->id);
4443}
4444
4445/**
Alan Cox15a55512008-03-28 14:33:46 -07004446 * cable_is_40wire - 40/80/SATA decider
4447 * @ap: port to consider
4448 *
4449 * This function encapsulates the policy for speed management
4450 * in one place. At the moment we don't cache the result but
4451 * there is a good case for setting ap->cbl to the result when
4452 * we are called with unknown cables (and figuring out if it
4453 * impacts hotplug at all).
4454 *
4455 * Return 1 if the cable appears to be 40 wire.
4456 */
4457
4458static int cable_is_40wire(struct ata_port *ap)
4459{
4460 struct ata_link *link;
4461 struct ata_device *dev;
4462
Tejun Heo4a9c7b32008-10-27 19:59:23 +09004463 /* If the controller thinks we are 40 wire, we are. */
Alan Cox15a55512008-03-28 14:33:46 -07004464 if (ap->cbl == ATA_CBL_PATA40)
4465 return 1;
Tejun Heo4a9c7b32008-10-27 19:59:23 +09004466
4467 /* If the controller thinks we are 80 wire, we are. */
Alan Cox15a55512008-03-28 14:33:46 -07004468 if (ap->cbl == ATA_CBL_PATA80 || ap->cbl == ATA_CBL_SATA)
4469 return 0;
Tejun Heo4a9c7b32008-10-27 19:59:23 +09004470
4471 /* If the system is known to be 40 wire short cable (eg
4472 * laptop), then we allow 80 wire modes even if the drive
4473 * isn't sure.
4474 */
Alan Coxf7920682008-04-08 16:37:50 +01004475 if (ap->cbl == ATA_CBL_PATA40_SHORT)
4476 return 0;
Alan Cox15a55512008-03-28 14:33:46 -07004477
Tejun Heo4a9c7b32008-10-27 19:59:23 +09004478 /* If the controller doesn't know, we scan.
4479 *
4480 * Note: We look for all 40 wire detects at this point. Any
4481 * 80 wire detect is taken to be 80 wire cable because
4482 * - in many setups only the one drive (slave if present) will
4483 * give a valid detect
4484 * - if you have a non detect capable drive you don't want it
4485 * to colour the choice
4486 */
Tejun Heo1eca4362008-11-03 20:03:17 +09004487 ata_for_each_link(link, ap, EDGE) {
4488 ata_for_each_dev(dev, link, ENABLED) {
4489 if (!ata_is_40wire(dev))
Alan Cox15a55512008-03-28 14:33:46 -07004490 return 0;
4491 }
4492 }
4493 return 1;
4494}
4495
4496/**
Tejun Heoa6d5a512006-03-06 04:31:57 +09004497 * ata_dev_xfermask - Compute supported xfermask of the given device
Tejun Heoa6d5a512006-03-06 04:31:57 +09004498 * @dev: Device to compute xfermask for
4499 *
Tejun Heoacf356b2006-03-24 14:07:50 +09004500 * Compute supported xfermask of @dev and store it in
4501 * dev->*_mask. This function is responsible for applying all
4502 * known limits including host controller limits, device
4503 * blacklist, etc...
Tejun Heoa6d5a512006-03-06 04:31:57 +09004504 *
4505 * LOCKING:
4506 * None.
Tejun Heoa6d5a512006-03-06 04:31:57 +09004507 */
Tejun Heo3373efd2006-05-15 20:57:53 +09004508static void ata_dev_xfermask(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004509{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09004510 struct ata_link *link = dev->link;
4511 struct ata_port *ap = link->ap;
Jeff Garzikcca39742006-08-24 03:19:22 -04004512 struct ata_host *host = ap->host;
Tejun Heoa6d5a512006-03-06 04:31:57 +09004513 unsigned long xfer_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514
Tejun Heo37deecb2006-08-10 16:59:07 +09004515 /* controller modes available */
Tejun Heo565083e2006-04-02 17:54:47 +09004516 xfer_mask = ata_pack_xfermask(ap->pio_mask,
4517 ap->mwdma_mask, ap->udma_mask);
4518
Robert Hancock8343f882007-03-06 02:37:51 -08004519 /* drive modes available */
Tejun Heo37deecb2006-08-10 16:59:07 +09004520 xfer_mask &= ata_pack_xfermask(dev->pio_mask,
4521 dev->mwdma_mask, dev->udma_mask);
4522 xfer_mask &= ata_id_xfermask(dev->id);
Tejun Heo565083e2006-04-02 17:54:47 +09004523
Alan Coxb352e572006-08-10 18:52:12 +01004524 /*
4525 * CFA Advanced TrueIDE timings are not allowed on a shared
4526 * cable
4527 */
4528 if (ata_dev_pair(dev)) {
4529 /* No PIO5 or PIO6 */
4530 xfer_mask &= ~(0x03 << (ATA_SHIFT_PIO + 5));
4531 /* No MWDMA3 or MWDMA 4 */
4532 xfer_mask &= ~(0x03 << (ATA_SHIFT_MWDMA + 3));
4533 }
4534
Tejun Heo37deecb2006-08-10 16:59:07 +09004535 if (ata_dma_blacklisted(dev)) {
4536 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
Tejun Heof15a1da2006-05-15 20:57:56 +09004537 ata_dev_printk(dev, KERN_WARNING,
4538 "device is on DMA blacklist, disabling DMA\n");
Tejun Heo37deecb2006-08-10 16:59:07 +09004539 }
Tejun Heoa6d5a512006-03-06 04:31:57 +09004540
Petr Vandrovec14d66ab2007-03-08 10:12:12 +01004541 if ((host->flags & ATA_HOST_SIMPLEX) &&
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004542 host->simplex_claimed && host->simplex_claimed != ap) {
Tejun Heo37deecb2006-08-10 16:59:07 +09004543 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
4544 ata_dev_printk(dev, KERN_WARNING, "simplex DMA is claimed by "
4545 "other device, disabling DMA\n");
Alan Cox5444a6f2006-03-27 18:58:20 +01004546 }
Tejun Heo565083e2006-04-02 17:54:47 +09004547
Jeff Garzike4246752007-03-09 09:56:46 -05004548 if (ap->flags & ATA_FLAG_NO_IORDY)
4549 xfer_mask &= ata_pio_mask_no_iordy(dev);
4550
Alan Cox5444a6f2006-03-27 18:58:20 +01004551 if (ap->ops->mode_filter)
Alan Coxa76b62c2007-03-09 09:34:07 -05004552 xfer_mask = ap->ops->mode_filter(dev, xfer_mask);
Alan Cox5444a6f2006-03-27 18:58:20 +01004553
Robert Hancock8343f882007-03-06 02:37:51 -08004554 /* Apply cable rule here. Don't apply it early because when
4555 * we handle hot plug the cable type can itself change.
4556 * Check this last so that we know if the transfer rate was
4557 * solely limited by the cable.
4558 * Unknown or 80 wire cables reported host side are checked
4559 * drive side as well. Cases where we know a 40wire cable
4560 * is used safely for 80 are not checked here.
4561 */
4562 if (xfer_mask & (0xF8 << ATA_SHIFT_UDMA))
4563 /* UDMA/44 or higher would be available */
Alan Cox15a55512008-03-28 14:33:46 -07004564 if (cable_is_40wire(ap)) {
Jeff Garzik2dcb4072007-10-19 06:42:56 -04004565 ata_dev_printk(dev, KERN_WARNING,
Robert Hancock8343f882007-03-06 02:37:51 -08004566 "limited to UDMA/33 due to 40-wire cable\n");
4567 xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
4568 }
4569
Tejun Heo565083e2006-04-02 17:54:47 +09004570 ata_unpack_xfermask(xfer_mask, &dev->pio_mask,
4571 &dev->mwdma_mask, &dev->udma_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572}
4573
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
Linus Torvalds1da177e2005-04-16 15:20:36 -07004576 * @dev: Device to which command will be sent
4577 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04004578 * Issue SET FEATURES - XFER MODE command to device @dev
4579 * on port @ap.
4580 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004582 * PCI/etc. bus probe sem.
Tejun Heo83206a22006-03-24 15:25:31 +09004583 *
4584 * RETURNS:
4585 * 0 on success, AC_ERR_* mask otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004586 */
4587
Tejun Heo3373efd2006-05-15 20:57:53 +09004588static unsigned int ata_dev_set_xfermode(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589{
Tejun Heoa0123702005-12-13 14:49:31 +09004590 struct ata_taskfile tf;
Tejun Heo83206a22006-03-24 15:25:31 +09004591 unsigned int err_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592
4593 /* set up set-features taskfile */
4594 DPRINTK("set features - xfer mode\n");
4595
Tejun Heo464cf172007-05-27 15:10:40 +02004596 /* Some controllers and ATAPI devices show flaky interrupt
4597 * behavior after setting xfer mode. Use polling instead.
4598 */
Tejun Heo3373efd2006-05-15 20:57:53 +09004599 ata_tf_init(dev, &tf);
Tejun Heoa0123702005-12-13 14:49:31 +09004600 tf.command = ATA_CMD_SET_FEATURES;
4601 tf.feature = SETFEATURES_XFER;
Tejun Heo464cf172007-05-27 15:10:40 +02004602 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_POLLING;
Tejun Heoa0123702005-12-13 14:49:31 +09004603 tf.protocol = ATA_PROT_NODATA;
Alan Coxb9f8ab22007-11-19 14:33:11 +00004604 /* If we are using IORDY we must send the mode setting command */
Jeff Garzik11b7bec2007-11-23 21:12:14 -05004605 if (ata_pio_need_iordy(dev))
4606 tf.nsect = dev->xfer_mode;
Alan Coxb9f8ab22007-11-19 14:33:11 +00004607 /* If the device has IORDY and the controller does not - turn it off */
4608 else if (ata_id_has_iordy(dev->id))
Jeff Garzik11b7bec2007-11-23 21:12:14 -05004609 tf.nsect = 0x01;
Alan Coxb9f8ab22007-11-19 14:33:11 +00004610 else /* In the ancient relic department - skip all of this */
4611 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004612
Tejun Heo2b789102007-10-09 15:05:44 +09004613 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614
Tejun Heo83206a22006-03-24 15:25:31 +09004615 DPRINTK("EXIT, err_mask=%x\n", err_mask);
4616 return err_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617}
Tejun Heo1152b262010-09-01 17:50:05 +02004618
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619/**
Jeff Garzik218f3d32007-10-25 00:33:27 -04004620 * ata_dev_set_feature - Issue SET FEATURES - SATA FEATURES
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04004621 * @dev: Device to which command will be sent
4622 * @enable: Whether to enable or disable the feature
Jeff Garzik218f3d32007-10-25 00:33:27 -04004623 * @feature: The sector count represents the feature to set
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04004624 *
4625 * Issue SET FEATURES - SATA FEATURES command to device @dev
Jeff Garzik218f3d32007-10-25 00:33:27 -04004626 * on port @ap with sector count
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04004627 *
4628 * LOCKING:
4629 * PCI/etc. bus probe sem.
4630 *
4631 * RETURNS:
4632 * 0 on success, AC_ERR_* mask otherwise.
4633 */
Tejun Heo1152b262010-09-01 17:50:05 +02004634unsigned int ata_dev_set_feature(struct ata_device *dev, u8 enable, u8 feature)
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04004635{
4636 struct ata_taskfile tf;
4637 unsigned int err_mask;
4638
4639 /* set up set-features taskfile */
4640 DPRINTK("set features - SATA features\n");
4641
4642 ata_tf_init(dev, &tf);
4643 tf.command = ATA_CMD_SET_FEATURES;
4644 tf.feature = enable;
4645 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
4646 tf.protocol = ATA_PROT_NODATA;
Jeff Garzik218f3d32007-10-25 00:33:27 -04004647 tf.nsect = feature;
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04004648
Tejun Heo2b789102007-10-09 15:05:44 +09004649 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
Kristen Carlson Accardi9f45cbd2007-08-15 03:57:11 -04004650
4651 DPRINTK("EXIT, err_mask=%x\n", err_mask);
4652 return err_mask;
4653}
4654
4655/**
Albert Lee8bf62ece2005-05-12 15:29:42 -04004656 * ata_dev_init_params - Issue INIT DEV PARAMS command
Albert Lee8bf62ece2005-05-12 15:29:42 -04004657 * @dev: Device to which command will be sent
Randy Dunlape2a7f772006-05-18 10:50:18 -07004658 * @heads: Number of heads (taskfile parameter)
4659 * @sectors: Number of sectors (taskfile parameter)
Albert Lee8bf62ece2005-05-12 15:29:42 -04004660 *
4661 * LOCKING:
Tejun Heo6aff8f12006-02-15 18:24:09 +09004662 * Kernel thread context (may sleep)
4663 *
4664 * RETURNS:
4665 * 0 on success, AC_ERR_* mask otherwise.
Albert Lee8bf62ece2005-05-12 15:29:42 -04004666 */
Tejun Heo3373efd2006-05-15 20:57:53 +09004667static unsigned int ata_dev_init_params(struct ata_device *dev,
4668 u16 heads, u16 sectors)
Albert Lee8bf62ece2005-05-12 15:29:42 -04004669{
Tejun Heoa0123702005-12-13 14:49:31 +09004670 struct ata_taskfile tf;
Tejun Heo6aff8f12006-02-15 18:24:09 +09004671 unsigned int err_mask;
Albert Lee8bf62ece2005-05-12 15:29:42 -04004672
4673 /* Number of sectors per track 1-255. Number of heads 1-16 */
4674 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
Albert Lee00b6f5e2006-03-27 16:39:18 +08004675 return AC_ERR_INVALID;
Albert Lee8bf62ece2005-05-12 15:29:42 -04004676
4677 /* set up init dev params taskfile */
4678 DPRINTK("init dev params \n");
4679
Tejun Heo3373efd2006-05-15 20:57:53 +09004680 ata_tf_init(dev, &tf);
Tejun Heoa0123702005-12-13 14:49:31 +09004681 tf.command = ATA_CMD_INIT_DEV_PARAMS;
4682 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
4683 tf.protocol = ATA_PROT_NODATA;
4684 tf.nsect = sectors;
4685 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
Albert Lee8bf62ece2005-05-12 15:29:42 -04004686
Tejun Heo2b789102007-10-09 15:05:44 +09004687 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
Alan Cox18b24662007-08-08 14:28:49 +01004688 /* A clean abort indicates an original or just out of spec drive
4689 and we should continue as we issue the setup based on the
4690 drive reported working geometry */
4691 if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
4692 err_mask = 0;
Albert Lee8bf62ece2005-05-12 15:29:42 -04004693
Tejun Heo6aff8f12006-02-15 18:24:09 +09004694 DPRINTK("EXIT, err_mask=%x\n", err_mask);
4695 return err_mask;
Albert Lee8bf62ece2005-05-12 15:29:42 -04004696}
4697
4698/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004699 * ata_sg_clean - Unmap DMA memory associated with command
4700 * @qc: Command containing DMA memory to be released
4701 *
4702 * Unmap all mapped DMA memory associated with this command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703 *
4704 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004705 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706 */
Tejun Heo70e6ad02006-11-14 22:47:10 +09004707void ata_sg_clean(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708{
4709 struct ata_port *ap = qc->ap;
Tejun Heoff2aeb12007-12-05 16:43:11 +09004710 struct scatterlist *sg = qc->sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711 int dir = qc->dma_dir;
4712
Tejun Heoefcb3cf2009-01-09 19:19:14 +09004713 WARN_ON_ONCE(sg == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714
James Bottomleydde20202008-02-19 11:36:56 +01004715 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716
James Bottomleydde20202008-02-19 11:36:56 +01004717 if (qc->n_elem)
FUJITA Tomonori58256272009-02-27 17:35:43 +09004718 dma_unmap_sg(ap->dev, sg, qc->orig_n_elem, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719
4720 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Tejun Heoff2aeb12007-12-05 16:43:11 +09004721 qc->sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722}
4723
4724/**
Tejun Heo5895ef92008-06-17 12:36:26 +09004725 * atapi_check_dma - Check whether ATAPI DMA can be supported
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726 * @qc: Metadata associated with taskfile to check
4727 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04004728 * Allow low-level driver to filter ATA PACKET commands, returning
4729 * a status indicating whether or not it is OK to use DMA for the
4730 * supplied PACKET command.
4731 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004732 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004733 * spin_lock_irqsave(host lock)
Jeff Garzik0cba6322005-05-30 19:49:12 -04004734 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735 * RETURNS: 0 when ATAPI DMA can be used
4736 * nonzero otherwise
4737 */
Tejun Heo5895ef92008-06-17 12:36:26 +09004738int atapi_check_dma(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004739{
4740 struct ata_port *ap = qc->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741
Tejun Heob9a41972007-06-27 02:48:43 +09004742 /* Don't allow DMA if it isn't multiple of 16 bytes. Quite a
4743 * few ATAPI devices choke on such DMA requests.
4744 */
Tejun Heo6a87e422008-11-03 19:01:09 +09004745 if (!(qc->dev->horkage & ATA_HORKAGE_ATAPI_MOD16_DMA) &&
4746 unlikely(qc->nbytes & 15))
Tejun Heob9a41972007-06-27 02:48:43 +09004747 return 1;
Albert Lee6f23a312007-04-02 11:39:25 +08004748
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749 if (ap->ops->check_atapi_dma)
Tejun Heob9a41972007-06-27 02:48:43 +09004750 return ap->ops->check_atapi_dma(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751
Tejun Heob9a41972007-06-27 02:48:43 +09004752 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753}
Tejun Heob9a41972007-06-27 02:48:43 +09004754
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755/**
Tejun Heo31cc23b2007-09-23 13:14:12 +09004756 * ata_std_qc_defer - Check whether a qc needs to be deferred
4757 * @qc: ATA command in question
4758 *
4759 * Non-NCQ commands cannot run with any other command, NCQ or
4760 * not. As upper layer only knows the queue depth, we are
4761 * responsible for maintaining exclusion. This function checks
4762 * whether a new command @qc can be issued.
4763 *
4764 * LOCKING:
4765 * spin_lock_irqsave(host lock)
4766 *
4767 * RETURNS:
4768 * ATA_DEFER_* if deferring is needed, 0 otherwise.
4769 */
4770int ata_std_qc_defer(struct ata_queued_cmd *qc)
4771{
4772 struct ata_link *link = qc->dev->link;
4773
4774 if (qc->tf.protocol == ATA_PROT_NCQ) {
4775 if (!ata_tag_valid(link->active_tag))
4776 return 0;
4777 } else {
4778 if (!ata_tag_valid(link->active_tag) && !link->sactive)
4779 return 0;
4780 }
4781
4782 return ATA_DEFER_LINK;
4783}
4784
Brian Kinge46834c2006-03-17 17:04:03 -06004785void ata_noop_qc_prep(struct ata_queued_cmd *qc) { }
4786
Jeff Garzik0cba6322005-05-30 19:49:12 -04004787/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004788 * ata_sg_init - Associate command with scatter-gather table.
4789 * @qc: Command to be associated
4790 * @sg: Scatter-gather table.
4791 * @n_elem: Number of elements in s/g table.
4792 *
4793 * Initialize the data-related elements of queued_cmd @qc
4794 * to point to a scatter-gather table @sg, containing @n_elem
4795 * elements.
4796 *
4797 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004798 * spin_lock_irqsave(host lock)
Jeff Garzik0cba6322005-05-30 19:49:12 -04004799 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004800void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
4801 unsigned int n_elem)
4802{
Tejun Heoff2aeb12007-12-05 16:43:11 +09004803 qc->sg = sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004804 qc->n_elem = n_elem;
Tejun Heoff2aeb12007-12-05 16:43:11 +09004805 qc->cursg = qc->sg;
4806}
4807
Linus Torvalds1da177e2005-04-16 15:20:36 -07004808/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04004809 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
4810 * @qc: Command with scatter-gather table to be mapped.
4811 *
4812 * DMA-map the scatter-gather table associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004813 *
4814 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004815 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004816 *
4817 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004818 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819 *
4820 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821static int ata_sg_setup(struct ata_queued_cmd *qc)
4822{
4823 struct ata_port *ap = qc->ap;
James Bottomleydde20202008-02-19 11:36:56 +01004824 unsigned int n_elem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825
Tejun Heo44877b42007-02-21 01:06:51 +09004826 VPRINTK("ENTER, ata%u\n", ap->print_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827
James Bottomleydde20202008-02-19 11:36:56 +01004828 n_elem = dma_map_sg(ap->dev, qc->sg, qc->n_elem, qc->dma_dir);
4829 if (n_elem < 1)
4830 return -1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004831
James Bottomleydde20202008-02-19 11:36:56 +01004832 DPRINTK("%d sg elements mapped\n", n_elem);
FUJITA Tomonori58256272009-02-27 17:35:43 +09004833 qc->orig_n_elem = qc->n_elem;
James Bottomleydde20202008-02-19 11:36:56 +01004834 qc->n_elem = n_elem;
Tejun Heof92a2632007-12-05 16:43:10 +09004835 qc->flags |= ATA_QCFLAG_DMAMAP;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836
4837 return 0;
4838}
4839
4840/**
Randy Dunlapc893a3a2006-01-28 13:15:32 -05004841 * swap_buf_le16 - swap halves of 16-bit words in place
Edward Falk0baab862005-06-02 18:17:13 -04004842 * @buf: Buffer to swap
4843 * @buf_words: Number of 16-bit words in buffer.
4844 *
4845 * Swap halves of 16-bit words if needed to convert from
4846 * little-endian byte order to native cpu byte order, or
4847 * vice-versa.
4848 *
4849 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04004850 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04004851 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852void swap_buf_le16(u16 *buf, unsigned int buf_words)
4853{
4854#ifdef __BIG_ENDIAN
4855 unsigned int i;
4856
4857 for (i = 0; i < buf_words; i++)
4858 buf[i] = le16_to_cpu(buf[i]);
4859#endif /* __BIG_ENDIAN */
4860}
4861
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004862/**
Tejun Heo8a8bc222008-11-10 14:48:21 +09004863 * ata_qc_new - Request an available ATA command, for queueing
Randy Dunlap5eb66fe2009-01-20 16:28:59 -08004864 * @ap: target port
Tejun Heo8a8bc222008-11-10 14:48:21 +09004865 *
4866 * LOCKING:
4867 * None.
4868 */
4869
4870static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
4871{
4872 struct ata_queued_cmd *qc = NULL;
4873 unsigned int i;
4874
4875 /* no command while frozen */
4876 if (unlikely(ap->pflags & ATA_PFLAG_FROZEN))
4877 return NULL;
4878
4879 /* the last tag is reserved for internal command. */
4880 for (i = 0; i < ATA_MAX_QUEUE - 1; i++)
4881 if (!test_and_set_bit(i, &ap->qc_allocated)) {
4882 qc = __ata_qc_from_tag(ap, i);
4883 break;
4884 }
4885
4886 if (qc)
4887 qc->tag = i;
4888
4889 return qc;
4890}
4891
4892/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893 * ata_qc_new_init - Request an available ATA command, and initialize it
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894 * @dev: Device from whom we request an available command structure
4895 *
4896 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004897 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898 */
4899
Tejun Heo8a8bc222008-11-10 14:48:21 +09004900struct ata_queued_cmd *ata_qc_new_init(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004901{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09004902 struct ata_port *ap = dev->link->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903 struct ata_queued_cmd *qc;
4904
Tejun Heo8a8bc222008-11-10 14:48:21 +09004905 qc = ata_qc_new(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004906 if (qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907 qc->scsicmd = NULL;
4908 qc->ap = ap;
4909 qc->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004910
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05004911 ata_qc_reinit(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912 }
4913
4914 return qc;
4915}
4916
Tejun Heo8a8bc222008-11-10 14:48:21 +09004917/**
4918 * ata_qc_free - free unused ata_queued_cmd
4919 * @qc: Command to complete
4920 *
4921 * Designed to free unused ata_queued_cmd object
4922 * in case something prevents using it.
4923 *
4924 * LOCKING:
4925 * spin_lock_irqsave(host lock)
4926 */
4927void ata_qc_free(struct ata_queued_cmd *qc)
4928{
Julia Lawalla1104012009-10-17 08:41:47 +02004929 struct ata_port *ap;
Tejun Heo8a8bc222008-11-10 14:48:21 +09004930 unsigned int tag;
4931
Tejun Heoefcb3cf2009-01-09 19:19:14 +09004932 WARN_ON_ONCE(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
Julia Lawalla1104012009-10-17 08:41:47 +02004933 ap = qc->ap;
Tejun Heo8a8bc222008-11-10 14:48:21 +09004934
4935 qc->flags = 0;
4936 tag = qc->tag;
4937 if (likely(ata_tag_valid(tag))) {
4938 qc->tag = ATA_TAG_POISON;
4939 clear_bit(tag, &ap->qc_allocated);
4940 }
4941}
4942
Tejun Heo76014422006-02-11 15:13:49 +09004943void __ata_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944{
Julia Lawalla1104012009-10-17 08:41:47 +02004945 struct ata_port *ap;
4946 struct ata_link *link;
Tejun Heodedaf2b2006-05-15 21:03:43 +09004947
Tejun Heoefcb3cf2009-01-09 19:19:14 +09004948 WARN_ON_ONCE(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
4949 WARN_ON_ONCE(!(qc->flags & ATA_QCFLAG_ACTIVE));
Julia Lawalla1104012009-10-17 08:41:47 +02004950 ap = qc->ap;
4951 link = qc->dev->link;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004952
4953 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
4954 ata_sg_clean(qc);
4955
Tejun Heo7401abf2006-05-15 20:57:32 +09004956 /* command should be marked inactive atomically with qc completion */
Tejun Heoda917d62007-09-23 13:14:12 +09004957 if (qc->tf.protocol == ATA_PROT_NCQ) {
Tejun Heo9af5c9c2007-08-06 18:36:22 +09004958 link->sactive &= ~(1 << qc->tag);
Tejun Heoda917d62007-09-23 13:14:12 +09004959 if (!link->sactive)
4960 ap->nr_active_links--;
4961 } else {
Tejun Heo9af5c9c2007-08-06 18:36:22 +09004962 link->active_tag = ATA_TAG_POISON;
Tejun Heoda917d62007-09-23 13:14:12 +09004963 ap->nr_active_links--;
4964 }
4965
4966 /* clear exclusive status */
4967 if (unlikely(qc->flags & ATA_QCFLAG_CLEAR_EXCL &&
4968 ap->excl_link == link))
4969 ap->excl_link = NULL;
Tejun Heo7401abf2006-05-15 20:57:32 +09004970
Albert Lee3f3791d2005-08-16 14:25:38 +08004971 /* atapi: mark qc as inactive to prevent the interrupt handler
4972 * from completing the command twice later, before the error handler
4973 * is called. (when rc != 0 and atapi request sense is needed)
4974 */
4975 qc->flags &= ~ATA_QCFLAG_ACTIVE;
Tejun Heodedaf2b2006-05-15 21:03:43 +09004976 ap->qc_active &= ~(1 << qc->tag);
Albert Lee3f3791d2005-08-16 14:25:38 +08004977
Linus Torvalds1da177e2005-04-16 15:20:36 -07004978 /* call completion callback */
Tejun Heo77853bf2006-01-23 13:09:36 +09004979 qc->complete_fn(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980}
4981
Tejun Heo39599a52006-11-14 22:37:35 +09004982static void fill_result_tf(struct ata_queued_cmd *qc)
4983{
4984 struct ata_port *ap = qc->ap;
4985
Tejun Heo39599a52006-11-14 22:37:35 +09004986 qc->result_tf.flags = qc->tf.flags;
Tejun Heo22183bf2008-04-07 22:47:20 +09004987 ap->ops->qc_fill_rtf(qc);
Tejun Heo39599a52006-11-14 22:37:35 +09004988}
4989
Tejun Heo00115e02007-11-27 19:28:58 +09004990static void ata_verify_xfer(struct ata_queued_cmd *qc)
4991{
4992 struct ata_device *dev = qc->dev;
4993
4994 if (ata_tag_internal(qc->tag))
4995 return;
4996
4997 if (ata_is_nodata(qc->tf.protocol))
4998 return;
4999
5000 if ((dev->mwdma_mask || dev->udma_mask) && ata_is_pio(qc->tf.protocol))
5001 return;
5002
5003 dev->flags &= ~ATA_DFLAG_DUBIOUS_XFER;
5004}
5005
Tejun Heof686bcb2006-05-15 20:58:05 +09005006/**
5007 * ata_qc_complete - Complete an active ATA command
5008 * @qc: Command to complete
Tejun Heof686bcb2006-05-15 20:58:05 +09005009 *
Tejun Heo1aadf5c2010-06-25 15:03:34 +02005010 * Indicate to the mid and upper layers that an ATA command has
5011 * completed, with either an ok or not-ok status.
5012 *
5013 * Refrain from calling this function multiple times when
5014 * successfully completing multiple NCQ commands.
5015 * ata_qc_complete_multiple() should be used instead, which will
5016 * properly update IRQ expect state.
Tejun Heof686bcb2006-05-15 20:58:05 +09005017 *
5018 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04005019 * spin_lock_irqsave(host lock)
Tejun Heof686bcb2006-05-15 20:58:05 +09005020 */
5021void ata_qc_complete(struct ata_queued_cmd *qc)
5022{
5023 struct ata_port *ap = qc->ap;
5024
5025 /* XXX: New EH and old EH use different mechanisms to
5026 * synchronize EH with regular execution path.
5027 *
5028 * In new EH, a failed qc is marked with ATA_QCFLAG_FAILED.
5029 * Normal execution path is responsible for not accessing a
5030 * failed qc. libata core enforces the rule by returning NULL
5031 * from ata_qc_from_tag() for failed qcs.
5032 *
5033 * Old EH depends on ata_qc_complete() nullifying completion
5034 * requests if ATA_QCFLAG_EH_SCHEDULED is set. Old EH does
5035 * not synchronize with interrupt handler. Only PIO task is
5036 * taken care of.
5037 */
5038 if (ap->ops->error_handler) {
Tejun Heo4dbfa392007-10-25 18:22:44 +09005039 struct ata_device *dev = qc->dev;
5040 struct ata_eh_info *ehi = &dev->link->eh_info;
5041
Tejun Heof686bcb2006-05-15 20:58:05 +09005042 if (unlikely(qc->err_mask))
5043 qc->flags |= ATA_QCFLAG_FAILED;
5044
5045 if (unlikely(qc->flags & ATA_QCFLAG_FAILED)) {
Tejun Heof4b31db92009-10-16 13:00:51 +09005046 /* always fill result TF for failed qc */
5047 fill_result_tf(qc);
5048
5049 if (!ata_tag_internal(qc->tag))
Tejun Heof686bcb2006-05-15 20:58:05 +09005050 ata_qc_schedule_eh(qc);
Tejun Heof4b31db92009-10-16 13:00:51 +09005051 else
5052 __ata_qc_complete(qc);
5053 return;
Tejun Heof686bcb2006-05-15 20:58:05 +09005054 }
5055
Tejun Heo4dc738e2009-09-17 18:45:27 +09005056 WARN_ON_ONCE(ap->pflags & ATA_PFLAG_FROZEN);
5057
Tejun Heof686bcb2006-05-15 20:58:05 +09005058 /* read result TF if requested */
5059 if (qc->flags & ATA_QCFLAG_RESULT_TF)
Tejun Heo39599a52006-11-14 22:37:35 +09005060 fill_result_tf(qc);
Tejun Heof686bcb2006-05-15 20:58:05 +09005061
Tejun Heo4dbfa392007-10-25 18:22:44 +09005062 /* Some commands need post-processing after successful
5063 * completion.
5064 */
5065 switch (qc->tf.command) {
5066 case ATA_CMD_SET_FEATURES:
5067 if (qc->tf.feature != SETFEATURES_WC_ON &&
5068 qc->tf.feature != SETFEATURES_WC_OFF)
5069 break;
5070 /* fall through */
5071 case ATA_CMD_INIT_DEV_PARAMS: /* CHS translation changed */
5072 case ATA_CMD_SET_MULTI: /* multi_count changed */
5073 /* revalidate device */
5074 ehi->dev_action[dev->devno] |= ATA_EH_REVALIDATE;
5075 ata_port_schedule_eh(ap);
5076 break;
Tejun Heo054a5fb2007-10-25 18:30:36 +09005077
5078 case ATA_CMD_SLEEP:
5079 dev->flags |= ATA_DFLAG_SLEEPING;
5080 break;
Tejun Heo4dbfa392007-10-25 18:22:44 +09005081 }
5082
Tejun Heo00115e02007-11-27 19:28:58 +09005083 if (unlikely(dev->flags & ATA_DFLAG_DUBIOUS_XFER))
5084 ata_verify_xfer(qc);
5085
Tejun Heof686bcb2006-05-15 20:58:05 +09005086 __ata_qc_complete(qc);
5087 } else {
5088 if (qc->flags & ATA_QCFLAG_EH_SCHEDULED)
5089 return;
5090
5091 /* read result TF if failed or requested */
5092 if (qc->err_mask || qc->flags & ATA_QCFLAG_RESULT_TF)
Tejun Heo39599a52006-11-14 22:37:35 +09005093 fill_result_tf(qc);
Tejun Heof686bcb2006-05-15 20:58:05 +09005094
5095 __ata_qc_complete(qc);
5096 }
5097}
5098
Tejun Heodedaf2b2006-05-15 21:03:43 +09005099/**
5100 * ata_qc_complete_multiple - Complete multiple qcs successfully
5101 * @ap: port in question
5102 * @qc_active: new qc_active mask
Tejun Heodedaf2b2006-05-15 21:03:43 +09005103 *
5104 * Complete in-flight commands. This functions is meant to be
5105 * called from low-level driver's interrupt routine to complete
5106 * requests normally. ap->qc_active and @qc_active is compared
5107 * and commands are completed accordingly.
5108 *
Tejun Heo1aadf5c2010-06-25 15:03:34 +02005109 * Always use this function when completing multiple NCQ commands
5110 * from IRQ handlers instead of calling ata_qc_complete()
5111 * multiple times to keep IRQ expect status properly in sync.
5112 *
Tejun Heodedaf2b2006-05-15 21:03:43 +09005113 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04005114 * spin_lock_irqsave(host lock)
Tejun Heodedaf2b2006-05-15 21:03:43 +09005115 *
5116 * RETURNS:
5117 * Number of completed commands on success, -errno otherwise.
5118 */
Tejun Heo79f97da2008-04-07 22:47:20 +09005119int ata_qc_complete_multiple(struct ata_port *ap, u32 qc_active)
Tejun Heodedaf2b2006-05-15 21:03:43 +09005120{
5121 int nr_done = 0;
5122 u32 done_mask;
Tejun Heodedaf2b2006-05-15 21:03:43 +09005123
5124 done_mask = ap->qc_active ^ qc_active;
5125
5126 if (unlikely(done_mask & qc_active)) {
5127 ata_port_printk(ap, KERN_ERR, "illegal qc_active transition "
5128 "(%08x->%08x)\n", ap->qc_active, qc_active);
5129 return -EINVAL;
5130 }
5131
Jens Axboe43768182009-05-20 09:44:39 +02005132 while (done_mask) {
Tejun Heodedaf2b2006-05-15 21:03:43 +09005133 struct ata_queued_cmd *qc;
Jens Axboe43768182009-05-20 09:44:39 +02005134 unsigned int tag = __ffs(done_mask);
Tejun Heodedaf2b2006-05-15 21:03:43 +09005135
Jens Axboe43768182009-05-20 09:44:39 +02005136 qc = ata_qc_from_tag(ap, tag);
5137 if (qc) {
Tejun Heodedaf2b2006-05-15 21:03:43 +09005138 ata_qc_complete(qc);
5139 nr_done++;
5140 }
Jens Axboe43768182009-05-20 09:44:39 +02005141 done_mask &= ~(1 << tag);
Tejun Heodedaf2b2006-05-15 21:03:43 +09005142 }
5143
5144 return nr_done;
5145}
5146
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147/**
5148 * ata_qc_issue - issue taskfile to device
5149 * @qc: command to issue to device
5150 *
5151 * Prepare an ATA command to submission to device.
5152 * This includes mapping the data into a DMA-able
5153 * area, filling in the S/G table, and finally
5154 * writing the taskfile to hardware, starting the command.
5155 *
5156 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04005157 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005158 */
Tejun Heo8e0e6942006-03-31 20:41:11 +09005159void ata_qc_issue(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005160{
5161 struct ata_port *ap = qc->ap;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005162 struct ata_link *link = qc->dev->link;
Tejun Heo405e66b2007-11-27 19:28:53 +09005163 u8 prot = qc->tf.protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005164
Tejun Heodedaf2b2006-05-15 21:03:43 +09005165 /* Make sure only one non-NCQ command is outstanding. The
5166 * check is skipped for old EH because it reuses active qc to
5167 * request ATAPI sense.
5168 */
Tejun Heoefcb3cf2009-01-09 19:19:14 +09005169 WARN_ON_ONCE(ap->ops->error_handler && ata_tag_valid(link->active_tag));
Tejun Heodedaf2b2006-05-15 21:03:43 +09005170
Tejun Heo1973a022007-12-05 10:36:13 +09005171 if (ata_is_ncq(prot)) {
Tejun Heoefcb3cf2009-01-09 19:19:14 +09005172 WARN_ON_ONCE(link->sactive & (1 << qc->tag));
Tejun Heoda917d62007-09-23 13:14:12 +09005173
5174 if (!link->sactive)
5175 ap->nr_active_links++;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005176 link->sactive |= 1 << qc->tag;
Tejun Heodedaf2b2006-05-15 21:03:43 +09005177 } else {
Tejun Heoefcb3cf2009-01-09 19:19:14 +09005178 WARN_ON_ONCE(link->sactive);
Tejun Heoda917d62007-09-23 13:14:12 +09005179
5180 ap->nr_active_links++;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005181 link->active_tag = qc->tag;
Tejun Heodedaf2b2006-05-15 21:03:43 +09005182 }
5183
Tejun Heoe4a70e72006-03-31 20:36:47 +09005184 qc->flags |= ATA_QCFLAG_ACTIVE;
Tejun Heodedaf2b2006-05-15 21:03:43 +09005185 ap->qc_active |= 1 << qc->tag;
Tejun Heoe4a70e72006-03-31 20:36:47 +09005186
Tejun Heo60f5d6e2010-08-23 11:27:27 +02005187 /*
5188 * We guarantee to LLDs that they will have at least one
Tejun Heof92a2632007-12-05 16:43:10 +09005189 * non-zero sg if the command is a data command.
5190 */
Tejun Heo60f5d6e2010-08-23 11:27:27 +02005191 if (WARN_ON_ONCE(ata_is_data(prot) &&
5192 (!qc->sg || !qc->n_elem || !qc->nbytes)))
5193 goto sys_err;
Tejun Heof92a2632007-12-05 16:43:10 +09005194
Tejun Heo405e66b2007-11-27 19:28:53 +09005195 if (ata_is_dma(prot) || (ata_is_pio(prot) &&
Tejun Heof92a2632007-12-05 16:43:10 +09005196 (ap->flags & ATA_FLAG_PIO_DMA)))
Tejun Heo001102d2007-12-05 16:43:09 +09005197 if (ata_sg_setup(qc))
Tejun Heo60f5d6e2010-08-23 11:27:27 +02005198 goto sys_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005199
Tejun Heocf480622008-01-24 00:05:14 +09005200 /* if device is sleeping, schedule reset and abort the link */
Tejun Heo054a5fb2007-10-25 18:30:36 +09005201 if (unlikely(qc->dev->flags & ATA_DFLAG_SLEEPING)) {
Tejun Heocf480622008-01-24 00:05:14 +09005202 link->eh_info.action |= ATA_EH_RESET;
Tejun Heo054a5fb2007-10-25 18:30:36 +09005203 ata_ehi_push_desc(&link->eh_info, "waking up from sleep");
5204 ata_link_abort(link);
5205 return;
5206 }
5207
Linus Torvalds1da177e2005-04-16 15:20:36 -07005208 ap->ops->qc_prep(qc);
5209
Tejun Heo8e0e6942006-03-31 20:41:11 +09005210 qc->err_mask |= ap->ops->qc_issue(qc);
5211 if (unlikely(qc->err_mask))
5212 goto err;
5213 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005214
Tejun Heo60f5d6e2010-08-23 11:27:27 +02005215sys_err:
Tejun Heo8e0e6942006-03-31 20:41:11 +09005216 qc->err_mask |= AC_ERR_SYSTEM;
5217err:
5218 ata_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005219}
5220
5221/**
Tejun Heo34bf2172006-05-15 20:57:46 +09005222 * sata_scr_valid - test whether SCRs are accessible
Tejun Heo936fd732007-08-06 18:36:23 +09005223 * @link: ATA link to test SCR accessibility for
Tejun Heo34bf2172006-05-15 20:57:46 +09005224 *
Tejun Heo936fd732007-08-06 18:36:23 +09005225 * Test whether SCRs are accessible for @link.
Tejun Heo34bf2172006-05-15 20:57:46 +09005226 *
5227 * LOCKING:
5228 * None.
5229 *
5230 * RETURNS:
5231 * 1 if SCRs are accessible, 0 otherwise.
5232 */
Tejun Heo936fd732007-08-06 18:36:23 +09005233int sata_scr_valid(struct ata_link *link)
Tejun Heo34bf2172006-05-15 20:57:46 +09005234{
Tejun Heo936fd732007-08-06 18:36:23 +09005235 struct ata_port *ap = link->ap;
5236
Tejun Heoa16abc02007-05-21 18:33:47 +02005237 return (ap->flags & ATA_FLAG_SATA) && ap->ops->scr_read;
Tejun Heo34bf2172006-05-15 20:57:46 +09005238}
5239
5240/**
5241 * sata_scr_read - read SCR register of the specified port
Tejun Heo936fd732007-08-06 18:36:23 +09005242 * @link: ATA link to read SCR for
Tejun Heo34bf2172006-05-15 20:57:46 +09005243 * @reg: SCR to read
5244 * @val: Place to store read value
5245 *
Tejun Heo936fd732007-08-06 18:36:23 +09005246 * Read SCR register @reg of @link into *@val. This function is
Tejun Heo633273a32007-09-23 13:19:54 +09005247 * guaranteed to succeed if @link is ap->link, the cable type of
5248 * the port is SATA and the port implements ->scr_read.
Tejun Heo34bf2172006-05-15 20:57:46 +09005249 *
5250 * LOCKING:
Tejun Heo633273a32007-09-23 13:19:54 +09005251 * None if @link is ap->link. Kernel thread context otherwise.
Tejun Heo34bf2172006-05-15 20:57:46 +09005252 *
5253 * RETURNS:
5254 * 0 on success, negative errno on failure.
5255 */
Tejun Heo936fd732007-08-06 18:36:23 +09005256int sata_scr_read(struct ata_link *link, int reg, u32 *val)
Tejun Heo34bf2172006-05-15 20:57:46 +09005257{
Tejun Heo633273a32007-09-23 13:19:54 +09005258 if (ata_is_host_link(link)) {
Tejun Heo633273a32007-09-23 13:19:54 +09005259 if (sata_scr_valid(link))
Tejun Heo82ef04f2008-07-31 17:02:40 +09005260 return link->ap->ops->scr_read(link, reg, val);
Tejun Heo633273a32007-09-23 13:19:54 +09005261 return -EOPNOTSUPP;
5262 }
5263
5264 return sata_pmp_scr_read(link, reg, val);
Tejun Heo34bf2172006-05-15 20:57:46 +09005265}
5266
5267/**
5268 * sata_scr_write - write SCR register of the specified port
Tejun Heo936fd732007-08-06 18:36:23 +09005269 * @link: ATA link to write SCR for
Tejun Heo34bf2172006-05-15 20:57:46 +09005270 * @reg: SCR to write
5271 * @val: value to write
5272 *
Tejun Heo936fd732007-08-06 18:36:23 +09005273 * Write @val to SCR register @reg of @link. This function is
Tejun Heo633273a32007-09-23 13:19:54 +09005274 * guaranteed to succeed if @link is ap->link, the cable type of
5275 * the port is SATA and the port implements ->scr_read.
Tejun Heo34bf2172006-05-15 20:57:46 +09005276 *
5277 * LOCKING:
Tejun Heo633273a32007-09-23 13:19:54 +09005278 * None if @link is ap->link. Kernel thread context otherwise.
Tejun Heo34bf2172006-05-15 20:57:46 +09005279 *
5280 * RETURNS:
5281 * 0 on success, negative errno on failure.
5282 */
Tejun Heo936fd732007-08-06 18:36:23 +09005283int sata_scr_write(struct ata_link *link, int reg, u32 val)
Tejun Heo34bf2172006-05-15 20:57:46 +09005284{
Tejun Heo633273a32007-09-23 13:19:54 +09005285 if (ata_is_host_link(link)) {
Tejun Heo633273a32007-09-23 13:19:54 +09005286 if (sata_scr_valid(link))
Tejun Heo82ef04f2008-07-31 17:02:40 +09005287 return link->ap->ops->scr_write(link, reg, val);
Tejun Heo633273a32007-09-23 13:19:54 +09005288 return -EOPNOTSUPP;
5289 }
5290
5291 return sata_pmp_scr_write(link, reg, val);
Tejun Heo34bf2172006-05-15 20:57:46 +09005292}
5293
5294/**
5295 * sata_scr_write_flush - write SCR register of the specified port and flush
Tejun Heo936fd732007-08-06 18:36:23 +09005296 * @link: ATA link to write SCR for
Tejun Heo34bf2172006-05-15 20:57:46 +09005297 * @reg: SCR to write
5298 * @val: value to write
5299 *
5300 * This function is identical to sata_scr_write() except that this
5301 * function performs flush after writing to the register.
5302 *
5303 * LOCKING:
Tejun Heo633273a32007-09-23 13:19:54 +09005304 * None if @link is ap->link. Kernel thread context otherwise.
Tejun Heo34bf2172006-05-15 20:57:46 +09005305 *
5306 * RETURNS:
5307 * 0 on success, negative errno on failure.
5308 */
Tejun Heo936fd732007-08-06 18:36:23 +09005309int sata_scr_write_flush(struct ata_link *link, int reg, u32 val)
Tejun Heo34bf2172006-05-15 20:57:46 +09005310{
Tejun Heo633273a32007-09-23 13:19:54 +09005311 if (ata_is_host_link(link)) {
Tejun Heo633273a32007-09-23 13:19:54 +09005312 int rc;
Tejun Heoda3dbb12007-07-16 14:29:40 +09005313
Tejun Heo633273a32007-09-23 13:19:54 +09005314 if (sata_scr_valid(link)) {
Tejun Heo82ef04f2008-07-31 17:02:40 +09005315 rc = link->ap->ops->scr_write(link, reg, val);
Tejun Heo633273a32007-09-23 13:19:54 +09005316 if (rc == 0)
Tejun Heo82ef04f2008-07-31 17:02:40 +09005317 rc = link->ap->ops->scr_read(link, reg, &val);
Tejun Heo633273a32007-09-23 13:19:54 +09005318 return rc;
5319 }
5320 return -EOPNOTSUPP;
Tejun Heo34bf2172006-05-15 20:57:46 +09005321 }
Tejun Heo633273a32007-09-23 13:19:54 +09005322
5323 return sata_pmp_scr_write(link, reg, val);
Tejun Heo34bf2172006-05-15 20:57:46 +09005324}
5325
5326/**
Tejun Heob1c72912008-07-31 17:02:43 +09005327 * ata_phys_link_online - test whether the given link is online
Tejun Heo936fd732007-08-06 18:36:23 +09005328 * @link: ATA link to test
Tejun Heo34bf2172006-05-15 20:57:46 +09005329 *
Tejun Heo936fd732007-08-06 18:36:23 +09005330 * Test whether @link is online. Note that this function returns
5331 * 0 if online status of @link cannot be obtained, so
5332 * ata_link_online(link) != !ata_link_offline(link).
Tejun Heo34bf2172006-05-15 20:57:46 +09005333 *
5334 * LOCKING:
5335 * None.
5336 *
5337 * RETURNS:
Tejun Heob5b3fa32008-07-31 17:02:42 +09005338 * True if the port online status is available and online.
Tejun Heo34bf2172006-05-15 20:57:46 +09005339 */
Tejun Heob1c72912008-07-31 17:02:43 +09005340bool ata_phys_link_online(struct ata_link *link)
Tejun Heo34bf2172006-05-15 20:57:46 +09005341{
5342 u32 sstatus;
5343
Tejun Heo936fd732007-08-06 18:36:23 +09005344 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
Tejun Heo9913ff82009-01-29 20:31:31 +09005345 ata_sstatus_online(sstatus))
Tejun Heob5b3fa32008-07-31 17:02:42 +09005346 return true;
5347 return false;
Tejun Heo34bf2172006-05-15 20:57:46 +09005348}
5349
5350/**
Tejun Heob1c72912008-07-31 17:02:43 +09005351 * ata_phys_link_offline - test whether the given link is offline
Tejun Heo936fd732007-08-06 18:36:23 +09005352 * @link: ATA link to test
Tejun Heo34bf2172006-05-15 20:57:46 +09005353 *
Tejun Heo936fd732007-08-06 18:36:23 +09005354 * Test whether @link is offline. Note that this function
5355 * returns 0 if offline status of @link cannot be obtained, so
5356 * ata_link_online(link) != !ata_link_offline(link).
Tejun Heo34bf2172006-05-15 20:57:46 +09005357 *
5358 * LOCKING:
5359 * None.
5360 *
5361 * RETURNS:
Tejun Heob5b3fa32008-07-31 17:02:42 +09005362 * True if the port offline status is available and offline.
Tejun Heo34bf2172006-05-15 20:57:46 +09005363 */
Tejun Heob1c72912008-07-31 17:02:43 +09005364bool ata_phys_link_offline(struct ata_link *link)
Tejun Heo34bf2172006-05-15 20:57:46 +09005365{
5366 u32 sstatus;
5367
Tejun Heo936fd732007-08-06 18:36:23 +09005368 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
Tejun Heo9913ff82009-01-29 20:31:31 +09005369 !ata_sstatus_online(sstatus))
Tejun Heob5b3fa32008-07-31 17:02:42 +09005370 return true;
5371 return false;
Tejun Heo34bf2172006-05-15 20:57:46 +09005372}
Edward Falk0baab862005-06-02 18:17:13 -04005373
Tejun Heob1c72912008-07-31 17:02:43 +09005374/**
5375 * ata_link_online - test whether the given link is online
5376 * @link: ATA link to test
5377 *
5378 * Test whether @link is online. This is identical to
5379 * ata_phys_link_online() when there's no slave link. When
5380 * there's a slave link, this function should only be called on
5381 * the master link and will return true if any of M/S links is
5382 * online.
5383 *
5384 * LOCKING:
5385 * None.
5386 *
5387 * RETURNS:
5388 * True if the port online status is available and online.
5389 */
5390bool ata_link_online(struct ata_link *link)
5391{
5392 struct ata_link *slave = link->ap->slave_link;
5393
5394 WARN_ON(link == slave); /* shouldn't be called on slave link */
5395
5396 return ata_phys_link_online(link) ||
5397 (slave && ata_phys_link_online(slave));
5398}
5399
5400/**
5401 * ata_link_offline - test whether the given link is offline
5402 * @link: ATA link to test
5403 *
5404 * Test whether @link is offline. This is identical to
5405 * ata_phys_link_offline() when there's no slave link. When
5406 * there's a slave link, this function should only be called on
5407 * the master link and will return true if both M/S links are
5408 * offline.
5409 *
5410 * LOCKING:
5411 * None.
5412 *
5413 * RETURNS:
5414 * True if the port offline status is available and offline.
5415 */
5416bool ata_link_offline(struct ata_link *link)
5417{
5418 struct ata_link *slave = link->ap->slave_link;
5419
5420 WARN_ON(link == slave); /* shouldn't be called on slave link */
5421
5422 return ata_phys_link_offline(link) &&
5423 (!slave || ata_phys_link_offline(slave));
5424}
5425
Tejun Heo6ffa01d2007-03-02 17:32:47 +09005426#ifdef CONFIG_PM
Jeff Garzikcca39742006-08-24 03:19:22 -04005427static int ata_host_request_pm(struct ata_host *host, pm_message_t mesg,
5428 unsigned int action, unsigned int ehi_flags,
5429 int wait)
Tejun Heo500530f2006-07-03 16:07:27 +09005430{
5431 unsigned long flags;
5432 int i, rc;
5433
Jeff Garzikcca39742006-08-24 03:19:22 -04005434 for (i = 0; i < host->n_ports; i++) {
5435 struct ata_port *ap = host->ports[i];
Tejun Heoe3667eb2007-08-06 18:36:24 +09005436 struct ata_link *link;
Tejun Heo500530f2006-07-03 16:07:27 +09005437
5438 /* Previous resume operation might still be in
5439 * progress. Wait for PM_PENDING to clear.
5440 */
5441 if (ap->pflags & ATA_PFLAG_PM_PENDING) {
5442 ata_port_wait_eh(ap);
5443 WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
5444 }
5445
5446 /* request PM ops to EH */
5447 spin_lock_irqsave(ap->lock, flags);
5448
5449 ap->pm_mesg = mesg;
5450 if (wait) {
5451 rc = 0;
5452 ap->pm_result = &rc;
5453 }
5454
5455 ap->pflags |= ATA_PFLAG_PM_PENDING;
Tejun Heo1eca4362008-11-03 20:03:17 +09005456 ata_for_each_link(link, ap, HOST_FIRST) {
Tejun Heoe3667eb2007-08-06 18:36:24 +09005457 link->eh_info.action |= action;
5458 link->eh_info.flags |= ehi_flags;
5459 }
Tejun Heo500530f2006-07-03 16:07:27 +09005460
5461 ata_port_schedule_eh(ap);
5462
5463 spin_unlock_irqrestore(ap->lock, flags);
5464
5465 /* wait and check result */
5466 if (wait) {
5467 ata_port_wait_eh(ap);
5468 WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
5469 if (rc)
5470 return rc;
5471 }
5472 }
5473
5474 return 0;
5475}
5476
5477/**
Jeff Garzikcca39742006-08-24 03:19:22 -04005478 * ata_host_suspend - suspend host
5479 * @host: host to suspend
Tejun Heo500530f2006-07-03 16:07:27 +09005480 * @mesg: PM message
5481 *
Jeff Garzikcca39742006-08-24 03:19:22 -04005482 * Suspend @host. Actual operation is performed by EH. This
Tejun Heo500530f2006-07-03 16:07:27 +09005483 * function requests EH to perform PM operations and waits for EH
5484 * to finish.
5485 *
5486 * LOCKING:
5487 * Kernel thread context (may sleep).
5488 *
5489 * RETURNS:
5490 * 0 on success, -errno on failure.
5491 */
Jeff Garzikcca39742006-08-24 03:19:22 -04005492int ata_host_suspend(struct ata_host *host, pm_message_t mesg)
Tejun Heo500530f2006-07-03 16:07:27 +09005493{
Tejun Heoe2f3d752010-09-07 14:05:31 +02005494 unsigned int ehi_flags = ATA_EHI_QUIET;
Tejun Heo9666f402007-05-04 21:27:47 +02005495 int rc;
Tejun Heo500530f2006-07-03 16:07:27 +09005496
Kristen Carlson Accardica773292007-10-25 00:58:59 -04005497 /*
5498 * disable link pm on all ports before requesting
5499 * any pm activity
5500 */
5501 ata_lpm_enable(host);
5502
Tejun Heoe2f3d752010-09-07 14:05:31 +02005503 /*
5504 * On some hardware, device fails to respond after spun down
5505 * for suspend. As the device won't be used before being
5506 * resumed, we don't need to touch the device. Ask EH to skip
5507 * the usual stuff and proceed directly to suspend.
5508 *
5509 * http://thread.gmane.org/gmane.linux.ide/46764
5510 */
5511 if (mesg.event == PM_EVENT_SUSPEND)
5512 ehi_flags |= ATA_EHI_NO_AUTOPSY | ATA_EHI_NO_RECOVERY;
5513
5514 rc = ata_host_request_pm(host, mesg, 0, ehi_flags, 1);
Jeff Garzik72ad6ec2008-02-25 17:31:10 -05005515 if (rc == 0)
5516 host->dev->power.power_state = mesg;
Tejun Heo500530f2006-07-03 16:07:27 +09005517 return rc;
5518}
5519
5520/**
Jeff Garzikcca39742006-08-24 03:19:22 -04005521 * ata_host_resume - resume host
5522 * @host: host to resume
Tejun Heo500530f2006-07-03 16:07:27 +09005523 *
Jeff Garzikcca39742006-08-24 03:19:22 -04005524 * Resume @host. Actual operation is performed by EH. This
Tejun Heo500530f2006-07-03 16:07:27 +09005525 * function requests EH to perform PM operations and returns.
5526 * Note that all resume operations are performed parallely.
5527 *
5528 * LOCKING:
5529 * Kernel thread context (may sleep).
5530 */
Jeff Garzikcca39742006-08-24 03:19:22 -04005531void ata_host_resume(struct ata_host *host)
Tejun Heo500530f2006-07-03 16:07:27 +09005532{
Tejun Heocf480622008-01-24 00:05:14 +09005533 ata_host_request_pm(host, PMSG_ON, ATA_EH_RESET,
Jeff Garzikcca39742006-08-24 03:19:22 -04005534 ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET, 0);
Jeff Garzik72ad6ec2008-02-25 17:31:10 -05005535 host->dev->power.power_state = PMSG_ON;
Kristen Carlson Accardica773292007-10-25 00:58:59 -04005536
5537 /* reenable link pm */
5538 ata_lpm_disable(host);
Tejun Heo500530f2006-07-03 16:07:27 +09005539}
Tejun Heo6ffa01d2007-03-02 17:32:47 +09005540#endif
Tejun Heo500530f2006-07-03 16:07:27 +09005541
Randy Dunlapc893a3a2006-01-28 13:15:32 -05005542/**
Tejun Heo3ef3b432006-05-31 18:27:30 +09005543 * ata_dev_init - Initialize an ata_device structure
5544 * @dev: Device structure to initialize
5545 *
5546 * Initialize @dev in preparation for probing.
5547 *
5548 * LOCKING:
5549 * Inherited from caller.
5550 */
5551void ata_dev_init(struct ata_device *dev)
5552{
Tejun Heob1c72912008-07-31 17:02:43 +09005553 struct ata_link *link = ata_dev_phys_link(dev);
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005554 struct ata_port *ap = link->ap;
Tejun Heo72fa4b72006-05-31 18:27:32 +09005555 unsigned long flags;
Tejun Heo3ef3b432006-05-31 18:27:30 +09005556
Tejun Heob1c72912008-07-31 17:02:43 +09005557 /* SATA spd limit is bound to the attached device, reset together */
Tejun Heo9af5c9c2007-08-06 18:36:22 +09005558 link->sata_spd_limit = link->hw_sata_spd_limit;
5559 link->sata_spd = 0;
Tejun Heo5a04bf42006-05-31 18:27:38 +09005560
Tejun Heo72fa4b72006-05-31 18:27:32 +09005561 /* High bits of dev->flags are used to record warm plug
5562 * requests which occur asynchronously. Synchronize using
Jeff Garzikcca39742006-08-24 03:19:22 -04005563 * host lock.
Tejun Heo72fa4b72006-05-31 18:27:32 +09005564 */
Jeff Garzikba6a1302006-06-22 23:46:10 -04005565 spin_lock_irqsave(ap->lock, flags);
Tejun Heo72fa4b72006-05-31 18:27:32 +09005566 dev->flags &= ~ATA_DFLAG_INIT_MASK;
Tejun Heo3dcc3232007-09-03 12:20:11 +09005567 dev->horkage = 0;
Jeff Garzikba6a1302006-06-22 23:46:10 -04005568 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo72fa4b72006-05-31 18:27:32 +09005569
Tejun Heo99cf6102009-01-29 20:31:32 +09005570 memset((void *)dev + ATA_DEVICE_CLEAR_BEGIN, 0,
5571 ATA_DEVICE_CLEAR_END - ATA_DEVICE_CLEAR_BEGIN);
Tejun Heo3ef3b432006-05-31 18:27:30 +09005572 dev->pio_mask = UINT_MAX;
5573 dev->mwdma_mask = UINT_MAX;
5574 dev->udma_mask = UINT_MAX;
5575}
5576
5577/**
Tejun Heo4fb37a22007-08-06 18:36:23 +09005578 * ata_link_init - Initialize an ata_link structure
5579 * @ap: ATA port link is attached to
5580 * @link: Link structure to initialize
Tejun Heo89898052007-08-06 18:36:23 +09005581 * @pmp: Port multiplier port number
Tejun Heo4fb37a22007-08-06 18:36:23 +09005582 *
5583 * Initialize @link.
5584 *
5585 * LOCKING:
5586 * Kernel thread context (may sleep)
5587 */
Tejun Heofb7fd612007-09-23 13:14:12 +09005588void ata_link_init(struct ata_port *ap, struct ata_link *link, int pmp)
Tejun Heo4fb37a22007-08-06 18:36:23 +09005589{
5590 int i;
5591
5592 /* clear everything except for devices */
Gwendal Grignoud9027472010-05-25 12:31:38 -07005593 memset((void *)link + ATA_LINK_CLEAR_BEGIN, 0,
5594 ATA_LINK_CLEAR_END - ATA_LINK_CLEAR_BEGIN);
Tejun Heo4fb37a22007-08-06 18:36:23 +09005595
5596 link->ap = ap;
Tejun Heo89898052007-08-06 18:36:23 +09005597 link->pmp = pmp;
Tejun Heo4fb37a22007-08-06 18:36:23 +09005598 link->active_tag = ATA_TAG_POISON;
5599 link->hw_sata_spd_limit = UINT_MAX;
5600
5601 /* can't use iterator, ap isn't initialized yet */
5602 for (i = 0; i < ATA_MAX_DEVICES; i++) {
5603 struct ata_device *dev = &link->device[i];
5604
5605 dev->link = link;
5606 dev->devno = dev - link->device;
Tejun Heo110f66d2009-09-16 04:17:28 +09005607#ifdef CONFIG_ATA_ACPI
5608 dev->gtf_filter = ata_acpi_gtf_filter;
5609#endif
Tejun Heo4fb37a22007-08-06 18:36:23 +09005610 ata_dev_init(dev);
5611 }
5612}
5613
5614/**
5615 * sata_link_init_spd - Initialize link->sata_spd_limit
5616 * @link: Link to configure sata_spd_limit for
5617 *
5618 * Initialize @link->[hw_]sata_spd_limit to the currently
5619 * configured value.
5620 *
5621 * LOCKING:
5622 * Kernel thread context (may sleep).
5623 *
5624 * RETURNS:
5625 * 0 on success, -errno on failure.
5626 */
Tejun Heofb7fd612007-09-23 13:14:12 +09005627int sata_link_init_spd(struct ata_link *link)
Tejun Heo4fb37a22007-08-06 18:36:23 +09005628{
Tejun Heo33267322008-02-13 09:15:09 +09005629 u8 spd;
Tejun Heo4fb37a22007-08-06 18:36:23 +09005630 int rc;
5631
Tejun Heod127ea72008-07-31 16:09:34 +09005632 rc = sata_scr_read(link, SCR_CONTROL, &link->saved_scontrol);
Tejun Heo4fb37a22007-08-06 18:36:23 +09005633 if (rc)
5634 return rc;
5635
Tejun Heod127ea72008-07-31 16:09:34 +09005636 spd = (link->saved_scontrol >> 4) & 0xf;
Tejun Heo4fb37a22007-08-06 18:36:23 +09005637 if (spd)
5638 link->hw_sata_spd_limit &= (1 << spd) - 1;
5639
Tejun Heo05944bd2008-08-13 20:19:09 +09005640 ata_force_link_limits(link);
Tejun Heo33267322008-02-13 09:15:09 +09005641
Tejun Heo4fb37a22007-08-06 18:36:23 +09005642 link->sata_spd_limit = link->hw_sata_spd_limit;
5643
5644 return 0;
5645}
5646
5647/**
Tejun Heof3187192007-04-17 23:44:07 +09005648 * ata_port_alloc - allocate and initialize basic ATA port resources
5649 * @host: ATA host this allocated port belongs to
Linus Torvalds1da177e2005-04-16 15:20:36 -07005650 *
Tejun Heof3187192007-04-17 23:44:07 +09005651 * Allocate and initialize basic ATA port resources.
5652 *
5653 * RETURNS:
5654 * Allocate ATA port on success, NULL on failure.
Jeff Garzik0cba6322005-05-30 19:49:12 -04005655 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005656 * LOCKING:
Tejun Heof3187192007-04-17 23:44:07 +09005657 * Inherited from calling layer (may sleep).
Linus Torvalds1da177e2005-04-16 15:20:36 -07005658 */
Tejun Heof3187192007-04-17 23:44:07 +09005659struct ata_port *ata_port_alloc(struct ata_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005660{
Tejun Heof3187192007-04-17 23:44:07 +09005661 struct ata_port *ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005662
Tejun Heof3187192007-04-17 23:44:07 +09005663 DPRINTK("ENTER\n");
5664
5665 ap = kzalloc(sizeof(*ap), GFP_KERNEL);
5666 if (!ap)
5667 return NULL;
Gwendal Grignoud9027472010-05-25 12:31:38 -07005668
Tejun Heof4d6d002007-05-01 11:50:15 +02005669 ap->pflags |= ATA_PFLAG_INITIALIZING;
Jeff Garzikcca39742006-08-24 03:19:22 -04005670 ap->lock = &host->lock;
Tejun Heof3187192007-04-17 23:44:07 +09005671 ap->print_id = -1;
Jeff Garzikcca39742006-08-24 03:19:22 -04005672 ap->host = host;
Tejun Heof3187192007-04-17 23:44:07 +09005673 ap->dev = host->dev;
Borislav Petkovbd5d8252006-06-11 23:17:01 -04005674
5675#if defined(ATA_VERBOSE_DEBUG)
5676 /* turn on all debugging levels */
5677 ap->msg_enable = 0x00FF;
5678#elif defined(ATA_DEBUG)
5679 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 +09005680#else
Borislav Petkov0dd4b212006-06-23 02:29:08 -04005681 ap->msg_enable = ATA_MSG_DRV | ATA_MSG_ERR | ATA_MSG_WARN;
Borislav Petkovbd5d8252006-06-11 23:17:01 -04005682#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005683
Tejun Heoad72cf92010-07-02 10:03:52 +02005684 mutex_init(&ap->scsi_scan_mutex);
David Howells65f27f32006-11-22 14:55:48 +00005685 INIT_DELAYED_WORK(&ap->hotplug_task, ata_scsi_hotplug);
5686 INIT_WORK(&ap->scsi_rescan_task, ata_scsi_dev_rescan);
Tejun Heoa72ec4c2006-01-23 13:09:37 +09005687 INIT_LIST_HEAD(&ap->eh_done_q);
Tejun Heoc6cf9e92006-05-31 18:27:27 +09005688 init_waitqueue_head(&ap->eh_wait_q);
Elias Oltmanns45fabbb2008-09-21 11:54:08 +02005689 init_completion(&ap->park_req_pending);
Tejun Heo5ddf24c2007-07-16 14:29:41 +09005690 init_timer_deferrable(&ap->fastdrain_timer);
5691 ap->fastdrain_timer.function = ata_eh_fastdrain_timerfn;
5692 ap->fastdrain_timer.data = (unsigned long)ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005693
Tejun Heo838df622006-05-15 20:57:44 +09005694 ap->cbl = ATA_CBL_NONE;
Tejun Heo838df622006-05-15 20:57:44 +09005695
Tejun Heo89898052007-08-06 18:36:23 +09005696 ata_link_init(ap, &ap->link, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005697
5698#ifdef ATA_IRQ_TRAP
5699 ap->stats.unhandled_irq = 1;
5700 ap->stats.idle_irq = 1;
5701#endif
Tejun Heo270390e2010-05-10 21:41:35 +02005702 ata_sff_port_init(ap);
5703
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704 return ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005705}
5706
Tejun Heof0d36ef2007-01-20 16:00:28 +09005707static void ata_host_release(struct device *gendev, void *res)
5708{
5709 struct ata_host *host = dev_get_drvdata(gendev);
5710 int i;
5711
5712 for (i = 0; i < host->n_ports; i++) {
5713 struct ata_port *ap = host->ports[i];
5714
Tejun Heoecef7252007-04-17 23:44:06 +09005715 if (!ap)
5716 continue;
5717
Tejun Heo49114872007-04-17 23:44:06 +09005718 if (ap->scsi_host)
Tejun Heo1aa506e2007-03-09 19:36:12 +09005719 scsi_host_put(ap->scsi_host);
5720
Tejun Heo633273a32007-09-23 13:19:54 +09005721 kfree(ap->pmp_link);
Tejun Heob1c72912008-07-31 17:02:43 +09005722 kfree(ap->slave_link);
Tejun Heo49114872007-04-17 23:44:06 +09005723 kfree(ap);
Tejun Heo1aa506e2007-03-09 19:36:12 +09005724 host->ports[i] = NULL;
5725 }
5726
Tejun Heo1aa56cc2007-02-27 22:33:21 +09005727 dev_set_drvdata(gendev, NULL);
Tejun Heof0d36ef2007-01-20 16:00:28 +09005728}
5729
Linus Torvalds1da177e2005-04-16 15:20:36 -07005730/**
Tejun Heof3187192007-04-17 23:44:07 +09005731 * ata_host_alloc - allocate and init basic ATA host resources
5732 * @dev: generic device this host is associated with
5733 * @max_ports: maximum number of ATA ports associated with this host
5734 *
5735 * Allocate and initialize basic ATA host resources. LLD calls
5736 * this function to allocate a host, initializes it fully and
5737 * attaches it using ata_host_register().
5738 *
5739 * @max_ports ports are allocated and host->n_ports is
5740 * initialized to @max_ports. The caller is allowed to decrease
5741 * host->n_ports before calling ata_host_register(). The unused
5742 * ports will be automatically freed on registration.
5743 *
5744 * RETURNS:
5745 * Allocate ATA host on success, NULL on failure.
5746 *
5747 * LOCKING:
5748 * Inherited from calling layer (may sleep).
5749 */
5750struct ata_host *ata_host_alloc(struct device *dev, int max_ports)
5751{
5752 struct ata_host *host;
5753 size_t sz;
5754 int i;
5755
5756 DPRINTK("ENTER\n");
5757
5758 if (!devres_open_group(dev, NULL, GFP_KERNEL))
5759 return NULL;
5760
5761 /* alloc a container for our list of ATA ports (buses) */
5762 sz = sizeof(struct ata_host) + (max_ports + 1) * sizeof(void *);
5763 /* alloc a container for our list of ATA ports (buses) */
5764 host = devres_alloc(ata_host_release, sz, GFP_KERNEL);
5765 if (!host)
5766 goto err_out;
5767
5768 devres_add(dev, host);
5769 dev_set_drvdata(dev, host);
5770
5771 spin_lock_init(&host->lock);
5772 host->dev = dev;
5773 host->n_ports = max_ports;
5774
5775 /* allocate ports bound to this host */
5776 for (i = 0; i < max_ports; i++) {
5777 struct ata_port *ap;
5778
5779 ap = ata_port_alloc(host);
5780 if (!ap)
5781 goto err_out;
5782
5783 ap->port_no = i;
5784 host->ports[i] = ap;
5785 }
5786
5787 devres_remove_group(dev, NULL);
5788 return host;
5789
5790 err_out:
5791 devres_release_group(dev, NULL);
5792 return NULL;
5793}
5794
5795/**
Tejun Heof5cda252007-04-17 23:44:07 +09005796 * ata_host_alloc_pinfo - alloc host and init with port_info array
5797 * @dev: generic device this host is associated with
5798 * @ppi: array of ATA port_info to initialize host with
5799 * @n_ports: number of ATA ports attached to this host
5800 *
5801 * Allocate ATA host and initialize with info from @ppi. If NULL
5802 * terminated, @ppi may contain fewer entries than @n_ports. The
5803 * last entry will be used for the remaining ports.
5804 *
5805 * RETURNS:
5806 * Allocate ATA host on success, NULL on failure.
5807 *
5808 * LOCKING:
5809 * Inherited from calling layer (may sleep).
5810 */
5811struct ata_host *ata_host_alloc_pinfo(struct device *dev,
5812 const struct ata_port_info * const * ppi,
5813 int n_ports)
5814{
5815 const struct ata_port_info *pi;
5816 struct ata_host *host;
5817 int i, j;
5818
5819 host = ata_host_alloc(dev, n_ports);
5820 if (!host)
5821 return NULL;
5822
5823 for (i = 0, j = 0, pi = NULL; i < host->n_ports; i++) {
5824 struct ata_port *ap = host->ports[i];
5825
5826 if (ppi[j])
5827 pi = ppi[j++];
5828
5829 ap->pio_mask = pi->pio_mask;
5830 ap->mwdma_mask = pi->mwdma_mask;
5831 ap->udma_mask = pi->udma_mask;
5832 ap->flags |= pi->flags;
Tejun Heo0c887582007-08-06 18:36:23 +09005833 ap->link.flags |= pi->link_flags;
Tejun Heof5cda252007-04-17 23:44:07 +09005834 ap->ops = pi->port_ops;
5835
5836 if (!host->ops && (pi->port_ops != &ata_dummy_port_ops))
5837 host->ops = pi->port_ops;
Tejun Heof5cda252007-04-17 23:44:07 +09005838 }
5839
5840 return host;
5841}
5842
Tejun Heob1c72912008-07-31 17:02:43 +09005843/**
5844 * ata_slave_link_init - initialize slave link
5845 * @ap: port to initialize slave link for
5846 *
5847 * Create and initialize slave link for @ap. This enables slave
5848 * link handling on the port.
5849 *
5850 * In libata, a port contains links and a link contains devices.
5851 * There is single host link but if a PMP is attached to it,
5852 * there can be multiple fan-out links. On SATA, there's usually
5853 * a single device connected to a link but PATA and SATA
5854 * controllers emulating TF based interface can have two - master
5855 * and slave.
5856 *
5857 * However, there are a few controllers which don't fit into this
5858 * abstraction too well - SATA controllers which emulate TF
5859 * interface with both master and slave devices but also have
5860 * separate SCR register sets for each device. These controllers
5861 * need separate links for physical link handling
5862 * (e.g. onlineness, link speed) but should be treated like a
5863 * traditional M/S controller for everything else (e.g. command
5864 * issue, softreset).
5865 *
5866 * slave_link is libata's way of handling this class of
5867 * controllers without impacting core layer too much. For
5868 * anything other than physical link handling, the default host
5869 * link is used for both master and slave. For physical link
5870 * handling, separate @ap->slave_link is used. All dirty details
5871 * are implemented inside libata core layer. From LLD's POV, the
5872 * only difference is that prereset, hardreset and postreset are
5873 * called once more for the slave link, so the reset sequence
5874 * looks like the following.
5875 *
5876 * prereset(M) -> prereset(S) -> hardreset(M) -> hardreset(S) ->
5877 * softreset(M) -> postreset(M) -> postreset(S)
5878 *
5879 * Note that softreset is called only for the master. Softreset
5880 * resets both M/S by definition, so SRST on master should handle
5881 * both (the standard method will work just fine).
5882 *
5883 * LOCKING:
5884 * Should be called before host is registered.
5885 *
5886 * RETURNS:
5887 * 0 on success, -errno on failure.
5888 */
5889int ata_slave_link_init(struct ata_port *ap)
5890{
5891 struct ata_link *link;
5892
5893 WARN_ON(ap->slave_link);
5894 WARN_ON(ap->flags & ATA_FLAG_PMP);
5895
5896 link = kzalloc(sizeof(*link), GFP_KERNEL);
5897 if (!link)
5898 return -ENOMEM;
5899
5900 ata_link_init(ap, link, 1);
5901 ap->slave_link = link;
5902 return 0;
5903}
5904
Tejun Heo32ebbc02007-11-08 13:09:00 +09005905static void ata_host_stop(struct device *gendev, void *res)
5906{
5907 struct ata_host *host = dev_get_drvdata(gendev);
5908 int i;
5909
5910 WARN_ON(!(host->flags & ATA_HOST_STARTED));
5911
5912 for (i = 0; i < host->n_ports; i++) {
5913 struct ata_port *ap = host->ports[i];
5914
5915 if (ap->ops->port_stop)
5916 ap->ops->port_stop(ap);
5917 }
5918
5919 if (host->ops->host_stop)
5920 host->ops->host_stop(host);
5921}
5922
Tejun Heof5cda252007-04-17 23:44:07 +09005923/**
Tejun Heo029cfd62008-03-25 12:22:49 +09005924 * ata_finalize_port_ops - finalize ata_port_operations
5925 * @ops: ata_port_operations to finalize
5926 *
5927 * An ata_port_operations can inherit from another ops and that
5928 * ops can again inherit from another. This can go on as many
5929 * times as necessary as long as there is no loop in the
5930 * inheritance chain.
5931 *
5932 * Ops tables are finalized when the host is started. NULL or
5933 * unspecified entries are inherited from the closet ancestor
5934 * which has the method and the entry is populated with it.
5935 * After finalization, the ops table directly points to all the
5936 * methods and ->inherits is no longer necessary and cleared.
5937 *
5938 * Using ATA_OP_NULL, inheriting ops can force a method to NULL.
5939 *
5940 * LOCKING:
5941 * None.
5942 */
5943static void ata_finalize_port_ops(struct ata_port_operations *ops)
5944{
Pradeep Singh Rautela2da67652008-05-29 23:28:14 +05305945 static DEFINE_SPINLOCK(lock);
Tejun Heo029cfd62008-03-25 12:22:49 +09005946 const struct ata_port_operations *cur;
5947 void **begin = (void **)ops;
5948 void **end = (void **)&ops->inherits;
5949 void **pp;
5950
5951 if (!ops || !ops->inherits)
5952 return;
5953
5954 spin_lock(&lock);
5955
5956 for (cur = ops->inherits; cur; cur = cur->inherits) {
5957 void **inherit = (void **)cur;
5958
5959 for (pp = begin; pp < end; pp++, inherit++)
5960 if (!*pp)
5961 *pp = *inherit;
5962 }
5963
5964 for (pp = begin; pp < end; pp++)
5965 if (IS_ERR(*pp))
5966 *pp = NULL;
5967
5968 ops->inherits = NULL;
5969
5970 spin_unlock(&lock);
5971}
5972
5973/**
Tejun Heoecef7252007-04-17 23:44:06 +09005974 * ata_host_start - start and freeze ports of an ATA host
5975 * @host: ATA host to start ports for
5976 *
5977 * Start and then freeze ports of @host. Started status is
5978 * recorded in host->flags, so this function can be called
5979 * multiple times. Ports are guaranteed to get started only
Tejun Heof3187192007-04-17 23:44:07 +09005980 * once. If host->ops isn't initialized yet, its set to the
5981 * first non-dummy port ops.
Tejun Heoecef7252007-04-17 23:44:06 +09005982 *
5983 * LOCKING:
5984 * Inherited from calling layer (may sleep).
5985 *
5986 * RETURNS:
5987 * 0 if all ports are started successfully, -errno otherwise.
5988 */
5989int ata_host_start(struct ata_host *host)
5990{
Tejun Heo32ebbc02007-11-08 13:09:00 +09005991 int have_stop = 0;
5992 void *start_dr = NULL;
Tejun Heoecef7252007-04-17 23:44:06 +09005993 int i, rc;
5994
5995 if (host->flags & ATA_HOST_STARTED)
5996 return 0;
5997
Tejun Heo029cfd62008-03-25 12:22:49 +09005998 ata_finalize_port_ops(host->ops);
5999
Tejun Heoecef7252007-04-17 23:44:06 +09006000 for (i = 0; i < host->n_ports; i++) {
6001 struct ata_port *ap = host->ports[i];
6002
Tejun Heo029cfd62008-03-25 12:22:49 +09006003 ata_finalize_port_ops(ap->ops);
6004
Tejun Heof3187192007-04-17 23:44:07 +09006005 if (!host->ops && !ata_port_is_dummy(ap))
6006 host->ops = ap->ops;
6007
Tejun Heo32ebbc02007-11-08 13:09:00 +09006008 if (ap->ops->port_stop)
6009 have_stop = 1;
6010 }
6011
6012 if (host->ops->host_stop)
6013 have_stop = 1;
6014
6015 if (have_stop) {
6016 start_dr = devres_alloc(ata_host_stop, 0, GFP_KERNEL);
6017 if (!start_dr)
6018 return -ENOMEM;
6019 }
6020
6021 for (i = 0; i < host->n_ports; i++) {
6022 struct ata_port *ap = host->ports[i];
6023
Tejun Heoecef7252007-04-17 23:44:06 +09006024 if (ap->ops->port_start) {
6025 rc = ap->ops->port_start(ap);
6026 if (rc) {
Alan Cox0f9fe9b2007-11-30 15:23:16 +00006027 if (rc != -ENODEV)
Andrew Morton0f757742008-01-10 14:33:09 -08006028 dev_printk(KERN_ERR, host->dev,
6029 "failed to start port %d "
6030 "(errno=%d)\n", i, rc);
Tejun Heoecef7252007-04-17 23:44:06 +09006031 goto err_out;
6032 }
6033 }
Tejun Heoecef7252007-04-17 23:44:06 +09006034 ata_eh_freeze_port(ap);
6035 }
6036
Tejun Heo32ebbc02007-11-08 13:09:00 +09006037 if (start_dr)
6038 devres_add(host->dev, start_dr);
Tejun Heoecef7252007-04-17 23:44:06 +09006039 host->flags |= ATA_HOST_STARTED;
6040 return 0;
6041
6042 err_out:
6043 while (--i >= 0) {
6044 struct ata_port *ap = host->ports[i];
6045
6046 if (ap->ops->port_stop)
6047 ap->ops->port_stop(ap);
6048 }
Tejun Heo32ebbc02007-11-08 13:09:00 +09006049 devres_free(start_dr);
Tejun Heoecef7252007-04-17 23:44:06 +09006050 return rc;
6051}
6052
6053/**
Jeff Garzikcca39742006-08-24 03:19:22 -04006054 * ata_sas_host_init - Initialize a host struct
6055 * @host: host to initialize
6056 * @dev: device host is attached to
6057 * @flags: host flags
6058 * @ops: port_ops
Brian Kingb03732f2006-08-07 14:27:10 -05006059 *
6060 * LOCKING:
6061 * PCI/etc. bus probe sem.
6062 *
6063 */
Tejun Heof3187192007-04-17 23:44:07 +09006064/* KILLME - the only user left is ipr */
Jeff Garzikcca39742006-08-24 03:19:22 -04006065void ata_host_init(struct ata_host *host, struct device *dev,
Tejun Heo029cfd62008-03-25 12:22:49 +09006066 unsigned long flags, struct ata_port_operations *ops)
Brian Kingb03732f2006-08-07 14:27:10 -05006067{
Jeff Garzikcca39742006-08-24 03:19:22 -04006068 spin_lock_init(&host->lock);
6069 host->dev = dev;
6070 host->flags = flags;
6071 host->ops = ops;
Brian Kingb03732f2006-08-07 14:27:10 -05006072}
6073
Arjan van de Ven79318052009-01-04 05:32:28 -08006074
6075static void async_port_probe(void *data, async_cookie_t cookie)
6076{
6077 int rc;
6078 struct ata_port *ap = data;
Arjan van de Ven886ad092009-01-09 15:54:07 -08006079
6080 /*
6081 * If we're not allowed to scan this host in parallel,
6082 * we need to wait until all previous scans have completed
6083 * before going further.
Arjan van de Venfa853a42009-01-10 10:18:44 -08006084 * Jeff Garzik says this is only within a controller, so we
6085 * don't need to wait for port 0, only for later ports.
Arjan van de Ven886ad092009-01-09 15:54:07 -08006086 */
Arjan van de Venfa853a42009-01-10 10:18:44 -08006087 if (!(ap->host->flags & ATA_HOST_PARALLEL_SCAN) && ap->port_no != 0)
Arjan van de Ven886ad092009-01-09 15:54:07 -08006088 async_synchronize_cookie(cookie);
6089
Arjan van de Ven79318052009-01-04 05:32:28 -08006090 /* probe */
6091 if (ap->ops->error_handler) {
6092 struct ata_eh_info *ehi = &ap->link.eh_info;
6093 unsigned long flags;
6094
Arjan van de Ven79318052009-01-04 05:32:28 -08006095 /* kick EH for boot probing */
6096 spin_lock_irqsave(ap->lock, flags);
6097
6098 ehi->probe_mask |= ATA_ALL_DEVICES;
6099 ehi->action |= ATA_EH_RESET | ATA_EH_LPM;
6100 ehi->flags |= ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET;
6101
6102 ap->pflags &= ~ATA_PFLAG_INITIALIZING;
6103 ap->pflags |= ATA_PFLAG_LOADING;
6104 ata_port_schedule_eh(ap);
6105
6106 spin_unlock_irqrestore(ap->lock, flags);
6107
6108 /* wait for EH to finish */
6109 ata_port_wait_eh(ap);
6110 } else {
6111 DPRINTK("ata%u: bus probe begin\n", ap->print_id);
6112 rc = ata_bus_probe(ap);
6113 DPRINTK("ata%u: bus probe end\n", ap->print_id);
6114
6115 if (rc) {
6116 /* FIXME: do something useful here?
6117 * Current libata behavior will
6118 * tear down everything when
6119 * the module is removed
6120 * or the h/w is unplugged.
6121 */
6122 }
6123 }
Arjan van de Venf29d3b22009-01-05 15:07:07 -08006124
6125 /* in order to keep device order, we need to synchronize at this point */
6126 async_synchronize_cookie(cookie);
6127
6128 ata_scsi_scan_host(ap, 1);
6129
Arjan van de Ven79318052009-01-04 05:32:28 -08006130}
Brian Kingb03732f2006-08-07 14:27:10 -05006131/**
Tejun Heof3187192007-04-17 23:44:07 +09006132 * ata_host_register - register initialized ATA host
6133 * @host: ATA host to register
6134 * @sht: template for SCSI host
Jeff Garzik0cba6322005-05-30 19:49:12 -04006135 *
Tejun Heof3187192007-04-17 23:44:07 +09006136 * Register initialized ATA host. @host is allocated using
6137 * ata_host_alloc() and fully initialized by LLD. This function
6138 * starts ports, registers @host with ATA and SCSI layers and
6139 * probe registered devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006140 *
6141 * LOCKING:
Tejun Heof3187192007-04-17 23:44:07 +09006142 * Inherited from calling layer (may sleep).
Linus Torvalds1da177e2005-04-16 15:20:36 -07006143 *
6144 * RETURNS:
Tejun Heof3187192007-04-17 23:44:07 +09006145 * 0 on success, -errno otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006146 */
Tejun Heof3187192007-04-17 23:44:07 +09006147int ata_host_register(struct ata_host *host, struct scsi_host_template *sht)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006148{
Tejun Heof3187192007-04-17 23:44:07 +09006149 int i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006150
Tejun Heof3187192007-04-17 23:44:07 +09006151 /* host must have been started */
6152 if (!(host->flags & ATA_HOST_STARTED)) {
6153 dev_printk(KERN_ERR, host->dev,
6154 "BUG: trying to register unstarted host\n");
6155 WARN_ON(1);
6156 return -EINVAL;
Alan Cox02f076a2006-09-26 17:35:32 +01006157 }
Tejun Heof0d36ef2007-01-20 16:00:28 +09006158
Tejun Heof3187192007-04-17 23:44:07 +09006159 /* Blow away unused ports. This happens when LLD can't
6160 * determine the exact number of ports to allocate at
6161 * allocation time.
6162 */
6163 for (i = host->n_ports; host->ports[i]; i++)
6164 kfree(host->ports[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006165
Tejun Heof3187192007-04-17 23:44:07 +09006166 /* give ports names and add SCSI hosts */
6167 for (i = 0; i < host->n_ports; i++)
6168 host->ports[i]->print_id = ata_print_id++;
Tejun Heof0d36ef2007-01-20 16:00:28 +09006169
Gwendal Grignoud9027472010-05-25 12:31:38 -07006170
6171 /* Create associated sysfs transport objects */
6172 for (i = 0; i < host->n_ports; i++) {
6173 rc = ata_tport_add(host->dev,host->ports[i]);
6174 if (rc) {
6175 goto err_tadd;
6176 }
6177 }
6178
Tejun Heof3187192007-04-17 23:44:07 +09006179 rc = ata_scsi_add_hosts(host, sht);
Tejun Heoecef7252007-04-17 23:44:06 +09006180 if (rc)
Gwendal Grignoud9027472010-05-25 12:31:38 -07006181 goto err_tadd;
Tejun Heoecef7252007-04-17 23:44:06 +09006182
Tejun Heofafbae82007-05-15 03:28:16 +09006183 /* associate with ACPI nodes */
6184 ata_acpi_associate(host);
6185
Tejun Heof3187192007-04-17 23:44:07 +09006186 /* set cable, sata_spd_limit and report */
Jeff Garzikcca39742006-08-24 03:19:22 -04006187 for (i = 0; i < host->n_ports; i++) {
6188 struct ata_port *ap = host->ports[i];
Tejun Heof3187192007-04-17 23:44:07 +09006189 unsigned long xfer_mask;
6190
6191 /* set SATA cable type if still unset */
6192 if (ap->cbl == ATA_CBL_NONE && (ap->flags & ATA_FLAG_SATA))
6193 ap->cbl = ATA_CBL_SATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006194
Tejun Heo5a04bf42006-05-31 18:27:38 +09006195 /* init sata_spd_limit to the current value */
Tejun Heo4fb37a22007-08-06 18:36:23 +09006196 sata_link_init_spd(&ap->link);
Tejun Heob1c72912008-07-31 17:02:43 +09006197 if (ap->slave_link)
6198 sata_link_init_spd(ap->slave_link);
Tejun Heo5a04bf42006-05-31 18:27:38 +09006199
Tejun Heocbcdd872007-08-18 13:14:55 +09006200 /* print per-port info to dmesg */
Tejun Heof3187192007-04-17 23:44:07 +09006201 xfer_mask = ata_pack_xfermask(ap->pio_mask, ap->mwdma_mask,
6202 ap->udma_mask);
6203
Tejun Heoabf6e8e2007-10-09 14:57:25 +09006204 if (!ata_port_is_dummy(ap)) {
Tejun Heocbcdd872007-08-18 13:14:55 +09006205 ata_port_printk(ap, KERN_INFO,
6206 "%cATA max %s %s\n",
Tejun Heoa16abc02007-05-21 18:33:47 +02006207 (ap->flags & ATA_FLAG_SATA) ? 'S' : 'P',
Tejun Heof3187192007-04-17 23:44:07 +09006208 ata_mode_string(xfer_mask),
Tejun Heocbcdd872007-08-18 13:14:55 +09006209 ap->link.eh_info.desc);
Tejun Heoabf6e8e2007-10-09 14:57:25 +09006210 ata_ehi_clear_desc(&ap->link.eh_info);
6211 } else
Tejun Heof3187192007-04-17 23:44:07 +09006212 ata_port_printk(ap, KERN_INFO, "DUMMY\n");
6213 }
6214
Vegard Nossumf6005352009-04-08 18:19:39 +02006215 /* perform each probe asynchronously */
Tejun Heof3187192007-04-17 23:44:07 +09006216 for (i = 0; i < host->n_ports; i++) {
6217 struct ata_port *ap = host->ports[i];
Arjan van de Ven79318052009-01-04 05:32:28 -08006218 async_schedule(async_port_probe, ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006220
Tejun Heof3187192007-04-17 23:44:07 +09006221 return 0;
Gwendal Grignoud9027472010-05-25 12:31:38 -07006222
6223 err_tadd:
6224 while (--i >= 0) {
6225 ata_tport_delete(host->ports[i]);
6226 }
6227 return rc;
6228
Tejun Heof3187192007-04-17 23:44:07 +09006229}
6230
6231/**
Tejun Heof5cda252007-04-17 23:44:07 +09006232 * ata_host_activate - start host, request IRQ and register it
6233 * @host: target ATA host
6234 * @irq: IRQ to request
6235 * @irq_handler: irq_handler used when requesting IRQ
6236 * @irq_flags: irq_flags used when requesting IRQ
6237 * @sht: scsi_host_template to use when registering the host
6238 *
6239 * After allocating an ATA host and initializing it, most libata
6240 * LLDs perform three steps to activate the host - start host,
6241 * request IRQ and register it. This helper takes necessasry
6242 * arguments and performs the three steps in one go.
6243 *
Paul Mundt3d46b2e2007-11-08 11:14:56 +09006244 * An invalid IRQ skips the IRQ registration and expects the host to
6245 * have set polling mode on the port. In this case, @irq_handler
6246 * should be NULL.
6247 *
Tejun Heof5cda252007-04-17 23:44:07 +09006248 * LOCKING:
6249 * Inherited from calling layer (may sleep).
6250 *
6251 * RETURNS:
6252 * 0 on success, -errno otherwise.
6253 */
6254int ata_host_activate(struct ata_host *host, int irq,
6255 irq_handler_t irq_handler, unsigned long irq_flags,
6256 struct scsi_host_template *sht)
6257{
Tejun Heocbcdd872007-08-18 13:14:55 +09006258 int i, rc;
Tejun Heof5cda252007-04-17 23:44:07 +09006259
6260 rc = ata_host_start(host);
6261 if (rc)
6262 return rc;
6263
Paul Mundt3d46b2e2007-11-08 11:14:56 +09006264 /* Special case for polling mode */
6265 if (!irq) {
6266 WARN_ON(irq_handler);
6267 return ata_host_register(host, sht);
6268 }
6269
Tejun Heof5cda252007-04-17 23:44:07 +09006270 rc = devm_request_irq(host->dev, irq, irq_handler, irq_flags,
6271 dev_driver_string(host->dev), host);
6272 if (rc)
6273 return rc;
6274
Tejun Heocbcdd872007-08-18 13:14:55 +09006275 for (i = 0; i < host->n_ports; i++)
6276 ata_port_desc(host->ports[i], "irq %d", irq);
Tejun Heo40318262007-07-03 01:38:47 +09006277
Tejun Heof5cda252007-04-17 23:44:07 +09006278 rc = ata_host_register(host, sht);
6279 /* if failed, just free the IRQ and leave ports alone */
6280 if (rc)
6281 devm_free_irq(host->dev, irq, host);
6282
6283 return rc;
6284}
6285
6286/**
Tejun Heo720ba122006-05-31 18:28:13 +09006287 * ata_port_detach - Detach ATA port in prepration of device removal
6288 * @ap: ATA port to be detached
6289 *
6290 * Detach all ATA devices and the associated SCSI devices of @ap;
6291 * then, remove the associated SCSI host. @ap is guaranteed to
6292 * be quiescent on return from this function.
6293 *
6294 * LOCKING:
6295 * Kernel thread context (may sleep).
6296 */
Adrian Bunk741b7762007-10-24 18:23:06 +02006297static void ata_port_detach(struct ata_port *ap)
Tejun Heo720ba122006-05-31 18:28:13 +09006298{
6299 unsigned long flags;
Tejun Heo720ba122006-05-31 18:28:13 +09006300
6301 if (!ap->ops->error_handler)
Tejun Heoc3cf30a2006-08-05 03:59:11 +09006302 goto skip_eh;
Tejun Heo720ba122006-05-31 18:28:13 +09006303
6304 /* tell EH we're leaving & flush EH */
Jeff Garzikba6a1302006-06-22 23:46:10 -04006305 spin_lock_irqsave(ap->lock, flags);
Tejun Heob51e9e52006-06-29 01:29:30 +09006306 ap->pflags |= ATA_PFLAG_UNLOADING;
Tejun Heoece180d2008-11-03 20:04:37 +09006307 ata_port_schedule_eh(ap);
Jeff Garzikba6a1302006-06-22 23:46:10 -04006308 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo720ba122006-05-31 18:28:13 +09006309
Tejun Heoece180d2008-11-03 20:04:37 +09006310 /* wait till EH commits suicide */
Tejun Heo720ba122006-05-31 18:28:13 +09006311 ata_port_wait_eh(ap);
6312
Tejun Heoece180d2008-11-03 20:04:37 +09006313 /* it better be dead now */
6314 WARN_ON(!(ap->pflags & ATA_PFLAG_UNLOADED));
Tejun Heo720ba122006-05-31 18:28:13 +09006315
Oleg Nesterov45a66c12007-07-09 11:46:13 -07006316 cancel_rearming_delayed_work(&ap->hotplug_task);
Tejun Heo720ba122006-05-31 18:28:13 +09006317
Tejun Heoc3cf30a2006-08-05 03:59:11 +09006318 skip_eh:
Gwendal Grignoud9027472010-05-25 12:31:38 -07006319 if (ap->pmp_link) {
6320 int i;
6321 for (i = 0; i < SATA_PMP_MAX_PORTS; i++)
6322 ata_tlink_delete(&ap->pmp_link[i]);
6323 }
6324 ata_tport_delete(ap);
6325
Tejun Heo720ba122006-05-31 18:28:13 +09006326 /* remove the associated SCSI host */
Jeff Garzikcca39742006-08-24 03:19:22 -04006327 scsi_remove_host(ap->scsi_host);
Tejun Heo720ba122006-05-31 18:28:13 +09006328}
6329
6330/**
Tejun Heo0529c152007-01-20 16:00:26 +09006331 * ata_host_detach - Detach all ports of an ATA host
6332 * @host: Host to detach
6333 *
6334 * Detach all ports of @host.
6335 *
6336 * LOCKING:
6337 * Kernel thread context (may sleep).
6338 */
6339void ata_host_detach(struct ata_host *host)
6340{
6341 int i;
6342
6343 for (i = 0; i < host->n_ports; i++)
6344 ata_port_detach(host->ports[i]);
Tejun Heo562f0c22007-12-15 15:05:01 +09006345
6346 /* the host is dead now, dissociate ACPI */
6347 ata_acpi_dissociate(host);
Tejun Heo0529c152007-01-20 16:00:26 +09006348}
6349
Jeff Garzik374b1872005-08-30 05:42:52 -04006350#ifdef CONFIG_PCI
6351
Edward Falk0baab862005-06-02 18:17:13 -04006352/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07006353 * ata_pci_remove_one - PCI layer callback for device removal
6354 * @pdev: PCI device that was removed
6355 *
Tejun Heob878ca52007-01-20 16:00:28 +09006356 * PCI layer indicates to libata via this hook that hot-unplug or
6357 * module unload event has occurred. Detach all ports. Resource
6358 * release is handled via devres.
Linus Torvalds1da177e2005-04-16 15:20:36 -07006359 *
6360 * LOCKING:
6361 * Inherited from PCI layer (may sleep).
6362 */
Tejun Heof0d36ef2007-01-20 16:00:28 +09006363void ata_pci_remove_one(struct pci_dev *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006364{
Jeff Garzik28555682007-10-11 17:12:35 -04006365 struct device *dev = &pdev->dev;
Jeff Garzikcca39742006-08-24 03:19:22 -04006366 struct ata_host *host = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006367
Tejun Heob878ca52007-01-20 16:00:28 +09006368 ata_host_detach(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006369}
6370
6371/* move to PCI subsystem */
Jeff Garzik057ace52005-10-22 14:27:05 -04006372int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006373{
6374 unsigned long tmp = 0;
6375
6376 switch (bits->width) {
6377 case 1: {
6378 u8 tmp8 = 0;
6379 pci_read_config_byte(pdev, bits->reg, &tmp8);
6380 tmp = tmp8;
6381 break;
6382 }
6383 case 2: {
6384 u16 tmp16 = 0;
6385 pci_read_config_word(pdev, bits->reg, &tmp16);
6386 tmp = tmp16;
6387 break;
6388 }
6389 case 4: {
6390 u32 tmp32 = 0;
6391 pci_read_config_dword(pdev, bits->reg, &tmp32);
6392 tmp = tmp32;
6393 break;
6394 }
6395
6396 default:
6397 return -EINVAL;
6398 }
6399
6400 tmp &= bits->mask;
6401
6402 return (tmp == bits->val) ? 1 : 0;
6403}
Jens Axboe9b847542006-01-06 09:28:07 +01006404
Tejun Heo6ffa01d2007-03-02 17:32:47 +09006405#ifdef CONFIG_PM
Tejun Heo3c5100c2006-07-26 16:58:33 +09006406void ata_pci_device_do_suspend(struct pci_dev *pdev, pm_message_t mesg)
Jens Axboe9b847542006-01-06 09:28:07 +01006407{
6408 pci_save_state(pdev);
Tejun Heo4c90d972007-02-20 18:14:48 +09006409 pci_disable_device(pdev);
Tejun Heo500530f2006-07-03 16:07:27 +09006410
Rafael J. Wysocki3a2d5b72008-02-23 19:13:25 +01006411 if (mesg.event & PM_EVENT_SLEEP)
Tejun Heo500530f2006-07-03 16:07:27 +09006412 pci_set_power_state(pdev, PCI_D3hot);
Jens Axboe9b847542006-01-06 09:28:07 +01006413}
6414
Tejun Heo553c4aa2006-12-26 19:39:50 +09006415int ata_pci_device_do_resume(struct pci_dev *pdev)
Jens Axboe9b847542006-01-06 09:28:07 +01006416{
Tejun Heo553c4aa2006-12-26 19:39:50 +09006417 int rc;
6418
Jens Axboe9b847542006-01-06 09:28:07 +01006419 pci_set_power_state(pdev, PCI_D0);
6420 pci_restore_state(pdev);
Tejun Heo553c4aa2006-12-26 19:39:50 +09006421
Tejun Heob878ca52007-01-20 16:00:28 +09006422 rc = pcim_enable_device(pdev);
Tejun Heo553c4aa2006-12-26 19:39:50 +09006423 if (rc) {
6424 dev_printk(KERN_ERR, &pdev->dev,
6425 "failed to enable device after resume (%d)\n", rc);
6426 return rc;
6427 }
6428
Jens Axboe9b847542006-01-06 09:28:07 +01006429 pci_set_master(pdev);
Tejun Heo553c4aa2006-12-26 19:39:50 +09006430 return 0;
Tejun Heo500530f2006-07-03 16:07:27 +09006431}
6432
Tejun Heo3c5100c2006-07-26 16:58:33 +09006433int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg)
Tejun Heo500530f2006-07-03 16:07:27 +09006434{
Jeff Garzikcca39742006-08-24 03:19:22 -04006435 struct ata_host *host = dev_get_drvdata(&pdev->dev);
Tejun Heo500530f2006-07-03 16:07:27 +09006436 int rc = 0;
6437
Jeff Garzikcca39742006-08-24 03:19:22 -04006438 rc = ata_host_suspend(host, mesg);
Tejun Heo500530f2006-07-03 16:07:27 +09006439 if (rc)
6440 return rc;
6441
Tejun Heo3c5100c2006-07-26 16:58:33 +09006442 ata_pci_device_do_suspend(pdev, mesg);
Tejun Heo500530f2006-07-03 16:07:27 +09006443
6444 return 0;
6445}
6446
6447int ata_pci_device_resume(struct pci_dev *pdev)
6448{
Jeff Garzikcca39742006-08-24 03:19:22 -04006449 struct ata_host *host = dev_get_drvdata(&pdev->dev);
Tejun Heo553c4aa2006-12-26 19:39:50 +09006450 int rc;
Tejun Heo500530f2006-07-03 16:07:27 +09006451
Tejun Heo553c4aa2006-12-26 19:39:50 +09006452 rc = ata_pci_device_do_resume(pdev);
6453 if (rc == 0)
6454 ata_host_resume(host);
6455 return rc;
Jens Axboe9b847542006-01-06 09:28:07 +01006456}
Tejun Heo6ffa01d2007-03-02 17:32:47 +09006457#endif /* CONFIG_PM */
6458
Linus Torvalds1da177e2005-04-16 15:20:36 -07006459#endif /* CONFIG_PCI */
6460
Tejun Heo33267322008-02-13 09:15:09 +09006461static int __init ata_parse_force_one(char **cur,
6462 struct ata_force_ent *force_ent,
6463 const char **reason)
6464{
6465 /* FIXME: Currently, there's no way to tag init const data and
6466 * using __initdata causes build failure on some versions of
6467 * gcc. Once __initdataconst is implemented, add const to the
6468 * following structure.
6469 */
6470 static struct ata_force_param force_tbl[] __initdata = {
6471 { "40c", .cbl = ATA_CBL_PATA40 },
6472 { "80c", .cbl = ATA_CBL_PATA80 },
6473 { "short40c", .cbl = ATA_CBL_PATA40_SHORT },
6474 { "unk", .cbl = ATA_CBL_PATA_UNK },
6475 { "ign", .cbl = ATA_CBL_PATA_IGN },
6476 { "sata", .cbl = ATA_CBL_SATA },
6477 { "1.5Gbps", .spd_limit = 1 },
6478 { "3.0Gbps", .spd_limit = 2 },
6479 { "noncq", .horkage_on = ATA_HORKAGE_NONCQ },
6480 { "ncq", .horkage_off = ATA_HORKAGE_NONCQ },
Tejun Heo43c9c592010-05-23 12:59:11 +02006481 { "dump_id", .horkage_on = ATA_HORKAGE_DUMP_ID },
Tejun Heo33267322008-02-13 09:15:09 +09006482 { "pio0", .xfer_mask = 1 << (ATA_SHIFT_PIO + 0) },
6483 { "pio1", .xfer_mask = 1 << (ATA_SHIFT_PIO + 1) },
6484 { "pio2", .xfer_mask = 1 << (ATA_SHIFT_PIO + 2) },
6485 { "pio3", .xfer_mask = 1 << (ATA_SHIFT_PIO + 3) },
6486 { "pio4", .xfer_mask = 1 << (ATA_SHIFT_PIO + 4) },
6487 { "pio5", .xfer_mask = 1 << (ATA_SHIFT_PIO + 5) },
6488 { "pio6", .xfer_mask = 1 << (ATA_SHIFT_PIO + 6) },
6489 { "mwdma0", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 0) },
6490 { "mwdma1", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 1) },
6491 { "mwdma2", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 2) },
6492 { "mwdma3", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 3) },
6493 { "mwdma4", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 4) },
6494 { "udma0", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 0) },
6495 { "udma16", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 0) },
6496 { "udma/16", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 0) },
6497 { "udma1", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 1) },
6498 { "udma25", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 1) },
6499 { "udma/25", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 1) },
6500 { "udma2", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 2) },
6501 { "udma33", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 2) },
6502 { "udma/33", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 2) },
6503 { "udma3", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 3) },
6504 { "udma44", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 3) },
6505 { "udma/44", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 3) },
6506 { "udma4", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 4) },
6507 { "udma66", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 4) },
6508 { "udma/66", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 4) },
6509 { "udma5", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 5) },
6510 { "udma100", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 5) },
6511 { "udma/100", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 5) },
6512 { "udma6", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 6) },
6513 { "udma133", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 6) },
6514 { "udma/133", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 6) },
6515 { "udma7", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 7) },
Tejun Heo05944bd2008-08-13 20:19:09 +09006516 { "nohrst", .lflags = ATA_LFLAG_NO_HRST },
6517 { "nosrst", .lflags = ATA_LFLAG_NO_SRST },
6518 { "norst", .lflags = ATA_LFLAG_NO_HRST | ATA_LFLAG_NO_SRST },
Tejun Heo33267322008-02-13 09:15:09 +09006519 };
6520 char *start = *cur, *p = *cur;
6521 char *id, *val, *endp;
6522 const struct ata_force_param *match_fp = NULL;
6523 int nr_matches = 0, i;
6524
6525 /* find where this param ends and update *cur */
6526 while (*p != '\0' && *p != ',')
6527 p++;
6528
6529 if (*p == '\0')
6530 *cur = p;
6531 else
6532 *cur = p + 1;
6533
6534 *p = '\0';
6535
6536 /* parse */
6537 p = strchr(start, ':');
6538 if (!p) {
6539 val = strstrip(start);
6540 goto parse_val;
6541 }
6542 *p = '\0';
6543
6544 id = strstrip(start);
6545 val = strstrip(p + 1);
6546
6547 /* parse id */
6548 p = strchr(id, '.');
6549 if (p) {
6550 *p++ = '\0';
6551 force_ent->device = simple_strtoul(p, &endp, 10);
6552 if (p == endp || *endp != '\0') {
6553 *reason = "invalid device";
6554 return -EINVAL;
6555 }
6556 }
6557
6558 force_ent->port = simple_strtoul(id, &endp, 10);
6559 if (p == endp || *endp != '\0') {
6560 *reason = "invalid port/link";
6561 return -EINVAL;
6562 }
6563
6564 parse_val:
6565 /* parse val, allow shortcuts so that both 1.5 and 1.5Gbps work */
6566 for (i = 0; i < ARRAY_SIZE(force_tbl); i++) {
6567 const struct ata_force_param *fp = &force_tbl[i];
6568
6569 if (strncasecmp(val, fp->name, strlen(val)))
6570 continue;
6571
6572 nr_matches++;
6573 match_fp = fp;
6574
6575 if (strcasecmp(val, fp->name) == 0) {
6576 nr_matches = 1;
6577 break;
6578 }
6579 }
6580
6581 if (!nr_matches) {
6582 *reason = "unknown value";
6583 return -EINVAL;
6584 }
6585 if (nr_matches > 1) {
6586 *reason = "ambigious value";
6587 return -EINVAL;
6588 }
6589
6590 force_ent->param = *match_fp;
6591
6592 return 0;
6593}
6594
6595static void __init ata_parse_force_param(void)
6596{
6597 int idx = 0, size = 1;
6598 int last_port = -1, last_device = -1;
6599 char *p, *cur, *next;
6600
6601 /* calculate maximum number of params and allocate force_tbl */
6602 for (p = ata_force_param_buf; *p; p++)
6603 if (*p == ',')
6604 size++;
6605
6606 ata_force_tbl = kzalloc(sizeof(ata_force_tbl[0]) * size, GFP_KERNEL);
6607 if (!ata_force_tbl) {
6608 printk(KERN_WARNING "ata: failed to extend force table, "
6609 "libata.force ignored\n");
6610 return;
6611 }
6612
6613 /* parse and populate the table */
6614 for (cur = ata_force_param_buf; *cur != '\0'; cur = next) {
6615 const char *reason = "";
6616 struct ata_force_ent te = { .port = -1, .device = -1 };
6617
6618 next = cur;
6619 if (ata_parse_force_one(&next, &te, &reason)) {
6620 printk(KERN_WARNING "ata: failed to parse force "
6621 "parameter \"%s\" (%s)\n",
6622 cur, reason);
6623 continue;
6624 }
6625
6626 if (te.port == -1) {
6627 te.port = last_port;
6628 te.device = last_device;
6629 }
6630
6631 ata_force_tbl[idx++] = te;
6632
6633 last_port = te.port;
6634 last_device = te.device;
6635 }
6636
6637 ata_force_tbl_size = idx;
6638}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006639
Linus Torvalds1da177e2005-04-16 15:20:36 -07006640static int __init ata_init(void)
6641{
Gwendal Grignoud9027472010-05-25 12:31:38 -07006642 int rc;
Tejun Heo270390e2010-05-10 21:41:35 +02006643
Tejun Heo33267322008-02-13 09:15:09 +09006644 ata_parse_force_param();
6645
Tejun Heo270390e2010-05-10 21:41:35 +02006646 rc = ata_sff_init();
Tejun Heoad72cf92010-07-02 10:03:52 +02006647 if (rc) {
6648 kfree(ata_force_tbl);
6649 return rc;
6650 }
Tejun Heo453b07a2006-05-31 18:27:42 +09006651
Gwendal Grignoud9027472010-05-25 12:31:38 -07006652 libata_transport_init();
6653 ata_scsi_transport_template = ata_attach_transport();
6654 if (!ata_scsi_transport_template) {
6655 ata_sff_exit();
6656 rc = -ENOMEM;
6657 goto err_out;
6658 }
6659
Linus Torvalds1da177e2005-04-16 15:20:36 -07006660 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
6661 return 0;
Gwendal Grignoud9027472010-05-25 12:31:38 -07006662
6663err_out:
6664 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006665}
6666
6667static void __exit ata_exit(void)
6668{
Gwendal Grignoud9027472010-05-25 12:31:38 -07006669 ata_release_transport(ata_scsi_transport_template);
6670 libata_transport_exit();
Tejun Heo270390e2010-05-10 21:41:35 +02006671 ata_sff_exit();
Tejun Heo33267322008-02-13 09:15:09 +09006672 kfree(ata_force_tbl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006673}
6674
Brian Kinga4625082006-11-13 16:32:36 -06006675subsys_initcall(ata_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006676module_exit(ata_exit);
6677
Akinobu Mita9990b6f2010-04-12 21:11:41 +09006678static DEFINE_RATELIMIT_STATE(ratelimit, HZ / 5, 1);
Jeff Garzik67846b32005-10-05 02:58:32 -04006679
6680int ata_ratelimit(void)
6681{
Akinobu Mita9990b6f2010-04-12 21:11:41 +09006682 return __ratelimit(&ratelimit);
Jeff Garzik67846b32005-10-05 02:58:32 -04006683}
6684
Tejun Heoc22daff2006-04-11 22:22:29 +09006685/**
6686 * ata_wait_register - wait until register value changes
6687 * @reg: IO-mapped register
6688 * @mask: Mask to apply to read register value
6689 * @val: Wait condition
Tejun Heo341c2c92008-05-20 02:17:51 +09006690 * @interval: polling interval in milliseconds
6691 * @timeout: timeout in milliseconds
Tejun Heoc22daff2006-04-11 22:22:29 +09006692 *
6693 * Waiting for some bits of register to change is a common
6694 * operation for ATA controllers. This function reads 32bit LE
6695 * IO-mapped register @reg and tests for the following condition.
6696 *
6697 * (*@reg & mask) != val
6698 *
6699 * If the condition is met, it returns; otherwise, the process is
6700 * repeated after @interval_msec until timeout.
6701 *
6702 * LOCKING:
6703 * Kernel thread context (may sleep)
6704 *
6705 * RETURNS:
6706 * The final register value.
6707 */
6708u32 ata_wait_register(void __iomem *reg, u32 mask, u32 val,
Tejun Heo341c2c92008-05-20 02:17:51 +09006709 unsigned long interval, unsigned long timeout)
Tejun Heoc22daff2006-04-11 22:22:29 +09006710{
Tejun Heo341c2c92008-05-20 02:17:51 +09006711 unsigned long deadline;
Tejun Heoc22daff2006-04-11 22:22:29 +09006712 u32 tmp;
6713
6714 tmp = ioread32(reg);
6715
6716 /* Calculate timeout _after_ the first read to make sure
6717 * preceding writes reach the controller before starting to
6718 * eat away the timeout.
6719 */
Tejun Heo341c2c92008-05-20 02:17:51 +09006720 deadline = ata_deadline(jiffies, timeout);
Tejun Heoc22daff2006-04-11 22:22:29 +09006721
Tejun Heo341c2c92008-05-20 02:17:51 +09006722 while ((tmp & mask) == val && time_before(jiffies, deadline)) {
6723 msleep(interval);
Tejun Heoc22daff2006-04-11 22:22:29 +09006724 tmp = ioread32(reg);
6725 }
6726
6727 return tmp;
6728}
6729
Linus Torvalds1da177e2005-04-16 15:20:36 -07006730/*
Tejun Heodd5b06c2006-08-10 16:59:12 +09006731 * Dummy port_ops
6732 */
Tejun Heodd5b06c2006-08-10 16:59:12 +09006733static unsigned int ata_dummy_qc_issue(struct ata_queued_cmd *qc)
6734{
6735 return AC_ERR_SYSTEM;
6736}
6737
Tejun Heo182d7bb2008-04-07 22:47:21 +09006738static void ata_dummy_error_handler(struct ata_port *ap)
6739{
6740 /* truly dummy */
6741}
6742
Tejun Heo029cfd62008-03-25 12:22:49 +09006743struct ata_port_operations ata_dummy_port_ops = {
Tejun Heodd5b06c2006-08-10 16:59:12 +09006744 .qc_prep = ata_noop_qc_prep,
6745 .qc_issue = ata_dummy_qc_issue,
Tejun Heo182d7bb2008-04-07 22:47:21 +09006746 .error_handler = ata_dummy_error_handler,
Tejun Heodd5b06c2006-08-10 16:59:12 +09006747};
6748
Tejun Heo21b0ad42007-04-17 23:44:07 +09006749const struct ata_port_info ata_dummy_port_info = {
6750 .port_ops = &ata_dummy_port_ops,
6751};
6752
Tejun Heodd5b06c2006-08-10 16:59:12 +09006753/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006754 * libata is essentially a library of internal helper functions for
6755 * low-level ATA host controller drivers. As such, the API/ABI is
6756 * likely to change as new drivers are added and updated.
6757 * Do not depend on ABI/API stability.
6758 */
Tejun Heoe9c83912006-07-03 16:07:26 +09006759EXPORT_SYMBOL_GPL(sata_deb_timing_normal);
6760EXPORT_SYMBOL_GPL(sata_deb_timing_hotplug);
6761EXPORT_SYMBOL_GPL(sata_deb_timing_long);
Tejun Heo029cfd62008-03-25 12:22:49 +09006762EXPORT_SYMBOL_GPL(ata_base_port_ops);
6763EXPORT_SYMBOL_GPL(sata_port_ops);
Tejun Heodd5b06c2006-08-10 16:59:12 +09006764EXPORT_SYMBOL_GPL(ata_dummy_port_ops);
Tejun Heo21b0ad42007-04-17 23:44:07 +09006765EXPORT_SYMBOL_GPL(ata_dummy_port_info);
Tejun Heo1eca4362008-11-03 20:03:17 +09006766EXPORT_SYMBOL_GPL(ata_link_next);
6767EXPORT_SYMBOL_GPL(ata_dev_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006768EXPORT_SYMBOL_GPL(ata_std_bios_param);
Tejun Heod8d91292010-05-15 20:09:34 +02006769EXPORT_SYMBOL_GPL(ata_scsi_unlock_native_capacity);
Jeff Garzikcca39742006-08-24 03:19:22 -04006770EXPORT_SYMBOL_GPL(ata_host_init);
Tejun Heof3187192007-04-17 23:44:07 +09006771EXPORT_SYMBOL_GPL(ata_host_alloc);
Tejun Heof5cda252007-04-17 23:44:07 +09006772EXPORT_SYMBOL_GPL(ata_host_alloc_pinfo);
Tejun Heob1c72912008-07-31 17:02:43 +09006773EXPORT_SYMBOL_GPL(ata_slave_link_init);
Tejun Heoecef7252007-04-17 23:44:06 +09006774EXPORT_SYMBOL_GPL(ata_host_start);
Tejun Heof3187192007-04-17 23:44:07 +09006775EXPORT_SYMBOL_GPL(ata_host_register);
Tejun Heof5cda252007-04-17 23:44:07 +09006776EXPORT_SYMBOL_GPL(ata_host_activate);
Tejun Heo0529c152007-01-20 16:00:26 +09006777EXPORT_SYMBOL_GPL(ata_host_detach);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006778EXPORT_SYMBOL_GPL(ata_sg_init);
Tejun Heof686bcb2006-05-15 20:58:05 +09006779EXPORT_SYMBOL_GPL(ata_qc_complete);
Tejun Heodedaf2b2006-05-15 21:03:43 +09006780EXPORT_SYMBOL_GPL(ata_qc_complete_multiple);
Tejun Heo436d34b2008-04-02 17:28:46 +09006781EXPORT_SYMBOL_GPL(atapi_cmd_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006782EXPORT_SYMBOL_GPL(ata_tf_to_fis);
6783EXPORT_SYMBOL_GPL(ata_tf_from_fis);
Tejun Heo63573572007-11-27 19:43:39 +09006784EXPORT_SYMBOL_GPL(ata_pack_xfermask);
6785EXPORT_SYMBOL_GPL(ata_unpack_xfermask);
6786EXPORT_SYMBOL_GPL(ata_xfer_mask2mode);
6787EXPORT_SYMBOL_GPL(ata_xfer_mode2mask);
6788EXPORT_SYMBOL_GPL(ata_xfer_mode2shift);
6789EXPORT_SYMBOL_GPL(ata_mode_string);
6790EXPORT_SYMBOL_GPL(ata_id_xfermask);
Alan04351822007-03-06 02:37:52 -08006791EXPORT_SYMBOL_GPL(ata_do_set_mode);
Tejun Heo31cc23b2007-09-23 13:14:12 +09006792EXPORT_SYMBOL_GPL(ata_std_qc_defer);
Brian Kinge46834c2006-03-17 17:04:03 -06006793EXPORT_SYMBOL_GPL(ata_noop_qc_prep);
Alan10305f02007-02-20 18:01:59 +00006794EXPORT_SYMBOL_GPL(ata_dev_disable);
Tejun Heo3c567b72006-05-15 20:57:23 +09006795EXPORT_SYMBOL_GPL(sata_set_spd);
Tejun Heoaa2731a2008-04-07 22:47:19 +09006796EXPORT_SYMBOL_GPL(ata_wait_after_reset);
Tejun Heo936fd732007-08-06 18:36:23 +09006797EXPORT_SYMBOL_GPL(sata_link_debounce);
6798EXPORT_SYMBOL_GPL(sata_link_resume);
Tejun Heo1152b262010-09-01 17:50:05 +02006799EXPORT_SYMBOL_GPL(sata_link_scr_lpm);
Tejun Heo0aa11132008-04-07 22:47:18 +09006800EXPORT_SYMBOL_GPL(ata_std_prereset);
Tejun Heocc0680a2007-08-06 18:36:23 +09006801EXPORT_SYMBOL_GPL(sata_link_hardreset);
Tejun Heo57c9efd2008-04-07 22:47:19 +09006802EXPORT_SYMBOL_GPL(sata_std_hardreset);
Tejun Heo203c75b2008-04-07 22:47:18 +09006803EXPORT_SYMBOL_GPL(ata_std_postreset);
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05006804EXPORT_SYMBOL_GPL(ata_dev_classify);
6805EXPORT_SYMBOL_GPL(ata_dev_pair);
Jeff Garzik67846b32005-10-05 02:58:32 -04006806EXPORT_SYMBOL_GPL(ata_ratelimit);
Tejun Heoc22daff2006-04-11 22:22:29 +09006807EXPORT_SYMBOL_GPL(ata_wait_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006808EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006809EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
Tejun Heo83c47bc2006-05-31 18:28:07 +09006810EXPORT_SYMBOL_GPL(ata_scsi_slave_destroy);
Tejun Heoa6e6ce82006-05-15 21:03:48 +09006811EXPORT_SYMBOL_GPL(ata_scsi_change_queue_depth);
Tejun Heo34bf2172006-05-15 20:57:46 +09006812EXPORT_SYMBOL_GPL(sata_scr_valid);
6813EXPORT_SYMBOL_GPL(sata_scr_read);
6814EXPORT_SYMBOL_GPL(sata_scr_write);
6815EXPORT_SYMBOL_GPL(sata_scr_write_flush);
Tejun Heo936fd732007-08-06 18:36:23 +09006816EXPORT_SYMBOL_GPL(ata_link_online);
6817EXPORT_SYMBOL_GPL(ata_link_offline);
Tejun Heo6ffa01d2007-03-02 17:32:47 +09006818#ifdef CONFIG_PM
Jeff Garzikcca39742006-08-24 03:19:22 -04006819EXPORT_SYMBOL_GPL(ata_host_suspend);
6820EXPORT_SYMBOL_GPL(ata_host_resume);
Tejun Heo6ffa01d2007-03-02 17:32:47 +09006821#endif /* CONFIG_PM */
Tejun Heo6a62a042006-02-13 10:02:46 +09006822EXPORT_SYMBOL_GPL(ata_id_string);
6823EXPORT_SYMBOL_GPL(ata_id_c_string);
Alan Cox963e4972008-07-24 17:16:06 +01006824EXPORT_SYMBOL_GPL(ata_do_dev_read_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006825EXPORT_SYMBOL_GPL(ata_scsi_simulate);
6826
Alan Cox1bc4ccf2006-01-09 17:18:14 +00006827EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
Tejun Heo63573572007-11-27 19:43:39 +09006828EXPORT_SYMBOL_GPL(ata_timing_find_mode);
Alan Cox452503f2005-10-21 19:01:32 -04006829EXPORT_SYMBOL_GPL(ata_timing_compute);
6830EXPORT_SYMBOL_GPL(ata_timing_merge);
Tejun Heoa0f79b92007-12-18 16:33:05 +09006831EXPORT_SYMBOL_GPL(ata_timing_cycle2mode);
Alan Cox452503f2005-10-21 19:01:32 -04006832
Linus Torvalds1da177e2005-04-16 15:20:36 -07006833#ifdef CONFIG_PCI
6834EXPORT_SYMBOL_GPL(pci_test_config_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006835EXPORT_SYMBOL_GPL(ata_pci_remove_one);
Tejun Heo6ffa01d2007-03-02 17:32:47 +09006836#ifdef CONFIG_PM
Tejun Heo500530f2006-07-03 16:07:27 +09006837EXPORT_SYMBOL_GPL(ata_pci_device_do_suspend);
6838EXPORT_SYMBOL_GPL(ata_pci_device_do_resume);
Jens Axboe9b847542006-01-06 09:28:07 +01006839EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
6840EXPORT_SYMBOL_GPL(ata_pci_device_resume);
Tejun Heo6ffa01d2007-03-02 17:32:47 +09006841#endif /* CONFIG_PM */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006842#endif /* CONFIG_PCI */
Jens Axboe9b847542006-01-06 09:28:07 +01006843
Tejun Heob64bbc32007-07-16 14:29:39 +09006844EXPORT_SYMBOL_GPL(__ata_ehi_push_desc);
6845EXPORT_SYMBOL_GPL(ata_ehi_push_desc);
6846EXPORT_SYMBOL_GPL(ata_ehi_clear_desc);
Tejun Heocbcdd872007-08-18 13:14:55 +09006847EXPORT_SYMBOL_GPL(ata_port_desc);
6848#ifdef CONFIG_PCI
6849EXPORT_SYMBOL_GPL(ata_port_pbar_desc);
6850#endif /* CONFIG_PCI */
Tejun Heo7b70fc02006-05-15 20:58:07 +09006851EXPORT_SYMBOL_GPL(ata_port_schedule_eh);
Tejun Heodbd82612007-08-06 18:36:23 +09006852EXPORT_SYMBOL_GPL(ata_link_abort);
Tejun Heo7b70fc02006-05-15 20:58:07 +09006853EXPORT_SYMBOL_GPL(ata_port_abort);
Tejun Heoe3180492006-05-15 20:58:09 +09006854EXPORT_SYMBOL_GPL(ata_port_freeze);
Tejun Heo7d77b242007-09-23 13:14:13 +09006855EXPORT_SYMBOL_GPL(sata_async_notification);
Tejun Heoe3180492006-05-15 20:58:09 +09006856EXPORT_SYMBOL_GPL(ata_eh_freeze_port);
6857EXPORT_SYMBOL_GPL(ata_eh_thaw_port);
Tejun Heoece1d632006-04-02 18:51:53 +09006858EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
6859EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
Mark Lord10acf3b2008-05-02 02:14:53 -04006860EXPORT_SYMBOL_GPL(ata_eh_analyze_ncq_error);
Tejun Heo022bdb02006-05-15 20:58:22 +09006861EXPORT_SYMBOL_GPL(ata_do_eh);
Tejun Heoa1efdab2008-03-25 12:22:50 +09006862EXPORT_SYMBOL_GPL(ata_std_error_handler);
Alan Coxbe0d18d2007-03-06 02:37:56 -08006863
6864EXPORT_SYMBOL_GPL(ata_cable_40wire);
6865EXPORT_SYMBOL_GPL(ata_cable_80wire);
6866EXPORT_SYMBOL_GPL(ata_cable_unknown);
Tejun Heoc88f90c2007-11-27 19:43:48 +09006867EXPORT_SYMBOL_GPL(ata_cable_ignore);
Alan Coxbe0d18d2007-03-06 02:37:56 -08006868EXPORT_SYMBOL_GPL(ata_cable_sata);