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