ide-probe.c 35 KB

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