ide-probe.c 42 KB

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