ide-probe.c 37 KB

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