ide-probe.c 43 KB

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