hvc_iucv.c 37 KB

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