hvc_iucv.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479
  1. /*
  2. * z/VM IUCV hypervisor console (HVC) device driver
  3. *
  4. * This HVC device driver provides terminal access using
  5. * z/VM IUCV communication paths.
  6. *
  7. * Copyright IBM Corp. 2008, 2013
  8. *
  9. * Author(s): Hendrik Brueckner <brueckner@linux.vnet.ibm.com>
  10. */
  11. #define KMSG_COMPONENT "hvc_iucv"
  12. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  13. #include <linux/types.h>
  14. #include <linux/slab.h>
  15. #include <asm/ebcdic.h>
  16. #include <linux/ctype.h>
  17. #include <linux/delay.h>
  18. #include <linux/device.h>
  19. #include <linux/init.h>
  20. #include <linux/mempool.h>
  21. #include <linux/moduleparam.h>
  22. #include <linux/tty.h>
  23. #include <linux/wait.h>
  24. #include <net/iucv/iucv.h>
  25. #include "hvc_console.h"
  26. /* General device driver settings */
  27. #define HVC_IUCV_MAGIC 0xc9e4c3e5
  28. #define MAX_HVC_IUCV_LINES HVC_ALLOC_TTY_ADAPTERS
  29. #define MEMPOOL_MIN_NR (PAGE_SIZE / sizeof(struct iucv_tty_buffer)/4)
  30. /* IUCV TTY message */
  31. #define MSG_VERSION 0x02 /* Message version */
  32. #define MSG_TYPE_ERROR 0x01 /* Error message */
  33. #define MSG_TYPE_TERMENV 0x02 /* Terminal environment variable */
  34. #define MSG_TYPE_TERMIOS 0x04 /* Terminal IO struct update */
  35. #define MSG_TYPE_WINSIZE 0x08 /* Terminal window size update */
  36. #define MSG_TYPE_DATA 0x10 /* Terminal data */
  37. struct iucv_tty_msg {
  38. u8 version; /* Message version */
  39. u8 type; /* Message type */
  40. #define MSG_MAX_DATALEN ((u16)(~0))
  41. u16 datalen; /* Payload length */
  42. u8 data[]; /* Payload buffer */
  43. } __attribute__((packed));
  44. #define MSG_SIZE(s) ((s) + offsetof(struct iucv_tty_msg, data))
  45. enum iucv_state_t {
  46. IUCV_DISCONN = 0,
  47. IUCV_CONNECTED = 1,
  48. IUCV_SEVERED = 2,
  49. };
  50. enum tty_state_t {
  51. TTY_CLOSED = 0,
  52. TTY_OPENED = 1,
  53. };
  54. struct hvc_iucv_private {
  55. struct hvc_struct *hvc; /* HVC struct reference */
  56. u8 srv_name[8]; /* IUCV service name (ebcdic) */
  57. unsigned char is_console; /* Linux console usage flag */
  58. enum iucv_state_t iucv_state; /* IUCV connection status */
  59. enum tty_state_t tty_state; /* TTY status */
  60. struct iucv_path *path; /* IUCV path pointer */
  61. spinlock_t lock; /* hvc_iucv_private lock */
  62. #define SNDBUF_SIZE (PAGE_SIZE) /* must be < MSG_MAX_DATALEN */
  63. void *sndbuf; /* send buffer */
  64. size_t sndbuf_len; /* length of send buffer */
  65. #define QUEUE_SNDBUF_DELAY (HZ / 25)
  66. struct delayed_work sndbuf_work; /* work: send iucv msg(s) */
  67. wait_queue_head_t sndbuf_waitq; /* wait for send completion */
  68. struct list_head tty_outqueue; /* outgoing IUCV messages */
  69. struct list_head tty_inqueue; /* incoming IUCV messages */
  70. struct device *dev; /* device structure */
  71. u8 info_path[16]; /* IUCV path info (dev attr) */
  72. };
  73. struct iucv_tty_buffer {
  74. struct list_head list; /* list pointer */
  75. struct iucv_message msg; /* store an IUCV message */
  76. size_t offset; /* data buffer offset */
  77. struct iucv_tty_msg *mbuf; /* buffer to store input/output data */
  78. };
  79. /* IUCV callback handler */
  80. static int hvc_iucv_path_pending(struct iucv_path *, u8[8], u8[16]);
  81. static void hvc_iucv_path_severed(struct iucv_path *, u8[16]);
  82. static void hvc_iucv_msg_pending(struct iucv_path *, struct iucv_message *);
  83. static void hvc_iucv_msg_complete(struct iucv_path *, struct iucv_message *);
  84. /* Kernel module parameter: use one terminal device as default */
  85. static unsigned long hvc_iucv_devices = 1;
  86. /* Array of allocated hvc iucv tty lines... */
  87. static struct hvc_iucv_private *hvc_iucv_table[MAX_HVC_IUCV_LINES];
  88. #define IUCV_HVC_CON_IDX (0)
  89. /* List of z/VM user ID filter entries (struct iucv_vmid_filter) */
  90. #define MAX_VMID_FILTER (500)
  91. #define FILTER_WILDCARD_CHAR '*'
  92. static size_t hvc_iucv_filter_size;
  93. static void *hvc_iucv_filter;
  94. static const char *hvc_iucv_filter_string;
  95. static DEFINE_RWLOCK(hvc_iucv_filter_lock);
  96. /* Kmem cache and mempool for iucv_tty_buffer elements */
  97. static struct kmem_cache *hvc_iucv_buffer_cache;
  98. static mempool_t *hvc_iucv_mempool;
  99. /* IUCV handler callback functions */
  100. static struct iucv_handler hvc_iucv_handler = {
  101. .path_pending = hvc_iucv_path_pending,
  102. .path_severed = hvc_iucv_path_severed,
  103. .message_complete = hvc_iucv_msg_complete,
  104. .message_pending = hvc_iucv_msg_pending,
  105. };
  106. /**
  107. * hvc_iucv_get_private() - Return a struct hvc_iucv_private instance.
  108. * @num: The HVC virtual terminal number (vtermno)
  109. *
  110. * This function returns the struct hvc_iucv_private instance that corresponds
  111. * to the HVC virtual terminal number specified as parameter @num.
  112. */
  113. static struct hvc_iucv_private *hvc_iucv_get_private(uint32_t num)
  114. {
  115. if ((num < HVC_IUCV_MAGIC) || (num - HVC_IUCV_MAGIC > hvc_iucv_devices))
  116. return NULL;
  117. return hvc_iucv_table[num - HVC_IUCV_MAGIC];
  118. }
  119. /**
  120. * alloc_tty_buffer() - Return a new struct iucv_tty_buffer element.
  121. * @size: Size of the internal buffer used to store data.
  122. * @flags: Memory allocation flags passed to mempool.
  123. *
  124. * This function allocates a new struct iucv_tty_buffer element and, optionally,
  125. * allocates an internal data buffer with the specified size @size.
  126. * The internal data buffer is always allocated with GFP_DMA which is
  127. * required for receiving and sending data with IUCV.
  128. * Note: The total message size arises from the internal buffer size and the
  129. * members of the iucv_tty_msg structure.
  130. * The function returns NULL if memory allocation has failed.
  131. */
  132. static struct iucv_tty_buffer *alloc_tty_buffer(size_t size, gfp_t flags)
  133. {
  134. struct iucv_tty_buffer *bufp;
  135. bufp = mempool_alloc(hvc_iucv_mempool, flags);
  136. if (!bufp)
  137. return NULL;
  138. memset(bufp, 0, sizeof(*bufp));
  139. if (size > 0) {
  140. bufp->msg.length = MSG_SIZE(size);
  141. bufp->mbuf = kmalloc(bufp->msg.length, flags | GFP_DMA);
  142. if (!bufp->mbuf) {
  143. mempool_free(bufp, hvc_iucv_mempool);
  144. return NULL;
  145. }
  146. bufp->mbuf->version = MSG_VERSION;
  147. bufp->mbuf->type = MSG_TYPE_DATA;
  148. bufp->mbuf->datalen = (u16) size;
  149. }
  150. return bufp;
  151. }
  152. /**
  153. * destroy_tty_buffer() - destroy struct iucv_tty_buffer element.
  154. * @bufp: Pointer to a struct iucv_tty_buffer element, SHALL NOT be NULL.
  155. */
  156. static void destroy_tty_buffer(struct iucv_tty_buffer *bufp)
  157. {
  158. kfree(bufp->mbuf);
  159. mempool_free(bufp, hvc_iucv_mempool);
  160. }
  161. /**
  162. * destroy_tty_buffer_list() - call destroy_tty_buffer() for each list element.
  163. * @list: List containing struct iucv_tty_buffer elements.
  164. */
  165. static void destroy_tty_buffer_list(struct list_head *list)
  166. {
  167. struct iucv_tty_buffer *ent, *next;
  168. list_for_each_entry_safe(ent, next, list, list) {
  169. list_del(&ent->list);
  170. destroy_tty_buffer(ent);
  171. }
  172. }
  173. /**
  174. * hvc_iucv_write() - Receive IUCV message & write data to HVC buffer.
  175. * @priv: Pointer to struct hvc_iucv_private
  176. * @buf: HVC buffer for writing received terminal data.
  177. * @count: HVC buffer size.
  178. * @has_more_data: Pointer to an int variable.
  179. *
  180. * The function picks up pending messages from the input queue and receives
  181. * the message data that is then written to the specified buffer @buf.
  182. * If the buffer size @count is less than the data message size, the
  183. * message is kept on the input queue and @has_more_data is set to 1.
  184. * If all message data has been written, the message is removed from
  185. * the input queue.
  186. *
  187. * The function returns the number of bytes written to the terminal, zero if
  188. * there are no pending data messages available or if there is no established
  189. * IUCV path.
  190. * If the IUCV path has been severed, then -EPIPE is returned to cause a
  191. * hang up (that is issued by the HVC layer).
  192. */
  193. static int hvc_iucv_write(struct hvc_iucv_private *priv,
  194. char *buf, int count, int *has_more_data)
  195. {
  196. struct iucv_tty_buffer *rb;
  197. int written;
  198. int rc;
  199. /* immediately return if there is no IUCV connection */
  200. if (priv->iucv_state == IUCV_DISCONN)
  201. return 0;
  202. /* if the IUCV path has been severed, return -EPIPE to inform the
  203. * HVC layer to hang up the tty device. */
  204. if (priv->iucv_state == IUCV_SEVERED)
  205. return -EPIPE;
  206. /* check if there are pending messages */
  207. if (list_empty(&priv->tty_inqueue))
  208. return 0;
  209. /* receive an iucv message and flip data to the tty (ldisc) */
  210. rb = list_first_entry(&priv->tty_inqueue, struct iucv_tty_buffer, list);
  211. written = 0;
  212. if (!rb->mbuf) { /* message not yet received ... */
  213. /* allocate mem to store msg data; if no memory is available
  214. * then leave the buffer on the list and re-try later */
  215. rb->mbuf = kmalloc(rb->msg.length, GFP_ATOMIC | GFP_DMA);
  216. if (!rb->mbuf)
  217. return -ENOMEM;
  218. rc = __iucv_message_receive(priv->path, &rb->msg, 0,
  219. rb->mbuf, rb->msg.length, NULL);
  220. switch (rc) {
  221. case 0: /* Successful */
  222. break;
  223. case 2: /* No message found */
  224. case 9: /* Message purged */
  225. break;
  226. default:
  227. written = -EIO;
  228. }
  229. /* remove buffer if an error has occurred or received data
  230. * is not correct */
  231. if (rc || (rb->mbuf->version != MSG_VERSION) ||
  232. (rb->msg.length != MSG_SIZE(rb->mbuf->datalen)))
  233. goto out_remove_buffer;
  234. }
  235. switch (rb->mbuf->type) {
  236. case MSG_TYPE_DATA:
  237. written = min_t(int, rb->mbuf->datalen - rb->offset, count);
  238. memcpy(buf, rb->mbuf->data + rb->offset, written);
  239. if (written < (rb->mbuf->datalen - rb->offset)) {
  240. rb->offset += written;
  241. *has_more_data = 1;
  242. goto out_written;
  243. }
  244. break;
  245. case MSG_TYPE_WINSIZE:
  246. if (rb->mbuf->datalen != sizeof(struct winsize))
  247. break;
  248. /* The caller must ensure that the hvc is locked, which
  249. * is the case when called from hvc_iucv_get_chars() */
  250. __hvc_resize(priv->hvc, *((struct winsize *) rb->mbuf->data));
  251. break;
  252. case MSG_TYPE_ERROR: /* ignored ... */
  253. case MSG_TYPE_TERMENV: /* ignored ... */
  254. case MSG_TYPE_TERMIOS: /* ignored ... */
  255. break;
  256. }
  257. out_remove_buffer:
  258. list_del(&rb->list);
  259. destroy_tty_buffer(rb);
  260. *has_more_data = !list_empty(&priv->tty_inqueue);
  261. out_written:
  262. return written;
  263. }
  264. /**
  265. * hvc_iucv_get_chars() - HVC get_chars operation.
  266. * @vtermno: HVC virtual terminal number.
  267. * @buf: Pointer to a buffer to store data
  268. * @count: Size of buffer available for writing
  269. *
  270. * The HVC thread calls this method to read characters from the back-end.
  271. * If an IUCV communication path has been established, pending IUCV messages
  272. * are received and data is copied into buffer @buf up to @count bytes.
  273. *
  274. * Locking: The routine gets called under an irqsave() spinlock; and
  275. * the routine locks the struct hvc_iucv_private->lock to call
  276. * helper functions.
  277. */
  278. static int hvc_iucv_get_chars(uint32_t vtermno, char *buf, int count)
  279. {
  280. struct hvc_iucv_private *priv = hvc_iucv_get_private(vtermno);
  281. int written;
  282. int has_more_data;
  283. if (count <= 0)
  284. return 0;
  285. if (!priv)
  286. return -ENODEV;
  287. spin_lock(&priv->lock);
  288. has_more_data = 0;
  289. written = hvc_iucv_write(priv, buf, count, &has_more_data);
  290. spin_unlock(&priv->lock);
  291. /* if there are still messages on the queue... schedule another run */
  292. if (has_more_data)
  293. hvc_kick();
  294. return written;
  295. }
  296. /**
  297. * hvc_iucv_queue() - Buffer terminal data for sending.
  298. * @priv: Pointer to struct hvc_iucv_private instance.
  299. * @buf: Buffer containing data to send.
  300. * @count: Size of buffer and amount of data to send.
  301. *
  302. * The function queues data for sending. To actually send the buffered data,
  303. * a work queue function is scheduled (with QUEUE_SNDBUF_DELAY).
  304. * The function returns the number of data bytes that has been buffered.
  305. *
  306. * If the device is not connected, data is ignored and the function returns
  307. * @count.
  308. * If the buffer is full, the function returns 0.
  309. * If an existing IUCV communicaton path has been severed, -EPIPE is returned
  310. * (that can be passed to HVC layer to cause a tty hangup).
  311. */
  312. static int hvc_iucv_queue(struct hvc_iucv_private *priv, const char *buf,
  313. int count)
  314. {
  315. size_t len;
  316. if (priv->iucv_state == IUCV_DISCONN)
  317. return count; /* ignore data */
  318. if (priv->iucv_state == IUCV_SEVERED)
  319. return -EPIPE;
  320. len = min_t(size_t, count, SNDBUF_SIZE - priv->sndbuf_len);
  321. if (!len)
  322. return 0;
  323. memcpy(priv->sndbuf + priv->sndbuf_len, buf, len);
  324. priv->sndbuf_len += len;
  325. if (priv->iucv_state == IUCV_CONNECTED)
  326. schedule_delayed_work(&priv->sndbuf_work, QUEUE_SNDBUF_DELAY);
  327. return len;
  328. }
  329. /**
  330. * hvc_iucv_send() - Send an IUCV message containing terminal data.
  331. * @priv: Pointer to struct hvc_iucv_private instance.
  332. *
  333. * If an IUCV communication path has been established, the buffered output data
  334. * is sent via an IUCV message and the number of bytes sent is returned.
  335. * Returns 0 if there is no established IUCV communication path or
  336. * -EPIPE if an existing IUCV communicaton path has been severed.
  337. */
  338. static int hvc_iucv_send(struct hvc_iucv_private *priv)
  339. {
  340. struct iucv_tty_buffer *sb;
  341. int rc, len;
  342. if (priv->iucv_state == IUCV_SEVERED)
  343. return -EPIPE;
  344. if (priv->iucv_state == IUCV_DISCONN)
  345. return -EIO;
  346. if (!priv->sndbuf_len)
  347. return 0;
  348. /* allocate internal buffer to store msg data and also compute total
  349. * message length */
  350. sb = alloc_tty_buffer(priv->sndbuf_len, GFP_ATOMIC);
  351. if (!sb)
  352. return -ENOMEM;
  353. memcpy(sb->mbuf->data, priv->sndbuf, priv->sndbuf_len);
  354. sb->mbuf->datalen = (u16) priv->sndbuf_len;
  355. sb->msg.length = MSG_SIZE(sb->mbuf->datalen);
  356. list_add_tail(&sb->list, &priv->tty_outqueue);
  357. rc = __iucv_message_send(priv->path, &sb->msg, 0, 0,
  358. (void *) sb->mbuf, sb->msg.length);
  359. if (rc) {
  360. /* drop the message here; however we might want to handle
  361. * 0x03 (msg limit reached) by trying again... */
  362. list_del(&sb->list);
  363. destroy_tty_buffer(sb);
  364. }
  365. len = priv->sndbuf_len;
  366. priv->sndbuf_len = 0;
  367. return len;
  368. }
  369. /**
  370. * hvc_iucv_sndbuf_work() - Send buffered data over IUCV
  371. * @work: Work structure.
  372. *
  373. * This work queue function sends buffered output data over IUCV and,
  374. * if not all buffered data could be sent, reschedules itself.
  375. */
  376. static void hvc_iucv_sndbuf_work(struct work_struct *work)
  377. {
  378. struct hvc_iucv_private *priv;
  379. priv = container_of(work, struct hvc_iucv_private, sndbuf_work.work);
  380. if (!priv)
  381. return;
  382. spin_lock_bh(&priv->lock);
  383. hvc_iucv_send(priv);
  384. spin_unlock_bh(&priv->lock);
  385. }
  386. /**
  387. * hvc_iucv_put_chars() - HVC put_chars operation.
  388. * @vtermno: HVC virtual terminal number.
  389. * @buf: Pointer to an buffer to read data from
  390. * @count: Size of buffer available for reading
  391. *
  392. * The HVC thread calls this method to write characters to the back-end.
  393. * The function calls hvc_iucv_queue() to queue terminal data for sending.
  394. *
  395. * Locking: The method gets called under an irqsave() spinlock; and
  396. * locks struct hvc_iucv_private->lock.
  397. */
  398. static int hvc_iucv_put_chars(uint32_t vtermno, const char *buf, int count)
  399. {
  400. struct hvc_iucv_private *priv = hvc_iucv_get_private(vtermno);
  401. int queued;
  402. if (count <= 0)
  403. return 0;
  404. if (!priv)
  405. return -ENODEV;
  406. spin_lock(&priv->lock);
  407. queued = hvc_iucv_queue(priv, buf, count);
  408. spin_unlock(&priv->lock);
  409. return queued;
  410. }
  411. /**
  412. * hvc_iucv_notifier_add() - HVC notifier for opening a TTY for the first time.
  413. * @hp: Pointer to the HVC device (struct hvc_struct)
  414. * @id: Additional data (originally passed to hvc_alloc): the index of an struct
  415. * hvc_iucv_private instance.
  416. *
  417. * The function sets the tty state to TTY_OPENED for the struct hvc_iucv_private
  418. * instance that is derived from @id. Always returns 0.
  419. *
  420. * Locking: struct hvc_iucv_private->lock, spin_lock_bh
  421. */
  422. static int hvc_iucv_notifier_add(struct hvc_struct *hp, int id)
  423. {
  424. struct hvc_iucv_private *priv;
  425. priv = hvc_iucv_get_private(id);
  426. if (!priv)
  427. return 0;
  428. spin_lock_bh(&priv->lock);
  429. priv->tty_state = TTY_OPENED;
  430. spin_unlock_bh(&priv->lock);
  431. return 0;
  432. }
  433. /**
  434. * hvc_iucv_cleanup() - Clean up and reset a z/VM IUCV HVC instance.
  435. * @priv: Pointer to the struct hvc_iucv_private instance.
  436. */
  437. static void hvc_iucv_cleanup(struct hvc_iucv_private *priv)
  438. {
  439. destroy_tty_buffer_list(&priv->tty_outqueue);
  440. destroy_tty_buffer_list(&priv->tty_inqueue);
  441. priv->tty_state = TTY_CLOSED;
  442. priv->iucv_state = IUCV_DISCONN;
  443. priv->sndbuf_len = 0;
  444. }
  445. /**
  446. * tty_outqueue_empty() - Test if the tty outq is empty
  447. * @priv: Pointer to struct hvc_iucv_private instance.
  448. */
  449. static inline int tty_outqueue_empty(struct hvc_iucv_private *priv)
  450. {
  451. int rc;
  452. spin_lock_bh(&priv->lock);
  453. rc = list_empty(&priv->tty_outqueue);
  454. spin_unlock_bh(&priv->lock);
  455. return rc;
  456. }
  457. /**
  458. * flush_sndbuf_sync() - Flush send buffer and wait for completion
  459. * @priv: Pointer to struct hvc_iucv_private instance.
  460. *
  461. * The routine cancels a pending sndbuf work, calls hvc_iucv_send()
  462. * to flush any buffered terminal output data and waits for completion.
  463. */
  464. static void flush_sndbuf_sync(struct hvc_iucv_private *priv)
  465. {
  466. int sync_wait;
  467. cancel_delayed_work_sync(&priv->sndbuf_work);
  468. spin_lock_bh(&priv->lock);
  469. hvc_iucv_send(priv); /* force sending buffered data */
  470. sync_wait = !list_empty(&priv->tty_outqueue); /* anything queued ? */
  471. spin_unlock_bh(&priv->lock);
  472. if (sync_wait)
  473. wait_event_timeout(priv->sndbuf_waitq,
  474. tty_outqueue_empty(priv), HZ/10);
  475. }
  476. /**
  477. * hvc_iucv_hangup() - Sever IUCV path and schedule hvc tty hang up
  478. * @priv: Pointer to hvc_iucv_private structure
  479. *
  480. * This routine severs an existing IUCV communication path and hangs
  481. * up the underlying HVC terminal device.
  482. * The hang-up occurs only if an IUCV communication path is established;
  483. * otherwise there is no need to hang up the terminal device.
  484. *
  485. * The IUCV HVC hang-up is separated into two steps:
  486. * 1. After the IUCV path has been severed, the iucv_state is set to
  487. * IUCV_SEVERED.
  488. * 2. Later, when the HVC thread calls hvc_iucv_get_chars(), the
  489. * IUCV_SEVERED state causes the tty hang-up in the HVC layer.
  490. *
  491. * If the tty has not yet been opened, clean up the hvc_iucv_private
  492. * structure to allow re-connects.
  493. * If the tty has been opened, let get_chars() return -EPIPE to signal
  494. * the HVC layer to hang up the tty and, if so, wake up the HVC thread
  495. * to call get_chars()...
  496. *
  497. * Special notes on hanging up a HVC terminal instantiated as console:
  498. * Hang-up: 1. do_tty_hangup() replaces file ops (= hung_up_tty_fops)
  499. * 2. do_tty_hangup() calls tty->ops->close() for console_filp
  500. * => no hangup notifier is called by HVC (default)
  501. * 2. hvc_close() returns because of tty_hung_up_p(filp)
  502. * => no delete notifier is called!
  503. * Finally, the back-end is not being notified, thus, the tty session is
  504. * kept active (TTY_OPEN) to be ready for re-connects.
  505. *
  506. * Locking: spin_lock(&priv->lock) w/o disabling bh
  507. */
  508. static void hvc_iucv_hangup(struct hvc_iucv_private *priv)
  509. {
  510. struct iucv_path *path;
  511. path = NULL;
  512. spin_lock(&priv->lock);
  513. if (priv->iucv_state == IUCV_CONNECTED) {
  514. path = priv->path;
  515. priv->path = NULL;
  516. priv->iucv_state = IUCV_SEVERED;
  517. if (priv->tty_state == TTY_CLOSED)
  518. hvc_iucv_cleanup(priv);
  519. else
  520. /* console is special (see above) */
  521. if (priv->is_console) {
  522. hvc_iucv_cleanup(priv);
  523. priv->tty_state = TTY_OPENED;
  524. } else
  525. hvc_kick();
  526. }
  527. spin_unlock(&priv->lock);
  528. /* finally sever path (outside of priv->lock due to lock ordering) */
  529. if (path) {
  530. iucv_path_sever(path, NULL);
  531. iucv_path_free(path);
  532. }
  533. }
  534. /**
  535. * hvc_iucv_notifier_hangup() - HVC notifier for TTY hangups.
  536. * @hp: Pointer to the HVC device (struct hvc_struct)
  537. * @id: Additional data (originally passed to hvc_alloc):
  538. * the index of an struct hvc_iucv_private instance.
  539. *
  540. * This routine notifies the HVC back-end that a tty hangup (carrier loss,
  541. * virtual or otherwise) has occurred.
  542. * The z/VM IUCV HVC device driver ignores virtual hangups (vhangup())
  543. * to keep an existing IUCV communication path established.
  544. * (Background: vhangup() is called from user space (by getty or login) to
  545. * disable writing to the tty by other applications).
  546. * If the tty has been opened and an established IUCV path has been severed
  547. * (we caused the tty hangup), the function calls hvc_iucv_cleanup().
  548. *
  549. * Locking: struct hvc_iucv_private->lock
  550. */
  551. static void hvc_iucv_notifier_hangup(struct hvc_struct *hp, int id)
  552. {
  553. struct hvc_iucv_private *priv;
  554. priv = hvc_iucv_get_private(id);
  555. if (!priv)
  556. return;
  557. flush_sndbuf_sync(priv);
  558. spin_lock_bh(&priv->lock);
  559. /* NOTE: If the hangup was scheduled by ourself (from the iucv
  560. * path_servered callback [IUCV_SEVERED]), we have to clean up
  561. * our structure and to set state to TTY_CLOSED.
  562. * If the tty was hung up otherwise (e.g. vhangup()), then we
  563. * ignore this hangup and keep an established IUCV path open...
  564. * (...the reason is that we are not able to connect back to the
  565. * client if we disconnect on hang up) */
  566. priv->tty_state = TTY_CLOSED;
  567. if (priv->iucv_state == IUCV_SEVERED)
  568. hvc_iucv_cleanup(priv);
  569. spin_unlock_bh(&priv->lock);
  570. }
  571. /**
  572. * hvc_iucv_dtr_rts() - HVC notifier for handling DTR/RTS
  573. * @hp: Pointer the HVC device (struct hvc_struct)
  574. * @raise: Non-zero to raise or zero to lower DTR/RTS lines
  575. *
  576. * This routine notifies the HVC back-end to raise or lower DTR/RTS
  577. * lines. Raising DTR/RTS is ignored. Lowering DTR/RTS indicates to
  578. * drop the IUCV connection (similar to hang up the modem).
  579. */
  580. static void hvc_iucv_dtr_rts(struct hvc_struct *hp, int raise)
  581. {
  582. struct hvc_iucv_private *priv;
  583. struct iucv_path *path;
  584. /* Raising the DTR/RTS is ignored as IUCV connections can be
  585. * established at any times.
  586. */
  587. if (raise)
  588. return;
  589. priv = hvc_iucv_get_private(hp->vtermno);
  590. if (!priv)
  591. return;
  592. /* Lowering the DTR/RTS lines disconnects an established IUCV
  593. * connection.
  594. */
  595. flush_sndbuf_sync(priv);
  596. spin_lock_bh(&priv->lock);
  597. path = priv->path; /* save reference to IUCV path */
  598. priv->path = NULL;
  599. priv->iucv_state = IUCV_DISCONN;
  600. spin_unlock_bh(&priv->lock);
  601. /* Sever IUCV path outside of priv->lock due to lock ordering of:
  602. * priv->lock <--> iucv_table_lock */
  603. if (path) {
  604. iucv_path_sever(path, NULL);
  605. iucv_path_free(path);
  606. }
  607. }
  608. /**
  609. * hvc_iucv_notifier_del() - HVC notifier for closing a TTY for the last time.
  610. * @hp: Pointer to the HVC device (struct hvc_struct)
  611. * @id: Additional data (originally passed to hvc_alloc):
  612. * the index of an struct hvc_iucv_private instance.
  613. *
  614. * This routine notifies the HVC back-end that the last tty device fd has been
  615. * closed. The function cleans up tty resources. The clean-up of the IUCV
  616. * connection is done in hvc_iucv_dtr_rts() and depends on the HUPCL termios
  617. * control setting.
  618. *
  619. * Locking: struct hvc_iucv_private->lock
  620. */
  621. static void hvc_iucv_notifier_del(struct hvc_struct *hp, int id)
  622. {
  623. struct hvc_iucv_private *priv;
  624. priv = hvc_iucv_get_private(id);
  625. if (!priv)
  626. return;
  627. flush_sndbuf_sync(priv);
  628. spin_lock_bh(&priv->lock);
  629. destroy_tty_buffer_list(&priv->tty_outqueue);
  630. destroy_tty_buffer_list(&priv->tty_inqueue);
  631. priv->tty_state = TTY_CLOSED;
  632. priv->sndbuf_len = 0;
  633. spin_unlock_bh(&priv->lock);
  634. }
  635. /**
  636. * hvc_iucv_filter_connreq() - Filter connection request based on z/VM user ID
  637. * @ipvmid: Originating z/VM user ID (right padded with blanks)
  638. *
  639. * Returns 0 if the z/VM user ID that is specified with @ipvmid is permitted to
  640. * connect, otherwise non-zero.
  641. */
  642. static int hvc_iucv_filter_connreq(u8 ipvmid[8])
  643. {
  644. const char *wildcard, *filter_entry;
  645. size_t i, len;
  646. /* Note: default policy is ACCEPT if no filter is set */
  647. if (!hvc_iucv_filter_size)
  648. return 0;
  649. for (i = 0; i < hvc_iucv_filter_size; i++) {
  650. filter_entry = hvc_iucv_filter + (8 * i);
  651. /* If a filter entry contains the filter wildcard character,
  652. * reduce the length to match the leading portion of the user
  653. * ID only (wildcard match). Characters following the wildcard
  654. * are ignored.
  655. */
  656. wildcard = strnchr(filter_entry, 8, FILTER_WILDCARD_CHAR);
  657. len = (wildcard) ? wildcard - filter_entry : 8;
  658. if (0 == memcmp(ipvmid, filter_entry, len))
  659. return 0;
  660. }
  661. return 1;
  662. }
  663. /**
  664. * hvc_iucv_path_pending() - IUCV handler to process a connection request.
  665. * @path: Pending path (struct iucv_path)
  666. * @ipvmid: z/VM system identifier of originator
  667. * @ipuser: User specified data for this path
  668. * (AF_IUCV: port/service name and originator port)
  669. *
  670. * The function uses the @ipuser data to determine if the pending path belongs
  671. * to a terminal managed by this device driver.
  672. * If the path belongs to this driver, ensure that the terminal is not accessed
  673. * multiple times (only one connection to a terminal is allowed).
  674. * If the terminal is not yet connected, the pending path is accepted and is
  675. * associated to the appropriate struct hvc_iucv_private instance.
  676. *
  677. * Returns 0 if @path belongs to a terminal managed by the this device driver;
  678. * otherwise returns -ENODEV in order to dispatch this path to other handlers.
  679. *
  680. * Locking: struct hvc_iucv_private->lock
  681. */
  682. static int hvc_iucv_path_pending(struct iucv_path *path,
  683. u8 ipvmid[8], u8 ipuser[16])
  684. {
  685. struct hvc_iucv_private *priv, *tmp;
  686. u8 wildcard[9] = "lnxhvc ";
  687. int i, rc, find_unused;
  688. u8 nuser_data[16];
  689. u8 vm_user_id[9];
  690. ASCEBC(wildcard, sizeof(wildcard));
  691. find_unused = !memcmp(wildcard, ipuser, 8);
  692. /* First, check if the pending path request is managed by this
  693. * IUCV handler:
  694. * - find a disconnected device if ipuser contains the wildcard
  695. * - find the device that matches the terminal ID in ipuser
  696. */
  697. priv = NULL;
  698. for (i = 0; i < hvc_iucv_devices; i++) {
  699. tmp = hvc_iucv_table[i];
  700. if (!tmp)
  701. continue;
  702. if (find_unused) {
  703. spin_lock(&tmp->lock);
  704. if (tmp->iucv_state == IUCV_DISCONN)
  705. priv = tmp;
  706. spin_unlock(&tmp->lock);
  707. } else if (!memcmp(tmp->srv_name, ipuser, 8))
  708. priv = tmp;
  709. if (priv)
  710. break;
  711. }
  712. if (!priv)
  713. return -ENODEV;
  714. /* Enforce that ipvmid is allowed to connect to us */
  715. read_lock(&hvc_iucv_filter_lock);
  716. rc = hvc_iucv_filter_connreq(ipvmid);
  717. read_unlock(&hvc_iucv_filter_lock);
  718. if (rc) {
  719. iucv_path_sever(path, ipuser);
  720. iucv_path_free(path);
  721. memcpy(vm_user_id, ipvmid, 8);
  722. vm_user_id[8] = 0;
  723. pr_info("A connection request from z/VM user ID %s "
  724. "was refused\n", vm_user_id);
  725. return 0;
  726. }
  727. spin_lock(&priv->lock);
  728. /* If the terminal is already connected or being severed, then sever
  729. * this path to enforce that there is only ONE established communication
  730. * path per terminal. */
  731. if (priv->iucv_state != IUCV_DISCONN) {
  732. iucv_path_sever(path, ipuser);
  733. iucv_path_free(path);
  734. goto out_path_handled;
  735. }
  736. /* accept path */
  737. memcpy(nuser_data, ipuser + 8, 8); /* remote service (for af_iucv) */
  738. memcpy(nuser_data + 8, ipuser, 8); /* local service (for af_iucv) */
  739. path->msglim = 0xffff; /* IUCV MSGLIMIT */
  740. path->flags &= ~IUCV_IPRMDATA; /* TODO: use IUCV_IPRMDATA */
  741. rc = iucv_path_accept(path, &hvc_iucv_handler, nuser_data, priv);
  742. if (rc) {
  743. iucv_path_sever(path, ipuser);
  744. iucv_path_free(path);
  745. goto out_path_handled;
  746. }
  747. priv->path = path;
  748. priv->iucv_state = IUCV_CONNECTED;
  749. /* store path information */
  750. memcpy(priv->info_path, ipvmid, 8);
  751. memcpy(priv->info_path + 8, ipuser + 8, 8);
  752. /* flush buffered output data... */
  753. schedule_delayed_work(&priv->sndbuf_work, 5);
  754. out_path_handled:
  755. spin_unlock(&priv->lock);
  756. return 0;
  757. }
  758. /**
  759. * hvc_iucv_path_severed() - IUCV handler to process a path sever.
  760. * @path: Pending path (struct iucv_path)
  761. * @ipuser: User specified data for this path
  762. * (AF_IUCV: port/service name and originator port)
  763. *
  764. * This function calls the hvc_iucv_hangup() function for the
  765. * respective IUCV HVC terminal.
  766. *
  767. * Locking: struct hvc_iucv_private->lock
  768. */
  769. static void hvc_iucv_path_severed(struct iucv_path *path, u8 ipuser[16])
  770. {
  771. struct hvc_iucv_private *priv = path->private;
  772. hvc_iucv_hangup(priv);
  773. }
  774. /**
  775. * hvc_iucv_msg_pending() - IUCV handler to process an incoming IUCV message.
  776. * @path: Pending path (struct iucv_path)
  777. * @msg: Pointer to the IUCV message
  778. *
  779. * The function puts an incoming message on the input queue for later
  780. * processing (by hvc_iucv_get_chars() / hvc_iucv_write()).
  781. * If the tty has not yet been opened, the message is rejected.
  782. *
  783. * Locking: struct hvc_iucv_private->lock
  784. */
  785. static void hvc_iucv_msg_pending(struct iucv_path *path,
  786. struct iucv_message *msg)
  787. {
  788. struct hvc_iucv_private *priv = path->private;
  789. struct iucv_tty_buffer *rb;
  790. /* reject messages that exceed max size of iucv_tty_msg->datalen */
  791. if (msg->length > MSG_SIZE(MSG_MAX_DATALEN)) {
  792. iucv_message_reject(path, msg);
  793. return;
  794. }
  795. spin_lock(&priv->lock);
  796. /* reject messages if tty has not yet been opened */
  797. if (priv->tty_state == TTY_CLOSED) {
  798. iucv_message_reject(path, msg);
  799. goto unlock_return;
  800. }
  801. /* allocate tty buffer to save iucv msg only */
  802. rb = alloc_tty_buffer(0, GFP_ATOMIC);
  803. if (!rb) {
  804. iucv_message_reject(path, msg);
  805. goto unlock_return; /* -ENOMEM */
  806. }
  807. rb->msg = *msg;
  808. list_add_tail(&rb->list, &priv->tty_inqueue);
  809. hvc_kick(); /* wake up hvc thread */
  810. unlock_return:
  811. spin_unlock(&priv->lock);
  812. }
  813. /**
  814. * hvc_iucv_msg_complete() - IUCV handler to process message completion
  815. * @path: Pending path (struct iucv_path)
  816. * @msg: Pointer to the IUCV message
  817. *
  818. * The function is called upon completion of message delivery to remove the
  819. * message from the outqueue. Additional delivery information can be found
  820. * msg->audit: rejected messages (0x040000 (IPADRJCT)), and
  821. * purged messages (0x010000 (IPADPGNR)).
  822. *
  823. * Locking: struct hvc_iucv_private->lock
  824. */
  825. static void hvc_iucv_msg_complete(struct iucv_path *path,
  826. struct iucv_message *msg)
  827. {
  828. struct hvc_iucv_private *priv = path->private;
  829. struct iucv_tty_buffer *ent, *next;
  830. LIST_HEAD(list_remove);
  831. spin_lock(&priv->lock);
  832. list_for_each_entry_safe(ent, next, &priv->tty_outqueue, list)
  833. if (ent->msg.id == msg->id) {
  834. list_move(&ent->list, &list_remove);
  835. break;
  836. }
  837. wake_up(&priv->sndbuf_waitq);
  838. spin_unlock(&priv->lock);
  839. destroy_tty_buffer_list(&list_remove);
  840. }
  841. /**
  842. * hvc_iucv_pm_freeze() - Freeze PM callback
  843. * @dev: IUVC HVC terminal device
  844. *
  845. * Sever an established IUCV communication path and
  846. * trigger a hang-up of the underlying HVC terminal.
  847. */
  848. static int hvc_iucv_pm_freeze(struct device *dev)
  849. {
  850. struct hvc_iucv_private *priv = dev_get_drvdata(dev);
  851. local_bh_disable();
  852. hvc_iucv_hangup(priv);
  853. local_bh_enable();
  854. return 0;
  855. }
  856. /**
  857. * hvc_iucv_pm_restore_thaw() - Thaw and restore PM callback
  858. * @dev: IUVC HVC terminal device
  859. *
  860. * Wake up the HVC thread to trigger hang-up and respective
  861. * HVC back-end notifier invocations.
  862. */
  863. static int hvc_iucv_pm_restore_thaw(struct device *dev)
  864. {
  865. hvc_kick();
  866. return 0;
  867. }
  868. static ssize_t hvc_iucv_dev_termid_show(struct device *dev,
  869. struct device_attribute *attr,
  870. char *buf)
  871. {
  872. struct hvc_iucv_private *priv = dev_get_drvdata(dev);
  873. size_t len;
  874. len = sizeof(priv->srv_name);
  875. memcpy(buf, priv->srv_name, len);
  876. EBCASC(buf, len);
  877. buf[len++] = '\n';
  878. return len;
  879. }
  880. static ssize_t hvc_iucv_dev_state_show(struct device *dev,
  881. struct device_attribute *attr,
  882. char *buf)
  883. {
  884. struct hvc_iucv_private *priv = dev_get_drvdata(dev);
  885. return sprintf(buf, "%u:%u\n", priv->iucv_state, priv->tty_state);
  886. }
  887. static ssize_t hvc_iucv_dev_peer_show(struct device *dev,
  888. struct device_attribute *attr,
  889. char *buf)
  890. {
  891. struct hvc_iucv_private *priv = dev_get_drvdata(dev);
  892. char vmid[9], ipuser[9];
  893. memset(vmid, 0, sizeof(vmid));
  894. memset(ipuser, 0, sizeof(ipuser));
  895. spin_lock_bh(&priv->lock);
  896. if (priv->iucv_state == IUCV_CONNECTED) {
  897. memcpy(vmid, priv->info_path, 8);
  898. memcpy(ipuser, priv->info_path + 8, 8);
  899. }
  900. spin_unlock_bh(&priv->lock);
  901. EBCASC(ipuser, 8);
  902. return sprintf(buf, "%s:%s\n", vmid, ipuser);
  903. }
  904. /* HVC operations */
  905. static const struct hv_ops hvc_iucv_ops = {
  906. .get_chars = hvc_iucv_get_chars,
  907. .put_chars = hvc_iucv_put_chars,
  908. .notifier_add = hvc_iucv_notifier_add,
  909. .notifier_del = hvc_iucv_notifier_del,
  910. .notifier_hangup = hvc_iucv_notifier_hangup,
  911. .dtr_rts = hvc_iucv_dtr_rts,
  912. };
  913. /* Suspend / resume device operations */
  914. static const struct dev_pm_ops hvc_iucv_pm_ops = {
  915. .freeze = hvc_iucv_pm_freeze,
  916. .thaw = hvc_iucv_pm_restore_thaw,
  917. .restore = hvc_iucv_pm_restore_thaw,
  918. };
  919. /* IUCV HVC device driver */
  920. static struct device_driver hvc_iucv_driver = {
  921. .name = KMSG_COMPONENT,
  922. .bus = &iucv_bus,
  923. .pm = &hvc_iucv_pm_ops,
  924. };
  925. /* IUCV HVC device attributes */
  926. static DEVICE_ATTR(termid, 0640, hvc_iucv_dev_termid_show, NULL);
  927. static DEVICE_ATTR(state, 0640, hvc_iucv_dev_state_show, NULL);
  928. static DEVICE_ATTR(peer, 0640, hvc_iucv_dev_peer_show, NULL);
  929. static struct attribute *hvc_iucv_dev_attrs[] = {
  930. &dev_attr_termid.attr,
  931. &dev_attr_state.attr,
  932. &dev_attr_peer.attr,
  933. NULL,
  934. };
  935. static struct attribute_group hvc_iucv_dev_attr_group = {
  936. .attrs = hvc_iucv_dev_attrs,
  937. };
  938. static const struct attribute_group *hvc_iucv_dev_attr_groups[] = {
  939. &hvc_iucv_dev_attr_group,
  940. NULL,
  941. };
  942. /**
  943. * hvc_iucv_alloc() - Allocates a new struct hvc_iucv_private instance
  944. * @id: hvc_iucv_table index
  945. * @is_console: Flag if the instance is used as Linux console
  946. *
  947. * This function allocates a new hvc_iucv_private structure and stores
  948. * the instance in hvc_iucv_table at index @id.
  949. * Returns 0 on success; otherwise non-zero.
  950. */
  951. static int __init hvc_iucv_alloc(int id, unsigned int is_console)
  952. {
  953. struct hvc_iucv_private *priv;
  954. char name[9];
  955. int rc;
  956. priv = kzalloc(sizeof(struct hvc_iucv_private), GFP_KERNEL);
  957. if (!priv)
  958. return -ENOMEM;
  959. spin_lock_init(&priv->lock);
  960. INIT_LIST_HEAD(&priv->tty_outqueue);
  961. INIT_LIST_HEAD(&priv->tty_inqueue);
  962. INIT_DELAYED_WORK(&priv->sndbuf_work, hvc_iucv_sndbuf_work);
  963. init_waitqueue_head(&priv->sndbuf_waitq);
  964. priv->sndbuf = (void *) get_zeroed_page(GFP_KERNEL);
  965. if (!priv->sndbuf) {
  966. kfree(priv);
  967. return -ENOMEM;
  968. }
  969. /* set console flag */
  970. priv->is_console = is_console;
  971. /* allocate hvc device */
  972. priv->hvc = hvc_alloc(HVC_IUCV_MAGIC + id, /* PAGE_SIZE */
  973. HVC_IUCV_MAGIC + id, &hvc_iucv_ops, 256);
  974. if (IS_ERR(priv->hvc)) {
  975. rc = PTR_ERR(priv->hvc);
  976. goto out_error_hvc;
  977. }
  978. /* notify HVC thread instead of using polling */
  979. priv->hvc->irq_requested = 1;
  980. /* setup iucv related information */
  981. snprintf(name, 9, "lnxhvc%-2d", id);
  982. memcpy(priv->srv_name, name, 8);
  983. ASCEBC(priv->srv_name, 8);
  984. /* create and setup device */
  985. priv->dev = kzalloc(sizeof(*priv->dev), GFP_KERNEL);
  986. if (!priv->dev) {
  987. rc = -ENOMEM;
  988. goto out_error_dev;
  989. }
  990. dev_set_name(priv->dev, "hvc_iucv%d", id);
  991. dev_set_drvdata(priv->dev, priv);
  992. priv->dev->bus = &iucv_bus;
  993. priv->dev->parent = iucv_root;
  994. priv->dev->driver = &hvc_iucv_driver;
  995. priv->dev->groups = hvc_iucv_dev_attr_groups;
  996. priv->dev->release = (void (*)(struct device *)) kfree;
  997. rc = device_register(priv->dev);
  998. if (rc) {
  999. put_device(priv->dev);
  1000. goto out_error_dev;
  1001. }
  1002. hvc_iucv_table[id] = priv;
  1003. return 0;
  1004. out_error_dev:
  1005. hvc_remove(priv->hvc);
  1006. out_error_hvc:
  1007. free_page((unsigned long) priv->sndbuf);
  1008. kfree(priv);
  1009. return rc;
  1010. }
  1011. /**
  1012. * hvc_iucv_destroy() - Destroy and free hvc_iucv_private instances
  1013. */
  1014. static void __init hvc_iucv_destroy(struct hvc_iucv_private *priv)
  1015. {
  1016. hvc_remove(priv->hvc);
  1017. device_unregister(priv->dev);
  1018. free_page((unsigned long) priv->sndbuf);
  1019. kfree(priv);
  1020. }
  1021. /**
  1022. * hvc_iucv_parse_filter() - Parse filter for a single z/VM user ID
  1023. * @filter: String containing a comma-separated list of z/VM user IDs
  1024. * @dest: Location where to store the parsed z/VM user ID
  1025. */
  1026. static const char *hvc_iucv_parse_filter(const char *filter, char *dest)
  1027. {
  1028. const char *nextdelim, *residual;
  1029. size_t len;
  1030. nextdelim = strchr(filter, ',');
  1031. if (nextdelim) {
  1032. len = nextdelim - filter;
  1033. residual = nextdelim + 1;
  1034. } else {
  1035. len = strlen(filter);
  1036. residual = filter + len;
  1037. }
  1038. if (len == 0)
  1039. return ERR_PTR(-EINVAL);
  1040. /* check for '\n' (if called from sysfs) */
  1041. if (filter[len - 1] == '\n')
  1042. len--;
  1043. /* prohibit filter entries containing the wildcard character only */
  1044. if (len == 1 && *filter == FILTER_WILDCARD_CHAR)
  1045. return ERR_PTR(-EINVAL);
  1046. if (len > 8)
  1047. return ERR_PTR(-EINVAL);
  1048. /* pad with blanks and save upper case version of user ID */
  1049. memset(dest, ' ', 8);
  1050. while (len--)
  1051. dest[len] = toupper(filter[len]);
  1052. return residual;
  1053. }
  1054. /**
  1055. * hvc_iucv_setup_filter() - Set up z/VM user ID filter
  1056. * @filter: String consisting of a comma-separated list of z/VM user IDs
  1057. *
  1058. * The function parses the @filter string and creates an array containing
  1059. * the list of z/VM user ID filter entries.
  1060. * Return code 0 means success, -EINVAL if the filter is syntactically
  1061. * incorrect, -ENOMEM if there was not enough memory to allocate the
  1062. * filter list array, or -ENOSPC if too many z/VM user IDs have been specified.
  1063. */
  1064. static int hvc_iucv_setup_filter(const char *val)
  1065. {
  1066. const char *residual;
  1067. int err;
  1068. size_t size, count;
  1069. void *array, *old_filter;
  1070. count = strlen(val);
  1071. if (count == 0 || (count == 1 && val[0] == '\n')) {
  1072. size = 0;
  1073. array = NULL;
  1074. goto out_replace_filter; /* clear filter */
  1075. }
  1076. /* count user IDs in order to allocate sufficient memory */
  1077. size = 1;
  1078. residual = val;
  1079. while ((residual = strchr(residual, ',')) != NULL) {
  1080. residual++;
  1081. size++;
  1082. }
  1083. /* check if the specified list exceeds the filter limit */
  1084. if (size > MAX_VMID_FILTER)
  1085. return -ENOSPC;
  1086. array = kzalloc(size * 8, GFP_KERNEL);
  1087. if (!array)
  1088. return -ENOMEM;
  1089. count = size;
  1090. residual = val;
  1091. while (*residual && count) {
  1092. residual = hvc_iucv_parse_filter(residual,
  1093. array + ((size - count) * 8));
  1094. if (IS_ERR(residual)) {
  1095. err = PTR_ERR(residual);
  1096. kfree(array);
  1097. goto out_err;
  1098. }
  1099. count--;
  1100. }
  1101. out_replace_filter:
  1102. write_lock_bh(&hvc_iucv_filter_lock);
  1103. old_filter = hvc_iucv_filter;
  1104. hvc_iucv_filter_size = size;
  1105. hvc_iucv_filter = array;
  1106. write_unlock_bh(&hvc_iucv_filter_lock);
  1107. kfree(old_filter);
  1108. err = 0;
  1109. out_err:
  1110. return err;
  1111. }
  1112. /**
  1113. * param_set_vmidfilter() - Set z/VM user ID filter parameter
  1114. * @val: String consisting of a comma-separated list of z/VM user IDs
  1115. * @kp: Kernel parameter pointing to hvc_iucv_filter array
  1116. *
  1117. * The function sets up the z/VM user ID filter specified as comma-separated
  1118. * list of user IDs in @val.
  1119. * Note: If it is called early in the boot process, @val is stored and
  1120. * parsed later in hvc_iucv_init().
  1121. */
  1122. static int param_set_vmidfilter(const char *val, const struct kernel_param *kp)
  1123. {
  1124. int rc;
  1125. if (!MACHINE_IS_VM || !hvc_iucv_devices)
  1126. return -ENODEV;
  1127. if (!val)
  1128. return -EINVAL;
  1129. rc = 0;
  1130. if (slab_is_available())
  1131. rc = hvc_iucv_setup_filter(val);
  1132. else
  1133. hvc_iucv_filter_string = val; /* defer... */
  1134. return rc;
  1135. }
  1136. /**
  1137. * param_get_vmidfilter() - Get z/VM user ID filter
  1138. * @buffer: Buffer to store z/VM user ID filter,
  1139. * (buffer size assumption PAGE_SIZE)
  1140. * @kp: Kernel parameter pointing to the hvc_iucv_filter array
  1141. *
  1142. * The function stores the filter as a comma-separated list of z/VM user IDs
  1143. * in @buffer. Typically, sysfs routines call this function for attr show.
  1144. */
  1145. static int param_get_vmidfilter(char *buffer, const struct kernel_param *kp)
  1146. {
  1147. int rc;
  1148. size_t index, len;
  1149. void *start, *end;
  1150. if (!MACHINE_IS_VM || !hvc_iucv_devices)
  1151. return -ENODEV;
  1152. rc = 0;
  1153. read_lock_bh(&hvc_iucv_filter_lock);
  1154. for (index = 0; index < hvc_iucv_filter_size; index++) {
  1155. start = hvc_iucv_filter + (8 * index);
  1156. end = memchr(start, ' ', 8);
  1157. len = (end) ? end - start : 8;
  1158. memcpy(buffer + rc, start, len);
  1159. rc += len;
  1160. buffer[rc++] = ',';
  1161. }
  1162. read_unlock_bh(&hvc_iucv_filter_lock);
  1163. if (rc)
  1164. buffer[--rc] = '\0'; /* replace last comma and update rc */
  1165. return rc;
  1166. }
  1167. #define param_check_vmidfilter(name, p) __param_check(name, p, void)
  1168. static const struct kernel_param_ops param_ops_vmidfilter = {
  1169. .set = param_set_vmidfilter,
  1170. .get = param_get_vmidfilter,
  1171. };
  1172. /**
  1173. * hvc_iucv_init() - z/VM IUCV HVC device driver initialization
  1174. */
  1175. static int __init hvc_iucv_init(void)
  1176. {
  1177. int rc;
  1178. unsigned int i;
  1179. if (!hvc_iucv_devices)
  1180. return -ENODEV;
  1181. if (!MACHINE_IS_VM) {
  1182. pr_notice("The z/VM IUCV HVC device driver cannot "
  1183. "be used without z/VM\n");
  1184. rc = -ENODEV;
  1185. goto out_error;
  1186. }
  1187. if (hvc_iucv_devices > MAX_HVC_IUCV_LINES) {
  1188. pr_err("%lu is not a valid value for the hvc_iucv= "
  1189. "kernel parameter\n", hvc_iucv_devices);
  1190. rc = -EINVAL;
  1191. goto out_error;
  1192. }
  1193. /* register IUCV HVC device driver */
  1194. rc = driver_register(&hvc_iucv_driver);
  1195. if (rc)
  1196. goto out_error;
  1197. /* parse hvc_iucv_allow string and create z/VM user ID filter list */
  1198. if (hvc_iucv_filter_string) {
  1199. rc = hvc_iucv_setup_filter(hvc_iucv_filter_string);
  1200. switch (rc) {
  1201. case 0:
  1202. break;
  1203. case -ENOMEM:
  1204. pr_err("Allocating memory failed with "
  1205. "reason code=%d\n", 3);
  1206. goto out_error;
  1207. case -EINVAL:
  1208. pr_err("hvc_iucv_allow= does not specify a valid "
  1209. "z/VM user ID list\n");
  1210. goto out_error;
  1211. case -ENOSPC:
  1212. pr_err("hvc_iucv_allow= specifies too many "
  1213. "z/VM user IDs\n");
  1214. goto out_error;
  1215. default:
  1216. goto out_error;
  1217. }
  1218. }
  1219. hvc_iucv_buffer_cache = kmem_cache_create(KMSG_COMPONENT,
  1220. sizeof(struct iucv_tty_buffer),
  1221. 0, 0, NULL);
  1222. if (!hvc_iucv_buffer_cache) {
  1223. pr_err("Allocating memory failed with reason code=%d\n", 1);
  1224. rc = -ENOMEM;
  1225. goto out_error;
  1226. }
  1227. hvc_iucv_mempool = mempool_create_slab_pool(MEMPOOL_MIN_NR,
  1228. hvc_iucv_buffer_cache);
  1229. if (!hvc_iucv_mempool) {
  1230. pr_err("Allocating memory failed with reason code=%d\n", 2);
  1231. kmem_cache_destroy(hvc_iucv_buffer_cache);
  1232. rc = -ENOMEM;
  1233. goto out_error;
  1234. }
  1235. /* register the first terminal device as console
  1236. * (must be done before allocating hvc terminal devices) */
  1237. rc = hvc_instantiate(HVC_IUCV_MAGIC, IUCV_HVC_CON_IDX, &hvc_iucv_ops);
  1238. if (rc) {
  1239. pr_err("Registering HVC terminal device as "
  1240. "Linux console failed\n");
  1241. goto out_error_memory;
  1242. }
  1243. /* allocate hvc_iucv_private structs */
  1244. for (i = 0; i < hvc_iucv_devices; i++) {
  1245. rc = hvc_iucv_alloc(i, (i == IUCV_HVC_CON_IDX) ? 1 : 0);
  1246. if (rc) {
  1247. pr_err("Creating a new HVC terminal device "
  1248. "failed with error code=%d\n", rc);
  1249. goto out_error_hvc;
  1250. }
  1251. }
  1252. /* register IUCV callback handler */
  1253. rc = iucv_register(&hvc_iucv_handler, 0);
  1254. if (rc) {
  1255. pr_err("Registering IUCV handlers failed with error code=%d\n",
  1256. rc);
  1257. goto out_error_hvc;
  1258. }
  1259. return 0;
  1260. out_error_hvc:
  1261. for (i = 0; i < hvc_iucv_devices; i++)
  1262. if (hvc_iucv_table[i])
  1263. hvc_iucv_destroy(hvc_iucv_table[i]);
  1264. out_error_memory:
  1265. mempool_destroy(hvc_iucv_mempool);
  1266. kmem_cache_destroy(hvc_iucv_buffer_cache);
  1267. out_error:
  1268. kfree(hvc_iucv_filter);
  1269. hvc_iucv_devices = 0; /* ensure that we do not provide any device */
  1270. return rc;
  1271. }
  1272. /**
  1273. * hvc_iucv_config() - Parsing of hvc_iucv= kernel command line parameter
  1274. * @val: Parameter value (numeric)
  1275. */
  1276. static int __init hvc_iucv_config(char *val)
  1277. {
  1278. return kstrtoul(val, 10, &hvc_iucv_devices);
  1279. }
  1280. device_initcall(hvc_iucv_init);
  1281. __setup("hvc_iucv=", hvc_iucv_config);
  1282. core_param(hvc_iucv_allow, hvc_iucv_filter, vmidfilter, 0640);