ide-probe.c 38 KB

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