vcc.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143
  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. rv = -ENOMEM;
  488. goto free_ldc;
  489. }
  490. /* Register the device using VCC table index as TTY index */
  491. dev = tty_register_device(vcc_tty_driver, port->index, &vdev->dev);
  492. if (IS_ERR(dev)) {
  493. rv = PTR_ERR(dev);
  494. goto free_table;
  495. }
  496. hp = mdesc_grab();
  497. node = vio_vdev_node(hp, vdev);
  498. if (node == MDESC_NODE_NULL) {
  499. rv = -ENXIO;
  500. mdesc_release(hp);
  501. goto unreg_tty;
  502. }
  503. domain = mdesc_get_property(hp, node, "vcc-domain-name", NULL);
  504. if (!domain) {
  505. rv = -ENXIO;
  506. mdesc_release(hp);
  507. goto unreg_tty;
  508. }
  509. port->domain = kstrdup(domain, GFP_KERNEL);
  510. mdesc_release(hp);
  511. rv = sysfs_create_group(&vdev->dev.kobj, &vcc_attribute_group);
  512. if (rv)
  513. goto free_domain;
  514. timer_setup(&port->rx_timer, vcc_rx_timer, 0);
  515. timer_setup(&port->tx_timer, vcc_tx_timer, 0);
  516. dev_set_drvdata(&vdev->dev, port);
  517. /* It's possible to receive IRQs in the middle of vio_port_up. Disable
  518. * IRQs until the port is up.
  519. */
  520. disable_irq_nosync(vdev->rx_irq);
  521. vio_port_up(&port->vio);
  522. enable_irq(vdev->rx_irq);
  523. return 0;
  524. free_domain:
  525. kfree(port->domain);
  526. unreg_tty:
  527. tty_unregister_device(vcc_tty_driver, port->index);
  528. free_table:
  529. vcc_table_remove(port->index);
  530. free_ldc:
  531. vio_ldc_free(&port->vio);
  532. free_port:
  533. kfree(name);
  534. kfree(port);
  535. return rv;
  536. }
  537. /**
  538. * vcc_remove() - Terminate a VCC port
  539. * @vdev: Pointer to VIO device of the VCC port
  540. *
  541. * Terminates a VCC port. Sets up the teardown of TTY and
  542. * VIO/LDC link between guest and primary domains.
  543. *
  544. * Return: status of removal
  545. */
  546. static int vcc_remove(struct vio_dev *vdev)
  547. {
  548. struct vcc_port *port = dev_get_drvdata(&vdev->dev);
  549. if (!port)
  550. return -ENODEV;
  551. del_timer_sync(&port->rx_timer);
  552. del_timer_sync(&port->tx_timer);
  553. /* If there's a process with the device open, do a synchronous
  554. * hangup of the TTY. This *may* cause the process to call close
  555. * asynchronously, but it's not guaranteed.
  556. */
  557. if (port->tty)
  558. tty_vhangup(port->tty);
  559. /* Get exclusive reference to VCC, ensures that there are no other
  560. * clients to this port
  561. */
  562. port = vcc_get(port->index, true);
  563. if (WARN_ON(!port))
  564. return -ENODEV;
  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. return 0;
  580. }
  581. static const struct vio_device_id vcc_match[] = {
  582. {
  583. .type = "vcc-port",
  584. },
  585. {},
  586. };
  587. MODULE_DEVICE_TABLE(vio, vcc_match);
  588. static struct vio_driver vcc_driver = {
  589. .id_table = vcc_match,
  590. .probe = vcc_probe,
  591. .remove = vcc_remove,
  592. .name = "vcc",
  593. };
  594. static int vcc_open(struct tty_struct *tty, struct file *vcc_file)
  595. {
  596. struct vcc_port *port;
  597. if (unlikely(!tty)) {
  598. pr_err("VCC: open: Invalid TTY handle\n");
  599. return -ENXIO;
  600. }
  601. if (tty->count > 1)
  602. return -EBUSY;
  603. port = vcc_get_ne(tty->index);
  604. if (unlikely(!port)) {
  605. pr_err("VCC: open: Failed to find VCC port\n");
  606. return -ENODEV;
  607. }
  608. if (unlikely(!port->vio.lp)) {
  609. pr_err("VCC: open: LDC channel not configured\n");
  610. vcc_put(port, false);
  611. return -EPIPE;
  612. }
  613. vccdbgl(port->vio.lp);
  614. vcc_put(port, false);
  615. if (unlikely(!tty->port)) {
  616. pr_err("VCC: open: TTY port not found\n");
  617. return -ENXIO;
  618. }
  619. if (unlikely(!tty->port->ops)) {
  620. pr_err("VCC: open: TTY ops not defined\n");
  621. return -ENXIO;
  622. }
  623. return tty_port_open(tty->port, tty, vcc_file);
  624. }
  625. static void vcc_close(struct tty_struct *tty, struct file *vcc_file)
  626. {
  627. if (unlikely(!tty)) {
  628. pr_err("VCC: close: Invalid TTY handle\n");
  629. return;
  630. }
  631. if (unlikely(tty->count > 1))
  632. return;
  633. if (unlikely(!tty->port)) {
  634. pr_err("VCC: close: TTY port not found\n");
  635. return;
  636. }
  637. tty_port_close(tty->port, tty, vcc_file);
  638. }
  639. static void vcc_ldc_hup(struct vcc_port *port)
  640. {
  641. unsigned long flags;
  642. spin_lock_irqsave(&port->lock, flags);
  643. if (vcc_send_ctl(port, VCC_CTL_HUP) < 0)
  644. vcc_kick_tx(port);
  645. spin_unlock_irqrestore(&port->lock, flags);
  646. }
  647. static void vcc_hangup(struct tty_struct *tty)
  648. {
  649. struct vcc_port *port;
  650. if (unlikely(!tty)) {
  651. pr_err("VCC: hangup: Invalid TTY handle\n");
  652. return;
  653. }
  654. port = vcc_get_ne(tty->index);
  655. if (unlikely(!port)) {
  656. pr_err("VCC: hangup: Failed to find VCC port\n");
  657. return;
  658. }
  659. if (unlikely(!tty->port)) {
  660. pr_err("VCC: hangup: TTY port not found\n");
  661. vcc_put(port, false);
  662. return;
  663. }
  664. vcc_ldc_hup(port);
  665. vcc_put(port, false);
  666. tty_port_hangup(tty->port);
  667. }
  668. static int vcc_write(struct tty_struct *tty, const unsigned char *buf,
  669. int count)
  670. {
  671. struct vcc_port *port;
  672. struct vio_vcc *pkt;
  673. unsigned long flags;
  674. int total_sent = 0;
  675. int tosend = 0;
  676. int rv = -EINVAL;
  677. if (unlikely(!tty)) {
  678. pr_err("VCC: write: Invalid TTY handle\n");
  679. return -ENXIO;
  680. }
  681. port = vcc_get_ne(tty->index);
  682. if (unlikely(!port)) {
  683. pr_err("VCC: write: Failed to find VCC port");
  684. return -ENODEV;
  685. }
  686. spin_lock_irqsave(&port->lock, flags);
  687. pkt = &port->buffer;
  688. pkt->tag.type = VIO_TYPE_DATA;
  689. while (count > 0) {
  690. /* Minimum of data to write and space available */
  691. tosend = min(count, (VCC_BUFF_LEN - port->chars_in_buffer));
  692. if (!tosend)
  693. break;
  694. memcpy(&pkt->data[port->chars_in_buffer], &buf[total_sent],
  695. tosend);
  696. port->chars_in_buffer += tosend;
  697. pkt->tag.stype = tosend;
  698. vccdbg("TAG [%02x:%02x:%04x:%08x]\n", pkt->tag.type,
  699. pkt->tag.stype, pkt->tag.stype_env, pkt->tag.sid);
  700. vccdbg("DATA [%s]\n", pkt->data);
  701. vccdbgl(port->vio.lp);
  702. /* Since we know we have enough room in VCC buffer for tosend
  703. * we record that it was sent regardless of whether the
  704. * hypervisor actually took it because we have it buffered.
  705. */
  706. rv = ldc_write(port->vio.lp, pkt, (VIO_TAG_SIZE + tosend));
  707. vccdbg("VCC: write: ldc_write(%d)=%d\n",
  708. (VIO_TAG_SIZE + tosend), rv);
  709. total_sent += tosend;
  710. count -= tosend;
  711. if (rv < 0) {
  712. vcc_kick_tx(port);
  713. break;
  714. }
  715. port->chars_in_buffer = 0;
  716. }
  717. spin_unlock_irqrestore(&port->lock, flags);
  718. vcc_put(port, false);
  719. vccdbg("VCC: write: total=%d rv=%d", total_sent, rv);
  720. return total_sent ? total_sent : rv;
  721. }
  722. static int vcc_write_room(struct tty_struct *tty)
  723. {
  724. struct vcc_port *port;
  725. u64 num;
  726. if (unlikely(!tty)) {
  727. pr_err("VCC: write_room: Invalid TTY handle\n");
  728. return -ENXIO;
  729. }
  730. port = vcc_get_ne(tty->index);
  731. if (unlikely(!port)) {
  732. pr_err("VCC: write_room: Failed to find VCC port\n");
  733. return -ENODEV;
  734. }
  735. num = VCC_BUFF_LEN - port->chars_in_buffer;
  736. vcc_put(port, false);
  737. return num;
  738. }
  739. static int vcc_chars_in_buffer(struct tty_struct *tty)
  740. {
  741. struct vcc_port *port;
  742. u64 num;
  743. if (unlikely(!tty)) {
  744. pr_err("VCC: chars_in_buffer: Invalid TTY handle\n");
  745. return -ENXIO;
  746. }
  747. port = vcc_get_ne(tty->index);
  748. if (unlikely(!port)) {
  749. pr_err("VCC: chars_in_buffer: Failed to find VCC port\n");
  750. return -ENODEV;
  751. }
  752. num = port->chars_in_buffer;
  753. vcc_put(port, false);
  754. return num;
  755. }
  756. static int vcc_break_ctl(struct tty_struct *tty, int state)
  757. {
  758. struct vcc_port *port;
  759. unsigned long flags;
  760. if (unlikely(!tty)) {
  761. pr_err("VCC: break_ctl: Invalid TTY handle\n");
  762. return -ENXIO;
  763. }
  764. port = vcc_get_ne(tty->index);
  765. if (unlikely(!port)) {
  766. pr_err("VCC: break_ctl: Failed to find VCC port\n");
  767. return -ENODEV;
  768. }
  769. /* Turn off break */
  770. if (state == 0) {
  771. vcc_put(port, false);
  772. return 0;
  773. }
  774. spin_lock_irqsave(&port->lock, flags);
  775. if (vcc_send_ctl(port, VCC_CTL_BREAK) < 0)
  776. vcc_kick_tx(port);
  777. spin_unlock_irqrestore(&port->lock, flags);
  778. vcc_put(port, false);
  779. return 0;
  780. }
  781. static int vcc_install(struct tty_driver *driver, struct tty_struct *tty)
  782. {
  783. struct vcc_port *port_vcc;
  784. struct tty_port *port_tty;
  785. int ret;
  786. if (unlikely(!tty)) {
  787. pr_err("VCC: install: Invalid TTY handle\n");
  788. return -ENXIO;
  789. }
  790. if (tty->index >= VCC_MAX_PORTS)
  791. return -EINVAL;
  792. ret = tty_standard_install(driver, tty);
  793. if (ret)
  794. return ret;
  795. port_tty = kzalloc(sizeof(struct tty_port), GFP_KERNEL);
  796. if (!port_tty)
  797. return -ENOMEM;
  798. port_vcc = vcc_get(tty->index, true);
  799. if (!port_vcc) {
  800. pr_err("VCC: install: Failed to find VCC port\n");
  801. tty->port = NULL;
  802. kfree(port_tty);
  803. return -ENODEV;
  804. }
  805. tty_port_init(port_tty);
  806. port_tty->ops = &vcc_port_ops;
  807. tty->port = port_tty;
  808. port_vcc->tty = tty;
  809. vcc_put(port_vcc, true);
  810. return 0;
  811. }
  812. static void vcc_cleanup(struct tty_struct *tty)
  813. {
  814. struct vcc_port *port;
  815. if (unlikely(!tty)) {
  816. pr_err("VCC: cleanup: Invalid TTY handle\n");
  817. return;
  818. }
  819. port = vcc_get(tty->index, true);
  820. if (port) {
  821. port->tty = NULL;
  822. if (port->removed) {
  823. vcc_table_remove(tty->index);
  824. kfree(port->vio.name);
  825. kfree(port->domain);
  826. kfree(port);
  827. } else {
  828. vcc_put(port, true);
  829. }
  830. }
  831. tty_port_destroy(tty->port);
  832. kfree(tty->port);
  833. tty->port = NULL;
  834. }
  835. static const struct tty_operations vcc_ops = {
  836. .open = vcc_open,
  837. .close = vcc_close,
  838. .hangup = vcc_hangup,
  839. .write = vcc_write,
  840. .write_room = vcc_write_room,
  841. .chars_in_buffer = vcc_chars_in_buffer,
  842. .break_ctl = vcc_break_ctl,
  843. .install = vcc_install,
  844. .cleanup = vcc_cleanup,
  845. };
  846. #define VCC_TTY_FLAGS (TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_REAL_RAW)
  847. static int vcc_tty_init(void)
  848. {
  849. int rv;
  850. pr_info("VCC: %s\n", version);
  851. vcc_tty_driver = tty_alloc_driver(VCC_MAX_PORTS, VCC_TTY_FLAGS);
  852. if (IS_ERR(vcc_tty_driver)) {
  853. pr_err("VCC: TTY driver alloc failed\n");
  854. return PTR_ERR(vcc_tty_driver);
  855. }
  856. vcc_tty_driver->driver_name = vcc_driver_name;
  857. vcc_tty_driver->name = vcc_device_node;
  858. vcc_tty_driver->minor_start = VCC_MINOR_START;
  859. vcc_tty_driver->type = TTY_DRIVER_TYPE_SYSTEM;
  860. vcc_tty_driver->init_termios = vcc_tty_termios;
  861. tty_set_operations(vcc_tty_driver, &vcc_ops);
  862. rv = tty_register_driver(vcc_tty_driver);
  863. if (rv) {
  864. pr_err("VCC: TTY driver registration failed\n");
  865. put_tty_driver(vcc_tty_driver);
  866. vcc_tty_driver = NULL;
  867. return rv;
  868. }
  869. vccdbg("VCC: TTY driver registered\n");
  870. return 0;
  871. }
  872. static void vcc_tty_exit(void)
  873. {
  874. tty_unregister_driver(vcc_tty_driver);
  875. put_tty_driver(vcc_tty_driver);
  876. vccdbg("VCC: TTY driver unregistered\n");
  877. vcc_tty_driver = NULL;
  878. }
  879. static int __init vcc_init(void)
  880. {
  881. int rv;
  882. rv = vcc_tty_init();
  883. if (rv) {
  884. pr_err("VCC: TTY init failed\n");
  885. return rv;
  886. }
  887. rv = vio_register_driver(&vcc_driver);
  888. if (rv) {
  889. pr_err("VCC: VIO driver registration failed\n");
  890. vcc_tty_exit();
  891. } else {
  892. vccdbg("VCC: VIO driver registered successfully\n");
  893. }
  894. return rv;
  895. }
  896. static void __exit vcc_exit(void)
  897. {
  898. vio_unregister_driver(&vcc_driver);
  899. vccdbg("VCC: VIO driver unregistered\n");
  900. vcc_tty_exit();
  901. vccdbg("VCC: TTY driver unregistered\n");
  902. }
  903. module_init(vcc_init);
  904. module_exit(vcc_exit);