iseries_veth.c 44 KB

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