ide-probe.c 39 KB

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