iseries_veth.c 38 KB

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