ide-probe.c 41 KB

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