iseries_veth.c 43 KB

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