ide-probe.c 36 KB

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