ibmveth.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * IBM Power Virtual Ethernet Device Driver
  4. *
  5. * Copyright (C) IBM Corporation, 2003, 2010
  6. *
  7. * Authors: Dave Larson <larson1@us.ibm.com>
  8. * Santiago Leon <santil@linux.vnet.ibm.com>
  9. * Brian King <brking@linux.vnet.ibm.com>
  10. * Robert Jennings <rcj@linux.vnet.ibm.com>
  11. * Anton Blanchard <anton@au.ibm.com>
  12. */
  13. #include <linux/module.h>
  14. #include <linux/types.h>
  15. #include <linux/errno.h>
  16. #include <linux/dma-mapping.h>
  17. #include <linux/kernel.h>
  18. #include <linux/netdevice.h>
  19. #include <linux/etherdevice.h>
  20. #include <linux/skbuff.h>
  21. #include <linux/init.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/mm.h>
  24. #include <linux/pm.h>
  25. #include <linux/ethtool.h>
  26. #include <linux/in.h>
  27. #include <linux/ip.h>
  28. #include <linux/ipv6.h>
  29. #include <linux/slab.h>
  30. #include <asm/hvcall.h>
  31. #include <linux/atomic.h>
  32. #include <asm/vio.h>
  33. #include <asm/iommu.h>
  34. #include <asm/firmware.h>
  35. #include <net/tcp.h>
  36. #include <net/ip6_checksum.h>
  37. #include "ibmveth.h"
  38. static irqreturn_t ibmveth_interrupt(int irq, void *dev_instance);
  39. static void ibmveth_rxq_harvest_buffer(struct ibmveth_adapter *adapter);
  40. static unsigned long ibmveth_get_desired_dma(struct vio_dev *vdev);
  41. static struct kobj_type ktype_veth_pool;
  42. static const char ibmveth_driver_name[] = "ibmveth";
  43. static const char ibmveth_driver_string[] = "IBM Power Virtual Ethernet Driver";
  44. #define ibmveth_driver_version "1.06"
  45. MODULE_AUTHOR("Santiago Leon <santil@linux.vnet.ibm.com>");
  46. MODULE_DESCRIPTION("IBM Power Virtual Ethernet Driver");
  47. MODULE_LICENSE("GPL");
  48. MODULE_VERSION(ibmveth_driver_version);
  49. static unsigned int tx_copybreak __read_mostly = 128;
  50. module_param(tx_copybreak, uint, 0644);
  51. MODULE_PARM_DESC(tx_copybreak,
  52. "Maximum size of packet that is copied to a new buffer on transmit");
  53. static unsigned int rx_copybreak __read_mostly = 128;
  54. module_param(rx_copybreak, uint, 0644);
  55. MODULE_PARM_DESC(rx_copybreak,
  56. "Maximum size of packet that is copied to a new buffer on receive");
  57. static unsigned int rx_flush __read_mostly = 0;
  58. module_param(rx_flush, uint, 0644);
  59. MODULE_PARM_DESC(rx_flush, "Flush receive buffers before use");
  60. static bool old_large_send __read_mostly;
  61. module_param(old_large_send, bool, 0444);
  62. MODULE_PARM_DESC(old_large_send,
  63. "Use old large send method on firmware that supports the new method");
  64. struct ibmveth_stat {
  65. char name[ETH_GSTRING_LEN];
  66. int offset;
  67. };
  68. #define IBMVETH_STAT_OFF(stat) offsetof(struct ibmveth_adapter, stat)
  69. #define IBMVETH_GET_STAT(a, off) *((u64 *)(((unsigned long)(a)) + off))
  70. static struct ibmveth_stat ibmveth_stats[] = {
  71. { "replenish_task_cycles", IBMVETH_STAT_OFF(replenish_task_cycles) },
  72. { "replenish_no_mem", IBMVETH_STAT_OFF(replenish_no_mem) },
  73. { "replenish_add_buff_failure",
  74. IBMVETH_STAT_OFF(replenish_add_buff_failure) },
  75. { "replenish_add_buff_success",
  76. IBMVETH_STAT_OFF(replenish_add_buff_success) },
  77. { "rx_invalid_buffer", IBMVETH_STAT_OFF(rx_invalid_buffer) },
  78. { "rx_no_buffer", IBMVETH_STAT_OFF(rx_no_buffer) },
  79. { "tx_map_failed", IBMVETH_STAT_OFF(tx_map_failed) },
  80. { "tx_send_failed", IBMVETH_STAT_OFF(tx_send_failed) },
  81. { "fw_enabled_ipv4_csum", IBMVETH_STAT_OFF(fw_ipv4_csum_support) },
  82. { "fw_enabled_ipv6_csum", IBMVETH_STAT_OFF(fw_ipv6_csum_support) },
  83. { "tx_large_packets", IBMVETH_STAT_OFF(tx_large_packets) },
  84. { "rx_large_packets", IBMVETH_STAT_OFF(rx_large_packets) },
  85. { "fw_enabled_large_send", IBMVETH_STAT_OFF(fw_large_send_support) }
  86. };
  87. /* simple methods of getting data from the current rxq entry */
  88. static inline u32 ibmveth_rxq_flags(struct ibmveth_adapter *adapter)
  89. {
  90. return be32_to_cpu(adapter->rx_queue.queue_addr[adapter->rx_queue.index].flags_off);
  91. }
  92. static inline int ibmveth_rxq_toggle(struct ibmveth_adapter *adapter)
  93. {
  94. return (ibmveth_rxq_flags(adapter) & IBMVETH_RXQ_TOGGLE) >>
  95. IBMVETH_RXQ_TOGGLE_SHIFT;
  96. }
  97. static inline int ibmveth_rxq_pending_buffer(struct ibmveth_adapter *adapter)
  98. {
  99. return ibmveth_rxq_toggle(adapter) == adapter->rx_queue.toggle;
  100. }
  101. static inline int ibmveth_rxq_buffer_valid(struct ibmveth_adapter *adapter)
  102. {
  103. return ibmveth_rxq_flags(adapter) & IBMVETH_RXQ_VALID;
  104. }
  105. static inline int ibmveth_rxq_frame_offset(struct ibmveth_adapter *adapter)
  106. {
  107. return ibmveth_rxq_flags(adapter) & IBMVETH_RXQ_OFF_MASK;
  108. }
  109. static inline int ibmveth_rxq_large_packet(struct ibmveth_adapter *adapter)
  110. {
  111. return ibmveth_rxq_flags(adapter) & IBMVETH_RXQ_LRG_PKT;
  112. }
  113. static inline int ibmveth_rxq_frame_length(struct ibmveth_adapter *adapter)
  114. {
  115. return be32_to_cpu(adapter->rx_queue.queue_addr[adapter->rx_queue.index].length);
  116. }
  117. static inline int ibmveth_rxq_csum_good(struct ibmveth_adapter *adapter)
  118. {
  119. return ibmveth_rxq_flags(adapter) & IBMVETH_RXQ_CSUM_GOOD;
  120. }
  121. /* setup the initial settings for a buffer pool */
  122. static void ibmveth_init_buffer_pool(struct ibmveth_buff_pool *pool,
  123. u32 pool_index, u32 pool_size,
  124. u32 buff_size, u32 pool_active)
  125. {
  126. pool->size = pool_size;
  127. pool->index = pool_index;
  128. pool->buff_size = buff_size;
  129. pool->threshold = pool_size * 7 / 8;
  130. pool->active = pool_active;
  131. }
  132. /* allocate and setup an buffer pool - called during open */
  133. static int ibmveth_alloc_buffer_pool(struct ibmveth_buff_pool *pool)
  134. {
  135. int i;
  136. pool->free_map = kmalloc_array(pool->size, sizeof(u16), GFP_KERNEL);
  137. if (!pool->free_map)
  138. return -1;
  139. pool->dma_addr = kcalloc(pool->size, sizeof(dma_addr_t), GFP_KERNEL);
  140. if (!pool->dma_addr) {
  141. kfree(pool->free_map);
  142. pool->free_map = NULL;
  143. return -1;
  144. }
  145. pool->skbuff = kcalloc(pool->size, sizeof(void *), GFP_KERNEL);
  146. if (!pool->skbuff) {
  147. kfree(pool->dma_addr);
  148. pool->dma_addr = NULL;
  149. kfree(pool->free_map);
  150. pool->free_map = NULL;
  151. return -1;
  152. }
  153. for (i = 0; i < pool->size; ++i)
  154. pool->free_map[i] = i;
  155. atomic_set(&pool->available, 0);
  156. pool->producer_index = 0;
  157. pool->consumer_index = 0;
  158. return 0;
  159. }
  160. static inline void ibmveth_flush_buffer(void *addr, unsigned long length)
  161. {
  162. unsigned long offset;
  163. for (offset = 0; offset < length; offset += SMP_CACHE_BYTES)
  164. asm("dcbfl %0,%1" :: "b" (addr), "r" (offset));
  165. }
  166. /* replenish the buffers for a pool. note that we don't need to
  167. * skb_reserve these since they are used for incoming...
  168. */
  169. static void ibmveth_replenish_buffer_pool(struct ibmveth_adapter *adapter,
  170. struct ibmveth_buff_pool *pool)
  171. {
  172. u32 i;
  173. u32 count = pool->size - atomic_read(&pool->available);
  174. u32 buffers_added = 0;
  175. struct sk_buff *skb;
  176. unsigned int free_index, index;
  177. u64 correlator;
  178. unsigned long lpar_rc;
  179. dma_addr_t dma_addr;
  180. mb();
  181. for (i = 0; i < count; ++i) {
  182. union ibmveth_buf_desc desc;
  183. skb = netdev_alloc_skb(adapter->netdev, pool->buff_size);
  184. if (!skb) {
  185. netdev_dbg(adapter->netdev,
  186. "replenish: unable to allocate skb\n");
  187. adapter->replenish_no_mem++;
  188. break;
  189. }
  190. free_index = pool->consumer_index;
  191. pool->consumer_index++;
  192. if (pool->consumer_index >= pool->size)
  193. pool->consumer_index = 0;
  194. index = pool->free_map[free_index];
  195. BUG_ON(index == IBM_VETH_INVALID_MAP);
  196. BUG_ON(pool->skbuff[index] != NULL);
  197. dma_addr = dma_map_single(&adapter->vdev->dev, skb->data,
  198. pool->buff_size, DMA_FROM_DEVICE);
  199. if (dma_mapping_error(&adapter->vdev->dev, dma_addr))
  200. goto failure;
  201. pool->free_map[free_index] = IBM_VETH_INVALID_MAP;
  202. pool->dma_addr[index] = dma_addr;
  203. pool->skbuff[index] = skb;
  204. correlator = ((u64)pool->index << 32) | index;
  205. *(u64 *)skb->data = correlator;
  206. desc.fields.flags_len = IBMVETH_BUF_VALID | pool->buff_size;
  207. desc.fields.address = dma_addr;
  208. if (rx_flush) {
  209. unsigned int len = min(pool->buff_size,
  210. adapter->netdev->mtu +
  211. IBMVETH_BUFF_OH);
  212. ibmveth_flush_buffer(skb->data, len);
  213. }
  214. lpar_rc = h_add_logical_lan_buffer(adapter->vdev->unit_address,
  215. desc.desc);
  216. if (lpar_rc != H_SUCCESS) {
  217. goto failure;
  218. } else {
  219. buffers_added++;
  220. adapter->replenish_add_buff_success++;
  221. }
  222. }
  223. mb();
  224. atomic_add(buffers_added, &(pool->available));
  225. return;
  226. failure:
  227. pool->free_map[free_index] = index;
  228. pool->skbuff[index] = NULL;
  229. if (pool->consumer_index == 0)
  230. pool->consumer_index = pool->size - 1;
  231. else
  232. pool->consumer_index--;
  233. if (!dma_mapping_error(&adapter->vdev->dev, dma_addr))
  234. dma_unmap_single(&adapter->vdev->dev,
  235. pool->dma_addr[index], pool->buff_size,
  236. DMA_FROM_DEVICE);
  237. dev_kfree_skb_any(skb);
  238. adapter->replenish_add_buff_failure++;
  239. mb();
  240. atomic_add(buffers_added, &(pool->available));
  241. }
  242. /*
  243. * The final 8 bytes of the buffer list is a counter of frames dropped
  244. * because there was not a buffer in the buffer list capable of holding
  245. * the frame.
  246. */
  247. static void ibmveth_update_rx_no_buffer(struct ibmveth_adapter *adapter)
  248. {
  249. __be64 *p = adapter->buffer_list_addr + 4096 - 8;
  250. adapter->rx_no_buffer = be64_to_cpup(p);
  251. }
  252. /* replenish routine */
  253. static void ibmveth_replenish_task(struct ibmveth_adapter *adapter)
  254. {
  255. int i;
  256. adapter->replenish_task_cycles++;
  257. for (i = (IBMVETH_NUM_BUFF_POOLS - 1); i >= 0; i--) {
  258. struct ibmveth_buff_pool *pool = &adapter->rx_buff_pool[i];
  259. if (pool->active &&
  260. (atomic_read(&pool->available) < pool->threshold))
  261. ibmveth_replenish_buffer_pool(adapter, pool);
  262. }
  263. ibmveth_update_rx_no_buffer(adapter);
  264. }
  265. /* empty and free ana buffer pool - also used to do cleanup in error paths */
  266. static void ibmveth_free_buffer_pool(struct ibmveth_adapter *adapter,
  267. struct ibmveth_buff_pool *pool)
  268. {
  269. int i;
  270. kfree(pool->free_map);
  271. pool->free_map = NULL;
  272. if (pool->skbuff && pool->dma_addr) {
  273. for (i = 0; i < pool->size; ++i) {
  274. struct sk_buff *skb = pool->skbuff[i];
  275. if (skb) {
  276. dma_unmap_single(&adapter->vdev->dev,
  277. pool->dma_addr[i],
  278. pool->buff_size,
  279. DMA_FROM_DEVICE);
  280. dev_kfree_skb_any(skb);
  281. pool->skbuff[i] = NULL;
  282. }
  283. }
  284. }
  285. if (pool->dma_addr) {
  286. kfree(pool->dma_addr);
  287. pool->dma_addr = NULL;
  288. }
  289. if (pool->skbuff) {
  290. kfree(pool->skbuff);
  291. pool->skbuff = NULL;
  292. }
  293. }
  294. /* remove a buffer from a pool */
  295. static void ibmveth_remove_buffer_from_pool(struct ibmveth_adapter *adapter,
  296. u64 correlator)
  297. {
  298. unsigned int pool = correlator >> 32;
  299. unsigned int index = correlator & 0xffffffffUL;
  300. unsigned int free_index;
  301. struct sk_buff *skb;
  302. BUG_ON(pool >= IBMVETH_NUM_BUFF_POOLS);
  303. BUG_ON(index >= adapter->rx_buff_pool[pool].size);
  304. skb = adapter->rx_buff_pool[pool].skbuff[index];
  305. BUG_ON(skb == NULL);
  306. adapter->rx_buff_pool[pool].skbuff[index] = NULL;
  307. dma_unmap_single(&adapter->vdev->dev,
  308. adapter->rx_buff_pool[pool].dma_addr[index],
  309. adapter->rx_buff_pool[pool].buff_size,
  310. DMA_FROM_DEVICE);
  311. free_index = adapter->rx_buff_pool[pool].producer_index;
  312. adapter->rx_buff_pool[pool].producer_index++;
  313. if (adapter->rx_buff_pool[pool].producer_index >=
  314. adapter->rx_buff_pool[pool].size)
  315. adapter->rx_buff_pool[pool].producer_index = 0;
  316. adapter->rx_buff_pool[pool].free_map[free_index] = index;
  317. mb();
  318. atomic_dec(&(adapter->rx_buff_pool[pool].available));
  319. }
  320. /* get the current buffer on the rx queue */
  321. static inline struct sk_buff *ibmveth_rxq_get_buffer(struct ibmveth_adapter *adapter)
  322. {
  323. u64 correlator = adapter->rx_queue.queue_addr[adapter->rx_queue.index].correlator;
  324. unsigned int pool = correlator >> 32;
  325. unsigned int index = correlator & 0xffffffffUL;
  326. BUG_ON(pool >= IBMVETH_NUM_BUFF_POOLS);
  327. BUG_ON(index >= adapter->rx_buff_pool[pool].size);
  328. return adapter->rx_buff_pool[pool].skbuff[index];
  329. }
  330. /* recycle the current buffer on the rx queue */
  331. static int ibmveth_rxq_recycle_buffer(struct ibmveth_adapter *adapter)
  332. {
  333. u32 q_index = adapter->rx_queue.index;
  334. u64 correlator = adapter->rx_queue.queue_addr[q_index].correlator;
  335. unsigned int pool = correlator >> 32;
  336. unsigned int index = correlator & 0xffffffffUL;
  337. union ibmveth_buf_desc desc;
  338. unsigned long lpar_rc;
  339. int ret = 1;
  340. BUG_ON(pool >= IBMVETH_NUM_BUFF_POOLS);
  341. BUG_ON(index >= adapter->rx_buff_pool[pool].size);
  342. if (!adapter->rx_buff_pool[pool].active) {
  343. ibmveth_rxq_harvest_buffer(adapter);
  344. ibmveth_free_buffer_pool(adapter, &adapter->rx_buff_pool[pool]);
  345. goto out;
  346. }
  347. desc.fields.flags_len = IBMVETH_BUF_VALID |
  348. adapter->rx_buff_pool[pool].buff_size;
  349. desc.fields.address = adapter->rx_buff_pool[pool].dma_addr[index];
  350. lpar_rc = h_add_logical_lan_buffer(adapter->vdev->unit_address, desc.desc);
  351. if (lpar_rc != H_SUCCESS) {
  352. netdev_dbg(adapter->netdev, "h_add_logical_lan_buffer failed "
  353. "during recycle rc=%ld", lpar_rc);
  354. ibmveth_remove_buffer_from_pool(adapter, adapter->rx_queue.queue_addr[adapter->rx_queue.index].correlator);
  355. ret = 0;
  356. }
  357. if (++adapter->rx_queue.index == adapter->rx_queue.num_slots) {
  358. adapter->rx_queue.index = 0;
  359. adapter->rx_queue.toggle = !adapter->rx_queue.toggle;
  360. }
  361. out:
  362. return ret;
  363. }
  364. static void ibmveth_rxq_harvest_buffer(struct ibmveth_adapter *adapter)
  365. {
  366. ibmveth_remove_buffer_from_pool(adapter, adapter->rx_queue.queue_addr[adapter->rx_queue.index].correlator);
  367. if (++adapter->rx_queue.index == adapter->rx_queue.num_slots) {
  368. adapter->rx_queue.index = 0;
  369. adapter->rx_queue.toggle = !adapter->rx_queue.toggle;
  370. }
  371. }
  372. static int ibmveth_register_logical_lan(struct ibmveth_adapter *adapter,
  373. union ibmveth_buf_desc rxq_desc, u64 mac_address)
  374. {
  375. int rc, try_again = 1;
  376. /*
  377. * After a kexec the adapter will still be open, so our attempt to
  378. * open it will fail. So if we get a failure we free the adapter and
  379. * try again, but only once.
  380. */
  381. retry:
  382. rc = h_register_logical_lan(adapter->vdev->unit_address,
  383. adapter->buffer_list_dma, rxq_desc.desc,
  384. adapter->filter_list_dma, mac_address);
  385. if (rc != H_SUCCESS && try_again) {
  386. do {
  387. rc = h_free_logical_lan(adapter->vdev->unit_address);
  388. } while (H_IS_LONG_BUSY(rc) || (rc == H_BUSY));
  389. try_again = 0;
  390. goto retry;
  391. }
  392. return rc;
  393. }
  394. static u64 ibmveth_encode_mac_addr(u8 *mac)
  395. {
  396. int i;
  397. u64 encoded = 0;
  398. for (i = 0; i < ETH_ALEN; i++)
  399. encoded = (encoded << 8) | mac[i];
  400. return encoded;
  401. }
  402. static int ibmveth_open(struct net_device *netdev)
  403. {
  404. struct ibmveth_adapter *adapter = netdev_priv(netdev);
  405. u64 mac_address;
  406. int rxq_entries = 1;
  407. unsigned long lpar_rc;
  408. int rc;
  409. union ibmveth_buf_desc rxq_desc;
  410. int i;
  411. struct device *dev;
  412. netdev_dbg(netdev, "open starting\n");
  413. napi_enable(&adapter->napi);
  414. for(i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++)
  415. rxq_entries += adapter->rx_buff_pool[i].size;
  416. rc = -ENOMEM;
  417. adapter->buffer_list_addr = (void*) get_zeroed_page(GFP_KERNEL);
  418. if (!adapter->buffer_list_addr) {
  419. netdev_err(netdev, "unable to allocate list pages\n");
  420. goto out;
  421. }
  422. adapter->filter_list_addr = (void*) get_zeroed_page(GFP_KERNEL);
  423. if (!adapter->filter_list_addr) {
  424. netdev_err(netdev, "unable to allocate filter pages\n");
  425. goto out_free_buffer_list;
  426. }
  427. dev = &adapter->vdev->dev;
  428. adapter->rx_queue.queue_len = sizeof(struct ibmveth_rx_q_entry) *
  429. rxq_entries;
  430. adapter->rx_queue.queue_addr =
  431. dma_alloc_coherent(dev, adapter->rx_queue.queue_len,
  432. &adapter->rx_queue.queue_dma, GFP_KERNEL);
  433. if (!adapter->rx_queue.queue_addr)
  434. goto out_free_filter_list;
  435. adapter->buffer_list_dma = dma_map_single(dev,
  436. adapter->buffer_list_addr, 4096, DMA_BIDIRECTIONAL);
  437. if (dma_mapping_error(dev, adapter->buffer_list_dma)) {
  438. netdev_err(netdev, "unable to map buffer list pages\n");
  439. goto out_free_queue_mem;
  440. }
  441. adapter->filter_list_dma = dma_map_single(dev,
  442. adapter->filter_list_addr, 4096, DMA_BIDIRECTIONAL);
  443. if (dma_mapping_error(dev, adapter->filter_list_dma)) {
  444. netdev_err(netdev, "unable to map filter list pages\n");
  445. goto out_unmap_buffer_list;
  446. }
  447. adapter->rx_queue.index = 0;
  448. adapter->rx_queue.num_slots = rxq_entries;
  449. adapter->rx_queue.toggle = 1;
  450. mac_address = ibmveth_encode_mac_addr(netdev->dev_addr);
  451. rxq_desc.fields.flags_len = IBMVETH_BUF_VALID |
  452. adapter->rx_queue.queue_len;
  453. rxq_desc.fields.address = adapter->rx_queue.queue_dma;
  454. netdev_dbg(netdev, "buffer list @ 0x%p\n", adapter->buffer_list_addr);
  455. netdev_dbg(netdev, "filter list @ 0x%p\n", adapter->filter_list_addr);
  456. netdev_dbg(netdev, "receive q @ 0x%p\n", adapter->rx_queue.queue_addr);
  457. h_vio_signal(adapter->vdev->unit_address, VIO_IRQ_DISABLE);
  458. lpar_rc = ibmveth_register_logical_lan(adapter, rxq_desc, mac_address);
  459. if (lpar_rc != H_SUCCESS) {
  460. netdev_err(netdev, "h_register_logical_lan failed with %ld\n",
  461. lpar_rc);
  462. netdev_err(netdev, "buffer TCE:0x%llx filter TCE:0x%llx rxq "
  463. "desc:0x%llx MAC:0x%llx\n",
  464. adapter->buffer_list_dma,
  465. adapter->filter_list_dma,
  466. rxq_desc.desc,
  467. mac_address);
  468. rc = -ENONET;
  469. goto out_unmap_filter_list;
  470. }
  471. for (i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++) {
  472. if (!adapter->rx_buff_pool[i].active)
  473. continue;
  474. if (ibmveth_alloc_buffer_pool(&adapter->rx_buff_pool[i])) {
  475. netdev_err(netdev, "unable to alloc pool\n");
  476. adapter->rx_buff_pool[i].active = 0;
  477. rc = -ENOMEM;
  478. goto out_free_buffer_pools;
  479. }
  480. }
  481. netdev_dbg(netdev, "registering irq 0x%x\n", netdev->irq);
  482. rc = request_irq(netdev->irq, ibmveth_interrupt, 0, netdev->name,
  483. netdev);
  484. if (rc != 0) {
  485. netdev_err(netdev, "unable to request irq 0x%x, rc %d\n",
  486. netdev->irq, rc);
  487. do {
  488. lpar_rc = h_free_logical_lan(adapter->vdev->unit_address);
  489. } while (H_IS_LONG_BUSY(lpar_rc) || (lpar_rc == H_BUSY));
  490. goto out_free_buffer_pools;
  491. }
  492. rc = -ENOMEM;
  493. adapter->bounce_buffer =
  494. kmalloc(netdev->mtu + IBMVETH_BUFF_OH, GFP_KERNEL);
  495. if (!adapter->bounce_buffer)
  496. goto out_free_irq;
  497. adapter->bounce_buffer_dma =
  498. dma_map_single(&adapter->vdev->dev, adapter->bounce_buffer,
  499. netdev->mtu + IBMVETH_BUFF_OH, DMA_BIDIRECTIONAL);
  500. if (dma_mapping_error(dev, adapter->bounce_buffer_dma)) {
  501. netdev_err(netdev, "unable to map bounce buffer\n");
  502. goto out_free_bounce_buffer;
  503. }
  504. netdev_dbg(netdev, "initial replenish cycle\n");
  505. ibmveth_interrupt(netdev->irq, netdev);
  506. netif_start_queue(netdev);
  507. netdev_dbg(netdev, "open complete\n");
  508. return 0;
  509. out_free_bounce_buffer:
  510. kfree(adapter->bounce_buffer);
  511. out_free_irq:
  512. free_irq(netdev->irq, netdev);
  513. out_free_buffer_pools:
  514. while (--i >= 0) {
  515. if (adapter->rx_buff_pool[i].active)
  516. ibmveth_free_buffer_pool(adapter,
  517. &adapter->rx_buff_pool[i]);
  518. }
  519. out_unmap_filter_list:
  520. dma_unmap_single(dev, adapter->filter_list_dma, 4096,
  521. DMA_BIDIRECTIONAL);
  522. out_unmap_buffer_list:
  523. dma_unmap_single(dev, adapter->buffer_list_dma, 4096,
  524. DMA_BIDIRECTIONAL);
  525. out_free_queue_mem:
  526. dma_free_coherent(dev, adapter->rx_queue.queue_len,
  527. adapter->rx_queue.queue_addr,
  528. adapter->rx_queue.queue_dma);
  529. out_free_filter_list:
  530. free_page((unsigned long)adapter->filter_list_addr);
  531. out_free_buffer_list:
  532. free_page((unsigned long)adapter->buffer_list_addr);
  533. out:
  534. napi_disable(&adapter->napi);
  535. return rc;
  536. }
  537. static int ibmveth_close(struct net_device *netdev)
  538. {
  539. struct ibmveth_adapter *adapter = netdev_priv(netdev);
  540. struct device *dev = &adapter->vdev->dev;
  541. long lpar_rc;
  542. int i;
  543. netdev_dbg(netdev, "close starting\n");
  544. napi_disable(&adapter->napi);
  545. if (!adapter->pool_config)
  546. netif_stop_queue(netdev);
  547. h_vio_signal(adapter->vdev->unit_address, VIO_IRQ_DISABLE);
  548. do {
  549. lpar_rc = h_free_logical_lan(adapter->vdev->unit_address);
  550. } while (H_IS_LONG_BUSY(lpar_rc) || (lpar_rc == H_BUSY));
  551. if (lpar_rc != H_SUCCESS) {
  552. netdev_err(netdev, "h_free_logical_lan failed with %lx, "
  553. "continuing with close\n", lpar_rc);
  554. }
  555. free_irq(netdev->irq, netdev);
  556. ibmveth_update_rx_no_buffer(adapter);
  557. dma_unmap_single(dev, adapter->buffer_list_dma, 4096,
  558. DMA_BIDIRECTIONAL);
  559. free_page((unsigned long)adapter->buffer_list_addr);
  560. dma_unmap_single(dev, adapter->filter_list_dma, 4096,
  561. DMA_BIDIRECTIONAL);
  562. free_page((unsigned long)adapter->filter_list_addr);
  563. dma_free_coherent(dev, adapter->rx_queue.queue_len,
  564. adapter->rx_queue.queue_addr,
  565. adapter->rx_queue.queue_dma);
  566. for (i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++)
  567. if (adapter->rx_buff_pool[i].active)
  568. ibmveth_free_buffer_pool(adapter,
  569. &adapter->rx_buff_pool[i]);
  570. dma_unmap_single(&adapter->vdev->dev, adapter->bounce_buffer_dma,
  571. adapter->netdev->mtu + IBMVETH_BUFF_OH,
  572. DMA_BIDIRECTIONAL);
  573. kfree(adapter->bounce_buffer);
  574. netdev_dbg(netdev, "close complete\n");
  575. return 0;
  576. }
  577. static int netdev_get_link_ksettings(struct net_device *dev,
  578. struct ethtool_link_ksettings *cmd)
  579. {
  580. u32 supported, advertising;
  581. supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg |
  582. SUPPORTED_FIBRE);
  583. advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg |
  584. ADVERTISED_FIBRE);
  585. cmd->base.speed = SPEED_1000;
  586. cmd->base.duplex = DUPLEX_FULL;
  587. cmd->base.port = PORT_FIBRE;
  588. cmd->base.phy_address = 0;
  589. cmd->base.autoneg = AUTONEG_ENABLE;
  590. ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
  591. supported);
  592. ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
  593. advertising);
  594. return 0;
  595. }
  596. static void netdev_get_drvinfo(struct net_device *dev,
  597. struct ethtool_drvinfo *info)
  598. {
  599. strlcpy(info->driver, ibmveth_driver_name, sizeof(info->driver));
  600. strlcpy(info->version, ibmveth_driver_version, sizeof(info->version));
  601. }
  602. static netdev_features_t ibmveth_fix_features(struct net_device *dev,
  603. netdev_features_t features)
  604. {
  605. /*
  606. * Since the ibmveth firmware interface does not have the
  607. * concept of separate tx/rx checksum offload enable, if rx
  608. * checksum is disabled we also have to disable tx checksum
  609. * offload. Once we disable rx checksum offload, we are no
  610. * longer allowed to send tx buffers that are not properly
  611. * checksummed.
  612. */
  613. if (!(features & NETIF_F_RXCSUM))
  614. features &= ~NETIF_F_CSUM_MASK;
  615. return features;
  616. }
  617. static int ibmveth_set_csum_offload(struct net_device *dev, u32 data)
  618. {
  619. struct ibmveth_adapter *adapter = netdev_priv(dev);
  620. unsigned long set_attr, clr_attr, ret_attr;
  621. unsigned long set_attr6, clr_attr6;
  622. long ret, ret4, ret6;
  623. int rc1 = 0, rc2 = 0;
  624. int restart = 0;
  625. if (netif_running(dev)) {
  626. restart = 1;
  627. adapter->pool_config = 1;
  628. ibmveth_close(dev);
  629. adapter->pool_config = 0;
  630. }
  631. set_attr = 0;
  632. clr_attr = 0;
  633. set_attr6 = 0;
  634. clr_attr6 = 0;
  635. if (data) {
  636. set_attr = IBMVETH_ILLAN_IPV4_TCP_CSUM;
  637. set_attr6 = IBMVETH_ILLAN_IPV6_TCP_CSUM;
  638. } else {
  639. clr_attr = IBMVETH_ILLAN_IPV4_TCP_CSUM;
  640. clr_attr6 = IBMVETH_ILLAN_IPV6_TCP_CSUM;
  641. }
  642. ret = h_illan_attributes(adapter->vdev->unit_address, 0, 0, &ret_attr);
  643. if (ret == H_SUCCESS &&
  644. (ret_attr & IBMVETH_ILLAN_PADDED_PKT_CSUM)) {
  645. ret4 = h_illan_attributes(adapter->vdev->unit_address, clr_attr,
  646. set_attr, &ret_attr);
  647. if (ret4 != H_SUCCESS) {
  648. netdev_err(dev, "unable to change IPv4 checksum "
  649. "offload settings. %d rc=%ld\n",
  650. data, ret4);
  651. h_illan_attributes(adapter->vdev->unit_address,
  652. set_attr, clr_attr, &ret_attr);
  653. if (data == 1)
  654. dev->features &= ~NETIF_F_IP_CSUM;
  655. } else {
  656. adapter->fw_ipv4_csum_support = data;
  657. }
  658. ret6 = h_illan_attributes(adapter->vdev->unit_address,
  659. clr_attr6, set_attr6, &ret_attr);
  660. if (ret6 != H_SUCCESS) {
  661. netdev_err(dev, "unable to change IPv6 checksum "
  662. "offload settings. %d rc=%ld\n",
  663. data, ret6);
  664. h_illan_attributes(adapter->vdev->unit_address,
  665. set_attr6, clr_attr6, &ret_attr);
  666. if (data == 1)
  667. dev->features &= ~NETIF_F_IPV6_CSUM;
  668. } else
  669. adapter->fw_ipv6_csum_support = data;
  670. if (ret4 == H_SUCCESS || ret6 == H_SUCCESS)
  671. adapter->rx_csum = data;
  672. else
  673. rc1 = -EIO;
  674. } else {
  675. rc1 = -EIO;
  676. netdev_err(dev, "unable to change checksum offload settings."
  677. " %d rc=%ld ret_attr=%lx\n", data, ret,
  678. ret_attr);
  679. }
  680. if (restart)
  681. rc2 = ibmveth_open(dev);
  682. return rc1 ? rc1 : rc2;
  683. }
  684. static int ibmveth_set_tso(struct net_device *dev, u32 data)
  685. {
  686. struct ibmveth_adapter *adapter = netdev_priv(dev);
  687. unsigned long set_attr, clr_attr, ret_attr;
  688. long ret1, ret2;
  689. int rc1 = 0, rc2 = 0;
  690. int restart = 0;
  691. if (netif_running(dev)) {
  692. restart = 1;
  693. adapter->pool_config = 1;
  694. ibmveth_close(dev);
  695. adapter->pool_config = 0;
  696. }
  697. set_attr = 0;
  698. clr_attr = 0;
  699. if (data)
  700. set_attr = IBMVETH_ILLAN_LRG_SR_ENABLED;
  701. else
  702. clr_attr = IBMVETH_ILLAN_LRG_SR_ENABLED;
  703. ret1 = h_illan_attributes(adapter->vdev->unit_address, 0, 0, &ret_attr);
  704. if (ret1 == H_SUCCESS && (ret_attr & IBMVETH_ILLAN_LRG_SND_SUPPORT) &&
  705. !old_large_send) {
  706. ret2 = h_illan_attributes(adapter->vdev->unit_address, clr_attr,
  707. set_attr, &ret_attr);
  708. if (ret2 != H_SUCCESS) {
  709. netdev_err(dev, "unable to change tso settings. %d rc=%ld\n",
  710. data, ret2);
  711. h_illan_attributes(adapter->vdev->unit_address,
  712. set_attr, clr_attr, &ret_attr);
  713. if (data == 1)
  714. dev->features &= ~(NETIF_F_TSO | NETIF_F_TSO6);
  715. rc1 = -EIO;
  716. } else {
  717. adapter->fw_large_send_support = data;
  718. adapter->large_send = data;
  719. }
  720. } else {
  721. /* Older firmware version of large send offload does not
  722. * support tcp6/ipv6
  723. */
  724. if (data == 1) {
  725. dev->features &= ~NETIF_F_TSO6;
  726. netdev_info(dev, "TSO feature requires all partitions to have updated driver");
  727. }
  728. adapter->large_send = data;
  729. }
  730. if (restart)
  731. rc2 = ibmveth_open(dev);
  732. return rc1 ? rc1 : rc2;
  733. }
  734. static int ibmveth_set_features(struct net_device *dev,
  735. netdev_features_t features)
  736. {
  737. struct ibmveth_adapter *adapter = netdev_priv(dev);
  738. int rx_csum = !!(features & NETIF_F_RXCSUM);
  739. int large_send = !!(features & (NETIF_F_TSO | NETIF_F_TSO6));
  740. int rc1 = 0, rc2 = 0;
  741. if (rx_csum != adapter->rx_csum) {
  742. rc1 = ibmveth_set_csum_offload(dev, rx_csum);
  743. if (rc1 && !adapter->rx_csum)
  744. dev->features =
  745. features & ~(NETIF_F_CSUM_MASK |
  746. NETIF_F_RXCSUM);
  747. }
  748. if (large_send != adapter->large_send) {
  749. rc2 = ibmveth_set_tso(dev, large_send);
  750. if (rc2 && !adapter->large_send)
  751. dev->features =
  752. features & ~(NETIF_F_TSO | NETIF_F_TSO6);
  753. }
  754. return rc1 ? rc1 : rc2;
  755. }
  756. static void ibmveth_get_strings(struct net_device *dev, u32 stringset, u8 *data)
  757. {
  758. int i;
  759. if (stringset != ETH_SS_STATS)
  760. return;
  761. for (i = 0; i < ARRAY_SIZE(ibmveth_stats); i++, data += ETH_GSTRING_LEN)
  762. memcpy(data, ibmveth_stats[i].name, ETH_GSTRING_LEN);
  763. }
  764. static int ibmveth_get_sset_count(struct net_device *dev, int sset)
  765. {
  766. switch (sset) {
  767. case ETH_SS_STATS:
  768. return ARRAY_SIZE(ibmveth_stats);
  769. default:
  770. return -EOPNOTSUPP;
  771. }
  772. }
  773. static void ibmveth_get_ethtool_stats(struct net_device *dev,
  774. struct ethtool_stats *stats, u64 *data)
  775. {
  776. int i;
  777. struct ibmveth_adapter *adapter = netdev_priv(dev);
  778. for (i = 0; i < ARRAY_SIZE(ibmveth_stats); i++)
  779. data[i] = IBMVETH_GET_STAT(adapter, ibmveth_stats[i].offset);
  780. }
  781. static const struct ethtool_ops netdev_ethtool_ops = {
  782. .get_drvinfo = netdev_get_drvinfo,
  783. .get_link = ethtool_op_get_link,
  784. .get_strings = ibmveth_get_strings,
  785. .get_sset_count = ibmveth_get_sset_count,
  786. .get_ethtool_stats = ibmveth_get_ethtool_stats,
  787. .get_link_ksettings = netdev_get_link_ksettings,
  788. };
  789. static int ibmveth_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
  790. {
  791. return -EOPNOTSUPP;
  792. }
  793. #define page_offset(v) ((unsigned long)(v) & ((1 << 12) - 1))
  794. static int ibmveth_send(struct ibmveth_adapter *adapter,
  795. union ibmveth_buf_desc *descs, unsigned long mss)
  796. {
  797. unsigned long correlator;
  798. unsigned int retry_count;
  799. unsigned long ret;
  800. /*
  801. * The retry count sets a maximum for the number of broadcast and
  802. * multicast destinations within the system.
  803. */
  804. retry_count = 1024;
  805. correlator = 0;
  806. do {
  807. ret = h_send_logical_lan(adapter->vdev->unit_address,
  808. descs[0].desc, descs[1].desc,
  809. descs[2].desc, descs[3].desc,
  810. descs[4].desc, descs[5].desc,
  811. correlator, &correlator, mss,
  812. adapter->fw_large_send_support);
  813. } while ((ret == H_BUSY) && (retry_count--));
  814. if (ret != H_SUCCESS && ret != H_DROPPED) {
  815. netdev_err(adapter->netdev, "tx: h_send_logical_lan failed "
  816. "with rc=%ld\n", ret);
  817. return 1;
  818. }
  819. return 0;
  820. }
  821. static netdev_tx_t ibmveth_start_xmit(struct sk_buff *skb,
  822. struct net_device *netdev)
  823. {
  824. struct ibmveth_adapter *adapter = netdev_priv(netdev);
  825. unsigned int desc_flags;
  826. union ibmveth_buf_desc descs[6];
  827. int last, i;
  828. int force_bounce = 0;
  829. dma_addr_t dma_addr;
  830. unsigned long mss = 0;
  831. /* veth doesn't handle frag_list, so linearize the skb.
  832. * When GRO is enabled SKB's can have frag_list.
  833. */
  834. if (adapter->is_active_trunk &&
  835. skb_has_frag_list(skb) && __skb_linearize(skb)) {
  836. netdev->stats.tx_dropped++;
  837. goto out;
  838. }
  839. /*
  840. * veth handles a maximum of 6 segments including the header, so
  841. * we have to linearize the skb if there are more than this.
  842. */
  843. if (skb_shinfo(skb)->nr_frags > 5 && __skb_linearize(skb)) {
  844. netdev->stats.tx_dropped++;
  845. goto out;
  846. }
  847. /* veth can't checksum offload UDP */
  848. if (skb->ip_summed == CHECKSUM_PARTIAL &&
  849. ((skb->protocol == htons(ETH_P_IP) &&
  850. ip_hdr(skb)->protocol != IPPROTO_TCP) ||
  851. (skb->protocol == htons(ETH_P_IPV6) &&
  852. ipv6_hdr(skb)->nexthdr != IPPROTO_TCP)) &&
  853. skb_checksum_help(skb)) {
  854. netdev_err(netdev, "tx: failed to checksum packet\n");
  855. netdev->stats.tx_dropped++;
  856. goto out;
  857. }
  858. desc_flags = IBMVETH_BUF_VALID;
  859. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  860. unsigned char *buf = skb_transport_header(skb) +
  861. skb->csum_offset;
  862. desc_flags |= (IBMVETH_BUF_NO_CSUM | IBMVETH_BUF_CSUM_GOOD);
  863. /* Need to zero out the checksum */
  864. buf[0] = 0;
  865. buf[1] = 0;
  866. if (skb_is_gso(skb) && adapter->fw_large_send_support)
  867. desc_flags |= IBMVETH_BUF_LRG_SND;
  868. }
  869. retry_bounce:
  870. memset(descs, 0, sizeof(descs));
  871. /*
  872. * If a linear packet is below the rx threshold then
  873. * copy it into the static bounce buffer. This avoids the
  874. * cost of a TCE insert and remove.
  875. */
  876. if (force_bounce || (!skb_is_nonlinear(skb) &&
  877. (skb->len < tx_copybreak))) {
  878. skb_copy_from_linear_data(skb, adapter->bounce_buffer,
  879. skb->len);
  880. descs[0].fields.flags_len = desc_flags | skb->len;
  881. descs[0].fields.address = adapter->bounce_buffer_dma;
  882. if (ibmveth_send(adapter, descs, 0)) {
  883. adapter->tx_send_failed++;
  884. netdev->stats.tx_dropped++;
  885. } else {
  886. netdev->stats.tx_packets++;
  887. netdev->stats.tx_bytes += skb->len;
  888. }
  889. goto out;
  890. }
  891. /* Map the header */
  892. dma_addr = dma_map_single(&adapter->vdev->dev, skb->data,
  893. skb_headlen(skb), DMA_TO_DEVICE);
  894. if (dma_mapping_error(&adapter->vdev->dev, dma_addr))
  895. goto map_failed;
  896. descs[0].fields.flags_len = desc_flags | skb_headlen(skb);
  897. descs[0].fields.address = dma_addr;
  898. /* Map the frags */
  899. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  900. const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  901. dma_addr = skb_frag_dma_map(&adapter->vdev->dev, frag, 0,
  902. skb_frag_size(frag), DMA_TO_DEVICE);
  903. if (dma_mapping_error(&adapter->vdev->dev, dma_addr))
  904. goto map_failed_frags;
  905. descs[i+1].fields.flags_len = desc_flags | skb_frag_size(frag);
  906. descs[i+1].fields.address = dma_addr;
  907. }
  908. if (skb->ip_summed == CHECKSUM_PARTIAL && skb_is_gso(skb)) {
  909. if (adapter->fw_large_send_support) {
  910. mss = (unsigned long)skb_shinfo(skb)->gso_size;
  911. adapter->tx_large_packets++;
  912. } else if (!skb_is_gso_v6(skb)) {
  913. /* Put -1 in the IP checksum to tell phyp it
  914. * is a largesend packet. Put the mss in
  915. * the TCP checksum.
  916. */
  917. ip_hdr(skb)->check = 0xffff;
  918. tcp_hdr(skb)->check =
  919. cpu_to_be16(skb_shinfo(skb)->gso_size);
  920. adapter->tx_large_packets++;
  921. }
  922. }
  923. if (ibmveth_send(adapter, descs, mss)) {
  924. adapter->tx_send_failed++;
  925. netdev->stats.tx_dropped++;
  926. } else {
  927. netdev->stats.tx_packets++;
  928. netdev->stats.tx_bytes += skb->len;
  929. }
  930. dma_unmap_single(&adapter->vdev->dev,
  931. descs[0].fields.address,
  932. descs[0].fields.flags_len & IBMVETH_BUF_LEN_MASK,
  933. DMA_TO_DEVICE);
  934. for (i = 1; i < skb_shinfo(skb)->nr_frags + 1; i++)
  935. dma_unmap_page(&adapter->vdev->dev, descs[i].fields.address,
  936. descs[i].fields.flags_len & IBMVETH_BUF_LEN_MASK,
  937. DMA_TO_DEVICE);
  938. out:
  939. dev_consume_skb_any(skb);
  940. return NETDEV_TX_OK;
  941. map_failed_frags:
  942. last = i+1;
  943. for (i = 1; i < last; i++)
  944. dma_unmap_page(&adapter->vdev->dev, descs[i].fields.address,
  945. descs[i].fields.flags_len & IBMVETH_BUF_LEN_MASK,
  946. DMA_TO_DEVICE);
  947. dma_unmap_single(&adapter->vdev->dev,
  948. descs[0].fields.address,
  949. descs[0].fields.flags_len & IBMVETH_BUF_LEN_MASK,
  950. DMA_TO_DEVICE);
  951. map_failed:
  952. if (!firmware_has_feature(FW_FEATURE_CMO))
  953. netdev_err(netdev, "tx: unable to map xmit buffer\n");
  954. adapter->tx_map_failed++;
  955. if (skb_linearize(skb)) {
  956. netdev->stats.tx_dropped++;
  957. goto out;
  958. }
  959. force_bounce = 1;
  960. goto retry_bounce;
  961. }
  962. static void ibmveth_rx_mss_helper(struct sk_buff *skb, u16 mss, int lrg_pkt)
  963. {
  964. struct tcphdr *tcph;
  965. int offset = 0;
  966. int hdr_len;
  967. /* only TCP packets will be aggregated */
  968. if (skb->protocol == htons(ETH_P_IP)) {
  969. struct iphdr *iph = (struct iphdr *)skb->data;
  970. if (iph->protocol == IPPROTO_TCP) {
  971. offset = iph->ihl * 4;
  972. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV4;
  973. } else {
  974. return;
  975. }
  976. } else if (skb->protocol == htons(ETH_P_IPV6)) {
  977. struct ipv6hdr *iph6 = (struct ipv6hdr *)skb->data;
  978. if (iph6->nexthdr == IPPROTO_TCP) {
  979. offset = sizeof(struct ipv6hdr);
  980. skb_shinfo(skb)->gso_type = SKB_GSO_TCPV6;
  981. } else {
  982. return;
  983. }
  984. } else {
  985. return;
  986. }
  987. /* if mss is not set through Large Packet bit/mss in rx buffer,
  988. * expect that the mss will be written to the tcp header checksum.
  989. */
  990. tcph = (struct tcphdr *)(skb->data + offset);
  991. if (lrg_pkt) {
  992. skb_shinfo(skb)->gso_size = mss;
  993. } else if (offset) {
  994. skb_shinfo(skb)->gso_size = ntohs(tcph->check);
  995. tcph->check = 0;
  996. }
  997. if (skb_shinfo(skb)->gso_size) {
  998. hdr_len = offset + tcph->doff * 4;
  999. skb_shinfo(skb)->gso_segs =
  1000. DIV_ROUND_UP(skb->len - hdr_len,
  1001. skb_shinfo(skb)->gso_size);
  1002. }
  1003. }
  1004. static void ibmveth_rx_csum_helper(struct sk_buff *skb,
  1005. struct ibmveth_adapter *adapter)
  1006. {
  1007. struct iphdr *iph = NULL;
  1008. struct ipv6hdr *iph6 = NULL;
  1009. __be16 skb_proto = 0;
  1010. u16 iphlen = 0;
  1011. u16 iph_proto = 0;
  1012. u16 tcphdrlen = 0;
  1013. skb_proto = be16_to_cpu(skb->protocol);
  1014. if (skb_proto == ETH_P_IP) {
  1015. iph = (struct iphdr *)skb->data;
  1016. /* If the IP checksum is not offloaded and if the packet
  1017. * is large send, the checksum must be rebuilt.
  1018. */
  1019. if (iph->check == 0xffff) {
  1020. iph->check = 0;
  1021. iph->check = ip_fast_csum((unsigned char *)iph,
  1022. iph->ihl);
  1023. }
  1024. iphlen = iph->ihl * 4;
  1025. iph_proto = iph->protocol;
  1026. } else if (skb_proto == ETH_P_IPV6) {
  1027. iph6 = (struct ipv6hdr *)skb->data;
  1028. iphlen = sizeof(struct ipv6hdr);
  1029. iph_proto = iph6->nexthdr;
  1030. }
  1031. /* In OVS environment, when a flow is not cached, specifically for a
  1032. * new TCP connection, the first packet information is passed up
  1033. * the user space for finding a flow. During this process, OVS computes
  1034. * checksum on the first packet when CHECKSUM_PARTIAL flag is set.
  1035. *
  1036. * Given that we zeroed out TCP checksum field in transmit path
  1037. * (refer ibmveth_start_xmit routine) as we set "no checksum bit",
  1038. * OVS computed checksum will be incorrect w/o TCP pseudo checksum
  1039. * in the packet. This leads to OVS dropping the packet and hence
  1040. * TCP retransmissions are seen.
  1041. *
  1042. * So, re-compute TCP pseudo header checksum.
  1043. */
  1044. if (iph_proto == IPPROTO_TCP && adapter->is_active_trunk) {
  1045. struct tcphdr *tcph = (struct tcphdr *)(skb->data + iphlen);
  1046. tcphdrlen = skb->len - iphlen;
  1047. /* Recompute TCP pseudo header checksum */
  1048. if (skb_proto == ETH_P_IP)
  1049. tcph->check = ~csum_tcpudp_magic(iph->saddr,
  1050. iph->daddr, tcphdrlen, iph_proto, 0);
  1051. else if (skb_proto == ETH_P_IPV6)
  1052. tcph->check = ~csum_ipv6_magic(&iph6->saddr,
  1053. &iph6->daddr, tcphdrlen, iph_proto, 0);
  1054. /* Setup SKB fields for checksum offload */
  1055. skb_partial_csum_set(skb, iphlen,
  1056. offsetof(struct tcphdr, check));
  1057. skb_reset_network_header(skb);
  1058. }
  1059. }
  1060. static int ibmveth_poll(struct napi_struct *napi, int budget)
  1061. {
  1062. struct ibmveth_adapter *adapter =
  1063. container_of(napi, struct ibmveth_adapter, napi);
  1064. struct net_device *netdev = adapter->netdev;
  1065. int frames_processed = 0;
  1066. unsigned long lpar_rc;
  1067. u16 mss = 0;
  1068. while (frames_processed < budget) {
  1069. if (!ibmveth_rxq_pending_buffer(adapter))
  1070. break;
  1071. smp_rmb();
  1072. if (!ibmveth_rxq_buffer_valid(adapter)) {
  1073. wmb(); /* suggested by larson1 */
  1074. adapter->rx_invalid_buffer++;
  1075. netdev_dbg(netdev, "recycling invalid buffer\n");
  1076. ibmveth_rxq_recycle_buffer(adapter);
  1077. } else {
  1078. struct sk_buff *skb, *new_skb;
  1079. int length = ibmveth_rxq_frame_length(adapter);
  1080. int offset = ibmveth_rxq_frame_offset(adapter);
  1081. int csum_good = ibmveth_rxq_csum_good(adapter);
  1082. int lrg_pkt = ibmveth_rxq_large_packet(adapter);
  1083. __sum16 iph_check = 0;
  1084. skb = ibmveth_rxq_get_buffer(adapter);
  1085. /* if the large packet bit is set in the rx queue
  1086. * descriptor, the mss will be written by PHYP eight
  1087. * bytes from the start of the rx buffer, which is
  1088. * skb->data at this stage
  1089. */
  1090. if (lrg_pkt) {
  1091. __be64 *rxmss = (__be64 *)(skb->data + 8);
  1092. mss = (u16)be64_to_cpu(*rxmss);
  1093. }
  1094. new_skb = NULL;
  1095. if (length < rx_copybreak)
  1096. new_skb = netdev_alloc_skb(netdev, length);
  1097. if (new_skb) {
  1098. skb_copy_to_linear_data(new_skb,
  1099. skb->data + offset,
  1100. length);
  1101. if (rx_flush)
  1102. ibmveth_flush_buffer(skb->data,
  1103. length + offset);
  1104. if (!ibmveth_rxq_recycle_buffer(adapter))
  1105. kfree_skb(skb);
  1106. skb = new_skb;
  1107. } else {
  1108. ibmveth_rxq_harvest_buffer(adapter);
  1109. skb_reserve(skb, offset);
  1110. }
  1111. skb_put(skb, length);
  1112. skb->protocol = eth_type_trans(skb, netdev);
  1113. /* PHYP without PLSO support places a -1 in the ip
  1114. * checksum for large send frames.
  1115. */
  1116. if (skb->protocol == cpu_to_be16(ETH_P_IP)) {
  1117. struct iphdr *iph = (struct iphdr *)skb->data;
  1118. iph_check = iph->check;
  1119. }
  1120. if ((length > netdev->mtu + ETH_HLEN) ||
  1121. lrg_pkt || iph_check == 0xffff) {
  1122. ibmveth_rx_mss_helper(skb, mss, lrg_pkt);
  1123. adapter->rx_large_packets++;
  1124. }
  1125. if (csum_good) {
  1126. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1127. ibmveth_rx_csum_helper(skb, adapter);
  1128. }
  1129. napi_gro_receive(napi, skb); /* send it up */
  1130. netdev->stats.rx_packets++;
  1131. netdev->stats.rx_bytes += length;
  1132. frames_processed++;
  1133. }
  1134. }
  1135. ibmveth_replenish_task(adapter);
  1136. if (frames_processed < budget) {
  1137. napi_complete_done(napi, frames_processed);
  1138. /* We think we are done - reenable interrupts,
  1139. * then check once more to make sure we are done.
  1140. */
  1141. lpar_rc = h_vio_signal(adapter->vdev->unit_address,
  1142. VIO_IRQ_ENABLE);
  1143. BUG_ON(lpar_rc != H_SUCCESS);
  1144. if (ibmveth_rxq_pending_buffer(adapter) &&
  1145. napi_reschedule(napi)) {
  1146. lpar_rc = h_vio_signal(adapter->vdev->unit_address,
  1147. VIO_IRQ_DISABLE);
  1148. }
  1149. }
  1150. return frames_processed;
  1151. }
  1152. static irqreturn_t ibmveth_interrupt(int irq, void *dev_instance)
  1153. {
  1154. struct net_device *netdev = dev_instance;
  1155. struct ibmveth_adapter *adapter = netdev_priv(netdev);
  1156. unsigned long lpar_rc;
  1157. if (napi_schedule_prep(&adapter->napi)) {
  1158. lpar_rc = h_vio_signal(adapter->vdev->unit_address,
  1159. VIO_IRQ_DISABLE);
  1160. BUG_ON(lpar_rc != H_SUCCESS);
  1161. __napi_schedule(&adapter->napi);
  1162. }
  1163. return IRQ_HANDLED;
  1164. }
  1165. static void ibmveth_set_multicast_list(struct net_device *netdev)
  1166. {
  1167. struct ibmveth_adapter *adapter = netdev_priv(netdev);
  1168. unsigned long lpar_rc;
  1169. if ((netdev->flags & IFF_PROMISC) ||
  1170. (netdev_mc_count(netdev) > adapter->mcastFilterSize)) {
  1171. lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
  1172. IbmVethMcastEnableRecv |
  1173. IbmVethMcastDisableFiltering,
  1174. 0);
  1175. if (lpar_rc != H_SUCCESS) {
  1176. netdev_err(netdev, "h_multicast_ctrl rc=%ld when "
  1177. "entering promisc mode\n", lpar_rc);
  1178. }
  1179. } else {
  1180. struct netdev_hw_addr *ha;
  1181. /* clear the filter table & disable filtering */
  1182. lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
  1183. IbmVethMcastEnableRecv |
  1184. IbmVethMcastDisableFiltering |
  1185. IbmVethMcastClearFilterTable,
  1186. 0);
  1187. if (lpar_rc != H_SUCCESS) {
  1188. netdev_err(netdev, "h_multicast_ctrl rc=%ld when "
  1189. "attempting to clear filter table\n",
  1190. lpar_rc);
  1191. }
  1192. /* add the addresses to the filter table */
  1193. netdev_for_each_mc_addr(ha, netdev) {
  1194. /* add the multicast address to the filter table */
  1195. u64 mcast_addr;
  1196. mcast_addr = ibmveth_encode_mac_addr(ha->addr);
  1197. lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
  1198. IbmVethMcastAddFilter,
  1199. mcast_addr);
  1200. if (lpar_rc != H_SUCCESS) {
  1201. netdev_err(netdev, "h_multicast_ctrl rc=%ld "
  1202. "when adding an entry to the filter "
  1203. "table\n", lpar_rc);
  1204. }
  1205. }
  1206. /* re-enable filtering */
  1207. lpar_rc = h_multicast_ctrl(adapter->vdev->unit_address,
  1208. IbmVethMcastEnableFiltering,
  1209. 0);
  1210. if (lpar_rc != H_SUCCESS) {
  1211. netdev_err(netdev, "h_multicast_ctrl rc=%ld when "
  1212. "enabling filtering\n", lpar_rc);
  1213. }
  1214. }
  1215. }
  1216. static int ibmveth_change_mtu(struct net_device *dev, int new_mtu)
  1217. {
  1218. struct ibmveth_adapter *adapter = netdev_priv(dev);
  1219. struct vio_dev *viodev = adapter->vdev;
  1220. int new_mtu_oh = new_mtu + IBMVETH_BUFF_OH;
  1221. int i, rc;
  1222. int need_restart = 0;
  1223. for (i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++)
  1224. if (new_mtu_oh <= adapter->rx_buff_pool[i].buff_size)
  1225. break;
  1226. if (i == IBMVETH_NUM_BUFF_POOLS)
  1227. return -EINVAL;
  1228. /* Deactivate all the buffer pools so that the next loop can activate
  1229. only the buffer pools necessary to hold the new MTU */
  1230. if (netif_running(adapter->netdev)) {
  1231. need_restart = 1;
  1232. adapter->pool_config = 1;
  1233. ibmveth_close(adapter->netdev);
  1234. adapter->pool_config = 0;
  1235. }
  1236. /* Look for an active buffer pool that can hold the new MTU */
  1237. for (i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++) {
  1238. adapter->rx_buff_pool[i].active = 1;
  1239. if (new_mtu_oh <= adapter->rx_buff_pool[i].buff_size) {
  1240. dev->mtu = new_mtu;
  1241. vio_cmo_set_dev_desired(viodev,
  1242. ibmveth_get_desired_dma
  1243. (viodev));
  1244. if (need_restart) {
  1245. return ibmveth_open(adapter->netdev);
  1246. }
  1247. return 0;
  1248. }
  1249. }
  1250. if (need_restart && (rc = ibmveth_open(adapter->netdev)))
  1251. return rc;
  1252. return -EINVAL;
  1253. }
  1254. #ifdef CONFIG_NET_POLL_CONTROLLER
  1255. static void ibmveth_poll_controller(struct net_device *dev)
  1256. {
  1257. ibmveth_replenish_task(netdev_priv(dev));
  1258. ibmveth_interrupt(dev->irq, dev);
  1259. }
  1260. #endif
  1261. /**
  1262. * ibmveth_get_desired_dma - Calculate IO memory desired by the driver
  1263. *
  1264. * @vdev: struct vio_dev for the device whose desired IO mem is to be returned
  1265. *
  1266. * Return value:
  1267. * Number of bytes of IO data the driver will need to perform well.
  1268. */
  1269. static unsigned long ibmveth_get_desired_dma(struct vio_dev *vdev)
  1270. {
  1271. struct net_device *netdev = dev_get_drvdata(&vdev->dev);
  1272. struct ibmveth_adapter *adapter;
  1273. struct iommu_table *tbl;
  1274. unsigned long ret;
  1275. int i;
  1276. int rxqentries = 1;
  1277. tbl = get_iommu_table_base(&vdev->dev);
  1278. /* netdev inits at probe time along with the structures we need below*/
  1279. if (netdev == NULL)
  1280. return IOMMU_PAGE_ALIGN(IBMVETH_IO_ENTITLEMENT_DEFAULT, tbl);
  1281. adapter = netdev_priv(netdev);
  1282. ret = IBMVETH_BUFF_LIST_SIZE + IBMVETH_FILT_LIST_SIZE;
  1283. ret += IOMMU_PAGE_ALIGN(netdev->mtu, tbl);
  1284. for (i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++) {
  1285. /* add the size of the active receive buffers */
  1286. if (adapter->rx_buff_pool[i].active)
  1287. ret +=
  1288. adapter->rx_buff_pool[i].size *
  1289. IOMMU_PAGE_ALIGN(adapter->rx_buff_pool[i].
  1290. buff_size, tbl);
  1291. rxqentries += adapter->rx_buff_pool[i].size;
  1292. }
  1293. /* add the size of the receive queue entries */
  1294. ret += IOMMU_PAGE_ALIGN(
  1295. rxqentries * sizeof(struct ibmveth_rx_q_entry), tbl);
  1296. return ret;
  1297. }
  1298. static int ibmveth_set_mac_addr(struct net_device *dev, void *p)
  1299. {
  1300. struct ibmveth_adapter *adapter = netdev_priv(dev);
  1301. struct sockaddr *addr = p;
  1302. u64 mac_address;
  1303. int rc;
  1304. if (!is_valid_ether_addr(addr->sa_data))
  1305. return -EADDRNOTAVAIL;
  1306. mac_address = ibmveth_encode_mac_addr(addr->sa_data);
  1307. rc = h_change_logical_lan_mac(adapter->vdev->unit_address, mac_address);
  1308. if (rc) {
  1309. netdev_err(adapter->netdev, "h_change_logical_lan_mac failed with rc=%d\n", rc);
  1310. return rc;
  1311. }
  1312. ether_addr_copy(dev->dev_addr, addr->sa_data);
  1313. return 0;
  1314. }
  1315. static const struct net_device_ops ibmveth_netdev_ops = {
  1316. .ndo_open = ibmveth_open,
  1317. .ndo_stop = ibmveth_close,
  1318. .ndo_start_xmit = ibmveth_start_xmit,
  1319. .ndo_set_rx_mode = ibmveth_set_multicast_list,
  1320. .ndo_do_ioctl = ibmveth_ioctl,
  1321. .ndo_change_mtu = ibmveth_change_mtu,
  1322. .ndo_fix_features = ibmveth_fix_features,
  1323. .ndo_set_features = ibmveth_set_features,
  1324. .ndo_validate_addr = eth_validate_addr,
  1325. .ndo_set_mac_address = ibmveth_set_mac_addr,
  1326. #ifdef CONFIG_NET_POLL_CONTROLLER
  1327. .ndo_poll_controller = ibmveth_poll_controller,
  1328. #endif
  1329. };
  1330. static int ibmveth_probe(struct vio_dev *dev, const struct vio_device_id *id)
  1331. {
  1332. int rc, i, mac_len;
  1333. struct net_device *netdev;
  1334. struct ibmveth_adapter *adapter;
  1335. unsigned char *mac_addr_p;
  1336. __be32 *mcastFilterSize_p;
  1337. long ret;
  1338. unsigned long ret_attr;
  1339. dev_dbg(&dev->dev, "entering ibmveth_probe for UA 0x%x\n",
  1340. dev->unit_address);
  1341. mac_addr_p = (unsigned char *)vio_get_attribute(dev, VETH_MAC_ADDR,
  1342. &mac_len);
  1343. if (!mac_addr_p) {
  1344. dev_err(&dev->dev, "Can't find VETH_MAC_ADDR attribute\n");
  1345. return -EINVAL;
  1346. }
  1347. /* Workaround for old/broken pHyp */
  1348. if (mac_len == 8)
  1349. mac_addr_p += 2;
  1350. else if (mac_len != 6) {
  1351. dev_err(&dev->dev, "VETH_MAC_ADDR attribute wrong len %d\n",
  1352. mac_len);
  1353. return -EINVAL;
  1354. }
  1355. mcastFilterSize_p = (__be32 *)vio_get_attribute(dev,
  1356. VETH_MCAST_FILTER_SIZE,
  1357. NULL);
  1358. if (!mcastFilterSize_p) {
  1359. dev_err(&dev->dev, "Can't find VETH_MCAST_FILTER_SIZE "
  1360. "attribute\n");
  1361. return -EINVAL;
  1362. }
  1363. netdev = alloc_etherdev(sizeof(struct ibmveth_adapter));
  1364. if (!netdev)
  1365. return -ENOMEM;
  1366. adapter = netdev_priv(netdev);
  1367. dev_set_drvdata(&dev->dev, netdev);
  1368. adapter->vdev = dev;
  1369. adapter->netdev = netdev;
  1370. adapter->mcastFilterSize = be32_to_cpu(*mcastFilterSize_p);
  1371. adapter->pool_config = 0;
  1372. netif_napi_add(netdev, &adapter->napi, ibmveth_poll, 16);
  1373. netdev->irq = dev->irq;
  1374. netdev->netdev_ops = &ibmveth_netdev_ops;
  1375. netdev->ethtool_ops = &netdev_ethtool_ops;
  1376. SET_NETDEV_DEV(netdev, &dev->dev);
  1377. netdev->hw_features = NETIF_F_SG;
  1378. if (vio_get_attribute(dev, "ibm,illan-options", NULL) != NULL) {
  1379. netdev->hw_features |= NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM |
  1380. NETIF_F_RXCSUM;
  1381. }
  1382. netdev->features |= netdev->hw_features;
  1383. ret = h_illan_attributes(adapter->vdev->unit_address, 0, 0, &ret_attr);
  1384. /* If running older firmware, TSO should not be enabled by default */
  1385. if (ret == H_SUCCESS && (ret_attr & IBMVETH_ILLAN_LRG_SND_SUPPORT) &&
  1386. !old_large_send) {
  1387. netdev->hw_features |= NETIF_F_TSO | NETIF_F_TSO6;
  1388. netdev->features |= netdev->hw_features;
  1389. } else {
  1390. netdev->hw_features |= NETIF_F_TSO;
  1391. }
  1392. adapter->is_active_trunk = false;
  1393. if (ret == H_SUCCESS && (ret_attr & IBMVETH_ILLAN_ACTIVE_TRUNK)) {
  1394. adapter->is_active_trunk = true;
  1395. netdev->hw_features |= NETIF_F_FRAGLIST;
  1396. netdev->features |= NETIF_F_FRAGLIST;
  1397. }
  1398. netdev->min_mtu = IBMVETH_MIN_MTU;
  1399. netdev->max_mtu = ETH_MAX_MTU - IBMVETH_BUFF_OH;
  1400. memcpy(netdev->dev_addr, mac_addr_p, ETH_ALEN);
  1401. if (firmware_has_feature(FW_FEATURE_CMO))
  1402. memcpy(pool_count, pool_count_cmo, sizeof(pool_count));
  1403. for (i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++) {
  1404. struct kobject *kobj = &adapter->rx_buff_pool[i].kobj;
  1405. int error;
  1406. ibmveth_init_buffer_pool(&adapter->rx_buff_pool[i], i,
  1407. pool_count[i], pool_size[i],
  1408. pool_active[i]);
  1409. error = kobject_init_and_add(kobj, &ktype_veth_pool,
  1410. &dev->dev.kobj, "pool%d", i);
  1411. if (!error)
  1412. kobject_uevent(kobj, KOBJ_ADD);
  1413. }
  1414. netdev_dbg(netdev, "adapter @ 0x%p\n", adapter);
  1415. netdev_dbg(netdev, "registering netdev...\n");
  1416. ibmveth_set_features(netdev, netdev->features);
  1417. rc = register_netdev(netdev);
  1418. if (rc) {
  1419. netdev_dbg(netdev, "failed to register netdev rc=%d\n", rc);
  1420. free_netdev(netdev);
  1421. return rc;
  1422. }
  1423. netdev_dbg(netdev, "registered\n");
  1424. return 0;
  1425. }
  1426. static int ibmveth_remove(struct vio_dev *dev)
  1427. {
  1428. struct net_device *netdev = dev_get_drvdata(&dev->dev);
  1429. struct ibmveth_adapter *adapter = netdev_priv(netdev);
  1430. int i;
  1431. for (i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++)
  1432. kobject_put(&adapter->rx_buff_pool[i].kobj);
  1433. unregister_netdev(netdev);
  1434. free_netdev(netdev);
  1435. dev_set_drvdata(&dev->dev, NULL);
  1436. return 0;
  1437. }
  1438. static struct attribute veth_active_attr;
  1439. static struct attribute veth_num_attr;
  1440. static struct attribute veth_size_attr;
  1441. static ssize_t veth_pool_show(struct kobject *kobj,
  1442. struct attribute *attr, char *buf)
  1443. {
  1444. struct ibmveth_buff_pool *pool = container_of(kobj,
  1445. struct ibmveth_buff_pool,
  1446. kobj);
  1447. if (attr == &veth_active_attr)
  1448. return sprintf(buf, "%d\n", pool->active);
  1449. else if (attr == &veth_num_attr)
  1450. return sprintf(buf, "%d\n", pool->size);
  1451. else if (attr == &veth_size_attr)
  1452. return sprintf(buf, "%d\n", pool->buff_size);
  1453. return 0;
  1454. }
  1455. static ssize_t veth_pool_store(struct kobject *kobj, struct attribute *attr,
  1456. const char *buf, size_t count)
  1457. {
  1458. struct ibmveth_buff_pool *pool = container_of(kobj,
  1459. struct ibmveth_buff_pool,
  1460. kobj);
  1461. struct net_device *netdev = dev_get_drvdata(
  1462. container_of(kobj->parent, struct device, kobj));
  1463. struct ibmveth_adapter *adapter = netdev_priv(netdev);
  1464. long value = simple_strtol(buf, NULL, 10);
  1465. long rc;
  1466. if (attr == &veth_active_attr) {
  1467. if (value && !pool->active) {
  1468. if (netif_running(netdev)) {
  1469. if (ibmveth_alloc_buffer_pool(pool)) {
  1470. netdev_err(netdev,
  1471. "unable to alloc pool\n");
  1472. return -ENOMEM;
  1473. }
  1474. pool->active = 1;
  1475. adapter->pool_config = 1;
  1476. ibmveth_close(netdev);
  1477. adapter->pool_config = 0;
  1478. if ((rc = ibmveth_open(netdev)))
  1479. return rc;
  1480. } else {
  1481. pool->active = 1;
  1482. }
  1483. } else if (!value && pool->active) {
  1484. int mtu = netdev->mtu + IBMVETH_BUFF_OH;
  1485. int i;
  1486. /* Make sure there is a buffer pool with buffers that
  1487. can hold a packet of the size of the MTU */
  1488. for (i = 0; i < IBMVETH_NUM_BUFF_POOLS; i++) {
  1489. if (pool == &adapter->rx_buff_pool[i])
  1490. continue;
  1491. if (!adapter->rx_buff_pool[i].active)
  1492. continue;
  1493. if (mtu <= adapter->rx_buff_pool[i].buff_size)
  1494. break;
  1495. }
  1496. if (i == IBMVETH_NUM_BUFF_POOLS) {
  1497. netdev_err(netdev, "no active pool >= MTU\n");
  1498. return -EPERM;
  1499. }
  1500. if (netif_running(netdev)) {
  1501. adapter->pool_config = 1;
  1502. ibmveth_close(netdev);
  1503. pool->active = 0;
  1504. adapter->pool_config = 0;
  1505. if ((rc = ibmveth_open(netdev)))
  1506. return rc;
  1507. }
  1508. pool->active = 0;
  1509. }
  1510. } else if (attr == &veth_num_attr) {
  1511. if (value <= 0 || value > IBMVETH_MAX_POOL_COUNT) {
  1512. return -EINVAL;
  1513. } else {
  1514. if (netif_running(netdev)) {
  1515. adapter->pool_config = 1;
  1516. ibmveth_close(netdev);
  1517. adapter->pool_config = 0;
  1518. pool->size = value;
  1519. if ((rc = ibmveth_open(netdev)))
  1520. return rc;
  1521. } else {
  1522. pool->size = value;
  1523. }
  1524. }
  1525. } else if (attr == &veth_size_attr) {
  1526. if (value <= IBMVETH_BUFF_OH || value > IBMVETH_MAX_BUF_SIZE) {
  1527. return -EINVAL;
  1528. } else {
  1529. if (netif_running(netdev)) {
  1530. adapter->pool_config = 1;
  1531. ibmveth_close(netdev);
  1532. adapter->pool_config = 0;
  1533. pool->buff_size = value;
  1534. if ((rc = ibmveth_open(netdev)))
  1535. return rc;
  1536. } else {
  1537. pool->buff_size = value;
  1538. }
  1539. }
  1540. }
  1541. /* kick the interrupt handler to allocate/deallocate pools */
  1542. ibmveth_interrupt(netdev->irq, netdev);
  1543. return count;
  1544. }
  1545. #define ATTR(_name, _mode) \
  1546. struct attribute veth_##_name##_attr = { \
  1547. .name = __stringify(_name), .mode = _mode, \
  1548. };
  1549. static ATTR(active, 0644);
  1550. static ATTR(num, 0644);
  1551. static ATTR(size, 0644);
  1552. static struct attribute *veth_pool_attrs[] = {
  1553. &veth_active_attr,
  1554. &veth_num_attr,
  1555. &veth_size_attr,
  1556. NULL,
  1557. };
  1558. static const struct sysfs_ops veth_pool_ops = {
  1559. .show = veth_pool_show,
  1560. .store = veth_pool_store,
  1561. };
  1562. static struct kobj_type ktype_veth_pool = {
  1563. .release = NULL,
  1564. .sysfs_ops = &veth_pool_ops,
  1565. .default_attrs = veth_pool_attrs,
  1566. };
  1567. static int ibmveth_resume(struct device *dev)
  1568. {
  1569. struct net_device *netdev = dev_get_drvdata(dev);
  1570. ibmveth_interrupt(netdev->irq, netdev);
  1571. return 0;
  1572. }
  1573. static const struct vio_device_id ibmveth_device_table[] = {
  1574. { "network", "IBM,l-lan"},
  1575. { "", "" }
  1576. };
  1577. MODULE_DEVICE_TABLE(vio, ibmveth_device_table);
  1578. static const struct dev_pm_ops ibmveth_pm_ops = {
  1579. .resume = ibmveth_resume
  1580. };
  1581. static struct vio_driver ibmveth_driver = {
  1582. .id_table = ibmveth_device_table,
  1583. .probe = ibmveth_probe,
  1584. .remove = ibmveth_remove,
  1585. .get_desired_dma = ibmveth_get_desired_dma,
  1586. .name = ibmveth_driver_name,
  1587. .pm = &ibmveth_pm_ops,
  1588. };
  1589. static int __init ibmveth_module_init(void)
  1590. {
  1591. printk(KERN_DEBUG "%s: %s %s\n", ibmveth_driver_name,
  1592. ibmveth_driver_string, ibmveth_driver_version);
  1593. return vio_register_driver(&ibmveth_driver);
  1594. }
  1595. static void __exit ibmveth_module_exit(void)
  1596. {
  1597. vio_unregister_driver(&ibmveth_driver);
  1598. }
  1599. module_init(ibmveth_module_init);
  1600. module_exit(ibmveth_module_exit);