ide-probe.c 35 KB

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