fsi-scom.c 14 KB

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