fsi-scom.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * SCOM FSI Client device driver
  4. *
  5. * Copyright (C) IBM Corporation 2016
  6. */
  7. #include <linux/fsi.h>
  8. #include <linux/module.h>
  9. #include <linux/cdev.h>
  10. #include <linux/delay.h>
  11. #include <linux/fs.h>
  12. #include <linux/mod_devicetable.h>
  13. #include <linux/uaccess.h>
  14. #include <linux/slab.h>
  15. #include <linux/list.h>
  16. #include <uapi/linux/fsi.h>
  17. #define FSI_ENGID_SCOM 0x5
  18. /* SCOM engine register set */
  19. #define SCOM_DATA0_REG 0x00
  20. #define SCOM_DATA1_REG 0x04
  21. #define SCOM_CMD_REG 0x08
  22. #define SCOM_FSI2PIB_RESET_REG 0x18
  23. #define SCOM_STATUS_REG 0x1C /* Read */
  24. #define SCOM_PIB_RESET_REG 0x1C /* Write */
  25. /* Command register */
  26. #define SCOM_WRITE_CMD 0x80000000
  27. #define SCOM_READ_CMD 0x00000000
  28. /* Status register bits */
  29. #define SCOM_STATUS_ERR_SUMMARY 0x80000000
  30. #define SCOM_STATUS_PROTECTION 0x01000000
  31. #define SCOM_STATUS_PARITY 0x04000000
  32. #define SCOM_STATUS_PIB_ABORT 0x00100000
  33. #define SCOM_STATUS_PIB_RESP_MASK 0x00007000
  34. #define SCOM_STATUS_PIB_RESP_SHIFT 12
  35. #define SCOM_STATUS_FSI2PIB_ERROR (SCOM_STATUS_PROTECTION | \
  36. SCOM_STATUS_PARITY | \
  37. SCOM_STATUS_PIB_ABORT)
  38. #define SCOM_STATUS_ANY_ERR (SCOM_STATUS_FSI2PIB_ERROR | \
  39. SCOM_STATUS_PIB_RESP_MASK)
  40. /* SCOM address encodings */
  41. #define XSCOM_ADDR_IND_FLAG BIT_ULL(63)
  42. #define XSCOM_ADDR_INF_FORM1 BIT_ULL(60)
  43. /* SCOM indirect stuff */
  44. #define XSCOM_ADDR_DIRECT_PART 0x7fffffffull
  45. #define XSCOM_ADDR_INDIRECT_PART 0x000fffff00000000ull
  46. #define XSCOM_DATA_IND_READ BIT_ULL(63)
  47. #define XSCOM_DATA_IND_COMPLETE BIT_ULL(31)
  48. #define XSCOM_DATA_IND_ERR_MASK 0x70000000ull
  49. #define XSCOM_DATA_IND_ERR_SHIFT 28
  50. #define XSCOM_DATA_IND_DATA 0x0000ffffull
  51. #define XSCOM_DATA_IND_FORM1_DATA 0x000fffffffffffffull
  52. #define XSCOM_ADDR_FORM1_LOW 0x000ffffffffull
  53. #define XSCOM_ADDR_FORM1_HI 0xfff00000000ull
  54. #define XSCOM_ADDR_FORM1_HI_SHIFT 20
  55. /* Retries */
  56. #define SCOM_MAX_IND_RETRIES 10 /* Retries indirect not ready */
  57. struct scom_device {
  58. struct list_head link;
  59. struct fsi_device *fsi_dev;
  60. struct device dev;
  61. struct cdev cdev;
  62. struct mutex lock;
  63. bool dead;
  64. };
  65. static int __put_scom(struct scom_device *scom_dev, uint64_t value,
  66. uint32_t addr, uint32_t *status)
  67. {
  68. __be32 data, raw_status;
  69. int rc;
  70. data = cpu_to_be32((value >> 32) & 0xffffffff);
  71. rc = fsi_device_write(scom_dev->fsi_dev, SCOM_DATA0_REG, &data,
  72. sizeof(uint32_t));
  73. if (rc)
  74. return rc;
  75. data = cpu_to_be32(value & 0xffffffff);
  76. rc = fsi_device_write(scom_dev->fsi_dev, SCOM_DATA1_REG, &data,
  77. sizeof(uint32_t));
  78. if (rc)
  79. return rc;
  80. data = cpu_to_be32(SCOM_WRITE_CMD | addr);
  81. rc = fsi_device_write(scom_dev->fsi_dev, SCOM_CMD_REG, &data,
  82. sizeof(uint32_t));
  83. if (rc)
  84. return rc;
  85. rc = fsi_device_read(scom_dev->fsi_dev, SCOM_STATUS_REG, &raw_status,
  86. sizeof(uint32_t));
  87. if (rc)
  88. return rc;
  89. *status = be32_to_cpu(raw_status);
  90. return 0;
  91. }
  92. static int __get_scom(struct scom_device *scom_dev, uint64_t *value,
  93. uint32_t addr, uint32_t *status)
  94. {
  95. __be32 data, raw_status;
  96. int rc;
  97. *value = 0ULL;
  98. data = cpu_to_be32(SCOM_READ_CMD | addr);
  99. rc = fsi_device_write(scom_dev->fsi_dev, SCOM_CMD_REG, &data,
  100. sizeof(uint32_t));
  101. if (rc)
  102. return rc;
  103. rc = fsi_device_read(scom_dev->fsi_dev, SCOM_STATUS_REG, &raw_status,
  104. sizeof(uint32_t));
  105. if (rc)
  106. return rc;
  107. /*
  108. * Read the data registers even on error, so we don't have
  109. * to interpret the status register here.
  110. */
  111. rc = fsi_device_read(scom_dev->fsi_dev, SCOM_DATA0_REG, &data,
  112. sizeof(uint32_t));
  113. if (rc)
  114. return rc;
  115. *value |= (uint64_t)be32_to_cpu(data) << 32;
  116. rc = fsi_device_read(scom_dev->fsi_dev, SCOM_DATA1_REG, &data,
  117. sizeof(uint32_t));
  118. if (rc)
  119. return rc;
  120. *value |= be32_to_cpu(data);
  121. *status = be32_to_cpu(raw_status);
  122. return rc;
  123. }
  124. static int put_indirect_scom_form0(struct scom_device *scom, uint64_t value,
  125. uint64_t addr, uint32_t *status)
  126. {
  127. uint64_t ind_data, ind_addr;
  128. int rc, err;
  129. if (value & ~XSCOM_DATA_IND_DATA)
  130. return -EINVAL;
  131. ind_addr = addr & XSCOM_ADDR_DIRECT_PART;
  132. ind_data = (addr & XSCOM_ADDR_INDIRECT_PART) | value;
  133. rc = __put_scom(scom, ind_data, ind_addr, status);
  134. if (rc || (*status & SCOM_STATUS_ANY_ERR))
  135. return rc;
  136. rc = __get_scom(scom, &ind_data, addr, status);
  137. if (rc || (*status & SCOM_STATUS_ANY_ERR))
  138. return rc;
  139. err = (ind_data & XSCOM_DATA_IND_ERR_MASK) >> XSCOM_DATA_IND_ERR_SHIFT;
  140. *status = err << SCOM_STATUS_PIB_RESP_SHIFT;
  141. return 0;
  142. }
  143. static int put_indirect_scom_form1(struct scom_device *scom, uint64_t value,
  144. uint64_t addr, uint32_t *status)
  145. {
  146. uint64_t ind_data, ind_addr;
  147. if (value & ~XSCOM_DATA_IND_FORM1_DATA)
  148. return -EINVAL;
  149. ind_addr = addr & XSCOM_ADDR_FORM1_LOW;
  150. ind_data = value | (addr & XSCOM_ADDR_FORM1_HI) << XSCOM_ADDR_FORM1_HI_SHIFT;
  151. return __put_scom(scom, ind_data, ind_addr, status);
  152. }
  153. static int get_indirect_scom_form0(struct scom_device *scom, uint64_t *value,
  154. uint64_t addr, uint32_t *status)
  155. {
  156. uint64_t ind_data, ind_addr;
  157. int rc, err;
  158. ind_addr = addr & XSCOM_ADDR_DIRECT_PART;
  159. ind_data = (addr & XSCOM_ADDR_INDIRECT_PART) | XSCOM_DATA_IND_READ;
  160. rc = __put_scom(scom, ind_data, ind_addr, status);
  161. if (rc || (*status & SCOM_STATUS_ANY_ERR))
  162. return rc;
  163. rc = __get_scom(scom, &ind_data, addr, status);
  164. if (rc || (*status & SCOM_STATUS_ANY_ERR))
  165. return rc;
  166. err = (ind_data & XSCOM_DATA_IND_ERR_MASK) >> XSCOM_DATA_IND_ERR_SHIFT;
  167. *status = err << SCOM_STATUS_PIB_RESP_SHIFT;
  168. *value = ind_data & XSCOM_DATA_IND_DATA;
  169. return 0;
  170. }
  171. static int raw_put_scom(struct scom_device *scom, uint64_t value,
  172. uint64_t addr, uint32_t *status)
  173. {
  174. if (addr & XSCOM_ADDR_IND_FLAG) {
  175. if (addr & XSCOM_ADDR_INF_FORM1)
  176. return put_indirect_scom_form1(scom, value, addr, status);
  177. else
  178. return put_indirect_scom_form0(scom, value, addr, status);
  179. } else
  180. return __put_scom(scom, value, addr, status);
  181. }
  182. static int raw_get_scom(struct scom_device *scom, uint64_t *value,
  183. uint64_t addr, uint32_t *status)
  184. {
  185. if (addr & XSCOM_ADDR_IND_FLAG) {
  186. if (addr & XSCOM_ADDR_INF_FORM1)
  187. return -ENXIO;
  188. return get_indirect_scom_form0(scom, value, addr, status);
  189. } else
  190. return __get_scom(scom, value, addr, status);
  191. }
  192. static int handle_fsi2pib_status(struct scom_device *scom, uint32_t status)
  193. {
  194. uint32_t dummy = -1;
  195. if (status & SCOM_STATUS_FSI2PIB_ERROR)
  196. fsi_device_write(scom->fsi_dev, SCOM_FSI2PIB_RESET_REG, &dummy,
  197. sizeof(uint32_t));
  198. if (status & SCOM_STATUS_PROTECTION)
  199. return -EPERM;
  200. if (status & SCOM_STATUS_PARITY)
  201. return -EIO;
  202. if (status & SCOM_STATUS_PIB_ABORT)
  203. return -EBUSY;
  204. return 0;
  205. }
  206. static int handle_pib_status(struct scom_device *scom, uint8_t status)
  207. {
  208. uint32_t dummy = -1;
  209. if (status == SCOM_PIB_SUCCESS)
  210. return 0;
  211. if (status == SCOM_PIB_BLOCKED)
  212. return -EBUSY;
  213. /* Reset the bridge */
  214. fsi_device_write(scom->fsi_dev, SCOM_FSI2PIB_RESET_REG, &dummy,
  215. sizeof(uint32_t));
  216. switch(status) {
  217. case SCOM_PIB_OFFLINE:
  218. return -ENODEV;
  219. case SCOM_PIB_BAD_ADDR:
  220. return -ENXIO;
  221. case SCOM_PIB_TIMEOUT:
  222. return -ETIMEDOUT;
  223. case SCOM_PIB_PARTIAL:
  224. case SCOM_PIB_CLK_ERR:
  225. case SCOM_PIB_PARITY_ERR:
  226. default:
  227. return -EIO;
  228. }
  229. }
  230. static int put_scom(struct scom_device *scom, uint64_t value,
  231. uint64_t addr)
  232. {
  233. uint32_t status;
  234. int rc;
  235. rc = raw_put_scom(scom, value, addr, &status);
  236. if (rc)
  237. return rc;
  238. rc = handle_fsi2pib_status(scom, status);
  239. if (rc)
  240. return rc;
  241. return handle_pib_status(scom,
  242. (status & SCOM_STATUS_PIB_RESP_MASK)
  243. >> SCOM_STATUS_PIB_RESP_SHIFT);
  244. }
  245. static int get_scom(struct scom_device *scom, uint64_t *value,
  246. uint64_t addr)
  247. {
  248. uint32_t status;
  249. int rc;
  250. rc = raw_get_scom(scom, value, addr, &status);
  251. if (rc)
  252. return rc;
  253. rc = handle_fsi2pib_status(scom, status);
  254. if (rc)
  255. return rc;
  256. return handle_pib_status(scom,
  257. (status & SCOM_STATUS_PIB_RESP_MASK)
  258. >> SCOM_STATUS_PIB_RESP_SHIFT);
  259. }
  260. static ssize_t scom_read(struct file *filep, char __user *buf, size_t len,
  261. loff_t *offset)
  262. {
  263. struct scom_device *scom = filep->private_data;
  264. struct device *dev = &scom->fsi_dev->dev;
  265. uint64_t val;
  266. int rc;
  267. if (len != sizeof(uint64_t))
  268. return -EINVAL;
  269. mutex_lock(&scom->lock);
  270. if (scom->dead)
  271. rc = -ENODEV;
  272. else
  273. rc = get_scom(scom, &val, *offset);
  274. mutex_unlock(&scom->lock);
  275. if (rc) {
  276. dev_dbg(dev, "get_scom fail:%d\n", rc);
  277. return rc;
  278. }
  279. rc = copy_to_user(buf, &val, len);
  280. if (rc)
  281. dev_dbg(dev, "copy to user failed:%d\n", rc);
  282. return rc ? rc : len;
  283. }
  284. static ssize_t scom_write(struct file *filep, const char __user *buf,
  285. size_t len, loff_t *offset)
  286. {
  287. int rc;
  288. struct scom_device *scom = filep->private_data;
  289. struct device *dev = &scom->fsi_dev->dev;
  290. uint64_t val;
  291. if (len != sizeof(uint64_t))
  292. return -EINVAL;
  293. rc = copy_from_user(&val, buf, len);
  294. if (rc) {
  295. dev_dbg(dev, "copy from user failed:%d\n", rc);
  296. return -EINVAL;
  297. }
  298. mutex_lock(&scom->lock);
  299. if (scom->dead)
  300. rc = -ENODEV;
  301. else
  302. rc = put_scom(scom, val, *offset);
  303. mutex_unlock(&scom->lock);
  304. if (rc) {
  305. dev_dbg(dev, "put_scom failed with:%d\n", rc);
  306. return rc;
  307. }
  308. return len;
  309. }
  310. static loff_t scom_llseek(struct file *file, loff_t offset, int whence)
  311. {
  312. switch (whence) {
  313. case SEEK_CUR:
  314. break;
  315. case SEEK_SET:
  316. file->f_pos = offset;
  317. break;
  318. default:
  319. return -EINVAL;
  320. }
  321. return offset;
  322. }
  323. static void raw_convert_status(struct scom_access *acc, uint32_t status)
  324. {
  325. acc->pib_status = (status & SCOM_STATUS_PIB_RESP_MASK) >>
  326. SCOM_STATUS_PIB_RESP_SHIFT;
  327. acc->intf_errors = 0;
  328. if (status & SCOM_STATUS_PROTECTION)
  329. acc->intf_errors |= SCOM_INTF_ERR_PROTECTION;
  330. else if (status & SCOM_STATUS_PARITY)
  331. acc->intf_errors |= SCOM_INTF_ERR_PARITY;
  332. else if (status & SCOM_STATUS_PIB_ABORT)
  333. acc->intf_errors |= SCOM_INTF_ERR_ABORT;
  334. else if (status & SCOM_STATUS_ERR_SUMMARY)
  335. acc->intf_errors |= SCOM_INTF_ERR_UNKNOWN;
  336. }
  337. static int scom_raw_read(struct scom_device *scom, void __user *argp)
  338. {
  339. struct scom_access acc;
  340. uint32_t status;
  341. int rc;
  342. if (copy_from_user(&acc, argp, sizeof(struct scom_access)))
  343. return -EFAULT;
  344. rc = raw_get_scom(scom, &acc.data, acc.addr, &status);
  345. if (rc)
  346. return rc;
  347. raw_convert_status(&acc, status);
  348. if (copy_to_user(argp, &acc, sizeof(struct scom_access)))
  349. return -EFAULT;
  350. return 0;
  351. }
  352. static int scom_raw_write(struct scom_device *scom, void __user *argp)
  353. {
  354. u64 prev_data, mask, data;
  355. struct scom_access acc;
  356. uint32_t status;
  357. int rc;
  358. if (copy_from_user(&acc, argp, sizeof(struct scom_access)))
  359. return -EFAULT;
  360. if (acc.mask) {
  361. rc = raw_get_scom(scom, &prev_data, acc.addr, &status);
  362. if (rc)
  363. return rc;
  364. if (status & SCOM_STATUS_ANY_ERR)
  365. goto fail;
  366. mask = acc.mask;
  367. } else {
  368. prev_data = mask = -1ull;
  369. }
  370. data = (prev_data & ~mask) | (acc.data & mask);
  371. rc = raw_put_scom(scom, data, acc.addr, &status);
  372. if (rc)
  373. return rc;
  374. fail:
  375. raw_convert_status(&acc, status);
  376. if (copy_to_user(argp, &acc, sizeof(struct scom_access)))
  377. return -EFAULT;
  378. return 0;
  379. }
  380. static int scom_reset(struct scom_device *scom, void __user *argp)
  381. {
  382. uint32_t flags, dummy = -1;
  383. int rc = 0;
  384. if (get_user(flags, (__u32 __user *)argp))
  385. return -EFAULT;
  386. if (flags & SCOM_RESET_PIB)
  387. rc = fsi_device_write(scom->fsi_dev, SCOM_PIB_RESET_REG, &dummy,
  388. sizeof(uint32_t));
  389. if (!rc && (flags & (SCOM_RESET_PIB | SCOM_RESET_INTF)))
  390. rc = fsi_device_write(scom->fsi_dev, SCOM_FSI2PIB_RESET_REG, &dummy,
  391. sizeof(uint32_t));
  392. return rc;
  393. }
  394. static int scom_check(struct scom_device *scom, void __user *argp)
  395. {
  396. /* Still need to find out how to get "protected" */
  397. return put_user(SCOM_CHECK_SUPPORTED, (__u32 __user *)argp);
  398. }
  399. static long scom_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
  400. {
  401. struct scom_device *scom = file->private_data;
  402. void __user *argp = (void __user *)arg;
  403. int rc = -ENOTTY;
  404. mutex_lock(&scom->lock);
  405. if (scom->dead) {
  406. mutex_unlock(&scom->lock);
  407. return -ENODEV;
  408. }
  409. switch(cmd) {
  410. case FSI_SCOM_CHECK:
  411. rc = scom_check(scom, argp);
  412. break;
  413. case FSI_SCOM_READ:
  414. rc = scom_raw_read(scom, argp);
  415. break;
  416. case FSI_SCOM_WRITE:
  417. rc = scom_raw_write(scom, argp);
  418. break;
  419. case FSI_SCOM_RESET:
  420. rc = scom_reset(scom, argp);
  421. break;
  422. }
  423. mutex_unlock(&scom->lock);
  424. return rc;
  425. }
  426. static int scom_open(struct inode *inode, struct file *file)
  427. {
  428. struct scom_device *scom = container_of(inode->i_cdev, struct scom_device, cdev);
  429. file->private_data = scom;
  430. return 0;
  431. }
  432. static const struct file_operations scom_fops = {
  433. .owner = THIS_MODULE,
  434. .open = scom_open,
  435. .llseek = scom_llseek,
  436. .read = scom_read,
  437. .write = scom_write,
  438. .unlocked_ioctl = scom_ioctl,
  439. };
  440. static void scom_free(struct device *dev)
  441. {
  442. struct scom_device *scom = container_of(dev, struct scom_device, dev);
  443. put_device(&scom->fsi_dev->dev);
  444. kfree(scom);
  445. }
  446. static int scom_probe(struct device *dev)
  447. {
  448. struct fsi_device *fsi_dev = to_fsi_dev(dev);
  449. struct scom_device *scom;
  450. int rc, didx;
  451. scom = kzalloc(sizeof(*scom), GFP_KERNEL);
  452. if (!scom)
  453. return -ENOMEM;
  454. dev_set_drvdata(dev, scom);
  455. mutex_init(&scom->lock);
  456. /* Grab a reference to the device (parent of our cdev), we'll drop it later */
  457. if (!get_device(dev)) {
  458. kfree(scom);
  459. return -ENODEV;
  460. }
  461. scom->fsi_dev = fsi_dev;
  462. /* Create chardev for userspace access */
  463. scom->dev.type = &fsi_cdev_type;
  464. scom->dev.parent = dev;
  465. scom->dev.release = scom_free;
  466. device_initialize(&scom->dev);
  467. /* Allocate a minor in the FSI space */
  468. rc = fsi_get_new_minor(fsi_dev, fsi_dev_scom, &scom->dev.devt, &didx);
  469. if (rc)
  470. goto err;
  471. dev_set_name(&scom->dev, "scom%d", didx);
  472. cdev_init(&scom->cdev, &scom_fops);
  473. rc = cdev_device_add(&scom->cdev, &scom->dev);
  474. if (rc) {
  475. dev_err(dev, "Error %d creating char device %s\n",
  476. rc, dev_name(&scom->dev));
  477. goto err_free_minor;
  478. }
  479. return 0;
  480. err_free_minor:
  481. fsi_free_minor(scom->dev.devt);
  482. err:
  483. put_device(&scom->dev);
  484. return rc;
  485. }
  486. static int scom_remove(struct device *dev)
  487. {
  488. struct scom_device *scom = dev_get_drvdata(dev);
  489. mutex_lock(&scom->lock);
  490. scom->dead = true;
  491. mutex_unlock(&scom->lock);
  492. cdev_device_del(&scom->cdev, &scom->dev);
  493. fsi_free_minor(scom->dev.devt);
  494. put_device(&scom->dev);
  495. return 0;
  496. }
  497. static const struct of_device_id scom_of_ids[] = {
  498. { .compatible = "ibm,fsi2pib" },
  499. { }
  500. };
  501. MODULE_DEVICE_TABLE(of, scom_of_ids);
  502. static const struct fsi_device_id scom_ids[] = {
  503. {
  504. .engine_type = FSI_ENGID_SCOM,
  505. .version = FSI_VERSION_ANY,
  506. },
  507. { 0 }
  508. };
  509. static struct fsi_driver scom_drv = {
  510. .id_table = scom_ids,
  511. .drv = {
  512. .name = "scom",
  513. .bus = &fsi_bus_type,
  514. .of_match_table = scom_of_ids,
  515. .probe = scom_probe,
  516. .remove = scom_remove,
  517. }
  518. };
  519. static int scom_init(void)
  520. {
  521. return fsi_driver_register(&scom_drv);
  522. }
  523. static void scom_exit(void)
  524. {
  525. fsi_driver_unregister(&scom_drv);
  526. }
  527. module_init(scom_init);
  528. module_exit(scom_exit);
  529. MODULE_DESCRIPTION("SCOM FSI Client device driver");
  530. MODULE_LICENSE("GPL");