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