ide-probe.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:30k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  *  linux/drivers/ide/ide-probe.c Version 1.07 March 18, 2001
  3.  *
  4.  *  Copyright (C) 1994-1998  Linus Torvalds & authors (see below)
  5.  */
  6. /*
  7.  *  Mostly written by Mark Lord <mlord@pobox.com>
  8.  *                and Gadi Oxman <gadio@netvision.net.il>
  9.  *                and Andre Hedrick <andre@linux-ide.org>
  10.  *
  11.  *  See linux/MAINTAINERS for address of current maintainer.
  12.  *
  13.  * This is the IDE probe module, as evolved from hd.c and ide.c.
  14.  *
  15.  * Version 1.00 move drive probing code from ide.c to ide-probe.c
  16.  * Version 1.01 fix compilation problem for m68k
  17.  * Version 1.02 increase WAIT_PIDENTIFY to avoid CD-ROM locking at boot
  18.  *  by Andrea Arcangeli
  19.  * Version 1.03 fix for (hwif->chipset == ide_4drives)
  20.  * Version 1.04 fixed buggy treatments of known flash memory cards
  21.  *
  22.  * Version 1.05 fix for (hwif->chipset == ide_pdc4030)
  23.  * added ide6/7/8/9
  24.  * allowed for secondary flash card to be detectable
  25.  *  with new flag : drive->ata_flash : 1;
  26.  * Version 1.06 stream line request queue and prep for cascade project.
  27.  * Version 1.07 max_sect <= 255; slower disks would get behind and
  28.  *  then fall over when they get to 256. Paul G.
  29.  */
  30. #undef REALLY_SLOW_IO /* most systems can safely undef this */
  31. #include <linux/config.h>
  32. #include <linux/module.h>
  33. #include <linux/types.h>
  34. #include <linux/string.h>
  35. #include <linux/kernel.h>
  36. #include <linux/timer.h>
  37. #include <linux/mm.h>
  38. #include <linux/interrupt.h>
  39. #include <linux/major.h>
  40. #include <linux/errno.h>
  41. #include <linux/genhd.h>
  42. #include <linux/slab.h>
  43. #include <linux/delay.h>
  44. #include <linux/ide.h>
  45. #include <linux/spinlock.h>
  46. #include <asm/byteorder.h>
  47. #include <asm/irq.h>
  48. #include <asm/uaccess.h>
  49. #include <asm/io.h>
  50. static inline void do_identify (ide_drive_t *drive, byte cmd)
  51. {
  52. int bswap = 1;
  53. struct hd_driveid *id;
  54. id = drive->id = kmalloc (SECTOR_WORDS*4, GFP_ATOMIC); /* called with interrupts disabled! */
  55. if (!id) {
  56. printk(KERN_WARNING "(ide-probe::do_identify) Out of memory.n");
  57. goto err_kmalloc;
  58. }
  59. ide_input_data(drive, id, SECTOR_WORDS); /* read 512 bytes of id info */
  60. ide__sti(); /* local CPU only */
  61. ide_fix_driveid(id);
  62. if (id->word156 == 0x4d42) {
  63. printk("%s: drive->id->word156 == 0x%04x n", drive->name, drive->id->word156);
  64. }
  65. if (!drive->forced_lun)
  66. drive->last_lun = id->last_lun & 0x7;
  67. #if defined (CONFIG_SCSI_EATA_DMA) || defined (CONFIG_SCSI_EATA_PIO) || defined (CONFIG_SCSI_EATA)
  68. /*
  69.  * EATA SCSI controllers do a hardware ATA emulation:
  70.  * Ignore them if there is a driver for them available.
  71.  */
  72. if ((id->model[0] == 'P' && id->model[1] == 'M')
  73.  || (id->model[0] == 'S' && id->model[1] == 'K')) {
  74. printk("%s: EATA SCSI HBA %.10sn", drive->name, id->model);
  75. goto err_misc;
  76. }
  77. #endif /* CONFIG_SCSI_EATA_DMA || CONFIG_SCSI_EATA_PIO */
  78. /*
  79.  *  WIN_IDENTIFY returns little-endian info,
  80.  *  WIN_PIDENTIFY *usually* returns little-endian info.
  81.  */
  82. if (cmd == WIN_PIDENTIFY) {
  83. if ((id->model[0] == 'N' && id->model[1] == 'E') /* NEC */
  84.  || (id->model[0] == 'F' && id->model[1] == 'X') /* Mitsumi */
  85.  || (id->model[0] == 'P' && id->model[1] == 'i'))/* Pioneer */
  86. bswap ^= 1; /* Vertos drives may still be weird */
  87. }
  88. ide_fixstring (id->model,     sizeof(id->model),     bswap);
  89. ide_fixstring (id->fw_rev,    sizeof(id->fw_rev),    bswap);
  90. ide_fixstring (id->serial_no, sizeof(id->serial_no), bswap);
  91. if (strstr(id->model, "E X A B Y T E N E S T"))
  92. goto err_misc;
  93. id->model[sizeof(id->model)-1] = ''; /* we depend on this a lot! */
  94. printk("%s: %s, ", drive->name, id->model);
  95. drive->present = 1;
  96. /*
  97.  * Check for an ATAPI device
  98.  */
  99. if (cmd == WIN_PIDENTIFY) {
  100. byte type = (id->config >> 8) & 0x1f;
  101. printk("ATAPI ");
  102. #ifdef CONFIG_BLK_DEV_PDC4030
  103. if (HWIF(drive)->channel == 1 && HWIF(drive)->chipset == ide_pdc4030) {
  104. printk(" -- not supported on 2nd Promise portn");
  105. goto err_misc;
  106. }
  107. #endif /* CONFIG_BLK_DEV_PDC4030 */
  108. switch (type) {
  109. case ide_floppy:
  110. if (!strstr(id->model, "CD-ROM")) {
  111. if (!strstr(id->model, "oppy") && !strstr(id->model, "poyp") && !strstr(id->model, "ZIP"))
  112. printk("cdrom or floppy?, assuming ");
  113. if (drive->media != ide_cdrom) {
  114. printk ("FLOPPY");
  115. break;
  116. }
  117. }
  118. type = ide_cdrom; /* Early cdrom models used zero */
  119. case ide_cdrom:
  120. drive->removable = 1;
  121. #ifdef CONFIG_PPC
  122. /* kludge for Apple PowerBook internal zip */
  123. if (!strstr(id->model, "CD-ROM") && strstr(id->model, "ZIP")) {
  124. printk ("FLOPPY");
  125. type = ide_floppy;
  126. break;
  127. }
  128. #endif
  129. printk ("CD/DVD-ROM");
  130. break;
  131. case ide_tape:
  132. printk ("TAPE");
  133. break;
  134. case ide_optical:
  135. printk ("OPTICAL");
  136. drive->removable = 1;
  137. break;
  138. default:
  139. printk("UNKNOWN (type %d)", type);
  140. break;
  141. }
  142. printk (" driven");
  143. drive->media = type;
  144. return;
  145. }
  146. /*
  147.  * Not an ATAPI device: looks like a "regular" hard disk
  148.  */
  149. if (id->config & (1<<7))
  150. drive->removable = 1;
  151. /*
  152.  * Prevent long system lockup probing later for non-existant
  153.  * slave drive if the hwif is actually a flash memory card of some variety:
  154.  */
  155. if (drive_is_flashcard(drive)) {
  156. ide_drive_t *mate = &HWIF(drive)->drives[1^drive->select.b.unit];
  157. if (!mate->ata_flash) {
  158. mate->present = 0;
  159. mate->noprobe = 1;
  160. }
  161. }
  162. drive->media = ide_disk;
  163. printk("ATA DISK driven");
  164. QUIRK_LIST(HWIF(drive),drive);
  165. return;
  166. err_misc:
  167. kfree(id);
  168. err_kmalloc:
  169. drive->present = 0;
  170. return;
  171. }
  172. /*
  173.  * try_to_identify() sends an ATA(PI) IDENTIFY request to a drive
  174.  * and waits for a response.  It also monitors irqs while this is
  175.  * happening, in hope of automatically determining which one is
  176.  * being used by the interface.
  177.  *
  178.  * Returns: 0  device was identified
  179.  * 1  device timed-out (no response to identify request)
  180.  * 2  device aborted the command (refused to identify itself)
  181.  */
  182. static int actual_try_to_identify (ide_drive_t *drive, byte cmd)
  183. {
  184. int rc;
  185. ide_ioreg_t hd_status;
  186. unsigned long timeout;
  187. byte s, a;
  188. if (IDE_CONTROL_REG) {
  189. /* take a deep breath */
  190. ide_delay_50ms();
  191. a = IN_BYTE(IDE_ALTSTATUS_REG);
  192. s = IN_BYTE(IDE_STATUS_REG);
  193. if ((a ^ s) & ~INDEX_STAT) {
  194. printk("%s: probing with STATUS(0x%02x) instead of ALTSTATUS(0x%02x)n", drive->name, s, a);
  195. hd_status = IDE_STATUS_REG; /* ancient Seagate drives, broken interfaces */
  196. } else {
  197. hd_status = IDE_ALTSTATUS_REG; /* use non-intrusive polling */
  198. }
  199. } else {
  200. ide_delay_50ms();
  201. hd_status = IDE_STATUS_REG;
  202. }
  203. /* set features register for atapi identify command to be sure of reply */
  204. if ((cmd == WIN_PIDENTIFY))
  205. OUT_BYTE(0,IDE_FEATURE_REG); /* disable dma & overlap */
  206. #if CONFIG_BLK_DEV_PDC4030
  207. if (HWIF(drive)->chipset == ide_pdc4030) {
  208. /* DC4030 hosted drives need their own identify... */
  209. extern int pdc4030_identify(ide_drive_t *);
  210. if (pdc4030_identify(drive)) {
  211. return 1;
  212. }
  213. } else
  214. #endif /* CONFIG_BLK_DEV_PDC4030 */
  215. OUT_BYTE(cmd,IDE_COMMAND_REG); /* ask drive for ID */
  216. timeout = ((cmd == WIN_IDENTIFY) ? WAIT_WORSTCASE : WAIT_PIDENTIFY) / 2;
  217. timeout += jiffies;
  218. do {
  219. if (0 < (signed long)(jiffies - timeout)) {
  220. return 1; /* drive timed-out */
  221. }
  222. ide_delay_50ms(); /* give drive a breather */
  223. } while (IN_BYTE(hd_status) & BUSY_STAT);
  224. ide_delay_50ms(); /* wait for IRQ and DRQ_STAT */
  225. if (OK_STAT(GET_STAT(),DRQ_STAT,BAD_R_STAT)) {
  226. unsigned long flags;
  227. __save_flags(flags); /* local CPU only */
  228. __cli(); /* local CPU only; some systems need this */
  229. do_identify(drive, cmd); /* drive returned ID */
  230. rc = 0; /* drive responded with ID */
  231. (void) GET_STAT(); /* clear drive IRQ */
  232. __restore_flags(flags); /* local CPU only */
  233. } else
  234. rc = 2; /* drive refused ID */
  235. return rc;
  236. }
  237. static int try_to_identify (ide_drive_t *drive, byte cmd)
  238. {
  239. int retval;
  240. int autoprobe = 0;
  241. unsigned long cookie = 0;
  242. if (IDE_CONTROL_REG && !HWIF(drive)->irq) {
  243. autoprobe = 1;
  244. cookie = probe_irq_on();
  245. OUT_BYTE(drive->ctl,IDE_CONTROL_REG); /* enable device irq */
  246. }
  247. retval = actual_try_to_identify(drive, cmd);
  248. if (autoprobe) {
  249. int irq;
  250. OUT_BYTE(drive->ctl|2,IDE_CONTROL_REG); /* mask device irq */
  251. (void) GET_STAT(); /* clear drive IRQ */
  252. udelay(5);
  253. irq = probe_irq_off(cookie);
  254. if (!HWIF(drive)->irq) {
  255. if (irq > 0) {
  256. HWIF(drive)->irq = irq;
  257. } else { /* Mmmm.. multiple IRQs.. don't know which was ours */
  258. printk("%s: IRQ probe failed (0x%lx)n", drive->name, cookie);
  259. #ifdef CONFIG_BLK_DEV_CMD640
  260. #ifdef CMD640_DUMP_REGS
  261. if (HWIF(drive)->chipset == ide_cmd640) {
  262. printk("%s: Hmmm.. probably a driver problem.n", drive->name);
  263. CMD640_DUMP_REGS;
  264. }
  265. #endif /* CMD640_DUMP_REGS */
  266. #endif /* CONFIG_BLK_DEV_CMD640 */
  267. }
  268. }
  269. }
  270. return retval;
  271. }
  272. /*
  273.  * do_probe() has the difficult job of finding a drive if it exists,
  274.  * without getting hung up if it doesn't exist, without trampling on
  275.  * ethernet cards, and without leaving any IRQs dangling to haunt us later.
  276.  *
  277.  * If a drive is "known" to exist (from CMOS or kernel parameters),
  278.  * but does not respond right away, the probe will "hang in there"
  279.  * for the maximum wait time (about 30 seconds), otherwise it will
  280.  * exit much more quickly.
  281.  *
  282.  * Returns: 0  device was identified
  283.  * 1  device timed-out (no response to identify request)
  284.  * 2  device aborted the command (refused to identify itself)
  285.  * 3  bad status from device (possible for ATAPI drives)
  286.  * 4  probe was not attempted because failure was obvious
  287.  */
  288. static int do_probe (ide_drive_t *drive, byte cmd)
  289. {
  290. int rc;
  291. ide_hwif_t *hwif = HWIF(drive);
  292. if (drive->present) { /* avoid waiting for inappropriate probes */
  293. if ((drive->media != ide_disk) && (cmd == WIN_IDENTIFY))
  294. return 4;
  295. }
  296. #ifdef DEBUG
  297. printk("probing for %s: present=%d, media=%d, probetype=%sn",
  298. drive->name, drive->present, drive->media,
  299. (cmd == WIN_IDENTIFY) ? "ATA" : "ATAPI");
  300. #endif
  301. ide_delay_50ms(); /* needed for some systems (e.g. crw9624 as drive0 with disk as slave) */
  302. SELECT_DRIVE(hwif,drive);
  303. ide_delay_50ms();
  304. if (IN_BYTE(IDE_SELECT_REG) != drive->select.all && !drive->present) {
  305. if (drive->select.b.unit != 0) {
  306. SELECT_DRIVE(hwif,&hwif->drives[0]); /* exit with drive0 selected */
  307. ide_delay_50ms(); /* allow BUSY_STAT to assert & clear */
  308. }
  309. return 3;    /* no i/f present: mmm.. this should be a 4 -ml */
  310. }
  311. if (OK_STAT(GET_STAT(),READY_STAT,BUSY_STAT)
  312.  || drive->present || cmd == WIN_PIDENTIFY)
  313. {
  314. if ((rc = try_to_identify(drive,cmd)))   /* send cmd and wait */
  315. rc = try_to_identify(drive,cmd); /* failed: try again */
  316. if (rc == 1 && cmd == WIN_PIDENTIFY && drive->autotune != 2) {
  317. unsigned long timeout;
  318. printk("%s: no response (status = 0x%02x), resetting driven", drive->name, GET_STAT());
  319. ide_delay_50ms();
  320. OUT_BYTE (drive->select.all, IDE_SELECT_REG);
  321. ide_delay_50ms();
  322. OUT_BYTE(WIN_SRST, IDE_COMMAND_REG);
  323. timeout = jiffies;
  324. while ((GET_STAT() & BUSY_STAT) && time_before(jiffies, timeout + WAIT_WORSTCASE))
  325. ide_delay_50ms();
  326. rc = try_to_identify(drive, cmd);
  327. }
  328. if (rc == 1)
  329. printk("%s: no response (status = 0x%02x)n", drive->name, GET_STAT());
  330. (void) GET_STAT(); /* ensure drive irq is clear */
  331. } else {
  332. rc = 3; /* not present or maybe ATAPI */
  333. }
  334. if (drive->select.b.unit != 0) {
  335. SELECT_DRIVE(hwif,&hwif->drives[0]); /* exit with drive0 selected */
  336. ide_delay_50ms();
  337. (void) GET_STAT(); /* ensure drive irq is clear */
  338. }
  339. return rc;
  340. }
  341. /*
  342.  *
  343.  */
  344. static void enable_nest (ide_drive_t *drive)
  345. {
  346. unsigned long timeout;
  347. printk("%s: enabling %s -- ", HWIF(drive)->name, drive->id->model);
  348. SELECT_DRIVE(HWIF(drive), drive);
  349. ide_delay_50ms();
  350. OUT_BYTE(EXABYTE_ENABLE_NEST, IDE_COMMAND_REG);
  351. timeout = jiffies + WAIT_WORSTCASE;
  352. do {
  353. if (time_after(jiffies, timeout)) {
  354. printk("failed (timeout)n");
  355. return;
  356. }
  357. ide_delay_50ms();
  358. } while (GET_STAT() & BUSY_STAT);
  359. ide_delay_50ms();
  360. if (!OK_STAT(GET_STAT(), 0, BAD_STAT))
  361. printk("failed (status = 0x%02x)n", GET_STAT());
  362. else
  363. printk("successn");
  364. if (do_probe(drive, WIN_IDENTIFY) >= 2) { /* if !(success||timed-out) */
  365. (void) do_probe(drive, WIN_PIDENTIFY); /* look for ATAPI device */
  366. }
  367. }
  368. /*
  369.  * probe_for_drive() tests for existence of a given drive using do_probe().
  370.  *
  371.  * Returns: 0  no device was found
  372.  * 1  device was found (note: drive->present might still be 0)
  373.  */
  374. static inline byte probe_for_drive (ide_drive_t *drive)
  375. {
  376. if (drive->noprobe) /* skip probing? */
  377. return drive->present;
  378. if (do_probe(drive, WIN_IDENTIFY) >= 2) { /* if !(success||timed-out) */
  379. (void) do_probe(drive, WIN_PIDENTIFY); /* look for ATAPI device */
  380. }
  381. if (drive->id && strstr(drive->id->model, "E X A B Y T E N E S T"))
  382. enable_nest(drive);
  383. if (!drive->present)
  384. return 0; /* drive not found */
  385. if (drive->id == NULL) { /* identification failed? */
  386. if (drive->media == ide_disk) {
  387. printk ("%s: non-IDE drive, CHS=%d/%d/%dn",
  388.  drive->name, drive->cyl, drive->head, drive->sect);
  389. } else if (drive->media == ide_cdrom) {
  390. printk("%s: ATAPI cdrom (?)n", drive->name);
  391. } else {
  392. drive->present = 0; /* nuke it */
  393. }
  394. }
  395. return 1; /* drive was found */
  396. }
  397. /*
  398.  * Calculate the region that this interface occupies,
  399.  * handling interfaces where the registers may not be
  400.  * ordered sanely.  We deal with the CONTROL register
  401.  * separately.
  402.  */
  403. static int hwif_check_regions (ide_hwif_t *hwif)
  404. {
  405. int region_errors = 0;
  406. hwif->straight8 = 0;
  407. region_errors  = ide_check_region(hwif->io_ports[IDE_DATA_OFFSET], 1);
  408. region_errors += ide_check_region(hwif->io_ports[IDE_ERROR_OFFSET], 1);
  409. region_errors += ide_check_region(hwif->io_ports[IDE_NSECTOR_OFFSET], 1);
  410. region_errors += ide_check_region(hwif->io_ports[IDE_SECTOR_OFFSET], 1);
  411. region_errors += ide_check_region(hwif->io_ports[IDE_LCYL_OFFSET], 1);
  412. region_errors += ide_check_region(hwif->io_ports[IDE_HCYL_OFFSET], 1);
  413. region_errors += ide_check_region(hwif->io_ports[IDE_SELECT_OFFSET], 1);
  414. region_errors += ide_check_region(hwif->io_ports[IDE_STATUS_OFFSET], 1);
  415. if (hwif->io_ports[IDE_CONTROL_OFFSET])
  416. region_errors += ide_check_region(hwif->io_ports[IDE_CONTROL_OFFSET], 1);
  417. #if defined(CONFIG_AMIGA) || defined(CONFIG_MAC)
  418. if (hwif->io_ports[IDE_IRQ_OFFSET])
  419. region_errors += ide_check_region(hwif->io_ports[IDE_IRQ_OFFSET], 1);
  420. #endif /* (CONFIG_AMIGA) || (CONFIG_MAC) */
  421. /*
  422.  * If any errors are return, we drop the hwif interface.
  423.  */
  424. return(region_errors);
  425. }
  426. static void hwif_register (ide_hwif_t *hwif)
  427. {
  428. if (((unsigned long)hwif->io_ports[IDE_DATA_OFFSET] | 7) ==
  429.     ((unsigned long)hwif->io_ports[IDE_STATUS_OFFSET])) {
  430. ide_request_region(hwif->io_ports[IDE_DATA_OFFSET], 8, hwif->name);
  431. hwif->straight8 = 1;
  432. goto jump_straight8;
  433. }
  434. if (hwif->io_ports[IDE_DATA_OFFSET])
  435. ide_request_region(hwif->io_ports[IDE_DATA_OFFSET], 1, hwif->name);
  436. if (hwif->io_ports[IDE_ERROR_OFFSET])
  437. ide_request_region(hwif->io_ports[IDE_ERROR_OFFSET], 1, hwif->name);
  438. if (hwif->io_ports[IDE_NSECTOR_OFFSET])
  439. ide_request_region(hwif->io_ports[IDE_NSECTOR_OFFSET], 1, hwif->name);
  440. if (hwif->io_ports[IDE_SECTOR_OFFSET])
  441. ide_request_region(hwif->io_ports[IDE_SECTOR_OFFSET], 1, hwif->name);
  442. if (hwif->io_ports[IDE_LCYL_OFFSET])
  443. ide_request_region(hwif->io_ports[IDE_LCYL_OFFSET], 1, hwif->name);
  444. if (hwif->io_ports[IDE_HCYL_OFFSET])
  445. ide_request_region(hwif->io_ports[IDE_HCYL_OFFSET], 1, hwif->name);
  446. if (hwif->io_ports[IDE_SELECT_OFFSET])
  447. ide_request_region(hwif->io_ports[IDE_SELECT_OFFSET], 1, hwif->name);
  448. if (hwif->io_ports[IDE_STATUS_OFFSET])
  449. ide_request_region(hwif->io_ports[IDE_STATUS_OFFSET], 1, hwif->name);
  450. jump_straight8:
  451. if (hwif->io_ports[IDE_CONTROL_OFFSET])
  452. ide_request_region(hwif->io_ports[IDE_CONTROL_OFFSET], 1, hwif->name);
  453. #if defined(CONFIG_AMIGA) || defined(CONFIG_MAC)
  454. if (hwif->io_ports[IDE_IRQ_OFFSET])
  455. ide_request_region(hwif->io_ports[IDE_IRQ_OFFSET], 1, hwif->name);
  456. #endif /* (CONFIG_AMIGA) || (CONFIG_MAC) */
  457. }
  458. /*
  459.  * This routine only knows how to look for drive units 0 and 1
  460.  * on an interface, so any setting of MAX_DRIVES > 2 won't work here.
  461.  */
  462. static void probe_hwif (ide_hwif_t *hwif)
  463. {
  464. unsigned int unit;
  465. unsigned long flags;
  466. if (hwif->noprobe)
  467. return;
  468. #ifdef CONFIG_BLK_DEV_IDE
  469. if (hwif->io_ports[IDE_DATA_OFFSET] == HD_DATA) {
  470. extern void probe_cmos_for_drives(ide_hwif_t *);
  471. probe_cmos_for_drives (hwif);
  472. }
  473. #endif
  474. if ((hwif->chipset != ide_4drives || !hwif->mate->present) &&
  475. #if CONFIG_BLK_DEV_PDC4030
  476.     (hwif->chipset != ide_pdc4030 || hwif->channel == 0) &&
  477. #endif /* CONFIG_BLK_DEV_PDC4030 */
  478.     (hwif_check_regions(hwif))) {
  479. int msgout = 0;
  480. for (unit = 0; unit < MAX_DRIVES; ++unit) {
  481. ide_drive_t *drive = &hwif->drives[unit];
  482. if (drive->present) {
  483. drive->present = 0;
  484. printk("%s: ERROR, PORTS ALREADY IN USEn", drive->name);
  485. msgout = 1;
  486. }
  487. }
  488. if (!msgout)
  489. printk("%s: ports already in use, skipping proben", hwif->name);
  490. return;
  491. }
  492. __save_flags(flags); /* local CPU only */
  493. __sti(); /* local CPU only; needed for jiffies and irq probing */
  494. /*
  495.  * Second drive should only exist if first drive was found,
  496.  * but a lot of cdrom drives are configured as single slaves.
  497.  */
  498. for (unit = 0; unit < MAX_DRIVES; ++unit) {
  499. ide_drive_t *drive = &hwif->drives[unit];
  500. (void) probe_for_drive (drive);
  501. if (drive->present && !hwif->present) {
  502. hwif->present = 1;
  503. if (hwif->chipset != ide_4drives || !hwif->mate->present) {
  504. hwif_register(hwif);
  505. }
  506. }
  507. }
  508. if (hwif->io_ports[IDE_CONTROL_OFFSET] && hwif->reset) {
  509. unsigned long timeout = jiffies + WAIT_WORSTCASE;
  510. byte stat;
  511. printk("%s: resetn", hwif->name);
  512. OUT_BYTE(12, hwif->io_ports[IDE_CONTROL_OFFSET]);
  513. udelay(10);
  514. OUT_BYTE(8, hwif->io_ports[IDE_CONTROL_OFFSET]);
  515. do {
  516. ide_delay_50ms();
  517. stat = IN_BYTE(hwif->io_ports[IDE_STATUS_OFFSET]);
  518. } while ((stat & BUSY_STAT) && 0 < (signed long)(timeout - jiffies));
  519. }
  520. __restore_flags(flags); /* local CPU only */
  521. for (unit = 0; unit < MAX_DRIVES; ++unit) {
  522. ide_drive_t *drive = &hwif->drives[unit];
  523. if (drive->present) {
  524. ide_tuneproc_t *tuneproc = HWIF(drive)->tuneproc;
  525. if (tuneproc != NULL && drive->autotune == 1)
  526. tuneproc(drive, 255); /* auto-tune PIO mode */
  527. }
  528. }
  529. }
  530. #if MAX_HWIFS > 1
  531. /*
  532.  * save_match() is used to simplify logic in init_irq() below.
  533.  *
  534.  * A loophole here is that we may not know about a particular
  535.  * hwif's irq until after that hwif is actually probed/initialized..
  536.  * This could be a problem for the case where an hwif is on a
  537.  * dual interface that requires serialization (eg. cmd640) and another
  538.  * hwif using one of the same irqs is initialized beforehand.
  539.  *
  540.  * This routine detects and reports such situations, but does not fix them.
  541.  */
  542. static void save_match (ide_hwif_t *hwif, ide_hwif_t *new, ide_hwif_t **match)
  543. {
  544. ide_hwif_t *m = *match;
  545. if (m && m->hwgroup && m->hwgroup != new->hwgroup) {
  546. if (!new->hwgroup)
  547. return;
  548. printk("%s: potential irq problem with %s and %sn", hwif->name, new->name, m->name);
  549. }
  550. if (!m || m->irq != hwif->irq) /* don't undo a prior perfect match */
  551. *match = new;
  552. }
  553. #endif /* MAX_HWIFS > 1 */
  554. /*
  555.  * init request queue
  556.  */
  557. static void ide_init_queue(ide_drive_t *drive)
  558. {
  559. request_queue_t *q = &drive->queue;
  560. q->queuedata = HWGROUP(drive);
  561. blk_init_queue(q, do_ide_request);
  562. if (drive->media == ide_disk) {
  563. #ifdef CONFIG_BLK_DEV_ELEVATOR_NOOP
  564. elevator_init(&q->elevator, ELEVATOR_NOOP);
  565. #endif
  566. }
  567. }
  568. /*
  569.  * This routine sets up the irq for an ide interface, and creates a new
  570.  * hwgroup for the irq/hwif if none was previously assigned.
  571.  *
  572.  * Much of the code is for correctly detecting/handling irq sharing
  573.  * and irq serialization situations.  This is somewhat complex because
  574.  * it handles static as well as dynamic (PCMCIA) IDE interfaces.
  575.  *
  576.  * The SA_INTERRUPT in sa_flags means ide_intr() is always entered with
  577.  * interrupts completely disabled.  This can be bad for interrupt latency,
  578.  * but anything else has led to problems on some machines.  We re-enable
  579.  * interrupts as much as we can safely do in most places.
  580.  */
  581. static int init_irq (ide_hwif_t *hwif)
  582. {
  583. unsigned long flags;
  584. unsigned int index;
  585. ide_hwgroup_t *hwgroup, *new_hwgroup;
  586. ide_hwif_t *match = NULL;
  587. /* Allocate the buffer and potentially sleep first */
  588. new_hwgroup = kmalloc(sizeof(ide_hwgroup_t),GFP_KERNEL);
  589. save_flags(flags); /* all CPUs */
  590. cli(); /* all CPUs */
  591. hwif->hwgroup = NULL;
  592. #if MAX_HWIFS > 1
  593. /*
  594.  * Group up with any other hwifs that share our irq(s).
  595.  */
  596. for (index = 0; index < MAX_HWIFS; index++) {
  597. ide_hwif_t *h = &ide_hwifs[index];
  598. if (h->hwgroup) {  /* scan only initialized hwif's */
  599. if (hwif->irq == h->irq) {
  600. hwif->sharing_irq = h->sharing_irq = 1;
  601. if (hwif->chipset != ide_pci || h->chipset != ide_pci) {
  602. save_match(hwif, h, &match);
  603. }
  604. }
  605. if (hwif->serialized) {
  606. if (hwif->mate && hwif->mate->irq == h->irq)
  607. save_match(hwif, h, &match);
  608. }
  609. if (h->serialized) {
  610. if (h->mate && hwif->irq == h->mate->irq)
  611. save_match(hwif, h, &match);
  612. }
  613. }
  614. }
  615. #endif /* MAX_HWIFS > 1 */
  616. /*
  617.  * If we are still without a hwgroup, then form a new one
  618.  */
  619. if (match) {
  620. hwgroup = match->hwgroup;
  621. if(new_hwgroup)
  622. kfree(new_hwgroup);
  623. } else {
  624. hwgroup = new_hwgroup;
  625. if (!hwgroup) {
  626. restore_flags(flags); /* all CPUs */
  627. return 1;
  628. }
  629. memset(hwgroup, 0, sizeof(ide_hwgroup_t));
  630. hwgroup->hwif     = hwif->next = hwif;
  631. hwgroup->rq       = NULL;
  632. hwgroup->handler  = NULL;
  633. hwgroup->drive    = NULL;
  634. hwgroup->busy     = 0;
  635. init_timer(&hwgroup->timer);
  636. hwgroup->timer.function = &ide_timer_expiry;
  637. hwgroup->timer.data = (unsigned long) hwgroup;
  638. }
  639. /*
  640.  * Allocate the irq, if not already obtained for another hwif
  641.  */
  642. if (!match || match->irq != hwif->irq) {
  643. #ifdef CONFIG_IDEPCI_SHARE_IRQ
  644. int sa = IDE_CHIPSET_IS_PCI(hwif->chipset) ? SA_SHIRQ : SA_INTERRUPT;
  645. #else /* !CONFIG_IDEPCI_SHARE_IRQ */
  646. int sa = IDE_CHIPSET_IS_PCI(hwif->chipset) ? SA_INTERRUPT|SA_SHIRQ : SA_INTERRUPT;
  647. #endif /* CONFIG_IDEPCI_SHARE_IRQ */
  648. if (hwif->io_ports[IDE_CONTROL_OFFSET])
  649. OUT_BYTE(0x08, hwif->io_ports[IDE_CONTROL_OFFSET]); /* clear nIEN */
  650. if (ide_request_irq(hwif->irq, &ide_intr, sa, hwif->name, hwgroup)) {
  651. if (!match)
  652. kfree(hwgroup);
  653. restore_flags(flags); /* all CPUs */
  654. return 1;
  655. }
  656. }
  657. /*
  658.  * Everything is okay, so link us into the hwgroup
  659.  */
  660. hwif->hwgroup = hwgroup;
  661. hwif->next = hwgroup->hwif->next;
  662. hwgroup->hwif->next = hwif;
  663. for (index = 0; index < MAX_DRIVES; ++index) {
  664. ide_drive_t *drive = &hwif->drives[index];
  665. if (!drive->present)
  666. continue;
  667. if (!hwgroup->drive)
  668. hwgroup->drive = drive;
  669. drive->next = hwgroup->drive->next;
  670. hwgroup->drive->next = drive;
  671. ide_init_queue(drive);
  672. }
  673. if (!hwgroup->hwif) {
  674. hwgroup->hwif = HWIF(hwgroup->drive);
  675. #ifdef DEBUG
  676. printk("%s : Adding missed hwif to hwgroup!!n", hwif->name);
  677. #endif
  678. }
  679. restore_flags(flags); /* all CPUs; safe now that hwif->hwgroup is set up */
  680. #if !defined(__mc68000__) && !defined(CONFIG_APUS) && !defined(__sparc__)
  681. printk("%s at 0x%03x-0x%03x,0x%03x on irq %d", hwif->name,
  682. hwif->io_ports[IDE_DATA_OFFSET],
  683. hwif->io_ports[IDE_DATA_OFFSET]+7,
  684. hwif->io_ports[IDE_CONTROL_OFFSET], hwif->irq);
  685. #elif defined(__sparc__)
  686. printk("%s at 0x%03lx-0x%03lx,0x%03lx on irq %s", hwif->name,
  687. hwif->io_ports[IDE_DATA_OFFSET],
  688. hwif->io_ports[IDE_DATA_OFFSET]+7,
  689. hwif->io_ports[IDE_CONTROL_OFFSET], __irq_itoa(hwif->irq));
  690. #else
  691. printk("%s at %p on irq 0x%08x", hwif->name,
  692. hwif->io_ports[IDE_DATA_OFFSET], hwif->irq);
  693. #endif /* __mc68000__ && CONFIG_APUS */
  694. if (match)
  695. printk(" (%sed with %s)",
  696. hwif->sharing_irq ? "shar" : "serializ", match->name);
  697. printk("n");
  698. return 0;
  699. }
  700. /*
  701.  * init_gendisk() (as opposed to ide_geninit) is called for each major device,
  702.  * after probing for drives, to allocate partition tables and other data
  703.  * structures needed for the routines in genhd.c.  ide_geninit() gets called
  704.  * somewhat later, during the partition check.
  705.  */
  706. static void init_gendisk (ide_hwif_t *hwif)
  707. {
  708. struct gendisk *gd;
  709. unsigned int unit, units, minors;
  710. int *bs, *max_sect, *max_ra;
  711. extern devfs_handle_t ide_devfs_handle;
  712. #if 1
  713. units = MAX_DRIVES;
  714. #else
  715. /* figure out maximum drive number on the interface */
  716. for (units = MAX_DRIVES; units > 0; --units) {
  717. if (hwif->drives[units-1].present)
  718. break;
  719. }
  720. #endif
  721. minors    = units * (1<<PARTN_BITS);
  722. gd        = kmalloc (sizeof(struct gendisk), GFP_KERNEL);
  723. if (!gd)
  724. goto err_kmalloc_gd;
  725. memset (gd, 0, sizeof(struct gendisk));
  726. gd->sizes = kmalloc (minors * sizeof(int), GFP_KERNEL);
  727. if (!gd->sizes)
  728. goto err_kmalloc_gd_sizes;
  729. gd->part  = kmalloc (minors * sizeof(struct hd_struct), GFP_KERNEL);
  730. if (!gd->part)
  731. goto err_kmalloc_gd_part;
  732. bs        = kmalloc (minors*sizeof(int), GFP_KERNEL);
  733. if (!bs)
  734. goto err_kmalloc_bs;
  735. max_sect  = kmalloc (minors*sizeof(int), GFP_KERNEL);
  736. if (!max_sect)
  737. goto err_kmalloc_max_sect;
  738. max_ra    = kmalloc (minors*sizeof(int), GFP_KERNEL);
  739. if (!max_ra)
  740. goto err_kmalloc_max_ra;
  741. memset(gd->part, 0, minors * sizeof(struct hd_struct));
  742. /* cdroms and msdos f/s are examples of non-1024 blocksizes */
  743. blksize_size[hwif->major] = bs;
  744. max_sectors[hwif->major] = max_sect;
  745. max_readahead[hwif->major] = max_ra;
  746. for (unit = 0; unit < minors; ++unit) {
  747. *bs++ = BLOCK_SIZE;
  748. /*
  749.  * IDE can do up to 128K per request == 256
  750.  */
  751. *max_sect++ = ((hwif->chipset == ide_pdc4030) ? 127 : 128);
  752. *max_ra++ = vm_max_readahead;
  753. }
  754. for (unit = 0; unit < units; ++unit)
  755. hwif->drives[unit].part = &gd->part[unit << PARTN_BITS];
  756. gd->major = hwif->major; /* our major device number */
  757. gd->major_name = IDE_MAJOR_NAME; /* treated special in genhd.c */
  758. gd->minor_shift = PARTN_BITS; /* num bits for partitions */
  759. gd->max_p = 1<<PARTN_BITS; /* 1 + max partitions / drive */
  760. gd->nr_real = units; /* current num real drives */
  761. gd->real_devices= hwif; /* ptr to internal data */
  762. gd->next = NULL; /* linked list of major devs */
  763. gd->fops        = ide_fops;             /* file operations */
  764. gd->de_arr = kmalloc (sizeof *gd->de_arr * units, GFP_KERNEL);
  765. gd->flags = kmalloc (sizeof *gd->flags * units, GFP_KERNEL);
  766. if (gd->de_arr)
  767. memset (gd->de_arr, 0, sizeof *gd->de_arr * units);
  768. if (gd->flags)
  769. memset (gd->flags, 0, sizeof *gd->flags * units);
  770. hwif->gd = gd;
  771. add_gendisk(gd);
  772. for (unit = 0; unit < units; ++unit) {
  773. #if 1
  774. char name[64];
  775. ide_add_generic_settings(hwif->drives + unit);
  776. hwif->drives[unit].dn = ((hwif->channel ? 2 : 0) + unit);
  777. sprintf (name, "host%d/bus%d/target%d/lun%d",
  778. (hwif->channel && hwif->mate) ?
  779. hwif->mate->index : hwif->index,
  780. hwif->channel, unit, hwif->drives[unit].lun);
  781. if (hwif->drives[unit].present)
  782. hwif->drives[unit].de = devfs_mk_dir(ide_devfs_handle, name, NULL);
  783. #else
  784. if (hwif->drives[unit].present) {
  785. char name[64];
  786. ide_add_generic_settings(hwif->drives + unit);
  787. hwif->drives[unit].dn = ((hwif->channel ? 2 : 0) + unit);
  788. sprintf (name, "host%d/bus%d/target%d/lun%d",
  789.  (hwif->channel && hwif->mate) ? hwif->mate->index : hwif->index,
  790.  hwif->channel, unit, hwif->drives[unit].lun);
  791. hwif->drives[unit].de =
  792. devfs_mk_dir (ide_devfs_handle, name, NULL);
  793. }
  794. #endif
  795. }
  796. return;
  797. err_kmalloc_max_ra:
  798. kfree(max_sect);
  799. err_kmalloc_max_sect:
  800. kfree(bs);
  801. err_kmalloc_bs:
  802. kfree(gd->part);
  803. err_kmalloc_gd_part:
  804. kfree(gd->sizes);
  805. err_kmalloc_gd_sizes:
  806. kfree(gd);
  807. err_kmalloc_gd:
  808. printk(KERN_WARNING "(ide::init_gendisk) Out of memoryn");
  809. return;
  810. }
  811. static int hwif_init (ide_hwif_t *hwif)
  812. {
  813. if (!hwif->present)
  814. return 0;
  815. if (!hwif->irq) {
  816. if (!(hwif->irq = ide_default_irq(hwif->io_ports[IDE_DATA_OFFSET])))
  817. {
  818. printk("%s: DISABLED, NO IRQn", hwif->name);
  819. return (hwif->present = 0);
  820. }
  821. }
  822. #ifdef CONFIG_BLK_DEV_HD
  823. if (hwif->irq == HD_IRQ && hwif->io_ports[IDE_DATA_OFFSET] != HD_DATA) {
  824. printk("%s: CANNOT SHARE IRQ WITH OLD HARDDISK DRIVER (hd.c)n", hwif->name);
  825. return (hwif->present = 0);
  826. }
  827. #endif /* CONFIG_BLK_DEV_HD */
  828. hwif->present = 0; /* we set it back to 1 if all is ok below */
  829. if (devfs_register_blkdev (hwif->major, hwif->name, ide_fops)) {
  830. printk("%s: UNABLE TO GET MAJOR NUMBER %dn", hwif->name, hwif->major);
  831. return (hwif->present = 0);
  832. }
  833. if (init_irq(hwif)) {
  834. int i = hwif->irq;
  835. /*
  836.  * It failed to initialise. Find the default IRQ for 
  837.  * this port and try that.
  838.  */
  839. if (!(hwif->irq = ide_default_irq(hwif->io_ports[IDE_DATA_OFFSET]))) {
  840. printk("%s: Disabled unable to get IRQ %d.n", hwif->name, i);
  841. (void) unregister_blkdev (hwif->major, hwif->name);
  842. return (hwif->present = 0);
  843. }
  844. if (init_irq(hwif)) {
  845. printk("%s: probed IRQ %d and default IRQ %d failed.n",
  846. hwif->name, i, hwif->irq);
  847. (void) unregister_blkdev (hwif->major, hwif->name);
  848. return (hwif->present = 0);
  849. }
  850. printk("%s: probed IRQ %d failed, using default.n",
  851. hwif->name, hwif->irq);
  852. }
  853. init_gendisk(hwif);
  854. blk_dev[hwif->major].data = hwif;
  855. blk_dev[hwif->major].queue = ide_get_queue;
  856. read_ahead[hwif->major] = 8; /* (4kB) */
  857. hwif->present = 1; /* success */
  858. #if (DEBUG_SPINLOCK > 0)
  859. {
  860. static int done = 0;
  861. if (!done++)
  862. printk("io_request_lock is %pn", &io_request_lock);    /* FIXME */
  863. }
  864. #endif
  865. return hwif->present;
  866. }
  867. void export_ide_init_queue (ide_drive_t *drive)
  868. {
  869. ide_init_queue(drive);
  870. }
  871. byte export_probe_for_drive (ide_drive_t *drive)
  872. {
  873. return probe_for_drive(drive);
  874. }
  875. EXPORT_SYMBOL(export_ide_init_queue);
  876. EXPORT_SYMBOL(export_probe_for_drive);
  877. int ideprobe_init (void);
  878. static ide_module_t ideprobe_module = {
  879. IDE_PROBE_MODULE,
  880. ideprobe_init,
  881. NULL
  882. };
  883. int ideprobe_init (void)
  884. {
  885. unsigned int index;
  886. int probe[MAX_HWIFS];
  887. MOD_INC_USE_COUNT;
  888. memset(probe, 0, MAX_HWIFS * sizeof(int));
  889. for (index = 0; index < MAX_HWIFS; ++index)
  890. probe[index] = !ide_hwifs[index].present;
  891. /*
  892.  * Probe for drives in the usual way.. CMOS/BIOS, then poke at ports
  893.  */
  894. for (index = 0; index < MAX_HWIFS; ++index)
  895. if (probe[index])
  896. probe_hwif(&ide_hwifs[index]);
  897. for (index = 0; index < MAX_HWIFS; ++index)
  898. if (probe[index])
  899. hwif_init(&ide_hwifs[index]);
  900. if (!ide_probe)
  901. ide_probe = &ideprobe_module;
  902. MOD_DEC_USE_COUNT;
  903. return 0;
  904. }
  905. #ifdef MODULE
  906. extern int (*ide_xlate_1024_hook)(kdev_t, int, int, const char *);
  907. int init_module (void)
  908. {
  909. unsigned int index;
  910. for (index = 0; index < MAX_HWIFS; ++index)
  911. ide_unregister(index);
  912. ideprobe_init();
  913. create_proc_ide_interfaces();
  914. ide_xlate_1024_hook = ide_xlate_1024;
  915. return 0;
  916. }
  917. void cleanup_module (void)
  918. {
  919. ide_probe = NULL;
  920. ide_xlate_1024_hook = 0;
  921. }
  922. MODULE_LICENSE("GPL");
  923. #endif /* MODULE */