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