ide-probe.c 43 KB

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