iseries_veth.c 44 KB

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