hvcs.c 47 KB

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