pata_it821x.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987
  1. /*
  2. * pata_it821x.c - IT821x PATA for new ATA layer
  3. * (C) 2005 Red Hat Inc
  4. * Alan Cox <[email protected]>
  5. * (C) 2007 Bartlomiej Zolnierkiewicz
  6. *
  7. * based upon
  8. *
  9. * it821x.c
  10. *
  11. * linux/drivers/ide/pci/it821x.c Version 0.09 December 2004
  12. *
  13. * Copyright (C) 2004 Red Hat
  14. *
  15. * May be copied or modified under the terms of the GNU General Public License
  16. * Based in part on the ITE vendor provided SCSI driver.
  17. *
  18. * Documentation available from IT8212F_V04.pdf
  19. * http://www.ite.com.tw/EN/products_more.aspx?CategoryID=3&ID=5,91
  20. * Some other documents are NDA.
  21. *
  22. * The ITE8212 isn't exactly a standard IDE controller. It has two
  23. * modes. In pass through mode then it is an IDE controller. In its smart
  24. * mode its actually quite a capable hardware raid controller disguised
  25. * as an IDE controller. Smart mode only understands DMA read/write and
  26. * identify, none of the fancier commands apply. The IT8211 is identical
  27. * in other respects but lacks the raid mode.
  28. *
  29. * Errata:
  30. * o Rev 0x10 also requires master/slave hold the same DMA timings and
  31. * cannot do ATAPI MWDMA.
  32. * o The identify data for raid volumes lacks CHS info (technically ok)
  33. * but also fails to set the LBA28 and other bits. We fix these in
  34. * the IDE probe quirk code.
  35. * o If you write LBA48 sized I/O's (ie > 256 sector) in smart mode
  36. * raid then the controller firmware dies
  37. * o Smart mode without RAID doesn't clear all the necessary identify
  38. * bits to reduce the command set to the one used
  39. *
  40. * This has a few impacts on the driver
  41. * - In pass through mode we do all the work you would expect
  42. * - In smart mode the clocking set up is done by the controller generally
  43. * but we must watch the other limits and filter.
  44. * - There are a few extra vendor commands that actually talk to the
  45. * controller but only work PIO with no IRQ.
  46. *
  47. * Vendor areas of the identify block in smart mode are used for the
  48. * timing and policy set up. Each HDD in raid mode also has a serial
  49. * block on the disk. The hardware extra commands are get/set chip status,
  50. * rebuild, get rebuild status.
  51. *
  52. * In Linux the driver supports pass through mode as if the device was
  53. * just another IDE controller. If the smart mode is running then
  54. * volumes are managed by the controller firmware and each IDE "disk"
  55. * is a raid volume. Even more cute - the controller can do automated
  56. * hotplug and rebuild.
  57. *
  58. * The pass through controller itself is a little demented. It has a
  59. * flaw that it has a single set of PIO/MWDMA timings per channel so
  60. * non UDMA devices restrict each others performance. It also has a
  61. * single clock source per channel so mixed UDMA100/133 performance
  62. * isn't perfect and we have to pick a clock. Thankfully none of this
  63. * matters in smart mode. ATAPI DMA is not currently supported.
  64. *
  65. * It seems the smart mode is a win for RAID1/RAID10 but otherwise not.
  66. *
  67. * TODO
  68. * - ATAPI and other speed filtering
  69. * - RAID configuration ioctls
  70. */
  71. #include <linux/kernel.h>
  72. #include <linux/module.h>
  73. #include <linux/pci.h>
  74. #include <linux/blkdev.h>
  75. #include <linux/delay.h>
  76. #include <linux/slab.h>
  77. #include <scsi/scsi_host.h>
  78. #include <linux/libata.h>
  79. #define DRV_NAME "pata_it821x"
  80. #define DRV_VERSION "0.4.2"
  81. struct it821x_dev
  82. {
  83. unsigned int smart:1, /* Are we in smart raid mode */
  84. timing10:1; /* Rev 0x10 */
  85. u8 clock_mode; /* 0, ATA_50 or ATA_66 */
  86. u8 want[2][2]; /* Mode/Pri log for master slave */
  87. /* We need these for switching the clock when DMA goes on/off
  88. The high byte is the 66Mhz timing */
  89. u16 pio[2]; /* Cached PIO values */
  90. u16 mwdma[2]; /* Cached MWDMA values */
  91. u16 udma[2]; /* Cached UDMA values (per drive) */
  92. u16 last_device; /* Master or slave loaded ? */
  93. };
  94. #define ATA_66 0
  95. #define ATA_50 1
  96. #define ATA_ANY 2
  97. #define UDMA_OFF 0
  98. #define MWDMA_OFF 0
  99. /*
  100. * We allow users to force the card into non raid mode without
  101. * flashing the alternative BIOS. This is also necessary right now
  102. * for embedded platforms that cannot run a PC BIOS but are using this
  103. * device.
  104. */
  105. static int it8212_noraid;
  106. /**
  107. * it821x_program - program the PIO/MWDMA registers
  108. * @ap: ATA port
  109. * @adev: Device to program
  110. * @timing: Timing value (66Mhz in top 8bits, 50 in the low 8)
  111. *
  112. * Program the PIO/MWDMA timing for this channel according to the
  113. * current clock. These share the same register so are managed by
  114. * the DMA start/stop sequence as with the old driver.
  115. */
  116. static void it821x_program(struct ata_port *ap, struct ata_device *adev, u16 timing)
  117. {
  118. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  119. struct it821x_dev *itdev = ap->private_data;
  120. int channel = ap->port_no;
  121. u8 conf;
  122. /* Program PIO/MWDMA timing bits */
  123. if (itdev->clock_mode == ATA_66)
  124. conf = timing >> 8;
  125. else
  126. conf = timing & 0xFF;
  127. pci_write_config_byte(pdev, 0x54 + 4 * channel, conf);
  128. }
  129. /**
  130. * it821x_program_udma - program the UDMA registers
  131. * @ap: ATA port
  132. * @adev: ATA device to update
  133. * @timing: Timing bits. Top 8 are for 66Mhz bottom for 50Mhz
  134. *
  135. * Program the UDMA timing for this drive according to the
  136. * current clock. Handles the dual clocks and also knows about
  137. * the errata on the 0x10 revision. The UDMA errata is partly handled
  138. * here and partly in start_dma.
  139. */
  140. static void it821x_program_udma(struct ata_port *ap, struct ata_device *adev, u16 timing)
  141. {
  142. struct it821x_dev *itdev = ap->private_data;
  143. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  144. int channel = ap->port_no;
  145. int unit = adev->devno;
  146. u8 conf;
  147. /* Program UDMA timing bits */
  148. if (itdev->clock_mode == ATA_66)
  149. conf = timing >> 8;
  150. else
  151. conf = timing & 0xFF;
  152. if (itdev->timing10 == 0)
  153. pci_write_config_byte(pdev, 0x56 + 4 * channel + unit, conf);
  154. else {
  155. /* Early revision must be programmed for both together */
  156. pci_write_config_byte(pdev, 0x56 + 4 * channel, conf);
  157. pci_write_config_byte(pdev, 0x56 + 4 * channel + 1, conf);
  158. }
  159. }
  160. /**
  161. * it821x_clock_strategy
  162. * @ap: ATA interface
  163. * @adev: ATA device being updated
  164. *
  165. * Select between the 50 and 66Mhz base clocks to get the best
  166. * results for this interface.
  167. */
  168. static void it821x_clock_strategy(struct ata_port *ap, struct ata_device *adev)
  169. {
  170. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  171. struct it821x_dev *itdev = ap->private_data;
  172. u8 unit = adev->devno;
  173. struct ata_device *pair = ata_dev_pair(adev);
  174. int clock, altclock;
  175. u8 v;
  176. int sel = 0;
  177. /* Look for the most wanted clocking */
  178. if (itdev->want[0][0] > itdev->want[1][0]) {
  179. clock = itdev->want[0][1];
  180. altclock = itdev->want[1][1];
  181. } else {
  182. clock = itdev->want[1][1];
  183. altclock = itdev->want[0][1];
  184. }
  185. /* Master doesn't care does the slave ? */
  186. if (clock == ATA_ANY)
  187. clock = altclock;
  188. /* Nobody cares - keep the same clock */
  189. if (clock == ATA_ANY)
  190. return;
  191. /* No change */
  192. if (clock == itdev->clock_mode)
  193. return;
  194. /* Load this into the controller */
  195. if (clock == ATA_66)
  196. itdev->clock_mode = ATA_66;
  197. else {
  198. itdev->clock_mode = ATA_50;
  199. sel = 1;
  200. }
  201. pci_read_config_byte(pdev, 0x50, &v);
  202. v &= ~(1 << (1 + ap->port_no));
  203. v |= sel << (1 + ap->port_no);
  204. pci_write_config_byte(pdev, 0x50, v);
  205. /*
  206. * Reprogram the UDMA/PIO of the pair drive for the switch
  207. * MWDMA will be dealt with by the dma switcher
  208. */
  209. if (pair && itdev->udma[1-unit] != UDMA_OFF) {
  210. it821x_program_udma(ap, pair, itdev->udma[1-unit]);
  211. it821x_program(ap, pair, itdev->pio[1-unit]);
  212. }
  213. /*
  214. * Reprogram the UDMA/PIO of our drive for the switch.
  215. * MWDMA will be dealt with by the dma switcher
  216. */
  217. if (itdev->udma[unit] != UDMA_OFF) {
  218. it821x_program_udma(ap, adev, itdev->udma[unit]);
  219. it821x_program(ap, adev, itdev->pio[unit]);
  220. }
  221. }
  222. /**
  223. * it821x_passthru_set_piomode - set PIO mode data
  224. * @ap: ATA interface
  225. * @adev: ATA device
  226. *
  227. * Configure for PIO mode. This is complicated as the register is
  228. * shared by PIO and MWDMA and for both channels.
  229. */
  230. static void it821x_passthru_set_piomode(struct ata_port *ap, struct ata_device *adev)
  231. {
  232. /* Spec says 89 ref driver uses 88 */
  233. static const u16 pio[] = { 0xAA88, 0xA382, 0xA181, 0x3332, 0x3121 };
  234. static const u8 pio_want[] = { ATA_66, ATA_66, ATA_66, ATA_66, ATA_ANY };
  235. struct it821x_dev *itdev = ap->private_data;
  236. int unit = adev->devno;
  237. int mode_wanted = adev->pio_mode - XFER_PIO_0;
  238. /* We prefer 66Mhz clock for PIO 0-3, don't care for PIO4 */
  239. itdev->want[unit][1] = pio_want[mode_wanted];
  240. itdev->want[unit][0] = 1; /* PIO is lowest priority */
  241. itdev->pio[unit] = pio[mode_wanted];
  242. it821x_clock_strategy(ap, adev);
  243. it821x_program(ap, adev, itdev->pio[unit]);
  244. }
  245. /**
  246. * it821x_passthru_set_dmamode - set initial DMA mode data
  247. * @ap: ATA interface
  248. * @adev: ATA device
  249. *
  250. * Set up the DMA modes. The actions taken depend heavily on the mode
  251. * to use. If UDMA is used as is hopefully the usual case then the
  252. * timing register is private and we need only consider the clock. If
  253. * we are using MWDMA then we have to manage the setting ourself as
  254. * we switch devices and mode.
  255. */
  256. static void it821x_passthru_set_dmamode(struct ata_port *ap, struct ata_device *adev)
  257. {
  258. static const u16 dma[] = { 0x8866, 0x3222, 0x3121 };
  259. static const u8 mwdma_want[] = { ATA_ANY, ATA_66, ATA_ANY };
  260. static const u16 udma[] = { 0x4433, 0x4231, 0x3121, 0x2121, 0x1111, 0x2211, 0x1111 };
  261. static const u8 udma_want[] = { ATA_ANY, ATA_50, ATA_ANY, ATA_66, ATA_66, ATA_50, ATA_66 };
  262. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  263. struct it821x_dev *itdev = ap->private_data;
  264. int channel = ap->port_no;
  265. int unit = adev->devno;
  266. u8 conf;
  267. if (adev->dma_mode >= XFER_UDMA_0) {
  268. int mode_wanted = adev->dma_mode - XFER_UDMA_0;
  269. itdev->want[unit][1] = udma_want[mode_wanted];
  270. itdev->want[unit][0] = 3; /* UDMA is high priority */
  271. itdev->mwdma[unit] = MWDMA_OFF;
  272. itdev->udma[unit] = udma[mode_wanted];
  273. if (mode_wanted >= 5)
  274. itdev->udma[unit] |= 0x8080; /* UDMA 5/6 select on */
  275. /* UDMA on. Again revision 0x10 must do the pair */
  276. pci_read_config_byte(pdev, 0x50, &conf);
  277. if (itdev->timing10)
  278. conf &= channel ? 0x9F: 0xE7;
  279. else
  280. conf &= ~ (1 << (3 + 2 * channel + unit));
  281. pci_write_config_byte(pdev, 0x50, conf);
  282. it821x_clock_strategy(ap, adev);
  283. it821x_program_udma(ap, adev, itdev->udma[unit]);
  284. } else {
  285. int mode_wanted = adev->dma_mode - XFER_MW_DMA_0;
  286. itdev->want[unit][1] = mwdma_want[mode_wanted];
  287. itdev->want[unit][0] = 2; /* MWDMA is low priority */
  288. itdev->mwdma[unit] = dma[mode_wanted];
  289. itdev->udma[unit] = UDMA_OFF;
  290. /* UDMA bits off - Revision 0x10 do them in pairs */
  291. pci_read_config_byte(pdev, 0x50, &conf);
  292. if (itdev->timing10)
  293. conf |= channel ? 0x60: 0x18;
  294. else
  295. conf |= 1 << (3 + 2 * channel + unit);
  296. pci_write_config_byte(pdev, 0x50, conf);
  297. it821x_clock_strategy(ap, adev);
  298. }
  299. }
  300. /**
  301. * it821x_passthru_bmdma_start - DMA start callback
  302. * @qc: Command in progress
  303. *
  304. * Usually drivers set the DMA timing at the point the set_dmamode call
  305. * is made. IT821x however requires we load new timings on the
  306. * transitions in some cases.
  307. */
  308. static void it821x_passthru_bmdma_start(struct ata_queued_cmd *qc)
  309. {
  310. struct ata_port *ap = qc->ap;
  311. struct ata_device *adev = qc->dev;
  312. struct it821x_dev *itdev = ap->private_data;
  313. int unit = adev->devno;
  314. if (itdev->mwdma[unit] != MWDMA_OFF)
  315. it821x_program(ap, adev, itdev->mwdma[unit]);
  316. else if (itdev->udma[unit] != UDMA_OFF && itdev->timing10)
  317. it821x_program_udma(ap, adev, itdev->udma[unit]);
  318. ata_bmdma_start(qc);
  319. }
  320. /**
  321. * it821x_passthru_bmdma_stop - DMA stop callback
  322. * @qc: ATA command
  323. *
  324. * We loaded new timings in dma_start, as a result we need to restore
  325. * the PIO timings in dma_stop so that the next command issue gets the
  326. * right clock values.
  327. */
  328. static void it821x_passthru_bmdma_stop(struct ata_queued_cmd *qc)
  329. {
  330. struct ata_port *ap = qc->ap;
  331. struct ata_device *adev = qc->dev;
  332. struct it821x_dev *itdev = ap->private_data;
  333. int unit = adev->devno;
  334. ata_bmdma_stop(qc);
  335. if (itdev->mwdma[unit] != MWDMA_OFF)
  336. it821x_program(ap, adev, itdev->pio[unit]);
  337. }
  338. /**
  339. * it821x_passthru_dev_select - Select master/slave
  340. * @ap: ATA port
  341. * @device: Device number (not pointer)
  342. *
  343. * Device selection hook. If necessary perform clock switching
  344. */
  345. static void it821x_passthru_dev_select(struct ata_port *ap,
  346. unsigned int device)
  347. {
  348. struct it821x_dev *itdev = ap->private_data;
  349. if (itdev && device != itdev->last_device) {
  350. struct ata_device *adev = &ap->link.device[device];
  351. it821x_program(ap, adev, itdev->pio[adev->devno]);
  352. itdev->last_device = device;
  353. }
  354. ata_sff_dev_select(ap, device);
  355. }
  356. /**
  357. * it821x_smart_qc_issue - wrap qc issue prot
  358. * @qc: command
  359. *
  360. * Wrap the command issue sequence for the IT821x. We need to
  361. * perform out own device selection timing loads before the
  362. * usual happenings kick off
  363. */
  364. static unsigned int it821x_smart_qc_issue(struct ata_queued_cmd *qc)
  365. {
  366. switch(qc->tf.command)
  367. {
  368. /* Commands the firmware supports */
  369. case ATA_CMD_READ:
  370. case ATA_CMD_READ_EXT:
  371. case ATA_CMD_WRITE:
  372. case ATA_CMD_WRITE_EXT:
  373. case ATA_CMD_PIO_READ:
  374. case ATA_CMD_PIO_READ_EXT:
  375. case ATA_CMD_PIO_WRITE:
  376. case ATA_CMD_PIO_WRITE_EXT:
  377. case ATA_CMD_READ_MULTI:
  378. case ATA_CMD_READ_MULTI_EXT:
  379. case ATA_CMD_WRITE_MULTI:
  380. case ATA_CMD_WRITE_MULTI_EXT:
  381. case ATA_CMD_ID_ATA:
  382. case ATA_CMD_INIT_DEV_PARAMS:
  383. case 0xFC: /* Internal 'report rebuild state' */
  384. /* Arguably should just no-op this one */
  385. case ATA_CMD_SET_FEATURES:
  386. return ata_bmdma_qc_issue(qc);
  387. }
  388. ata_dev_dbg(qc->dev, "it821x: can't process command 0x%02X\n",
  389. qc->tf.command);
  390. return AC_ERR_DEV;
  391. }
  392. /**
  393. * it821x_passthru_qc_issue - wrap qc issue prot
  394. * @qc: command
  395. *
  396. * Wrap the command issue sequence for the IT821x. We need to
  397. * perform out own device selection timing loads before the
  398. * usual happenings kick off
  399. */
  400. static unsigned int it821x_passthru_qc_issue(struct ata_queued_cmd *qc)
  401. {
  402. it821x_passthru_dev_select(qc->ap, qc->dev->devno);
  403. return ata_bmdma_qc_issue(qc);
  404. }
  405. /**
  406. * it821x_smart_set_mode - mode setting
  407. * @link: interface to set up
  408. * @unused: device that failed (error only)
  409. *
  410. * Use a non standard set_mode function. We don't want to be tuned.
  411. * The BIOS configured everything. Our job is not to fiddle. We
  412. * read the dma enabled bits from the PCI configuration of the device
  413. * and respect them.
  414. */
  415. static int it821x_smart_set_mode(struct ata_link *link, struct ata_device **unused)
  416. {
  417. struct ata_device *dev;
  418. ata_for_each_dev(dev, link, ENABLED) {
  419. /* We don't really care */
  420. dev->pio_mode = XFER_PIO_0;
  421. dev->dma_mode = XFER_MW_DMA_0;
  422. /* We do need the right mode information for DMA or PIO
  423. and this comes from the current configuration flags */
  424. if (ata_id_has_dma(dev->id)) {
  425. ata_dev_info(dev, "configured for DMA\n");
  426. dev->xfer_mode = XFER_MW_DMA_0;
  427. dev->xfer_shift = ATA_SHIFT_MWDMA;
  428. dev->flags &= ~ATA_DFLAG_PIO;
  429. } else {
  430. ata_dev_info(dev, "configured for PIO\n");
  431. dev->xfer_mode = XFER_PIO_0;
  432. dev->xfer_shift = ATA_SHIFT_PIO;
  433. dev->flags |= ATA_DFLAG_PIO;
  434. }
  435. }
  436. return 0;
  437. }
  438. /**
  439. * it821x_dev_config - Called each device identify
  440. * @adev: Device that has just been identified
  441. *
  442. * Perform the initial setup needed for each device that is chip
  443. * special. In our case we need to lock the sector count to avoid
  444. * blowing the brains out of the firmware with large LBA48 requests
  445. *
  446. */
  447. static void it821x_dev_config(struct ata_device *adev)
  448. {
  449. unsigned char model_num[ATA_ID_PROD_LEN + 1];
  450. ata_id_c_string(adev->id, model_num, ATA_ID_PROD, sizeof(model_num));
  451. if (adev->max_sectors > 255)
  452. adev->max_sectors = 255;
  453. if (strstr(model_num, "Integrated Technology Express")) {
  454. /* RAID mode */
  455. if (adev->id[129] == 1)
  456. ata_dev_info(adev, "%sRAID%d volume\n",
  457. adev->id[147] ? "Bootable " : "",
  458. adev->id[129]);
  459. else
  460. ata_dev_info(adev, "%sRAID%d volume (%dK stripe)\n",
  461. adev->id[147] ? "Bootable " : "",
  462. adev->id[129], adev->id[146]);
  463. }
  464. /* This is a controller firmware triggered funny, don't
  465. report the drive faulty! */
  466. adev->horkage &= ~ATA_HORKAGE_DIAGNOSTIC;
  467. /* No HPA in 'smart' mode */
  468. adev->horkage |= ATA_HORKAGE_BROKEN_HPA;
  469. }
  470. /**
  471. * it821x_read_id - Hack identify data up
  472. * @adev: device to read
  473. * @tf: proposed taskfile
  474. * @id: buffer for returned ident data
  475. *
  476. * Query the devices on this firmware driven port and slightly
  477. * mash the identify data to stop us and common tools trying to
  478. * use features not firmware supported. The firmware itself does
  479. * some masking (eg SMART) but not enough.
  480. */
  481. static unsigned int it821x_read_id(struct ata_device *adev,
  482. struct ata_taskfile *tf, __le16 *id)
  483. {
  484. unsigned int err_mask;
  485. unsigned char model_num[ATA_ID_PROD_LEN + 1];
  486. err_mask = ata_do_dev_read_id(adev, tf, id);
  487. if (err_mask)
  488. return err_mask;
  489. ata_id_c_string((u16 *)id, model_num, ATA_ID_PROD, sizeof(model_num));
  490. id[83] &= cpu_to_le16(~(1 << 12)); /* Cache flush is firmware handled */
  491. id[84] &= cpu_to_le16(~(1 << 6)); /* No FUA */
  492. id[85] &= cpu_to_le16(~(1 << 10)); /* No HPA */
  493. id[76] = 0; /* No NCQ/AN etc */
  494. if (strstr(model_num, "Integrated Technology Express")) {
  495. /* Set feature bits the firmware neglects */
  496. id[49] |= cpu_to_le16(0x0300); /* LBA, DMA */
  497. id[83] &= cpu_to_le16(0x7FFF);
  498. id[83] |= cpu_to_le16(0x4400); /* Word 83 is valid and LBA48 */
  499. id[86] |= cpu_to_le16(0x0400); /* LBA48 on */
  500. id[ATA_ID_MAJOR_VER] |= cpu_to_le16(0x1F);
  501. /* Clear the serial number because it's different each boot
  502. which breaks validation on resume */
  503. memset(&id[ATA_ID_SERNO], 0x20, ATA_ID_SERNO_LEN);
  504. }
  505. return err_mask;
  506. }
  507. /**
  508. * it821x_check_atapi_dma - ATAPI DMA handler
  509. * @qc: Command we are about to issue
  510. *
  511. * Decide if this ATAPI command can be issued by DMA on this
  512. * controller. Return 0 if it can be.
  513. */
  514. static int it821x_check_atapi_dma(struct ata_queued_cmd *qc)
  515. {
  516. struct ata_port *ap = qc->ap;
  517. struct it821x_dev *itdev = ap->private_data;
  518. /* Only use dma for transfers to/from the media. */
  519. if (ata_qc_raw_nbytes(qc) < 2048)
  520. return -EOPNOTSUPP;
  521. /* No ATAPI DMA in smart mode */
  522. if (itdev->smart)
  523. return -EOPNOTSUPP;
  524. /* No ATAPI DMA on rev 10 */
  525. if (itdev->timing10)
  526. return -EOPNOTSUPP;
  527. /* Cool */
  528. return 0;
  529. }
  530. /**
  531. * it821x_display_disk - display disk setup
  532. * @ap: ATA port
  533. * @n: Device number
  534. * @buf: Buffer block from firmware
  535. *
  536. * Produce a nice informative display of the device setup as provided
  537. * by the firmware.
  538. */
  539. static void it821x_display_disk(struct ata_port *ap, int n, u8 *buf)
  540. {
  541. unsigned char id[41];
  542. int mode = 0;
  543. const char *mtype = "";
  544. char mbuf[8];
  545. const char *cbl = "(40 wire cable)";
  546. static const char *types[5] = {
  547. "RAID0", "RAID1", "RAID 0+1", "JBOD", "DISK"
  548. };
  549. if (buf[52] > 4) /* No Disk */
  550. return;
  551. ata_id_c_string((u16 *)buf, id, 0, 41);
  552. if (buf[51]) {
  553. mode = ffs(buf[51]);
  554. mtype = "UDMA";
  555. } else if (buf[49]) {
  556. mode = ffs(buf[49]);
  557. mtype = "MWDMA";
  558. }
  559. if (buf[76])
  560. cbl = "";
  561. if (mode)
  562. snprintf(mbuf, 8, "%5s%d", mtype, mode - 1);
  563. else
  564. strcpy(mbuf, "PIO");
  565. if (buf[52] == 4)
  566. ata_port_info(ap, "%d: %-6s %-8s %s %s\n",
  567. n, mbuf, types[buf[52]], id, cbl);
  568. else
  569. ata_port_info(ap, "%d: %-6s %-8s Volume: %1d %s %s\n",
  570. n, mbuf, types[buf[52]], buf[53], id, cbl);
  571. if (buf[125] < 100)
  572. ata_port_info(ap, "%d: Rebuilding: %d%%\n", n, buf[125]);
  573. }
  574. /**
  575. * it821x_firmware_command - issue firmware command
  576. * @ap: IT821x port to interrogate
  577. * @cmd: command
  578. * @len: length
  579. *
  580. * Issue firmware commands expecting data back from the controller. We
  581. * use this to issue commands that do not go via the normal paths. Other
  582. * commands such as 0xFC can be issued normally.
  583. */
  584. static u8 *it821x_firmware_command(struct ata_port *ap, u8 cmd, int len)
  585. {
  586. u8 status;
  587. int n = 0;
  588. u16 *buf = kmalloc(len, GFP_KERNEL);
  589. if (!buf)
  590. return NULL;
  591. /* This isn't quite a normal ATA command as we are talking to the
  592. firmware not the drives */
  593. ap->ctl |= ATA_NIEN;
  594. iowrite8(ap->ctl, ap->ioaddr.ctl_addr);
  595. ata_wait_idle(ap);
  596. iowrite8(ATA_DEVICE_OBS, ap->ioaddr.device_addr);
  597. iowrite8(cmd, ap->ioaddr.command_addr);
  598. udelay(1);
  599. /* This should be almost immediate but a little paranoia goes a long
  600. way. */
  601. while(n++ < 10) {
  602. status = ioread8(ap->ioaddr.status_addr);
  603. if (status & ATA_ERR) {
  604. kfree(buf);
  605. ata_port_err(ap, "%s: rejected\n", __func__);
  606. return NULL;
  607. }
  608. if (status & ATA_DRQ) {
  609. ioread16_rep(ap->ioaddr.data_addr, buf, len/2);
  610. return (u8 *)buf;
  611. }
  612. usleep_range(500, 1000);
  613. }
  614. kfree(buf);
  615. ata_port_err(ap, "%s: timeout\n", __func__);
  616. return NULL;
  617. }
  618. /**
  619. * it821x_probe_firmware - firmware reporting/setup
  620. * @ap: IT821x port being probed
  621. *
  622. * Probe the firmware of the controller by issuing firmware command
  623. * 0xFA and analysing the returned data.
  624. */
  625. static void it821x_probe_firmware(struct ata_port *ap)
  626. {
  627. u8 *buf;
  628. int i;
  629. /* This is a bit ugly as we can't just issue a task file to a device
  630. as this is controller magic */
  631. buf = it821x_firmware_command(ap, 0xFA, 512);
  632. if (buf != NULL) {
  633. ata_port_info(ap, "pata_it821x: Firmware %02X/%02X/%02X%02X\n",
  634. buf[505],
  635. buf[506],
  636. buf[507],
  637. buf[508]);
  638. for (i = 0; i < 4; i++)
  639. it821x_display_disk(ap, i, buf + 128 * i);
  640. kfree(buf);
  641. }
  642. }
  643. /**
  644. * it821x_port_start - port setup
  645. * @ap: ATA port being set up
  646. *
  647. * The it821x needs to maintain private data structures and also to
  648. * use the standard PCI interface which lacks support for this
  649. * functionality. We instead set up the private data on the port
  650. * start hook, and tear it down on port stop
  651. */
  652. static int it821x_port_start(struct ata_port *ap)
  653. {
  654. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  655. struct it821x_dev *itdev;
  656. u8 conf;
  657. int ret = ata_bmdma_port_start(ap);
  658. if (ret < 0)
  659. return ret;
  660. itdev = devm_kzalloc(&pdev->dev, sizeof(struct it821x_dev), GFP_KERNEL);
  661. if (itdev == NULL)
  662. return -ENOMEM;
  663. ap->private_data = itdev;
  664. pci_read_config_byte(pdev, 0x50, &conf);
  665. if (conf & 1) {
  666. itdev->smart = 1;
  667. /* Long I/O's although allowed in LBA48 space cause the
  668. onboard firmware to enter the twighlight zone */
  669. /* No ATAPI DMA in this mode either */
  670. if (ap->port_no == 0)
  671. it821x_probe_firmware(ap);
  672. }
  673. /* Pull the current clocks from 0x50 */
  674. if (conf & (1 << (1 + ap->port_no)))
  675. itdev->clock_mode = ATA_50;
  676. else
  677. itdev->clock_mode = ATA_66;
  678. itdev->want[0][1] = ATA_ANY;
  679. itdev->want[1][1] = ATA_ANY;
  680. itdev->last_device = -1;
  681. if (pdev->revision == 0x10) {
  682. itdev->timing10 = 1;
  683. /* Need to disable ATAPI DMA for this case */
  684. if (!itdev->smart)
  685. dev_warn(&pdev->dev,
  686. "Revision 0x10, workarounds activated.\n");
  687. }
  688. return 0;
  689. }
  690. /**
  691. * it821x_rdc_cable - Cable detect for RDC1010
  692. * @ap: port we are checking
  693. *
  694. * Return the RDC1010 cable type. Unlike the IT821x we know how to do
  695. * this and can do host side cable detect
  696. */
  697. static int it821x_rdc_cable(struct ata_port *ap)
  698. {
  699. u16 r40;
  700. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  701. pci_read_config_word(pdev, 0x40, &r40);
  702. if (r40 & (1 << (2 + ap->port_no)))
  703. return ATA_CBL_PATA40;
  704. return ATA_CBL_PATA80;
  705. }
  706. static struct scsi_host_template it821x_sht = {
  707. ATA_BMDMA_SHT(DRV_NAME),
  708. };
  709. static struct ata_port_operations it821x_smart_port_ops = {
  710. .inherits = &ata_bmdma_port_ops,
  711. .check_atapi_dma= it821x_check_atapi_dma,
  712. .qc_issue = it821x_smart_qc_issue,
  713. .cable_detect = ata_cable_80wire,
  714. .set_mode = it821x_smart_set_mode,
  715. .dev_config = it821x_dev_config,
  716. .read_id = it821x_read_id,
  717. .port_start = it821x_port_start,
  718. };
  719. static struct ata_port_operations it821x_passthru_port_ops = {
  720. .inherits = &ata_bmdma_port_ops,
  721. .check_atapi_dma= it821x_check_atapi_dma,
  722. .sff_dev_select = it821x_passthru_dev_select,
  723. .bmdma_start = it821x_passthru_bmdma_start,
  724. .bmdma_stop = it821x_passthru_bmdma_stop,
  725. .qc_issue = it821x_passthru_qc_issue,
  726. .cable_detect = ata_cable_unknown,
  727. .set_piomode = it821x_passthru_set_piomode,
  728. .set_dmamode = it821x_passthru_set_dmamode,
  729. .port_start = it821x_port_start,
  730. };
  731. static struct ata_port_operations it821x_rdc_port_ops = {
  732. .inherits = &ata_bmdma_port_ops,
  733. .check_atapi_dma= it821x_check_atapi_dma,
  734. .sff_dev_select = it821x_passthru_dev_select,
  735. .bmdma_start = it821x_passthru_bmdma_start,
  736. .bmdma_stop = it821x_passthru_bmdma_stop,
  737. .qc_issue = it821x_passthru_qc_issue,
  738. .cable_detect = it821x_rdc_cable,
  739. .set_piomode = it821x_passthru_set_piomode,
  740. .set_dmamode = it821x_passthru_set_dmamode,
  741. .port_start = it821x_port_start,
  742. };
  743. static void it821x_disable_raid(struct pci_dev *pdev)
  744. {
  745. /* Neither the RDC nor the IT8211 */
  746. if (pdev->vendor != PCI_VENDOR_ID_ITE ||
  747. pdev->device != PCI_DEVICE_ID_ITE_8212)
  748. return;
  749. /* Reset local CPU, and set BIOS not ready */
  750. pci_write_config_byte(pdev, 0x5E, 0x01);
  751. /* Set to bypass mode, and reset PCI bus */
  752. pci_write_config_byte(pdev, 0x50, 0x00);
  753. pci_write_config_word(pdev, PCI_COMMAND,
  754. PCI_COMMAND_PARITY | PCI_COMMAND_IO |
  755. PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
  756. pci_write_config_word(pdev, 0x40, 0xA0F3);
  757. pci_write_config_dword(pdev,0x4C, 0x02040204);
  758. pci_write_config_byte(pdev, 0x42, 0x36);
  759. pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0x20);
  760. }
  761. static int it821x_init_one(struct pci_dev *pdev, const struct pci_device_id *id)
  762. {
  763. u8 conf;
  764. static const struct ata_port_info info_smart = {
  765. .flags = ATA_FLAG_SLAVE_POSS,
  766. .pio_mask = ATA_PIO4,
  767. .mwdma_mask = ATA_MWDMA2,
  768. .udma_mask = ATA_UDMA6,
  769. .port_ops = &it821x_smart_port_ops
  770. };
  771. static const struct ata_port_info info_passthru = {
  772. .flags = ATA_FLAG_SLAVE_POSS,
  773. .pio_mask = ATA_PIO4,
  774. .mwdma_mask = ATA_MWDMA2,
  775. .udma_mask = ATA_UDMA6,
  776. .port_ops = &it821x_passthru_port_ops
  777. };
  778. static const struct ata_port_info info_rdc = {
  779. .flags = ATA_FLAG_SLAVE_POSS,
  780. .pio_mask = ATA_PIO4,
  781. .mwdma_mask = ATA_MWDMA2,
  782. .udma_mask = ATA_UDMA6,
  783. .port_ops = &it821x_rdc_port_ops
  784. };
  785. static const struct ata_port_info info_rdc_11 = {
  786. .flags = ATA_FLAG_SLAVE_POSS,
  787. .pio_mask = ATA_PIO4,
  788. .mwdma_mask = ATA_MWDMA2,
  789. /* No UDMA */
  790. .port_ops = &it821x_rdc_port_ops
  791. };
  792. const struct ata_port_info *ppi[] = { NULL, NULL };
  793. static const char *mode[2] = { "pass through", "smart" };
  794. int rc;
  795. rc = pcim_enable_device(pdev);
  796. if (rc)
  797. return rc;
  798. if (pdev->vendor == PCI_VENDOR_ID_RDC) {
  799. /* Deal with Vortex86SX */
  800. if (pdev->revision == 0x11)
  801. ppi[0] = &info_rdc_11;
  802. else
  803. ppi[0] = &info_rdc;
  804. } else {
  805. /* Force the card into bypass mode if so requested */
  806. if (it8212_noraid) {
  807. dev_info(&pdev->dev, "forcing bypass mode.\n");
  808. it821x_disable_raid(pdev);
  809. }
  810. pci_read_config_byte(pdev, 0x50, &conf);
  811. conf &= 1;
  812. dev_info(&pdev->dev, "controller in %s mode.\n", mode[conf]);
  813. if (conf == 0)
  814. ppi[0] = &info_passthru;
  815. else
  816. ppi[0] = &info_smart;
  817. }
  818. return ata_pci_bmdma_init_one(pdev, ppi, &it821x_sht, NULL, 0);
  819. }
  820. #ifdef CONFIG_PM_SLEEP
  821. static int it821x_reinit_one(struct pci_dev *pdev)
  822. {
  823. struct ata_host *host = pci_get_drvdata(pdev);
  824. int rc;
  825. rc = ata_pci_device_do_resume(pdev);
  826. if (rc)
  827. return rc;
  828. /* Resume - turn raid back off if need be */
  829. if (it8212_noraid)
  830. it821x_disable_raid(pdev);
  831. ata_host_resume(host);
  832. return rc;
  833. }
  834. #endif
  835. static const struct pci_device_id it821x[] = {
  836. { PCI_VDEVICE(ITE, PCI_DEVICE_ID_ITE_8211), },
  837. { PCI_VDEVICE(ITE, PCI_DEVICE_ID_ITE_8212), },
  838. { PCI_VDEVICE(RDC, PCI_DEVICE_ID_RDC_D1010), },
  839. { },
  840. };
  841. static struct pci_driver it821x_pci_driver = {
  842. .name = DRV_NAME,
  843. .id_table = it821x,
  844. .probe = it821x_init_one,
  845. .remove = ata_pci_remove_one,
  846. #ifdef CONFIG_PM_SLEEP
  847. .suspend = ata_pci_device_suspend,
  848. .resume = it821x_reinit_one,
  849. #endif
  850. };
  851. module_pci_driver(it821x_pci_driver);
  852. MODULE_AUTHOR("Alan Cox");
  853. MODULE_DESCRIPTION("low-level driver for the IT8211/IT8212 IDE RAID controller");
  854. MODULE_LICENSE("GPL");
  855. MODULE_DEVICE_TABLE(pci, it821x);
  856. MODULE_VERSION(DRV_VERSION);
  857. module_param_named(noraid, it8212_noraid, int, S_IRUGO);
  858. MODULE_PARM_DESC(noraid, "Force card into bypass mode");