Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | #include <linux/types.h> |
2 | #include <linux/string.h> | |
3 | #include <linux/kernel.h> | |
1da177e4 | 4 | #include <linux/interrupt.h> |
1da177e4 LT |
5 | #include <linux/ide.h> |
6 | #include <linux/bitops.h> | |
7 | ||
3ab7efe8 BZ |
8 | static const char *udma_str[] = |
9 | { "UDMA/16", "UDMA/25", "UDMA/33", "UDMA/44", | |
10 | "UDMA/66", "UDMA/100", "UDMA/133", "UDMA7" }; | |
11 | static const char *mwdma_str[] = | |
12 | { "MWDMA0", "MWDMA1", "MWDMA2" }; | |
13 | static const char *swdma_str[] = | |
14 | { "SWDMA0", "SWDMA1", "SWDMA2" }; | |
15 | static const char *pio_str[] = | |
16 | { "PIO0", "PIO1", "PIO2", "PIO3", "PIO4", "PIO5" }; | |
1da177e4 LT |
17 | |
18 | /** | |
19 | * ide_xfer_verbose - return IDE mode names | |
3ab7efe8 | 20 | * @mode: transfer mode |
1da177e4 LT |
21 | * |
22 | * Returns a constant string giving the name of the mode | |
23 | * requested. | |
24 | */ | |
25 | ||
3ab7efe8 | 26 | const char *ide_xfer_verbose(u8 mode) |
1da177e4 | 27 | { |
3ab7efe8 BZ |
28 | const char *s; |
29 | u8 i = mode & 0xf; | |
30 | ||
31 | if (mode >= XFER_UDMA_0 && mode <= XFER_UDMA_7) | |
32 | s = udma_str[i]; | |
33 | else if (mode >= XFER_MW_DMA_0 && mode <= XFER_MW_DMA_2) | |
34 | s = mwdma_str[i]; | |
35 | else if (mode >= XFER_SW_DMA_0 && mode <= XFER_SW_DMA_2) | |
36 | s = swdma_str[i]; | |
37 | else if (mode >= XFER_PIO_0 && mode <= XFER_PIO_5) | |
38 | s = pio_str[i & 0x7]; | |
39 | else if (mode == XFER_PIO_SLOW) | |
40 | s = "PIO SLOW"; | |
41 | else | |
42 | s = "XFER ERROR"; | |
43 | ||
44 | return s; | |
1da177e4 LT |
45 | } |
46 | ||
47 | EXPORT_SYMBOL(ide_xfer_verbose); | |
48 | ||
49 | /** | |
2d5eaa6d BZ |
50 | * ide_rate_filter - filter transfer mode |
51 | * @drive: IDE device | |
1da177e4 LT |
52 | * @speed: desired speed |
53 | * | |
2d5eaa6d | 54 | * Given the available transfer modes this function returns |
1da177e4 | 55 | * the best available speed at or below the speed requested. |
2d5eaa6d | 56 | * |
7670df73 | 57 | * TODO: check device PIO capabilities |
1da177e4 LT |
58 | */ |
59 | ||
f212ff28 | 60 | static u8 ide_rate_filter(ide_drive_t *drive, u8 speed) |
1da177e4 | 61 | { |
2d5eaa6d | 62 | ide_hwif_t *hwif = drive->hwif; |
7670df73 | 63 | u8 mode = ide_find_dma_mode(drive, speed); |
2d5eaa6d | 64 | |
7670df73 BZ |
65 | if (mode == 0) { |
66 | if (hwif->pio_mask) | |
67 | mode = fls(hwif->pio_mask) - 1 + XFER_PIO_0; | |
68 | else | |
69 | mode = XFER_PIO_4; | |
70 | } | |
1da177e4 | 71 | |
eb63963a | 72 | /* printk("%s: mode 0x%02x, speed 0x%02x\n", __func__, mode, speed); */ |
1da177e4 | 73 | |
2d5eaa6d | 74 | return min(speed, mode); |
1da177e4 LT |
75 | } |
76 | ||
1da177e4 LT |
77 | /** |
78 | * ide_get_best_pio_mode - get PIO mode from drive | |
81d368e0 | 79 | * @drive: drive to consider |
1da177e4 | 80 | * @mode_wanted: preferred mode |
81d368e0 | 81 | * @max_mode: highest allowed mode |
1da177e4 LT |
82 | * |
83 | * This routine returns the recommended PIO settings for a given drive, | |
84 | * based on the drive->id information and the ide_pio_blacklist[]. | |
1da177e4 | 85 | * |
81d368e0 SS |
86 | * Drive PIO mode is auto-selected if 255 is passed as mode_wanted. |
87 | * This is used by most chipset support modules when "auto-tuning". | |
1da177e4 LT |
88 | */ |
89 | ||
2134758d | 90 | u8 ide_get_best_pio_mode (ide_drive_t *drive, u8 mode_wanted, u8 max_mode) |
1da177e4 | 91 | { |
4dde4492 BZ |
92 | u16 *id = drive->id; |
93 | int pio_mode = -1, overridden = 0; | |
1da177e4 | 94 | |
6a824c92 BZ |
95 | if (mode_wanted != 255) |
96 | return min_t(u8, mode_wanted, max_mode); | |
97 | ||
4dde4492 BZ |
98 | if ((drive->hwif->host_flags & IDE_HFLAG_PIO_NO_BLACKLIST) == 0) |
99 | pio_mode = ide_scan_pio_blacklist((char *)&id[ATA_ID_PROD]); | |
100 | ||
101 | if (pio_mode != -1) { | |
342cdb6d | 102 | printk(KERN_INFO "%s: is on PIO blacklist\n", drive->name); |
1da177e4 | 103 | } else { |
48fb2688 | 104 | pio_mode = id[ATA_ID_OLD_PIO_MODES] >> 8; |
1da177e4 LT |
105 | if (pio_mode > 2) { /* 2 is maximum allowed tPIO value */ |
106 | pio_mode = 2; | |
107 | overridden = 1; | |
108 | } | |
4dde4492 BZ |
109 | |
110 | if (id[ATA_ID_FIELD_VALID] & 2) { /* ATA2? */ | |
48fb2688 | 111 | if (ata_id_has_iordy(id)) { |
4dde4492 | 112 | if (id[ATA_ID_PIO_MODES] & 7) { |
1da177e4 | 113 | overridden = 0; |
4dde4492 | 114 | if (id[ATA_ID_PIO_MODES] & 4) |
1da177e4 | 115 | pio_mode = 5; |
4dde4492 | 116 | else if (id[ATA_ID_PIO_MODES] & 2) |
1da177e4 LT |
117 | pio_mode = 4; |
118 | else | |
119 | pio_mode = 3; | |
120 | } | |
1da177e4 LT |
121 | } |
122 | } | |
123 | ||
342cdb6d BZ |
124 | if (overridden) |
125 | printk(KERN_INFO "%s: tPIO > 2, assuming tPIO = 2\n", | |
126 | drive->name); | |
1da177e4 | 127 | } |
7dd00083 BZ |
128 | |
129 | if (pio_mode > max_mode) | |
1da177e4 | 130 | pio_mode = max_mode; |
7dd00083 | 131 | |
1da177e4 LT |
132 | return pio_mode; |
133 | } | |
134 | ||
135 | EXPORT_SYMBOL_GPL(ide_get_best_pio_mode); | |
136 | ||
26bcb879 BZ |
137 | /* req_pio == "255" for auto-tune */ |
138 | void ide_set_pio(ide_drive_t *drive, u8 req_pio) | |
139 | { | |
140 | ide_hwif_t *hwif = drive->hwif; | |
ac95beed | 141 | const struct ide_port_ops *port_ops = hwif->port_ops; |
26bcb879 BZ |
142 | u8 host_pio, pio; |
143 | ||
ac95beed | 144 | if (port_ops == NULL || port_ops->set_pio_mode == NULL || |
784506cb | 145 | (hwif->host_flags & IDE_HFLAG_NO_SET_MODE)) |
26bcb879 BZ |
146 | return; |
147 | ||
148 | BUG_ON(hwif->pio_mask == 0x00); | |
149 | ||
150 | host_pio = fls(hwif->pio_mask) - 1; | |
151 | ||
152 | pio = ide_get_best_pio_mode(drive, req_pio, host_pio); | |
153 | ||
154 | /* | |
155 | * TODO: | |
156 | * - report device max PIO mode | |
157 | * - check req_pio != 255 against device max PIO mode | |
158 | */ | |
159 | printk(KERN_DEBUG "%s: host max PIO%d wanted PIO%d%s selected PIO%d\n", | |
160 | drive->name, host_pio, req_pio, | |
161 | req_pio == 255 ? "(auto-tune)" : "", pio); | |
162 | ||
88b2b32b | 163 | (void)ide_set_pio_mode(drive, XFER_PIO_0 + pio); |
26bcb879 BZ |
164 | } |
165 | ||
166 | EXPORT_SYMBOL_GPL(ide_set_pio); | |
167 | ||
1da177e4 LT |
168 | /** |
169 | * ide_toggle_bounce - handle bounce buffering | |
170 | * @drive: drive to update | |
171 | * @on: on/off boolean | |
172 | * | |
173 | * Enable or disable bounce buffering for the device. Drives move | |
174 | * between PIO and DMA and that changes the rules we need. | |
175 | */ | |
176 | ||
177 | void ide_toggle_bounce(ide_drive_t *drive, int on) | |
178 | { | |
179 | u64 addr = BLK_BOUNCE_HIGH; /* dma64_addr_t */ | |
180 | ||
6593178d JB |
181 | if (!PCI_DMA_BUS_IS_PHYS) { |
182 | addr = BLK_BOUNCE_ANY; | |
183 | } else if (on && drive->media == ide_disk) { | |
36501650 BZ |
184 | struct device *dev = drive->hwif->dev; |
185 | ||
186 | if (dev && dev->dma_mask) | |
187 | addr = *dev->dma_mask; | |
1da177e4 LT |
188 | } |
189 | ||
190 | if (drive->queue) | |
191 | blk_queue_bounce_limit(drive->queue, addr); | |
192 | } | |
193 | ||
88b2b32b BZ |
194 | int ide_set_pio_mode(ide_drive_t *drive, const u8 mode) |
195 | { | |
196 | ide_hwif_t *hwif = drive->hwif; | |
ac95beed | 197 | const struct ide_port_ops *port_ops = hwif->port_ops; |
88b2b32b | 198 | |
784506cb BZ |
199 | if (hwif->host_flags & IDE_HFLAG_NO_SET_MODE) |
200 | return 0; | |
201 | ||
ac95beed | 202 | if (port_ops == NULL || port_ops->set_pio_mode == NULL) |
88b2b32b BZ |
203 | return -1; |
204 | ||
205 | /* | |
206 | * TODO: temporary hack for some legacy host drivers that didn't | |
207 | * set transfer mode on the device in ->set_pio_mode method... | |
208 | */ | |
ac95beed BZ |
209 | if (port_ops->set_dma_mode == NULL) { |
210 | port_ops->set_pio_mode(drive, mode - XFER_PIO_0); | |
88b2b32b BZ |
211 | return 0; |
212 | } | |
213 | ||
214 | if (hwif->host_flags & IDE_HFLAG_POST_SET_MODE) { | |
215 | if (ide_config_drive_speed(drive, mode)) | |
216 | return -1; | |
ac95beed | 217 | port_ops->set_pio_mode(drive, mode - XFER_PIO_0); |
88b2b32b BZ |
218 | return 0; |
219 | } else { | |
ac95beed | 220 | port_ops->set_pio_mode(drive, mode - XFER_PIO_0); |
88b2b32b BZ |
221 | return ide_config_drive_speed(drive, mode); |
222 | } | |
223 | } | |
224 | ||
225 | int ide_set_dma_mode(ide_drive_t *drive, const u8 mode) | |
226 | { | |
227 | ide_hwif_t *hwif = drive->hwif; | |
ac95beed | 228 | const struct ide_port_ops *port_ops = hwif->port_ops; |
88b2b32b | 229 | |
784506cb BZ |
230 | if (hwif->host_flags & IDE_HFLAG_NO_SET_MODE) |
231 | return 0; | |
232 | ||
ac95beed | 233 | if (port_ops == NULL || port_ops->set_dma_mode == NULL) |
88b2b32b BZ |
234 | return -1; |
235 | ||
236 | if (hwif->host_flags & IDE_HFLAG_POST_SET_MODE) { | |
237 | if (ide_config_drive_speed(drive, mode)) | |
238 | return -1; | |
ac95beed | 239 | port_ops->set_dma_mode(drive, mode); |
88b2b32b BZ |
240 | return 0; |
241 | } else { | |
ac95beed | 242 | port_ops->set_dma_mode(drive, mode); |
88b2b32b BZ |
243 | return ide_config_drive_speed(drive, mode); |
244 | } | |
245 | } | |
246 | ||
247 | EXPORT_SYMBOL_GPL(ide_set_dma_mode); | |
248 | ||
1da177e4 LT |
249 | /** |
250 | * ide_set_xfer_rate - set transfer rate | |
251 | * @drive: drive to set | |
88b2b32b | 252 | * @rate: speed to attempt to set |
1da177e4 LT |
253 | * |
254 | * General helper for setting the speed of an IDE device. This | |
255 | * function knows about user enforced limits from the configuration | |
88b2b32b | 256 | * which ->set_pio_mode/->set_dma_mode does not. |
1da177e4 | 257 | */ |
88b2b32b | 258 | |
1da177e4 LT |
259 | int ide_set_xfer_rate(ide_drive_t *drive, u8 rate) |
260 | { | |
f212ff28 | 261 | ide_hwif_t *hwif = drive->hwif; |
ac95beed | 262 | const struct ide_port_ops *port_ops = hwif->port_ops; |
f212ff28 | 263 | |
ac95beed | 264 | if (port_ops == NULL || port_ops->set_dma_mode == NULL || |
784506cb | 265 | (hwif->host_flags & IDE_HFLAG_NO_SET_MODE)) |
1da177e4 | 266 | return -1; |
f212ff28 BZ |
267 | |
268 | rate = ide_rate_filter(drive, rate); | |
269 | ||
3b2a5c71 BZ |
270 | BUG_ON(rate < XFER_PIO_0); |
271 | ||
88b2b32b BZ |
272 | if (rate >= XFER_PIO_0 && rate <= XFER_PIO_5) |
273 | return ide_set_pio_mode(drive, rate); | |
8f4dd2e4 | 274 | |
88b2b32b | 275 | return ide_set_dma_mode(drive, rate); |
1da177e4 LT |
276 | } |
277 | ||
1da177e4 LT |
278 | static void ide_dump_opcode(ide_drive_t *drive) |
279 | { | |
280 | struct request *rq; | |
7267c337 | 281 | ide_task_t *task = NULL; |
1da177e4 LT |
282 | |
283 | spin_lock(&ide_lock); | |
284 | rq = NULL; | |
285 | if (HWGROUP(drive)) | |
286 | rq = HWGROUP(drive)->rq; | |
287 | spin_unlock(&ide_lock); | |
288 | if (!rq) | |
289 | return; | |
7267c337 BZ |
290 | |
291 | if (rq->cmd_type == REQ_TYPE_ATA_TASKFILE) | |
292 | task = rq->special; | |
1da177e4 LT |
293 | |
294 | printk("ide: failed opcode was: "); | |
7267c337 BZ |
295 | if (task == NULL) |
296 | printk(KERN_CONT "unknown\n"); | |
1da177e4 | 297 | else |
7267c337 | 298 | printk(KERN_CONT "0x%02x\n", task->tf.command); |
1da177e4 LT |
299 | } |
300 | ||
a501633c | 301 | u64 ide_get_lba_addr(struct ide_taskfile *tf, int lba48) |
c2b57cdc BZ |
302 | { |
303 | u32 high, low; | |
304 | ||
305 | if (lba48) | |
306 | high = (tf->hob_lbah << 16) | (tf->hob_lbam << 8) | | |
307 | tf->hob_lbal; | |
308 | else | |
309 | high = tf->device & 0xf; | |
310 | low = (tf->lbah << 16) | (tf->lbam << 8) | tf->lbal; | |
311 | ||
312 | return ((u64)high << 24) | low; | |
313 | } | |
a501633c | 314 | EXPORT_SYMBOL_GPL(ide_get_lba_addr); |
c2b57cdc BZ |
315 | |
316 | static void ide_dump_sector(ide_drive_t *drive) | |
317 | { | |
318 | ide_task_t task; | |
319 | struct ide_taskfile *tf = &task.tf; | |
97100fc8 | 320 | u8 lba48 = !!(drive->dev_flags & IDE_DFLAG_LBA48); |
c2b57cdc BZ |
321 | |
322 | memset(&task, 0, sizeof(task)); | |
323 | if (lba48) | |
324 | task.tf_flags = IDE_TFLAG_IN_LBA | IDE_TFLAG_IN_HOB_LBA | | |
325 | IDE_TFLAG_LBA48; | |
326 | else | |
327 | task.tf_flags = IDE_TFLAG_IN_LBA | IDE_TFLAG_IN_DEVICE; | |
328 | ||
374e042c | 329 | drive->hwif->tp_ops->tf_read(drive, &task); |
c2b57cdc BZ |
330 | |
331 | if (lba48 || (tf->device & ATA_LBA)) | |
1c904fcf AM |
332 | printk(", LBAsect=%llu", |
333 | (unsigned long long)ide_get_lba_addr(tf, lba48)); | |
c2b57cdc BZ |
334 | else |
335 | printk(", CHS=%d/%d/%d", (tf->lbah << 8) + tf->lbam, | |
336 | tf->device & 0xf, tf->lbal); | |
337 | } | |
338 | ||
e62925dd | 339 | static void ide_dump_ata_error(ide_drive_t *drive, u8 err) |
1da177e4 | 340 | { |
e62925dd | 341 | printk("{ "); |
3a7d2484 BZ |
342 | if (err & ATA_ABORTED) printk("DriveStatusError "); |
343 | if (err & ATA_ICRC) | |
344 | printk((err & ATA_ABORTED) ? "BadCRC " : "BadSector "); | |
345 | if (err & ATA_UNC) printk("UncorrectableError "); | |
346 | if (err & ATA_IDNF) printk("SectorIdNotFound "); | |
347 | if (err & ATA_TRK0NF) printk("TrackZeroNotFound "); | |
348 | if (err & ATA_AMNF) printk("AddrMarkNotFound "); | |
e62925dd | 349 | printk("}"); |
3a7d2484 BZ |
350 | if ((err & (ATA_BBK | ATA_ABORTED)) == ATA_BBK || |
351 | (err & (ATA_UNC | ATA_IDNF | ATA_AMNF))) { | |
e62925dd BZ |
352 | ide_dump_sector(drive); |
353 | if (HWGROUP(drive) && HWGROUP(drive)->rq) | |
354 | printk(", sector=%llu", | |
355 | (unsigned long long)HWGROUP(drive)->rq->sector); | |
1da177e4 | 356 | } |
e62925dd BZ |
357 | printk("\n"); |
358 | } | |
359 | ||
360 | static void ide_dump_atapi_error(ide_drive_t *drive, u8 err) | |
361 | { | |
362 | printk("{ "); | |
3a7d2484 BZ |
363 | if (err & ATAPI_ILI) printk("IllegalLengthIndication "); |
364 | if (err & ATAPI_EOM) printk("EndOfMedia "); | |
365 | if (err & ATA_ABORTED) printk("AbortedCommand "); | |
366 | if (err & ATA_MCR) printk("MediaChangeRequested "); | |
367 | if (err & ATAPI_LFS) printk("LastFailedSense=0x%02x ", | |
368 | (err & ATAPI_LFS) >> 4); | |
13bbbf28 | 369 | printk("}\n"); |
1da177e4 LT |
370 | } |
371 | ||
372 | /** | |
e62925dd | 373 | * ide_dump_status - translate ATA/ATAPI error |
1da177e4 LT |
374 | * @drive: drive that status applies to |
375 | * @msg: text message to print | |
376 | * @stat: status byte to decode | |
377 | * | |
378 | * Error reporting, in human readable form (luxurious, but a memory hog). | |
e62925dd BZ |
379 | * Combines the drive name, message and status byte to provide a |
380 | * user understandable explanation of the device error. | |
1da177e4 LT |
381 | */ |
382 | ||
e62925dd | 383 | u8 ide_dump_status(ide_drive_t *drive, const char *msg, u8 stat) |
1da177e4 LT |
384 | { |
385 | unsigned long flags; | |
0e38a66a | 386 | u8 err = 0; |
1da177e4 | 387 | |
3d1c1cc9 | 388 | local_irq_save(flags); |
1da177e4 | 389 | printk("%s: %s: status=0x%02x { ", drive->name, msg, stat); |
3a7d2484 | 390 | if (stat & ATA_BUSY) |
1da177e4 LT |
391 | printk("Busy "); |
392 | else { | |
3a7d2484 BZ |
393 | if (stat & ATA_DRDY) printk("DriveReady "); |
394 | if (stat & ATA_DF) printk("DeviceFault "); | |
395 | if (stat & ATA_DSC) printk("SeekComplete "); | |
396 | if (stat & ATA_DRQ) printk("DataRequest "); | |
397 | if (stat & ATA_CORR) printk("CorrectedError "); | |
398 | if (stat & ATA_IDX) printk("Index "); | |
399 | if (stat & ATA_ERR) printk("Error "); | |
1da177e4 LT |
400 | } |
401 | printk("}\n"); | |
3a7d2484 | 402 | if ((stat & (ATA_BUSY | ATA_ERR)) == ATA_ERR) { |
64a57fe4 | 403 | err = ide_read_error(drive); |
e62925dd BZ |
404 | printk("%s: %s: error=0x%02x ", drive->name, msg, err); |
405 | if (drive->media == ide_disk) | |
406 | ide_dump_ata_error(drive, err); | |
407 | else | |
408 | ide_dump_atapi_error(drive, err); | |
1da177e4 LT |
409 | } |
410 | ide_dump_opcode(drive); | |
411 | local_irq_restore(flags); | |
0e38a66a | 412 | return err; |
1da177e4 LT |
413 | } |
414 | ||
1da177e4 | 415 | EXPORT_SYMBOL(ide_dump_status); |