iseries_veth.c 37 KB

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