Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/drivers/ide/ide-probe.c Version 1.11 Mar 05, 2003 | |
3 | * | |
4 | * Copyright (C) 1994-1998 Linus Torvalds & authors (see below) | |
5 | */ | |
6 | ||
7 | /* | |
8 | * Mostly written by Mark Lord <mlord@pobox.com> | |
9 | * and Gadi Oxman <gadio@netvision.net.il> | |
10 | * and Andre Hedrick <andre@linux-ide.org> | |
11 | * | |
12 | * See linux/MAINTAINERS for address of current maintainer. | |
13 | * | |
14 | * This is the IDE probe module, as evolved from hd.c and ide.c. | |
15 | * | |
bbe4d6d8 BZ |
16 | * -- increase WAIT_PIDENTIFY to avoid CD-ROM locking at boot |
17 | * by Andrea Arcangeli | |
1da177e4 LT |
18 | */ |
19 | ||
1da177e4 LT |
20 | #include <linux/module.h> |
21 | #include <linux/types.h> | |
22 | #include <linux/string.h> | |
23 | #include <linux/kernel.h> | |
24 | #include <linux/timer.h> | |
25 | #include <linux/mm.h> | |
26 | #include <linux/interrupt.h> | |
27 | #include <linux/major.h> | |
28 | #include <linux/errno.h> | |
29 | #include <linux/genhd.h> | |
30 | #include <linux/slab.h> | |
31 | #include <linux/delay.h> | |
32 | #include <linux/ide.h> | |
33 | #include <linux/spinlock.h> | |
34 | #include <linux/kmod.h> | |
35 | #include <linux/pci.h> | |
dc81785d | 36 | #include <linux/scatterlist.h> |
1da177e4 LT |
37 | |
38 | #include <asm/byteorder.h> | |
39 | #include <asm/irq.h> | |
40 | #include <asm/uaccess.h> | |
41 | #include <asm/io.h> | |
42 | ||
43 | /** | |
44 | * generic_id - add a generic drive id | |
45 | * @drive: drive to make an ID block for | |
46 | * | |
47 | * Add a fake id field to the drive we are passed. This allows | |
48 | * use to skip a ton of NULL checks (which people always miss) | |
49 | * and make drive properties unconditional outside of this file | |
50 | */ | |
51 | ||
52 | static void generic_id(ide_drive_t *drive) | |
53 | { | |
54 | drive->id->cyls = drive->cyl; | |
55 | drive->id->heads = drive->head; | |
56 | drive->id->sectors = drive->sect; | |
57 | drive->id->cur_cyls = drive->cyl; | |
58 | drive->id->cur_heads = drive->head; | |
59 | drive->id->cur_sectors = drive->sect; | |
60 | } | |
61 | ||
62 | static void ide_disk_init_chs(ide_drive_t *drive) | |
63 | { | |
64 | struct hd_driveid *id = drive->id; | |
65 | ||
66 | /* Extract geometry if we did not already have one for the drive */ | |
67 | if (!drive->cyl || !drive->head || !drive->sect) { | |
68 | drive->cyl = drive->bios_cyl = id->cyls; | |
69 | drive->head = drive->bios_head = id->heads; | |
70 | drive->sect = drive->bios_sect = id->sectors; | |
71 | } | |
72 | ||
73 | /* Handle logical geometry translation by the drive */ | |
74 | if ((id->field_valid & 1) && id->cur_cyls && | |
75 | id->cur_heads && (id->cur_heads <= 16) && id->cur_sectors) { | |
76 | drive->cyl = id->cur_cyls; | |
77 | drive->head = id->cur_heads; | |
78 | drive->sect = id->cur_sectors; | |
79 | } | |
80 | ||
81 | /* Use physical geometry if what we have still makes no sense */ | |
82 | if (drive->head > 16 && id->heads && id->heads <= 16) { | |
83 | drive->cyl = id->cyls; | |
84 | drive->head = id->heads; | |
85 | drive->sect = id->sectors; | |
86 | } | |
87 | } | |
88 | ||
89 | static void ide_disk_init_mult_count(ide_drive_t *drive) | |
90 | { | |
91 | struct hd_driveid *id = drive->id; | |
92 | ||
93 | drive->mult_count = 0; | |
94 | if (id->max_multsect) { | |
95 | #ifdef CONFIG_IDEDISK_MULTI_MODE | |
96 | id->multsect = ((id->max_multsect/2) > 1) ? id->max_multsect : 0; | |
97 | id->multsect_valid = id->multsect ? 1 : 0; | |
4ee06b7e | 98 | drive->mult_req = id->multsect_valid ? id->max_multsect : 0; |
1da177e4 LT |
99 | drive->special.b.set_multmode = drive->mult_req ? 1 : 0; |
100 | #else /* original, pre IDE-NFG, per request of AC */ | |
4ee06b7e | 101 | drive->mult_req = 0; |
1da177e4 LT |
102 | if (drive->mult_req > id->max_multsect) |
103 | drive->mult_req = id->max_multsect; | |
104 | if (drive->mult_req || ((id->multsect_valid & 1) && id->multsect)) | |
105 | drive->special.b.set_multmode = 1; | |
106 | #endif | |
107 | } | |
108 | } | |
109 | ||
1da177e4 LT |
110 | /** |
111 | * do_identify - identify a drive | |
112 | * @drive: drive to identify | |
113 | * @cmd: command used | |
114 | * | |
115 | * Called when we have issued a drive identify command to | |
116 | * read and parse the results. This function is run with | |
117 | * interrupts disabled. | |
118 | */ | |
119 | ||
120 | static inline void do_identify (ide_drive_t *drive, u8 cmd) | |
121 | { | |
122 | ide_hwif_t *hwif = HWIF(drive); | |
123 | int bswap = 1; | |
124 | struct hd_driveid *id; | |
125 | ||
126 | id = drive->id; | |
127 | /* read 512 bytes of id info */ | |
128 | hwif->ata_input_data(drive, id, SECTOR_WORDS); | |
129 | ||
130 | drive->id_read = 1; | |
131 | local_irq_enable(); | |
132 | ide_fix_driveid(id); | |
133 | ||
e71bc140 | 134 | #if defined (CONFIG_SCSI_EATA_PIO) || defined (CONFIG_SCSI_EATA) |
1da177e4 LT |
135 | /* |
136 | * EATA SCSI controllers do a hardware ATA emulation: | |
137 | * Ignore them if there is a driver for them available. | |
138 | */ | |
139 | if ((id->model[0] == 'P' && id->model[1] == 'M') || | |
140 | (id->model[0] == 'S' && id->model[1] == 'K')) { | |
141 | printk("%s: EATA SCSI HBA %.10s\n", drive->name, id->model); | |
142 | goto err_misc; | |
143 | } | |
e71bc140 | 144 | #endif /* CONFIG_SCSI_EATA || CONFIG_SCSI_EATA_PIO */ |
1da177e4 LT |
145 | |
146 | /* | |
147 | * WIN_IDENTIFY returns little-endian info, | |
148 | * WIN_PIDENTIFY *usually* returns little-endian info. | |
149 | */ | |
150 | if (cmd == WIN_PIDENTIFY) { | |
151 | if ((id->model[0] == 'N' && id->model[1] == 'E') /* NEC */ | |
152 | || (id->model[0] == 'F' && id->model[1] == 'X') /* Mitsumi */ | |
153 | || (id->model[0] == 'P' && id->model[1] == 'i'))/* Pioneer */ | |
154 | /* Vertos drives may still be weird */ | |
155 | bswap ^= 1; | |
156 | } | |
157 | ide_fixstring(id->model, sizeof(id->model), bswap); | |
158 | ide_fixstring(id->fw_rev, sizeof(id->fw_rev), bswap); | |
159 | ide_fixstring(id->serial_no, sizeof(id->serial_no), bswap); | |
160 | ||
699b052a TH |
161 | /* we depend on this a lot! */ |
162 | id->model[sizeof(id->model)-1] = '\0'; | |
163 | ||
1da177e4 LT |
164 | if (strstr(id->model, "E X A B Y T E N E S T")) |
165 | goto err_misc; | |
166 | ||
1da177e4 LT |
167 | printk("%s: %s, ", drive->name, id->model); |
168 | drive->present = 1; | |
169 | drive->dead = 0; | |
170 | ||
171 | /* | |
172 | * Check for an ATAPI device | |
173 | */ | |
174 | if (cmd == WIN_PIDENTIFY) { | |
175 | u8 type = (id->config >> 8) & 0x1f; | |
176 | printk("ATAPI "); | |
177 | switch (type) { | |
178 | case ide_floppy: | |
179 | if (!strstr(id->model, "CD-ROM")) { | |
180 | if (!strstr(id->model, "oppy") && | |
181 | !strstr(id->model, "poyp") && | |
182 | !strstr(id->model, "ZIP")) | |
183 | printk("cdrom or floppy?, assuming "); | |
184 | if (drive->media != ide_cdrom) { | |
185 | printk ("FLOPPY"); | |
186 | drive->removable = 1; | |
187 | break; | |
188 | } | |
189 | } | |
190 | /* Early cdrom models used zero */ | |
191 | type = ide_cdrom; | |
192 | case ide_cdrom: | |
193 | drive->removable = 1; | |
194 | #ifdef CONFIG_PPC | |
195 | /* kludge for Apple PowerBook internal zip */ | |
196 | if (!strstr(id->model, "CD-ROM") && | |
197 | strstr(id->model, "ZIP")) { | |
198 | printk ("FLOPPY"); | |
199 | type = ide_floppy; | |
200 | break; | |
201 | } | |
202 | #endif | |
203 | printk ("CD/DVD-ROM"); | |
204 | break; | |
205 | case ide_tape: | |
206 | printk ("TAPE"); | |
207 | break; | |
208 | case ide_optical: | |
209 | printk ("OPTICAL"); | |
210 | drive->removable = 1; | |
211 | break; | |
212 | default: | |
213 | printk("UNKNOWN (type %d)", type); | |
214 | break; | |
215 | } | |
216 | printk (" drive\n"); | |
217 | drive->media = type; | |
218 | /* an ATAPI device ignores DRDY */ | |
219 | drive->ready_stat = 0; | |
220 | return; | |
221 | } | |
222 | ||
223 | /* | |
224 | * Not an ATAPI device: looks like a "regular" hard disk | |
225 | */ | |
98109337 RP |
226 | |
227 | /* | |
228 | * 0x848a = CompactFlash device | |
229 | * These are *not* removable in Linux definition of the term | |
230 | */ | |
231 | ||
232 | if ((id->config != 0x848a) && (id->config & (1<<7))) | |
1da177e4 LT |
233 | drive->removable = 1; |
234 | ||
1da177e4 | 235 | drive->media = ide_disk; |
98109337 | 236 | printk("%s DISK drive\n", (id->config == 0x848a) ? "CFA" : "ATA" ); |
1da177e4 LT |
237 | QUIRK_LIST(drive); |
238 | return; | |
239 | ||
240 | err_misc: | |
241 | kfree(id); | |
242 | drive->present = 0; | |
243 | return; | |
244 | } | |
245 | ||
246 | /** | |
247 | * actual_try_to_identify - send ata/atapi identify | |
248 | * @drive: drive to identify | |
249 | * @cmd: command to use | |
250 | * | |
251 | * try_to_identify() sends an ATA(PI) IDENTIFY request to a drive | |
252 | * and waits for a response. It also monitors irqs while this is | |
253 | * happening, in hope of automatically determining which one is | |
254 | * being used by the interface. | |
255 | * | |
256 | * Returns: 0 device was identified | |
257 | * 1 device timed-out (no response to identify request) | |
258 | * 2 device aborted the command (refused to identify itself) | |
259 | */ | |
260 | ||
261 | static int actual_try_to_identify (ide_drive_t *drive, u8 cmd) | |
262 | { | |
263 | ide_hwif_t *hwif = HWIF(drive); | |
264 | int rc; | |
265 | unsigned long hd_status; | |
266 | unsigned long timeout; | |
267 | u8 s = 0, a = 0; | |
268 | ||
269 | /* take a deep breath */ | |
270 | msleep(50); | |
271 | ||
272 | if (IDE_CONTROL_REG) { | |
273 | a = hwif->INB(IDE_ALTSTATUS_REG); | |
274 | s = hwif->INB(IDE_STATUS_REG); | |
275 | if ((a ^ s) & ~INDEX_STAT) { | |
276 | printk(KERN_INFO "%s: probing with STATUS(0x%02x) instead of " | |
277 | "ALTSTATUS(0x%02x)\n", drive->name, s, a); | |
278 | /* ancient Seagate drives, broken interfaces */ | |
279 | hd_status = IDE_STATUS_REG; | |
280 | } else { | |
281 | /* use non-intrusive polling */ | |
282 | hd_status = IDE_ALTSTATUS_REG; | |
283 | } | |
284 | } else | |
285 | hd_status = IDE_STATUS_REG; | |
286 | ||
287 | /* set features register for atapi | |
288 | * identify command to be sure of reply | |
289 | */ | |
290 | if ((cmd == WIN_PIDENTIFY)) | |
291 | /* disable dma & overlap */ | |
292 | hwif->OUTB(0, IDE_FEATURE_REG); | |
293 | ||
294 | /* ask drive for ID */ | |
295 | hwif->OUTB(cmd, IDE_COMMAND_REG); | |
296 | ||
297 | timeout = ((cmd == WIN_IDENTIFY) ? WAIT_WORSTCASE : WAIT_PIDENTIFY) / 2; | |
298 | timeout += jiffies; | |
299 | do { | |
300 | if (time_after(jiffies, timeout)) { | |
301 | /* drive timed-out */ | |
302 | return 1; | |
303 | } | |
304 | /* give drive a breather */ | |
305 | msleep(50); | |
306 | } while ((hwif->INB(hd_status)) & BUSY_STAT); | |
307 | ||
308 | /* wait for IRQ and DRQ_STAT */ | |
309 | msleep(50); | |
310 | if (OK_STAT((hwif->INB(IDE_STATUS_REG)), DRQ_STAT, BAD_R_STAT)) { | |
311 | unsigned long flags; | |
312 | ||
313 | /* local CPU only; some systems need this */ | |
314 | local_irq_save(flags); | |
315 | /* drive returned ID */ | |
316 | do_identify(drive, cmd); | |
317 | /* drive responded with ID */ | |
318 | rc = 0; | |
319 | /* clear drive IRQ */ | |
320 | (void) hwif->INB(IDE_STATUS_REG); | |
321 | local_irq_restore(flags); | |
322 | } else { | |
323 | /* drive refused ID */ | |
324 | rc = 2; | |
325 | } | |
326 | return rc; | |
327 | } | |
328 | ||
329 | /** | |
330 | * try_to_identify - try to identify a drive | |
331 | * @drive: drive to probe | |
332 | * @cmd: command to use | |
333 | * | |
334 | * Issue the identify command and then do IRQ probing to | |
335 | * complete the identification when needed by finding the | |
336 | * IRQ the drive is attached to | |
337 | */ | |
338 | ||
339 | static int try_to_identify (ide_drive_t *drive, u8 cmd) | |
340 | { | |
341 | ide_hwif_t *hwif = HWIF(drive); | |
342 | int retval; | |
343 | int autoprobe = 0; | |
344 | unsigned long cookie = 0; | |
345 | ||
346 | /* | |
347 | * Disable device irq unless we need to | |
348 | * probe for it. Otherwise we'll get spurious | |
349 | * interrupts during the identify-phase that | |
350 | * the irq handler isn't expecting. | |
351 | */ | |
352 | if (IDE_CONTROL_REG) { | |
353 | u8 ctl = drive->ctl | 2; | |
354 | if (!hwif->irq) { | |
355 | autoprobe = 1; | |
356 | cookie = probe_irq_on(); | |
357 | /* enable device irq */ | |
358 | ctl &= ~2; | |
359 | } | |
360 | hwif->OUTB(ctl, IDE_CONTROL_REG); | |
361 | } | |
362 | ||
363 | retval = actual_try_to_identify(drive, cmd); | |
364 | ||
365 | if (autoprobe) { | |
366 | int irq; | |
367 | /* mask device irq */ | |
368 | hwif->OUTB(drive->ctl|2, IDE_CONTROL_REG); | |
369 | /* clear drive IRQ */ | |
370 | (void) hwif->INB(IDE_STATUS_REG); | |
371 | udelay(5); | |
372 | irq = probe_irq_off(cookie); | |
373 | if (!hwif->irq) { | |
374 | if (irq > 0) { | |
375 | hwif->irq = irq; | |
376 | } else { | |
377 | /* Mmmm.. multiple IRQs.. | |
378 | * don't know which was ours | |
379 | */ | |
380 | printk("%s: IRQ probe failed (0x%lx)\n", | |
381 | drive->name, cookie); | |
382 | } | |
383 | } | |
384 | } | |
385 | return retval; | |
386 | } | |
387 | ||
388 | ||
389 | /** | |
390 | * do_probe - probe an IDE device | |
391 | * @drive: drive to probe | |
392 | * @cmd: command to use | |
393 | * | |
394 | * do_probe() has the difficult job of finding a drive if it exists, | |
395 | * without getting hung up if it doesn't exist, without trampling on | |
396 | * ethernet cards, and without leaving any IRQs dangling to haunt us later. | |
397 | * | |
398 | * If a drive is "known" to exist (from CMOS or kernel parameters), | |
399 | * but does not respond right away, the probe will "hang in there" | |
400 | * for the maximum wait time (about 30 seconds), otherwise it will | |
401 | * exit much more quickly. | |
402 | * | |
403 | * Returns: 0 device was identified | |
404 | * 1 device timed-out (no response to identify request) | |
405 | * 2 device aborted the command (refused to identify itself) | |
406 | * 3 bad status from device (possible for ATAPI drives) | |
407 | * 4 probe was not attempted because failure was obvious | |
408 | */ | |
409 | ||
410 | static int do_probe (ide_drive_t *drive, u8 cmd) | |
411 | { | |
412 | int rc; | |
413 | ide_hwif_t *hwif = HWIF(drive); | |
414 | ||
415 | if (drive->present) { | |
416 | /* avoid waiting for inappropriate probes */ | |
417 | if ((drive->media != ide_disk) && (cmd == WIN_IDENTIFY)) | |
418 | return 4; | |
419 | } | |
420 | #ifdef DEBUG | |
421 | printk("probing for %s: present=%d, media=%d, probetype=%s\n", | |
422 | drive->name, drive->present, drive->media, | |
423 | (cmd == WIN_IDENTIFY) ? "ATA" : "ATAPI"); | |
424 | #endif | |
425 | ||
426 | /* needed for some systems | |
427 | * (e.g. crw9624 as drive0 with disk as slave) | |
428 | */ | |
429 | msleep(50); | |
430 | SELECT_DRIVE(drive); | |
431 | msleep(50); | |
432 | if (hwif->INB(IDE_SELECT_REG) != drive->select.all && !drive->present) { | |
433 | if (drive->select.b.unit != 0) { | |
434 | /* exit with drive0 selected */ | |
435 | SELECT_DRIVE(&hwif->drives[0]); | |
436 | /* allow BUSY_STAT to assert & clear */ | |
437 | msleep(50); | |
438 | } | |
439 | /* no i/f present: mmm.. this should be a 4 -ml */ | |
440 | return 3; | |
441 | } | |
442 | ||
443 | if (OK_STAT((hwif->INB(IDE_STATUS_REG)), READY_STAT, BUSY_STAT) || | |
444 | drive->present || cmd == WIN_PIDENTIFY) { | |
445 | /* send cmd and wait */ | |
446 | if ((rc = try_to_identify(drive, cmd))) { | |
447 | /* failed: try again */ | |
448 | rc = try_to_identify(drive,cmd); | |
449 | } | |
450 | if (hwif->INB(IDE_STATUS_REG) == (BUSY_STAT|READY_STAT)) | |
451 | return 4; | |
452 | ||
453 | if ((rc == 1 && cmd == WIN_PIDENTIFY) && | |
454 | ((drive->autotune == IDE_TUNE_DEFAULT) || | |
455 | (drive->autotune == IDE_TUNE_AUTO))) { | |
456 | unsigned long timeout; | |
457 | printk("%s: no response (status = 0x%02x), " | |
458 | "resetting drive\n", drive->name, | |
459 | hwif->INB(IDE_STATUS_REG)); | |
460 | msleep(50); | |
461 | hwif->OUTB(drive->select.all, IDE_SELECT_REG); | |
462 | msleep(50); | |
463 | hwif->OUTB(WIN_SRST, IDE_COMMAND_REG); | |
464 | timeout = jiffies; | |
465 | while (((hwif->INB(IDE_STATUS_REG)) & BUSY_STAT) && | |
466 | time_before(jiffies, timeout + WAIT_WORSTCASE)) | |
467 | msleep(50); | |
468 | rc = try_to_identify(drive, cmd); | |
469 | } | |
470 | if (rc == 1) | |
471 | printk("%s: no response (status = 0x%02x)\n", | |
472 | drive->name, hwif->INB(IDE_STATUS_REG)); | |
473 | /* ensure drive irq is clear */ | |
474 | (void) hwif->INB(IDE_STATUS_REG); | |
475 | } else { | |
476 | /* not present or maybe ATAPI */ | |
477 | rc = 3; | |
478 | } | |
479 | if (drive->select.b.unit != 0) { | |
480 | /* exit with drive0 selected */ | |
481 | SELECT_DRIVE(&hwif->drives[0]); | |
482 | msleep(50); | |
483 | /* ensure drive irq is clear */ | |
484 | (void) hwif->INB(IDE_STATUS_REG); | |
485 | } | |
486 | return rc; | |
487 | } | |
488 | ||
489 | /* | |
490 | * | |
491 | */ | |
492 | static void enable_nest (ide_drive_t *drive) | |
493 | { | |
494 | ide_hwif_t *hwif = HWIF(drive); | |
495 | unsigned long timeout; | |
496 | ||
497 | printk("%s: enabling %s -- ", hwif->name, drive->id->model); | |
498 | SELECT_DRIVE(drive); | |
499 | msleep(50); | |
500 | hwif->OUTB(EXABYTE_ENABLE_NEST, IDE_COMMAND_REG); | |
501 | timeout = jiffies + WAIT_WORSTCASE; | |
502 | do { | |
503 | if (time_after(jiffies, timeout)) { | |
504 | printk("failed (timeout)\n"); | |
505 | return; | |
506 | } | |
507 | msleep(50); | |
508 | } while ((hwif->INB(IDE_STATUS_REG)) & BUSY_STAT); | |
509 | ||
510 | msleep(50); | |
511 | ||
512 | if (!OK_STAT((hwif->INB(IDE_STATUS_REG)), 0, BAD_STAT)) { | |
513 | printk("failed (status = 0x%02x)\n", hwif->INB(IDE_STATUS_REG)); | |
514 | } else { | |
515 | printk("success\n"); | |
516 | } | |
517 | ||
518 | /* if !(success||timed-out) */ | |
519 | if (do_probe(drive, WIN_IDENTIFY) >= 2) { | |
520 | /* look for ATAPI device */ | |
521 | (void) do_probe(drive, WIN_PIDENTIFY); | |
522 | } | |
523 | } | |
524 | ||
525 | /** | |
526 | * probe_for_drives - upper level drive probe | |
527 | * @drive: drive to probe for | |
528 | * | |
529 | * probe_for_drive() tests for existence of a given drive using do_probe() | |
530 | * and presents things to the user as needed. | |
531 | * | |
532 | * Returns: 0 no device was found | |
533 | * 1 device was found (note: drive->present might | |
534 | * still be 0) | |
535 | */ | |
536 | ||
537 | static inline u8 probe_for_drive (ide_drive_t *drive) | |
538 | { | |
539 | /* | |
540 | * In order to keep things simple we have an id | |
541 | * block for all drives at all times. If the device | |
542 | * is pre ATA or refuses ATA/ATAPI identify we | |
543 | * will add faked data to this. | |
544 | * | |
545 | * Also note that 0 everywhere means "can't do X" | |
546 | */ | |
547 | ||
f5e3c2fa | 548 | drive->id = kzalloc(SECTOR_WORDS *4, GFP_KERNEL); |
1da177e4 LT |
549 | drive->id_read = 0; |
550 | if(drive->id == NULL) | |
551 | { | |
552 | printk(KERN_ERR "ide: out of memory for id data.\n"); | |
553 | return 0; | |
554 | } | |
1da177e4 LT |
555 | strcpy(drive->id->model, "UNKNOWN"); |
556 | ||
557 | /* skip probing? */ | |
558 | if (!drive->noprobe) | |
559 | { | |
560 | /* if !(success||timed-out) */ | |
561 | if (do_probe(drive, WIN_IDENTIFY) >= 2) { | |
562 | /* look for ATAPI device */ | |
563 | (void) do_probe(drive, WIN_PIDENTIFY); | |
564 | } | |
1da177e4 LT |
565 | if (!drive->present) |
566 | /* drive not found */ | |
567 | return 0; | |
78595575 AC |
568 | if (strstr(drive->id->model, "E X A B Y T E N E S T")) |
569 | enable_nest(drive); | |
1da177e4 LT |
570 | |
571 | /* identification failed? */ | |
572 | if (!drive->id_read) { | |
573 | if (drive->media == ide_disk) { | |
574 | printk(KERN_INFO "%s: non-IDE drive, CHS=%d/%d/%d\n", | |
575 | drive->name, drive->cyl, | |
576 | drive->head, drive->sect); | |
577 | } else if (drive->media == ide_cdrom) { | |
578 | printk(KERN_INFO "%s: ATAPI cdrom (?)\n", drive->name); | |
579 | } else { | |
580 | /* nuke it */ | |
581 | printk(KERN_WARNING "%s: Unknown device on bus refused identification. Ignoring.\n", drive->name); | |
582 | drive->present = 0; | |
583 | } | |
584 | } | |
585 | /* drive was found */ | |
586 | } | |
587 | if(!drive->present) | |
588 | return 0; | |
589 | /* The drive wasn't being helpful. Add generic info only */ | |
590 | if (drive->id_read == 0) { | |
591 | generic_id(drive); | |
592 | return 1; | |
593 | } | |
594 | ||
595 | if (drive->media == ide_disk) { | |
596 | ide_disk_init_chs(drive); | |
597 | ide_disk_init_mult_count(drive); | |
598 | } | |
599 | ||
600 | return drive->present; | |
601 | } | |
602 | ||
603 | static void hwif_release_dev (struct device *dev) | |
604 | { | |
605 | ide_hwif_t *hwif = container_of(dev, ide_hwif_t, gendev); | |
606 | ||
f36d4024 | 607 | complete(&hwif->gendev_rel_comp); |
1da177e4 LT |
608 | } |
609 | ||
610 | static void hwif_register (ide_hwif_t *hwif) | |
611 | { | |
349ae23f RD |
612 | int ret; |
613 | ||
1da177e4 LT |
614 | /* register with global device tree */ |
615 | strlcpy(hwif->gendev.bus_id,hwif->name,BUS_ID_SIZE); | |
616 | hwif->gendev.driver_data = hwif; | |
617 | if (hwif->gendev.parent == NULL) { | |
618 | if (hwif->pci_dev) | |
619 | hwif->gendev.parent = &hwif->pci_dev->dev; | |
620 | else | |
621 | /* Would like to do = &device_legacy */ | |
622 | hwif->gendev.parent = NULL; | |
623 | } | |
624 | hwif->gendev.release = hwif_release_dev; | |
349ae23f RD |
625 | ret = device_register(&hwif->gendev); |
626 | if (ret < 0) | |
627 | printk(KERN_WARNING "IDE: %s: device_register error: %d\n", | |
628 | __FUNCTION__, ret); | |
1da177e4 LT |
629 | } |
630 | ||
631 | static int wait_hwif_ready(ide_hwif_t *hwif) | |
632 | { | |
8266105b | 633 | int unit, rc; |
1da177e4 LT |
634 | |
635 | printk(KERN_DEBUG "Probing IDE interface %s...\n", hwif->name); | |
636 | ||
637 | /* Let HW settle down a bit from whatever init state we | |
638 | * come from */ | |
639 | mdelay(2); | |
640 | ||
641 | /* Wait for BSY bit to go away, spec timeout is 30 seconds, | |
642 | * I know of at least one disk who takes 31 seconds, I use 35 | |
643 | * here to be safe | |
644 | */ | |
645 | rc = ide_wait_not_busy(hwif, 35000); | |
646 | if (rc) | |
647 | return rc; | |
648 | ||
649 | /* Now make sure both master & slave are ready */ | |
8266105b JS |
650 | for (unit = 0; unit < MAX_DRIVES; unit++) { |
651 | ide_drive_t *drive = &hwif->drives[unit]; | |
652 | ||
653 | /* Ignore disks that we will not probe for later. */ | |
654 | if (!drive->noprobe || drive->present) { | |
655 | SELECT_DRIVE(drive); | |
ad0e74d3 BZ |
656 | if (IDE_CONTROL_REG) |
657 | hwif->OUTB(drive->ctl, IDE_CONTROL_REG); | |
8266105b JS |
658 | mdelay(2); |
659 | rc = ide_wait_not_busy(hwif, 35000); | |
660 | if (rc) | |
661 | goto out; | |
662 | } else | |
663 | printk(KERN_DEBUG "%s: ide_wait_not_busy() skipped\n", | |
664 | drive->name); | |
665 | } | |
666 | out: | |
1da177e4 | 667 | /* Exit function with master reselected (let's be sane) */ |
8266105b JS |
668 | if (unit) |
669 | SELECT_DRIVE(&hwif->drives[0]); | |
670 | ||
1da177e4 LT |
671 | return rc; |
672 | } | |
673 | ||
674 | /** | |
675 | * ide_undecoded_slave - look for bad CF adapters | |
676 | * @hwif: interface | |
677 | * | |
678 | * Analyse the drives on the interface and attempt to decide if we | |
679 | * have the same drive viewed twice. This occurs with crap CF adapters | |
680 | * and PCMCIA sometimes. | |
681 | */ | |
682 | ||
683 | void ide_undecoded_slave(ide_hwif_t *hwif) | |
684 | { | |
685 | ide_drive_t *drive0 = &hwif->drives[0]; | |
686 | ide_drive_t *drive1 = &hwif->drives[1]; | |
687 | ||
688 | if (drive0->present == 0 || drive1->present == 0) | |
689 | return; | |
690 | ||
691 | /* If the models don't match they are not the same product */ | |
692 | if (strcmp(drive0->id->model, drive1->id->model)) | |
693 | return; | |
694 | ||
695 | /* Serial numbers do not match */ | |
696 | if (strncmp(drive0->id->serial_no, drive1->id->serial_no, 20)) | |
697 | return; | |
698 | ||
699 | /* No serial number, thankfully very rare for CF */ | |
700 | if (drive0->id->serial_no[0] == 0) | |
701 | return; | |
702 | ||
703 | /* Appears to be an IDE flash adapter with decode bugs */ | |
704 | printk(KERN_WARNING "ide-probe: ignoring undecoded slave\n"); | |
705 | ||
706 | drive1->present = 0; | |
707 | } | |
708 | ||
709 | EXPORT_SYMBOL_GPL(ide_undecoded_slave); | |
710 | ||
711 | /* | |
712 | * This routine only knows how to look for drive units 0 and 1 | |
713 | * on an interface, so any setting of MAX_DRIVES > 2 won't work here. | |
714 | */ | |
fd9bb539 | 715 | static void probe_hwif(ide_hwif_t *hwif) |
1da177e4 | 716 | { |
1da177e4 LT |
717 | unsigned long flags; |
718 | unsigned int irqd; | |
b140b99c | 719 | int unit; |
1da177e4 LT |
720 | |
721 | if (hwif->noprobe) | |
722 | return; | |
723 | ||
724 | if ((hwif->chipset != ide_4drives || !hwif->mate || !hwif->mate->present) && | |
725 | (ide_hwif_request_regions(hwif))) { | |
726 | u16 msgout = 0; | |
727 | for (unit = 0; unit < MAX_DRIVES; ++unit) { | |
728 | ide_drive_t *drive = &hwif->drives[unit]; | |
729 | if (drive->present) { | |
730 | drive->present = 0; | |
731 | printk(KERN_ERR "%s: ERROR, PORTS ALREADY IN USE\n", | |
732 | drive->name); | |
733 | msgout = 1; | |
734 | } | |
735 | } | |
736 | if (!msgout) | |
737 | printk(KERN_ERR "%s: ports already in use, skipping probe\n", | |
738 | hwif->name); | |
739 | return; | |
740 | } | |
741 | ||
742 | /* | |
743 | * We must always disable IRQ, as probe_for_drive will assert IRQ, but | |
744 | * we'll install our IRQ driver much later... | |
745 | */ | |
746 | irqd = hwif->irq; | |
747 | if (irqd) | |
748 | disable_irq(hwif->irq); | |
749 | ||
750 | local_irq_set(flags); | |
751 | ||
752 | /* This is needed on some PPCs and a bunch of BIOS-less embedded | |
753 | * platforms. Typical cases are: | |
754 | * | |
755 | * - The firmware hard reset the disk before booting the kernel, | |
756 | * the drive is still doing it's poweron-reset sequence, that | |
757 | * can take up to 30 seconds | |
758 | * - The firmware does nothing (or no firmware), the device is | |
759 | * still in POST state (same as above actually). | |
760 | * - Some CD/DVD/Writer combo drives tend to drive the bus during | |
761 | * their reset sequence even when they are non-selected slave | |
762 | * devices, thus preventing discovery of the main HD | |
763 | * | |
764 | * Doing this wait-for-busy should not harm any existing configuration | |
765 | * (at least things won't be worse than what current code does, that | |
766 | * is blindly go & talk to the drive) and fix some issues like the | |
767 | * above. | |
768 | * | |
769 | * BenH. | |
770 | */ | |
771 | if (wait_hwif_ready(hwif) == -EBUSY) | |
772 | printk(KERN_DEBUG "%s: Wait for ready failed before probe !\n", hwif->name); | |
773 | ||
774 | /* | |
b140b99c | 775 | * Need to probe slave device first to make it release PDIAG-. |
1da177e4 | 776 | */ |
b140b99c | 777 | for (unit = MAX_DRIVES - 1; unit >= 0; unit--) { |
1da177e4 LT |
778 | ide_drive_t *drive = &hwif->drives[unit]; |
779 | drive->dn = (hwif->channel ? 2 : 0) + unit; | |
780 | (void) probe_for_drive(drive); | |
781 | if (drive->present && !hwif->present) { | |
782 | hwif->present = 1; | |
783 | if (hwif->chipset != ide_4drives || | |
784 | !hwif->mate || | |
785 | !hwif->mate->present) { | |
786 | hwif_register(hwif); | |
787 | } | |
788 | } | |
789 | } | |
790 | if (hwif->io_ports[IDE_CONTROL_OFFSET] && hwif->reset) { | |
791 | unsigned long timeout = jiffies + WAIT_WORSTCASE; | |
792 | u8 stat; | |
793 | ||
794 | printk(KERN_WARNING "%s: reset\n", hwif->name); | |
795 | hwif->OUTB(12, hwif->io_ports[IDE_CONTROL_OFFSET]); | |
796 | udelay(10); | |
797 | hwif->OUTB(8, hwif->io_ports[IDE_CONTROL_OFFSET]); | |
798 | do { | |
799 | msleep(50); | |
800 | stat = hwif->INB(hwif->io_ports[IDE_STATUS_OFFSET]); | |
801 | } while ((stat & BUSY_STAT) && time_after(timeout, jiffies)); | |
802 | ||
803 | } | |
804 | local_irq_restore(flags); | |
805 | /* | |
806 | * Use cached IRQ number. It might be (and is...) changed by probe | |
807 | * code above | |
808 | */ | |
809 | if (irqd) | |
810 | enable_irq(irqd); | |
811 | ||
812 | if (!hwif->present) { | |
813 | ide_hwif_release_regions(hwif); | |
814 | return; | |
815 | } | |
816 | ||
fd9bb539 BZ |
817 | if (hwif->fixup) |
818 | hwif->fixup(hwif); | |
0380dad4 | 819 | |
1da177e4 LT |
820 | for (unit = 0; unit < MAX_DRIVES; ++unit) { |
821 | ide_drive_t *drive = &hwif->drives[unit]; | |
822 | ||
823 | if (drive->present) { | |
26bcb879 BZ |
824 | if (drive->autotune == IDE_TUNE_AUTO) |
825 | ide_set_max_pio(drive); | |
1da177e4 LT |
826 | |
827 | if (drive->autotune != IDE_TUNE_DEFAULT && | |
828 | drive->autotune != IDE_TUNE_AUTO) | |
829 | continue; | |
830 | ||
831 | drive->nice1 = 1; | |
832 | ||
0ae2e178 | 833 | if (hwif->ide_dma_on) { |
1da177e4 LT |
834 | /* |
835 | * Force DMAing for the beginning of the check. | |
836 | * Some chipsets appear to do interesting | |
837 | * things, if not checked and cleared. | |
838 | * PARANOIA!!! | |
839 | */ | |
7469aaf6 | 840 | hwif->dma_off_quietly(drive); |
8c0697cc | 841 | ide_set_dma(drive); |
1da177e4 LT |
842 | } |
843 | } | |
844 | } | |
208a08f7 KG |
845 | |
846 | for (unit = 0; unit < MAX_DRIVES; ++unit) { | |
847 | ide_drive_t *drive = &hwif->drives[unit]; | |
848 | ||
849 | if (hwif->no_io_32bit) | |
850 | drive->no_io_32bit = 1; | |
851 | else | |
852 | drive->no_io_32bit = drive->id->dword_io ? 1 : 0; | |
853 | } | |
1da177e4 LT |
854 | } |
855 | ||
856 | static int hwif_init(ide_hwif_t *hwif); | |
9601a607 | 857 | static void hwif_register_devices(ide_hwif_t *hwif); |
1da177e4 | 858 | |
8447d9d5 | 859 | static int probe_hwif_init(ide_hwif_t *hwif) |
1da177e4 | 860 | { |
fd9bb539 | 861 | probe_hwif(hwif); |
1da177e4 LT |
862 | |
863 | if (!hwif_init(hwif)) { | |
864 | printk(KERN_INFO "%s: failed to initialize IDE interface\n", | |
865 | hwif->name); | |
866 | return -1; | |
867 | } | |
868 | ||
9601a607 BZ |
869 | if (hwif->present) |
870 | hwif_register_devices(hwif); | |
349ae23f | 871 | |
1da177e4 LT |
872 | return 0; |
873 | } | |
874 | ||
1da177e4 LT |
875 | #if MAX_HWIFS > 1 |
876 | /* | |
877 | * save_match() is used to simplify logic in init_irq() below. | |
878 | * | |
879 | * A loophole here is that we may not know about a particular | |
880 | * hwif's irq until after that hwif is actually probed/initialized.. | |
881 | * This could be a problem for the case where an hwif is on a | |
882 | * dual interface that requires serialization (eg. cmd640) and another | |
883 | * hwif using one of the same irqs is initialized beforehand. | |
884 | * | |
885 | * This routine detects and reports such situations, but does not fix them. | |
886 | */ | |
887 | static void save_match(ide_hwif_t *hwif, ide_hwif_t *new, ide_hwif_t **match) | |
888 | { | |
889 | ide_hwif_t *m = *match; | |
890 | ||
891 | if (m && m->hwgroup && m->hwgroup != new->hwgroup) { | |
892 | if (!new->hwgroup) | |
893 | return; | |
894 | printk("%s: potential irq problem with %s and %s\n", | |
895 | hwif->name, new->name, m->name); | |
896 | } | |
897 | if (!m || m->irq != hwif->irq) /* don't undo a prior perfect match */ | |
898 | *match = new; | |
899 | } | |
900 | #endif /* MAX_HWIFS > 1 */ | |
901 | ||
902 | /* | |
903 | * init request queue | |
904 | */ | |
905 | static int ide_init_queue(ide_drive_t *drive) | |
906 | { | |
165125e1 | 907 | struct request_queue *q; |
1da177e4 LT |
908 | ide_hwif_t *hwif = HWIF(drive); |
909 | int max_sectors = 256; | |
910 | int max_sg_entries = PRD_ENTRIES; | |
911 | ||
912 | /* | |
913 | * Our default set up assumes the normal IDE case, | |
914 | * that is 64K segmenting, standard PRD setup | |
915 | * and LBA28. Some drivers then impose their own | |
916 | * limits and LBA48 we could raise it but as yet | |
917 | * do not. | |
918 | */ | |
1946089a | 919 | |
556e58fe | 920 | q = blk_init_queue_node(do_ide_request, &ide_lock, hwif_to_node(hwif)); |
1da177e4 LT |
921 | if (!q) |
922 | return 1; | |
923 | ||
924 | q->queuedata = drive; | |
925 | blk_queue_segment_boundary(q, 0xffff); | |
926 | ||
927 | if (!hwif->rqsize) { | |
238e4f14 BZ |
928 | if ((hwif->host_flags & IDE_HFLAG_NO_LBA48) || |
929 | (hwif->host_flags & IDE_HFLAG_NO_LBA48_DMA)) | |
1da177e4 LT |
930 | hwif->rqsize = 256; |
931 | else | |
932 | hwif->rqsize = 65536; | |
933 | } | |
934 | if (hwif->rqsize < max_sectors) | |
935 | max_sectors = hwif->rqsize; | |
936 | blk_queue_max_sectors(q, max_sectors); | |
937 | ||
938 | #ifdef CONFIG_PCI | |
939 | /* When we have an IOMMU, we may have a problem where pci_map_sg() | |
940 | * creates segments that don't completely match our boundary | |
941 | * requirements and thus need to be broken up again. Because it | |
942 | * doesn't align properly either, we may actually have to break up | |
943 | * to more segments than what was we got in the first place, a max | |
944 | * worst case is twice as many. | |
945 | * This will be fixed once we teach pci_map_sg() about our boundary | |
946 | * requirements, hopefully soon. *FIXME* | |
947 | */ | |
948 | if (!PCI_DMA_BUS_IS_PHYS) | |
949 | max_sg_entries >>= 1; | |
950 | #endif /* CONFIG_PCI */ | |
951 | ||
952 | blk_queue_max_hw_segments(q, max_sg_entries); | |
953 | blk_queue_max_phys_segments(q, max_sg_entries); | |
954 | ||
955 | /* assign drive queue */ | |
956 | drive->queue = q; | |
957 | ||
958 | /* needs drive->queue to be set */ | |
959 | ide_toggle_bounce(drive, 1); | |
960 | ||
1da177e4 LT |
961 | return 0; |
962 | } | |
963 | ||
964 | /* | |
965 | * This routine sets up the irq for an ide interface, and creates a new | |
966 | * hwgroup for the irq/hwif if none was previously assigned. | |
967 | * | |
968 | * Much of the code is for correctly detecting/handling irq sharing | |
969 | * and irq serialization situations. This is somewhat complex because | |
970 | * it handles static as well as dynamic (PCMCIA) IDE interfaces. | |
1da177e4 LT |
971 | */ |
972 | static int init_irq (ide_hwif_t *hwif) | |
973 | { | |
974 | unsigned int index; | |
975 | ide_hwgroup_t *hwgroup; | |
976 | ide_hwif_t *match = NULL; | |
977 | ||
978 | ||
979 | BUG_ON(in_interrupt()); | |
980 | BUG_ON(irqs_disabled()); | |
556e58fe RT |
981 | BUG_ON(hwif == NULL); |
982 | ||
ef29888e | 983 | mutex_lock(&ide_cfg_mtx); |
1da177e4 LT |
984 | hwif->hwgroup = NULL; |
985 | #if MAX_HWIFS > 1 | |
986 | /* | |
987 | * Group up with any other hwifs that share our irq(s). | |
988 | */ | |
989 | for (index = 0; index < MAX_HWIFS; index++) { | |
990 | ide_hwif_t *h = &ide_hwifs[index]; | |
991 | if (h->hwgroup) { /* scan only initialized hwif's */ | |
992 | if (hwif->irq == h->irq) { | |
993 | hwif->sharing_irq = h->sharing_irq = 1; | |
994 | if (hwif->chipset != ide_pci || | |
995 | h->chipset != ide_pci) { | |
996 | save_match(hwif, h, &match); | |
997 | } | |
998 | } | |
999 | if (hwif->serialized) { | |
1000 | if (hwif->mate && hwif->mate->irq == h->irq) | |
1001 | save_match(hwif, h, &match); | |
1002 | } | |
1003 | if (h->serialized) { | |
1004 | if (h->mate && hwif->irq == h->mate->irq) | |
1005 | save_match(hwif, h, &match); | |
1006 | } | |
1007 | } | |
1008 | } | |
1009 | #endif /* MAX_HWIFS > 1 */ | |
1010 | /* | |
1011 | * If we are still without a hwgroup, then form a new one | |
1012 | */ | |
1013 | if (match) { | |
1014 | hwgroup = match->hwgroup; | |
1015 | hwif->hwgroup = hwgroup; | |
1016 | /* | |
1017 | * Link us into the hwgroup. | |
1018 | * This must be done early, do ensure that unexpected_intr | |
1019 | * can find the hwif and prevent irq storms. | |
1020 | * No drives are attached to the new hwif, choose_drive | |
1021 | * can't do anything stupid (yet). | |
1022 | * Add ourself as the 2nd entry to the hwgroup->hwif | |
1023 | * linked list, the first entry is the hwif that owns | |
1024 | * hwgroup->handler - do not change that. | |
1025 | */ | |
1026 | spin_lock_irq(&ide_lock); | |
1027 | hwif->next = hwgroup->hwif->next; | |
1028 | hwgroup->hwif->next = hwif; | |
1029 | spin_unlock_irq(&ide_lock); | |
1030 | } else { | |
94f6030c CL |
1031 | hwgroup = kmalloc_node(sizeof(ide_hwgroup_t), |
1032 | GFP_KERNEL | __GFP_ZERO, | |
556e58fe | 1033 | hwif_to_node(hwif->drives[0].hwif)); |
1da177e4 LT |
1034 | if (!hwgroup) |
1035 | goto out_up; | |
1036 | ||
1037 | hwif->hwgroup = hwgroup; | |
1038 | ||
1da177e4 LT |
1039 | hwgroup->hwif = hwif->next = hwif; |
1040 | hwgroup->rq = NULL; | |
1041 | hwgroup->handler = NULL; | |
1042 | hwgroup->drive = NULL; | |
1043 | hwgroup->busy = 0; | |
1044 | init_timer(&hwgroup->timer); | |
1045 | hwgroup->timer.function = &ide_timer_expiry; | |
1046 | hwgroup->timer.data = (unsigned long) hwgroup; | |
1047 | } | |
1048 | ||
1049 | /* | |
1050 | * Allocate the irq, if not already obtained for another hwif | |
1051 | */ | |
1052 | if (!match || match->irq != hwif->irq) { | |
7b5da4be | 1053 | int sa = 0; |
1da177e4 | 1054 | #if defined(__mc68000__) || defined(CONFIG_APUS) |
362537b9 | 1055 | sa = IRQF_SHARED; |
1da177e4 LT |
1056 | #endif /* __mc68000__ || CONFIG_APUS */ |
1057 | ||
7b5da4be | 1058 | if (IDE_CHIPSET_IS_PCI(hwif->chipset)) |
362537b9 | 1059 | sa = IRQF_SHARED; |
1da177e4 LT |
1060 | |
1061 | if (hwif->io_ports[IDE_CONTROL_OFFSET]) | |
1062 | /* clear nIEN */ | |
1063 | hwif->OUTB(0x08, hwif->io_ports[IDE_CONTROL_OFFSET]); | |
1064 | ||
1065 | if (request_irq(hwif->irq,&ide_intr,sa,hwif->name,hwgroup)) | |
1066 | goto out_unlink; | |
1067 | } | |
1068 | ||
1069 | /* | |
1070 | * For any present drive: | |
1071 | * - allocate the block device queue | |
1072 | * - link drive into the hwgroup | |
1073 | */ | |
1074 | for (index = 0; index < MAX_DRIVES; ++index) { | |
1075 | ide_drive_t *drive = &hwif->drives[index]; | |
1076 | if (!drive->present) | |
1077 | continue; | |
1078 | if (ide_init_queue(drive)) { | |
1079 | printk(KERN_ERR "ide: failed to init %s\n",drive->name); | |
1080 | continue; | |
1081 | } | |
1082 | spin_lock_irq(&ide_lock); | |
1083 | if (!hwgroup->drive) { | |
1084 | /* first drive for hwgroup. */ | |
1085 | drive->next = drive; | |
1086 | hwgroup->drive = drive; | |
1087 | hwgroup->hwif = HWIF(hwgroup->drive); | |
1088 | } else { | |
1089 | drive->next = hwgroup->drive->next; | |
1090 | hwgroup->drive->next = drive; | |
1091 | } | |
1092 | spin_unlock_irq(&ide_lock); | |
1093 | } | |
1094 | ||
c6387a48 | 1095 | #if !defined(__mc68000__) && !defined(CONFIG_APUS) |
1da177e4 LT |
1096 | printk("%s at 0x%03lx-0x%03lx,0x%03lx on irq %d", hwif->name, |
1097 | hwif->io_ports[IDE_DATA_OFFSET], | |
1098 | hwif->io_ports[IDE_DATA_OFFSET]+7, | |
1099 | hwif->io_ports[IDE_CONTROL_OFFSET], hwif->irq); | |
1da177e4 LT |
1100 | #else |
1101 | printk("%s at 0x%08lx on irq %d", hwif->name, | |
1102 | hwif->io_ports[IDE_DATA_OFFSET], hwif->irq); | |
1103 | #endif /* __mc68000__ && CONFIG_APUS */ | |
1104 | if (match) | |
1105 | printk(" (%sed with %s)", | |
1106 | hwif->sharing_irq ? "shar" : "serializ", match->name); | |
1107 | printk("\n"); | |
ef29888e | 1108 | mutex_unlock(&ide_cfg_mtx); |
1da177e4 LT |
1109 | return 0; |
1110 | out_unlink: | |
1111 | spin_lock_irq(&ide_lock); | |
1112 | if (hwif->next == hwif) { | |
1113 | BUG_ON(match); | |
1114 | BUG_ON(hwgroup->hwif != hwif); | |
1115 | kfree(hwgroup); | |
1116 | } else { | |
1117 | ide_hwif_t *g; | |
1118 | g = hwgroup->hwif; | |
1119 | while (g->next != hwif) | |
1120 | g = g->next; | |
1121 | g->next = hwif->next; | |
1122 | if (hwgroup->hwif == hwif) { | |
1123 | /* Impossible. */ | |
1124 | printk(KERN_ERR "Duh. Uninitialized hwif listed as active hwif.\n"); | |
1125 | hwgroup->hwif = g; | |
1126 | } | |
1127 | BUG_ON(hwgroup->hwif == hwif); | |
1128 | } | |
1129 | spin_unlock_irq(&ide_lock); | |
1130 | out_up: | |
ef29888e | 1131 | mutex_unlock(&ide_cfg_mtx); |
1da177e4 LT |
1132 | return 1; |
1133 | } | |
1134 | ||
1135 | static int ata_lock(dev_t dev, void *data) | |
1136 | { | |
1137 | /* FIXME: we want to pin hwif down */ | |
1138 | return 0; | |
1139 | } | |
1140 | ||
1141 | static struct kobject *ata_probe(dev_t dev, int *part, void *data) | |
1142 | { | |
1143 | ide_hwif_t *hwif = data; | |
1144 | int unit = *part >> PARTN_BITS; | |
1145 | ide_drive_t *drive = &hwif->drives[unit]; | |
1146 | if (!drive->present) | |
1147 | return NULL; | |
1148 | ||
1149 | if (drive->media == ide_disk) | |
1150 | request_module("ide-disk"); | |
1151 | if (drive->scsi) | |
1152 | request_module("ide-scsi"); | |
1153 | if (drive->media == ide_cdrom || drive->media == ide_optical) | |
1154 | request_module("ide-cd"); | |
1155 | if (drive->media == ide_tape) | |
1156 | request_module("ide-tape"); | |
1157 | if (drive->media == ide_floppy) | |
1158 | request_module("ide-floppy"); | |
1159 | ||
1160 | return NULL; | |
1161 | } | |
1162 | ||
1163 | static struct kobject *exact_match(dev_t dev, int *part, void *data) | |
1164 | { | |
1165 | struct gendisk *p = data; | |
1166 | *part &= (1 << PARTN_BITS) - 1; | |
edfaa7c3 | 1167 | return &p->dev.kobj; |
1da177e4 LT |
1168 | } |
1169 | ||
1170 | static int exact_lock(dev_t dev, void *data) | |
1171 | { | |
1172 | struct gendisk *p = data; | |
1173 | ||
1174 | if (!get_disk(p)) | |
1175 | return -1; | |
1176 | return 0; | |
1177 | } | |
1178 | ||
1179 | void ide_register_region(struct gendisk *disk) | |
1180 | { | |
1181 | blk_register_region(MKDEV(disk->major, disk->first_minor), | |
1182 | disk->minors, NULL, exact_match, exact_lock, disk); | |
1183 | } | |
1184 | ||
1185 | EXPORT_SYMBOL_GPL(ide_register_region); | |
1186 | ||
1187 | void ide_unregister_region(struct gendisk *disk) | |
1188 | { | |
1189 | blk_unregister_region(MKDEV(disk->major, disk->first_minor), | |
1190 | disk->minors); | |
1191 | } | |
1192 | ||
1193 | EXPORT_SYMBOL_GPL(ide_unregister_region); | |
1194 | ||
1195 | void ide_init_disk(struct gendisk *disk, ide_drive_t *drive) | |
1196 | { | |
1197 | ide_hwif_t *hwif = drive->hwif; | |
1198 | unsigned int unit = (drive->select.all >> 4) & 1; | |
1199 | ||
1200 | disk->major = hwif->major; | |
1201 | disk->first_minor = unit << PARTN_BITS; | |
1202 | sprintf(disk->disk_name, "hd%c", 'a' + hwif->index * MAX_DRIVES + unit); | |
1203 | disk->queue = drive->queue; | |
1204 | } | |
1205 | ||
1206 | EXPORT_SYMBOL_GPL(ide_init_disk); | |
1207 | ||
8604affd BZ |
1208 | static void ide_remove_drive_from_hwgroup(ide_drive_t *drive) |
1209 | { | |
1210 | ide_hwgroup_t *hwgroup = drive->hwif->hwgroup; | |
1211 | ||
1212 | if (drive == drive->next) { | |
1213 | /* special case: last drive from hwgroup. */ | |
1214 | BUG_ON(hwgroup->drive != drive); | |
1215 | hwgroup->drive = NULL; | |
1216 | } else { | |
1217 | ide_drive_t *walk; | |
1218 | ||
1219 | walk = hwgroup->drive; | |
1220 | while (walk->next != drive) | |
1221 | walk = walk->next; | |
1222 | walk->next = drive->next; | |
1223 | if (hwgroup->drive == drive) { | |
1224 | hwgroup->drive = drive->next; | |
1225 | hwgroup->hwif = hwgroup->drive->hwif; | |
1226 | } | |
1227 | } | |
1228 | BUG_ON(hwgroup->drive == drive); | |
1229 | } | |
1230 | ||
1da177e4 LT |
1231 | static void drive_release_dev (struct device *dev) |
1232 | { | |
1233 | ide_drive_t *drive = container_of(dev, ide_drive_t, gendev); | |
1234 | ||
8604affd | 1235 | spin_lock_irq(&ide_lock); |
8604affd | 1236 | ide_remove_drive_from_hwgroup(drive); |
6044ec88 JJ |
1237 | kfree(drive->id); |
1238 | drive->id = NULL; | |
8604affd BZ |
1239 | drive->present = 0; |
1240 | /* Messed up locking ... */ | |
1241 | spin_unlock_irq(&ide_lock); | |
1242 | blk_cleanup_queue(drive->queue); | |
1243 | spin_lock_irq(&ide_lock); | |
1244 | drive->queue = NULL; | |
1245 | spin_unlock_irq(&ide_lock); | |
1246 | ||
f36d4024 | 1247 | complete(&drive->gendev_rel_comp); |
1da177e4 LT |
1248 | } |
1249 | ||
1250 | /* | |
1251 | * init_gendisk() (as opposed to ide_geninit) is called for each major device, | |
1252 | * after probing for drives, to allocate partition tables and other data | |
1253 | * structures needed for the routines in genhd.c. ide_geninit() gets called | |
1254 | * somewhat later, during the partition check. | |
1255 | */ | |
1256 | static void init_gendisk (ide_hwif_t *hwif) | |
1257 | { | |
1258 | unsigned int unit; | |
1259 | ||
1260 | for (unit = 0; unit < MAX_DRIVES; ++unit) { | |
1261 | ide_drive_t * drive = &hwif->drives[unit]; | |
1262 | ide_add_generic_settings(drive); | |
1263 | snprintf(drive->gendev.bus_id,BUS_ID_SIZE,"%u.%u", | |
1264 | hwif->index,unit); | |
1265 | drive->gendev.parent = &hwif->gendev; | |
1266 | drive->gendev.bus = &ide_bus_type; | |
1267 | drive->gendev.driver_data = drive; | |
1268 | drive->gendev.release = drive_release_dev; | |
1da177e4 LT |
1269 | } |
1270 | blk_register_region(MKDEV(hwif->major, 0), MAX_DRIVES << PARTN_BITS, | |
1271 | THIS_MODULE, ata_probe, ata_lock, hwif); | |
1272 | } | |
1273 | ||
1274 | static int hwif_init(ide_hwif_t *hwif) | |
1275 | { | |
1276 | int old_irq; | |
1277 | ||
1278 | /* Return success if no device is connected */ | |
1279 | if (!hwif->present) | |
1280 | return 1; | |
1281 | ||
1282 | if (!hwif->irq) { | |
1283 | if (!(hwif->irq = ide_default_irq(hwif->io_ports[IDE_DATA_OFFSET]))) | |
1284 | { | |
1285 | printk("%s: DISABLED, NO IRQ\n", hwif->name); | |
1286 | return (hwif->present = 0); | |
1287 | } | |
1288 | } | |
1289 | #ifdef CONFIG_BLK_DEV_HD | |
1290 | if (hwif->irq == HD_IRQ && hwif->io_ports[IDE_DATA_OFFSET] != HD_DATA) { | |
1291 | printk("%s: CANNOT SHARE IRQ WITH OLD " | |
1292 | "HARDDISK DRIVER (hd.c)\n", hwif->name); | |
1293 | return (hwif->present = 0); | |
1294 | } | |
1295 | #endif /* CONFIG_BLK_DEV_HD */ | |
1296 | ||
1297 | /* we set it back to 1 if all is ok below */ | |
1298 | hwif->present = 0; | |
1299 | ||
1300 | if (register_blkdev(hwif->major, hwif->name)) | |
1301 | return 0; | |
1302 | ||
1303 | if (!hwif->sg_max_nents) | |
1304 | hwif->sg_max_nents = PRD_ENTRIES; | |
1305 | ||
45711f1a | 1306 | hwif->sg_table = kmalloc(sizeof(struct scatterlist)*hwif->sg_max_nents, |
1da177e4 LT |
1307 | GFP_KERNEL); |
1308 | if (!hwif->sg_table) { | |
1309 | printk(KERN_ERR "%s: unable to allocate SG table.\n", hwif->name); | |
1310 | goto out; | |
1311 | } | |
45711f1a JA |
1312 | |
1313 | sg_init_table(hwif->sg_table, hwif->sg_max_nents); | |
1da177e4 LT |
1314 | |
1315 | if (init_irq(hwif) == 0) | |
1316 | goto done; | |
1317 | ||
1318 | old_irq = hwif->irq; | |
1319 | /* | |
1320 | * It failed to initialise. Find the default IRQ for | |
1321 | * this port and try that. | |
1322 | */ | |
1323 | if (!(hwif->irq = ide_default_irq(hwif->io_ports[IDE_DATA_OFFSET]))) { | |
1324 | printk("%s: Disabled unable to get IRQ %d.\n", | |
1325 | hwif->name, old_irq); | |
1326 | goto out; | |
1327 | } | |
1328 | if (init_irq(hwif)) { | |
1329 | printk("%s: probed IRQ %d and default IRQ %d failed.\n", | |
1330 | hwif->name, old_irq, hwif->irq); | |
1331 | goto out; | |
1332 | } | |
1333 | printk("%s: probed IRQ %d failed, using default.\n", | |
1334 | hwif->name, hwif->irq); | |
1335 | ||
1336 | done: | |
1337 | init_gendisk(hwif); | |
e3a59b4d HR |
1338 | |
1339 | ide_acpi_init(hwif); | |
1340 | ||
1da177e4 LT |
1341 | hwif->present = 1; /* success */ |
1342 | return 1; | |
1343 | ||
1344 | out: | |
1345 | unregister_blkdev(hwif->major, hwif->name); | |
1346 | return 0; | |
1347 | } | |
1348 | ||
9601a607 BZ |
1349 | static void hwif_register_devices(ide_hwif_t *hwif) |
1350 | { | |
1351 | unsigned int i; | |
1352 | ||
1353 | for (i = 0; i < MAX_DRIVES; i++) { | |
1354 | ide_drive_t *drive = &hwif->drives[i]; | |
1355 | ||
1356 | if (drive->present) { | |
1357 | int ret = device_register(&drive->gendev); | |
1358 | ||
1359 | if (ret < 0) | |
1360 | printk(KERN_WARNING "IDE: %s: " | |
1361 | "device_register error: %d\n", | |
1362 | __FUNCTION__, ret); | |
1363 | } | |
1364 | } | |
1365 | } | |
1366 | ||
1da177e4 LT |
1367 | int ideprobe_init (void) |
1368 | { | |
1369 | unsigned int index; | |
1370 | int probe[MAX_HWIFS]; | |
1371 | ||
1372 | memset(probe, 0, MAX_HWIFS * sizeof(int)); | |
1373 | for (index = 0; index < MAX_HWIFS; ++index) | |
1374 | probe[index] = !ide_hwifs[index].present; | |
1375 | ||
1376 | for (index = 0; index < MAX_HWIFS; ++index) | |
1377 | if (probe[index]) | |
fd9bb539 | 1378 | probe_hwif(&ide_hwifs[index]); |
1da177e4 LT |
1379 | for (index = 0; index < MAX_HWIFS; ++index) |
1380 | if (probe[index]) | |
1381 | hwif_init(&ide_hwifs[index]); | |
1382 | for (index = 0; index < MAX_HWIFS; ++index) { | |
1383 | if (probe[index]) { | |
1384 | ide_hwif_t *hwif = &ide_hwifs[index]; | |
1da177e4 LT |
1385 | if (!hwif->present) |
1386 | continue; | |
1387 | if (hwif->chipset == ide_unknown || hwif->chipset == ide_forced) | |
1388 | hwif->chipset = ide_generic; | |
9601a607 | 1389 | hwif_register_devices(hwif); |
1da177e4 LT |
1390 | } |
1391 | } | |
5cbf79cd BZ |
1392 | for (index = 0; index < MAX_HWIFS; ++index) |
1393 | if (probe[index]) | |
1394 | ide_proc_register_port(&ide_hwifs[index]); | |
1da177e4 LT |
1395 | return 0; |
1396 | } | |
1397 | ||
1398 | EXPORT_SYMBOL_GPL(ideprobe_init); | |
8447d9d5 BZ |
1399 | |
1400 | int ide_device_add(u8 idx[4]) | |
1401 | { | |
1402 | int i, rc = 0; | |
1403 | ||
1404 | for (i = 0; i < 4; i++) { | |
1405 | if (idx[i] != 0xff) | |
1406 | rc |= probe_hwif_init(&ide_hwifs[idx[i]]); | |
1407 | } | |
1408 | ||
1409 | for (i = 0; i < 4; i++) { | |
1410 | if (idx[i] != 0xff) | |
1411 | ide_proc_register_port(&ide_hwifs[idx[i]]); | |
1412 | } | |
1413 | ||
1414 | return rc; | |
1415 | } | |
1416 | ||
1417 | EXPORT_SYMBOL_GPL(ide_device_add); |