ide-probe.c 36 KB

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