hvc_iucv.c 41 KB

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