vcc.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* vcc.c: sun4v virtual channel concentrator
  3. *
  4. * Copyright (C) 2017 Oracle. All rights reserved.
  5. */
  6. #include <linux/delay.h>
  7. #include <linux/interrupt.h>
  8. #include <linux/module.h>
  9. #include <linux/slab.h>
  10. #include <linux/sysfs.h>
  11. #include <linux/tty.h>
  12. #include <linux/tty_flip.h>
  13. #include <asm/vio.h>
  14. #include <asm/ldc.h>
  15. #define DRV_MODULE_NAME "vcc"
  16. #define DRV_MODULE_VERSION "1.1"
  17. #define DRV_MODULE_RELDATE "July 1, 2017"
  18. static char version[] =
  19. DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")";
  20. MODULE_DESCRIPTION("Sun LDOM virtual console concentrator driver");
  21. MODULE_LICENSE("GPL");
  22. MODULE_VERSION(DRV_MODULE_VERSION);
  23. struct vcc_port {
  24. struct vio_driver_state vio;
  25. spinlock_t lock;
  26. char *domain;
  27. struct tty_struct *tty; /* only populated while dev is open */
  28. unsigned long index; /* index into the vcc_table */
  29. u64 refcnt;
  30. bool excl_locked;
  31. bool removed;
  32. /* This buffer is required to support the tty write_room interface
  33. * and guarantee that any characters that the driver accepts will
  34. * be eventually sent, either immediately or later.
  35. */
  36. int chars_in_buffer;
  37. struct vio_vcc buffer;
  38. struct timer_list rx_timer;
  39. struct timer_list tx_timer;
  40. };
  41. /* Microseconds that thread will delay waiting for a vcc port ref */
  42. #define VCC_REF_DELAY 100
  43. #define VCC_MAX_PORTS 1024
  44. #define VCC_MINOR_START 0 /* must be zero */
  45. #define VCC_BUFF_LEN VIO_VCC_MTU_SIZE
  46. #define VCC_CTL_BREAK -1
  47. #define VCC_CTL_HUP -2
  48. static const char vcc_driver_name[] = "vcc";
  49. static const char vcc_device_node[] = "vcc";
  50. static struct tty_driver *vcc_tty_driver;
  51. static struct vcc_port *vcc_table[VCC_MAX_PORTS];
  52. static DEFINE_SPINLOCK(vcc_table_lock);
  53. int vcc_dbg;
  54. int vcc_dbg_ldc;
  55. int vcc_dbg_vio;
  56. module_param(vcc_dbg, uint, 0664);
  57. module_param(vcc_dbg_ldc, uint, 0664);
  58. module_param(vcc_dbg_vio, uint, 0664);
  59. #define VCC_DBG_DRV 0x1
  60. #define VCC_DBG_LDC 0x2
  61. #define VCC_DBG_PKT 0x4
  62. #define vccdbg(f, a...) \
  63. do { \
  64. if (vcc_dbg & VCC_DBG_DRV) \
  65. pr_info(f, ## a); \
  66. } while (0) \
  67. #define vccdbgl(l) \
  68. do { \
  69. if (vcc_dbg & VCC_DBG_LDC) \
  70. ldc_print(l); \
  71. } while (0) \
  72. #define vccdbgp(pkt) \
  73. do { \
  74. if (vcc_dbg & VCC_DBG_PKT) { \
  75. int i; \
  76. for (i = 0; i < pkt.tag.stype; i++) \
  77. pr_info("[%c]", pkt.data[i]); \
  78. } \
  79. } while (0) \
  80. /* Note: Be careful when adding flags to this line discipline. Don't
  81. * add anything that will cause echoing or we'll go into recursive
  82. * loop echoing chars back and forth with the console drivers.
  83. */
  84. static const struct ktermios vcc_tty_termios = {
  85. .c_iflag = IGNBRK | IGNPAR,
  86. .c_oflag = OPOST,
  87. .c_cflag = B38400 | CS8 | CREAD | HUPCL,
  88. .c_cc = INIT_C_CC,
  89. .c_ispeed = 38400,
  90. .c_ospeed = 38400
  91. };
  92. /**
  93. * vcc_table_add() - Add VCC port to the VCC table
  94. * @port: pointer to the VCC port
  95. *
  96. * Return: index of the port in the VCC table on success,
  97. * -1 on failure
  98. */
  99. static int vcc_table_add(struct vcc_port *port)
  100. {
  101. unsigned long flags;
  102. int i;
  103. spin_lock_irqsave(&vcc_table_lock, flags);
  104. for (i = VCC_MINOR_START; i < VCC_MAX_PORTS; i++) {
  105. if (!vcc_table[i]) {
  106. vcc_table[i] = port;
  107. break;
  108. }
  109. }
  110. spin_unlock_irqrestore(&vcc_table_lock, flags);
  111. if (i < VCC_MAX_PORTS)
  112. return i;
  113. else
  114. return -1;
  115. }
  116. /**
  117. * vcc_table_remove() - Removes a VCC port from the VCC table
  118. * @index: Index into the VCC table
  119. */
  120. static void vcc_table_remove(unsigned long index)
  121. {
  122. unsigned long flags;
  123. if (WARN_ON(index >= VCC_MAX_PORTS))
  124. return;
  125. spin_lock_irqsave(&vcc_table_lock, flags);
  126. vcc_table[index] = NULL;
  127. spin_unlock_irqrestore(&vcc_table_lock, flags);
  128. }
  129. /**
  130. * vcc_get() - Gets a reference to VCC port
  131. * @index: Index into the VCC table
  132. * @excl: Indicates if an exclusive access is requested
  133. *
  134. * Return: reference to the VCC port, if found
  135. * NULL, if port not found
  136. */
  137. static struct vcc_port *vcc_get(unsigned long index, bool excl)
  138. {
  139. struct vcc_port *port;
  140. unsigned long flags;
  141. try_again:
  142. spin_lock_irqsave(&vcc_table_lock, flags);
  143. port = vcc_table[index];
  144. if (!port) {
  145. spin_unlock_irqrestore(&vcc_table_lock, flags);
  146. return NULL;
  147. }
  148. if (!excl) {
  149. if (port->excl_locked) {
  150. spin_unlock_irqrestore(&vcc_table_lock, flags);
  151. udelay(VCC_REF_DELAY);
  152. goto try_again;
  153. }
  154. port->refcnt++;
  155. spin_unlock_irqrestore(&vcc_table_lock, flags);
  156. return port;
  157. }
  158. if (port->refcnt) {
  159. spin_unlock_irqrestore(&vcc_table_lock, flags);
  160. /* Threads wanting exclusive access will wait half the time,
  161. * probably giving them higher priority in the case of
  162. * multiple waiters.
  163. */
  164. udelay(VCC_REF_DELAY/2);
  165. goto try_again;
  166. }
  167. port->refcnt++;
  168. port->excl_locked = true;
  169. spin_unlock_irqrestore(&vcc_table_lock, flags);
  170. return port;
  171. }
  172. /**
  173. * vcc_put() - Returns a reference to VCC port
  174. * @port: pointer to VCC port
  175. * @excl: Indicates if the returned reference is an exclusive reference
  176. *
  177. * Note: It's the caller's responsibility to ensure the correct value
  178. * for the excl flag
  179. */
  180. static void vcc_put(struct vcc_port *port, bool excl)
  181. {
  182. unsigned long flags;
  183. if (!port)
  184. return;
  185. spin_lock_irqsave(&vcc_table_lock, flags);
  186. /* check if caller attempted to put with the wrong flags */
  187. if (WARN_ON((excl && !port->excl_locked) ||
  188. (!excl && port->excl_locked)))
  189. goto done;
  190. port->refcnt--;
  191. if (excl)
  192. port->excl_locked = false;
  193. done:
  194. spin_unlock_irqrestore(&vcc_table_lock, flags);
  195. }
  196. /**
  197. * vcc_get_ne() - Get a non-exclusive reference to VCC port
  198. * @index: Index into the VCC table
  199. *
  200. * Gets a non-exclusive reference to VCC port, if it's not removed
  201. *
  202. * Return: pointer to the VCC port, if found
  203. * NULL, if port not found
  204. */
  205. static struct vcc_port *vcc_get_ne(unsigned long index)
  206. {
  207. struct vcc_port *port;
  208. port = vcc_get(index, false);
  209. if (port && port->removed) {
  210. vcc_put(port, false);
  211. return NULL;
  212. }
  213. return port;
  214. }
  215. static void vcc_kick_rx(struct vcc_port *port)
  216. {
  217. struct vio_driver_state *vio = &port->vio;
  218. assert_spin_locked(&port->lock);
  219. if (!timer_pending(&port->rx_timer) && !port->removed) {
  220. disable_irq_nosync(vio->vdev->rx_irq);
  221. port->rx_timer.expires = (jiffies + 1);
  222. add_timer(&port->rx_timer);
  223. }
  224. }
  225. static void vcc_kick_tx(struct vcc_port *port)
  226. {
  227. assert_spin_locked(&port->lock);
  228. if (!timer_pending(&port->tx_timer) && !port->removed) {
  229. port->tx_timer.expires = (jiffies + 1);
  230. add_timer(&port->tx_timer);
  231. }
  232. }
  233. static int vcc_rx_check(struct tty_struct *tty, int size)
  234. {
  235. if (WARN_ON(!tty || !tty->port))
  236. return 1;
  237. /* tty_buffer_request_room won't sleep because it uses
  238. * GFP_ATOMIC flag to allocate buffer
  239. */
  240. if (test_bit(TTY_THROTTLED, &tty->flags) ||
  241. (tty_buffer_request_room(tty->port, VCC_BUFF_LEN) < VCC_BUFF_LEN))
  242. return 0;
  243. return 1;
  244. }
  245. static int vcc_rx(struct tty_struct *tty, char *buf, int size)
  246. {
  247. int len = 0;
  248. if (WARN_ON(!tty || !tty->port))
  249. return len;
  250. len = tty_insert_flip_string(tty->port, buf, size);
  251. if (len)
  252. tty_flip_buffer_push(tty->port);
  253. return len;
  254. }
  255. static int vcc_ldc_read(struct vcc_port *port)
  256. {
  257. struct vio_driver_state *vio = &port->vio;
  258. struct tty_struct *tty;
  259. struct vio_vcc pkt;
  260. int rv = 0;
  261. tty = port->tty;
  262. if (!tty) {
  263. rv = ldc_rx_reset(vio->lp);
  264. vccdbg("VCC: reset rx q: rv=%d\n", rv);
  265. goto done;
  266. }
  267. /* Read as long as LDC has incoming data. */
  268. while (1) {
  269. if (!vcc_rx_check(tty, VIO_VCC_MTU_SIZE)) {
  270. vcc_kick_rx(port);
  271. break;
  272. }
  273. vccdbgl(vio->lp);
  274. rv = ldc_read(vio->lp, &pkt, sizeof(pkt));
  275. if (rv <= 0)
  276. break;
  277. vccdbg("VCC: ldc_read()=%d\n", rv);
  278. vccdbg("TAG [%02x:%02x:%04x:%08x]\n",
  279. pkt.tag.type, pkt.tag.stype,
  280. pkt.tag.stype_env, pkt.tag.sid);
  281. if (pkt.tag.type == VIO_TYPE_DATA) {
  282. vccdbgp(pkt);
  283. /* vcc_rx_check ensures memory availability */
  284. vcc_rx(tty, pkt.data, pkt.tag.stype);
  285. } else {
  286. pr_err("VCC: unknown msg [%02x:%02x:%04x:%08x]\n",
  287. pkt.tag.type, pkt.tag.stype,
  288. pkt.tag.stype_env, pkt.tag.sid);
  289. rv = -ECONNRESET;
  290. break;
  291. }
  292. WARN_ON(rv != LDC_PACKET_SIZE);
  293. }
  294. done:
  295. return rv;
  296. }
  297. static void vcc_rx_timer(struct timer_list *t)
  298. {
  299. struct vcc_port *port = from_timer(port, t, rx_timer);
  300. struct vio_driver_state *vio;
  301. unsigned long flags;
  302. int rv;
  303. spin_lock_irqsave(&port->lock, flags);
  304. port->rx_timer.expires = 0;
  305. vio = &port->vio;
  306. enable_irq(vio->vdev->rx_irq);
  307. if (!port->tty || port->removed)
  308. goto done;
  309. rv = vcc_ldc_read(port);
  310. if (rv == -ECONNRESET)
  311. vio_conn_reset(vio);
  312. done:
  313. spin_unlock_irqrestore(&port->lock, flags);
  314. vcc_put(port, false);
  315. }
  316. static void vcc_tx_timer(struct timer_list *t)
  317. {
  318. struct vcc_port *port = from_timer(port, t, tx_timer);
  319. struct vio_vcc *pkt;
  320. unsigned long flags;
  321. int tosend = 0;
  322. int rv;
  323. spin_lock_irqsave(&port->lock, flags);
  324. port->tx_timer.expires = 0;
  325. if (!port->tty || port->removed)
  326. goto done;
  327. tosend = min(VCC_BUFF_LEN, port->chars_in_buffer);
  328. if (!tosend)
  329. goto done;
  330. pkt = &port->buffer;
  331. pkt->tag.type = VIO_TYPE_DATA;
  332. pkt->tag.stype = tosend;
  333. vccdbgl(port->vio.lp);
  334. rv = ldc_write(port->vio.lp, pkt, (VIO_TAG_SIZE + tosend));
  335. WARN_ON(!rv);
  336. if (rv < 0) {
  337. vccdbg("VCC: ldc_write()=%d\n", rv);
  338. vcc_kick_tx(port);
  339. } else {
  340. struct tty_struct *tty = port->tty;
  341. port->chars_in_buffer = 0;
  342. if (tty)
  343. tty_wakeup(tty);
  344. }
  345. done:
  346. spin_unlock_irqrestore(&port->lock, flags);
  347. vcc_put(port, false);
  348. }
  349. /**
  350. * vcc_event() - LDC event processing engine
  351. * @arg: VCC private data
  352. * @event: LDC event
  353. *
  354. * Handles LDC events for VCC
  355. */
  356. static void vcc_event(void *arg, int event)
  357. {
  358. struct vio_driver_state *vio;
  359. struct vcc_port *port;
  360. unsigned long flags;
  361. int rv;
  362. port = arg;
  363. vio = &port->vio;
  364. spin_lock_irqsave(&port->lock, flags);
  365. switch (event) {
  366. case LDC_EVENT_RESET:
  367. case LDC_EVENT_UP:
  368. vio_link_state_change(vio, event);
  369. break;
  370. case LDC_EVENT_DATA_READY:
  371. rv = vcc_ldc_read(port);
  372. if (rv == -ECONNRESET)
  373. vio_conn_reset(vio);
  374. break;
  375. default:
  376. pr_err("VCC: unexpected LDC event(%d)\n", event);
  377. }
  378. spin_unlock_irqrestore(&port->lock, flags);
  379. }
  380. static struct ldc_channel_config vcc_ldc_cfg = {
  381. .event = vcc_event,
  382. .mtu = VIO_VCC_MTU_SIZE,
  383. .mode = LDC_MODE_RAW,
  384. .debug = 0,
  385. };
  386. /* Ordered from largest major to lowest */
  387. static struct vio_version vcc_versions[] = {
  388. { .major = 1, .minor = 0 },
  389. };
  390. static struct tty_port_operations vcc_port_ops = { 0 };
  391. static ssize_t vcc_sysfs_domain_show(struct device *dev,
  392. struct device_attribute *attr,
  393. char *buf)
  394. {
  395. struct vcc_port *port;
  396. int rv;
  397. port = dev_get_drvdata(dev);
  398. if (!port)
  399. return -ENODEV;
  400. rv = scnprintf(buf, PAGE_SIZE, "%s\n", port->domain);
  401. return rv;
  402. }
  403. static int vcc_send_ctl(struct vcc_port *port, int ctl)
  404. {
  405. struct vio_vcc pkt;
  406. int rv;
  407. pkt.tag.type = VIO_TYPE_CTRL;
  408. pkt.tag.sid = ctl;
  409. pkt.tag.stype = 0;
  410. rv = ldc_write(port->vio.lp, &pkt, sizeof(pkt.tag));
  411. WARN_ON(!rv);
  412. vccdbg("VCC: ldc_write(%ld)=%d\n", sizeof(pkt.tag), rv);
  413. return rv;
  414. }
  415. static ssize_t vcc_sysfs_break_store(struct device *dev,
  416. struct device_attribute *attr,
  417. const char *buf, size_t count)
  418. {
  419. struct vcc_port *port;
  420. unsigned long flags;
  421. int rv = count;
  422. int brk;
  423. port = dev_get_drvdata(dev);
  424. if (!port)
  425. return -ENODEV;
  426. spin_lock_irqsave(&port->lock, flags);
  427. if (sscanf(buf, "%ud", &brk) != 1 || brk != 1)
  428. rv = -EINVAL;
  429. else if (vcc_send_ctl(port, VCC_CTL_BREAK) < 0)
  430. vcc_kick_tx(port);
  431. spin_unlock_irqrestore(&port->lock, flags);
  432. return rv;
  433. }
  434. static DEVICE_ATTR(domain, 0400, vcc_sysfs_domain_show, NULL);
  435. static DEVICE_ATTR(break, 0200, NULL, vcc_sysfs_break_store);
  436. static struct attribute *vcc_sysfs_entries[] = {
  437. &dev_attr_domain.attr,
  438. &dev_attr_break.attr,
  439. NULL
  440. };
  441. static struct attribute_group vcc_attribute_group = {
  442. .name = NULL,
  443. .attrs = vcc_sysfs_entries,
  444. };
  445. /**
  446. * vcc_probe() - Initialize VCC port
  447. * @vdev: Pointer to VIO device of the new VCC port
  448. * @id: VIO device ID
  449. *
  450. * Initializes a VCC port to receive serial console data from
  451. * the guest domain. Sets up a TTY end point on the control
  452. * domain. Sets up VIO/LDC link between the guest & control
  453. * domain endpoints.
  454. *
  455. * Return: status of the probe
  456. */
  457. static int vcc_probe(struct vio_dev *vdev, const struct vio_device_id *id)
  458. {
  459. struct mdesc_handle *hp;
  460. struct vcc_port *port;
  461. struct device *dev;
  462. const char *domain;
  463. char *name;
  464. u64 node;
  465. int rv;
  466. vccdbg("VCC: name=%s\n", dev_name(&vdev->dev));
  467. if (!vcc_tty_driver) {
  468. pr_err("VCC: TTY driver not registered\n");
  469. return -ENODEV;
  470. }
  471. port = kzalloc(sizeof(struct vcc_port), GFP_KERNEL);
  472. if (!port)
  473. return -ENOMEM;
  474. name = kstrdup(dev_name(&vdev->dev), GFP_KERNEL);
  475. rv = vio_driver_init(&port->vio, vdev, VDEV_CONSOLE_CON, vcc_versions,
  476. ARRAY_SIZE(vcc_versions), NULL, name);
  477. if (rv)
  478. goto free_port;
  479. port->vio.debug = vcc_dbg_vio;
  480. vcc_ldc_cfg.debug = vcc_dbg_ldc;
  481. rv = vio_ldc_alloc(&port->vio, &vcc_ldc_cfg, port);
  482. if (rv)
  483. goto free_port;
  484. spin_lock_init(&port->lock);
  485. port->index = vcc_table_add(port);
  486. if (port->index == -1) {
  487. pr_err("VCC: no more TTY indices left for allocation\n");
  488. rv = -ENOMEM;
  489. goto free_ldc;
  490. }
  491. /* Register the device using VCC table index as TTY index */
  492. dev = tty_register_device(vcc_tty_driver, port->index, &vdev->dev);
  493. if (IS_ERR(dev)) {
  494. rv = PTR_ERR(dev);
  495. goto free_table;
  496. }
  497. hp = mdesc_grab();
  498. node = vio_vdev_node(hp, vdev);
  499. if (node == MDESC_NODE_NULL) {
  500. rv = -ENXIO;
  501. mdesc_release(hp);
  502. goto unreg_tty;
  503. }
  504. domain = mdesc_get_property(hp, node, "vcc-domain-name", NULL);
  505. if (!domain) {
  506. rv = -ENXIO;
  507. mdesc_release(hp);
  508. goto unreg_tty;
  509. }
  510. port->domain = kstrdup(domain, GFP_KERNEL);
  511. mdesc_release(hp);
  512. rv = sysfs_create_group(&vdev->dev.kobj, &vcc_attribute_group);
  513. if (rv)
  514. goto free_domain;
  515. timer_setup(&port->rx_timer, vcc_rx_timer, 0);
  516. timer_setup(&port->tx_timer, vcc_tx_timer, 0);
  517. dev_set_drvdata(&vdev->dev, port);
  518. /* It's possible to receive IRQs in the middle of vio_port_up. Disable
  519. * IRQs until the port is up.
  520. */
  521. disable_irq_nosync(vdev->rx_irq);
  522. vio_port_up(&port->vio);
  523. enable_irq(vdev->rx_irq);
  524. return 0;
  525. free_domain:
  526. kfree(port->domain);
  527. unreg_tty:
  528. tty_unregister_device(vcc_tty_driver, port->index);
  529. free_table:
  530. vcc_table_remove(port->index);
  531. free_ldc:
  532. vio_ldc_free(&port->vio);
  533. free_port:
  534. kfree(name);
  535. kfree(port);
  536. return rv;
  537. }
  538. /**
  539. * vcc_remove() - Terminate a VCC port
  540. * @vdev: Pointer to VIO device of the VCC port
  541. *
  542. * Terminates a VCC port. Sets up the teardown of TTY and
  543. * VIO/LDC link between guest and primary domains.
  544. *
  545. * Return: status of removal
  546. */
  547. static int vcc_remove(struct vio_dev *vdev)
  548. {
  549. struct vcc_port *port = dev_get_drvdata(&vdev->dev);
  550. if (!port)
  551. return -ENODEV;
  552. del_timer_sync(&port->rx_timer);
  553. del_timer_sync(&port->tx_timer);
  554. /* If there's a process with the device open, do a synchronous
  555. * hangup of the TTY. This *may* cause the process to call close
  556. * asynchronously, but it's not guaranteed.
  557. */
  558. if (port->tty)
  559. tty_vhangup(port->tty);
  560. /* Get exclusive reference to VCC, ensures that there are no other
  561. * clients to this port
  562. */
  563. port = vcc_get(port->index, true);
  564. if (WARN_ON(!port))
  565. return -ENODEV;
  566. tty_unregister_device(vcc_tty_driver, port->index);
  567. del_timer_sync(&port->vio.timer);
  568. vio_ldc_free(&port->vio);
  569. sysfs_remove_group(&vdev->dev.kobj, &vcc_attribute_group);
  570. dev_set_drvdata(&vdev->dev, NULL);
  571. if (port->tty) {
  572. port->removed = true;
  573. vcc_put(port, true);
  574. } else {
  575. vcc_table_remove(port->index);
  576. kfree(port->vio.name);
  577. kfree(port->domain);
  578. kfree(port);
  579. }
  580. return 0;
  581. }
  582. static const struct vio_device_id vcc_match[] = {
  583. {
  584. .type = "vcc-port",
  585. },
  586. {},
  587. };
  588. MODULE_DEVICE_TABLE(vio, vcc_match);
  589. static struct vio_driver vcc_driver = {
  590. .id_table = vcc_match,
  591. .probe = vcc_probe,
  592. .remove = vcc_remove,
  593. .name = "vcc",
  594. };
  595. static int vcc_open(struct tty_struct *tty, struct file *vcc_file)
  596. {
  597. struct vcc_port *port;
  598. if (unlikely(!tty)) {
  599. pr_err("VCC: open: Invalid TTY handle\n");
  600. return -ENXIO;
  601. }
  602. if (tty->count > 1)
  603. return -EBUSY;
  604. port = vcc_get_ne(tty->index);
  605. if (unlikely(!port)) {
  606. pr_err("VCC: open: Failed to find VCC port\n");
  607. return -ENODEV;
  608. }
  609. if (unlikely(!port->vio.lp)) {
  610. pr_err("VCC: open: LDC channel not configured\n");
  611. vcc_put(port, false);
  612. return -EPIPE;
  613. }
  614. vccdbgl(port->vio.lp);
  615. vcc_put(port, false);
  616. if (unlikely(!tty->port)) {
  617. pr_err("VCC: open: TTY port not found\n");
  618. return -ENXIO;
  619. }
  620. if (unlikely(!tty->port->ops)) {
  621. pr_err("VCC: open: TTY ops not defined\n");
  622. return -ENXIO;
  623. }
  624. return tty_port_open(tty->port, tty, vcc_file);
  625. }
  626. static void vcc_close(struct tty_struct *tty, struct file *vcc_file)
  627. {
  628. if (unlikely(!tty)) {
  629. pr_err("VCC: close: Invalid TTY handle\n");
  630. return;
  631. }
  632. if (unlikely(tty->count > 1))
  633. return;
  634. if (unlikely(!tty->port)) {
  635. pr_err("VCC: close: TTY port not found\n");
  636. return;
  637. }
  638. tty_port_close(tty->port, tty, vcc_file);
  639. }
  640. static void vcc_ldc_hup(struct vcc_port *port)
  641. {
  642. unsigned long flags;
  643. spin_lock_irqsave(&port->lock, flags);
  644. if (vcc_send_ctl(port, VCC_CTL_HUP) < 0)
  645. vcc_kick_tx(port);
  646. spin_unlock_irqrestore(&port->lock, flags);
  647. }
  648. static void vcc_hangup(struct tty_struct *tty)
  649. {
  650. struct vcc_port *port;
  651. if (unlikely(!tty)) {
  652. pr_err("VCC: hangup: Invalid TTY handle\n");
  653. return;
  654. }
  655. port = vcc_get_ne(tty->index);
  656. if (unlikely(!port)) {
  657. pr_err("VCC: hangup: Failed to find VCC port\n");
  658. return;
  659. }
  660. if (unlikely(!tty->port)) {
  661. pr_err("VCC: hangup: TTY port not found\n");
  662. vcc_put(port, false);
  663. return;
  664. }
  665. vcc_ldc_hup(port);
  666. vcc_put(port, false);
  667. tty_port_hangup(tty->port);
  668. }
  669. static int vcc_write(struct tty_struct *tty, const unsigned char *buf,
  670. int count)
  671. {
  672. struct vcc_port *port;
  673. struct vio_vcc *pkt;
  674. unsigned long flags;
  675. int total_sent = 0;
  676. int tosend = 0;
  677. int rv = -EINVAL;
  678. if (unlikely(!tty)) {
  679. pr_err("VCC: write: Invalid TTY handle\n");
  680. return -ENXIO;
  681. }
  682. port = vcc_get_ne(tty->index);
  683. if (unlikely(!port)) {
  684. pr_err("VCC: write: Failed to find VCC port");
  685. return -ENODEV;
  686. }
  687. spin_lock_irqsave(&port->lock, flags);
  688. pkt = &port->buffer;
  689. pkt->tag.type = VIO_TYPE_DATA;
  690. while (count > 0) {
  691. /* Minimum of data to write and space available */
  692. tosend = min(count, (VCC_BUFF_LEN - port->chars_in_buffer));
  693. if (!tosend)
  694. break;
  695. memcpy(&pkt->data[port->chars_in_buffer], &buf[total_sent],
  696. tosend);
  697. port->chars_in_buffer += tosend;
  698. pkt->tag.stype = tosend;
  699. vccdbg("TAG [%02x:%02x:%04x:%08x]\n", pkt->tag.type,
  700. pkt->tag.stype, pkt->tag.stype_env, pkt->tag.sid);
  701. vccdbg("DATA [%s]\n", pkt->data);
  702. vccdbgl(port->vio.lp);
  703. /* Since we know we have enough room in VCC buffer for tosend
  704. * we record that it was sent regardless of whether the
  705. * hypervisor actually took it because we have it buffered.
  706. */
  707. rv = ldc_write(port->vio.lp, pkt, (VIO_TAG_SIZE + tosend));
  708. vccdbg("VCC: write: ldc_write(%d)=%d\n",
  709. (VIO_TAG_SIZE + tosend), rv);
  710. total_sent += tosend;
  711. count -= tosend;
  712. if (rv < 0) {
  713. vcc_kick_tx(port);
  714. break;
  715. }
  716. port->chars_in_buffer = 0;
  717. }
  718. spin_unlock_irqrestore(&port->lock, flags);
  719. vcc_put(port, false);
  720. vccdbg("VCC: write: total=%d rv=%d", total_sent, rv);
  721. return total_sent ? total_sent : rv;
  722. }
  723. static int vcc_write_room(struct tty_struct *tty)
  724. {
  725. struct vcc_port *port;
  726. u64 num;
  727. if (unlikely(!tty)) {
  728. pr_err("VCC: write_room: Invalid TTY handle\n");
  729. return -ENXIO;
  730. }
  731. port = vcc_get_ne(tty->index);
  732. if (unlikely(!port)) {
  733. pr_err("VCC: write_room: Failed to find VCC port\n");
  734. return -ENODEV;
  735. }
  736. num = VCC_BUFF_LEN - port->chars_in_buffer;
  737. vcc_put(port, false);
  738. return num;
  739. }
  740. static int vcc_chars_in_buffer(struct tty_struct *tty)
  741. {
  742. struct vcc_port *port;
  743. u64 num;
  744. if (unlikely(!tty)) {
  745. pr_err("VCC: chars_in_buffer: Invalid TTY handle\n");
  746. return -ENXIO;
  747. }
  748. port = vcc_get_ne(tty->index);
  749. if (unlikely(!port)) {
  750. pr_err("VCC: chars_in_buffer: Failed to find VCC port\n");
  751. return -ENODEV;
  752. }
  753. num = port->chars_in_buffer;
  754. vcc_put(port, false);
  755. return num;
  756. }
  757. static int vcc_break_ctl(struct tty_struct *tty, int state)
  758. {
  759. struct vcc_port *port;
  760. unsigned long flags;
  761. if (unlikely(!tty)) {
  762. pr_err("VCC: break_ctl: Invalid TTY handle\n");
  763. return -ENXIO;
  764. }
  765. port = vcc_get_ne(tty->index);
  766. if (unlikely(!port)) {
  767. pr_err("VCC: break_ctl: Failed to find VCC port\n");
  768. return -ENODEV;
  769. }
  770. /* Turn off break */
  771. if (state == 0) {
  772. vcc_put(port, false);
  773. return 0;
  774. }
  775. spin_lock_irqsave(&port->lock, flags);
  776. if (vcc_send_ctl(port, VCC_CTL_BREAK) < 0)
  777. vcc_kick_tx(port);
  778. spin_unlock_irqrestore(&port->lock, flags);
  779. vcc_put(port, false);
  780. return 0;
  781. }
  782. static int vcc_install(struct tty_driver *driver, struct tty_struct *tty)
  783. {
  784. struct vcc_port *port_vcc;
  785. struct tty_port *port_tty;
  786. int ret;
  787. if (unlikely(!tty)) {
  788. pr_err("VCC: install: Invalid TTY handle\n");
  789. return -ENXIO;
  790. }
  791. if (tty->index >= VCC_MAX_PORTS)
  792. return -EINVAL;
  793. ret = tty_standard_install(driver, tty);
  794. if (ret)
  795. return ret;
  796. port_tty = kzalloc(sizeof(struct tty_port), GFP_KERNEL);
  797. if (!port_tty)
  798. return -ENOMEM;
  799. port_vcc = vcc_get(tty->index, true);
  800. if (!port_vcc) {
  801. pr_err("VCC: install: Failed to find VCC port\n");
  802. tty->port = NULL;
  803. kfree(port_tty);
  804. return -ENODEV;
  805. }
  806. tty_port_init(port_tty);
  807. port_tty->ops = &vcc_port_ops;
  808. tty->port = port_tty;
  809. port_vcc->tty = tty;
  810. vcc_put(port_vcc, true);
  811. return 0;
  812. }
  813. static void vcc_cleanup(struct tty_struct *tty)
  814. {
  815. struct vcc_port *port;
  816. if (unlikely(!tty)) {
  817. pr_err("VCC: cleanup: Invalid TTY handle\n");
  818. return;
  819. }
  820. port = vcc_get(tty->index, true);
  821. if (port) {
  822. port->tty = NULL;
  823. if (port->removed) {
  824. vcc_table_remove(tty->index);
  825. kfree(port->vio.name);
  826. kfree(port->domain);
  827. kfree(port);
  828. } else {
  829. vcc_put(port, true);
  830. }
  831. }
  832. tty_port_destroy(tty->port);
  833. kfree(tty->port);
  834. tty->port = NULL;
  835. }
  836. static const struct tty_operations vcc_ops = {
  837. .open = vcc_open,
  838. .close = vcc_close,
  839. .hangup = vcc_hangup,
  840. .write = vcc_write,
  841. .write_room = vcc_write_room,
  842. .chars_in_buffer = vcc_chars_in_buffer,
  843. .break_ctl = vcc_break_ctl,
  844. .install = vcc_install,
  845. .cleanup = vcc_cleanup,
  846. };
  847. #define VCC_TTY_FLAGS (TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_REAL_RAW)
  848. static int vcc_tty_init(void)
  849. {
  850. int rv;
  851. pr_info("VCC: %s\n", version);
  852. vcc_tty_driver = tty_alloc_driver(VCC_MAX_PORTS, VCC_TTY_FLAGS);
  853. if (IS_ERR(vcc_tty_driver)) {
  854. pr_err("VCC: TTY driver alloc failed\n");
  855. return PTR_ERR(vcc_tty_driver);
  856. }
  857. vcc_tty_driver->driver_name = vcc_driver_name;
  858. vcc_tty_driver->name = vcc_device_node;
  859. vcc_tty_driver->minor_start = VCC_MINOR_START;
  860. vcc_tty_driver->type = TTY_DRIVER_TYPE_SYSTEM;
  861. vcc_tty_driver->init_termios = vcc_tty_termios;
  862. tty_set_operations(vcc_tty_driver, &vcc_ops);
  863. rv = tty_register_driver(vcc_tty_driver);
  864. if (rv) {
  865. pr_err("VCC: TTY driver registration failed\n");
  866. put_tty_driver(vcc_tty_driver);
  867. vcc_tty_driver = NULL;
  868. return rv;
  869. }
  870. vccdbg("VCC: TTY driver registered\n");
  871. return 0;
  872. }
  873. static void vcc_tty_exit(void)
  874. {
  875. tty_unregister_driver(vcc_tty_driver);
  876. put_tty_driver(vcc_tty_driver);
  877. vccdbg("VCC: TTY driver unregistered\n");
  878. vcc_tty_driver = NULL;
  879. }
  880. static int __init vcc_init(void)
  881. {
  882. int rv;
  883. rv = vcc_tty_init();
  884. if (rv) {
  885. pr_err("VCC: TTY init failed\n");
  886. return rv;
  887. }
  888. rv = vio_register_driver(&vcc_driver);
  889. if (rv) {
  890. pr_err("VCC: VIO driver registration failed\n");
  891. vcc_tty_exit();
  892. } else {
  893. vccdbg("VCC: VIO driver registered successfully\n");
  894. }
  895. return rv;
  896. }
  897. static void __exit vcc_exit(void)
  898. {
  899. vio_unregister_driver(&vcc_driver);
  900. vccdbg("VCC: VIO driver unregistered\n");
  901. vcc_tty_exit();
  902. vccdbg("VCC: TTY driver unregistered\n");
  903. }
  904. module_init(vcc_init);
  905. module_exit(vcc_exit);