ide-probe.c 38 KB

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