hvcs.c 44 KB

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