pata_it821x.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983
  1. /*
  2. * pata_it821x.c - IT821x PATA for new ATA layer
  3. * (C) 2005 Red Hat Inc
  4. * Alan Cox <alan@lxorguk.ukuu.org.uk>
  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_dma_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_dma_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. printk(KERN_DEBUG "it821x: can't process command 0x%02X\n", qc->tf.command);
  389. return AC_ERR_DEV;
  390. }
  391. /**
  392. * it821x_passthru_qc_issue - wrap qc issue prot
  393. * @qc: command
  394. *
  395. * Wrap the command issue sequence for the IT821x. We need to
  396. * perform out own device selection timing loads before the
  397. * usual happenings kick off
  398. */
  399. static unsigned int it821x_passthru_qc_issue(struct ata_queued_cmd *qc)
  400. {
  401. it821x_passthru_dev_select(qc->ap, qc->dev->devno);
  402. return ata_bmdma_qc_issue(qc);
  403. }
  404. /**
  405. * it821x_smart_set_mode - mode setting
  406. * @link: interface to set up
  407. * @unused: device that failed (error only)
  408. *
  409. * Use a non standard set_mode function. We don't want to be tuned.
  410. * The BIOS configured everything. Our job is not to fiddle. We
  411. * read the dma enabled bits from the PCI configuration of the device
  412. * and respect them.
  413. */
  414. static int it821x_smart_set_mode(struct ata_link *link, struct ata_device **unused)
  415. {
  416. struct ata_device *dev;
  417. ata_for_each_dev(dev, link, ENABLED) {
  418. /* We don't really care */
  419. dev->pio_mode = XFER_PIO_0;
  420. dev->dma_mode = XFER_MW_DMA_0;
  421. /* We do need the right mode information for DMA or PIO
  422. and this comes from the current configuration flags */
  423. if (ata_id_has_dma(dev->id)) {
  424. ata_dev_info(dev, "configured for DMA\n");
  425. dev->xfer_mode = XFER_MW_DMA_0;
  426. dev->xfer_shift = ATA_SHIFT_MWDMA;
  427. dev->flags &= ~ATA_DFLAG_PIO;
  428. } else {
  429. ata_dev_info(dev, "configured for PIO\n");
  430. dev->xfer_mode = XFER_PIO_0;
  431. dev->xfer_shift = ATA_SHIFT_PIO;
  432. dev->flags |= ATA_DFLAG_PIO;
  433. }
  434. }
  435. return 0;
  436. }
  437. /**
  438. * it821x_dev_config - Called each device identify
  439. * @adev: Device that has just been identified
  440. *
  441. * Perform the initial setup needed for each device that is chip
  442. * special. In our case we need to lock the sector count to avoid
  443. * blowing the brains out of the firmware with large LBA48 requests
  444. *
  445. */
  446. static void it821x_dev_config(struct ata_device *adev)
  447. {
  448. unsigned char model_num[ATA_ID_PROD_LEN + 1];
  449. ata_id_c_string(adev->id, model_num, ATA_ID_PROD, sizeof(model_num));
  450. if (adev->max_sectors > 255)
  451. adev->max_sectors = 255;
  452. if (strstr(model_num, "Integrated Technology Express")) {
  453. /* RAID mode */
  454. ata_dev_info(adev, "%sRAID%d volume",
  455. adev->id[147] ? "Bootable " : "",
  456. adev->id[129]);
  457. if (adev->id[129] != 1)
  458. pr_cont("(%dK stripe)", adev->id[146]);
  459. pr_cont("\n");
  460. }
  461. /* This is a controller firmware triggered funny, don't
  462. report the drive faulty! */
  463. adev->horkage &= ~ATA_HORKAGE_DIAGNOSTIC;
  464. /* No HPA in 'smart' mode */
  465. adev->horkage |= ATA_HORKAGE_BROKEN_HPA;
  466. }
  467. /**
  468. * it821x_read_id - Hack identify data up
  469. * @adev: device to read
  470. * @tf: proposed taskfile
  471. * @id: buffer for returned ident data
  472. *
  473. * Query the devices on this firmware driven port and slightly
  474. * mash the identify data to stop us and common tools trying to
  475. * use features not firmware supported. The firmware itself does
  476. * some masking (eg SMART) but not enough.
  477. */
  478. static unsigned int it821x_read_id(struct ata_device *adev,
  479. struct ata_taskfile *tf, u16 *id)
  480. {
  481. unsigned int err_mask;
  482. unsigned char model_num[ATA_ID_PROD_LEN + 1];
  483. err_mask = ata_do_dev_read_id(adev, tf, id);
  484. if (err_mask)
  485. return err_mask;
  486. ata_id_c_string(id, model_num, ATA_ID_PROD, sizeof(model_num));
  487. id[83] &= ~(1 << 12); /* Cache flush is firmware handled */
  488. id[83] &= ~(1 << 13); /* Ditto for LBA48 flushes */
  489. id[84] &= ~(1 << 6); /* No FUA */
  490. id[85] &= ~(1 << 10); /* No HPA */
  491. id[76] = 0; /* No NCQ/AN etc */
  492. if (strstr(model_num, "Integrated Technology Express")) {
  493. /* Set feature bits the firmware neglects */
  494. id[49] |= 0x0300; /* LBA, DMA */
  495. id[83] &= 0x7FFF;
  496. id[83] |= 0x4400; /* Word 83 is valid and LBA48 */
  497. id[86] |= 0x0400; /* LBA48 on */
  498. id[ATA_ID_MAJOR_VER] |= 0x1F;
  499. /* Clear the serial number because it's different each boot
  500. which breaks validation on resume */
  501. memset(&id[ATA_ID_SERNO], 0x20, ATA_ID_SERNO_LEN);
  502. }
  503. return err_mask;
  504. }
  505. /**
  506. * it821x_check_atapi_dma - ATAPI DMA handler
  507. * @qc: Command we are about to issue
  508. *
  509. * Decide if this ATAPI command can be issued by DMA on this
  510. * controller. Return 0 if it can be.
  511. */
  512. static int it821x_check_atapi_dma(struct ata_queued_cmd *qc)
  513. {
  514. struct ata_port *ap = qc->ap;
  515. struct it821x_dev *itdev = ap->private_data;
  516. /* Only use dma for transfers to/from the media. */
  517. if (ata_qc_raw_nbytes(qc) < 2048)
  518. return -EOPNOTSUPP;
  519. /* No ATAPI DMA in smart mode */
  520. if (itdev->smart)
  521. return -EOPNOTSUPP;
  522. /* No ATAPI DMA on rev 10 */
  523. if (itdev->timing10)
  524. return -EOPNOTSUPP;
  525. /* Cool */
  526. return 0;
  527. }
  528. /**
  529. * it821x_display_disk - display disk setup
  530. * @n: Device number
  531. * @buf: Buffer block from firmware
  532. *
  533. * Produce a nice informative display of the device setup as provided
  534. * by the firmware.
  535. */
  536. static void it821x_display_disk(int n, u8 *buf)
  537. {
  538. unsigned char id[41];
  539. int mode = 0;
  540. const char *mtype = "";
  541. char mbuf[8];
  542. const char *cbl = "(40 wire cable)";
  543. static const char *types[5] = {
  544. "RAID0", "RAID1", "RAID 0+1", "JBOD", "DISK"
  545. };
  546. if (buf[52] > 4) /* No Disk */
  547. return;
  548. ata_id_c_string((u16 *)buf, id, 0, 41);
  549. if (buf[51]) {
  550. mode = ffs(buf[51]);
  551. mtype = "UDMA";
  552. } else if (buf[49]) {
  553. mode = ffs(buf[49]);
  554. mtype = "MWDMA";
  555. }
  556. if (buf[76])
  557. cbl = "";
  558. if (mode)
  559. snprintf(mbuf, 8, "%5s%d", mtype, mode - 1);
  560. else
  561. strcpy(mbuf, "PIO");
  562. if (buf[52] == 4)
  563. printk(KERN_INFO "%d: %-6s %-8s %s %s\n",
  564. n, mbuf, types[buf[52]], id, cbl);
  565. else
  566. printk(KERN_INFO "%d: %-6s %-8s Volume: %1d %s %s\n",
  567. n, mbuf, types[buf[52]], buf[53], id, cbl);
  568. if (buf[125] < 100)
  569. printk(KERN_INFO "%d: Rebuilding: %d%%\n", n, buf[125]);
  570. }
  571. /**
  572. * it821x_firmware_command - issue firmware command
  573. * @ap: IT821x port to interrogate
  574. * @cmd: command
  575. * @len: length
  576. *
  577. * Issue firmware commands expecting data back from the controller. We
  578. * use this to issue commands that do not go via the normal paths. Other
  579. * commands such as 0xFC can be issued normally.
  580. */
  581. static u8 *it821x_firmware_command(struct ata_port *ap, u8 cmd, int len)
  582. {
  583. u8 status;
  584. int n = 0;
  585. u16 *buf = kmalloc(len, GFP_KERNEL);
  586. if (buf == NULL) {
  587. printk(KERN_ERR "it821x_firmware_command: Out of memory\n");
  588. return NULL;
  589. }
  590. /* This isn't quite a normal ATA command as we are talking to the
  591. firmware not the drives */
  592. ap->ctl |= ATA_NIEN;
  593. iowrite8(ap->ctl, ap->ioaddr.ctl_addr);
  594. ata_wait_idle(ap);
  595. iowrite8(ATA_DEVICE_OBS, ap->ioaddr.device_addr);
  596. iowrite8(cmd, ap->ioaddr.command_addr);
  597. udelay(1);
  598. /* This should be almost immediate but a little paranoia goes a long
  599. way. */
  600. while(n++ < 10) {
  601. status = ioread8(ap->ioaddr.status_addr);
  602. if (status & ATA_ERR) {
  603. kfree(buf);
  604. printk(KERN_ERR "it821x_firmware_command: rejected\n");
  605. return NULL;
  606. }
  607. if (status & ATA_DRQ) {
  608. ioread16_rep(ap->ioaddr.data_addr, buf, len/2);
  609. return (u8 *)buf;
  610. }
  611. mdelay(1);
  612. }
  613. kfree(buf);
  614. printk(KERN_ERR "it821x_firmware_command: timeout\n");
  615. return NULL;
  616. }
  617. /**
  618. * it821x_probe_firmware - firmware reporting/setup
  619. * @ap: IT821x port being probed
  620. *
  621. * Probe the firmware of the controller by issuing firmware command
  622. * 0xFA and analysing the returned data.
  623. */
  624. static void it821x_probe_firmware(struct ata_port *ap)
  625. {
  626. u8 *buf;
  627. int i;
  628. /* This is a bit ugly as we can't just issue a task file to a device
  629. as this is controller magic */
  630. buf = it821x_firmware_command(ap, 0xFA, 512);
  631. if (buf != NULL) {
  632. printk(KERN_INFO "pata_it821x: Firmware %02X/%02X/%02X%02X\n",
  633. buf[505],
  634. buf[506],
  635. buf[507],
  636. buf[508]);
  637. for (i = 0; i < 4; i++)
  638. it821x_display_disk(i, buf + 128 * i);
  639. kfree(buf);
  640. }
  641. }
  642. /**
  643. * it821x_port_start - port setup
  644. * @ap: ATA port being set up
  645. *
  646. * The it821x needs to maintain private data structures and also to
  647. * use the standard PCI interface which lacks support for this
  648. * functionality. We instead set up the private data on the port
  649. * start hook, and tear it down on port stop
  650. */
  651. static int it821x_port_start(struct ata_port *ap)
  652. {
  653. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  654. struct it821x_dev *itdev;
  655. u8 conf;
  656. int ret = ata_bmdma_port_start(ap);
  657. if (ret < 0)
  658. return ret;
  659. itdev = devm_kzalloc(&pdev->dev, sizeof(struct it821x_dev), GFP_KERNEL);
  660. if (itdev == NULL)
  661. return -ENOMEM;
  662. ap->private_data = itdev;
  663. pci_read_config_byte(pdev, 0x50, &conf);
  664. if (conf & 1) {
  665. itdev->smart = 1;
  666. /* Long I/O's although allowed in LBA48 space cause the
  667. onboard firmware to enter the twighlight zone */
  668. /* No ATAPI DMA in this mode either */
  669. if (ap->port_no == 0)
  670. it821x_probe_firmware(ap);
  671. }
  672. /* Pull the current clocks from 0x50 */
  673. if (conf & (1 << (1 + ap->port_no)))
  674. itdev->clock_mode = ATA_50;
  675. else
  676. itdev->clock_mode = ATA_66;
  677. itdev->want[0][1] = ATA_ANY;
  678. itdev->want[1][1] = ATA_ANY;
  679. itdev->last_device = -1;
  680. if (pdev->revision == 0x10) {
  681. itdev->timing10 = 1;
  682. /* Need to disable ATAPI DMA for this case */
  683. if (!itdev->smart)
  684. printk(KERN_WARNING DRV_NAME": Revision 0x10, workarounds activated.\n");
  685. }
  686. return 0;
  687. }
  688. /**
  689. * it821x_rdc_cable - Cable detect for RDC1010
  690. * @ap: port we are checking
  691. *
  692. * Return the RDC1010 cable type. Unlike the IT821x we know how to do
  693. * this and can do host side cable detect
  694. */
  695. static int it821x_rdc_cable(struct ata_port *ap)
  696. {
  697. u16 r40;
  698. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  699. pci_read_config_word(pdev, 0x40, &r40);
  700. if (r40 & (1 << (2 + ap->port_no)))
  701. return ATA_CBL_PATA40;
  702. return ATA_CBL_PATA80;
  703. }
  704. static struct scsi_host_template it821x_sht = {
  705. ATA_BMDMA_SHT(DRV_NAME),
  706. };
  707. static struct ata_port_operations it821x_smart_port_ops = {
  708. .inherits = &ata_bmdma_port_ops,
  709. .check_atapi_dma= it821x_check_atapi_dma,
  710. .qc_issue = it821x_smart_qc_issue,
  711. .cable_detect = ata_cable_80wire,
  712. .set_mode = it821x_smart_set_mode,
  713. .dev_config = it821x_dev_config,
  714. .read_id = it821x_read_id,
  715. .port_start = it821x_port_start,
  716. };
  717. static struct ata_port_operations it821x_passthru_port_ops = {
  718. .inherits = &ata_bmdma_port_ops,
  719. .check_atapi_dma= it821x_check_atapi_dma,
  720. .sff_dev_select = it821x_passthru_dev_select,
  721. .bmdma_start = it821x_passthru_bmdma_start,
  722. .bmdma_stop = it821x_passthru_bmdma_stop,
  723. .qc_issue = it821x_passthru_qc_issue,
  724. .cable_detect = ata_cable_unknown,
  725. .set_piomode = it821x_passthru_set_piomode,
  726. .set_dmamode = it821x_passthru_set_dmamode,
  727. .port_start = it821x_port_start,
  728. };
  729. static struct ata_port_operations it821x_rdc_port_ops = {
  730. .inherits = &ata_bmdma_port_ops,
  731. .check_atapi_dma= it821x_check_atapi_dma,
  732. .sff_dev_select = it821x_passthru_dev_select,
  733. .bmdma_start = it821x_passthru_bmdma_start,
  734. .bmdma_stop = it821x_passthru_bmdma_stop,
  735. .qc_issue = it821x_passthru_qc_issue,
  736. .cable_detect = it821x_rdc_cable,
  737. .set_piomode = it821x_passthru_set_piomode,
  738. .set_dmamode = it821x_passthru_set_dmamode,
  739. .port_start = it821x_port_start,
  740. };
  741. static void it821x_disable_raid(struct pci_dev *pdev)
  742. {
  743. /* Neither the RDC nor the IT8211 */
  744. if (pdev->vendor != PCI_VENDOR_ID_ITE ||
  745. pdev->device != PCI_DEVICE_ID_ITE_8212)
  746. return;
  747. /* Reset local CPU, and set BIOS not ready */
  748. pci_write_config_byte(pdev, 0x5E, 0x01);
  749. /* Set to bypass mode, and reset PCI bus */
  750. pci_write_config_byte(pdev, 0x50, 0x00);
  751. pci_write_config_word(pdev, PCI_COMMAND,
  752. PCI_COMMAND_PARITY | PCI_COMMAND_IO |
  753. PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
  754. pci_write_config_word(pdev, 0x40, 0xA0F3);
  755. pci_write_config_dword(pdev,0x4C, 0x02040204);
  756. pci_write_config_byte(pdev, 0x42, 0x36);
  757. pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0x20);
  758. }
  759. static int it821x_init_one(struct pci_dev *pdev, const struct pci_device_id *id)
  760. {
  761. u8 conf;
  762. static const struct ata_port_info info_smart = {
  763. .flags = ATA_FLAG_SLAVE_POSS,
  764. .pio_mask = ATA_PIO4,
  765. .mwdma_mask = ATA_MWDMA2,
  766. .udma_mask = ATA_UDMA6,
  767. .port_ops = &it821x_smart_port_ops
  768. };
  769. static const struct ata_port_info info_passthru = {
  770. .flags = ATA_FLAG_SLAVE_POSS,
  771. .pio_mask = ATA_PIO4,
  772. .mwdma_mask = ATA_MWDMA2,
  773. .udma_mask = ATA_UDMA6,
  774. .port_ops = &it821x_passthru_port_ops
  775. };
  776. static const struct ata_port_info info_rdc = {
  777. .flags = ATA_FLAG_SLAVE_POSS,
  778. .pio_mask = ATA_PIO4,
  779. .mwdma_mask = ATA_MWDMA2,
  780. .udma_mask = ATA_UDMA6,
  781. .port_ops = &it821x_rdc_port_ops
  782. };
  783. static const struct ata_port_info info_rdc_11 = {
  784. .flags = ATA_FLAG_SLAVE_POSS,
  785. .pio_mask = ATA_PIO4,
  786. .mwdma_mask = ATA_MWDMA2,
  787. /* No UDMA */
  788. .port_ops = &it821x_rdc_port_ops
  789. };
  790. const struct ata_port_info *ppi[] = { NULL, NULL };
  791. static const char *mode[2] = { "pass through", "smart" };
  792. int rc;
  793. rc = pcim_enable_device(pdev);
  794. if (rc)
  795. return rc;
  796. if (pdev->vendor == PCI_VENDOR_ID_RDC) {
  797. /* Deal with Vortex86SX */
  798. if (pdev->revision == 0x11)
  799. ppi[0] = &info_rdc_11;
  800. else
  801. ppi[0] = &info_rdc;
  802. } else {
  803. /* Force the card into bypass mode if so requested */
  804. if (it8212_noraid) {
  805. printk(KERN_INFO DRV_NAME ": forcing bypass mode.\n");
  806. it821x_disable_raid(pdev);
  807. }
  808. pci_read_config_byte(pdev, 0x50, &conf);
  809. conf &= 1;
  810. printk(KERN_INFO DRV_NAME": controller in %s mode.\n",
  811. mode[conf]);
  812. if (conf == 0)
  813. ppi[0] = &info_passthru;
  814. else
  815. ppi[0] = &info_smart;
  816. }
  817. return ata_pci_bmdma_init_one(pdev, ppi, &it821x_sht, NULL, 0);
  818. }
  819. #ifdef CONFIG_PM_SLEEP
  820. static int it821x_reinit_one(struct pci_dev *pdev)
  821. {
  822. struct ata_host *host = pci_get_drvdata(pdev);
  823. int rc;
  824. rc = ata_pci_device_do_resume(pdev);
  825. if (rc)
  826. return rc;
  827. /* Resume - turn raid back off if need be */
  828. if (it8212_noraid)
  829. it821x_disable_raid(pdev);
  830. ata_host_resume(host);
  831. return rc;
  832. }
  833. #endif
  834. static const struct pci_device_id it821x[] = {
  835. { PCI_VDEVICE(ITE, PCI_DEVICE_ID_ITE_8211), },
  836. { PCI_VDEVICE(ITE, PCI_DEVICE_ID_ITE_8212), },
  837. { PCI_VDEVICE(RDC, PCI_DEVICE_ID_RDC_D1010), },
  838. { },
  839. };
  840. static struct pci_driver it821x_pci_driver = {
  841. .name = DRV_NAME,
  842. .id_table = it821x,
  843. .probe = it821x_init_one,
  844. .remove = ata_pci_remove_one,
  845. #ifdef CONFIG_PM_SLEEP
  846. .suspend = ata_pci_device_suspend,
  847. .resume = it821x_reinit_one,
  848. #endif
  849. };
  850. module_pci_driver(it821x_pci_driver);
  851. MODULE_AUTHOR("Alan Cox");
  852. MODULE_DESCRIPTION("low-level driver for the IT8211/IT8212 IDE RAID controller");
  853. MODULE_LICENSE("GPL");
  854. MODULE_DEVICE_TABLE(pci, it821x);
  855. MODULE_VERSION(DRV_VERSION);
  856. module_param_named(noraid, it8212_noraid, int, S_IRUGO);
  857. MODULE_PARM_DESC(noraid, "Force card into bypass mode");