hvc_iucv.c 37 KB

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