ide-probe.c 40 KB

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