pata_it821x.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997
  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/init.h>
  75. #include <linux/blkdev.h>
  76. #include <linux/delay.h>
  77. #include <linux/slab.h>
  78. #include <scsi/scsi_host.h>
  79. #include <linux/libata.h>
  80. #define DRV_NAME "pata_it821x"
  81. #define DRV_VERSION "0.4.2"
  82. struct it821x_dev
  83. {
  84. unsigned int smart:1, /* Are we in smart raid mode */
  85. timing10:1; /* Rev 0x10 */
  86. u8 clock_mode; /* 0, ATA_50 or ATA_66 */
  87. u8 want[2][2]; /* Mode/Pri log for master slave */
  88. /* We need these for switching the clock when DMA goes on/off
  89. The high byte is the 66Mhz timing */
  90. u16 pio[2]; /* Cached PIO values */
  91. u16 mwdma[2]; /* Cached MWDMA values */
  92. u16 udma[2]; /* Cached UDMA values (per drive) */
  93. u16 last_device; /* Master or slave loaded ? */
  94. };
  95. #define ATA_66 0
  96. #define ATA_50 1
  97. #define ATA_ANY 2
  98. #define UDMA_OFF 0
  99. #define MWDMA_OFF 0
  100. /*
  101. * We allow users to force the card into non raid mode without
  102. * flashing the alternative BIOS. This is also necessary right now
  103. * for embedded platforms that cannot run a PC BIOS but are using this
  104. * device.
  105. */
  106. static int it8212_noraid;
  107. /**
  108. * it821x_program - program the PIO/MWDMA registers
  109. * @ap: ATA port
  110. * @adev: Device to program
  111. * @timing: Timing value (66Mhz in top 8bits, 50 in the low 8)
  112. *
  113. * Program the PIO/MWDMA timing for this channel according to the
  114. * current clock. These share the same register so are managed by
  115. * the DMA start/stop sequence as with the old driver.
  116. */
  117. static void it821x_program(struct ata_port *ap, struct ata_device *adev, u16 timing)
  118. {
  119. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  120. struct it821x_dev *itdev = ap->private_data;
  121. int channel = ap->port_no;
  122. u8 conf;
  123. /* Program PIO/MWDMA timing bits */
  124. if (itdev->clock_mode == ATA_66)
  125. conf = timing >> 8;
  126. else
  127. conf = timing & 0xFF;
  128. pci_write_config_byte(pdev, 0x54 + 4 * channel, conf);
  129. }
  130. /**
  131. * it821x_program_udma - program the UDMA registers
  132. * @ap: ATA port
  133. * @adev: ATA device to update
  134. * @timing: Timing bits. Top 8 are for 66Mhz bottom for 50Mhz
  135. *
  136. * Program the UDMA timing for this drive according to the
  137. * current clock. Handles the dual clocks and also knows about
  138. * the errata on the 0x10 revision. The UDMA errata is partly handled
  139. * here and partly in start_dma.
  140. */
  141. static void it821x_program_udma(struct ata_port *ap, struct ata_device *adev, u16 timing)
  142. {
  143. struct it821x_dev *itdev = ap->private_data;
  144. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  145. int channel = ap->port_no;
  146. int unit = adev->devno;
  147. u8 conf;
  148. /* Program UDMA timing bits */
  149. if (itdev->clock_mode == ATA_66)
  150. conf = timing >> 8;
  151. else
  152. conf = timing & 0xFF;
  153. if (itdev->timing10 == 0)
  154. pci_write_config_byte(pdev, 0x56 + 4 * channel + unit, conf);
  155. else {
  156. /* Early revision must be programmed for both together */
  157. pci_write_config_byte(pdev, 0x56 + 4 * channel, conf);
  158. pci_write_config_byte(pdev, 0x56 + 4 * channel + 1, conf);
  159. }
  160. }
  161. /**
  162. * it821x_clock_strategy
  163. * @ap: ATA interface
  164. * @adev: ATA device being updated
  165. *
  166. * Select between the 50 and 66Mhz base clocks to get the best
  167. * results for this interface.
  168. */
  169. static void it821x_clock_strategy(struct ata_port *ap, struct ata_device *adev)
  170. {
  171. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  172. struct it821x_dev *itdev = ap->private_data;
  173. u8 unit = adev->devno;
  174. struct ata_device *pair = ata_dev_pair(adev);
  175. int clock, altclock;
  176. u8 v;
  177. int sel = 0;
  178. /* Look for the most wanted clocking */
  179. if (itdev->want[0][0] > itdev->want[1][0]) {
  180. clock = itdev->want[0][1];
  181. altclock = itdev->want[1][1];
  182. } else {
  183. clock = itdev->want[1][1];
  184. altclock = itdev->want[0][1];
  185. }
  186. /* Master doesn't care does the slave ? */
  187. if (clock == ATA_ANY)
  188. clock = altclock;
  189. /* Nobody cares - keep the same clock */
  190. if (clock == ATA_ANY)
  191. return;
  192. /* No change */
  193. if (clock == itdev->clock_mode)
  194. return;
  195. /* Load this into the controller */
  196. if (clock == ATA_66)
  197. itdev->clock_mode = ATA_66;
  198. else {
  199. itdev->clock_mode = ATA_50;
  200. sel = 1;
  201. }
  202. pci_read_config_byte(pdev, 0x50, &v);
  203. v &= ~(1 << (1 + ap->port_no));
  204. v |= sel << (1 + ap->port_no);
  205. pci_write_config_byte(pdev, 0x50, v);
  206. /*
  207. * Reprogram the UDMA/PIO of the pair drive for the switch
  208. * MWDMA will be dealt with by the dma switcher
  209. */
  210. if (pair && itdev->udma[1-unit] != UDMA_OFF) {
  211. it821x_program_udma(ap, pair, itdev->udma[1-unit]);
  212. it821x_program(ap, pair, itdev->pio[1-unit]);
  213. }
  214. /*
  215. * Reprogram the UDMA/PIO of our drive for the switch.
  216. * MWDMA will be dealt with by the dma switcher
  217. */
  218. if (itdev->udma[unit] != UDMA_OFF) {
  219. it821x_program_udma(ap, adev, itdev->udma[unit]);
  220. it821x_program(ap, adev, itdev->pio[unit]);
  221. }
  222. }
  223. /**
  224. * it821x_passthru_set_piomode - set PIO mode data
  225. * @ap: ATA interface
  226. * @adev: ATA device
  227. *
  228. * Configure for PIO mode. This is complicated as the register is
  229. * shared by PIO and MWDMA and for both channels.
  230. */
  231. static void it821x_passthru_set_piomode(struct ata_port *ap, struct ata_device *adev)
  232. {
  233. /* Spec says 89 ref driver uses 88 */
  234. static const u16 pio[] = { 0xAA88, 0xA382, 0xA181, 0x3332, 0x3121 };
  235. static const u8 pio_want[] = { ATA_66, ATA_66, ATA_66, ATA_66, ATA_ANY };
  236. struct it821x_dev *itdev = ap->private_data;
  237. int unit = adev->devno;
  238. int mode_wanted = adev->pio_mode - XFER_PIO_0;
  239. /* We prefer 66Mhz clock for PIO 0-3, don't care for PIO4 */
  240. itdev->want[unit][1] = pio_want[mode_wanted];
  241. itdev->want[unit][0] = 1; /* PIO is lowest priority */
  242. itdev->pio[unit] = pio[mode_wanted];
  243. it821x_clock_strategy(ap, adev);
  244. it821x_program(ap, adev, itdev->pio[unit]);
  245. }
  246. /**
  247. * it821x_passthru_set_dmamode - set initial DMA mode data
  248. * @ap: ATA interface
  249. * @adev: ATA device
  250. *
  251. * Set up the DMA modes. The actions taken depend heavily on the mode
  252. * to use. If UDMA is used as is hopefully the usual case then the
  253. * timing register is private and we need only consider the clock. If
  254. * we are using MWDMA then we have to manage the setting ourself as
  255. * we switch devices and mode.
  256. */
  257. static void it821x_passthru_set_dmamode(struct ata_port *ap, struct ata_device *adev)
  258. {
  259. static const u16 dma[] = { 0x8866, 0x3222, 0x3121 };
  260. static const u8 mwdma_want[] = { ATA_ANY, ATA_66, ATA_ANY };
  261. static const u16 udma[] = { 0x4433, 0x4231, 0x3121, 0x2121, 0x1111, 0x2211, 0x1111 };
  262. static const u8 udma_want[] = { ATA_ANY, ATA_50, ATA_ANY, ATA_66, ATA_66, ATA_50, ATA_66 };
  263. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  264. struct it821x_dev *itdev = ap->private_data;
  265. int channel = ap->port_no;
  266. int unit = adev->devno;
  267. u8 conf;
  268. if (adev->dma_mode >= XFER_UDMA_0) {
  269. int mode_wanted = adev->dma_mode - XFER_UDMA_0;
  270. itdev->want[unit][1] = udma_want[mode_wanted];
  271. itdev->want[unit][0] = 3; /* UDMA is high priority */
  272. itdev->mwdma[unit] = MWDMA_OFF;
  273. itdev->udma[unit] = udma[mode_wanted];
  274. if (mode_wanted >= 5)
  275. itdev->udma[unit] |= 0x8080; /* UDMA 5/6 select on */
  276. /* UDMA on. Again revision 0x10 must do the pair */
  277. pci_read_config_byte(pdev, 0x50, &conf);
  278. if (itdev->timing10)
  279. conf &= channel ? 0x9F: 0xE7;
  280. else
  281. conf &= ~ (1 << (3 + 2 * channel + unit));
  282. pci_write_config_byte(pdev, 0x50, conf);
  283. it821x_clock_strategy(ap, adev);
  284. it821x_program_udma(ap, adev, itdev->udma[unit]);
  285. } else {
  286. int mode_wanted = adev->dma_mode - XFER_MW_DMA_0;
  287. itdev->want[unit][1] = mwdma_want[mode_wanted];
  288. itdev->want[unit][0] = 2; /* MWDMA is low priority */
  289. itdev->mwdma[unit] = dma[mode_wanted];
  290. itdev->udma[unit] = UDMA_OFF;
  291. /* UDMA bits off - Revision 0x10 do them in pairs */
  292. pci_read_config_byte(pdev, 0x50, &conf);
  293. if (itdev->timing10)
  294. conf |= channel ? 0x60: 0x18;
  295. else
  296. conf |= 1 << (3 + 2 * channel + unit);
  297. pci_write_config_byte(pdev, 0x50, conf);
  298. it821x_clock_strategy(ap, adev);
  299. }
  300. }
  301. /**
  302. * it821x_passthru_dma_start - DMA start callback
  303. * @qc: Command in progress
  304. *
  305. * Usually drivers set the DMA timing at the point the set_dmamode call
  306. * is made. IT821x however requires we load new timings on the
  307. * transitions in some cases.
  308. */
  309. static void it821x_passthru_bmdma_start(struct ata_queued_cmd *qc)
  310. {
  311. struct ata_port *ap = qc->ap;
  312. struct ata_device *adev = qc->dev;
  313. struct it821x_dev *itdev = ap->private_data;
  314. int unit = adev->devno;
  315. if (itdev->mwdma[unit] != MWDMA_OFF)
  316. it821x_program(ap, adev, itdev->mwdma[unit]);
  317. else if (itdev->udma[unit] != UDMA_OFF && itdev->timing10)
  318. it821x_program_udma(ap, adev, itdev->udma[unit]);
  319. ata_bmdma_start(qc);
  320. }
  321. /**
  322. * it821x_passthru_dma_stop - DMA stop callback
  323. * @qc: ATA command
  324. *
  325. * We loaded new timings in dma_start, as a result we need to restore
  326. * the PIO timings in dma_stop so that the next command issue gets the
  327. * right clock values.
  328. */
  329. static void it821x_passthru_bmdma_stop(struct ata_queued_cmd *qc)
  330. {
  331. struct ata_port *ap = qc->ap;
  332. struct ata_device *adev = qc->dev;
  333. struct it821x_dev *itdev = ap->private_data;
  334. int unit = adev->devno;
  335. ata_bmdma_stop(qc);
  336. if (itdev->mwdma[unit] != MWDMA_OFF)
  337. it821x_program(ap, adev, itdev->pio[unit]);
  338. }
  339. /**
  340. * it821x_passthru_dev_select - Select master/slave
  341. * @ap: ATA port
  342. * @device: Device number (not pointer)
  343. *
  344. * Device selection hook. If necessary perform clock switching
  345. */
  346. static void it821x_passthru_dev_select(struct ata_port *ap,
  347. unsigned int device)
  348. {
  349. struct it821x_dev *itdev = ap->private_data;
  350. if (itdev && device != itdev->last_device) {
  351. struct ata_device *adev = &ap->link.device[device];
  352. it821x_program(ap, adev, itdev->pio[adev->devno]);
  353. itdev->last_device = device;
  354. }
  355. ata_sff_dev_select(ap, device);
  356. }
  357. /**
  358. * it821x_smart_qc_issue - wrap qc issue prot
  359. * @qc: command
  360. *
  361. * Wrap the command issue sequence for the IT821x. We need to
  362. * perform out own device selection timing loads before the
  363. * usual happenings kick off
  364. */
  365. static unsigned int it821x_smart_qc_issue(struct ata_queued_cmd *qc)
  366. {
  367. switch(qc->tf.command)
  368. {
  369. /* Commands the firmware supports */
  370. case ATA_CMD_READ:
  371. case ATA_CMD_READ_EXT:
  372. case ATA_CMD_WRITE:
  373. case ATA_CMD_WRITE_EXT:
  374. case ATA_CMD_PIO_READ:
  375. case ATA_CMD_PIO_READ_EXT:
  376. case ATA_CMD_PIO_WRITE:
  377. case ATA_CMD_PIO_WRITE_EXT:
  378. case ATA_CMD_READ_MULTI:
  379. case ATA_CMD_READ_MULTI_EXT:
  380. case ATA_CMD_WRITE_MULTI:
  381. case ATA_CMD_WRITE_MULTI_EXT:
  382. case ATA_CMD_ID_ATA:
  383. case ATA_CMD_INIT_DEV_PARAMS:
  384. case 0xFC: /* Internal 'report rebuild state' */
  385. /* Arguably should just no-op this one */
  386. case ATA_CMD_SET_FEATURES:
  387. return ata_bmdma_qc_issue(qc);
  388. }
  389. printk(KERN_DEBUG "it821x: can't process command 0x%02X\n", 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. ata_dev_info(adev, "%sRAID%d volume",
  456. adev->id[147] ? "Bootable " : "",
  457. adev->id[129]);
  458. if (adev->id[129] != 1)
  459. pr_cont("(%dK stripe)", adev->id[146]);
  460. pr_cont("\n");
  461. }
  462. /* This is a controller firmware triggered funny, don't
  463. report the drive faulty! */
  464. adev->horkage &= ~ATA_HORKAGE_DIAGNOSTIC;
  465. /* No HPA in 'smart' mode */
  466. adev->horkage |= ATA_HORKAGE_BROKEN_HPA;
  467. }
  468. /**
  469. * it821x_read_id - Hack identify data up
  470. * @adev: device to read
  471. * @tf: proposed taskfile
  472. * @id: buffer for returned ident data
  473. *
  474. * Query the devices on this firmware driven port and slightly
  475. * mash the identify data to stop us and common tools trying to
  476. * use features not firmware supported. The firmware itself does
  477. * some masking (eg SMART) but not enough.
  478. */
  479. static unsigned int it821x_read_id(struct ata_device *adev,
  480. struct ata_taskfile *tf, u16 *id)
  481. {
  482. unsigned int err_mask;
  483. unsigned char model_num[ATA_ID_PROD_LEN + 1];
  484. err_mask = ata_do_dev_read_id(adev, tf, id);
  485. if (err_mask)
  486. return err_mask;
  487. ata_id_c_string(id, model_num, ATA_ID_PROD, sizeof(model_num));
  488. id[83] &= ~(1 << 12); /* Cache flush is firmware handled */
  489. id[83] &= ~(1 << 13); /* Ditto for LBA48 flushes */
  490. id[84] &= ~(1 << 6); /* No FUA */
  491. id[85] &= ~(1 << 10); /* No HPA */
  492. id[76] = 0; /* No NCQ/AN etc */
  493. if (strstr(model_num, "Integrated Technology Express")) {
  494. /* Set feature bits the firmware neglects */
  495. id[49] |= 0x0300; /* LBA, DMA */
  496. id[83] &= 0x7FFF;
  497. id[83] |= 0x4400; /* Word 83 is valid and LBA48 */
  498. id[86] |= 0x0400; /* LBA48 on */
  499. id[ATA_ID_MAJOR_VER] |= 0x1F;
  500. /* Clear the serial number because it's different each boot
  501. which breaks validation on resume */
  502. memset(&id[ATA_ID_SERNO], 0x20, ATA_ID_SERNO_LEN);
  503. }
  504. return err_mask;
  505. }
  506. /**
  507. * it821x_check_atapi_dma - ATAPI DMA handler
  508. * @qc: Command we are about to issue
  509. *
  510. * Decide if this ATAPI command can be issued by DMA on this
  511. * controller. Return 0 if it can be.
  512. */
  513. static int it821x_check_atapi_dma(struct ata_queued_cmd *qc)
  514. {
  515. struct ata_port *ap = qc->ap;
  516. struct it821x_dev *itdev = ap->private_data;
  517. /* Only use dma for transfers to/from the media. */
  518. if (ata_qc_raw_nbytes(qc) < 2048)
  519. return -EOPNOTSUPP;
  520. /* No ATAPI DMA in smart mode */
  521. if (itdev->smart)
  522. return -EOPNOTSUPP;
  523. /* No ATAPI DMA on rev 10 */
  524. if (itdev->timing10)
  525. return -EOPNOTSUPP;
  526. /* Cool */
  527. return 0;
  528. }
  529. /**
  530. * it821x_display_disk - display disk setup
  531. * @n: Device number
  532. * @buf: Buffer block from firmware
  533. *
  534. * Produce a nice informative display of the device setup as provided
  535. * by the firmware.
  536. */
  537. static void it821x_display_disk(int n, u8 *buf)
  538. {
  539. unsigned char id[41];
  540. int mode = 0;
  541. char *mtype = "";
  542. char mbuf[8];
  543. char *cbl = "(40 wire cable)";
  544. static const char *types[5] = {
  545. "RAID0", "RAID1", "RAID 0+1", "JBOD", "DISK"
  546. };
  547. if (buf[52] > 4) /* No Disk */
  548. return;
  549. ata_id_c_string((u16 *)buf, id, 0, 41);
  550. if (buf[51]) {
  551. mode = ffs(buf[51]);
  552. mtype = "UDMA";
  553. } else if (buf[49]) {
  554. mode = ffs(buf[49]);
  555. mtype = "MWDMA";
  556. }
  557. if (buf[76])
  558. cbl = "";
  559. if (mode)
  560. snprintf(mbuf, 8, "%5s%d", mtype, mode - 1);
  561. else
  562. strcpy(mbuf, "PIO");
  563. if (buf[52] == 4)
  564. printk(KERN_INFO "%d: %-6s %-8s %s %s\n",
  565. n, mbuf, types[buf[52]], id, cbl);
  566. else
  567. printk(KERN_INFO "%d: %-6s %-8s Volume: %1d %s %s\n",
  568. n, mbuf, types[buf[52]], buf[53], id, cbl);
  569. if (buf[125] < 100)
  570. printk(KERN_INFO "%d: Rebuilding: %d%%\n", n, buf[125]);
  571. }
  572. /**
  573. * it821x_firmware_command - issue firmware command
  574. * @ap: IT821x port to interrogate
  575. * @cmd: command
  576. * @len: length
  577. *
  578. * Issue firmware commands expecting data back from the controller. We
  579. * use this to issue commands that do not go via the normal paths. Other
  580. * commands such as 0xFC can be issued normally.
  581. */
  582. static u8 *it821x_firmware_command(struct ata_port *ap, u8 cmd, int len)
  583. {
  584. u8 status;
  585. int n = 0;
  586. u16 *buf = kmalloc(len, GFP_KERNEL);
  587. if (buf == NULL) {
  588. printk(KERN_ERR "it821x_firmware_command: Out of memory\n");
  589. return NULL;
  590. }
  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. printk(KERN_ERR "it821x_firmware_command: rejected\n");
  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. mdelay(1);
  613. }
  614. kfree(buf);
  615. printk(KERN_ERR "it821x_firmware_command: timeout\n");
  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. printk(KERN_INFO "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(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. printk(KERN_WARNING DRV_NAME": Revision 0x10, workarounds activated.\n");
  686. }
  687. return 0;
  688. }
  689. /**
  690. * it821x_rdc_cable - Cable detect for RDC1010
  691. * @ap: port we are checking
  692. *
  693. * Return the RDC1010 cable type. Unlike the IT821x we know how to do
  694. * this and can do host side cable detect
  695. */
  696. static int it821x_rdc_cable(struct ata_port *ap)
  697. {
  698. u16 r40;
  699. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  700. pci_read_config_word(pdev, 0x40, &r40);
  701. if (r40 & (1 << (2 + ap->port_no)))
  702. return ATA_CBL_PATA40;
  703. return ATA_CBL_PATA80;
  704. }
  705. static struct scsi_host_template it821x_sht = {
  706. ATA_BMDMA_SHT(DRV_NAME),
  707. };
  708. static struct ata_port_operations it821x_smart_port_ops = {
  709. .inherits = &ata_bmdma_port_ops,
  710. .check_atapi_dma= it821x_check_atapi_dma,
  711. .qc_issue = it821x_smart_qc_issue,
  712. .cable_detect = ata_cable_80wire,
  713. .set_mode = it821x_smart_set_mode,
  714. .dev_config = it821x_dev_config,
  715. .read_id = it821x_read_id,
  716. .port_start = it821x_port_start,
  717. };
  718. static struct ata_port_operations it821x_passthru_port_ops = {
  719. .inherits = &ata_bmdma_port_ops,
  720. .check_atapi_dma= it821x_check_atapi_dma,
  721. .sff_dev_select = it821x_passthru_dev_select,
  722. .bmdma_start = it821x_passthru_bmdma_start,
  723. .bmdma_stop = it821x_passthru_bmdma_stop,
  724. .qc_issue = it821x_passthru_qc_issue,
  725. .cable_detect = ata_cable_unknown,
  726. .set_piomode = it821x_passthru_set_piomode,
  727. .set_dmamode = it821x_passthru_set_dmamode,
  728. .port_start = it821x_port_start,
  729. };
  730. static struct ata_port_operations it821x_rdc_port_ops = {
  731. .inherits = &ata_bmdma_port_ops,
  732. .check_atapi_dma= it821x_check_atapi_dma,
  733. .sff_dev_select = it821x_passthru_dev_select,
  734. .bmdma_start = it821x_passthru_bmdma_start,
  735. .bmdma_stop = it821x_passthru_bmdma_stop,
  736. .qc_issue = it821x_passthru_qc_issue,
  737. .cable_detect = it821x_rdc_cable,
  738. .set_piomode = it821x_passthru_set_piomode,
  739. .set_dmamode = it821x_passthru_set_dmamode,
  740. .port_start = it821x_port_start,
  741. };
  742. static void it821x_disable_raid(struct pci_dev *pdev)
  743. {
  744. /* Neither the RDC nor the IT8211 */
  745. if (pdev->vendor != PCI_VENDOR_ID_ITE ||
  746. pdev->device != PCI_DEVICE_ID_ITE_8212)
  747. return;
  748. /* Reset local CPU, and set BIOS not ready */
  749. pci_write_config_byte(pdev, 0x5E, 0x01);
  750. /* Set to bypass mode, and reset PCI bus */
  751. pci_write_config_byte(pdev, 0x50, 0x00);
  752. pci_write_config_word(pdev, PCI_COMMAND,
  753. PCI_COMMAND_PARITY | PCI_COMMAND_IO |
  754. PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
  755. pci_write_config_word(pdev, 0x40, 0xA0F3);
  756. pci_write_config_dword(pdev,0x4C, 0x02040204);
  757. pci_write_config_byte(pdev, 0x42, 0x36);
  758. pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0x20);
  759. }
  760. static int it821x_init_one(struct pci_dev *pdev, const struct pci_device_id *id)
  761. {
  762. u8 conf;
  763. static const struct ata_port_info info_smart = {
  764. .flags = ATA_FLAG_SLAVE_POSS,
  765. .pio_mask = ATA_PIO4,
  766. .mwdma_mask = ATA_MWDMA2,
  767. .udma_mask = ATA_UDMA6,
  768. .port_ops = &it821x_smart_port_ops
  769. };
  770. static const struct ata_port_info info_passthru = {
  771. .flags = ATA_FLAG_SLAVE_POSS,
  772. .pio_mask = ATA_PIO4,
  773. .mwdma_mask = ATA_MWDMA2,
  774. .udma_mask = ATA_UDMA6,
  775. .port_ops = &it821x_passthru_port_ops
  776. };
  777. static const struct ata_port_info info_rdc = {
  778. .flags = ATA_FLAG_SLAVE_POSS,
  779. .pio_mask = ATA_PIO4,
  780. .mwdma_mask = ATA_MWDMA2,
  781. .udma_mask = ATA_UDMA6,
  782. .port_ops = &it821x_rdc_port_ops
  783. };
  784. static const struct ata_port_info info_rdc_11 = {
  785. .flags = ATA_FLAG_SLAVE_POSS,
  786. .pio_mask = ATA_PIO4,
  787. .mwdma_mask = ATA_MWDMA2,
  788. /* No UDMA */
  789. .port_ops = &it821x_rdc_port_ops
  790. };
  791. const struct ata_port_info *ppi[] = { NULL, NULL };
  792. static char *mode[2] = { "pass through", "smart" };
  793. int rc;
  794. rc = pcim_enable_device(pdev);
  795. if (rc)
  796. return rc;
  797. if (pdev->vendor == PCI_VENDOR_ID_RDC) {
  798. /* Deal with Vortex86SX */
  799. if (pdev->revision == 0x11)
  800. ppi[0] = &info_rdc_11;
  801. else
  802. ppi[0] = &info_rdc;
  803. } else {
  804. /* Force the card into bypass mode if so requested */
  805. if (it8212_noraid) {
  806. printk(KERN_INFO DRV_NAME ": forcing bypass mode.\n");
  807. it821x_disable_raid(pdev);
  808. }
  809. pci_read_config_byte(pdev, 0x50, &conf);
  810. conf &= 1;
  811. printk(KERN_INFO DRV_NAME": controller in %s mode.\n",
  812. 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
  821. static int it821x_reinit_one(struct pci_dev *pdev)
  822. {
  823. struct ata_host *host = dev_get_drvdata(&pdev->dev);
  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
  847. .suspend = ata_pci_device_suspend,
  848. .resume = it821x_reinit_one,
  849. #endif
  850. };
  851. static int __init it821x_init(void)
  852. {
  853. return pci_register_driver(&it821x_pci_driver);
  854. }
  855. static void __exit it821x_exit(void)
  856. {
  857. pci_unregister_driver(&it821x_pci_driver);
  858. }
  859. MODULE_AUTHOR("Alan Cox");
  860. MODULE_DESCRIPTION("low-level driver for the IT8211/IT8212 IDE RAID controller");
  861. MODULE_LICENSE("GPL");
  862. MODULE_DEVICE_TABLE(pci, it821x);
  863. MODULE_VERSION(DRV_VERSION);
  864. module_param_named(noraid, it8212_noraid, int, S_IRUGO);
  865. MODULE_PARM_DESC(noraid, "Force card into bypass mode");
  866. module_init(it821x_init);
  867. module_exit(it821x_exit);