hvcs.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * IBM eServer Hypervisor Virtual Console Server Device Driver
  4. * Copyright (C) 2003, 2004 IBM Corp.
  5. * Ryan S. Arnold (rsa@us.ibm.com)
  6. *
  7. * Author(s) : Ryan S. Arnold <rsa@us.ibm.com>
  8. *
  9. * This is the device driver for the IBM Hypervisor Virtual Console Server,
  10. * "hvcs". The IBM hvcs provides a tty driver interface to allow Linux
  11. * user space applications access to the system consoles of logically
  12. * partitioned operating systems, e.g. Linux, running on the same partitioned
  13. * Power5 ppc64 system. Physical hardware consoles per partition are not
  14. * practical on this hardware so system consoles are accessed by this driver
  15. * using inter-partition firmware interfaces to virtual terminal devices.
  16. *
  17. * A vty is known to the HMC as a "virtual serial server adapter". It is a
  18. * virtual terminal device that is created by firmware upon partition creation
  19. * to act as a partitioned OS's console device.
  20. *
  21. * Firmware dynamically (via hotplug) exposes vty-servers to a running ppc64
  22. * Linux system upon their creation by the HMC or their exposure during boot.
  23. * The non-user interactive backend of this driver is implemented as a vio
  24. * device driver so that it can receive notification of vty-server lifetimes
  25. * after it registers with the vio bus to handle vty-server probe and remove
  26. * callbacks.
  27. *
  28. * Many vty-servers can be configured to connect to one vty, but a vty can
  29. * only be actively connected to by a single vty-server, in any manner, at one
  30. * time. If the HMC is currently hosting the console for a target Linux
  31. * partition; attempts to open the tty device to the partition's console using
  32. * the hvcs on any partition will return -EBUSY with every open attempt until
  33. * the HMC frees the connection between its vty-server and the desired
  34. * partition's vty device. Conversely, a vty-server may only be connected to
  35. * a single vty at one time even though it may have several configured vty
  36. * partner possibilities.
  37. *
  38. * Firmware does not provide notification of vty partner changes to this
  39. * driver. This means that an HMC Super Admin may add or remove partner vtys
  40. * from a vty-server's partner list but the changes will not be signaled to
  41. * the vty-server. Firmware only notifies the driver when a vty-server is
  42. * added or removed from the system. To compensate for this deficiency, this
  43. * driver implements a sysfs update attribute which provides a method for
  44. * rescanning partner information upon a user's request.
  45. *
  46. * Each vty-server, prior to being exposed to this driver is reference counted
  47. * using the 2.6 Linux kernel kref construct.
  48. *
  49. * For direction on installation and usage of this driver please reference
  50. * Documentation/arch/powerpc/hvcs.rst.
  51. */
  52. #include <linux/device.h>
  53. #include <linux/init.h>
  54. #include <linux/completion.h>
  55. #include <linux/interrupt.h>
  56. #include <linux/kernel.h>
  57. #include <linux/kref.h>
  58. #include <linux/kthread.h>
  59. #include <linux/list.h>
  60. #include <linux/major.h>
  61. #include <linux/module.h>
  62. #include <linux/moduleparam.h>
  63. #include <linux/sched.h>
  64. #include <linux/slab.h>
  65. #include <linux/spinlock.h>
  66. #include <linux/stat.h>
  67. #include <linux/tty.h>
  68. #include <linux/tty_flip.h>
  69. #include <asm/hvconsole.h>
  70. #include <asm/hvcserver.h>
  71. #include <linux/uaccess.h>
  72. #include <linux/termios_internal.h>
  73. #include <asm/vio.h>
  74. /*
  75. * 1.3.0 -> 1.3.1 In hvcs_open memset(..,0x00,..) instead of memset(..,0x3F,00).
  76. * Removed braces around single statements following conditionals. Removed '=
  77. * 0' after static int declarations since these default to zero. Removed
  78. * list_for_each_safe() and replaced with list_for_each_entry() in
  79. * hvcs_get_by_index(). The 'safe' version is un-needed now that the driver is
  80. * using spinlocks. Changed spin_lock_irqsave() to spin_lock() when locking
  81. * hvcs_structs_lock and hvcs_pi_lock since these are not touched in an int
  82. * handler. Initialized hvcs_structs_lock and hvcs_pi_lock to
  83. * SPIN_LOCK_UNLOCKED at declaration time rather than in hvcs_module_init().
  84. * Added spin_lock around list_del() in destroy_hvcs_struct() to protect the
  85. * list traversals from a deletion. Removed '= NULL' from pointer declaration
  86. * statements since they are initialized NULL by default. Removed wmb()
  87. * instances from hvcs_try_write(). They probably aren't needed with locking in
  88. * place. Added check and cleanup for hvcs_pi_buff = kmalloc() in
  89. * hvcs_module_init(). Exposed hvcs_struct.index via a sysfs attribute so that
  90. * the coupling between /dev/hvcs* and a vty-server can be automatically
  91. * determined. Moved kobject_put() in hvcs_open outside of the
  92. * spin_unlock_irqrestore().
  93. *
  94. * 1.3.1 -> 1.3.2 Changed method for determining hvcs_struct->index and had it
  95. * align with how the tty layer always assigns the lowest index available. This
  96. * change resulted in a list of ints that denotes which indexes are available.
  97. * Device additions and removals use the new hvcs_get_index() and
  98. * hvcs_return_index() helper functions. The list is created with
  99. * hvsc_alloc_index_list() and it is destroyed with hvcs_free_index_list().
  100. * Without these fixes hotplug vty-server adapter support goes crazy with this
  101. * driver if the user removes a vty-server adapter. Moved free_irq() outside of
  102. * the hvcs_final_close() function in order to get it out of the spinlock.
  103. * Rearranged hvcs_close(). Cleaned up some printks and did some housekeeping
  104. * on the changelog. Removed local CLC_LENGTH and used HVCS_CLC_LENGTH from
  105. * arch/powerepc/include/asm/hvcserver.h
  106. *
  107. * 1.3.2 -> 1.3.3 Replaced yield() in hvcs_close() with tty_wait_until_sent() to
  108. * prevent possible lockup with realtime scheduling as similarly pointed out by
  109. * akpm in hvc_console. Changed resulted in the removal of hvcs_final_close()
  110. * to reorder cleanup operations and prevent discarding of pending data during
  111. * an hvcs_close(). Removed spinlock protection of hvcs_struct data members in
  112. * hvcs_write_room() and hvcs_chars_in_buffer() because they aren't needed.
  113. */
  114. #define HVCS_DRIVER_VERSION "1.3.3"
  115. MODULE_AUTHOR("Ryan S. Arnold <rsa@us.ibm.com>");
  116. MODULE_DESCRIPTION("IBM hvcs (Hypervisor Virtual Console Server) Driver");
  117. MODULE_LICENSE("GPL");
  118. MODULE_VERSION(HVCS_DRIVER_VERSION);
  119. /*
  120. * Wait this long per iteration while trying to push buffered data to the
  121. * hypervisor before allowing the tty to complete a close operation.
  122. */
  123. #define HVCS_CLOSE_WAIT (HZ/100) /* 1/10 of a second */
  124. /*
  125. * Since the Linux TTY code does not currently (2-04-2004) support dynamic
  126. * addition of tty derived devices and we shouldn't allocate thousands of
  127. * tty_device pointers when the number of vty-server & vty partner connections
  128. * will most often be much lower than this, we'll arbitrarily allocate
  129. * HVCS_DEFAULT_SERVER_ADAPTERS tty_structs and cdev's by default when we
  130. * register the tty_driver. This can be overridden using an insmod parameter.
  131. */
  132. #define HVCS_DEFAULT_SERVER_ADAPTERS 64
  133. /*
  134. * The user can't insmod with more than HVCS_MAX_SERVER_ADAPTERS hvcs device
  135. * nodes as a sanity check. Theoretically there can be over 1 Billion
  136. * vty-server & vty partner connections.
  137. */
  138. #define HVCS_MAX_SERVER_ADAPTERS 1024
  139. /*
  140. * We let Linux assign us a major number and we start the minors at zero. There
  141. * is no intuitive mapping between minor number and the target vty-server
  142. * adapter except that each new vty-server adapter is always assigned to the
  143. * smallest minor number available.
  144. */
  145. #define HVCS_MINOR_START 0
  146. /*
  147. * The hcall interface involves putting 8 chars into each of two registers.
  148. * We load up those 2 registers (in arch/powerpc/platforms/pseries/hvconsole.c)
  149. * by casting char[16] to long[2]. It would work without __ALIGNED__, but a
  150. * little (tiny) bit slower because an unaligned load is slower than aligned
  151. * load.
  152. */
  153. #define __ALIGNED__ __attribute__((__aligned__(8)))
  154. /*
  155. * How much data can firmware send with each hvc_put_chars()? Maybe this
  156. * should be moved into an architecture specific area.
  157. */
  158. #define HVCS_BUFF_LEN 16
  159. /*
  160. * This is the maximum amount of data we'll let the user send us (hvcs_write) at
  161. * once in a chunk as a sanity check.
  162. */
  163. #define HVCS_MAX_FROM_USER 4096
  164. /*
  165. * Be careful when adding flags to this line discipline. Don't add anything
  166. * that will cause echoing or we'll go into recursive loop echoing chars back
  167. * and forth with the console drivers.
  168. */
  169. static const struct ktermios hvcs_tty_termios = {
  170. .c_iflag = IGNBRK | IGNPAR,
  171. .c_oflag = OPOST,
  172. .c_cflag = B38400 | CS8 | CREAD | HUPCL,
  173. .c_cc = INIT_C_CC,
  174. .c_ispeed = 38400,
  175. .c_ospeed = 38400
  176. };
  177. /*
  178. * This value is used to take the place of a command line parameter when the
  179. * module is inserted. It starts as -1 and stays as such if the user doesn't
  180. * specify a module insmod parameter. If they DO specify one then it is set to
  181. * the value of the integer passed in.
  182. */
  183. static int hvcs_parm_num_devs = -1;
  184. module_param(hvcs_parm_num_devs, int, 0);
  185. static const char hvcs_driver_name[] = "hvcs";
  186. static const char hvcs_device_node[] = "hvcs";
  187. /* Status of partner info rescan triggered via sysfs. */
  188. static int hvcs_rescan_status;
  189. static struct tty_driver *hvcs_tty_driver;
  190. /*
  191. * In order to be somewhat sane this driver always associates the hvcs_struct
  192. * index element with the numerically equal tty->index. This means that a
  193. * hotplugged vty-server adapter will always map to the lowest index valued
  194. * device node. If vty-servers were hotplug removed from the system and then
  195. * new ones added the new vty-server may have the largest slot number of all
  196. * the vty-server adapters in the partition but it may have the lowest dev node
  197. * index of all the adapters due to the hole left by the hotplug removed
  198. * adapter. There are a set of functions provided to get the lowest index for
  199. * a new device as well as return the index to the list. This list is allocated
  200. * with a number of elements equal to the number of device nodes requested when
  201. * the module was inserted.
  202. */
  203. static int *hvcs_index_list;
  204. /*
  205. * How large is the list? This is kept for traversal since the list is
  206. * dynamically created.
  207. */
  208. static int hvcs_index_count;
  209. /*
  210. * Used by the khvcsd to pick up I/O operations when the kernel_thread is
  211. * already awake but potentially shifted to TASK_INTERRUPTIBLE state.
  212. */
  213. static int hvcs_kicked;
  214. /*
  215. * Use by the kthread construct for task operations like waking the sleeping
  216. * thread and stopping the kthread.
  217. */
  218. static struct task_struct *hvcs_task;
  219. /*
  220. * We allocate this for the use of all of the hvcs_structs when they fetch
  221. * partner info.
  222. */
  223. static unsigned long *hvcs_pi_buff;
  224. /* Only allow one hvcs_struct to use the hvcs_pi_buff at a time. */
  225. static DEFINE_SPINLOCK(hvcs_pi_lock);
  226. /* One vty-server per hvcs_struct */
  227. struct hvcs_struct {
  228. struct tty_port port;
  229. spinlock_t lock;
  230. /*
  231. * This index identifies this hvcs device as the complement to a
  232. * specific tty index.
  233. */
  234. unsigned int index;
  235. /*
  236. * Used to tell the driver kernel_thread what operations need to take
  237. * place upon this hvcs_struct instance.
  238. */
  239. int todo_mask;
  240. /*
  241. * This buffer is required so that when hvcs_write_room() reports that
  242. * it can send HVCS_BUFF_LEN characters that it will buffer the full
  243. * HVCS_BUFF_LEN characters if need be. This is essential for opost
  244. * writes since they do not do high level buffering and expect to be
  245. * able to send what the driver commits to sending buffering
  246. * [e.g. tab to space conversions in n_tty.c opost()].
  247. */
  248. char buffer[HVCS_BUFF_LEN];
  249. int chars_in_buffer;
  250. /*
  251. * Any variable below is valid before a tty is connected and
  252. * stays valid after the tty is disconnected. These shouldn't be
  253. * whacked until the kobject refcount reaches zero though some entries
  254. * may be changed via sysfs initiatives.
  255. */
  256. int connected; /* is the vty-server currently connected to a vty? */
  257. uint32_t p_unit_address; /* partner unit address */
  258. uint32_t p_partition_ID; /* partner partition ID */
  259. char p_location_code[HVCS_CLC_LENGTH + 1]; /* CLC + Null Term */
  260. struct list_head next; /* list management */
  261. struct vio_dev *vdev;
  262. struct completion *destroyed;
  263. };
  264. static LIST_HEAD(hvcs_structs);
  265. static DEFINE_SPINLOCK(hvcs_structs_lock);
  266. static DEFINE_MUTEX(hvcs_init_mutex);
  267. static int hvcs_get_pi(struct hvcs_struct *hvcsd);
  268. static int hvcs_rescan_devices_list(void);
  269. static void hvcs_partner_free(struct hvcs_struct *hvcsd);
  270. static int hvcs_initialize(void);
  271. #define HVCS_SCHED_READ 0x00000001
  272. #define HVCS_QUICK_READ 0x00000002
  273. #define HVCS_TRY_WRITE 0x00000004
  274. #define HVCS_READ_MASK (HVCS_SCHED_READ | HVCS_QUICK_READ)
  275. static inline struct hvcs_struct *from_vio_dev(struct vio_dev *viod)
  276. {
  277. return dev_get_drvdata(&viod->dev);
  278. }
  279. /* The sysfs interface for the driver and devices */
  280. static ssize_t hvcs_partner_vtys_show(struct device *dev, struct device_attribute *attr, char *buf)
  281. {
  282. struct vio_dev *viod = to_vio_dev(dev);
  283. struct hvcs_struct *hvcsd = from_vio_dev(viod);
  284. unsigned long flags;
  285. int retval;
  286. spin_lock_irqsave(&hvcsd->lock, flags);
  287. retval = sprintf(buf, "%X\n", hvcsd->p_unit_address);
  288. spin_unlock_irqrestore(&hvcsd->lock, flags);
  289. return retval;
  290. }
  291. static DEVICE_ATTR(partner_vtys, S_IRUGO, hvcs_partner_vtys_show, NULL);
  292. static ssize_t hvcs_partner_clcs_show(struct device *dev, struct device_attribute *attr, char *buf)
  293. {
  294. struct vio_dev *viod = to_vio_dev(dev);
  295. struct hvcs_struct *hvcsd = from_vio_dev(viod);
  296. unsigned long flags;
  297. int retval;
  298. spin_lock_irqsave(&hvcsd->lock, flags);
  299. retval = sprintf(buf, "%s\n", &hvcsd->p_location_code[0]);
  300. spin_unlock_irqrestore(&hvcsd->lock, flags);
  301. return retval;
  302. }
  303. static DEVICE_ATTR(partner_clcs, S_IRUGO, hvcs_partner_clcs_show, NULL);
  304. static ssize_t hvcs_current_vty_store(struct device *dev, struct device_attribute *attr, const char * buf,
  305. size_t count)
  306. {
  307. /*
  308. * Don't need this feature at the present time because firmware doesn't
  309. * yet support multiple partners.
  310. */
  311. printk(KERN_INFO "HVCS: Denied current_vty change: -EPERM.\n");
  312. return -EPERM;
  313. }
  314. static ssize_t hvcs_current_vty_show(struct device *dev, struct device_attribute *attr, char *buf)
  315. {
  316. struct vio_dev *viod = to_vio_dev(dev);
  317. struct hvcs_struct *hvcsd = from_vio_dev(viod);
  318. unsigned long flags;
  319. int retval;
  320. spin_lock_irqsave(&hvcsd->lock, flags);
  321. retval = sprintf(buf, "%s\n", &hvcsd->p_location_code[0]);
  322. spin_unlock_irqrestore(&hvcsd->lock, flags);
  323. return retval;
  324. }
  325. static DEVICE_ATTR(current_vty,
  326. S_IRUGO | S_IWUSR, hvcs_current_vty_show, hvcs_current_vty_store);
  327. static ssize_t hvcs_vterm_state_store(struct device *dev, struct device_attribute *attr, const char *buf,
  328. size_t count)
  329. {
  330. struct vio_dev *viod = to_vio_dev(dev);
  331. struct hvcs_struct *hvcsd = from_vio_dev(viod);
  332. unsigned long flags;
  333. /* writing a '0' to this sysfs entry will result in the disconnect. */
  334. if (simple_strtol(buf, NULL, 0) != 0)
  335. return -EINVAL;
  336. spin_lock_irqsave(&hvcsd->lock, flags);
  337. if (hvcsd->port.count > 0) {
  338. spin_unlock_irqrestore(&hvcsd->lock, flags);
  339. printk(KERN_INFO "HVCS: vterm state unchanged. "
  340. "The hvcs device node is still in use.\n");
  341. return -EPERM;
  342. }
  343. if (hvcsd->connected == 0) {
  344. spin_unlock_irqrestore(&hvcsd->lock, flags);
  345. printk(KERN_INFO "HVCS: vterm state unchanged. The"
  346. " vty-server is not connected to a vty.\n");
  347. return -EPERM;
  348. }
  349. hvcs_partner_free(hvcsd);
  350. printk(KERN_INFO "HVCS: Closed vty-server@%X and"
  351. " partner vty@%X:%d connection.\n",
  352. hvcsd->vdev->unit_address,
  353. hvcsd->p_unit_address,
  354. (uint32_t)hvcsd->p_partition_ID);
  355. spin_unlock_irqrestore(&hvcsd->lock, flags);
  356. return count;
  357. }
  358. static ssize_t hvcs_vterm_state_show(struct device *dev, struct device_attribute *attr, char *buf)
  359. {
  360. struct vio_dev *viod = to_vio_dev(dev);
  361. struct hvcs_struct *hvcsd = from_vio_dev(viod);
  362. unsigned long flags;
  363. int retval;
  364. spin_lock_irqsave(&hvcsd->lock, flags);
  365. retval = sprintf(buf, "%d\n", hvcsd->connected);
  366. spin_unlock_irqrestore(&hvcsd->lock, flags);
  367. return retval;
  368. }
  369. static DEVICE_ATTR(vterm_state, S_IRUGO | S_IWUSR,
  370. hvcs_vterm_state_show, hvcs_vterm_state_store);
  371. static ssize_t hvcs_index_show(struct device *dev, struct device_attribute *attr, char *buf)
  372. {
  373. struct vio_dev *viod = to_vio_dev(dev);
  374. struct hvcs_struct *hvcsd = from_vio_dev(viod);
  375. unsigned long flags;
  376. int retval;
  377. spin_lock_irqsave(&hvcsd->lock, flags);
  378. retval = sprintf(buf, "%d\n", hvcsd->index);
  379. spin_unlock_irqrestore(&hvcsd->lock, flags);
  380. return retval;
  381. }
  382. static DEVICE_ATTR(index, S_IRUGO, hvcs_index_show, NULL);
  383. static struct attribute *hvcs_dev_attrs[] = {
  384. &dev_attr_partner_vtys.attr,
  385. &dev_attr_partner_clcs.attr,
  386. &dev_attr_current_vty.attr,
  387. &dev_attr_vterm_state.attr,
  388. &dev_attr_index.attr,
  389. NULL,
  390. };
  391. ATTRIBUTE_GROUPS(hvcs_dev);
  392. static ssize_t rescan_show(struct device_driver *ddp, char *buf)
  393. {
  394. /* A 1 means it is updating, a 0 means it is done updating */
  395. return snprintf(buf, PAGE_SIZE, "%d\n", hvcs_rescan_status);
  396. }
  397. static ssize_t rescan_store(struct device_driver *ddp, const char * buf,
  398. size_t count)
  399. {
  400. if ((simple_strtol(buf, NULL, 0) != 1)
  401. && (hvcs_rescan_status != 0))
  402. return -EINVAL;
  403. hvcs_rescan_status = 1;
  404. printk(KERN_INFO "HVCS: rescanning partner info for all"
  405. " vty-servers.\n");
  406. hvcs_rescan_devices_list();
  407. hvcs_rescan_status = 0;
  408. return count;
  409. }
  410. static DRIVER_ATTR_RW(rescan);
  411. static struct attribute *hvcs_attrs[] = {
  412. &driver_attr_rescan.attr,
  413. NULL,
  414. };
  415. ATTRIBUTE_GROUPS(hvcs);
  416. static void hvcs_kick(void)
  417. {
  418. hvcs_kicked = 1;
  419. wmb();
  420. wake_up_process(hvcs_task);
  421. }
  422. static void hvcs_unthrottle(struct tty_struct *tty)
  423. {
  424. struct hvcs_struct *hvcsd = tty->driver_data;
  425. unsigned long flags;
  426. spin_lock_irqsave(&hvcsd->lock, flags);
  427. hvcsd->todo_mask |= HVCS_SCHED_READ;
  428. spin_unlock_irqrestore(&hvcsd->lock, flags);
  429. hvcs_kick();
  430. }
  431. static void hvcs_throttle(struct tty_struct *tty)
  432. {
  433. struct hvcs_struct *hvcsd = tty->driver_data;
  434. unsigned long flags;
  435. spin_lock_irqsave(&hvcsd->lock, flags);
  436. vio_disable_interrupts(hvcsd->vdev);
  437. spin_unlock_irqrestore(&hvcsd->lock, flags);
  438. }
  439. /*
  440. * If the device is being removed we don't have to worry about this interrupt
  441. * handler taking any further interrupts because they are disabled which means
  442. * the hvcs_struct will always be valid in this handler.
  443. */
  444. static irqreturn_t hvcs_handle_interrupt(int irq, void *dev_instance)
  445. {
  446. struct hvcs_struct *hvcsd = dev_instance;
  447. spin_lock(&hvcsd->lock);
  448. vio_disable_interrupts(hvcsd->vdev);
  449. hvcsd->todo_mask |= HVCS_SCHED_READ;
  450. spin_unlock(&hvcsd->lock);
  451. hvcs_kick();
  452. return IRQ_HANDLED;
  453. }
  454. /* This function must be called with the hvcsd->lock held */
  455. static void hvcs_try_write(struct hvcs_struct *hvcsd)
  456. {
  457. uint32_t unit_address = hvcsd->vdev->unit_address;
  458. struct tty_struct *tty = hvcsd->port.tty;
  459. int sent;
  460. if (hvcsd->todo_mask & HVCS_TRY_WRITE) {
  461. /* won't send partial writes */
  462. sent = hvc_put_chars(unit_address,
  463. &hvcsd->buffer[0],
  464. hvcsd->chars_in_buffer );
  465. if (sent > 0) {
  466. hvcsd->chars_in_buffer = 0;
  467. /* wmb(); */
  468. hvcsd->todo_mask &= ~(HVCS_TRY_WRITE);
  469. /* wmb(); */
  470. /*
  471. * We are still obligated to deliver the data to the
  472. * hypervisor even if the tty has been closed because
  473. * we committed to delivering it. But don't try to wake
  474. * a non-existent tty.
  475. */
  476. if (tty) {
  477. tty_wakeup(tty);
  478. }
  479. }
  480. }
  481. }
  482. static int hvcs_io(struct hvcs_struct *hvcsd)
  483. {
  484. uint32_t unit_address;
  485. struct tty_struct *tty;
  486. char buf[HVCS_BUFF_LEN] __ALIGNED__;
  487. unsigned long flags;
  488. int got = 0;
  489. spin_lock_irqsave(&hvcsd->lock, flags);
  490. unit_address = hvcsd->vdev->unit_address;
  491. tty = hvcsd->port.tty;
  492. hvcs_try_write(hvcsd);
  493. if (!tty || tty_throttled(tty)) {
  494. hvcsd->todo_mask &= ~(HVCS_READ_MASK);
  495. goto bail;
  496. } else if (!(hvcsd->todo_mask & (HVCS_READ_MASK)))
  497. goto bail;
  498. /* remove the read masks */
  499. hvcsd->todo_mask &= ~(HVCS_READ_MASK);
  500. if (tty_buffer_request_room(&hvcsd->port, HVCS_BUFF_LEN) >= HVCS_BUFF_LEN) {
  501. got = hvc_get_chars(unit_address,
  502. &buf[0],
  503. HVCS_BUFF_LEN);
  504. tty_insert_flip_string(&hvcsd->port, buf, got);
  505. }
  506. /* Give the TTY time to process the data we just sent. */
  507. if (got)
  508. hvcsd->todo_mask |= HVCS_QUICK_READ;
  509. spin_unlock_irqrestore(&hvcsd->lock, flags);
  510. /* This is synch -- FIXME :js: it is not! */
  511. if (got)
  512. tty_flip_buffer_push(&hvcsd->port);
  513. else {
  514. /* Do this _after_ the flip_buffer_push */
  515. spin_lock_irqsave(&hvcsd->lock, flags);
  516. vio_enable_interrupts(hvcsd->vdev);
  517. spin_unlock_irqrestore(&hvcsd->lock, flags);
  518. }
  519. return hvcsd->todo_mask;
  520. bail:
  521. spin_unlock_irqrestore(&hvcsd->lock, flags);
  522. return hvcsd->todo_mask;
  523. }
  524. static int khvcsd(void *unused)
  525. {
  526. struct hvcs_struct *hvcsd;
  527. int hvcs_todo_mask;
  528. __set_current_state(TASK_RUNNING);
  529. do {
  530. hvcs_todo_mask = 0;
  531. hvcs_kicked = 0;
  532. wmb();
  533. spin_lock(&hvcs_structs_lock);
  534. list_for_each_entry(hvcsd, &hvcs_structs, next) {
  535. hvcs_todo_mask |= hvcs_io(hvcsd);
  536. }
  537. spin_unlock(&hvcs_structs_lock);
  538. /*
  539. * If any of the hvcs adapters want to try a write or quick read
  540. * don't schedule(), yield a smidgen then execute the hvcs_io
  541. * thread again for those that want the write.
  542. */
  543. if (hvcs_todo_mask & (HVCS_TRY_WRITE | HVCS_QUICK_READ)) {
  544. yield();
  545. continue;
  546. }
  547. set_current_state(TASK_INTERRUPTIBLE);
  548. if (!hvcs_kicked)
  549. schedule();
  550. __set_current_state(TASK_RUNNING);
  551. } while (!kthread_should_stop());
  552. return 0;
  553. }
  554. static const struct vio_device_id hvcs_driver_table[] = {
  555. {"serial-server", "hvterm2"},
  556. { "", "" }
  557. };
  558. MODULE_DEVICE_TABLE(vio, hvcs_driver_table);
  559. static void hvcs_return_index(int index)
  560. {
  561. /* Paranoia check */
  562. if (!hvcs_index_list)
  563. return;
  564. if (index < 0 || index >= hvcs_index_count)
  565. return;
  566. if (hvcs_index_list[index] == -1)
  567. return;
  568. else
  569. hvcs_index_list[index] = -1;
  570. }
  571. static void hvcs_destruct_port(struct tty_port *p)
  572. {
  573. struct hvcs_struct *hvcsd = container_of(p, struct hvcs_struct, port);
  574. struct completion *comp;
  575. unsigned long flags;
  576. spin_lock(&hvcs_structs_lock);
  577. spin_lock_irqsave(&hvcsd->lock, flags);
  578. comp = hvcsd->destroyed;
  579. /* the list_del poisons the pointers */
  580. list_del(&(hvcsd->next));
  581. if (hvcsd->connected == 1) {
  582. hvcs_partner_free(hvcsd);
  583. printk(KERN_INFO "HVCS: Closed vty-server@%X and"
  584. " partner vty@%X:%d connection.\n",
  585. hvcsd->vdev->unit_address,
  586. hvcsd->p_unit_address,
  587. (uint32_t)hvcsd->p_partition_ID);
  588. }
  589. printk(KERN_INFO "HVCS: Destroyed hvcs_struct for vty-server@%X.\n",
  590. hvcsd->vdev->unit_address);
  591. hvcsd->vdev = NULL;
  592. hvcsd->p_unit_address = 0;
  593. hvcsd->p_partition_ID = 0;
  594. hvcsd->destroyed = NULL;
  595. hvcs_return_index(hvcsd->index);
  596. memset(&hvcsd->p_location_code[0], 0x00, HVCS_CLC_LENGTH + 1);
  597. spin_unlock_irqrestore(&hvcsd->lock, flags);
  598. spin_unlock(&hvcs_structs_lock);
  599. kfree(hvcsd);
  600. if (comp)
  601. complete(comp);
  602. }
  603. static const struct tty_port_operations hvcs_port_ops = {
  604. .destruct = hvcs_destruct_port,
  605. };
  606. static int hvcs_get_index(void)
  607. {
  608. int i;
  609. /* Paranoia check */
  610. if (!hvcs_index_list) {
  611. printk(KERN_ERR "HVCS: hvcs_index_list NOT valid!.\n");
  612. return -EFAULT;
  613. }
  614. /* Find the numerically lowest first free index. */
  615. for(i = 0; i < hvcs_index_count; i++) {
  616. if (hvcs_index_list[i] == -1) {
  617. hvcs_index_list[i] = 0;
  618. return i;
  619. }
  620. }
  621. return -1;
  622. }
  623. static int hvcs_probe(
  624. struct vio_dev *dev,
  625. const struct vio_device_id *id)
  626. {
  627. struct hvcs_struct *hvcsd;
  628. int index, rc;
  629. if (!dev || !id) {
  630. printk(KERN_ERR "HVCS: probed with invalid parameter.\n");
  631. return -EPERM;
  632. }
  633. /* Make sure we are properly initialized */
  634. rc = hvcs_initialize();
  635. if (rc) {
  636. pr_err("HVCS: Failed to initialize core driver.\n");
  637. return rc;
  638. }
  639. /* early to avoid cleanup on failure */
  640. index = hvcs_get_index();
  641. if (index < 0) {
  642. return -EFAULT;
  643. }
  644. hvcsd = kzalloc(sizeof(*hvcsd), GFP_KERNEL);
  645. if (!hvcsd)
  646. return -ENODEV;
  647. tty_port_init(&hvcsd->port);
  648. hvcsd->port.ops = &hvcs_port_ops;
  649. spin_lock_init(&hvcsd->lock);
  650. hvcsd->vdev = dev;
  651. dev_set_drvdata(&dev->dev, hvcsd);
  652. hvcsd->index = index;
  653. /* hvcsd->index = ++hvcs_struct_count; */
  654. hvcsd->chars_in_buffer = 0;
  655. hvcsd->todo_mask = 0;
  656. hvcsd->connected = 0;
  657. /*
  658. * This will populate the hvcs_struct's partner info fields for the
  659. * first time.
  660. */
  661. if (hvcs_get_pi(hvcsd)) {
  662. printk(KERN_ERR "HVCS: Failed to fetch partner"
  663. " info for vty-server@%X on device probe.\n",
  664. hvcsd->vdev->unit_address);
  665. }
  666. /*
  667. * If a user app opens a tty that corresponds to this vty-server before
  668. * the hvcs_struct has been added to the devices list then the user app
  669. * will get -ENODEV.
  670. */
  671. spin_lock(&hvcs_structs_lock);
  672. list_add_tail(&(hvcsd->next), &hvcs_structs);
  673. spin_unlock(&hvcs_structs_lock);
  674. printk(KERN_INFO "HVCS: vty-server@%X added to the vio bus.\n", dev->unit_address);
  675. /*
  676. * DON'T enable interrupts here because there is no user to receive the
  677. * data.
  678. */
  679. return 0;
  680. }
  681. static void hvcs_remove(struct vio_dev *dev)
  682. {
  683. struct hvcs_struct *hvcsd = dev_get_drvdata(&dev->dev);
  684. DECLARE_COMPLETION_ONSTACK(comp);
  685. unsigned long flags;
  686. struct tty_struct *tty;
  687. /* By this time the vty-server won't be getting any more interrupts */
  688. spin_lock_irqsave(&hvcsd->lock, flags);
  689. hvcsd->destroyed = &comp;
  690. tty = tty_port_tty_get(&hvcsd->port);
  691. spin_unlock_irqrestore(&hvcsd->lock, flags);
  692. /*
  693. * The tty should always be valid at this time unless a
  694. * simultaneous tty close already cleaned up the hvcs_struct.
  695. */
  696. if (tty) {
  697. tty_vhangup(tty);
  698. tty_kref_put(tty);
  699. }
  700. tty_port_put(&hvcsd->port);
  701. wait_for_completion(&comp);
  702. printk(KERN_INFO "HVCS: vty-server@%X removed from the"
  703. " vio bus.\n", dev->unit_address);
  704. };
  705. static struct vio_driver hvcs_vio_driver = {
  706. .id_table = hvcs_driver_table,
  707. .probe = hvcs_probe,
  708. .remove = hvcs_remove,
  709. .name = hvcs_driver_name,
  710. .driver = {
  711. .groups = hvcs_groups,
  712. .dev_groups = hvcs_dev_groups,
  713. },
  714. };
  715. /* Only called from hvcs_get_pi please */
  716. static void hvcs_set_pi(struct hvcs_partner_info *pi, struct hvcs_struct *hvcsd)
  717. {
  718. hvcsd->p_unit_address = pi->unit_address;
  719. hvcsd->p_partition_ID = pi->partition_ID;
  720. /* copy the null-term char too */
  721. strscpy(hvcsd->p_location_code, pi->location_code,
  722. sizeof(hvcsd->p_location_code));
  723. }
  724. /*
  725. * Traverse the list and add the partner info that is found to the hvcs_struct
  726. * struct entry. NOTE: At this time I know that partner info will return a
  727. * single entry but in the future there may be multiple partner info entries per
  728. * vty-server and you'll want to zero out that list and reset it. If for some
  729. * reason you have an old version of this driver but there IS more than one
  730. * partner info then hvcsd->p_* will hold the last partner info data from the
  731. * firmware query. A good way to update this code would be to replace the three
  732. * partner info fields in hvcs_struct with a list of hvcs_partner_info
  733. * instances.
  734. *
  735. * This function must be called with the hvcsd->lock held.
  736. */
  737. static int hvcs_get_pi(struct hvcs_struct *hvcsd)
  738. {
  739. struct hvcs_partner_info *pi;
  740. uint32_t unit_address = hvcsd->vdev->unit_address;
  741. struct list_head head;
  742. int retval;
  743. spin_lock(&hvcs_pi_lock);
  744. if (!hvcs_pi_buff) {
  745. spin_unlock(&hvcs_pi_lock);
  746. return -EFAULT;
  747. }
  748. retval = hvcs_get_partner_info(unit_address, &head, hvcs_pi_buff);
  749. spin_unlock(&hvcs_pi_lock);
  750. if (retval) {
  751. printk(KERN_ERR "HVCS: Failed to fetch partner"
  752. " info for vty-server@%x.\n", unit_address);
  753. return retval;
  754. }
  755. /* nixes the values if the partner vty went away */
  756. hvcsd->p_unit_address = 0;
  757. hvcsd->p_partition_ID = 0;
  758. list_for_each_entry(pi, &head, node)
  759. hvcs_set_pi(pi, hvcsd);
  760. hvcs_free_partner_info(&head);
  761. return 0;
  762. }
  763. /*
  764. * This function is executed by the driver "rescan" sysfs entry. It shouldn't
  765. * be executed elsewhere, in order to prevent deadlock issues.
  766. */
  767. static int hvcs_rescan_devices_list(void)
  768. {
  769. struct hvcs_struct *hvcsd;
  770. unsigned long flags;
  771. spin_lock(&hvcs_structs_lock);
  772. list_for_each_entry(hvcsd, &hvcs_structs, next) {
  773. spin_lock_irqsave(&hvcsd->lock, flags);
  774. hvcs_get_pi(hvcsd);
  775. spin_unlock_irqrestore(&hvcsd->lock, flags);
  776. }
  777. spin_unlock(&hvcs_structs_lock);
  778. return 0;
  779. }
  780. /*
  781. * Farm this off into its own function because it could be more complex once
  782. * multiple partners support is added. This function should be called with
  783. * the hvcsd->lock held.
  784. */
  785. static int hvcs_has_pi(struct hvcs_struct *hvcsd)
  786. {
  787. if ((!hvcsd->p_unit_address) || (!hvcsd->p_partition_ID))
  788. return 0;
  789. return 1;
  790. }
  791. /*
  792. * NOTE: It is possible that the super admin removed a partner vty and then
  793. * added a different vty as the new partner.
  794. *
  795. * This function must be called with the hvcsd->lock held.
  796. */
  797. static int hvcs_partner_connect(struct hvcs_struct *hvcsd)
  798. {
  799. int retval;
  800. unsigned int unit_address = hvcsd->vdev->unit_address;
  801. /*
  802. * If there wasn't any pi when the device was added it doesn't meant
  803. * there isn't any now. This driver isn't notified when a new partner
  804. * vty is added to a vty-server so we discover changes on our own.
  805. * Please see comments in hvcs_register_connection() for justification
  806. * of this bizarre code.
  807. */
  808. retval = hvcs_register_connection(unit_address,
  809. hvcsd->p_partition_ID,
  810. hvcsd->p_unit_address);
  811. if (!retval) {
  812. hvcsd->connected = 1;
  813. return 0;
  814. } else if (retval != -EINVAL)
  815. return retval;
  816. /*
  817. * As per the spec re-get the pi and try again if -EINVAL after the
  818. * first connection attempt.
  819. */
  820. if (hvcs_get_pi(hvcsd))
  821. return -ENOMEM;
  822. if (!hvcs_has_pi(hvcsd))
  823. return -ENODEV;
  824. retval = hvcs_register_connection(unit_address,
  825. hvcsd->p_partition_ID,
  826. hvcsd->p_unit_address);
  827. if (retval != -EINVAL) {
  828. hvcsd->connected = 1;
  829. return retval;
  830. }
  831. /*
  832. * EBUSY is the most likely scenario though the vty could have been
  833. * removed or there really could be an hcall error due to the parameter
  834. * data but thanks to ambiguous firmware return codes we can't really
  835. * tell.
  836. */
  837. printk(KERN_INFO "HVCS: vty-server or partner"
  838. " vty is busy. Try again later.\n");
  839. return -EBUSY;
  840. }
  841. /* This function must be called with the hvcsd->lock held */
  842. static void hvcs_partner_free(struct hvcs_struct *hvcsd)
  843. {
  844. int retval;
  845. do {
  846. retval = hvcs_free_connection(hvcsd->vdev->unit_address);
  847. } while (retval == -EBUSY);
  848. hvcsd->connected = 0;
  849. }
  850. /* This helper function must be called WITHOUT the hvcsd->lock held */
  851. static int hvcs_enable_device(struct hvcs_struct *hvcsd, uint32_t unit_address,
  852. unsigned int irq, struct vio_dev *vdev)
  853. {
  854. unsigned long flags;
  855. int rc;
  856. /*
  857. * It is possible that the vty-server was removed between the time that
  858. * the conn was registered and now.
  859. */
  860. rc = request_irq(irq, &hvcs_handle_interrupt, 0, "ibmhvcs", hvcsd);
  861. if (!rc) {
  862. /*
  863. * It is possible the vty-server was removed after the irq was
  864. * requested but before we have time to enable interrupts.
  865. */
  866. if (vio_enable_interrupts(vdev) == H_SUCCESS)
  867. return 0;
  868. else {
  869. printk(KERN_ERR "HVCS: int enable failed for"
  870. " vty-server@%X.\n", unit_address);
  871. free_irq(irq, hvcsd);
  872. }
  873. } else
  874. printk(KERN_ERR "HVCS: irq req failed for"
  875. " vty-server@%X.\n", unit_address);
  876. spin_lock_irqsave(&hvcsd->lock, flags);
  877. hvcs_partner_free(hvcsd);
  878. spin_unlock_irqrestore(&hvcsd->lock, flags);
  879. return rc;
  880. }
  881. /*
  882. * This always increments the kref ref count if the call is successful.
  883. * Please remember to dec when you are done with the instance.
  884. *
  885. * NOTICE: Do NOT hold either the hvcs_struct.lock or hvcs_structs_lock when
  886. * calling this function or you will get deadlock.
  887. */
  888. static struct hvcs_struct *hvcs_get_by_index(int index)
  889. {
  890. struct hvcs_struct *hvcsd;
  891. unsigned long flags;
  892. spin_lock(&hvcs_structs_lock);
  893. list_for_each_entry(hvcsd, &hvcs_structs, next) {
  894. spin_lock_irqsave(&hvcsd->lock, flags);
  895. if (hvcsd->index == index) {
  896. tty_port_get(&hvcsd->port);
  897. spin_unlock_irqrestore(&hvcsd->lock, flags);
  898. spin_unlock(&hvcs_structs_lock);
  899. return hvcsd;
  900. }
  901. spin_unlock_irqrestore(&hvcsd->lock, flags);
  902. }
  903. spin_unlock(&hvcs_structs_lock);
  904. return NULL;
  905. }
  906. static int hvcs_install(struct tty_driver *driver, struct tty_struct *tty)
  907. {
  908. struct hvcs_struct *hvcsd;
  909. struct vio_dev *vdev;
  910. unsigned long unit_address, flags;
  911. unsigned int irq;
  912. int retval;
  913. /*
  914. * Is there a vty-server that shares the same index?
  915. * This function increments the kref index.
  916. */
  917. hvcsd = hvcs_get_by_index(tty->index);
  918. if (!hvcsd) {
  919. printk(KERN_WARNING "HVCS: open failed, no device associated"
  920. " with tty->index %d.\n", tty->index);
  921. return -ENODEV;
  922. }
  923. spin_lock_irqsave(&hvcsd->lock, flags);
  924. if (hvcsd->connected == 0) {
  925. retval = hvcs_partner_connect(hvcsd);
  926. if (retval) {
  927. spin_unlock_irqrestore(&hvcsd->lock, flags);
  928. printk(KERN_WARNING "HVCS: partner connect failed.\n");
  929. goto err_put;
  930. }
  931. }
  932. hvcsd->port.count = 0;
  933. hvcsd->port.tty = tty;
  934. tty->driver_data = hvcsd;
  935. memset(&hvcsd->buffer[0], 0x00, HVCS_BUFF_LEN);
  936. /*
  937. * Save these in the spinlock for the enable operations that need them
  938. * outside of the spinlock.
  939. */
  940. irq = hvcsd->vdev->irq;
  941. vdev = hvcsd->vdev;
  942. unit_address = hvcsd->vdev->unit_address;
  943. hvcsd->todo_mask |= HVCS_SCHED_READ;
  944. spin_unlock_irqrestore(&hvcsd->lock, flags);
  945. /*
  946. * This must be done outside of the spinlock because it requests irqs
  947. * and will grab the spinlock and free the connection if it fails.
  948. */
  949. retval = hvcs_enable_device(hvcsd, unit_address, irq, vdev);
  950. if (retval) {
  951. printk(KERN_WARNING "HVCS: enable device failed.\n");
  952. goto err_put;
  953. }
  954. retval = tty_port_install(&hvcsd->port, driver, tty);
  955. if (retval)
  956. goto err_irq;
  957. return 0;
  958. err_irq:
  959. spin_lock_irqsave(&hvcsd->lock, flags);
  960. vio_disable_interrupts(hvcsd->vdev);
  961. spin_unlock_irqrestore(&hvcsd->lock, flags);
  962. free_irq(irq, hvcsd);
  963. err_put:
  964. tty_port_put(&hvcsd->port);
  965. return retval;
  966. }
  967. /*
  968. * This is invoked via the tty_open interface when a user app connects to the
  969. * /dev node.
  970. */
  971. static int hvcs_open(struct tty_struct *tty, struct file *filp)
  972. {
  973. struct hvcs_struct *hvcsd = tty->driver_data;
  974. unsigned long flags;
  975. spin_lock_irqsave(&hvcsd->lock, flags);
  976. hvcsd->port.count++;
  977. hvcsd->todo_mask |= HVCS_SCHED_READ;
  978. spin_unlock_irqrestore(&hvcsd->lock, flags);
  979. hvcs_kick();
  980. printk(KERN_INFO "HVCS: vty-server@%X connection opened.\n",
  981. hvcsd->vdev->unit_address );
  982. return 0;
  983. }
  984. static void hvcs_close(struct tty_struct *tty, struct file *filp)
  985. {
  986. struct hvcs_struct *hvcsd;
  987. unsigned long flags;
  988. int irq;
  989. /*
  990. * Is someone trying to close the file associated with this device after
  991. * we have hung up? If so tty->driver_data wouldn't be valid.
  992. */
  993. if (tty_hung_up_p(filp))
  994. return;
  995. /*
  996. * No driver_data means that this close was probably issued after a
  997. * failed hvcs_open by the tty layer's release_dev() api and we can just
  998. * exit cleanly.
  999. */
  1000. if (!tty->driver_data)
  1001. return;
  1002. hvcsd = tty->driver_data;
  1003. spin_lock_irqsave(&hvcsd->lock, flags);
  1004. if (hvcsd->port.count == 0) {
  1005. spin_unlock_irqrestore(&hvcsd->lock, flags);
  1006. return;
  1007. } else if (--hvcsd->port.count == 0) {
  1008. vio_disable_interrupts(hvcsd->vdev);
  1009. /*
  1010. * NULL this early so that the kernel_thread doesn't try to
  1011. * execute any operations on the TTY even though it is obligated
  1012. * to deliver any pending I/O to the hypervisor.
  1013. */
  1014. hvcsd->port.tty = NULL;
  1015. irq = hvcsd->vdev->irq;
  1016. spin_unlock_irqrestore(&hvcsd->lock, flags);
  1017. tty_wait_until_sent(tty, HVCS_CLOSE_WAIT);
  1018. free_irq(irq, hvcsd);
  1019. return;
  1020. } else if (hvcsd->port.count < 0) {
  1021. printk(KERN_ERR "HVCS: vty-server@%X open_count: %d is mismanaged.\n",
  1022. hvcsd->vdev->unit_address, hvcsd->port.count);
  1023. }
  1024. spin_unlock_irqrestore(&hvcsd->lock, flags);
  1025. }
  1026. static void hvcs_cleanup(struct tty_struct * tty)
  1027. {
  1028. struct hvcs_struct *hvcsd = tty->driver_data;
  1029. /*
  1030. * This line is important because it tells hvcs_open that this
  1031. * device needs to be re-configured the next time hvcs_open is
  1032. * called.
  1033. */
  1034. tty->driver_data = NULL;
  1035. tty_port_put(&hvcsd->port);
  1036. }
  1037. static void hvcs_hangup(struct tty_struct * tty)
  1038. {
  1039. struct hvcs_struct *hvcsd = tty->driver_data;
  1040. unsigned long flags;
  1041. int irq;
  1042. spin_lock_irqsave(&hvcsd->lock, flags);
  1043. /*
  1044. * Don't kref put inside the spinlock because the destruction
  1045. * callback may use the spinlock and it may get called before the
  1046. * spinlock has been released.
  1047. */
  1048. vio_disable_interrupts(hvcsd->vdev);
  1049. hvcsd->todo_mask = 0;
  1050. hvcsd->port.tty = NULL;
  1051. hvcsd->port.count = 0;
  1052. /* This will drop any buffered data on the floor which is OK in a hangup
  1053. * scenario. */
  1054. memset(&hvcsd->buffer[0], 0x00, HVCS_BUFF_LEN);
  1055. hvcsd->chars_in_buffer = 0;
  1056. irq = hvcsd->vdev->irq;
  1057. spin_unlock_irqrestore(&hvcsd->lock, flags);
  1058. free_irq(irq, hvcsd);
  1059. }
  1060. /*
  1061. * NOTE: This is almost always from_user since user level apps interact with the
  1062. * /dev nodes. I'm trusting that if hvcs_write gets called and interrupted by
  1063. * hvcs_remove (which removes the target device and executes tty_hangup()) that
  1064. * tty_hangup will allow hvcs_write time to complete execution before it
  1065. * terminates our device.
  1066. */
  1067. static ssize_t hvcs_write(struct tty_struct *tty, const u8 *buf, size_t count)
  1068. {
  1069. struct hvcs_struct *hvcsd = tty->driver_data;
  1070. unsigned int unit_address;
  1071. const unsigned char *charbuf;
  1072. unsigned long flags;
  1073. size_t total_sent = 0;
  1074. size_t tosend = 0;
  1075. int result = 0;
  1076. /*
  1077. * If they don't check the return code off of their open they may
  1078. * attempt this even if there is no connected device.
  1079. */
  1080. if (!hvcsd)
  1081. return -ENODEV;
  1082. /* Reasonable size to prevent user level flooding */
  1083. if (count > HVCS_MAX_FROM_USER) {
  1084. printk(KERN_WARNING "HVCS write: count being truncated to"
  1085. " HVCS_MAX_FROM_USER.\n");
  1086. count = HVCS_MAX_FROM_USER;
  1087. }
  1088. charbuf = buf;
  1089. spin_lock_irqsave(&hvcsd->lock, flags);
  1090. /*
  1091. * Somehow an open succeeded but the device was removed or the
  1092. * connection terminated between the vty-server and partner vty during
  1093. * the middle of a write operation? This is a crummy place to do this
  1094. * but we want to keep it all in the spinlock.
  1095. */
  1096. if (hvcsd->port.count <= 0) {
  1097. spin_unlock_irqrestore(&hvcsd->lock, flags);
  1098. return -ENODEV;
  1099. }
  1100. unit_address = hvcsd->vdev->unit_address;
  1101. while (count > 0) {
  1102. tosend = min_t(size_t, count,
  1103. (HVCS_BUFF_LEN - hvcsd->chars_in_buffer));
  1104. /*
  1105. * No more space, this probably means that the last call to
  1106. * hvcs_write() didn't succeed and the buffer was filled up.
  1107. */
  1108. if (!tosend)
  1109. break;
  1110. memcpy(&hvcsd->buffer[hvcsd->chars_in_buffer],
  1111. &charbuf[total_sent],
  1112. tosend);
  1113. hvcsd->chars_in_buffer += tosend;
  1114. result = 0;
  1115. /*
  1116. * If this is true then we don't want to try writing to the
  1117. * hypervisor because that is the kernel_threads job now. We'll
  1118. * just add to the buffer.
  1119. */
  1120. if (!(hvcsd->todo_mask & HVCS_TRY_WRITE))
  1121. /* won't send partial writes */
  1122. result = hvc_put_chars(unit_address,
  1123. &hvcsd->buffer[0],
  1124. hvcsd->chars_in_buffer);
  1125. /*
  1126. * Since we know we have enough room in hvcsd->buffer for
  1127. * tosend we record that it was sent regardless of whether the
  1128. * hypervisor actually took it because we have it buffered.
  1129. */
  1130. total_sent+=tosend;
  1131. count-=tosend;
  1132. if (result == 0) {
  1133. hvcsd->todo_mask |= HVCS_TRY_WRITE;
  1134. hvcs_kick();
  1135. break;
  1136. }
  1137. hvcsd->chars_in_buffer = 0;
  1138. /*
  1139. * Test after the chars_in_buffer reset otherwise this could
  1140. * deadlock our writes if hvc_put_chars fails.
  1141. */
  1142. if (result < 0)
  1143. break;
  1144. }
  1145. spin_unlock_irqrestore(&hvcsd->lock, flags);
  1146. if (result == -1)
  1147. return -EIO;
  1148. else
  1149. return total_sent;
  1150. }
  1151. /*
  1152. * This is really asking how much can we guarantee that we can send or that we
  1153. * absolutely WILL BUFFER if we can't send it. This driver MUST honor the
  1154. * return value, hence the reason for hvcs_struct buffering.
  1155. */
  1156. static unsigned int hvcs_write_room(struct tty_struct *tty)
  1157. {
  1158. struct hvcs_struct *hvcsd = tty->driver_data;
  1159. if (!hvcsd || hvcsd->port.count <= 0)
  1160. return 0;
  1161. return HVCS_BUFF_LEN - hvcsd->chars_in_buffer;
  1162. }
  1163. static unsigned int hvcs_chars_in_buffer(struct tty_struct *tty)
  1164. {
  1165. struct hvcs_struct *hvcsd = tty->driver_data;
  1166. return hvcsd->chars_in_buffer;
  1167. }
  1168. static const struct tty_operations hvcs_ops = {
  1169. .install = hvcs_install,
  1170. .open = hvcs_open,
  1171. .close = hvcs_close,
  1172. .cleanup = hvcs_cleanup,
  1173. .hangup = hvcs_hangup,
  1174. .write = hvcs_write,
  1175. .write_room = hvcs_write_room,
  1176. .chars_in_buffer = hvcs_chars_in_buffer,
  1177. .unthrottle = hvcs_unthrottle,
  1178. .throttle = hvcs_throttle,
  1179. };
  1180. static int hvcs_alloc_index_list(int n)
  1181. {
  1182. int i;
  1183. hvcs_index_list = kmalloc_array(n, sizeof(hvcs_index_count),
  1184. GFP_KERNEL);
  1185. if (!hvcs_index_list)
  1186. return -ENOMEM;
  1187. hvcs_index_count = n;
  1188. for (i = 0; i < hvcs_index_count; i++)
  1189. hvcs_index_list[i] = -1;
  1190. return 0;
  1191. }
  1192. static void hvcs_free_index_list(void)
  1193. {
  1194. /* Paranoia check to be thorough. */
  1195. kfree(hvcs_index_list);
  1196. hvcs_index_list = NULL;
  1197. hvcs_index_count = 0;
  1198. }
  1199. static int hvcs_initialize(void)
  1200. {
  1201. int rc, num_ttys_to_alloc;
  1202. mutex_lock(&hvcs_init_mutex);
  1203. if (hvcs_task) {
  1204. mutex_unlock(&hvcs_init_mutex);
  1205. return 0;
  1206. }
  1207. /* Has the user specified an overload with an insmod param? */
  1208. if (hvcs_parm_num_devs <= 0 ||
  1209. (hvcs_parm_num_devs > HVCS_MAX_SERVER_ADAPTERS)) {
  1210. num_ttys_to_alloc = HVCS_DEFAULT_SERVER_ADAPTERS;
  1211. } else
  1212. num_ttys_to_alloc = hvcs_parm_num_devs;
  1213. hvcs_tty_driver = tty_alloc_driver(num_ttys_to_alloc,
  1214. TTY_DRIVER_REAL_RAW);
  1215. if (IS_ERR(hvcs_tty_driver)) {
  1216. mutex_unlock(&hvcs_init_mutex);
  1217. return PTR_ERR(hvcs_tty_driver);
  1218. }
  1219. if (hvcs_alloc_index_list(num_ttys_to_alloc)) {
  1220. rc = -ENOMEM;
  1221. goto index_fail;
  1222. }
  1223. hvcs_tty_driver->driver_name = hvcs_driver_name;
  1224. hvcs_tty_driver->name = hvcs_device_node;
  1225. /*
  1226. * We'll let the system assign us a major number, indicated by leaving
  1227. * it blank.
  1228. */
  1229. hvcs_tty_driver->minor_start = HVCS_MINOR_START;
  1230. hvcs_tty_driver->type = TTY_DRIVER_TYPE_SYSTEM;
  1231. /*
  1232. * We role our own so that we DONT ECHO. We can't echo because the
  1233. * device we are connecting to already echoes by default and this would
  1234. * throw us into a horrible recursive echo-echo-echo loop.
  1235. */
  1236. hvcs_tty_driver->init_termios = hvcs_tty_termios;
  1237. tty_set_operations(hvcs_tty_driver, &hvcs_ops);
  1238. /*
  1239. * The following call will result in sysfs entries that denote the
  1240. * dynamically assigned major and minor numbers for our devices.
  1241. */
  1242. if (tty_register_driver(hvcs_tty_driver)) {
  1243. printk(KERN_ERR "HVCS: registration as a tty driver failed.\n");
  1244. rc = -EIO;
  1245. goto register_fail;
  1246. }
  1247. hvcs_pi_buff = (unsigned long *) __get_free_page(GFP_KERNEL);
  1248. if (!hvcs_pi_buff) {
  1249. rc = -ENOMEM;
  1250. goto buff_alloc_fail;
  1251. }
  1252. hvcs_task = kthread_run(khvcsd, NULL, "khvcsd");
  1253. if (IS_ERR(hvcs_task)) {
  1254. printk(KERN_ERR "HVCS: khvcsd creation failed.\n");
  1255. rc = -EIO;
  1256. goto kthread_fail;
  1257. }
  1258. mutex_unlock(&hvcs_init_mutex);
  1259. return 0;
  1260. kthread_fail:
  1261. free_page((unsigned long)hvcs_pi_buff);
  1262. buff_alloc_fail:
  1263. tty_unregister_driver(hvcs_tty_driver);
  1264. register_fail:
  1265. hvcs_free_index_list();
  1266. index_fail:
  1267. tty_driver_kref_put(hvcs_tty_driver);
  1268. hvcs_tty_driver = NULL;
  1269. mutex_unlock(&hvcs_init_mutex);
  1270. return rc;
  1271. }
  1272. static int __init hvcs_module_init(void)
  1273. {
  1274. int rc = vio_register_driver(&hvcs_vio_driver);
  1275. if (rc) {
  1276. printk(KERN_ERR "HVCS: can't register vio driver\n");
  1277. return rc;
  1278. }
  1279. pr_info("HVCS: Driver registered.\n");
  1280. return 0;
  1281. }
  1282. static void __exit hvcs_module_exit(void)
  1283. {
  1284. /*
  1285. * This driver receives hvcs_remove callbacks for each device upon
  1286. * module removal.
  1287. */
  1288. vio_unregister_driver(&hvcs_vio_driver);
  1289. if (!hvcs_task)
  1290. return;
  1291. /*
  1292. * This synchronous operation will wake the khvcsd kthread if it is
  1293. * asleep and will return when khvcsd has terminated.
  1294. */
  1295. kthread_stop(hvcs_task);
  1296. spin_lock(&hvcs_pi_lock);
  1297. free_page((unsigned long)hvcs_pi_buff);
  1298. hvcs_pi_buff = NULL;
  1299. spin_unlock(&hvcs_pi_lock);
  1300. tty_unregister_driver(hvcs_tty_driver);
  1301. hvcs_free_index_list();
  1302. tty_driver_kref_put(hvcs_tty_driver);
  1303. printk(KERN_INFO "HVCS: driver module removed.\n");
  1304. }
  1305. module_init(hvcs_module_init);
  1306. module_exit(hvcs_module_exit);