ide-probe.c 43 KB

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