ide-probe.c 36 KB

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