iseries_veth.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735
  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/config.h>
  59. #include <linux/module.h>
  60. #include <linux/version.h>
  61. #include <linux/types.h>
  62. #include <linux/errno.h>
  63. #include <linux/ioport.h>
  64. #include <linux/kernel.h>
  65. #include <linux/netdevice.h>
  66. #include <linux/etherdevice.h>
  67. #include <linux/skbuff.h>
  68. #include <linux/init.h>
  69. #include <linux/delay.h>
  70. #include <linux/mm.h>
  71. #include <linux/ethtool.h>
  72. #include <asm/abs_addr.h>
  73. #include <asm/iSeries/mf.h>
  74. #include <asm/uaccess.h>
  75. #include <asm/iseries/hv_lp_config.h>
  76. #include <asm/iSeries/HvTypes.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 work_struct 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_work(&cnx->statemachine_wq);
  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, struct pt_regs *regs)
  497. {
  498. struct veth_lpevent *veth_event = (struct veth_lpevent *)event;
  499. if (event->xFlags.xFunction == HvLpEvent_Function_Ack)
  500. veth_handle_ack(veth_event);
  501. else if (event->xFlags.xFunction == HvLpEvent_Function_Int)
  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(void *p)
  537. {
  538. struct veth_lpar_connection *cnx = (struct veth_lpar_connection *)p;
  539. int rlp = cnx->remote_lp;
  540. int rc;
  541. spin_lock_irq(&cnx->lock);
  542. restart:
  543. if (cnx->state & VETH_STATE_RESET) {
  544. if (cnx->state & VETH_STATE_OPEN)
  545. HvCallEvent_closeLpEventPath(cnx->remote_lp,
  546. HvLpEvent_Type_VirtualLan);
  547. /*
  548. * Reset ack data. This prevents the ack_timer actually
  549. * doing anything, even if it runs one more time when
  550. * we drop the lock below.
  551. */
  552. memset(&cnx->pending_acks, 0xff, sizeof (cnx->pending_acks));
  553. cnx->num_pending_acks = 0;
  554. cnx->state &= ~(VETH_STATE_RESET | VETH_STATE_SENTMON
  555. | VETH_STATE_OPEN | VETH_STATE_SENTCAPS
  556. | VETH_STATE_GOTCAPACK | VETH_STATE_GOTCAPS
  557. | VETH_STATE_SENTCAPACK | VETH_STATE_READY);
  558. /* Clean up any leftover messages */
  559. if (cnx->msgs) {
  560. int i;
  561. for (i = 0; i < VETH_NUMBUFFERS; ++i)
  562. veth_recycle_msg(cnx, cnx->msgs + i);
  563. }
  564. cnx->outstanding_tx = 0;
  565. veth_wake_queues(cnx);
  566. /* Drop the lock so we can do stuff that might sleep or
  567. * take other locks. */
  568. spin_unlock_irq(&cnx->lock);
  569. del_timer_sync(&cnx->ack_timer);
  570. del_timer_sync(&cnx->reset_timer);
  571. spin_lock_irq(&cnx->lock);
  572. if (cnx->state & VETH_STATE_RESET)
  573. goto restart;
  574. /* Hack, wait for the other end to reset itself. */
  575. if (! (cnx->state & VETH_STATE_SHUTDOWN)) {
  576. schedule_delayed_work(&cnx->statemachine_wq, 5 * HZ);
  577. goto out;
  578. }
  579. }
  580. if (cnx->state & VETH_STATE_SHUTDOWN)
  581. /* It's all over, do nothing */
  582. goto out;
  583. if ( !(cnx->state & VETH_STATE_OPEN) ) {
  584. if (! cnx->msgs || (cnx->num_events < (2 + VETH_NUMBUFFERS)) )
  585. goto cant_cope;
  586. HvCallEvent_openLpEventPath(rlp, HvLpEvent_Type_VirtualLan);
  587. cnx->src_inst =
  588. HvCallEvent_getSourceLpInstanceId(rlp,
  589. HvLpEvent_Type_VirtualLan);
  590. cnx->dst_inst =
  591. HvCallEvent_getTargetLpInstanceId(rlp,
  592. HvLpEvent_Type_VirtualLan);
  593. cnx->state |= VETH_STATE_OPEN;
  594. }
  595. if ( (cnx->state & VETH_STATE_OPEN)
  596. && !(cnx->state & VETH_STATE_SENTMON) ) {
  597. rc = veth_signalevent(cnx, VETH_EVENT_MONITOR,
  598. HvLpEvent_AckInd_DoAck,
  599. HvLpEvent_AckType_DeferredAck,
  600. 0, 0, 0, 0, 0, 0);
  601. if (rc == HvLpEvent_Rc_Good) {
  602. cnx->state |= VETH_STATE_SENTMON;
  603. } else {
  604. if ( (rc != HvLpEvent_Rc_PartitionDead)
  605. && (rc != HvLpEvent_Rc_PathClosed) )
  606. veth_error("Error sending monitor to LPAR %d, "
  607. "rc = %d\n", rlp, rc);
  608. /* Oh well, hope we get a cap from the other
  609. * end and do better when that kicks us */
  610. goto out;
  611. }
  612. }
  613. if ( (cnx->state & VETH_STATE_OPEN)
  614. && !(cnx->state & VETH_STATE_SENTCAPS)) {
  615. u64 *rawcap = (u64 *)&cnx->local_caps;
  616. rc = veth_signalevent(cnx, VETH_EVENT_CAP,
  617. HvLpEvent_AckInd_DoAck,
  618. HvLpEvent_AckType_ImmediateAck,
  619. 0, rawcap[0], rawcap[1], rawcap[2],
  620. rawcap[3], rawcap[4]);
  621. if (rc == HvLpEvent_Rc_Good) {
  622. cnx->state |= VETH_STATE_SENTCAPS;
  623. } else {
  624. if ( (rc != HvLpEvent_Rc_PartitionDead)
  625. && (rc != HvLpEvent_Rc_PathClosed) )
  626. veth_error("Error sending caps to LPAR %d, "
  627. "rc = %d\n", rlp, rc);
  628. /* Oh well, hope we get a cap from the other
  629. * end and do better when that kicks us */
  630. goto out;
  631. }
  632. }
  633. if ((cnx->state & VETH_STATE_GOTCAPS)
  634. && !(cnx->state & VETH_STATE_SENTCAPACK)) {
  635. struct veth_cap_data *remote_caps = &cnx->remote_caps;
  636. memcpy(remote_caps, &cnx->cap_event.u.caps_data,
  637. sizeof(*remote_caps));
  638. spin_unlock_irq(&cnx->lock);
  639. rc = veth_process_caps(cnx);
  640. spin_lock_irq(&cnx->lock);
  641. /* We dropped the lock, so recheck for anything which
  642. * might mess us up */
  643. if (cnx->state & (VETH_STATE_RESET|VETH_STATE_SHUTDOWN))
  644. goto restart;
  645. cnx->cap_event.base_event.xRc = rc;
  646. HvCallEvent_ackLpEvent((struct HvLpEvent *)&cnx->cap_event);
  647. if (rc == HvLpEvent_Rc_Good)
  648. cnx->state |= VETH_STATE_SENTCAPACK;
  649. else
  650. goto cant_cope;
  651. }
  652. if ((cnx->state & VETH_STATE_GOTCAPACK)
  653. && (cnx->state & VETH_STATE_GOTCAPS)
  654. && !(cnx->state & VETH_STATE_READY)) {
  655. if (cnx->cap_ack_event.base_event.xRc == HvLpEvent_Rc_Good) {
  656. /* Start the ACK timer */
  657. cnx->ack_timer.expires = jiffies + cnx->ack_timeout;
  658. add_timer(&cnx->ack_timer);
  659. cnx->state |= VETH_STATE_READY;
  660. } else {
  661. veth_error("Caps rejected by LPAR %d, rc = %d\n",
  662. rlp, cnx->cap_ack_event.base_event.xRc);
  663. goto cant_cope;
  664. }
  665. }
  666. out:
  667. spin_unlock_irq(&cnx->lock);
  668. return;
  669. cant_cope:
  670. /* FIXME: we get here if something happens we really can't
  671. * cope with. The link will never work once we get here, and
  672. * all we can do is not lock the rest of the system up */
  673. veth_error("Unrecoverable error on connection to LPAR %d, shutting down"
  674. " (state = 0x%04lx)\n", rlp, cnx->state);
  675. cnx->state |= VETH_STATE_SHUTDOWN;
  676. spin_unlock_irq(&cnx->lock);
  677. }
  678. static int veth_init_connection(u8 rlp)
  679. {
  680. struct veth_lpar_connection *cnx;
  681. struct veth_msg *msgs;
  682. int i, rc;
  683. if ( (rlp == this_lp)
  684. || ! HvLpConfig_doLpsCommunicateOnVirtualLan(this_lp, rlp) )
  685. return 0;
  686. cnx = kmalloc(sizeof(*cnx), GFP_KERNEL);
  687. if (! cnx)
  688. return -ENOMEM;
  689. memset(cnx, 0, sizeof(*cnx));
  690. cnx->remote_lp = rlp;
  691. spin_lock_init(&cnx->lock);
  692. INIT_WORK(&cnx->statemachine_wq, veth_statemachine, cnx);
  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 = kmalloc(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. memset(msgs, 0, VETH_NUMBUFFERS * sizeof(struct veth_msg));
  716. for (i = 0; i < VETH_NUMBUFFERS; i++) {
  717. msgs[i].token = i;
  718. veth_stack_push(cnx, msgs + i);
  719. }
  720. cnx->num_events = veth_allocate_events(rlp, 2 + VETH_NUMBUFFERS);
  721. if (cnx->num_events < (2 + VETH_NUMBUFFERS)) {
  722. veth_error("Can't allocate enough events for LPAR %d.\n", rlp);
  723. return -ENOMEM;
  724. }
  725. cnx->local_caps.num_buffers = VETH_NUMBUFFERS;
  726. cnx->local_caps.ack_threshold = ACK_THRESHOLD;
  727. cnx->local_caps.ack_timeout = VETH_ACKTIMEOUT;
  728. return 0;
  729. }
  730. static void veth_stop_connection(struct veth_lpar_connection *cnx)
  731. {
  732. if (!cnx)
  733. return;
  734. spin_lock_irq(&cnx->lock);
  735. cnx->state |= VETH_STATE_RESET | VETH_STATE_SHUTDOWN;
  736. veth_kick_statemachine(cnx);
  737. spin_unlock_irq(&cnx->lock);
  738. /* There's a slim chance the reset code has just queued the
  739. * statemachine to run in five seconds. If so we need to cancel
  740. * that and requeue the work to run now. */
  741. if (cancel_delayed_work(&cnx->statemachine_wq)) {
  742. spin_lock_irq(&cnx->lock);
  743. veth_kick_statemachine(cnx);
  744. spin_unlock_irq(&cnx->lock);
  745. }
  746. /* Wait for the state machine to run. */
  747. flush_scheduled_work();
  748. }
  749. static void veth_destroy_connection(struct veth_lpar_connection *cnx)
  750. {
  751. if (!cnx)
  752. return;
  753. if (cnx->num_events > 0)
  754. mf_deallocate_lp_events(cnx->remote_lp,
  755. HvLpEvent_Type_VirtualLan,
  756. cnx->num_events,
  757. NULL, NULL);
  758. if (cnx->num_ack_events > 0)
  759. mf_deallocate_lp_events(cnx->remote_lp,
  760. HvLpEvent_Type_VirtualLan,
  761. cnx->num_ack_events,
  762. NULL, NULL);
  763. kfree(cnx->msgs);
  764. veth_cnx[cnx->remote_lp] = NULL;
  765. kfree(cnx);
  766. }
  767. static void veth_release_connection(struct kobject *kobj)
  768. {
  769. struct veth_lpar_connection *cnx;
  770. cnx = container_of(kobj, struct veth_lpar_connection, kobject);
  771. veth_stop_connection(cnx);
  772. veth_destroy_connection(cnx);
  773. }
  774. /*
  775. * net_device code
  776. */
  777. static int veth_open(struct net_device *dev)
  778. {
  779. struct veth_port *port = (struct veth_port *) dev->priv;
  780. memset(&port->stats, 0, sizeof (port->stats));
  781. netif_start_queue(dev);
  782. return 0;
  783. }
  784. static int veth_close(struct net_device *dev)
  785. {
  786. netif_stop_queue(dev);
  787. return 0;
  788. }
  789. static struct net_device_stats *veth_get_stats(struct net_device *dev)
  790. {
  791. struct veth_port *port = (struct veth_port *) dev->priv;
  792. return &port->stats;
  793. }
  794. static int veth_change_mtu(struct net_device *dev, int new_mtu)
  795. {
  796. if ((new_mtu < 68) || (new_mtu > VETH_MAX_MTU))
  797. return -EINVAL;
  798. dev->mtu = new_mtu;
  799. return 0;
  800. }
  801. static void veth_set_multicast_list(struct net_device *dev)
  802. {
  803. struct veth_port *port = (struct veth_port *) dev->priv;
  804. unsigned long flags;
  805. write_lock_irqsave(&port->mcast_gate, flags);
  806. if ((dev->flags & IFF_PROMISC) || (dev->flags & IFF_ALLMULTI) ||
  807. (dev->mc_count > VETH_MAX_MCAST)) {
  808. port->promiscuous = 1;
  809. } else {
  810. struct dev_mc_list *dmi = dev->mc_list;
  811. int i;
  812. port->promiscuous = 0;
  813. /* Update table */
  814. port->num_mcast = 0;
  815. for (i = 0; i < dev->mc_count; i++) {
  816. u8 *addr = dmi->dmi_addr;
  817. u64 xaddr = 0;
  818. if (addr[0] & 0x01) {/* multicast address? */
  819. memcpy(&xaddr, addr, ETH_ALEN);
  820. port->mcast_addr[port->num_mcast] = xaddr;
  821. port->num_mcast++;
  822. }
  823. dmi = dmi->next;
  824. }
  825. }
  826. write_unlock_irqrestore(&port->mcast_gate, flags);
  827. }
  828. static void veth_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  829. {
  830. strncpy(info->driver, DRV_NAME, sizeof(info->driver) - 1);
  831. info->driver[sizeof(info->driver) - 1] = '\0';
  832. strncpy(info->version, DRV_VERSION, sizeof(info->version) - 1);
  833. info->version[sizeof(info->version) - 1] = '\0';
  834. }
  835. static int veth_get_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
  836. {
  837. ecmd->supported = (SUPPORTED_1000baseT_Full
  838. | SUPPORTED_Autoneg | SUPPORTED_FIBRE);
  839. ecmd->advertising = (SUPPORTED_1000baseT_Full
  840. | SUPPORTED_Autoneg | SUPPORTED_FIBRE);
  841. ecmd->port = PORT_FIBRE;
  842. ecmd->transceiver = XCVR_INTERNAL;
  843. ecmd->phy_address = 0;
  844. ecmd->speed = SPEED_1000;
  845. ecmd->duplex = DUPLEX_FULL;
  846. ecmd->autoneg = AUTONEG_ENABLE;
  847. ecmd->maxtxpkt = 120;
  848. ecmd->maxrxpkt = 120;
  849. return 0;
  850. }
  851. static u32 veth_get_link(struct net_device *dev)
  852. {
  853. return 1;
  854. }
  855. static struct ethtool_ops ops = {
  856. .get_drvinfo = veth_get_drvinfo,
  857. .get_settings = veth_get_settings,
  858. .get_link = veth_get_link,
  859. };
  860. static struct net_device * __init veth_probe_one(int vlan, struct device *vdev)
  861. {
  862. struct net_device *dev;
  863. struct veth_port *port;
  864. int i, rc;
  865. dev = alloc_etherdev(sizeof (struct veth_port));
  866. if (! dev) {
  867. veth_error("Unable to allocate net_device structure!\n");
  868. return NULL;
  869. }
  870. port = (struct veth_port *) dev->priv;
  871. spin_lock_init(&port->queue_lock);
  872. rwlock_init(&port->mcast_gate);
  873. port->stopped_map = 0;
  874. for (i = 0; i < HVMAXARCHITECTEDLPS; i++) {
  875. HvLpVirtualLanIndexMap map;
  876. if (i == this_lp)
  877. continue;
  878. map = HvLpConfig_getVirtualLanIndexMapForLp(i);
  879. if (map & (0x8000 >> vlan))
  880. port->lpar_map |= (1 << i);
  881. }
  882. port->dev = vdev;
  883. dev->dev_addr[0] = 0x02;
  884. dev->dev_addr[1] = 0x01;
  885. dev->dev_addr[2] = 0xff;
  886. dev->dev_addr[3] = vlan;
  887. dev->dev_addr[4] = 0xff;
  888. dev->dev_addr[5] = this_lp;
  889. dev->mtu = VETH_MAX_MTU;
  890. memcpy(&port->mac_addr, dev->dev_addr, 6);
  891. dev->open = veth_open;
  892. dev->hard_start_xmit = veth_start_xmit;
  893. dev->stop = veth_close;
  894. dev->get_stats = veth_get_stats;
  895. dev->change_mtu = veth_change_mtu;
  896. dev->set_mac_address = NULL;
  897. dev->set_multicast_list = veth_set_multicast_list;
  898. SET_ETHTOOL_OPS(dev, &ops);
  899. SET_NETDEV_DEV(dev, vdev);
  900. rc = register_netdev(dev);
  901. if (rc != 0) {
  902. veth_error("Failed registering net device for vlan%d.\n", vlan);
  903. free_netdev(dev);
  904. return NULL;
  905. }
  906. kobject_init(&port->kobject);
  907. port->kobject.parent = &dev->class_dev.kobj;
  908. port->kobject.ktype = &veth_port_ktype;
  909. kobject_set_name(&port->kobject, "veth_port");
  910. if (0 != kobject_add(&port->kobject))
  911. veth_error("Failed adding port for %s to sysfs.\n", dev->name);
  912. veth_info("%s attached to iSeries vlan %d (LPAR map = 0x%.4X)\n",
  913. dev->name, vlan, port->lpar_map);
  914. return dev;
  915. }
  916. /*
  917. * Tx path
  918. */
  919. static int veth_transmit_to_one(struct sk_buff *skb, HvLpIndex rlp,
  920. struct net_device *dev)
  921. {
  922. struct veth_lpar_connection *cnx = veth_cnx[rlp];
  923. struct veth_port *port = (struct veth_port *) dev->priv;
  924. HvLpEvent_Rc rc;
  925. struct veth_msg *msg = NULL;
  926. unsigned long flags;
  927. if (! cnx)
  928. return 0;
  929. spin_lock_irqsave(&cnx->lock, flags);
  930. if (! (cnx->state & VETH_STATE_READY))
  931. goto no_error;
  932. if ((skb->len - ETH_HLEN) > VETH_MAX_MTU)
  933. goto drop;
  934. msg = veth_stack_pop(cnx);
  935. if (! msg)
  936. goto drop;
  937. msg->in_use = 1;
  938. msg->skb = skb_get(skb);
  939. msg->data.addr[0] = dma_map_single(port->dev, skb->data,
  940. skb->len, DMA_TO_DEVICE);
  941. if (dma_mapping_error(msg->data.addr[0]))
  942. goto recycle_and_drop;
  943. msg->dev = port->dev;
  944. msg->data.len[0] = skb->len;
  945. msg->data.eofmask = 1 << VETH_EOF_SHIFT;
  946. rc = veth_signaldata(cnx, VETH_EVENT_FRAMES, msg->token, &msg->data);
  947. if (rc != HvLpEvent_Rc_Good)
  948. goto recycle_and_drop;
  949. /* If the timer's not already running, start it now. */
  950. if (0 == cnx->outstanding_tx)
  951. mod_timer(&cnx->reset_timer, jiffies + cnx->reset_timeout);
  952. cnx->last_contact = jiffies;
  953. cnx->outstanding_tx++;
  954. if (veth_stack_is_empty(cnx))
  955. veth_stop_queues(cnx);
  956. no_error:
  957. spin_unlock_irqrestore(&cnx->lock, flags);
  958. return 0;
  959. recycle_and_drop:
  960. veth_recycle_msg(cnx, msg);
  961. drop:
  962. spin_unlock_irqrestore(&cnx->lock, flags);
  963. return 1;
  964. }
  965. static void veth_transmit_to_many(struct sk_buff *skb,
  966. HvLpIndexMap lpmask,
  967. struct net_device *dev)
  968. {
  969. struct veth_port *port = (struct veth_port *) dev->priv;
  970. int i, success, error;
  971. success = error = 0;
  972. for (i = 0; i < HVMAXARCHITECTEDLPS; i++) {
  973. if ((lpmask & (1 << i)) == 0)
  974. continue;
  975. if (veth_transmit_to_one(skb, i, dev))
  976. error = 1;
  977. else
  978. success = 1;
  979. }
  980. if (error)
  981. port->stats.tx_errors++;
  982. if (success) {
  983. port->stats.tx_packets++;
  984. port->stats.tx_bytes += skb->len;
  985. }
  986. }
  987. static int veth_start_xmit(struct sk_buff *skb, struct net_device *dev)
  988. {
  989. unsigned char *frame = skb->data;
  990. struct veth_port *port = (struct veth_port *) dev->priv;
  991. HvLpIndexMap lpmask;
  992. if (! (frame[0] & 0x01)) {
  993. /* unicast packet */
  994. HvLpIndex rlp = frame[5];
  995. if ( ! ((1 << rlp) & port->lpar_map) ) {
  996. dev_kfree_skb(skb);
  997. return 0;
  998. }
  999. lpmask = 1 << rlp;
  1000. } else {
  1001. lpmask = port->lpar_map;
  1002. }
  1003. veth_transmit_to_many(skb, lpmask, dev);
  1004. dev_kfree_skb(skb);
  1005. return 0;
  1006. }
  1007. /* You must hold the connection's lock when you call this function. */
  1008. static void veth_recycle_msg(struct veth_lpar_connection *cnx,
  1009. struct veth_msg *msg)
  1010. {
  1011. u32 dma_address, dma_length;
  1012. if (msg->in_use) {
  1013. msg->in_use = 0;
  1014. dma_address = msg->data.addr[0];
  1015. dma_length = msg->data.len[0];
  1016. if (!dma_mapping_error(dma_address))
  1017. dma_unmap_single(msg->dev, dma_address, dma_length,
  1018. DMA_TO_DEVICE);
  1019. if (msg->skb) {
  1020. dev_kfree_skb_any(msg->skb);
  1021. msg->skb = NULL;
  1022. }
  1023. memset(&msg->data, 0, sizeof(msg->data));
  1024. veth_stack_push(cnx, msg);
  1025. } else if (cnx->state & VETH_STATE_OPEN) {
  1026. veth_error("Non-pending frame (# %d) acked by LPAR %d.\n",
  1027. cnx->remote_lp, msg->token);
  1028. }
  1029. }
  1030. static void veth_wake_queues(struct veth_lpar_connection *cnx)
  1031. {
  1032. int i;
  1033. for (i = 0; i < HVMAXARCHITECTEDVIRTUALLANS; i++) {
  1034. struct net_device *dev = veth_dev[i];
  1035. struct veth_port *port;
  1036. unsigned long flags;
  1037. if (! dev)
  1038. continue;
  1039. port = (struct veth_port *)dev->priv;
  1040. if (! (port->lpar_map & (1<<cnx->remote_lp)))
  1041. continue;
  1042. spin_lock_irqsave(&port->queue_lock, flags);
  1043. port->stopped_map &= ~(1 << cnx->remote_lp);
  1044. if (0 == port->stopped_map && netif_queue_stopped(dev)) {
  1045. veth_debug("cnx %d: woke queue for %s.\n",
  1046. cnx->remote_lp, dev->name);
  1047. netif_wake_queue(dev);
  1048. }
  1049. spin_unlock_irqrestore(&port->queue_lock, flags);
  1050. }
  1051. }
  1052. static void veth_stop_queues(struct veth_lpar_connection *cnx)
  1053. {
  1054. int i;
  1055. for (i = 0; i < HVMAXARCHITECTEDVIRTUALLANS; i++) {
  1056. struct net_device *dev = veth_dev[i];
  1057. struct veth_port *port;
  1058. if (! dev)
  1059. continue;
  1060. port = (struct veth_port *)dev->priv;
  1061. /* If this cnx is not on the vlan for this port, continue */
  1062. if (! (port->lpar_map & (1 << cnx->remote_lp)))
  1063. continue;
  1064. spin_lock(&port->queue_lock);
  1065. netif_stop_queue(dev);
  1066. port->stopped_map |= (1 << cnx->remote_lp);
  1067. veth_debug("cnx %d: stopped queue for %s, map = 0x%x.\n",
  1068. cnx->remote_lp, dev->name, port->stopped_map);
  1069. spin_unlock(&port->queue_lock);
  1070. }
  1071. }
  1072. static void veth_timed_reset(unsigned long ptr)
  1073. {
  1074. struct veth_lpar_connection *cnx = (struct veth_lpar_connection *)ptr;
  1075. unsigned long trigger_time, flags;
  1076. /* FIXME is it possible this fires after veth_stop_connection()?
  1077. * That would reschedule the statemachine for 5 seconds and probably
  1078. * execute it after the module's been unloaded. Hmm. */
  1079. spin_lock_irqsave(&cnx->lock, flags);
  1080. if (cnx->outstanding_tx > 0) {
  1081. trigger_time = cnx->last_contact + cnx->reset_timeout;
  1082. if (trigger_time < jiffies) {
  1083. cnx->state |= VETH_STATE_RESET;
  1084. veth_kick_statemachine(cnx);
  1085. veth_error("%d packets not acked by LPAR %d within %d "
  1086. "seconds, resetting.\n",
  1087. cnx->outstanding_tx, cnx->remote_lp,
  1088. cnx->reset_timeout / HZ);
  1089. } else {
  1090. /* Reschedule the timer */
  1091. trigger_time = jiffies + cnx->reset_timeout;
  1092. mod_timer(&cnx->reset_timer, trigger_time);
  1093. }
  1094. }
  1095. spin_unlock_irqrestore(&cnx->lock, flags);
  1096. }
  1097. /*
  1098. * Rx path
  1099. */
  1100. static inline int veth_frame_wanted(struct veth_port *port, u64 mac_addr)
  1101. {
  1102. int wanted = 0;
  1103. int i;
  1104. unsigned long flags;
  1105. if ( (mac_addr == port->mac_addr) || (mac_addr == 0xffffffffffff0000) )
  1106. return 1;
  1107. read_lock_irqsave(&port->mcast_gate, flags);
  1108. if (port->promiscuous) {
  1109. wanted = 1;
  1110. goto out;
  1111. }
  1112. for (i = 0; i < port->num_mcast; ++i) {
  1113. if (port->mcast_addr[i] == mac_addr) {
  1114. wanted = 1;
  1115. break;
  1116. }
  1117. }
  1118. out:
  1119. read_unlock_irqrestore(&port->mcast_gate, flags);
  1120. return wanted;
  1121. }
  1122. struct dma_chunk {
  1123. u64 addr;
  1124. u64 size;
  1125. };
  1126. #define VETH_MAX_PAGES_PER_FRAME ( (VETH_MAX_MTU+PAGE_SIZE-2)/PAGE_SIZE + 1 )
  1127. static inline void veth_build_dma_list(struct dma_chunk *list,
  1128. unsigned char *p, unsigned long length)
  1129. {
  1130. unsigned long done;
  1131. int i = 1;
  1132. /* FIXME: skbs are continguous in real addresses. Do we
  1133. * really need to break it into PAGE_SIZE chunks, or can we do
  1134. * it just at the granularity of iSeries real->absolute
  1135. * mapping? Indeed, given the way the allocator works, can we
  1136. * count on them being absolutely contiguous? */
  1137. list[0].addr = iseries_hv_addr(p);
  1138. list[0].size = min(length,
  1139. PAGE_SIZE - ((unsigned long)p & ~PAGE_MASK));
  1140. done = list[0].size;
  1141. while (done < length) {
  1142. list[i].addr = iseries_hv_addr(p + done);
  1143. list[i].size = min(length-done, PAGE_SIZE);
  1144. done += list[i].size;
  1145. i++;
  1146. }
  1147. }
  1148. static void veth_flush_acks(struct veth_lpar_connection *cnx)
  1149. {
  1150. HvLpEvent_Rc rc;
  1151. rc = veth_signaldata(cnx, VETH_EVENT_FRAMES_ACK,
  1152. 0, &cnx->pending_acks);
  1153. if (rc != HvLpEvent_Rc_Good)
  1154. veth_error("Failed acking frames from LPAR %d, rc = %d\n",
  1155. cnx->remote_lp, (int)rc);
  1156. cnx->num_pending_acks = 0;
  1157. memset(&cnx->pending_acks, 0xff, sizeof(cnx->pending_acks));
  1158. }
  1159. static void veth_receive(struct veth_lpar_connection *cnx,
  1160. struct veth_lpevent *event)
  1161. {
  1162. struct veth_frames_data *senddata = &event->u.frames_data;
  1163. int startchunk = 0;
  1164. int nchunks;
  1165. unsigned long flags;
  1166. HvLpDma_Rc rc;
  1167. do {
  1168. u16 length = 0;
  1169. struct sk_buff *skb;
  1170. struct dma_chunk local_list[VETH_MAX_PAGES_PER_FRAME];
  1171. struct dma_chunk remote_list[VETH_MAX_FRAMES_PER_MSG];
  1172. u64 dest;
  1173. HvLpVirtualLanIndex vlan;
  1174. struct net_device *dev;
  1175. struct veth_port *port;
  1176. /* FIXME: do we need this? */
  1177. memset(local_list, 0, sizeof(local_list));
  1178. memset(remote_list, 0, sizeof(VETH_MAX_FRAMES_PER_MSG));
  1179. /* a 0 address marks the end of the valid entries */
  1180. if (senddata->addr[startchunk] == 0)
  1181. break;
  1182. /* make sure that we have at least 1 EOF entry in the
  1183. * remaining entries */
  1184. if (! (senddata->eofmask >> (startchunk + VETH_EOF_SHIFT))) {
  1185. veth_error("Missing EOF fragment in event "
  1186. "eofmask = 0x%x startchunk = %d\n",
  1187. (unsigned)senddata->eofmask,
  1188. startchunk);
  1189. break;
  1190. }
  1191. /* build list of chunks in this frame */
  1192. nchunks = 0;
  1193. do {
  1194. remote_list[nchunks].addr =
  1195. (u64) senddata->addr[startchunk+nchunks] << 32;
  1196. remote_list[nchunks].size =
  1197. senddata->len[startchunk+nchunks];
  1198. length += remote_list[nchunks].size;
  1199. } while (! (senddata->eofmask &
  1200. (1 << (VETH_EOF_SHIFT + startchunk + nchunks++))));
  1201. /* length == total length of all chunks */
  1202. /* nchunks == # of chunks in this frame */
  1203. if ((length - ETH_HLEN) > VETH_MAX_MTU) {
  1204. veth_error("Received oversize frame from LPAR %d "
  1205. "(length = %d)\n",
  1206. cnx->remote_lp, length);
  1207. continue;
  1208. }
  1209. skb = alloc_skb(length, GFP_ATOMIC);
  1210. if (!skb)
  1211. continue;
  1212. veth_build_dma_list(local_list, skb->data, length);
  1213. rc = HvCallEvent_dmaBufList(HvLpEvent_Type_VirtualLan,
  1214. event->base_event.xSourceLp,
  1215. HvLpDma_Direction_RemoteToLocal,
  1216. cnx->src_inst,
  1217. cnx->dst_inst,
  1218. HvLpDma_AddressType_RealAddress,
  1219. HvLpDma_AddressType_TceIndex,
  1220. iseries_hv_addr(&local_list),
  1221. iseries_hv_addr(&remote_list),
  1222. length);
  1223. if (rc != HvLpDma_Rc_Good) {
  1224. dev_kfree_skb_irq(skb);
  1225. continue;
  1226. }
  1227. vlan = skb->data[9];
  1228. dev = veth_dev[vlan];
  1229. if (! dev) {
  1230. /*
  1231. * Some earlier versions of the driver sent
  1232. * broadcasts down all connections, even to lpars
  1233. * that weren't on the relevant vlan. So ignore
  1234. * packets belonging to a vlan we're not on.
  1235. * We can also be here if we receive packets while
  1236. * the driver is going down, because then dev is NULL.
  1237. */
  1238. dev_kfree_skb_irq(skb);
  1239. continue;
  1240. }
  1241. port = (struct veth_port *)dev->priv;
  1242. dest = *((u64 *) skb->data) & 0xFFFFFFFFFFFF0000;
  1243. if ((vlan > HVMAXARCHITECTEDVIRTUALLANS) || !port) {
  1244. dev_kfree_skb_irq(skb);
  1245. continue;
  1246. }
  1247. if (! veth_frame_wanted(port, dest)) {
  1248. dev_kfree_skb_irq(skb);
  1249. continue;
  1250. }
  1251. skb_put(skb, length);
  1252. skb->dev = dev;
  1253. skb->protocol = eth_type_trans(skb, dev);
  1254. skb->ip_summed = CHECKSUM_NONE;
  1255. netif_rx(skb); /* send it up */
  1256. port->stats.rx_packets++;
  1257. port->stats.rx_bytes += length;
  1258. } while (startchunk += nchunks, startchunk < VETH_MAX_FRAMES_PER_MSG);
  1259. /* Ack it */
  1260. spin_lock_irqsave(&cnx->lock, flags);
  1261. BUG_ON(cnx->num_pending_acks > VETH_MAX_ACKS_PER_MSG);
  1262. cnx->pending_acks[cnx->num_pending_acks++] =
  1263. event->base_event.xCorrelationToken;
  1264. if ( (cnx->num_pending_acks >= cnx->remote_caps.ack_threshold)
  1265. || (cnx->num_pending_acks >= VETH_MAX_ACKS_PER_MSG) )
  1266. veth_flush_acks(cnx);
  1267. spin_unlock_irqrestore(&cnx->lock, flags);
  1268. }
  1269. static void veth_timed_ack(unsigned long ptr)
  1270. {
  1271. struct veth_lpar_connection *cnx = (struct veth_lpar_connection *) ptr;
  1272. unsigned long flags;
  1273. /* Ack all the events */
  1274. spin_lock_irqsave(&cnx->lock, flags);
  1275. if (cnx->num_pending_acks > 0)
  1276. veth_flush_acks(cnx);
  1277. /* Reschedule the timer */
  1278. cnx->ack_timer.expires = jiffies + cnx->ack_timeout;
  1279. add_timer(&cnx->ack_timer);
  1280. spin_unlock_irqrestore(&cnx->lock, flags);
  1281. }
  1282. static int veth_remove(struct vio_dev *vdev)
  1283. {
  1284. struct veth_lpar_connection *cnx;
  1285. struct net_device *dev;
  1286. struct veth_port *port;
  1287. int i;
  1288. dev = veth_dev[vdev->unit_address];
  1289. if (! dev)
  1290. return 0;
  1291. port = netdev_priv(dev);
  1292. for (i = 0; i < HVMAXARCHITECTEDLPS; i++) {
  1293. cnx = veth_cnx[i];
  1294. if (cnx && (port->lpar_map & (1 << i))) {
  1295. /* Drop our reference to connections on our VLAN */
  1296. kobject_put(&cnx->kobject);
  1297. }
  1298. }
  1299. veth_dev[vdev->unit_address] = NULL;
  1300. kobject_del(&port->kobject);
  1301. kobject_put(&port->kobject);
  1302. unregister_netdev(dev);
  1303. free_netdev(dev);
  1304. return 0;
  1305. }
  1306. static int veth_probe(struct vio_dev *vdev, const struct vio_device_id *id)
  1307. {
  1308. int i = vdev->unit_address;
  1309. struct net_device *dev;
  1310. struct veth_port *port;
  1311. dev = veth_probe_one(i, &vdev->dev);
  1312. if (dev == NULL) {
  1313. veth_remove(vdev);
  1314. return 1;
  1315. }
  1316. veth_dev[i] = dev;
  1317. port = (struct veth_port*)netdev_priv(dev);
  1318. /* Start the state machine on each connection on this vlan. If we're
  1319. * the first dev to do so this will commence link negotiation */
  1320. for (i = 0; i < HVMAXARCHITECTEDLPS; i++) {
  1321. struct veth_lpar_connection *cnx;
  1322. if (! (port->lpar_map & (1 << i)))
  1323. continue;
  1324. cnx = veth_cnx[i];
  1325. if (!cnx)
  1326. continue;
  1327. kobject_get(&cnx->kobject);
  1328. veth_kick_statemachine(cnx);
  1329. }
  1330. return 0;
  1331. }
  1332. /**
  1333. * veth_device_table: Used by vio.c to match devices that we
  1334. * support.
  1335. */
  1336. static struct vio_device_id veth_device_table[] __devinitdata = {
  1337. { "vlan", "" },
  1338. { "", "" }
  1339. };
  1340. MODULE_DEVICE_TABLE(vio, veth_device_table);
  1341. static struct vio_driver veth_driver = {
  1342. .id_table = veth_device_table,
  1343. .probe = veth_probe,
  1344. .remove = veth_remove,
  1345. .driver = {
  1346. .name = DRV_NAME,
  1347. .owner = THIS_MODULE,
  1348. }
  1349. };
  1350. /*
  1351. * Module initialization/cleanup
  1352. */
  1353. void __exit veth_module_cleanup(void)
  1354. {
  1355. int i;
  1356. struct veth_lpar_connection *cnx;
  1357. /* Disconnect our "irq" to stop events coming from the Hypervisor. */
  1358. HvLpEvent_unregisterHandler(HvLpEvent_Type_VirtualLan);
  1359. /* Make sure any work queued from Hypervisor callbacks is finished. */
  1360. flush_scheduled_work();
  1361. for (i = 0; i < HVMAXARCHITECTEDLPS; ++i) {
  1362. cnx = veth_cnx[i];
  1363. if (!cnx)
  1364. continue;
  1365. /* Remove the connection from sysfs */
  1366. kobject_del(&cnx->kobject);
  1367. /* Drop the driver's reference to the connection */
  1368. kobject_put(&cnx->kobject);
  1369. }
  1370. /* Unregister the driver, which will close all the netdevs and stop
  1371. * the connections when they're no longer referenced. */
  1372. vio_unregister_driver(&veth_driver);
  1373. }
  1374. module_exit(veth_module_cleanup);
  1375. int __init veth_module_init(void)
  1376. {
  1377. int i;
  1378. int rc;
  1379. this_lp = HvLpConfig_getLpIndex_outline();
  1380. for (i = 0; i < HVMAXARCHITECTEDLPS; ++i) {
  1381. rc = veth_init_connection(i);
  1382. if (rc != 0)
  1383. goto error;
  1384. }
  1385. HvLpEvent_registerHandler(HvLpEvent_Type_VirtualLan,
  1386. &veth_handle_event);
  1387. rc = vio_register_driver(&veth_driver);
  1388. if (rc != 0)
  1389. goto error;
  1390. for (i = 0; i < HVMAXARCHITECTEDLPS; ++i) {
  1391. struct kobject *kobj;
  1392. if (!veth_cnx[i])
  1393. continue;
  1394. kobj = &veth_cnx[i]->kobject;
  1395. kobj->parent = &veth_driver.driver.kobj;
  1396. /* If the add failes, complain but otherwise continue */
  1397. if (0 != kobject_add(kobj))
  1398. veth_error("cnx %d: Failed adding to sysfs.\n", i);
  1399. }
  1400. return 0;
  1401. error:
  1402. for (i = 0; i < HVMAXARCHITECTEDLPS; ++i) {
  1403. veth_destroy_connection(veth_cnx[i]);
  1404. }
  1405. return rc;
  1406. }
  1407. module_init(veth_module_init);