fsi-scom.c 16 KB

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