iseries_veth.c 43 KB

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