ibmvnic.c 101 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643
  1. /**************************************************************************/
  2. /* */
  3. /* IBM System i and System p Virtual NIC Device Driver */
  4. /* Copyright (C) 2014 IBM Corp. */
  5. /* Santiago Leon (santi_leon@yahoo.com) */
  6. /* Thomas Falcon (tlfalcon@linux.vnet.ibm.com) */
  7. /* John Allen (jallen@linux.vnet.ibm.com) */
  8. /* */
  9. /* This program is free software; you can redistribute it and/or modify */
  10. /* it under the terms of the GNU General Public License as published by */
  11. /* the Free Software Foundation; either version 2 of the License, or */
  12. /* (at your option) any later version. */
  13. /* */
  14. /* This program is distributed in the hope that it will be useful, */
  15. /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
  16. /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
  17. /* GNU 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. */
  21. /* */
  22. /* This module contains the implementation of a virtual ethernet device */
  23. /* for use with IBM i/p Series LPAR Linux. It utilizes the logical LAN */
  24. /* option of the RS/6000 Platform Architecture to interface with virtual */
  25. /* ethernet NICs that are presented to the partition by the hypervisor. */
  26. /* */
  27. /* Messages are passed between the VNIC driver and the VNIC server using */
  28. /* Command/Response Queues (CRQs) and sub CRQs (sCRQs). CRQs are used to */
  29. /* issue and receive commands that initiate communication with the server */
  30. /* on driver initialization. Sub CRQs (sCRQs) are similar to CRQs, but */
  31. /* are used by the driver to notify the server that a packet is */
  32. /* ready for transmission or that a buffer has been added to receive a */
  33. /* packet. Subsequently, sCRQs are used by the server to notify the */
  34. /* driver that a packet transmission has been completed or that a packet */
  35. /* has been received and placed in a waiting buffer. */
  36. /* */
  37. /* In lieu of a more conventional "on-the-fly" DMA mapping strategy in */
  38. /* which skbs are DMA mapped and immediately unmapped when the transmit */
  39. /* or receive has been completed, the VNIC driver is required to use */
  40. /* "long term mapping". This entails that large, continuous DMA mapped */
  41. /* buffers are allocated on driver initialization and these buffers are */
  42. /* then continuously reused to pass skbs to and from the VNIC server. */
  43. /* */
  44. /**************************************************************************/
  45. #include <linux/module.h>
  46. #include <linux/moduleparam.h>
  47. #include <linux/types.h>
  48. #include <linux/errno.h>
  49. #include <linux/completion.h>
  50. #include <linux/ioport.h>
  51. #include <linux/dma-mapping.h>
  52. #include <linux/kernel.h>
  53. #include <linux/netdevice.h>
  54. #include <linux/etherdevice.h>
  55. #include <linux/skbuff.h>
  56. #include <linux/init.h>
  57. #include <linux/delay.h>
  58. #include <linux/mm.h>
  59. #include <linux/ethtool.h>
  60. #include <linux/proc_fs.h>
  61. #include <linux/in.h>
  62. #include <linux/ip.h>
  63. #include <linux/ipv6.h>
  64. #include <linux/irq.h>
  65. #include <linux/kthread.h>
  66. #include <linux/seq_file.h>
  67. #include <linux/interrupt.h>
  68. #include <net/net_namespace.h>
  69. #include <asm/hvcall.h>
  70. #include <linux/atomic.h>
  71. #include <asm/vio.h>
  72. #include <asm/iommu.h>
  73. #include <linux/uaccess.h>
  74. #include <asm/firmware.h>
  75. #include <linux/workqueue.h>
  76. #include <linux/if_vlan.h>
  77. #include "ibmvnic.h"
  78. static const char ibmvnic_driver_name[] = "ibmvnic";
  79. static const char ibmvnic_driver_string[] = "IBM System i/p Virtual NIC Driver";
  80. MODULE_AUTHOR("Santiago Leon <santi_leon@yahoo.com>");
  81. MODULE_DESCRIPTION("IBM System i/p Virtual NIC Driver");
  82. MODULE_LICENSE("GPL");
  83. MODULE_VERSION(IBMVNIC_DRIVER_VERSION);
  84. static int ibmvnic_version = IBMVNIC_INITIAL_VERSION;
  85. static int ibmvnic_remove(struct vio_dev *);
  86. static void release_sub_crqs(struct ibmvnic_adapter *);
  87. static int ibmvnic_reset_crq(struct ibmvnic_adapter *);
  88. static int ibmvnic_send_crq_init(struct ibmvnic_adapter *);
  89. static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *);
  90. static int ibmvnic_send_crq(struct ibmvnic_adapter *, union ibmvnic_crq *);
  91. static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
  92. union sub_crq *sub_crq);
  93. static int send_subcrq_indirect(struct ibmvnic_adapter *, u64, u64, u64);
  94. static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance);
  95. static int enable_scrq_irq(struct ibmvnic_adapter *,
  96. struct ibmvnic_sub_crq_queue *);
  97. static int disable_scrq_irq(struct ibmvnic_adapter *,
  98. struct ibmvnic_sub_crq_queue *);
  99. static int pending_scrq(struct ibmvnic_adapter *,
  100. struct ibmvnic_sub_crq_queue *);
  101. static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *,
  102. struct ibmvnic_sub_crq_queue *);
  103. static int ibmvnic_poll(struct napi_struct *napi, int data);
  104. static void send_map_query(struct ibmvnic_adapter *adapter);
  105. static void send_request_map(struct ibmvnic_adapter *, dma_addr_t, __be32, u8);
  106. static void send_request_unmap(struct ibmvnic_adapter *, u8);
  107. static void send_login(struct ibmvnic_adapter *adapter);
  108. static void send_cap_queries(struct ibmvnic_adapter *adapter);
  109. static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter);
  110. static int ibmvnic_init(struct ibmvnic_adapter *);
  111. static void release_crq_queue(struct ibmvnic_adapter *);
  112. struct ibmvnic_stat {
  113. char name[ETH_GSTRING_LEN];
  114. int offset;
  115. };
  116. #define IBMVNIC_STAT_OFF(stat) (offsetof(struct ibmvnic_adapter, stats) + \
  117. offsetof(struct ibmvnic_statistics, stat))
  118. #define IBMVNIC_GET_STAT(a, off) (*((u64 *)(((unsigned long)(a)) + off)))
  119. static const struct ibmvnic_stat ibmvnic_stats[] = {
  120. {"rx_packets", IBMVNIC_STAT_OFF(rx_packets)},
  121. {"rx_bytes", IBMVNIC_STAT_OFF(rx_bytes)},
  122. {"tx_packets", IBMVNIC_STAT_OFF(tx_packets)},
  123. {"tx_bytes", IBMVNIC_STAT_OFF(tx_bytes)},
  124. {"ucast_tx_packets", IBMVNIC_STAT_OFF(ucast_tx_packets)},
  125. {"ucast_rx_packets", IBMVNIC_STAT_OFF(ucast_rx_packets)},
  126. {"mcast_tx_packets", IBMVNIC_STAT_OFF(mcast_tx_packets)},
  127. {"mcast_rx_packets", IBMVNIC_STAT_OFF(mcast_rx_packets)},
  128. {"bcast_tx_packets", IBMVNIC_STAT_OFF(bcast_tx_packets)},
  129. {"bcast_rx_packets", IBMVNIC_STAT_OFF(bcast_rx_packets)},
  130. {"align_errors", IBMVNIC_STAT_OFF(align_errors)},
  131. {"fcs_errors", IBMVNIC_STAT_OFF(fcs_errors)},
  132. {"single_collision_frames", IBMVNIC_STAT_OFF(single_collision_frames)},
  133. {"multi_collision_frames", IBMVNIC_STAT_OFF(multi_collision_frames)},
  134. {"sqe_test_errors", IBMVNIC_STAT_OFF(sqe_test_errors)},
  135. {"deferred_tx", IBMVNIC_STAT_OFF(deferred_tx)},
  136. {"late_collisions", IBMVNIC_STAT_OFF(late_collisions)},
  137. {"excess_collisions", IBMVNIC_STAT_OFF(excess_collisions)},
  138. {"internal_mac_tx_errors", IBMVNIC_STAT_OFF(internal_mac_tx_errors)},
  139. {"carrier_sense", IBMVNIC_STAT_OFF(carrier_sense)},
  140. {"too_long_frames", IBMVNIC_STAT_OFF(too_long_frames)},
  141. {"internal_mac_rx_errors", IBMVNIC_STAT_OFF(internal_mac_rx_errors)},
  142. };
  143. static long h_reg_sub_crq(unsigned long unit_address, unsigned long token,
  144. unsigned long length, unsigned long *number,
  145. unsigned long *irq)
  146. {
  147. unsigned long retbuf[PLPAR_HCALL_BUFSIZE];
  148. long rc;
  149. rc = plpar_hcall(H_REG_SUB_CRQ, retbuf, unit_address, token, length);
  150. *number = retbuf[0];
  151. *irq = retbuf[1];
  152. return rc;
  153. }
  154. static int alloc_long_term_buff(struct ibmvnic_adapter *adapter,
  155. struct ibmvnic_long_term_buff *ltb, int size)
  156. {
  157. struct device *dev = &adapter->vdev->dev;
  158. ltb->size = size;
  159. ltb->buff = dma_alloc_coherent(dev, ltb->size, &ltb->addr,
  160. GFP_KERNEL);
  161. if (!ltb->buff) {
  162. dev_err(dev, "Couldn't alloc long term buffer\n");
  163. return -ENOMEM;
  164. }
  165. ltb->map_id = adapter->map_id;
  166. adapter->map_id++;
  167. init_completion(&adapter->fw_done);
  168. send_request_map(adapter, ltb->addr,
  169. ltb->size, ltb->map_id);
  170. wait_for_completion(&adapter->fw_done);
  171. return 0;
  172. }
  173. static void free_long_term_buff(struct ibmvnic_adapter *adapter,
  174. struct ibmvnic_long_term_buff *ltb)
  175. {
  176. struct device *dev = &adapter->vdev->dev;
  177. if (!ltb->buff)
  178. return;
  179. if (adapter->reset_reason != VNIC_RESET_FAILOVER &&
  180. adapter->reset_reason != VNIC_RESET_MOBILITY)
  181. send_request_unmap(adapter, ltb->map_id);
  182. dma_free_coherent(dev, ltb->size, ltb->buff, ltb->addr);
  183. }
  184. static void replenish_rx_pool(struct ibmvnic_adapter *adapter,
  185. struct ibmvnic_rx_pool *pool)
  186. {
  187. int count = pool->size - atomic_read(&pool->available);
  188. struct device *dev = &adapter->vdev->dev;
  189. int buffers_added = 0;
  190. unsigned long lpar_rc;
  191. union sub_crq sub_crq;
  192. struct sk_buff *skb;
  193. unsigned int offset;
  194. dma_addr_t dma_addr;
  195. unsigned char *dst;
  196. u64 *handle_array;
  197. int shift = 0;
  198. int index;
  199. int i;
  200. handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
  201. be32_to_cpu(adapter->login_rsp_buf->
  202. off_rxadd_subcrqs));
  203. for (i = 0; i < count; ++i) {
  204. skb = alloc_skb(pool->buff_size, GFP_ATOMIC);
  205. if (!skb) {
  206. dev_err(dev, "Couldn't replenish rx buff\n");
  207. adapter->replenish_no_mem++;
  208. break;
  209. }
  210. index = pool->free_map[pool->next_free];
  211. if (pool->rx_buff[index].skb)
  212. dev_err(dev, "Inconsistent free_map!\n");
  213. /* Copy the skb to the long term mapped DMA buffer */
  214. offset = index * pool->buff_size;
  215. dst = pool->long_term_buff.buff + offset;
  216. memset(dst, 0, pool->buff_size);
  217. dma_addr = pool->long_term_buff.addr + offset;
  218. pool->rx_buff[index].data = dst;
  219. pool->free_map[pool->next_free] = IBMVNIC_INVALID_MAP;
  220. pool->rx_buff[index].dma = dma_addr;
  221. pool->rx_buff[index].skb = skb;
  222. pool->rx_buff[index].pool_index = pool->index;
  223. pool->rx_buff[index].size = pool->buff_size;
  224. memset(&sub_crq, 0, sizeof(sub_crq));
  225. sub_crq.rx_add.first = IBMVNIC_CRQ_CMD;
  226. sub_crq.rx_add.correlator =
  227. cpu_to_be64((u64)&pool->rx_buff[index]);
  228. sub_crq.rx_add.ioba = cpu_to_be32(dma_addr);
  229. sub_crq.rx_add.map_id = pool->long_term_buff.map_id;
  230. /* The length field of the sCRQ is defined to be 24 bits so the
  231. * buffer size needs to be left shifted by a byte before it is
  232. * converted to big endian to prevent the last byte from being
  233. * truncated.
  234. */
  235. #ifdef __LITTLE_ENDIAN__
  236. shift = 8;
  237. #endif
  238. sub_crq.rx_add.len = cpu_to_be32(pool->buff_size << shift);
  239. lpar_rc = send_subcrq(adapter, handle_array[pool->index],
  240. &sub_crq);
  241. if (lpar_rc != H_SUCCESS)
  242. goto failure;
  243. buffers_added++;
  244. adapter->replenish_add_buff_success++;
  245. pool->next_free = (pool->next_free + 1) % pool->size;
  246. }
  247. atomic_add(buffers_added, &pool->available);
  248. return;
  249. failure:
  250. dev_info(dev, "replenish pools failure\n");
  251. pool->free_map[pool->next_free] = index;
  252. pool->rx_buff[index].skb = NULL;
  253. if (!dma_mapping_error(dev, dma_addr))
  254. dma_unmap_single(dev, dma_addr, pool->buff_size,
  255. DMA_FROM_DEVICE);
  256. dev_kfree_skb_any(skb);
  257. adapter->replenish_add_buff_failure++;
  258. atomic_add(buffers_added, &pool->available);
  259. }
  260. static void replenish_pools(struct ibmvnic_adapter *adapter)
  261. {
  262. int i;
  263. adapter->replenish_task_cycles++;
  264. for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
  265. i++) {
  266. if (adapter->rx_pool[i].active)
  267. replenish_rx_pool(adapter, &adapter->rx_pool[i]);
  268. }
  269. }
  270. static void release_stats_token(struct ibmvnic_adapter *adapter)
  271. {
  272. struct device *dev = &adapter->vdev->dev;
  273. if (!adapter->stats_token)
  274. return;
  275. dma_unmap_single(dev, adapter->stats_token,
  276. sizeof(struct ibmvnic_statistics),
  277. DMA_FROM_DEVICE);
  278. adapter->stats_token = 0;
  279. }
  280. static int init_stats_token(struct ibmvnic_adapter *adapter)
  281. {
  282. struct device *dev = &adapter->vdev->dev;
  283. dma_addr_t stok;
  284. stok = dma_map_single(dev, &adapter->stats,
  285. sizeof(struct ibmvnic_statistics),
  286. DMA_FROM_DEVICE);
  287. if (dma_mapping_error(dev, stok)) {
  288. dev_err(dev, "Couldn't map stats buffer\n");
  289. return -1;
  290. }
  291. adapter->stats_token = stok;
  292. return 0;
  293. }
  294. static void release_rx_pools(struct ibmvnic_adapter *adapter)
  295. {
  296. struct ibmvnic_rx_pool *rx_pool;
  297. int rx_scrqs;
  298. int i, j;
  299. if (!adapter->rx_pool)
  300. return;
  301. rx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
  302. for (i = 0; i < rx_scrqs; i++) {
  303. rx_pool = &adapter->rx_pool[i];
  304. kfree(rx_pool->free_map);
  305. free_long_term_buff(adapter, &rx_pool->long_term_buff);
  306. if (!rx_pool->rx_buff)
  307. continue;
  308. for (j = 0; j < rx_pool->size; j++) {
  309. if (rx_pool->rx_buff[j].skb) {
  310. dev_kfree_skb_any(rx_pool->rx_buff[i].skb);
  311. rx_pool->rx_buff[i].skb = NULL;
  312. }
  313. }
  314. kfree(rx_pool->rx_buff);
  315. }
  316. kfree(adapter->rx_pool);
  317. adapter->rx_pool = NULL;
  318. }
  319. static int init_rx_pools(struct net_device *netdev)
  320. {
  321. struct ibmvnic_adapter *adapter = netdev_priv(netdev);
  322. struct device *dev = &adapter->vdev->dev;
  323. struct ibmvnic_rx_pool *rx_pool;
  324. int rxadd_subcrqs;
  325. u64 *size_array;
  326. int i, j;
  327. rxadd_subcrqs =
  328. be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
  329. size_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
  330. be32_to_cpu(adapter->login_rsp_buf->off_rxadd_buff_size));
  331. adapter->rx_pool = kcalloc(rxadd_subcrqs,
  332. sizeof(struct ibmvnic_rx_pool),
  333. GFP_KERNEL);
  334. if (!adapter->rx_pool) {
  335. dev_err(dev, "Failed to allocate rx pools\n");
  336. return -1;
  337. }
  338. for (i = 0; i < rxadd_subcrqs; i++) {
  339. rx_pool = &adapter->rx_pool[i];
  340. netdev_dbg(adapter->netdev,
  341. "Initializing rx_pool %d, %lld buffs, %lld bytes each\n",
  342. i, adapter->req_rx_add_entries_per_subcrq,
  343. be64_to_cpu(size_array[i]));
  344. rx_pool->size = adapter->req_rx_add_entries_per_subcrq;
  345. rx_pool->index = i;
  346. rx_pool->buff_size = be64_to_cpu(size_array[i]);
  347. rx_pool->active = 1;
  348. rx_pool->free_map = kcalloc(rx_pool->size, sizeof(int),
  349. GFP_KERNEL);
  350. if (!rx_pool->free_map) {
  351. release_rx_pools(adapter);
  352. return -1;
  353. }
  354. rx_pool->rx_buff = kcalloc(rx_pool->size,
  355. sizeof(struct ibmvnic_rx_buff),
  356. GFP_KERNEL);
  357. if (!rx_pool->rx_buff) {
  358. dev_err(dev, "Couldn't alloc rx buffers\n");
  359. release_rx_pools(adapter);
  360. return -1;
  361. }
  362. if (alloc_long_term_buff(adapter, &rx_pool->long_term_buff,
  363. rx_pool->size * rx_pool->buff_size)) {
  364. release_rx_pools(adapter);
  365. return -1;
  366. }
  367. for (j = 0; j < rx_pool->size; ++j)
  368. rx_pool->free_map[j] = j;
  369. atomic_set(&rx_pool->available, 0);
  370. rx_pool->next_alloc = 0;
  371. rx_pool->next_free = 0;
  372. }
  373. return 0;
  374. }
  375. static void release_tx_pools(struct ibmvnic_adapter *adapter)
  376. {
  377. struct ibmvnic_tx_pool *tx_pool;
  378. int i, tx_scrqs;
  379. if (!adapter->tx_pool)
  380. return;
  381. tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
  382. for (i = 0; i < tx_scrqs; i++) {
  383. tx_pool = &adapter->tx_pool[i];
  384. kfree(tx_pool->tx_buff);
  385. free_long_term_buff(adapter, &tx_pool->long_term_buff);
  386. kfree(tx_pool->free_map);
  387. }
  388. kfree(adapter->tx_pool);
  389. adapter->tx_pool = NULL;
  390. }
  391. static int init_tx_pools(struct net_device *netdev)
  392. {
  393. struct ibmvnic_adapter *adapter = netdev_priv(netdev);
  394. struct device *dev = &adapter->vdev->dev;
  395. struct ibmvnic_tx_pool *tx_pool;
  396. int tx_subcrqs;
  397. int i, j;
  398. tx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
  399. adapter->tx_pool = kcalloc(tx_subcrqs,
  400. sizeof(struct ibmvnic_tx_pool), GFP_KERNEL);
  401. if (!adapter->tx_pool)
  402. return -1;
  403. for (i = 0; i < tx_subcrqs; i++) {
  404. tx_pool = &adapter->tx_pool[i];
  405. tx_pool->tx_buff = kcalloc(adapter->req_tx_entries_per_subcrq,
  406. sizeof(struct ibmvnic_tx_buff),
  407. GFP_KERNEL);
  408. if (!tx_pool->tx_buff) {
  409. dev_err(dev, "tx pool buffer allocation failed\n");
  410. release_tx_pools(adapter);
  411. return -1;
  412. }
  413. if (alloc_long_term_buff(adapter, &tx_pool->long_term_buff,
  414. adapter->req_tx_entries_per_subcrq *
  415. adapter->req_mtu)) {
  416. release_tx_pools(adapter);
  417. return -1;
  418. }
  419. tx_pool->free_map = kcalloc(adapter->req_tx_entries_per_subcrq,
  420. sizeof(int), GFP_KERNEL);
  421. if (!tx_pool->free_map) {
  422. release_tx_pools(adapter);
  423. return -1;
  424. }
  425. for (j = 0; j < adapter->req_tx_entries_per_subcrq; j++)
  426. tx_pool->free_map[j] = j;
  427. tx_pool->consumer_index = 0;
  428. tx_pool->producer_index = 0;
  429. }
  430. return 0;
  431. }
  432. static void release_error_buffers(struct ibmvnic_adapter *adapter)
  433. {
  434. struct device *dev = &adapter->vdev->dev;
  435. struct ibmvnic_error_buff *error_buff, *tmp;
  436. unsigned long flags;
  437. spin_lock_irqsave(&adapter->error_list_lock, flags);
  438. list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list) {
  439. list_del(&error_buff->list);
  440. dma_unmap_single(dev, error_buff->dma, error_buff->len,
  441. DMA_FROM_DEVICE);
  442. kfree(error_buff->buff);
  443. kfree(error_buff);
  444. }
  445. spin_unlock_irqrestore(&adapter->error_list_lock, flags);
  446. }
  447. static int ibmvnic_login(struct net_device *netdev)
  448. {
  449. struct ibmvnic_adapter *adapter = netdev_priv(netdev);
  450. unsigned long timeout = msecs_to_jiffies(30000);
  451. struct device *dev = &adapter->vdev->dev;
  452. do {
  453. if (adapter->renegotiate) {
  454. adapter->renegotiate = false;
  455. release_sub_crqs(adapter);
  456. reinit_completion(&adapter->init_done);
  457. send_cap_queries(adapter);
  458. if (!wait_for_completion_timeout(&adapter->init_done,
  459. timeout)) {
  460. dev_err(dev, "Capabilities query timeout\n");
  461. return -1;
  462. }
  463. }
  464. reinit_completion(&adapter->init_done);
  465. send_login(adapter);
  466. if (!wait_for_completion_timeout(&adapter->init_done,
  467. timeout)) {
  468. dev_err(dev, "Login timeout\n");
  469. return -1;
  470. }
  471. } while (adapter->renegotiate);
  472. return 0;
  473. }
  474. static void release_resources(struct ibmvnic_adapter *adapter)
  475. {
  476. int i;
  477. release_tx_pools(adapter);
  478. release_rx_pools(adapter);
  479. release_stats_token(adapter);
  480. release_error_buffers(adapter);
  481. if (adapter->napi) {
  482. for (i = 0; i < adapter->req_rx_queues; i++) {
  483. if (&adapter->napi[i])
  484. netif_napi_del(&adapter->napi[i]);
  485. }
  486. }
  487. }
  488. static int set_link_state(struct ibmvnic_adapter *adapter, u8 link_state)
  489. {
  490. struct net_device *netdev = adapter->netdev;
  491. unsigned long timeout = msecs_to_jiffies(30000);
  492. union ibmvnic_crq crq;
  493. bool resend;
  494. int rc;
  495. netdev_err(netdev, "setting link state %d\n", link_state);
  496. memset(&crq, 0, sizeof(crq));
  497. crq.logical_link_state.first = IBMVNIC_CRQ_CMD;
  498. crq.logical_link_state.cmd = LOGICAL_LINK_STATE;
  499. crq.logical_link_state.link_state = link_state;
  500. do {
  501. resend = false;
  502. reinit_completion(&adapter->init_done);
  503. rc = ibmvnic_send_crq(adapter, &crq);
  504. if (rc) {
  505. netdev_err(netdev, "Failed to set link state\n");
  506. return rc;
  507. }
  508. if (!wait_for_completion_timeout(&adapter->init_done,
  509. timeout)) {
  510. netdev_err(netdev, "timeout setting link state\n");
  511. return -1;
  512. }
  513. if (adapter->init_done_rc == 1) {
  514. /* Partuial success, delay and re-send */
  515. mdelay(1000);
  516. resend = true;
  517. }
  518. } while (resend);
  519. return 0;
  520. }
  521. static int set_real_num_queues(struct net_device *netdev)
  522. {
  523. struct ibmvnic_adapter *adapter = netdev_priv(netdev);
  524. int rc;
  525. rc = netif_set_real_num_tx_queues(netdev, adapter->req_tx_queues);
  526. if (rc) {
  527. netdev_err(netdev, "failed to set the number of tx queues\n");
  528. return rc;
  529. }
  530. rc = netif_set_real_num_rx_queues(netdev, adapter->req_rx_queues);
  531. if (rc)
  532. netdev_err(netdev, "failed to set the number of rx queues\n");
  533. return rc;
  534. }
  535. static int init_resources(struct ibmvnic_adapter *adapter)
  536. {
  537. struct net_device *netdev = adapter->netdev;
  538. int i, rc;
  539. rc = set_real_num_queues(netdev);
  540. if (rc)
  541. return rc;
  542. rc = init_sub_crq_irqs(adapter);
  543. if (rc) {
  544. netdev_err(netdev, "failed to initialize sub crq irqs\n");
  545. return -1;
  546. }
  547. rc = init_stats_token(adapter);
  548. if (rc)
  549. return rc;
  550. adapter->map_id = 1;
  551. adapter->napi = kcalloc(adapter->req_rx_queues,
  552. sizeof(struct napi_struct), GFP_KERNEL);
  553. if (!adapter->napi)
  554. return -ENOMEM;
  555. for (i = 0; i < adapter->req_rx_queues; i++) {
  556. netif_napi_add(netdev, &adapter->napi[i], ibmvnic_poll,
  557. NAPI_POLL_WEIGHT);
  558. }
  559. send_map_query(adapter);
  560. rc = init_rx_pools(netdev);
  561. if (rc)
  562. return rc;
  563. rc = init_tx_pools(netdev);
  564. return rc;
  565. }
  566. static int __ibmvnic_open(struct net_device *netdev)
  567. {
  568. struct ibmvnic_adapter *adapter = netdev_priv(netdev);
  569. enum vnic_state prev_state = adapter->state;
  570. int i, rc;
  571. adapter->state = VNIC_OPENING;
  572. replenish_pools(adapter);
  573. for (i = 0; i < adapter->req_rx_queues; i++)
  574. napi_enable(&adapter->napi[i]);
  575. /* We're ready to receive frames, enable the sub-crq interrupts and
  576. * set the logical link state to up
  577. */
  578. for (i = 0; i < adapter->req_rx_queues; i++) {
  579. if (prev_state == VNIC_CLOSED)
  580. enable_irq(adapter->rx_scrq[i]->irq);
  581. else
  582. enable_scrq_irq(adapter, adapter->rx_scrq[i]);
  583. }
  584. for (i = 0; i < adapter->req_tx_queues; i++) {
  585. if (prev_state == VNIC_CLOSED)
  586. enable_irq(adapter->tx_scrq[i]->irq);
  587. else
  588. enable_scrq_irq(adapter, adapter->tx_scrq[i]);
  589. }
  590. rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_UP);
  591. if (rc) {
  592. for (i = 0; i < adapter->req_rx_queues; i++)
  593. napi_disable(&adapter->napi[i]);
  594. release_resources(adapter);
  595. return rc;
  596. }
  597. netif_tx_start_all_queues(netdev);
  598. if (prev_state == VNIC_CLOSED) {
  599. for (i = 0; i < adapter->req_rx_queues; i++)
  600. napi_schedule(&adapter->napi[i]);
  601. }
  602. adapter->state = VNIC_OPEN;
  603. return rc;
  604. }
  605. static int ibmvnic_open(struct net_device *netdev)
  606. {
  607. struct ibmvnic_adapter *adapter = netdev_priv(netdev);
  608. int rc;
  609. mutex_lock(&adapter->reset_lock);
  610. if (adapter->state != VNIC_CLOSED) {
  611. rc = ibmvnic_login(netdev);
  612. if (rc) {
  613. mutex_unlock(&adapter->reset_lock);
  614. return rc;
  615. }
  616. rc = init_resources(adapter);
  617. if (rc) {
  618. netdev_err(netdev, "failed to initialize resources\n");
  619. release_resources(adapter);
  620. mutex_unlock(&adapter->reset_lock);
  621. return rc;
  622. }
  623. }
  624. rc = __ibmvnic_open(netdev);
  625. mutex_unlock(&adapter->reset_lock);
  626. return rc;
  627. }
  628. static void disable_sub_crqs(struct ibmvnic_adapter *adapter)
  629. {
  630. int i;
  631. if (adapter->tx_scrq) {
  632. for (i = 0; i < adapter->req_tx_queues; i++)
  633. if (adapter->tx_scrq[i])
  634. disable_irq(adapter->tx_scrq[i]->irq);
  635. }
  636. if (adapter->rx_scrq) {
  637. for (i = 0; i < adapter->req_rx_queues; i++)
  638. if (adapter->rx_scrq[i])
  639. disable_irq(adapter->rx_scrq[i]->irq);
  640. }
  641. }
  642. static void clean_tx_pools(struct ibmvnic_adapter *adapter)
  643. {
  644. struct ibmvnic_tx_pool *tx_pool;
  645. u64 tx_entries;
  646. int tx_scrqs;
  647. int i, j;
  648. if (!adapter->tx_pool)
  649. return;
  650. tx_scrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
  651. tx_entries = adapter->req_tx_entries_per_subcrq;
  652. /* Free any remaining skbs in the tx buffer pools */
  653. for (i = 0; i < tx_scrqs; i++) {
  654. tx_pool = &adapter->tx_pool[i];
  655. if (!tx_pool)
  656. continue;
  657. for (j = 0; j < tx_entries; j++) {
  658. if (tx_pool->tx_buff[j].skb) {
  659. dev_kfree_skb_any(tx_pool->tx_buff[j].skb);
  660. tx_pool->tx_buff[j].skb = NULL;
  661. }
  662. }
  663. }
  664. }
  665. static int __ibmvnic_close(struct net_device *netdev)
  666. {
  667. struct ibmvnic_adapter *adapter = netdev_priv(netdev);
  668. int rc = 0;
  669. int i;
  670. adapter->state = VNIC_CLOSING;
  671. netif_tx_stop_all_queues(netdev);
  672. clean_tx_pools(adapter);
  673. disable_sub_crqs(adapter);
  674. if (adapter->napi) {
  675. for (i = 0; i < adapter->req_rx_queues; i++)
  676. napi_disable(&adapter->napi[i]);
  677. }
  678. rc = set_link_state(adapter, IBMVNIC_LOGICAL_LNK_DN);
  679. adapter->state = VNIC_CLOSED;
  680. return rc;
  681. }
  682. static int ibmvnic_close(struct net_device *netdev)
  683. {
  684. struct ibmvnic_adapter *adapter = netdev_priv(netdev);
  685. int rc;
  686. mutex_lock(&adapter->reset_lock);
  687. rc = __ibmvnic_close(netdev);
  688. mutex_unlock(&adapter->reset_lock);
  689. return rc;
  690. }
  691. /**
  692. * build_hdr_data - creates L2/L3/L4 header data buffer
  693. * @hdr_field - bitfield determining needed headers
  694. * @skb - socket buffer
  695. * @hdr_len - array of header lengths
  696. * @tot_len - total length of data
  697. *
  698. * Reads hdr_field to determine which headers are needed by firmware.
  699. * Builds a buffer containing these headers. Saves individual header
  700. * lengths and total buffer length to be used to build descriptors.
  701. */
  702. static int build_hdr_data(u8 hdr_field, struct sk_buff *skb,
  703. int *hdr_len, u8 *hdr_data)
  704. {
  705. int len = 0;
  706. u8 *hdr;
  707. hdr_len[0] = sizeof(struct ethhdr);
  708. if (skb->protocol == htons(ETH_P_IP)) {
  709. hdr_len[1] = ip_hdr(skb)->ihl * 4;
  710. if (ip_hdr(skb)->protocol == IPPROTO_TCP)
  711. hdr_len[2] = tcp_hdrlen(skb);
  712. else if (ip_hdr(skb)->protocol == IPPROTO_UDP)
  713. hdr_len[2] = sizeof(struct udphdr);
  714. } else if (skb->protocol == htons(ETH_P_IPV6)) {
  715. hdr_len[1] = sizeof(struct ipv6hdr);
  716. if (ipv6_hdr(skb)->nexthdr == IPPROTO_TCP)
  717. hdr_len[2] = tcp_hdrlen(skb);
  718. else if (ipv6_hdr(skb)->nexthdr == IPPROTO_UDP)
  719. hdr_len[2] = sizeof(struct udphdr);
  720. }
  721. memset(hdr_data, 0, 120);
  722. if ((hdr_field >> 6) & 1) {
  723. hdr = skb_mac_header(skb);
  724. memcpy(hdr_data, hdr, hdr_len[0]);
  725. len += hdr_len[0];
  726. }
  727. if ((hdr_field >> 5) & 1) {
  728. hdr = skb_network_header(skb);
  729. memcpy(hdr_data + len, hdr, hdr_len[1]);
  730. len += hdr_len[1];
  731. }
  732. if ((hdr_field >> 4) & 1) {
  733. hdr = skb_transport_header(skb);
  734. memcpy(hdr_data + len, hdr, hdr_len[2]);
  735. len += hdr_len[2];
  736. }
  737. return len;
  738. }
  739. /**
  740. * create_hdr_descs - create header and header extension descriptors
  741. * @hdr_field - bitfield determining needed headers
  742. * @data - buffer containing header data
  743. * @len - length of data buffer
  744. * @hdr_len - array of individual header lengths
  745. * @scrq_arr - descriptor array
  746. *
  747. * Creates header and, if needed, header extension descriptors and
  748. * places them in a descriptor array, scrq_arr
  749. */
  750. static void create_hdr_descs(u8 hdr_field, u8 *hdr_data, int len, int *hdr_len,
  751. union sub_crq *scrq_arr)
  752. {
  753. union sub_crq hdr_desc;
  754. int tmp_len = len;
  755. u8 *data, *cur;
  756. int tmp;
  757. while (tmp_len > 0) {
  758. cur = hdr_data + len - tmp_len;
  759. memset(&hdr_desc, 0, sizeof(hdr_desc));
  760. if (cur != hdr_data) {
  761. data = hdr_desc.hdr_ext.data;
  762. tmp = tmp_len > 29 ? 29 : tmp_len;
  763. hdr_desc.hdr_ext.first = IBMVNIC_CRQ_CMD;
  764. hdr_desc.hdr_ext.type = IBMVNIC_HDR_EXT_DESC;
  765. hdr_desc.hdr_ext.len = tmp;
  766. } else {
  767. data = hdr_desc.hdr.data;
  768. tmp = tmp_len > 24 ? 24 : tmp_len;
  769. hdr_desc.hdr.first = IBMVNIC_CRQ_CMD;
  770. hdr_desc.hdr.type = IBMVNIC_HDR_DESC;
  771. hdr_desc.hdr.len = tmp;
  772. hdr_desc.hdr.l2_len = (u8)hdr_len[0];
  773. hdr_desc.hdr.l3_len = cpu_to_be16((u16)hdr_len[1]);
  774. hdr_desc.hdr.l4_len = (u8)hdr_len[2];
  775. hdr_desc.hdr.flag = hdr_field << 1;
  776. }
  777. memcpy(data, cur, tmp);
  778. tmp_len -= tmp;
  779. *scrq_arr = hdr_desc;
  780. scrq_arr++;
  781. }
  782. }
  783. /**
  784. * build_hdr_descs_arr - build a header descriptor array
  785. * @skb - socket buffer
  786. * @num_entries - number of descriptors to be sent
  787. * @subcrq - first TX descriptor
  788. * @hdr_field - bit field determining which headers will be sent
  789. *
  790. * This function will build a TX descriptor array with applicable
  791. * L2/L3/L4 packet header descriptors to be sent by send_subcrq_indirect.
  792. */
  793. static void build_hdr_descs_arr(struct ibmvnic_tx_buff *txbuff,
  794. int *num_entries, u8 hdr_field)
  795. {
  796. int hdr_len[3] = {0, 0, 0};
  797. int tot_len, len;
  798. u8 *hdr_data = txbuff->hdr_data;
  799. tot_len = build_hdr_data(hdr_field, txbuff->skb, hdr_len,
  800. txbuff->hdr_data);
  801. len = tot_len;
  802. len -= 24;
  803. if (len > 0)
  804. num_entries += len % 29 ? len / 29 + 1 : len / 29;
  805. create_hdr_descs(hdr_field, hdr_data, tot_len, hdr_len,
  806. txbuff->indir_arr + 1);
  807. }
  808. static int ibmvnic_xmit(struct sk_buff *skb, struct net_device *netdev)
  809. {
  810. struct ibmvnic_adapter *adapter = netdev_priv(netdev);
  811. int queue_num = skb_get_queue_mapping(skb);
  812. u8 *hdrs = (u8 *)&adapter->tx_rx_desc_req;
  813. struct device *dev = &adapter->vdev->dev;
  814. struct ibmvnic_tx_buff *tx_buff = NULL;
  815. struct ibmvnic_sub_crq_queue *tx_scrq;
  816. struct ibmvnic_tx_pool *tx_pool;
  817. unsigned int tx_send_failed = 0;
  818. unsigned int tx_map_failed = 0;
  819. unsigned int tx_dropped = 0;
  820. unsigned int tx_packets = 0;
  821. unsigned int tx_bytes = 0;
  822. dma_addr_t data_dma_addr;
  823. struct netdev_queue *txq;
  824. unsigned long lpar_rc;
  825. union sub_crq tx_crq;
  826. unsigned int offset;
  827. int num_entries = 1;
  828. unsigned char *dst;
  829. u64 *handle_array;
  830. int index = 0;
  831. int ret = 0;
  832. tx_pool = &adapter->tx_pool[queue_num];
  833. tx_scrq = adapter->tx_scrq[queue_num];
  834. txq = netdev_get_tx_queue(netdev, skb_get_queue_mapping(skb));
  835. handle_array = (u64 *)((u8 *)(adapter->login_rsp_buf) +
  836. be32_to_cpu(adapter->login_rsp_buf->
  837. off_txsubm_subcrqs));
  838. if (adapter->resetting) {
  839. if (!netif_subqueue_stopped(netdev, skb))
  840. netif_stop_subqueue(netdev, queue_num);
  841. dev_kfree_skb_any(skb);
  842. tx_send_failed++;
  843. tx_dropped++;
  844. ret = NETDEV_TX_OK;
  845. goto out;
  846. }
  847. index = tx_pool->free_map[tx_pool->consumer_index];
  848. offset = index * adapter->req_mtu;
  849. dst = tx_pool->long_term_buff.buff + offset;
  850. memset(dst, 0, adapter->req_mtu);
  851. skb_copy_from_linear_data(skb, dst, skb->len);
  852. data_dma_addr = tx_pool->long_term_buff.addr + offset;
  853. tx_pool->consumer_index =
  854. (tx_pool->consumer_index + 1) %
  855. adapter->req_tx_entries_per_subcrq;
  856. tx_buff = &tx_pool->tx_buff[index];
  857. tx_buff->skb = skb;
  858. tx_buff->data_dma[0] = data_dma_addr;
  859. tx_buff->data_len[0] = skb->len;
  860. tx_buff->index = index;
  861. tx_buff->pool_index = queue_num;
  862. tx_buff->last_frag = true;
  863. memset(&tx_crq, 0, sizeof(tx_crq));
  864. tx_crq.v1.first = IBMVNIC_CRQ_CMD;
  865. tx_crq.v1.type = IBMVNIC_TX_DESC;
  866. tx_crq.v1.n_crq_elem = 1;
  867. tx_crq.v1.n_sge = 1;
  868. tx_crq.v1.flags1 = IBMVNIC_TX_COMP_NEEDED;
  869. tx_crq.v1.correlator = cpu_to_be32(index);
  870. tx_crq.v1.dma_reg = cpu_to_be16(tx_pool->long_term_buff.map_id);
  871. tx_crq.v1.sge_len = cpu_to_be32(skb->len);
  872. tx_crq.v1.ioba = cpu_to_be64(data_dma_addr);
  873. if (adapter->vlan_header_insertion) {
  874. tx_crq.v1.flags2 |= IBMVNIC_TX_VLAN_INSERT;
  875. tx_crq.v1.vlan_id = cpu_to_be16(skb->vlan_tci);
  876. }
  877. if (skb->protocol == htons(ETH_P_IP)) {
  878. if (ip_hdr(skb)->version == 4)
  879. tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV4;
  880. else if (ip_hdr(skb)->version == 6)
  881. tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_IPV6;
  882. if (ip_hdr(skb)->protocol == IPPROTO_TCP)
  883. tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_TCP;
  884. else if (ip_hdr(skb)->protocol != IPPROTO_TCP)
  885. tx_crq.v1.flags1 |= IBMVNIC_TX_PROT_UDP;
  886. }
  887. if (skb->ip_summed == CHECKSUM_PARTIAL) {
  888. tx_crq.v1.flags1 |= IBMVNIC_TX_CHKSUM_OFFLOAD;
  889. hdrs += 2;
  890. }
  891. /* determine if l2/3/4 headers are sent to firmware */
  892. if ((*hdrs >> 7) & 1 &&
  893. (skb->protocol == htons(ETH_P_IP) ||
  894. skb->protocol == htons(ETH_P_IPV6))) {
  895. build_hdr_descs_arr(tx_buff, &num_entries, *hdrs);
  896. tx_crq.v1.n_crq_elem = num_entries;
  897. tx_buff->indir_arr[0] = tx_crq;
  898. tx_buff->indir_dma = dma_map_single(dev, tx_buff->indir_arr,
  899. sizeof(tx_buff->indir_arr),
  900. DMA_TO_DEVICE);
  901. if (dma_mapping_error(dev, tx_buff->indir_dma)) {
  902. dev_kfree_skb_any(skb);
  903. tx_buff->skb = NULL;
  904. if (!firmware_has_feature(FW_FEATURE_CMO))
  905. dev_err(dev, "tx: unable to map descriptor array\n");
  906. tx_map_failed++;
  907. tx_dropped++;
  908. ret = NETDEV_TX_OK;
  909. goto out;
  910. }
  911. lpar_rc = send_subcrq_indirect(adapter, handle_array[queue_num],
  912. (u64)tx_buff->indir_dma,
  913. (u64)num_entries);
  914. } else {
  915. lpar_rc = send_subcrq(adapter, handle_array[queue_num],
  916. &tx_crq);
  917. }
  918. if (lpar_rc != H_SUCCESS) {
  919. dev_err(dev, "tx failed with code %ld\n", lpar_rc);
  920. if (tx_pool->consumer_index == 0)
  921. tx_pool->consumer_index =
  922. adapter->req_tx_entries_per_subcrq - 1;
  923. else
  924. tx_pool->consumer_index--;
  925. dev_kfree_skb_any(skb);
  926. tx_buff->skb = NULL;
  927. if (lpar_rc == H_CLOSED)
  928. netif_stop_subqueue(netdev, queue_num);
  929. tx_send_failed++;
  930. tx_dropped++;
  931. ret = NETDEV_TX_OK;
  932. goto out;
  933. }
  934. if (atomic_inc_return(&tx_scrq->used)
  935. >= adapter->req_tx_entries_per_subcrq) {
  936. netdev_info(netdev, "Stopping queue %d\n", queue_num);
  937. netif_stop_subqueue(netdev, queue_num);
  938. }
  939. tx_packets++;
  940. tx_bytes += skb->len;
  941. txq->trans_start = jiffies;
  942. ret = NETDEV_TX_OK;
  943. out:
  944. netdev->stats.tx_dropped += tx_dropped;
  945. netdev->stats.tx_bytes += tx_bytes;
  946. netdev->stats.tx_packets += tx_packets;
  947. adapter->tx_send_failed += tx_send_failed;
  948. adapter->tx_map_failed += tx_map_failed;
  949. return ret;
  950. }
  951. static void ibmvnic_set_multi(struct net_device *netdev)
  952. {
  953. struct ibmvnic_adapter *adapter = netdev_priv(netdev);
  954. struct netdev_hw_addr *ha;
  955. union ibmvnic_crq crq;
  956. memset(&crq, 0, sizeof(crq));
  957. crq.request_capability.first = IBMVNIC_CRQ_CMD;
  958. crq.request_capability.cmd = REQUEST_CAPABILITY;
  959. if (netdev->flags & IFF_PROMISC) {
  960. if (!adapter->promisc_supported)
  961. return;
  962. } else {
  963. if (netdev->flags & IFF_ALLMULTI) {
  964. /* Accept all multicast */
  965. memset(&crq, 0, sizeof(crq));
  966. crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
  967. crq.multicast_ctrl.cmd = MULTICAST_CTRL;
  968. crq.multicast_ctrl.flags = IBMVNIC_ENABLE_ALL;
  969. ibmvnic_send_crq(adapter, &crq);
  970. } else if (netdev_mc_empty(netdev)) {
  971. /* Reject all multicast */
  972. memset(&crq, 0, sizeof(crq));
  973. crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
  974. crq.multicast_ctrl.cmd = MULTICAST_CTRL;
  975. crq.multicast_ctrl.flags = IBMVNIC_DISABLE_ALL;
  976. ibmvnic_send_crq(adapter, &crq);
  977. } else {
  978. /* Accept one or more multicast(s) */
  979. netdev_for_each_mc_addr(ha, netdev) {
  980. memset(&crq, 0, sizeof(crq));
  981. crq.multicast_ctrl.first = IBMVNIC_CRQ_CMD;
  982. crq.multicast_ctrl.cmd = MULTICAST_CTRL;
  983. crq.multicast_ctrl.flags = IBMVNIC_ENABLE_MC;
  984. ether_addr_copy(&crq.multicast_ctrl.mac_addr[0],
  985. ha->addr);
  986. ibmvnic_send_crq(adapter, &crq);
  987. }
  988. }
  989. }
  990. }
  991. static int ibmvnic_set_mac(struct net_device *netdev, void *p)
  992. {
  993. struct ibmvnic_adapter *adapter = netdev_priv(netdev);
  994. struct sockaddr *addr = p;
  995. union ibmvnic_crq crq;
  996. if (!is_valid_ether_addr(addr->sa_data))
  997. return -EADDRNOTAVAIL;
  998. memset(&crq, 0, sizeof(crq));
  999. crq.change_mac_addr.first = IBMVNIC_CRQ_CMD;
  1000. crq.change_mac_addr.cmd = CHANGE_MAC_ADDR;
  1001. ether_addr_copy(&crq.change_mac_addr.mac_addr[0], addr->sa_data);
  1002. ibmvnic_send_crq(adapter, &crq);
  1003. /* netdev->dev_addr is changed in handle_change_mac_rsp function */
  1004. return 0;
  1005. }
  1006. /**
  1007. * do_reset returns zero if we are able to keep processing reset events, or
  1008. * non-zero if we hit a fatal error and must halt.
  1009. */
  1010. static int do_reset(struct ibmvnic_adapter *adapter,
  1011. struct ibmvnic_rwi *rwi, u32 reset_state)
  1012. {
  1013. struct net_device *netdev = adapter->netdev;
  1014. int i, rc;
  1015. netif_carrier_off(netdev);
  1016. adapter->reset_reason = rwi->reset_reason;
  1017. if (rwi->reset_reason == VNIC_RESET_MOBILITY) {
  1018. rc = ibmvnic_reenable_crq_queue(adapter);
  1019. if (rc)
  1020. return 0;
  1021. }
  1022. rc = __ibmvnic_close(netdev);
  1023. if (rc)
  1024. return rc;
  1025. /* remove the closed state so when we call open it appears
  1026. * we are coming from the probed state.
  1027. */
  1028. adapter->state = VNIC_PROBED;
  1029. release_resources(adapter);
  1030. release_sub_crqs(adapter);
  1031. release_crq_queue(adapter);
  1032. rc = ibmvnic_init(adapter);
  1033. if (rc)
  1034. return 0;
  1035. /* If the adapter was in PROBE state prior to the reset, exit here. */
  1036. if (reset_state == VNIC_PROBED)
  1037. return 0;
  1038. rc = ibmvnic_login(netdev);
  1039. if (rc) {
  1040. adapter->state = VNIC_PROBED;
  1041. return 0;
  1042. }
  1043. rtnl_lock();
  1044. rc = init_resources(adapter);
  1045. rtnl_unlock();
  1046. if (rc)
  1047. return rc;
  1048. if (reset_state == VNIC_CLOSED)
  1049. return 0;
  1050. rc = __ibmvnic_open(netdev);
  1051. if (rc) {
  1052. if (list_empty(&adapter->rwi_list))
  1053. adapter->state = VNIC_CLOSED;
  1054. else
  1055. adapter->state = reset_state;
  1056. return 0;
  1057. }
  1058. netif_carrier_on(netdev);
  1059. /* kick napi */
  1060. for (i = 0; i < adapter->req_rx_queues; i++)
  1061. napi_schedule(&adapter->napi[i]);
  1062. return 0;
  1063. }
  1064. static struct ibmvnic_rwi *get_next_rwi(struct ibmvnic_adapter *adapter)
  1065. {
  1066. struct ibmvnic_rwi *rwi;
  1067. mutex_lock(&adapter->rwi_lock);
  1068. if (!list_empty(&adapter->rwi_list)) {
  1069. rwi = list_first_entry(&adapter->rwi_list, struct ibmvnic_rwi,
  1070. list);
  1071. list_del(&rwi->list);
  1072. } else {
  1073. rwi = NULL;
  1074. }
  1075. mutex_unlock(&adapter->rwi_lock);
  1076. return rwi;
  1077. }
  1078. static void free_all_rwi(struct ibmvnic_adapter *adapter)
  1079. {
  1080. struct ibmvnic_rwi *rwi;
  1081. rwi = get_next_rwi(adapter);
  1082. while (rwi) {
  1083. kfree(rwi);
  1084. rwi = get_next_rwi(adapter);
  1085. }
  1086. }
  1087. static void __ibmvnic_reset(struct work_struct *work)
  1088. {
  1089. struct ibmvnic_rwi *rwi;
  1090. struct ibmvnic_adapter *adapter;
  1091. struct net_device *netdev;
  1092. u32 reset_state;
  1093. int rc;
  1094. adapter = container_of(work, struct ibmvnic_adapter, ibmvnic_reset);
  1095. netdev = adapter->netdev;
  1096. mutex_lock(&adapter->reset_lock);
  1097. adapter->resetting = true;
  1098. reset_state = adapter->state;
  1099. rwi = get_next_rwi(adapter);
  1100. while (rwi) {
  1101. rc = do_reset(adapter, rwi, reset_state);
  1102. kfree(rwi);
  1103. if (rc)
  1104. break;
  1105. rwi = get_next_rwi(adapter);
  1106. }
  1107. if (rc) {
  1108. free_all_rwi(adapter);
  1109. return;
  1110. }
  1111. adapter->resetting = false;
  1112. mutex_unlock(&adapter->reset_lock);
  1113. }
  1114. static void ibmvnic_reset(struct ibmvnic_adapter *adapter,
  1115. enum ibmvnic_reset_reason reason)
  1116. {
  1117. struct ibmvnic_rwi *rwi, *tmp;
  1118. struct net_device *netdev = adapter->netdev;
  1119. struct list_head *entry;
  1120. if (adapter->state == VNIC_REMOVING ||
  1121. adapter->state == VNIC_REMOVED) {
  1122. netdev_dbg(netdev, "Adapter removing, skipping reset\n");
  1123. return;
  1124. }
  1125. mutex_lock(&adapter->rwi_lock);
  1126. list_for_each(entry, &adapter->rwi_list) {
  1127. tmp = list_entry(entry, struct ibmvnic_rwi, list);
  1128. if (tmp->reset_reason == reason) {
  1129. netdev_err(netdev, "Matching reset found, skipping\n");
  1130. mutex_unlock(&adapter->rwi_lock);
  1131. return;
  1132. }
  1133. }
  1134. rwi = kzalloc(sizeof(*rwi), GFP_KERNEL);
  1135. if (!rwi) {
  1136. mutex_unlock(&adapter->rwi_lock);
  1137. ibmvnic_close(netdev);
  1138. return;
  1139. }
  1140. rwi->reset_reason = reason;
  1141. list_add_tail(&rwi->list, &adapter->rwi_list);
  1142. mutex_unlock(&adapter->rwi_lock);
  1143. schedule_work(&adapter->ibmvnic_reset);
  1144. }
  1145. static void ibmvnic_tx_timeout(struct net_device *dev)
  1146. {
  1147. struct ibmvnic_adapter *adapter = netdev_priv(dev);
  1148. ibmvnic_reset(adapter, VNIC_RESET_TIMEOUT);
  1149. }
  1150. static void remove_buff_from_pool(struct ibmvnic_adapter *adapter,
  1151. struct ibmvnic_rx_buff *rx_buff)
  1152. {
  1153. struct ibmvnic_rx_pool *pool = &adapter->rx_pool[rx_buff->pool_index];
  1154. rx_buff->skb = NULL;
  1155. pool->free_map[pool->next_alloc] = (int)(rx_buff - pool->rx_buff);
  1156. pool->next_alloc = (pool->next_alloc + 1) % pool->size;
  1157. atomic_dec(&pool->available);
  1158. }
  1159. static int ibmvnic_poll(struct napi_struct *napi, int budget)
  1160. {
  1161. struct net_device *netdev = napi->dev;
  1162. struct ibmvnic_adapter *adapter = netdev_priv(netdev);
  1163. int scrq_num = (int)(napi - adapter->napi);
  1164. int frames_processed = 0;
  1165. restart_poll:
  1166. while (frames_processed < budget) {
  1167. struct sk_buff *skb;
  1168. struct ibmvnic_rx_buff *rx_buff;
  1169. union sub_crq *next;
  1170. u32 length;
  1171. u16 offset;
  1172. u8 flags = 0;
  1173. if (!pending_scrq(adapter, adapter->rx_scrq[scrq_num]))
  1174. break;
  1175. next = ibmvnic_next_scrq(adapter, adapter->rx_scrq[scrq_num]);
  1176. rx_buff =
  1177. (struct ibmvnic_rx_buff *)be64_to_cpu(next->
  1178. rx_comp.correlator);
  1179. /* do error checking */
  1180. if (next->rx_comp.rc) {
  1181. netdev_err(netdev, "rx error %x\n", next->rx_comp.rc);
  1182. /* free the entry */
  1183. next->rx_comp.first = 0;
  1184. remove_buff_from_pool(adapter, rx_buff);
  1185. break;
  1186. }
  1187. length = be32_to_cpu(next->rx_comp.len);
  1188. offset = be16_to_cpu(next->rx_comp.off_frame_data);
  1189. flags = next->rx_comp.flags;
  1190. skb = rx_buff->skb;
  1191. skb_copy_to_linear_data(skb, rx_buff->data + offset,
  1192. length);
  1193. /* VLAN Header has been stripped by the system firmware and
  1194. * needs to be inserted by the driver
  1195. */
  1196. if (adapter->rx_vlan_header_insertion &&
  1197. (flags & IBMVNIC_VLAN_STRIPPED))
  1198. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
  1199. ntohs(next->rx_comp.vlan_tci));
  1200. /* free the entry */
  1201. next->rx_comp.first = 0;
  1202. remove_buff_from_pool(adapter, rx_buff);
  1203. skb_put(skb, length);
  1204. skb->protocol = eth_type_trans(skb, netdev);
  1205. if (flags & IBMVNIC_IP_CHKSUM_GOOD &&
  1206. flags & IBMVNIC_TCP_UDP_CHKSUM_GOOD) {
  1207. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1208. }
  1209. length = skb->len;
  1210. napi_gro_receive(napi, skb); /* send it up */
  1211. netdev->stats.rx_packets++;
  1212. netdev->stats.rx_bytes += length;
  1213. frames_processed++;
  1214. }
  1215. replenish_rx_pool(adapter, &adapter->rx_pool[scrq_num]);
  1216. if (frames_processed < budget) {
  1217. enable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
  1218. napi_complete_done(napi, frames_processed);
  1219. if (pending_scrq(adapter, adapter->rx_scrq[scrq_num]) &&
  1220. napi_reschedule(napi)) {
  1221. disable_scrq_irq(adapter, adapter->rx_scrq[scrq_num]);
  1222. goto restart_poll;
  1223. }
  1224. }
  1225. return frames_processed;
  1226. }
  1227. #ifdef CONFIG_NET_POLL_CONTROLLER
  1228. static void ibmvnic_netpoll_controller(struct net_device *dev)
  1229. {
  1230. struct ibmvnic_adapter *adapter = netdev_priv(dev);
  1231. int i;
  1232. replenish_pools(netdev_priv(dev));
  1233. for (i = 0; i < adapter->req_rx_queues; i++)
  1234. ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq,
  1235. adapter->rx_scrq[i]);
  1236. }
  1237. #endif
  1238. static const struct net_device_ops ibmvnic_netdev_ops = {
  1239. .ndo_open = ibmvnic_open,
  1240. .ndo_stop = ibmvnic_close,
  1241. .ndo_start_xmit = ibmvnic_xmit,
  1242. .ndo_set_rx_mode = ibmvnic_set_multi,
  1243. .ndo_set_mac_address = ibmvnic_set_mac,
  1244. .ndo_validate_addr = eth_validate_addr,
  1245. .ndo_tx_timeout = ibmvnic_tx_timeout,
  1246. #ifdef CONFIG_NET_POLL_CONTROLLER
  1247. .ndo_poll_controller = ibmvnic_netpoll_controller,
  1248. #endif
  1249. };
  1250. /* ethtool functions */
  1251. static int ibmvnic_get_link_ksettings(struct net_device *netdev,
  1252. struct ethtool_link_ksettings *cmd)
  1253. {
  1254. u32 supported, advertising;
  1255. supported = (SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg |
  1256. SUPPORTED_FIBRE);
  1257. advertising = (ADVERTISED_1000baseT_Full | ADVERTISED_Autoneg |
  1258. ADVERTISED_FIBRE);
  1259. cmd->base.speed = SPEED_1000;
  1260. cmd->base.duplex = DUPLEX_FULL;
  1261. cmd->base.port = PORT_FIBRE;
  1262. cmd->base.phy_address = 0;
  1263. cmd->base.autoneg = AUTONEG_ENABLE;
  1264. ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
  1265. supported);
  1266. ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
  1267. advertising);
  1268. return 0;
  1269. }
  1270. static void ibmvnic_get_drvinfo(struct net_device *dev,
  1271. struct ethtool_drvinfo *info)
  1272. {
  1273. strlcpy(info->driver, ibmvnic_driver_name, sizeof(info->driver));
  1274. strlcpy(info->version, IBMVNIC_DRIVER_VERSION, sizeof(info->version));
  1275. }
  1276. static u32 ibmvnic_get_msglevel(struct net_device *netdev)
  1277. {
  1278. struct ibmvnic_adapter *adapter = netdev_priv(netdev);
  1279. return adapter->msg_enable;
  1280. }
  1281. static void ibmvnic_set_msglevel(struct net_device *netdev, u32 data)
  1282. {
  1283. struct ibmvnic_adapter *adapter = netdev_priv(netdev);
  1284. adapter->msg_enable = data;
  1285. }
  1286. static u32 ibmvnic_get_link(struct net_device *netdev)
  1287. {
  1288. struct ibmvnic_adapter *adapter = netdev_priv(netdev);
  1289. /* Don't need to send a query because we request a logical link up at
  1290. * init and then we wait for link state indications
  1291. */
  1292. return adapter->logical_link_state;
  1293. }
  1294. static void ibmvnic_get_ringparam(struct net_device *netdev,
  1295. struct ethtool_ringparam *ring)
  1296. {
  1297. ring->rx_max_pending = 0;
  1298. ring->tx_max_pending = 0;
  1299. ring->rx_mini_max_pending = 0;
  1300. ring->rx_jumbo_max_pending = 0;
  1301. ring->rx_pending = 0;
  1302. ring->tx_pending = 0;
  1303. ring->rx_mini_pending = 0;
  1304. ring->rx_jumbo_pending = 0;
  1305. }
  1306. static void ibmvnic_get_strings(struct net_device *dev, u32 stringset, u8 *data)
  1307. {
  1308. int i;
  1309. if (stringset != ETH_SS_STATS)
  1310. return;
  1311. for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++, data += ETH_GSTRING_LEN)
  1312. memcpy(data, ibmvnic_stats[i].name, ETH_GSTRING_LEN);
  1313. }
  1314. static int ibmvnic_get_sset_count(struct net_device *dev, int sset)
  1315. {
  1316. switch (sset) {
  1317. case ETH_SS_STATS:
  1318. return ARRAY_SIZE(ibmvnic_stats);
  1319. default:
  1320. return -EOPNOTSUPP;
  1321. }
  1322. }
  1323. static void ibmvnic_get_ethtool_stats(struct net_device *dev,
  1324. struct ethtool_stats *stats, u64 *data)
  1325. {
  1326. struct ibmvnic_adapter *adapter = netdev_priv(dev);
  1327. union ibmvnic_crq crq;
  1328. int i;
  1329. memset(&crq, 0, sizeof(crq));
  1330. crq.request_statistics.first = IBMVNIC_CRQ_CMD;
  1331. crq.request_statistics.cmd = REQUEST_STATISTICS;
  1332. crq.request_statistics.ioba = cpu_to_be32(adapter->stats_token);
  1333. crq.request_statistics.len =
  1334. cpu_to_be32(sizeof(struct ibmvnic_statistics));
  1335. /* Wait for data to be written */
  1336. init_completion(&adapter->stats_done);
  1337. ibmvnic_send_crq(adapter, &crq);
  1338. wait_for_completion(&adapter->stats_done);
  1339. for (i = 0; i < ARRAY_SIZE(ibmvnic_stats); i++)
  1340. data[i] = IBMVNIC_GET_STAT(adapter, ibmvnic_stats[i].offset);
  1341. }
  1342. static const struct ethtool_ops ibmvnic_ethtool_ops = {
  1343. .get_drvinfo = ibmvnic_get_drvinfo,
  1344. .get_msglevel = ibmvnic_get_msglevel,
  1345. .set_msglevel = ibmvnic_set_msglevel,
  1346. .get_link = ibmvnic_get_link,
  1347. .get_ringparam = ibmvnic_get_ringparam,
  1348. .get_strings = ibmvnic_get_strings,
  1349. .get_sset_count = ibmvnic_get_sset_count,
  1350. .get_ethtool_stats = ibmvnic_get_ethtool_stats,
  1351. .get_link_ksettings = ibmvnic_get_link_ksettings,
  1352. };
  1353. /* Routines for managing CRQs/sCRQs */
  1354. static void release_sub_crq_queue(struct ibmvnic_adapter *adapter,
  1355. struct ibmvnic_sub_crq_queue *scrq)
  1356. {
  1357. struct device *dev = &adapter->vdev->dev;
  1358. long rc;
  1359. netdev_dbg(adapter->netdev, "Releasing sub-CRQ\n");
  1360. /* Close the sub-crqs */
  1361. do {
  1362. rc = plpar_hcall_norets(H_FREE_SUB_CRQ,
  1363. adapter->vdev->unit_address,
  1364. scrq->crq_num);
  1365. } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
  1366. if (rc) {
  1367. netdev_err(adapter->netdev,
  1368. "Failed to release sub-CRQ %16lx, rc = %ld\n",
  1369. scrq->crq_num, rc);
  1370. }
  1371. dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
  1372. DMA_BIDIRECTIONAL);
  1373. free_pages((unsigned long)scrq->msgs, 2);
  1374. kfree(scrq);
  1375. }
  1376. static struct ibmvnic_sub_crq_queue *init_sub_crq_queue(struct ibmvnic_adapter
  1377. *adapter)
  1378. {
  1379. struct device *dev = &adapter->vdev->dev;
  1380. struct ibmvnic_sub_crq_queue *scrq;
  1381. int rc;
  1382. scrq = kzalloc(sizeof(*scrq), GFP_KERNEL);
  1383. if (!scrq)
  1384. return NULL;
  1385. scrq->msgs =
  1386. (union sub_crq *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, 2);
  1387. if (!scrq->msgs) {
  1388. dev_warn(dev, "Couldn't allocate crq queue messages page\n");
  1389. goto zero_page_failed;
  1390. }
  1391. scrq->msg_token = dma_map_single(dev, scrq->msgs, 4 * PAGE_SIZE,
  1392. DMA_BIDIRECTIONAL);
  1393. if (dma_mapping_error(dev, scrq->msg_token)) {
  1394. dev_warn(dev, "Couldn't map crq queue messages page\n");
  1395. goto map_failed;
  1396. }
  1397. rc = h_reg_sub_crq(adapter->vdev->unit_address, scrq->msg_token,
  1398. 4 * PAGE_SIZE, &scrq->crq_num, &scrq->hw_irq);
  1399. if (rc == H_RESOURCE)
  1400. rc = ibmvnic_reset_crq(adapter);
  1401. if (rc == H_CLOSED) {
  1402. dev_warn(dev, "Partner adapter not ready, waiting.\n");
  1403. } else if (rc) {
  1404. dev_warn(dev, "Error %d registering sub-crq\n", rc);
  1405. goto reg_failed;
  1406. }
  1407. scrq->adapter = adapter;
  1408. scrq->size = 4 * PAGE_SIZE / sizeof(*scrq->msgs);
  1409. spin_lock_init(&scrq->lock);
  1410. netdev_dbg(adapter->netdev,
  1411. "sub-crq initialized, num %lx, hw_irq=%lx, irq=%x\n",
  1412. scrq->crq_num, scrq->hw_irq, scrq->irq);
  1413. return scrq;
  1414. reg_failed:
  1415. dma_unmap_single(dev, scrq->msg_token, 4 * PAGE_SIZE,
  1416. DMA_BIDIRECTIONAL);
  1417. map_failed:
  1418. free_pages((unsigned long)scrq->msgs, 2);
  1419. zero_page_failed:
  1420. kfree(scrq);
  1421. return NULL;
  1422. }
  1423. static void release_sub_crqs(struct ibmvnic_adapter *adapter)
  1424. {
  1425. int i;
  1426. if (adapter->tx_scrq) {
  1427. for (i = 0; i < adapter->req_tx_queues; i++) {
  1428. if (!adapter->tx_scrq[i])
  1429. continue;
  1430. if (adapter->tx_scrq[i]->irq) {
  1431. free_irq(adapter->tx_scrq[i]->irq,
  1432. adapter->tx_scrq[i]);
  1433. irq_dispose_mapping(adapter->tx_scrq[i]->irq);
  1434. adapter->tx_scrq[i]->irq = 0;
  1435. }
  1436. release_sub_crq_queue(adapter, adapter->tx_scrq[i]);
  1437. }
  1438. kfree(adapter->tx_scrq);
  1439. adapter->tx_scrq = NULL;
  1440. }
  1441. if (adapter->rx_scrq) {
  1442. for (i = 0; i < adapter->req_rx_queues; i++) {
  1443. if (!adapter->rx_scrq[i])
  1444. continue;
  1445. if (adapter->rx_scrq[i]->irq) {
  1446. free_irq(adapter->rx_scrq[i]->irq,
  1447. adapter->rx_scrq[i]);
  1448. irq_dispose_mapping(adapter->rx_scrq[i]->irq);
  1449. adapter->rx_scrq[i]->irq = 0;
  1450. }
  1451. release_sub_crq_queue(adapter, adapter->rx_scrq[i]);
  1452. }
  1453. kfree(adapter->rx_scrq);
  1454. adapter->rx_scrq = NULL;
  1455. }
  1456. }
  1457. static int disable_scrq_irq(struct ibmvnic_adapter *adapter,
  1458. struct ibmvnic_sub_crq_queue *scrq)
  1459. {
  1460. struct device *dev = &adapter->vdev->dev;
  1461. unsigned long rc;
  1462. rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
  1463. H_DISABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
  1464. if (rc)
  1465. dev_err(dev, "Couldn't disable scrq irq 0x%lx. rc=%ld\n",
  1466. scrq->hw_irq, rc);
  1467. return rc;
  1468. }
  1469. static int enable_scrq_irq(struct ibmvnic_adapter *adapter,
  1470. struct ibmvnic_sub_crq_queue *scrq)
  1471. {
  1472. struct device *dev = &adapter->vdev->dev;
  1473. unsigned long rc;
  1474. if (scrq->hw_irq > 0x100000000ULL) {
  1475. dev_err(dev, "bad hw_irq = %lx\n", scrq->hw_irq);
  1476. return 1;
  1477. }
  1478. rc = plpar_hcall_norets(H_VIOCTL, adapter->vdev->unit_address,
  1479. H_ENABLE_VIO_INTERRUPT, scrq->hw_irq, 0, 0);
  1480. if (rc)
  1481. dev_err(dev, "Couldn't enable scrq irq 0x%lx. rc=%ld\n",
  1482. scrq->hw_irq, rc);
  1483. return rc;
  1484. }
  1485. static int ibmvnic_complete_tx(struct ibmvnic_adapter *adapter,
  1486. struct ibmvnic_sub_crq_queue *scrq)
  1487. {
  1488. struct device *dev = &adapter->vdev->dev;
  1489. struct ibmvnic_tx_buff *txbuff;
  1490. union sub_crq *next;
  1491. int index;
  1492. int i, j;
  1493. u8 first;
  1494. restart_loop:
  1495. while (pending_scrq(adapter, scrq)) {
  1496. unsigned int pool = scrq->pool_index;
  1497. next = ibmvnic_next_scrq(adapter, scrq);
  1498. for (i = 0; i < next->tx_comp.num_comps; i++) {
  1499. if (next->tx_comp.rcs[i]) {
  1500. dev_err(dev, "tx error %x\n",
  1501. next->tx_comp.rcs[i]);
  1502. continue;
  1503. }
  1504. index = be32_to_cpu(next->tx_comp.correlators[i]);
  1505. txbuff = &adapter->tx_pool[pool].tx_buff[index];
  1506. for (j = 0; j < IBMVNIC_MAX_FRAGS_PER_CRQ; j++) {
  1507. if (!txbuff->data_dma[j])
  1508. continue;
  1509. txbuff->data_dma[j] = 0;
  1510. }
  1511. /* if sub_crq was sent indirectly */
  1512. first = txbuff->indir_arr[0].generic.first;
  1513. if (first == IBMVNIC_CRQ_CMD) {
  1514. dma_unmap_single(dev, txbuff->indir_dma,
  1515. sizeof(txbuff->indir_arr),
  1516. DMA_TO_DEVICE);
  1517. }
  1518. if (txbuff->last_frag) {
  1519. if (atomic_sub_return(next->tx_comp.num_comps,
  1520. &scrq->used) <=
  1521. (adapter->req_tx_entries_per_subcrq / 2) &&
  1522. netif_subqueue_stopped(adapter->netdev,
  1523. txbuff->skb)) {
  1524. netif_wake_subqueue(adapter->netdev,
  1525. scrq->pool_index);
  1526. netdev_dbg(adapter->netdev,
  1527. "Started queue %d\n",
  1528. scrq->pool_index);
  1529. }
  1530. dev_kfree_skb_any(txbuff->skb);
  1531. }
  1532. adapter->tx_pool[pool].free_map[adapter->tx_pool[pool].
  1533. producer_index] = index;
  1534. adapter->tx_pool[pool].producer_index =
  1535. (adapter->tx_pool[pool].producer_index + 1) %
  1536. adapter->req_tx_entries_per_subcrq;
  1537. }
  1538. /* remove tx_comp scrq*/
  1539. next->tx_comp.first = 0;
  1540. }
  1541. enable_scrq_irq(adapter, scrq);
  1542. if (pending_scrq(adapter, scrq)) {
  1543. disable_scrq_irq(adapter, scrq);
  1544. goto restart_loop;
  1545. }
  1546. return 0;
  1547. }
  1548. static irqreturn_t ibmvnic_interrupt_tx(int irq, void *instance)
  1549. {
  1550. struct ibmvnic_sub_crq_queue *scrq = instance;
  1551. struct ibmvnic_adapter *adapter = scrq->adapter;
  1552. disable_scrq_irq(adapter, scrq);
  1553. ibmvnic_complete_tx(adapter, scrq);
  1554. return IRQ_HANDLED;
  1555. }
  1556. static irqreturn_t ibmvnic_interrupt_rx(int irq, void *instance)
  1557. {
  1558. struct ibmvnic_sub_crq_queue *scrq = instance;
  1559. struct ibmvnic_adapter *adapter = scrq->adapter;
  1560. if (napi_schedule_prep(&adapter->napi[scrq->scrq_num])) {
  1561. disable_scrq_irq(adapter, scrq);
  1562. __napi_schedule(&adapter->napi[scrq->scrq_num]);
  1563. }
  1564. return IRQ_HANDLED;
  1565. }
  1566. static int init_sub_crq_irqs(struct ibmvnic_adapter *adapter)
  1567. {
  1568. struct device *dev = &adapter->vdev->dev;
  1569. struct ibmvnic_sub_crq_queue *scrq;
  1570. int i = 0, j = 0;
  1571. int rc = 0;
  1572. for (i = 0; i < adapter->req_tx_queues; i++) {
  1573. scrq = adapter->tx_scrq[i];
  1574. scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
  1575. if (!scrq->irq) {
  1576. rc = -EINVAL;
  1577. dev_err(dev, "Error mapping irq\n");
  1578. goto req_tx_irq_failed;
  1579. }
  1580. rc = request_irq(scrq->irq, ibmvnic_interrupt_tx,
  1581. 0, "ibmvnic_tx", scrq);
  1582. if (rc) {
  1583. dev_err(dev, "Couldn't register tx irq 0x%x. rc=%d\n",
  1584. scrq->irq, rc);
  1585. irq_dispose_mapping(scrq->irq);
  1586. goto req_rx_irq_failed;
  1587. }
  1588. }
  1589. for (i = 0; i < adapter->req_rx_queues; i++) {
  1590. scrq = adapter->rx_scrq[i];
  1591. scrq->irq = irq_create_mapping(NULL, scrq->hw_irq);
  1592. if (!scrq->irq) {
  1593. rc = -EINVAL;
  1594. dev_err(dev, "Error mapping irq\n");
  1595. goto req_rx_irq_failed;
  1596. }
  1597. rc = request_irq(scrq->irq, ibmvnic_interrupt_rx,
  1598. 0, "ibmvnic_rx", scrq);
  1599. if (rc) {
  1600. dev_err(dev, "Couldn't register rx irq 0x%x. rc=%d\n",
  1601. scrq->irq, rc);
  1602. irq_dispose_mapping(scrq->irq);
  1603. goto req_rx_irq_failed;
  1604. }
  1605. }
  1606. return rc;
  1607. req_rx_irq_failed:
  1608. for (j = 0; j < i; j++) {
  1609. free_irq(adapter->rx_scrq[j]->irq, adapter->rx_scrq[j]);
  1610. irq_dispose_mapping(adapter->rx_scrq[j]->irq);
  1611. }
  1612. i = adapter->req_tx_queues;
  1613. req_tx_irq_failed:
  1614. for (j = 0; j < i; j++) {
  1615. free_irq(adapter->tx_scrq[j]->irq, adapter->tx_scrq[j]);
  1616. irq_dispose_mapping(adapter->rx_scrq[j]->irq);
  1617. }
  1618. release_sub_crqs(adapter);
  1619. return rc;
  1620. }
  1621. static int init_sub_crqs(struct ibmvnic_adapter *adapter)
  1622. {
  1623. struct device *dev = &adapter->vdev->dev;
  1624. struct ibmvnic_sub_crq_queue **allqueues;
  1625. int registered_queues = 0;
  1626. int total_queues;
  1627. int more = 0;
  1628. int i;
  1629. total_queues = adapter->req_tx_queues + adapter->req_rx_queues;
  1630. allqueues = kcalloc(total_queues, sizeof(*allqueues), GFP_KERNEL);
  1631. if (!allqueues)
  1632. return -1;
  1633. for (i = 0; i < total_queues; i++) {
  1634. allqueues[i] = init_sub_crq_queue(adapter);
  1635. if (!allqueues[i]) {
  1636. dev_warn(dev, "Couldn't allocate all sub-crqs\n");
  1637. break;
  1638. }
  1639. registered_queues++;
  1640. }
  1641. /* Make sure we were able to register the minimum number of queues */
  1642. if (registered_queues <
  1643. adapter->min_tx_queues + adapter->min_rx_queues) {
  1644. dev_err(dev, "Fatal: Couldn't init min number of sub-crqs\n");
  1645. goto tx_failed;
  1646. }
  1647. /* Distribute the failed allocated queues*/
  1648. for (i = 0; i < total_queues - registered_queues + more ; i++) {
  1649. netdev_dbg(adapter->netdev, "Reducing number of queues\n");
  1650. switch (i % 3) {
  1651. case 0:
  1652. if (adapter->req_rx_queues > adapter->min_rx_queues)
  1653. adapter->req_rx_queues--;
  1654. else
  1655. more++;
  1656. break;
  1657. case 1:
  1658. if (adapter->req_tx_queues > adapter->min_tx_queues)
  1659. adapter->req_tx_queues--;
  1660. else
  1661. more++;
  1662. break;
  1663. }
  1664. }
  1665. adapter->tx_scrq = kcalloc(adapter->req_tx_queues,
  1666. sizeof(*adapter->tx_scrq), GFP_KERNEL);
  1667. if (!adapter->tx_scrq)
  1668. goto tx_failed;
  1669. for (i = 0; i < adapter->req_tx_queues; i++) {
  1670. adapter->tx_scrq[i] = allqueues[i];
  1671. adapter->tx_scrq[i]->pool_index = i;
  1672. }
  1673. adapter->rx_scrq = kcalloc(adapter->req_rx_queues,
  1674. sizeof(*adapter->rx_scrq), GFP_KERNEL);
  1675. if (!adapter->rx_scrq)
  1676. goto rx_failed;
  1677. for (i = 0; i < adapter->req_rx_queues; i++) {
  1678. adapter->rx_scrq[i] = allqueues[i + adapter->req_tx_queues];
  1679. adapter->rx_scrq[i]->scrq_num = i;
  1680. }
  1681. kfree(allqueues);
  1682. return 0;
  1683. rx_failed:
  1684. kfree(adapter->tx_scrq);
  1685. adapter->tx_scrq = NULL;
  1686. tx_failed:
  1687. for (i = 0; i < registered_queues; i++)
  1688. release_sub_crq_queue(adapter, allqueues[i]);
  1689. kfree(allqueues);
  1690. return -1;
  1691. }
  1692. static void ibmvnic_send_req_caps(struct ibmvnic_adapter *adapter, int retry)
  1693. {
  1694. struct device *dev = &adapter->vdev->dev;
  1695. union ibmvnic_crq crq;
  1696. if (!retry) {
  1697. /* Sub-CRQ entries are 32 byte long */
  1698. int entries_page = 4 * PAGE_SIZE / (sizeof(u64) * 4);
  1699. if (adapter->min_tx_entries_per_subcrq > entries_page ||
  1700. adapter->min_rx_add_entries_per_subcrq > entries_page) {
  1701. dev_err(dev, "Fatal, invalid entries per sub-crq\n");
  1702. return;
  1703. }
  1704. /* Get the minimum between the queried max and the entries
  1705. * that fit in our PAGE_SIZE
  1706. */
  1707. adapter->req_tx_entries_per_subcrq =
  1708. adapter->max_tx_entries_per_subcrq > entries_page ?
  1709. entries_page : adapter->max_tx_entries_per_subcrq;
  1710. adapter->req_rx_add_entries_per_subcrq =
  1711. adapter->max_rx_add_entries_per_subcrq > entries_page ?
  1712. entries_page : adapter->max_rx_add_entries_per_subcrq;
  1713. adapter->req_tx_queues = adapter->opt_tx_comp_sub_queues;
  1714. adapter->req_rx_queues = adapter->opt_rx_comp_queues;
  1715. adapter->req_rx_add_queues = adapter->max_rx_add_queues;
  1716. adapter->req_mtu = adapter->netdev->mtu + ETH_HLEN;
  1717. }
  1718. memset(&crq, 0, sizeof(crq));
  1719. crq.request_capability.first = IBMVNIC_CRQ_CMD;
  1720. crq.request_capability.cmd = REQUEST_CAPABILITY;
  1721. crq.request_capability.capability = cpu_to_be16(REQ_TX_QUEUES);
  1722. crq.request_capability.number = cpu_to_be64(adapter->req_tx_queues);
  1723. atomic_inc(&adapter->running_cap_crqs);
  1724. ibmvnic_send_crq(adapter, &crq);
  1725. crq.request_capability.capability = cpu_to_be16(REQ_RX_QUEUES);
  1726. crq.request_capability.number = cpu_to_be64(adapter->req_rx_queues);
  1727. atomic_inc(&adapter->running_cap_crqs);
  1728. ibmvnic_send_crq(adapter, &crq);
  1729. crq.request_capability.capability = cpu_to_be16(REQ_RX_ADD_QUEUES);
  1730. crq.request_capability.number = cpu_to_be64(adapter->req_rx_add_queues);
  1731. atomic_inc(&adapter->running_cap_crqs);
  1732. ibmvnic_send_crq(adapter, &crq);
  1733. crq.request_capability.capability =
  1734. cpu_to_be16(REQ_TX_ENTRIES_PER_SUBCRQ);
  1735. crq.request_capability.number =
  1736. cpu_to_be64(adapter->req_tx_entries_per_subcrq);
  1737. atomic_inc(&adapter->running_cap_crqs);
  1738. ibmvnic_send_crq(adapter, &crq);
  1739. crq.request_capability.capability =
  1740. cpu_to_be16(REQ_RX_ADD_ENTRIES_PER_SUBCRQ);
  1741. crq.request_capability.number =
  1742. cpu_to_be64(adapter->req_rx_add_entries_per_subcrq);
  1743. atomic_inc(&adapter->running_cap_crqs);
  1744. ibmvnic_send_crq(adapter, &crq);
  1745. crq.request_capability.capability = cpu_to_be16(REQ_MTU);
  1746. crq.request_capability.number = cpu_to_be64(adapter->req_mtu);
  1747. atomic_inc(&adapter->running_cap_crqs);
  1748. ibmvnic_send_crq(adapter, &crq);
  1749. if (adapter->netdev->flags & IFF_PROMISC) {
  1750. if (adapter->promisc_supported) {
  1751. crq.request_capability.capability =
  1752. cpu_to_be16(PROMISC_REQUESTED);
  1753. crq.request_capability.number = cpu_to_be64(1);
  1754. atomic_inc(&adapter->running_cap_crqs);
  1755. ibmvnic_send_crq(adapter, &crq);
  1756. }
  1757. } else {
  1758. crq.request_capability.capability =
  1759. cpu_to_be16(PROMISC_REQUESTED);
  1760. crq.request_capability.number = cpu_to_be64(0);
  1761. atomic_inc(&adapter->running_cap_crqs);
  1762. ibmvnic_send_crq(adapter, &crq);
  1763. }
  1764. }
  1765. static int pending_scrq(struct ibmvnic_adapter *adapter,
  1766. struct ibmvnic_sub_crq_queue *scrq)
  1767. {
  1768. union sub_crq *entry = &scrq->msgs[scrq->cur];
  1769. if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP ||
  1770. adapter->state == VNIC_CLOSING)
  1771. return 1;
  1772. else
  1773. return 0;
  1774. }
  1775. static union sub_crq *ibmvnic_next_scrq(struct ibmvnic_adapter *adapter,
  1776. struct ibmvnic_sub_crq_queue *scrq)
  1777. {
  1778. union sub_crq *entry;
  1779. unsigned long flags;
  1780. spin_lock_irqsave(&scrq->lock, flags);
  1781. entry = &scrq->msgs[scrq->cur];
  1782. if (entry->generic.first & IBMVNIC_CRQ_CMD_RSP) {
  1783. if (++scrq->cur == scrq->size)
  1784. scrq->cur = 0;
  1785. } else {
  1786. entry = NULL;
  1787. }
  1788. spin_unlock_irqrestore(&scrq->lock, flags);
  1789. return entry;
  1790. }
  1791. static union ibmvnic_crq *ibmvnic_next_crq(struct ibmvnic_adapter *adapter)
  1792. {
  1793. struct ibmvnic_crq_queue *queue = &adapter->crq;
  1794. union ibmvnic_crq *crq;
  1795. crq = &queue->msgs[queue->cur];
  1796. if (crq->generic.first & IBMVNIC_CRQ_CMD_RSP) {
  1797. if (++queue->cur == queue->size)
  1798. queue->cur = 0;
  1799. } else {
  1800. crq = NULL;
  1801. }
  1802. return crq;
  1803. }
  1804. static int send_subcrq(struct ibmvnic_adapter *adapter, u64 remote_handle,
  1805. union sub_crq *sub_crq)
  1806. {
  1807. unsigned int ua = adapter->vdev->unit_address;
  1808. struct device *dev = &adapter->vdev->dev;
  1809. u64 *u64_crq = (u64 *)sub_crq;
  1810. int rc;
  1811. netdev_dbg(adapter->netdev,
  1812. "Sending sCRQ %016lx: %016lx %016lx %016lx %016lx\n",
  1813. (unsigned long int)cpu_to_be64(remote_handle),
  1814. (unsigned long int)cpu_to_be64(u64_crq[0]),
  1815. (unsigned long int)cpu_to_be64(u64_crq[1]),
  1816. (unsigned long int)cpu_to_be64(u64_crq[2]),
  1817. (unsigned long int)cpu_to_be64(u64_crq[3]));
  1818. /* Make sure the hypervisor sees the complete request */
  1819. mb();
  1820. rc = plpar_hcall_norets(H_SEND_SUB_CRQ, ua,
  1821. cpu_to_be64(remote_handle),
  1822. cpu_to_be64(u64_crq[0]),
  1823. cpu_to_be64(u64_crq[1]),
  1824. cpu_to_be64(u64_crq[2]),
  1825. cpu_to_be64(u64_crq[3]));
  1826. if (rc) {
  1827. if (rc == H_CLOSED)
  1828. dev_warn(dev, "CRQ Queue closed\n");
  1829. dev_err(dev, "Send error (rc=%d)\n", rc);
  1830. }
  1831. return rc;
  1832. }
  1833. static int send_subcrq_indirect(struct ibmvnic_adapter *adapter,
  1834. u64 remote_handle, u64 ioba, u64 num_entries)
  1835. {
  1836. unsigned int ua = adapter->vdev->unit_address;
  1837. struct device *dev = &adapter->vdev->dev;
  1838. int rc;
  1839. /* Make sure the hypervisor sees the complete request */
  1840. mb();
  1841. rc = plpar_hcall_norets(H_SEND_SUB_CRQ_INDIRECT, ua,
  1842. cpu_to_be64(remote_handle),
  1843. ioba, num_entries);
  1844. if (rc) {
  1845. if (rc == H_CLOSED)
  1846. dev_warn(dev, "CRQ Queue closed\n");
  1847. dev_err(dev, "Send (indirect) error (rc=%d)\n", rc);
  1848. }
  1849. return rc;
  1850. }
  1851. static int ibmvnic_send_crq(struct ibmvnic_adapter *adapter,
  1852. union ibmvnic_crq *crq)
  1853. {
  1854. unsigned int ua = adapter->vdev->unit_address;
  1855. struct device *dev = &adapter->vdev->dev;
  1856. u64 *u64_crq = (u64 *)crq;
  1857. int rc;
  1858. netdev_dbg(adapter->netdev, "Sending CRQ: %016lx %016lx\n",
  1859. (unsigned long int)cpu_to_be64(u64_crq[0]),
  1860. (unsigned long int)cpu_to_be64(u64_crq[1]));
  1861. /* Make sure the hypervisor sees the complete request */
  1862. mb();
  1863. rc = plpar_hcall_norets(H_SEND_CRQ, ua,
  1864. cpu_to_be64(u64_crq[0]),
  1865. cpu_to_be64(u64_crq[1]));
  1866. if (rc) {
  1867. if (rc == H_CLOSED)
  1868. dev_warn(dev, "CRQ Queue closed\n");
  1869. dev_warn(dev, "Send error (rc=%d)\n", rc);
  1870. }
  1871. return rc;
  1872. }
  1873. static int ibmvnic_send_crq_init(struct ibmvnic_adapter *adapter)
  1874. {
  1875. union ibmvnic_crq crq;
  1876. memset(&crq, 0, sizeof(crq));
  1877. crq.generic.first = IBMVNIC_CRQ_INIT_CMD;
  1878. crq.generic.cmd = IBMVNIC_CRQ_INIT;
  1879. netdev_dbg(adapter->netdev, "Sending CRQ init\n");
  1880. return ibmvnic_send_crq(adapter, &crq);
  1881. }
  1882. static int send_version_xchg(struct ibmvnic_adapter *adapter)
  1883. {
  1884. union ibmvnic_crq crq;
  1885. memset(&crq, 0, sizeof(crq));
  1886. crq.version_exchange.first = IBMVNIC_CRQ_CMD;
  1887. crq.version_exchange.cmd = VERSION_EXCHANGE;
  1888. crq.version_exchange.version = cpu_to_be16(ibmvnic_version);
  1889. return ibmvnic_send_crq(adapter, &crq);
  1890. }
  1891. static void send_login(struct ibmvnic_adapter *adapter)
  1892. {
  1893. struct ibmvnic_login_rsp_buffer *login_rsp_buffer;
  1894. struct ibmvnic_login_buffer *login_buffer;
  1895. struct device *dev = &adapter->vdev->dev;
  1896. dma_addr_t rsp_buffer_token;
  1897. dma_addr_t buffer_token;
  1898. size_t rsp_buffer_size;
  1899. union ibmvnic_crq crq;
  1900. size_t buffer_size;
  1901. __be64 *tx_list_p;
  1902. __be64 *rx_list_p;
  1903. int i;
  1904. buffer_size =
  1905. sizeof(struct ibmvnic_login_buffer) +
  1906. sizeof(u64) * (adapter->req_tx_queues + adapter->req_rx_queues);
  1907. login_buffer = kmalloc(buffer_size, GFP_ATOMIC);
  1908. if (!login_buffer)
  1909. goto buf_alloc_failed;
  1910. buffer_token = dma_map_single(dev, login_buffer, buffer_size,
  1911. DMA_TO_DEVICE);
  1912. if (dma_mapping_error(dev, buffer_token)) {
  1913. dev_err(dev, "Couldn't map login buffer\n");
  1914. goto buf_map_failed;
  1915. }
  1916. rsp_buffer_size = sizeof(struct ibmvnic_login_rsp_buffer) +
  1917. sizeof(u64) * adapter->req_tx_queues +
  1918. sizeof(u64) * adapter->req_rx_queues +
  1919. sizeof(u64) * adapter->req_rx_queues +
  1920. sizeof(u8) * IBMVNIC_TX_DESC_VERSIONS;
  1921. login_rsp_buffer = kmalloc(rsp_buffer_size, GFP_ATOMIC);
  1922. if (!login_rsp_buffer)
  1923. goto buf_rsp_alloc_failed;
  1924. rsp_buffer_token = dma_map_single(dev, login_rsp_buffer,
  1925. rsp_buffer_size, DMA_FROM_DEVICE);
  1926. if (dma_mapping_error(dev, rsp_buffer_token)) {
  1927. dev_err(dev, "Couldn't map login rsp buffer\n");
  1928. goto buf_rsp_map_failed;
  1929. }
  1930. adapter->login_buf = login_buffer;
  1931. adapter->login_buf_token = buffer_token;
  1932. adapter->login_buf_sz = buffer_size;
  1933. adapter->login_rsp_buf = login_rsp_buffer;
  1934. adapter->login_rsp_buf_token = rsp_buffer_token;
  1935. adapter->login_rsp_buf_sz = rsp_buffer_size;
  1936. login_buffer->len = cpu_to_be32(buffer_size);
  1937. login_buffer->version = cpu_to_be32(INITIAL_VERSION_LB);
  1938. login_buffer->num_txcomp_subcrqs = cpu_to_be32(adapter->req_tx_queues);
  1939. login_buffer->off_txcomp_subcrqs =
  1940. cpu_to_be32(sizeof(struct ibmvnic_login_buffer));
  1941. login_buffer->num_rxcomp_subcrqs = cpu_to_be32(adapter->req_rx_queues);
  1942. login_buffer->off_rxcomp_subcrqs =
  1943. cpu_to_be32(sizeof(struct ibmvnic_login_buffer) +
  1944. sizeof(u64) * adapter->req_tx_queues);
  1945. login_buffer->login_rsp_ioba = cpu_to_be32(rsp_buffer_token);
  1946. login_buffer->login_rsp_len = cpu_to_be32(rsp_buffer_size);
  1947. tx_list_p = (__be64 *)((char *)login_buffer +
  1948. sizeof(struct ibmvnic_login_buffer));
  1949. rx_list_p = (__be64 *)((char *)login_buffer +
  1950. sizeof(struct ibmvnic_login_buffer) +
  1951. sizeof(u64) * adapter->req_tx_queues);
  1952. for (i = 0; i < adapter->req_tx_queues; i++) {
  1953. if (adapter->tx_scrq[i]) {
  1954. tx_list_p[i] = cpu_to_be64(adapter->tx_scrq[i]->
  1955. crq_num);
  1956. }
  1957. }
  1958. for (i = 0; i < adapter->req_rx_queues; i++) {
  1959. if (adapter->rx_scrq[i]) {
  1960. rx_list_p[i] = cpu_to_be64(adapter->rx_scrq[i]->
  1961. crq_num);
  1962. }
  1963. }
  1964. netdev_dbg(adapter->netdev, "Login Buffer:\n");
  1965. for (i = 0; i < (adapter->login_buf_sz - 1) / 8 + 1; i++) {
  1966. netdev_dbg(adapter->netdev, "%016lx\n",
  1967. ((unsigned long int *)(adapter->login_buf))[i]);
  1968. }
  1969. memset(&crq, 0, sizeof(crq));
  1970. crq.login.first = IBMVNIC_CRQ_CMD;
  1971. crq.login.cmd = LOGIN;
  1972. crq.login.ioba = cpu_to_be32(buffer_token);
  1973. crq.login.len = cpu_to_be32(buffer_size);
  1974. ibmvnic_send_crq(adapter, &crq);
  1975. return;
  1976. buf_rsp_map_failed:
  1977. kfree(login_rsp_buffer);
  1978. buf_rsp_alloc_failed:
  1979. dma_unmap_single(dev, buffer_token, buffer_size, DMA_TO_DEVICE);
  1980. buf_map_failed:
  1981. kfree(login_buffer);
  1982. buf_alloc_failed:
  1983. return;
  1984. }
  1985. static void send_request_map(struct ibmvnic_adapter *adapter, dma_addr_t addr,
  1986. u32 len, u8 map_id)
  1987. {
  1988. union ibmvnic_crq crq;
  1989. memset(&crq, 0, sizeof(crq));
  1990. crq.request_map.first = IBMVNIC_CRQ_CMD;
  1991. crq.request_map.cmd = REQUEST_MAP;
  1992. crq.request_map.map_id = map_id;
  1993. crq.request_map.ioba = cpu_to_be32(addr);
  1994. crq.request_map.len = cpu_to_be32(len);
  1995. ibmvnic_send_crq(adapter, &crq);
  1996. }
  1997. static void send_request_unmap(struct ibmvnic_adapter *adapter, u8 map_id)
  1998. {
  1999. union ibmvnic_crq crq;
  2000. memset(&crq, 0, sizeof(crq));
  2001. crq.request_unmap.first = IBMVNIC_CRQ_CMD;
  2002. crq.request_unmap.cmd = REQUEST_UNMAP;
  2003. crq.request_unmap.map_id = map_id;
  2004. ibmvnic_send_crq(adapter, &crq);
  2005. }
  2006. static void send_map_query(struct ibmvnic_adapter *adapter)
  2007. {
  2008. union ibmvnic_crq crq;
  2009. memset(&crq, 0, sizeof(crq));
  2010. crq.query_map.first = IBMVNIC_CRQ_CMD;
  2011. crq.query_map.cmd = QUERY_MAP;
  2012. ibmvnic_send_crq(adapter, &crq);
  2013. }
  2014. /* Send a series of CRQs requesting various capabilities of the VNIC server */
  2015. static void send_cap_queries(struct ibmvnic_adapter *adapter)
  2016. {
  2017. union ibmvnic_crq crq;
  2018. atomic_set(&adapter->running_cap_crqs, 0);
  2019. memset(&crq, 0, sizeof(crq));
  2020. crq.query_capability.first = IBMVNIC_CRQ_CMD;
  2021. crq.query_capability.cmd = QUERY_CAPABILITY;
  2022. crq.query_capability.capability = cpu_to_be16(MIN_TX_QUEUES);
  2023. atomic_inc(&adapter->running_cap_crqs);
  2024. ibmvnic_send_crq(adapter, &crq);
  2025. crq.query_capability.capability = cpu_to_be16(MIN_RX_QUEUES);
  2026. atomic_inc(&adapter->running_cap_crqs);
  2027. ibmvnic_send_crq(adapter, &crq);
  2028. crq.query_capability.capability = cpu_to_be16(MIN_RX_ADD_QUEUES);
  2029. atomic_inc(&adapter->running_cap_crqs);
  2030. ibmvnic_send_crq(adapter, &crq);
  2031. crq.query_capability.capability = cpu_to_be16(MAX_TX_QUEUES);
  2032. atomic_inc(&adapter->running_cap_crqs);
  2033. ibmvnic_send_crq(adapter, &crq);
  2034. crq.query_capability.capability = cpu_to_be16(MAX_RX_QUEUES);
  2035. atomic_inc(&adapter->running_cap_crqs);
  2036. ibmvnic_send_crq(adapter, &crq);
  2037. crq.query_capability.capability = cpu_to_be16(MAX_RX_ADD_QUEUES);
  2038. atomic_inc(&adapter->running_cap_crqs);
  2039. ibmvnic_send_crq(adapter, &crq);
  2040. crq.query_capability.capability =
  2041. cpu_to_be16(MIN_TX_ENTRIES_PER_SUBCRQ);
  2042. atomic_inc(&adapter->running_cap_crqs);
  2043. ibmvnic_send_crq(adapter, &crq);
  2044. crq.query_capability.capability =
  2045. cpu_to_be16(MIN_RX_ADD_ENTRIES_PER_SUBCRQ);
  2046. atomic_inc(&adapter->running_cap_crqs);
  2047. ibmvnic_send_crq(adapter, &crq);
  2048. crq.query_capability.capability =
  2049. cpu_to_be16(MAX_TX_ENTRIES_PER_SUBCRQ);
  2050. atomic_inc(&adapter->running_cap_crqs);
  2051. ibmvnic_send_crq(adapter, &crq);
  2052. crq.query_capability.capability =
  2053. cpu_to_be16(MAX_RX_ADD_ENTRIES_PER_SUBCRQ);
  2054. atomic_inc(&adapter->running_cap_crqs);
  2055. ibmvnic_send_crq(adapter, &crq);
  2056. crq.query_capability.capability = cpu_to_be16(TCP_IP_OFFLOAD);
  2057. atomic_inc(&adapter->running_cap_crqs);
  2058. ibmvnic_send_crq(adapter, &crq);
  2059. crq.query_capability.capability = cpu_to_be16(PROMISC_SUPPORTED);
  2060. atomic_inc(&adapter->running_cap_crqs);
  2061. ibmvnic_send_crq(adapter, &crq);
  2062. crq.query_capability.capability = cpu_to_be16(MIN_MTU);
  2063. atomic_inc(&adapter->running_cap_crqs);
  2064. ibmvnic_send_crq(adapter, &crq);
  2065. crq.query_capability.capability = cpu_to_be16(MAX_MTU);
  2066. atomic_inc(&adapter->running_cap_crqs);
  2067. ibmvnic_send_crq(adapter, &crq);
  2068. crq.query_capability.capability = cpu_to_be16(MAX_MULTICAST_FILTERS);
  2069. atomic_inc(&adapter->running_cap_crqs);
  2070. ibmvnic_send_crq(adapter, &crq);
  2071. crq.query_capability.capability = cpu_to_be16(VLAN_HEADER_INSERTION);
  2072. atomic_inc(&adapter->running_cap_crqs);
  2073. ibmvnic_send_crq(adapter, &crq);
  2074. crq.query_capability.capability = cpu_to_be16(RX_VLAN_HEADER_INSERTION);
  2075. atomic_inc(&adapter->running_cap_crqs);
  2076. ibmvnic_send_crq(adapter, &crq);
  2077. crq.query_capability.capability = cpu_to_be16(MAX_TX_SG_ENTRIES);
  2078. atomic_inc(&adapter->running_cap_crqs);
  2079. ibmvnic_send_crq(adapter, &crq);
  2080. crq.query_capability.capability = cpu_to_be16(RX_SG_SUPPORTED);
  2081. atomic_inc(&adapter->running_cap_crqs);
  2082. ibmvnic_send_crq(adapter, &crq);
  2083. crq.query_capability.capability = cpu_to_be16(OPT_TX_COMP_SUB_QUEUES);
  2084. atomic_inc(&adapter->running_cap_crqs);
  2085. ibmvnic_send_crq(adapter, &crq);
  2086. crq.query_capability.capability = cpu_to_be16(OPT_RX_COMP_QUEUES);
  2087. atomic_inc(&adapter->running_cap_crqs);
  2088. ibmvnic_send_crq(adapter, &crq);
  2089. crq.query_capability.capability =
  2090. cpu_to_be16(OPT_RX_BUFADD_Q_PER_RX_COMP_Q);
  2091. atomic_inc(&adapter->running_cap_crqs);
  2092. ibmvnic_send_crq(adapter, &crq);
  2093. crq.query_capability.capability =
  2094. cpu_to_be16(OPT_TX_ENTRIES_PER_SUBCRQ);
  2095. atomic_inc(&adapter->running_cap_crqs);
  2096. ibmvnic_send_crq(adapter, &crq);
  2097. crq.query_capability.capability =
  2098. cpu_to_be16(OPT_RXBA_ENTRIES_PER_SUBCRQ);
  2099. atomic_inc(&adapter->running_cap_crqs);
  2100. ibmvnic_send_crq(adapter, &crq);
  2101. crq.query_capability.capability = cpu_to_be16(TX_RX_DESC_REQ);
  2102. atomic_inc(&adapter->running_cap_crqs);
  2103. ibmvnic_send_crq(adapter, &crq);
  2104. }
  2105. static void handle_query_ip_offload_rsp(struct ibmvnic_adapter *adapter)
  2106. {
  2107. struct device *dev = &adapter->vdev->dev;
  2108. struct ibmvnic_query_ip_offload_buffer *buf = &adapter->ip_offload_buf;
  2109. union ibmvnic_crq crq;
  2110. int i;
  2111. dma_unmap_single(dev, adapter->ip_offload_tok,
  2112. sizeof(adapter->ip_offload_buf), DMA_FROM_DEVICE);
  2113. netdev_dbg(adapter->netdev, "Query IP Offload Buffer:\n");
  2114. for (i = 0; i < (sizeof(adapter->ip_offload_buf) - 1) / 8 + 1; i++)
  2115. netdev_dbg(adapter->netdev, "%016lx\n",
  2116. ((unsigned long int *)(buf))[i]);
  2117. netdev_dbg(adapter->netdev, "ipv4_chksum = %d\n", buf->ipv4_chksum);
  2118. netdev_dbg(adapter->netdev, "ipv6_chksum = %d\n", buf->ipv6_chksum);
  2119. netdev_dbg(adapter->netdev, "tcp_ipv4_chksum = %d\n",
  2120. buf->tcp_ipv4_chksum);
  2121. netdev_dbg(adapter->netdev, "tcp_ipv6_chksum = %d\n",
  2122. buf->tcp_ipv6_chksum);
  2123. netdev_dbg(adapter->netdev, "udp_ipv4_chksum = %d\n",
  2124. buf->udp_ipv4_chksum);
  2125. netdev_dbg(adapter->netdev, "udp_ipv6_chksum = %d\n",
  2126. buf->udp_ipv6_chksum);
  2127. netdev_dbg(adapter->netdev, "large_tx_ipv4 = %d\n",
  2128. buf->large_tx_ipv4);
  2129. netdev_dbg(adapter->netdev, "large_tx_ipv6 = %d\n",
  2130. buf->large_tx_ipv6);
  2131. netdev_dbg(adapter->netdev, "large_rx_ipv4 = %d\n",
  2132. buf->large_rx_ipv4);
  2133. netdev_dbg(adapter->netdev, "large_rx_ipv6 = %d\n",
  2134. buf->large_rx_ipv6);
  2135. netdev_dbg(adapter->netdev, "max_ipv4_hdr_sz = %d\n",
  2136. buf->max_ipv4_header_size);
  2137. netdev_dbg(adapter->netdev, "max_ipv6_hdr_sz = %d\n",
  2138. buf->max_ipv6_header_size);
  2139. netdev_dbg(adapter->netdev, "max_tcp_hdr_size = %d\n",
  2140. buf->max_tcp_header_size);
  2141. netdev_dbg(adapter->netdev, "max_udp_hdr_size = %d\n",
  2142. buf->max_udp_header_size);
  2143. netdev_dbg(adapter->netdev, "max_large_tx_size = %d\n",
  2144. buf->max_large_tx_size);
  2145. netdev_dbg(adapter->netdev, "max_large_rx_size = %d\n",
  2146. buf->max_large_rx_size);
  2147. netdev_dbg(adapter->netdev, "ipv6_ext_hdr = %d\n",
  2148. buf->ipv6_extension_header);
  2149. netdev_dbg(adapter->netdev, "tcp_pseudosum_req = %d\n",
  2150. buf->tcp_pseudosum_req);
  2151. netdev_dbg(adapter->netdev, "num_ipv6_ext_hd = %d\n",
  2152. buf->num_ipv6_ext_headers);
  2153. netdev_dbg(adapter->netdev, "off_ipv6_ext_hd = %d\n",
  2154. buf->off_ipv6_ext_headers);
  2155. adapter->ip_offload_ctrl_tok =
  2156. dma_map_single(dev, &adapter->ip_offload_ctrl,
  2157. sizeof(adapter->ip_offload_ctrl), DMA_TO_DEVICE);
  2158. if (dma_mapping_error(dev, adapter->ip_offload_ctrl_tok)) {
  2159. dev_err(dev, "Couldn't map ip offload control buffer\n");
  2160. return;
  2161. }
  2162. adapter->ip_offload_ctrl.version = cpu_to_be32(INITIAL_VERSION_IOB);
  2163. adapter->ip_offload_ctrl.tcp_ipv4_chksum = buf->tcp_ipv4_chksum;
  2164. adapter->ip_offload_ctrl.udp_ipv4_chksum = buf->udp_ipv4_chksum;
  2165. adapter->ip_offload_ctrl.tcp_ipv6_chksum = buf->tcp_ipv6_chksum;
  2166. adapter->ip_offload_ctrl.udp_ipv6_chksum = buf->udp_ipv6_chksum;
  2167. /* large_tx/rx disabled for now, additional features needed */
  2168. adapter->ip_offload_ctrl.large_tx_ipv4 = 0;
  2169. adapter->ip_offload_ctrl.large_tx_ipv6 = 0;
  2170. adapter->ip_offload_ctrl.large_rx_ipv4 = 0;
  2171. adapter->ip_offload_ctrl.large_rx_ipv6 = 0;
  2172. adapter->netdev->features = NETIF_F_GSO;
  2173. if (buf->tcp_ipv4_chksum || buf->udp_ipv4_chksum)
  2174. adapter->netdev->features |= NETIF_F_IP_CSUM;
  2175. if (buf->tcp_ipv6_chksum || buf->udp_ipv6_chksum)
  2176. adapter->netdev->features |= NETIF_F_IPV6_CSUM;
  2177. if ((adapter->netdev->features &
  2178. (NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM)))
  2179. adapter->netdev->features |= NETIF_F_RXCSUM;
  2180. memset(&crq, 0, sizeof(crq));
  2181. crq.control_ip_offload.first = IBMVNIC_CRQ_CMD;
  2182. crq.control_ip_offload.cmd = CONTROL_IP_OFFLOAD;
  2183. crq.control_ip_offload.len =
  2184. cpu_to_be32(sizeof(adapter->ip_offload_ctrl));
  2185. crq.control_ip_offload.ioba = cpu_to_be32(adapter->ip_offload_ctrl_tok);
  2186. ibmvnic_send_crq(adapter, &crq);
  2187. }
  2188. static void handle_error_info_rsp(union ibmvnic_crq *crq,
  2189. struct ibmvnic_adapter *adapter)
  2190. {
  2191. struct device *dev = &adapter->vdev->dev;
  2192. struct ibmvnic_error_buff *error_buff, *tmp;
  2193. unsigned long flags;
  2194. bool found = false;
  2195. int i;
  2196. if (!crq->request_error_rsp.rc.code) {
  2197. dev_info(dev, "Request Error Rsp returned with rc=%x\n",
  2198. crq->request_error_rsp.rc.code);
  2199. return;
  2200. }
  2201. spin_lock_irqsave(&adapter->error_list_lock, flags);
  2202. list_for_each_entry_safe(error_buff, tmp, &adapter->errors, list)
  2203. if (error_buff->error_id == crq->request_error_rsp.error_id) {
  2204. found = true;
  2205. list_del(&error_buff->list);
  2206. break;
  2207. }
  2208. spin_unlock_irqrestore(&adapter->error_list_lock, flags);
  2209. if (!found) {
  2210. dev_err(dev, "Couldn't find error id %x\n",
  2211. be32_to_cpu(crq->request_error_rsp.error_id));
  2212. return;
  2213. }
  2214. dev_err(dev, "Detailed info for error id %x:",
  2215. be32_to_cpu(crq->request_error_rsp.error_id));
  2216. for (i = 0; i < error_buff->len; i++) {
  2217. pr_cont("%02x", (int)error_buff->buff[i]);
  2218. if (i % 8 == 7)
  2219. pr_cont(" ");
  2220. }
  2221. pr_cont("\n");
  2222. dma_unmap_single(dev, error_buff->dma, error_buff->len,
  2223. DMA_FROM_DEVICE);
  2224. kfree(error_buff->buff);
  2225. kfree(error_buff);
  2226. }
  2227. static void request_error_information(struct ibmvnic_adapter *adapter,
  2228. union ibmvnic_crq *err_crq)
  2229. {
  2230. struct device *dev = &adapter->vdev->dev;
  2231. struct net_device *netdev = adapter->netdev;
  2232. struct ibmvnic_error_buff *error_buff;
  2233. unsigned long timeout = msecs_to_jiffies(30000);
  2234. union ibmvnic_crq crq;
  2235. unsigned long flags;
  2236. int rc, detail_len;
  2237. error_buff = kmalloc(sizeof(*error_buff), GFP_ATOMIC);
  2238. if (!error_buff)
  2239. return;
  2240. detail_len = be32_to_cpu(err_crq->error_indication.detail_error_sz);
  2241. error_buff->buff = kmalloc(detail_len, GFP_ATOMIC);
  2242. if (!error_buff->buff) {
  2243. kfree(error_buff);
  2244. return;
  2245. }
  2246. error_buff->dma = dma_map_single(dev, error_buff->buff, detail_len,
  2247. DMA_FROM_DEVICE);
  2248. if (dma_mapping_error(dev, error_buff->dma)) {
  2249. netdev_err(netdev, "Couldn't map error buffer\n");
  2250. kfree(error_buff->buff);
  2251. kfree(error_buff);
  2252. return;
  2253. }
  2254. error_buff->len = detail_len;
  2255. error_buff->error_id = err_crq->error_indication.error_id;
  2256. spin_lock_irqsave(&adapter->error_list_lock, flags);
  2257. list_add_tail(&error_buff->list, &adapter->errors);
  2258. spin_unlock_irqrestore(&adapter->error_list_lock, flags);
  2259. memset(&crq, 0, sizeof(crq));
  2260. crq.request_error_info.first = IBMVNIC_CRQ_CMD;
  2261. crq.request_error_info.cmd = REQUEST_ERROR_INFO;
  2262. crq.request_error_info.ioba = cpu_to_be32(error_buff->dma);
  2263. crq.request_error_info.len = cpu_to_be32(detail_len);
  2264. crq.request_error_info.error_id = err_crq->error_indication.error_id;
  2265. rc = ibmvnic_send_crq(adapter, &crq);
  2266. if (rc) {
  2267. netdev_err(netdev, "failed to request error information\n");
  2268. goto err_info_fail;
  2269. }
  2270. if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
  2271. netdev_err(netdev, "timeout waiting for error information\n");
  2272. goto err_info_fail;
  2273. }
  2274. return;
  2275. err_info_fail:
  2276. spin_lock_irqsave(&adapter->error_list_lock, flags);
  2277. list_del(&error_buff->list);
  2278. spin_unlock_irqrestore(&adapter->error_list_lock, flags);
  2279. kfree(error_buff->buff);
  2280. kfree(error_buff);
  2281. }
  2282. static void handle_error_indication(union ibmvnic_crq *crq,
  2283. struct ibmvnic_adapter *adapter)
  2284. {
  2285. struct device *dev = &adapter->vdev->dev;
  2286. dev_err(dev, "Firmware reports %serror id %x, cause %d\n",
  2287. crq->error_indication.flags
  2288. & IBMVNIC_FATAL_ERROR ? "FATAL " : "",
  2289. be32_to_cpu(crq->error_indication.error_id),
  2290. be16_to_cpu(crq->error_indication.error_cause));
  2291. if (be32_to_cpu(crq->error_indication.error_id))
  2292. request_error_information(adapter, crq);
  2293. if (crq->error_indication.flags & IBMVNIC_FATAL_ERROR)
  2294. ibmvnic_reset(adapter, VNIC_RESET_FATAL);
  2295. }
  2296. static void handle_change_mac_rsp(union ibmvnic_crq *crq,
  2297. struct ibmvnic_adapter *adapter)
  2298. {
  2299. struct net_device *netdev = adapter->netdev;
  2300. struct device *dev = &adapter->vdev->dev;
  2301. long rc;
  2302. rc = crq->change_mac_addr_rsp.rc.code;
  2303. if (rc) {
  2304. dev_err(dev, "Error %ld in CHANGE_MAC_ADDR_RSP\n", rc);
  2305. return;
  2306. }
  2307. memcpy(netdev->dev_addr, &crq->change_mac_addr_rsp.mac_addr[0],
  2308. ETH_ALEN);
  2309. }
  2310. static void handle_request_cap_rsp(union ibmvnic_crq *crq,
  2311. struct ibmvnic_adapter *adapter)
  2312. {
  2313. struct device *dev = &adapter->vdev->dev;
  2314. u64 *req_value;
  2315. char *name;
  2316. atomic_dec(&adapter->running_cap_crqs);
  2317. switch (be16_to_cpu(crq->request_capability_rsp.capability)) {
  2318. case REQ_TX_QUEUES:
  2319. req_value = &adapter->req_tx_queues;
  2320. name = "tx";
  2321. break;
  2322. case REQ_RX_QUEUES:
  2323. req_value = &adapter->req_rx_queues;
  2324. name = "rx";
  2325. break;
  2326. case REQ_RX_ADD_QUEUES:
  2327. req_value = &adapter->req_rx_add_queues;
  2328. name = "rx_add";
  2329. break;
  2330. case REQ_TX_ENTRIES_PER_SUBCRQ:
  2331. req_value = &adapter->req_tx_entries_per_subcrq;
  2332. name = "tx_entries_per_subcrq";
  2333. break;
  2334. case REQ_RX_ADD_ENTRIES_PER_SUBCRQ:
  2335. req_value = &adapter->req_rx_add_entries_per_subcrq;
  2336. name = "rx_add_entries_per_subcrq";
  2337. break;
  2338. case REQ_MTU:
  2339. req_value = &adapter->req_mtu;
  2340. name = "mtu";
  2341. break;
  2342. case PROMISC_REQUESTED:
  2343. req_value = &adapter->promisc;
  2344. name = "promisc";
  2345. break;
  2346. default:
  2347. dev_err(dev, "Got invalid cap request rsp %d\n",
  2348. crq->request_capability.capability);
  2349. return;
  2350. }
  2351. switch (crq->request_capability_rsp.rc.code) {
  2352. case SUCCESS:
  2353. break;
  2354. case PARTIALSUCCESS:
  2355. dev_info(dev, "req=%lld, rsp=%ld in %s queue, retrying.\n",
  2356. *req_value,
  2357. (long int)be64_to_cpu(crq->request_capability_rsp.
  2358. number), name);
  2359. release_sub_crqs(adapter);
  2360. *req_value = be64_to_cpu(crq->request_capability_rsp.number);
  2361. ibmvnic_send_req_caps(adapter, 1);
  2362. return;
  2363. default:
  2364. dev_err(dev, "Error %d in request cap rsp\n",
  2365. crq->request_capability_rsp.rc.code);
  2366. return;
  2367. }
  2368. /* Done receiving requested capabilities, query IP offload support */
  2369. if (atomic_read(&adapter->running_cap_crqs) == 0) {
  2370. union ibmvnic_crq newcrq;
  2371. int buf_sz = sizeof(struct ibmvnic_query_ip_offload_buffer);
  2372. struct ibmvnic_query_ip_offload_buffer *ip_offload_buf =
  2373. &adapter->ip_offload_buf;
  2374. adapter->wait_capability = false;
  2375. adapter->ip_offload_tok = dma_map_single(dev, ip_offload_buf,
  2376. buf_sz,
  2377. DMA_FROM_DEVICE);
  2378. if (dma_mapping_error(dev, adapter->ip_offload_tok)) {
  2379. if (!firmware_has_feature(FW_FEATURE_CMO))
  2380. dev_err(dev, "Couldn't map offload buffer\n");
  2381. return;
  2382. }
  2383. memset(&newcrq, 0, sizeof(newcrq));
  2384. newcrq.query_ip_offload.first = IBMVNIC_CRQ_CMD;
  2385. newcrq.query_ip_offload.cmd = QUERY_IP_OFFLOAD;
  2386. newcrq.query_ip_offload.len = cpu_to_be32(buf_sz);
  2387. newcrq.query_ip_offload.ioba =
  2388. cpu_to_be32(adapter->ip_offload_tok);
  2389. ibmvnic_send_crq(adapter, &newcrq);
  2390. }
  2391. }
  2392. static int handle_login_rsp(union ibmvnic_crq *login_rsp_crq,
  2393. struct ibmvnic_adapter *adapter)
  2394. {
  2395. struct device *dev = &adapter->vdev->dev;
  2396. struct ibmvnic_login_rsp_buffer *login_rsp = adapter->login_rsp_buf;
  2397. struct ibmvnic_login_buffer *login = adapter->login_buf;
  2398. int i;
  2399. dma_unmap_single(dev, adapter->login_buf_token, adapter->login_buf_sz,
  2400. DMA_BIDIRECTIONAL);
  2401. dma_unmap_single(dev, adapter->login_rsp_buf_token,
  2402. adapter->login_rsp_buf_sz, DMA_BIDIRECTIONAL);
  2403. /* If the number of queues requested can't be allocated by the
  2404. * server, the login response will return with code 1. We will need
  2405. * to resend the login buffer with fewer queues requested.
  2406. */
  2407. if (login_rsp_crq->generic.rc.code) {
  2408. adapter->renegotiate = true;
  2409. complete(&adapter->init_done);
  2410. return 0;
  2411. }
  2412. netdev_dbg(adapter->netdev, "Login Response Buffer:\n");
  2413. for (i = 0; i < (adapter->login_rsp_buf_sz - 1) / 8 + 1; i++) {
  2414. netdev_dbg(adapter->netdev, "%016lx\n",
  2415. ((unsigned long int *)(adapter->login_rsp_buf))[i]);
  2416. }
  2417. /* Sanity checks */
  2418. if (login->num_txcomp_subcrqs != login_rsp->num_txsubm_subcrqs ||
  2419. (be32_to_cpu(login->num_rxcomp_subcrqs) *
  2420. adapter->req_rx_add_queues !=
  2421. be32_to_cpu(login_rsp->num_rxadd_subcrqs))) {
  2422. dev_err(dev, "FATAL: Inconsistent login and login rsp\n");
  2423. ibmvnic_remove(adapter->vdev);
  2424. return -EIO;
  2425. }
  2426. complete(&adapter->init_done);
  2427. return 0;
  2428. }
  2429. static void handle_request_map_rsp(union ibmvnic_crq *crq,
  2430. struct ibmvnic_adapter *adapter)
  2431. {
  2432. struct device *dev = &adapter->vdev->dev;
  2433. u8 map_id = crq->request_map_rsp.map_id;
  2434. int tx_subcrqs;
  2435. int rx_subcrqs;
  2436. long rc;
  2437. int i;
  2438. tx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_txsubm_subcrqs);
  2439. rx_subcrqs = be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
  2440. rc = crq->request_map_rsp.rc.code;
  2441. if (rc) {
  2442. dev_err(dev, "Error %ld in REQUEST_MAP_RSP\n", rc);
  2443. adapter->map_id--;
  2444. /* need to find and zero tx/rx_pool map_id */
  2445. for (i = 0; i < tx_subcrqs; i++) {
  2446. if (adapter->tx_pool[i].long_term_buff.map_id == map_id)
  2447. adapter->tx_pool[i].long_term_buff.map_id = 0;
  2448. }
  2449. for (i = 0; i < rx_subcrqs; i++) {
  2450. if (adapter->rx_pool[i].long_term_buff.map_id == map_id)
  2451. adapter->rx_pool[i].long_term_buff.map_id = 0;
  2452. }
  2453. }
  2454. complete(&adapter->fw_done);
  2455. }
  2456. static void handle_request_unmap_rsp(union ibmvnic_crq *crq,
  2457. struct ibmvnic_adapter *adapter)
  2458. {
  2459. struct device *dev = &adapter->vdev->dev;
  2460. long rc;
  2461. rc = crq->request_unmap_rsp.rc.code;
  2462. if (rc)
  2463. dev_err(dev, "Error %ld in REQUEST_UNMAP_RSP\n", rc);
  2464. }
  2465. static void handle_query_map_rsp(union ibmvnic_crq *crq,
  2466. struct ibmvnic_adapter *adapter)
  2467. {
  2468. struct net_device *netdev = adapter->netdev;
  2469. struct device *dev = &adapter->vdev->dev;
  2470. long rc;
  2471. rc = crq->query_map_rsp.rc.code;
  2472. if (rc) {
  2473. dev_err(dev, "Error %ld in QUERY_MAP_RSP\n", rc);
  2474. return;
  2475. }
  2476. netdev_dbg(netdev, "page_size = %d\ntot_pages = %d\nfree_pages = %d\n",
  2477. crq->query_map_rsp.page_size, crq->query_map_rsp.tot_pages,
  2478. crq->query_map_rsp.free_pages);
  2479. }
  2480. static void handle_query_cap_rsp(union ibmvnic_crq *crq,
  2481. struct ibmvnic_adapter *adapter)
  2482. {
  2483. struct net_device *netdev = adapter->netdev;
  2484. struct device *dev = &adapter->vdev->dev;
  2485. long rc;
  2486. atomic_dec(&adapter->running_cap_crqs);
  2487. netdev_dbg(netdev, "Outstanding queries: %d\n",
  2488. atomic_read(&adapter->running_cap_crqs));
  2489. rc = crq->query_capability.rc.code;
  2490. if (rc) {
  2491. dev_err(dev, "Error %ld in QUERY_CAP_RSP\n", rc);
  2492. goto out;
  2493. }
  2494. switch (be16_to_cpu(crq->query_capability.capability)) {
  2495. case MIN_TX_QUEUES:
  2496. adapter->min_tx_queues =
  2497. be64_to_cpu(crq->query_capability.number);
  2498. netdev_dbg(netdev, "min_tx_queues = %lld\n",
  2499. adapter->min_tx_queues);
  2500. break;
  2501. case MIN_RX_QUEUES:
  2502. adapter->min_rx_queues =
  2503. be64_to_cpu(crq->query_capability.number);
  2504. netdev_dbg(netdev, "min_rx_queues = %lld\n",
  2505. adapter->min_rx_queues);
  2506. break;
  2507. case MIN_RX_ADD_QUEUES:
  2508. adapter->min_rx_add_queues =
  2509. be64_to_cpu(crq->query_capability.number);
  2510. netdev_dbg(netdev, "min_rx_add_queues = %lld\n",
  2511. adapter->min_rx_add_queues);
  2512. break;
  2513. case MAX_TX_QUEUES:
  2514. adapter->max_tx_queues =
  2515. be64_to_cpu(crq->query_capability.number);
  2516. netdev_dbg(netdev, "max_tx_queues = %lld\n",
  2517. adapter->max_tx_queues);
  2518. break;
  2519. case MAX_RX_QUEUES:
  2520. adapter->max_rx_queues =
  2521. be64_to_cpu(crq->query_capability.number);
  2522. netdev_dbg(netdev, "max_rx_queues = %lld\n",
  2523. adapter->max_rx_queues);
  2524. break;
  2525. case MAX_RX_ADD_QUEUES:
  2526. adapter->max_rx_add_queues =
  2527. be64_to_cpu(crq->query_capability.number);
  2528. netdev_dbg(netdev, "max_rx_add_queues = %lld\n",
  2529. adapter->max_rx_add_queues);
  2530. break;
  2531. case MIN_TX_ENTRIES_PER_SUBCRQ:
  2532. adapter->min_tx_entries_per_subcrq =
  2533. be64_to_cpu(crq->query_capability.number);
  2534. netdev_dbg(netdev, "min_tx_entries_per_subcrq = %lld\n",
  2535. adapter->min_tx_entries_per_subcrq);
  2536. break;
  2537. case MIN_RX_ADD_ENTRIES_PER_SUBCRQ:
  2538. adapter->min_rx_add_entries_per_subcrq =
  2539. be64_to_cpu(crq->query_capability.number);
  2540. netdev_dbg(netdev, "min_rx_add_entrs_per_subcrq = %lld\n",
  2541. adapter->min_rx_add_entries_per_subcrq);
  2542. break;
  2543. case MAX_TX_ENTRIES_PER_SUBCRQ:
  2544. adapter->max_tx_entries_per_subcrq =
  2545. be64_to_cpu(crq->query_capability.number);
  2546. netdev_dbg(netdev, "max_tx_entries_per_subcrq = %lld\n",
  2547. adapter->max_tx_entries_per_subcrq);
  2548. break;
  2549. case MAX_RX_ADD_ENTRIES_PER_SUBCRQ:
  2550. adapter->max_rx_add_entries_per_subcrq =
  2551. be64_to_cpu(crq->query_capability.number);
  2552. netdev_dbg(netdev, "max_rx_add_entrs_per_subcrq = %lld\n",
  2553. adapter->max_rx_add_entries_per_subcrq);
  2554. break;
  2555. case TCP_IP_OFFLOAD:
  2556. adapter->tcp_ip_offload =
  2557. be64_to_cpu(crq->query_capability.number);
  2558. netdev_dbg(netdev, "tcp_ip_offload = %lld\n",
  2559. adapter->tcp_ip_offload);
  2560. break;
  2561. case PROMISC_SUPPORTED:
  2562. adapter->promisc_supported =
  2563. be64_to_cpu(crq->query_capability.number);
  2564. netdev_dbg(netdev, "promisc_supported = %lld\n",
  2565. adapter->promisc_supported);
  2566. break;
  2567. case MIN_MTU:
  2568. adapter->min_mtu = be64_to_cpu(crq->query_capability.number);
  2569. netdev->min_mtu = adapter->min_mtu - ETH_HLEN;
  2570. netdev_dbg(netdev, "min_mtu = %lld\n", adapter->min_mtu);
  2571. break;
  2572. case MAX_MTU:
  2573. adapter->max_mtu = be64_to_cpu(crq->query_capability.number);
  2574. netdev->max_mtu = adapter->max_mtu - ETH_HLEN;
  2575. netdev_dbg(netdev, "max_mtu = %lld\n", adapter->max_mtu);
  2576. break;
  2577. case MAX_MULTICAST_FILTERS:
  2578. adapter->max_multicast_filters =
  2579. be64_to_cpu(crq->query_capability.number);
  2580. netdev_dbg(netdev, "max_multicast_filters = %lld\n",
  2581. adapter->max_multicast_filters);
  2582. break;
  2583. case VLAN_HEADER_INSERTION:
  2584. adapter->vlan_header_insertion =
  2585. be64_to_cpu(crq->query_capability.number);
  2586. if (adapter->vlan_header_insertion)
  2587. netdev->features |= NETIF_F_HW_VLAN_STAG_TX;
  2588. netdev_dbg(netdev, "vlan_header_insertion = %lld\n",
  2589. adapter->vlan_header_insertion);
  2590. break;
  2591. case RX_VLAN_HEADER_INSERTION:
  2592. adapter->rx_vlan_header_insertion =
  2593. be64_to_cpu(crq->query_capability.number);
  2594. netdev_dbg(netdev, "rx_vlan_header_insertion = %lld\n",
  2595. adapter->rx_vlan_header_insertion);
  2596. break;
  2597. case MAX_TX_SG_ENTRIES:
  2598. adapter->max_tx_sg_entries =
  2599. be64_to_cpu(crq->query_capability.number);
  2600. netdev_dbg(netdev, "max_tx_sg_entries = %lld\n",
  2601. adapter->max_tx_sg_entries);
  2602. break;
  2603. case RX_SG_SUPPORTED:
  2604. adapter->rx_sg_supported =
  2605. be64_to_cpu(crq->query_capability.number);
  2606. netdev_dbg(netdev, "rx_sg_supported = %lld\n",
  2607. adapter->rx_sg_supported);
  2608. break;
  2609. case OPT_TX_COMP_SUB_QUEUES:
  2610. adapter->opt_tx_comp_sub_queues =
  2611. be64_to_cpu(crq->query_capability.number);
  2612. netdev_dbg(netdev, "opt_tx_comp_sub_queues = %lld\n",
  2613. adapter->opt_tx_comp_sub_queues);
  2614. break;
  2615. case OPT_RX_COMP_QUEUES:
  2616. adapter->opt_rx_comp_queues =
  2617. be64_to_cpu(crq->query_capability.number);
  2618. netdev_dbg(netdev, "opt_rx_comp_queues = %lld\n",
  2619. adapter->opt_rx_comp_queues);
  2620. break;
  2621. case OPT_RX_BUFADD_Q_PER_RX_COMP_Q:
  2622. adapter->opt_rx_bufadd_q_per_rx_comp_q =
  2623. be64_to_cpu(crq->query_capability.number);
  2624. netdev_dbg(netdev, "opt_rx_bufadd_q_per_rx_comp_q = %lld\n",
  2625. adapter->opt_rx_bufadd_q_per_rx_comp_q);
  2626. break;
  2627. case OPT_TX_ENTRIES_PER_SUBCRQ:
  2628. adapter->opt_tx_entries_per_subcrq =
  2629. be64_to_cpu(crq->query_capability.number);
  2630. netdev_dbg(netdev, "opt_tx_entries_per_subcrq = %lld\n",
  2631. adapter->opt_tx_entries_per_subcrq);
  2632. break;
  2633. case OPT_RXBA_ENTRIES_PER_SUBCRQ:
  2634. adapter->opt_rxba_entries_per_subcrq =
  2635. be64_to_cpu(crq->query_capability.number);
  2636. netdev_dbg(netdev, "opt_rxba_entries_per_subcrq = %lld\n",
  2637. adapter->opt_rxba_entries_per_subcrq);
  2638. break;
  2639. case TX_RX_DESC_REQ:
  2640. adapter->tx_rx_desc_req = crq->query_capability.number;
  2641. netdev_dbg(netdev, "tx_rx_desc_req = %llx\n",
  2642. adapter->tx_rx_desc_req);
  2643. break;
  2644. default:
  2645. netdev_err(netdev, "Got invalid cap rsp %d\n",
  2646. crq->query_capability.capability);
  2647. }
  2648. out:
  2649. if (atomic_read(&adapter->running_cap_crqs) == 0) {
  2650. adapter->wait_capability = false;
  2651. ibmvnic_send_req_caps(adapter, 0);
  2652. }
  2653. }
  2654. static void ibmvnic_handle_crq(union ibmvnic_crq *crq,
  2655. struct ibmvnic_adapter *adapter)
  2656. {
  2657. struct ibmvnic_generic_crq *gen_crq = &crq->generic;
  2658. struct net_device *netdev = adapter->netdev;
  2659. struct device *dev = &adapter->vdev->dev;
  2660. u64 *u64_crq = (u64 *)crq;
  2661. long rc;
  2662. netdev_dbg(netdev, "Handling CRQ: %016lx %016lx\n",
  2663. (unsigned long int)cpu_to_be64(u64_crq[0]),
  2664. (unsigned long int)cpu_to_be64(u64_crq[1]));
  2665. switch (gen_crq->first) {
  2666. case IBMVNIC_CRQ_INIT_RSP:
  2667. switch (gen_crq->cmd) {
  2668. case IBMVNIC_CRQ_INIT:
  2669. dev_info(dev, "Partner initialized\n");
  2670. break;
  2671. case IBMVNIC_CRQ_INIT_COMPLETE:
  2672. dev_info(dev, "Partner initialization complete\n");
  2673. send_version_xchg(adapter);
  2674. break;
  2675. default:
  2676. dev_err(dev, "Unknown crq cmd: %d\n", gen_crq->cmd);
  2677. }
  2678. return;
  2679. case IBMVNIC_CRQ_XPORT_EVENT:
  2680. netif_carrier_off(netdev);
  2681. if (gen_crq->cmd == IBMVNIC_PARTITION_MIGRATED) {
  2682. dev_info(dev, "Migrated, re-enabling adapter\n");
  2683. ibmvnic_reset(adapter, VNIC_RESET_MOBILITY);
  2684. } else if (gen_crq->cmd == IBMVNIC_DEVICE_FAILOVER) {
  2685. dev_info(dev, "Backing device failover detected\n");
  2686. ibmvnic_reset(adapter, VNIC_RESET_FAILOVER);
  2687. } else {
  2688. /* The adapter lost the connection */
  2689. dev_err(dev, "Virtual Adapter failed (rc=%d)\n",
  2690. gen_crq->cmd);
  2691. ibmvnic_reset(adapter, VNIC_RESET_FATAL);
  2692. }
  2693. return;
  2694. case IBMVNIC_CRQ_CMD_RSP:
  2695. break;
  2696. default:
  2697. dev_err(dev, "Got an invalid msg type 0x%02x\n",
  2698. gen_crq->first);
  2699. return;
  2700. }
  2701. switch (gen_crq->cmd) {
  2702. case VERSION_EXCHANGE_RSP:
  2703. rc = crq->version_exchange_rsp.rc.code;
  2704. if (rc) {
  2705. dev_err(dev, "Error %ld in VERSION_EXCHG_RSP\n", rc);
  2706. break;
  2707. }
  2708. dev_info(dev, "Partner protocol version is %d\n",
  2709. crq->version_exchange_rsp.version);
  2710. if (be16_to_cpu(crq->version_exchange_rsp.version) <
  2711. ibmvnic_version)
  2712. ibmvnic_version =
  2713. be16_to_cpu(crq->version_exchange_rsp.version);
  2714. send_cap_queries(adapter);
  2715. break;
  2716. case QUERY_CAPABILITY_RSP:
  2717. handle_query_cap_rsp(crq, adapter);
  2718. break;
  2719. case QUERY_MAP_RSP:
  2720. handle_query_map_rsp(crq, adapter);
  2721. break;
  2722. case REQUEST_MAP_RSP:
  2723. handle_request_map_rsp(crq, adapter);
  2724. break;
  2725. case REQUEST_UNMAP_RSP:
  2726. handle_request_unmap_rsp(crq, adapter);
  2727. break;
  2728. case REQUEST_CAPABILITY_RSP:
  2729. handle_request_cap_rsp(crq, adapter);
  2730. break;
  2731. case LOGIN_RSP:
  2732. netdev_dbg(netdev, "Got Login Response\n");
  2733. handle_login_rsp(crq, adapter);
  2734. break;
  2735. case LOGICAL_LINK_STATE_RSP:
  2736. netdev_dbg(netdev,
  2737. "Got Logical Link State Response, state: %d rc: %d\n",
  2738. crq->logical_link_state_rsp.link_state,
  2739. crq->logical_link_state_rsp.rc.code);
  2740. adapter->logical_link_state =
  2741. crq->logical_link_state_rsp.link_state;
  2742. adapter->init_done_rc = crq->logical_link_state_rsp.rc.code;
  2743. complete(&adapter->init_done);
  2744. break;
  2745. case LINK_STATE_INDICATION:
  2746. netdev_dbg(netdev, "Got Logical Link State Indication\n");
  2747. adapter->phys_link_state =
  2748. crq->link_state_indication.phys_link_state;
  2749. adapter->logical_link_state =
  2750. crq->link_state_indication.logical_link_state;
  2751. break;
  2752. case CHANGE_MAC_ADDR_RSP:
  2753. netdev_dbg(netdev, "Got MAC address change Response\n");
  2754. handle_change_mac_rsp(crq, adapter);
  2755. break;
  2756. case ERROR_INDICATION:
  2757. netdev_dbg(netdev, "Got Error Indication\n");
  2758. handle_error_indication(crq, adapter);
  2759. break;
  2760. case REQUEST_ERROR_RSP:
  2761. netdev_dbg(netdev, "Got Error Detail Response\n");
  2762. handle_error_info_rsp(crq, adapter);
  2763. break;
  2764. case REQUEST_STATISTICS_RSP:
  2765. netdev_dbg(netdev, "Got Statistics Response\n");
  2766. complete(&adapter->stats_done);
  2767. break;
  2768. case QUERY_IP_OFFLOAD_RSP:
  2769. netdev_dbg(netdev, "Got Query IP offload Response\n");
  2770. handle_query_ip_offload_rsp(adapter);
  2771. break;
  2772. case MULTICAST_CTRL_RSP:
  2773. netdev_dbg(netdev, "Got multicast control Response\n");
  2774. break;
  2775. case CONTROL_IP_OFFLOAD_RSP:
  2776. netdev_dbg(netdev, "Got Control IP offload Response\n");
  2777. dma_unmap_single(dev, adapter->ip_offload_ctrl_tok,
  2778. sizeof(adapter->ip_offload_ctrl),
  2779. DMA_TO_DEVICE);
  2780. complete(&adapter->init_done);
  2781. break;
  2782. case COLLECT_FW_TRACE_RSP:
  2783. netdev_dbg(netdev, "Got Collect firmware trace Response\n");
  2784. complete(&adapter->fw_done);
  2785. break;
  2786. default:
  2787. netdev_err(netdev, "Got an invalid cmd type 0x%02x\n",
  2788. gen_crq->cmd);
  2789. }
  2790. }
  2791. static irqreturn_t ibmvnic_interrupt(int irq, void *instance)
  2792. {
  2793. struct ibmvnic_adapter *adapter = instance;
  2794. tasklet_schedule(&adapter->tasklet);
  2795. return IRQ_HANDLED;
  2796. }
  2797. static void ibmvnic_tasklet(void *data)
  2798. {
  2799. struct ibmvnic_adapter *adapter = data;
  2800. struct ibmvnic_crq_queue *queue = &adapter->crq;
  2801. union ibmvnic_crq *crq;
  2802. unsigned long flags;
  2803. bool done = false;
  2804. spin_lock_irqsave(&queue->lock, flags);
  2805. while (!done) {
  2806. /* Pull all the valid messages off the CRQ */
  2807. while ((crq = ibmvnic_next_crq(adapter)) != NULL) {
  2808. ibmvnic_handle_crq(crq, adapter);
  2809. crq->generic.first = 0;
  2810. }
  2811. /* remain in tasklet until all
  2812. * capabilities responses are received
  2813. */
  2814. if (!adapter->wait_capability)
  2815. done = true;
  2816. }
  2817. /* if capabilities CRQ's were sent in this tasklet, the following
  2818. * tasklet must wait until all responses are received
  2819. */
  2820. if (atomic_read(&adapter->running_cap_crqs) != 0)
  2821. adapter->wait_capability = true;
  2822. spin_unlock_irqrestore(&queue->lock, flags);
  2823. }
  2824. static int ibmvnic_reenable_crq_queue(struct ibmvnic_adapter *adapter)
  2825. {
  2826. struct vio_dev *vdev = adapter->vdev;
  2827. int rc;
  2828. do {
  2829. rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address);
  2830. } while (rc == H_IN_PROGRESS || rc == H_BUSY || H_IS_LONG_BUSY(rc));
  2831. if (rc)
  2832. dev_err(&vdev->dev, "Error enabling adapter (rc=%d)\n", rc);
  2833. return rc;
  2834. }
  2835. static int ibmvnic_reset_crq(struct ibmvnic_adapter *adapter)
  2836. {
  2837. struct ibmvnic_crq_queue *crq = &adapter->crq;
  2838. struct device *dev = &adapter->vdev->dev;
  2839. struct vio_dev *vdev = adapter->vdev;
  2840. int rc;
  2841. /* Close the CRQ */
  2842. do {
  2843. rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
  2844. } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
  2845. /* Clean out the queue */
  2846. memset(crq->msgs, 0, PAGE_SIZE);
  2847. crq->cur = 0;
  2848. /* And re-open it again */
  2849. rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
  2850. crq->msg_token, PAGE_SIZE);
  2851. if (rc == H_CLOSED)
  2852. /* Adapter is good, but other end is not ready */
  2853. dev_warn(dev, "Partner adapter not ready\n");
  2854. else if (rc != 0)
  2855. dev_warn(dev, "Couldn't register crq (rc=%d)\n", rc);
  2856. return rc;
  2857. }
  2858. static void release_crq_queue(struct ibmvnic_adapter *adapter)
  2859. {
  2860. struct ibmvnic_crq_queue *crq = &adapter->crq;
  2861. struct vio_dev *vdev = adapter->vdev;
  2862. long rc;
  2863. if (!crq->msgs)
  2864. return;
  2865. netdev_dbg(adapter->netdev, "Releasing CRQ\n");
  2866. free_irq(vdev->irq, adapter);
  2867. tasklet_kill(&adapter->tasklet);
  2868. do {
  2869. rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
  2870. } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
  2871. dma_unmap_single(&vdev->dev, crq->msg_token, PAGE_SIZE,
  2872. DMA_BIDIRECTIONAL);
  2873. free_page((unsigned long)crq->msgs);
  2874. crq->msgs = NULL;
  2875. }
  2876. static int init_crq_queue(struct ibmvnic_adapter *adapter)
  2877. {
  2878. struct ibmvnic_crq_queue *crq = &adapter->crq;
  2879. struct device *dev = &adapter->vdev->dev;
  2880. struct vio_dev *vdev = adapter->vdev;
  2881. int rc, retrc = -ENOMEM;
  2882. if (crq->msgs)
  2883. return 0;
  2884. crq->msgs = (union ibmvnic_crq *)get_zeroed_page(GFP_KERNEL);
  2885. /* Should we allocate more than one page? */
  2886. if (!crq->msgs)
  2887. return -ENOMEM;
  2888. crq->size = PAGE_SIZE / sizeof(*crq->msgs);
  2889. crq->msg_token = dma_map_single(dev, crq->msgs, PAGE_SIZE,
  2890. DMA_BIDIRECTIONAL);
  2891. if (dma_mapping_error(dev, crq->msg_token))
  2892. goto map_failed;
  2893. rc = plpar_hcall_norets(H_REG_CRQ, vdev->unit_address,
  2894. crq->msg_token, PAGE_SIZE);
  2895. if (rc == H_RESOURCE)
  2896. /* maybe kexecing and resource is busy. try a reset */
  2897. rc = ibmvnic_reset_crq(adapter);
  2898. retrc = rc;
  2899. if (rc == H_CLOSED) {
  2900. dev_warn(dev, "Partner adapter not ready\n");
  2901. } else if (rc) {
  2902. dev_warn(dev, "Error %d opening adapter\n", rc);
  2903. goto reg_crq_failed;
  2904. }
  2905. retrc = 0;
  2906. tasklet_init(&adapter->tasklet, (void *)ibmvnic_tasklet,
  2907. (unsigned long)adapter);
  2908. netdev_dbg(adapter->netdev, "registering irq 0x%x\n", vdev->irq);
  2909. rc = request_irq(vdev->irq, ibmvnic_interrupt, 0, IBMVNIC_NAME,
  2910. adapter);
  2911. if (rc) {
  2912. dev_err(dev, "Couldn't register irq 0x%x. rc=%d\n",
  2913. vdev->irq, rc);
  2914. goto req_irq_failed;
  2915. }
  2916. rc = vio_enable_interrupts(vdev);
  2917. if (rc) {
  2918. dev_err(dev, "Error %d enabling interrupts\n", rc);
  2919. goto req_irq_failed;
  2920. }
  2921. crq->cur = 0;
  2922. spin_lock_init(&crq->lock);
  2923. return retrc;
  2924. req_irq_failed:
  2925. tasklet_kill(&adapter->tasklet);
  2926. do {
  2927. rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address);
  2928. } while (rc == H_BUSY || H_IS_LONG_BUSY(rc));
  2929. reg_crq_failed:
  2930. dma_unmap_single(dev, crq->msg_token, PAGE_SIZE, DMA_BIDIRECTIONAL);
  2931. map_failed:
  2932. free_page((unsigned long)crq->msgs);
  2933. crq->msgs = NULL;
  2934. return retrc;
  2935. }
  2936. static int ibmvnic_init(struct ibmvnic_adapter *adapter)
  2937. {
  2938. struct device *dev = &adapter->vdev->dev;
  2939. unsigned long timeout = msecs_to_jiffies(30000);
  2940. int rc;
  2941. rc = init_crq_queue(adapter);
  2942. if (rc) {
  2943. dev_err(dev, "Couldn't initialize crq. rc=%d\n", rc);
  2944. return rc;
  2945. }
  2946. init_completion(&adapter->init_done);
  2947. ibmvnic_send_crq_init(adapter);
  2948. if (!wait_for_completion_timeout(&adapter->init_done, timeout)) {
  2949. dev_err(dev, "Initialization sequence timed out\n");
  2950. release_crq_queue(adapter);
  2951. return -1;
  2952. }
  2953. rc = init_sub_crqs(adapter);
  2954. if (rc) {
  2955. dev_err(dev, "Initialization of sub crqs failed\n");
  2956. release_crq_queue(adapter);
  2957. }
  2958. return rc;
  2959. }
  2960. static int ibmvnic_probe(struct vio_dev *dev, const struct vio_device_id *id)
  2961. {
  2962. struct ibmvnic_adapter *adapter;
  2963. struct net_device *netdev;
  2964. unsigned char *mac_addr_p;
  2965. int rc;
  2966. dev_dbg(&dev->dev, "entering ibmvnic_probe for UA 0x%x\n",
  2967. dev->unit_address);
  2968. mac_addr_p = (unsigned char *)vio_get_attribute(dev,
  2969. VETH_MAC_ADDR, NULL);
  2970. if (!mac_addr_p) {
  2971. dev_err(&dev->dev,
  2972. "(%s:%3.3d) ERROR: Can't find MAC_ADDR attribute\n",
  2973. __FILE__, __LINE__);
  2974. return 0;
  2975. }
  2976. netdev = alloc_etherdev_mq(sizeof(struct ibmvnic_adapter),
  2977. IBMVNIC_MAX_TX_QUEUES);
  2978. if (!netdev)
  2979. return -ENOMEM;
  2980. adapter = netdev_priv(netdev);
  2981. adapter->state = VNIC_PROBING;
  2982. dev_set_drvdata(&dev->dev, netdev);
  2983. adapter->vdev = dev;
  2984. adapter->netdev = netdev;
  2985. ether_addr_copy(adapter->mac_addr, mac_addr_p);
  2986. ether_addr_copy(netdev->dev_addr, adapter->mac_addr);
  2987. netdev->irq = dev->irq;
  2988. netdev->netdev_ops = &ibmvnic_netdev_ops;
  2989. netdev->ethtool_ops = &ibmvnic_ethtool_ops;
  2990. SET_NETDEV_DEV(netdev, &dev->dev);
  2991. spin_lock_init(&adapter->stats_lock);
  2992. INIT_LIST_HEAD(&adapter->errors);
  2993. spin_lock_init(&adapter->error_list_lock);
  2994. INIT_WORK(&adapter->ibmvnic_reset, __ibmvnic_reset);
  2995. INIT_LIST_HEAD(&adapter->rwi_list);
  2996. mutex_init(&adapter->reset_lock);
  2997. mutex_init(&adapter->rwi_lock);
  2998. adapter->resetting = false;
  2999. rc = ibmvnic_init(adapter);
  3000. if (rc) {
  3001. free_netdev(netdev);
  3002. return rc;
  3003. }
  3004. netdev->mtu = adapter->req_mtu - ETH_HLEN;
  3005. rc = register_netdev(netdev);
  3006. if (rc) {
  3007. dev_err(&dev->dev, "failed to register netdev rc=%d\n", rc);
  3008. free_netdev(netdev);
  3009. return rc;
  3010. }
  3011. dev_info(&dev->dev, "ibmvnic registered\n");
  3012. adapter->state = VNIC_PROBED;
  3013. return 0;
  3014. }
  3015. static int ibmvnic_remove(struct vio_dev *dev)
  3016. {
  3017. struct net_device *netdev = dev_get_drvdata(&dev->dev);
  3018. struct ibmvnic_adapter *adapter = netdev_priv(netdev);
  3019. adapter->state = VNIC_REMOVING;
  3020. unregister_netdev(netdev);
  3021. mutex_lock(&adapter->reset_lock);
  3022. release_resources(adapter);
  3023. release_sub_crqs(adapter);
  3024. release_crq_queue(adapter);
  3025. adapter->state = VNIC_REMOVED;
  3026. mutex_unlock(&adapter->reset_lock);
  3027. free_netdev(netdev);
  3028. dev_set_drvdata(&dev->dev, NULL);
  3029. return 0;
  3030. }
  3031. static unsigned long ibmvnic_get_desired_dma(struct vio_dev *vdev)
  3032. {
  3033. struct net_device *netdev = dev_get_drvdata(&vdev->dev);
  3034. struct ibmvnic_adapter *adapter;
  3035. struct iommu_table *tbl;
  3036. unsigned long ret = 0;
  3037. int i;
  3038. tbl = get_iommu_table_base(&vdev->dev);
  3039. /* netdev inits at probe time along with the structures we need below*/
  3040. if (!netdev)
  3041. return IOMMU_PAGE_ALIGN(IBMVNIC_IO_ENTITLEMENT_DEFAULT, tbl);
  3042. adapter = netdev_priv(netdev);
  3043. ret += PAGE_SIZE; /* the crq message queue */
  3044. ret += IOMMU_PAGE_ALIGN(sizeof(struct ibmvnic_statistics), tbl);
  3045. for (i = 0; i < adapter->req_tx_queues + adapter->req_rx_queues; i++)
  3046. ret += 4 * PAGE_SIZE; /* the scrq message queue */
  3047. for (i = 0; i < be32_to_cpu(adapter->login_rsp_buf->num_rxadd_subcrqs);
  3048. i++)
  3049. ret += adapter->rx_pool[i].size *
  3050. IOMMU_PAGE_ALIGN(adapter->rx_pool[i].buff_size, tbl);
  3051. return ret;
  3052. }
  3053. static int ibmvnic_resume(struct device *dev)
  3054. {
  3055. struct net_device *netdev = dev_get_drvdata(dev);
  3056. struct ibmvnic_adapter *adapter = netdev_priv(netdev);
  3057. int i;
  3058. /* kick the interrupt handlers just in case we lost an interrupt */
  3059. for (i = 0; i < adapter->req_rx_queues; i++)
  3060. ibmvnic_interrupt_rx(adapter->rx_scrq[i]->irq,
  3061. adapter->rx_scrq[i]);
  3062. return 0;
  3063. }
  3064. static struct vio_device_id ibmvnic_device_table[] = {
  3065. {"network", "IBM,vnic"},
  3066. {"", "" }
  3067. };
  3068. MODULE_DEVICE_TABLE(vio, ibmvnic_device_table);
  3069. static const struct dev_pm_ops ibmvnic_pm_ops = {
  3070. .resume = ibmvnic_resume
  3071. };
  3072. static struct vio_driver ibmvnic_driver = {
  3073. .id_table = ibmvnic_device_table,
  3074. .probe = ibmvnic_probe,
  3075. .remove = ibmvnic_remove,
  3076. .get_desired_dma = ibmvnic_get_desired_dma,
  3077. .name = ibmvnic_driver_name,
  3078. .pm = &ibmvnic_pm_ops,
  3079. };
  3080. /* module functions */
  3081. static int __init ibmvnic_module_init(void)
  3082. {
  3083. pr_info("%s: %s %s\n", ibmvnic_driver_name, ibmvnic_driver_string,
  3084. IBMVNIC_DRIVER_VERSION);
  3085. return vio_register_driver(&ibmvnic_driver);
  3086. }
  3087. static void __exit ibmvnic_module_exit(void)
  3088. {
  3089. vio_unregister_driver(&ibmvnic_driver);
  3090. }
  3091. module_init(ibmvnic_module_init);
  3092. module_exit(ibmvnic_module_exit);