Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
af36d7f0 JG |
2 | * 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 | * | |
1da177e4 LT |
33 | */ |
34 | ||
35 | #include <linux/config.h> | |
36 | #include <linux/kernel.h> | |
37 | #include <linux/module.h> | |
38 | #include <linux/pci.h> | |
39 | #include <linux/init.h> | |
40 | #include <linux/list.h> | |
41 | #include <linux/mm.h> | |
42 | #include <linux/highmem.h> | |
43 | #include <linux/spinlock.h> | |
44 | #include <linux/blkdev.h> | |
45 | #include <linux/delay.h> | |
46 | #include <linux/timer.h> | |
47 | #include <linux/interrupt.h> | |
48 | #include <linux/completion.h> | |
49 | #include <linux/suspend.h> | |
50 | #include <linux/workqueue.h> | |
67846b30 | 51 | #include <linux/jiffies.h> |
378f058c | 52 | #include <linux/scatterlist.h> |
1da177e4 | 53 | #include <scsi/scsi.h> |
1da177e4 | 54 | #include "scsi_priv.h" |
193515d5 | 55 | #include <scsi/scsi_cmnd.h> |
1da177e4 LT |
56 | #include <scsi/scsi_host.h> |
57 | #include <linux/libata.h> | |
58 | #include <asm/io.h> | |
59 | #include <asm/semaphore.h> | |
60 | #include <asm/byteorder.h> | |
61 | ||
62 | #include "libata.h" | |
63 | ||
59a10b17 | 64 | static void ata_dev_reread_id(struct ata_port *ap, struct ata_device *dev); |
8bf62ece | 65 | static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev); |
1da177e4 LT |
66 | static void ata_set_mode(struct ata_port *ap); |
67 | static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev); | |
057ace5e | 68 | static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift); |
1da177e4 | 69 | static int fgb(u32 bitmap); |
057ace5e | 70 | static int ata_choose_xfer_mode(const struct ata_port *ap, |
1da177e4 LT |
71 | u8 *xfer_mode_out, |
72 | unsigned int *xfer_shift_out); | |
1da177e4 LT |
73 | |
74 | static unsigned int ata_unique_id = 1; | |
75 | static struct workqueue_struct *ata_wq; | |
76 | ||
1623c81e JG |
77 | int atapi_enabled = 0; |
78 | module_param(atapi_enabled, int, 0444); | |
79 | MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)"); | |
80 | ||
1da177e4 LT |
81 | MODULE_AUTHOR("Jeff Garzik"); |
82 | MODULE_DESCRIPTION("Library module for ATA devices"); | |
83 | MODULE_LICENSE("GPL"); | |
84 | MODULE_VERSION(DRV_VERSION); | |
85 | ||
0baab86b | 86 | |
1da177e4 LT |
87 | /** |
88 | * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure | |
89 | * @tf: Taskfile to convert | |
90 | * @fis: Buffer into which data will output | |
91 | * @pmp: Port multiplier port | |
92 | * | |
93 | * Converts a standard ATA taskfile to a Serial ATA | |
94 | * FIS structure (Register - Host to Device). | |
95 | * | |
96 | * LOCKING: | |
97 | * Inherited from caller. | |
98 | */ | |
99 | ||
057ace5e | 100 | void ata_tf_to_fis(const struct ata_taskfile *tf, u8 *fis, u8 pmp) |
1da177e4 LT |
101 | { |
102 | fis[0] = 0x27; /* Register - Host to Device FIS */ | |
103 | fis[1] = (pmp & 0xf) | (1 << 7); /* Port multiplier number, | |
104 | bit 7 indicates Command FIS */ | |
105 | fis[2] = tf->command; | |
106 | fis[3] = tf->feature; | |
107 | ||
108 | fis[4] = tf->lbal; | |
109 | fis[5] = tf->lbam; | |
110 | fis[6] = tf->lbah; | |
111 | fis[7] = tf->device; | |
112 | ||
113 | fis[8] = tf->hob_lbal; | |
114 | fis[9] = tf->hob_lbam; | |
115 | fis[10] = tf->hob_lbah; | |
116 | fis[11] = tf->hob_feature; | |
117 | ||
118 | fis[12] = tf->nsect; | |
119 | fis[13] = tf->hob_nsect; | |
120 | fis[14] = 0; | |
121 | fis[15] = tf->ctl; | |
122 | ||
123 | fis[16] = 0; | |
124 | fis[17] = 0; | |
125 | fis[18] = 0; | |
126 | fis[19] = 0; | |
127 | } | |
128 | ||
129 | /** | |
130 | * ata_tf_from_fis - Convert SATA FIS to ATA taskfile | |
131 | * @fis: Buffer from which data will be input | |
132 | * @tf: Taskfile to output | |
133 | * | |
e12a1be6 | 134 | * Converts a serial ATA FIS structure to a standard ATA taskfile. |
1da177e4 LT |
135 | * |
136 | * LOCKING: | |
137 | * Inherited from caller. | |
138 | */ | |
139 | ||
057ace5e | 140 | void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf) |
1da177e4 LT |
141 | { |
142 | tf->command = fis[2]; /* status */ | |
143 | tf->feature = fis[3]; /* error */ | |
144 | ||
145 | tf->lbal = fis[4]; | |
146 | tf->lbam = fis[5]; | |
147 | tf->lbah = fis[6]; | |
148 | tf->device = fis[7]; | |
149 | ||
150 | tf->hob_lbal = fis[8]; | |
151 | tf->hob_lbam = fis[9]; | |
152 | tf->hob_lbah = fis[10]; | |
153 | ||
154 | tf->nsect = fis[12]; | |
155 | tf->hob_nsect = fis[13]; | |
156 | } | |
157 | ||
8cbd6df1 AL |
158 | static const u8 ata_rw_cmds[] = { |
159 | /* pio multi */ | |
160 | ATA_CMD_READ_MULTI, | |
161 | ATA_CMD_WRITE_MULTI, | |
162 | ATA_CMD_READ_MULTI_EXT, | |
163 | ATA_CMD_WRITE_MULTI_EXT, | |
9a3dccc4 TH |
164 | 0, |
165 | 0, | |
166 | 0, | |
167 | ATA_CMD_WRITE_MULTI_FUA_EXT, | |
8cbd6df1 AL |
168 | /* pio */ |
169 | ATA_CMD_PIO_READ, | |
170 | ATA_CMD_PIO_WRITE, | |
171 | ATA_CMD_PIO_READ_EXT, | |
172 | ATA_CMD_PIO_WRITE_EXT, | |
9a3dccc4 TH |
173 | 0, |
174 | 0, | |
175 | 0, | |
176 | 0, | |
8cbd6df1 AL |
177 | /* dma */ |
178 | ATA_CMD_READ, | |
179 | ATA_CMD_WRITE, | |
180 | ATA_CMD_READ_EXT, | |
9a3dccc4 TH |
181 | ATA_CMD_WRITE_EXT, |
182 | 0, | |
183 | 0, | |
184 | 0, | |
185 | ATA_CMD_WRITE_FUA_EXT | |
8cbd6df1 | 186 | }; |
1da177e4 LT |
187 | |
188 | /** | |
8cbd6df1 AL |
189 | * ata_rwcmd_protocol - set taskfile r/w commands and protocol |
190 | * @qc: command to examine and configure | |
1da177e4 | 191 | * |
8cbd6df1 AL |
192 | * Examine the device configuration and tf->flags to calculate |
193 | * the proper read/write commands and protocol to use. | |
1da177e4 LT |
194 | * |
195 | * LOCKING: | |
196 | * caller. | |
197 | */ | |
9a3dccc4 | 198 | int ata_rwcmd_protocol(struct ata_queued_cmd *qc) |
1da177e4 | 199 | { |
8cbd6df1 AL |
200 | struct ata_taskfile *tf = &qc->tf; |
201 | struct ata_device *dev = qc->dev; | |
9a3dccc4 | 202 | u8 cmd; |
1da177e4 | 203 | |
9a3dccc4 | 204 | int index, fua, lba48, write; |
8cbd6df1 | 205 | |
9a3dccc4 | 206 | fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0; |
8cbd6df1 AL |
207 | lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0; |
208 | write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0; | |
1da177e4 | 209 | |
8cbd6df1 AL |
210 | if (dev->flags & ATA_DFLAG_PIO) { |
211 | tf->protocol = ATA_PROT_PIO; | |
9a3dccc4 | 212 | index = dev->multi_count ? 0 : 8; |
8d238e01 AC |
213 | } else if (lba48 && (qc->ap->flags & ATA_FLAG_PIO_LBA48)) { |
214 | /* Unable to use DMA due to host limitation */ | |
215 | tf->protocol = ATA_PROT_PIO; | |
216 | index = dev->multi_count ? 0 : 4; | |
8cbd6df1 AL |
217 | } else { |
218 | tf->protocol = ATA_PROT_DMA; | |
9a3dccc4 | 219 | index = 16; |
8cbd6df1 | 220 | } |
1da177e4 | 221 | |
9a3dccc4 TH |
222 | cmd = ata_rw_cmds[index + fua + lba48 + write]; |
223 | if (cmd) { | |
224 | tf->command = cmd; | |
225 | return 0; | |
226 | } | |
227 | return -1; | |
1da177e4 LT |
228 | } |
229 | ||
98ac62de | 230 | static const char * const xfer_mode_str[] = { |
1da177e4 LT |
231 | "UDMA/16", |
232 | "UDMA/25", | |
233 | "UDMA/33", | |
234 | "UDMA/44", | |
235 | "UDMA/66", | |
236 | "UDMA/100", | |
237 | "UDMA/133", | |
238 | "UDMA7", | |
239 | "MWDMA0", | |
240 | "MWDMA1", | |
241 | "MWDMA2", | |
242 | "PIO0", | |
243 | "PIO1", | |
244 | "PIO2", | |
245 | "PIO3", | |
246 | "PIO4", | |
247 | }; | |
248 | ||
249 | /** | |
250 | * ata_udma_string - convert UDMA bit offset to string | |
251 | * @mask: mask of bits supported; only highest bit counts. | |
252 | * | |
253 | * Determine string which represents the highest speed | |
254 | * (highest bit in @udma_mask). | |
255 | * | |
256 | * LOCKING: | |
257 | * None. | |
258 | * | |
259 | * RETURNS: | |
260 | * Constant C string representing highest speed listed in | |
261 | * @udma_mask, or the constant C string "<n/a>". | |
262 | */ | |
263 | ||
264 | static const char *ata_mode_string(unsigned int mask) | |
265 | { | |
266 | int i; | |
267 | ||
268 | for (i = 7; i >= 0; i--) | |
269 | if (mask & (1 << i)) | |
270 | goto out; | |
271 | for (i = ATA_SHIFT_MWDMA + 2; i >= ATA_SHIFT_MWDMA; i--) | |
272 | if (mask & (1 << i)) | |
273 | goto out; | |
274 | for (i = ATA_SHIFT_PIO + 4; i >= ATA_SHIFT_PIO; i--) | |
275 | if (mask & (1 << i)) | |
276 | goto out; | |
277 | ||
278 | return "<n/a>"; | |
279 | ||
280 | out: | |
281 | return xfer_mode_str[i]; | |
282 | } | |
283 | ||
284 | /** | |
285 | * ata_pio_devchk - PATA device presence detection | |
286 | * @ap: ATA channel to examine | |
287 | * @device: Device to examine (starting at zero) | |
288 | * | |
289 | * This technique was originally described in | |
290 | * Hale Landis's ATADRVR (www.ata-atapi.com), and | |
291 | * later found its way into the ATA/ATAPI spec. | |
292 | * | |
293 | * Write a pattern to the ATA shadow registers, | |
294 | * and if a device is present, it will respond by | |
295 | * correctly storing and echoing back the | |
296 | * ATA shadow register contents. | |
297 | * | |
298 | * LOCKING: | |
299 | * caller. | |
300 | */ | |
301 | ||
302 | static unsigned int ata_pio_devchk(struct ata_port *ap, | |
303 | unsigned int device) | |
304 | { | |
305 | struct ata_ioports *ioaddr = &ap->ioaddr; | |
306 | u8 nsect, lbal; | |
307 | ||
308 | ap->ops->dev_select(ap, device); | |
309 | ||
310 | outb(0x55, ioaddr->nsect_addr); | |
311 | outb(0xaa, ioaddr->lbal_addr); | |
312 | ||
313 | outb(0xaa, ioaddr->nsect_addr); | |
314 | outb(0x55, ioaddr->lbal_addr); | |
315 | ||
316 | outb(0x55, ioaddr->nsect_addr); | |
317 | outb(0xaa, ioaddr->lbal_addr); | |
318 | ||
319 | nsect = inb(ioaddr->nsect_addr); | |
320 | lbal = inb(ioaddr->lbal_addr); | |
321 | ||
322 | if ((nsect == 0x55) && (lbal == 0xaa)) | |
323 | return 1; /* we found a device */ | |
324 | ||
325 | return 0; /* nothing found */ | |
326 | } | |
327 | ||
328 | /** | |
329 | * ata_mmio_devchk - PATA device presence detection | |
330 | * @ap: ATA channel to examine | |
331 | * @device: Device to examine (starting at zero) | |
332 | * | |
333 | * This technique was originally described in | |
334 | * Hale Landis's ATADRVR (www.ata-atapi.com), and | |
335 | * later found its way into the ATA/ATAPI spec. | |
336 | * | |
337 | * Write a pattern to the ATA shadow registers, | |
338 | * and if a device is present, it will respond by | |
339 | * correctly storing and echoing back the | |
340 | * ATA shadow register contents. | |
341 | * | |
342 | * LOCKING: | |
343 | * caller. | |
344 | */ | |
345 | ||
346 | static unsigned int ata_mmio_devchk(struct ata_port *ap, | |
347 | unsigned int device) | |
348 | { | |
349 | struct ata_ioports *ioaddr = &ap->ioaddr; | |
350 | u8 nsect, lbal; | |
351 | ||
352 | ap->ops->dev_select(ap, device); | |
353 | ||
354 | writeb(0x55, (void __iomem *) ioaddr->nsect_addr); | |
355 | writeb(0xaa, (void __iomem *) ioaddr->lbal_addr); | |
356 | ||
357 | writeb(0xaa, (void __iomem *) ioaddr->nsect_addr); | |
358 | writeb(0x55, (void __iomem *) ioaddr->lbal_addr); | |
359 | ||
360 | writeb(0x55, (void __iomem *) ioaddr->nsect_addr); | |
361 | writeb(0xaa, (void __iomem *) ioaddr->lbal_addr); | |
362 | ||
363 | nsect = readb((void __iomem *) ioaddr->nsect_addr); | |
364 | lbal = readb((void __iomem *) ioaddr->lbal_addr); | |
365 | ||
366 | if ((nsect == 0x55) && (lbal == 0xaa)) | |
367 | return 1; /* we found a device */ | |
368 | ||
369 | return 0; /* nothing found */ | |
370 | } | |
371 | ||
372 | /** | |
373 | * ata_devchk - PATA device presence detection | |
374 | * @ap: ATA channel to examine | |
375 | * @device: Device to examine (starting at zero) | |
376 | * | |
377 | * Dispatch ATA device presence detection, depending | |
378 | * on whether we are using PIO or MMIO to talk to the | |
379 | * ATA shadow registers. | |
380 | * | |
381 | * LOCKING: | |
382 | * caller. | |
383 | */ | |
384 | ||
385 | static unsigned int ata_devchk(struct ata_port *ap, | |
386 | unsigned int device) | |
387 | { | |
388 | if (ap->flags & ATA_FLAG_MMIO) | |
389 | return ata_mmio_devchk(ap, device); | |
390 | return ata_pio_devchk(ap, device); | |
391 | } | |
392 | ||
393 | /** | |
394 | * ata_dev_classify - determine device type based on ATA-spec signature | |
395 | * @tf: ATA taskfile register set for device to be identified | |
396 | * | |
397 | * Determine from taskfile register contents whether a device is | |
398 | * ATA or ATAPI, as per "Signature and persistence" section | |
399 | * of ATA/PI spec (volume 1, sect 5.14). | |
400 | * | |
401 | * LOCKING: | |
402 | * None. | |
403 | * | |
404 | * RETURNS: | |
405 | * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN | |
406 | * the event of failure. | |
407 | */ | |
408 | ||
057ace5e | 409 | unsigned int ata_dev_classify(const struct ata_taskfile *tf) |
1da177e4 LT |
410 | { |
411 | /* Apple's open source Darwin code hints that some devices only | |
412 | * put a proper signature into the LBA mid/high registers, | |
413 | * So, we only check those. It's sufficient for uniqueness. | |
414 | */ | |
415 | ||
416 | if (((tf->lbam == 0) && (tf->lbah == 0)) || | |
417 | ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) { | |
418 | DPRINTK("found ATA device by sig\n"); | |
419 | return ATA_DEV_ATA; | |
420 | } | |
421 | ||
422 | if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) || | |
423 | ((tf->lbam == 0x69) && (tf->lbah == 0x96))) { | |
424 | DPRINTK("found ATAPI device by sig\n"); | |
425 | return ATA_DEV_ATAPI; | |
426 | } | |
427 | ||
428 | DPRINTK("unknown device\n"); | |
429 | return ATA_DEV_UNKNOWN; | |
430 | } | |
431 | ||
432 | /** | |
433 | * ata_dev_try_classify - Parse returned ATA device signature | |
434 | * @ap: ATA channel to examine | |
435 | * @device: Device to examine (starting at zero) | |
b4dc7623 | 436 | * @r_err: Value of error register on completion |
1da177e4 LT |
437 | * |
438 | * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs, | |
439 | * an ATA/ATAPI-defined set of values is placed in the ATA | |
440 | * shadow registers, indicating the results of device detection | |
441 | * and diagnostics. | |
442 | * | |
443 | * Select the ATA device, and read the values from the ATA shadow | |
444 | * registers. Then parse according to the Error register value, | |
445 | * and the spec-defined values examined by ata_dev_classify(). | |
446 | * | |
447 | * LOCKING: | |
448 | * caller. | |
b4dc7623 TH |
449 | * |
450 | * RETURNS: | |
451 | * Device type - %ATA_DEV_ATA, %ATA_DEV_ATAPI or %ATA_DEV_NONE. | |
1da177e4 LT |
452 | */ |
453 | ||
b4dc7623 TH |
454 | static unsigned int |
455 | ata_dev_try_classify(struct ata_port *ap, unsigned int device, u8 *r_err) | |
1da177e4 | 456 | { |
1da177e4 LT |
457 | struct ata_taskfile tf; |
458 | unsigned int class; | |
459 | u8 err; | |
460 | ||
461 | ap->ops->dev_select(ap, device); | |
462 | ||
463 | memset(&tf, 0, sizeof(tf)); | |
464 | ||
1da177e4 | 465 | ap->ops->tf_read(ap, &tf); |
0169e284 | 466 | err = tf.feature; |
b4dc7623 TH |
467 | if (r_err) |
468 | *r_err = err; | |
1da177e4 LT |
469 | |
470 | /* see if device passed diags */ | |
471 | if (err == 1) | |
472 | /* do nothing */ ; | |
473 | else if ((device == 0) && (err == 0x81)) | |
474 | /* do nothing */ ; | |
475 | else | |
b4dc7623 | 476 | return ATA_DEV_NONE; |
1da177e4 | 477 | |
b4dc7623 | 478 | /* determine if device is ATA or ATAPI */ |
1da177e4 | 479 | class = ata_dev_classify(&tf); |
b4dc7623 | 480 | |
1da177e4 | 481 | if (class == ATA_DEV_UNKNOWN) |
b4dc7623 | 482 | return ATA_DEV_NONE; |
1da177e4 | 483 | if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0)) |
b4dc7623 TH |
484 | return ATA_DEV_NONE; |
485 | return class; | |
1da177e4 LT |
486 | } |
487 | ||
488 | /** | |
489 | * ata_dev_id_string - Convert IDENTIFY DEVICE page into string | |
490 | * @id: IDENTIFY DEVICE results we will examine | |
491 | * @s: string into which data is output | |
492 | * @ofs: offset into identify device page | |
493 | * @len: length of string to return. must be an even number. | |
494 | * | |
495 | * The strings in the IDENTIFY DEVICE page are broken up into | |
496 | * 16-bit chunks. Run through the string, and output each | |
497 | * 8-bit chunk linearly, regardless of platform. | |
498 | * | |
499 | * LOCKING: | |
500 | * caller. | |
501 | */ | |
502 | ||
057ace5e | 503 | void ata_dev_id_string(const u16 *id, unsigned char *s, |
1da177e4 LT |
504 | unsigned int ofs, unsigned int len) |
505 | { | |
506 | unsigned int c; | |
507 | ||
508 | while (len > 0) { | |
509 | c = id[ofs] >> 8; | |
510 | *s = c; | |
511 | s++; | |
512 | ||
513 | c = id[ofs] & 0xff; | |
514 | *s = c; | |
515 | s++; | |
516 | ||
517 | ofs++; | |
518 | len -= 2; | |
519 | } | |
520 | } | |
521 | ||
0baab86b EF |
522 | |
523 | /** | |
524 | * ata_noop_dev_select - Select device 0/1 on ATA bus | |
525 | * @ap: ATA channel to manipulate | |
526 | * @device: ATA device (numbered from zero) to select | |
527 | * | |
528 | * This function performs no actual function. | |
529 | * | |
530 | * May be used as the dev_select() entry in ata_port_operations. | |
531 | * | |
532 | * LOCKING: | |
533 | * caller. | |
534 | */ | |
1da177e4 LT |
535 | void ata_noop_dev_select (struct ata_port *ap, unsigned int device) |
536 | { | |
537 | } | |
538 | ||
0baab86b | 539 | |
1da177e4 LT |
540 | /** |
541 | * ata_std_dev_select - Select device 0/1 on ATA bus | |
542 | * @ap: ATA channel to manipulate | |
543 | * @device: ATA device (numbered from zero) to select | |
544 | * | |
545 | * Use the method defined in the ATA specification to | |
546 | * make either device 0, or device 1, active on the | |
0baab86b EF |
547 | * ATA channel. Works with both PIO and MMIO. |
548 | * | |
549 | * May be used as the dev_select() entry in ata_port_operations. | |
1da177e4 LT |
550 | * |
551 | * LOCKING: | |
552 | * caller. | |
553 | */ | |
554 | ||
555 | void ata_std_dev_select (struct ata_port *ap, unsigned int device) | |
556 | { | |
557 | u8 tmp; | |
558 | ||
559 | if (device == 0) | |
560 | tmp = ATA_DEVICE_OBS; | |
561 | else | |
562 | tmp = ATA_DEVICE_OBS | ATA_DEV1; | |
563 | ||
564 | if (ap->flags & ATA_FLAG_MMIO) { | |
565 | writeb(tmp, (void __iomem *) ap->ioaddr.device_addr); | |
566 | } else { | |
567 | outb(tmp, ap->ioaddr.device_addr); | |
568 | } | |
569 | ata_pause(ap); /* needed; also flushes, for mmio */ | |
570 | } | |
571 | ||
572 | /** | |
573 | * ata_dev_select - Select device 0/1 on ATA bus | |
574 | * @ap: ATA channel to manipulate | |
575 | * @device: ATA device (numbered from zero) to select | |
576 | * @wait: non-zero to wait for Status register BSY bit to clear | |
577 | * @can_sleep: non-zero if context allows sleeping | |
578 | * | |
579 | * Use the method defined in the ATA specification to | |
580 | * make either device 0, or device 1, active on the | |
581 | * ATA channel. | |
582 | * | |
583 | * This is a high-level version of ata_std_dev_select(), | |
584 | * which additionally provides the services of inserting | |
585 | * the proper pauses and status polling, where needed. | |
586 | * | |
587 | * LOCKING: | |
588 | * caller. | |
589 | */ | |
590 | ||
591 | void ata_dev_select(struct ata_port *ap, unsigned int device, | |
592 | unsigned int wait, unsigned int can_sleep) | |
593 | { | |
594 | VPRINTK("ENTER, ata%u: device %u, wait %u\n", | |
595 | ap->id, device, wait); | |
596 | ||
597 | if (wait) | |
598 | ata_wait_idle(ap); | |
599 | ||
600 | ap->ops->dev_select(ap, device); | |
601 | ||
602 | if (wait) { | |
603 | if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI) | |
604 | msleep(150); | |
605 | ata_wait_idle(ap); | |
606 | } | |
607 | } | |
608 | ||
609 | /** | |
610 | * ata_dump_id - IDENTIFY DEVICE info debugging output | |
611 | * @dev: Device whose IDENTIFY DEVICE page we will dump | |
612 | * | |
613 | * Dump selected 16-bit words from a detected device's | |
614 | * IDENTIFY PAGE page. | |
615 | * | |
616 | * LOCKING: | |
617 | * caller. | |
618 | */ | |
619 | ||
057ace5e | 620 | static inline void ata_dump_id(const struct ata_device *dev) |
1da177e4 LT |
621 | { |
622 | DPRINTK("49==0x%04x " | |
623 | "53==0x%04x " | |
624 | "63==0x%04x " | |
625 | "64==0x%04x " | |
626 | "75==0x%04x \n", | |
627 | dev->id[49], | |
628 | dev->id[53], | |
629 | dev->id[63], | |
630 | dev->id[64], | |
631 | dev->id[75]); | |
632 | DPRINTK("80==0x%04x " | |
633 | "81==0x%04x " | |
634 | "82==0x%04x " | |
635 | "83==0x%04x " | |
636 | "84==0x%04x \n", | |
637 | dev->id[80], | |
638 | dev->id[81], | |
639 | dev->id[82], | |
640 | dev->id[83], | |
641 | dev->id[84]); | |
642 | DPRINTK("88==0x%04x " | |
643 | "93==0x%04x\n", | |
644 | dev->id[88], | |
645 | dev->id[93]); | |
646 | } | |
647 | ||
11e29e21 AC |
648 | /* |
649 | * Compute the PIO modes available for this device. This is not as | |
650 | * trivial as it seems if we must consider early devices correctly. | |
651 | * | |
652 | * FIXME: pre IDE drive timing (do we care ?). | |
653 | */ | |
654 | ||
057ace5e | 655 | static unsigned int ata_pio_modes(const struct ata_device *adev) |
11e29e21 AC |
656 | { |
657 | u16 modes; | |
658 | ||
ffa29456 AC |
659 | /* Usual case. Word 53 indicates word 64 is valid */ |
660 | if (adev->id[ATA_ID_FIELD_VALID] & (1 << 1)) { | |
11e29e21 AC |
661 | modes = adev->id[ATA_ID_PIO_MODES] & 0x03; |
662 | modes <<= 3; | |
663 | modes |= 0x7; | |
664 | return modes; | |
665 | } | |
666 | ||
ffa29456 AC |
667 | /* If word 64 isn't valid then Word 51 high byte holds the PIO timing |
668 | number for the maximum. Turn it into a mask and return it */ | |
669 | modes = (2 << ((adev->id[ATA_ID_OLD_PIO_MODES] >> 8) & 0xFF)) - 1 ; | |
11e29e21 | 670 | return modes; |
ffa29456 AC |
671 | /* But wait.. there's more. Design your standards by committee and |
672 | you too can get a free iordy field to process. However its the | |
673 | speeds not the modes that are supported... Note drivers using the | |
674 | timing API will get this right anyway */ | |
11e29e21 AC |
675 | } |
676 | ||
95064379 TH |
677 | static inline void |
678 | ata_queue_packet_task(struct ata_port *ap) | |
679 | { | |
c18d06f8 TH |
680 | if (!(ap->flags & ATA_FLAG_FLUSH_PIO_TASK)) |
681 | queue_work(ata_wq, &ap->packet_task); | |
95064379 TH |
682 | } |
683 | ||
684 | static inline void | |
685 | ata_queue_pio_task(struct ata_port *ap) | |
686 | { | |
c18d06f8 TH |
687 | if (!(ap->flags & ATA_FLAG_FLUSH_PIO_TASK)) |
688 | queue_work(ata_wq, &ap->pio_task); | |
95064379 TH |
689 | } |
690 | ||
691 | static inline void | |
692 | ata_queue_delayed_pio_task(struct ata_port *ap, unsigned long delay) | |
693 | { | |
c18d06f8 TH |
694 | if (!(ap->flags & ATA_FLAG_FLUSH_PIO_TASK)) |
695 | queue_delayed_work(ata_wq, &ap->pio_task, delay); | |
696 | } | |
697 | ||
698 | /** | |
699 | * ata_flush_pio_tasks - Flush pio_task and packet_task | |
700 | * @ap: the target ata_port | |
701 | * | |
702 | * After this function completes, pio_task and packet_task are | |
703 | * guranteed not to be running or scheduled. | |
704 | * | |
705 | * LOCKING: | |
706 | * Kernel thread context (may sleep) | |
707 | */ | |
708 | ||
709 | static void ata_flush_pio_tasks(struct ata_port *ap) | |
710 | { | |
711 | int tmp = 0; | |
712 | unsigned long flags; | |
713 | ||
714 | DPRINTK("ENTER\n"); | |
715 | ||
716 | spin_lock_irqsave(&ap->host_set->lock, flags); | |
717 | ap->flags |= ATA_FLAG_FLUSH_PIO_TASK; | |
718 | spin_unlock_irqrestore(&ap->host_set->lock, flags); | |
719 | ||
720 | DPRINTK("flush #1\n"); | |
721 | flush_workqueue(ata_wq); | |
722 | ||
723 | /* | |
724 | * At this point, if a task is running, it's guaranteed to see | |
725 | * the FLUSH flag; thus, it will never queue pio tasks again. | |
726 | * Cancel and flush. | |
727 | */ | |
728 | tmp |= cancel_delayed_work(&ap->pio_task); | |
729 | tmp |= cancel_delayed_work(&ap->packet_task); | |
730 | if (!tmp) { | |
731 | DPRINTK("flush #2\n"); | |
732 | flush_workqueue(ata_wq); | |
733 | } | |
734 | ||
735 | spin_lock_irqsave(&ap->host_set->lock, flags); | |
736 | ap->flags &= ~ATA_FLAG_FLUSH_PIO_TASK; | |
737 | spin_unlock_irqrestore(&ap->host_set->lock, flags); | |
738 | ||
739 | DPRINTK("EXIT\n"); | |
95064379 TH |
740 | } |
741 | ||
77853bf2 | 742 | void ata_qc_complete_internal(struct ata_queued_cmd *qc) |
a2a7a662 | 743 | { |
77853bf2 | 744 | struct completion *waiting = qc->private_data; |
a2a7a662 | 745 | |
77853bf2 | 746 | qc->ap->ops->tf_read(qc->ap, &qc->tf); |
a2a7a662 | 747 | complete(waiting); |
a2a7a662 TH |
748 | } |
749 | ||
750 | /** | |
751 | * ata_exec_internal - execute libata internal command | |
752 | * @ap: Port to which the command is sent | |
753 | * @dev: Device to which the command is sent | |
754 | * @tf: Taskfile registers for the command and the result | |
755 | * @dma_dir: Data tranfer direction of the command | |
756 | * @buf: Data buffer of the command | |
757 | * @buflen: Length of data buffer | |
758 | * | |
759 | * Executes libata internal command with timeout. @tf contains | |
760 | * command on entry and result on return. Timeout and error | |
761 | * conditions are reported via return value. No recovery action | |
762 | * is taken after a command times out. It's caller's duty to | |
763 | * clean up after timeout. | |
764 | * | |
765 | * LOCKING: | |
766 | * None. Should be called with kernel context, might sleep. | |
767 | */ | |
768 | ||
769 | static unsigned | |
770 | ata_exec_internal(struct ata_port *ap, struct ata_device *dev, | |
771 | struct ata_taskfile *tf, | |
772 | int dma_dir, void *buf, unsigned int buflen) | |
773 | { | |
774 | u8 command = tf->command; | |
775 | struct ata_queued_cmd *qc; | |
776 | DECLARE_COMPLETION(wait); | |
777 | unsigned long flags; | |
77853bf2 | 778 | unsigned int err_mask; |
a2a7a662 TH |
779 | |
780 | spin_lock_irqsave(&ap->host_set->lock, flags); | |
781 | ||
782 | qc = ata_qc_new_init(ap, dev); | |
783 | BUG_ON(qc == NULL); | |
784 | ||
785 | qc->tf = *tf; | |
786 | qc->dma_dir = dma_dir; | |
787 | if (dma_dir != DMA_NONE) { | |
788 | ata_sg_init_one(qc, buf, buflen); | |
789 | qc->nsect = buflen / ATA_SECT_SIZE; | |
790 | } | |
791 | ||
77853bf2 | 792 | qc->private_data = &wait; |
a2a7a662 TH |
793 | qc->complete_fn = ata_qc_complete_internal; |
794 | ||
9a3d9eb0 TH |
795 | qc->err_mask = ata_qc_issue(qc); |
796 | if (qc->err_mask) | |
8e436af9 | 797 | ata_qc_complete(qc); |
a2a7a662 TH |
798 | |
799 | spin_unlock_irqrestore(&ap->host_set->lock, flags); | |
800 | ||
801 | if (!wait_for_completion_timeout(&wait, ATA_TMOUT_INTERNAL)) { | |
802 | spin_lock_irqsave(&ap->host_set->lock, flags); | |
803 | ||
804 | /* We're racing with irq here. If we lose, the | |
805 | * following test prevents us from completing the qc | |
806 | * again. If completion irq occurs after here but | |
807 | * before the caller cleans up, it will result in a | |
808 | * spurious interrupt. We can live with that. | |
809 | */ | |
77853bf2 | 810 | if (qc->flags & ATA_QCFLAG_ACTIVE) { |
11a56d24 | 811 | qc->err_mask = AC_ERR_TIMEOUT; |
a2a7a662 TH |
812 | ata_qc_complete(qc); |
813 | printk(KERN_WARNING "ata%u: qc timeout (cmd 0x%x)\n", | |
814 | ap->id, command); | |
815 | } | |
816 | ||
817 | spin_unlock_irqrestore(&ap->host_set->lock, flags); | |
818 | } | |
819 | ||
77853bf2 TH |
820 | *tf = qc->tf; |
821 | err_mask = qc->err_mask; | |
822 | ||
823 | ata_qc_free(qc); | |
824 | ||
825 | return err_mask; | |
a2a7a662 TH |
826 | } |
827 | ||
1bc4ccff AC |
828 | /** |
829 | * ata_pio_need_iordy - check if iordy needed | |
830 | * @adev: ATA device | |
831 | * | |
832 | * Check if the current speed of the device requires IORDY. Used | |
833 | * by various controllers for chip configuration. | |
834 | */ | |
835 | ||
836 | unsigned int ata_pio_need_iordy(const struct ata_device *adev) | |
837 | { | |
838 | int pio; | |
839 | int speed = adev->pio_mode - XFER_PIO_0; | |
840 | ||
841 | if (speed < 2) | |
842 | return 0; | |
843 | if (speed > 2) | |
844 | return 1; | |
845 | ||
846 | /* If we have no drive specific rule, then PIO 2 is non IORDY */ | |
847 | ||
848 | if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */ | |
849 | pio = adev->id[ATA_ID_EIDE_PIO]; | |
850 | /* Is the speed faster than the drive allows non IORDY ? */ | |
851 | if (pio) { | |
852 | /* This is cycle times not frequency - watch the logic! */ | |
853 | if (pio > 240) /* PIO2 is 240nS per cycle */ | |
854 | return 1; | |
855 | return 0; | |
856 | } | |
857 | } | |
858 | return 0; | |
859 | } | |
860 | ||
1da177e4 LT |
861 | /** |
862 | * ata_dev_identify - obtain IDENTIFY x DEVICE page | |
863 | * @ap: port on which device we wish to probe resides | |
864 | * @device: device bus address, starting at zero | |
865 | * | |
866 | * Following bus reset, we issue the IDENTIFY [PACKET] DEVICE | |
867 | * command, and read back the 512-byte device information page. | |
868 | * The device information page is fed to us via the standard | |
869 | * PIO-IN protocol, but we hand-code it here. (TODO: investigate | |
870 | * using standard PIO-IN paths) | |
871 | * | |
872 | * After reading the device information page, we use several | |
873 | * bits of information from it to initialize data structures | |
874 | * that will be used during the lifetime of the ata_device. | |
875 | * Other data from the info page is used to disqualify certain | |
876 | * older ATA devices we do not wish to support. | |
877 | * | |
878 | * LOCKING: | |
879 | * Inherited from caller. Some functions called by this function | |
880 | * obtain the host_set lock. | |
881 | */ | |
882 | ||
883 | static void ata_dev_identify(struct ata_port *ap, unsigned int device) | |
884 | { | |
885 | struct ata_device *dev = &ap->device[device]; | |
8bf62ece | 886 | unsigned int major_version; |
1da177e4 LT |
887 | u16 tmp; |
888 | unsigned long xfer_modes; | |
1da177e4 | 889 | unsigned int using_edd; |
a0123703 TH |
890 | struct ata_taskfile tf; |
891 | unsigned int err_mask; | |
1da177e4 LT |
892 | int rc; |
893 | ||
894 | if (!ata_dev_present(dev)) { | |
895 | DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n", | |
896 | ap->id, device); | |
897 | return; | |
898 | } | |
899 | ||
900 | if (ap->flags & (ATA_FLAG_SRST | ATA_FLAG_SATA_RESET)) | |
901 | using_edd = 0; | |
902 | else | |
903 | using_edd = 1; | |
904 | ||
905 | DPRINTK("ENTER, host %u, dev %u\n", ap->id, device); | |
906 | ||
907 | assert (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ATAPI || | |
908 | dev->class == ATA_DEV_NONE); | |
909 | ||
910 | ata_dev_select(ap, device, 1, 1); /* select device 0/1 */ | |
911 | ||
1da177e4 | 912 | retry: |
a0123703 TH |
913 | ata_tf_init(ap, &tf, device); |
914 | ||
1da177e4 | 915 | if (dev->class == ATA_DEV_ATA) { |
a0123703 | 916 | tf.command = ATA_CMD_ID_ATA; |
1da177e4 LT |
917 | DPRINTK("do ATA identify\n"); |
918 | } else { | |
a0123703 | 919 | tf.command = ATA_CMD_ID_ATAPI; |
1da177e4 LT |
920 | DPRINTK("do ATAPI identify\n"); |
921 | } | |
922 | ||
a0123703 | 923 | tf.protocol = ATA_PROT_PIO; |
1da177e4 | 924 | |
a0123703 TH |
925 | err_mask = ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE, |
926 | dev->id, sizeof(dev->id)); | |
1da177e4 | 927 | |
a0123703 TH |
928 | if (err_mask) { |
929 | if (err_mask & ~AC_ERR_DEV) | |
930 | goto err_out; | |
0169e284 | 931 | |
1da177e4 LT |
932 | /* |
933 | * arg! EDD works for all test cases, but seems to return | |
934 | * the ATA signature for some ATAPI devices. Until the | |
935 | * reason for this is found and fixed, we fix up the mess | |
936 | * here. If IDENTIFY DEVICE returns command aborted | |
937 | * (as ATAPI devices do), then we issue an | |
938 | * IDENTIFY PACKET DEVICE. | |
939 | * | |
940 | * ATA software reset (SRST, the default) does not appear | |
941 | * to have this problem. | |
942 | */ | |
7c398335 | 943 | if ((using_edd) && (dev->class == ATA_DEV_ATA)) { |
a0123703 | 944 | u8 err = tf.feature; |
1da177e4 LT |
945 | if (err & ATA_ABORTED) { |
946 | dev->class = ATA_DEV_ATAPI; | |
1da177e4 LT |
947 | goto retry; |
948 | } | |
949 | } | |
950 | goto err_out; | |
951 | } | |
952 | ||
953 | swap_buf_le16(dev->id, ATA_ID_WORDS); | |
954 | ||
955 | /* print device capabilities */ | |
956 | printk(KERN_DEBUG "ata%u: dev %u cfg " | |
957 | "49:%04x 82:%04x 83:%04x 84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n", | |
958 | ap->id, device, dev->id[49], | |
959 | dev->id[82], dev->id[83], dev->id[84], | |
960 | dev->id[85], dev->id[86], dev->id[87], | |
961 | dev->id[88]); | |
962 | ||
963 | /* | |
964 | * common ATA, ATAPI feature tests | |
965 | */ | |
966 | ||
8bf62ece AL |
967 | /* we require DMA support (bits 8 of word 49) */ |
968 | if (!ata_id_has_dma(dev->id)) { | |
969 | printk(KERN_DEBUG "ata%u: no dma\n", ap->id); | |
1da177e4 LT |
970 | goto err_out_nosup; |
971 | } | |
972 | ||
973 | /* quick-n-dirty find max transfer mode; for printk only */ | |
974 | xfer_modes = dev->id[ATA_ID_UDMA_MODES]; | |
975 | if (!xfer_modes) | |
976 | xfer_modes = (dev->id[ATA_ID_MWDMA_MODES]) << ATA_SHIFT_MWDMA; | |
11e29e21 AC |
977 | if (!xfer_modes) |
978 | xfer_modes = ata_pio_modes(dev); | |
1da177e4 LT |
979 | |
980 | ata_dump_id(dev); | |
981 | ||
982 | /* ATA-specific feature tests */ | |
983 | if (dev->class == ATA_DEV_ATA) { | |
984 | if (!ata_id_is_ata(dev->id)) /* sanity check */ | |
985 | goto err_out_nosup; | |
986 | ||
8bf62ece | 987 | /* get major version */ |
1da177e4 | 988 | tmp = dev->id[ATA_ID_MAJOR_VER]; |
8bf62ece AL |
989 | for (major_version = 14; major_version >= 1; major_version--) |
990 | if (tmp & (1 << major_version)) | |
1da177e4 LT |
991 | break; |
992 | ||
8bf62ece AL |
993 | /* |
994 | * The exact sequence expected by certain pre-ATA4 drives is: | |
995 | * SRST RESET | |
996 | * IDENTIFY | |
997 | * INITIALIZE DEVICE PARAMETERS | |
998 | * anything else.. | |
999 | * Some drives were very specific about that exact sequence. | |
1000 | */ | |
59a10b17 | 1001 | if (major_version < 4 || (!ata_id_has_lba(dev->id))) { |
8bf62ece AL |
1002 | ata_dev_init_params(ap, dev); |
1003 | ||
59a10b17 AL |
1004 | /* current CHS translation info (id[53-58]) might be |
1005 | * changed. reread the identify device info. | |
1006 | */ | |
1007 | ata_dev_reread_id(ap, dev); | |
1008 | } | |
1009 | ||
8bf62ece AL |
1010 | if (ata_id_has_lba(dev->id)) { |
1011 | dev->flags |= ATA_DFLAG_LBA; | |
1012 | ||
1013 | if (ata_id_has_lba48(dev->id)) { | |
1014 | dev->flags |= ATA_DFLAG_LBA48; | |
1015 | dev->n_sectors = ata_id_u64(dev->id, 100); | |
1016 | } else { | |
1017 | dev->n_sectors = ata_id_u32(dev->id, 60); | |
1018 | } | |
1019 | ||
1020 | /* print device info to dmesg */ | |
1021 | printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors:%s\n", | |
1022 | ap->id, device, | |
1023 | major_version, | |
1024 | ata_mode_string(xfer_modes), | |
1025 | (unsigned long long)dev->n_sectors, | |
1026 | dev->flags & ATA_DFLAG_LBA48 ? " LBA48" : " LBA"); | |
1027 | } else { | |
1028 | /* CHS */ | |
1029 | ||
1030 | /* Default translation */ | |
1031 | dev->cylinders = dev->id[1]; | |
1032 | dev->heads = dev->id[3]; | |
1033 | dev->sectors = dev->id[6]; | |
1034 | dev->n_sectors = dev->cylinders * dev->heads * dev->sectors; | |
1035 | ||
1036 | if (ata_id_current_chs_valid(dev->id)) { | |
1037 | /* Current CHS translation is valid. */ | |
1038 | dev->cylinders = dev->id[54]; | |
1039 | dev->heads = dev->id[55]; | |
1040 | dev->sectors = dev->id[56]; | |
1041 | ||
1042 | dev->n_sectors = ata_id_u32(dev->id, 57); | |
1043 | } | |
1044 | ||
1045 | /* print device info to dmesg */ | |
1046 | printk(KERN_INFO "ata%u: dev %u ATA-%d, max %s, %Lu sectors: CHS %d/%d/%d\n", | |
1047 | ap->id, device, | |
1048 | major_version, | |
1049 | ata_mode_string(xfer_modes), | |
1050 | (unsigned long long)dev->n_sectors, | |
1051 | (int)dev->cylinders, (int)dev->heads, (int)dev->sectors); | |
1da177e4 | 1052 | |
1da177e4 LT |
1053 | } |
1054 | ||
1055 | ap->host->max_cmd_len = 16; | |
1da177e4 LT |
1056 | } |
1057 | ||
1058 | /* ATAPI-specific feature tests */ | |
2c13b7ce | 1059 | else if (dev->class == ATA_DEV_ATAPI) { |
1da177e4 LT |
1060 | if (ata_id_is_ata(dev->id)) /* sanity check */ |
1061 | goto err_out_nosup; | |
1062 | ||
1063 | rc = atapi_cdb_len(dev->id); | |
1064 | if ((rc < 12) || (rc > ATAPI_CDB_LEN)) { | |
1065 | printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id); | |
1066 | goto err_out_nosup; | |
1067 | } | |
1068 | ap->cdb_len = (unsigned int) rc; | |
1069 | ap->host->max_cmd_len = (unsigned char) ap->cdb_len; | |
1070 | ||
1071 | /* print device info to dmesg */ | |
1072 | printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n", | |
1073 | ap->id, device, | |
1074 | ata_mode_string(xfer_modes)); | |
1075 | } | |
1076 | ||
1077 | DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap)); | |
1078 | return; | |
1079 | ||
1080 | err_out_nosup: | |
1081 | printk(KERN_WARNING "ata%u: dev %u not supported, ignoring\n", | |
1082 | ap->id, device); | |
1083 | err_out: | |
1084 | dev->class++; /* converts ATA_DEV_xxx into ATA_DEV_xxx_UNSUP */ | |
1085 | DPRINTK("EXIT, err\n"); | |
1086 | } | |
1087 | ||
6f2f3812 | 1088 | |
057ace5e | 1089 | static inline u8 ata_dev_knobble(const struct ata_port *ap) |
6f2f3812 BC |
1090 | { |
1091 | return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(ap->device->id))); | |
1092 | } | |
1093 | ||
1094 | /** | |
c893a3ae RD |
1095 | * ata_dev_config - Run device specific handlers & check for SATA->PATA bridges |
1096 | * @ap: Bus | |
1097 | * @i: Device | |
6f2f3812 | 1098 | * |
c893a3ae | 1099 | * LOCKING: |
6f2f3812 | 1100 | */ |
8a60a071 | 1101 | |
6f2f3812 BC |
1102 | void ata_dev_config(struct ata_port *ap, unsigned int i) |
1103 | { | |
1104 | /* limit bridge transfers to udma5, 200 sectors */ | |
1105 | if (ata_dev_knobble(ap)) { | |
1106 | printk(KERN_INFO "ata%u(%u): applying bridge limits\n", | |
1107 | ap->id, ap->device->devno); | |
1108 | ap->udma_mask &= ATA_UDMA5; | |
1109 | ap->host->max_sectors = ATA_MAX_SECTORS; | |
1110 | ap->host->hostt->max_sectors = ATA_MAX_SECTORS; | |
9d824d07 | 1111 | ap->device[i].flags |= ATA_DFLAG_LOCK_SECTORS; |
6f2f3812 BC |
1112 | } |
1113 | ||
1114 | if (ap->ops->dev_config) | |
1115 | ap->ops->dev_config(ap, &ap->device[i]); | |
1116 | } | |
1117 | ||
1da177e4 LT |
1118 | /** |
1119 | * ata_bus_probe - Reset and probe ATA bus | |
1120 | * @ap: Bus to probe | |
1121 | * | |
0cba632b JG |
1122 | * Master ATA bus probing function. Initiates a hardware-dependent |
1123 | * bus reset, then attempts to identify any devices found on | |
1124 | * the bus. | |
1125 | * | |
1da177e4 | 1126 | * LOCKING: |
0cba632b | 1127 | * PCI/etc. bus probe sem. |
1da177e4 LT |
1128 | * |
1129 | * RETURNS: | |
1130 | * Zero on success, non-zero on error. | |
1131 | */ | |
1132 | ||
1133 | static int ata_bus_probe(struct ata_port *ap) | |
1134 | { | |
1135 | unsigned int i, found = 0; | |
1136 | ||
c19ba8af TH |
1137 | if (ap->ops->probe_reset) { |
1138 | unsigned int classes[ATA_MAX_DEVICES]; | |
1139 | int rc; | |
1140 | ||
1141 | ata_port_probe(ap); | |
1142 | ||
1143 | rc = ap->ops->probe_reset(ap, classes); | |
1144 | if (rc == 0) { | |
1145 | for (i = 0; i < ATA_MAX_DEVICES; i++) | |
1146 | ap->device[i].class = classes[i]; | |
1147 | } else { | |
1148 | printk(KERN_ERR "ata%u: probe reset failed, " | |
1149 | "disabling port\n", ap->id); | |
1150 | ata_port_disable(ap); | |
1151 | } | |
1152 | } else | |
1153 | ap->ops->phy_reset(ap); | |
1154 | ||
1da177e4 LT |
1155 | if (ap->flags & ATA_FLAG_PORT_DISABLED) |
1156 | goto err_out; | |
1157 | ||
1158 | for (i = 0; i < ATA_MAX_DEVICES; i++) { | |
1159 | ata_dev_identify(ap, i); | |
1160 | if (ata_dev_present(&ap->device[i])) { | |
1161 | found = 1; | |
6f2f3812 | 1162 | ata_dev_config(ap,i); |
1da177e4 LT |
1163 | } |
1164 | } | |
1165 | ||
1166 | if ((!found) || (ap->flags & ATA_FLAG_PORT_DISABLED)) | |
1167 | goto err_out_disable; | |
1168 | ||
1169 | ata_set_mode(ap); | |
1170 | if (ap->flags & ATA_FLAG_PORT_DISABLED) | |
1171 | goto err_out_disable; | |
1172 | ||
1173 | return 0; | |
1174 | ||
1175 | err_out_disable: | |
1176 | ap->ops->port_disable(ap); | |
1177 | err_out: | |
1178 | return -1; | |
1179 | } | |
1180 | ||
1181 | /** | |
0cba632b JG |
1182 | * ata_port_probe - Mark port as enabled |
1183 | * @ap: Port for which we indicate enablement | |
1da177e4 | 1184 | * |
0cba632b JG |
1185 | * Modify @ap data structure such that the system |
1186 | * thinks that the entire port is enabled. | |
1187 | * | |
1188 | * LOCKING: host_set lock, or some other form of | |
1189 | * serialization. | |
1da177e4 LT |
1190 | */ |
1191 | ||
1192 | void ata_port_probe(struct ata_port *ap) | |
1193 | { | |
1194 | ap->flags &= ~ATA_FLAG_PORT_DISABLED; | |
1195 | } | |
1196 | ||
3be680b7 TH |
1197 | /** |
1198 | * sata_print_link_status - Print SATA link status | |
1199 | * @ap: SATA port to printk link status about | |
1200 | * | |
1201 | * This function prints link speed and status of a SATA link. | |
1202 | * | |
1203 | * LOCKING: | |
1204 | * None. | |
1205 | */ | |
1206 | static void sata_print_link_status(struct ata_port *ap) | |
1207 | { | |
1208 | u32 sstatus, tmp; | |
1209 | const char *speed; | |
1210 | ||
1211 | if (!ap->ops->scr_read) | |
1212 | return; | |
1213 | ||
1214 | sstatus = scr_read(ap, SCR_STATUS); | |
1215 | ||
1216 | if (sata_dev_present(ap)) { | |
1217 | tmp = (sstatus >> 4) & 0xf; | |
1218 | if (tmp & (1 << 0)) | |
1219 | speed = "1.5"; | |
1220 | else if (tmp & (1 << 1)) | |
1221 | speed = "3.0"; | |
1222 | else | |
1223 | speed = "<unknown>"; | |
1224 | printk(KERN_INFO "ata%u: SATA link up %s Gbps (SStatus %X)\n", | |
1225 | ap->id, speed, sstatus); | |
1226 | } else { | |
1227 | printk(KERN_INFO "ata%u: SATA link down (SStatus %X)\n", | |
1228 | ap->id, sstatus); | |
1229 | } | |
1230 | } | |
1231 | ||
1da177e4 | 1232 | /** |
780a87f7 JG |
1233 | * __sata_phy_reset - Wake/reset a low-level SATA PHY |
1234 | * @ap: SATA port associated with target SATA PHY. | |
1da177e4 | 1235 | * |
780a87f7 JG |
1236 | * This function issues commands to standard SATA Sxxx |
1237 | * PHY registers, to wake up the phy (and device), and | |
1238 | * clear any reset condition. | |
1da177e4 LT |
1239 | * |
1240 | * LOCKING: | |
0cba632b | 1241 | * PCI/etc. bus probe sem. |
1da177e4 LT |
1242 | * |
1243 | */ | |
1244 | void __sata_phy_reset(struct ata_port *ap) | |
1245 | { | |
1246 | u32 sstatus; | |
1247 | unsigned long timeout = jiffies + (HZ * 5); | |
1248 | ||
1249 | if (ap->flags & ATA_FLAG_SATA_RESET) { | |
cdcca89e BR |
1250 | /* issue phy wake/reset */ |
1251 | scr_write_flush(ap, SCR_CONTROL, 0x301); | |
62ba2841 TH |
1252 | /* Couldn't find anything in SATA I/II specs, but |
1253 | * AHCI-1.1 10.4.2 says at least 1 ms. */ | |
1254 | mdelay(1); | |
1da177e4 | 1255 | } |
cdcca89e | 1256 | scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */ |
1da177e4 LT |
1257 | |
1258 | /* wait for phy to become ready, if necessary */ | |
1259 | do { | |
1260 | msleep(200); | |
1261 | sstatus = scr_read(ap, SCR_STATUS); | |
1262 | if ((sstatus & 0xf) != 1) | |
1263 | break; | |
1264 | } while (time_before(jiffies, timeout)); | |
1265 | ||
3be680b7 TH |
1266 | /* print link status */ |
1267 | sata_print_link_status(ap); | |
656563e3 | 1268 | |
3be680b7 TH |
1269 | /* TODO: phy layer with polling, timeouts, etc. */ |
1270 | if (sata_dev_present(ap)) | |
1da177e4 | 1271 | ata_port_probe(ap); |
3be680b7 | 1272 | else |
1da177e4 | 1273 | ata_port_disable(ap); |
1da177e4 LT |
1274 | |
1275 | if (ap->flags & ATA_FLAG_PORT_DISABLED) | |
1276 | return; | |
1277 | ||
1278 | if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) { | |
1279 | ata_port_disable(ap); | |
1280 | return; | |
1281 | } | |
1282 | ||
1283 | ap->cbl = ATA_CBL_SATA; | |
1284 | } | |
1285 | ||
1286 | /** | |
780a87f7 JG |
1287 | * sata_phy_reset - Reset SATA bus. |
1288 | * @ap: SATA port associated with target SATA PHY. | |
1da177e4 | 1289 | * |
780a87f7 JG |
1290 | * This function resets the SATA bus, and then probes |
1291 | * the bus for devices. | |
1da177e4 LT |
1292 | * |
1293 | * LOCKING: | |
0cba632b | 1294 | * PCI/etc. bus probe sem. |
1da177e4 LT |
1295 | * |
1296 | */ | |
1297 | void sata_phy_reset(struct ata_port *ap) | |
1298 | { | |
1299 | __sata_phy_reset(ap); | |
1300 | if (ap->flags & ATA_FLAG_PORT_DISABLED) | |
1301 | return; | |
1302 | ata_bus_reset(ap); | |
1303 | } | |
1304 | ||
1305 | /** | |
780a87f7 JG |
1306 | * ata_port_disable - Disable port. |
1307 | * @ap: Port to be disabled. | |
1da177e4 | 1308 | * |
780a87f7 JG |
1309 | * Modify @ap data structure such that the system |
1310 | * thinks that the entire port is disabled, and should | |
1311 | * never attempt to probe or communicate with devices | |
1312 | * on this port. | |
1313 | * | |
1314 | * LOCKING: host_set lock, or some other form of | |
1315 | * serialization. | |
1da177e4 LT |
1316 | */ |
1317 | ||
1318 | void ata_port_disable(struct ata_port *ap) | |
1319 | { | |
1320 | ap->device[0].class = ATA_DEV_NONE; | |
1321 | ap->device[1].class = ATA_DEV_NONE; | |
1322 | ap->flags |= ATA_FLAG_PORT_DISABLED; | |
1323 | } | |
1324 | ||
452503f9 AC |
1325 | /* |
1326 | * This mode timing computation functionality is ported over from | |
1327 | * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik | |
1328 | */ | |
1329 | /* | |
1330 | * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds). | |
1331 | * These were taken from ATA/ATAPI-6 standard, rev 0a, except | |
1332 | * for PIO 5, which is a nonstandard extension and UDMA6, which | |
1333 | * is currently supported only by Maxtor drives. | |
1334 | */ | |
1335 | ||
1336 | static const struct ata_timing ata_timing[] = { | |
1337 | ||
1338 | { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 }, | |
1339 | { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 }, | |
1340 | { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 }, | |
1341 | { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 }, | |
1342 | ||
1343 | { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 }, | |
1344 | { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 }, | |
1345 | { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 }, | |
1346 | ||
1347 | /* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */ | |
1348 | ||
1349 | { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 }, | |
1350 | { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 }, | |
1351 | { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 }, | |
1352 | ||
1353 | { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 }, | |
1354 | { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 }, | |
1355 | { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 }, | |
1356 | ||
1357 | /* { XFER_PIO_5, 20, 50, 30, 100, 50, 30, 100, 0 }, */ | |
1358 | { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 }, | |
1359 | { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 }, | |
1360 | ||
1361 | { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 }, | |
1362 | { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 }, | |
1363 | { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 }, | |
1364 | ||
1365 | /* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */ | |
1366 | ||
1367 | { 0xFF } | |
1368 | }; | |
1369 | ||
1370 | #define ENOUGH(v,unit) (((v)-1)/(unit)+1) | |
1371 | #define EZ(v,unit) ((v)?ENOUGH(v,unit):0) | |
1372 | ||
1373 | static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT) | |
1374 | { | |
1375 | q->setup = EZ(t->setup * 1000, T); | |
1376 | q->act8b = EZ(t->act8b * 1000, T); | |
1377 | q->rec8b = EZ(t->rec8b * 1000, T); | |
1378 | q->cyc8b = EZ(t->cyc8b * 1000, T); | |
1379 | q->active = EZ(t->active * 1000, T); | |
1380 | q->recover = EZ(t->recover * 1000, T); | |
1381 | q->cycle = EZ(t->cycle * 1000, T); | |
1382 | q->udma = EZ(t->udma * 1000, UT); | |
1383 | } | |
1384 | ||
1385 | void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b, | |
1386 | struct ata_timing *m, unsigned int what) | |
1387 | { | |
1388 | if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup); | |
1389 | if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b); | |
1390 | if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b); | |
1391 | if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b); | |
1392 | if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active); | |
1393 | if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover); | |
1394 | if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle); | |
1395 | if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma); | |
1396 | } | |
1397 | ||
1398 | static const struct ata_timing* ata_timing_find_mode(unsigned short speed) | |
1399 | { | |
1400 | const struct ata_timing *t; | |
1401 | ||
1402 | for (t = ata_timing; t->mode != speed; t++) | |
91190758 | 1403 | if (t->mode == 0xFF) |
452503f9 AC |
1404 | return NULL; |
1405 | return t; | |
1406 | } | |
1407 | ||
1408 | int ata_timing_compute(struct ata_device *adev, unsigned short speed, | |
1409 | struct ata_timing *t, int T, int UT) | |
1410 | { | |
1411 | const struct ata_timing *s; | |
1412 | struct ata_timing p; | |
1413 | ||
1414 | /* | |
1415 | * Find the mode. | |
75b1f2f8 | 1416 | */ |
452503f9 AC |
1417 | |
1418 | if (!(s = ata_timing_find_mode(speed))) | |
1419 | return -EINVAL; | |
1420 | ||
75b1f2f8 AL |
1421 | memcpy(t, s, sizeof(*s)); |
1422 | ||
452503f9 AC |
1423 | /* |
1424 | * If the drive is an EIDE drive, it can tell us it needs extended | |
1425 | * PIO/MW_DMA cycle timing. | |
1426 | */ | |
1427 | ||
1428 | if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */ | |
1429 | memset(&p, 0, sizeof(p)); | |
1430 | if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) { | |
1431 | if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO]; | |
1432 | else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY]; | |
1433 | } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) { | |
1434 | p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN]; | |
1435 | } | |
1436 | ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B); | |
1437 | } | |
1438 | ||
1439 | /* | |
1440 | * Convert the timing to bus clock counts. | |
1441 | */ | |
1442 | ||
75b1f2f8 | 1443 | ata_timing_quantize(t, t, T, UT); |
452503f9 AC |
1444 | |
1445 | /* | |
c893a3ae RD |
1446 | * Even in DMA/UDMA modes we still use PIO access for IDENTIFY, |
1447 | * S.M.A.R.T * and some other commands. We have to ensure that the | |
1448 | * DMA cycle timing is slower/equal than the fastest PIO timing. | |
452503f9 AC |
1449 | */ |
1450 | ||
1451 | if (speed > XFER_PIO_4) { | |
1452 | ata_timing_compute(adev, adev->pio_mode, &p, T, UT); | |
1453 | ata_timing_merge(&p, t, t, ATA_TIMING_ALL); | |
1454 | } | |
1455 | ||
1456 | /* | |
c893a3ae | 1457 | * Lengthen active & recovery time so that cycle time is correct. |
452503f9 AC |
1458 | */ |
1459 | ||
1460 | if (t->act8b + t->rec8b < t->cyc8b) { | |
1461 | t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2; | |
1462 | t->rec8b = t->cyc8b - t->act8b; | |
1463 | } | |
1464 | ||
1465 | if (t->active + t->recover < t->cycle) { | |
1466 | t->active += (t->cycle - (t->active + t->recover)) / 2; | |
1467 | t->recover = t->cycle - t->active; | |
1468 | } | |
1469 | ||
1470 | return 0; | |
1471 | } | |
1472 | ||
057ace5e | 1473 | static const struct { |
1da177e4 LT |
1474 | unsigned int shift; |
1475 | u8 base; | |
1476 | } xfer_mode_classes[] = { | |
1477 | { ATA_SHIFT_UDMA, XFER_UDMA_0 }, | |
1478 | { ATA_SHIFT_MWDMA, XFER_MW_DMA_0 }, | |
1479 | { ATA_SHIFT_PIO, XFER_PIO_0 }, | |
1480 | }; | |
1481 | ||
858119e1 | 1482 | static u8 base_from_shift(unsigned int shift) |
1da177e4 LT |
1483 | { |
1484 | int i; | |
1485 | ||
1486 | for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++) | |
1487 | if (xfer_mode_classes[i].shift == shift) | |
1488 | return xfer_mode_classes[i].base; | |
1489 | ||
1490 | return 0xff; | |
1491 | } | |
1492 | ||
1493 | static void ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev) | |
1494 | { | |
1495 | int ofs, idx; | |
1496 | u8 base; | |
1497 | ||
1498 | if (!ata_dev_present(dev) || (ap->flags & ATA_FLAG_PORT_DISABLED)) | |
1499 | return; | |
1500 | ||
1501 | if (dev->xfer_shift == ATA_SHIFT_PIO) | |
1502 | dev->flags |= ATA_DFLAG_PIO; | |
1503 | ||
1504 | ata_dev_set_xfermode(ap, dev); | |
1505 | ||
1506 | base = base_from_shift(dev->xfer_shift); | |
1507 | ofs = dev->xfer_mode - base; | |
1508 | idx = ofs + dev->xfer_shift; | |
1509 | WARN_ON(idx >= ARRAY_SIZE(xfer_mode_str)); | |
1510 | ||
1511 | DPRINTK("idx=%d xfer_shift=%u, xfer_mode=0x%x, base=0x%x, offset=%d\n", | |
1512 | idx, dev->xfer_shift, (int)dev->xfer_mode, (int)base, ofs); | |
1513 | ||
1514 | printk(KERN_INFO "ata%u: dev %u configured for %s\n", | |
1515 | ap->id, dev->devno, xfer_mode_str[idx]); | |
1516 | } | |
1517 | ||
1518 | static int ata_host_set_pio(struct ata_port *ap) | |
1519 | { | |
1520 | unsigned int mask; | |
1521 | int x, i; | |
1522 | u8 base, xfer_mode; | |
1523 | ||
1524 | mask = ata_get_mode_mask(ap, ATA_SHIFT_PIO); | |
1525 | x = fgb(mask); | |
1526 | if (x < 0) { | |
1527 | printk(KERN_WARNING "ata%u: no PIO support\n", ap->id); | |
1528 | return -1; | |
1529 | } | |
1530 | ||
1531 | base = base_from_shift(ATA_SHIFT_PIO); | |
1532 | xfer_mode = base + x; | |
1533 | ||
1534 | DPRINTK("base 0x%x xfer_mode 0x%x mask 0x%x x %d\n", | |
1535 | (int)base, (int)xfer_mode, mask, x); | |
1536 | ||
1537 | for (i = 0; i < ATA_MAX_DEVICES; i++) { | |
1538 | struct ata_device *dev = &ap->device[i]; | |
1539 | if (ata_dev_present(dev)) { | |
1540 | dev->pio_mode = xfer_mode; | |
1541 | dev->xfer_mode = xfer_mode; | |
1542 | dev->xfer_shift = ATA_SHIFT_PIO; | |
1543 | if (ap->ops->set_piomode) | |
1544 | ap->ops->set_piomode(ap, dev); | |
1545 | } | |
1546 | } | |
1547 | ||
1548 | return 0; | |
1549 | } | |
1550 | ||
1551 | static void ata_host_set_dma(struct ata_port *ap, u8 xfer_mode, | |
1552 | unsigned int xfer_shift) | |
1553 | { | |
1554 | int i; | |
1555 | ||
1556 | for (i = 0; i < ATA_MAX_DEVICES; i++) { | |
1557 | struct ata_device *dev = &ap->device[i]; | |
1558 | if (ata_dev_present(dev)) { | |
1559 | dev->dma_mode = xfer_mode; | |
1560 | dev->xfer_mode = xfer_mode; | |
1561 | dev->xfer_shift = xfer_shift; | |
1562 | if (ap->ops->set_dmamode) | |
1563 | ap->ops->set_dmamode(ap, dev); | |
1564 | } | |
1565 | } | |
1566 | } | |
1567 | ||
1568 | /** | |
1569 | * ata_set_mode - Program timings and issue SET FEATURES - XFER | |
1570 | * @ap: port on which timings will be programmed | |
1571 | * | |
780a87f7 JG |
1572 | * Set ATA device disk transfer mode (PIO3, UDMA6, etc.). |
1573 | * | |
1da177e4 | 1574 | * LOCKING: |
0cba632b | 1575 | * PCI/etc. bus probe sem. |
1da177e4 LT |
1576 | */ |
1577 | static void ata_set_mode(struct ata_port *ap) | |
1578 | { | |
8cbd6df1 | 1579 | unsigned int xfer_shift; |
1da177e4 LT |
1580 | u8 xfer_mode; |
1581 | int rc; | |
1582 | ||
1583 | /* step 1: always set host PIO timings */ | |
1584 | rc = ata_host_set_pio(ap); | |
1585 | if (rc) | |
1586 | goto err_out; | |
1587 | ||
1588 | /* step 2: choose the best data xfer mode */ | |
1589 | xfer_mode = xfer_shift = 0; | |
1590 | rc = ata_choose_xfer_mode(ap, &xfer_mode, &xfer_shift); | |
1591 | if (rc) | |
1592 | goto err_out; | |
1593 | ||
1594 | /* step 3: if that xfer mode isn't PIO, set host DMA timings */ | |
1595 | if (xfer_shift != ATA_SHIFT_PIO) | |
1596 | ata_host_set_dma(ap, xfer_mode, xfer_shift); | |
1597 | ||
1598 | /* step 4: update devices' xfer mode */ | |
1599 | ata_dev_set_mode(ap, &ap->device[0]); | |
1600 | ata_dev_set_mode(ap, &ap->device[1]); | |
1601 | ||
1602 | if (ap->flags & ATA_FLAG_PORT_DISABLED) | |
1603 | return; | |
1604 | ||
1605 | if (ap->ops->post_set_mode) | |
1606 | ap->ops->post_set_mode(ap); | |
1607 | ||
1da177e4 LT |
1608 | return; |
1609 | ||
1610 | err_out: | |
1611 | ata_port_disable(ap); | |
1612 | } | |
1613 | ||
1fdffbce JG |
1614 | /** |
1615 | * ata_tf_to_host - issue ATA taskfile to host controller | |
1616 | * @ap: port to which command is being issued | |
1617 | * @tf: ATA taskfile register set | |
1618 | * | |
1619 | * Issues ATA taskfile register set to ATA host controller, | |
1620 | * with proper synchronization with interrupt handler and | |
1621 | * other threads. | |
1622 | * | |
1623 | * LOCKING: | |
1624 | * spin_lock_irqsave(host_set lock) | |
1625 | */ | |
1626 | ||
1627 | static inline void ata_tf_to_host(struct ata_port *ap, | |
1628 | const struct ata_taskfile *tf) | |
1629 | { | |
1630 | ap->ops->tf_load(ap, tf); | |
1631 | ap->ops->exec_command(ap, tf); | |
1632 | } | |
1633 | ||
1da177e4 LT |
1634 | /** |
1635 | * ata_busy_sleep - sleep until BSY clears, or timeout | |
1636 | * @ap: port containing status register to be polled | |
1637 | * @tmout_pat: impatience timeout | |
1638 | * @tmout: overall timeout | |
1639 | * | |
780a87f7 JG |
1640 | * Sleep until ATA Status register bit BSY clears, |
1641 | * or a timeout occurs. | |
1642 | * | |
1643 | * LOCKING: None. | |
1da177e4 LT |
1644 | */ |
1645 | ||
6f8b9958 TH |
1646 | unsigned int ata_busy_sleep (struct ata_port *ap, |
1647 | unsigned long tmout_pat, unsigned long tmout) | |
1da177e4 LT |
1648 | { |
1649 | unsigned long timer_start, timeout; | |
1650 | u8 status; | |
1651 | ||
1652 | status = ata_busy_wait(ap, ATA_BUSY, 300); | |
1653 | timer_start = jiffies; | |
1654 | timeout = timer_start + tmout_pat; | |
1655 | while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) { | |
1656 | msleep(50); | |
1657 | status = ata_busy_wait(ap, ATA_BUSY, 3); | |
1658 | } | |
1659 | ||
1660 | if (status & ATA_BUSY) | |
1661 | printk(KERN_WARNING "ata%u is slow to respond, " | |
1662 | "please be patient\n", ap->id); | |
1663 | ||
1664 | timeout = timer_start + tmout; | |
1665 | while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) { | |
1666 | msleep(50); | |
1667 | status = ata_chk_status(ap); | |
1668 | } | |
1669 | ||
1670 | if (status & ATA_BUSY) { | |
1671 | printk(KERN_ERR "ata%u failed to respond (%lu secs)\n", | |
1672 | ap->id, tmout / HZ); | |
1673 | return 1; | |
1674 | } | |
1675 | ||
1676 | return 0; | |
1677 | } | |
1678 | ||
1679 | static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask) | |
1680 | { | |
1681 | struct ata_ioports *ioaddr = &ap->ioaddr; | |
1682 | unsigned int dev0 = devmask & (1 << 0); | |
1683 | unsigned int dev1 = devmask & (1 << 1); | |
1684 | unsigned long timeout; | |
1685 | ||
1686 | /* if device 0 was found in ata_devchk, wait for its | |
1687 | * BSY bit to clear | |
1688 | */ | |
1689 | if (dev0) | |
1690 | ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT); | |
1691 | ||
1692 | /* if device 1 was found in ata_devchk, wait for | |
1693 | * register access, then wait for BSY to clear | |
1694 | */ | |
1695 | timeout = jiffies + ATA_TMOUT_BOOT; | |
1696 | while (dev1) { | |
1697 | u8 nsect, lbal; | |
1698 | ||
1699 | ap->ops->dev_select(ap, 1); | |
1700 | if (ap->flags & ATA_FLAG_MMIO) { | |
1701 | nsect = readb((void __iomem *) ioaddr->nsect_addr); | |
1702 | lbal = readb((void __iomem *) ioaddr->lbal_addr); | |
1703 | } else { | |
1704 | nsect = inb(ioaddr->nsect_addr); | |
1705 | lbal = inb(ioaddr->lbal_addr); | |
1706 | } | |
1707 | if ((nsect == 1) && (lbal == 1)) | |
1708 | break; | |
1709 | if (time_after(jiffies, timeout)) { | |
1710 | dev1 = 0; | |
1711 | break; | |
1712 | } | |
1713 | msleep(50); /* give drive a breather */ | |
1714 | } | |
1715 | if (dev1) | |
1716 | ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT); | |
1717 | ||
1718 | /* is all this really necessary? */ | |
1719 | ap->ops->dev_select(ap, 0); | |
1720 | if (dev1) | |
1721 | ap->ops->dev_select(ap, 1); | |
1722 | if (dev0) | |
1723 | ap->ops->dev_select(ap, 0); | |
1724 | } | |
1725 | ||
1726 | /** | |
0cba632b JG |
1727 | * ata_bus_edd - Issue EXECUTE DEVICE DIAGNOSTIC command. |
1728 | * @ap: Port to reset and probe | |
1729 | * | |
1730 | * Use the EXECUTE DEVICE DIAGNOSTIC command to reset and | |
1731 | * probe the bus. Not often used these days. | |
1da177e4 LT |
1732 | * |
1733 | * LOCKING: | |
0cba632b | 1734 | * PCI/etc. bus probe sem. |
e5338254 | 1735 | * Obtains host_set lock. |
1da177e4 LT |
1736 | * |
1737 | */ | |
1738 | ||
1739 | static unsigned int ata_bus_edd(struct ata_port *ap) | |
1740 | { | |
1741 | struct ata_taskfile tf; | |
e5338254 | 1742 | unsigned long flags; |
1da177e4 LT |
1743 | |
1744 | /* set up execute-device-diag (bus reset) taskfile */ | |
1745 | /* also, take interrupts to a known state (disabled) */ | |
1746 | DPRINTK("execute-device-diag\n"); | |
1747 | ata_tf_init(ap, &tf, 0); | |
1748 | tf.ctl |= ATA_NIEN; | |
1749 | tf.command = ATA_CMD_EDD; | |
1750 | tf.protocol = ATA_PROT_NODATA; | |
1751 | ||
1752 | /* do bus reset */ | |
e5338254 | 1753 | spin_lock_irqsave(&ap->host_set->lock, flags); |
1da177e4 | 1754 | ata_tf_to_host(ap, &tf); |
e5338254 | 1755 | spin_unlock_irqrestore(&ap->host_set->lock, flags); |
1da177e4 LT |
1756 | |
1757 | /* spec says at least 2ms. but who knows with those | |
1758 | * crazy ATAPI devices... | |
1759 | */ | |
1760 | msleep(150); | |
1761 | ||
1762 | return ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT); | |
1763 | } | |
1764 | ||
1765 | static unsigned int ata_bus_softreset(struct ata_port *ap, | |
1766 | unsigned int devmask) | |
1767 | { | |
1768 | struct ata_ioports *ioaddr = &ap->ioaddr; | |
1769 | ||
1770 | DPRINTK("ata%u: bus reset via SRST\n", ap->id); | |
1771 | ||
1772 | /* software reset. causes dev0 to be selected */ | |
1773 | if (ap->flags & ATA_FLAG_MMIO) { | |
1774 | writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr); | |
1775 | udelay(20); /* FIXME: flush */ | |
1776 | writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr); | |
1777 | udelay(20); /* FIXME: flush */ | |
1778 | writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr); | |
1779 | } else { | |
1780 | outb(ap->ctl, ioaddr->ctl_addr); | |
1781 | udelay(10); | |
1782 | outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr); | |
1783 | udelay(10); | |
1784 | outb(ap->ctl, ioaddr->ctl_addr); | |
1785 | } | |
1786 | ||
1787 | /* spec mandates ">= 2ms" before checking status. | |
1788 | * We wait 150ms, because that was the magic delay used for | |
1789 | * ATAPI devices in Hale Landis's ATADRVR, for the period of time | |
1790 | * between when the ATA command register is written, and then | |
1791 | * status is checked. Because waiting for "a while" before | |
1792 | * checking status is fine, post SRST, we perform this magic | |
1793 | * delay here as well. | |
1794 | */ | |
1795 | msleep(150); | |
1796 | ||
1797 | ata_bus_post_reset(ap, devmask); | |
1798 | ||
1799 | return 0; | |
1800 | } | |
1801 | ||
1802 | /** | |
1803 | * ata_bus_reset - reset host port and associated ATA channel | |
1804 | * @ap: port to reset | |
1805 | * | |
1806 | * This is typically the first time we actually start issuing | |
1807 | * commands to the ATA channel. We wait for BSY to clear, then | |
1808 | * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its | |
1809 | * result. Determine what devices, if any, are on the channel | |
1810 | * by looking at the device 0/1 error register. Look at the signature | |
1811 | * stored in each device's taskfile registers, to determine if | |
1812 | * the device is ATA or ATAPI. | |
1813 | * | |
1814 | * LOCKING: | |
0cba632b JG |
1815 | * PCI/etc. bus probe sem. |
1816 | * Obtains host_set lock. | |
1da177e4 LT |
1817 | * |
1818 | * SIDE EFFECTS: | |
1819 | * Sets ATA_FLAG_PORT_DISABLED if bus reset fails. | |
1820 | */ | |
1821 | ||
1822 | void ata_bus_reset(struct ata_port *ap) | |
1823 | { | |
1824 | struct ata_ioports *ioaddr = &ap->ioaddr; | |
1825 | unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS; | |
1826 | u8 err; | |
1827 | unsigned int dev0, dev1 = 0, rc = 0, devmask = 0; | |
1828 | ||
1829 | DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no); | |
1830 | ||
1831 | /* determine if device 0/1 are present */ | |
1832 | if (ap->flags & ATA_FLAG_SATA_RESET) | |
1833 | dev0 = 1; | |
1834 | else { | |
1835 | dev0 = ata_devchk(ap, 0); | |
1836 | if (slave_possible) | |
1837 | dev1 = ata_devchk(ap, 1); | |
1838 | } | |
1839 | ||
1840 | if (dev0) | |
1841 | devmask |= (1 << 0); | |
1842 | if (dev1) | |
1843 | devmask |= (1 << 1); | |
1844 | ||
1845 | /* select device 0 again */ | |
1846 | ap->ops->dev_select(ap, 0); | |
1847 | ||
1848 | /* issue bus reset */ | |
1849 | if (ap->flags & ATA_FLAG_SRST) | |
1850 | rc = ata_bus_softreset(ap, devmask); | |
1851 | else if ((ap->flags & ATA_FLAG_SATA_RESET) == 0) { | |
1852 | /* set up device control */ | |
1853 | if (ap->flags & ATA_FLAG_MMIO) | |
1854 | writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr); | |
1855 | else | |
1856 | outb(ap->ctl, ioaddr->ctl_addr); | |
1857 | rc = ata_bus_edd(ap); | |
1858 | } | |
1859 | ||
1860 | if (rc) | |
1861 | goto err_out; | |
1862 | ||
1863 | /* | |
1864 | * determine by signature whether we have ATA or ATAPI devices | |
1865 | */ | |
b4dc7623 | 1866 | ap->device[0].class = ata_dev_try_classify(ap, 0, &err); |
1da177e4 | 1867 | if ((slave_possible) && (err != 0x81)) |
b4dc7623 | 1868 | ap->device[1].class = ata_dev_try_classify(ap, 1, &err); |
1da177e4 LT |
1869 | |
1870 | /* re-enable interrupts */ | |
1871 | if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */ | |
1872 | ata_irq_on(ap); | |
1873 | ||
1874 | /* is double-select really necessary? */ | |
1875 | if (ap->device[1].class != ATA_DEV_NONE) | |
1876 | ap->ops->dev_select(ap, 1); | |
1877 | if (ap->device[0].class != ATA_DEV_NONE) | |
1878 | ap->ops->dev_select(ap, 0); | |
1879 | ||
1880 | /* if no devices were detected, disable this port */ | |
1881 | if ((ap->device[0].class == ATA_DEV_NONE) && | |
1882 | (ap->device[1].class == ATA_DEV_NONE)) | |
1883 | goto err_out; | |
1884 | ||
1885 | if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) { | |
1886 | /* set up device control for ATA_FLAG_SATA_RESET */ | |
1887 | if (ap->flags & ATA_FLAG_MMIO) | |
1888 | writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr); | |
1889 | else | |
1890 | outb(ap->ctl, ioaddr->ctl_addr); | |
1891 | } | |
1892 | ||
1893 | DPRINTK("EXIT\n"); | |
1894 | return; | |
1895 | ||
1896 | err_out: | |
1897 | printk(KERN_ERR "ata%u: disabling port\n", ap->id); | |
1898 | ap->ops->port_disable(ap); | |
1899 | ||
1900 | DPRINTK("EXIT\n"); | |
1901 | } | |
1902 | ||
7a7921e8 TH |
1903 | static int sata_phy_resume(struct ata_port *ap) |
1904 | { | |
1905 | unsigned long timeout = jiffies + (HZ * 5); | |
1906 | u32 sstatus; | |
1907 | ||
1908 | scr_write_flush(ap, SCR_CONTROL, 0x300); | |
1909 | ||
1910 | /* Wait for phy to become ready, if necessary. */ | |
1911 | do { | |
1912 | msleep(200); | |
1913 | sstatus = scr_read(ap, SCR_STATUS); | |
1914 | if ((sstatus & 0xf) != 1) | |
1915 | return 0; | |
1916 | } while (time_before(jiffies, timeout)); | |
1917 | ||
1918 | return -1; | |
1919 | } | |
1920 | ||
8a19ac89 TH |
1921 | /** |
1922 | * ata_std_probeinit - initialize probing | |
1923 | * @ap: port to be probed | |
1924 | * | |
1925 | * @ap is about to be probed. Initialize it. This function is | |
1926 | * to be used as standard callback for ata_drive_probe_reset(). | |
3a39746a TH |
1927 | * |
1928 | * NOTE!!! Do not use this function as probeinit if a low level | |
1929 | * driver implements only hardreset. Just pass NULL as probeinit | |
1930 | * in that case. Using this function is probably okay but doing | |
1931 | * so makes reset sequence different from the original | |
1932 | * ->phy_reset implementation and Jeff nervous. :-P | |
8a19ac89 TH |
1933 | */ |
1934 | extern void ata_std_probeinit(struct ata_port *ap) | |
1935 | { | |
3a39746a | 1936 | if (ap->flags & ATA_FLAG_SATA && ap->ops->scr_read) { |
8a19ac89 | 1937 | sata_phy_resume(ap); |
3a39746a TH |
1938 | if (sata_dev_present(ap)) |
1939 | ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT); | |
1940 | } | |
8a19ac89 TH |
1941 | } |
1942 | ||
c2bd5804 TH |
1943 | /** |
1944 | * ata_std_softreset - reset host port via ATA SRST | |
1945 | * @ap: port to reset | |
1946 | * @verbose: fail verbosely | |
1947 | * @classes: resulting classes of attached devices | |
1948 | * | |
1949 | * Reset host port using ATA SRST. This function is to be used | |
1950 | * as standard callback for ata_drive_*_reset() functions. | |
1951 | * | |
1952 | * LOCKING: | |
1953 | * Kernel thread context (may sleep) | |
1954 | * | |
1955 | * RETURNS: | |
1956 | * 0 on success, -errno otherwise. | |
1957 | */ | |
1958 | int ata_std_softreset(struct ata_port *ap, int verbose, unsigned int *classes) | |
1959 | { | |
1960 | unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS; | |
1961 | unsigned int devmask = 0, err_mask; | |
1962 | u8 err; | |
1963 | ||
1964 | DPRINTK("ENTER\n"); | |
1965 | ||
3a39746a TH |
1966 | if (ap->ops->scr_read && !sata_dev_present(ap)) { |
1967 | classes[0] = ATA_DEV_NONE; | |
1968 | goto out; | |
1969 | } | |
1970 | ||
c2bd5804 TH |
1971 | /* determine if device 0/1 are present */ |
1972 | if (ata_devchk(ap, 0)) | |
1973 | devmask |= (1 << 0); | |
1974 | if (slave_possible && ata_devchk(ap, 1)) | |
1975 | devmask |= (1 << 1); | |
1976 | ||
c2bd5804 TH |
1977 | /* select device 0 again */ |
1978 | ap->ops->dev_select(ap, 0); | |
1979 | ||
1980 | /* issue bus reset */ | |
1981 | DPRINTK("about to softreset, devmask=%x\n", devmask); | |
1982 | err_mask = ata_bus_softreset(ap, devmask); | |
1983 | if (err_mask) { | |
1984 | if (verbose) | |
1985 | printk(KERN_ERR "ata%u: SRST failed (err_mask=0x%x)\n", | |
1986 | ap->id, err_mask); | |
1987 | else | |
1988 | DPRINTK("EXIT, softreset failed (err_mask=0x%x)\n", | |
1989 | err_mask); | |
1990 | return -EIO; | |
1991 | } | |
1992 | ||
1993 | /* determine by signature whether we have ATA or ATAPI devices */ | |
1994 | classes[0] = ata_dev_try_classify(ap, 0, &err); | |
1995 | if (slave_possible && err != 0x81) | |
1996 | classes[1] = ata_dev_try_classify(ap, 1, &err); | |
1997 | ||
3a39746a | 1998 | out: |
c2bd5804 TH |
1999 | DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]); |
2000 | return 0; | |
2001 | } | |
2002 | ||
2003 | /** | |
2004 | * sata_std_hardreset - reset host port via SATA phy reset | |
2005 | * @ap: port to reset | |
2006 | * @verbose: fail verbosely | |
2007 | * @class: resulting class of attached device | |
2008 | * | |
2009 | * SATA phy-reset host port using DET bits of SControl register. | |
2010 | * This function is to be used as standard callback for | |
2011 | * ata_drive_*_reset(). | |
2012 | * | |
2013 | * LOCKING: | |
2014 | * Kernel thread context (may sleep) | |
2015 | * | |
2016 | * RETURNS: | |
2017 | * 0 on success, -errno otherwise. | |
2018 | */ | |
2019 | int sata_std_hardreset(struct ata_port *ap, int verbose, unsigned int *class) | |
2020 | { | |
c2bd5804 TH |
2021 | DPRINTK("ENTER\n"); |
2022 | ||
2023 | /* Issue phy wake/reset */ | |
2024 | scr_write_flush(ap, SCR_CONTROL, 0x301); | |
2025 | ||
2026 | /* | |
2027 | * Couldn't find anything in SATA I/II specs, but AHCI-1.1 | |
2028 | * 10.4.2 says at least 1 ms. | |
2029 | */ | |
2030 | msleep(1); | |
2031 | ||
7a7921e8 TH |
2032 | /* Bring phy back */ |
2033 | sata_phy_resume(ap); | |
c2bd5804 | 2034 | |
c2bd5804 TH |
2035 | /* TODO: phy layer with polling, timeouts, etc. */ |
2036 | if (!sata_dev_present(ap)) { | |
2037 | *class = ATA_DEV_NONE; | |
2038 | DPRINTK("EXIT, link offline\n"); | |
2039 | return 0; | |
2040 | } | |
2041 | ||
2042 | if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) { | |
2043 | if (verbose) | |
2044 | printk(KERN_ERR "ata%u: COMRESET failed " | |
2045 | "(device not ready)\n", ap->id); | |
2046 | else | |
2047 | DPRINTK("EXIT, device not ready\n"); | |
2048 | return -EIO; | |
2049 | } | |
2050 | ||
3a39746a TH |
2051 | ap->ops->dev_select(ap, 0); /* probably unnecessary */ |
2052 | ||
c2bd5804 TH |
2053 | *class = ata_dev_try_classify(ap, 0, NULL); |
2054 | ||
2055 | DPRINTK("EXIT, class=%u\n", *class); | |
2056 | return 0; | |
2057 | } | |
2058 | ||
2059 | /** | |
2060 | * ata_std_postreset - standard postreset callback | |
2061 | * @ap: the target ata_port | |
2062 | * @classes: classes of attached devices | |
2063 | * | |
2064 | * This function is invoked after a successful reset. Note that | |
2065 | * the device might have been reset more than once using | |
2066 | * different reset methods before postreset is invoked. | |
2067 | * postreset is also reponsible for setting cable type. | |
2068 | * | |
2069 | * This function is to be used as standard callback for | |
2070 | * ata_drive_*_reset(). | |
2071 | * | |
2072 | * LOCKING: | |
2073 | * Kernel thread context (may sleep) | |
2074 | */ | |
2075 | void ata_std_postreset(struct ata_port *ap, unsigned int *classes) | |
2076 | { | |
2077 | DPRINTK("ENTER\n"); | |
2078 | ||
2079 | /* set cable type */ | |
2080 | if (ap->cbl == ATA_CBL_NONE && ap->flags & ATA_FLAG_SATA) | |
2081 | ap->cbl = ATA_CBL_SATA; | |
2082 | ||
2083 | /* print link status */ | |
2084 | if (ap->cbl == ATA_CBL_SATA) | |
2085 | sata_print_link_status(ap); | |
2086 | ||
3a39746a TH |
2087 | /* re-enable interrupts */ |
2088 | if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */ | |
2089 | ata_irq_on(ap); | |
c2bd5804 TH |
2090 | |
2091 | /* is double-select really necessary? */ | |
2092 | if (classes[0] != ATA_DEV_NONE) | |
2093 | ap->ops->dev_select(ap, 1); | |
2094 | if (classes[1] != ATA_DEV_NONE) | |
2095 | ap->ops->dev_select(ap, 0); | |
2096 | ||
3a39746a TH |
2097 | /* bail out if no device is present */ |
2098 | if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) { | |
2099 | DPRINTK("EXIT, no device\n"); | |
2100 | return; | |
2101 | } | |
2102 | ||
2103 | /* set up device control */ | |
2104 | if (ap->ioaddr.ctl_addr) { | |
2105 | if (ap->flags & ATA_FLAG_MMIO) | |
2106 | writeb(ap->ctl, (void __iomem *) ap->ioaddr.ctl_addr); | |
2107 | else | |
2108 | outb(ap->ctl, ap->ioaddr.ctl_addr); | |
2109 | } | |
c2bd5804 TH |
2110 | |
2111 | DPRINTK("EXIT\n"); | |
2112 | } | |
2113 | ||
2114 | /** | |
2115 | * ata_std_probe_reset - standard probe reset method | |
2116 | * @ap: prot to perform probe-reset | |
2117 | * @classes: resulting classes of attached devices | |
2118 | * | |
2119 | * The stock off-the-shelf ->probe_reset method. | |
2120 | * | |
2121 | * LOCKING: | |
2122 | * Kernel thread context (may sleep) | |
2123 | * | |
2124 | * RETURNS: | |
2125 | * 0 on success, -errno otherwise. | |
2126 | */ | |
2127 | int ata_std_probe_reset(struct ata_port *ap, unsigned int *classes) | |
2128 | { | |
2129 | ata_reset_fn_t hardreset; | |
2130 | ||
2131 | hardreset = NULL; | |
b911fc3a | 2132 | if (ap->flags & ATA_FLAG_SATA && ap->ops->scr_read) |
c2bd5804 TH |
2133 | hardreset = sata_std_hardreset; |
2134 | ||
8a19ac89 | 2135 | return ata_drive_probe_reset(ap, ata_std_probeinit, |
7944ea95 | 2136 | ata_std_softreset, hardreset, |
c2bd5804 TH |
2137 | ata_std_postreset, classes); |
2138 | } | |
2139 | ||
a62c0fc5 TH |
2140 | static int do_probe_reset(struct ata_port *ap, ata_reset_fn_t reset, |
2141 | ata_postreset_fn_t postreset, | |
2142 | unsigned int *classes) | |
2143 | { | |
2144 | int i, rc; | |
2145 | ||
2146 | for (i = 0; i < ATA_MAX_DEVICES; i++) | |
2147 | classes[i] = ATA_DEV_UNKNOWN; | |
2148 | ||
2149 | rc = reset(ap, 0, classes); | |
2150 | if (rc) | |
2151 | return rc; | |
2152 | ||
2153 | /* If any class isn't ATA_DEV_UNKNOWN, consider classification | |
2154 | * is complete and convert all ATA_DEV_UNKNOWN to | |
2155 | * ATA_DEV_NONE. | |
2156 | */ | |
2157 | for (i = 0; i < ATA_MAX_DEVICES; i++) | |
2158 | if (classes[i] != ATA_DEV_UNKNOWN) | |
2159 | break; | |
2160 | ||
2161 | if (i < ATA_MAX_DEVICES) | |
2162 | for (i = 0; i < ATA_MAX_DEVICES; i++) | |
2163 | if (classes[i] == ATA_DEV_UNKNOWN) | |
2164 | classes[i] = ATA_DEV_NONE; | |
2165 | ||
2166 | if (postreset) | |
2167 | postreset(ap, classes); | |
2168 | ||
2169 | return classes[0] != ATA_DEV_UNKNOWN ? 0 : -ENODEV; | |
2170 | } | |
2171 | ||
2172 | /** | |
2173 | * ata_drive_probe_reset - Perform probe reset with given methods | |
2174 | * @ap: port to reset | |
7944ea95 | 2175 | * @probeinit: probeinit method (can be NULL) |
a62c0fc5 TH |
2176 | * @softreset: softreset method (can be NULL) |
2177 | * @hardreset: hardreset method (can be NULL) | |
2178 | * @postreset: postreset method (can be NULL) | |
2179 | * @classes: resulting classes of attached devices | |
2180 | * | |
2181 | * Reset the specified port and classify attached devices using | |
2182 | * given methods. This function prefers softreset but tries all | |
2183 | * possible reset sequences to reset and classify devices. This | |
2184 | * function is intended to be used for constructing ->probe_reset | |
2185 | * callback by low level drivers. | |
2186 | * | |
2187 | * Reset methods should follow the following rules. | |
2188 | * | |
2189 | * - Return 0 on sucess, -errno on failure. | |
2190 | * - If classification is supported, fill classes[] with | |
2191 | * recognized class codes. | |
2192 | * - If classification is not supported, leave classes[] alone. | |
2193 | * - If verbose is non-zero, print error message on failure; | |
2194 | * otherwise, shut up. | |
2195 | * | |
2196 | * LOCKING: | |
2197 | * Kernel thread context (may sleep) | |
2198 | * | |
2199 | * RETURNS: | |
2200 | * 0 on success, -EINVAL if no reset method is avaliable, -ENODEV | |
2201 | * if classification fails, and any error code from reset | |
2202 | * methods. | |
2203 | */ | |
7944ea95 | 2204 | int ata_drive_probe_reset(struct ata_port *ap, ata_probeinit_fn_t probeinit, |
a62c0fc5 TH |
2205 | ata_reset_fn_t softreset, ata_reset_fn_t hardreset, |
2206 | ata_postreset_fn_t postreset, unsigned int *classes) | |
2207 | { | |
2208 | int rc = -EINVAL; | |
2209 | ||
7944ea95 TH |
2210 | if (probeinit) |
2211 | probeinit(ap); | |
2212 | ||
a62c0fc5 TH |
2213 | if (softreset) { |
2214 | rc = do_probe_reset(ap, softreset, postreset, classes); | |
2215 | if (rc == 0) | |
2216 | return 0; | |
2217 | } | |
2218 | ||
2219 | if (!hardreset) | |
2220 | return rc; | |
2221 | ||
2222 | rc = do_probe_reset(ap, hardreset, postreset, classes); | |
2223 | if (rc == 0 || rc != -ENODEV) | |
2224 | return rc; | |
2225 | ||
2226 | if (softreset) | |
2227 | rc = do_probe_reset(ap, softreset, postreset, classes); | |
2228 | ||
2229 | return rc; | |
2230 | } | |
2231 | ||
057ace5e JG |
2232 | static void ata_pr_blacklisted(const struct ata_port *ap, |
2233 | const struct ata_device *dev) | |
1da177e4 LT |
2234 | { |
2235 | printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, disabling DMA\n", | |
2236 | ap->id, dev->devno); | |
2237 | } | |
2238 | ||
98ac62de | 2239 | static const char * const ata_dma_blacklist [] = { |
1da177e4 LT |
2240 | "WDC AC11000H", |
2241 | "WDC AC22100H", | |
2242 | "WDC AC32500H", | |
2243 | "WDC AC33100H", | |
2244 | "WDC AC31600H", | |
2245 | "WDC AC32100H", | |
2246 | "WDC AC23200L", | |
2247 | "Compaq CRD-8241B", | |
2248 | "CRD-8400B", | |
2249 | "CRD-8480B", | |
2250 | "CRD-8482B", | |
2251 | "CRD-84", | |
2252 | "SanDisk SDP3B", | |
2253 | "SanDisk SDP3B-64", | |
2254 | "SANYO CD-ROM CRD", | |
2255 | "HITACHI CDR-8", | |
2256 | "HITACHI CDR-8335", | |
2257 | "HITACHI CDR-8435", | |
2258 | "Toshiba CD-ROM XM-6202B", | |
e922256a | 2259 | "TOSHIBA CD-ROM XM-1702BC", |
1da177e4 LT |
2260 | "CD-532E-A", |
2261 | "E-IDE CD-ROM CR-840", | |
2262 | "CD-ROM Drive/F5A", | |
2263 | "WPI CDD-820", | |
2264 | "SAMSUNG CD-ROM SC-148C", | |
2265 | "SAMSUNG CD-ROM SC", | |
2266 | "SanDisk SDP3B-64", | |
1da177e4 LT |
2267 | "ATAPI CD-ROM DRIVE 40X MAXIMUM", |
2268 | "_NEC DV5800A", | |
2269 | }; | |
2270 | ||
057ace5e | 2271 | static int ata_dma_blacklisted(const struct ata_device *dev) |
1da177e4 LT |
2272 | { |
2273 | unsigned char model_num[40]; | |
2274 | char *s; | |
2275 | unsigned int len; | |
2276 | int i; | |
2277 | ||
2278 | ata_dev_id_string(dev->id, model_num, ATA_ID_PROD_OFS, | |
2279 | sizeof(model_num)); | |
2280 | s = &model_num[0]; | |
2281 | len = strnlen(s, sizeof(model_num)); | |
2282 | ||
2283 | /* ATAPI specifies that empty space is blank-filled; remove blanks */ | |
2284 | while ((len > 0) && (s[len - 1] == ' ')) { | |
2285 | len--; | |
2286 | s[len] = 0; | |
2287 | } | |
2288 | ||
2289 | for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i++) | |
2290 | if (!strncmp(ata_dma_blacklist[i], s, len)) | |
2291 | return 1; | |
2292 | ||
2293 | return 0; | |
2294 | } | |
2295 | ||
057ace5e | 2296 | static unsigned int ata_get_mode_mask(const struct ata_port *ap, int shift) |
1da177e4 | 2297 | { |
057ace5e | 2298 | const struct ata_device *master, *slave; |
1da177e4 LT |
2299 | unsigned int mask; |
2300 | ||
2301 | master = &ap->device[0]; | |
2302 | slave = &ap->device[1]; | |
2303 | ||
2304 | assert (ata_dev_present(master) || ata_dev_present(slave)); | |
2305 | ||
2306 | if (shift == ATA_SHIFT_UDMA) { | |
2307 | mask = ap->udma_mask; | |
2308 | if (ata_dev_present(master)) { | |
2309 | mask &= (master->id[ATA_ID_UDMA_MODES] & 0xff); | |
057ace5e | 2310 | if (ata_dma_blacklisted(master)) { |
1da177e4 LT |
2311 | mask = 0; |
2312 | ata_pr_blacklisted(ap, master); | |
2313 | } | |
2314 | } | |
2315 | if (ata_dev_present(slave)) { | |
2316 | mask &= (slave->id[ATA_ID_UDMA_MODES] & 0xff); | |
057ace5e | 2317 | if (ata_dma_blacklisted(slave)) { |
1da177e4 LT |
2318 | mask = 0; |
2319 | ata_pr_blacklisted(ap, slave); | |
2320 | } | |
2321 | } | |
2322 | } | |
2323 | else if (shift == ATA_SHIFT_MWDMA) { | |
2324 | mask = ap->mwdma_mask; | |
2325 | if (ata_dev_present(master)) { | |
2326 | mask &= (master->id[ATA_ID_MWDMA_MODES] & 0x07); | |
057ace5e | 2327 | if (ata_dma_blacklisted(master)) { |
1da177e4 LT |
2328 | mask = 0; |
2329 | ata_pr_blacklisted(ap, master); | |
2330 | } | |
2331 | } | |
2332 | if (ata_dev_present(slave)) { | |
2333 | mask &= (slave->id[ATA_ID_MWDMA_MODES] & 0x07); | |
057ace5e | 2334 | if (ata_dma_blacklisted(slave)) { |
1da177e4 LT |
2335 | mask = 0; |
2336 | ata_pr_blacklisted(ap, slave); | |
2337 | } | |
2338 | } | |
2339 | } | |
2340 | else if (shift == ATA_SHIFT_PIO) { | |
2341 | mask = ap->pio_mask; | |
2342 | if (ata_dev_present(master)) { | |
2343 | /* spec doesn't return explicit support for | |
2344 | * PIO0-2, so we fake it | |
2345 | */ | |
2346 | u16 tmp_mode = master->id[ATA_ID_PIO_MODES] & 0x03; | |
2347 | tmp_mode <<= 3; | |
2348 | tmp_mode |= 0x7; | |
2349 | mask &= tmp_mode; | |
2350 | } | |
2351 | if (ata_dev_present(slave)) { | |
2352 | /* spec doesn't return explicit support for | |
2353 | * PIO0-2, so we fake it | |
2354 | */ | |
2355 | u16 tmp_mode = slave->id[ATA_ID_PIO_MODES] & 0x03; | |
2356 | tmp_mode <<= 3; | |
2357 | tmp_mode |= 0x7; | |
2358 | mask &= tmp_mode; | |
2359 | } | |
2360 | } | |
2361 | else { | |
2362 | mask = 0xffffffff; /* shut up compiler warning */ | |
2363 | BUG(); | |
2364 | } | |
2365 | ||
2366 | return mask; | |
2367 | } | |
2368 | ||
2369 | /* find greatest bit */ | |
2370 | static int fgb(u32 bitmap) | |
2371 | { | |
2372 | unsigned int i; | |
2373 | int x = -1; | |
2374 | ||
2375 | for (i = 0; i < 32; i++) | |
2376 | if (bitmap & (1 << i)) | |
2377 | x = i; | |
2378 | ||
2379 | return x; | |
2380 | } | |
2381 | ||
2382 | /** | |
2383 | * ata_choose_xfer_mode - attempt to find best transfer mode | |
2384 | * @ap: Port for which an xfer mode will be selected | |
2385 | * @xfer_mode_out: (output) SET FEATURES - XFER MODE code | |
2386 | * @xfer_shift_out: (output) bit shift that selects this mode | |
2387 | * | |
0cba632b JG |
2388 | * Based on host and device capabilities, determine the |
2389 | * maximum transfer mode that is amenable to all. | |
2390 | * | |
1da177e4 | 2391 | * LOCKING: |
0cba632b | 2392 | * PCI/etc. bus probe sem. |
1da177e4 LT |
2393 | * |
2394 | * RETURNS: | |
2395 | * Zero on success, negative on error. | |
2396 | */ | |
2397 | ||
057ace5e | 2398 | static int ata_choose_xfer_mode(const struct ata_port *ap, |
1da177e4 LT |
2399 | u8 *xfer_mode_out, |
2400 | unsigned int *xfer_shift_out) | |
2401 | { | |
2402 | unsigned int mask, shift; | |
2403 | int x, i; | |
2404 | ||
2405 | for (i = 0; i < ARRAY_SIZE(xfer_mode_classes); i++) { | |
2406 | shift = xfer_mode_classes[i].shift; | |
2407 | mask = ata_get_mode_mask(ap, shift); | |
2408 | ||
2409 | x = fgb(mask); | |
2410 | if (x >= 0) { | |
2411 | *xfer_mode_out = xfer_mode_classes[i].base + x; | |
2412 | *xfer_shift_out = shift; | |
2413 | return 0; | |
2414 | } | |
2415 | } | |
2416 | ||
2417 | return -1; | |
2418 | } | |
2419 | ||
2420 | /** | |
2421 | * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command | |
2422 | * @ap: Port associated with device @dev | |
2423 | * @dev: Device to which command will be sent | |
2424 | * | |
780a87f7 JG |
2425 | * Issue SET FEATURES - XFER MODE command to device @dev |
2426 | * on port @ap. | |
2427 | * | |
1da177e4 | 2428 | * LOCKING: |
0cba632b | 2429 | * PCI/etc. bus probe sem. |
1da177e4 LT |
2430 | */ |
2431 | ||
2432 | static void ata_dev_set_xfermode(struct ata_port *ap, struct ata_device *dev) | |
2433 | { | |
a0123703 | 2434 | struct ata_taskfile tf; |
1da177e4 LT |
2435 | |
2436 | /* set up set-features taskfile */ | |
2437 | DPRINTK("set features - xfer mode\n"); | |
2438 | ||
a0123703 TH |
2439 | ata_tf_init(ap, &tf, dev->devno); |
2440 | tf.command = ATA_CMD_SET_FEATURES; | |
2441 | tf.feature = SETFEATURES_XFER; | |
2442 | tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; | |
2443 | tf.protocol = ATA_PROT_NODATA; | |
2444 | tf.nsect = dev->xfer_mode; | |
1da177e4 | 2445 | |
a0123703 TH |
2446 | if (ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0)) { |
2447 | printk(KERN_ERR "ata%u: failed to set xfermode, disabled\n", | |
2448 | ap->id); | |
1da177e4 | 2449 | ata_port_disable(ap); |
a0123703 | 2450 | } |
1da177e4 LT |
2451 | |
2452 | DPRINTK("EXIT\n"); | |
2453 | } | |
2454 | ||
59a10b17 AL |
2455 | /** |
2456 | * ata_dev_reread_id - Reread the device identify device info | |
2457 | * @ap: port where the device is | |
2458 | * @dev: device to reread the identify device info | |
2459 | * | |
2460 | * LOCKING: | |
2461 | */ | |
2462 | ||
2463 | static void ata_dev_reread_id(struct ata_port *ap, struct ata_device *dev) | |
2464 | { | |
a0123703 | 2465 | struct ata_taskfile tf; |
59a10b17 | 2466 | |
a0123703 | 2467 | ata_tf_init(ap, &tf, dev->devno); |
59a10b17 AL |
2468 | |
2469 | if (dev->class == ATA_DEV_ATA) { | |
a0123703 | 2470 | tf.command = ATA_CMD_ID_ATA; |
59a10b17 AL |
2471 | DPRINTK("do ATA identify\n"); |
2472 | } else { | |
a0123703 | 2473 | tf.command = ATA_CMD_ID_ATAPI; |
59a10b17 AL |
2474 | DPRINTK("do ATAPI identify\n"); |
2475 | } | |
2476 | ||
a0123703 TH |
2477 | tf.flags |= ATA_TFLAG_DEVICE; |
2478 | tf.protocol = ATA_PROT_PIO; | |
59a10b17 | 2479 | |
a0123703 TH |
2480 | if (ata_exec_internal(ap, dev, &tf, DMA_FROM_DEVICE, |
2481 | dev->id, sizeof(dev->id))) | |
59a10b17 AL |
2482 | goto err_out; |
2483 | ||
59a10b17 AL |
2484 | swap_buf_le16(dev->id, ATA_ID_WORDS); |
2485 | ||
2486 | ata_dump_id(dev); | |
2487 | ||
2488 | DPRINTK("EXIT\n"); | |
2489 | ||
2490 | return; | |
2491 | err_out: | |
a0123703 | 2492 | printk(KERN_ERR "ata%u: failed to reread ID, disabled\n", ap->id); |
59a10b17 AL |
2493 | ata_port_disable(ap); |
2494 | } | |
2495 | ||
8bf62ece AL |
2496 | /** |
2497 | * ata_dev_init_params - Issue INIT DEV PARAMS command | |
2498 | * @ap: Port associated with device @dev | |
2499 | * @dev: Device to which command will be sent | |
2500 | * | |
2501 | * LOCKING: | |
2502 | */ | |
2503 | ||
2504 | static void ata_dev_init_params(struct ata_port *ap, struct ata_device *dev) | |
2505 | { | |
a0123703 | 2506 | struct ata_taskfile tf; |
8bf62ece AL |
2507 | u16 sectors = dev->id[6]; |
2508 | u16 heads = dev->id[3]; | |
2509 | ||
2510 | /* Number of sectors per track 1-255. Number of heads 1-16 */ | |
2511 | if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16) | |
2512 | return; | |
2513 | ||
2514 | /* set up init dev params taskfile */ | |
2515 | DPRINTK("init dev params \n"); | |
2516 | ||
a0123703 TH |
2517 | ata_tf_init(ap, &tf, dev->devno); |
2518 | tf.command = ATA_CMD_INIT_DEV_PARAMS; | |
2519 | tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; | |
2520 | tf.protocol = ATA_PROT_NODATA; | |
2521 | tf.nsect = sectors; | |
2522 | tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */ | |
8bf62ece | 2523 | |
a0123703 TH |
2524 | if (ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0)) { |
2525 | printk(KERN_ERR "ata%u: failed to init parameters, disabled\n", | |
2526 | ap->id); | |
8bf62ece | 2527 | ata_port_disable(ap); |
a0123703 | 2528 | } |
8bf62ece AL |
2529 | |
2530 | DPRINTK("EXIT\n"); | |
2531 | } | |
2532 | ||
1da177e4 | 2533 | /** |
0cba632b JG |
2534 | * ata_sg_clean - Unmap DMA memory associated with command |
2535 | * @qc: Command containing DMA memory to be released | |
2536 | * | |
2537 | * Unmap all mapped DMA memory associated with this command. | |
1da177e4 LT |
2538 | * |
2539 | * LOCKING: | |
0cba632b | 2540 | * spin_lock_irqsave(host_set lock) |
1da177e4 LT |
2541 | */ |
2542 | ||
2543 | static void ata_sg_clean(struct ata_queued_cmd *qc) | |
2544 | { | |
2545 | struct ata_port *ap = qc->ap; | |
cedc9a47 | 2546 | struct scatterlist *sg = qc->__sg; |
1da177e4 | 2547 | int dir = qc->dma_dir; |
cedc9a47 | 2548 | void *pad_buf = NULL; |
1da177e4 LT |
2549 | |
2550 | assert(qc->flags & ATA_QCFLAG_DMAMAP); | |
2551 | assert(sg != NULL); | |
2552 | ||
2553 | if (qc->flags & ATA_QCFLAG_SINGLE) | |
2554 | assert(qc->n_elem == 1); | |
2555 | ||
2c13b7ce | 2556 | VPRINTK("unmapping %u sg elements\n", qc->n_elem); |
1da177e4 | 2557 | |
cedc9a47 JG |
2558 | /* if we padded the buffer out to 32-bit bound, and data |
2559 | * xfer direction is from-device, we must copy from the | |
2560 | * pad buffer back into the supplied buffer | |
2561 | */ | |
2562 | if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE)) | |
2563 | pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ); | |
2564 | ||
2565 | if (qc->flags & ATA_QCFLAG_SG) { | |
e1410f2d JG |
2566 | if (qc->n_elem) |
2567 | dma_unmap_sg(ap->host_set->dev, sg, qc->n_elem, dir); | |
cedc9a47 JG |
2568 | /* restore last sg */ |
2569 | sg[qc->orig_n_elem - 1].length += qc->pad_len; | |
2570 | if (pad_buf) { | |
2571 | struct scatterlist *psg = &qc->pad_sgent; | |
2572 | void *addr = kmap_atomic(psg->page, KM_IRQ0); | |
2573 | memcpy(addr + psg->offset, pad_buf, qc->pad_len); | |
dfa15988 | 2574 | kunmap_atomic(addr, KM_IRQ0); |
cedc9a47 JG |
2575 | } |
2576 | } else { | |
e1410f2d JG |
2577 | if (sg_dma_len(&sg[0]) > 0) |
2578 | dma_unmap_single(ap->host_set->dev, | |
2579 | sg_dma_address(&sg[0]), sg_dma_len(&sg[0]), | |
2580 | dir); | |
cedc9a47 JG |
2581 | /* restore sg */ |
2582 | sg->length += qc->pad_len; | |
2583 | if (pad_buf) | |
2584 | memcpy(qc->buf_virt + sg->length - qc->pad_len, | |
2585 | pad_buf, qc->pad_len); | |
2586 | } | |
1da177e4 LT |
2587 | |
2588 | qc->flags &= ~ATA_QCFLAG_DMAMAP; | |
cedc9a47 | 2589 | qc->__sg = NULL; |
1da177e4 LT |
2590 | } |
2591 | ||
2592 | /** | |
2593 | * ata_fill_sg - Fill PCI IDE PRD table | |
2594 | * @qc: Metadata associated with taskfile to be transferred | |
2595 | * | |
780a87f7 JG |
2596 | * Fill PCI IDE PRD (scatter-gather) table with segments |
2597 | * associated with the current disk command. | |
2598 | * | |
1da177e4 | 2599 | * LOCKING: |
780a87f7 | 2600 | * spin_lock_irqsave(host_set lock) |
1da177e4 LT |
2601 | * |
2602 | */ | |
2603 | static void ata_fill_sg(struct ata_queued_cmd *qc) | |
2604 | { | |
1da177e4 | 2605 | struct ata_port *ap = qc->ap; |
cedc9a47 JG |
2606 | struct scatterlist *sg; |
2607 | unsigned int idx; | |
1da177e4 | 2608 | |
cedc9a47 | 2609 | assert(qc->__sg != NULL); |
1da177e4 LT |
2610 | assert(qc->n_elem > 0); |
2611 | ||
2612 | idx = 0; | |
cedc9a47 | 2613 | ata_for_each_sg(sg, qc) { |
1da177e4 LT |
2614 | u32 addr, offset; |
2615 | u32 sg_len, len; | |
2616 | ||
2617 | /* determine if physical DMA addr spans 64K boundary. | |
2618 | * Note h/w doesn't support 64-bit, so we unconditionally | |
2619 | * truncate dma_addr_t to u32. | |
2620 | */ | |
2621 | addr = (u32) sg_dma_address(sg); | |
2622 | sg_len = sg_dma_len(sg); | |
2623 | ||
2624 | while (sg_len) { | |
2625 | offset = addr & 0xffff; | |
2626 | len = sg_len; | |
2627 | if ((offset + sg_len) > 0x10000) | |
2628 | len = 0x10000 - offset; | |
2629 | ||
2630 | ap->prd[idx].addr = cpu_to_le32(addr); | |
2631 | ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff); | |
2632 | VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len); | |
2633 | ||
2634 | idx++; | |
2635 | sg_len -= len; | |
2636 | addr += len; | |
2637 | } | |
2638 | } | |
2639 | ||
2640 | if (idx) | |
2641 | ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT); | |
2642 | } | |
2643 | /** | |
2644 | * ata_check_atapi_dma - Check whether ATAPI DMA can be supported | |
2645 | * @qc: Metadata associated with taskfile to check | |
2646 | * | |
780a87f7 JG |
2647 | * Allow low-level driver to filter ATA PACKET commands, returning |
2648 | * a status indicating whether or not it is OK to use DMA for the | |
2649 | * supplied PACKET command. | |
2650 | * | |
1da177e4 | 2651 | * LOCKING: |
0cba632b JG |
2652 | * spin_lock_irqsave(host_set lock) |
2653 | * | |
1da177e4 LT |
2654 | * RETURNS: 0 when ATAPI DMA can be used |
2655 | * nonzero otherwise | |
2656 | */ | |
2657 | int ata_check_atapi_dma(struct ata_queued_cmd *qc) | |
2658 | { | |
2659 | struct ata_port *ap = qc->ap; | |
2660 | int rc = 0; /* Assume ATAPI DMA is OK by default */ | |
2661 | ||
2662 | if (ap->ops->check_atapi_dma) | |
2663 | rc = ap->ops->check_atapi_dma(qc); | |
2664 | ||
2665 | return rc; | |
2666 | } | |
2667 | /** | |
2668 | * ata_qc_prep - Prepare taskfile for submission | |
2669 | * @qc: Metadata associated with taskfile to be prepared | |
2670 | * | |
780a87f7 JG |
2671 | * Prepare ATA taskfile for submission. |
2672 | * | |
1da177e4 LT |
2673 | * LOCKING: |
2674 | * spin_lock_irqsave(host_set lock) | |
2675 | */ | |
2676 | void ata_qc_prep(struct ata_queued_cmd *qc) | |
2677 | { | |
2678 | if (!(qc->flags & ATA_QCFLAG_DMAMAP)) | |
2679 | return; | |
2680 | ||
2681 | ata_fill_sg(qc); | |
2682 | } | |
2683 | ||
0cba632b JG |
2684 | /** |
2685 | * ata_sg_init_one - Associate command with memory buffer | |
2686 | * @qc: Command to be associated | |
2687 | * @buf: Memory buffer | |
2688 | * @buflen: Length of memory buffer, in bytes. | |
2689 | * | |
2690 | * Initialize the data-related elements of queued_cmd @qc | |
2691 | * to point to a single memory buffer, @buf of byte length @buflen. | |
2692 | * | |
2693 | * LOCKING: | |
2694 | * spin_lock_irqsave(host_set lock) | |
2695 | */ | |
2696 | ||
1da177e4 LT |
2697 | void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen) |
2698 | { | |
2699 | struct scatterlist *sg; | |
2700 | ||
2701 | qc->flags |= ATA_QCFLAG_SINGLE; | |
2702 | ||
2703 | memset(&qc->sgent, 0, sizeof(qc->sgent)); | |
cedc9a47 | 2704 | qc->__sg = &qc->sgent; |
1da177e4 | 2705 | qc->n_elem = 1; |
cedc9a47 | 2706 | qc->orig_n_elem = 1; |
1da177e4 LT |
2707 | qc->buf_virt = buf; |
2708 | ||
cedc9a47 | 2709 | sg = qc->__sg; |
f0612bbc | 2710 | sg_init_one(sg, buf, buflen); |
1da177e4 LT |
2711 | } |
2712 | ||
0cba632b JG |
2713 | /** |
2714 | * ata_sg_init - Associate command with scatter-gather table. | |
2715 | * @qc: Command to be associated | |
2716 | * @sg: Scatter-gather table. | |
2717 | * @n_elem: Number of elements in s/g table. | |
2718 | * | |
2719 | * Initialize the data-related elements of queued_cmd @qc | |
2720 | * to point to a scatter-gather table @sg, containing @n_elem | |
2721 | * elements. | |
2722 | * | |
2723 | * LOCKING: | |
2724 | * spin_lock_irqsave(host_set lock) | |
2725 | */ | |
2726 | ||
1da177e4 LT |
2727 | void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg, |
2728 | unsigned int n_elem) | |
2729 | { | |
2730 | qc->flags |= ATA_QCFLAG_SG; | |
cedc9a47 | 2731 | qc->__sg = sg; |
1da177e4 | 2732 | qc->n_elem = n_elem; |
cedc9a47 | 2733 | qc->orig_n_elem = n_elem; |
1da177e4 LT |
2734 | } |
2735 | ||
2736 | /** | |
0cba632b JG |
2737 | * ata_sg_setup_one - DMA-map the memory buffer associated with a command. |
2738 | * @qc: Command with memory buffer to be mapped. | |
2739 | * | |
2740 | * DMA-map the memory buffer associated with queued_cmd @qc. | |
1da177e4 LT |
2741 | * |
2742 | * LOCKING: | |
2743 | * spin_lock_irqsave(host_set lock) | |
2744 | * | |
2745 | * RETURNS: | |
0cba632b | 2746 | * Zero on success, negative on error. |
1da177e4 LT |
2747 | */ |
2748 | ||
2749 | static int ata_sg_setup_one(struct ata_queued_cmd *qc) | |
2750 | { | |
2751 | struct ata_port *ap = qc->ap; | |
2752 | int dir = qc->dma_dir; | |
cedc9a47 | 2753 | struct scatterlist *sg = qc->__sg; |
1da177e4 LT |
2754 | dma_addr_t dma_address; |
2755 | ||
cedc9a47 JG |
2756 | /* we must lengthen transfers to end on a 32-bit boundary */ |
2757 | qc->pad_len = sg->length & 3; | |
2758 | if (qc->pad_len) { | |
2759 | void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ); | |
2760 | struct scatterlist *psg = &qc->pad_sgent; | |
2761 | ||
2762 | assert(qc->dev->class == ATA_DEV_ATAPI); | |
2763 | ||
2764 | memset(pad_buf, 0, ATA_DMA_PAD_SZ); | |
2765 | ||
2766 | if (qc->tf.flags & ATA_TFLAG_WRITE) | |
2767 | memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len, | |
2768 | qc->pad_len); | |
2769 | ||
2770 | sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ); | |
2771 | sg_dma_len(psg) = ATA_DMA_PAD_SZ; | |
2772 | /* trim sg */ | |
2773 | sg->length -= qc->pad_len; | |
2774 | ||
2775 | DPRINTK("padding done, sg->length=%u pad_len=%u\n", | |
2776 | sg->length, qc->pad_len); | |
2777 | } | |
2778 | ||
e1410f2d JG |
2779 | if (!sg->length) { |
2780 | sg_dma_address(sg) = 0; | |
2781 | goto skip_map; | |
2782 | } | |
2783 | ||
1da177e4 | 2784 | dma_address = dma_map_single(ap->host_set->dev, qc->buf_virt, |
32529e01 | 2785 | sg->length, dir); |
537a95d9 TH |
2786 | if (dma_mapping_error(dma_address)) { |
2787 | /* restore sg */ | |
2788 | sg->length += qc->pad_len; | |
1da177e4 | 2789 | return -1; |
537a95d9 | 2790 | } |
1da177e4 LT |
2791 | |
2792 | sg_dma_address(sg) = dma_address; | |
e1410f2d | 2793 | skip_map: |
32529e01 | 2794 | sg_dma_len(sg) = sg->length; |
1da177e4 LT |
2795 | |
2796 | DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg), | |
2797 | qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read"); | |
2798 | ||
2799 | return 0; | |
2800 | } | |
2801 | ||
2802 | /** | |
0cba632b JG |
2803 | * ata_sg_setup - DMA-map the scatter-gather table associated with a command. |
2804 | * @qc: Command with scatter-gather table to be mapped. | |
2805 | * | |
2806 | * DMA-map the scatter-gather table associated with queued_cmd @qc. | |
1da177e4 LT |
2807 | * |
2808 | * LOCKING: | |
2809 | * spin_lock_irqsave(host_set lock) | |
2810 | * | |
2811 | * RETURNS: | |
0cba632b | 2812 | * Zero on success, negative on error. |
1da177e4 LT |
2813 | * |
2814 | */ | |
2815 | ||
2816 | static int ata_sg_setup(struct ata_queued_cmd *qc) | |
2817 | { | |
2818 | struct ata_port *ap = qc->ap; | |
cedc9a47 JG |
2819 | struct scatterlist *sg = qc->__sg; |
2820 | struct scatterlist *lsg = &sg[qc->n_elem - 1]; | |
e1410f2d | 2821 | int n_elem, pre_n_elem, dir, trim_sg = 0; |
1da177e4 LT |
2822 | |
2823 | VPRINTK("ENTER, ata%u\n", ap->id); | |
2824 | assert(qc->flags & ATA_QCFLAG_SG); | |
2825 | ||
cedc9a47 JG |
2826 | /* we must lengthen transfers to end on a 32-bit boundary */ |
2827 | qc->pad_len = lsg->length & 3; | |
2828 | if (qc->pad_len) { | |
2829 | void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ); | |
2830 | struct scatterlist *psg = &qc->pad_sgent; | |
2831 | unsigned int offset; | |
2832 | ||
2833 | assert(qc->dev->class == ATA_DEV_ATAPI); | |
2834 | ||
2835 | memset(pad_buf, 0, ATA_DMA_PAD_SZ); | |
2836 | ||
2837 | /* | |
2838 | * psg->page/offset are used to copy to-be-written | |
2839 | * data in this function or read data in ata_sg_clean. | |
2840 | */ | |
2841 | offset = lsg->offset + lsg->length - qc->pad_len; | |
2842 | psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT); | |
2843 | psg->offset = offset_in_page(offset); | |
2844 | ||
2845 | if (qc->tf.flags & ATA_TFLAG_WRITE) { | |
2846 | void *addr = kmap_atomic(psg->page, KM_IRQ0); | |
2847 | memcpy(pad_buf, addr + psg->offset, qc->pad_len); | |
dfa15988 | 2848 | kunmap_atomic(addr, KM_IRQ0); |
cedc9a47 JG |
2849 | } |
2850 | ||
2851 | sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ); | |
2852 | sg_dma_len(psg) = ATA_DMA_PAD_SZ; | |
2853 | /* trim last sg */ | |
2854 | lsg->length -= qc->pad_len; | |
e1410f2d JG |
2855 | if (lsg->length == 0) |
2856 | trim_sg = 1; | |
cedc9a47 JG |
2857 | |
2858 | DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n", | |
2859 | qc->n_elem - 1, lsg->length, qc->pad_len); | |
2860 | } | |
2861 | ||
e1410f2d JG |
2862 | pre_n_elem = qc->n_elem; |
2863 | if (trim_sg && pre_n_elem) | |
2864 | pre_n_elem--; | |
2865 | ||
2866 | if (!pre_n_elem) { | |
2867 | n_elem = 0; | |
2868 | goto skip_map; | |
2869 | } | |
2870 | ||
1da177e4 | 2871 | dir = qc->dma_dir; |
e1410f2d | 2872 | n_elem = dma_map_sg(ap->host_set->dev, sg, pre_n_elem, dir); |
537a95d9 TH |
2873 | if (n_elem < 1) { |
2874 | /* restore last sg */ | |
2875 | lsg->length += qc->pad_len; | |
1da177e4 | 2876 | return -1; |
537a95d9 | 2877 | } |
1da177e4 LT |
2878 | |
2879 | DPRINTK("%d sg elements mapped\n", n_elem); | |
2880 | ||
e1410f2d | 2881 | skip_map: |
1da177e4 LT |
2882 | qc->n_elem = n_elem; |
2883 | ||
2884 | return 0; | |
2885 | } | |
2886 | ||
40e8c82c TH |
2887 | /** |
2888 | * ata_poll_qc_complete - turn irq back on and finish qc | |
2889 | * @qc: Command to complete | |
8e8b77dd | 2890 | * @err_mask: ATA status register content |
40e8c82c TH |
2891 | * |
2892 | * LOCKING: | |
2893 | * None. (grabs host lock) | |
2894 | */ | |
2895 | ||
a22e2eb0 | 2896 | void ata_poll_qc_complete(struct ata_queued_cmd *qc) |
40e8c82c TH |
2897 | { |
2898 | struct ata_port *ap = qc->ap; | |
b8f6153e | 2899 | unsigned long flags; |
40e8c82c | 2900 | |
b8f6153e | 2901 | spin_lock_irqsave(&ap->host_set->lock, flags); |
40e8c82c TH |
2902 | ap->flags &= ~ATA_FLAG_NOINTR; |
2903 | ata_irq_on(ap); | |
a22e2eb0 | 2904 | ata_qc_complete(qc); |
b8f6153e | 2905 | spin_unlock_irqrestore(&ap->host_set->lock, flags); |
40e8c82c TH |
2906 | } |
2907 | ||
1da177e4 | 2908 | /** |
c893a3ae | 2909 | * ata_pio_poll - poll using PIO, depending on current state |
6f0ef4fa | 2910 | * @ap: the target ata_port |
1da177e4 LT |
2911 | * |
2912 | * LOCKING: | |
0cba632b | 2913 | * None. (executing in kernel thread context) |
1da177e4 LT |
2914 | * |
2915 | * RETURNS: | |
6f0ef4fa | 2916 | * timeout value to use |
1da177e4 LT |
2917 | */ |
2918 | ||
2919 | static unsigned long ata_pio_poll(struct ata_port *ap) | |
2920 | { | |
c14b8331 | 2921 | struct ata_queued_cmd *qc; |
1da177e4 | 2922 | u8 status; |
14be71f4 AL |
2923 | unsigned int poll_state = HSM_ST_UNKNOWN; |
2924 | unsigned int reg_state = HSM_ST_UNKNOWN; | |
14be71f4 | 2925 | |
c14b8331 AL |
2926 | qc = ata_qc_from_tag(ap, ap->active_tag); |
2927 | assert(qc != NULL); | |
2928 | ||
14be71f4 AL |
2929 | switch (ap->hsm_task_state) { |
2930 | case HSM_ST: | |
2931 | case HSM_ST_POLL: | |
2932 | poll_state = HSM_ST_POLL; | |
2933 | reg_state = HSM_ST; | |
1da177e4 | 2934 | break; |
14be71f4 AL |
2935 | case HSM_ST_LAST: |
2936 | case HSM_ST_LAST_POLL: | |
2937 | poll_state = HSM_ST_LAST_POLL; | |
2938 | reg_state = HSM_ST_LAST; | |
1da177e4 LT |
2939 | break; |
2940 | default: | |
2941 | BUG(); | |
2942 | break; | |
2943 | } | |
2944 | ||
2945 | status = ata_chk_status(ap); | |
2946 | if (status & ATA_BUSY) { | |
2947 | if (time_after(jiffies, ap->pio_task_timeout)) { | |
11a56d24 | 2948 | qc->err_mask |= AC_ERR_TIMEOUT; |
7c398335 | 2949 | ap->hsm_task_state = HSM_ST_TMOUT; |
1da177e4 LT |
2950 | return 0; |
2951 | } | |
14be71f4 | 2952 | ap->hsm_task_state = poll_state; |
1da177e4 LT |
2953 | return ATA_SHORT_PAUSE; |
2954 | } | |
2955 | ||
14be71f4 | 2956 | ap->hsm_task_state = reg_state; |
1da177e4 LT |
2957 | return 0; |
2958 | } | |
2959 | ||
2960 | /** | |
6f0ef4fa RD |
2961 | * ata_pio_complete - check if drive is busy or idle |
2962 | * @ap: the target ata_port | |
1da177e4 LT |
2963 | * |
2964 | * LOCKING: | |
0cba632b | 2965 | * None. (executing in kernel thread context) |
7fb6ec28 JG |
2966 | * |
2967 | * RETURNS: | |
2968 | * Non-zero if qc completed, zero otherwise. | |
1da177e4 LT |
2969 | */ |
2970 | ||
7fb6ec28 | 2971 | static int ata_pio_complete (struct ata_port *ap) |
1da177e4 LT |
2972 | { |
2973 | struct ata_queued_cmd *qc; | |
2974 | u8 drv_stat; | |
2975 | ||
2976 | /* | |
31433ea3 AC |
2977 | * This is purely heuristic. This is a fast path. Sometimes when |
2978 | * we enter, BSY will be cleared in a chk-status or two. If not, | |
2979 | * the drive is probably seeking or something. Snooze for a couple | |
2980 | * msecs, then chk-status again. If still busy, fall back to | |
14be71f4 | 2981 | * HSM_ST_POLL state. |
1da177e4 | 2982 | */ |
fe79e683 AL |
2983 | drv_stat = ata_busy_wait(ap, ATA_BUSY, 10); |
2984 | if (drv_stat & ATA_BUSY) { | |
1da177e4 | 2985 | msleep(2); |
fe79e683 AL |
2986 | drv_stat = ata_busy_wait(ap, ATA_BUSY, 10); |
2987 | if (drv_stat & ATA_BUSY) { | |
14be71f4 | 2988 | ap->hsm_task_state = HSM_ST_LAST_POLL; |
1da177e4 | 2989 | ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO; |
7fb6ec28 | 2990 | return 0; |
1da177e4 LT |
2991 | } |
2992 | } | |
2993 | ||
c14b8331 AL |
2994 | qc = ata_qc_from_tag(ap, ap->active_tag); |
2995 | assert(qc != NULL); | |
2996 | ||
1da177e4 LT |
2997 | drv_stat = ata_wait_idle(ap); |
2998 | if (!ata_ok(drv_stat)) { | |
1c848984 | 2999 | qc->err_mask |= __ac_err_mask(drv_stat); |
14be71f4 | 3000 | ap->hsm_task_state = HSM_ST_ERR; |
7fb6ec28 | 3001 | return 0; |
1da177e4 LT |
3002 | } |
3003 | ||
14be71f4 | 3004 | ap->hsm_task_state = HSM_ST_IDLE; |
1da177e4 | 3005 | |
a22e2eb0 AL |
3006 | assert(qc->err_mask == 0); |
3007 | ata_poll_qc_complete(qc); | |
7fb6ec28 JG |
3008 | |
3009 | /* another command may start at this point */ | |
3010 | ||
3011 | return 1; | |
1da177e4 LT |
3012 | } |
3013 | ||
0baab86b EF |
3014 | |
3015 | /** | |
c893a3ae | 3016 | * swap_buf_le16 - swap halves of 16-bit words in place |
0baab86b EF |
3017 | * @buf: Buffer to swap |
3018 | * @buf_words: Number of 16-bit words in buffer. | |
3019 | * | |
3020 | * Swap halves of 16-bit words if needed to convert from | |
3021 | * little-endian byte order to native cpu byte order, or | |
3022 | * vice-versa. | |
3023 | * | |
3024 | * LOCKING: | |
6f0ef4fa | 3025 | * Inherited from caller. |
0baab86b | 3026 | */ |
1da177e4 LT |
3027 | void swap_buf_le16(u16 *buf, unsigned int buf_words) |
3028 | { | |
3029 | #ifdef __BIG_ENDIAN | |
3030 | unsigned int i; | |
3031 | ||
3032 | for (i = 0; i < buf_words; i++) | |
3033 | buf[i] = le16_to_cpu(buf[i]); | |
3034 | #endif /* __BIG_ENDIAN */ | |
3035 | } | |
3036 | ||
6ae4cfb5 AL |
3037 | /** |
3038 | * ata_mmio_data_xfer - Transfer data by MMIO | |
3039 | * @ap: port to read/write | |
3040 | * @buf: data buffer | |
3041 | * @buflen: buffer length | |
344babaa | 3042 | * @write_data: read/write |
6ae4cfb5 AL |
3043 | * |
3044 | * Transfer data from/to the device data register by MMIO. | |
3045 | * | |
3046 | * LOCKING: | |
3047 | * Inherited from caller. | |
6ae4cfb5 AL |
3048 | */ |
3049 | ||
1da177e4 LT |
3050 | static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf, |
3051 | unsigned int buflen, int write_data) | |
3052 | { | |
3053 | unsigned int i; | |
3054 | unsigned int words = buflen >> 1; | |
3055 | u16 *buf16 = (u16 *) buf; | |
3056 | void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr; | |
3057 | ||
6ae4cfb5 | 3058 | /* Transfer multiple of 2 bytes */ |
1da177e4 LT |
3059 | if (write_data) { |
3060 | for (i = 0; i < words; i++) | |
3061 | writew(le16_to_cpu(buf16[i]), mmio); | |
3062 | } else { | |
3063 | for (i = 0; i < words; i++) | |
3064 | buf16[i] = cpu_to_le16(readw(mmio)); | |
3065 | } | |
6ae4cfb5 AL |
3066 | |
3067 | /* Transfer trailing 1 byte, if any. */ | |
3068 | if (unlikely(buflen & 0x01)) { | |
3069 | u16 align_buf[1] = { 0 }; | |
3070 | unsigned char *trailing_buf = buf + buflen - 1; | |
3071 | ||
3072 | if (write_data) { | |
3073 | memcpy(align_buf, trailing_buf, 1); | |
3074 | writew(le16_to_cpu(align_buf[0]), mmio); | |
3075 | } else { | |
3076 | align_buf[0] = cpu_to_le16(readw(mmio)); | |
3077 | memcpy(trailing_buf, align_buf, 1); | |
3078 | } | |
3079 | } | |
1da177e4 LT |
3080 | } |
3081 | ||
6ae4cfb5 AL |
3082 | /** |
3083 | * ata_pio_data_xfer - Transfer data by PIO | |
3084 | * @ap: port to read/write | |
3085 | * @buf: data buffer | |
3086 | * @buflen: buffer length | |
344babaa | 3087 | * @write_data: read/write |
6ae4cfb5 AL |
3088 | * |
3089 | * Transfer data from/to the device data register by PIO. | |
3090 | * | |
3091 | * LOCKING: | |
3092 | * Inherited from caller. | |
6ae4cfb5 AL |
3093 | */ |
3094 | ||
1da177e4 LT |
3095 | static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf, |
3096 | unsigned int buflen, int write_data) | |
3097 | { | |
6ae4cfb5 | 3098 | unsigned int words = buflen >> 1; |
1da177e4 | 3099 | |
6ae4cfb5 | 3100 | /* Transfer multiple of 2 bytes */ |
1da177e4 | 3101 | if (write_data) |
6ae4cfb5 | 3102 | outsw(ap->ioaddr.data_addr, buf, words); |
1da177e4 | 3103 | else |
6ae4cfb5 AL |
3104 | insw(ap->ioaddr.data_addr, buf, words); |
3105 | ||
3106 | /* Transfer trailing 1 byte, if any. */ | |
3107 | if (unlikely(buflen & 0x01)) { | |
3108 | u16 align_buf[1] = { 0 }; | |
3109 | unsigned char *trailing_buf = buf + buflen - 1; | |
3110 | ||
3111 | if (write_data) { | |
3112 | memcpy(align_buf, trailing_buf, 1); | |
3113 | outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr); | |
3114 | } else { | |
3115 | align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr)); | |
3116 | memcpy(trailing_buf, align_buf, 1); | |
3117 | } | |
3118 | } | |
1da177e4 LT |
3119 | } |
3120 | ||
6ae4cfb5 AL |
3121 | /** |
3122 | * ata_data_xfer - Transfer data from/to the data register. | |
3123 | * @ap: port to read/write | |
3124 | * @buf: data buffer | |
3125 | * @buflen: buffer length | |
3126 | * @do_write: read/write | |
3127 | * | |
3128 | * Transfer data from/to the device data register. | |
3129 | * | |
3130 | * LOCKING: | |
3131 | * Inherited from caller. | |
6ae4cfb5 AL |
3132 | */ |
3133 | ||
1da177e4 LT |
3134 | static void ata_data_xfer(struct ata_port *ap, unsigned char *buf, |
3135 | unsigned int buflen, int do_write) | |
3136 | { | |
a1bd9e68 AC |
3137 | /* Make the crap hardware pay the costs not the good stuff */ |
3138 | if (unlikely(ap->flags & ATA_FLAG_IRQ_MASK)) { | |
3139 | unsigned long flags; | |
3140 | local_irq_save(flags); | |
3141 | if (ap->flags & ATA_FLAG_MMIO) | |
3142 | ata_mmio_data_xfer(ap, buf, buflen, do_write); | |
3143 | else | |
3144 | ata_pio_data_xfer(ap, buf, buflen, do_write); | |
3145 | local_irq_restore(flags); | |
3146 | } else { | |
3147 | if (ap->flags & ATA_FLAG_MMIO) | |
3148 | ata_mmio_data_xfer(ap, buf, buflen, do_write); | |
3149 | else | |
3150 | ata_pio_data_xfer(ap, buf, buflen, do_write); | |
3151 | } | |
1da177e4 LT |
3152 | } |
3153 | ||
6ae4cfb5 AL |
3154 | /** |
3155 | * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data. | |
3156 | * @qc: Command on going | |
3157 | * | |
3158 | * Transfer ATA_SECT_SIZE of data from/to the ATA device. | |
3159 | * | |
3160 | * LOCKING: | |
3161 | * Inherited from caller. | |
3162 | */ | |
3163 | ||
1da177e4 LT |
3164 | static void ata_pio_sector(struct ata_queued_cmd *qc) |
3165 | { | |
3166 | int do_write = (qc->tf.flags & ATA_TFLAG_WRITE); | |
cedc9a47 | 3167 | struct scatterlist *sg = qc->__sg; |
1da177e4 LT |
3168 | struct ata_port *ap = qc->ap; |
3169 | struct page *page; | |
3170 | unsigned int offset; | |
3171 | unsigned char *buf; | |
3172 | ||
3173 | if (qc->cursect == (qc->nsect - 1)) | |
14be71f4 | 3174 | ap->hsm_task_state = HSM_ST_LAST; |
1da177e4 LT |
3175 | |
3176 | page = sg[qc->cursg].page; | |
3177 | offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE; | |
3178 | ||
3179 | /* get the current page and offset */ | |
3180 | page = nth_page(page, (offset >> PAGE_SHIFT)); | |
3181 | offset %= PAGE_SIZE; | |
3182 | ||
3183 | buf = kmap(page) + offset; | |
3184 | ||
3185 | qc->cursect++; | |
3186 | qc->cursg_ofs++; | |
3187 | ||
32529e01 | 3188 | if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) { |
1da177e4 LT |
3189 | qc->cursg++; |
3190 | qc->cursg_ofs = 0; | |
3191 | } | |
3192 | ||
3193 | DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read"); | |
3194 | ||
3195 | /* do the actual data transfer */ | |
3196 | do_write = (qc->tf.flags & ATA_TFLAG_WRITE); | |
3197 | ata_data_xfer(ap, buf, ATA_SECT_SIZE, do_write); | |
3198 | ||
3199 | kunmap(page); | |
3200 | } | |
3201 | ||
6ae4cfb5 AL |
3202 | /** |
3203 | * __atapi_pio_bytes - Transfer data from/to the ATAPI device. | |
3204 | * @qc: Command on going | |
3205 | * @bytes: number of bytes | |
3206 | * | |
3207 | * Transfer Transfer data from/to the ATAPI device. | |
3208 | * | |
3209 | * LOCKING: | |
3210 | * Inherited from caller. | |
3211 | * | |
3212 | */ | |
3213 | ||
1da177e4 LT |
3214 | static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes) |
3215 | { | |
3216 | int do_write = (qc->tf.flags & ATA_TFLAG_WRITE); | |
cedc9a47 | 3217 | struct scatterlist *sg = qc->__sg; |
1da177e4 LT |
3218 | struct ata_port *ap = qc->ap; |
3219 | struct page *page; | |
3220 | unsigned char *buf; | |
3221 | unsigned int offset, count; | |
3222 | ||
563a6e1f | 3223 | if (qc->curbytes + bytes >= qc->nbytes) |
14be71f4 | 3224 | ap->hsm_task_state = HSM_ST_LAST; |
1da177e4 LT |
3225 | |
3226 | next_sg: | |
563a6e1f | 3227 | if (unlikely(qc->cursg >= qc->n_elem)) { |
7fb6ec28 | 3228 | /* |
563a6e1f AL |
3229 | * The end of qc->sg is reached and the device expects |
3230 | * more data to transfer. In order not to overrun qc->sg | |
3231 | * and fulfill length specified in the byte count register, | |
3232 | * - for read case, discard trailing data from the device | |
3233 | * - for write case, padding zero data to the device | |
3234 | */ | |
3235 | u16 pad_buf[1] = { 0 }; | |
3236 | unsigned int words = bytes >> 1; | |
3237 | unsigned int i; | |
3238 | ||
3239 | if (words) /* warning if bytes > 1 */ | |
7fb6ec28 | 3240 | printk(KERN_WARNING "ata%u: %u bytes trailing data\n", |
563a6e1f AL |
3241 | ap->id, bytes); |
3242 | ||
3243 | for (i = 0; i < words; i++) | |
3244 | ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write); | |
3245 | ||
14be71f4 | 3246 | ap->hsm_task_state = HSM_ST_LAST; |
563a6e1f AL |
3247 | return; |
3248 | } | |
3249 | ||
cedc9a47 | 3250 | sg = &qc->__sg[qc->cursg]; |
1da177e4 | 3251 | |
1da177e4 LT |
3252 | page = sg->page; |
3253 | offset = sg->offset + qc->cursg_ofs; | |
3254 | ||
3255 | /* get the current page and offset */ | |
3256 | page = nth_page(page, (offset >> PAGE_SHIFT)); | |
3257 | offset %= PAGE_SIZE; | |
3258 | ||
6952df03 | 3259 | /* don't overrun current sg */ |
32529e01 | 3260 | count = min(sg->length - qc->cursg_ofs, bytes); |
1da177e4 LT |
3261 | |
3262 | /* don't cross page boundaries */ | |
3263 | count = min(count, (unsigned int)PAGE_SIZE - offset); | |
3264 | ||
3265 | buf = kmap(page) + offset; | |
3266 | ||
3267 | bytes -= count; | |
3268 | qc->curbytes += count; | |
3269 | qc->cursg_ofs += count; | |
3270 | ||
32529e01 | 3271 | if (qc->cursg_ofs == sg->length) { |
1da177e4 LT |
3272 | qc->cursg++; |
3273 | qc->cursg_ofs = 0; | |
3274 | } | |
3275 | ||
3276 | DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read"); | |
3277 | ||
3278 | /* do the actual data transfer */ | |
3279 | ata_data_xfer(ap, buf, count, do_write); | |
3280 | ||
3281 | kunmap(page); | |
3282 | ||
563a6e1f | 3283 | if (bytes) |
1da177e4 | 3284 | goto next_sg; |
1da177e4 LT |
3285 | } |
3286 | ||
6ae4cfb5 AL |
3287 | /** |
3288 | * atapi_pio_bytes - Transfer data from/to the ATAPI device. | |
3289 | * @qc: Command on going | |
3290 | * | |
3291 | * Transfer Transfer data from/to the ATAPI device. | |
3292 | * | |
3293 | * LOCKING: | |
3294 | * Inherited from caller. | |
6ae4cfb5 AL |
3295 | */ |
3296 | ||
1da177e4 LT |
3297 | static void atapi_pio_bytes(struct ata_queued_cmd *qc) |
3298 | { | |
3299 | struct ata_port *ap = qc->ap; | |
3300 | struct ata_device *dev = qc->dev; | |
3301 | unsigned int ireason, bc_lo, bc_hi, bytes; | |
3302 | int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0; | |
3303 | ||
3304 | ap->ops->tf_read(ap, &qc->tf); | |
3305 | ireason = qc->tf.nsect; | |
3306 | bc_lo = qc->tf.lbam; | |
3307 | bc_hi = qc->tf.lbah; | |
3308 | bytes = (bc_hi << 8) | bc_lo; | |
3309 | ||
3310 | /* shall be cleared to zero, indicating xfer of data */ | |
3311 | if (ireason & (1 << 0)) | |
3312 | goto err_out; | |
3313 | ||
3314 | /* make sure transfer direction matches expected */ | |
3315 | i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0; | |
3316 | if (do_write != i_write) | |
3317 | goto err_out; | |
3318 | ||
3319 | __atapi_pio_bytes(qc, bytes); | |
3320 | ||
3321 | return; | |
3322 | ||
3323 | err_out: | |
3324 | printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n", | |
3325 | ap->id, dev->devno); | |
11a56d24 | 3326 | qc->err_mask |= AC_ERR_HSM; |
14be71f4 | 3327 | ap->hsm_task_state = HSM_ST_ERR; |
1da177e4 LT |
3328 | } |
3329 | ||
3330 | /** | |
6f0ef4fa RD |
3331 | * ata_pio_block - start PIO on a block |
3332 | * @ap: the target ata_port | |
1da177e4 LT |
3333 | * |
3334 | * LOCKING: | |
0cba632b | 3335 | * None. (executing in kernel thread context) |
1da177e4 LT |
3336 | */ |
3337 | ||
3338 | static void ata_pio_block(struct ata_port *ap) | |
3339 | { | |
3340 | struct ata_queued_cmd *qc; | |
3341 | u8 status; | |
3342 | ||
3343 | /* | |
6f0ef4fa | 3344 | * This is purely heuristic. This is a fast path. |
1da177e4 LT |
3345 | * Sometimes when we enter, BSY will be cleared in |
3346 | * a chk-status or two. If not, the drive is probably seeking | |
3347 | * or something. Snooze for a couple msecs, then | |
3348 | * chk-status again. If still busy, fall back to | |
14be71f4 | 3349 | * HSM_ST_POLL state. |
1da177e4 LT |
3350 | */ |
3351 | status = ata_busy_wait(ap, ATA_BUSY, 5); | |
3352 | if (status & ATA_BUSY) { | |
3353 | msleep(2); | |
3354 | status = ata_busy_wait(ap, ATA_BUSY, 10); | |
3355 | if (status & ATA_BUSY) { | |
14be71f4 | 3356 | ap->hsm_task_state = HSM_ST_POLL; |
1da177e4 LT |
3357 | ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO; |
3358 | return; | |
3359 | } | |
3360 | } | |
3361 | ||
3362 | qc = ata_qc_from_tag(ap, ap->active_tag); | |
3363 | assert(qc != NULL); | |
3364 | ||
fe79e683 AL |
3365 | /* check error */ |
3366 | if (status & (ATA_ERR | ATA_DF)) { | |
3367 | qc->err_mask |= AC_ERR_DEV; | |
3368 | ap->hsm_task_state = HSM_ST_ERR; | |
3369 | return; | |
3370 | } | |
3371 | ||
3372 | /* transfer data if any */ | |
1da177e4 | 3373 | if (is_atapi_taskfile(&qc->tf)) { |
fe79e683 | 3374 | /* DRQ=0 means no more data to transfer */ |
1da177e4 | 3375 | if ((status & ATA_DRQ) == 0) { |
14be71f4 | 3376 | ap->hsm_task_state = HSM_ST_LAST; |
1da177e4 LT |
3377 | return; |
3378 | } | |
3379 | ||
3380 | atapi_pio_bytes(qc); | |
3381 | } else { | |
3382 | /* handle BSY=0, DRQ=0 as error */ | |
3383 | if ((status & ATA_DRQ) == 0) { | |
11a56d24 | 3384 | qc->err_mask |= AC_ERR_HSM; |
14be71f4 | 3385 | ap->hsm_task_state = HSM_ST_ERR; |
1da177e4 LT |
3386 | return; |
3387 | } | |
3388 | ||
3389 | ata_pio_sector(qc); | |
3390 | } | |
3391 | } | |
3392 | ||
3393 | static void ata_pio_error(struct ata_port *ap) | |
3394 | { | |
3395 | struct ata_queued_cmd *qc; | |
a7dac447 JG |
3396 | |
3397 | printk(KERN_WARNING "ata%u: PIO error\n", ap->id); | |
1da177e4 LT |
3398 | |
3399 | qc = ata_qc_from_tag(ap, ap->active_tag); | |
3400 | assert(qc != NULL); | |
3401 | ||
1c848984 AL |
3402 | /* make sure qc->err_mask is available to |
3403 | * know what's wrong and recover | |
3404 | */ | |
3405 | assert(qc->err_mask); | |
3406 | ||
14be71f4 | 3407 | ap->hsm_task_state = HSM_ST_IDLE; |
1da177e4 | 3408 | |
a22e2eb0 | 3409 | ata_poll_qc_complete(qc); |
1da177e4 LT |
3410 | } |
3411 | ||
3412 | static void ata_pio_task(void *_data) | |
3413 | { | |
3414 | struct ata_port *ap = _data; | |
7fb6ec28 JG |
3415 | unsigned long timeout; |
3416 | int qc_completed; | |
3417 | ||
3418 | fsm_start: | |
3419 | timeout = 0; | |
3420 | qc_completed = 0; | |
1da177e4 | 3421 | |
14be71f4 AL |
3422 | switch (ap->hsm_task_state) { |
3423 | case HSM_ST_IDLE: | |
1da177e4 LT |
3424 | return; |
3425 | ||
14be71f4 | 3426 | case HSM_ST: |
1da177e4 LT |
3427 | ata_pio_block(ap); |
3428 | break; | |
3429 | ||
14be71f4 | 3430 | case HSM_ST_LAST: |
7fb6ec28 | 3431 | qc_completed = ata_pio_complete(ap); |
1da177e4 LT |
3432 | break; |
3433 | ||
14be71f4 AL |
3434 | case HSM_ST_POLL: |
3435 | case HSM_ST_LAST_POLL: | |
1da177e4 LT |
3436 | timeout = ata_pio_poll(ap); |
3437 | break; | |
3438 | ||
14be71f4 AL |
3439 | case HSM_ST_TMOUT: |
3440 | case HSM_ST_ERR: | |
1da177e4 LT |
3441 | ata_pio_error(ap); |
3442 | return; | |
3443 | } | |
3444 | ||
3445 | if (timeout) | |
95064379 | 3446 | ata_queue_delayed_pio_task(ap, timeout); |
7fb6ec28 JG |
3447 | else if (!qc_completed) |
3448 | goto fsm_start; | |
1da177e4 LT |
3449 | } |
3450 | ||
1da177e4 LT |
3451 | /** |
3452 | * ata_qc_timeout - Handle timeout of queued command | |
3453 | * @qc: Command that timed out | |
3454 | * | |
3455 | * Some part of the kernel (currently, only the SCSI layer) | |
3456 | * has noticed that the active command on port @ap has not | |
3457 | * completed after a specified length of time. Handle this | |
3458 | * condition by disabling DMA (if necessary) and completing | |
3459 | * transactions, with error if necessary. | |
3460 | * | |
3461 | * This also handles the case of the "lost interrupt", where | |
3462 | * for some reason (possibly hardware bug, possibly driver bug) | |
3463 | * an interrupt was not delivered to the driver, even though the | |
3464 | * transaction completed successfully. | |
3465 | * | |
3466 | * LOCKING: | |
0cba632b | 3467 | * Inherited from SCSI layer (none, can sleep) |
1da177e4 LT |
3468 | */ |
3469 | ||
3470 | static void ata_qc_timeout(struct ata_queued_cmd *qc) | |
3471 | { | |
3472 | struct ata_port *ap = qc->ap; | |
b8f6153e | 3473 | struct ata_host_set *host_set = ap->host_set; |
1da177e4 | 3474 | u8 host_stat = 0, drv_stat; |
b8f6153e | 3475 | unsigned long flags; |
1da177e4 LT |
3476 | |
3477 | DPRINTK("ENTER\n"); | |
3478 | ||
c18d06f8 TH |
3479 | ata_flush_pio_tasks(ap); |
3480 | ap->hsm_task_state = HSM_ST_IDLE; | |
3481 | ||
b8f6153e JG |
3482 | spin_lock_irqsave(&host_set->lock, flags); |
3483 | ||
1da177e4 LT |
3484 | switch (qc->tf.protocol) { |
3485 | ||
3486 | case ATA_PROT_DMA: | |
3487 | case ATA_PROT_ATAPI_DMA: | |
3488 | host_stat = ap->ops->bmdma_status(ap); | |
3489 | ||
3490 | /* before we do anything else, clear DMA-Start bit */ | |
b73fc89f | 3491 | ap->ops->bmdma_stop(qc); |
1da177e4 LT |
3492 | |
3493 | /* fall through */ | |
3494 | ||
3495 | default: | |
3496 | ata_altstatus(ap); | |
3497 | drv_stat = ata_chk_status(ap); | |
3498 | ||
3499 | /* ack bmdma irq events */ | |
3500 | ap->ops->irq_clear(ap); | |
3501 | ||
3502 | printk(KERN_ERR "ata%u: command 0x%x timeout, stat 0x%x host_stat 0x%x\n", | |
3503 | ap->id, qc->tf.command, drv_stat, host_stat); | |
3504 | ||
3505 | /* complete taskfile transaction */ | |
a22e2eb0 | 3506 | qc->err_mask |= ac_err_mask(drv_stat); |
1da177e4 LT |
3507 | break; |
3508 | } | |
b8f6153e JG |
3509 | |
3510 | spin_unlock_irqrestore(&host_set->lock, flags); | |
3511 | ||
a72ec4ce TH |
3512 | ata_eh_qc_complete(qc); |
3513 | ||
1da177e4 LT |
3514 | DPRINTK("EXIT\n"); |
3515 | } | |
3516 | ||
3517 | /** | |
3518 | * ata_eng_timeout - Handle timeout of queued command | |
3519 | * @ap: Port on which timed-out command is active | |
3520 | * | |
3521 | * Some part of the kernel (currently, only the SCSI layer) | |
3522 | * has noticed that the active command on port @ap has not | |
3523 | * completed after a specified length of time. Handle this | |
3524 | * condition by disabling DMA (if necessary) and completing | |
3525 | * transactions, with error if necessary. | |
3526 | * | |
3527 | * This also handles the case of the "lost interrupt", where | |
3528 | * for some reason (possibly hardware bug, possibly driver bug) | |
3529 | * an interrupt was not delivered to the driver, even though the | |
3530 | * transaction completed successfully. | |
3531 | * | |
3532 | * LOCKING: | |
3533 | * Inherited from SCSI layer (none, can sleep) | |
3534 | */ | |
3535 | ||
3536 | void ata_eng_timeout(struct ata_port *ap) | |
3537 | { | |
1da177e4 LT |
3538 | DPRINTK("ENTER\n"); |
3539 | ||
f6379020 | 3540 | ata_qc_timeout(ata_qc_from_tag(ap, ap->active_tag)); |
1da177e4 | 3541 | |
1da177e4 LT |
3542 | DPRINTK("EXIT\n"); |
3543 | } | |
3544 | ||
3545 | /** | |
3546 | * ata_qc_new - Request an available ATA command, for queueing | |
3547 | * @ap: Port associated with device @dev | |
3548 | * @dev: Device from whom we request an available command structure | |
3549 | * | |
3550 | * LOCKING: | |
0cba632b | 3551 | * None. |
1da177e4 LT |
3552 | */ |
3553 | ||
3554 | static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap) | |
3555 | { | |
3556 | struct ata_queued_cmd *qc = NULL; | |
3557 | unsigned int i; | |
3558 | ||
3559 | for (i = 0; i < ATA_MAX_QUEUE; i++) | |
3560 | if (!test_and_set_bit(i, &ap->qactive)) { | |
3561 | qc = ata_qc_from_tag(ap, i); | |
3562 | break; | |
3563 | } | |
3564 | ||
3565 | if (qc) | |
3566 | qc->tag = i; | |
3567 | ||
3568 | return qc; | |
3569 | } | |
3570 | ||
3571 | /** | |
3572 | * ata_qc_new_init - Request an available ATA command, and initialize it | |
3573 | * @ap: Port associated with device @dev | |
3574 | * @dev: Device from whom we request an available command structure | |
3575 | * | |
3576 | * LOCKING: | |
0cba632b | 3577 | * None. |
1da177e4 LT |
3578 | */ |
3579 | ||
3580 | struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap, | |
3581 | struct ata_device *dev) | |
3582 | { | |
3583 | struct ata_queued_cmd *qc; | |
3584 | ||
3585 | qc = ata_qc_new(ap); | |
3586 | if (qc) { | |
1da177e4 LT |
3587 | qc->scsicmd = NULL; |
3588 | qc->ap = ap; | |
3589 | qc->dev = dev; | |
1da177e4 | 3590 | |
2c13b7ce | 3591 | ata_qc_reinit(qc); |
1da177e4 LT |
3592 | } |
3593 | ||
3594 | return qc; | |
3595 | } | |
3596 | ||
1da177e4 LT |
3597 | /** |
3598 | * ata_qc_free - free unused ata_queued_cmd | |
3599 | * @qc: Command to complete | |
3600 | * | |
3601 | * Designed to free unused ata_queued_cmd object | |
3602 | * in case something prevents using it. | |
3603 | * | |
3604 | * LOCKING: | |
0cba632b | 3605 | * spin_lock_irqsave(host_set lock) |
1da177e4 LT |
3606 | */ |
3607 | void ata_qc_free(struct ata_queued_cmd *qc) | |
3608 | { | |
4ba946e9 TH |
3609 | struct ata_port *ap = qc->ap; |
3610 | unsigned int tag; | |
3611 | ||
1da177e4 | 3612 | assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */ |
1da177e4 | 3613 | |
4ba946e9 TH |
3614 | qc->flags = 0; |
3615 | tag = qc->tag; | |
3616 | if (likely(ata_tag_valid(tag))) { | |
3617 | if (tag == ap->active_tag) | |
3618 | ap->active_tag = ATA_TAG_POISON; | |
3619 | qc->tag = ATA_TAG_POISON; | |
3620 | clear_bit(tag, &ap->qactive); | |
3621 | } | |
1da177e4 LT |
3622 | } |
3623 | ||
341963b9 | 3624 | inline void __ata_qc_complete(struct ata_queued_cmd *qc) |
1da177e4 | 3625 | { |
1da177e4 LT |
3626 | assert(qc != NULL); /* ata_qc_from_tag _might_ return NULL */ |
3627 | assert(qc->flags & ATA_QCFLAG_ACTIVE); | |
3628 | ||
3629 | if (likely(qc->flags & ATA_QCFLAG_DMAMAP)) | |
3630 | ata_sg_clean(qc); | |
3631 | ||
3f3791d3 AL |
3632 | /* atapi: mark qc as inactive to prevent the interrupt handler |
3633 | * from completing the command twice later, before the error handler | |
3634 | * is called. (when rc != 0 and atapi request sense is needed) | |
3635 | */ | |
3636 | qc->flags &= ~ATA_QCFLAG_ACTIVE; | |
3637 | ||
1da177e4 | 3638 | /* call completion callback */ |
77853bf2 | 3639 | qc->complete_fn(qc); |
1da177e4 LT |
3640 | } |
3641 | ||
341963b9 TH |
3642 | /** |
3643 | * ata_qc_complete - Complete an active ATA command | |
3644 | * @qc: Command to complete | |
3645 | * @err_mask: ATA Status register contents | |
3646 | * | |
3647 | * Indicate to the mid and upper layers that an ATA | |
3648 | * command has completed, with either an ok or not-ok status. | |
3649 | * | |
3650 | * LOCKING: | |
3651 | * spin_lock_irqsave(host_set lock) | |
3652 | */ | |
3653 | void ata_qc_complete(struct ata_queued_cmd *qc) | |
3654 | { | |
3655 | if (unlikely(qc->flags & ATA_QCFLAG_EH_SCHEDULED)) | |
3656 | return; | |
3657 | ||
3658 | __ata_qc_complete(qc); | |
3659 | } | |
3660 | ||
1da177e4 LT |
3661 | static inline int ata_should_dma_map(struct ata_queued_cmd *qc) |
3662 | { | |
3663 | struct ata_port *ap = qc->ap; | |
3664 | ||
3665 | switch (qc->tf.protocol) { | |
3666 | case ATA_PROT_DMA: | |
3667 | case ATA_PROT_ATAPI_DMA: | |
3668 | return 1; | |
3669 | ||
3670 | case ATA_PROT_ATAPI: | |
3671 | case ATA_PROT_PIO: | |
3672 | case ATA_PROT_PIO_MULT: | |
3673 | if (ap->flags & ATA_FLAG_PIO_DMA) | |
3674 | return 1; | |
3675 | ||
3676 | /* fall through */ | |
3677 | ||
3678 | default: | |
3679 | return 0; | |
3680 | } | |
3681 | ||
3682 | /* never reached */ | |
3683 | } | |
3684 | ||
3685 | /** | |
3686 | * ata_qc_issue - issue taskfile to device | |
3687 | * @qc: command to issue to device | |
3688 | * | |
3689 | * Prepare an ATA command to submission to device. | |
3690 | * This includes mapping the data into a DMA-able | |
3691 | * area, filling in the S/G table, and finally | |
3692 | * writing the taskfile to hardware, starting the command. | |
3693 | * | |
3694 | * LOCKING: | |
3695 | * spin_lock_irqsave(host_set lock) | |
3696 | * | |
3697 | * RETURNS: | |
9a3d9eb0 | 3698 | * Zero on success, AC_ERR_* mask on failure |
1da177e4 LT |
3699 | */ |
3700 | ||
9a3d9eb0 | 3701 | unsigned int ata_qc_issue(struct ata_queued_cmd *qc) |
1da177e4 LT |
3702 | { |
3703 | struct ata_port *ap = qc->ap; | |
3704 | ||
3705 | if (ata_should_dma_map(qc)) { | |
3706 | if (qc->flags & ATA_QCFLAG_SG) { | |
3707 | if (ata_sg_setup(qc)) | |
8e436af9 | 3708 | goto sg_err; |
1da177e4 LT |
3709 | } else if (qc->flags & ATA_QCFLAG_SINGLE) { |
3710 | if (ata_sg_setup_one(qc)) | |
8e436af9 | 3711 | goto sg_err; |
1da177e4 LT |
3712 | } |
3713 | } else { | |
3714 | qc->flags &= ~ATA_QCFLAG_DMAMAP; | |
3715 | } | |
3716 | ||
3717 | ap->ops->qc_prep(qc); | |
3718 | ||
3719 | qc->ap->active_tag = qc->tag; | |
3720 | qc->flags |= ATA_QCFLAG_ACTIVE; | |
3721 | ||
3722 | return ap->ops->qc_issue(qc); | |
3723 | ||
8e436af9 TH |
3724 | sg_err: |
3725 | qc->flags &= ~ATA_QCFLAG_DMAMAP; | |
9a3d9eb0 | 3726 | return AC_ERR_SYSTEM; |
1da177e4 LT |
3727 | } |
3728 | ||
0baab86b | 3729 | |
1da177e4 LT |
3730 | /** |
3731 | * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner | |
3732 | * @qc: command to issue to device | |
3733 | * | |
3734 | * Using various libata functions and hooks, this function | |
3735 | * starts an ATA command. ATA commands are grouped into | |
3736 | * classes called "protocols", and issuing each type of protocol | |
3737 | * is slightly different. | |
3738 | * | |
0baab86b EF |
3739 | * May be used as the qc_issue() entry in ata_port_operations. |
3740 | * | |
1da177e4 LT |
3741 | * LOCKING: |
3742 | * spin_lock_irqsave(host_set lock) | |
3743 | * | |
3744 | * RETURNS: | |
9a3d9eb0 | 3745 | * Zero on success, AC_ERR_* mask on failure |
1da177e4 LT |
3746 | */ |
3747 | ||
9a3d9eb0 | 3748 | unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc) |
1da177e4 LT |
3749 | { |
3750 | struct ata_port *ap = qc->ap; | |
3751 | ||
3752 | ata_dev_select(ap, qc->dev->devno, 1, 0); | |
3753 | ||
3754 | switch (qc->tf.protocol) { | |
3755 | case ATA_PROT_NODATA: | |
e5338254 | 3756 | ata_tf_to_host(ap, &qc->tf); |
1da177e4 LT |
3757 | break; |
3758 | ||
3759 | case ATA_PROT_DMA: | |
3760 | ap->ops->tf_load(ap, &qc->tf); /* load tf registers */ | |
3761 | ap->ops->bmdma_setup(qc); /* set up bmdma */ | |
3762 | ap->ops->bmdma_start(qc); /* initiate bmdma */ | |
3763 | break; | |
3764 | ||
3765 | case ATA_PROT_PIO: /* load tf registers, initiate polling pio */ | |
3766 | ata_qc_set_polling(qc); | |
e5338254 | 3767 | ata_tf_to_host(ap, &qc->tf); |
14be71f4 | 3768 | ap->hsm_task_state = HSM_ST; |
95064379 | 3769 | ata_queue_pio_task(ap); |
1da177e4 LT |
3770 | break; |
3771 | ||
3772 | case ATA_PROT_ATAPI: | |
3773 | ata_qc_set_polling(qc); | |
e5338254 | 3774 | ata_tf_to_host(ap, &qc->tf); |
95064379 | 3775 | ata_queue_packet_task(ap); |
1da177e4 LT |
3776 | break; |
3777 | ||
3778 | case ATA_PROT_ATAPI_NODATA: | |
c1389503 | 3779 | ap->flags |= ATA_FLAG_NOINTR; |
e5338254 | 3780 | ata_tf_to_host(ap, &qc->tf); |
95064379 | 3781 | ata_queue_packet_task(ap); |
1da177e4 LT |
3782 | break; |
3783 | ||
3784 | case ATA_PROT_ATAPI_DMA: | |
c1389503 | 3785 | ap->flags |= ATA_FLAG_NOINTR; |
1da177e4 LT |
3786 | ap->ops->tf_load(ap, &qc->tf); /* load tf registers */ |
3787 | ap->ops->bmdma_setup(qc); /* set up bmdma */ | |
95064379 | 3788 | ata_queue_packet_task(ap); |
1da177e4 LT |
3789 | break; |
3790 | ||
3791 | default: | |
3792 | WARN_ON(1); | |
9a3d9eb0 | 3793 | return AC_ERR_SYSTEM; |
1da177e4 LT |
3794 | } |
3795 | ||
3796 | return 0; | |
3797 | } | |
3798 | ||
3799 | /** | |
0baab86b | 3800 | * ata_bmdma_setup_mmio - Set up PCI IDE BMDMA transaction |
1da177e4 LT |
3801 | * @qc: Info associated with this ATA transaction. |
3802 | * | |
3803 | * LOCKING: | |
3804 | * spin_lock_irqsave(host_set lock) | |
3805 | */ | |
3806 | ||
3807 | static void ata_bmdma_setup_mmio (struct ata_queued_cmd *qc) | |
3808 | { | |
3809 | struct ata_port *ap = qc->ap; | |
3810 | unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE); | |
3811 | u8 dmactl; | |
3812 | void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr; | |
3813 | ||
3814 | /* load PRD table addr. */ | |
3815 | mb(); /* make sure PRD table writes are visible to controller */ | |
3816 | writel(ap->prd_dma, mmio + ATA_DMA_TABLE_OFS); | |
3817 | ||
3818 | /* specify data direction, triple-check start bit is clear */ | |
3819 | dmactl = readb(mmio + ATA_DMA_CMD); | |
3820 | dmactl &= ~(ATA_DMA_WR | ATA_DMA_START); | |
3821 | if (!rw) | |
3822 | dmactl |= ATA_DMA_WR; | |
3823 | writeb(dmactl, mmio + ATA_DMA_CMD); | |
3824 | ||
3825 | /* issue r/w command */ | |
3826 | ap->ops->exec_command(ap, &qc->tf); | |
3827 | } | |
3828 | ||
3829 | /** | |
b73fc89f | 3830 | * ata_bmdma_start_mmio - Start a PCI IDE BMDMA transaction |
1da177e4 LT |
3831 | * @qc: Info associated with this ATA transaction. |
3832 | * | |
3833 | * LOCKING: | |
3834 | * spin_lock_irqsave(host_set lock) | |
3835 | */ | |
3836 | ||
3837 | static void ata_bmdma_start_mmio (struct ata_queued_cmd *qc) | |
3838 | { | |
3839 | struct ata_port *ap = qc->ap; | |
3840 | void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr; | |
3841 | u8 dmactl; | |
3842 | ||
3843 | /* start host DMA transaction */ | |
3844 | dmactl = readb(mmio + ATA_DMA_CMD); | |
3845 | writeb(dmactl | ATA_DMA_START, mmio + ATA_DMA_CMD); | |
3846 | ||
3847 | /* Strictly, one may wish to issue a readb() here, to | |
3848 | * flush the mmio write. However, control also passes | |
3849 | * to the hardware at this point, and it will interrupt | |
3850 | * us when we are to resume control. So, in effect, | |
3851 | * we don't care when the mmio write flushes. | |
3852 | * Further, a read of the DMA status register _immediately_ | |
3853 | * following the write may not be what certain flaky hardware | |
3854 | * is expected, so I think it is best to not add a readb() | |
3855 | * without first all the MMIO ATA cards/mobos. | |
3856 | * Or maybe I'm just being paranoid. | |
3857 | */ | |
3858 | } | |
3859 | ||
3860 | /** | |
3861 | * ata_bmdma_setup_pio - Set up PCI IDE BMDMA transaction (PIO) | |
3862 | * @qc: Info associated with this ATA transaction. | |
3863 | * | |
3864 | * LOCKING: | |
3865 | * spin_lock_irqsave(host_set lock) | |
3866 | */ | |
3867 | ||
3868 | static void ata_bmdma_setup_pio (struct ata_queued_cmd *qc) | |
3869 | { | |
3870 | struct ata_port *ap = qc->ap; | |
3871 | unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE); | |
3872 | u8 dmactl; | |
3873 | ||
3874 | /* load PRD table addr. */ | |
3875 | outl(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS); | |
3876 | ||
3877 | /* specify data direction, triple-check start bit is clear */ | |
3878 | dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD); | |
3879 | dmactl &= ~(ATA_DMA_WR | ATA_DMA_START); | |
3880 | if (!rw) | |
3881 | dmactl |= ATA_DMA_WR; | |
3882 | outb(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD); | |
3883 | ||
3884 | /* issue r/w command */ | |
3885 | ap->ops->exec_command(ap, &qc->tf); | |
3886 | } | |
3887 | ||
3888 | /** | |
3889 | * ata_bmdma_start_pio - Start a PCI IDE BMDMA transaction (PIO) | |
3890 | * @qc: Info associated with this ATA transaction. | |
3891 | * | |
3892 | * LOCKING: | |
3893 | * spin_lock_irqsave(host_set lock) | |
3894 | */ | |
3895 | ||
3896 | static void ata_bmdma_start_pio (struct ata_queued_cmd *qc) | |
3897 | { | |
3898 | struct ata_port *ap = qc->ap; | |
3899 | u8 dmactl; | |
3900 | ||
3901 | /* start host DMA transaction */ | |
3902 | dmactl = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD); | |
3903 | outb(dmactl | ATA_DMA_START, | |
3904 | ap->ioaddr.bmdma_addr + ATA_DMA_CMD); | |
3905 | } | |
3906 | ||
0baab86b EF |
3907 | |
3908 | /** | |
3909 | * ata_bmdma_start - Start a PCI IDE BMDMA transaction | |
3910 | * @qc: Info associated with this ATA transaction. | |
3911 | * | |
3912 | * Writes the ATA_DMA_START flag to the DMA command register. | |
3913 | * | |
3914 | * May be used as the bmdma_start() entry in ata_port_operations. | |
3915 | * | |
3916 | * LOCKING: | |
3917 | * spin_lock_irqsave(host_set lock) | |
3918 | */ | |
1da177e4 LT |
3919 | void ata_bmdma_start(struct ata_queued_cmd *qc) |
3920 | { | |
3921 | if (qc->ap->flags & ATA_FLAG_MMIO) | |
3922 | ata_bmdma_start_mmio(qc); | |
3923 | else | |
3924 | ata_bmdma_start_pio(qc); | |
3925 | } | |
3926 | ||
0baab86b EF |
3927 | |
3928 | /** | |
3929 | * ata_bmdma_setup - Set up PCI IDE BMDMA transaction | |
3930 | * @qc: Info associated with this ATA transaction. | |
3931 | * | |
3932 | * Writes address of PRD table to device's PRD Table Address | |
3933 | * register, sets the DMA control register, and calls | |
3934 | * ops->exec_command() to start the transfer. | |
3935 | * | |
3936 | * May be used as the bmdma_setup() entry in ata_port_operations. | |
3937 | * | |
3938 | * LOCKING: | |
3939 | * spin_lock_irqsave(host_set lock) | |
3940 | */ | |
1da177e4 LT |
3941 | void ata_bmdma_setup(struct ata_queued_cmd *qc) |
3942 | { | |
3943 | if (qc->ap->flags & ATA_FLAG_MMIO) | |
3944 | ata_bmdma_setup_mmio(qc); | |
3945 | else | |
3946 | ata_bmdma_setup_pio(qc); | |
3947 | } | |
3948 | ||
0baab86b EF |
3949 | |
3950 | /** | |
3951 | * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt. | |
decc6d0b | 3952 | * @ap: Port associated with this ATA transaction. |
0baab86b EF |
3953 | * |
3954 | * Clear interrupt and error flags in DMA status register. | |
3955 | * | |
3956 | * May be used as the irq_clear() entry in ata_port_operations. | |
3957 | * | |
3958 | * LOCKING: | |
3959 | * spin_lock_irqsave(host_set lock) | |
3960 | */ | |
3961 | ||
1da177e4 LT |
3962 | void ata_bmdma_irq_clear(struct ata_port *ap) |
3963 | { | |
3964 | if (ap->flags & ATA_FLAG_MMIO) { | |
3965 | void __iomem *mmio = ((void __iomem *) ap->ioaddr.bmdma_addr) + ATA_DMA_STATUS; | |
3966 | writeb(readb(mmio), mmio); | |
3967 | } else { | |
3968 | unsigned long addr = ap->ioaddr.bmdma_addr + ATA_DMA_STATUS; | |
3969 | outb(inb(addr), addr); | |
3970 | } | |
3971 | ||
3972 | } | |
3973 | ||
0baab86b EF |
3974 | |
3975 | /** | |
3976 | * ata_bmdma_status - Read PCI IDE BMDMA status | |
decc6d0b | 3977 | * @ap: Port associated with this ATA transaction. |
0baab86b EF |
3978 | * |
3979 | * Read and return BMDMA status register. | |
3980 | * | |
3981 | * May be used as the bmdma_status() entry in ata_port_operations. | |
3982 | * | |
3983 | * LOCKING: | |
3984 | * spin_lock_irqsave(host_set lock) | |
3985 | */ | |
3986 | ||
1da177e4 LT |
3987 | u8 ata_bmdma_status(struct ata_port *ap) |
3988 | { | |
3989 | u8 host_stat; | |
3990 | if (ap->flags & ATA_FLAG_MMIO) { | |
3991 | void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr; | |
3992 | host_stat = readb(mmio + ATA_DMA_STATUS); | |
3993 | } else | |
ee500aab | 3994 | host_stat = inb(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS); |
1da177e4 LT |
3995 | return host_stat; |
3996 | } | |
3997 | ||
0baab86b EF |
3998 | |
3999 | /** | |
4000 | * ata_bmdma_stop - Stop PCI IDE BMDMA transfer | |
b73fc89f | 4001 | * @qc: Command we are ending DMA for |
0baab86b EF |
4002 | * |
4003 | * Clears the ATA_DMA_START flag in the dma control register | |
4004 | * | |
4005 | * May be used as the bmdma_stop() entry in ata_port_operations. | |
4006 | * | |
4007 | * LOCKING: | |
4008 | * spin_lock_irqsave(host_set lock) | |
4009 | */ | |
4010 | ||
b73fc89f | 4011 | void ata_bmdma_stop(struct ata_queued_cmd *qc) |
1da177e4 | 4012 | { |
b73fc89f | 4013 | struct ata_port *ap = qc->ap; |
1da177e4 LT |
4014 | if (ap->flags & ATA_FLAG_MMIO) { |
4015 | void __iomem *mmio = (void __iomem *) ap->ioaddr.bmdma_addr; | |
4016 | ||
4017 | /* clear start/stop bit */ | |
4018 | writeb(readb(mmio + ATA_DMA_CMD) & ~ATA_DMA_START, | |
4019 | mmio + ATA_DMA_CMD); | |
4020 | } else { | |
4021 | /* clear start/stop bit */ | |
4022 | outb(inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD) & ~ATA_DMA_START, | |
4023 | ap->ioaddr.bmdma_addr + ATA_DMA_CMD); | |
4024 | } | |
4025 | ||
4026 | /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */ | |
4027 | ata_altstatus(ap); /* dummy read */ | |
4028 | } | |
4029 | ||
4030 | /** | |
4031 | * ata_host_intr - Handle host interrupt for given (port, task) | |
4032 | * @ap: Port on which interrupt arrived (possibly...) | |
4033 | * @qc: Taskfile currently active in engine | |
4034 | * | |
4035 | * Handle host interrupt for given queued command. Currently, | |
4036 | * only DMA interrupts are handled. All other commands are | |
4037 | * handled via polling with interrupts disabled (nIEN bit). | |
4038 | * | |
4039 | * LOCKING: | |
4040 | * spin_lock_irqsave(host_set lock) | |
4041 | * | |
4042 | * RETURNS: | |
4043 | * One if interrupt was handled, zero if not (shared irq). | |
4044 | */ | |
4045 | ||
4046 | inline unsigned int ata_host_intr (struct ata_port *ap, | |
4047 | struct ata_queued_cmd *qc) | |
4048 | { | |
4049 | u8 status, host_stat; | |
4050 | ||
4051 | switch (qc->tf.protocol) { | |
4052 | ||
4053 | case ATA_PROT_DMA: | |
4054 | case ATA_PROT_ATAPI_DMA: | |
4055 | case ATA_PROT_ATAPI: | |
4056 | /* check status of DMA engine */ | |
4057 | host_stat = ap->ops->bmdma_status(ap); | |
4058 | VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat); | |
4059 | ||
4060 | /* if it's not our irq... */ | |
4061 | if (!(host_stat & ATA_DMA_INTR)) | |
4062 | goto idle_irq; | |
4063 | ||
4064 | /* before we do anything else, clear DMA-Start bit */ | |
b73fc89f | 4065 | ap->ops->bmdma_stop(qc); |
1da177e4 LT |
4066 | |
4067 | /* fall through */ | |
4068 | ||
4069 | case ATA_PROT_ATAPI_NODATA: | |
4070 | case ATA_PROT_NODATA: | |
4071 | /* check altstatus */ | |
4072 | status = ata_altstatus(ap); | |
4073 | if (status & ATA_BUSY) | |
4074 | goto idle_irq; | |
4075 | ||
4076 | /* check main status, clearing INTRQ */ | |
4077 | status = ata_chk_status(ap); | |
4078 | if (unlikely(status & ATA_BUSY)) | |
4079 | goto idle_irq; | |
4080 | DPRINTK("ata%u: protocol %d (dev_stat 0x%X)\n", | |
4081 | ap->id, qc->tf.protocol, status); | |
4082 | ||
4083 | /* ack bmdma irq events */ | |
4084 | ap->ops->irq_clear(ap); | |
4085 | ||
4086 | /* complete taskfile transaction */ | |
a22e2eb0 AL |
4087 | qc->err_mask |= ac_err_mask(status); |
4088 | ata_qc_complete(qc); | |
1da177e4 LT |
4089 | break; |
4090 | ||
4091 | default: | |
4092 | goto idle_irq; | |
4093 | } | |
4094 | ||
4095 | return 1; /* irq handled */ | |
4096 | ||
4097 | idle_irq: | |
4098 | ap->stats.idle_irq++; | |
4099 | ||
4100 | #ifdef ATA_IRQ_TRAP | |
4101 | if ((ap->stats.idle_irq % 1000) == 0) { | |
4102 | handled = 1; | |
4103 | ata_irq_ack(ap, 0); /* debug trap */ | |
4104 | printk(KERN_WARNING "ata%d: irq trap\n", ap->id); | |
4105 | } | |
4106 | #endif | |
4107 | return 0; /* irq not handled */ | |
4108 | } | |
4109 | ||
4110 | /** | |
4111 | * ata_interrupt - Default ATA host interrupt handler | |
0cba632b JG |
4112 | * @irq: irq line (unused) |
4113 | * @dev_instance: pointer to our ata_host_set information structure | |
1da177e4 LT |
4114 | * @regs: unused |
4115 | * | |
0cba632b JG |
4116 | * Default interrupt handler for PCI IDE devices. Calls |
4117 | * ata_host_intr() for each port that is not disabled. | |
4118 | * | |
1da177e4 | 4119 | * LOCKING: |
0cba632b | 4120 | * Obtains host_set lock during operation. |
1da177e4 LT |
4121 | * |
4122 | * RETURNS: | |
0cba632b | 4123 | * IRQ_NONE or IRQ_HANDLED. |
1da177e4 LT |
4124 | */ |
4125 | ||
4126 | irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs) | |
4127 | { | |
4128 | struct ata_host_set *host_set = dev_instance; | |
4129 | unsigned int i; | |
4130 | unsigned int handled = 0; | |
4131 | unsigned long flags; | |
4132 | ||
4133 | /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */ | |
4134 | spin_lock_irqsave(&host_set->lock, flags); | |
4135 | ||
4136 | for (i = 0; i < host_set->n_ports; i++) { | |
4137 | struct ata_port *ap; | |
4138 | ||
4139 | ap = host_set->ports[i]; | |
c1389503 TH |
4140 | if (ap && |
4141 | !(ap->flags & (ATA_FLAG_PORT_DISABLED | ATA_FLAG_NOINTR))) { | |
1da177e4 LT |
4142 | struct ata_queued_cmd *qc; |
4143 | ||
4144 | qc = ata_qc_from_tag(ap, ap->active_tag); | |
21b1ed74 AL |
4145 | if (qc && (!(qc->tf.ctl & ATA_NIEN)) && |
4146 | (qc->flags & ATA_QCFLAG_ACTIVE)) | |
1da177e4 LT |
4147 | handled |= ata_host_intr(ap, qc); |
4148 | } | |
4149 | } | |
4150 | ||
4151 | spin_unlock_irqrestore(&host_set->lock, flags); | |
4152 | ||
4153 | return IRQ_RETVAL(handled); | |
4154 | } | |
4155 | ||
4156 | /** | |
4157 | * atapi_packet_task - Write CDB bytes to hardware | |
4158 | * @_data: Port to which ATAPI device is attached. | |
4159 | * | |
4160 | * When device has indicated its readiness to accept | |
4161 | * a CDB, this function is called. Send the CDB. | |
4162 | * If DMA is to be performed, exit immediately. | |
4163 | * Otherwise, we are in polling mode, so poll | |
4164 | * status under operation succeeds or fails. | |
4165 | * | |
4166 | * LOCKING: | |
4167 | * Kernel thread context (may sleep) | |
4168 | */ | |
4169 | ||
4170 | static void atapi_packet_task(void *_data) | |
4171 | { | |
4172 | struct ata_port *ap = _data; | |
4173 | struct ata_queued_cmd *qc; | |
4174 | u8 status; | |
4175 | ||
4176 | qc = ata_qc_from_tag(ap, ap->active_tag); | |
4177 | assert(qc != NULL); | |
4178 | assert(qc->flags & ATA_QCFLAG_ACTIVE); | |
4179 | ||
4180 | /* sleep-wait for BSY to clear */ | |
4181 | DPRINTK("busy wait\n"); | |
d8fe452b | 4182 | if (ata_busy_sleep(ap, ATA_TMOUT_CDB_QUICK, ATA_TMOUT_CDB)) { |
11a56d24 | 4183 | qc->err_mask |= AC_ERR_TIMEOUT; |
d8fe452b AL |
4184 | goto err_out; |
4185 | } | |
1da177e4 LT |
4186 | |
4187 | /* make sure DRQ is set */ | |
4188 | status = ata_chk_status(ap); | |
d8fe452b | 4189 | if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ) { |
11a56d24 | 4190 | qc->err_mask |= AC_ERR_HSM; |
1da177e4 | 4191 | goto err_out; |
d8fe452b | 4192 | } |
1da177e4 LT |
4193 | |
4194 | /* send SCSI cdb */ | |
4195 | DPRINTK("send cdb\n"); | |
4196 | assert(ap->cdb_len >= 12); | |
1da177e4 | 4197 | |
c1389503 TH |
4198 | if (qc->tf.protocol == ATA_PROT_ATAPI_DMA || |
4199 | qc->tf.protocol == ATA_PROT_ATAPI_NODATA) { | |
4200 | unsigned long flags; | |
1da177e4 | 4201 | |
c1389503 TH |
4202 | /* Once we're done issuing command and kicking bmdma, |
4203 | * irq handler takes over. To not lose irq, we need | |
4204 | * to clear NOINTR flag before sending cdb, but | |
4205 | * interrupt handler shouldn't be invoked before we're | |
4206 | * finished. Hence, the following locking. | |
4207 | */ | |
4208 | spin_lock_irqsave(&ap->host_set->lock, flags); | |
4209 | ap->flags &= ~ATA_FLAG_NOINTR; | |
4210 | ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1); | |
4211 | if (qc->tf.protocol == ATA_PROT_ATAPI_DMA) | |
4212 | ap->ops->bmdma_start(qc); /* initiate bmdma */ | |
4213 | spin_unlock_irqrestore(&ap->host_set->lock, flags); | |
4214 | } else { | |
4215 | ata_data_xfer(ap, qc->cdb, ap->cdb_len, 1); | |
1da177e4 | 4216 | |
c1389503 | 4217 | /* PIO commands are handled by polling */ |
14be71f4 | 4218 | ap->hsm_task_state = HSM_ST; |
95064379 | 4219 | ata_queue_pio_task(ap); |
1da177e4 LT |
4220 | } |
4221 | ||
4222 | return; | |
4223 | ||
4224 | err_out: | |
a22e2eb0 | 4225 | ata_poll_qc_complete(qc); |
1da177e4 LT |
4226 | } |
4227 | ||
0baab86b | 4228 | |
9b847548 JA |
4229 | /* |
4230 | * Execute a 'simple' command, that only consists of the opcode 'cmd' itself, | |
4231 | * without filling any other registers | |
4232 | */ | |
4233 | static int ata_do_simple_cmd(struct ata_port *ap, struct ata_device *dev, | |
4234 | u8 cmd) | |
4235 | { | |
4236 | struct ata_taskfile tf; | |
4237 | int err; | |
4238 | ||
4239 | ata_tf_init(ap, &tf, dev->devno); | |
4240 | ||
4241 | tf.command = cmd; | |
4242 | tf.flags |= ATA_TFLAG_DEVICE; | |
4243 | tf.protocol = ATA_PROT_NODATA; | |
4244 | ||
4245 | err = ata_exec_internal(ap, dev, &tf, DMA_NONE, NULL, 0); | |
4246 | if (err) | |
4247 | printk(KERN_ERR "%s: ata command failed: %d\n", | |
4248 | __FUNCTION__, err); | |
4249 | ||
4250 | return err; | |
4251 | } | |
4252 | ||
4253 | static int ata_flush_cache(struct ata_port *ap, struct ata_device *dev) | |
4254 | { | |
4255 | u8 cmd; | |
4256 | ||
4257 | if (!ata_try_flush_cache(dev)) | |
4258 | return 0; | |
4259 | ||
4260 | if (ata_id_has_flush_ext(dev->id)) | |
4261 | cmd = ATA_CMD_FLUSH_EXT; | |
4262 | else | |
4263 | cmd = ATA_CMD_FLUSH; | |
4264 | ||
4265 | return ata_do_simple_cmd(ap, dev, cmd); | |
4266 | } | |
4267 | ||
4268 | static int ata_standby_drive(struct ata_port *ap, struct ata_device *dev) | |
4269 | { | |
4270 | return ata_do_simple_cmd(ap, dev, ATA_CMD_STANDBYNOW1); | |
4271 | } | |
4272 | ||
4273 | static int ata_start_drive(struct ata_port *ap, struct ata_device *dev) | |
4274 | { | |
4275 | return ata_do_simple_cmd(ap, dev, ATA_CMD_IDLEIMMEDIATE); | |
4276 | } | |
4277 | ||
4278 | /** | |
4279 | * ata_device_resume - wakeup a previously suspended devices | |
c893a3ae RD |
4280 | * @ap: port the device is connected to |
4281 | * @dev: the device to resume | |
9b847548 JA |
4282 | * |
4283 | * Kick the drive back into action, by sending it an idle immediate | |
4284 | * command and making sure its transfer mode matches between drive | |
4285 | * and host. | |
4286 | * | |
4287 | */ | |
4288 | int ata_device_resume(struct ata_port *ap, struct ata_device *dev) | |
4289 | { | |
4290 | if (ap->flags & ATA_FLAG_SUSPENDED) { | |
4291 | ap->flags &= ~ATA_FLAG_SUSPENDED; | |
4292 | ata_set_mode(ap); | |
4293 | } | |
4294 | if (!ata_dev_present(dev)) | |
4295 | return 0; | |
4296 | if (dev->class == ATA_DEV_ATA) | |
4297 | ata_start_drive(ap, dev); | |
4298 | ||
4299 | return 0; | |
4300 | } | |
4301 | ||
4302 | /** | |
4303 | * ata_device_suspend - prepare a device for suspend | |
c893a3ae RD |
4304 | * @ap: port the device is connected to |
4305 | * @dev: the device to suspend | |
9b847548 JA |
4306 | * |
4307 | * Flush the cache on the drive, if appropriate, then issue a | |
4308 | * standbynow command. | |
9b847548 JA |
4309 | */ |
4310 | int ata_device_suspend(struct ata_port *ap, struct ata_device *dev) | |
4311 | { | |
4312 | if (!ata_dev_present(dev)) | |
4313 | return 0; | |
4314 | if (dev->class == ATA_DEV_ATA) | |
4315 | ata_flush_cache(ap, dev); | |
4316 | ||
4317 | ata_standby_drive(ap, dev); | |
4318 | ap->flags |= ATA_FLAG_SUSPENDED; | |
4319 | return 0; | |
4320 | } | |
4321 | ||
c893a3ae RD |
4322 | /** |
4323 | * ata_port_start - Set port up for dma. | |
4324 | * @ap: Port to initialize | |
4325 | * | |
4326 | * Called just after data structures for each port are | |
4327 | * initialized. Allocates space for PRD table. | |
4328 | * | |
4329 | * May be used as the port_start() entry in ata_port_operations. | |
4330 | * | |
4331 | * LOCKING: | |
4332 | * Inherited from caller. | |
4333 | */ | |
4334 | ||
1da177e4 LT |
4335 | int ata_port_start (struct ata_port *ap) |
4336 | { | |
4337 | struct device *dev = ap->host_set->dev; | |
6037d6bb | 4338 | int rc; |
1da177e4 LT |
4339 | |
4340 | ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL); | |
4341 | if (!ap->prd) | |
4342 | return -ENOMEM; | |
4343 | ||
6037d6bb JG |
4344 | rc = ata_pad_alloc(ap, dev); |
4345 | if (rc) { | |
cedc9a47 | 4346 | dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma); |
6037d6bb | 4347 | return rc; |
cedc9a47 JG |
4348 | } |
4349 | ||
1da177e4 LT |
4350 | DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma); |
4351 | ||
4352 | return 0; | |
4353 | } | |
4354 | ||
0baab86b EF |
4355 | |
4356 | /** | |
4357 | * ata_port_stop - Undo ata_port_start() | |
4358 | * @ap: Port to shut down | |
4359 | * | |
4360 | * Frees the PRD table. | |
4361 | * | |
4362 | * May be used as the port_stop() entry in ata_port_operations. | |
4363 | * | |
4364 | * LOCKING: | |
6f0ef4fa | 4365 | * Inherited from caller. |
0baab86b EF |
4366 | */ |
4367 | ||
1da177e4 LT |
4368 | void ata_port_stop (struct ata_port *ap) |
4369 | { | |
4370 | struct device *dev = ap->host_set->dev; | |
4371 | ||
4372 | dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma); | |
6037d6bb | 4373 | ata_pad_free(ap, dev); |
1da177e4 LT |
4374 | } |
4375 | ||
aa8f0dc6 JG |
4376 | void ata_host_stop (struct ata_host_set *host_set) |
4377 | { | |
4378 | if (host_set->mmio_base) | |
4379 | iounmap(host_set->mmio_base); | |
4380 | } | |
4381 | ||
4382 | ||
1da177e4 LT |
4383 | /** |
4384 | * ata_host_remove - Unregister SCSI host structure with upper layers | |
4385 | * @ap: Port to unregister | |
4386 | * @do_unregister: 1 if we fully unregister, 0 to just stop the port | |
4387 | * | |
4388 | * LOCKING: | |
6f0ef4fa | 4389 | * Inherited from caller. |
1da177e4 LT |
4390 | */ |
4391 | ||
4392 | static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister) | |
4393 | { | |
4394 | struct Scsi_Host *sh = ap->host; | |
4395 | ||
4396 | DPRINTK("ENTER\n"); | |
4397 | ||
4398 | if (do_unregister) | |
4399 | scsi_remove_host(sh); | |
4400 | ||
4401 | ap->ops->port_stop(ap); | |
4402 | } | |
4403 | ||
4404 | /** | |
4405 | * ata_host_init - Initialize an ata_port structure | |
4406 | * @ap: Structure to initialize | |
4407 | * @host: associated SCSI mid-layer structure | |
4408 | * @host_set: Collection of hosts to which @ap belongs | |
4409 | * @ent: Probe information provided by low-level driver | |
4410 | * @port_no: Port number associated with this ata_port | |
4411 | * | |
0cba632b JG |
4412 | * Initialize a new ata_port structure, and its associated |
4413 | * scsi_host. | |
4414 | * | |
1da177e4 | 4415 | * LOCKING: |
0cba632b | 4416 | * Inherited from caller. |
1da177e4 LT |
4417 | */ |
4418 | ||
4419 | static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host, | |
4420 | struct ata_host_set *host_set, | |
057ace5e | 4421 | const struct ata_probe_ent *ent, unsigned int port_no) |
1da177e4 LT |
4422 | { |
4423 | unsigned int i; | |
4424 | ||
4425 | host->max_id = 16; | |
4426 | host->max_lun = 1; | |
4427 | host->max_channel = 1; | |
4428 | host->unique_id = ata_unique_id++; | |
4429 | host->max_cmd_len = 12; | |
12413197 | 4430 | |
1da177e4 LT |
4431 | ap->flags = ATA_FLAG_PORT_DISABLED; |
4432 | ap->id = host->unique_id; | |
4433 | ap->host = host; | |
4434 | ap->ctl = ATA_DEVCTL_OBS; | |
4435 | ap->host_set = host_set; | |
4436 | ap->port_no = port_no; | |
4437 | ap->hard_port_no = | |
4438 | ent->legacy_mode ? ent->hard_port_no : port_no; | |
4439 | ap->pio_mask = ent->pio_mask; | |
4440 | ap->mwdma_mask = ent->mwdma_mask; | |
4441 | ap->udma_mask = ent->udma_mask; | |
4442 | ap->flags |= ent->host_flags; | |
4443 | ap->ops = ent->port_ops; | |
4444 | ap->cbl = ATA_CBL_NONE; | |
4445 | ap->active_tag = ATA_TAG_POISON; | |
4446 | ap->last_ctl = 0xFF; | |
4447 | ||
4448 | INIT_WORK(&ap->packet_task, atapi_packet_task, ap); | |
4449 | INIT_WORK(&ap->pio_task, ata_pio_task, ap); | |
a72ec4ce | 4450 | INIT_LIST_HEAD(&ap->eh_done_q); |
1da177e4 LT |
4451 | |
4452 | for (i = 0; i < ATA_MAX_DEVICES; i++) | |
4453 | ap->device[i].devno = i; | |
4454 | ||
4455 | #ifdef ATA_IRQ_TRAP | |
4456 | ap->stats.unhandled_irq = 1; | |
4457 | ap->stats.idle_irq = 1; | |
4458 | #endif | |
4459 | ||
4460 | memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports)); | |
4461 | } | |
4462 | ||
4463 | /** | |
4464 | * ata_host_add - Attach low-level ATA driver to system | |
4465 | * @ent: Information provided by low-level driver | |
4466 | * @host_set: Collections of ports to which we add | |
4467 | * @port_no: Port number associated with this host | |
4468 | * | |
0cba632b JG |
4469 | * Attach low-level ATA driver to system. |
4470 | * | |
1da177e4 | 4471 | * LOCKING: |
0cba632b | 4472 | * PCI/etc. bus probe sem. |
1da177e4 LT |
4473 | * |
4474 | * RETURNS: | |
0cba632b | 4475 | * New ata_port on success, for NULL on error. |
1da177e4 LT |
4476 | */ |
4477 | ||
057ace5e | 4478 | static struct ata_port * ata_host_add(const struct ata_probe_ent *ent, |
1da177e4 LT |
4479 | struct ata_host_set *host_set, |
4480 | unsigned int port_no) | |
4481 | { | |
4482 | struct Scsi_Host *host; | |
4483 | struct ata_port *ap; | |
4484 | int rc; | |
4485 | ||
4486 | DPRINTK("ENTER\n"); | |
4487 | host = scsi_host_alloc(ent->sht, sizeof(struct ata_port)); | |
4488 | if (!host) | |
4489 | return NULL; | |
4490 | ||
4491 | ap = (struct ata_port *) &host->hostdata[0]; | |
4492 | ||
4493 | ata_host_init(ap, host, host_set, ent, port_no); | |
4494 | ||
4495 | rc = ap->ops->port_start(ap); | |
4496 | if (rc) | |
4497 | goto err_out; | |
4498 | ||
4499 | return ap; | |
4500 | ||
4501 | err_out: | |
4502 | scsi_host_put(host); | |
4503 | return NULL; | |
4504 | } | |
4505 | ||
4506 | /** | |
0cba632b JG |
4507 | * ata_device_add - Register hardware device with ATA and SCSI layers |
4508 | * @ent: Probe information describing hardware device to be registered | |
4509 | * | |
4510 | * This function processes the information provided in the probe | |
4511 | * information struct @ent, allocates the necessary ATA and SCSI | |
4512 | * host information structures, initializes them, and registers | |
4513 | * everything with requisite kernel subsystems. | |
4514 | * | |
4515 | * This function requests irqs, probes the ATA bus, and probes | |
4516 | * the SCSI bus. | |
1da177e4 LT |
4517 | * |
4518 | * LOCKING: | |
0cba632b | 4519 | * PCI/etc. bus probe sem. |
1da177e4 LT |
4520 | * |
4521 | * RETURNS: | |
0cba632b | 4522 | * Number of ports registered. Zero on error (no ports registered). |
1da177e4 LT |
4523 | */ |
4524 | ||
057ace5e | 4525 | int ata_device_add(const struct ata_probe_ent *ent) |
1da177e4 LT |
4526 | { |
4527 | unsigned int count = 0, i; | |
4528 | struct device *dev = ent->dev; | |
4529 | struct ata_host_set *host_set; | |
4530 | ||
4531 | DPRINTK("ENTER\n"); | |
4532 | /* alloc a container for our list of ATA ports (buses) */ | |
57f3bda8 | 4533 | host_set = kzalloc(sizeof(struct ata_host_set) + |
1da177e4 LT |
4534 | (ent->n_ports * sizeof(void *)), GFP_KERNEL); |
4535 | if (!host_set) | |
4536 | return 0; | |
1da177e4 LT |
4537 | spin_lock_init(&host_set->lock); |
4538 | ||
4539 | host_set->dev = dev; | |
4540 | host_set->n_ports = ent->n_ports; | |
4541 | host_set->irq = ent->irq; | |
4542 | host_set->mmio_base = ent->mmio_base; | |
4543 | host_set->private_data = ent->private_data; | |
4544 | host_set->ops = ent->port_ops; | |
4545 | ||
4546 | /* register each port bound to this device */ | |
4547 | for (i = 0; i < ent->n_ports; i++) { | |
4548 | struct ata_port *ap; | |
4549 | unsigned long xfer_mode_mask; | |
4550 | ||
4551 | ap = ata_host_add(ent, host_set, i); | |
4552 | if (!ap) | |
4553 | goto err_out; | |
4554 | ||
4555 | host_set->ports[i] = ap; | |
4556 | xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) | | |
4557 | (ap->mwdma_mask << ATA_SHIFT_MWDMA) | | |
4558 | (ap->pio_mask << ATA_SHIFT_PIO); | |
4559 | ||
4560 | /* print per-port info to dmesg */ | |
4561 | printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX " | |
4562 | "bmdma 0x%lX irq %lu\n", | |
4563 | ap->id, | |
4564 | ap->flags & ATA_FLAG_SATA ? 'S' : 'P', | |
4565 | ata_mode_string(xfer_mode_mask), | |
4566 | ap->ioaddr.cmd_addr, | |
4567 | ap->ioaddr.ctl_addr, | |
4568 | ap->ioaddr.bmdma_addr, | |
4569 | ent->irq); | |
4570 | ||
4571 | ata_chk_status(ap); | |
4572 | host_set->ops->irq_clear(ap); | |
4573 | count++; | |
4574 | } | |
4575 | ||
57f3bda8 RD |
4576 | if (!count) |
4577 | goto err_free_ret; | |
1da177e4 LT |
4578 | |
4579 | /* obtain irq, that is shared between channels */ | |
4580 | if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags, | |
4581 | DRV_NAME, host_set)) | |
4582 | goto err_out; | |
4583 | ||
4584 | /* perform each probe synchronously */ | |
4585 | DPRINTK("probe begin\n"); | |
4586 | for (i = 0; i < count; i++) { | |
4587 | struct ata_port *ap; | |
4588 | int rc; | |
4589 | ||
4590 | ap = host_set->ports[i]; | |
4591 | ||
c893a3ae | 4592 | DPRINTK("ata%u: bus probe begin\n", ap->id); |
1da177e4 | 4593 | rc = ata_bus_probe(ap); |
c893a3ae | 4594 | DPRINTK("ata%u: bus probe end\n", ap->id); |
1da177e4 LT |
4595 | |
4596 | if (rc) { | |
4597 | /* FIXME: do something useful here? | |
4598 | * Current libata behavior will | |
4599 | * tear down everything when | |
4600 | * the module is removed | |
4601 | * or the h/w is unplugged. | |
4602 | */ | |
4603 | } | |
4604 | ||
4605 | rc = scsi_add_host(ap->host, dev); | |
4606 | if (rc) { | |
4607 | printk(KERN_ERR "ata%u: scsi_add_host failed\n", | |
4608 | ap->id); | |
4609 | /* FIXME: do something useful here */ | |
4610 | /* FIXME: handle unconditional calls to | |
4611 | * scsi_scan_host and ata_host_remove, below, | |
4612 | * at the very least | |
4613 | */ | |
4614 | } | |
4615 | } | |
4616 | ||
4617 | /* probes are done, now scan each port's disk(s) */ | |
c893a3ae | 4618 | DPRINTK("host probe begin\n"); |
1da177e4 LT |
4619 | for (i = 0; i < count; i++) { |
4620 | struct ata_port *ap = host_set->ports[i]; | |
4621 | ||
644dd0cc | 4622 | ata_scsi_scan_host(ap); |
1da177e4 LT |
4623 | } |
4624 | ||
4625 | dev_set_drvdata(dev, host_set); | |
4626 | ||
4627 | VPRINTK("EXIT, returning %u\n", ent->n_ports); | |
4628 | return ent->n_ports; /* success */ | |
4629 | ||
4630 | err_out: | |
4631 | for (i = 0; i < count; i++) { | |
4632 | ata_host_remove(host_set->ports[i], 1); | |
4633 | scsi_host_put(host_set->ports[i]->host); | |
4634 | } | |
57f3bda8 | 4635 | err_free_ret: |
1da177e4 LT |
4636 | kfree(host_set); |
4637 | VPRINTK("EXIT, returning 0\n"); | |
4638 | return 0; | |
4639 | } | |
4640 | ||
17b14451 AC |
4641 | /** |
4642 | * ata_host_set_remove - PCI layer callback for device removal | |
4643 | * @host_set: ATA host set that was removed | |
4644 | * | |
4645 | * Unregister all objects associated with this host set. Free those | |
4646 | * objects. | |
4647 | * | |
4648 | * LOCKING: | |
4649 | * Inherited from calling layer (may sleep). | |
4650 | */ | |
4651 | ||
17b14451 AC |
4652 | void ata_host_set_remove(struct ata_host_set *host_set) |
4653 | { | |
4654 | struct ata_port *ap; | |
4655 | unsigned int i; | |
4656 | ||
4657 | for (i = 0; i < host_set->n_ports; i++) { | |
4658 | ap = host_set->ports[i]; | |
4659 | scsi_remove_host(ap->host); | |
4660 | } | |
4661 | ||
4662 | free_irq(host_set->irq, host_set); | |
4663 | ||
4664 | for (i = 0; i < host_set->n_ports; i++) { | |
4665 | ap = host_set->ports[i]; | |
4666 | ||
4667 | ata_scsi_release(ap->host); | |
4668 | ||
4669 | if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) { | |
4670 | struct ata_ioports *ioaddr = &ap->ioaddr; | |
4671 | ||
4672 | if (ioaddr->cmd_addr == 0x1f0) | |
4673 | release_region(0x1f0, 8); | |
4674 | else if (ioaddr->cmd_addr == 0x170) | |
4675 | release_region(0x170, 8); | |
4676 | } | |
4677 | ||
4678 | scsi_host_put(ap->host); | |
4679 | } | |
4680 | ||
4681 | if (host_set->ops->host_stop) | |
4682 | host_set->ops->host_stop(host_set); | |
4683 | ||
4684 | kfree(host_set); | |
4685 | } | |
4686 | ||
1da177e4 LT |
4687 | /** |
4688 | * ata_scsi_release - SCSI layer callback hook for host unload | |
4689 | * @host: libata host to be unloaded | |
4690 | * | |
4691 | * Performs all duties necessary to shut down a libata port... | |
4692 | * Kill port kthread, disable port, and release resources. | |
4693 | * | |
4694 | * LOCKING: | |
4695 | * Inherited from SCSI layer. | |
4696 | * | |
4697 | * RETURNS: | |
4698 | * One. | |
4699 | */ | |
4700 | ||
4701 | int ata_scsi_release(struct Scsi_Host *host) | |
4702 | { | |
4703 | struct ata_port *ap = (struct ata_port *) &host->hostdata[0]; | |
4704 | ||
4705 | DPRINTK("ENTER\n"); | |
4706 | ||
4707 | ap->ops->port_disable(ap); | |
4708 | ata_host_remove(ap, 0); | |
4709 | ||
4710 | DPRINTK("EXIT\n"); | |
4711 | return 1; | |
4712 | } | |
4713 | ||
4714 | /** | |
4715 | * ata_std_ports - initialize ioaddr with standard port offsets. | |
4716 | * @ioaddr: IO address structure to be initialized | |
0baab86b EF |
4717 | * |
4718 | * Utility function which initializes data_addr, error_addr, | |
4719 | * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr, | |
4720 | * device_addr, status_addr, and command_addr to standard offsets | |
4721 | * relative to cmd_addr. | |
4722 | * | |
4723 | * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr. | |
1da177e4 | 4724 | */ |
0baab86b | 4725 | |
1da177e4 LT |
4726 | void ata_std_ports(struct ata_ioports *ioaddr) |
4727 | { | |
4728 | ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA; | |
4729 | ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR; | |
4730 | ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE; | |
4731 | ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT; | |
4732 | ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL; | |
4733 | ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM; | |
4734 | ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH; | |
4735 | ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE; | |
4736 | ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS; | |
4737 | ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD; | |
4738 | } | |
4739 | ||
0baab86b | 4740 | |
374b1873 JG |
4741 | #ifdef CONFIG_PCI |
4742 | ||
4743 | void ata_pci_host_stop (struct ata_host_set *host_set) | |
4744 | { | |
4745 | struct pci_dev *pdev = to_pci_dev(host_set->dev); | |
4746 | ||
4747 | pci_iounmap(pdev, host_set->mmio_base); | |
4748 | } | |
4749 | ||
1da177e4 LT |
4750 | /** |
4751 | * ata_pci_remove_one - PCI layer callback for device removal | |
4752 | * @pdev: PCI device that was removed | |
4753 | * | |
4754 | * PCI layer indicates to libata via this hook that | |
6f0ef4fa | 4755 | * hot-unplug or module unload event has occurred. |
1da177e4 LT |
4756 | * Handle this by unregistering all objects associated |
4757 | * with this PCI device. Free those objects. Then finally | |
4758 | * release PCI resources and disable device. | |
4759 | * | |
4760 | * LOCKING: | |
4761 | * Inherited from PCI layer (may sleep). | |
4762 | */ | |
4763 | ||
4764 | void ata_pci_remove_one (struct pci_dev *pdev) | |
4765 | { | |
4766 | struct device *dev = pci_dev_to_dev(pdev); | |
4767 | struct ata_host_set *host_set = dev_get_drvdata(dev); | |
1da177e4 | 4768 | |
17b14451 | 4769 | ata_host_set_remove(host_set); |
1da177e4 LT |
4770 | pci_release_regions(pdev); |
4771 | pci_disable_device(pdev); | |
4772 | dev_set_drvdata(dev, NULL); | |
4773 | } | |
4774 | ||
4775 | /* move to PCI subsystem */ | |
057ace5e | 4776 | int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits) |
1da177e4 LT |
4777 | { |
4778 | unsigned long tmp = 0; | |
4779 | ||
4780 | switch (bits->width) { | |
4781 | case 1: { | |
4782 | u8 tmp8 = 0; | |
4783 | pci_read_config_byte(pdev, bits->reg, &tmp8); | |
4784 | tmp = tmp8; | |
4785 | break; | |
4786 | } | |
4787 | case 2: { | |
4788 | u16 tmp16 = 0; | |
4789 | pci_read_config_word(pdev, bits->reg, &tmp16); | |
4790 | tmp = tmp16; | |
4791 | break; | |
4792 | } | |
4793 | case 4: { | |
4794 | u32 tmp32 = 0; | |
4795 | pci_read_config_dword(pdev, bits->reg, &tmp32); | |
4796 | tmp = tmp32; | |
4797 | break; | |
4798 | } | |
4799 | ||
4800 | default: | |
4801 | return -EINVAL; | |
4802 | } | |
4803 | ||
4804 | tmp &= bits->mask; | |
4805 | ||
4806 | return (tmp == bits->val) ? 1 : 0; | |
4807 | } | |
9b847548 JA |
4808 | |
4809 | int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t state) | |
4810 | { | |
4811 | pci_save_state(pdev); | |
4812 | pci_disable_device(pdev); | |
4813 | pci_set_power_state(pdev, PCI_D3hot); | |
4814 | return 0; | |
4815 | } | |
4816 | ||
4817 | int ata_pci_device_resume(struct pci_dev *pdev) | |
4818 | { | |
4819 | pci_set_power_state(pdev, PCI_D0); | |
4820 | pci_restore_state(pdev); | |
4821 | pci_enable_device(pdev); | |
4822 | pci_set_master(pdev); | |
4823 | return 0; | |
4824 | } | |
1da177e4 LT |
4825 | #endif /* CONFIG_PCI */ |
4826 | ||
4827 | ||
1da177e4 LT |
4828 | static int __init ata_init(void) |
4829 | { | |
4830 | ata_wq = create_workqueue("ata"); | |
4831 | if (!ata_wq) | |
4832 | return -ENOMEM; | |
4833 | ||
4834 | printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n"); | |
4835 | return 0; | |
4836 | } | |
4837 | ||
4838 | static void __exit ata_exit(void) | |
4839 | { | |
4840 | destroy_workqueue(ata_wq); | |
4841 | } | |
4842 | ||
4843 | module_init(ata_init); | |
4844 | module_exit(ata_exit); | |
4845 | ||
67846b30 JG |
4846 | static unsigned long ratelimit_time; |
4847 | static spinlock_t ata_ratelimit_lock = SPIN_LOCK_UNLOCKED; | |
4848 | ||
4849 | int ata_ratelimit(void) | |
4850 | { | |
4851 | int rc; | |
4852 | unsigned long flags; | |
4853 | ||
4854 | spin_lock_irqsave(&ata_ratelimit_lock, flags); | |
4855 | ||
4856 | if (time_after(jiffies, ratelimit_time)) { | |
4857 | rc = 1; | |
4858 | ratelimit_time = jiffies + (HZ/5); | |
4859 | } else | |
4860 | rc = 0; | |
4861 | ||
4862 | spin_unlock_irqrestore(&ata_ratelimit_lock, flags); | |
4863 | ||
4864 | return rc; | |
4865 | } | |
4866 | ||
1da177e4 LT |
4867 | /* |
4868 | * libata is essentially a library of internal helper functions for | |
4869 | * low-level ATA host controller drivers. As such, the API/ABI is | |
4870 | * likely to change as new drivers are added and updated. | |
4871 | * Do not depend on ABI/API stability. | |
4872 | */ | |
4873 | ||
4874 | EXPORT_SYMBOL_GPL(ata_std_bios_param); | |
4875 | EXPORT_SYMBOL_GPL(ata_std_ports); | |
4876 | EXPORT_SYMBOL_GPL(ata_device_add); | |
17b14451 | 4877 | EXPORT_SYMBOL_GPL(ata_host_set_remove); |
1da177e4 LT |
4878 | EXPORT_SYMBOL_GPL(ata_sg_init); |
4879 | EXPORT_SYMBOL_GPL(ata_sg_init_one); | |
4880 | EXPORT_SYMBOL_GPL(ata_qc_complete); | |
4881 | EXPORT_SYMBOL_GPL(ata_qc_issue_prot); | |
4882 | EXPORT_SYMBOL_GPL(ata_eng_timeout); | |
4883 | EXPORT_SYMBOL_GPL(ata_tf_load); | |
4884 | EXPORT_SYMBOL_GPL(ata_tf_read); | |
4885 | EXPORT_SYMBOL_GPL(ata_noop_dev_select); | |
4886 | EXPORT_SYMBOL_GPL(ata_std_dev_select); | |
4887 | EXPORT_SYMBOL_GPL(ata_tf_to_fis); | |
4888 | EXPORT_SYMBOL_GPL(ata_tf_from_fis); | |
4889 | EXPORT_SYMBOL_GPL(ata_check_status); | |
4890 | EXPORT_SYMBOL_GPL(ata_altstatus); | |
1da177e4 LT |
4891 | EXPORT_SYMBOL_GPL(ata_exec_command); |
4892 | EXPORT_SYMBOL_GPL(ata_port_start); | |
4893 | EXPORT_SYMBOL_GPL(ata_port_stop); | |
aa8f0dc6 | 4894 | EXPORT_SYMBOL_GPL(ata_host_stop); |
1da177e4 LT |
4895 | EXPORT_SYMBOL_GPL(ata_interrupt); |
4896 | EXPORT_SYMBOL_GPL(ata_qc_prep); | |
4897 | EXPORT_SYMBOL_GPL(ata_bmdma_setup); | |
4898 | EXPORT_SYMBOL_GPL(ata_bmdma_start); | |
4899 | EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear); | |
4900 | EXPORT_SYMBOL_GPL(ata_bmdma_status); | |
4901 | EXPORT_SYMBOL_GPL(ata_bmdma_stop); | |
4902 | EXPORT_SYMBOL_GPL(ata_port_probe); | |
4903 | EXPORT_SYMBOL_GPL(sata_phy_reset); | |
4904 | EXPORT_SYMBOL_GPL(__sata_phy_reset); | |
4905 | EXPORT_SYMBOL_GPL(ata_bus_reset); | |
8a19ac89 | 4906 | EXPORT_SYMBOL_GPL(ata_std_probeinit); |
c2bd5804 TH |
4907 | EXPORT_SYMBOL_GPL(ata_std_softreset); |
4908 | EXPORT_SYMBOL_GPL(sata_std_hardreset); | |
4909 | EXPORT_SYMBOL_GPL(ata_std_postreset); | |
4910 | EXPORT_SYMBOL_GPL(ata_std_probe_reset); | |
a62c0fc5 | 4911 | EXPORT_SYMBOL_GPL(ata_drive_probe_reset); |
1da177e4 | 4912 | EXPORT_SYMBOL_GPL(ata_port_disable); |
67846b30 | 4913 | EXPORT_SYMBOL_GPL(ata_ratelimit); |
6f8b9958 | 4914 | EXPORT_SYMBOL_GPL(ata_busy_sleep); |
1da177e4 LT |
4915 | EXPORT_SYMBOL_GPL(ata_scsi_ioctl); |
4916 | EXPORT_SYMBOL_GPL(ata_scsi_queuecmd); | |
f29841e0 | 4917 | EXPORT_SYMBOL_GPL(ata_scsi_timed_out); |
1da177e4 LT |
4918 | EXPORT_SYMBOL_GPL(ata_scsi_error); |
4919 | EXPORT_SYMBOL_GPL(ata_scsi_slave_config); | |
4920 | EXPORT_SYMBOL_GPL(ata_scsi_release); | |
4921 | EXPORT_SYMBOL_GPL(ata_host_intr); | |
4922 | EXPORT_SYMBOL_GPL(ata_dev_classify); | |
4923 | EXPORT_SYMBOL_GPL(ata_dev_id_string); | |
6f2f3812 | 4924 | EXPORT_SYMBOL_GPL(ata_dev_config); |
1da177e4 | 4925 | EXPORT_SYMBOL_GPL(ata_scsi_simulate); |
a72ec4ce TH |
4926 | EXPORT_SYMBOL_GPL(ata_eh_qc_complete); |
4927 | EXPORT_SYMBOL_GPL(ata_eh_qc_retry); | |
1da177e4 | 4928 | |
1bc4ccff | 4929 | EXPORT_SYMBOL_GPL(ata_pio_need_iordy); |
452503f9 AC |
4930 | EXPORT_SYMBOL_GPL(ata_timing_compute); |
4931 | EXPORT_SYMBOL_GPL(ata_timing_merge); | |
4932 | ||
1da177e4 LT |
4933 | #ifdef CONFIG_PCI |
4934 | EXPORT_SYMBOL_GPL(pci_test_config_bits); | |
374b1873 | 4935 | EXPORT_SYMBOL_GPL(ata_pci_host_stop); |
1da177e4 LT |
4936 | EXPORT_SYMBOL_GPL(ata_pci_init_native_mode); |
4937 | EXPORT_SYMBOL_GPL(ata_pci_init_one); | |
4938 | EXPORT_SYMBOL_GPL(ata_pci_remove_one); | |
9b847548 JA |
4939 | EXPORT_SYMBOL_GPL(ata_pci_device_suspend); |
4940 | EXPORT_SYMBOL_GPL(ata_pci_device_resume); | |
1da177e4 | 4941 | #endif /* CONFIG_PCI */ |
9b847548 JA |
4942 | |
4943 | EXPORT_SYMBOL_GPL(ata_device_suspend); | |
4944 | EXPORT_SYMBOL_GPL(ata_device_resume); | |
4945 | EXPORT_SYMBOL_GPL(ata_scsi_device_suspend); | |
4946 | EXPORT_SYMBOL_GPL(ata_scsi_device_resume); |