vcc.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155
  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(unsigned long index)
  297. {
  298. struct vio_driver_state *vio;
  299. struct vcc_port *port;
  300. unsigned long flags;
  301. int rv;
  302. port = vcc_get_ne(index);
  303. if (!port)
  304. return;
  305. spin_lock_irqsave(&port->lock, flags);
  306. port->rx_timer.expires = 0;
  307. vio = &port->vio;
  308. enable_irq(vio->vdev->rx_irq);
  309. if (!port->tty || port->removed)
  310. goto done;
  311. rv = vcc_ldc_read(port);
  312. if (rv == -ECONNRESET)
  313. vio_conn_reset(vio);
  314. done:
  315. spin_unlock_irqrestore(&port->lock, flags);
  316. vcc_put(port, false);
  317. }
  318. static void vcc_tx_timer(unsigned long index)
  319. {
  320. struct vcc_port *port;
  321. struct vio_vcc *pkt;
  322. unsigned long flags;
  323. int tosend = 0;
  324. int rv;
  325. port = vcc_get_ne(index);
  326. if (!port)
  327. return;
  328. spin_lock_irqsave(&port->lock, flags);
  329. port->tx_timer.expires = 0;
  330. if (!port->tty || port->removed)
  331. goto done;
  332. tosend = min(VCC_BUFF_LEN, port->chars_in_buffer);
  333. if (!tosend)
  334. goto done;
  335. pkt = &port->buffer;
  336. pkt->tag.type = VIO_TYPE_DATA;
  337. pkt->tag.stype = tosend;
  338. vccdbgl(port->vio.lp);
  339. rv = ldc_write(port->vio.lp, pkt, (VIO_TAG_SIZE + tosend));
  340. WARN_ON(!rv);
  341. if (rv < 0) {
  342. vccdbg("VCC: ldc_write()=%d\n", rv);
  343. vcc_kick_tx(port);
  344. } else {
  345. struct tty_struct *tty = port->tty;
  346. port->chars_in_buffer = 0;
  347. if (tty)
  348. tty_wakeup(tty);
  349. }
  350. done:
  351. spin_unlock_irqrestore(&port->lock, flags);
  352. vcc_put(port, false);
  353. }
  354. /**
  355. * vcc_event() - LDC event processing engine
  356. * @arg: VCC private data
  357. * @event: LDC event
  358. *
  359. * Handles LDC events for VCC
  360. */
  361. static void vcc_event(void *arg, int event)
  362. {
  363. struct vio_driver_state *vio;
  364. struct vcc_port *port;
  365. unsigned long flags;
  366. int rv;
  367. port = arg;
  368. vio = &port->vio;
  369. spin_lock_irqsave(&port->lock, flags);
  370. switch (event) {
  371. case LDC_EVENT_RESET:
  372. case LDC_EVENT_UP:
  373. vio_link_state_change(vio, event);
  374. break;
  375. case LDC_EVENT_DATA_READY:
  376. rv = vcc_ldc_read(port);
  377. if (rv == -ECONNRESET)
  378. vio_conn_reset(vio);
  379. break;
  380. default:
  381. pr_err("VCC: unexpected LDC event(%d)\n", event);
  382. }
  383. spin_unlock_irqrestore(&port->lock, flags);
  384. }
  385. static struct ldc_channel_config vcc_ldc_cfg = {
  386. .event = vcc_event,
  387. .mtu = VIO_VCC_MTU_SIZE,
  388. .mode = LDC_MODE_RAW,
  389. .debug = 0,
  390. };
  391. /* Ordered from largest major to lowest */
  392. static struct vio_version vcc_versions[] = {
  393. { .major = 1, .minor = 0 },
  394. };
  395. static struct tty_port_operations vcc_port_ops = { 0 };
  396. static ssize_t vcc_sysfs_domain_show(struct device *dev,
  397. struct device_attribute *attr,
  398. char *buf)
  399. {
  400. struct vcc_port *port;
  401. int rv;
  402. port = dev_get_drvdata(dev);
  403. if (!port)
  404. return -ENODEV;
  405. rv = scnprintf(buf, PAGE_SIZE, "%s\n", port->domain);
  406. return rv;
  407. }
  408. static int vcc_send_ctl(struct vcc_port *port, int ctl)
  409. {
  410. struct vio_vcc pkt;
  411. int rv;
  412. pkt.tag.type = VIO_TYPE_CTRL;
  413. pkt.tag.sid = ctl;
  414. pkt.tag.stype = 0;
  415. rv = ldc_write(port->vio.lp, &pkt, sizeof(pkt.tag));
  416. WARN_ON(!rv);
  417. vccdbg("VCC: ldc_write(%ld)=%d\n", sizeof(pkt.tag), rv);
  418. return rv;
  419. }
  420. static ssize_t vcc_sysfs_break_store(struct device *dev,
  421. struct device_attribute *attr,
  422. const char *buf, size_t count)
  423. {
  424. struct vcc_port *port;
  425. unsigned long flags;
  426. int rv = count;
  427. int brk;
  428. port = dev_get_drvdata(dev);
  429. if (!port)
  430. return -ENODEV;
  431. spin_lock_irqsave(&port->lock, flags);
  432. if (sscanf(buf, "%ud", &brk) != 1 || brk != 1)
  433. rv = -EINVAL;
  434. else if (vcc_send_ctl(port, VCC_CTL_BREAK) < 0)
  435. vcc_kick_tx(port);
  436. spin_unlock_irqrestore(&port->lock, flags);
  437. return rv;
  438. }
  439. static DEVICE_ATTR(domain, 0400, vcc_sysfs_domain_show, NULL);
  440. static DEVICE_ATTR(break, 0200, NULL, vcc_sysfs_break_store);
  441. static struct attribute *vcc_sysfs_entries[] = {
  442. &dev_attr_domain.attr,
  443. &dev_attr_break.attr,
  444. NULL
  445. };
  446. static struct attribute_group vcc_attribute_group = {
  447. .name = NULL,
  448. .attrs = vcc_sysfs_entries,
  449. };
  450. /**
  451. * vcc_probe() - Initialize VCC port
  452. * @vdev: Pointer to VIO device of the new VCC port
  453. * @id: VIO device ID
  454. *
  455. * Initializes a VCC port to receive serial console data from
  456. * the guest domain. Sets up a TTY end point on the control
  457. * domain. Sets up VIO/LDC link between the guest & control
  458. * domain endpoints.
  459. *
  460. * Return: status of the probe
  461. */
  462. static int vcc_probe(struct vio_dev *vdev, const struct vio_device_id *id)
  463. {
  464. struct mdesc_handle *hp;
  465. struct vcc_port *port;
  466. struct device *dev;
  467. const char *domain;
  468. char *name;
  469. u64 node;
  470. int rv;
  471. vccdbg("VCC: name=%s\n", dev_name(&vdev->dev));
  472. if (!vcc_tty_driver) {
  473. pr_err("VCC: TTY driver not registered\n");
  474. return -ENODEV;
  475. }
  476. port = kzalloc(sizeof(struct vcc_port), GFP_KERNEL);
  477. if (!port)
  478. return -ENOMEM;
  479. name = kstrdup(dev_name(&vdev->dev), GFP_KERNEL);
  480. rv = vio_driver_init(&port->vio, vdev, VDEV_CONSOLE_CON, vcc_versions,
  481. ARRAY_SIZE(vcc_versions), NULL, name);
  482. if (rv)
  483. goto free_port;
  484. port->vio.debug = vcc_dbg_vio;
  485. vcc_ldc_cfg.debug = vcc_dbg_ldc;
  486. rv = vio_ldc_alloc(&port->vio, &vcc_ldc_cfg, port);
  487. if (rv)
  488. goto free_port;
  489. spin_lock_init(&port->lock);
  490. port->index = vcc_table_add(port);
  491. if (port->index == -1) {
  492. pr_err("VCC: no more TTY indices left for allocation\n");
  493. goto free_ldc;
  494. }
  495. /* Register the device using VCC table index as TTY index */
  496. dev = tty_register_device(vcc_tty_driver, port->index, &vdev->dev);
  497. if (IS_ERR(dev)) {
  498. rv = PTR_ERR(dev);
  499. goto free_table;
  500. }
  501. hp = mdesc_grab();
  502. node = vio_vdev_node(hp, vdev);
  503. if (node == MDESC_NODE_NULL) {
  504. rv = -ENXIO;
  505. mdesc_release(hp);
  506. goto unreg_tty;
  507. }
  508. domain = mdesc_get_property(hp, node, "vcc-domain-name", NULL);
  509. if (!domain) {
  510. rv = -ENXIO;
  511. mdesc_release(hp);
  512. goto unreg_tty;
  513. }
  514. port->domain = kstrdup(domain, GFP_KERNEL);
  515. mdesc_release(hp);
  516. rv = sysfs_create_group(&vdev->dev.kobj, &vcc_attribute_group);
  517. if (rv)
  518. goto free_domain;
  519. init_timer(&port->rx_timer);
  520. port->rx_timer.function = vcc_rx_timer;
  521. port->rx_timer.data = port->index;
  522. init_timer(&port->tx_timer);
  523. port->tx_timer.function = vcc_tx_timer;
  524. port->tx_timer.data = port->index;
  525. dev_set_drvdata(&vdev->dev, port);
  526. /* It's possible to receive IRQs in the middle of vio_port_up. Disable
  527. * IRQs until the port is up.
  528. */
  529. disable_irq_nosync(vdev->rx_irq);
  530. vio_port_up(&port->vio);
  531. enable_irq(vdev->rx_irq);
  532. return 0;
  533. free_domain:
  534. kfree(port->domain);
  535. unreg_tty:
  536. tty_unregister_device(vcc_tty_driver, port->index);
  537. free_table:
  538. vcc_table_remove(port->index);
  539. free_ldc:
  540. vio_ldc_free(&port->vio);
  541. free_port:
  542. kfree(name);
  543. kfree(port);
  544. return rv;
  545. }
  546. /**
  547. * vcc_remove() - Terminate a VCC port
  548. * @vdev: Pointer to VIO device of the VCC port
  549. *
  550. * Terminates a VCC port. Sets up the teardown of TTY and
  551. * VIO/LDC link between guest and primary domains.
  552. *
  553. * Return: status of removal
  554. */
  555. static int vcc_remove(struct vio_dev *vdev)
  556. {
  557. struct vcc_port *port = dev_get_drvdata(&vdev->dev);
  558. if (!port)
  559. return -ENODEV;
  560. del_timer_sync(&port->rx_timer);
  561. del_timer_sync(&port->tx_timer);
  562. /* If there's a process with the device open, do a synchronous
  563. * hangup of the TTY. This *may* cause the process to call close
  564. * asynchronously, but it's not guaranteed.
  565. */
  566. if (port->tty)
  567. tty_vhangup(port->tty);
  568. /* Get exclusive reference to VCC, ensures that there are no other
  569. * clients to this port
  570. */
  571. port = vcc_get(port->index, true);
  572. if (WARN_ON(!port))
  573. return -ENODEV;
  574. tty_unregister_device(vcc_tty_driver, port->index);
  575. del_timer_sync(&port->vio.timer);
  576. vio_ldc_free(&port->vio);
  577. sysfs_remove_group(&vdev->dev.kobj, &vcc_attribute_group);
  578. dev_set_drvdata(&vdev->dev, NULL);
  579. if (port->tty) {
  580. port->removed = true;
  581. vcc_put(port, true);
  582. } else {
  583. vcc_table_remove(port->index);
  584. kfree(port->vio.name);
  585. kfree(port->domain);
  586. kfree(port);
  587. }
  588. return 0;
  589. }
  590. static const struct vio_device_id vcc_match[] = {
  591. {
  592. .type = "vcc-port",
  593. },
  594. {},
  595. };
  596. MODULE_DEVICE_TABLE(vio, vcc_match);
  597. static struct vio_driver vcc_driver = {
  598. .id_table = vcc_match,
  599. .probe = vcc_probe,
  600. .remove = vcc_remove,
  601. .name = "vcc",
  602. };
  603. static int vcc_open(struct tty_struct *tty, struct file *vcc_file)
  604. {
  605. struct vcc_port *port;
  606. if (unlikely(!tty)) {
  607. pr_err("VCC: open: Invalid TTY handle\n");
  608. return -ENXIO;
  609. }
  610. if (tty->count > 1)
  611. return -EBUSY;
  612. port = vcc_get_ne(tty->index);
  613. if (unlikely(!port)) {
  614. pr_err("VCC: open: Failed to find VCC port\n");
  615. return -ENODEV;
  616. }
  617. if (unlikely(!port->vio.lp)) {
  618. pr_err("VCC: open: LDC channel not configured\n");
  619. vcc_put(port, false);
  620. return -EPIPE;
  621. }
  622. vccdbgl(port->vio.lp);
  623. vcc_put(port, false);
  624. if (unlikely(!tty->port)) {
  625. pr_err("VCC: open: TTY port not found\n");
  626. return -ENXIO;
  627. }
  628. if (unlikely(!tty->port->ops)) {
  629. pr_err("VCC: open: TTY ops not defined\n");
  630. return -ENXIO;
  631. }
  632. return tty_port_open(tty->port, tty, vcc_file);
  633. }
  634. static void vcc_close(struct tty_struct *tty, struct file *vcc_file)
  635. {
  636. if (unlikely(!tty)) {
  637. pr_err("VCC: close: Invalid TTY handle\n");
  638. return;
  639. }
  640. if (unlikely(tty->count > 1))
  641. return;
  642. if (unlikely(!tty->port)) {
  643. pr_err("VCC: close: TTY port not found\n");
  644. return;
  645. }
  646. tty_port_close(tty->port, tty, vcc_file);
  647. }
  648. static void vcc_ldc_hup(struct vcc_port *port)
  649. {
  650. unsigned long flags;
  651. spin_lock_irqsave(&port->lock, flags);
  652. if (vcc_send_ctl(port, VCC_CTL_HUP) < 0)
  653. vcc_kick_tx(port);
  654. spin_unlock_irqrestore(&port->lock, flags);
  655. }
  656. static void vcc_hangup(struct tty_struct *tty)
  657. {
  658. struct vcc_port *port;
  659. if (unlikely(!tty)) {
  660. pr_err("VCC: hangup: Invalid TTY handle\n");
  661. return;
  662. }
  663. port = vcc_get_ne(tty->index);
  664. if (unlikely(!port)) {
  665. pr_err("VCC: hangup: Failed to find VCC port\n");
  666. return;
  667. }
  668. if (unlikely(!tty->port)) {
  669. pr_err("VCC: hangup: TTY port not found\n");
  670. vcc_put(port, false);
  671. return;
  672. }
  673. vcc_ldc_hup(port);
  674. vcc_put(port, false);
  675. tty_port_hangup(tty->port);
  676. }
  677. static int vcc_write(struct tty_struct *tty, const unsigned char *buf,
  678. int count)
  679. {
  680. struct vcc_port *port;
  681. struct vio_vcc *pkt;
  682. unsigned long flags;
  683. int total_sent = 0;
  684. int tosend = 0;
  685. int rv = -EINVAL;
  686. if (unlikely(!tty)) {
  687. pr_err("VCC: write: Invalid TTY handle\n");
  688. return -ENXIO;
  689. }
  690. port = vcc_get_ne(tty->index);
  691. if (unlikely(!port)) {
  692. pr_err("VCC: write: Failed to find VCC port");
  693. return -ENODEV;
  694. }
  695. spin_lock_irqsave(&port->lock, flags);
  696. pkt = &port->buffer;
  697. pkt->tag.type = VIO_TYPE_DATA;
  698. while (count > 0) {
  699. /* Minimum of data to write and space available */
  700. tosend = min(count, (VCC_BUFF_LEN - port->chars_in_buffer));
  701. if (!tosend)
  702. break;
  703. memcpy(&pkt->data[port->chars_in_buffer], &buf[total_sent],
  704. tosend);
  705. port->chars_in_buffer += tosend;
  706. pkt->tag.stype = tosend;
  707. vccdbg("TAG [%02x:%02x:%04x:%08x]\n", pkt->tag.type,
  708. pkt->tag.stype, pkt->tag.stype_env, pkt->tag.sid);
  709. vccdbg("DATA [%s]\n", pkt->data);
  710. vccdbgl(port->vio.lp);
  711. /* Since we know we have enough room in VCC buffer for tosend
  712. * we record that it was sent regardless of whether the
  713. * hypervisor actually took it because we have it buffered.
  714. */
  715. rv = ldc_write(port->vio.lp, pkt, (VIO_TAG_SIZE + tosend));
  716. vccdbg("VCC: write: ldc_write(%d)=%d\n",
  717. (VIO_TAG_SIZE + tosend), rv);
  718. total_sent += tosend;
  719. count -= tosend;
  720. if (rv < 0) {
  721. vcc_kick_tx(port);
  722. break;
  723. }
  724. port->chars_in_buffer = 0;
  725. }
  726. spin_unlock_irqrestore(&port->lock, flags);
  727. vcc_put(port, false);
  728. vccdbg("VCC: write: total=%d rv=%d", total_sent, rv);
  729. return total_sent ? total_sent : rv;
  730. }
  731. static int vcc_write_room(struct tty_struct *tty)
  732. {
  733. struct vcc_port *port;
  734. u64 num;
  735. if (unlikely(!tty)) {
  736. pr_err("VCC: write_room: Invalid TTY handle\n");
  737. return -ENXIO;
  738. }
  739. port = vcc_get_ne(tty->index);
  740. if (unlikely(!port)) {
  741. pr_err("VCC: write_room: Failed to find VCC port\n");
  742. return -ENODEV;
  743. }
  744. num = VCC_BUFF_LEN - port->chars_in_buffer;
  745. vcc_put(port, false);
  746. return num;
  747. }
  748. static int vcc_chars_in_buffer(struct tty_struct *tty)
  749. {
  750. struct vcc_port *port;
  751. u64 num;
  752. if (unlikely(!tty)) {
  753. pr_err("VCC: chars_in_buffer: Invalid TTY handle\n");
  754. return -ENXIO;
  755. }
  756. port = vcc_get_ne(tty->index);
  757. if (unlikely(!port)) {
  758. pr_err("VCC: chars_in_buffer: Failed to find VCC port\n");
  759. return -ENODEV;
  760. }
  761. num = port->chars_in_buffer;
  762. vcc_put(port, false);
  763. return num;
  764. }
  765. static int vcc_break_ctl(struct tty_struct *tty, int state)
  766. {
  767. struct vcc_port *port;
  768. unsigned long flags;
  769. if (unlikely(!tty)) {
  770. pr_err("VCC: break_ctl: Invalid TTY handle\n");
  771. return -ENXIO;
  772. }
  773. port = vcc_get_ne(tty->index);
  774. if (unlikely(!port)) {
  775. pr_err("VCC: break_ctl: Failed to find VCC port\n");
  776. return -ENODEV;
  777. }
  778. /* Turn off break */
  779. if (state == 0) {
  780. vcc_put(port, false);
  781. return 0;
  782. }
  783. spin_lock_irqsave(&port->lock, flags);
  784. if (vcc_send_ctl(port, VCC_CTL_BREAK) < 0)
  785. vcc_kick_tx(port);
  786. spin_unlock_irqrestore(&port->lock, flags);
  787. vcc_put(port, false);
  788. return 0;
  789. }
  790. static int vcc_install(struct tty_driver *driver, struct tty_struct *tty)
  791. {
  792. struct vcc_port *port_vcc;
  793. struct tty_port *port_tty;
  794. int ret;
  795. if (unlikely(!tty)) {
  796. pr_err("VCC: install: Invalid TTY handle\n");
  797. return -ENXIO;
  798. }
  799. if (tty->index >= VCC_MAX_PORTS)
  800. return -EINVAL;
  801. ret = tty_standard_install(driver, tty);
  802. if (ret)
  803. return ret;
  804. port_tty = kzalloc(sizeof(struct tty_port), GFP_KERNEL);
  805. if (!port_tty)
  806. return -ENOMEM;
  807. port_vcc = vcc_get(tty->index, true);
  808. if (!port_vcc) {
  809. pr_err("VCC: install: Failed to find VCC port\n");
  810. tty->port = NULL;
  811. kfree(port_tty);
  812. return -ENODEV;
  813. }
  814. tty_port_init(port_tty);
  815. port_tty->ops = &vcc_port_ops;
  816. tty->port = port_tty;
  817. port_vcc->tty = tty;
  818. vcc_put(port_vcc, true);
  819. return 0;
  820. }
  821. static void vcc_cleanup(struct tty_struct *tty)
  822. {
  823. struct vcc_port *port;
  824. if (unlikely(!tty)) {
  825. pr_err("VCC: cleanup: Invalid TTY handle\n");
  826. return;
  827. }
  828. port = vcc_get(tty->index, true);
  829. if (port) {
  830. port->tty = NULL;
  831. if (port->removed) {
  832. vcc_table_remove(tty->index);
  833. kfree(port->vio.name);
  834. kfree(port->domain);
  835. kfree(port);
  836. } else {
  837. vcc_put(port, true);
  838. }
  839. }
  840. tty_port_destroy(tty->port);
  841. kfree(tty->port);
  842. tty->port = NULL;
  843. }
  844. static const struct tty_operations vcc_ops = {
  845. .open = vcc_open,
  846. .close = vcc_close,
  847. .hangup = vcc_hangup,
  848. .write = vcc_write,
  849. .write_room = vcc_write_room,
  850. .chars_in_buffer = vcc_chars_in_buffer,
  851. .break_ctl = vcc_break_ctl,
  852. .install = vcc_install,
  853. .cleanup = vcc_cleanup,
  854. };
  855. #define VCC_TTY_FLAGS (TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_REAL_RAW)
  856. static int vcc_tty_init(void)
  857. {
  858. int rv;
  859. pr_info("VCC: %s\n", version);
  860. vcc_tty_driver = tty_alloc_driver(VCC_MAX_PORTS, VCC_TTY_FLAGS);
  861. if (IS_ERR(vcc_tty_driver)) {
  862. pr_err("VCC: TTY driver alloc failed\n");
  863. return PTR_ERR(vcc_tty_driver);
  864. }
  865. vcc_tty_driver->driver_name = vcc_driver_name;
  866. vcc_tty_driver->name = vcc_device_node;
  867. vcc_tty_driver->minor_start = VCC_MINOR_START;
  868. vcc_tty_driver->type = TTY_DRIVER_TYPE_SYSTEM;
  869. vcc_tty_driver->init_termios = vcc_tty_termios;
  870. tty_set_operations(vcc_tty_driver, &vcc_ops);
  871. rv = tty_register_driver(vcc_tty_driver);
  872. if (rv) {
  873. pr_err("VCC: TTY driver registration failed\n");
  874. put_tty_driver(vcc_tty_driver);
  875. vcc_tty_driver = NULL;
  876. return rv;
  877. }
  878. vccdbg("VCC: TTY driver registered\n");
  879. return 0;
  880. }
  881. static void vcc_tty_exit(void)
  882. {
  883. tty_unregister_driver(vcc_tty_driver);
  884. put_tty_driver(vcc_tty_driver);
  885. vccdbg("VCC: TTY driver unregistered\n");
  886. vcc_tty_driver = NULL;
  887. }
  888. static int __init vcc_init(void)
  889. {
  890. int rv;
  891. rv = vcc_tty_init();
  892. if (rv) {
  893. pr_err("VCC: TTY init failed\n");
  894. return rv;
  895. }
  896. rv = vio_register_driver(&vcc_driver);
  897. if (rv) {
  898. pr_err("VCC: VIO driver registration failed\n");
  899. vcc_tty_exit();
  900. } else {
  901. vccdbg("VCC: VIO driver registered successfully\n");
  902. }
  903. return rv;
  904. }
  905. static void __exit vcc_exit(void)
  906. {
  907. vio_unregister_driver(&vcc_driver);
  908. vccdbg("VCC: VIO driver unregistered\n");
  909. vcc_tty_exit();
  910. vccdbg("VCC: TTY driver unregistered\n");
  911. }
  912. module_init(vcc_init);
  913. module_exit(vcc_exit);