ide-probe.c 43 KB

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