ide-probe.c 37 KB

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