ide-probe.c 39 KB

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