ide-probe.c 43 KB

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