ide-probe.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422
  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, void (*fixup)(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 (fixup)
  734. 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. int probe_hwif_init_with_fixup(ide_hwif_t *hwif, void (*fixup)(ide_hwif_t *hwif))
  766. {
  767. probe_hwif(hwif, fixup);
  768. if (!hwif_init(hwif)) {
  769. printk(KERN_INFO "%s: failed to initialize IDE interface\n",
  770. hwif->name);
  771. return -1;
  772. }
  773. if (hwif->present) {
  774. u16 unit = 0;
  775. int ret;
  776. for (unit = 0; unit < MAX_DRIVES; ++unit) {
  777. ide_drive_t *drive = &hwif->drives[unit];
  778. /* For now don't attach absent drives, we may
  779. want them on default or a new "empty" class
  780. for hotplug reprobing ? */
  781. if (drive->present) {
  782. ret = device_register(&drive->gendev);
  783. if (ret < 0)
  784. printk(KERN_WARNING "IDE: %s: "
  785. "device_register error: %d\n",
  786. __FUNCTION__, ret);
  787. }
  788. }
  789. }
  790. return 0;
  791. }
  792. int probe_hwif_init(ide_hwif_t *hwif)
  793. {
  794. return probe_hwif_init_with_fixup(hwif, NULL);
  795. }
  796. EXPORT_SYMBOL(probe_hwif_init);
  797. #if MAX_HWIFS > 1
  798. /*
  799. * save_match() is used to simplify logic in init_irq() below.
  800. *
  801. * A loophole here is that we may not know about a particular
  802. * hwif's irq until after that hwif is actually probed/initialized..
  803. * This could be a problem for the case where an hwif is on a
  804. * dual interface that requires serialization (eg. cmd640) and another
  805. * hwif using one of the same irqs is initialized beforehand.
  806. *
  807. * This routine detects and reports such situations, but does not fix them.
  808. */
  809. static void save_match(ide_hwif_t *hwif, ide_hwif_t *new, ide_hwif_t **match)
  810. {
  811. ide_hwif_t *m = *match;
  812. if (m && m->hwgroup && m->hwgroup != new->hwgroup) {
  813. if (!new->hwgroup)
  814. return;
  815. printk("%s: potential irq problem with %s and %s\n",
  816. hwif->name, new->name, m->name);
  817. }
  818. if (!m || m->irq != hwif->irq) /* don't undo a prior perfect match */
  819. *match = new;
  820. }
  821. #endif /* MAX_HWIFS > 1 */
  822. /*
  823. * init request queue
  824. */
  825. static int ide_init_queue(ide_drive_t *drive)
  826. {
  827. struct request_queue *q;
  828. ide_hwif_t *hwif = HWIF(drive);
  829. int max_sectors = 256;
  830. int max_sg_entries = PRD_ENTRIES;
  831. /*
  832. * Our default set up assumes the normal IDE case,
  833. * that is 64K segmenting, standard PRD setup
  834. * and LBA28. Some drivers then impose their own
  835. * limits and LBA48 we could raise it but as yet
  836. * do not.
  837. */
  838. q = blk_init_queue_node(do_ide_request, &ide_lock, hwif_to_node(hwif));
  839. if (!q)
  840. return 1;
  841. q->queuedata = drive;
  842. blk_queue_segment_boundary(q, 0xffff);
  843. if (!hwif->rqsize) {
  844. if ((hwif->host_flags & IDE_HFLAG_NO_LBA48) ||
  845. (hwif->host_flags & IDE_HFLAG_NO_LBA48_DMA))
  846. hwif->rqsize = 256;
  847. else
  848. hwif->rqsize = 65536;
  849. }
  850. if (hwif->rqsize < max_sectors)
  851. max_sectors = hwif->rqsize;
  852. blk_queue_max_sectors(q, max_sectors);
  853. #ifdef CONFIG_PCI
  854. /* When we have an IOMMU, we may have a problem where pci_map_sg()
  855. * creates segments that don't completely match our boundary
  856. * requirements and thus need to be broken up again. Because it
  857. * doesn't align properly either, we may actually have to break up
  858. * to more segments than what was we got in the first place, a max
  859. * worst case is twice as many.
  860. * This will be fixed once we teach pci_map_sg() about our boundary
  861. * requirements, hopefully soon. *FIXME*
  862. */
  863. if (!PCI_DMA_BUS_IS_PHYS)
  864. max_sg_entries >>= 1;
  865. #endif /* CONFIG_PCI */
  866. blk_queue_max_hw_segments(q, max_sg_entries);
  867. blk_queue_max_phys_segments(q, max_sg_entries);
  868. /* assign drive queue */
  869. drive->queue = q;
  870. /* needs drive->queue to be set */
  871. ide_toggle_bounce(drive, 1);
  872. return 0;
  873. }
  874. /*
  875. * This routine sets up the irq for an ide interface, and creates a new
  876. * hwgroup for the irq/hwif if none was previously assigned.
  877. *
  878. * Much of the code is for correctly detecting/handling irq sharing
  879. * and irq serialization situations. This is somewhat complex because
  880. * it handles static as well as dynamic (PCMCIA) IDE interfaces.
  881. *
  882. * The IRQF_DISABLED in sa_flags means ide_intr() is always entered with
  883. * interrupts completely disabled. This can be bad for interrupt latency,
  884. * but anything else has led to problems on some machines. We re-enable
  885. * interrupts as much as we can safely do in most places.
  886. */
  887. static int init_irq (ide_hwif_t *hwif)
  888. {
  889. unsigned int index;
  890. ide_hwgroup_t *hwgroup;
  891. ide_hwif_t *match = NULL;
  892. BUG_ON(in_interrupt());
  893. BUG_ON(irqs_disabled());
  894. BUG_ON(hwif == NULL);
  895. mutex_lock(&ide_cfg_mtx);
  896. hwif->hwgroup = NULL;
  897. #if MAX_HWIFS > 1
  898. /*
  899. * Group up with any other hwifs that share our irq(s).
  900. */
  901. for (index = 0; index < MAX_HWIFS; index++) {
  902. ide_hwif_t *h = &ide_hwifs[index];
  903. if (h->hwgroup) { /* scan only initialized hwif's */
  904. if (hwif->irq == h->irq) {
  905. hwif->sharing_irq = h->sharing_irq = 1;
  906. if (hwif->chipset != ide_pci ||
  907. h->chipset != ide_pci) {
  908. save_match(hwif, h, &match);
  909. }
  910. }
  911. if (hwif->serialized) {
  912. if (hwif->mate && hwif->mate->irq == h->irq)
  913. save_match(hwif, h, &match);
  914. }
  915. if (h->serialized) {
  916. if (h->mate && hwif->irq == h->mate->irq)
  917. save_match(hwif, h, &match);
  918. }
  919. }
  920. }
  921. #endif /* MAX_HWIFS > 1 */
  922. /*
  923. * If we are still without a hwgroup, then form a new one
  924. */
  925. if (match) {
  926. hwgroup = match->hwgroup;
  927. hwif->hwgroup = hwgroup;
  928. /*
  929. * Link us into the hwgroup.
  930. * This must be done early, do ensure that unexpected_intr
  931. * can find the hwif and prevent irq storms.
  932. * No drives are attached to the new hwif, choose_drive
  933. * can't do anything stupid (yet).
  934. * Add ourself as the 2nd entry to the hwgroup->hwif
  935. * linked list, the first entry is the hwif that owns
  936. * hwgroup->handler - do not change that.
  937. */
  938. spin_lock_irq(&ide_lock);
  939. hwif->next = hwgroup->hwif->next;
  940. hwgroup->hwif->next = hwif;
  941. spin_unlock_irq(&ide_lock);
  942. } else {
  943. hwgroup = kmalloc_node(sizeof(ide_hwgroup_t),
  944. GFP_KERNEL | __GFP_ZERO,
  945. hwif_to_node(hwif->drives[0].hwif));
  946. if (!hwgroup)
  947. goto out_up;
  948. hwif->hwgroup = hwgroup;
  949. hwgroup->hwif = hwif->next = hwif;
  950. hwgroup->rq = NULL;
  951. hwgroup->handler = NULL;
  952. hwgroup->drive = NULL;
  953. hwgroup->busy = 0;
  954. init_timer(&hwgroup->timer);
  955. hwgroup->timer.function = &ide_timer_expiry;
  956. hwgroup->timer.data = (unsigned long) hwgroup;
  957. }
  958. /*
  959. * Allocate the irq, if not already obtained for another hwif
  960. */
  961. if (!match || match->irq != hwif->irq) {
  962. int sa = IRQF_DISABLED;
  963. #if defined(__mc68000__) || defined(CONFIG_APUS)
  964. sa = IRQF_SHARED;
  965. #endif /* __mc68000__ || CONFIG_APUS */
  966. if (IDE_CHIPSET_IS_PCI(hwif->chipset)) {
  967. sa = IRQF_SHARED;
  968. #ifndef CONFIG_IDEPCI_SHARE_IRQ
  969. sa |= IRQF_DISABLED;
  970. #endif /* CONFIG_IDEPCI_SHARE_IRQ */
  971. }
  972. if (hwif->io_ports[IDE_CONTROL_OFFSET])
  973. /* clear nIEN */
  974. hwif->OUTB(0x08, hwif->io_ports[IDE_CONTROL_OFFSET]);
  975. if (request_irq(hwif->irq,&ide_intr,sa,hwif->name,hwgroup))
  976. goto out_unlink;
  977. }
  978. /*
  979. * For any present drive:
  980. * - allocate the block device queue
  981. * - link drive into the hwgroup
  982. */
  983. for (index = 0; index < MAX_DRIVES; ++index) {
  984. ide_drive_t *drive = &hwif->drives[index];
  985. if (!drive->present)
  986. continue;
  987. if (ide_init_queue(drive)) {
  988. printk(KERN_ERR "ide: failed to init %s\n",drive->name);
  989. continue;
  990. }
  991. spin_lock_irq(&ide_lock);
  992. if (!hwgroup->drive) {
  993. /* first drive for hwgroup. */
  994. drive->next = drive;
  995. hwgroup->drive = drive;
  996. hwgroup->hwif = HWIF(hwgroup->drive);
  997. } else {
  998. drive->next = hwgroup->drive->next;
  999. hwgroup->drive->next = drive;
  1000. }
  1001. spin_unlock_irq(&ide_lock);
  1002. }
  1003. #if !defined(__mc68000__) && !defined(CONFIG_APUS)
  1004. printk("%s at 0x%03lx-0x%03lx,0x%03lx on irq %d", hwif->name,
  1005. hwif->io_ports[IDE_DATA_OFFSET],
  1006. hwif->io_ports[IDE_DATA_OFFSET]+7,
  1007. hwif->io_ports[IDE_CONTROL_OFFSET], hwif->irq);
  1008. #else
  1009. printk("%s at 0x%08lx on irq %d", hwif->name,
  1010. hwif->io_ports[IDE_DATA_OFFSET], hwif->irq);
  1011. #endif /* __mc68000__ && CONFIG_APUS */
  1012. if (match)
  1013. printk(" (%sed with %s)",
  1014. hwif->sharing_irq ? "shar" : "serializ", match->name);
  1015. printk("\n");
  1016. mutex_unlock(&ide_cfg_mtx);
  1017. return 0;
  1018. out_unlink:
  1019. spin_lock_irq(&ide_lock);
  1020. if (hwif->next == hwif) {
  1021. BUG_ON(match);
  1022. BUG_ON(hwgroup->hwif != hwif);
  1023. kfree(hwgroup);
  1024. } else {
  1025. ide_hwif_t *g;
  1026. g = hwgroup->hwif;
  1027. while (g->next != hwif)
  1028. g = g->next;
  1029. g->next = hwif->next;
  1030. if (hwgroup->hwif == hwif) {
  1031. /* Impossible. */
  1032. printk(KERN_ERR "Duh. Uninitialized hwif listed as active hwif.\n");
  1033. hwgroup->hwif = g;
  1034. }
  1035. BUG_ON(hwgroup->hwif == hwif);
  1036. }
  1037. spin_unlock_irq(&ide_lock);
  1038. out_up:
  1039. mutex_unlock(&ide_cfg_mtx);
  1040. return 1;
  1041. }
  1042. static int ata_lock(dev_t dev, void *data)
  1043. {
  1044. /* FIXME: we want to pin hwif down */
  1045. return 0;
  1046. }
  1047. static struct kobject *ata_probe(dev_t dev, int *part, void *data)
  1048. {
  1049. ide_hwif_t *hwif = data;
  1050. int unit = *part >> PARTN_BITS;
  1051. ide_drive_t *drive = &hwif->drives[unit];
  1052. if (!drive->present)
  1053. return NULL;
  1054. if (drive->media == ide_disk)
  1055. request_module("ide-disk");
  1056. if (drive->scsi)
  1057. request_module("ide-scsi");
  1058. if (drive->media == ide_cdrom || drive->media == ide_optical)
  1059. request_module("ide-cd");
  1060. if (drive->media == ide_tape)
  1061. request_module("ide-tape");
  1062. if (drive->media == ide_floppy)
  1063. request_module("ide-floppy");
  1064. return NULL;
  1065. }
  1066. static struct kobject *exact_match(dev_t dev, int *part, void *data)
  1067. {
  1068. struct gendisk *p = data;
  1069. *part &= (1 << PARTN_BITS) - 1;
  1070. return &p->kobj;
  1071. }
  1072. static int exact_lock(dev_t dev, void *data)
  1073. {
  1074. struct gendisk *p = data;
  1075. if (!get_disk(p))
  1076. return -1;
  1077. return 0;
  1078. }
  1079. void ide_register_region(struct gendisk *disk)
  1080. {
  1081. blk_register_region(MKDEV(disk->major, disk->first_minor),
  1082. disk->minors, NULL, exact_match, exact_lock, disk);
  1083. }
  1084. EXPORT_SYMBOL_GPL(ide_register_region);
  1085. void ide_unregister_region(struct gendisk *disk)
  1086. {
  1087. blk_unregister_region(MKDEV(disk->major, disk->first_minor),
  1088. disk->minors);
  1089. }
  1090. EXPORT_SYMBOL_GPL(ide_unregister_region);
  1091. void ide_init_disk(struct gendisk *disk, ide_drive_t *drive)
  1092. {
  1093. ide_hwif_t *hwif = drive->hwif;
  1094. unsigned int unit = (drive->select.all >> 4) & 1;
  1095. disk->major = hwif->major;
  1096. disk->first_minor = unit << PARTN_BITS;
  1097. sprintf(disk->disk_name, "hd%c", 'a' + hwif->index * MAX_DRIVES + unit);
  1098. disk->queue = drive->queue;
  1099. }
  1100. EXPORT_SYMBOL_GPL(ide_init_disk);
  1101. static void ide_remove_drive_from_hwgroup(ide_drive_t *drive)
  1102. {
  1103. ide_hwgroup_t *hwgroup = drive->hwif->hwgroup;
  1104. if (drive == drive->next) {
  1105. /* special case: last drive from hwgroup. */
  1106. BUG_ON(hwgroup->drive != drive);
  1107. hwgroup->drive = NULL;
  1108. } else {
  1109. ide_drive_t *walk;
  1110. walk = hwgroup->drive;
  1111. while (walk->next != drive)
  1112. walk = walk->next;
  1113. walk->next = drive->next;
  1114. if (hwgroup->drive == drive) {
  1115. hwgroup->drive = drive->next;
  1116. hwgroup->hwif = hwgroup->drive->hwif;
  1117. }
  1118. }
  1119. BUG_ON(hwgroup->drive == drive);
  1120. }
  1121. static void drive_release_dev (struct device *dev)
  1122. {
  1123. ide_drive_t *drive = container_of(dev, ide_drive_t, gendev);
  1124. spin_lock_irq(&ide_lock);
  1125. ide_remove_drive_from_hwgroup(drive);
  1126. kfree(drive->id);
  1127. drive->id = NULL;
  1128. drive->present = 0;
  1129. /* Messed up locking ... */
  1130. spin_unlock_irq(&ide_lock);
  1131. blk_cleanup_queue(drive->queue);
  1132. spin_lock_irq(&ide_lock);
  1133. drive->queue = NULL;
  1134. spin_unlock_irq(&ide_lock);
  1135. complete(&drive->gendev_rel_comp);
  1136. }
  1137. /*
  1138. * init_gendisk() (as opposed to ide_geninit) is called for each major device,
  1139. * after probing for drives, to allocate partition tables and other data
  1140. * structures needed for the routines in genhd.c. ide_geninit() gets called
  1141. * somewhat later, during the partition check.
  1142. */
  1143. static void init_gendisk (ide_hwif_t *hwif)
  1144. {
  1145. unsigned int unit;
  1146. for (unit = 0; unit < MAX_DRIVES; ++unit) {
  1147. ide_drive_t * drive = &hwif->drives[unit];
  1148. ide_add_generic_settings(drive);
  1149. snprintf(drive->gendev.bus_id,BUS_ID_SIZE,"%u.%u",
  1150. hwif->index,unit);
  1151. drive->gendev.parent = &hwif->gendev;
  1152. drive->gendev.bus = &ide_bus_type;
  1153. drive->gendev.driver_data = drive;
  1154. drive->gendev.release = drive_release_dev;
  1155. }
  1156. blk_register_region(MKDEV(hwif->major, 0), MAX_DRIVES << PARTN_BITS,
  1157. THIS_MODULE, ata_probe, ata_lock, hwif);
  1158. }
  1159. static int hwif_init(ide_hwif_t *hwif)
  1160. {
  1161. int old_irq;
  1162. /* Return success if no device is connected */
  1163. if (!hwif->present)
  1164. return 1;
  1165. if (!hwif->irq) {
  1166. if (!(hwif->irq = ide_default_irq(hwif->io_ports[IDE_DATA_OFFSET])))
  1167. {
  1168. printk("%s: DISABLED, NO IRQ\n", hwif->name);
  1169. return (hwif->present = 0);
  1170. }
  1171. }
  1172. #ifdef CONFIG_BLK_DEV_HD
  1173. if (hwif->irq == HD_IRQ && hwif->io_ports[IDE_DATA_OFFSET] != HD_DATA) {
  1174. printk("%s: CANNOT SHARE IRQ WITH OLD "
  1175. "HARDDISK DRIVER (hd.c)\n", hwif->name);
  1176. return (hwif->present = 0);
  1177. }
  1178. #endif /* CONFIG_BLK_DEV_HD */
  1179. /* we set it back to 1 if all is ok below */
  1180. hwif->present = 0;
  1181. if (register_blkdev(hwif->major, hwif->name))
  1182. return 0;
  1183. if (!hwif->sg_max_nents)
  1184. hwif->sg_max_nents = PRD_ENTRIES;
  1185. hwif->sg_table = kzalloc(sizeof(struct scatterlist)*hwif->sg_max_nents,
  1186. GFP_KERNEL);
  1187. if (!hwif->sg_table) {
  1188. printk(KERN_ERR "%s: unable to allocate SG table.\n", hwif->name);
  1189. goto out;
  1190. }
  1191. if (init_irq(hwif) == 0)
  1192. goto done;
  1193. old_irq = hwif->irq;
  1194. /*
  1195. * It failed to initialise. Find the default IRQ for
  1196. * this port and try that.
  1197. */
  1198. if (!(hwif->irq = ide_default_irq(hwif->io_ports[IDE_DATA_OFFSET]))) {
  1199. printk("%s: Disabled unable to get IRQ %d.\n",
  1200. hwif->name, old_irq);
  1201. goto out;
  1202. }
  1203. if (init_irq(hwif)) {
  1204. printk("%s: probed IRQ %d and default IRQ %d failed.\n",
  1205. hwif->name, old_irq, hwif->irq);
  1206. goto out;
  1207. }
  1208. printk("%s: probed IRQ %d failed, using default.\n",
  1209. hwif->name, hwif->irq);
  1210. done:
  1211. init_gendisk(hwif);
  1212. ide_acpi_init(hwif);
  1213. hwif->present = 1; /* success */
  1214. return 1;
  1215. out:
  1216. unregister_blkdev(hwif->major, hwif->name);
  1217. return 0;
  1218. }
  1219. int ideprobe_init (void)
  1220. {
  1221. unsigned int index;
  1222. int probe[MAX_HWIFS];
  1223. memset(probe, 0, MAX_HWIFS * sizeof(int));
  1224. for (index = 0; index < MAX_HWIFS; ++index)
  1225. probe[index] = !ide_hwifs[index].present;
  1226. for (index = 0; index < MAX_HWIFS; ++index)
  1227. if (probe[index])
  1228. probe_hwif(&ide_hwifs[index], NULL);
  1229. for (index = 0; index < MAX_HWIFS; ++index)
  1230. if (probe[index])
  1231. hwif_init(&ide_hwifs[index]);
  1232. for (index = 0; index < MAX_HWIFS; ++index) {
  1233. if (probe[index]) {
  1234. ide_hwif_t *hwif = &ide_hwifs[index];
  1235. int unit;
  1236. if (!hwif->present)
  1237. continue;
  1238. if (hwif->chipset == ide_unknown || hwif->chipset == ide_forced)
  1239. hwif->chipset = ide_generic;
  1240. for (unit = 0; unit < MAX_DRIVES; ++unit)
  1241. if (hwif->drives[unit].present) {
  1242. int ret = device_register(
  1243. &hwif->drives[unit].gendev);
  1244. if (ret < 0)
  1245. printk(KERN_WARNING "IDE: %s: "
  1246. "device_register error: %d\n",
  1247. __FUNCTION__, ret);
  1248. }
  1249. }
  1250. }
  1251. for (index = 0; index < MAX_HWIFS; ++index)
  1252. if (probe[index])
  1253. ide_proc_register_port(&ide_hwifs[index]);
  1254. return 0;
  1255. }
  1256. EXPORT_SYMBOL_GPL(ideprobe_init);