iseries_veth.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422
  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. #include "iseries_veth.h"
  80. MODULE_AUTHOR("Kyle Lucke <klucke@us.ibm.com>");
  81. MODULE_DESCRIPTION("iSeries Virtual ethernet driver");
  82. MODULE_LICENSE("GPL");
  83. #define VETH_NUMBUFFERS (120)
  84. #define VETH_ACKTIMEOUT (1000000) /* microseconds */
  85. #define VETH_MAX_MCAST (12)
  86. #define VETH_MAX_MTU (9000)
  87. #if VETH_NUMBUFFERS < 10
  88. #define ACK_THRESHOLD (1)
  89. #elif VETH_NUMBUFFERS < 20
  90. #define ACK_THRESHOLD (4)
  91. #elif VETH_NUMBUFFERS < 40
  92. #define ACK_THRESHOLD (10)
  93. #else
  94. #define ACK_THRESHOLD (20)
  95. #endif
  96. #define VETH_STATE_SHUTDOWN (0x0001)
  97. #define VETH_STATE_OPEN (0x0002)
  98. #define VETH_STATE_RESET (0x0004)
  99. #define VETH_STATE_SENTMON (0x0008)
  100. #define VETH_STATE_SENTCAPS (0x0010)
  101. #define VETH_STATE_GOTCAPACK (0x0020)
  102. #define VETH_STATE_GOTCAPS (0x0040)
  103. #define VETH_STATE_SENTCAPACK (0x0080)
  104. #define VETH_STATE_READY (0x0100)
  105. struct veth_msg {
  106. struct veth_msg *next;
  107. struct VethFramesData data;
  108. int token;
  109. unsigned long in_use;
  110. struct sk_buff *skb;
  111. struct device *dev;
  112. };
  113. struct veth_lpar_connection {
  114. HvLpIndex remote_lp;
  115. struct work_struct statemachine_wq;
  116. struct veth_msg *msgs;
  117. int num_events;
  118. struct VethCapData local_caps;
  119. struct timer_list ack_timer;
  120. spinlock_t lock;
  121. unsigned long state;
  122. HvLpInstanceId src_inst;
  123. HvLpInstanceId dst_inst;
  124. struct VethLpEvent cap_event, cap_ack_event;
  125. u16 pending_acks[VETH_MAX_ACKS_PER_MSG];
  126. u32 num_pending_acks;
  127. int num_ack_events;
  128. struct VethCapData remote_caps;
  129. u32 ack_timeout;
  130. spinlock_t msg_stack_lock;
  131. struct veth_msg *msg_stack_head;
  132. };
  133. struct veth_port {
  134. struct device *dev;
  135. struct net_device_stats stats;
  136. u64 mac_addr;
  137. HvLpIndexMap lpar_map;
  138. spinlock_t pending_gate;
  139. struct sk_buff *pending_skb;
  140. HvLpIndexMap pending_lpmask;
  141. rwlock_t mcast_gate;
  142. int promiscuous;
  143. int all_mcast;
  144. int num_mcast;
  145. u64 mcast_addr[VETH_MAX_MCAST];
  146. };
  147. static HvLpIndex this_lp;
  148. static struct veth_lpar_connection *veth_cnx[HVMAXARCHITECTEDLPS]; /* = 0 */
  149. static struct net_device *veth_dev[HVMAXARCHITECTEDVIRTUALLANS]; /* = 0 */
  150. static int veth_start_xmit(struct sk_buff *skb, struct net_device *dev);
  151. static void veth_recycle_msg(struct veth_lpar_connection *, struct veth_msg *);
  152. static void veth_flush_pending(struct veth_lpar_connection *cnx);
  153. static void veth_receive(struct veth_lpar_connection *, struct VethLpEvent *);
  154. static void veth_timed_ack(unsigned long connectionPtr);
  155. /*
  156. * Utility functions
  157. */
  158. #define veth_printk(prio, fmt, args...) \
  159. printk(prio "%s: " fmt, __FILE__, ## args)
  160. #define veth_error(fmt, args...) \
  161. printk(KERN_ERR "(%s:%3.3d) ERROR: " fmt, __FILE__, __LINE__ , ## args)
  162. static inline void veth_stack_push(struct veth_lpar_connection *cnx,
  163. struct veth_msg *msg)
  164. {
  165. unsigned long flags;
  166. spin_lock_irqsave(&cnx->msg_stack_lock, flags);
  167. msg->next = cnx->msg_stack_head;
  168. cnx->msg_stack_head = msg;
  169. spin_unlock_irqrestore(&cnx->msg_stack_lock, flags);
  170. }
  171. static inline struct veth_msg *veth_stack_pop(struct veth_lpar_connection *cnx)
  172. {
  173. unsigned long flags;
  174. struct veth_msg *msg;
  175. spin_lock_irqsave(&cnx->msg_stack_lock, flags);
  176. msg = cnx->msg_stack_head;
  177. if (msg)
  178. cnx->msg_stack_head = cnx->msg_stack_head->next;
  179. spin_unlock_irqrestore(&cnx->msg_stack_lock, flags);
  180. return msg;
  181. }
  182. static inline HvLpEvent_Rc
  183. veth_signalevent(struct veth_lpar_connection *cnx, u16 subtype,
  184. HvLpEvent_AckInd ackind, HvLpEvent_AckType acktype,
  185. u64 token,
  186. u64 data1, u64 data2, u64 data3, u64 data4, u64 data5)
  187. {
  188. return HvCallEvent_signalLpEventFast(cnx->remote_lp,
  189. HvLpEvent_Type_VirtualLan,
  190. subtype, ackind, acktype,
  191. cnx->src_inst,
  192. cnx->dst_inst,
  193. token, data1, data2, data3,
  194. data4, data5);
  195. }
  196. static inline HvLpEvent_Rc veth_signaldata(struct veth_lpar_connection *cnx,
  197. u16 subtype, u64 token, void *data)
  198. {
  199. u64 *p = (u64 *) data;
  200. return veth_signalevent(cnx, subtype, HvLpEvent_AckInd_NoAck,
  201. HvLpEvent_AckType_ImmediateAck,
  202. token, p[0], p[1], p[2], p[3], p[4]);
  203. }
  204. struct veth_allocation {
  205. struct completion c;
  206. int num;
  207. };
  208. static void veth_complete_allocation(void *parm, int number)
  209. {
  210. struct veth_allocation *vc = (struct veth_allocation *)parm;
  211. vc->num = number;
  212. complete(&vc->c);
  213. }
  214. static int veth_allocate_events(HvLpIndex rlp, int number)
  215. {
  216. struct veth_allocation vc = { COMPLETION_INITIALIZER(vc.c), 0 };
  217. mf_allocate_lp_events(rlp, HvLpEvent_Type_VirtualLan,
  218. sizeof(struct VethLpEvent), number,
  219. &veth_complete_allocation, &vc);
  220. wait_for_completion(&vc.c);
  221. return vc.num;
  222. }
  223. /*
  224. * LPAR connection code
  225. */
  226. static inline void veth_kick_statemachine(struct veth_lpar_connection *cnx)
  227. {
  228. schedule_work(&cnx->statemachine_wq);
  229. }
  230. static void veth_take_cap(struct veth_lpar_connection *cnx,
  231. struct VethLpEvent *event)
  232. {
  233. unsigned long flags;
  234. spin_lock_irqsave(&cnx->lock, flags);
  235. /* Receiving caps may mean the other end has just come up, so
  236. * we need to reload the instance ID of the far end */
  237. cnx->dst_inst =
  238. HvCallEvent_getTargetLpInstanceId(cnx->remote_lp,
  239. HvLpEvent_Type_VirtualLan);
  240. if (cnx->state & VETH_STATE_GOTCAPS) {
  241. veth_error("Received a second capabilities from lpar %d\n",
  242. cnx->remote_lp);
  243. event->base_event.xRc = HvLpEvent_Rc_BufferNotAvailable;
  244. HvCallEvent_ackLpEvent((struct HvLpEvent *) event);
  245. } else {
  246. memcpy(&cnx->cap_event, event, sizeof(cnx->cap_event));
  247. cnx->state |= VETH_STATE_GOTCAPS;
  248. veth_kick_statemachine(cnx);
  249. }
  250. spin_unlock_irqrestore(&cnx->lock, flags);
  251. }
  252. static void veth_take_cap_ack(struct veth_lpar_connection *cnx,
  253. struct VethLpEvent *event)
  254. {
  255. unsigned long flags;
  256. spin_lock_irqsave(&cnx->lock, flags);
  257. if (cnx->state & VETH_STATE_GOTCAPACK) {
  258. veth_error("Received a second capabilities ack from lpar %d\n",
  259. cnx->remote_lp);
  260. } else {
  261. memcpy(&cnx->cap_ack_event, event,
  262. sizeof(&cnx->cap_ack_event));
  263. cnx->state |= VETH_STATE_GOTCAPACK;
  264. veth_kick_statemachine(cnx);
  265. }
  266. spin_unlock_irqrestore(&cnx->lock, flags);
  267. }
  268. static void veth_take_monitor_ack(struct veth_lpar_connection *cnx,
  269. struct VethLpEvent *event)
  270. {
  271. unsigned long flags;
  272. spin_lock_irqsave(&cnx->lock, flags);
  273. veth_printk(KERN_DEBUG, "Monitor ack returned for lpar %d\n",
  274. cnx->remote_lp);
  275. cnx->state |= VETH_STATE_RESET;
  276. veth_kick_statemachine(cnx);
  277. spin_unlock_irqrestore(&cnx->lock, flags);
  278. }
  279. static void veth_handle_ack(struct VethLpEvent *event)
  280. {
  281. HvLpIndex rlp = event->base_event.xTargetLp;
  282. struct veth_lpar_connection *cnx = veth_cnx[rlp];
  283. BUG_ON(! cnx);
  284. switch (event->base_event.xSubtype) {
  285. case VethEventTypeCap:
  286. veth_take_cap_ack(cnx, event);
  287. break;
  288. case VethEventTypeMonitor:
  289. veth_take_monitor_ack(cnx, event);
  290. break;
  291. default:
  292. veth_error("Unknown ack type %d from lpar %d\n",
  293. event->base_event.xSubtype, rlp);
  294. };
  295. }
  296. static void veth_handle_int(struct VethLpEvent *event)
  297. {
  298. HvLpIndex rlp = event->base_event.xSourceLp;
  299. struct veth_lpar_connection *cnx = veth_cnx[rlp];
  300. unsigned long flags;
  301. int i;
  302. BUG_ON(! cnx);
  303. switch (event->base_event.xSubtype) {
  304. case VethEventTypeCap:
  305. veth_take_cap(cnx, event);
  306. break;
  307. case VethEventTypeMonitor:
  308. /* do nothing... this'll hang out here til we're dead,
  309. * and the hypervisor will return it for us. */
  310. break;
  311. case VethEventTypeFramesAck:
  312. spin_lock_irqsave(&cnx->lock, flags);
  313. for (i = 0; i < VETH_MAX_ACKS_PER_MSG; ++i) {
  314. u16 msgnum = event->u.frames_ack_data.token[i];
  315. if (msgnum < VETH_NUMBUFFERS)
  316. veth_recycle_msg(cnx, cnx->msgs + msgnum);
  317. }
  318. spin_unlock_irqrestore(&cnx->lock, flags);
  319. veth_flush_pending(cnx);
  320. break;
  321. case VethEventTypeFrames:
  322. veth_receive(cnx, event);
  323. break;
  324. default:
  325. veth_error("Unknown interrupt type %d from lpar %d\n",
  326. event->base_event.xSubtype, rlp);
  327. };
  328. }
  329. static void veth_handle_event(struct HvLpEvent *event, struct pt_regs *regs)
  330. {
  331. struct VethLpEvent *veth_event = (struct VethLpEvent *)event;
  332. if (event->xFlags.xFunction == HvLpEvent_Function_Ack)
  333. veth_handle_ack(veth_event);
  334. else if (event->xFlags.xFunction == HvLpEvent_Function_Int)
  335. veth_handle_int(veth_event);
  336. }
  337. static int veth_process_caps(struct veth_lpar_connection *cnx)
  338. {
  339. struct VethCapData *remote_caps = &cnx->remote_caps;
  340. int num_acks_needed;
  341. /* Convert timer to jiffies */
  342. cnx->ack_timeout = remote_caps->ack_timeout * HZ / 1000000;
  343. if ( (remote_caps->num_buffers == 0)
  344. || (remote_caps->ack_threshold > VETH_MAX_ACKS_PER_MSG)
  345. || (remote_caps->ack_threshold == 0)
  346. || (cnx->ack_timeout == 0) ) {
  347. veth_error("Received incompatible capabilities from lpar %d\n",
  348. cnx->remote_lp);
  349. return HvLpEvent_Rc_InvalidSubtypeData;
  350. }
  351. num_acks_needed = (remote_caps->num_buffers
  352. / remote_caps->ack_threshold) + 1;
  353. /* FIXME: locking on num_ack_events? */
  354. if (cnx->num_ack_events < num_acks_needed) {
  355. int num;
  356. num = veth_allocate_events(cnx->remote_lp,
  357. num_acks_needed-cnx->num_ack_events);
  358. if (num > 0)
  359. cnx->num_ack_events += num;
  360. if (cnx->num_ack_events < num_acks_needed) {
  361. veth_error("Couldn't allocate enough ack events for lpar %d\n",
  362. cnx->remote_lp);
  363. return HvLpEvent_Rc_BufferNotAvailable;
  364. }
  365. }
  366. return HvLpEvent_Rc_Good;
  367. }
  368. /* FIXME: The gotos here are a bit dubious */
  369. static void veth_statemachine(void *p)
  370. {
  371. struct veth_lpar_connection *cnx = (struct veth_lpar_connection *)p;
  372. int rlp = cnx->remote_lp;
  373. int rc;
  374. spin_lock_irq(&cnx->lock);
  375. restart:
  376. if (cnx->state & VETH_STATE_RESET) {
  377. int i;
  378. del_timer(&cnx->ack_timer);
  379. if (cnx->state & VETH_STATE_OPEN)
  380. HvCallEvent_closeLpEventPath(cnx->remote_lp,
  381. HvLpEvent_Type_VirtualLan);
  382. /* reset ack data */
  383. memset(&cnx->pending_acks, 0xff, sizeof (cnx->pending_acks));
  384. cnx->num_pending_acks = 0;
  385. cnx->state &= ~(VETH_STATE_RESET | VETH_STATE_SENTMON
  386. | VETH_STATE_OPEN | VETH_STATE_SENTCAPS
  387. | VETH_STATE_GOTCAPACK | VETH_STATE_GOTCAPS
  388. | VETH_STATE_SENTCAPACK | VETH_STATE_READY);
  389. /* Clean up any leftover messages */
  390. if (cnx->msgs)
  391. for (i = 0; i < VETH_NUMBUFFERS; ++i)
  392. veth_recycle_msg(cnx, cnx->msgs + i);
  393. spin_unlock_irq(&cnx->lock);
  394. veth_flush_pending(cnx);
  395. spin_lock_irq(&cnx->lock);
  396. if (cnx->state & VETH_STATE_RESET)
  397. goto restart;
  398. }
  399. if (cnx->state & VETH_STATE_SHUTDOWN)
  400. /* It's all over, do nothing */
  401. goto out;
  402. if ( !(cnx->state & VETH_STATE_OPEN) ) {
  403. if (! cnx->msgs || (cnx->num_events < (2 + VETH_NUMBUFFERS)) )
  404. goto cant_cope;
  405. HvCallEvent_openLpEventPath(rlp, HvLpEvent_Type_VirtualLan);
  406. cnx->src_inst =
  407. HvCallEvent_getSourceLpInstanceId(rlp,
  408. HvLpEvent_Type_VirtualLan);
  409. cnx->dst_inst =
  410. HvCallEvent_getTargetLpInstanceId(rlp,
  411. HvLpEvent_Type_VirtualLan);
  412. cnx->state |= VETH_STATE_OPEN;
  413. }
  414. if ( (cnx->state & VETH_STATE_OPEN)
  415. && !(cnx->state & VETH_STATE_SENTMON) ) {
  416. rc = veth_signalevent(cnx, VethEventTypeMonitor,
  417. HvLpEvent_AckInd_DoAck,
  418. HvLpEvent_AckType_DeferredAck,
  419. 0, 0, 0, 0, 0, 0);
  420. if (rc == HvLpEvent_Rc_Good) {
  421. cnx->state |= VETH_STATE_SENTMON;
  422. } else {
  423. if ( (rc != HvLpEvent_Rc_PartitionDead)
  424. && (rc != HvLpEvent_Rc_PathClosed) )
  425. veth_error("Error sending monitor to "
  426. "lpar %d, rc=%x\n",
  427. rlp, (int) rc);
  428. /* Oh well, hope we get a cap from the other
  429. * end and do better when that kicks us */
  430. goto out;
  431. }
  432. }
  433. if ( (cnx->state & VETH_STATE_OPEN)
  434. && !(cnx->state & VETH_STATE_SENTCAPS)) {
  435. u64 *rawcap = (u64 *)&cnx->local_caps;
  436. rc = veth_signalevent(cnx, VethEventTypeCap,
  437. HvLpEvent_AckInd_DoAck,
  438. HvLpEvent_AckType_ImmediateAck,
  439. 0, rawcap[0], rawcap[1], rawcap[2],
  440. rawcap[3], rawcap[4]);
  441. if (rc == HvLpEvent_Rc_Good) {
  442. cnx->state |= VETH_STATE_SENTCAPS;
  443. } else {
  444. if ( (rc != HvLpEvent_Rc_PartitionDead)
  445. && (rc != HvLpEvent_Rc_PathClosed) )
  446. veth_error("Error sending caps to "
  447. "lpar %d, rc=%x\n",
  448. rlp, (int) rc);
  449. /* Oh well, hope we get a cap from the other
  450. * end and do better when that kicks us */
  451. goto out;
  452. }
  453. }
  454. if ((cnx->state & VETH_STATE_GOTCAPS)
  455. && !(cnx->state & VETH_STATE_SENTCAPACK)) {
  456. struct VethCapData *remote_caps = &cnx->remote_caps;
  457. memcpy(remote_caps, &cnx->cap_event.u.caps_data,
  458. sizeof(*remote_caps));
  459. spin_unlock_irq(&cnx->lock);
  460. rc = veth_process_caps(cnx);
  461. spin_lock_irq(&cnx->lock);
  462. /* We dropped the lock, so recheck for anything which
  463. * might mess us up */
  464. if (cnx->state & (VETH_STATE_RESET|VETH_STATE_SHUTDOWN))
  465. goto restart;
  466. cnx->cap_event.base_event.xRc = rc;
  467. HvCallEvent_ackLpEvent((struct HvLpEvent *)&cnx->cap_event);
  468. if (rc == HvLpEvent_Rc_Good)
  469. cnx->state |= VETH_STATE_SENTCAPACK;
  470. else
  471. goto cant_cope;
  472. }
  473. if ((cnx->state & VETH_STATE_GOTCAPACK)
  474. && (cnx->state & VETH_STATE_GOTCAPS)
  475. && !(cnx->state & VETH_STATE_READY)) {
  476. if (cnx->cap_ack_event.base_event.xRc == HvLpEvent_Rc_Good) {
  477. /* Start the ACK timer */
  478. cnx->ack_timer.expires = jiffies + cnx->ack_timeout;
  479. add_timer(&cnx->ack_timer);
  480. cnx->state |= VETH_STATE_READY;
  481. } else {
  482. veth_printk(KERN_ERR, "Caps rejected (rc=%d) by "
  483. "lpar %d\n",
  484. cnx->cap_ack_event.base_event.xRc,
  485. rlp);
  486. goto cant_cope;
  487. }
  488. }
  489. out:
  490. spin_unlock_irq(&cnx->lock);
  491. return;
  492. cant_cope:
  493. /* FIXME: we get here if something happens we really can't
  494. * cope with. The link will never work once we get here, and
  495. * all we can do is not lock the rest of the system up */
  496. veth_error("Badness on connection to lpar %d (state=%04lx) "
  497. " - shutting down\n", rlp, cnx->state);
  498. cnx->state |= VETH_STATE_SHUTDOWN;
  499. spin_unlock_irq(&cnx->lock);
  500. }
  501. static int veth_init_connection(u8 rlp)
  502. {
  503. struct veth_lpar_connection *cnx;
  504. struct veth_msg *msgs;
  505. int i;
  506. if ( (rlp == this_lp)
  507. || ! HvLpConfig_doLpsCommunicateOnVirtualLan(this_lp, rlp) )
  508. return 0;
  509. cnx = kmalloc(sizeof(*cnx), GFP_KERNEL);
  510. if (! cnx)
  511. return -ENOMEM;
  512. memset(cnx, 0, sizeof(*cnx));
  513. cnx->remote_lp = rlp;
  514. spin_lock_init(&cnx->lock);
  515. INIT_WORK(&cnx->statemachine_wq, veth_statemachine, cnx);
  516. init_timer(&cnx->ack_timer);
  517. cnx->ack_timer.function = veth_timed_ack;
  518. cnx->ack_timer.data = (unsigned long) cnx;
  519. memset(&cnx->pending_acks, 0xff, sizeof (cnx->pending_acks));
  520. veth_cnx[rlp] = cnx;
  521. msgs = kmalloc(VETH_NUMBUFFERS * sizeof(struct veth_msg), GFP_KERNEL);
  522. if (! msgs) {
  523. veth_error("Can't allocate buffers for lpar %d\n", rlp);
  524. return -ENOMEM;
  525. }
  526. cnx->msgs = msgs;
  527. memset(msgs, 0, VETH_NUMBUFFERS * sizeof(struct veth_msg));
  528. spin_lock_init(&cnx->msg_stack_lock);
  529. for (i = 0; i < VETH_NUMBUFFERS; i++) {
  530. msgs[i].token = i;
  531. veth_stack_push(cnx, msgs + i);
  532. }
  533. cnx->num_events = veth_allocate_events(rlp, 2 + VETH_NUMBUFFERS);
  534. if (cnx->num_events < (2 + VETH_NUMBUFFERS)) {
  535. veth_error("Can't allocate events for lpar %d, only got %d\n",
  536. rlp, cnx->num_events);
  537. return -ENOMEM;
  538. }
  539. cnx->local_caps.num_buffers = VETH_NUMBUFFERS;
  540. cnx->local_caps.ack_threshold = ACK_THRESHOLD;
  541. cnx->local_caps.ack_timeout = VETH_ACKTIMEOUT;
  542. return 0;
  543. }
  544. static void veth_stop_connection(u8 rlp)
  545. {
  546. struct veth_lpar_connection *cnx = veth_cnx[rlp];
  547. if (! cnx)
  548. return;
  549. spin_lock_irq(&cnx->lock);
  550. cnx->state |= VETH_STATE_RESET | VETH_STATE_SHUTDOWN;
  551. veth_kick_statemachine(cnx);
  552. spin_unlock_irq(&cnx->lock);
  553. flush_scheduled_work();
  554. /* FIXME: not sure if this is necessary - will already have
  555. * been deleted by the state machine, just want to make sure
  556. * its not running any more */
  557. del_timer_sync(&cnx->ack_timer);
  558. if (cnx->num_events > 0)
  559. mf_deallocate_lp_events(cnx->remote_lp,
  560. HvLpEvent_Type_VirtualLan,
  561. cnx->num_events,
  562. NULL, NULL);
  563. if (cnx->num_ack_events > 0)
  564. mf_deallocate_lp_events(cnx->remote_lp,
  565. HvLpEvent_Type_VirtualLan,
  566. cnx->num_ack_events,
  567. NULL, NULL);
  568. }
  569. static void veth_destroy_connection(u8 rlp)
  570. {
  571. struct veth_lpar_connection *cnx = veth_cnx[rlp];
  572. if (! cnx)
  573. return;
  574. kfree(cnx->msgs);
  575. kfree(cnx);
  576. veth_cnx[rlp] = NULL;
  577. }
  578. /*
  579. * net_device code
  580. */
  581. static int veth_open(struct net_device *dev)
  582. {
  583. struct veth_port *port = (struct veth_port *) dev->priv;
  584. memset(&port->stats, 0, sizeof (port->stats));
  585. netif_start_queue(dev);
  586. return 0;
  587. }
  588. static int veth_close(struct net_device *dev)
  589. {
  590. netif_stop_queue(dev);
  591. return 0;
  592. }
  593. static struct net_device_stats *veth_get_stats(struct net_device *dev)
  594. {
  595. struct veth_port *port = (struct veth_port *) dev->priv;
  596. return &port->stats;
  597. }
  598. static int veth_change_mtu(struct net_device *dev, int new_mtu)
  599. {
  600. if ((new_mtu < 68) || (new_mtu > VETH_MAX_MTU))
  601. return -EINVAL;
  602. dev->mtu = new_mtu;
  603. return 0;
  604. }
  605. static void veth_set_multicast_list(struct net_device *dev)
  606. {
  607. struct veth_port *port = (struct veth_port *) dev->priv;
  608. unsigned long flags;
  609. write_lock_irqsave(&port->mcast_gate, flags);
  610. if (dev->flags & IFF_PROMISC) { /* set promiscuous mode */
  611. printk(KERN_INFO "%s: Promiscuous mode enabled.\n",
  612. dev->name);
  613. port->promiscuous = 1;
  614. } else if ( (dev->flags & IFF_ALLMULTI)
  615. || (dev->mc_count > VETH_MAX_MCAST) ) {
  616. port->all_mcast = 1;
  617. } else {
  618. struct dev_mc_list *dmi = dev->mc_list;
  619. int i;
  620. /* Update table */
  621. port->num_mcast = 0;
  622. for (i = 0; i < dev->mc_count; i++) {
  623. u8 *addr = dmi->dmi_addr;
  624. u64 xaddr = 0;
  625. if (addr[0] & 0x01) {/* multicast address? */
  626. memcpy(&xaddr, addr, ETH_ALEN);
  627. port->mcast_addr[port->num_mcast] = xaddr;
  628. port->num_mcast++;
  629. }
  630. dmi = dmi->next;
  631. }
  632. }
  633. write_unlock_irqrestore(&port->mcast_gate, flags);
  634. }
  635. static void veth_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
  636. {
  637. strncpy(info->driver, "veth", sizeof(info->driver) - 1);
  638. info->driver[sizeof(info->driver) - 1] = '\0';
  639. strncpy(info->version, "1.0", sizeof(info->version) - 1);
  640. }
  641. static int veth_get_settings(struct net_device *dev, struct ethtool_cmd *ecmd)
  642. {
  643. ecmd->supported = (SUPPORTED_1000baseT_Full
  644. | SUPPORTED_Autoneg | SUPPORTED_FIBRE);
  645. ecmd->advertising = (SUPPORTED_1000baseT_Full
  646. | SUPPORTED_Autoneg | SUPPORTED_FIBRE);
  647. ecmd->port = PORT_FIBRE;
  648. ecmd->transceiver = XCVR_INTERNAL;
  649. ecmd->phy_address = 0;
  650. ecmd->speed = SPEED_1000;
  651. ecmd->duplex = DUPLEX_FULL;
  652. ecmd->autoneg = AUTONEG_ENABLE;
  653. ecmd->maxtxpkt = 120;
  654. ecmd->maxrxpkt = 120;
  655. return 0;
  656. }
  657. static u32 veth_get_link(struct net_device *dev)
  658. {
  659. return 1;
  660. }
  661. static struct ethtool_ops ops = {
  662. .get_drvinfo = veth_get_drvinfo,
  663. .get_settings = veth_get_settings,
  664. .get_link = veth_get_link,
  665. };
  666. static void veth_tx_timeout(struct net_device *dev)
  667. {
  668. struct veth_port *port = (struct veth_port *)dev->priv;
  669. struct net_device_stats *stats = &port->stats;
  670. unsigned long flags;
  671. int i;
  672. stats->tx_errors++;
  673. spin_lock_irqsave(&port->pending_gate, flags);
  674. printk(KERN_WARNING "%s: Tx timeout! Resetting lp connections: %08x\n",
  675. dev->name, port->pending_lpmask);
  676. /* If we've timed out the queue must be stopped, which should
  677. * only ever happen when there is a pending packet. */
  678. WARN_ON(! port->pending_lpmask);
  679. for (i = 0; i < HVMAXARCHITECTEDLPS; i++) {
  680. struct veth_lpar_connection *cnx = veth_cnx[i];
  681. if (! (port->pending_lpmask & (1<<i)))
  682. continue;
  683. /* If we're pending on it, we must be connected to it,
  684. * so we should certainly have a structure for it. */
  685. BUG_ON(! cnx);
  686. /* Theoretically we could be kicking a connection
  687. * which doesn't deserve it, but in practice if we've
  688. * had a Tx timeout, the pending_lpmask will have
  689. * exactly one bit set - the connection causing the
  690. * problem. */
  691. spin_lock(&cnx->lock);
  692. cnx->state |= VETH_STATE_RESET;
  693. veth_kick_statemachine(cnx);
  694. spin_unlock(&cnx->lock);
  695. }
  696. spin_unlock_irqrestore(&port->pending_gate, flags);
  697. }
  698. static struct net_device * __init veth_probe_one(int vlan, struct device *vdev)
  699. {
  700. struct net_device *dev;
  701. struct veth_port *port;
  702. int i, rc;
  703. dev = alloc_etherdev(sizeof (struct veth_port));
  704. if (! dev) {
  705. veth_error("Unable to allocate net_device structure!\n");
  706. return NULL;
  707. }
  708. port = (struct veth_port *) dev->priv;
  709. spin_lock_init(&port->pending_gate);
  710. rwlock_init(&port->mcast_gate);
  711. for (i = 0; i < HVMAXARCHITECTEDLPS; i++) {
  712. HvLpVirtualLanIndexMap map;
  713. if (i == this_lp)
  714. continue;
  715. map = HvLpConfig_getVirtualLanIndexMapForLp(i);
  716. if (map & (0x8000 >> vlan))
  717. port->lpar_map |= (1 << i);
  718. }
  719. port->dev = vdev;
  720. dev->dev_addr[0] = 0x02;
  721. dev->dev_addr[1] = 0x01;
  722. dev->dev_addr[2] = 0xff;
  723. dev->dev_addr[3] = vlan;
  724. dev->dev_addr[4] = 0xff;
  725. dev->dev_addr[5] = this_lp;
  726. dev->mtu = VETH_MAX_MTU;
  727. memcpy(&port->mac_addr, dev->dev_addr, 6);
  728. dev->open = veth_open;
  729. dev->hard_start_xmit = veth_start_xmit;
  730. dev->stop = veth_close;
  731. dev->get_stats = veth_get_stats;
  732. dev->change_mtu = veth_change_mtu;
  733. dev->set_mac_address = NULL;
  734. dev->set_multicast_list = veth_set_multicast_list;
  735. SET_ETHTOOL_OPS(dev, &ops);
  736. dev->watchdog_timeo = 2 * (VETH_ACKTIMEOUT * HZ / 1000000);
  737. dev->tx_timeout = veth_tx_timeout;
  738. SET_NETDEV_DEV(dev, vdev);
  739. rc = register_netdev(dev);
  740. if (rc != 0) {
  741. veth_printk(KERN_ERR,
  742. "Failed to register ethernet device for vlan %d\n",
  743. vlan);
  744. free_netdev(dev);
  745. return NULL;
  746. }
  747. veth_printk(KERN_DEBUG, "%s attached to iSeries vlan %d (lpar_map=0x%04x)\n",
  748. dev->name, vlan, port->lpar_map);
  749. return dev;
  750. }
  751. /*
  752. * Tx path
  753. */
  754. static int veth_transmit_to_one(struct sk_buff *skb, HvLpIndex rlp,
  755. struct net_device *dev)
  756. {
  757. struct veth_lpar_connection *cnx = veth_cnx[rlp];
  758. struct veth_port *port = (struct veth_port *) dev->priv;
  759. HvLpEvent_Rc rc;
  760. u32 dma_address, dma_length;
  761. struct veth_msg *msg = NULL;
  762. int err = 0;
  763. unsigned long flags;
  764. if (! cnx) {
  765. port->stats.tx_errors++;
  766. dev_kfree_skb(skb);
  767. return 0;
  768. }
  769. spin_lock_irqsave(&cnx->lock, flags);
  770. if (! cnx->state & VETH_STATE_READY)
  771. goto drop;
  772. if ((skb->len - 14) > VETH_MAX_MTU)
  773. goto drop;
  774. msg = veth_stack_pop(cnx);
  775. if (! msg) {
  776. err = 1;
  777. goto drop;
  778. }
  779. dma_length = skb->len;
  780. dma_address = dma_map_single(port->dev, skb->data,
  781. dma_length, DMA_TO_DEVICE);
  782. if (dma_mapping_error(dma_address))
  783. goto recycle_and_drop;
  784. /* Is it really necessary to check the length and address
  785. * fields of the first entry here? */
  786. msg->skb = skb;
  787. msg->dev = port->dev;
  788. msg->data.addr[0] = dma_address;
  789. msg->data.len[0] = dma_length;
  790. msg->data.eofmask = 1 << VETH_EOF_SHIFT;
  791. set_bit(0, &(msg->in_use));
  792. rc = veth_signaldata(cnx, VethEventTypeFrames, msg->token, &msg->data);
  793. if (rc != HvLpEvent_Rc_Good)
  794. goto recycle_and_drop;
  795. spin_unlock_irqrestore(&cnx->lock, flags);
  796. return 0;
  797. recycle_and_drop:
  798. msg->skb = NULL;
  799. /* need to set in use to make veth_recycle_msg in case this
  800. * was a mapping failure */
  801. set_bit(0, &msg->in_use);
  802. veth_recycle_msg(cnx, msg);
  803. drop:
  804. port->stats.tx_errors++;
  805. dev_kfree_skb(skb);
  806. spin_unlock_irqrestore(&cnx->lock, flags);
  807. return err;
  808. }
  809. static HvLpIndexMap veth_transmit_to_many(struct sk_buff *skb,
  810. HvLpIndexMap lpmask,
  811. struct net_device *dev)
  812. {
  813. struct veth_port *port = (struct veth_port *) dev->priv;
  814. int i;
  815. int rc;
  816. for (i = 0; i < HVMAXARCHITECTEDLPS; i++) {
  817. if ((lpmask & (1 << i)) == 0)
  818. continue;
  819. rc = veth_transmit_to_one(skb_get(skb), i, dev);
  820. if (! rc)
  821. lpmask &= ~(1<<i);
  822. }
  823. if (! lpmask) {
  824. port->stats.tx_packets++;
  825. port->stats.tx_bytes += skb->len;
  826. }
  827. return lpmask;
  828. }
  829. static int veth_start_xmit(struct sk_buff *skb, struct net_device *dev)
  830. {
  831. unsigned char *frame = skb->data;
  832. struct veth_port *port = (struct veth_port *) dev->priv;
  833. unsigned long flags;
  834. HvLpIndexMap lpmask;
  835. if (! (frame[0] & 0x01)) {
  836. /* unicast packet */
  837. HvLpIndex rlp = frame[5];
  838. if ( ! ((1 << rlp) & port->lpar_map) ) {
  839. dev_kfree_skb(skb);
  840. return 0;
  841. }
  842. lpmask = 1 << rlp;
  843. } else {
  844. lpmask = port->lpar_map;
  845. }
  846. spin_lock_irqsave(&port->pending_gate, flags);
  847. lpmask = veth_transmit_to_many(skb, lpmask, dev);
  848. if (! lpmask) {
  849. dev_kfree_skb(skb);
  850. } else {
  851. if (port->pending_skb) {
  852. veth_error("%s: Tx while skb was pending!\n",
  853. dev->name);
  854. dev_kfree_skb(skb);
  855. spin_unlock_irqrestore(&port->pending_gate, flags);
  856. return 1;
  857. }
  858. port->pending_skb = skb;
  859. port->pending_lpmask = lpmask;
  860. netif_stop_queue(dev);
  861. }
  862. spin_unlock_irqrestore(&port->pending_gate, flags);
  863. return 0;
  864. }
  865. static void veth_recycle_msg(struct veth_lpar_connection *cnx,
  866. struct veth_msg *msg)
  867. {
  868. u32 dma_address, dma_length;
  869. if (test_and_clear_bit(0, &msg->in_use)) {
  870. dma_address = msg->data.addr[0];
  871. dma_length = msg->data.len[0];
  872. dma_unmap_single(msg->dev, dma_address, dma_length,
  873. DMA_TO_DEVICE);
  874. if (msg->skb) {
  875. dev_kfree_skb_any(msg->skb);
  876. msg->skb = NULL;
  877. }
  878. memset(&msg->data, 0, sizeof(msg->data));
  879. veth_stack_push(cnx, msg);
  880. } else
  881. if (cnx->state & VETH_STATE_OPEN)
  882. veth_error("Bogus frames ack from lpar %d (#%d)\n",
  883. cnx->remote_lp, msg->token);
  884. }
  885. static void veth_flush_pending(struct veth_lpar_connection *cnx)
  886. {
  887. int i;
  888. for (i = 0; i < HVMAXARCHITECTEDVIRTUALLANS; i++) {
  889. struct net_device *dev = veth_dev[i];
  890. struct veth_port *port;
  891. unsigned long flags;
  892. if (! dev)
  893. continue;
  894. port = (struct veth_port *)dev->priv;
  895. if (! (port->lpar_map & (1<<cnx->remote_lp)))
  896. continue;
  897. spin_lock_irqsave(&port->pending_gate, flags);
  898. if (port->pending_skb) {
  899. port->pending_lpmask =
  900. veth_transmit_to_many(port->pending_skb,
  901. port->pending_lpmask,
  902. dev);
  903. if (! port->pending_lpmask) {
  904. dev_kfree_skb_any(port->pending_skb);
  905. port->pending_skb = NULL;
  906. netif_wake_queue(dev);
  907. }
  908. }
  909. spin_unlock_irqrestore(&port->pending_gate, flags);
  910. }
  911. }
  912. /*
  913. * Rx path
  914. */
  915. static inline int veth_frame_wanted(struct veth_port *port, u64 mac_addr)
  916. {
  917. int wanted = 0;
  918. int i;
  919. unsigned long flags;
  920. if ( (mac_addr == port->mac_addr) || (mac_addr == 0xffffffffffff0000) )
  921. return 1;
  922. if (! (((char *) &mac_addr)[0] & 0x01))
  923. return 0;
  924. read_lock_irqsave(&port->mcast_gate, flags);
  925. if (port->promiscuous || port->all_mcast) {
  926. wanted = 1;
  927. goto out;
  928. }
  929. for (i = 0; i < port->num_mcast; ++i) {
  930. if (port->mcast_addr[i] == mac_addr) {
  931. wanted = 1;
  932. break;
  933. }
  934. }
  935. out:
  936. read_unlock_irqrestore(&port->mcast_gate, flags);
  937. return wanted;
  938. }
  939. struct dma_chunk {
  940. u64 addr;
  941. u64 size;
  942. };
  943. #define VETH_MAX_PAGES_PER_FRAME ( (VETH_MAX_MTU+PAGE_SIZE-2)/PAGE_SIZE + 1 )
  944. static inline void veth_build_dma_list(struct dma_chunk *list,
  945. unsigned char *p, unsigned long length)
  946. {
  947. unsigned long done;
  948. int i = 1;
  949. /* FIXME: skbs are continguous in real addresses. Do we
  950. * really need to break it into PAGE_SIZE chunks, or can we do
  951. * it just at the granularity of iSeries real->absolute
  952. * mapping? Indeed, given the way the allocator works, can we
  953. * count on them being absolutely contiguous? */
  954. list[0].addr = ISERIES_HV_ADDR(p);
  955. list[0].size = min(length,
  956. PAGE_SIZE - ((unsigned long)p & ~PAGE_MASK));
  957. done = list[0].size;
  958. while (done < length) {
  959. list[i].addr = ISERIES_HV_ADDR(p + done);
  960. list[i].size = min(length-done, PAGE_SIZE);
  961. done += list[i].size;
  962. i++;
  963. }
  964. }
  965. static void veth_flush_acks(struct veth_lpar_connection *cnx)
  966. {
  967. HvLpEvent_Rc rc;
  968. rc = veth_signaldata(cnx, VethEventTypeFramesAck,
  969. 0, &cnx->pending_acks);
  970. if (rc != HvLpEvent_Rc_Good)
  971. veth_error("Error 0x%x acking frames from lpar %d!\n",
  972. (unsigned)rc, cnx->remote_lp);
  973. cnx->num_pending_acks = 0;
  974. memset(&cnx->pending_acks, 0xff, sizeof(cnx->pending_acks));
  975. }
  976. static void veth_receive(struct veth_lpar_connection *cnx,
  977. struct VethLpEvent *event)
  978. {
  979. struct VethFramesData *senddata = &event->u.frames_data;
  980. int startchunk = 0;
  981. int nchunks;
  982. unsigned long flags;
  983. HvLpDma_Rc rc;
  984. do {
  985. u16 length = 0;
  986. struct sk_buff *skb;
  987. struct dma_chunk local_list[VETH_MAX_PAGES_PER_FRAME];
  988. struct dma_chunk remote_list[VETH_MAX_FRAMES_PER_MSG];
  989. u64 dest;
  990. HvLpVirtualLanIndex vlan;
  991. struct net_device *dev;
  992. struct veth_port *port;
  993. /* FIXME: do we need this? */
  994. memset(local_list, 0, sizeof(local_list));
  995. memset(remote_list, 0, sizeof(VETH_MAX_FRAMES_PER_MSG));
  996. /* a 0 address marks the end of the valid entries */
  997. if (senddata->addr[startchunk] == 0)
  998. break;
  999. /* make sure that we have at least 1 EOF entry in the
  1000. * remaining entries */
  1001. if (! (senddata->eofmask >> (startchunk + VETH_EOF_SHIFT))) {
  1002. veth_error("missing EOF frag in event "
  1003. "eofmask=0x%x startchunk=%d\n",
  1004. (unsigned) senddata->eofmask, startchunk);
  1005. break;
  1006. }
  1007. /* build list of chunks in this frame */
  1008. nchunks = 0;
  1009. do {
  1010. remote_list[nchunks].addr =
  1011. (u64) senddata->addr[startchunk+nchunks] << 32;
  1012. remote_list[nchunks].size =
  1013. senddata->len[startchunk+nchunks];
  1014. length += remote_list[nchunks].size;
  1015. } while (! (senddata->eofmask &
  1016. (1 << (VETH_EOF_SHIFT + startchunk + nchunks++))));
  1017. /* length == total length of all chunks */
  1018. /* nchunks == # of chunks in this frame */
  1019. if ((length - ETH_HLEN) > VETH_MAX_MTU) {
  1020. veth_error("Received oversize frame from lpar %d "
  1021. "(length=%d)\n", cnx->remote_lp, length);
  1022. continue;
  1023. }
  1024. skb = alloc_skb(length, GFP_ATOMIC);
  1025. if (!skb)
  1026. continue;
  1027. veth_build_dma_list(local_list, skb->data, length);
  1028. rc = HvCallEvent_dmaBufList(HvLpEvent_Type_VirtualLan,
  1029. event->base_event.xSourceLp,
  1030. HvLpDma_Direction_RemoteToLocal,
  1031. cnx->src_inst,
  1032. cnx->dst_inst,
  1033. HvLpDma_AddressType_RealAddress,
  1034. HvLpDma_AddressType_TceIndex,
  1035. ISERIES_HV_ADDR(&local_list),
  1036. ISERIES_HV_ADDR(&remote_list),
  1037. length);
  1038. if (rc != HvLpDma_Rc_Good) {
  1039. dev_kfree_skb_irq(skb);
  1040. continue;
  1041. }
  1042. vlan = skb->data[9];
  1043. dev = veth_dev[vlan];
  1044. if (! dev)
  1045. /* Some earlier versions of the driver sent
  1046. broadcasts down all connections, even to
  1047. lpars that weren't on the relevant vlan.
  1048. So ignore packets belonging to a vlan we're
  1049. not on. */
  1050. continue;
  1051. port = (struct veth_port *)dev->priv;
  1052. dest = *((u64 *) skb->data) & 0xFFFFFFFFFFFF0000;
  1053. if ((vlan > HVMAXARCHITECTEDVIRTUALLANS) || !port) {
  1054. dev_kfree_skb_irq(skb);
  1055. continue;
  1056. }
  1057. if (! veth_frame_wanted(port, dest)) {
  1058. dev_kfree_skb_irq(skb);
  1059. continue;
  1060. }
  1061. skb_put(skb, length);
  1062. skb->dev = dev;
  1063. skb->protocol = eth_type_trans(skb, dev);
  1064. skb->ip_summed = CHECKSUM_NONE;
  1065. netif_rx(skb); /* send it up */
  1066. port->stats.rx_packets++;
  1067. port->stats.rx_bytes += length;
  1068. } while (startchunk += nchunks, startchunk < VETH_MAX_FRAMES_PER_MSG);
  1069. /* Ack it */
  1070. spin_lock_irqsave(&cnx->lock, flags);
  1071. BUG_ON(cnx->num_pending_acks > VETH_MAX_ACKS_PER_MSG);
  1072. cnx->pending_acks[cnx->num_pending_acks++] =
  1073. event->base_event.xCorrelationToken;
  1074. if ( (cnx->num_pending_acks >= cnx->remote_caps.ack_threshold)
  1075. || (cnx->num_pending_acks >= VETH_MAX_ACKS_PER_MSG) )
  1076. veth_flush_acks(cnx);
  1077. spin_unlock_irqrestore(&cnx->lock, flags);
  1078. }
  1079. static void veth_timed_ack(unsigned long ptr)
  1080. {
  1081. struct veth_lpar_connection *cnx = (struct veth_lpar_connection *) ptr;
  1082. unsigned long flags;
  1083. /* Ack all the events */
  1084. spin_lock_irqsave(&cnx->lock, flags);
  1085. if (cnx->num_pending_acks > 0)
  1086. veth_flush_acks(cnx);
  1087. /* Reschedule the timer */
  1088. cnx->ack_timer.expires = jiffies + cnx->ack_timeout;
  1089. add_timer(&cnx->ack_timer);
  1090. spin_unlock_irqrestore(&cnx->lock, flags);
  1091. }
  1092. static int veth_remove(struct vio_dev *vdev)
  1093. {
  1094. int i = vdev->unit_address;
  1095. struct net_device *dev;
  1096. dev = veth_dev[i];
  1097. if (dev != NULL) {
  1098. veth_dev[i] = NULL;
  1099. unregister_netdev(dev);
  1100. free_netdev(dev);
  1101. }
  1102. return 0;
  1103. }
  1104. static int veth_probe(struct vio_dev *vdev, const struct vio_device_id *id)
  1105. {
  1106. int i = vdev->unit_address;
  1107. struct net_device *dev;
  1108. dev = veth_probe_one(i, &vdev->dev);
  1109. if (dev == NULL) {
  1110. veth_remove(vdev);
  1111. return 1;
  1112. }
  1113. veth_dev[i] = dev;
  1114. /* Start the state machine on each connection, to commence
  1115. * link negotiation */
  1116. for (i = 0; i < HVMAXARCHITECTEDLPS; i++)
  1117. if (veth_cnx[i])
  1118. veth_kick_statemachine(veth_cnx[i]);
  1119. return 0;
  1120. }
  1121. /**
  1122. * veth_device_table: Used by vio.c to match devices that we
  1123. * support.
  1124. */
  1125. static struct vio_device_id veth_device_table[] __devinitdata = {
  1126. { "vlan", "" },
  1127. { NULL, NULL }
  1128. };
  1129. MODULE_DEVICE_TABLE(vio, veth_device_table);
  1130. static struct vio_driver veth_driver = {
  1131. .name = "iseries_veth",
  1132. .id_table = veth_device_table,
  1133. .probe = veth_probe,
  1134. .remove = veth_remove
  1135. };
  1136. /*
  1137. * Module initialization/cleanup
  1138. */
  1139. void __exit veth_module_cleanup(void)
  1140. {
  1141. int i;
  1142. vio_unregister_driver(&veth_driver);
  1143. for (i = 0; i < HVMAXARCHITECTEDLPS; ++i)
  1144. veth_stop_connection(i);
  1145. HvLpEvent_unregisterHandler(HvLpEvent_Type_VirtualLan);
  1146. /* Hypervisor callbacks may have scheduled more work while we
  1147. * were destroying connections. Now that we've disconnected from
  1148. * the hypervisor make sure everything's finished. */
  1149. flush_scheduled_work();
  1150. for (i = 0; i < HVMAXARCHITECTEDLPS; ++i)
  1151. veth_destroy_connection(i);
  1152. }
  1153. module_exit(veth_module_cleanup);
  1154. int __init veth_module_init(void)
  1155. {
  1156. int i;
  1157. int rc;
  1158. this_lp = HvLpConfig_getLpIndex_outline();
  1159. for (i = 0; i < HVMAXARCHITECTEDLPS; ++i) {
  1160. rc = veth_init_connection(i);
  1161. if (rc != 0) {
  1162. veth_module_cleanup();
  1163. return rc;
  1164. }
  1165. }
  1166. HvLpEvent_registerHandler(HvLpEvent_Type_VirtualLan,
  1167. &veth_handle_event);
  1168. return vio_register_driver(&veth_driver);
  1169. }
  1170. module_init(veth_module_init);