ide-probe.c 35 KB

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