ide-probe.c 42 KB

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