hvcs.c 45 KB

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