ide-probe.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419
  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. /* 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. device_register(&hwif->gendev);
  576. }
  577. static int wait_hwif_ready(ide_hwif_t *hwif)
  578. {
  579. int rc;
  580. printk(KERN_DEBUG "Probing IDE interface %s...\n", hwif->name);
  581. /* Let HW settle down a bit from whatever init state we
  582. * come from */
  583. mdelay(2);
  584. /* Wait for BSY bit to go away, spec timeout is 30 seconds,
  585. * I know of at least one disk who takes 31 seconds, I use 35
  586. * here to be safe
  587. */
  588. rc = ide_wait_not_busy(hwif, 35000);
  589. if (rc)
  590. return rc;
  591. /* Now make sure both master & slave are ready */
  592. SELECT_DRIVE(&hwif->drives[0]);
  593. hwif->OUTB(8, hwif->io_ports[IDE_CONTROL_OFFSET]);
  594. mdelay(2);
  595. rc = ide_wait_not_busy(hwif, 35000);
  596. if (rc)
  597. return rc;
  598. SELECT_DRIVE(&hwif->drives[1]);
  599. hwif->OUTB(8, hwif->io_ports[IDE_CONTROL_OFFSET]);
  600. mdelay(2);
  601. rc = ide_wait_not_busy(hwif, 35000);
  602. /* Exit function with master reselected (let's be sane) */
  603. SELECT_DRIVE(&hwif->drives[0]);
  604. return rc;
  605. }
  606. /**
  607. * ide_undecoded_slave - look for bad CF adapters
  608. * @hwif: interface
  609. *
  610. * Analyse the drives on the interface and attempt to decide if we
  611. * have the same drive viewed twice. This occurs with crap CF adapters
  612. * and PCMCIA sometimes.
  613. */
  614. void ide_undecoded_slave(ide_hwif_t *hwif)
  615. {
  616. ide_drive_t *drive0 = &hwif->drives[0];
  617. ide_drive_t *drive1 = &hwif->drives[1];
  618. if (drive0->present == 0 || drive1->present == 0)
  619. return;
  620. /* If the models don't match they are not the same product */
  621. if (strcmp(drive0->id->model, drive1->id->model))
  622. return;
  623. /* Serial numbers do not match */
  624. if (strncmp(drive0->id->serial_no, drive1->id->serial_no, 20))
  625. return;
  626. /* No serial number, thankfully very rare for CF */
  627. if (drive0->id->serial_no[0] == 0)
  628. return;
  629. /* Appears to be an IDE flash adapter with decode bugs */
  630. printk(KERN_WARNING "ide-probe: ignoring undecoded slave\n");
  631. drive1->present = 0;
  632. }
  633. EXPORT_SYMBOL_GPL(ide_undecoded_slave);
  634. /*
  635. * This routine only knows how to look for drive units 0 and 1
  636. * on an interface, so any setting of MAX_DRIVES > 2 won't work here.
  637. */
  638. static void probe_hwif(ide_hwif_t *hwif)
  639. {
  640. unsigned int unit;
  641. unsigned long flags;
  642. unsigned int irqd;
  643. if (hwif->noprobe)
  644. return;
  645. if ((hwif->chipset != ide_4drives || !hwif->mate || !hwif->mate->present) &&
  646. (ide_hwif_request_regions(hwif))) {
  647. u16 msgout = 0;
  648. for (unit = 0; unit < MAX_DRIVES; ++unit) {
  649. ide_drive_t *drive = &hwif->drives[unit];
  650. if (drive->present) {
  651. drive->present = 0;
  652. printk(KERN_ERR "%s: ERROR, PORTS ALREADY IN USE\n",
  653. drive->name);
  654. msgout = 1;
  655. }
  656. }
  657. if (!msgout)
  658. printk(KERN_ERR "%s: ports already in use, skipping probe\n",
  659. hwif->name);
  660. return;
  661. }
  662. /*
  663. * We must always disable IRQ, as probe_for_drive will assert IRQ, but
  664. * we'll install our IRQ driver much later...
  665. */
  666. irqd = hwif->irq;
  667. if (irqd)
  668. disable_irq(hwif->irq);
  669. local_irq_set(flags);
  670. /* This is needed on some PPCs and a bunch of BIOS-less embedded
  671. * platforms. Typical cases are:
  672. *
  673. * - The firmware hard reset the disk before booting the kernel,
  674. * the drive is still doing it's poweron-reset sequence, that
  675. * can take up to 30 seconds
  676. * - The firmware does nothing (or no firmware), the device is
  677. * still in POST state (same as above actually).
  678. * - Some CD/DVD/Writer combo drives tend to drive the bus during
  679. * their reset sequence even when they are non-selected slave
  680. * devices, thus preventing discovery of the main HD
  681. *
  682. * Doing this wait-for-busy should not harm any existing configuration
  683. * (at least things won't be worse than what current code does, that
  684. * is blindly go & talk to the drive) and fix some issues like the
  685. * above.
  686. *
  687. * BenH.
  688. */
  689. if (wait_hwif_ready(hwif) == -EBUSY)
  690. printk(KERN_DEBUG "%s: Wait for ready failed before probe !\n", hwif->name);
  691. /*
  692. * Second drive should only exist if first drive was found,
  693. * but a lot of cdrom drives are configured as single slaves.
  694. */
  695. for (unit = 0; unit < MAX_DRIVES; ++unit) {
  696. ide_drive_t *drive = &hwif->drives[unit];
  697. drive->dn = (hwif->channel ? 2 : 0) + unit;
  698. (void) probe_for_drive(drive);
  699. if (drive->present && !hwif->present) {
  700. hwif->present = 1;
  701. if (hwif->chipset != ide_4drives ||
  702. !hwif->mate ||
  703. !hwif->mate->present) {
  704. hwif_register(hwif);
  705. }
  706. }
  707. }
  708. if (hwif->io_ports[IDE_CONTROL_OFFSET] && hwif->reset) {
  709. unsigned long timeout = jiffies + WAIT_WORSTCASE;
  710. u8 stat;
  711. printk(KERN_WARNING "%s: reset\n", hwif->name);
  712. hwif->OUTB(12, hwif->io_ports[IDE_CONTROL_OFFSET]);
  713. udelay(10);
  714. hwif->OUTB(8, hwif->io_ports[IDE_CONTROL_OFFSET]);
  715. do {
  716. msleep(50);
  717. stat = hwif->INB(hwif->io_ports[IDE_STATUS_OFFSET]);
  718. } while ((stat & BUSY_STAT) && time_after(timeout, jiffies));
  719. }
  720. local_irq_restore(flags);
  721. /*
  722. * Use cached IRQ number. It might be (and is...) changed by probe
  723. * code above
  724. */
  725. if (irqd)
  726. enable_irq(irqd);
  727. if (!hwif->present) {
  728. ide_hwif_release_regions(hwif);
  729. return;
  730. }
  731. for (unit = 0; unit < MAX_DRIVES; ++unit) {
  732. ide_drive_t *drive = &hwif->drives[unit];
  733. if (drive->present) {
  734. if (hwif->tuneproc != NULL &&
  735. drive->autotune == IDE_TUNE_AUTO)
  736. /* auto-tune PIO mode */
  737. hwif->tuneproc(drive, 255);
  738. if (drive->autotune != IDE_TUNE_DEFAULT &&
  739. drive->autotune != IDE_TUNE_AUTO)
  740. continue;
  741. drive->nice1 = 1;
  742. /*
  743. * MAJOR HACK BARF :-/
  744. *
  745. * FIXME: chipsets own this cruft!
  746. */
  747. /*
  748. * Move here to prevent module loading clashing.
  749. */
  750. // drive->autodma = hwif->autodma;
  751. if (hwif->ide_dma_check) {
  752. /*
  753. * Force DMAing for the beginning of the check.
  754. * Some chipsets appear to do interesting
  755. * things, if not checked and cleared.
  756. * PARANOIA!!!
  757. */
  758. hwif->ide_dma_off_quietly(drive);
  759. #ifdef CONFIG_IDEDMA_ONLYDISK
  760. if (drive->media == ide_disk)
  761. #endif
  762. hwif->ide_dma_check(drive);
  763. }
  764. }
  765. }
  766. for (unit = 0; unit < MAX_DRIVES; ++unit) {
  767. ide_drive_t *drive = &hwif->drives[unit];
  768. if (hwif->no_io_32bit)
  769. drive->no_io_32bit = 1;
  770. else
  771. drive->no_io_32bit = drive->id->dword_io ? 1 : 0;
  772. }
  773. }
  774. static int hwif_init(ide_hwif_t *hwif);
  775. int probe_hwif_init_with_fixup(ide_hwif_t *hwif, void (*fixup)(ide_hwif_t *hwif))
  776. {
  777. probe_hwif(hwif);
  778. if (fixup)
  779. fixup(hwif);
  780. if (!hwif_init(hwif)) {
  781. printk(KERN_INFO "%s: failed to initialize IDE interface\n",
  782. hwif->name);
  783. return -1;
  784. }
  785. if (hwif->present) {
  786. u16 unit = 0;
  787. for (unit = 0; unit < MAX_DRIVES; ++unit) {
  788. ide_drive_t *drive = &hwif->drives[unit];
  789. /* For now don't attach absent drives, we may
  790. want them on default or a new "empty" class
  791. for hotplug reprobing ? */
  792. if (drive->present) {
  793. device_register(&drive->gendev);
  794. }
  795. }
  796. }
  797. return 0;
  798. }
  799. int probe_hwif_init(ide_hwif_t *hwif)
  800. {
  801. return probe_hwif_init_with_fixup(hwif, NULL);
  802. }
  803. EXPORT_SYMBOL(probe_hwif_init);
  804. #if MAX_HWIFS > 1
  805. /*
  806. * save_match() is used to simplify logic in init_irq() below.
  807. *
  808. * A loophole here is that we may not know about a particular
  809. * hwif's irq until after that hwif is actually probed/initialized..
  810. * This could be a problem for the case where an hwif is on a
  811. * dual interface that requires serialization (eg. cmd640) and another
  812. * hwif using one of the same irqs is initialized beforehand.
  813. *
  814. * This routine detects and reports such situations, but does not fix them.
  815. */
  816. static void save_match(ide_hwif_t *hwif, ide_hwif_t *new, ide_hwif_t **match)
  817. {
  818. ide_hwif_t *m = *match;
  819. if (m && m->hwgroup && m->hwgroup != new->hwgroup) {
  820. if (!new->hwgroup)
  821. return;
  822. printk("%s: potential irq problem with %s and %s\n",
  823. hwif->name, new->name, m->name);
  824. }
  825. if (!m || m->irq != hwif->irq) /* don't undo a prior perfect match */
  826. *match = new;
  827. }
  828. #endif /* MAX_HWIFS > 1 */
  829. /*
  830. * init request queue
  831. */
  832. static int ide_init_queue(ide_drive_t *drive)
  833. {
  834. request_queue_t *q;
  835. ide_hwif_t *hwif = HWIF(drive);
  836. int max_sectors = 256;
  837. int max_sg_entries = PRD_ENTRIES;
  838. /*
  839. * Our default set up assumes the normal IDE case,
  840. * that is 64K segmenting, standard PRD setup
  841. * and LBA28. Some drivers then impose their own
  842. * limits and LBA48 we could raise it but as yet
  843. * do not.
  844. */
  845. q = blk_init_queue_node(do_ide_request, &ide_lock, hwif_to_node(hwif));
  846. if (!q)
  847. return 1;
  848. q->queuedata = drive;
  849. blk_queue_segment_boundary(q, 0xffff);
  850. if (!hwif->rqsize) {
  851. if (hwif->no_lba48 || hwif->no_lba48_dma)
  852. hwif->rqsize = 256;
  853. else
  854. hwif->rqsize = 65536;
  855. }
  856. if (hwif->rqsize < max_sectors)
  857. max_sectors = hwif->rqsize;
  858. blk_queue_max_sectors(q, max_sectors);
  859. #ifdef CONFIG_PCI
  860. /* When we have an IOMMU, we may have a problem where pci_map_sg()
  861. * creates segments that don't completely match our boundary
  862. * requirements and thus need to be broken up again. Because it
  863. * doesn't align properly either, we may actually have to break up
  864. * to more segments than what was we got in the first place, a max
  865. * worst case is twice as many.
  866. * This will be fixed once we teach pci_map_sg() about our boundary
  867. * requirements, hopefully soon. *FIXME*
  868. */
  869. if (!PCI_DMA_BUS_IS_PHYS)
  870. max_sg_entries >>= 1;
  871. #endif /* CONFIG_PCI */
  872. blk_queue_max_hw_segments(q, max_sg_entries);
  873. blk_queue_max_phys_segments(q, max_sg_entries);
  874. /* assign drive queue */
  875. drive->queue = q;
  876. /* needs drive->queue to be set */
  877. ide_toggle_bounce(drive, 1);
  878. /* enable led activity for disk drives only */
  879. if (drive->media == ide_disk && hwif->led_act)
  880. blk_queue_activity_fn(q, hwif->led_act, drive);
  881. return 0;
  882. }
  883. /*
  884. * This routine sets up the irq for an ide interface, and creates a new
  885. * hwgroup for the irq/hwif if none was previously assigned.
  886. *
  887. * Much of the code is for correctly detecting/handling irq sharing
  888. * and irq serialization situations. This is somewhat complex because
  889. * it handles static as well as dynamic (PCMCIA) IDE interfaces.
  890. *
  891. * The IRQF_DISABLED in sa_flags means ide_intr() is always entered with
  892. * interrupts completely disabled. This can be bad for interrupt latency,
  893. * but anything else has led to problems on some machines. We re-enable
  894. * interrupts as much as we can safely do in most places.
  895. */
  896. static int init_irq (ide_hwif_t *hwif)
  897. {
  898. unsigned int index;
  899. ide_hwgroup_t *hwgroup;
  900. ide_hwif_t *match = NULL;
  901. BUG_ON(in_interrupt());
  902. BUG_ON(irqs_disabled());
  903. BUG_ON(hwif == NULL);
  904. down(&ide_cfg_sem);
  905. hwif->hwgroup = NULL;
  906. #if MAX_HWIFS > 1
  907. /*
  908. * Group up with any other hwifs that share our irq(s).
  909. */
  910. for (index = 0; index < MAX_HWIFS; index++) {
  911. ide_hwif_t *h = &ide_hwifs[index];
  912. if (h->hwgroup) { /* scan only initialized hwif's */
  913. if (hwif->irq == h->irq) {
  914. hwif->sharing_irq = h->sharing_irq = 1;
  915. if (hwif->chipset != ide_pci ||
  916. h->chipset != ide_pci) {
  917. save_match(hwif, h, &match);
  918. }
  919. }
  920. if (hwif->serialized) {
  921. if (hwif->mate && hwif->mate->irq == h->irq)
  922. save_match(hwif, h, &match);
  923. }
  924. if (h->serialized) {
  925. if (h->mate && hwif->irq == h->mate->irq)
  926. save_match(hwif, h, &match);
  927. }
  928. }
  929. }
  930. #endif /* MAX_HWIFS > 1 */
  931. /*
  932. * If we are still without a hwgroup, then form a new one
  933. */
  934. if (match) {
  935. hwgroup = match->hwgroup;
  936. hwif->hwgroup = hwgroup;
  937. /*
  938. * Link us into the hwgroup.
  939. * This must be done early, do ensure that unexpected_intr
  940. * can find the hwif and prevent irq storms.
  941. * No drives are attached to the new hwif, choose_drive
  942. * can't do anything stupid (yet).
  943. * Add ourself as the 2nd entry to the hwgroup->hwif
  944. * linked list, the first entry is the hwif that owns
  945. * hwgroup->handler - do not change that.
  946. */
  947. spin_lock_irq(&ide_lock);
  948. hwif->next = hwgroup->hwif->next;
  949. hwgroup->hwif->next = hwif;
  950. spin_unlock_irq(&ide_lock);
  951. } else {
  952. hwgroup = kmalloc_node(sizeof(ide_hwgroup_t), GFP_KERNEL,
  953. hwif_to_node(hwif->drives[0].hwif));
  954. if (!hwgroup)
  955. goto out_up;
  956. hwif->hwgroup = hwgroup;
  957. memset(hwgroup, 0, sizeof(ide_hwgroup_t));
  958. hwgroup->hwif = hwif->next = hwif;
  959. hwgroup->rq = NULL;
  960. hwgroup->handler = NULL;
  961. hwgroup->drive = NULL;
  962. hwgroup->busy = 0;
  963. init_timer(&hwgroup->timer);
  964. hwgroup->timer.function = &ide_timer_expiry;
  965. hwgroup->timer.data = (unsigned long) hwgroup;
  966. }
  967. /*
  968. * Allocate the irq, if not already obtained for another hwif
  969. */
  970. if (!match || match->irq != hwif->irq) {
  971. int sa = IRQF_DISABLED;
  972. #if defined(__mc68000__) || defined(CONFIG_APUS)
  973. sa = IRQF_SHARED;
  974. #endif /* __mc68000__ || CONFIG_APUS */
  975. if (IDE_CHIPSET_IS_PCI(hwif->chipset)) {
  976. sa = IRQF_SHARED;
  977. #ifndef CONFIG_IDEPCI_SHARE_IRQ
  978. sa |= IRQF_DISABLED;
  979. #endif /* CONFIG_IDEPCI_SHARE_IRQ */
  980. }
  981. if (hwif->io_ports[IDE_CONTROL_OFFSET])
  982. /* clear nIEN */
  983. hwif->OUTB(0x08, hwif->io_ports[IDE_CONTROL_OFFSET]);
  984. if (request_irq(hwif->irq,&ide_intr,sa,hwif->name,hwgroup))
  985. goto out_unlink;
  986. }
  987. /*
  988. * For any present drive:
  989. * - allocate the block device queue
  990. * - link drive into the hwgroup
  991. */
  992. for (index = 0; index < MAX_DRIVES; ++index) {
  993. ide_drive_t *drive = &hwif->drives[index];
  994. if (!drive->present)
  995. continue;
  996. if (ide_init_queue(drive)) {
  997. printk(KERN_ERR "ide: failed to init %s\n",drive->name);
  998. continue;
  999. }
  1000. spin_lock_irq(&ide_lock);
  1001. if (!hwgroup->drive) {
  1002. /* first drive for hwgroup. */
  1003. drive->next = drive;
  1004. hwgroup->drive = drive;
  1005. hwgroup->hwif = HWIF(hwgroup->drive);
  1006. } else {
  1007. drive->next = hwgroup->drive->next;
  1008. hwgroup->drive->next = drive;
  1009. }
  1010. spin_unlock_irq(&ide_lock);
  1011. }
  1012. #if !defined(__mc68000__) && !defined(CONFIG_APUS)
  1013. printk("%s at 0x%03lx-0x%03lx,0x%03lx on irq %d", hwif->name,
  1014. hwif->io_ports[IDE_DATA_OFFSET],
  1015. hwif->io_ports[IDE_DATA_OFFSET]+7,
  1016. hwif->io_ports[IDE_CONTROL_OFFSET], hwif->irq);
  1017. #else
  1018. printk("%s at 0x%08lx on irq %d", hwif->name,
  1019. hwif->io_ports[IDE_DATA_OFFSET], hwif->irq);
  1020. #endif /* __mc68000__ && CONFIG_APUS */
  1021. if (match)
  1022. printk(" (%sed with %s)",
  1023. hwif->sharing_irq ? "shar" : "serializ", match->name);
  1024. printk("\n");
  1025. up(&ide_cfg_sem);
  1026. return 0;
  1027. out_unlink:
  1028. spin_lock_irq(&ide_lock);
  1029. if (hwif->next == hwif) {
  1030. BUG_ON(match);
  1031. BUG_ON(hwgroup->hwif != hwif);
  1032. kfree(hwgroup);
  1033. } else {
  1034. ide_hwif_t *g;
  1035. g = hwgroup->hwif;
  1036. while (g->next != hwif)
  1037. g = g->next;
  1038. g->next = hwif->next;
  1039. if (hwgroup->hwif == hwif) {
  1040. /* Impossible. */
  1041. printk(KERN_ERR "Duh. Uninitialized hwif listed as active hwif.\n");
  1042. hwgroup->hwif = g;
  1043. }
  1044. BUG_ON(hwgroup->hwif == hwif);
  1045. }
  1046. spin_unlock_irq(&ide_lock);
  1047. out_up:
  1048. up(&ide_cfg_sem);
  1049. return 1;
  1050. }
  1051. static int ata_lock(dev_t dev, void *data)
  1052. {
  1053. /* FIXME: we want to pin hwif down */
  1054. return 0;
  1055. }
  1056. static struct kobject *ata_probe(dev_t dev, int *part, void *data)
  1057. {
  1058. ide_hwif_t *hwif = data;
  1059. int unit = *part >> PARTN_BITS;
  1060. ide_drive_t *drive = &hwif->drives[unit];
  1061. if (!drive->present)
  1062. return NULL;
  1063. if (drive->media == ide_disk)
  1064. request_module("ide-disk");
  1065. if (drive->scsi)
  1066. request_module("ide-scsi");
  1067. if (drive->media == ide_cdrom || drive->media == ide_optical)
  1068. request_module("ide-cd");
  1069. if (drive->media == ide_tape)
  1070. request_module("ide-tape");
  1071. if (drive->media == ide_floppy)
  1072. request_module("ide-floppy");
  1073. return NULL;
  1074. }
  1075. static struct kobject *exact_match(dev_t dev, int *part, void *data)
  1076. {
  1077. struct gendisk *p = data;
  1078. *part &= (1 << PARTN_BITS) - 1;
  1079. return &p->kobj;
  1080. }
  1081. static int exact_lock(dev_t dev, void *data)
  1082. {
  1083. struct gendisk *p = data;
  1084. if (!get_disk(p))
  1085. return -1;
  1086. return 0;
  1087. }
  1088. void ide_register_region(struct gendisk *disk)
  1089. {
  1090. blk_register_region(MKDEV(disk->major, disk->first_minor),
  1091. disk->minors, NULL, exact_match, exact_lock, disk);
  1092. }
  1093. EXPORT_SYMBOL_GPL(ide_register_region);
  1094. void ide_unregister_region(struct gendisk *disk)
  1095. {
  1096. blk_unregister_region(MKDEV(disk->major, disk->first_minor),
  1097. disk->minors);
  1098. }
  1099. EXPORT_SYMBOL_GPL(ide_unregister_region);
  1100. void ide_init_disk(struct gendisk *disk, ide_drive_t *drive)
  1101. {
  1102. ide_hwif_t *hwif = drive->hwif;
  1103. unsigned int unit = (drive->select.all >> 4) & 1;
  1104. disk->major = hwif->major;
  1105. disk->first_minor = unit << PARTN_BITS;
  1106. sprintf(disk->disk_name, "hd%c", 'a' + hwif->index * MAX_DRIVES + unit);
  1107. disk->queue = drive->queue;
  1108. }
  1109. EXPORT_SYMBOL_GPL(ide_init_disk);
  1110. static void ide_remove_drive_from_hwgroup(ide_drive_t *drive)
  1111. {
  1112. ide_hwgroup_t *hwgroup = drive->hwif->hwgroup;
  1113. if (drive == drive->next) {
  1114. /* special case: last drive from hwgroup. */
  1115. BUG_ON(hwgroup->drive != drive);
  1116. hwgroup->drive = NULL;
  1117. } else {
  1118. ide_drive_t *walk;
  1119. walk = hwgroup->drive;
  1120. while (walk->next != drive)
  1121. walk = walk->next;
  1122. walk->next = drive->next;
  1123. if (hwgroup->drive == drive) {
  1124. hwgroup->drive = drive->next;
  1125. hwgroup->hwif = hwgroup->drive->hwif;
  1126. }
  1127. }
  1128. BUG_ON(hwgroup->drive == drive);
  1129. }
  1130. static void drive_release_dev (struct device *dev)
  1131. {
  1132. ide_drive_t *drive = container_of(dev, ide_drive_t, gendev);
  1133. spin_lock_irq(&ide_lock);
  1134. ide_remove_drive_from_hwgroup(drive);
  1135. kfree(drive->id);
  1136. drive->id = NULL;
  1137. drive->present = 0;
  1138. /* Messed up locking ... */
  1139. spin_unlock_irq(&ide_lock);
  1140. blk_cleanup_queue(drive->queue);
  1141. spin_lock_irq(&ide_lock);
  1142. drive->queue = NULL;
  1143. spin_unlock_irq(&ide_lock);
  1144. complete(&drive->gendev_rel_comp);
  1145. }
  1146. /*
  1147. * init_gendisk() (as opposed to ide_geninit) is called for each major device,
  1148. * after probing for drives, to allocate partition tables and other data
  1149. * structures needed for the routines in genhd.c. ide_geninit() gets called
  1150. * somewhat later, during the partition check.
  1151. */
  1152. static void init_gendisk (ide_hwif_t *hwif)
  1153. {
  1154. unsigned int unit;
  1155. for (unit = 0; unit < MAX_DRIVES; ++unit) {
  1156. ide_drive_t * drive = &hwif->drives[unit];
  1157. ide_add_generic_settings(drive);
  1158. snprintf(drive->gendev.bus_id,BUS_ID_SIZE,"%u.%u",
  1159. hwif->index,unit);
  1160. drive->gendev.parent = &hwif->gendev;
  1161. drive->gendev.bus = &ide_bus_type;
  1162. drive->gendev.driver_data = drive;
  1163. drive->gendev.release = drive_release_dev;
  1164. }
  1165. blk_register_region(MKDEV(hwif->major, 0), MAX_DRIVES << PARTN_BITS,
  1166. THIS_MODULE, ata_probe, ata_lock, hwif);
  1167. }
  1168. static int hwif_init(ide_hwif_t *hwif)
  1169. {
  1170. int old_irq;
  1171. /* Return success if no device is connected */
  1172. if (!hwif->present)
  1173. return 1;
  1174. if (!hwif->irq) {
  1175. if (!(hwif->irq = ide_default_irq(hwif->io_ports[IDE_DATA_OFFSET])))
  1176. {
  1177. printk("%s: DISABLED, NO IRQ\n", hwif->name);
  1178. return (hwif->present = 0);
  1179. }
  1180. }
  1181. #ifdef CONFIG_BLK_DEV_HD
  1182. if (hwif->irq == HD_IRQ && hwif->io_ports[IDE_DATA_OFFSET] != HD_DATA) {
  1183. printk("%s: CANNOT SHARE IRQ WITH OLD "
  1184. "HARDDISK DRIVER (hd.c)\n", hwif->name);
  1185. return (hwif->present = 0);
  1186. }
  1187. #endif /* CONFIG_BLK_DEV_HD */
  1188. /* we set it back to 1 if all is ok below */
  1189. hwif->present = 0;
  1190. if (register_blkdev(hwif->major, hwif->name))
  1191. return 0;
  1192. if (!hwif->sg_max_nents)
  1193. hwif->sg_max_nents = PRD_ENTRIES;
  1194. hwif->sg_table = kmalloc(sizeof(struct scatterlist)*hwif->sg_max_nents,
  1195. GFP_KERNEL);
  1196. if (!hwif->sg_table) {
  1197. printk(KERN_ERR "%s: unable to allocate SG table.\n", hwif->name);
  1198. goto out;
  1199. }
  1200. if (init_irq(hwif) == 0)
  1201. goto done;
  1202. old_irq = hwif->irq;
  1203. /*
  1204. * It failed to initialise. Find the default IRQ for
  1205. * this port and try that.
  1206. */
  1207. if (!(hwif->irq = ide_default_irq(hwif->io_ports[IDE_DATA_OFFSET]))) {
  1208. printk("%s: Disabled unable to get IRQ %d.\n",
  1209. hwif->name, old_irq);
  1210. goto out;
  1211. }
  1212. if (init_irq(hwif)) {
  1213. printk("%s: probed IRQ %d and default IRQ %d failed.\n",
  1214. hwif->name, old_irq, hwif->irq);
  1215. goto out;
  1216. }
  1217. printk("%s: probed IRQ %d failed, using default.\n",
  1218. hwif->name, hwif->irq);
  1219. done:
  1220. init_gendisk(hwif);
  1221. hwif->present = 1; /* success */
  1222. return 1;
  1223. out:
  1224. unregister_blkdev(hwif->major, hwif->name);
  1225. return 0;
  1226. }
  1227. int ideprobe_init (void)
  1228. {
  1229. unsigned int index;
  1230. int probe[MAX_HWIFS];
  1231. memset(probe, 0, MAX_HWIFS * sizeof(int));
  1232. for (index = 0; index < MAX_HWIFS; ++index)
  1233. probe[index] = !ide_hwifs[index].present;
  1234. for (index = 0; index < MAX_HWIFS; ++index)
  1235. if (probe[index])
  1236. probe_hwif(&ide_hwifs[index]);
  1237. for (index = 0; index < MAX_HWIFS; ++index)
  1238. if (probe[index])
  1239. hwif_init(&ide_hwifs[index]);
  1240. for (index = 0; index < MAX_HWIFS; ++index) {
  1241. if (probe[index]) {
  1242. ide_hwif_t *hwif = &ide_hwifs[index];
  1243. int unit;
  1244. if (!hwif->present)
  1245. continue;
  1246. if (hwif->chipset == ide_unknown || hwif->chipset == ide_forced)
  1247. hwif->chipset = ide_generic;
  1248. for (unit = 0; unit < MAX_DRIVES; ++unit)
  1249. if (hwif->drives[unit].present)
  1250. device_register(&hwif->drives[unit].gendev);
  1251. }
  1252. }
  1253. return 0;
  1254. }
  1255. EXPORT_SYMBOL_GPL(ideprobe_init);