ide-probe.c 43 KB

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