ide-probe.c 40 KB

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