blob: 3fd7c7932707ec48758b03dc137e655661cf2d08 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Jeff Garzikaf36d7f2005-08-28 20:18:39 -04002 * libata-core.c - helper library for ATA
3 *
4 * Maintained by: Jeff Garzik <jgarzik@pobox.com>
5 * Please ALWAYS copy linux-ide@vger.kernel.org
6 * on emails.
7 *
8 * Copyright 2003-2004 Red Hat, Inc. All rights reserved.
9 * Copyright 2003-2004 Jeff Garzik
10 *
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2, or (at your option)
15 * any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; see the file COPYING. If not, write to
24 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
25 *
26 *
27 * libata documentation is available via 'make {ps|pdf}docs',
28 * as Documentation/DocBook/libata.*
29 *
30 * Hardware documentation available from http://www.t13.org/ and
31 * http://www.sata-io.org/
32 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 */
34
Linus Torvalds1da177e2005-04-16 15:20:36 -070035#include <linux/kernel.h>
36#include <linux/module.h>
37#include <linux/pci.h>
38#include <linux/init.h>
39#include <linux/list.h>
40#include <linux/mm.h>
41#include <linux/highmem.h>
42#include <linux/spinlock.h>
43#include <linux/blkdev.h>
44#include <linux/delay.h>
45#include <linux/timer.h>
46#include <linux/interrupt.h>
47#include <linux/completion.h>
48#include <linux/suspend.h>
49#include <linux/workqueue.h>
Jeff Garzik67846b32005-10-05 02:58:32 -040050#include <linux/jiffies.h>
David Hardeman378f0582005-09-17 17:55:31 +100051#include <linux/scatterlist.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <scsi/scsi.h>
Jeff Garzik193515d2005-11-07 00:59:37 -050053#include <scsi/scsi_cmnd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#include <scsi/scsi_host.h>
55#include <linux/libata.h>
56#include <asm/io.h>
57#include <asm/semaphore.h>
58#include <asm/byteorder.h>
59
60#include "libata.h"
61
Tejun Heod7bb4cc2006-05-31 18:27:46 +090062/* debounce timing parameters in msecs { interval, duration, timeout } */
Tejun Heoe9c83912006-07-03 16:07:26 +090063const unsigned long sata_deb_timing_normal[] = { 5, 100, 2000 };
64const unsigned long sata_deb_timing_hotplug[] = { 25, 500, 2000 };
65const unsigned long sata_deb_timing_long[] = { 100, 2000, 5000 };
Tejun Heod7bb4cc2006-05-31 18:27:46 +090066
Tejun Heo3373efd2006-05-15 20:57:53 +090067static unsigned int ata_dev_init_params(struct ata_device *dev,
68 u16 heads, u16 sectors);
69static unsigned int ata_dev_set_xfermode(struct ata_device *dev);
70static void ata_dev_xfermask(struct ata_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
72static unsigned int ata_unique_id = 1;
73static struct workqueue_struct *ata_wq;
74
Tejun Heo453b07a2006-05-31 18:27:42 +090075struct workqueue_struct *ata_aux_wq;
76
Jeff Garzik418dc1f2006-03-11 20:50:08 -050077int atapi_enabled = 1;
Jeff Garzik1623c812005-08-30 03:37:42 -040078module_param(atapi_enabled, int, 0444);
79MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)");
80
Albert Lee95de7192006-04-04 10:57:18 +080081int atapi_dmadir = 0;
82module_param(atapi_dmadir, int, 0444);
83MODULE_PARM_DESC(atapi_dmadir, "Enable ATAPI DMADIR bridge support (0=off, 1=on)");
84
Jeff Garzikc3c013a2006-02-27 22:31:19 -050085int libata_fua = 0;
86module_param_named(fua, libata_fua, int, 0444);
87MODULE_PARM_DESC(fua, "FUA support (0=off, 1=on)");
88
Andrew Mortona8601e52006-06-25 01:36:52 -070089static int ata_probe_timeout = ATA_TMOUT_INTERNAL / HZ;
90module_param(ata_probe_timeout, int, 0444);
91MODULE_PARM_DESC(ata_probe_timeout, "Set ATA probing timeout (seconds)");
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093MODULE_AUTHOR("Jeff Garzik");
94MODULE_DESCRIPTION("Library module for ATA devices");
95MODULE_LICENSE("GPL");
96MODULE_VERSION(DRV_VERSION);
97
Edward Falk0baab862005-06-02 18:17:13 -040098
99/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
101 * @tf: Taskfile to convert
102 * @fis: Buffer into which data will output
103 * @pmp: Port multiplier port
104 *
105 * Converts a standard ATA taskfile to a Serial ATA
106 * FIS structure (Register - Host to Device).
107 *
108 * LOCKING:
109 * Inherited from caller.
110 */
111
Jeff Garzik057ace52005-10-22 14:27:05 -0400112void ata_tf_to_fis(const struct ata_taskfile *tf, u8 *fis, u8 pmp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
114 fis[0] = 0x27; /* Register - Host to Device FIS */
115 fis[1] = (pmp & 0xf) | (1 << 7); /* Port multiplier number,
116 bit 7 indicates Command FIS */
117 fis[2] = tf->command;
118 fis[3] = tf->feature;
119
120 fis[4] = tf->lbal;
121 fis[5] = tf->lbam;
122 fis[6] = tf->lbah;
123 fis[7] = tf->device;
124
125 fis[8] = tf->hob_lbal;
126 fis[9] = tf->hob_lbam;
127 fis[10] = tf->hob_lbah;
128 fis[11] = tf->hob_feature;
129
130 fis[12] = tf->nsect;
131 fis[13] = tf->hob_nsect;
132 fis[14] = 0;
133 fis[15] = tf->ctl;
134
135 fis[16] = 0;
136 fis[17] = 0;
137 fis[18] = 0;
138 fis[19] = 0;
139}
140
141/**
142 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
143 * @fis: Buffer from which data will be input
144 * @tf: Taskfile to output
145 *
Mark Lorde12a1be2005-11-12 18:55:45 -0500146 * Converts a serial ATA FIS structure to a standard ATA taskfile.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 *
148 * LOCKING:
149 * Inherited from caller.
150 */
151
Jeff Garzik057ace52005-10-22 14:27:05 -0400152void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
154 tf->command = fis[2]; /* status */
155 tf->feature = fis[3]; /* error */
156
157 tf->lbal = fis[4];
158 tf->lbam = fis[5];
159 tf->lbah = fis[6];
160 tf->device = fis[7];
161
162 tf->hob_lbal = fis[8];
163 tf->hob_lbam = fis[9];
164 tf->hob_lbah = fis[10];
165
166 tf->nsect = fis[12];
167 tf->hob_nsect = fis[13];
168}
169
Albert Lee8cbd6df2005-10-12 15:06:27 +0800170static const u8 ata_rw_cmds[] = {
171 /* pio multi */
172 ATA_CMD_READ_MULTI,
173 ATA_CMD_WRITE_MULTI,
174 ATA_CMD_READ_MULTI_EXT,
175 ATA_CMD_WRITE_MULTI_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100176 0,
177 0,
178 0,
179 ATA_CMD_WRITE_MULTI_FUA_EXT,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800180 /* pio */
181 ATA_CMD_PIO_READ,
182 ATA_CMD_PIO_WRITE,
183 ATA_CMD_PIO_READ_EXT,
184 ATA_CMD_PIO_WRITE_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100185 0,
186 0,
187 0,
188 0,
Albert Lee8cbd6df2005-10-12 15:06:27 +0800189 /* dma */
190 ATA_CMD_READ,
191 ATA_CMD_WRITE,
192 ATA_CMD_READ_EXT,
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100193 ATA_CMD_WRITE_EXT,
194 0,
195 0,
196 0,
197 ATA_CMD_WRITE_FUA_EXT
Albert Lee8cbd6df2005-10-12 15:06:27 +0800198};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200/**
Albert Lee8cbd6df2005-10-12 15:06:27 +0800201 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
202 * @qc: command to examine and configure
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 *
Jeff Garzik2e9edbf2006-03-24 09:56:57 -0500204 * Examine the device configuration and tf->flags to calculate
Albert Lee8cbd6df2005-10-12 15:06:27 +0800205 * the proper read/write commands and protocol to use.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 *
207 * LOCKING:
208 * caller.
209 */
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100210int ata_rwcmd_protocol(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
Albert Lee8cbd6df2005-10-12 15:06:27 +0800212 struct ata_taskfile *tf = &qc->tf;
213 struct ata_device *dev = qc->dev;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100214 u8 cmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100216 int index, fua, lba48, write;
Jeff Garzik2e9edbf2006-03-24 09:56:57 -0500217
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100218 fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800219 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
220 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Albert Lee8cbd6df2005-10-12 15:06:27 +0800222 if (dev->flags & ATA_DFLAG_PIO) {
223 tf->protocol = ATA_PROT_PIO;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100224 index = dev->multi_count ? 0 : 8;
Alan Cox8d238e02006-01-17 20:50:31 +0000225 } else if (lba48 && (qc->ap->flags & ATA_FLAG_PIO_LBA48)) {
226 /* Unable to use DMA due to host limitation */
227 tf->protocol = ATA_PROT_PIO;
Albert Lee0565c262006-02-13 18:55:25 +0800228 index = dev->multi_count ? 0 : 8;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800229 } else {
230 tf->protocol = ATA_PROT_DMA;
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100231 index = 16;
Albert Lee8cbd6df2005-10-12 15:06:27 +0800232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Tejun Heo9a3dccc2006-01-06 09:56:18 +0100234 cmd = ata_rw_cmds[index + fua + lba48 + write];
235 if (cmd) {
236 tf->command = cmd;
237 return 0;
238 }
239 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240}
241
Tejun Heocb95d562006-03-06 04:31:56 +0900242/**
Tejun Heo35b649f2006-11-14 22:37:35 +0900243 * ata_tf_read_block - Read block address from ATA taskfile
244 * @tf: ATA taskfile of interest
245 * @dev: ATA device @tf belongs to
246 *
247 * LOCKING:
248 * None.
249 *
250 * Read block address from @tf. This function can handle all
251 * three address formats - LBA, LBA48 and CHS. tf->protocol and
252 * flags select the address format to use.
253 *
254 * RETURNS:
255 * Block address read from @tf.
256 */
257u64 ata_tf_read_block(struct ata_taskfile *tf, struct ata_device *dev)
258{
259 u64 block = 0;
260
261 if (tf->flags & ATA_TFLAG_LBA) {
262 if (tf->flags & ATA_TFLAG_LBA48) {
263 block |= (u64)tf->hob_lbah << 40;
264 block |= (u64)tf->hob_lbam << 32;
265 block |= tf->hob_lbal << 24;
266 } else
267 block |= (tf->device & 0xf) << 24;
268
269 block |= tf->lbah << 16;
270 block |= tf->lbam << 8;
271 block |= tf->lbal;
272 } else {
273 u32 cyl, head, sect;
274
275 cyl = tf->lbam | (tf->lbah << 8);
276 head = tf->device & 0xf;
277 sect = tf->lbal;
278
279 block = (cyl * dev->heads + head) * dev->sectors + sect;
280 }
281
282 return block;
283}
284
285/**
Tejun Heocb95d562006-03-06 04:31:56 +0900286 * ata_pack_xfermask - Pack pio, mwdma and udma masks into xfer_mask
287 * @pio_mask: pio_mask
288 * @mwdma_mask: mwdma_mask
289 * @udma_mask: udma_mask
290 *
291 * Pack @pio_mask, @mwdma_mask and @udma_mask into a single
292 * unsigned int xfer_mask.
293 *
294 * LOCKING:
295 * None.
296 *
297 * RETURNS:
298 * Packed xfer_mask.
299 */
300static unsigned int ata_pack_xfermask(unsigned int pio_mask,
301 unsigned int mwdma_mask,
302 unsigned int udma_mask)
303{
304 return ((pio_mask << ATA_SHIFT_PIO) & ATA_MASK_PIO) |
305 ((mwdma_mask << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA) |
306 ((udma_mask << ATA_SHIFT_UDMA) & ATA_MASK_UDMA);
307}
308
Tejun Heoc0489e42006-03-24 14:07:49 +0900309/**
310 * ata_unpack_xfermask - Unpack xfer_mask into pio, mwdma and udma masks
311 * @xfer_mask: xfer_mask to unpack
312 * @pio_mask: resulting pio_mask
313 * @mwdma_mask: resulting mwdma_mask
314 * @udma_mask: resulting udma_mask
315 *
316 * Unpack @xfer_mask into @pio_mask, @mwdma_mask and @udma_mask.
317 * Any NULL distination masks will be ignored.
318 */
319static void ata_unpack_xfermask(unsigned int xfer_mask,
320 unsigned int *pio_mask,
321 unsigned int *mwdma_mask,
322 unsigned int *udma_mask)
323{
324 if (pio_mask)
325 *pio_mask = (xfer_mask & ATA_MASK_PIO) >> ATA_SHIFT_PIO;
326 if (mwdma_mask)
327 *mwdma_mask = (xfer_mask & ATA_MASK_MWDMA) >> ATA_SHIFT_MWDMA;
328 if (udma_mask)
329 *udma_mask = (xfer_mask & ATA_MASK_UDMA) >> ATA_SHIFT_UDMA;
330}
331
Tejun Heocb95d562006-03-06 04:31:56 +0900332static const struct ata_xfer_ent {
Tejun Heobe9a50c82006-03-31 22:48:52 +0900333 int shift, bits;
Tejun Heocb95d562006-03-06 04:31:56 +0900334 u8 base;
335} ata_xfer_tbl[] = {
336 { ATA_SHIFT_PIO, ATA_BITS_PIO, XFER_PIO_0 },
337 { ATA_SHIFT_MWDMA, ATA_BITS_MWDMA, XFER_MW_DMA_0 },
338 { ATA_SHIFT_UDMA, ATA_BITS_UDMA, XFER_UDMA_0 },
339 { -1, },
340};
341
342/**
343 * ata_xfer_mask2mode - Find matching XFER_* for the given xfer_mask
344 * @xfer_mask: xfer_mask of interest
345 *
346 * Return matching XFER_* value for @xfer_mask. Only the highest
347 * bit of @xfer_mask is considered.
348 *
349 * LOCKING:
350 * None.
351 *
352 * RETURNS:
353 * Matching XFER_* value, 0 if no match found.
354 */
355static u8 ata_xfer_mask2mode(unsigned int xfer_mask)
356{
357 int highbit = fls(xfer_mask) - 1;
358 const struct ata_xfer_ent *ent;
359
360 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
361 if (highbit >= ent->shift && highbit < ent->shift + ent->bits)
362 return ent->base + highbit - ent->shift;
363 return 0;
364}
365
366/**
367 * ata_xfer_mode2mask - Find matching xfer_mask for XFER_*
368 * @xfer_mode: XFER_* of interest
369 *
370 * Return matching xfer_mask for @xfer_mode.
371 *
372 * LOCKING:
373 * None.
374 *
375 * RETURNS:
376 * Matching xfer_mask, 0 if no match found.
377 */
378static unsigned int ata_xfer_mode2mask(u8 xfer_mode)
379{
380 const struct ata_xfer_ent *ent;
381
382 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
383 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
384 return 1 << (ent->shift + xfer_mode - ent->base);
385 return 0;
386}
387
388/**
389 * ata_xfer_mode2shift - Find matching xfer_shift for XFER_*
390 * @xfer_mode: XFER_* of interest
391 *
392 * Return matching xfer_shift for @xfer_mode.
393 *
394 * LOCKING:
395 * None.
396 *
397 * RETURNS:
398 * Matching xfer_shift, -1 if no match found.
399 */
400static int ata_xfer_mode2shift(unsigned int xfer_mode)
401{
402 const struct ata_xfer_ent *ent;
403
404 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
405 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
406 return ent->shift;
407 return -1;
408}
409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410/**
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900411 * ata_mode_string - convert xfer_mask to string
412 * @xfer_mask: mask of bits supported; only highest bit counts.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 *
414 * Determine string which represents the highest speed
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900415 * (highest bit in @modemask).
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 *
417 * LOCKING:
418 * None.
419 *
420 * RETURNS:
421 * Constant C string representing highest speed listed in
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900422 * @mode_mask, or the constant C string "<n/a>".
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 */
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900424static const char *ata_mode_string(unsigned int xfer_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425{
Tejun Heo75f554b2006-03-06 04:31:57 +0900426 static const char * const xfer_mode_str[] = {
427 "PIO0",
428 "PIO1",
429 "PIO2",
430 "PIO3",
431 "PIO4",
Alan Coxb352e572006-08-10 18:52:12 +0100432 "PIO5",
433 "PIO6",
Tejun Heo75f554b2006-03-06 04:31:57 +0900434 "MWDMA0",
435 "MWDMA1",
436 "MWDMA2",
Alan Coxb352e572006-08-10 18:52:12 +0100437 "MWDMA3",
438 "MWDMA4",
Tejun Heo75f554b2006-03-06 04:31:57 +0900439 "UDMA/16",
440 "UDMA/25",
441 "UDMA/33",
442 "UDMA/44",
443 "UDMA/66",
444 "UDMA/100",
445 "UDMA/133",
446 "UDMA7",
447 };
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900448 int highbit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Tejun Heo1da7b0d2006-03-06 04:31:56 +0900450 highbit = fls(xfer_mask) - 1;
451 if (highbit >= 0 && highbit < ARRAY_SIZE(xfer_mode_str))
452 return xfer_mode_str[highbit];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 return "<n/a>";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454}
455
Tejun Heo4c360c82006-04-01 01:38:17 +0900456static const char *sata_spd_string(unsigned int spd)
457{
458 static const char * const spd_str[] = {
459 "1.5 Gbps",
460 "3.0 Gbps",
461 };
462
463 if (spd == 0 || (spd - 1) >= ARRAY_SIZE(spd_str))
464 return "<unknown>";
465 return spd_str[spd - 1];
466}
467
Tejun Heo3373efd2006-05-15 20:57:53 +0900468void ata_dev_disable(struct ata_device *dev)
Tejun Heo0b8efb02006-03-24 15:25:31 +0900469{
Borislav Petkov0dd4b212006-06-23 02:29:08 -0400470 if (ata_dev_enabled(dev) && ata_msg_drv(dev->ap)) {
Tejun Heof15a1da2006-05-15 20:57:56 +0900471 ata_dev_printk(dev, KERN_WARNING, "disabled\n");
Tejun Heo0b8efb02006-03-24 15:25:31 +0900472 dev->class++;
473 }
474}
475
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476/**
477 * ata_pio_devchk - PATA device presence detection
478 * @ap: ATA channel to examine
479 * @device: Device to examine (starting at zero)
480 *
481 * This technique was originally described in
482 * Hale Landis's ATADRVR (www.ata-atapi.com), and
483 * later found its way into the ATA/ATAPI spec.
484 *
485 * Write a pattern to the ATA shadow registers,
486 * and if a device is present, it will respond by
487 * correctly storing and echoing back the
488 * ATA shadow register contents.
489 *
490 * LOCKING:
491 * caller.
492 */
493
494static unsigned int ata_pio_devchk(struct ata_port *ap,
495 unsigned int device)
496{
497 struct ata_ioports *ioaddr = &ap->ioaddr;
498 u8 nsect, lbal;
499
500 ap->ops->dev_select(ap, device);
501
502 outb(0x55, ioaddr->nsect_addr);
503 outb(0xaa, ioaddr->lbal_addr);
504
505 outb(0xaa, ioaddr->nsect_addr);
506 outb(0x55, ioaddr->lbal_addr);
507
508 outb(0x55, ioaddr->nsect_addr);
509 outb(0xaa, ioaddr->lbal_addr);
510
511 nsect = inb(ioaddr->nsect_addr);
512 lbal = inb(ioaddr->lbal_addr);
513
514 if ((nsect == 0x55) && (lbal == 0xaa))
515 return 1; /* we found a device */
516
517 return 0; /* nothing found */
518}
519
520/**
521 * ata_mmio_devchk - PATA device presence detection
522 * @ap: ATA channel to examine
523 * @device: Device to examine (starting at zero)
524 *
525 * This technique was originally described in
526 * Hale Landis's ATADRVR (www.ata-atapi.com), and
527 * later found its way into the ATA/ATAPI spec.
528 *
529 * Write a pattern to the ATA shadow registers,
530 * and if a device is present, it will respond by
531 * correctly storing and echoing back the
532 * ATA shadow register contents.
533 *
534 * LOCKING:
535 * caller.
536 */
537
538static unsigned int ata_mmio_devchk(struct ata_port *ap,
539 unsigned int device)
540{
541 struct ata_ioports *ioaddr = &ap->ioaddr;
542 u8 nsect, lbal;
543
544 ap->ops->dev_select(ap, device);
545
546 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
547 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
548
549 writeb(0xaa, (void __iomem *) ioaddr->nsect_addr);
550 writeb(0x55, (void __iomem *) ioaddr->lbal_addr);
551
552 writeb(0x55, (void __iomem *) ioaddr->nsect_addr);
553 writeb(0xaa, (void __iomem *) ioaddr->lbal_addr);
554
555 nsect = readb((void __iomem *) ioaddr->nsect_addr);
556 lbal = readb((void __iomem *) ioaddr->lbal_addr);
557
558 if ((nsect == 0x55) && (lbal == 0xaa))
559 return 1; /* we found a device */
560
561 return 0; /* nothing found */
562}
563
564/**
565 * ata_devchk - PATA device presence detection
566 * @ap: ATA channel to examine
567 * @device: Device to examine (starting at zero)
568 *
569 * Dispatch ATA device presence detection, depending
570 * on whether we are using PIO or MMIO to talk to the
571 * ATA shadow registers.
572 *
573 * LOCKING:
574 * caller.
575 */
576
577static unsigned int ata_devchk(struct ata_port *ap,
578 unsigned int device)
579{
580 if (ap->flags & ATA_FLAG_MMIO)
581 return ata_mmio_devchk(ap, device);
582 return ata_pio_devchk(ap, device);
583}
584
585/**
586 * ata_dev_classify - determine device type based on ATA-spec signature
587 * @tf: ATA taskfile register set for device to be identified
588 *
589 * Determine from taskfile register contents whether a device is
590 * ATA or ATAPI, as per "Signature and persistence" section
591 * of ATA/PI spec (volume 1, sect 5.14).
592 *
593 * LOCKING:
594 * None.
595 *
596 * RETURNS:
597 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
598 * the event of failure.
599 */
600
Jeff Garzik057ace52005-10-22 14:27:05 -0400601unsigned int ata_dev_classify(const struct ata_taskfile *tf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602{
603 /* Apple's open source Darwin code hints that some devices only
604 * put a proper signature into the LBA mid/high registers,
605 * So, we only check those. It's sufficient for uniqueness.
606 */
607
608 if (((tf->lbam == 0) && (tf->lbah == 0)) ||
609 ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
610 DPRINTK("found ATA device by sig\n");
611 return ATA_DEV_ATA;
612 }
613
614 if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
615 ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
616 DPRINTK("found ATAPI device by sig\n");
617 return ATA_DEV_ATAPI;
618 }
619
620 DPRINTK("unknown device\n");
621 return ATA_DEV_UNKNOWN;
622}
623
624/**
625 * ata_dev_try_classify - Parse returned ATA device signature
626 * @ap: ATA channel to examine
627 * @device: Device to examine (starting at zero)
Tejun Heob4dc7622006-01-24 17:05:22 +0900628 * @r_err: Value of error register on completion
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 *
630 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
631 * an ATA/ATAPI-defined set of values is placed in the ATA
632 * shadow registers, indicating the results of device detection
633 * and diagnostics.
634 *
635 * Select the ATA device, and read the values from the ATA shadow
636 * registers. Then parse according to the Error register value,
637 * and the spec-defined values examined by ata_dev_classify().
638 *
639 * LOCKING:
640 * caller.
Tejun Heob4dc7622006-01-24 17:05:22 +0900641 *
642 * RETURNS:
643 * Device type - %ATA_DEV_ATA, %ATA_DEV_ATAPI or %ATA_DEV_NONE.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 */
645
Tejun Heob4dc7622006-01-24 17:05:22 +0900646static unsigned int
647ata_dev_try_classify(struct ata_port *ap, unsigned int device, u8 *r_err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 struct ata_taskfile tf;
650 unsigned int class;
651 u8 err;
652
653 ap->ops->dev_select(ap, device);
654
655 memset(&tf, 0, sizeof(tf));
656
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 ap->ops->tf_read(ap, &tf);
Jeff Garzik0169e282005-10-29 21:25:10 -0400658 err = tf.feature;
Tejun Heob4dc7622006-01-24 17:05:22 +0900659 if (r_err)
660 *r_err = err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Alan Cox93590852006-09-12 16:55:12 +0100662 /* see if device passed diags: if master then continue and warn later */
663 if (err == 0 && device == 0)
664 /* diagnostic fail : do nothing _YET_ */
665 ap->device[device].horkage |= ATA_HORKAGE_DIAGNOSTIC;
666 else if (err == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 /* do nothing */ ;
668 else if ((device == 0) && (err == 0x81))
669 /* do nothing */ ;
670 else
Tejun Heob4dc7622006-01-24 17:05:22 +0900671 return ATA_DEV_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Tejun Heob4dc7622006-01-24 17:05:22 +0900673 /* determine if device is ATA or ATAPI */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 class = ata_dev_classify(&tf);
Tejun Heob4dc7622006-01-24 17:05:22 +0900675
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 if (class == ATA_DEV_UNKNOWN)
Tejun Heob4dc7622006-01-24 17:05:22 +0900677 return ATA_DEV_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
Tejun Heob4dc7622006-01-24 17:05:22 +0900679 return ATA_DEV_NONE;
680 return class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681}
682
683/**
Tejun Heo6a62a042006-02-13 10:02:46 +0900684 * ata_id_string - Convert IDENTIFY DEVICE page into string
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 * @id: IDENTIFY DEVICE results we will examine
686 * @s: string into which data is output
687 * @ofs: offset into identify device page
688 * @len: length of string to return. must be an even number.
689 *
690 * The strings in the IDENTIFY DEVICE page are broken up into
691 * 16-bit chunks. Run through the string, and output each
692 * 8-bit chunk linearly, regardless of platform.
693 *
694 * LOCKING:
695 * caller.
696 */
697
Tejun Heo6a62a042006-02-13 10:02:46 +0900698void ata_id_string(const u16 *id, unsigned char *s,
699 unsigned int ofs, unsigned int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
701 unsigned int c;
702
703 while (len > 0) {
704 c = id[ofs] >> 8;
705 *s = c;
706 s++;
707
708 c = id[ofs] & 0xff;
709 *s = c;
710 s++;
711
712 ofs++;
713 len -= 2;
714 }
715}
716
Tejun Heo0e949ff2006-02-12 22:47:04 +0900717/**
Tejun Heo6a62a042006-02-13 10:02:46 +0900718 * ata_id_c_string - Convert IDENTIFY DEVICE page into C string
Tejun Heo0e949ff2006-02-12 22:47:04 +0900719 * @id: IDENTIFY DEVICE results we will examine
720 * @s: string into which data is output
721 * @ofs: offset into identify device page
722 * @len: length of string to return. must be an odd number.
723 *
Tejun Heo6a62a042006-02-13 10:02:46 +0900724 * This function is identical to ata_id_string except that it
Tejun Heo0e949ff2006-02-12 22:47:04 +0900725 * trims trailing spaces and terminates the resulting string with
726 * null. @len must be actual maximum length (even number) + 1.
727 *
728 * LOCKING:
729 * caller.
730 */
Tejun Heo6a62a042006-02-13 10:02:46 +0900731void ata_id_c_string(const u16 *id, unsigned char *s,
732 unsigned int ofs, unsigned int len)
Tejun Heo0e949ff2006-02-12 22:47:04 +0900733{
734 unsigned char *p;
735
736 WARN_ON(!(len & 1));
737
Tejun Heo6a62a042006-02-13 10:02:46 +0900738 ata_id_string(id, s, ofs, len - 1);
Tejun Heo0e949ff2006-02-12 22:47:04 +0900739
740 p = s + strnlen(s, len - 1);
741 while (p > s && p[-1] == ' ')
742 p--;
743 *p = '\0';
744}
Edward Falk0baab862005-06-02 18:17:13 -0400745
Tejun Heo29407402006-02-12 22:47:04 +0900746static u64 ata_id_n_sectors(const u16 *id)
747{
748 if (ata_id_has_lba(id)) {
749 if (ata_id_has_lba48(id))
750 return ata_id_u64(id, 100);
751 else
752 return ata_id_u32(id, 60);
753 } else {
754 if (ata_id_current_chs_valid(id))
755 return ata_id_u32(id, 57);
756 else
757 return id[1] * id[3] * id[6];
758 }
759}
760
Edward Falk0baab862005-06-02 18:17:13 -0400761/**
762 * ata_noop_dev_select - Select device 0/1 on ATA bus
763 * @ap: ATA channel to manipulate
764 * @device: ATA device (numbered from zero) to select
765 *
766 * This function performs no actual function.
767 *
768 * May be used as the dev_select() entry in ata_port_operations.
769 *
770 * LOCKING:
771 * caller.
772 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
774{
775}
776
Edward Falk0baab862005-06-02 18:17:13 -0400777
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778/**
779 * ata_std_dev_select - Select device 0/1 on ATA bus
780 * @ap: ATA channel to manipulate
781 * @device: ATA device (numbered from zero) to select
782 *
783 * Use the method defined in the ATA specification to
784 * make either device 0, or device 1, active on the
Edward Falk0baab862005-06-02 18:17:13 -0400785 * ATA channel. Works with both PIO and MMIO.
786 *
787 * May be used as the dev_select() entry in ata_port_operations.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 *
789 * LOCKING:
790 * caller.
791 */
792
793void ata_std_dev_select (struct ata_port *ap, unsigned int device)
794{
795 u8 tmp;
796
797 if (device == 0)
798 tmp = ATA_DEVICE_OBS;
799 else
800 tmp = ATA_DEVICE_OBS | ATA_DEV1;
801
802 if (ap->flags & ATA_FLAG_MMIO) {
803 writeb(tmp, (void __iomem *) ap->ioaddr.device_addr);
804 } else {
805 outb(tmp, ap->ioaddr.device_addr);
806 }
807 ata_pause(ap); /* needed; also flushes, for mmio */
808}
809
810/**
811 * ata_dev_select - Select device 0/1 on ATA bus
812 * @ap: ATA channel to manipulate
813 * @device: ATA device (numbered from zero) to select
814 * @wait: non-zero to wait for Status register BSY bit to clear
815 * @can_sleep: non-zero if context allows sleeping
816 *
817 * Use the method defined in the ATA specification to
818 * make either device 0, or device 1, active on the
819 * ATA channel.
820 *
821 * This is a high-level version of ata_std_dev_select(),
822 * which additionally provides the services of inserting
823 * the proper pauses and status polling, where needed.
824 *
825 * LOCKING:
826 * caller.
827 */
828
829void ata_dev_select(struct ata_port *ap, unsigned int device,
830 unsigned int wait, unsigned int can_sleep)
831{
Tejun Heo88574552006-06-25 20:00:35 +0900832 if (ata_msg_probe(ap))
Borislav Petkov0dd4b212006-06-23 02:29:08 -0400833 ata_port_printk(ap, KERN_INFO, "ata_dev_select: ENTER, ata%u: "
Tejun Heo88574552006-06-25 20:00:35 +0900834 "device %u, wait %u\n", ap->id, device, wait);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835
836 if (wait)
837 ata_wait_idle(ap);
838
839 ap->ops->dev_select(ap, device);
840
841 if (wait) {
842 if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI)
843 msleep(150);
844 ata_wait_idle(ap);
845 }
846}
847
848/**
849 * ata_dump_id - IDENTIFY DEVICE info debugging output
Tejun Heo0bd33002006-02-12 22:47:05 +0900850 * @id: IDENTIFY DEVICE page to dump
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 *
Tejun Heo0bd33002006-02-12 22:47:05 +0900852 * Dump selected 16-bit words from the given IDENTIFY DEVICE
853 * page.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 *
855 * LOCKING:
856 * caller.
857 */
858
Tejun Heo0bd33002006-02-12 22:47:05 +0900859static inline void ata_dump_id(const u16 *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860{
861 DPRINTK("49==0x%04x "
862 "53==0x%04x "
863 "63==0x%04x "
864 "64==0x%04x "
865 "75==0x%04x \n",
Tejun Heo0bd33002006-02-12 22:47:05 +0900866 id[49],
867 id[53],
868 id[63],
869 id[64],
870 id[75]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 DPRINTK("80==0x%04x "
872 "81==0x%04x "
873 "82==0x%04x "
874 "83==0x%04x "
875 "84==0x%04x \n",
Tejun Heo0bd33002006-02-12 22:47:05 +0900876 id[80],
877 id[81],
878 id[82],
879 id[83],
880 id[84]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 DPRINTK("88==0x%04x "
882 "93==0x%04x\n",
Tejun Heo0bd33002006-02-12 22:47:05 +0900883 id[88],
884 id[93]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885}
886
Tejun Heocb95d562006-03-06 04:31:56 +0900887/**
888 * ata_id_xfermask - Compute xfermask from the given IDENTIFY data
889 * @id: IDENTIFY data to compute xfer mask from
890 *
891 * Compute the xfermask for this device. This is not as trivial
892 * as it seems if we must consider early devices correctly.
893 *
894 * FIXME: pre IDE drive timing (do we care ?).
895 *
896 * LOCKING:
897 * None.
898 *
899 * RETURNS:
900 * Computed xfermask
901 */
902static unsigned int ata_id_xfermask(const u16 *id)
903{
904 unsigned int pio_mask, mwdma_mask, udma_mask;
905
906 /* Usual case. Word 53 indicates word 64 is valid */
907 if (id[ATA_ID_FIELD_VALID] & (1 << 1)) {
908 pio_mask = id[ATA_ID_PIO_MODES] & 0x03;
909 pio_mask <<= 3;
910 pio_mask |= 0x7;
911 } else {
912 /* If word 64 isn't valid then Word 51 high byte holds
913 * the PIO timing number for the maximum. Turn it into
914 * a mask.
915 */
Alan Cox46767aeb2006-09-29 18:26:47 +0100916 u8 mode = id[ATA_ID_OLD_PIO_MODES] & 0xFF;
917 if (mode < 5) /* Valid PIO range */
918 pio_mask = (2 << mode) - 1;
919 else
920 pio_mask = 1;
Tejun Heocb95d562006-03-06 04:31:56 +0900921
922 /* But wait.. there's more. Design your standards by
923 * committee and you too can get a free iordy field to
924 * process. However its the speeds not the modes that
925 * are supported... Note drivers using the timing API
926 * will get this right anyway
927 */
928 }
929
930 mwdma_mask = id[ATA_ID_MWDMA_MODES] & 0x07;
Tejun Heofb21f0d2006-03-12 12:34:35 +0900931
Alan Coxb352e572006-08-10 18:52:12 +0100932 if (ata_id_is_cfa(id)) {
933 /*
934 * Process compact flash extended modes
935 */
936 int pio = id[163] & 0x7;
937 int dma = (id[163] >> 3) & 7;
938
939 if (pio)
940 pio_mask |= (1 << 5);
941 if (pio > 1)
942 pio_mask |= (1 << 6);
943 if (dma)
944 mwdma_mask |= (1 << 3);
945 if (dma > 1)
946 mwdma_mask |= (1 << 4);
947 }
948
Tejun Heofb21f0d2006-03-12 12:34:35 +0900949 udma_mask = 0;
950 if (id[ATA_ID_FIELD_VALID] & (1 << 2))
951 udma_mask = id[ATA_ID_UDMA_MODES] & 0xff;
Tejun Heocb95d562006-03-06 04:31:56 +0900952
953 return ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
954}
955
Tejun Heo86e45b62006-03-05 15:29:09 +0900956/**
957 * ata_port_queue_task - Queue port_task
958 * @ap: The ata_port to queue port_task for
Randy Dunlape2a7f772006-05-18 10:50:18 -0700959 * @fn: workqueue function to be scheduled
960 * @data: data value to pass to workqueue function
961 * @delay: delay time for workqueue function
Tejun Heo86e45b62006-03-05 15:29:09 +0900962 *
963 * Schedule @fn(@data) for execution after @delay jiffies using
964 * port_task. There is one port_task per port and it's the
965 * user(low level driver)'s responsibility to make sure that only
966 * one task is active at any given time.
967 *
968 * libata core layer takes care of synchronization between
969 * port_task and EH. ata_port_queue_task() may be ignored for EH
970 * synchronization.
971 *
972 * LOCKING:
973 * Inherited from caller.
974 */
975void ata_port_queue_task(struct ata_port *ap, void (*fn)(void *), void *data,
976 unsigned long delay)
977{
978 int rc;
979
Tejun Heob51e9e52006-06-29 01:29:30 +0900980 if (ap->pflags & ATA_PFLAG_FLUSH_PORT_TASK)
Tejun Heo86e45b62006-03-05 15:29:09 +0900981 return;
982
983 PREPARE_WORK(&ap->port_task, fn, data);
984
985 if (!delay)
986 rc = queue_work(ata_wq, &ap->port_task);
987 else
988 rc = queue_delayed_work(ata_wq, &ap->port_task, delay);
989
990 /* rc == 0 means that another user is using port task */
991 WARN_ON(rc == 0);
992}
993
994/**
995 * ata_port_flush_task - Flush port_task
996 * @ap: The ata_port to flush port_task for
997 *
998 * After this function completes, port_task is guranteed not to
999 * be running or scheduled.
1000 *
1001 * LOCKING:
1002 * Kernel thread context (may sleep)
1003 */
1004void ata_port_flush_task(struct ata_port *ap)
1005{
1006 unsigned long flags;
1007
1008 DPRINTK("ENTER\n");
1009
Jeff Garzikba6a1302006-06-22 23:46:10 -04001010 spin_lock_irqsave(ap->lock, flags);
Tejun Heob51e9e52006-06-29 01:29:30 +09001011 ap->pflags |= ATA_PFLAG_FLUSH_PORT_TASK;
Jeff Garzikba6a1302006-06-22 23:46:10 -04001012 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo86e45b62006-03-05 15:29:09 +09001013
1014 DPRINTK("flush #1\n");
1015 flush_workqueue(ata_wq);
1016
1017 /*
1018 * At this point, if a task is running, it's guaranteed to see
1019 * the FLUSH flag; thus, it will never queue pio tasks again.
1020 * Cancel and flush.
1021 */
1022 if (!cancel_delayed_work(&ap->port_task)) {
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001023 if (ata_msg_ctl(ap))
Tejun Heo88574552006-06-25 20:00:35 +09001024 ata_port_printk(ap, KERN_DEBUG, "%s: flush #2\n",
1025 __FUNCTION__);
Tejun Heo86e45b62006-03-05 15:29:09 +09001026 flush_workqueue(ata_wq);
1027 }
1028
Jeff Garzikba6a1302006-06-22 23:46:10 -04001029 spin_lock_irqsave(ap->lock, flags);
Tejun Heob51e9e52006-06-29 01:29:30 +09001030 ap->pflags &= ~ATA_PFLAG_FLUSH_PORT_TASK;
Jeff Garzikba6a1302006-06-22 23:46:10 -04001031 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo86e45b62006-03-05 15:29:09 +09001032
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001033 if (ata_msg_ctl(ap))
1034 ata_port_printk(ap, KERN_DEBUG, "%s: EXIT\n", __FUNCTION__);
Tejun Heo86e45b62006-03-05 15:29:09 +09001035}
1036
Tejun Heo77853bf2006-01-23 13:09:36 +09001037void ata_qc_complete_internal(struct ata_queued_cmd *qc)
Tejun Heoa2a7a662005-12-13 14:48:31 +09001038{
Tejun Heo77853bf2006-01-23 13:09:36 +09001039 struct completion *waiting = qc->private_data;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001040
Tejun Heoa2a7a662005-12-13 14:48:31 +09001041 complete(waiting);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001042}
1043
1044/**
1045 * ata_exec_internal - execute libata internal command
Tejun Heoa2a7a662005-12-13 14:48:31 +09001046 * @dev: Device to which the command is sent
1047 * @tf: Taskfile registers for the command and the result
Tejun Heod69cf372006-04-02 18:51:53 +09001048 * @cdb: CDB for packet command
Tejun Heoa2a7a662005-12-13 14:48:31 +09001049 * @dma_dir: Data tranfer direction of the command
1050 * @buf: Data buffer of the command
1051 * @buflen: Length of data buffer
1052 *
1053 * Executes libata internal command with timeout. @tf contains
1054 * command on entry and result on return. Timeout and error
1055 * conditions are reported via return value. No recovery action
1056 * is taken after a command times out. It's caller's duty to
1057 * clean up after timeout.
1058 *
1059 * LOCKING:
1060 * None. Should be called with kernel context, might sleep.
Tejun Heo551e8882006-06-12 14:09:49 +09001061 *
1062 * RETURNS:
1063 * Zero on success, AC_ERR_* mask on failure
Tejun Heoa2a7a662005-12-13 14:48:31 +09001064 */
Tejun Heo3373efd2006-05-15 20:57:53 +09001065unsigned ata_exec_internal(struct ata_device *dev,
Tejun Heo1ad8e7f2006-04-02 18:51:53 +09001066 struct ata_taskfile *tf, const u8 *cdb,
1067 int dma_dir, void *buf, unsigned int buflen)
Tejun Heoa2a7a662005-12-13 14:48:31 +09001068{
Tejun Heo3373efd2006-05-15 20:57:53 +09001069 struct ata_port *ap = dev->ap;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001070 u8 command = tf->command;
1071 struct ata_queued_cmd *qc;
Tejun Heo2ab7db12006-05-15 20:58:02 +09001072 unsigned int tag, preempted_tag;
Tejun Heodedaf2b2006-05-15 21:03:43 +09001073 u32 preempted_sactive, preempted_qc_active;
Ingo Molnar60be6b92006-07-03 00:25:26 -07001074 DECLARE_COMPLETION_ONSTACK(wait);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001075 unsigned long flags;
Tejun Heo77853bf2006-01-23 13:09:36 +09001076 unsigned int err_mask;
Tejun Heod95a7172006-05-15 20:58:14 +09001077 int rc;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001078
Jeff Garzikba6a1302006-06-22 23:46:10 -04001079 spin_lock_irqsave(ap->lock, flags);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001080
Tejun Heoe3180492006-05-15 20:58:09 +09001081 /* no internal command while frozen */
Tejun Heob51e9e52006-06-29 01:29:30 +09001082 if (ap->pflags & ATA_PFLAG_FROZEN) {
Jeff Garzikba6a1302006-06-22 23:46:10 -04001083 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoe3180492006-05-15 20:58:09 +09001084 return AC_ERR_SYSTEM;
1085 }
1086
Tejun Heo2ab7db12006-05-15 20:58:02 +09001087 /* initialize internal qc */
Tejun Heoa2a7a662005-12-13 14:48:31 +09001088
Tejun Heo2ab7db12006-05-15 20:58:02 +09001089 /* XXX: Tag 0 is used for drivers with legacy EH as some
1090 * drivers choke if any other tag is given. This breaks
1091 * ata_tag_internal() test for those drivers. Don't use new
1092 * EH stuff without converting to it.
1093 */
1094 if (ap->ops->error_handler)
1095 tag = ATA_TAG_INTERNAL;
1096 else
1097 tag = 0;
1098
Tejun Heo6cec4a32006-05-15 21:03:41 +09001099 if (test_and_set_bit(tag, &ap->qc_allocated))
Tejun Heo2ab7db12006-05-15 20:58:02 +09001100 BUG();
Tejun Heof69499f2006-05-15 20:58:03 +09001101 qc = __ata_qc_from_tag(ap, tag);
Tejun Heo2ab7db12006-05-15 20:58:02 +09001102
1103 qc->tag = tag;
1104 qc->scsicmd = NULL;
1105 qc->ap = ap;
1106 qc->dev = dev;
1107 ata_qc_reinit(qc);
1108
1109 preempted_tag = ap->active_tag;
Tejun Heodedaf2b2006-05-15 21:03:43 +09001110 preempted_sactive = ap->sactive;
1111 preempted_qc_active = ap->qc_active;
Tejun Heo2ab7db12006-05-15 20:58:02 +09001112 ap->active_tag = ATA_TAG_POISON;
Tejun Heodedaf2b2006-05-15 21:03:43 +09001113 ap->sactive = 0;
1114 ap->qc_active = 0;
Tejun Heo2ab7db12006-05-15 20:58:02 +09001115
1116 /* prepare & issue qc */
Tejun Heoa2a7a662005-12-13 14:48:31 +09001117 qc->tf = *tf;
Tejun Heod69cf372006-04-02 18:51:53 +09001118 if (cdb)
1119 memcpy(qc->cdb, cdb, ATAPI_CDB_LEN);
Tejun Heoe61e0672006-05-15 20:57:40 +09001120 qc->flags |= ATA_QCFLAG_RESULT_TF;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001121 qc->dma_dir = dma_dir;
1122 if (dma_dir != DMA_NONE) {
1123 ata_sg_init_one(qc, buf, buflen);
1124 qc->nsect = buflen / ATA_SECT_SIZE;
1125 }
1126
Tejun Heo77853bf2006-01-23 13:09:36 +09001127 qc->private_data = &wait;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001128 qc->complete_fn = ata_qc_complete_internal;
1129
Tejun Heo8e0e6942006-03-31 20:41:11 +09001130 ata_qc_issue(qc);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001131
Jeff Garzikba6a1302006-06-22 23:46:10 -04001132 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001133
Andrew Mortona8601e52006-06-25 01:36:52 -07001134 rc = wait_for_completion_timeout(&wait, ata_probe_timeout);
Albert Lee41ade502006-03-14 11:19:04 +08001135
Tejun Heod95a7172006-05-15 20:58:14 +09001136 ata_port_flush_task(ap);
1137
1138 if (!rc) {
Jeff Garzikba6a1302006-06-22 23:46:10 -04001139 spin_lock_irqsave(ap->lock, flags);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001140
1141 /* We're racing with irq here. If we lose, the
1142 * following test prevents us from completing the qc
Tejun Heod95a7172006-05-15 20:58:14 +09001143 * twice. If we win, the port is frozen and will be
1144 * cleaned up by ->post_internal_cmd().
Tejun Heoa2a7a662005-12-13 14:48:31 +09001145 */
Tejun Heo77853bf2006-01-23 13:09:36 +09001146 if (qc->flags & ATA_QCFLAG_ACTIVE) {
Tejun Heod95a7172006-05-15 20:58:14 +09001147 qc->err_mask |= AC_ERR_TIMEOUT;
1148
1149 if (ap->ops->error_handler)
1150 ata_port_freeze(ap);
1151 else
1152 ata_qc_complete(qc);
Tejun Heof15a1da2006-05-15 20:57:56 +09001153
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001154 if (ata_msg_warn(ap))
1155 ata_dev_printk(dev, KERN_WARNING,
Tejun Heo88574552006-06-25 20:00:35 +09001156 "qc timeout (cmd 0x%x)\n", command);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001157 }
1158
Jeff Garzikba6a1302006-06-22 23:46:10 -04001159 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoa2a7a662005-12-13 14:48:31 +09001160 }
1161
Tejun Heod95a7172006-05-15 20:58:14 +09001162 /* do post_internal_cmd */
1163 if (ap->ops->post_internal_cmd)
1164 ap->ops->post_internal_cmd(qc);
1165
1166 if (qc->flags & ATA_QCFLAG_FAILED && !qc->err_mask) {
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001167 if (ata_msg_warn(ap))
Tejun Heo88574552006-06-25 20:00:35 +09001168 ata_dev_printk(dev, KERN_WARNING,
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001169 "zero err_mask for failed "
Tejun Heo88574552006-06-25 20:00:35 +09001170 "internal command, assuming AC_ERR_OTHER\n");
Tejun Heod95a7172006-05-15 20:58:14 +09001171 qc->err_mask |= AC_ERR_OTHER;
1172 }
1173
Tejun Heo15869302006-05-15 20:57:33 +09001174 /* finish up */
Jeff Garzikba6a1302006-06-22 23:46:10 -04001175 spin_lock_irqsave(ap->lock, flags);
Tejun Heo15869302006-05-15 20:57:33 +09001176
Tejun Heoe61e0672006-05-15 20:57:40 +09001177 *tf = qc->result_tf;
Tejun Heo77853bf2006-01-23 13:09:36 +09001178 err_mask = qc->err_mask;
1179
1180 ata_qc_free(qc);
Tejun Heo2ab7db12006-05-15 20:58:02 +09001181 ap->active_tag = preempted_tag;
Tejun Heodedaf2b2006-05-15 21:03:43 +09001182 ap->sactive = preempted_sactive;
1183 ap->qc_active = preempted_qc_active;
Tejun Heo77853bf2006-01-23 13:09:36 +09001184
Tejun Heo1f7dd3e2006-03-24 15:25:30 +09001185 /* XXX - Some LLDDs (sata_mv) disable port on command failure.
1186 * Until those drivers are fixed, we detect the condition
1187 * here, fail the command with AC_ERR_SYSTEM and reenable the
1188 * port.
1189 *
1190 * Note that this doesn't change any behavior as internal
1191 * command failure results in disabling the device in the
1192 * higher layer for LLDDs without new reset/EH callbacks.
1193 *
1194 * Kill the following code as soon as those drivers are fixed.
1195 */
Tejun Heo198e0fe2006-04-02 18:51:52 +09001196 if (ap->flags & ATA_FLAG_DISABLED) {
Tejun Heo1f7dd3e2006-03-24 15:25:30 +09001197 err_mask |= AC_ERR_SYSTEM;
1198 ata_port_probe(ap);
1199 }
1200
Jeff Garzikba6a1302006-06-22 23:46:10 -04001201 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo15869302006-05-15 20:57:33 +09001202
Tejun Heo77853bf2006-01-23 13:09:36 +09001203 return err_mask;
Tejun Heoa2a7a662005-12-13 14:48:31 +09001204}
1205
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206/**
Tejun Heo977e6b92006-06-24 20:30:19 +09001207 * ata_do_simple_cmd - execute simple internal command
1208 * @dev: Device to which the command is sent
1209 * @cmd: Opcode to execute
1210 *
1211 * Execute a 'simple' command, that only consists of the opcode
1212 * 'cmd' itself, without filling any other registers
1213 *
1214 * LOCKING:
1215 * Kernel thread context (may sleep).
1216 *
1217 * RETURNS:
1218 * Zero on success, AC_ERR_* mask on failure
Tejun Heoe58eb582006-06-24 20:30:19 +09001219 */
Tejun Heo77b08fb2006-06-24 20:30:19 +09001220unsigned int ata_do_simple_cmd(struct ata_device *dev, u8 cmd)
Tejun Heoe58eb582006-06-24 20:30:19 +09001221{
1222 struct ata_taskfile tf;
Tejun Heoe58eb582006-06-24 20:30:19 +09001223
1224 ata_tf_init(dev, &tf);
1225
1226 tf.command = cmd;
1227 tf.flags |= ATA_TFLAG_DEVICE;
1228 tf.protocol = ATA_PROT_NODATA;
1229
Tejun Heo977e6b92006-06-24 20:30:19 +09001230 return ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
Tejun Heoe58eb582006-06-24 20:30:19 +09001231}
1232
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233/**
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001234 * ata_pio_need_iordy - check if iordy needed
1235 * @adev: ATA device
1236 *
1237 * Check if the current speed of the device requires IORDY. Used
1238 * by various controllers for chip configuration.
1239 */
1240
1241unsigned int ata_pio_need_iordy(const struct ata_device *adev)
1242{
1243 int pio;
1244 int speed = adev->pio_mode - XFER_PIO_0;
1245
1246 if (speed < 2)
1247 return 0;
1248 if (speed > 2)
1249 return 1;
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001250
Alan Cox1bc4ccf2006-01-09 17:18:14 +00001251 /* If we have no drive specific rule, then PIO 2 is non IORDY */
1252
1253 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
1254 pio = adev->id[ATA_ID_EIDE_PIO];
1255 /* Is the speed faster than the drive allows non IORDY ? */
1256 if (pio) {
1257 /* This is cycle times not frequency - watch the logic! */
1258 if (pio > 240) /* PIO2 is 240nS per cycle */
1259 return 1;
1260 return 0;
1261 }
1262 }
1263 return 0;
1264}
1265
1266/**
Tejun Heo49016ac2006-02-21 02:12:11 +09001267 * ata_dev_read_id - Read ID data from the specified device
Tejun Heo49016ac2006-02-21 02:12:11 +09001268 * @dev: target device
1269 * @p_class: pointer to class of the target device (may be changed)
Tejun Heobff04642006-11-10 18:08:10 +09001270 * @flags: ATA_READID_* flags
Tejun Heofe635c72006-05-15 20:57:35 +09001271 * @id: buffer to read IDENTIFY data into
Tejun Heo49016ac2006-02-21 02:12:11 +09001272 *
1273 * Read ID data from the specified device. ATA_CMD_ID_ATA is
1274 * performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
Tejun Heoaec5c3c2006-03-25 01:33:34 +09001275 * devices. This function also issues ATA_CMD_INIT_DEV_PARAMS
1276 * for pre-ATA4 drives.
Tejun Heo49016ac2006-02-21 02:12:11 +09001277 *
1278 * LOCKING:
1279 * Kernel thread context (may sleep)
1280 *
1281 * RETURNS:
1282 * 0 on success, -errno otherwise.
1283 */
Tejun Heoa9beec92006-05-31 18:27:44 +09001284int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
Tejun Heobff04642006-11-10 18:08:10 +09001285 unsigned int flags, u16 *id)
Tejun Heo49016ac2006-02-21 02:12:11 +09001286{
Tejun Heo3373efd2006-05-15 20:57:53 +09001287 struct ata_port *ap = dev->ap;
Tejun Heo49016ac2006-02-21 02:12:11 +09001288 unsigned int class = *p_class;
Tejun Heo49016ac2006-02-21 02:12:11 +09001289 struct ata_taskfile tf;
1290 unsigned int err_mask = 0;
1291 const char *reason;
1292 int rc;
1293
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001294 if (ata_msg_ctl(ap))
Tejun Heo88574552006-06-25 20:00:35 +09001295 ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER, host %u, dev %u\n",
1296 __FUNCTION__, ap->id, dev->devno);
Tejun Heo49016ac2006-02-21 02:12:11 +09001297
Tejun Heo49016ac2006-02-21 02:12:11 +09001298 ata_dev_select(ap, dev->devno, 1, 1); /* select device 0/1 */
1299
1300 retry:
Tejun Heo3373efd2006-05-15 20:57:53 +09001301 ata_tf_init(dev, &tf);
Tejun Heo49016ac2006-02-21 02:12:11 +09001302
1303 switch (class) {
1304 case ATA_DEV_ATA:
1305 tf.command = ATA_CMD_ID_ATA;
1306 break;
1307 case ATA_DEV_ATAPI:
1308 tf.command = ATA_CMD_ID_ATAPI;
1309 break;
1310 default:
1311 rc = -ENODEV;
1312 reason = "unsupported class";
1313 goto err_out;
1314 }
1315
1316 tf.protocol = ATA_PROT_PIO;
1317
Tejun Heo55a8e2c2006-11-10 18:08:10 +09001318 /* presence detection using polling IDENTIFY? */
1319 if (flags & ATA_READID_DETECT)
1320 tf.flags |= ATA_TFLAG_POLLING;
1321
Tejun Heo3373efd2006-05-15 20:57:53 +09001322 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
Tejun Heo49016ac2006-02-21 02:12:11 +09001323 id, sizeof(id[0]) * ATA_ID_WORDS);
Tejun Heo49016ac2006-02-21 02:12:11 +09001324 if (err_mask) {
Tejun Heo55a8e2c2006-11-10 18:08:10 +09001325 if ((flags & ATA_READID_DETECT) &&
1326 (err_mask & AC_ERR_NODEV_HINT)) {
1327 DPRINTK("ata%u.%d: NODEV after polling detection\n",
1328 ap->id, dev->devno);
1329 return -ENOENT;
1330 }
1331
Tejun Heo49016ac2006-02-21 02:12:11 +09001332 rc = -EIO;
1333 reason = "I/O error";
Tejun Heo49016ac2006-02-21 02:12:11 +09001334 goto err_out;
1335 }
1336
1337 swap_buf_le16(id, ATA_ID_WORDS);
1338
Tejun Heo49016ac2006-02-21 02:12:11 +09001339 /* sanity check */
Tejun Heoa4f57492006-09-12 20:35:49 -07001340 rc = -EINVAL;
1341 reason = "device reports illegal type";
1342
1343 if (class == ATA_DEV_ATA) {
1344 if (!ata_id_is_ata(id) && !ata_id_is_cfa(id))
1345 goto err_out;
1346 } else {
1347 if (ata_id_is_ata(id))
1348 goto err_out;
Tejun Heo49016ac2006-02-21 02:12:11 +09001349 }
1350
Tejun Heobff04642006-11-10 18:08:10 +09001351 if ((flags & ATA_READID_POSTRESET) && class == ATA_DEV_ATA) {
Tejun Heo49016ac2006-02-21 02:12:11 +09001352 /*
1353 * The exact sequence expected by certain pre-ATA4 drives is:
1354 * SRST RESET
1355 * IDENTIFY
1356 * INITIALIZE DEVICE PARAMETERS
1357 * anything else..
1358 * Some drives were very specific about that exact sequence.
1359 */
1360 if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
Tejun Heo3373efd2006-05-15 20:57:53 +09001361 err_mask = ata_dev_init_params(dev, id[3], id[6]);
Tejun Heo49016ac2006-02-21 02:12:11 +09001362 if (err_mask) {
1363 rc = -EIO;
1364 reason = "INIT_DEV_PARAMS failed";
1365 goto err_out;
1366 }
1367
1368 /* current CHS translation info (id[53-58]) might be
1369 * changed. reread the identify device info.
1370 */
Tejun Heobff04642006-11-10 18:08:10 +09001371 flags &= ~ATA_READID_POSTRESET;
Tejun Heo49016ac2006-02-21 02:12:11 +09001372 goto retry;
1373 }
1374 }
1375
1376 *p_class = class;
Tejun Heofe635c72006-05-15 20:57:35 +09001377
Tejun Heo49016ac2006-02-21 02:12:11 +09001378 return 0;
1379
1380 err_out:
Tejun Heo88574552006-06-25 20:00:35 +09001381 if (ata_msg_warn(ap))
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001382 ata_dev_printk(dev, KERN_WARNING, "failed to IDENTIFY "
Tejun Heo88574552006-06-25 20:00:35 +09001383 "(%s, err_mask=0x%x)\n", reason, err_mask);
Tejun Heo49016ac2006-02-21 02:12:11 +09001384 return rc;
1385}
1386
Tejun Heo3373efd2006-05-15 20:57:53 +09001387static inline u8 ata_dev_knobble(struct ata_device *dev)
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09001388{
Tejun Heo3373efd2006-05-15 20:57:53 +09001389 return ((dev->ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09001390}
1391
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001392static void ata_dev_config_ncq(struct ata_device *dev,
1393 char *desc, size_t desc_sz)
1394{
1395 struct ata_port *ap = dev->ap;
1396 int hdepth = 0, ddepth = ata_id_queue_depth(dev->id);
1397
1398 if (!ata_id_has_ncq(dev->id)) {
1399 desc[0] = '\0';
1400 return;
1401 }
Alan Cox6919a0a2006-10-27 19:08:46 -07001402 if (ata_device_blacklisted(dev) & ATA_HORKAGE_NONCQ) {
1403 snprintf(desc, desc_sz, "NCQ (not used)");
1404 return;
1405 }
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001406 if (ap->flags & ATA_FLAG_NCQ) {
Jeff Garzikcca39742006-08-24 03:19:22 -04001407 hdepth = min(ap->scsi_host->can_queue, ATA_MAX_QUEUE - 1);
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001408 dev->flags |= ATA_DFLAG_NCQ;
1409 }
1410
1411 if (hdepth >= ddepth)
1412 snprintf(desc, desc_sz, "NCQ (depth %d)", ddepth);
1413 else
1414 snprintf(desc, desc_sz, "NCQ (depth %d/%d)", hdepth, ddepth);
1415}
1416
Brian Kinge6d902a2006-06-28 08:30:31 -05001417static void ata_set_port_max_cmd_len(struct ata_port *ap)
1418{
1419 int i;
1420
Jeff Garzikcca39742006-08-24 03:19:22 -04001421 if (ap->scsi_host) {
1422 unsigned int len = 0;
1423
Brian Kinge6d902a2006-06-28 08:30:31 -05001424 for (i = 0; i < ATA_MAX_DEVICES; i++)
Jeff Garzikcca39742006-08-24 03:19:22 -04001425 len = max(len, ap->device[i].cdb_len);
1426
1427 ap->scsi_host->max_cmd_len = len;
Brian Kinge6d902a2006-06-28 08:30:31 -05001428 }
1429}
1430
Tejun Heo49016ac2006-02-21 02:12:11 +09001431/**
Tejun Heoffeae412006-03-01 16:09:35 +09001432 * ata_dev_configure - Configure the specified ATA/ATAPI device
Tejun Heoffeae412006-03-01 16:09:35 +09001433 * @dev: Target device to configure
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 *
Tejun Heoffeae412006-03-01 16:09:35 +09001435 * Configure @dev according to @dev->id. Generic and low-level
1436 * driver specific fixups are also applied.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437 *
1438 * LOCKING:
Tejun Heoffeae412006-03-01 16:09:35 +09001439 * Kernel thread context (may sleep)
1440 *
1441 * RETURNS:
1442 * 0 on success, -errno otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 */
Tejun Heoefdaedc2006-11-01 18:38:52 +09001444int ata_dev_configure(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445{
Tejun Heo3373efd2006-05-15 20:57:53 +09001446 struct ata_port *ap = dev->ap;
Tejun Heoefdaedc2006-11-01 18:38:52 +09001447 int print_info = ap->eh_context.i.flags & ATA_EHI_PRINTINFO;
Tejun Heo1148c3a2006-03-13 19:48:04 +09001448 const u16 *id = dev->id;
Tejun Heoff8854b2006-03-06 04:31:56 +09001449 unsigned int xfer_mask;
Alan Coxb352e572006-08-10 18:52:12 +01001450 char revbuf[7]; /* XYZ-99\0 */
Brian Kinge6d902a2006-06-28 08:30:31 -05001451 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001453 if (!ata_dev_enabled(dev) && ata_msg_info(ap)) {
Tejun Heo88574552006-06-25 20:00:35 +09001454 ata_dev_printk(dev, KERN_INFO,
1455 "%s: ENTER/EXIT (host %u, dev %u) -- nodev\n",
1456 __FUNCTION__, ap->id, dev->devno);
Tejun Heoffeae412006-03-01 16:09:35 +09001457 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 }
1459
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001460 if (ata_msg_probe(ap))
Tejun Heo88574552006-06-25 20:00:35 +09001461 ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER, host %u, dev %u\n",
1462 __FUNCTION__, ap->id, dev->devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
Tejun Heoc39f5eb2006-03-13 19:51:19 +09001464 /* print device capabilities */
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001465 if (ata_msg_probe(ap))
Tejun Heo88574552006-06-25 20:00:35 +09001466 ata_dev_printk(dev, KERN_DEBUG,
1467 "%s: cfg 49:%04x 82:%04x 83:%04x 84:%04x "
1468 "85:%04x 86:%04x 87:%04x 88:%04x\n",
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001469 __FUNCTION__,
Tejun Heof15a1da2006-05-15 20:57:56 +09001470 id[49], id[82], id[83], id[84],
1471 id[85], id[86], id[87], id[88]);
Tejun Heoc39f5eb2006-03-13 19:51:19 +09001472
Tejun Heo208a9932006-03-05 17:55:58 +09001473 /* initialize to-be-configured parameters */
Tejun Heoea1dd4e2006-04-02 18:51:53 +09001474 dev->flags &= ~ATA_DFLAG_CFG_MASK;
Tejun Heo208a9932006-03-05 17:55:58 +09001475 dev->max_sectors = 0;
1476 dev->cdb_len = 0;
1477 dev->n_sectors = 0;
1478 dev->cylinders = 0;
1479 dev->heads = 0;
1480 dev->sectors = 0;
1481
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482 /*
1483 * common ATA, ATAPI feature tests
1484 */
1485
Tejun Heoff8854b2006-03-06 04:31:56 +09001486 /* find max transfer mode; for printk only */
Tejun Heo1148c3a2006-03-13 19:48:04 +09001487 xfer_mask = ata_id_xfermask(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001489 if (ata_msg_probe(ap))
1490 ata_dump_id(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
1492 /* ATA-specific feature tests */
1493 if (dev->class == ATA_DEV_ATA) {
Alan Coxb352e572006-08-10 18:52:12 +01001494 if (ata_id_is_cfa(id)) {
1495 if (id[162] & 1) /* CPRM may make this media unusable */
1496 ata_dev_printk(dev, KERN_WARNING, "ata%u: device %u supports DRM functions and may not be fully accessable.\n",
1497 ap->id, dev->devno);
1498 snprintf(revbuf, 7, "CFA");
1499 }
1500 else
1501 snprintf(revbuf, 7, "ATA-%d", ata_id_major_version(id));
1502
Tejun Heo1148c3a2006-03-13 19:48:04 +09001503 dev->n_sectors = ata_id_n_sectors(id);
Tejun Heo29407402006-02-12 22:47:04 +09001504
Tejun Heo1148c3a2006-03-13 19:48:04 +09001505 if (ata_id_has_lba(id)) {
Tejun Heo4c2d7212006-03-05 17:55:58 +09001506 const char *lba_desc;
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001507 char ncq_desc[20];
Albert Lee8bf62ec2005-05-12 15:29:42 -04001508
Tejun Heo4c2d7212006-03-05 17:55:58 +09001509 lba_desc = "LBA";
1510 dev->flags |= ATA_DFLAG_LBA;
Tejun Heo1148c3a2006-03-13 19:48:04 +09001511 if (ata_id_has_lba48(id)) {
Albert Lee8bf62ec2005-05-12 15:29:42 -04001512 dev->flags |= ATA_DFLAG_LBA48;
Tejun Heo4c2d7212006-03-05 17:55:58 +09001513 lba_desc = "LBA48";
Tejun Heo6fc49ad2006-11-11 20:10:45 +09001514
1515 if (dev->n_sectors >= (1UL << 28) &&
1516 ata_id_has_flush_ext(id))
1517 dev->flags |= ATA_DFLAG_FLUSH_EXT;
Tejun Heo4c2d7212006-03-05 17:55:58 +09001518 }
Albert Lee8bf62ec2005-05-12 15:29:42 -04001519
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001520 /* config NCQ */
1521 ata_dev_config_ncq(dev, ncq_desc, sizeof(ncq_desc));
1522
Albert Lee8bf62ec2005-05-12 15:29:42 -04001523 /* print device info to dmesg */
Borislav Petkov5afc8142006-06-27 14:51:25 +02001524 if (ata_msg_drv(ap) && print_info)
Alan Coxb352e572006-08-10 18:52:12 +01001525 ata_dev_printk(dev, KERN_INFO, "%s, "
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001526 "max %s, %Lu sectors: %s %s\n",
Alan Coxb352e572006-08-10 18:52:12 +01001527 revbuf,
Tejun Heof15a1da2006-05-15 20:57:56 +09001528 ata_mode_string(xfer_mask),
1529 (unsigned long long)dev->n_sectors,
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001530 lba_desc, ncq_desc);
Tejun Heoffeae412006-03-01 16:09:35 +09001531 } else {
Albert Lee8bf62ec2005-05-12 15:29:42 -04001532 /* CHS */
1533
1534 /* Default translation */
Tejun Heo1148c3a2006-03-13 19:48:04 +09001535 dev->cylinders = id[1];
1536 dev->heads = id[3];
1537 dev->sectors = id[6];
Albert Lee8bf62ec2005-05-12 15:29:42 -04001538
Tejun Heo1148c3a2006-03-13 19:48:04 +09001539 if (ata_id_current_chs_valid(id)) {
Albert Lee8bf62ec2005-05-12 15:29:42 -04001540 /* Current CHS translation is valid. */
Tejun Heo1148c3a2006-03-13 19:48:04 +09001541 dev->cylinders = id[54];
1542 dev->heads = id[55];
1543 dev->sectors = id[56];
Albert Lee8bf62ec2005-05-12 15:29:42 -04001544 }
1545
1546 /* print device info to dmesg */
Borislav Petkov5afc8142006-06-27 14:51:25 +02001547 if (ata_msg_drv(ap) && print_info)
Alan Coxb352e572006-08-10 18:52:12 +01001548 ata_dev_printk(dev, KERN_INFO, "%s, "
Tejun Heof15a1da2006-05-15 20:57:56 +09001549 "max %s, %Lu sectors: CHS %u/%u/%u\n",
Alan Coxb352e572006-08-10 18:52:12 +01001550 revbuf,
Tejun Heof15a1da2006-05-15 20:57:56 +09001551 ata_mode_string(xfer_mask),
1552 (unsigned long long)dev->n_sectors,
Tejun Heo88574552006-06-25 20:00:35 +09001553 dev->cylinders, dev->heads,
1554 dev->sectors);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 }
1556
Albert Lee07f6f7d2005-11-01 19:33:20 +08001557 if (dev->id[59] & 0x100) {
1558 dev->multi_count = dev->id[59] & 0xff;
Borislav Petkov5afc8142006-06-27 14:51:25 +02001559 if (ata_msg_drv(ap) && print_info)
Tejun Heo88574552006-06-25 20:00:35 +09001560 ata_dev_printk(dev, KERN_INFO,
1561 "ata%u: dev %u multi count %u\n",
1562 ap->id, dev->devno, dev->multi_count);
Albert Lee07f6f7d2005-11-01 19:33:20 +08001563 }
1564
Tejun Heo6e7846e2006-02-12 23:32:58 +09001565 dev->cdb_len = 16;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 }
1567
1568 /* ATAPI-specific feature tests */
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05001569 else if (dev->class == ATA_DEV_ATAPI) {
Albert Lee08a556d2006-03-31 13:29:04 +08001570 char *cdb_intr_string = "";
1571
Tejun Heo1148c3a2006-03-13 19:48:04 +09001572 rc = atapi_cdb_len(id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001574 if (ata_msg_warn(ap))
Tejun Heo88574552006-06-25 20:00:35 +09001575 ata_dev_printk(dev, KERN_WARNING,
1576 "unsupported CDB len\n");
Tejun Heoffeae412006-03-01 16:09:35 +09001577 rc = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 goto err_out_nosup;
1579 }
Tejun Heo6e7846e2006-02-12 23:32:58 +09001580 dev->cdb_len = (unsigned int) rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
Albert Lee08a556d2006-03-31 13:29:04 +08001582 if (ata_id_cdb_intr(dev->id)) {
Albert Lee312f7da2005-09-27 17:38:03 +08001583 dev->flags |= ATA_DFLAG_CDB_INTR;
Albert Lee08a556d2006-03-31 13:29:04 +08001584 cdb_intr_string = ", CDB intr";
1585 }
Albert Lee312f7da2005-09-27 17:38:03 +08001586
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 /* print device info to dmesg */
Borislav Petkov5afc8142006-06-27 14:51:25 +02001588 if (ata_msg_drv(ap) && print_info)
Tejun Heo12436c32006-05-15 20:59:15 +09001589 ata_dev_printk(dev, KERN_INFO, "ATAPI, max %s%s\n",
1590 ata_mode_string(xfer_mask),
1591 cdb_intr_string);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 }
1593
Tejun Heo914ed352006-11-01 18:39:55 +09001594 /* determine max_sectors */
1595 dev->max_sectors = ATA_MAX_SECTORS;
1596 if (dev->flags & ATA_DFLAG_LBA48)
1597 dev->max_sectors = ATA_MAX_SECTORS_LBA48;
1598
Alan Cox93590852006-09-12 16:55:12 +01001599 if (dev->horkage & ATA_HORKAGE_DIAGNOSTIC) {
1600 /* Let the user know. We don't want to disallow opens for
1601 rescue purposes, or in case the vendor is just a blithering
1602 idiot */
1603 if (print_info) {
1604 ata_dev_printk(dev, KERN_WARNING,
1605"Drive reports diagnostics failure. This may indicate a drive\n");
1606 ata_dev_printk(dev, KERN_WARNING,
1607"fault or invalid emulation. Contact drive vendor for information.\n");
1608 }
1609 }
1610
Brian Kinge6d902a2006-06-28 08:30:31 -05001611 ata_set_port_max_cmd_len(ap);
Tejun Heo6e7846e2006-02-12 23:32:58 +09001612
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09001613 /* limit bridge transfers to udma5, 200 sectors */
Tejun Heo3373efd2006-05-15 20:57:53 +09001614 if (ata_dev_knobble(dev)) {
Borislav Petkov5afc8142006-06-27 14:51:25 +02001615 if (ata_msg_drv(ap) && print_info)
Tejun Heof15a1da2006-05-15 20:57:56 +09001616 ata_dev_printk(dev, KERN_INFO,
1617 "applying bridge limits\n");
Tejun Heo5a529132006-03-24 14:07:50 +09001618 dev->udma_mask &= ATA_UDMA5;
Tejun Heo4b2f3ed2006-03-01 16:09:36 +09001619 dev->max_sectors = ATA_MAX_SECTORS;
1620 }
1621
1622 if (ap->ops->dev_config)
1623 ap->ops->dev_config(ap, dev);
1624
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001625 if (ata_msg_probe(ap))
1626 ata_dev_printk(dev, KERN_DEBUG, "%s: EXIT, drv_stat = 0x%x\n",
1627 __FUNCTION__, ata_chk_status(ap));
Tejun Heoffeae412006-03-01 16:09:35 +09001628 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629
1630err_out_nosup:
Borislav Petkov0dd4b212006-06-23 02:29:08 -04001631 if (ata_msg_probe(ap))
Tejun Heo88574552006-06-25 20:00:35 +09001632 ata_dev_printk(dev, KERN_DEBUG,
1633 "%s: EXIT, err\n", __FUNCTION__);
Tejun Heoffeae412006-03-01 16:09:35 +09001634 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635}
1636
1637/**
1638 * ata_bus_probe - Reset and probe ATA bus
1639 * @ap: Bus to probe
1640 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001641 * Master ATA bus probing function. Initiates a hardware-dependent
1642 * bus reset, then attempts to identify any devices found on
1643 * the bus.
1644 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001646 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 *
1648 * RETURNS:
Tejun Heo96072e62006-04-01 01:38:17 +09001649 * Zero on success, negative errno otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 */
1651
Brian King80289162006-08-07 14:27:31 -05001652int ata_bus_probe(struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653{
Tejun Heo28ca5c52006-03-01 16:09:36 +09001654 unsigned int classes[ATA_MAX_DEVICES];
Tejun Heo14d2bac2006-04-02 17:54:46 +09001655 int tries[ATA_MAX_DEVICES];
1656 int i, rc, down_xfermask;
Tejun Heoe82cbdb2006-04-01 01:38:18 +09001657 struct ata_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
Tejun Heo28ca5c52006-03-01 16:09:36 +09001659 ata_port_probe(ap);
1660
Tejun Heo14d2bac2006-04-02 17:54:46 +09001661 for (i = 0; i < ATA_MAX_DEVICES; i++)
1662 tries[i] = ATA_PROBE_MAX_TRIES;
1663
1664 retry:
1665 down_xfermask = 0;
1666
Tejun Heo20444702006-03-13 01:57:01 +09001667 /* reset and determine device classes */
Tejun Heo52783c52006-05-31 18:28:22 +09001668 ap->ops->phy_reset(ap);
Tejun Heo2061a472006-03-12 00:57:39 +09001669
Tejun Heo52783c52006-05-31 18:28:22 +09001670 for (i = 0; i < ATA_MAX_DEVICES; i++) {
1671 dev = &ap->device[i];
Tejun Heoc19ba8a2006-01-24 17:05:22 +09001672
Tejun Heo52783c52006-05-31 18:28:22 +09001673 if (!(ap->flags & ATA_FLAG_DISABLED) &&
1674 dev->class != ATA_DEV_UNKNOWN)
1675 classes[dev->devno] = dev->class;
1676 else
1677 classes[dev->devno] = ATA_DEV_NONE;
Tejun Heo20444702006-03-13 01:57:01 +09001678
Tejun Heo52783c52006-05-31 18:28:22 +09001679 dev->class = ATA_DEV_UNKNOWN;
Tejun Heo28ca5c52006-03-01 16:09:36 +09001680 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
Tejun Heo52783c52006-05-31 18:28:22 +09001682 ata_port_probe(ap);
Tejun Heo20444702006-03-13 01:57:01 +09001683
Alan Coxb6079ca2006-05-22 16:52:06 +01001684 /* after the reset the device state is PIO 0 and the controller
1685 state is undefined. Record the mode */
1686
1687 for (i = 0; i < ATA_MAX_DEVICES; i++)
1688 ap->device[i].pio_mode = XFER_PIO_0;
1689
Tejun Heo28ca5c52006-03-01 16:09:36 +09001690 /* read IDENTIFY page and configure devices */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 for (i = 0; i < ATA_MAX_DEVICES; i++) {
Tejun Heoe82cbdb2006-04-01 01:38:18 +09001692 dev = &ap->device[i];
Tejun Heo28ca5c52006-03-01 16:09:36 +09001693
Tejun Heoec573752006-04-11 22:26:29 +09001694 if (tries[i])
1695 dev->class = classes[i];
Tejun Heo14d2bac2006-04-02 17:54:46 +09001696
Tejun Heoe1211e32006-04-01 01:38:18 +09001697 if (!ata_dev_enabled(dev))
Tejun Heoffeae412006-03-01 16:09:35 +09001698 continue;
1699
Tejun Heobff04642006-11-10 18:08:10 +09001700 rc = ata_dev_read_id(dev, &dev->class, ATA_READID_POSTRESET,
1701 dev->id);
Tejun Heo14d2bac2006-04-02 17:54:46 +09001702 if (rc)
1703 goto fail;
Tejun Heoffeae412006-03-01 16:09:35 +09001704
Tejun Heoefdaedc2006-11-01 18:38:52 +09001705 ap->eh_context.i.flags |= ATA_EHI_PRINTINFO;
1706 rc = ata_dev_configure(dev);
1707 ap->eh_context.i.flags &= ~ATA_EHI_PRINTINFO;
Tejun Heo14d2bac2006-04-02 17:54:46 +09001708 if (rc)
1709 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 }
1711
Tejun Heoe82cbdb2006-04-01 01:38:18 +09001712 /* configure transfer mode */
Tejun Heo3adcebb2006-05-15 20:57:37 +09001713 rc = ata_set_mode(ap, &dev);
Tejun Heo51713d32006-04-11 22:26:29 +09001714 if (rc) {
1715 down_xfermask = 1;
1716 goto fail;
Tejun Heoe82cbdb2006-04-01 01:38:18 +09001717 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718
Tejun Heoe82cbdb2006-04-01 01:38:18 +09001719 for (i = 0; i < ATA_MAX_DEVICES; i++)
1720 if (ata_dev_enabled(&ap->device[i]))
1721 return 0;
Alan Coxe35a9e02006-03-27 18:46:37 +01001722
Tejun Heoe82cbdb2006-04-01 01:38:18 +09001723 /* no device present, disable port */
1724 ata_port_disable(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 ap->ops->port_disable(ap);
Tejun Heo96072e62006-04-01 01:38:17 +09001726 return -ENODEV;
Tejun Heo14d2bac2006-04-02 17:54:46 +09001727
1728 fail:
1729 switch (rc) {
1730 case -EINVAL:
1731 case -ENODEV:
1732 tries[dev->devno] = 0;
1733 break;
1734 case -EIO:
Tejun Heo3c567b72006-05-15 20:57:23 +09001735 sata_down_spd_limit(ap);
Tejun Heo14d2bac2006-04-02 17:54:46 +09001736 /* fall through */
1737 default:
1738 tries[dev->devno]--;
1739 if (down_xfermask &&
Tejun Heo3373efd2006-05-15 20:57:53 +09001740 ata_down_xfermask_limit(dev, tries[dev->devno] == 1))
Tejun Heo14d2bac2006-04-02 17:54:46 +09001741 tries[dev->devno] = 0;
1742 }
1743
Tejun Heoec573752006-04-11 22:26:29 +09001744 if (!tries[dev->devno]) {
Tejun Heo3373efd2006-05-15 20:57:53 +09001745 ata_down_xfermask_limit(dev, 1);
1746 ata_dev_disable(dev);
Tejun Heoec573752006-04-11 22:26:29 +09001747 }
1748
Tejun Heo14d2bac2006-04-02 17:54:46 +09001749 goto retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750}
1751
1752/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001753 * ata_port_probe - Mark port as enabled
1754 * @ap: Port for which we indicate enablement
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04001756 * Modify @ap data structure such that the system
1757 * thinks that the entire port is enabled.
1758 *
Jeff Garzikcca39742006-08-24 03:19:22 -04001759 * LOCKING: host lock, or some other form of
Jeff Garzik0cba6322005-05-30 19:49:12 -04001760 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 */
1762
1763void ata_port_probe(struct ata_port *ap)
1764{
Tejun Heo198e0fe2006-04-02 18:51:52 +09001765 ap->flags &= ~ATA_FLAG_DISABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766}
1767
1768/**
Tejun Heo3be680b2005-12-19 22:35:02 +09001769 * sata_print_link_status - Print SATA link status
1770 * @ap: SATA port to printk link status about
1771 *
1772 * This function prints link speed and status of a SATA link.
1773 *
1774 * LOCKING:
1775 * None.
1776 */
1777static void sata_print_link_status(struct ata_port *ap)
1778{
Tejun Heo6d5f9732006-04-03 00:09:41 +09001779 u32 sstatus, scontrol, tmp;
Tejun Heo3be680b2005-12-19 22:35:02 +09001780
Tejun Heo81952c52006-05-15 20:57:47 +09001781 if (sata_scr_read(ap, SCR_STATUS, &sstatus))
Tejun Heo3be680b2005-12-19 22:35:02 +09001782 return;
Tejun Heo81952c52006-05-15 20:57:47 +09001783 sata_scr_read(ap, SCR_CONTROL, &scontrol);
Tejun Heo3be680b2005-12-19 22:35:02 +09001784
Tejun Heo81952c52006-05-15 20:57:47 +09001785 if (ata_port_online(ap)) {
Tejun Heo3be680b2005-12-19 22:35:02 +09001786 tmp = (sstatus >> 4) & 0xf;
Tejun Heof15a1da2006-05-15 20:57:56 +09001787 ata_port_printk(ap, KERN_INFO,
1788 "SATA link up %s (SStatus %X SControl %X)\n",
1789 sata_spd_string(tmp), sstatus, scontrol);
Tejun Heo3be680b2005-12-19 22:35:02 +09001790 } else {
Tejun Heof15a1da2006-05-15 20:57:56 +09001791 ata_port_printk(ap, KERN_INFO,
1792 "SATA link down (SStatus %X SControl %X)\n",
1793 sstatus, scontrol);
Tejun Heo3be680b2005-12-19 22:35:02 +09001794 }
1795}
1796
1797/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001798 * __sata_phy_reset - Wake/reset a low-level SATA PHY
1799 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001801 * This function issues commands to standard SATA Sxxx
1802 * PHY registers, to wake up the phy (and device), and
1803 * clear any reset condition.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 *
1805 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001806 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 *
1808 */
1809void __sata_phy_reset(struct ata_port *ap)
1810{
1811 u32 sstatus;
1812 unsigned long timeout = jiffies + (HZ * 5);
1813
1814 if (ap->flags & ATA_FLAG_SATA_RESET) {
Brett Russcdcca892005-03-28 15:10:27 -05001815 /* issue phy wake/reset */
Tejun Heo81952c52006-05-15 20:57:47 +09001816 sata_scr_write_flush(ap, SCR_CONTROL, 0x301);
Tejun Heo62ba2842005-06-26 23:27:19 +09001817 /* Couldn't find anything in SATA I/II specs, but
1818 * AHCI-1.1 10.4.2 says at least 1 ms. */
1819 mdelay(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 }
Tejun Heo81952c52006-05-15 20:57:47 +09001821 /* phy wake/clear reset */
1822 sata_scr_write_flush(ap, SCR_CONTROL, 0x300);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
1824 /* wait for phy to become ready, if necessary */
1825 do {
1826 msleep(200);
Tejun Heo81952c52006-05-15 20:57:47 +09001827 sata_scr_read(ap, SCR_STATUS, &sstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 if ((sstatus & 0xf) != 1)
1829 break;
1830 } while (time_before(jiffies, timeout));
1831
Tejun Heo3be680b2005-12-19 22:35:02 +09001832 /* print link status */
1833 sata_print_link_status(ap);
Jeff Garzik656563e2005-11-20 03:36:45 -05001834
Tejun Heo3be680b2005-12-19 22:35:02 +09001835 /* TODO: phy layer with polling, timeouts, etc. */
Tejun Heo81952c52006-05-15 20:57:47 +09001836 if (!ata_port_offline(ap))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 ata_port_probe(ap);
Tejun Heo3be680b2005-12-19 22:35:02 +09001838 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 ata_port_disable(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840
Tejun Heo198e0fe2006-04-02 18:51:52 +09001841 if (ap->flags & ATA_FLAG_DISABLED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 return;
1843
1844 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
1845 ata_port_disable(ap);
1846 return;
1847 }
1848
1849 ap->cbl = ATA_CBL_SATA;
1850}
1851
1852/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001853 * sata_phy_reset - Reset SATA bus.
1854 * @ap: SATA port associated with target SATA PHY.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001856 * This function resets the SATA bus, and then probes
1857 * the bus for devices.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 *
1859 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04001860 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 *
1862 */
1863void sata_phy_reset(struct ata_port *ap)
1864{
1865 __sata_phy_reset(ap);
Tejun Heo198e0fe2006-04-02 18:51:52 +09001866 if (ap->flags & ATA_FLAG_DISABLED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 return;
1868 ata_bus_reset(ap);
1869}
1870
1871/**
Alan Coxebdfca62006-03-23 15:38:34 +00001872 * ata_dev_pair - return other device on cable
Alan Coxebdfca62006-03-23 15:38:34 +00001873 * @adev: device
1874 *
1875 * Obtain the other device on the same cable, or if none is
1876 * present NULL is returned
1877 */
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001878
Tejun Heo3373efd2006-05-15 20:57:53 +09001879struct ata_device *ata_dev_pair(struct ata_device *adev)
Alan Coxebdfca62006-03-23 15:38:34 +00001880{
Tejun Heo3373efd2006-05-15 20:57:53 +09001881 struct ata_port *ap = adev->ap;
Alan Coxebdfca62006-03-23 15:38:34 +00001882 struct ata_device *pair = &ap->device[1 - adev->devno];
Tejun Heoe1211e32006-04-01 01:38:18 +09001883 if (!ata_dev_enabled(pair))
Alan Coxebdfca62006-03-23 15:38:34 +00001884 return NULL;
1885 return pair;
1886}
1887
1888/**
Jeff Garzik780a87f2005-05-30 15:41:05 -04001889 * ata_port_disable - Disable port.
1890 * @ap: Port to be disabled.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04001892 * Modify @ap data structure such that the system
1893 * thinks that the entire port is disabled, and should
1894 * never attempt to probe or communicate with devices
1895 * on this port.
1896 *
Jeff Garzikcca39742006-08-24 03:19:22 -04001897 * LOCKING: host lock, or some other form of
Jeff Garzik780a87f2005-05-30 15:41:05 -04001898 * serialization.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 */
1900
1901void ata_port_disable(struct ata_port *ap)
1902{
1903 ap->device[0].class = ATA_DEV_NONE;
1904 ap->device[1].class = ATA_DEV_NONE;
Tejun Heo198e0fe2006-04-02 18:51:52 +09001905 ap->flags |= ATA_FLAG_DISABLED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906}
1907
Tejun Heo1c3fae42006-04-02 20:53:28 +09001908/**
Tejun Heo3c567b72006-05-15 20:57:23 +09001909 * sata_down_spd_limit - adjust SATA spd limit downward
Tejun Heo1c3fae42006-04-02 20:53:28 +09001910 * @ap: Port to adjust SATA spd limit for
1911 *
1912 * Adjust SATA spd limit of @ap downward. Note that this
1913 * function only adjusts the limit. The change must be applied
Tejun Heo3c567b72006-05-15 20:57:23 +09001914 * using sata_set_spd().
Tejun Heo1c3fae42006-04-02 20:53:28 +09001915 *
1916 * LOCKING:
1917 * Inherited from caller.
1918 *
1919 * RETURNS:
1920 * 0 on success, negative errno on failure
1921 */
Tejun Heo3c567b72006-05-15 20:57:23 +09001922int sata_down_spd_limit(struct ata_port *ap)
Tejun Heo1c3fae42006-04-02 20:53:28 +09001923{
Tejun Heo81952c52006-05-15 20:57:47 +09001924 u32 sstatus, spd, mask;
1925 int rc, highbit;
Tejun Heo1c3fae42006-04-02 20:53:28 +09001926
Tejun Heo81952c52006-05-15 20:57:47 +09001927 rc = sata_scr_read(ap, SCR_STATUS, &sstatus);
1928 if (rc)
1929 return rc;
Tejun Heo1c3fae42006-04-02 20:53:28 +09001930
1931 mask = ap->sata_spd_limit;
1932 if (mask <= 1)
1933 return -EINVAL;
1934 highbit = fls(mask) - 1;
1935 mask &= ~(1 << highbit);
1936
Tejun Heo81952c52006-05-15 20:57:47 +09001937 spd = (sstatus >> 4) & 0xf;
Tejun Heo1c3fae42006-04-02 20:53:28 +09001938 if (spd <= 1)
1939 return -EINVAL;
1940 spd--;
1941 mask &= (1 << spd) - 1;
1942 if (!mask)
1943 return -EINVAL;
1944
1945 ap->sata_spd_limit = mask;
1946
Tejun Heof15a1da2006-05-15 20:57:56 +09001947 ata_port_printk(ap, KERN_WARNING, "limiting SATA link speed to %s\n",
1948 sata_spd_string(fls(mask)));
Tejun Heo1c3fae42006-04-02 20:53:28 +09001949
1950 return 0;
1951}
1952
Tejun Heo3c567b72006-05-15 20:57:23 +09001953static int __sata_set_spd_needed(struct ata_port *ap, u32 *scontrol)
Tejun Heo1c3fae42006-04-02 20:53:28 +09001954{
1955 u32 spd, limit;
1956
1957 if (ap->sata_spd_limit == UINT_MAX)
1958 limit = 0;
1959 else
1960 limit = fls(ap->sata_spd_limit);
1961
1962 spd = (*scontrol >> 4) & 0xf;
1963 *scontrol = (*scontrol & ~0xf0) | ((limit & 0xf) << 4);
1964
1965 return spd != limit;
1966}
1967
1968/**
Tejun Heo3c567b72006-05-15 20:57:23 +09001969 * sata_set_spd_needed - is SATA spd configuration needed
Tejun Heo1c3fae42006-04-02 20:53:28 +09001970 * @ap: Port in question
1971 *
1972 * Test whether the spd limit in SControl matches
1973 * @ap->sata_spd_limit. This function is used to determine
1974 * whether hardreset is necessary to apply SATA spd
1975 * configuration.
1976 *
1977 * LOCKING:
1978 * Inherited from caller.
1979 *
1980 * RETURNS:
1981 * 1 if SATA spd configuration is needed, 0 otherwise.
1982 */
Tejun Heo3c567b72006-05-15 20:57:23 +09001983int sata_set_spd_needed(struct ata_port *ap)
Tejun Heo1c3fae42006-04-02 20:53:28 +09001984{
1985 u32 scontrol;
1986
Tejun Heo81952c52006-05-15 20:57:47 +09001987 if (sata_scr_read(ap, SCR_CONTROL, &scontrol))
Tejun Heo1c3fae42006-04-02 20:53:28 +09001988 return 0;
1989
Tejun Heo3c567b72006-05-15 20:57:23 +09001990 return __sata_set_spd_needed(ap, &scontrol);
Tejun Heo1c3fae42006-04-02 20:53:28 +09001991}
1992
1993/**
Tejun Heo3c567b72006-05-15 20:57:23 +09001994 * sata_set_spd - set SATA spd according to spd limit
Tejun Heo1c3fae42006-04-02 20:53:28 +09001995 * @ap: Port to set SATA spd for
1996 *
1997 * Set SATA spd of @ap according to sata_spd_limit.
1998 *
1999 * LOCKING:
2000 * Inherited from caller.
2001 *
2002 * RETURNS:
2003 * 0 if spd doesn't need to be changed, 1 if spd has been
Tejun Heo81952c52006-05-15 20:57:47 +09002004 * changed. Negative errno if SCR registers are inaccessible.
Tejun Heo1c3fae42006-04-02 20:53:28 +09002005 */
Tejun Heo3c567b72006-05-15 20:57:23 +09002006int sata_set_spd(struct ata_port *ap)
Tejun Heo1c3fae42006-04-02 20:53:28 +09002007{
2008 u32 scontrol;
Tejun Heo81952c52006-05-15 20:57:47 +09002009 int rc;
Tejun Heo1c3fae42006-04-02 20:53:28 +09002010
Tejun Heo81952c52006-05-15 20:57:47 +09002011 if ((rc = sata_scr_read(ap, SCR_CONTROL, &scontrol)))
2012 return rc;
Tejun Heo1c3fae42006-04-02 20:53:28 +09002013
Tejun Heo3c567b72006-05-15 20:57:23 +09002014 if (!__sata_set_spd_needed(ap, &scontrol))
Tejun Heo1c3fae42006-04-02 20:53:28 +09002015 return 0;
2016
Tejun Heo81952c52006-05-15 20:57:47 +09002017 if ((rc = sata_scr_write(ap, SCR_CONTROL, scontrol)))
2018 return rc;
2019
Tejun Heo1c3fae42006-04-02 20:53:28 +09002020 return 1;
2021}
2022
Alan Cox452503f2005-10-21 19:01:32 -04002023/*
2024 * This mode timing computation functionality is ported over from
2025 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
2026 */
2027/*
Alan Coxb352e572006-08-10 18:52:12 +01002028 * PIO 0-4, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
Alan Cox452503f2005-10-21 19:01:32 -04002029 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
Alan Coxb352e572006-08-10 18:52:12 +01002030 * for UDMA6, which is currently supported only by Maxtor drives.
2031 *
2032 * For PIO 5/6 MWDMA 3/4 see the CFA specification 3.0.
Alan Cox452503f2005-10-21 19:01:32 -04002033 */
2034
2035static const struct ata_timing ata_timing[] = {
2036
2037 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
2038 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
2039 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
2040 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
2041
Alan Coxb352e572006-08-10 18:52:12 +01002042 { XFER_MW_DMA_4, 25, 0, 0, 0, 55, 20, 80, 0 },
2043 { XFER_MW_DMA_3, 25, 0, 0, 0, 65, 25, 100, 0 },
Alan Cox452503f2005-10-21 19:01:32 -04002044 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
2045 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
2046 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
2047
2048/* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002049
Alan Cox452503f2005-10-21 19:01:32 -04002050 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
2051 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
2052 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002053
Alan Cox452503f2005-10-21 19:01:32 -04002054 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
2055 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
2056 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
2057
Alan Coxb352e572006-08-10 18:52:12 +01002058 { XFER_PIO_6, 10, 55, 20, 80, 55, 20, 80, 0 },
2059 { XFER_PIO_5, 15, 65, 25, 100, 65, 25, 100, 0 },
Alan Cox452503f2005-10-21 19:01:32 -04002060 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
2061 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
2062
2063 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
2064 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
2065 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
2066
2067/* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
2068
2069 { 0xFF }
2070};
2071
2072#define ENOUGH(v,unit) (((v)-1)/(unit)+1)
2073#define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
2074
2075static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
2076{
2077 q->setup = EZ(t->setup * 1000, T);
2078 q->act8b = EZ(t->act8b * 1000, T);
2079 q->rec8b = EZ(t->rec8b * 1000, T);
2080 q->cyc8b = EZ(t->cyc8b * 1000, T);
2081 q->active = EZ(t->active * 1000, T);
2082 q->recover = EZ(t->recover * 1000, T);
2083 q->cycle = EZ(t->cycle * 1000, T);
2084 q->udma = EZ(t->udma * 1000, UT);
2085}
2086
2087void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
2088 struct ata_timing *m, unsigned int what)
2089{
2090 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
2091 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
2092 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
2093 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
2094 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
2095 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
2096 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
2097 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
2098}
2099
2100static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
2101{
2102 const struct ata_timing *t;
2103
2104 for (t = ata_timing; t->mode != speed; t++)
Alan Cox91190752005-10-26 12:17:46 -04002105 if (t->mode == 0xFF)
Alan Cox452503f2005-10-21 19:01:32 -04002106 return NULL;
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002107 return t;
Alan Cox452503f2005-10-21 19:01:32 -04002108}
2109
2110int ata_timing_compute(struct ata_device *adev, unsigned short speed,
2111 struct ata_timing *t, int T, int UT)
2112{
2113 const struct ata_timing *s;
2114 struct ata_timing p;
2115
2116 /*
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002117 * Find the mode.
Albert Lee75b1f2f2005-11-16 17:06:18 +08002118 */
Alan Cox452503f2005-10-21 19:01:32 -04002119
2120 if (!(s = ata_timing_find_mode(speed)))
2121 return -EINVAL;
2122
Albert Lee75b1f2f2005-11-16 17:06:18 +08002123 memcpy(t, s, sizeof(*s));
2124
Alan Cox452503f2005-10-21 19:01:32 -04002125 /*
2126 * If the drive is an EIDE drive, it can tell us it needs extended
2127 * PIO/MW_DMA cycle timing.
2128 */
2129
2130 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
2131 memset(&p, 0, sizeof(p));
2132 if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
2133 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
2134 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
2135 } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
2136 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
2137 }
2138 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
2139 }
2140
2141 /*
2142 * Convert the timing to bus clock counts.
2143 */
2144
Albert Lee75b1f2f2005-11-16 17:06:18 +08002145 ata_timing_quantize(t, t, T, UT);
Alan Cox452503f2005-10-21 19:01:32 -04002146
2147 /*
Randy Dunlapc893a3a2006-01-28 13:15:32 -05002148 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
2149 * S.M.A.R.T * and some other commands. We have to ensure that the
2150 * DMA cycle timing is slower/equal than the fastest PIO timing.
Alan Cox452503f2005-10-21 19:01:32 -04002151 */
2152
2153 if (speed > XFER_PIO_4) {
2154 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
2155 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
2156 }
2157
2158 /*
Randy Dunlapc893a3a2006-01-28 13:15:32 -05002159 * Lengthen active & recovery time so that cycle time is correct.
Alan Cox452503f2005-10-21 19:01:32 -04002160 */
2161
2162 if (t->act8b + t->rec8b < t->cyc8b) {
2163 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
2164 t->rec8b = t->cyc8b - t->act8b;
2165 }
2166
2167 if (t->active + t->recover < t->cycle) {
2168 t->active += (t->cycle - (t->active + t->recover)) / 2;
2169 t->recover = t->cycle - t->active;
2170 }
2171
2172 return 0;
2173}
2174
Tejun Heocf176e12006-04-02 17:54:46 +09002175/**
2176 * ata_down_xfermask_limit - adjust dev xfer masks downward
Tejun Heocf176e12006-04-02 17:54:46 +09002177 * @dev: Device to adjust xfer masks
2178 * @force_pio0: Force PIO0
2179 *
2180 * Adjust xfer masks of @dev downward. Note that this function
2181 * does not apply the change. Invoking ata_set_mode() afterwards
2182 * will apply the limit.
2183 *
2184 * LOCKING:
2185 * Inherited from caller.
2186 *
2187 * RETURNS:
2188 * 0 on success, negative errno on failure
2189 */
Tejun Heo3373efd2006-05-15 20:57:53 +09002190int ata_down_xfermask_limit(struct ata_device *dev, int force_pio0)
Tejun Heocf176e12006-04-02 17:54:46 +09002191{
2192 unsigned long xfer_mask;
2193 int highbit;
2194
2195 xfer_mask = ata_pack_xfermask(dev->pio_mask, dev->mwdma_mask,
2196 dev->udma_mask);
2197
2198 if (!xfer_mask)
2199 goto fail;
2200 /* don't gear down to MWDMA from UDMA, go directly to PIO */
2201 if (xfer_mask & ATA_MASK_UDMA)
2202 xfer_mask &= ~ATA_MASK_MWDMA;
2203
2204 highbit = fls(xfer_mask) - 1;
2205 xfer_mask &= ~(1 << highbit);
2206 if (force_pio0)
2207 xfer_mask &= 1 << ATA_SHIFT_PIO;
2208 if (!xfer_mask)
2209 goto fail;
2210
2211 ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask,
2212 &dev->udma_mask);
2213
Tejun Heof15a1da2006-05-15 20:57:56 +09002214 ata_dev_printk(dev, KERN_WARNING, "limiting speed to %s\n",
2215 ata_mode_string(xfer_mask));
Tejun Heocf176e12006-04-02 17:54:46 +09002216
2217 return 0;
2218
2219 fail:
2220 return -EINVAL;
2221}
2222
Tejun Heo3373efd2006-05-15 20:57:53 +09002223static int ata_dev_set_mode(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224{
Tejun Heobaa1e782006-11-01 18:39:27 +09002225 struct ata_eh_context *ehc = &dev->ap->eh_context;
Tejun Heo83206a22006-03-24 15:25:31 +09002226 unsigned int err_mask;
2227 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228
Tejun Heoe8384602006-04-02 18:51:53 +09002229 dev->flags &= ~ATA_DFLAG_PIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 if (dev->xfer_shift == ATA_SHIFT_PIO)
2231 dev->flags |= ATA_DFLAG_PIO;
2232
Tejun Heo3373efd2006-05-15 20:57:53 +09002233 err_mask = ata_dev_set_xfermode(dev);
Tejun Heo83206a22006-03-24 15:25:31 +09002234 if (err_mask) {
Tejun Heof15a1da2006-05-15 20:57:56 +09002235 ata_dev_printk(dev, KERN_ERR, "failed to set xfermode "
2236 "(err_mask=0x%x)\n", err_mask);
Tejun Heo83206a22006-03-24 15:25:31 +09002237 return -EIO;
2238 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239
Tejun Heobaa1e782006-11-01 18:39:27 +09002240 ehc->i.flags |= ATA_EHI_POST_SETMODE;
Tejun Heo3373efd2006-05-15 20:57:53 +09002241 rc = ata_dev_revalidate(dev, 0);
Tejun Heobaa1e782006-11-01 18:39:27 +09002242 ehc->i.flags &= ~ATA_EHI_POST_SETMODE;
Tejun Heo5eb45c02006-04-02 18:51:52 +09002243 if (rc)
Tejun Heo83206a22006-03-24 15:25:31 +09002244 return rc;
Tejun Heo48a8a142006-03-05 17:55:58 +09002245
Tejun Heo23e71c32006-03-06 04:31:57 +09002246 DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n",
2247 dev->xfer_shift, (int)dev->xfer_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248
Tejun Heof15a1da2006-05-15 20:57:56 +09002249 ata_dev_printk(dev, KERN_INFO, "configured for %s\n",
2250 ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)));
Tejun Heo83206a22006-03-24 15:25:31 +09002251 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252}
2253
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254/**
2255 * ata_set_mode - Program timings and issue SET FEATURES - XFER
2256 * @ap: port on which timings will be programmed
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002257 * @r_failed_dev: out paramter for failed device
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258 *
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002259 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.). If
2260 * ata_set_mode() fails, pointer to the failing device is
2261 * returned in @r_failed_dev.
Jeff Garzik780a87f2005-05-30 15:41:05 -04002262 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002264 * PCI/etc. bus probe sem.
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002265 *
2266 * RETURNS:
2267 * 0 on success, negative errno otherwise
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 */
Tejun Heo1ad8e7f2006-04-02 18:51:53 +09002269int ata_set_mode(struct ata_port *ap, struct ata_device **r_failed_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270{
Tejun Heoe8e06192006-04-01 01:38:18 +09002271 struct ata_device *dev;
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002272 int i, rc = 0, used_dma = 0, found = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273
Tejun Heo3adcebb2006-05-15 20:57:37 +09002274 /* has private set_mode? */
2275 if (ap->ops->set_mode) {
2276 /* FIXME: make ->set_mode handle no device case and
2277 * return error code and failing device on failure.
2278 */
2279 for (i = 0; i < ATA_MAX_DEVICES; i++) {
Tejun Heo02670bf2006-07-03 16:07:26 +09002280 if (ata_dev_ready(&ap->device[i])) {
Tejun Heo3adcebb2006-05-15 20:57:37 +09002281 ap->ops->set_mode(ap);
2282 break;
2283 }
2284 }
2285 return 0;
2286 }
2287
Tejun Heoa6d5a512006-03-06 04:31:57 +09002288 /* step 1: calculate xfer_mask */
2289 for (i = 0; i < ATA_MAX_DEVICES; i++) {
Tejun Heoacf356b2006-03-24 14:07:50 +09002290 unsigned int pio_mask, dma_mask;
Tejun Heoa6d5a512006-03-06 04:31:57 +09002291
Tejun Heoe8e06192006-04-01 01:38:18 +09002292 dev = &ap->device[i];
2293
Tejun Heoe1211e32006-04-01 01:38:18 +09002294 if (!ata_dev_enabled(dev))
Tejun Heoa6d5a512006-03-06 04:31:57 +09002295 continue;
2296
Tejun Heo3373efd2006-05-15 20:57:53 +09002297 ata_dev_xfermask(dev);
Tejun Heoa6d5a512006-03-06 04:31:57 +09002298
Tejun Heoacf356b2006-03-24 14:07:50 +09002299 pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0);
2300 dma_mask = ata_pack_xfermask(0, dev->mwdma_mask, dev->udma_mask);
2301 dev->pio_mode = ata_xfer_mask2mode(pio_mask);
2302 dev->dma_mode = ata_xfer_mask2mode(dma_mask);
Alan Cox5444a6f2006-03-27 18:58:20 +01002303
Tejun Heo4f659772006-04-01 01:38:18 +09002304 found = 1;
Alan Cox5444a6f2006-03-27 18:58:20 +01002305 if (dev->dma_mode)
2306 used_dma = 1;
Tejun Heoa6d5a512006-03-06 04:31:57 +09002307 }
Tejun Heo4f659772006-04-01 01:38:18 +09002308 if (!found)
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002309 goto out;
Tejun Heoa6d5a512006-03-06 04:31:57 +09002310
2311 /* step 2: always set host PIO timings */
Tejun Heoe8e06192006-04-01 01:38:18 +09002312 for (i = 0; i < ATA_MAX_DEVICES; i++) {
2313 dev = &ap->device[i];
2314 if (!ata_dev_enabled(dev))
2315 continue;
2316
2317 if (!dev->pio_mode) {
Tejun Heof15a1da2006-05-15 20:57:56 +09002318 ata_dev_printk(dev, KERN_WARNING, "no PIO support\n");
Tejun Heoe8e06192006-04-01 01:38:18 +09002319 rc = -EINVAL;
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002320 goto out;
Tejun Heoe8e06192006-04-01 01:38:18 +09002321 }
2322
2323 dev->xfer_mode = dev->pio_mode;
2324 dev->xfer_shift = ATA_SHIFT_PIO;
2325 if (ap->ops->set_piomode)
2326 ap->ops->set_piomode(ap, dev);
2327 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328
Tejun Heoa6d5a512006-03-06 04:31:57 +09002329 /* step 3: set host DMA timings */
Tejun Heoe8e06192006-04-01 01:38:18 +09002330 for (i = 0; i < ATA_MAX_DEVICES; i++) {
2331 dev = &ap->device[i];
2332
2333 if (!ata_dev_enabled(dev) || !dev->dma_mode)
2334 continue;
2335
2336 dev->xfer_mode = dev->dma_mode;
2337 dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
2338 if (ap->ops->set_dmamode)
2339 ap->ops->set_dmamode(ap, dev);
2340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341
2342 /* step 4: update devices' xfer mode */
Tejun Heo83206a22006-03-24 15:25:31 +09002343 for (i = 0; i < ATA_MAX_DEVICES; i++) {
Tejun Heoe8e06192006-04-01 01:38:18 +09002344 dev = &ap->device[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345
Tejun Heo02670bf2006-07-03 16:07:26 +09002346 /* don't udpate suspended devices' xfer mode */
2347 if (!ata_dev_ready(dev))
Tejun Heo83206a22006-03-24 15:25:31 +09002348 continue;
2349
Tejun Heo3373efd2006-05-15 20:57:53 +09002350 rc = ata_dev_set_mode(dev);
Tejun Heo5bbc53f2006-04-01 01:38:17 +09002351 if (rc)
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002352 goto out;
Tejun Heo83206a22006-03-24 15:25:31 +09002353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354
Tejun Heoe8e06192006-04-01 01:38:18 +09002355 /* Record simplex status. If we selected DMA then the other
2356 * host channels are not permitted to do so.
Alan Cox5444a6f2006-03-27 18:58:20 +01002357 */
Jeff Garzikcca39742006-08-24 03:19:22 -04002358 if (used_dma && (ap->host->flags & ATA_HOST_SIMPLEX))
2359 ap->host->simplex_claimed = 1;
Alan Cox5444a6f2006-03-27 18:58:20 +01002360
Tejun Heoe8e06192006-04-01 01:38:18 +09002361 /* step5: chip specific finalisation */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 if (ap->ops->post_set_mode)
2363 ap->ops->post_set_mode(ap);
2364
Tejun Heoe82cbdb2006-04-01 01:38:18 +09002365 out:
2366 if (rc)
2367 *r_failed_dev = dev;
2368 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369}
2370
2371/**
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002372 * ata_tf_to_host - issue ATA taskfile to host controller
2373 * @ap: port to which command is being issued
2374 * @tf: ATA taskfile register set
2375 *
2376 * Issues ATA taskfile register set to ATA host controller,
2377 * with proper synchronization with interrupt handler and
2378 * other threads.
2379 *
2380 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002381 * spin_lock_irqsave(host lock)
Jeff Garzik1fdffbc2006-02-09 05:15:27 -05002382 */
2383
2384static inline void ata_tf_to_host(struct ata_port *ap,
2385 const struct ata_taskfile *tf)
2386{
2387 ap->ops->tf_load(ap, tf);
2388 ap->ops->exec_command(ap, tf);
2389}
2390
2391/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392 * ata_busy_sleep - sleep until BSY clears, or timeout
2393 * @ap: port containing status register to be polled
2394 * @tmout_pat: impatience timeout
2395 * @tmout: overall timeout
2396 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04002397 * Sleep until ATA Status register bit BSY clears,
2398 * or a timeout occurs.
2399 *
Tejun Heod1adc1b2006-10-09 18:32:15 +09002400 * LOCKING:
2401 * Kernel thread context (may sleep).
2402 *
2403 * RETURNS:
2404 * 0 on success, -errno otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 */
Tejun Heod1adc1b2006-10-09 18:32:15 +09002406int ata_busy_sleep(struct ata_port *ap,
2407 unsigned long tmout_pat, unsigned long tmout)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408{
2409 unsigned long timer_start, timeout;
2410 u8 status;
2411
2412 status = ata_busy_wait(ap, ATA_BUSY, 300);
2413 timer_start = jiffies;
2414 timeout = timer_start + tmout_pat;
Tejun Heod1adc1b2006-10-09 18:32:15 +09002415 while (status != 0xff && (status & ATA_BUSY) &&
2416 time_before(jiffies, timeout)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 msleep(50);
2418 status = ata_busy_wait(ap, ATA_BUSY, 3);
2419 }
2420
Tejun Heod1adc1b2006-10-09 18:32:15 +09002421 if (status != 0xff && (status & ATA_BUSY))
Tejun Heof15a1da2006-05-15 20:57:56 +09002422 ata_port_printk(ap, KERN_WARNING,
Jeff Garzik35aa7a42006-09-28 06:50:24 -04002423 "port is slow to respond, please be patient "
2424 "(Status 0x%x)\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425
2426 timeout = timer_start + tmout;
Tejun Heod1adc1b2006-10-09 18:32:15 +09002427 while (status != 0xff && (status & ATA_BUSY) &&
2428 time_before(jiffies, timeout)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 msleep(50);
2430 status = ata_chk_status(ap);
2431 }
2432
Tejun Heod1adc1b2006-10-09 18:32:15 +09002433 if (status == 0xff)
2434 return -ENODEV;
2435
Linus Torvalds1da177e2005-04-16 15:20:36 -07002436 if (status & ATA_BUSY) {
Tejun Heof15a1da2006-05-15 20:57:56 +09002437 ata_port_printk(ap, KERN_ERR, "port failed to respond "
Jeff Garzik35aa7a42006-09-28 06:50:24 -04002438 "(%lu secs, Status 0x%x)\n",
2439 tmout / HZ, status);
Tejun Heod1adc1b2006-10-09 18:32:15 +09002440 return -EBUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 }
2442
2443 return 0;
2444}
2445
2446static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask)
2447{
2448 struct ata_ioports *ioaddr = &ap->ioaddr;
2449 unsigned int dev0 = devmask & (1 << 0);
2450 unsigned int dev1 = devmask & (1 << 1);
2451 unsigned long timeout;
2452
2453 /* if device 0 was found in ata_devchk, wait for its
2454 * BSY bit to clear
2455 */
2456 if (dev0)
2457 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2458
2459 /* if device 1 was found in ata_devchk, wait for
2460 * register access, then wait for BSY to clear
2461 */
2462 timeout = jiffies + ATA_TMOUT_BOOT;
2463 while (dev1) {
2464 u8 nsect, lbal;
2465
2466 ap->ops->dev_select(ap, 1);
2467 if (ap->flags & ATA_FLAG_MMIO) {
2468 nsect = readb((void __iomem *) ioaddr->nsect_addr);
2469 lbal = readb((void __iomem *) ioaddr->lbal_addr);
2470 } else {
2471 nsect = inb(ioaddr->nsect_addr);
2472 lbal = inb(ioaddr->lbal_addr);
2473 }
2474 if ((nsect == 1) && (lbal == 1))
2475 break;
2476 if (time_after(jiffies, timeout)) {
2477 dev1 = 0;
2478 break;
2479 }
2480 msleep(50); /* give drive a breather */
2481 }
2482 if (dev1)
2483 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2484
2485 /* is all this really necessary? */
2486 ap->ops->dev_select(ap, 0);
2487 if (dev1)
2488 ap->ops->dev_select(ap, 1);
2489 if (dev0)
2490 ap->ops->dev_select(ap, 0);
2491}
2492
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493static unsigned int ata_bus_softreset(struct ata_port *ap,
2494 unsigned int devmask)
2495{
2496 struct ata_ioports *ioaddr = &ap->ioaddr;
2497
2498 DPRINTK("ata%u: bus reset via SRST\n", ap->id);
2499
2500 /* software reset. causes dev0 to be selected */
2501 if (ap->flags & ATA_FLAG_MMIO) {
2502 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2503 udelay(20); /* FIXME: flush */
2504 writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr);
2505 udelay(20); /* FIXME: flush */
2506 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2507 } else {
2508 outb(ap->ctl, ioaddr->ctl_addr);
2509 udelay(10);
2510 outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
2511 udelay(10);
2512 outb(ap->ctl, ioaddr->ctl_addr);
2513 }
2514
2515 /* spec mandates ">= 2ms" before checking status.
2516 * We wait 150ms, because that was the magic delay used for
2517 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
2518 * between when the ATA command register is written, and then
2519 * status is checked. Because waiting for "a while" before
2520 * checking status is fine, post SRST, we perform this magic
2521 * delay here as well.
Alan Cox09c7ad72006-03-22 15:52:40 +00002522 *
2523 * Old drivers/ide uses the 2mS rule and then waits for ready
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 */
2525 msleep(150);
2526
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05002527 /* Before we perform post reset processing we want to see if
Tejun Heo298a41c2006-03-25 02:58:13 +09002528 * the bus shows 0xFF because the odd clown forgets the D7
2529 * pulldown resistor.
2530 */
Tejun Heod1adc1b2006-10-09 18:32:15 +09002531 if (ata_check_status(ap) == 0xFF)
2532 return 0;
Alan Cox09c7ad72006-03-22 15:52:40 +00002533
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 ata_bus_post_reset(ap, devmask);
2535
2536 return 0;
2537}
2538
2539/**
2540 * ata_bus_reset - reset host port and associated ATA channel
2541 * @ap: port to reset
2542 *
2543 * This is typically the first time we actually start issuing
2544 * commands to the ATA channel. We wait for BSY to clear, then
2545 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
2546 * result. Determine what devices, if any, are on the channel
2547 * by looking at the device 0/1 error register. Look at the signature
2548 * stored in each device's taskfile registers, to determine if
2549 * the device is ATA or ATAPI.
2550 *
2551 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04002552 * PCI/etc. bus probe sem.
Jeff Garzikcca39742006-08-24 03:19:22 -04002553 * Obtains host lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 *
2555 * SIDE EFFECTS:
Tejun Heo198e0fe2006-04-02 18:51:52 +09002556 * Sets ATA_FLAG_DISABLED if bus reset fails.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002557 */
2558
2559void ata_bus_reset(struct ata_port *ap)
2560{
2561 struct ata_ioports *ioaddr = &ap->ioaddr;
2562 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2563 u8 err;
Tejun Heoaec5c3c2006-03-25 01:33:34 +09002564 unsigned int dev0, dev1 = 0, devmask = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565
2566 DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no);
2567
2568 /* determine if device 0/1 are present */
2569 if (ap->flags & ATA_FLAG_SATA_RESET)
2570 dev0 = 1;
2571 else {
2572 dev0 = ata_devchk(ap, 0);
2573 if (slave_possible)
2574 dev1 = ata_devchk(ap, 1);
2575 }
2576
2577 if (dev0)
2578 devmask |= (1 << 0);
2579 if (dev1)
2580 devmask |= (1 << 1);
2581
2582 /* select device 0 again */
2583 ap->ops->dev_select(ap, 0);
2584
2585 /* issue bus reset */
2586 if (ap->flags & ATA_FLAG_SRST)
Tejun Heoaec5c3c2006-03-25 01:33:34 +09002587 if (ata_bus_softreset(ap, devmask))
2588 goto err_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589
2590 /*
2591 * determine by signature whether we have ATA or ATAPI devices
2592 */
Tejun Heob4dc7622006-01-24 17:05:22 +09002593 ap->device[0].class = ata_dev_try_classify(ap, 0, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 if ((slave_possible) && (err != 0x81))
Tejun Heob4dc7622006-01-24 17:05:22 +09002595 ap->device[1].class = ata_dev_try_classify(ap, 1, &err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596
2597 /* re-enable interrupts */
2598 if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */
2599 ata_irq_on(ap);
2600
2601 /* is double-select really necessary? */
2602 if (ap->device[1].class != ATA_DEV_NONE)
2603 ap->ops->dev_select(ap, 1);
2604 if (ap->device[0].class != ATA_DEV_NONE)
2605 ap->ops->dev_select(ap, 0);
2606
2607 /* if no devices were detected, disable this port */
2608 if ((ap->device[0].class == ATA_DEV_NONE) &&
2609 (ap->device[1].class == ATA_DEV_NONE))
2610 goto err_out;
2611
2612 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
2613 /* set up device control for ATA_FLAG_SATA_RESET */
2614 if (ap->flags & ATA_FLAG_MMIO)
2615 writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr);
2616 else
2617 outb(ap->ctl, ioaddr->ctl_addr);
2618 }
2619
2620 DPRINTK("EXIT\n");
2621 return;
2622
2623err_out:
Tejun Heof15a1da2006-05-15 20:57:56 +09002624 ata_port_printk(ap, KERN_ERR, "disabling port\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 ap->ops->port_disable(ap);
2626
2627 DPRINTK("EXIT\n");
2628}
2629
Tejun Heod7bb4cc2006-05-31 18:27:46 +09002630/**
2631 * sata_phy_debounce - debounce SATA phy status
2632 * @ap: ATA port to debounce SATA phy status for
2633 * @params: timing parameters { interval, duratinon, timeout } in msec
2634 *
2635 * Make sure SStatus of @ap reaches stable state, determined by
2636 * holding the same value where DET is not 1 for @duration polled
2637 * every @interval, before @timeout. Timeout constraints the
2638 * beginning of the stable state. Because, after hot unplugging,
2639 * DET gets stuck at 1 on some controllers, this functions waits
2640 * until timeout then returns 0 if DET is stable at 1.
2641 *
2642 * LOCKING:
2643 * Kernel thread context (may sleep)
2644 *
2645 * RETURNS:
2646 * 0 on success, -errno on failure.
2647 */
2648int sata_phy_debounce(struct ata_port *ap, const unsigned long *params)
Tejun Heo7a7921e2006-02-02 18:20:00 +09002649{
Tejun Heod7bb4cc2006-05-31 18:27:46 +09002650 unsigned long interval_msec = params[0];
2651 unsigned long duration = params[1] * HZ / 1000;
2652 unsigned long timeout = jiffies + params[2] * HZ / 1000;
2653 unsigned long last_jiffies;
2654 u32 last, cur;
2655 int rc;
2656
2657 if ((rc = sata_scr_read(ap, SCR_STATUS, &cur)))
2658 return rc;
2659 cur &= 0xf;
2660
2661 last = cur;
2662 last_jiffies = jiffies;
2663
2664 while (1) {
2665 msleep(interval_msec);
2666 if ((rc = sata_scr_read(ap, SCR_STATUS, &cur)))
2667 return rc;
2668 cur &= 0xf;
2669
2670 /* DET stable? */
2671 if (cur == last) {
2672 if (cur == 1 && time_before(jiffies, timeout))
2673 continue;
2674 if (time_after(jiffies, last_jiffies + duration))
2675 return 0;
2676 continue;
2677 }
2678
2679 /* unstable, start over */
2680 last = cur;
2681 last_jiffies = jiffies;
2682
2683 /* check timeout */
2684 if (time_after(jiffies, timeout))
2685 return -EBUSY;
2686 }
2687}
2688
2689/**
2690 * sata_phy_resume - resume SATA phy
2691 * @ap: ATA port to resume SATA phy for
2692 * @params: timing parameters { interval, duratinon, timeout } in msec
2693 *
2694 * Resume SATA phy of @ap and debounce it.
2695 *
2696 * LOCKING:
2697 * Kernel thread context (may sleep)
2698 *
2699 * RETURNS:
2700 * 0 on success, -errno on failure.
2701 */
2702int sata_phy_resume(struct ata_port *ap, const unsigned long *params)
2703{
2704 u32 scontrol;
Tejun Heo81952c52006-05-15 20:57:47 +09002705 int rc;
Tejun Heo7a7921e2006-02-02 18:20:00 +09002706
Tejun Heo81952c52006-05-15 20:57:47 +09002707 if ((rc = sata_scr_read(ap, SCR_CONTROL, &scontrol)))
2708 return rc;
2709
Tejun Heo852ee162006-04-01 01:38:18 +09002710 scontrol = (scontrol & 0x0f0) | 0x300;
Tejun Heo81952c52006-05-15 20:57:47 +09002711
2712 if ((rc = sata_scr_write(ap, SCR_CONTROL, scontrol)))
2713 return rc;
Tejun Heo7a7921e2006-02-02 18:20:00 +09002714
Tejun Heod7bb4cc2006-05-31 18:27:46 +09002715 /* Some PHYs react badly if SStatus is pounded immediately
2716 * after resuming. Delay 200ms before debouncing.
2717 */
2718 msleep(200);
Tejun Heo7a7921e2006-02-02 18:20:00 +09002719
Tejun Heod7bb4cc2006-05-31 18:27:46 +09002720 return sata_phy_debounce(ap, params);
Tejun Heo7a7921e2006-02-02 18:20:00 +09002721}
2722
Tejun Heof5914a42006-05-31 18:27:48 +09002723static void ata_wait_spinup(struct ata_port *ap)
2724{
2725 struct ata_eh_context *ehc = &ap->eh_context;
2726 unsigned long end, secs;
2727 int rc;
2728
2729 /* first, debounce phy if SATA */
2730 if (ap->cbl == ATA_CBL_SATA) {
Tejun Heoe9c83912006-07-03 16:07:26 +09002731 rc = sata_phy_debounce(ap, sata_deb_timing_hotplug);
Tejun Heof5914a42006-05-31 18:27:48 +09002732
2733 /* if debounced successfully and offline, no need to wait */
2734 if ((rc == 0 || rc == -EOPNOTSUPP) && ata_port_offline(ap))
2735 return;
2736 }
2737
2738 /* okay, let's give the drive time to spin up */
2739 end = ehc->i.hotplug_timestamp + ATA_SPINUP_WAIT * HZ / 1000;
2740 secs = ((end - jiffies) + HZ - 1) / HZ;
2741
2742 if (time_after(jiffies, end))
2743 return;
2744
2745 if (secs > 5)
2746 ata_port_printk(ap, KERN_INFO, "waiting for device to spin up "
2747 "(%lu secs)\n", secs);
2748
2749 schedule_timeout_uninterruptible(end - jiffies);
2750}
2751
2752/**
2753 * ata_std_prereset - prepare for reset
2754 * @ap: ATA port to be reset
2755 *
2756 * @ap is about to be reset. Initialize it.
2757 *
2758 * LOCKING:
2759 * Kernel thread context (may sleep)
2760 *
2761 * RETURNS:
2762 * 0 on success, -errno otherwise.
2763 */
2764int ata_std_prereset(struct ata_port *ap)
2765{
2766 struct ata_eh_context *ehc = &ap->eh_context;
Tejun Heoe9c83912006-07-03 16:07:26 +09002767 const unsigned long *timing = sata_ehc_deb_timing(ehc);
Tejun Heof5914a42006-05-31 18:27:48 +09002768 int rc;
2769
Tejun Heo28324302006-07-03 16:07:26 +09002770 /* handle link resume & hotplug spinup */
2771 if ((ehc->i.flags & ATA_EHI_RESUME_LINK) &&
2772 (ap->flags & ATA_FLAG_HRST_TO_RESUME))
2773 ehc->i.action |= ATA_EH_HARDRESET;
2774
2775 if ((ehc->i.flags & ATA_EHI_HOTPLUGGED) &&
2776 (ap->flags & ATA_FLAG_SKIP_D2H_BSY))
2777 ata_wait_spinup(ap);
Tejun Heof5914a42006-05-31 18:27:48 +09002778
2779 /* if we're about to do hardreset, nothing more to do */
2780 if (ehc->i.action & ATA_EH_HARDRESET)
2781 return 0;
2782
2783 /* if SATA, resume phy */
2784 if (ap->cbl == ATA_CBL_SATA) {
Tejun Heof5914a42006-05-31 18:27:48 +09002785 rc = sata_phy_resume(ap, timing);
2786 if (rc && rc != -EOPNOTSUPP) {
2787 /* phy resume failed */
2788 ata_port_printk(ap, KERN_WARNING, "failed to resume "
2789 "link for reset (errno=%d)\n", rc);
2790 return rc;
2791 }
2792 }
2793
2794 /* Wait for !BSY if the controller can wait for the first D2H
2795 * Reg FIS and we don't know that no device is attached.
2796 */
2797 if (!(ap->flags & ATA_FLAG_SKIP_D2H_BSY) && !ata_port_offline(ap))
2798 ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT);
2799
2800 return 0;
2801}
2802
Tejun Heoc2bd5802006-01-24 17:05:22 +09002803/**
2804 * ata_std_softreset - reset host port via ATA SRST
2805 * @ap: port to reset
Tejun Heoc2bd5802006-01-24 17:05:22 +09002806 * @classes: resulting classes of attached devices
2807 *
Tejun Heo52783c52006-05-31 18:28:22 +09002808 * Reset host port using ATA SRST.
Tejun Heoc2bd5802006-01-24 17:05:22 +09002809 *
2810 * LOCKING:
2811 * Kernel thread context (may sleep)
2812 *
2813 * RETURNS:
2814 * 0 on success, -errno otherwise.
2815 */
Tejun Heo2bf2cb22006-04-11 22:16:45 +09002816int ata_std_softreset(struct ata_port *ap, unsigned int *classes)
Tejun Heoc2bd5802006-01-24 17:05:22 +09002817{
2818 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
2819 unsigned int devmask = 0, err_mask;
2820 u8 err;
2821
2822 DPRINTK("ENTER\n");
2823
Tejun Heo81952c52006-05-15 20:57:47 +09002824 if (ata_port_offline(ap)) {
Tejun Heo3a397462006-02-10 23:58:48 +09002825 classes[0] = ATA_DEV_NONE;
2826 goto out;
2827 }
2828
Tejun Heoc2bd5802006-01-24 17:05:22 +09002829 /* determine if device 0/1 are present */
2830 if (ata_devchk(ap, 0))
2831 devmask |= (1 << 0);
2832 if (slave_possible && ata_devchk(ap, 1))
2833 devmask |= (1 << 1);
2834
Tejun Heoc2bd5802006-01-24 17:05:22 +09002835 /* select device 0 again */
2836 ap->ops->dev_select(ap, 0);
2837
2838 /* issue bus reset */
2839 DPRINTK("about to softreset, devmask=%x\n", devmask);
2840 err_mask = ata_bus_softreset(ap, devmask);
2841 if (err_mask) {
Tejun Heof15a1da2006-05-15 20:57:56 +09002842 ata_port_printk(ap, KERN_ERR, "SRST failed (err_mask=0x%x)\n",
2843 err_mask);
Tejun Heoc2bd5802006-01-24 17:05:22 +09002844 return -EIO;
2845 }
2846
2847 /* determine by signature whether we have ATA or ATAPI devices */
2848 classes[0] = ata_dev_try_classify(ap, 0, &err);
2849 if (slave_possible && err != 0x81)
2850 classes[1] = ata_dev_try_classify(ap, 1, &err);
2851
Tejun Heo3a397462006-02-10 23:58:48 +09002852 out:
Tejun Heoc2bd5802006-01-24 17:05:22 +09002853 DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]);
2854 return 0;
2855}
2856
2857/**
Tejun Heob6103f62006-11-01 17:59:53 +09002858 * sata_port_hardreset - reset port via SATA phy reset
Tejun Heoc2bd5802006-01-24 17:05:22 +09002859 * @ap: port to reset
Tejun Heob6103f62006-11-01 17:59:53 +09002860 * @timing: timing parameters { interval, duratinon, timeout } in msec
Tejun Heoc2bd5802006-01-24 17:05:22 +09002861 *
2862 * SATA phy-reset host port using DET bits of SControl register.
Tejun Heoc2bd5802006-01-24 17:05:22 +09002863 *
2864 * LOCKING:
2865 * Kernel thread context (may sleep)
2866 *
2867 * RETURNS:
2868 * 0 on success, -errno otherwise.
2869 */
Tejun Heob6103f62006-11-01 17:59:53 +09002870int sata_port_hardreset(struct ata_port *ap, const unsigned long *timing)
Tejun Heoc2bd5802006-01-24 17:05:22 +09002871{
Tejun Heo852ee162006-04-01 01:38:18 +09002872 u32 scontrol;
Tejun Heo81952c52006-05-15 20:57:47 +09002873 int rc;
Tejun Heo852ee162006-04-01 01:38:18 +09002874
Tejun Heoc2bd5802006-01-24 17:05:22 +09002875 DPRINTK("ENTER\n");
2876
Tejun Heo3c567b72006-05-15 20:57:23 +09002877 if (sata_set_spd_needed(ap)) {
Tejun Heo1c3fae42006-04-02 20:53:28 +09002878 /* SATA spec says nothing about how to reconfigure
2879 * spd. To be on the safe side, turn off phy during
2880 * reconfiguration. This works for at least ICH7 AHCI
2881 * and Sil3124.
2882 */
Tejun Heo81952c52006-05-15 20:57:47 +09002883 if ((rc = sata_scr_read(ap, SCR_CONTROL, &scontrol)))
Tejun Heob6103f62006-11-01 17:59:53 +09002884 goto out;
Tejun Heo81952c52006-05-15 20:57:47 +09002885
Martin Hicksa34b6fc2006-07-05 15:06:13 -04002886 scontrol = (scontrol & 0x0f0) | 0x304;
Tejun Heo81952c52006-05-15 20:57:47 +09002887
2888 if ((rc = sata_scr_write(ap, SCR_CONTROL, scontrol)))
Tejun Heob6103f62006-11-01 17:59:53 +09002889 goto out;
Tejun Heo1c3fae42006-04-02 20:53:28 +09002890
Tejun Heo3c567b72006-05-15 20:57:23 +09002891 sata_set_spd(ap);
Tejun Heo1c3fae42006-04-02 20:53:28 +09002892 }
2893
2894 /* issue phy wake/reset */
Tejun Heo81952c52006-05-15 20:57:47 +09002895 if ((rc = sata_scr_read(ap, SCR_CONTROL, &scontrol)))
Tejun Heob6103f62006-11-01 17:59:53 +09002896 goto out;
Tejun Heo81952c52006-05-15 20:57:47 +09002897
Tejun Heo852ee162006-04-01 01:38:18 +09002898 scontrol = (scontrol & 0x0f0) | 0x301;
Tejun Heo81952c52006-05-15 20:57:47 +09002899
2900 if ((rc = sata_scr_write_flush(ap, SCR_CONTROL, scontrol)))
Tejun Heob6103f62006-11-01 17:59:53 +09002901 goto out;
Tejun Heoc2bd5802006-01-24 17:05:22 +09002902
Tejun Heo1c3fae42006-04-02 20:53:28 +09002903 /* Couldn't find anything in SATA I/II specs, but AHCI-1.1
Tejun Heoc2bd5802006-01-24 17:05:22 +09002904 * 10.4.2 says at least 1 ms.
2905 */
2906 msleep(1);
2907
Tejun Heo1c3fae42006-04-02 20:53:28 +09002908 /* bring phy back */
Tejun Heob6103f62006-11-01 17:59:53 +09002909 rc = sata_phy_resume(ap, timing);
2910 out:
2911 DPRINTK("EXIT, rc=%d\n", rc);
2912 return rc;
2913}
2914
2915/**
2916 * sata_std_hardreset - reset host port via SATA phy reset
2917 * @ap: port to reset
2918 * @class: resulting class of attached device
2919 *
2920 * SATA phy-reset host port using DET bits of SControl register,
2921 * wait for !BSY and classify the attached device.
2922 *
2923 * LOCKING:
2924 * Kernel thread context (may sleep)
2925 *
2926 * RETURNS:
2927 * 0 on success, -errno otherwise.
2928 */
2929int sata_std_hardreset(struct ata_port *ap, unsigned int *class)
2930{
2931 const unsigned long *timing = sata_ehc_deb_timing(&ap->eh_context);
2932 int rc;
2933
2934 DPRINTK("ENTER\n");
2935
2936 /* do hardreset */
2937 rc = sata_port_hardreset(ap, timing);
2938 if (rc) {
2939 ata_port_printk(ap, KERN_ERR,
2940 "COMRESET failed (errno=%d)\n", rc);
2941 return rc;
2942 }
Tejun Heoc2bd5802006-01-24 17:05:22 +09002943
Tejun Heoc2bd5802006-01-24 17:05:22 +09002944 /* TODO: phy layer with polling, timeouts, etc. */
Tejun Heo81952c52006-05-15 20:57:47 +09002945 if (ata_port_offline(ap)) {
Tejun Heoc2bd5802006-01-24 17:05:22 +09002946 *class = ATA_DEV_NONE;
2947 DPRINTK("EXIT, link offline\n");
2948 return 0;
2949 }
2950
2951 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
Tejun Heof15a1da2006-05-15 20:57:56 +09002952 ata_port_printk(ap, KERN_ERR,
2953 "COMRESET failed (device not ready)\n");
Tejun Heoc2bd5802006-01-24 17:05:22 +09002954 return -EIO;
2955 }
2956
Tejun Heo3a397462006-02-10 23:58:48 +09002957 ap->ops->dev_select(ap, 0); /* probably unnecessary */
2958
Tejun Heoc2bd5802006-01-24 17:05:22 +09002959 *class = ata_dev_try_classify(ap, 0, NULL);
2960
2961 DPRINTK("EXIT, class=%u\n", *class);
2962 return 0;
2963}
2964
2965/**
2966 * ata_std_postreset - standard postreset callback
2967 * @ap: the target ata_port
2968 * @classes: classes of attached devices
2969 *
2970 * This function is invoked after a successful reset. Note that
2971 * the device might have been reset more than once using
2972 * different reset methods before postreset is invoked.
Tejun Heoc2bd5802006-01-24 17:05:22 +09002973 *
Tejun Heoc2bd5802006-01-24 17:05:22 +09002974 * LOCKING:
2975 * Kernel thread context (may sleep)
2976 */
2977void ata_std_postreset(struct ata_port *ap, unsigned int *classes)
2978{
Tejun Heodc2b3512006-05-15 20:58:00 +09002979 u32 serror;
2980
Tejun Heoc2bd5802006-01-24 17:05:22 +09002981 DPRINTK("ENTER\n");
2982
Tejun Heoc2bd5802006-01-24 17:05:22 +09002983 /* print link status */
Tejun Heo81952c52006-05-15 20:57:47 +09002984 sata_print_link_status(ap);
Tejun Heoc2bd5802006-01-24 17:05:22 +09002985
Tejun Heodc2b3512006-05-15 20:58:00 +09002986 /* clear SError */
2987 if (sata_scr_read(ap, SCR_ERROR, &serror) == 0)
2988 sata_scr_write(ap, SCR_ERROR, serror);
2989
Tejun Heo3a397462006-02-10 23:58:48 +09002990 /* re-enable interrupts */
Tejun Heoe3180492006-05-15 20:58:09 +09002991 if (!ap->ops->error_handler) {
2992 /* FIXME: hack. create a hook instead */
2993 if (ap->ioaddr.ctl_addr)
2994 ata_irq_on(ap);
2995 }
Tejun Heoc2bd5802006-01-24 17:05:22 +09002996
2997 /* is double-select really necessary? */
2998 if (classes[0] != ATA_DEV_NONE)
2999 ap->ops->dev_select(ap, 1);
3000 if (classes[1] != ATA_DEV_NONE)
3001 ap->ops->dev_select(ap, 0);
3002
Tejun Heo3a397462006-02-10 23:58:48 +09003003 /* bail out if no device is present */
3004 if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
3005 DPRINTK("EXIT, no device\n");
3006 return;
3007 }
3008
3009 /* set up device control */
3010 if (ap->ioaddr.ctl_addr) {
3011 if (ap->flags & ATA_FLAG_MMIO)
3012 writeb(ap->ctl, (void __iomem *) ap->ioaddr.ctl_addr);
3013 else
3014 outb(ap->ctl, ap->ioaddr.ctl_addr);
3015 }
Tejun Heoc2bd5802006-01-24 17:05:22 +09003016
3017 DPRINTK("EXIT\n");
3018}
3019
Tejun Heoa62c0fc2006-01-24 17:05:22 +09003020/**
Tejun Heo623a3122006-03-05 17:55:58 +09003021 * ata_dev_same_device - Determine whether new ID matches configured device
Tejun Heo623a3122006-03-05 17:55:58 +09003022 * @dev: device to compare against
3023 * @new_class: class of the new device
3024 * @new_id: IDENTIFY page of the new device
3025 *
3026 * Compare @new_class and @new_id against @dev and determine
3027 * whether @dev is the device indicated by @new_class and
3028 * @new_id.
3029 *
3030 * LOCKING:
3031 * None.
3032 *
3033 * RETURNS:
3034 * 1 if @dev matches @new_class and @new_id, 0 otherwise.
3035 */
Tejun Heo3373efd2006-05-15 20:57:53 +09003036static int ata_dev_same_device(struct ata_device *dev, unsigned int new_class,
3037 const u16 *new_id)
Tejun Heo623a3122006-03-05 17:55:58 +09003038{
3039 const u16 *old_id = dev->id;
3040 unsigned char model[2][41], serial[2][21];
3041 u64 new_n_sectors;
3042
3043 if (dev->class != new_class) {
Tejun Heof15a1da2006-05-15 20:57:56 +09003044 ata_dev_printk(dev, KERN_INFO, "class mismatch %d != %d\n",
3045 dev->class, new_class);
Tejun Heo623a3122006-03-05 17:55:58 +09003046 return 0;
3047 }
3048
3049 ata_id_c_string(old_id, model[0], ATA_ID_PROD_OFS, sizeof(model[0]));
3050 ata_id_c_string(new_id, model[1], ATA_ID_PROD_OFS, sizeof(model[1]));
3051 ata_id_c_string(old_id, serial[0], ATA_ID_SERNO_OFS, sizeof(serial[0]));
3052 ata_id_c_string(new_id, serial[1], ATA_ID_SERNO_OFS, sizeof(serial[1]));
3053 new_n_sectors = ata_id_n_sectors(new_id);
3054
3055 if (strcmp(model[0], model[1])) {
Tejun Heof15a1da2006-05-15 20:57:56 +09003056 ata_dev_printk(dev, KERN_INFO, "model number mismatch "
3057 "'%s' != '%s'\n", model[0], model[1]);
Tejun Heo623a3122006-03-05 17:55:58 +09003058 return 0;
3059 }
3060
3061 if (strcmp(serial[0], serial[1])) {
Tejun Heof15a1da2006-05-15 20:57:56 +09003062 ata_dev_printk(dev, KERN_INFO, "serial number mismatch "
3063 "'%s' != '%s'\n", serial[0], serial[1]);
Tejun Heo623a3122006-03-05 17:55:58 +09003064 return 0;
3065 }
3066
3067 if (dev->class == ATA_DEV_ATA && dev->n_sectors != new_n_sectors) {
Tejun Heof15a1da2006-05-15 20:57:56 +09003068 ata_dev_printk(dev, KERN_INFO, "n_sectors mismatch "
3069 "%llu != %llu\n",
3070 (unsigned long long)dev->n_sectors,
3071 (unsigned long long)new_n_sectors);
Tejun Heo623a3122006-03-05 17:55:58 +09003072 return 0;
3073 }
3074
3075 return 1;
3076}
3077
3078/**
3079 * ata_dev_revalidate - Revalidate ATA device
Tejun Heo623a3122006-03-05 17:55:58 +09003080 * @dev: device to revalidate
Tejun Heobff04642006-11-10 18:08:10 +09003081 * @readid_flags: read ID flags
Tejun Heo623a3122006-03-05 17:55:58 +09003082 *
3083 * Re-read IDENTIFY page and make sure @dev is still attached to
3084 * the port.
3085 *
3086 * LOCKING:
3087 * Kernel thread context (may sleep)
3088 *
3089 * RETURNS:
3090 * 0 on success, negative errno otherwise
3091 */
Tejun Heobff04642006-11-10 18:08:10 +09003092int ata_dev_revalidate(struct ata_device *dev, unsigned int readid_flags)
Tejun Heo623a3122006-03-05 17:55:58 +09003093{
Tejun Heo5eb45c02006-04-02 18:51:52 +09003094 unsigned int class = dev->class;
Tejun Heof15a1da2006-05-15 20:57:56 +09003095 u16 *id = (void *)dev->ap->sector_buf;
Tejun Heo623a3122006-03-05 17:55:58 +09003096 int rc;
3097
Tejun Heo5eb45c02006-04-02 18:51:52 +09003098 if (!ata_dev_enabled(dev)) {
3099 rc = -ENODEV;
3100 goto fail;
3101 }
Tejun Heo623a3122006-03-05 17:55:58 +09003102
Tejun Heofe635c72006-05-15 20:57:35 +09003103 /* read ID data */
Tejun Heobff04642006-11-10 18:08:10 +09003104 rc = ata_dev_read_id(dev, &class, readid_flags, id);
Tejun Heo623a3122006-03-05 17:55:58 +09003105 if (rc)
3106 goto fail;
3107
3108 /* is the device still there? */
Tejun Heo3373efd2006-05-15 20:57:53 +09003109 if (!ata_dev_same_device(dev, class, id)) {
Tejun Heo623a3122006-03-05 17:55:58 +09003110 rc = -ENODEV;
3111 goto fail;
3112 }
3113
Tejun Heofe635c72006-05-15 20:57:35 +09003114 memcpy(dev->id, id, sizeof(id[0]) * ATA_ID_WORDS);
Tejun Heo623a3122006-03-05 17:55:58 +09003115
3116 /* configure device according to the new ID */
Tejun Heoefdaedc2006-11-01 18:38:52 +09003117 rc = ata_dev_configure(dev);
Tejun Heo5eb45c02006-04-02 18:51:52 +09003118 if (rc == 0)
3119 return 0;
Tejun Heo623a3122006-03-05 17:55:58 +09003120
3121 fail:
Tejun Heof15a1da2006-05-15 20:57:56 +09003122 ata_dev_printk(dev, KERN_ERR, "revalidation failed (errno=%d)\n", rc);
Tejun Heo623a3122006-03-05 17:55:58 +09003123 return rc;
3124}
3125
Alan Cox6919a0a2006-10-27 19:08:46 -07003126struct ata_blacklist_entry {
3127 const char *model_num;
3128 const char *model_rev;
3129 unsigned long horkage;
3130};
3131
3132static const struct ata_blacklist_entry ata_device_blacklist [] = {
3133 /* Devices with DMA related problems under Linux */
3134 { "WDC AC11000H", NULL, ATA_HORKAGE_NODMA },
3135 { "WDC AC22100H", NULL, ATA_HORKAGE_NODMA },
3136 { "WDC AC32500H", NULL, ATA_HORKAGE_NODMA },
3137 { "WDC AC33100H", NULL, ATA_HORKAGE_NODMA },
3138 { "WDC AC31600H", NULL, ATA_HORKAGE_NODMA },
3139 { "WDC AC32100H", "24.09P07", ATA_HORKAGE_NODMA },
3140 { "WDC AC23200L", "21.10N21", ATA_HORKAGE_NODMA },
3141 { "Compaq CRD-8241B", NULL, ATA_HORKAGE_NODMA },
3142 { "CRD-8400B", NULL, ATA_HORKAGE_NODMA },
3143 { "CRD-8480B", NULL, ATA_HORKAGE_NODMA },
3144 { "CRD-8482B", NULL, ATA_HORKAGE_NODMA },
3145 { "CRD-84", NULL, ATA_HORKAGE_NODMA },
3146 { "SanDisk SDP3B", NULL, ATA_HORKAGE_NODMA },
3147 { "SanDisk SDP3B-64", NULL, ATA_HORKAGE_NODMA },
3148 { "SANYO CD-ROM CRD", NULL, ATA_HORKAGE_NODMA },
3149 { "HITACHI CDR-8", NULL, ATA_HORKAGE_NODMA },
3150 { "HITACHI CDR-8335", NULL, ATA_HORKAGE_NODMA },
3151 { "HITACHI CDR-8435", NULL, ATA_HORKAGE_NODMA },
3152 { "Toshiba CD-ROM XM-6202B", NULL, ATA_HORKAGE_NODMA },
3153 { "TOSHIBA CD-ROM XM-1702BC", NULL, ATA_HORKAGE_NODMA },
3154 { "CD-532E-A", NULL, ATA_HORKAGE_NODMA },
3155 { "E-IDE CD-ROM CR-840",NULL, ATA_HORKAGE_NODMA },
3156 { "CD-ROM Drive/F5A", NULL, ATA_HORKAGE_NODMA },
3157 { "WPI CDD-820", NULL, ATA_HORKAGE_NODMA },
3158 { "SAMSUNG CD-ROM SC-148C", NULL, ATA_HORKAGE_NODMA },
3159 { "SAMSUNG CD-ROM SC", NULL, ATA_HORKAGE_NODMA },
3160 { "SanDisk SDP3B-64", NULL, ATA_HORKAGE_NODMA },
3161 { "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,ATA_HORKAGE_NODMA },
3162 { "_NEC DV5800A", NULL, ATA_HORKAGE_NODMA },
3163 { "SAMSUNG CD-ROM SN-124","N001", ATA_HORKAGE_NODMA },
3164
3165 /* Devices we expect to fail diagnostics */
3166
3167 /* Devices where NCQ should be avoided */
3168 /* NCQ is slow */
3169 { "WDC WD740ADFD-00", NULL, ATA_HORKAGE_NONCQ },
3170
3171 /* Devices with NCQ limits */
3172
3173 /* End Marker */
3174 { }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003175};
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05003176
Alan Coxf4b15fe2006-03-22 15:54:04 +00003177static int ata_strim(char *s, size_t len)
3178{
3179 len = strnlen(s, len);
3180
3181 /* ATAPI specifies that empty space is blank-filled; remove blanks */
3182 while ((len > 0) && (s[len - 1] == ' ')) {
3183 len--;
3184 s[len] = 0;
3185 }
3186 return len;
3187}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188
Alan Cox6919a0a2006-10-27 19:08:46 -07003189unsigned long ata_device_blacklisted(const struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190{
Alan Coxf4b15fe2006-03-22 15:54:04 +00003191 unsigned char model_num[40];
3192 unsigned char model_rev[16];
3193 unsigned int nlen, rlen;
Alan Cox6919a0a2006-10-27 19:08:46 -07003194 const struct ata_blacklist_entry *ad = ata_device_blacklist;
Albert Lee3a778272006-06-22 13:00:25 +08003195
Alan Coxf4b15fe2006-03-22 15:54:04 +00003196 ata_id_string(dev->id, model_num, ATA_ID_PROD_OFS,
3197 sizeof(model_num));
3198 ata_id_string(dev->id, model_rev, ATA_ID_FW_REV_OFS,
3199 sizeof(model_rev));
3200 nlen = ata_strim(model_num, sizeof(model_num));
3201 rlen = ata_strim(model_rev, sizeof(model_rev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003202
Alan Cox6919a0a2006-10-27 19:08:46 -07003203 while (ad->model_num) {
3204 if (!strncmp(ad->model_num, model_num, nlen)) {
3205 if (ad->model_rev == NULL)
3206 return ad->horkage;
3207 if (!strncmp(ad->model_rev, model_rev, rlen))
3208 return ad->horkage;
Alan Coxf4b15fe2006-03-22 15:54:04 +00003209 }
Alan Cox6919a0a2006-10-27 19:08:46 -07003210 ad++;
Alan Coxf4b15fe2006-03-22 15:54:04 +00003211 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 return 0;
3213}
3214
Alan Cox6919a0a2006-10-27 19:08:46 -07003215static int ata_dma_blacklisted(const struct ata_device *dev)
3216{
3217 /* We don't support polling DMA.
3218 * DMA blacklist those ATAPI devices with CDB-intr (and use PIO)
3219 * if the LLDD handles only interrupts in the HSM_ST_LAST state.
3220 */
3221 if ((dev->ap->flags & ATA_FLAG_PIO_POLLING) &&
3222 (dev->flags & ATA_DFLAG_CDB_INTR))
3223 return 1;
3224 return (ata_device_blacklisted(dev) & ATA_HORKAGE_NODMA) ? 1 : 0;
3225}
3226
Tejun Heoa6d5a512006-03-06 04:31:57 +09003227/**
3228 * ata_dev_xfermask - Compute supported xfermask of the given device
Tejun Heoa6d5a512006-03-06 04:31:57 +09003229 * @dev: Device to compute xfermask for
3230 *
Tejun Heoacf356b2006-03-24 14:07:50 +09003231 * Compute supported xfermask of @dev and store it in
3232 * dev->*_mask. This function is responsible for applying all
3233 * known limits including host controller limits, device
3234 * blacklist, etc...
Tejun Heoa6d5a512006-03-06 04:31:57 +09003235 *
3236 * LOCKING:
3237 * None.
Tejun Heoa6d5a512006-03-06 04:31:57 +09003238 */
Tejun Heo3373efd2006-05-15 20:57:53 +09003239static void ata_dev_xfermask(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240{
Tejun Heo3373efd2006-05-15 20:57:53 +09003241 struct ata_port *ap = dev->ap;
Jeff Garzikcca39742006-08-24 03:19:22 -04003242 struct ata_host *host = ap->host;
Tejun Heoa6d5a512006-03-06 04:31:57 +09003243 unsigned long xfer_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244
Tejun Heo37deecb2006-08-10 16:59:07 +09003245 /* controller modes available */
Tejun Heo565083e2006-04-02 17:54:47 +09003246 xfer_mask = ata_pack_xfermask(ap->pio_mask,
3247 ap->mwdma_mask, ap->udma_mask);
3248
3249 /* Apply cable rule here. Don't apply it early because when
3250 * we handle hot plug the cable type can itself change.
3251 */
3252 if (ap->cbl == ATA_CBL_PATA40)
3253 xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
Alan Coxfc085152006-10-10 14:28:11 -07003254 /* Apply drive side cable rule. Unknown or 80 pin cables reported
3255 * host side are checked drive side as well. Cases where we know a
3256 * 40wire cable is used safely for 80 are not checked here.
3257 */
3258 if (ata_drive_40wire(dev->id) && (ap->cbl == ATA_CBL_PATA_UNK || ap->cbl == ATA_CBL_PATA80))
3259 xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
3260
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261
Tejun Heo37deecb2006-08-10 16:59:07 +09003262 xfer_mask &= ata_pack_xfermask(dev->pio_mask,
3263 dev->mwdma_mask, dev->udma_mask);
3264 xfer_mask &= ata_id_xfermask(dev->id);
Tejun Heo565083e2006-04-02 17:54:47 +09003265
Alan Coxb352e572006-08-10 18:52:12 +01003266 /*
3267 * CFA Advanced TrueIDE timings are not allowed on a shared
3268 * cable
3269 */
3270 if (ata_dev_pair(dev)) {
3271 /* No PIO5 or PIO6 */
3272 xfer_mask &= ~(0x03 << (ATA_SHIFT_PIO + 5));
3273 /* No MWDMA3 or MWDMA 4 */
3274 xfer_mask &= ~(0x03 << (ATA_SHIFT_MWDMA + 3));
3275 }
3276
Tejun Heo37deecb2006-08-10 16:59:07 +09003277 if (ata_dma_blacklisted(dev)) {
3278 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
Tejun Heof15a1da2006-05-15 20:57:56 +09003279 ata_dev_printk(dev, KERN_WARNING,
3280 "device is on DMA blacklist, disabling DMA\n");
Tejun Heo37deecb2006-08-10 16:59:07 +09003281 }
Tejun Heoa6d5a512006-03-06 04:31:57 +09003282
Jeff Garzikcca39742006-08-24 03:19:22 -04003283 if ((host->flags & ATA_HOST_SIMPLEX) && host->simplex_claimed) {
Tejun Heo37deecb2006-08-10 16:59:07 +09003284 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
3285 ata_dev_printk(dev, KERN_WARNING, "simplex DMA is claimed by "
3286 "other device, disabling DMA\n");
Alan Cox5444a6f2006-03-27 18:58:20 +01003287 }
Tejun Heo565083e2006-04-02 17:54:47 +09003288
Alan Cox5444a6f2006-03-27 18:58:20 +01003289 if (ap->ops->mode_filter)
3290 xfer_mask = ap->ops->mode_filter(ap, dev, xfer_mask);
3291
Tejun Heo565083e2006-04-02 17:54:47 +09003292 ata_unpack_xfermask(xfer_mask, &dev->pio_mask,
3293 &dev->mwdma_mask, &dev->udma_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003294}
3295
Linus Torvalds1da177e2005-04-16 15:20:36 -07003296/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07003297 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
Linus Torvalds1da177e2005-04-16 15:20:36 -07003298 * @dev: Device to which command will be sent
3299 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04003300 * Issue SET FEATURES - XFER MODE command to device @dev
3301 * on port @ap.
3302 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003303 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003304 * PCI/etc. bus probe sem.
Tejun Heo83206a22006-03-24 15:25:31 +09003305 *
3306 * RETURNS:
3307 * 0 on success, AC_ERR_* mask otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 */
3309
Tejun Heo3373efd2006-05-15 20:57:53 +09003310static unsigned int ata_dev_set_xfermode(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003311{
Tejun Heoa0123702005-12-13 14:49:31 +09003312 struct ata_taskfile tf;
Tejun Heo83206a22006-03-24 15:25:31 +09003313 unsigned int err_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314
3315 /* set up set-features taskfile */
3316 DPRINTK("set features - xfer mode\n");
3317
Tejun Heo3373efd2006-05-15 20:57:53 +09003318 ata_tf_init(dev, &tf);
Tejun Heoa0123702005-12-13 14:49:31 +09003319 tf.command = ATA_CMD_SET_FEATURES;
3320 tf.feature = SETFEATURES_XFER;
3321 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
3322 tf.protocol = ATA_PROT_NODATA;
3323 tf.nsect = dev->xfer_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324
Tejun Heo3373efd2006-05-15 20:57:53 +09003325 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003326
Tejun Heo83206a22006-03-24 15:25:31 +09003327 DPRINTK("EXIT, err_mask=%x\n", err_mask);
3328 return err_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003329}
3330
3331/**
Albert Lee8bf62ec2005-05-12 15:29:42 -04003332 * ata_dev_init_params - Issue INIT DEV PARAMS command
Albert Lee8bf62ec2005-05-12 15:29:42 -04003333 * @dev: Device to which command will be sent
Randy Dunlape2a7f772006-05-18 10:50:18 -07003334 * @heads: Number of heads (taskfile parameter)
3335 * @sectors: Number of sectors (taskfile parameter)
Albert Lee8bf62ec2005-05-12 15:29:42 -04003336 *
3337 * LOCKING:
Tejun Heo6aff8f12006-02-15 18:24:09 +09003338 * Kernel thread context (may sleep)
3339 *
3340 * RETURNS:
3341 * 0 on success, AC_ERR_* mask otherwise.
Albert Lee8bf62ec2005-05-12 15:29:42 -04003342 */
Tejun Heo3373efd2006-05-15 20:57:53 +09003343static unsigned int ata_dev_init_params(struct ata_device *dev,
3344 u16 heads, u16 sectors)
Albert Lee8bf62ec2005-05-12 15:29:42 -04003345{
Tejun Heoa0123702005-12-13 14:49:31 +09003346 struct ata_taskfile tf;
Tejun Heo6aff8f12006-02-15 18:24:09 +09003347 unsigned int err_mask;
Albert Lee8bf62ec2005-05-12 15:29:42 -04003348
3349 /* Number of sectors per track 1-255. Number of heads 1-16 */
3350 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
Albert Lee00b6f5e2006-03-27 16:39:18 +08003351 return AC_ERR_INVALID;
Albert Lee8bf62ec2005-05-12 15:29:42 -04003352
3353 /* set up init dev params taskfile */
3354 DPRINTK("init dev params \n");
3355
Tejun Heo3373efd2006-05-15 20:57:53 +09003356 ata_tf_init(dev, &tf);
Tejun Heoa0123702005-12-13 14:49:31 +09003357 tf.command = ATA_CMD_INIT_DEV_PARAMS;
3358 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
3359 tf.protocol = ATA_PROT_NODATA;
3360 tf.nsect = sectors;
3361 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
Albert Lee8bf62ec2005-05-12 15:29:42 -04003362
Tejun Heo3373efd2006-05-15 20:57:53 +09003363 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
Albert Lee8bf62ec2005-05-12 15:29:42 -04003364
Tejun Heo6aff8f12006-02-15 18:24:09 +09003365 DPRINTK("EXIT, err_mask=%x\n", err_mask);
3366 return err_mask;
Albert Lee8bf62ec2005-05-12 15:29:42 -04003367}
3368
3369/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04003370 * ata_sg_clean - Unmap DMA memory associated with command
3371 * @qc: Command containing DMA memory to be released
3372 *
3373 * Unmap all mapped DMA memory associated with this command.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 *
3375 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04003376 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 */
3378
3379static void ata_sg_clean(struct ata_queued_cmd *qc)
3380{
3381 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003382 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003384 void *pad_buf = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385
Tejun Heoa4631472006-02-11 19:11:13 +09003386 WARN_ON(!(qc->flags & ATA_QCFLAG_DMAMAP));
3387 WARN_ON(sg == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003388
3389 if (qc->flags & ATA_QCFLAG_SINGLE)
Jeff Garzikf1318832006-02-20 16:55:56 -05003390 WARN_ON(qc->n_elem > 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05003392 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003394 /* if we padded the buffer out to 32-bit bound, and data
3395 * xfer direction is from-device, we must copy from the
3396 * pad buffer back into the supplied buffer
3397 */
3398 if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
3399 pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
3400
3401 if (qc->flags & ATA_QCFLAG_SG) {
Jeff Garzike1410f22005-11-14 14:06:26 -05003402 if (qc->n_elem)
Brian King2f1f6102006-03-23 17:30:15 -06003403 dma_unmap_sg(ap->dev, sg, qc->n_elem, dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003404 /* restore last sg */
3405 sg[qc->orig_n_elem - 1].length += qc->pad_len;
3406 if (pad_buf) {
3407 struct scatterlist *psg = &qc->pad_sgent;
3408 void *addr = kmap_atomic(psg->page, KM_IRQ0);
3409 memcpy(addr + psg->offset, pad_buf, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05003410 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003411 }
3412 } else {
Tejun Heo2e242fa2006-02-20 23:48:38 +09003413 if (qc->n_elem)
Brian King2f1f6102006-03-23 17:30:15 -06003414 dma_unmap_single(ap->dev,
Jeff Garzike1410f22005-11-14 14:06:26 -05003415 sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
3416 dir);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003417 /* restore sg */
3418 sg->length += qc->pad_len;
3419 if (pad_buf)
3420 memcpy(qc->buf_virt + sg->length - qc->pad_len,
3421 pad_buf, qc->pad_len);
3422 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423
3424 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003425 qc->__sg = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426}
3427
3428/**
3429 * ata_fill_sg - Fill PCI IDE PRD table
3430 * @qc: Metadata associated with taskfile to be transferred
3431 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04003432 * Fill PCI IDE PRD (scatter-gather) table with segments
3433 * associated with the current disk command.
3434 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04003436 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437 *
3438 */
3439static void ata_fill_sg(struct ata_queued_cmd *qc)
3440{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003441 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003442 struct scatterlist *sg;
3443 unsigned int idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444
Tejun Heoa4631472006-02-11 19:11:13 +09003445 WARN_ON(qc->__sg == NULL);
Jeff Garzikf1318832006-02-20 16:55:56 -05003446 WARN_ON(qc->n_elem == 0 && qc->pad_len == 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447
3448 idx = 0;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003449 ata_for_each_sg(sg, qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003450 u32 addr, offset;
3451 u32 sg_len, len;
3452
3453 /* determine if physical DMA addr spans 64K boundary.
3454 * Note h/w doesn't support 64-bit, so we unconditionally
3455 * truncate dma_addr_t to u32.
3456 */
3457 addr = (u32) sg_dma_address(sg);
3458 sg_len = sg_dma_len(sg);
3459
3460 while (sg_len) {
3461 offset = addr & 0xffff;
3462 len = sg_len;
3463 if ((offset + sg_len) > 0x10000)
3464 len = 0x10000 - offset;
3465
3466 ap->prd[idx].addr = cpu_to_le32(addr);
3467 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
3468 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
3469
3470 idx++;
3471 sg_len -= len;
3472 addr += len;
3473 }
3474 }
3475
3476 if (idx)
3477 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
3478}
3479/**
3480 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
3481 * @qc: Metadata associated with taskfile to check
3482 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04003483 * Allow low-level driver to filter ATA PACKET commands, returning
3484 * a status indicating whether or not it is OK to use DMA for the
3485 * supplied PACKET command.
3486 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003487 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04003488 * spin_lock_irqsave(host lock)
Jeff Garzik0cba6322005-05-30 19:49:12 -04003489 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 * RETURNS: 0 when ATAPI DMA can be used
3491 * nonzero otherwise
3492 */
3493int ata_check_atapi_dma(struct ata_queued_cmd *qc)
3494{
3495 struct ata_port *ap = qc->ap;
3496 int rc = 0; /* Assume ATAPI DMA is OK by default */
3497
3498 if (ap->ops->check_atapi_dma)
3499 rc = ap->ops->check_atapi_dma(qc);
3500
3501 return rc;
3502}
3503/**
3504 * ata_qc_prep - Prepare taskfile for submission
3505 * @qc: Metadata associated with taskfile to be prepared
3506 *
Jeff Garzik780a87f2005-05-30 15:41:05 -04003507 * Prepare ATA taskfile for submission.
3508 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07003509 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04003510 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003511 */
3512void ata_qc_prep(struct ata_queued_cmd *qc)
3513{
3514 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
3515 return;
3516
3517 ata_fill_sg(qc);
3518}
3519
Brian Kinge46834c2006-03-17 17:04:03 -06003520void ata_noop_qc_prep(struct ata_queued_cmd *qc) { }
3521
Jeff Garzik0cba6322005-05-30 19:49:12 -04003522/**
3523 * ata_sg_init_one - Associate command with memory buffer
3524 * @qc: Command to be associated
3525 * @buf: Memory buffer
3526 * @buflen: Length of memory buffer, in bytes.
3527 *
3528 * Initialize the data-related elements of queued_cmd @qc
3529 * to point to a single memory buffer, @buf of byte length @buflen.
3530 *
3531 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04003532 * spin_lock_irqsave(host lock)
Jeff Garzik0cba6322005-05-30 19:49:12 -04003533 */
3534
Linus Torvalds1da177e2005-04-16 15:20:36 -07003535void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
3536{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003537 qc->flags |= ATA_QCFLAG_SINGLE;
3538
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003539 qc->__sg = &qc->sgent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540 qc->n_elem = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003541 qc->orig_n_elem = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542 qc->buf_virt = buf;
Brian King233277c2006-06-07 11:25:31 -05003543 qc->nbytes = buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544
Tejun Heo61c05962006-11-14 22:35:43 +09003545 sg_init_one(&qc->sgent, buf, buflen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546}
3547
Jeff Garzik0cba6322005-05-30 19:49:12 -04003548/**
3549 * ata_sg_init - Associate command with scatter-gather table.
3550 * @qc: Command to be associated
3551 * @sg: Scatter-gather table.
3552 * @n_elem: Number of elements in s/g table.
3553 *
3554 * Initialize the data-related elements of queued_cmd @qc
3555 * to point to a scatter-gather table @sg, containing @n_elem
3556 * elements.
3557 *
3558 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04003559 * spin_lock_irqsave(host lock)
Jeff Garzik0cba6322005-05-30 19:49:12 -04003560 */
3561
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
3563 unsigned int n_elem)
3564{
3565 qc->flags |= ATA_QCFLAG_SG;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003566 qc->__sg = sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567 qc->n_elem = n_elem;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003568 qc->orig_n_elem = n_elem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569}
3570
3571/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04003572 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
3573 * @qc: Command with memory buffer to be mapped.
3574 *
3575 * DMA-map the memory buffer associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576 *
3577 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04003578 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579 *
3580 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003581 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003582 */
3583
3584static int ata_sg_setup_one(struct ata_queued_cmd *qc)
3585{
3586 struct ata_port *ap = qc->ap;
3587 int dir = qc->dma_dir;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003588 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589 dma_addr_t dma_address;
Tejun Heo2e242fa2006-02-20 23:48:38 +09003590 int trim_sg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003591
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003592 /* we must lengthen transfers to end on a 32-bit boundary */
3593 qc->pad_len = sg->length & 3;
3594 if (qc->pad_len) {
3595 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
3596 struct scatterlist *psg = &qc->pad_sgent;
3597
Tejun Heoa4631472006-02-11 19:11:13 +09003598 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003599
3600 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
3601
3602 if (qc->tf.flags & ATA_TFLAG_WRITE)
3603 memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
3604 qc->pad_len);
3605
3606 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
3607 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
3608 /* trim sg */
3609 sg->length -= qc->pad_len;
Tejun Heo2e242fa2006-02-20 23:48:38 +09003610 if (sg->length == 0)
3611 trim_sg = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003612
3613 DPRINTK("padding done, sg->length=%u pad_len=%u\n",
3614 sg->length, qc->pad_len);
3615 }
3616
Tejun Heo2e242fa2006-02-20 23:48:38 +09003617 if (trim_sg) {
3618 qc->n_elem--;
Jeff Garzike1410f22005-11-14 14:06:26 -05003619 goto skip_map;
3620 }
3621
Brian King2f1f6102006-03-23 17:30:15 -06003622 dma_address = dma_map_single(ap->dev, qc->buf_virt,
Albert Lee32529e02005-05-26 03:49:42 -04003623 sg->length, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05003624 if (dma_mapping_error(dma_address)) {
3625 /* restore sg */
3626 sg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003627 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05003628 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629
3630 sg_dma_address(sg) = dma_address;
Albert Lee32529e02005-05-26 03:49:42 -04003631 sg_dma_len(sg) = sg->length;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003632
Tejun Heo2e242fa2006-02-20 23:48:38 +09003633skip_map:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003634 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
3635 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3636
3637 return 0;
3638}
3639
3640/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04003641 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
3642 * @qc: Command with scatter-gather table to be mapped.
3643 *
3644 * DMA-map the scatter-gather table associated with queued_cmd @qc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645 *
3646 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04003647 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003648 *
3649 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04003650 * Zero on success, negative on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003651 *
3652 */
3653
3654static int ata_sg_setup(struct ata_queued_cmd *qc)
3655{
3656 struct ata_port *ap = qc->ap;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003657 struct scatterlist *sg = qc->__sg;
3658 struct scatterlist *lsg = &sg[qc->n_elem - 1];
Jeff Garzike1410f22005-11-14 14:06:26 -05003659 int n_elem, pre_n_elem, dir, trim_sg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660
3661 VPRINTK("ENTER, ata%u\n", ap->id);
Tejun Heoa4631472006-02-11 19:11:13 +09003662 WARN_ON(!(qc->flags & ATA_QCFLAG_SG));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003663
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003664 /* we must lengthen transfers to end on a 32-bit boundary */
3665 qc->pad_len = lsg->length & 3;
3666 if (qc->pad_len) {
3667 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
3668 struct scatterlist *psg = &qc->pad_sgent;
3669 unsigned int offset;
3670
Tejun Heoa4631472006-02-11 19:11:13 +09003671 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003672
3673 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
3674
3675 /*
3676 * psg->page/offset are used to copy to-be-written
3677 * data in this function or read data in ata_sg_clean.
3678 */
3679 offset = lsg->offset + lsg->length - qc->pad_len;
3680 psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
3681 psg->offset = offset_in_page(offset);
3682
3683 if (qc->tf.flags & ATA_TFLAG_WRITE) {
3684 void *addr = kmap_atomic(psg->page, KM_IRQ0);
3685 memcpy(pad_buf, addr + psg->offset, qc->pad_len);
Mark Lorddfa15982005-12-12 23:19:28 -05003686 kunmap_atomic(addr, KM_IRQ0);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003687 }
3688
3689 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
3690 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
3691 /* trim last sg */
3692 lsg->length -= qc->pad_len;
Jeff Garzike1410f22005-11-14 14:06:26 -05003693 if (lsg->length == 0)
3694 trim_sg = 1;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003695
3696 DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
3697 qc->n_elem - 1, lsg->length, qc->pad_len);
3698 }
3699
Jeff Garzike1410f22005-11-14 14:06:26 -05003700 pre_n_elem = qc->n_elem;
3701 if (trim_sg && pre_n_elem)
3702 pre_n_elem--;
3703
3704 if (!pre_n_elem) {
3705 n_elem = 0;
3706 goto skip_map;
3707 }
3708
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709 dir = qc->dma_dir;
Brian King2f1f6102006-03-23 17:30:15 -06003710 n_elem = dma_map_sg(ap->dev, sg, pre_n_elem, dir);
Tejun Heo537a95d2005-11-05 14:29:01 -05003711 if (n_elem < 1) {
3712 /* restore last sg */
3713 lsg->length += qc->pad_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003714 return -1;
Tejun Heo537a95d2005-11-05 14:29:01 -05003715 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003716
3717 DPRINTK("%d sg elements mapped\n", n_elem);
3718
Jeff Garzike1410f22005-11-14 14:06:26 -05003719skip_map:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720 qc->n_elem = n_elem;
3721
3722 return 0;
3723}
3724
3725/**
Randy Dunlapc893a3a2006-01-28 13:15:32 -05003726 * swap_buf_le16 - swap halves of 16-bit words in place
Edward Falk0baab862005-06-02 18:17:13 -04003727 * @buf: Buffer to swap
3728 * @buf_words: Number of 16-bit words in buffer.
3729 *
3730 * Swap halves of 16-bit words if needed to convert from
3731 * little-endian byte order to native cpu byte order, or
3732 * vice-versa.
3733 *
3734 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04003735 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04003736 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003737void swap_buf_le16(u16 *buf, unsigned int buf_words)
3738{
3739#ifdef __BIG_ENDIAN
3740 unsigned int i;
3741
3742 for (i = 0; i < buf_words; i++)
3743 buf[i] = le16_to_cpu(buf[i]);
3744#endif /* __BIG_ENDIAN */
3745}
3746
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003747/**
3748 * ata_mmio_data_xfer - Transfer data by MMIO
Jeff Garzikbf717b112006-06-13 20:27:03 -04003749 * @adev: device for this I/O
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003750 * @buf: data buffer
3751 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04003752 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003753 *
3754 * Transfer data from/to the device data register by MMIO.
3755 *
3756 * LOCKING:
3757 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003758 */
3759
Tejun Heo88574552006-06-25 20:00:35 +09003760void ata_mmio_data_xfer(struct ata_device *adev, unsigned char *buf,
Alan Coxa6b2c5d2006-05-22 16:59:59 +01003761 unsigned int buflen, int write_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003762{
Alan Coxa6b2c5d2006-05-22 16:59:59 +01003763 struct ata_port *ap = adev->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003764 unsigned int i;
3765 unsigned int words = buflen >> 1;
3766 u16 *buf16 = (u16 *) buf;
3767 void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
3768
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003769 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003770 if (write_data) {
3771 for (i = 0; i < words; i++)
3772 writew(le16_to_cpu(buf16[i]), mmio);
3773 } else {
3774 for (i = 0; i < words; i++)
3775 buf16[i] = cpu_to_le16(readw(mmio));
3776 }
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003777
3778 /* Transfer trailing 1 byte, if any. */
3779 if (unlikely(buflen & 0x01)) {
3780 u16 align_buf[1] = { 0 };
3781 unsigned char *trailing_buf = buf + buflen - 1;
3782
3783 if (write_data) {
3784 memcpy(align_buf, trailing_buf, 1);
3785 writew(le16_to_cpu(align_buf[0]), mmio);
3786 } else {
3787 align_buf[0] = cpu_to_le16(readw(mmio));
3788 memcpy(trailing_buf, align_buf, 1);
3789 }
3790 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003791}
3792
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003793/**
3794 * ata_pio_data_xfer - Transfer data by PIO
Alan Coxa6b2c5d2006-05-22 16:59:59 +01003795 * @adev: device to target
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003796 * @buf: data buffer
3797 * @buflen: buffer length
Jeff Garzik344baba2005-09-07 01:15:17 -04003798 * @write_data: read/write
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003799 *
3800 * Transfer data from/to the device data register by PIO.
3801 *
3802 * LOCKING:
3803 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003804 */
3805
Tejun Heo88574552006-06-25 20:00:35 +09003806void ata_pio_data_xfer(struct ata_device *adev, unsigned char *buf,
Alan Coxa6b2c5d2006-05-22 16:59:59 +01003807 unsigned int buflen, int write_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808{
Alan Coxa6b2c5d2006-05-22 16:59:59 +01003809 struct ata_port *ap = adev->ap;
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003810 unsigned int words = buflen >> 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003812 /* Transfer multiple of 2 bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813 if (write_data)
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003814 outsw(ap->ioaddr.data_addr, buf, words);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815 else
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003816 insw(ap->ioaddr.data_addr, buf, words);
3817
3818 /* Transfer trailing 1 byte, if any. */
3819 if (unlikely(buflen & 0x01)) {
3820 u16 align_buf[1] = { 0 };
3821 unsigned char *trailing_buf = buf + buflen - 1;
3822
3823 if (write_data) {
3824 memcpy(align_buf, trailing_buf, 1);
3825 outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
3826 } else {
3827 align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr));
3828 memcpy(trailing_buf, align_buf, 1);
3829 }
3830 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831}
3832
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003833/**
Alan Cox75e99582006-05-24 14:14:41 +01003834 * ata_pio_data_xfer_noirq - Transfer data by PIO
3835 * @adev: device to target
3836 * @buf: data buffer
3837 * @buflen: buffer length
3838 * @write_data: read/write
3839 *
Tejun Heo88574552006-06-25 20:00:35 +09003840 * Transfer data from/to the device data register by PIO. Do the
Alan Cox75e99582006-05-24 14:14:41 +01003841 * transfer with interrupts disabled.
3842 *
3843 * LOCKING:
3844 * Inherited from caller.
3845 */
3846
3847void ata_pio_data_xfer_noirq(struct ata_device *adev, unsigned char *buf,
3848 unsigned int buflen, int write_data)
3849{
3850 unsigned long flags;
3851 local_irq_save(flags);
3852 ata_pio_data_xfer(adev, buf, buflen, write_data);
3853 local_irq_restore(flags);
3854}
3855
3856
3857/**
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003858 * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data.
3859 * @qc: Command on going
3860 *
3861 * Transfer ATA_SECT_SIZE of data from/to the ATA device.
3862 *
3863 * LOCKING:
3864 * Inherited from caller.
3865 */
3866
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867static void ata_pio_sector(struct ata_queued_cmd *qc)
3868{
3869 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003870 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871 struct ata_port *ap = qc->ap;
3872 struct page *page;
3873 unsigned int offset;
3874 unsigned char *buf;
3875
3876 if (qc->cursect == (qc->nsect - 1))
Albert Lee14be71f2005-09-27 17:36:35 +08003877 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003878
3879 page = sg[qc->cursg].page;
3880 offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE;
3881
3882 /* get the current page and offset */
3883 page = nth_page(page, (offset >> PAGE_SHIFT));
3884 offset %= PAGE_SIZE;
3885
Albert Lee7282aa42005-10-09 09:46:07 -04003886 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
3887
Albert Lee91b8b312005-10-09 09:48:44 -04003888 if (PageHighMem(page)) {
3889 unsigned long flags;
Albert Lee7282aa42005-10-09 09:46:07 -04003890
Alan Coxa6b2c5d2006-05-22 16:59:59 +01003891 /* FIXME: use a bounce buffer */
Albert Lee91b8b312005-10-09 09:48:44 -04003892 local_irq_save(flags);
3893 buf = kmap_atomic(page, KM_IRQ0);
Albert Lee083958d2005-10-09 09:47:31 -04003894
Albert Lee91b8b312005-10-09 09:48:44 -04003895 /* do the actual data transfer */
Alan Coxa6b2c5d2006-05-22 16:59:59 +01003896 ap->ops->data_xfer(qc->dev, buf + offset, ATA_SECT_SIZE, do_write);
Albert Lee91b8b312005-10-09 09:48:44 -04003897
3898 kunmap_atomic(buf, KM_IRQ0);
3899 local_irq_restore(flags);
3900 } else {
3901 buf = page_address(page);
Alan Coxa6b2c5d2006-05-22 16:59:59 +01003902 ap->ops->data_xfer(qc->dev, buf + offset, ATA_SECT_SIZE, do_write);
Albert Lee91b8b312005-10-09 09:48:44 -04003903 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904
3905 qc->cursect++;
3906 qc->cursg_ofs++;
3907
Albert Lee32529e02005-05-26 03:49:42 -04003908 if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909 qc->cursg++;
3910 qc->cursg_ofs = 0;
3911 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003913
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003914/**
Albert Lee07f6f7d2005-11-01 19:33:20 +08003915 * ata_pio_sectors - Transfer one or many 512-byte sectors.
3916 * @qc: Command on going
3917 *
Jeff Garzikc81e29b2006-05-24 01:49:12 -04003918 * Transfer one or many ATA_SECT_SIZE of data from/to the
Albert Lee07f6f7d2005-11-01 19:33:20 +08003919 * ATA device for the DRQ request.
3920 *
3921 * LOCKING:
3922 * Inherited from caller.
3923 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003924
Albert Lee07f6f7d2005-11-01 19:33:20 +08003925static void ata_pio_sectors(struct ata_queued_cmd *qc)
3926{
3927 if (is_multi_taskfile(&qc->tf)) {
3928 /* READ/WRITE MULTIPLE */
3929 unsigned int nsect;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003930
Jeff Garzik587005d2006-02-11 18:17:32 -05003931 WARN_ON(qc->dev->multi_count == 0);
Albert Lee07f6f7d2005-11-01 19:33:20 +08003932
3933 nsect = min(qc->nsect - qc->cursect, qc->dev->multi_count);
3934 while (nsect--)
3935 ata_pio_sector(qc);
3936 } else
3937 ata_pio_sector(qc);
3938}
3939
3940/**
Albert Leec71c1852005-10-04 06:03:45 -04003941 * atapi_send_cdb - Write CDB bytes to hardware
3942 * @ap: Port to which ATAPI device is attached.
3943 * @qc: Taskfile currently active
3944 *
3945 * When device has indicated its readiness to accept
3946 * a CDB, this function is called. Send the CDB.
3947 *
3948 * LOCKING:
3949 * caller.
3950 */
3951
3952static void atapi_send_cdb(struct ata_port *ap, struct ata_queued_cmd *qc)
3953{
3954 /* send SCSI cdb */
3955 DPRINTK("send cdb\n");
Jeff Garzikdb024d52006-02-13 00:23:57 -05003956 WARN_ON(qc->dev->cdb_len < 12);
Albert Leec71c1852005-10-04 06:03:45 -04003957
Alan Coxa6b2c5d2006-05-22 16:59:59 +01003958 ap->ops->data_xfer(qc->dev, qc->cdb, qc->dev->cdb_len, 1);
Albert Leec71c1852005-10-04 06:03:45 -04003959 ata_altstatus(ap); /* flush */
3960
3961 switch (qc->tf.protocol) {
3962 case ATA_PROT_ATAPI:
3963 ap->hsm_task_state = HSM_ST;
3964 break;
3965 case ATA_PROT_ATAPI_NODATA:
3966 ap->hsm_task_state = HSM_ST_LAST;
3967 break;
3968 case ATA_PROT_ATAPI_DMA:
3969 ap->hsm_task_state = HSM_ST_LAST;
3970 /* initiate bmdma */
3971 ap->ops->bmdma_start(qc);
3972 break;
3973 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974}
3975
Albert Lee6ae4cfb2005-08-12 14:15:34 +08003976/**
3977 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
3978 * @qc: Command on going
3979 * @bytes: number of bytes
3980 *
3981 * Transfer Transfer data from/to the ATAPI device.
3982 *
3983 * LOCKING:
3984 * Inherited from caller.
3985 *
3986 */
3987
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
3989{
3990 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
Jeff Garzikcedc9a42005-10-05 07:13:30 -04003991 struct scatterlist *sg = qc->__sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992 struct ata_port *ap = qc->ap;
3993 struct page *page;
3994 unsigned char *buf;
3995 unsigned int offset, count;
3996
Albert Lee563a6e12005-08-12 14:17:50 +08003997 if (qc->curbytes + bytes >= qc->nbytes)
Albert Lee14be71f2005-09-27 17:36:35 +08003998 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999
4000next_sg:
Albert Lee563a6e12005-08-12 14:17:50 +08004001 if (unlikely(qc->cursg >= qc->n_elem)) {
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04004002 /*
Albert Lee563a6e12005-08-12 14:17:50 +08004003 * The end of qc->sg is reached and the device expects
4004 * more data to transfer. In order not to overrun qc->sg
4005 * and fulfill length specified in the byte count register,
4006 * - for read case, discard trailing data from the device
4007 * - for write case, padding zero data to the device
4008 */
4009 u16 pad_buf[1] = { 0 };
4010 unsigned int words = bytes >> 1;
4011 unsigned int i;
4012
4013 if (words) /* warning if bytes > 1 */
Tejun Heof15a1da2006-05-15 20:57:56 +09004014 ata_dev_printk(qc->dev, KERN_WARNING,
4015 "%u bytes trailing data\n", bytes);
Albert Lee563a6e12005-08-12 14:17:50 +08004016
4017 for (i = 0; i < words; i++)
Alan Coxa6b2c5d2006-05-22 16:59:59 +01004018 ap->ops->data_xfer(qc->dev, (unsigned char*)pad_buf, 2, do_write);
Albert Lee563a6e12005-08-12 14:17:50 +08004019
Albert Lee14be71f2005-09-27 17:36:35 +08004020 ap->hsm_task_state = HSM_ST_LAST;
Albert Lee563a6e12005-08-12 14:17:50 +08004021 return;
4022 }
4023
Jeff Garzikcedc9a42005-10-05 07:13:30 -04004024 sg = &qc->__sg[qc->cursg];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025
Linus Torvalds1da177e2005-04-16 15:20:36 -07004026 page = sg->page;
4027 offset = sg->offset + qc->cursg_ofs;
4028
4029 /* get the current page and offset */
4030 page = nth_page(page, (offset >> PAGE_SHIFT));
4031 offset %= PAGE_SIZE;
4032
Albert Lee6952df02005-06-06 15:56:03 +08004033 /* don't overrun current sg */
Albert Lee32529e02005-05-26 03:49:42 -04004034 count = min(sg->length - qc->cursg_ofs, bytes);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004035
4036 /* don't cross page boundaries */
4037 count = min(count, (unsigned int)PAGE_SIZE - offset);
4038
Albert Lee7282aa42005-10-09 09:46:07 -04004039 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
4040
Albert Lee91b8b312005-10-09 09:48:44 -04004041 if (PageHighMem(page)) {
4042 unsigned long flags;
Albert Lee7282aa42005-10-09 09:46:07 -04004043
Alan Coxa6b2c5d2006-05-22 16:59:59 +01004044 /* FIXME: use bounce buffer */
Albert Lee91b8b312005-10-09 09:48:44 -04004045 local_irq_save(flags);
4046 buf = kmap_atomic(page, KM_IRQ0);
Albert Lee083958d2005-10-09 09:47:31 -04004047
Albert Lee91b8b312005-10-09 09:48:44 -04004048 /* do the actual data transfer */
Alan Coxa6b2c5d2006-05-22 16:59:59 +01004049 ap->ops->data_xfer(qc->dev, buf + offset, count, do_write);
Albert Lee91b8b312005-10-09 09:48:44 -04004050
4051 kunmap_atomic(buf, KM_IRQ0);
4052 local_irq_restore(flags);
4053 } else {
4054 buf = page_address(page);
Alan Coxa6b2c5d2006-05-22 16:59:59 +01004055 ap->ops->data_xfer(qc->dev, buf + offset, count, do_write);
Albert Lee91b8b312005-10-09 09:48:44 -04004056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004057
4058 bytes -= count;
4059 qc->curbytes += count;
4060 qc->cursg_ofs += count;
4061
Albert Lee32529e02005-05-26 03:49:42 -04004062 if (qc->cursg_ofs == sg->length) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004063 qc->cursg++;
4064 qc->cursg_ofs = 0;
4065 }
4066
Albert Lee563a6e12005-08-12 14:17:50 +08004067 if (bytes)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068 goto next_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069}
4070
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004071/**
4072 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
4073 * @qc: Command on going
4074 *
4075 * Transfer Transfer data from/to the ATAPI device.
4076 *
4077 * LOCKING:
4078 * Inherited from caller.
Albert Lee6ae4cfb2005-08-12 14:15:34 +08004079 */
4080
Linus Torvalds1da177e2005-04-16 15:20:36 -07004081static void atapi_pio_bytes(struct ata_queued_cmd *qc)
4082{
4083 struct ata_port *ap = qc->ap;
4084 struct ata_device *dev = qc->dev;
4085 unsigned int ireason, bc_lo, bc_hi, bytes;
4086 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
4087
Albert Leeeec4c3f2006-05-18 17:51:10 +08004088 /* Abuse qc->result_tf for temp storage of intermediate TF
4089 * here to save some kernel stack usage.
4090 * For normal completion, qc->result_tf is not relevant. For
4091 * error, qc->result_tf is later overwritten by ata_qc_complete().
4092 * So, the correctness of qc->result_tf is not affected.
4093 */
4094 ap->ops->tf_read(ap, &qc->result_tf);
4095 ireason = qc->result_tf.nsect;
4096 bc_lo = qc->result_tf.lbam;
4097 bc_hi = qc->result_tf.lbah;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004098 bytes = (bc_hi << 8) | bc_lo;
4099
4100 /* shall be cleared to zero, indicating xfer of data */
4101 if (ireason & (1 << 0))
4102 goto err_out;
4103
4104 /* make sure transfer direction matches expected */
4105 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
4106 if (do_write != i_write)
4107 goto err_out;
4108
Albert Lee312f7da2005-09-27 17:38:03 +08004109 VPRINTK("ata%u: xfering %d bytes\n", ap->id, bytes);
4110
Linus Torvalds1da177e2005-04-16 15:20:36 -07004111 __atapi_pio_bytes(qc, bytes);
4112
4113 return;
4114
4115err_out:
Tejun Heof15a1da2006-05-15 20:57:56 +09004116 ata_dev_printk(dev, KERN_INFO, "ATAPI check failed\n");
Tejun Heo11a56d22006-01-23 13:09:36 +09004117 qc->err_mask |= AC_ERR_HSM;
Albert Lee14be71f2005-09-27 17:36:35 +08004118 ap->hsm_task_state = HSM_ST_ERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004119}
4120
4121/**
Albert Leec234fb02006-03-25 17:58:38 +08004122 * ata_hsm_ok_in_wq - Check if the qc can be handled in the workqueue.
4123 * @ap: the target ata_port
4124 * @qc: qc on going
Linus Torvalds1da177e2005-04-16 15:20:36 -07004125 *
Albert Leec234fb02006-03-25 17:58:38 +08004126 * RETURNS:
4127 * 1 if ok in workqueue, 0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128 */
Albert Leec234fb02006-03-25 17:58:38 +08004129
4130static inline int ata_hsm_ok_in_wq(struct ata_port *ap, struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131{
Albert Leec234fb02006-03-25 17:58:38 +08004132 if (qc->tf.flags & ATA_TFLAG_POLLING)
4133 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134
Albert Leec234fb02006-03-25 17:58:38 +08004135 if (ap->hsm_task_state == HSM_ST_FIRST) {
4136 if (qc->tf.protocol == ATA_PROT_PIO &&
4137 (qc->tf.flags & ATA_TFLAG_WRITE))
4138 return 1;
4139
4140 if (is_atapi_taskfile(&qc->tf) &&
4141 !(qc->dev->flags & ATA_DFLAG_CDB_INTR))
4142 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004143 }
4144
Albert Leec234fb02006-03-25 17:58:38 +08004145 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146}
4147
Albert Leec234fb02006-03-25 17:58:38 +08004148/**
Tejun Heoc17ea202006-05-15 20:59:29 +09004149 * ata_hsm_qc_complete - finish a qc running on standard HSM
4150 * @qc: Command to complete
4151 * @in_wq: 1 if called from workqueue, 0 otherwise
4152 *
4153 * Finish @qc which is running on standard HSM.
4154 *
4155 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004156 * If @in_wq is zero, spin_lock_irqsave(host lock).
Tejun Heoc17ea202006-05-15 20:59:29 +09004157 * Otherwise, none on entry and grabs host lock.
4158 */
4159static void ata_hsm_qc_complete(struct ata_queued_cmd *qc, int in_wq)
4160{
4161 struct ata_port *ap = qc->ap;
4162 unsigned long flags;
4163
4164 if (ap->ops->error_handler) {
4165 if (in_wq) {
Jeff Garzikba6a1302006-06-22 23:46:10 -04004166 spin_lock_irqsave(ap->lock, flags);
Tejun Heoc17ea202006-05-15 20:59:29 +09004167
Jeff Garzikcca39742006-08-24 03:19:22 -04004168 /* EH might have kicked in while host lock is
4169 * released.
Tejun Heoc17ea202006-05-15 20:59:29 +09004170 */
4171 qc = ata_qc_from_tag(ap, qc->tag);
4172 if (qc) {
4173 if (likely(!(qc->err_mask & AC_ERR_HSM))) {
4174 ata_irq_on(ap);
4175 ata_qc_complete(qc);
4176 } else
4177 ata_port_freeze(ap);
4178 }
4179
Jeff Garzikba6a1302006-06-22 23:46:10 -04004180 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoc17ea202006-05-15 20:59:29 +09004181 } else {
4182 if (likely(!(qc->err_mask & AC_ERR_HSM)))
4183 ata_qc_complete(qc);
4184 else
4185 ata_port_freeze(ap);
4186 }
4187 } else {
4188 if (in_wq) {
Jeff Garzikba6a1302006-06-22 23:46:10 -04004189 spin_lock_irqsave(ap->lock, flags);
Tejun Heoc17ea202006-05-15 20:59:29 +09004190 ata_irq_on(ap);
4191 ata_qc_complete(qc);
Jeff Garzikba6a1302006-06-22 23:46:10 -04004192 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heoc17ea202006-05-15 20:59:29 +09004193 } else
4194 ata_qc_complete(qc);
4195 }
Jeff Garzikc81e29b2006-05-24 01:49:12 -04004196
4197 ata_altstatus(ap); /* flush */
Tejun Heoc17ea202006-05-15 20:59:29 +09004198}
4199
4200/**
Albert Leebb5cb292006-03-25 17:48:02 +08004201 * ata_hsm_move - move the HSM to the next state.
4202 * @ap: the target ata_port
4203 * @qc: qc on going
4204 * @status: current device status
4205 * @in_wq: 1 if called from workqueue, 0 otherwise
4206 *
4207 * RETURNS:
4208 * 1 when poll next status needed, 0 otherwise.
4209 */
Tejun Heo9a1004d2006-05-31 18:27:52 +09004210int ata_hsm_move(struct ata_port *ap, struct ata_queued_cmd *qc,
4211 u8 status, int in_wq)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212{
Albert Leebb5cb292006-03-25 17:48:02 +08004213 unsigned long flags = 0;
4214 int poll_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215
Albert Lee6912ccd2006-03-25 17:45:49 +08004216 WARN_ON((qc->flags & ATA_QCFLAG_ACTIVE) == 0);
Albert Lee0565c262006-02-13 18:55:25 +08004217
Albert Leebb5cb292006-03-25 17:48:02 +08004218 /* Make sure ata_qc_issue_prot() does not throw things
4219 * like DMA polling into the workqueue. Notice that
4220 * in_wq is not equivalent to (qc->tf.flags & ATA_TFLAG_POLLING).
Albert Lee1c848982005-12-05 15:40:15 +08004221 */
Albert Leec234fb02006-03-25 17:58:38 +08004222 WARN_ON(in_wq != ata_hsm_ok_in_wq(ap, qc));
Albert Lee1c848982005-12-05 15:40:15 +08004223
Albert Leee2cec772006-03-25 17:43:49 +08004224fsm_start:
Albert Lee999bb6f2006-03-25 18:07:48 +08004225 DPRINTK("ata%u: protocol %d task_state %d (dev_stat 0x%X)\n",
4226 ap->id, qc->tf.protocol, ap->hsm_task_state, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227
Albert Leee2cec772006-03-25 17:43:49 +08004228 switch (ap->hsm_task_state) {
4229 case HSM_ST_FIRST:
Albert Leebb5cb292006-03-25 17:48:02 +08004230 /* Send first data block or PACKET CDB */
4231
4232 /* If polling, we will stay in the work queue after
4233 * sending the data. Otherwise, interrupt handler
4234 * takes over after sending the data.
4235 */
4236 poll_next = (qc->tf.flags & ATA_TFLAG_POLLING);
4237
Albert Leee2cec772006-03-25 17:43:49 +08004238 /* check device status */
Albert Lee3655d1d2006-05-19 11:43:04 +08004239 if (unlikely((status & ATA_DRQ) == 0)) {
4240 /* handle BSY=0, DRQ=0 as error */
4241 if (likely(status & (ATA_ERR | ATA_DF)))
4242 /* device stops HSM for abort/error */
4243 qc->err_mask |= AC_ERR_DEV;
4244 else
4245 /* HSM violation. Let EH handle this */
4246 qc->err_mask |= AC_ERR_HSM;
4247
Albert Leee2cec772006-03-25 17:43:49 +08004248 ap->hsm_task_state = HSM_ST_ERR;
4249 goto fsm_start;
4250 }
4251
Albert Lee71601952006-03-25 18:11:12 +08004252 /* Device should not ask for data transfer (DRQ=1)
4253 * when it finds something wrong.
Albert Leeeee6c322006-04-01 17:38:43 +08004254 * We ignore DRQ here and stop the HSM by
4255 * changing hsm_task_state to HSM_ST_ERR and
4256 * let the EH abort the command or reset the device.
Albert Lee71601952006-03-25 18:11:12 +08004257 */
4258 if (unlikely(status & (ATA_ERR | ATA_DF))) {
4259 printk(KERN_WARNING "ata%d: DRQ=1 with device error, dev_stat 0x%X\n",
4260 ap->id, status);
Albert Lee3655d1d2006-05-19 11:43:04 +08004261 qc->err_mask |= AC_ERR_HSM;
Albert Leeeee6c322006-04-01 17:38:43 +08004262 ap->hsm_task_state = HSM_ST_ERR;
4263 goto fsm_start;
Albert Lee71601952006-03-25 18:11:12 +08004264 }
4265
Albert Leebb5cb292006-03-25 17:48:02 +08004266 /* Send the CDB (atapi) or the first data block (ata pio out).
4267 * During the state transition, interrupt handler shouldn't
4268 * be invoked before the data transfer is complete and
4269 * hsm_task_state is changed. Hence, the following locking.
4270 */
4271 if (in_wq)
Jeff Garzikba6a1302006-06-22 23:46:10 -04004272 spin_lock_irqsave(ap->lock, flags);
Albert Leee2cec772006-03-25 17:43:49 +08004273
Albert Leebb5cb292006-03-25 17:48:02 +08004274 if (qc->tf.protocol == ATA_PROT_PIO) {
4275 /* PIO data out protocol.
4276 * send first data block.
4277 */
4278
4279 /* ata_pio_sectors() might change the state
4280 * to HSM_ST_LAST. so, the state is changed here
4281 * before ata_pio_sectors().
4282 */
4283 ap->hsm_task_state = HSM_ST;
4284 ata_pio_sectors(qc);
4285 ata_altstatus(ap); /* flush */
4286 } else
4287 /* send CDB */
4288 atapi_send_cdb(ap, qc);
4289
4290 if (in_wq)
Jeff Garzikba6a1302006-06-22 23:46:10 -04004291 spin_unlock_irqrestore(ap->lock, flags);
Albert Leebb5cb292006-03-25 17:48:02 +08004292
4293 /* if polling, ata_pio_task() handles the rest.
4294 * otherwise, interrupt handler takes over from here.
4295 */
Albert Leee2cec772006-03-25 17:43:49 +08004296 break;
4297
4298 case HSM_ST:
4299 /* complete command or read/write the data register */
4300 if (qc->tf.protocol == ATA_PROT_ATAPI) {
4301 /* ATAPI PIO protocol */
4302 if ((status & ATA_DRQ) == 0) {
Albert Lee3655d1d2006-05-19 11:43:04 +08004303 /* No more data to transfer or device error.
4304 * Device error will be tagged in HSM_ST_LAST.
4305 */
Albert Leee2cec772006-03-25 17:43:49 +08004306 ap->hsm_task_state = HSM_ST_LAST;
4307 goto fsm_start;
4308 }
4309
Albert Lee71601952006-03-25 18:11:12 +08004310 /* Device should not ask for data transfer (DRQ=1)
4311 * when it finds something wrong.
Albert Leeeee6c322006-04-01 17:38:43 +08004312 * We ignore DRQ here and stop the HSM by
4313 * changing hsm_task_state to HSM_ST_ERR and
4314 * let the EH abort the command or reset the device.
Albert Lee71601952006-03-25 18:11:12 +08004315 */
4316 if (unlikely(status & (ATA_ERR | ATA_DF))) {
4317 printk(KERN_WARNING "ata%d: DRQ=1 with device error, dev_stat 0x%X\n",
4318 ap->id, status);
Albert Lee3655d1d2006-05-19 11:43:04 +08004319 qc->err_mask |= AC_ERR_HSM;
Albert Leeeee6c322006-04-01 17:38:43 +08004320 ap->hsm_task_state = HSM_ST_ERR;
4321 goto fsm_start;
Albert Lee71601952006-03-25 18:11:12 +08004322 }
4323
Albert Leee2cec772006-03-25 17:43:49 +08004324 atapi_pio_bytes(qc);
4325
4326 if (unlikely(ap->hsm_task_state == HSM_ST_ERR))
4327 /* bad ireason reported by device */
4328 goto fsm_start;
4329
4330 } else {
4331 /* ATA PIO protocol */
4332 if (unlikely((status & ATA_DRQ) == 0)) {
4333 /* handle BSY=0, DRQ=0 as error */
Albert Lee3655d1d2006-05-19 11:43:04 +08004334 if (likely(status & (ATA_ERR | ATA_DF)))
4335 /* device stops HSM for abort/error */
4336 qc->err_mask |= AC_ERR_DEV;
4337 else
Tejun Heo55a8e2c2006-11-10 18:08:10 +09004338 /* HSM violation. Let EH handle this.
4339 * Phantom devices also trigger this
4340 * condition. Mark hint.
4341 */
4342 qc->err_mask |= AC_ERR_HSM |
4343 AC_ERR_NODEV_HINT;
Albert Lee3655d1d2006-05-19 11:43:04 +08004344
Albert Leee2cec772006-03-25 17:43:49 +08004345 ap->hsm_task_state = HSM_ST_ERR;
4346 goto fsm_start;
4347 }
4348
Albert Leeeee6c322006-04-01 17:38:43 +08004349 /* For PIO reads, some devices may ask for
4350 * data transfer (DRQ=1) alone with ERR=1.
4351 * We respect DRQ here and transfer one
4352 * block of junk data before changing the
4353 * hsm_task_state to HSM_ST_ERR.
4354 *
4355 * For PIO writes, ERR=1 DRQ=1 doesn't make
4356 * sense since the data block has been
4357 * transferred to the device.
Albert Lee71601952006-03-25 18:11:12 +08004358 */
4359 if (unlikely(status & (ATA_ERR | ATA_DF))) {
Albert Lee71601952006-03-25 18:11:12 +08004360 /* data might be corrputed */
4361 qc->err_mask |= AC_ERR_DEV;
Albert Leeeee6c322006-04-01 17:38:43 +08004362
4363 if (!(qc->tf.flags & ATA_TFLAG_WRITE)) {
4364 ata_pio_sectors(qc);
4365 ata_altstatus(ap);
4366 status = ata_wait_idle(ap);
4367 }
4368
Albert Lee3655d1d2006-05-19 11:43:04 +08004369 if (status & (ATA_BUSY | ATA_DRQ))
4370 qc->err_mask |= AC_ERR_HSM;
4371
Albert Leeeee6c322006-04-01 17:38:43 +08004372 /* ata_pio_sectors() might change the
4373 * state to HSM_ST_LAST. so, the state
4374 * is changed after ata_pio_sectors().
4375 */
4376 ap->hsm_task_state = HSM_ST_ERR;
4377 goto fsm_start;
Albert Lee71601952006-03-25 18:11:12 +08004378 }
4379
Albert Leee2cec772006-03-25 17:43:49 +08004380 ata_pio_sectors(qc);
4381
4382 if (ap->hsm_task_state == HSM_ST_LAST &&
4383 (!(qc->tf.flags & ATA_TFLAG_WRITE))) {
4384 /* all data read */
4385 ata_altstatus(ap);
Albert Lee52a32202006-03-25 18:18:15 +08004386 status = ata_wait_idle(ap);
Albert Leee2cec772006-03-25 17:43:49 +08004387 goto fsm_start;
4388 }
4389 }
4390
4391 ata_altstatus(ap); /* flush */
Albert Leebb5cb292006-03-25 17:48:02 +08004392 poll_next = 1;
Albert Leee2cec772006-03-25 17:43:49 +08004393 break;
4394
4395 case HSM_ST_LAST:
Albert Lee6912ccd2006-03-25 17:45:49 +08004396 if (unlikely(!ata_ok(status))) {
4397 qc->err_mask |= __ac_err_mask(status);
Albert Leee2cec772006-03-25 17:43:49 +08004398 ap->hsm_task_state = HSM_ST_ERR;
4399 goto fsm_start;
4400 }
4401
4402 /* no more data to transfer */
Albert Lee4332a772006-04-03 17:43:24 +08004403 DPRINTK("ata%u: dev %u command complete, drv_stat 0x%x\n",
4404 ap->id, qc->dev->devno, status);
Albert Leee2cec772006-03-25 17:43:49 +08004405
Albert Lee6912ccd2006-03-25 17:45:49 +08004406 WARN_ON(qc->err_mask);
4407
Albert Leee2cec772006-03-25 17:43:49 +08004408 ap->hsm_task_state = HSM_ST_IDLE;
4409
4410 /* complete taskfile transaction */
Tejun Heoc17ea202006-05-15 20:59:29 +09004411 ata_hsm_qc_complete(qc, in_wq);
Albert Leebb5cb292006-03-25 17:48:02 +08004412
4413 poll_next = 0;
Albert Leee2cec772006-03-25 17:43:49 +08004414 break;
4415
4416 case HSM_ST_ERR:
Albert Leee2cec772006-03-25 17:43:49 +08004417 /* make sure qc->err_mask is available to
4418 * know what's wrong and recover
4419 */
4420 WARN_ON(qc->err_mask == 0);
4421
4422 ap->hsm_task_state = HSM_ST_IDLE;
Albert Leebb5cb292006-03-25 17:48:02 +08004423
Albert Lee999bb6f2006-03-25 18:07:48 +08004424 /* complete taskfile transaction */
Tejun Heoc17ea202006-05-15 20:59:29 +09004425 ata_hsm_qc_complete(qc, in_wq);
Albert Leebb5cb292006-03-25 17:48:02 +08004426
4427 poll_next = 0;
Albert Leee2cec772006-03-25 17:43:49 +08004428 break;
4429 default:
Albert Leebb5cb292006-03-25 17:48:02 +08004430 poll_next = 0;
Albert Lee6912ccd2006-03-25 17:45:49 +08004431 BUG();
Albert Leee2cec772006-03-25 17:43:49 +08004432 }
4433
Albert Leebb5cb292006-03-25 17:48:02 +08004434 return poll_next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435}
4436
4437static void ata_pio_task(void *_data)
4438{
Tejun Heoc91af2c2006-04-02 18:51:53 +09004439 struct ata_queued_cmd *qc = _data;
4440 struct ata_port *ap = qc->ap;
Albert Leea1af3732006-03-25 17:50:15 +08004441 u8 status;
4442 int poll_next;
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04004443
4444fsm_start:
Albert Leea1af3732006-03-25 17:50:15 +08004445 WARN_ON(ap->hsm_task_state == HSM_ST_IDLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004446
Albert Leea1af3732006-03-25 17:50:15 +08004447 /*
4448 * This is purely heuristic. This is a fast path.
4449 * Sometimes when we enter, BSY will be cleared in
4450 * a chk-status or two. If not, the drive is probably seeking
4451 * or something. Snooze for a couple msecs, then
4452 * chk-status again. If still busy, queue delayed work.
4453 */
4454 status = ata_busy_wait(ap, ATA_BUSY, 5);
4455 if (status & ATA_BUSY) {
4456 msleep(2);
4457 status = ata_busy_wait(ap, ATA_BUSY, 10);
4458 if (status & ATA_BUSY) {
Albert Lee31ce6da2006-04-03 18:31:44 +08004459 ata_port_queue_task(ap, ata_pio_task, qc, ATA_SHORT_PAUSE);
Albert Leea1af3732006-03-25 17:50:15 +08004460 return;
4461 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462 }
4463
Albert Leea1af3732006-03-25 17:50:15 +08004464 /* move the HSM */
4465 poll_next = ata_hsm_move(ap, qc, status, 1);
4466
4467 /* another command or interrupt handler
4468 * may be running at this point.
4469 */
4470 if (poll_next)
Jeff Garzik7fb6ec22005-09-16 06:01:48 -04004471 goto fsm_start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472}
4473
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475 * ata_qc_new - Request an available ATA command, for queueing
4476 * @ap: Port associated with device @dev
4477 * @dev: Device from whom we request an available command structure
4478 *
4479 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004480 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481 */
4482
4483static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
4484{
4485 struct ata_queued_cmd *qc = NULL;
4486 unsigned int i;
4487
Tejun Heoe3180492006-05-15 20:58:09 +09004488 /* no command while frozen */
Tejun Heob51e9e52006-06-29 01:29:30 +09004489 if (unlikely(ap->pflags & ATA_PFLAG_FROZEN))
Tejun Heoe3180492006-05-15 20:58:09 +09004490 return NULL;
4491
Tejun Heo2ab7db12006-05-15 20:58:02 +09004492 /* the last tag is reserved for internal command. */
4493 for (i = 0; i < ATA_MAX_QUEUE - 1; i++)
Tejun Heo6cec4a32006-05-15 21:03:41 +09004494 if (!test_and_set_bit(i, &ap->qc_allocated)) {
Tejun Heof69499f2006-05-15 20:58:03 +09004495 qc = __ata_qc_from_tag(ap, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496 break;
4497 }
4498
4499 if (qc)
4500 qc->tag = i;
4501
4502 return qc;
4503}
4504
4505/**
4506 * ata_qc_new_init - Request an available ATA command, and initialize it
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507 * @dev: Device from whom we request an available command structure
4508 *
4509 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04004510 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511 */
4512
Tejun Heo3373efd2006-05-15 20:57:53 +09004513struct ata_queued_cmd *ata_qc_new_init(struct ata_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514{
Tejun Heo3373efd2006-05-15 20:57:53 +09004515 struct ata_port *ap = dev->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516 struct ata_queued_cmd *qc;
4517
4518 qc = ata_qc_new(ap);
4519 if (qc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520 qc->scsicmd = NULL;
4521 qc->ap = ap;
4522 qc->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004523
Jeff Garzik2c13b7c2005-11-14 14:14:16 -05004524 ata_qc_reinit(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004525 }
4526
4527 return qc;
4528}
4529
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530/**
4531 * ata_qc_free - free unused ata_queued_cmd
4532 * @qc: Command to complete
4533 *
4534 * Designed to free unused ata_queued_cmd object
4535 * in case something prevents using it.
4536 *
4537 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004538 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539 */
4540void ata_qc_free(struct ata_queued_cmd *qc)
4541{
Tejun Heo4ba946e2006-01-23 13:09:36 +09004542 struct ata_port *ap = qc->ap;
4543 unsigned int tag;
4544
Tejun Heoa4631472006-02-11 19:11:13 +09004545 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546
Tejun Heo4ba946e2006-01-23 13:09:36 +09004547 qc->flags = 0;
4548 tag = qc->tag;
4549 if (likely(ata_tag_valid(tag))) {
Tejun Heo4ba946e2006-01-23 13:09:36 +09004550 qc->tag = ATA_TAG_POISON;
Tejun Heo6cec4a32006-05-15 21:03:41 +09004551 clear_bit(tag, &ap->qc_allocated);
Tejun Heo4ba946e2006-01-23 13:09:36 +09004552 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553}
4554
Tejun Heo76014422006-02-11 15:13:49 +09004555void __ata_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556{
Tejun Heodedaf2b2006-05-15 21:03:43 +09004557 struct ata_port *ap = qc->ap;
4558
Tejun Heoa4631472006-02-11 19:11:13 +09004559 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
4560 WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
Linus Torvalds1da177e2005-04-16 15:20:36 -07004561
4562 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
4563 ata_sg_clean(qc);
4564
Tejun Heo7401abf2006-05-15 20:57:32 +09004565 /* command should be marked inactive atomically with qc completion */
Tejun Heodedaf2b2006-05-15 21:03:43 +09004566 if (qc->tf.protocol == ATA_PROT_NCQ)
4567 ap->sactive &= ~(1 << qc->tag);
4568 else
4569 ap->active_tag = ATA_TAG_POISON;
Tejun Heo7401abf2006-05-15 20:57:32 +09004570
Albert Lee3f3791d2005-08-16 14:25:38 +08004571 /* atapi: mark qc as inactive to prevent the interrupt handler
4572 * from completing the command twice later, before the error handler
4573 * is called. (when rc != 0 and atapi request sense is needed)
4574 */
4575 qc->flags &= ~ATA_QCFLAG_ACTIVE;
Tejun Heodedaf2b2006-05-15 21:03:43 +09004576 ap->qc_active &= ~(1 << qc->tag);
Albert Lee3f3791d2005-08-16 14:25:38 +08004577
Linus Torvalds1da177e2005-04-16 15:20:36 -07004578 /* call completion callback */
Tejun Heo77853bf2006-01-23 13:09:36 +09004579 qc->complete_fn(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580}
4581
Tejun Heo39599a52006-11-14 22:37:35 +09004582static void fill_result_tf(struct ata_queued_cmd *qc)
4583{
4584 struct ata_port *ap = qc->ap;
4585
4586 ap->ops->tf_read(ap, &qc->result_tf);
4587 qc->result_tf.flags = qc->tf.flags;
4588}
4589
Tejun Heof686bcb2006-05-15 20:58:05 +09004590/**
4591 * ata_qc_complete - Complete an active ATA command
4592 * @qc: Command to complete
4593 * @err_mask: ATA Status register contents
4594 *
4595 * Indicate to the mid and upper layers that an ATA
4596 * command has completed, with either an ok or not-ok status.
4597 *
4598 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004599 * spin_lock_irqsave(host lock)
Tejun Heof686bcb2006-05-15 20:58:05 +09004600 */
4601void ata_qc_complete(struct ata_queued_cmd *qc)
4602{
4603 struct ata_port *ap = qc->ap;
4604
4605 /* XXX: New EH and old EH use different mechanisms to
4606 * synchronize EH with regular execution path.
4607 *
4608 * In new EH, a failed qc is marked with ATA_QCFLAG_FAILED.
4609 * Normal execution path is responsible for not accessing a
4610 * failed qc. libata core enforces the rule by returning NULL
4611 * from ata_qc_from_tag() for failed qcs.
4612 *
4613 * Old EH depends on ata_qc_complete() nullifying completion
4614 * requests if ATA_QCFLAG_EH_SCHEDULED is set. Old EH does
4615 * not synchronize with interrupt handler. Only PIO task is
4616 * taken care of.
4617 */
4618 if (ap->ops->error_handler) {
Tejun Heob51e9e52006-06-29 01:29:30 +09004619 WARN_ON(ap->pflags & ATA_PFLAG_FROZEN);
Tejun Heof686bcb2006-05-15 20:58:05 +09004620
4621 if (unlikely(qc->err_mask))
4622 qc->flags |= ATA_QCFLAG_FAILED;
4623
4624 if (unlikely(qc->flags & ATA_QCFLAG_FAILED)) {
4625 if (!ata_tag_internal(qc->tag)) {
4626 /* always fill result TF for failed qc */
Tejun Heo39599a52006-11-14 22:37:35 +09004627 fill_result_tf(qc);
Tejun Heof686bcb2006-05-15 20:58:05 +09004628 ata_qc_schedule_eh(qc);
4629 return;
4630 }
4631 }
4632
4633 /* read result TF if requested */
4634 if (qc->flags & ATA_QCFLAG_RESULT_TF)
Tejun Heo39599a52006-11-14 22:37:35 +09004635 fill_result_tf(qc);
Tejun Heof686bcb2006-05-15 20:58:05 +09004636
4637 __ata_qc_complete(qc);
4638 } else {
4639 if (qc->flags & ATA_QCFLAG_EH_SCHEDULED)
4640 return;
4641
4642 /* read result TF if failed or requested */
4643 if (qc->err_mask || qc->flags & ATA_QCFLAG_RESULT_TF)
Tejun Heo39599a52006-11-14 22:37:35 +09004644 fill_result_tf(qc);
Tejun Heof686bcb2006-05-15 20:58:05 +09004645
4646 __ata_qc_complete(qc);
4647 }
4648}
4649
Tejun Heodedaf2b2006-05-15 21:03:43 +09004650/**
4651 * ata_qc_complete_multiple - Complete multiple qcs successfully
4652 * @ap: port in question
4653 * @qc_active: new qc_active mask
4654 * @finish_qc: LLDD callback invoked before completing a qc
4655 *
4656 * Complete in-flight commands. This functions is meant to be
4657 * called from low-level driver's interrupt routine to complete
4658 * requests normally. ap->qc_active and @qc_active is compared
4659 * and commands are completed accordingly.
4660 *
4661 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004662 * spin_lock_irqsave(host lock)
Tejun Heodedaf2b2006-05-15 21:03:43 +09004663 *
4664 * RETURNS:
4665 * Number of completed commands on success, -errno otherwise.
4666 */
4667int ata_qc_complete_multiple(struct ata_port *ap, u32 qc_active,
4668 void (*finish_qc)(struct ata_queued_cmd *))
4669{
4670 int nr_done = 0;
4671 u32 done_mask;
4672 int i;
4673
4674 done_mask = ap->qc_active ^ qc_active;
4675
4676 if (unlikely(done_mask & qc_active)) {
4677 ata_port_printk(ap, KERN_ERR, "illegal qc_active transition "
4678 "(%08x->%08x)\n", ap->qc_active, qc_active);
4679 return -EINVAL;
4680 }
4681
4682 for (i = 0; i < ATA_MAX_QUEUE; i++) {
4683 struct ata_queued_cmd *qc;
4684
4685 if (!(done_mask & (1 << i)))
4686 continue;
4687
4688 if ((qc = ata_qc_from_tag(ap, i))) {
4689 if (finish_qc)
4690 finish_qc(qc);
4691 ata_qc_complete(qc);
4692 nr_done++;
4693 }
4694 }
4695
4696 return nr_done;
4697}
4698
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
4700{
4701 struct ata_port *ap = qc->ap;
4702
4703 switch (qc->tf.protocol) {
Tejun Heo3dc1d882006-05-15 21:03:45 +09004704 case ATA_PROT_NCQ:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705 case ATA_PROT_DMA:
4706 case ATA_PROT_ATAPI_DMA:
4707 return 1;
4708
4709 case ATA_PROT_ATAPI:
4710 case ATA_PROT_PIO:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711 if (ap->flags & ATA_FLAG_PIO_DMA)
4712 return 1;
4713
4714 /* fall through */
4715
4716 default:
4717 return 0;
4718 }
4719
4720 /* never reached */
4721}
4722
4723/**
4724 * ata_qc_issue - issue taskfile to device
4725 * @qc: command to issue to device
4726 *
4727 * Prepare an ATA command to submission to device.
4728 * This includes mapping the data into a DMA-able
4729 * area, filling in the S/G table, and finally
4730 * writing the taskfile to hardware, starting the command.
4731 *
4732 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004733 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734 */
Tejun Heo8e0e6942006-03-31 20:41:11 +09004735void ata_qc_issue(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736{
4737 struct ata_port *ap = qc->ap;
4738
Tejun Heodedaf2b2006-05-15 21:03:43 +09004739 /* Make sure only one non-NCQ command is outstanding. The
4740 * check is skipped for old EH because it reuses active qc to
4741 * request ATAPI sense.
4742 */
4743 WARN_ON(ap->ops->error_handler && ata_tag_valid(ap->active_tag));
4744
4745 if (qc->tf.protocol == ATA_PROT_NCQ) {
4746 WARN_ON(ap->sactive & (1 << qc->tag));
4747 ap->sactive |= 1 << qc->tag;
4748 } else {
4749 WARN_ON(ap->sactive);
4750 ap->active_tag = qc->tag;
4751 }
4752
Tejun Heoe4a70e72006-03-31 20:36:47 +09004753 qc->flags |= ATA_QCFLAG_ACTIVE;
Tejun Heodedaf2b2006-05-15 21:03:43 +09004754 ap->qc_active |= 1 << qc->tag;
Tejun Heoe4a70e72006-03-31 20:36:47 +09004755
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756 if (ata_should_dma_map(qc)) {
4757 if (qc->flags & ATA_QCFLAG_SG) {
4758 if (ata_sg_setup(qc))
Tejun Heo8e436af2006-01-23 13:09:36 +09004759 goto sg_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004760 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
4761 if (ata_sg_setup_one(qc))
Tejun Heo8e436af2006-01-23 13:09:36 +09004762 goto sg_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763 }
4764 } else {
4765 qc->flags &= ~ATA_QCFLAG_DMAMAP;
4766 }
4767
4768 ap->ops->qc_prep(qc);
4769
Tejun Heo8e0e6942006-03-31 20:41:11 +09004770 qc->err_mask |= ap->ops->qc_issue(qc);
4771 if (unlikely(qc->err_mask))
4772 goto err;
4773 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004774
Tejun Heo8e436af2006-01-23 13:09:36 +09004775sg_err:
4776 qc->flags &= ~ATA_QCFLAG_DMAMAP;
Tejun Heo8e0e6942006-03-31 20:41:11 +09004777 qc->err_mask |= AC_ERR_SYSTEM;
4778err:
4779 ata_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780}
4781
4782/**
4783 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
4784 * @qc: command to issue to device
4785 *
4786 * Using various libata functions and hooks, this function
4787 * starts an ATA command. ATA commands are grouped into
4788 * classes called "protocols", and issuing each type of protocol
4789 * is slightly different.
4790 *
Edward Falk0baab862005-06-02 18:17:13 -04004791 * May be used as the qc_issue() entry in ata_port_operations.
4792 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004794 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004795 *
4796 * RETURNS:
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09004797 * Zero on success, AC_ERR_* mask on failure
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798 */
4799
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09004800unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801{
4802 struct ata_port *ap = qc->ap;
4803
Albert Leee50362e2005-09-27 17:39:50 +08004804 /* Use polling pio if the LLD doesn't handle
4805 * interrupt driven pio and atapi CDB interrupt.
4806 */
4807 if (ap->flags & ATA_FLAG_PIO_POLLING) {
4808 switch (qc->tf.protocol) {
4809 case ATA_PROT_PIO:
4810 case ATA_PROT_ATAPI:
4811 case ATA_PROT_ATAPI_NODATA:
4812 qc->tf.flags |= ATA_TFLAG_POLLING;
4813 break;
4814 case ATA_PROT_ATAPI_DMA:
4815 if (qc->dev->flags & ATA_DFLAG_CDB_INTR)
Albert Lee3a778272006-06-22 13:00:25 +08004816 /* see ata_dma_blacklisted() */
Albert Leee50362e2005-09-27 17:39:50 +08004817 BUG();
4818 break;
4819 default:
4820 break;
4821 }
4822 }
4823
Albert Lee312f7da2005-09-27 17:38:03 +08004824 /* select the device */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825 ata_dev_select(ap, qc->dev->devno, 1, 0);
4826
Albert Lee312f7da2005-09-27 17:38:03 +08004827 /* start the command */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004828 switch (qc->tf.protocol) {
4829 case ATA_PROT_NODATA:
Albert Lee312f7da2005-09-27 17:38:03 +08004830 if (qc->tf.flags & ATA_TFLAG_POLLING)
4831 ata_qc_set_polling(qc);
4832
Jeff Garzike5338252005-10-30 21:37:17 -05004833 ata_tf_to_host(ap, &qc->tf);
Albert Lee312f7da2005-09-27 17:38:03 +08004834 ap->hsm_task_state = HSM_ST_LAST;
4835
4836 if (qc->tf.flags & ATA_TFLAG_POLLING)
Albert Lee31ce6da2006-04-03 18:31:44 +08004837 ata_port_queue_task(ap, ata_pio_task, qc, 0);
Albert Lee312f7da2005-09-27 17:38:03 +08004838
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839 break;
4840
4841 case ATA_PROT_DMA:
Jeff Garzik587005d2006-02-11 18:17:32 -05004842 WARN_ON(qc->tf.flags & ATA_TFLAG_POLLING);
Albert Lee312f7da2005-09-27 17:38:03 +08004843
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
4845 ap->ops->bmdma_setup(qc); /* set up bmdma */
4846 ap->ops->bmdma_start(qc); /* initiate bmdma */
Albert Lee312f7da2005-09-27 17:38:03 +08004847 ap->hsm_task_state = HSM_ST_LAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848 break;
4849
Albert Lee312f7da2005-09-27 17:38:03 +08004850 case ATA_PROT_PIO:
4851 if (qc->tf.flags & ATA_TFLAG_POLLING)
4852 ata_qc_set_polling(qc);
4853
Jeff Garzike5338252005-10-30 21:37:17 -05004854 ata_tf_to_host(ap, &qc->tf);
Albert Lee312f7da2005-09-27 17:38:03 +08004855
Albert Lee54f00382005-09-30 19:14:19 +08004856 if (qc->tf.flags & ATA_TFLAG_WRITE) {
4857 /* PIO data out protocol */
4858 ap->hsm_task_state = HSM_ST_FIRST;
Albert Lee31ce6da2006-04-03 18:31:44 +08004859 ata_port_queue_task(ap, ata_pio_task, qc, 0);
Albert Lee54f00382005-09-30 19:14:19 +08004860
4861 /* always send first data block using
Albert Leee27486d2005-11-01 19:24:49 +08004862 * the ata_pio_task() codepath.
Albert Lee54f00382005-09-30 19:14:19 +08004863 */
Albert Lee312f7da2005-09-27 17:38:03 +08004864 } else {
Albert Lee54f00382005-09-30 19:14:19 +08004865 /* PIO data in protocol */
4866 ap->hsm_task_state = HSM_ST;
Albert Lee312f7da2005-09-27 17:38:03 +08004867
Albert Lee54f00382005-09-30 19:14:19 +08004868 if (qc->tf.flags & ATA_TFLAG_POLLING)
Albert Lee31ce6da2006-04-03 18:31:44 +08004869 ata_port_queue_task(ap, ata_pio_task, qc, 0);
Albert Lee312f7da2005-09-27 17:38:03 +08004870
Albert Lee54f00382005-09-30 19:14:19 +08004871 /* if polling, ata_pio_task() handles the rest.
4872 * otherwise, interrupt handler takes over from here.
4873 */
Albert Lee312f7da2005-09-27 17:38:03 +08004874 }
4875
Linus Torvalds1da177e2005-04-16 15:20:36 -07004876 break;
4877
4878 case ATA_PROT_ATAPI:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879 case ATA_PROT_ATAPI_NODATA:
Albert Lee312f7da2005-09-27 17:38:03 +08004880 if (qc->tf.flags & ATA_TFLAG_POLLING)
4881 ata_qc_set_polling(qc);
4882
Jeff Garzike5338252005-10-30 21:37:17 -05004883 ata_tf_to_host(ap, &qc->tf);
Jeff Garzikf6ef65e2006-01-27 02:45:00 -05004884
Albert Lee312f7da2005-09-27 17:38:03 +08004885 ap->hsm_task_state = HSM_ST_FIRST;
4886
4887 /* send cdb by polling if no cdb interrupt */
4888 if ((!(qc->dev->flags & ATA_DFLAG_CDB_INTR)) ||
4889 (qc->tf.flags & ATA_TFLAG_POLLING))
Albert Lee31ce6da2006-04-03 18:31:44 +08004890 ata_port_queue_task(ap, ata_pio_task, qc, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891 break;
4892
4893 case ATA_PROT_ATAPI_DMA:
Jeff Garzik587005d2006-02-11 18:17:32 -05004894 WARN_ON(qc->tf.flags & ATA_TFLAG_POLLING);
Albert Lee312f7da2005-09-27 17:38:03 +08004895
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
4897 ap->ops->bmdma_setup(qc); /* set up bmdma */
Albert Lee312f7da2005-09-27 17:38:03 +08004898 ap->hsm_task_state = HSM_ST_FIRST;
4899
4900 /* send cdb by polling if no cdb interrupt */
4901 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
Albert Lee31ce6da2006-04-03 18:31:44 +08004902 ata_port_queue_task(ap, ata_pio_task, qc, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903 break;
4904
4905 default:
4906 WARN_ON(1);
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09004907 return AC_ERR_SYSTEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908 }
4909
4910 return 0;
4911}
4912
4913/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07004914 * ata_host_intr - Handle host interrupt for given (port, task)
4915 * @ap: Port on which interrupt arrived (possibly...)
4916 * @qc: Taskfile currently active in engine
4917 *
4918 * Handle host interrupt for given queued command. Currently,
4919 * only DMA interrupts are handled. All other commands are
4920 * handled via polling with interrupts disabled (nIEN bit).
4921 *
4922 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04004923 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924 *
4925 * RETURNS:
4926 * One if interrupt was handled, zero if not (shared irq).
4927 */
4928
4929inline unsigned int ata_host_intr (struct ata_port *ap,
4930 struct ata_queued_cmd *qc)
4931{
Albert Lee312f7da2005-09-27 17:38:03 +08004932 u8 status, host_stat = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933
Albert Lee312f7da2005-09-27 17:38:03 +08004934 VPRINTK("ata%u: protocol %d task_state %d\n",
4935 ap->id, qc->tf.protocol, ap->hsm_task_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936
Albert Lee312f7da2005-09-27 17:38:03 +08004937 /* Check whether we are expecting interrupt in this state */
4938 switch (ap->hsm_task_state) {
4939 case HSM_ST_FIRST:
Albert Lee6912ccd2006-03-25 17:45:49 +08004940 /* Some pre-ATAPI-4 devices assert INTRQ
4941 * at this state when ready to receive CDB.
4942 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943
Albert Lee312f7da2005-09-27 17:38:03 +08004944 /* Check the ATA_DFLAG_CDB_INTR flag is enough here.
4945 * The flag was turned on only for atapi devices.
4946 * No need to check is_atapi_taskfile(&qc->tf) again.
4947 */
4948 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004950 break;
Albert Lee312f7da2005-09-27 17:38:03 +08004951 case HSM_ST_LAST:
4952 if (qc->tf.protocol == ATA_PROT_DMA ||
4953 qc->tf.protocol == ATA_PROT_ATAPI_DMA) {
4954 /* check status of DMA engine */
4955 host_stat = ap->ops->bmdma_status(ap);
4956 VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004957
Albert Lee312f7da2005-09-27 17:38:03 +08004958 /* if it's not our irq... */
4959 if (!(host_stat & ATA_DMA_INTR))
4960 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961
Albert Lee312f7da2005-09-27 17:38:03 +08004962 /* before we do anything else, clear DMA-Start bit */
4963 ap->ops->bmdma_stop(qc);
Albert Leea4f16612005-12-26 16:40:53 +08004964
4965 if (unlikely(host_stat & ATA_DMA_ERR)) {
4966 /* error when transfering data to/from memory */
4967 qc->err_mask |= AC_ERR_HOST_BUS;
4968 ap->hsm_task_state = HSM_ST_ERR;
4969 }
Albert Lee312f7da2005-09-27 17:38:03 +08004970 }
4971 break;
4972 case HSM_ST:
4973 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974 default:
4975 goto idle_irq;
4976 }
4977
Albert Lee312f7da2005-09-27 17:38:03 +08004978 /* check altstatus */
4979 status = ata_altstatus(ap);
4980 if (status & ATA_BUSY)
4981 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982
Albert Lee312f7da2005-09-27 17:38:03 +08004983 /* check main status, clearing INTRQ */
4984 status = ata_chk_status(ap);
4985 if (unlikely(status & ATA_BUSY))
4986 goto idle_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987
Albert Lee312f7da2005-09-27 17:38:03 +08004988 /* ack bmdma irq events */
4989 ap->ops->irq_clear(ap);
4990
Albert Leebb5cb292006-03-25 17:48:02 +08004991 ata_hsm_move(ap, qc, status, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992 return 1; /* irq handled */
4993
4994idle_irq:
4995 ap->stats.idle_irq++;
4996
4997#ifdef ATA_IRQ_TRAP
4998 if ((ap->stats.idle_irq % 1000) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004999 ata_irq_ack(ap, 0); /* debug trap */
Tejun Heof15a1da2006-05-15 20:57:56 +09005000 ata_port_printk(ap, KERN_WARNING, "irq trap\n");
Alan Cox23cfce82006-03-21 16:06:53 +00005001 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002 }
5003#endif
5004 return 0; /* irq not handled */
5005}
5006
5007/**
5008 * ata_interrupt - Default ATA host interrupt handler
Jeff Garzik0cba6322005-05-30 19:49:12 -04005009 * @irq: irq line (unused)
Jeff Garzikcca39742006-08-24 03:19:22 -04005010 * @dev_instance: pointer to our ata_host information structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07005011 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04005012 * Default interrupt handler for PCI IDE devices. Calls
5013 * ata_host_intr() for each port that is not disabled.
5014 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04005016 * Obtains host lock during operation.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017 *
5018 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04005019 * IRQ_NONE or IRQ_HANDLED.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005020 */
5021
David Howells7d12e782006-10-05 14:55:46 +01005022irqreturn_t ata_interrupt (int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023{
Jeff Garzikcca39742006-08-24 03:19:22 -04005024 struct ata_host *host = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025 unsigned int i;
5026 unsigned int handled = 0;
5027 unsigned long flags;
5028
5029 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
Jeff Garzikcca39742006-08-24 03:19:22 -04005030 spin_lock_irqsave(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031
Jeff Garzikcca39742006-08-24 03:19:22 -04005032 for (i = 0; i < host->n_ports; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033 struct ata_port *ap;
5034
Jeff Garzikcca39742006-08-24 03:19:22 -04005035 ap = host->ports[i];
Tejun Heoc1389502005-08-22 14:59:24 +09005036 if (ap &&
Jeff Garzik029f5462006-04-02 10:30:40 -04005037 !(ap->flags & ATA_FLAG_DISABLED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038 struct ata_queued_cmd *qc;
5039
5040 qc = ata_qc_from_tag(ap, ap->active_tag);
Albert Lee312f7da2005-09-27 17:38:03 +08005041 if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING)) &&
Albert Lee21b1ed72005-04-29 17:34:59 +08005042 (qc->flags & ATA_QCFLAG_ACTIVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043 handled |= ata_host_intr(ap, qc);
5044 }
5045 }
5046
Jeff Garzikcca39742006-08-24 03:19:22 -04005047 spin_unlock_irqrestore(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005048
5049 return IRQ_RETVAL(handled);
5050}
5051
Tejun Heo34bf2172006-05-15 20:57:46 +09005052/**
5053 * sata_scr_valid - test whether SCRs are accessible
5054 * @ap: ATA port to test SCR accessibility for
5055 *
5056 * Test whether SCRs are accessible for @ap.
5057 *
5058 * LOCKING:
5059 * None.
5060 *
5061 * RETURNS:
5062 * 1 if SCRs are accessible, 0 otherwise.
5063 */
5064int sata_scr_valid(struct ata_port *ap)
5065{
5066 return ap->cbl == ATA_CBL_SATA && ap->ops->scr_read;
5067}
5068
5069/**
5070 * sata_scr_read - read SCR register of the specified port
5071 * @ap: ATA port to read SCR for
5072 * @reg: SCR to read
5073 * @val: Place to store read value
5074 *
5075 * Read SCR register @reg of @ap into *@val. This function is
5076 * guaranteed to succeed if the cable type of the port is SATA
5077 * and the port implements ->scr_read.
5078 *
5079 * LOCKING:
5080 * None.
5081 *
5082 * RETURNS:
5083 * 0 on success, negative errno on failure.
5084 */
5085int sata_scr_read(struct ata_port *ap, int reg, u32 *val)
5086{
5087 if (sata_scr_valid(ap)) {
5088 *val = ap->ops->scr_read(ap, reg);
5089 return 0;
5090 }
5091 return -EOPNOTSUPP;
5092}
5093
5094/**
5095 * sata_scr_write - write SCR register of the specified port
5096 * @ap: ATA port to write SCR for
5097 * @reg: SCR to write
5098 * @val: value to write
5099 *
5100 * Write @val to SCR register @reg of @ap. This function is
5101 * guaranteed to succeed if the cable type of the port is SATA
5102 * and the port implements ->scr_read.
5103 *
5104 * LOCKING:
5105 * None.
5106 *
5107 * RETURNS:
5108 * 0 on success, negative errno on failure.
5109 */
5110int sata_scr_write(struct ata_port *ap, int reg, u32 val)
5111{
5112 if (sata_scr_valid(ap)) {
5113 ap->ops->scr_write(ap, reg, val);
5114 return 0;
5115 }
5116 return -EOPNOTSUPP;
5117}
5118
5119/**
5120 * sata_scr_write_flush - write SCR register of the specified port and flush
5121 * @ap: ATA port to write SCR for
5122 * @reg: SCR to write
5123 * @val: value to write
5124 *
5125 * This function is identical to sata_scr_write() except that this
5126 * function performs flush after writing to the register.
5127 *
5128 * LOCKING:
5129 * None.
5130 *
5131 * RETURNS:
5132 * 0 on success, negative errno on failure.
5133 */
5134int sata_scr_write_flush(struct ata_port *ap, int reg, u32 val)
5135{
5136 if (sata_scr_valid(ap)) {
5137 ap->ops->scr_write(ap, reg, val);
5138 ap->ops->scr_read(ap, reg);
5139 return 0;
5140 }
5141 return -EOPNOTSUPP;
5142}
5143
5144/**
5145 * ata_port_online - test whether the given port is online
5146 * @ap: ATA port to test
5147 *
5148 * Test whether @ap is online. Note that this function returns 0
5149 * if online status of @ap cannot be obtained, so
5150 * ata_port_online(ap) != !ata_port_offline(ap).
5151 *
5152 * LOCKING:
5153 * None.
5154 *
5155 * RETURNS:
5156 * 1 if the port online status is available and online.
5157 */
5158int ata_port_online(struct ata_port *ap)
5159{
5160 u32 sstatus;
5161
5162 if (!sata_scr_read(ap, SCR_STATUS, &sstatus) && (sstatus & 0xf) == 0x3)
5163 return 1;
5164 return 0;
5165}
5166
5167/**
5168 * ata_port_offline - test whether the given port is offline
5169 * @ap: ATA port to test
5170 *
5171 * Test whether @ap is offline. Note that this function returns
5172 * 0 if offline status of @ap cannot be obtained, so
5173 * ata_port_online(ap) != !ata_port_offline(ap).
5174 *
5175 * LOCKING:
5176 * None.
5177 *
5178 * RETURNS:
5179 * 1 if the port offline status is available and offline.
5180 */
5181int ata_port_offline(struct ata_port *ap)
5182{
5183 u32 sstatus;
5184
5185 if (!sata_scr_read(ap, SCR_STATUS, &sstatus) && (sstatus & 0xf) != 0x3)
5186 return 1;
5187 return 0;
5188}
Edward Falk0baab862005-06-02 18:17:13 -04005189
Tejun Heo77b08fb2006-06-24 20:30:19 +09005190int ata_flush_cache(struct ata_device *dev)
Jens Axboe9b847542006-01-06 09:28:07 +01005191{
Tejun Heo977e6b92006-06-24 20:30:19 +09005192 unsigned int err_mask;
Jens Axboe9b847542006-01-06 09:28:07 +01005193 u8 cmd;
5194
5195 if (!ata_try_flush_cache(dev))
5196 return 0;
5197
Tejun Heo6fc49ad2006-11-11 20:10:45 +09005198 if (dev->flags & ATA_DFLAG_FLUSH_EXT)
Jens Axboe9b847542006-01-06 09:28:07 +01005199 cmd = ATA_CMD_FLUSH_EXT;
5200 else
5201 cmd = ATA_CMD_FLUSH;
5202
Tejun Heo977e6b92006-06-24 20:30:19 +09005203 err_mask = ata_do_simple_cmd(dev, cmd);
5204 if (err_mask) {
5205 ata_dev_printk(dev, KERN_ERR, "failed to flush cache\n");
5206 return -EIO;
5207 }
5208
5209 return 0;
Jens Axboe9b847542006-01-06 09:28:07 +01005210}
5211
Jeff Garzikcca39742006-08-24 03:19:22 -04005212static int ata_host_request_pm(struct ata_host *host, pm_message_t mesg,
5213 unsigned int action, unsigned int ehi_flags,
5214 int wait)
Tejun Heo500530f2006-07-03 16:07:27 +09005215{
5216 unsigned long flags;
5217 int i, rc;
5218
Jeff Garzikcca39742006-08-24 03:19:22 -04005219 for (i = 0; i < host->n_ports; i++) {
5220 struct ata_port *ap = host->ports[i];
Tejun Heo500530f2006-07-03 16:07:27 +09005221
5222 /* Previous resume operation might still be in
5223 * progress. Wait for PM_PENDING to clear.
5224 */
5225 if (ap->pflags & ATA_PFLAG_PM_PENDING) {
5226 ata_port_wait_eh(ap);
5227 WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
5228 }
5229
5230 /* request PM ops to EH */
5231 spin_lock_irqsave(ap->lock, flags);
5232
5233 ap->pm_mesg = mesg;
5234 if (wait) {
5235 rc = 0;
5236 ap->pm_result = &rc;
5237 }
5238
5239 ap->pflags |= ATA_PFLAG_PM_PENDING;
5240 ap->eh_info.action |= action;
5241 ap->eh_info.flags |= ehi_flags;
5242
5243 ata_port_schedule_eh(ap);
5244
5245 spin_unlock_irqrestore(ap->lock, flags);
5246
5247 /* wait and check result */
5248 if (wait) {
5249 ata_port_wait_eh(ap);
5250 WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
5251 if (rc)
5252 return rc;
5253 }
5254 }
5255
5256 return 0;
5257}
5258
5259/**
Jeff Garzikcca39742006-08-24 03:19:22 -04005260 * ata_host_suspend - suspend host
5261 * @host: host to suspend
Tejun Heo500530f2006-07-03 16:07:27 +09005262 * @mesg: PM message
5263 *
Jeff Garzikcca39742006-08-24 03:19:22 -04005264 * Suspend @host. Actual operation is performed by EH. This
Tejun Heo500530f2006-07-03 16:07:27 +09005265 * function requests EH to perform PM operations and waits for EH
5266 * to finish.
5267 *
5268 * LOCKING:
5269 * Kernel thread context (may sleep).
5270 *
5271 * RETURNS:
5272 * 0 on success, -errno on failure.
5273 */
Jeff Garzikcca39742006-08-24 03:19:22 -04005274int ata_host_suspend(struct ata_host *host, pm_message_t mesg)
Tejun Heo500530f2006-07-03 16:07:27 +09005275{
5276 int i, j, rc;
5277
Jeff Garzikcca39742006-08-24 03:19:22 -04005278 rc = ata_host_request_pm(host, mesg, 0, ATA_EHI_QUIET, 1);
Tejun Heo500530f2006-07-03 16:07:27 +09005279 if (rc)
5280 goto fail;
5281
5282 /* EH is quiescent now. Fail if we have any ready device.
5283 * This happens if hotplug occurs between completion of device
5284 * suspension and here.
5285 */
Jeff Garzikcca39742006-08-24 03:19:22 -04005286 for (i = 0; i < host->n_ports; i++) {
5287 struct ata_port *ap = host->ports[i];
Tejun Heo500530f2006-07-03 16:07:27 +09005288
5289 for (j = 0; j < ATA_MAX_DEVICES; j++) {
5290 struct ata_device *dev = &ap->device[j];
5291
5292 if (ata_dev_ready(dev)) {
5293 ata_port_printk(ap, KERN_WARNING,
5294 "suspend failed, device %d "
5295 "still active\n", dev->devno);
5296 rc = -EBUSY;
5297 goto fail;
5298 }
5299 }
5300 }
5301
Jeff Garzikcca39742006-08-24 03:19:22 -04005302 host->dev->power.power_state = mesg;
Tejun Heo500530f2006-07-03 16:07:27 +09005303 return 0;
5304
5305 fail:
Jeff Garzikcca39742006-08-24 03:19:22 -04005306 ata_host_resume(host);
Tejun Heo500530f2006-07-03 16:07:27 +09005307 return rc;
5308}
5309
5310/**
Jeff Garzikcca39742006-08-24 03:19:22 -04005311 * ata_host_resume - resume host
5312 * @host: host to resume
Tejun Heo500530f2006-07-03 16:07:27 +09005313 *
Jeff Garzikcca39742006-08-24 03:19:22 -04005314 * Resume @host. Actual operation is performed by EH. This
Tejun Heo500530f2006-07-03 16:07:27 +09005315 * function requests EH to perform PM operations and returns.
5316 * Note that all resume operations are performed parallely.
5317 *
5318 * LOCKING:
5319 * Kernel thread context (may sleep).
5320 */
Jeff Garzikcca39742006-08-24 03:19:22 -04005321void ata_host_resume(struct ata_host *host)
Tejun Heo500530f2006-07-03 16:07:27 +09005322{
Jeff Garzikcca39742006-08-24 03:19:22 -04005323 ata_host_request_pm(host, PMSG_ON, ATA_EH_SOFTRESET,
5324 ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET, 0);
5325 host->dev->power.power_state = PMSG_ON;
Tejun Heo500530f2006-07-03 16:07:27 +09005326}
5327
Randy Dunlapc893a3a2006-01-28 13:15:32 -05005328/**
5329 * ata_port_start - Set port up for dma.
5330 * @ap: Port to initialize
5331 *
5332 * Called just after data structures for each port are
5333 * initialized. Allocates space for PRD table.
5334 *
5335 * May be used as the port_start() entry in ata_port_operations.
5336 *
5337 * LOCKING:
5338 * Inherited from caller.
5339 */
5340
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341int ata_port_start (struct ata_port *ap)
5342{
Brian King2f1f6102006-03-23 17:30:15 -06005343 struct device *dev = ap->dev;
Jeff Garzik6037d6b2005-11-04 22:08:00 -05005344 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005345
5346 ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL);
5347 if (!ap->prd)
5348 return -ENOMEM;
5349
Jeff Garzik6037d6b2005-11-04 22:08:00 -05005350 rc = ata_pad_alloc(ap, dev);
5351 if (rc) {
Jeff Garzikcedc9a42005-10-05 07:13:30 -04005352 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05005353 return rc;
Jeff Garzikcedc9a42005-10-05 07:13:30 -04005354 }
5355
Linus Torvalds1da177e2005-04-16 15:20:36 -07005356 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma);
5357
5358 return 0;
5359}
5360
Edward Falk0baab862005-06-02 18:17:13 -04005361
5362/**
5363 * ata_port_stop - Undo ata_port_start()
5364 * @ap: Port to shut down
5365 *
5366 * Frees the PRD table.
5367 *
5368 * May be used as the port_stop() entry in ata_port_operations.
5369 *
5370 * LOCKING:
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04005371 * Inherited from caller.
Edward Falk0baab862005-06-02 18:17:13 -04005372 */
5373
Linus Torvalds1da177e2005-04-16 15:20:36 -07005374void ata_port_stop (struct ata_port *ap)
5375{
Brian King2f1f6102006-03-23 17:30:15 -06005376 struct device *dev = ap->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005377
5378 dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05005379 ata_pad_free(ap, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005380}
5381
Jeff Garzikcca39742006-08-24 03:19:22 -04005382void ata_host_stop (struct ata_host *host)
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04005383{
Jeff Garzikcca39742006-08-24 03:19:22 -04005384 if (host->mmio_base)
5385 iounmap(host->mmio_base);
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04005386}
5387
Linus Torvalds1da177e2005-04-16 15:20:36 -07005388/**
Tejun Heo3ef3b432006-05-31 18:27:30 +09005389 * ata_dev_init - Initialize an ata_device structure
5390 * @dev: Device structure to initialize
5391 *
5392 * Initialize @dev in preparation for probing.
5393 *
5394 * LOCKING:
5395 * Inherited from caller.
5396 */
5397void ata_dev_init(struct ata_device *dev)
5398{
5399 struct ata_port *ap = dev->ap;
Tejun Heo72fa4b72006-05-31 18:27:32 +09005400 unsigned long flags;
Tejun Heo3ef3b432006-05-31 18:27:30 +09005401
Tejun Heo5a04bf42006-05-31 18:27:38 +09005402 /* SATA spd limit is bound to the first device */
5403 ap->sata_spd_limit = ap->hw_sata_spd_limit;
5404
Tejun Heo72fa4b72006-05-31 18:27:32 +09005405 /* High bits of dev->flags are used to record warm plug
5406 * requests which occur asynchronously. Synchronize using
Jeff Garzikcca39742006-08-24 03:19:22 -04005407 * host lock.
Tejun Heo72fa4b72006-05-31 18:27:32 +09005408 */
Jeff Garzikba6a1302006-06-22 23:46:10 -04005409 spin_lock_irqsave(ap->lock, flags);
Tejun Heo72fa4b72006-05-31 18:27:32 +09005410 dev->flags &= ~ATA_DFLAG_INIT_MASK;
Jeff Garzikba6a1302006-06-22 23:46:10 -04005411 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo72fa4b72006-05-31 18:27:32 +09005412
5413 memset((void *)dev + ATA_DEVICE_CLEAR_OFFSET, 0,
5414 sizeof(*dev) - ATA_DEVICE_CLEAR_OFFSET);
Tejun Heo3ef3b432006-05-31 18:27:30 +09005415 dev->pio_mask = UINT_MAX;
5416 dev->mwdma_mask = UINT_MAX;
5417 dev->udma_mask = UINT_MAX;
5418}
5419
5420/**
Brian King155a8a92006-08-07 14:27:17 -05005421 * ata_port_init - Initialize an ata_port structure
Linus Torvalds1da177e2005-04-16 15:20:36 -07005422 * @ap: Structure to initialize
Jeff Garzikcca39742006-08-24 03:19:22 -04005423 * @host: Collection of hosts to which @ap belongs
Linus Torvalds1da177e2005-04-16 15:20:36 -07005424 * @ent: Probe information provided by low-level driver
5425 * @port_no: Port number associated with this ata_port
5426 *
Brian King155a8a92006-08-07 14:27:17 -05005427 * Initialize a new ata_port structure.
Jeff Garzik0cba6322005-05-30 19:49:12 -04005428 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005429 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04005430 * Inherited from caller.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005431 */
Jeff Garzikcca39742006-08-24 03:19:22 -04005432void ata_port_init(struct ata_port *ap, struct ata_host *host,
Brian King155a8a92006-08-07 14:27:17 -05005433 const struct ata_probe_ent *ent, unsigned int port_no)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005434{
5435 unsigned int i;
5436
Jeff Garzikcca39742006-08-24 03:19:22 -04005437 ap->lock = &host->lock;
Tejun Heo198e0fe2006-04-02 18:51:52 +09005438 ap->flags = ATA_FLAG_DISABLED;
Brian King155a8a92006-08-07 14:27:17 -05005439 ap->id = ata_unique_id++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005440 ap->ctl = ATA_DEVCTL_OBS;
Jeff Garzikcca39742006-08-24 03:19:22 -04005441 ap->host = host;
Brian King2f1f6102006-03-23 17:30:15 -06005442 ap->dev = ent->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005443 ap->port_no = port_no;
Tejun Heofea63e32006-09-16 03:04:15 +09005444 if (port_no == 1 && ent->pinfo2) {
5445 ap->pio_mask = ent->pinfo2->pio_mask;
5446 ap->mwdma_mask = ent->pinfo2->mwdma_mask;
5447 ap->udma_mask = ent->pinfo2->udma_mask;
5448 ap->flags |= ent->pinfo2->flags;
5449 ap->ops = ent->pinfo2->port_ops;
5450 } else {
5451 ap->pio_mask = ent->pio_mask;
5452 ap->mwdma_mask = ent->mwdma_mask;
5453 ap->udma_mask = ent->udma_mask;
5454 ap->flags |= ent->port_flags;
5455 ap->ops = ent->port_ops;
5456 }
Tejun Heo5a04bf42006-05-31 18:27:38 +09005457 ap->hw_sata_spd_limit = UINT_MAX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005458 ap->active_tag = ATA_TAG_POISON;
5459 ap->last_ctl = 0xFF;
Borislav Petkovbd5d8252006-06-11 23:17:01 -04005460
5461#if defined(ATA_VERBOSE_DEBUG)
5462 /* turn on all debugging levels */
5463 ap->msg_enable = 0x00FF;
5464#elif defined(ATA_DEBUG)
5465 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 +09005466#else
Borislav Petkov0dd4b212006-06-23 02:29:08 -04005467 ap->msg_enable = ATA_MSG_DRV | ATA_MSG_ERR | ATA_MSG_WARN;
Borislav Petkovbd5d8252006-06-11 23:17:01 -04005468#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07005469
Tejun Heo86e45b62006-03-05 15:29:09 +09005470 INIT_WORK(&ap->port_task, NULL, NULL);
Tejun Heo580b2102006-05-31 18:28:05 +09005471 INIT_WORK(&ap->hotplug_task, ata_scsi_hotplug, ap);
zhao, forrest3057ac32006-06-12 12:01:34 +08005472 INIT_WORK(&ap->scsi_rescan_task, ata_scsi_dev_rescan, ap);
Tejun Heoa72ec4c2006-01-23 13:09:37 +09005473 INIT_LIST_HEAD(&ap->eh_done_q);
Tejun Heoc6cf9e92006-05-31 18:27:27 +09005474 init_waitqueue_head(&ap->eh_wait_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005475
Tejun Heo838df622006-05-15 20:57:44 +09005476 /* set cable type */
5477 ap->cbl = ATA_CBL_NONE;
5478 if (ap->flags & ATA_FLAG_SATA)
5479 ap->cbl = ATA_CBL_SATA;
5480
Tejun Heoacf356b2006-03-24 14:07:50 +09005481 for (i = 0; i < ATA_MAX_DEVICES; i++) {
5482 struct ata_device *dev = &ap->device[i];
Tejun Heo38d87232006-05-15 20:57:51 +09005483 dev->ap = ap;
Tejun Heo72fa4b72006-05-31 18:27:32 +09005484 dev->devno = i;
Tejun Heo3ef3b432006-05-31 18:27:30 +09005485 ata_dev_init(dev);
Tejun Heoacf356b2006-03-24 14:07:50 +09005486 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005487
5488#ifdef ATA_IRQ_TRAP
5489 ap->stats.unhandled_irq = 1;
5490 ap->stats.idle_irq = 1;
5491#endif
5492
5493 memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports));
5494}
5495
5496/**
Tejun Heo4608c162006-08-10 16:59:01 +09005497 * ata_port_init_shost - Initialize SCSI host associated with ATA port
5498 * @ap: ATA port to initialize SCSI host for
5499 * @shost: SCSI host associated with @ap
Brian King155a8a92006-08-07 14:27:17 -05005500 *
Tejun Heo4608c162006-08-10 16:59:01 +09005501 * Initialize SCSI host @shost associated with ATA port @ap.
Brian King155a8a92006-08-07 14:27:17 -05005502 *
5503 * LOCKING:
5504 * Inherited from caller.
5505 */
Tejun Heo4608c162006-08-10 16:59:01 +09005506static void ata_port_init_shost(struct ata_port *ap, struct Scsi_Host *shost)
Brian King155a8a92006-08-07 14:27:17 -05005507{
Jeff Garzikcca39742006-08-24 03:19:22 -04005508 ap->scsi_host = shost;
Brian King155a8a92006-08-07 14:27:17 -05005509
Tejun Heo4608c162006-08-10 16:59:01 +09005510 shost->unique_id = ap->id;
5511 shost->max_id = 16;
5512 shost->max_lun = 1;
5513 shost->max_channel = 1;
5514 shost->max_cmd_len = 12;
Brian King155a8a92006-08-07 14:27:17 -05005515}
5516
5517/**
Jeff Garzik996139f2006-08-10 16:59:03 +09005518 * ata_port_add - Attach low-level ATA driver to system
Linus Torvalds1da177e2005-04-16 15:20:36 -07005519 * @ent: Information provided by low-level driver
Jeff Garzikcca39742006-08-24 03:19:22 -04005520 * @host: Collections of ports to which we add
Linus Torvalds1da177e2005-04-16 15:20:36 -07005521 * @port_no: Port number associated with this host
5522 *
Jeff Garzik0cba6322005-05-30 19:49:12 -04005523 * Attach low-level ATA driver to system.
5524 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005525 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04005526 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005527 *
5528 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04005529 * New ata_port on success, for NULL on error.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005530 */
Jeff Garzik996139f2006-08-10 16:59:03 +09005531static struct ata_port * ata_port_add(const struct ata_probe_ent *ent,
Jeff Garzikcca39742006-08-24 03:19:22 -04005532 struct ata_host *host,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005533 unsigned int port_no)
5534{
Jeff Garzik996139f2006-08-10 16:59:03 +09005535 struct Scsi_Host *shost;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005536 struct ata_port *ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005537
5538 DPRINTK("ENTER\n");
Tejun Heoaec5c3c2006-03-25 01:33:34 +09005539
Tejun Heo52783c52006-05-31 18:28:22 +09005540 if (!ent->port_ops->error_handler &&
Jeff Garzikcca39742006-08-24 03:19:22 -04005541 !(ent->port_flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST))) {
Tejun Heoaec5c3c2006-03-25 01:33:34 +09005542 printk(KERN_ERR "ata%u: no reset mechanism available\n",
5543 port_no);
5544 return NULL;
5545 }
5546
Jeff Garzik996139f2006-08-10 16:59:03 +09005547 shost = scsi_host_alloc(ent->sht, sizeof(struct ata_port));
5548 if (!shost)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005549 return NULL;
5550
Jeff Garzik996139f2006-08-10 16:59:03 +09005551 shost->transportt = &ata_scsi_transport_template;
Tejun Heo30afc842006-03-18 18:40:14 +09005552
Jeff Garzik996139f2006-08-10 16:59:03 +09005553 ap = ata_shost_to_port(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005554
Jeff Garzikcca39742006-08-24 03:19:22 -04005555 ata_port_init(ap, host, ent, port_no);
Jeff Garzik996139f2006-08-10 16:59:03 +09005556 ata_port_init_shost(ap, shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005557
Linus Torvalds1da177e2005-04-16 15:20:36 -07005558 return ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005559}
5560
5561/**
Jeff Garzikcca39742006-08-24 03:19:22 -04005562 * ata_sas_host_init - Initialize a host struct
5563 * @host: host to initialize
5564 * @dev: device host is attached to
5565 * @flags: host flags
5566 * @ops: port_ops
Brian Kingb03732f2006-08-07 14:27:10 -05005567 *
5568 * LOCKING:
5569 * PCI/etc. bus probe sem.
5570 *
5571 */
5572
Jeff Garzikcca39742006-08-24 03:19:22 -04005573void ata_host_init(struct ata_host *host, struct device *dev,
5574 unsigned long flags, const struct ata_port_operations *ops)
Brian Kingb03732f2006-08-07 14:27:10 -05005575{
Jeff Garzikcca39742006-08-24 03:19:22 -04005576 spin_lock_init(&host->lock);
5577 host->dev = dev;
5578 host->flags = flags;
5579 host->ops = ops;
Brian Kingb03732f2006-08-07 14:27:10 -05005580}
5581
5582/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04005583 * ata_device_add - Register hardware device with ATA and SCSI layers
5584 * @ent: Probe information describing hardware device to be registered
5585 *
5586 * This function processes the information provided in the probe
5587 * information struct @ent, allocates the necessary ATA and SCSI
5588 * host information structures, initializes them, and registers
5589 * everything with requisite kernel subsystems.
5590 *
5591 * This function requests irqs, probes the ATA bus, and probes
5592 * the SCSI bus.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005593 *
5594 * LOCKING:
Jeff Garzik0cba6322005-05-30 19:49:12 -04005595 * PCI/etc. bus probe sem.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005596 *
5597 * RETURNS:
Jeff Garzik0cba6322005-05-30 19:49:12 -04005598 * Number of ports registered. Zero on error (no ports registered).
Linus Torvalds1da177e2005-04-16 15:20:36 -07005599 */
Jeff Garzik057ace52005-10-22 14:27:05 -04005600int ata_device_add(const struct ata_probe_ent *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005601{
Jeff Garzik6d0500d2006-08-10 16:59:05 +09005602 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005603 struct device *dev = ent->dev;
Jeff Garzikcca39742006-08-24 03:19:22 -04005604 struct ata_host *host;
Jeff Garzik39b07ce2006-06-11 23:59:44 -04005605 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005606
5607 DPRINTK("ENTER\n");
Alan Cox02f076a2006-09-26 17:35:32 +01005608
5609 if (ent->irq == 0) {
5610 dev_printk(KERN_ERR, dev, "is not available: No interrupt assigned.\n");
5611 return 0;
5612 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005613 /* alloc a container for our list of ATA ports (buses) */
Jeff Garzikcca39742006-08-24 03:19:22 -04005614 host = kzalloc(sizeof(struct ata_host) +
5615 (ent->n_ports * sizeof(void *)), GFP_KERNEL);
5616 if (!host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005617 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005618
Jeff Garzikcca39742006-08-24 03:19:22 -04005619 ata_host_init(host, dev, ent->_host_flags, ent->port_ops);
5620 host->n_ports = ent->n_ports;
5621 host->irq = ent->irq;
5622 host->irq2 = ent->irq2;
5623 host->mmio_base = ent->mmio_base;
5624 host->private_data = ent->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005625
5626 /* register each port bound to this device */
Jeff Garzikcca39742006-08-24 03:19:22 -04005627 for (i = 0; i < host->n_ports; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005628 struct ata_port *ap;
5629 unsigned long xfer_mode_mask;
Alan Cox2ec7df02006-08-10 16:59:10 +09005630 int irq_line = ent->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005631
Jeff Garzikcca39742006-08-24 03:19:22 -04005632 ap = ata_port_add(ent, host, i);
Dave Jonesc38778c2006-09-26 23:52:50 -07005633 host->ports[i] = ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005634 if (!ap)
5635 goto err_out;
5636
Tejun Heodd5b06c2006-08-10 16:59:12 +09005637 /* dummy? */
5638 if (ent->dummy_port_mask & (1 << i)) {
5639 ata_port_printk(ap, KERN_INFO, "DUMMY\n");
5640 ap->ops = &ata_dummy_port_ops;
5641 continue;
5642 }
5643
5644 /* start port */
5645 rc = ap->ops->port_start(ap);
5646 if (rc) {
Jeff Garzikcca39742006-08-24 03:19:22 -04005647 host->ports[i] = NULL;
5648 scsi_host_put(ap->scsi_host);
Tejun Heodd5b06c2006-08-10 16:59:12 +09005649 goto err_out;
5650 }
5651
Alan Cox2ec7df02006-08-10 16:59:10 +09005652 /* Report the secondary IRQ for second channel legacy */
5653 if (i == 1 && ent->irq2)
5654 irq_line = ent->irq2;
5655
Linus Torvalds1da177e2005-04-16 15:20:36 -07005656 xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) |
5657 (ap->mwdma_mask << ATA_SHIFT_MWDMA) |
5658 (ap->pio_mask << ATA_SHIFT_PIO);
5659
5660 /* print per-port info to dmesg */
Tejun Heof15a1da2006-05-15 20:57:56 +09005661 ata_port_printk(ap, KERN_INFO, "%cATA max %s cmd 0x%lX "
Alan Cox2ec7df02006-08-10 16:59:10 +09005662 "ctl 0x%lX bmdma 0x%lX irq %d\n",
Tejun Heof15a1da2006-05-15 20:57:56 +09005663 ap->flags & ATA_FLAG_SATA ? 'S' : 'P',
5664 ata_mode_string(xfer_mode_mask),
5665 ap->ioaddr.cmd_addr,
5666 ap->ioaddr.ctl_addr,
5667 ap->ioaddr.bmdma_addr,
Alan Cox2ec7df02006-08-10 16:59:10 +09005668 irq_line);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005669
5670 ata_chk_status(ap);
Jeff Garzikcca39742006-08-24 03:19:22 -04005671 host->ops->irq_clear(ap);
Tejun Heoe3180492006-05-15 20:58:09 +09005672 ata_eh_freeze_port(ap); /* freeze port before requesting IRQ */
Linus Torvalds1da177e2005-04-16 15:20:36 -07005673 }
5674
Alan Cox2ec7df02006-08-10 16:59:10 +09005675 /* obtain irq, that may be shared between channels */
Jeff Garzik39b07ce2006-06-11 23:59:44 -04005676 rc = request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags,
Jeff Garzikcca39742006-08-24 03:19:22 -04005677 DRV_NAME, host);
Jeff Garzik39b07ce2006-06-11 23:59:44 -04005678 if (rc) {
5679 dev_printk(KERN_ERR, dev, "irq %lu request failed: %d\n",
5680 ent->irq, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005681 goto err_out;
Jeff Garzik39b07ce2006-06-11 23:59:44 -04005682 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005683
Alan Cox2ec7df02006-08-10 16:59:10 +09005684 /* do we have a second IRQ for the other channel, eg legacy mode */
5685 if (ent->irq2) {
5686 /* We will get weird core code crashes later if this is true
5687 so trap it now */
5688 BUG_ON(ent->irq == ent->irq2);
5689
5690 rc = request_irq(ent->irq2, ent->port_ops->irq_handler, ent->irq_flags,
Jeff Garzikcca39742006-08-24 03:19:22 -04005691 DRV_NAME, host);
Alan Cox2ec7df02006-08-10 16:59:10 +09005692 if (rc) {
5693 dev_printk(KERN_ERR, dev, "irq %lu request failed: %d\n",
5694 ent->irq2, rc);
5695 goto err_out_free_irq;
5696 }
5697 }
5698
Linus Torvalds1da177e2005-04-16 15:20:36 -07005699 /* perform each probe synchronously */
5700 DPRINTK("probe begin\n");
Jeff Garzikcca39742006-08-24 03:19:22 -04005701 for (i = 0; i < host->n_ports; i++) {
5702 struct ata_port *ap = host->ports[i];
Tejun Heo5a04bf42006-05-31 18:27:38 +09005703 u32 scontrol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704 int rc;
5705
Tejun Heo5a04bf42006-05-31 18:27:38 +09005706 /* init sata_spd_limit to the current value */
5707 if (sata_scr_read(ap, SCR_CONTROL, &scontrol) == 0) {
5708 int spd = (scontrol >> 4) & 0xf;
5709 ap->hw_sata_spd_limit &= (1 << spd) - 1;
5710 }
5711 ap->sata_spd_limit = ap->hw_sata_spd_limit;
5712
Jeff Garzikcca39742006-08-24 03:19:22 -04005713 rc = scsi_add_host(ap->scsi_host, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005714 if (rc) {
Tejun Heof15a1da2006-05-15 20:57:56 +09005715 ata_port_printk(ap, KERN_ERR, "scsi_add_host failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07005716 /* FIXME: do something useful here */
5717 /* FIXME: handle unconditional calls to
5718 * scsi_scan_host and ata_host_remove, below,
5719 * at the very least
5720 */
5721 }
Tejun Heo3e706392006-05-31 18:28:11 +09005722
Tejun Heo52783c52006-05-31 18:28:22 +09005723 if (ap->ops->error_handler) {
Tejun Heo1cdaf532006-07-03 16:07:26 +09005724 struct ata_eh_info *ehi = &ap->eh_info;
Tejun Heo3e706392006-05-31 18:28:11 +09005725 unsigned long flags;
5726
5727 ata_port_probe(ap);
5728
5729 /* kick EH for boot probing */
Jeff Garzikba6a1302006-06-22 23:46:10 -04005730 spin_lock_irqsave(ap->lock, flags);
Tejun Heo3e706392006-05-31 18:28:11 +09005731
Tejun Heo1cdaf532006-07-03 16:07:26 +09005732 ehi->probe_mask = (1 << ATA_MAX_DEVICES) - 1;
5733 ehi->action |= ATA_EH_SOFTRESET;
5734 ehi->flags |= ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET;
Tejun Heo3e706392006-05-31 18:28:11 +09005735
Tejun Heob51e9e52006-06-29 01:29:30 +09005736 ap->pflags |= ATA_PFLAG_LOADING;
Tejun Heo3e706392006-05-31 18:28:11 +09005737 ata_port_schedule_eh(ap);
5738
Jeff Garzikba6a1302006-06-22 23:46:10 -04005739 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo3e706392006-05-31 18:28:11 +09005740
5741 /* wait for EH to finish */
5742 ata_port_wait_eh(ap);
5743 } else {
5744 DPRINTK("ata%u: bus probe begin\n", ap->id);
5745 rc = ata_bus_probe(ap);
5746 DPRINTK("ata%u: bus probe end\n", ap->id);
5747
5748 if (rc) {
5749 /* FIXME: do something useful here?
5750 * Current libata behavior will
5751 * tear down everything when
5752 * the module is removed
5753 * or the h/w is unplugged.
5754 */
5755 }
5756 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005757 }
5758
5759 /* probes are done, now scan each port's disk(s) */
Randy Dunlapc893a3a2006-01-28 13:15:32 -05005760 DPRINTK("host probe begin\n");
Jeff Garzikcca39742006-08-24 03:19:22 -04005761 for (i = 0; i < host->n_ports; i++) {
5762 struct ata_port *ap = host->ports[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -07005763
Jeff Garzik644dd0c2005-10-03 15:55:19 -04005764 ata_scsi_scan_host(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005765 }
5766
Jeff Garzikcca39742006-08-24 03:19:22 -04005767 dev_set_drvdata(dev, host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005768
5769 VPRINTK("EXIT, returning %u\n", ent->n_ports);
5770 return ent->n_ports; /* success */
5771
Alan Cox2ec7df02006-08-10 16:59:10 +09005772err_out_free_irq:
Jeff Garzikcca39742006-08-24 03:19:22 -04005773 free_irq(ent->irq, host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005774err_out:
Jeff Garzikcca39742006-08-24 03:19:22 -04005775 for (i = 0; i < host->n_ports; i++) {
5776 struct ata_port *ap = host->ports[i];
Tejun Heo77f3f872006-08-05 03:59:19 +09005777 if (ap) {
5778 ap->ops->port_stop(ap);
Jeff Garzikcca39742006-08-24 03:19:22 -04005779 scsi_host_put(ap->scsi_host);
Tejun Heo77f3f872006-08-05 03:59:19 +09005780 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005781 }
Jeff Garzik6d0500d2006-08-10 16:59:05 +09005782
Jeff Garzikcca39742006-08-24 03:19:22 -04005783 kfree(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005784 VPRINTK("EXIT, returning 0\n");
5785 return 0;
5786}
5787
5788/**
Tejun Heo720ba122006-05-31 18:28:13 +09005789 * ata_port_detach - Detach ATA port in prepration of device removal
5790 * @ap: ATA port to be detached
5791 *
5792 * Detach all ATA devices and the associated SCSI devices of @ap;
5793 * then, remove the associated SCSI host. @ap is guaranteed to
5794 * be quiescent on return from this function.
5795 *
5796 * LOCKING:
5797 * Kernel thread context (may sleep).
5798 */
5799void ata_port_detach(struct ata_port *ap)
5800{
5801 unsigned long flags;
5802 int i;
5803
5804 if (!ap->ops->error_handler)
Tejun Heoc3cf30a2006-08-05 03:59:11 +09005805 goto skip_eh;
Tejun Heo720ba122006-05-31 18:28:13 +09005806
5807 /* tell EH we're leaving & flush EH */
Jeff Garzikba6a1302006-06-22 23:46:10 -04005808 spin_lock_irqsave(ap->lock, flags);
Tejun Heob51e9e52006-06-29 01:29:30 +09005809 ap->pflags |= ATA_PFLAG_UNLOADING;
Jeff Garzikba6a1302006-06-22 23:46:10 -04005810 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo720ba122006-05-31 18:28:13 +09005811
5812 ata_port_wait_eh(ap);
5813
5814 /* EH is now guaranteed to see UNLOADING, so no new device
5815 * will be attached. Disable all existing devices.
5816 */
Jeff Garzikba6a1302006-06-22 23:46:10 -04005817 spin_lock_irqsave(ap->lock, flags);
Tejun Heo720ba122006-05-31 18:28:13 +09005818
5819 for (i = 0; i < ATA_MAX_DEVICES; i++)
5820 ata_dev_disable(&ap->device[i]);
5821
Jeff Garzikba6a1302006-06-22 23:46:10 -04005822 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo720ba122006-05-31 18:28:13 +09005823
5824 /* Final freeze & EH. All in-flight commands are aborted. EH
5825 * will be skipped and retrials will be terminated with bad
5826 * target.
5827 */
Jeff Garzikba6a1302006-06-22 23:46:10 -04005828 spin_lock_irqsave(ap->lock, flags);
Tejun Heo720ba122006-05-31 18:28:13 +09005829 ata_port_freeze(ap); /* won't be thawed */
Jeff Garzikba6a1302006-06-22 23:46:10 -04005830 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo720ba122006-05-31 18:28:13 +09005831
5832 ata_port_wait_eh(ap);
5833
5834 /* Flush hotplug task. The sequence is similar to
5835 * ata_port_flush_task().
5836 */
5837 flush_workqueue(ata_aux_wq);
5838 cancel_delayed_work(&ap->hotplug_task);
5839 flush_workqueue(ata_aux_wq);
5840
Tejun Heoc3cf30a2006-08-05 03:59:11 +09005841 skip_eh:
Tejun Heo720ba122006-05-31 18:28:13 +09005842 /* remove the associated SCSI host */
Jeff Garzikcca39742006-08-24 03:19:22 -04005843 scsi_remove_host(ap->scsi_host);
Tejun Heo720ba122006-05-31 18:28:13 +09005844}
5845
5846/**
Jeff Garzikcca39742006-08-24 03:19:22 -04005847 * ata_host_remove - PCI layer callback for device removal
5848 * @host: ATA host set that was removed
Alan Cox17b14452005-09-15 15:44:00 +01005849 *
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05005850 * Unregister all objects associated with this host set. Free those
Alan Cox17b14452005-09-15 15:44:00 +01005851 * objects.
5852 *
5853 * LOCKING:
5854 * Inherited from calling layer (may sleep).
5855 */
5856
Jeff Garzikcca39742006-08-24 03:19:22 -04005857void ata_host_remove(struct ata_host *host)
Alan Cox17b14452005-09-15 15:44:00 +01005858{
Alan Cox17b14452005-09-15 15:44:00 +01005859 unsigned int i;
5860
Jeff Garzikcca39742006-08-24 03:19:22 -04005861 for (i = 0; i < host->n_ports; i++)
5862 ata_port_detach(host->ports[i]);
Alan Cox17b14452005-09-15 15:44:00 +01005863
Jeff Garzikcca39742006-08-24 03:19:22 -04005864 free_irq(host->irq, host);
5865 if (host->irq2)
5866 free_irq(host->irq2, host);
Alan Cox17b14452005-09-15 15:44:00 +01005867
Jeff Garzikcca39742006-08-24 03:19:22 -04005868 for (i = 0; i < host->n_ports; i++) {
5869 struct ata_port *ap = host->ports[i];
Alan Cox17b14452005-09-15 15:44:00 +01005870
Jeff Garzikcca39742006-08-24 03:19:22 -04005871 ata_scsi_release(ap->scsi_host);
Alan Cox17b14452005-09-15 15:44:00 +01005872
5873 if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) {
5874 struct ata_ioports *ioaddr = &ap->ioaddr;
5875
Alan Cox2ec7df02006-08-10 16:59:10 +09005876 /* FIXME: Add -ac IDE pci mods to remove these special cases */
5877 if (ioaddr->cmd_addr == ATA_PRIMARY_CMD)
5878 release_region(ATA_PRIMARY_CMD, 8);
5879 else if (ioaddr->cmd_addr == ATA_SECONDARY_CMD)
5880 release_region(ATA_SECONDARY_CMD, 8);
Alan Cox17b14452005-09-15 15:44:00 +01005881 }
5882
Jeff Garzikcca39742006-08-24 03:19:22 -04005883 scsi_host_put(ap->scsi_host);
Alan Cox17b14452005-09-15 15:44:00 +01005884 }
5885
Jeff Garzikcca39742006-08-24 03:19:22 -04005886 if (host->ops->host_stop)
5887 host->ops->host_stop(host);
Alan Cox17b14452005-09-15 15:44:00 +01005888
Jeff Garzikcca39742006-08-24 03:19:22 -04005889 kfree(host);
Alan Cox17b14452005-09-15 15:44:00 +01005890}
5891
5892/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07005893 * ata_scsi_release - SCSI layer callback hook for host unload
Jeff Garzik4f931372006-09-29 05:07:25 -04005894 * @shost: libata host to be unloaded
Linus Torvalds1da177e2005-04-16 15:20:36 -07005895 *
5896 * Performs all duties necessary to shut down a libata port...
5897 * Kill port kthread, disable port, and release resources.
5898 *
5899 * LOCKING:
5900 * Inherited from SCSI layer.
5901 *
5902 * RETURNS:
5903 * One.
5904 */
5905
Jeff Garzikcca39742006-08-24 03:19:22 -04005906int ata_scsi_release(struct Scsi_Host *shost)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005907{
Jeff Garzikcca39742006-08-24 03:19:22 -04005908 struct ata_port *ap = ata_shost_to_port(shost);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005909
5910 DPRINTK("ENTER\n");
5911
5912 ap->ops->port_disable(ap);
Jeff Garzik6543bc02006-08-05 03:59:17 +09005913 ap->ops->port_stop(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005914
5915 DPRINTK("EXIT\n");
5916 return 1;
5917}
5918
Brian Kingf6d950e2006-08-07 14:27:24 -05005919struct ata_probe_ent *
5920ata_probe_ent_alloc(struct device *dev, const struct ata_port_info *port)
5921{
5922 struct ata_probe_ent *probe_ent;
5923
5924 probe_ent = kzalloc(sizeof(*probe_ent), GFP_KERNEL);
5925 if (!probe_ent) {
5926 printk(KERN_ERR DRV_NAME "(%s): out of memory\n",
5927 kobject_name(&(dev->kobj)));
5928 return NULL;
5929 }
5930
5931 INIT_LIST_HEAD(&probe_ent->node);
5932 probe_ent->dev = dev;
5933
5934 probe_ent->sht = port->sht;
Jeff Garzikcca39742006-08-24 03:19:22 -04005935 probe_ent->port_flags = port->flags;
Brian Kingf6d950e2006-08-07 14:27:24 -05005936 probe_ent->pio_mask = port->pio_mask;
5937 probe_ent->mwdma_mask = port->mwdma_mask;
5938 probe_ent->udma_mask = port->udma_mask;
5939 probe_ent->port_ops = port->port_ops;
Jeff Garzikd639ca92006-09-28 03:48:18 -04005940 probe_ent->private_data = port->private_data;
Brian Kingf6d950e2006-08-07 14:27:24 -05005941
5942 return probe_ent;
5943}
5944
Linus Torvalds1da177e2005-04-16 15:20:36 -07005945/**
5946 * ata_std_ports - initialize ioaddr with standard port offsets.
5947 * @ioaddr: IO address structure to be initialized
Edward Falk0baab862005-06-02 18:17:13 -04005948 *
5949 * Utility function which initializes data_addr, error_addr,
5950 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
5951 * device_addr, status_addr, and command_addr to standard offsets
5952 * relative to cmd_addr.
5953 *
5954 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005955 */
Edward Falk0baab862005-06-02 18:17:13 -04005956
Linus Torvalds1da177e2005-04-16 15:20:36 -07005957void ata_std_ports(struct ata_ioports *ioaddr)
5958{
5959 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
5960 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
5961 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
5962 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
5963 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
5964 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
5965 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
5966 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
5967 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
5968 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
5969}
5970
Edward Falk0baab862005-06-02 18:17:13 -04005971
Jeff Garzik374b1872005-08-30 05:42:52 -04005972#ifdef CONFIG_PCI
5973
Jeff Garzikcca39742006-08-24 03:19:22 -04005974void ata_pci_host_stop (struct ata_host *host)
Jeff Garzik374b1872005-08-30 05:42:52 -04005975{
Jeff Garzikcca39742006-08-24 03:19:22 -04005976 struct pci_dev *pdev = to_pci_dev(host->dev);
Jeff Garzik374b1872005-08-30 05:42:52 -04005977
Jeff Garzikcca39742006-08-24 03:19:22 -04005978 pci_iounmap(pdev, host->mmio_base);
Jeff Garzik374b1872005-08-30 05:42:52 -04005979}
5980
Edward Falk0baab862005-06-02 18:17:13 -04005981/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07005982 * ata_pci_remove_one - PCI layer callback for device removal
5983 * @pdev: PCI device that was removed
5984 *
5985 * PCI layer indicates to libata via this hook that
Randy Dunlap6f0ef4f2005-10-25 01:44:30 -04005986 * hot-unplug or module unload event has occurred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005987 * Handle this by unregistering all objects associated
5988 * with this PCI device. Free those objects. Then finally
5989 * release PCI resources and disable device.
5990 *
5991 * LOCKING:
5992 * Inherited from PCI layer (may sleep).
5993 */
5994
5995void ata_pci_remove_one (struct pci_dev *pdev)
5996{
5997 struct device *dev = pci_dev_to_dev(pdev);
Jeff Garzikcca39742006-08-24 03:19:22 -04005998 struct ata_host *host = dev_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005999
Jeff Garzikcca39742006-08-24 03:19:22 -04006000 ata_host_remove(host);
Tejun Heof0eb62b2006-06-12 23:05:38 +09006001
Linus Torvalds1da177e2005-04-16 15:20:36 -07006002 pci_release_regions(pdev);
6003 pci_disable_device(pdev);
6004 dev_set_drvdata(dev, NULL);
6005}
6006
6007/* move to PCI subsystem */
Jeff Garzik057ace52005-10-22 14:27:05 -04006008int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006009{
6010 unsigned long tmp = 0;
6011
6012 switch (bits->width) {
6013 case 1: {
6014 u8 tmp8 = 0;
6015 pci_read_config_byte(pdev, bits->reg, &tmp8);
6016 tmp = tmp8;
6017 break;
6018 }
6019 case 2: {
6020 u16 tmp16 = 0;
6021 pci_read_config_word(pdev, bits->reg, &tmp16);
6022 tmp = tmp16;
6023 break;
6024 }
6025 case 4: {
6026 u32 tmp32 = 0;
6027 pci_read_config_dword(pdev, bits->reg, &tmp32);
6028 tmp = tmp32;
6029 break;
6030 }
6031
6032 default:
6033 return -EINVAL;
6034 }
6035
6036 tmp &= bits->mask;
6037
6038 return (tmp == bits->val) ? 1 : 0;
6039}
Jens Axboe9b847542006-01-06 09:28:07 +01006040
Tejun Heo3c5100c2006-07-26 16:58:33 +09006041void ata_pci_device_do_suspend(struct pci_dev *pdev, pm_message_t mesg)
Jens Axboe9b847542006-01-06 09:28:07 +01006042{
6043 pci_save_state(pdev);
Tejun Heo500530f2006-07-03 16:07:27 +09006044
Tejun Heo3c5100c2006-07-26 16:58:33 +09006045 if (mesg.event == PM_EVENT_SUSPEND) {
Tejun Heo500530f2006-07-03 16:07:27 +09006046 pci_disable_device(pdev);
6047 pci_set_power_state(pdev, PCI_D3hot);
6048 }
Jens Axboe9b847542006-01-06 09:28:07 +01006049}
6050
Tejun Heo500530f2006-07-03 16:07:27 +09006051void ata_pci_device_do_resume(struct pci_dev *pdev)
Jens Axboe9b847542006-01-06 09:28:07 +01006052{
6053 pci_set_power_state(pdev, PCI_D0);
6054 pci_restore_state(pdev);
6055 pci_enable_device(pdev);
6056 pci_set_master(pdev);
Tejun Heo500530f2006-07-03 16:07:27 +09006057}
6058
Tejun Heo3c5100c2006-07-26 16:58:33 +09006059int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg)
Tejun Heo500530f2006-07-03 16:07:27 +09006060{
Jeff Garzikcca39742006-08-24 03:19:22 -04006061 struct ata_host *host = dev_get_drvdata(&pdev->dev);
Tejun Heo500530f2006-07-03 16:07:27 +09006062 int rc = 0;
6063
Jeff Garzikcca39742006-08-24 03:19:22 -04006064 rc = ata_host_suspend(host, mesg);
Tejun Heo500530f2006-07-03 16:07:27 +09006065 if (rc)
6066 return rc;
6067
Tejun Heo3c5100c2006-07-26 16:58:33 +09006068 ata_pci_device_do_suspend(pdev, mesg);
Tejun Heo500530f2006-07-03 16:07:27 +09006069
6070 return 0;
6071}
6072
6073int ata_pci_device_resume(struct pci_dev *pdev)
6074{
Jeff Garzikcca39742006-08-24 03:19:22 -04006075 struct ata_host *host = dev_get_drvdata(&pdev->dev);
Tejun Heo500530f2006-07-03 16:07:27 +09006076
6077 ata_pci_device_do_resume(pdev);
Jeff Garzikcca39742006-08-24 03:19:22 -04006078 ata_host_resume(host);
Jens Axboe9b847542006-01-06 09:28:07 +01006079 return 0;
6080}
Linus Torvalds1da177e2005-04-16 15:20:36 -07006081#endif /* CONFIG_PCI */
6082
6083
Linus Torvalds1da177e2005-04-16 15:20:36 -07006084static int __init ata_init(void)
6085{
Andrew Mortona8601e52006-06-25 01:36:52 -07006086 ata_probe_timeout *= HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006087 ata_wq = create_workqueue("ata");
6088 if (!ata_wq)
6089 return -ENOMEM;
6090
Tejun Heo453b07a2006-05-31 18:27:42 +09006091 ata_aux_wq = create_singlethread_workqueue("ata_aux");
6092 if (!ata_aux_wq) {
6093 destroy_workqueue(ata_wq);
6094 return -ENOMEM;
6095 }
6096
Linus Torvalds1da177e2005-04-16 15:20:36 -07006097 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
6098 return 0;
6099}
6100
6101static void __exit ata_exit(void)
6102{
6103 destroy_workqueue(ata_wq);
Tejun Heo453b07a2006-05-31 18:27:42 +09006104 destroy_workqueue(ata_aux_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006105}
6106
Brian Kinga4625082006-11-13 16:32:36 -06006107subsys_initcall(ata_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006108module_exit(ata_exit);
6109
Jeff Garzik67846b32005-10-05 02:58:32 -04006110static unsigned long ratelimit_time;
Ingo Molnar34af9462006-06-27 02:53:55 -07006111static DEFINE_SPINLOCK(ata_ratelimit_lock);
Jeff Garzik67846b32005-10-05 02:58:32 -04006112
6113int ata_ratelimit(void)
6114{
6115 int rc;
6116 unsigned long flags;
6117
6118 spin_lock_irqsave(&ata_ratelimit_lock, flags);
6119
6120 if (time_after(jiffies, ratelimit_time)) {
6121 rc = 1;
6122 ratelimit_time = jiffies + (HZ/5);
6123 } else
6124 rc = 0;
6125
6126 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
6127
6128 return rc;
6129}
6130
Tejun Heoc22daff2006-04-11 22:22:29 +09006131/**
6132 * ata_wait_register - wait until register value changes
6133 * @reg: IO-mapped register
6134 * @mask: Mask to apply to read register value
6135 * @val: Wait condition
6136 * @interval_msec: polling interval in milliseconds
6137 * @timeout_msec: timeout in milliseconds
6138 *
6139 * Waiting for some bits of register to change is a common
6140 * operation for ATA controllers. This function reads 32bit LE
6141 * IO-mapped register @reg and tests for the following condition.
6142 *
6143 * (*@reg & mask) != val
6144 *
6145 * If the condition is met, it returns; otherwise, the process is
6146 * repeated after @interval_msec until timeout.
6147 *
6148 * LOCKING:
6149 * Kernel thread context (may sleep)
6150 *
6151 * RETURNS:
6152 * The final register value.
6153 */
6154u32 ata_wait_register(void __iomem *reg, u32 mask, u32 val,
6155 unsigned long interval_msec,
6156 unsigned long timeout_msec)
6157{
6158 unsigned long timeout;
6159 u32 tmp;
6160
6161 tmp = ioread32(reg);
6162
6163 /* Calculate timeout _after_ the first read to make sure
6164 * preceding writes reach the controller before starting to
6165 * eat away the timeout.
6166 */
6167 timeout = jiffies + (timeout_msec * HZ) / 1000;
6168
6169 while ((tmp & mask) == val && time_before(jiffies, timeout)) {
6170 msleep(interval_msec);
6171 tmp = ioread32(reg);
6172 }
6173
6174 return tmp;
6175}
6176
Linus Torvalds1da177e2005-04-16 15:20:36 -07006177/*
Tejun Heodd5b06c2006-08-10 16:59:12 +09006178 * Dummy port_ops
6179 */
6180static void ata_dummy_noret(struct ata_port *ap) { }
6181static int ata_dummy_ret0(struct ata_port *ap) { return 0; }
6182static void ata_dummy_qc_noret(struct ata_queued_cmd *qc) { }
6183
6184static u8 ata_dummy_check_status(struct ata_port *ap)
6185{
6186 return ATA_DRDY;
6187}
6188
6189static unsigned int ata_dummy_qc_issue(struct ata_queued_cmd *qc)
6190{
6191 return AC_ERR_SYSTEM;
6192}
6193
6194const struct ata_port_operations ata_dummy_port_ops = {
6195 .port_disable = ata_port_disable,
6196 .check_status = ata_dummy_check_status,
6197 .check_altstatus = ata_dummy_check_status,
6198 .dev_select = ata_noop_dev_select,
6199 .qc_prep = ata_noop_qc_prep,
6200 .qc_issue = ata_dummy_qc_issue,
6201 .freeze = ata_dummy_noret,
6202 .thaw = ata_dummy_noret,
6203 .error_handler = ata_dummy_noret,
6204 .post_internal_cmd = ata_dummy_qc_noret,
6205 .irq_clear = ata_dummy_noret,
6206 .port_start = ata_dummy_ret0,
6207 .port_stop = ata_dummy_noret,
6208};
6209
6210/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07006211 * libata is essentially a library of internal helper functions for
6212 * low-level ATA host controller drivers. As such, the API/ABI is
6213 * likely to change as new drivers are added and updated.
6214 * Do not depend on ABI/API stability.
6215 */
6216
Tejun Heoe9c83912006-07-03 16:07:26 +09006217EXPORT_SYMBOL_GPL(sata_deb_timing_normal);
6218EXPORT_SYMBOL_GPL(sata_deb_timing_hotplug);
6219EXPORT_SYMBOL_GPL(sata_deb_timing_long);
Tejun Heodd5b06c2006-08-10 16:59:12 +09006220EXPORT_SYMBOL_GPL(ata_dummy_port_ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006221EXPORT_SYMBOL_GPL(ata_std_bios_param);
6222EXPORT_SYMBOL_GPL(ata_std_ports);
Jeff Garzikcca39742006-08-24 03:19:22 -04006223EXPORT_SYMBOL_GPL(ata_host_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006224EXPORT_SYMBOL_GPL(ata_device_add);
Tejun Heo720ba122006-05-31 18:28:13 +09006225EXPORT_SYMBOL_GPL(ata_port_detach);
Jeff Garzikcca39742006-08-24 03:19:22 -04006226EXPORT_SYMBOL_GPL(ata_host_remove);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006227EXPORT_SYMBOL_GPL(ata_sg_init);
6228EXPORT_SYMBOL_GPL(ata_sg_init_one);
Tejun Heo9a1004d2006-05-31 18:27:52 +09006229EXPORT_SYMBOL_GPL(ata_hsm_move);
Tejun Heof686bcb2006-05-15 20:58:05 +09006230EXPORT_SYMBOL_GPL(ata_qc_complete);
Tejun Heodedaf2b2006-05-15 21:03:43 +09006231EXPORT_SYMBOL_GPL(ata_qc_complete_multiple);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006232EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006233EXPORT_SYMBOL_GPL(ata_tf_load);
6234EXPORT_SYMBOL_GPL(ata_tf_read);
6235EXPORT_SYMBOL_GPL(ata_noop_dev_select);
6236EXPORT_SYMBOL_GPL(ata_std_dev_select);
6237EXPORT_SYMBOL_GPL(ata_tf_to_fis);
6238EXPORT_SYMBOL_GPL(ata_tf_from_fis);
6239EXPORT_SYMBOL_GPL(ata_check_status);
6240EXPORT_SYMBOL_GPL(ata_altstatus);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006241EXPORT_SYMBOL_GPL(ata_exec_command);
6242EXPORT_SYMBOL_GPL(ata_port_start);
6243EXPORT_SYMBOL_GPL(ata_port_stop);
Jeff Garzikaa8f0dc2005-05-26 21:54:27 -04006244EXPORT_SYMBOL_GPL(ata_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006245EXPORT_SYMBOL_GPL(ata_interrupt);
Alan Coxa6b2c5d2006-05-22 16:59:59 +01006246EXPORT_SYMBOL_GPL(ata_mmio_data_xfer);
6247EXPORT_SYMBOL_GPL(ata_pio_data_xfer);
Alan Cox75e99582006-05-24 14:14:41 +01006248EXPORT_SYMBOL_GPL(ata_pio_data_xfer_noirq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006249EXPORT_SYMBOL_GPL(ata_qc_prep);
Brian Kinge46834c2006-03-17 17:04:03 -06006250EXPORT_SYMBOL_GPL(ata_noop_qc_prep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006251EXPORT_SYMBOL_GPL(ata_bmdma_setup);
6252EXPORT_SYMBOL_GPL(ata_bmdma_start);
6253EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
6254EXPORT_SYMBOL_GPL(ata_bmdma_status);
6255EXPORT_SYMBOL_GPL(ata_bmdma_stop);
Tejun Heo6d97dbd2006-05-15 20:58:24 +09006256EXPORT_SYMBOL_GPL(ata_bmdma_freeze);
6257EXPORT_SYMBOL_GPL(ata_bmdma_thaw);
6258EXPORT_SYMBOL_GPL(ata_bmdma_drive_eh);
6259EXPORT_SYMBOL_GPL(ata_bmdma_error_handler);
6260EXPORT_SYMBOL_GPL(ata_bmdma_post_internal_cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006261EXPORT_SYMBOL_GPL(ata_port_probe);
Tejun Heo3c567b72006-05-15 20:57:23 +09006262EXPORT_SYMBOL_GPL(sata_set_spd);
Tejun Heod7bb4cc2006-05-31 18:27:46 +09006263EXPORT_SYMBOL_GPL(sata_phy_debounce);
6264EXPORT_SYMBOL_GPL(sata_phy_resume);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006265EXPORT_SYMBOL_GPL(sata_phy_reset);
6266EXPORT_SYMBOL_GPL(__sata_phy_reset);
6267EXPORT_SYMBOL_GPL(ata_bus_reset);
Tejun Heof5914a42006-05-31 18:27:48 +09006268EXPORT_SYMBOL_GPL(ata_std_prereset);
Tejun Heoc2bd5802006-01-24 17:05:22 +09006269EXPORT_SYMBOL_GPL(ata_std_softreset);
Tejun Heob6103f62006-11-01 17:59:53 +09006270EXPORT_SYMBOL_GPL(sata_port_hardreset);
Tejun Heoc2bd5802006-01-24 17:05:22 +09006271EXPORT_SYMBOL_GPL(sata_std_hardreset);
6272EXPORT_SYMBOL_GPL(ata_std_postreset);
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05006273EXPORT_SYMBOL_GPL(ata_dev_classify);
6274EXPORT_SYMBOL_GPL(ata_dev_pair);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006275EXPORT_SYMBOL_GPL(ata_port_disable);
Jeff Garzik67846b32005-10-05 02:58:32 -04006276EXPORT_SYMBOL_GPL(ata_ratelimit);
Tejun Heoc22daff2006-04-11 22:22:29 +09006277EXPORT_SYMBOL_GPL(ata_wait_register);
Tejun Heo6f8b9952006-01-24 17:05:21 +09006278EXPORT_SYMBOL_GPL(ata_busy_sleep);
Tejun Heo86e45b62006-03-05 15:29:09 +09006279EXPORT_SYMBOL_GPL(ata_port_queue_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006280EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
6281EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006282EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
Tejun Heo83c47bc2006-05-31 18:28:07 +09006283EXPORT_SYMBOL_GPL(ata_scsi_slave_destroy);
Tejun Heoa6e6ce82006-05-15 21:03:48 +09006284EXPORT_SYMBOL_GPL(ata_scsi_change_queue_depth);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006285EXPORT_SYMBOL_GPL(ata_scsi_release);
6286EXPORT_SYMBOL_GPL(ata_host_intr);
Tejun Heo34bf2172006-05-15 20:57:46 +09006287EXPORT_SYMBOL_GPL(sata_scr_valid);
6288EXPORT_SYMBOL_GPL(sata_scr_read);
6289EXPORT_SYMBOL_GPL(sata_scr_write);
6290EXPORT_SYMBOL_GPL(sata_scr_write_flush);
6291EXPORT_SYMBOL_GPL(ata_port_online);
6292EXPORT_SYMBOL_GPL(ata_port_offline);
Jeff Garzikcca39742006-08-24 03:19:22 -04006293EXPORT_SYMBOL_GPL(ata_host_suspend);
6294EXPORT_SYMBOL_GPL(ata_host_resume);
Tejun Heo6a62a042006-02-13 10:02:46 +09006295EXPORT_SYMBOL_GPL(ata_id_string);
6296EXPORT_SYMBOL_GPL(ata_id_c_string);
Alan Cox6919a0a2006-10-27 19:08:46 -07006297EXPORT_SYMBOL_GPL(ata_device_blacklisted);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006298EXPORT_SYMBOL_GPL(ata_scsi_simulate);
6299
Alan Cox1bc4ccf2006-01-09 17:18:14 +00006300EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
Alan Cox452503f2005-10-21 19:01:32 -04006301EXPORT_SYMBOL_GPL(ata_timing_compute);
6302EXPORT_SYMBOL_GPL(ata_timing_merge);
6303
Linus Torvalds1da177e2005-04-16 15:20:36 -07006304#ifdef CONFIG_PCI
6305EXPORT_SYMBOL_GPL(pci_test_config_bits);
Jeff Garzik374b1872005-08-30 05:42:52 -04006306EXPORT_SYMBOL_GPL(ata_pci_host_stop);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006307EXPORT_SYMBOL_GPL(ata_pci_init_native_mode);
6308EXPORT_SYMBOL_GPL(ata_pci_init_one);
6309EXPORT_SYMBOL_GPL(ata_pci_remove_one);
Tejun Heo500530f2006-07-03 16:07:27 +09006310EXPORT_SYMBOL_GPL(ata_pci_device_do_suspend);
6311EXPORT_SYMBOL_GPL(ata_pci_device_do_resume);
Jens Axboe9b847542006-01-06 09:28:07 +01006312EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
6313EXPORT_SYMBOL_GPL(ata_pci_device_resume);
Alan Cox67951ad2006-03-22 15:55:54 +00006314EXPORT_SYMBOL_GPL(ata_pci_default_filter);
6315EXPORT_SYMBOL_GPL(ata_pci_clear_simplex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006316#endif /* CONFIG_PCI */
Jens Axboe9b847542006-01-06 09:28:07 +01006317
Jens Axboe9b847542006-01-06 09:28:07 +01006318EXPORT_SYMBOL_GPL(ata_scsi_device_suspend);
6319EXPORT_SYMBOL_GPL(ata_scsi_device_resume);
Tejun Heoece1d632006-04-02 18:51:53 +09006320
Tejun Heoece1d632006-04-02 18:51:53 +09006321EXPORT_SYMBOL_GPL(ata_eng_timeout);
Tejun Heo7b70fc02006-05-15 20:58:07 +09006322EXPORT_SYMBOL_GPL(ata_port_schedule_eh);
6323EXPORT_SYMBOL_GPL(ata_port_abort);
Tejun Heoe3180492006-05-15 20:58:09 +09006324EXPORT_SYMBOL_GPL(ata_port_freeze);
6325EXPORT_SYMBOL_GPL(ata_eh_freeze_port);
6326EXPORT_SYMBOL_GPL(ata_eh_thaw_port);
Tejun Heoece1d632006-04-02 18:51:53 +09006327EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
6328EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
Tejun Heo022bdb02006-05-15 20:58:22 +09006329EXPORT_SYMBOL_GPL(ata_do_eh);