ide-probe.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486
  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 void 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. }
  571. /**
  572. * ide_port_wait_ready - wait for port to become ready
  573. * @hwif: IDE port
  574. *
  575. * This is needed on some PPCs and a bunch of BIOS-less embedded
  576. * platforms. Typical cases are:
  577. *
  578. * - The firmware hard reset the disk before booting the kernel,
  579. * the drive is still doing it's poweron-reset sequence, that
  580. * can take up to 30 seconds.
  581. *
  582. * - The firmware does nothing (or no firmware), the device is
  583. * still in POST state (same as above actually).
  584. *
  585. * - Some CD/DVD/Writer combo drives tend to drive the bus during
  586. * their reset sequence even when they are non-selected slave
  587. * devices, thus preventing discovery of the main HD.
  588. *
  589. * Doing this wait-for-non-busy should not harm any existing
  590. * configuration and fix some issues like the above.
  591. *
  592. * BenH.
  593. *
  594. * Returns 0 on success, error code (< 0) otherwise.
  595. */
  596. static int ide_port_wait_ready(ide_hwif_t *hwif)
  597. {
  598. int unit, rc;
  599. printk(KERN_DEBUG "Probing IDE interface %s...\n", hwif->name);
  600. /* Let HW settle down a bit from whatever init state we
  601. * come from */
  602. mdelay(2);
  603. /* Wait for BSY bit to go away, spec timeout is 30 seconds,
  604. * I know of at least one disk who takes 31 seconds, I use 35
  605. * here to be safe
  606. */
  607. rc = ide_wait_not_busy(hwif, 35000);
  608. if (rc)
  609. return rc;
  610. /* Now make sure both master & slave are ready */
  611. for (unit = 0; unit < MAX_DRIVES; unit++) {
  612. ide_drive_t *drive = &hwif->drives[unit];
  613. /* Ignore disks that we will not probe for later. */
  614. if (!drive->noprobe || drive->present) {
  615. SELECT_DRIVE(drive);
  616. ide_set_irq(drive, 1);
  617. mdelay(2);
  618. rc = ide_wait_not_busy(hwif, 35000);
  619. if (rc)
  620. goto out;
  621. } else
  622. printk(KERN_DEBUG "%s: ide_wait_not_busy() skipped\n",
  623. drive->name);
  624. }
  625. out:
  626. /* Exit function with master reselected (let's be sane) */
  627. if (unit)
  628. SELECT_DRIVE(&hwif->drives[0]);
  629. return rc;
  630. }
  631. /**
  632. * ide_undecoded_slave - look for bad CF adapters
  633. * @drive1: drive
  634. *
  635. * Analyse the drives on the interface and attempt to decide if we
  636. * have the same drive viewed twice. This occurs with crap CF adapters
  637. * and PCMCIA sometimes.
  638. */
  639. void ide_undecoded_slave(ide_drive_t *drive1)
  640. {
  641. ide_drive_t *drive0 = &drive1->hwif->drives[0];
  642. if ((drive1->dn & 1) == 0 || drive0->present == 0)
  643. return;
  644. /* If the models don't match they are not the same product */
  645. if (strcmp(drive0->id->model, drive1->id->model))
  646. return;
  647. /* Serial numbers do not match */
  648. if (strncmp(drive0->id->serial_no, drive1->id->serial_no, 20))
  649. return;
  650. /* No serial number, thankfully very rare for CF */
  651. if (drive0->id->serial_no[0] == 0)
  652. return;
  653. /* Appears to be an IDE flash adapter with decode bugs */
  654. printk(KERN_WARNING "ide-probe: ignoring undecoded slave\n");
  655. drive1->present = 0;
  656. }
  657. EXPORT_SYMBOL_GPL(ide_undecoded_slave);
  658. static int ide_probe_port(ide_hwif_t *hwif)
  659. {
  660. unsigned long flags;
  661. unsigned int irqd;
  662. int unit, rc = -ENODEV;
  663. BUG_ON(hwif->present);
  664. if (hwif->noprobe)
  665. return -EACCES;
  666. /*
  667. * We must always disable IRQ, as probe_for_drive will assert IRQ, but
  668. * we'll install our IRQ driver much later...
  669. */
  670. irqd = hwif->irq;
  671. if (irqd)
  672. disable_irq(hwif->irq);
  673. local_irq_set(flags);
  674. if (ide_port_wait_ready(hwif) == -EBUSY)
  675. printk(KERN_DEBUG "%s: Wait for ready failed before probe !\n", hwif->name);
  676. /*
  677. * Need to probe slave device first to make it release PDIAG-.
  678. */
  679. for (unit = MAX_DRIVES - 1; unit >= 0; unit--) {
  680. ide_drive_t *drive = &hwif->drives[unit];
  681. drive->dn = (hwif->channel ? 2 : 0) + unit;
  682. (void) probe_for_drive(drive);
  683. if (drive->present)
  684. rc = 0;
  685. }
  686. if (hwif->io_ports[IDE_CONTROL_OFFSET] && hwif->reset) {
  687. printk(KERN_WARNING "%s: reset\n", hwif->name);
  688. hwif->OUTB(12, hwif->io_ports[IDE_CONTROL_OFFSET]);
  689. udelay(10);
  690. hwif->OUTB(8, hwif->io_ports[IDE_CONTROL_OFFSET]);
  691. (void)ide_busy_sleep(hwif);
  692. }
  693. local_irq_restore(flags);
  694. /*
  695. * Use cached IRQ number. It might be (and is...) changed by probe
  696. * code above
  697. */
  698. if (irqd)
  699. enable_irq(irqd);
  700. return rc;
  701. }
  702. static void ide_port_tune_devices(ide_hwif_t *hwif)
  703. {
  704. int unit;
  705. for (unit = 0; unit < MAX_DRIVES; unit++) {
  706. ide_drive_t *drive = &hwif->drives[unit];
  707. if (drive->present && hwif->quirkproc)
  708. hwif->quirkproc(drive);
  709. }
  710. for (unit = 0; unit < MAX_DRIVES; ++unit) {
  711. ide_drive_t *drive = &hwif->drives[unit];
  712. if (drive->present) {
  713. if (drive->autotune == IDE_TUNE_AUTO)
  714. ide_set_max_pio(drive);
  715. if (drive->autotune != IDE_TUNE_DEFAULT &&
  716. drive->autotune != IDE_TUNE_AUTO)
  717. continue;
  718. drive->nice1 = 1;
  719. if (hwif->dma_host_set)
  720. ide_set_dma(drive);
  721. }
  722. }
  723. for (unit = 0; unit < MAX_DRIVES; ++unit) {
  724. ide_drive_t *drive = &hwif->drives[unit];
  725. if (hwif->host_flags & IDE_HFLAG_NO_IO_32BIT)
  726. drive->no_io_32bit = 1;
  727. else
  728. drive->no_io_32bit = drive->id->dword_io ? 1 : 0;
  729. }
  730. }
  731. #if MAX_HWIFS > 1
  732. /*
  733. * save_match() is used to simplify logic in init_irq() below.
  734. *
  735. * A loophole here is that we may not know about a particular
  736. * hwif's irq until after that hwif is actually probed/initialized..
  737. * This could be a problem for the case where an hwif is on a
  738. * dual interface that requires serialization (eg. cmd640) and another
  739. * hwif using one of the same irqs is initialized beforehand.
  740. *
  741. * This routine detects and reports such situations, but does not fix them.
  742. */
  743. static void save_match(ide_hwif_t *hwif, ide_hwif_t *new, ide_hwif_t **match)
  744. {
  745. ide_hwif_t *m = *match;
  746. if (m && m->hwgroup && m->hwgroup != new->hwgroup) {
  747. if (!new->hwgroup)
  748. return;
  749. printk("%s: potential irq problem with %s and %s\n",
  750. hwif->name, new->name, m->name);
  751. }
  752. if (!m || m->irq != hwif->irq) /* don't undo a prior perfect match */
  753. *match = new;
  754. }
  755. #endif /* MAX_HWIFS > 1 */
  756. /*
  757. * init request queue
  758. */
  759. static int ide_init_queue(ide_drive_t *drive)
  760. {
  761. struct request_queue *q;
  762. ide_hwif_t *hwif = HWIF(drive);
  763. int max_sectors = 256;
  764. int max_sg_entries = PRD_ENTRIES;
  765. /*
  766. * Our default set up assumes the normal IDE case,
  767. * that is 64K segmenting, standard PRD setup
  768. * and LBA28. Some drivers then impose their own
  769. * limits and LBA48 we could raise it but as yet
  770. * do not.
  771. */
  772. q = blk_init_queue_node(do_ide_request, &ide_lock, hwif_to_node(hwif));
  773. if (!q)
  774. return 1;
  775. q->queuedata = drive;
  776. blk_queue_segment_boundary(q, 0xffff);
  777. if (hwif->rqsize < max_sectors)
  778. max_sectors = hwif->rqsize;
  779. blk_queue_max_sectors(q, max_sectors);
  780. #ifdef CONFIG_PCI
  781. /* When we have an IOMMU, we may have a problem where pci_map_sg()
  782. * creates segments that don't completely match our boundary
  783. * requirements and thus need to be broken up again. Because it
  784. * doesn't align properly either, we may actually have to break up
  785. * to more segments than what was we got in the first place, a max
  786. * worst case is twice as many.
  787. * This will be fixed once we teach pci_map_sg() about our boundary
  788. * requirements, hopefully soon. *FIXME*
  789. */
  790. if (!PCI_DMA_BUS_IS_PHYS)
  791. max_sg_entries >>= 1;
  792. #endif /* CONFIG_PCI */
  793. blk_queue_max_hw_segments(q, max_sg_entries);
  794. blk_queue_max_phys_segments(q, max_sg_entries);
  795. /* assign drive queue */
  796. drive->queue = q;
  797. /* needs drive->queue to be set */
  798. ide_toggle_bounce(drive, 1);
  799. return 0;
  800. }
  801. static void ide_add_drive_to_hwgroup(ide_drive_t *drive)
  802. {
  803. ide_hwgroup_t *hwgroup = drive->hwif->hwgroup;
  804. spin_lock_irq(&ide_lock);
  805. if (!hwgroup->drive) {
  806. /* first drive for hwgroup. */
  807. drive->next = drive;
  808. hwgroup->drive = drive;
  809. hwgroup->hwif = HWIF(hwgroup->drive);
  810. } else {
  811. drive->next = hwgroup->drive->next;
  812. hwgroup->drive->next = drive;
  813. }
  814. spin_unlock_irq(&ide_lock);
  815. }
  816. /*
  817. * For any present drive:
  818. * - allocate the block device queue
  819. * - link drive into the hwgroup
  820. */
  821. static void ide_port_setup_devices(ide_hwif_t *hwif)
  822. {
  823. int i;
  824. for (i = 0; i < MAX_DRIVES; i++) {
  825. ide_drive_t *drive = &hwif->drives[i];
  826. if (!drive->present)
  827. continue;
  828. if (ide_init_queue(drive)) {
  829. printk(KERN_ERR "ide: failed to init %s\n",
  830. drive->name);
  831. continue;
  832. }
  833. ide_add_drive_to_hwgroup(drive);
  834. }
  835. }
  836. /*
  837. * This routine sets up the irq for an ide interface, and creates a new
  838. * hwgroup for the irq/hwif if none was previously assigned.
  839. *
  840. * Much of the code is for correctly detecting/handling irq sharing
  841. * and irq serialization situations. This is somewhat complex because
  842. * it handles static as well as dynamic (PCMCIA) IDE interfaces.
  843. */
  844. static int init_irq (ide_hwif_t *hwif)
  845. {
  846. unsigned int index;
  847. ide_hwgroup_t *hwgroup;
  848. ide_hwif_t *match = NULL;
  849. BUG_ON(in_interrupt());
  850. BUG_ON(irqs_disabled());
  851. BUG_ON(hwif == NULL);
  852. mutex_lock(&ide_cfg_mtx);
  853. hwif->hwgroup = NULL;
  854. #if MAX_HWIFS > 1
  855. /*
  856. * Group up with any other hwifs that share our irq(s).
  857. */
  858. for (index = 0; index < MAX_HWIFS; index++) {
  859. ide_hwif_t *h = &ide_hwifs[index];
  860. if (h->hwgroup) { /* scan only initialized hwif's */
  861. if (hwif->irq == h->irq) {
  862. hwif->sharing_irq = h->sharing_irq = 1;
  863. if (hwif->chipset != ide_pci ||
  864. h->chipset != ide_pci) {
  865. save_match(hwif, h, &match);
  866. }
  867. }
  868. if (hwif->serialized) {
  869. if (hwif->mate && hwif->mate->irq == h->irq)
  870. save_match(hwif, h, &match);
  871. }
  872. if (h->serialized) {
  873. if (h->mate && hwif->irq == h->mate->irq)
  874. save_match(hwif, h, &match);
  875. }
  876. }
  877. }
  878. #endif /* MAX_HWIFS > 1 */
  879. /*
  880. * If we are still without a hwgroup, then form a new one
  881. */
  882. if (match) {
  883. hwgroup = match->hwgroup;
  884. hwif->hwgroup = hwgroup;
  885. /*
  886. * Link us into the hwgroup.
  887. * This must be done early, do ensure that unexpected_intr
  888. * can find the hwif and prevent irq storms.
  889. * No drives are attached to the new hwif, choose_drive
  890. * can't do anything stupid (yet).
  891. * Add ourself as the 2nd entry to the hwgroup->hwif
  892. * linked list, the first entry is the hwif that owns
  893. * hwgroup->handler - do not change that.
  894. */
  895. spin_lock_irq(&ide_lock);
  896. hwif->next = hwgroup->hwif->next;
  897. hwgroup->hwif->next = hwif;
  898. BUG_ON(hwif->next == hwif);
  899. spin_unlock_irq(&ide_lock);
  900. } else {
  901. hwgroup = kmalloc_node(sizeof(*hwgroup), GFP_KERNEL|__GFP_ZERO,
  902. hwif_to_node(hwif));
  903. if (hwgroup == NULL)
  904. goto out_up;
  905. hwif->hwgroup = hwgroup;
  906. hwgroup->hwif = hwif->next = hwif;
  907. init_timer(&hwgroup->timer);
  908. hwgroup->timer.function = &ide_timer_expiry;
  909. hwgroup->timer.data = (unsigned long) hwgroup;
  910. }
  911. /*
  912. * Allocate the irq, if not already obtained for another hwif
  913. */
  914. if (!match || match->irq != hwif->irq) {
  915. int sa = 0;
  916. #if defined(__mc68000__)
  917. sa = IRQF_SHARED;
  918. #endif /* __mc68000__ || CONFIG_APUS */
  919. if (IDE_CHIPSET_IS_PCI(hwif->chipset))
  920. sa = IRQF_SHARED;
  921. if (hwif->io_ports[IDE_CONTROL_OFFSET])
  922. /* clear nIEN */
  923. hwif->OUTB(0x08, hwif->io_ports[IDE_CONTROL_OFFSET]);
  924. if (request_irq(hwif->irq,&ide_intr,sa,hwif->name,hwgroup))
  925. goto out_unlink;
  926. }
  927. if (!hwif->rqsize) {
  928. if ((hwif->host_flags & IDE_HFLAG_NO_LBA48) ||
  929. (hwif->host_flags & IDE_HFLAG_NO_LBA48_DMA))
  930. hwif->rqsize = 256;
  931. else
  932. hwif->rqsize = 65536;
  933. }
  934. #if !defined(__mc68000__)
  935. printk("%s at 0x%03lx-0x%03lx,0x%03lx on irq %d", hwif->name,
  936. hwif->io_ports[IDE_DATA_OFFSET],
  937. hwif->io_ports[IDE_DATA_OFFSET]+7,
  938. hwif->io_ports[IDE_CONTROL_OFFSET], hwif->irq);
  939. #else
  940. printk("%s at 0x%08lx on irq %d", hwif->name,
  941. hwif->io_ports[IDE_DATA_OFFSET], hwif->irq);
  942. #endif /* __mc68000__ */
  943. if (match)
  944. printk(" (%sed with %s)",
  945. hwif->sharing_irq ? "shar" : "serializ", match->name);
  946. printk("\n");
  947. ide_port_setup_devices(hwif);
  948. mutex_unlock(&ide_cfg_mtx);
  949. return 0;
  950. out_unlink:
  951. ide_remove_port_from_hwgroup(hwif);
  952. out_up:
  953. mutex_unlock(&ide_cfg_mtx);
  954. return 1;
  955. }
  956. static int ata_lock(dev_t dev, void *data)
  957. {
  958. /* FIXME: we want to pin hwif down */
  959. return 0;
  960. }
  961. static struct kobject *ata_probe(dev_t dev, int *part, void *data)
  962. {
  963. ide_hwif_t *hwif = data;
  964. int unit = *part >> PARTN_BITS;
  965. ide_drive_t *drive = &hwif->drives[unit];
  966. if (!drive->present)
  967. return NULL;
  968. if (drive->media == ide_disk)
  969. request_module("ide-disk");
  970. if (drive->scsi)
  971. request_module("ide-scsi");
  972. if (drive->media == ide_cdrom || drive->media == ide_optical)
  973. request_module("ide-cd");
  974. if (drive->media == ide_tape)
  975. request_module("ide-tape");
  976. if (drive->media == ide_floppy)
  977. request_module("ide-floppy");
  978. return NULL;
  979. }
  980. static struct kobject *exact_match(dev_t dev, int *part, void *data)
  981. {
  982. struct gendisk *p = data;
  983. *part &= (1 << PARTN_BITS) - 1;
  984. return &p->dev.kobj;
  985. }
  986. static int exact_lock(dev_t dev, void *data)
  987. {
  988. struct gendisk *p = data;
  989. if (!get_disk(p))
  990. return -1;
  991. return 0;
  992. }
  993. void ide_register_region(struct gendisk *disk)
  994. {
  995. blk_register_region(MKDEV(disk->major, disk->first_minor),
  996. disk->minors, NULL, exact_match, exact_lock, disk);
  997. }
  998. EXPORT_SYMBOL_GPL(ide_register_region);
  999. void ide_unregister_region(struct gendisk *disk)
  1000. {
  1001. blk_unregister_region(MKDEV(disk->major, disk->first_minor),
  1002. disk->minors);
  1003. }
  1004. EXPORT_SYMBOL_GPL(ide_unregister_region);
  1005. void ide_init_disk(struct gendisk *disk, ide_drive_t *drive)
  1006. {
  1007. ide_hwif_t *hwif = drive->hwif;
  1008. unsigned int unit = (drive->select.all >> 4) & 1;
  1009. disk->major = hwif->major;
  1010. disk->first_minor = unit << PARTN_BITS;
  1011. sprintf(disk->disk_name, "hd%c", 'a' + hwif->index * MAX_DRIVES + unit);
  1012. disk->queue = drive->queue;
  1013. }
  1014. EXPORT_SYMBOL_GPL(ide_init_disk);
  1015. static void ide_remove_drive_from_hwgroup(ide_drive_t *drive)
  1016. {
  1017. ide_hwgroup_t *hwgroup = drive->hwif->hwgroup;
  1018. if (drive == drive->next) {
  1019. /* special case: last drive from hwgroup. */
  1020. BUG_ON(hwgroup->drive != drive);
  1021. hwgroup->drive = NULL;
  1022. } else {
  1023. ide_drive_t *walk;
  1024. walk = hwgroup->drive;
  1025. while (walk->next != drive)
  1026. walk = walk->next;
  1027. walk->next = drive->next;
  1028. if (hwgroup->drive == drive) {
  1029. hwgroup->drive = drive->next;
  1030. hwgroup->hwif = hwgroup->drive->hwif;
  1031. }
  1032. }
  1033. BUG_ON(hwgroup->drive == drive);
  1034. }
  1035. static void drive_release_dev (struct device *dev)
  1036. {
  1037. ide_drive_t *drive = container_of(dev, ide_drive_t, gendev);
  1038. spin_lock_irq(&ide_lock);
  1039. ide_remove_drive_from_hwgroup(drive);
  1040. kfree(drive->id);
  1041. drive->id = NULL;
  1042. drive->present = 0;
  1043. /* Messed up locking ... */
  1044. spin_unlock_irq(&ide_lock);
  1045. blk_cleanup_queue(drive->queue);
  1046. spin_lock_irq(&ide_lock);
  1047. drive->queue = NULL;
  1048. spin_unlock_irq(&ide_lock);
  1049. complete(&drive->gendev_rel_comp);
  1050. }
  1051. static int hwif_init(ide_hwif_t *hwif)
  1052. {
  1053. int old_irq;
  1054. if (!hwif->irq) {
  1055. if (!(hwif->irq = ide_default_irq(hwif->io_ports[IDE_DATA_OFFSET])))
  1056. {
  1057. printk("%s: DISABLED, NO IRQ\n", hwif->name);
  1058. return 0;
  1059. }
  1060. }
  1061. #ifdef CONFIG_BLK_DEV_HD
  1062. if (hwif->irq == HD_IRQ && hwif->io_ports[IDE_DATA_OFFSET] != HD_DATA) {
  1063. printk("%s: CANNOT SHARE IRQ WITH OLD "
  1064. "HARDDISK DRIVER (hd.c)\n", hwif->name);
  1065. return 0;
  1066. }
  1067. #endif /* CONFIG_BLK_DEV_HD */
  1068. if (register_blkdev(hwif->major, hwif->name))
  1069. return 0;
  1070. if (!hwif->sg_max_nents)
  1071. hwif->sg_max_nents = PRD_ENTRIES;
  1072. hwif->sg_table = kmalloc(sizeof(struct scatterlist)*hwif->sg_max_nents,
  1073. GFP_KERNEL);
  1074. if (!hwif->sg_table) {
  1075. printk(KERN_ERR "%s: unable to allocate SG table.\n", hwif->name);
  1076. goto out;
  1077. }
  1078. sg_init_table(hwif->sg_table, hwif->sg_max_nents);
  1079. if (init_irq(hwif) == 0)
  1080. goto done;
  1081. old_irq = hwif->irq;
  1082. /*
  1083. * It failed to initialise. Find the default IRQ for
  1084. * this port and try that.
  1085. */
  1086. if (!(hwif->irq = ide_default_irq(hwif->io_ports[IDE_DATA_OFFSET]))) {
  1087. printk("%s: Disabled unable to get IRQ %d.\n",
  1088. hwif->name, old_irq);
  1089. goto out;
  1090. }
  1091. if (init_irq(hwif)) {
  1092. printk("%s: probed IRQ %d and default IRQ %d failed.\n",
  1093. hwif->name, old_irq, hwif->irq);
  1094. goto out;
  1095. }
  1096. printk("%s: probed IRQ %d failed, using default.\n",
  1097. hwif->name, hwif->irq);
  1098. done:
  1099. blk_register_region(MKDEV(hwif->major, 0), MAX_DRIVES << PARTN_BITS,
  1100. THIS_MODULE, ata_probe, ata_lock, hwif);
  1101. return 1;
  1102. out:
  1103. unregister_blkdev(hwif->major, hwif->name);
  1104. return 0;
  1105. }
  1106. static void hwif_register_devices(ide_hwif_t *hwif)
  1107. {
  1108. unsigned int i;
  1109. for (i = 0; i < MAX_DRIVES; i++) {
  1110. ide_drive_t *drive = &hwif->drives[i];
  1111. struct device *dev = &drive->gendev;
  1112. int ret;
  1113. if (!drive->present)
  1114. continue;
  1115. ide_add_generic_settings(drive);
  1116. snprintf(dev->bus_id, BUS_ID_SIZE, "%u.%u", hwif->index, i);
  1117. dev->parent = &hwif->gendev;
  1118. dev->bus = &ide_bus_type;
  1119. dev->driver_data = drive;
  1120. dev->release = drive_release_dev;
  1121. ret = device_register(dev);
  1122. if (ret < 0)
  1123. printk(KERN_WARNING "IDE: %s: device_register error: "
  1124. "%d\n", __func__, ret);
  1125. }
  1126. }
  1127. static void ide_port_init_devices(ide_hwif_t *hwif)
  1128. {
  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. if ((hwif->host_flags & IDE_HFLAG_NO_AUTOTUNE) == 0)
  1139. drive->autotune = 1;
  1140. }
  1141. if (hwif->port_init_devs)
  1142. hwif->port_init_devs(hwif);
  1143. }
  1144. static void ide_init_port(ide_hwif_t *hwif, unsigned int port,
  1145. const struct ide_port_info *d)
  1146. {
  1147. if (d->chipset != ide_etrax100)
  1148. hwif->channel = port;
  1149. if (d->chipset)
  1150. hwif->chipset = d->chipset;
  1151. if (d->init_iops)
  1152. d->init_iops(hwif);
  1153. if ((d->host_flags & IDE_HFLAG_NO_DMA) == 0)
  1154. ide_hwif_setup_dma(hwif, d);
  1155. if ((!hwif->irq && (d->host_flags & IDE_HFLAG_LEGACY_IRQS)) ||
  1156. (d->host_flags & IDE_HFLAG_FORCE_LEGACY_IRQS))
  1157. hwif->irq = port ? 15 : 14;
  1158. hwif->host_flags = d->host_flags;
  1159. hwif->pio_mask = d->pio_mask;
  1160. if ((d->host_flags & IDE_HFLAG_SERIALIZE) && hwif->mate)
  1161. hwif->mate->serialized = hwif->serialized = 1;
  1162. hwif->swdma_mask = d->swdma_mask;
  1163. hwif->mwdma_mask = d->mwdma_mask;
  1164. hwif->ultra_mask = d->udma_mask;
  1165. /* reset DMA masks only for SFF-style DMA controllers */
  1166. if ((d->host_flags && IDE_HFLAG_NO_DMA) == 0 && hwif->dma_base == 0)
  1167. hwif->swdma_mask = hwif->mwdma_mask = hwif->ultra_mask = 0;
  1168. if (d->host_flags & IDE_HFLAG_RQSIZE_256)
  1169. hwif->rqsize = 256;
  1170. /* call chipset specific routine for each enabled port */
  1171. if (d->init_hwif)
  1172. d->init_hwif(hwif);
  1173. if (hwif->cable_detect && (hwif->ultra_mask & 0x78)) {
  1174. if (hwif->cbl != ATA_CBL_PATA40_SHORT)
  1175. hwif->cbl = hwif->cable_detect(hwif);
  1176. }
  1177. }
  1178. int ide_device_add_all(u8 *idx, const struct ide_port_info *d)
  1179. {
  1180. ide_hwif_t *hwif, *mate = NULL;
  1181. int i, rc = 0;
  1182. for (i = 0; i < MAX_HWIFS; i++) {
  1183. if (d == NULL || idx[i] == 0xff) {
  1184. mate = NULL;
  1185. continue;
  1186. }
  1187. hwif = &ide_hwifs[idx[i]];
  1188. if (d->chipset != ide_etrax100 && (i & 1) && mate) {
  1189. hwif->mate = mate;
  1190. mate->mate = hwif;
  1191. }
  1192. mate = (i & 1) ? NULL : hwif;
  1193. ide_init_port(hwif, i & 1, d);
  1194. ide_port_init_devices(hwif);
  1195. }
  1196. for (i = 0; i < MAX_HWIFS; i++) {
  1197. if (idx[i] == 0xff)
  1198. continue;
  1199. hwif = &ide_hwifs[idx[i]];
  1200. if ((hwif->chipset != ide_4drives || !hwif->mate ||
  1201. !hwif->mate->present) && ide_hwif_request_regions(hwif)) {
  1202. printk(KERN_ERR "%s: ports already in use, "
  1203. "skipping probe\n", hwif->name);
  1204. continue;
  1205. }
  1206. if (ide_probe_port(hwif) < 0) {
  1207. ide_hwif_release_regions(hwif);
  1208. continue;
  1209. }
  1210. hwif->present = 1;
  1211. if (hwif->chipset != ide_4drives || !hwif->mate ||
  1212. !hwif->mate->present)
  1213. ide_register_port(hwif);
  1214. ide_port_tune_devices(hwif);
  1215. }
  1216. for (i = 0; i < MAX_HWIFS; i++) {
  1217. if (idx[i] == 0xff)
  1218. continue;
  1219. hwif = &ide_hwifs[idx[i]];
  1220. if (!hwif->present)
  1221. continue;
  1222. if (hwif_init(hwif) == 0) {
  1223. printk(KERN_INFO "%s: failed to initialize IDE "
  1224. "interface\n", hwif->name);
  1225. hwif->present = 0;
  1226. rc = -1;
  1227. continue;
  1228. }
  1229. ide_acpi_init(hwif);
  1230. ide_acpi_port_init_devices(hwif);
  1231. }
  1232. for (i = 0; i < MAX_HWIFS; i++) {
  1233. if (idx[i] == 0xff)
  1234. continue;
  1235. hwif = &ide_hwifs[idx[i]];
  1236. if (hwif->present) {
  1237. if (hwif->chipset == ide_unknown ||
  1238. hwif->chipset == ide_forced)
  1239. hwif->chipset = ide_generic;
  1240. hwif_register_devices(hwif);
  1241. }
  1242. }
  1243. for (i = 0; i < MAX_HWIFS; i++) {
  1244. if (idx[i] == 0xff)
  1245. continue;
  1246. hwif = &ide_hwifs[idx[i]];
  1247. if (hwif->present) {
  1248. ide_proc_register_port(hwif);
  1249. ide_proc_port_register_devices(hwif);
  1250. }
  1251. }
  1252. return rc;
  1253. }
  1254. EXPORT_SYMBOL_GPL(ide_device_add_all);
  1255. int ide_device_add(u8 idx[4], const struct ide_port_info *d)
  1256. {
  1257. u8 idx_all[MAX_HWIFS];
  1258. int i;
  1259. for (i = 0; i < MAX_HWIFS; i++)
  1260. idx_all[i] = (i < 4) ? idx[i] : 0xff;
  1261. return ide_device_add_all(idx_all, d);
  1262. }
  1263. EXPORT_SYMBOL_GPL(ide_device_add);