aboutsummaryrefslogtreecommitdiff
path: root/drivers/s390/char/sclp_vt220.c
diff options
context:
space:
mode:
authorLibravatar Linus Torvalds <torvalds@linux-foundation.org>2023-02-21 18:24:12 -0800
committerLibravatar Linus Torvalds <torvalds@linux-foundation.org>2023-02-21 18:24:12 -0800
commit5b7c4cabbb65f5c469464da6c5f614cbd7f730f2 (patch)
treecc5c2d0a898769fd59549594fedb3ee6f84e59a0 /drivers/s390/char/sclp_vt220.c
downloadlinux-5b7c4cabbb65f5c469464da6c5f614cbd7f730f2.tar.gz
linux-5b7c4cabbb65f5c469464da6c5f614cbd7f730f2.zip
Merge tag 'net-next-6.3' of git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net-nextgrafted
Pull networking updates from Jakub Kicinski: "Core: - Add dedicated kmem_cache for typical/small skb->head, avoid having to access struct page at kfree time, and improve memory use. - Introduce sysctl to set default RPS configuration for new netdevs. - Define Netlink protocol specification format which can be used to describe messages used by each family and auto-generate parsers. Add tools for generating kernel data structures and uAPI headers. - Expose all net/core sysctls inside netns. - Remove 4s sleep in netpoll if carrier is instantly detected on boot. - Add configurable limit of MDB entries per port, and port-vlan. - Continue populating drop reasons throughout the stack. - Retire a handful of legacy Qdiscs and classifiers. Protocols: - Support IPv4 big TCP (TSO frames larger than 64kB). - Add IP_LOCAL_PORT_RANGE socket option, to control local port range on socket by socket basis. - Track and report in procfs number of MPTCP sockets used. - Support mixing IPv4 and IPv6 flows in the in-kernel MPTCP path manager. - IPv6: don't check net.ipv6.route.max_size and rely on garbage collection to free memory (similarly to IPv4). - Support Penultimate Segment Pop (PSP) flavor in SRv6 (RFC8986). - ICMP: add per-rate limit counters. - Add support for user scanning requests in ieee802154. - Remove static WEP support. - Support minimal Wi-Fi 7 Extremely High Throughput (EHT) rate reporting. - WiFi 7 EHT channel puncturing support (client & AP). BPF: - Add a rbtree data structure following the "next-gen data structure" precedent set by recently added linked list, that is, by using kfunc + kptr instead of adding a new BPF map type. - Expose XDP hints via kfuncs with initial support for RX hash and timestamp metadata. - Add BPF_F_NO_TUNNEL_KEY extension to bpf_skb_set_tunnel_key to better support decap on GRE tunnel devices not operating in collect metadata. - Improve x86 JIT's codegen for PROBE_MEM runtime error checks. - Remove the need for trace_printk_lock for bpf_trace_printk and bpf_trace_vprintk helpers. - Extend libbpf's bpf_tracing.h support for tracing arguments of kprobes/uprobes and syscall as a special case. - Significantly reduce the search time for module symbols by livepatch and BPF. - Enable cpumasks to be used as kptrs, which is useful for tracing programs tracking which tasks end up running on which CPUs in different time intervals. - Add support for BPF trampoline on s390x and riscv64. - Add capability to export the XDP features supported by the NIC. - Add __bpf_kfunc tag for marking kernel functions as kfuncs. - Add cgroup.memory=nobpf kernel parameter option to disable BPF memory accounting for container environments. Netfilter: - Remove the CLUSTERIP target. It has been marked as obsolete for years, and we still have WARN splats wrt races of the out-of-band /proc interface installed by this target. - Add 'destroy' commands to nf_tables. They are identical to the existing 'delete' commands, but do not return an error if the referenced object (set, chain, rule...) did not exist. Driver API: - Improve cpumask_local_spread() locality to help NICs set the right IRQ affinity on AMD platforms. - Separate C22 and C45 MDIO bus transactions more clearly. - Introduce new DCB table to control DSCP rewrite on egress. - Support configuration of Physical Layer Collision Avoidance (PLCA) Reconciliation Sublayer (RS) (802.3cg-2019). Modern version of shared medium Ethernet. - Support for MAC Merge layer (IEEE 802.3-2018 clause 99). Allowing preemption of low priority frames by high priority frames. - Add support for controlling MACSec offload using netlink SET. - Rework devlink instance refcounts to allow registration and de-registration under the instance lock. Split the code into multiple files, drop some of the unnecessarily granular locks and factor out common parts of netlink operation handling. - Add TX frame aggregation parameters (for USB drivers). - Add a new attr TCA_EXT_WARN_MSG to report TC (offload) warning messages with notifications for debug. - Allow offloading of UDP NEW connections via act_ct. - Add support for per action HW stats in TC. - Support hardware miss to TC action (continue processing in SW from a specific point in the action chain). - Warn if old Wireless Extension user space interface is used with modern cfg80211/mac80211 drivers. Do not support Wireless Extensions for Wi-Fi 7 devices at all. Everyone should switch to using nl80211 interface instead. - Improve the CAN bit timing configuration. Use extack to return error messages directly to user space, update the SJW handling, including the definition of a new default value that will benefit CAN-FD controllers, by increasing their oscillator tolerance. New hardware / drivers: - Ethernet: - nVidia BlueField-3 support (control traffic driver) - Ethernet support for imx93 SoCs - Motorcomm yt8531 gigabit Ethernet PHY - onsemi NCN26000 10BASE-T1S PHY (with support for PLCA) - Microchip LAN8841 PHY (incl. cable diagnostics and PTP) - Amlogic gxl MDIO mux - WiFi: - RealTek RTL8188EU (rtl8xxxu) - Qualcomm Wi-Fi 7 devices (ath12k) - CAN: - Renesas R-Car V4H Drivers: - Bluetooth: - Set Per Platform Antenna Gain (PPAG) for Intel controllers. - Ethernet NICs: - Intel (1G, igc): - support TSN / Qbv / packet scheduling features of i226 model - Intel (100G, ice): - use GNSS subsystem instead of TTY - multi-buffer XDP support - extend support for GPIO pins to E823 devices - nVidia/Mellanox: - update the shared buffer configuration on PFC commands - implement PTP adjphase function for HW offset control - TC support for Geneve and GRE with VF tunnel offload - more efficient crypto key management method - multi-port eswitch support - Netronome/Corigine: - add DCB IEEE support - support IPsec offloading for NFP3800 - Freescale/NXP (enetc): - support XDP_REDIRECT for XDP non-linear buffers - improve reconfig, avoid link flap and waiting for idle - support MAC Merge layer - Other NICs: - sfc/ef100: add basic devlink support for ef100 - ionic: rx_push mode operation (writing descriptors via MMIO) - bnxt: use the auxiliary bus abstraction for RDMA - r8169: disable ASPM and reset bus in case of tx timeout - cpsw: support QSGMII mode for J721e CPSW9G - cpts: support pulse-per-second output - ngbe: add an mdio bus driver - usbnet: optimize usbnet_bh() by avoiding unnecessary queuing - r8152: handle devices with FW with NCM support - amd-xgbe: support 10Mbps, 2.5GbE speeds and rx-adaptation - virtio-net: support multi buffer XDP - virtio/vsock: replace virtio_vsock_pkt with sk_buff - tsnep: XDP support - Ethernet high-speed switches: - nVidia/Mellanox (mlxsw): - add support for latency TLV (in FW control messages) - Microchip (sparx5): - separate explicit and implicit traffic forwarding rules, make the implicit rules always active - add support for egress DSCP rewrite - IS0 VCAP support (Ingress Classification) - IS2 VCAP filters (protos, L3 addrs, L4 ports, flags, ToS etc.) - ES2 VCAP support (Egress Access Control) - support for Per-Stream Filtering and Policing (802.1Q, 8.6.5.1) - Ethernet embedded switches: - Marvell (mv88e6xxx): - add MAB (port auth) offload support - enable PTP receive for mv88e6390 - NXP (ocelot): - support MAC Merge layer - support for the the vsc7512 internal copper phys - Microchip: - lan9303: convert to PHYLINK - lan966x: support TC flower filter statistics - lan937x: PTP support for KSZ9563/KSZ8563 and LAN937x - lan937x: support Credit Based Shaper configuration - ksz9477: support Energy Efficient Ethernet - other: - qca8k: convert to regmap read/write API, use bulk operations - rswitch: Improve TX timestamp accuracy - Intel WiFi (iwlwifi): - EHT (Wi-Fi 7) rate reporting - STEP equalizer support: transfer some STEP (connection to radio on platforms with integrated wifi) related parameters from the BIOS to the firmware. - Qualcomm 802.11ax WiFi (ath11k): - IPQ5018 support - Fine Timing Measurement (FTM) responder role support - channel 177 support - MediaTek WiFi (mt76): - per-PHY LED support - mt7996: EHT (Wi-Fi 7) support - Wireless Ethernet Dispatch (WED) reset support - switch to using page pool allocator - RealTek WiFi (rtw89): - support new version of Bluetooth co-existance - Mobile: - rmnet: support TX aggregation" * tag 'net-next-6.3' of git://git.kernel.org/pub/scm/linux/kernel/git/netdev/net-next: (1872 commits) page_pool: add a comment explaining the fragment counter usage net: ethtool: fix __ethtool_dev_mm_supported() implementation ethtool: pse-pd: Fix double word in comments xsk: add linux/vmalloc.h to xsk.c sefltests: netdevsim: wait for devlink instance after netns removal selftest: fib_tests: Always cleanup before exit net/mlx5e: Align IPsec ASO result memory to be as required by hardware net/mlx5e: TC, Set CT miss to the specific ct action instance net/mlx5e: Rename CHAIN_TO_REG to MAPPED_OBJ_TO_REG net/mlx5: Refactor tc miss handling to a single function net/mlx5: Kconfig: Make tc offload depend on tc skb extension net/sched: flower: Support hardware miss to tc action net/sched: flower: Move filter handle initialization earlier net/sched: cls_api: Support hardware miss to tc action net/sched: Rename user cookie and act cookie sfc: fix builds without CONFIG_RTC_LIB sfc: clean up some inconsistent indentings net/mlx4_en: Introduce flexible array to silence overflow warning net: lan966x: Fix possible deadlock inside PTP net/ulp: Remove redundant ->clone() test in inet_clone_ulp(). ...
Diffstat (limited to 'drivers/s390/char/sclp_vt220.c')
-rw-r--r--drivers/s390/char/sclp_vt220.c849
1 files changed, 849 insertions, 0 deletions
diff --git a/drivers/s390/char/sclp_vt220.c b/drivers/s390/char/sclp_vt220.c
new file mode 100644
index 000000000..a32f34a1c
--- /dev/null
+++ b/drivers/s390/char/sclp_vt220.c
@@ -0,0 +1,849 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * SCLP VT220 terminal driver.
+ *
+ * Copyright IBM Corp. 2003, 2009
+ *
+ * Author(s): Peter Oberparleiter <Peter.Oberparleiter@de.ibm.com>
+ */
+
+#include <linux/module.h>
+#include <linux/spinlock.h>
+#include <linux/panic_notifier.h>
+#include <linux/list.h>
+#include <linux/wait.h>
+#include <linux/timer.h>
+#include <linux/kernel.h>
+#include <linux/sysrq.h>
+#include <linux/tty.h>
+#include <linux/tty_driver.h>
+#include <linux/tty_flip.h>
+#include <linux/errno.h>
+#include <linux/mm.h>
+#include <linux/major.h>
+#include <linux/console.h>
+#include <linux/kdev_t.h>
+#include <linux/interrupt.h>
+#include <linux/init.h>
+#include <linux/reboot.h>
+#include <linux/slab.h>
+
+#include <linux/uaccess.h>
+#include "sclp.h"
+#include "ctrlchar.h"
+
+#define SCLP_VT220_MAJOR TTY_MAJOR
+#define SCLP_VT220_MINOR 65
+#define SCLP_VT220_DRIVER_NAME "sclp_vt220"
+#define SCLP_VT220_DEVICE_NAME "ttysclp"
+#define SCLP_VT220_CONSOLE_NAME "ttysclp"
+#define SCLP_VT220_CONSOLE_INDEX 0 /* console=ttysclp0 */
+
+/* Representation of a single write request */
+struct sclp_vt220_request {
+ struct list_head list;
+ struct sclp_req sclp_req;
+ int retry_count;
+};
+
+/* VT220 SCCB */
+struct sclp_vt220_sccb {
+ struct sccb_header header;
+ struct evbuf_header evbuf;
+};
+
+#define SCLP_VT220_MAX_CHARS_PER_BUFFER (PAGE_SIZE - \
+ sizeof(struct sclp_vt220_request) - \
+ sizeof(struct sclp_vt220_sccb))
+
+/* Structures and data needed to register tty driver */
+static struct tty_driver *sclp_vt220_driver;
+
+static struct tty_port sclp_vt220_port;
+
+/* Lock to protect internal data from concurrent access */
+static DEFINE_SPINLOCK(sclp_vt220_lock);
+
+/* List of empty pages to be used as write request buffers */
+static LIST_HEAD(sclp_vt220_empty);
+
+/* List of pending requests */
+static LIST_HEAD(sclp_vt220_outqueue);
+
+/* Flag that output queue is currently running */
+static int sclp_vt220_queue_running;
+
+/* Timer used for delaying write requests to merge subsequent messages into
+ * a single buffer */
+static struct timer_list sclp_vt220_timer;
+
+/* Pointer to current request buffer which has been partially filled but not
+ * yet sent */
+static struct sclp_vt220_request *sclp_vt220_current_request;
+
+/* Number of characters in current request buffer */
+static int sclp_vt220_buffered_chars;
+
+/* Counter controlling core driver initialization. */
+static int __initdata sclp_vt220_init_count;
+
+/* Flag indicating that sclp_vt220_current_request should really
+ * have been already queued but wasn't because the SCLP was processing
+ * another buffer */
+static int sclp_vt220_flush_later;
+
+static void sclp_vt220_receiver_fn(struct evbuf_header *evbuf);
+static int __sclp_vt220_emit(struct sclp_vt220_request *request);
+static void sclp_vt220_emit_current(void);
+
+/* Registration structure for SCLP output event buffers */
+static struct sclp_register sclp_vt220_register = {
+ .send_mask = EVTYP_VT220MSG_MASK,
+};
+
+/* Registration structure for SCLP input event buffers */
+static struct sclp_register sclp_vt220_register_input = {
+ .receive_mask = EVTYP_VT220MSG_MASK,
+ .receiver_fn = sclp_vt220_receiver_fn,
+};
+
+
+/*
+ * Put provided request buffer back into queue and check emit pending
+ * buffers if necessary.
+ */
+static void
+sclp_vt220_process_queue(struct sclp_vt220_request *request)
+{
+ unsigned long flags;
+ void *page;
+
+ do {
+ /* Put buffer back to list of empty buffers */
+ page = request->sclp_req.sccb;
+ spin_lock_irqsave(&sclp_vt220_lock, flags);
+ /* Move request from outqueue to empty queue */
+ list_del(&request->list);
+ list_add_tail((struct list_head *) page, &sclp_vt220_empty);
+ /* Check if there is a pending buffer on the out queue. */
+ request = NULL;
+ if (!list_empty(&sclp_vt220_outqueue))
+ request = list_entry(sclp_vt220_outqueue.next,
+ struct sclp_vt220_request, list);
+ if (!request) {
+ sclp_vt220_queue_running = 0;
+ spin_unlock_irqrestore(&sclp_vt220_lock, flags);
+ break;
+ }
+ spin_unlock_irqrestore(&sclp_vt220_lock, flags);
+ } while (__sclp_vt220_emit(request));
+ if (request == NULL && sclp_vt220_flush_later)
+ sclp_vt220_emit_current();
+ tty_port_tty_wakeup(&sclp_vt220_port);
+}
+
+#define SCLP_BUFFER_MAX_RETRY 1
+
+/*
+ * Callback through which the result of a write request is reported by the
+ * SCLP.
+ */
+static void
+sclp_vt220_callback(struct sclp_req *request, void *data)
+{
+ struct sclp_vt220_request *vt220_request;
+ struct sclp_vt220_sccb *sccb;
+
+ vt220_request = (struct sclp_vt220_request *) data;
+ if (request->status == SCLP_REQ_FAILED) {
+ sclp_vt220_process_queue(vt220_request);
+ return;
+ }
+ sccb = (struct sclp_vt220_sccb *) vt220_request->sclp_req.sccb;
+
+ /* Check SCLP response code and choose suitable action */
+ switch (sccb->header.response_code) {
+ case 0x0020 :
+ break;
+
+ case 0x05f0: /* Target resource in improper state */
+ break;
+
+ case 0x0340: /* Contained SCLP equipment check */
+ if (++vt220_request->retry_count > SCLP_BUFFER_MAX_RETRY)
+ break;
+ /* Remove processed buffers and requeue rest */
+ if (sclp_remove_processed((struct sccb_header *) sccb) > 0) {
+ /* Not all buffers were processed */
+ sccb->header.response_code = 0x0000;
+ vt220_request->sclp_req.status = SCLP_REQ_FILLED;
+ if (sclp_add_request(request) == 0)
+ return;
+ }
+ break;
+
+ case 0x0040: /* SCLP equipment check */
+ if (++vt220_request->retry_count > SCLP_BUFFER_MAX_RETRY)
+ break;
+ sccb->header.response_code = 0x0000;
+ vt220_request->sclp_req.status = SCLP_REQ_FILLED;
+ if (sclp_add_request(request) == 0)
+ return;
+ break;
+
+ default:
+ break;
+ }
+ sclp_vt220_process_queue(vt220_request);
+}
+
+/*
+ * Emit vt220 request buffer to SCLP. Return zero on success, non-zero
+ * otherwise.
+ */
+static int
+__sclp_vt220_emit(struct sclp_vt220_request *request)
+{
+ request->sclp_req.command = SCLP_CMDW_WRITE_EVENT_DATA;
+ request->sclp_req.status = SCLP_REQ_FILLED;
+ request->sclp_req.callback = sclp_vt220_callback;
+ request->sclp_req.callback_data = (void *) request;
+
+ return sclp_add_request(&request->sclp_req);
+}
+
+/*
+ * Queue and emit current request.
+ */
+static void
+sclp_vt220_emit_current(void)
+{
+ unsigned long flags;
+ struct sclp_vt220_request *request;
+ struct sclp_vt220_sccb *sccb;
+
+ spin_lock_irqsave(&sclp_vt220_lock, flags);
+ if (sclp_vt220_current_request) {
+ sccb = (struct sclp_vt220_sccb *)
+ sclp_vt220_current_request->sclp_req.sccb;
+ /* Only emit buffers with content */
+ if (sccb->header.length != sizeof(struct sclp_vt220_sccb)) {
+ list_add_tail(&sclp_vt220_current_request->list,
+ &sclp_vt220_outqueue);
+ sclp_vt220_current_request = NULL;
+ del_timer(&sclp_vt220_timer);
+ }
+ sclp_vt220_flush_later = 0;
+ }
+ if (sclp_vt220_queue_running)
+ goto out_unlock;
+ if (list_empty(&sclp_vt220_outqueue))
+ goto out_unlock;
+ request = list_first_entry(&sclp_vt220_outqueue,
+ struct sclp_vt220_request, list);
+ sclp_vt220_queue_running = 1;
+ spin_unlock_irqrestore(&sclp_vt220_lock, flags);
+
+ if (__sclp_vt220_emit(request))
+ sclp_vt220_process_queue(request);
+ return;
+out_unlock:
+ spin_unlock_irqrestore(&sclp_vt220_lock, flags);
+}
+
+#define SCLP_NORMAL_WRITE 0x00
+
+/*
+ * Helper function to initialize a page with the sclp request structure.
+ */
+static struct sclp_vt220_request *
+sclp_vt220_initialize_page(void *page)
+{
+ struct sclp_vt220_request *request;
+ struct sclp_vt220_sccb *sccb;
+
+ /* Place request structure at end of page */
+ request = ((struct sclp_vt220_request *)
+ ((addr_t) page + PAGE_SIZE)) - 1;
+ request->retry_count = 0;
+ request->sclp_req.sccb = page;
+ /* SCCB goes at start of page */
+ sccb = (struct sclp_vt220_sccb *) page;
+ memset((void *) sccb, 0, sizeof(struct sclp_vt220_sccb));
+ sccb->header.length = sizeof(struct sclp_vt220_sccb);
+ sccb->header.function_code = SCLP_NORMAL_WRITE;
+ sccb->header.response_code = 0x0000;
+ sccb->evbuf.type = EVTYP_VT220MSG;
+ sccb->evbuf.length = sizeof(struct evbuf_header);
+
+ return request;
+}
+
+static inline unsigned int
+sclp_vt220_space_left(struct sclp_vt220_request *request)
+{
+ struct sclp_vt220_sccb *sccb;
+ sccb = (struct sclp_vt220_sccb *) request->sclp_req.sccb;
+ return PAGE_SIZE - sizeof(struct sclp_vt220_request) -
+ sccb->header.length;
+}
+
+static inline unsigned int
+sclp_vt220_chars_stored(struct sclp_vt220_request *request)
+{
+ struct sclp_vt220_sccb *sccb;
+ sccb = (struct sclp_vt220_sccb *) request->sclp_req.sccb;
+ return sccb->evbuf.length - sizeof(struct evbuf_header);
+}
+
+/*
+ * Add msg to buffer associated with request. Return the number of characters
+ * added.
+ */
+static int
+sclp_vt220_add_msg(struct sclp_vt220_request *request,
+ const unsigned char *msg, int count, int convertlf)
+{
+ struct sclp_vt220_sccb *sccb;
+ void *buffer;
+ unsigned char c;
+ int from;
+ int to;
+
+ if (count > sclp_vt220_space_left(request))
+ count = sclp_vt220_space_left(request);
+ if (count <= 0)
+ return 0;
+
+ sccb = (struct sclp_vt220_sccb *) request->sclp_req.sccb;
+ buffer = (void *) ((addr_t) sccb + sccb->header.length);
+
+ if (convertlf) {
+ /* Perform Linefeed conversion (0x0a -> 0x0a 0x0d)*/
+ for (from=0, to=0;
+ (from < count) && (to < sclp_vt220_space_left(request));
+ from++) {
+ /* Retrieve character */
+ c = msg[from];
+ /* Perform conversion */
+ if (c == 0x0a) {
+ if (to + 1 < sclp_vt220_space_left(request)) {
+ ((unsigned char *) buffer)[to++] = c;
+ ((unsigned char *) buffer)[to++] = 0x0d;
+ } else
+ break;
+
+ } else
+ ((unsigned char *) buffer)[to++] = c;
+ }
+ sccb->header.length += to;
+ sccb->evbuf.length += to;
+ return from;
+ } else {
+ memcpy(buffer, (const void *) msg, count);
+ sccb->header.length += count;
+ sccb->evbuf.length += count;
+ return count;
+ }
+}
+
+/*
+ * Emit buffer after having waited long enough for more data to arrive.
+ */
+static void
+sclp_vt220_timeout(struct timer_list *unused)
+{
+ sclp_vt220_emit_current();
+}
+
+#define BUFFER_MAX_DELAY HZ/20
+
+/*
+ * Drop oldest console buffer if sclp_con_drop is set
+ */
+static int
+sclp_vt220_drop_buffer(void)
+{
+ struct list_head *list;
+ struct sclp_vt220_request *request;
+ void *page;
+
+ if (!sclp_console_drop)
+ return 0;
+ list = sclp_vt220_outqueue.next;
+ if (sclp_vt220_queue_running)
+ /* The first element is in I/O */
+ list = list->next;
+ if (list == &sclp_vt220_outqueue)
+ return 0;
+ list_del(list);
+ request = list_entry(list, struct sclp_vt220_request, list);
+ page = request->sclp_req.sccb;
+ list_add_tail((struct list_head *) page, &sclp_vt220_empty);
+ return 1;
+}
+
+/*
+ * Internal implementation of the write function. Write COUNT bytes of data
+ * from memory at BUF
+ * to the SCLP interface. In case that the data does not fit into the current
+ * write buffer, emit the current one and allocate a new one. If there are no
+ * more empty buffers available, wait until one gets emptied. If DO_SCHEDULE
+ * is non-zero, the buffer will be scheduled for emitting after a timeout -
+ * otherwise the user has to explicitly call the flush function.
+ * A non-zero CONVERTLF parameter indicates that 0x0a characters in the message
+ * buffer should be converted to 0x0a 0x0d. After completion, return the number
+ * of bytes written.
+ */
+static int
+__sclp_vt220_write(const unsigned char *buf, int count, int do_schedule,
+ int convertlf, int may_fail)
+{
+ unsigned long flags;
+ void *page;
+ int written;
+ int overall_written;
+
+ if (count <= 0)
+ return 0;
+ overall_written = 0;
+ spin_lock_irqsave(&sclp_vt220_lock, flags);
+ do {
+ /* Create an sclp output buffer if none exists yet */
+ if (sclp_vt220_current_request == NULL) {
+ if (list_empty(&sclp_vt220_empty))
+ sclp_console_full++;
+ while (list_empty(&sclp_vt220_empty)) {
+ if (may_fail)
+ goto out;
+ if (sclp_vt220_drop_buffer())
+ break;
+ spin_unlock_irqrestore(&sclp_vt220_lock, flags);
+
+ sclp_sync_wait();
+ spin_lock_irqsave(&sclp_vt220_lock, flags);
+ }
+ page = (void *) sclp_vt220_empty.next;
+ list_del((struct list_head *) page);
+ sclp_vt220_current_request =
+ sclp_vt220_initialize_page(page);
+ }
+ /* Try to write the string to the current request buffer */
+ written = sclp_vt220_add_msg(sclp_vt220_current_request,
+ buf, count, convertlf);
+ overall_written += written;
+ if (written == count)
+ break;
+ /*
+ * Not all characters could be written to the current
+ * output buffer. Emit the buffer, create a new buffer
+ * and then output the rest of the string.
+ */
+ spin_unlock_irqrestore(&sclp_vt220_lock, flags);
+ sclp_vt220_emit_current();
+ spin_lock_irqsave(&sclp_vt220_lock, flags);
+ buf += written;
+ count -= written;
+ } while (count > 0);
+ /* Setup timer to output current console buffer after some time */
+ if (sclp_vt220_current_request != NULL &&
+ !timer_pending(&sclp_vt220_timer) && do_schedule) {
+ sclp_vt220_timer.expires = jiffies + BUFFER_MAX_DELAY;
+ add_timer(&sclp_vt220_timer);
+ }
+out:
+ spin_unlock_irqrestore(&sclp_vt220_lock, flags);
+ return overall_written;
+}
+
+/*
+ * This routine is called by the kernel to write a series of
+ * characters to the tty device. The characters may come from
+ * user space or kernel space. This routine will return the
+ * number of characters actually accepted for writing.
+ */
+static int
+sclp_vt220_write(struct tty_struct *tty, const unsigned char *buf, int count)
+{
+ return __sclp_vt220_write(buf, count, 1, 0, 1);
+}
+
+#define SCLP_VT220_SESSION_ENDED 0x01
+#define SCLP_VT220_SESSION_STARTED 0x80
+#define SCLP_VT220_SESSION_DATA 0x00
+
+#ifdef CONFIG_MAGIC_SYSRQ
+
+static int sysrq_pressed;
+static struct sysrq_work sysrq;
+
+static void sclp_vt220_reset_session(void)
+{
+ sysrq_pressed = 0;
+}
+
+static void sclp_vt220_handle_input(const char *buffer, unsigned int count)
+{
+ int i;
+
+ for (i = 0; i < count; i++) {
+ /* Handle magic sys request */
+ if (buffer[i] == ('O' ^ 0100)) { /* CTRL-O */
+ /*
+ * If pressed again, reset sysrq_pressed
+ * and flip CTRL-O character
+ */
+ sysrq_pressed = !sysrq_pressed;
+ if (sysrq_pressed)
+ continue;
+ } else if (sysrq_pressed) {
+ sysrq.key = buffer[i];
+ schedule_sysrq_work(&sysrq);
+ sysrq_pressed = 0;
+ continue;
+ }
+ tty_insert_flip_char(&sclp_vt220_port, buffer[i], 0);
+ }
+}
+
+#else
+
+static void sclp_vt220_reset_session(void)
+{
+}
+
+static void sclp_vt220_handle_input(const char *buffer, unsigned int count)
+{
+ tty_insert_flip_string(&sclp_vt220_port, buffer, count);
+}
+
+#endif
+
+/*
+ * Called by the SCLP to report incoming event buffers.
+ */
+static void
+sclp_vt220_receiver_fn(struct evbuf_header *evbuf)
+{
+ char *buffer;
+ unsigned int count;
+
+ buffer = (char *) ((addr_t) evbuf + sizeof(struct evbuf_header));
+ count = evbuf->length - sizeof(struct evbuf_header);
+
+ switch (*buffer) {
+ case SCLP_VT220_SESSION_ENDED:
+ case SCLP_VT220_SESSION_STARTED:
+ sclp_vt220_reset_session();
+ break;
+ case SCLP_VT220_SESSION_DATA:
+ /* Send input to line discipline */
+ buffer++;
+ count--;
+ sclp_vt220_handle_input(buffer, count);
+ tty_flip_buffer_push(&sclp_vt220_port);
+ break;
+ }
+}
+
+/*
+ * This routine is called when a particular tty device is opened.
+ */
+static int
+sclp_vt220_open(struct tty_struct *tty, struct file *filp)
+{
+ if (tty->count == 1) {
+ tty_port_tty_set(&sclp_vt220_port, tty);
+ if (!tty->winsize.ws_row && !tty->winsize.ws_col) {
+ tty->winsize.ws_row = 24;
+ tty->winsize.ws_col = 80;
+ }
+ }
+ return 0;
+}
+
+/*
+ * This routine is called when a particular tty device is closed.
+ */
+static void
+sclp_vt220_close(struct tty_struct *tty, struct file *filp)
+{
+ if (tty->count == 1)
+ tty_port_tty_set(&sclp_vt220_port, NULL);
+}
+
+/*
+ * This routine is called by the kernel to write a single
+ * character to the tty device. If the kernel uses this routine,
+ * it must call the flush_chars() routine (if defined) when it is
+ * done stuffing characters into the driver.
+ */
+static int
+sclp_vt220_put_char(struct tty_struct *tty, unsigned char ch)
+{
+ return __sclp_vt220_write(&ch, 1, 0, 0, 1);
+}
+
+/*
+ * This routine is called by the kernel after it has written a
+ * series of characters to the tty device using put_char().
+ */
+static void
+sclp_vt220_flush_chars(struct tty_struct *tty)
+{
+ if (!sclp_vt220_queue_running)
+ sclp_vt220_emit_current();
+ else
+ sclp_vt220_flush_later = 1;
+}
+
+/*
+ * This routine returns the numbers of characters the tty driver
+ * will accept for queuing to be written. This number is subject
+ * to change as output buffers get emptied, or if the output flow
+ * control is acted.
+ */
+static unsigned int
+sclp_vt220_write_room(struct tty_struct *tty)
+{
+ unsigned long flags;
+ struct list_head *l;
+ unsigned int count;
+
+ spin_lock_irqsave(&sclp_vt220_lock, flags);
+ count = 0;
+ if (sclp_vt220_current_request != NULL)
+ count = sclp_vt220_space_left(sclp_vt220_current_request);
+ list_for_each(l, &sclp_vt220_empty)
+ count += SCLP_VT220_MAX_CHARS_PER_BUFFER;
+ spin_unlock_irqrestore(&sclp_vt220_lock, flags);
+ return count;
+}
+
+/*
+ * Return number of buffered chars.
+ */
+static unsigned int
+sclp_vt220_chars_in_buffer(struct tty_struct *tty)
+{
+ unsigned long flags;
+ struct list_head *l;
+ struct sclp_vt220_request *r;
+ unsigned int count = 0;
+
+ spin_lock_irqsave(&sclp_vt220_lock, flags);
+ if (sclp_vt220_current_request != NULL)
+ count = sclp_vt220_chars_stored(sclp_vt220_current_request);
+ list_for_each(l, &sclp_vt220_outqueue) {
+ r = list_entry(l, struct sclp_vt220_request, list);
+ count += sclp_vt220_chars_stored(r);
+ }
+ spin_unlock_irqrestore(&sclp_vt220_lock, flags);
+ return count;
+}
+
+/*
+ * Pass on all buffers to the hardware. Return only when there are no more
+ * buffers pending.
+ */
+static void
+sclp_vt220_flush_buffer(struct tty_struct *tty)
+{
+ sclp_vt220_emit_current();
+}
+
+/* Release allocated pages. */
+static void __init __sclp_vt220_free_pages(void)
+{
+ struct list_head *page, *p;
+
+ list_for_each_safe(page, p, &sclp_vt220_empty) {
+ list_del(page);
+ free_page((unsigned long) page);
+ }
+}
+
+/* Release memory and unregister from sclp core. Controlled by init counting -
+ * only the last invoker will actually perform these actions. */
+static void __init __sclp_vt220_cleanup(void)
+{
+ sclp_vt220_init_count--;
+ if (sclp_vt220_init_count != 0)
+ return;
+ sclp_unregister(&sclp_vt220_register);
+ __sclp_vt220_free_pages();
+ tty_port_destroy(&sclp_vt220_port);
+}
+
+/* Allocate buffer pages and register with sclp core. Controlled by init
+ * counting - only the first invoker will actually perform these actions. */
+static int __init __sclp_vt220_init(int num_pages)
+{
+ void *page;
+ int i;
+ int rc;
+
+ sclp_vt220_init_count++;
+ if (sclp_vt220_init_count != 1)
+ return 0;
+ timer_setup(&sclp_vt220_timer, sclp_vt220_timeout, 0);
+ tty_port_init(&sclp_vt220_port);
+ sclp_vt220_current_request = NULL;
+ sclp_vt220_buffered_chars = 0;
+ sclp_vt220_flush_later = 0;
+
+ /* Allocate pages for output buffering */
+ rc = -ENOMEM;
+ for (i = 0; i < num_pages; i++) {
+ page = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
+ if (!page)
+ goto out;
+ list_add_tail(page, &sclp_vt220_empty);
+ }
+ rc = sclp_register(&sclp_vt220_register);
+out:
+ if (rc) {
+ __sclp_vt220_free_pages();
+ sclp_vt220_init_count--;
+ tty_port_destroy(&sclp_vt220_port);
+ }
+ return rc;
+}
+
+static const struct tty_operations sclp_vt220_ops = {
+ .open = sclp_vt220_open,
+ .close = sclp_vt220_close,
+ .write = sclp_vt220_write,
+ .put_char = sclp_vt220_put_char,
+ .flush_chars = sclp_vt220_flush_chars,
+ .write_room = sclp_vt220_write_room,
+ .chars_in_buffer = sclp_vt220_chars_in_buffer,
+ .flush_buffer = sclp_vt220_flush_buffer,
+};
+
+/*
+ * Register driver with SCLP and Linux and initialize internal tty structures.
+ */
+static int __init sclp_vt220_tty_init(void)
+{
+ struct tty_driver *driver;
+ int rc;
+
+ /* Note: we're not testing for CONSOLE_IS_SCLP here to preserve
+ * symmetry between VM and LPAR systems regarding ttyS1. */
+ driver = tty_alloc_driver(1, TTY_DRIVER_REAL_RAW);
+ if (IS_ERR(driver))
+ return PTR_ERR(driver);
+ rc = __sclp_vt220_init(MAX_KMEM_PAGES);
+ if (rc)
+ goto out_driver;
+
+ driver->driver_name = SCLP_VT220_DRIVER_NAME;
+ driver->name = SCLP_VT220_DEVICE_NAME;
+ driver->major = SCLP_VT220_MAJOR;
+ driver->minor_start = SCLP_VT220_MINOR;
+ driver->type = TTY_DRIVER_TYPE_SYSTEM;
+ driver->subtype = SYSTEM_TYPE_TTY;
+ driver->init_termios = tty_std_termios;
+ tty_set_operations(driver, &sclp_vt220_ops);
+ tty_port_link_device(&sclp_vt220_port, driver, 0);
+
+ rc = tty_register_driver(driver);
+ if (rc)
+ goto out_init;
+ rc = sclp_register(&sclp_vt220_register_input);
+ if (rc)
+ goto out_reg;
+ sclp_vt220_driver = driver;
+ return 0;
+
+out_reg:
+ tty_unregister_driver(driver);
+out_init:
+ __sclp_vt220_cleanup();
+out_driver:
+ tty_driver_kref_put(driver);
+ return rc;
+}
+__initcall(sclp_vt220_tty_init);
+
+#ifdef CONFIG_SCLP_VT220_CONSOLE
+
+static void
+sclp_vt220_con_write(struct console *con, const char *buf, unsigned int count)
+{
+ __sclp_vt220_write((const unsigned char *) buf, count, 1, 1, 0);
+}
+
+static struct tty_driver *
+sclp_vt220_con_device(struct console *c, int *index)
+{
+ *index = 0;
+ return sclp_vt220_driver;
+}
+
+/*
+ * This panic/reboot notifier runs in atomic context, so
+ * locking restrictions apply to prevent potential lockups.
+ */
+static int
+sclp_vt220_notify(struct notifier_block *self,
+ unsigned long event, void *data)
+{
+ unsigned long flags;
+
+ if (spin_is_locked(&sclp_vt220_lock))
+ return NOTIFY_DONE;
+
+ sclp_vt220_emit_current();
+
+ spin_lock_irqsave(&sclp_vt220_lock, flags);
+ del_timer(&sclp_vt220_timer);
+ while (sclp_vt220_queue_running) {
+ spin_unlock_irqrestore(&sclp_vt220_lock, flags);
+ sclp_sync_wait();
+ spin_lock_irqsave(&sclp_vt220_lock, flags);
+ }
+ spin_unlock_irqrestore(&sclp_vt220_lock, flags);
+
+ return NOTIFY_DONE;
+}
+
+static struct notifier_block on_panic_nb = {
+ .notifier_call = sclp_vt220_notify,
+ .priority = INT_MIN + 1, /* run the callback late */
+};
+
+static struct notifier_block on_reboot_nb = {
+ .notifier_call = sclp_vt220_notify,
+ .priority = INT_MIN + 1, /* run the callback late */
+};
+
+/* Structure needed to register with printk */
+static struct console sclp_vt220_console =
+{
+ .name = SCLP_VT220_CONSOLE_NAME,
+ .write = sclp_vt220_con_write,
+ .device = sclp_vt220_con_device,
+ .flags = CON_PRINTBUFFER,
+ .index = SCLP_VT220_CONSOLE_INDEX
+};
+
+static int __init
+sclp_vt220_con_init(void)
+{
+ int rc;
+
+ rc = __sclp_vt220_init(sclp_console_pages);
+ if (rc)
+ return rc;
+ /* Attach linux console */
+ atomic_notifier_chain_register(&panic_notifier_list, &on_panic_nb);
+ register_reboot_notifier(&on_reboot_nb);
+ register_console(&sclp_vt220_console);
+ return 0;
+}
+
+console_initcall(sclp_vt220_con_init);
+#endif /* CONFIG_SCLP_VT220_CONSOLE */
+