vcc.c 22 KB

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