iseries_veth.c 43 KB

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