iseries_veth.c 36 KB

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