hvc_iucv.c 41 KB

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