ide-probe.c 43 KB

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