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