ide-probe.c 36 KB

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