ide-probe.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573
  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. static void ide_classify_ata_dev(ide_drive_t *drive)
  90. {
  91. u16 *id = drive->id;
  92. char *m = (char *)&id[ATA_ID_PROD];
  93. int is_cfa = ata_id_is_cfa(id);
  94. /* CF devices are *not* removable in Linux definition of the term */
  95. if (is_cfa == 0 && (id[ATA_ID_CONFIG] & (1 << 7)))
  96. drive->dev_flags |= IDE_DFLAG_REMOVABLE;
  97. drive->media = ide_disk;
  98. if (!ata_id_has_unload(drive->id))
  99. drive->dev_flags |= IDE_DFLAG_NO_UNLOAD;
  100. printk(KERN_INFO "%s: %s, %s DISK drive\n", drive->name, m,
  101. is_cfa ? "CFA" : "ATA");
  102. }
  103. static void ide_classify_atapi_dev(ide_drive_t *drive)
  104. {
  105. u16 *id = drive->id;
  106. char *m = (char *)&id[ATA_ID_PROD];
  107. u8 type = (id[ATA_ID_CONFIG] >> 8) & 0x1f;
  108. printk(KERN_INFO "%s: %s, ATAPI ", drive->name, m);
  109. switch (type) {
  110. case ide_floppy:
  111. if (!strstr(m, "CD-ROM")) {
  112. if (!strstr(m, "oppy") &&
  113. !strstr(m, "poyp") &&
  114. !strstr(m, "ZIP"))
  115. printk(KERN_CONT "cdrom or floppy?, assuming ");
  116. if (drive->media != ide_cdrom) {
  117. printk(KERN_CONT "FLOPPY");
  118. drive->dev_flags |= IDE_DFLAG_REMOVABLE;
  119. break;
  120. }
  121. }
  122. /* Early cdrom models used zero */
  123. type = ide_cdrom;
  124. case ide_cdrom:
  125. drive->dev_flags |= IDE_DFLAG_REMOVABLE;
  126. #ifdef CONFIG_PPC
  127. /* kludge for Apple PowerBook internal zip */
  128. if (!strstr(m, "CD-ROM") && strstr(m, "ZIP")) {
  129. printk(KERN_CONT "FLOPPY");
  130. type = ide_floppy;
  131. break;
  132. }
  133. #endif
  134. printk(KERN_CONT "CD/DVD-ROM");
  135. break;
  136. case ide_tape:
  137. printk(KERN_CONT "TAPE");
  138. break;
  139. case ide_optical:
  140. printk(KERN_CONT "OPTICAL");
  141. drive->dev_flags |= IDE_DFLAG_REMOVABLE;
  142. break;
  143. default:
  144. printk(KERN_CONT "UNKNOWN (type %d)", type);
  145. break;
  146. }
  147. printk(KERN_CONT " drive\n");
  148. drive->media = type;
  149. /* an ATAPI device ignores DRDY */
  150. drive->ready_stat = 0;
  151. if (ata_id_cdb_intr(id))
  152. drive->atapi_flags |= IDE_AFLAG_DRQ_INTERRUPT;
  153. drive->dev_flags |= IDE_DFLAG_DOORLOCKING;
  154. /* we don't do head unloading on ATAPI devices */
  155. drive->dev_flags |= IDE_DFLAG_NO_UNLOAD;
  156. }
  157. /**
  158. * do_identify - identify a drive
  159. * @drive: drive to identify
  160. * @cmd: command used
  161. * @id: buffer for IDENTIFY data
  162. *
  163. * Called when we have issued a drive identify command to
  164. * read and parse the results. This function is run with
  165. * interrupts disabled.
  166. */
  167. static void do_identify(ide_drive_t *drive, u8 cmd, u16 *id)
  168. {
  169. ide_hwif_t *hwif = drive->hwif;
  170. char *m = (char *)&id[ATA_ID_PROD];
  171. unsigned long flags;
  172. int bswap = 1;
  173. /* local CPU only; some systems need this */
  174. local_irq_save(flags);
  175. /* read 512 bytes of id info */
  176. hwif->tp_ops->input_data(drive, NULL, id, SECTOR_SIZE);
  177. local_irq_restore(flags);
  178. drive->dev_flags |= IDE_DFLAG_ID_READ;
  179. #ifdef DEBUG
  180. printk(KERN_INFO "%s: dumping identify data\n", drive->name);
  181. ide_dump_identify((u8 *)id);
  182. #endif
  183. ide_fix_driveid(id);
  184. /*
  185. * ATA_CMD_ID_ATA returns little-endian info,
  186. * ATA_CMD_ID_ATAPI *usually* returns little-endian info.
  187. */
  188. if (cmd == ATA_CMD_ID_ATAPI) {
  189. if ((m[0] == 'N' && m[1] == 'E') || /* NEC */
  190. (m[0] == 'F' && m[1] == 'X') || /* Mitsumi */
  191. (m[0] == 'P' && m[1] == 'i')) /* Pioneer */
  192. /* Vertos drives may still be weird */
  193. bswap ^= 1;
  194. }
  195. ide_fixstring(m, ATA_ID_PROD_LEN, bswap);
  196. ide_fixstring((char *)&id[ATA_ID_FW_REV], ATA_ID_FW_REV_LEN, bswap);
  197. ide_fixstring((char *)&id[ATA_ID_SERNO], ATA_ID_SERNO_LEN, bswap);
  198. /* we depend on this a lot! */
  199. m[ATA_ID_PROD_LEN - 1] = '\0';
  200. if (strstr(m, "E X A B Y T E N E S T"))
  201. drive->dev_flags &= ~IDE_DFLAG_PRESENT;
  202. else
  203. drive->dev_flags |= IDE_DFLAG_PRESENT;
  204. }
  205. /**
  206. * ide_dev_read_id - send ATA/ATAPI IDENTIFY command
  207. * @drive: drive to identify
  208. * @cmd: command to use
  209. * @id: buffer for IDENTIFY data
  210. *
  211. * Sends an ATA(PI) IDENTIFY request to a drive and waits for a response.
  212. *
  213. * Returns: 0 device was identified
  214. * 1 device timed-out (no response to identify request)
  215. * 2 device aborted the command (refused to identify itself)
  216. */
  217. int ide_dev_read_id(ide_drive_t *drive, u8 cmd, u16 *id)
  218. {
  219. ide_hwif_t *hwif = drive->hwif;
  220. struct ide_io_ports *io_ports = &hwif->io_ports;
  221. const struct ide_tp_ops *tp_ops = hwif->tp_ops;
  222. int use_altstatus = 0, rc;
  223. unsigned long timeout;
  224. u8 s = 0, a = 0;
  225. /*
  226. * Disable device IRQ. Otherwise we'll get spurious interrupts
  227. * during the identify phase that the IRQ handler isn't expecting.
  228. */
  229. if (io_ports->ctl_addr)
  230. tp_ops->write_devctl(hwif, ATA_NIEN | ATA_DEVCTL_OBS);
  231. /* take a deep breath */
  232. msleep(50);
  233. if (io_ports->ctl_addr &&
  234. (hwif->host_flags & IDE_HFLAG_BROKEN_ALTSTATUS) == 0) {
  235. a = tp_ops->read_altstatus(hwif);
  236. s = tp_ops->read_status(hwif);
  237. if ((a ^ s) & ~ATA_IDX)
  238. /* ancient Seagate drives, broken interfaces */
  239. printk(KERN_INFO "%s: probing with STATUS(0x%02x) "
  240. "instead of ALTSTATUS(0x%02x)\n",
  241. drive->name, s, a);
  242. else
  243. /* use non-intrusive polling */
  244. use_altstatus = 1;
  245. }
  246. /* set features register for atapi
  247. * identify command to be sure of reply
  248. */
  249. if (cmd == ATA_CMD_ID_ATAPI) {
  250. struct ide_taskfile tf;
  251. memset(&tf, 0, sizeof(tf));
  252. /* disable DMA & overlap */
  253. tp_ops->tf_load(drive, &tf, IDE_VALID_FEATURE);
  254. }
  255. /* ask drive for ID */
  256. tp_ops->exec_command(hwif, cmd);
  257. timeout = ((cmd == ATA_CMD_ID_ATA) ? WAIT_WORSTCASE : WAIT_PIDENTIFY) / 2;
  258. if (ide_busy_sleep(hwif, timeout, use_altstatus))
  259. return 1;
  260. /* wait for IRQ and ATA_DRQ */
  261. msleep(50);
  262. s = tp_ops->read_status(hwif);
  263. if (OK_STAT(s, ATA_DRQ, BAD_R_STAT)) {
  264. /* drive returned ID */
  265. do_identify(drive, cmd, id);
  266. /* drive responded with ID */
  267. rc = 0;
  268. /* clear drive IRQ */
  269. (void)tp_ops->read_status(hwif);
  270. } else {
  271. /* drive refused ID */
  272. rc = 2;
  273. }
  274. return rc;
  275. }
  276. int ide_busy_sleep(ide_hwif_t *hwif, unsigned long timeout, int altstatus)
  277. {
  278. u8 stat;
  279. timeout += jiffies;
  280. do {
  281. msleep(50); /* give drive a breather */
  282. stat = altstatus ? hwif->tp_ops->read_altstatus(hwif)
  283. : hwif->tp_ops->read_status(hwif);
  284. if ((stat & ATA_BUSY) == 0)
  285. return 0;
  286. } while (time_before(jiffies, timeout));
  287. return 1; /* drive timed-out */
  288. }
  289. static u8 ide_read_device(ide_drive_t *drive)
  290. {
  291. struct ide_taskfile tf;
  292. drive->hwif->tp_ops->tf_read(drive, &tf, IDE_VALID_DEVICE);
  293. return tf.device;
  294. }
  295. /**
  296. * do_probe - probe an IDE device
  297. * @drive: drive to probe
  298. * @cmd: command to use
  299. *
  300. * do_probe() has the difficult job of finding a drive if it exists,
  301. * without getting hung up if it doesn't exist, without trampling on
  302. * ethernet cards, and without leaving any IRQs dangling to haunt us later.
  303. *
  304. * If a drive is "known" to exist (from CMOS or kernel parameters),
  305. * but does not respond right away, the probe will "hang in there"
  306. * for the maximum wait time (about 30 seconds), otherwise it will
  307. * exit much more quickly.
  308. *
  309. * Returns: 0 device was identified
  310. * 1 device timed-out (no response to identify request)
  311. * 2 device aborted the command (refused to identify itself)
  312. * 3 bad status from device (possible for ATAPI drives)
  313. * 4 probe was not attempted because failure was obvious
  314. */
  315. static int do_probe (ide_drive_t *drive, u8 cmd)
  316. {
  317. ide_hwif_t *hwif = drive->hwif;
  318. const struct ide_tp_ops *tp_ops = hwif->tp_ops;
  319. u16 *id = drive->id;
  320. int rc;
  321. u8 present = !!(drive->dev_flags & IDE_DFLAG_PRESENT), stat;
  322. /* avoid waiting for inappropriate probes */
  323. if (present && drive->media != ide_disk && cmd == ATA_CMD_ID_ATA)
  324. return 4;
  325. #ifdef DEBUG
  326. printk(KERN_INFO "probing for %s: present=%d, media=%d, probetype=%s\n",
  327. drive->name, present, drive->media,
  328. (cmd == ATA_CMD_ID_ATA) ? "ATA" : "ATAPI");
  329. #endif
  330. /* needed for some systems
  331. * (e.g. crw9624 as drive0 with disk as slave)
  332. */
  333. msleep(50);
  334. tp_ops->dev_select(drive);
  335. msleep(50);
  336. if (ide_read_device(drive) != drive->select && present == 0) {
  337. if (drive->dn & 1) {
  338. /* exit with drive0 selected */
  339. tp_ops->dev_select(hwif->devices[0]);
  340. /* allow ATA_BUSY to assert & clear */
  341. msleep(50);
  342. }
  343. /* no i/f present: mmm.. this should be a 4 -ml */
  344. return 3;
  345. }
  346. stat = tp_ops->read_status(hwif);
  347. if (OK_STAT(stat, ATA_DRDY, ATA_BUSY) ||
  348. present || cmd == ATA_CMD_ID_ATAPI) {
  349. rc = ide_dev_read_id(drive, cmd, id);
  350. if (rc)
  351. /* failed: try again */
  352. rc = ide_dev_read_id(drive, cmd, id);
  353. stat = tp_ops->read_status(hwif);
  354. if (stat == (ATA_BUSY | ATA_DRDY))
  355. return 4;
  356. if (rc == 1 && cmd == ATA_CMD_ID_ATAPI) {
  357. printk(KERN_ERR "%s: no response (status = 0x%02x), "
  358. "resetting drive\n", drive->name, stat);
  359. msleep(50);
  360. tp_ops->dev_select(drive);
  361. msleep(50);
  362. tp_ops->exec_command(hwif, ATA_CMD_DEV_RESET);
  363. (void)ide_busy_sleep(hwif, WAIT_WORSTCASE, 0);
  364. rc = ide_dev_read_id(drive, cmd, id);
  365. }
  366. /* ensure drive IRQ is clear */
  367. stat = tp_ops->read_status(hwif);
  368. if (rc == 1)
  369. printk(KERN_ERR "%s: no response (status = 0x%02x)\n",
  370. drive->name, stat);
  371. } else {
  372. /* not present or maybe ATAPI */
  373. rc = 3;
  374. }
  375. if (drive->dn & 1) {
  376. /* exit with drive0 selected */
  377. tp_ops->dev_select(hwif->devices[0]);
  378. msleep(50);
  379. /* ensure drive irq is clear */
  380. (void)tp_ops->read_status(hwif);
  381. }
  382. return rc;
  383. }
  384. /**
  385. * probe_for_drives - upper level drive probe
  386. * @drive: drive to probe for
  387. *
  388. * probe_for_drive() tests for existence of a given drive using do_probe()
  389. * and presents things to the user as needed.
  390. *
  391. * Returns: 0 no device was found
  392. * 1 device was found
  393. * (note: IDE_DFLAG_PRESENT might still be not set)
  394. */
  395. static u8 probe_for_drive(ide_drive_t *drive)
  396. {
  397. char *m;
  398. int rc;
  399. u8 cmd;
  400. /*
  401. * In order to keep things simple we have an id
  402. * block for all drives at all times. If the device
  403. * is pre ATA or refuses ATA/ATAPI identify we
  404. * will add faked data to this.
  405. *
  406. * Also note that 0 everywhere means "can't do X"
  407. */
  408. drive->dev_flags &= ~IDE_DFLAG_ID_READ;
  409. drive->id = kzalloc(SECTOR_SIZE, GFP_KERNEL);
  410. if (drive->id == NULL) {
  411. printk(KERN_ERR "ide: out of memory for id data.\n");
  412. return 0;
  413. }
  414. m = (char *)&drive->id[ATA_ID_PROD];
  415. strcpy(m, "UNKNOWN");
  416. /* skip probing? */
  417. if ((drive->dev_flags & IDE_DFLAG_NOPROBE) == 0) {
  418. /* if !(success||timed-out) */
  419. cmd = ATA_CMD_ID_ATA;
  420. rc = do_probe(drive, cmd);
  421. if (rc >= 2) {
  422. /* look for ATAPI device */
  423. cmd = ATA_CMD_ID_ATAPI;
  424. rc = do_probe(drive, cmd);
  425. }
  426. if ((drive->dev_flags & IDE_DFLAG_PRESENT) == 0)
  427. goto out_free;
  428. /* identification failed? */
  429. if ((drive->dev_flags & IDE_DFLAG_ID_READ) == 0) {
  430. if (drive->media == ide_disk) {
  431. printk(KERN_INFO "%s: non-IDE drive, CHS=%d/%d/%d\n",
  432. drive->name, drive->cyl,
  433. drive->head, drive->sect);
  434. } else if (drive->media == ide_cdrom) {
  435. printk(KERN_INFO "%s: ATAPI cdrom (?)\n", drive->name);
  436. } else {
  437. /* nuke it */
  438. printk(KERN_WARNING "%s: Unknown device on bus refused identification. Ignoring.\n", drive->name);
  439. drive->dev_flags &= ~IDE_DFLAG_PRESENT;
  440. }
  441. } else {
  442. if (cmd == ATA_CMD_ID_ATAPI)
  443. ide_classify_atapi_dev(drive);
  444. else
  445. ide_classify_ata_dev(drive);
  446. }
  447. }
  448. if ((drive->dev_flags & IDE_DFLAG_PRESENT) == 0)
  449. goto out_free;
  450. /* The drive wasn't being helpful. Add generic info only */
  451. if ((drive->dev_flags & IDE_DFLAG_ID_READ) == 0) {
  452. generic_id(drive);
  453. return 1;
  454. }
  455. if (drive->media == ide_disk) {
  456. ide_disk_init_chs(drive);
  457. ide_disk_init_mult_count(drive);
  458. }
  459. return 1;
  460. out_free:
  461. kfree(drive->id);
  462. return 0;
  463. }
  464. static void hwif_release_dev(struct device *dev)
  465. {
  466. ide_hwif_t *hwif = container_of(dev, ide_hwif_t, gendev);
  467. complete(&hwif->gendev_rel_comp);
  468. }
  469. static int ide_register_port(ide_hwif_t *hwif)
  470. {
  471. int ret;
  472. /* register with global device tree */
  473. dev_set_name(&hwif->gendev, hwif->name);
  474. hwif->gendev.driver_data = hwif;
  475. if (hwif->gendev.parent == NULL)
  476. hwif->gendev.parent = hwif->dev;
  477. hwif->gendev.release = hwif_release_dev;
  478. ret = device_register(&hwif->gendev);
  479. if (ret < 0) {
  480. printk(KERN_WARNING "IDE: %s: device_register error: %d\n",
  481. __func__, ret);
  482. goto out;
  483. }
  484. hwif->portdev = device_create(ide_port_class, &hwif->gendev,
  485. MKDEV(0, 0), hwif, hwif->name);
  486. if (IS_ERR(hwif->portdev)) {
  487. ret = PTR_ERR(hwif->portdev);
  488. device_unregister(&hwif->gendev);
  489. }
  490. out:
  491. return ret;
  492. }
  493. /**
  494. * ide_port_wait_ready - wait for port to become ready
  495. * @hwif: IDE port
  496. *
  497. * This is needed on some PPCs and a bunch of BIOS-less embedded
  498. * platforms. Typical cases are:
  499. *
  500. * - The firmware hard reset the disk before booting the kernel,
  501. * the drive is still doing it's poweron-reset sequence, that
  502. * can take up to 30 seconds.
  503. *
  504. * - The firmware does nothing (or no firmware), the device is
  505. * still in POST state (same as above actually).
  506. *
  507. * - Some CD/DVD/Writer combo drives tend to drive the bus during
  508. * their reset sequence even when they are non-selected slave
  509. * devices, thus preventing discovery of the main HD.
  510. *
  511. * Doing this wait-for-non-busy should not harm any existing
  512. * configuration and fix some issues like the above.
  513. *
  514. * BenH.
  515. *
  516. * Returns 0 on success, error code (< 0) otherwise.
  517. */
  518. static int ide_port_wait_ready(ide_hwif_t *hwif)
  519. {
  520. const struct ide_tp_ops *tp_ops = hwif->tp_ops;
  521. ide_drive_t *drive;
  522. int i, rc;
  523. printk(KERN_DEBUG "Probing IDE interface %s...\n", hwif->name);
  524. /* Let HW settle down a bit from whatever init state we
  525. * come from */
  526. mdelay(2);
  527. /* Wait for BSY bit to go away, spec timeout is 30 seconds,
  528. * I know of at least one disk who takes 31 seconds, I use 35
  529. * here to be safe
  530. */
  531. rc = ide_wait_not_busy(hwif, 35000);
  532. if (rc)
  533. return rc;
  534. /* Now make sure both master & slave are ready */
  535. ide_port_for_each_dev(i, drive, hwif) {
  536. /* Ignore disks that we will not probe for later. */
  537. if ((drive->dev_flags & IDE_DFLAG_NOPROBE) == 0 ||
  538. (drive->dev_flags & IDE_DFLAG_PRESENT)) {
  539. tp_ops->dev_select(drive);
  540. tp_ops->write_devctl(hwif, ATA_DEVCTL_OBS);
  541. mdelay(2);
  542. rc = ide_wait_not_busy(hwif, 35000);
  543. if (rc)
  544. goto out;
  545. } else
  546. printk(KERN_DEBUG "%s: ide_wait_not_busy() skipped\n",
  547. drive->name);
  548. }
  549. out:
  550. /* Exit function with master reselected (let's be sane) */
  551. if (i)
  552. tp_ops->dev_select(hwif->devices[0]);
  553. return rc;
  554. }
  555. /**
  556. * ide_undecoded_slave - look for bad CF adapters
  557. * @dev1: slave device
  558. *
  559. * Analyse the drives on the interface and attempt to decide if we
  560. * have the same drive viewed twice. This occurs with crap CF adapters
  561. * and PCMCIA sometimes.
  562. */
  563. void ide_undecoded_slave(ide_drive_t *dev1)
  564. {
  565. ide_drive_t *dev0 = dev1->hwif->devices[0];
  566. if ((dev1->dn & 1) == 0 || (dev0->dev_flags & IDE_DFLAG_PRESENT) == 0)
  567. return;
  568. /* If the models don't match they are not the same product */
  569. if (strcmp((char *)&dev0->id[ATA_ID_PROD],
  570. (char *)&dev1->id[ATA_ID_PROD]))
  571. return;
  572. /* Serial numbers do not match */
  573. if (strncmp((char *)&dev0->id[ATA_ID_SERNO],
  574. (char *)&dev1->id[ATA_ID_SERNO], ATA_ID_SERNO_LEN))
  575. return;
  576. /* No serial number, thankfully very rare for CF */
  577. if (*(char *)&dev0->id[ATA_ID_SERNO] == 0)
  578. return;
  579. /* Appears to be an IDE flash adapter with decode bugs */
  580. printk(KERN_WARNING "ide-probe: ignoring undecoded slave\n");
  581. dev1->dev_flags &= ~IDE_DFLAG_PRESENT;
  582. }
  583. EXPORT_SYMBOL_GPL(ide_undecoded_slave);
  584. static int ide_probe_port(ide_hwif_t *hwif)
  585. {
  586. ide_drive_t *drive;
  587. unsigned int irqd;
  588. int i, rc = -ENODEV;
  589. BUG_ON(hwif->present);
  590. if ((hwif->devices[0]->dev_flags & IDE_DFLAG_NOPROBE) &&
  591. (hwif->devices[1]->dev_flags & IDE_DFLAG_NOPROBE))
  592. return -EACCES;
  593. /*
  594. * We must always disable IRQ, as probe_for_drive will assert IRQ, but
  595. * we'll install our IRQ driver much later...
  596. */
  597. irqd = hwif->irq;
  598. if (irqd)
  599. disable_irq(hwif->irq);
  600. if (ide_port_wait_ready(hwif) == -EBUSY)
  601. printk(KERN_DEBUG "%s: Wait for ready failed before probe !\n", hwif->name);
  602. /*
  603. * Second drive should only exist if first drive was found,
  604. * but a lot of cdrom drives are configured as single slaves.
  605. */
  606. ide_port_for_each_dev(i, drive, hwif) {
  607. (void) probe_for_drive(drive);
  608. if (drive->dev_flags & IDE_DFLAG_PRESENT)
  609. rc = 0;
  610. }
  611. /*
  612. * Use cached IRQ number. It might be (and is...) changed by probe
  613. * code above
  614. */
  615. if (irqd)
  616. enable_irq(irqd);
  617. return rc;
  618. }
  619. static void ide_port_tune_devices(ide_hwif_t *hwif)
  620. {
  621. const struct ide_port_ops *port_ops = hwif->port_ops;
  622. ide_drive_t *drive;
  623. int i;
  624. ide_port_for_each_present_dev(i, drive, hwif) {
  625. if (port_ops && port_ops->quirkproc)
  626. port_ops->quirkproc(drive);
  627. }
  628. ide_port_for_each_present_dev(i, drive, hwif) {
  629. ide_set_max_pio(drive);
  630. drive->dev_flags |= IDE_DFLAG_NICE1;
  631. if (hwif->dma_ops)
  632. ide_set_dma(drive);
  633. }
  634. }
  635. /*
  636. * init request queue
  637. */
  638. static int ide_init_queue(ide_drive_t *drive)
  639. {
  640. struct request_queue *q;
  641. ide_hwif_t *hwif = drive->hwif;
  642. int max_sectors = 256;
  643. int max_sg_entries = PRD_ENTRIES;
  644. /*
  645. * Our default set up assumes the normal IDE case,
  646. * that is 64K segmenting, standard PRD setup
  647. * and LBA28. Some drivers then impose their own
  648. * limits and LBA48 we could raise it but as yet
  649. * do not.
  650. */
  651. q = blk_init_queue_node(do_ide_request, NULL, hwif_to_node(hwif));
  652. if (!q)
  653. return 1;
  654. q->queuedata = drive;
  655. blk_queue_segment_boundary(q, 0xffff);
  656. if (hwif->rqsize < max_sectors)
  657. max_sectors = hwif->rqsize;
  658. blk_queue_max_sectors(q, max_sectors);
  659. #ifdef CONFIG_PCI
  660. /* When we have an IOMMU, we may have a problem where pci_map_sg()
  661. * creates segments that don't completely match our boundary
  662. * requirements and thus need to be broken up again. Because it
  663. * doesn't align properly either, we may actually have to break up
  664. * to more segments than what was we got in the first place, a max
  665. * worst case is twice as many.
  666. * This will be fixed once we teach pci_map_sg() about our boundary
  667. * requirements, hopefully soon. *FIXME*
  668. */
  669. if (!PCI_DMA_BUS_IS_PHYS)
  670. max_sg_entries >>= 1;
  671. #endif /* CONFIG_PCI */
  672. blk_queue_max_hw_segments(q, max_sg_entries);
  673. blk_queue_max_phys_segments(q, max_sg_entries);
  674. /* assign drive queue */
  675. drive->queue = q;
  676. /* needs drive->queue to be set */
  677. ide_toggle_bounce(drive, 1);
  678. return 0;
  679. }
  680. static DEFINE_MUTEX(ide_cfg_mtx);
  681. /*
  682. * For any present drive:
  683. * - allocate the block device queue
  684. */
  685. static int ide_port_setup_devices(ide_hwif_t *hwif)
  686. {
  687. ide_drive_t *drive;
  688. int i, j = 0;
  689. mutex_lock(&ide_cfg_mtx);
  690. ide_port_for_each_present_dev(i, drive, hwif) {
  691. if (ide_init_queue(drive)) {
  692. printk(KERN_ERR "ide: failed to init %s\n",
  693. drive->name);
  694. kfree(drive->id);
  695. drive->id = NULL;
  696. drive->dev_flags &= ~IDE_DFLAG_PRESENT;
  697. continue;
  698. }
  699. j++;
  700. }
  701. mutex_unlock(&ide_cfg_mtx);
  702. return j;
  703. }
  704. /*
  705. * This routine sets up the IRQ for an IDE interface.
  706. */
  707. static int init_irq (ide_hwif_t *hwif)
  708. {
  709. struct ide_io_ports *io_ports = &hwif->io_ports;
  710. struct ide_host *host = hwif->host;
  711. irq_handler_t irq_handler = host->irq_handler;
  712. int sa = host->irq_flags;
  713. if (irq_handler == NULL)
  714. irq_handler = ide_intr;
  715. if (io_ports->ctl_addr)
  716. hwif->tp_ops->write_devctl(hwif, ATA_DEVCTL_OBS);
  717. if (request_irq(hwif->irq, irq_handler, sa, hwif->name, hwif))
  718. goto out_up;
  719. #if !defined(__mc68000__)
  720. printk(KERN_INFO "%s at 0x%03lx-0x%03lx,0x%03lx on irq %d", hwif->name,
  721. io_ports->data_addr, io_ports->status_addr,
  722. io_ports->ctl_addr, hwif->irq);
  723. #else
  724. printk(KERN_INFO "%s at 0x%08lx on irq %d", hwif->name,
  725. io_ports->data_addr, hwif->irq);
  726. #endif /* __mc68000__ */
  727. if (hwif->host->host_flags & IDE_HFLAG_SERIALIZE)
  728. printk(KERN_CONT " (serialized)");
  729. printk(KERN_CONT "\n");
  730. return 0;
  731. out_up:
  732. return 1;
  733. }
  734. static int ata_lock(dev_t dev, void *data)
  735. {
  736. /* FIXME: we want to pin hwif down */
  737. return 0;
  738. }
  739. static struct kobject *ata_probe(dev_t dev, int *part, void *data)
  740. {
  741. ide_hwif_t *hwif = data;
  742. int unit = *part >> PARTN_BITS;
  743. ide_drive_t *drive = hwif->devices[unit];
  744. if ((drive->dev_flags & IDE_DFLAG_PRESENT) == 0)
  745. return NULL;
  746. if (drive->media == ide_disk)
  747. request_module("ide-disk");
  748. if (drive->media == ide_cdrom || drive->media == ide_optical)
  749. request_module("ide-cd");
  750. if (drive->media == ide_tape)
  751. request_module("ide-tape");
  752. if (drive->media == ide_floppy)
  753. request_module("ide-floppy");
  754. return NULL;
  755. }
  756. static struct kobject *exact_match(dev_t dev, int *part, void *data)
  757. {
  758. struct gendisk *p = data;
  759. *part &= (1 << PARTN_BITS) - 1;
  760. return &disk_to_dev(p)->kobj;
  761. }
  762. static int exact_lock(dev_t dev, void *data)
  763. {
  764. struct gendisk *p = data;
  765. if (!get_disk(p))
  766. return -1;
  767. return 0;
  768. }
  769. void ide_register_region(struct gendisk *disk)
  770. {
  771. blk_register_region(MKDEV(disk->major, disk->first_minor),
  772. disk->minors, NULL, exact_match, exact_lock, disk);
  773. }
  774. EXPORT_SYMBOL_GPL(ide_register_region);
  775. void ide_unregister_region(struct gendisk *disk)
  776. {
  777. blk_unregister_region(MKDEV(disk->major, disk->first_minor),
  778. disk->minors);
  779. }
  780. EXPORT_SYMBOL_GPL(ide_unregister_region);
  781. void ide_init_disk(struct gendisk *disk, ide_drive_t *drive)
  782. {
  783. ide_hwif_t *hwif = drive->hwif;
  784. unsigned int unit = drive->dn & 1;
  785. disk->major = hwif->major;
  786. disk->first_minor = unit << PARTN_BITS;
  787. sprintf(disk->disk_name, "hd%c", 'a' + hwif->index * MAX_DRIVES + unit);
  788. disk->queue = drive->queue;
  789. }
  790. EXPORT_SYMBOL_GPL(ide_init_disk);
  791. static void drive_release_dev (struct device *dev)
  792. {
  793. ide_drive_t *drive = container_of(dev, ide_drive_t, gendev);
  794. ide_proc_unregister_device(drive);
  795. blk_cleanup_queue(drive->queue);
  796. drive->queue = NULL;
  797. kfree(drive->id);
  798. drive->id = NULL;
  799. drive->dev_flags &= ~IDE_DFLAG_PRESENT;
  800. complete(&drive->gendev_rel_comp);
  801. }
  802. static int hwif_init(ide_hwif_t *hwif)
  803. {
  804. if (!hwif->irq) {
  805. printk(KERN_ERR "%s: disabled, no IRQ\n", hwif->name);
  806. return 0;
  807. }
  808. if (register_blkdev(hwif->major, hwif->name))
  809. return 0;
  810. if (!hwif->sg_max_nents)
  811. hwif->sg_max_nents = PRD_ENTRIES;
  812. hwif->sg_table = kmalloc(sizeof(struct scatterlist)*hwif->sg_max_nents,
  813. GFP_KERNEL);
  814. if (!hwif->sg_table) {
  815. printk(KERN_ERR "%s: unable to allocate SG table.\n", hwif->name);
  816. goto out;
  817. }
  818. sg_init_table(hwif->sg_table, hwif->sg_max_nents);
  819. if (init_irq(hwif)) {
  820. printk(KERN_ERR "%s: disabled, unable to get IRQ %d\n",
  821. hwif->name, hwif->irq);
  822. goto out;
  823. }
  824. blk_register_region(MKDEV(hwif->major, 0), MAX_DRIVES << PARTN_BITS,
  825. THIS_MODULE, ata_probe, ata_lock, hwif);
  826. return 1;
  827. out:
  828. unregister_blkdev(hwif->major, hwif->name);
  829. return 0;
  830. }
  831. static void hwif_register_devices(ide_hwif_t *hwif)
  832. {
  833. ide_drive_t *drive;
  834. unsigned int i;
  835. ide_port_for_each_present_dev(i, drive, hwif) {
  836. struct device *dev = &drive->gendev;
  837. int ret;
  838. dev_set_name(dev, "%u.%u", hwif->index, i);
  839. dev->parent = &hwif->gendev;
  840. dev->bus = &ide_bus_type;
  841. dev->driver_data = drive;
  842. dev->release = drive_release_dev;
  843. ret = device_register(dev);
  844. if (ret < 0)
  845. printk(KERN_WARNING "IDE: %s: device_register error: "
  846. "%d\n", __func__, ret);
  847. }
  848. }
  849. static void ide_port_init_devices(ide_hwif_t *hwif)
  850. {
  851. const struct ide_port_ops *port_ops = hwif->port_ops;
  852. ide_drive_t *drive;
  853. int i;
  854. ide_port_for_each_dev(i, drive, hwif) {
  855. drive->dn = i + hwif->channel * 2;
  856. if (hwif->host_flags & IDE_HFLAG_IO_32BIT)
  857. drive->io_32bit = 1;
  858. if (hwif->host_flags & IDE_HFLAG_NO_IO_32BIT)
  859. drive->dev_flags |= IDE_DFLAG_NO_IO_32BIT;
  860. if (hwif->host_flags & IDE_HFLAG_UNMASK_IRQS)
  861. drive->dev_flags |= IDE_DFLAG_UNMASK;
  862. if (hwif->host_flags & IDE_HFLAG_NO_UNMASK_IRQS)
  863. drive->dev_flags |= IDE_DFLAG_NO_UNMASK;
  864. if (port_ops && port_ops->init_dev)
  865. port_ops->init_dev(drive);
  866. }
  867. }
  868. static void ide_init_port(ide_hwif_t *hwif, unsigned int port,
  869. const struct ide_port_info *d)
  870. {
  871. hwif->channel = port;
  872. if (d->chipset)
  873. hwif->chipset = d->chipset;
  874. if (d->init_iops)
  875. d->init_iops(hwif);
  876. /* ->host_flags may be set by ->init_iops (or even earlier...) */
  877. hwif->host_flags |= d->host_flags;
  878. hwif->pio_mask = d->pio_mask;
  879. if (d->tp_ops)
  880. hwif->tp_ops = d->tp_ops;
  881. /* ->set_pio_mode for DTC2278 is currently limited to port 0 */
  882. if ((hwif->host_flags & IDE_HFLAG_DTC2278) == 0 || hwif->channel == 0)
  883. hwif->port_ops = d->port_ops;
  884. hwif->swdma_mask = d->swdma_mask;
  885. hwif->mwdma_mask = d->mwdma_mask;
  886. hwif->ultra_mask = d->udma_mask;
  887. if ((d->host_flags & IDE_HFLAG_NO_DMA) == 0) {
  888. int rc;
  889. hwif->dma_ops = d->dma_ops;
  890. if (d->init_dma)
  891. rc = d->init_dma(hwif, d);
  892. else
  893. rc = ide_hwif_setup_dma(hwif, d);
  894. if (rc < 0) {
  895. printk(KERN_INFO "%s: DMA disabled\n", hwif->name);
  896. hwif->dma_ops = NULL;
  897. hwif->dma_base = 0;
  898. hwif->swdma_mask = 0;
  899. hwif->mwdma_mask = 0;
  900. hwif->ultra_mask = 0;
  901. }
  902. }
  903. if ((d->host_flags & IDE_HFLAG_SERIALIZE) ||
  904. ((d->host_flags & IDE_HFLAG_SERIALIZE_DMA) && hwif->dma_base))
  905. hwif->host->host_flags |= IDE_HFLAG_SERIALIZE;
  906. if (d->max_sectors)
  907. hwif->rqsize = d->max_sectors;
  908. else {
  909. if ((hwif->host_flags & IDE_HFLAG_NO_LBA48) ||
  910. (hwif->host_flags & IDE_HFLAG_NO_LBA48_DMA))
  911. hwif->rqsize = 256;
  912. else
  913. hwif->rqsize = 65536;
  914. }
  915. /* call chipset specific routine for each enabled port */
  916. if (d->init_hwif)
  917. d->init_hwif(hwif);
  918. }
  919. static void ide_port_cable_detect(ide_hwif_t *hwif)
  920. {
  921. const struct ide_port_ops *port_ops = hwif->port_ops;
  922. if (port_ops && port_ops->cable_detect && (hwif->ultra_mask & 0x78)) {
  923. if (hwif->cbl != ATA_CBL_PATA40_SHORT)
  924. hwif->cbl = port_ops->cable_detect(hwif);
  925. }
  926. }
  927. static const u8 ide_hwif_to_major[] =
  928. { IDE0_MAJOR, IDE1_MAJOR, IDE2_MAJOR, IDE3_MAJOR, IDE4_MAJOR,
  929. IDE5_MAJOR, IDE6_MAJOR, IDE7_MAJOR, IDE8_MAJOR, IDE9_MAJOR };
  930. static void ide_port_init_devices_data(ide_hwif_t *hwif)
  931. {
  932. ide_drive_t *drive;
  933. int i;
  934. ide_port_for_each_dev(i, drive, hwif) {
  935. u8 j = (hwif->index * MAX_DRIVES) + i;
  936. memset(drive, 0, sizeof(*drive));
  937. drive->media = ide_disk;
  938. drive->select = (i << 4) | ATA_DEVICE_OBS;
  939. drive->hwif = hwif;
  940. drive->ready_stat = ATA_DRDY;
  941. drive->bad_wstat = BAD_W_STAT;
  942. drive->special.b.recalibrate = 1;
  943. drive->special.b.set_geometry = 1;
  944. drive->name[0] = 'h';
  945. drive->name[1] = 'd';
  946. drive->name[2] = 'a' + j;
  947. drive->max_failures = IDE_DEFAULT_MAX_FAILURES;
  948. INIT_LIST_HEAD(&drive->list);
  949. init_completion(&drive->gendev_rel_comp);
  950. }
  951. }
  952. static void ide_init_port_data(ide_hwif_t *hwif, unsigned int index)
  953. {
  954. /* fill in any non-zero initial values */
  955. hwif->index = index;
  956. hwif->major = ide_hwif_to_major[index];
  957. hwif->name[0] = 'i';
  958. hwif->name[1] = 'd';
  959. hwif->name[2] = 'e';
  960. hwif->name[3] = '0' + index;
  961. spin_lock_init(&hwif->lock);
  962. init_timer(&hwif->timer);
  963. hwif->timer.function = &ide_timer_expiry;
  964. hwif->timer.data = (unsigned long)hwif;
  965. init_completion(&hwif->gendev_rel_comp);
  966. hwif->tp_ops = &default_tp_ops;
  967. ide_port_init_devices_data(hwif);
  968. }
  969. static void ide_init_port_hw(ide_hwif_t *hwif, hw_regs_t *hw)
  970. {
  971. memcpy(&hwif->io_ports, &hw->io_ports, sizeof(hwif->io_ports));
  972. hwif->irq = hw->irq;
  973. hwif->chipset = hw->chipset;
  974. hwif->dev = hw->dev;
  975. hwif->gendev.parent = hw->parent ? hw->parent : hw->dev;
  976. hwif->ack_intr = hw->ack_intr;
  977. hwif->config_data = hw->config;
  978. }
  979. static unsigned int ide_indexes;
  980. /**
  981. * ide_find_port_slot - find free port slot
  982. * @d: IDE port info
  983. *
  984. * Return the new port slot index or -ENOENT if we are out of free slots.
  985. */
  986. static int ide_find_port_slot(const struct ide_port_info *d)
  987. {
  988. int idx = -ENOENT;
  989. u8 bootable = (d && (d->host_flags & IDE_HFLAG_NON_BOOTABLE)) ? 0 : 1;
  990. u8 i = (d && (d->host_flags & IDE_HFLAG_QD_2ND_PORT)) ? 1 : 0;;
  991. /*
  992. * Claim an unassigned slot.
  993. *
  994. * Give preference to claiming other slots before claiming ide0/ide1,
  995. * just in case there's another interface yet-to-be-scanned
  996. * which uses ports 0x1f0/0x170 (the ide0/ide1 defaults).
  997. *
  998. * Unless there is a bootable card that does not use the standard
  999. * ports 0x1f0/0x170 (the ide0/ide1 defaults).
  1000. */
  1001. mutex_lock(&ide_cfg_mtx);
  1002. if (bootable) {
  1003. if ((ide_indexes | i) != (1 << MAX_HWIFS) - 1)
  1004. idx = ffz(ide_indexes | i);
  1005. } else {
  1006. if ((ide_indexes | 3) != (1 << MAX_HWIFS) - 1)
  1007. idx = ffz(ide_indexes | 3);
  1008. else if ((ide_indexes & 3) != 3)
  1009. idx = ffz(ide_indexes);
  1010. }
  1011. if (idx >= 0)
  1012. ide_indexes |= (1 << idx);
  1013. mutex_unlock(&ide_cfg_mtx);
  1014. return idx;
  1015. }
  1016. static void ide_free_port_slot(int idx)
  1017. {
  1018. mutex_lock(&ide_cfg_mtx);
  1019. ide_indexes &= ~(1 << idx);
  1020. mutex_unlock(&ide_cfg_mtx);
  1021. }
  1022. static void ide_port_free_devices(ide_hwif_t *hwif)
  1023. {
  1024. ide_drive_t *drive;
  1025. int i;
  1026. ide_port_for_each_dev(i, drive, hwif)
  1027. kfree(drive);
  1028. }
  1029. static int ide_port_alloc_devices(ide_hwif_t *hwif, int node)
  1030. {
  1031. int i;
  1032. for (i = 0; i < MAX_DRIVES; i++) {
  1033. ide_drive_t *drive;
  1034. drive = kzalloc_node(sizeof(*drive), GFP_KERNEL, node);
  1035. if (drive == NULL)
  1036. goto out_nomem;
  1037. hwif->devices[i] = drive;
  1038. }
  1039. return 0;
  1040. out_nomem:
  1041. ide_port_free_devices(hwif);
  1042. return -ENOMEM;
  1043. }
  1044. struct ide_host *ide_host_alloc(const struct ide_port_info *d, hw_regs_t **hws)
  1045. {
  1046. struct ide_host *host;
  1047. struct device *dev = hws[0] ? hws[0]->dev : NULL;
  1048. int node = dev ? dev_to_node(dev) : -1;
  1049. int i;
  1050. host = kzalloc_node(sizeof(*host), GFP_KERNEL, node);
  1051. if (host == NULL)
  1052. return NULL;
  1053. for (i = 0; i < MAX_HOST_PORTS; i++) {
  1054. ide_hwif_t *hwif;
  1055. int idx;
  1056. if (hws[i] == NULL)
  1057. continue;
  1058. hwif = kzalloc_node(sizeof(*hwif), GFP_KERNEL, node);
  1059. if (hwif == NULL)
  1060. continue;
  1061. if (ide_port_alloc_devices(hwif, node) < 0) {
  1062. kfree(hwif);
  1063. continue;
  1064. }
  1065. idx = ide_find_port_slot(d);
  1066. if (idx < 0) {
  1067. printk(KERN_ERR "%s: no free slot for interface\n",
  1068. d ? d->name : "ide");
  1069. kfree(hwif);
  1070. continue;
  1071. }
  1072. ide_init_port_data(hwif, idx);
  1073. hwif->host = host;
  1074. host->ports[i] = hwif;
  1075. host->n_ports++;
  1076. }
  1077. if (host->n_ports == 0) {
  1078. kfree(host);
  1079. return NULL;
  1080. }
  1081. host->dev[0] = dev;
  1082. if (d) {
  1083. host->init_chipset = d->init_chipset;
  1084. host->get_lock = d->get_lock;
  1085. host->release_lock = d->release_lock;
  1086. host->host_flags = d->host_flags;
  1087. host->irq_flags = d->irq_flags;
  1088. }
  1089. return host;
  1090. }
  1091. EXPORT_SYMBOL_GPL(ide_host_alloc);
  1092. static void ide_port_free(ide_hwif_t *hwif)
  1093. {
  1094. ide_port_free_devices(hwif);
  1095. ide_free_port_slot(hwif->index);
  1096. kfree(hwif);
  1097. }
  1098. static void ide_disable_port(ide_hwif_t *hwif)
  1099. {
  1100. struct ide_host *host = hwif->host;
  1101. int i;
  1102. printk(KERN_INFO "%s: disabling port\n", hwif->name);
  1103. for (i = 0; i < MAX_HOST_PORTS; i++) {
  1104. if (host->ports[i] == hwif) {
  1105. host->ports[i] = NULL;
  1106. host->n_ports--;
  1107. }
  1108. }
  1109. ide_port_free(hwif);
  1110. }
  1111. int ide_host_register(struct ide_host *host, const struct ide_port_info *d,
  1112. hw_regs_t **hws)
  1113. {
  1114. ide_hwif_t *hwif, *mate = NULL;
  1115. int i, j = 0;
  1116. ide_host_for_each_port(i, hwif, host) {
  1117. if (hwif == NULL) {
  1118. mate = NULL;
  1119. continue;
  1120. }
  1121. ide_init_port_hw(hwif, hws[i]);
  1122. ide_port_apply_params(hwif);
  1123. if ((i & 1) && mate) {
  1124. hwif->mate = mate;
  1125. mate->mate = hwif;
  1126. }
  1127. mate = (i & 1) ? NULL : hwif;
  1128. ide_init_port(hwif, i & 1, d);
  1129. ide_port_cable_detect(hwif);
  1130. ide_port_init_devices(hwif);
  1131. }
  1132. ide_host_for_each_port(i, hwif, host) {
  1133. if (hwif == NULL)
  1134. continue;
  1135. if (ide_probe_port(hwif) == 0)
  1136. hwif->present = 1;
  1137. if ((hwif->host_flags & IDE_HFLAG_4DRIVES) == 0 ||
  1138. hwif->mate == NULL || hwif->mate->present == 0) {
  1139. if (ide_register_port(hwif)) {
  1140. ide_disable_port(hwif);
  1141. continue;
  1142. }
  1143. }
  1144. if (hwif->present)
  1145. ide_port_tune_devices(hwif);
  1146. }
  1147. ide_host_for_each_port(i, hwif, host) {
  1148. if (hwif == NULL)
  1149. continue;
  1150. if (hwif_init(hwif) == 0) {
  1151. printk(KERN_INFO "%s: failed to initialize IDE "
  1152. "interface\n", hwif->name);
  1153. device_unregister(&hwif->gendev);
  1154. ide_disable_port(hwif);
  1155. continue;
  1156. }
  1157. if (hwif->present)
  1158. if (ide_port_setup_devices(hwif) == 0) {
  1159. hwif->present = 0;
  1160. continue;
  1161. }
  1162. j++;
  1163. ide_acpi_init_port(hwif);
  1164. if (hwif->present)
  1165. ide_acpi_port_init_devices(hwif);
  1166. }
  1167. ide_host_for_each_port(i, hwif, host) {
  1168. if (hwif == NULL)
  1169. continue;
  1170. if (hwif->present)
  1171. hwif_register_devices(hwif);
  1172. }
  1173. ide_host_for_each_port(i, hwif, host) {
  1174. if (hwif == NULL)
  1175. continue;
  1176. ide_sysfs_register_port(hwif);
  1177. ide_proc_register_port(hwif);
  1178. if (hwif->present)
  1179. ide_proc_port_register_devices(hwif);
  1180. }
  1181. return j ? 0 : -1;
  1182. }
  1183. EXPORT_SYMBOL_GPL(ide_host_register);
  1184. int ide_host_add(const struct ide_port_info *d, hw_regs_t **hws,
  1185. struct ide_host **hostp)
  1186. {
  1187. struct ide_host *host;
  1188. int rc;
  1189. host = ide_host_alloc(d, hws);
  1190. if (host == NULL)
  1191. return -ENOMEM;
  1192. rc = ide_host_register(host, d, hws);
  1193. if (rc) {
  1194. ide_host_free(host);
  1195. return rc;
  1196. }
  1197. if (hostp)
  1198. *hostp = host;
  1199. return 0;
  1200. }
  1201. EXPORT_SYMBOL_GPL(ide_host_add);
  1202. static void __ide_port_unregister_devices(ide_hwif_t *hwif)
  1203. {
  1204. ide_drive_t *drive;
  1205. int i;
  1206. ide_port_for_each_present_dev(i, drive, hwif) {
  1207. device_unregister(&drive->gendev);
  1208. wait_for_completion(&drive->gendev_rel_comp);
  1209. }
  1210. }
  1211. void ide_port_unregister_devices(ide_hwif_t *hwif)
  1212. {
  1213. mutex_lock(&ide_cfg_mtx);
  1214. __ide_port_unregister_devices(hwif);
  1215. hwif->present = 0;
  1216. ide_port_init_devices_data(hwif);
  1217. mutex_unlock(&ide_cfg_mtx);
  1218. }
  1219. EXPORT_SYMBOL_GPL(ide_port_unregister_devices);
  1220. /**
  1221. * ide_unregister - free an IDE interface
  1222. * @hwif: IDE interface
  1223. *
  1224. * Perform the final unregister of an IDE interface.
  1225. *
  1226. * Locking:
  1227. * The caller must not hold the IDE locks.
  1228. *
  1229. * It is up to the caller to be sure there is no pending I/O here,
  1230. * and that the interface will not be reopened (present/vanishing
  1231. * locking isn't yet done BTW).
  1232. */
  1233. static void ide_unregister(ide_hwif_t *hwif)
  1234. {
  1235. BUG_ON(in_interrupt());
  1236. BUG_ON(irqs_disabled());
  1237. mutex_lock(&ide_cfg_mtx);
  1238. if (hwif->present) {
  1239. __ide_port_unregister_devices(hwif);
  1240. hwif->present = 0;
  1241. }
  1242. ide_proc_unregister_port(hwif);
  1243. free_irq(hwif->irq, hwif);
  1244. device_unregister(hwif->portdev);
  1245. device_unregister(&hwif->gendev);
  1246. wait_for_completion(&hwif->gendev_rel_comp);
  1247. /*
  1248. * Remove us from the kernel's knowledge
  1249. */
  1250. blk_unregister_region(MKDEV(hwif->major, 0), MAX_DRIVES<<PARTN_BITS);
  1251. kfree(hwif->sg_table);
  1252. unregister_blkdev(hwif->major, hwif->name);
  1253. ide_release_dma_engine(hwif);
  1254. mutex_unlock(&ide_cfg_mtx);
  1255. }
  1256. void ide_host_free(struct ide_host *host)
  1257. {
  1258. ide_hwif_t *hwif;
  1259. int i;
  1260. ide_host_for_each_port(i, hwif, host) {
  1261. if (hwif)
  1262. ide_port_free(hwif);
  1263. }
  1264. kfree(host);
  1265. }
  1266. EXPORT_SYMBOL_GPL(ide_host_free);
  1267. void ide_host_remove(struct ide_host *host)
  1268. {
  1269. ide_hwif_t *hwif;
  1270. int i;
  1271. ide_host_for_each_port(i, hwif, host) {
  1272. if (hwif)
  1273. ide_unregister(hwif);
  1274. }
  1275. ide_host_free(host);
  1276. }
  1277. EXPORT_SYMBOL_GPL(ide_host_remove);
  1278. void ide_port_scan(ide_hwif_t *hwif)
  1279. {
  1280. ide_port_apply_params(hwif);
  1281. ide_port_cable_detect(hwif);
  1282. ide_port_init_devices(hwif);
  1283. if (ide_probe_port(hwif) < 0)
  1284. return;
  1285. hwif->present = 1;
  1286. ide_port_tune_devices(hwif);
  1287. ide_port_setup_devices(hwif);
  1288. ide_acpi_port_init_devices(hwif);
  1289. hwif_register_devices(hwif);
  1290. ide_proc_port_register_devices(hwif);
  1291. }
  1292. EXPORT_SYMBOL_GPL(ide_port_scan);