ide-probe.c 40 KB

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