fsi-scom.c 16 KB

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