ide-probe.c 43 KB

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