iseries_veth.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437
  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. if (!port->pending_lpmask) {
  675. spin_unlock_irqrestore(&port->pending_gate, flags);
  676. return;
  677. }
  678. printk(KERN_WARNING "%s: Tx timeout! Resetting lp connections: %08x\n",
  679. dev->name, port->pending_lpmask);
  680. for (i = 0; i < HVMAXARCHITECTEDLPS; i++) {
  681. struct veth_lpar_connection *cnx = veth_cnx[i];
  682. if (! (port->pending_lpmask & (1<<i)))
  683. continue;
  684. /* If we're pending on it, we must be connected to it,
  685. * so we should certainly have a structure for it. */
  686. BUG_ON(! cnx);
  687. /* Theoretically we could be kicking a connection
  688. * which doesn't deserve it, but in practice if we've
  689. * had a Tx timeout, the pending_lpmask will have
  690. * exactly one bit set - the connection causing the
  691. * problem. */
  692. spin_lock(&cnx->lock);
  693. cnx->state |= VETH_STATE_RESET;
  694. veth_kick_statemachine(cnx);
  695. spin_unlock(&cnx->lock);
  696. }
  697. spin_unlock_irqrestore(&port->pending_gate, flags);
  698. }
  699. static struct net_device * __init veth_probe_one(int vlan, struct device *vdev)
  700. {
  701. struct net_device *dev;
  702. struct veth_port *port;
  703. int i, rc;
  704. dev = alloc_etherdev(sizeof (struct veth_port));
  705. if (! dev) {
  706. veth_error("Unable to allocate net_device structure!\n");
  707. return NULL;
  708. }
  709. port = (struct veth_port *) dev->priv;
  710. spin_lock_init(&port->pending_gate);
  711. rwlock_init(&port->mcast_gate);
  712. for (i = 0; i < HVMAXARCHITECTEDLPS; i++) {
  713. HvLpVirtualLanIndexMap map;
  714. if (i == this_lp)
  715. continue;
  716. map = HvLpConfig_getVirtualLanIndexMapForLp(i);
  717. if (map & (0x8000 >> vlan))
  718. port->lpar_map |= (1 << i);
  719. }
  720. port->dev = vdev;
  721. dev->dev_addr[0] = 0x02;
  722. dev->dev_addr[1] = 0x01;
  723. dev->dev_addr[2] = 0xff;
  724. dev->dev_addr[3] = vlan;
  725. dev->dev_addr[4] = 0xff;
  726. dev->dev_addr[5] = this_lp;
  727. dev->mtu = VETH_MAX_MTU;
  728. memcpy(&port->mac_addr, dev->dev_addr, 6);
  729. dev->open = veth_open;
  730. dev->hard_start_xmit = veth_start_xmit;
  731. dev->stop = veth_close;
  732. dev->get_stats = veth_get_stats;
  733. dev->change_mtu = veth_change_mtu;
  734. dev->set_mac_address = NULL;
  735. dev->set_multicast_list = veth_set_multicast_list;
  736. SET_ETHTOOL_OPS(dev, &ops);
  737. dev->watchdog_timeo = 2 * (VETH_ACKTIMEOUT * HZ / 1000000);
  738. dev->tx_timeout = veth_tx_timeout;
  739. SET_NETDEV_DEV(dev, vdev);
  740. rc = register_netdev(dev);
  741. if (rc != 0) {
  742. veth_printk(KERN_ERR,
  743. "Failed to register ethernet device for vlan %d\n",
  744. vlan);
  745. free_netdev(dev);
  746. return NULL;
  747. }
  748. veth_printk(KERN_DEBUG, "%s attached to iSeries vlan %d (lpar_map=0x%04x)\n",
  749. dev->name, vlan, port->lpar_map);
  750. return dev;
  751. }
  752. /*
  753. * Tx path
  754. */
  755. static int veth_transmit_to_one(struct sk_buff *skb, HvLpIndex rlp,
  756. struct net_device *dev)
  757. {
  758. struct veth_lpar_connection *cnx = veth_cnx[rlp];
  759. struct veth_port *port = (struct veth_port *) dev->priv;
  760. HvLpEvent_Rc rc;
  761. u32 dma_address, dma_length;
  762. struct veth_msg *msg = NULL;
  763. int err = 0;
  764. unsigned long flags;
  765. if (! cnx) {
  766. port->stats.tx_errors++;
  767. dev_kfree_skb(skb);
  768. return 0;
  769. }
  770. spin_lock_irqsave(&cnx->lock, flags);
  771. if (! (cnx->state & VETH_STATE_READY))
  772. goto drop;
  773. if ((skb->len - 14) > VETH_MAX_MTU)
  774. goto drop;
  775. msg = veth_stack_pop(cnx);
  776. if (! msg) {
  777. err = 1;
  778. goto drop;
  779. }
  780. dma_length = skb->len;
  781. dma_address = dma_map_single(port->dev, skb->data,
  782. dma_length, DMA_TO_DEVICE);
  783. if (dma_mapping_error(dma_address))
  784. goto recycle_and_drop;
  785. /* Is it really necessary to check the length and address
  786. * fields of the first entry here? */
  787. msg->skb = skb;
  788. msg->dev = port->dev;
  789. msg->data.addr[0] = dma_address;
  790. msg->data.len[0] = dma_length;
  791. msg->data.eofmask = 1 << VETH_EOF_SHIFT;
  792. set_bit(0, &(msg->in_use));
  793. rc = veth_signaldata(cnx, VethEventTypeFrames, msg->token, &msg->data);
  794. if (rc != HvLpEvent_Rc_Good)
  795. goto recycle_and_drop;
  796. spin_unlock_irqrestore(&cnx->lock, flags);
  797. return 0;
  798. recycle_and_drop:
  799. msg->skb = NULL;
  800. /* need to set in use to make veth_recycle_msg in case this
  801. * was a mapping failure */
  802. set_bit(0, &msg->in_use);
  803. veth_recycle_msg(cnx, msg);
  804. drop:
  805. port->stats.tx_errors++;
  806. dev_kfree_skb(skb);
  807. spin_unlock_irqrestore(&cnx->lock, flags);
  808. return err;
  809. }
  810. static HvLpIndexMap veth_transmit_to_many(struct sk_buff *skb,
  811. HvLpIndexMap lpmask,
  812. struct net_device *dev)
  813. {
  814. struct veth_port *port = (struct veth_port *) dev->priv;
  815. int i;
  816. int rc;
  817. for (i = 0; i < HVMAXARCHITECTEDLPS; i++) {
  818. if ((lpmask & (1 << i)) == 0)
  819. continue;
  820. rc = veth_transmit_to_one(skb_get(skb), i, dev);
  821. if (! rc)
  822. lpmask &= ~(1<<i);
  823. }
  824. if (! lpmask) {
  825. port->stats.tx_packets++;
  826. port->stats.tx_bytes += skb->len;
  827. }
  828. return lpmask;
  829. }
  830. static int veth_start_xmit(struct sk_buff *skb, struct net_device *dev)
  831. {
  832. unsigned char *frame = skb->data;
  833. struct veth_port *port = (struct veth_port *) dev->priv;
  834. unsigned long flags;
  835. HvLpIndexMap lpmask;
  836. if (! (frame[0] & 0x01)) {
  837. /* unicast packet */
  838. HvLpIndex rlp = frame[5];
  839. if ( ! ((1 << rlp) & port->lpar_map) ) {
  840. dev_kfree_skb(skb);
  841. return 0;
  842. }
  843. lpmask = 1 << rlp;
  844. } else {
  845. lpmask = port->lpar_map;
  846. }
  847. spin_lock_irqsave(&port->pending_gate, flags);
  848. lpmask = veth_transmit_to_many(skb, lpmask, dev);
  849. dev->trans_start = jiffies;
  850. if (! lpmask) {
  851. dev_kfree_skb(skb);
  852. } else {
  853. if (port->pending_skb) {
  854. veth_error("%s: Tx while skb was pending!\n",
  855. dev->name);
  856. dev_kfree_skb(skb);
  857. spin_unlock_irqrestore(&port->pending_gate, flags);
  858. return 1;
  859. }
  860. port->pending_skb = skb;
  861. port->pending_lpmask = lpmask;
  862. netif_stop_queue(dev);
  863. }
  864. spin_unlock_irqrestore(&port->pending_gate, flags);
  865. return 0;
  866. }
  867. static void veth_recycle_msg(struct veth_lpar_connection *cnx,
  868. struct veth_msg *msg)
  869. {
  870. u32 dma_address, dma_length;
  871. if (test_and_clear_bit(0, &msg->in_use)) {
  872. dma_address = msg->data.addr[0];
  873. dma_length = msg->data.len[0];
  874. dma_unmap_single(msg->dev, dma_address, dma_length,
  875. DMA_TO_DEVICE);
  876. if (msg->skb) {
  877. dev_kfree_skb_any(msg->skb);
  878. msg->skb = NULL;
  879. }
  880. memset(&msg->data, 0, sizeof(msg->data));
  881. veth_stack_push(cnx, msg);
  882. } else
  883. if (cnx->state & VETH_STATE_OPEN)
  884. veth_error("Bogus frames ack from lpar %d (#%d)\n",
  885. cnx->remote_lp, msg->token);
  886. }
  887. static void veth_flush_pending(struct veth_lpar_connection *cnx)
  888. {
  889. int i;
  890. for (i = 0; i < HVMAXARCHITECTEDVIRTUALLANS; i++) {
  891. struct net_device *dev = veth_dev[i];
  892. struct veth_port *port;
  893. unsigned long flags;
  894. if (! dev)
  895. continue;
  896. port = (struct veth_port *)dev->priv;
  897. if (! (port->lpar_map & (1<<cnx->remote_lp)))
  898. continue;
  899. spin_lock_irqsave(&port->pending_gate, flags);
  900. if (port->pending_skb) {
  901. port->pending_lpmask =
  902. veth_transmit_to_many(port->pending_skb,
  903. port->pending_lpmask,
  904. dev);
  905. if (! port->pending_lpmask) {
  906. dev_kfree_skb_any(port->pending_skb);
  907. port->pending_skb = NULL;
  908. netif_wake_queue(dev);
  909. }
  910. }
  911. spin_unlock_irqrestore(&port->pending_gate, flags);
  912. }
  913. }
  914. /*
  915. * Rx path
  916. */
  917. static inline int veth_frame_wanted(struct veth_port *port, u64 mac_addr)
  918. {
  919. int wanted = 0;
  920. int i;
  921. unsigned long flags;
  922. if ( (mac_addr == port->mac_addr) || (mac_addr == 0xffffffffffff0000) )
  923. return 1;
  924. if (! (((char *) &mac_addr)[0] & 0x01))
  925. return 0;
  926. read_lock_irqsave(&port->mcast_gate, flags);
  927. if (port->promiscuous || port->all_mcast) {
  928. wanted = 1;
  929. goto out;
  930. }
  931. for (i = 0; i < port->num_mcast; ++i) {
  932. if (port->mcast_addr[i] == mac_addr) {
  933. wanted = 1;
  934. break;
  935. }
  936. }
  937. out:
  938. read_unlock_irqrestore(&port->mcast_gate, flags);
  939. return wanted;
  940. }
  941. struct dma_chunk {
  942. u64 addr;
  943. u64 size;
  944. };
  945. #define VETH_MAX_PAGES_PER_FRAME ( (VETH_MAX_MTU+PAGE_SIZE-2)/PAGE_SIZE + 1 )
  946. static inline void veth_build_dma_list(struct dma_chunk *list,
  947. unsigned char *p, unsigned long length)
  948. {
  949. unsigned long done;
  950. int i = 1;
  951. /* FIXME: skbs are continguous in real addresses. Do we
  952. * really need to break it into PAGE_SIZE chunks, or can we do
  953. * it just at the granularity of iSeries real->absolute
  954. * mapping? Indeed, given the way the allocator works, can we
  955. * count on them being absolutely contiguous? */
  956. list[0].addr = ISERIES_HV_ADDR(p);
  957. list[0].size = min(length,
  958. PAGE_SIZE - ((unsigned long)p & ~PAGE_MASK));
  959. done = list[0].size;
  960. while (done < length) {
  961. list[i].addr = ISERIES_HV_ADDR(p + done);
  962. list[i].size = min(length-done, PAGE_SIZE);
  963. done += list[i].size;
  964. i++;
  965. }
  966. }
  967. static void veth_flush_acks(struct veth_lpar_connection *cnx)
  968. {
  969. HvLpEvent_Rc rc;
  970. rc = veth_signaldata(cnx, VethEventTypeFramesAck,
  971. 0, &cnx->pending_acks);
  972. if (rc != HvLpEvent_Rc_Good)
  973. veth_error("Error 0x%x acking frames from lpar %d!\n",
  974. (unsigned)rc, cnx->remote_lp);
  975. cnx->num_pending_acks = 0;
  976. memset(&cnx->pending_acks, 0xff, sizeof(cnx->pending_acks));
  977. }
  978. static void veth_receive(struct veth_lpar_connection *cnx,
  979. struct VethLpEvent *event)
  980. {
  981. struct VethFramesData *senddata = &event->u.frames_data;
  982. int startchunk = 0;
  983. int nchunks;
  984. unsigned long flags;
  985. HvLpDma_Rc rc;
  986. do {
  987. u16 length = 0;
  988. struct sk_buff *skb;
  989. struct dma_chunk local_list[VETH_MAX_PAGES_PER_FRAME];
  990. struct dma_chunk remote_list[VETH_MAX_FRAMES_PER_MSG];
  991. u64 dest;
  992. HvLpVirtualLanIndex vlan;
  993. struct net_device *dev;
  994. struct veth_port *port;
  995. /* FIXME: do we need this? */
  996. memset(local_list, 0, sizeof(local_list));
  997. memset(remote_list, 0, sizeof(VETH_MAX_FRAMES_PER_MSG));
  998. /* a 0 address marks the end of the valid entries */
  999. if (senddata->addr[startchunk] == 0)
  1000. break;
  1001. /* make sure that we have at least 1 EOF entry in the
  1002. * remaining entries */
  1003. if (! (senddata->eofmask >> (startchunk + VETH_EOF_SHIFT))) {
  1004. veth_error("missing EOF frag in event "
  1005. "eofmask=0x%x startchunk=%d\n",
  1006. (unsigned) senddata->eofmask, startchunk);
  1007. break;
  1008. }
  1009. /* build list of chunks in this frame */
  1010. nchunks = 0;
  1011. do {
  1012. remote_list[nchunks].addr =
  1013. (u64) senddata->addr[startchunk+nchunks] << 32;
  1014. remote_list[nchunks].size =
  1015. senddata->len[startchunk+nchunks];
  1016. length += remote_list[nchunks].size;
  1017. } while (! (senddata->eofmask &
  1018. (1 << (VETH_EOF_SHIFT + startchunk + nchunks++))));
  1019. /* length == total length of all chunks */
  1020. /* nchunks == # of chunks in this frame */
  1021. if ((length - ETH_HLEN) > VETH_MAX_MTU) {
  1022. veth_error("Received oversize frame from lpar %d "
  1023. "(length=%d)\n", cnx->remote_lp, length);
  1024. continue;
  1025. }
  1026. skb = alloc_skb(length, GFP_ATOMIC);
  1027. if (!skb)
  1028. continue;
  1029. veth_build_dma_list(local_list, skb->data, length);
  1030. rc = HvCallEvent_dmaBufList(HvLpEvent_Type_VirtualLan,
  1031. event->base_event.xSourceLp,
  1032. HvLpDma_Direction_RemoteToLocal,
  1033. cnx->src_inst,
  1034. cnx->dst_inst,
  1035. HvLpDma_AddressType_RealAddress,
  1036. HvLpDma_AddressType_TceIndex,
  1037. ISERIES_HV_ADDR(&local_list),
  1038. ISERIES_HV_ADDR(&remote_list),
  1039. length);
  1040. if (rc != HvLpDma_Rc_Good) {
  1041. dev_kfree_skb_irq(skb);
  1042. continue;
  1043. }
  1044. vlan = skb->data[9];
  1045. dev = veth_dev[vlan];
  1046. if (! dev) {
  1047. /*
  1048. * Some earlier versions of the driver sent
  1049. * broadcasts down all connections, even to lpars
  1050. * that weren't on the relevant vlan. So ignore
  1051. * packets belonging to a vlan we're not on.
  1052. * We can also be here if we receive packets while
  1053. * the driver is going down, because then dev is NULL.
  1054. */
  1055. dev_kfree_skb_irq(skb);
  1056. continue;
  1057. }
  1058. port = (struct veth_port *)dev->priv;
  1059. dest = *((u64 *) skb->data) & 0xFFFFFFFFFFFF0000;
  1060. if ((vlan > HVMAXARCHITECTEDVIRTUALLANS) || !port) {
  1061. dev_kfree_skb_irq(skb);
  1062. continue;
  1063. }
  1064. if (! veth_frame_wanted(port, dest)) {
  1065. dev_kfree_skb_irq(skb);
  1066. continue;
  1067. }
  1068. skb_put(skb, length);
  1069. skb->dev = dev;
  1070. skb->protocol = eth_type_trans(skb, dev);
  1071. skb->ip_summed = CHECKSUM_NONE;
  1072. netif_rx(skb); /* send it up */
  1073. port->stats.rx_packets++;
  1074. port->stats.rx_bytes += length;
  1075. } while (startchunk += nchunks, startchunk < VETH_MAX_FRAMES_PER_MSG);
  1076. /* Ack it */
  1077. spin_lock_irqsave(&cnx->lock, flags);
  1078. BUG_ON(cnx->num_pending_acks > VETH_MAX_ACKS_PER_MSG);
  1079. cnx->pending_acks[cnx->num_pending_acks++] =
  1080. event->base_event.xCorrelationToken;
  1081. if ( (cnx->num_pending_acks >= cnx->remote_caps.ack_threshold)
  1082. || (cnx->num_pending_acks >= VETH_MAX_ACKS_PER_MSG) )
  1083. veth_flush_acks(cnx);
  1084. spin_unlock_irqrestore(&cnx->lock, flags);
  1085. }
  1086. static void veth_timed_ack(unsigned long ptr)
  1087. {
  1088. struct veth_lpar_connection *cnx = (struct veth_lpar_connection *) ptr;
  1089. unsigned long flags;
  1090. /* Ack all the events */
  1091. spin_lock_irqsave(&cnx->lock, flags);
  1092. if (cnx->num_pending_acks > 0)
  1093. veth_flush_acks(cnx);
  1094. /* Reschedule the timer */
  1095. cnx->ack_timer.expires = jiffies + cnx->ack_timeout;
  1096. add_timer(&cnx->ack_timer);
  1097. spin_unlock_irqrestore(&cnx->lock, flags);
  1098. }
  1099. static int veth_remove(struct vio_dev *vdev)
  1100. {
  1101. int i = vdev->unit_address;
  1102. struct net_device *dev;
  1103. dev = veth_dev[i];
  1104. if (dev != NULL) {
  1105. veth_dev[i] = NULL;
  1106. unregister_netdev(dev);
  1107. free_netdev(dev);
  1108. }
  1109. return 0;
  1110. }
  1111. static int veth_probe(struct vio_dev *vdev, const struct vio_device_id *id)
  1112. {
  1113. int i = vdev->unit_address;
  1114. struct net_device *dev;
  1115. dev = veth_probe_one(i, &vdev->dev);
  1116. if (dev == NULL) {
  1117. veth_remove(vdev);
  1118. return 1;
  1119. }
  1120. veth_dev[i] = dev;
  1121. /* Start the state machine on each connection, to commence
  1122. * link negotiation */
  1123. for (i = 0; i < HVMAXARCHITECTEDLPS; i++)
  1124. if (veth_cnx[i])
  1125. veth_kick_statemachine(veth_cnx[i]);
  1126. return 0;
  1127. }
  1128. /**
  1129. * veth_device_table: Used by vio.c to match devices that we
  1130. * support.
  1131. */
  1132. static struct vio_device_id veth_device_table[] __devinitdata = {
  1133. { "vlan", "" },
  1134. { NULL, NULL }
  1135. };
  1136. MODULE_DEVICE_TABLE(vio, veth_device_table);
  1137. static struct vio_driver veth_driver = {
  1138. .name = "iseries_veth",
  1139. .id_table = veth_device_table,
  1140. .probe = veth_probe,
  1141. .remove = veth_remove
  1142. };
  1143. /*
  1144. * Module initialization/cleanup
  1145. */
  1146. void __exit veth_module_cleanup(void)
  1147. {
  1148. int i;
  1149. /* Stop the queues first to stop any new packets being sent. */
  1150. for (i = 0; i < HVMAXARCHITECTEDVIRTUALLANS; i++)
  1151. if (veth_dev[i])
  1152. netif_stop_queue(veth_dev[i]);
  1153. /* Stop the connections before we unregister the driver. This
  1154. * ensures there's no skbs lying around holding the device open. */
  1155. for (i = 0; i < HVMAXARCHITECTEDLPS; ++i)
  1156. veth_stop_connection(i);
  1157. HvLpEvent_unregisterHandler(HvLpEvent_Type_VirtualLan);
  1158. /* Hypervisor callbacks may have scheduled more work while we
  1159. * were stoping connections. Now that we've disconnected from
  1160. * the hypervisor make sure everything's finished. */
  1161. flush_scheduled_work();
  1162. vio_unregister_driver(&veth_driver);
  1163. for (i = 0; i < HVMAXARCHITECTEDLPS; ++i)
  1164. veth_destroy_connection(i);
  1165. }
  1166. module_exit(veth_module_cleanup);
  1167. int __init veth_module_init(void)
  1168. {
  1169. int i;
  1170. int rc;
  1171. this_lp = HvLpConfig_getLpIndex_outline();
  1172. for (i = 0; i < HVMAXARCHITECTEDLPS; ++i) {
  1173. rc = veth_init_connection(i);
  1174. if (rc != 0) {
  1175. veth_module_cleanup();
  1176. return rc;
  1177. }
  1178. }
  1179. HvLpEvent_registerHandler(HvLpEvent_Type_VirtualLan,
  1180. &veth_handle_event);
  1181. return vio_register_driver(&veth_driver);
  1182. }
  1183. module_init(veth_module_init);