pata_it821x.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  1. /*
  2. * ata-it821x.c - IT821x PATA for new ATA layer
  3. * (C) 2005 Red Hat Inc
  4. * Alan Cox <alan@redhat.com>
  5. *
  6. * based upon
  7. *
  8. * it821x.c
  9. *
  10. * linux/drivers/ide/pci/it821x.c Version 0.09 December 2004
  11. *
  12. * Copyright (C) 2004 Red Hat <alan@redhat.com>
  13. *
  14. * May be copied or modified under the terms of the GNU General Public License
  15. * Based in part on the ITE vendor provided SCSI driver.
  16. *
  17. * Documentation available from
  18. * http://www.ite.com.tw/pc/IT8212F_V04.pdf
  19. * Some other documents are NDA.
  20. *
  21. * The ITE8212 isn't exactly a standard IDE controller. It has two
  22. * modes. In pass through mode then it is an IDE controller. In its smart
  23. * mode its actually quite a capable hardware raid controller disguised
  24. * as an IDE controller. Smart mode only understands DMA read/write and
  25. * identify, none of the fancier commands apply. The IT8211 is identical
  26. * in other respects but lacks the raid mode.
  27. *
  28. * Errata:
  29. * o Rev 0x10 also requires master/slave hold the same DMA timings and
  30. * cannot do ATAPI MWDMA.
  31. * o The identify data for raid volumes lacks CHS info (technically ok)
  32. * but also fails to set the LBA28 and other bits. We fix these in
  33. * the IDE probe quirk code.
  34. * o If you write LBA48 sized I/O's (ie > 256 sector) in smart mode
  35. * raid then the controller firmware dies
  36. * o Smart mode without RAID doesn't clear all the necessary identify
  37. * bits to reduce the command set to the one used
  38. *
  39. * This has a few impacts on the driver
  40. * - In pass through mode we do all the work you would expect
  41. * - In smart mode the clocking set up is done by the controller generally
  42. * but we must watch the other limits and filter.
  43. * - There are a few extra vendor commands that actually talk to the
  44. * controller but only work PIO with no IRQ.
  45. *
  46. * Vendor areas of the identify block in smart mode are used for the
  47. * timing and policy set up. Each HDD in raid mode also has a serial
  48. * block on the disk. The hardware extra commands are get/set chip status,
  49. * rebuild, get rebuild status.
  50. *
  51. * In Linux the driver supports pass through mode as if the device was
  52. * just another IDE controller. If the smart mode is running then
  53. * volumes are managed by the controller firmware and each IDE "disk"
  54. * is a raid volume. Even more cute - the controller can do automated
  55. * hotplug and rebuild.
  56. *
  57. * The pass through controller itself is a little demented. It has a
  58. * flaw that it has a single set of PIO/MWDMA timings per channel so
  59. * non UDMA devices restrict each others performance. It also has a
  60. * single clock source per channel so mixed UDMA100/133 performance
  61. * isn't perfect and we have to pick a clock. Thankfully none of this
  62. * matters in smart mode. ATAPI DMA is not currently supported.
  63. *
  64. * It seems the smart mode is a win for RAID1/RAID10 but otherwise not.
  65. *
  66. * TODO
  67. * - ATAPI and other speed filtering
  68. * - Command filter in smart mode
  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.3.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 neccessary 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_pre_reset - probe
  108. * @ap: ATA port
  109. *
  110. * Set the cable type
  111. */
  112. static int it821x_pre_reset(struct ata_port *ap)
  113. {
  114. ap->cbl = ATA_CBL_PATA80;
  115. return ata_std_prereset(ap);
  116. }
  117. /**
  118. * it821x_error_handler - probe/reset
  119. * @ap: ATA port
  120. *
  121. * Set the cable type and trigger a probe
  122. */
  123. static void it821x_error_handler(struct ata_port *ap)
  124. {
  125. return ata_bmdma_drive_eh(ap, it821x_pre_reset, ata_std_softreset, NULL, ata_std_postreset);
  126. }
  127. /**
  128. * it821x_program - program the PIO/MWDMA registers
  129. * @ap: ATA port
  130. * @adev: Device to program
  131. * @timing: Timing value (66Mhz in top 8bits, 50 in the low 8)
  132. *
  133. * Program the PIO/MWDMA timing for this channel according to the
  134. * current clock. These share the same register so are managed by
  135. * the DMA start/stop sequence as with the old driver.
  136. */
  137. static void it821x_program(struct ata_port *ap, struct ata_device *adev, u16 timing)
  138. {
  139. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  140. struct it821x_dev *itdev = ap->private_data;
  141. int channel = ap->port_no;
  142. u8 conf;
  143. /* Program PIO/MWDMA timing bits */
  144. if (itdev->clock_mode == ATA_66)
  145. conf = timing >> 8;
  146. else
  147. conf = timing & 0xFF;
  148. pci_write_config_byte(pdev, 0x54 + 4 * channel, conf);
  149. }
  150. /**
  151. * it821x_program_udma - program the UDMA registers
  152. * @ap: ATA port
  153. * @adev: ATA device to update
  154. * @timing: Timing bits. Top 8 are for 66Mhz bottom for 50Mhz
  155. *
  156. * Program the UDMA timing for this drive according to the
  157. * current clock. Handles the dual clocks and also knows about
  158. * the errata on the 0x10 revision. The UDMA errata is partly handled
  159. * here and partly in start_dma.
  160. */
  161. static void it821x_program_udma(struct ata_port *ap, struct ata_device *adev, u16 timing)
  162. {
  163. struct it821x_dev *itdev = ap->private_data;
  164. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  165. int channel = ap->port_no;
  166. int unit = adev->devno;
  167. u8 conf;
  168. /* Program UDMA timing bits */
  169. if (itdev->clock_mode == ATA_66)
  170. conf = timing >> 8;
  171. else
  172. conf = timing & 0xFF;
  173. if (itdev->timing10 == 0)
  174. pci_write_config_byte(pdev, 0x56 + 4 * channel + unit, conf);
  175. else {
  176. /* Early revision must be programmed for both together */
  177. pci_write_config_byte(pdev, 0x56 + 4 * channel, conf);
  178. pci_write_config_byte(pdev, 0x56 + 4 * channel + 1, conf);
  179. }
  180. }
  181. /**
  182. * it821x_clock_strategy
  183. * @ap: ATA interface
  184. * @adev: ATA device being updated
  185. *
  186. * Select between the 50 and 66Mhz base clocks to get the best
  187. * results for this interface.
  188. */
  189. static void it821x_clock_strategy(struct ata_port *ap, struct ata_device *adev)
  190. {
  191. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  192. struct it821x_dev *itdev = ap->private_data;
  193. u8 unit = adev->devno;
  194. struct ata_device *pair = ata_dev_pair(adev);
  195. int clock, altclock;
  196. u8 v;
  197. int sel = 0;
  198. /* Look for the most wanted clocking */
  199. if (itdev->want[0][0] > itdev->want[1][0]) {
  200. clock = itdev->want[0][1];
  201. altclock = itdev->want[1][1];
  202. } else {
  203. clock = itdev->want[1][1];
  204. altclock = itdev->want[0][1];
  205. }
  206. /* Master doesn't care does the slave ? */
  207. if (clock == ATA_ANY)
  208. clock = altclock;
  209. /* Nobody cares - keep the same clock */
  210. if (clock == ATA_ANY)
  211. return;
  212. /* No change */
  213. if (clock == itdev->clock_mode)
  214. return;
  215. /* Load this into the controller */
  216. if (clock == ATA_66)
  217. itdev->clock_mode = ATA_66;
  218. else {
  219. itdev->clock_mode = ATA_50;
  220. sel = 1;
  221. }
  222. pci_read_config_byte(pdev, 0x50, &v);
  223. v &= ~(1 << (1 + ap->port_no));
  224. v |= sel << (1 + ap->port_no);
  225. pci_write_config_byte(pdev, 0x50, v);
  226. /*
  227. * Reprogram the UDMA/PIO of the pair drive for the switch
  228. * MWDMA will be dealt with by the dma switcher
  229. */
  230. if (pair && itdev->udma[1-unit] != UDMA_OFF) {
  231. it821x_program_udma(ap, pair, itdev->udma[1-unit]);
  232. it821x_program(ap, pair, itdev->pio[1-unit]);
  233. }
  234. /*
  235. * Reprogram the UDMA/PIO of our drive for the switch.
  236. * MWDMA will be dealt with by the dma switcher
  237. */
  238. if (itdev->udma[unit] != UDMA_OFF) {
  239. it821x_program_udma(ap, adev, itdev->udma[unit]);
  240. it821x_program(ap, adev, itdev->pio[unit]);
  241. }
  242. }
  243. /**
  244. * it821x_passthru_set_piomode - set PIO mode data
  245. * @ap: ATA interface
  246. * @adev: ATA device
  247. *
  248. * Configure for PIO mode. This is complicated as the register is
  249. * shared by PIO and MWDMA and for both channels.
  250. */
  251. static void it821x_passthru_set_piomode(struct ata_port *ap, struct ata_device *adev)
  252. {
  253. /* Spec says 89 ref driver uses 88 */
  254. static const u16 pio[] = { 0xAA88, 0xA382, 0xA181, 0x3332, 0x3121 };
  255. static const u8 pio_want[] = { ATA_66, ATA_66, ATA_66, ATA_66, ATA_ANY };
  256. struct it821x_dev *itdev = ap->private_data;
  257. int unit = adev->devno;
  258. int mode_wanted = adev->pio_mode - XFER_PIO_0;
  259. /* We prefer 66Mhz clock for PIO 0-3, don't care for PIO4 */
  260. itdev->want[unit][1] = pio_want[mode_wanted];
  261. itdev->want[unit][0] = 1; /* PIO is lowest priority */
  262. itdev->pio[unit] = pio[mode_wanted];
  263. it821x_clock_strategy(ap, adev);
  264. it821x_program(ap, adev, itdev->pio[unit]);
  265. }
  266. /**
  267. * it821x_passthru_set_dmamode - set initial DMA mode data
  268. * @ap: ATA interface
  269. * @adev: ATA device
  270. *
  271. * Set up the DMA modes. The actions taken depend heavily on the mode
  272. * to use. If UDMA is used as is hopefully the usual case then the
  273. * timing register is private and we need only consider the clock. If
  274. * we are using MWDMA then we have to manage the setting ourself as
  275. * we switch devices and mode.
  276. */
  277. static void it821x_passthru_set_dmamode(struct ata_port *ap, struct ata_device *adev)
  278. {
  279. static const u16 dma[] = { 0x8866, 0x3222, 0x3121 };
  280. static const u8 mwdma_want[] = { ATA_ANY, ATA_66, ATA_ANY };
  281. static const u16 udma[] = { 0x4433, 0x4231, 0x3121, 0x2121, 0x1111, 0x2211, 0x1111 };
  282. static const u8 udma_want[] = { ATA_ANY, ATA_50, ATA_ANY, ATA_66, ATA_66, ATA_50, ATA_66 };
  283. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  284. struct it821x_dev *itdev = ap->private_data;
  285. int channel = ap->port_no;
  286. int unit = adev->devno;
  287. u8 conf;
  288. if (adev->dma_mode >= XFER_UDMA_0) {
  289. int mode_wanted = adev->dma_mode - XFER_UDMA_0;
  290. itdev->want[unit][1] = udma_want[mode_wanted];
  291. itdev->want[unit][0] = 3; /* UDMA is high priority */
  292. itdev->mwdma[unit] = MWDMA_OFF;
  293. itdev->udma[unit] = udma[mode_wanted];
  294. if (mode_wanted >= 5)
  295. itdev->udma[unit] |= 0x8080; /* UDMA 5/6 select on */
  296. /* UDMA on. Again revision 0x10 must do the pair */
  297. pci_read_config_byte(pdev, 0x50, &conf);
  298. if (itdev->timing10)
  299. conf &= channel ? 0x9F: 0xE7;
  300. else
  301. conf &= ~ (1 << (3 + 2 * channel + unit));
  302. pci_write_config_byte(pdev, 0x50, conf);
  303. it821x_clock_strategy(ap, adev);
  304. it821x_program_udma(ap, adev, itdev->udma[unit]);
  305. } else {
  306. int mode_wanted = adev->dma_mode - XFER_MW_DMA_0;
  307. itdev->want[unit][1] = mwdma_want[mode_wanted];
  308. itdev->want[unit][0] = 2; /* MWDMA is low priority */
  309. itdev->mwdma[unit] = dma[mode_wanted];
  310. itdev->udma[unit] = UDMA_OFF;
  311. /* UDMA bits off - Revision 0x10 do them in pairs */
  312. pci_read_config_byte(pdev, 0x50, &conf);
  313. if (itdev->timing10)
  314. conf |= channel ? 0x60: 0x18;
  315. else
  316. conf |= 1 << (3 + 2 * channel + unit);
  317. pci_write_config_byte(pdev, 0x50, conf);
  318. it821x_clock_strategy(ap, adev);
  319. }
  320. }
  321. /**
  322. * it821x_passthru_dma_start - DMA start callback
  323. * @qc: Command in progress
  324. *
  325. * Usually drivers set the DMA timing at the point the set_dmamode call
  326. * is made. IT821x however requires we load new timings on the
  327. * transitions in some cases.
  328. */
  329. static void it821x_passthru_bmdma_start(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. if (itdev->mwdma[unit] != MWDMA_OFF)
  336. it821x_program(ap, adev, itdev->mwdma[unit]);
  337. else if (itdev->udma[unit] != UDMA_OFF && itdev->timing10)
  338. it821x_program_udma(ap, adev, itdev->udma[unit]);
  339. ata_bmdma_start(qc);
  340. }
  341. /**
  342. * it821x_passthru_dma_stop - DMA stop callback
  343. * @qc: ATA command
  344. *
  345. * We loaded new timings in dma_start, as a result we need to restore
  346. * the PIO timings in dma_stop so that the next command issue gets the
  347. * right clock values.
  348. */
  349. static void it821x_passthru_bmdma_stop(struct ata_queued_cmd *qc)
  350. {
  351. struct ata_port *ap = qc->ap;
  352. struct ata_device *adev = qc->dev;
  353. struct it821x_dev *itdev = ap->private_data;
  354. int unit = adev->devno;
  355. ata_bmdma_stop(qc);
  356. if (itdev->mwdma[unit] != MWDMA_OFF)
  357. it821x_program(ap, adev, itdev->pio[unit]);
  358. }
  359. /**
  360. * it821x_passthru_dev_select - Select master/slave
  361. * @ap: ATA port
  362. * @device: Device number (not pointer)
  363. *
  364. * Device selection hook. If neccessary perform clock switching
  365. */
  366. static void it821x_passthru_dev_select(struct ata_port *ap,
  367. unsigned int device)
  368. {
  369. struct it821x_dev *itdev = ap->private_data;
  370. if (itdev && device != itdev->last_device) {
  371. struct ata_device *adev = &ap->device[device];
  372. it821x_program(ap, adev, itdev->pio[adev->devno]);
  373. itdev->last_device = device;
  374. }
  375. ata_std_dev_select(ap, device);
  376. }
  377. /**
  378. * it821x_smart_qc_issue_prot - wrap qc issue prot
  379. * @qc: command
  380. *
  381. * Wrap the command issue sequence for the IT821x. We need to
  382. * perform out own device selection timing loads before the
  383. * usual happenings kick off
  384. */
  385. static unsigned int it821x_smart_qc_issue_prot(struct ata_queued_cmd *qc)
  386. {
  387. switch(qc->tf.command)
  388. {
  389. /* Commands the firmware supports */
  390. case ATA_CMD_READ:
  391. case ATA_CMD_READ_EXT:
  392. case ATA_CMD_WRITE:
  393. case ATA_CMD_WRITE_EXT:
  394. case ATA_CMD_PIO_READ:
  395. case ATA_CMD_PIO_READ_EXT:
  396. case ATA_CMD_PIO_WRITE:
  397. case ATA_CMD_PIO_WRITE_EXT:
  398. case ATA_CMD_READ_MULTI:
  399. case ATA_CMD_READ_MULTI_EXT:
  400. case ATA_CMD_WRITE_MULTI:
  401. case ATA_CMD_WRITE_MULTI_EXT:
  402. case ATA_CMD_ID_ATA:
  403. /* Arguably should just no-op this one */
  404. case ATA_CMD_SET_FEATURES:
  405. return ata_qc_issue_prot(qc);
  406. }
  407. printk(KERN_DEBUG "it821x: can't process command 0x%02X\n", qc->tf.command);
  408. return AC_ERR_INVALID;
  409. }
  410. /**
  411. * it821x_passthru_qc_issue_prot - wrap qc issue prot
  412. * @qc: command
  413. *
  414. * Wrap the command issue sequence for the IT821x. We need to
  415. * perform out own device selection timing loads before the
  416. * usual happenings kick off
  417. */
  418. static unsigned int it821x_passthru_qc_issue_prot(struct ata_queued_cmd *qc)
  419. {
  420. it821x_passthru_dev_select(qc->ap, qc->dev->devno);
  421. return ata_qc_issue_prot(qc);
  422. }
  423. /**
  424. * it821x_smart_set_mode - mode setting
  425. * @ap: interface to set up
  426. *
  427. * Use a non standard set_mode function. We don't want to be tuned.
  428. * The BIOS configured everything. Our job is not to fiddle. We
  429. * read the dma enabled bits from the PCI configuration of the device
  430. * and respect them.
  431. */
  432. static void it821x_smart_set_mode(struct ata_port *ap)
  433. {
  434. int dma_enabled = 0;
  435. int i;
  436. /* Bits 5 and 6 indicate if DMA is active on master/slave */
  437. /* It is possible that BMDMA isn't allocated */
  438. if (ap->ioaddr.bmdma_addr)
  439. dma_enabled = inb(ap->ioaddr.bmdma_addr + ATA_DMA_CMD);
  440. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  441. struct ata_device *dev = &ap->device[i];
  442. if (ata_dev_enabled(dev)) {
  443. /* We don't really care */
  444. dev->pio_mode = XFER_PIO_0;
  445. dev->dma_mode = XFER_MW_DMA_0;
  446. /* We do need the right mode information for DMA or PIO
  447. and this comes from the current configuration flags */
  448. if (dma_enabled & (1 << (5 + i))) {
  449. dev->xfer_mode = XFER_MW_DMA_0;
  450. dev->xfer_shift = ATA_SHIFT_MWDMA;
  451. dev->flags &= ~ATA_DFLAG_PIO;
  452. } else {
  453. dev->xfer_mode = XFER_PIO_0;
  454. dev->xfer_shift = ATA_SHIFT_PIO;
  455. dev->flags |= ATA_DFLAG_PIO;
  456. }
  457. }
  458. }
  459. }
  460. /**
  461. * it821x_dev_config - Called each device identify
  462. * @ap: ATA port
  463. * @adev: Device that has just been identified
  464. *
  465. * Perform the initial setup needed for each device that is chip
  466. * special. In our case we need to lock the sector count to avoid
  467. * blowing the brains out of the firmware with large LBA48 requests
  468. *
  469. * FIXME: When FUA appears we need to block FUA too. And SMART and
  470. * basically we need to filter commands for this chip.
  471. */
  472. static void it821x_dev_config(struct ata_port *ap, struct ata_device *adev)
  473. {
  474. unsigned char model_num[40];
  475. char *s;
  476. unsigned int len;
  477. /* This block ought to be a library routine as it is in several
  478. drivers now */
  479. ata_id_string(adev->id, model_num, ATA_ID_PROD_OFS,
  480. sizeof(model_num));
  481. s = &model_num[0];
  482. len = strnlen(s, sizeof(model_num));
  483. /* ATAPI specifies that empty space is blank-filled; remove blanks */
  484. while ((len > 0) && (s[len - 1] == ' ')) {
  485. len--;
  486. s[len] = 0;
  487. }
  488. if (adev->max_sectors > 255)
  489. adev->max_sectors = 255;
  490. if (strstr(model_num, "Integrated Technology Express")) {
  491. /* RAID mode */
  492. printk(KERN_INFO "IT821x %sRAID%d volume",
  493. adev->id[147]?"Bootable ":"",
  494. adev->id[129]);
  495. if (adev->id[129] != 1)
  496. printk("(%dK stripe)", adev->id[146]);
  497. printk(".\n");
  498. }
  499. }
  500. /**
  501. * it821x_check_atapi_dma - ATAPI DMA handler
  502. * @qc: Command we are about to issue
  503. *
  504. * Decide if this ATAPI command can be issued by DMA on this
  505. * controller. Return 0 if it can be.
  506. */
  507. static int it821x_check_atapi_dma(struct ata_queued_cmd *qc)
  508. {
  509. struct ata_port *ap = qc->ap;
  510. struct it821x_dev *itdev = ap->private_data;
  511. /* No ATAPI DMA in smart mode */
  512. if (itdev->smart)
  513. return -EOPNOTSUPP;
  514. /* No ATAPI DMA on rev 10 */
  515. if (itdev->timing10)
  516. return -EOPNOTSUPP;
  517. /* Cool */
  518. return 0;
  519. }
  520. /**
  521. * it821x_port_start - port setup
  522. * @ap: ATA port being set up
  523. *
  524. * The it821x needs to maintain private data structures and also to
  525. * use the standard PCI interface which lacks support for this
  526. * functionality. We instead set up the private data on the port
  527. * start hook, and tear it down on port stop
  528. */
  529. static int it821x_port_start(struct ata_port *ap)
  530. {
  531. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  532. struct it821x_dev *itdev;
  533. u8 conf;
  534. int ret = ata_port_start(ap);
  535. if (ret < 0)
  536. return ret;
  537. ap->private_data = kmalloc(sizeof(struct it821x_dev), GFP_KERNEL);
  538. if (ap->private_data == NULL) {
  539. ata_port_stop(ap);
  540. return -ENOMEM;
  541. }
  542. itdev = ap->private_data;
  543. memset(itdev, 0, sizeof(struct it821x_dev));
  544. pci_read_config_byte(pdev, 0x50, &conf);
  545. if (conf & 1) {
  546. itdev->smart = 1;
  547. /* Long I/O's although allowed in LBA48 space cause the
  548. onboard firmware to enter the twighlight zone */
  549. /* No ATAPI DMA in this mode either */
  550. }
  551. /* Pull the current clocks from 0x50 */
  552. if (conf & (1 << (1 + ap->port_no)))
  553. itdev->clock_mode = ATA_50;
  554. else
  555. itdev->clock_mode = ATA_66;
  556. itdev->want[0][1] = ATA_ANY;
  557. itdev->want[1][1] = ATA_ANY;
  558. itdev->last_device = -1;
  559. pci_read_config_byte(pdev, PCI_REVISION_ID, &conf);
  560. if (conf == 0x10) {
  561. itdev->timing10 = 1;
  562. /* Need to disable ATAPI DMA for this case */
  563. if (!itdev->smart)
  564. printk(KERN_WARNING DRV_NAME": Revision 0x10, workarounds activated.\n");
  565. }
  566. return 0;
  567. }
  568. /**
  569. * it821x_port_stop - port shutdown
  570. * @ap: ATA port being removed
  571. *
  572. * Release the private objects we added in it821x_port_start
  573. */
  574. static void it821x_port_stop(struct ata_port *ap) {
  575. kfree(ap->private_data);
  576. ap->private_data = NULL; /* We want an OOPS if we reuse this
  577. too late! */
  578. ata_port_stop(ap);
  579. }
  580. static struct scsi_host_template it821x_sht = {
  581. .module = THIS_MODULE,
  582. .name = DRV_NAME,
  583. .ioctl = ata_scsi_ioctl,
  584. .queuecommand = ata_scsi_queuecmd,
  585. .can_queue = ATA_DEF_QUEUE,
  586. .this_id = ATA_SHT_THIS_ID,
  587. .sg_tablesize = LIBATA_MAX_PRD,
  588. /* 255 sectors to begin with. This is locked in smart mode but not
  589. in pass through */
  590. .max_sectors = 255,
  591. .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
  592. .emulated = ATA_SHT_EMULATED,
  593. .use_clustering = ATA_SHT_USE_CLUSTERING,
  594. .proc_name = DRV_NAME,
  595. .dma_boundary = ATA_DMA_BOUNDARY,
  596. .slave_configure = ata_scsi_slave_config,
  597. .bios_param = ata_std_bios_param,
  598. };
  599. static struct ata_port_operations it821x_smart_port_ops = {
  600. .set_mode = it821x_smart_set_mode,
  601. .port_disable = ata_port_disable,
  602. .tf_load = ata_tf_load,
  603. .tf_read = ata_tf_read,
  604. .mode_filter = ata_pci_default_filter,
  605. .check_status = ata_check_status,
  606. .check_atapi_dma= it821x_check_atapi_dma,
  607. .exec_command = ata_exec_command,
  608. .dev_select = ata_std_dev_select,
  609. .dev_config = it821x_dev_config,
  610. .freeze = ata_bmdma_freeze,
  611. .thaw = ata_bmdma_thaw,
  612. .error_handler = it821x_error_handler,
  613. .post_internal_cmd = ata_bmdma_post_internal_cmd,
  614. .bmdma_setup = ata_bmdma_setup,
  615. .bmdma_start = ata_bmdma_start,
  616. .bmdma_stop = ata_bmdma_stop,
  617. .bmdma_status = ata_bmdma_status,
  618. .qc_prep = ata_qc_prep,
  619. .qc_issue = it821x_smart_qc_issue_prot,
  620. .data_xfer = ata_pio_data_xfer,
  621. .irq_handler = ata_interrupt,
  622. .irq_clear = ata_bmdma_irq_clear,
  623. .port_start = it821x_port_start,
  624. .port_stop = it821x_port_stop,
  625. .host_stop = ata_host_stop
  626. };
  627. static struct ata_port_operations it821x_passthru_port_ops = {
  628. .port_disable = ata_port_disable,
  629. .set_piomode = it821x_passthru_set_piomode,
  630. .set_dmamode = it821x_passthru_set_dmamode,
  631. .mode_filter = ata_pci_default_filter,
  632. .tf_load = ata_tf_load,
  633. .tf_read = ata_tf_read,
  634. .check_status = ata_check_status,
  635. .exec_command = ata_exec_command,
  636. .check_atapi_dma= it821x_check_atapi_dma,
  637. .dev_select = it821x_passthru_dev_select,
  638. .freeze = ata_bmdma_freeze,
  639. .thaw = ata_bmdma_thaw,
  640. .error_handler = it821x_error_handler,
  641. .post_internal_cmd = ata_bmdma_post_internal_cmd,
  642. .bmdma_setup = ata_bmdma_setup,
  643. .bmdma_start = it821x_passthru_bmdma_start,
  644. .bmdma_stop = it821x_passthru_bmdma_stop,
  645. .bmdma_status = ata_bmdma_status,
  646. .qc_prep = ata_qc_prep,
  647. .qc_issue = it821x_passthru_qc_issue_prot,
  648. .data_xfer = ata_pio_data_xfer,
  649. .irq_clear = ata_bmdma_irq_clear,
  650. .irq_handler = ata_interrupt,
  651. .port_start = it821x_port_start,
  652. .port_stop = it821x_port_stop,
  653. .host_stop = ata_host_stop
  654. };
  655. static void __devinit it821x_disable_raid(struct pci_dev *pdev)
  656. {
  657. /* Reset local CPU, and set BIOS not ready */
  658. pci_write_config_byte(pdev, 0x5E, 0x01);
  659. /* Set to bypass mode, and reset PCI bus */
  660. pci_write_config_byte(pdev, 0x50, 0x00);
  661. pci_write_config_word(pdev, PCI_COMMAND,
  662. PCI_COMMAND_PARITY | PCI_COMMAND_IO |
  663. PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
  664. pci_write_config_word(pdev, 0x40, 0xA0F3);
  665. pci_write_config_dword(pdev,0x4C, 0x02040204);
  666. pci_write_config_byte(pdev, 0x42, 0x36);
  667. pci_write_config_byte(pdev, PCI_LATENCY_TIMER, 0x20);
  668. }
  669. static int it821x_init_one(struct pci_dev *pdev, const struct pci_device_id *id)
  670. {
  671. u8 conf;
  672. static struct ata_port_info info_smart = {
  673. .sht = &it821x_sht,
  674. .flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_SRST,
  675. .pio_mask = 0x1f,
  676. .mwdma_mask = 0x07,
  677. .port_ops = &it821x_smart_port_ops
  678. };
  679. static struct ata_port_info info_passthru = {
  680. .sht = &it821x_sht,
  681. .flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_SRST,
  682. .pio_mask = 0x1f,
  683. .mwdma_mask = 0x07,
  684. .udma_mask = 0x7f,
  685. .port_ops = &it821x_passthru_port_ops
  686. };
  687. static struct ata_port_info *port_info[2];
  688. static char *mode[2] = { "pass through", "smart" };
  689. /* Force the card into bypass mode if so requested */
  690. if (it8212_noraid) {
  691. printk(KERN_INFO DRV_NAME ": forcing bypass mode.\n");
  692. it821x_disable_raid(pdev);
  693. }
  694. pci_read_config_byte(pdev, 0x50, &conf);
  695. conf &= 1;
  696. printk(KERN_INFO DRV_NAME ": controller in %s mode.\n", mode[conf]);
  697. if (conf == 0)
  698. port_info[0] = port_info[1] = &info_passthru;
  699. else
  700. port_info[0] = port_info[1] = &info_smart;
  701. return ata_pci_init_one(pdev, port_info, 2);
  702. }
  703. static const struct pci_device_id it821x[] = {
  704. { PCI_VDEVICE(ITE, PCI_DEVICE_ID_ITE_8211), },
  705. { PCI_VDEVICE(ITE, PCI_DEVICE_ID_ITE_8212), },
  706. { },
  707. };
  708. static struct pci_driver it821x_pci_driver = {
  709. .name = DRV_NAME,
  710. .id_table = it821x,
  711. .probe = it821x_init_one,
  712. .remove = ata_pci_remove_one
  713. };
  714. static int __init it821x_init(void)
  715. {
  716. return pci_register_driver(&it821x_pci_driver);
  717. }
  718. static void __exit it821x_exit(void)
  719. {
  720. pci_unregister_driver(&it821x_pci_driver);
  721. }
  722. MODULE_AUTHOR("Alan Cox");
  723. MODULE_DESCRIPTION("low-level driver for the IT8211/IT8212 IDE RAID controller");
  724. MODULE_LICENSE("GPL");
  725. MODULE_DEVICE_TABLE(pci, it821x);
  726. MODULE_VERSION(DRV_VERSION);
  727. module_param_named(noraid, it8212_noraid, int, S_IRUGO);
  728. MODULE_PARM_DESC(it8212_noraid, "Force card into bypass mode");
  729. module_init(it821x_init);
  730. module_exit(it821x_exit);