ide-probe.c 40 KB

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