am53c974.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * AMD am53c974 driver.
  4. * Copyright (c) 2014 Hannes Reinecke, SUSE Linux GmbH
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/module.h>
  8. #include <linux/init.h>
  9. #include <linux/delay.h>
  10. #include <linux/pci.h>
  11. #include <linux/interrupt.h>
  12. #include <scsi/scsi_host.h>
  13. #include "esp_scsi.h"
  14. #define DRV_MODULE_NAME "am53c974"
  15. #define DRV_MODULE_VERSION "1.00"
  16. static bool am53c974_debug;
  17. static bool am53c974_fenab = true;
  18. #define esp_dma_log(f, a...) \
  19. do { \
  20. if (am53c974_debug) \
  21. shost_printk(KERN_DEBUG, esp->host, f, ##a); \
  22. } while (0)
  23. #define ESP_DMA_CMD 0x10
  24. #define ESP_DMA_STC 0x11
  25. #define ESP_DMA_SPA 0x12
  26. #define ESP_DMA_WBC 0x13
  27. #define ESP_DMA_WAC 0x14
  28. #define ESP_DMA_STATUS 0x15
  29. #define ESP_DMA_SMDLA 0x16
  30. #define ESP_DMA_WMAC 0x17
  31. #define ESP_DMA_CMD_IDLE 0x00
  32. #define ESP_DMA_CMD_BLAST 0x01
  33. #define ESP_DMA_CMD_ABORT 0x02
  34. #define ESP_DMA_CMD_START 0x03
  35. #define ESP_DMA_CMD_MASK 0x03
  36. #define ESP_DMA_CMD_DIAG 0x04
  37. #define ESP_DMA_CMD_MDL 0x10
  38. #define ESP_DMA_CMD_INTE_P 0x20
  39. #define ESP_DMA_CMD_INTE_D 0x40
  40. #define ESP_DMA_CMD_DIR 0x80
  41. #define ESP_DMA_STAT_PWDN 0x01
  42. #define ESP_DMA_STAT_ERROR 0x02
  43. #define ESP_DMA_STAT_ABORT 0x04
  44. #define ESP_DMA_STAT_DONE 0x08
  45. #define ESP_DMA_STAT_SCSIINT 0x10
  46. #define ESP_DMA_STAT_BCMPLT 0x20
  47. /* EEPROM is accessed with 16-bit values */
  48. #define DC390_EEPROM_READ 0x80
  49. #define DC390_EEPROM_LEN 0x40
  50. /*
  51. * DC390 EEPROM
  52. *
  53. * 8 * 4 bytes of per-device options
  54. * followed by HBA specific options
  55. */
  56. /* Per-device options */
  57. #define DC390_EE_MODE1 0x00
  58. #define DC390_EE_SPEED 0x01
  59. /* HBA-specific options */
  60. #define DC390_EE_ADAPT_SCSI_ID 0x40
  61. #define DC390_EE_MODE2 0x41
  62. #define DC390_EE_DELAY 0x42
  63. #define DC390_EE_TAG_CMD_NUM 0x43
  64. #define DC390_EE_MODE1_PARITY_CHK 0x01
  65. #define DC390_EE_MODE1_SYNC_NEGO 0x02
  66. #define DC390_EE_MODE1_EN_DISC 0x04
  67. #define DC390_EE_MODE1_SEND_START 0x08
  68. #define DC390_EE_MODE1_TCQ 0x10
  69. #define DC390_EE_MODE2_MORE_2DRV 0x01
  70. #define DC390_EE_MODE2_GREATER_1G 0x02
  71. #define DC390_EE_MODE2_RST_SCSI_BUS 0x04
  72. #define DC390_EE_MODE2_ACTIVE_NEGATION 0x08
  73. #define DC390_EE_MODE2_NO_SEEK 0x10
  74. #define DC390_EE_MODE2_LUN_CHECK 0x20
  75. struct pci_esp_priv {
  76. struct esp *esp;
  77. u8 dma_status;
  78. };
  79. static void pci_esp_dma_drain(struct esp *esp);
  80. static inline struct pci_esp_priv *pci_esp_get_priv(struct esp *esp)
  81. {
  82. return dev_get_drvdata(esp->dev);
  83. }
  84. static void pci_esp_write8(struct esp *esp, u8 val, unsigned long reg)
  85. {
  86. iowrite8(val, esp->regs + (reg * 4UL));
  87. }
  88. static u8 pci_esp_read8(struct esp *esp, unsigned long reg)
  89. {
  90. return ioread8(esp->regs + (reg * 4UL));
  91. }
  92. static void pci_esp_write32(struct esp *esp, u32 val, unsigned long reg)
  93. {
  94. return iowrite32(val, esp->regs + (reg * 4UL));
  95. }
  96. static int pci_esp_irq_pending(struct esp *esp)
  97. {
  98. struct pci_esp_priv *pep = pci_esp_get_priv(esp);
  99. pep->dma_status = pci_esp_read8(esp, ESP_DMA_STATUS);
  100. esp_dma_log("dma intr dreg[%02x]\n", pep->dma_status);
  101. if (pep->dma_status & (ESP_DMA_STAT_ERROR |
  102. ESP_DMA_STAT_ABORT |
  103. ESP_DMA_STAT_DONE |
  104. ESP_DMA_STAT_SCSIINT))
  105. return 1;
  106. return 0;
  107. }
  108. static void pci_esp_reset_dma(struct esp *esp)
  109. {
  110. /* Nothing to do ? */
  111. }
  112. static void pci_esp_dma_drain(struct esp *esp)
  113. {
  114. u8 resid;
  115. int lim = 1000;
  116. if ((esp->sreg & ESP_STAT_PMASK) == ESP_DOP ||
  117. (esp->sreg & ESP_STAT_PMASK) == ESP_DIP)
  118. /* Data-In or Data-Out, nothing to be done */
  119. return;
  120. while (--lim > 0) {
  121. resid = pci_esp_read8(esp, ESP_FFLAGS) & ESP_FF_FBYTES;
  122. if (resid <= 1)
  123. break;
  124. cpu_relax();
  125. }
  126. /*
  127. * When there is a residual BCMPLT will never be set
  128. * (obviously). But we still have to issue the BLAST
  129. * command, otherwise the data will not being transferred.
  130. * But we'll never know when the BLAST operation is
  131. * finished. So check for some time and give up eventually.
  132. */
  133. lim = 1000;
  134. pci_esp_write8(esp, ESP_DMA_CMD_DIR | ESP_DMA_CMD_BLAST, ESP_DMA_CMD);
  135. while (pci_esp_read8(esp, ESP_DMA_STATUS) & ESP_DMA_STAT_BCMPLT) {
  136. if (--lim == 0)
  137. break;
  138. cpu_relax();
  139. }
  140. pci_esp_write8(esp, ESP_DMA_CMD_DIR | ESP_DMA_CMD_IDLE, ESP_DMA_CMD);
  141. esp_dma_log("DMA blast done (%d tries, %d bytes left)\n", lim, resid);
  142. /* BLAST residual handling is currently untested */
  143. if (WARN_ON_ONCE(resid == 1)) {
  144. struct esp_cmd_entry *ent = esp->active_cmd;
  145. ent->flags |= ESP_CMD_FLAG_RESIDUAL;
  146. }
  147. }
  148. static void pci_esp_dma_invalidate(struct esp *esp)
  149. {
  150. struct pci_esp_priv *pep = pci_esp_get_priv(esp);
  151. esp_dma_log("invalidate DMA\n");
  152. pci_esp_write8(esp, ESP_DMA_CMD_IDLE, ESP_DMA_CMD);
  153. pep->dma_status = 0;
  154. }
  155. static int pci_esp_dma_error(struct esp *esp)
  156. {
  157. struct pci_esp_priv *pep = pci_esp_get_priv(esp);
  158. if (pep->dma_status & ESP_DMA_STAT_ERROR) {
  159. u8 dma_cmd = pci_esp_read8(esp, ESP_DMA_CMD);
  160. if ((dma_cmd & ESP_DMA_CMD_MASK) == ESP_DMA_CMD_START)
  161. pci_esp_write8(esp, ESP_DMA_CMD_ABORT, ESP_DMA_CMD);
  162. return 1;
  163. }
  164. if (pep->dma_status & ESP_DMA_STAT_ABORT) {
  165. pci_esp_write8(esp, ESP_DMA_CMD_IDLE, ESP_DMA_CMD);
  166. pep->dma_status = pci_esp_read8(esp, ESP_DMA_CMD);
  167. return 1;
  168. }
  169. return 0;
  170. }
  171. static void pci_esp_send_dma_cmd(struct esp *esp, u32 addr, u32 esp_count,
  172. u32 dma_count, int write, u8 cmd)
  173. {
  174. struct pci_esp_priv *pep = pci_esp_get_priv(esp);
  175. u32 val = 0;
  176. BUG_ON(!(cmd & ESP_CMD_DMA));
  177. pep->dma_status = 0;
  178. /* Set DMA engine to IDLE */
  179. if (write)
  180. /* DMA write direction logic is inverted */
  181. val |= ESP_DMA_CMD_DIR;
  182. pci_esp_write8(esp, ESP_DMA_CMD_IDLE | val, ESP_DMA_CMD);
  183. pci_esp_write8(esp, (esp_count >> 0) & 0xff, ESP_TCLOW);
  184. pci_esp_write8(esp, (esp_count >> 8) & 0xff, ESP_TCMED);
  185. if (esp->config2 & ESP_CONFIG2_FENAB)
  186. pci_esp_write8(esp, (esp_count >> 16) & 0xff, ESP_TCHI);
  187. pci_esp_write32(esp, esp_count, ESP_DMA_STC);
  188. pci_esp_write32(esp, addr, ESP_DMA_SPA);
  189. esp_dma_log("start dma addr[%x] count[%d:%d]\n",
  190. addr, esp_count, dma_count);
  191. scsi_esp_cmd(esp, cmd);
  192. /* Send DMA Start command */
  193. pci_esp_write8(esp, ESP_DMA_CMD_START | val, ESP_DMA_CMD);
  194. }
  195. static u32 pci_esp_dma_length_limit(struct esp *esp, u32 dma_addr, u32 dma_len)
  196. {
  197. int dma_limit = 16;
  198. u32 base, end;
  199. /*
  200. * If CONFIG2_FENAB is set we can
  201. * handle up to 24 bit addresses
  202. */
  203. if (esp->config2 & ESP_CONFIG2_FENAB)
  204. dma_limit = 24;
  205. if (dma_len > (1U << dma_limit))
  206. dma_len = (1U << dma_limit);
  207. /*
  208. * Prevent crossing a 24-bit address boundary.
  209. */
  210. base = dma_addr & ((1U << 24) - 1U);
  211. end = base + dma_len;
  212. if (end > (1U << 24))
  213. end = (1U <<24);
  214. dma_len = end - base;
  215. return dma_len;
  216. }
  217. static const struct esp_driver_ops pci_esp_ops = {
  218. .esp_write8 = pci_esp_write8,
  219. .esp_read8 = pci_esp_read8,
  220. .irq_pending = pci_esp_irq_pending,
  221. .reset_dma = pci_esp_reset_dma,
  222. .dma_drain = pci_esp_dma_drain,
  223. .dma_invalidate = pci_esp_dma_invalidate,
  224. .send_dma_cmd = pci_esp_send_dma_cmd,
  225. .dma_error = pci_esp_dma_error,
  226. .dma_length_limit = pci_esp_dma_length_limit,
  227. };
  228. /*
  229. * Read DC-390 eeprom
  230. */
  231. static void dc390_eeprom_prepare_read(struct pci_dev *pdev, u8 cmd)
  232. {
  233. u8 carry_flag = 1, j = 0x80, bval;
  234. int i;
  235. for (i = 0; i < 9; i++) {
  236. if (carry_flag) {
  237. pci_write_config_byte(pdev, 0x80, 0x40);
  238. bval = 0xc0;
  239. } else
  240. bval = 0x80;
  241. udelay(160);
  242. pci_write_config_byte(pdev, 0x80, bval);
  243. udelay(160);
  244. pci_write_config_byte(pdev, 0x80, 0);
  245. udelay(160);
  246. carry_flag = (cmd & j) ? 1 : 0;
  247. j >>= 1;
  248. }
  249. }
  250. static u16 dc390_eeprom_get_data(struct pci_dev *pdev)
  251. {
  252. int i;
  253. u16 wval = 0;
  254. u8 bval;
  255. for (i = 0; i < 16; i++) {
  256. wval <<= 1;
  257. pci_write_config_byte(pdev, 0x80, 0x80);
  258. udelay(160);
  259. pci_write_config_byte(pdev, 0x80, 0x40);
  260. udelay(160);
  261. pci_read_config_byte(pdev, 0x00, &bval);
  262. if (bval == 0x22)
  263. wval |= 1;
  264. }
  265. return wval;
  266. }
  267. static void dc390_read_eeprom(struct pci_dev *pdev, u16 *ptr)
  268. {
  269. u8 cmd = DC390_EEPROM_READ, i;
  270. for (i = 0; i < DC390_EEPROM_LEN; i++) {
  271. pci_write_config_byte(pdev, 0xc0, 0);
  272. udelay(160);
  273. dc390_eeprom_prepare_read(pdev, cmd++);
  274. *ptr++ = dc390_eeprom_get_data(pdev);
  275. pci_write_config_byte(pdev, 0x80, 0);
  276. pci_write_config_byte(pdev, 0x80, 0);
  277. udelay(160);
  278. }
  279. }
  280. static void dc390_check_eeprom(struct esp *esp)
  281. {
  282. struct pci_dev *pdev = to_pci_dev(esp->dev);
  283. u8 EEbuf[128];
  284. u16 *ptr = (u16 *)EEbuf, wval = 0;
  285. int i;
  286. dc390_read_eeprom(pdev, ptr);
  287. for (i = 0; i < DC390_EEPROM_LEN; i++, ptr++)
  288. wval += *ptr;
  289. /* no Tekram EEprom found */
  290. if (wval != 0x1234) {
  291. dev_printk(KERN_INFO, &pdev->dev,
  292. "No valid Tekram EEprom found\n");
  293. return;
  294. }
  295. esp->scsi_id = EEbuf[DC390_EE_ADAPT_SCSI_ID];
  296. esp->num_tags = 2 << EEbuf[DC390_EE_TAG_CMD_NUM];
  297. if (EEbuf[DC390_EE_MODE2] & DC390_EE_MODE2_ACTIVE_NEGATION)
  298. esp->config4 |= ESP_CONFIG4_RADE | ESP_CONFIG4_RAE;
  299. }
  300. static int pci_esp_probe_one(struct pci_dev *pdev,
  301. const struct pci_device_id *id)
  302. {
  303. struct scsi_host_template *hostt = &scsi_esp_template;
  304. int err = -ENODEV;
  305. struct Scsi_Host *shost;
  306. struct esp *esp;
  307. struct pci_esp_priv *pep;
  308. if (pci_enable_device(pdev)) {
  309. dev_printk(KERN_INFO, &pdev->dev, "cannot enable device\n");
  310. return -ENODEV;
  311. }
  312. if (dma_set_mask(&pdev->dev, DMA_BIT_MASK(32))) {
  313. dev_printk(KERN_INFO, &pdev->dev,
  314. "failed to set 32bit DMA mask\n");
  315. goto fail_disable_device;
  316. }
  317. shost = scsi_host_alloc(hostt, sizeof(struct esp));
  318. if (!shost) {
  319. dev_printk(KERN_INFO, &pdev->dev,
  320. "failed to allocate scsi host\n");
  321. err = -ENOMEM;
  322. goto fail_disable_device;
  323. }
  324. pep = kzalloc(sizeof(struct pci_esp_priv), GFP_KERNEL);
  325. if (!pep) {
  326. dev_printk(KERN_INFO, &pdev->dev,
  327. "failed to allocate esp_priv\n");
  328. err = -ENOMEM;
  329. goto fail_host_alloc;
  330. }
  331. esp = shost_priv(shost);
  332. esp->host = shost;
  333. esp->dev = &pdev->dev;
  334. esp->ops = &pci_esp_ops;
  335. /*
  336. * The am53c974 HBA has a design flaw of generating
  337. * spurious DMA completion interrupts when using
  338. * DMA for command submission.
  339. */
  340. esp->flags |= ESP_FLAG_USE_FIFO;
  341. /*
  342. * Enable CONFIG2_FENAB to allow for large DMA transfers
  343. */
  344. if (am53c974_fenab)
  345. esp->config2 |= ESP_CONFIG2_FENAB;
  346. pep->esp = esp;
  347. if (pci_request_regions(pdev, DRV_MODULE_NAME)) {
  348. dev_printk(KERN_ERR, &pdev->dev,
  349. "pci memory selection failed\n");
  350. goto fail_priv_alloc;
  351. }
  352. esp->regs = pci_iomap(pdev, 0, pci_resource_len(pdev, 0));
  353. if (!esp->regs) {
  354. dev_printk(KERN_ERR, &pdev->dev, "pci I/O map failed\n");
  355. err = -EINVAL;
  356. goto fail_release_regions;
  357. }
  358. esp->dma_regs = esp->regs;
  359. pci_set_master(pdev);
  360. esp->command_block = dma_alloc_coherent(&pdev->dev, 16,
  361. &esp->command_block_dma, GFP_KERNEL);
  362. if (!esp->command_block) {
  363. dev_printk(KERN_ERR, &pdev->dev,
  364. "failed to allocate command block\n");
  365. err = -ENOMEM;
  366. goto fail_unmap_regs;
  367. }
  368. pci_set_drvdata(pdev, pep);
  369. err = request_irq(pdev->irq, scsi_esp_intr, IRQF_SHARED,
  370. DRV_MODULE_NAME, esp);
  371. if (err < 0) {
  372. dev_printk(KERN_ERR, &pdev->dev, "failed to register IRQ\n");
  373. goto fail_unmap_command_block;
  374. }
  375. esp->scsi_id = 7;
  376. dc390_check_eeprom(esp);
  377. shost->this_id = esp->scsi_id;
  378. shost->max_id = 8;
  379. shost->irq = pdev->irq;
  380. shost->io_port = pci_resource_start(pdev, 0);
  381. shost->n_io_port = pci_resource_len(pdev, 0);
  382. shost->unique_id = shost->io_port;
  383. esp->scsi_id_mask = (1 << esp->scsi_id);
  384. /* Assume 40MHz clock */
  385. esp->cfreq = 40000000;
  386. err = scsi_esp_register(esp);
  387. if (err)
  388. goto fail_free_irq;
  389. return 0;
  390. fail_free_irq:
  391. free_irq(pdev->irq, esp);
  392. fail_unmap_command_block:
  393. pci_set_drvdata(pdev, NULL);
  394. dma_free_coherent(&pdev->dev, 16, esp->command_block,
  395. esp->command_block_dma);
  396. fail_unmap_regs:
  397. pci_iounmap(pdev, esp->regs);
  398. fail_release_regions:
  399. pci_release_regions(pdev);
  400. fail_priv_alloc:
  401. kfree(pep);
  402. fail_host_alloc:
  403. scsi_host_put(shost);
  404. fail_disable_device:
  405. pci_disable_device(pdev);
  406. return err;
  407. }
  408. static void pci_esp_remove_one(struct pci_dev *pdev)
  409. {
  410. struct pci_esp_priv *pep = pci_get_drvdata(pdev);
  411. struct esp *esp = pep->esp;
  412. scsi_esp_unregister(esp);
  413. free_irq(pdev->irq, esp);
  414. pci_set_drvdata(pdev, NULL);
  415. dma_free_coherent(&pdev->dev, 16, esp->command_block,
  416. esp->command_block_dma);
  417. pci_iounmap(pdev, esp->regs);
  418. pci_release_regions(pdev);
  419. pci_disable_device(pdev);
  420. kfree(pep);
  421. scsi_host_put(esp->host);
  422. }
  423. static struct pci_device_id am53c974_pci_tbl[] = {
  424. { PCI_VENDOR_ID_AMD, PCI_DEVICE_ID_AMD_SCSI,
  425. PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
  426. { }
  427. };
  428. MODULE_DEVICE_TABLE(pci, am53c974_pci_tbl);
  429. static struct pci_driver am53c974_driver = {
  430. .name = DRV_MODULE_NAME,
  431. .id_table = am53c974_pci_tbl,
  432. .probe = pci_esp_probe_one,
  433. .remove = pci_esp_remove_one,
  434. };
  435. module_pci_driver(am53c974_driver);
  436. MODULE_DESCRIPTION("AM53C974 SCSI driver");
  437. MODULE_AUTHOR("Hannes Reinecke <[email protected]>");
  438. MODULE_LICENSE("GPL");
  439. MODULE_VERSION(DRV_MODULE_VERSION);
  440. MODULE_ALIAS("tmscsim");
  441. module_param(am53c974_debug, bool, 0644);
  442. MODULE_PARM_DESC(am53c974_debug, "Enable debugging");
  443. module_param(am53c974_fenab, bool, 0444);
  444. MODULE_PARM_DESC(am53c974_fenab, "Enable 24-bit DMA transfer sizes");