iseries_veth.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727
  1. /* File veth.c created by Kyle A. Lucke on Mon Aug 7 2000. */
  2. /*
  3. * IBM eServer iSeries Virtual Ethernet Device Driver
  4. * Copyright (C) 2001 Kyle A. Lucke (klucke@us.ibm.com), IBM Corp.
  5. * Substantially cleaned up by:
  6. * Copyright (C) 2003 David Gibson <dwg@au1.ibm.com>, IBM Corporation.
  7. * Copyright (C) 2004-2005 Michael Ellerman, IBM Corporation.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  22. * USA
  23. *
  24. *
  25. * This module implements the virtual ethernet device for iSeries LPAR
  26. * Linux. It uses hypervisor message passing to implement an
  27. * ethernet-like network device communicating between partitions on
  28. * the iSeries.
  29. *
  30. * The iSeries LPAR hypervisor currently allows for up to 16 different
  31. * virtual ethernets. These are all dynamically configurable on
  32. * OS/400 partitions, but dynamic configuration is not supported under
  33. * Linux yet. An ethXX network device will be created for each
  34. * virtual ethernet this partition is connected to.
  35. *
  36. * - This driver is responsible for routing packets to and from other
  37. * partitions. The MAC addresses used by the virtual ethernets
  38. * contains meaning and must not be modified.
  39. *
  40. * - Having 2 virtual ethernets to the same remote partition DOES NOT
  41. * double the available bandwidth. The 2 devices will share the
  42. * available hypervisor bandwidth.
  43. *
  44. * - If you send a packet to your own mac address, it will just be
  45. * dropped, you won't get it on the receive side.
  46. *
  47. * - Multicast is implemented by sending the frame frame to every
  48. * other partition. It is the responsibility of the receiving
  49. * partition to filter the addresses desired.
  50. *
  51. * Tunable parameters:
  52. *
  53. * VETH_NUMBUFFERS: This compile time option defaults to 120. It
  54. * controls how much memory Linux will allocate per remote partition
  55. * it is communicating with. It can be thought of as the maximum
  56. * number of packets outstanding to a remote partition at a time.
  57. */
  58. #include <linux/module.h>
  59. #include <linux/types.h>
  60. #include <linux/errno.h>
  61. #include <linux/ioport.h>
  62. #include <linux/kernel.h>
  63. #include <linux/netdevice.h>
  64. #include <linux/etherdevice.h>
  65. #include <linux/skbuff.h>
  66. #include <linux/init.h>
  67. #include <linux/delay.h>
  68. #include <linux/mm.h>
  69. #include <linux/ethtool.h>
  70. #include <linux/if_ether.h>
  71. #include <linux/slab.h>
  72. #include <asm/abs_addr.h>
  73. #include <asm/iseries/mf.h>
  74. #include <asm/uaccess.h>
  75. #include <asm/firmware.h>
  76. #include <asm/iseries/hv_lp_config.h>
  77. #include <asm/iseries/hv_types.h>
  78. #include <asm/iseries/hv_lp_event.h>
  79. #include <asm/iommu.h>
  80. #include <asm/vio.h>
  81. #undef DEBUG
  82. MODULE_AUTHOR("Kyle Lucke <klucke@us.ibm.com>");
  83. MODULE_DESCRIPTION("iSeries Virtual ethernet driver");
  84. MODULE_LICENSE("GPL");
  85. #define VETH_EVENT_CAP (0)
  86. #define VETH_EVENT_FRAMES (1)
  87. #define VETH_EVENT_MONITOR (2)
  88. #define VETH_EVENT_FRAMES_ACK (3)
  89. #define VETH_MAX_ACKS_PER_MSG (20)
  90. #define VETH_MAX_FRAMES_PER_MSG (6)
  91. struct veth_frames_data {
  92. u32 addr[VETH_MAX_FRAMES_PER_MSG];
  93. u16 len[VETH_MAX_FRAMES_PER_MSG];
  94. u32 eofmask;
  95. };
  96. #define VETH_EOF_SHIFT (32-VETH_MAX_FRAMES_PER_MSG)
  97. struct veth_frames_ack_data {
  98. u16 token[VETH_MAX_ACKS_PER_MSG];
  99. };
  100. struct veth_cap_data {
  101. u8 caps_version;
  102. u8 rsvd1;
  103. u16 num_buffers;
  104. u16 ack_threshold;
  105. u16 rsvd2;
  106. u32 ack_timeout;
  107. u32 rsvd3;
  108. u64 rsvd4[3];
  109. };
  110. struct veth_lpevent {
  111. struct HvLpEvent base_event;
  112. union {
  113. struct veth_cap_data caps_data;
  114. struct veth_frames_data frames_data;
  115. struct veth_frames_ack_data frames_ack_data;
  116. } u;
  117. };
  118. #define DRV_NAME "iseries_veth"
  119. #define DRV_VERSION "2.0"
  120. #define VETH_NUMBUFFERS (120)
  121. #define VETH_ACKTIMEOUT (1000000) /* microseconds */
  122. #define VETH_MAX_MCAST (12)
  123. #define VETH_MAX_MTU (9000)
  124. #if VETH_NUMBUFFERS < 10
  125. #define ACK_THRESHOLD (1)
  126. #elif VETH_NUMBUFFERS < 20
  127. #define ACK_THRESHOLD (4)
  128. #elif VETH_NUMBUFFERS < 40
  129. #define ACK_THRESHOLD (10)
  130. #else
  131. #define ACK_THRESHOLD (20)
  132. #endif
  133. #define VETH_STATE_SHUTDOWN (0x0001)
  134. #define VETH_STATE_OPEN (0x0002)
  135. #define VETH_STATE_RESET (0x0004)
  136. #define VETH_STATE_SENTMON (0x0008)
  137. #define VETH_STATE_SENTCAPS (0x0010)
  138. #define VETH_STATE_GOTCAPACK (0x0020)
  139. #define VETH_STATE_GOTCAPS (0x0040)
  140. #define VETH_STATE_SENTCAPACK (0x0080)
  141. #define VETH_STATE_READY (0x0100)
  142. struct veth_msg {
  143. struct veth_msg *next;
  144. struct veth_frames_data data;
  145. int token;
  146. int in_use;
  147. struct sk_buff *skb;
  148. struct device *dev;
  149. };
  150. struct veth_lpar_connection {
  151. HvLpIndex remote_lp;
  152. struct delayed_work statemachine_wq;
  153. struct veth_msg *msgs;
  154. int num_events;
  155. struct veth_cap_data local_caps;
  156. struct kobject kobject;
  157. struct timer_list ack_timer;
  158. struct timer_list reset_timer;
  159. unsigned int reset_timeout;
  160. unsigned long last_contact;
  161. int outstanding_tx;
  162. spinlock_t lock;
  163. unsigned long state;
  164. HvLpInstanceId src_inst;
  165. HvLpInstanceId dst_inst;
  166. struct veth_lpevent cap_event, cap_ack_event;
  167. u16 pending_acks[VETH_MAX_ACKS_PER_MSG];
  168. u32 num_pending_acks;
  169. int num_ack_events;
  170. struct veth_cap_data remote_caps;
  171. u32 ack_timeout;
  172. struct veth_msg *msg_stack_head;
  173. };
  174. struct veth_port {
  175. struct device *dev;
  176. u64 mac_addr;
  177. HvLpIndexMap lpar_map;
  178. /* queue_lock protects the stopped_map and dev's queue. */
  179. spinlock_t queue_lock;
  180. HvLpIndexMap stopped_map;
  181. /* mcast_gate protects promiscuous, num_mcast & mcast_addr. */
  182. rwlock_t mcast_gate;
  183. int promiscuous;
  184. int num_mcast;
  185. u64 mcast_addr[VETH_MAX_MCAST];
  186. struct kobject kobject;
  187. };
  188. static HvLpIndex this_lp;
  189. static struct veth_lpar_connection *veth_cnx[HVMAXARCHITECTEDLPS]; /* = 0 */
  190. static struct net_device *veth_dev[HVMAXARCHITECTEDVIRTUALLANS]; /* = 0 */
  191. static int veth_start_xmit(struct sk_buff *skb, struct net_device *dev);
  192. static void veth_recycle_msg(struct veth_lpar_connection *, struct veth_msg *);
  193. static void veth_wake_queues(struct veth_lpar_connection *cnx);
  194. static void veth_stop_queues(struct veth_lpar_connection *cnx);
  195. static void veth_receive(struct veth_lpar_connection *, struct veth_lpevent *);
  196. static void veth_release_connection(struct kobject *kobject);
  197. static void veth_timed_ack(unsigned long ptr);
  198. static void veth_timed_reset(unsigned long ptr);
  199. /*
  200. * Utility functions
  201. */
  202. #define veth_info(fmt, args...) \
  203. printk(KERN_INFO DRV_NAME ": " fmt, ## args)
  204. #define veth_error(fmt, args...) \
  205. printk(KERN_ERR DRV_NAME ": Error: " fmt, ## args)
  206. #ifdef DEBUG
  207. #define veth_debug(fmt, args...) \
  208. printk(KERN_DEBUG DRV_NAME ": " fmt, ## args)
  209. #else
  210. #define veth_debug(fmt, args...) do {} while (0)
  211. #endif
  212. /* You must hold the connection's lock when you call this function. */
  213. static inline void veth_stack_push(struct veth_lpar_connection *cnx,
  214. struct veth_msg *msg)
  215. {
  216. msg->next = cnx->msg_stack_head;
  217. cnx->msg_stack_head = msg;
  218. }
  219. /* You must hold the connection's lock when you call this function. */
  220. static inline struct veth_msg *veth_stack_pop(struct veth_lpar_connection *cnx)
  221. {
  222. struct veth_msg *msg;
  223. msg = cnx->msg_stack_head;
  224. if (msg)
  225. cnx->msg_stack_head = cnx->msg_stack_head->next;
  226. return msg;
  227. }
  228. /* You must hold the connection's lock when you call this function. */
  229. static inline int veth_stack_is_empty(struct veth_lpar_connection *cnx)
  230. {
  231. return cnx->msg_stack_head == NULL;
  232. }
  233. static inline HvLpEvent_Rc
  234. veth_signalevent(struct veth_lpar_connection *cnx, u16 subtype,
  235. HvLpEvent_AckInd ackind, HvLpEvent_AckType acktype,
  236. u64 token,
  237. u64 data1, u64 data2, u64 data3, u64 data4, u64 data5)
  238. {
  239. return HvCallEvent_signalLpEventFast(cnx->remote_lp,
  240. HvLpEvent_Type_VirtualLan,
  241. subtype, ackind, acktype,
  242. cnx->src_inst,
  243. cnx->dst_inst,
  244. token, data1, data2, data3,
  245. data4, data5);
  246. }
  247. static inline HvLpEvent_Rc veth_signaldata(struct veth_lpar_connection *cnx,
  248. u16 subtype, u64 token, void *data)
  249. {
  250. u64 *p = (u64 *) data;
  251. return veth_signalevent(cnx, subtype, HvLpEvent_AckInd_NoAck,
  252. HvLpEvent_AckType_ImmediateAck,
  253. token, p[0], p[1], p[2], p[3], p[4]);
  254. }
  255. struct veth_allocation {
  256. struct completion c;
  257. int num;
  258. };
  259. static void veth_complete_allocation(void *parm, int number)
  260. {
  261. struct veth_allocation *vc = (struct veth_allocation *)parm;
  262. vc->num = number;
  263. complete(&vc->c);
  264. }
  265. static int veth_allocate_events(HvLpIndex rlp, int number)
  266. {
  267. struct veth_allocation vc =
  268. { COMPLETION_INITIALIZER_ONSTACK(vc.c), 0 };
  269. mf_allocate_lp_events(rlp, HvLpEvent_Type_VirtualLan,
  270. sizeof(struct veth_lpevent), number,
  271. &veth_complete_allocation, &vc);
  272. wait_for_completion(&vc.c);
  273. return vc.num;
  274. }
  275. /*
  276. * sysfs support
  277. */
  278. struct veth_cnx_attribute {
  279. struct attribute attr;
  280. ssize_t (*show)(struct veth_lpar_connection *, char *buf);
  281. ssize_t (*store)(struct veth_lpar_connection *, const char *buf);
  282. };
  283. static ssize_t veth_cnx_attribute_show(struct kobject *kobj,
  284. struct attribute *attr, char *buf)
  285. {
  286. struct veth_cnx_attribute *cnx_attr;
  287. struct veth_lpar_connection *cnx;
  288. cnx_attr = container_of(attr, struct veth_cnx_attribute, attr);
  289. cnx = container_of(kobj, struct veth_lpar_connection, kobject);
  290. if (!cnx_attr->show)
  291. return -EIO;
  292. return cnx_attr->show(cnx, buf);
  293. }
  294. #define CUSTOM_CNX_ATTR(_name, _format, _expression) \
  295. static ssize_t _name##_show(struct veth_lpar_connection *cnx, char *buf)\
  296. { \
  297. return sprintf(buf, _format, _expression); \
  298. } \
  299. struct veth_cnx_attribute veth_cnx_attr_##_name = __ATTR_RO(_name)
  300. #define SIMPLE_CNX_ATTR(_name) \
  301. CUSTOM_CNX_ATTR(_name, "%lu\n", (unsigned long)cnx->_name)
  302. SIMPLE_CNX_ATTR(outstanding_tx);
  303. SIMPLE_CNX_ATTR(remote_lp);
  304. SIMPLE_CNX_ATTR(num_events);
  305. SIMPLE_CNX_ATTR(src_inst);
  306. SIMPLE_CNX_ATTR(dst_inst);
  307. SIMPLE_CNX_ATTR(num_pending_acks);
  308. SIMPLE_CNX_ATTR(num_ack_events);
  309. CUSTOM_CNX_ATTR(ack_timeout, "%d\n", jiffies_to_msecs(cnx->ack_timeout));
  310. CUSTOM_CNX_ATTR(reset_timeout, "%d\n", jiffies_to_msecs(cnx->reset_timeout));
  311. CUSTOM_CNX_ATTR(state, "0x%.4lX\n", cnx->state);
  312. CUSTOM_CNX_ATTR(last_contact, "%d\n", cnx->last_contact ?
  313. jiffies_to_msecs(jiffies - cnx->last_contact) : 0);
  314. #define GET_CNX_ATTR(_name) (&veth_cnx_attr_##_name.attr)
  315. static struct attribute *veth_cnx_default_attrs[] = {
  316. GET_CNX_ATTR(outstanding_tx),
  317. GET_CNX_ATTR(remote_lp),
  318. GET_CNX_ATTR(num_events),
  319. GET_CNX_ATTR(reset_timeout),
  320. GET_CNX_ATTR(last_contact),
  321. GET_CNX_ATTR(state),
  322. GET_CNX_ATTR(src_inst),
  323. GET_CNX_ATTR(dst_inst),
  324. GET_CNX_ATTR(num_pending_acks),
  325. GET_CNX_ATTR(num_ack_events),
  326. GET_CNX_ATTR(ack_timeout),
  327. NULL
  328. };
  329. static const struct sysfs_ops veth_cnx_sysfs_ops = {
  330. .show = veth_cnx_attribute_show
  331. };
  332. static struct kobj_type veth_lpar_connection_ktype = {
  333. .release = veth_release_connection,
  334. .sysfs_ops = &veth_cnx_sysfs_ops,
  335. .default_attrs = veth_cnx_default_attrs
  336. };
  337. struct veth_port_attribute {
  338. struct attribute attr;
  339. ssize_t (*show)(struct veth_port *, char *buf);
  340. ssize_t (*store)(struct veth_port *, const char *buf);
  341. };
  342. static ssize_t veth_port_attribute_show(struct kobject *kobj,
  343. struct attribute *attr, char *buf)
  344. {
  345. struct veth_port_attribute *port_attr;
  346. struct veth_port *port;
  347. port_attr = container_of(attr, struct veth_port_attribute, attr);
  348. port = container_of(kobj, struct veth_port, kobject);
  349. if (!port_attr->show)
  350. return -EIO;
  351. return port_attr->show(port, buf);
  352. }
  353. #define CUSTOM_PORT_ATTR(_name, _format, _expression) \
  354. static ssize_t _name##_show(struct veth_port *port, char *buf) \
  355. { \
  356. return sprintf(buf, _format, _expression); \
  357. } \
  358. struct veth_port_attribute veth_port_attr_##_name = __ATTR_RO(_name)
  359. #define SIMPLE_PORT_ATTR(_name) \
  360. CUSTOM_PORT_ATTR(_name, "%lu\n", (unsigned long)port->_name)
  361. SIMPLE_PORT_ATTR(promiscuous);
  362. SIMPLE_PORT_ATTR(num_mcast);
  363. CUSTOM_PORT_ATTR(lpar_map, "0x%X\n", port->lpar_map);
  364. CUSTOM_PORT_ATTR(stopped_map, "0x%X\n", port->stopped_map);
  365. CUSTOM_PORT_ATTR(mac_addr, "0x%llX\n", port->mac_addr);
  366. #define GET_PORT_ATTR(_name) (&veth_port_attr_##_name.attr)
  367. static struct attribute *veth_port_default_attrs[] = {
  368. GET_PORT_ATTR(mac_addr),
  369. GET_PORT_ATTR(lpar_map),
  370. GET_PORT_ATTR(stopped_map),
  371. GET_PORT_ATTR(promiscuous),
  372. GET_PORT_ATTR(num_mcast),
  373. NULL
  374. };
  375. static const struct sysfs_ops veth_port_sysfs_ops = {
  376. .show = veth_port_attribute_show
  377. };
  378. static struct kobj_type veth_port_ktype = {
  379. .sysfs_ops = &veth_port_sysfs_ops,
  380. .default_attrs = veth_port_default_attrs
  381. };
  382. /*
  383. * LPAR connection code
  384. */
  385. static inline void veth_kick_statemachine(struct veth_lpar_connection *cnx)
  386. {
  387. schedule_delayed_work(&cnx->statemachine_wq, 0);
  388. }
  389. static void veth_take_cap(struct veth_lpar_connection *cnx,
  390. struct veth_lpevent *event)
  391. {
  392. unsigned long flags;
  393. spin_lock_irqsave(&cnx->lock, flags);
  394. /* Receiving caps may mean the other end has just come up, so
  395. * we need to reload the instance ID of the far end */
  396. cnx->dst_inst =
  397. HvCallEvent_getTargetLpInstanceId(cnx->remote_lp,
  398. HvLpEvent_Type_VirtualLan);
  399. if (cnx->state & VETH_STATE_GOTCAPS) {
  400. veth_error("Received a second capabilities from LPAR %d.\n",
  401. cnx->remote_lp);
  402. event->base_event.xRc = HvLpEvent_Rc_BufferNotAvailable;
  403. HvCallEvent_ackLpEvent((struct HvLpEvent *) event);
  404. } else {
  405. memcpy(&cnx->cap_event, event, sizeof(cnx->cap_event));
  406. cnx->state |= VETH_STATE_GOTCAPS;
  407. veth_kick_statemachine(cnx);
  408. }
  409. spin_unlock_irqrestore(&cnx->lock, flags);
  410. }
  411. static void veth_take_cap_ack(struct veth_lpar_connection *cnx,
  412. struct veth_lpevent *event)
  413. {
  414. unsigned long flags;
  415. spin_lock_irqsave(&cnx->lock, flags);
  416. if (cnx->state & VETH_STATE_GOTCAPACK) {
  417. veth_error("Received a second capabilities ack from LPAR %d.\n",
  418. cnx->remote_lp);
  419. } else {
  420. memcpy(&cnx->cap_ack_event, event,
  421. sizeof(cnx->cap_ack_event));
  422. cnx->state |= VETH_STATE_GOTCAPACK;
  423. veth_kick_statemachine(cnx);
  424. }
  425. spin_unlock_irqrestore(&cnx->lock, flags);
  426. }
  427. static void veth_take_monitor_ack(struct veth_lpar_connection *cnx,
  428. struct veth_lpevent *event)
  429. {
  430. unsigned long flags;
  431. spin_lock_irqsave(&cnx->lock, flags);
  432. veth_debug("cnx %d: lost connection.\n", cnx->remote_lp);
  433. /* Avoid kicking the statemachine once we're shutdown.
  434. * It's unnecessary and it could break veth_stop_connection(). */
  435. if (! (cnx->state & VETH_STATE_SHUTDOWN)) {
  436. cnx->state |= VETH_STATE_RESET;
  437. veth_kick_statemachine(cnx);
  438. }
  439. spin_unlock_irqrestore(&cnx->lock, flags);
  440. }
  441. static void veth_handle_ack(struct veth_lpevent *event)
  442. {
  443. HvLpIndex rlp = event->base_event.xTargetLp;
  444. struct veth_lpar_connection *cnx = veth_cnx[rlp];
  445. BUG_ON(! cnx);
  446. switch (event->base_event.xSubtype) {
  447. case VETH_EVENT_CAP:
  448. veth_take_cap_ack(cnx, event);
  449. break;
  450. case VETH_EVENT_MONITOR:
  451. veth_take_monitor_ack(cnx, event);
  452. break;
  453. default:
  454. veth_error("Unknown ack type %d from LPAR %d.\n",
  455. event->base_event.xSubtype, rlp);
  456. };
  457. }
  458. static void veth_handle_int(struct veth_lpevent *event)
  459. {
  460. HvLpIndex rlp = event->base_event.xSourceLp;
  461. struct veth_lpar_connection *cnx = veth_cnx[rlp];
  462. unsigned long flags;
  463. int i, acked = 0;
  464. BUG_ON(! cnx);
  465. switch (event->base_event.xSubtype) {
  466. case VETH_EVENT_CAP:
  467. veth_take_cap(cnx, event);
  468. break;
  469. case VETH_EVENT_MONITOR:
  470. /* do nothing... this'll hang out here til we're dead,
  471. * and the hypervisor will return it for us. */
  472. break;
  473. case VETH_EVENT_FRAMES_ACK:
  474. spin_lock_irqsave(&cnx->lock, flags);
  475. for (i = 0; i < VETH_MAX_ACKS_PER_MSG; ++i) {
  476. u16 msgnum = event->u.frames_ack_data.token[i];
  477. if (msgnum < VETH_NUMBUFFERS) {
  478. veth_recycle_msg(cnx, cnx->msgs + msgnum);
  479. cnx->outstanding_tx--;
  480. acked++;
  481. }
  482. }
  483. if (acked > 0) {
  484. cnx->last_contact = jiffies;
  485. veth_wake_queues(cnx);
  486. }
  487. spin_unlock_irqrestore(&cnx->lock, flags);
  488. break;
  489. case VETH_EVENT_FRAMES:
  490. veth_receive(cnx, event);
  491. break;
  492. default:
  493. veth_error("Unknown interrupt type %d from LPAR %d.\n",
  494. event->base_event.xSubtype, rlp);
  495. };
  496. }
  497. static void veth_handle_event(struct HvLpEvent *event)
  498. {
  499. struct veth_lpevent *veth_event = (struct veth_lpevent *)event;
  500. if (hvlpevent_is_ack(event))
  501. veth_handle_ack(veth_event);
  502. else
  503. veth_handle_int(veth_event);
  504. }
  505. static int veth_process_caps(struct veth_lpar_connection *cnx)
  506. {
  507. struct veth_cap_data *remote_caps = &cnx->remote_caps;
  508. int num_acks_needed;
  509. /* Convert timer to jiffies */
  510. cnx->ack_timeout = remote_caps->ack_timeout * HZ / 1000000;
  511. if ( (remote_caps->num_buffers == 0) ||
  512. (remote_caps->ack_threshold > VETH_MAX_ACKS_PER_MSG) ||
  513. (remote_caps->ack_threshold == 0) ||
  514. (cnx->ack_timeout == 0) ) {
  515. veth_error("Received incompatible capabilities from LPAR %d.\n",
  516. cnx->remote_lp);
  517. return HvLpEvent_Rc_InvalidSubtypeData;
  518. }
  519. num_acks_needed = (remote_caps->num_buffers
  520. / remote_caps->ack_threshold) + 1;
  521. /* FIXME: locking on num_ack_events? */
  522. if (cnx->num_ack_events < num_acks_needed) {
  523. int num;
  524. num = veth_allocate_events(cnx->remote_lp,
  525. num_acks_needed-cnx->num_ack_events);
  526. if (num > 0)
  527. cnx->num_ack_events += num;
  528. if (cnx->num_ack_events < num_acks_needed) {
  529. veth_error("Couldn't allocate enough ack events "
  530. "for LPAR %d.\n", cnx->remote_lp);
  531. return HvLpEvent_Rc_BufferNotAvailable;
  532. }
  533. }
  534. return HvLpEvent_Rc_Good;
  535. }
  536. /* FIXME: The gotos here are a bit dubious */
  537. static void veth_statemachine(struct work_struct *work)
  538. {
  539. struct veth_lpar_connection *cnx =
  540. container_of(work, struct veth_lpar_connection,
  541. statemachine_wq.work);
  542. int rlp = cnx->remote_lp;
  543. int rc;
  544. spin_lock_irq(&cnx->lock);
  545. restart:
  546. if (cnx->state & VETH_STATE_RESET) {
  547. if (cnx->state & VETH_STATE_OPEN)
  548. HvCallEvent_closeLpEventPath(cnx->remote_lp,
  549. HvLpEvent_Type_VirtualLan);
  550. /*
  551. * Reset ack data. This prevents the ack_timer actually
  552. * doing anything, even if it runs one more time when
  553. * we drop the lock below.
  554. */
  555. memset(&cnx->pending_acks, 0xff, sizeof (cnx->pending_acks));
  556. cnx->num_pending_acks = 0;
  557. cnx->state &= ~(VETH_STATE_RESET | VETH_STATE_SENTMON
  558. | VETH_STATE_OPEN | VETH_STATE_SENTCAPS
  559. | VETH_STATE_GOTCAPACK | VETH_STATE_GOTCAPS
  560. | VETH_STATE_SENTCAPACK | VETH_STATE_READY);
  561. /* Clean up any leftover messages */
  562. if (cnx->msgs) {
  563. int i;
  564. for (i = 0; i < VETH_NUMBUFFERS; ++i)
  565. veth_recycle_msg(cnx, cnx->msgs + i);
  566. }
  567. cnx->outstanding_tx = 0;
  568. veth_wake_queues(cnx);
  569. /* Drop the lock so we can do stuff that might sleep or
  570. * take other locks. */
  571. spin_unlock_irq(&cnx->lock);
  572. del_timer_sync(&cnx->ack_timer);
  573. del_timer_sync(&cnx->reset_timer);
  574. spin_lock_irq(&cnx->lock);
  575. if (cnx->state & VETH_STATE_RESET)
  576. goto restart;
  577. /* Hack, wait for the other end to reset itself. */
  578. if (! (cnx->state & VETH_STATE_SHUTDOWN)) {
  579. schedule_delayed_work(&cnx->statemachine_wq, 5 * HZ);
  580. goto out;
  581. }
  582. }
  583. if (cnx->state & VETH_STATE_SHUTDOWN)
  584. /* It's all over, do nothing */
  585. goto out;
  586. if ( !(cnx->state & VETH_STATE_OPEN) ) {
  587. if (! cnx->msgs || (cnx->num_events < (2 + VETH_NUMBUFFERS)) )
  588. goto cant_cope;
  589. HvCallEvent_openLpEventPath(rlp, HvLpEvent_Type_VirtualLan);
  590. cnx->src_inst =
  591. HvCallEvent_getSourceLpInstanceId(rlp,
  592. HvLpEvent_Type_VirtualLan);
  593. cnx->dst_inst =
  594. HvCallEvent_getTargetLpInstanceId(rlp,
  595. HvLpEvent_Type_VirtualLan);
  596. cnx->state |= VETH_STATE_OPEN;
  597. }
  598. if ( (cnx->state & VETH_STATE_OPEN) &&
  599. !(cnx->state & VETH_STATE_SENTMON) ) {
  600. rc = veth_signalevent(cnx, VETH_EVENT_MONITOR,
  601. HvLpEvent_AckInd_DoAck,
  602. HvLpEvent_AckType_DeferredAck,
  603. 0, 0, 0, 0, 0, 0);
  604. if (rc == HvLpEvent_Rc_Good) {
  605. cnx->state |= VETH_STATE_SENTMON;
  606. } else {
  607. if ( (rc != HvLpEvent_Rc_PartitionDead) &&
  608. (rc != HvLpEvent_Rc_PathClosed) )
  609. veth_error("Error sending monitor to LPAR %d, "
  610. "rc = %d\n", rlp, rc);
  611. /* Oh well, hope we get a cap from the other
  612. * end and do better when that kicks us */
  613. goto out;
  614. }
  615. }
  616. if ( (cnx->state & VETH_STATE_OPEN) &&
  617. !(cnx->state & VETH_STATE_SENTCAPS)) {
  618. u64 *rawcap = (u64 *)&cnx->local_caps;
  619. rc = veth_signalevent(cnx, VETH_EVENT_CAP,
  620. HvLpEvent_AckInd_DoAck,
  621. HvLpEvent_AckType_ImmediateAck,
  622. 0, rawcap[0], rawcap[1], rawcap[2],
  623. rawcap[3], rawcap[4]);
  624. if (rc == HvLpEvent_Rc_Good) {
  625. cnx->state |= VETH_STATE_SENTCAPS;
  626. } else {
  627. if ( (rc != HvLpEvent_Rc_PartitionDead) &&
  628. (rc != HvLpEvent_Rc_PathClosed) )
  629. veth_error("Error sending caps to LPAR %d, "
  630. "rc = %d\n", rlp, rc);
  631. /* Oh well, hope we get a cap from the other
  632. * end and do better when that kicks us */
  633. goto out;
  634. }
  635. }
  636. if ((cnx->state & VETH_STATE_GOTCAPS) &&
  637. !(cnx->state & VETH_STATE_SENTCAPACK)) {
  638. struct veth_cap_data *remote_caps = &cnx->remote_caps;
  639. memcpy(remote_caps, &cnx->cap_event.u.caps_data,
  640. sizeof(*remote_caps));
  641. spin_unlock_irq(&cnx->lock);
  642. rc = veth_process_caps(cnx);
  643. spin_lock_irq(&cnx->lock);
  644. /* We dropped the lock, so recheck for anything which
  645. * might mess us up */
  646. if (cnx->state & (VETH_STATE_RESET|VETH_STATE_SHUTDOWN))
  647. goto restart;
  648. cnx->cap_event.base_event.xRc = rc;
  649. HvCallEvent_ackLpEvent((struct HvLpEvent *)&cnx->cap_event);
  650. if (rc == HvLpEvent_Rc_Good)
  651. cnx->state |= VETH_STATE_SENTCAPACK;
  652. else
  653. goto cant_cope;
  654. }
  655. if ((cnx->state & VETH_STATE_GOTCAPACK) &&
  656. (cnx->state & VETH_STATE_GOTCAPS) &&
  657. !(cnx->state & VETH_STATE_READY)) {
  658. if (cnx->cap_ack_event.base_event.xRc == HvLpEvent_Rc_Good) {
  659. /* Start the ACK timer */
  660. cnx->ack_timer.expires = jiffies + cnx->ack_timeout;
  661. add_timer(&cnx->ack_timer);
  662. cnx->state |= VETH_STATE_READY;
  663. } else {
  664. veth_error("Caps rejected by LPAR %d, rc = %d\n",
  665. rlp, cnx->cap_ack_event.base_event.xRc);
  666. goto cant_cope;
  667. }
  668. }
  669. out:
  670. spin_unlock_irq(&cnx->lock);
  671. return;
  672. cant_cope:
  673. /* FIXME: we get here if something happens we really can't
  674. * cope with. The link will never work once we get here, and
  675. * all we can do is not lock the rest of the system up */
  676. veth_error("Unrecoverable error on connection to LPAR %d, shutting down"
  677. " (state = 0x%04lx)\n", rlp, cnx->state);
  678. cnx->state |= VETH_STATE_SHUTDOWN;
  679. spin_unlock_irq(&cnx->lock);
  680. }
  681. static int veth_init_connection(u8 rlp)
  682. {
  683. struct veth_lpar_connection *cnx;
  684. struct veth_msg *msgs;
  685. int i;
  686. if ( (rlp == this_lp) ||
  687. ! HvLpConfig_doLpsCommunicateOnVirtualLan(this_lp, rlp) )
  688. return 0;
  689. cnx = kzalloc(sizeof(*cnx), GFP_KERNEL);
  690. if (! cnx)
  691. return -ENOMEM;
  692. cnx->remote_lp = rlp;
  693. spin_lock_init(&cnx->lock);
  694. INIT_DELAYED_WORK(&cnx->statemachine_wq, veth_statemachine);
  695. init_timer(&cnx->ack_timer);
  696. cnx->ack_timer.function = veth_timed_ack;
  697. cnx->ack_timer.data = (unsigned long) cnx;
  698. init_timer(&cnx->reset_timer);
  699. cnx->reset_timer.function = veth_timed_reset;
  700. cnx->reset_timer.data = (unsigned long) cnx;
  701. cnx->reset_timeout = 5 * HZ * (VETH_ACKTIMEOUT / 1000000);
  702. memset(&cnx->pending_acks, 0xff, sizeof (cnx->pending_acks));
  703. veth_cnx[rlp] = cnx;
  704. /* This gets us 1 reference, which is held on behalf of the driver
  705. * infrastructure. It's released at module unload. */
  706. kobject_init(&cnx->kobject, &veth_lpar_connection_ktype);
  707. msgs = kcalloc(VETH_NUMBUFFERS, sizeof(struct veth_msg), GFP_KERNEL);
  708. if (! msgs) {
  709. veth_error("Can't allocate buffers for LPAR %d.\n", rlp);
  710. return -ENOMEM;
  711. }
  712. cnx->msgs = msgs;
  713. for (i = 0; i < VETH_NUMBUFFERS; i++) {
  714. msgs[i].token = i;
  715. veth_stack_push(cnx, msgs + i);
  716. }
  717. cnx->num_events = veth_allocate_events(rlp, 2 + VETH_NUMBUFFERS);
  718. if (cnx->num_events < (2 + VETH_NUMBUFFERS)) {
  719. veth_error("Can't allocate enough events for LPAR %d.\n", rlp);
  720. return -ENOMEM;
  721. }
  722. cnx->local_caps.num_buffers = VETH_NUMBUFFERS;
  723. cnx->local_caps.ack_threshold = ACK_THRESHOLD;
  724. cnx->local_caps.ack_timeout = VETH_ACKTIMEOUT;
  725. return 0;
  726. }
  727. static void veth_stop_connection(struct veth_lpar_connection *cnx)
  728. {
  729. if (!cnx)
  730. return;
  731. spin_lock_irq(&cnx->lock);
  732. cnx->state |= VETH_STATE_RESET | VETH_STATE_SHUTDOWN;
  733. veth_kick_statemachine(cnx);
  734. spin_unlock_irq(&cnx->lock);
  735. /* There's a slim chance the reset code has just queued the
  736. * statemachine to run in five seconds. If so we need to cancel
  737. * that and requeue the work to run now. */
  738. if (cancel_delayed_work(&cnx->statemachine_wq)) {
  739. spin_lock_irq(&cnx->lock);
  740. veth_kick_statemachine(cnx);
  741. spin_unlock_irq(&cnx->lock);
  742. }
  743. /* Wait for the state machine to run. */
  744. flush_scheduled_work();
  745. }
  746. static void veth_destroy_connection(struct veth_lpar_connection *cnx)
  747. {
  748. if (!cnx)
  749. return;
  750. if (cnx->num_events > 0)
  751. mf_deallocate_lp_events(cnx->remote_lp,
  752. HvLpEvent_Type_VirtualLan,
  753. cnx->num_events,
  754. NULL, NULL);
  755. if (cnx->num_ack_events > 0)
  756. mf_deallocate_lp_events(cnx->remote_lp,
  757. HvLpEvent_Type_VirtualLan,
  758. cnx->num_ack_events,
  759. NULL, NULL);
  760. kfree(cnx->msgs);
  761. veth_cnx[cnx->remote_lp] = NULL;
  762. kfree(cnx);
  763. }
  764. static void veth_release_connection(struct kobject *kobj)
  765. {
  766. struct veth_lpar_connection *cnx;
  767. cnx = container_of(kobj, struct veth_lpar_connection, kobject);
  768. veth_stop_connection(cnx);
  769. veth_destroy_connection(cnx);
  770. }
  771. /*
  772. * net_device code
  773. */
  774. static int veth_open(struct net_device *dev)
  775. {
  776. netif_start_queue(dev);
  777. return 0;
  778. }
  779. static int veth_close(struct net_device *dev)
  780. {
  781. netif_stop_queue(dev);
  782. return 0;
  783. }
  784. static int veth_change_mtu(struct net_device *dev, int new_mtu)
  785. {
  786. if ((new_mtu < 68) || (new_mtu > VETH_MAX_MTU))
  787. return -EINVAL;
  788. dev->mtu = new_mtu;
  789. return 0;
  790. }
  791. static void veth_set_multicast_list(struct net_device *dev)
  792. {
  793. struct veth_port *port = netdev_priv(dev);
  794. unsigned long flags;
  795. write_lock_irqsave(&port->mcast_gate, flags);
  796. if ((dev->flags & IFF_PROMISC) || (dev->flags & IFF_ALLMULTI) ||
  797. (netdev_mc_count(dev) > VETH_MAX_MCAST)) {
  798. port->promiscuous = 1;
  799. } else {
  800. struct netdev_hw_addr *ha;
  801. port->promiscuous = 0;
  802. /* Update table */
  803. port->num_mcast = 0;
  804. netdev_for_each_mc_addr(ha, dev) {
  805. u8 *addr = ha->addr;
  806. u64 xaddr = 0;
  807. if (addr[0] & 0x01) {/* multicast address? */
  808. memcpy(&xaddr, addr, ETH_ALEN);
  809. port->mcast_addr[port->num_mcast] = xaddr;
  810. port->num_mcast++;
  811. }
  812. }
  813. }
  814. write_unlock_irqrestore(&port->mcast_gate, flags);
  815. }
  816. static void veth_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  817. {
  818. strncpy(info->driver, DRV_NAME, sizeof(info->driver) - 1);
  819. info->driver[sizeof(info->driver) - 1] = '\0';
  820. strncpy(info->version, DRV_VERSION, sizeof(info->version) - 1);
  821. info->version[sizeof(info->version) - 1] = '\0';
  822. }
  823. static int veth_get_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
  824. {
  825. ecmd->supported = (SUPPORTED_1000baseT_Full
  826. | SUPPORTED_Autoneg | SUPPORTED_FIBRE);
  827. ecmd->advertising = (SUPPORTED_1000baseT_Full
  828. | SUPPORTED_Autoneg | SUPPORTED_FIBRE);
  829. ecmd->port = PORT_FIBRE;
  830. ecmd->transceiver = XCVR_INTERNAL;
  831. ecmd->phy_address = 0;
  832. ecmd->speed = SPEED_1000;
  833. ecmd->duplex = DUPLEX_FULL;
  834. ecmd->autoneg = AUTONEG_ENABLE;
  835. ecmd->maxtxpkt = 120;
  836. ecmd->maxrxpkt = 120;
  837. return 0;
  838. }
  839. static u32 veth_get_link(struct net_device *dev)
  840. {
  841. return 1;
  842. }
  843. static const struct ethtool_ops ops = {
  844. .get_drvinfo = veth_get_drvinfo,
  845. .get_settings = veth_get_settings,
  846. .get_link = veth_get_link,
  847. };
  848. static const struct net_device_ops veth_netdev_ops = {
  849. .ndo_open = veth_open,
  850. .ndo_stop = veth_close,
  851. .ndo_start_xmit = veth_start_xmit,
  852. .ndo_change_mtu = veth_change_mtu,
  853. .ndo_set_multicast_list = veth_set_multicast_list,
  854. .ndo_set_mac_address = NULL,
  855. .ndo_validate_addr = eth_validate_addr,
  856. };
  857. static struct net_device *veth_probe_one(int vlan,
  858. struct vio_dev *vio_dev)
  859. {
  860. struct net_device *dev;
  861. struct veth_port *port;
  862. struct device *vdev = &vio_dev->dev;
  863. int i, rc;
  864. const unsigned char *mac_addr;
  865. mac_addr = vio_get_attribute(vio_dev, "local-mac-address", NULL);
  866. if (mac_addr == NULL)
  867. mac_addr = vio_get_attribute(vio_dev, "mac-address", NULL);
  868. if (mac_addr == NULL) {
  869. veth_error("Unable to fetch MAC address from device tree.\n");
  870. return NULL;
  871. }
  872. dev = alloc_etherdev(sizeof (struct veth_port));
  873. if (! dev) {
  874. veth_error("Unable to allocate net_device structure!\n");
  875. return NULL;
  876. }
  877. port = netdev_priv(dev);
  878. spin_lock_init(&port->queue_lock);
  879. rwlock_init(&port->mcast_gate);
  880. port->stopped_map = 0;
  881. for (i = 0; i < HVMAXARCHITECTEDLPS; i++) {
  882. HvLpVirtualLanIndexMap map;
  883. if (i == this_lp)
  884. continue;
  885. map = HvLpConfig_getVirtualLanIndexMapForLp(i);
  886. if (map & (0x8000 >> vlan))
  887. port->lpar_map |= (1 << i);
  888. }
  889. port->dev = vdev;
  890. memcpy(dev->dev_addr, mac_addr, ETH_ALEN);
  891. dev->mtu = VETH_MAX_MTU;
  892. memcpy(&port->mac_addr, mac_addr, ETH_ALEN);
  893. dev->netdev_ops = &veth_netdev_ops;
  894. SET_ETHTOOL_OPS(dev, &ops);
  895. SET_NETDEV_DEV(dev, vdev);
  896. rc = register_netdev(dev);
  897. if (rc != 0) {
  898. veth_error("Failed registering net device for vlan%d.\n", vlan);
  899. free_netdev(dev);
  900. return NULL;
  901. }
  902. kobject_init(&port->kobject, &veth_port_ktype);
  903. if (0 != kobject_add(&port->kobject, &dev->dev.kobj, "veth_port"))
  904. veth_error("Failed adding port for %s to sysfs.\n", dev->name);
  905. veth_info("%s attached to iSeries vlan %d (LPAR map = 0x%.4X)\n",
  906. dev->name, vlan, port->lpar_map);
  907. return dev;
  908. }
  909. /*
  910. * Tx path
  911. */
  912. static int veth_transmit_to_one(struct sk_buff *skb, HvLpIndex rlp,
  913. struct net_device *dev)
  914. {
  915. struct veth_lpar_connection *cnx = veth_cnx[rlp];
  916. struct veth_port *port = netdev_priv(dev);
  917. HvLpEvent_Rc rc;
  918. struct veth_msg *msg = NULL;
  919. unsigned long flags;
  920. if (! cnx)
  921. return 0;
  922. spin_lock_irqsave(&cnx->lock, flags);
  923. if (! (cnx->state & VETH_STATE_READY))
  924. goto no_error;
  925. if ((skb->len - ETH_HLEN) > VETH_MAX_MTU)
  926. goto drop;
  927. msg = veth_stack_pop(cnx);
  928. if (! msg)
  929. goto drop;
  930. msg->in_use = 1;
  931. msg->skb = skb_get(skb);
  932. msg->data.addr[0] = dma_map_single(port->dev, skb->data,
  933. skb->len, DMA_TO_DEVICE);
  934. if (dma_mapping_error(port->dev, msg->data.addr[0]))
  935. goto recycle_and_drop;
  936. msg->dev = port->dev;
  937. msg->data.len[0] = skb->len;
  938. msg->data.eofmask = 1 << VETH_EOF_SHIFT;
  939. rc = veth_signaldata(cnx, VETH_EVENT_FRAMES, msg->token, &msg->data);
  940. if (rc != HvLpEvent_Rc_Good)
  941. goto recycle_and_drop;
  942. /* If the timer's not already running, start it now. */
  943. if (0 == cnx->outstanding_tx)
  944. mod_timer(&cnx->reset_timer, jiffies + cnx->reset_timeout);
  945. cnx->last_contact = jiffies;
  946. cnx->outstanding_tx++;
  947. if (veth_stack_is_empty(cnx))
  948. veth_stop_queues(cnx);
  949. no_error:
  950. spin_unlock_irqrestore(&cnx->lock, flags);
  951. return 0;
  952. recycle_and_drop:
  953. veth_recycle_msg(cnx, msg);
  954. drop:
  955. spin_unlock_irqrestore(&cnx->lock, flags);
  956. return 1;
  957. }
  958. static void veth_transmit_to_many(struct sk_buff *skb,
  959. HvLpIndexMap lpmask,
  960. struct net_device *dev)
  961. {
  962. int i, success, error;
  963. success = error = 0;
  964. for (i = 0; i < HVMAXARCHITECTEDLPS; i++) {
  965. if ((lpmask & (1 << i)) == 0)
  966. continue;
  967. if (veth_transmit_to_one(skb, i, dev))
  968. error = 1;
  969. else
  970. success = 1;
  971. }
  972. if (error)
  973. dev->stats.tx_errors++;
  974. if (success) {
  975. dev->stats.tx_packets++;
  976. dev->stats.tx_bytes += skb->len;
  977. }
  978. }
  979. static int veth_start_xmit(struct sk_buff *skb, struct net_device *dev)
  980. {
  981. unsigned char *frame = skb->data;
  982. struct veth_port *port = netdev_priv(dev);
  983. HvLpIndexMap lpmask;
  984. if (! (frame[0] & 0x01)) {
  985. /* unicast packet */
  986. HvLpIndex rlp = frame[5];
  987. if ( ! ((1 << rlp) & port->lpar_map) ) {
  988. dev_kfree_skb(skb);
  989. return NETDEV_TX_OK;
  990. }
  991. lpmask = 1 << rlp;
  992. } else {
  993. lpmask = port->lpar_map;
  994. }
  995. veth_transmit_to_many(skb, lpmask, dev);
  996. dev_kfree_skb(skb);
  997. return NETDEV_TX_OK;
  998. }
  999. /* You must hold the connection's lock when you call this function. */
  1000. static void veth_recycle_msg(struct veth_lpar_connection *cnx,
  1001. struct veth_msg *msg)
  1002. {
  1003. u32 dma_address, dma_length;
  1004. if (msg->in_use) {
  1005. msg->in_use = 0;
  1006. dma_address = msg->data.addr[0];
  1007. dma_length = msg->data.len[0];
  1008. if (!dma_mapping_error(msg->dev, dma_address))
  1009. dma_unmap_single(msg->dev, dma_address, dma_length,
  1010. DMA_TO_DEVICE);
  1011. if (msg->skb) {
  1012. dev_kfree_skb_any(msg->skb);
  1013. msg->skb = NULL;
  1014. }
  1015. memset(&msg->data, 0, sizeof(msg->data));
  1016. veth_stack_push(cnx, msg);
  1017. } else if (cnx->state & VETH_STATE_OPEN) {
  1018. veth_error("Non-pending frame (# %d) acked by LPAR %d.\n",
  1019. cnx->remote_lp, msg->token);
  1020. }
  1021. }
  1022. static void veth_wake_queues(struct veth_lpar_connection *cnx)
  1023. {
  1024. int i;
  1025. for (i = 0; i < HVMAXARCHITECTEDVIRTUALLANS; i++) {
  1026. struct net_device *dev = veth_dev[i];
  1027. struct veth_port *port;
  1028. unsigned long flags;
  1029. if (! dev)
  1030. continue;
  1031. port = netdev_priv(dev);
  1032. if (! (port->lpar_map & (1<<cnx->remote_lp)))
  1033. continue;
  1034. spin_lock_irqsave(&port->queue_lock, flags);
  1035. port->stopped_map &= ~(1 << cnx->remote_lp);
  1036. if (0 == port->stopped_map && netif_queue_stopped(dev)) {
  1037. veth_debug("cnx %d: woke queue for %s.\n",
  1038. cnx->remote_lp, dev->name);
  1039. netif_wake_queue(dev);
  1040. }
  1041. spin_unlock_irqrestore(&port->queue_lock, flags);
  1042. }
  1043. }
  1044. static void veth_stop_queues(struct veth_lpar_connection *cnx)
  1045. {
  1046. int i;
  1047. for (i = 0; i < HVMAXARCHITECTEDVIRTUALLANS; i++) {
  1048. struct net_device *dev = veth_dev[i];
  1049. struct veth_port *port;
  1050. if (! dev)
  1051. continue;
  1052. port = netdev_priv(dev);
  1053. /* If this cnx is not on the vlan for this port, continue */
  1054. if (! (port->lpar_map & (1 << cnx->remote_lp)))
  1055. continue;
  1056. spin_lock(&port->queue_lock);
  1057. netif_stop_queue(dev);
  1058. port->stopped_map |= (1 << cnx->remote_lp);
  1059. veth_debug("cnx %d: stopped queue for %s, map = 0x%x.\n",
  1060. cnx->remote_lp, dev->name, port->stopped_map);
  1061. spin_unlock(&port->queue_lock);
  1062. }
  1063. }
  1064. static void veth_timed_reset(unsigned long ptr)
  1065. {
  1066. struct veth_lpar_connection *cnx = (struct veth_lpar_connection *)ptr;
  1067. unsigned long trigger_time, flags;
  1068. /* FIXME is it possible this fires after veth_stop_connection()?
  1069. * That would reschedule the statemachine for 5 seconds and probably
  1070. * execute it after the module's been unloaded. Hmm. */
  1071. spin_lock_irqsave(&cnx->lock, flags);
  1072. if (cnx->outstanding_tx > 0) {
  1073. trigger_time = cnx->last_contact + cnx->reset_timeout;
  1074. if (trigger_time < jiffies) {
  1075. cnx->state |= VETH_STATE_RESET;
  1076. veth_kick_statemachine(cnx);
  1077. veth_error("%d packets not acked by LPAR %d within %d "
  1078. "seconds, resetting.\n",
  1079. cnx->outstanding_tx, cnx->remote_lp,
  1080. cnx->reset_timeout / HZ);
  1081. } else {
  1082. /* Reschedule the timer */
  1083. trigger_time = jiffies + cnx->reset_timeout;
  1084. mod_timer(&cnx->reset_timer, trigger_time);
  1085. }
  1086. }
  1087. spin_unlock_irqrestore(&cnx->lock, flags);
  1088. }
  1089. /*
  1090. * Rx path
  1091. */
  1092. static inline int veth_frame_wanted(struct veth_port *port, u64 mac_addr)
  1093. {
  1094. int wanted = 0;
  1095. int i;
  1096. unsigned long flags;
  1097. if ( (mac_addr == port->mac_addr) || (mac_addr == 0xffffffffffff0000) )
  1098. return 1;
  1099. read_lock_irqsave(&port->mcast_gate, flags);
  1100. if (port->promiscuous) {
  1101. wanted = 1;
  1102. goto out;
  1103. }
  1104. for (i = 0; i < port->num_mcast; ++i) {
  1105. if (port->mcast_addr[i] == mac_addr) {
  1106. wanted = 1;
  1107. break;
  1108. }
  1109. }
  1110. out:
  1111. read_unlock_irqrestore(&port->mcast_gate, flags);
  1112. return wanted;
  1113. }
  1114. struct dma_chunk {
  1115. u64 addr;
  1116. u64 size;
  1117. };
  1118. #define VETH_MAX_PAGES_PER_FRAME ( (VETH_MAX_MTU+PAGE_SIZE-2)/PAGE_SIZE + 1 )
  1119. static inline void veth_build_dma_list(struct dma_chunk *list,
  1120. unsigned char *p, unsigned long length)
  1121. {
  1122. unsigned long done;
  1123. int i = 1;
  1124. /* FIXME: skbs are contiguous in real addresses. Do we
  1125. * really need to break it into PAGE_SIZE chunks, or can we do
  1126. * it just at the granularity of iSeries real->absolute
  1127. * mapping? Indeed, given the way the allocator works, can we
  1128. * count on them being absolutely contiguous? */
  1129. list[0].addr = iseries_hv_addr(p);
  1130. list[0].size = min(length,
  1131. PAGE_SIZE - ((unsigned long)p & ~PAGE_MASK));
  1132. done = list[0].size;
  1133. while (done < length) {
  1134. list[i].addr = iseries_hv_addr(p + done);
  1135. list[i].size = min(length-done, PAGE_SIZE);
  1136. done += list[i].size;
  1137. i++;
  1138. }
  1139. }
  1140. static void veth_flush_acks(struct veth_lpar_connection *cnx)
  1141. {
  1142. HvLpEvent_Rc rc;
  1143. rc = veth_signaldata(cnx, VETH_EVENT_FRAMES_ACK,
  1144. 0, &cnx->pending_acks);
  1145. if (rc != HvLpEvent_Rc_Good)
  1146. veth_error("Failed acking frames from LPAR %d, rc = %d\n",
  1147. cnx->remote_lp, (int)rc);
  1148. cnx->num_pending_acks = 0;
  1149. memset(&cnx->pending_acks, 0xff, sizeof(cnx->pending_acks));
  1150. }
  1151. static void veth_receive(struct veth_lpar_connection *cnx,
  1152. struct veth_lpevent *event)
  1153. {
  1154. struct veth_frames_data *senddata = &event->u.frames_data;
  1155. int startchunk = 0;
  1156. int nchunks;
  1157. unsigned long flags;
  1158. HvLpDma_Rc rc;
  1159. do {
  1160. u16 length = 0;
  1161. struct sk_buff *skb;
  1162. struct dma_chunk local_list[VETH_MAX_PAGES_PER_FRAME];
  1163. struct dma_chunk remote_list[VETH_MAX_FRAMES_PER_MSG];
  1164. u64 dest;
  1165. HvLpVirtualLanIndex vlan;
  1166. struct net_device *dev;
  1167. struct veth_port *port;
  1168. /* FIXME: do we need this? */
  1169. memset(local_list, 0, sizeof(local_list));
  1170. memset(remote_list, 0, sizeof(VETH_MAX_FRAMES_PER_MSG));
  1171. /* a 0 address marks the end of the valid entries */
  1172. if (senddata->addr[startchunk] == 0)
  1173. break;
  1174. /* make sure that we have at least 1 EOF entry in the
  1175. * remaining entries */
  1176. if (! (senddata->eofmask >> (startchunk + VETH_EOF_SHIFT))) {
  1177. veth_error("Missing EOF fragment in event "
  1178. "eofmask = 0x%x startchunk = %d\n",
  1179. (unsigned)senddata->eofmask,
  1180. startchunk);
  1181. break;
  1182. }
  1183. /* build list of chunks in this frame */
  1184. nchunks = 0;
  1185. do {
  1186. remote_list[nchunks].addr =
  1187. (u64) senddata->addr[startchunk+nchunks] << 32;
  1188. remote_list[nchunks].size =
  1189. senddata->len[startchunk+nchunks];
  1190. length += remote_list[nchunks].size;
  1191. } while (! (senddata->eofmask &
  1192. (1 << (VETH_EOF_SHIFT + startchunk + nchunks++))));
  1193. /* length == total length of all chunks */
  1194. /* nchunks == # of chunks in this frame */
  1195. if ((length - ETH_HLEN) > VETH_MAX_MTU) {
  1196. veth_error("Received oversize frame from LPAR %d "
  1197. "(length = %d)\n",
  1198. cnx->remote_lp, length);
  1199. continue;
  1200. }
  1201. skb = alloc_skb(length, GFP_ATOMIC);
  1202. if (!skb)
  1203. continue;
  1204. veth_build_dma_list(local_list, skb->data, length);
  1205. rc = HvCallEvent_dmaBufList(HvLpEvent_Type_VirtualLan,
  1206. event->base_event.xSourceLp,
  1207. HvLpDma_Direction_RemoteToLocal,
  1208. cnx->src_inst,
  1209. cnx->dst_inst,
  1210. HvLpDma_AddressType_RealAddress,
  1211. HvLpDma_AddressType_TceIndex,
  1212. iseries_hv_addr(&local_list),
  1213. iseries_hv_addr(&remote_list),
  1214. length);
  1215. if (rc != HvLpDma_Rc_Good) {
  1216. dev_kfree_skb_irq(skb);
  1217. continue;
  1218. }
  1219. vlan = skb->data[9];
  1220. dev = veth_dev[vlan];
  1221. if (! dev) {
  1222. /*
  1223. * Some earlier versions of the driver sent
  1224. * broadcasts down all connections, even to lpars
  1225. * that weren't on the relevant vlan. So ignore
  1226. * packets belonging to a vlan we're not on.
  1227. * We can also be here if we receive packets while
  1228. * the driver is going down, because then dev is NULL.
  1229. */
  1230. dev_kfree_skb_irq(skb);
  1231. continue;
  1232. }
  1233. port = netdev_priv(dev);
  1234. dest = *((u64 *) skb->data) & 0xFFFFFFFFFFFF0000;
  1235. if ((vlan > HVMAXARCHITECTEDVIRTUALLANS) || !port) {
  1236. dev_kfree_skb_irq(skb);
  1237. continue;
  1238. }
  1239. if (! veth_frame_wanted(port, dest)) {
  1240. dev_kfree_skb_irq(skb);
  1241. continue;
  1242. }
  1243. skb_put(skb, length);
  1244. skb->protocol = eth_type_trans(skb, dev);
  1245. skb->ip_summed = CHECKSUM_NONE;
  1246. netif_rx(skb); /* send it up */
  1247. dev->stats.rx_packets++;
  1248. dev->stats.rx_bytes += length;
  1249. } while (startchunk += nchunks, startchunk < VETH_MAX_FRAMES_PER_MSG);
  1250. /* Ack it */
  1251. spin_lock_irqsave(&cnx->lock, flags);
  1252. BUG_ON(cnx->num_pending_acks > VETH_MAX_ACKS_PER_MSG);
  1253. cnx->pending_acks[cnx->num_pending_acks++] =
  1254. event->base_event.xCorrelationToken;
  1255. if ( (cnx->num_pending_acks >= cnx->remote_caps.ack_threshold) ||
  1256. (cnx->num_pending_acks >= VETH_MAX_ACKS_PER_MSG) )
  1257. veth_flush_acks(cnx);
  1258. spin_unlock_irqrestore(&cnx->lock, flags);
  1259. }
  1260. static void veth_timed_ack(unsigned long ptr)
  1261. {
  1262. struct veth_lpar_connection *cnx = (struct veth_lpar_connection *) ptr;
  1263. unsigned long flags;
  1264. /* Ack all the events */
  1265. spin_lock_irqsave(&cnx->lock, flags);
  1266. if (cnx->num_pending_acks > 0)
  1267. veth_flush_acks(cnx);
  1268. /* Reschedule the timer */
  1269. cnx->ack_timer.expires = jiffies + cnx->ack_timeout;
  1270. add_timer(&cnx->ack_timer);
  1271. spin_unlock_irqrestore(&cnx->lock, flags);
  1272. }
  1273. static int veth_remove(struct vio_dev *vdev)
  1274. {
  1275. struct veth_lpar_connection *cnx;
  1276. struct net_device *dev;
  1277. struct veth_port *port;
  1278. int i;
  1279. dev = veth_dev[vdev->unit_address];
  1280. if (! dev)
  1281. return 0;
  1282. port = netdev_priv(dev);
  1283. for (i = 0; i < HVMAXARCHITECTEDLPS; i++) {
  1284. cnx = veth_cnx[i];
  1285. if (cnx && (port->lpar_map & (1 << i))) {
  1286. /* Drop our reference to connections on our VLAN */
  1287. kobject_put(&cnx->kobject);
  1288. }
  1289. }
  1290. veth_dev[vdev->unit_address] = NULL;
  1291. kobject_del(&port->kobject);
  1292. kobject_put(&port->kobject);
  1293. unregister_netdev(dev);
  1294. free_netdev(dev);
  1295. return 0;
  1296. }
  1297. static int veth_probe(struct vio_dev *vdev, const struct vio_device_id *id)
  1298. {
  1299. int i = vdev->unit_address;
  1300. struct net_device *dev;
  1301. struct veth_port *port;
  1302. dev = veth_probe_one(i, vdev);
  1303. if (dev == NULL) {
  1304. veth_remove(vdev);
  1305. return 1;
  1306. }
  1307. veth_dev[i] = dev;
  1308. port = (struct veth_port*)netdev_priv(dev);
  1309. /* Start the state machine on each connection on this vlan. If we're
  1310. * the first dev to do so this will commence link negotiation */
  1311. for (i = 0; i < HVMAXARCHITECTEDLPS; i++) {
  1312. struct veth_lpar_connection *cnx;
  1313. if (! (port->lpar_map & (1 << i)))
  1314. continue;
  1315. cnx = veth_cnx[i];
  1316. if (!cnx)
  1317. continue;
  1318. kobject_get(&cnx->kobject);
  1319. veth_kick_statemachine(cnx);
  1320. }
  1321. return 0;
  1322. }
  1323. /**
  1324. * veth_device_table: Used by vio.c to match devices that we
  1325. * support.
  1326. */
  1327. static struct vio_device_id veth_device_table[] __devinitdata = {
  1328. { "network", "IBM,iSeries-l-lan" },
  1329. { "", "" }
  1330. };
  1331. MODULE_DEVICE_TABLE(vio, veth_device_table);
  1332. static struct vio_driver veth_driver = {
  1333. .id_table = veth_device_table,
  1334. .probe = veth_probe,
  1335. .remove = veth_remove,
  1336. .driver = {
  1337. .name = DRV_NAME,
  1338. .owner = THIS_MODULE,
  1339. }
  1340. };
  1341. /*
  1342. * Module initialization/cleanup
  1343. */
  1344. static void __exit veth_module_cleanup(void)
  1345. {
  1346. int i;
  1347. struct veth_lpar_connection *cnx;
  1348. /* Disconnect our "irq" to stop events coming from the Hypervisor. */
  1349. HvLpEvent_unregisterHandler(HvLpEvent_Type_VirtualLan);
  1350. /* Make sure any work queued from Hypervisor callbacks is finished. */
  1351. flush_scheduled_work();
  1352. for (i = 0; i < HVMAXARCHITECTEDLPS; ++i) {
  1353. cnx = veth_cnx[i];
  1354. if (!cnx)
  1355. continue;
  1356. /* Remove the connection from sysfs */
  1357. kobject_del(&cnx->kobject);
  1358. /* Drop the driver's reference to the connection */
  1359. kobject_put(&cnx->kobject);
  1360. }
  1361. /* Unregister the driver, which will close all the netdevs and stop
  1362. * the connections when they're no longer referenced. */
  1363. vio_unregister_driver(&veth_driver);
  1364. }
  1365. module_exit(veth_module_cleanup);
  1366. static int __init veth_module_init(void)
  1367. {
  1368. int i;
  1369. int rc;
  1370. if (!firmware_has_feature(FW_FEATURE_ISERIES))
  1371. return -ENODEV;
  1372. this_lp = HvLpConfig_getLpIndex_outline();
  1373. for (i = 0; i < HVMAXARCHITECTEDLPS; ++i) {
  1374. rc = veth_init_connection(i);
  1375. if (rc != 0)
  1376. goto error;
  1377. }
  1378. HvLpEvent_registerHandler(HvLpEvent_Type_VirtualLan,
  1379. &veth_handle_event);
  1380. rc = vio_register_driver(&veth_driver);
  1381. if (rc != 0)
  1382. goto error;
  1383. for (i = 0; i < HVMAXARCHITECTEDLPS; ++i) {
  1384. struct kobject *kobj;
  1385. if (!veth_cnx[i])
  1386. continue;
  1387. kobj = &veth_cnx[i]->kobject;
  1388. /* If the add failes, complain but otherwise continue */
  1389. if (0 != driver_add_kobj(&veth_driver.driver, kobj,
  1390. "cnx%.2d", veth_cnx[i]->remote_lp))
  1391. veth_error("cnx %d: Failed adding to sysfs.\n", i);
  1392. }
  1393. return 0;
  1394. error:
  1395. for (i = 0; i < HVMAXARCHITECTEDLPS; ++i) {
  1396. veth_destroy_connection(veth_cnx[i]);
  1397. }
  1398. return rc;
  1399. }
  1400. module_init(veth_module_init);