hvcs.c 46 KB

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