ide-probe.c 43 KB

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