vcc.c 23 KB

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