ide-probe.c 37 KB

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