ide-probe.c 43 KB

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