hvc_iucv.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  1. /*
  2. * hvc_iucv.c - z/VM IUCV back-end for the Hypervisor Console (HVC)
  3. *
  4. * This back-end for HVC provides terminal access via
  5. * z/VM IUCV communication paths.
  6. *
  7. * Copyright IBM Corp. 2008.
  8. *
  9. * Author(s): Hendrik Brueckner <brueckner@linux.vnet.ibm.com>
  10. */
  11. #define KMSG_COMPONENT "hvc_iucv"
  12. #include <linux/types.h>
  13. #include <asm/ebcdic.h>
  14. #include <linux/mempool.h>
  15. #include <linux/module.h>
  16. #include <linux/tty.h>
  17. #include <net/iucv/iucv.h>
  18. #include "hvc_console.h"
  19. /* HVC backend for z/VM IUCV */
  20. #define HVC_IUCV_MAGIC 0xc9e4c3e5
  21. #define MAX_HVC_IUCV_LINES HVC_ALLOC_TTY_ADAPTERS
  22. #define MEMPOOL_MIN_NR (PAGE_SIZE / sizeof(struct iucv_tty_buffer)/4)
  23. /* IUCV TTY message */
  24. #define MSG_VERSION 0x02 /* Message version */
  25. #define MSG_TYPE_ERROR 0x01 /* Error message */
  26. #define MSG_TYPE_TERMENV 0x02 /* Terminal environment variable */
  27. #define MSG_TYPE_TERMIOS 0x04 /* Terminal IO struct update */
  28. #define MSG_TYPE_WINSIZE 0x08 /* Terminal window size update */
  29. #define MSG_TYPE_DATA 0x10 /* Terminal data */
  30. #define MSG_SIZE(s) ((s) + offsetof(struct iucv_tty_msg, data))
  31. struct iucv_tty_msg {
  32. u8 version; /* Message version */
  33. u8 type; /* Message type */
  34. #define MSG_MAX_DATALEN (~(u16)0)
  35. u16 datalen; /* Payload length */
  36. u8 data[]; /* Payload buffer */
  37. } __attribute__((packed));
  38. enum iucv_state_t {
  39. IUCV_DISCONN = 0,
  40. IUCV_CONNECTED = 1,
  41. IUCV_SEVERED = 2,
  42. };
  43. enum tty_state_t {
  44. TTY_CLOSED = 0,
  45. TTY_OPENED = 1,
  46. };
  47. struct hvc_iucv_private {
  48. struct hvc_struct *hvc; /* HVC console struct reference */
  49. u8 srv_name[8]; /* IUCV service name (ebcdic) */
  50. enum iucv_state_t iucv_state; /* IUCV connection status */
  51. enum tty_state_t tty_state; /* TTY status */
  52. struct iucv_path *path; /* IUCV path pointer */
  53. spinlock_t lock; /* hvc_iucv_private lock */
  54. struct list_head tty_outqueue; /* outgoing IUCV messages */
  55. struct list_head tty_inqueue; /* incoming IUCV messages */
  56. };
  57. struct iucv_tty_buffer {
  58. struct list_head list; /* list pointer */
  59. struct iucv_message msg; /* store an incoming IUCV message */
  60. size_t offset; /* data buffer offset */
  61. struct iucv_tty_msg *mbuf; /* buffer to store input/output data */
  62. };
  63. /* IUCV callback handler */
  64. static int hvc_iucv_path_pending(struct iucv_path *, u8[8], u8[16]);
  65. static void hvc_iucv_path_severed(struct iucv_path *, u8[16]);
  66. static void hvc_iucv_msg_pending(struct iucv_path *, struct iucv_message *);
  67. static void hvc_iucv_msg_complete(struct iucv_path *, struct iucv_message *);
  68. /* Kernel module parameters */
  69. static unsigned long hvc_iucv_devices;
  70. /* Array of allocated hvc iucv tty lines... */
  71. static struct hvc_iucv_private *hvc_iucv_table[MAX_HVC_IUCV_LINES];
  72. /* Kmem cache and mempool for iucv_tty_buffer elements */
  73. static struct kmem_cache *hvc_iucv_buffer_cache;
  74. static mempool_t *hvc_iucv_mempool;
  75. /* IUCV handler callback functions */
  76. static struct iucv_handler hvc_iucv_handler = {
  77. .path_pending = hvc_iucv_path_pending,
  78. .path_severed = hvc_iucv_path_severed,
  79. .message_complete = hvc_iucv_msg_complete,
  80. .message_pending = hvc_iucv_msg_pending,
  81. };
  82. /**
  83. * hvc_iucv_get_private() - Return a struct hvc_iucv_private instance.
  84. * @num: The HVC virtual terminal number (vtermno)
  85. *
  86. * This function returns the struct hvc_iucv_private instance that corresponds
  87. * to the HVC virtual terminal number specified as parameter @num.
  88. */
  89. struct hvc_iucv_private *hvc_iucv_get_private(uint32_t num)
  90. {
  91. if ((num < HVC_IUCV_MAGIC) || (num - HVC_IUCV_MAGIC > hvc_iucv_devices))
  92. return NULL;
  93. return hvc_iucv_table[num - HVC_IUCV_MAGIC];
  94. }
  95. /**
  96. * alloc_tty_buffer() - Returns a new struct iucv_tty_buffer element.
  97. * @size: Size of the internal buffer used to store data.
  98. * @flags: Memory allocation flags passed to mempool.
  99. *
  100. * This function allocates a new struct iucv_tty_buffer element and, optionally,
  101. * allocates an internal data buffer with the specified size @size.
  102. * Note: The total message size arises from the internal buffer size and the
  103. * members of the iucv_tty_msg structure.
  104. *
  105. * The function returns NULL if memory allocation has failed.
  106. */
  107. static struct iucv_tty_buffer *alloc_tty_buffer(size_t size, gfp_t flags)
  108. {
  109. struct iucv_tty_buffer *bufp;
  110. bufp = mempool_alloc(hvc_iucv_mempool, flags);
  111. if (!bufp)
  112. return NULL;
  113. memset(bufp, 0, sizeof(struct iucv_tty_buffer));
  114. if (size > 0) {
  115. bufp->msg.length = MSG_SIZE(size);
  116. bufp->mbuf = kmalloc(bufp->msg.length, flags);
  117. if (!bufp->mbuf) {
  118. mempool_free(bufp, hvc_iucv_mempool);
  119. return NULL;
  120. }
  121. bufp->mbuf->version = MSG_VERSION;
  122. bufp->mbuf->type = MSG_TYPE_DATA;
  123. bufp->mbuf->datalen = (u16) size;
  124. }
  125. return bufp;
  126. }
  127. /**
  128. * destroy_tty_buffer() - destroy struct iucv_tty_buffer element.
  129. * @bufp: Pointer to a struct iucv_tty_buffer element, SHALL NOT be NULL.
  130. *
  131. * The destroy_tty_buffer() function frees the internal data buffer and returns
  132. * the struct iucv_tty_buffer element back to the mempool for freeing.
  133. */
  134. static void destroy_tty_buffer(struct iucv_tty_buffer *bufp)
  135. {
  136. kfree(bufp->mbuf);
  137. mempool_free(bufp, hvc_iucv_mempool);
  138. }
  139. /**
  140. * destroy_tty_buffer_list() - call destroy_tty_buffer() for each list element.
  141. * @list: List head pointer to a list containing struct iucv_tty_buffer
  142. * elements.
  143. *
  144. * Calls destroy_tty_buffer() for each struct iucv_tty_buffer element in the
  145. * list @list.
  146. */
  147. static void destroy_tty_buffer_list(struct list_head *list)
  148. {
  149. struct iucv_tty_buffer *ent, *next;
  150. list_for_each_entry_safe(ent, next, list, list) {
  151. list_del(&ent->list);
  152. destroy_tty_buffer(ent);
  153. }
  154. }
  155. /**
  156. * hvc_iucv_write() - Receive IUCV message write data to HVC console buffer.
  157. * @priv: Pointer to hvc_iucv_private structure.
  158. * @buf: HVC console buffer for writing received terminal data.
  159. * @count: HVC console buffer size.
  160. * @has_more_data: Pointer to an int variable.
  161. *
  162. * The function picks up pending messages from the input queue and receives
  163. * the message data that is then written to the specified buffer @buf.
  164. * If the buffer size @count is less than the data message size, then the
  165. * message is kept on the input queue and @has_more_data is set to 1.
  166. * If the message data has been entirely written, the message is removed from
  167. * the input queue.
  168. *
  169. * The function returns the number of bytes written to the terminal, zero if
  170. * there are no pending data messages available or if there is no established
  171. * IUCV path.
  172. * If the IUCV path has been severed, then -EPIPE is returned to cause a
  173. * hang up (that is issued by the HVC console layer).
  174. */
  175. static int hvc_iucv_write(struct hvc_iucv_private *priv,
  176. char *buf, int count, int *has_more_data)
  177. {
  178. struct iucv_tty_buffer *rb;
  179. int written;
  180. int rc;
  181. /* Immediately return if there is no IUCV connection */
  182. if (priv->iucv_state == IUCV_DISCONN)
  183. return 0;
  184. /* If the IUCV path has been severed, return -EPIPE to inform the
  185. * hvc console layer to hang up the tty device. */
  186. if (priv->iucv_state == IUCV_SEVERED)
  187. return -EPIPE;
  188. /* check if there are pending messages */
  189. if (list_empty(&priv->tty_inqueue))
  190. return 0;
  191. /* receive a iucv message and flip data to the tty (ldisc) */
  192. rb = list_first_entry(&priv->tty_inqueue, struct iucv_tty_buffer, list);
  193. written = 0;
  194. if (!rb->mbuf) { /* message not yet received ... */
  195. /* allocate mem to store msg data; if no memory is available
  196. * then leave the buffer on the list and re-try later */
  197. rb->mbuf = kmalloc(rb->msg.length, GFP_ATOMIC);
  198. if (!rb->mbuf)
  199. return -ENOMEM;
  200. rc = __iucv_message_receive(priv->path, &rb->msg, 0,
  201. rb->mbuf, rb->msg.length, NULL);
  202. switch (rc) {
  203. case 0: /* Successful */
  204. break;
  205. case 2: /* No message found */
  206. case 9: /* Message purged */
  207. break;
  208. default:
  209. written = -EIO;
  210. }
  211. /* remove buffer if an error has occured or received data
  212. * is not correct */
  213. if (rc || (rb->mbuf->version != MSG_VERSION) ||
  214. (rb->msg.length != MSG_SIZE(rb->mbuf->datalen)))
  215. goto out_remove_buffer;
  216. }
  217. switch (rb->mbuf->type) {
  218. case MSG_TYPE_DATA:
  219. written = min_t(int, rb->mbuf->datalen - rb->offset, count);
  220. memcpy(buf, rb->mbuf->data + rb->offset, written);
  221. if (written < (rb->mbuf->datalen - rb->offset)) {
  222. rb->offset += written;
  223. *has_more_data = 1;
  224. goto out_written;
  225. }
  226. break;
  227. case MSG_TYPE_WINSIZE:
  228. if (rb->mbuf->datalen != sizeof(struct winsize))
  229. break;
  230. hvc_resize(priv->hvc, *((struct winsize *)rb->mbuf->data));
  231. break;
  232. case MSG_TYPE_ERROR: /* ignored ... */
  233. case MSG_TYPE_TERMENV: /* ignored ... */
  234. case MSG_TYPE_TERMIOS: /* ignored ... */
  235. break;
  236. }
  237. out_remove_buffer:
  238. list_del(&rb->list);
  239. destroy_tty_buffer(rb);
  240. *has_more_data = !list_empty(&priv->tty_inqueue);
  241. out_written:
  242. return written;
  243. }
  244. /**
  245. * hvc_iucv_get_chars() - HVC get_chars operation.
  246. * @vtermno: HVC virtual terminal number.
  247. * @buf: Pointer to a buffer to store data
  248. * @count: Size of buffer available for writing
  249. *
  250. * The hvc_console thread calls this method to read characters from
  251. * the terminal backend. If an IUCV communication path has been established,
  252. * pending IUCV messages are received and data is copied into buffer @buf
  253. * up to @count bytes.
  254. *
  255. * Locking: The routine gets called under an irqsave() spinlock; and
  256. * the routine locks the struct hvc_iucv_private->lock to call
  257. * helper functions.
  258. */
  259. static int hvc_iucv_get_chars(uint32_t vtermno, char *buf, int count)
  260. {
  261. struct hvc_iucv_private *priv = hvc_iucv_get_private(vtermno);
  262. int written;
  263. int has_more_data;
  264. if (count <= 0)
  265. return 0;
  266. if (!priv)
  267. return -ENODEV;
  268. spin_lock(&priv->lock);
  269. has_more_data = 0;
  270. written = hvc_iucv_write(priv, buf, count, &has_more_data);
  271. spin_unlock(&priv->lock);
  272. /* if there are still messages on the queue... schedule another run */
  273. if (has_more_data)
  274. hvc_kick();
  275. return written;
  276. }
  277. /**
  278. * hvc_iucv_send() - Send an IUCV message containing terminal data.
  279. * @priv: Pointer to struct hvc_iucv_private instance.
  280. * @buf: Buffer containing data to send.
  281. * @size: Size of buffer and amount of data to send.
  282. *
  283. * If an IUCV communication path is established, the function copies the buffer
  284. * data to a newly allocated struct iucv_tty_buffer element, sends the data and
  285. * puts the element to the outqueue.
  286. *
  287. * If there is no IUCV communication path established, the function returns 0.
  288. * If an existing IUCV communicaton path has been severed, the function returns
  289. * -EPIPE (can be passed to HVC layer to cause a tty hangup).
  290. */
  291. static int hvc_iucv_send(struct hvc_iucv_private *priv, const char *buf,
  292. int count)
  293. {
  294. struct iucv_tty_buffer *sb;
  295. int rc;
  296. u16 len;
  297. if (priv->iucv_state == IUCV_SEVERED)
  298. return -EPIPE;
  299. if (priv->iucv_state == IUCV_DISCONN)
  300. return 0;
  301. len = min_t(u16, MSG_MAX_DATALEN, count);
  302. /* allocate internal buffer to store msg data and also compute total
  303. * message length */
  304. sb = alloc_tty_buffer(len, GFP_ATOMIC);
  305. if (!sb)
  306. return -ENOMEM;
  307. sb->mbuf->datalen = len;
  308. memcpy(sb->mbuf->data, buf, len);
  309. list_add_tail(&sb->list, &priv->tty_outqueue);
  310. rc = __iucv_message_send(priv->path, &sb->msg, 0, 0,
  311. (void *) sb->mbuf, sb->msg.length);
  312. if (rc) {
  313. list_del(&sb->list);
  314. destroy_tty_buffer(sb);
  315. len = 0;
  316. }
  317. return len;
  318. }
  319. /**
  320. * hvc_iucv_put_chars() - HVC put_chars operation.
  321. * @vtermno: HVC virtual terminal number.
  322. * @buf: Pointer to an buffer to read data from
  323. * @count: Size of buffer available for reading
  324. *
  325. * The hvc_console thread calls this method to write characters from
  326. * to the terminal backend.
  327. * The function calls hvc_iucv_send() under the lock of the
  328. * struct hvc_iucv_private instance that corresponds to the tty @vtermno.
  329. *
  330. * Locking: The method gets called under an irqsave() spinlock; and
  331. * locks struct hvc_iucv_private->lock.
  332. */
  333. static int hvc_iucv_put_chars(uint32_t vtermno, const char *buf, int count)
  334. {
  335. struct hvc_iucv_private *priv = hvc_iucv_get_private(vtermno);
  336. int sent;
  337. if (count <= 0)
  338. return 0;
  339. if (!priv)
  340. return -ENODEV;
  341. spin_lock(&priv->lock);
  342. sent = hvc_iucv_send(priv, buf, count);
  343. spin_unlock(&priv->lock);
  344. return sent;
  345. }
  346. /**
  347. * hvc_iucv_notifier_add() - HVC notifier for opening a TTY for the first time.
  348. * @hp: Pointer to the HVC device (struct hvc_struct)
  349. * @id: Additional data (originally passed to hvc_alloc): the index of an struct
  350. * hvc_iucv_private instance.
  351. *
  352. * The function sets the tty state to TTY_OPEN for the struct hvc_iucv_private
  353. * instance that is derived from @id. Always returns 0.
  354. *
  355. * Locking: struct hvc_iucv_private->lock, spin_lock_bh
  356. */
  357. static int hvc_iucv_notifier_add(struct hvc_struct *hp, int id)
  358. {
  359. struct hvc_iucv_private *priv;
  360. priv = hvc_iucv_get_private(id);
  361. if (!priv)
  362. return 0;
  363. spin_lock_bh(&priv->lock);
  364. priv->tty_state = TTY_OPENED;
  365. spin_unlock_bh(&priv->lock);
  366. return 0;
  367. }
  368. /**
  369. * hvc_iucv_cleanup() - Clean up function if the tty portion is finally closed.
  370. * @priv: Pointer to the struct hvc_iucv_private instance.
  371. *
  372. * The functions severs the established IUCV communication path (if any), and
  373. * destroy struct iucv_tty_buffer elements from the in- and outqueue. Finally,
  374. * the functions resets the states to TTY_CLOSED and IUCV_DISCONN.
  375. */
  376. static void hvc_iucv_cleanup(struct hvc_iucv_private *priv)
  377. {
  378. destroy_tty_buffer_list(&priv->tty_outqueue);
  379. destroy_tty_buffer_list(&priv->tty_inqueue);
  380. priv->tty_state = TTY_CLOSED;
  381. priv->iucv_state = IUCV_DISCONN;
  382. }
  383. /**
  384. * hvc_iucv_notifier_hangup() - HVC notifier for tty hangups.
  385. * @hp: Pointer to the HVC device (struct hvc_struct)
  386. * @id: Additional data (originally passed to hvc_alloc): the index of an struct
  387. * hvc_iucv_private instance.
  388. *
  389. * This routine notifies the HVC backend that a tty hangup (carrier loss,
  390. * virtual or otherwise) has occured.
  391. *
  392. * The HVC backend for z/VM IUCV ignores virtual hangups (vhangup()), to keep
  393. * an existing IUCV communication path established.
  394. * (Background: vhangup() is called from user space (by getty or login) to
  395. * disable writing to the tty by other applications).
  396. *
  397. * If the tty has been opened (e.g. getty) and an established IUCV path has been
  398. * severed (we caused the tty hangup in that case), then the functions invokes
  399. * hvc_iucv_cleanup() to clean up.
  400. *
  401. * Locking: struct hvc_iucv_private->lock
  402. */
  403. static void hvc_iucv_notifier_hangup(struct hvc_struct *hp, int id)
  404. {
  405. struct hvc_iucv_private *priv;
  406. priv = hvc_iucv_get_private(id);
  407. if (!priv)
  408. return;
  409. spin_lock_bh(&priv->lock);
  410. /* NOTE: If the hangup was scheduled by ourself (from the iucv
  411. * path_servered callback [IUCV_SEVERED]), then we have to
  412. * finally clean up the tty backend structure and set state to
  413. * TTY_CLOSED.
  414. *
  415. * If the tty was hung up otherwise (e.g. vhangup()), then we
  416. * ignore this hangup and keep an established IUCV path open...
  417. * (...the reason is that we are not able to connect back to the
  418. * client if we disconnect on hang up) */
  419. priv->tty_state = TTY_CLOSED;
  420. if (priv->iucv_state == IUCV_SEVERED)
  421. hvc_iucv_cleanup(priv);
  422. spin_unlock_bh(&priv->lock);
  423. }
  424. /**
  425. * hvc_iucv_notifier_del() - HVC notifier for closing a TTY for the last time.
  426. * @hp: Pointer to the HVC device (struct hvc_struct)
  427. * @id: Additional data (originally passed to hvc_alloc):
  428. * the index of an struct hvc_iucv_private instance.
  429. *
  430. * This routine notifies the HVC backend that the last tty device file
  431. * descriptor has been closed.
  432. * The function calls hvc_iucv_cleanup() to clean up the struct hvc_iucv_private
  433. * instance.
  434. *
  435. * Locking: struct hvc_iucv_private->lock
  436. */
  437. static void hvc_iucv_notifier_del(struct hvc_struct *hp, int id)
  438. {
  439. struct hvc_iucv_private *priv;
  440. struct iucv_path *path;
  441. priv = hvc_iucv_get_private(id);
  442. if (!priv)
  443. return;
  444. spin_lock_bh(&priv->lock);
  445. path = priv->path; /* save reference to IUCV path */
  446. priv->path = NULL;
  447. hvc_iucv_cleanup(priv);
  448. spin_unlock_bh(&priv->lock);
  449. /* sever IUCV path outside of priv->lock due to lock ordering of:
  450. * priv->lock <--> iucv_table_lock */
  451. if (path) {
  452. iucv_path_sever(path, NULL);
  453. iucv_path_free(path);
  454. }
  455. }
  456. /**
  457. * hvc_iucv_path_pending() - IUCV handler to process a connection request.
  458. * @path: Pending path (struct iucv_path)
  459. * @ipvmid: Originator z/VM system identifier
  460. * @ipuser: User specified data for this path
  461. * (AF_IUCV: port/service name and originator port)
  462. *
  463. * The function uses the @ipuser data to check to determine if the pending
  464. * path belongs to a terminal managed by this HVC backend.
  465. * If the check is successful, then an additional check is done to ensure
  466. * that a terminal cannot be accessed multiple times (only one connection
  467. * to a terminal is allowed). In that particular case, the pending path is
  468. * severed. If it is the first connection, the pending path is accepted and
  469. * associated to the struct hvc_iucv_private. The iucv state is updated to
  470. * reflect that a communication path has been established.
  471. *
  472. * Returns 0 if the path belongs to a terminal managed by the this HVC backend;
  473. * otherwise returns -ENODEV in order to dispatch this path to other handlers.
  474. *
  475. * Locking: struct hvc_iucv_private->lock
  476. */
  477. static int hvc_iucv_path_pending(struct iucv_path *path,
  478. u8 ipvmid[8], u8 ipuser[16])
  479. {
  480. struct hvc_iucv_private *priv;
  481. u8 nuser_data[16];
  482. int i, rc;
  483. priv = NULL;
  484. for (i = 0; i < hvc_iucv_devices; i++)
  485. if (hvc_iucv_table[i] &&
  486. (0 == memcmp(hvc_iucv_table[i]->srv_name, ipuser, 8))) {
  487. priv = hvc_iucv_table[i];
  488. break;
  489. }
  490. if (!priv)
  491. return -ENODEV;
  492. spin_lock(&priv->lock);
  493. /* If the terminal is already connected or being severed, then sever
  494. * this path to enforce that there is only ONE established communication
  495. * path per terminal. */
  496. if (priv->iucv_state != IUCV_DISCONN) {
  497. iucv_path_sever(path, ipuser);
  498. iucv_path_free(path);
  499. goto out_path_handled;
  500. }
  501. /* accept path */
  502. memcpy(nuser_data, ipuser + 8, 8); /* remote service (for af_iucv) */
  503. memcpy(nuser_data + 8, ipuser, 8); /* local service (for af_iucv) */
  504. path->msglim = 0xffff; /* IUCV MSGLIMIT */
  505. path->flags &= ~IUCV_IPRMDATA; /* TODO: use IUCV_IPRMDATA */
  506. rc = iucv_path_accept(path, &hvc_iucv_handler, nuser_data, priv);
  507. if (rc) {
  508. iucv_path_sever(path, ipuser);
  509. iucv_path_free(path);
  510. goto out_path_handled;
  511. }
  512. priv->path = path;
  513. priv->iucv_state = IUCV_CONNECTED;
  514. out_path_handled:
  515. spin_unlock(&priv->lock);
  516. return 0;
  517. }
  518. /**
  519. * hvc_iucv_path_severed() - IUCV handler to process a path sever.
  520. * @path: Pending path (struct iucv_path)
  521. * @ipuser: User specified data for this path
  522. * (AF_IUCV: port/service name and originator port)
  523. *
  524. * The function also severs the path (as required by the IUCV protocol) and
  525. * sets the iucv state to IUCV_SEVERED for the associated struct
  526. * hvc_iucv_private instance. Later, the IUCV_SEVERED state triggers a tty
  527. * hangup (hvc_iucv_get_chars() / hvc_iucv_write()).
  528. *
  529. * If tty portion of the HVC is closed then clean up the outqueue in addition.
  530. *
  531. * Locking: struct hvc_iucv_private->lock
  532. */
  533. static void hvc_iucv_path_severed(struct iucv_path *path, u8 ipuser[16])
  534. {
  535. struct hvc_iucv_private *priv = path->private;
  536. spin_lock(&priv->lock);
  537. priv->iucv_state = IUCV_SEVERED;
  538. /* NOTE: If the tty has not yet been opened by a getty program
  539. * (e.g. to see console messages), then cleanup the
  540. * hvc_iucv_private structure to allow re-connects.
  541. *
  542. * If the tty has been opened, the get_chars() callback returns
  543. * -EPIPE to signal the hvc console layer to hang up the tty. */
  544. priv->path = NULL;
  545. if (priv->tty_state == TTY_CLOSED)
  546. hvc_iucv_cleanup(priv);
  547. spin_unlock(&priv->lock);
  548. /* finally sever path (outside of priv->lock due to lock ordering) */
  549. iucv_path_sever(path, ipuser);
  550. iucv_path_free(path);
  551. }
  552. /**
  553. * hvc_iucv_msg_pending() - IUCV handler to process an incoming IUCV message.
  554. * @path: Pending path (struct iucv_path)
  555. * @msg: Pointer to the IUCV message
  556. *
  557. * The function stores an incoming message on the input queue for later
  558. * processing (by hvc_iucv_get_chars() / hvc_iucv_write()).
  559. * However, if the tty has not yet been opened, the message is rejected.
  560. *
  561. * Locking: struct hvc_iucv_private->lock
  562. */
  563. static void hvc_iucv_msg_pending(struct iucv_path *path,
  564. struct iucv_message *msg)
  565. {
  566. struct hvc_iucv_private *priv = path->private;
  567. struct iucv_tty_buffer *rb;
  568. spin_lock(&priv->lock);
  569. /* reject messages if tty has not yet been opened */
  570. if (priv->tty_state == TTY_CLOSED) {
  571. iucv_message_reject(path, msg);
  572. goto unlock_return;
  573. }
  574. /* allocate buffer an empty buffer element */
  575. rb = alloc_tty_buffer(0, GFP_ATOMIC);
  576. if (!rb) {
  577. iucv_message_reject(path, msg);
  578. goto unlock_return; /* -ENOMEM */
  579. }
  580. rb->msg = *msg;
  581. list_add_tail(&rb->list, &priv->tty_inqueue);
  582. hvc_kick(); /* wakup hvc console thread */
  583. unlock_return:
  584. spin_unlock(&priv->lock);
  585. }
  586. /**
  587. * hvc_iucv_msg_complete() - IUCV handler to process message completion
  588. * @path: Pending path (struct iucv_path)
  589. * @msg: Pointer to the IUCV message
  590. *
  591. * The function is called upon completion of message delivery and the
  592. * message is removed from the outqueue. Additional delivery information
  593. * can be found in msg->audit: rejected messages (0x040000 (IPADRJCT)) and
  594. * purged messages (0x010000 (IPADPGNR)).
  595. *
  596. * Locking: struct hvc_iucv_private->lock
  597. */
  598. static void hvc_iucv_msg_complete(struct iucv_path *path,
  599. struct iucv_message *msg)
  600. {
  601. struct hvc_iucv_private *priv = path->private;
  602. struct iucv_tty_buffer *ent, *next;
  603. LIST_HEAD(list_remove);
  604. spin_lock(&priv->lock);
  605. list_for_each_entry_safe(ent, next, &priv->tty_outqueue, list)
  606. if (ent->msg.id == msg->id) {
  607. list_move(&ent->list, &list_remove);
  608. break;
  609. }
  610. spin_unlock(&priv->lock);
  611. destroy_tty_buffer_list(&list_remove);
  612. }
  613. /* HVC operations */
  614. static struct hv_ops hvc_iucv_ops = {
  615. .get_chars = hvc_iucv_get_chars,
  616. .put_chars = hvc_iucv_put_chars,
  617. .notifier_add = hvc_iucv_notifier_add,
  618. .notifier_del = hvc_iucv_notifier_del,
  619. .notifier_hangup = hvc_iucv_notifier_hangup,
  620. };
  621. /**
  622. * hvc_iucv_alloc() - Allocates a new struct hvc_iucv_private instance
  623. * @id: hvc_iucv_table index
  624. *
  625. * This function allocates a new hvc_iucv_private struct and put the
  626. * instance into hvc_iucv_table at index @id.
  627. * Returns 0 on success; otherwise non-zero.
  628. */
  629. static int __init hvc_iucv_alloc(int id)
  630. {
  631. struct hvc_iucv_private *priv;
  632. char name[9];
  633. int rc;
  634. priv = kzalloc(sizeof(struct hvc_iucv_private), GFP_KERNEL);
  635. if (!priv)
  636. return -ENOMEM;
  637. spin_lock_init(&priv->lock);
  638. INIT_LIST_HEAD(&priv->tty_outqueue);
  639. INIT_LIST_HEAD(&priv->tty_inqueue);
  640. /* Finally allocate hvc */
  641. priv->hvc = hvc_alloc(HVC_IUCV_MAGIC + id,
  642. HVC_IUCV_MAGIC + id, &hvc_iucv_ops, PAGE_SIZE);
  643. if (IS_ERR(priv->hvc)) {
  644. rc = PTR_ERR(priv->hvc);
  645. kfree(priv);
  646. return rc;
  647. }
  648. /* setup iucv related information */
  649. snprintf(name, 9, "ihvc%-4d", id);
  650. memcpy(priv->srv_name, name, 8);
  651. ASCEBC(priv->srv_name, 8);
  652. hvc_iucv_table[id] = priv;
  653. return 0;
  654. }
  655. /**
  656. * hvc_iucv_init() - Initialization of HVC backend for z/VM IUCV
  657. */
  658. static int __init hvc_iucv_init(void)
  659. {
  660. int rc, i;
  661. if (!MACHINE_IS_VM) {
  662. pr_warning("The z/VM IUCV Hypervisor console cannot be "
  663. "used without z/VM.\n");
  664. return -ENODEV;
  665. }
  666. if (!hvc_iucv_devices)
  667. return -ENODEV;
  668. if (hvc_iucv_devices > MAX_HVC_IUCV_LINES)
  669. return -EINVAL;
  670. hvc_iucv_buffer_cache = kmem_cache_create(KMSG_COMPONENT,
  671. sizeof(struct iucv_tty_buffer),
  672. 0, 0, NULL);
  673. if (!hvc_iucv_buffer_cache) {
  674. pr_err("Not enough memory for driver initialization "
  675. "(rs=%d).\n", 1);
  676. return -ENOMEM;
  677. }
  678. hvc_iucv_mempool = mempool_create_slab_pool(MEMPOOL_MIN_NR,
  679. hvc_iucv_buffer_cache);
  680. if (!hvc_iucv_mempool) {
  681. pr_err("Not enough memory for driver initialization "
  682. "(rs=%d).\n", 2);
  683. kmem_cache_destroy(hvc_iucv_buffer_cache);
  684. return -ENOMEM;
  685. }
  686. /* allocate hvc_iucv_private structs */
  687. for (i = 0; i < hvc_iucv_devices; i++) {
  688. rc = hvc_iucv_alloc(i);
  689. if (rc) {
  690. pr_err("Could not create new z/VM IUCV HVC backend "
  691. "rc=%d.\n", rc);
  692. goto out_error_hvc;
  693. }
  694. }
  695. /* register IUCV callback handler */
  696. rc = iucv_register(&hvc_iucv_handler, 0);
  697. if (rc) {
  698. pr_err("Could not register iucv handler (rc=%d).\n", rc);
  699. goto out_error_iucv;
  700. }
  701. return 0;
  702. out_error_iucv:
  703. iucv_unregister(&hvc_iucv_handler, 0);
  704. out_error_hvc:
  705. for (i = 0; i < hvc_iucv_devices; i++)
  706. if (hvc_iucv_table[i]) {
  707. if (hvc_iucv_table[i]->hvc)
  708. hvc_remove(hvc_iucv_table[i]->hvc);
  709. kfree(hvc_iucv_table[i]);
  710. }
  711. mempool_destroy(hvc_iucv_mempool);
  712. kmem_cache_destroy(hvc_iucv_buffer_cache);
  713. return rc;
  714. }
  715. /**
  716. * hvc_iucv_console_init() - Early console initialization
  717. */
  718. static int __init hvc_iucv_console_init(void)
  719. {
  720. if (!MACHINE_IS_VM || !hvc_iucv_devices)
  721. return -ENODEV;
  722. return hvc_instantiate(HVC_IUCV_MAGIC, 0, &hvc_iucv_ops);
  723. }
  724. /**
  725. * hvc_iucv_config() - Parsing of hvc_iucv= kernel command line parameter
  726. * @val: Parameter value (numeric)
  727. */
  728. static int __init hvc_iucv_config(char *val)
  729. {
  730. return strict_strtoul(val, 10, &hvc_iucv_devices);
  731. }
  732. module_init(hvc_iucv_init);
  733. console_initcall(hvc_iucv_console_init);
  734. __setup("hvc_iucv=", hvc_iucv_config);
  735. MODULE_LICENSE("GPL");
  736. MODULE_DESCRIPTION("HVC back-end for z/VM IUCV.");
  737. MODULE_AUTHOR("Hendrik Brueckner <brueckner@linux.vnet.ibm.com>");