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