ide-probe.c 43 KB

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