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
  19. * http://www.ite.com.tw/pc/IT8212F_V04.pdf
  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/init.h>
  75. #include <linux/blkdev.h>
  76. #include <linux/delay.h>
  77. #include <scsi/scsi_host.h>
  78. #include <linux/libata.h>
  79. #define DRV_NAME "pata_it821x"
  80. #define DRV_VERSION "0.4.0"
  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_sff_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_sff_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_printk(dev, KERN_INFO, "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_printk(dev, KERN_INFO, "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. * FIXME: When FUA appears we need to block FUA too. And SMART and
  446. * basically we need to filter commands for this chip.
  447. */
  448. static void it821x_dev_config(struct ata_device *adev)
  449. {
  450. unsigned char model_num[ATA_ID_PROD_LEN + 1];
  451. ata_id_c_string(adev->id, model_num, ATA_ID_PROD, sizeof(model_num));
  452. if (adev->max_sectors > 255)
  453. adev->max_sectors = 255;
  454. if (strstr(model_num, "Integrated Technology Express")) {
  455. /* RAID mode */
  456. ata_dev_printk(adev, KERN_INFO, "%sRAID%d volume",
  457. adev->id[147]?"Bootable ":"",
  458. adev->id[129]);
  459. if (adev->id[129] != 1)
  460. printk("(%dK stripe)", adev->id[146]);
  461. printk(".\n");
  462. }
  463. /* This is a controller firmware triggered funny, don't
  464. report the drive faulty! */
  465. adev->horkage &= ~ATA_HORKAGE_DIAGNOSTIC;
  466. /* No HPA in 'smart' mode */
  467. adev->horkage |= ATA_HORKAGE_BROKEN_HPA;
  468. }
  469. /**
  470. * it821x_read_id - Hack identify data up
  471. * @adev: device to read
  472. * @tf: proposed taskfile
  473. * @id: buffer for returned ident data
  474. *
  475. * Query the devices on this firmware driven port and slightly
  476. * mash the identify data to stop us and common tools trying to
  477. * use features not firmware supported. The firmware itself does
  478. * some masking (eg SMART) but not enough.
  479. */
  480. static unsigned int it821x_read_id(struct ata_device *adev,
  481. struct ata_taskfile *tf, u16 *id)
  482. {
  483. unsigned int err_mask;
  484. unsigned char model_num[ATA_ID_PROD_LEN + 1];
  485. err_mask = ata_do_dev_read_id(adev, tf, id);
  486. if (err_mask)
  487. return err_mask;
  488. ata_id_c_string(id, model_num, ATA_ID_PROD, sizeof(model_num));
  489. id[83] &= ~(1 << 12); /* Cache flush is firmware handled */
  490. id[83] &= ~(1 << 13); /* Ditto for LBA48 flushes */
  491. id[84] &= ~(1 << 6); /* No FUA */
  492. id[85] &= ~(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] |= 0x0300; /* LBA, DMA */
  497. id[83] &= 0x7FFF;
  498. id[83] |= 0x4400; /* Word 83 is valid and LBA48 */
  499. id[86] |= 0x0400; /* LBA48 on */
  500. id[ATA_ID_MAJOR_VER] |= 0x1F;
  501. }
  502. return err_mask;
  503. }
  504. /**
  505. * it821x_check_atapi_dma - ATAPI DMA handler
  506. * @qc: Command we are about to issue
  507. *
  508. * Decide if this ATAPI command can be issued by DMA on this
  509. * controller. Return 0 if it can be.
  510. */
  511. static int it821x_check_atapi_dma(struct ata_queued_cmd *qc)
  512. {
  513. struct ata_port *ap = qc->ap;
  514. struct it821x_dev *itdev = ap->private_data;
  515. /* Only use dma for transfers to/from the media. */
  516. if (ata_qc_raw_nbytes(qc) < 2048)
  517. return -EOPNOTSUPP;
  518. /* No ATAPI DMA in smart mode */
  519. if (itdev->smart)
  520. return -EOPNOTSUPP;
  521. /* No ATAPI DMA on rev 10 */
  522. if (itdev->timing10)
  523. return -EOPNOTSUPP;
  524. /* Cool */
  525. return 0;
  526. }
  527. /**
  528. * it821x_display_disk - display disk setup
  529. * @n: Device number
  530. * @buf: Buffer block from firmware
  531. *
  532. * Produce a nice informative display of the device setup as provided
  533. * by the firmware.
  534. */
  535. static void it821x_display_disk(int n, u8 *buf)
  536. {
  537. unsigned char id[41];
  538. int mode = 0;
  539. char *mtype = "";
  540. char mbuf[8];
  541. char *cbl = "(40 wire cable)";
  542. static const char *types[5] = {
  543. "RAID0", "RAID1" "RAID 0+1", "JBOD", "DISK"
  544. };
  545. if (buf[52] > 4) /* No Disk */
  546. return;
  547. ata_id_c_string((u16 *)buf, id, 0, 41);
  548. if (buf[51]) {
  549. mode = ffs(buf[51]);
  550. mtype = "UDMA";
  551. } else if (buf[49]) {
  552. mode = ffs(buf[49]);
  553. mtype = "MWDMA";
  554. }
  555. if (buf[76])
  556. cbl = "";
  557. if (mode)
  558. snprintf(mbuf, 8, "%5s%d", mtype, mode - 1);
  559. else
  560. strcpy(mbuf, "PIO");
  561. if (buf[52] == 4)
  562. printk(KERN_INFO "%d: %-6s %-8s %s %s\n",
  563. n, mbuf, types[buf[52]], id, cbl);
  564. else
  565. printk(KERN_INFO "%d: %-6s %-8s Volume: %1d %s %s\n",
  566. n, mbuf, types[buf[52]], buf[53], id, cbl);
  567. if (buf[125] < 100)
  568. printk(KERN_INFO "%d: Rebuilding: %d%%\n", n, buf[125]);
  569. }
  570. /**
  571. * it821x_firmware_command - issue firmware command
  572. * @ap: IT821x port to interrogate
  573. * @cmd: command
  574. * @len: length
  575. *
  576. * Issue firmware commands expecting data back from the controller. We
  577. * use this to issue commands that do not go via the normal paths. Other
  578. * commands such as 0xFC can be issued normally.
  579. */
  580. static u8 *it821x_firmware_command(struct ata_port *ap, u8 cmd, int len)
  581. {
  582. u8 status;
  583. int n = 0;
  584. u16 *buf = kmalloc(len, GFP_KERNEL);
  585. if (buf == NULL) {
  586. printk(KERN_ERR "it821x_firmware_command: Out of memory\n");
  587. return NULL;
  588. }
  589. /* This isn't quite a normal ATA command as we are talking to the
  590. firmware not the drives */
  591. ap->ctl |= ATA_NIEN;
  592. iowrite8(ap->ctl, ap->ioaddr.ctl_addr);
  593. ata_wait_idle(ap);
  594. iowrite8(ATA_DEVICE_OBS, ap->ioaddr.device_addr);
  595. iowrite8(cmd, ap->ioaddr.command_addr);
  596. udelay(1);
  597. /* This should be almost immediate but a little paranoia goes a long
  598. way. */
  599. while(n++ < 10) {
  600. status = ioread8(ap->ioaddr.status_addr);
  601. if (status & ATA_ERR) {
  602. kfree(buf);
  603. printk(KERN_ERR "it821x_firmware_command: rejected\n");
  604. return NULL;
  605. }
  606. if (status & ATA_DRQ) {
  607. ioread16_rep(ap->ioaddr.data_addr, buf, len/2);
  608. return (u8 *)buf;
  609. }
  610. mdelay(1);
  611. }
  612. kfree(buf);
  613. printk(KERN_ERR "it821x_firmware_command: timeout\n");
  614. return NULL;
  615. }
  616. /**
  617. * it821x_probe_firmware - firmware reporting/setup
  618. * @ap: IT821x port being probed
  619. *
  620. * Probe the firmware of the controller by issuing firmware command
  621. * 0xFA and analysing the returned data.
  622. */
  623. static void it821x_probe_firmware(struct ata_port *ap)
  624. {
  625. u8 *buf;
  626. int i;
  627. /* This is a bit ugly as we can't just issue a task file to a device
  628. as this is controller magic */
  629. buf = it821x_firmware_command(ap, 0xFA, 512);
  630. if (buf != NULL) {
  631. printk(KERN_INFO "pata_it821x: Firmware %02X/%02X/%02X%02X\n",
  632. buf[505],
  633. buf[506],
  634. buf[507],
  635. buf[508]);
  636. for (i = 0; i < 4; i++)
  637. it821x_display_disk(i, buf + 128 * i);
  638. kfree(buf);
  639. }
  640. }
  641. /**
  642. * it821x_port_start - port setup
  643. * @ap: ATA port being set up
  644. *
  645. * The it821x needs to maintain private data structures and also to
  646. * use the standard PCI interface which lacks support for this
  647. * functionality. We instead set up the private data on the port
  648. * start hook, and tear it down on port stop
  649. */
  650. static int it821x_port_start(struct ata_port *ap)
  651. {
  652. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  653. struct it821x_dev *itdev;
  654. u8 conf;
  655. int ret = ata_sff_port_start(ap);
  656. if (ret < 0)
  657. return ret;
  658. itdev = devm_kzalloc(&pdev->dev, sizeof(struct it821x_dev), GFP_KERNEL);
  659. if (itdev == NULL)
  660. return -ENOMEM;
  661. ap->private_data = itdev;
  662. pci_read_config_byte(pdev, 0x50, &conf);
  663. if (conf & 1) {
  664. itdev->smart = 1;
  665. /* Long I/O's although allowed in LBA48 space cause the
  666. onboard firmware to enter the twighlight zone */
  667. /* No ATAPI DMA in this mode either */
  668. if (ap->port_no == 0)
  669. it821x_probe_firmware(ap);
  670. }
  671. /* Pull the current clocks from 0x50 */
  672. if (conf & (1 << (1 + ap->port_no)))
  673. itdev->clock_mode = ATA_50;
  674. else
  675. itdev->clock_mode = ATA_66;
  676. itdev->want[0][1] = ATA_ANY;
  677. itdev->want[1][1] = ATA_ANY;
  678. itdev->last_device = -1;
  679. if (pdev->revision == 0x10) {
  680. itdev->timing10 = 1;
  681. /* Need to disable ATAPI DMA for this case */
  682. if (!itdev->smart)
  683. printk(KERN_WARNING DRV_NAME": Revision 0x10, workarounds activated.\n");
  684. }
  685. return 0;
  686. }
  687. /**
  688. * it821x_rdc_cable - Cable detect for RDC1010
  689. * @ap: port we are checking
  690. *
  691. * Return the RDC1010 cable type. Unlike the IT821x we know how to do
  692. * this and can do host side cable detect
  693. */
  694. static int it821x_rdc_cable(struct ata_port *ap)
  695. {
  696. u16 r40;
  697. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  698. pci_read_config_word(pdev, 0x40, &r40);
  699. if (r40 & (1 << (2 + ap->port_no)))
  700. return ATA_CBL_PATA40;
  701. return ATA_CBL_PATA80;
  702. }
  703. static struct scsi_host_template it821x_sht = {
  704. ATA_BMDMA_SHT(DRV_NAME),
  705. };
  706. static struct ata_port_operations it821x_smart_port_ops = {
  707. .inherits = &ata_bmdma_port_ops,
  708. .check_atapi_dma= it821x_check_atapi_dma,
  709. .qc_issue = it821x_smart_qc_issue,
  710. .cable_detect = ata_cable_80wire,
  711. .set_mode = it821x_smart_set_mode,
  712. .dev_config = it821x_dev_config,
  713. .read_id = it821x_read_id,
  714. .port_start = it821x_port_start,
  715. };
  716. static struct ata_port_operations it821x_passthru_port_ops = {
  717. .inherits = &ata_bmdma_port_ops,
  718. .check_atapi_dma= it821x_check_atapi_dma,
  719. .sff_dev_select = it821x_passthru_dev_select,
  720. .bmdma_start = it821x_passthru_bmdma_start,
  721. .bmdma_stop = it821x_passthru_bmdma_stop,
  722. .qc_issue = it821x_passthru_qc_issue,
  723. .cable_detect = ata_cable_unknown,
  724. .set_piomode = it821x_passthru_set_piomode,
  725. .set_dmamode = it821x_passthru_set_dmamode,
  726. .port_start = it821x_port_start,
  727. };
  728. static struct ata_port_operations it821x_rdc_port_ops = {
  729. .inherits = &ata_bmdma_port_ops,
  730. .check_atapi_dma= it821x_check_atapi_dma,
  731. .sff_dev_select = it821x_passthru_dev_select,
  732. .bmdma_start = it821x_passthru_bmdma_start,
  733. .bmdma_stop = it821x_passthru_bmdma_stop,
  734. .qc_issue = it821x_passthru_qc_issue,
  735. .cable_detect = it821x_rdc_cable,
  736. .set_piomode = it821x_passthru_set_piomode,
  737. .set_dmamode = it821x_passthru_set_dmamode,
  738. .port_start = it821x_port_start,
  739. };
  740. static void it821x_disable_raid(struct pci_dev *pdev)
  741. {
  742. /* Neither the RDC nor the IT8211 */
  743. if (pdev->vendor != PCI_VENDOR_ID_ITE ||
  744. pdev->device != PCI_DEVICE_ID_ITE_8212)
  745. return;
  746. /* Reset local CPU, and set BIOS not ready */
  747. pci_write_config_byte(pdev, 0x5E, 0x01);
  748. /* Set to bypass mode, and reset PCI bus */
  749. pci_write_config_byte(pdev, 0x50, 0x00);
  750. pci_write_config_word(pdev, PCI_COMMAND,
  751. PCI_COMMAND_PARITY | PCI_COMMAND_IO |
  752. PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
  753. pci_write_config_word(pdev, 0x40, 0xA0F3);
  754. pci_write_config_dword(pdev,0x4C, 0x02040204);
  755. pci_write_config_byte(pdev, 0x42, 0x36);
  756. pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0x20);
  757. }
  758. static int it821x_init_one(struct pci_dev *pdev, const struct pci_device_id *id)
  759. {
  760. u8 conf;
  761. static const struct ata_port_info info_smart = {
  762. .flags = ATA_FLAG_SLAVE_POSS,
  763. .pio_mask = 0x1f,
  764. .mwdma_mask = 0x07,
  765. .udma_mask = ATA_UDMA6,
  766. .port_ops = &it821x_smart_port_ops
  767. };
  768. static const struct ata_port_info info_passthru = {
  769. .flags = ATA_FLAG_SLAVE_POSS,
  770. .pio_mask = 0x1f,
  771. .mwdma_mask = 0x07,
  772. .udma_mask = ATA_UDMA6,
  773. .port_ops = &it821x_passthru_port_ops
  774. };
  775. static const struct ata_port_info info_rdc = {
  776. .flags = ATA_FLAG_SLAVE_POSS,
  777. .pio_mask = 0x1f,
  778. .mwdma_mask = 0x07,
  779. /* No UDMA */
  780. .port_ops = &it821x_rdc_port_ops
  781. };
  782. const struct ata_port_info *ppi[] = { NULL, NULL };
  783. static char *mode[2] = { "pass through", "smart" };
  784. int rc;
  785. rc = pcim_enable_device(pdev);
  786. if (rc)
  787. return rc;
  788. if (pdev->vendor == PCI_VENDOR_ID_RDC) {
  789. ppi[0] = &info_rdc;
  790. } else {
  791. /* Force the card into bypass mode if so requested */
  792. if (it8212_noraid) {
  793. printk(KERN_INFO DRV_NAME ": forcing bypass mode.\n");
  794. it821x_disable_raid(pdev);
  795. }
  796. pci_read_config_byte(pdev, 0x50, &conf);
  797. conf &= 1;
  798. printk(KERN_INFO DRV_NAME": controller in %s mode.\n",
  799. mode[conf]);
  800. if (conf == 0)
  801. ppi[0] = &info_passthru;
  802. else
  803. ppi[0] = &info_smart;
  804. }
  805. return ata_pci_sff_init_one(pdev, ppi, &it821x_sht, NULL);
  806. }
  807. #ifdef CONFIG_PM
  808. static int it821x_reinit_one(struct pci_dev *pdev)
  809. {
  810. struct ata_host *host = dev_get_drvdata(&pdev->dev);
  811. int rc;
  812. rc = ata_pci_device_do_resume(pdev);
  813. if (rc)
  814. return rc;
  815. /* Resume - turn raid back off if need be */
  816. if (it8212_noraid)
  817. it821x_disable_raid(pdev);
  818. ata_host_resume(host);
  819. return rc;
  820. }
  821. #endif
  822. static const struct pci_device_id it821x[] = {
  823. { PCI_VDEVICE(ITE, PCI_DEVICE_ID_ITE_8211), },
  824. { PCI_VDEVICE(ITE, PCI_DEVICE_ID_ITE_8212), },
  825. { PCI_VDEVICE(RDC, 0x1010), },
  826. { },
  827. };
  828. static struct pci_driver it821x_pci_driver = {
  829. .name = DRV_NAME,
  830. .id_table = it821x,
  831. .probe = it821x_init_one,
  832. .remove = ata_pci_remove_one,
  833. #ifdef CONFIG_PM
  834. .suspend = ata_pci_device_suspend,
  835. .resume = it821x_reinit_one,
  836. #endif
  837. };
  838. static int __init it821x_init(void)
  839. {
  840. return pci_register_driver(&it821x_pci_driver);
  841. }
  842. static void __exit it821x_exit(void)
  843. {
  844. pci_unregister_driver(&it821x_pci_driver);
  845. }
  846. MODULE_AUTHOR("Alan Cox");
  847. MODULE_DESCRIPTION("low-level driver for the IT8211/IT8212 IDE RAID controller");
  848. MODULE_LICENSE("GPL");
  849. MODULE_DEVICE_TABLE(pci, it821x);
  850. MODULE_VERSION(DRV_VERSION);
  851. module_param_named(noraid, it8212_noraid, int, S_IRUGO);
  852. MODULE_PARM_DESC(noraid, "Force card into bypass mode");
  853. module_init(it821x_init);
  854. module_exit(it821x_exit);