ide-probe.c 43 KB

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