iseries_veth.c 38 KB

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