ide-probe.c 37 KB

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